]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/config/tc-i386.c
x86: extend FPU test coverage for AT&T / Intel mnemonic differences
[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
3bfea8ba
L
467/* To be indexed by segment register number. */
468static const unsigned char i386_seg_prefixes[] = {
469 ES_PREFIX_OPCODE,
470 CS_PREFIX_OPCODE,
471 SS_PREFIX_OPCODE,
472 DS_PREFIX_OPCODE,
473 FS_PREFIX_OPCODE,
474 GS_PREFIX_OPCODE
475};
476
252b5132
RH
477/* List of chars besides those in app.c:symbol_chars that can start an
478 operand. Used to prevent the scrubber eating vital white-space. */
86fa6981 479const char extra_symbol_chars[] = "*%-([{}"
252b5132 480#ifdef LEX_AT
32137342
NC
481 "@"
482#endif
483#ifdef LEX_QM
484 "?"
252b5132 485#endif
32137342 486 ;
252b5132 487
b3983e5f
JB
488#if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
489 && !defined (TE_GNU) \
490 && !defined (TE_LINUX) \
d85e70a3 491 && !defined (TE_Haiku) \
b3983e5f
JB
492 && !defined (TE_FreeBSD) \
493 && !defined (TE_DragonFly) \
494 && !defined (TE_NetBSD))
252b5132 495/* This array holds the chars that always start a comment. If the
b3b91714
AM
496 pre-processor is disabled, these aren't very useful. The option
497 --divide will remove '/' from this list. */
498const char *i386_comment_chars = "#/";
499#define SVR4_COMMENT_CHARS 1
252b5132 500#define PREFIX_SEPARATOR '\\'
252b5132 501
b3b91714
AM
502#else
503const char *i386_comment_chars = "#";
504#define PREFIX_SEPARATOR '/'
505#endif
506
252b5132
RH
507/* This array holds the chars that only start a comment at the beginning of
508 a line. If the line seems to have the form '# 123 filename'
ce8a8b2f
AM
509 .line and .file directives will appear in the pre-processed output.
510 Note that input_file.c hand checks for '#' at the beginning of the
252b5132 511 first line of the input file. This is because the compiler outputs
ce8a8b2f
AM
512 #NO_APP at the beginning of its output.
513 Also note that comments started like this one will always work if
252b5132 514 '/' isn't otherwise defined. */
b3b91714 515const char line_comment_chars[] = "#/";
252b5132 516
63a0b638 517const char line_separator_chars[] = ";";
252b5132 518
ce8a8b2f
AM
519/* Chars that can be used to separate mant from exp in floating point
520 nums. */
252b5132
RH
521const char EXP_CHARS[] = "eE";
522
ce8a8b2f
AM
523/* Chars that mean this number is a floating point constant
524 As in 0f12.456
525 or 0d1.2345e12. */
de133cf9 526const char FLT_CHARS[] = "fFdDxXhHbB";
252b5132 527
ce8a8b2f 528/* Tables for lexical analysis. */
252b5132
RH
529static char mnemonic_chars[256];
530static char register_chars[256];
531static char operand_chars[256];
532static char identifier_chars[256];
252b5132 533
ce8a8b2f 534/* Lexical macros. */
252b5132
RH
535#define is_mnemonic_char(x) (mnemonic_chars[(unsigned char) x])
536#define is_operand_char(x) (operand_chars[(unsigned char) x])
537#define is_register_char(x) (register_chars[(unsigned char) x])
538#define is_space_char(x) ((x) == ' ')
539#define is_identifier_char(x) (identifier_chars[(unsigned char) x])
252b5132 540
0234cb7c 541/* All non-digit non-letter characters that may occur in an operand. */
252b5132
RH
542static char operand_special_chars[] = "%$-+(,)*._~/<>|&^!:[@]";
543
544/* md_assemble() always leaves the strings it's passed unaltered. To
545 effect this we maintain a stack of saved characters that we've smashed
546 with '\0's (indicating end of strings for various sub-fields of the
47926f60 547 assembler instruction). */
252b5132 548static char save_stack[32];
ce8a8b2f 549static char *save_stack_p;
252b5132
RH
550#define END_STRING_AND_SAVE(s) \
551 do { *save_stack_p++ = *(s); *(s) = '\0'; } while (0)
552#define RESTORE_END_STRING(s) \
553 do { *(s) = *--save_stack_p; } while (0)
554
47926f60 555/* The instruction we're assembling. */
252b5132
RH
556static i386_insn i;
557
558/* Possible templates for current insn. */
559static const templates *current_templates;
560
31b2323c
L
561/* Per instruction expressionS buffers: max displacements & immediates. */
562static expressionS disp_expressions[MAX_MEMORY_OPERANDS];
563static expressionS im_expressions[MAX_IMMEDIATE_OPERANDS];
252b5132 564
47926f60 565/* Current operand we are working on. */
ee86248c 566static int this_operand = -1;
252b5132 567
3e73aa7c
JH
568/* We support four different modes. FLAG_CODE variable is used to distinguish
569 these. */
570
571enum flag_code {
572 CODE_32BIT,
573 CODE_16BIT,
574 CODE_64BIT };
575
576static enum flag_code flag_code;
4fa24527 577static unsigned int object_64bit;
862be3fb 578static unsigned int disallow_64bit_reloc;
3e73aa7c 579static int use_rela_relocations = 0;
e379e5f3
L
580/* __tls_get_addr/___tls_get_addr symbol for TLS. */
581static const char *tls_get_addr;
3e73aa7c 582
7af8ed2d
NC
583#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
584 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
585 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
586
351f65ca
L
587/* The ELF ABI to use. */
588enum x86_elf_abi
589{
590 I386_ABI,
7f56bc95
L
591 X86_64_ABI,
592 X86_64_X32_ABI
351f65ca
L
593};
594
595static enum x86_elf_abi x86_elf_abi = I386_ABI;
7af8ed2d 596#endif
351f65ca 597
167ad85b
TG
598#if defined (TE_PE) || defined (TE_PEP)
599/* Use big object file format. */
600static int use_big_obj = 0;
601#endif
602
8dcea932
L
603#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
604/* 1 if generating code for a shared library. */
605static int shared = 0;
b52c4ee4
IB
606
607unsigned int x86_sframe_cfa_sp_reg;
608/* The other CFA base register for SFrame unwind info. */
609unsigned int x86_sframe_cfa_fp_reg;
610unsigned int x86_sframe_cfa_ra_reg;
611
8dcea932
L
612#endif
613
47926f60
KH
614/* 1 for intel syntax,
615 0 if att syntax. */
616static int intel_syntax = 0;
252b5132 617
4b5aaf5f
L
618static enum x86_64_isa
619{
620 amd64 = 1, /* AMD64 ISA. */
621 intel64 /* Intel64 ISA. */
622} isa64;
e89c5eaa 623
1efbbeb4
L
624/* 1 for intel mnemonic,
625 0 if att mnemonic. */
626static int intel_mnemonic = !SYSV386_COMPAT;
627
a60de03c
JB
628/* 1 if pseudo registers are permitted. */
629static int allow_pseudo_reg = 0;
630
47926f60
KH
631/* 1 if register prefix % not required. */
632static int allow_naked_reg = 0;
252b5132 633
33eaf5de 634/* 1 if the assembler should add BND prefix for all control-transferring
7e8b059b
L
635 instructions supporting it, even if this prefix wasn't specified
636 explicitly. */
637static int add_bnd_prefix = 0;
638
ba104c83 639/* 1 if pseudo index register, eiz/riz, is allowed . */
db51cc60
L
640static int allow_index_reg = 0;
641
d022bddd
IT
642/* 1 if the assembler should ignore LOCK prefix, even if it was
643 specified explicitly. */
644static int omit_lock_prefix = 0;
645
e4e00185
AS
646/* 1 if the assembler should encode lfence, mfence, and sfence as
647 "lock addl $0, (%{re}sp)". */
648static int avoid_fence = 0;
649
ae531041
L
650/* 1 if lfence should be inserted after every load. */
651static int lfence_after_load = 0;
652
653/* Non-zero if lfence should be inserted before indirect branch. */
654static enum lfence_before_indirect_branch_kind
655 {
656 lfence_branch_none = 0,
657 lfence_branch_register,
658 lfence_branch_memory,
659 lfence_branch_all
660 }
661lfence_before_indirect_branch;
662
663/* Non-zero if lfence should be inserted before ret. */
664static enum lfence_before_ret_kind
665 {
666 lfence_before_ret_none = 0,
667 lfence_before_ret_not,
a09f656b 668 lfence_before_ret_or,
669 lfence_before_ret_shl
ae531041
L
670 }
671lfence_before_ret;
672
673/* Types of previous instruction is .byte or prefix. */
e379e5f3
L
674static struct
675 {
676 segT seg;
677 const char *file;
678 const char *name;
679 unsigned int line;
680 enum last_insn_kind
681 {
682 last_insn_other = 0,
683 last_insn_directive,
684 last_insn_prefix
685 } kind;
686 } last_insn;
687
0cb4071e
L
688/* 1 if the assembler should generate relax relocations. */
689
690static int generate_relax_relocations
691 = DEFAULT_GENERATE_X86_RELAX_RELOCATIONS;
692
7bab8ab5 693static enum check_kind
daf50ae7 694 {
7bab8ab5
JB
695 check_none = 0,
696 check_warning,
697 check_error
daf50ae7 698 }
7bab8ab5 699sse_check, operand_check = check_warning;
daf50ae7 700
e379e5f3
L
701/* Non-zero if branches should be aligned within power of 2 boundary. */
702static int align_branch_power = 0;
703
704/* Types of branches to align. */
705enum align_branch_kind
706 {
707 align_branch_none = 0,
708 align_branch_jcc = 1,
709 align_branch_fused = 2,
710 align_branch_jmp = 3,
711 align_branch_call = 4,
712 align_branch_indirect = 5,
713 align_branch_ret = 6
714 };
715
716/* Type bits of branches to align. */
717enum align_branch_bit
718 {
719 align_branch_jcc_bit = 1 << align_branch_jcc,
720 align_branch_fused_bit = 1 << align_branch_fused,
721 align_branch_jmp_bit = 1 << align_branch_jmp,
722 align_branch_call_bit = 1 << align_branch_call,
723 align_branch_indirect_bit = 1 << align_branch_indirect,
724 align_branch_ret_bit = 1 << align_branch_ret
725 };
726
727static unsigned int align_branch = (align_branch_jcc_bit
728 | align_branch_fused_bit
729 | align_branch_jmp_bit);
730
79d72f45
HL
731/* Types of condition jump used by macro-fusion. */
732enum mf_jcc_kind
733 {
734 mf_jcc_jo = 0, /* base opcode 0x70 */
735 mf_jcc_jc, /* base opcode 0x72 */
736 mf_jcc_je, /* base opcode 0x74 */
737 mf_jcc_jna, /* base opcode 0x76 */
738 mf_jcc_js, /* base opcode 0x78 */
739 mf_jcc_jp, /* base opcode 0x7a */
740 mf_jcc_jl, /* base opcode 0x7c */
741 mf_jcc_jle, /* base opcode 0x7e */
742 };
743
744/* Types of compare flag-modifying insntructions used by macro-fusion. */
745enum mf_cmp_kind
746 {
747 mf_cmp_test_and, /* test/cmp */
748 mf_cmp_alu_cmp, /* add/sub/cmp */
749 mf_cmp_incdec /* inc/dec */
750 };
751
e379e5f3
L
752/* The maximum padding size for fused jcc. CMP like instruction can
753 be 9 bytes and jcc can be 6 bytes. Leave room just in case for
754 prefixes. */
755#define MAX_FUSED_JCC_PADDING_SIZE 20
756
757/* The maximum number of prefixes added for an instruction. */
758static unsigned int align_branch_prefix_size = 5;
759
b6f8c7c4
L
760/* Optimization:
761 1. Clear the REX_W bit with register operand if possible.
762 2. Above plus use 128bit vector instruction to clear the full vector
763 register.
764 */
765static int optimize = 0;
766
767/* Optimization:
768 1. Clear the REX_W bit with register operand if possible.
769 2. Above plus use 128bit vector instruction to clear the full vector
770 register.
771 3. Above plus optimize "test{q,l,w} $imm8,%r{64,32,16}" to
772 "testb $imm7,%r8".
773 */
774static int optimize_for_space = 0;
775
2ca3ace5
L
776/* Register prefix used for error message. */
777static const char *register_prefix = "%";
778
47926f60
KH
779/* Used in 16 bit gcc mode to add an l suffix to call, ret, enter,
780 leave, push, and pop instructions so that gcc has the same stack
781 frame as in 32 bit mode. */
782static char stackop_size = '\0';
eecb386c 783
12b55ccc
L
784/* Non-zero to optimize code alignment. */
785int optimize_align_code = 1;
786
47926f60
KH
787/* Non-zero to quieten some warnings. */
788static int quiet_warnings = 0;
a38cf1db 789
d59a54c2
JB
790/* Guard to avoid repeated warnings about non-16-bit code on 16-bit CPUs. */
791static bool pre_386_16bit_warned;
792
47926f60
KH
793/* CPU name. */
794static const char *cpu_arch_name = NULL;
6305a203 795static char *cpu_sub_arch_name = NULL;
a38cf1db 796
47926f60 797/* CPU feature flags. */
40fb9820
L
798static i386_cpu_flags cpu_arch_flags = CPU_UNKNOWN_FLAGS;
799
ccc9c027
L
800/* If we have selected a cpu we are generating instructions for. */
801static int cpu_arch_tune_set = 0;
802
9103f4f4 803/* Cpu we are generating instructions for. */
fbf3f584 804enum processor_type cpu_arch_tune = PROCESSOR_UNKNOWN;
9103f4f4
L
805
806/* CPU feature flags of cpu we are generating instructions for. */
40fb9820 807static i386_cpu_flags cpu_arch_tune_flags;
9103f4f4 808
ccc9c027 809/* CPU instruction set architecture used. */
fbf3f584 810enum processor_type cpu_arch_isa = PROCESSOR_UNKNOWN;
ccc9c027 811
9103f4f4 812/* CPU feature flags of instruction set architecture used. */
fbf3f584 813i386_cpu_flags cpu_arch_isa_flags;
9103f4f4 814
fddf5b5b
AM
815/* If set, conditional jumps are not automatically promoted to handle
816 larger than a byte offset. */
f68697e8 817static bool no_cond_jump_promotion = false;
fddf5b5b 818
c0f3af97
L
819/* Encode SSE instructions with VEX prefix. */
820static unsigned int sse2avx;
821
c8480b58
L
822/* Encode aligned vector move as unaligned vector move. */
823static unsigned int use_unaligned_vector_move;
824
539f890d
L
825/* Encode scalar AVX instructions with specific vector length. */
826static enum
827 {
828 vex128 = 0,
829 vex256
830 } avxscalar;
831
03751133
L
832/* Encode VEX WIG instructions with specific vex.w. */
833static enum
834 {
835 vexw0 = 0,
836 vexw1
837 } vexwig;
838
43234a1e
L
839/* Encode scalar EVEX LIG instructions with specific vector length. */
840static enum
841 {
842 evexl128 = 0,
843 evexl256,
844 evexl512
845 } evexlig;
846
847/* Encode EVEX WIG instructions with specific evex.w. */
848static enum
849 {
850 evexw0 = 0,
851 evexw1
852 } evexwig;
853
d3d3c6db
IT
854/* Value to encode in EVEX RC bits, for SAE-only instructions. */
855static enum rc_type evexrcig = rne;
856
29b0f896 857/* Pre-defined "_GLOBAL_OFFSET_TABLE_". */
87c245cc 858static symbolS *GOT_symbol;
29b0f896 859
a4447b93
RH
860/* The dwarf2 return column, adjusted for 32 or 64 bit. */
861unsigned int x86_dwarf2_return_column;
862
863/* The dwarf2 data alignment, adjusted for 32 or 64 bit. */
864int x86_cie_data_alignment;
865
252b5132 866/* Interface to relax_segment.
fddf5b5b
AM
867 There are 3 major relax states for 386 jump insns because the
868 different types of jumps add different sizes to frags when we're
e379e5f3
L
869 figuring out what sort of jump to choose to reach a given label.
870
871 BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING are used to align
872 branches which are handled by md_estimate_size_before_relax() and
873 i386_generic_table_relax_frag(). */
252b5132 874
47926f60 875/* Types. */
93c2a809
AM
876#define UNCOND_JUMP 0
877#define COND_JUMP 1
878#define COND_JUMP86 2
e379e5f3
L
879#define BRANCH_PADDING 3
880#define BRANCH_PREFIX 4
881#define FUSED_JCC_PADDING 5
fddf5b5b 882
47926f60 883/* Sizes. */
252b5132
RH
884#define CODE16 1
885#define SMALL 0
29b0f896 886#define SMALL16 (SMALL | CODE16)
252b5132 887#define BIG 2
29b0f896 888#define BIG16 (BIG | CODE16)
252b5132
RH
889
890#ifndef INLINE
891#ifdef __GNUC__
892#define INLINE __inline__
893#else
894#define INLINE
895#endif
896#endif
897
fddf5b5b
AM
898#define ENCODE_RELAX_STATE(type, size) \
899 ((relax_substateT) (((type) << 2) | (size)))
900#define TYPE_FROM_RELAX_STATE(s) \
901 ((s) >> 2)
902#define DISP_SIZE_FROM_RELAX_STATE(s) \
903 ((((s) & 3) == BIG ? 4 : (((s) & 3) == BIG16 ? 2 : 1)))
252b5132
RH
904
905/* This table is used by relax_frag to promote short jumps to long
906 ones where necessary. SMALL (short) jumps may be promoted to BIG
907 (32 bit long) ones, and SMALL16 jumps to BIG16 (16 bit long). We
908 don't allow a short jump in a 32 bit code segment to be promoted to
909 a 16 bit offset jump because it's slower (requires data size
910 prefix), and doesn't work, unless the destination is in the bottom
911 64k of the code segment (The top 16 bits of eip are zeroed). */
912
913const relax_typeS md_relax_table[] =
914{
24eab124
AM
915 /* The fields are:
916 1) most positive reach of this state,
917 2) most negative reach of this state,
93c2a809 918 3) how many bytes this mode will have in the variable part of the frag
ce8a8b2f 919 4) which index into the table to try if we can't fit into this one. */
252b5132 920
fddf5b5b 921 /* UNCOND_JUMP states. */
93c2a809
AM
922 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG)},
923 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16)},
924 /* dword jmp adds 4 bytes to frag:
925 0 extra opcode bytes, 4 displacement bytes. */
252b5132 926 {0, 0, 4, 0},
93c2a809
AM
927 /* word jmp adds 2 byte2 to frag:
928 0 extra opcode bytes, 2 displacement bytes. */
252b5132
RH
929 {0, 0, 2, 0},
930
93c2a809
AM
931 /* COND_JUMP states. */
932 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG)},
933 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG16)},
934 /* dword conditionals adds 5 bytes to frag:
935 1 extra opcode byte, 4 displacement bytes. */
936 {0, 0, 5, 0},
fddf5b5b 937 /* word conditionals add 3 bytes to frag:
93c2a809
AM
938 1 extra opcode byte, 2 displacement bytes. */
939 {0, 0, 3, 0},
940
941 /* COND_JUMP86 states. */
942 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG)},
943 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG16)},
944 /* dword conditionals adds 5 bytes to frag:
945 1 extra opcode byte, 4 displacement bytes. */
946 {0, 0, 5, 0},
947 /* word conditionals add 4 bytes to frag:
948 1 displacement byte and a 3 byte long branch insn. */
949 {0, 0, 4, 0}
252b5132
RH
950};
951
6ceeed25 952#define ARCH(n, t, f, s) \
ae89daec
JB
953 { STRING_COMMA_LEN (#n), s, PROCESSOR_ ## t, CPU_ ## f ## _FLAGS, \
954 CPU_NONE_FLAGS }
955#define SUBARCH(n, e, d, s) \
956 { STRING_COMMA_LEN (#n), s, PROCESSOR_NONE, CPU_ ## e ## _FLAGS, \
957 CPU_ ## d ## _FLAGS }
6ceeed25 958
9103f4f4
L
959static const arch_entry cpu_arch[] =
960{
3ce2ebcf
JB
961 /* Do not replace the first two entries - i386_target_format() and
962 set_cpu_arch() rely on them being there in this order. */
6ceeed25
JB
963 ARCH (generic32, GENERIC32, GENERIC32, false),
964 ARCH (generic64, GENERIC64, GENERIC64, false),
965 ARCH (i8086, UNKNOWN, NONE, false),
966 ARCH (i186, UNKNOWN, I186, false),
967 ARCH (i286, UNKNOWN, I286, false),
968 ARCH (i386, I386, I386, false),
969 ARCH (i486, I486, I486, false),
970 ARCH (i586, PENTIUM, I586, false),
971 ARCH (i686, PENTIUMPRO, I686, false),
972 ARCH (pentium, PENTIUM, I586, false),
973 ARCH (pentiumpro, PENTIUMPRO, PENTIUMPRO, false),
974 ARCH (pentiumii, PENTIUMPRO, P2, false),
975 ARCH (pentiumiii, PENTIUMPRO, P3, false),
976 ARCH (pentium4, PENTIUM4, P4, false),
977 ARCH (prescott, NOCONA, CORE, false),
978 ARCH (nocona, NOCONA, NOCONA, false),
979 ARCH (yonah, CORE, CORE, true),
980 ARCH (core, CORE, CORE, false),
981 ARCH (merom, CORE2, CORE2, true),
982 ARCH (core2, CORE2, CORE2, false),
983 ARCH (corei7, COREI7, COREI7, false),
984 ARCH (iamcu, IAMCU, IAMCU, false),
985 ARCH (k6, K6, K6, false),
986 ARCH (k6_2, K6, K6_2, false),
987 ARCH (athlon, ATHLON, ATHLON, false),
988 ARCH (sledgehammer, K8, K8, true),
989 ARCH (opteron, K8, K8, false),
990 ARCH (k8, K8, K8, false),
991 ARCH (amdfam10, AMDFAM10, AMDFAM10, false),
992 ARCH (bdver1, BD, BDVER1, false),
993 ARCH (bdver2, BD, BDVER2, false),
994 ARCH (bdver3, BD, BDVER3, false),
995 ARCH (bdver4, BD, BDVER4, false),
996 ARCH (znver1, ZNVER, ZNVER1, false),
997 ARCH (znver2, ZNVER, ZNVER2, false),
998 ARCH (znver3, ZNVER, ZNVER3, false),
b0e8fa7f 999 ARCH (znver4, ZNVER, ZNVER4, false),
6ceeed25
JB
1000 ARCH (btver1, BT, BTVER1, false),
1001 ARCH (btver2, BT, BTVER2, false),
1002
ae89daec
JB
1003 SUBARCH (8087, 8087, ANY_X87, false),
1004 SUBARCH (87, NONE, ANY_X87, false), /* Disable only! */
1005 SUBARCH (287, 287, ANY_287, false),
1006 SUBARCH (387, 387, ANY_387, false),
1007 SUBARCH (687, 687, ANY_687, false),
1008 SUBARCH (cmov, CMOV, ANY_CMOV, false),
1009 SUBARCH (fxsr, FXSR, ANY_FXSR, false),
1010 SUBARCH (mmx, MMX, ANY_MMX, false),
1011 SUBARCH (sse, SSE, ANY_SSE, false),
1012 SUBARCH (sse2, SSE2, ANY_SSE2, false),
1013 SUBARCH (sse3, SSE3, ANY_SSE3, false),
1014 SUBARCH (sse4a, SSE4A, ANY_SSE4A, false),
1015 SUBARCH (ssse3, SSSE3, ANY_SSSE3, false),
1016 SUBARCH (sse4.1, SSE4_1, ANY_SSE4_1, false),
1017 SUBARCH (sse4.2, SSE4_2, ANY_SSE4_2, false),
1018 SUBARCH (sse4, SSE4_2, ANY_SSE4_1, false),
1019 SUBARCH (avx, AVX, ANY_AVX, false),
1020 SUBARCH (avx2, AVX2, ANY_AVX2, false),
1021 SUBARCH (avx512f, AVX512F, ANY_AVX512F, false),
1022 SUBARCH (avx512cd, AVX512CD, ANY_AVX512CD, false),
1023 SUBARCH (avx512er, AVX512ER, ANY_AVX512ER, false),
1024 SUBARCH (avx512pf, AVX512PF, ANY_AVX512PF, false),
1025 SUBARCH (avx512dq, AVX512DQ, ANY_AVX512DQ, false),
1026 SUBARCH (avx512bw, AVX512BW, ANY_AVX512BW, false),
1027 SUBARCH (avx512vl, AVX512VL, ANY_AVX512VL, false),
1028 SUBARCH (vmx, VMX, VMX, false),
1029 SUBARCH (vmfunc, VMFUNC, VMFUNC, false),
1030 SUBARCH (smx, SMX, SMX, false),
1031 SUBARCH (xsave, XSAVE, XSAVE, false),
1032 SUBARCH (xsaveopt, XSAVEOPT, XSAVEOPT, false),
1033 SUBARCH (xsavec, XSAVEC, XSAVEC, false),
1034 SUBARCH (xsaves, XSAVES, XSAVES, false),
1035 SUBARCH (aes, AES, AES, false),
1036 SUBARCH (pclmul, PCLMUL, PCLMUL, false),
1037 SUBARCH (clmul, PCLMUL, PCLMUL, true),
1038 SUBARCH (fsgsbase, FSGSBASE, FSGSBASE, false),
1039 SUBARCH (rdrnd, RDRND, RDRND, false),
1040 SUBARCH (f16c, F16C, F16C, false),
1041 SUBARCH (bmi2, BMI2, BMI2, false),
1042 SUBARCH (fma, FMA, FMA, false),
1043 SUBARCH (fma4, FMA4, FMA4, false),
1044 SUBARCH (xop, XOP, XOP, false),
1045 SUBARCH (lwp, LWP, LWP, false),
1046 SUBARCH (movbe, MOVBE, MOVBE, false),
1047 SUBARCH (cx16, CX16, CX16, false),
1048 SUBARCH (ept, EPT, EPT, false),
1049 SUBARCH (lzcnt, LZCNT, LZCNT, false),
1050 SUBARCH (popcnt, POPCNT, POPCNT, false),
1051 SUBARCH (hle, HLE, HLE, false),
1052 SUBARCH (rtm, RTM, RTM, false),
1053 SUBARCH (invpcid, INVPCID, INVPCID, false),
1054 SUBARCH (clflush, CLFLUSH, CLFLUSH, false),
1055 SUBARCH (nop, NOP, NOP, false),
1056 SUBARCH (syscall, SYSCALL, SYSCALL, false),
1057 SUBARCH (rdtscp, RDTSCP, RDTSCP, false),
1058 SUBARCH (3dnow, 3DNOW, 3DNOW, false),
1059 SUBARCH (3dnowa, 3DNOWA, 3DNOWA, false),
1060 SUBARCH (padlock, PADLOCK, PADLOCK, false),
1061 SUBARCH (pacifica, SVME, SVME, true),
1062 SUBARCH (svme, SVME, SVME, false),
ae89daec
JB
1063 SUBARCH (abm, ABM, ABM, false),
1064 SUBARCH (bmi, BMI, BMI, false),
1065 SUBARCH (tbm, TBM, TBM, false),
1066 SUBARCH (adx, ADX, ADX, false),
1067 SUBARCH (rdseed, RDSEED, RDSEED, false),
1068 SUBARCH (prfchw, PRFCHW, PRFCHW, false),
1069 SUBARCH (smap, SMAP, SMAP, false),
1070 SUBARCH (mpx, MPX, MPX, false),
1071 SUBARCH (sha, SHA, SHA, false),
1072 SUBARCH (clflushopt, CLFLUSHOPT, CLFLUSHOPT, false),
1073 SUBARCH (prefetchwt1, PREFETCHWT1, PREFETCHWT1, false),
1074 SUBARCH (se1, SE1, SE1, false),
1075 SUBARCH (clwb, CLWB, CLWB, false),
1076 SUBARCH (avx512ifma, AVX512IFMA, ANY_AVX512IFMA, false),
1077 SUBARCH (avx512vbmi, AVX512VBMI, ANY_AVX512VBMI, false),
1078 SUBARCH (avx512_4fmaps, AVX512_4FMAPS, ANY_AVX512_4FMAPS, false),
1079 SUBARCH (avx512_4vnniw, AVX512_4VNNIW, ANY_AVX512_4VNNIW, false),
1080 SUBARCH (avx512_vpopcntdq, AVX512_VPOPCNTDQ, ANY_AVX512_VPOPCNTDQ, false),
1081 SUBARCH (avx512_vbmi2, AVX512_VBMI2, ANY_AVX512_VBMI2, false),
1082 SUBARCH (avx512_vnni, AVX512_VNNI, ANY_AVX512_VNNI, false),
1083 SUBARCH (avx512_bitalg, AVX512_BITALG, ANY_AVX512_BITALG, false),
1084 SUBARCH (avx_vnni, AVX_VNNI, ANY_AVX_VNNI, false),
1085 SUBARCH (clzero, CLZERO, CLZERO, false),
1086 SUBARCH (mwaitx, MWAITX, MWAITX, false),
1087 SUBARCH (ospke, OSPKE, OSPKE, false),
1088 SUBARCH (rdpid, RDPID, RDPID, false),
1089 SUBARCH (ptwrite, PTWRITE, PTWRITE, false),
1090 SUBARCH (ibt, IBT, ANY_IBT, false),
1091 SUBARCH (shstk, SHSTK, ANY_SHSTK, false),
1092 SUBARCH (gfni, GFNI, GFNI, false),
1093 SUBARCH (vaes, VAES, VAES, false),
1094 SUBARCH (vpclmulqdq, VPCLMULQDQ, VPCLMULQDQ, false),
1095 SUBARCH (wbnoinvd, WBNOINVD, WBNOINVD, false),
1096 SUBARCH (pconfig, PCONFIG, PCONFIG, false),
1097 SUBARCH (waitpkg, WAITPKG, WAITPKG, false),
1098 SUBARCH (cldemote, CLDEMOTE, CLDEMOTE, false),
1099 SUBARCH (amx_int8, AMX_INT8, ANY_AMX_INT8, false),
1100 SUBARCH (amx_bf16, AMX_BF16, ANY_AMX_BF16, false),
68830fba 1101 SUBARCH (amx_fp16, AMX_FP16, AMX_FP16, false),
ae89daec
JB
1102 SUBARCH (amx_tile, AMX_TILE, ANY_AMX_TILE, false),
1103 SUBARCH (movdiri, MOVDIRI, ANY_MOVDIRI, false),
1104 SUBARCH (movdir64b, MOVDIR64B, ANY_MOVDIR64B, false),
1105 SUBARCH (avx512_bf16, AVX512_BF16, ANY_AVX512_BF16, false),
1106 SUBARCH (avx512_vp2intersect, AVX512_VP2INTERSECT,
1107 ANY_AVX512_VP2INTERSECT, false),
1108 SUBARCH (tdx, TDX, ANY_TDX, false),
1109 SUBARCH (enqcmd, ENQCMD, ANY_ENQCMD, false),
1110 SUBARCH (serialize, SERIALIZE, ANY_SERIALIZE, false),
1111 SUBARCH (rdpru, RDPRU, RDPRU, false),
1112 SUBARCH (mcommit, MCOMMIT, MCOMMIT, false),
1113 SUBARCH (sev_es, SEV_ES, SEV_ES, false),
1114 SUBARCH (tsxldtrk, TSXLDTRK, ANY_TSXLDTRK, false),
1115 SUBARCH (kl, KL, ANY_KL, false),
1116 SUBARCH (widekl, WIDEKL, ANY_WIDEKL, false),
1117 SUBARCH (uintr, UINTR, ANY_UINTR, false),
1118 SUBARCH (hreset, HRESET, ANY_HRESET, false),
1119 SUBARCH (avx512_fp16, AVX512_FP16, ANY_AVX512_FP16, false),
ef07be45 1120 SUBARCH (prefetchi, PREFETCHI, PREFETCHI, false),
4321af3e 1121 SUBARCH (avx_ifma, AVX_IFMA, ANY_AVX_IFMA, false),
23ae61ad 1122 SUBARCH (avx_vnni_int8, AVX_VNNI_INT8, ANY_AVX_VNNI_INT8, false),
a93e3234 1123 SUBARCH (cmpccxadd, CMPCCXADD, ANY_CMPCCXADD, false),
941f0833 1124 SUBARCH (wrmsrns, WRMSRNS, ANY_WRMSRNS, false),
2188d6ea 1125 SUBARCH (msrlist, MSRLIST, ANY_MSRLIST, false),
01d8ce74 1126 SUBARCH (avx_ne_convert, AVX_NE_CONVERT, ANY_AVX_NE_CONVERT, false),
b06311ad 1127 SUBARCH (rao_int, RAO_INT, ANY_RAO_INT, false),
b0e8fa7f 1128 SUBARCH (rmpquery, RMPQUERY, RMPQUERY, false),
293f5f65
L
1129};
1130
6ceeed25
JB
1131#undef SUBARCH
1132#undef ARCH
1133
704209c0 1134#ifdef I386COFF
a6c24e68
NC
1135/* Like s_lcomm_internal in gas/read.c but the alignment string
1136 is allowed to be optional. */
1137
1138static symbolS *
1139pe_lcomm_internal (int needs_align, symbolS *symbolP, addressT size)
1140{
1141 addressT align = 0;
1142
1143 SKIP_WHITESPACE ();
1144
7ab9ffdd 1145 if (needs_align
a6c24e68
NC
1146 && *input_line_pointer == ',')
1147 {
1148 align = parse_align (needs_align - 1);
7ab9ffdd 1149
a6c24e68
NC
1150 if (align == (addressT) -1)
1151 return NULL;
1152 }
1153 else
1154 {
1155 if (size >= 8)
1156 align = 3;
1157 else if (size >= 4)
1158 align = 2;
1159 else if (size >= 2)
1160 align = 1;
1161 else
1162 align = 0;
1163 }
1164
1165 bss_alloc (symbolP, size, align);
1166 return symbolP;
1167}
1168
704209c0 1169static void
a6c24e68
NC
1170pe_lcomm (int needs_align)
1171{
1172 s_comm_internal (needs_align * 2, pe_lcomm_internal);
1173}
704209c0 1174#endif
a6c24e68 1175
29b0f896
AM
1176const pseudo_typeS md_pseudo_table[] =
1177{
1178#if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
1179 {"align", s_align_bytes, 0},
1180#else
1181 {"align", s_align_ptwo, 0},
1182#endif
1183 {"arch", set_cpu_arch, 0},
1184#ifndef I386COFF
1185 {"bss", s_bss, 0},
a6c24e68
NC
1186#else
1187 {"lcomm", pe_lcomm, 1},
29b0f896
AM
1188#endif
1189 {"ffloat", float_cons, 'f'},
1190 {"dfloat", float_cons, 'd'},
1191 {"tfloat", float_cons, 'x'},
7d19d096 1192 {"hfloat", float_cons, 'h'},
de133cf9 1193 {"bfloat16", float_cons, 'b'},
29b0f896 1194 {"value", cons, 2},
d182319b 1195 {"slong", signed_cons, 4},
29b0f896
AM
1196 {"noopt", s_ignore, 0},
1197 {"optim", s_ignore, 0},
1198 {"code16gcc", set_16bit_gcc_code_flag, CODE_16BIT},
1199 {"code16", set_code_flag, CODE_16BIT},
1200 {"code32", set_code_flag, CODE_32BIT},
da5f19a2 1201#ifdef BFD64
29b0f896 1202 {"code64", set_code_flag, CODE_64BIT},
da5f19a2 1203#endif
29b0f896
AM
1204 {"intel_syntax", set_intel_syntax, 1},
1205 {"att_syntax", set_intel_syntax, 0},
1efbbeb4
L
1206 {"intel_mnemonic", set_intel_mnemonic, 1},
1207 {"att_mnemonic", set_intel_mnemonic, 0},
db51cc60
L
1208 {"allow_index_reg", set_allow_index_reg, 1},
1209 {"disallow_index_reg", set_allow_index_reg, 0},
7bab8ab5
JB
1210 {"sse_check", set_check, 0},
1211 {"operand_check", set_check, 1},
3b22753a
L
1212#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1213 {"largecomm", handle_large_common, 0},
07a53e5c 1214#else
68d20676 1215 {"file", dwarf2_directive_file, 0},
07a53e5c
RH
1216 {"loc", dwarf2_directive_loc, 0},
1217 {"loc_mark_labels", dwarf2_directive_loc_mark_labels, 0},
3b22753a 1218#endif
6482c264
NC
1219#ifdef TE_PE
1220 {"secrel32", pe_directive_secrel, 0},
145667f8 1221 {"secidx", pe_directive_secidx, 0},
6482c264 1222#endif
29b0f896
AM
1223 {0, 0, 0}
1224};
1225
1226/* For interface with expression (). */
1227extern char *input_line_pointer;
1228
1229/* Hash table for instruction mnemonic lookup. */
629310ab 1230static htab_t op_hash;
29b0f896
AM
1231
1232/* Hash table for register lookup. */
629310ab 1233static htab_t reg_hash;
29b0f896 1234\f
ce8a8b2f
AM
1235 /* Various efficient no-op patterns for aligning code labels.
1236 Note: Don't try to assemble the instructions in the comments.
1237 0L and 0w are not legal. */
62a02d25
L
1238static const unsigned char f32_1[] =
1239 {0x90}; /* nop */
1240static const unsigned char f32_2[] =
1241 {0x66,0x90}; /* xchg %ax,%ax */
1242static const unsigned char f32_3[] =
1243 {0x8d,0x76,0x00}; /* leal 0(%esi),%esi */
1244static const unsigned char f32_4[] =
1245 {0x8d,0x74,0x26,0x00}; /* leal 0(%esi,1),%esi */
62a02d25
L
1246static const unsigned char f32_6[] =
1247 {0x8d,0xb6,0x00,0x00,0x00,0x00}; /* leal 0L(%esi),%esi */
1248static const unsigned char f32_7[] =
1249 {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal 0L(%esi,1),%esi */
62a02d25 1250static const unsigned char f16_3[] =
3ae729d5 1251 {0x8d,0x74,0x00}; /* lea 0(%si),%si */
62a02d25 1252static const unsigned char f16_4[] =
3ae729d5
L
1253 {0x8d,0xb4,0x00,0x00}; /* lea 0W(%si),%si */
1254static const unsigned char jump_disp8[] =
1255 {0xeb}; /* jmp disp8 */
1256static const unsigned char jump32_disp32[] =
1257 {0xe9}; /* jmp disp32 */
1258static const unsigned char jump16_disp32[] =
1259 {0x66,0xe9}; /* jmp disp32 */
62a02d25
L
1260/* 32-bit NOPs patterns. */
1261static const unsigned char *const f32_patt[] = {
3ae729d5 1262 f32_1, f32_2, f32_3, f32_4, NULL, f32_6, f32_7
62a02d25
L
1263};
1264/* 16-bit NOPs patterns. */
1265static const unsigned char *const f16_patt[] = {
3ae729d5 1266 f32_1, f32_2, f16_3, f16_4
62a02d25
L
1267};
1268/* nopl (%[re]ax) */
1269static const unsigned char alt_3[] =
1270 {0x0f,0x1f,0x00};
1271/* nopl 0(%[re]ax) */
1272static const unsigned char alt_4[] =
1273 {0x0f,0x1f,0x40,0x00};
1274/* nopl 0(%[re]ax,%[re]ax,1) */
1275static const unsigned char alt_5[] =
1276 {0x0f,0x1f,0x44,0x00,0x00};
1277/* nopw 0(%[re]ax,%[re]ax,1) */
1278static const unsigned char alt_6[] =
1279 {0x66,0x0f,0x1f,0x44,0x00,0x00};
1280/* nopl 0L(%[re]ax) */
1281static const unsigned char alt_7[] =
1282 {0x0f,0x1f,0x80,0x00,0x00,0x00,0x00};
1283/* nopl 0L(%[re]ax,%[re]ax,1) */
1284static const unsigned char alt_8[] =
1285 {0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1286/* nopw 0L(%[re]ax,%[re]ax,1) */
1287static const unsigned char alt_9[] =
1288 {0x66,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1289/* nopw %cs:0L(%[re]ax,%[re]ax,1) */
1290static const unsigned char alt_10[] =
1291 {0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
3ae729d5
L
1292/* data16 nopw %cs:0L(%eax,%eax,1) */
1293static const unsigned char alt_11[] =
1294 {0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
62a02d25
L
1295/* 32-bit and 64-bit NOPs patterns. */
1296static const unsigned char *const alt_patt[] = {
1297 f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
3ae729d5 1298 alt_9, alt_10, alt_11
62a02d25
L
1299};
1300
1301/* Genenerate COUNT bytes of NOPs to WHERE from PATT with the maximum
1302 size of a single NOP instruction MAX_SINGLE_NOP_SIZE. */
1303
1304static void
1305i386_output_nops (char *where, const unsigned char *const *patt,
1306 int count, int max_single_nop_size)
1307
1308{
3ae729d5
L
1309 /* Place the longer NOP first. */
1310 int last;
1311 int offset;
3076e594
NC
1312 const unsigned char *nops;
1313
1314 if (max_single_nop_size < 1)
1315 {
1316 as_fatal (_("i386_output_nops called to generate nops of at most %d bytes!"),
1317 max_single_nop_size);
1318 return;
1319 }
1320
1321 nops = patt[max_single_nop_size - 1];
3ae729d5
L
1322
1323 /* Use the smaller one if the requsted one isn't available. */
1324 if (nops == NULL)
62a02d25 1325 {
3ae729d5
L
1326 max_single_nop_size--;
1327 nops = patt[max_single_nop_size - 1];
62a02d25
L
1328 }
1329
3ae729d5
L
1330 last = count % max_single_nop_size;
1331
1332 count -= last;
1333 for (offset = 0; offset < count; offset += max_single_nop_size)
1334 memcpy (where + offset, nops, max_single_nop_size);
1335
1336 if (last)
1337 {
1338 nops = patt[last - 1];
1339 if (nops == NULL)
1340 {
1341 /* Use the smaller one plus one-byte NOP if the needed one
1342 isn't available. */
1343 last--;
1344 nops = patt[last - 1];
1345 memcpy (where + offset, nops, last);
1346 where[offset + last] = *patt[0];
1347 }
1348 else
1349 memcpy (where + offset, nops, last);
1350 }
62a02d25
L
1351}
1352
3ae729d5
L
1353static INLINE int
1354fits_in_imm7 (offsetT num)
1355{
1356 return (num & 0x7f) == num;
1357}
1358
1359static INLINE int
1360fits_in_imm31 (offsetT num)
1361{
1362 return (num & 0x7fffffff) == num;
1363}
62a02d25
L
1364
1365/* Genenerate COUNT bytes of NOPs to WHERE with the maximum size of a
1366 single NOP instruction LIMIT. */
1367
1368void
3ae729d5 1369i386_generate_nops (fragS *fragP, char *where, offsetT count, int limit)
62a02d25 1370{
3ae729d5 1371 const unsigned char *const *patt = NULL;
62a02d25 1372 int max_single_nop_size;
3ae729d5
L
1373 /* Maximum number of NOPs before switching to jump over NOPs. */
1374 int max_number_of_nops;
62a02d25 1375
3ae729d5 1376 switch (fragP->fr_type)
62a02d25 1377 {
3ae729d5
L
1378 case rs_fill_nop:
1379 case rs_align_code:
1380 break;
e379e5f3
L
1381 case rs_machine_dependent:
1382 /* Allow NOP padding for jumps and calls. */
1383 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
1384 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
1385 break;
1386 /* Fall through. */
3ae729d5 1387 default:
62a02d25
L
1388 return;
1389 }
1390
ccc9c027
L
1391 /* We need to decide which NOP sequence to use for 32bit and
1392 64bit. When -mtune= is used:
4eed87de 1393
76bc74dc
L
1394 1. For PROCESSOR_I386, PROCESSOR_I486, PROCESSOR_PENTIUM and
1395 PROCESSOR_GENERIC32, f32_patt will be used.
80b8656c
L
1396 2. For the rest, alt_patt will be used.
1397
1398 When -mtune= isn't used, alt_patt will be used if
22109423 1399 cpu_arch_isa_flags has CpuNop. Otherwise, f32_patt will
76bc74dc 1400 be used.
ccc9c027
L
1401
1402 When -march= or .arch is used, we can't use anything beyond
1403 cpu_arch_isa_flags. */
1404
1405 if (flag_code == CODE_16BIT)
1406 {
3ae729d5
L
1407 patt = f16_patt;
1408 max_single_nop_size = sizeof (f16_patt) / sizeof (f16_patt[0]);
1409 /* Limit number of NOPs to 2 in 16-bit mode. */
1410 max_number_of_nops = 2;
252b5132 1411 }
33fef721 1412 else
ccc9c027 1413 {
fbf3f584 1414 if (fragP->tc_frag_data.isa == PROCESSOR_UNKNOWN)
ccc9c027
L
1415 {
1416 /* PROCESSOR_UNKNOWN means that all ISAs may be used. */
1417 switch (cpu_arch_tune)
1418 {
1419 case PROCESSOR_UNKNOWN:
1420 /* We use cpu_arch_isa_flags to check if we SHOULD
22109423
L
1421 optimize with nops. */
1422 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1423 patt = alt_patt;
ccc9c027
L
1424 else
1425 patt = f32_patt;
1426 break;
ccc9c027
L
1427 case PROCESSOR_PENTIUM4:
1428 case PROCESSOR_NOCONA:
ef05d495 1429 case PROCESSOR_CORE:
76bc74dc 1430 case PROCESSOR_CORE2:
bd5295b2 1431 case PROCESSOR_COREI7:
76bc74dc 1432 case PROCESSOR_GENERIC64:
ccc9c027
L
1433 case PROCESSOR_K6:
1434 case PROCESSOR_ATHLON:
1435 case PROCESSOR_K8:
4eed87de 1436 case PROCESSOR_AMDFAM10:
8aedb9fe 1437 case PROCESSOR_BD:
029f3522 1438 case PROCESSOR_ZNVER:
7b458c12 1439 case PROCESSOR_BT:
80b8656c 1440 patt = alt_patt;
ccc9c027 1441 break;
76bc74dc 1442 case PROCESSOR_I386:
ccc9c027
L
1443 case PROCESSOR_I486:
1444 case PROCESSOR_PENTIUM:
2dde1948 1445 case PROCESSOR_PENTIUMPRO:
81486035 1446 case PROCESSOR_IAMCU:
ccc9c027
L
1447 case PROCESSOR_GENERIC32:
1448 patt = f32_patt;
1449 break;
c368d2a8
JB
1450 case PROCESSOR_NONE:
1451 abort ();
4eed87de 1452 }
ccc9c027
L
1453 }
1454 else
1455 {
fbf3f584 1456 switch (fragP->tc_frag_data.tune)
ccc9c027
L
1457 {
1458 case PROCESSOR_UNKNOWN:
e6a14101 1459 /* When cpu_arch_isa is set, cpu_arch_tune shouldn't be
ccc9c027
L
1460 PROCESSOR_UNKNOWN. */
1461 abort ();
1462 break;
1463
76bc74dc 1464 case PROCESSOR_I386:
ccc9c027
L
1465 case PROCESSOR_I486:
1466 case PROCESSOR_PENTIUM:
81486035 1467 case PROCESSOR_IAMCU:
ccc9c027
L
1468 case PROCESSOR_K6:
1469 case PROCESSOR_ATHLON:
1470 case PROCESSOR_K8:
4eed87de 1471 case PROCESSOR_AMDFAM10:
8aedb9fe 1472 case PROCESSOR_BD:
029f3522 1473 case PROCESSOR_ZNVER:
7b458c12 1474 case PROCESSOR_BT:
ccc9c027
L
1475 case PROCESSOR_GENERIC32:
1476 /* We use cpu_arch_isa_flags to check if we CAN optimize
22109423
L
1477 with nops. */
1478 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1479 patt = alt_patt;
ccc9c027
L
1480 else
1481 patt = f32_patt;
1482 break;
76bc74dc
L
1483 case PROCESSOR_PENTIUMPRO:
1484 case PROCESSOR_PENTIUM4:
1485 case PROCESSOR_NOCONA:
1486 case PROCESSOR_CORE:
ef05d495 1487 case PROCESSOR_CORE2:
bd5295b2 1488 case PROCESSOR_COREI7:
22109423 1489 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1490 patt = alt_patt;
ccc9c027
L
1491 else
1492 patt = f32_patt;
1493 break;
1494 case PROCESSOR_GENERIC64:
80b8656c 1495 patt = alt_patt;
ccc9c027 1496 break;
c368d2a8
JB
1497 case PROCESSOR_NONE:
1498 abort ();
4eed87de 1499 }
ccc9c027
L
1500 }
1501
76bc74dc
L
1502 if (patt == f32_patt)
1503 {
3ae729d5
L
1504 max_single_nop_size = sizeof (f32_patt) / sizeof (f32_patt[0]);
1505 /* Limit number of NOPs to 2 for older processors. */
1506 max_number_of_nops = 2;
76bc74dc
L
1507 }
1508 else
1509 {
3ae729d5
L
1510 max_single_nop_size = sizeof (alt_patt) / sizeof (alt_patt[0]);
1511 /* Limit number of NOPs to 7 for newer processors. */
1512 max_number_of_nops = 7;
1513 }
1514 }
1515
1516 if (limit == 0)
1517 limit = max_single_nop_size;
1518
1519 if (fragP->fr_type == rs_fill_nop)
1520 {
1521 /* Output NOPs for .nop directive. */
1522 if (limit > max_single_nop_size)
1523 {
1524 as_bad_where (fragP->fr_file, fragP->fr_line,
1525 _("invalid single nop size: %d "
1526 "(expect within [0, %d])"),
1527 limit, max_single_nop_size);
1528 return;
1529 }
1530 }
e379e5f3 1531 else if (fragP->fr_type != rs_machine_dependent)
3ae729d5
L
1532 fragP->fr_var = count;
1533
1534 if ((count / max_single_nop_size) > max_number_of_nops)
1535 {
1536 /* Generate jump over NOPs. */
1537 offsetT disp = count - 2;
1538 if (fits_in_imm7 (disp))
1539 {
1540 /* Use "jmp disp8" if possible. */
1541 count = disp;
1542 where[0] = jump_disp8[0];
1543 where[1] = count;
1544 where += 2;
1545 }
1546 else
1547 {
1548 unsigned int size_of_jump;
1549
1550 if (flag_code == CODE_16BIT)
1551 {
1552 where[0] = jump16_disp32[0];
1553 where[1] = jump16_disp32[1];
1554 size_of_jump = 2;
1555 }
1556 else
1557 {
1558 where[0] = jump32_disp32[0];
1559 size_of_jump = 1;
1560 }
1561
1562 count -= size_of_jump + 4;
1563 if (!fits_in_imm31 (count))
1564 {
1565 as_bad_where (fragP->fr_file, fragP->fr_line,
1566 _("jump over nop padding out of range"));
1567 return;
1568 }
1569
1570 md_number_to_chars (where + size_of_jump, count, 4);
1571 where += size_of_jump + 4;
76bc74dc 1572 }
ccc9c027 1573 }
3ae729d5
L
1574
1575 /* Generate multiple NOPs. */
1576 i386_output_nops (where, patt, count, limit);
252b5132
RH
1577}
1578
c6fb90c8 1579static INLINE int
0dfbf9d7 1580operand_type_all_zero (const union i386_operand_type *x)
40fb9820 1581{
0dfbf9d7 1582 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1583 {
1584 case 3:
0dfbf9d7 1585 if (x->array[2])
c6fb90c8 1586 return 0;
1a0670f3 1587 /* Fall through. */
c6fb90c8 1588 case 2:
0dfbf9d7 1589 if (x->array[1])
c6fb90c8 1590 return 0;
1a0670f3 1591 /* Fall through. */
c6fb90c8 1592 case 1:
0dfbf9d7 1593 return !x->array[0];
c6fb90c8
L
1594 default:
1595 abort ();
1596 }
40fb9820
L
1597}
1598
c6fb90c8 1599static INLINE void
0dfbf9d7 1600operand_type_set (union i386_operand_type *x, unsigned int v)
40fb9820 1601{
0dfbf9d7 1602 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1603 {
1604 case 3:
0dfbf9d7 1605 x->array[2] = v;
1a0670f3 1606 /* Fall through. */
c6fb90c8 1607 case 2:
0dfbf9d7 1608 x->array[1] = v;
1a0670f3 1609 /* Fall through. */
c6fb90c8 1610 case 1:
0dfbf9d7 1611 x->array[0] = v;
1a0670f3 1612 /* Fall through. */
c6fb90c8
L
1613 break;
1614 default:
1615 abort ();
1616 }
bab6aec1
JB
1617
1618 x->bitfield.class = ClassNone;
75e5731b 1619 x->bitfield.instance = InstanceNone;
c6fb90c8 1620}
40fb9820 1621
c6fb90c8 1622static INLINE int
0dfbf9d7
L
1623operand_type_equal (const union i386_operand_type *x,
1624 const union i386_operand_type *y)
c6fb90c8 1625{
0dfbf9d7 1626 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1627 {
1628 case 3:
0dfbf9d7 1629 if (x->array[2] != y->array[2])
c6fb90c8 1630 return 0;
1a0670f3 1631 /* Fall through. */
c6fb90c8 1632 case 2:
0dfbf9d7 1633 if (x->array[1] != y->array[1])
c6fb90c8 1634 return 0;
1a0670f3 1635 /* Fall through. */
c6fb90c8 1636 case 1:
0dfbf9d7 1637 return x->array[0] == y->array[0];
c6fb90c8
L
1638 break;
1639 default:
1640 abort ();
1641 }
1642}
40fb9820 1643
0dfbf9d7
L
1644static INLINE int
1645cpu_flags_all_zero (const union i386_cpu_flags *x)
1646{
1647 switch (ARRAY_SIZE(x->array))
1648 {
75f8266a
KL
1649 case 5:
1650 if (x->array[4])
1651 return 0;
1652 /* Fall through. */
53467f57
IT
1653 case 4:
1654 if (x->array[3])
1655 return 0;
1656 /* Fall through. */
0dfbf9d7
L
1657 case 3:
1658 if (x->array[2])
1659 return 0;
1a0670f3 1660 /* Fall through. */
0dfbf9d7
L
1661 case 2:
1662 if (x->array[1])
1663 return 0;
1a0670f3 1664 /* Fall through. */
0dfbf9d7
L
1665 case 1:
1666 return !x->array[0];
1667 default:
1668 abort ();
1669 }
1670}
1671
0dfbf9d7
L
1672static INLINE int
1673cpu_flags_equal (const union i386_cpu_flags *x,
1674 const union i386_cpu_flags *y)
1675{
1676 switch (ARRAY_SIZE(x->array))
1677 {
75f8266a
KL
1678 case 5:
1679 if (x->array[4] != y->array[4])
1680 return 0;
1681 /* Fall through. */
53467f57
IT
1682 case 4:
1683 if (x->array[3] != y->array[3])
1684 return 0;
1685 /* Fall through. */
0dfbf9d7
L
1686 case 3:
1687 if (x->array[2] != y->array[2])
1688 return 0;
1a0670f3 1689 /* Fall through. */
0dfbf9d7
L
1690 case 2:
1691 if (x->array[1] != y->array[1])
1692 return 0;
1a0670f3 1693 /* Fall through. */
0dfbf9d7
L
1694 case 1:
1695 return x->array[0] == y->array[0];
1696 break;
1697 default:
1698 abort ();
1699 }
1700}
c6fb90c8
L
1701
1702static INLINE int
1703cpu_flags_check_cpu64 (i386_cpu_flags f)
1704{
1705 return !((flag_code == CODE_64BIT && f.bitfield.cpuno64)
1706 || (flag_code != CODE_64BIT && f.bitfield.cpu64));
40fb9820
L
1707}
1708
c6fb90c8
L
1709static INLINE i386_cpu_flags
1710cpu_flags_and (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1711{
c6fb90c8
L
1712 switch (ARRAY_SIZE (x.array))
1713 {
75f8266a
KL
1714 case 5:
1715 x.array [4] &= y.array [4];
1716 /* Fall through. */
53467f57
IT
1717 case 4:
1718 x.array [3] &= y.array [3];
1719 /* Fall through. */
c6fb90c8
L
1720 case 3:
1721 x.array [2] &= y.array [2];
1a0670f3 1722 /* Fall through. */
c6fb90c8
L
1723 case 2:
1724 x.array [1] &= y.array [1];
1a0670f3 1725 /* Fall through. */
c6fb90c8
L
1726 case 1:
1727 x.array [0] &= y.array [0];
1728 break;
1729 default:
1730 abort ();
1731 }
1732 return x;
1733}
40fb9820 1734
c6fb90c8
L
1735static INLINE i386_cpu_flags
1736cpu_flags_or (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1737{
c6fb90c8 1738 switch (ARRAY_SIZE (x.array))
40fb9820 1739 {
75f8266a
KL
1740 case 5:
1741 x.array [4] |= y.array [4];
1742 /* Fall through. */
53467f57
IT
1743 case 4:
1744 x.array [3] |= y.array [3];
1745 /* Fall through. */
c6fb90c8
L
1746 case 3:
1747 x.array [2] |= y.array [2];
1a0670f3 1748 /* Fall through. */
c6fb90c8
L
1749 case 2:
1750 x.array [1] |= y.array [1];
1a0670f3 1751 /* Fall through. */
c6fb90c8
L
1752 case 1:
1753 x.array [0] |= y.array [0];
40fb9820
L
1754 break;
1755 default:
1756 abort ();
1757 }
40fb9820
L
1758 return x;
1759}
1760
309d3373
JB
1761static INLINE i386_cpu_flags
1762cpu_flags_and_not (i386_cpu_flags x, i386_cpu_flags y)
1763{
1764 switch (ARRAY_SIZE (x.array))
1765 {
75f8266a
KL
1766 case 5:
1767 x.array [4] &= ~y.array [4];
1768 /* Fall through. */
53467f57
IT
1769 case 4:
1770 x.array [3] &= ~y.array [3];
1771 /* Fall through. */
309d3373
JB
1772 case 3:
1773 x.array [2] &= ~y.array [2];
1a0670f3 1774 /* Fall through. */
309d3373
JB
1775 case 2:
1776 x.array [1] &= ~y.array [1];
1a0670f3 1777 /* Fall through. */
309d3373
JB
1778 case 1:
1779 x.array [0] &= ~y.array [0];
1780 break;
1781 default:
1782 abort ();
1783 }
1784 return x;
1785}
1786
6c0946d0
JB
1787static const i386_cpu_flags avx512 = CPU_ANY_AVX512F_FLAGS;
1788
c0f3af97
L
1789#define CPU_FLAGS_ARCH_MATCH 0x1
1790#define CPU_FLAGS_64BIT_MATCH 0x2
1791
c0f3af97 1792#define CPU_FLAGS_PERFECT_MATCH \
db12e14e 1793 (CPU_FLAGS_ARCH_MATCH | CPU_FLAGS_64BIT_MATCH)
c0f3af97
L
1794
1795/* Return CPU flags match bits. */
3629bb00 1796
40fb9820 1797static int
d3ce72d0 1798cpu_flags_match (const insn_template *t)
40fb9820 1799{
c0f3af97
L
1800 i386_cpu_flags x = t->cpu_flags;
1801 int match = cpu_flags_check_cpu64 (x) ? CPU_FLAGS_64BIT_MATCH : 0;
40fb9820
L
1802
1803 x.bitfield.cpu64 = 0;
1804 x.bitfield.cpuno64 = 0;
1805
0dfbf9d7 1806 if (cpu_flags_all_zero (&x))
c0f3af97
L
1807 {
1808 /* This instruction is available on all archs. */
db12e14e 1809 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 1810 }
3629bb00
L
1811 else
1812 {
c0f3af97 1813 /* This instruction is available only on some archs. */
3629bb00
L
1814 i386_cpu_flags cpu = cpu_arch_flags;
1815
ab592e75
JB
1816 /* AVX512VL is no standalone feature - match it and then strip it. */
1817 if (x.bitfield.cpuavx512vl && !cpu.bitfield.cpuavx512vl)
1818 return match;
1819 x.bitfield.cpuavx512vl = 0;
1820
22c36940
JB
1821 /* AVX and AVX2 present at the same time express an operand size
1822 dependency - strip AVX2 for the purposes here. The operand size
1823 dependent check occurs in check_vecOperands(). */
1824 if (x.bitfield.cpuavx && x.bitfield.cpuavx2)
1825 x.bitfield.cpuavx2 = 0;
1826
3629bb00 1827 cpu = cpu_flags_and (x, cpu);
c0f3af97
L
1828 if (!cpu_flags_all_zero (&cpu))
1829 {
57392598 1830 if (x.bitfield.cpuavx)
a5ff0eb2 1831 {
929f69fa 1832 /* We need to check a few extra flags with AVX. */
b9d49817 1833 if (cpu.bitfield.cpuavx
40d231b4
JB
1834 && (!t->opcode_modifier.sse2avx
1835 || (sse2avx && !i.prefix[DATA_PREFIX]))
b9d49817 1836 && (!x.bitfield.cpuaes || cpu.bitfield.cpuaes)
929f69fa 1837 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
b9d49817
JB
1838 && (!x.bitfield.cpupclmul || cpu.bitfield.cpupclmul))
1839 match |= CPU_FLAGS_ARCH_MATCH;
a5ff0eb2 1840 }
929f69fa
JB
1841 else if (x.bitfield.cpuavx512f)
1842 {
1843 /* We need to check a few extra flags with AVX512F. */
1844 if (cpu.bitfield.cpuavx512f
1845 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
1846 && (!x.bitfield.cpuvaes || cpu.bitfield.cpuvaes)
1847 && (!x.bitfield.cpuvpclmulqdq || cpu.bitfield.cpuvpclmulqdq))
1848 match |= CPU_FLAGS_ARCH_MATCH;
1849 }
a5ff0eb2 1850 else
db12e14e 1851 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 1852 }
3629bb00 1853 }
c0f3af97 1854 return match;
40fb9820
L
1855}
1856
c6fb90c8
L
1857static INLINE i386_operand_type
1858operand_type_and (i386_operand_type x, i386_operand_type y)
40fb9820 1859{
bab6aec1
JB
1860 if (x.bitfield.class != y.bitfield.class)
1861 x.bitfield.class = ClassNone;
75e5731b
JB
1862 if (x.bitfield.instance != y.bitfield.instance)
1863 x.bitfield.instance = InstanceNone;
bab6aec1 1864
c6fb90c8
L
1865 switch (ARRAY_SIZE (x.array))
1866 {
1867 case 3:
1868 x.array [2] &= y.array [2];
1a0670f3 1869 /* Fall through. */
c6fb90c8
L
1870 case 2:
1871 x.array [1] &= y.array [1];
1a0670f3 1872 /* Fall through. */
c6fb90c8
L
1873 case 1:
1874 x.array [0] &= y.array [0];
1875 break;
1876 default:
1877 abort ();
1878 }
1879 return x;
40fb9820
L
1880}
1881
73053c1f
JB
1882static INLINE i386_operand_type
1883operand_type_and_not (i386_operand_type x, i386_operand_type y)
1884{
bab6aec1 1885 gas_assert (y.bitfield.class == ClassNone);
75e5731b 1886 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 1887
73053c1f
JB
1888 switch (ARRAY_SIZE (x.array))
1889 {
1890 case 3:
1891 x.array [2] &= ~y.array [2];
1892 /* Fall through. */
1893 case 2:
1894 x.array [1] &= ~y.array [1];
1895 /* Fall through. */
1896 case 1:
1897 x.array [0] &= ~y.array [0];
1898 break;
1899 default:
1900 abort ();
1901 }
1902 return x;
1903}
1904
c6fb90c8
L
1905static INLINE i386_operand_type
1906operand_type_or (i386_operand_type x, i386_operand_type y)
40fb9820 1907{
bab6aec1
JB
1908 gas_assert (x.bitfield.class == ClassNone ||
1909 y.bitfield.class == ClassNone ||
1910 x.bitfield.class == y.bitfield.class);
75e5731b
JB
1911 gas_assert (x.bitfield.instance == InstanceNone ||
1912 y.bitfield.instance == InstanceNone ||
1913 x.bitfield.instance == y.bitfield.instance);
bab6aec1 1914
c6fb90c8 1915 switch (ARRAY_SIZE (x.array))
40fb9820 1916 {
c6fb90c8
L
1917 case 3:
1918 x.array [2] |= y.array [2];
1a0670f3 1919 /* Fall through. */
c6fb90c8
L
1920 case 2:
1921 x.array [1] |= y.array [1];
1a0670f3 1922 /* Fall through. */
c6fb90c8
L
1923 case 1:
1924 x.array [0] |= y.array [0];
40fb9820
L
1925 break;
1926 default:
1927 abort ();
1928 }
c6fb90c8
L
1929 return x;
1930}
40fb9820 1931
c6fb90c8
L
1932static INLINE i386_operand_type
1933operand_type_xor (i386_operand_type x, i386_operand_type y)
1934{
bab6aec1 1935 gas_assert (y.bitfield.class == ClassNone);
75e5731b 1936 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 1937
c6fb90c8
L
1938 switch (ARRAY_SIZE (x.array))
1939 {
1940 case 3:
1941 x.array [2] ^= y.array [2];
1a0670f3 1942 /* Fall through. */
c6fb90c8
L
1943 case 2:
1944 x.array [1] ^= y.array [1];
1a0670f3 1945 /* Fall through. */
c6fb90c8
L
1946 case 1:
1947 x.array [0] ^= y.array [0];
1948 break;
1949 default:
1950 abort ();
1951 }
40fb9820
L
1952 return x;
1953}
1954
40fb9820 1955static const i386_operand_type disp16_32 = OPERAND_TYPE_DISP16_32;
bab6aec1 1956static const i386_operand_type anydisp = OPERAND_TYPE_ANYDISP;
40fb9820 1957static const i386_operand_type regxmm = OPERAND_TYPE_REGXMM;
40fb9820
L
1958static const i386_operand_type imm16 = OPERAND_TYPE_IMM16;
1959static const i386_operand_type imm32 = OPERAND_TYPE_IMM32;
1960static const i386_operand_type imm32s = OPERAND_TYPE_IMM32S;
40fb9820
L
1961static const i386_operand_type imm16_32 = OPERAND_TYPE_IMM16_32;
1962static const i386_operand_type imm16_32s = OPERAND_TYPE_IMM16_32S;
1963static const i386_operand_type imm16_32_32s = OPERAND_TYPE_IMM16_32_32S;
1964
1965enum operand_type
1966{
1967 reg,
40fb9820
L
1968 imm,
1969 disp,
1970 anymem
1971};
1972
c6fb90c8 1973static INLINE int
40fb9820
L
1974operand_type_check (i386_operand_type t, enum operand_type c)
1975{
1976 switch (c)
1977 {
1978 case reg:
bab6aec1 1979 return t.bitfield.class == Reg;
40fb9820 1980
40fb9820
L
1981 case imm:
1982 return (t.bitfield.imm8
1983 || t.bitfield.imm8s
1984 || t.bitfield.imm16
1985 || t.bitfield.imm32
1986 || t.bitfield.imm32s
1987 || t.bitfield.imm64);
1988
1989 case disp:
1990 return (t.bitfield.disp8
1991 || t.bitfield.disp16
1992 || t.bitfield.disp32
40fb9820
L
1993 || t.bitfield.disp64);
1994
1995 case anymem:
1996 return (t.bitfield.disp8
1997 || t.bitfield.disp16
1998 || t.bitfield.disp32
40fb9820
L
1999 || t.bitfield.disp64
2000 || t.bitfield.baseindex);
2001
2002 default:
2003 abort ();
2004 }
2cfe26b6
AM
2005
2006 return 0;
40fb9820
L
2007}
2008
7a54636a
L
2009/* Return 1 if there is no conflict in 8bit/16bit/32bit/64bit/80bit size
2010 between operand GIVEN and opeand WANTED for instruction template T. */
5c07affc
L
2011
2012static INLINE int
7a54636a
L
2013match_operand_size (const insn_template *t, unsigned int wanted,
2014 unsigned int given)
5c07affc 2015{
3ac21baa
JB
2016 return !((i.types[given].bitfield.byte
2017 && !t->operand_types[wanted].bitfield.byte)
2018 || (i.types[given].bitfield.word
2019 && !t->operand_types[wanted].bitfield.word)
2020 || (i.types[given].bitfield.dword
2021 && !t->operand_types[wanted].bitfield.dword)
2022 || (i.types[given].bitfield.qword
2023 && !t->operand_types[wanted].bitfield.qword)
2024 || (i.types[given].bitfield.tbyte
2025 && !t->operand_types[wanted].bitfield.tbyte));
5c07affc
L
2026}
2027
dd40ce22
L
2028/* Return 1 if there is no conflict in SIMD register between operand
2029 GIVEN and opeand WANTED for instruction template T. */
1b54b8d7
JB
2030
2031static INLINE int
dd40ce22
L
2032match_simd_size (const insn_template *t, unsigned int wanted,
2033 unsigned int given)
1b54b8d7 2034{
3ac21baa
JB
2035 return !((i.types[given].bitfield.xmmword
2036 && !t->operand_types[wanted].bitfield.xmmword)
2037 || (i.types[given].bitfield.ymmword
2038 && !t->operand_types[wanted].bitfield.ymmword)
2039 || (i.types[given].bitfield.zmmword
260cd341
LC
2040 && !t->operand_types[wanted].bitfield.zmmword)
2041 || (i.types[given].bitfield.tmmword
2042 && !t->operand_types[wanted].bitfield.tmmword));
1b54b8d7
JB
2043}
2044
7a54636a
L
2045/* Return 1 if there is no conflict in any size between operand GIVEN
2046 and opeand WANTED for instruction template T. */
5c07affc
L
2047
2048static INLINE int
dd40ce22
L
2049match_mem_size (const insn_template *t, unsigned int wanted,
2050 unsigned int given)
5c07affc 2051{
7a54636a 2052 return (match_operand_size (t, wanted, given)
3ac21baa 2053 && !((i.types[given].bitfield.unspecified
5273a3cd 2054 && !i.broadcast.type
a5748e0d 2055 && !i.broadcast.bytes
3ac21baa
JB
2056 && !t->operand_types[wanted].bitfield.unspecified)
2057 || (i.types[given].bitfield.fword
2058 && !t->operand_types[wanted].bitfield.fword)
1b54b8d7
JB
2059 /* For scalar opcode templates to allow register and memory
2060 operands at the same time, some special casing is needed
d6793fa1
JB
2061 here. Also for v{,p}broadcast*, {,v}pmov{s,z}*, and
2062 down-conversion vpmov*. */
3528c362 2063 || ((t->operand_types[wanted].bitfield.class == RegSIMD
bc49bfd8
JB
2064 && t->operand_types[wanted].bitfield.byte
2065 + t->operand_types[wanted].bitfield.word
2066 + t->operand_types[wanted].bitfield.dword
2067 + t->operand_types[wanted].bitfield.qword
2068 > !!t->opcode_modifier.broadcast)
3ac21baa
JB
2069 ? (i.types[given].bitfield.xmmword
2070 || i.types[given].bitfield.ymmword
2071 || i.types[given].bitfield.zmmword)
2072 : !match_simd_size(t, wanted, given))));
5c07affc
L
2073}
2074
3ac21baa
JB
2075/* Return value has MATCH_STRAIGHT set if there is no size conflict on any
2076 operands for instruction template T, and it has MATCH_REVERSE set if there
2077 is no size conflict on any operands for the template with operands reversed
2078 (and the template allows for reversing in the first place). */
5c07affc 2079
3ac21baa
JB
2080#define MATCH_STRAIGHT 1
2081#define MATCH_REVERSE 2
2082
2083static INLINE unsigned int
d3ce72d0 2084operand_size_match (const insn_template *t)
5c07affc 2085{
3ac21baa 2086 unsigned int j, match = MATCH_STRAIGHT;
5c07affc 2087
0cfa3eb3 2088 /* Don't check non-absolute jump instructions. */
5c07affc 2089 if (t->opcode_modifier.jump
0cfa3eb3 2090 && t->opcode_modifier.jump != JUMP_ABSOLUTE)
5c07affc
L
2091 return match;
2092
2093 /* Check memory and accumulator operand size. */
2094 for (j = 0; j < i.operands; j++)
2095 {
3528c362
JB
2096 if (i.types[j].bitfield.class != Reg
2097 && i.types[j].bitfield.class != RegSIMD
255571cd 2098 && t->opcode_modifier.operandconstraint == ANY_SIZE)
5c07affc
L
2099 continue;
2100
bab6aec1 2101 if (t->operand_types[j].bitfield.class == Reg
7a54636a 2102 && !match_operand_size (t, j, j))
5c07affc
L
2103 {
2104 match = 0;
2105 break;
2106 }
2107
3528c362 2108 if (t->operand_types[j].bitfield.class == RegSIMD
3ac21baa 2109 && !match_simd_size (t, j, j))
1b54b8d7
JB
2110 {
2111 match = 0;
2112 break;
2113 }
2114
75e5731b 2115 if (t->operand_types[j].bitfield.instance == Accum
7a54636a 2116 && (!match_operand_size (t, j, j) || !match_simd_size (t, j, j)))
1b54b8d7
JB
2117 {
2118 match = 0;
2119 break;
2120 }
2121
c48dadc9 2122 if ((i.flags[j] & Operand_Mem) && !match_mem_size (t, j, j))
5c07affc
L
2123 {
2124 match = 0;
2125 break;
2126 }
2127 }
2128
3ac21baa 2129 if (!t->opcode_modifier.d)
7b94647a 2130 return match;
5c07affc
L
2131
2132 /* Check reverse. */
8bd915b7
JB
2133 gas_assert ((i.operands >= 2 && i.operands <= 3)
2134 || t->opcode_modifier.vexsources);
5c07affc 2135
f5eb1d70 2136 for (j = 0; j < i.operands; j++)
5c07affc 2137 {
f5eb1d70
JB
2138 unsigned int given = i.operands - j - 1;
2139
8bd915b7
JB
2140 /* For 4- and 5-operand insns VEX.W controls just the first two
2141 register operands. */
2142 if (t->opcode_modifier.vexsources)
2143 given = j < 2 ? 1 - j : j;
2144
bab6aec1 2145 if (t->operand_types[j].bitfield.class == Reg
f5eb1d70 2146 && !match_operand_size (t, j, given))
7b94647a 2147 return match;
5c07affc 2148
3528c362 2149 if (t->operand_types[j].bitfield.class == RegSIMD
f5eb1d70 2150 && !match_simd_size (t, j, given))
7b94647a 2151 return match;
dbbc8b7e 2152
75e5731b 2153 if (t->operand_types[j].bitfield.instance == Accum
f5eb1d70
JB
2154 && (!match_operand_size (t, j, given)
2155 || !match_simd_size (t, j, given)))
7b94647a 2156 return match;
dbbc8b7e 2157
f5eb1d70 2158 if ((i.flags[given] & Operand_Mem) && !match_mem_size (t, j, given))
7b94647a 2159 return match;
5c07affc
L
2160 }
2161
3ac21baa 2162 return match | MATCH_REVERSE;
5c07affc
L
2163}
2164
c6fb90c8 2165static INLINE int
40fb9820
L
2166operand_type_match (i386_operand_type overlap,
2167 i386_operand_type given)
2168{
2169 i386_operand_type temp = overlap;
2170
7d5e4556 2171 temp.bitfield.unspecified = 0;
5c07affc
L
2172 temp.bitfield.byte = 0;
2173 temp.bitfield.word = 0;
2174 temp.bitfield.dword = 0;
2175 temp.bitfield.fword = 0;
2176 temp.bitfield.qword = 0;
2177 temp.bitfield.tbyte = 0;
2178 temp.bitfield.xmmword = 0;
c0f3af97 2179 temp.bitfield.ymmword = 0;
43234a1e 2180 temp.bitfield.zmmword = 0;
260cd341 2181 temp.bitfield.tmmword = 0;
0dfbf9d7 2182 if (operand_type_all_zero (&temp))
891edac4 2183 goto mismatch;
40fb9820 2184
6f2f06be 2185 if (given.bitfield.baseindex == overlap.bitfield.baseindex)
891edac4
L
2186 return 1;
2187
dc1e8a47 2188 mismatch:
a65babc9 2189 i.error = operand_type_mismatch;
891edac4 2190 return 0;
40fb9820
L
2191}
2192
7d5e4556 2193/* If given types g0 and g1 are registers they must be of the same type
10c17abd 2194 unless the expected operand type register overlap is null.
8ee52bcf 2195 Intel syntax sized memory operands are also checked here. */
40fb9820 2196
c6fb90c8 2197static INLINE int
dc821c5f 2198operand_type_register_match (i386_operand_type g0,
40fb9820 2199 i386_operand_type t0,
40fb9820
L
2200 i386_operand_type g1,
2201 i386_operand_type t1)
2202{
bab6aec1 2203 if (g0.bitfield.class != Reg
3528c362 2204 && g0.bitfield.class != RegSIMD
8ee52bcf
JB
2205 && (g0.bitfield.unspecified
2206 || !operand_type_check (g0, anymem)))
40fb9820
L
2207 return 1;
2208
bab6aec1 2209 if (g1.bitfield.class != Reg
3528c362 2210 && g1.bitfield.class != RegSIMD
8ee52bcf
JB
2211 && (g1.bitfield.unspecified
2212 || !operand_type_check (g1, anymem)))
40fb9820
L
2213 return 1;
2214
dc821c5f
JB
2215 if (g0.bitfield.byte == g1.bitfield.byte
2216 && g0.bitfield.word == g1.bitfield.word
2217 && g0.bitfield.dword == g1.bitfield.dword
10c17abd
JB
2218 && g0.bitfield.qword == g1.bitfield.qword
2219 && g0.bitfield.xmmword == g1.bitfield.xmmword
2220 && g0.bitfield.ymmword == g1.bitfield.ymmword
2221 && g0.bitfield.zmmword == g1.bitfield.zmmword)
40fb9820
L
2222 return 1;
2223
c4d09633
JB
2224 /* If expectations overlap in no more than a single size, all is fine. */
2225 g0 = operand_type_and (t0, t1);
2226 if (g0.bitfield.byte
2227 + g0.bitfield.word
2228 + g0.bitfield.dword
2229 + g0.bitfield.qword
2230 + g0.bitfield.xmmword
2231 + g0.bitfield.ymmword
2232 + g0.bitfield.zmmword <= 1)
891edac4
L
2233 return 1;
2234
a65babc9 2235 i.error = register_type_mismatch;
891edac4
L
2236
2237 return 0;
40fb9820
L
2238}
2239
4c692bc7
JB
2240static INLINE unsigned int
2241register_number (const reg_entry *r)
2242{
2243 unsigned int nr = r->reg_num;
2244
2245 if (r->reg_flags & RegRex)
2246 nr += 8;
2247
200cbe0f
L
2248 if (r->reg_flags & RegVRex)
2249 nr += 16;
2250
4c692bc7
JB
2251 return nr;
2252}
2253
252b5132 2254static INLINE unsigned int
40fb9820 2255mode_from_disp_size (i386_operand_type t)
252b5132 2256{
b5014f7a 2257 if (t.bitfield.disp8)
40fb9820
L
2258 return 1;
2259 else if (t.bitfield.disp16
a775efc8 2260 || t.bitfield.disp32)
40fb9820
L
2261 return 2;
2262 else
2263 return 0;
252b5132
RH
2264}
2265
2266static INLINE int
65879393 2267fits_in_signed_byte (addressT num)
252b5132 2268{
65879393 2269 return num + 0x80 <= 0xff;
47926f60 2270}
252b5132
RH
2271
2272static INLINE int
65879393 2273fits_in_unsigned_byte (addressT num)
252b5132 2274{
65879393 2275 return num <= 0xff;
47926f60 2276}
252b5132
RH
2277
2278static INLINE int
65879393 2279fits_in_unsigned_word (addressT num)
252b5132 2280{
65879393 2281 return num <= 0xffff;
47926f60 2282}
252b5132
RH
2283
2284static INLINE int
65879393 2285fits_in_signed_word (addressT num)
252b5132 2286{
65879393 2287 return num + 0x8000 <= 0xffff;
47926f60 2288}
2a962e6d 2289
3e73aa7c 2290static INLINE int
65879393 2291fits_in_signed_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2292{
2293#ifndef BFD64
2294 return 1;
2295#else
65879393 2296 return num + 0x80000000 <= 0xffffffff;
3e73aa7c
JH
2297#endif
2298} /* fits_in_signed_long() */
2a962e6d 2299
3e73aa7c 2300static INLINE int
65879393 2301fits_in_unsigned_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2302{
2303#ifndef BFD64
2304 return 1;
2305#else
65879393 2306 return num <= 0xffffffff;
3e73aa7c
JH
2307#endif
2308} /* fits_in_unsigned_long() */
252b5132 2309
a442cac5
JB
2310static INLINE valueT extend_to_32bit_address (addressT num)
2311{
2312#ifdef BFD64
2313 if (fits_in_unsigned_long(num))
2314 return (num ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
2315
2316 if (!fits_in_signed_long (num))
2317 return num & 0xffffffff;
2318#endif
2319
2320 return num;
2321}
2322
43234a1e 2323static INLINE int
b5014f7a 2324fits_in_disp8 (offsetT num)
43234a1e
L
2325{
2326 int shift = i.memshift;
2327 unsigned int mask;
2328
2329 if (shift == -1)
2330 abort ();
2331
2332 mask = (1 << shift) - 1;
2333
2334 /* Return 0 if NUM isn't properly aligned. */
2335 if ((num & mask))
2336 return 0;
2337
2338 /* Check if NUM will fit in 8bit after shift. */
2339 return fits_in_signed_byte (num >> shift);
2340}
2341
a683cc34
SP
2342static INLINE int
2343fits_in_imm4 (offsetT num)
2344{
2345 return (num & 0xf) == num;
2346}
2347
40fb9820 2348static i386_operand_type
e3bb37b5 2349smallest_imm_type (offsetT num)
252b5132 2350{
40fb9820 2351 i386_operand_type t;
7ab9ffdd 2352
0dfbf9d7 2353 operand_type_set (&t, 0);
40fb9820
L
2354 t.bitfield.imm64 = 1;
2355
2356 if (cpu_arch_tune != PROCESSOR_I486 && num == 1)
e413e4e9
AM
2357 {
2358 /* This code is disabled on the 486 because all the Imm1 forms
2359 in the opcode table are slower on the i486. They're the
2360 versions with the implicitly specified single-position
2361 displacement, which has another syntax if you really want to
2362 use that form. */
40fb9820
L
2363 t.bitfield.imm1 = 1;
2364 t.bitfield.imm8 = 1;
2365 t.bitfield.imm8s = 1;
2366 t.bitfield.imm16 = 1;
2367 t.bitfield.imm32 = 1;
2368 t.bitfield.imm32s = 1;
2369 }
2370 else if (fits_in_signed_byte (num))
2371 {
2372 t.bitfield.imm8 = 1;
2373 t.bitfield.imm8s = 1;
2374 t.bitfield.imm16 = 1;
2375 t.bitfield.imm32 = 1;
2376 t.bitfield.imm32s = 1;
2377 }
2378 else if (fits_in_unsigned_byte (num))
2379 {
2380 t.bitfield.imm8 = 1;
2381 t.bitfield.imm16 = 1;
2382 t.bitfield.imm32 = 1;
2383 t.bitfield.imm32s = 1;
2384 }
2385 else if (fits_in_signed_word (num) || fits_in_unsigned_word (num))
2386 {
2387 t.bitfield.imm16 = 1;
2388 t.bitfield.imm32 = 1;
2389 t.bitfield.imm32s = 1;
2390 }
2391 else if (fits_in_signed_long (num))
2392 {
2393 t.bitfield.imm32 = 1;
2394 t.bitfield.imm32s = 1;
2395 }
2396 else if (fits_in_unsigned_long (num))
2397 t.bitfield.imm32 = 1;
2398
2399 return t;
47926f60 2400}
252b5132 2401
847f7ad4 2402static offsetT
e3bb37b5 2403offset_in_range (offsetT val, int size)
847f7ad4 2404{
508866be 2405 addressT mask;
ba2adb93 2406
847f7ad4
AM
2407 switch (size)
2408 {
508866be
L
2409 case 1: mask = ((addressT) 1 << 8) - 1; break;
2410 case 2: mask = ((addressT) 1 << 16) - 1; break;
3e73aa7c 2411#ifdef BFD64
64965897 2412 case 4: mask = ((addressT) 1 << 32) - 1; break;
3e73aa7c 2413#endif
64965897 2414 case sizeof (val): return val;
47926f60 2415 default: abort ();
847f7ad4
AM
2416 }
2417
4fe51f7d 2418 if ((val & ~mask) != 0 && (-val & ~mask) != 0)
f493c217
AM
2419 as_warn (_("0x%" PRIx64 " shortened to 0x%" PRIx64),
2420 (uint64_t) val, (uint64_t) (val & mask));
847f7ad4 2421
847f7ad4
AM
2422 return val & mask;
2423}
2424
c32fa91d
L
2425enum PREFIX_GROUP
2426{
2427 PREFIX_EXIST = 0,
2428 PREFIX_LOCK,
2429 PREFIX_REP,
04ef582a 2430 PREFIX_DS,
c32fa91d
L
2431 PREFIX_OTHER
2432};
2433
2434/* Returns
2435 a. PREFIX_EXIST if attempting to add a prefix where one from the
2436 same class already exists.
2437 b. PREFIX_LOCK if lock prefix is added.
2438 c. PREFIX_REP if rep/repne prefix is added.
04ef582a
L
2439 d. PREFIX_DS if ds prefix is added.
2440 e. PREFIX_OTHER if other prefix is added.
c32fa91d
L
2441 */
2442
2443static enum PREFIX_GROUP
e3bb37b5 2444add_prefix (unsigned int prefix)
252b5132 2445{
c32fa91d 2446 enum PREFIX_GROUP ret = PREFIX_OTHER;
b1905489 2447 unsigned int q;
252b5132 2448
29b0f896
AM
2449 if (prefix >= REX_OPCODE && prefix < REX_OPCODE + 16
2450 && flag_code == CODE_64BIT)
b1905489 2451 {
161a04f6 2452 if ((i.prefix[REX_PREFIX] & prefix & REX_W)
44846f29
JB
2453 || (i.prefix[REX_PREFIX] & prefix & REX_R)
2454 || (i.prefix[REX_PREFIX] & prefix & REX_X)
2455 || (i.prefix[REX_PREFIX] & prefix & REX_B))
c32fa91d 2456 ret = PREFIX_EXIST;
b1905489
JB
2457 q = REX_PREFIX;
2458 }
3e73aa7c 2459 else
b1905489
JB
2460 {
2461 switch (prefix)
2462 {
2463 default:
2464 abort ();
2465
b1905489 2466 case DS_PREFIX_OPCODE:
04ef582a
L
2467 ret = PREFIX_DS;
2468 /* Fall through. */
2469 case CS_PREFIX_OPCODE:
b1905489
JB
2470 case ES_PREFIX_OPCODE:
2471 case FS_PREFIX_OPCODE:
2472 case GS_PREFIX_OPCODE:
2473 case SS_PREFIX_OPCODE:
2474 q = SEG_PREFIX;
2475 break;
2476
2477 case REPNE_PREFIX_OPCODE:
2478 case REPE_PREFIX_OPCODE:
c32fa91d
L
2479 q = REP_PREFIX;
2480 ret = PREFIX_REP;
2481 break;
2482
b1905489 2483 case LOCK_PREFIX_OPCODE:
c32fa91d
L
2484 q = LOCK_PREFIX;
2485 ret = PREFIX_LOCK;
b1905489
JB
2486 break;
2487
2488 case FWAIT_OPCODE:
2489 q = WAIT_PREFIX;
2490 break;
2491
2492 case ADDR_PREFIX_OPCODE:
2493 q = ADDR_PREFIX;
2494 break;
2495
2496 case DATA_PREFIX_OPCODE:
2497 q = DATA_PREFIX;
2498 break;
2499 }
2500 if (i.prefix[q] != 0)
c32fa91d 2501 ret = PREFIX_EXIST;
b1905489 2502 }
252b5132 2503
b1905489 2504 if (ret)
252b5132 2505 {
b1905489
JB
2506 if (!i.prefix[q])
2507 ++i.prefixes;
2508 i.prefix[q] |= prefix;
252b5132 2509 }
b1905489
JB
2510 else
2511 as_bad (_("same type of prefix used twice"));
252b5132 2512
252b5132
RH
2513 return ret;
2514}
2515
2516static void
78f12dd3 2517update_code_flag (int value, int check)
eecb386c 2518{
78f12dd3
L
2519 PRINTF_LIKE ((*as_error));
2520
1e9cc1c2 2521 flag_code = (enum flag_code) value;
40fb9820
L
2522 if (flag_code == CODE_64BIT)
2523 {
2524 cpu_arch_flags.bitfield.cpu64 = 1;
2525 cpu_arch_flags.bitfield.cpuno64 = 0;
40fb9820
L
2526 }
2527 else
2528 {
2529 cpu_arch_flags.bitfield.cpu64 = 0;
2530 cpu_arch_flags.bitfield.cpuno64 = 1;
40fb9820
L
2531 }
2532 if (value == CODE_64BIT && !cpu_arch_flags.bitfield.cpulm )
3e73aa7c 2533 {
78f12dd3
L
2534 if (check)
2535 as_error = as_fatal;
2536 else
2537 as_error = as_bad;
2538 (*as_error) (_("64bit mode not supported on `%s'."),
2539 cpu_arch_name ? cpu_arch_name : default_arch);
3e73aa7c 2540 }
40fb9820 2541 if (value == CODE_32BIT && !cpu_arch_flags.bitfield.cpui386)
3e73aa7c 2542 {
78f12dd3
L
2543 if (check)
2544 as_error = as_fatal;
2545 else
2546 as_error = as_bad;
2547 (*as_error) (_("32bit mode not supported on `%s'."),
2548 cpu_arch_name ? cpu_arch_name : default_arch);
3e73aa7c 2549 }
eecb386c
AM
2550 stackop_size = '\0';
2551}
2552
78f12dd3
L
2553static void
2554set_code_flag (int value)
2555{
2556 update_code_flag (value, 0);
2557}
2558
eecb386c 2559static void
e3bb37b5 2560set_16bit_gcc_code_flag (int new_code_flag)
252b5132 2561{
1e9cc1c2 2562 flag_code = (enum flag_code) new_code_flag;
40fb9820
L
2563 if (flag_code != CODE_16BIT)
2564 abort ();
2565 cpu_arch_flags.bitfield.cpu64 = 0;
2566 cpu_arch_flags.bitfield.cpuno64 = 1;
9306ca4a 2567 stackop_size = LONG_MNEM_SUFFIX;
252b5132
RH
2568}
2569
2570static void
e3bb37b5 2571set_intel_syntax (int syntax_flag)
252b5132
RH
2572{
2573 /* Find out if register prefixing is specified. */
2574 int ask_naked_reg = 0;
2575
2576 SKIP_WHITESPACE ();
29b0f896 2577 if (!is_end_of_line[(unsigned char) *input_line_pointer])
252b5132 2578 {
d02603dc
NC
2579 char *string;
2580 int e = get_symbol_name (&string);
252b5132 2581
47926f60 2582 if (strcmp (string, "prefix") == 0)
252b5132 2583 ask_naked_reg = 1;
47926f60 2584 else if (strcmp (string, "noprefix") == 0)
252b5132
RH
2585 ask_naked_reg = -1;
2586 else
d0b47220 2587 as_bad (_("bad argument to syntax directive."));
d02603dc 2588 (void) restore_line_pointer (e);
252b5132
RH
2589 }
2590 demand_empty_rest_of_line ();
c3332e24 2591
252b5132
RH
2592 intel_syntax = syntax_flag;
2593
2594 if (ask_naked_reg == 0)
f86103b7
AM
2595 allow_naked_reg = (intel_syntax
2596 && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
252b5132
RH
2597 else
2598 allow_naked_reg = (ask_naked_reg < 0);
9306ca4a 2599
ee86248c 2600 expr_set_rank (O_full_ptr, syntax_flag ? 10 : 0);
7ab9ffdd 2601
e4a3b5a4 2602 identifier_chars['%'] = intel_syntax && allow_naked_reg ? '%' : 0;
9306ca4a 2603 identifier_chars['$'] = intel_syntax ? '$' : 0;
e4a3b5a4 2604 register_prefix = allow_naked_reg ? "" : "%";
252b5132
RH
2605}
2606
1efbbeb4
L
2607static void
2608set_intel_mnemonic (int mnemonic_flag)
2609{
e1d4d893 2610 intel_mnemonic = mnemonic_flag;
1efbbeb4
L
2611}
2612
db51cc60
L
2613static void
2614set_allow_index_reg (int flag)
2615{
2616 allow_index_reg = flag;
2617}
2618
cb19c032 2619static void
7bab8ab5 2620set_check (int what)
cb19c032 2621{
7bab8ab5
JB
2622 enum check_kind *kind;
2623 const char *str;
2624
2625 if (what)
2626 {
2627 kind = &operand_check;
2628 str = "operand";
2629 }
2630 else
2631 {
2632 kind = &sse_check;
2633 str = "sse";
2634 }
2635
cb19c032
L
2636 SKIP_WHITESPACE ();
2637
2638 if (!is_end_of_line[(unsigned char) *input_line_pointer])
2639 {
d02603dc
NC
2640 char *string;
2641 int e = get_symbol_name (&string);
cb19c032
L
2642
2643 if (strcmp (string, "none") == 0)
7bab8ab5 2644 *kind = check_none;
cb19c032 2645 else if (strcmp (string, "warning") == 0)
7bab8ab5 2646 *kind = check_warning;
cb19c032 2647 else if (strcmp (string, "error") == 0)
7bab8ab5 2648 *kind = check_error;
cb19c032 2649 else
7bab8ab5 2650 as_bad (_("bad argument to %s_check directive."), str);
d02603dc 2651 (void) restore_line_pointer (e);
cb19c032
L
2652 }
2653 else
7bab8ab5 2654 as_bad (_("missing argument for %s_check directive"), str);
cb19c032
L
2655
2656 demand_empty_rest_of_line ();
2657}
2658
8a9036a4
L
2659static void
2660check_cpu_arch_compatible (const char *name ATTRIBUTE_UNUSED,
1e9cc1c2 2661 i386_cpu_flags new_flag ATTRIBUTE_UNUSED)
8a9036a4
L
2662{
2663#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2664 static const char *arch;
2665
c085ab00 2666 /* Intel MCU is only supported on ELF. */
8a9036a4
L
2667 if (!IS_ELF)
2668 return;
2669
2670 if (!arch)
2671 {
2672 /* Use cpu_arch_name if it is set in md_parse_option. Otherwise
2673 use default_arch. */
2674 arch = cpu_arch_name;
2675 if (!arch)
2676 arch = default_arch;
2677 }
2678
81486035 2679 /* If we are targeting Intel MCU, we must enable it. */
648d04db
JB
2680 if ((get_elf_backend_data (stdoutput)->elf_machine_code == EM_IAMCU)
2681 == new_flag.bitfield.cpuiamcu)
81486035
L
2682 return;
2683
8a9036a4
L
2684 as_bad (_("`%s' is not supported on `%s'"), name, arch);
2685#endif
2686}
2687
8180707f
JB
2688static void
2689extend_cpu_sub_arch_name (const char *name)
2690{
2691 if (cpu_sub_arch_name)
2692 cpu_sub_arch_name = reconcat (cpu_sub_arch_name, cpu_sub_arch_name,
ae89daec 2693 ".", name, (const char *) NULL);
8180707f 2694 else
ae89daec 2695 cpu_sub_arch_name = concat (".", name, (const char *) NULL);
8180707f
JB
2696}
2697
e413e4e9 2698static void
e3bb37b5 2699set_cpu_arch (int dummy ATTRIBUTE_UNUSED)
e413e4e9 2700{
f68697e8
JB
2701 typedef struct arch_stack_entry
2702 {
2703 const struct arch_stack_entry *prev;
2704 const char *name;
2705 char *sub_name;
2706 i386_cpu_flags flags;
2707 i386_cpu_flags isa_flags;
2708 enum processor_type isa;
2709 enum flag_code flag_code;
2710 char stackop_size;
2711 bool no_cond_jump_promotion;
2712 } arch_stack_entry;
2713 static const arch_stack_entry *arch_stack_top;
2714
47926f60 2715 SKIP_WHITESPACE ();
e413e4e9 2716
29b0f896 2717 if (!is_end_of_line[(unsigned char) *input_line_pointer])
e413e4e9 2718 {
3ce2ebcf
JB
2719 char *s;
2720 int e = get_symbol_name (&s);
2721 const char *string = s;
2722 unsigned int j = 0;
40fb9820 2723 i386_cpu_flags flags;
e413e4e9 2724
3ce2ebcf
JB
2725 if (strcmp (string, "default") == 0)
2726 {
2727 if (strcmp (default_arch, "iamcu") == 0)
2728 string = default_arch;
2729 else
2730 {
2731 static const i386_cpu_flags cpu_unknown_flags = CPU_UNKNOWN_FLAGS;
2732
2733 cpu_arch_name = NULL;
2734 free (cpu_sub_arch_name);
2735 cpu_sub_arch_name = NULL;
2736 cpu_arch_flags = cpu_unknown_flags;
2737 if (flag_code == CODE_64BIT)
2738 {
2739 cpu_arch_flags.bitfield.cpu64 = 1;
2740 cpu_arch_flags.bitfield.cpuno64 = 0;
2741 }
2742 else
2743 {
2744 cpu_arch_flags.bitfield.cpu64 = 0;
2745 cpu_arch_flags.bitfield.cpuno64 = 1;
2746 }
2747 cpu_arch_isa = PROCESSOR_UNKNOWN;
ae89daec 2748 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
3ce2ebcf
JB
2749 if (!cpu_arch_tune_set)
2750 {
2751 cpu_arch_tune = cpu_arch_isa;
2752 cpu_arch_tune_flags = cpu_arch_isa_flags;
2753 }
2754
2755 j = ARRAY_SIZE (cpu_arch) + 1;
2756 }
2757 }
f68697e8
JB
2758 else if (strcmp (string, "push") == 0)
2759 {
2760 arch_stack_entry *top = XNEW (arch_stack_entry);
2761
2762 top->name = cpu_arch_name;
2763 if (cpu_sub_arch_name)
2764 top->sub_name = xstrdup (cpu_sub_arch_name);
2765 else
2766 top->sub_name = NULL;
2767 top->flags = cpu_arch_flags;
2768 top->isa = cpu_arch_isa;
2769 top->isa_flags = cpu_arch_isa_flags;
2770 top->flag_code = flag_code;
2771 top->stackop_size = stackop_size;
2772 top->no_cond_jump_promotion = no_cond_jump_promotion;
2773
2774 top->prev = arch_stack_top;
2775 arch_stack_top = top;
2776
2777 (void) restore_line_pointer (e);
2778 demand_empty_rest_of_line ();
2779 return;
2780 }
2781 else if (strcmp (string, "pop") == 0)
2782 {
2783 const arch_stack_entry *top = arch_stack_top;
2784
2785 if (!top)
2786 as_bad (_(".arch stack is empty"));
2787 else if (top->flag_code != flag_code
2788 || top->stackop_size != stackop_size)
2789 {
2790 static const unsigned int bits[] = {
2791 [CODE_16BIT] = 16,
2792 [CODE_32BIT] = 32,
2793 [CODE_64BIT] = 64,
2794 };
2795
2796 as_bad (_("this `.arch pop' requires `.code%u%s' to be in effect"),
2797 bits[top->flag_code],
2798 top->stackop_size == LONG_MNEM_SUFFIX ? "gcc" : "");
2799 }
2800 else
2801 {
2802 arch_stack_top = top->prev;
2803
2804 cpu_arch_name = top->name;
2805 free (cpu_sub_arch_name);
2806 cpu_sub_arch_name = top->sub_name;
2807 cpu_arch_flags = top->flags;
2808 cpu_arch_isa = top->isa;
2809 cpu_arch_isa_flags = top->isa_flags;
2810 no_cond_jump_promotion = top->no_cond_jump_promotion;
2811
2812 XDELETE (top);
2813 }
2814
2815 (void) restore_line_pointer (e);
2816 demand_empty_rest_of_line ();
2817 return;
2818 }
3ce2ebcf
JB
2819
2820 for (; j < ARRAY_SIZE (cpu_arch); j++)
e413e4e9 2821 {
ae89daec
JB
2822 if (strcmp (string + (*string == '.'), cpu_arch[j].name) == 0
2823 && (*string == '.') == (cpu_arch[j].type == PROCESSOR_NONE))
e413e4e9 2824 {
5c6af06e
JB
2825 if (*string != '.')
2826 {
ae89daec 2827 check_cpu_arch_compatible (string, cpu_arch[j].enable);
648d04db 2828
91d6fa6a 2829 cpu_arch_name = cpu_arch[j].name;
d92c7521 2830 free (cpu_sub_arch_name);
5c6af06e 2831 cpu_sub_arch_name = NULL;
ae89daec 2832 cpu_arch_flags = cpu_arch[j].enable;
40fb9820
L
2833 if (flag_code == CODE_64BIT)
2834 {
2835 cpu_arch_flags.bitfield.cpu64 = 1;
2836 cpu_arch_flags.bitfield.cpuno64 = 0;
2837 }
2838 else
2839 {
2840 cpu_arch_flags.bitfield.cpu64 = 0;
2841 cpu_arch_flags.bitfield.cpuno64 = 1;
2842 }
91d6fa6a 2843 cpu_arch_isa = cpu_arch[j].type;
ae89daec 2844 cpu_arch_isa_flags = cpu_arch[j].enable;
ccc9c027
L
2845 if (!cpu_arch_tune_set)
2846 {
2847 cpu_arch_tune = cpu_arch_isa;
2848 cpu_arch_tune_flags = cpu_arch_isa_flags;
2849 }
d59a54c2 2850 pre_386_16bit_warned = false;
5c6af06e
JB
2851 break;
2852 }
40fb9820 2853
ae89daec
JB
2854 if (cpu_flags_all_zero (&cpu_arch[j].enable))
2855 continue;
2856
293f5f65 2857 flags = cpu_flags_or (cpu_arch_flags,
ae89daec 2858 cpu_arch[j].enable);
81486035 2859
5b64d091 2860 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
5c6af06e 2861 {
ae89daec 2862 extend_cpu_sub_arch_name (string + 1);
40fb9820 2863 cpu_arch_flags = flags;
a586129e 2864 cpu_arch_isa_flags = flags;
5c6af06e 2865 }
0089dace
L
2866 else
2867 cpu_arch_isa_flags
2868 = cpu_flags_or (cpu_arch_isa_flags,
ae89daec 2869 cpu_arch[j].enable);
d02603dc 2870 (void) restore_line_pointer (e);
5c6af06e
JB
2871 demand_empty_rest_of_line ();
2872 return;
e413e4e9
AM
2873 }
2874 }
293f5f65 2875
ae89daec 2876 if (startswith (string, ".no") && j >= ARRAY_SIZE (cpu_arch))
293f5f65 2877 {
33eaf5de 2878 /* Disable an ISA extension. */
ae89daec
JB
2879 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
2880 if (cpu_arch[j].type == PROCESSOR_NONE
2881 && strcmp (string + 3, cpu_arch[j].name) == 0)
293f5f65
L
2882 {
2883 flags = cpu_flags_and_not (cpu_arch_flags,
ae89daec 2884 cpu_arch[j].disable);
293f5f65
L
2885 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
2886 {
ae89daec 2887 extend_cpu_sub_arch_name (string + 1);
293f5f65
L
2888 cpu_arch_flags = flags;
2889 cpu_arch_isa_flags = flags;
2890 }
2891 (void) restore_line_pointer (e);
2892 demand_empty_rest_of_line ();
2893 return;
2894 }
293f5f65
L
2895 }
2896
3ce2ebcf 2897 if (j == ARRAY_SIZE (cpu_arch))
e413e4e9
AM
2898 as_bad (_("no such architecture: `%s'"), string);
2899
2900 *input_line_pointer = e;
2901 }
2902 else
2903 as_bad (_("missing cpu architecture"));
2904
fddf5b5b
AM
2905 no_cond_jump_promotion = 0;
2906 if (*input_line_pointer == ','
29b0f896 2907 && !is_end_of_line[(unsigned char) input_line_pointer[1]])
fddf5b5b 2908 {
d02603dc
NC
2909 char *string;
2910 char e;
2911
2912 ++input_line_pointer;
2913 e = get_symbol_name (&string);
fddf5b5b
AM
2914
2915 if (strcmp (string, "nojumps") == 0)
2916 no_cond_jump_promotion = 1;
2917 else if (strcmp (string, "jumps") == 0)
2918 ;
2919 else
2920 as_bad (_("no such architecture modifier: `%s'"), string);
2921
d02603dc 2922 (void) restore_line_pointer (e);
fddf5b5b
AM
2923 }
2924
e413e4e9
AM
2925 demand_empty_rest_of_line ();
2926}
2927
8a9036a4
L
2928enum bfd_architecture
2929i386_arch (void)
2930{
c085ab00 2931 if (cpu_arch_isa == PROCESSOR_IAMCU)
81486035
L
2932 {
2933 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2934 || flag_code == CODE_64BIT)
2935 as_fatal (_("Intel MCU is 32bit ELF only"));
2936 return bfd_arch_iamcu;
2937 }
8a9036a4
L
2938 else
2939 return bfd_arch_i386;
2940}
2941
b9d79e03 2942unsigned long
7016a5d5 2943i386_mach (void)
b9d79e03 2944{
d34049e8 2945 if (startswith (default_arch, "x86_64"))
8a9036a4 2946 {
c085ab00 2947 if (default_arch[6] == '\0')
8a9036a4 2948 return bfd_mach_x86_64;
351f65ca
L
2949 else
2950 return bfd_mach_x64_32;
8a9036a4 2951 }
5197d474
L
2952 else if (!strcmp (default_arch, "i386")
2953 || !strcmp (default_arch, "iamcu"))
81486035
L
2954 {
2955 if (cpu_arch_isa == PROCESSOR_IAMCU)
2956 {
2957 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
2958 as_fatal (_("Intel MCU is 32bit ELF only"));
2959 return bfd_mach_i386_iamcu;
2960 }
2961 else
2962 return bfd_mach_i386_i386;
2963 }
b9d79e03 2964 else
2b5d6a91 2965 as_fatal (_("unknown architecture"));
b9d79e03 2966}
b9d79e03 2967\f
252b5132 2968void
7016a5d5 2969md_begin (void)
252b5132 2970{
86fa6981
L
2971 /* Support pseudo prefixes like {disp32}. */
2972 lex_type ['{'] = LEX_BEGIN_NAME;
2973
47926f60 2974 /* Initialize op_hash hash table. */
629310ab 2975 op_hash = str_htab_create ();
252b5132
RH
2976
2977 {
d3ce72d0 2978 const insn_template *optab;
29b0f896 2979 templates *core_optab;
252b5132 2980
47926f60
KH
2981 /* Setup for loop. */
2982 optab = i386_optab;
654d6f31 2983 core_optab = notes_alloc (sizeof (*core_optab));
252b5132
RH
2984 core_optab->start = optab;
2985
2986 while (1)
2987 {
2988 ++optab;
2989 if (optab->name == NULL
2990 || strcmp (optab->name, (optab - 1)->name) != 0)
2991 {
2992 /* different name --> ship out current template list;
47926f60 2993 add to hash table; & begin anew. */
252b5132 2994 core_optab->end = optab;
fe0e921f
AM
2995 if (str_hash_insert (op_hash, (optab - 1)->name, core_optab, 0))
2996 as_fatal (_("duplicate %s"), (optab - 1)->name);
2997
252b5132
RH
2998 if (optab->name == NULL)
2999 break;
654d6f31 3000 core_optab = notes_alloc (sizeof (*core_optab));
252b5132
RH
3001 core_optab->start = optab;
3002 }
3003 }
3004 }
3005
47926f60 3006 /* Initialize reg_hash hash table. */
629310ab 3007 reg_hash = str_htab_create ();
252b5132 3008 {
29b0f896 3009 const reg_entry *regtab;
c3fe08fa 3010 unsigned int regtab_size = i386_regtab_size;
252b5132 3011
c3fe08fa 3012 for (regtab = i386_regtab; regtab_size--; regtab++)
6225c532 3013 {
6288d05f
JB
3014 switch (regtab->reg_type.bitfield.class)
3015 {
3016 case Reg:
34684862
JB
3017 if (regtab->reg_type.bitfield.dword)
3018 {
3019 if (regtab->reg_type.bitfield.instance == Accum)
3020 reg_eax = regtab;
3021 }
3022 else if (regtab->reg_type.bitfield.tbyte)
6288d05f
JB
3023 {
3024 /* There's no point inserting st(<N>) in the hash table, as
3025 parentheses aren't included in register_chars[] anyway. */
3026 if (regtab->reg_type.bitfield.instance != Accum)
3027 continue;
3028 reg_st0 = regtab;
3029 }
3030 break;
3031
5e042380
JB
3032 case SReg:
3033 switch (regtab->reg_num)
3034 {
3035 case 0: reg_es = regtab; break;
3036 case 2: reg_ss = regtab; break;
3037 case 3: reg_ds = regtab; break;
3038 }
3039 break;
3040
6288d05f
JB
3041 case RegMask:
3042 if (!regtab->reg_num)
3043 reg_k0 = regtab;
3044 break;
3045 }
3046
6225c532
JB
3047 if (str_hash_insert (reg_hash, regtab->reg_name, regtab, 0) != NULL)
3048 as_fatal (_("duplicate %s"), regtab->reg_name);
6225c532 3049 }
252b5132
RH
3050 }
3051
47926f60 3052 /* Fill in lexical tables: mnemonic_chars, operand_chars. */
252b5132 3053 {
29b0f896
AM
3054 int c;
3055 char *p;
252b5132
RH
3056
3057 for (c = 0; c < 256; c++)
3058 {
014fbcda 3059 if (ISDIGIT (c) || ISLOWER (c))
252b5132
RH
3060 {
3061 mnemonic_chars[c] = c;
3062 register_chars[c] = c;
3063 operand_chars[c] = c;
3064 }
3882b010 3065 else if (ISUPPER (c))
252b5132 3066 {
3882b010 3067 mnemonic_chars[c] = TOLOWER (c);
252b5132
RH
3068 register_chars[c] = mnemonic_chars[c];
3069 operand_chars[c] = c;
3070 }
43234a1e 3071 else if (c == '{' || c == '}')
86fa6981
L
3072 {
3073 mnemonic_chars[c] = c;
3074 operand_chars[c] = c;
3075 }
b3983e5f
JB
3076#ifdef SVR4_COMMENT_CHARS
3077 else if (c == '\\' && strchr (i386_comment_chars, '/'))
3078 operand_chars[c] = c;
3079#endif
252b5132 3080
3882b010 3081 if (ISALPHA (c) || ISDIGIT (c))
252b5132
RH
3082 identifier_chars[c] = c;
3083 else if (c >= 128)
3084 {
3085 identifier_chars[c] = c;
3086 operand_chars[c] = c;
3087 }
3088 }
3089
3090#ifdef LEX_AT
3091 identifier_chars['@'] = '@';
32137342
NC
3092#endif
3093#ifdef LEX_QM
3094 identifier_chars['?'] = '?';
3095 operand_chars['?'] = '?';
252b5132 3096#endif
c0f3af97 3097 mnemonic_chars['_'] = '_';
791fe849 3098 mnemonic_chars['-'] = '-';
0003779b 3099 mnemonic_chars['.'] = '.';
252b5132
RH
3100 identifier_chars['_'] = '_';
3101 identifier_chars['.'] = '.';
3102
3103 for (p = operand_special_chars; *p != '\0'; p++)
3104 operand_chars[(unsigned char) *p] = *p;
3105 }
3106
a4447b93
RH
3107 if (flag_code == CODE_64BIT)
3108 {
ca19b261
KT
3109#if defined (OBJ_COFF) && defined (TE_PE)
3110 x86_dwarf2_return_column = (OUTPUT_FLAVOR == bfd_target_coff_flavour
3111 ? 32 : 16);
3112#else
a4447b93 3113 x86_dwarf2_return_column = 16;
ca19b261 3114#endif
61ff971f 3115 x86_cie_data_alignment = -8;
b52c4ee4
IB
3116#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3117 x86_sframe_cfa_sp_reg = 7;
3118 x86_sframe_cfa_fp_reg = 6;
3119#endif
a4447b93
RH
3120 }
3121 else
3122 {
3123 x86_dwarf2_return_column = 8;
3124 x86_cie_data_alignment = -4;
3125 }
e379e5f3
L
3126
3127 /* NB: FUSED_JCC_PADDING frag must have sufficient room so that it
3128 can be turned into BRANCH_PREFIX frag. */
3129 if (align_branch_prefix_size > MAX_FUSED_JCC_PADDING_SIZE)
3130 abort ();
252b5132
RH
3131}
3132
3133void
e3bb37b5 3134i386_print_statistics (FILE *file)
252b5132 3135{
629310ab
ML
3136 htab_print_statistics (file, "i386 opcode", op_hash);
3137 htab_print_statistics (file, "i386 register", reg_hash);
252b5132 3138}
654d6f31
AM
3139
3140void
3141i386_md_end (void)
3142{
3143 htab_delete (op_hash);
3144 htab_delete (reg_hash);
3145}
252b5132 3146\f
252b5132
RH
3147#ifdef DEBUG386
3148
ce8a8b2f 3149/* Debugging routines for md_assemble. */
d3ce72d0 3150static void pte (insn_template *);
40fb9820 3151static void pt (i386_operand_type);
e3bb37b5
L
3152static void pe (expressionS *);
3153static void ps (symbolS *);
252b5132
RH
3154
3155static void
2c703856 3156pi (const char *line, i386_insn *x)
252b5132 3157{
09137c09 3158 unsigned int j;
252b5132
RH
3159
3160 fprintf (stdout, "%s: template ", line);
3161 pte (&x->tm);
09f131f2
JH
3162 fprintf (stdout, " address: base %s index %s scale %x\n",
3163 x->base_reg ? x->base_reg->reg_name : "none",
3164 x->index_reg ? x->index_reg->reg_name : "none",
3165 x->log2_scale_factor);
3166 fprintf (stdout, " modrm: mode %x reg %x reg/mem %x\n",
252b5132 3167 x->rm.mode, x->rm.reg, x->rm.regmem);
09f131f2
JH
3168 fprintf (stdout, " sib: base %x index %x scale %x\n",
3169 x->sib.base, x->sib.index, x->sib.scale);
3170 fprintf (stdout, " rex: 64bit %x extX %x extY %x extZ %x\n",
161a04f6
L
3171 (x->rex & REX_W) != 0,
3172 (x->rex & REX_R) != 0,
3173 (x->rex & REX_X) != 0,
3174 (x->rex & REX_B) != 0);
09137c09 3175 for (j = 0; j < x->operands; j++)
252b5132 3176 {
09137c09
SP
3177 fprintf (stdout, " #%d: ", j + 1);
3178 pt (x->types[j]);
252b5132 3179 fprintf (stdout, "\n");
bab6aec1 3180 if (x->types[j].bitfield.class == Reg
3528c362
JB
3181 || x->types[j].bitfield.class == RegMMX
3182 || x->types[j].bitfield.class == RegSIMD
dd6b8a0b 3183 || x->types[j].bitfield.class == RegMask
00cee14f 3184 || x->types[j].bitfield.class == SReg
4a5c67ed
JB
3185 || x->types[j].bitfield.class == RegCR
3186 || x->types[j].bitfield.class == RegDR
dd6b8a0b
JB
3187 || x->types[j].bitfield.class == RegTR
3188 || x->types[j].bitfield.class == RegBND)
09137c09
SP
3189 fprintf (stdout, "%s\n", x->op[j].regs->reg_name);
3190 if (operand_type_check (x->types[j], imm))
3191 pe (x->op[j].imms);
3192 if (operand_type_check (x->types[j], disp))
3193 pe (x->op[j].disps);
252b5132
RH
3194 }
3195}
3196
3197static void
d3ce72d0 3198pte (insn_template *t)
252b5132 3199{
b933fa4b 3200 static const unsigned char opc_pfx[] = { 0, 0x66, 0xf3, 0xf2 };
441f6aca 3201 static const char *const opc_spc[] = {
0cc78721 3202 NULL, "0f", "0f38", "0f3a", NULL, "evexmap5", "evexmap6", NULL,
441f6aca
JB
3203 "XOP08", "XOP09", "XOP0A",
3204 };
09137c09 3205 unsigned int j;
441f6aca 3206
252b5132 3207 fprintf (stdout, " %d operands ", t->operands);
441f6aca
JB
3208 if (opc_pfx[t->opcode_modifier.opcodeprefix])
3209 fprintf (stdout, "pfx %x ", opc_pfx[t->opcode_modifier.opcodeprefix]);
3210 if (opc_spc[t->opcode_modifier.opcodespace])
3211 fprintf (stdout, "space %s ", opc_spc[t->opcode_modifier.opcodespace]);
47926f60 3212 fprintf (stdout, "opcode %x ", t->base_opcode);
252b5132
RH
3213 if (t->extension_opcode != None)
3214 fprintf (stdout, "ext %x ", t->extension_opcode);
40fb9820 3215 if (t->opcode_modifier.d)
252b5132 3216 fprintf (stdout, "D");
40fb9820 3217 if (t->opcode_modifier.w)
252b5132
RH
3218 fprintf (stdout, "W");
3219 fprintf (stdout, "\n");
09137c09 3220 for (j = 0; j < t->operands; j++)
252b5132 3221 {
09137c09
SP
3222 fprintf (stdout, " #%d type ", j + 1);
3223 pt (t->operand_types[j]);
252b5132
RH
3224 fprintf (stdout, "\n");
3225 }
3226}
3227
3228static void
e3bb37b5 3229pe (expressionS *e)
252b5132 3230{
24eab124 3231 fprintf (stdout, " operation %d\n", e->X_op);
b8281767
AM
3232 fprintf (stdout, " add_number %" PRId64 " (%" PRIx64 ")\n",
3233 (int64_t) e->X_add_number, (uint64_t) (valueT) e->X_add_number);
252b5132
RH
3234 if (e->X_add_symbol)
3235 {
3236 fprintf (stdout, " add_symbol ");
3237 ps (e->X_add_symbol);
3238 fprintf (stdout, "\n");
3239 }
3240 if (e->X_op_symbol)
3241 {
3242 fprintf (stdout, " op_symbol ");
3243 ps (e->X_op_symbol);
3244 fprintf (stdout, "\n");
3245 }
3246}
3247
3248static void
e3bb37b5 3249ps (symbolS *s)
252b5132
RH
3250{
3251 fprintf (stdout, "%s type %s%s",
3252 S_GET_NAME (s),
3253 S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
3254 segment_name (S_GET_SEGMENT (s)));
3255}
3256
7b81dfbb 3257static struct type_name
252b5132 3258 {
40fb9820
L
3259 i386_operand_type mask;
3260 const char *name;
252b5132 3261 }
7b81dfbb 3262const type_names[] =
252b5132 3263{
40fb9820
L
3264 { OPERAND_TYPE_REG8, "r8" },
3265 { OPERAND_TYPE_REG16, "r16" },
3266 { OPERAND_TYPE_REG32, "r32" },
3267 { OPERAND_TYPE_REG64, "r64" },
2c703856
JB
3268 { OPERAND_TYPE_ACC8, "acc8" },
3269 { OPERAND_TYPE_ACC16, "acc16" },
3270 { OPERAND_TYPE_ACC32, "acc32" },
3271 { OPERAND_TYPE_ACC64, "acc64" },
40fb9820
L
3272 { OPERAND_TYPE_IMM8, "i8" },
3273 { OPERAND_TYPE_IMM8, "i8s" },
3274 { OPERAND_TYPE_IMM16, "i16" },
3275 { OPERAND_TYPE_IMM32, "i32" },
3276 { OPERAND_TYPE_IMM32S, "i32s" },
3277 { OPERAND_TYPE_IMM64, "i64" },
3278 { OPERAND_TYPE_IMM1, "i1" },
3279 { OPERAND_TYPE_BASEINDEX, "BaseIndex" },
3280 { OPERAND_TYPE_DISP8, "d8" },
3281 { OPERAND_TYPE_DISP16, "d16" },
3282 { OPERAND_TYPE_DISP32, "d32" },
40fb9820
L
3283 { OPERAND_TYPE_DISP64, "d64" },
3284 { OPERAND_TYPE_INOUTPORTREG, "InOutPortReg" },
3285 { OPERAND_TYPE_SHIFTCOUNT, "ShiftCount" },
3286 { OPERAND_TYPE_CONTROL, "control reg" },
3287 { OPERAND_TYPE_TEST, "test reg" },
3288 { OPERAND_TYPE_DEBUG, "debug reg" },
3289 { OPERAND_TYPE_FLOATREG, "FReg" },
3290 { OPERAND_TYPE_FLOATACC, "FAcc" },
21df382b 3291 { OPERAND_TYPE_SREG, "SReg" },
40fb9820
L
3292 { OPERAND_TYPE_REGMMX, "rMMX" },
3293 { OPERAND_TYPE_REGXMM, "rXMM" },
0349dc08 3294 { OPERAND_TYPE_REGYMM, "rYMM" },
43234a1e 3295 { OPERAND_TYPE_REGZMM, "rZMM" },
260cd341 3296 { OPERAND_TYPE_REGTMM, "rTMM" },
43234a1e 3297 { OPERAND_TYPE_REGMASK, "Mask reg" },
252b5132
RH
3298};
3299
3300static void
40fb9820 3301pt (i386_operand_type t)
252b5132 3302{
40fb9820 3303 unsigned int j;
c6fb90c8 3304 i386_operand_type a;
252b5132 3305
40fb9820 3306 for (j = 0; j < ARRAY_SIZE (type_names); j++)
c6fb90c8
L
3307 {
3308 a = operand_type_and (t, type_names[j].mask);
2c703856 3309 if (operand_type_equal (&a, &type_names[j].mask))
c6fb90c8
L
3310 fprintf (stdout, "%s, ", type_names[j].name);
3311 }
252b5132
RH
3312 fflush (stdout);
3313}
3314
3315#endif /* DEBUG386 */
3316\f
252b5132 3317static bfd_reloc_code_real_type
3956db08 3318reloc (unsigned int size,
64e74474
AM
3319 int pcrel,
3320 int sign,
3321 bfd_reloc_code_real_type other)
252b5132 3322{
47926f60 3323 if (other != NO_RELOC)
3956db08 3324 {
91d6fa6a 3325 reloc_howto_type *rel;
3956db08
JB
3326
3327 if (size == 8)
3328 switch (other)
3329 {
64e74474
AM
3330 case BFD_RELOC_X86_64_GOT32:
3331 return BFD_RELOC_X86_64_GOT64;
3332 break;
553d1284
L
3333 case BFD_RELOC_X86_64_GOTPLT64:
3334 return BFD_RELOC_X86_64_GOTPLT64;
3335 break;
64e74474
AM
3336 case BFD_RELOC_X86_64_PLTOFF64:
3337 return BFD_RELOC_X86_64_PLTOFF64;
3338 break;
3339 case BFD_RELOC_X86_64_GOTPC32:
3340 other = BFD_RELOC_X86_64_GOTPC64;
3341 break;
3342 case BFD_RELOC_X86_64_GOTPCREL:
3343 other = BFD_RELOC_X86_64_GOTPCREL64;
3344 break;
3345 case BFD_RELOC_X86_64_TPOFF32:
3346 other = BFD_RELOC_X86_64_TPOFF64;
3347 break;
3348 case BFD_RELOC_X86_64_DTPOFF32:
3349 other = BFD_RELOC_X86_64_DTPOFF64;
3350 break;
3351 default:
3352 break;
3956db08 3353 }
e05278af 3354
8ce3d284 3355#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
3356 if (other == BFD_RELOC_SIZE32)
3357 {
3358 if (size == 8)
1ab668bf 3359 other = BFD_RELOC_SIZE64;
8fd4256d 3360 if (pcrel)
1ab668bf
AM
3361 {
3362 as_bad (_("there are no pc-relative size relocations"));
3363 return NO_RELOC;
3364 }
8fd4256d 3365 }
8ce3d284 3366#endif
8fd4256d 3367
e05278af 3368 /* Sign-checking 4-byte relocations in 16-/32-bit code is pointless. */
f2d8a97c 3369 if (size == 4 && (flag_code != CODE_64BIT || disallow_64bit_reloc))
e05278af
JB
3370 sign = -1;
3371
91d6fa6a
NC
3372 rel = bfd_reloc_type_lookup (stdoutput, other);
3373 if (!rel)
3956db08 3374 as_bad (_("unknown relocation (%u)"), other);
91d6fa6a 3375 else if (size != bfd_get_reloc_size (rel))
3956db08 3376 as_bad (_("%u-byte relocation cannot be applied to %u-byte field"),
91d6fa6a 3377 bfd_get_reloc_size (rel),
3956db08 3378 size);
91d6fa6a 3379 else if (pcrel && !rel->pc_relative)
3956db08 3380 as_bad (_("non-pc-relative relocation for pc-relative field"));
91d6fa6a 3381 else if ((rel->complain_on_overflow == complain_overflow_signed
3956db08 3382 && !sign)
91d6fa6a 3383 || (rel->complain_on_overflow == complain_overflow_unsigned
64e74474 3384 && sign > 0))
3956db08
JB
3385 as_bad (_("relocated field and relocation type differ in signedness"));
3386 else
3387 return other;
3388 return NO_RELOC;
3389 }
252b5132
RH
3390
3391 if (pcrel)
3392 {
3e73aa7c 3393 if (!sign)
3956db08 3394 as_bad (_("there are no unsigned pc-relative relocations"));
252b5132
RH
3395 switch (size)
3396 {
3397 case 1: return BFD_RELOC_8_PCREL;
3398 case 2: return BFD_RELOC_16_PCREL;
d258b828 3399 case 4: return BFD_RELOC_32_PCREL;
d6ab8113 3400 case 8: return BFD_RELOC_64_PCREL;
252b5132 3401 }
3956db08 3402 as_bad (_("cannot do %u byte pc-relative relocation"), size);
252b5132
RH
3403 }
3404 else
3405 {
3956db08 3406 if (sign > 0)
e5cb08ac 3407 switch (size)
3e73aa7c
JH
3408 {
3409 case 4: return BFD_RELOC_X86_64_32S;
3410 }
3411 else
3412 switch (size)
3413 {
3414 case 1: return BFD_RELOC_8;
3415 case 2: return BFD_RELOC_16;
3416 case 4: return BFD_RELOC_32;
3417 case 8: return BFD_RELOC_64;
3418 }
3956db08
JB
3419 as_bad (_("cannot do %s %u byte relocation"),
3420 sign > 0 ? "signed" : "unsigned", size);
252b5132
RH
3421 }
3422
0cc9e1d3 3423 return NO_RELOC;
252b5132
RH
3424}
3425
47926f60
KH
3426/* Here we decide which fixups can be adjusted to make them relative to
3427 the beginning of the section instead of the symbol. Basically we need
3428 to make sure that the dynamic relocations are done correctly, so in
3429 some cases we force the original symbol to be used. */
3430
252b5132 3431int
e3bb37b5 3432tc_i386_fix_adjustable (fixS *fixP ATTRIBUTE_UNUSED)
252b5132 3433{
6d249963 3434#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 3435 if (!IS_ELF)
31312f95
AM
3436 return 1;
3437
a161fe53
AM
3438 /* Don't adjust pc-relative references to merge sections in 64-bit
3439 mode. */
3440 if (use_rela_relocations
3441 && (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0
3442 && fixP->fx_pcrel)
252b5132 3443 return 0;
31312f95 3444
8d01d9a9
AJ
3445 /* The x86_64 GOTPCREL are represented as 32bit PCrel relocations
3446 and changed later by validate_fix. */
3447 if (GOT_symbol && fixP->fx_subsy == GOT_symbol
3448 && fixP->fx_r_type == BFD_RELOC_32_PCREL)
3449 return 0;
3450
8fd4256d
L
3451 /* Adjust_reloc_syms doesn't know about the GOT. Need to keep symbol
3452 for size relocations. */
3453 if (fixP->fx_r_type == BFD_RELOC_SIZE32
3454 || fixP->fx_r_type == BFD_RELOC_SIZE64
3455 || fixP->fx_r_type == BFD_RELOC_386_GOTOFF
252b5132 3456 || fixP->fx_r_type == BFD_RELOC_386_GOT32
02a86693 3457 || fixP->fx_r_type == BFD_RELOC_386_GOT32X
13ae64f3
JJ
3458 || fixP->fx_r_type == BFD_RELOC_386_TLS_GD
3459 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDM
3460 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDO_32
3461 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE_32
37e55690
JJ
3462 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE
3463 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTIE
13ae64f3
JJ
3464 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE_32
3465 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE
67a4f2b7
AO
3466 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTDESC
3467 || fixP->fx_r_type == BFD_RELOC_386_TLS_DESC_CALL
3e73aa7c 3468 || fixP->fx_r_type == BFD_RELOC_X86_64_GOT32
80b3ee89 3469 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCREL
56ceb5b5
L
3470 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCRELX
3471 || fixP->fx_r_type == BFD_RELOC_X86_64_REX_GOTPCRELX
bffbf940
JJ
3472 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSGD
3473 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSLD
3474 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF32
d6ab8113 3475 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF64
bffbf940
JJ
3476 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF
3477 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF32
d6ab8113
JB
3478 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF64
3479 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTOFF64
67a4f2b7
AO
3480 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
3481 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSDESC_CALL
252b5132
RH
3482 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3483 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3484 return 0;
31312f95 3485#endif
252b5132
RH
3486 return 1;
3487}
252b5132 3488
a9aabc23
JB
3489static INLINE bool
3490want_disp32 (const insn_template *t)
3491{
3492 return flag_code != CODE_64BIT
3493 || i.prefix[ADDR_PREFIX]
3494 || (t->base_opcode == 0x8d
3495 && t->opcode_modifier.opcodespace == SPACE_BASE
fe134c65
JB
3496 && (!i.types[1].bitfield.qword
3497 || t->opcode_modifier.size == SIZE32));
a9aabc23
JB
3498}
3499
b4cac588 3500static int
e3bb37b5 3501intel_float_operand (const char *mnemonic)
252b5132 3502{
9306ca4a
JB
3503 /* Note that the value returned is meaningful only for opcodes with (memory)
3504 operands, hence the code here is free to improperly handle opcodes that
3505 have no operands (for better performance and smaller code). */
3506
3507 if (mnemonic[0] != 'f')
3508 return 0; /* non-math */
3509
3510 switch (mnemonic[1])
3511 {
3512 /* fclex, fdecstp, fdisi, femms, feni, fincstp, finit, fsetpm, and
3513 the fs segment override prefix not currently handled because no
3514 call path can make opcodes without operands get here */
3515 case 'i':
3516 return 2 /* integer op */;
3517 case 'l':
3518 if (mnemonic[2] == 'd' && (mnemonic[3] == 'c' || mnemonic[3] == 'e'))
3519 return 3; /* fldcw/fldenv */
3520 break;
3521 case 'n':
3522 if (mnemonic[2] != 'o' /* fnop */)
3523 return 3; /* non-waiting control op */
3524 break;
3525 case 'r':
3526 if (mnemonic[2] == 's')
3527 return 3; /* frstor/frstpm */
3528 break;
3529 case 's':
3530 if (mnemonic[2] == 'a')
3531 return 3; /* fsave */
3532 if (mnemonic[2] == 't')
3533 {
3534 switch (mnemonic[3])
3535 {
3536 case 'c': /* fstcw */
3537 case 'd': /* fstdw */
3538 case 'e': /* fstenv */
3539 case 's': /* fsts[gw] */
3540 return 3;
3541 }
3542 }
3543 break;
3544 case 'x':
3545 if (mnemonic[2] == 'r' || mnemonic[2] == 's')
3546 return 0; /* fxsave/fxrstor are not really math ops */
3547 break;
3548 }
252b5132 3549
9306ca4a 3550 return 1;
252b5132
RH
3551}
3552
9a182d04
JB
3553static INLINE void
3554install_template (const insn_template *t)
3555{
3556 unsigned int l;
3557
3558 i.tm = *t;
3559
3560 /* Note that for pseudo prefixes this produces a length of 1. But for them
3561 the length isn't interesting at all. */
3562 for (l = 1; l < 4; ++l)
3563 if (!(t->base_opcode >> (8 * l)))
3564 break;
3565
3566 i.opcode_length = l;
3567}
3568
c0f3af97
L
3569/* Build the VEX prefix. */
3570
3571static void
d3ce72d0 3572build_vex_prefix (const insn_template *t)
c0f3af97
L
3573{
3574 unsigned int register_specifier;
c0f3af97 3575 unsigned int vector_length;
03751133 3576 unsigned int w;
c0f3af97
L
3577
3578 /* Check register specifier. */
3579 if (i.vex.register_specifier)
43234a1e
L
3580 {
3581 register_specifier =
3582 ~register_number (i.vex.register_specifier) & 0xf;
3583 gas_assert ((i.vex.register_specifier->reg_flags & RegVRex) == 0);
3584 }
c0f3af97
L
3585 else
3586 register_specifier = 0xf;
3587
79f0fa25
L
3588 /* Use 2-byte VEX prefix by swapping destination and source operand
3589 if there are more than 1 register operand. */
3590 if (i.reg_operands > 1
3591 && i.vec_encoding != vex_encoding_vex3
86fa6981 3592 && i.dir_encoding == dir_encoding_default
fa99fab2 3593 && i.operands == i.reg_operands
dbbc8b7e 3594 && operand_type_equal (&i.types[0], &i.types[i.operands - 1])
441f6aca 3595 && i.tm.opcode_modifier.opcodespace == SPACE_0F
dbbc8b7e 3596 && (i.tm.opcode_modifier.load || i.tm.opcode_modifier.d)
fa99fab2
L
3597 && i.rex == REX_B)
3598 {
3599 unsigned int xchg = i.operands - 1;
3600 union i386_op temp_op;
3601 i386_operand_type temp_type;
3602
3603 temp_type = i.types[xchg];
3604 i.types[xchg] = i.types[0];
3605 i.types[0] = temp_type;
3606 temp_op = i.op[xchg];
3607 i.op[xchg] = i.op[0];
3608 i.op[0] = temp_op;
3609
9c2799c2 3610 gas_assert (i.rm.mode == 3);
fa99fab2
L
3611
3612 i.rex = REX_R;
3613 xchg = i.rm.regmem;
3614 i.rm.regmem = i.rm.reg;
3615 i.rm.reg = xchg;
3616
dbbc8b7e
JB
3617 if (i.tm.opcode_modifier.d)
3618 i.tm.base_opcode ^= (i.tm.base_opcode & 0xee) != 0x6e
2c735193 3619 ? Opcode_ExtD : Opcode_SIMD_IntD;
dbbc8b7e 3620 else /* Use the next insn. */
9a182d04 3621 install_template (&t[1]);
fa99fab2
L
3622 }
3623
79dec6b7
JB
3624 /* Use 2-byte VEX prefix by swapping commutative source operands if there
3625 are no memory operands and at least 3 register ones. */
3626 if (i.reg_operands >= 3
3627 && i.vec_encoding != vex_encoding_vex3
3628 && i.reg_operands == i.operands - i.imm_operands
3629 && i.tm.opcode_modifier.vex
3630 && i.tm.opcode_modifier.commutative
3631 && (i.tm.opcode_modifier.sse2avx || optimize > 1)
3632 && i.rex == REX_B
3633 && i.vex.register_specifier
3634 && !(i.vex.register_specifier->reg_flags & RegRex))
3635 {
3636 unsigned int xchg = i.operands - i.reg_operands;
3637 union i386_op temp_op;
3638 i386_operand_type temp_type;
3639
441f6aca 3640 gas_assert (i.tm.opcode_modifier.opcodespace == SPACE_0F);
79dec6b7
JB
3641 gas_assert (!i.tm.opcode_modifier.sae);
3642 gas_assert (operand_type_equal (&i.types[i.operands - 2],
3643 &i.types[i.operands - 3]));
3644 gas_assert (i.rm.mode == 3);
3645
3646 temp_type = i.types[xchg];
3647 i.types[xchg] = i.types[xchg + 1];
3648 i.types[xchg + 1] = temp_type;
3649 temp_op = i.op[xchg];
3650 i.op[xchg] = i.op[xchg + 1];
3651 i.op[xchg + 1] = temp_op;
3652
3653 i.rex = 0;
3654 xchg = i.rm.regmem | 8;
3655 i.rm.regmem = ~register_specifier & 0xf;
3656 gas_assert (!(i.rm.regmem & 8));
3657 i.vex.register_specifier += xchg - i.rm.regmem;
3658 register_specifier = ~xchg & 0xf;
3659 }
3660
539f890d
L
3661 if (i.tm.opcode_modifier.vex == VEXScalar)
3662 vector_length = avxscalar;
10c17abd
JB
3663 else if (i.tm.opcode_modifier.vex == VEX256)
3664 vector_length = 1;
539f890d 3665 else
10c17abd 3666 {
56522fc5 3667 unsigned int op;
10c17abd 3668
c7213af9
L
3669 /* Determine vector length from the last multi-length vector
3670 operand. */
10c17abd 3671 vector_length = 0;
56522fc5 3672 for (op = t->operands; op--;)
10c17abd
JB
3673 if (t->operand_types[op].bitfield.xmmword
3674 && t->operand_types[op].bitfield.ymmword
3675 && i.types[op].bitfield.ymmword)
3676 {
3677 vector_length = 1;
3678 break;
3679 }
3680 }
c0f3af97 3681
03751133
L
3682 /* Check the REX.W bit and VEXW. */
3683 if (i.tm.opcode_modifier.vexw == VEXWIG)
3684 w = (vexwig == vexw1 || (i.rex & REX_W)) ? 1 : 0;
3685 else if (i.tm.opcode_modifier.vexw)
3686 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3687 else
931d03b7 3688 w = (flag_code == CODE_64BIT ? i.rex & REX_W : vexwig == vexw1) ? 1 : 0;
03751133 3689
c0f3af97 3690 /* Use 2-byte VEX prefix if possible. */
03751133
L
3691 if (w == 0
3692 && i.vec_encoding != vex_encoding_vex3
441f6aca 3693 && i.tm.opcode_modifier.opcodespace == SPACE_0F
c0f3af97
L
3694 && (i.rex & (REX_W | REX_X | REX_B)) == 0)
3695 {
3696 /* 2-byte VEX prefix. */
3697 unsigned int r;
3698
3699 i.vex.length = 2;
3700 i.vex.bytes[0] = 0xc5;
3701
3702 /* Check the REX.R bit. */
3703 r = (i.rex & REX_R) ? 0 : 1;
3704 i.vex.bytes[1] = (r << 7
3705 | register_specifier << 3
3706 | vector_length << 2
35648716 3707 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3708 }
3709 else
3710 {
3711 /* 3-byte VEX prefix. */
f88c9eb0 3712 i.vex.length = 3;
f88c9eb0 3713
441f6aca 3714 switch (i.tm.opcode_modifier.opcodespace)
5dd85c99 3715 {
441f6aca
JB
3716 case SPACE_0F:
3717 case SPACE_0F38:
3718 case SPACE_0F3A:
80de6e00 3719 i.vex.bytes[0] = 0xc4;
7f399153 3720 break;
441f6aca
JB
3721 case SPACE_XOP08:
3722 case SPACE_XOP09:
3723 case SPACE_XOP0A:
f88c9eb0 3724 i.vex.bytes[0] = 0x8f;
7f399153
L
3725 break;
3726 default:
3727 abort ();
f88c9eb0 3728 }
c0f3af97 3729
c0f3af97
L
3730 /* The high 3 bits of the second VEX byte are 1's compliment
3731 of RXB bits from REX. */
441f6aca 3732 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | i.tm.opcode_modifier.opcodespace;
c0f3af97 3733
c0f3af97
L
3734 i.vex.bytes[2] = (w << 7
3735 | register_specifier << 3
3736 | vector_length << 2
35648716 3737 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3738 }
3739}
3740
5b7c81bd 3741static INLINE bool
e771e7c9
JB
3742is_evex_encoding (const insn_template *t)
3743{
7091c612 3744 return t->opcode_modifier.evex || t->opcode_modifier.disp8memshift
e771e7c9 3745 || t->opcode_modifier.broadcast || t->opcode_modifier.masking
a80195f1 3746 || t->opcode_modifier.sae;
e771e7c9
JB
3747}
3748
5b7c81bd 3749static INLINE bool
7a8655d2
JB
3750is_any_vex_encoding (const insn_template *t)
3751{
7b47a312 3752 return t->opcode_modifier.vex || is_evex_encoding (t);
7a8655d2
JB
3753}
3754
a5748e0d
JB
3755static unsigned int
3756get_broadcast_bytes (const insn_template *t, bool diag)
3757{
3758 unsigned int op, bytes;
3759 const i386_operand_type *types;
3760
3761 if (i.broadcast.type)
3762 return i.broadcast.bytes = ((1 << (t->opcode_modifier.broadcast - 1))
3763 * i.broadcast.type);
3764
3765 gas_assert (intel_syntax);
3766
3767 for (op = 0; op < t->operands; ++op)
3768 if (t->operand_types[op].bitfield.baseindex)
3769 break;
3770
3771 gas_assert (op < t->operands);
3772
3773 if (t->opcode_modifier.evex
3774 && t->opcode_modifier.evex != EVEXDYN)
3775 switch (i.broadcast.bytes)
3776 {
3777 case 1:
3778 if (t->operand_types[op].bitfield.word)
3779 return 2;
3780 /* Fall through. */
3781 case 2:
3782 if (t->operand_types[op].bitfield.dword)
3783 return 4;
3784 /* Fall through. */
3785 case 4:
3786 if (t->operand_types[op].bitfield.qword)
3787 return 8;
3788 /* Fall through. */
3789 case 8:
3790 if (t->operand_types[op].bitfield.xmmword)
3791 return 16;
3792 if (t->operand_types[op].bitfield.ymmword)
3793 return 32;
3794 if (t->operand_types[op].bitfield.zmmword)
3795 return 64;
3796 /* Fall through. */
3797 default:
3798 abort ();
3799 }
3800
3801 gas_assert (op + 1 < t->operands);
3802
3803 if (t->operand_types[op + 1].bitfield.xmmword
3804 + t->operand_types[op + 1].bitfield.ymmword
3805 + t->operand_types[op + 1].bitfield.zmmword > 1)
3806 {
3807 types = &i.types[op + 1];
3808 diag = false;
3809 }
3810 else /* Ambiguous - guess with a preference to non-AVX512VL forms. */
3811 types = &t->operand_types[op];
3812
3813 if (types->bitfield.zmmword)
3814 bytes = 64;
3815 else if (types->bitfield.ymmword)
3816 bytes = 32;
3817 else
3818 bytes = 16;
3819
3820 if (diag)
3821 as_warn (_("ambiguous broadcast for `%s', using %u-bit form"),
3822 t->name, bytes * 8);
3823
3824 return bytes;
3825}
3826
43234a1e
L
3827/* Build the EVEX prefix. */
3828
3829static void
3830build_evex_prefix (void)
3831{
35648716 3832 unsigned int register_specifier, w;
43234a1e
L
3833 rex_byte vrex_used = 0;
3834
3835 /* Check register specifier. */
3836 if (i.vex.register_specifier)
3837 {
3838 gas_assert ((i.vrex & REX_X) == 0);
3839
3840 register_specifier = i.vex.register_specifier->reg_num;
3841 if ((i.vex.register_specifier->reg_flags & RegRex))
3842 register_specifier += 8;
3843 /* The upper 16 registers are encoded in the fourth byte of the
3844 EVEX prefix. */
3845 if (!(i.vex.register_specifier->reg_flags & RegVRex))
3846 i.vex.bytes[3] = 0x8;
3847 register_specifier = ~register_specifier & 0xf;
3848 }
3849 else
3850 {
3851 register_specifier = 0xf;
3852
3853 /* Encode upper 16 vector index register in the fourth byte of
3854 the EVEX prefix. */
3855 if (!(i.vrex & REX_X))
3856 i.vex.bytes[3] = 0x8;
3857 else
3858 vrex_used |= REX_X;
3859 }
3860
43234a1e
L
3861 /* 4 byte EVEX prefix. */
3862 i.vex.length = 4;
3863 i.vex.bytes[0] = 0x62;
3864
43234a1e
L
3865 /* The high 3 bits of the second EVEX byte are 1's compliment of RXB
3866 bits from REX. */
441f6aca 3867 gas_assert (i.tm.opcode_modifier.opcodespace >= SPACE_0F);
0cc78721 3868 gas_assert (i.tm.opcode_modifier.opcodespace <= SPACE_EVEXMAP6);
441f6aca 3869 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | i.tm.opcode_modifier.opcodespace;
43234a1e
L
3870
3871 /* The fifth bit of the second EVEX byte is 1's compliment of the
3872 REX_R bit in VREX. */
3873 if (!(i.vrex & REX_R))
3874 i.vex.bytes[1] |= 0x10;
3875 else
3876 vrex_used |= REX_R;
3877
3878 if ((i.reg_operands + i.imm_operands) == i.operands)
3879 {
3880 /* When all operands are registers, the REX_X bit in REX is not
3881 used. We reuse it to encode the upper 16 registers, which is
3882 indicated by the REX_B bit in VREX. The REX_X bit is encoded
3883 as 1's compliment. */
3884 if ((i.vrex & REX_B))
3885 {
3886 vrex_used |= REX_B;
3887 i.vex.bytes[1] &= ~0x40;
3888 }
3889 }
3890
3891 /* EVEX instructions shouldn't need the REX prefix. */
3892 i.vrex &= ~vrex_used;
3893 gas_assert (i.vrex == 0);
3894
6865c043
L
3895 /* Check the REX.W bit and VEXW. */
3896 if (i.tm.opcode_modifier.vexw == VEXWIG)
3897 w = (evexwig == evexw1 || (i.rex & REX_W)) ? 1 : 0;
3898 else if (i.tm.opcode_modifier.vexw)
3899 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3900 else
931d03b7 3901 w = (flag_code == CODE_64BIT ? i.rex & REX_W : evexwig == evexw1) ? 1 : 0;
43234a1e 3902
43234a1e 3903 /* The third byte of the EVEX prefix. */
35648716
JB
3904 i.vex.bytes[2] = ((w << 7)
3905 | (register_specifier << 3)
3906 | 4 /* Encode the U bit. */
3907 | i.tm.opcode_modifier.opcodeprefix);
43234a1e
L
3908
3909 /* The fourth byte of the EVEX prefix. */
3910 /* The zeroing-masking bit. */
6225c532 3911 if (i.mask.reg && i.mask.zeroing)
43234a1e
L
3912 i.vex.bytes[3] |= 0x80;
3913
3914 /* Don't always set the broadcast bit if there is no RC. */
ca5312a2 3915 if (i.rounding.type == rc_none)
43234a1e
L
3916 {
3917 /* Encode the vector length. */
3918 unsigned int vec_length;
3919
e771e7c9
JB
3920 if (!i.tm.opcode_modifier.evex
3921 || i.tm.opcode_modifier.evex == EVEXDYN)
3922 {
56522fc5 3923 unsigned int op;
e771e7c9 3924
c7213af9
L
3925 /* Determine vector length from the last multi-length vector
3926 operand. */
56522fc5 3927 for (op = i.operands; op--;)
e771e7c9
JB
3928 if (i.tm.operand_types[op].bitfield.xmmword
3929 + i.tm.operand_types[op].bitfield.ymmword
3930 + i.tm.operand_types[op].bitfield.zmmword > 1)
3931 {
3932 if (i.types[op].bitfield.zmmword)
c7213af9
L
3933 {
3934 i.tm.opcode_modifier.evex = EVEX512;
3935 break;
3936 }
e771e7c9 3937 else if (i.types[op].bitfield.ymmword)
c7213af9
L
3938 {
3939 i.tm.opcode_modifier.evex = EVEX256;
3940 break;
3941 }
e771e7c9 3942 else if (i.types[op].bitfield.xmmword)
c7213af9
L
3943 {
3944 i.tm.opcode_modifier.evex = EVEX128;
3945 break;
3946 }
a5748e0d 3947 else if (i.broadcast.bytes && op == i.broadcast.operand)
625cbd7a 3948 {
a5748e0d 3949 switch (get_broadcast_bytes (&i.tm, true))
625cbd7a
JB
3950 {
3951 case 64:
3952 i.tm.opcode_modifier.evex = EVEX512;
3953 break;
3954 case 32:
3955 i.tm.opcode_modifier.evex = EVEX256;
3956 break;
3957 case 16:
3958 i.tm.opcode_modifier.evex = EVEX128;
3959 break;
3960 default:
c7213af9 3961 abort ();
625cbd7a 3962 }
c7213af9 3963 break;
625cbd7a 3964 }
e771e7c9 3965 }
c7213af9 3966
56522fc5 3967 if (op >= MAX_OPERANDS)
c7213af9 3968 abort ();
e771e7c9
JB
3969 }
3970
43234a1e
L
3971 switch (i.tm.opcode_modifier.evex)
3972 {
3973 case EVEXLIG: /* LL' is ignored */
3974 vec_length = evexlig << 5;
3975 break;
3976 case EVEX128:
3977 vec_length = 0 << 5;
3978 break;
3979 case EVEX256:
3980 vec_length = 1 << 5;
3981 break;
3982 case EVEX512:
3983 vec_length = 2 << 5;
3984 break;
3985 default:
3986 abort ();
3987 break;
3988 }
3989 i.vex.bytes[3] |= vec_length;
3990 /* Encode the broadcast bit. */
a5748e0d 3991 if (i.broadcast.bytes)
43234a1e
L
3992 i.vex.bytes[3] |= 0x10;
3993 }
ca5312a2
JB
3994 else if (i.rounding.type != saeonly)
3995 i.vex.bytes[3] |= 0x10 | (i.rounding.type << 5);
43234a1e 3996 else
ca5312a2 3997 i.vex.bytes[3] |= 0x10 | (evexrcig << 5);
43234a1e 3998
6225c532
JB
3999 if (i.mask.reg)
4000 i.vex.bytes[3] |= i.mask.reg->reg_num;
43234a1e
L
4001}
4002
65da13b5
L
4003static void
4004process_immext (void)
4005{
4006 expressionS *exp;
4007
c0f3af97 4008 /* These AMD 3DNow! and SSE2 instructions have an opcode suffix
65da13b5
L
4009 which is coded in the same place as an 8-bit immediate field
4010 would be. Here we fake an 8-bit immediate operand from the
4011 opcode suffix stored in tm.extension_opcode.
4012
c1e679ec 4013 AVX instructions also use this encoding, for some of
c0f3af97 4014 3 argument instructions. */
65da13b5 4015
43234a1e 4016 gas_assert (i.imm_operands <= 1
7ab9ffdd 4017 && (i.operands <= 2
7a8655d2 4018 || (is_any_vex_encoding (&i.tm)
7ab9ffdd 4019 && i.operands <= 4)));
65da13b5
L
4020
4021 exp = &im_expressions[i.imm_operands++];
4022 i.op[i.operands].imms = exp;
be1643ff 4023 i.types[i.operands].bitfield.imm8 = 1;
65da13b5
L
4024 i.operands++;
4025 exp->X_op = O_constant;
4026 exp->X_add_number = i.tm.extension_opcode;
4027 i.tm.extension_opcode = None;
4028}
4029
42164a71
L
4030
4031static int
4032check_hle (void)
4033{
742732c7 4034 switch (i.tm.opcode_modifier.prefixok)
42164a71
L
4035 {
4036 default:
4037 abort ();
742732c7
JB
4038 case PrefixLock:
4039 case PrefixNone:
4040 case PrefixNoTrack:
4041 case PrefixRep:
165de32a
L
4042 as_bad (_("invalid instruction `%s' after `%s'"),
4043 i.tm.name, i.hle_prefix);
42164a71 4044 return 0;
742732c7 4045 case PrefixHLELock:
42164a71
L
4046 if (i.prefix[LOCK_PREFIX])
4047 return 1;
165de32a 4048 as_bad (_("missing `lock' with `%s'"), i.hle_prefix);
42164a71 4049 return 0;
742732c7 4050 case PrefixHLEAny:
42164a71 4051 return 1;
742732c7 4052 case PrefixHLERelease:
42164a71
L
4053 if (i.prefix[HLE_PREFIX] != XRELEASE_PREFIX_OPCODE)
4054 {
4055 as_bad (_("instruction `%s' after `xacquire' not allowed"),
4056 i.tm.name);
4057 return 0;
4058 }
8dc0818e 4059 if (i.mem_operands == 0 || !(i.flags[i.operands - 1] & Operand_Mem))
42164a71
L
4060 {
4061 as_bad (_("memory destination needed for instruction `%s'"
4062 " after `xrelease'"), i.tm.name);
4063 return 0;
4064 }
4065 return 1;
4066 }
4067}
4068
c8480b58
L
4069/* Encode aligned vector move as unaligned vector move. */
4070
4071static void
4072encode_with_unaligned_vector_move (void)
4073{
4074 switch (i.tm.base_opcode)
4075 {
b3a9fe6f
L
4076 case 0x28: /* Load instructions. */
4077 case 0x29: /* Store instructions. */
c8480b58
L
4078 /* movaps/movapd/vmovaps/vmovapd. */
4079 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4080 && i.tm.opcode_modifier.opcodeprefix <= PREFIX_0X66)
b3a9fe6f 4081 i.tm.base_opcode = 0x10 | (i.tm.base_opcode & 1);
c8480b58 4082 break;
b3a9fe6f
L
4083 case 0x6f: /* Load instructions. */
4084 case 0x7f: /* Store instructions. */
c8480b58
L
4085 /* movdqa/vmovdqa/vmovdqa64/vmovdqa32. */
4086 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4087 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0X66)
4088 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
4089 break;
4090 default:
4091 break;
4092 }
4093}
4094
b6f8c7c4
L
4095/* Try the shortest encoding by shortening operand size. */
4096
4097static void
4098optimize_encoding (void)
4099{
a0a1771e 4100 unsigned int j;
b6f8c7c4 4101
fe134c65
JB
4102 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
4103 && i.tm.base_opcode == 0x8d)
4104 {
4105 /* Optimize: -O:
4106 lea symbol, %rN -> mov $symbol, %rN
4107 lea (%rM), %rN -> mov %rM, %rN
4108 lea (,%rM,1), %rN -> mov %rM, %rN
4109
4110 and in 32-bit mode for 16-bit addressing
4111
4112 lea (%rM), %rN -> movzx %rM, %rN
4113
4114 and in 64-bit mode zap 32-bit addressing in favor of using a
4115 32-bit (or less) destination.
4116 */
4117 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
4118 {
4119 if (!i.op[1].regs->reg_type.bitfield.word)
4120 i.tm.opcode_modifier.size = SIZE32;
4121 i.prefix[ADDR_PREFIX] = 0;
4122 }
4123
4124 if (!i.index_reg && !i.base_reg)
4125 {
4126 /* Handle:
4127 lea symbol, %rN -> mov $symbol, %rN
4128 */
4129 if (flag_code == CODE_64BIT)
4130 {
4131 /* Don't transform a relocation to a 16-bit one. */
4132 if (i.op[0].disps
4133 && i.op[0].disps->X_op != O_constant
4134 && i.op[1].regs->reg_type.bitfield.word)
4135 return;
4136
4137 if (!i.op[1].regs->reg_type.bitfield.qword
4138 || i.tm.opcode_modifier.size == SIZE32)
4139 {
4140 i.tm.base_opcode = 0xb8;
4141 i.tm.opcode_modifier.modrm = 0;
4142 if (!i.op[1].regs->reg_type.bitfield.word)
4143 i.types[0].bitfield.imm32 = 1;
4144 else
4145 {
4146 i.tm.opcode_modifier.size = SIZE16;
4147 i.types[0].bitfield.imm16 = 1;
4148 }
4149 }
4150 else
4151 {
4152 /* Subject to further optimization below. */
4153 i.tm.base_opcode = 0xc7;
4154 i.tm.extension_opcode = 0;
4155 i.types[0].bitfield.imm32s = 1;
4156 i.types[0].bitfield.baseindex = 0;
4157 }
4158 }
4159 /* Outside of 64-bit mode address and operand sizes have to match if
4160 a relocation is involved, as otherwise we wouldn't (currently) or
4161 even couldn't express the relocation correctly. */
4162 else if (i.op[0].disps
4163 && i.op[0].disps->X_op != O_constant
4164 && ((!i.prefix[ADDR_PREFIX])
4165 != (flag_code == CODE_32BIT
4166 ? i.op[1].regs->reg_type.bitfield.dword
4167 : i.op[1].regs->reg_type.bitfield.word)))
4168 return;
7772f168
JB
4169 /* In 16-bit mode converting LEA with 16-bit addressing and a 32-bit
4170 destination is going to grow encoding size. */
4171 else if (flag_code == CODE_16BIT
4172 && (optimize <= 1 || optimize_for_space)
4173 && !i.prefix[ADDR_PREFIX]
4174 && i.op[1].regs->reg_type.bitfield.dword)
4175 return;
fe134c65
JB
4176 else
4177 {
4178 i.tm.base_opcode = 0xb8;
4179 i.tm.opcode_modifier.modrm = 0;
4180 if (i.op[1].regs->reg_type.bitfield.dword)
4181 i.types[0].bitfield.imm32 = 1;
4182 else
4183 i.types[0].bitfield.imm16 = 1;
4184
4185 if (i.op[0].disps
4186 && i.op[0].disps->X_op == O_constant
4187 && i.op[1].regs->reg_type.bitfield.dword
60cfa10c
L
4188 /* NB: Add () to !i.prefix[ADDR_PREFIX] to silence
4189 GCC 5. */
4190 && (!i.prefix[ADDR_PREFIX]) != (flag_code == CODE_32BIT))
fe134c65
JB
4191 i.op[0].disps->X_add_number &= 0xffff;
4192 }
4193
4194 i.tm.operand_types[0] = i.types[0];
4195 i.imm_operands = 1;
4196 if (!i.op[0].imms)
4197 {
4198 i.op[0].imms = &im_expressions[0];
4199 i.op[0].imms->X_op = O_absent;
4200 }
4201 }
4202 else if (i.op[0].disps
4203 && (i.op[0].disps->X_op != O_constant
4204 || i.op[0].disps->X_add_number))
4205 return;
4206 else
4207 {
4208 /* Handle:
4209 lea (%rM), %rN -> mov %rM, %rN
4210 lea (,%rM,1), %rN -> mov %rM, %rN
4211 lea (%rM), %rN -> movzx %rM, %rN
4212 */
4213 const reg_entry *addr_reg;
4214
4215 if (!i.index_reg && i.base_reg->reg_num != RegIP)
4216 addr_reg = i.base_reg;
4217 else if (!i.base_reg
4218 && i.index_reg->reg_num != RegIZ
4219 && !i.log2_scale_factor)
4220 addr_reg = i.index_reg;
4221 else
4222 return;
4223
4224 if (addr_reg->reg_type.bitfield.word
4225 && i.op[1].regs->reg_type.bitfield.dword)
4226 {
4227 if (flag_code != CODE_32BIT)
4228 return;
4229 i.tm.opcode_modifier.opcodespace = SPACE_0F;
4230 i.tm.base_opcode = 0xb7;
4231 }
4232 else
4233 i.tm.base_opcode = 0x8b;
4234
4235 if (addr_reg->reg_type.bitfield.dword
4236 && i.op[1].regs->reg_type.bitfield.qword)
4237 i.tm.opcode_modifier.size = SIZE32;
4238
4239 i.op[0].regs = addr_reg;
4240 i.reg_operands = 2;
4241 }
4242
4243 i.mem_operands = 0;
4244 i.disp_operands = 0;
4245 i.prefix[ADDR_PREFIX] = 0;
4246 i.prefix[SEG_PREFIX] = 0;
4247 i.seg[0] = NULL;
4248 }
4249
b6f8c7c4 4250 if (optimize_for_space
389d00a5 4251 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
b6f8c7c4
L
4252 && i.reg_operands == 1
4253 && i.imm_operands == 1
4254 && !i.types[1].bitfield.byte
4255 && i.op[0].imms->X_op == O_constant
4256 && fits_in_imm7 (i.op[0].imms->X_add_number)
72aea328 4257 && (i.tm.base_opcode == 0xa8
b6f8c7c4
L
4258 || (i.tm.base_opcode == 0xf6
4259 && i.tm.extension_opcode == 0x0)))
4260 {
4261 /* Optimize: -Os:
4262 test $imm7, %r64/%r32/%r16 -> test $imm7, %r8
4263 */
4264 unsigned int base_regnum = i.op[1].regs->reg_num;
4265 if (flag_code == CODE_64BIT || base_regnum < 4)
4266 {
4267 i.types[1].bitfield.byte = 1;
4268 /* Ignore the suffix. */
4269 i.suffix = 0;
7697afb6
JB
4270 /* Convert to byte registers. */
4271 if (i.types[1].bitfield.word)
4272 j = 16;
4273 else if (i.types[1].bitfield.dword)
4274 j = 32;
4275 else
4276 j = 48;
4277 if (!(i.op[1].regs->reg_flags & RegRex) && base_regnum < 4)
4278 j += 8;
4279 i.op[1].regs -= j;
b6f8c7c4
L
4280 }
4281 }
4282 else if (flag_code == CODE_64BIT
389d00a5 4283 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
d3d50934
L
4284 && ((i.types[1].bitfield.qword
4285 && i.reg_operands == 1
b6f8c7c4
L
4286 && i.imm_operands == 1
4287 && i.op[0].imms->X_op == O_constant
507916b8 4288 && ((i.tm.base_opcode == 0xb8
b6f8c7c4
L
4289 && i.tm.extension_opcode == None
4290 && fits_in_unsigned_long (i.op[0].imms->X_add_number))
4291 || (fits_in_imm31 (i.op[0].imms->X_add_number)
72aea328
JB
4292 && ((i.tm.base_opcode == 0x24
4293 || i.tm.base_opcode == 0xa8)
b6f8c7c4
L
4294 || (i.tm.base_opcode == 0x80
4295 && i.tm.extension_opcode == 0x4)
4296 || ((i.tm.base_opcode == 0xf6
507916b8 4297 || (i.tm.base_opcode | 1) == 0xc7)
b8364fa7
JB
4298 && i.tm.extension_opcode == 0x0)))
4299 || (fits_in_imm7 (i.op[0].imms->X_add_number)
4300 && i.tm.base_opcode == 0x83
4301 && i.tm.extension_opcode == 0x4)))
d3d50934
L
4302 || (i.types[0].bitfield.qword
4303 && ((i.reg_operands == 2
4304 && i.op[0].regs == i.op[1].regs
72aea328
JB
4305 && (i.tm.base_opcode == 0x30
4306 || i.tm.base_opcode == 0x28))
d3d50934
L
4307 || (i.reg_operands == 1
4308 && i.operands == 1
72aea328 4309 && i.tm.base_opcode == 0x30)))))
b6f8c7c4
L
4310 {
4311 /* Optimize: -O:
4312 andq $imm31, %r64 -> andl $imm31, %r32
b8364fa7 4313 andq $imm7, %r64 -> andl $imm7, %r32
b6f8c7c4
L
4314 testq $imm31, %r64 -> testl $imm31, %r32
4315 xorq %r64, %r64 -> xorl %r32, %r32
4316 subq %r64, %r64 -> subl %r32, %r32
4317 movq $imm31, %r64 -> movl $imm31, %r32
4318 movq $imm32, %r64 -> movl $imm32, %r32
4319 */
4320 i.tm.opcode_modifier.norex64 = 1;
507916b8 4321 if (i.tm.base_opcode == 0xb8 || (i.tm.base_opcode | 1) == 0xc7)
b6f8c7c4
L
4322 {
4323 /* Handle
4324 movq $imm31, %r64 -> movl $imm31, %r32
4325 movq $imm32, %r64 -> movl $imm32, %r32
4326 */
4327 i.tm.operand_types[0].bitfield.imm32 = 1;
4328 i.tm.operand_types[0].bitfield.imm32s = 0;
4329 i.tm.operand_types[0].bitfield.imm64 = 0;
4330 i.types[0].bitfield.imm32 = 1;
4331 i.types[0].bitfield.imm32s = 0;
4332 i.types[0].bitfield.imm64 = 0;
4333 i.types[1].bitfield.dword = 1;
4334 i.types[1].bitfield.qword = 0;
507916b8 4335 if ((i.tm.base_opcode | 1) == 0xc7)
b6f8c7c4
L
4336 {
4337 /* Handle
4338 movq $imm31, %r64 -> movl $imm31, %r32
4339 */
507916b8 4340 i.tm.base_opcode = 0xb8;
b6f8c7c4 4341 i.tm.extension_opcode = None;
507916b8 4342 i.tm.opcode_modifier.w = 0;
b6f8c7c4
L
4343 i.tm.opcode_modifier.modrm = 0;
4344 }
4345 }
4346 }
5641ec01
JB
4347 else if (optimize > 1
4348 && !optimize_for_space
389d00a5 4349 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
5641ec01
JB
4350 && i.reg_operands == 2
4351 && i.op[0].regs == i.op[1].regs
4352 && ((i.tm.base_opcode & ~(Opcode_D | 1)) == 0x8
4353 || (i.tm.base_opcode & ~(Opcode_D | 1)) == 0x20)
4354 && (flag_code != CODE_64BIT || !i.types[0].bitfield.dword))
4355 {
4356 /* Optimize: -O2:
4357 andb %rN, %rN -> testb %rN, %rN
4358 andw %rN, %rN -> testw %rN, %rN
4359 andq %rN, %rN -> testq %rN, %rN
4360 orb %rN, %rN -> testb %rN, %rN
4361 orw %rN, %rN -> testw %rN, %rN
4362 orq %rN, %rN -> testq %rN, %rN
4363
4364 and outside of 64-bit mode
4365
4366 andl %rN, %rN -> testl %rN, %rN
4367 orl %rN, %rN -> testl %rN, %rN
4368 */
4369 i.tm.base_opcode = 0x84 | (i.tm.base_opcode & 1);
4370 }
99112332 4371 else if (i.reg_operands == 3
b6f8c7c4
L
4372 && i.op[0].regs == i.op[1].regs
4373 && !i.types[2].bitfield.xmmword
4374 && (i.tm.opcode_modifier.vex
6225c532 4375 || ((!i.mask.reg || i.mask.zeroing)
e771e7c9 4376 && is_evex_encoding (&i.tm)
80c34c38 4377 && (i.vec_encoding != vex_encoding_evex
dd22218c 4378 || cpu_arch_isa_flags.bitfield.cpuavx512vl
80c34c38 4379 || i.tm.cpu_flags.bitfield.cpuavx512vl
7091c612 4380 || (i.tm.operand_types[2].bitfield.zmmword
dd22218c 4381 && i.types[2].bitfield.ymmword))))
5844ccaa
JB
4382 && i.tm.opcode_modifier.opcodespace == SPACE_0F
4383 && ((i.tm.base_opcode | 2) == 0x57
4384 || i.tm.base_opcode == 0xdf
4385 || i.tm.base_opcode == 0xef
4386 || (i.tm.base_opcode | 3) == 0xfb
4387 || i.tm.base_opcode == 0x42
4388 || i.tm.base_opcode == 0x47))
b6f8c7c4 4389 {
99112332 4390 /* Optimize: -O1:
8305403a
L
4391 VOP, one of vandnps, vandnpd, vxorps, vxorpd, vpsubb, vpsubd,
4392 vpsubq and vpsubw:
b6f8c7c4
L
4393 EVEX VOP %zmmM, %zmmM, %zmmN
4394 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4395 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4396 EVEX VOP %ymmM, %ymmM, %ymmN
4397 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4398 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4399 VEX VOP %ymmM, %ymmM, %ymmN
4400 -> VEX VOP %xmmM, %xmmM, %xmmN
4401 VOP, one of vpandn and vpxor:
4402 VEX VOP %ymmM, %ymmM, %ymmN
4403 -> VEX VOP %xmmM, %xmmM, %xmmN
4404 VOP, one of vpandnd and vpandnq:
4405 EVEX VOP %zmmM, %zmmM, %zmmN
4406 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4407 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4408 EVEX VOP %ymmM, %ymmM, %ymmN
4409 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4410 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4411 VOP, one of vpxord and vpxorq:
4412 EVEX VOP %zmmM, %zmmM, %zmmN
4413 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4414 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4415 EVEX VOP %ymmM, %ymmM, %ymmN
4416 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4417 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
1424ad86
JB
4418 VOP, one of kxord and kxorq:
4419 VEX VOP %kM, %kM, %kN
4420 -> VEX kxorw %kM, %kM, %kN
4421 VOP, one of kandnd and kandnq:
4422 VEX VOP %kM, %kM, %kN
4423 -> VEX kandnw %kM, %kM, %kN
b6f8c7c4 4424 */
e771e7c9 4425 if (is_evex_encoding (&i.tm))
b6f8c7c4 4426 {
7b1d7ca1 4427 if (i.vec_encoding != vex_encoding_evex)
b6f8c7c4
L
4428 {
4429 i.tm.opcode_modifier.vex = VEX128;
4430 i.tm.opcode_modifier.vexw = VEXW0;
4431 i.tm.opcode_modifier.evex = 0;
4432 }
7b1d7ca1 4433 else if (optimize > 1)
dd22218c
L
4434 i.tm.opcode_modifier.evex = EVEX128;
4435 else
4436 return;
b6f8c7c4 4437 }
f74a6307 4438 else if (i.tm.operand_types[0].bitfield.class == RegMask)
1424ad86 4439 {
35648716 4440 i.tm.opcode_modifier.opcodeprefix = PREFIX_NONE;
1424ad86
JB
4441 i.tm.opcode_modifier.vexw = VEXW0;
4442 }
b6f8c7c4
L
4443 else
4444 i.tm.opcode_modifier.vex = VEX128;
4445
4446 if (i.tm.opcode_modifier.vex)
4447 for (j = 0; j < 3; j++)
4448 {
4449 i.types[j].bitfield.xmmword = 1;
4450 i.types[j].bitfield.ymmword = 0;
4451 }
4452 }
392a5972 4453 else if (i.vec_encoding != vex_encoding_evex
97ed31ae 4454 && !i.types[0].bitfield.zmmword
392a5972 4455 && !i.types[1].bitfield.zmmword
6225c532 4456 && !i.mask.reg
a5748e0d 4457 && !i.broadcast.bytes
97ed31ae 4458 && is_evex_encoding (&i.tm)
35648716
JB
4459 && ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4460 || (i.tm.base_opcode & ~4) == 0xdb
4461 || (i.tm.base_opcode & ~4) == 0xeb)
97ed31ae
L
4462 && i.tm.extension_opcode == None)
4463 {
4464 /* Optimize: -O1:
4465 VOP, one of vmovdqa32, vmovdqa64, vmovdqu8, vmovdqu16,
4466 vmovdqu32 and vmovdqu64:
4467 EVEX VOP %xmmM, %xmmN
4468 -> VEX vmovdqa|vmovdqu %xmmM, %xmmN (M and N < 16)
4469 EVEX VOP %ymmM, %ymmN
4470 -> VEX vmovdqa|vmovdqu %ymmM, %ymmN (M and N < 16)
4471 EVEX VOP %xmmM, mem
4472 -> VEX vmovdqa|vmovdqu %xmmM, mem (M < 16)
4473 EVEX VOP %ymmM, mem
4474 -> VEX vmovdqa|vmovdqu %ymmM, mem (M < 16)
4475 EVEX VOP mem, %xmmN
4476 -> VEX mvmovdqa|vmovdquem, %xmmN (N < 16)
4477 EVEX VOP mem, %ymmN
4478 -> VEX vmovdqa|vmovdqu mem, %ymmN (N < 16)
a0a1771e
JB
4479 VOP, one of vpand, vpandn, vpor, vpxor:
4480 EVEX VOP{d,q} %xmmL, %xmmM, %xmmN
4481 -> VEX VOP %xmmL, %xmmM, %xmmN (L, M, and N < 16)
4482 EVEX VOP{d,q} %ymmL, %ymmM, %ymmN
4483 -> VEX VOP %ymmL, %ymmM, %ymmN (L, M, and N < 16)
4484 EVEX VOP{d,q} mem, %xmmM, %xmmN
4485 -> VEX VOP mem, %xmmM, %xmmN (M and N < 16)
4486 EVEX VOP{d,q} mem, %ymmM, %ymmN
4487 -> VEX VOP mem, %ymmM, %ymmN (M and N < 16)
97ed31ae 4488 */
a0a1771e 4489 for (j = 0; j < i.operands; j++)
392a5972
L
4490 if (operand_type_check (i.types[j], disp)
4491 && i.op[j].disps->X_op == O_constant)
4492 {
4493 /* Since the VEX prefix has 2 or 3 bytes, the EVEX prefix
4494 has 4 bytes, EVEX Disp8 has 1 byte and VEX Disp32 has 4
4495 bytes, we choose EVEX Disp8 over VEX Disp32. */
4496 int evex_disp8, vex_disp8;
4497 unsigned int memshift = i.memshift;
4498 offsetT n = i.op[j].disps->X_add_number;
4499
4500 evex_disp8 = fits_in_disp8 (n);
4501 i.memshift = 0;
4502 vex_disp8 = fits_in_disp8 (n);
4503 if (evex_disp8 != vex_disp8)
4504 {
4505 i.memshift = memshift;
4506 return;
4507 }
4508
4509 i.types[j].bitfield.disp8 = vex_disp8;
4510 break;
4511 }
35648716
JB
4512 if ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4513 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2)
4514 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
97ed31ae
L
4515 i.tm.opcode_modifier.vex
4516 = i.types[0].bitfield.ymmword ? VEX256 : VEX128;
4517 i.tm.opcode_modifier.vexw = VEXW0;
79dec6b7 4518 /* VPAND, VPOR, and VPXOR are commutative. */
35648716 4519 if (i.reg_operands == 3 && i.tm.base_opcode != 0xdf)
79dec6b7 4520 i.tm.opcode_modifier.commutative = 1;
97ed31ae
L
4521 i.tm.opcode_modifier.evex = 0;
4522 i.tm.opcode_modifier.masking = 0;
a0a1771e 4523 i.tm.opcode_modifier.broadcast = 0;
97ed31ae
L
4524 i.tm.opcode_modifier.disp8memshift = 0;
4525 i.memshift = 0;
a0a1771e
JB
4526 if (j < i.operands)
4527 i.types[j].bitfield.disp8
4528 = fits_in_disp8 (i.op[j].disps->X_add_number);
97ed31ae 4529 }
b6f8c7c4
L
4530}
4531
ae531041
L
4532/* Return non-zero for load instruction. */
4533
4534static int
4535load_insn_p (void)
4536{
4537 unsigned int dest;
4538 int any_vex_p = is_any_vex_encoding (&i.tm);
4539 unsigned int base_opcode = i.tm.base_opcode | 1;
4540
4541 if (!any_vex_p)
4542 {
ef07be45
CL
4543 /* Anysize insns: lea, invlpg, clflush, prefetch*, bndmk, bndcl, bndcu,
4544 bndcn, bndstx, bndldx, clflushopt, clwb, cldemote. */
255571cd 4545 if (i.tm.opcode_modifier.operandconstraint == ANY_SIZE)
ae531041
L
4546 return 0;
4547
389d00a5
JB
4548 /* pop. */
4549 if (strcmp (i.tm.name, "pop") == 0)
4550 return 1;
4551 }
4552
4553 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE)
4554 {
4555 /* popf, popa. */
4556 if (i.tm.base_opcode == 0x9d
a09f656b 4557 || i.tm.base_opcode == 0x61)
ae531041
L
4558 return 1;
4559
4560 /* movs, cmps, lods, scas. */
4561 if ((i.tm.base_opcode | 0xb) == 0xaf)
4562 return 1;
4563
a09f656b 4564 /* outs, xlatb. */
4565 if (base_opcode == 0x6f
4566 || i.tm.base_opcode == 0xd7)
ae531041 4567 return 1;
a09f656b 4568 /* NB: For AMD-specific insns with implicit memory operands,
4569 they're intentionally not covered. */
ae531041
L
4570 }
4571
4572 /* No memory operand. */
4573 if (!i.mem_operands)
4574 return 0;
4575
4576 if (any_vex_p)
4577 {
4578 /* vldmxcsr. */
4579 if (i.tm.base_opcode == 0xae
4580 && i.tm.opcode_modifier.vex
441f6aca 4581 && i.tm.opcode_modifier.opcodespace == SPACE_0F
35648716 4582 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
ae531041
L
4583 && i.tm.extension_opcode == 2)
4584 return 1;
4585 }
389d00a5 4586 else if (i.tm.opcode_modifier.opcodespace == SPACE_BASE)
ae531041
L
4587 {
4588 /* test, not, neg, mul, imul, div, idiv. */
4589 if ((i.tm.base_opcode == 0xf6 || i.tm.base_opcode == 0xf7)
4590 && i.tm.extension_opcode != 1)
4591 return 1;
4592
4593 /* inc, dec. */
4594 if (base_opcode == 0xff && i.tm.extension_opcode <= 1)
4595 return 1;
4596
4597 /* add, or, adc, sbb, and, sub, xor, cmp. */
4598 if (i.tm.base_opcode >= 0x80 && i.tm.base_opcode <= 0x83)
4599 return 1;
4600
ae531041
L
4601 /* rol, ror, rcl, rcr, shl/sal, shr, sar. */
4602 if ((base_opcode == 0xc1
4603 || (i.tm.base_opcode >= 0xd0 && i.tm.base_opcode <= 0xd3))
4604 && i.tm.extension_opcode != 6)
4605 return 1;
4606
ae531041 4607 /* Check for x87 instructions. */
389d00a5 4608 if (base_opcode >= 0xd8 && base_opcode <= 0xdf)
ae531041
L
4609 {
4610 /* Skip fst, fstp, fstenv, fstcw. */
4611 if (i.tm.base_opcode == 0xd9
4612 && (i.tm.extension_opcode == 2
4613 || i.tm.extension_opcode == 3
4614 || i.tm.extension_opcode == 6
4615 || i.tm.extension_opcode == 7))
4616 return 0;
4617
4618 /* Skip fisttp, fist, fistp, fstp. */
4619 if (i.tm.base_opcode == 0xdb
4620 && (i.tm.extension_opcode == 1
4621 || i.tm.extension_opcode == 2
4622 || i.tm.extension_opcode == 3
4623 || i.tm.extension_opcode == 7))
4624 return 0;
4625
4626 /* Skip fisttp, fst, fstp, fsave, fstsw. */
4627 if (i.tm.base_opcode == 0xdd
4628 && (i.tm.extension_opcode == 1
4629 || i.tm.extension_opcode == 2
4630 || i.tm.extension_opcode == 3
4631 || i.tm.extension_opcode == 6
4632 || i.tm.extension_opcode == 7))
4633 return 0;
4634
4635 /* Skip fisttp, fist, fistp, fbstp, fistp. */
4636 if (i.tm.base_opcode == 0xdf
4637 && (i.tm.extension_opcode == 1
4638 || i.tm.extension_opcode == 2
4639 || i.tm.extension_opcode == 3
4640 || i.tm.extension_opcode == 6
4641 || i.tm.extension_opcode == 7))
4642 return 0;
4643
4644 return 1;
4645 }
4646 }
389d00a5
JB
4647 else if (i.tm.opcode_modifier.opcodespace == SPACE_0F)
4648 {
4649 /* bt, bts, btr, btc. */
4650 if (i.tm.base_opcode == 0xba
4651 && (i.tm.extension_opcode >= 4 && i.tm.extension_opcode <= 7))
4652 return 1;
4653
4654 /* cmpxchg8b, cmpxchg16b, xrstors, vmptrld. */
4655 if (i.tm.base_opcode == 0xc7
4656 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
4657 && (i.tm.extension_opcode == 1 || i.tm.extension_opcode == 3
4658 || i.tm.extension_opcode == 6))
4659 return 1;
4660
4661 /* fxrstor, ldmxcsr, xrstor. */
4662 if (i.tm.base_opcode == 0xae
4663 && (i.tm.extension_opcode == 1
4664 || i.tm.extension_opcode == 2
4665 || i.tm.extension_opcode == 5))
4666 return 1;
4667
4668 /* lgdt, lidt, lmsw. */
4669 if (i.tm.base_opcode == 0x01
4670 && (i.tm.extension_opcode == 2
4671 || i.tm.extension_opcode == 3
4672 || i.tm.extension_opcode == 6))
4673 return 1;
4674 }
ae531041
L
4675
4676 dest = i.operands - 1;
4677
4678 /* Check fake imm8 operand and 3 source operands. */
4679 if ((i.tm.opcode_modifier.immext
4680 || i.tm.opcode_modifier.vexsources == VEX3SOURCES)
4681 && i.types[dest].bitfield.imm8)
4682 dest--;
4683
389d00a5
JB
4684 /* add, or, adc, sbb, and, sub, xor, cmp, test, xchg. */
4685 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
ae531041
L
4686 && (base_opcode == 0x1
4687 || base_opcode == 0x9
4688 || base_opcode == 0x11
4689 || base_opcode == 0x19
4690 || base_opcode == 0x21
4691 || base_opcode == 0x29
4692 || base_opcode == 0x31
4693 || base_opcode == 0x39
389d00a5
JB
4694 || (base_opcode | 2) == 0x87))
4695 return 1;
4696
4697 /* xadd. */
4698 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4699 && base_opcode == 0xc1)
ae531041
L
4700 return 1;
4701
4702 /* Check for load instruction. */
4703 return (i.types[dest].bitfield.class != ClassNone
4704 || i.types[dest].bitfield.instance == Accum);
4705}
4706
4707/* Output lfence, 0xfaee8, after instruction. */
4708
4709static void
4710insert_lfence_after (void)
4711{
4712 if (lfence_after_load && load_insn_p ())
4713 {
a09f656b 4714 /* There are also two REP string instructions that require
4715 special treatment. Specifically, the compare string (CMPS)
4716 and scan string (SCAS) instructions set EFLAGS in a manner
4717 that depends on the data being compared/scanned. When used
4718 with a REP prefix, the number of iterations may therefore
4719 vary depending on this data. If the data is a program secret
4720 chosen by the adversary using an LVI method,
4721 then this data-dependent behavior may leak some aspect
4722 of the secret. */
4723 if (((i.tm.base_opcode | 0x1) == 0xa7
4724 || (i.tm.base_opcode | 0x1) == 0xaf)
4725 && i.prefix[REP_PREFIX])
4726 {
4727 as_warn (_("`%s` changes flags which would affect control flow behavior"),
4728 i.tm.name);
4729 }
ae531041
L
4730 char *p = frag_more (3);
4731 *p++ = 0xf;
4732 *p++ = 0xae;
4733 *p = 0xe8;
4734 }
4735}
4736
4737/* Output lfence, 0xfaee8, before instruction. */
4738
4739static void
4740insert_lfence_before (void)
4741{
4742 char *p;
4743
389d00a5 4744 if (i.tm.opcode_modifier.opcodespace != SPACE_BASE)
ae531041
L
4745 return;
4746
4747 if (i.tm.base_opcode == 0xff
4748 && (i.tm.extension_opcode == 2 || i.tm.extension_opcode == 4))
4749 {
4750 /* Insert lfence before indirect branch if needed. */
4751
4752 if (lfence_before_indirect_branch == lfence_branch_none)
4753 return;
4754
4755 if (i.operands != 1)
4756 abort ();
4757
4758 if (i.reg_operands == 1)
4759 {
4760 /* Indirect branch via register. Don't insert lfence with
4761 -mlfence-after-load=yes. */
4762 if (lfence_after_load
4763 || lfence_before_indirect_branch == lfence_branch_memory)
4764 return;
4765 }
4766 else if (i.mem_operands == 1
4767 && lfence_before_indirect_branch != lfence_branch_register)
4768 {
4769 as_warn (_("indirect `%s` with memory operand should be avoided"),
4770 i.tm.name);
4771 return;
4772 }
4773 else
4774 return;
4775
4776 if (last_insn.kind != last_insn_other
4777 && last_insn.seg == now_seg)
4778 {
4779 as_warn_where (last_insn.file, last_insn.line,
4780 _("`%s` skips -mlfence-before-indirect-branch on `%s`"),
4781 last_insn.name, i.tm.name);
4782 return;
4783 }
4784
4785 p = frag_more (3);
4786 *p++ = 0xf;
4787 *p++ = 0xae;
4788 *p = 0xe8;
4789 return;
4790 }
4791
503648e4 4792 /* Output or/not/shl and lfence before near ret. */
ae531041
L
4793 if (lfence_before_ret != lfence_before_ret_none
4794 && (i.tm.base_opcode == 0xc2
503648e4 4795 || i.tm.base_opcode == 0xc3))
ae531041
L
4796 {
4797 if (last_insn.kind != last_insn_other
4798 && last_insn.seg == now_seg)
4799 {
4800 as_warn_where (last_insn.file, last_insn.line,
4801 _("`%s` skips -mlfence-before-ret on `%s`"),
4802 last_insn.name, i.tm.name);
4803 return;
4804 }
a09f656b 4805
a09f656b 4806 /* Near ret ingore operand size override under CPU64. */
503648e4 4807 char prefix = flag_code == CODE_64BIT
4808 ? 0x48
4809 : i.prefix[DATA_PREFIX] ? 0x66 : 0x0;
a09f656b 4810
4811 if (lfence_before_ret == lfence_before_ret_not)
4812 {
4813 /* not: 0xf71424, may add prefix
4814 for operand size override or 64-bit code. */
4815 p = frag_more ((prefix ? 2 : 0) + 6 + 3);
4816 if (prefix)
4817 *p++ = prefix;
ae531041
L
4818 *p++ = 0xf7;
4819 *p++ = 0x14;
4820 *p++ = 0x24;
a09f656b 4821 if (prefix)
4822 *p++ = prefix;
ae531041
L
4823 *p++ = 0xf7;
4824 *p++ = 0x14;
4825 *p++ = 0x24;
4826 }
a09f656b 4827 else
4828 {
4829 p = frag_more ((prefix ? 1 : 0) + 4 + 3);
4830 if (prefix)
4831 *p++ = prefix;
4832 if (lfence_before_ret == lfence_before_ret_or)
4833 {
4834 /* or: 0x830c2400, may add prefix
4835 for operand size override or 64-bit code. */
4836 *p++ = 0x83;
4837 *p++ = 0x0c;
4838 }
4839 else
4840 {
4841 /* shl: 0xc1242400, may add prefix
4842 for operand size override or 64-bit code. */
4843 *p++ = 0xc1;
4844 *p++ = 0x24;
4845 }
4846
4847 *p++ = 0x24;
4848 *p++ = 0x0;
4849 }
4850
ae531041
L
4851 *p++ = 0xf;
4852 *p++ = 0xae;
4853 *p = 0xe8;
4854 }
4855}
4856
252b5132
RH
4857/* This is the guts of the machine-dependent assembler. LINE points to a
4858 machine dependent instruction. This function is supposed to emit
4859 the frags/bytes it assembles to. */
4860
4861void
65da13b5 4862md_assemble (char *line)
252b5132 4863{
40fb9820 4864 unsigned int j;
83b16ac6 4865 char mnemonic[MAX_MNEM_SIZE], mnem_suffix;
d3ce72d0 4866 const insn_template *t;
252b5132 4867
47926f60 4868 /* Initialize globals. */
252b5132 4869 memset (&i, '\0', sizeof (i));
ca5312a2 4870 i.rounding.type = rc_none;
252b5132 4871 for (j = 0; j < MAX_OPERANDS; j++)
1ae12ab7 4872 i.reloc[j] = NO_RELOC;
252b5132
RH
4873 memset (disp_expressions, '\0', sizeof (disp_expressions));
4874 memset (im_expressions, '\0', sizeof (im_expressions));
ce8a8b2f 4875 save_stack_p = save_stack;
252b5132
RH
4876
4877 /* First parse an instruction mnemonic & call i386_operand for the operands.
4878 We assume that the scrubber has arranged it so that line[0] is the valid
47926f60 4879 start of a (possibly prefixed) mnemonic. */
252b5132 4880
29b0f896
AM
4881 line = parse_insn (line, mnemonic);
4882 if (line == NULL)
4883 return;
83b16ac6 4884 mnem_suffix = i.suffix;
252b5132 4885
29b0f896 4886 line = parse_operands (line, mnemonic);
ee86248c 4887 this_operand = -1;
8325cc63
JB
4888 xfree (i.memop1_string);
4889 i.memop1_string = NULL;
29b0f896
AM
4890 if (line == NULL)
4891 return;
252b5132 4892
29b0f896
AM
4893 /* Now we've parsed the mnemonic into a set of templates, and have the
4894 operands at hand. */
4895
b630c145 4896 /* All Intel opcodes have reversed operands except for "bound", "enter",
c0e54661 4897 "invlpg*", "monitor*", "mwait*", "tpause", "umwait", "pvalidate",
b0e8fa7f
TJ
4898 "rmpadjust", "rmpupdate", and "rmpquery". We also don't reverse
4899 intersegment "jmp" and "call" instructions with 2 immediate operands so
4900 that the immediate segment precedes the offset consistently in Intel and
4901 AT&T modes. */
4d456e3d
L
4902 if (intel_syntax
4903 && i.operands > 1
29b0f896 4904 && (strcmp (mnemonic, "bound") != 0)
c0e54661 4905 && (strncmp (mnemonic, "invlpg", 6) != 0)
d34049e8
ML
4906 && !startswith (mnemonic, "monitor")
4907 && !startswith (mnemonic, "mwait")
c0e54661 4908 && (strcmp (mnemonic, "pvalidate") != 0)
d34049e8 4909 && !startswith (mnemonic, "rmp")
b630c145
JB
4910 && (strcmp (mnemonic, "tpause") != 0)
4911 && (strcmp (mnemonic, "umwait") != 0)
47c0279b
JB
4912 && !(i.operands == 2
4913 && operand_type_check (i.types[0], imm)
40fb9820 4914 && operand_type_check (i.types[1], imm)))
29b0f896
AM
4915 swap_operands ();
4916
ec56d5c0
JB
4917 /* The order of the immediates should be reversed
4918 for 2 immediates extrq and insertq instructions */
4919 if (i.imm_operands == 2
4920 && (strcmp (mnemonic, "extrq") == 0
4921 || strcmp (mnemonic, "insertq") == 0))
4922 swap_2_operands (0, 1);
4923
29b0f896
AM
4924 if (i.imm_operands)
4925 optimize_imm ();
4926
9386188e
JB
4927 if (i.disp_operands && !want_disp32 (current_templates->start)
4928 && (!current_templates->start->opcode_modifier.jump
4929 || i.jumpabsolute || i.types[0].bitfield.baseindex))
cce08655
JB
4930 {
4931 for (j = 0; j < i.operands; ++j)
4932 {
4933 const expressionS *exp = i.op[j].disps;
4934
4935 if (!operand_type_check (i.types[j], disp))
4936 continue;
4937
4938 if (exp->X_op != O_constant)
4939 continue;
4940
4941 /* Since displacement is signed extended to 64bit, don't allow
a775efc8 4942 disp32 if it is out of range. */
cce08655
JB
4943 if (fits_in_signed_long (exp->X_add_number))
4944 continue;
4945
a775efc8 4946 i.types[j].bitfield.disp32 = 0;
cce08655
JB
4947 if (i.types[j].bitfield.baseindex)
4948 {
f493c217
AM
4949 as_bad (_("0x%" PRIx64 " out of range of signed 32bit displacement"),
4950 (uint64_t) exp->X_add_number);
cce08655
JB
4951 return;
4952 }
4953 }
4954 }
4955
b300c311
L
4956 /* Don't optimize displacement for movabs since it only takes 64bit
4957 displacement. */
4958 if (i.disp_operands
1a42a9fe 4959 && i.disp_encoding <= disp_encoding_8bit
862be3fb
L
4960 && (flag_code != CODE_64BIT
4961 || strcmp (mnemonic, "movabs") != 0))
4962 optimize_disp ();
29b0f896
AM
4963
4964 /* Next, we find a template that matches the given insn,
4965 making sure the overlap of the given operands types is consistent
4966 with the template operand types. */
252b5132 4967
83b16ac6 4968 if (!(t = match_template (mnem_suffix)))
29b0f896 4969 return;
252b5132 4970
7bab8ab5 4971 if (sse_check != check_none
ffb86450
JB
4972 /* The opcode space check isn't strictly needed; it's there only to
4973 bypass the logic below when easily possible. */
4974 && t->opcode_modifier.opcodespace >= SPACE_0F
4975 && t->opcode_modifier.opcodespace <= SPACE_0F3A
4976 && !i.tm.cpu_flags.bitfield.cpusse4a
4977 && !is_any_vex_encoding (t))
daf50ae7 4978 {
ffb86450
JB
4979 bool simd = false;
4980
4981 for (j = 0; j < t->operands; ++j)
4982 {
4983 if (t->operand_types[j].bitfield.class == RegMMX)
4984 break;
4985 if (t->operand_types[j].bitfield.class == RegSIMD)
4986 simd = true;
4987 }
4988
4989 if (j >= t->operands && simd)
4990 (sse_check == check_warning
4991 ? as_warn
4992 : as_bad) (_("SSE instruction `%s' is used"), i.tm.name);
daf50ae7
L
4993 }
4994
40fb9820 4995 if (i.tm.opcode_modifier.fwait)
29b0f896
AM
4996 if (!add_prefix (FWAIT_OPCODE))
4997 return;
252b5132 4998
d5de92cf 4999 /* Check if REP prefix is OK. */
742732c7 5000 if (i.rep_prefix && i.tm.opcode_modifier.prefixok != PrefixRep)
d5de92cf
L
5001 {
5002 as_bad (_("invalid instruction `%s' after `%s'"),
5003 i.tm.name, i.rep_prefix);
5004 return;
5005 }
5006
c1ba0266
L
5007 /* Check for lock without a lockable instruction. Destination operand
5008 must be memory unless it is xchg (0x86). */
c32fa91d 5009 if (i.prefix[LOCK_PREFIX]
742732c7 5010 && (i.tm.opcode_modifier.prefixok < PrefixLock
c1ba0266
L
5011 || i.mem_operands == 0
5012 || (i.tm.base_opcode != 0x86
8dc0818e 5013 && !(i.flags[i.operands - 1] & Operand_Mem))))
c32fa91d
L
5014 {
5015 as_bad (_("expecting lockable instruction after `lock'"));
5016 return;
5017 }
5018
40d231b4
JB
5019 /* Check for data size prefix on VEX/XOP/EVEX encoded and SIMD insns. */
5020 if (i.prefix[DATA_PREFIX]
5021 && (is_any_vex_encoding (&i.tm)
5022 || i.tm.operand_types[i.imm_operands].bitfield.class >= RegMMX
5023 || i.tm.operand_types[i.imm_operands + 1].bitfield.class >= RegMMX))
7a8655d2
JB
5024 {
5025 as_bad (_("data size prefix invalid with `%s'"), i.tm.name);
5026 return;
5027 }
5028
42164a71 5029 /* Check if HLE prefix is OK. */
165de32a 5030 if (i.hle_prefix && !check_hle ())
42164a71
L
5031 return;
5032
7e8b059b
L
5033 /* Check BND prefix. */
5034 if (i.bnd_prefix && !i.tm.opcode_modifier.bndprefixok)
5035 as_bad (_("expecting valid branch instruction after `bnd'"));
5036
04ef582a 5037 /* Check NOTRACK prefix. */
742732c7 5038 if (i.notrack_prefix && i.tm.opcode_modifier.prefixok != PrefixNoTrack)
9fef80d6 5039 as_bad (_("expecting indirect branch instruction after `notrack'"));
04ef582a 5040
327e8c42
JB
5041 if (i.tm.cpu_flags.bitfield.cpumpx)
5042 {
5043 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
5044 as_bad (_("32-bit address isn't allowed in 64-bit MPX instructions."));
5045 else if (flag_code != CODE_16BIT
5046 ? i.prefix[ADDR_PREFIX]
5047 : i.mem_operands && !i.prefix[ADDR_PREFIX])
5048 as_bad (_("16-bit address isn't allowed in MPX instructions"));
5049 }
7e8b059b
L
5050
5051 /* Insert BND prefix. */
76d3a78a
JB
5052 if (add_bnd_prefix && i.tm.opcode_modifier.bndprefixok)
5053 {
5054 if (!i.prefix[BND_PREFIX])
5055 add_prefix (BND_PREFIX_OPCODE);
5056 else if (i.prefix[BND_PREFIX] != BND_PREFIX_OPCODE)
5057 {
5058 as_warn (_("replacing `rep'/`repe' prefix by `bnd'"));
5059 i.prefix[BND_PREFIX] = BND_PREFIX_OPCODE;
5060 }
5061 }
7e8b059b 5062
29b0f896 5063 /* Check string instruction segment overrides. */
51c8edf6 5064 if (i.tm.opcode_modifier.isstring >= IS_STRING_ES_OP0)
29b0f896 5065 {
51c8edf6 5066 gas_assert (i.mem_operands);
29b0f896 5067 if (!check_string ())
5dd0794d 5068 return;
fc0763e6 5069 i.disp_operands = 0;
29b0f896 5070 }
5dd0794d 5071
9373f275
L
5072 /* The memory operand of (%dx) should be only used with input/output
5073 instructions (base opcodes: 0x6c, 0x6e, 0xec, 0xee). */
5074 if (i.input_output_operand
5075 && ((i.tm.base_opcode | 0x82) != 0xee
5076 || i.tm.opcode_modifier.opcodespace != SPACE_BASE))
5077 {
5078 as_bad (_("input/output port address isn't allowed with `%s'"),
5079 i.tm.name);
5080 return;
5081 }
5082
b6f8c7c4
L
5083 if (optimize && !i.no_optimize && i.tm.opcode_modifier.optimize)
5084 optimize_encoding ();
5085
c8480b58
L
5086 if (use_unaligned_vector_move)
5087 encode_with_unaligned_vector_move ();
5088
29b0f896
AM
5089 if (!process_suffix ())
5090 return;
e413e4e9 5091
ef07be45
CL
5092 /* Check if IP-relative addressing requirements can be satisfied. */
5093 if (i.tm.cpu_flags.bitfield.cpuprefetchi
5094 && !(i.base_reg && i.base_reg->reg_num == RegIP))
f2462532 5095 as_warn (_("'%s' only supports RIP-relative address"), i.tm.name);
ef07be45 5096
921eafea 5097 /* Update operand types and check extended states. */
bc0844ae 5098 for (j = 0; j < i.operands; j++)
921eafea
L
5099 {
5100 i.types[j] = operand_type_and (i.types[j], i.tm.operand_types[j]);
3d70986f 5101 switch (i.tm.operand_types[j].bitfield.class)
921eafea
L
5102 {
5103 default:
5104 break;
5105 case RegMMX:
5106 i.xstate |= xstate_mmx;
5107 break;
5108 case RegMask:
32930e4e 5109 i.xstate |= xstate_mask;
921eafea
L
5110 break;
5111 case RegSIMD:
3d70986f 5112 if (i.tm.operand_types[j].bitfield.tmmword)
921eafea 5113 i.xstate |= xstate_tmm;
3d70986f 5114 else if (i.tm.operand_types[j].bitfield.zmmword)
921eafea 5115 i.xstate |= xstate_zmm;
3d70986f 5116 else if (i.tm.operand_types[j].bitfield.ymmword)
921eafea 5117 i.xstate |= xstate_ymm;
3d70986f 5118 else if (i.tm.operand_types[j].bitfield.xmmword)
921eafea
L
5119 i.xstate |= xstate_xmm;
5120 break;
5121 }
5122 }
bc0844ae 5123
29b0f896
AM
5124 /* Make still unresolved immediate matches conform to size of immediate
5125 given in i.suffix. */
5126 if (!finalize_imm ())
5127 return;
252b5132 5128
40fb9820 5129 if (i.types[0].bitfield.imm1)
29b0f896 5130 i.imm_operands = 0; /* kludge for shift insns. */
252b5132 5131
9afe6eb8
L
5132 /* We only need to check those implicit registers for instructions
5133 with 3 operands or less. */
5134 if (i.operands <= 3)
5135 for (j = 0; j < i.operands; j++)
75e5731b
JB
5136 if (i.types[j].bitfield.instance != InstanceNone
5137 && !i.types[j].bitfield.xmmword)
9afe6eb8 5138 i.reg_operands--;
40fb9820 5139
29b0f896
AM
5140 /* For insns with operands there are more diddles to do to the opcode. */
5141 if (i.operands)
5142 {
5143 if (!process_operands ())
5144 return;
5145 }
255571cd 5146 else if (!quiet_warnings && i.tm.opcode_modifier.operandconstraint == UGH)
29b0f896
AM
5147 {
5148 /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc. */
5149 as_warn (_("translating to `%sp'"), i.tm.name);
5150 }
252b5132 5151
7a8655d2 5152 if (is_any_vex_encoding (&i.tm))
9e5e5283 5153 {
c1dc7af5 5154 if (!cpu_arch_flags.bitfield.cpui286)
9e5e5283 5155 {
c1dc7af5 5156 as_bad (_("instruction `%s' isn't supported outside of protected mode."),
9e5e5283
L
5157 i.tm.name);
5158 return;
5159 }
c0f3af97 5160
0b9404fd
JB
5161 /* Check for explicit REX prefix. */
5162 if (i.prefix[REX_PREFIX] || i.rex_encoding)
5163 {
5164 as_bad (_("REX prefix invalid with `%s'"), i.tm.name);
5165 return;
5166 }
5167
9e5e5283
L
5168 if (i.tm.opcode_modifier.vex)
5169 build_vex_prefix (t);
5170 else
5171 build_evex_prefix ();
0b9404fd
JB
5172
5173 /* The individual REX.RXBW bits got consumed. */
5174 i.rex &= REX_OPCODE;
9e5e5283 5175 }
43234a1e 5176
5dd85c99
SP
5177 /* Handle conversion of 'int $3' --> special int3 insn. XOP or FMA4
5178 instructions may define INT_OPCODE as well, so avoid this corner
5179 case for those instructions that use MODRM. */
389d00a5
JB
5180 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
5181 && i.tm.base_opcode == INT_OPCODE
a6461c02
SP
5182 && !i.tm.opcode_modifier.modrm
5183 && i.op[0].imms->X_add_number == 3)
29b0f896
AM
5184 {
5185 i.tm.base_opcode = INT3_OPCODE;
5186 i.imm_operands = 0;
5187 }
252b5132 5188
0cfa3eb3
JB
5189 if ((i.tm.opcode_modifier.jump == JUMP
5190 || i.tm.opcode_modifier.jump == JUMP_BYTE
5191 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896
AM
5192 && i.op[0].disps->X_op == O_constant)
5193 {
5194 /* Convert "jmp constant" (and "call constant") to a jump (call) to
5195 the absolute address given by the constant. Since ix86 jumps and
5196 calls are pc relative, we need to generate a reloc. */
5197 i.op[0].disps->X_add_symbol = &abs_symbol;
5198 i.op[0].disps->X_op = O_symbol;
5199 }
252b5132 5200
29b0f896
AM
5201 /* For 8 bit registers we need an empty rex prefix. Also if the
5202 instruction already has a prefix, we need to convert old
5203 registers to new ones. */
773f551c 5204
bab6aec1 5205 if ((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte
29b0f896 5206 && (i.op[0].regs->reg_flags & RegRex64) != 0)
bab6aec1 5207 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte
29b0f896 5208 && (i.op[1].regs->reg_flags & RegRex64) != 0)
bab6aec1
JB
5209 || (((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte)
5210 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte))
29b0f896
AM
5211 && i.rex != 0))
5212 {
5213 int x;
726c5dcd 5214
29b0f896
AM
5215 i.rex |= REX_OPCODE;
5216 for (x = 0; x < 2; x++)
5217 {
5218 /* Look for 8 bit operand that uses old registers. */
bab6aec1 5219 if (i.types[x].bitfield.class == Reg && i.types[x].bitfield.byte
29b0f896 5220 && (i.op[x].regs->reg_flags & RegRex64) == 0)
773f551c 5221 {
3f93af61 5222 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
29b0f896
AM
5223 /* In case it is "hi" register, give up. */
5224 if (i.op[x].regs->reg_num > 3)
a540244d 5225 as_bad (_("can't encode register '%s%s' in an "
4eed87de 5226 "instruction requiring REX prefix."),
a540244d 5227 register_prefix, i.op[x].regs->reg_name);
773f551c 5228
29b0f896
AM
5229 /* Otherwise it is equivalent to the extended register.
5230 Since the encoding doesn't change this is merely
5231 cosmetic cleanup for debug output. */
5232
5233 i.op[x].regs = i.op[x].regs + 8;
773f551c 5234 }
29b0f896
AM
5235 }
5236 }
773f551c 5237
6b6b6807
L
5238 if (i.rex == 0 && i.rex_encoding)
5239 {
5240 /* Check if we can add a REX_OPCODE byte. Look for 8 bit operand
3f93af61 5241 that uses legacy register. If it is "hi" register, don't add
6b6b6807
L
5242 the REX_OPCODE byte. */
5243 int x;
5244 for (x = 0; x < 2; x++)
bab6aec1 5245 if (i.types[x].bitfield.class == Reg
6b6b6807
L
5246 && i.types[x].bitfield.byte
5247 && (i.op[x].regs->reg_flags & RegRex64) == 0
5248 && i.op[x].regs->reg_num > 3)
5249 {
3f93af61 5250 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
5b7c81bd 5251 i.rex_encoding = false;
6b6b6807
L
5252 break;
5253 }
5254
5255 if (i.rex_encoding)
5256 i.rex = REX_OPCODE;
5257 }
5258
7ab9ffdd 5259 if (i.rex != 0)
29b0f896
AM
5260 add_prefix (REX_OPCODE | i.rex);
5261
ae531041
L
5262 insert_lfence_before ();
5263
29b0f896
AM
5264 /* We are ready to output the insn. */
5265 output_insn ();
e379e5f3 5266
ae531041
L
5267 insert_lfence_after ();
5268
e379e5f3
L
5269 last_insn.seg = now_seg;
5270
5271 if (i.tm.opcode_modifier.isprefix)
5272 {
5273 last_insn.kind = last_insn_prefix;
5274 last_insn.name = i.tm.name;
5275 last_insn.file = as_where (&last_insn.line);
5276 }
5277 else
5278 last_insn.kind = last_insn_other;
29b0f896
AM
5279}
5280
5281static char *
e3bb37b5 5282parse_insn (char *line, char *mnemonic)
29b0f896
AM
5283{
5284 char *l = line;
5285 char *token_start = l;
5286 char *mnem_p;
5c6af06e 5287 int supported;
d3ce72d0 5288 const insn_template *t;
b6169b20 5289 char *dot_p = NULL;
29b0f896 5290
29b0f896
AM
5291 while (1)
5292 {
5293 mnem_p = mnemonic;
5294 while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
5295 {
b6169b20
L
5296 if (*mnem_p == '.')
5297 dot_p = mnem_p;
29b0f896
AM
5298 mnem_p++;
5299 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
45288df1 5300 {
29b0f896
AM
5301 as_bad (_("no such instruction: `%s'"), token_start);
5302 return NULL;
5303 }
5304 l++;
5305 }
5306 if (!is_space_char (*l)
5307 && *l != END_OF_INSN
e44823cf
JB
5308 && (intel_syntax
5309 || (*l != PREFIX_SEPARATOR
5310 && *l != ',')))
29b0f896
AM
5311 {
5312 as_bad (_("invalid character %s in mnemonic"),
5313 output_invalid (*l));
5314 return NULL;
5315 }
5316 if (token_start == l)
5317 {
e44823cf 5318 if (!intel_syntax && *l == PREFIX_SEPARATOR)
29b0f896
AM
5319 as_bad (_("expecting prefix; got nothing"));
5320 else
5321 as_bad (_("expecting mnemonic; got nothing"));
5322 return NULL;
5323 }
45288df1 5324
29b0f896 5325 /* Look up instruction (or prefix) via hash table. */
629310ab 5326 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
47926f60 5327
29b0f896
AM
5328 if (*l != END_OF_INSN
5329 && (!is_space_char (*l) || l[1] != END_OF_INSN)
5330 && current_templates
40fb9820 5331 && current_templates->start->opcode_modifier.isprefix)
29b0f896 5332 {
c6fb90c8 5333 if (!cpu_flags_check_cpu64 (current_templates->start->cpu_flags))
2dd88dca
JB
5334 {
5335 as_bad ((flag_code != CODE_64BIT
5336 ? _("`%s' is only supported in 64-bit mode")
5337 : _("`%s' is not supported in 64-bit mode")),
5338 current_templates->start->name);
5339 return NULL;
5340 }
29b0f896
AM
5341 /* If we are in 16-bit mode, do not allow addr16 or data16.
5342 Similarly, in 32-bit mode, do not allow addr32 or data32. */
673fe0f0
JB
5343 if ((current_templates->start->opcode_modifier.size == SIZE16
5344 || current_templates->start->opcode_modifier.size == SIZE32)
29b0f896 5345 && flag_code != CODE_64BIT
673fe0f0 5346 && ((current_templates->start->opcode_modifier.size == SIZE32)
29b0f896
AM
5347 ^ (flag_code == CODE_16BIT)))
5348 {
5349 as_bad (_("redundant %s prefix"),
5350 current_templates->start->name);
5351 return NULL;
45288df1 5352 }
31184569
JB
5353
5354 if (current_templates->start->base_opcode == PSEUDO_PREFIX)
29b0f896 5355 {
86fa6981 5356 /* Handle pseudo prefixes. */
31184569 5357 switch (current_templates->start->extension_opcode)
86fa6981 5358 {
41eb8e88 5359 case Prefix_Disp8:
86fa6981
L
5360 /* {disp8} */
5361 i.disp_encoding = disp_encoding_8bit;
5362 break;
41eb8e88
L
5363 case Prefix_Disp16:
5364 /* {disp16} */
5365 i.disp_encoding = disp_encoding_16bit;
5366 break;
5367 case Prefix_Disp32:
86fa6981
L
5368 /* {disp32} */
5369 i.disp_encoding = disp_encoding_32bit;
5370 break;
41eb8e88 5371 case Prefix_Load:
86fa6981
L
5372 /* {load} */
5373 i.dir_encoding = dir_encoding_load;
5374 break;
41eb8e88 5375 case Prefix_Store:
86fa6981
L
5376 /* {store} */
5377 i.dir_encoding = dir_encoding_store;
5378 break;
41eb8e88 5379 case Prefix_VEX:
42e04b36
L
5380 /* {vex} */
5381 i.vec_encoding = vex_encoding_vex;
86fa6981 5382 break;
41eb8e88 5383 case Prefix_VEX3:
86fa6981
L
5384 /* {vex3} */
5385 i.vec_encoding = vex_encoding_vex3;
5386 break;
41eb8e88 5387 case Prefix_EVEX:
86fa6981
L
5388 /* {evex} */
5389 i.vec_encoding = vex_encoding_evex;
5390 break;
41eb8e88 5391 case Prefix_REX:
6b6b6807 5392 /* {rex} */
5b7c81bd 5393 i.rex_encoding = true;
6b6b6807 5394 break;
41eb8e88 5395 case Prefix_NoOptimize:
b6f8c7c4 5396 /* {nooptimize} */
5b7c81bd 5397 i.no_optimize = true;
b6f8c7c4 5398 break;
86fa6981
L
5399 default:
5400 abort ();
5401 }
5402 }
5403 else
5404 {
5405 /* Add prefix, checking for repeated prefixes. */
4e9ac44a 5406 switch (add_prefix (current_templates->start->base_opcode))
86fa6981 5407 {
4e9ac44a
L
5408 case PREFIX_EXIST:
5409 return NULL;
5410 case PREFIX_DS:
d777820b 5411 if (current_templates->start->cpu_flags.bitfield.cpuibt)
4e9ac44a
L
5412 i.notrack_prefix = current_templates->start->name;
5413 break;
5414 case PREFIX_REP:
5415 if (current_templates->start->cpu_flags.bitfield.cpuhle)
5416 i.hle_prefix = current_templates->start->name;
5417 else if (current_templates->start->cpu_flags.bitfield.cpumpx)
5418 i.bnd_prefix = current_templates->start->name;
5419 else
5420 i.rep_prefix = current_templates->start->name;
5421 break;
5422 default:
5423 break;
86fa6981 5424 }
29b0f896
AM
5425 }
5426 /* Skip past PREFIX_SEPARATOR and reset token_start. */
5427 token_start = ++l;
5428 }
5429 else
5430 break;
5431 }
45288df1 5432
30a55f88 5433 if (!current_templates)
b6169b20 5434 {
07d5e953
JB
5435 /* Deprecated functionality (new code should use pseudo-prefixes instead):
5436 Check if we should swap operand or force 32bit displacement in
f8a5c266 5437 encoding. */
30a55f88 5438 if (mnem_p - 2 == dot_p && dot_p[1] == 's')
64c49ab3 5439 i.dir_encoding = dir_encoding_swap;
8d63c93e 5440 else if (mnem_p - 3 == dot_p
a501d77e
L
5441 && dot_p[1] == 'd'
5442 && dot_p[2] == '8')
5443 i.disp_encoding = disp_encoding_8bit;
8d63c93e 5444 else if (mnem_p - 4 == dot_p
f8a5c266
L
5445 && dot_p[1] == 'd'
5446 && dot_p[2] == '3'
5447 && dot_p[3] == '2')
a501d77e 5448 i.disp_encoding = disp_encoding_32bit;
30a55f88
L
5449 else
5450 goto check_suffix;
5451 mnem_p = dot_p;
5452 *dot_p = '\0';
629310ab 5453 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
b6169b20
L
5454 }
5455
29b0f896
AM
5456 if (!current_templates)
5457 {
dc1e8a47 5458 check_suffix:
1c529385 5459 if (mnem_p > mnemonic)
29b0f896 5460 {
1c529385
LH
5461 /* See if we can get a match by trimming off a suffix. */
5462 switch (mnem_p[-1])
29b0f896 5463 {
1c529385
LH
5464 case WORD_MNEM_SUFFIX:
5465 if (intel_syntax && (intel_float_operand (mnemonic) & 2))
29b0f896
AM
5466 i.suffix = SHORT_MNEM_SUFFIX;
5467 else
1c529385
LH
5468 /* Fall through. */
5469 case BYTE_MNEM_SUFFIX:
5470 case QWORD_MNEM_SUFFIX:
5471 i.suffix = mnem_p[-1];
29b0f896 5472 mnem_p[-1] = '\0';
fe0e921f
AM
5473 current_templates
5474 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5475 break;
5476 case SHORT_MNEM_SUFFIX:
5477 case LONG_MNEM_SUFFIX:
5478 if (!intel_syntax)
5479 {
5480 i.suffix = mnem_p[-1];
5481 mnem_p[-1] = '\0';
fe0e921f
AM
5482 current_templates
5483 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5484 }
5485 break;
5486
5487 /* Intel Syntax. */
5488 case 'd':
5489 if (intel_syntax)
5490 {
5491 if (intel_float_operand (mnemonic) == 1)
5492 i.suffix = SHORT_MNEM_SUFFIX;
5493 else
5494 i.suffix = LONG_MNEM_SUFFIX;
5495 mnem_p[-1] = '\0';
fe0e921f
AM
5496 current_templates
5497 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5498 }
5499 break;
29b0f896 5500 }
29b0f896 5501 }
1c529385 5502
29b0f896
AM
5503 if (!current_templates)
5504 {
5505 as_bad (_("no such instruction: `%s'"), token_start);
5506 return NULL;
5507 }
5508 }
252b5132 5509
0cfa3eb3
JB
5510 if (current_templates->start->opcode_modifier.jump == JUMP
5511 || current_templates->start->opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
5512 {
5513 /* Check for a branch hint. We allow ",pt" and ",pn" for
5514 predict taken and predict not taken respectively.
5515 I'm not sure that branch hints actually do anything on loop
5516 and jcxz insns (JumpByte) for current Pentium4 chips. They
5517 may work in the future and it doesn't hurt to accept them
5518 now. */
5519 if (l[0] == ',' && l[1] == 'p')
5520 {
5521 if (l[2] == 't')
5522 {
5523 if (!add_prefix (DS_PREFIX_OPCODE))
5524 return NULL;
5525 l += 3;
5526 }
5527 else if (l[2] == 'n')
5528 {
5529 if (!add_prefix (CS_PREFIX_OPCODE))
5530 return NULL;
5531 l += 3;
5532 }
5533 }
5534 }
5535 /* Any other comma loses. */
5536 if (*l == ',')
5537 {
5538 as_bad (_("invalid character %s in mnemonic"),
5539 output_invalid (*l));
5540 return NULL;
5541 }
252b5132 5542
29b0f896 5543 /* Check if instruction is supported on specified architecture. */
5c6af06e
JB
5544 supported = 0;
5545 for (t = current_templates->start; t < current_templates->end; ++t)
5546 {
c0f3af97
L
5547 supported |= cpu_flags_match (t);
5548 if (supported == CPU_FLAGS_PERFECT_MATCH)
d59a54c2 5549 return l;
29b0f896 5550 }
3629bb00 5551
548d0ee6
JB
5552 if (!(supported & CPU_FLAGS_64BIT_MATCH))
5553 as_bad (flag_code == CODE_64BIT
5554 ? _("`%s' is not supported in 64-bit mode")
5555 : _("`%s' is only supported in 64-bit mode"),
5556 current_templates->start->name);
5557 else
5558 as_bad (_("`%s' is not supported on `%s%s'"),
5559 current_templates->start->name,
5560 cpu_arch_name ? cpu_arch_name : default_arch,
5561 cpu_sub_arch_name ? cpu_sub_arch_name : "");
252b5132 5562
548d0ee6 5563 return NULL;
29b0f896 5564}
252b5132 5565
29b0f896 5566static char *
e3bb37b5 5567parse_operands (char *l, const char *mnemonic)
29b0f896
AM
5568{
5569 char *token_start;
3138f287 5570
29b0f896
AM
5571 /* 1 if operand is pending after ','. */
5572 unsigned int expecting_operand = 0;
252b5132 5573
29b0f896
AM
5574 while (*l != END_OF_INSN)
5575 {
e68c3d59
JB
5576 /* Non-zero if operand parens not balanced. */
5577 unsigned int paren_not_balanced = 0;
5578 /* True if inside double quotes. */
5579 bool in_quotes = false;
5580
29b0f896
AM
5581 /* Skip optional white space before operand. */
5582 if (is_space_char (*l))
5583 ++l;
d02603dc 5584 if (!is_operand_char (*l) && *l != END_OF_INSN && *l != '"')
29b0f896
AM
5585 {
5586 as_bad (_("invalid character %s before operand %d"),
5587 output_invalid (*l),
5588 i.operands + 1);
5589 return NULL;
5590 }
d02603dc 5591 token_start = l; /* After white space. */
e68c3d59 5592 while (in_quotes || paren_not_balanced || *l != ',')
29b0f896
AM
5593 {
5594 if (*l == END_OF_INSN)
5595 {
e68c3d59
JB
5596 if (in_quotes)
5597 {
5598 as_bad (_("unbalanced double quotes in operand %d."),
5599 i.operands + 1);
5600 return NULL;
5601 }
29b0f896
AM
5602 if (paren_not_balanced)
5603 {
98ff9f1c
JB
5604 know (!intel_syntax);
5605 as_bad (_("unbalanced parenthesis in operand %d."),
5606 i.operands + 1);
29b0f896
AM
5607 return NULL;
5608 }
5609 else
5610 break; /* we are done */
5611 }
e68c3d59
JB
5612 else if (*l == '\\' && l[1] == '"')
5613 ++l;
5614 else if (*l == '"')
5615 in_quotes = !in_quotes;
5616 else if (!in_quotes && !is_operand_char (*l) && !is_space_char (*l))
29b0f896
AM
5617 {
5618 as_bad (_("invalid character %s in operand %d"),
5619 output_invalid (*l),
5620 i.operands + 1);
5621 return NULL;
5622 }
e68c3d59 5623 if (!intel_syntax && !in_quotes)
29b0f896
AM
5624 {
5625 if (*l == '(')
5626 ++paren_not_balanced;
5627 if (*l == ')')
5628 --paren_not_balanced;
5629 }
29b0f896
AM
5630 l++;
5631 }
5632 if (l != token_start)
5633 { /* Yes, we've read in another operand. */
5634 unsigned int operand_ok;
5635 this_operand = i.operands++;
5636 if (i.operands > MAX_OPERANDS)
5637 {
5638 as_bad (_("spurious operands; (%d operands/instruction max)"),
5639 MAX_OPERANDS);
5640 return NULL;
5641 }
9d46ce34 5642 i.types[this_operand].bitfield.unspecified = 1;
29b0f896
AM
5643 /* Now parse operand adding info to 'i' as we go along. */
5644 END_STRING_AND_SAVE (l);
5645
1286ab78
L
5646 if (i.mem_operands > 1)
5647 {
5648 as_bad (_("too many memory references for `%s'"),
5649 mnemonic);
5650 return 0;
5651 }
5652
29b0f896
AM
5653 if (intel_syntax)
5654 operand_ok =
5655 i386_intel_operand (token_start,
5656 intel_float_operand (mnemonic));
5657 else
a7619375 5658 operand_ok = i386_att_operand (token_start);
29b0f896
AM
5659
5660 RESTORE_END_STRING (l);
5661 if (!operand_ok)
5662 return NULL;
5663 }
5664 else
5665 {
5666 if (expecting_operand)
5667 {
5668 expecting_operand_after_comma:
5669 as_bad (_("expecting operand after ','; got nothing"));
5670 return NULL;
5671 }
5672 if (*l == ',')
5673 {
5674 as_bad (_("expecting operand before ','; got nothing"));
5675 return NULL;
5676 }
5677 }
7f3f1ea2 5678
29b0f896
AM
5679 /* Now *l must be either ',' or END_OF_INSN. */
5680 if (*l == ',')
5681 {
5682 if (*++l == END_OF_INSN)
5683 {
5684 /* Just skip it, if it's \n complain. */
5685 goto expecting_operand_after_comma;
5686 }
5687 expecting_operand = 1;
5688 }
5689 }
5690 return l;
5691}
7f3f1ea2 5692
050dfa73 5693static void
783c187b 5694swap_2_operands (unsigned int xchg1, unsigned int xchg2)
050dfa73
MM
5695{
5696 union i386_op temp_op;
40fb9820 5697 i386_operand_type temp_type;
c48dadc9 5698 unsigned int temp_flags;
050dfa73 5699 enum bfd_reloc_code_real temp_reloc;
4eed87de 5700
050dfa73
MM
5701 temp_type = i.types[xchg2];
5702 i.types[xchg2] = i.types[xchg1];
5703 i.types[xchg1] = temp_type;
c48dadc9
JB
5704
5705 temp_flags = i.flags[xchg2];
5706 i.flags[xchg2] = i.flags[xchg1];
5707 i.flags[xchg1] = temp_flags;
5708
050dfa73
MM
5709 temp_op = i.op[xchg2];
5710 i.op[xchg2] = i.op[xchg1];
5711 i.op[xchg1] = temp_op;
c48dadc9 5712
050dfa73
MM
5713 temp_reloc = i.reloc[xchg2];
5714 i.reloc[xchg2] = i.reloc[xchg1];
5715 i.reloc[xchg1] = temp_reloc;
43234a1e 5716
6225c532 5717 if (i.mask.reg)
43234a1e 5718 {
6225c532
JB
5719 if (i.mask.operand == xchg1)
5720 i.mask.operand = xchg2;
5721 else if (i.mask.operand == xchg2)
5722 i.mask.operand = xchg1;
43234a1e 5723 }
a5748e0d 5724 if (i.broadcast.type || i.broadcast.bytes)
43234a1e 5725 {
5273a3cd
JB
5726 if (i.broadcast.operand == xchg1)
5727 i.broadcast.operand = xchg2;
5728 else if (i.broadcast.operand == xchg2)
5729 i.broadcast.operand = xchg1;
43234a1e 5730 }
050dfa73
MM
5731}
5732
29b0f896 5733static void
e3bb37b5 5734swap_operands (void)
29b0f896 5735{
b7c61d9a 5736 switch (i.operands)
050dfa73 5737 {
c0f3af97 5738 case 5:
b7c61d9a 5739 case 4:
4d456e3d 5740 swap_2_operands (1, i.operands - 2);
1a0670f3 5741 /* Fall through. */
b7c61d9a
L
5742 case 3:
5743 case 2:
4d456e3d 5744 swap_2_operands (0, i.operands - 1);
b7c61d9a
L
5745 break;
5746 default:
5747 abort ();
29b0f896 5748 }
29b0f896
AM
5749
5750 if (i.mem_operands == 2)
5751 {
5e042380 5752 const reg_entry *temp_seg;
29b0f896
AM
5753 temp_seg = i.seg[0];
5754 i.seg[0] = i.seg[1];
5755 i.seg[1] = temp_seg;
5756 }
5757}
252b5132 5758
29b0f896
AM
5759/* Try to ensure constant immediates are represented in the smallest
5760 opcode possible. */
5761static void
e3bb37b5 5762optimize_imm (void)
29b0f896
AM
5763{
5764 char guess_suffix = 0;
5765 int op;
252b5132 5766
29b0f896
AM
5767 if (i.suffix)
5768 guess_suffix = i.suffix;
5769 else if (i.reg_operands)
5770 {
5771 /* Figure out a suffix from the last register operand specified.
75e5731b
JB
5772 We can't do this properly yet, i.e. excluding special register
5773 instances, but the following works for instructions with
5774 immediates. In any case, we can't set i.suffix yet. */
29b0f896 5775 for (op = i.operands; --op >= 0;)
bab6aec1
JB
5776 if (i.types[op].bitfield.class != Reg)
5777 continue;
5778 else if (i.types[op].bitfield.byte)
7ab9ffdd 5779 {
40fb9820
L
5780 guess_suffix = BYTE_MNEM_SUFFIX;
5781 break;
5782 }
bab6aec1 5783 else if (i.types[op].bitfield.word)
252b5132 5784 {
40fb9820
L
5785 guess_suffix = WORD_MNEM_SUFFIX;
5786 break;
5787 }
bab6aec1 5788 else if (i.types[op].bitfield.dword)
40fb9820
L
5789 {
5790 guess_suffix = LONG_MNEM_SUFFIX;
5791 break;
5792 }
bab6aec1 5793 else if (i.types[op].bitfield.qword)
40fb9820
L
5794 {
5795 guess_suffix = QWORD_MNEM_SUFFIX;
29b0f896 5796 break;
252b5132 5797 }
29b0f896
AM
5798 }
5799 else if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
5800 guess_suffix = WORD_MNEM_SUFFIX;
5801
5802 for (op = i.operands; --op >= 0;)
40fb9820 5803 if (operand_type_check (i.types[op], imm))
29b0f896
AM
5804 {
5805 switch (i.op[op].imms->X_op)
252b5132 5806 {
29b0f896
AM
5807 case O_constant:
5808 /* If a suffix is given, this operand may be shortened. */
5809 switch (guess_suffix)
252b5132 5810 {
29b0f896 5811 case LONG_MNEM_SUFFIX:
40fb9820
L
5812 i.types[op].bitfield.imm32 = 1;
5813 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
5814 break;
5815 case WORD_MNEM_SUFFIX:
40fb9820
L
5816 i.types[op].bitfield.imm16 = 1;
5817 i.types[op].bitfield.imm32 = 1;
5818 i.types[op].bitfield.imm32s = 1;
5819 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
5820 break;
5821 case BYTE_MNEM_SUFFIX:
40fb9820
L
5822 i.types[op].bitfield.imm8 = 1;
5823 i.types[op].bitfield.imm8s = 1;
5824 i.types[op].bitfield.imm16 = 1;
5825 i.types[op].bitfield.imm32 = 1;
5826 i.types[op].bitfield.imm32s = 1;
5827 i.types[op].bitfield.imm64 = 1;
29b0f896 5828 break;
252b5132 5829 }
252b5132 5830
29b0f896
AM
5831 /* If this operand is at most 16 bits, convert it
5832 to a signed 16 bit number before trying to see
5833 whether it will fit in an even smaller size.
5834 This allows a 16-bit operand such as $0xffe0 to
5835 be recognised as within Imm8S range. */
40fb9820 5836 if ((i.types[op].bitfield.imm16)
7e96fb68 5837 && fits_in_unsigned_word (i.op[op].imms->X_add_number))
252b5132 5838 {
87ed972d
JB
5839 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
5840 ^ 0x8000) - 0x8000);
29b0f896 5841 }
a28def75
L
5842#ifdef BFD64
5843 /* Store 32-bit immediate in 64-bit for 64-bit BFD. */
40fb9820 5844 if ((i.types[op].bitfield.imm32)
7e96fb68 5845 && fits_in_unsigned_long (i.op[op].imms->X_add_number))
29b0f896
AM
5846 {
5847 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
5848 ^ ((offsetT) 1 << 31))
5849 - ((offsetT) 1 << 31));
5850 }
a28def75 5851#endif
40fb9820 5852 i.types[op]
c6fb90c8
L
5853 = operand_type_or (i.types[op],
5854 smallest_imm_type (i.op[op].imms->X_add_number));
252b5132 5855
29b0f896
AM
5856 /* We must avoid matching of Imm32 templates when 64bit
5857 only immediate is available. */
5858 if (guess_suffix == QWORD_MNEM_SUFFIX)
40fb9820 5859 i.types[op].bitfield.imm32 = 0;
29b0f896 5860 break;
252b5132 5861
29b0f896
AM
5862 case O_absent:
5863 case O_register:
5864 abort ();
5865
5866 /* Symbols and expressions. */
5867 default:
9cd96992
JB
5868 /* Convert symbolic operand to proper sizes for matching, but don't
5869 prevent matching a set of insns that only supports sizes other
5870 than those matching the insn suffix. */
5871 {
40fb9820 5872 i386_operand_type mask, allowed;
87ed972d 5873 const insn_template *t = current_templates->start;
9cd96992 5874
0dfbf9d7 5875 operand_type_set (&mask, 0);
9cd96992
JB
5876 switch (guess_suffix)
5877 {
5878 case QWORD_MNEM_SUFFIX:
40fb9820
L
5879 mask.bitfield.imm64 = 1;
5880 mask.bitfield.imm32s = 1;
9cd96992
JB
5881 break;
5882 case LONG_MNEM_SUFFIX:
40fb9820 5883 mask.bitfield.imm32 = 1;
9cd96992
JB
5884 break;
5885 case WORD_MNEM_SUFFIX:
40fb9820 5886 mask.bitfield.imm16 = 1;
9cd96992
JB
5887 break;
5888 case BYTE_MNEM_SUFFIX:
40fb9820 5889 mask.bitfield.imm8 = 1;
9cd96992
JB
5890 break;
5891 default:
9cd96992
JB
5892 break;
5893 }
8f0212ac
JB
5894
5895 allowed = operand_type_and (t->operand_types[op], mask);
5896 while (++t < current_templates->end)
5897 {
5898 allowed = operand_type_or (allowed, t->operand_types[op]);
5899 allowed = operand_type_and (allowed, mask);
5900 }
5901
0dfbf9d7 5902 if (!operand_type_all_zero (&allowed))
c6fb90c8 5903 i.types[op] = operand_type_and (i.types[op], mask);
9cd96992 5904 }
29b0f896 5905 break;
252b5132 5906 }
29b0f896
AM
5907 }
5908}
47926f60 5909
29b0f896
AM
5910/* Try to use the smallest displacement type too. */
5911static void
e3bb37b5 5912optimize_disp (void)
29b0f896
AM
5913{
5914 int op;
3e73aa7c 5915
29b0f896 5916 for (op = i.operands; --op >= 0;)
40fb9820 5917 if (operand_type_check (i.types[op], disp))
252b5132 5918 {
b300c311 5919 if (i.op[op].disps->X_op == O_constant)
252b5132 5920 {
91d6fa6a 5921 offsetT op_disp = i.op[op].disps->X_add_number;
29b0f896 5922
91d6fa6a 5923 if (!op_disp && i.types[op].bitfield.baseindex)
b300c311 5924 {
2f2be86b
JB
5925 i.types[op] = operand_type_and_not (i.types[op], anydisp);
5926 i.op[op].disps = NULL;
b300c311 5927 i.disp_operands--;
f185acdd
JB
5928 continue;
5929 }
5930
5931 if (i.types[op].bitfield.disp16
cd613c1f 5932 && fits_in_unsigned_word (op_disp))
f185acdd
JB
5933 {
5934 /* If this operand is at most 16 bits, convert
5935 to a signed 16 bit number and don't use 64bit
5936 displacement. */
5937 op_disp = ((op_disp ^ 0x8000) - 0x8000);
5938 i.types[op].bitfield.disp64 = 0;
b300c311 5939 }
f185acdd 5940
28a167a4 5941#ifdef BFD64
a50187b2 5942 /* Optimize 64-bit displacement to 32-bit for 64-bit BFD. */
a775efc8
JB
5943 if ((flag_code != CODE_64BIT
5944 ? i.types[op].bitfield.disp32
5945 : want_disp32 (current_templates->start)
5946 && (!current_templates->start->opcode_modifier.jump
5947 || i.jumpabsolute || i.types[op].bitfield.baseindex))
a50187b2 5948 && fits_in_unsigned_long (op_disp))
b300c311 5949 {
a50187b2
JB
5950 /* If this operand is at most 32 bits, convert
5951 to a signed 32 bit number and don't use 64bit
5952 displacement. */
5953 op_disp = (op_disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
5954 i.types[op].bitfield.disp64 = 0;
5955 i.types[op].bitfield.disp32 = 1;
5956 }
28a167a4 5957
a50187b2
JB
5958 if (flag_code == CODE_64BIT && fits_in_signed_long (op_disp))
5959 {
5960 i.types[op].bitfield.disp64 = 0;
a775efc8 5961 i.types[op].bitfield.disp32 = 1;
b300c311 5962 }
28a167a4 5963#endif
40fb9820 5964 if ((i.types[op].bitfield.disp32
40fb9820 5965 || i.types[op].bitfield.disp16)
b5014f7a 5966 && fits_in_disp8 (op_disp))
40fb9820 5967 i.types[op].bitfield.disp8 = 1;
77c59789
JB
5968
5969 i.op[op].disps->X_add_number = op_disp;
252b5132 5970 }
67a4f2b7
AO
5971 else if (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
5972 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL)
5973 {
5974 fix_new_exp (frag_now, frag_more (0) - frag_now->fr_literal, 0,
5975 i.op[op].disps, 0, i.reloc[op]);
2f2be86b 5976 i.types[op] = operand_type_and_not (i.types[op], anydisp);
67a4f2b7
AO
5977 }
5978 else
b300c311 5979 /* We only support 64bit displacement on constants. */
40fb9820 5980 i.types[op].bitfield.disp64 = 0;
252b5132 5981 }
29b0f896
AM
5982}
5983
4a1b91ea
L
5984/* Return 1 if there is a match in broadcast bytes between operand
5985 GIVEN and instruction template T. */
5986
5987static INLINE int
5988match_broadcast_size (const insn_template *t, unsigned int given)
5989{
5990 return ((t->opcode_modifier.broadcast == BYTE_BROADCAST
5991 && i.types[given].bitfield.byte)
5992 || (t->opcode_modifier.broadcast == WORD_BROADCAST
5993 && i.types[given].bitfield.word)
5994 || (t->opcode_modifier.broadcast == DWORD_BROADCAST
5995 && i.types[given].bitfield.dword)
5996 || (t->opcode_modifier.broadcast == QWORD_BROADCAST
5997 && i.types[given].bitfield.qword));
5998}
5999
6c30d220
L
6000/* Check if operands are valid for the instruction. */
6001
6002static int
6003check_VecOperands (const insn_template *t)
6004{
43234a1e 6005 unsigned int op;
e2195274 6006 i386_cpu_flags cpu;
e2195274
JB
6007
6008 /* Templates allowing for ZMMword as well as YMMword and/or XMMword for
6009 any one operand are implicity requiring AVX512VL support if the actual
6010 operand size is YMMword or XMMword. Since this function runs after
6011 template matching, there's no need to check for YMMword/XMMword in
6012 the template. */
6013 cpu = cpu_flags_and (t->cpu_flags, avx512);
6014 if (!cpu_flags_all_zero (&cpu)
6015 && !t->cpu_flags.bitfield.cpuavx512vl
6016 && !cpu_arch_flags.bitfield.cpuavx512vl)
6017 {
6018 for (op = 0; op < t->operands; ++op)
6019 {
6020 if (t->operand_types[op].bitfield.zmmword
6021 && (i.types[op].bitfield.ymmword
6022 || i.types[op].bitfield.xmmword))
6023 {
6024 i.error = unsupported;
6025 return 1;
6026 }
6027 }
6028 }
43234a1e 6029
22c36940
JB
6030 /* Somewhat similarly, templates specifying both AVX and AVX2 are
6031 requiring AVX2 support if the actual operand size is YMMword. */
6032 if (t->cpu_flags.bitfield.cpuavx
6033 && t->cpu_flags.bitfield.cpuavx2
6034 && !cpu_arch_flags.bitfield.cpuavx2)
6035 {
6036 for (op = 0; op < t->operands; ++op)
6037 {
6038 if (t->operand_types[op].bitfield.xmmword
6039 && i.types[op].bitfield.ymmword)
6040 {
6041 i.error = unsupported;
6042 return 1;
6043 }
6044 }
6045 }
6046
6c30d220 6047 /* Without VSIB byte, we can't have a vector register for index. */
63112cd6 6048 if (!t->opcode_modifier.sib
6c30d220 6049 && i.index_reg
1b54b8d7
JB
6050 && (i.index_reg->reg_type.bitfield.xmmword
6051 || i.index_reg->reg_type.bitfield.ymmword
6052 || i.index_reg->reg_type.bitfield.zmmword))
6c30d220
L
6053 {
6054 i.error = unsupported_vector_index_register;
6055 return 1;
6056 }
6057
ad8ecc81 6058 /* Check if default mask is allowed. */
255571cd 6059 if (t->opcode_modifier.operandconstraint == NO_DEFAULT_MASK
6225c532 6060 && (!i.mask.reg || i.mask.reg->reg_num == 0))
ad8ecc81
MZ
6061 {
6062 i.error = no_default_mask;
6063 return 1;
6064 }
6065
7bab8ab5
JB
6066 /* For VSIB byte, we need a vector register for index, and all vector
6067 registers must be distinct. */
260cd341 6068 if (t->opcode_modifier.sib && t->opcode_modifier.sib != SIBMEM)
7bab8ab5
JB
6069 {
6070 if (!i.index_reg
63112cd6 6071 || !((t->opcode_modifier.sib == VECSIB128
1b54b8d7 6072 && i.index_reg->reg_type.bitfield.xmmword)
63112cd6 6073 || (t->opcode_modifier.sib == VECSIB256
1b54b8d7 6074 && i.index_reg->reg_type.bitfield.ymmword)
63112cd6 6075 || (t->opcode_modifier.sib == VECSIB512
1b54b8d7 6076 && i.index_reg->reg_type.bitfield.zmmword)))
7bab8ab5
JB
6077 {
6078 i.error = invalid_vsib_address;
6079 return 1;
6080 }
6081
6225c532
JB
6082 gas_assert (i.reg_operands == 2 || i.mask.reg);
6083 if (i.reg_operands == 2 && !i.mask.reg)
43234a1e 6084 {
3528c362 6085 gas_assert (i.types[0].bitfield.class == RegSIMD);
1b54b8d7
JB
6086 gas_assert (i.types[0].bitfield.xmmword
6087 || i.types[0].bitfield.ymmword);
3528c362 6088 gas_assert (i.types[2].bitfield.class == RegSIMD);
1b54b8d7
JB
6089 gas_assert (i.types[2].bitfield.xmmword
6090 || i.types[2].bitfield.ymmword);
43234a1e
L
6091 if (operand_check == check_none)
6092 return 0;
6093 if (register_number (i.op[0].regs)
6094 != register_number (i.index_reg)
6095 && register_number (i.op[2].regs)
6096 != register_number (i.index_reg)
6097 && register_number (i.op[0].regs)
6098 != register_number (i.op[2].regs))
6099 return 0;
6100 if (operand_check == check_error)
6101 {
6102 i.error = invalid_vector_register_set;
6103 return 1;
6104 }
6105 as_warn (_("mask, index, and destination registers should be distinct"));
6106 }
6225c532 6107 else if (i.reg_operands == 1 && i.mask.reg)
8444f82a 6108 {
3528c362 6109 if (i.types[1].bitfield.class == RegSIMD
1b54b8d7
JB
6110 && (i.types[1].bitfield.xmmword
6111 || i.types[1].bitfield.ymmword
6112 || i.types[1].bitfield.zmmword)
8444f82a
MZ
6113 && (register_number (i.op[1].regs)
6114 == register_number (i.index_reg)))
6115 {
6116 if (operand_check == check_error)
6117 {
6118 i.error = invalid_vector_register_set;
6119 return 1;
6120 }
6121 if (operand_check != check_none)
6122 as_warn (_("index and destination registers should be distinct"));
6123 }
6124 }
43234a1e 6125 }
7bab8ab5 6126
fc141319
L
6127 /* For AMX instructions with 3 TMM register operands, all operands
6128 must be distinct. */
6129 if (i.reg_operands == 3
6130 && t->operand_types[0].bitfield.tmmword
6131 && (i.op[0].regs == i.op[1].regs
6132 || i.op[0].regs == i.op[2].regs
6133 || i.op[1].regs == i.op[2].regs))
6134 {
6135 i.error = invalid_tmm_register_set;
6136 return 1;
260cd341
LC
6137 }
6138
0cc78721
CL
6139 /* For some special instructions require that destination must be distinct
6140 from source registers. */
255571cd 6141 if (t->opcode_modifier.operandconstraint == DISTINCT_DEST)
0cc78721
CL
6142 {
6143 unsigned int dest_reg = i.operands - 1;
6144
6145 know (i.operands >= 3);
6146
6147 /* #UD if dest_reg == src1_reg or dest_reg == src2_reg. */
6148 if (i.op[dest_reg - 1].regs == i.op[dest_reg].regs
6149 || (i.reg_operands > 2
6150 && i.op[dest_reg - 2].regs == i.op[dest_reg].regs))
6151 {
6152 i.error = invalid_dest_and_src_register_set;
6153 return 1;
6154 }
6155 }
6156
43234a1e
L
6157 /* Check if broadcast is supported by the instruction and is applied
6158 to the memory operand. */
a5748e0d 6159 if (i.broadcast.type || i.broadcast.bytes)
43234a1e 6160 {
8e6e0792 6161 i386_operand_type type, overlap;
43234a1e
L
6162
6163 /* Check if specified broadcast is supported in this instruction,
4a1b91ea 6164 and its broadcast bytes match the memory operand. */
5273a3cd 6165 op = i.broadcast.operand;
8e6e0792 6166 if (!t->opcode_modifier.broadcast
c48dadc9 6167 || !(i.flags[op] & Operand_Mem)
c39e5b26 6168 || (!i.types[op].bitfield.unspecified
4a1b91ea 6169 && !match_broadcast_size (t, op)))
43234a1e
L
6170 {
6171 bad_broadcast:
6172 i.error = unsupported_broadcast;
6173 return 1;
6174 }
8e6e0792 6175
a5748e0d
JB
6176 if (i.broadcast.type)
6177 i.broadcast.bytes = ((1 << (t->opcode_modifier.broadcast - 1))
6178 * i.broadcast.type);
8e6e0792 6179 operand_type_set (&type, 0);
a5748e0d 6180 switch (get_broadcast_bytes (t, false))
8e6e0792 6181 {
4a1b91ea
L
6182 case 2:
6183 type.bitfield.word = 1;
6184 break;
6185 case 4:
6186 type.bitfield.dword = 1;
6187 break;
8e6e0792
JB
6188 case 8:
6189 type.bitfield.qword = 1;
6190 break;
6191 case 16:
6192 type.bitfield.xmmword = 1;
6193 break;
6194 case 32:
6195 type.bitfield.ymmword = 1;
6196 break;
6197 case 64:
6198 type.bitfield.zmmword = 1;
6199 break;
6200 default:
6201 goto bad_broadcast;
6202 }
6203
6204 overlap = operand_type_and (type, t->operand_types[op]);
bc49bfd8
JB
6205 if (t->operand_types[op].bitfield.class == RegSIMD
6206 && t->operand_types[op].bitfield.byte
6207 + t->operand_types[op].bitfield.word
6208 + t->operand_types[op].bitfield.dword
6209 + t->operand_types[op].bitfield.qword > 1)
6210 {
6211 overlap.bitfield.xmmword = 0;
6212 overlap.bitfield.ymmword = 0;
6213 overlap.bitfield.zmmword = 0;
6214 }
8e6e0792
JB
6215 if (operand_type_all_zero (&overlap))
6216 goto bad_broadcast;
6217
6218 if (t->opcode_modifier.checkregsize)
6219 {
6220 unsigned int j;
6221
e2195274 6222 type.bitfield.baseindex = 1;
8e6e0792
JB
6223 for (j = 0; j < i.operands; ++j)
6224 {
6225 if (j != op
6226 && !operand_type_register_match(i.types[j],
6227 t->operand_types[j],
6228 type,
6229 t->operand_types[op]))
6230 goto bad_broadcast;
6231 }
6232 }
43234a1e
L
6233 }
6234 /* If broadcast is supported in this instruction, we need to check if
6235 operand of one-element size isn't specified without broadcast. */
6236 else if (t->opcode_modifier.broadcast && i.mem_operands)
6237 {
6238 /* Find memory operand. */
6239 for (op = 0; op < i.operands; op++)
8dc0818e 6240 if (i.flags[op] & Operand_Mem)
43234a1e
L
6241 break;
6242 gas_assert (op < i.operands);
6243 /* Check size of the memory operand. */
4a1b91ea 6244 if (match_broadcast_size (t, op))
43234a1e
L
6245 {
6246 i.error = broadcast_needed;
6247 return 1;
6248 }
6249 }
c39e5b26
JB
6250 else
6251 op = MAX_OPERANDS - 1; /* Avoid uninitialized variable warning. */
43234a1e
L
6252
6253 /* Check if requested masking is supported. */
6225c532 6254 if (i.mask.reg)
43234a1e 6255 {
ae2387fe
JB
6256 switch (t->opcode_modifier.masking)
6257 {
6258 case BOTH_MASKING:
6259 break;
6260 case MERGING_MASKING:
6225c532 6261 if (i.mask.zeroing)
ae2387fe
JB
6262 {
6263 case 0:
6264 i.error = unsupported_masking;
6265 return 1;
6266 }
6267 break;
6268 case DYNAMIC_MASKING:
6269 /* Memory destinations allow only merging masking. */
6225c532 6270 if (i.mask.zeroing && i.mem_operands)
ae2387fe
JB
6271 {
6272 /* Find memory operand. */
6273 for (op = 0; op < i.operands; op++)
c48dadc9 6274 if (i.flags[op] & Operand_Mem)
ae2387fe
JB
6275 break;
6276 gas_assert (op < i.operands);
6277 if (op == i.operands - 1)
6278 {
6279 i.error = unsupported_masking;
6280 return 1;
6281 }
6282 }
6283 break;
6284 default:
6285 abort ();
6286 }
43234a1e
L
6287 }
6288
6289 /* Check if masking is applied to dest operand. */
6225c532 6290 if (i.mask.reg && (i.mask.operand != i.operands - 1))
43234a1e
L
6291 {
6292 i.error = mask_not_on_destination;
6293 return 1;
6294 }
6295
43234a1e 6296 /* Check RC/SAE. */
ca5312a2 6297 if (i.rounding.type != rc_none)
43234a1e 6298 {
a80195f1 6299 if (!t->opcode_modifier.sae
cf665fee
JB
6300 || ((i.rounding.type != saeonly) != t->opcode_modifier.staticrounding)
6301 || i.mem_operands)
43234a1e
L
6302 {
6303 i.error = unsupported_rc_sae;
6304 return 1;
6305 }
cf665fee
JB
6306
6307 /* Non-EVEX.LIG forms need to have a ZMM register as at least one
6308 operand. */
6309 if (t->opcode_modifier.evex != EVEXLIG)
7bab8ab5 6310 {
cf665fee
JB
6311 for (op = 0; op < t->operands; ++op)
6312 if (i.types[op].bitfield.zmmword)
6313 break;
6314 if (op >= t->operands)
6315 {
6316 i.error = operand_size_mismatch;
6317 return 1;
6318 }
7bab8ab5 6319 }
6c30d220
L
6320 }
6321
da4977e0
JB
6322 /* Check the special Imm4 cases; must be the first operand. */
6323 if (t->cpu_flags.bitfield.cpuxop && t->operands == 5)
6324 {
6325 if (i.op[0].imms->X_op != O_constant
6326 || !fits_in_imm4 (i.op[0].imms->X_add_number))
6327 {
6328 i.error = bad_imm4;
6329 return 1;
6330 }
6331
6332 /* Turn off Imm<N> so that update_imm won't complain. */
6333 operand_type_set (&i.types[0], 0);
6334 }
6335
43234a1e 6336 /* Check vector Disp8 operand. */
b5014f7a 6337 if (t->opcode_modifier.disp8memshift
1a42a9fe 6338 && i.disp_encoding <= disp_encoding_8bit)
43234a1e 6339 {
a5748e0d 6340 if (i.broadcast.bytes)
4a1b91ea 6341 i.memshift = t->opcode_modifier.broadcast - 1;
7091c612 6342 else if (t->opcode_modifier.disp8memshift != DISP8_SHIFT_VL)
43234a1e 6343 i.memshift = t->opcode_modifier.disp8memshift;
7091c612
JB
6344 else
6345 {
125ff819 6346 const i386_operand_type *type = NULL, *fallback = NULL;
7091c612
JB
6347
6348 i.memshift = 0;
6349 for (op = 0; op < i.operands; op++)
8dc0818e 6350 if (i.flags[op] & Operand_Mem)
7091c612 6351 {
4174bfff
JB
6352 if (t->opcode_modifier.evex == EVEXLIG)
6353 i.memshift = 2 + (i.suffix == QWORD_MNEM_SUFFIX);
6354 else if (t->operand_types[op].bitfield.xmmword
6355 + t->operand_types[op].bitfield.ymmword
6356 + t->operand_types[op].bitfield.zmmword <= 1)
7091c612
JB
6357 type = &t->operand_types[op];
6358 else if (!i.types[op].bitfield.unspecified)
6359 type = &i.types[op];
125ff819
JB
6360 else /* Ambiguities get resolved elsewhere. */
6361 fallback = &t->operand_types[op];
7091c612 6362 }
3528c362 6363 else if (i.types[op].bitfield.class == RegSIMD
4174bfff 6364 && t->opcode_modifier.evex != EVEXLIG)
7091c612
JB
6365 {
6366 if (i.types[op].bitfield.zmmword)
6367 i.memshift = 6;
6368 else if (i.types[op].bitfield.ymmword && i.memshift < 5)
6369 i.memshift = 5;
6370 else if (i.types[op].bitfield.xmmword && i.memshift < 4)
6371 i.memshift = 4;
6372 }
6373
125ff819
JB
6374 if (!type && !i.memshift)
6375 type = fallback;
7091c612
JB
6376 if (type)
6377 {
6378 if (type->bitfield.zmmword)
6379 i.memshift = 6;
6380 else if (type->bitfield.ymmword)
6381 i.memshift = 5;
6382 else if (type->bitfield.xmmword)
6383 i.memshift = 4;
6384 }
6385
6386 /* For the check in fits_in_disp8(). */
6387 if (i.memshift == 0)
6388 i.memshift = -1;
6389 }
43234a1e
L
6390
6391 for (op = 0; op < i.operands; op++)
6392 if (operand_type_check (i.types[op], disp)
6393 && i.op[op].disps->X_op == O_constant)
6394 {
b5014f7a 6395 if (fits_in_disp8 (i.op[op].disps->X_add_number))
43234a1e 6396 {
b5014f7a
JB
6397 i.types[op].bitfield.disp8 = 1;
6398 return 0;
43234a1e 6399 }
b5014f7a 6400 i.types[op].bitfield.disp8 = 0;
43234a1e
L
6401 }
6402 }
b5014f7a
JB
6403
6404 i.memshift = 0;
43234a1e 6405
6c30d220
L
6406 return 0;
6407}
6408
da4977e0 6409/* Check if encoding requirements are met by the instruction. */
a683cc34
SP
6410
6411static int
da4977e0 6412VEX_check_encoding (const insn_template *t)
a683cc34 6413{
da4977e0
JB
6414 if (i.vec_encoding == vex_encoding_error)
6415 {
6416 i.error = unsupported;
6417 return 1;
6418 }
6419
86fa6981 6420 if (i.vec_encoding == vex_encoding_evex)
43234a1e 6421 {
86fa6981 6422 /* This instruction must be encoded with EVEX prefix. */
e771e7c9 6423 if (!is_evex_encoding (t))
86fa6981
L
6424 {
6425 i.error = unsupported;
6426 return 1;
6427 }
6428 return 0;
43234a1e
L
6429 }
6430
a683cc34 6431 if (!t->opcode_modifier.vex)
86fa6981
L
6432 {
6433 /* This instruction template doesn't have VEX prefix. */
6434 if (i.vec_encoding != vex_encoding_default)
6435 {
6436 i.error = unsupported;
6437 return 1;
6438 }
6439 return 0;
6440 }
a683cc34 6441
a683cc34
SP
6442 return 0;
6443}
6444
7b94647a
JB
6445/* Helper function for the progress() macro in match_template(). */
6446static INLINE enum i386_error progress (enum i386_error new,
6447 enum i386_error last,
6448 unsigned int line, unsigned int *line_p)
6449{
6450 if (line <= *line_p)
6451 return last;
6452 *line_p = line;
6453 return new;
6454}
6455
d3ce72d0 6456static const insn_template *
83b16ac6 6457match_template (char mnem_suffix)
29b0f896
AM
6458{
6459 /* Points to template once we've found it. */
d3ce72d0 6460 const insn_template *t;
40fb9820 6461 i386_operand_type overlap0, overlap1, overlap2, overlap3;
c0f3af97 6462 i386_operand_type overlap4;
29b0f896 6463 unsigned int found_reverse_match;
dc2be329 6464 i386_opcode_modifier suffix_check;
40fb9820 6465 i386_operand_type operand_types [MAX_OPERANDS];
539e75ad 6466 int addr_prefix_disp;
7b94647a
JB
6467 unsigned int j, size_match, check_register, errline = __LINE__;
6468 enum i386_error specific_error = number_of_operands_mismatch;
6469#define progress(err) progress (err, specific_error, __LINE__, &errline)
29b0f896 6470
c0f3af97
L
6471#if MAX_OPERANDS != 5
6472# error "MAX_OPERANDS must be 5."
f48ff2ae
L
6473#endif
6474
29b0f896 6475 found_reverse_match = 0;
539e75ad 6476 addr_prefix_disp = -1;
40fb9820 6477
dc2be329 6478 /* Prepare for mnemonic suffix check. */
40fb9820 6479 memset (&suffix_check, 0, sizeof (suffix_check));
dc2be329
L
6480 switch (mnem_suffix)
6481 {
6482 case BYTE_MNEM_SUFFIX:
6483 suffix_check.no_bsuf = 1;
6484 break;
6485 case WORD_MNEM_SUFFIX:
6486 suffix_check.no_wsuf = 1;
6487 break;
6488 case SHORT_MNEM_SUFFIX:
6489 suffix_check.no_ssuf = 1;
6490 break;
6491 case LONG_MNEM_SUFFIX:
6492 suffix_check.no_lsuf = 1;
6493 break;
6494 case QWORD_MNEM_SUFFIX:
6495 suffix_check.no_qsuf = 1;
6496 break;
6497 default:
6498 /* NB: In Intel syntax, normally we can check for memory operand
6499 size when there is no mnemonic suffix. But jmp and call have
6500 2 different encodings with Dword memory operand size, one with
6501 No_ldSuf and the other without. i.suffix is set to
6502 LONG_DOUBLE_MNEM_SUFFIX to skip the one with No_ldSuf. */
6503 if (i.suffix == LONG_DOUBLE_MNEM_SUFFIX)
6504 suffix_check.no_ldsuf = 1;
83b16ac6
JB
6505 }
6506
45aa61fe 6507 for (t = current_templates->start; t < current_templates->end; t++)
29b0f896 6508 {
539e75ad 6509 addr_prefix_disp = -1;
dbbc8b7e 6510 found_reverse_match = 0;
539e75ad 6511
7b94647a 6512 /* Must have right number of operands. */
29b0f896
AM
6513 if (i.operands != t->operands)
6514 continue;
6515
50aecf8c 6516 /* Check processor support. */
7b94647a 6517 specific_error = progress (unsupported);
45a4bb20 6518 if (cpu_flags_match (t) != CPU_FLAGS_PERFECT_MATCH)
50aecf8c
L
6519 continue;
6520
e1d4d893 6521 /* Check AT&T mnemonic. */
7b94647a 6522 specific_error = progress (unsupported_with_intel_mnemonic);
e1d4d893 6523 if (intel_mnemonic && t->opcode_modifier.attmnemonic)
1efbbeb4
L
6524 continue;
6525
4b5aaf5f 6526 /* Check AT&T/Intel syntax. */
7b94647a 6527 specific_error = progress (unsupported_syntax);
5c07affc 6528 if ((intel_syntax && t->opcode_modifier.attsyntax)
4b5aaf5f 6529 || (!intel_syntax && t->opcode_modifier.intelsyntax))
1efbbeb4
L
6530 continue;
6531
4b5aaf5f
L
6532 /* Check Intel64/AMD64 ISA. */
6533 switch (isa64)
6534 {
6535 default:
6536 /* Default: Don't accept Intel64. */
6537 if (t->opcode_modifier.isa64 == INTEL64)
6538 continue;
6539 break;
6540 case amd64:
6541 /* -mamd64: Don't accept Intel64 and Intel64 only. */
6542 if (t->opcode_modifier.isa64 >= INTEL64)
6543 continue;
6544 break;
6545 case intel64:
6546 /* -mintel64: Don't accept AMD64. */
5990e377 6547 if (t->opcode_modifier.isa64 == AMD64 && flag_code == CODE_64BIT)
4b5aaf5f
L
6548 continue;
6549 break;
6550 }
6551
dc2be329 6552 /* Check the suffix. */
7b94647a 6553 specific_error = progress (invalid_instruction_suffix);
dc2be329
L
6554 if ((t->opcode_modifier.no_bsuf && suffix_check.no_bsuf)
6555 || (t->opcode_modifier.no_wsuf && suffix_check.no_wsuf)
6556 || (t->opcode_modifier.no_lsuf && suffix_check.no_lsuf)
6557 || (t->opcode_modifier.no_ssuf && suffix_check.no_ssuf)
6558 || (t->opcode_modifier.no_qsuf && suffix_check.no_qsuf)
6559 || (t->opcode_modifier.no_ldsuf && suffix_check.no_ldsuf))
83b16ac6 6560 continue;
29b0f896 6561
7b94647a 6562 specific_error = progress (operand_size_mismatch);
3ac21baa
JB
6563 size_match = operand_size_match (t);
6564 if (!size_match)
7d5e4556 6565 continue;
539e75ad 6566
6f2f06be
JB
6567 /* This is intentionally not
6568
0cfa3eb3 6569 if (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE))
6f2f06be
JB
6570
6571 as the case of a missing * on the operand is accepted (perhaps with
6572 a warning, issued further down). */
7b94647a 6573 specific_error = progress (operand_type_mismatch);
0cfa3eb3 6574 if (i.jumpabsolute && t->opcode_modifier.jump != JUMP_ABSOLUTE)
7b94647a 6575 continue;
6f2f06be 6576
5c07affc
L
6577 for (j = 0; j < MAX_OPERANDS; j++)
6578 operand_types[j] = t->operand_types[j];
6579
e365e234
JB
6580 /* In general, don't allow
6581 - 64-bit operands outside of 64-bit mode,
6582 - 32-bit operands on pre-386. */
7b94647a
JB
6583 specific_error = progress (mnem_suffix ? invalid_instruction_suffix
6584 : operand_size_mismatch);
4873e243 6585 j = i.imm_operands + (t->operands > i.imm_operands + 1);
e365e234
JB
6586 if (((i.suffix == QWORD_MNEM_SUFFIX
6587 && flag_code != CODE_64BIT
389d00a5
JB
6588 && !(t->opcode_modifier.opcodespace == SPACE_0F
6589 && t->base_opcode == 0xc7
5e74b495 6590 && t->opcode_modifier.opcodeprefix == PREFIX_NONE
8b65b895 6591 && t->extension_opcode == 1) /* cmpxchg8b */)
e365e234
JB
6592 || (i.suffix == LONG_MNEM_SUFFIX
6593 && !cpu_arch_flags.bitfield.cpui386))
45aa61fe 6594 && (intel_syntax
3cd7f3e3 6595 ? (t->opcode_modifier.mnemonicsize != IGNORESIZE
45aa61fe
AM
6596 && !intel_float_operand (t->name))
6597 : intel_float_operand (t->name) != 2)
4873e243
JB
6598 && (t->operands == i.imm_operands
6599 || (operand_types[i.imm_operands].bitfield.class != RegMMX
6600 && operand_types[i.imm_operands].bitfield.class != RegSIMD
6601 && operand_types[i.imm_operands].bitfield.class != RegMask)
6602 || (operand_types[j].bitfield.class != RegMMX
6603 && operand_types[j].bitfield.class != RegSIMD
6604 && operand_types[j].bitfield.class != RegMask))
63112cd6 6605 && !t->opcode_modifier.sib)
192dc9c6
JB
6606 continue;
6607
29b0f896 6608 /* Do not verify operands when there are none. */
e365e234 6609 if (!t->operands)
da4977e0
JB
6610 {
6611 if (VEX_check_encoding (t))
6612 {
7b94647a 6613 specific_error = progress (i.error);
da4977e0
JB
6614 continue;
6615 }
6616
6617 /* We've found a match; break out of loop. */
6618 break;
6619 }
252b5132 6620
48bcea9f
JB
6621 if (!t->opcode_modifier.jump
6622 || t->opcode_modifier.jump == JUMP_ABSOLUTE)
6623 {
6624 /* There should be only one Disp operand. */
6625 for (j = 0; j < MAX_OPERANDS; j++)
6626 if (operand_type_check (operand_types[j], disp))
539e75ad 6627 break;
48bcea9f
JB
6628 if (j < MAX_OPERANDS)
6629 {
5b7c81bd 6630 bool override = (i.prefix[ADDR_PREFIX] != 0);
48bcea9f
JB
6631
6632 addr_prefix_disp = j;
6633
a775efc8
JB
6634 /* Address size prefix will turn Disp64 operand into Disp32 and
6635 Disp32/Disp16 one into Disp16/Disp32 respectively. */
48bcea9f 6636 switch (flag_code)
40fb9820 6637 {
48bcea9f
JB
6638 case CODE_16BIT:
6639 override = !override;
6640 /* Fall through. */
6641 case CODE_32BIT:
6642 if (operand_types[j].bitfield.disp32
6643 && operand_types[j].bitfield.disp16)
40fb9820 6644 {
48bcea9f
JB
6645 operand_types[j].bitfield.disp16 = override;
6646 operand_types[j].bitfield.disp32 = !override;
40fb9820 6647 }
a775efc8 6648 gas_assert (!operand_types[j].bitfield.disp64);
48bcea9f
JB
6649 break;
6650
6651 case CODE_64BIT:
a775efc8 6652 if (operand_types[j].bitfield.disp64)
40fb9820 6653 {
a775efc8 6654 gas_assert (!operand_types[j].bitfield.disp32);
48bcea9f 6655 operand_types[j].bitfield.disp32 = override;
a775efc8 6656 operand_types[j].bitfield.disp64 = !override;
40fb9820 6657 }
48bcea9f
JB
6658 operand_types[j].bitfield.disp16 = 0;
6659 break;
40fb9820 6660 }
539e75ad 6661 }
48bcea9f 6662 }
539e75ad 6663
d7e3e627
L
6664 switch (i.reloc[0])
6665 {
6666 case BFD_RELOC_386_GOT32:
6667 /* Force 0x8b encoding for "mov foo@GOT, %eax". */
6668 if (t->base_opcode == 0xa0
6669 && t->opcode_modifier.opcodespace == SPACE_BASE)
6670 continue;
6671 break;
6672 case BFD_RELOC_386_TLS_GOTIE:
6673 case BFD_RELOC_386_TLS_LE_32:
6674 case BFD_RELOC_X86_64_GOTTPOFF:
6675 case BFD_RELOC_X86_64_TLSLD:
6676 /* Don't allow KMOV in TLS code sequences. */
6677 if (t->opcode_modifier.vex)
6678 continue;
6679 break;
6680 default:
6681 break;
6682 }
02a86693 6683
56ffb741 6684 /* We check register size if needed. */
e2195274
JB
6685 if (t->opcode_modifier.checkregsize)
6686 {
6687 check_register = (1 << t->operands) - 1;
a5748e0d 6688 if (i.broadcast.type || i.broadcast.bytes)
5273a3cd 6689 check_register &= ~(1 << i.broadcast.operand);
e2195274
JB
6690 }
6691 else
6692 check_register = 0;
6693
c6fb90c8 6694 overlap0 = operand_type_and (i.types[0], operand_types[0]);
29b0f896
AM
6695 switch (t->operands)
6696 {
6697 case 1:
40fb9820 6698 if (!operand_type_match (overlap0, i.types[0]))
29b0f896
AM
6699 continue;
6700 break;
6701 case 2:
33eaf5de 6702 /* xchg %eax, %eax is a special case. It is an alias for nop
8b38ad71
L
6703 only in 32bit mode and we can use opcode 0x90. In 64bit
6704 mode, we can't use 0x90 for xchg %eax, %eax since it should
6705 zero-extend %eax to %rax. */
6706 if (flag_code == CODE_64BIT
6707 && t->base_opcode == 0x90
35648716 6708 && t->opcode_modifier.opcodespace == SPACE_BASE
75e5731b
JB
6709 && i.types[0].bitfield.instance == Accum
6710 && i.types[0].bitfield.dword
6711 && i.types[1].bitfield.instance == Accum
6712 && i.types[1].bitfield.dword)
8b38ad71 6713 continue;
1212781b
JB
6714 /* xrelease mov %eax, <disp> is another special case. It must not
6715 match the accumulator-only encoding of mov. */
6716 if (flag_code != CODE_64BIT
6717 && i.hle_prefix
6718 && t->base_opcode == 0xa0
35648716 6719 && t->opcode_modifier.opcodespace == SPACE_BASE
75e5731b 6720 && i.types[0].bitfield.instance == Accum
8dc0818e 6721 && (i.flags[1] & Operand_Mem))
1212781b 6722 continue;
f5eb1d70
JB
6723 /* Fall through. */
6724
6725 case 3:
3ac21baa
JB
6726 if (!(size_match & MATCH_STRAIGHT))
6727 goto check_reverse;
64c49ab3
JB
6728 /* Reverse direction of operands if swapping is possible in the first
6729 place (operands need to be symmetric) and
6730 - the load form is requested, and the template is a store form,
6731 - the store form is requested, and the template is a load form,
6732 - the non-default (swapped) form is requested. */
6733 overlap1 = operand_type_and (operand_types[0], operand_types[1]);
f5eb1d70 6734 if (t->opcode_modifier.d && i.reg_operands == i.operands
64c49ab3
JB
6735 && !operand_type_all_zero (&overlap1))
6736 switch (i.dir_encoding)
6737 {
6738 case dir_encoding_load:
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_store:
6745 if (!operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 6746 && !t->opcode_modifier.regmem)
64c49ab3
JB
6747 goto check_reverse;
6748 break;
6749
6750 case dir_encoding_swap:
6751 goto check_reverse;
6752
6753 case dir_encoding_default:
6754 break;
6755 }
86fa6981 6756 /* If we want store form, we skip the current load. */
64c49ab3
JB
6757 if ((i.dir_encoding == dir_encoding_store
6758 || i.dir_encoding == dir_encoding_swap)
86fa6981
L
6759 && i.mem_operands == 0
6760 && t->opcode_modifier.load)
fa99fab2 6761 continue;
1a0670f3 6762 /* Fall through. */
f48ff2ae 6763 case 4:
c0f3af97 6764 case 5:
c6fb90c8 6765 overlap1 = operand_type_and (i.types[1], operand_types[1]);
40fb9820
L
6766 if (!operand_type_match (overlap0, i.types[0])
6767 || !operand_type_match (overlap1, i.types[1])
e2195274 6768 || ((check_register & 3) == 3
dc821c5f 6769 && !operand_type_register_match (i.types[0],
40fb9820 6770 operand_types[0],
dc821c5f 6771 i.types[1],
40fb9820 6772 operand_types[1])))
29b0f896 6773 {
7b94647a
JB
6774 specific_error = progress (i.error);
6775
29b0f896 6776 /* Check if other direction is valid ... */
38e314eb 6777 if (!t->opcode_modifier.d)
29b0f896
AM
6778 continue;
6779
dc1e8a47 6780 check_reverse:
3ac21baa
JB
6781 if (!(size_match & MATCH_REVERSE))
6782 continue;
29b0f896 6783 /* Try reversing direction of operands. */
8bd915b7
JB
6784 j = t->opcode_modifier.vexsources ? 1 : i.operands - 1;
6785 overlap0 = operand_type_and (i.types[0], operand_types[j]);
6786 overlap1 = operand_type_and (i.types[j], operand_types[0]);
c975cec5
JB
6787 overlap2 = operand_type_and (i.types[1], operand_types[1]);
6788 gas_assert (t->operands != 3 || !check_register);
40fb9820 6789 if (!operand_type_match (overlap0, i.types[0])
8bd915b7 6790 || !operand_type_match (overlap1, i.types[j])
c975cec5
JB
6791 || (t->operands == 3
6792 && !operand_type_match (overlap2, i.types[1]))
45664ddb 6793 || (check_register
dc821c5f 6794 && !operand_type_register_match (i.types[0],
8bd915b7
JB
6795 operand_types[j],
6796 i.types[j],
45664ddb 6797 operand_types[0])))
29b0f896
AM
6798 {
6799 /* Does not match either direction. */
7b94647a 6800 specific_error = progress (i.error);
29b0f896
AM
6801 continue;
6802 }
38e314eb 6803 /* found_reverse_match holds which of D or FloatR
29b0f896 6804 we've found. */
38e314eb
JB
6805 if (!t->opcode_modifier.d)
6806 found_reverse_match = 0;
6807 else if (operand_types[0].bitfield.tbyte)
8a2ed489 6808 found_reverse_match = Opcode_FloatD;
8bd915b7
JB
6809 else if (t->opcode_modifier.vexsources)
6810 {
6811 found_reverse_match = Opcode_VexW;
6812 goto check_operands_345;
6813 }
2c735193
JB
6814 else if (t->opcode_modifier.opcodespace != SPACE_BASE
6815 && (t->opcode_modifier.opcodespace != SPACE_0F
6816 /* MOV to/from CR/DR/TR, as an exception, follow
6817 the base opcode space encoding model. */
6818 || (t->base_opcode | 7) != 0x27))
dbbc8b7e 6819 found_reverse_match = (t->base_opcode & 0xee) != 0x6e
2c735193 6820 ? Opcode_ExtD : Opcode_SIMD_IntD;
8a2ed489 6821 else
38e314eb 6822 found_reverse_match = Opcode_D;
40fb9820 6823 if (t->opcode_modifier.floatr)
8a2ed489 6824 found_reverse_match |= Opcode_FloatR;
29b0f896 6825 }
f48ff2ae 6826 else
29b0f896 6827 {
f48ff2ae 6828 /* Found a forward 2 operand match here. */
8bd915b7 6829 check_operands_345:
d1cbb4db
L
6830 switch (t->operands)
6831 {
c0f3af97 6832 case 5:
3d0738af 6833 overlap4 = operand_type_and (i.types[4], operand_types[4]);
c0f3af97 6834 if (!operand_type_match (overlap4, i.types[4])
dc821c5f 6835 || !operand_type_register_match (i.types[3],
c0f3af97 6836 operand_types[3],
c0f3af97
L
6837 i.types[4],
6838 operand_types[4]))
7b94647a
JB
6839 {
6840 specific_error = progress (i.error);
6841 continue;
6842 }
1a0670f3 6843 /* Fall through. */
f48ff2ae 6844 case 4:
3d0738af 6845 overlap3 = operand_type_and (i.types[3], operand_types[3]);
40fb9820 6846 if (!operand_type_match (overlap3, i.types[3])
e2195274
JB
6847 || ((check_register & 0xa) == 0xa
6848 && !operand_type_register_match (i.types[1],
f7768225
JB
6849 operand_types[1],
6850 i.types[3],
e2195274
JB
6851 operand_types[3]))
6852 || ((check_register & 0xc) == 0xc
6853 && !operand_type_register_match (i.types[2],
6854 operand_types[2],
6855 i.types[3],
6856 operand_types[3])))
7b94647a
JB
6857 {
6858 specific_error = progress (i.error);
6859 continue;
6860 }
1a0670f3 6861 /* Fall through. */
f48ff2ae 6862 case 3:
3d0738af 6863 overlap2 = operand_type_and (i.types[2], operand_types[2]);
40fb9820 6864 if (!operand_type_match (overlap2, i.types[2])
e2195274
JB
6865 || ((check_register & 5) == 5
6866 && !operand_type_register_match (i.types[0],
23e42951
JB
6867 operand_types[0],
6868 i.types[2],
e2195274
JB
6869 operand_types[2]))
6870 || ((check_register & 6) == 6
6871 && !operand_type_register_match (i.types[1],
6872 operand_types[1],
6873 i.types[2],
6874 operand_types[2])))
7b94647a
JB
6875 {
6876 specific_error = progress (i.error);
6877 continue;
6878 }
f48ff2ae
L
6879 break;
6880 }
29b0f896 6881 }
f48ff2ae 6882 /* Found either forward/reverse 2, 3 or 4 operand match here:
29b0f896
AM
6883 slip through to break. */
6884 }
c0f3af97 6885
9bb4d860
L
6886 /* Check if VEX/EVEX encoding requirements can be satisfied. */
6887 if (VEX_check_encoding (t))
da4977e0 6888 {
7b94647a 6889 specific_error = progress (i.error);
da4977e0
JB
6890 continue;
6891 }
6892
9bb4d860
L
6893 /* Check if vector operands are valid. */
6894 if (check_VecOperands (t))
5614d22c 6895 {
7b94647a 6896 specific_error = progress (i.error);
5614d22c
JB
6897 continue;
6898 }
a683cc34 6899
29b0f896
AM
6900 /* We've found a match; break out of loop. */
6901 break;
6902 }
6903
7b94647a
JB
6904#undef progress
6905
29b0f896
AM
6906 if (t == current_templates->end)
6907 {
6908 /* We found no match. */
a65babc9 6909 const char *err_msg;
7b94647a 6910 switch (specific_error)
a65babc9
L
6911 {
6912 default:
6913 abort ();
86e026a4 6914 case operand_size_mismatch:
a65babc9
L
6915 err_msg = _("operand size mismatch");
6916 break;
6917 case operand_type_mismatch:
6918 err_msg = _("operand type mismatch");
6919 break;
6920 case register_type_mismatch:
6921 err_msg = _("register type mismatch");
6922 break;
6923 case number_of_operands_mismatch:
6924 err_msg = _("number of operands mismatch");
6925 break;
6926 case invalid_instruction_suffix:
6927 err_msg = _("invalid instruction suffix");
6928 break;
6929 case bad_imm4:
4a2608e3 6930 err_msg = _("constant doesn't fit in 4 bits");
a65babc9 6931 break;
a65babc9
L
6932 case unsupported_with_intel_mnemonic:
6933 err_msg = _("unsupported with Intel mnemonic");
6934 break;
6935 case unsupported_syntax:
6936 err_msg = _("unsupported syntax");
6937 break;
6938 case unsupported:
35262a23 6939 as_bad (_("unsupported instruction `%s'"),
10efe3f6
L
6940 current_templates->start->name);
6941 return NULL;
260cd341
LC
6942 case invalid_sib_address:
6943 err_msg = _("invalid SIB address");
6944 break;
6c30d220
L
6945 case invalid_vsib_address:
6946 err_msg = _("invalid VSIB address");
6947 break;
7bab8ab5
JB
6948 case invalid_vector_register_set:
6949 err_msg = _("mask, index, and destination registers must be distinct");
6950 break;
260cd341
LC
6951 case invalid_tmm_register_set:
6952 err_msg = _("all tmm registers must be distinct");
6953 break;
0cc78721
CL
6954 case invalid_dest_and_src_register_set:
6955 err_msg = _("destination and source registers must be distinct");
6956 break;
6c30d220
L
6957 case unsupported_vector_index_register:
6958 err_msg = _("unsupported vector index register");
6959 break;
43234a1e
L
6960 case unsupported_broadcast:
6961 err_msg = _("unsupported broadcast");
6962 break;
43234a1e
L
6963 case broadcast_needed:
6964 err_msg = _("broadcast is needed for operand of such type");
6965 break;
6966 case unsupported_masking:
6967 err_msg = _("unsupported masking");
6968 break;
6969 case mask_not_on_destination:
6970 err_msg = _("mask not on destination operand");
6971 break;
6972 case no_default_mask:
6973 err_msg = _("default mask isn't allowed");
6974 break;
6975 case unsupported_rc_sae:
6976 err_msg = _("unsupported static rounding/sae");
6977 break;
43234a1e
L
6978 case invalid_register_operand:
6979 err_msg = _("invalid register operand");
6980 break;
a65babc9
L
6981 }
6982 as_bad (_("%s for `%s'"), err_msg,
891edac4 6983 current_templates->start->name);
fa99fab2 6984 return NULL;
29b0f896 6985 }
252b5132 6986
29b0f896
AM
6987 if (!quiet_warnings)
6988 {
6989 if (!intel_syntax
0cfa3eb3 6990 && (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE)))
6f2f06be 6991 as_warn (_("indirect %s without `*'"), t->name);
29b0f896 6992
40fb9820 6993 if (t->opcode_modifier.isprefix
3cd7f3e3 6994 && t->opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
6995 {
6996 /* Warn them that a data or address size prefix doesn't
6997 affect assembly of the next line of code. */
6998 as_warn (_("stand-alone `%s' prefix"), t->name);
6999 }
7000 }
7001
7002 /* Copy the template we found. */
9a182d04 7003 install_template (t);
539e75ad
L
7004
7005 if (addr_prefix_disp != -1)
7006 i.tm.operand_types[addr_prefix_disp]
7007 = operand_types[addr_prefix_disp];
7008
8bd915b7 7009 switch (found_reverse_match)
29b0f896 7010 {
8bd915b7
JB
7011 case 0:
7012 break;
7013
7014 default:
dfd69174
JB
7015 /* If we found a reverse match we must alter the opcode direction
7016 bit and clear/flip the regmem modifier one. found_reverse_match
7017 holds bits to change (different for int & float insns). */
29b0f896
AM
7018
7019 i.tm.base_opcode ^= found_reverse_match;
7020
f5eb1d70
JB
7021 i.tm.operand_types[0] = operand_types[i.operands - 1];
7022 i.tm.operand_types[i.operands - 1] = operand_types[0];
dfd69174
JB
7023
7024 /* Certain SIMD insns have their load forms specified in the opcode
7025 table, and hence we need to _set_ RegMem instead of clearing it.
7026 We need to avoid setting the bit though on insns like KMOVW. */
7027 i.tm.opcode_modifier.regmem
7028 = i.tm.opcode_modifier.modrm && i.tm.opcode_modifier.d
7029 && i.tm.operands > 2U - i.tm.opcode_modifier.sse2avx
7030 && !i.tm.opcode_modifier.regmem;
8bd915b7
JB
7031 break;
7032
7033 case Opcode_VexW:
7034 /* Only the first two register operands need reversing, alongside
7035 flipping VEX.W. */
7036 i.tm.opcode_modifier.vexw ^= VEXW0 ^ VEXW1;
7037
7038 j = i.tm.operand_types[0].bitfield.imm8;
7039 i.tm.operand_types[j] = operand_types[j + 1];
7040 i.tm.operand_types[j + 1] = operand_types[j];
7041 break;
29b0f896
AM
7042 }
7043
fa99fab2 7044 return t;
29b0f896
AM
7045}
7046
7047static int
e3bb37b5 7048check_string (void)
29b0f896 7049{
51c8edf6
JB
7050 unsigned int es_op = i.tm.opcode_modifier.isstring - IS_STRING_ES_OP0;
7051 unsigned int op = i.tm.operand_types[0].bitfield.baseindex ? es_op : 0;
8dc0818e 7052
5e042380 7053 if (i.seg[op] != NULL && i.seg[op] != reg_es)
29b0f896 7054 {
51c8edf6
JB
7055 as_bad (_("`%s' operand %u must use `%ses' segment"),
7056 i.tm.name,
7057 intel_syntax ? i.tm.operands - es_op : es_op + 1,
7058 register_prefix);
7059 return 0;
29b0f896 7060 }
51c8edf6
JB
7061
7062 /* There's only ever one segment override allowed per instruction.
7063 This instruction possibly has a legal segment override on the
7064 second operand, so copy the segment to where non-string
7065 instructions store it, allowing common code. */
7066 i.seg[op] = i.seg[1];
7067
29b0f896
AM
7068 return 1;
7069}
7070
7071static int
543613e9 7072process_suffix (void)
29b0f896 7073{
5b7c81bd 7074 bool is_crc32 = false, is_movx = false;
8b65b895 7075
29b0f896
AM
7076 /* If matched instruction specifies an explicit instruction mnemonic
7077 suffix, use it. */
673fe0f0 7078 if (i.tm.opcode_modifier.size == SIZE16)
40fb9820 7079 i.suffix = WORD_MNEM_SUFFIX;
673fe0f0 7080 else if (i.tm.opcode_modifier.size == SIZE32)
40fb9820 7081 i.suffix = LONG_MNEM_SUFFIX;
673fe0f0 7082 else if (i.tm.opcode_modifier.size == SIZE64)
40fb9820 7083 i.suffix = QWORD_MNEM_SUFFIX;
13e600d0 7084 else if (i.reg_operands
c8f8eebc 7085 && (i.operands > 1 || i.types[0].bitfield.class == Reg)
255571cd 7086 && i.tm.opcode_modifier.operandconstraint != ADDR_PREFIX_OP_REG)
29b0f896 7087 {
65fca059 7088 unsigned int numop = i.operands;
389d00a5
JB
7089
7090 /* MOVSX/MOVZX */
7091 is_movx = (i.tm.opcode_modifier.opcodespace == SPACE_0F
7092 && (i.tm.base_opcode | 8) == 0xbe)
7093 || (i.tm.opcode_modifier.opcodespace == SPACE_BASE
7094 && i.tm.base_opcode == 0x63
7095 && i.tm.cpu_flags.bitfield.cpu64);
7096
8b65b895 7097 /* CRC32 */
389d00a5
JB
7098 is_crc32 = (i.tm.base_opcode == 0xf0
7099 && i.tm.opcode_modifier.opcodespace == SPACE_0F38
8b65b895 7100 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2);
65fca059
JB
7101
7102 /* movsx/movzx want only their source operand considered here, for the
7103 ambiguity checking below. The suffix will be replaced afterwards
7104 to represent the destination (register). */
389d00a5 7105 if (is_movx && (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63))
65fca059
JB
7106 --i.operands;
7107
643bb870 7108 /* crc32 needs REX.W set regardless of suffix / source operand size. */
8b65b895 7109 if (is_crc32 && i.tm.operand_types[1].bitfield.qword)
643bb870
JB
7110 i.rex |= REX_W;
7111
29b0f896 7112 /* If there's no instruction mnemonic suffix we try to invent one
13e600d0 7113 based on GPR operands. */
29b0f896
AM
7114 if (!i.suffix)
7115 {
7116 /* We take i.suffix from the last register operand specified,
7117 Destination register type is more significant than source
381d071f
L
7118 register type. crc32 in SSE4.2 prefers source register
7119 type. */
8b65b895 7120 unsigned int op = is_crc32 ? 1 : i.operands;
20592a94 7121
1a035124
JB
7122 while (op--)
7123 if (i.tm.operand_types[op].bitfield.instance == InstanceNone
7124 || i.tm.operand_types[op].bitfield.instance == Accum)
7125 {
7126 if (i.types[op].bitfield.class != Reg)
7127 continue;
7128 if (i.types[op].bitfield.byte)
7129 i.suffix = BYTE_MNEM_SUFFIX;
7130 else if (i.types[op].bitfield.word)
7131 i.suffix = WORD_MNEM_SUFFIX;
7132 else if (i.types[op].bitfield.dword)
7133 i.suffix = LONG_MNEM_SUFFIX;
7134 else if (i.types[op].bitfield.qword)
7135 i.suffix = QWORD_MNEM_SUFFIX;
7136 else
7137 continue;
7138 break;
7139 }
65fca059
JB
7140
7141 /* As an exception, movsx/movzx silently default to a byte source
7142 in AT&T mode. */
389d00a5 7143 if (is_movx && i.tm.opcode_modifier.w && !i.suffix && !intel_syntax)
65fca059 7144 i.suffix = BYTE_MNEM_SUFFIX;
29b0f896
AM
7145 }
7146 else if (i.suffix == BYTE_MNEM_SUFFIX)
7147 {
1cb0ab18 7148 if (!check_byte_reg ())
29b0f896
AM
7149 return 0;
7150 }
7151 else if (i.suffix == LONG_MNEM_SUFFIX)
7152 {
1cb0ab18 7153 if (!check_long_reg ())
29b0f896
AM
7154 return 0;
7155 }
7156 else if (i.suffix == QWORD_MNEM_SUFFIX)
7157 {
1cb0ab18 7158 if (!check_qword_reg ())
29b0f896
AM
7159 return 0;
7160 }
7161 else if (i.suffix == WORD_MNEM_SUFFIX)
7162 {
1cb0ab18 7163 if (!check_word_reg ())
29b0f896
AM
7164 return 0;
7165 }
3cd7f3e3
L
7166 else if (intel_syntax
7167 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
7168 /* Do nothing if the instruction is going to ignore the prefix. */
7169 ;
7170 else
7171 abort ();
65fca059
JB
7172
7173 /* Undo the movsx/movzx change done above. */
7174 i.operands = numop;
29b0f896 7175 }
3cd7f3e3
L
7176 else if (i.tm.opcode_modifier.mnemonicsize == DEFAULTSIZE
7177 && !i.suffix)
29b0f896 7178 {
13e600d0
JB
7179 i.suffix = stackop_size;
7180 if (stackop_size == LONG_MNEM_SUFFIX)
06f74c5c
L
7181 {
7182 /* stackop_size is set to LONG_MNEM_SUFFIX for the
7183 .code16gcc directive to support 16-bit mode with
7184 32-bit address. For IRET without a suffix, generate
7185 16-bit IRET (opcode 0xcf) to return from an interrupt
7186 handler. */
13e600d0
JB
7187 if (i.tm.base_opcode == 0xcf)
7188 {
7189 i.suffix = WORD_MNEM_SUFFIX;
7190 as_warn (_("generating 16-bit `iret' for .code16gcc directive"));
7191 }
7192 /* Warn about changed behavior for segment register push/pop. */
7193 else if ((i.tm.base_opcode | 1) == 0x07)
7194 as_warn (_("generating 32-bit `%s', unlike earlier gas versions"),
7195 i.tm.name);
06f74c5c 7196 }
29b0f896 7197 }
c006a730 7198 else if (!i.suffix
0cfa3eb3
JB
7199 && (i.tm.opcode_modifier.jump == JUMP_ABSOLUTE
7200 || i.tm.opcode_modifier.jump == JUMP_BYTE
7201 || i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT
389d00a5
JB
7202 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
7203 && i.tm.base_opcode == 0x01 /* [ls][gi]dt */
64e74474 7204 && i.tm.extension_opcode <= 3)))
9306ca4a
JB
7205 {
7206 switch (flag_code)
7207 {
7208 case CODE_64BIT:
40fb9820 7209 if (!i.tm.opcode_modifier.no_qsuf)
9306ca4a 7210 {
828c2a25
JB
7211 if (i.tm.opcode_modifier.jump == JUMP_BYTE
7212 || i.tm.opcode_modifier.no_lsuf)
7213 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a
JB
7214 break;
7215 }
1a0670f3 7216 /* Fall through. */
9306ca4a 7217 case CODE_32BIT:
40fb9820 7218 if (!i.tm.opcode_modifier.no_lsuf)
9306ca4a
JB
7219 i.suffix = LONG_MNEM_SUFFIX;
7220 break;
7221 case CODE_16BIT:
40fb9820 7222 if (!i.tm.opcode_modifier.no_wsuf)
9306ca4a
JB
7223 i.suffix = WORD_MNEM_SUFFIX;
7224 break;
7225 }
7226 }
252b5132 7227
c006a730 7228 if (!i.suffix
3cd7f3e3 7229 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8
JB
7230 /* Also cover lret/retf/iret in 64-bit mode. */
7231 || (flag_code == CODE_64BIT
7232 && !i.tm.opcode_modifier.no_lsuf
7233 && !i.tm.opcode_modifier.no_qsuf))
3cd7f3e3 7234 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
8bbb3ad8
JB
7235 /* Explicit sizing prefixes are assumed to disambiguate insns. */
7236 && !i.prefix[DATA_PREFIX] && !(i.prefix[REX_PREFIX] & REX_W)
62b3f548
JB
7237 /* Accept FLDENV et al without suffix. */
7238 && (i.tm.opcode_modifier.no_ssuf || i.tm.opcode_modifier.floatmf))
29b0f896 7239 {
6c0946d0 7240 unsigned int suffixes, evex = 0;
c006a730
JB
7241
7242 suffixes = !i.tm.opcode_modifier.no_bsuf;
7243 if (!i.tm.opcode_modifier.no_wsuf)
7244 suffixes |= 1 << 1;
7245 if (!i.tm.opcode_modifier.no_lsuf)
7246 suffixes |= 1 << 2;
7247 if (!i.tm.opcode_modifier.no_ldsuf)
7248 suffixes |= 1 << 3;
7249 if (!i.tm.opcode_modifier.no_ssuf)
7250 suffixes |= 1 << 4;
7251 if (flag_code == CODE_64BIT && !i.tm.opcode_modifier.no_qsuf)
7252 suffixes |= 1 << 5;
7253
6c0946d0
JB
7254 /* For [XYZ]MMWORD operands inspect operand sizes. While generally
7255 also suitable for AT&T syntax mode, it was requested that this be
7256 restricted to just Intel syntax. */
a5748e0d
JB
7257 if (intel_syntax && is_any_vex_encoding (&i.tm)
7258 && !i.broadcast.type && !i.broadcast.bytes)
6c0946d0 7259 {
b9915cbc 7260 unsigned int op;
6c0946d0 7261
b9915cbc 7262 for (op = 0; op < i.tm.operands; ++op)
6c0946d0 7263 {
b9915cbc
JB
7264 if (is_evex_encoding (&i.tm)
7265 && !cpu_arch_flags.bitfield.cpuavx512vl)
6c0946d0 7266 {
b9915cbc
JB
7267 if (i.tm.operand_types[op].bitfield.ymmword)
7268 i.tm.operand_types[op].bitfield.xmmword = 0;
7269 if (i.tm.operand_types[op].bitfield.zmmword)
7270 i.tm.operand_types[op].bitfield.ymmword = 0;
7271 if (!i.tm.opcode_modifier.evex
7272 || i.tm.opcode_modifier.evex == EVEXDYN)
7273 i.tm.opcode_modifier.evex = EVEX512;
7274 }
6c0946d0 7275
b9915cbc
JB
7276 if (i.tm.operand_types[op].bitfield.xmmword
7277 + i.tm.operand_types[op].bitfield.ymmword
7278 + i.tm.operand_types[op].bitfield.zmmword < 2)
7279 continue;
6c0946d0 7280
b9915cbc
JB
7281 /* Any properly sized operand disambiguates the insn. */
7282 if (i.types[op].bitfield.xmmword
7283 || i.types[op].bitfield.ymmword
7284 || i.types[op].bitfield.zmmword)
7285 {
7286 suffixes &= ~(7 << 6);
7287 evex = 0;
7288 break;
7289 }
6c0946d0 7290
b9915cbc
JB
7291 if ((i.flags[op] & Operand_Mem)
7292 && i.tm.operand_types[op].bitfield.unspecified)
7293 {
7294 if (i.tm.operand_types[op].bitfield.xmmword)
7295 suffixes |= 1 << 6;
7296 if (i.tm.operand_types[op].bitfield.ymmword)
7297 suffixes |= 1 << 7;
7298 if (i.tm.operand_types[op].bitfield.zmmword)
7299 suffixes |= 1 << 8;
7300 if (is_evex_encoding (&i.tm))
7301 evex = EVEX512;
6c0946d0
JB
7302 }
7303 }
7304 }
7305
7306 /* Are multiple suffixes / operand sizes allowed? */
c006a730 7307 if (suffixes & (suffixes - 1))
9306ca4a 7308 {
873494c8 7309 if (intel_syntax
3cd7f3e3 7310 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8 7311 || operand_check == check_error))
9306ca4a 7312 {
c006a730 7313 as_bad (_("ambiguous operand size for `%s'"), i.tm.name);
9306ca4a
JB
7314 return 0;
7315 }
c006a730 7316 if (operand_check == check_error)
9306ca4a 7317 {
c006a730
JB
7318 as_bad (_("no instruction mnemonic suffix given and "
7319 "no register operands; can't size `%s'"), i.tm.name);
9306ca4a
JB
7320 return 0;
7321 }
c006a730 7322 if (operand_check == check_warning)
873494c8
JB
7323 as_warn (_("%s; using default for `%s'"),
7324 intel_syntax
7325 ? _("ambiguous operand size")
7326 : _("no instruction mnemonic suffix given and "
7327 "no register operands"),
7328 i.tm.name);
c006a730
JB
7329
7330 if (i.tm.opcode_modifier.floatmf)
7331 i.suffix = SHORT_MNEM_SUFFIX;
389d00a5 7332 else if (is_movx)
65fca059 7333 /* handled below */;
6c0946d0
JB
7334 else if (evex)
7335 i.tm.opcode_modifier.evex = evex;
c006a730
JB
7336 else if (flag_code == CODE_16BIT)
7337 i.suffix = WORD_MNEM_SUFFIX;
1a035124 7338 else if (!i.tm.opcode_modifier.no_lsuf)
c006a730 7339 i.suffix = LONG_MNEM_SUFFIX;
1a035124
JB
7340 else
7341 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a 7342 }
29b0f896 7343 }
252b5132 7344
389d00a5 7345 if (is_movx)
65fca059
JB
7346 {
7347 /* In Intel syntax, movsx/movzx must have a "suffix" (checked above).
7348 In AT&T syntax, if there is no suffix (warned about above), the default
7349 will be byte extension. */
7350 if (i.tm.opcode_modifier.w && i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
7351 i.tm.base_opcode |= 1;
7352
7353 /* For further processing, the suffix should represent the destination
7354 (register). This is already the case when one was used with
7355 mov[sz][bw]*, but we need to replace it for mov[sz]x, or if there was
7356 no suffix to begin with. */
7357 if (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63 || !i.suffix)
7358 {
7359 if (i.types[1].bitfield.word)
7360 i.suffix = WORD_MNEM_SUFFIX;
7361 else if (i.types[1].bitfield.qword)
7362 i.suffix = QWORD_MNEM_SUFFIX;
7363 else
7364 i.suffix = LONG_MNEM_SUFFIX;
7365
7366 i.tm.opcode_modifier.w = 0;
7367 }
7368 }
7369
50128d0c
JB
7370 if (!i.tm.opcode_modifier.modrm && i.reg_operands && i.tm.operands < 3)
7371 i.short_form = (i.tm.operand_types[0].bitfield.class == Reg)
7372 != (i.tm.operand_types[1].bitfield.class == Reg);
7373
d2224064
JB
7374 /* Change the opcode based on the operand size given by i.suffix. */
7375 switch (i.suffix)
29b0f896 7376 {
d2224064
JB
7377 /* Size floating point instruction. */
7378 case LONG_MNEM_SUFFIX:
7379 if (i.tm.opcode_modifier.floatmf)
7380 {
7381 i.tm.base_opcode ^= 4;
7382 break;
7383 }
7384 /* fall through */
7385 case WORD_MNEM_SUFFIX:
7386 case QWORD_MNEM_SUFFIX:
29b0f896 7387 /* It's not a byte, select word/dword operation. */
40fb9820 7388 if (i.tm.opcode_modifier.w)
29b0f896 7389 {
50128d0c 7390 if (i.short_form)
29b0f896
AM
7391 i.tm.base_opcode |= 8;
7392 else
7393 i.tm.base_opcode |= 1;
7394 }
d2224064
JB
7395 /* fall through */
7396 case SHORT_MNEM_SUFFIX:
29b0f896
AM
7397 /* Now select between word & dword operations via the operand
7398 size prefix, except for instructions that will ignore this
7399 prefix anyway. */
c8f8eebc 7400 if (i.suffix != QWORD_MNEM_SUFFIX
3cd7f3e3 7401 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
c8f8eebc
JB
7402 && !i.tm.opcode_modifier.floatmf
7403 && !is_any_vex_encoding (&i.tm)
7404 && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
7405 || (flag_code == CODE_64BIT
7406 && i.tm.opcode_modifier.jump == JUMP_BYTE)))
24eab124
AM
7407 {
7408 unsigned int prefix = DATA_PREFIX_OPCODE;
543613e9 7409
0cfa3eb3 7410 if (i.tm.opcode_modifier.jump == JUMP_BYTE) /* jcxz, loop */
29b0f896 7411 prefix = ADDR_PREFIX_OPCODE;
252b5132 7412
29b0f896
AM
7413 if (!add_prefix (prefix))
7414 return 0;
24eab124 7415 }
252b5132 7416
29b0f896
AM
7417 /* Set mode64 for an operand. */
7418 if (i.suffix == QWORD_MNEM_SUFFIX
9146926a 7419 && flag_code == CODE_64BIT
d2224064 7420 && !i.tm.opcode_modifier.norex64
4ed21b58 7421 && !i.tm.opcode_modifier.vexw
46e883c5 7422 /* Special case for xchg %rax,%rax. It is NOP and doesn't
d2224064
JB
7423 need rex64. */
7424 && ! (i.operands == 2
7425 && i.tm.base_opcode == 0x90
7426 && i.tm.extension_opcode == None
75e5731b
JB
7427 && i.types[0].bitfield.instance == Accum
7428 && i.types[0].bitfield.qword
7429 && i.types[1].bitfield.instance == Accum
7430 && i.types[1].bitfield.qword))
d2224064 7431 i.rex |= REX_W;
3e73aa7c 7432
d2224064 7433 break;
8bbb3ad8
JB
7434
7435 case 0:
f9a6a8f0 7436 /* Select word/dword/qword operation with explicit data sizing prefix
8bbb3ad8
JB
7437 when there are no suitable register operands. */
7438 if (i.tm.opcode_modifier.w
7439 && (i.prefix[DATA_PREFIX] || (i.prefix[REX_PREFIX] & REX_W))
7440 && (!i.reg_operands
7441 || (i.reg_operands == 1
7442 /* ShiftCount */
7443 && (i.tm.operand_types[0].bitfield.instance == RegC
7444 /* InOutPortReg */
7445 || i.tm.operand_types[0].bitfield.instance == RegD
7446 || i.tm.operand_types[1].bitfield.instance == RegD
7447 /* CRC32 */
8b65b895 7448 || is_crc32))))
8bbb3ad8
JB
7449 i.tm.base_opcode |= 1;
7450 break;
29b0f896 7451 }
7ecd2f8b 7452
255571cd 7453 if (i.tm.opcode_modifier.operandconstraint == ADDR_PREFIX_OP_REG)
c0a30a9f 7454 {
c8f8eebc
JB
7455 gas_assert (!i.suffix);
7456 gas_assert (i.reg_operands);
c0a30a9f 7457
c8f8eebc
JB
7458 if (i.tm.operand_types[0].bitfield.instance == Accum
7459 || i.operands == 1)
7460 {
7461 /* The address size override prefix changes the size of the
7462 first operand. */
7463 if (flag_code == CODE_64BIT
7464 && i.op[0].regs->reg_type.bitfield.word)
7465 {
7466 as_bad (_("16-bit addressing unavailable for `%s'"),
7467 i.tm.name);
7468 return 0;
7469 }
7470
7471 if ((flag_code == CODE_32BIT
7472 ? i.op[0].regs->reg_type.bitfield.word
7473 : i.op[0].regs->reg_type.bitfield.dword)
7474 && !add_prefix (ADDR_PREFIX_OPCODE))
7475 return 0;
7476 }
c0a30a9f
L
7477 else
7478 {
c8f8eebc
JB
7479 /* Check invalid register operand when the address size override
7480 prefix changes the size of register operands. */
7481 unsigned int op;
7482 enum { need_word, need_dword, need_qword } need;
7483
27f13469 7484 /* Check the register operand for the address size prefix if
b3a3496f 7485 the memory operand has no real registers, like symbol, DISP
829f3fe1 7486 or bogus (x32-only) symbol(%rip) when symbol(%eip) is meant. */
27f13469
L
7487 if (i.mem_operands == 1
7488 && i.reg_operands == 1
7489 && i.operands == 2
27f13469 7490 && i.types[1].bitfield.class == Reg
b3a3496f
L
7491 && (flag_code == CODE_32BIT
7492 ? i.op[1].regs->reg_type.bitfield.word
7493 : i.op[1].regs->reg_type.bitfield.dword)
7494 && ((i.base_reg == NULL && i.index_reg == NULL)
829f3fe1
JB
7495#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
7496 || (x86_elf_abi == X86_64_X32_ABI
7497 && i.base_reg
b3a3496f
L
7498 && i.base_reg->reg_num == RegIP
7499 && i.base_reg->reg_type.bitfield.qword))
829f3fe1
JB
7500#else
7501 || 0)
7502#endif
27f13469
L
7503 && !add_prefix (ADDR_PREFIX_OPCODE))
7504 return 0;
7505
c8f8eebc
JB
7506 if (flag_code == CODE_32BIT)
7507 need = i.prefix[ADDR_PREFIX] ? need_word : need_dword;
7508 else if (i.prefix[ADDR_PREFIX])
c0a30a9f
L
7509 need = need_dword;
7510 else
7511 need = flag_code == CODE_64BIT ? need_qword : need_word;
c0a30a9f 7512
c8f8eebc
JB
7513 for (op = 0; op < i.operands; op++)
7514 {
7515 if (i.types[op].bitfield.class != Reg)
7516 continue;
7517
7518 switch (need)
7519 {
7520 case need_word:
7521 if (i.op[op].regs->reg_type.bitfield.word)
7522 continue;
7523 break;
7524 case need_dword:
7525 if (i.op[op].regs->reg_type.bitfield.dword)
7526 continue;
7527 break;
7528 case need_qword:
7529 if (i.op[op].regs->reg_type.bitfield.qword)
7530 continue;
7531 break;
7532 }
7533
7534 as_bad (_("invalid register operand size for `%s'"),
7535 i.tm.name);
7536 return 0;
7537 }
7538 }
c0a30a9f
L
7539 }
7540
29b0f896
AM
7541 return 1;
7542}
3e73aa7c 7543
29b0f896 7544static int
543613e9 7545check_byte_reg (void)
29b0f896
AM
7546{
7547 int op;
543613e9 7548
29b0f896
AM
7549 for (op = i.operands; --op >= 0;)
7550 {
dc821c5f 7551 /* Skip non-register operands. */
bab6aec1 7552 if (i.types[op].bitfield.class != Reg)
dc821c5f
JB
7553 continue;
7554
29b0f896
AM
7555 /* If this is an eight bit register, it's OK. If it's the 16 or
7556 32 bit version of an eight bit register, we will just use the
7557 low portion, and that's OK too. */
dc821c5f 7558 if (i.types[op].bitfield.byte)
29b0f896
AM
7559 continue;
7560
5a819eb9 7561 /* I/O port address operands are OK too. */
75e5731b
JB
7562 if (i.tm.operand_types[op].bitfield.instance == RegD
7563 && i.tm.operand_types[op].bitfield.word)
5a819eb9
JB
7564 continue;
7565
9706160a 7566 /* crc32 only wants its source operand checked here. */
389d00a5
JB
7567 if (i.tm.base_opcode == 0xf0
7568 && i.tm.opcode_modifier.opcodespace == SPACE_0F38
8b65b895
L
7569 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2
7570 && op != 0)
9344ff29
L
7571 continue;
7572
29b0f896 7573 /* Any other register is bad. */
73c76375
JB
7574 as_bad (_("`%s%s' not allowed with `%s%c'"),
7575 register_prefix, i.op[op].regs->reg_name,
7576 i.tm.name, i.suffix);
7577 return 0;
29b0f896
AM
7578 }
7579 return 1;
7580}
7581
7582static int
e3bb37b5 7583check_long_reg (void)
29b0f896
AM
7584{
7585 int op;
7586
7587 for (op = i.operands; --op >= 0;)
dc821c5f 7588 /* Skip non-register operands. */
bab6aec1 7589 if (i.types[op].bitfield.class != Reg)
dc821c5f 7590 continue;
29b0f896
AM
7591 /* Reject eight bit registers, except where the template requires
7592 them. (eg. movzb) */
dc821c5f 7593 else if (i.types[op].bitfield.byte
bab6aec1 7594 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7595 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7596 && (i.tm.operand_types[op].bitfield.word
7597 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7598 {
a540244d
L
7599 as_bad (_("`%s%s' not allowed with `%s%c'"),
7600 register_prefix,
29b0f896
AM
7601 i.op[op].regs->reg_name,
7602 i.tm.name,
7603 i.suffix);
7604 return 0;
7605 }
be4c5e58
L
7606 /* Error if the e prefix on a general reg is missing. */
7607 else if (i.types[op].bitfield.word
bab6aec1 7608 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7609 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7610 && i.tm.operand_types[op].bitfield.dword)
29b0f896 7611 {
be4c5e58
L
7612 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7613 register_prefix, i.op[op].regs->reg_name,
7614 i.suffix);
7615 return 0;
252b5132 7616 }
e4630f71 7617 /* Warn if the r prefix on a general reg is present. */
dc821c5f 7618 else if (i.types[op].bitfield.qword
bab6aec1 7619 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7620 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7621 && i.tm.operand_types[op].bitfield.dword)
252b5132 7622 {
1cb0ab18
JB
7623 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7624 register_prefix, i.op[op].regs->reg_name, i.suffix);
7625 return 0;
29b0f896
AM
7626 }
7627 return 1;
7628}
252b5132 7629
29b0f896 7630static int
e3bb37b5 7631check_qword_reg (void)
29b0f896
AM
7632{
7633 int op;
252b5132 7634
29b0f896 7635 for (op = i.operands; --op >= 0; )
dc821c5f 7636 /* Skip non-register operands. */
bab6aec1 7637 if (i.types[op].bitfield.class != Reg)
dc821c5f 7638 continue;
29b0f896
AM
7639 /* Reject eight bit registers, except where the template requires
7640 them. (eg. movzb) */
dc821c5f 7641 else if (i.types[op].bitfield.byte
bab6aec1 7642 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7643 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7644 && (i.tm.operand_types[op].bitfield.word
7645 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7646 {
a540244d
L
7647 as_bad (_("`%s%s' not allowed with `%s%c'"),
7648 register_prefix,
29b0f896
AM
7649 i.op[op].regs->reg_name,
7650 i.tm.name,
7651 i.suffix);
7652 return 0;
7653 }
e4630f71 7654 /* Warn if the r prefix on a general reg is missing. */
dc821c5f
JB
7655 else if ((i.types[op].bitfield.word
7656 || i.types[op].bitfield.dword)
bab6aec1 7657 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7658 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7659 && i.tm.operand_types[op].bitfield.qword)
29b0f896
AM
7660 {
7661 /* Prohibit these changes in the 64bit mode, since the
7662 lowering is more complicated. */
1cb0ab18
JB
7663 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7664 register_prefix, i.op[op].regs->reg_name, i.suffix);
7665 return 0;
252b5132 7666 }
29b0f896
AM
7667 return 1;
7668}
252b5132 7669
29b0f896 7670static int
e3bb37b5 7671check_word_reg (void)
29b0f896
AM
7672{
7673 int op;
7674 for (op = i.operands; --op >= 0;)
dc821c5f 7675 /* Skip non-register operands. */
bab6aec1 7676 if (i.types[op].bitfield.class != Reg)
dc821c5f 7677 continue;
29b0f896
AM
7678 /* Reject eight bit registers, except where the template requires
7679 them. (eg. movzb) */
dc821c5f 7680 else if (i.types[op].bitfield.byte
bab6aec1 7681 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7682 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7683 && (i.tm.operand_types[op].bitfield.word
7684 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7685 {
a540244d
L
7686 as_bad (_("`%s%s' not allowed with `%s%c'"),
7687 register_prefix,
29b0f896
AM
7688 i.op[op].regs->reg_name,
7689 i.tm.name,
7690 i.suffix);
7691 return 0;
7692 }
9706160a
JB
7693 /* Error if the e or r prefix on a general reg is present. */
7694 else if ((i.types[op].bitfield.dword
dc821c5f 7695 || i.types[op].bitfield.qword)
bab6aec1 7696 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7697 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7698 && i.tm.operand_types[op].bitfield.word)
252b5132 7699 {
9706160a
JB
7700 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7701 register_prefix, i.op[op].regs->reg_name,
7702 i.suffix);
7703 return 0;
29b0f896
AM
7704 }
7705 return 1;
7706}
252b5132 7707
29b0f896 7708static int
40fb9820 7709update_imm (unsigned int j)
29b0f896 7710{
bc0844ae 7711 i386_operand_type overlap = i.types[j];
be1643ff
JB
7712 if (overlap.bitfield.imm8
7713 + overlap.bitfield.imm8s
7714 + overlap.bitfield.imm16
7715 + overlap.bitfield.imm32
7716 + overlap.bitfield.imm32s
7717 + overlap.bitfield.imm64 > 1)
29b0f896
AM
7718 {
7719 if (i.suffix)
7720 {
40fb9820
L
7721 i386_operand_type temp;
7722
0dfbf9d7 7723 operand_type_set (&temp, 0);
7ab9ffdd 7724 if (i.suffix == BYTE_MNEM_SUFFIX)
40fb9820
L
7725 {
7726 temp.bitfield.imm8 = overlap.bitfield.imm8;
7727 temp.bitfield.imm8s = overlap.bitfield.imm8s;
7728 }
7729 else if (i.suffix == WORD_MNEM_SUFFIX)
7730 temp.bitfield.imm16 = overlap.bitfield.imm16;
7731 else if (i.suffix == QWORD_MNEM_SUFFIX)
7732 {
7733 temp.bitfield.imm64 = overlap.bitfield.imm64;
7734 temp.bitfield.imm32s = overlap.bitfield.imm32s;
7735 }
7736 else
7737 temp.bitfield.imm32 = overlap.bitfield.imm32;
7738 overlap = temp;
29b0f896 7739 }
0dfbf9d7
L
7740 else if (operand_type_equal (&overlap, &imm16_32_32s)
7741 || operand_type_equal (&overlap, &imm16_32)
7742 || operand_type_equal (&overlap, &imm16_32s))
29b0f896 7743 {
40fb9820 7744 if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
65da13b5 7745 overlap = imm16;
40fb9820 7746 else
65da13b5 7747 overlap = imm32s;
29b0f896 7748 }
8bbb3ad8
JB
7749 else if (i.prefix[REX_PREFIX] & REX_W)
7750 overlap = operand_type_and (overlap, imm32s);
7751 else if (i.prefix[DATA_PREFIX])
7752 overlap = operand_type_and (overlap,
7753 flag_code != CODE_16BIT ? imm16 : imm32);
be1643ff
JB
7754 if (overlap.bitfield.imm8
7755 + overlap.bitfield.imm8s
7756 + overlap.bitfield.imm16
7757 + overlap.bitfield.imm32
7758 + overlap.bitfield.imm32s
7759 + overlap.bitfield.imm64 != 1)
29b0f896 7760 {
4eed87de
AM
7761 as_bad (_("no instruction mnemonic suffix given; "
7762 "can't determine immediate size"));
29b0f896
AM
7763 return 0;
7764 }
7765 }
40fb9820 7766 i.types[j] = overlap;
29b0f896 7767
40fb9820
L
7768 return 1;
7769}
7770
7771static int
7772finalize_imm (void)
7773{
bc0844ae 7774 unsigned int j, n;
29b0f896 7775
bc0844ae
L
7776 /* Update the first 2 immediate operands. */
7777 n = i.operands > 2 ? 2 : i.operands;
7778 if (n)
7779 {
7780 for (j = 0; j < n; j++)
7781 if (update_imm (j) == 0)
7782 return 0;
40fb9820 7783
bc0844ae
L
7784 /* The 3rd operand can't be immediate operand. */
7785 gas_assert (operand_type_check (i.types[2], imm) == 0);
7786 }
29b0f896
AM
7787
7788 return 1;
7789}
7790
7791static int
e3bb37b5 7792process_operands (void)
29b0f896
AM
7793{
7794 /* Default segment register this instruction will use for memory
7795 accesses. 0 means unknown. This is only for optimizing out
7796 unnecessary segment overrides. */
5e042380 7797 const reg_entry *default_seg = NULL;
29b0f896 7798
a5aeccd9
JB
7799 if (i.tm.opcode_modifier.sse2avx)
7800 {
7801 /* Legacy encoded insns allow explicit REX prefixes, so these prefixes
7802 need converting. */
7803 i.rex |= i.prefix[REX_PREFIX] & (REX_W | REX_R | REX_X | REX_B);
7804 i.prefix[REX_PREFIX] = 0;
7805 i.rex_encoding = 0;
7806 }
c423d21a
JB
7807 /* ImmExt should be processed after SSE2AVX. */
7808 else if (i.tm.opcode_modifier.immext)
7809 process_immext ();
a5aeccd9 7810
2426c15f 7811 if (i.tm.opcode_modifier.sse2avx && i.tm.opcode_modifier.vexvvvv)
29b0f896 7812 {
91d6fa6a
NC
7813 unsigned int dupl = i.operands;
7814 unsigned int dest = dupl - 1;
9fcfb3d7
L
7815 unsigned int j;
7816
c0f3af97 7817 /* The destination must be an xmm register. */
9c2799c2 7818 gas_assert (i.reg_operands
91d6fa6a 7819 && MAX_OPERANDS > dupl
7ab9ffdd 7820 && operand_type_equal (&i.types[dest], &regxmm));
c0f3af97 7821
75e5731b 7822 if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 7823 && i.tm.operand_types[0].bitfield.xmmword)
e2ec9d29 7824 {
8cd7925b 7825 if (i.tm.opcode_modifier.vexsources == VEX3SOURCES)
c0f3af97
L
7826 {
7827 /* Keep xmm0 for instructions with VEX prefix and 3
7828 sources. */
75e5731b 7829 i.tm.operand_types[0].bitfield.instance = InstanceNone;
3528c362 7830 i.tm.operand_types[0].bitfield.class = RegSIMD;
c0f3af97
L
7831 goto duplicate;
7832 }
e2ec9d29 7833 else
c0f3af97
L
7834 {
7835 /* We remove the first xmm0 and keep the number of
7836 operands unchanged, which in fact duplicates the
7837 destination. */
7838 for (j = 1; j < i.operands; j++)
7839 {
7840 i.op[j - 1] = i.op[j];
7841 i.types[j - 1] = i.types[j];
7842 i.tm.operand_types[j - 1] = i.tm.operand_types[j];
8dc0818e 7843 i.flags[j - 1] = i.flags[j];
c0f3af97
L
7844 }
7845 }
7846 }
255571cd 7847 else if (i.tm.opcode_modifier.operandconstraint == IMPLICIT_1ST_XMM0)
7ab9ffdd 7848 {
91d6fa6a 7849 gas_assert ((MAX_OPERANDS - 1) > dupl
8cd7925b
L
7850 && (i.tm.opcode_modifier.vexsources
7851 == VEX3SOURCES));
c0f3af97
L
7852
7853 /* Add the implicit xmm0 for instructions with VEX prefix
7854 and 3 sources. */
7855 for (j = i.operands; j > 0; j--)
7856 {
7857 i.op[j] = i.op[j - 1];
7858 i.types[j] = i.types[j - 1];
7859 i.tm.operand_types[j] = i.tm.operand_types[j - 1];
8dc0818e 7860 i.flags[j] = i.flags[j - 1];
c0f3af97
L
7861 }
7862 i.op[0].regs
629310ab 7863 = (const reg_entry *) str_hash_find (reg_hash, "xmm0");
7ab9ffdd 7864 i.types[0] = regxmm;
c0f3af97
L
7865 i.tm.operand_types[0] = regxmm;
7866
7867 i.operands += 2;
7868 i.reg_operands += 2;
7869 i.tm.operands += 2;
7870
91d6fa6a 7871 dupl++;
c0f3af97 7872 dest++;
91d6fa6a
NC
7873 i.op[dupl] = i.op[dest];
7874 i.types[dupl] = i.types[dest];
7875 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 7876 i.flags[dupl] = i.flags[dest];
e2ec9d29 7877 }
c0f3af97
L
7878 else
7879 {
dc1e8a47 7880 duplicate:
c0f3af97
L
7881 i.operands++;
7882 i.reg_operands++;
7883 i.tm.operands++;
7884
91d6fa6a
NC
7885 i.op[dupl] = i.op[dest];
7886 i.types[dupl] = i.types[dest];
7887 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 7888 i.flags[dupl] = i.flags[dest];
c0f3af97
L
7889 }
7890
7891 if (i.tm.opcode_modifier.immext)
7892 process_immext ();
7893 }
75e5731b 7894 else if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 7895 && i.tm.operand_types[0].bitfield.xmmword)
c0f3af97
L
7896 {
7897 unsigned int j;
7898
9fcfb3d7
L
7899 for (j = 1; j < i.operands; j++)
7900 {
7901 i.op[j - 1] = i.op[j];
7902 i.types[j - 1] = i.types[j];
7903
7904 /* We need to adjust fields in i.tm since they are used by
7905 build_modrm_byte. */
7906 i.tm.operand_types [j - 1] = i.tm.operand_types [j];
8dc0818e
JB
7907
7908 i.flags[j - 1] = i.flags[j];
9fcfb3d7
L
7909 }
7910
e2ec9d29
L
7911 i.operands--;
7912 i.reg_operands--;
e2ec9d29
L
7913 i.tm.operands--;
7914 }
255571cd 7915 else if (i.tm.opcode_modifier.operandconstraint == IMPLICIT_QUAD_GROUP)
920d2ddc 7916 {
a477a8c4
JB
7917 unsigned int regnum, first_reg_in_group, last_reg_in_group;
7918
920d2ddc 7919 /* The second operand must be {x,y,z}mmN, where N is a multiple of 4. */
3528c362 7920 gas_assert (i.operands >= 2 && i.types[1].bitfield.class == RegSIMD);
a477a8c4
JB
7921 regnum = register_number (i.op[1].regs);
7922 first_reg_in_group = regnum & ~3;
7923 last_reg_in_group = first_reg_in_group + 3;
7924 if (regnum != first_reg_in_group)
7925 as_warn (_("source register `%s%s' implicitly denotes"
7926 " `%s%.3s%u' to `%s%.3s%u' source group in `%s'"),
7927 register_prefix, i.op[1].regs->reg_name,
7928 register_prefix, i.op[1].regs->reg_name, first_reg_in_group,
7929 register_prefix, i.op[1].regs->reg_name, last_reg_in_group,
7930 i.tm.name);
7931 }
255571cd 7932 else if (i.tm.opcode_modifier.operandconstraint == REG_KLUDGE)
e2ec9d29
L
7933 {
7934 /* The imul $imm, %reg instruction is converted into
7935 imul $imm, %reg, %reg, and the clr %reg instruction
7936 is converted into xor %reg, %reg. */
7937
7938 unsigned int first_reg_op;
7939
7940 if (operand_type_check (i.types[0], reg))
7941 first_reg_op = 0;
7942 else
7943 first_reg_op = 1;
7944 /* Pretend we saw the extra register operand. */
9c2799c2 7945 gas_assert (i.reg_operands == 1
7ab9ffdd 7946 && i.op[first_reg_op + 1].regs == 0);
e2ec9d29
L
7947 i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
7948 i.types[first_reg_op + 1] = i.types[first_reg_op];
7949 i.operands++;
7950 i.reg_operands++;
29b0f896
AM
7951 }
7952
85b80b0f 7953 if (i.tm.opcode_modifier.modrm)
29b0f896
AM
7954 {
7955 /* The opcode is completed (modulo i.tm.extension_opcode which
52271982
AM
7956 must be put into the modrm byte). Now, we make the modrm and
7957 index base bytes based on all the info we've collected. */
29b0f896
AM
7958
7959 default_seg = build_modrm_byte ();
7960 }
00cee14f 7961 else if (i.types[0].bitfield.class == SReg)
85b80b0f
JB
7962 {
7963 if (flag_code != CODE_64BIT
7964 ? i.tm.base_opcode == POP_SEG_SHORT
7965 && i.op[0].regs->reg_num == 1
389d00a5 7966 : (i.tm.base_opcode | 1) == (POP_SEG386_SHORT & 0xff)
85b80b0f
JB
7967 && i.op[0].regs->reg_num < 4)
7968 {
7969 as_bad (_("you can't `%s %s%s'"),
7970 i.tm.name, register_prefix, i.op[0].regs->reg_name);
7971 return 0;
7972 }
389d00a5
JB
7973 if (i.op[0].regs->reg_num > 3
7974 && i.tm.opcode_modifier.opcodespace == SPACE_BASE )
85b80b0f 7975 {
389d00a5
JB
7976 i.tm.base_opcode ^= (POP_SEG_SHORT ^ POP_SEG386_SHORT) & 0xff;
7977 i.tm.opcode_modifier.opcodespace = SPACE_0F;
85b80b0f
JB
7978 }
7979 i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
7980 }
389d00a5
JB
7981 else if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
7982 && (i.tm.base_opcode & ~3) == MOV_AX_DISP32)
29b0f896 7983 {
5e042380 7984 default_seg = reg_ds;
29b0f896 7985 }
40fb9820 7986 else if (i.tm.opcode_modifier.isstring)
29b0f896
AM
7987 {
7988 /* For the string instructions that allow a segment override
7989 on one of their operands, the default segment is ds. */
5e042380 7990 default_seg = reg_ds;
29b0f896 7991 }
50128d0c 7992 else if (i.short_form)
85b80b0f
JB
7993 {
7994 /* The register or float register operand is in operand
7995 0 or 1. */
bab6aec1 7996 unsigned int op = i.tm.operand_types[0].bitfield.class != Reg;
85b80b0f
JB
7997
7998 /* Register goes in low 3 bits of opcode. */
7999 i.tm.base_opcode |= i.op[op].regs->reg_num;
8000 if ((i.op[op].regs->reg_flags & RegRex) != 0)
8001 i.rex |= REX_B;
255571cd 8002 if (!quiet_warnings && i.tm.opcode_modifier.operandconstraint == UGH)
85b80b0f
JB
8003 {
8004 /* Warn about some common errors, but press on regardless.
8005 The first case can be generated by gcc (<= 2.8.1). */
8006 if (i.operands == 2)
8007 {
8008 /* Reversed arguments on faddp, fsubp, etc. */
8009 as_warn (_("translating to `%s %s%s,%s%s'"), i.tm.name,
8010 register_prefix, i.op[!intel_syntax].regs->reg_name,
8011 register_prefix, i.op[intel_syntax].regs->reg_name);
8012 }
8013 else
8014 {
8015 /* Extraneous `l' suffix on fp insn. */
8016 as_warn (_("translating to `%s %s%s'"), i.tm.name,
8017 register_prefix, i.op[0].regs->reg_name);
8018 }
8019 }
8020 }
29b0f896 8021
514a8bb0 8022 if ((i.seg[0] || i.prefix[SEG_PREFIX])
514a8bb0 8023 && i.tm.base_opcode == 0x8d /* lea */
35648716 8024 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
514a8bb0 8025 && !is_any_vex_encoding(&i.tm))
92334ad2
JB
8026 {
8027 if (!quiet_warnings)
8028 as_warn (_("segment override on `%s' is ineffectual"), i.tm.name);
8029 if (optimize)
8030 {
8031 i.seg[0] = NULL;
8032 i.prefix[SEG_PREFIX] = 0;
8033 }
8034 }
52271982
AM
8035
8036 /* If a segment was explicitly specified, and the specified segment
b6773884
JB
8037 is neither the default nor the one already recorded from a prefix,
8038 use an opcode prefix to select it. If we never figured out what
8039 the default segment is, then default_seg will be zero at this
8040 point, and the specified segment prefix will always be used. */
8041 if (i.seg[0]
8042 && i.seg[0] != default_seg
5e042380 8043 && i386_seg_prefixes[i.seg[0]->reg_num] != i.prefix[SEG_PREFIX])
29b0f896 8044 {
5e042380 8045 if (!add_prefix (i386_seg_prefixes[i.seg[0]->reg_num]))
29b0f896
AM
8046 return 0;
8047 }
8048 return 1;
8049}
8050
a5aeccd9 8051static INLINE void set_rex_vrex (const reg_entry *r, unsigned int rex_bit,
5b7c81bd 8052 bool do_sse2avx)
a5aeccd9
JB
8053{
8054 if (r->reg_flags & RegRex)
8055 {
8056 if (i.rex & rex_bit)
8057 as_bad (_("same type of prefix used twice"));
8058 i.rex |= rex_bit;
8059 }
8060 else if (do_sse2avx && (i.rex & rex_bit) && i.vex.register_specifier)
8061 {
8062 gas_assert (i.vex.register_specifier == r);
8063 i.vex.register_specifier += 8;
8064 }
8065
8066 if (r->reg_flags & RegVRex)
8067 i.vrex |= rex_bit;
8068}
8069
5e042380 8070static const reg_entry *
e3bb37b5 8071build_modrm_byte (void)
29b0f896 8072{
5e042380 8073 const reg_entry *default_seg = NULL;
c0f3af97 8074 unsigned int source, dest;
8cd7925b 8075 int vex_3_sources;
c0f3af97 8076
8cd7925b 8077 vex_3_sources = i.tm.opcode_modifier.vexsources == VEX3SOURCES;
c0f3af97
L
8078 if (vex_3_sources)
8079 {
91d6fa6a 8080 unsigned int nds, reg_slot;
4c2c6516 8081 expressionS *exp;
c0f3af97 8082
6b8d3588 8083 dest = i.operands - 1;
c0f3af97 8084 nds = dest - 1;
922d8de8 8085
a683cc34 8086 /* There are 2 kinds of instructions:
bed3d976 8087 1. 5 operands: 4 register operands or 3 register operands
9d3bf266 8088 plus 1 memory operand plus one Imm4 operand, VexXDS, and
bed3d976 8089 VexW0 or VexW1. The destination must be either XMM, YMM or
43234a1e 8090 ZMM register.
bed3d976 8091 2. 4 operands: 4 register operands or 3 register operands
2f1bada2 8092 plus 1 memory operand, with VexXDS. */
922d8de8 8093 gas_assert ((i.reg_operands == 4
bed3d976
JB
8094 || (i.reg_operands == 3 && i.mem_operands == 1))
8095 && i.tm.opcode_modifier.vexvvvv == VEXXDS
dcd7e323 8096 && i.tm.opcode_modifier.vexw
3528c362 8097 && i.tm.operand_types[dest].bitfield.class == RegSIMD);
a683cc34 8098
48db9223
JB
8099 /* If VexW1 is set, the first non-immediate operand is the source and
8100 the second non-immediate one is encoded in the immediate operand. */
8101 if (i.tm.opcode_modifier.vexw == VEXW1)
8102 {
8103 source = i.imm_operands;
8104 reg_slot = i.imm_operands + 1;
8105 }
8106 else
8107 {
8108 source = i.imm_operands + 1;
8109 reg_slot = i.imm_operands;
8110 }
8111
a683cc34 8112 if (i.imm_operands == 0)
bed3d976
JB
8113 {
8114 /* When there is no immediate operand, generate an 8bit
8115 immediate operand to encode the first operand. */
8116 exp = &im_expressions[i.imm_operands++];
8117 i.op[i.operands].imms = exp;
be1643ff 8118 i.types[i.operands].bitfield.imm8 = 1;
bed3d976
JB
8119 i.operands++;
8120
3528c362 8121 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
bed3d976
JB
8122 exp->X_op = O_constant;
8123 exp->X_add_number = register_number (i.op[reg_slot].regs) << 4;
43234a1e
L
8124 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
8125 }
922d8de8 8126 else
bed3d976 8127 {
9d3bf266
JB
8128 gas_assert (i.imm_operands == 1);
8129 gas_assert (fits_in_imm4 (i.op[0].imms->X_add_number));
8130 gas_assert (!i.tm.opcode_modifier.immext);
a683cc34 8131
9d3bf266
JB
8132 /* Turn on Imm8 again so that output_imm will generate it. */
8133 i.types[0].bitfield.imm8 = 1;
bed3d976 8134
3528c362 8135 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
9d3bf266 8136 i.op[0].imms->X_add_number
bed3d976 8137 |= register_number (i.op[reg_slot].regs) << 4;
43234a1e 8138 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
bed3d976 8139 }
a683cc34 8140
3528c362 8141 gas_assert (i.tm.operand_types[nds].bitfield.class == RegSIMD);
dae39acc 8142 i.vex.register_specifier = i.op[nds].regs;
c0f3af97
L
8143 }
8144 else
8145 source = dest = 0;
29b0f896
AM
8146
8147 /* i.reg_operands MUST be the number of real register operands;
c0f3af97
L
8148 implicit registers do not count. If there are 3 register
8149 operands, it must be a instruction with VexNDS. For a
8150 instruction with VexNDD, the destination register is encoded
8151 in VEX prefix. If there are 4 register operands, it must be
8152 a instruction with VEX prefix and 3 sources. */
7ab9ffdd
L
8153 if (i.mem_operands == 0
8154 && ((i.reg_operands == 2
2426c15f 8155 && i.tm.opcode_modifier.vexvvvv <= VEXXDS)
7ab9ffdd 8156 || (i.reg_operands == 3
2426c15f 8157 && i.tm.opcode_modifier.vexvvvv == VEXXDS)
7ab9ffdd 8158 || (i.reg_operands == 4 && vex_3_sources)))
29b0f896 8159 {
cab737b9
L
8160 switch (i.operands)
8161 {
8162 case 2:
8163 source = 0;
8164 break;
8165 case 3:
c81128dc
L
8166 /* When there are 3 operands, one of them may be immediate,
8167 which may be the first or the last operand. Otherwise,
c0f3af97
L
8168 the first operand must be shift count register (cl) or it
8169 is an instruction with VexNDS. */
9c2799c2 8170 gas_assert (i.imm_operands == 1
7ab9ffdd 8171 || (i.imm_operands == 0
2426c15f 8172 && (i.tm.opcode_modifier.vexvvvv == VEXXDS
75e5731b
JB
8173 || (i.types[0].bitfield.instance == RegC
8174 && i.types[0].bitfield.byte))));
40fb9820 8175 if (operand_type_check (i.types[0], imm)
75e5731b
JB
8176 || (i.types[0].bitfield.instance == RegC
8177 && i.types[0].bitfield.byte))
40fb9820
L
8178 source = 1;
8179 else
8180 source = 0;
cab737b9
L
8181 break;
8182 case 4:
368d64cc
L
8183 /* When there are 4 operands, the first two must be 8bit
8184 immediate operands. The source operand will be the 3rd
c0f3af97
L
8185 one.
8186
8187 For instructions with VexNDS, if the first operand
8188 an imm8, the source operand is the 2nd one. If the last
8189 operand is imm8, the source operand is the first one. */
9c2799c2 8190 gas_assert ((i.imm_operands == 2
7ab9ffdd
L
8191 && i.types[0].bitfield.imm8
8192 && i.types[1].bitfield.imm8)
2426c15f 8193 || (i.tm.opcode_modifier.vexvvvv == VEXXDS
7ab9ffdd
L
8194 && i.imm_operands == 1
8195 && (i.types[0].bitfield.imm8
cf665fee 8196 || i.types[i.operands - 1].bitfield.imm8)));
9f2670f2
L
8197 if (i.imm_operands == 2)
8198 source = 2;
8199 else
c0f3af97
L
8200 {
8201 if (i.types[0].bitfield.imm8)
8202 source = 1;
8203 else
8204 source = 0;
8205 }
c0f3af97
L
8206 break;
8207 case 5:
cf665fee
JB
8208 gas_assert (!is_evex_encoding (&i.tm));
8209 gas_assert (i.imm_operands == 1 && vex_3_sources);
cab737b9
L
8210 break;
8211 default:
8212 abort ();
8213 }
8214
c0f3af97
L
8215 if (!vex_3_sources)
8216 {
8217 dest = source + 1;
8218
2426c15f 8219 if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
c0f3af97 8220 {
43234a1e 8221 /* For instructions with VexNDS, the register-only source
c5d0745b 8222 operand must be a 32/64bit integer, XMM, YMM, ZMM, or mask
dfd69174 8223 register. It is encoded in VEX prefix. */
f12dc422
L
8224
8225 i386_operand_type op;
8226 unsigned int vvvv;
8227
c2ecccb3 8228 /* Swap two source operands if needed. */
255571cd 8229 if (i.tm.opcode_modifier.operandconstraint == SWAP_SOURCES)
f12dc422
L
8230 {
8231 vvvv = source;
8232 source = dest;
8233 }
8234 else
8235 vvvv = dest;
8236
8237 op = i.tm.operand_types[vvvv];
c0f3af97 8238 if ((dest + 1) >= i.operands
bab6aec1 8239 || ((op.bitfield.class != Reg
dc821c5f 8240 || (!op.bitfield.dword && !op.bitfield.qword))
3528c362 8241 && op.bitfield.class != RegSIMD
c0f327b8 8242 && op.bitfield.class != RegMask))
c0f3af97 8243 abort ();
f12dc422 8244 i.vex.register_specifier = i.op[vvvv].regs;
c0f3af97
L
8245 dest++;
8246 }
8247 }
29b0f896
AM
8248
8249 i.rm.mode = 3;
dfd69174
JB
8250 /* One of the register operands will be encoded in the i.rm.reg
8251 field, the other in the combined i.rm.mode and i.rm.regmem
29b0f896
AM
8252 fields. If no form of this instruction supports a memory
8253 destination operand, then we assume the source operand may
8254 sometimes be a memory operand and so we need to store the
8255 destination in the i.rm.reg field. */
dfd69174 8256 if (!i.tm.opcode_modifier.regmem
40fb9820 8257 && operand_type_check (i.tm.operand_types[dest], anymem) == 0)
29b0f896
AM
8258 {
8259 i.rm.reg = i.op[dest].regs->reg_num;
8260 i.rm.regmem = i.op[source].regs->reg_num;
a5aeccd9 8261 set_rex_vrex (i.op[dest].regs, REX_R, i.tm.opcode_modifier.sse2avx);
5b7c81bd 8262 set_rex_vrex (i.op[source].regs, REX_B, false);
29b0f896
AM
8263 }
8264 else
8265 {
8266 i.rm.reg = i.op[source].regs->reg_num;
8267 i.rm.regmem = i.op[dest].regs->reg_num;
a5aeccd9 8268 set_rex_vrex (i.op[dest].regs, REX_B, i.tm.opcode_modifier.sse2avx);
5b7c81bd 8269 set_rex_vrex (i.op[source].regs, REX_R, false);
29b0f896 8270 }
e0c7f900 8271 if (flag_code != CODE_64BIT && (i.rex & REX_R))
c4a530c5 8272 {
4a5c67ed 8273 if (i.types[!i.tm.opcode_modifier.regmem].bitfield.class != RegCR)
c4a530c5 8274 abort ();
e0c7f900 8275 i.rex &= ~REX_R;
c4a530c5
JB
8276 add_prefix (LOCK_PREFIX_OPCODE);
8277 }
29b0f896
AM
8278 }
8279 else
8280 { /* If it's not 2 reg operands... */
c0f3af97
L
8281 unsigned int mem;
8282
29b0f896
AM
8283 if (i.mem_operands)
8284 {
8285 unsigned int fake_zero_displacement = 0;
99018f42 8286 unsigned int op;
4eed87de 8287
7ab9ffdd 8288 for (op = 0; op < i.operands; op++)
8dc0818e 8289 if (i.flags[op] & Operand_Mem)
7ab9ffdd 8290 break;
7ab9ffdd 8291 gas_assert (op < i.operands);
29b0f896 8292
63112cd6 8293 if (i.tm.opcode_modifier.sib)
6c30d220 8294 {
260cd341
LC
8295 /* The index register of VSIB shouldn't be RegIZ. */
8296 if (i.tm.opcode_modifier.sib != SIBMEM
8297 && i.index_reg->reg_num == RegIZ)
6c30d220
L
8298 abort ();
8299
8300 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8301 if (!i.base_reg)
8302 {
8303 i.sib.base = NO_BASE_REGISTER;
8304 i.sib.scale = i.log2_scale_factor;
2f2be86b 8305 i.types[op] = operand_type_and_not (i.types[op], anydisp);
a775efc8 8306 i.types[op].bitfield.disp32 = 1;
6c30d220 8307 }
260cd341
LC
8308
8309 /* Since the mandatory SIB always has index register, so
8310 the code logic remains unchanged. The non-mandatory SIB
8311 without index register is allowed and will be handled
8312 later. */
8313 if (i.index_reg)
8314 {
8315 if (i.index_reg->reg_num == RegIZ)
8316 i.sib.index = NO_INDEX_REGISTER;
8317 else
8318 i.sib.index = i.index_reg->reg_num;
5b7c81bd 8319 set_rex_vrex (i.index_reg, REX_X, false);
260cd341 8320 }
6c30d220
L
8321 }
8322
5e042380 8323 default_seg = reg_ds;
29b0f896
AM
8324
8325 if (i.base_reg == 0)
8326 {
8327 i.rm.mode = 0;
8328 if (!i.disp_operands)
9bb129e8 8329 fake_zero_displacement = 1;
29b0f896
AM
8330 if (i.index_reg == 0)
8331 {
260cd341
LC
8332 /* Both check for VSIB and mandatory non-vector SIB. */
8333 gas_assert (!i.tm.opcode_modifier.sib
8334 || i.tm.opcode_modifier.sib == SIBMEM);
29b0f896 8335 /* Operand is just <disp> */
2f2be86b 8336 i.types[op] = operand_type_and_not (i.types[op], anydisp);
20f0a1fc 8337 if (flag_code == CODE_64BIT)
29b0f896
AM
8338 {
8339 /* 64bit mode overwrites the 32bit absolute
8340 addressing by RIP relative addressing and
8341 absolute addressing is encoded by one of the
8342 redundant SIB forms. */
8343 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8344 i.sib.base = NO_BASE_REGISTER;
8345 i.sib.index = NO_INDEX_REGISTER;
a775efc8 8346 i.types[op].bitfield.disp32 = 1;
20f0a1fc 8347 }
fc225355
L
8348 else if ((flag_code == CODE_16BIT)
8349 ^ (i.prefix[ADDR_PREFIX] != 0))
20f0a1fc
NC
8350 {
8351 i.rm.regmem = NO_BASE_REGISTER_16;
2f2be86b 8352 i.types[op].bitfield.disp16 = 1;
20f0a1fc
NC
8353 }
8354 else
8355 {
8356 i.rm.regmem = NO_BASE_REGISTER;
2f2be86b 8357 i.types[op].bitfield.disp32 = 1;
29b0f896
AM
8358 }
8359 }
63112cd6 8360 else if (!i.tm.opcode_modifier.sib)
29b0f896 8361 {
6c30d220 8362 /* !i.base_reg && i.index_reg */
e968fc9b 8363 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8364 i.sib.index = NO_INDEX_REGISTER;
8365 else
8366 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8367 i.sib.base = NO_BASE_REGISTER;
8368 i.sib.scale = i.log2_scale_factor;
8369 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
2f2be86b 8370 i.types[op] = operand_type_and_not (i.types[op], anydisp);
a775efc8 8371 i.types[op].bitfield.disp32 = 1;
29b0f896 8372 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8373 i.rex |= REX_X;
29b0f896
AM
8374 }
8375 }
8376 /* RIP addressing for 64bit mode. */
e968fc9b 8377 else if (i.base_reg->reg_num == RegIP)
29b0f896 8378 {
63112cd6 8379 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896 8380 i.rm.regmem = NO_BASE_REGISTER;
40fb9820
L
8381 i.types[op].bitfield.disp8 = 0;
8382 i.types[op].bitfield.disp16 = 0;
a775efc8 8383 i.types[op].bitfield.disp32 = 1;
40fb9820 8384 i.types[op].bitfield.disp64 = 0;
71903a11 8385 i.flags[op] |= Operand_PCrel;
20f0a1fc
NC
8386 if (! i.disp_operands)
8387 fake_zero_displacement = 1;
29b0f896 8388 }
dc821c5f 8389 else if (i.base_reg->reg_type.bitfield.word)
29b0f896 8390 {
63112cd6 8391 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896
AM
8392 switch (i.base_reg->reg_num)
8393 {
8394 case 3: /* (%bx) */
8395 if (i.index_reg == 0)
8396 i.rm.regmem = 7;
8397 else /* (%bx,%si) -> 0, or (%bx,%di) -> 1 */
8398 i.rm.regmem = i.index_reg->reg_num - 6;
8399 break;
8400 case 5: /* (%bp) */
5e042380 8401 default_seg = reg_ss;
29b0f896
AM
8402 if (i.index_reg == 0)
8403 {
8404 i.rm.regmem = 6;
40fb9820 8405 if (operand_type_check (i.types[op], disp) == 0)
29b0f896
AM
8406 {
8407 /* fake (%bp) into 0(%bp) */
41eb8e88 8408 if (i.disp_encoding == disp_encoding_16bit)
1a02d6b0
L
8409 i.types[op].bitfield.disp16 = 1;
8410 else
8411 i.types[op].bitfield.disp8 = 1;
252b5132 8412 fake_zero_displacement = 1;
29b0f896
AM
8413 }
8414 }
8415 else /* (%bp,%si) -> 2, or (%bp,%di) -> 3 */
8416 i.rm.regmem = i.index_reg->reg_num - 6 + 2;
8417 break;
8418 default: /* (%si) -> 4 or (%di) -> 5 */
8419 i.rm.regmem = i.base_reg->reg_num - 6 + 4;
8420 }
41eb8e88
L
8421 if (!fake_zero_displacement
8422 && !i.disp_operands
8423 && i.disp_encoding)
8424 {
8425 fake_zero_displacement = 1;
8426 if (i.disp_encoding == disp_encoding_8bit)
8427 i.types[op].bitfield.disp8 = 1;
8428 else
8429 i.types[op].bitfield.disp16 = 1;
8430 }
29b0f896
AM
8431 i.rm.mode = mode_from_disp_size (i.types[op]);
8432 }
8433 else /* i.base_reg and 32/64 bit mode */
8434 {
a9aabc23 8435 if (operand_type_check (i.types[op], disp))
40fb9820 8436 {
73053c1f
JB
8437 i.types[op].bitfield.disp16 = 0;
8438 i.types[op].bitfield.disp64 = 0;
a775efc8 8439 i.types[op].bitfield.disp32 = 1;
40fb9820 8440 }
20f0a1fc 8441
63112cd6 8442 if (!i.tm.opcode_modifier.sib)
6c30d220 8443 i.rm.regmem = i.base_reg->reg_num;
29b0f896 8444 if ((i.base_reg->reg_flags & RegRex) != 0)
161a04f6 8445 i.rex |= REX_B;
29b0f896
AM
8446 i.sib.base = i.base_reg->reg_num;
8447 /* x86-64 ignores REX prefix bit here to avoid decoder
8448 complications. */
848930b2
JB
8449 if (!(i.base_reg->reg_flags & RegRex)
8450 && (i.base_reg->reg_num == EBP_REG_NUM
8451 || i.base_reg->reg_num == ESP_REG_NUM))
5e042380 8452 default_seg = reg_ss;
848930b2 8453 if (i.base_reg->reg_num == 5 && i.disp_operands == 0)
29b0f896 8454 {
848930b2 8455 fake_zero_displacement = 1;
1a02d6b0
L
8456 if (i.disp_encoding == disp_encoding_32bit)
8457 i.types[op].bitfield.disp32 = 1;
8458 else
8459 i.types[op].bitfield.disp8 = 1;
29b0f896
AM
8460 }
8461 i.sib.scale = i.log2_scale_factor;
8462 if (i.index_reg == 0)
8463 {
260cd341
LC
8464 /* Only check for VSIB. */
8465 gas_assert (i.tm.opcode_modifier.sib != VECSIB128
8466 && i.tm.opcode_modifier.sib != VECSIB256
8467 && i.tm.opcode_modifier.sib != VECSIB512);
8468
29b0f896
AM
8469 /* <disp>(%esp) becomes two byte modrm with no index
8470 register. We've already stored the code for esp
8471 in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
8472 Any base register besides %esp will not use the
8473 extra modrm byte. */
8474 i.sib.index = NO_INDEX_REGISTER;
29b0f896 8475 }
63112cd6 8476 else if (!i.tm.opcode_modifier.sib)
29b0f896 8477 {
e968fc9b 8478 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8479 i.sib.index = NO_INDEX_REGISTER;
8480 else
8481 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8482 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8483 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8484 i.rex |= REX_X;
29b0f896 8485 }
67a4f2b7
AO
8486
8487 if (i.disp_operands
8488 && (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
8489 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL))
8490 i.rm.mode = 0;
8491 else
a501d77e
L
8492 {
8493 if (!fake_zero_displacement
8494 && !i.disp_operands
8495 && i.disp_encoding)
8496 {
8497 fake_zero_displacement = 1;
8498 if (i.disp_encoding == disp_encoding_8bit)
8499 i.types[op].bitfield.disp8 = 1;
8500 else
8501 i.types[op].bitfield.disp32 = 1;
8502 }
8503 i.rm.mode = mode_from_disp_size (i.types[op]);
8504 }
29b0f896 8505 }
252b5132 8506
29b0f896
AM
8507 if (fake_zero_displacement)
8508 {
8509 /* Fakes a zero displacement assuming that i.types[op]
8510 holds the correct displacement size. */
8511 expressionS *exp;
8512
9c2799c2 8513 gas_assert (i.op[op].disps == 0);
29b0f896
AM
8514 exp = &disp_expressions[i.disp_operands++];
8515 i.op[op].disps = exp;
8516 exp->X_op = O_constant;
8517 exp->X_add_number = 0;
8518 exp->X_add_symbol = (symbolS *) 0;
8519 exp->X_op_symbol = (symbolS *) 0;
8520 }
c0f3af97
L
8521
8522 mem = op;
29b0f896 8523 }
c0f3af97
L
8524 else
8525 mem = ~0;
252b5132 8526
8c43a48b 8527 if (i.tm.opcode_modifier.vexsources == XOP2SOURCES)
5dd85c99
SP
8528 {
8529 if (operand_type_check (i.types[0], imm))
8530 i.vex.register_specifier = NULL;
8531 else
8532 {
8533 /* VEX.vvvv encodes one of the sources when the first
8534 operand is not an immediate. */
1ef99a7b 8535 if (i.tm.opcode_modifier.vexw == VEXW0)
5dd85c99
SP
8536 i.vex.register_specifier = i.op[0].regs;
8537 else
8538 i.vex.register_specifier = i.op[1].regs;
8539 }
8540
8541 /* Destination is a XMM register encoded in the ModRM.reg
8542 and VEX.R bit. */
8543 i.rm.reg = i.op[2].regs->reg_num;
8544 if ((i.op[2].regs->reg_flags & RegRex) != 0)
8545 i.rex |= REX_R;
8546
8547 /* ModRM.rm and VEX.B encodes the other source. */
8548 if (!i.mem_operands)
8549 {
8550 i.rm.mode = 3;
8551
1ef99a7b 8552 if (i.tm.opcode_modifier.vexw == VEXW0)
5dd85c99
SP
8553 i.rm.regmem = i.op[1].regs->reg_num;
8554 else
8555 i.rm.regmem = i.op[0].regs->reg_num;
8556
8557 if ((i.op[1].regs->reg_flags & RegRex) != 0)
8558 i.rex |= REX_B;
8559 }
8560 }
2426c15f 8561 else if (i.tm.opcode_modifier.vexvvvv == VEXLWP)
f88c9eb0
SP
8562 {
8563 i.vex.register_specifier = i.op[2].regs;
8564 if (!i.mem_operands)
8565 {
8566 i.rm.mode = 3;
8567 i.rm.regmem = i.op[1].regs->reg_num;
8568 if ((i.op[1].regs->reg_flags & RegRex) != 0)
8569 i.rex |= REX_B;
8570 }
8571 }
29b0f896
AM
8572 /* Fill in i.rm.reg or i.rm.regmem field with register operand
8573 (if any) based on i.tm.extension_opcode. Again, we must be
8574 careful to make sure that segment/control/debug/test/MMX
8575 registers are coded into the i.rm.reg field. */
f88c9eb0 8576 else if (i.reg_operands)
29b0f896 8577 {
99018f42 8578 unsigned int op;
7ab9ffdd
L
8579 unsigned int vex_reg = ~0;
8580
8581 for (op = 0; op < i.operands; op++)
921eafea
L
8582 if (i.types[op].bitfield.class == Reg
8583 || i.types[op].bitfield.class == RegBND
8584 || i.types[op].bitfield.class == RegMask
8585 || i.types[op].bitfield.class == SReg
8586 || i.types[op].bitfield.class == RegCR
8587 || i.types[op].bitfield.class == RegDR
8588 || i.types[op].bitfield.class == RegTR
8589 || i.types[op].bitfield.class == RegSIMD
8590 || i.types[op].bitfield.class == RegMMX)
8591 break;
c0209578 8592
7ab9ffdd
L
8593 if (vex_3_sources)
8594 op = dest;
2426c15f 8595 else if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
7ab9ffdd
L
8596 {
8597 /* For instructions with VexNDS, the register-only
8598 source operand is encoded in VEX prefix. */
8599 gas_assert (mem != (unsigned int) ~0);
c0f3af97 8600
a93e3234 8601 if (op > mem || i.tm.cpu_flags.bitfield.cpucmpccxadd)
c0f3af97 8602 {
7ab9ffdd
L
8603 vex_reg = op++;
8604 gas_assert (op < i.operands);
c0f3af97
L
8605 }
8606 else
c0f3af97 8607 {
f12dc422
L
8608 /* Check register-only source operand when two source
8609 operands are swapped. */
8610 if (!i.tm.operand_types[op].bitfield.baseindex
8611 && i.tm.operand_types[op + 1].bitfield.baseindex)
8612 {
8613 vex_reg = op;
8614 op += 2;
8615 gas_assert (mem == (vex_reg + 1)
8616 && op < i.operands);
8617 }
8618 else
8619 {
8620 vex_reg = op + 1;
8621 gas_assert (vex_reg < i.operands);
8622 }
c0f3af97 8623 }
7ab9ffdd 8624 }
2426c15f 8625 else if (i.tm.opcode_modifier.vexvvvv == VEXNDD)
7ab9ffdd 8626 {
f12dc422 8627 /* For instructions with VexNDD, the register destination
7ab9ffdd 8628 is encoded in VEX prefix. */
f12dc422
L
8629 if (i.mem_operands == 0)
8630 {
8631 /* There is no memory operand. */
8632 gas_assert ((op + 2) == i.operands);
8633 vex_reg = op + 1;
8634 }
8635 else
8d63c93e 8636 {
ed438a93
JB
8637 /* There are only 2 non-immediate operands. */
8638 gas_assert (op < i.imm_operands + 2
8639 && i.operands == i.imm_operands + 2);
8640 vex_reg = i.imm_operands + 1;
f12dc422 8641 }
7ab9ffdd
L
8642 }
8643 else
8644 gas_assert (op < i.operands);
99018f42 8645
7ab9ffdd
L
8646 if (vex_reg != (unsigned int) ~0)
8647 {
f12dc422 8648 i386_operand_type *type = &i.tm.operand_types[vex_reg];
7ab9ffdd 8649
bab6aec1 8650 if ((type->bitfield.class != Reg
dc821c5f 8651 || (!type->bitfield.dword && !type->bitfield.qword))
3528c362 8652 && type->bitfield.class != RegSIMD
c0f327b8 8653 && type->bitfield.class != RegMask)
7ab9ffdd 8654 abort ();
f88c9eb0 8655
7ab9ffdd
L
8656 i.vex.register_specifier = i.op[vex_reg].regs;
8657 }
8658
1b9f0c97
L
8659 /* Don't set OP operand twice. */
8660 if (vex_reg != op)
7ab9ffdd 8661 {
1b9f0c97
L
8662 /* If there is an extension opcode to put here, the
8663 register number must be put into the regmem field. */
8664 if (i.tm.extension_opcode != None)
8665 {
8666 i.rm.regmem = i.op[op].regs->reg_num;
a5aeccd9
JB
8667 set_rex_vrex (i.op[op].regs, REX_B,
8668 i.tm.opcode_modifier.sse2avx);
1b9f0c97
L
8669 }
8670 else
8671 {
8672 i.rm.reg = i.op[op].regs->reg_num;
a5aeccd9
JB
8673 set_rex_vrex (i.op[op].regs, REX_R,
8674 i.tm.opcode_modifier.sse2avx);
1b9f0c97 8675 }
7ab9ffdd 8676 }
252b5132 8677
29b0f896
AM
8678 /* Now, if no memory operand has set i.rm.mode = 0, 1, 2 we
8679 must set it to 3 to indicate this is a register operand
8680 in the regmem field. */
8681 if (!i.mem_operands)
8682 i.rm.mode = 3;
8683 }
252b5132 8684
29b0f896 8685 /* Fill in i.rm.reg field with extension opcode (if any). */
c1e679ec 8686 if (i.tm.extension_opcode != None)
29b0f896
AM
8687 i.rm.reg = i.tm.extension_opcode;
8688 }
8689 return default_seg;
8690}
252b5132 8691
48ef937e
JB
8692static INLINE void
8693frag_opcode_byte (unsigned char byte)
8694{
8695 if (now_seg != absolute_section)
8696 FRAG_APPEND_1_CHAR (byte);
8697 else
8698 ++abs_section_offset;
8699}
8700
376cd056
JB
8701static unsigned int
8702flip_code16 (unsigned int code16)
8703{
8704 gas_assert (i.tm.operands == 1);
8705
8706 return !(i.prefix[REX_PREFIX] & REX_W)
8707 && (code16 ? i.tm.operand_types[0].bitfield.disp32
376cd056
JB
8708 : i.tm.operand_types[0].bitfield.disp16)
8709 ? CODE16 : 0;
8710}
8711
29b0f896 8712static void
e3bb37b5 8713output_branch (void)
29b0f896
AM
8714{
8715 char *p;
f8a5c266 8716 int size;
29b0f896
AM
8717 int code16;
8718 int prefix;
8719 relax_substateT subtype;
8720 symbolS *sym;
8721 offsetT off;
8722
48ef937e
JB
8723 if (now_seg == absolute_section)
8724 {
8725 as_bad (_("relaxable branches not supported in absolute section"));
8726 return;
8727 }
8728
f8a5c266 8729 code16 = flag_code == CODE_16BIT ? CODE16 : 0;
1a42a9fe 8730 size = i.disp_encoding > disp_encoding_8bit ? BIG : SMALL;
29b0f896
AM
8731
8732 prefix = 0;
8733 if (i.prefix[DATA_PREFIX] != 0)
252b5132 8734 {
29b0f896
AM
8735 prefix = 1;
8736 i.prefixes -= 1;
376cd056 8737 code16 ^= flip_code16(code16);
252b5132 8738 }
29b0f896
AM
8739 /* Pentium4 branch hints. */
8740 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8741 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
2f66722d 8742 {
29b0f896
AM
8743 prefix++;
8744 i.prefixes--;
8745 }
8746 if (i.prefix[REX_PREFIX] != 0)
8747 {
8748 prefix++;
8749 i.prefixes--;
2f66722d
AM
8750 }
8751
7e8b059b
L
8752 /* BND prefixed jump. */
8753 if (i.prefix[BND_PREFIX] != 0)
8754 {
6cb0a70e
JB
8755 prefix++;
8756 i.prefixes--;
7e8b059b
L
8757 }
8758
f2810fe0
JB
8759 if (i.prefixes != 0)
8760 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
29b0f896
AM
8761
8762 /* It's always a symbol; End frag & setup for relax.
8763 Make sure there is enough room in this frag for the largest
8764 instruction we may generate in md_convert_frag. This is 2
8765 bytes for the opcode and room for the prefix and largest
8766 displacement. */
8767 frag_grow (prefix + 2 + 4);
8768 /* Prefix and 1 opcode byte go in fr_fix. */
8769 p = frag_more (prefix + 1);
8770 if (i.prefix[DATA_PREFIX] != 0)
8771 *p++ = DATA_PREFIX_OPCODE;
8772 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
8773 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
8774 *p++ = i.prefix[SEG_PREFIX];
6cb0a70e
JB
8775 if (i.prefix[BND_PREFIX] != 0)
8776 *p++ = BND_PREFIX_OPCODE;
29b0f896
AM
8777 if (i.prefix[REX_PREFIX] != 0)
8778 *p++ = i.prefix[REX_PREFIX];
8779 *p = i.tm.base_opcode;
8780
8781 if ((unsigned char) *p == JUMP_PC_RELATIVE)
f8a5c266 8782 subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, size);
40fb9820 8783 else if (cpu_arch_flags.bitfield.cpui386)
f8a5c266 8784 subtype = ENCODE_RELAX_STATE (COND_JUMP, size);
29b0f896 8785 else
f8a5c266 8786 subtype = ENCODE_RELAX_STATE (COND_JUMP86, size);
29b0f896 8787 subtype |= code16;
3e73aa7c 8788
29b0f896
AM
8789 sym = i.op[0].disps->X_add_symbol;
8790 off = i.op[0].disps->X_add_number;
3e73aa7c 8791
29b0f896
AM
8792 if (i.op[0].disps->X_op != O_constant
8793 && i.op[0].disps->X_op != O_symbol)
3e73aa7c 8794 {
29b0f896
AM
8795 /* Handle complex expressions. */
8796 sym = make_expr_symbol (i.op[0].disps);
8797 off = 0;
8798 }
3e73aa7c 8799
1ef3994a
JB
8800 frag_now->tc_frag_data.code64 = flag_code == CODE_64BIT;
8801
29b0f896
AM
8802 /* 1 possible extra opcode + 4 byte displacement go in var part.
8803 Pass reloc in fr_var. */
d258b828 8804 frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
29b0f896 8805}
3e73aa7c 8806
bd7ab16b
L
8807#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8808/* Return TRUE iff PLT32 relocation should be used for branching to
8809 symbol S. */
8810
5b7c81bd 8811static bool
bd7ab16b
L
8812need_plt32_p (symbolS *s)
8813{
8814 /* PLT32 relocation is ELF only. */
8815 if (!IS_ELF)
5b7c81bd 8816 return false;
bd7ab16b 8817
a5def729
RO
8818#ifdef TE_SOLARIS
8819 /* Don't emit PLT32 relocation on Solaris: neither native linker nor
8820 krtld support it. */
5b7c81bd 8821 return false;
a5def729
RO
8822#endif
8823
bd7ab16b
L
8824 /* Since there is no need to prepare for PLT branch on x86-64, we
8825 can generate R_X86_64_PLT32, instead of R_X86_64_PC32, which can
8826 be used as a marker for 32-bit PC-relative branches. */
8827 if (!object_64bit)
5b7c81bd 8828 return false;
bd7ab16b 8829
44365e88 8830 if (s == NULL)
5b7c81bd 8831 return false;
44365e88 8832
bd7ab16b
L
8833 /* Weak or undefined symbol need PLT32 relocation. */
8834 if (S_IS_WEAK (s) || !S_IS_DEFINED (s))
5b7c81bd 8835 return true;
bd7ab16b
L
8836
8837 /* Non-global symbol doesn't need PLT32 relocation. */
8838 if (! S_IS_EXTERNAL (s))
5b7c81bd 8839 return false;
bd7ab16b
L
8840
8841 /* Other global symbols need PLT32 relocation. NB: Symbol with
8842 non-default visibilities are treated as normal global symbol
8843 so that PLT32 relocation can be used as a marker for 32-bit
8844 PC-relative branches. It is useful for linker relaxation. */
5b7c81bd 8845 return true;
bd7ab16b
L
8846}
8847#endif
8848
29b0f896 8849static void
e3bb37b5 8850output_jump (void)
29b0f896
AM
8851{
8852 char *p;
8853 int size;
3e02c1cc 8854 fixS *fixP;
bd7ab16b 8855 bfd_reloc_code_real_type jump_reloc = i.reloc[0];
29b0f896 8856
0cfa3eb3 8857 if (i.tm.opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
8858 {
8859 /* This is a loop or jecxz type instruction. */
8860 size = 1;
8861 if (i.prefix[ADDR_PREFIX] != 0)
8862 {
48ef937e 8863 frag_opcode_byte (ADDR_PREFIX_OPCODE);
29b0f896
AM
8864 i.prefixes -= 1;
8865 }
8866 /* Pentium4 branch hints. */
8867 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8868 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
8869 {
48ef937e 8870 frag_opcode_byte (i.prefix[SEG_PREFIX]);
29b0f896 8871 i.prefixes--;
3e73aa7c
JH
8872 }
8873 }
29b0f896
AM
8874 else
8875 {
8876 int code16;
3e73aa7c 8877
29b0f896
AM
8878 code16 = 0;
8879 if (flag_code == CODE_16BIT)
8880 code16 = CODE16;
3e73aa7c 8881
29b0f896
AM
8882 if (i.prefix[DATA_PREFIX] != 0)
8883 {
48ef937e 8884 frag_opcode_byte (DATA_PREFIX_OPCODE);
29b0f896 8885 i.prefixes -= 1;
376cd056 8886 code16 ^= flip_code16(code16);
29b0f896 8887 }
252b5132 8888
29b0f896
AM
8889 size = 4;
8890 if (code16)
8891 size = 2;
8892 }
9fcc94b6 8893
6cb0a70e
JB
8894 /* BND prefixed jump. */
8895 if (i.prefix[BND_PREFIX] != 0)
29b0f896 8896 {
48ef937e 8897 frag_opcode_byte (i.prefix[BND_PREFIX]);
29b0f896
AM
8898 i.prefixes -= 1;
8899 }
252b5132 8900
6cb0a70e 8901 if (i.prefix[REX_PREFIX] != 0)
7e8b059b 8902 {
48ef937e 8903 frag_opcode_byte (i.prefix[REX_PREFIX]);
7e8b059b
L
8904 i.prefixes -= 1;
8905 }
8906
f2810fe0
JB
8907 if (i.prefixes != 0)
8908 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
e0890092 8909
48ef937e
JB
8910 if (now_seg == absolute_section)
8911 {
9a182d04 8912 abs_section_offset += i.opcode_length + size;
48ef937e
JB
8913 return;
8914 }
8915
9a182d04
JB
8916 p = frag_more (i.opcode_length + size);
8917 switch (i.opcode_length)
42164a71
L
8918 {
8919 case 2:
8920 *p++ = i.tm.base_opcode >> 8;
1a0670f3 8921 /* Fall through. */
42164a71
L
8922 case 1:
8923 *p++ = i.tm.base_opcode;
8924 break;
8925 default:
8926 abort ();
8927 }
e0890092 8928
bd7ab16b 8929#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1ef3994a
JB
8930 if (flag_code == CODE_64BIT && size == 4
8931 && jump_reloc == NO_RELOC && i.op[0].disps->X_add_number == 0
bd7ab16b
L
8932 && need_plt32_p (i.op[0].disps->X_add_symbol))
8933 jump_reloc = BFD_RELOC_X86_64_PLT32;
8934#endif
8935
8936 jump_reloc = reloc (size, 1, 1, jump_reloc);
8937
3e02c1cc 8938 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
bd7ab16b 8939 i.op[0].disps, 1, jump_reloc);
3e02c1cc 8940
eb19308f
JB
8941 /* All jumps handled here are signed, but don't unconditionally use a
8942 signed limit check for 32 and 16 bit jumps as we want to allow wrap
8943 around at 4G (outside of 64-bit mode) and 64k (except for XBEGIN)
8944 respectively. */
8945 switch (size)
8946 {
8947 case 1:
8948 fixP->fx_signed = 1;
8949 break;
8950
8951 case 2:
8952 if (i.tm.base_opcode == 0xc7f8)
8953 fixP->fx_signed = 1;
8954 break;
8955
8956 case 4:
8957 if (flag_code == CODE_64BIT)
8958 fixP->fx_signed = 1;
8959 break;
8960 }
29b0f896 8961}
e0890092 8962
29b0f896 8963static void
e3bb37b5 8964output_interseg_jump (void)
29b0f896
AM
8965{
8966 char *p;
8967 int size;
8968 int prefix;
8969 int code16;
252b5132 8970
29b0f896
AM
8971 code16 = 0;
8972 if (flag_code == CODE_16BIT)
8973 code16 = CODE16;
a217f122 8974
29b0f896
AM
8975 prefix = 0;
8976 if (i.prefix[DATA_PREFIX] != 0)
8977 {
8978 prefix = 1;
8979 i.prefixes -= 1;
8980 code16 ^= CODE16;
8981 }
6cb0a70e
JB
8982
8983 gas_assert (!i.prefix[REX_PREFIX]);
252b5132 8984
29b0f896
AM
8985 size = 4;
8986 if (code16)
8987 size = 2;
252b5132 8988
f2810fe0
JB
8989 if (i.prefixes != 0)
8990 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
252b5132 8991
48ef937e
JB
8992 if (now_seg == absolute_section)
8993 {
8994 abs_section_offset += prefix + 1 + 2 + size;
8995 return;
8996 }
8997
29b0f896
AM
8998 /* 1 opcode; 2 segment; offset */
8999 p = frag_more (prefix + 1 + 2 + size);
3e73aa7c 9000
29b0f896
AM
9001 if (i.prefix[DATA_PREFIX] != 0)
9002 *p++ = DATA_PREFIX_OPCODE;
252b5132 9003
29b0f896
AM
9004 if (i.prefix[REX_PREFIX] != 0)
9005 *p++ = i.prefix[REX_PREFIX];
252b5132 9006
29b0f896
AM
9007 *p++ = i.tm.base_opcode;
9008 if (i.op[1].imms->X_op == O_constant)
9009 {
9010 offsetT n = i.op[1].imms->X_add_number;
252b5132 9011
29b0f896
AM
9012 if (size == 2
9013 && !fits_in_unsigned_word (n)
9014 && !fits_in_signed_word (n))
9015 {
9016 as_bad (_("16-bit jump out of range"));
9017 return;
9018 }
9019 md_number_to_chars (p, n, size);
9020 }
9021 else
9022 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
d258b828 9023 i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
6d96a594
C
9024
9025 p += size;
9026 if (i.op[0].imms->X_op == O_constant)
9027 md_number_to_chars (p, (valueT) i.op[0].imms->X_add_number, 2);
9028 else
9029 fix_new_exp (frag_now, p - frag_now->fr_literal, 2,
9030 i.op[0].imms, 0, reloc (2, 0, 0, i.reloc[0]));
29b0f896 9031}
a217f122 9032
b4a3a7b4
L
9033#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9034void
9035x86_cleanup (void)
9036{
9037 char *p;
9038 asection *seg = now_seg;
9039 subsegT subseg = now_subseg;
9040 asection *sec;
9041 unsigned int alignment, align_size_1;
9042 unsigned int isa_1_descsz, feature_2_descsz, descsz;
9043 unsigned int isa_1_descsz_raw, feature_2_descsz_raw;
9044 unsigned int padding;
9045
1273b2f8 9046 if (!IS_ELF || !x86_used_note)
b4a3a7b4
L
9047 return;
9048
b4a3a7b4
L
9049 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X86;
9050
9051 /* The .note.gnu.property section layout:
9052
9053 Field Length Contents
9054 ---- ---- ----
9055 n_namsz 4 4
9056 n_descsz 4 The note descriptor size
9057 n_type 4 NT_GNU_PROPERTY_TYPE_0
9058 n_name 4 "GNU"
9059 n_desc n_descsz The program property array
9060 .... .... ....
9061 */
9062
9063 /* Create the .note.gnu.property section. */
9064 sec = subseg_new (NOTE_GNU_PROPERTY_SECTION_NAME, 0);
fd361982 9065 bfd_set_section_flags (sec,
b4a3a7b4
L
9066 (SEC_ALLOC
9067 | SEC_LOAD
9068 | SEC_DATA
9069 | SEC_HAS_CONTENTS
9070 | SEC_READONLY));
9071
9072 if (get_elf_backend_data (stdoutput)->s->elfclass == ELFCLASS64)
9073 {
9074 align_size_1 = 7;
9075 alignment = 3;
9076 }
9077 else
9078 {
9079 align_size_1 = 3;
9080 alignment = 2;
9081 }
9082
fd361982 9083 bfd_set_section_alignment (sec, alignment);
b4a3a7b4
L
9084 elf_section_type (sec) = SHT_NOTE;
9085
1273b2f8
L
9086 /* GNU_PROPERTY_X86_ISA_1_USED: 4-byte type + 4-byte data size
9087 + 4-byte data */
9088 isa_1_descsz_raw = 4 + 4 + 4;
9089 /* Align GNU_PROPERTY_X86_ISA_1_USED. */
9090 isa_1_descsz = (isa_1_descsz_raw + align_size_1) & ~align_size_1;
b4a3a7b4
L
9091
9092 feature_2_descsz_raw = isa_1_descsz;
9093 /* GNU_PROPERTY_X86_FEATURE_2_USED: 4-byte type + 4-byte data size
1273b2f8 9094 + 4-byte data */
b4a3a7b4
L
9095 feature_2_descsz_raw += 4 + 4 + 4;
9096 /* Align GNU_PROPERTY_X86_FEATURE_2_USED. */
9097 feature_2_descsz = ((feature_2_descsz_raw + align_size_1)
9098 & ~align_size_1);
9099
9100 descsz = feature_2_descsz;
9101 /* Section size: n_namsz + n_descsz + n_type + n_name + n_descsz. */
9102 p = frag_more (4 + 4 + 4 + 4 + descsz);
9103
9104 /* Write n_namsz. */
9105 md_number_to_chars (p, (valueT) 4, 4);
9106
9107 /* Write n_descsz. */
9108 md_number_to_chars (p + 4, (valueT) descsz, 4);
9109
9110 /* Write n_type. */
9111 md_number_to_chars (p + 4 * 2, (valueT) NT_GNU_PROPERTY_TYPE_0, 4);
9112
9113 /* Write n_name. */
9114 memcpy (p + 4 * 3, "GNU", 4);
9115
1273b2f8
L
9116 /* Write 4-byte type. */
9117 md_number_to_chars (p + 4 * 4,
9118 (valueT) GNU_PROPERTY_X86_ISA_1_USED, 4);
b4a3a7b4 9119
1273b2f8
L
9120 /* Write 4-byte data size. */
9121 md_number_to_chars (p + 4 * 5, (valueT) 4, 4);
b4a3a7b4 9122
1273b2f8
L
9123 /* Write 4-byte data. */
9124 md_number_to_chars (p + 4 * 6, (valueT) x86_isa_1_used, 4);
b4a3a7b4 9125
1273b2f8
L
9126 /* Zero out paddings. */
9127 padding = isa_1_descsz - isa_1_descsz_raw;
9128 if (padding)
9129 memset (p + 4 * 7, 0, padding);
b4a3a7b4
L
9130
9131 /* Write 4-byte type. */
9132 md_number_to_chars (p + isa_1_descsz + 4 * 4,
9133 (valueT) GNU_PROPERTY_X86_FEATURE_2_USED, 4);
9134
9135 /* Write 4-byte data size. */
9136 md_number_to_chars (p + isa_1_descsz + 4 * 5, (valueT) 4, 4);
9137
9138 /* Write 4-byte data. */
9139 md_number_to_chars (p + isa_1_descsz + 4 * 6,
9140 (valueT) x86_feature_2_used, 4);
9141
9142 /* Zero out paddings. */
9143 padding = feature_2_descsz - feature_2_descsz_raw;
9144 if (padding)
9145 memset (p + isa_1_descsz + 4 * 7, 0, padding);
9146
9147 /* We probably can't restore the current segment, for there likely
9148 isn't one yet... */
9149 if (seg && subseg)
9150 subseg_set (seg, subseg);
9151}
b52c4ee4
IB
9152
9153bool
9154x86_support_sframe_p (void)
9155{
9156 /* At this time, SFrame unwind is supported for AMD64 ABI only. */
9157 return (x86_elf_abi == X86_64_ABI);
9158}
9159
9160bool
9161x86_sframe_ra_tracking_p (void)
9162{
9163 /* In AMD64, return address is always stored on the stack at a fixed offset
9164 from the CFA (provided via x86_sframe_cfa_ra_offset ()).
9165 Do not track explicitly via an SFrame Frame Row Entry. */
9166 return false;
9167}
9168
9169offsetT
9170x86_sframe_cfa_ra_offset (void)
9171{
9172 gas_assert (x86_elf_abi == X86_64_ABI);
9173 return (offsetT) -8;
9174}
9175
9176unsigned char
9177x86_sframe_get_abi_arch (void)
9178{
9179 unsigned char sframe_abi_arch = 0;
9180
9181 if (x86_support_sframe_p ())
9182 {
9183 gas_assert (!target_big_endian);
9184 sframe_abi_arch = SFRAME_ABI_AMD64_ENDIAN_LITTLE;
9185 }
9186
9187 return sframe_abi_arch;
9188}
9189
b4a3a7b4
L
9190#endif
9191
9c33702b
JB
9192static unsigned int
9193encoding_length (const fragS *start_frag, offsetT start_off,
9194 const char *frag_now_ptr)
9195{
9196 unsigned int len = 0;
9197
9198 if (start_frag != frag_now)
9199 {
9200 const fragS *fr = start_frag;
9201
9202 do {
9203 len += fr->fr_fix;
9204 fr = fr->fr_next;
9205 } while (fr && fr != frag_now);
9206 }
9207
9208 return len - start_off + (frag_now_ptr - frag_now->fr_literal);
9209}
9210
e379e5f3 9211/* Return 1 for test, and, cmp, add, sub, inc and dec which may
79d72f45
HL
9212 be macro-fused with conditional jumps.
9213 NB: If TEST/AND/CMP/ADD/SUB/INC/DEC is of RIP relative address,
9214 or is one of the following format:
9215
9216 cmp m, imm
9217 add m, imm
9218 sub m, imm
9219 test m, imm
9220 and m, imm
9221 inc m
9222 dec m
9223
9224 it is unfusible. */
e379e5f3
L
9225
9226static int
79d72f45 9227maybe_fused_with_jcc_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
9228{
9229 /* No RIP address. */
9230 if (i.base_reg && i.base_reg->reg_num == RegIP)
9231 return 0;
9232
389d00a5
JB
9233 /* No opcodes outside of base encoding space. */
9234 if (i.tm.opcode_modifier.opcodespace != SPACE_BASE)
e379e5f3
L
9235 return 0;
9236
79d72f45
HL
9237 /* add, sub without add/sub m, imm. */
9238 if (i.tm.base_opcode <= 5
e379e5f3
L
9239 || (i.tm.base_opcode >= 0x28 && i.tm.base_opcode <= 0x2d)
9240 || ((i.tm.base_opcode | 3) == 0x83
79d72f45 9241 && (i.tm.extension_opcode == 0x5
e379e5f3 9242 || i.tm.extension_opcode == 0x0)))
79d72f45
HL
9243 {
9244 *mf_cmp_p = mf_cmp_alu_cmp;
9245 return !(i.mem_operands && i.imm_operands);
9246 }
e379e5f3 9247
79d72f45
HL
9248 /* and without and m, imm. */
9249 if ((i.tm.base_opcode >= 0x20 && i.tm.base_opcode <= 0x25)
9250 || ((i.tm.base_opcode | 3) == 0x83
9251 && i.tm.extension_opcode == 0x4))
9252 {
9253 *mf_cmp_p = mf_cmp_test_and;
9254 return !(i.mem_operands && i.imm_operands);
9255 }
9256
9257 /* test without test m imm. */
e379e5f3
L
9258 if ((i.tm.base_opcode | 1) == 0x85
9259 || (i.tm.base_opcode | 1) == 0xa9
9260 || ((i.tm.base_opcode | 1) == 0xf7
79d72f45
HL
9261 && i.tm.extension_opcode == 0))
9262 {
9263 *mf_cmp_p = mf_cmp_test_and;
9264 return !(i.mem_operands && i.imm_operands);
9265 }
9266
9267 /* cmp without cmp m, imm. */
9268 if ((i.tm.base_opcode >= 0x38 && i.tm.base_opcode <= 0x3d)
e379e5f3
L
9269 || ((i.tm.base_opcode | 3) == 0x83
9270 && (i.tm.extension_opcode == 0x7)))
79d72f45
HL
9271 {
9272 *mf_cmp_p = mf_cmp_alu_cmp;
9273 return !(i.mem_operands && i.imm_operands);
9274 }
e379e5f3 9275
79d72f45 9276 /* inc, dec without inc/dec m. */
e379e5f3
L
9277 if ((i.tm.cpu_flags.bitfield.cpuno64
9278 && (i.tm.base_opcode | 0xf) == 0x4f)
9279 || ((i.tm.base_opcode | 1) == 0xff
9280 && i.tm.extension_opcode <= 0x1))
79d72f45
HL
9281 {
9282 *mf_cmp_p = mf_cmp_incdec;
9283 return !i.mem_operands;
9284 }
e379e5f3
L
9285
9286 return 0;
9287}
9288
9289/* Return 1 if a FUSED_JCC_PADDING frag should be generated. */
9290
9291static int
79d72f45 9292add_fused_jcc_padding_frag_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
9293{
9294 /* NB: Don't work with COND_JUMP86 without i386. */
9295 if (!align_branch_power
9296 || now_seg == absolute_section
9297 || !cpu_arch_flags.bitfield.cpui386
9298 || !(align_branch & align_branch_fused_bit))
9299 return 0;
9300
79d72f45 9301 if (maybe_fused_with_jcc_p (mf_cmp_p))
e379e5f3
L
9302 {
9303 if (last_insn.kind == last_insn_other
9304 || last_insn.seg != now_seg)
9305 return 1;
9306 if (flag_debug)
9307 as_warn_where (last_insn.file, last_insn.line,
9308 _("`%s` skips -malign-branch-boundary on `%s`"),
9309 last_insn.name, i.tm.name);
9310 }
9311
9312 return 0;
9313}
9314
9315/* Return 1 if a BRANCH_PREFIX frag should be generated. */
9316
9317static int
9318add_branch_prefix_frag_p (void)
9319{
9320 /* NB: Don't work with COND_JUMP86 without i386. Don't add prefix
9321 to PadLock instructions since they include prefixes in opcode. */
9322 if (!align_branch_power
9323 || !align_branch_prefix_size
9324 || now_seg == absolute_section
9325 || i.tm.cpu_flags.bitfield.cpupadlock
9326 || !cpu_arch_flags.bitfield.cpui386)
9327 return 0;
9328
9329 /* Don't add prefix if it is a prefix or there is no operand in case
9330 that segment prefix is special. */
9331 if (!i.operands || i.tm.opcode_modifier.isprefix)
9332 return 0;
9333
9334 if (last_insn.kind == last_insn_other
9335 || last_insn.seg != now_seg)
9336 return 1;
9337
9338 if (flag_debug)
9339 as_warn_where (last_insn.file, last_insn.line,
9340 _("`%s` skips -malign-branch-boundary on `%s`"),
9341 last_insn.name, i.tm.name);
9342
9343 return 0;
9344}
9345
9346/* Return 1 if a BRANCH_PADDING frag should be generated. */
9347
9348static int
79d72f45
HL
9349add_branch_padding_frag_p (enum align_branch_kind *branch_p,
9350 enum mf_jcc_kind *mf_jcc_p)
e379e5f3
L
9351{
9352 int add_padding;
9353
9354 /* NB: Don't work with COND_JUMP86 without i386. */
9355 if (!align_branch_power
9356 || now_seg == absolute_section
389d00a5
JB
9357 || !cpu_arch_flags.bitfield.cpui386
9358 || i.tm.opcode_modifier.opcodespace != SPACE_BASE)
e379e5f3
L
9359 return 0;
9360
9361 add_padding = 0;
9362
9363 /* Check for jcc and direct jmp. */
9364 if (i.tm.opcode_modifier.jump == JUMP)
9365 {
9366 if (i.tm.base_opcode == JUMP_PC_RELATIVE)
9367 {
9368 *branch_p = align_branch_jmp;
9369 add_padding = align_branch & align_branch_jmp_bit;
9370 }
9371 else
9372 {
79d72f45
HL
9373 /* Because J<cc> and JN<cc> share same group in macro-fusible table,
9374 igore the lowest bit. */
9375 *mf_jcc_p = (i.tm.base_opcode & 0x0e) >> 1;
e379e5f3
L
9376 *branch_p = align_branch_jcc;
9377 if ((align_branch & align_branch_jcc_bit))
9378 add_padding = 1;
9379 }
9380 }
e379e5f3
L
9381 else if ((i.tm.base_opcode | 1) == 0xc3)
9382 {
9383 /* Near ret. */
9384 *branch_p = align_branch_ret;
9385 if ((align_branch & align_branch_ret_bit))
9386 add_padding = 1;
9387 }
9388 else
9389 {
9390 /* Check for indirect jmp, direct and indirect calls. */
9391 if (i.tm.base_opcode == 0xe8)
9392 {
9393 /* Direct call. */
9394 *branch_p = align_branch_call;
9395 if ((align_branch & align_branch_call_bit))
9396 add_padding = 1;
9397 }
9398 else if (i.tm.base_opcode == 0xff
9399 && (i.tm.extension_opcode == 2
9400 || i.tm.extension_opcode == 4))
9401 {
9402 /* Indirect call and jmp. */
9403 *branch_p = align_branch_indirect;
9404 if ((align_branch & align_branch_indirect_bit))
9405 add_padding = 1;
9406 }
9407
9408 if (add_padding
9409 && i.disp_operands
9410 && tls_get_addr
9411 && (i.op[0].disps->X_op == O_symbol
9412 || (i.op[0].disps->X_op == O_subtract
9413 && i.op[0].disps->X_op_symbol == GOT_symbol)))
9414 {
9415 symbolS *s = i.op[0].disps->X_add_symbol;
9416 /* No padding to call to global or undefined tls_get_addr. */
9417 if ((S_IS_EXTERNAL (s) || !S_IS_DEFINED (s))
9418 && strcmp (S_GET_NAME (s), tls_get_addr) == 0)
9419 return 0;
9420 }
9421 }
9422
9423 if (add_padding
9424 && last_insn.kind != last_insn_other
9425 && last_insn.seg == now_seg)
9426 {
9427 if (flag_debug)
9428 as_warn_where (last_insn.file, last_insn.line,
9429 _("`%s` skips -malign-branch-boundary on `%s`"),
9430 last_insn.name, i.tm.name);
9431 return 0;
9432 }
9433
9434 return add_padding;
9435}
9436
29b0f896 9437static void
e3bb37b5 9438output_insn (void)
29b0f896 9439{
2bbd9c25
JJ
9440 fragS *insn_start_frag;
9441 offsetT insn_start_off;
e379e5f3
L
9442 fragS *fragP = NULL;
9443 enum align_branch_kind branch = align_branch_none;
79d72f45
HL
9444 /* The initializer is arbitrary just to avoid uninitialized error.
9445 it's actually either assigned in add_branch_padding_frag_p
9446 or never be used. */
9447 enum mf_jcc_kind mf_jcc = mf_jcc_jo;
2bbd9c25 9448
b4a3a7b4 9449#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
48ef937e 9450 if (IS_ELF && x86_used_note && now_seg != absolute_section)
b4a3a7b4 9451 {
32930e4e
L
9452 if ((i.xstate & xstate_tmm) == xstate_tmm
9453 || i.tm.cpu_flags.bitfield.cpuamx_tile)
9454 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_TMM;
9455
b4a3a7b4
L
9456 if (i.tm.cpu_flags.bitfield.cpu8087
9457 || i.tm.cpu_flags.bitfield.cpu287
9458 || i.tm.cpu_flags.bitfield.cpu387
9459 || i.tm.cpu_flags.bitfield.cpu687
9460 || i.tm.cpu_flags.bitfield.cpufisttp)
9461 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X87;
014d61ea 9462
921eafea 9463 if ((i.xstate & xstate_mmx)
389d00a5
JB
9464 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
9465 && !is_any_vex_encoding (&i.tm)
9466 && (i.tm.base_opcode == 0x77 /* emms */
9467 || i.tm.base_opcode == 0x0e /* femms */)))
b4a3a7b4 9468 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MMX;
014d61ea 9469
32930e4e
L
9470 if (i.index_reg)
9471 {
9472 if (i.index_reg->reg_type.bitfield.zmmword)
9473 i.xstate |= xstate_zmm;
9474 else if (i.index_reg->reg_type.bitfield.ymmword)
9475 i.xstate |= xstate_ymm;
9476 else if (i.index_reg->reg_type.bitfield.xmmword)
9477 i.xstate |= xstate_xmm;
9478 }
014d61ea
JB
9479
9480 /* vzeroall / vzeroupper */
9481 if (i.tm.base_opcode == 0x77 && i.tm.cpu_flags.bitfield.cpuavx)
9482 i.xstate |= xstate_ymm;
9483
c4694f17 9484 if ((i.xstate & xstate_xmm)
389d00a5
JB
9485 /* ldmxcsr / stmxcsr / vldmxcsr / vstmxcsr */
9486 || (i.tm.base_opcode == 0xae
9487 && (i.tm.cpu_flags.bitfield.cpusse
9488 || i.tm.cpu_flags.bitfield.cpuavx))
c4694f17
TG
9489 || i.tm.cpu_flags.bitfield.cpuwidekl
9490 || i.tm.cpu_flags.bitfield.cpukl)
b4a3a7b4 9491 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XMM;
014d61ea 9492
921eafea 9493 if ((i.xstate & xstate_ymm) == xstate_ymm)
b4a3a7b4 9494 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_YMM;
921eafea 9495 if ((i.xstate & xstate_zmm) == xstate_zmm)
b4a3a7b4 9496 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_ZMM;
6225c532 9497 if (i.mask.reg || (i.xstate & xstate_mask) == xstate_mask)
32930e4e 9498 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MASK;
b4a3a7b4
L
9499 if (i.tm.cpu_flags.bitfield.cpufxsr)
9500 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_FXSR;
9501 if (i.tm.cpu_flags.bitfield.cpuxsave)
9502 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVE;
9503 if (i.tm.cpu_flags.bitfield.cpuxsaveopt)
9504 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT;
9505 if (i.tm.cpu_flags.bitfield.cpuxsavec)
9506 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEC;
b0ab0693
L
9507
9508 if (x86_feature_2_used
9509 || i.tm.cpu_flags.bitfield.cpucmov
9510 || i.tm.cpu_flags.bitfield.cpusyscall
389d00a5
JB
9511 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
9512 && i.tm.base_opcode == 0xc7
70e95837 9513 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
b0ab0693
L
9514 && i.tm.extension_opcode == 1) /* cmpxchg8b */)
9515 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_BASELINE;
9516 if (i.tm.cpu_flags.bitfield.cpusse3
9517 || i.tm.cpu_flags.bitfield.cpussse3
9518 || i.tm.cpu_flags.bitfield.cpusse4_1
9519 || i.tm.cpu_flags.bitfield.cpusse4_2
9520 || i.tm.cpu_flags.bitfield.cpucx16
9521 || i.tm.cpu_flags.bitfield.cpupopcnt
9522 /* LAHF-SAHF insns in 64-bit mode. */
9523 || (flag_code == CODE_64BIT
35648716
JB
9524 && (i.tm.base_opcode | 1) == 0x9f
9525 && i.tm.opcode_modifier.opcodespace == SPACE_BASE))
b0ab0693
L
9526 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V2;
9527 if (i.tm.cpu_flags.bitfield.cpuavx
9528 || i.tm.cpu_flags.bitfield.cpuavx2
a9860005
JB
9529 /* Any VEX encoded insns execpt for AVX512F, AVX512BW, AVX512DQ,
9530 XOP, FMA4, LPW, TBM, and AMX. */
b0ab0693
L
9531 || (i.tm.opcode_modifier.vex
9532 && !i.tm.cpu_flags.bitfield.cpuavx512f
9533 && !i.tm.cpu_flags.bitfield.cpuavx512bw
9534 && !i.tm.cpu_flags.bitfield.cpuavx512dq
a9860005
JB
9535 && !i.tm.cpu_flags.bitfield.cpuxop
9536 && !i.tm.cpu_flags.bitfield.cpufma4
b0ab0693
L
9537 && !i.tm.cpu_flags.bitfield.cpulwp
9538 && !i.tm.cpu_flags.bitfield.cputbm
9539 && !(x86_feature_2_used & GNU_PROPERTY_X86_FEATURE_2_TMM))
9540 || i.tm.cpu_flags.bitfield.cpuf16c
9541 || i.tm.cpu_flags.bitfield.cpufma
9542 || i.tm.cpu_flags.bitfield.cpulzcnt
9543 || i.tm.cpu_flags.bitfield.cpumovbe
9544 || i.tm.cpu_flags.bitfield.cpuxsaves
9545 || (x86_feature_2_used
9546 & (GNU_PROPERTY_X86_FEATURE_2_XSAVE
9547 | GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT
9548 | GNU_PROPERTY_X86_FEATURE_2_XSAVEC)) != 0)
9549 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V3;
9550 if (i.tm.cpu_flags.bitfield.cpuavx512f
9551 || i.tm.cpu_flags.bitfield.cpuavx512bw
9552 || i.tm.cpu_flags.bitfield.cpuavx512dq
9553 || i.tm.cpu_flags.bitfield.cpuavx512vl
a9860005
JB
9554 /* Any EVEX encoded insns except for AVX512ER, AVX512PF,
9555 AVX512-4FMAPS, and AVX512-4VNNIW. */
b0ab0693
L
9556 || (i.tm.opcode_modifier.evex
9557 && !i.tm.cpu_flags.bitfield.cpuavx512er
9558 && !i.tm.cpu_flags.bitfield.cpuavx512pf
a9860005 9559 && !i.tm.cpu_flags.bitfield.cpuavx512_4fmaps
b0ab0693
L
9560 && !i.tm.cpu_flags.bitfield.cpuavx512_4vnniw))
9561 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V4;
b4a3a7b4
L
9562 }
9563#endif
9564
29b0f896
AM
9565 /* Tie dwarf2 debug info to the address at the start of the insn.
9566 We can't do this after the insn has been output as the current
9567 frag may have been closed off. eg. by frag_var. */
9568 dwarf2_emit_insn (0);
9569
2bbd9c25
JJ
9570 insn_start_frag = frag_now;
9571 insn_start_off = frag_now_fix ();
9572
79d72f45 9573 if (add_branch_padding_frag_p (&branch, &mf_jcc))
e379e5f3
L
9574 {
9575 char *p;
9576 /* Branch can be 8 bytes. Leave some room for prefixes. */
9577 unsigned int max_branch_padding_size = 14;
9578
9579 /* Align section to boundary. */
9580 record_alignment (now_seg, align_branch_power);
9581
9582 /* Make room for padding. */
9583 frag_grow (max_branch_padding_size);
9584
9585 /* Start of the padding. */
9586 p = frag_more (0);
9587
9588 fragP = frag_now;
9589
9590 frag_var (rs_machine_dependent, max_branch_padding_size, 0,
9591 ENCODE_RELAX_STATE (BRANCH_PADDING, 0),
9592 NULL, 0, p);
9593
79d72f45 9594 fragP->tc_frag_data.mf_type = mf_jcc;
e379e5f3
L
9595 fragP->tc_frag_data.branch_type = branch;
9596 fragP->tc_frag_data.max_bytes = max_branch_padding_size;
9597 }
9598
d59a54c2
JB
9599 if (!cpu_arch_flags.bitfield.cpui386 && (flag_code != CODE_16BIT)
9600 && !pre_386_16bit_warned)
9601 {
9602 as_warn (_("use .code16 to ensure correct addressing mode"));
9603 pre_386_16bit_warned = true;
9604 }
9605
29b0f896 9606 /* Output jumps. */
0cfa3eb3 9607 if (i.tm.opcode_modifier.jump == JUMP)
29b0f896 9608 output_branch ();
0cfa3eb3
JB
9609 else if (i.tm.opcode_modifier.jump == JUMP_BYTE
9610 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896 9611 output_jump ();
0cfa3eb3 9612 else if (i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT)
29b0f896
AM
9613 output_interseg_jump ();
9614 else
9615 {
9616 /* Output normal instructions here. */
9617 char *p;
9618 unsigned char *q;
47465058 9619 unsigned int j;
79d72f45 9620 enum mf_cmp_kind mf_cmp;
4dffcebc 9621
e4e00185 9622 if (avoid_fence
389d00a5
JB
9623 && (i.tm.base_opcode == 0xaee8
9624 || i.tm.base_opcode == 0xaef0
9625 || i.tm.base_opcode == 0xaef8))
48ef937e
JB
9626 {
9627 /* Encode lfence, mfence, and sfence as
9628 f0 83 04 24 00 lock addl $0x0, (%{re}sp). */
47f4115a
JB
9629 if (flag_code == CODE_16BIT)
9630 as_bad (_("Cannot convert `%s' in 16-bit mode"), i.tm.name);
9631 else if (omit_lock_prefix)
9632 as_bad (_("Cannot convert `%s' with `-momit-lock-prefix=yes' in effect"),
9633 i.tm.name);
9634 else if (now_seg != absolute_section)
48ef937e
JB
9635 {
9636 offsetT val = 0x240483f0ULL;
9637
9638 p = frag_more (5);
9639 md_number_to_chars (p, val, 5);
9640 }
9641 else
9642 abs_section_offset += 5;
9643 return;
9644 }
e4e00185 9645
d022bddd
IT
9646 /* Some processors fail on LOCK prefix. This options makes
9647 assembler ignore LOCK prefix and serves as a workaround. */
9648 if (omit_lock_prefix)
9649 {
35648716
JB
9650 if (i.tm.base_opcode == LOCK_PREFIX_OPCODE
9651 && i.tm.opcode_modifier.isprefix)
d022bddd
IT
9652 return;
9653 i.prefix[LOCK_PREFIX] = 0;
9654 }
9655
e379e5f3
L
9656 if (branch)
9657 /* Skip if this is a branch. */
9658 ;
79d72f45 9659 else if (add_fused_jcc_padding_frag_p (&mf_cmp))
e379e5f3
L
9660 {
9661 /* Make room for padding. */
9662 frag_grow (MAX_FUSED_JCC_PADDING_SIZE);
9663 p = frag_more (0);
9664
9665 fragP = frag_now;
9666
9667 frag_var (rs_machine_dependent, MAX_FUSED_JCC_PADDING_SIZE, 0,
9668 ENCODE_RELAX_STATE (FUSED_JCC_PADDING, 0),
9669 NULL, 0, p);
9670
79d72f45 9671 fragP->tc_frag_data.mf_type = mf_cmp;
e379e5f3
L
9672 fragP->tc_frag_data.branch_type = align_branch_fused;
9673 fragP->tc_frag_data.max_bytes = MAX_FUSED_JCC_PADDING_SIZE;
9674 }
9675 else if (add_branch_prefix_frag_p ())
9676 {
9677 unsigned int max_prefix_size = align_branch_prefix_size;
9678
9679 /* Make room for padding. */
9680 frag_grow (max_prefix_size);
9681 p = frag_more (0);
9682
9683 fragP = frag_now;
9684
9685 frag_var (rs_machine_dependent, max_prefix_size, 0,
9686 ENCODE_RELAX_STATE (BRANCH_PREFIX, 0),
9687 NULL, 0, p);
9688
9689 fragP->tc_frag_data.max_bytes = max_prefix_size;
9690 }
9691
43234a1e
L
9692 /* Since the VEX/EVEX prefix contains the implicit prefix, we
9693 don't need the explicit prefix. */
cf665fee 9694 if (!is_any_vex_encoding (&i.tm))
bc4bd9ab 9695 {
7b47a312 9696 switch (i.tm.opcode_modifier.opcodeprefix)
bc4bd9ab 9697 {
7b47a312
L
9698 case PREFIX_0X66:
9699 add_prefix (0x66);
9700 break;
9701 case PREFIX_0XF2:
9702 add_prefix (0xf2);
9703 break;
9704 case PREFIX_0XF3:
8b65b895
L
9705 if (!i.tm.cpu_flags.bitfield.cpupadlock
9706 || (i.prefix[REP_PREFIX] != 0xf3))
9707 add_prefix (0xf3);
c0f3af97 9708 break;
7b47a312 9709 case PREFIX_NONE:
9a182d04 9710 switch (i.opcode_length)
c0f3af97 9711 {
7b47a312 9712 case 2:
7b47a312 9713 break;
9a182d04 9714 case 1:
7b47a312 9715 /* Check for pseudo prefixes. */
9a182d04
JB
9716 if (!i.tm.opcode_modifier.isprefix || i.tm.base_opcode)
9717 break;
7b47a312
L
9718 as_bad_where (insn_start_frag->fr_file,
9719 insn_start_frag->fr_line,
9720 _("pseudo prefix without instruction"));
9721 return;
9722 default:
9723 abort ();
4dffcebc 9724 }
c0f3af97 9725 break;
c0f3af97
L
9726 default:
9727 abort ();
bc4bd9ab 9728 }
c0f3af97 9729
6d19a37a 9730#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
cf61b747
L
9731 /* For x32, add a dummy REX_OPCODE prefix for mov/add with
9732 R_X86_64_GOTTPOFF relocation so that linker can safely
14470f07
L
9733 perform IE->LE optimization. A dummy REX_OPCODE prefix
9734 is also needed for lea with R_X86_64_GOTPC32_TLSDESC
9735 relocation for GDesc -> IE/LE optimization. */
cf61b747
L
9736 if (x86_elf_abi == X86_64_X32_ABI
9737 && i.operands == 2
14470f07
L
9738 && (i.reloc[0] == BFD_RELOC_X86_64_GOTTPOFF
9739 || i.reloc[0] == BFD_RELOC_X86_64_GOTPC32_TLSDESC)
cf61b747
L
9740 && i.prefix[REX_PREFIX] == 0)
9741 add_prefix (REX_OPCODE);
6d19a37a 9742#endif
cf61b747 9743
c0f3af97
L
9744 /* The prefix bytes. */
9745 for (j = ARRAY_SIZE (i.prefix), q = i.prefix; j > 0; j--, q++)
9746 if (*q)
48ef937e 9747 frag_opcode_byte (*q);
0f10071e 9748 }
ae5c1c7b 9749 else
c0f3af97
L
9750 {
9751 for (j = 0, q = i.prefix; j < ARRAY_SIZE (i.prefix); j++, q++)
9752 if (*q)
9753 switch (j)
9754 {
c0f3af97
L
9755 case SEG_PREFIX:
9756 case ADDR_PREFIX:
48ef937e 9757 frag_opcode_byte (*q);
c0f3af97
L
9758 break;
9759 default:
9760 /* There should be no other prefixes for instructions
9761 with VEX prefix. */
9762 abort ();
9763 }
9764
43234a1e
L
9765 /* For EVEX instructions i.vrex should become 0 after
9766 build_evex_prefix. For VEX instructions upper 16 registers
9767 aren't available, so VREX should be 0. */
9768 if (i.vrex)
9769 abort ();
c0f3af97 9770 /* Now the VEX prefix. */
48ef937e
JB
9771 if (now_seg != absolute_section)
9772 {
9773 p = frag_more (i.vex.length);
9774 for (j = 0; j < i.vex.length; j++)
9775 p[j] = i.vex.bytes[j];
9776 }
9777 else
9778 abs_section_offset += i.vex.length;
c0f3af97 9779 }
252b5132 9780
29b0f896 9781 /* Now the opcode; be careful about word order here! */
389d00a5
JB
9782 j = i.opcode_length;
9783 if (!i.vex.length)
9784 switch (i.tm.opcode_modifier.opcodespace)
9785 {
9786 case SPACE_BASE:
9787 break;
9788 case SPACE_0F:
9789 ++j;
9790 break;
9791 case SPACE_0F38:
9792 case SPACE_0F3A:
9793 j += 2;
9794 break;
9795 default:
9796 abort ();
9797 }
9798
48ef937e 9799 if (now_seg == absolute_section)
389d00a5
JB
9800 abs_section_offset += j;
9801 else if (j == 1)
29b0f896
AM
9802 {
9803 FRAG_APPEND_1_CHAR (i.tm.base_opcode);
9804 }
9805 else
9806 {
389d00a5
JB
9807 p = frag_more (j);
9808 if (!i.vex.length
9809 && i.tm.opcode_modifier.opcodespace != SPACE_BASE)
9810 {
9811 *p++ = 0x0f;
9812 if (i.tm.opcode_modifier.opcodespace != SPACE_0F)
9813 *p++ = i.tm.opcode_modifier.opcodespace == SPACE_0F38
9814 ? 0x38 : 0x3a;
9815 }
9816
9a182d04 9817 switch (i.opcode_length)
331d2d0d 9818 {
4dffcebc 9819 case 2:
389d00a5
JB
9820 /* Put out high byte first: can't use md_number_to_chars! */
9821 *p++ = (i.tm.base_opcode >> 8) & 0xff;
9822 /* Fall through. */
9823 case 1:
9824 *p = i.tm.base_opcode & 0xff;
4dffcebc
L
9825 break;
9826 default:
9827 abort ();
9828 break;
331d2d0d 9829 }
0f10071e 9830
29b0f896 9831 }
3e73aa7c 9832
29b0f896 9833 /* Now the modrm byte and sib byte (if present). */
40fb9820 9834 if (i.tm.opcode_modifier.modrm)
29b0f896 9835 {
48ef937e
JB
9836 frag_opcode_byte ((i.rm.regmem << 0)
9837 | (i.rm.reg << 3)
9838 | (i.rm.mode << 6));
29b0f896
AM
9839 /* If i.rm.regmem == ESP (4)
9840 && i.rm.mode != (Register mode)
9841 && not 16 bit
9842 ==> need second modrm byte. */
9843 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
9844 && i.rm.mode != 3
dc821c5f 9845 && !(i.base_reg && i.base_reg->reg_type.bitfield.word))
48ef937e
JB
9846 frag_opcode_byte ((i.sib.base << 0)
9847 | (i.sib.index << 3)
9848 | (i.sib.scale << 6));
29b0f896 9849 }
3e73aa7c 9850
29b0f896 9851 if (i.disp_operands)
2bbd9c25 9852 output_disp (insn_start_frag, insn_start_off);
3e73aa7c 9853
29b0f896 9854 if (i.imm_operands)
2bbd9c25 9855 output_imm (insn_start_frag, insn_start_off);
9c33702b
JB
9856
9857 /*
9858 * frag_now_fix () returning plain abs_section_offset when we're in the
9859 * absolute section, and abs_section_offset not getting updated as data
9860 * gets added to the frag breaks the logic below.
9861 */
9862 if (now_seg != absolute_section)
9863 {
9864 j = encoding_length (insn_start_frag, insn_start_off, frag_more (0));
9865 if (j > 15)
9866 as_warn (_("instruction length of %u bytes exceeds the limit of 15"),
9867 j);
e379e5f3
L
9868 else if (fragP)
9869 {
9870 /* NB: Don't add prefix with GOTPC relocation since
9871 output_disp() above depends on the fixed encoding
9872 length. Can't add prefix with TLS relocation since
9873 it breaks TLS linker optimization. */
9874 unsigned int max = i.has_gotpc_tls_reloc ? 0 : 15 - j;
9875 /* Prefix count on the current instruction. */
9876 unsigned int count = i.vex.length;
9877 unsigned int k;
9878 for (k = 0; k < ARRAY_SIZE (i.prefix); k++)
9879 /* REX byte is encoded in VEX/EVEX prefix. */
9880 if (i.prefix[k] && (k != REX_PREFIX || !i.vex.length))
9881 count++;
9882
9883 /* Count prefixes for extended opcode maps. */
9884 if (!i.vex.length)
389d00a5 9885 switch (i.tm.opcode_modifier.opcodespace)
e379e5f3 9886 {
389d00a5 9887 case SPACE_BASE:
e379e5f3 9888 break;
389d00a5
JB
9889 case SPACE_0F:
9890 count++;
e379e5f3 9891 break;
389d00a5
JB
9892 case SPACE_0F38:
9893 case SPACE_0F3A:
9894 count += 2;
e379e5f3
L
9895 break;
9896 default:
9897 abort ();
9898 }
9899
9900 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
9901 == BRANCH_PREFIX)
9902 {
9903 /* Set the maximum prefix size in BRANCH_PREFIX
9904 frag. */
9905 if (fragP->tc_frag_data.max_bytes > max)
9906 fragP->tc_frag_data.max_bytes = max;
9907 if (fragP->tc_frag_data.max_bytes > count)
9908 fragP->tc_frag_data.max_bytes -= count;
9909 else
9910 fragP->tc_frag_data.max_bytes = 0;
9911 }
9912 else
9913 {
9914 /* Remember the maximum prefix size in FUSED_JCC_PADDING
9915 frag. */
9916 unsigned int max_prefix_size;
9917 if (align_branch_prefix_size > max)
9918 max_prefix_size = max;
9919 else
9920 max_prefix_size = align_branch_prefix_size;
9921 if (max_prefix_size > count)
9922 fragP->tc_frag_data.max_prefix_length
9923 = max_prefix_size - count;
9924 }
9925
9926 /* Use existing segment prefix if possible. Use CS
9927 segment prefix in 64-bit mode. In 32-bit mode, use SS
9928 segment prefix with ESP/EBP base register and use DS
9929 segment prefix without ESP/EBP base register. */
9930 if (i.prefix[SEG_PREFIX])
9931 fragP->tc_frag_data.default_prefix = i.prefix[SEG_PREFIX];
9932 else if (flag_code == CODE_64BIT)
9933 fragP->tc_frag_data.default_prefix = CS_PREFIX_OPCODE;
9934 else if (i.base_reg
9935 && (i.base_reg->reg_num == 4
9936 || i.base_reg->reg_num == 5))
9937 fragP->tc_frag_data.default_prefix = SS_PREFIX_OPCODE;
9938 else
9939 fragP->tc_frag_data.default_prefix = DS_PREFIX_OPCODE;
9940 }
9c33702b 9941 }
29b0f896 9942 }
252b5132 9943
e379e5f3
L
9944 /* NB: Don't work with COND_JUMP86 without i386. */
9945 if (align_branch_power
9946 && now_seg != absolute_section
9947 && cpu_arch_flags.bitfield.cpui386)
9948 {
9949 /* Terminate each frag so that we can add prefix and check for
9950 fused jcc. */
9951 frag_wane (frag_now);
9952 frag_new (0);
9953 }
9954
29b0f896
AM
9955#ifdef DEBUG386
9956 if (flag_debug)
9957 {
7b81dfbb 9958 pi ("" /*line*/, &i);
29b0f896
AM
9959 }
9960#endif /* DEBUG386 */
9961}
252b5132 9962
e205caa7
L
9963/* Return the size of the displacement operand N. */
9964
9965static int
9966disp_size (unsigned int n)
9967{
9968 int size = 4;
43234a1e 9969
b5014f7a 9970 if (i.types[n].bitfield.disp64)
40fb9820
L
9971 size = 8;
9972 else if (i.types[n].bitfield.disp8)
9973 size = 1;
9974 else if (i.types[n].bitfield.disp16)
9975 size = 2;
e205caa7
L
9976 return size;
9977}
9978
9979/* Return the size of the immediate operand N. */
9980
9981static int
9982imm_size (unsigned int n)
9983{
9984 int size = 4;
40fb9820
L
9985 if (i.types[n].bitfield.imm64)
9986 size = 8;
9987 else if (i.types[n].bitfield.imm8 || i.types[n].bitfield.imm8s)
9988 size = 1;
9989 else if (i.types[n].bitfield.imm16)
9990 size = 2;
e205caa7
L
9991 return size;
9992}
9993
29b0f896 9994static void
64e74474 9995output_disp (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
9996{
9997 char *p;
9998 unsigned int n;
252b5132 9999
29b0f896
AM
10000 for (n = 0; n < i.operands; n++)
10001 {
b5014f7a 10002 if (operand_type_check (i.types[n], disp))
29b0f896 10003 {
48ef937e
JB
10004 int size = disp_size (n);
10005
10006 if (now_seg == absolute_section)
10007 abs_section_offset += size;
10008 else if (i.op[n].disps->X_op == O_constant)
29b0f896 10009 {
43234a1e 10010 offsetT val = i.op[n].disps->X_add_number;
252b5132 10011
629cfaf1
JB
10012 val = offset_in_range (val >> (size == 1 ? i.memshift : 0),
10013 size);
29b0f896
AM
10014 p = frag_more (size);
10015 md_number_to_chars (p, val, size);
10016 }
10017 else
10018 {
f86103b7 10019 enum bfd_reloc_code_real reloc_type;
a775efc8
JB
10020 bool pcrel = (i.flags[n] & Operand_PCrel) != 0;
10021 bool sign = (flag_code == CODE_64BIT && size == 4
10022 && (!want_disp32 (&i.tm)
10023 || (i.tm.opcode_modifier.jump && !i.jumpabsolute
10024 && !i.types[n].bitfield.baseindex)))
10025 || pcrel;
02a86693 10026 fixS *fixP;
29b0f896 10027
e205caa7 10028 /* We can't have 8 bit displacement here. */
9c2799c2 10029 gas_assert (!i.types[n].bitfield.disp8);
e205caa7 10030
29b0f896
AM
10031 /* The PC relative address is computed relative
10032 to the instruction boundary, so in case immediate
10033 fields follows, we need to adjust the value. */
10034 if (pcrel && i.imm_operands)
10035 {
29b0f896 10036 unsigned int n1;
e205caa7 10037 int sz = 0;
252b5132 10038
29b0f896 10039 for (n1 = 0; n1 < i.operands; n1++)
40fb9820 10040 if (operand_type_check (i.types[n1], imm))
252b5132 10041 {
e205caa7
L
10042 /* Only one immediate is allowed for PC
10043 relative address. */
9c2799c2 10044 gas_assert (sz == 0);
e205caa7
L
10045 sz = imm_size (n1);
10046 i.op[n].disps->X_add_number -= sz;
252b5132 10047 }
29b0f896 10048 /* We should find the immediate. */
9c2799c2 10049 gas_assert (sz != 0);
29b0f896 10050 }
520dc8e8 10051
29b0f896 10052 p = frag_more (size);
d258b828 10053 reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
d6ab8113 10054 if (GOT_symbol
2bbd9c25 10055 && GOT_symbol == i.op[n].disps->X_add_symbol
d6ab8113 10056 && (((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
10057 || reloc_type == BFD_RELOC_X86_64_32S
10058 || (reloc_type == BFD_RELOC_64
10059 && object_64bit))
d6ab8113
JB
10060 && (i.op[n].disps->X_op == O_symbol
10061 || (i.op[n].disps->X_op == O_add
10062 && ((symbol_get_value_expression
10063 (i.op[n].disps->X_op_symbol)->X_op)
10064 == O_subtract))))
10065 || reloc_type == BFD_RELOC_32_PCREL))
2bbd9c25 10066 {
4fa24527 10067 if (!object_64bit)
7b81dfbb
AJ
10068 {
10069 reloc_type = BFD_RELOC_386_GOTPC;
5b7c81bd 10070 i.has_gotpc_tls_reloc = true;
98da05bf 10071 i.op[n].disps->X_add_number +=
d583596c 10072 encoding_length (insn_start_frag, insn_start_off, p);
7b81dfbb
AJ
10073 }
10074 else if (reloc_type == BFD_RELOC_64)
10075 reloc_type = BFD_RELOC_X86_64_GOTPC64;
d6ab8113 10076 else
7b81dfbb
AJ
10077 /* Don't do the adjustment for x86-64, as there
10078 the pcrel addressing is relative to the _next_
10079 insn, and that is taken care of in other code. */
d6ab8113 10080 reloc_type = BFD_RELOC_X86_64_GOTPC32;
2bbd9c25 10081 }
e379e5f3
L
10082 else if (align_branch_power)
10083 {
10084 switch (reloc_type)
10085 {
10086 case BFD_RELOC_386_TLS_GD:
10087 case BFD_RELOC_386_TLS_LDM:
10088 case BFD_RELOC_386_TLS_IE:
10089 case BFD_RELOC_386_TLS_IE_32:
10090 case BFD_RELOC_386_TLS_GOTIE:
10091 case BFD_RELOC_386_TLS_GOTDESC:
10092 case BFD_RELOC_386_TLS_DESC_CALL:
10093 case BFD_RELOC_X86_64_TLSGD:
10094 case BFD_RELOC_X86_64_TLSLD:
10095 case BFD_RELOC_X86_64_GOTTPOFF:
10096 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
10097 case BFD_RELOC_X86_64_TLSDESC_CALL:
5b7c81bd 10098 i.has_gotpc_tls_reloc = true;
e379e5f3
L
10099 default:
10100 break;
10101 }
10102 }
02a86693
L
10103 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal,
10104 size, i.op[n].disps, pcrel,
10105 reloc_type);
eb19308f
JB
10106
10107 if (flag_code == CODE_64BIT && size == 4 && pcrel
10108 && !i.prefix[ADDR_PREFIX])
10109 fixP->fx_signed = 1;
10110
02a86693
L
10111 /* Check for "call/jmp *mem", "mov mem, %reg",
10112 "test %reg, mem" and "binop mem, %reg" where binop
10113 is one of adc, add, and, cmp, or, sbb, sub, xor
e60f4d3b
L
10114 instructions without data prefix. Always generate
10115 R_386_GOT32X for "sym*GOT" operand in 32-bit mode. */
10116 if (i.prefix[DATA_PREFIX] == 0
10117 && (generate_relax_relocations
10118 || (!object_64bit
10119 && i.rm.mode == 0
10120 && i.rm.regmem == 5))
0cb4071e
L
10121 && (i.rm.mode == 2
10122 || (i.rm.mode == 0 && i.rm.regmem == 5))
389d00a5 10123 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
02a86693
L
10124 && ((i.operands == 1
10125 && i.tm.base_opcode == 0xff
10126 && (i.rm.reg == 2 || i.rm.reg == 4))
10127 || (i.operands == 2
10128 && (i.tm.base_opcode == 0x8b
10129 || i.tm.base_opcode == 0x85
2ae4c703 10130 || (i.tm.base_opcode & ~0x38) == 0x03))))
02a86693
L
10131 {
10132 if (object_64bit)
10133 {
10134 fixP->fx_tcbit = i.rex != 0;
10135 if (i.base_reg
e968fc9b 10136 && (i.base_reg->reg_num == RegIP))
02a86693
L
10137 fixP->fx_tcbit2 = 1;
10138 }
10139 else
10140 fixP->fx_tcbit2 = 1;
10141 }
29b0f896
AM
10142 }
10143 }
10144 }
10145}
252b5132 10146
29b0f896 10147static void
64e74474 10148output_imm (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
10149{
10150 char *p;
10151 unsigned int n;
252b5132 10152
29b0f896
AM
10153 for (n = 0; n < i.operands; n++)
10154 {
40fb9820 10155 if (operand_type_check (i.types[n], imm))
29b0f896 10156 {
48ef937e
JB
10157 int size = imm_size (n);
10158
10159 if (now_seg == absolute_section)
10160 abs_section_offset += size;
10161 else if (i.op[n].imms->X_op == O_constant)
29b0f896 10162 {
29b0f896 10163 offsetT val;
b4cac588 10164
29b0f896
AM
10165 val = offset_in_range (i.op[n].imms->X_add_number,
10166 size);
10167 p = frag_more (size);
10168 md_number_to_chars (p, val, size);
10169 }
10170 else
10171 {
10172 /* Not absolute_section.
10173 Need a 32-bit fixup (don't support 8bit
10174 non-absolute imms). Try to support other
10175 sizes ... */
f86103b7 10176 enum bfd_reloc_code_real reloc_type;
e205caa7 10177 int sign;
29b0f896 10178
40fb9820 10179 if (i.types[n].bitfield.imm32s
a7d61044 10180 && (i.suffix == QWORD_MNEM_SUFFIX
40fb9820 10181 || (!i.suffix && i.tm.opcode_modifier.no_lsuf)))
29b0f896 10182 sign = 1;
e205caa7
L
10183 else
10184 sign = 0;
520dc8e8 10185
29b0f896 10186 p = frag_more (size);
d258b828 10187 reloc_type = reloc (size, 0, sign, i.reloc[n]);
f86103b7 10188
2bbd9c25
JJ
10189 /* This is tough to explain. We end up with this one if we
10190 * have operands that look like
10191 * "_GLOBAL_OFFSET_TABLE_+[.-.L284]". The goal here is to
10192 * obtain the absolute address of the GOT, and it is strongly
10193 * preferable from a performance point of view to avoid using
10194 * a runtime relocation for this. The actual sequence of
10195 * instructions often look something like:
10196 *
10197 * call .L66
10198 * .L66:
10199 * popl %ebx
10200 * addl $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
10201 *
10202 * The call and pop essentially return the absolute address
10203 * of the label .L66 and store it in %ebx. The linker itself
10204 * will ultimately change the first operand of the addl so
10205 * that %ebx points to the GOT, but to keep things simple, the
10206 * .o file must have this operand set so that it generates not
10207 * the absolute address of .L66, but the absolute address of
10208 * itself. This allows the linker itself simply treat a GOTPC
10209 * relocation as asking for a pcrel offset to the GOT to be
10210 * added in, and the addend of the relocation is stored in the
10211 * operand field for the instruction itself.
10212 *
10213 * Our job here is to fix the operand so that it would add
10214 * the correct offset so that %ebx would point to itself. The
10215 * thing that is tricky is that .-.L66 will point to the
10216 * beginning of the instruction, so we need to further modify
10217 * the operand so that it will point to itself. There are
10218 * other cases where you have something like:
10219 *
10220 * .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
10221 *
10222 * and here no correction would be required. Internally in
10223 * the assembler we treat operands of this form as not being
10224 * pcrel since the '.' is explicitly mentioned, and I wonder
10225 * whether it would simplify matters to do it this way. Who
10226 * knows. In earlier versions of the PIC patches, the
10227 * pcrel_adjust field was used to store the correction, but
10228 * since the expression is not pcrel, I felt it would be
10229 * confusing to do it this way. */
10230
d6ab8113 10231 if ((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
10232 || reloc_type == BFD_RELOC_X86_64_32S
10233 || reloc_type == BFD_RELOC_64)
29b0f896
AM
10234 && GOT_symbol
10235 && GOT_symbol == i.op[n].imms->X_add_symbol
10236 && (i.op[n].imms->X_op == O_symbol
10237 || (i.op[n].imms->X_op == O_add
10238 && ((symbol_get_value_expression
10239 (i.op[n].imms->X_op_symbol)->X_op)
10240 == O_subtract))))
10241 {
4fa24527 10242 if (!object_64bit)
d6ab8113 10243 reloc_type = BFD_RELOC_386_GOTPC;
7b81dfbb 10244 else if (size == 4)
d6ab8113 10245 reloc_type = BFD_RELOC_X86_64_GOTPC32;
7b81dfbb
AJ
10246 else if (size == 8)
10247 reloc_type = BFD_RELOC_X86_64_GOTPC64;
5b7c81bd 10248 i.has_gotpc_tls_reloc = true;
d583596c
JB
10249 i.op[n].imms->X_add_number +=
10250 encoding_length (insn_start_frag, insn_start_off, p);
29b0f896 10251 }
29b0f896
AM
10252 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
10253 i.op[n].imms, 0, reloc_type);
10254 }
10255 }
10256 }
252b5132
RH
10257}
10258\f
d182319b
JB
10259/* x86_cons_fix_new is called via the expression parsing code when a
10260 reloc is needed. We use this hook to get the correct .got reloc. */
d182319b
JB
10261static int cons_sign = -1;
10262
10263void
e3bb37b5 10264x86_cons_fix_new (fragS *frag, unsigned int off, unsigned int len,
62ebcb5c 10265 expressionS *exp, bfd_reloc_code_real_type r)
d182319b 10266{
d258b828 10267 r = reloc (len, 0, cons_sign, r);
d182319b
JB
10268
10269#ifdef TE_PE
10270 if (exp->X_op == O_secrel)
10271 {
10272 exp->X_op = O_symbol;
10273 r = BFD_RELOC_32_SECREL;
10274 }
145667f8
MH
10275 else if (exp->X_op == O_secidx)
10276 r = BFD_RELOC_16_SECIDX;
d182319b
JB
10277#endif
10278
10279 fix_new_exp (frag, off, len, exp, 0, r);
10280}
10281
357d1bd8
L
10282/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
10283 purpose of the `.dc.a' internal pseudo-op. */
10284
10285int
10286x86_address_bytes (void)
10287{
10288 if ((stdoutput->arch_info->mach & bfd_mach_x64_32))
10289 return 4;
10290 return stdoutput->arch_info->bits_per_address / 8;
10291}
10292
deea4973
JB
10293#if (!(defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined (OBJ_MACH_O)) \
10294 || defined (LEX_AT)) && !defined (TE_PE)
d258b828 10295# define lex_got(reloc, adjust, types) NULL
718ddfc0 10296#else
f3c180ae
AM
10297/* Parse operands of the form
10298 <symbol>@GOTOFF+<nnn>
10299 and similar .plt or .got references.
10300
10301 If we find one, set up the correct relocation in RELOC and copy the
10302 input string, minus the `@GOTOFF' into a malloc'd buffer for
10303 parsing by the calling routine. Return this buffer, and if ADJUST
10304 is non-null set it to the length of the string we removed from the
10305 input line. Otherwise return NULL. */
10306static char *
91d6fa6a 10307lex_got (enum bfd_reloc_code_real *rel,
64e74474 10308 int *adjust,
d258b828 10309 i386_operand_type *types)
f3c180ae 10310{
7b81dfbb
AJ
10311 /* Some of the relocations depend on the size of what field is to
10312 be relocated. But in our callers i386_immediate and i386_displacement
10313 we don't yet know the operand size (this will be set by insn
10314 matching). Hence we record the word32 relocation here,
10315 and adjust the reloc according to the real size in reloc(). */
145667f8
MH
10316 static const struct
10317 {
f3c180ae 10318 const char *str;
cff8d58a 10319 int len;
4fa24527 10320 const enum bfd_reloc_code_real rel[2];
40fb9820 10321 const i386_operand_type types64;
5b7c81bd 10322 bool need_GOT_symbol;
145667f8
MH
10323 }
10324 gotrel[] =
10325 {
deea4973 10326#ifndef TE_PE
8ce3d284 10327#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
10328 { STRING_COMMA_LEN ("SIZE"), { BFD_RELOC_SIZE32,
10329 BFD_RELOC_SIZE32 },
5b7c81bd 10330 OPERAND_TYPE_IMM32_64, false },
8ce3d284 10331#endif
cff8d58a
L
10332 { STRING_COMMA_LEN ("PLTOFF"), { _dummy_first_bfd_reloc_code_real,
10333 BFD_RELOC_X86_64_PLTOFF64 },
5b7c81bd 10334 OPERAND_TYPE_IMM64, true },
cff8d58a
L
10335 { STRING_COMMA_LEN ("PLT"), { BFD_RELOC_386_PLT32,
10336 BFD_RELOC_X86_64_PLT32 },
a775efc8 10337 OPERAND_TYPE_IMM32_32S_DISP32, false },
cff8d58a
L
10338 { STRING_COMMA_LEN ("GOTPLT"), { _dummy_first_bfd_reloc_code_real,
10339 BFD_RELOC_X86_64_GOTPLT64 },
5b7c81bd 10340 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
10341 { STRING_COMMA_LEN ("GOTOFF"), { BFD_RELOC_386_GOTOFF,
10342 BFD_RELOC_X86_64_GOTOFF64 },
5b7c81bd 10343 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
10344 { STRING_COMMA_LEN ("GOTPCREL"), { _dummy_first_bfd_reloc_code_real,
10345 BFD_RELOC_X86_64_GOTPCREL },
a775efc8 10346 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10347 { STRING_COMMA_LEN ("TLSGD"), { BFD_RELOC_386_TLS_GD,
10348 BFD_RELOC_X86_64_TLSGD },
a775efc8 10349 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10350 { STRING_COMMA_LEN ("TLSLDM"), { BFD_RELOC_386_TLS_LDM,
10351 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10352 OPERAND_TYPE_NONE, true },
cff8d58a
L
10353 { STRING_COMMA_LEN ("TLSLD"), { _dummy_first_bfd_reloc_code_real,
10354 BFD_RELOC_X86_64_TLSLD },
a775efc8 10355 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10356 { STRING_COMMA_LEN ("GOTTPOFF"), { BFD_RELOC_386_TLS_IE_32,
10357 BFD_RELOC_X86_64_GOTTPOFF },
a775efc8 10358 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10359 { STRING_COMMA_LEN ("TPOFF"), { BFD_RELOC_386_TLS_LE_32,
10360 BFD_RELOC_X86_64_TPOFF32 },
a775efc8 10361 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
10362 { STRING_COMMA_LEN ("NTPOFF"), { BFD_RELOC_386_TLS_LE,
10363 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10364 OPERAND_TYPE_NONE, true },
cff8d58a
L
10365 { STRING_COMMA_LEN ("DTPOFF"), { BFD_RELOC_386_TLS_LDO_32,
10366 BFD_RELOC_X86_64_DTPOFF32 },
a775efc8 10367 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
10368 { STRING_COMMA_LEN ("GOTNTPOFF"),{ BFD_RELOC_386_TLS_GOTIE,
10369 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10370 OPERAND_TYPE_NONE, true },
cff8d58a
L
10371 { STRING_COMMA_LEN ("INDNTPOFF"),{ BFD_RELOC_386_TLS_IE,
10372 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10373 OPERAND_TYPE_NONE, true },
cff8d58a
L
10374 { STRING_COMMA_LEN ("GOT"), { BFD_RELOC_386_GOT32,
10375 BFD_RELOC_X86_64_GOT32 },
a775efc8 10376 OPERAND_TYPE_IMM32_32S_64_DISP32, true },
cff8d58a
L
10377 { STRING_COMMA_LEN ("TLSDESC"), { BFD_RELOC_386_TLS_GOTDESC,
10378 BFD_RELOC_X86_64_GOTPC32_TLSDESC },
a775efc8 10379 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10380 { STRING_COMMA_LEN ("TLSCALL"), { BFD_RELOC_386_TLS_DESC_CALL,
10381 BFD_RELOC_X86_64_TLSDESC_CALL },
a775efc8 10382 OPERAND_TYPE_IMM32_32S_DISP32, true },
deea4973
JB
10383#else /* TE_PE */
10384 { STRING_COMMA_LEN ("SECREL32"), { BFD_RELOC_32_SECREL,
10385 BFD_RELOC_32_SECREL },
a775efc8 10386 OPERAND_TYPE_IMM32_32S_64_DISP32_64, false },
deea4973 10387#endif
f3c180ae
AM
10388 };
10389 char *cp;
10390 unsigned int j;
10391
deea4973 10392#if defined (OBJ_MAYBE_ELF) && !defined (TE_PE)
718ddfc0
JB
10393 if (!IS_ELF)
10394 return NULL;
d382c579 10395#endif
718ddfc0 10396
f3c180ae 10397 for (cp = input_line_pointer; *cp != '@'; cp++)
67c11a9b 10398 if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
f3c180ae
AM
10399 return NULL;
10400
47465058 10401 for (j = 0; j < ARRAY_SIZE (gotrel); j++)
f3c180ae 10402 {
cff8d58a 10403 int len = gotrel[j].len;
28f81592 10404 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
f3c180ae 10405 {
4fa24527 10406 if (gotrel[j].rel[object_64bit] != 0)
f3c180ae 10407 {
28f81592
AM
10408 int first, second;
10409 char *tmpbuf, *past_reloc;
f3c180ae 10410
91d6fa6a 10411 *rel = gotrel[j].rel[object_64bit];
f3c180ae 10412
3956db08
JB
10413 if (types)
10414 {
10415 if (flag_code != CODE_64BIT)
40fb9820
L
10416 {
10417 types->bitfield.imm32 = 1;
10418 types->bitfield.disp32 = 1;
10419 }
3956db08
JB
10420 else
10421 *types = gotrel[j].types64;
10422 }
10423
844bf810 10424 if (gotrel[j].need_GOT_symbol && GOT_symbol == NULL)
f3c180ae
AM
10425 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
10426
28f81592 10427 /* The length of the first part of our input line. */
f3c180ae 10428 first = cp - input_line_pointer;
28f81592
AM
10429
10430 /* The second part goes from after the reloc token until
67c11a9b 10431 (and including) an end_of_line char or comma. */
28f81592 10432 past_reloc = cp + 1 + len;
67c11a9b
AM
10433 cp = past_reloc;
10434 while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
10435 ++cp;
10436 second = cp + 1 - past_reloc;
28f81592
AM
10437
10438 /* Allocate and copy string. The trailing NUL shouldn't
10439 be necessary, but be safe. */
add39d23 10440 tmpbuf = XNEWVEC (char, first + second + 2);
f3c180ae 10441 memcpy (tmpbuf, input_line_pointer, first);
0787a12d
AM
10442 if (second != 0 && *past_reloc != ' ')
10443 /* Replace the relocation token with ' ', so that
10444 errors like foo@GOTOFF1 will be detected. */
10445 tmpbuf[first++] = ' ';
af89796a
L
10446 else
10447 /* Increment length by 1 if the relocation token is
10448 removed. */
10449 len++;
10450 if (adjust)
10451 *adjust = len;
0787a12d
AM
10452 memcpy (tmpbuf + first, past_reloc, second);
10453 tmpbuf[first + second] = '\0';
f3c180ae
AM
10454 return tmpbuf;
10455 }
10456
4fa24527
JB
10457 as_bad (_("@%s reloc is not supported with %d-bit output format"),
10458 gotrel[j].str, 1 << (5 + object_64bit));
f3c180ae
AM
10459 return NULL;
10460 }
10461 }
10462
10463 /* Might be a symbol version string. Don't as_bad here. */
10464 return NULL;
10465}
4e4f7c87 10466#endif
f3c180ae 10467
62ebcb5c 10468bfd_reloc_code_real_type
e3bb37b5 10469x86_cons (expressionS *exp, int size)
f3c180ae 10470{
62ebcb5c
AM
10471 bfd_reloc_code_real_type got_reloc = NO_RELOC;
10472
2748c1b1
L
10473#if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
10474 && !defined (LEX_AT)) \
10475 || defined (TE_PE)
ee86248c
JB
10476 intel_syntax = -intel_syntax;
10477
3c7b9c2c 10478 exp->X_md = 0;
4fa24527 10479 if (size == 4 || (object_64bit && size == 8))
f3c180ae
AM
10480 {
10481 /* Handle @GOTOFF and the like in an expression. */
10482 char *save;
10483 char *gotfree_input_line;
4a57f2cf 10484 int adjust = 0;
f3c180ae
AM
10485
10486 save = input_line_pointer;
d258b828 10487 gotfree_input_line = lex_got (&got_reloc, &adjust, NULL);
f3c180ae
AM
10488 if (gotfree_input_line)
10489 input_line_pointer = gotfree_input_line;
10490
10491 expression (exp);
10492
10493 if (gotfree_input_line)
10494 {
10495 /* expression () has merrily parsed up to the end of line,
10496 or a comma - in the wrong buffer. Transfer how far
10497 input_line_pointer has moved to the right buffer. */
10498 input_line_pointer = (save
10499 + (input_line_pointer - gotfree_input_line)
10500 + adjust);
10501 free (gotfree_input_line);
3992d3b7
AM
10502 if (exp->X_op == O_constant
10503 || exp->X_op == O_absent
10504 || exp->X_op == O_illegal
0398aac5 10505 || exp->X_op == O_register
3992d3b7
AM
10506 || exp->X_op == O_big)
10507 {
10508 char c = *input_line_pointer;
10509 *input_line_pointer = 0;
10510 as_bad (_("missing or invalid expression `%s'"), save);
10511 *input_line_pointer = c;
10512 }
b9519cfe
L
10513 else if ((got_reloc == BFD_RELOC_386_PLT32
10514 || got_reloc == BFD_RELOC_X86_64_PLT32)
10515 && exp->X_op != O_symbol)
10516 {
10517 char c = *input_line_pointer;
10518 *input_line_pointer = 0;
10519 as_bad (_("invalid PLT expression `%s'"), save);
10520 *input_line_pointer = c;
10521 }
f3c180ae
AM
10522 }
10523 }
10524 else
10525 expression (exp);
ee86248c
JB
10526
10527 intel_syntax = -intel_syntax;
10528
10529 if (intel_syntax)
10530 i386_intel_simplify (exp);
2748c1b1
L
10531#else
10532 expression (exp);
10533#endif
62ebcb5c 10534
a442cac5
JB
10535 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
10536 if (size == 4 && exp->X_op == O_constant && !object_64bit)
10537 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
10538
62ebcb5c 10539 return got_reloc;
f3c180ae 10540}
f3c180ae 10541
9f32dd5b
L
10542static void
10543signed_cons (int size)
6482c264 10544{
a442cac5 10545 if (object_64bit)
d182319b
JB
10546 cons_sign = 1;
10547 cons (size);
10548 cons_sign = -1;
6482c264
NC
10549}
10550
d182319b 10551#ifdef TE_PE
6482c264 10552static void
7016a5d5 10553pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
6482c264
NC
10554{
10555 expressionS exp;
10556
10557 do
10558 {
10559 expression (&exp);
10560 if (exp.X_op == O_symbol)
10561 exp.X_op = O_secrel;
10562
10563 emit_expr (&exp, 4);
10564 }
10565 while (*input_line_pointer++ == ',');
10566
10567 input_line_pointer--;
10568 demand_empty_rest_of_line ();
10569}
145667f8
MH
10570
10571static void
10572pe_directive_secidx (int dummy ATTRIBUTE_UNUSED)
10573{
10574 expressionS exp;
10575
10576 do
10577 {
10578 expression (&exp);
10579 if (exp.X_op == O_symbol)
10580 exp.X_op = O_secidx;
10581
10582 emit_expr (&exp, 2);
10583 }
10584 while (*input_line_pointer++ == ',');
10585
10586 input_line_pointer--;
10587 demand_empty_rest_of_line ();
10588}
6482c264
NC
10589#endif
10590
7063667e
JB
10591/* Handle Rounding Control / SAE specifiers. */
10592
10593static char *
10594RC_SAE_specifier (const char *pstr)
10595{
10596 unsigned int j;
10597
10598 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); j++)
10599 {
10600 if (!strncmp (pstr, RC_NamesTable[j].name, RC_NamesTable[j].len))
10601 {
10602 if (i.rounding.type != rc_none)
10603 {
10604 as_bad (_("duplicated `{%s}'"), RC_NamesTable[j].name);
10605 return NULL;
10606 }
10607
10608 i.rounding.type = RC_NamesTable[j].type;
10609
10610 return (char *)(pstr + RC_NamesTable[j].len);
10611 }
10612 }
10613
10614 return NULL;
10615}
10616
43234a1e
L
10617/* Handle Vector operations. */
10618
10619static char *
f70c6814 10620check_VecOperations (char *op_string)
43234a1e
L
10621{
10622 const reg_entry *mask;
10623 const char *saved;
10624 char *end_op;
10625
f70c6814 10626 while (*op_string)
43234a1e
L
10627 {
10628 saved = op_string;
10629 if (*op_string == '{')
10630 {
10631 op_string++;
10632
10633 /* Check broadcasts. */
d34049e8 10634 if (startswith (op_string, "1to"))
43234a1e 10635 {
5273a3cd 10636 unsigned int bcst_type;
43234a1e 10637
5273a3cd 10638 if (i.broadcast.type)
43234a1e
L
10639 goto duplicated_vec_op;
10640
10641 op_string += 3;
10642 if (*op_string == '8')
8e6e0792 10643 bcst_type = 8;
b28d1bda 10644 else if (*op_string == '4')
8e6e0792 10645 bcst_type = 4;
b28d1bda 10646 else if (*op_string == '2')
8e6e0792 10647 bcst_type = 2;
43234a1e
L
10648 else if (*op_string == '1'
10649 && *(op_string+1) == '6')
10650 {
8e6e0792 10651 bcst_type = 16;
43234a1e
L
10652 op_string++;
10653 }
0cc78721
CL
10654 else if (*op_string == '3'
10655 && *(op_string+1) == '2')
10656 {
10657 bcst_type = 32;
10658 op_string++;
10659 }
43234a1e
L
10660 else
10661 {
10662 as_bad (_("Unsupported broadcast: `%s'"), saved);
10663 return NULL;
10664 }
10665 op_string++;
10666
5273a3cd
JB
10667 i.broadcast.type = bcst_type;
10668 i.broadcast.operand = this_operand;
43234a1e
L
10669 }
10670 /* Check masking operation. */
10671 else if ((mask = parse_register (op_string, &end_op)) != NULL)
10672 {
8a6fb3f9
JB
10673 if (mask == &bad_reg)
10674 return NULL;
10675
43234a1e 10676 /* k0 can't be used for write mask. */
f74a6307 10677 if (mask->reg_type.bitfield.class != RegMask || !mask->reg_num)
43234a1e 10678 {
6d2cd6b2
JB
10679 as_bad (_("`%s%s' can't be used for write mask"),
10680 register_prefix, mask->reg_name);
43234a1e
L
10681 return NULL;
10682 }
10683
6225c532 10684 if (!i.mask.reg)
43234a1e 10685 {
6225c532
JB
10686 i.mask.reg = mask;
10687 i.mask.operand = this_operand;
43234a1e 10688 }
6225c532
JB
10689 else if (i.mask.reg->reg_num)
10690 goto duplicated_vec_op;
43234a1e
L
10691 else
10692 {
6225c532 10693 i.mask.reg = mask;
43234a1e
L
10694
10695 /* Only "{z}" is allowed here. No need to check
10696 zeroing mask explicitly. */
6225c532 10697 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
10698 {
10699 as_bad (_("invalid write mask `%s'"), saved);
10700 return NULL;
10701 }
10702 }
10703
10704 op_string = end_op;
10705 }
10706 /* Check zeroing-flag for masking operation. */
10707 else if (*op_string == 'z')
10708 {
6225c532 10709 if (!i.mask.reg)
43234a1e 10710 {
6225c532
JB
10711 i.mask.reg = reg_k0;
10712 i.mask.zeroing = 1;
10713 i.mask.operand = this_operand;
43234a1e
L
10714 }
10715 else
10716 {
6225c532 10717 if (i.mask.zeroing)
43234a1e
L
10718 {
10719 duplicated_vec_op:
10720 as_bad (_("duplicated `%s'"), saved);
10721 return NULL;
10722 }
10723
6225c532 10724 i.mask.zeroing = 1;
43234a1e
L
10725
10726 /* Only "{%k}" is allowed here. No need to check mask
10727 register explicitly. */
6225c532 10728 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
10729 {
10730 as_bad (_("invalid zeroing-masking `%s'"),
10731 saved);
10732 return NULL;
10733 }
10734 }
10735
10736 op_string++;
10737 }
7063667e
JB
10738 else if (intel_syntax
10739 && (op_string = RC_SAE_specifier (op_string)) != NULL)
10740 i.rounding.modifier = true;
43234a1e
L
10741 else
10742 goto unknown_vec_op;
10743
10744 if (*op_string != '}')
10745 {
10746 as_bad (_("missing `}' in `%s'"), saved);
10747 return NULL;
10748 }
10749 op_string++;
0ba3a731
L
10750
10751 /* Strip whitespace since the addition of pseudo prefixes
10752 changed how the scrubber treats '{'. */
10753 if (is_space_char (*op_string))
10754 ++op_string;
10755
43234a1e
L
10756 continue;
10757 }
10758 unknown_vec_op:
10759 /* We don't know this one. */
10760 as_bad (_("unknown vector operation: `%s'"), saved);
10761 return NULL;
10762 }
10763
6225c532 10764 if (i.mask.reg && i.mask.zeroing && !i.mask.reg->reg_num)
6d2cd6b2
JB
10765 {
10766 as_bad (_("zeroing-masking only allowed with write mask"));
10767 return NULL;
10768 }
10769
43234a1e
L
10770 return op_string;
10771}
10772
252b5132 10773static int
70e41ade 10774i386_immediate (char *imm_start)
252b5132
RH
10775{
10776 char *save_input_line_pointer;
f3c180ae 10777 char *gotfree_input_line;
252b5132 10778 segT exp_seg = 0;
47926f60 10779 expressionS *exp;
40fb9820
L
10780 i386_operand_type types;
10781
0dfbf9d7 10782 operand_type_set (&types, ~0);
252b5132
RH
10783
10784 if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
10785 {
31b2323c
L
10786 as_bad (_("at most %d immediate operands are allowed"),
10787 MAX_IMMEDIATE_OPERANDS);
252b5132
RH
10788 return 0;
10789 }
10790
10791 exp = &im_expressions[i.imm_operands++];
520dc8e8 10792 i.op[this_operand].imms = exp;
252b5132
RH
10793
10794 if (is_space_char (*imm_start))
10795 ++imm_start;
10796
10797 save_input_line_pointer = input_line_pointer;
10798 input_line_pointer = imm_start;
10799
d258b828 10800 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
10801 if (gotfree_input_line)
10802 input_line_pointer = gotfree_input_line;
252b5132
RH
10803
10804 exp_seg = expression (exp);
10805
83183c0c 10806 SKIP_WHITESPACE ();
252b5132 10807 if (*input_line_pointer)
f3c180ae 10808 as_bad (_("junk `%s' after expression"), input_line_pointer);
252b5132
RH
10809
10810 input_line_pointer = save_input_line_pointer;
f3c180ae 10811 if (gotfree_input_line)
ee86248c
JB
10812 {
10813 free (gotfree_input_line);
10814
9aac24b1 10815 if (exp->X_op == O_constant)
ee86248c
JB
10816 exp->X_op = O_illegal;
10817 }
10818
9aac24b1
JB
10819 if (exp_seg == reg_section)
10820 {
10821 as_bad (_("illegal immediate register operand %s"), imm_start);
10822 return 0;
10823 }
10824
ee86248c
JB
10825 return i386_finalize_immediate (exp_seg, exp, types, imm_start);
10826}
252b5132 10827
ee86248c
JB
10828static int
10829i386_finalize_immediate (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
10830 i386_operand_type types, const char *imm_start)
10831{
10832 if (exp->X_op == O_absent || exp->X_op == O_illegal || exp->X_op == O_big)
252b5132 10833 {
313c53d1
L
10834 if (imm_start)
10835 as_bad (_("missing or invalid immediate expression `%s'"),
10836 imm_start);
3992d3b7 10837 return 0;
252b5132 10838 }
3e73aa7c 10839 else if (exp->X_op == O_constant)
252b5132 10840 {
47926f60 10841 /* Size it properly later. */
40fb9820 10842 i.types[this_operand].bitfield.imm64 = 1;
a442cac5
JB
10843
10844 /* If not 64bit, sign/zero extend val, to account for wraparound
10845 when !BFD64. */
10846 if (flag_code != CODE_64BIT)
10847 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
252b5132 10848 }
4c63da97 10849#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
f86103b7 10850 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
31312f95 10851 && exp_seg != absolute_section
47926f60 10852 && exp_seg != text_section
24eab124
AM
10853 && exp_seg != data_section
10854 && exp_seg != bss_section
10855 && exp_seg != undefined_section
f86103b7 10856 && !bfd_is_com_section (exp_seg))
252b5132 10857 {
d0b47220 10858 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
252b5132
RH
10859 return 0;
10860 }
10861#endif
10862 else
10863 {
10864 /* This is an address. The size of the address will be
24eab124 10865 determined later, depending on destination register,
3e73aa7c 10866 suffix, or the default for the section. */
40fb9820
L
10867 i.types[this_operand].bitfield.imm8 = 1;
10868 i.types[this_operand].bitfield.imm16 = 1;
10869 i.types[this_operand].bitfield.imm32 = 1;
10870 i.types[this_operand].bitfield.imm32s = 1;
10871 i.types[this_operand].bitfield.imm64 = 1;
c6fb90c8
L
10872 i.types[this_operand] = operand_type_and (i.types[this_operand],
10873 types);
252b5132
RH
10874 }
10875
10876 return 1;
10877}
10878
551c1ca1 10879static char *
e3bb37b5 10880i386_scale (char *scale)
252b5132 10881{
551c1ca1
AM
10882 offsetT val;
10883 char *save = input_line_pointer;
252b5132 10884
551c1ca1
AM
10885 input_line_pointer = scale;
10886 val = get_absolute_expression ();
10887
10888 switch (val)
252b5132 10889 {
551c1ca1 10890 case 1:
252b5132
RH
10891 i.log2_scale_factor = 0;
10892 break;
551c1ca1 10893 case 2:
252b5132
RH
10894 i.log2_scale_factor = 1;
10895 break;
551c1ca1 10896 case 4:
252b5132
RH
10897 i.log2_scale_factor = 2;
10898 break;
551c1ca1 10899 case 8:
252b5132
RH
10900 i.log2_scale_factor = 3;
10901 break;
10902 default:
a724f0f4
JB
10903 {
10904 char sep = *input_line_pointer;
10905
10906 *input_line_pointer = '\0';
10907 as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
10908 scale);
10909 *input_line_pointer = sep;
10910 input_line_pointer = save;
10911 return NULL;
10912 }
252b5132 10913 }
29b0f896 10914 if (i.log2_scale_factor != 0 && i.index_reg == 0)
252b5132
RH
10915 {
10916 as_warn (_("scale factor of %d without an index register"),
24eab124 10917 1 << i.log2_scale_factor);
252b5132 10918 i.log2_scale_factor = 0;
252b5132 10919 }
551c1ca1
AM
10920 scale = input_line_pointer;
10921 input_line_pointer = save;
10922 return scale;
252b5132
RH
10923}
10924
252b5132 10925static int
e3bb37b5 10926i386_displacement (char *disp_start, char *disp_end)
252b5132 10927{
29b0f896 10928 expressionS *exp;
252b5132
RH
10929 segT exp_seg = 0;
10930 char *save_input_line_pointer;
f3c180ae 10931 char *gotfree_input_line;
40fb9820
L
10932 int override;
10933 i386_operand_type bigdisp, types = anydisp;
3992d3b7 10934 int ret;
252b5132 10935
31b2323c
L
10936 if (i.disp_operands == MAX_MEMORY_OPERANDS)
10937 {
10938 as_bad (_("at most %d displacement operands are allowed"),
10939 MAX_MEMORY_OPERANDS);
10940 return 0;
10941 }
10942
0dfbf9d7 10943 operand_type_set (&bigdisp, 0);
6f2f06be 10944 if (i.jumpabsolute
48bcea9f 10945 || i.types[this_operand].bitfield.baseindex
0cfa3eb3
JB
10946 || (current_templates->start->opcode_modifier.jump != JUMP
10947 && current_templates->start->opcode_modifier.jump != JUMP_DWORD))
e05278af 10948 {
48bcea9f 10949 i386_addressing_mode ();
e05278af 10950 override = (i.prefix[ADDR_PREFIX] != 0);
40fb9820
L
10951 if (flag_code == CODE_64BIT)
10952 {
a775efc8 10953 bigdisp.bitfield.disp32 = 1;
40fb9820 10954 if (!override)
a775efc8 10955 bigdisp.bitfield.disp64 = 1;
40fb9820
L
10956 }
10957 else if ((flag_code == CODE_16BIT) ^ override)
40fb9820 10958 bigdisp.bitfield.disp16 = 1;
48bcea9f
JB
10959 else
10960 bigdisp.bitfield.disp32 = 1;
e05278af
JB
10961 }
10962 else
10963 {
376cd056
JB
10964 /* For PC-relative branches, the width of the displacement may be
10965 dependent upon data size, but is never dependent upon address size.
10966 Also make sure to not unintentionally match against a non-PC-relative
10967 branch template. */
10968 static templates aux_templates;
10969 const insn_template *t = current_templates->start;
5b7c81bd 10970 bool has_intel64 = false;
376cd056
JB
10971
10972 aux_templates.start = t;
10973 while (++t < current_templates->end)
10974 {
10975 if (t->opcode_modifier.jump
10976 != current_templates->start->opcode_modifier.jump)
10977 break;
4b5aaf5f 10978 if ((t->opcode_modifier.isa64 >= INTEL64))
5b7c81bd 10979 has_intel64 = true;
376cd056
JB
10980 }
10981 if (t < current_templates->end)
10982 {
10983 aux_templates.end = t;
10984 current_templates = &aux_templates;
10985 }
10986
e05278af 10987 override = (i.prefix[DATA_PREFIX] != 0);
40fb9820
L
10988 if (flag_code == CODE_64BIT)
10989 {
376cd056
JB
10990 if ((override || i.suffix == WORD_MNEM_SUFFIX)
10991 && (!intel64 || !has_intel64))
40fb9820
L
10992 bigdisp.bitfield.disp16 = 1;
10993 else
a775efc8 10994 bigdisp.bitfield.disp32 = 1;
40fb9820
L
10995 }
10996 else
e05278af
JB
10997 {
10998 if (!override)
10999 override = (i.suffix == (flag_code != CODE_16BIT
11000 ? WORD_MNEM_SUFFIX
11001 : LONG_MNEM_SUFFIX));
40fb9820
L
11002 bigdisp.bitfield.disp32 = 1;
11003 if ((flag_code == CODE_16BIT) ^ override)
11004 {
11005 bigdisp.bitfield.disp32 = 0;
11006 bigdisp.bitfield.disp16 = 1;
11007 }
e05278af 11008 }
e05278af 11009 }
c6fb90c8
L
11010 i.types[this_operand] = operand_type_or (i.types[this_operand],
11011 bigdisp);
252b5132
RH
11012
11013 exp = &disp_expressions[i.disp_operands];
520dc8e8 11014 i.op[this_operand].disps = exp;
252b5132
RH
11015 i.disp_operands++;
11016 save_input_line_pointer = input_line_pointer;
11017 input_line_pointer = disp_start;
11018 END_STRING_AND_SAVE (disp_end);
11019
11020#ifndef GCC_ASM_O_HACK
11021#define GCC_ASM_O_HACK 0
11022#endif
11023#if GCC_ASM_O_HACK
11024 END_STRING_AND_SAVE (disp_end + 1);
40fb9820 11025 if (i.types[this_operand].bitfield.baseIndex
24eab124 11026 && displacement_string_end[-1] == '+')
252b5132
RH
11027 {
11028 /* This hack is to avoid a warning when using the "o"
24eab124
AM
11029 constraint within gcc asm statements.
11030 For instance:
11031
11032 #define _set_tssldt_desc(n,addr,limit,type) \
11033 __asm__ __volatile__ ( \
11034 "movw %w2,%0\n\t" \
11035 "movw %w1,2+%0\n\t" \
11036 "rorl $16,%1\n\t" \
11037 "movb %b1,4+%0\n\t" \
11038 "movb %4,5+%0\n\t" \
11039 "movb $0,6+%0\n\t" \
11040 "movb %h1,7+%0\n\t" \
11041 "rorl $16,%1" \
11042 : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
11043
11044 This works great except that the output assembler ends
11045 up looking a bit weird if it turns out that there is
11046 no offset. You end up producing code that looks like:
11047
11048 #APP
11049 movw $235,(%eax)
11050 movw %dx,2+(%eax)
11051 rorl $16,%edx
11052 movb %dl,4+(%eax)
11053 movb $137,5+(%eax)
11054 movb $0,6+(%eax)
11055 movb %dh,7+(%eax)
11056 rorl $16,%edx
11057 #NO_APP
11058
47926f60 11059 So here we provide the missing zero. */
24eab124
AM
11060
11061 *displacement_string_end = '0';
252b5132
RH
11062 }
11063#endif
d258b828 11064 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
11065 if (gotfree_input_line)
11066 input_line_pointer = gotfree_input_line;
252b5132 11067
24eab124 11068 exp_seg = expression (exp);
252b5132 11069
636c26b0
AM
11070 SKIP_WHITESPACE ();
11071 if (*input_line_pointer)
11072 as_bad (_("junk `%s' after expression"), input_line_pointer);
11073#if GCC_ASM_O_HACK
11074 RESTORE_END_STRING (disp_end + 1);
11075#endif
636c26b0 11076 input_line_pointer = save_input_line_pointer;
636c26b0 11077 if (gotfree_input_line)
ee86248c
JB
11078 {
11079 free (gotfree_input_line);
11080
11081 if (exp->X_op == O_constant || exp->X_op == O_register)
11082 exp->X_op = O_illegal;
11083 }
11084
11085 ret = i386_finalize_displacement (exp_seg, exp, types, disp_start);
11086
11087 RESTORE_END_STRING (disp_end);
11088
11089 return ret;
11090}
11091
11092static int
11093i386_finalize_displacement (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
11094 i386_operand_type types, const char *disp_start)
11095{
ee86248c 11096 int ret = 1;
636c26b0 11097
24eab124
AM
11098 /* We do this to make sure that the section symbol is in
11099 the symbol table. We will ultimately change the relocation
47926f60 11100 to be relative to the beginning of the section. */
1ae12ab7 11101 if (i.reloc[this_operand] == BFD_RELOC_386_GOTOFF
d6ab8113
JB
11102 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
11103 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
24eab124 11104 {
636c26b0 11105 if (exp->X_op != O_symbol)
3992d3b7 11106 goto inv_disp;
636c26b0 11107
e5cb08ac 11108 if (S_IS_LOCAL (exp->X_add_symbol)
c64efb4b
L
11109 && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section
11110 && S_GET_SEGMENT (exp->X_add_symbol) != expr_section)
24eab124 11111 section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
24eab124
AM
11112 exp->X_op = O_subtract;
11113 exp->X_op_symbol = GOT_symbol;
1ae12ab7 11114 if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
29b0f896 11115 i.reloc[this_operand] = BFD_RELOC_32_PCREL;
d6ab8113
JB
11116 else if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
11117 i.reloc[this_operand] = BFD_RELOC_64;
23df1078 11118 else
29b0f896 11119 i.reloc[this_operand] = BFD_RELOC_32;
24eab124 11120 }
252b5132 11121
3992d3b7
AM
11122 else if (exp->X_op == O_absent
11123 || exp->X_op == O_illegal
ee86248c 11124 || exp->X_op == O_big)
2daf4fd8 11125 {
3992d3b7
AM
11126 inv_disp:
11127 as_bad (_("missing or invalid displacement expression `%s'"),
2daf4fd8 11128 disp_start);
3992d3b7 11129 ret = 0;
2daf4fd8
AM
11130 }
11131
a50187b2
JB
11132 else if (exp->X_op == O_constant)
11133 {
11134 /* Sizing gets taken care of by optimize_disp().
11135
11136 If not 64bit, sign/zero extend val, to account for wraparound
11137 when !BFD64. */
11138 if (flag_code != CODE_64BIT)
11139 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
11140 }
11141
4c63da97 11142#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
a50187b2 11143 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
3992d3b7
AM
11144 && exp_seg != absolute_section
11145 && exp_seg != text_section
11146 && exp_seg != data_section
11147 && exp_seg != bss_section
11148 && exp_seg != undefined_section
11149 && !bfd_is_com_section (exp_seg))
24eab124 11150 {
d0b47220 11151 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
3992d3b7 11152 ret = 0;
24eab124 11153 }
252b5132 11154#endif
3956db08 11155
a50187b2 11156 else if (current_templates->start->opcode_modifier.jump == JUMP_BYTE)
48bcea9f
JB
11157 i.types[this_operand].bitfield.disp8 = 1;
11158
40fb9820 11159 /* Check if this is a displacement only operand. */
02b83698
JB
11160 if (!i.types[this_operand].bitfield.baseindex)
11161 i.types[this_operand] =
11162 operand_type_or (operand_type_and_not (i.types[this_operand], anydisp),
11163 operand_type_and (i.types[this_operand], types));
3956db08 11164
3992d3b7 11165 return ret;
252b5132
RH
11166}
11167
2abc2bec
JB
11168/* Return the active addressing mode, taking address override and
11169 registers forming the address into consideration. Update the
11170 address override prefix if necessary. */
47926f60 11171
2abc2bec
JB
11172static enum flag_code
11173i386_addressing_mode (void)
252b5132 11174{
be05d201
L
11175 enum flag_code addr_mode;
11176
11177 if (i.prefix[ADDR_PREFIX])
11178 addr_mode = flag_code == CODE_32BIT ? CODE_16BIT : CODE_32BIT;
a23b33b3
JB
11179 else if (flag_code == CODE_16BIT
11180 && current_templates->start->cpu_flags.bitfield.cpumpx
11181 /* Avoid replacing the "16-bit addressing not allowed" diagnostic
11182 from md_assemble() by "is not a valid base/index expression"
11183 when there is a base and/or index. */
11184 && !i.types[this_operand].bitfield.baseindex)
11185 {
11186 /* MPX insn memory operands with neither base nor index must be forced
11187 to use 32-bit addressing in 16-bit mode. */
11188 addr_mode = CODE_32BIT;
11189 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
11190 ++i.prefixes;
11191 gas_assert (!i.types[this_operand].bitfield.disp16);
11192 gas_assert (!i.types[this_operand].bitfield.disp32);
11193 }
be05d201
L
11194 else
11195 {
11196 addr_mode = flag_code;
11197
24eab124 11198#if INFER_ADDR_PREFIX
be05d201
L
11199 if (i.mem_operands == 0)
11200 {
11201 /* Infer address prefix from the first memory operand. */
11202 const reg_entry *addr_reg = i.base_reg;
11203
11204 if (addr_reg == NULL)
11205 addr_reg = i.index_reg;
eecb386c 11206
be05d201
L
11207 if (addr_reg)
11208 {
e968fc9b 11209 if (addr_reg->reg_type.bitfield.dword)
be05d201
L
11210 addr_mode = CODE_32BIT;
11211 else if (flag_code != CODE_64BIT
dc821c5f 11212 && addr_reg->reg_type.bitfield.word)
be05d201
L
11213 addr_mode = CODE_16BIT;
11214
11215 if (addr_mode != flag_code)
11216 {
11217 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
11218 i.prefixes += 1;
11219 /* Change the size of any displacement too. At most one
11220 of Disp16 or Disp32 is set.
11221 FIXME. There doesn't seem to be any real need for
11222 separate Disp16 and Disp32 flags. The same goes for
11223 Imm16 and Imm32. Removing them would probably clean
11224 up the code quite a lot. */
11225 if (flag_code != CODE_64BIT
11226 && (i.types[this_operand].bitfield.disp16
11227 || i.types[this_operand].bitfield.disp32))
11228 i.types[this_operand]
11229 = operand_type_xor (i.types[this_operand], disp16_32);
11230 }
11231 }
11232 }
24eab124 11233#endif
be05d201
L
11234 }
11235
2abc2bec
JB
11236 return addr_mode;
11237}
11238
11239/* Make sure the memory operand we've been dealt is valid.
11240 Return 1 on success, 0 on a failure. */
11241
11242static int
11243i386_index_check (const char *operand_string)
11244{
11245 const char *kind = "base/index";
11246 enum flag_code addr_mode = i386_addressing_mode ();
a152332d 11247 const insn_template *t = current_templates->start;
2abc2bec 11248
a152332d 11249 if (t->opcode_modifier.isstring
fc0763e6
JB
11250 && (current_templates->end[-1].opcode_modifier.isstring
11251 || i.mem_operands))
11252 {
11253 /* Memory operands of string insns are special in that they only allow
11254 a single register (rDI, rSI, or rBX) as their memory address. */
be05d201
L
11255 const reg_entry *expected_reg;
11256 static const char *di_si[][2] =
11257 {
11258 { "esi", "edi" },
11259 { "si", "di" },
11260 { "rsi", "rdi" }
11261 };
11262 static const char *bx[] = { "ebx", "bx", "rbx" };
fc0763e6
JB
11263
11264 kind = "string address";
11265
a152332d 11266 if (t->opcode_modifier.prefixok == PrefixRep)
fc0763e6 11267 {
51c8edf6
JB
11268 int es_op = current_templates->end[-1].opcode_modifier.isstring
11269 - IS_STRING_ES_OP0;
11270 int op = 0;
fc0763e6 11271
51c8edf6 11272 if (!current_templates->end[-1].operand_types[0].bitfield.baseindex
fc0763e6
JB
11273 || ((!i.mem_operands != !intel_syntax)
11274 && current_templates->end[-1].operand_types[1]
11275 .bitfield.baseindex))
51c8edf6 11276 op = 1;
fe0e921f
AM
11277 expected_reg
11278 = (const reg_entry *) str_hash_find (reg_hash,
11279 di_si[addr_mode][op == es_op]);
fc0763e6
JB
11280 }
11281 else
fe0e921f
AM
11282 expected_reg
11283 = (const reg_entry *)str_hash_find (reg_hash, bx[addr_mode]);
fc0763e6 11284
be05d201
L
11285 if (i.base_reg != expected_reg
11286 || i.index_reg
fc0763e6 11287 || operand_type_check (i.types[this_operand], disp))
fc0763e6 11288 {
be05d201
L
11289 /* The second memory operand must have the same size as
11290 the first one. */
11291 if (i.mem_operands
11292 && i.base_reg
11293 && !((addr_mode == CODE_64BIT
dc821c5f 11294 && i.base_reg->reg_type.bitfield.qword)
be05d201 11295 || (addr_mode == CODE_32BIT
dc821c5f
JB
11296 ? i.base_reg->reg_type.bitfield.dword
11297 : i.base_reg->reg_type.bitfield.word)))
be05d201
L
11298 goto bad_address;
11299
fc0763e6
JB
11300 as_warn (_("`%s' is not valid here (expected `%c%s%s%c')"),
11301 operand_string,
11302 intel_syntax ? '[' : '(',
11303 register_prefix,
be05d201 11304 expected_reg->reg_name,
fc0763e6 11305 intel_syntax ? ']' : ')');
be05d201 11306 return 1;
fc0763e6 11307 }
be05d201
L
11308 else
11309 return 1;
11310
dc1e8a47 11311 bad_address:
be05d201
L
11312 as_bad (_("`%s' is not a valid %s expression"),
11313 operand_string, kind);
11314 return 0;
3e73aa7c
JH
11315 }
11316 else
11317 {
be05d201
L
11318 if (addr_mode != CODE_16BIT)
11319 {
11320 /* 32-bit/64-bit checks. */
41eb8e88
L
11321 if (i.disp_encoding == disp_encoding_16bit)
11322 {
11323 bad_disp:
11324 as_bad (_("invalid `%s' prefix"),
11325 addr_mode == CODE_16BIT ? "{disp32}" : "{disp16}");
11326 return 0;
11327 }
11328
be05d201 11329 if ((i.base_reg
e968fc9b
JB
11330 && ((addr_mode == CODE_64BIT
11331 ? !i.base_reg->reg_type.bitfield.qword
11332 : !i.base_reg->reg_type.bitfield.dword)
11333 || (i.index_reg && i.base_reg->reg_num == RegIP)
11334 || i.base_reg->reg_num == RegIZ))
be05d201 11335 || (i.index_reg
1b54b8d7
JB
11336 && !i.index_reg->reg_type.bitfield.xmmword
11337 && !i.index_reg->reg_type.bitfield.ymmword
11338 && !i.index_reg->reg_type.bitfield.zmmword
be05d201 11339 && ((addr_mode == CODE_64BIT
e968fc9b
JB
11340 ? !i.index_reg->reg_type.bitfield.qword
11341 : !i.index_reg->reg_type.bitfield.dword)
be05d201
L
11342 || !i.index_reg->reg_type.bitfield.baseindex)))
11343 goto bad_address;
8178be5b 11344
260cd341 11345 /* bndmk, bndldx, bndstx and mandatory non-vector SIB have special restrictions. */
a152332d 11346 if ((t->opcode_modifier.opcodeprefix == PREFIX_0XF3
389d00a5
JB
11347 && t->opcode_modifier.opcodespace == SPACE_0F
11348 && t->base_opcode == 0x1b)
a152332d 11349 || (t->opcode_modifier.opcodeprefix == PREFIX_NONE
389d00a5
JB
11350 && t->opcode_modifier.opcodespace == SPACE_0F
11351 && (t->base_opcode & ~1) == 0x1a)
a152332d 11352 || t->opcode_modifier.sib == SIBMEM)
8178be5b
JB
11353 {
11354 /* They cannot use RIP-relative addressing. */
e968fc9b 11355 if (i.base_reg && i.base_reg->reg_num == RegIP)
8178be5b
JB
11356 {
11357 as_bad (_("`%s' cannot be used here"), operand_string);
11358 return 0;
11359 }
11360
11361 /* bndldx and bndstx ignore their scale factor. */
a152332d 11362 if (t->opcode_modifier.opcodeprefix == PREFIX_NONE
389d00a5
JB
11363 && t->opcode_modifier.opcodespace == SPACE_0F
11364 && (t->base_opcode & ~1) == 0x1a
8178be5b
JB
11365 && i.log2_scale_factor)
11366 as_warn (_("register scaling is being ignored here"));
11367 }
be05d201
L
11368 }
11369 else
3e73aa7c 11370 {
be05d201 11371 /* 16-bit checks. */
41eb8e88
L
11372 if (i.disp_encoding == disp_encoding_32bit)
11373 goto bad_disp;
11374
3e73aa7c 11375 if ((i.base_reg
dc821c5f 11376 && (!i.base_reg->reg_type.bitfield.word
40fb9820 11377 || !i.base_reg->reg_type.bitfield.baseindex))
3e73aa7c 11378 || (i.index_reg
dc821c5f 11379 && (!i.index_reg->reg_type.bitfield.word
40fb9820 11380 || !i.index_reg->reg_type.bitfield.baseindex
29b0f896
AM
11381 || !(i.base_reg
11382 && i.base_reg->reg_num < 6
11383 && i.index_reg->reg_num >= 6
11384 && i.log2_scale_factor == 0))))
be05d201 11385 goto bad_address;
3e73aa7c
JH
11386 }
11387 }
be05d201 11388 return 1;
24eab124 11389}
252b5132 11390
43234a1e
L
11391/* Handle vector immediates. */
11392
11393static int
11394RC_SAE_immediate (const char *imm_start)
11395{
43234a1e 11396 const char *pstr = imm_start;
43234a1e
L
11397
11398 if (*pstr != '{')
11399 return 0;
11400
7063667e
JB
11401 pstr = RC_SAE_specifier (pstr + 1);
11402 if (pstr == NULL)
43234a1e
L
11403 return 0;
11404
11405 if (*pstr++ != '}')
11406 {
11407 as_bad (_("Missing '}': '%s'"), imm_start);
11408 return 0;
11409 }
11410 /* RC/SAE immediate string should contain nothing more. */;
11411 if (*pstr != 0)
11412 {
11413 as_bad (_("Junk after '}': '%s'"), imm_start);
11414 return 0;
11415 }
11416
cf665fee
JB
11417 /* Internally this doesn't count as an operand. */
11418 --i.operands;
43234a1e 11419
43234a1e
L
11420 return 1;
11421}
11422
8325cc63
JB
11423/* Only string instructions can have a second memory operand, so
11424 reduce current_templates to just those if it contains any. */
11425static int
11426maybe_adjust_templates (void)
11427{
11428 const insn_template *t;
11429
11430 gas_assert (i.mem_operands == 1);
11431
11432 for (t = current_templates->start; t < current_templates->end; ++t)
11433 if (t->opcode_modifier.isstring)
11434 break;
11435
11436 if (t < current_templates->end)
11437 {
11438 static templates aux_templates;
5b7c81bd 11439 bool recheck;
8325cc63
JB
11440
11441 aux_templates.start = t;
11442 for (; t < current_templates->end; ++t)
11443 if (!t->opcode_modifier.isstring)
11444 break;
11445 aux_templates.end = t;
11446
11447 /* Determine whether to re-check the first memory operand. */
11448 recheck = (aux_templates.start != current_templates->start
11449 || t != current_templates->end);
11450
11451 current_templates = &aux_templates;
11452
11453 if (recheck)
11454 {
11455 i.mem_operands = 0;
11456 if (i.memop1_string != NULL
11457 && i386_index_check (i.memop1_string) == 0)
11458 return 0;
11459 i.mem_operands = 1;
11460 }
11461 }
11462
11463 return 1;
11464}
11465
9d299bea
JB
11466static INLINE bool starts_memory_operand (char c)
11467{
014fbcda 11468 return ISDIGIT (c)
9d299bea 11469 || is_identifier_char (c)
014fbcda 11470 || strchr ("([\"+-!~", c);
9d299bea
JB
11471}
11472
fc0763e6 11473/* Parse OPERAND_STRING into the i386_insn structure I. Returns zero
47926f60 11474 on error. */
252b5132 11475
252b5132 11476static int
a7619375 11477i386_att_operand (char *operand_string)
252b5132 11478{
af6bdddf
AM
11479 const reg_entry *r;
11480 char *end_op;
24eab124 11481 char *op_string = operand_string;
252b5132 11482
24eab124 11483 if (is_space_char (*op_string))
252b5132
RH
11484 ++op_string;
11485
24eab124 11486 /* We check for an absolute prefix (differentiating,
47926f60 11487 for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
24eab124
AM
11488 if (*op_string == ABSOLUTE_PREFIX)
11489 {
11490 ++op_string;
11491 if (is_space_char (*op_string))
11492 ++op_string;
5b7c81bd 11493 i.jumpabsolute = true;
24eab124 11494 }
252b5132 11495
47926f60 11496 /* Check if operand is a register. */
4d1bb795 11497 if ((r = parse_register (op_string, &end_op)) != NULL)
24eab124 11498 {
40fb9820
L
11499 i386_operand_type temp;
11500
8a6fb3f9
JB
11501 if (r == &bad_reg)
11502 return 0;
11503
24eab124
AM
11504 /* Check for a segment override by searching for ':' after a
11505 segment register. */
11506 op_string = end_op;
11507 if (is_space_char (*op_string))
11508 ++op_string;
00cee14f 11509 if (*op_string == ':' && r->reg_type.bitfield.class == SReg)
24eab124 11510 {
5e042380 11511 i.seg[i.mem_operands] = r;
252b5132 11512
24eab124 11513 /* Skip the ':' and whitespace. */
252b5132
RH
11514 ++op_string;
11515 if (is_space_char (*op_string))
24eab124 11516 ++op_string;
252b5132 11517
47926f60 11518 /* Handle case of %es:*foo. */
c8d541e2 11519 if (!i.jumpabsolute && *op_string == ABSOLUTE_PREFIX)
24eab124
AM
11520 {
11521 ++op_string;
11522 if (is_space_char (*op_string))
11523 ++op_string;
5b7c81bd 11524 i.jumpabsolute = true;
24eab124 11525 }
c8d541e2 11526
9d299bea 11527 if (!starts_memory_operand (*op_string))
c8d541e2
JB
11528 {
11529 as_bad (_("bad memory operand `%s'"), op_string);
11530 return 0;
11531 }
24eab124
AM
11532 goto do_memory_reference;
11533 }
43234a1e
L
11534
11535 /* Handle vector operations. */
11536 if (*op_string == '{')
11537 {
f70c6814 11538 op_string = check_VecOperations (op_string);
43234a1e
L
11539 if (op_string == NULL)
11540 return 0;
11541 }
11542
24eab124
AM
11543 if (*op_string)
11544 {
d0b47220 11545 as_bad (_("junk `%s' after register"), op_string);
24eab124
AM
11546 return 0;
11547 }
40fb9820
L
11548 temp = r->reg_type;
11549 temp.bitfield.baseindex = 0;
c6fb90c8
L
11550 i.types[this_operand] = operand_type_or (i.types[this_operand],
11551 temp);
7d5e4556 11552 i.types[this_operand].bitfield.unspecified = 0;
520dc8e8 11553 i.op[this_operand].regs = r;
24eab124 11554 i.reg_operands++;
cf665fee
JB
11555
11556 /* A GPR may follow an RC or SAE immediate only if a (vector) register
11557 operand was also present earlier on. */
11558 if (i.rounding.type != rc_none && temp.bitfield.class == Reg
11559 && i.reg_operands == 1)
11560 {
11561 unsigned int j;
11562
11563 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); ++j)
11564 if (i.rounding.type == RC_NamesTable[j].type)
11565 break;
11566 as_bad (_("`%s': misplaced `{%s}'"),
11567 current_templates->start->name, RC_NamesTable[j].name);
11568 return 0;
11569 }
24eab124 11570 }
af6bdddf
AM
11571 else if (*op_string == REGISTER_PREFIX)
11572 {
11573 as_bad (_("bad register name `%s'"), op_string);
11574 return 0;
11575 }
24eab124 11576 else if (*op_string == IMMEDIATE_PREFIX)
ce8a8b2f 11577 {
24eab124 11578 ++op_string;
6f2f06be 11579 if (i.jumpabsolute)
24eab124 11580 {
d0b47220 11581 as_bad (_("immediate operand illegal with absolute jump"));
24eab124
AM
11582 return 0;
11583 }
11584 if (!i386_immediate (op_string))
11585 return 0;
cf665fee
JB
11586 if (i.rounding.type != rc_none)
11587 {
11588 as_bad (_("`%s': RC/SAE operand must follow immediate operands"),
11589 current_templates->start->name);
11590 return 0;
11591 }
24eab124 11592 }
43234a1e
L
11593 else if (RC_SAE_immediate (operand_string))
11594 {
cf665fee
JB
11595 /* If it is a RC or SAE immediate, do the necessary placement check:
11596 Only another immediate or a GPR may precede it. */
11597 if (i.mem_operands || i.reg_operands + i.imm_operands > 1
11598 || (i.reg_operands == 1
11599 && i.op[0].regs->reg_type.bitfield.class != Reg))
11600 {
11601 as_bad (_("`%s': misplaced `%s'"),
11602 current_templates->start->name, operand_string);
11603 return 0;
11604 }
43234a1e 11605 }
9d299bea 11606 else if (starts_memory_operand (*op_string))
24eab124 11607 {
47926f60 11608 /* This is a memory reference of some sort. */
af6bdddf 11609 char *base_string;
252b5132 11610
47926f60 11611 /* Start and end of displacement string expression (if found). */
eecb386c
AM
11612 char *displacement_string_start;
11613 char *displacement_string_end;
252b5132 11614
24eab124 11615 do_memory_reference:
8325cc63
JB
11616 if (i.mem_operands == 1 && !maybe_adjust_templates ())
11617 return 0;
24eab124 11618 if ((i.mem_operands == 1
40fb9820 11619 && !current_templates->start->opcode_modifier.isstring)
24eab124
AM
11620 || i.mem_operands == 2)
11621 {
11622 as_bad (_("too many memory references for `%s'"),
11623 current_templates->start->name);
11624 return 0;
11625 }
252b5132 11626
24eab124
AM
11627 /* Check for base index form. We detect the base index form by
11628 looking for an ')' at the end of the operand, searching
11629 for the '(' matching it, and finding a REGISTER_PREFIX or ','
11630 after the '('. */
af6bdddf 11631 base_string = op_string + strlen (op_string);
c3332e24 11632
43234a1e 11633 /* Handle vector operations. */
6b5ba0d4
JB
11634 --base_string;
11635 if (is_space_char (*base_string))
11636 --base_string;
11637
11638 if (*base_string == '}')
43234a1e 11639 {
6b5ba0d4
JB
11640 char *vop_start = NULL;
11641
11642 while (base_string-- > op_string)
11643 {
11644 if (*base_string == '"')
11645 break;
11646 if (*base_string != '{')
11647 continue;
11648
11649 vop_start = base_string;
11650
11651 --base_string;
11652 if (is_space_char (*base_string))
11653 --base_string;
11654
11655 if (*base_string != '}')
11656 break;
11657
11658 vop_start = NULL;
11659 }
11660
11661 if (!vop_start)
11662 {
11663 as_bad (_("unbalanced figure braces"));
11664 return 0;
11665 }
11666
f70c6814 11667 if (check_VecOperations (vop_start) == NULL)
43234a1e 11668 return 0;
43234a1e
L
11669 }
11670
47926f60 11671 /* If we only have a displacement, set-up for it to be parsed later. */
af6bdddf
AM
11672 displacement_string_start = op_string;
11673 displacement_string_end = base_string + 1;
252b5132 11674
24eab124
AM
11675 if (*base_string == ')')
11676 {
af6bdddf 11677 char *temp_string;
e87fb6a6
JB
11678 unsigned int parens_not_balanced = 0;
11679 bool in_quotes = false;
e68c3d59 11680
24eab124 11681 /* We've already checked that the number of left & right ()'s are
e87fb6a6
JB
11682 equal, and that there's a matching set of double quotes. */
11683 end_op = base_string;
11684 for (temp_string = op_string; temp_string < end_op; temp_string++)
24eab124 11685 {
e87fb6a6
JB
11686 if (*temp_string == '\\' && temp_string[1] == '"')
11687 ++temp_string;
11688 else if (*temp_string == '"')
11689 in_quotes = !in_quotes;
11690 else if (!in_quotes)
11691 {
11692 if (*temp_string == '(' && !parens_not_balanced++)
11693 base_string = temp_string;
11694 if (*temp_string == ')')
11695 --parens_not_balanced;
11696 }
24eab124 11697 }
c3332e24 11698
af6bdddf 11699 temp_string = base_string;
c3332e24 11700
24eab124 11701 /* Skip past '(' and whitespace. */
e87fb6a6
JB
11702 gas_assert (*base_string == '(');
11703 ++base_string;
252b5132 11704 if (is_space_char (*base_string))
24eab124 11705 ++base_string;
252b5132 11706
af6bdddf 11707 if (*base_string == ','
4eed87de
AM
11708 || ((i.base_reg = parse_register (base_string, &end_op))
11709 != NULL))
252b5132 11710 {
af6bdddf 11711 displacement_string_end = temp_string;
252b5132 11712
40fb9820 11713 i.types[this_operand].bitfield.baseindex = 1;
252b5132 11714
af6bdddf 11715 if (i.base_reg)
24eab124 11716 {
8a6fb3f9
JB
11717 if (i.base_reg == &bad_reg)
11718 return 0;
24eab124
AM
11719 base_string = end_op;
11720 if (is_space_char (*base_string))
11721 ++base_string;
af6bdddf
AM
11722 }
11723
11724 /* There may be an index reg or scale factor here. */
11725 if (*base_string == ',')
11726 {
11727 ++base_string;
11728 if (is_space_char (*base_string))
11729 ++base_string;
11730
4eed87de
AM
11731 if ((i.index_reg = parse_register (base_string, &end_op))
11732 != NULL)
24eab124 11733 {
8a6fb3f9
JB
11734 if (i.index_reg == &bad_reg)
11735 return 0;
af6bdddf 11736 base_string = end_op;
24eab124
AM
11737 if (is_space_char (*base_string))
11738 ++base_string;
af6bdddf
AM
11739 if (*base_string == ',')
11740 {
11741 ++base_string;
11742 if (is_space_char (*base_string))
11743 ++base_string;
11744 }
e5cb08ac 11745 else if (*base_string != ')')
af6bdddf 11746 {
4eed87de
AM
11747 as_bad (_("expecting `,' or `)' "
11748 "after index register in `%s'"),
af6bdddf
AM
11749 operand_string);
11750 return 0;
11751 }
24eab124 11752 }
af6bdddf 11753 else if (*base_string == REGISTER_PREFIX)
24eab124 11754 {
f76bf5e0
L
11755 end_op = strchr (base_string, ',');
11756 if (end_op)
11757 *end_op = '\0';
af6bdddf 11758 as_bad (_("bad register name `%s'"), base_string);
24eab124
AM
11759 return 0;
11760 }
252b5132 11761
47926f60 11762 /* Check for scale factor. */
551c1ca1 11763 if (*base_string != ')')
af6bdddf 11764 {
551c1ca1
AM
11765 char *end_scale = i386_scale (base_string);
11766
11767 if (!end_scale)
af6bdddf 11768 return 0;
24eab124 11769
551c1ca1 11770 base_string = end_scale;
af6bdddf
AM
11771 if (is_space_char (*base_string))
11772 ++base_string;
11773 if (*base_string != ')')
11774 {
4eed87de
AM
11775 as_bad (_("expecting `)' "
11776 "after scale factor in `%s'"),
af6bdddf
AM
11777 operand_string);
11778 return 0;
11779 }
11780 }
11781 else if (!i.index_reg)
24eab124 11782 {
4eed87de
AM
11783 as_bad (_("expecting index register or scale factor "
11784 "after `,'; got '%c'"),
af6bdddf 11785 *base_string);
24eab124
AM
11786 return 0;
11787 }
11788 }
af6bdddf 11789 else if (*base_string != ')')
24eab124 11790 {
4eed87de
AM
11791 as_bad (_("expecting `,' or `)' "
11792 "after base register in `%s'"),
af6bdddf 11793 operand_string);
24eab124
AM
11794 return 0;
11795 }
c3332e24 11796 }
af6bdddf 11797 else if (*base_string == REGISTER_PREFIX)
c3332e24 11798 {
f76bf5e0
L
11799 end_op = strchr (base_string, ',');
11800 if (end_op)
11801 *end_op = '\0';
af6bdddf 11802 as_bad (_("bad register name `%s'"), base_string);
24eab124 11803 return 0;
c3332e24 11804 }
24eab124
AM
11805 }
11806
11807 /* If there's an expression beginning the operand, parse it,
11808 assuming displacement_string_start and
11809 displacement_string_end are meaningful. */
11810 if (displacement_string_start != displacement_string_end)
11811 {
11812 if (!i386_displacement (displacement_string_start,
11813 displacement_string_end))
11814 return 0;
11815 }
11816
11817 /* Special case for (%dx) while doing input/output op. */
11818 if (i.base_reg
75e5731b
JB
11819 && i.base_reg->reg_type.bitfield.instance == RegD
11820 && i.base_reg->reg_type.bitfield.word
24eab124
AM
11821 && i.index_reg == 0
11822 && i.log2_scale_factor == 0
11823 && i.seg[i.mem_operands] == 0
40fb9820 11824 && !operand_type_check (i.types[this_operand], disp))
24eab124 11825 {
2fb5be8d 11826 i.types[this_operand] = i.base_reg->reg_type;
9373f275 11827 i.input_output_operand = true;
24eab124
AM
11828 return 1;
11829 }
11830
eecb386c
AM
11831 if (i386_index_check (operand_string) == 0)
11832 return 0;
c48dadc9 11833 i.flags[this_operand] |= Operand_Mem;
8325cc63
JB
11834 if (i.mem_operands == 0)
11835 i.memop1_string = xstrdup (operand_string);
24eab124
AM
11836 i.mem_operands++;
11837 }
11838 else
ce8a8b2f
AM
11839 {
11840 /* It's not a memory operand; argh! */
24eab124
AM
11841 as_bad (_("invalid char %s beginning operand %d `%s'"),
11842 output_invalid (*op_string),
11843 this_operand + 1,
11844 op_string);
11845 return 0;
11846 }
47926f60 11847 return 1; /* Normal return. */
252b5132
RH
11848}
11849\f
fa94de6b
RM
11850/* Calculate the maximum variable size (i.e., excluding fr_fix)
11851 that an rs_machine_dependent frag may reach. */
11852
11853unsigned int
11854i386_frag_max_var (fragS *frag)
11855{
11856 /* The only relaxable frags are for jumps.
11857 Unconditional jumps can grow by 4 bytes and others by 5 bytes. */
11858 gas_assert (frag->fr_type == rs_machine_dependent);
11859 return TYPE_FROM_RELAX_STATE (frag->fr_subtype) == UNCOND_JUMP ? 4 : 5;
11860}
11861
b084df0b
L
11862#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
11863static int
8dcea932 11864elf_symbol_resolved_in_segment_p (symbolS *fr_symbol, offsetT fr_var)
b084df0b
L
11865{
11866 /* STT_GNU_IFUNC symbol must go through PLT. */
11867 if ((symbol_get_bfdsym (fr_symbol)->flags
11868 & BSF_GNU_INDIRECT_FUNCTION) != 0)
11869 return 0;
11870
11871 if (!S_IS_EXTERNAL (fr_symbol))
11872 /* Symbol may be weak or local. */
11873 return !S_IS_WEAK (fr_symbol);
11874
8dcea932
L
11875 /* Global symbols with non-default visibility can't be preempted. */
11876 if (ELF_ST_VISIBILITY (S_GET_OTHER (fr_symbol)) != STV_DEFAULT)
11877 return 1;
11878
11879 if (fr_var != NO_RELOC)
11880 switch ((enum bfd_reloc_code_real) fr_var)
11881 {
11882 case BFD_RELOC_386_PLT32:
11883 case BFD_RELOC_X86_64_PLT32:
33eaf5de 11884 /* Symbol with PLT relocation may be preempted. */
8dcea932
L
11885 return 0;
11886 default:
11887 abort ();
11888 }
11889
b084df0b
L
11890 /* Global symbols with default visibility in a shared library may be
11891 preempted by another definition. */
8dcea932 11892 return !shared;
b084df0b
L
11893}
11894#endif
11895
79d72f45
HL
11896/* Table 3-2. Macro-Fusible Instructions in Haswell Microarchitecture
11897 Note also work for Skylake and Cascadelake.
11898---------------------------------------------------------------------
11899| JCC | ADD/SUB/CMP | INC/DEC | TEST/AND |
11900| ------ | ----------- | ------- | -------- |
11901| Jo | N | N | Y |
11902| Jno | N | N | Y |
11903| Jc/Jb | Y | N | Y |
11904| Jae/Jnb | Y | N | Y |
11905| Je/Jz | Y | Y | Y |
11906| Jne/Jnz | Y | Y | Y |
11907| Jna/Jbe | Y | N | Y |
11908| Ja/Jnbe | Y | N | Y |
11909| Js | N | N | Y |
11910| Jns | N | N | Y |
11911| Jp/Jpe | N | N | Y |
11912| Jnp/Jpo | N | N | Y |
11913| Jl/Jnge | Y | Y | Y |
11914| Jge/Jnl | Y | Y | Y |
11915| Jle/Jng | Y | Y | Y |
11916| Jg/Jnle | Y | Y | Y |
11917--------------------------------------------------------------------- */
11918static int
11919i386_macro_fusible_p (enum mf_cmp_kind mf_cmp, enum mf_jcc_kind mf_jcc)
11920{
11921 if (mf_cmp == mf_cmp_alu_cmp)
11922 return ((mf_jcc >= mf_jcc_jc && mf_jcc <= mf_jcc_jna)
11923 || mf_jcc == mf_jcc_jl || mf_jcc == mf_jcc_jle);
11924 if (mf_cmp == mf_cmp_incdec)
11925 return (mf_jcc == mf_jcc_je || mf_jcc == mf_jcc_jl
11926 || mf_jcc == mf_jcc_jle);
11927 if (mf_cmp == mf_cmp_test_and)
11928 return 1;
11929 return 0;
11930}
11931
e379e5f3
L
11932/* Return the next non-empty frag. */
11933
11934static fragS *
11935i386_next_non_empty_frag (fragS *fragP)
11936{
11937 /* There may be a frag with a ".fill 0" when there is no room in
11938 the current frag for frag_grow in output_insn. */
11939 for (fragP = fragP->fr_next;
11940 (fragP != NULL
11941 && fragP->fr_type == rs_fill
11942 && fragP->fr_fix == 0);
11943 fragP = fragP->fr_next)
11944 ;
11945 return fragP;
11946}
11947
11948/* Return the next jcc frag after BRANCH_PADDING. */
11949
11950static fragS *
79d72f45 11951i386_next_fusible_jcc_frag (fragS *maybe_cmp_fragP, fragS *pad_fragP)
e379e5f3 11952{
79d72f45
HL
11953 fragS *branch_fragP;
11954 if (!pad_fragP)
e379e5f3
L
11955 return NULL;
11956
79d72f45
HL
11957 if (pad_fragP->fr_type == rs_machine_dependent
11958 && (TYPE_FROM_RELAX_STATE (pad_fragP->fr_subtype)
e379e5f3
L
11959 == BRANCH_PADDING))
11960 {
79d72f45
HL
11961 branch_fragP = i386_next_non_empty_frag (pad_fragP);
11962 if (branch_fragP->fr_type != rs_machine_dependent)
e379e5f3 11963 return NULL;
79d72f45
HL
11964 if (TYPE_FROM_RELAX_STATE (branch_fragP->fr_subtype) == COND_JUMP
11965 && i386_macro_fusible_p (maybe_cmp_fragP->tc_frag_data.mf_type,
11966 pad_fragP->tc_frag_data.mf_type))
11967 return branch_fragP;
e379e5f3
L
11968 }
11969
11970 return NULL;
11971}
11972
11973/* Classify BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags. */
11974
11975static void
11976i386_classify_machine_dependent_frag (fragS *fragP)
11977{
11978 fragS *cmp_fragP;
11979 fragS *pad_fragP;
11980 fragS *branch_fragP;
11981 fragS *next_fragP;
11982 unsigned int max_prefix_length;
11983
11984 if (fragP->tc_frag_data.classified)
11985 return;
11986
11987 /* First scan for BRANCH_PADDING and FUSED_JCC_PADDING. Convert
11988 FUSED_JCC_PADDING and merge BRANCH_PADDING. */
11989 for (next_fragP = fragP;
11990 next_fragP != NULL;
11991 next_fragP = next_fragP->fr_next)
11992 {
11993 next_fragP->tc_frag_data.classified = 1;
11994 if (next_fragP->fr_type == rs_machine_dependent)
11995 switch (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype))
11996 {
11997 case BRANCH_PADDING:
11998 /* The BRANCH_PADDING frag must be followed by a branch
11999 frag. */
12000 branch_fragP = i386_next_non_empty_frag (next_fragP);
12001 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
12002 break;
12003 case FUSED_JCC_PADDING:
12004 /* Check if this is a fused jcc:
12005 FUSED_JCC_PADDING
12006 CMP like instruction
12007 BRANCH_PADDING
12008 COND_JUMP
12009 */
12010 cmp_fragP = i386_next_non_empty_frag (next_fragP);
12011 pad_fragP = i386_next_non_empty_frag (cmp_fragP);
79d72f45 12012 branch_fragP = i386_next_fusible_jcc_frag (next_fragP, pad_fragP);
e379e5f3
L
12013 if (branch_fragP)
12014 {
12015 /* The BRANCH_PADDING frag is merged with the
12016 FUSED_JCC_PADDING frag. */
12017 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
12018 /* CMP like instruction size. */
12019 next_fragP->tc_frag_data.cmp_size = cmp_fragP->fr_fix;
12020 frag_wane (pad_fragP);
12021 /* Skip to branch_fragP. */
12022 next_fragP = branch_fragP;
12023 }
12024 else if (next_fragP->tc_frag_data.max_prefix_length)
12025 {
12026 /* Turn FUSED_JCC_PADDING into BRANCH_PREFIX if it isn't
12027 a fused jcc. */
12028 next_fragP->fr_subtype
12029 = ENCODE_RELAX_STATE (BRANCH_PREFIX, 0);
12030 next_fragP->tc_frag_data.max_bytes
12031 = next_fragP->tc_frag_data.max_prefix_length;
12032 /* This will be updated in the BRANCH_PREFIX scan. */
12033 next_fragP->tc_frag_data.max_prefix_length = 0;
12034 }
12035 else
12036 frag_wane (next_fragP);
12037 break;
12038 }
12039 }
12040
12041 /* Stop if there is no BRANCH_PREFIX. */
12042 if (!align_branch_prefix_size)
12043 return;
12044
12045 /* Scan for BRANCH_PREFIX. */
12046 for (; fragP != NULL; fragP = fragP->fr_next)
12047 {
12048 if (fragP->fr_type != rs_machine_dependent
12049 || (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
12050 != BRANCH_PREFIX))
12051 continue;
12052
12053 /* Count all BRANCH_PREFIX frags before BRANCH_PADDING and
12054 COND_JUMP_PREFIX. */
12055 max_prefix_length = 0;
12056 for (next_fragP = fragP;
12057 next_fragP != NULL;
12058 next_fragP = next_fragP->fr_next)
12059 {
12060 if (next_fragP->fr_type == rs_fill)
12061 /* Skip rs_fill frags. */
12062 continue;
12063 else if (next_fragP->fr_type != rs_machine_dependent)
12064 /* Stop for all other frags. */
12065 break;
12066
12067 /* rs_machine_dependent frags. */
12068 if (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12069 == BRANCH_PREFIX)
12070 {
12071 /* Count BRANCH_PREFIX frags. */
12072 if (max_prefix_length >= MAX_FUSED_JCC_PADDING_SIZE)
12073 {
12074 max_prefix_length = MAX_FUSED_JCC_PADDING_SIZE;
12075 frag_wane (next_fragP);
12076 }
12077 else
12078 max_prefix_length
12079 += next_fragP->tc_frag_data.max_bytes;
12080 }
12081 else if ((TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12082 == BRANCH_PADDING)
12083 || (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12084 == FUSED_JCC_PADDING))
12085 {
12086 /* Stop at BRANCH_PADDING and FUSED_JCC_PADDING. */
12087 fragP->tc_frag_data.u.padding_fragP = next_fragP;
12088 break;
12089 }
12090 else
12091 /* Stop for other rs_machine_dependent frags. */
12092 break;
12093 }
12094
12095 fragP->tc_frag_data.max_prefix_length = max_prefix_length;
12096
12097 /* Skip to the next frag. */
12098 fragP = next_fragP;
12099 }
12100}
12101
12102/* Compute padding size for
12103
12104 FUSED_JCC_PADDING
12105 CMP like instruction
12106 BRANCH_PADDING
12107 COND_JUMP/UNCOND_JUMP
12108
12109 or
12110
12111 BRANCH_PADDING
12112 COND_JUMP/UNCOND_JUMP
12113 */
12114
12115static int
12116i386_branch_padding_size (fragS *fragP, offsetT address)
12117{
12118 unsigned int offset, size, padding_size;
12119 fragS *branch_fragP = fragP->tc_frag_data.u.branch_fragP;
12120
12121 /* The start address of the BRANCH_PADDING or FUSED_JCC_PADDING frag. */
12122 if (!address)
12123 address = fragP->fr_address;
12124 address += fragP->fr_fix;
12125
12126 /* CMP like instrunction size. */
12127 size = fragP->tc_frag_data.cmp_size;
12128
12129 /* The base size of the branch frag. */
12130 size += branch_fragP->fr_fix;
12131
12132 /* Add opcode and displacement bytes for the rs_machine_dependent
12133 branch frag. */
12134 if (branch_fragP->fr_type == rs_machine_dependent)
12135 size += md_relax_table[branch_fragP->fr_subtype].rlx_length;
12136
12137 /* Check if branch is within boundary and doesn't end at the last
12138 byte. */
12139 offset = address & ((1U << align_branch_power) - 1);
12140 if ((offset + size) >= (1U << align_branch_power))
12141 /* Padding needed to avoid crossing boundary. */
12142 padding_size = (1U << align_branch_power) - offset;
12143 else
12144 /* No padding needed. */
12145 padding_size = 0;
12146
12147 /* The return value may be saved in tc_frag_data.length which is
12148 unsigned byte. */
12149 if (!fits_in_unsigned_byte (padding_size))
12150 abort ();
12151
12152 return padding_size;
12153}
12154
12155/* i386_generic_table_relax_frag()
12156
12157 Handle BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags to
12158 grow/shrink padding to align branch frags. Hand others to
12159 relax_frag(). */
12160
12161long
12162i386_generic_table_relax_frag (segT segment, fragS *fragP, long stretch)
12163{
12164 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12165 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
12166 {
12167 long padding_size = i386_branch_padding_size (fragP, 0);
12168 long grow = padding_size - fragP->tc_frag_data.length;
12169
12170 /* When the BRANCH_PREFIX frag is used, the computed address
12171 must match the actual address and there should be no padding. */
12172 if (fragP->tc_frag_data.padding_address
12173 && (fragP->tc_frag_data.padding_address != fragP->fr_address
12174 || padding_size))
12175 abort ();
12176
12177 /* Update the padding size. */
12178 if (grow)
12179 fragP->tc_frag_data.length = padding_size;
12180
12181 return grow;
12182 }
12183 else if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12184 {
12185 fragS *padding_fragP, *next_fragP;
12186 long padding_size, left_size, last_size;
12187
12188 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
12189 if (!padding_fragP)
12190 /* Use the padding set by the leading BRANCH_PREFIX frag. */
12191 return (fragP->tc_frag_data.length
12192 - fragP->tc_frag_data.last_length);
12193
12194 /* Compute the relative address of the padding frag in the very
12195 first time where the BRANCH_PREFIX frag sizes are zero. */
12196 if (!fragP->tc_frag_data.padding_address)
12197 fragP->tc_frag_data.padding_address
12198 = padding_fragP->fr_address - (fragP->fr_address - stretch);
12199
12200 /* First update the last length from the previous interation. */
12201 left_size = fragP->tc_frag_data.prefix_length;
12202 for (next_fragP = fragP;
12203 next_fragP != padding_fragP;
12204 next_fragP = next_fragP->fr_next)
12205 if (next_fragP->fr_type == rs_machine_dependent
12206 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12207 == BRANCH_PREFIX))
12208 {
12209 if (left_size)
12210 {
12211 int max = next_fragP->tc_frag_data.max_bytes;
12212 if (max)
12213 {
12214 int size;
12215 if (max > left_size)
12216 size = left_size;
12217 else
12218 size = max;
12219 left_size -= size;
12220 next_fragP->tc_frag_data.last_length = size;
12221 }
12222 }
12223 else
12224 next_fragP->tc_frag_data.last_length = 0;
12225 }
12226
12227 /* Check the padding size for the padding frag. */
12228 padding_size = i386_branch_padding_size
12229 (padding_fragP, (fragP->fr_address
12230 + fragP->tc_frag_data.padding_address));
12231
12232 last_size = fragP->tc_frag_data.prefix_length;
12233 /* Check if there is change from the last interation. */
12234 if (padding_size == last_size)
12235 {
12236 /* Update the expected address of the padding frag. */
12237 padding_fragP->tc_frag_data.padding_address
12238 = (fragP->fr_address + padding_size
12239 + fragP->tc_frag_data.padding_address);
12240 return 0;
12241 }
12242
12243 if (padding_size > fragP->tc_frag_data.max_prefix_length)
12244 {
12245 /* No padding if there is no sufficient room. Clear the
12246 expected address of the padding frag. */
12247 padding_fragP->tc_frag_data.padding_address = 0;
12248 padding_size = 0;
12249 }
12250 else
12251 /* Store the expected address of the padding frag. */
12252 padding_fragP->tc_frag_data.padding_address
12253 = (fragP->fr_address + padding_size
12254 + fragP->tc_frag_data.padding_address);
12255
12256 fragP->tc_frag_data.prefix_length = padding_size;
12257
12258 /* Update the length for the current interation. */
12259 left_size = padding_size;
12260 for (next_fragP = fragP;
12261 next_fragP != padding_fragP;
12262 next_fragP = next_fragP->fr_next)
12263 if (next_fragP->fr_type == rs_machine_dependent
12264 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12265 == BRANCH_PREFIX))
12266 {
12267 if (left_size)
12268 {
12269 int max = next_fragP->tc_frag_data.max_bytes;
12270 if (max)
12271 {
12272 int size;
12273 if (max > left_size)
12274 size = left_size;
12275 else
12276 size = max;
12277 left_size -= size;
12278 next_fragP->tc_frag_data.length = size;
12279 }
12280 }
12281 else
12282 next_fragP->tc_frag_data.length = 0;
12283 }
12284
12285 return (fragP->tc_frag_data.length
12286 - fragP->tc_frag_data.last_length);
12287 }
12288 return relax_frag (segment, fragP, stretch);
12289}
12290
ee7fcc42
AM
12291/* md_estimate_size_before_relax()
12292
12293 Called just before relax() for rs_machine_dependent frags. The x86
12294 assembler uses these frags to handle variable size jump
12295 instructions.
12296
12297 Any symbol that is now undefined will not become defined.
12298 Return the correct fr_subtype in the frag.
12299 Return the initial "guess for variable size of frag" to caller.
12300 The guess is actually the growth beyond the fixed part. Whatever
12301 we do to grow the fixed or variable part contributes to our
12302 returned value. */
12303
252b5132 12304int
7016a5d5 12305md_estimate_size_before_relax (fragS *fragP, segT segment)
252b5132 12306{
e379e5f3
L
12307 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12308 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX
12309 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
12310 {
12311 i386_classify_machine_dependent_frag (fragP);
12312 return fragP->tc_frag_data.length;
12313 }
12314
252b5132 12315 /* We've already got fragP->fr_subtype right; all we have to do is
b98ef147
AM
12316 check for un-relaxable symbols. On an ELF system, we can't relax
12317 an externally visible symbol, because it may be overridden by a
12318 shared library. */
12319 if (S_GET_SEGMENT (fragP->fr_symbol) != segment
6d249963 12320#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12321 || (IS_ELF
8dcea932
L
12322 && !elf_symbol_resolved_in_segment_p (fragP->fr_symbol,
12323 fragP->fr_var))
fbeb56a4
DK
12324#endif
12325#if defined (OBJ_COFF) && defined (TE_PE)
7ab9ffdd 12326 || (OUTPUT_FLAVOR == bfd_target_coff_flavour
fbeb56a4 12327 && S_IS_WEAK (fragP->fr_symbol))
b98ef147
AM
12328#endif
12329 )
252b5132 12330 {
b98ef147
AM
12331 /* Symbol is undefined in this segment, or we need to keep a
12332 reloc so that weak symbols can be overridden. */
12333 int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
f86103b7 12334 enum bfd_reloc_code_real reloc_type;
ee7fcc42
AM
12335 unsigned char *opcode;
12336 int old_fr_fix;
eb19308f 12337 fixS *fixP = NULL;
f6af82bd 12338
ee7fcc42 12339 if (fragP->fr_var != NO_RELOC)
1e9cc1c2 12340 reloc_type = (enum bfd_reloc_code_real) fragP->fr_var;
b98ef147 12341 else if (size == 2)
f6af82bd 12342 reloc_type = BFD_RELOC_16_PCREL;
bd7ab16b 12343#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1ef3994a
JB
12344 else if (fragP->tc_frag_data.code64 && fragP->fr_offset == 0
12345 && need_plt32_p (fragP->fr_symbol))
bd7ab16b
L
12346 reloc_type = BFD_RELOC_X86_64_PLT32;
12347#endif
f6af82bd
AM
12348 else
12349 reloc_type = BFD_RELOC_32_PCREL;
252b5132 12350
ee7fcc42
AM
12351 old_fr_fix = fragP->fr_fix;
12352 opcode = (unsigned char *) fragP->fr_opcode;
12353
fddf5b5b 12354 switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
252b5132 12355 {
fddf5b5b
AM
12356 case UNCOND_JUMP:
12357 /* Make jmp (0xeb) a (d)word displacement jump. */
47926f60 12358 opcode[0] = 0xe9;
252b5132 12359 fragP->fr_fix += size;
eb19308f
JB
12360 fixP = fix_new (fragP, old_fr_fix, size,
12361 fragP->fr_symbol,
12362 fragP->fr_offset, 1,
12363 reloc_type);
252b5132
RH
12364 break;
12365
fddf5b5b 12366 case COND_JUMP86:
412167cb
AM
12367 if (size == 2
12368 && (!no_cond_jump_promotion || fragP->fr_var != NO_RELOC))
fddf5b5b
AM
12369 {
12370 /* Negate the condition, and branch past an
12371 unconditional jump. */
12372 opcode[0] ^= 1;
12373 opcode[1] = 3;
12374 /* Insert an unconditional jump. */
12375 opcode[2] = 0xe9;
12376 /* We added two extra opcode bytes, and have a two byte
12377 offset. */
12378 fragP->fr_fix += 2 + 2;
062cd5e7
AS
12379 fix_new (fragP, old_fr_fix + 2, 2,
12380 fragP->fr_symbol,
12381 fragP->fr_offset, 1,
12382 reloc_type);
fddf5b5b
AM
12383 break;
12384 }
12385 /* Fall through. */
12386
12387 case COND_JUMP:
412167cb
AM
12388 if (no_cond_jump_promotion && fragP->fr_var == NO_RELOC)
12389 {
12390 fragP->fr_fix += 1;
3e02c1cc
AM
12391 fixP = fix_new (fragP, old_fr_fix, 1,
12392 fragP->fr_symbol,
12393 fragP->fr_offset, 1,
12394 BFD_RELOC_8_PCREL);
12395 fixP->fx_signed = 1;
412167cb
AM
12396 break;
12397 }
93c2a809 12398
24eab124 12399 /* This changes the byte-displacement jump 0x7N
fddf5b5b 12400 to the (d)word-displacement jump 0x0f,0x8N. */
252b5132 12401 opcode[1] = opcode[0] + 0x10;
f6af82bd 12402 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
47926f60
KH
12403 /* We've added an opcode byte. */
12404 fragP->fr_fix += 1 + size;
eb19308f
JB
12405 fixP = fix_new (fragP, old_fr_fix + 1, size,
12406 fragP->fr_symbol,
12407 fragP->fr_offset, 1,
12408 reloc_type);
252b5132 12409 break;
fddf5b5b
AM
12410
12411 default:
12412 BAD_CASE (fragP->fr_subtype);
12413 break;
252b5132 12414 }
eb19308f
JB
12415
12416 /* All jumps handled here are signed, but don't unconditionally use a
12417 signed limit check for 32 and 16 bit jumps as we want to allow wrap
12418 around at 4G (outside of 64-bit mode) and 64k. */
12419 if (size == 4 && flag_code == CODE_64BIT)
12420 fixP->fx_signed = 1;
12421
252b5132 12422 frag_wane (fragP);
ee7fcc42 12423 return fragP->fr_fix - old_fr_fix;
252b5132 12424 }
93c2a809 12425
93c2a809
AM
12426 /* Guess size depending on current relax state. Initially the relax
12427 state will correspond to a short jump and we return 1, because
12428 the variable part of the frag (the branch offset) is one byte
12429 long. However, we can relax a section more than once and in that
12430 case we must either set fr_subtype back to the unrelaxed state,
12431 or return the value for the appropriate branch. */
12432 return md_relax_table[fragP->fr_subtype].rlx_length;
ee7fcc42
AM
12433}
12434
47926f60
KH
12435/* Called after relax() is finished.
12436
12437 In: Address of frag.
12438 fr_type == rs_machine_dependent.
12439 fr_subtype is what the address relaxed to.
12440
12441 Out: Any fixSs and constants are set up.
12442 Caller will turn frag into a ".space 0". */
12443
252b5132 12444void
7016a5d5
TG
12445md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec ATTRIBUTE_UNUSED,
12446 fragS *fragP)
252b5132 12447{
29b0f896 12448 unsigned char *opcode;
252b5132 12449 unsigned char *where_to_put_displacement = NULL;
847f7ad4
AM
12450 offsetT target_address;
12451 offsetT opcode_address;
252b5132 12452 unsigned int extension = 0;
847f7ad4 12453 offsetT displacement_from_opcode_start;
252b5132 12454
e379e5f3
L
12455 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12456 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING
12457 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12458 {
12459 /* Generate nop padding. */
12460 unsigned int size = fragP->tc_frag_data.length;
12461 if (size)
12462 {
12463 if (size > fragP->tc_frag_data.max_bytes)
12464 abort ();
12465
12466 if (flag_debug)
12467 {
12468 const char *msg;
12469 const char *branch = "branch";
12470 const char *prefix = "";
12471 fragS *padding_fragP;
12472 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
12473 == BRANCH_PREFIX)
12474 {
12475 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
12476 switch (fragP->tc_frag_data.default_prefix)
12477 {
12478 default:
12479 abort ();
12480 break;
12481 case CS_PREFIX_OPCODE:
12482 prefix = " cs";
12483 break;
12484 case DS_PREFIX_OPCODE:
12485 prefix = " ds";
12486 break;
12487 case ES_PREFIX_OPCODE:
12488 prefix = " es";
12489 break;
12490 case FS_PREFIX_OPCODE:
12491 prefix = " fs";
12492 break;
12493 case GS_PREFIX_OPCODE:
12494 prefix = " gs";
12495 break;
12496 case SS_PREFIX_OPCODE:
12497 prefix = " ss";
12498 break;
12499 }
12500 if (padding_fragP)
12501 msg = _("%s:%u: add %d%s at 0x%llx to align "
12502 "%s within %d-byte boundary\n");
12503 else
12504 msg = _("%s:%u: add additional %d%s at 0x%llx to "
12505 "align %s within %d-byte boundary\n");
12506 }
12507 else
12508 {
12509 padding_fragP = fragP;
12510 msg = _("%s:%u: add %d%s-byte nop at 0x%llx to align "
12511 "%s within %d-byte boundary\n");
12512 }
12513
12514 if (padding_fragP)
12515 switch (padding_fragP->tc_frag_data.branch_type)
12516 {
12517 case align_branch_jcc:
12518 branch = "jcc";
12519 break;
12520 case align_branch_fused:
12521 branch = "fused jcc";
12522 break;
12523 case align_branch_jmp:
12524 branch = "jmp";
12525 break;
12526 case align_branch_call:
12527 branch = "call";
12528 break;
12529 case align_branch_indirect:
12530 branch = "indiret branch";
12531 break;
12532 case align_branch_ret:
12533 branch = "ret";
12534 break;
12535 default:
12536 break;
12537 }
12538
12539 fprintf (stdout, msg,
12540 fragP->fr_file, fragP->fr_line, size, prefix,
12541 (long long) fragP->fr_address, branch,
12542 1 << align_branch_power);
12543 }
12544 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12545 memset (fragP->fr_opcode,
12546 fragP->tc_frag_data.default_prefix, size);
12547 else
12548 i386_generate_nops (fragP, (char *) fragP->fr_opcode,
12549 size, 0);
12550 fragP->fr_fix += size;
12551 }
12552 return;
12553 }
12554
252b5132
RH
12555 opcode = (unsigned char *) fragP->fr_opcode;
12556
47926f60 12557 /* Address we want to reach in file space. */
252b5132 12558 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
252b5132 12559
47926f60 12560 /* Address opcode resides at in file space. */
252b5132
RH
12561 opcode_address = fragP->fr_address + fragP->fr_fix;
12562
47926f60 12563 /* Displacement from opcode start to fill into instruction. */
252b5132
RH
12564 displacement_from_opcode_start = target_address - opcode_address;
12565
fddf5b5b 12566 if ((fragP->fr_subtype & BIG) == 0)
252b5132 12567 {
47926f60
KH
12568 /* Don't have to change opcode. */
12569 extension = 1; /* 1 opcode + 1 displacement */
252b5132 12570 where_to_put_displacement = &opcode[1];
fddf5b5b
AM
12571 }
12572 else
12573 {
12574 if (no_cond_jump_promotion
12575 && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
4eed87de
AM
12576 as_warn_where (fragP->fr_file, fragP->fr_line,
12577 _("long jump required"));
252b5132 12578
fddf5b5b
AM
12579 switch (fragP->fr_subtype)
12580 {
12581 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
12582 extension = 4; /* 1 opcode + 4 displacement */
12583 opcode[0] = 0xe9;
12584 where_to_put_displacement = &opcode[1];
12585 break;
252b5132 12586
fddf5b5b
AM
12587 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
12588 extension = 2; /* 1 opcode + 2 displacement */
12589 opcode[0] = 0xe9;
12590 where_to_put_displacement = &opcode[1];
12591 break;
252b5132 12592
fddf5b5b
AM
12593 case ENCODE_RELAX_STATE (COND_JUMP, BIG):
12594 case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
12595 extension = 5; /* 2 opcode + 4 displacement */
12596 opcode[1] = opcode[0] + 0x10;
12597 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12598 where_to_put_displacement = &opcode[2];
12599 break;
252b5132 12600
fddf5b5b
AM
12601 case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
12602 extension = 3; /* 2 opcode + 2 displacement */
12603 opcode[1] = opcode[0] + 0x10;
12604 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12605 where_to_put_displacement = &opcode[2];
12606 break;
252b5132 12607
fddf5b5b
AM
12608 case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
12609 extension = 4;
12610 opcode[0] ^= 1;
12611 opcode[1] = 3;
12612 opcode[2] = 0xe9;
12613 where_to_put_displacement = &opcode[3];
12614 break;
12615
12616 default:
12617 BAD_CASE (fragP->fr_subtype);
12618 break;
12619 }
252b5132 12620 }
fddf5b5b 12621
7b81dfbb
AJ
12622 /* If size if less then four we are sure that the operand fits,
12623 but if it's 4, then it could be that the displacement is larger
12624 then -/+ 2GB. */
12625 if (DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype) == 4
12626 && object_64bit
12627 && ((addressT) (displacement_from_opcode_start - extension
4eed87de
AM
12628 + ((addressT) 1 << 31))
12629 > (((addressT) 2 << 31) - 1)))
7b81dfbb
AJ
12630 {
12631 as_bad_where (fragP->fr_file, fragP->fr_line,
12632 _("jump target out of range"));
12633 /* Make us emit 0. */
12634 displacement_from_opcode_start = extension;
12635 }
47926f60 12636 /* Now put displacement after opcode. */
252b5132
RH
12637 md_number_to_chars ((char *) where_to_put_displacement,
12638 (valueT) (displacement_from_opcode_start - extension),
fddf5b5b 12639 DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
252b5132
RH
12640 fragP->fr_fix += extension;
12641}
12642\f
7016a5d5 12643/* Apply a fixup (fixP) to segment data, once it has been determined
252b5132
RH
12644 by our caller that we have all the info we need to fix it up.
12645
7016a5d5
TG
12646 Parameter valP is the pointer to the value of the bits.
12647
252b5132
RH
12648 On the 386, immediates, displacements, and data pointers are all in
12649 the same (little-endian) format, so we don't need to care about which
12650 we are handling. */
12651
94f592af 12652void
7016a5d5 12653md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 12654{
94f592af 12655 char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
c6682705 12656 valueT value = *valP;
252b5132 12657
f86103b7 12658#if !defined (TE_Mach)
93382f6d
AM
12659 if (fixP->fx_pcrel)
12660 {
12661 switch (fixP->fx_r_type)
12662 {
5865bb77
ILT
12663 default:
12664 break;
12665
d6ab8113
JB
12666 case BFD_RELOC_64:
12667 fixP->fx_r_type = BFD_RELOC_64_PCREL;
12668 break;
93382f6d 12669 case BFD_RELOC_32:
ae8887b5 12670 case BFD_RELOC_X86_64_32S:
93382f6d
AM
12671 fixP->fx_r_type = BFD_RELOC_32_PCREL;
12672 break;
12673 case BFD_RELOC_16:
12674 fixP->fx_r_type = BFD_RELOC_16_PCREL;
12675 break;
12676 case BFD_RELOC_8:
12677 fixP->fx_r_type = BFD_RELOC_8_PCREL;
12678 break;
12679 }
12680 }
252b5132 12681
a161fe53 12682 if (fixP->fx_addsy != NULL
31312f95 12683 && (fixP->fx_r_type == BFD_RELOC_32_PCREL
d6ab8113 12684 || fixP->fx_r_type == BFD_RELOC_64_PCREL
31312f95 12685 || fixP->fx_r_type == BFD_RELOC_16_PCREL
d258b828 12686 || fixP->fx_r_type == BFD_RELOC_8_PCREL)
31312f95 12687 && !use_rela_relocations)
252b5132 12688 {
31312f95
AM
12689 /* This is a hack. There should be a better way to handle this.
12690 This covers for the fact that bfd_install_relocation will
12691 subtract the current location (for partial_inplace, PC relative
12692 relocations); see more below. */
252b5132 12693#ifndef OBJ_AOUT
718ddfc0 12694 if (IS_ELF
252b5132
RH
12695#ifdef TE_PE
12696 || OUTPUT_FLAVOR == bfd_target_coff_flavour
12697#endif
12698 )
12699 value += fixP->fx_where + fixP->fx_frag->fr_address;
12700#endif
12701#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12702 if (IS_ELF)
252b5132 12703 {
6539b54b 12704 segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
2f66722d 12705
6539b54b 12706 if ((sym_seg == seg
2f66722d 12707 || (symbol_section_p (fixP->fx_addsy)
6539b54b 12708 && sym_seg != absolute_section))
af65af87 12709 && !generic_force_reloc (fixP))
2f66722d
AM
12710 {
12711 /* Yes, we add the values in twice. This is because
6539b54b
AM
12712 bfd_install_relocation subtracts them out again. I think
12713 bfd_install_relocation is broken, but I don't dare change
2f66722d
AM
12714 it. FIXME. */
12715 value += fixP->fx_where + fixP->fx_frag->fr_address;
12716 }
252b5132
RH
12717 }
12718#endif
12719#if defined (OBJ_COFF) && defined (TE_PE)
977cdf5a
NC
12720 /* For some reason, the PE format does not store a
12721 section address offset for a PC relative symbol. */
12722 if (S_GET_SEGMENT (fixP->fx_addsy) != seg
7be1c489 12723 || S_IS_WEAK (fixP->fx_addsy))
252b5132
RH
12724 value += md_pcrel_from (fixP);
12725#endif
12726 }
fbeb56a4 12727#if defined (OBJ_COFF) && defined (TE_PE)
f01c1a09
NC
12728 if (fixP->fx_addsy != NULL
12729 && S_IS_WEAK (fixP->fx_addsy)
12730 /* PR 16858: Do not modify weak function references. */
12731 && ! fixP->fx_pcrel)
fbeb56a4 12732 {
296a8689
NC
12733#if !defined (TE_PEP)
12734 /* For x86 PE weak function symbols are neither PC-relative
12735 nor do they set S_IS_FUNCTION. So the only reliable way
12736 to detect them is to check the flags of their containing
12737 section. */
12738 if (S_GET_SEGMENT (fixP->fx_addsy) != NULL
12739 && S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_CODE)
12740 ;
12741 else
12742#endif
fbeb56a4
DK
12743 value -= S_GET_VALUE (fixP->fx_addsy);
12744 }
12745#endif
252b5132
RH
12746
12747 /* Fix a few things - the dynamic linker expects certain values here,
0234cb7c 12748 and we must not disappoint it. */
252b5132 12749#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12750 if (IS_ELF && fixP->fx_addsy)
47926f60
KH
12751 switch (fixP->fx_r_type)
12752 {
12753 case BFD_RELOC_386_PLT32:
3e73aa7c 12754 case BFD_RELOC_X86_64_PLT32:
b9519cfe
L
12755 /* Make the jump instruction point to the address of the operand.
12756 At runtime we merely add the offset to the actual PLT entry.
12757 NB: Subtract the offset size only for jump instructions. */
12758 if (fixP->fx_pcrel)
12759 value = -4;
47926f60 12760 break;
31312f95 12761
13ae64f3
JJ
12762 case BFD_RELOC_386_TLS_GD:
12763 case BFD_RELOC_386_TLS_LDM:
13ae64f3 12764 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
12765 case BFD_RELOC_386_TLS_IE:
12766 case BFD_RELOC_386_TLS_GOTIE:
67a4f2b7 12767 case BFD_RELOC_386_TLS_GOTDESC:
bffbf940
JJ
12768 case BFD_RELOC_X86_64_TLSGD:
12769 case BFD_RELOC_X86_64_TLSLD:
12770 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7 12771 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
00f7efb6
JJ
12772 value = 0; /* Fully resolved at runtime. No addend. */
12773 /* Fallthrough */
12774 case BFD_RELOC_386_TLS_LE:
12775 case BFD_RELOC_386_TLS_LDO_32:
12776 case BFD_RELOC_386_TLS_LE_32:
12777 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 12778 case BFD_RELOC_X86_64_DTPOFF64:
00f7efb6 12779 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113 12780 case BFD_RELOC_X86_64_TPOFF64:
00f7efb6
JJ
12781 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12782 break;
12783
67a4f2b7
AO
12784 case BFD_RELOC_386_TLS_DESC_CALL:
12785 case BFD_RELOC_X86_64_TLSDESC_CALL:
12786 value = 0; /* Fully resolved at runtime. No addend. */
12787 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12788 fixP->fx_done = 0;
12789 return;
12790
47926f60
KH
12791 case BFD_RELOC_VTABLE_INHERIT:
12792 case BFD_RELOC_VTABLE_ENTRY:
12793 fixP->fx_done = 0;
94f592af 12794 return;
47926f60
KH
12795
12796 default:
12797 break;
12798 }
12799#endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) */
a442cac5
JB
12800
12801 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
12802 if (!object_64bit)
12803 value = extend_to_32bit_address (value);
12804
c6682705 12805 *valP = value;
f86103b7 12806#endif /* !defined (TE_Mach) */
3e73aa7c 12807
3e73aa7c 12808 /* Are we finished with this relocation now? */
c6682705 12809 if (fixP->fx_addsy == NULL)
b8188555
JB
12810 {
12811 fixP->fx_done = 1;
12812 switch (fixP->fx_r_type)
12813 {
12814 case BFD_RELOC_X86_64_32S:
12815 fixP->fx_signed = 1;
12816 break;
12817
12818 default:
12819 break;
12820 }
12821 }
fbeb56a4
DK
12822#if defined (OBJ_COFF) && defined (TE_PE)
12823 else if (fixP->fx_addsy != NULL && S_IS_WEAK (fixP->fx_addsy))
12824 {
12825 fixP->fx_done = 0;
12826 /* Remember value for tc_gen_reloc. */
12827 fixP->fx_addnumber = value;
12828 /* Clear out the frag for now. */
12829 value = 0;
12830 }
12831#endif
3e73aa7c
JH
12832 else if (use_rela_relocations)
12833 {
46fb6d5a
JB
12834 if (!disallow_64bit_reloc || fixP->fx_r_type == NO_RELOC)
12835 fixP->fx_no_overflow = 1;
062cd5e7
AS
12836 /* Remember value for tc_gen_reloc. */
12837 fixP->fx_addnumber = value;
3e73aa7c
JH
12838 value = 0;
12839 }
f86103b7 12840
94f592af 12841 md_number_to_chars (p, value, fixP->fx_size);
252b5132 12842}
252b5132 12843\f
6d4af3c2 12844const char *
499ac353 12845md_atof (int type, char *litP, int *sizeP)
252b5132 12846{
499ac353
NC
12847 /* This outputs the LITTLENUMs in REVERSE order;
12848 in accord with the bigendian 386. */
5b7c81bd 12849 return ieee_md_atof (type, litP, sizeP, false);
252b5132
RH
12850}
12851\f
2d545b82 12852static char output_invalid_buf[sizeof (unsigned char) * 2 + 6];
252b5132 12853
252b5132 12854static char *
e3bb37b5 12855output_invalid (int c)
252b5132 12856{
3882b010 12857 if (ISPRINT (c))
f9f21a03
L
12858 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
12859 "'%c'", c);
252b5132 12860 else
f9f21a03 12861 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
2d545b82 12862 "(0x%x)", (unsigned char) c);
252b5132
RH
12863 return output_invalid_buf;
12864}
12865
8a6fb3f9
JB
12866/* Verify that @r can be used in the current context. */
12867
5b7c81bd 12868static bool check_register (const reg_entry *r)
8a6fb3f9
JB
12869{
12870 if (allow_pseudo_reg)
5b7c81bd 12871 return true;
8a6fb3f9
JB
12872
12873 if (operand_type_all_zero (&r->reg_type))
5b7c81bd 12874 return false;
8a6fb3f9
JB
12875
12876 if ((r->reg_type.bitfield.dword
12877 || (r->reg_type.bitfield.class == SReg && r->reg_num > 3)
12878 || r->reg_type.bitfield.class == RegCR
22e00a3f 12879 || r->reg_type.bitfield.class == RegDR)
8a6fb3f9 12880 && !cpu_arch_flags.bitfield.cpui386)
5b7c81bd 12881 return false;
8a6fb3f9 12882
22e00a3f
JB
12883 if (r->reg_type.bitfield.class == RegTR
12884 && (flag_code == CODE_64BIT
12885 || !cpu_arch_flags.bitfield.cpui386
12886 || cpu_arch_isa_flags.bitfield.cpui586
12887 || cpu_arch_isa_flags.bitfield.cpui686))
5b7c81bd 12888 return false;
22e00a3f 12889
8a6fb3f9 12890 if (r->reg_type.bitfield.class == RegMMX && !cpu_arch_flags.bitfield.cpummx)
5b7c81bd 12891 return false;
8a6fb3f9
JB
12892
12893 if (!cpu_arch_flags.bitfield.cpuavx512f)
12894 {
12895 if (r->reg_type.bitfield.zmmword
12896 || r->reg_type.bitfield.class == RegMask)
5b7c81bd 12897 return false;
8a6fb3f9
JB
12898
12899 if (!cpu_arch_flags.bitfield.cpuavx)
12900 {
12901 if (r->reg_type.bitfield.ymmword)
5b7c81bd 12902 return false;
8a6fb3f9
JB
12903
12904 if (!cpu_arch_flags.bitfield.cpusse && r->reg_type.bitfield.xmmword)
5b7c81bd 12905 return false;
8a6fb3f9
JB
12906 }
12907 }
12908
260cd341
LC
12909 if (r->reg_type.bitfield.tmmword
12910 && (!cpu_arch_flags.bitfield.cpuamx_tile
12911 || flag_code != CODE_64BIT))
5b7c81bd 12912 return false;
260cd341 12913
8a6fb3f9 12914 if (r->reg_type.bitfield.class == RegBND && !cpu_arch_flags.bitfield.cpumpx)
5b7c81bd 12915 return false;
8a6fb3f9
JB
12916
12917 /* Don't allow fake index register unless allow_index_reg isn't 0. */
12918 if (!allow_index_reg && r->reg_num == RegIZ)
5b7c81bd 12919 return false;
8a6fb3f9
JB
12920
12921 /* Upper 16 vector registers are only available with VREX in 64bit
12922 mode, and require EVEX encoding. */
12923 if (r->reg_flags & RegVRex)
12924 {
12925 if (!cpu_arch_flags.bitfield.cpuavx512f
12926 || flag_code != CODE_64BIT)
5b7c81bd 12927 return false;
8a6fb3f9 12928
da4977e0
JB
12929 if (i.vec_encoding == vex_encoding_default)
12930 i.vec_encoding = vex_encoding_evex;
12931 else if (i.vec_encoding != vex_encoding_evex)
12932 i.vec_encoding = vex_encoding_error;
8a6fb3f9
JB
12933 }
12934
12935 if (((r->reg_flags & (RegRex64 | RegRex)) || r->reg_type.bitfield.qword)
12936 && (!cpu_arch_flags.bitfield.cpulm || r->reg_type.bitfield.class != RegCR)
12937 && flag_code != CODE_64BIT)
5b7c81bd 12938 return false;
8a6fb3f9
JB
12939
12940 if (r->reg_type.bitfield.class == SReg && r->reg_num == RegFlat
12941 && !intel_syntax)
5b7c81bd 12942 return false;
8a6fb3f9 12943
5b7c81bd 12944 return true;
8a6fb3f9
JB
12945}
12946
af6bdddf 12947/* REG_STRING starts *before* REGISTER_PREFIX. */
252b5132
RH
12948
12949static const reg_entry *
4d1bb795 12950parse_real_register (char *reg_string, char **end_op)
252b5132 12951{
af6bdddf
AM
12952 char *s = reg_string;
12953 char *p;
252b5132
RH
12954 char reg_name_given[MAX_REG_NAME_SIZE + 1];
12955 const reg_entry *r;
12956
12957 /* Skip possible REGISTER_PREFIX and possible whitespace. */
12958 if (*s == REGISTER_PREFIX)
12959 ++s;
12960
12961 if (is_space_char (*s))
12962 ++s;
12963
12964 p = reg_name_given;
af6bdddf 12965 while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
252b5132
RH
12966 {
12967 if (p >= reg_name_given + MAX_REG_NAME_SIZE)
af6bdddf
AM
12968 return (const reg_entry *) NULL;
12969 s++;
252b5132
RH
12970 }
12971
6588847e
DN
12972 /* For naked regs, make sure that we are not dealing with an identifier.
12973 This prevents confusing an identifier like `eax_var' with register
12974 `eax'. */
12975 if (allow_naked_reg && identifier_chars[(unsigned char) *s])
12976 return (const reg_entry *) NULL;
12977
af6bdddf 12978 *end_op = s;
252b5132 12979
629310ab 12980 r = (const reg_entry *) str_hash_find (reg_hash, reg_name_given);
252b5132 12981
5f47d35b 12982 /* Handle floating point regs, allowing spaces in the (i) part. */
6288d05f 12983 if (r == reg_st0)
5f47d35b 12984 {
0e0eea78
JB
12985 if (!cpu_arch_flags.bitfield.cpu8087
12986 && !cpu_arch_flags.bitfield.cpu287
af32b722
JB
12987 && !cpu_arch_flags.bitfield.cpu387
12988 && !allow_pseudo_reg)
0e0eea78
JB
12989 return (const reg_entry *) NULL;
12990
5f47d35b
AM
12991 if (is_space_char (*s))
12992 ++s;
12993 if (*s == '(')
12994 {
af6bdddf 12995 ++s;
5f47d35b
AM
12996 if (is_space_char (*s))
12997 ++s;
12998 if (*s >= '0' && *s <= '7')
12999 {
db557034 13000 int fpr = *s - '0';
af6bdddf 13001 ++s;
5f47d35b
AM
13002 if (is_space_char (*s))
13003 ++s;
13004 if (*s == ')')
13005 {
13006 *end_op = s + 1;
6288d05f 13007 know (r[fpr].reg_num == fpr);
db557034 13008 return r + fpr;
5f47d35b 13009 }
5f47d35b 13010 }
47926f60 13011 /* We have "%st(" then garbage. */
5f47d35b
AM
13012 return (const reg_entry *) NULL;
13013 }
13014 }
13015
8a6fb3f9 13016 return r && check_register (r) ? r : NULL;
252b5132 13017}
4d1bb795
JB
13018
13019/* REG_STRING starts *before* REGISTER_PREFIX. */
13020
13021static const reg_entry *
13022parse_register (char *reg_string, char **end_op)
13023{
13024 const reg_entry *r;
13025
13026 if (*reg_string == REGISTER_PREFIX || allow_naked_reg)
13027 r = parse_real_register (reg_string, end_op);
13028 else
13029 r = NULL;
13030 if (!r)
13031 {
13032 char *save = input_line_pointer;
13033 char c;
13034 symbolS *symbolP;
13035
13036 input_line_pointer = reg_string;
d02603dc 13037 c = get_symbol_name (&reg_string);
4d1bb795 13038 symbolP = symbol_find (reg_string);
64d23078
JB
13039 while (symbolP && S_GET_SEGMENT (symbolP) != reg_section)
13040 {
13041 const expressionS *e = symbol_get_value_expression(symbolP);
13042
13043 if (e->X_op != O_symbol || e->X_add_number)
13044 break;
13045 symbolP = e->X_add_symbol;
13046 }
4d1bb795
JB
13047 if (symbolP && S_GET_SEGMENT (symbolP) == reg_section)
13048 {
13049 const expressionS *e = symbol_get_value_expression (symbolP);
13050
3b55a1d0
JB
13051 know (e->X_op == O_register);
13052 know (e->X_add_number >= 0
13053 && (valueT) e->X_add_number < i386_regtab_size);
13054 r = i386_regtab + e->X_add_number;
13055 if (!check_register (r))
8a6fb3f9 13056 {
3b55a1d0
JB
13057 as_bad (_("register '%s%s' cannot be used here"),
13058 register_prefix, r->reg_name);
13059 r = &bad_reg;
8a6fb3f9 13060 }
3b55a1d0 13061 *end_op = input_line_pointer;
4d1bb795
JB
13062 }
13063 *input_line_pointer = c;
13064 input_line_pointer = save;
13065 }
13066 return r;
13067}
13068
13069int
13070i386_parse_name (char *name, expressionS *e, char *nextcharP)
13071{
4faaa10f 13072 const reg_entry *r = NULL;
4d1bb795
JB
13073 char *end = input_line_pointer;
13074
13075 *end = *nextcharP;
4faaa10f
JB
13076 if (*name == REGISTER_PREFIX || allow_naked_reg)
13077 r = parse_real_register (name, &input_line_pointer);
4d1bb795
JB
13078 if (r && end <= input_line_pointer)
13079 {
13080 *nextcharP = *input_line_pointer;
13081 *input_line_pointer = 0;
8a6fb3f9
JB
13082 if (r != &bad_reg)
13083 {
13084 e->X_op = O_register;
13085 e->X_add_number = r - i386_regtab;
13086 }
13087 else
13088 e->X_op = O_illegal;
4d1bb795
JB
13089 return 1;
13090 }
13091 input_line_pointer = end;
13092 *end = 0;
ee86248c 13093 return intel_syntax ? i386_intel_parse_name (name, e) : 0;
4d1bb795
JB
13094}
13095
13096void
13097md_operand (expressionS *e)
13098{
ee86248c
JB
13099 char *end;
13100 const reg_entry *r;
4d1bb795 13101
ee86248c
JB
13102 switch (*input_line_pointer)
13103 {
13104 case REGISTER_PREFIX:
13105 r = parse_real_register (input_line_pointer, &end);
4d1bb795
JB
13106 if (r)
13107 {
13108 e->X_op = O_register;
13109 e->X_add_number = r - i386_regtab;
13110 input_line_pointer = end;
13111 }
ee86248c
JB
13112 break;
13113
13114 case '[':
9c2799c2 13115 gas_assert (intel_syntax);
ee86248c
JB
13116 end = input_line_pointer++;
13117 expression (e);
13118 if (*input_line_pointer == ']')
13119 {
13120 ++input_line_pointer;
13121 e->X_op_symbol = make_expr_symbol (e);
13122 e->X_add_symbol = NULL;
13123 e->X_add_number = 0;
13124 e->X_op = O_index;
13125 }
13126 else
13127 {
13128 e->X_op = O_absent;
13129 input_line_pointer = end;
13130 }
13131 break;
4d1bb795
JB
13132 }
13133}
13134
252b5132 13135\f
4cc782b5 13136#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
b6f8c7c4 13137const char *md_shortopts = "kVQ:sqnO::";
252b5132 13138#else
b6f8c7c4 13139const char *md_shortopts = "qnO::";
252b5132 13140#endif
6e0b89ee 13141
3e73aa7c 13142#define OPTION_32 (OPTION_MD_BASE + 0)
b3b91714
AM
13143#define OPTION_64 (OPTION_MD_BASE + 1)
13144#define OPTION_DIVIDE (OPTION_MD_BASE + 2)
9103f4f4
L
13145#define OPTION_MARCH (OPTION_MD_BASE + 3)
13146#define OPTION_MTUNE (OPTION_MD_BASE + 4)
1efbbeb4
L
13147#define OPTION_MMNEMONIC (OPTION_MD_BASE + 5)
13148#define OPTION_MSYNTAX (OPTION_MD_BASE + 6)
13149#define OPTION_MINDEX_REG (OPTION_MD_BASE + 7)
13150#define OPTION_MNAKED_REG (OPTION_MD_BASE + 8)
bd5dea88 13151#define OPTION_MRELAX_RELOCATIONS (OPTION_MD_BASE + 9)
c0f3af97 13152#define OPTION_MSSE2AVX (OPTION_MD_BASE + 10)
daf50ae7 13153#define OPTION_MSSE_CHECK (OPTION_MD_BASE + 11)
7bab8ab5
JB
13154#define OPTION_MOPERAND_CHECK (OPTION_MD_BASE + 12)
13155#define OPTION_MAVXSCALAR (OPTION_MD_BASE + 13)
13156#define OPTION_X32 (OPTION_MD_BASE + 14)
7e8b059b 13157#define OPTION_MADD_BND_PREFIX (OPTION_MD_BASE + 15)
43234a1e
L
13158#define OPTION_MEVEXLIG (OPTION_MD_BASE + 16)
13159#define OPTION_MEVEXWIG (OPTION_MD_BASE + 17)
167ad85b 13160#define OPTION_MBIG_OBJ (OPTION_MD_BASE + 18)
d1982f93 13161#define OPTION_MOMIT_LOCK_PREFIX (OPTION_MD_BASE + 19)
d3d3c6db 13162#define OPTION_MEVEXRCIG (OPTION_MD_BASE + 20)
8dcea932 13163#define OPTION_MSHARED (OPTION_MD_BASE + 21)
5db04b09
L
13164#define OPTION_MAMD64 (OPTION_MD_BASE + 22)
13165#define OPTION_MINTEL64 (OPTION_MD_BASE + 23)
e4e00185 13166#define OPTION_MFENCE_AS_LOCK_ADD (OPTION_MD_BASE + 24)
b4a3a7b4 13167#define OPTION_X86_USED_NOTE (OPTION_MD_BASE + 25)
03751133 13168#define OPTION_MVEXWIG (OPTION_MD_BASE + 26)
e379e5f3
L
13169#define OPTION_MALIGN_BRANCH_BOUNDARY (OPTION_MD_BASE + 27)
13170#define OPTION_MALIGN_BRANCH_PREFIX_SIZE (OPTION_MD_BASE + 28)
13171#define OPTION_MALIGN_BRANCH (OPTION_MD_BASE + 29)
76cf450b 13172#define OPTION_MBRANCHES_WITH_32B_BOUNDARIES (OPTION_MD_BASE + 30)
ae531041
L
13173#define OPTION_MLFENCE_AFTER_LOAD (OPTION_MD_BASE + 31)
13174#define OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH (OPTION_MD_BASE + 32)
13175#define OPTION_MLFENCE_BEFORE_RET (OPTION_MD_BASE + 33)
c8480b58 13176#define OPTION_MUSE_UNALIGNED_VECTOR_MOVE (OPTION_MD_BASE + 34)
b3b91714 13177
99ad8390
NC
13178struct option md_longopts[] =
13179{
3e73aa7c 13180 {"32", no_argument, NULL, OPTION_32},
321098a5 13181#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 13182 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c 13183 {"64", no_argument, NULL, OPTION_64},
351f65ca
L
13184#endif
13185#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 13186 {"x32", no_argument, NULL, OPTION_X32},
8dcea932 13187 {"mshared", no_argument, NULL, OPTION_MSHARED},
b4a3a7b4 13188 {"mx86-used-note", required_argument, NULL, OPTION_X86_USED_NOTE},
6e0b89ee 13189#endif
b3b91714 13190 {"divide", no_argument, NULL, OPTION_DIVIDE},
9103f4f4
L
13191 {"march", required_argument, NULL, OPTION_MARCH},
13192 {"mtune", required_argument, NULL, OPTION_MTUNE},
1efbbeb4
L
13193 {"mmnemonic", required_argument, NULL, OPTION_MMNEMONIC},
13194 {"msyntax", required_argument, NULL, OPTION_MSYNTAX},
13195 {"mindex-reg", no_argument, NULL, OPTION_MINDEX_REG},
13196 {"mnaked-reg", no_argument, NULL, OPTION_MNAKED_REG},
c0f3af97 13197 {"msse2avx", no_argument, NULL, OPTION_MSSE2AVX},
c8480b58 13198 {"muse-unaligned-vector-move", no_argument, NULL, OPTION_MUSE_UNALIGNED_VECTOR_MOVE},
daf50ae7 13199 {"msse-check", required_argument, NULL, OPTION_MSSE_CHECK},
7bab8ab5 13200 {"moperand-check", required_argument, NULL, OPTION_MOPERAND_CHECK},
539f890d 13201 {"mavxscalar", required_argument, NULL, OPTION_MAVXSCALAR},
03751133 13202 {"mvexwig", required_argument, NULL, OPTION_MVEXWIG},
7e8b059b 13203 {"madd-bnd-prefix", no_argument, NULL, OPTION_MADD_BND_PREFIX},
43234a1e
L
13204 {"mevexlig", required_argument, NULL, OPTION_MEVEXLIG},
13205 {"mevexwig", required_argument, NULL, OPTION_MEVEXWIG},
167ad85b
TG
13206# if defined (TE_PE) || defined (TE_PEP)
13207 {"mbig-obj", no_argument, NULL, OPTION_MBIG_OBJ},
13208#endif
d1982f93 13209 {"momit-lock-prefix", required_argument, NULL, OPTION_MOMIT_LOCK_PREFIX},
e4e00185 13210 {"mfence-as-lock-add", required_argument, NULL, OPTION_MFENCE_AS_LOCK_ADD},
0cb4071e 13211 {"mrelax-relocations", required_argument, NULL, OPTION_MRELAX_RELOCATIONS},
d3d3c6db 13212 {"mevexrcig", required_argument, NULL, OPTION_MEVEXRCIG},
e379e5f3
L
13213 {"malign-branch-boundary", required_argument, NULL, OPTION_MALIGN_BRANCH_BOUNDARY},
13214 {"malign-branch-prefix-size", required_argument, NULL, OPTION_MALIGN_BRANCH_PREFIX_SIZE},
13215 {"malign-branch", required_argument, NULL, OPTION_MALIGN_BRANCH},
76cf450b 13216 {"mbranches-within-32B-boundaries", no_argument, NULL, OPTION_MBRANCHES_WITH_32B_BOUNDARIES},
ae531041
L
13217 {"mlfence-after-load", required_argument, NULL, OPTION_MLFENCE_AFTER_LOAD},
13218 {"mlfence-before-indirect-branch", required_argument, NULL,
13219 OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH},
13220 {"mlfence-before-ret", required_argument, NULL, OPTION_MLFENCE_BEFORE_RET},
5db04b09
L
13221 {"mamd64", no_argument, NULL, OPTION_MAMD64},
13222 {"mintel64", no_argument, NULL, OPTION_MINTEL64},
252b5132
RH
13223 {NULL, no_argument, NULL, 0}
13224};
13225size_t md_longopts_size = sizeof (md_longopts);
13226
13227int
17b9d67d 13228md_parse_option (int c, const char *arg)
252b5132 13229{
91d6fa6a 13230 unsigned int j;
e379e5f3 13231 char *arch, *next, *saved, *type;
9103f4f4 13232
252b5132
RH
13233 switch (c)
13234 {
12b55ccc
L
13235 case 'n':
13236 optimize_align_code = 0;
13237 break;
13238
a38cf1db
AM
13239 case 'q':
13240 quiet_warnings = 1;
252b5132
RH
13241 break;
13242
13243#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
a38cf1db
AM
13244 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
13245 should be emitted or not. FIXME: Not implemented. */
13246 case 'Q':
d4693039
JB
13247 if ((arg[0] != 'y' && arg[0] != 'n') || arg[1])
13248 return 0;
252b5132
RH
13249 break;
13250
13251 /* -V: SVR4 argument to print version ID. */
13252 case 'V':
13253 print_version_id ();
13254 break;
13255
a38cf1db
AM
13256 /* -k: Ignore for FreeBSD compatibility. */
13257 case 'k':
252b5132 13258 break;
4cc782b5
ILT
13259
13260 case 's':
13261 /* -s: On i386 Solaris, this tells the native assembler to use
29b0f896 13262 .stab instead of .stab.excl. We always use .stab anyhow. */
4cc782b5 13263 break;
8dcea932
L
13264
13265 case OPTION_MSHARED:
13266 shared = 1;
13267 break;
b4a3a7b4
L
13268
13269 case OPTION_X86_USED_NOTE:
13270 if (strcasecmp (arg, "yes") == 0)
13271 x86_used_note = 1;
13272 else if (strcasecmp (arg, "no") == 0)
13273 x86_used_note = 0;
13274 else
13275 as_fatal (_("invalid -mx86-used-note= option: `%s'"), arg);
13276 break;
13277
13278
99ad8390 13279#endif
321098a5 13280#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 13281 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c
JH
13282 case OPTION_64:
13283 {
13284 const char **list, **l;
13285
3e73aa7c
JH
13286 list = bfd_target_list ();
13287 for (l = list; *l != NULL; l++)
08dedd66 13288 if (startswith (*l, "elf64-x86-64")
99ad8390
NC
13289 || strcmp (*l, "coff-x86-64") == 0
13290 || strcmp (*l, "pe-x86-64") == 0
d382c579
TG
13291 || strcmp (*l, "pei-x86-64") == 0
13292 || strcmp (*l, "mach-o-x86-64") == 0)
6e0b89ee
AM
13293 {
13294 default_arch = "x86_64";
13295 break;
13296 }
3e73aa7c 13297 if (*l == NULL)
2b5d6a91 13298 as_fatal (_("no compiled in support for x86_64"));
3e73aa7c
JH
13299 free (list);
13300 }
13301 break;
13302#endif
252b5132 13303
351f65ca 13304#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 13305 case OPTION_X32:
351f65ca
L
13306 if (IS_ELF)
13307 {
13308 const char **list, **l;
13309
13310 list = bfd_target_list ();
13311 for (l = list; *l != NULL; l++)
08dedd66 13312 if (startswith (*l, "elf32-x86-64"))
351f65ca
L
13313 {
13314 default_arch = "x86_64:32";
13315 break;
13316 }
13317 if (*l == NULL)
2b5d6a91 13318 as_fatal (_("no compiled in support for 32bit x86_64"));
351f65ca
L
13319 free (list);
13320 }
13321 else
13322 as_fatal (_("32bit x86_64 is only supported for ELF"));
13323 break;
13324#endif
13325
6e0b89ee
AM
13326 case OPTION_32:
13327 default_arch = "i386";
13328 break;
13329
b3b91714
AM
13330 case OPTION_DIVIDE:
13331#ifdef SVR4_COMMENT_CHARS
13332 {
13333 char *n, *t;
13334 const char *s;
13335
add39d23 13336 n = XNEWVEC (char, strlen (i386_comment_chars) + 1);
b3b91714
AM
13337 t = n;
13338 for (s = i386_comment_chars; *s != '\0'; s++)
13339 if (*s != '/')
13340 *t++ = *s;
13341 *t = '\0';
13342 i386_comment_chars = n;
13343 }
13344#endif
13345 break;
13346
9103f4f4 13347 case OPTION_MARCH:
293f5f65
L
13348 saved = xstrdup (arg);
13349 arch = saved;
13350 /* Allow -march=+nosse. */
13351 if (*arch == '+')
13352 arch++;
6305a203 13353 do
9103f4f4 13354 {
6305a203 13355 if (*arch == '.')
2b5d6a91 13356 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
13357 next = strchr (arch, '+');
13358 if (next)
13359 *next++ = '\0';
91d6fa6a 13360 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 13361 {
ae89daec
JB
13362 if (arch == saved && cpu_arch[j].type != PROCESSOR_NONE
13363 && strcmp (arch, cpu_arch[j].name) == 0)
ccc9c027 13364 {
6305a203 13365 /* Processor. */
ae89daec 13366 if (! cpu_arch[j].enable.bitfield.cpui386)
1ded5609
JB
13367 continue;
13368
91d6fa6a 13369 cpu_arch_name = cpu_arch[j].name;
d92c7521 13370 free (cpu_sub_arch_name);
6305a203 13371 cpu_sub_arch_name = NULL;
ae89daec 13372 cpu_arch_flags = cpu_arch[j].enable;
91d6fa6a 13373 cpu_arch_isa = cpu_arch[j].type;
ae89daec 13374 cpu_arch_isa_flags = cpu_arch[j].enable;
6305a203
L
13375 if (!cpu_arch_tune_set)
13376 {
13377 cpu_arch_tune = cpu_arch_isa;
13378 cpu_arch_tune_flags = cpu_arch_isa_flags;
13379 }
13380 break;
13381 }
ae89daec
JB
13382 else if (cpu_arch[j].type == PROCESSOR_NONE
13383 && strcmp (arch, cpu_arch[j].name) == 0
13384 && !cpu_flags_all_zero (&cpu_arch[j].enable))
6305a203 13385 {
33eaf5de 13386 /* ISA extension. */
6305a203 13387 i386_cpu_flags flags;
309d3373 13388
293f5f65 13389 flags = cpu_flags_or (cpu_arch_flags,
ae89daec 13390 cpu_arch[j].enable);
81486035 13391
5b64d091 13392 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
6305a203 13393 {
ae89daec 13394 extend_cpu_sub_arch_name (arch);
6305a203 13395 cpu_arch_flags = flags;
a586129e 13396 cpu_arch_isa_flags = flags;
6305a203 13397 }
0089dace
L
13398 else
13399 cpu_arch_isa_flags
13400 = cpu_flags_or (cpu_arch_isa_flags,
ae89daec 13401 cpu_arch[j].enable);
6305a203 13402 break;
ccc9c027 13403 }
9103f4f4 13404 }
6305a203 13405
ae89daec 13406 if (j >= ARRAY_SIZE (cpu_arch) && startswith (arch, "no"))
293f5f65 13407 {
33eaf5de 13408 /* Disable an ISA extension. */
ae89daec
JB
13409 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13410 if (cpu_arch[j].type == PROCESSOR_NONE
13411 && strcmp (arch + 2, cpu_arch[j].name) == 0)
293f5f65
L
13412 {
13413 i386_cpu_flags flags;
13414
13415 flags = cpu_flags_and_not (cpu_arch_flags,
ae89daec 13416 cpu_arch[j].disable);
293f5f65
L
13417 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
13418 {
8180707f 13419 extend_cpu_sub_arch_name (arch);
293f5f65
L
13420 cpu_arch_flags = flags;
13421 cpu_arch_isa_flags = flags;
13422 }
13423 break;
13424 }
293f5f65
L
13425 }
13426
91d6fa6a 13427 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 13428 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
13429
13430 arch = next;
9103f4f4 13431 }
293f5f65
L
13432 while (next != NULL);
13433 free (saved);
9103f4f4
L
13434 break;
13435
13436 case OPTION_MTUNE:
13437 if (*arg == '.')
2b5d6a91 13438 as_fatal (_("invalid -mtune= option: `%s'"), arg);
91d6fa6a 13439 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 13440 {
ae89daec
JB
13441 if (cpu_arch[j].type != PROCESSOR_NONE
13442 && strcmp (arg, cpu_arch[j].name) == 0)
9103f4f4 13443 {
ccc9c027 13444 cpu_arch_tune_set = 1;
91d6fa6a 13445 cpu_arch_tune = cpu_arch [j].type;
ae89daec 13446 cpu_arch_tune_flags = cpu_arch[j].enable;
9103f4f4
L
13447 break;
13448 }
13449 }
91d6fa6a 13450 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 13451 as_fatal (_("invalid -mtune= option: `%s'"), arg);
9103f4f4
L
13452 break;
13453
1efbbeb4
L
13454 case OPTION_MMNEMONIC:
13455 if (strcasecmp (arg, "att") == 0)
13456 intel_mnemonic = 0;
13457 else if (strcasecmp (arg, "intel") == 0)
13458 intel_mnemonic = 1;
13459 else
2b5d6a91 13460 as_fatal (_("invalid -mmnemonic= option: `%s'"), arg);
1efbbeb4
L
13461 break;
13462
13463 case OPTION_MSYNTAX:
13464 if (strcasecmp (arg, "att") == 0)
13465 intel_syntax = 0;
13466 else if (strcasecmp (arg, "intel") == 0)
13467 intel_syntax = 1;
13468 else
2b5d6a91 13469 as_fatal (_("invalid -msyntax= option: `%s'"), arg);
1efbbeb4
L
13470 break;
13471
13472 case OPTION_MINDEX_REG:
13473 allow_index_reg = 1;
13474 break;
13475
13476 case OPTION_MNAKED_REG:
13477 allow_naked_reg = 1;
13478 break;
13479
c0f3af97
L
13480 case OPTION_MSSE2AVX:
13481 sse2avx = 1;
13482 break;
13483
c8480b58
L
13484 case OPTION_MUSE_UNALIGNED_VECTOR_MOVE:
13485 use_unaligned_vector_move = 1;
13486 break;
13487
daf50ae7
L
13488 case OPTION_MSSE_CHECK:
13489 if (strcasecmp (arg, "error") == 0)
7bab8ab5 13490 sse_check = check_error;
daf50ae7 13491 else if (strcasecmp (arg, "warning") == 0)
7bab8ab5 13492 sse_check = check_warning;
daf50ae7 13493 else if (strcasecmp (arg, "none") == 0)
7bab8ab5 13494 sse_check = check_none;
daf50ae7 13495 else
2b5d6a91 13496 as_fatal (_("invalid -msse-check= option: `%s'"), arg);
daf50ae7
L
13497 break;
13498
7bab8ab5
JB
13499 case OPTION_MOPERAND_CHECK:
13500 if (strcasecmp (arg, "error") == 0)
13501 operand_check = check_error;
13502 else if (strcasecmp (arg, "warning") == 0)
13503 operand_check = check_warning;
13504 else if (strcasecmp (arg, "none") == 0)
13505 operand_check = check_none;
13506 else
13507 as_fatal (_("invalid -moperand-check= option: `%s'"), arg);
13508 break;
13509
539f890d
L
13510 case OPTION_MAVXSCALAR:
13511 if (strcasecmp (arg, "128") == 0)
13512 avxscalar = vex128;
13513 else if (strcasecmp (arg, "256") == 0)
13514 avxscalar = vex256;
13515 else
2b5d6a91 13516 as_fatal (_("invalid -mavxscalar= option: `%s'"), arg);
539f890d
L
13517 break;
13518
03751133
L
13519 case OPTION_MVEXWIG:
13520 if (strcmp (arg, "0") == 0)
40c9c8de 13521 vexwig = vexw0;
03751133 13522 else if (strcmp (arg, "1") == 0)
40c9c8de 13523 vexwig = vexw1;
03751133
L
13524 else
13525 as_fatal (_("invalid -mvexwig= option: `%s'"), arg);
13526 break;
13527
7e8b059b
L
13528 case OPTION_MADD_BND_PREFIX:
13529 add_bnd_prefix = 1;
13530 break;
13531
43234a1e
L
13532 case OPTION_MEVEXLIG:
13533 if (strcmp (arg, "128") == 0)
13534 evexlig = evexl128;
13535 else if (strcmp (arg, "256") == 0)
13536 evexlig = evexl256;
13537 else if (strcmp (arg, "512") == 0)
13538 evexlig = evexl512;
13539 else
13540 as_fatal (_("invalid -mevexlig= option: `%s'"), arg);
13541 break;
13542
d3d3c6db
IT
13543 case OPTION_MEVEXRCIG:
13544 if (strcmp (arg, "rne") == 0)
13545 evexrcig = rne;
13546 else if (strcmp (arg, "rd") == 0)
13547 evexrcig = rd;
13548 else if (strcmp (arg, "ru") == 0)
13549 evexrcig = ru;
13550 else if (strcmp (arg, "rz") == 0)
13551 evexrcig = rz;
13552 else
13553 as_fatal (_("invalid -mevexrcig= option: `%s'"), arg);
13554 break;
13555
43234a1e
L
13556 case OPTION_MEVEXWIG:
13557 if (strcmp (arg, "0") == 0)
13558 evexwig = evexw0;
13559 else if (strcmp (arg, "1") == 0)
13560 evexwig = evexw1;
13561 else
13562 as_fatal (_("invalid -mevexwig= option: `%s'"), arg);
13563 break;
13564
167ad85b
TG
13565# if defined (TE_PE) || defined (TE_PEP)
13566 case OPTION_MBIG_OBJ:
13567 use_big_obj = 1;
13568 break;
13569#endif
13570
d1982f93 13571 case OPTION_MOMIT_LOCK_PREFIX:
d022bddd
IT
13572 if (strcasecmp (arg, "yes") == 0)
13573 omit_lock_prefix = 1;
13574 else if (strcasecmp (arg, "no") == 0)
13575 omit_lock_prefix = 0;
13576 else
13577 as_fatal (_("invalid -momit-lock-prefix= option: `%s'"), arg);
13578 break;
13579
e4e00185
AS
13580 case OPTION_MFENCE_AS_LOCK_ADD:
13581 if (strcasecmp (arg, "yes") == 0)
13582 avoid_fence = 1;
13583 else if (strcasecmp (arg, "no") == 0)
13584 avoid_fence = 0;
13585 else
13586 as_fatal (_("invalid -mfence-as-lock-add= option: `%s'"), arg);
13587 break;
13588
ae531041
L
13589 case OPTION_MLFENCE_AFTER_LOAD:
13590 if (strcasecmp (arg, "yes") == 0)
13591 lfence_after_load = 1;
13592 else if (strcasecmp (arg, "no") == 0)
13593 lfence_after_load = 0;
13594 else
13595 as_fatal (_("invalid -mlfence-after-load= option: `%s'"), arg);
13596 break;
13597
13598 case OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH:
13599 if (strcasecmp (arg, "all") == 0)
a09f656b 13600 {
13601 lfence_before_indirect_branch = lfence_branch_all;
13602 if (lfence_before_ret == lfence_before_ret_none)
13603 lfence_before_ret = lfence_before_ret_shl;
13604 }
ae531041
L
13605 else if (strcasecmp (arg, "memory") == 0)
13606 lfence_before_indirect_branch = lfence_branch_memory;
13607 else if (strcasecmp (arg, "register") == 0)
13608 lfence_before_indirect_branch = lfence_branch_register;
13609 else if (strcasecmp (arg, "none") == 0)
13610 lfence_before_indirect_branch = lfence_branch_none;
13611 else
13612 as_fatal (_("invalid -mlfence-before-indirect-branch= option: `%s'"),
13613 arg);
13614 break;
13615
13616 case OPTION_MLFENCE_BEFORE_RET:
13617 if (strcasecmp (arg, "or") == 0)
13618 lfence_before_ret = lfence_before_ret_or;
13619 else if (strcasecmp (arg, "not") == 0)
13620 lfence_before_ret = lfence_before_ret_not;
a09f656b 13621 else if (strcasecmp (arg, "shl") == 0 || strcasecmp (arg, "yes") == 0)
13622 lfence_before_ret = lfence_before_ret_shl;
ae531041
L
13623 else if (strcasecmp (arg, "none") == 0)
13624 lfence_before_ret = lfence_before_ret_none;
13625 else
13626 as_fatal (_("invalid -mlfence-before-ret= option: `%s'"),
13627 arg);
13628 break;
13629
0cb4071e
L
13630 case OPTION_MRELAX_RELOCATIONS:
13631 if (strcasecmp (arg, "yes") == 0)
13632 generate_relax_relocations = 1;
13633 else if (strcasecmp (arg, "no") == 0)
13634 generate_relax_relocations = 0;
13635 else
13636 as_fatal (_("invalid -mrelax-relocations= option: `%s'"), arg);
13637 break;
13638
e379e5f3
L
13639 case OPTION_MALIGN_BRANCH_BOUNDARY:
13640 {
13641 char *end;
13642 long int align = strtoul (arg, &end, 0);
13643 if (*end == '\0')
13644 {
13645 if (align == 0)
13646 {
13647 align_branch_power = 0;
13648 break;
13649 }
13650 else if (align >= 16)
13651 {
13652 int align_power;
13653 for (align_power = 0;
13654 (align & 1) == 0;
13655 align >>= 1, align_power++)
13656 continue;
13657 /* Limit alignment power to 31. */
13658 if (align == 1 && align_power < 32)
13659 {
13660 align_branch_power = align_power;
13661 break;
13662 }
13663 }
13664 }
13665 as_fatal (_("invalid -malign-branch-boundary= value: %s"), arg);
13666 }
13667 break;
13668
13669 case OPTION_MALIGN_BRANCH_PREFIX_SIZE:
13670 {
13671 char *end;
13672 int align = strtoul (arg, &end, 0);
13673 /* Some processors only support 5 prefixes. */
13674 if (*end == '\0' && align >= 0 && align < 6)
13675 {
13676 align_branch_prefix_size = align;
13677 break;
13678 }
13679 as_fatal (_("invalid -malign-branch-prefix-size= value: %s"),
13680 arg);
13681 }
13682 break;
13683
13684 case OPTION_MALIGN_BRANCH:
13685 align_branch = 0;
13686 saved = xstrdup (arg);
13687 type = saved;
13688 do
13689 {
13690 next = strchr (type, '+');
13691 if (next)
13692 *next++ = '\0';
13693 if (strcasecmp (type, "jcc") == 0)
13694 align_branch |= align_branch_jcc_bit;
13695 else if (strcasecmp (type, "fused") == 0)
13696 align_branch |= align_branch_fused_bit;
13697 else if (strcasecmp (type, "jmp") == 0)
13698 align_branch |= align_branch_jmp_bit;
13699 else if (strcasecmp (type, "call") == 0)
13700 align_branch |= align_branch_call_bit;
13701 else if (strcasecmp (type, "ret") == 0)
13702 align_branch |= align_branch_ret_bit;
13703 else if (strcasecmp (type, "indirect") == 0)
13704 align_branch |= align_branch_indirect_bit;
13705 else
13706 as_fatal (_("invalid -malign-branch= option: `%s'"), arg);
13707 type = next;
13708 }
13709 while (next != NULL);
13710 free (saved);
13711 break;
13712
76cf450b
L
13713 case OPTION_MBRANCHES_WITH_32B_BOUNDARIES:
13714 align_branch_power = 5;
13715 align_branch_prefix_size = 5;
13716 align_branch = (align_branch_jcc_bit
13717 | align_branch_fused_bit
13718 | align_branch_jmp_bit);
13719 break;
13720
5db04b09 13721 case OPTION_MAMD64:
4b5aaf5f 13722 isa64 = amd64;
5db04b09
L
13723 break;
13724
13725 case OPTION_MINTEL64:
4b5aaf5f 13726 isa64 = intel64;
5db04b09
L
13727 break;
13728
b6f8c7c4
L
13729 case 'O':
13730 if (arg == NULL)
13731 {
13732 optimize = 1;
13733 /* Turn off -Os. */
13734 optimize_for_space = 0;
13735 }
13736 else if (*arg == 's')
13737 {
13738 optimize_for_space = 1;
13739 /* Turn on all encoding optimizations. */
41fd2579 13740 optimize = INT_MAX;
b6f8c7c4
L
13741 }
13742 else
13743 {
13744 optimize = atoi (arg);
13745 /* Turn off -Os. */
13746 optimize_for_space = 0;
13747 }
13748 break;
13749
252b5132
RH
13750 default:
13751 return 0;
13752 }
13753 return 1;
13754}
13755
8a2c8fef
L
13756#define MESSAGE_TEMPLATE \
13757" "
13758
293f5f65
L
13759static char *
13760output_message (FILE *stream, char *p, char *message, char *start,
13761 int *left_p, const char *name, int len)
13762{
13763 int size = sizeof (MESSAGE_TEMPLATE);
13764 int left = *left_p;
13765
13766 /* Reserve 2 spaces for ", " or ",\0" */
13767 left -= len + 2;
13768
13769 /* Check if there is any room. */
13770 if (left >= 0)
13771 {
13772 if (p != start)
13773 {
13774 *p++ = ',';
13775 *p++ = ' ';
13776 }
13777 p = mempcpy (p, name, len);
13778 }
13779 else
13780 {
13781 /* Output the current message now and start a new one. */
13782 *p++ = ',';
13783 *p = '\0';
13784 fprintf (stream, "%s\n", message);
13785 p = start;
13786 left = size - (start - message) - len - 2;
13787
13788 gas_assert (left >= 0);
13789
13790 p = mempcpy (p, name, len);
13791 }
13792
13793 *left_p = left;
13794 return p;
13795}
13796
8a2c8fef 13797static void
1ded5609 13798show_arch (FILE *stream, int ext, int check)
8a2c8fef
L
13799{
13800 static char message[] = MESSAGE_TEMPLATE;
13801 char *start = message + 27;
13802 char *p;
13803 int size = sizeof (MESSAGE_TEMPLATE);
13804 int left;
13805 const char *name;
13806 int len;
13807 unsigned int j;
13808
13809 p = start;
13810 left = size - (start - message);
3ce2ebcf
JB
13811
13812 if (!ext && check)
13813 {
13814 p = output_message (stream, p, message, start, &left,
13815 STRING_COMMA_LEN ("default"));
f68697e8
JB
13816 p = output_message (stream, p, message, start, &left,
13817 STRING_COMMA_LEN ("push"));
13818 p = output_message (stream, p, message, start, &left,
13819 STRING_COMMA_LEN ("pop"));
3ce2ebcf
JB
13820 }
13821
8a2c8fef
L
13822 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13823 {
13824 /* Should it be skipped? */
13825 if (cpu_arch [j].skip)
13826 continue;
13827
13828 name = cpu_arch [j].name;
13829 len = cpu_arch [j].len;
ae89daec 13830 if (cpu_arch[j].type == PROCESSOR_NONE)
8a2c8fef
L
13831 {
13832 /* It is an extension. Skip if we aren't asked to show it. */
ae89daec 13833 if (!ext || cpu_flags_all_zero (&cpu_arch[j].enable))
8a2c8fef
L
13834 continue;
13835 }
13836 else if (ext)
13837 {
13838 /* It is an processor. Skip if we show only extension. */
13839 continue;
13840 }
ae89daec 13841 else if (check && ! cpu_arch[j].enable.bitfield.cpui386)
1ded5609
JB
13842 {
13843 /* It is an impossible processor - skip. */
13844 continue;
13845 }
8a2c8fef 13846
293f5f65 13847 p = output_message (stream, p, message, start, &left, name, len);
8a2c8fef
L
13848 }
13849
293f5f65
L
13850 /* Display disabled extensions. */
13851 if (ext)
ae89daec 13852 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
293f5f65 13853 {
ae89daec
JB
13854 char *str;
13855
13856 if (cpu_arch[j].type != PROCESSOR_NONE
13857 || !cpu_flags_all_zero (&cpu_arch[j].enable))
13858 continue;
13859 str = xasprintf ("no%s", cpu_arch[j].name);
13860 p = output_message (stream, p, message, start, &left, str,
13861 strlen (str));
13862 free (str);
293f5f65
L
13863 }
13864
8a2c8fef
L
13865 *p = '\0';
13866 fprintf (stream, "%s\n", message);
13867}
13868
252b5132 13869void
8a2c8fef 13870md_show_usage (FILE *stream)
252b5132 13871{
4cc782b5
ILT
13872#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13873 fprintf (stream, _("\
d4693039 13874 -Qy, -Qn ignored\n\
a38cf1db 13875 -V print assembler version number\n\
b3b91714
AM
13876 -k ignored\n"));
13877#endif
13878 fprintf (stream, _("\
7ebd68d1
NC
13879 -n do not optimize code alignment\n\
13880 -O{012s} attempt some code optimizations\n\
b3b91714
AM
13881 -q quieten some warnings\n"));
13882#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13883 fprintf (stream, _("\
a38cf1db 13884 -s ignored\n"));
b3b91714 13885#endif
b00af7c8
JB
13886#ifdef BFD64
13887# if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13888 fprintf (stream, _("\
13889 --32/--64/--x32 generate 32bit/64bit/x32 object\n"));
13890# elif defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O)
751d281c 13891 fprintf (stream, _("\
b00af7c8
JB
13892 --32/--64 generate 32bit/64bit object\n"));
13893# endif
751d281c 13894#endif
b3b91714
AM
13895#ifdef SVR4_COMMENT_CHARS
13896 fprintf (stream, _("\
13897 --divide do not treat `/' as a comment character\n"));
a38cf1db
AM
13898#else
13899 fprintf (stream, _("\
b3b91714 13900 --divide ignored\n"));
4cc782b5 13901#endif
9103f4f4 13902 fprintf (stream, _("\
6305a203 13903 -march=CPU[,+EXTENSION...]\n\
8a2c8fef 13904 generate code for CPU and EXTENSION, CPU is one of:\n"));
1ded5609 13905 show_arch (stream, 0, 1);
8a2c8fef 13906 fprintf (stream, _("\
ae89daec 13907 EXTENSION is combination of (possibly \"no\"-prefixed):\n"));
1ded5609 13908 show_arch (stream, 1, 0);
6305a203 13909 fprintf (stream, _("\
8a2c8fef 13910 -mtune=CPU optimize for CPU, CPU is one of:\n"));
1ded5609 13911 show_arch (stream, 0, 0);
ba104c83 13912 fprintf (stream, _("\
c0f3af97
L
13913 -msse2avx encode SSE instructions with VEX prefix\n"));
13914 fprintf (stream, _("\
c8480b58
L
13915 -muse-unaligned-vector-move\n\
13916 encode aligned vector move as unaligned vector move\n"));
13917 fprintf (stream, _("\
7c5c05ef 13918 -msse-check=[none|error|warning] (default: warning)\n\
daf50ae7
L
13919 check SSE instructions\n"));
13920 fprintf (stream, _("\
7c5c05ef 13921 -moperand-check=[none|error|warning] (default: warning)\n\
7bab8ab5
JB
13922 check operand combinations for validity\n"));
13923 fprintf (stream, _("\
7c5c05ef
L
13924 -mavxscalar=[128|256] (default: 128)\n\
13925 encode scalar AVX instructions with specific vector\n\
539f890d
L
13926 length\n"));
13927 fprintf (stream, _("\
03751133
L
13928 -mvexwig=[0|1] (default: 0)\n\
13929 encode VEX instructions with specific VEX.W value\n\
13930 for VEX.W bit ignored instructions\n"));
13931 fprintf (stream, _("\
7c5c05ef
L
13932 -mevexlig=[128|256|512] (default: 128)\n\
13933 encode scalar EVEX instructions with specific vector\n\
43234a1e
L
13934 length\n"));
13935 fprintf (stream, _("\
7c5c05ef
L
13936 -mevexwig=[0|1] (default: 0)\n\
13937 encode EVEX instructions with specific EVEX.W value\n\
43234a1e
L
13938 for EVEX.W bit ignored instructions\n"));
13939 fprintf (stream, _("\
7c5c05ef 13940 -mevexrcig=[rne|rd|ru|rz] (default: rne)\n\
d3d3c6db
IT
13941 encode EVEX instructions with specific EVEX.RC value\n\
13942 for SAE-only ignored instructions\n"));
13943 fprintf (stream, _("\
7c5c05ef
L
13944 -mmnemonic=[att|intel] "));
13945 if (SYSV386_COMPAT)
13946 fprintf (stream, _("(default: att)\n"));
13947 else
13948 fprintf (stream, _("(default: intel)\n"));
13949 fprintf (stream, _("\
13950 use AT&T/Intel mnemonic\n"));
ba104c83 13951 fprintf (stream, _("\
7c5c05ef
L
13952 -msyntax=[att|intel] (default: att)\n\
13953 use AT&T/Intel syntax\n"));
ba104c83
L
13954 fprintf (stream, _("\
13955 -mindex-reg support pseudo index registers\n"));
13956 fprintf (stream, _("\
13957 -mnaked-reg don't require `%%' prefix for registers\n"));
13958 fprintf (stream, _("\
7e8b059b 13959 -madd-bnd-prefix add BND prefix for all valid branches\n"));
b4a3a7b4 13960#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8dcea932
L
13961 fprintf (stream, _("\
13962 -mshared disable branch optimization for shared code\n"));
b4a3a7b4
L
13963 fprintf (stream, _("\
13964 -mx86-used-note=[no|yes] "));
13965 if (DEFAULT_X86_USED_NOTE)
13966 fprintf (stream, _("(default: yes)\n"));
13967 else
13968 fprintf (stream, _("(default: no)\n"));
13969 fprintf (stream, _("\
13970 generate x86 used ISA and feature properties\n"));
13971#endif
13972#if defined (TE_PE) || defined (TE_PEP)
167ad85b
TG
13973 fprintf (stream, _("\
13974 -mbig-obj generate big object files\n"));
13975#endif
d022bddd 13976 fprintf (stream, _("\
7c5c05ef 13977 -momit-lock-prefix=[no|yes] (default: no)\n\
d022bddd 13978 strip all lock prefixes\n"));
5db04b09 13979 fprintf (stream, _("\
7c5c05ef 13980 -mfence-as-lock-add=[no|yes] (default: no)\n\
e4e00185
AS
13981 encode lfence, mfence and sfence as\n\
13982 lock addl $0x0, (%%{re}sp)\n"));
13983 fprintf (stream, _("\
7c5c05ef
L
13984 -mrelax-relocations=[no|yes] "));
13985 if (DEFAULT_GENERATE_X86_RELAX_RELOCATIONS)
13986 fprintf (stream, _("(default: yes)\n"));
13987 else
13988 fprintf (stream, _("(default: no)\n"));
13989 fprintf (stream, _("\
0cb4071e
L
13990 generate relax relocations\n"));
13991 fprintf (stream, _("\
e379e5f3
L
13992 -malign-branch-boundary=NUM (default: 0)\n\
13993 align branches within NUM byte boundary\n"));
13994 fprintf (stream, _("\
13995 -malign-branch=TYPE[+TYPE...] (default: jcc+fused+jmp)\n\
13996 TYPE is combination of jcc, fused, jmp, call, ret,\n\
13997 indirect\n\
13998 specify types of branches to align\n"));
13999 fprintf (stream, _("\
14000 -malign-branch-prefix-size=NUM (default: 5)\n\
14001 align branches with NUM prefixes per instruction\n"));
14002 fprintf (stream, _("\
76cf450b
L
14003 -mbranches-within-32B-boundaries\n\
14004 align branches within 32 byte boundary\n"));
14005 fprintf (stream, _("\
ae531041
L
14006 -mlfence-after-load=[no|yes] (default: no)\n\
14007 generate lfence after load\n"));
14008 fprintf (stream, _("\
14009 -mlfence-before-indirect-branch=[none|all|register|memory] (default: none)\n\
14010 generate lfence before indirect near branch\n"));
14011 fprintf (stream, _("\
a09f656b 14012 -mlfence-before-ret=[none|or|not|shl|yes] (default: none)\n\
ae531041
L
14013 generate lfence before ret\n"));
14014 fprintf (stream, _("\
7c5c05ef 14015 -mamd64 accept only AMD64 ISA [default]\n"));
5db04b09
L
14016 fprintf (stream, _("\
14017 -mintel64 accept only Intel64 ISA\n"));
252b5132
RH
14018}
14019
3e73aa7c 14020#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
321098a5 14021 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
e57f8c65 14022 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
252b5132
RH
14023
14024/* Pick the target format to use. */
14025
47926f60 14026const char *
e3bb37b5 14027i386_target_format (void)
252b5132 14028{
d34049e8 14029 if (startswith (default_arch, "x86_64"))
351f65ca
L
14030 {
14031 update_code_flag (CODE_64BIT, 1);
14032 if (default_arch[6] == '\0')
7f56bc95 14033 x86_elf_abi = X86_64_ABI;
351f65ca 14034 else
7f56bc95 14035 x86_elf_abi = X86_64_X32_ABI;
351f65ca 14036 }
3e73aa7c 14037 else if (!strcmp (default_arch, "i386"))
78f12dd3 14038 update_code_flag (CODE_32BIT, 1);
5197d474
L
14039 else if (!strcmp (default_arch, "iamcu"))
14040 {
14041 update_code_flag (CODE_32BIT, 1);
14042 if (cpu_arch_isa == PROCESSOR_UNKNOWN)
14043 {
14044 static const i386_cpu_flags iamcu_flags = CPU_IAMCU_FLAGS;
14045 cpu_arch_name = "iamcu";
d92c7521 14046 free (cpu_sub_arch_name);
5197d474
L
14047 cpu_sub_arch_name = NULL;
14048 cpu_arch_flags = iamcu_flags;
14049 cpu_arch_isa = PROCESSOR_IAMCU;
14050 cpu_arch_isa_flags = iamcu_flags;
14051 if (!cpu_arch_tune_set)
14052 {
14053 cpu_arch_tune = cpu_arch_isa;
14054 cpu_arch_tune_flags = cpu_arch_isa_flags;
14055 }
14056 }
8d471ec1 14057 else if (cpu_arch_isa != PROCESSOR_IAMCU)
5197d474
L
14058 as_fatal (_("Intel MCU doesn't support `%s' architecture"),
14059 cpu_arch_name);
14060 }
3e73aa7c 14061 else
2b5d6a91 14062 as_fatal (_("unknown architecture"));
89507696
JB
14063
14064 if (cpu_flags_all_zero (&cpu_arch_isa_flags))
ae89daec 14065 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
89507696 14066 if (cpu_flags_all_zero (&cpu_arch_tune_flags))
ae89daec 14067 cpu_arch_tune_flags = cpu_arch[flag_code == CODE_64BIT].enable;
89507696 14068
252b5132
RH
14069 switch (OUTPUT_FLAVOR)
14070 {
9384f2ff 14071#if defined (OBJ_MAYBE_AOUT) || defined (OBJ_AOUT)
4c63da97 14072 case bfd_target_aout_flavour:
47926f60 14073 return AOUT_TARGET_FORMAT;
4c63da97 14074#endif
9384f2ff
AM
14075#if defined (OBJ_MAYBE_COFF) || defined (OBJ_COFF)
14076# if defined (TE_PE) || defined (TE_PEP)
14077 case bfd_target_coff_flavour:
167ad85b 14078 if (flag_code == CODE_64BIT)
eb19308f
JB
14079 {
14080 object_64bit = 1;
14081 return use_big_obj ? "pe-bigobj-x86-64" : "pe-x86-64";
14082 }
14083 return use_big_obj ? "pe-bigobj-i386" : "pe-i386";
9384f2ff 14084# elif defined (TE_GO32)
0561d57c
JK
14085 case bfd_target_coff_flavour:
14086 return "coff-go32";
9384f2ff 14087# else
252b5132
RH
14088 case bfd_target_coff_flavour:
14089 return "coff-i386";
9384f2ff 14090# endif
4c63da97 14091#endif
3e73aa7c 14092#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
252b5132 14093 case bfd_target_elf_flavour:
3e73aa7c 14094 {
351f65ca
L
14095 const char *format;
14096
14097 switch (x86_elf_abi)
4fa24527 14098 {
351f65ca
L
14099 default:
14100 format = ELF_TARGET_FORMAT;
e379e5f3
L
14101#ifndef TE_SOLARIS
14102 tls_get_addr = "___tls_get_addr";
14103#endif
351f65ca 14104 break;
7f56bc95 14105 case X86_64_ABI:
351f65ca 14106 use_rela_relocations = 1;
4fa24527 14107 object_64bit = 1;
e379e5f3
L
14108#ifndef TE_SOLARIS
14109 tls_get_addr = "__tls_get_addr";
14110#endif
351f65ca
L
14111 format = ELF_TARGET_FORMAT64;
14112 break;
7f56bc95 14113 case X86_64_X32_ABI:
4fa24527 14114 use_rela_relocations = 1;
351f65ca 14115 object_64bit = 1;
e379e5f3
L
14116#ifndef TE_SOLARIS
14117 tls_get_addr = "__tls_get_addr";
14118#endif
862be3fb 14119 disallow_64bit_reloc = 1;
351f65ca
L
14120 format = ELF_TARGET_FORMAT32;
14121 break;
4fa24527 14122 }
c085ab00 14123 if (cpu_arch_isa == PROCESSOR_IAMCU)
81486035
L
14124 {
14125 if (x86_elf_abi != I386_ABI)
14126 as_fatal (_("Intel MCU is 32bit only"));
14127 return ELF_TARGET_IAMCU_FORMAT;
14128 }
8a9036a4 14129 else
351f65ca 14130 return format;
3e73aa7c 14131 }
e57f8c65
TG
14132#endif
14133#if defined (OBJ_MACH_O)
14134 case bfd_target_mach_o_flavour:
d382c579
TG
14135 if (flag_code == CODE_64BIT)
14136 {
14137 use_rela_relocations = 1;
14138 object_64bit = 1;
14139 return "mach-o-x86-64";
14140 }
14141 else
14142 return "mach-o-i386";
4c63da97 14143#endif
252b5132
RH
14144 default:
14145 abort ();
14146 return NULL;
14147 }
14148}
14149
47926f60 14150#endif /* OBJ_MAYBE_ more than one */
252b5132 14151\f
252b5132 14152symbolS *
7016a5d5 14153md_undefined_symbol (char *name)
252b5132 14154{
18dc2407
ILT
14155 if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
14156 && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
14157 && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
14158 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
24eab124
AM
14159 {
14160 if (!GOT_symbol)
14161 {
14162 if (symbol_find (name))
14163 as_bad (_("GOT already in symbol table"));
14164 GOT_symbol = symbol_new (name, undefined_section,
e01e1cee 14165 &zero_address_frag, 0);
24eab124
AM
14166 };
14167 return GOT_symbol;
14168 }
252b5132
RH
14169 return 0;
14170}
14171
14172/* Round up a section size to the appropriate boundary. */
47926f60 14173
252b5132 14174valueT
7016a5d5 14175md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
252b5132 14176{
4c63da97
AM
14177#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
14178 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
14179 {
14180 /* For a.out, force the section size to be aligned. If we don't do
14181 this, BFD will align it for us, but it will not write out the
14182 final bytes of the section. This may be a bug in BFD, but it is
14183 easier to fix it here since that is how the other a.out targets
14184 work. */
14185 int align;
14186
fd361982 14187 align = bfd_section_alignment (segment);
8d3842cd 14188 size = ((size + (1 << align) - 1) & (-((valueT) 1 << align)));
4c63da97 14189 }
252b5132
RH
14190#endif
14191
14192 return size;
14193}
14194
14195/* On the i386, PC-relative offsets are relative to the start of the
14196 next instruction. That is, the address of the offset, plus its
14197 size, since the offset is always the last part of the insn. */
14198
14199long
e3bb37b5 14200md_pcrel_from (fixS *fixP)
252b5132
RH
14201{
14202 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
14203}
14204
14205#ifndef I386COFF
14206
14207static void
e3bb37b5 14208s_bss (int ignore ATTRIBUTE_UNUSED)
252b5132 14209{
29b0f896 14210 int temp;
252b5132 14211
8a75718c
JB
14212#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14213 if (IS_ELF)
14214 obj_elf_section_change_hook ();
14215#endif
252b5132
RH
14216 temp = get_absolute_expression ();
14217 subseg_set (bss_section, (subsegT) temp);
14218 demand_empty_rest_of_line ();
14219}
14220
14221#endif
14222
e379e5f3
L
14223/* Remember constant directive. */
14224
14225void
14226i386_cons_align (int ignore ATTRIBUTE_UNUSED)
14227{
14228 if (last_insn.kind != last_insn_directive
14229 && (bfd_section_flags (now_seg) & SEC_CODE))
14230 {
14231 last_insn.seg = now_seg;
14232 last_insn.kind = last_insn_directive;
14233 last_insn.name = "constant directive";
14234 last_insn.file = as_where (&last_insn.line);
ae531041
L
14235 if (lfence_before_ret != lfence_before_ret_none)
14236 {
14237 if (lfence_before_indirect_branch != lfence_branch_none)
14238 as_warn (_("constant directive skips -mlfence-before-ret "
14239 "and -mlfence-before-indirect-branch"));
14240 else
14241 as_warn (_("constant directive skips -mlfence-before-ret"));
14242 }
14243 else if (lfence_before_indirect_branch != lfence_branch_none)
14244 as_warn (_("constant directive skips -mlfence-before-indirect-branch"));
e379e5f3
L
14245 }
14246}
14247
3abbafc2 14248int
e3bb37b5 14249i386_validate_fix (fixS *fixp)
252b5132 14250{
e52a16f2
JB
14251 if (fixp->fx_addsy && S_GET_SEGMENT(fixp->fx_addsy) == reg_section)
14252 {
14253 reloc_howto_type *howto;
14254
14255 howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
14256 as_bad_where (fixp->fx_file, fixp->fx_line,
14257 _("invalid %s relocation against register"),
14258 howto ? howto->name : "<unknown>");
14259 return 0;
14260 }
14261
3abbafc2
JB
14262#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14263 if (fixp->fx_r_type == BFD_RELOC_SIZE32
14264 || fixp->fx_r_type == BFD_RELOC_SIZE64)
14265 return IS_ELF && fixp->fx_addsy
14266 && (!S_IS_DEFINED (fixp->fx_addsy)
14267 || S_IS_EXTERNAL (fixp->fx_addsy));
14268#endif
14269
02a86693 14270 if (fixp->fx_subsy)
252b5132 14271 {
02a86693 14272 if (fixp->fx_subsy == GOT_symbol)
23df1078 14273 {
02a86693
L
14274 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
14275 {
14276 if (!object_64bit)
14277 abort ();
14278#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14279 if (fixp->fx_tcbit2)
56ceb5b5
L
14280 fixp->fx_r_type = (fixp->fx_tcbit
14281 ? BFD_RELOC_X86_64_REX_GOTPCRELX
14282 : BFD_RELOC_X86_64_GOTPCRELX);
02a86693
L
14283 else
14284#endif
14285 fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
14286 }
d6ab8113 14287 else
02a86693
L
14288 {
14289 if (!object_64bit)
14290 fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
14291 else
14292 fixp->fx_r_type = BFD_RELOC_X86_64_GOTOFF64;
14293 }
14294 fixp->fx_subsy = 0;
23df1078 14295 }
252b5132 14296 }
02a86693 14297#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2585b7a5 14298 else
02a86693 14299 {
2585b7a5
L
14300 /* NB: Commit 292676c1 resolved PLT32 reloc aganst local symbol
14301 to section. Since PLT32 relocation must be against symbols,
14302 turn such PLT32 relocation into PC32 relocation. */
14303 if (fixp->fx_addsy
14304 && (fixp->fx_r_type == BFD_RELOC_386_PLT32
14305 || fixp->fx_r_type == BFD_RELOC_X86_64_PLT32)
14306 && symbol_section_p (fixp->fx_addsy))
14307 fixp->fx_r_type = BFD_RELOC_32_PCREL;
14308 if (!object_64bit)
14309 {
14310 if (fixp->fx_r_type == BFD_RELOC_386_GOT32
14311 && fixp->fx_tcbit2)
14312 fixp->fx_r_type = BFD_RELOC_386_GOT32X;
14313 }
02a86693
L
14314 }
14315#endif
3abbafc2
JB
14316
14317 return 1;
252b5132
RH
14318}
14319
252b5132 14320arelent *
7016a5d5 14321tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
14322{
14323 arelent *rel;
14324 bfd_reloc_code_real_type code;
14325
14326 switch (fixp->fx_r_type)
14327 {
8ce3d284 14328#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3abbafc2
JB
14329 symbolS *sym;
14330
8fd4256d
L
14331 case BFD_RELOC_SIZE32:
14332 case BFD_RELOC_SIZE64:
3abbafc2
JB
14333 if (fixp->fx_addsy
14334 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))
14335 && (!fixp->fx_subsy
14336 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))))
14337 sym = fixp->fx_addsy;
14338 else if (fixp->fx_subsy
14339 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))
14340 && (!fixp->fx_addsy
14341 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))))
14342 sym = fixp->fx_subsy;
14343 else
14344 sym = NULL;
14345 if (IS_ELF && sym && S_IS_DEFINED (sym) && !S_IS_EXTERNAL (sym))
8fd4256d
L
14346 {
14347 /* Resolve size relocation against local symbol to size of
14348 the symbol plus addend. */
3abbafc2 14349 valueT value = S_GET_SIZE (sym);
44f87162 14350
3abbafc2
JB
14351 if (symbol_get_bfdsym (sym)->flags & BSF_SECTION_SYM)
14352 value = bfd_section_size (S_GET_SEGMENT (sym));
14353 if (sym == fixp->fx_subsy)
14354 {
14355 value = -value;
14356 if (fixp->fx_addsy)
14357 value += S_GET_VALUE (fixp->fx_addsy);
14358 }
14359 else if (fixp->fx_subsy)
14360 value -= S_GET_VALUE (fixp->fx_subsy);
44f87162 14361 value += fixp->fx_offset;
8fd4256d 14362 if (fixp->fx_r_type == BFD_RELOC_SIZE32
d965814f 14363 && object_64bit
8fd4256d
L
14364 && !fits_in_unsigned_long (value))
14365 as_bad_where (fixp->fx_file, fixp->fx_line,
14366 _("symbol size computation overflow"));
14367 fixp->fx_addsy = NULL;
14368 fixp->fx_subsy = NULL;
14369 md_apply_fix (fixp, (valueT *) &value, NULL);
14370 return NULL;
14371 }
3abbafc2
JB
14372 if (!fixp->fx_addsy || fixp->fx_subsy)
14373 {
14374 as_bad_where (fixp->fx_file, fixp->fx_line,
14375 "unsupported expression involving @size");
14376 return NULL;
14377 }
8ce3d284 14378#endif
1a0670f3 14379 /* Fall through. */
8fd4256d 14380
3e73aa7c
JH
14381 case BFD_RELOC_X86_64_PLT32:
14382 case BFD_RELOC_X86_64_GOT32:
14383 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
14384 case BFD_RELOC_X86_64_GOTPCRELX:
14385 case BFD_RELOC_X86_64_REX_GOTPCRELX:
252b5132
RH
14386 case BFD_RELOC_386_PLT32:
14387 case BFD_RELOC_386_GOT32:
02a86693 14388 case BFD_RELOC_386_GOT32X:
252b5132
RH
14389 case BFD_RELOC_386_GOTOFF:
14390 case BFD_RELOC_386_GOTPC:
13ae64f3
JJ
14391 case BFD_RELOC_386_TLS_GD:
14392 case BFD_RELOC_386_TLS_LDM:
14393 case BFD_RELOC_386_TLS_LDO_32:
14394 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
14395 case BFD_RELOC_386_TLS_IE:
14396 case BFD_RELOC_386_TLS_GOTIE:
13ae64f3
JJ
14397 case BFD_RELOC_386_TLS_LE_32:
14398 case BFD_RELOC_386_TLS_LE:
67a4f2b7
AO
14399 case BFD_RELOC_386_TLS_GOTDESC:
14400 case BFD_RELOC_386_TLS_DESC_CALL:
bffbf940
JJ
14401 case BFD_RELOC_X86_64_TLSGD:
14402 case BFD_RELOC_X86_64_TLSLD:
14403 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 14404 case BFD_RELOC_X86_64_DTPOFF64:
bffbf940
JJ
14405 case BFD_RELOC_X86_64_GOTTPOFF:
14406 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113
JB
14407 case BFD_RELOC_X86_64_TPOFF64:
14408 case BFD_RELOC_X86_64_GOTOFF64:
14409 case BFD_RELOC_X86_64_GOTPC32:
7b81dfbb
AJ
14410 case BFD_RELOC_X86_64_GOT64:
14411 case BFD_RELOC_X86_64_GOTPCREL64:
14412 case BFD_RELOC_X86_64_GOTPC64:
14413 case BFD_RELOC_X86_64_GOTPLT64:
14414 case BFD_RELOC_X86_64_PLTOFF64:
67a4f2b7
AO
14415 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14416 case BFD_RELOC_X86_64_TLSDESC_CALL:
252b5132
RH
14417 case BFD_RELOC_RVA:
14418 case BFD_RELOC_VTABLE_ENTRY:
14419 case BFD_RELOC_VTABLE_INHERIT:
6482c264
NC
14420#ifdef TE_PE
14421 case BFD_RELOC_32_SECREL:
145667f8 14422 case BFD_RELOC_16_SECIDX:
6482c264 14423#endif
252b5132
RH
14424 code = fixp->fx_r_type;
14425 break;
dbbaec26
L
14426 case BFD_RELOC_X86_64_32S:
14427 if (!fixp->fx_pcrel)
14428 {
14429 /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32. */
14430 code = fixp->fx_r_type;
14431 break;
14432 }
1a0670f3 14433 /* Fall through. */
252b5132 14434 default:
93382f6d 14435 if (fixp->fx_pcrel)
252b5132 14436 {
93382f6d
AM
14437 switch (fixp->fx_size)
14438 {
14439 default:
b091f402
AM
14440 as_bad_where (fixp->fx_file, fixp->fx_line,
14441 _("can not do %d byte pc-relative relocation"),
14442 fixp->fx_size);
93382f6d
AM
14443 code = BFD_RELOC_32_PCREL;
14444 break;
14445 case 1: code = BFD_RELOC_8_PCREL; break;
14446 case 2: code = BFD_RELOC_16_PCREL; break;
d258b828 14447 case 4: code = BFD_RELOC_32_PCREL; break;
d6ab8113
JB
14448#ifdef BFD64
14449 case 8: code = BFD_RELOC_64_PCREL; break;
14450#endif
93382f6d
AM
14451 }
14452 }
14453 else
14454 {
14455 switch (fixp->fx_size)
14456 {
14457 default:
b091f402
AM
14458 as_bad_where (fixp->fx_file, fixp->fx_line,
14459 _("can not do %d byte relocation"),
14460 fixp->fx_size);
93382f6d
AM
14461 code = BFD_RELOC_32;
14462 break;
14463 case 1: code = BFD_RELOC_8; break;
14464 case 2: code = BFD_RELOC_16; break;
14465 case 4: code = BFD_RELOC_32; break;
937149dd 14466#ifdef BFD64
3e73aa7c 14467 case 8: code = BFD_RELOC_64; break;
937149dd 14468#endif
93382f6d 14469 }
252b5132
RH
14470 }
14471 break;
14472 }
252b5132 14473
d182319b
JB
14474 if ((code == BFD_RELOC_32
14475 || code == BFD_RELOC_32_PCREL
14476 || code == BFD_RELOC_X86_64_32S)
252b5132
RH
14477 && GOT_symbol
14478 && fixp->fx_addsy == GOT_symbol)
3e73aa7c 14479 {
4fa24527 14480 if (!object_64bit)
d6ab8113
JB
14481 code = BFD_RELOC_386_GOTPC;
14482 else
14483 code = BFD_RELOC_X86_64_GOTPC32;
3e73aa7c 14484 }
7b81dfbb
AJ
14485 if ((code == BFD_RELOC_64 || code == BFD_RELOC_64_PCREL)
14486 && GOT_symbol
14487 && fixp->fx_addsy == GOT_symbol)
14488 {
14489 code = BFD_RELOC_X86_64_GOTPC64;
14490 }
252b5132 14491
add39d23
TS
14492 rel = XNEW (arelent);
14493 rel->sym_ptr_ptr = XNEW (asymbol *);
49309057 14494 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
14495
14496 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
c87db184 14497
3e73aa7c
JH
14498 if (!use_rela_relocations)
14499 {
14500 /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
14501 vtable entry to be used in the relocation's section offset. */
14502 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
14503 rel->address = fixp->fx_offset;
fbeb56a4
DK
14504#if defined (OBJ_COFF) && defined (TE_PE)
14505 else if (fixp->fx_addsy && S_IS_WEAK (fixp->fx_addsy))
14506 rel->addend = fixp->fx_addnumber - (S_GET_VALUE (fixp->fx_addsy) * 2);
14507 else
14508#endif
c6682705 14509 rel->addend = 0;
3e73aa7c
JH
14510 }
14511 /* Use the rela in 64bit mode. */
252b5132 14512 else
3e73aa7c 14513 {
862be3fb
L
14514 if (disallow_64bit_reloc)
14515 switch (code)
14516 {
862be3fb
L
14517 case BFD_RELOC_X86_64_DTPOFF64:
14518 case BFD_RELOC_X86_64_TPOFF64:
14519 case BFD_RELOC_64_PCREL:
14520 case BFD_RELOC_X86_64_GOTOFF64:
14521 case BFD_RELOC_X86_64_GOT64:
14522 case BFD_RELOC_X86_64_GOTPCREL64:
14523 case BFD_RELOC_X86_64_GOTPC64:
14524 case BFD_RELOC_X86_64_GOTPLT64:
14525 case BFD_RELOC_X86_64_PLTOFF64:
14526 as_bad_where (fixp->fx_file, fixp->fx_line,
14527 _("cannot represent relocation type %s in x32 mode"),
14528 bfd_get_reloc_code_name (code));
14529 break;
14530 default:
14531 break;
14532 }
14533
062cd5e7
AS
14534 if (!fixp->fx_pcrel)
14535 rel->addend = fixp->fx_offset;
14536 else
14537 switch (code)
14538 {
14539 case BFD_RELOC_X86_64_PLT32:
14540 case BFD_RELOC_X86_64_GOT32:
14541 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
14542 case BFD_RELOC_X86_64_GOTPCRELX:
14543 case BFD_RELOC_X86_64_REX_GOTPCRELX:
bffbf940
JJ
14544 case BFD_RELOC_X86_64_TLSGD:
14545 case BFD_RELOC_X86_64_TLSLD:
14546 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7
AO
14547 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14548 case BFD_RELOC_X86_64_TLSDESC_CALL:
062cd5e7
AS
14549 rel->addend = fixp->fx_offset - fixp->fx_size;
14550 break;
14551 default:
14552 rel->addend = (section->vma
14553 - fixp->fx_size
14554 + fixp->fx_addnumber
14555 + md_pcrel_from (fixp));
14556 break;
14557 }
3e73aa7c
JH
14558 }
14559
252b5132
RH
14560 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
14561 if (rel->howto == NULL)
14562 {
14563 as_bad_where (fixp->fx_file, fixp->fx_line,
d0b47220 14564 _("cannot represent relocation type %s"),
252b5132
RH
14565 bfd_get_reloc_code_name (code));
14566 /* Set howto to a garbage value so that we can keep going. */
14567 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
9c2799c2 14568 gas_assert (rel->howto != NULL);
252b5132
RH
14569 }
14570
14571 return rel;
14572}
14573
ee86248c 14574#include "tc-i386-intel.c"
54cfded0 14575
a60de03c
JB
14576void
14577tc_x86_parse_to_dw2regnum (expressionS *exp)
54cfded0 14578{
a60de03c
JB
14579 int saved_naked_reg;
14580 char saved_register_dot;
54cfded0 14581
a60de03c
JB
14582 saved_naked_reg = allow_naked_reg;
14583 allow_naked_reg = 1;
14584 saved_register_dot = register_chars['.'];
14585 register_chars['.'] = '.';
14586 allow_pseudo_reg = 1;
14587 expression_and_evaluate (exp);
14588 allow_pseudo_reg = 0;
14589 register_chars['.'] = saved_register_dot;
14590 allow_naked_reg = saved_naked_reg;
14591
e96d56a1 14592 if (exp->X_op == O_register && exp->X_add_number >= 0)
54cfded0 14593 {
a60de03c
JB
14594 if ((addressT) exp->X_add_number < i386_regtab_size)
14595 {
14596 exp->X_op = O_constant;
14597 exp->X_add_number = i386_regtab[exp->X_add_number]
14598 .dw2_regnum[flag_code >> 1];
14599 }
14600 else
14601 exp->X_op = O_illegal;
54cfded0 14602 }
54cfded0
AM
14603}
14604
14605void
14606tc_x86_frame_initial_instructions (void)
14607{
a60de03c
JB
14608 static unsigned int sp_regno[2];
14609
14610 if (!sp_regno[flag_code >> 1])
14611 {
14612 char *saved_input = input_line_pointer;
14613 char sp[][4] = {"esp", "rsp"};
14614 expressionS exp;
a4447b93 14615
a60de03c
JB
14616 input_line_pointer = sp[flag_code >> 1];
14617 tc_x86_parse_to_dw2regnum (&exp);
9c2799c2 14618 gas_assert (exp.X_op == O_constant);
a60de03c
JB
14619 sp_regno[flag_code >> 1] = exp.X_add_number;
14620 input_line_pointer = saved_input;
14621 }
a4447b93 14622
61ff971f
L
14623 cfi_add_CFA_def_cfa (sp_regno[flag_code >> 1], -x86_cie_data_alignment);
14624 cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
54cfded0 14625}
d2b2c203 14626
d7921315
L
14627int
14628x86_dwarf2_addr_size (void)
14629{
14630#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
14631 if (x86_elf_abi == X86_64_X32_ABI)
14632 return 4;
14633#endif
14634 return bfd_arch_bits_per_address (stdoutput) / 8;
14635}
14636
d2b2c203
DJ
14637int
14638i386_elf_section_type (const char *str, size_t len)
14639{
14640 if (flag_code == CODE_64BIT
14641 && len == sizeof ("unwind") - 1
d34049e8 14642 && startswith (str, "unwind"))
d2b2c203
DJ
14643 return SHT_X86_64_UNWIND;
14644
14645 return -1;
14646}
bb41ade5 14647
ad5fec3b
EB
14648#ifdef TE_SOLARIS
14649void
14650i386_solaris_fix_up_eh_frame (segT sec)
14651{
14652 if (flag_code == CODE_64BIT)
14653 elf_section_type (sec) = SHT_X86_64_UNWIND;
14654}
14655#endif
14656
bb41ade5
AM
14657#ifdef TE_PE
14658void
14659tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
14660{
91d6fa6a 14661 expressionS exp;
bb41ade5 14662
91d6fa6a
NC
14663 exp.X_op = O_secrel;
14664 exp.X_add_symbol = symbol;
14665 exp.X_add_number = 0;
14666 emit_expr (&exp, size);
bb41ade5
AM
14667}
14668#endif
3b22753a
L
14669
14670#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14671/* For ELF on x86-64, add support for SHF_X86_64_LARGE. */
14672
01e1a5bc 14673bfd_vma
6d4af3c2 14674x86_64_section_letter (int letter, const char **ptr_msg)
3b22753a
L
14675{
14676 if (flag_code == CODE_64BIT)
14677 {
14678 if (letter == 'l')
14679 return SHF_X86_64_LARGE;
14680
8f3bae45 14681 *ptr_msg = _("bad .section directive: want a,l,w,x,M,S,G,T in string");
64e74474 14682 }
3b22753a 14683 else
8f3bae45 14684 *ptr_msg = _("bad .section directive: want a,w,x,M,S,G,T in string");
3b22753a
L
14685 return -1;
14686}
14687
01e1a5bc 14688bfd_vma
3b22753a
L
14689x86_64_section_word (char *str, size_t len)
14690{
08dedd66 14691 if (len == 5 && flag_code == CODE_64BIT && startswith (str, "large"))
3b22753a
L
14692 return SHF_X86_64_LARGE;
14693
14694 return -1;
14695}
14696
14697static void
14698handle_large_common (int small ATTRIBUTE_UNUSED)
14699{
14700 if (flag_code != CODE_64BIT)
14701 {
14702 s_comm_internal (0, elf_common_parse);
14703 as_warn (_(".largecomm supported only in 64bit mode, producing .comm"));
14704 }
14705 else
14706 {
14707 static segT lbss_section;
14708 asection *saved_com_section_ptr = elf_com_section_ptr;
14709 asection *saved_bss_section = bss_section;
14710
14711 if (lbss_section == NULL)
14712 {
14713 flagword applicable;
14714 segT seg = now_seg;
14715 subsegT subseg = now_subseg;
14716
14717 /* The .lbss section is for local .largecomm symbols. */
14718 lbss_section = subseg_new (".lbss", 0);
14719 applicable = bfd_applicable_section_flags (stdoutput);
fd361982 14720 bfd_set_section_flags (lbss_section, applicable & SEC_ALLOC);
3b22753a
L
14721 seg_info (lbss_section)->bss = 1;
14722
14723 subseg_set (seg, subseg);
14724 }
14725
14726 elf_com_section_ptr = &_bfd_elf_large_com_section;
14727 bss_section = lbss_section;
14728
14729 s_comm_internal (0, elf_common_parse);
14730
14731 elf_com_section_ptr = saved_com_section_ptr;
14732 bss_section = saved_bss_section;
14733 }
14734}
14735#endif /* OBJ_ELF || OBJ_MAYBE_ELF */