]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/config/tc-i386.c
Update etc/update-copyright.py
[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
84#define END_OF_INSN '\0'
85
05909f23
JB
86#define OPERAND_TYPE_NONE { .bitfield = { .class = ClassNone } }
87
79dec6b7
JB
88/* This matches the C -> StaticRounding alias in the opcode table. */
89#define commutative staticrounding
90
6305a203
L
91/*
92 'templates' is for grouping together 'template' structures for opcodes
93 of the same name. This is only used for storing the insns in the grand
94 ole hash table of insns.
95 The templates themselves start at START and range up to (but not including)
96 END.
97 */
98typedef struct
99{
d3ce72d0
NC
100 const insn_template *start;
101 const insn_template *end;
6305a203
L
102}
103templates;
104
105/* 386 operand encoding bytes: see 386 book for details of this. */
106typedef struct
107{
108 unsigned int regmem; /* codes register or memory operand */
109 unsigned int reg; /* codes register operand (or extended opcode) */
110 unsigned int mode; /* how to interpret regmem & reg */
111}
112modrm_byte;
113
114/* x86-64 extension prefix. */
115typedef int rex_byte;
116
6305a203
L
117/* 386 opcode byte to code indirect addressing. */
118typedef struct
119{
120 unsigned base;
121 unsigned index;
122 unsigned scale;
123}
124sib_byte;
125
6305a203
L
126/* x86 arch names, types and features */
127typedef struct
128{
129 const char *name; /* arch name */
6ceeed25
JB
130 unsigned int len:8; /* arch string length */
131 bool skip:1; /* show_arch should skip this. */
6305a203 132 enum processor_type type; /* arch type */
ae89daec
JB
133 i386_cpu_flags enable; /* cpu feature enable flags */
134 i386_cpu_flags disable; /* cpu feature disable flags */
6305a203
L
135}
136arch_entry;
137
78f12dd3 138static void update_code_flag (int, int);
e3bb37b5
L
139static void set_code_flag (int);
140static void set_16bit_gcc_code_flag (int);
141static void set_intel_syntax (int);
1efbbeb4 142static void set_intel_mnemonic (int);
db51cc60 143static void set_allow_index_reg (int);
7bab8ab5 144static void set_check (int);
e3bb37b5 145static void set_cpu_arch (int);
6482c264 146#ifdef TE_PE
e3bb37b5 147static void pe_directive_secrel (int);
145667f8 148static void pe_directive_secidx (int);
6482c264 149#endif
e3bb37b5
L
150static void signed_cons (int);
151static char *output_invalid (int c);
ee86248c
JB
152static int i386_finalize_immediate (segT, expressionS *, i386_operand_type,
153 const char *);
154static int i386_finalize_displacement (segT, expressionS *, i386_operand_type,
155 const char *);
a7619375 156static int i386_att_operand (char *);
e3bb37b5 157static int i386_intel_operand (char *, int);
ee86248c
JB
158static int i386_intel_simplify (expressionS *);
159static int i386_intel_parse_name (const char *, expressionS *);
e3bb37b5 160static const reg_entry *parse_register (char *, char **);
5317ad2c 161static const char *parse_insn (const char *, char *);
e3bb37b5
L
162static char *parse_operands (char *, const char *);
163static void swap_operands (void);
783c187b 164static void swap_2_operands (unsigned int, unsigned int);
48bcea9f 165static enum flag_code i386_addressing_mode (void);
e3bb37b5
L
166static void optimize_imm (void);
167static void optimize_disp (void);
83b16ac6 168static const insn_template *match_template (char);
e3bb37b5
L
169static int check_string (void);
170static int process_suffix (void);
171static int check_byte_reg (void);
172static int check_long_reg (void);
173static int check_qword_reg (void);
174static int check_word_reg (void);
175static int finalize_imm (void);
176static int process_operands (void);
5e042380 177static const reg_entry *build_modrm_byte (void);
e3bb37b5
L
178static void output_insn (void);
179static void output_imm (fragS *, offsetT);
180static void output_disp (fragS *, offsetT);
29b0f896 181#ifndef I386COFF
e3bb37b5 182static void s_bss (int);
252b5132 183#endif
17d4e2a2
L
184#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
185static void handle_large_common (int small ATTRIBUTE_UNUSED);
b4a3a7b4
L
186
187/* GNU_PROPERTY_X86_ISA_1_USED. */
188static unsigned int x86_isa_1_used;
189/* GNU_PROPERTY_X86_FEATURE_2_USED. */
190static unsigned int x86_feature_2_used;
191/* Generate x86 used ISA and feature properties. */
192static unsigned int x86_used_note = DEFAULT_X86_USED_NOTE;
17d4e2a2 193#endif
252b5132 194
a847613f 195static const char *default_arch = DEFAULT_ARCH;
3e73aa7c 196
8a6fb3f9
JB
197/* parse_register() returns this when a register alias cannot be used. */
198static const reg_entry bad_reg = { "<bad>", OPERAND_TYPE_NONE, 0, 0,
199 { Dw2Inval, Dw2Inval } };
200
34684862 201static const reg_entry *reg_eax;
5e042380
JB
202static const reg_entry *reg_ds;
203static const reg_entry *reg_es;
204static const reg_entry *reg_ss;
6288d05f 205static const reg_entry *reg_st0;
6225c532
JB
206static const reg_entry *reg_k0;
207
c0f3af97
L
208/* VEX prefix. */
209typedef struct
210{
43234a1e
L
211 /* VEX prefix is either 2 byte or 3 byte. EVEX is 4 byte. */
212 unsigned char bytes[4];
c0f3af97
L
213 unsigned int length;
214 /* Destination or source register specifier. */
215 const reg_entry *register_specifier;
216} vex_prefix;
217
252b5132 218/* 'md_assemble ()' gathers together information and puts it into a
47926f60 219 i386_insn. */
252b5132 220
520dc8e8
AM
221union i386_op
222 {
223 expressionS *disps;
224 expressionS *imms;
225 const reg_entry *regs;
226 };
227
a65babc9
L
228enum i386_error
229 {
b4d65f2d 230 no_error, /* Must be first. */
86e026a4 231 operand_size_mismatch,
a65babc9
L
232 operand_type_mismatch,
233 register_type_mismatch,
234 number_of_operands_mismatch,
235 invalid_instruction_suffix,
236 bad_imm4,
a65babc9
L
237 unsupported_with_intel_mnemonic,
238 unsupported_syntax,
6c30d220 239 unsupported,
9db83a32
JB
240 unsupported_on_arch,
241 unsupported_64bit,
260cd341 242 invalid_sib_address,
6c30d220 243 invalid_vsib_address,
7bab8ab5 244 invalid_vector_register_set,
260cd341 245 invalid_tmm_register_set,
0cc78721 246 invalid_dest_and_src_register_set,
43234a1e
L
247 unsupported_vector_index_register,
248 unsupported_broadcast,
43234a1e
L
249 broadcast_needed,
250 unsupported_masking,
251 mask_not_on_destination,
252 no_default_mask,
253 unsupported_rc_sae,
43234a1e 254 invalid_register_operand,
a65babc9
L
255 };
256
252b5132
RH
257struct _i386_insn
258 {
47926f60 259 /* TM holds the template for the insn were currently assembling. */
d3ce72d0 260 insn_template tm;
252b5132 261
7d5e4556
L
262 /* SUFFIX holds the instruction size suffix for byte, word, dword
263 or qword, if given. */
252b5132
RH
264 char suffix;
265
9a182d04
JB
266 /* OPCODE_LENGTH holds the number of base opcode bytes. */
267 unsigned char opcode_length;
268
47926f60 269 /* OPERANDS gives the number of given operands. */
252b5132
RH
270 unsigned int operands;
271
272 /* REG_OPERANDS, DISP_OPERANDS, MEM_OPERANDS, IMM_OPERANDS give the number
273 of given register, displacement, memory operands and immediate
47926f60 274 operands. */
252b5132
RH
275 unsigned int reg_operands, disp_operands, mem_operands, imm_operands;
276
277 /* TYPES [i] is the type (see above #defines) which tells us how to
520dc8e8 278 use OP[i] for the corresponding operand. */
40fb9820 279 i386_operand_type types[MAX_OPERANDS];
252b5132 280
520dc8e8
AM
281 /* Displacement expression, immediate expression, or register for each
282 operand. */
283 union i386_op op[MAX_OPERANDS];
252b5132 284
3e73aa7c
JH
285 /* Flags for operands. */
286 unsigned int flags[MAX_OPERANDS];
287#define Operand_PCrel 1
c48dadc9 288#define Operand_Mem 2
3e73aa7c 289
252b5132 290 /* Relocation type for operand */
f86103b7 291 enum bfd_reloc_code_real reloc[MAX_OPERANDS];
252b5132 292
252b5132
RH
293 /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
294 the base index byte below. */
295 const reg_entry *base_reg;
296 const reg_entry *index_reg;
297 unsigned int log2_scale_factor;
298
299 /* SEG gives the seg_entries of this insn. They are zero unless
47926f60 300 explicit segment overrides are given. */
5e042380 301 const reg_entry *seg[2];
252b5132
RH
302
303 /* PREFIX holds all the given prefix opcodes (usually null).
304 PREFIXES is the number of prefix opcodes. */
305 unsigned int prefixes;
306 unsigned char prefix[MAX_PREFIXES];
307
50128d0c 308 /* Register is in low 3 bits of opcode. */
5b7c81bd 309 bool short_form;
50128d0c 310
6f2f06be 311 /* The operand to a branch insn indicates an absolute branch. */
5b7c81bd 312 bool jumpabsolute;
6f2f06be 313
a4d3acd2
JB
314 /* The operand to a branch insn indicates a far branch. */
315 bool far_branch;
316
9373f275
L
317 /* There is a memory operand of (%dx) which should be only used
318 with input/output instructions. */
319 bool input_output_operand;
320
921eafea
L
321 /* Extended states. */
322 enum
323 {
324 /* Use MMX state. */
325 xstate_mmx = 1 << 0,
326 /* Use XMM state. */
327 xstate_xmm = 1 << 1,
328 /* Use YMM state. */
329 xstate_ymm = 1 << 2 | xstate_xmm,
330 /* Use ZMM state. */
331 xstate_zmm = 1 << 3 | xstate_ymm,
332 /* Use TMM state. */
32930e4e
L
333 xstate_tmm = 1 << 4,
334 /* Use MASK state. */
335 xstate_mask = 1 << 5
921eafea 336 } xstate;
260cd341 337
e379e5f3 338 /* Has GOTPC or TLS relocation. */
5b7c81bd 339 bool has_gotpc_tls_reloc;
e379e5f3 340
252b5132 341 /* RM and SIB are the modrm byte and the sib byte where the
c1e679ec 342 addressing modes of this insn are encoded. */
252b5132 343 modrm_byte rm;
3e73aa7c 344 rex_byte rex;
43234a1e 345 rex_byte vrex;
252b5132 346 sib_byte sib;
c0f3af97 347 vex_prefix vex;
b6169b20 348
6225c532
JB
349 /* Masking attributes.
350
351 The struct describes masking, applied to OPERAND in the instruction.
352 REG is a pointer to the corresponding mask register. ZEROING tells
353 whether merging or zeroing mask is used. */
354 struct Mask_Operation
355 {
356 const reg_entry *reg;
357 unsigned int zeroing;
358 /* The operand where this operation is associated. */
359 unsigned int operand;
360 } mask;
43234a1e
L
361
362 /* Rounding control and SAE attributes. */
ca5312a2
JB
363 struct RC_Operation
364 {
365 enum rc_type
366 {
367 rc_none = -1,
368 rne,
369 rd,
370 ru,
371 rz,
372 saeonly
373 } type;
7063667e
JB
374 /* In Intel syntax the operand modifier form is supposed to be used, but
375 we continue to accept the immediate forms as well. */
376 bool modifier;
ca5312a2 377 } rounding;
43234a1e 378
5273a3cd
JB
379 /* Broadcasting attributes.
380
381 The struct describes broadcasting, applied to OPERAND. TYPE is
382 expresses the broadcast factor. */
383 struct Broadcast_Operation
384 {
0cc78721 385 /* Type of broadcast: {1to2}, {1to4}, {1to8}, {1to16} or {1to32}. */
5273a3cd
JB
386 unsigned int type;
387
388 /* Index of broadcasted operand. */
389 unsigned int operand;
390
391 /* Number of bytes to broadcast. */
392 unsigned int bytes;
393 } broadcast;
43234a1e
L
394
395 /* Compressed disp8*N attribute. */
396 unsigned int memshift;
397
86fa6981
L
398 /* Prefer load or store in encoding. */
399 enum
400 {
401 dir_encoding_default = 0,
402 dir_encoding_load,
64c49ab3
JB
403 dir_encoding_store,
404 dir_encoding_swap
86fa6981 405 } dir_encoding;
891edac4 406
41eb8e88 407 /* Prefer 8bit, 16bit, 32bit displacement in encoding. */
a501d77e
L
408 enum
409 {
410 disp_encoding_default = 0,
411 disp_encoding_8bit,
41eb8e88 412 disp_encoding_16bit,
a501d77e
L
413 disp_encoding_32bit
414 } disp_encoding;
f8a5c266 415
6b6b6807 416 /* Prefer the REX byte in encoding. */
5b7c81bd 417 bool rex_encoding;
6b6b6807 418
b6f8c7c4 419 /* Disable instruction size optimization. */
5b7c81bd 420 bool no_optimize;
b6f8c7c4 421
86fa6981
L
422 /* How to encode vector instructions. */
423 enum
424 {
425 vex_encoding_default = 0,
42e04b36 426 vex_encoding_vex,
86fa6981 427 vex_encoding_vex3,
da4977e0
JB
428 vex_encoding_evex,
429 vex_encoding_error
86fa6981
L
430 } vec_encoding;
431
d5de92cf
L
432 /* REP prefix. */
433 const char *rep_prefix;
434
165de32a
L
435 /* HLE prefix. */
436 const char *hle_prefix;
42164a71 437
7e8b059b
L
438 /* Have BND prefix. */
439 const char *bnd_prefix;
440
04ef582a
L
441 /* Have NOTRACK prefix. */
442 const char *notrack_prefix;
443
891edac4 444 /* Error message. */
a65babc9 445 enum i386_error error;
252b5132
RH
446 };
447
448typedef struct _i386_insn i386_insn;
449
43234a1e
L
450/* Link RC type with corresponding string, that'll be looked for in
451 asm. */
452struct RC_name
453{
454 enum rc_type type;
455 const char *name;
456 unsigned int len;
457};
458
459static const struct RC_name RC_NamesTable[] =
460{
461 { rne, STRING_COMMA_LEN ("rn-sae") },
462 { rd, STRING_COMMA_LEN ("rd-sae") },
463 { ru, STRING_COMMA_LEN ("ru-sae") },
464 { rz, STRING_COMMA_LEN ("rz-sae") },
465 { saeonly, STRING_COMMA_LEN ("sae") },
466};
467
3bfea8ba
L
468/* To be indexed by segment register number. */
469static const unsigned char i386_seg_prefixes[] = {
470 ES_PREFIX_OPCODE,
471 CS_PREFIX_OPCODE,
472 SS_PREFIX_OPCODE,
473 DS_PREFIX_OPCODE,
474 FS_PREFIX_OPCODE,
475 GS_PREFIX_OPCODE
476};
477
252b5132
RH
478/* List of chars besides those in app.c:symbol_chars that can start an
479 operand. Used to prevent the scrubber eating vital white-space. */
86fa6981 480const char extra_symbol_chars[] = "*%-([{}"
252b5132 481#ifdef LEX_AT
32137342
NC
482 "@"
483#endif
484#ifdef LEX_QM
485 "?"
252b5132 486#endif
32137342 487 ;
252b5132 488
b3983e5f
JB
489#if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
490 && !defined (TE_GNU) \
491 && !defined (TE_LINUX) \
d85e70a3 492 && !defined (TE_Haiku) \
b3983e5f
JB
493 && !defined (TE_FreeBSD) \
494 && !defined (TE_DragonFly) \
495 && !defined (TE_NetBSD))
252b5132 496/* This array holds the chars that always start a comment. If the
b3b91714
AM
497 pre-processor is disabled, these aren't very useful. The option
498 --divide will remove '/' from this list. */
499const char *i386_comment_chars = "#/";
500#define SVR4_COMMENT_CHARS 1
252b5132 501#define PREFIX_SEPARATOR '\\'
252b5132 502
b3b91714
AM
503#else
504const char *i386_comment_chars = "#";
505#define PREFIX_SEPARATOR '/'
506#endif
507
252b5132
RH
508/* This array holds the chars that only start a comment at the beginning of
509 a line. If the line seems to have the form '# 123 filename'
ce8a8b2f
AM
510 .line and .file directives will appear in the pre-processed output.
511 Note that input_file.c hand checks for '#' at the beginning of the
252b5132 512 first line of the input file. This is because the compiler outputs
ce8a8b2f
AM
513 #NO_APP at the beginning of its output.
514 Also note that comments started like this one will always work if
252b5132 515 '/' isn't otherwise defined. */
b3b91714 516const char line_comment_chars[] = "#/";
252b5132 517
63a0b638 518const char line_separator_chars[] = ";";
252b5132 519
ce8a8b2f
AM
520/* Chars that can be used to separate mant from exp in floating point
521 nums. */
252b5132
RH
522const char EXP_CHARS[] = "eE";
523
ce8a8b2f
AM
524/* Chars that mean this number is a floating point constant
525 As in 0f12.456
526 or 0d1.2345e12. */
de133cf9 527const char FLT_CHARS[] = "fFdDxXhHbB";
252b5132 528
ce8a8b2f 529/* Tables for lexical analysis. */
252b5132
RH
530static char mnemonic_chars[256];
531static char register_chars[256];
532static char operand_chars[256];
533static char identifier_chars[256];
252b5132 534
ce8a8b2f 535/* Lexical macros. */
252b5132
RH
536#define is_mnemonic_char(x) (mnemonic_chars[(unsigned char) x])
537#define is_operand_char(x) (operand_chars[(unsigned char) x])
538#define is_register_char(x) (register_chars[(unsigned char) x])
539#define is_space_char(x) ((x) == ' ')
540#define is_identifier_char(x) (identifier_chars[(unsigned char) x])
252b5132 541
0234cb7c 542/* All non-digit non-letter characters that may occur in an operand. */
252b5132
RH
543static char operand_special_chars[] = "%$-+(,)*._~/<>|&^!:[@]";
544
545/* md_assemble() always leaves the strings it's passed unaltered. To
546 effect this we maintain a stack of saved characters that we've smashed
547 with '\0's (indicating end of strings for various sub-fields of the
47926f60 548 assembler instruction). */
252b5132 549static char save_stack[32];
ce8a8b2f 550static char *save_stack_p;
252b5132
RH
551#define END_STRING_AND_SAVE(s) \
552 do { *save_stack_p++ = *(s); *(s) = '\0'; } while (0)
553#define RESTORE_END_STRING(s) \
554 do { *(s) = *--save_stack_p; } while (0)
555
47926f60 556/* The instruction we're assembling. */
252b5132
RH
557static i386_insn i;
558
559/* Possible templates for current insn. */
560static const templates *current_templates;
561
31b2323c
L
562/* Per instruction expressionS buffers: max displacements & immediates. */
563static expressionS disp_expressions[MAX_MEMORY_OPERANDS];
564static expressionS im_expressions[MAX_IMMEDIATE_OPERANDS];
252b5132 565
47926f60 566/* Current operand we are working on. */
ee86248c 567static int this_operand = -1;
252b5132 568
3e73aa7c
JH
569/* We support four different modes. FLAG_CODE variable is used to distinguish
570 these. */
571
572enum flag_code {
573 CODE_32BIT,
574 CODE_16BIT,
575 CODE_64BIT };
576
577static enum flag_code flag_code;
4fa24527 578static unsigned int object_64bit;
862be3fb 579static unsigned int disallow_64bit_reloc;
3e73aa7c 580static int use_rela_relocations = 0;
e379e5f3
L
581/* __tls_get_addr/___tls_get_addr symbol for TLS. */
582static const char *tls_get_addr;
3e73aa7c 583
7af8ed2d
NC
584#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
585 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
586 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
587
351f65ca
L
588/* The ELF ABI to use. */
589enum x86_elf_abi
590{
591 I386_ABI,
7f56bc95
L
592 X86_64_ABI,
593 X86_64_X32_ABI
351f65ca
L
594};
595
596static enum x86_elf_abi x86_elf_abi = I386_ABI;
7af8ed2d 597#endif
351f65ca 598
167ad85b
TG
599#if defined (TE_PE) || defined (TE_PEP)
600/* Use big object file format. */
601static int use_big_obj = 0;
602#endif
603
8dcea932
L
604#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
605/* 1 if generating code for a shared library. */
606static int shared = 0;
b52c4ee4
IB
607
608unsigned int x86_sframe_cfa_sp_reg;
609/* The other CFA base register for SFrame unwind info. */
610unsigned int x86_sframe_cfa_fp_reg;
611unsigned int x86_sframe_cfa_ra_reg;
612
8dcea932
L
613#endif
614
47926f60
KH
615/* 1 for intel syntax,
616 0 if att syntax. */
617static int intel_syntax = 0;
252b5132 618
4b5aaf5f
L
619static enum x86_64_isa
620{
621 amd64 = 1, /* AMD64 ISA. */
622 intel64 /* Intel64 ISA. */
623} isa64;
e89c5eaa 624
1efbbeb4
L
625/* 1 for intel mnemonic,
626 0 if att mnemonic. */
627static int intel_mnemonic = !SYSV386_COMPAT;
628
a60de03c
JB
629/* 1 if pseudo registers are permitted. */
630static int allow_pseudo_reg = 0;
631
47926f60
KH
632/* 1 if register prefix % not required. */
633static int allow_naked_reg = 0;
252b5132 634
33eaf5de 635/* 1 if the assembler should add BND prefix for all control-transferring
7e8b059b
L
636 instructions supporting it, even if this prefix wasn't specified
637 explicitly. */
638static int add_bnd_prefix = 0;
639
ba104c83 640/* 1 if pseudo index register, eiz/riz, is allowed . */
db51cc60
L
641static int allow_index_reg = 0;
642
d022bddd
IT
643/* 1 if the assembler should ignore LOCK prefix, even if it was
644 specified explicitly. */
645static int omit_lock_prefix = 0;
646
e4e00185
AS
647/* 1 if the assembler should encode lfence, mfence, and sfence as
648 "lock addl $0, (%{re}sp)". */
649static int avoid_fence = 0;
650
ae531041
L
651/* 1 if lfence should be inserted after every load. */
652static int lfence_after_load = 0;
653
654/* Non-zero if lfence should be inserted before indirect branch. */
655static enum lfence_before_indirect_branch_kind
656 {
657 lfence_branch_none = 0,
658 lfence_branch_register,
659 lfence_branch_memory,
660 lfence_branch_all
661 }
662lfence_before_indirect_branch;
663
664/* Non-zero if lfence should be inserted before ret. */
665static enum lfence_before_ret_kind
666 {
667 lfence_before_ret_none = 0,
668 lfence_before_ret_not,
a09f656b 669 lfence_before_ret_or,
670 lfence_before_ret_shl
ae531041
L
671 }
672lfence_before_ret;
673
674/* Types of previous instruction is .byte or prefix. */
e379e5f3
L
675static struct
676 {
677 segT seg;
678 const char *file;
679 const char *name;
680 unsigned int line;
681 enum last_insn_kind
682 {
683 last_insn_other = 0,
684 last_insn_directive,
685 last_insn_prefix
686 } kind;
687 } last_insn;
688
0cb4071e
L
689/* 1 if the assembler should generate relax relocations. */
690
691static int generate_relax_relocations
692 = DEFAULT_GENERATE_X86_RELAX_RELOCATIONS;
693
7bab8ab5 694static enum check_kind
daf50ae7 695 {
7bab8ab5
JB
696 check_none = 0,
697 check_warning,
698 check_error
daf50ae7 699 }
7bab8ab5 700sse_check, operand_check = check_warning;
daf50ae7 701
e379e5f3
L
702/* Non-zero if branches should be aligned within power of 2 boundary. */
703static int align_branch_power = 0;
704
705/* Types of branches to align. */
706enum align_branch_kind
707 {
708 align_branch_none = 0,
709 align_branch_jcc = 1,
710 align_branch_fused = 2,
711 align_branch_jmp = 3,
712 align_branch_call = 4,
713 align_branch_indirect = 5,
714 align_branch_ret = 6
715 };
716
717/* Type bits of branches to align. */
718enum align_branch_bit
719 {
720 align_branch_jcc_bit = 1 << align_branch_jcc,
721 align_branch_fused_bit = 1 << align_branch_fused,
722 align_branch_jmp_bit = 1 << align_branch_jmp,
723 align_branch_call_bit = 1 << align_branch_call,
724 align_branch_indirect_bit = 1 << align_branch_indirect,
725 align_branch_ret_bit = 1 << align_branch_ret
726 };
727
728static unsigned int align_branch = (align_branch_jcc_bit
729 | align_branch_fused_bit
730 | align_branch_jmp_bit);
731
79d72f45
HL
732/* Types of condition jump used by macro-fusion. */
733enum mf_jcc_kind
734 {
735 mf_jcc_jo = 0, /* base opcode 0x70 */
736 mf_jcc_jc, /* base opcode 0x72 */
737 mf_jcc_je, /* base opcode 0x74 */
738 mf_jcc_jna, /* base opcode 0x76 */
739 mf_jcc_js, /* base opcode 0x78 */
740 mf_jcc_jp, /* base opcode 0x7a */
741 mf_jcc_jl, /* base opcode 0x7c */
742 mf_jcc_jle, /* base opcode 0x7e */
743 };
744
745/* Types of compare flag-modifying insntructions used by macro-fusion. */
746enum mf_cmp_kind
747 {
748 mf_cmp_test_and, /* test/cmp */
749 mf_cmp_alu_cmp, /* add/sub/cmp */
750 mf_cmp_incdec /* inc/dec */
751 };
752
e379e5f3
L
753/* The maximum padding size for fused jcc. CMP like instruction can
754 be 9 bytes and jcc can be 6 bytes. Leave room just in case for
755 prefixes. */
756#define MAX_FUSED_JCC_PADDING_SIZE 20
757
758/* The maximum number of prefixes added for an instruction. */
759static unsigned int align_branch_prefix_size = 5;
760
b6f8c7c4
L
761/* Optimization:
762 1. Clear the REX_W bit with register operand if possible.
763 2. Above plus use 128bit vector instruction to clear the full vector
764 register.
765 */
766static int optimize = 0;
767
768/* Optimization:
769 1. Clear the REX_W bit with register operand if possible.
770 2. Above plus use 128bit vector instruction to clear the full vector
771 register.
772 3. Above plus optimize "test{q,l,w} $imm8,%r{64,32,16}" to
773 "testb $imm7,%r8".
774 */
775static int optimize_for_space = 0;
776
2ca3ace5
L
777/* Register prefix used for error message. */
778static const char *register_prefix = "%";
779
47926f60
KH
780/* Used in 16 bit gcc mode to add an l suffix to call, ret, enter,
781 leave, push, and pop instructions so that gcc has the same stack
782 frame as in 32 bit mode. */
783static char stackop_size = '\0';
eecb386c 784
12b55ccc
L
785/* Non-zero to optimize code alignment. */
786int optimize_align_code = 1;
787
47926f60
KH
788/* Non-zero to quieten some warnings. */
789static int quiet_warnings = 0;
a38cf1db 790
d59a54c2
JB
791/* Guard to avoid repeated warnings about non-16-bit code on 16-bit CPUs. */
792static bool pre_386_16bit_warned;
793
47926f60
KH
794/* CPU name. */
795static const char *cpu_arch_name = NULL;
6305a203 796static char *cpu_sub_arch_name = NULL;
a38cf1db 797
47926f60 798/* CPU feature flags. */
40fb9820
L
799static i386_cpu_flags cpu_arch_flags = CPU_UNKNOWN_FLAGS;
800
ccc9c027
L
801/* If we have selected a cpu we are generating instructions for. */
802static int cpu_arch_tune_set = 0;
803
9103f4f4 804/* Cpu we are generating instructions for. */
fbf3f584 805enum processor_type cpu_arch_tune = PROCESSOR_UNKNOWN;
9103f4f4
L
806
807/* CPU feature flags of cpu we are generating instructions for. */
40fb9820 808static i386_cpu_flags cpu_arch_tune_flags;
9103f4f4 809
ccc9c027 810/* CPU instruction set architecture used. */
fbf3f584 811enum processor_type cpu_arch_isa = PROCESSOR_UNKNOWN;
ccc9c027 812
9103f4f4 813/* CPU feature flags of instruction set architecture used. */
fbf3f584 814i386_cpu_flags cpu_arch_isa_flags;
9103f4f4 815
fddf5b5b
AM
816/* If set, conditional jumps are not automatically promoted to handle
817 larger than a byte offset. */
f68697e8 818static bool no_cond_jump_promotion = false;
fddf5b5b 819
c0f3af97
L
820/* Encode SSE instructions with VEX prefix. */
821static unsigned int sse2avx;
822
c8480b58
L
823/* Encode aligned vector move as unaligned vector move. */
824static unsigned int use_unaligned_vector_move;
825
539f890d
L
826/* Encode scalar AVX instructions with specific vector length. */
827static enum
828 {
829 vex128 = 0,
830 vex256
831 } avxscalar;
832
03751133
L
833/* Encode VEX WIG instructions with specific vex.w. */
834static enum
835 {
836 vexw0 = 0,
837 vexw1
838 } vexwig;
839
43234a1e
L
840/* Encode scalar EVEX LIG instructions with specific vector length. */
841static enum
842 {
843 evexl128 = 0,
844 evexl256,
845 evexl512
846 } evexlig;
847
848/* Encode EVEX WIG instructions with specific evex.w. */
849static enum
850 {
851 evexw0 = 0,
852 evexw1
853 } evexwig;
854
d3d3c6db
IT
855/* Value to encode in EVEX RC bits, for SAE-only instructions. */
856static enum rc_type evexrcig = rne;
857
29b0f896 858/* Pre-defined "_GLOBAL_OFFSET_TABLE_". */
87c245cc 859static symbolS *GOT_symbol;
29b0f896 860
a4447b93
RH
861/* The dwarf2 return column, adjusted for 32 or 64 bit. */
862unsigned int x86_dwarf2_return_column;
863
864/* The dwarf2 data alignment, adjusted for 32 or 64 bit. */
865int x86_cie_data_alignment;
866
252b5132 867/* Interface to relax_segment.
fddf5b5b
AM
868 There are 3 major relax states for 386 jump insns because the
869 different types of jumps add different sizes to frags when we're
e379e5f3
L
870 figuring out what sort of jump to choose to reach a given label.
871
872 BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING are used to align
873 branches which are handled by md_estimate_size_before_relax() and
874 i386_generic_table_relax_frag(). */
252b5132 875
47926f60 876/* Types. */
93c2a809
AM
877#define UNCOND_JUMP 0
878#define COND_JUMP 1
879#define COND_JUMP86 2
e379e5f3
L
880#define BRANCH_PADDING 3
881#define BRANCH_PREFIX 4
882#define FUSED_JCC_PADDING 5
fddf5b5b 883
47926f60 884/* Sizes. */
252b5132
RH
885#define CODE16 1
886#define SMALL 0
29b0f896 887#define SMALL16 (SMALL | CODE16)
252b5132 888#define BIG 2
29b0f896 889#define BIG16 (BIG | CODE16)
252b5132
RH
890
891#ifndef INLINE
892#ifdef __GNUC__
893#define INLINE __inline__
894#else
895#define INLINE
896#endif
897#endif
898
fddf5b5b
AM
899#define ENCODE_RELAX_STATE(type, size) \
900 ((relax_substateT) (((type) << 2) | (size)))
901#define TYPE_FROM_RELAX_STATE(s) \
902 ((s) >> 2)
903#define DISP_SIZE_FROM_RELAX_STATE(s) \
904 ((((s) & 3) == BIG ? 4 : (((s) & 3) == BIG16 ? 2 : 1)))
252b5132
RH
905
906/* This table is used by relax_frag to promote short jumps to long
907 ones where necessary. SMALL (short) jumps may be promoted to BIG
908 (32 bit long) ones, and SMALL16 jumps to BIG16 (16 bit long). We
909 don't allow a short jump in a 32 bit code segment to be promoted to
910 a 16 bit offset jump because it's slower (requires data size
911 prefix), and doesn't work, unless the destination is in the bottom
912 64k of the code segment (The top 16 bits of eip are zeroed). */
913
914const relax_typeS md_relax_table[] =
915{
24eab124
AM
916 /* The fields are:
917 1) most positive reach of this state,
918 2) most negative reach of this state,
93c2a809 919 3) how many bytes this mode will have in the variable part of the frag
ce8a8b2f 920 4) which index into the table to try if we can't fit into this one. */
252b5132 921
fddf5b5b 922 /* UNCOND_JUMP states. */
93c2a809
AM
923 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG)},
924 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16)},
925 /* dword jmp adds 4 bytes to frag:
926 0 extra opcode bytes, 4 displacement bytes. */
252b5132 927 {0, 0, 4, 0},
93c2a809
AM
928 /* word jmp adds 2 byte2 to frag:
929 0 extra opcode bytes, 2 displacement bytes. */
252b5132
RH
930 {0, 0, 2, 0},
931
93c2a809
AM
932 /* COND_JUMP states. */
933 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG)},
934 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG16)},
935 /* dword conditionals adds 5 bytes to frag:
936 1 extra opcode byte, 4 displacement bytes. */
937 {0, 0, 5, 0},
fddf5b5b 938 /* word conditionals add 3 bytes to frag:
93c2a809
AM
939 1 extra opcode byte, 2 displacement bytes. */
940 {0, 0, 3, 0},
941
942 /* COND_JUMP86 states. */
943 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG)},
944 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG16)},
945 /* dword conditionals adds 5 bytes to frag:
946 1 extra opcode byte, 4 displacement bytes. */
947 {0, 0, 5, 0},
948 /* word conditionals add 4 bytes to frag:
949 1 displacement byte and a 3 byte long branch insn. */
950 {0, 0, 4, 0}
252b5132
RH
951};
952
6ceeed25 953#define ARCH(n, t, f, s) \
ae89daec
JB
954 { STRING_COMMA_LEN (#n), s, PROCESSOR_ ## t, CPU_ ## f ## _FLAGS, \
955 CPU_NONE_FLAGS }
956#define SUBARCH(n, e, d, s) \
957 { STRING_COMMA_LEN (#n), s, PROCESSOR_NONE, CPU_ ## e ## _FLAGS, \
958 CPU_ ## d ## _FLAGS }
6ceeed25 959
9103f4f4
L
960static const arch_entry cpu_arch[] =
961{
3ce2ebcf
JB
962 /* Do not replace the first two entries - i386_target_format() and
963 set_cpu_arch() rely on them being there in this order. */
6ceeed25
JB
964 ARCH (generic32, GENERIC32, GENERIC32, false),
965 ARCH (generic64, GENERIC64, GENERIC64, false),
966 ARCH (i8086, UNKNOWN, NONE, false),
4d97c5c8
JB
967 ARCH (i186, UNKNOWN, 186, false),
968 ARCH (i286, UNKNOWN, 286, false),
969 ARCH (i386, I386, 386, false),
970 ARCH (i486, I486, 486, false),
971 ARCH (i586, PENTIUM, 586, false),
972 ARCH (i686, PENTIUMPRO, 686, false),
973 ARCH (pentium, PENTIUM, 586, false),
6ceeed25
JB
974 ARCH (pentiumpro, PENTIUMPRO, PENTIUMPRO, false),
975 ARCH (pentiumii, PENTIUMPRO, P2, false),
976 ARCH (pentiumiii, PENTIUMPRO, P3, false),
977 ARCH (pentium4, PENTIUM4, P4, false),
978 ARCH (prescott, NOCONA, CORE, false),
979 ARCH (nocona, NOCONA, NOCONA, false),
980 ARCH (yonah, CORE, CORE, true),
981 ARCH (core, CORE, CORE, false),
982 ARCH (merom, CORE2, CORE2, true),
983 ARCH (core2, CORE2, CORE2, false),
984 ARCH (corei7, COREI7, COREI7, false),
985 ARCH (iamcu, IAMCU, IAMCU, false),
986 ARCH (k6, K6, K6, false),
987 ARCH (k6_2, K6, K6_2, false),
988 ARCH (athlon, ATHLON, ATHLON, false),
989 ARCH (sledgehammer, K8, K8, true),
990 ARCH (opteron, K8, K8, false),
991 ARCH (k8, K8, K8, false),
992 ARCH (amdfam10, AMDFAM10, AMDFAM10, false),
993 ARCH (bdver1, BD, BDVER1, false),
994 ARCH (bdver2, BD, BDVER2, false),
995 ARCH (bdver3, BD, BDVER3, false),
996 ARCH (bdver4, BD, BDVER4, false),
997 ARCH (znver1, ZNVER, ZNVER1, false),
998 ARCH (znver2, ZNVER, ZNVER2, false),
999 ARCH (znver3, ZNVER, ZNVER3, false),
b0e8fa7f 1000 ARCH (znver4, ZNVER, ZNVER4, false),
6ceeed25
JB
1001 ARCH (btver1, BT, BTVER1, false),
1002 ARCH (btver2, BT, BTVER2, false),
1003
4d97c5c8
JB
1004 SUBARCH (8087, 8087, ANY_8087, false),
1005 SUBARCH (87, NONE, ANY_8087, false), /* Disable only! */
ae89daec
JB
1006 SUBARCH (287, 287, ANY_287, false),
1007 SUBARCH (387, 387, ANY_387, false),
1008 SUBARCH (687, 687, ANY_687, false),
4d97c5c8 1009 SUBARCH (cmov, CMOV, CMOV, false),
88bd2203 1010 SUBARCH (fxsr, FXSR, ANY_FXSR, false),
ae89daec
JB
1011 SUBARCH (mmx, MMX, ANY_MMX, false),
1012 SUBARCH (sse, SSE, ANY_SSE, false),
1013 SUBARCH (sse2, SSE2, ANY_SSE2, false),
1014 SUBARCH (sse3, SSE3, ANY_SSE3, false),
1015 SUBARCH (sse4a, SSE4A, ANY_SSE4A, false),
1016 SUBARCH (ssse3, SSSE3, ANY_SSSE3, false),
1017 SUBARCH (sse4.1, SSE4_1, ANY_SSE4_1, false),
1018 SUBARCH (sse4.2, SSE4_2, ANY_SSE4_2, false),
1019 SUBARCH (sse4, SSE4_2, ANY_SSE4_1, false),
1020 SUBARCH (avx, AVX, ANY_AVX, false),
1021 SUBARCH (avx2, AVX2, ANY_AVX2, false),
1022 SUBARCH (avx512f, AVX512F, ANY_AVX512F, false),
1023 SUBARCH (avx512cd, AVX512CD, ANY_AVX512CD, false),
1024 SUBARCH (avx512er, AVX512ER, ANY_AVX512ER, false),
1025 SUBARCH (avx512pf, AVX512PF, ANY_AVX512PF, false),
1026 SUBARCH (avx512dq, AVX512DQ, ANY_AVX512DQ, false),
1027 SUBARCH (avx512bw, AVX512BW, ANY_AVX512BW, false),
1028 SUBARCH (avx512vl, AVX512VL, ANY_AVX512VL, false),
25626f79
JB
1029 SUBARCH (vmx, VMX, ANY_VMX, false),
1030 SUBARCH (vmfunc, VMFUNC, ANY_VMFUNC, false),
ae89daec 1031 SUBARCH (smx, SMX, SMX, false),
5091b9ee
JB
1032 SUBARCH (xsave, XSAVE, ANY_XSAVE, false),
1033 SUBARCH (xsaveopt, XSAVEOPT, ANY_XSAVEOPT, false),
1034 SUBARCH (xsavec, XSAVEC, ANY_XSAVEC, false),
1035 SUBARCH (xsaves, XSAVES, ANY_XSAVES, false),
1036 SUBARCH (aes, AES, ANY_AES, false),
1037 SUBARCH (pclmul, PCLMUL, ANY_PCLMUL, false),
1038 SUBARCH (clmul, PCLMUL, ANY_PCLMUL, true),
ae89daec
JB
1039 SUBARCH (fsgsbase, FSGSBASE, FSGSBASE, false),
1040 SUBARCH (rdrnd, RDRND, RDRND, false),
5091b9ee 1041 SUBARCH (f16c, F16C, ANY_F16C, false),
ae89daec 1042 SUBARCH (bmi2, BMI2, BMI2, false),
5091b9ee
JB
1043 SUBARCH (fma, FMA, ANY_FMA, false),
1044 SUBARCH (fma4, FMA4, ANY_FMA4, false),
1045 SUBARCH (xop, XOP, ANY_XOP, false),
1046 SUBARCH (lwp, LWP, ANY_LWP, false),
ae89daec
JB
1047 SUBARCH (movbe, MOVBE, MOVBE, false),
1048 SUBARCH (cx16, CX16, CX16, false),
25626f79 1049 SUBARCH (ept, EPT, ANY_EPT, false),
ae89daec
JB
1050 SUBARCH (lzcnt, LZCNT, LZCNT, false),
1051 SUBARCH (popcnt, POPCNT, POPCNT, false),
1052 SUBARCH (hle, HLE, HLE, false),
760ab3d0
JB
1053 SUBARCH (rtm, RTM, ANY_RTM, false),
1054 SUBARCH (tsx, TSX, TSX, false),
ae89daec
JB
1055 SUBARCH (invpcid, INVPCID, INVPCID, false),
1056 SUBARCH (clflush, CLFLUSH, CLFLUSH, false),
1057 SUBARCH (nop, NOP, NOP, false),
1058 SUBARCH (syscall, SYSCALL, SYSCALL, false),
1059 SUBARCH (rdtscp, RDTSCP, RDTSCP, false),
5091b9ee
JB
1060 SUBARCH (3dnow, 3DNOW, ANY_3DNOW, false),
1061 SUBARCH (3dnowa, 3DNOWA, ANY_3DNOWA, false),
ae89daec 1062 SUBARCH (padlock, PADLOCK, PADLOCK, false),
0919e770
JB
1063 SUBARCH (pacifica, SVME, ANY_SVME, true),
1064 SUBARCH (svme, SVME, ANY_SVME, false),
ae89daec
JB
1065 SUBARCH (abm, ABM, ABM, false),
1066 SUBARCH (bmi, BMI, BMI, false),
1067 SUBARCH (tbm, TBM, TBM, false),
1068 SUBARCH (adx, ADX, ADX, false),
1069 SUBARCH (rdseed, RDSEED, RDSEED, false),
1070 SUBARCH (prfchw, PRFCHW, PRFCHW, false),
1071 SUBARCH (smap, SMAP, SMAP, false),
5091b9ee
JB
1072 SUBARCH (mpx, MPX, ANY_MPX, false),
1073 SUBARCH (sha, SHA, ANY_SHA, false),
ae89daec
JB
1074 SUBARCH (clflushopt, CLFLUSHOPT, CLFLUSHOPT, false),
1075 SUBARCH (prefetchwt1, PREFETCHWT1, PREFETCHWT1, false),
1076 SUBARCH (se1, SE1, SE1, false),
1077 SUBARCH (clwb, CLWB, CLWB, false),
1078 SUBARCH (avx512ifma, AVX512IFMA, ANY_AVX512IFMA, false),
1079 SUBARCH (avx512vbmi, AVX512VBMI, ANY_AVX512VBMI, false),
1080 SUBARCH (avx512_4fmaps, AVX512_4FMAPS, ANY_AVX512_4FMAPS, false),
1081 SUBARCH (avx512_4vnniw, AVX512_4VNNIW, ANY_AVX512_4VNNIW, false),
1082 SUBARCH (avx512_vpopcntdq, AVX512_VPOPCNTDQ, ANY_AVX512_VPOPCNTDQ, false),
1083 SUBARCH (avx512_vbmi2, AVX512_VBMI2, ANY_AVX512_VBMI2, false),
1084 SUBARCH (avx512_vnni, AVX512_VNNI, ANY_AVX512_VNNI, false),
1085 SUBARCH (avx512_bitalg, AVX512_BITALG, ANY_AVX512_BITALG, false),
1086 SUBARCH (avx_vnni, AVX_VNNI, ANY_AVX_VNNI, false),
1087 SUBARCH (clzero, CLZERO, CLZERO, false),
1088 SUBARCH (mwaitx, MWAITX, MWAITX, false),
5091b9ee 1089 SUBARCH (ospke, OSPKE, ANY_OSPKE, false),
ae89daec
JB
1090 SUBARCH (rdpid, RDPID, RDPID, false),
1091 SUBARCH (ptwrite, PTWRITE, PTWRITE, false),
4d97c5c8
JB
1092 SUBARCH (ibt, IBT, IBT, false),
1093 SUBARCH (shstk, SHSTK, SHSTK, false),
88bd2203 1094 SUBARCH (gfni, GFNI, ANY_GFNI, false),
b20f4261
JB
1095 SUBARCH (vaes, VAES, ANY_VAES, false),
1096 SUBARCH (vpclmulqdq, VPCLMULQDQ, ANY_VPCLMULQDQ, false),
ae89daec
JB
1097 SUBARCH (wbnoinvd, WBNOINVD, WBNOINVD, false),
1098 SUBARCH (pconfig, PCONFIG, PCONFIG, false),
1099 SUBARCH (waitpkg, WAITPKG, WAITPKG, false),
1100 SUBARCH (cldemote, CLDEMOTE, CLDEMOTE, false),
1101 SUBARCH (amx_int8, AMX_INT8, ANY_AMX_INT8, false),
1102 SUBARCH (amx_bf16, AMX_BF16, ANY_AMX_BF16, false),
5091b9ee 1103 SUBARCH (amx_fp16, AMX_FP16, ANY_AMX_FP16, false),
ae89daec 1104 SUBARCH (amx_tile, AMX_TILE, ANY_AMX_TILE, false),
4d97c5c8
JB
1105 SUBARCH (movdiri, MOVDIRI, MOVDIRI, false),
1106 SUBARCH (movdir64b, MOVDIR64B, MOVDIR64B, false),
ae89daec
JB
1107 SUBARCH (avx512_bf16, AVX512_BF16, ANY_AVX512_BF16, false),
1108 SUBARCH (avx512_vp2intersect, AVX512_VP2INTERSECT,
1109 ANY_AVX512_VP2INTERSECT, false),
4d97c5c8
JB
1110 SUBARCH (tdx, TDX, TDX, false),
1111 SUBARCH (enqcmd, ENQCMD, ENQCMD, false),
1112 SUBARCH (serialize, SERIALIZE, SERIALIZE, false),
ae89daec
JB
1113 SUBARCH (rdpru, RDPRU, RDPRU, false),
1114 SUBARCH (mcommit, MCOMMIT, MCOMMIT, false),
0919e770 1115 SUBARCH (sev_es, SEV_ES, ANY_SEV_ES, false),
760ab3d0 1116 SUBARCH (tsxldtrk, TSXLDTRK, ANY_TSXLDTRK, false),
88bd2203
JB
1117 SUBARCH (kl, KL, ANY_KL, false),
1118 SUBARCH (widekl, WIDEKL, ANY_WIDEKL, false),
4d97c5c8
JB
1119 SUBARCH (uintr, UINTR, UINTR, false),
1120 SUBARCH (hreset, HRESET, HRESET, false),
ae89daec 1121 SUBARCH (avx512_fp16, AVX512_FP16, ANY_AVX512_FP16, false),
ef07be45 1122 SUBARCH (prefetchi, PREFETCHI, PREFETCHI, false),
4321af3e 1123 SUBARCH (avx_ifma, AVX_IFMA, ANY_AVX_IFMA, false),
23ae61ad 1124 SUBARCH (avx_vnni_int8, AVX_VNNI_INT8, ANY_AVX_VNNI_INT8, false),
4d97c5c8
JB
1125 SUBARCH (cmpccxadd, CMPCCXADD, CMPCCXADD, false),
1126 SUBARCH (wrmsrns, WRMSRNS, WRMSRNS, false),
1127 SUBARCH (msrlist, MSRLIST, MSRLIST, false),
01d8ce74 1128 SUBARCH (avx_ne_convert, AVX_NE_CONVERT, ANY_AVX_NE_CONVERT, false),
4d97c5c8 1129 SUBARCH (rao_int, RAO_INT, RAO_INT, false),
0919e770 1130 SUBARCH (rmpquery, RMPQUERY, ANY_RMPQUERY, false),
293f5f65
L
1131};
1132
6ceeed25
JB
1133#undef SUBARCH
1134#undef ARCH
1135
704209c0 1136#ifdef I386COFF
a6c24e68
NC
1137/* Like s_lcomm_internal in gas/read.c but the alignment string
1138 is allowed to be optional. */
1139
1140static symbolS *
1141pe_lcomm_internal (int needs_align, symbolS *symbolP, addressT size)
1142{
1143 addressT align = 0;
1144
1145 SKIP_WHITESPACE ();
1146
7ab9ffdd 1147 if (needs_align
a6c24e68
NC
1148 && *input_line_pointer == ',')
1149 {
1150 align = parse_align (needs_align - 1);
7ab9ffdd 1151
a6c24e68
NC
1152 if (align == (addressT) -1)
1153 return NULL;
1154 }
1155 else
1156 {
1157 if (size >= 8)
1158 align = 3;
1159 else if (size >= 4)
1160 align = 2;
1161 else if (size >= 2)
1162 align = 1;
1163 else
1164 align = 0;
1165 }
1166
1167 bss_alloc (symbolP, size, align);
1168 return symbolP;
1169}
1170
704209c0 1171static void
a6c24e68
NC
1172pe_lcomm (int needs_align)
1173{
1174 s_comm_internal (needs_align * 2, pe_lcomm_internal);
1175}
704209c0 1176#endif
a6c24e68 1177
29b0f896
AM
1178const pseudo_typeS md_pseudo_table[] =
1179{
1180#if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
1181 {"align", s_align_bytes, 0},
1182#else
1183 {"align", s_align_ptwo, 0},
1184#endif
1185 {"arch", set_cpu_arch, 0},
1186#ifndef I386COFF
1187 {"bss", s_bss, 0},
a6c24e68
NC
1188#else
1189 {"lcomm", pe_lcomm, 1},
29b0f896
AM
1190#endif
1191 {"ffloat", float_cons, 'f'},
1192 {"dfloat", float_cons, 'd'},
1193 {"tfloat", float_cons, 'x'},
7d19d096 1194 {"hfloat", float_cons, 'h'},
de133cf9 1195 {"bfloat16", float_cons, 'b'},
29b0f896 1196 {"value", cons, 2},
d182319b 1197 {"slong", signed_cons, 4},
29b0f896
AM
1198 {"noopt", s_ignore, 0},
1199 {"optim", s_ignore, 0},
1200 {"code16gcc", set_16bit_gcc_code_flag, CODE_16BIT},
1201 {"code16", set_code_flag, CODE_16BIT},
1202 {"code32", set_code_flag, CODE_32BIT},
da5f19a2 1203#ifdef BFD64
29b0f896 1204 {"code64", set_code_flag, CODE_64BIT},
da5f19a2 1205#endif
29b0f896
AM
1206 {"intel_syntax", set_intel_syntax, 1},
1207 {"att_syntax", set_intel_syntax, 0},
1efbbeb4
L
1208 {"intel_mnemonic", set_intel_mnemonic, 1},
1209 {"att_mnemonic", set_intel_mnemonic, 0},
db51cc60
L
1210 {"allow_index_reg", set_allow_index_reg, 1},
1211 {"disallow_index_reg", set_allow_index_reg, 0},
7bab8ab5
JB
1212 {"sse_check", set_check, 0},
1213 {"operand_check", set_check, 1},
3b22753a
L
1214#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1215 {"largecomm", handle_large_common, 0},
07a53e5c 1216#else
68d20676 1217 {"file", dwarf2_directive_file, 0},
07a53e5c
RH
1218 {"loc", dwarf2_directive_loc, 0},
1219 {"loc_mark_labels", dwarf2_directive_loc_mark_labels, 0},
3b22753a 1220#endif
6482c264
NC
1221#ifdef TE_PE
1222 {"secrel32", pe_directive_secrel, 0},
145667f8 1223 {"secidx", pe_directive_secidx, 0},
6482c264 1224#endif
29b0f896
AM
1225 {0, 0, 0}
1226};
1227
1228/* For interface with expression (). */
1229extern char *input_line_pointer;
1230
1231/* Hash table for instruction mnemonic lookup. */
629310ab 1232static htab_t op_hash;
29b0f896
AM
1233
1234/* Hash table for register lookup. */
629310ab 1235static htab_t reg_hash;
29b0f896 1236\f
ce8a8b2f
AM
1237 /* Various efficient no-op patterns for aligning code labels.
1238 Note: Don't try to assemble the instructions in the comments.
1239 0L and 0w are not legal. */
62a02d25
L
1240static const unsigned char f32_1[] =
1241 {0x90}; /* nop */
1242static const unsigned char f32_2[] =
1243 {0x66,0x90}; /* xchg %ax,%ax */
1244static const unsigned char f32_3[] =
1245 {0x8d,0x76,0x00}; /* leal 0(%esi),%esi */
1246static const unsigned char f32_4[] =
1247 {0x8d,0x74,0x26,0x00}; /* leal 0(%esi,1),%esi */
62a02d25
L
1248static const unsigned char f32_6[] =
1249 {0x8d,0xb6,0x00,0x00,0x00,0x00}; /* leal 0L(%esi),%esi */
1250static const unsigned char f32_7[] =
1251 {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal 0L(%esi,1),%esi */
62a02d25 1252static const unsigned char f16_3[] =
3ae729d5 1253 {0x8d,0x74,0x00}; /* lea 0(%si),%si */
62a02d25 1254static const unsigned char f16_4[] =
3ae729d5
L
1255 {0x8d,0xb4,0x00,0x00}; /* lea 0W(%si),%si */
1256static const unsigned char jump_disp8[] =
1257 {0xeb}; /* jmp disp8 */
1258static const unsigned char jump32_disp32[] =
1259 {0xe9}; /* jmp disp32 */
1260static const unsigned char jump16_disp32[] =
1261 {0x66,0xe9}; /* jmp disp32 */
62a02d25
L
1262/* 32-bit NOPs patterns. */
1263static const unsigned char *const f32_patt[] = {
3ae729d5 1264 f32_1, f32_2, f32_3, f32_4, NULL, f32_6, f32_7
62a02d25
L
1265};
1266/* 16-bit NOPs patterns. */
1267static const unsigned char *const f16_patt[] = {
3ae729d5 1268 f32_1, f32_2, f16_3, f16_4
62a02d25
L
1269};
1270/* nopl (%[re]ax) */
1271static const unsigned char alt_3[] =
1272 {0x0f,0x1f,0x00};
1273/* nopl 0(%[re]ax) */
1274static const unsigned char alt_4[] =
1275 {0x0f,0x1f,0x40,0x00};
1276/* nopl 0(%[re]ax,%[re]ax,1) */
1277static const unsigned char alt_5[] =
1278 {0x0f,0x1f,0x44,0x00,0x00};
1279/* nopw 0(%[re]ax,%[re]ax,1) */
1280static const unsigned char alt_6[] =
1281 {0x66,0x0f,0x1f,0x44,0x00,0x00};
1282/* nopl 0L(%[re]ax) */
1283static const unsigned char alt_7[] =
1284 {0x0f,0x1f,0x80,0x00,0x00,0x00,0x00};
1285/* nopl 0L(%[re]ax,%[re]ax,1) */
1286static const unsigned char alt_8[] =
1287 {0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1288/* nopw 0L(%[re]ax,%[re]ax,1) */
1289static const unsigned char alt_9[] =
1290 {0x66,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1291/* nopw %cs:0L(%[re]ax,%[re]ax,1) */
1292static const unsigned char alt_10[] =
1293 {0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
3ae729d5
L
1294/* data16 nopw %cs:0L(%eax,%eax,1) */
1295static const unsigned char alt_11[] =
1296 {0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
62a02d25
L
1297/* 32-bit and 64-bit NOPs patterns. */
1298static const unsigned char *const alt_patt[] = {
1299 f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
3ae729d5 1300 alt_9, alt_10, alt_11
62a02d25
L
1301};
1302
1303/* Genenerate COUNT bytes of NOPs to WHERE from PATT with the maximum
1304 size of a single NOP instruction MAX_SINGLE_NOP_SIZE. */
1305
1306static void
1307i386_output_nops (char *where, const unsigned char *const *patt,
1308 int count, int max_single_nop_size)
1309
1310{
3ae729d5
L
1311 /* Place the longer NOP first. */
1312 int last;
1313 int offset;
3076e594
NC
1314 const unsigned char *nops;
1315
1316 if (max_single_nop_size < 1)
1317 {
1318 as_fatal (_("i386_output_nops called to generate nops of at most %d bytes!"),
1319 max_single_nop_size);
1320 return;
1321 }
1322
1323 nops = patt[max_single_nop_size - 1];
3ae729d5
L
1324
1325 /* Use the smaller one if the requsted one isn't available. */
1326 if (nops == NULL)
62a02d25 1327 {
3ae729d5
L
1328 max_single_nop_size--;
1329 nops = patt[max_single_nop_size - 1];
62a02d25
L
1330 }
1331
3ae729d5
L
1332 last = count % max_single_nop_size;
1333
1334 count -= last;
1335 for (offset = 0; offset < count; offset += max_single_nop_size)
1336 memcpy (where + offset, nops, max_single_nop_size);
1337
1338 if (last)
1339 {
1340 nops = patt[last - 1];
1341 if (nops == NULL)
1342 {
1343 /* Use the smaller one plus one-byte NOP if the needed one
1344 isn't available. */
1345 last--;
1346 nops = patt[last - 1];
1347 memcpy (where + offset, nops, last);
1348 where[offset + last] = *patt[0];
1349 }
1350 else
1351 memcpy (where + offset, nops, last);
1352 }
62a02d25
L
1353}
1354
3ae729d5
L
1355static INLINE int
1356fits_in_imm7 (offsetT num)
1357{
1358 return (num & 0x7f) == num;
1359}
1360
1361static INLINE int
1362fits_in_imm31 (offsetT num)
1363{
1364 return (num & 0x7fffffff) == num;
1365}
62a02d25
L
1366
1367/* Genenerate COUNT bytes of NOPs to WHERE with the maximum size of a
1368 single NOP instruction LIMIT. */
1369
1370void
3ae729d5 1371i386_generate_nops (fragS *fragP, char *where, offsetT count, int limit)
62a02d25 1372{
3ae729d5 1373 const unsigned char *const *patt = NULL;
62a02d25 1374 int max_single_nop_size;
3ae729d5
L
1375 /* Maximum number of NOPs before switching to jump over NOPs. */
1376 int max_number_of_nops;
62a02d25 1377
3ae729d5 1378 switch (fragP->fr_type)
62a02d25 1379 {
3ae729d5
L
1380 case rs_fill_nop:
1381 case rs_align_code:
1382 break;
e379e5f3
L
1383 case rs_machine_dependent:
1384 /* Allow NOP padding for jumps and calls. */
1385 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
1386 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
1387 break;
1388 /* Fall through. */
3ae729d5 1389 default:
62a02d25
L
1390 return;
1391 }
1392
ccc9c027
L
1393 /* We need to decide which NOP sequence to use for 32bit and
1394 64bit. When -mtune= is used:
4eed87de 1395
76bc74dc
L
1396 1. For PROCESSOR_I386, PROCESSOR_I486, PROCESSOR_PENTIUM and
1397 PROCESSOR_GENERIC32, f32_patt will be used.
80b8656c
L
1398 2. For the rest, alt_patt will be used.
1399
1400 When -mtune= isn't used, alt_patt will be used if
22109423 1401 cpu_arch_isa_flags has CpuNop. Otherwise, f32_patt will
76bc74dc 1402 be used.
ccc9c027
L
1403
1404 When -march= or .arch is used, we can't use anything beyond
1405 cpu_arch_isa_flags. */
1406
1407 if (flag_code == CODE_16BIT)
1408 {
3ae729d5
L
1409 patt = f16_patt;
1410 max_single_nop_size = sizeof (f16_patt) / sizeof (f16_patt[0]);
1411 /* Limit number of NOPs to 2 in 16-bit mode. */
1412 max_number_of_nops = 2;
252b5132 1413 }
33fef721 1414 else
ccc9c027 1415 {
fbf3f584 1416 if (fragP->tc_frag_data.isa == PROCESSOR_UNKNOWN)
ccc9c027
L
1417 {
1418 /* PROCESSOR_UNKNOWN means that all ISAs may be used. */
1419 switch (cpu_arch_tune)
1420 {
1421 case PROCESSOR_UNKNOWN:
1422 /* We use cpu_arch_isa_flags to check if we SHOULD
22109423
L
1423 optimize with nops. */
1424 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1425 patt = alt_patt;
ccc9c027
L
1426 else
1427 patt = f32_patt;
1428 break;
ccc9c027
L
1429 case PROCESSOR_PENTIUM4:
1430 case PROCESSOR_NOCONA:
ef05d495 1431 case PROCESSOR_CORE:
76bc74dc 1432 case PROCESSOR_CORE2:
bd5295b2 1433 case PROCESSOR_COREI7:
76bc74dc 1434 case PROCESSOR_GENERIC64:
ccc9c027
L
1435 case PROCESSOR_K6:
1436 case PROCESSOR_ATHLON:
1437 case PROCESSOR_K8:
4eed87de 1438 case PROCESSOR_AMDFAM10:
8aedb9fe 1439 case PROCESSOR_BD:
029f3522 1440 case PROCESSOR_ZNVER:
7b458c12 1441 case PROCESSOR_BT:
80b8656c 1442 patt = alt_patt;
ccc9c027 1443 break;
76bc74dc 1444 case PROCESSOR_I386:
ccc9c027
L
1445 case PROCESSOR_I486:
1446 case PROCESSOR_PENTIUM:
2dde1948 1447 case PROCESSOR_PENTIUMPRO:
81486035 1448 case PROCESSOR_IAMCU:
ccc9c027
L
1449 case PROCESSOR_GENERIC32:
1450 patt = f32_patt;
1451 break;
c368d2a8
JB
1452 case PROCESSOR_NONE:
1453 abort ();
4eed87de 1454 }
ccc9c027
L
1455 }
1456 else
1457 {
fbf3f584 1458 switch (fragP->tc_frag_data.tune)
ccc9c027
L
1459 {
1460 case PROCESSOR_UNKNOWN:
e6a14101 1461 /* When cpu_arch_isa is set, cpu_arch_tune shouldn't be
ccc9c027
L
1462 PROCESSOR_UNKNOWN. */
1463 abort ();
1464 break;
1465
76bc74dc 1466 case PROCESSOR_I386:
ccc9c027
L
1467 case PROCESSOR_I486:
1468 case PROCESSOR_PENTIUM:
81486035 1469 case PROCESSOR_IAMCU:
ccc9c027
L
1470 case PROCESSOR_K6:
1471 case PROCESSOR_ATHLON:
1472 case PROCESSOR_K8:
4eed87de 1473 case PROCESSOR_AMDFAM10:
8aedb9fe 1474 case PROCESSOR_BD:
029f3522 1475 case PROCESSOR_ZNVER:
7b458c12 1476 case PROCESSOR_BT:
ccc9c027
L
1477 case PROCESSOR_GENERIC32:
1478 /* We use cpu_arch_isa_flags to check if we CAN optimize
22109423
L
1479 with nops. */
1480 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1481 patt = alt_patt;
ccc9c027
L
1482 else
1483 patt = f32_patt;
1484 break;
76bc74dc
L
1485 case PROCESSOR_PENTIUMPRO:
1486 case PROCESSOR_PENTIUM4:
1487 case PROCESSOR_NOCONA:
1488 case PROCESSOR_CORE:
ef05d495 1489 case PROCESSOR_CORE2:
bd5295b2 1490 case PROCESSOR_COREI7:
22109423 1491 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1492 patt = alt_patt;
ccc9c027
L
1493 else
1494 patt = f32_patt;
1495 break;
1496 case PROCESSOR_GENERIC64:
80b8656c 1497 patt = alt_patt;
ccc9c027 1498 break;
c368d2a8
JB
1499 case PROCESSOR_NONE:
1500 abort ();
4eed87de 1501 }
ccc9c027
L
1502 }
1503
76bc74dc
L
1504 if (patt == f32_patt)
1505 {
3ae729d5
L
1506 max_single_nop_size = sizeof (f32_patt) / sizeof (f32_patt[0]);
1507 /* Limit number of NOPs to 2 for older processors. */
1508 max_number_of_nops = 2;
76bc74dc
L
1509 }
1510 else
1511 {
3ae729d5
L
1512 max_single_nop_size = sizeof (alt_patt) / sizeof (alt_patt[0]);
1513 /* Limit number of NOPs to 7 for newer processors. */
1514 max_number_of_nops = 7;
1515 }
1516 }
1517
1518 if (limit == 0)
1519 limit = max_single_nop_size;
1520
1521 if (fragP->fr_type == rs_fill_nop)
1522 {
1523 /* Output NOPs for .nop directive. */
1524 if (limit > max_single_nop_size)
1525 {
1526 as_bad_where (fragP->fr_file, fragP->fr_line,
1527 _("invalid single nop size: %d "
1528 "(expect within [0, %d])"),
1529 limit, max_single_nop_size);
1530 return;
1531 }
1532 }
e379e5f3 1533 else if (fragP->fr_type != rs_machine_dependent)
3ae729d5
L
1534 fragP->fr_var = count;
1535
1536 if ((count / max_single_nop_size) > max_number_of_nops)
1537 {
1538 /* Generate jump over NOPs. */
1539 offsetT disp = count - 2;
1540 if (fits_in_imm7 (disp))
1541 {
1542 /* Use "jmp disp8" if possible. */
1543 count = disp;
1544 where[0] = jump_disp8[0];
1545 where[1] = count;
1546 where += 2;
1547 }
1548 else
1549 {
1550 unsigned int size_of_jump;
1551
1552 if (flag_code == CODE_16BIT)
1553 {
1554 where[0] = jump16_disp32[0];
1555 where[1] = jump16_disp32[1];
1556 size_of_jump = 2;
1557 }
1558 else
1559 {
1560 where[0] = jump32_disp32[0];
1561 size_of_jump = 1;
1562 }
1563
1564 count -= size_of_jump + 4;
1565 if (!fits_in_imm31 (count))
1566 {
1567 as_bad_where (fragP->fr_file, fragP->fr_line,
1568 _("jump over nop padding out of range"));
1569 return;
1570 }
1571
1572 md_number_to_chars (where + size_of_jump, count, 4);
1573 where += size_of_jump + 4;
76bc74dc 1574 }
ccc9c027 1575 }
3ae729d5
L
1576
1577 /* Generate multiple NOPs. */
1578 i386_output_nops (where, patt, count, limit);
252b5132
RH
1579}
1580
c6fb90c8 1581static INLINE int
0dfbf9d7 1582operand_type_all_zero (const union i386_operand_type *x)
40fb9820 1583{
0dfbf9d7 1584 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1585 {
1586 case 3:
0dfbf9d7 1587 if (x->array[2])
c6fb90c8 1588 return 0;
1a0670f3 1589 /* Fall through. */
c6fb90c8 1590 case 2:
0dfbf9d7 1591 if (x->array[1])
c6fb90c8 1592 return 0;
1a0670f3 1593 /* Fall through. */
c6fb90c8 1594 case 1:
0dfbf9d7 1595 return !x->array[0];
c6fb90c8
L
1596 default:
1597 abort ();
1598 }
40fb9820
L
1599}
1600
c6fb90c8 1601static INLINE void
0dfbf9d7 1602operand_type_set (union i386_operand_type *x, unsigned int v)
40fb9820 1603{
0dfbf9d7 1604 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1605 {
1606 case 3:
0dfbf9d7 1607 x->array[2] = v;
1a0670f3 1608 /* Fall through. */
c6fb90c8 1609 case 2:
0dfbf9d7 1610 x->array[1] = v;
1a0670f3 1611 /* Fall through. */
c6fb90c8 1612 case 1:
0dfbf9d7 1613 x->array[0] = v;
1a0670f3 1614 /* Fall through. */
c6fb90c8
L
1615 break;
1616 default:
1617 abort ();
1618 }
bab6aec1
JB
1619
1620 x->bitfield.class = ClassNone;
75e5731b 1621 x->bitfield.instance = InstanceNone;
c6fb90c8 1622}
40fb9820 1623
c6fb90c8 1624static INLINE int
0dfbf9d7
L
1625operand_type_equal (const union i386_operand_type *x,
1626 const union i386_operand_type *y)
c6fb90c8 1627{
0dfbf9d7 1628 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1629 {
1630 case 3:
0dfbf9d7 1631 if (x->array[2] != y->array[2])
c6fb90c8 1632 return 0;
1a0670f3 1633 /* Fall through. */
c6fb90c8 1634 case 2:
0dfbf9d7 1635 if (x->array[1] != y->array[1])
c6fb90c8 1636 return 0;
1a0670f3 1637 /* Fall through. */
c6fb90c8 1638 case 1:
0dfbf9d7 1639 return x->array[0] == y->array[0];
c6fb90c8
L
1640 break;
1641 default:
1642 abort ();
1643 }
1644}
40fb9820 1645
0dfbf9d7
L
1646static INLINE int
1647cpu_flags_all_zero (const union i386_cpu_flags *x)
1648{
1649 switch (ARRAY_SIZE(x->array))
1650 {
75f8266a
KL
1651 case 5:
1652 if (x->array[4])
1653 return 0;
1654 /* Fall through. */
53467f57
IT
1655 case 4:
1656 if (x->array[3])
1657 return 0;
1658 /* Fall through. */
0dfbf9d7
L
1659 case 3:
1660 if (x->array[2])
1661 return 0;
1a0670f3 1662 /* Fall through. */
0dfbf9d7
L
1663 case 2:
1664 if (x->array[1])
1665 return 0;
1a0670f3 1666 /* Fall through. */
0dfbf9d7
L
1667 case 1:
1668 return !x->array[0];
1669 default:
1670 abort ();
1671 }
1672}
1673
0dfbf9d7
L
1674static INLINE int
1675cpu_flags_equal (const union i386_cpu_flags *x,
1676 const union i386_cpu_flags *y)
1677{
1678 switch (ARRAY_SIZE(x->array))
1679 {
75f8266a
KL
1680 case 5:
1681 if (x->array[4] != y->array[4])
1682 return 0;
1683 /* Fall through. */
53467f57
IT
1684 case 4:
1685 if (x->array[3] != y->array[3])
1686 return 0;
1687 /* Fall through. */
0dfbf9d7
L
1688 case 3:
1689 if (x->array[2] != y->array[2])
1690 return 0;
1a0670f3 1691 /* Fall through. */
0dfbf9d7
L
1692 case 2:
1693 if (x->array[1] != y->array[1])
1694 return 0;
1a0670f3 1695 /* Fall through. */
0dfbf9d7
L
1696 case 1:
1697 return x->array[0] == y->array[0];
1698 break;
1699 default:
1700 abort ();
1701 }
1702}
c6fb90c8
L
1703
1704static INLINE int
1705cpu_flags_check_cpu64 (i386_cpu_flags f)
1706{
1707 return !((flag_code == CODE_64BIT && f.bitfield.cpuno64)
1708 || (flag_code != CODE_64BIT && f.bitfield.cpu64));
40fb9820
L
1709}
1710
c6fb90c8
L
1711static INLINE i386_cpu_flags
1712cpu_flags_and (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1713{
c6fb90c8
L
1714 switch (ARRAY_SIZE (x.array))
1715 {
75f8266a
KL
1716 case 5:
1717 x.array [4] &= y.array [4];
1718 /* Fall through. */
53467f57
IT
1719 case 4:
1720 x.array [3] &= y.array [3];
1721 /* Fall through. */
c6fb90c8
L
1722 case 3:
1723 x.array [2] &= y.array [2];
1a0670f3 1724 /* Fall through. */
c6fb90c8
L
1725 case 2:
1726 x.array [1] &= y.array [1];
1a0670f3 1727 /* Fall through. */
c6fb90c8
L
1728 case 1:
1729 x.array [0] &= y.array [0];
1730 break;
1731 default:
1732 abort ();
1733 }
1734 return x;
1735}
40fb9820 1736
c6fb90c8
L
1737static INLINE i386_cpu_flags
1738cpu_flags_or (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1739{
c6fb90c8 1740 switch (ARRAY_SIZE (x.array))
40fb9820 1741 {
75f8266a
KL
1742 case 5:
1743 x.array [4] |= y.array [4];
1744 /* Fall through. */
53467f57
IT
1745 case 4:
1746 x.array [3] |= y.array [3];
1747 /* Fall through. */
c6fb90c8
L
1748 case 3:
1749 x.array [2] |= y.array [2];
1a0670f3 1750 /* Fall through. */
c6fb90c8
L
1751 case 2:
1752 x.array [1] |= y.array [1];
1a0670f3 1753 /* Fall through. */
c6fb90c8
L
1754 case 1:
1755 x.array [0] |= y.array [0];
40fb9820
L
1756 break;
1757 default:
1758 abort ();
1759 }
40fb9820
L
1760 return x;
1761}
1762
309d3373
JB
1763static INLINE i386_cpu_flags
1764cpu_flags_and_not (i386_cpu_flags x, i386_cpu_flags y)
1765{
1766 switch (ARRAY_SIZE (x.array))
1767 {
75f8266a
KL
1768 case 5:
1769 x.array [4] &= ~y.array [4];
1770 /* Fall through. */
53467f57
IT
1771 case 4:
1772 x.array [3] &= ~y.array [3];
1773 /* Fall through. */
309d3373
JB
1774 case 3:
1775 x.array [2] &= ~y.array [2];
1a0670f3 1776 /* Fall through. */
309d3373
JB
1777 case 2:
1778 x.array [1] &= ~y.array [1];
1a0670f3 1779 /* Fall through. */
309d3373
JB
1780 case 1:
1781 x.array [0] &= ~y.array [0];
1782 break;
1783 default:
1784 abort ();
1785 }
1786 return x;
1787}
1788
6c0946d0
JB
1789static const i386_cpu_flags avx512 = CPU_ANY_AVX512F_FLAGS;
1790
c0f3af97
L
1791#define CPU_FLAGS_ARCH_MATCH 0x1
1792#define CPU_FLAGS_64BIT_MATCH 0x2
1793
c0f3af97 1794#define CPU_FLAGS_PERFECT_MATCH \
db12e14e 1795 (CPU_FLAGS_ARCH_MATCH | CPU_FLAGS_64BIT_MATCH)
c0f3af97
L
1796
1797/* Return CPU flags match bits. */
3629bb00 1798
40fb9820 1799static int
d3ce72d0 1800cpu_flags_match (const insn_template *t)
40fb9820 1801{
c0f3af97
L
1802 i386_cpu_flags x = t->cpu_flags;
1803 int match = cpu_flags_check_cpu64 (x) ? CPU_FLAGS_64BIT_MATCH : 0;
40fb9820
L
1804
1805 x.bitfield.cpu64 = 0;
1806 x.bitfield.cpuno64 = 0;
1807
0dfbf9d7 1808 if (cpu_flags_all_zero (&x))
c0f3af97
L
1809 {
1810 /* This instruction is available on all archs. */
db12e14e 1811 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 1812 }
3629bb00
L
1813 else
1814 {
c0f3af97 1815 /* This instruction is available only on some archs. */
3629bb00
L
1816 i386_cpu_flags cpu = cpu_arch_flags;
1817
ab592e75
JB
1818 /* AVX512VL is no standalone feature - match it and then strip it. */
1819 if (x.bitfield.cpuavx512vl && !cpu.bitfield.cpuavx512vl)
1820 return match;
1821 x.bitfield.cpuavx512vl = 0;
1822
22c36940
JB
1823 /* AVX and AVX2 present at the same time express an operand size
1824 dependency - strip AVX2 for the purposes here. The operand size
1825 dependent check occurs in check_vecOperands(). */
1826 if (x.bitfield.cpuavx && x.bitfield.cpuavx2)
1827 x.bitfield.cpuavx2 = 0;
1828
3629bb00 1829 cpu = cpu_flags_and (x, cpu);
c0f3af97
L
1830 if (!cpu_flags_all_zero (&cpu))
1831 {
57392598 1832 if (x.bitfield.cpuavx)
a5ff0eb2 1833 {
929f69fa 1834 /* We need to check a few extra flags with AVX. */
b9d49817 1835 if (cpu.bitfield.cpuavx
40d231b4
JB
1836 && (!t->opcode_modifier.sse2avx
1837 || (sse2avx && !i.prefix[DATA_PREFIX]))
b9d49817 1838 && (!x.bitfield.cpuaes || cpu.bitfield.cpuaes)
929f69fa 1839 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
b9d49817
JB
1840 && (!x.bitfield.cpupclmul || cpu.bitfield.cpupclmul))
1841 match |= CPU_FLAGS_ARCH_MATCH;
a5ff0eb2 1842 }
929f69fa
JB
1843 else if (x.bitfield.cpuavx512f)
1844 {
1845 /* We need to check a few extra flags with AVX512F. */
1846 if (cpu.bitfield.cpuavx512f
1847 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
1848 && (!x.bitfield.cpuvaes || cpu.bitfield.cpuvaes)
1849 && (!x.bitfield.cpuvpclmulqdq || cpu.bitfield.cpuvpclmulqdq))
1850 match |= CPU_FLAGS_ARCH_MATCH;
1851 }
a5ff0eb2 1852 else
db12e14e 1853 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 1854 }
3629bb00 1855 }
c0f3af97 1856 return match;
40fb9820
L
1857}
1858
c6fb90c8
L
1859static INLINE i386_operand_type
1860operand_type_and (i386_operand_type x, i386_operand_type y)
40fb9820 1861{
bab6aec1
JB
1862 if (x.bitfield.class != y.bitfield.class)
1863 x.bitfield.class = ClassNone;
75e5731b
JB
1864 if (x.bitfield.instance != y.bitfield.instance)
1865 x.bitfield.instance = InstanceNone;
bab6aec1 1866
c6fb90c8
L
1867 switch (ARRAY_SIZE (x.array))
1868 {
1869 case 3:
1870 x.array [2] &= y.array [2];
1a0670f3 1871 /* Fall through. */
c6fb90c8
L
1872 case 2:
1873 x.array [1] &= y.array [1];
1a0670f3 1874 /* Fall through. */
c6fb90c8
L
1875 case 1:
1876 x.array [0] &= y.array [0];
1877 break;
1878 default:
1879 abort ();
1880 }
1881 return x;
40fb9820
L
1882}
1883
73053c1f
JB
1884static INLINE i386_operand_type
1885operand_type_and_not (i386_operand_type x, i386_operand_type y)
1886{
bab6aec1 1887 gas_assert (y.bitfield.class == ClassNone);
75e5731b 1888 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 1889
73053c1f
JB
1890 switch (ARRAY_SIZE (x.array))
1891 {
1892 case 3:
1893 x.array [2] &= ~y.array [2];
1894 /* Fall through. */
1895 case 2:
1896 x.array [1] &= ~y.array [1];
1897 /* Fall through. */
1898 case 1:
1899 x.array [0] &= ~y.array [0];
1900 break;
1901 default:
1902 abort ();
1903 }
1904 return x;
1905}
1906
c6fb90c8
L
1907static INLINE i386_operand_type
1908operand_type_or (i386_operand_type x, i386_operand_type y)
40fb9820 1909{
bab6aec1
JB
1910 gas_assert (x.bitfield.class == ClassNone ||
1911 y.bitfield.class == ClassNone ||
1912 x.bitfield.class == y.bitfield.class);
75e5731b
JB
1913 gas_assert (x.bitfield.instance == InstanceNone ||
1914 y.bitfield.instance == InstanceNone ||
1915 x.bitfield.instance == y.bitfield.instance);
bab6aec1 1916
c6fb90c8 1917 switch (ARRAY_SIZE (x.array))
40fb9820 1918 {
c6fb90c8
L
1919 case 3:
1920 x.array [2] |= y.array [2];
1a0670f3 1921 /* Fall through. */
c6fb90c8
L
1922 case 2:
1923 x.array [1] |= y.array [1];
1a0670f3 1924 /* Fall through. */
c6fb90c8
L
1925 case 1:
1926 x.array [0] |= y.array [0];
40fb9820
L
1927 break;
1928 default:
1929 abort ();
1930 }
c6fb90c8
L
1931 return x;
1932}
40fb9820 1933
c6fb90c8
L
1934static INLINE i386_operand_type
1935operand_type_xor (i386_operand_type x, i386_operand_type y)
1936{
bab6aec1 1937 gas_assert (y.bitfield.class == ClassNone);
75e5731b 1938 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 1939
c6fb90c8
L
1940 switch (ARRAY_SIZE (x.array))
1941 {
1942 case 3:
1943 x.array [2] ^= y.array [2];
1a0670f3 1944 /* Fall through. */
c6fb90c8
L
1945 case 2:
1946 x.array [1] ^= y.array [1];
1a0670f3 1947 /* Fall through. */
c6fb90c8
L
1948 case 1:
1949 x.array [0] ^= y.array [0];
1950 break;
1951 default:
1952 abort ();
1953 }
40fb9820
L
1954 return x;
1955}
1956
05909f23
JB
1957static const i386_operand_type anydisp = {
1958 .bitfield = { .disp8 = 1, .disp16 = 1, .disp32 = 1, .disp64 = 1 }
1959};
40fb9820
L
1960
1961enum operand_type
1962{
1963 reg,
40fb9820
L
1964 imm,
1965 disp,
1966 anymem
1967};
1968
c6fb90c8 1969static INLINE int
40fb9820
L
1970operand_type_check (i386_operand_type t, enum operand_type c)
1971{
1972 switch (c)
1973 {
1974 case reg:
bab6aec1 1975 return t.bitfield.class == Reg;
40fb9820 1976
40fb9820
L
1977 case imm:
1978 return (t.bitfield.imm8
1979 || t.bitfield.imm8s
1980 || t.bitfield.imm16
1981 || t.bitfield.imm32
1982 || t.bitfield.imm32s
1983 || t.bitfield.imm64);
1984
1985 case disp:
1986 return (t.bitfield.disp8
1987 || t.bitfield.disp16
1988 || t.bitfield.disp32
40fb9820
L
1989 || t.bitfield.disp64);
1990
1991 case anymem:
1992 return (t.bitfield.disp8
1993 || t.bitfield.disp16
1994 || t.bitfield.disp32
40fb9820
L
1995 || t.bitfield.disp64
1996 || t.bitfield.baseindex);
1997
1998 default:
1999 abort ();
2000 }
2cfe26b6
AM
2001
2002 return 0;
40fb9820
L
2003}
2004
7a54636a
L
2005/* Return 1 if there is no conflict in 8bit/16bit/32bit/64bit/80bit size
2006 between operand GIVEN and opeand WANTED for instruction template T. */
5c07affc
L
2007
2008static INLINE int
7a54636a
L
2009match_operand_size (const insn_template *t, unsigned int wanted,
2010 unsigned int given)
5c07affc 2011{
3ac21baa
JB
2012 return !((i.types[given].bitfield.byte
2013 && !t->operand_types[wanted].bitfield.byte)
2014 || (i.types[given].bitfield.word
2015 && !t->operand_types[wanted].bitfield.word)
2016 || (i.types[given].bitfield.dword
2017 && !t->operand_types[wanted].bitfield.dword)
2018 || (i.types[given].bitfield.qword
9db83a32
JB
2019 && (!t->operand_types[wanted].bitfield.qword
2020 /* Don't allow 64-bit (memory) operands outside of 64-bit
2021 mode, when they're used where a 64-bit GPR could also
2022 be used. Checking is needed for Intel Syntax only. */
2023 || (intel_syntax
2024 && flag_code != CODE_64BIT
2025 && (t->operand_types[wanted].bitfield.class == Reg
2026 || t->operand_types[wanted].bitfield.class == Accum
2027 || t->opcode_modifier.isstring))))
3ac21baa
JB
2028 || (i.types[given].bitfield.tbyte
2029 && !t->operand_types[wanted].bitfield.tbyte));
5c07affc
L
2030}
2031
dd40ce22
L
2032/* Return 1 if there is no conflict in SIMD register between operand
2033 GIVEN and opeand WANTED for instruction template T. */
1b54b8d7
JB
2034
2035static INLINE int
dd40ce22
L
2036match_simd_size (const insn_template *t, unsigned int wanted,
2037 unsigned int given)
1b54b8d7 2038{
3ac21baa
JB
2039 return !((i.types[given].bitfield.xmmword
2040 && !t->operand_types[wanted].bitfield.xmmword)
2041 || (i.types[given].bitfield.ymmword
2042 && !t->operand_types[wanted].bitfield.ymmword)
2043 || (i.types[given].bitfield.zmmword
260cd341
LC
2044 && !t->operand_types[wanted].bitfield.zmmword)
2045 || (i.types[given].bitfield.tmmword
2046 && !t->operand_types[wanted].bitfield.tmmword));
1b54b8d7
JB
2047}
2048
7a54636a
L
2049/* Return 1 if there is no conflict in any size between operand GIVEN
2050 and opeand WANTED for instruction template T. */
5c07affc
L
2051
2052static INLINE int
dd40ce22
L
2053match_mem_size (const insn_template *t, unsigned int wanted,
2054 unsigned int given)
5c07affc 2055{
7a54636a 2056 return (match_operand_size (t, wanted, given)
3ac21baa 2057 && !((i.types[given].bitfield.unspecified
5273a3cd 2058 && !i.broadcast.type
a5748e0d 2059 && !i.broadcast.bytes
3ac21baa
JB
2060 && !t->operand_types[wanted].bitfield.unspecified)
2061 || (i.types[given].bitfield.fword
2062 && !t->operand_types[wanted].bitfield.fword)
1b54b8d7
JB
2063 /* For scalar opcode templates to allow register and memory
2064 operands at the same time, some special casing is needed
d6793fa1
JB
2065 here. Also for v{,p}broadcast*, {,v}pmov{s,z}*, and
2066 down-conversion vpmov*. */
3528c362 2067 || ((t->operand_types[wanted].bitfield.class == RegSIMD
bc49bfd8
JB
2068 && t->operand_types[wanted].bitfield.byte
2069 + t->operand_types[wanted].bitfield.word
2070 + t->operand_types[wanted].bitfield.dword
2071 + t->operand_types[wanted].bitfield.qword
2072 > !!t->opcode_modifier.broadcast)
3ac21baa
JB
2073 ? (i.types[given].bitfield.xmmword
2074 || i.types[given].bitfield.ymmword
2075 || i.types[given].bitfield.zmmword)
2076 : !match_simd_size(t, wanted, given))));
5c07affc
L
2077}
2078
3ac21baa
JB
2079/* Return value has MATCH_STRAIGHT set if there is no size conflict on any
2080 operands for instruction template T, and it has MATCH_REVERSE set if there
2081 is no size conflict on any operands for the template with operands reversed
2082 (and the template allows for reversing in the first place). */
5c07affc 2083
3ac21baa
JB
2084#define MATCH_STRAIGHT 1
2085#define MATCH_REVERSE 2
2086
2087static INLINE unsigned int
d3ce72d0 2088operand_size_match (const insn_template *t)
5c07affc 2089{
3ac21baa 2090 unsigned int j, match = MATCH_STRAIGHT;
5c07affc 2091
0cfa3eb3 2092 /* Don't check non-absolute jump instructions. */
5c07affc 2093 if (t->opcode_modifier.jump
0cfa3eb3 2094 && t->opcode_modifier.jump != JUMP_ABSOLUTE)
5c07affc
L
2095 return match;
2096
2097 /* Check memory and accumulator operand size. */
2098 for (j = 0; j < i.operands; j++)
2099 {
3528c362
JB
2100 if (i.types[j].bitfield.class != Reg
2101 && i.types[j].bitfield.class != RegSIMD
255571cd 2102 && t->opcode_modifier.operandconstraint == ANY_SIZE)
5c07affc
L
2103 continue;
2104
bab6aec1 2105 if (t->operand_types[j].bitfield.class == Reg
7a54636a 2106 && !match_operand_size (t, j, j))
5c07affc
L
2107 {
2108 match = 0;
2109 break;
2110 }
2111
3528c362 2112 if (t->operand_types[j].bitfield.class == RegSIMD
3ac21baa 2113 && !match_simd_size (t, j, j))
1b54b8d7
JB
2114 {
2115 match = 0;
2116 break;
2117 }
2118
75e5731b 2119 if (t->operand_types[j].bitfield.instance == Accum
7a54636a 2120 && (!match_operand_size (t, j, j) || !match_simd_size (t, j, j)))
1b54b8d7
JB
2121 {
2122 match = 0;
2123 break;
2124 }
2125
c48dadc9 2126 if ((i.flags[j] & Operand_Mem) && !match_mem_size (t, j, j))
5c07affc
L
2127 {
2128 match = 0;
2129 break;
2130 }
2131 }
2132
3ac21baa 2133 if (!t->opcode_modifier.d)
7b94647a 2134 return match;
5c07affc
L
2135
2136 /* Check reverse. */
8bd915b7
JB
2137 gas_assert ((i.operands >= 2 && i.operands <= 3)
2138 || t->opcode_modifier.vexsources);
5c07affc 2139
f5eb1d70 2140 for (j = 0; j < i.operands; j++)
5c07affc 2141 {
f5eb1d70
JB
2142 unsigned int given = i.operands - j - 1;
2143
8bd915b7
JB
2144 /* For 4- and 5-operand insns VEX.W controls just the first two
2145 register operands. */
2146 if (t->opcode_modifier.vexsources)
2147 given = j < 2 ? 1 - j : j;
2148
bab6aec1 2149 if (t->operand_types[j].bitfield.class == Reg
f5eb1d70 2150 && !match_operand_size (t, j, given))
7b94647a 2151 return match;
5c07affc 2152
3528c362 2153 if (t->operand_types[j].bitfield.class == RegSIMD
f5eb1d70 2154 && !match_simd_size (t, j, given))
7b94647a 2155 return match;
dbbc8b7e 2156
75e5731b 2157 if (t->operand_types[j].bitfield.instance == Accum
f5eb1d70
JB
2158 && (!match_operand_size (t, j, given)
2159 || !match_simd_size (t, j, given)))
7b94647a 2160 return match;
dbbc8b7e 2161
f5eb1d70 2162 if ((i.flags[given] & Operand_Mem) && !match_mem_size (t, j, given))
7b94647a 2163 return match;
5c07affc
L
2164 }
2165
3ac21baa 2166 return match | MATCH_REVERSE;
5c07affc
L
2167}
2168
c6fb90c8 2169static INLINE int
40fb9820
L
2170operand_type_match (i386_operand_type overlap,
2171 i386_operand_type given)
2172{
2173 i386_operand_type temp = overlap;
2174
7d5e4556 2175 temp.bitfield.unspecified = 0;
5c07affc
L
2176 temp.bitfield.byte = 0;
2177 temp.bitfield.word = 0;
2178 temp.bitfield.dword = 0;
2179 temp.bitfield.fword = 0;
2180 temp.bitfield.qword = 0;
2181 temp.bitfield.tbyte = 0;
2182 temp.bitfield.xmmword = 0;
c0f3af97 2183 temp.bitfield.ymmword = 0;
43234a1e 2184 temp.bitfield.zmmword = 0;
260cd341 2185 temp.bitfield.tmmword = 0;
0dfbf9d7 2186 if (operand_type_all_zero (&temp))
891edac4 2187 goto mismatch;
40fb9820 2188
6f2f06be 2189 if (given.bitfield.baseindex == overlap.bitfield.baseindex)
891edac4
L
2190 return 1;
2191
dc1e8a47 2192 mismatch:
a65babc9 2193 i.error = operand_type_mismatch;
891edac4 2194 return 0;
40fb9820
L
2195}
2196
7d5e4556 2197/* If given types g0 and g1 are registers they must be of the same type
10c17abd 2198 unless the expected operand type register overlap is null.
8ee52bcf 2199 Intel syntax sized memory operands are also checked here. */
40fb9820 2200
c6fb90c8 2201static INLINE int
dc821c5f 2202operand_type_register_match (i386_operand_type g0,
40fb9820 2203 i386_operand_type t0,
40fb9820
L
2204 i386_operand_type g1,
2205 i386_operand_type t1)
2206{
bab6aec1 2207 if (g0.bitfield.class != Reg
3528c362 2208 && g0.bitfield.class != RegSIMD
8ee52bcf
JB
2209 && (g0.bitfield.unspecified
2210 || !operand_type_check (g0, anymem)))
40fb9820
L
2211 return 1;
2212
bab6aec1 2213 if (g1.bitfield.class != Reg
3528c362 2214 && g1.bitfield.class != RegSIMD
8ee52bcf
JB
2215 && (g1.bitfield.unspecified
2216 || !operand_type_check (g1, anymem)))
40fb9820
L
2217 return 1;
2218
dc821c5f
JB
2219 if (g0.bitfield.byte == g1.bitfield.byte
2220 && g0.bitfield.word == g1.bitfield.word
2221 && g0.bitfield.dword == g1.bitfield.dword
10c17abd
JB
2222 && g0.bitfield.qword == g1.bitfield.qword
2223 && g0.bitfield.xmmword == g1.bitfield.xmmword
2224 && g0.bitfield.ymmword == g1.bitfield.ymmword
2225 && g0.bitfield.zmmword == g1.bitfield.zmmword)
40fb9820
L
2226 return 1;
2227
c4d09633
JB
2228 /* If expectations overlap in no more than a single size, all is fine. */
2229 g0 = operand_type_and (t0, t1);
2230 if (g0.bitfield.byte
2231 + g0.bitfield.word
2232 + g0.bitfield.dword
2233 + g0.bitfield.qword
2234 + g0.bitfield.xmmword
2235 + g0.bitfield.ymmword
2236 + g0.bitfield.zmmword <= 1)
891edac4
L
2237 return 1;
2238
a65babc9 2239 i.error = register_type_mismatch;
891edac4
L
2240
2241 return 0;
40fb9820
L
2242}
2243
4c692bc7
JB
2244static INLINE unsigned int
2245register_number (const reg_entry *r)
2246{
2247 unsigned int nr = r->reg_num;
2248
2249 if (r->reg_flags & RegRex)
2250 nr += 8;
2251
200cbe0f
L
2252 if (r->reg_flags & RegVRex)
2253 nr += 16;
2254
4c692bc7
JB
2255 return nr;
2256}
2257
252b5132 2258static INLINE unsigned int
40fb9820 2259mode_from_disp_size (i386_operand_type t)
252b5132 2260{
b5014f7a 2261 if (t.bitfield.disp8)
40fb9820
L
2262 return 1;
2263 else if (t.bitfield.disp16
a775efc8 2264 || t.bitfield.disp32)
40fb9820
L
2265 return 2;
2266 else
2267 return 0;
252b5132
RH
2268}
2269
2270static INLINE int
65879393 2271fits_in_signed_byte (addressT num)
252b5132 2272{
65879393 2273 return num + 0x80 <= 0xff;
47926f60 2274}
252b5132
RH
2275
2276static INLINE int
65879393 2277fits_in_unsigned_byte (addressT num)
252b5132 2278{
65879393 2279 return num <= 0xff;
47926f60 2280}
252b5132
RH
2281
2282static INLINE int
65879393 2283fits_in_unsigned_word (addressT num)
252b5132 2284{
65879393 2285 return num <= 0xffff;
47926f60 2286}
252b5132
RH
2287
2288static INLINE int
65879393 2289fits_in_signed_word (addressT num)
252b5132 2290{
65879393 2291 return num + 0x8000 <= 0xffff;
47926f60 2292}
2a962e6d 2293
3e73aa7c 2294static INLINE int
65879393 2295fits_in_signed_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2296{
2297#ifndef BFD64
2298 return 1;
2299#else
65879393 2300 return num + 0x80000000 <= 0xffffffff;
3e73aa7c
JH
2301#endif
2302} /* fits_in_signed_long() */
2a962e6d 2303
3e73aa7c 2304static INLINE int
65879393 2305fits_in_unsigned_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2306{
2307#ifndef BFD64
2308 return 1;
2309#else
65879393 2310 return num <= 0xffffffff;
3e73aa7c
JH
2311#endif
2312} /* fits_in_unsigned_long() */
252b5132 2313
a442cac5
JB
2314static INLINE valueT extend_to_32bit_address (addressT num)
2315{
2316#ifdef BFD64
2317 if (fits_in_unsigned_long(num))
2318 return (num ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
2319
2320 if (!fits_in_signed_long (num))
2321 return num & 0xffffffff;
2322#endif
2323
2324 return num;
2325}
2326
43234a1e 2327static INLINE int
b5014f7a 2328fits_in_disp8 (offsetT num)
43234a1e
L
2329{
2330 int shift = i.memshift;
2331 unsigned int mask;
2332
2333 if (shift == -1)
2334 abort ();
2335
2336 mask = (1 << shift) - 1;
2337
2338 /* Return 0 if NUM isn't properly aligned. */
2339 if ((num & mask))
2340 return 0;
2341
2342 /* Check if NUM will fit in 8bit after shift. */
2343 return fits_in_signed_byte (num >> shift);
2344}
2345
a683cc34
SP
2346static INLINE int
2347fits_in_imm4 (offsetT num)
2348{
2349 return (num & 0xf) == num;
2350}
2351
40fb9820 2352static i386_operand_type
e3bb37b5 2353smallest_imm_type (offsetT num)
252b5132 2354{
40fb9820 2355 i386_operand_type t;
7ab9ffdd 2356
0dfbf9d7 2357 operand_type_set (&t, 0);
40fb9820
L
2358 t.bitfield.imm64 = 1;
2359
2360 if (cpu_arch_tune != PROCESSOR_I486 && num == 1)
e413e4e9
AM
2361 {
2362 /* This code is disabled on the 486 because all the Imm1 forms
2363 in the opcode table are slower on the i486. They're the
2364 versions with the implicitly specified single-position
2365 displacement, which has another syntax if you really want to
2366 use that form. */
40fb9820
L
2367 t.bitfield.imm1 = 1;
2368 t.bitfield.imm8 = 1;
2369 t.bitfield.imm8s = 1;
2370 t.bitfield.imm16 = 1;
2371 t.bitfield.imm32 = 1;
2372 t.bitfield.imm32s = 1;
2373 }
2374 else if (fits_in_signed_byte (num))
2375 {
2376 t.bitfield.imm8 = 1;
2377 t.bitfield.imm8s = 1;
2378 t.bitfield.imm16 = 1;
2379 t.bitfield.imm32 = 1;
2380 t.bitfield.imm32s = 1;
2381 }
2382 else if (fits_in_unsigned_byte (num))
2383 {
2384 t.bitfield.imm8 = 1;
2385 t.bitfield.imm16 = 1;
2386 t.bitfield.imm32 = 1;
2387 t.bitfield.imm32s = 1;
2388 }
2389 else if (fits_in_signed_word (num) || fits_in_unsigned_word (num))
2390 {
2391 t.bitfield.imm16 = 1;
2392 t.bitfield.imm32 = 1;
2393 t.bitfield.imm32s = 1;
2394 }
2395 else if (fits_in_signed_long (num))
2396 {
2397 t.bitfield.imm32 = 1;
2398 t.bitfield.imm32s = 1;
2399 }
2400 else if (fits_in_unsigned_long (num))
2401 t.bitfield.imm32 = 1;
2402
2403 return t;
47926f60 2404}
252b5132 2405
847f7ad4 2406static offsetT
e3bb37b5 2407offset_in_range (offsetT val, int size)
847f7ad4 2408{
508866be 2409 addressT mask;
ba2adb93 2410
847f7ad4
AM
2411 switch (size)
2412 {
508866be
L
2413 case 1: mask = ((addressT) 1 << 8) - 1; break;
2414 case 2: mask = ((addressT) 1 << 16) - 1; break;
3e73aa7c 2415#ifdef BFD64
64965897 2416 case 4: mask = ((addressT) 1 << 32) - 1; break;
3e73aa7c 2417#endif
64965897 2418 case sizeof (val): return val;
47926f60 2419 default: abort ();
847f7ad4
AM
2420 }
2421
4fe51f7d 2422 if ((val & ~mask) != 0 && (-val & ~mask) != 0)
f493c217
AM
2423 as_warn (_("0x%" PRIx64 " shortened to 0x%" PRIx64),
2424 (uint64_t) val, (uint64_t) (val & mask));
847f7ad4 2425
847f7ad4
AM
2426 return val & mask;
2427}
2428
c32fa91d
L
2429enum PREFIX_GROUP
2430{
2431 PREFIX_EXIST = 0,
2432 PREFIX_LOCK,
2433 PREFIX_REP,
04ef582a 2434 PREFIX_DS,
c32fa91d
L
2435 PREFIX_OTHER
2436};
2437
2438/* Returns
2439 a. PREFIX_EXIST if attempting to add a prefix where one from the
2440 same class already exists.
2441 b. PREFIX_LOCK if lock prefix is added.
2442 c. PREFIX_REP if rep/repne prefix is added.
04ef582a
L
2443 d. PREFIX_DS if ds prefix is added.
2444 e. PREFIX_OTHER if other prefix is added.
c32fa91d
L
2445 */
2446
2447static enum PREFIX_GROUP
e3bb37b5 2448add_prefix (unsigned int prefix)
252b5132 2449{
c32fa91d 2450 enum PREFIX_GROUP ret = PREFIX_OTHER;
b1905489 2451 unsigned int q;
252b5132 2452
29b0f896
AM
2453 if (prefix >= REX_OPCODE && prefix < REX_OPCODE + 16
2454 && flag_code == CODE_64BIT)
b1905489 2455 {
161a04f6 2456 if ((i.prefix[REX_PREFIX] & prefix & REX_W)
44846f29
JB
2457 || (i.prefix[REX_PREFIX] & prefix & REX_R)
2458 || (i.prefix[REX_PREFIX] & prefix & REX_X)
2459 || (i.prefix[REX_PREFIX] & prefix & REX_B))
c32fa91d 2460 ret = PREFIX_EXIST;
b1905489
JB
2461 q = REX_PREFIX;
2462 }
3e73aa7c 2463 else
b1905489
JB
2464 {
2465 switch (prefix)
2466 {
2467 default:
2468 abort ();
2469
b1905489 2470 case DS_PREFIX_OPCODE:
04ef582a
L
2471 ret = PREFIX_DS;
2472 /* Fall through. */
2473 case CS_PREFIX_OPCODE:
b1905489
JB
2474 case ES_PREFIX_OPCODE:
2475 case FS_PREFIX_OPCODE:
2476 case GS_PREFIX_OPCODE:
2477 case SS_PREFIX_OPCODE:
2478 q = SEG_PREFIX;
2479 break;
2480
2481 case REPNE_PREFIX_OPCODE:
2482 case REPE_PREFIX_OPCODE:
c32fa91d
L
2483 q = REP_PREFIX;
2484 ret = PREFIX_REP;
2485 break;
2486
b1905489 2487 case LOCK_PREFIX_OPCODE:
c32fa91d
L
2488 q = LOCK_PREFIX;
2489 ret = PREFIX_LOCK;
b1905489
JB
2490 break;
2491
2492 case FWAIT_OPCODE:
2493 q = WAIT_PREFIX;
2494 break;
2495
2496 case ADDR_PREFIX_OPCODE:
2497 q = ADDR_PREFIX;
2498 break;
2499
2500 case DATA_PREFIX_OPCODE:
2501 q = DATA_PREFIX;
2502 break;
2503 }
2504 if (i.prefix[q] != 0)
c32fa91d 2505 ret = PREFIX_EXIST;
b1905489 2506 }
252b5132 2507
b1905489 2508 if (ret)
252b5132 2509 {
b1905489
JB
2510 if (!i.prefix[q])
2511 ++i.prefixes;
2512 i.prefix[q] |= prefix;
252b5132 2513 }
b1905489
JB
2514 else
2515 as_bad (_("same type of prefix used twice"));
252b5132 2516
252b5132
RH
2517 return ret;
2518}
2519
2520static void
78f12dd3 2521update_code_flag (int value, int check)
eecb386c 2522{
78f12dd3
L
2523 PRINTF_LIKE ((*as_error));
2524
1e9cc1c2 2525 flag_code = (enum flag_code) value;
40fb9820
L
2526 if (flag_code == CODE_64BIT)
2527 {
2528 cpu_arch_flags.bitfield.cpu64 = 1;
2529 cpu_arch_flags.bitfield.cpuno64 = 0;
40fb9820
L
2530 }
2531 else
2532 {
2533 cpu_arch_flags.bitfield.cpu64 = 0;
2534 cpu_arch_flags.bitfield.cpuno64 = 1;
40fb9820
L
2535 }
2536 if (value == CODE_64BIT && !cpu_arch_flags.bitfield.cpulm )
3e73aa7c 2537 {
78f12dd3
L
2538 if (check)
2539 as_error = as_fatal;
2540 else
2541 as_error = as_bad;
2542 (*as_error) (_("64bit mode not supported on `%s'."),
2543 cpu_arch_name ? cpu_arch_name : default_arch);
3e73aa7c 2544 }
40fb9820 2545 if (value == CODE_32BIT && !cpu_arch_flags.bitfield.cpui386)
3e73aa7c 2546 {
78f12dd3
L
2547 if (check)
2548 as_error = as_fatal;
2549 else
2550 as_error = as_bad;
2551 (*as_error) (_("32bit mode not supported on `%s'."),
2552 cpu_arch_name ? cpu_arch_name : default_arch);
3e73aa7c 2553 }
eecb386c
AM
2554 stackop_size = '\0';
2555}
2556
78f12dd3
L
2557static void
2558set_code_flag (int value)
2559{
2560 update_code_flag (value, 0);
2561}
2562
eecb386c 2563static void
e3bb37b5 2564set_16bit_gcc_code_flag (int new_code_flag)
252b5132 2565{
1e9cc1c2 2566 flag_code = (enum flag_code) new_code_flag;
40fb9820
L
2567 if (flag_code != CODE_16BIT)
2568 abort ();
2569 cpu_arch_flags.bitfield.cpu64 = 0;
2570 cpu_arch_flags.bitfield.cpuno64 = 1;
9306ca4a 2571 stackop_size = LONG_MNEM_SUFFIX;
252b5132
RH
2572}
2573
2574static void
e3bb37b5 2575set_intel_syntax (int syntax_flag)
252b5132
RH
2576{
2577 /* Find out if register prefixing is specified. */
2578 int ask_naked_reg = 0;
2579
2580 SKIP_WHITESPACE ();
29b0f896 2581 if (!is_end_of_line[(unsigned char) *input_line_pointer])
252b5132 2582 {
d02603dc
NC
2583 char *string;
2584 int e = get_symbol_name (&string);
252b5132 2585
47926f60 2586 if (strcmp (string, "prefix") == 0)
252b5132 2587 ask_naked_reg = 1;
47926f60 2588 else if (strcmp (string, "noprefix") == 0)
252b5132
RH
2589 ask_naked_reg = -1;
2590 else
d0b47220 2591 as_bad (_("bad argument to syntax directive."));
d02603dc 2592 (void) restore_line_pointer (e);
252b5132
RH
2593 }
2594 demand_empty_rest_of_line ();
c3332e24 2595
252b5132
RH
2596 intel_syntax = syntax_flag;
2597
2598 if (ask_naked_reg == 0)
f86103b7
AM
2599 allow_naked_reg = (intel_syntax
2600 && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
252b5132
RH
2601 else
2602 allow_naked_reg = (ask_naked_reg < 0);
9306ca4a 2603
ee86248c 2604 expr_set_rank (O_full_ptr, syntax_flag ? 10 : 0);
7ab9ffdd 2605
e4a3b5a4 2606 identifier_chars['%'] = intel_syntax && allow_naked_reg ? '%' : 0;
9306ca4a 2607 identifier_chars['$'] = intel_syntax ? '$' : 0;
e4a3b5a4 2608 register_prefix = allow_naked_reg ? "" : "%";
252b5132
RH
2609}
2610
1efbbeb4
L
2611static void
2612set_intel_mnemonic (int mnemonic_flag)
2613{
e1d4d893 2614 intel_mnemonic = mnemonic_flag;
1efbbeb4
L
2615}
2616
db51cc60
L
2617static void
2618set_allow_index_reg (int flag)
2619{
2620 allow_index_reg = flag;
2621}
2622
cb19c032 2623static void
7bab8ab5 2624set_check (int what)
cb19c032 2625{
7bab8ab5
JB
2626 enum check_kind *kind;
2627 const char *str;
2628
2629 if (what)
2630 {
2631 kind = &operand_check;
2632 str = "operand";
2633 }
2634 else
2635 {
2636 kind = &sse_check;
2637 str = "sse";
2638 }
2639
cb19c032
L
2640 SKIP_WHITESPACE ();
2641
2642 if (!is_end_of_line[(unsigned char) *input_line_pointer])
2643 {
d02603dc
NC
2644 char *string;
2645 int e = get_symbol_name (&string);
cb19c032
L
2646
2647 if (strcmp (string, "none") == 0)
7bab8ab5 2648 *kind = check_none;
cb19c032 2649 else if (strcmp (string, "warning") == 0)
7bab8ab5 2650 *kind = check_warning;
cb19c032 2651 else if (strcmp (string, "error") == 0)
7bab8ab5 2652 *kind = check_error;
cb19c032 2653 else
7bab8ab5 2654 as_bad (_("bad argument to %s_check directive."), str);
d02603dc 2655 (void) restore_line_pointer (e);
cb19c032
L
2656 }
2657 else
7bab8ab5 2658 as_bad (_("missing argument for %s_check directive"), str);
cb19c032
L
2659
2660 demand_empty_rest_of_line ();
2661}
2662
8a9036a4
L
2663static void
2664check_cpu_arch_compatible (const char *name ATTRIBUTE_UNUSED,
1e9cc1c2 2665 i386_cpu_flags new_flag ATTRIBUTE_UNUSED)
8a9036a4
L
2666{
2667#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2668 static const char *arch;
2669
c085ab00 2670 /* Intel MCU is only supported on ELF. */
8a9036a4
L
2671 if (!IS_ELF)
2672 return;
2673
2674 if (!arch)
2675 {
2676 /* Use cpu_arch_name if it is set in md_parse_option. Otherwise
2677 use default_arch. */
2678 arch = cpu_arch_name;
2679 if (!arch)
2680 arch = default_arch;
2681 }
2682
81486035 2683 /* If we are targeting Intel MCU, we must enable it. */
648d04db
JB
2684 if ((get_elf_backend_data (stdoutput)->elf_machine_code == EM_IAMCU)
2685 == new_flag.bitfield.cpuiamcu)
81486035
L
2686 return;
2687
8a9036a4
L
2688 as_bad (_("`%s' is not supported on `%s'"), name, arch);
2689#endif
2690}
2691
8180707f
JB
2692static void
2693extend_cpu_sub_arch_name (const char *name)
2694{
2695 if (cpu_sub_arch_name)
2696 cpu_sub_arch_name = reconcat (cpu_sub_arch_name, cpu_sub_arch_name,
ae89daec 2697 ".", name, (const char *) NULL);
8180707f 2698 else
ae89daec 2699 cpu_sub_arch_name = concat (".", name, (const char *) NULL);
8180707f
JB
2700}
2701
e413e4e9 2702static void
e3bb37b5 2703set_cpu_arch (int dummy ATTRIBUTE_UNUSED)
e413e4e9 2704{
f68697e8
JB
2705 typedef struct arch_stack_entry
2706 {
2707 const struct arch_stack_entry *prev;
2708 const char *name;
2709 char *sub_name;
2710 i386_cpu_flags flags;
2711 i386_cpu_flags isa_flags;
2712 enum processor_type isa;
2713 enum flag_code flag_code;
2714 char stackop_size;
2715 bool no_cond_jump_promotion;
2716 } arch_stack_entry;
2717 static const arch_stack_entry *arch_stack_top;
2718
47926f60 2719 SKIP_WHITESPACE ();
e413e4e9 2720
29b0f896 2721 if (!is_end_of_line[(unsigned char) *input_line_pointer])
e413e4e9 2722 {
3ce2ebcf
JB
2723 char *s;
2724 int e = get_symbol_name (&s);
2725 const char *string = s;
2726 unsigned int j = 0;
40fb9820 2727 i386_cpu_flags flags;
e413e4e9 2728
3ce2ebcf
JB
2729 if (strcmp (string, "default") == 0)
2730 {
2731 if (strcmp (default_arch, "iamcu") == 0)
2732 string = default_arch;
2733 else
2734 {
2735 static const i386_cpu_flags cpu_unknown_flags = CPU_UNKNOWN_FLAGS;
2736
2737 cpu_arch_name = NULL;
2738 free (cpu_sub_arch_name);
2739 cpu_sub_arch_name = NULL;
2740 cpu_arch_flags = cpu_unknown_flags;
2741 if (flag_code == CODE_64BIT)
2742 {
2743 cpu_arch_flags.bitfield.cpu64 = 1;
2744 cpu_arch_flags.bitfield.cpuno64 = 0;
2745 }
2746 else
2747 {
2748 cpu_arch_flags.bitfield.cpu64 = 0;
2749 cpu_arch_flags.bitfield.cpuno64 = 1;
2750 }
2751 cpu_arch_isa = PROCESSOR_UNKNOWN;
ae89daec 2752 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
3ce2ebcf
JB
2753 if (!cpu_arch_tune_set)
2754 {
2755 cpu_arch_tune = cpu_arch_isa;
2756 cpu_arch_tune_flags = cpu_arch_isa_flags;
2757 }
2758
2759 j = ARRAY_SIZE (cpu_arch) + 1;
2760 }
2761 }
f68697e8
JB
2762 else if (strcmp (string, "push") == 0)
2763 {
2764 arch_stack_entry *top = XNEW (arch_stack_entry);
2765
2766 top->name = cpu_arch_name;
2767 if (cpu_sub_arch_name)
2768 top->sub_name = xstrdup (cpu_sub_arch_name);
2769 else
2770 top->sub_name = NULL;
2771 top->flags = cpu_arch_flags;
2772 top->isa = cpu_arch_isa;
2773 top->isa_flags = cpu_arch_isa_flags;
2774 top->flag_code = flag_code;
2775 top->stackop_size = stackop_size;
2776 top->no_cond_jump_promotion = no_cond_jump_promotion;
2777
2778 top->prev = arch_stack_top;
2779 arch_stack_top = top;
2780
2781 (void) restore_line_pointer (e);
2782 demand_empty_rest_of_line ();
2783 return;
2784 }
2785 else if (strcmp (string, "pop") == 0)
2786 {
2787 const arch_stack_entry *top = arch_stack_top;
2788
2789 if (!top)
2790 as_bad (_(".arch stack is empty"));
2791 else if (top->flag_code != flag_code
2792 || top->stackop_size != stackop_size)
2793 {
2794 static const unsigned int bits[] = {
2795 [CODE_16BIT] = 16,
2796 [CODE_32BIT] = 32,
2797 [CODE_64BIT] = 64,
2798 };
2799
2800 as_bad (_("this `.arch pop' requires `.code%u%s' to be in effect"),
2801 bits[top->flag_code],
2802 top->stackop_size == LONG_MNEM_SUFFIX ? "gcc" : "");
2803 }
2804 else
2805 {
2806 arch_stack_top = top->prev;
2807
2808 cpu_arch_name = top->name;
2809 free (cpu_sub_arch_name);
2810 cpu_sub_arch_name = top->sub_name;
2811 cpu_arch_flags = top->flags;
2812 cpu_arch_isa = top->isa;
2813 cpu_arch_isa_flags = top->isa_flags;
2814 no_cond_jump_promotion = top->no_cond_jump_promotion;
2815
2816 XDELETE (top);
2817 }
2818
2819 (void) restore_line_pointer (e);
2820 demand_empty_rest_of_line ();
2821 return;
2822 }
3ce2ebcf
JB
2823
2824 for (; j < ARRAY_SIZE (cpu_arch); j++)
e413e4e9 2825 {
ae89daec
JB
2826 if (strcmp (string + (*string == '.'), cpu_arch[j].name) == 0
2827 && (*string == '.') == (cpu_arch[j].type == PROCESSOR_NONE))
e413e4e9 2828 {
5c6af06e
JB
2829 if (*string != '.')
2830 {
ae89daec 2831 check_cpu_arch_compatible (string, cpu_arch[j].enable);
648d04db 2832
91d6fa6a 2833 cpu_arch_name = cpu_arch[j].name;
d92c7521 2834 free (cpu_sub_arch_name);
5c6af06e 2835 cpu_sub_arch_name = NULL;
ae89daec 2836 cpu_arch_flags = cpu_arch[j].enable;
40fb9820
L
2837 if (flag_code == CODE_64BIT)
2838 {
2839 cpu_arch_flags.bitfield.cpu64 = 1;
2840 cpu_arch_flags.bitfield.cpuno64 = 0;
2841 }
2842 else
2843 {
2844 cpu_arch_flags.bitfield.cpu64 = 0;
2845 cpu_arch_flags.bitfield.cpuno64 = 1;
2846 }
91d6fa6a 2847 cpu_arch_isa = cpu_arch[j].type;
ae89daec 2848 cpu_arch_isa_flags = cpu_arch[j].enable;
ccc9c027
L
2849 if (!cpu_arch_tune_set)
2850 {
2851 cpu_arch_tune = cpu_arch_isa;
2852 cpu_arch_tune_flags = cpu_arch_isa_flags;
2853 }
d59a54c2 2854 pre_386_16bit_warned = false;
5c6af06e
JB
2855 break;
2856 }
40fb9820 2857
ae89daec
JB
2858 if (cpu_flags_all_zero (&cpu_arch[j].enable))
2859 continue;
2860
293f5f65 2861 flags = cpu_flags_or (cpu_arch_flags,
ae89daec 2862 cpu_arch[j].enable);
81486035 2863
5b64d091 2864 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
5c6af06e 2865 {
ae89daec 2866 extend_cpu_sub_arch_name (string + 1);
40fb9820 2867 cpu_arch_flags = flags;
a586129e 2868 cpu_arch_isa_flags = flags;
5c6af06e 2869 }
0089dace
L
2870 else
2871 cpu_arch_isa_flags
2872 = cpu_flags_or (cpu_arch_isa_flags,
ae89daec 2873 cpu_arch[j].enable);
d02603dc 2874 (void) restore_line_pointer (e);
5c6af06e
JB
2875 demand_empty_rest_of_line ();
2876 return;
e413e4e9
AM
2877 }
2878 }
293f5f65 2879
ae89daec 2880 if (startswith (string, ".no") && j >= ARRAY_SIZE (cpu_arch))
293f5f65 2881 {
33eaf5de 2882 /* Disable an ISA extension. */
ae89daec
JB
2883 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
2884 if (cpu_arch[j].type == PROCESSOR_NONE
2885 && strcmp (string + 3, cpu_arch[j].name) == 0)
293f5f65
L
2886 {
2887 flags = cpu_flags_and_not (cpu_arch_flags,
ae89daec 2888 cpu_arch[j].disable);
293f5f65
L
2889 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
2890 {
ae89daec 2891 extend_cpu_sub_arch_name (string + 1);
293f5f65
L
2892 cpu_arch_flags = flags;
2893 cpu_arch_isa_flags = flags;
2894 }
2895 (void) restore_line_pointer (e);
2896 demand_empty_rest_of_line ();
2897 return;
2898 }
293f5f65
L
2899 }
2900
3ce2ebcf 2901 if (j == ARRAY_SIZE (cpu_arch))
e413e4e9
AM
2902 as_bad (_("no such architecture: `%s'"), string);
2903
2904 *input_line_pointer = e;
2905 }
2906 else
2907 as_bad (_("missing cpu architecture"));
2908
fddf5b5b
AM
2909 no_cond_jump_promotion = 0;
2910 if (*input_line_pointer == ','
29b0f896 2911 && !is_end_of_line[(unsigned char) input_line_pointer[1]])
fddf5b5b 2912 {
d02603dc
NC
2913 char *string;
2914 char e;
2915
2916 ++input_line_pointer;
2917 e = get_symbol_name (&string);
fddf5b5b
AM
2918
2919 if (strcmp (string, "nojumps") == 0)
2920 no_cond_jump_promotion = 1;
2921 else if (strcmp (string, "jumps") == 0)
2922 ;
2923 else
2924 as_bad (_("no such architecture modifier: `%s'"), string);
2925
d02603dc 2926 (void) restore_line_pointer (e);
fddf5b5b
AM
2927 }
2928
e413e4e9
AM
2929 demand_empty_rest_of_line ();
2930}
2931
8a9036a4
L
2932enum bfd_architecture
2933i386_arch (void)
2934{
c085ab00 2935 if (cpu_arch_isa == PROCESSOR_IAMCU)
81486035
L
2936 {
2937 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2938 || flag_code == CODE_64BIT)
2939 as_fatal (_("Intel MCU is 32bit ELF only"));
2940 return bfd_arch_iamcu;
2941 }
8a9036a4
L
2942 else
2943 return bfd_arch_i386;
2944}
2945
b9d79e03 2946unsigned long
7016a5d5 2947i386_mach (void)
b9d79e03 2948{
d34049e8 2949 if (startswith (default_arch, "x86_64"))
8a9036a4 2950 {
c085ab00 2951 if (default_arch[6] == '\0')
8a9036a4 2952 return bfd_mach_x86_64;
351f65ca
L
2953 else
2954 return bfd_mach_x64_32;
8a9036a4 2955 }
5197d474
L
2956 else if (!strcmp (default_arch, "i386")
2957 || !strcmp (default_arch, "iamcu"))
81486035
L
2958 {
2959 if (cpu_arch_isa == PROCESSOR_IAMCU)
2960 {
2961 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
2962 as_fatal (_("Intel MCU is 32bit ELF only"));
2963 return bfd_mach_i386_iamcu;
2964 }
2965 else
2966 return bfd_mach_i386_i386;
2967 }
b9d79e03 2968 else
2b5d6a91 2969 as_fatal (_("unknown architecture"));
b9d79e03 2970}
b9d79e03 2971\f
99f0fb12
JB
2972#include "opcodes/i386-tbl.h"
2973
252b5132 2974void
7016a5d5 2975md_begin (void)
252b5132 2976{
86fa6981
L
2977 /* Support pseudo prefixes like {disp32}. */
2978 lex_type ['{'] = LEX_BEGIN_NAME;
2979
47926f60 2980 /* Initialize op_hash hash table. */
629310ab 2981 op_hash = str_htab_create ();
252b5132
RH
2982
2983 {
65f440c8
JB
2984 const insn_template *const *sets = i386_op_sets;
2985 const insn_template *const *end = sets + ARRAY_SIZE (i386_op_sets) - 1;
2986
2987 /* Type checks to compensate for the conversion through void * which
2988 occurs during hash table insertion / lookup. */
eb993861
JB
2989 (void) sizeof (sets == &current_templates->start);
2990 (void) sizeof (end == &current_templates->end);
65f440c8
JB
2991 for (; sets < end; ++sets)
2992 if (str_hash_insert (op_hash, (*sets)->name, sets, 0))
2993 as_fatal (_("duplicate %s"), (*sets)->name);
252b5132
RH
2994 }
2995
47926f60 2996 /* Initialize reg_hash hash table. */
629310ab 2997 reg_hash = str_htab_create ();
252b5132 2998 {
29b0f896 2999 const reg_entry *regtab;
c3fe08fa 3000 unsigned int regtab_size = i386_regtab_size;
252b5132 3001
c3fe08fa 3002 for (regtab = i386_regtab; regtab_size--; regtab++)
6225c532 3003 {
6288d05f
JB
3004 switch (regtab->reg_type.bitfield.class)
3005 {
3006 case Reg:
34684862
JB
3007 if (regtab->reg_type.bitfield.dword)
3008 {
3009 if (regtab->reg_type.bitfield.instance == Accum)
3010 reg_eax = regtab;
3011 }
3012 else if (regtab->reg_type.bitfield.tbyte)
6288d05f
JB
3013 {
3014 /* There's no point inserting st(<N>) in the hash table, as
3015 parentheses aren't included in register_chars[] anyway. */
3016 if (regtab->reg_type.bitfield.instance != Accum)
3017 continue;
3018 reg_st0 = regtab;
3019 }
3020 break;
3021
5e042380
JB
3022 case SReg:
3023 switch (regtab->reg_num)
3024 {
3025 case 0: reg_es = regtab; break;
3026 case 2: reg_ss = regtab; break;
3027 case 3: reg_ds = regtab; break;
3028 }
3029 break;
3030
6288d05f
JB
3031 case RegMask:
3032 if (!regtab->reg_num)
3033 reg_k0 = regtab;
3034 break;
3035 }
3036
6225c532
JB
3037 if (str_hash_insert (reg_hash, regtab->reg_name, regtab, 0) != NULL)
3038 as_fatal (_("duplicate %s"), regtab->reg_name);
6225c532 3039 }
252b5132
RH
3040 }
3041
47926f60 3042 /* Fill in lexical tables: mnemonic_chars, operand_chars. */
252b5132 3043 {
29b0f896
AM
3044 int c;
3045 char *p;
252b5132
RH
3046
3047 for (c = 0; c < 256; c++)
3048 {
014fbcda 3049 if (ISDIGIT (c) || ISLOWER (c))
252b5132
RH
3050 {
3051 mnemonic_chars[c] = c;
3052 register_chars[c] = c;
3053 operand_chars[c] = c;
3054 }
3882b010 3055 else if (ISUPPER (c))
252b5132 3056 {
3882b010 3057 mnemonic_chars[c] = TOLOWER (c);
252b5132
RH
3058 register_chars[c] = mnemonic_chars[c];
3059 operand_chars[c] = c;
3060 }
43234a1e 3061 else if (c == '{' || c == '}')
86fa6981
L
3062 {
3063 mnemonic_chars[c] = c;
3064 operand_chars[c] = c;
3065 }
b3983e5f
JB
3066#ifdef SVR4_COMMENT_CHARS
3067 else if (c == '\\' && strchr (i386_comment_chars, '/'))
3068 operand_chars[c] = c;
3069#endif
252b5132 3070
3882b010 3071 if (ISALPHA (c) || ISDIGIT (c))
252b5132
RH
3072 identifier_chars[c] = c;
3073 else if (c >= 128)
3074 {
3075 identifier_chars[c] = c;
3076 operand_chars[c] = c;
3077 }
3078 }
3079
3080#ifdef LEX_AT
3081 identifier_chars['@'] = '@';
32137342
NC
3082#endif
3083#ifdef LEX_QM
3084 identifier_chars['?'] = '?';
3085 operand_chars['?'] = '?';
252b5132 3086#endif
c0f3af97 3087 mnemonic_chars['_'] = '_';
791fe849 3088 mnemonic_chars['-'] = '-';
0003779b 3089 mnemonic_chars['.'] = '.';
252b5132
RH
3090 identifier_chars['_'] = '_';
3091 identifier_chars['.'] = '.';
3092
3093 for (p = operand_special_chars; *p != '\0'; p++)
3094 operand_chars[(unsigned char) *p] = *p;
3095 }
3096
a4447b93
RH
3097 if (flag_code == CODE_64BIT)
3098 {
ca19b261
KT
3099#if defined (OBJ_COFF) && defined (TE_PE)
3100 x86_dwarf2_return_column = (OUTPUT_FLAVOR == bfd_target_coff_flavour
3101 ? 32 : 16);
3102#else
a4447b93 3103 x86_dwarf2_return_column = 16;
ca19b261 3104#endif
61ff971f 3105 x86_cie_data_alignment = -8;
b52c4ee4
IB
3106#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3107 x86_sframe_cfa_sp_reg = 7;
3108 x86_sframe_cfa_fp_reg = 6;
3109#endif
a4447b93
RH
3110 }
3111 else
3112 {
3113 x86_dwarf2_return_column = 8;
3114 x86_cie_data_alignment = -4;
3115 }
e379e5f3
L
3116
3117 /* NB: FUSED_JCC_PADDING frag must have sufficient room so that it
3118 can be turned into BRANCH_PREFIX frag. */
3119 if (align_branch_prefix_size > MAX_FUSED_JCC_PADDING_SIZE)
3120 abort ();
252b5132
RH
3121}
3122
3123void
e3bb37b5 3124i386_print_statistics (FILE *file)
252b5132 3125{
629310ab
ML
3126 htab_print_statistics (file, "i386 opcode", op_hash);
3127 htab_print_statistics (file, "i386 register", reg_hash);
252b5132 3128}
654d6f31
AM
3129
3130void
3131i386_md_end (void)
3132{
3133 htab_delete (op_hash);
3134 htab_delete (reg_hash);
3135}
252b5132 3136\f
252b5132
RH
3137#ifdef DEBUG386
3138
ce8a8b2f 3139/* Debugging routines for md_assemble. */
d3ce72d0 3140static void pte (insn_template *);
40fb9820 3141static void pt (i386_operand_type);
e3bb37b5
L
3142static void pe (expressionS *);
3143static void ps (symbolS *);
252b5132
RH
3144
3145static void
2c703856 3146pi (const char *line, i386_insn *x)
252b5132 3147{
09137c09 3148 unsigned int j;
252b5132
RH
3149
3150 fprintf (stdout, "%s: template ", line);
3151 pte (&x->tm);
09f131f2
JH
3152 fprintf (stdout, " address: base %s index %s scale %x\n",
3153 x->base_reg ? x->base_reg->reg_name : "none",
3154 x->index_reg ? x->index_reg->reg_name : "none",
3155 x->log2_scale_factor);
3156 fprintf (stdout, " modrm: mode %x reg %x reg/mem %x\n",
252b5132 3157 x->rm.mode, x->rm.reg, x->rm.regmem);
09f131f2
JH
3158 fprintf (stdout, " sib: base %x index %x scale %x\n",
3159 x->sib.base, x->sib.index, x->sib.scale);
3160 fprintf (stdout, " rex: 64bit %x extX %x extY %x extZ %x\n",
161a04f6
L
3161 (x->rex & REX_W) != 0,
3162 (x->rex & REX_R) != 0,
3163 (x->rex & REX_X) != 0,
3164 (x->rex & REX_B) != 0);
09137c09 3165 for (j = 0; j < x->operands; j++)
252b5132 3166 {
09137c09
SP
3167 fprintf (stdout, " #%d: ", j + 1);
3168 pt (x->types[j]);
252b5132 3169 fprintf (stdout, "\n");
bab6aec1 3170 if (x->types[j].bitfield.class == Reg
3528c362
JB
3171 || x->types[j].bitfield.class == RegMMX
3172 || x->types[j].bitfield.class == RegSIMD
dd6b8a0b 3173 || x->types[j].bitfield.class == RegMask
00cee14f 3174 || x->types[j].bitfield.class == SReg
4a5c67ed
JB
3175 || x->types[j].bitfield.class == RegCR
3176 || x->types[j].bitfield.class == RegDR
dd6b8a0b
JB
3177 || x->types[j].bitfield.class == RegTR
3178 || x->types[j].bitfield.class == RegBND)
09137c09
SP
3179 fprintf (stdout, "%s\n", x->op[j].regs->reg_name);
3180 if (operand_type_check (x->types[j], imm))
3181 pe (x->op[j].imms);
3182 if (operand_type_check (x->types[j], disp))
3183 pe (x->op[j].disps);
252b5132
RH
3184 }
3185}
3186
3187static void
d3ce72d0 3188pte (insn_template *t)
252b5132 3189{
b933fa4b 3190 static const unsigned char opc_pfx[] = { 0, 0x66, 0xf3, 0xf2 };
441f6aca 3191 static const char *const opc_spc[] = {
0cc78721 3192 NULL, "0f", "0f38", "0f3a", NULL, "evexmap5", "evexmap6", NULL,
441f6aca
JB
3193 "XOP08", "XOP09", "XOP0A",
3194 };
09137c09 3195 unsigned int j;
441f6aca 3196
252b5132 3197 fprintf (stdout, " %d operands ", t->operands);
441f6aca
JB
3198 if (opc_pfx[t->opcode_modifier.opcodeprefix])
3199 fprintf (stdout, "pfx %x ", opc_pfx[t->opcode_modifier.opcodeprefix]);
3200 if (opc_spc[t->opcode_modifier.opcodespace])
3201 fprintf (stdout, "space %s ", opc_spc[t->opcode_modifier.opcodespace]);
47926f60 3202 fprintf (stdout, "opcode %x ", t->base_opcode);
252b5132
RH
3203 if (t->extension_opcode != None)
3204 fprintf (stdout, "ext %x ", t->extension_opcode);
40fb9820 3205 if (t->opcode_modifier.d)
252b5132 3206 fprintf (stdout, "D");
40fb9820 3207 if (t->opcode_modifier.w)
252b5132
RH
3208 fprintf (stdout, "W");
3209 fprintf (stdout, "\n");
09137c09 3210 for (j = 0; j < t->operands; j++)
252b5132 3211 {
09137c09
SP
3212 fprintf (stdout, " #%d type ", j + 1);
3213 pt (t->operand_types[j]);
252b5132
RH
3214 fprintf (stdout, "\n");
3215 }
3216}
3217
3218static void
e3bb37b5 3219pe (expressionS *e)
252b5132 3220{
24eab124 3221 fprintf (stdout, " operation %d\n", e->X_op);
b8281767
AM
3222 fprintf (stdout, " add_number %" PRId64 " (%" PRIx64 ")\n",
3223 (int64_t) e->X_add_number, (uint64_t) (valueT) e->X_add_number);
252b5132
RH
3224 if (e->X_add_symbol)
3225 {
3226 fprintf (stdout, " add_symbol ");
3227 ps (e->X_add_symbol);
3228 fprintf (stdout, "\n");
3229 }
3230 if (e->X_op_symbol)
3231 {
3232 fprintf (stdout, " op_symbol ");
3233 ps (e->X_op_symbol);
3234 fprintf (stdout, "\n");
3235 }
3236}
3237
3238static void
e3bb37b5 3239ps (symbolS *s)
252b5132
RH
3240{
3241 fprintf (stdout, "%s type %s%s",
3242 S_GET_NAME (s),
3243 S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
3244 segment_name (S_GET_SEGMENT (s)));
3245}
3246
7b81dfbb 3247static struct type_name
252b5132 3248 {
40fb9820
L
3249 i386_operand_type mask;
3250 const char *name;
252b5132 3251 }
7b81dfbb 3252const type_names[] =
252b5132 3253{
05909f23
JB
3254 { { .bitfield = { .class = Reg, .byte = 1 } }, "r8" },
3255 { { .bitfield = { .class = Reg, .word = 1 } }, "r16" },
3256 { { .bitfield = { .class = Reg, .dword = 1 } }, "r32" },
3257 { { .bitfield = { .class = Reg, .qword = 1 } }, "r64" },
3258 { { .bitfield = { .instance = Accum, .byte = 1 } }, "acc8" },
3259 { { .bitfield = { .instance = Accum, .word = 1 } }, "acc16" },
3260 { { .bitfield = { .instance = Accum, .dword = 1 } }, "acc32" },
3261 { { .bitfield = { .instance = Accum, .qword = 1 } }, "acc64" },
3262 { { .bitfield = { .imm8 = 1 } }, "i8" },
3263 { { .bitfield = { .imm8s = 1 } }, "i8s" },
3264 { { .bitfield = { .imm16 = 1 } }, "i16" },
3265 { { .bitfield = { .imm32 = 1 } }, "i32" },
3266 { { .bitfield = { .imm32s = 1 } }, "i32s" },
3267 { { .bitfield = { .imm64 = 1 } }, "i64" },
3268 { { .bitfield = { .imm1 = 1 } }, "i1" },
3269 { { .bitfield = { .baseindex = 1 } }, "BaseIndex" },
3270 { { .bitfield = { .disp8 = 1 } }, "d8" },
3271 { { .bitfield = { .disp16 = 1 } }, "d16" },
3272 { { .bitfield = { .disp32 = 1 } }, "d32" },
3273 { { .bitfield = { .disp64 = 1 } }, "d64" },
3274 { { .bitfield = { .instance = RegD, .word = 1 } }, "InOutPortReg" },
3275 { { .bitfield = { .instance = RegC, .byte = 1 } }, "ShiftCount" },
3276 { { .bitfield = { .class = RegCR } }, "control reg" },
3277 { { .bitfield = { .class = RegTR } }, "test reg" },
3278 { { .bitfield = { .class = RegDR } }, "debug reg" },
3279 { { .bitfield = { .class = Reg, .tbyte = 1 } }, "FReg" },
3280 { { .bitfield = { .instance = Accum, .tbyte = 1 } }, "FAcc" },
3281 { { .bitfield = { .class = SReg } }, "SReg" },
3282 { { .bitfield = { .class = RegMMX } }, "rMMX" },
3283 { { .bitfield = { .class = RegSIMD, .xmmword = 1 } }, "rXMM" },
3284 { { .bitfield = { .class = RegSIMD, .ymmword = 1 } }, "rYMM" },
3285 { { .bitfield = { .class = RegSIMD, .zmmword = 1 } }, "rZMM" },
3286 { { .bitfield = { .class = RegSIMD, .tmmword = 1 } }, "rTMM" },
3287 { { .bitfield = { .class = RegMask } }, "Mask reg" },
252b5132
RH
3288};
3289
3290static void
40fb9820 3291pt (i386_operand_type t)
252b5132 3292{
40fb9820 3293 unsigned int j;
c6fb90c8 3294 i386_operand_type a;
252b5132 3295
40fb9820 3296 for (j = 0; j < ARRAY_SIZE (type_names); j++)
c6fb90c8
L
3297 {
3298 a = operand_type_and (t, type_names[j].mask);
2c703856 3299 if (operand_type_equal (&a, &type_names[j].mask))
c6fb90c8
L
3300 fprintf (stdout, "%s, ", type_names[j].name);
3301 }
252b5132
RH
3302 fflush (stdout);
3303}
3304
3305#endif /* DEBUG386 */
3306\f
252b5132 3307static bfd_reloc_code_real_type
3956db08 3308reloc (unsigned int size,
64e74474
AM
3309 int pcrel,
3310 int sign,
3311 bfd_reloc_code_real_type other)
252b5132 3312{
47926f60 3313 if (other != NO_RELOC)
3956db08 3314 {
91d6fa6a 3315 reloc_howto_type *rel;
3956db08
JB
3316
3317 if (size == 8)
3318 switch (other)
3319 {
64e74474
AM
3320 case BFD_RELOC_X86_64_GOT32:
3321 return BFD_RELOC_X86_64_GOT64;
3322 break;
553d1284
L
3323 case BFD_RELOC_X86_64_GOTPLT64:
3324 return BFD_RELOC_X86_64_GOTPLT64;
3325 break;
64e74474
AM
3326 case BFD_RELOC_X86_64_PLTOFF64:
3327 return BFD_RELOC_X86_64_PLTOFF64;
3328 break;
3329 case BFD_RELOC_X86_64_GOTPC32:
3330 other = BFD_RELOC_X86_64_GOTPC64;
3331 break;
3332 case BFD_RELOC_X86_64_GOTPCREL:
3333 other = BFD_RELOC_X86_64_GOTPCREL64;
3334 break;
3335 case BFD_RELOC_X86_64_TPOFF32:
3336 other = BFD_RELOC_X86_64_TPOFF64;
3337 break;
3338 case BFD_RELOC_X86_64_DTPOFF32:
3339 other = BFD_RELOC_X86_64_DTPOFF64;
3340 break;
3341 default:
3342 break;
3956db08 3343 }
e05278af 3344
8ce3d284 3345#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
3346 if (other == BFD_RELOC_SIZE32)
3347 {
3348 if (size == 8)
1ab668bf 3349 other = BFD_RELOC_SIZE64;
8fd4256d 3350 if (pcrel)
1ab668bf
AM
3351 {
3352 as_bad (_("there are no pc-relative size relocations"));
3353 return NO_RELOC;
3354 }
8fd4256d 3355 }
8ce3d284 3356#endif
8fd4256d 3357
e05278af 3358 /* Sign-checking 4-byte relocations in 16-/32-bit code is pointless. */
f2d8a97c 3359 if (size == 4 && (flag_code != CODE_64BIT || disallow_64bit_reloc))
e05278af
JB
3360 sign = -1;
3361
91d6fa6a
NC
3362 rel = bfd_reloc_type_lookup (stdoutput, other);
3363 if (!rel)
3956db08 3364 as_bad (_("unknown relocation (%u)"), other);
91d6fa6a 3365 else if (size != bfd_get_reloc_size (rel))
3956db08 3366 as_bad (_("%u-byte relocation cannot be applied to %u-byte field"),
91d6fa6a 3367 bfd_get_reloc_size (rel),
3956db08 3368 size);
91d6fa6a 3369 else if (pcrel && !rel->pc_relative)
3956db08 3370 as_bad (_("non-pc-relative relocation for pc-relative field"));
91d6fa6a 3371 else if ((rel->complain_on_overflow == complain_overflow_signed
3956db08 3372 && !sign)
91d6fa6a 3373 || (rel->complain_on_overflow == complain_overflow_unsigned
64e74474 3374 && sign > 0))
3956db08
JB
3375 as_bad (_("relocated field and relocation type differ in signedness"));
3376 else
3377 return other;
3378 return NO_RELOC;
3379 }
252b5132
RH
3380
3381 if (pcrel)
3382 {
3e73aa7c 3383 if (!sign)
3956db08 3384 as_bad (_("there are no unsigned pc-relative relocations"));
252b5132
RH
3385 switch (size)
3386 {
3387 case 1: return BFD_RELOC_8_PCREL;
3388 case 2: return BFD_RELOC_16_PCREL;
d258b828 3389 case 4: return BFD_RELOC_32_PCREL;
d6ab8113 3390 case 8: return BFD_RELOC_64_PCREL;
252b5132 3391 }
3956db08 3392 as_bad (_("cannot do %u byte pc-relative relocation"), size);
252b5132
RH
3393 }
3394 else
3395 {
3956db08 3396 if (sign > 0)
e5cb08ac 3397 switch (size)
3e73aa7c
JH
3398 {
3399 case 4: return BFD_RELOC_X86_64_32S;
3400 }
3401 else
3402 switch (size)
3403 {
3404 case 1: return BFD_RELOC_8;
3405 case 2: return BFD_RELOC_16;
3406 case 4: return BFD_RELOC_32;
3407 case 8: return BFD_RELOC_64;
3408 }
3956db08
JB
3409 as_bad (_("cannot do %s %u byte relocation"),
3410 sign > 0 ? "signed" : "unsigned", size);
252b5132
RH
3411 }
3412
0cc9e1d3 3413 return NO_RELOC;
252b5132
RH
3414}
3415
47926f60
KH
3416/* Here we decide which fixups can be adjusted to make them relative to
3417 the beginning of the section instead of the symbol. Basically we need
3418 to make sure that the dynamic relocations are done correctly, so in
3419 some cases we force the original symbol to be used. */
3420
252b5132 3421int
e3bb37b5 3422tc_i386_fix_adjustable (fixS *fixP ATTRIBUTE_UNUSED)
252b5132 3423{
6d249963 3424#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 3425 if (!IS_ELF)
31312f95
AM
3426 return 1;
3427
a161fe53
AM
3428 /* Don't adjust pc-relative references to merge sections in 64-bit
3429 mode. */
3430 if (use_rela_relocations
3431 && (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0
3432 && fixP->fx_pcrel)
252b5132 3433 return 0;
31312f95 3434
8d01d9a9
AJ
3435 /* The x86_64 GOTPCREL are represented as 32bit PCrel relocations
3436 and changed later by validate_fix. */
3437 if (GOT_symbol && fixP->fx_subsy == GOT_symbol
3438 && fixP->fx_r_type == BFD_RELOC_32_PCREL)
3439 return 0;
3440
8fd4256d
L
3441 /* Adjust_reloc_syms doesn't know about the GOT. Need to keep symbol
3442 for size relocations. */
3443 if (fixP->fx_r_type == BFD_RELOC_SIZE32
3444 || fixP->fx_r_type == BFD_RELOC_SIZE64
3445 || fixP->fx_r_type == BFD_RELOC_386_GOTOFF
252b5132 3446 || fixP->fx_r_type == BFD_RELOC_386_GOT32
02a86693 3447 || fixP->fx_r_type == BFD_RELOC_386_GOT32X
13ae64f3
JJ
3448 || fixP->fx_r_type == BFD_RELOC_386_TLS_GD
3449 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDM
3450 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDO_32
3451 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE_32
37e55690
JJ
3452 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE
3453 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTIE
13ae64f3
JJ
3454 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE_32
3455 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE
67a4f2b7
AO
3456 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTDESC
3457 || fixP->fx_r_type == BFD_RELOC_386_TLS_DESC_CALL
3e73aa7c 3458 || fixP->fx_r_type == BFD_RELOC_X86_64_GOT32
80b3ee89 3459 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCREL
56ceb5b5
L
3460 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCRELX
3461 || fixP->fx_r_type == BFD_RELOC_X86_64_REX_GOTPCRELX
bffbf940
JJ
3462 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSGD
3463 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSLD
3464 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF32
d6ab8113 3465 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF64
bffbf940
JJ
3466 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF
3467 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF32
d6ab8113
JB
3468 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF64
3469 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTOFF64
67a4f2b7
AO
3470 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
3471 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSDESC_CALL
252b5132
RH
3472 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3473 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3474 return 0;
31312f95 3475#endif
252b5132
RH
3476 return 1;
3477}
252b5132 3478
a9aabc23
JB
3479static INLINE bool
3480want_disp32 (const insn_template *t)
3481{
3482 return flag_code != CODE_64BIT
3483 || i.prefix[ADDR_PREFIX]
3484 || (t->base_opcode == 0x8d
3485 && t->opcode_modifier.opcodespace == SPACE_BASE
fe134c65
JB
3486 && (!i.types[1].bitfield.qword
3487 || t->opcode_modifier.size == SIZE32));
a9aabc23
JB
3488}
3489
b4cac588 3490static int
e3bb37b5 3491intel_float_operand (const char *mnemonic)
252b5132 3492{
9306ca4a
JB
3493 /* Note that the value returned is meaningful only for opcodes with (memory)
3494 operands, hence the code here is free to improperly handle opcodes that
3495 have no operands (for better performance and smaller code). */
3496
3497 if (mnemonic[0] != 'f')
3498 return 0; /* non-math */
3499
3500 switch (mnemonic[1])
3501 {
3502 /* fclex, fdecstp, fdisi, femms, feni, fincstp, finit, fsetpm, and
3503 the fs segment override prefix not currently handled because no
3504 call path can make opcodes without operands get here */
3505 case 'i':
3506 return 2 /* integer op */;
3507 case 'l':
3508 if (mnemonic[2] == 'd' && (mnemonic[3] == 'c' || mnemonic[3] == 'e'))
3509 return 3; /* fldcw/fldenv */
3510 break;
3511 case 'n':
3512 if (mnemonic[2] != 'o' /* fnop */)
3513 return 3; /* non-waiting control op */
3514 break;
3515 case 'r':
3516 if (mnemonic[2] == 's')
3517 return 3; /* frstor/frstpm */
3518 break;
3519 case 's':
3520 if (mnemonic[2] == 'a')
3521 return 3; /* fsave */
3522 if (mnemonic[2] == 't')
3523 {
3524 switch (mnemonic[3])
3525 {
3526 case 'c': /* fstcw */
3527 case 'd': /* fstdw */
3528 case 'e': /* fstenv */
3529 case 's': /* fsts[gw] */
3530 return 3;
3531 }
3532 }
3533 break;
3534 case 'x':
3535 if (mnemonic[2] == 'r' || mnemonic[2] == 's')
3536 return 0; /* fxsave/fxrstor are not really math ops */
3537 break;
3538 }
252b5132 3539
9306ca4a 3540 return 1;
252b5132
RH
3541}
3542
9a182d04
JB
3543static INLINE void
3544install_template (const insn_template *t)
3545{
3546 unsigned int l;
3547
3548 i.tm = *t;
3549
3550 /* Note that for pseudo prefixes this produces a length of 1. But for them
3551 the length isn't interesting at all. */
3552 for (l = 1; l < 4; ++l)
3553 if (!(t->base_opcode >> (8 * l)))
3554 break;
3555
3556 i.opcode_length = l;
3557}
3558
c0f3af97
L
3559/* Build the VEX prefix. */
3560
3561static void
d3ce72d0 3562build_vex_prefix (const insn_template *t)
c0f3af97
L
3563{
3564 unsigned int register_specifier;
c0f3af97 3565 unsigned int vector_length;
03751133 3566 unsigned int w;
c0f3af97
L
3567
3568 /* Check register specifier. */
3569 if (i.vex.register_specifier)
43234a1e
L
3570 {
3571 register_specifier =
3572 ~register_number (i.vex.register_specifier) & 0xf;
3573 gas_assert ((i.vex.register_specifier->reg_flags & RegVRex) == 0);
3574 }
c0f3af97
L
3575 else
3576 register_specifier = 0xf;
3577
79f0fa25
L
3578 /* Use 2-byte VEX prefix by swapping destination and source operand
3579 if there are more than 1 register operand. */
3580 if (i.reg_operands > 1
3581 && i.vec_encoding != vex_encoding_vex3
86fa6981 3582 && i.dir_encoding == dir_encoding_default
fa99fab2 3583 && i.operands == i.reg_operands
dbbc8b7e 3584 && operand_type_equal (&i.types[0], &i.types[i.operands - 1])
441f6aca 3585 && i.tm.opcode_modifier.opcodespace == SPACE_0F
dbbc8b7e 3586 && (i.tm.opcode_modifier.load || i.tm.opcode_modifier.d)
fa99fab2
L
3587 && i.rex == REX_B)
3588 {
3589 unsigned int xchg = i.operands - 1;
3590 union i386_op temp_op;
3591 i386_operand_type temp_type;
3592
3593 temp_type = i.types[xchg];
3594 i.types[xchg] = i.types[0];
3595 i.types[0] = temp_type;
3596 temp_op = i.op[xchg];
3597 i.op[xchg] = i.op[0];
3598 i.op[0] = temp_op;
3599
9c2799c2 3600 gas_assert (i.rm.mode == 3);
fa99fab2
L
3601
3602 i.rex = REX_R;
3603 xchg = i.rm.regmem;
3604 i.rm.regmem = i.rm.reg;
3605 i.rm.reg = xchg;
3606
dbbc8b7e
JB
3607 if (i.tm.opcode_modifier.d)
3608 i.tm.base_opcode ^= (i.tm.base_opcode & 0xee) != 0x6e
2c735193 3609 ? Opcode_ExtD : Opcode_SIMD_IntD;
dbbc8b7e 3610 else /* Use the next insn. */
9a182d04 3611 install_template (&t[1]);
fa99fab2
L
3612 }
3613
79dec6b7
JB
3614 /* Use 2-byte VEX prefix by swapping commutative source operands if there
3615 are no memory operands and at least 3 register ones. */
3616 if (i.reg_operands >= 3
3617 && i.vec_encoding != vex_encoding_vex3
3618 && i.reg_operands == i.operands - i.imm_operands
3619 && i.tm.opcode_modifier.vex
3620 && i.tm.opcode_modifier.commutative
3621 && (i.tm.opcode_modifier.sse2avx || optimize > 1)
3622 && i.rex == REX_B
3623 && i.vex.register_specifier
3624 && !(i.vex.register_specifier->reg_flags & RegRex))
3625 {
3626 unsigned int xchg = i.operands - i.reg_operands;
3627 union i386_op temp_op;
3628 i386_operand_type temp_type;
3629
441f6aca 3630 gas_assert (i.tm.opcode_modifier.opcodespace == SPACE_0F);
79dec6b7
JB
3631 gas_assert (!i.tm.opcode_modifier.sae);
3632 gas_assert (operand_type_equal (&i.types[i.operands - 2],
3633 &i.types[i.operands - 3]));
3634 gas_assert (i.rm.mode == 3);
3635
3636 temp_type = i.types[xchg];
3637 i.types[xchg] = i.types[xchg + 1];
3638 i.types[xchg + 1] = temp_type;
3639 temp_op = i.op[xchg];
3640 i.op[xchg] = i.op[xchg + 1];
3641 i.op[xchg + 1] = temp_op;
3642
3643 i.rex = 0;
3644 xchg = i.rm.regmem | 8;
3645 i.rm.regmem = ~register_specifier & 0xf;
3646 gas_assert (!(i.rm.regmem & 8));
3647 i.vex.register_specifier += xchg - i.rm.regmem;
3648 register_specifier = ~xchg & 0xf;
3649 }
3650
539f890d
L
3651 if (i.tm.opcode_modifier.vex == VEXScalar)
3652 vector_length = avxscalar;
10c17abd
JB
3653 else if (i.tm.opcode_modifier.vex == VEX256)
3654 vector_length = 1;
539f890d 3655 else
10c17abd 3656 {
56522fc5 3657 unsigned int op;
10c17abd 3658
c7213af9
L
3659 /* Determine vector length from the last multi-length vector
3660 operand. */
10c17abd 3661 vector_length = 0;
56522fc5 3662 for (op = t->operands; op--;)
10c17abd
JB
3663 if (t->operand_types[op].bitfield.xmmword
3664 && t->operand_types[op].bitfield.ymmword
3665 && i.types[op].bitfield.ymmword)
3666 {
3667 vector_length = 1;
3668 break;
3669 }
3670 }
c0f3af97 3671
03751133
L
3672 /* Check the REX.W bit and VEXW. */
3673 if (i.tm.opcode_modifier.vexw == VEXWIG)
3674 w = (vexwig == vexw1 || (i.rex & REX_W)) ? 1 : 0;
3675 else if (i.tm.opcode_modifier.vexw)
3676 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3677 else
931d03b7 3678 w = (flag_code == CODE_64BIT ? i.rex & REX_W : vexwig == vexw1) ? 1 : 0;
03751133 3679
c0f3af97 3680 /* Use 2-byte VEX prefix if possible. */
03751133
L
3681 if (w == 0
3682 && i.vec_encoding != vex_encoding_vex3
441f6aca 3683 && i.tm.opcode_modifier.opcodespace == SPACE_0F
c0f3af97
L
3684 && (i.rex & (REX_W | REX_X | REX_B)) == 0)
3685 {
3686 /* 2-byte VEX prefix. */
3687 unsigned int r;
3688
3689 i.vex.length = 2;
3690 i.vex.bytes[0] = 0xc5;
3691
3692 /* Check the REX.R bit. */
3693 r = (i.rex & REX_R) ? 0 : 1;
3694 i.vex.bytes[1] = (r << 7
3695 | register_specifier << 3
3696 | vector_length << 2
35648716 3697 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3698 }
3699 else
3700 {
3701 /* 3-byte VEX prefix. */
f88c9eb0 3702 i.vex.length = 3;
f88c9eb0 3703
441f6aca 3704 switch (i.tm.opcode_modifier.opcodespace)
5dd85c99 3705 {
441f6aca
JB
3706 case SPACE_0F:
3707 case SPACE_0F38:
3708 case SPACE_0F3A:
80de6e00 3709 i.vex.bytes[0] = 0xc4;
7f399153 3710 break;
441f6aca
JB
3711 case SPACE_XOP08:
3712 case SPACE_XOP09:
3713 case SPACE_XOP0A:
f88c9eb0 3714 i.vex.bytes[0] = 0x8f;
7f399153
L
3715 break;
3716 default:
3717 abort ();
f88c9eb0 3718 }
c0f3af97 3719
c0f3af97
L
3720 /* The high 3 bits of the second VEX byte are 1's compliment
3721 of RXB bits from REX. */
441f6aca 3722 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | i.tm.opcode_modifier.opcodespace;
c0f3af97 3723
c0f3af97
L
3724 i.vex.bytes[2] = (w << 7
3725 | register_specifier << 3
3726 | vector_length << 2
35648716 3727 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3728 }
3729}
3730
5b7c81bd 3731static INLINE bool
e771e7c9
JB
3732is_evex_encoding (const insn_template *t)
3733{
7091c612 3734 return t->opcode_modifier.evex || t->opcode_modifier.disp8memshift
e771e7c9 3735 || t->opcode_modifier.broadcast || t->opcode_modifier.masking
a80195f1 3736 || t->opcode_modifier.sae;
e771e7c9
JB
3737}
3738
5b7c81bd 3739static INLINE bool
7a8655d2
JB
3740is_any_vex_encoding (const insn_template *t)
3741{
7b47a312 3742 return t->opcode_modifier.vex || is_evex_encoding (t);
7a8655d2
JB
3743}
3744
a5748e0d
JB
3745static unsigned int
3746get_broadcast_bytes (const insn_template *t, bool diag)
3747{
3748 unsigned int op, bytes;
3749 const i386_operand_type *types;
3750
3751 if (i.broadcast.type)
3752 return i.broadcast.bytes = ((1 << (t->opcode_modifier.broadcast - 1))
3753 * i.broadcast.type);
3754
3755 gas_assert (intel_syntax);
3756
3757 for (op = 0; op < t->operands; ++op)
3758 if (t->operand_types[op].bitfield.baseindex)
3759 break;
3760
3761 gas_assert (op < t->operands);
3762
3763 if (t->opcode_modifier.evex
3764 && t->opcode_modifier.evex != EVEXDYN)
3765 switch (i.broadcast.bytes)
3766 {
3767 case 1:
3768 if (t->operand_types[op].bitfield.word)
3769 return 2;
3770 /* Fall through. */
3771 case 2:
3772 if (t->operand_types[op].bitfield.dword)
3773 return 4;
3774 /* Fall through. */
3775 case 4:
3776 if (t->operand_types[op].bitfield.qword)
3777 return 8;
3778 /* Fall through. */
3779 case 8:
3780 if (t->operand_types[op].bitfield.xmmword)
3781 return 16;
3782 if (t->operand_types[op].bitfield.ymmword)
3783 return 32;
3784 if (t->operand_types[op].bitfield.zmmword)
3785 return 64;
3786 /* Fall through. */
3787 default:
3788 abort ();
3789 }
3790
3791 gas_assert (op + 1 < t->operands);
3792
3793 if (t->operand_types[op + 1].bitfield.xmmword
3794 + t->operand_types[op + 1].bitfield.ymmword
3795 + t->operand_types[op + 1].bitfield.zmmword > 1)
3796 {
3797 types = &i.types[op + 1];
3798 diag = false;
3799 }
3800 else /* Ambiguous - guess with a preference to non-AVX512VL forms. */
3801 types = &t->operand_types[op];
3802
3803 if (types->bitfield.zmmword)
3804 bytes = 64;
3805 else if (types->bitfield.ymmword)
3806 bytes = 32;
3807 else
3808 bytes = 16;
3809
3810 if (diag)
3811 as_warn (_("ambiguous broadcast for `%s', using %u-bit form"),
3812 t->name, bytes * 8);
3813
3814 return bytes;
3815}
3816
43234a1e
L
3817/* Build the EVEX prefix. */
3818
3819static void
3820build_evex_prefix (void)
3821{
35648716 3822 unsigned int register_specifier, w;
43234a1e
L
3823 rex_byte vrex_used = 0;
3824
3825 /* Check register specifier. */
3826 if (i.vex.register_specifier)
3827 {
3828 gas_assert ((i.vrex & REX_X) == 0);
3829
3830 register_specifier = i.vex.register_specifier->reg_num;
3831 if ((i.vex.register_specifier->reg_flags & RegRex))
3832 register_specifier += 8;
3833 /* The upper 16 registers are encoded in the fourth byte of the
3834 EVEX prefix. */
3835 if (!(i.vex.register_specifier->reg_flags & RegVRex))
3836 i.vex.bytes[3] = 0x8;
3837 register_specifier = ~register_specifier & 0xf;
3838 }
3839 else
3840 {
3841 register_specifier = 0xf;
3842
3843 /* Encode upper 16 vector index register in the fourth byte of
3844 the EVEX prefix. */
3845 if (!(i.vrex & REX_X))
3846 i.vex.bytes[3] = 0x8;
3847 else
3848 vrex_used |= REX_X;
3849 }
3850
43234a1e
L
3851 /* 4 byte EVEX prefix. */
3852 i.vex.length = 4;
3853 i.vex.bytes[0] = 0x62;
3854
43234a1e
L
3855 /* The high 3 bits of the second EVEX byte are 1's compliment of RXB
3856 bits from REX. */
441f6aca 3857 gas_assert (i.tm.opcode_modifier.opcodespace >= SPACE_0F);
0cc78721 3858 gas_assert (i.tm.opcode_modifier.opcodespace <= SPACE_EVEXMAP6);
441f6aca 3859 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | i.tm.opcode_modifier.opcodespace;
43234a1e
L
3860
3861 /* The fifth bit of the second EVEX byte is 1's compliment of the
3862 REX_R bit in VREX. */
3863 if (!(i.vrex & REX_R))
3864 i.vex.bytes[1] |= 0x10;
3865 else
3866 vrex_used |= REX_R;
3867
3868 if ((i.reg_operands + i.imm_operands) == i.operands)
3869 {
3870 /* When all operands are registers, the REX_X bit in REX is not
3871 used. We reuse it to encode the upper 16 registers, which is
3872 indicated by the REX_B bit in VREX. The REX_X bit is encoded
3873 as 1's compliment. */
3874 if ((i.vrex & REX_B))
3875 {
3876 vrex_used |= REX_B;
3877 i.vex.bytes[1] &= ~0x40;
3878 }
3879 }
3880
3881 /* EVEX instructions shouldn't need the REX prefix. */
3882 i.vrex &= ~vrex_used;
3883 gas_assert (i.vrex == 0);
3884
6865c043
L
3885 /* Check the REX.W bit and VEXW. */
3886 if (i.tm.opcode_modifier.vexw == VEXWIG)
3887 w = (evexwig == evexw1 || (i.rex & REX_W)) ? 1 : 0;
3888 else if (i.tm.opcode_modifier.vexw)
3889 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3890 else
931d03b7 3891 w = (flag_code == CODE_64BIT ? i.rex & REX_W : evexwig == evexw1) ? 1 : 0;
43234a1e 3892
43234a1e 3893 /* The third byte of the EVEX prefix. */
35648716
JB
3894 i.vex.bytes[2] = ((w << 7)
3895 | (register_specifier << 3)
3896 | 4 /* Encode the U bit. */
3897 | i.tm.opcode_modifier.opcodeprefix);
43234a1e
L
3898
3899 /* The fourth byte of the EVEX prefix. */
3900 /* The zeroing-masking bit. */
6225c532 3901 if (i.mask.reg && i.mask.zeroing)
43234a1e
L
3902 i.vex.bytes[3] |= 0x80;
3903
3904 /* Don't always set the broadcast bit if there is no RC. */
ca5312a2 3905 if (i.rounding.type == rc_none)
43234a1e
L
3906 {
3907 /* Encode the vector length. */
3908 unsigned int vec_length;
3909
e771e7c9
JB
3910 if (!i.tm.opcode_modifier.evex
3911 || i.tm.opcode_modifier.evex == EVEXDYN)
3912 {
56522fc5 3913 unsigned int op;
e771e7c9 3914
c7213af9
L
3915 /* Determine vector length from the last multi-length vector
3916 operand. */
56522fc5 3917 for (op = i.operands; op--;)
e771e7c9
JB
3918 if (i.tm.operand_types[op].bitfield.xmmword
3919 + i.tm.operand_types[op].bitfield.ymmword
3920 + i.tm.operand_types[op].bitfield.zmmword > 1)
3921 {
3922 if (i.types[op].bitfield.zmmword)
c7213af9
L
3923 {
3924 i.tm.opcode_modifier.evex = EVEX512;
3925 break;
3926 }
e771e7c9 3927 else if (i.types[op].bitfield.ymmword)
c7213af9
L
3928 {
3929 i.tm.opcode_modifier.evex = EVEX256;
3930 break;
3931 }
e771e7c9 3932 else if (i.types[op].bitfield.xmmword)
c7213af9
L
3933 {
3934 i.tm.opcode_modifier.evex = EVEX128;
3935 break;
3936 }
a5748e0d 3937 else if (i.broadcast.bytes && op == i.broadcast.operand)
625cbd7a 3938 {
a5748e0d 3939 switch (get_broadcast_bytes (&i.tm, true))
625cbd7a
JB
3940 {
3941 case 64:
3942 i.tm.opcode_modifier.evex = EVEX512;
3943 break;
3944 case 32:
3945 i.tm.opcode_modifier.evex = EVEX256;
3946 break;
3947 case 16:
3948 i.tm.opcode_modifier.evex = EVEX128;
3949 break;
3950 default:
c7213af9 3951 abort ();
625cbd7a 3952 }
c7213af9 3953 break;
625cbd7a 3954 }
e771e7c9 3955 }
c7213af9 3956
56522fc5 3957 if (op >= MAX_OPERANDS)
c7213af9 3958 abort ();
e771e7c9
JB
3959 }
3960
43234a1e
L
3961 switch (i.tm.opcode_modifier.evex)
3962 {
3963 case EVEXLIG: /* LL' is ignored */
3964 vec_length = evexlig << 5;
3965 break;
3966 case EVEX128:
3967 vec_length = 0 << 5;
3968 break;
3969 case EVEX256:
3970 vec_length = 1 << 5;
3971 break;
3972 case EVEX512:
3973 vec_length = 2 << 5;
3974 break;
3975 default:
3976 abort ();
3977 break;
3978 }
3979 i.vex.bytes[3] |= vec_length;
3980 /* Encode the broadcast bit. */
a5748e0d 3981 if (i.broadcast.bytes)
43234a1e
L
3982 i.vex.bytes[3] |= 0x10;
3983 }
ca5312a2
JB
3984 else if (i.rounding.type != saeonly)
3985 i.vex.bytes[3] |= 0x10 | (i.rounding.type << 5);
43234a1e 3986 else
ca5312a2 3987 i.vex.bytes[3] |= 0x10 | (evexrcig << 5);
43234a1e 3988
6225c532
JB
3989 if (i.mask.reg)
3990 i.vex.bytes[3] |= i.mask.reg->reg_num;
43234a1e
L
3991}
3992
65da13b5
L
3993static void
3994process_immext (void)
3995{
3996 expressionS *exp;
3997
c0f3af97 3998 /* These AMD 3DNow! and SSE2 instructions have an opcode suffix
65da13b5
L
3999 which is coded in the same place as an 8-bit immediate field
4000 would be. Here we fake an 8-bit immediate operand from the
4001 opcode suffix stored in tm.extension_opcode.
4002
c1e679ec 4003 AVX instructions also use this encoding, for some of
c0f3af97 4004 3 argument instructions. */
65da13b5 4005
43234a1e 4006 gas_assert (i.imm_operands <= 1
7ab9ffdd 4007 && (i.operands <= 2
7a8655d2 4008 || (is_any_vex_encoding (&i.tm)
7ab9ffdd 4009 && i.operands <= 4)));
65da13b5
L
4010
4011 exp = &im_expressions[i.imm_operands++];
4012 i.op[i.operands].imms = exp;
be1643ff 4013 i.types[i.operands].bitfield.imm8 = 1;
65da13b5
L
4014 i.operands++;
4015 exp->X_op = O_constant;
4016 exp->X_add_number = i.tm.extension_opcode;
4017 i.tm.extension_opcode = None;
4018}
4019
42164a71
L
4020
4021static int
4022check_hle (void)
4023{
742732c7 4024 switch (i.tm.opcode_modifier.prefixok)
42164a71
L
4025 {
4026 default:
4027 abort ();
742732c7
JB
4028 case PrefixLock:
4029 case PrefixNone:
4030 case PrefixNoTrack:
4031 case PrefixRep:
165de32a
L
4032 as_bad (_("invalid instruction `%s' after `%s'"),
4033 i.tm.name, i.hle_prefix);
42164a71 4034 return 0;
742732c7 4035 case PrefixHLELock:
42164a71
L
4036 if (i.prefix[LOCK_PREFIX])
4037 return 1;
165de32a 4038 as_bad (_("missing `lock' with `%s'"), i.hle_prefix);
42164a71 4039 return 0;
742732c7 4040 case PrefixHLEAny:
42164a71 4041 return 1;
742732c7 4042 case PrefixHLERelease:
42164a71
L
4043 if (i.prefix[HLE_PREFIX] != XRELEASE_PREFIX_OPCODE)
4044 {
4045 as_bad (_("instruction `%s' after `xacquire' not allowed"),
4046 i.tm.name);
4047 return 0;
4048 }
8dc0818e 4049 if (i.mem_operands == 0 || !(i.flags[i.operands - 1] & Operand_Mem))
42164a71
L
4050 {
4051 as_bad (_("memory destination needed for instruction `%s'"
4052 " after `xrelease'"), i.tm.name);
4053 return 0;
4054 }
4055 return 1;
4056 }
4057}
4058
c8480b58
L
4059/* Encode aligned vector move as unaligned vector move. */
4060
4061static void
4062encode_with_unaligned_vector_move (void)
4063{
4064 switch (i.tm.base_opcode)
4065 {
b3a9fe6f
L
4066 case 0x28: /* Load instructions. */
4067 case 0x29: /* Store instructions. */
c8480b58
L
4068 /* movaps/movapd/vmovaps/vmovapd. */
4069 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4070 && i.tm.opcode_modifier.opcodeprefix <= PREFIX_0X66)
b3a9fe6f 4071 i.tm.base_opcode = 0x10 | (i.tm.base_opcode & 1);
c8480b58 4072 break;
b3a9fe6f
L
4073 case 0x6f: /* Load instructions. */
4074 case 0x7f: /* Store instructions. */
c8480b58
L
4075 /* movdqa/vmovdqa/vmovdqa64/vmovdqa32. */
4076 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4077 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0X66)
4078 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
4079 break;
4080 default:
4081 break;
4082 }
4083}
4084
b6f8c7c4
L
4085/* Try the shortest encoding by shortening operand size. */
4086
4087static void
4088optimize_encoding (void)
4089{
a0a1771e 4090 unsigned int j;
b6f8c7c4 4091
fe134c65
JB
4092 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
4093 && i.tm.base_opcode == 0x8d)
4094 {
4095 /* Optimize: -O:
4096 lea symbol, %rN -> mov $symbol, %rN
4097 lea (%rM), %rN -> mov %rM, %rN
4098 lea (,%rM,1), %rN -> mov %rM, %rN
4099
4100 and in 32-bit mode for 16-bit addressing
4101
4102 lea (%rM), %rN -> movzx %rM, %rN
4103
4104 and in 64-bit mode zap 32-bit addressing in favor of using a
4105 32-bit (or less) destination.
4106 */
4107 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
4108 {
4109 if (!i.op[1].regs->reg_type.bitfield.word)
4110 i.tm.opcode_modifier.size = SIZE32;
4111 i.prefix[ADDR_PREFIX] = 0;
4112 }
4113
4114 if (!i.index_reg && !i.base_reg)
4115 {
4116 /* Handle:
4117 lea symbol, %rN -> mov $symbol, %rN
4118 */
4119 if (flag_code == CODE_64BIT)
4120 {
4121 /* Don't transform a relocation to a 16-bit one. */
4122 if (i.op[0].disps
4123 && i.op[0].disps->X_op != O_constant
4124 && i.op[1].regs->reg_type.bitfield.word)
4125 return;
4126
4127 if (!i.op[1].regs->reg_type.bitfield.qword
4128 || i.tm.opcode_modifier.size == SIZE32)
4129 {
4130 i.tm.base_opcode = 0xb8;
4131 i.tm.opcode_modifier.modrm = 0;
4132 if (!i.op[1].regs->reg_type.bitfield.word)
4133 i.types[0].bitfield.imm32 = 1;
4134 else
4135 {
4136 i.tm.opcode_modifier.size = SIZE16;
4137 i.types[0].bitfield.imm16 = 1;
4138 }
4139 }
4140 else
4141 {
4142 /* Subject to further optimization below. */
4143 i.tm.base_opcode = 0xc7;
4144 i.tm.extension_opcode = 0;
4145 i.types[0].bitfield.imm32s = 1;
4146 i.types[0].bitfield.baseindex = 0;
4147 }
4148 }
4149 /* Outside of 64-bit mode address and operand sizes have to match if
4150 a relocation is involved, as otherwise we wouldn't (currently) or
4151 even couldn't express the relocation correctly. */
4152 else if (i.op[0].disps
4153 && i.op[0].disps->X_op != O_constant
4154 && ((!i.prefix[ADDR_PREFIX])
4155 != (flag_code == CODE_32BIT
4156 ? i.op[1].regs->reg_type.bitfield.dword
4157 : i.op[1].regs->reg_type.bitfield.word)))
4158 return;
7772f168
JB
4159 /* In 16-bit mode converting LEA with 16-bit addressing and a 32-bit
4160 destination is going to grow encoding size. */
4161 else if (flag_code == CODE_16BIT
4162 && (optimize <= 1 || optimize_for_space)
4163 && !i.prefix[ADDR_PREFIX]
4164 && i.op[1].regs->reg_type.bitfield.dword)
4165 return;
fe134c65
JB
4166 else
4167 {
4168 i.tm.base_opcode = 0xb8;
4169 i.tm.opcode_modifier.modrm = 0;
4170 if (i.op[1].regs->reg_type.bitfield.dword)
4171 i.types[0].bitfield.imm32 = 1;
4172 else
4173 i.types[0].bitfield.imm16 = 1;
4174
4175 if (i.op[0].disps
4176 && i.op[0].disps->X_op == O_constant
4177 && i.op[1].regs->reg_type.bitfield.dword
60cfa10c
L
4178 /* NB: Add () to !i.prefix[ADDR_PREFIX] to silence
4179 GCC 5. */
4180 && (!i.prefix[ADDR_PREFIX]) != (flag_code == CODE_32BIT))
fe134c65
JB
4181 i.op[0].disps->X_add_number &= 0xffff;
4182 }
4183
4184 i.tm.operand_types[0] = i.types[0];
4185 i.imm_operands = 1;
4186 if (!i.op[0].imms)
4187 {
4188 i.op[0].imms = &im_expressions[0];
4189 i.op[0].imms->X_op = O_absent;
4190 }
4191 }
4192 else if (i.op[0].disps
4193 && (i.op[0].disps->X_op != O_constant
4194 || i.op[0].disps->X_add_number))
4195 return;
4196 else
4197 {
4198 /* Handle:
4199 lea (%rM), %rN -> mov %rM, %rN
4200 lea (,%rM,1), %rN -> mov %rM, %rN
4201 lea (%rM), %rN -> movzx %rM, %rN
4202 */
4203 const reg_entry *addr_reg;
4204
4205 if (!i.index_reg && i.base_reg->reg_num != RegIP)
4206 addr_reg = i.base_reg;
4207 else if (!i.base_reg
4208 && i.index_reg->reg_num != RegIZ
4209 && !i.log2_scale_factor)
4210 addr_reg = i.index_reg;
4211 else
4212 return;
4213
4214 if (addr_reg->reg_type.bitfield.word
4215 && i.op[1].regs->reg_type.bitfield.dword)
4216 {
4217 if (flag_code != CODE_32BIT)
4218 return;
4219 i.tm.opcode_modifier.opcodespace = SPACE_0F;
4220 i.tm.base_opcode = 0xb7;
4221 }
4222 else
4223 i.tm.base_opcode = 0x8b;
4224
4225 if (addr_reg->reg_type.bitfield.dword
4226 && i.op[1].regs->reg_type.bitfield.qword)
4227 i.tm.opcode_modifier.size = SIZE32;
4228
4229 i.op[0].regs = addr_reg;
4230 i.reg_operands = 2;
4231 }
4232
4233 i.mem_operands = 0;
4234 i.disp_operands = 0;
4235 i.prefix[ADDR_PREFIX] = 0;
4236 i.prefix[SEG_PREFIX] = 0;
4237 i.seg[0] = NULL;
4238 }
4239
b6f8c7c4 4240 if (optimize_for_space
389d00a5 4241 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
b6f8c7c4
L
4242 && i.reg_operands == 1
4243 && i.imm_operands == 1
4244 && !i.types[1].bitfield.byte
4245 && i.op[0].imms->X_op == O_constant
4246 && fits_in_imm7 (i.op[0].imms->X_add_number)
72aea328 4247 && (i.tm.base_opcode == 0xa8
b6f8c7c4
L
4248 || (i.tm.base_opcode == 0xf6
4249 && i.tm.extension_opcode == 0x0)))
4250 {
4251 /* Optimize: -Os:
4252 test $imm7, %r64/%r32/%r16 -> test $imm7, %r8
4253 */
4254 unsigned int base_regnum = i.op[1].regs->reg_num;
4255 if (flag_code == CODE_64BIT || base_regnum < 4)
4256 {
4257 i.types[1].bitfield.byte = 1;
4258 /* Ignore the suffix. */
4259 i.suffix = 0;
7697afb6
JB
4260 /* Convert to byte registers. */
4261 if (i.types[1].bitfield.word)
4262 j = 16;
4263 else if (i.types[1].bitfield.dword)
4264 j = 32;
4265 else
4266 j = 48;
4267 if (!(i.op[1].regs->reg_flags & RegRex) && base_regnum < 4)
4268 j += 8;
4269 i.op[1].regs -= j;
b6f8c7c4
L
4270 }
4271 }
4272 else if (flag_code == CODE_64BIT
389d00a5 4273 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
d3d50934
L
4274 && ((i.types[1].bitfield.qword
4275 && i.reg_operands == 1
b6f8c7c4
L
4276 && i.imm_operands == 1
4277 && i.op[0].imms->X_op == O_constant
507916b8 4278 && ((i.tm.base_opcode == 0xb8
b6f8c7c4
L
4279 && i.tm.extension_opcode == None
4280 && fits_in_unsigned_long (i.op[0].imms->X_add_number))
4281 || (fits_in_imm31 (i.op[0].imms->X_add_number)
72aea328
JB
4282 && ((i.tm.base_opcode == 0x24
4283 || i.tm.base_opcode == 0xa8)
b6f8c7c4
L
4284 || (i.tm.base_opcode == 0x80
4285 && i.tm.extension_opcode == 0x4)
4286 || ((i.tm.base_opcode == 0xf6
507916b8 4287 || (i.tm.base_opcode | 1) == 0xc7)
b8364fa7
JB
4288 && i.tm.extension_opcode == 0x0)))
4289 || (fits_in_imm7 (i.op[0].imms->X_add_number)
4290 && i.tm.base_opcode == 0x83
4291 && i.tm.extension_opcode == 0x4)))
d3d50934
L
4292 || (i.types[0].bitfield.qword
4293 && ((i.reg_operands == 2
4294 && i.op[0].regs == i.op[1].regs
72aea328
JB
4295 && (i.tm.base_opcode == 0x30
4296 || i.tm.base_opcode == 0x28))
d3d50934
L
4297 || (i.reg_operands == 1
4298 && i.operands == 1
72aea328 4299 && i.tm.base_opcode == 0x30)))))
b6f8c7c4
L
4300 {
4301 /* Optimize: -O:
4302 andq $imm31, %r64 -> andl $imm31, %r32
b8364fa7 4303 andq $imm7, %r64 -> andl $imm7, %r32
b6f8c7c4
L
4304 testq $imm31, %r64 -> testl $imm31, %r32
4305 xorq %r64, %r64 -> xorl %r32, %r32
4306 subq %r64, %r64 -> subl %r32, %r32
4307 movq $imm31, %r64 -> movl $imm31, %r32
4308 movq $imm32, %r64 -> movl $imm32, %r32
4309 */
04784e33
JB
4310 i.tm.opcode_modifier.size = SIZE32;
4311 if (i.imm_operands)
4312 {
4313 i.types[0].bitfield.imm32 = 1;
4314 i.types[0].bitfield.imm32s = 0;
4315 i.types[0].bitfield.imm64 = 0;
4316 }
4317 else
4318 {
4319 i.types[0].bitfield.dword = 1;
4320 i.types[0].bitfield.qword = 0;
4321 }
4322 i.types[1].bitfield.dword = 1;
4323 i.types[1].bitfield.qword = 0;
507916b8 4324 if (i.tm.base_opcode == 0xb8 || (i.tm.base_opcode | 1) == 0xc7)
b6f8c7c4
L
4325 {
4326 /* Handle
4327 movq $imm31, %r64 -> movl $imm31, %r32
4328 movq $imm32, %r64 -> movl $imm32, %r32
4329 */
4330 i.tm.operand_types[0].bitfield.imm32 = 1;
4331 i.tm.operand_types[0].bitfield.imm32s = 0;
4332 i.tm.operand_types[0].bitfield.imm64 = 0;
507916b8 4333 if ((i.tm.base_opcode | 1) == 0xc7)
b6f8c7c4
L
4334 {
4335 /* Handle
4336 movq $imm31, %r64 -> movl $imm31, %r32
4337 */
507916b8 4338 i.tm.base_opcode = 0xb8;
b6f8c7c4 4339 i.tm.extension_opcode = None;
507916b8 4340 i.tm.opcode_modifier.w = 0;
b6f8c7c4
L
4341 i.tm.opcode_modifier.modrm = 0;
4342 }
4343 }
4344 }
5641ec01
JB
4345 else if (optimize > 1
4346 && !optimize_for_space
389d00a5 4347 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
5641ec01
JB
4348 && i.reg_operands == 2
4349 && i.op[0].regs == i.op[1].regs
4350 && ((i.tm.base_opcode & ~(Opcode_D | 1)) == 0x8
4351 || (i.tm.base_opcode & ~(Opcode_D | 1)) == 0x20)
4352 && (flag_code != CODE_64BIT || !i.types[0].bitfield.dword))
4353 {
4354 /* Optimize: -O2:
4355 andb %rN, %rN -> testb %rN, %rN
4356 andw %rN, %rN -> testw %rN, %rN
4357 andq %rN, %rN -> testq %rN, %rN
4358 orb %rN, %rN -> testb %rN, %rN
4359 orw %rN, %rN -> testw %rN, %rN
4360 orq %rN, %rN -> testq %rN, %rN
4361
4362 and outside of 64-bit mode
4363
4364 andl %rN, %rN -> testl %rN, %rN
4365 orl %rN, %rN -> testl %rN, %rN
4366 */
4367 i.tm.base_opcode = 0x84 | (i.tm.base_opcode & 1);
4368 }
99112332 4369 else if (i.reg_operands == 3
b6f8c7c4
L
4370 && i.op[0].regs == i.op[1].regs
4371 && !i.types[2].bitfield.xmmword
4372 && (i.tm.opcode_modifier.vex
6225c532 4373 || ((!i.mask.reg || i.mask.zeroing)
e771e7c9 4374 && is_evex_encoding (&i.tm)
80c34c38 4375 && (i.vec_encoding != vex_encoding_evex
dd22218c 4376 || cpu_arch_isa_flags.bitfield.cpuavx512vl
80c34c38 4377 || i.tm.cpu_flags.bitfield.cpuavx512vl
7091c612 4378 || (i.tm.operand_types[2].bitfield.zmmword
dd22218c 4379 && i.types[2].bitfield.ymmword))))
5844ccaa
JB
4380 && i.tm.opcode_modifier.opcodespace == SPACE_0F
4381 && ((i.tm.base_opcode | 2) == 0x57
4382 || i.tm.base_opcode == 0xdf
4383 || i.tm.base_opcode == 0xef
4384 || (i.tm.base_opcode | 3) == 0xfb
4385 || i.tm.base_opcode == 0x42
4386 || i.tm.base_opcode == 0x47))
b6f8c7c4 4387 {
99112332 4388 /* Optimize: -O1:
8305403a
L
4389 VOP, one of vandnps, vandnpd, vxorps, vxorpd, vpsubb, vpsubd,
4390 vpsubq and vpsubw:
b6f8c7c4
L
4391 EVEX VOP %zmmM, %zmmM, %zmmN
4392 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4393 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4394 EVEX VOP %ymmM, %ymmM, %ymmN
4395 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4396 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4397 VEX VOP %ymmM, %ymmM, %ymmN
4398 -> VEX VOP %xmmM, %xmmM, %xmmN
4399 VOP, one of vpandn and vpxor:
4400 VEX VOP %ymmM, %ymmM, %ymmN
4401 -> VEX VOP %xmmM, %xmmM, %xmmN
4402 VOP, one of vpandnd and vpandnq:
4403 EVEX VOP %zmmM, %zmmM, %zmmN
4404 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4405 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4406 EVEX VOP %ymmM, %ymmM, %ymmN
4407 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4408 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4409 VOP, one of vpxord and vpxorq:
4410 EVEX VOP %zmmM, %zmmM, %zmmN
4411 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4412 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4413 EVEX VOP %ymmM, %ymmM, %ymmN
4414 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4415 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
1424ad86
JB
4416 VOP, one of kxord and kxorq:
4417 VEX VOP %kM, %kM, %kN
4418 -> VEX kxorw %kM, %kM, %kN
4419 VOP, one of kandnd and kandnq:
4420 VEX VOP %kM, %kM, %kN
4421 -> VEX kandnw %kM, %kM, %kN
b6f8c7c4 4422 */
e771e7c9 4423 if (is_evex_encoding (&i.tm))
b6f8c7c4 4424 {
7b1d7ca1 4425 if (i.vec_encoding != vex_encoding_evex)
b6f8c7c4
L
4426 {
4427 i.tm.opcode_modifier.vex = VEX128;
4428 i.tm.opcode_modifier.vexw = VEXW0;
4429 i.tm.opcode_modifier.evex = 0;
4430 }
7b1d7ca1 4431 else if (optimize > 1)
dd22218c
L
4432 i.tm.opcode_modifier.evex = EVEX128;
4433 else
4434 return;
b6f8c7c4 4435 }
f74a6307 4436 else if (i.tm.operand_types[0].bitfield.class == RegMask)
1424ad86 4437 {
35648716 4438 i.tm.opcode_modifier.opcodeprefix = PREFIX_NONE;
1424ad86
JB
4439 i.tm.opcode_modifier.vexw = VEXW0;
4440 }
b6f8c7c4
L
4441 else
4442 i.tm.opcode_modifier.vex = VEX128;
4443
4444 if (i.tm.opcode_modifier.vex)
4445 for (j = 0; j < 3; j++)
4446 {
4447 i.types[j].bitfield.xmmword = 1;
4448 i.types[j].bitfield.ymmword = 0;
4449 }
4450 }
392a5972 4451 else if (i.vec_encoding != vex_encoding_evex
97ed31ae 4452 && !i.types[0].bitfield.zmmword
392a5972 4453 && !i.types[1].bitfield.zmmword
6225c532 4454 && !i.mask.reg
a5748e0d 4455 && !i.broadcast.bytes
97ed31ae 4456 && is_evex_encoding (&i.tm)
35648716
JB
4457 && ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4458 || (i.tm.base_opcode & ~4) == 0xdb
4459 || (i.tm.base_opcode & ~4) == 0xeb)
97ed31ae
L
4460 && i.tm.extension_opcode == None)
4461 {
4462 /* Optimize: -O1:
4463 VOP, one of vmovdqa32, vmovdqa64, vmovdqu8, vmovdqu16,
4464 vmovdqu32 and vmovdqu64:
4465 EVEX VOP %xmmM, %xmmN
4466 -> VEX vmovdqa|vmovdqu %xmmM, %xmmN (M and N < 16)
4467 EVEX VOP %ymmM, %ymmN
4468 -> VEX vmovdqa|vmovdqu %ymmM, %ymmN (M and N < 16)
4469 EVEX VOP %xmmM, mem
4470 -> VEX vmovdqa|vmovdqu %xmmM, mem (M < 16)
4471 EVEX VOP %ymmM, mem
4472 -> VEX vmovdqa|vmovdqu %ymmM, mem (M < 16)
4473 EVEX VOP mem, %xmmN
4474 -> VEX mvmovdqa|vmovdquem, %xmmN (N < 16)
4475 EVEX VOP mem, %ymmN
4476 -> VEX vmovdqa|vmovdqu mem, %ymmN (N < 16)
a0a1771e
JB
4477 VOP, one of vpand, vpandn, vpor, vpxor:
4478 EVEX VOP{d,q} %xmmL, %xmmM, %xmmN
4479 -> VEX VOP %xmmL, %xmmM, %xmmN (L, M, and N < 16)
4480 EVEX VOP{d,q} %ymmL, %ymmM, %ymmN
4481 -> VEX VOP %ymmL, %ymmM, %ymmN (L, M, and N < 16)
4482 EVEX VOP{d,q} mem, %xmmM, %xmmN
4483 -> VEX VOP mem, %xmmM, %xmmN (M and N < 16)
4484 EVEX VOP{d,q} mem, %ymmM, %ymmN
4485 -> VEX VOP mem, %ymmM, %ymmN (M and N < 16)
97ed31ae 4486 */
a0a1771e 4487 for (j = 0; j < i.operands; j++)
392a5972
L
4488 if (operand_type_check (i.types[j], disp)
4489 && i.op[j].disps->X_op == O_constant)
4490 {
4491 /* Since the VEX prefix has 2 or 3 bytes, the EVEX prefix
4492 has 4 bytes, EVEX Disp8 has 1 byte and VEX Disp32 has 4
4493 bytes, we choose EVEX Disp8 over VEX Disp32. */
4494 int evex_disp8, vex_disp8;
4495 unsigned int memshift = i.memshift;
4496 offsetT n = i.op[j].disps->X_add_number;
4497
4498 evex_disp8 = fits_in_disp8 (n);
4499 i.memshift = 0;
4500 vex_disp8 = fits_in_disp8 (n);
4501 if (evex_disp8 != vex_disp8)
4502 {
4503 i.memshift = memshift;
4504 return;
4505 }
4506
4507 i.types[j].bitfield.disp8 = vex_disp8;
4508 break;
4509 }
35648716
JB
4510 if ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4511 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2)
4512 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
97ed31ae
L
4513 i.tm.opcode_modifier.vex
4514 = i.types[0].bitfield.ymmword ? VEX256 : VEX128;
4515 i.tm.opcode_modifier.vexw = VEXW0;
79dec6b7 4516 /* VPAND, VPOR, and VPXOR are commutative. */
35648716 4517 if (i.reg_operands == 3 && i.tm.base_opcode != 0xdf)
79dec6b7 4518 i.tm.opcode_modifier.commutative = 1;
97ed31ae
L
4519 i.tm.opcode_modifier.evex = 0;
4520 i.tm.opcode_modifier.masking = 0;
a0a1771e 4521 i.tm.opcode_modifier.broadcast = 0;
97ed31ae
L
4522 i.tm.opcode_modifier.disp8memshift = 0;
4523 i.memshift = 0;
a0a1771e
JB
4524 if (j < i.operands)
4525 i.types[j].bitfield.disp8
4526 = fits_in_disp8 (i.op[j].disps->X_add_number);
97ed31ae 4527 }
b6f8c7c4
L
4528}
4529
ae531041
L
4530/* Return non-zero for load instruction. */
4531
4532static int
4533load_insn_p (void)
4534{
4535 unsigned int dest;
4536 int any_vex_p = is_any_vex_encoding (&i.tm);
4537 unsigned int base_opcode = i.tm.base_opcode | 1;
4538
4539 if (!any_vex_p)
4540 {
ef07be45
CL
4541 /* Anysize insns: lea, invlpg, clflush, prefetch*, bndmk, bndcl, bndcu,
4542 bndcn, bndstx, bndldx, clflushopt, clwb, cldemote. */
255571cd 4543 if (i.tm.opcode_modifier.operandconstraint == ANY_SIZE)
ae531041
L
4544 return 0;
4545
389d00a5
JB
4546 /* pop. */
4547 if (strcmp (i.tm.name, "pop") == 0)
4548 return 1;
4549 }
4550
4551 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE)
4552 {
4553 /* popf, popa. */
4554 if (i.tm.base_opcode == 0x9d
a09f656b 4555 || i.tm.base_opcode == 0x61)
ae531041
L
4556 return 1;
4557
4558 /* movs, cmps, lods, scas. */
4559 if ((i.tm.base_opcode | 0xb) == 0xaf)
4560 return 1;
4561
a09f656b 4562 /* outs, xlatb. */
4563 if (base_opcode == 0x6f
4564 || i.tm.base_opcode == 0xd7)
ae531041 4565 return 1;
a09f656b 4566 /* NB: For AMD-specific insns with implicit memory operands,
4567 they're intentionally not covered. */
ae531041
L
4568 }
4569
4570 /* No memory operand. */
4571 if (!i.mem_operands)
4572 return 0;
4573
4574 if (any_vex_p)
4575 {
4576 /* vldmxcsr. */
4577 if (i.tm.base_opcode == 0xae
4578 && i.tm.opcode_modifier.vex
441f6aca 4579 && i.tm.opcode_modifier.opcodespace == SPACE_0F
35648716 4580 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
ae531041
L
4581 && i.tm.extension_opcode == 2)
4582 return 1;
4583 }
389d00a5 4584 else if (i.tm.opcode_modifier.opcodespace == SPACE_BASE)
ae531041
L
4585 {
4586 /* test, not, neg, mul, imul, div, idiv. */
4587 if ((i.tm.base_opcode == 0xf6 || i.tm.base_opcode == 0xf7)
4588 && i.tm.extension_opcode != 1)
4589 return 1;
4590
4591 /* inc, dec. */
4592 if (base_opcode == 0xff && i.tm.extension_opcode <= 1)
4593 return 1;
4594
4595 /* add, or, adc, sbb, and, sub, xor, cmp. */
4596 if (i.tm.base_opcode >= 0x80 && i.tm.base_opcode <= 0x83)
4597 return 1;
4598
ae531041
L
4599 /* rol, ror, rcl, rcr, shl/sal, shr, sar. */
4600 if ((base_opcode == 0xc1
4601 || (i.tm.base_opcode >= 0xd0 && i.tm.base_opcode <= 0xd3))
4602 && i.tm.extension_opcode != 6)
4603 return 1;
4604
ae531041 4605 /* Check for x87 instructions. */
389d00a5 4606 if (base_opcode >= 0xd8 && base_opcode <= 0xdf)
ae531041
L
4607 {
4608 /* Skip fst, fstp, fstenv, fstcw. */
4609 if (i.tm.base_opcode == 0xd9
4610 && (i.tm.extension_opcode == 2
4611 || i.tm.extension_opcode == 3
4612 || i.tm.extension_opcode == 6
4613 || i.tm.extension_opcode == 7))
4614 return 0;
4615
4616 /* Skip fisttp, fist, fistp, fstp. */
4617 if (i.tm.base_opcode == 0xdb
4618 && (i.tm.extension_opcode == 1
4619 || i.tm.extension_opcode == 2
4620 || i.tm.extension_opcode == 3
4621 || i.tm.extension_opcode == 7))
4622 return 0;
4623
4624 /* Skip fisttp, fst, fstp, fsave, fstsw. */
4625 if (i.tm.base_opcode == 0xdd
4626 && (i.tm.extension_opcode == 1
4627 || i.tm.extension_opcode == 2
4628 || i.tm.extension_opcode == 3
4629 || i.tm.extension_opcode == 6
4630 || i.tm.extension_opcode == 7))
4631 return 0;
4632
4633 /* Skip fisttp, fist, fistp, fbstp, fistp. */
4634 if (i.tm.base_opcode == 0xdf
4635 && (i.tm.extension_opcode == 1
4636 || i.tm.extension_opcode == 2
4637 || i.tm.extension_opcode == 3
4638 || i.tm.extension_opcode == 6
4639 || i.tm.extension_opcode == 7))
4640 return 0;
4641
4642 return 1;
4643 }
4644 }
389d00a5
JB
4645 else if (i.tm.opcode_modifier.opcodespace == SPACE_0F)
4646 {
4647 /* bt, bts, btr, btc. */
4648 if (i.tm.base_opcode == 0xba
4649 && (i.tm.extension_opcode >= 4 && i.tm.extension_opcode <= 7))
4650 return 1;
4651
4652 /* cmpxchg8b, cmpxchg16b, xrstors, vmptrld. */
4653 if (i.tm.base_opcode == 0xc7
4654 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
4655 && (i.tm.extension_opcode == 1 || i.tm.extension_opcode == 3
4656 || i.tm.extension_opcode == 6))
4657 return 1;
4658
4659 /* fxrstor, ldmxcsr, xrstor. */
4660 if (i.tm.base_opcode == 0xae
4661 && (i.tm.extension_opcode == 1
4662 || i.tm.extension_opcode == 2
4663 || i.tm.extension_opcode == 5))
4664 return 1;
4665
4666 /* lgdt, lidt, lmsw. */
4667 if (i.tm.base_opcode == 0x01
4668 && (i.tm.extension_opcode == 2
4669 || i.tm.extension_opcode == 3
4670 || i.tm.extension_opcode == 6))
4671 return 1;
4672 }
ae531041
L
4673
4674 dest = i.operands - 1;
4675
4676 /* Check fake imm8 operand and 3 source operands. */
4677 if ((i.tm.opcode_modifier.immext
4678 || i.tm.opcode_modifier.vexsources == VEX3SOURCES)
4679 && i.types[dest].bitfield.imm8)
4680 dest--;
4681
389d00a5
JB
4682 /* add, or, adc, sbb, and, sub, xor, cmp, test, xchg. */
4683 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
ae531041
L
4684 && (base_opcode == 0x1
4685 || base_opcode == 0x9
4686 || base_opcode == 0x11
4687 || base_opcode == 0x19
4688 || base_opcode == 0x21
4689 || base_opcode == 0x29
4690 || base_opcode == 0x31
4691 || base_opcode == 0x39
389d00a5
JB
4692 || (base_opcode | 2) == 0x87))
4693 return 1;
4694
4695 /* xadd. */
4696 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4697 && base_opcode == 0xc1)
ae531041
L
4698 return 1;
4699
4700 /* Check for load instruction. */
4701 return (i.types[dest].bitfield.class != ClassNone
4702 || i.types[dest].bitfield.instance == Accum);
4703}
4704
4705/* Output lfence, 0xfaee8, after instruction. */
4706
4707static void
4708insert_lfence_after (void)
4709{
4710 if (lfence_after_load && load_insn_p ())
4711 {
a09f656b 4712 /* There are also two REP string instructions that require
4713 special treatment. Specifically, the compare string (CMPS)
4714 and scan string (SCAS) instructions set EFLAGS in a manner
4715 that depends on the data being compared/scanned. When used
4716 with a REP prefix, the number of iterations may therefore
4717 vary depending on this data. If the data is a program secret
4718 chosen by the adversary using an LVI method,
4719 then this data-dependent behavior may leak some aspect
4720 of the secret. */
4721 if (((i.tm.base_opcode | 0x1) == 0xa7
4722 || (i.tm.base_opcode | 0x1) == 0xaf)
4723 && i.prefix[REP_PREFIX])
4724 {
4725 as_warn (_("`%s` changes flags which would affect control flow behavior"),
4726 i.tm.name);
4727 }
ae531041
L
4728 char *p = frag_more (3);
4729 *p++ = 0xf;
4730 *p++ = 0xae;
4731 *p = 0xe8;
4732 }
4733}
4734
4735/* Output lfence, 0xfaee8, before instruction. */
4736
4737static void
4738insert_lfence_before (void)
4739{
4740 char *p;
4741
389d00a5 4742 if (i.tm.opcode_modifier.opcodespace != SPACE_BASE)
ae531041
L
4743 return;
4744
4745 if (i.tm.base_opcode == 0xff
4746 && (i.tm.extension_opcode == 2 || i.tm.extension_opcode == 4))
4747 {
4748 /* Insert lfence before indirect branch if needed. */
4749
4750 if (lfence_before_indirect_branch == lfence_branch_none)
4751 return;
4752
4753 if (i.operands != 1)
4754 abort ();
4755
4756 if (i.reg_operands == 1)
4757 {
4758 /* Indirect branch via register. Don't insert lfence with
4759 -mlfence-after-load=yes. */
4760 if (lfence_after_load
4761 || lfence_before_indirect_branch == lfence_branch_memory)
4762 return;
4763 }
4764 else if (i.mem_operands == 1
4765 && lfence_before_indirect_branch != lfence_branch_register)
4766 {
4767 as_warn (_("indirect `%s` with memory operand should be avoided"),
4768 i.tm.name);
4769 return;
4770 }
4771 else
4772 return;
4773
4774 if (last_insn.kind != last_insn_other
4775 && last_insn.seg == now_seg)
4776 {
4777 as_warn_where (last_insn.file, last_insn.line,
4778 _("`%s` skips -mlfence-before-indirect-branch on `%s`"),
4779 last_insn.name, i.tm.name);
4780 return;
4781 }
4782
4783 p = frag_more (3);
4784 *p++ = 0xf;
4785 *p++ = 0xae;
4786 *p = 0xe8;
4787 return;
4788 }
4789
503648e4 4790 /* Output or/not/shl and lfence before near ret. */
ae531041
L
4791 if (lfence_before_ret != lfence_before_ret_none
4792 && (i.tm.base_opcode == 0xc2
503648e4 4793 || i.tm.base_opcode == 0xc3))
ae531041
L
4794 {
4795 if (last_insn.kind != last_insn_other
4796 && last_insn.seg == now_seg)
4797 {
4798 as_warn_where (last_insn.file, last_insn.line,
4799 _("`%s` skips -mlfence-before-ret on `%s`"),
4800 last_insn.name, i.tm.name);
4801 return;
4802 }
a09f656b 4803
a09f656b 4804 /* Near ret ingore operand size override under CPU64. */
503648e4 4805 char prefix = flag_code == CODE_64BIT
4806 ? 0x48
4807 : i.prefix[DATA_PREFIX] ? 0x66 : 0x0;
a09f656b 4808
4809 if (lfence_before_ret == lfence_before_ret_not)
4810 {
4811 /* not: 0xf71424, may add prefix
4812 for operand size override or 64-bit code. */
4813 p = frag_more ((prefix ? 2 : 0) + 6 + 3);
4814 if (prefix)
4815 *p++ = prefix;
ae531041
L
4816 *p++ = 0xf7;
4817 *p++ = 0x14;
4818 *p++ = 0x24;
a09f656b 4819 if (prefix)
4820 *p++ = prefix;
ae531041
L
4821 *p++ = 0xf7;
4822 *p++ = 0x14;
4823 *p++ = 0x24;
4824 }
a09f656b 4825 else
4826 {
4827 p = frag_more ((prefix ? 1 : 0) + 4 + 3);
4828 if (prefix)
4829 *p++ = prefix;
4830 if (lfence_before_ret == lfence_before_ret_or)
4831 {
4832 /* or: 0x830c2400, may add prefix
4833 for operand size override or 64-bit code. */
4834 *p++ = 0x83;
4835 *p++ = 0x0c;
4836 }
4837 else
4838 {
4839 /* shl: 0xc1242400, may add prefix
4840 for operand size override or 64-bit code. */
4841 *p++ = 0xc1;
4842 *p++ = 0x24;
4843 }
4844
4845 *p++ = 0x24;
4846 *p++ = 0x0;
4847 }
4848
ae531041
L
4849 *p++ = 0xf;
4850 *p++ = 0xae;
4851 *p = 0xe8;
4852 }
4853}
4854
04784e33
JB
4855/* Helper for md_assemble() to decide whether to prepare for a possible 2nd
4856 parsing pass. Instead of introducing a rarely use new insn attribute this
4857 utilizes a common pattern between affected templates. It is deemed
4858 acceptable that this will lead to unnecessary pass 2 preparations in a
4859 limited set of cases. */
4860static INLINE bool may_need_pass2 (const insn_template *t)
4861{
4862 return t->opcode_modifier.sse2avx
4863 /* Note that all SSE2AVX templates have at least one operand. */
a28fedbc
JB
4864 ? t->operand_types[t->operands - 1].bitfield.class == RegSIMD
4865 : (t->opcode_modifier.opcodespace == SPACE_0F
4866 && (t->base_opcode | 1) == 0xbf)
4867 || (t->opcode_modifier.opcodespace == SPACE_BASE
4868 && t->base_opcode == 0x63);
04784e33
JB
4869}
4870
252b5132
RH
4871/* This is the guts of the machine-dependent assembler. LINE points to a
4872 machine dependent instruction. This function is supposed to emit
4873 the frags/bytes it assembles to. */
4874
4875void
65da13b5 4876md_assemble (char *line)
252b5132 4877{
40fb9820 4878 unsigned int j;
9db83a32 4879 char mnemonic[MAX_MNEM_SIZE], mnem_suffix = 0, *copy = NULL;
04784e33
JB
4880 const char *end, *pass1_mnem = NULL;
4881 enum i386_error pass1_err = 0;
d3ce72d0 4882 const insn_template *t;
252b5132 4883
47926f60 4884 /* Initialize globals. */
04784e33
JB
4885 current_templates = NULL;
4886 retry:
252b5132 4887 memset (&i, '\0', sizeof (i));
ca5312a2 4888 i.rounding.type = rc_none;
252b5132 4889 for (j = 0; j < MAX_OPERANDS; j++)
1ae12ab7 4890 i.reloc[j] = NO_RELOC;
252b5132
RH
4891 memset (disp_expressions, '\0', sizeof (disp_expressions));
4892 memset (im_expressions, '\0', sizeof (im_expressions));
ce8a8b2f 4893 save_stack_p = save_stack;
252b5132
RH
4894
4895 /* First parse an instruction mnemonic & call i386_operand for the operands.
4896 We assume that the scrubber has arranged it so that line[0] is the valid
47926f60 4897 start of a (possibly prefixed) mnemonic. */
252b5132 4898
5317ad2c
JB
4899 end = parse_insn (line, mnemonic);
4900 if (end == NULL)
04784e33
JB
4901 {
4902 if (pass1_mnem != NULL)
4903 goto match_error;
9db83a32
JB
4904 if (i.error != no_error)
4905 {
4906 gas_assert (current_templates != NULL);
4907 if (may_need_pass2 (current_templates->start) && !i.suffix)
4908 goto no_match;
4909 /* No point in trying a 2nd pass - it'll only find the same suffix
4910 again. */
4911 mnem_suffix = i.suffix;
4912 goto match_error;
4913 }
04784e33
JB
4914 return;
4915 }
4916 if (may_need_pass2 (current_templates->start))
4917 {
4918 /* Make a copy of the full line in case we need to retry. */
4919 copy = xstrdup (line);
4920 }
5317ad2c 4921 line += end - line;
83b16ac6 4922 mnem_suffix = i.suffix;
252b5132 4923
29b0f896 4924 line = parse_operands (line, mnemonic);
ee86248c 4925 this_operand = -1;
29b0f896 4926 if (line == NULL)
04784e33
JB
4927 {
4928 free (copy);
4929 return;
4930 }
252b5132 4931
29b0f896
AM
4932 /* Now we've parsed the mnemonic into a set of templates, and have the
4933 operands at hand. */
4934
b630c145 4935 /* All Intel opcodes have reversed operands except for "bound", "enter",
c0e54661 4936 "invlpg*", "monitor*", "mwait*", "tpause", "umwait", "pvalidate",
b0e8fa7f
TJ
4937 "rmpadjust", "rmpupdate", and "rmpquery". We also don't reverse
4938 intersegment "jmp" and "call" instructions with 2 immediate operands so
4939 that the immediate segment precedes the offset consistently in Intel and
4940 AT&T modes. */
4d456e3d
L
4941 if (intel_syntax
4942 && i.operands > 1
29b0f896 4943 && (strcmp (mnemonic, "bound") != 0)
c0e54661 4944 && (strncmp (mnemonic, "invlpg", 6) != 0)
d34049e8
ML
4945 && !startswith (mnemonic, "monitor")
4946 && !startswith (mnemonic, "mwait")
c0e54661 4947 && (strcmp (mnemonic, "pvalidate") != 0)
d34049e8 4948 && !startswith (mnemonic, "rmp")
b630c145
JB
4949 && (strcmp (mnemonic, "tpause") != 0)
4950 && (strcmp (mnemonic, "umwait") != 0)
47c0279b
JB
4951 && !(i.operands == 2
4952 && operand_type_check (i.types[0], imm)
40fb9820 4953 && operand_type_check (i.types[1], imm)))
29b0f896
AM
4954 swap_operands ();
4955
ec56d5c0
JB
4956 /* The order of the immediates should be reversed
4957 for 2 immediates extrq and insertq instructions */
4958 if (i.imm_operands == 2
4959 && (strcmp (mnemonic, "extrq") == 0
4960 || strcmp (mnemonic, "insertq") == 0))
4961 swap_2_operands (0, 1);
4962
29b0f896
AM
4963 if (i.imm_operands)
4964 optimize_imm ();
4965
9386188e
JB
4966 if (i.disp_operands && !want_disp32 (current_templates->start)
4967 && (!current_templates->start->opcode_modifier.jump
4968 || i.jumpabsolute || i.types[0].bitfield.baseindex))
cce08655
JB
4969 {
4970 for (j = 0; j < i.operands; ++j)
4971 {
4972 const expressionS *exp = i.op[j].disps;
4973
4974 if (!operand_type_check (i.types[j], disp))
4975 continue;
4976
4977 if (exp->X_op != O_constant)
4978 continue;
4979
4980 /* Since displacement is signed extended to 64bit, don't allow
a775efc8 4981 disp32 if it is out of range. */
cce08655
JB
4982 if (fits_in_signed_long (exp->X_add_number))
4983 continue;
4984
a775efc8 4985 i.types[j].bitfield.disp32 = 0;
cce08655
JB
4986 if (i.types[j].bitfield.baseindex)
4987 {
f493c217
AM
4988 as_bad (_("0x%" PRIx64 " out of range of signed 32bit displacement"),
4989 (uint64_t) exp->X_add_number);
cce08655
JB
4990 return;
4991 }
4992 }
4993 }
4994
b300c311
L
4995 /* Don't optimize displacement for movabs since it only takes 64bit
4996 displacement. */
4997 if (i.disp_operands
1a42a9fe 4998 && i.disp_encoding <= disp_encoding_8bit
862be3fb
L
4999 && (flag_code != CODE_64BIT
5000 || strcmp (mnemonic, "movabs") != 0))
5001 optimize_disp ();
29b0f896
AM
5002
5003 /* Next, we find a template that matches the given insn,
5004 making sure the overlap of the given operands types is consistent
5005 with the template operand types. */
252b5132 5006
83b16ac6 5007 if (!(t = match_template (mnem_suffix)))
04784e33
JB
5008 {
5009 const char *err_msg;
5010
5011 if (copy && !mnem_suffix)
5012 {
5013 line = copy;
5014 copy = NULL;
9db83a32 5015 no_match:
04784e33
JB
5016 pass1_err = i.error;
5017 pass1_mnem = current_templates->start->name;
5018 goto retry;
5019 }
9db83a32
JB
5020
5021 /* If a non-/only-64bit template (group) was found in pass 1, and if
5022 _some_ template (group) was found in pass 2, squash pass 1's
5023 error. */
5024 if (pass1_err == unsupported_64bit)
5025 pass1_mnem = NULL;
5026
04784e33 5027 match_error:
9db83a32
JB
5028 free (copy);
5029
04784e33
JB
5030 switch (pass1_mnem ? pass1_err : i.error)
5031 {
5032 default:
5033 abort ();
5034 case operand_size_mismatch:
5035 err_msg = _("operand size mismatch");
5036 break;
5037 case operand_type_mismatch:
5038 err_msg = _("operand type mismatch");
5039 break;
5040 case register_type_mismatch:
5041 err_msg = _("register type mismatch");
5042 break;
5043 case number_of_operands_mismatch:
5044 err_msg = _("number of operands mismatch");
5045 break;
5046 case invalid_instruction_suffix:
5047 err_msg = _("invalid instruction suffix");
5048 break;
5049 case bad_imm4:
5050 err_msg = _("constant doesn't fit in 4 bits");
5051 break;
5052 case unsupported_with_intel_mnemonic:
5053 err_msg = _("unsupported with Intel mnemonic");
5054 break;
5055 case unsupported_syntax:
5056 err_msg = _("unsupported syntax");
5057 break;
5058 case unsupported:
5059 as_bad (_("unsupported instruction `%s'"),
5060 pass1_mnem ? pass1_mnem : current_templates->start->name);
5061 return;
9db83a32
JB
5062 case unsupported_on_arch:
5063 as_bad (_("`%s' is not supported on `%s%s'"),
5064 pass1_mnem ? pass1_mnem : current_templates->start->name,
5065 cpu_arch_name ? cpu_arch_name : default_arch,
5066 cpu_sub_arch_name ? cpu_sub_arch_name : "");
5067 return;
5068 case unsupported_64bit:
5069 if (ISLOWER (mnem_suffix))
5070 as_bad (_("`%s%c' is %s supported in 64-bit mode"),
5071 pass1_mnem ? pass1_mnem : current_templates->start->name,
5072 mnem_suffix,
5073 flag_code == CODE_64BIT ? _("not") : _("only"));
5074 else
5075 as_bad (_("`%s' is %s supported in 64-bit mode"),
5076 pass1_mnem ? pass1_mnem : current_templates->start->name,
5077 flag_code == CODE_64BIT ? _("not") : _("only"));
5078 return;
04784e33
JB
5079 case invalid_sib_address:
5080 err_msg = _("invalid SIB address");
5081 break;
5082 case invalid_vsib_address:
5083 err_msg = _("invalid VSIB address");
5084 break;
5085 case invalid_vector_register_set:
5086 err_msg = _("mask, index, and destination registers must be distinct");
5087 break;
5088 case invalid_tmm_register_set:
5089 err_msg = _("all tmm registers must be distinct");
5090 break;
5091 case invalid_dest_and_src_register_set:
5092 err_msg = _("destination and source registers must be distinct");
5093 break;
5094 case unsupported_vector_index_register:
5095 err_msg = _("unsupported vector index register");
5096 break;
5097 case unsupported_broadcast:
5098 err_msg = _("unsupported broadcast");
5099 break;
5100 case broadcast_needed:
5101 err_msg = _("broadcast is needed for operand of such type");
5102 break;
5103 case unsupported_masking:
5104 err_msg = _("unsupported masking");
5105 break;
5106 case mask_not_on_destination:
5107 err_msg = _("mask not on destination operand");
5108 break;
5109 case no_default_mask:
5110 err_msg = _("default mask isn't allowed");
5111 break;
5112 case unsupported_rc_sae:
5113 err_msg = _("unsupported static rounding/sae");
5114 break;
5115 case invalid_register_operand:
5116 err_msg = _("invalid register operand");
5117 break;
5118 }
5119 as_bad (_("%s for `%s'"), err_msg,
5120 pass1_mnem ? pass1_mnem : current_templates->start->name);
5121 return;
5122 }
5123
5124 free (copy);
252b5132 5125
7bab8ab5 5126 if (sse_check != check_none
ffb86450
JB
5127 /* The opcode space check isn't strictly needed; it's there only to
5128 bypass the logic below when easily possible. */
5129 && t->opcode_modifier.opcodespace >= SPACE_0F
5130 && t->opcode_modifier.opcodespace <= SPACE_0F3A
5131 && !i.tm.cpu_flags.bitfield.cpusse4a
5132 && !is_any_vex_encoding (t))
daf50ae7 5133 {
ffb86450
JB
5134 bool simd = false;
5135
5136 for (j = 0; j < t->operands; ++j)
5137 {
5138 if (t->operand_types[j].bitfield.class == RegMMX)
5139 break;
5140 if (t->operand_types[j].bitfield.class == RegSIMD)
5141 simd = true;
5142 }
5143
5144 if (j >= t->operands && simd)
5145 (sse_check == check_warning
5146 ? as_warn
5147 : as_bad) (_("SSE instruction `%s' is used"), i.tm.name);
daf50ae7
L
5148 }
5149
40fb9820 5150 if (i.tm.opcode_modifier.fwait)
29b0f896
AM
5151 if (!add_prefix (FWAIT_OPCODE))
5152 return;
252b5132 5153
d5de92cf 5154 /* Check if REP prefix is OK. */
742732c7 5155 if (i.rep_prefix && i.tm.opcode_modifier.prefixok != PrefixRep)
d5de92cf
L
5156 {
5157 as_bad (_("invalid instruction `%s' after `%s'"),
5158 i.tm.name, i.rep_prefix);
5159 return;
5160 }
5161
c1ba0266
L
5162 /* Check for lock without a lockable instruction. Destination operand
5163 must be memory unless it is xchg (0x86). */
c32fa91d 5164 if (i.prefix[LOCK_PREFIX]
742732c7 5165 && (i.tm.opcode_modifier.prefixok < PrefixLock
c1ba0266
L
5166 || i.mem_operands == 0
5167 || (i.tm.base_opcode != 0x86
8dc0818e 5168 && !(i.flags[i.operands - 1] & Operand_Mem))))
c32fa91d
L
5169 {
5170 as_bad (_("expecting lockable instruction after `lock'"));
5171 return;
5172 }
5173
e3669c7f
JB
5174 if (is_any_vex_encoding (&i.tm)
5175 || i.tm.operand_types[i.imm_operands].bitfield.class >= RegMMX
5176 || i.tm.operand_types[i.imm_operands + 1].bitfield.class >= RegMMX)
7a8655d2 5177 {
e3669c7f
JB
5178 /* Check for data size prefix on VEX/XOP/EVEX encoded and SIMD insns. */
5179 if (i.prefix[DATA_PREFIX])
5180 {
5181 as_bad (_("data size prefix invalid with `%s'"), i.tm.name);
5182 return;
5183 }
5184
5185 /* Don't allow e.g. KMOV in TLS code sequences. */
5186 for (j = i.imm_operands; j < i.operands; ++j)
5187 switch (i.reloc[j])
5188 {
5189 case BFD_RELOC_386_TLS_GOTIE:
5190 case BFD_RELOC_386_TLS_LE_32:
5191 case BFD_RELOC_X86_64_GOTTPOFF:
5192 case BFD_RELOC_X86_64_TLSLD:
5193 as_bad (_("TLS relocation cannot be used with `%s'"), i.tm.name);
5194 return;
5195 default:
5196 break;
5197 }
7a8655d2
JB
5198 }
5199
42164a71 5200 /* Check if HLE prefix is OK. */
165de32a 5201 if (i.hle_prefix && !check_hle ())
42164a71
L
5202 return;
5203
7e8b059b
L
5204 /* Check BND prefix. */
5205 if (i.bnd_prefix && !i.tm.opcode_modifier.bndprefixok)
5206 as_bad (_("expecting valid branch instruction after `bnd'"));
5207
04ef582a 5208 /* Check NOTRACK prefix. */
742732c7 5209 if (i.notrack_prefix && i.tm.opcode_modifier.prefixok != PrefixNoTrack)
9fef80d6 5210 as_bad (_("expecting indirect branch instruction after `notrack'"));
04ef582a 5211
327e8c42
JB
5212 if (i.tm.cpu_flags.bitfield.cpumpx)
5213 {
5214 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
5215 as_bad (_("32-bit address isn't allowed in 64-bit MPX instructions."));
5216 else if (flag_code != CODE_16BIT
5217 ? i.prefix[ADDR_PREFIX]
5218 : i.mem_operands && !i.prefix[ADDR_PREFIX])
5219 as_bad (_("16-bit address isn't allowed in MPX instructions"));
5220 }
7e8b059b
L
5221
5222 /* Insert BND prefix. */
76d3a78a
JB
5223 if (add_bnd_prefix && i.tm.opcode_modifier.bndprefixok)
5224 {
5225 if (!i.prefix[BND_PREFIX])
5226 add_prefix (BND_PREFIX_OPCODE);
5227 else if (i.prefix[BND_PREFIX] != BND_PREFIX_OPCODE)
5228 {
5229 as_warn (_("replacing `rep'/`repe' prefix by `bnd'"));
5230 i.prefix[BND_PREFIX] = BND_PREFIX_OPCODE;
5231 }
5232 }
7e8b059b 5233
29b0f896 5234 /* Check string instruction segment overrides. */
51c8edf6 5235 if (i.tm.opcode_modifier.isstring >= IS_STRING_ES_OP0)
29b0f896 5236 {
51c8edf6 5237 gas_assert (i.mem_operands);
29b0f896 5238 if (!check_string ())
5dd0794d 5239 return;
fc0763e6 5240 i.disp_operands = 0;
29b0f896 5241 }
5dd0794d 5242
9373f275
L
5243 /* The memory operand of (%dx) should be only used with input/output
5244 instructions (base opcodes: 0x6c, 0x6e, 0xec, 0xee). */
5245 if (i.input_output_operand
5246 && ((i.tm.base_opcode | 0x82) != 0xee
5247 || i.tm.opcode_modifier.opcodespace != SPACE_BASE))
5248 {
5249 as_bad (_("input/output port address isn't allowed with `%s'"),
5250 i.tm.name);
5251 return;
5252 }
5253
b6f8c7c4
L
5254 if (optimize && !i.no_optimize && i.tm.opcode_modifier.optimize)
5255 optimize_encoding ();
5256
c8480b58
L
5257 if (use_unaligned_vector_move)
5258 encode_with_unaligned_vector_move ();
5259
29b0f896
AM
5260 if (!process_suffix ())
5261 return;
e413e4e9 5262
ef07be45
CL
5263 /* Check if IP-relative addressing requirements can be satisfied. */
5264 if (i.tm.cpu_flags.bitfield.cpuprefetchi
5265 && !(i.base_reg && i.base_reg->reg_num == RegIP))
f2462532 5266 as_warn (_("'%s' only supports RIP-relative address"), i.tm.name);
ef07be45 5267
921eafea 5268 /* Update operand types and check extended states. */
bc0844ae 5269 for (j = 0; j < i.operands; j++)
921eafea
L
5270 {
5271 i.types[j] = operand_type_and (i.types[j], i.tm.operand_types[j]);
3d70986f 5272 switch (i.tm.operand_types[j].bitfield.class)
921eafea
L
5273 {
5274 default:
5275 break;
5276 case RegMMX:
5277 i.xstate |= xstate_mmx;
5278 break;
5279 case RegMask:
32930e4e 5280 i.xstate |= xstate_mask;
921eafea
L
5281 break;
5282 case RegSIMD:
3d70986f 5283 if (i.tm.operand_types[j].bitfield.tmmword)
921eafea 5284 i.xstate |= xstate_tmm;
3d70986f 5285 else if (i.tm.operand_types[j].bitfield.zmmword)
921eafea 5286 i.xstate |= xstate_zmm;
3d70986f 5287 else if (i.tm.operand_types[j].bitfield.ymmword)
921eafea 5288 i.xstate |= xstate_ymm;
3d70986f 5289 else if (i.tm.operand_types[j].bitfield.xmmword)
921eafea
L
5290 i.xstate |= xstate_xmm;
5291 break;
5292 }
5293 }
bc0844ae 5294
29b0f896
AM
5295 /* Make still unresolved immediate matches conform to size of immediate
5296 given in i.suffix. */
5297 if (!finalize_imm ())
5298 return;
252b5132 5299
40fb9820 5300 if (i.types[0].bitfield.imm1)
29b0f896 5301 i.imm_operands = 0; /* kludge for shift insns. */
252b5132 5302
9afe6eb8
L
5303 /* We only need to check those implicit registers for instructions
5304 with 3 operands or less. */
5305 if (i.operands <= 3)
5306 for (j = 0; j < i.operands; j++)
75e5731b
JB
5307 if (i.types[j].bitfield.instance != InstanceNone
5308 && !i.types[j].bitfield.xmmword)
9afe6eb8 5309 i.reg_operands--;
40fb9820 5310
29b0f896
AM
5311 /* For insns with operands there are more diddles to do to the opcode. */
5312 if (i.operands)
5313 {
5314 if (!process_operands ())
5315 return;
5316 }
255571cd 5317 else if (!quiet_warnings && i.tm.opcode_modifier.operandconstraint == UGH)
29b0f896
AM
5318 {
5319 /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc. */
5320 as_warn (_("translating to `%sp'"), i.tm.name);
5321 }
252b5132 5322
7a8655d2 5323 if (is_any_vex_encoding (&i.tm))
9e5e5283 5324 {
c1dc7af5 5325 if (!cpu_arch_flags.bitfield.cpui286)
9e5e5283 5326 {
c1dc7af5 5327 as_bad (_("instruction `%s' isn't supported outside of protected mode."),
9e5e5283
L
5328 i.tm.name);
5329 return;
5330 }
c0f3af97 5331
0b9404fd
JB
5332 /* Check for explicit REX prefix. */
5333 if (i.prefix[REX_PREFIX] || i.rex_encoding)
5334 {
5335 as_bad (_("REX prefix invalid with `%s'"), i.tm.name);
5336 return;
5337 }
5338
9e5e5283
L
5339 if (i.tm.opcode_modifier.vex)
5340 build_vex_prefix (t);
5341 else
5342 build_evex_prefix ();
0b9404fd
JB
5343
5344 /* The individual REX.RXBW bits got consumed. */
5345 i.rex &= REX_OPCODE;
9e5e5283 5346 }
43234a1e 5347
5dd85c99
SP
5348 /* Handle conversion of 'int $3' --> special int3 insn. XOP or FMA4
5349 instructions may define INT_OPCODE as well, so avoid this corner
5350 case for those instructions that use MODRM. */
389d00a5
JB
5351 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
5352 && i.tm.base_opcode == INT_OPCODE
a6461c02
SP
5353 && !i.tm.opcode_modifier.modrm
5354 && i.op[0].imms->X_add_number == 3)
29b0f896
AM
5355 {
5356 i.tm.base_opcode = INT3_OPCODE;
5357 i.imm_operands = 0;
5358 }
252b5132 5359
0cfa3eb3
JB
5360 if ((i.tm.opcode_modifier.jump == JUMP
5361 || i.tm.opcode_modifier.jump == JUMP_BYTE
5362 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896
AM
5363 && i.op[0].disps->X_op == O_constant)
5364 {
5365 /* Convert "jmp constant" (and "call constant") to a jump (call) to
5366 the absolute address given by the constant. Since ix86 jumps and
5367 calls are pc relative, we need to generate a reloc. */
5368 i.op[0].disps->X_add_symbol = &abs_symbol;
5369 i.op[0].disps->X_op = O_symbol;
5370 }
252b5132 5371
29b0f896
AM
5372 /* For 8 bit registers we need an empty rex prefix. Also if the
5373 instruction already has a prefix, we need to convert old
5374 registers to new ones. */
773f551c 5375
bab6aec1 5376 if ((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte
29b0f896 5377 && (i.op[0].regs->reg_flags & RegRex64) != 0)
bab6aec1 5378 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte
29b0f896 5379 && (i.op[1].regs->reg_flags & RegRex64) != 0)
bab6aec1
JB
5380 || (((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte)
5381 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte))
29b0f896
AM
5382 && i.rex != 0))
5383 {
5384 int x;
726c5dcd 5385
29b0f896
AM
5386 i.rex |= REX_OPCODE;
5387 for (x = 0; x < 2; x++)
5388 {
5389 /* Look for 8 bit operand that uses old registers. */
bab6aec1 5390 if (i.types[x].bitfield.class == Reg && i.types[x].bitfield.byte
29b0f896 5391 && (i.op[x].regs->reg_flags & RegRex64) == 0)
773f551c 5392 {
3f93af61 5393 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
29b0f896
AM
5394 /* In case it is "hi" register, give up. */
5395 if (i.op[x].regs->reg_num > 3)
a540244d 5396 as_bad (_("can't encode register '%s%s' in an "
4eed87de 5397 "instruction requiring REX prefix."),
a540244d 5398 register_prefix, i.op[x].regs->reg_name);
773f551c 5399
29b0f896
AM
5400 /* Otherwise it is equivalent to the extended register.
5401 Since the encoding doesn't change this is merely
5402 cosmetic cleanup for debug output. */
5403
5404 i.op[x].regs = i.op[x].regs + 8;
773f551c 5405 }
29b0f896
AM
5406 }
5407 }
773f551c 5408
6b6b6807
L
5409 if (i.rex == 0 && i.rex_encoding)
5410 {
5411 /* Check if we can add a REX_OPCODE byte. Look for 8 bit operand
3f93af61 5412 that uses legacy register. If it is "hi" register, don't add
6b6b6807
L
5413 the REX_OPCODE byte. */
5414 int x;
5415 for (x = 0; x < 2; x++)
bab6aec1 5416 if (i.types[x].bitfield.class == Reg
6b6b6807
L
5417 && i.types[x].bitfield.byte
5418 && (i.op[x].regs->reg_flags & RegRex64) == 0
5419 && i.op[x].regs->reg_num > 3)
5420 {
3f93af61 5421 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
5b7c81bd 5422 i.rex_encoding = false;
6b6b6807
L
5423 break;
5424 }
5425
5426 if (i.rex_encoding)
5427 i.rex = REX_OPCODE;
5428 }
5429
7ab9ffdd 5430 if (i.rex != 0)
29b0f896
AM
5431 add_prefix (REX_OPCODE | i.rex);
5432
ae531041
L
5433 insert_lfence_before ();
5434
29b0f896
AM
5435 /* We are ready to output the insn. */
5436 output_insn ();
e379e5f3 5437
ae531041
L
5438 insert_lfence_after ();
5439
e379e5f3
L
5440 last_insn.seg = now_seg;
5441
5442 if (i.tm.opcode_modifier.isprefix)
5443 {
5444 last_insn.kind = last_insn_prefix;
5445 last_insn.name = i.tm.name;
5446 last_insn.file = as_where (&last_insn.line);
5447 }
5448 else
5449 last_insn.kind = last_insn_other;
29b0f896
AM
5450}
5451
9db83a32
JB
5452/* The Q suffix is generally valid only in 64-bit mode, with very few
5453 exceptions: fild, fistp, fisttp, and cmpxchg8b. Note that for fild
5454 and fisttp only one of their two templates is matched below: That's
5455 sufficient since other relevant attributes are the same between both
5456 respective templates. */
5457static INLINE bool q_suffix_allowed(const insn_template *t)
5458{
5459 return flag_code == CODE_64BIT
5460 || (t->opcode_modifier.opcodespace == SPACE_BASE
5461 && t->base_opcode == 0xdf
5462 && (t->extension_opcode & 1)) /* fild / fistp / fisttp */
5463 || (t->opcode_modifier.opcodespace == SPACE_0F
5464 && t->base_opcode == 0xc7
5465 && t->opcode_modifier.opcodeprefix == PREFIX_NONE
5466 && t->extension_opcode == 1) /* cmpxchg8b */;
5467}
5468
5317ad2c
JB
5469static const char *
5470parse_insn (const char *line, char *mnemonic)
29b0f896 5471{
5317ad2c 5472 const char *l = line, *token_start = l;
29b0f896 5473 char *mnem_p;
04784e33 5474 bool pass1 = !current_templates;
5c6af06e 5475 int supported;
d3ce72d0 5476 const insn_template *t;
b6169b20 5477 char *dot_p = NULL;
29b0f896 5478
29b0f896
AM
5479 while (1)
5480 {
5481 mnem_p = mnemonic;
5482 while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
5483 {
b6169b20
L
5484 if (*mnem_p == '.')
5485 dot_p = mnem_p;
29b0f896
AM
5486 mnem_p++;
5487 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
45288df1 5488 {
29b0f896
AM
5489 as_bad (_("no such instruction: `%s'"), token_start);
5490 return NULL;
5491 }
5492 l++;
5493 }
5494 if (!is_space_char (*l)
5495 && *l != END_OF_INSN
e44823cf
JB
5496 && (intel_syntax
5497 || (*l != PREFIX_SEPARATOR
5498 && *l != ',')))
29b0f896
AM
5499 {
5500 as_bad (_("invalid character %s in mnemonic"),
5501 output_invalid (*l));
5502 return NULL;
5503 }
5504 if (token_start == l)
5505 {
e44823cf 5506 if (!intel_syntax && *l == PREFIX_SEPARATOR)
29b0f896
AM
5507 as_bad (_("expecting prefix; got nothing"));
5508 else
5509 as_bad (_("expecting mnemonic; got nothing"));
5510 return NULL;
5511 }
45288df1 5512
29b0f896 5513 /* Look up instruction (or prefix) via hash table. */
629310ab 5514 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
47926f60 5515
29b0f896
AM
5516 if (*l != END_OF_INSN
5517 && (!is_space_char (*l) || l[1] != END_OF_INSN)
5518 && current_templates
40fb9820 5519 && current_templates->start->opcode_modifier.isprefix)
29b0f896 5520 {
c6fb90c8 5521 if (!cpu_flags_check_cpu64 (current_templates->start->cpu_flags))
2dd88dca
JB
5522 {
5523 as_bad ((flag_code != CODE_64BIT
5524 ? _("`%s' is only supported in 64-bit mode")
5525 : _("`%s' is not supported in 64-bit mode")),
5526 current_templates->start->name);
5527 return NULL;
5528 }
29b0f896
AM
5529 /* If we are in 16-bit mode, do not allow addr16 or data16.
5530 Similarly, in 32-bit mode, do not allow addr32 or data32. */
673fe0f0
JB
5531 if ((current_templates->start->opcode_modifier.size == SIZE16
5532 || current_templates->start->opcode_modifier.size == SIZE32)
29b0f896 5533 && flag_code != CODE_64BIT
673fe0f0 5534 && ((current_templates->start->opcode_modifier.size == SIZE32)
29b0f896
AM
5535 ^ (flag_code == CODE_16BIT)))
5536 {
5537 as_bad (_("redundant %s prefix"),
5538 current_templates->start->name);
5539 return NULL;
45288df1 5540 }
31184569
JB
5541
5542 if (current_templates->start->base_opcode == PSEUDO_PREFIX)
29b0f896 5543 {
86fa6981 5544 /* Handle pseudo prefixes. */
31184569 5545 switch (current_templates->start->extension_opcode)
86fa6981 5546 {
41eb8e88 5547 case Prefix_Disp8:
86fa6981
L
5548 /* {disp8} */
5549 i.disp_encoding = disp_encoding_8bit;
5550 break;
41eb8e88
L
5551 case Prefix_Disp16:
5552 /* {disp16} */
5553 i.disp_encoding = disp_encoding_16bit;
5554 break;
5555 case Prefix_Disp32:
86fa6981
L
5556 /* {disp32} */
5557 i.disp_encoding = disp_encoding_32bit;
5558 break;
41eb8e88 5559 case Prefix_Load:
86fa6981
L
5560 /* {load} */
5561 i.dir_encoding = dir_encoding_load;
5562 break;
41eb8e88 5563 case Prefix_Store:
86fa6981
L
5564 /* {store} */
5565 i.dir_encoding = dir_encoding_store;
5566 break;
41eb8e88 5567 case Prefix_VEX:
42e04b36
L
5568 /* {vex} */
5569 i.vec_encoding = vex_encoding_vex;
86fa6981 5570 break;
41eb8e88 5571 case Prefix_VEX3:
86fa6981
L
5572 /* {vex3} */
5573 i.vec_encoding = vex_encoding_vex3;
5574 break;
41eb8e88 5575 case Prefix_EVEX:
86fa6981
L
5576 /* {evex} */
5577 i.vec_encoding = vex_encoding_evex;
5578 break;
41eb8e88 5579 case Prefix_REX:
6b6b6807 5580 /* {rex} */
5b7c81bd 5581 i.rex_encoding = true;
6b6b6807 5582 break;
41eb8e88 5583 case Prefix_NoOptimize:
b6f8c7c4 5584 /* {nooptimize} */
5b7c81bd 5585 i.no_optimize = true;
b6f8c7c4 5586 break;
86fa6981
L
5587 default:
5588 abort ();
5589 }
5590 }
5591 else
5592 {
5593 /* Add prefix, checking for repeated prefixes. */
4e9ac44a 5594 switch (add_prefix (current_templates->start->base_opcode))
86fa6981 5595 {
4e9ac44a
L
5596 case PREFIX_EXIST:
5597 return NULL;
5598 case PREFIX_DS:
d777820b 5599 if (current_templates->start->cpu_flags.bitfield.cpuibt)
4e9ac44a
L
5600 i.notrack_prefix = current_templates->start->name;
5601 break;
5602 case PREFIX_REP:
5603 if (current_templates->start->cpu_flags.bitfield.cpuhle)
5604 i.hle_prefix = current_templates->start->name;
5605 else if (current_templates->start->cpu_flags.bitfield.cpumpx)
5606 i.bnd_prefix = current_templates->start->name;
5607 else
5608 i.rep_prefix = current_templates->start->name;
5609 break;
5610 default:
5611 break;
86fa6981 5612 }
29b0f896
AM
5613 }
5614 /* Skip past PREFIX_SEPARATOR and reset token_start. */
5615 token_start = ++l;
5616 }
5617 else
5618 break;
5619 }
45288df1 5620
30a55f88 5621 if (!current_templates)
b6169b20 5622 {
07d5e953
JB
5623 /* Deprecated functionality (new code should use pseudo-prefixes instead):
5624 Check if we should swap operand or force 32bit displacement in
f8a5c266 5625 encoding. */
30a55f88 5626 if (mnem_p - 2 == dot_p && dot_p[1] == 's')
64c49ab3 5627 i.dir_encoding = dir_encoding_swap;
8d63c93e 5628 else if (mnem_p - 3 == dot_p
a501d77e
L
5629 && dot_p[1] == 'd'
5630 && dot_p[2] == '8')
5631 i.disp_encoding = disp_encoding_8bit;
8d63c93e 5632 else if (mnem_p - 4 == dot_p
f8a5c266
L
5633 && dot_p[1] == 'd'
5634 && dot_p[2] == '3'
5635 && dot_p[3] == '2')
a501d77e 5636 i.disp_encoding = disp_encoding_32bit;
30a55f88
L
5637 else
5638 goto check_suffix;
5639 mnem_p = dot_p;
5640 *dot_p = '\0';
629310ab 5641 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
b6169b20
L
5642 }
5643
04784e33 5644 if (!current_templates || !pass1)
29b0f896 5645 {
04784e33
JB
5646 current_templates = NULL;
5647
dc1e8a47 5648 check_suffix:
1c529385 5649 if (mnem_p > mnemonic)
29b0f896 5650 {
1c529385
LH
5651 /* See if we can get a match by trimming off a suffix. */
5652 switch (mnem_p[-1])
29b0f896 5653 {
1c529385
LH
5654 case WORD_MNEM_SUFFIX:
5655 if (intel_syntax && (intel_float_operand (mnemonic) & 2))
29b0f896
AM
5656 i.suffix = SHORT_MNEM_SUFFIX;
5657 else
1c529385
LH
5658 /* Fall through. */
5659 case BYTE_MNEM_SUFFIX:
5660 case QWORD_MNEM_SUFFIX:
5661 i.suffix = mnem_p[-1];
29b0f896 5662 mnem_p[-1] = '\0';
fe0e921f
AM
5663 current_templates
5664 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5665 break;
5666 case SHORT_MNEM_SUFFIX:
5667 case LONG_MNEM_SUFFIX:
5668 if (!intel_syntax)
5669 {
5670 i.suffix = mnem_p[-1];
5671 mnem_p[-1] = '\0';
fe0e921f
AM
5672 current_templates
5673 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5674 }
5675 break;
5676
5677 /* Intel Syntax. */
5678 case 'd':
5679 if (intel_syntax)
5680 {
5681 if (intel_float_operand (mnemonic) == 1)
5682 i.suffix = SHORT_MNEM_SUFFIX;
5683 else
5684 i.suffix = LONG_MNEM_SUFFIX;
5685 mnem_p[-1] = '\0';
fe0e921f
AM
5686 current_templates
5687 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385 5688 }
04784e33
JB
5689 /* For compatibility reasons accept MOVSD and CMPSD without
5690 operands even in AT&T mode. */
5691 else if (*l == END_OF_INSN
5692 || (is_space_char (*l) && l[1] == END_OF_INSN))
5693 {
5694 mnem_p[-1] = '\0';
5695 current_templates
5696 = (const templates *) str_hash_find (op_hash, mnemonic);
5697 if (current_templates != NULL
5698 /* MOVS or CMPS */
5699 && (current_templates->start->base_opcode | 2) == 0xa6
5700 && current_templates->start->opcode_modifier.opcodespace
5701 == SPACE_BASE
5702 && mnem_p[-2] == 's')
5703 {
5704 as_warn (_("found `%sd'; assuming `%sl' was meant"),
5705 mnemonic, mnemonic);
5706 i.suffix = LONG_MNEM_SUFFIX;
5707 }
5708 else
5709 {
5710 current_templates = NULL;
5711 mnem_p[-1] = 'd';
5712 }
5713 }
1c529385 5714 break;
29b0f896 5715 }
29b0f896 5716 }
1c529385 5717
29b0f896
AM
5718 if (!current_templates)
5719 {
04784e33
JB
5720 if (pass1)
5721 as_bad (_("no such instruction: `%s'"), token_start);
29b0f896
AM
5722 return NULL;
5723 }
5724 }
252b5132 5725
0cfa3eb3
JB
5726 if (current_templates->start->opcode_modifier.jump == JUMP
5727 || current_templates->start->opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
5728 {
5729 /* Check for a branch hint. We allow ",pt" and ",pn" for
5730 predict taken and predict not taken respectively.
5731 I'm not sure that branch hints actually do anything on loop
5732 and jcxz insns (JumpByte) for current Pentium4 chips. They
5733 may work in the future and it doesn't hurt to accept them
5734 now. */
5735 if (l[0] == ',' && l[1] == 'p')
5736 {
5737 if (l[2] == 't')
5738 {
5739 if (!add_prefix (DS_PREFIX_OPCODE))
5740 return NULL;
5741 l += 3;
5742 }
5743 else if (l[2] == 'n')
5744 {
5745 if (!add_prefix (CS_PREFIX_OPCODE))
5746 return NULL;
5747 l += 3;
5748 }
5749 }
5750 }
5751 /* Any other comma loses. */
5752 if (*l == ',')
5753 {
5754 as_bad (_("invalid character %s in mnemonic"),
5755 output_invalid (*l));
5756 return NULL;
5757 }
252b5132 5758
29b0f896 5759 /* Check if instruction is supported on specified architecture. */
5c6af06e
JB
5760 supported = 0;
5761 for (t = current_templates->start; t < current_templates->end; ++t)
5762 {
c0f3af97 5763 supported |= cpu_flags_match (t);
9db83a32
JB
5764
5765 if (i.suffix == QWORD_MNEM_SUFFIX && !q_suffix_allowed (t))
5766 supported &= ~CPU_FLAGS_64BIT_MATCH;
5767
c0f3af97 5768 if (supported == CPU_FLAGS_PERFECT_MATCH)
d59a54c2 5769 return l;
29b0f896 5770 }
3629bb00 5771
9db83a32
JB
5772 if (pass1)
5773 {
5774 if (supported & CPU_FLAGS_64BIT_MATCH)
5775 i.error = unsupported_on_arch;
5776 else
5777 i.error = unsupported_64bit;
5778 }
252b5132 5779
548d0ee6 5780 return NULL;
29b0f896 5781}
252b5132 5782
29b0f896 5783static char *
e3bb37b5 5784parse_operands (char *l, const char *mnemonic)
29b0f896
AM
5785{
5786 char *token_start;
3138f287 5787
29b0f896
AM
5788 /* 1 if operand is pending after ','. */
5789 unsigned int expecting_operand = 0;
252b5132 5790
29b0f896
AM
5791 while (*l != END_OF_INSN)
5792 {
e68c3d59
JB
5793 /* Non-zero if operand parens not balanced. */
5794 unsigned int paren_not_balanced = 0;
5795 /* True if inside double quotes. */
5796 bool in_quotes = false;
5797
29b0f896
AM
5798 /* Skip optional white space before operand. */
5799 if (is_space_char (*l))
5800 ++l;
d02603dc 5801 if (!is_operand_char (*l) && *l != END_OF_INSN && *l != '"')
29b0f896
AM
5802 {
5803 as_bad (_("invalid character %s before operand %d"),
5804 output_invalid (*l),
5805 i.operands + 1);
5806 return NULL;
5807 }
d02603dc 5808 token_start = l; /* After white space. */
e68c3d59 5809 while (in_quotes || paren_not_balanced || *l != ',')
29b0f896
AM
5810 {
5811 if (*l == END_OF_INSN)
5812 {
e68c3d59
JB
5813 if (in_quotes)
5814 {
5815 as_bad (_("unbalanced double quotes in operand %d."),
5816 i.operands + 1);
5817 return NULL;
5818 }
29b0f896
AM
5819 if (paren_not_balanced)
5820 {
98ff9f1c
JB
5821 know (!intel_syntax);
5822 as_bad (_("unbalanced parenthesis in operand %d."),
5823 i.operands + 1);
29b0f896
AM
5824 return NULL;
5825 }
5826 else
5827 break; /* we are done */
5828 }
e68c3d59
JB
5829 else if (*l == '\\' && l[1] == '"')
5830 ++l;
5831 else if (*l == '"')
5832 in_quotes = !in_quotes;
5833 else if (!in_quotes && !is_operand_char (*l) && !is_space_char (*l))
29b0f896
AM
5834 {
5835 as_bad (_("invalid character %s in operand %d"),
5836 output_invalid (*l),
5837 i.operands + 1);
5838 return NULL;
5839 }
e68c3d59 5840 if (!intel_syntax && !in_quotes)
29b0f896
AM
5841 {
5842 if (*l == '(')
5843 ++paren_not_balanced;
5844 if (*l == ')')
5845 --paren_not_balanced;
5846 }
29b0f896
AM
5847 l++;
5848 }
5849 if (l != token_start)
5850 { /* Yes, we've read in another operand. */
5851 unsigned int operand_ok;
5852 this_operand = i.operands++;
5853 if (i.operands > MAX_OPERANDS)
5854 {
5855 as_bad (_("spurious operands; (%d operands/instruction max)"),
5856 MAX_OPERANDS);
5857 return NULL;
5858 }
9d46ce34 5859 i.types[this_operand].bitfield.unspecified = 1;
29b0f896
AM
5860 /* Now parse operand adding info to 'i' as we go along. */
5861 END_STRING_AND_SAVE (l);
5862
1286ab78
L
5863 if (i.mem_operands > 1)
5864 {
5865 as_bad (_("too many memory references for `%s'"),
5866 mnemonic);
5867 return 0;
5868 }
5869
29b0f896
AM
5870 if (intel_syntax)
5871 operand_ok =
5872 i386_intel_operand (token_start,
5873 intel_float_operand (mnemonic));
5874 else
a7619375 5875 operand_ok = i386_att_operand (token_start);
29b0f896
AM
5876
5877 RESTORE_END_STRING (l);
5878 if (!operand_ok)
5879 return NULL;
5880 }
5881 else
5882 {
5883 if (expecting_operand)
5884 {
5885 expecting_operand_after_comma:
5886 as_bad (_("expecting operand after ','; got nothing"));
5887 return NULL;
5888 }
5889 if (*l == ',')
5890 {
5891 as_bad (_("expecting operand before ','; got nothing"));
5892 return NULL;
5893 }
5894 }
7f3f1ea2 5895
29b0f896
AM
5896 /* Now *l must be either ',' or END_OF_INSN. */
5897 if (*l == ',')
5898 {
5899 if (*++l == END_OF_INSN)
5900 {
5901 /* Just skip it, if it's \n complain. */
5902 goto expecting_operand_after_comma;
5903 }
5904 expecting_operand = 1;
5905 }
5906 }
5907 return l;
5908}
7f3f1ea2 5909
050dfa73 5910static void
783c187b 5911swap_2_operands (unsigned int xchg1, unsigned int xchg2)
050dfa73
MM
5912{
5913 union i386_op temp_op;
40fb9820 5914 i386_operand_type temp_type;
c48dadc9 5915 unsigned int temp_flags;
050dfa73 5916 enum bfd_reloc_code_real temp_reloc;
4eed87de 5917
050dfa73
MM
5918 temp_type = i.types[xchg2];
5919 i.types[xchg2] = i.types[xchg1];
5920 i.types[xchg1] = temp_type;
c48dadc9
JB
5921
5922 temp_flags = i.flags[xchg2];
5923 i.flags[xchg2] = i.flags[xchg1];
5924 i.flags[xchg1] = temp_flags;
5925
050dfa73
MM
5926 temp_op = i.op[xchg2];
5927 i.op[xchg2] = i.op[xchg1];
5928 i.op[xchg1] = temp_op;
c48dadc9 5929
050dfa73
MM
5930 temp_reloc = i.reloc[xchg2];
5931 i.reloc[xchg2] = i.reloc[xchg1];
5932 i.reloc[xchg1] = temp_reloc;
43234a1e 5933
6225c532 5934 if (i.mask.reg)
43234a1e 5935 {
6225c532
JB
5936 if (i.mask.operand == xchg1)
5937 i.mask.operand = xchg2;
5938 else if (i.mask.operand == xchg2)
5939 i.mask.operand = xchg1;
43234a1e 5940 }
a5748e0d 5941 if (i.broadcast.type || i.broadcast.bytes)
43234a1e 5942 {
5273a3cd
JB
5943 if (i.broadcast.operand == xchg1)
5944 i.broadcast.operand = xchg2;
5945 else if (i.broadcast.operand == xchg2)
5946 i.broadcast.operand = xchg1;
43234a1e 5947 }
050dfa73
MM
5948}
5949
29b0f896 5950static void
e3bb37b5 5951swap_operands (void)
29b0f896 5952{
b7c61d9a 5953 switch (i.operands)
050dfa73 5954 {
c0f3af97 5955 case 5:
b7c61d9a 5956 case 4:
4d456e3d 5957 swap_2_operands (1, i.operands - 2);
1a0670f3 5958 /* Fall through. */
b7c61d9a
L
5959 case 3:
5960 case 2:
4d456e3d 5961 swap_2_operands (0, i.operands - 1);
b7c61d9a
L
5962 break;
5963 default:
5964 abort ();
29b0f896 5965 }
29b0f896
AM
5966
5967 if (i.mem_operands == 2)
5968 {
5e042380 5969 const reg_entry *temp_seg;
29b0f896
AM
5970 temp_seg = i.seg[0];
5971 i.seg[0] = i.seg[1];
5972 i.seg[1] = temp_seg;
5973 }
5974}
252b5132 5975
29b0f896
AM
5976/* Try to ensure constant immediates are represented in the smallest
5977 opcode possible. */
5978static void
e3bb37b5 5979optimize_imm (void)
29b0f896
AM
5980{
5981 char guess_suffix = 0;
5982 int op;
252b5132 5983
29b0f896
AM
5984 if (i.suffix)
5985 guess_suffix = i.suffix;
5986 else if (i.reg_operands)
5987 {
5988 /* Figure out a suffix from the last register operand specified.
75e5731b
JB
5989 We can't do this properly yet, i.e. excluding special register
5990 instances, but the following works for instructions with
5991 immediates. In any case, we can't set i.suffix yet. */
29b0f896 5992 for (op = i.operands; --op >= 0;)
bab6aec1
JB
5993 if (i.types[op].bitfield.class != Reg)
5994 continue;
5995 else if (i.types[op].bitfield.byte)
7ab9ffdd 5996 {
40fb9820
L
5997 guess_suffix = BYTE_MNEM_SUFFIX;
5998 break;
5999 }
bab6aec1 6000 else if (i.types[op].bitfield.word)
252b5132 6001 {
40fb9820
L
6002 guess_suffix = WORD_MNEM_SUFFIX;
6003 break;
6004 }
bab6aec1 6005 else if (i.types[op].bitfield.dword)
40fb9820
L
6006 {
6007 guess_suffix = LONG_MNEM_SUFFIX;
6008 break;
6009 }
bab6aec1 6010 else if (i.types[op].bitfield.qword)
40fb9820
L
6011 {
6012 guess_suffix = QWORD_MNEM_SUFFIX;
29b0f896 6013 break;
252b5132 6014 }
29b0f896
AM
6015 }
6016 else if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
6017 guess_suffix = WORD_MNEM_SUFFIX;
6018
6019 for (op = i.operands; --op >= 0;)
40fb9820 6020 if (operand_type_check (i.types[op], imm))
29b0f896
AM
6021 {
6022 switch (i.op[op].imms->X_op)
252b5132 6023 {
29b0f896
AM
6024 case O_constant:
6025 /* If a suffix is given, this operand may be shortened. */
6026 switch (guess_suffix)
252b5132 6027 {
29b0f896 6028 case LONG_MNEM_SUFFIX:
40fb9820
L
6029 i.types[op].bitfield.imm32 = 1;
6030 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
6031 break;
6032 case WORD_MNEM_SUFFIX:
40fb9820
L
6033 i.types[op].bitfield.imm16 = 1;
6034 i.types[op].bitfield.imm32 = 1;
6035 i.types[op].bitfield.imm32s = 1;
6036 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
6037 break;
6038 case BYTE_MNEM_SUFFIX:
40fb9820
L
6039 i.types[op].bitfield.imm8 = 1;
6040 i.types[op].bitfield.imm8s = 1;
6041 i.types[op].bitfield.imm16 = 1;
6042 i.types[op].bitfield.imm32 = 1;
6043 i.types[op].bitfield.imm32s = 1;
6044 i.types[op].bitfield.imm64 = 1;
29b0f896 6045 break;
252b5132 6046 }
252b5132 6047
29b0f896
AM
6048 /* If this operand is at most 16 bits, convert it
6049 to a signed 16 bit number before trying to see
6050 whether it will fit in an even smaller size.
6051 This allows a 16-bit operand such as $0xffe0 to
6052 be recognised as within Imm8S range. */
40fb9820 6053 if ((i.types[op].bitfield.imm16)
7e96fb68 6054 && fits_in_unsigned_word (i.op[op].imms->X_add_number))
252b5132 6055 {
87ed972d
JB
6056 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
6057 ^ 0x8000) - 0x8000);
29b0f896 6058 }
a28def75
L
6059#ifdef BFD64
6060 /* Store 32-bit immediate in 64-bit for 64-bit BFD. */
40fb9820 6061 if ((i.types[op].bitfield.imm32)
7e96fb68 6062 && fits_in_unsigned_long (i.op[op].imms->X_add_number))
29b0f896
AM
6063 {
6064 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
6065 ^ ((offsetT) 1 << 31))
6066 - ((offsetT) 1 << 31));
6067 }
a28def75 6068#endif
40fb9820 6069 i.types[op]
c6fb90c8
L
6070 = operand_type_or (i.types[op],
6071 smallest_imm_type (i.op[op].imms->X_add_number));
252b5132 6072
29b0f896
AM
6073 /* We must avoid matching of Imm32 templates when 64bit
6074 only immediate is available. */
6075 if (guess_suffix == QWORD_MNEM_SUFFIX)
40fb9820 6076 i.types[op].bitfield.imm32 = 0;
29b0f896 6077 break;
252b5132 6078
29b0f896
AM
6079 case O_absent:
6080 case O_register:
6081 abort ();
6082
6083 /* Symbols and expressions. */
6084 default:
9cd96992
JB
6085 /* Convert symbolic operand to proper sizes for matching, but don't
6086 prevent matching a set of insns that only supports sizes other
6087 than those matching the insn suffix. */
6088 {
40fb9820 6089 i386_operand_type mask, allowed;
87ed972d 6090 const insn_template *t = current_templates->start;
9cd96992 6091
0dfbf9d7 6092 operand_type_set (&mask, 0);
9cd96992
JB
6093 switch (guess_suffix)
6094 {
6095 case QWORD_MNEM_SUFFIX:
40fb9820
L
6096 mask.bitfield.imm64 = 1;
6097 mask.bitfield.imm32s = 1;
9cd96992
JB
6098 break;
6099 case LONG_MNEM_SUFFIX:
40fb9820 6100 mask.bitfield.imm32 = 1;
9cd96992
JB
6101 break;
6102 case WORD_MNEM_SUFFIX:
40fb9820 6103 mask.bitfield.imm16 = 1;
9cd96992
JB
6104 break;
6105 case BYTE_MNEM_SUFFIX:
40fb9820 6106 mask.bitfield.imm8 = 1;
9cd96992
JB
6107 break;
6108 default:
9cd96992
JB
6109 break;
6110 }
8f0212ac
JB
6111
6112 allowed = operand_type_and (t->operand_types[op], mask);
6113 while (++t < current_templates->end)
6114 {
6115 allowed = operand_type_or (allowed, t->operand_types[op]);
6116 allowed = operand_type_and (allowed, mask);
6117 }
6118
0dfbf9d7 6119 if (!operand_type_all_zero (&allowed))
c6fb90c8 6120 i.types[op] = operand_type_and (i.types[op], mask);
9cd96992 6121 }
29b0f896 6122 break;
252b5132 6123 }
29b0f896
AM
6124 }
6125}
47926f60 6126
29b0f896
AM
6127/* Try to use the smallest displacement type too. */
6128static void
e3bb37b5 6129optimize_disp (void)
29b0f896
AM
6130{
6131 int op;
3e73aa7c 6132
29b0f896 6133 for (op = i.operands; --op >= 0;)
40fb9820 6134 if (operand_type_check (i.types[op], disp))
252b5132 6135 {
b300c311 6136 if (i.op[op].disps->X_op == O_constant)
252b5132 6137 {
91d6fa6a 6138 offsetT op_disp = i.op[op].disps->X_add_number;
29b0f896 6139
91d6fa6a 6140 if (!op_disp && i.types[op].bitfield.baseindex)
b300c311 6141 {
2f2be86b
JB
6142 i.types[op] = operand_type_and_not (i.types[op], anydisp);
6143 i.op[op].disps = NULL;
b300c311 6144 i.disp_operands--;
f185acdd
JB
6145 continue;
6146 }
6147
6148 if (i.types[op].bitfield.disp16
cd613c1f 6149 && fits_in_unsigned_word (op_disp))
f185acdd
JB
6150 {
6151 /* If this operand is at most 16 bits, convert
6152 to a signed 16 bit number and don't use 64bit
6153 displacement. */
6154 op_disp = ((op_disp ^ 0x8000) - 0x8000);
6155 i.types[op].bitfield.disp64 = 0;
b300c311 6156 }
f185acdd 6157
28a167a4 6158#ifdef BFD64
a50187b2 6159 /* Optimize 64-bit displacement to 32-bit for 64-bit BFD. */
a775efc8
JB
6160 if ((flag_code != CODE_64BIT
6161 ? i.types[op].bitfield.disp32
6162 : want_disp32 (current_templates->start)
6163 && (!current_templates->start->opcode_modifier.jump
6164 || i.jumpabsolute || i.types[op].bitfield.baseindex))
a50187b2 6165 && fits_in_unsigned_long (op_disp))
b300c311 6166 {
a50187b2
JB
6167 /* If this operand is at most 32 bits, convert
6168 to a signed 32 bit number and don't use 64bit
6169 displacement. */
6170 op_disp = (op_disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
6171 i.types[op].bitfield.disp64 = 0;
6172 i.types[op].bitfield.disp32 = 1;
6173 }
28a167a4 6174
a50187b2
JB
6175 if (flag_code == CODE_64BIT && fits_in_signed_long (op_disp))
6176 {
6177 i.types[op].bitfield.disp64 = 0;
a775efc8 6178 i.types[op].bitfield.disp32 = 1;
b300c311 6179 }
28a167a4 6180#endif
40fb9820 6181 if ((i.types[op].bitfield.disp32
40fb9820 6182 || i.types[op].bitfield.disp16)
b5014f7a 6183 && fits_in_disp8 (op_disp))
40fb9820 6184 i.types[op].bitfield.disp8 = 1;
77c59789
JB
6185
6186 i.op[op].disps->X_add_number = op_disp;
252b5132 6187 }
67a4f2b7
AO
6188 else if (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
6189 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL)
6190 {
6191 fix_new_exp (frag_now, frag_more (0) - frag_now->fr_literal, 0,
6192 i.op[op].disps, 0, i.reloc[op]);
2f2be86b 6193 i.types[op] = operand_type_and_not (i.types[op], anydisp);
67a4f2b7
AO
6194 }
6195 else
b300c311 6196 /* We only support 64bit displacement on constants. */
40fb9820 6197 i.types[op].bitfield.disp64 = 0;
252b5132 6198 }
29b0f896
AM
6199}
6200
4a1b91ea
L
6201/* Return 1 if there is a match in broadcast bytes between operand
6202 GIVEN and instruction template T. */
6203
6204static INLINE int
6205match_broadcast_size (const insn_template *t, unsigned int given)
6206{
6207 return ((t->opcode_modifier.broadcast == BYTE_BROADCAST
6208 && i.types[given].bitfield.byte)
6209 || (t->opcode_modifier.broadcast == WORD_BROADCAST
6210 && i.types[given].bitfield.word)
6211 || (t->opcode_modifier.broadcast == DWORD_BROADCAST
6212 && i.types[given].bitfield.dword)
6213 || (t->opcode_modifier.broadcast == QWORD_BROADCAST
6214 && i.types[given].bitfield.qword));
6215}
6216
6c30d220
L
6217/* Check if operands are valid for the instruction. */
6218
6219static int
6220check_VecOperands (const insn_template *t)
6221{
43234a1e 6222 unsigned int op;
e2195274 6223 i386_cpu_flags cpu;
e2195274
JB
6224
6225 /* Templates allowing for ZMMword as well as YMMword and/or XMMword for
6226 any one operand are implicity requiring AVX512VL support if the actual
6227 operand size is YMMword or XMMword. Since this function runs after
6228 template matching, there's no need to check for YMMword/XMMword in
6229 the template. */
6230 cpu = cpu_flags_and (t->cpu_flags, avx512);
6231 if (!cpu_flags_all_zero (&cpu)
6232 && !t->cpu_flags.bitfield.cpuavx512vl
6233 && !cpu_arch_flags.bitfield.cpuavx512vl)
6234 {
6235 for (op = 0; op < t->operands; ++op)
6236 {
6237 if (t->operand_types[op].bitfield.zmmword
6238 && (i.types[op].bitfield.ymmword
6239 || i.types[op].bitfield.xmmword))
6240 {
6241 i.error = unsupported;
6242 return 1;
6243 }
6244 }
6245 }
43234a1e 6246
22c36940
JB
6247 /* Somewhat similarly, templates specifying both AVX and AVX2 are
6248 requiring AVX2 support if the actual operand size is YMMword. */
6249 if (t->cpu_flags.bitfield.cpuavx
6250 && t->cpu_flags.bitfield.cpuavx2
6251 && !cpu_arch_flags.bitfield.cpuavx2)
6252 {
6253 for (op = 0; op < t->operands; ++op)
6254 {
6255 if (t->operand_types[op].bitfield.xmmword
6256 && i.types[op].bitfield.ymmword)
6257 {
6258 i.error = unsupported;
6259 return 1;
6260 }
6261 }
6262 }
6263
6c30d220 6264 /* Without VSIB byte, we can't have a vector register for index. */
63112cd6 6265 if (!t->opcode_modifier.sib
6c30d220 6266 && i.index_reg
1b54b8d7
JB
6267 && (i.index_reg->reg_type.bitfield.xmmword
6268 || i.index_reg->reg_type.bitfield.ymmword
6269 || i.index_reg->reg_type.bitfield.zmmword))
6c30d220
L
6270 {
6271 i.error = unsupported_vector_index_register;
6272 return 1;
6273 }
6274
ad8ecc81 6275 /* Check if default mask is allowed. */
255571cd 6276 if (t->opcode_modifier.operandconstraint == NO_DEFAULT_MASK
6225c532 6277 && (!i.mask.reg || i.mask.reg->reg_num == 0))
ad8ecc81
MZ
6278 {
6279 i.error = no_default_mask;
6280 return 1;
6281 }
6282
7bab8ab5
JB
6283 /* For VSIB byte, we need a vector register for index, and all vector
6284 registers must be distinct. */
260cd341 6285 if (t->opcode_modifier.sib && t->opcode_modifier.sib != SIBMEM)
7bab8ab5
JB
6286 {
6287 if (!i.index_reg
63112cd6 6288 || !((t->opcode_modifier.sib == VECSIB128
1b54b8d7 6289 && i.index_reg->reg_type.bitfield.xmmword)
63112cd6 6290 || (t->opcode_modifier.sib == VECSIB256
1b54b8d7 6291 && i.index_reg->reg_type.bitfield.ymmword)
63112cd6 6292 || (t->opcode_modifier.sib == VECSIB512
1b54b8d7 6293 && i.index_reg->reg_type.bitfield.zmmword)))
7bab8ab5
JB
6294 {
6295 i.error = invalid_vsib_address;
6296 return 1;
6297 }
6298
6225c532
JB
6299 gas_assert (i.reg_operands == 2 || i.mask.reg);
6300 if (i.reg_operands == 2 && !i.mask.reg)
43234a1e 6301 {
3528c362 6302 gas_assert (i.types[0].bitfield.class == RegSIMD);
1b54b8d7
JB
6303 gas_assert (i.types[0].bitfield.xmmword
6304 || i.types[0].bitfield.ymmword);
3528c362 6305 gas_assert (i.types[2].bitfield.class == RegSIMD);
1b54b8d7
JB
6306 gas_assert (i.types[2].bitfield.xmmword
6307 || i.types[2].bitfield.ymmword);
43234a1e
L
6308 if (operand_check == check_none)
6309 return 0;
6310 if (register_number (i.op[0].regs)
6311 != register_number (i.index_reg)
6312 && register_number (i.op[2].regs)
6313 != register_number (i.index_reg)
6314 && register_number (i.op[0].regs)
6315 != register_number (i.op[2].regs))
6316 return 0;
6317 if (operand_check == check_error)
6318 {
6319 i.error = invalid_vector_register_set;
6320 return 1;
6321 }
6322 as_warn (_("mask, index, and destination registers should be distinct"));
6323 }
6225c532 6324 else if (i.reg_operands == 1 && i.mask.reg)
8444f82a 6325 {
3528c362 6326 if (i.types[1].bitfield.class == RegSIMD
1b54b8d7
JB
6327 && (i.types[1].bitfield.xmmword
6328 || i.types[1].bitfield.ymmword
6329 || i.types[1].bitfield.zmmword)
8444f82a
MZ
6330 && (register_number (i.op[1].regs)
6331 == register_number (i.index_reg)))
6332 {
6333 if (operand_check == check_error)
6334 {
6335 i.error = invalid_vector_register_set;
6336 return 1;
6337 }
6338 if (operand_check != check_none)
6339 as_warn (_("index and destination registers should be distinct"));
6340 }
6341 }
43234a1e 6342 }
7bab8ab5 6343
fc141319
L
6344 /* For AMX instructions with 3 TMM register operands, all operands
6345 must be distinct. */
6346 if (i.reg_operands == 3
6347 && t->operand_types[0].bitfield.tmmword
6348 && (i.op[0].regs == i.op[1].regs
6349 || i.op[0].regs == i.op[2].regs
6350 || i.op[1].regs == i.op[2].regs))
6351 {
6352 i.error = invalid_tmm_register_set;
6353 return 1;
260cd341
LC
6354 }
6355
0cc78721
CL
6356 /* For some special instructions require that destination must be distinct
6357 from source registers. */
255571cd 6358 if (t->opcode_modifier.operandconstraint == DISTINCT_DEST)
0cc78721
CL
6359 {
6360 unsigned int dest_reg = i.operands - 1;
6361
6362 know (i.operands >= 3);
6363
6364 /* #UD if dest_reg == src1_reg or dest_reg == src2_reg. */
6365 if (i.op[dest_reg - 1].regs == i.op[dest_reg].regs
6366 || (i.reg_operands > 2
6367 && i.op[dest_reg - 2].regs == i.op[dest_reg].regs))
6368 {
6369 i.error = invalid_dest_and_src_register_set;
6370 return 1;
6371 }
6372 }
6373
43234a1e
L
6374 /* Check if broadcast is supported by the instruction and is applied
6375 to the memory operand. */
a5748e0d 6376 if (i.broadcast.type || i.broadcast.bytes)
43234a1e 6377 {
8e6e0792 6378 i386_operand_type type, overlap;
43234a1e
L
6379
6380 /* Check if specified broadcast is supported in this instruction,
4a1b91ea 6381 and its broadcast bytes match the memory operand. */
5273a3cd 6382 op = i.broadcast.operand;
8e6e0792 6383 if (!t->opcode_modifier.broadcast
c48dadc9 6384 || !(i.flags[op] & Operand_Mem)
c39e5b26 6385 || (!i.types[op].bitfield.unspecified
4a1b91ea 6386 && !match_broadcast_size (t, op)))
43234a1e
L
6387 {
6388 bad_broadcast:
6389 i.error = unsupported_broadcast;
6390 return 1;
6391 }
8e6e0792 6392
a5748e0d
JB
6393 if (i.broadcast.type)
6394 i.broadcast.bytes = ((1 << (t->opcode_modifier.broadcast - 1))
6395 * i.broadcast.type);
8e6e0792 6396 operand_type_set (&type, 0);
a5748e0d 6397 switch (get_broadcast_bytes (t, false))
8e6e0792 6398 {
4a1b91ea
L
6399 case 2:
6400 type.bitfield.word = 1;
6401 break;
6402 case 4:
6403 type.bitfield.dword = 1;
6404 break;
8e6e0792
JB
6405 case 8:
6406 type.bitfield.qword = 1;
6407 break;
6408 case 16:
6409 type.bitfield.xmmword = 1;
6410 break;
6411 case 32:
6412 type.bitfield.ymmword = 1;
6413 break;
6414 case 64:
6415 type.bitfield.zmmword = 1;
6416 break;
6417 default:
6418 goto bad_broadcast;
6419 }
6420
6421 overlap = operand_type_and (type, t->operand_types[op]);
bc49bfd8
JB
6422 if (t->operand_types[op].bitfield.class == RegSIMD
6423 && t->operand_types[op].bitfield.byte
6424 + t->operand_types[op].bitfield.word
6425 + t->operand_types[op].bitfield.dword
6426 + t->operand_types[op].bitfield.qword > 1)
6427 {
6428 overlap.bitfield.xmmword = 0;
6429 overlap.bitfield.ymmword = 0;
6430 overlap.bitfield.zmmword = 0;
6431 }
8e6e0792
JB
6432 if (operand_type_all_zero (&overlap))
6433 goto bad_broadcast;
6434
9c19e9ec 6435 if (t->opcode_modifier.checkoperandsize)
8e6e0792
JB
6436 {
6437 unsigned int j;
6438
e2195274 6439 type.bitfield.baseindex = 1;
8e6e0792
JB
6440 for (j = 0; j < i.operands; ++j)
6441 {
6442 if (j != op
6443 && !operand_type_register_match(i.types[j],
6444 t->operand_types[j],
6445 type,
6446 t->operand_types[op]))
6447 goto bad_broadcast;
6448 }
6449 }
43234a1e
L
6450 }
6451 /* If broadcast is supported in this instruction, we need to check if
6452 operand of one-element size isn't specified without broadcast. */
6453 else if (t->opcode_modifier.broadcast && i.mem_operands)
6454 {
6455 /* Find memory operand. */
6456 for (op = 0; op < i.operands; op++)
8dc0818e 6457 if (i.flags[op] & Operand_Mem)
43234a1e
L
6458 break;
6459 gas_assert (op < i.operands);
6460 /* Check size of the memory operand. */
4a1b91ea 6461 if (match_broadcast_size (t, op))
43234a1e
L
6462 {
6463 i.error = broadcast_needed;
6464 return 1;
6465 }
6466 }
c39e5b26
JB
6467 else
6468 op = MAX_OPERANDS - 1; /* Avoid uninitialized variable warning. */
43234a1e
L
6469
6470 /* Check if requested masking is supported. */
6225c532 6471 if (i.mask.reg)
43234a1e 6472 {
ae2387fe
JB
6473 switch (t->opcode_modifier.masking)
6474 {
6475 case BOTH_MASKING:
6476 break;
6477 case MERGING_MASKING:
6225c532 6478 if (i.mask.zeroing)
ae2387fe
JB
6479 {
6480 case 0:
6481 i.error = unsupported_masking;
6482 return 1;
6483 }
6484 break;
6485 case DYNAMIC_MASKING:
6486 /* Memory destinations allow only merging masking. */
6225c532 6487 if (i.mask.zeroing && i.mem_operands)
ae2387fe
JB
6488 {
6489 /* Find memory operand. */
6490 for (op = 0; op < i.operands; op++)
c48dadc9 6491 if (i.flags[op] & Operand_Mem)
ae2387fe
JB
6492 break;
6493 gas_assert (op < i.operands);
6494 if (op == i.operands - 1)
6495 {
6496 i.error = unsupported_masking;
6497 return 1;
6498 }
6499 }
6500 break;
6501 default:
6502 abort ();
6503 }
43234a1e
L
6504 }
6505
6506 /* Check if masking is applied to dest operand. */
6225c532 6507 if (i.mask.reg && (i.mask.operand != i.operands - 1))
43234a1e
L
6508 {
6509 i.error = mask_not_on_destination;
6510 return 1;
6511 }
6512
43234a1e 6513 /* Check RC/SAE. */
ca5312a2 6514 if (i.rounding.type != rc_none)
43234a1e 6515 {
a80195f1 6516 if (!t->opcode_modifier.sae
cf665fee
JB
6517 || ((i.rounding.type != saeonly) != t->opcode_modifier.staticrounding)
6518 || i.mem_operands)
43234a1e
L
6519 {
6520 i.error = unsupported_rc_sae;
6521 return 1;
6522 }
cf665fee
JB
6523
6524 /* Non-EVEX.LIG forms need to have a ZMM register as at least one
6525 operand. */
6526 if (t->opcode_modifier.evex != EVEXLIG)
7bab8ab5 6527 {
cf665fee
JB
6528 for (op = 0; op < t->operands; ++op)
6529 if (i.types[op].bitfield.zmmword)
6530 break;
6531 if (op >= t->operands)
6532 {
6533 i.error = operand_size_mismatch;
6534 return 1;
6535 }
7bab8ab5 6536 }
6c30d220
L
6537 }
6538
da4977e0
JB
6539 /* Check the special Imm4 cases; must be the first operand. */
6540 if (t->cpu_flags.bitfield.cpuxop && t->operands == 5)
6541 {
6542 if (i.op[0].imms->X_op != O_constant
6543 || !fits_in_imm4 (i.op[0].imms->X_add_number))
6544 {
6545 i.error = bad_imm4;
6546 return 1;
6547 }
6548
6549 /* Turn off Imm<N> so that update_imm won't complain. */
6550 operand_type_set (&i.types[0], 0);
6551 }
6552
43234a1e 6553 /* Check vector Disp8 operand. */
b5014f7a 6554 if (t->opcode_modifier.disp8memshift
1a42a9fe 6555 && i.disp_encoding <= disp_encoding_8bit)
43234a1e 6556 {
a5748e0d 6557 if (i.broadcast.bytes)
4a1b91ea 6558 i.memshift = t->opcode_modifier.broadcast - 1;
7091c612 6559 else if (t->opcode_modifier.disp8memshift != DISP8_SHIFT_VL)
43234a1e 6560 i.memshift = t->opcode_modifier.disp8memshift;
7091c612
JB
6561 else
6562 {
125ff819 6563 const i386_operand_type *type = NULL, *fallback = NULL;
7091c612
JB
6564
6565 i.memshift = 0;
6566 for (op = 0; op < i.operands; op++)
8dc0818e 6567 if (i.flags[op] & Operand_Mem)
7091c612 6568 {
4174bfff
JB
6569 if (t->opcode_modifier.evex == EVEXLIG)
6570 i.memshift = 2 + (i.suffix == QWORD_MNEM_SUFFIX);
6571 else if (t->operand_types[op].bitfield.xmmword
6572 + t->operand_types[op].bitfield.ymmword
6573 + t->operand_types[op].bitfield.zmmword <= 1)
7091c612
JB
6574 type = &t->operand_types[op];
6575 else if (!i.types[op].bitfield.unspecified)
6576 type = &i.types[op];
125ff819
JB
6577 else /* Ambiguities get resolved elsewhere. */
6578 fallback = &t->operand_types[op];
7091c612 6579 }
3528c362 6580 else if (i.types[op].bitfield.class == RegSIMD
4174bfff 6581 && t->opcode_modifier.evex != EVEXLIG)
7091c612
JB
6582 {
6583 if (i.types[op].bitfield.zmmword)
6584 i.memshift = 6;
6585 else if (i.types[op].bitfield.ymmword && i.memshift < 5)
6586 i.memshift = 5;
6587 else if (i.types[op].bitfield.xmmword && i.memshift < 4)
6588 i.memshift = 4;
6589 }
6590
125ff819
JB
6591 if (!type && !i.memshift)
6592 type = fallback;
7091c612
JB
6593 if (type)
6594 {
6595 if (type->bitfield.zmmword)
6596 i.memshift = 6;
6597 else if (type->bitfield.ymmword)
6598 i.memshift = 5;
6599 else if (type->bitfield.xmmword)
6600 i.memshift = 4;
6601 }
6602
6603 /* For the check in fits_in_disp8(). */
6604 if (i.memshift == 0)
6605 i.memshift = -1;
6606 }
43234a1e
L
6607
6608 for (op = 0; op < i.operands; op++)
6609 if (operand_type_check (i.types[op], disp)
6610 && i.op[op].disps->X_op == O_constant)
6611 {
b5014f7a 6612 if (fits_in_disp8 (i.op[op].disps->X_add_number))
43234a1e 6613 {
b5014f7a
JB
6614 i.types[op].bitfield.disp8 = 1;
6615 return 0;
43234a1e 6616 }
b5014f7a 6617 i.types[op].bitfield.disp8 = 0;
43234a1e
L
6618 }
6619 }
b5014f7a
JB
6620
6621 i.memshift = 0;
43234a1e 6622
6c30d220
L
6623 return 0;
6624}
6625
da4977e0 6626/* Check if encoding requirements are met by the instruction. */
a683cc34
SP
6627
6628static int
da4977e0 6629VEX_check_encoding (const insn_template *t)
a683cc34 6630{
da4977e0
JB
6631 if (i.vec_encoding == vex_encoding_error)
6632 {
6633 i.error = unsupported;
6634 return 1;
6635 }
6636
86fa6981 6637 if (i.vec_encoding == vex_encoding_evex)
43234a1e 6638 {
86fa6981 6639 /* This instruction must be encoded with EVEX prefix. */
e771e7c9 6640 if (!is_evex_encoding (t))
86fa6981
L
6641 {
6642 i.error = unsupported;
6643 return 1;
6644 }
6645 return 0;
43234a1e
L
6646 }
6647
a683cc34 6648 if (!t->opcode_modifier.vex)
86fa6981
L
6649 {
6650 /* This instruction template doesn't have VEX prefix. */
6651 if (i.vec_encoding != vex_encoding_default)
6652 {
6653 i.error = unsupported;
6654 return 1;
6655 }
6656 return 0;
6657 }
a683cc34 6658
a683cc34
SP
6659 return 0;
6660}
6661
7b94647a
JB
6662/* Helper function for the progress() macro in match_template(). */
6663static INLINE enum i386_error progress (enum i386_error new,
6664 enum i386_error last,
6665 unsigned int line, unsigned int *line_p)
6666{
6667 if (line <= *line_p)
6668 return last;
6669 *line_p = line;
6670 return new;
6671}
6672
d3ce72d0 6673static const insn_template *
83b16ac6 6674match_template (char mnem_suffix)
29b0f896
AM
6675{
6676 /* Points to template once we've found it. */
d3ce72d0 6677 const insn_template *t;
40fb9820 6678 i386_operand_type overlap0, overlap1, overlap2, overlap3;
c0f3af97 6679 i386_operand_type overlap4;
29b0f896 6680 unsigned int found_reverse_match;
40fb9820 6681 i386_operand_type operand_types [MAX_OPERANDS];
539e75ad 6682 int addr_prefix_disp;
7b94647a
JB
6683 unsigned int j, size_match, check_register, errline = __LINE__;
6684 enum i386_error specific_error = number_of_operands_mismatch;
6685#define progress(err) progress (err, specific_error, __LINE__, &errline)
29b0f896 6686
c0f3af97
L
6687#if MAX_OPERANDS != 5
6688# error "MAX_OPERANDS must be 5."
f48ff2ae
L
6689#endif
6690
29b0f896 6691 found_reverse_match = 0;
539e75ad 6692 addr_prefix_disp = -1;
40fb9820 6693
45aa61fe 6694 for (t = current_templates->start; t < current_templates->end; t++)
29b0f896 6695 {
539e75ad 6696 addr_prefix_disp = -1;
dbbc8b7e 6697 found_reverse_match = 0;
539e75ad 6698
7b94647a 6699 /* Must have right number of operands. */
29b0f896
AM
6700 if (i.operands != t->operands)
6701 continue;
6702
50aecf8c 6703 /* Check processor support. */
7b94647a 6704 specific_error = progress (unsupported);
45a4bb20 6705 if (cpu_flags_match (t) != CPU_FLAGS_PERFECT_MATCH)
50aecf8c
L
6706 continue;
6707
e1d4d893 6708 /* Check AT&T mnemonic. */
7b94647a 6709 specific_error = progress (unsupported_with_intel_mnemonic);
e1d4d893 6710 if (intel_mnemonic && t->opcode_modifier.attmnemonic)
1efbbeb4
L
6711 continue;
6712
4b5aaf5f 6713 /* Check AT&T/Intel syntax. */
7b94647a 6714 specific_error = progress (unsupported_syntax);
5c07affc 6715 if ((intel_syntax && t->opcode_modifier.attsyntax)
4b5aaf5f 6716 || (!intel_syntax && t->opcode_modifier.intelsyntax))
1efbbeb4
L
6717 continue;
6718
4b5aaf5f
L
6719 /* Check Intel64/AMD64 ISA. */
6720 switch (isa64)
6721 {
6722 default:
6723 /* Default: Don't accept Intel64. */
6724 if (t->opcode_modifier.isa64 == INTEL64)
6725 continue;
6726 break;
6727 case amd64:
6728 /* -mamd64: Don't accept Intel64 and Intel64 only. */
6729 if (t->opcode_modifier.isa64 >= INTEL64)
6730 continue;
6731 break;
6732 case intel64:
6733 /* -mintel64: Don't accept AMD64. */
5990e377 6734 if (t->opcode_modifier.isa64 == AMD64 && flag_code == CODE_64BIT)
4b5aaf5f
L
6735 continue;
6736 break;
6737 }
6738
dc2be329 6739 /* Check the suffix. */
7b94647a 6740 specific_error = progress (invalid_instruction_suffix);
7505bb03
JB
6741 if ((t->opcode_modifier.no_bsuf && mnem_suffix == BYTE_MNEM_SUFFIX)
6742 || (t->opcode_modifier.no_wsuf && mnem_suffix == WORD_MNEM_SUFFIX)
6743 || (t->opcode_modifier.no_lsuf && mnem_suffix == LONG_MNEM_SUFFIX)
6744 || (t->opcode_modifier.no_ssuf && mnem_suffix == SHORT_MNEM_SUFFIX)
6745 || (t->opcode_modifier.no_qsuf && mnem_suffix == QWORD_MNEM_SUFFIX))
83b16ac6 6746 continue;
29b0f896 6747
7b94647a 6748 specific_error = progress (operand_size_mismatch);
3ac21baa
JB
6749 size_match = operand_size_match (t);
6750 if (!size_match)
7d5e4556 6751 continue;
539e75ad 6752
6f2f06be
JB
6753 /* This is intentionally not
6754
0cfa3eb3 6755 if (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE))
6f2f06be
JB
6756
6757 as the case of a missing * on the operand is accepted (perhaps with
6758 a warning, issued further down). */
7b94647a 6759 specific_error = progress (operand_type_mismatch);
0cfa3eb3 6760 if (i.jumpabsolute && t->opcode_modifier.jump != JUMP_ABSOLUTE)
7b94647a 6761 continue;
6f2f06be 6762
a4d3acd2
JB
6763 /* In Intel syntax, normally we can check for memory operand size when
6764 there is no mnemonic suffix. But jmp and call have 2 different
6765 encodings with Dword memory operand size. Skip the "near" one
6766 (permitting a register operand) when "far" was requested. */
6767 if (i.far_branch
6768 && t->opcode_modifier.jump == JUMP_ABSOLUTE
6769 && t->operand_types[0].bitfield.class == Reg)
6770 continue;
6771
5c07affc
L
6772 for (j = 0; j < MAX_OPERANDS; j++)
6773 operand_types[j] = t->operand_types[j];
6774
9db83a32 6775 /* In general, don't allow 32-bit operands on pre-386. */
7b94647a
JB
6776 specific_error = progress (mnem_suffix ? invalid_instruction_suffix
6777 : operand_size_mismatch);
4873e243 6778 j = i.imm_operands + (t->operands > i.imm_operands + 1);
9db83a32
JB
6779 if (i.suffix == LONG_MNEM_SUFFIX
6780 && !cpu_arch_flags.bitfield.cpui386
45aa61fe 6781 && (intel_syntax
3cd7f3e3 6782 ? (t->opcode_modifier.mnemonicsize != IGNORESIZE
45aa61fe
AM
6783 && !intel_float_operand (t->name))
6784 : intel_float_operand (t->name) != 2)
4873e243
JB
6785 && (t->operands == i.imm_operands
6786 || (operand_types[i.imm_operands].bitfield.class != RegMMX
6787 && operand_types[i.imm_operands].bitfield.class != RegSIMD
6788 && operand_types[i.imm_operands].bitfield.class != RegMask)
6789 || (operand_types[j].bitfield.class != RegMMX
6790 && operand_types[j].bitfield.class != RegSIMD
6791 && operand_types[j].bitfield.class != RegMask))
63112cd6 6792 && !t->opcode_modifier.sib)
192dc9c6
JB
6793 continue;
6794
29b0f896 6795 /* Do not verify operands when there are none. */
e365e234 6796 if (!t->operands)
da4977e0
JB
6797 {
6798 if (VEX_check_encoding (t))
6799 {
7b94647a 6800 specific_error = progress (i.error);
da4977e0
JB
6801 continue;
6802 }
6803
6804 /* We've found a match; break out of loop. */
6805 break;
6806 }
252b5132 6807
48bcea9f
JB
6808 if (!t->opcode_modifier.jump
6809 || t->opcode_modifier.jump == JUMP_ABSOLUTE)
6810 {
6811 /* There should be only one Disp operand. */
6812 for (j = 0; j < MAX_OPERANDS; j++)
6813 if (operand_type_check (operand_types[j], disp))
539e75ad 6814 break;
48bcea9f
JB
6815 if (j < MAX_OPERANDS)
6816 {
5b7c81bd 6817 bool override = (i.prefix[ADDR_PREFIX] != 0);
48bcea9f
JB
6818
6819 addr_prefix_disp = j;
6820
a775efc8
JB
6821 /* Address size prefix will turn Disp64 operand into Disp32 and
6822 Disp32/Disp16 one into Disp16/Disp32 respectively. */
48bcea9f 6823 switch (flag_code)
40fb9820 6824 {
48bcea9f
JB
6825 case CODE_16BIT:
6826 override = !override;
6827 /* Fall through. */
6828 case CODE_32BIT:
6829 if (operand_types[j].bitfield.disp32
6830 && operand_types[j].bitfield.disp16)
40fb9820 6831 {
48bcea9f
JB
6832 operand_types[j].bitfield.disp16 = override;
6833 operand_types[j].bitfield.disp32 = !override;
40fb9820 6834 }
a775efc8 6835 gas_assert (!operand_types[j].bitfield.disp64);
48bcea9f
JB
6836 break;
6837
6838 case CODE_64BIT:
a775efc8 6839 if (operand_types[j].bitfield.disp64)
40fb9820 6840 {
a775efc8 6841 gas_assert (!operand_types[j].bitfield.disp32);
48bcea9f 6842 operand_types[j].bitfield.disp32 = override;
a775efc8 6843 operand_types[j].bitfield.disp64 = !override;
40fb9820 6844 }
48bcea9f
JB
6845 operand_types[j].bitfield.disp16 = 0;
6846 break;
40fb9820 6847 }
539e75ad 6848 }
48bcea9f 6849 }
539e75ad 6850
56ffb741 6851 /* We check register size if needed. */
9c19e9ec 6852 if (t->opcode_modifier.checkoperandsize)
e2195274
JB
6853 {
6854 check_register = (1 << t->operands) - 1;
a5748e0d 6855 if (i.broadcast.type || i.broadcast.bytes)
5273a3cd 6856 check_register &= ~(1 << i.broadcast.operand);
e2195274
JB
6857 }
6858 else
6859 check_register = 0;
6860
c6fb90c8 6861 overlap0 = operand_type_and (i.types[0], operand_types[0]);
29b0f896
AM
6862 switch (t->operands)
6863 {
6864 case 1:
40fb9820 6865 if (!operand_type_match (overlap0, i.types[0]))
29b0f896
AM
6866 continue;
6867 break;
6868 case 2:
33eaf5de 6869 /* xchg %eax, %eax is a special case. It is an alias for nop
8b38ad71
L
6870 only in 32bit mode and we can use opcode 0x90. In 64bit
6871 mode, we can't use 0x90 for xchg %eax, %eax since it should
6872 zero-extend %eax to %rax. */
6873 if (flag_code == CODE_64BIT
6874 && t->base_opcode == 0x90
35648716 6875 && t->opcode_modifier.opcodespace == SPACE_BASE
75e5731b
JB
6876 && i.types[0].bitfield.instance == Accum
6877 && i.types[0].bitfield.dword
44732014 6878 && i.types[1].bitfield.instance == Accum)
8b38ad71 6879 continue;
e3669c7f
JB
6880
6881 if (t->base_opcode == MOV_AX_DISP32
e2b983d4 6882 && t->opcode_modifier.opcodespace == SPACE_BASE)
e3669c7f
JB
6883 {
6884 /* Force 0x8b encoding for "mov foo@GOT, %eax". */
6885 if (i.reloc[0] == BFD_RELOC_386_GOT32)
6886 continue;
6887
6888 /* xrelease mov %eax, <disp> is another special case. It must not
6889 match the accumulator-only encoding of mov. */
6890 if (i.hle_prefix)
6891 continue;
6892 }
f5eb1d70
JB
6893 /* Fall through. */
6894
6895 case 3:
3ac21baa
JB
6896 if (!(size_match & MATCH_STRAIGHT))
6897 goto check_reverse;
64c49ab3
JB
6898 /* Reverse direction of operands if swapping is possible in the first
6899 place (operands need to be symmetric) and
6900 - the load form is requested, and the template is a store form,
6901 - the store form is requested, and the template is a load form,
6902 - the non-default (swapped) form is requested. */
6903 overlap1 = operand_type_and (operand_types[0], operand_types[1]);
f5eb1d70 6904 if (t->opcode_modifier.d && i.reg_operands == i.operands
64c49ab3
JB
6905 && !operand_type_all_zero (&overlap1))
6906 switch (i.dir_encoding)
6907 {
6908 case dir_encoding_load:
6909 if (operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 6910 || t->opcode_modifier.regmem)
64c49ab3
JB
6911 goto check_reverse;
6912 break;
6913
6914 case dir_encoding_store:
6915 if (!operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 6916 && !t->opcode_modifier.regmem)
64c49ab3
JB
6917 goto check_reverse;
6918 break;
6919
6920 case dir_encoding_swap:
6921 goto check_reverse;
6922
6923 case dir_encoding_default:
6924 break;
6925 }
86fa6981 6926 /* If we want store form, we skip the current load. */
64c49ab3
JB
6927 if ((i.dir_encoding == dir_encoding_store
6928 || i.dir_encoding == dir_encoding_swap)
86fa6981
L
6929 && i.mem_operands == 0
6930 && t->opcode_modifier.load)
fa99fab2 6931 continue;
1a0670f3 6932 /* Fall through. */
f48ff2ae 6933 case 4:
c0f3af97 6934 case 5:
c6fb90c8 6935 overlap1 = operand_type_and (i.types[1], operand_types[1]);
40fb9820
L
6936 if (!operand_type_match (overlap0, i.types[0])
6937 || !operand_type_match (overlap1, i.types[1])
e2195274 6938 || ((check_register & 3) == 3
dc821c5f 6939 && !operand_type_register_match (i.types[0],
40fb9820 6940 operand_types[0],
dc821c5f 6941 i.types[1],
40fb9820 6942 operand_types[1])))
29b0f896 6943 {
7b94647a
JB
6944 specific_error = progress (i.error);
6945
29b0f896 6946 /* Check if other direction is valid ... */
38e314eb 6947 if (!t->opcode_modifier.d)
29b0f896
AM
6948 continue;
6949
dc1e8a47 6950 check_reverse:
3ac21baa
JB
6951 if (!(size_match & MATCH_REVERSE))
6952 continue;
29b0f896 6953 /* Try reversing direction of operands. */
8bd915b7
JB
6954 j = t->opcode_modifier.vexsources ? 1 : i.operands - 1;
6955 overlap0 = operand_type_and (i.types[0], operand_types[j]);
6956 overlap1 = operand_type_and (i.types[j], operand_types[0]);
c975cec5
JB
6957 overlap2 = operand_type_and (i.types[1], operand_types[1]);
6958 gas_assert (t->operands != 3 || !check_register);
40fb9820 6959 if (!operand_type_match (overlap0, i.types[0])
8bd915b7 6960 || !operand_type_match (overlap1, i.types[j])
c975cec5
JB
6961 || (t->operands == 3
6962 && !operand_type_match (overlap2, i.types[1]))
45664ddb 6963 || (check_register
dc821c5f 6964 && !operand_type_register_match (i.types[0],
8bd915b7
JB
6965 operand_types[j],
6966 i.types[j],
45664ddb 6967 operand_types[0])))
29b0f896
AM
6968 {
6969 /* Does not match either direction. */
7b94647a 6970 specific_error = progress (i.error);
29b0f896
AM
6971 continue;
6972 }
ac9226cf 6973 /* found_reverse_match holds which variant of D
29b0f896 6974 we've found. */
38e314eb
JB
6975 if (!t->opcode_modifier.d)
6976 found_reverse_match = 0;
6977 else if (operand_types[0].bitfield.tbyte)
ac9226cf 6978 {
4943d587
JB
6979 if (t->opcode_modifier.operandconstraint != UGH)
6980 found_reverse_match = Opcode_FloatD;
ac9226cf
JB
6981 /* FSUB{,R} and FDIV{,R} may need a 2nd bit flipped. */
6982 if ((t->base_opcode & 0x20)
6983 && (intel_syntax || intel_mnemonic))
6984 found_reverse_match |= Opcode_FloatR;
6985 }
8bd915b7
JB
6986 else if (t->opcode_modifier.vexsources)
6987 {
6988 found_reverse_match = Opcode_VexW;
6989 goto check_operands_345;
6990 }
2c735193
JB
6991 else if (t->opcode_modifier.opcodespace != SPACE_BASE
6992 && (t->opcode_modifier.opcodespace != SPACE_0F
6993 /* MOV to/from CR/DR/TR, as an exception, follow
6994 the base opcode space encoding model. */
6995 || (t->base_opcode | 7) != 0x27))
dbbc8b7e 6996 found_reverse_match = (t->base_opcode & 0xee) != 0x6e
2c735193 6997 ? Opcode_ExtD : Opcode_SIMD_IntD;
a33ef3c2 6998 else if (!t->opcode_modifier.commutative)
38e314eb 6999 found_reverse_match = Opcode_D;
a33ef3c2
JB
7000 else
7001 found_reverse_match = ~0;
29b0f896 7002 }
f48ff2ae 7003 else
29b0f896 7004 {
f48ff2ae 7005 /* Found a forward 2 operand match here. */
8bd915b7 7006 check_operands_345:
d1cbb4db
L
7007 switch (t->operands)
7008 {
c0f3af97 7009 case 5:
3d0738af 7010 overlap4 = operand_type_and (i.types[4], operand_types[4]);
c0f3af97 7011 if (!operand_type_match (overlap4, i.types[4])
dc821c5f 7012 || !operand_type_register_match (i.types[3],
c0f3af97 7013 operand_types[3],
c0f3af97
L
7014 i.types[4],
7015 operand_types[4]))
7b94647a
JB
7016 {
7017 specific_error = progress (i.error);
7018 continue;
7019 }
1a0670f3 7020 /* Fall through. */
f48ff2ae 7021 case 4:
3d0738af 7022 overlap3 = operand_type_and (i.types[3], operand_types[3]);
40fb9820 7023 if (!operand_type_match (overlap3, i.types[3])
e2195274
JB
7024 || ((check_register & 0xa) == 0xa
7025 && !operand_type_register_match (i.types[1],
f7768225
JB
7026 operand_types[1],
7027 i.types[3],
e2195274
JB
7028 operand_types[3]))
7029 || ((check_register & 0xc) == 0xc
7030 && !operand_type_register_match (i.types[2],
7031 operand_types[2],
7032 i.types[3],
7033 operand_types[3])))
7b94647a
JB
7034 {
7035 specific_error = progress (i.error);
7036 continue;
7037 }
1a0670f3 7038 /* Fall through. */
f48ff2ae 7039 case 3:
3d0738af 7040 overlap2 = operand_type_and (i.types[2], operand_types[2]);
40fb9820 7041 if (!operand_type_match (overlap2, i.types[2])
e2195274
JB
7042 || ((check_register & 5) == 5
7043 && !operand_type_register_match (i.types[0],
23e42951
JB
7044 operand_types[0],
7045 i.types[2],
e2195274
JB
7046 operand_types[2]))
7047 || ((check_register & 6) == 6
7048 && !operand_type_register_match (i.types[1],
7049 operand_types[1],
7050 i.types[2],
7051 operand_types[2])))
7b94647a
JB
7052 {
7053 specific_error = progress (i.error);
7054 continue;
7055 }
f48ff2ae
L
7056 break;
7057 }
29b0f896 7058 }
f48ff2ae 7059 /* Found either forward/reverse 2, 3 or 4 operand match here:
29b0f896
AM
7060 slip through to break. */
7061 }
c0f3af97 7062
9bb4d860
L
7063 /* Check if VEX/EVEX encoding requirements can be satisfied. */
7064 if (VEX_check_encoding (t))
da4977e0 7065 {
7b94647a 7066 specific_error = progress (i.error);
da4977e0
JB
7067 continue;
7068 }
7069
9bb4d860
L
7070 /* Check if vector operands are valid. */
7071 if (check_VecOperands (t))
5614d22c 7072 {
7b94647a 7073 specific_error = progress (i.error);
5614d22c
JB
7074 continue;
7075 }
a683cc34 7076
29b0f896
AM
7077 /* We've found a match; break out of loop. */
7078 break;
7079 }
7080
7b94647a
JB
7081#undef progress
7082
29b0f896
AM
7083 if (t == current_templates->end)
7084 {
7085 /* We found no match. */
04784e33 7086 i.error = specific_error;
fa99fab2 7087 return NULL;
29b0f896 7088 }
252b5132 7089
29b0f896
AM
7090 if (!quiet_warnings)
7091 {
7092 if (!intel_syntax
0cfa3eb3 7093 && (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE)))
6f2f06be 7094 as_warn (_("indirect %s without `*'"), t->name);
29b0f896 7095
40fb9820 7096 if (t->opcode_modifier.isprefix
3cd7f3e3 7097 && t->opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
7098 {
7099 /* Warn them that a data or address size prefix doesn't
7100 affect assembly of the next line of code. */
7101 as_warn (_("stand-alone `%s' prefix"), t->name);
7102 }
7103 }
7104
7105 /* Copy the template we found. */
9a182d04 7106 install_template (t);
539e75ad
L
7107
7108 if (addr_prefix_disp != -1)
7109 i.tm.operand_types[addr_prefix_disp]
7110 = operand_types[addr_prefix_disp];
7111
8bd915b7 7112 switch (found_reverse_match)
29b0f896 7113 {
8bd915b7
JB
7114 case 0:
7115 break;
7116
7117 default:
dfd69174
JB
7118 /* If we found a reverse match we must alter the opcode direction
7119 bit and clear/flip the regmem modifier one. found_reverse_match
7120 holds bits to change (different for int & float insns). */
29b0f896
AM
7121
7122 i.tm.base_opcode ^= found_reverse_match;
7123
dfd69174
JB
7124 /* Certain SIMD insns have their load forms specified in the opcode
7125 table, and hence we need to _set_ RegMem instead of clearing it.
7126 We need to avoid setting the bit though on insns like KMOVW. */
7127 i.tm.opcode_modifier.regmem
7128 = i.tm.opcode_modifier.modrm && i.tm.opcode_modifier.d
7129 && i.tm.operands > 2U - i.tm.opcode_modifier.sse2avx
7130 && !i.tm.opcode_modifier.regmem;
a33ef3c2
JB
7131
7132 /* Fall through. */
7133 case ~0:
7134 i.tm.operand_types[0] = operand_types[i.operands - 1];
7135 i.tm.operand_types[i.operands - 1] = operand_types[0];
8bd915b7
JB
7136 break;
7137
7138 case Opcode_VexW:
7139 /* Only the first two register operands need reversing, alongside
7140 flipping VEX.W. */
7141 i.tm.opcode_modifier.vexw ^= VEXW0 ^ VEXW1;
7142
7143 j = i.tm.operand_types[0].bitfield.imm8;
7144 i.tm.operand_types[j] = operand_types[j + 1];
7145 i.tm.operand_types[j + 1] = operand_types[j];
7146 break;
29b0f896
AM
7147 }
7148
fa99fab2 7149 return t;
29b0f896
AM
7150}
7151
7152static int
e3bb37b5 7153check_string (void)
29b0f896 7154{
51c8edf6
JB
7155 unsigned int es_op = i.tm.opcode_modifier.isstring - IS_STRING_ES_OP0;
7156 unsigned int op = i.tm.operand_types[0].bitfield.baseindex ? es_op : 0;
8dc0818e 7157
5e042380 7158 if (i.seg[op] != NULL && i.seg[op] != reg_es)
29b0f896 7159 {
51c8edf6
JB
7160 as_bad (_("`%s' operand %u must use `%ses' segment"),
7161 i.tm.name,
7162 intel_syntax ? i.tm.operands - es_op : es_op + 1,
7163 register_prefix);
7164 return 0;
29b0f896 7165 }
51c8edf6
JB
7166
7167 /* There's only ever one segment override allowed per instruction.
7168 This instruction possibly has a legal segment override on the
7169 second operand, so copy the segment to where non-string
7170 instructions store it, allowing common code. */
7171 i.seg[op] = i.seg[1];
7172
29b0f896
AM
7173 return 1;
7174}
7175
7176static int
543613e9 7177process_suffix (void)
29b0f896 7178{
5b7c81bd 7179 bool is_crc32 = false, is_movx = false;
8b65b895 7180
29b0f896
AM
7181 /* If matched instruction specifies an explicit instruction mnemonic
7182 suffix, use it. */
673fe0f0 7183 if (i.tm.opcode_modifier.size == SIZE16)
40fb9820 7184 i.suffix = WORD_MNEM_SUFFIX;
673fe0f0 7185 else if (i.tm.opcode_modifier.size == SIZE32)
40fb9820 7186 i.suffix = LONG_MNEM_SUFFIX;
673fe0f0 7187 else if (i.tm.opcode_modifier.size == SIZE64)
40fb9820 7188 i.suffix = QWORD_MNEM_SUFFIX;
13e600d0 7189 else if (i.reg_operands
c8f8eebc 7190 && (i.operands > 1 || i.types[0].bitfield.class == Reg)
255571cd 7191 && i.tm.opcode_modifier.operandconstraint != ADDR_PREFIX_OP_REG)
29b0f896 7192 {
65fca059 7193 unsigned int numop = i.operands;
389d00a5
JB
7194
7195 /* MOVSX/MOVZX */
7196 is_movx = (i.tm.opcode_modifier.opcodespace == SPACE_0F
7197 && (i.tm.base_opcode | 8) == 0xbe)
7198 || (i.tm.opcode_modifier.opcodespace == SPACE_BASE
7199 && i.tm.base_opcode == 0x63
7200 && i.tm.cpu_flags.bitfield.cpu64);
7201
8b65b895 7202 /* CRC32 */
389d00a5
JB
7203 is_crc32 = (i.tm.base_opcode == 0xf0
7204 && i.tm.opcode_modifier.opcodespace == SPACE_0F38
8b65b895 7205 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2);
65fca059
JB
7206
7207 /* movsx/movzx want only their source operand considered here, for the
7208 ambiguity checking below. The suffix will be replaced afterwards
7209 to represent the destination (register). */
389d00a5 7210 if (is_movx && (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63))
65fca059
JB
7211 --i.operands;
7212
643bb870 7213 /* crc32 needs REX.W set regardless of suffix / source operand size. */
8b65b895 7214 if (is_crc32 && i.tm.operand_types[1].bitfield.qword)
643bb870
JB
7215 i.rex |= REX_W;
7216
29b0f896 7217 /* If there's no instruction mnemonic suffix we try to invent one
13e600d0 7218 based on GPR operands. */
29b0f896
AM
7219 if (!i.suffix)
7220 {
7221 /* We take i.suffix from the last register operand specified,
7222 Destination register type is more significant than source
381d071f
L
7223 register type. crc32 in SSE4.2 prefers source register
7224 type. */
8b65b895 7225 unsigned int op = is_crc32 ? 1 : i.operands;
20592a94 7226
1a035124
JB
7227 while (op--)
7228 if (i.tm.operand_types[op].bitfield.instance == InstanceNone
7229 || i.tm.operand_types[op].bitfield.instance == Accum)
7230 {
7231 if (i.types[op].bitfield.class != Reg)
7232 continue;
7233 if (i.types[op].bitfield.byte)
7234 i.suffix = BYTE_MNEM_SUFFIX;
7235 else if (i.types[op].bitfield.word)
7236 i.suffix = WORD_MNEM_SUFFIX;
7237 else if (i.types[op].bitfield.dword)
7238 i.suffix = LONG_MNEM_SUFFIX;
7239 else if (i.types[op].bitfield.qword)
7240 i.suffix = QWORD_MNEM_SUFFIX;
7241 else
7242 continue;
7243 break;
7244 }
65fca059
JB
7245
7246 /* As an exception, movsx/movzx silently default to a byte source
7247 in AT&T mode. */
389d00a5 7248 if (is_movx && i.tm.opcode_modifier.w && !i.suffix && !intel_syntax)
65fca059 7249 i.suffix = BYTE_MNEM_SUFFIX;
29b0f896
AM
7250 }
7251 else if (i.suffix == BYTE_MNEM_SUFFIX)
7252 {
1cb0ab18 7253 if (!check_byte_reg ())
29b0f896
AM
7254 return 0;
7255 }
7256 else if (i.suffix == LONG_MNEM_SUFFIX)
7257 {
1cb0ab18 7258 if (!check_long_reg ())
29b0f896
AM
7259 return 0;
7260 }
7261 else if (i.suffix == QWORD_MNEM_SUFFIX)
7262 {
1cb0ab18 7263 if (!check_qword_reg ())
29b0f896
AM
7264 return 0;
7265 }
7266 else if (i.suffix == WORD_MNEM_SUFFIX)
7267 {
1cb0ab18 7268 if (!check_word_reg ())
29b0f896
AM
7269 return 0;
7270 }
3cd7f3e3
L
7271 else if (intel_syntax
7272 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
7273 /* Do nothing if the instruction is going to ignore the prefix. */
7274 ;
7275 else
7276 abort ();
65fca059
JB
7277
7278 /* Undo the movsx/movzx change done above. */
7279 i.operands = numop;
29b0f896 7280 }
3cd7f3e3
L
7281 else if (i.tm.opcode_modifier.mnemonicsize == DEFAULTSIZE
7282 && !i.suffix)
29b0f896 7283 {
13e600d0
JB
7284 i.suffix = stackop_size;
7285 if (stackop_size == LONG_MNEM_SUFFIX)
06f74c5c
L
7286 {
7287 /* stackop_size is set to LONG_MNEM_SUFFIX for the
7288 .code16gcc directive to support 16-bit mode with
7289 32-bit address. For IRET without a suffix, generate
7290 16-bit IRET (opcode 0xcf) to return from an interrupt
7291 handler. */
13e600d0
JB
7292 if (i.tm.base_opcode == 0xcf)
7293 {
7294 i.suffix = WORD_MNEM_SUFFIX;
7295 as_warn (_("generating 16-bit `iret' for .code16gcc directive"));
7296 }
7297 /* Warn about changed behavior for segment register push/pop. */
7298 else if ((i.tm.base_opcode | 1) == 0x07)
7299 as_warn (_("generating 32-bit `%s', unlike earlier gas versions"),
7300 i.tm.name);
06f74c5c 7301 }
29b0f896 7302 }
c006a730 7303 else if (!i.suffix
0cfa3eb3
JB
7304 && (i.tm.opcode_modifier.jump == JUMP_ABSOLUTE
7305 || i.tm.opcode_modifier.jump == JUMP_BYTE
7306 || i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT
389d00a5
JB
7307 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
7308 && i.tm.base_opcode == 0x01 /* [ls][gi]dt */
64e74474 7309 && i.tm.extension_opcode <= 3)))
9306ca4a
JB
7310 {
7311 switch (flag_code)
7312 {
7313 case CODE_64BIT:
40fb9820 7314 if (!i.tm.opcode_modifier.no_qsuf)
9306ca4a 7315 {
828c2a25
JB
7316 if (i.tm.opcode_modifier.jump == JUMP_BYTE
7317 || i.tm.opcode_modifier.no_lsuf)
7318 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a
JB
7319 break;
7320 }
1a0670f3 7321 /* Fall through. */
9306ca4a 7322 case CODE_32BIT:
40fb9820 7323 if (!i.tm.opcode_modifier.no_lsuf)
9306ca4a
JB
7324 i.suffix = LONG_MNEM_SUFFIX;
7325 break;
7326 case CODE_16BIT:
40fb9820 7327 if (!i.tm.opcode_modifier.no_wsuf)
9306ca4a
JB
7328 i.suffix = WORD_MNEM_SUFFIX;
7329 break;
7330 }
7331 }
252b5132 7332
c006a730 7333 if (!i.suffix
3cd7f3e3 7334 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8
JB
7335 /* Also cover lret/retf/iret in 64-bit mode. */
7336 || (flag_code == CODE_64BIT
7337 && !i.tm.opcode_modifier.no_lsuf
7338 && !i.tm.opcode_modifier.no_qsuf))
3cd7f3e3 7339 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
8bbb3ad8
JB
7340 /* Explicit sizing prefixes are assumed to disambiguate insns. */
7341 && !i.prefix[DATA_PREFIX] && !(i.prefix[REX_PREFIX] & REX_W)
62b3f548
JB
7342 /* Accept FLDENV et al without suffix. */
7343 && (i.tm.opcode_modifier.no_ssuf || i.tm.opcode_modifier.floatmf))
29b0f896 7344 {
6c0946d0 7345 unsigned int suffixes, evex = 0;
c006a730
JB
7346
7347 suffixes = !i.tm.opcode_modifier.no_bsuf;
7348 if (!i.tm.opcode_modifier.no_wsuf)
7349 suffixes |= 1 << 1;
7350 if (!i.tm.opcode_modifier.no_lsuf)
7351 suffixes |= 1 << 2;
c006a730
JB
7352 if (!i.tm.opcode_modifier.no_ssuf)
7353 suffixes |= 1 << 4;
7354 if (flag_code == CODE_64BIT && !i.tm.opcode_modifier.no_qsuf)
7355 suffixes |= 1 << 5;
7356
6c0946d0
JB
7357 /* For [XYZ]MMWORD operands inspect operand sizes. While generally
7358 also suitable for AT&T syntax mode, it was requested that this be
7359 restricted to just Intel syntax. */
a5748e0d
JB
7360 if (intel_syntax && is_any_vex_encoding (&i.tm)
7361 && !i.broadcast.type && !i.broadcast.bytes)
6c0946d0 7362 {
b9915cbc 7363 unsigned int op;
6c0946d0 7364
b9915cbc 7365 for (op = 0; op < i.tm.operands; ++op)
6c0946d0 7366 {
b9915cbc
JB
7367 if (is_evex_encoding (&i.tm)
7368 && !cpu_arch_flags.bitfield.cpuavx512vl)
6c0946d0 7369 {
b9915cbc
JB
7370 if (i.tm.operand_types[op].bitfield.ymmword)
7371 i.tm.operand_types[op].bitfield.xmmword = 0;
7372 if (i.tm.operand_types[op].bitfield.zmmword)
7373 i.tm.operand_types[op].bitfield.ymmword = 0;
7374 if (!i.tm.opcode_modifier.evex
7375 || i.tm.opcode_modifier.evex == EVEXDYN)
7376 i.tm.opcode_modifier.evex = EVEX512;
7377 }
6c0946d0 7378
b9915cbc
JB
7379 if (i.tm.operand_types[op].bitfield.xmmword
7380 + i.tm.operand_types[op].bitfield.ymmword
7381 + i.tm.operand_types[op].bitfield.zmmword < 2)
7382 continue;
6c0946d0 7383
b9915cbc
JB
7384 /* Any properly sized operand disambiguates the insn. */
7385 if (i.types[op].bitfield.xmmword
7386 || i.types[op].bitfield.ymmword
7387 || i.types[op].bitfield.zmmword)
7388 {
7389 suffixes &= ~(7 << 6);
7390 evex = 0;
7391 break;
7392 }
6c0946d0 7393
b9915cbc
JB
7394 if ((i.flags[op] & Operand_Mem)
7395 && i.tm.operand_types[op].bitfield.unspecified)
7396 {
7397 if (i.tm.operand_types[op].bitfield.xmmword)
7398 suffixes |= 1 << 6;
7399 if (i.tm.operand_types[op].bitfield.ymmword)
7400 suffixes |= 1 << 7;
7401 if (i.tm.operand_types[op].bitfield.zmmword)
7402 suffixes |= 1 << 8;
7403 if (is_evex_encoding (&i.tm))
7404 evex = EVEX512;
6c0946d0
JB
7405 }
7406 }
7407 }
7408
7409 /* Are multiple suffixes / operand sizes allowed? */
c006a730 7410 if (suffixes & (suffixes - 1))
9306ca4a 7411 {
873494c8 7412 if (intel_syntax
3cd7f3e3 7413 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8 7414 || operand_check == check_error))
9306ca4a 7415 {
c006a730 7416 as_bad (_("ambiguous operand size for `%s'"), i.tm.name);
9306ca4a
JB
7417 return 0;
7418 }
c006a730 7419 if (operand_check == check_error)
9306ca4a 7420 {
c006a730
JB
7421 as_bad (_("no instruction mnemonic suffix given and "
7422 "no register operands; can't size `%s'"), i.tm.name);
9306ca4a
JB
7423 return 0;
7424 }
c006a730 7425 if (operand_check == check_warning)
873494c8
JB
7426 as_warn (_("%s; using default for `%s'"),
7427 intel_syntax
7428 ? _("ambiguous operand size")
7429 : _("no instruction mnemonic suffix given and "
7430 "no register operands"),
7431 i.tm.name);
c006a730
JB
7432
7433 if (i.tm.opcode_modifier.floatmf)
7434 i.suffix = SHORT_MNEM_SUFFIX;
389d00a5 7435 else if (is_movx)
65fca059 7436 /* handled below */;
6c0946d0
JB
7437 else if (evex)
7438 i.tm.opcode_modifier.evex = evex;
c006a730
JB
7439 else if (flag_code == CODE_16BIT)
7440 i.suffix = WORD_MNEM_SUFFIX;
1a035124 7441 else if (!i.tm.opcode_modifier.no_lsuf)
c006a730 7442 i.suffix = LONG_MNEM_SUFFIX;
1a035124
JB
7443 else
7444 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a 7445 }
29b0f896 7446 }
252b5132 7447
389d00a5 7448 if (is_movx)
65fca059
JB
7449 {
7450 /* In Intel syntax, movsx/movzx must have a "suffix" (checked above).
7451 In AT&T syntax, if there is no suffix (warned about above), the default
7452 will be byte extension. */
7453 if (i.tm.opcode_modifier.w && i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
7454 i.tm.base_opcode |= 1;
7455
7456 /* For further processing, the suffix should represent the destination
7457 (register). This is already the case when one was used with
7458 mov[sz][bw]*, but we need to replace it for mov[sz]x, or if there was
7459 no suffix to begin with. */
7460 if (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63 || !i.suffix)
7461 {
7462 if (i.types[1].bitfield.word)
7463 i.suffix = WORD_MNEM_SUFFIX;
7464 else if (i.types[1].bitfield.qword)
7465 i.suffix = QWORD_MNEM_SUFFIX;
7466 else
7467 i.suffix = LONG_MNEM_SUFFIX;
7468
7469 i.tm.opcode_modifier.w = 0;
7470 }
7471 }
7472
50128d0c
JB
7473 if (!i.tm.opcode_modifier.modrm && i.reg_operands && i.tm.operands < 3)
7474 i.short_form = (i.tm.operand_types[0].bitfield.class == Reg)
7475 != (i.tm.operand_types[1].bitfield.class == Reg);
7476
d2224064
JB
7477 /* Change the opcode based on the operand size given by i.suffix. */
7478 switch (i.suffix)
29b0f896 7479 {
d2224064
JB
7480 /* Size floating point instruction. */
7481 case LONG_MNEM_SUFFIX:
7482 if (i.tm.opcode_modifier.floatmf)
7483 {
7484 i.tm.base_opcode ^= 4;
7485 break;
7486 }
7487 /* fall through */
7488 case WORD_MNEM_SUFFIX:
7489 case QWORD_MNEM_SUFFIX:
29b0f896 7490 /* It's not a byte, select word/dword operation. */
40fb9820 7491 if (i.tm.opcode_modifier.w)
29b0f896 7492 {
50128d0c 7493 if (i.short_form)
29b0f896
AM
7494 i.tm.base_opcode |= 8;
7495 else
7496 i.tm.base_opcode |= 1;
7497 }
d2224064
JB
7498 /* fall through */
7499 case SHORT_MNEM_SUFFIX:
29b0f896
AM
7500 /* Now select between word & dword operations via the operand
7501 size prefix, except for instructions that will ignore this
7502 prefix anyway. */
c8f8eebc 7503 if (i.suffix != QWORD_MNEM_SUFFIX
3cd7f3e3 7504 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
c8f8eebc
JB
7505 && !i.tm.opcode_modifier.floatmf
7506 && !is_any_vex_encoding (&i.tm)
7507 && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
7508 || (flag_code == CODE_64BIT
7509 && i.tm.opcode_modifier.jump == JUMP_BYTE)))
24eab124
AM
7510 {
7511 unsigned int prefix = DATA_PREFIX_OPCODE;
543613e9 7512
0cfa3eb3 7513 if (i.tm.opcode_modifier.jump == JUMP_BYTE) /* jcxz, loop */
29b0f896 7514 prefix = ADDR_PREFIX_OPCODE;
252b5132 7515
29b0f896
AM
7516 if (!add_prefix (prefix))
7517 return 0;
24eab124 7518 }
252b5132 7519
29b0f896
AM
7520 /* Set mode64 for an operand. */
7521 if (i.suffix == QWORD_MNEM_SUFFIX
9146926a 7522 && flag_code == CODE_64BIT
d2224064 7523 && !i.tm.opcode_modifier.norex64
4ed21b58 7524 && !i.tm.opcode_modifier.vexw
46e883c5 7525 /* Special case for xchg %rax,%rax. It is NOP and doesn't
d2224064
JB
7526 need rex64. */
7527 && ! (i.operands == 2
7528 && i.tm.base_opcode == 0x90
44732014 7529 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
75e5731b
JB
7530 && i.types[0].bitfield.instance == Accum
7531 && i.types[0].bitfield.qword
44732014 7532 && i.types[1].bitfield.instance == Accum))
d2224064 7533 i.rex |= REX_W;
3e73aa7c 7534
d2224064 7535 break;
8bbb3ad8
JB
7536
7537 case 0:
f9a6a8f0 7538 /* Select word/dword/qword operation with explicit data sizing prefix
8bbb3ad8
JB
7539 when there are no suitable register operands. */
7540 if (i.tm.opcode_modifier.w
7541 && (i.prefix[DATA_PREFIX] || (i.prefix[REX_PREFIX] & REX_W))
7542 && (!i.reg_operands
7543 || (i.reg_operands == 1
7544 /* ShiftCount */
7545 && (i.tm.operand_types[0].bitfield.instance == RegC
7546 /* InOutPortReg */
7547 || i.tm.operand_types[0].bitfield.instance == RegD
7548 || i.tm.operand_types[1].bitfield.instance == RegD
7549 /* CRC32 */
8b65b895 7550 || is_crc32))))
8bbb3ad8
JB
7551 i.tm.base_opcode |= 1;
7552 break;
29b0f896 7553 }
7ecd2f8b 7554
255571cd 7555 if (i.tm.opcode_modifier.operandconstraint == ADDR_PREFIX_OP_REG)
c0a30a9f 7556 {
c8f8eebc
JB
7557 gas_assert (!i.suffix);
7558 gas_assert (i.reg_operands);
c0a30a9f 7559
c8f8eebc
JB
7560 if (i.tm.operand_types[0].bitfield.instance == Accum
7561 || i.operands == 1)
7562 {
7563 /* The address size override prefix changes the size of the
7564 first operand. */
7565 if (flag_code == CODE_64BIT
7566 && i.op[0].regs->reg_type.bitfield.word)
7567 {
7568 as_bad (_("16-bit addressing unavailable for `%s'"),
7569 i.tm.name);
7570 return 0;
7571 }
7572
7573 if ((flag_code == CODE_32BIT
7574 ? i.op[0].regs->reg_type.bitfield.word
7575 : i.op[0].regs->reg_type.bitfield.dword)
7576 && !add_prefix (ADDR_PREFIX_OPCODE))
7577 return 0;
7578 }
c0a30a9f
L
7579 else
7580 {
c8f8eebc
JB
7581 /* Check invalid register operand when the address size override
7582 prefix changes the size of register operands. */
7583 unsigned int op;
7584 enum { need_word, need_dword, need_qword } need;
7585
27f13469 7586 /* Check the register operand for the address size prefix if
b3a3496f 7587 the memory operand has no real registers, like symbol, DISP
829f3fe1 7588 or bogus (x32-only) symbol(%rip) when symbol(%eip) is meant. */
27f13469
L
7589 if (i.mem_operands == 1
7590 && i.reg_operands == 1
7591 && i.operands == 2
27f13469 7592 && i.types[1].bitfield.class == Reg
b3a3496f
L
7593 && (flag_code == CODE_32BIT
7594 ? i.op[1].regs->reg_type.bitfield.word
7595 : i.op[1].regs->reg_type.bitfield.dword)
7596 && ((i.base_reg == NULL && i.index_reg == NULL)
829f3fe1
JB
7597#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
7598 || (x86_elf_abi == X86_64_X32_ABI
7599 && i.base_reg
b3a3496f
L
7600 && i.base_reg->reg_num == RegIP
7601 && i.base_reg->reg_type.bitfield.qword))
829f3fe1
JB
7602#else
7603 || 0)
7604#endif
27f13469
L
7605 && !add_prefix (ADDR_PREFIX_OPCODE))
7606 return 0;
7607
c8f8eebc
JB
7608 if (flag_code == CODE_32BIT)
7609 need = i.prefix[ADDR_PREFIX] ? need_word : need_dword;
7610 else if (i.prefix[ADDR_PREFIX])
c0a30a9f
L
7611 need = need_dword;
7612 else
7613 need = flag_code == CODE_64BIT ? need_qword : need_word;
c0a30a9f 7614
c8f8eebc
JB
7615 for (op = 0; op < i.operands; op++)
7616 {
7617 if (i.types[op].bitfield.class != Reg)
7618 continue;
7619
7620 switch (need)
7621 {
7622 case need_word:
7623 if (i.op[op].regs->reg_type.bitfield.word)
7624 continue;
7625 break;
7626 case need_dword:
7627 if (i.op[op].regs->reg_type.bitfield.dword)
7628 continue;
7629 break;
7630 case need_qword:
7631 if (i.op[op].regs->reg_type.bitfield.qword)
7632 continue;
7633 break;
7634 }
7635
7636 as_bad (_("invalid register operand size for `%s'"),
7637 i.tm.name);
7638 return 0;
7639 }
7640 }
c0a30a9f
L
7641 }
7642
29b0f896
AM
7643 return 1;
7644}
3e73aa7c 7645
29b0f896 7646static int
543613e9 7647check_byte_reg (void)
29b0f896
AM
7648{
7649 int op;
543613e9 7650
29b0f896
AM
7651 for (op = i.operands; --op >= 0;)
7652 {
dc821c5f 7653 /* Skip non-register operands. */
bab6aec1 7654 if (i.types[op].bitfield.class != Reg)
dc821c5f
JB
7655 continue;
7656
29b0f896
AM
7657 /* If this is an eight bit register, it's OK. If it's the 16 or
7658 32 bit version of an eight bit register, we will just use the
7659 low portion, and that's OK too. */
dc821c5f 7660 if (i.types[op].bitfield.byte)
29b0f896
AM
7661 continue;
7662
5a819eb9 7663 /* I/O port address operands are OK too. */
75e5731b
JB
7664 if (i.tm.operand_types[op].bitfield.instance == RegD
7665 && i.tm.operand_types[op].bitfield.word)
5a819eb9
JB
7666 continue;
7667
9706160a 7668 /* crc32 only wants its source operand checked here. */
389d00a5
JB
7669 if (i.tm.base_opcode == 0xf0
7670 && i.tm.opcode_modifier.opcodespace == SPACE_0F38
8b65b895
L
7671 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2
7672 && op != 0)
9344ff29
L
7673 continue;
7674
29b0f896 7675 /* Any other register is bad. */
73c76375
JB
7676 as_bad (_("`%s%s' not allowed with `%s%c'"),
7677 register_prefix, i.op[op].regs->reg_name,
7678 i.tm.name, i.suffix);
7679 return 0;
29b0f896
AM
7680 }
7681 return 1;
7682}
7683
7684static int
e3bb37b5 7685check_long_reg (void)
29b0f896
AM
7686{
7687 int op;
7688
7689 for (op = i.operands; --op >= 0;)
dc821c5f 7690 /* Skip non-register operands. */
bab6aec1 7691 if (i.types[op].bitfield.class != Reg)
dc821c5f 7692 continue;
29b0f896
AM
7693 /* Reject eight bit registers, except where the template requires
7694 them. (eg. movzb) */
dc821c5f 7695 else if (i.types[op].bitfield.byte
bab6aec1 7696 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7697 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7698 && (i.tm.operand_types[op].bitfield.word
7699 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7700 {
a540244d
L
7701 as_bad (_("`%s%s' not allowed with `%s%c'"),
7702 register_prefix,
29b0f896
AM
7703 i.op[op].regs->reg_name,
7704 i.tm.name,
7705 i.suffix);
7706 return 0;
7707 }
be4c5e58
L
7708 /* Error if the e prefix on a general reg is missing. */
7709 else if (i.types[op].bitfield.word
bab6aec1 7710 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7711 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7712 && i.tm.operand_types[op].bitfield.dword)
29b0f896 7713 {
be4c5e58
L
7714 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7715 register_prefix, i.op[op].regs->reg_name,
7716 i.suffix);
7717 return 0;
252b5132 7718 }
e4630f71 7719 /* Warn if the r prefix on a general reg is present. */
dc821c5f 7720 else if (i.types[op].bitfield.qword
bab6aec1 7721 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7722 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7723 && i.tm.operand_types[op].bitfield.dword)
252b5132 7724 {
1cb0ab18
JB
7725 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7726 register_prefix, i.op[op].regs->reg_name, i.suffix);
7727 return 0;
29b0f896
AM
7728 }
7729 return 1;
7730}
252b5132 7731
29b0f896 7732static int
e3bb37b5 7733check_qword_reg (void)
29b0f896
AM
7734{
7735 int op;
252b5132 7736
29b0f896 7737 for (op = i.operands; --op >= 0; )
dc821c5f 7738 /* Skip non-register operands. */
bab6aec1 7739 if (i.types[op].bitfield.class != Reg)
dc821c5f 7740 continue;
29b0f896
AM
7741 /* Reject eight bit registers, except where the template requires
7742 them. (eg. movzb) */
dc821c5f 7743 else if (i.types[op].bitfield.byte
bab6aec1 7744 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7745 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7746 && (i.tm.operand_types[op].bitfield.word
7747 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7748 {
a540244d
L
7749 as_bad (_("`%s%s' not allowed with `%s%c'"),
7750 register_prefix,
29b0f896
AM
7751 i.op[op].regs->reg_name,
7752 i.tm.name,
7753 i.suffix);
7754 return 0;
7755 }
e4630f71 7756 /* Warn if the r prefix on a general reg is missing. */
dc821c5f
JB
7757 else if ((i.types[op].bitfield.word
7758 || i.types[op].bitfield.dword)
bab6aec1 7759 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7760 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7761 && i.tm.operand_types[op].bitfield.qword)
29b0f896
AM
7762 {
7763 /* Prohibit these changes in the 64bit mode, since the
7764 lowering is more complicated. */
1cb0ab18
JB
7765 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7766 register_prefix, i.op[op].regs->reg_name, i.suffix);
7767 return 0;
252b5132 7768 }
29b0f896
AM
7769 return 1;
7770}
252b5132 7771
29b0f896 7772static int
e3bb37b5 7773check_word_reg (void)
29b0f896
AM
7774{
7775 int op;
7776 for (op = i.operands; --op >= 0;)
dc821c5f 7777 /* Skip non-register operands. */
bab6aec1 7778 if (i.types[op].bitfield.class != Reg)
dc821c5f 7779 continue;
29b0f896
AM
7780 /* Reject eight bit registers, except where the template requires
7781 them. (eg. movzb) */
dc821c5f 7782 else if (i.types[op].bitfield.byte
bab6aec1 7783 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7784 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7785 && (i.tm.operand_types[op].bitfield.word
7786 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7787 {
a540244d
L
7788 as_bad (_("`%s%s' not allowed with `%s%c'"),
7789 register_prefix,
29b0f896
AM
7790 i.op[op].regs->reg_name,
7791 i.tm.name,
7792 i.suffix);
7793 return 0;
7794 }
9706160a
JB
7795 /* Error if the e or r prefix on a general reg is present. */
7796 else if ((i.types[op].bitfield.dword
dc821c5f 7797 || i.types[op].bitfield.qword)
bab6aec1 7798 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7799 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7800 && i.tm.operand_types[op].bitfield.word)
252b5132 7801 {
9706160a
JB
7802 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7803 register_prefix, i.op[op].regs->reg_name,
7804 i.suffix);
7805 return 0;
29b0f896
AM
7806 }
7807 return 1;
7808}
252b5132 7809
29b0f896 7810static int
40fb9820 7811update_imm (unsigned int j)
29b0f896 7812{
bc0844ae 7813 i386_operand_type overlap = i.types[j];
be1643ff
JB
7814 if (overlap.bitfield.imm8
7815 + overlap.bitfield.imm8s
7816 + overlap.bitfield.imm16
7817 + overlap.bitfield.imm32
7818 + overlap.bitfield.imm32s
7819 + overlap.bitfield.imm64 > 1)
29b0f896 7820 {
05909f23
JB
7821 static const i386_operand_type imm16 = { .bitfield = { .imm16 = 1 } };
7822 static const i386_operand_type imm32 = { .bitfield = { .imm32 = 1 } };
7823 static const i386_operand_type imm32s = { .bitfield = { .imm32s = 1 } };
7824 static const i386_operand_type imm16_32 = { .bitfield =
7825 { .imm16 = 1, .imm32 = 1 }
7826 };
7827 static const i386_operand_type imm16_32s = { .bitfield =
7828 { .imm16 = 1, .imm32s = 1 }
7829 };
7830 static const i386_operand_type imm16_32_32s = { .bitfield =
7831 { .imm16 = 1, .imm32 = 1, .imm32s = 1 }
7832 };
7833
29b0f896
AM
7834 if (i.suffix)
7835 {
40fb9820
L
7836 i386_operand_type temp;
7837
0dfbf9d7 7838 operand_type_set (&temp, 0);
7ab9ffdd 7839 if (i.suffix == BYTE_MNEM_SUFFIX)
40fb9820
L
7840 {
7841 temp.bitfield.imm8 = overlap.bitfield.imm8;
7842 temp.bitfield.imm8s = overlap.bitfield.imm8s;
7843 }
7844 else if (i.suffix == WORD_MNEM_SUFFIX)
7845 temp.bitfield.imm16 = overlap.bitfield.imm16;
7846 else if (i.suffix == QWORD_MNEM_SUFFIX)
7847 {
7848 temp.bitfield.imm64 = overlap.bitfield.imm64;
7849 temp.bitfield.imm32s = overlap.bitfield.imm32s;
7850 }
7851 else
7852 temp.bitfield.imm32 = overlap.bitfield.imm32;
7853 overlap = temp;
29b0f896 7854 }
0dfbf9d7
L
7855 else if (operand_type_equal (&overlap, &imm16_32_32s)
7856 || operand_type_equal (&overlap, &imm16_32)
7857 || operand_type_equal (&overlap, &imm16_32s))
29b0f896 7858 {
40fb9820 7859 if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
65da13b5 7860 overlap = imm16;
40fb9820 7861 else
65da13b5 7862 overlap = imm32s;
29b0f896 7863 }
8bbb3ad8
JB
7864 else if (i.prefix[REX_PREFIX] & REX_W)
7865 overlap = operand_type_and (overlap, imm32s);
7866 else if (i.prefix[DATA_PREFIX])
7867 overlap = operand_type_and (overlap,
7868 flag_code != CODE_16BIT ? imm16 : imm32);
be1643ff
JB
7869 if (overlap.bitfield.imm8
7870 + overlap.bitfield.imm8s
7871 + overlap.bitfield.imm16
7872 + overlap.bitfield.imm32
7873 + overlap.bitfield.imm32s
7874 + overlap.bitfield.imm64 != 1)
29b0f896 7875 {
4eed87de
AM
7876 as_bad (_("no instruction mnemonic suffix given; "
7877 "can't determine immediate size"));
29b0f896
AM
7878 return 0;
7879 }
7880 }
40fb9820 7881 i.types[j] = overlap;
29b0f896 7882
40fb9820
L
7883 return 1;
7884}
7885
7886static int
7887finalize_imm (void)
7888{
bc0844ae 7889 unsigned int j, n;
29b0f896 7890
bc0844ae
L
7891 /* Update the first 2 immediate operands. */
7892 n = i.operands > 2 ? 2 : i.operands;
7893 if (n)
7894 {
7895 for (j = 0; j < n; j++)
7896 if (update_imm (j) == 0)
7897 return 0;
40fb9820 7898
bc0844ae
L
7899 /* The 3rd operand can't be immediate operand. */
7900 gas_assert (operand_type_check (i.types[2], imm) == 0);
7901 }
29b0f896
AM
7902
7903 return 1;
7904}
7905
7906static int
e3bb37b5 7907process_operands (void)
29b0f896
AM
7908{
7909 /* Default segment register this instruction will use for memory
7910 accesses. 0 means unknown. This is only for optimizing out
7911 unnecessary segment overrides. */
5e042380 7912 const reg_entry *default_seg = NULL;
29b0f896 7913
a5aeccd9
JB
7914 if (i.tm.opcode_modifier.sse2avx)
7915 {
7916 /* Legacy encoded insns allow explicit REX prefixes, so these prefixes
7917 need converting. */
7918 i.rex |= i.prefix[REX_PREFIX] & (REX_W | REX_R | REX_X | REX_B);
7919 i.prefix[REX_PREFIX] = 0;
7920 i.rex_encoding = 0;
7921 }
c423d21a
JB
7922 /* ImmExt should be processed after SSE2AVX. */
7923 else if (i.tm.opcode_modifier.immext)
7924 process_immext ();
a5aeccd9 7925
2426c15f 7926 if (i.tm.opcode_modifier.sse2avx && i.tm.opcode_modifier.vexvvvv)
29b0f896 7927 {
05909f23
JB
7928 static const i386_operand_type regxmm = {
7929 .bitfield = { .class = RegSIMD, .xmmword = 1 }
7930 };
91d6fa6a
NC
7931 unsigned int dupl = i.operands;
7932 unsigned int dest = dupl - 1;
9fcfb3d7
L
7933 unsigned int j;
7934
c0f3af97 7935 /* The destination must be an xmm register. */
9c2799c2 7936 gas_assert (i.reg_operands
91d6fa6a 7937 && MAX_OPERANDS > dupl
7ab9ffdd 7938 && operand_type_equal (&i.types[dest], &regxmm));
c0f3af97 7939
75e5731b 7940 if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 7941 && i.tm.operand_types[0].bitfield.xmmword)
e2ec9d29 7942 {
8cd7925b 7943 if (i.tm.opcode_modifier.vexsources == VEX3SOURCES)
c0f3af97
L
7944 {
7945 /* Keep xmm0 for instructions with VEX prefix and 3
7946 sources. */
75e5731b 7947 i.tm.operand_types[0].bitfield.instance = InstanceNone;
3528c362 7948 i.tm.operand_types[0].bitfield.class = RegSIMD;
c0f3af97
L
7949 goto duplicate;
7950 }
e2ec9d29 7951 else
c0f3af97
L
7952 {
7953 /* We remove the first xmm0 and keep the number of
7954 operands unchanged, which in fact duplicates the
7955 destination. */
7956 for (j = 1; j < i.operands; j++)
7957 {
7958 i.op[j - 1] = i.op[j];
7959 i.types[j - 1] = i.types[j];
7960 i.tm.operand_types[j - 1] = i.tm.operand_types[j];
8dc0818e 7961 i.flags[j - 1] = i.flags[j];
c0f3af97
L
7962 }
7963 }
7964 }
255571cd 7965 else if (i.tm.opcode_modifier.operandconstraint == IMPLICIT_1ST_XMM0)
7ab9ffdd 7966 {
91d6fa6a 7967 gas_assert ((MAX_OPERANDS - 1) > dupl
8cd7925b
L
7968 && (i.tm.opcode_modifier.vexsources
7969 == VEX3SOURCES));
c0f3af97
L
7970
7971 /* Add the implicit xmm0 for instructions with VEX prefix
7972 and 3 sources. */
7973 for (j = i.operands; j > 0; j--)
7974 {
7975 i.op[j] = i.op[j - 1];
7976 i.types[j] = i.types[j - 1];
7977 i.tm.operand_types[j] = i.tm.operand_types[j - 1];
8dc0818e 7978 i.flags[j] = i.flags[j - 1];
c0f3af97
L
7979 }
7980 i.op[0].regs
629310ab 7981 = (const reg_entry *) str_hash_find (reg_hash, "xmm0");
7ab9ffdd 7982 i.types[0] = regxmm;
c0f3af97
L
7983 i.tm.operand_types[0] = regxmm;
7984
7985 i.operands += 2;
7986 i.reg_operands += 2;
7987 i.tm.operands += 2;
7988
91d6fa6a 7989 dupl++;
c0f3af97 7990 dest++;
91d6fa6a
NC
7991 i.op[dupl] = i.op[dest];
7992 i.types[dupl] = i.types[dest];
7993 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 7994 i.flags[dupl] = i.flags[dest];
e2ec9d29 7995 }
c0f3af97
L
7996 else
7997 {
dc1e8a47 7998 duplicate:
c0f3af97
L
7999 i.operands++;
8000 i.reg_operands++;
8001 i.tm.operands++;
8002
91d6fa6a
NC
8003 i.op[dupl] = i.op[dest];
8004 i.types[dupl] = i.types[dest];
8005 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 8006 i.flags[dupl] = i.flags[dest];
c0f3af97
L
8007 }
8008
8009 if (i.tm.opcode_modifier.immext)
8010 process_immext ();
8011 }
75e5731b 8012 else if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 8013 && i.tm.operand_types[0].bitfield.xmmword)
c0f3af97
L
8014 {
8015 unsigned int j;
8016
9fcfb3d7
L
8017 for (j = 1; j < i.operands; j++)
8018 {
8019 i.op[j - 1] = i.op[j];
8020 i.types[j - 1] = i.types[j];
8021
8022 /* We need to adjust fields in i.tm since they are used by
8023 build_modrm_byte. */
8024 i.tm.operand_types [j - 1] = i.tm.operand_types [j];
8dc0818e
JB
8025
8026 i.flags[j - 1] = i.flags[j];
9fcfb3d7
L
8027 }
8028
e2ec9d29
L
8029 i.operands--;
8030 i.reg_operands--;
e2ec9d29
L
8031 i.tm.operands--;
8032 }
255571cd 8033 else if (i.tm.opcode_modifier.operandconstraint == IMPLICIT_QUAD_GROUP)
920d2ddc 8034 {
a477a8c4
JB
8035 unsigned int regnum, first_reg_in_group, last_reg_in_group;
8036
920d2ddc 8037 /* The second operand must be {x,y,z}mmN, where N is a multiple of 4. */
3528c362 8038 gas_assert (i.operands >= 2 && i.types[1].bitfield.class == RegSIMD);
a477a8c4
JB
8039 regnum = register_number (i.op[1].regs);
8040 first_reg_in_group = regnum & ~3;
8041 last_reg_in_group = first_reg_in_group + 3;
8042 if (regnum != first_reg_in_group)
8043 as_warn (_("source register `%s%s' implicitly denotes"
8044 " `%s%.3s%u' to `%s%.3s%u' source group in `%s'"),
8045 register_prefix, i.op[1].regs->reg_name,
8046 register_prefix, i.op[1].regs->reg_name, first_reg_in_group,
8047 register_prefix, i.op[1].regs->reg_name, last_reg_in_group,
8048 i.tm.name);
8049 }
255571cd 8050 else if (i.tm.opcode_modifier.operandconstraint == REG_KLUDGE)
e2ec9d29
L
8051 {
8052 /* The imul $imm, %reg instruction is converted into
8053 imul $imm, %reg, %reg, and the clr %reg instruction
8054 is converted into xor %reg, %reg. */
8055
8056 unsigned int first_reg_op;
8057
8058 if (operand_type_check (i.types[0], reg))
8059 first_reg_op = 0;
8060 else
8061 first_reg_op = 1;
8062 /* Pretend we saw the extra register operand. */
9c2799c2 8063 gas_assert (i.reg_operands == 1
7ab9ffdd 8064 && i.op[first_reg_op + 1].regs == 0);
e2ec9d29
L
8065 i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
8066 i.types[first_reg_op + 1] = i.types[first_reg_op];
8067 i.operands++;
8068 i.reg_operands++;
29b0f896
AM
8069 }
8070
85b80b0f 8071 if (i.tm.opcode_modifier.modrm)
29b0f896
AM
8072 {
8073 /* The opcode is completed (modulo i.tm.extension_opcode which
52271982
AM
8074 must be put into the modrm byte). Now, we make the modrm and
8075 index base bytes based on all the info we've collected. */
29b0f896
AM
8076
8077 default_seg = build_modrm_byte ();
8078 }
00cee14f 8079 else if (i.types[0].bitfield.class == SReg)
85b80b0f
JB
8080 {
8081 if (flag_code != CODE_64BIT
8082 ? i.tm.base_opcode == POP_SEG_SHORT
8083 && i.op[0].regs->reg_num == 1
389d00a5 8084 : (i.tm.base_opcode | 1) == (POP_SEG386_SHORT & 0xff)
85b80b0f
JB
8085 && i.op[0].regs->reg_num < 4)
8086 {
8087 as_bad (_("you can't `%s %s%s'"),
8088 i.tm.name, register_prefix, i.op[0].regs->reg_name);
8089 return 0;
8090 }
389d00a5
JB
8091 if (i.op[0].regs->reg_num > 3
8092 && i.tm.opcode_modifier.opcodespace == SPACE_BASE )
85b80b0f 8093 {
389d00a5
JB
8094 i.tm.base_opcode ^= (POP_SEG_SHORT ^ POP_SEG386_SHORT) & 0xff;
8095 i.tm.opcode_modifier.opcodespace = SPACE_0F;
85b80b0f
JB
8096 }
8097 i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
8098 }
389d00a5
JB
8099 else if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
8100 && (i.tm.base_opcode & ~3) == MOV_AX_DISP32)
29b0f896 8101 {
5e042380 8102 default_seg = reg_ds;
29b0f896 8103 }
40fb9820 8104 else if (i.tm.opcode_modifier.isstring)
29b0f896
AM
8105 {
8106 /* For the string instructions that allow a segment override
8107 on one of their operands, the default segment is ds. */
5e042380 8108 default_seg = reg_ds;
29b0f896 8109 }
50128d0c 8110 else if (i.short_form)
85b80b0f
JB
8111 {
8112 /* The register or float register operand is in operand
8113 0 or 1. */
4943d587 8114 const reg_entry *r = i.op[0].regs;
85b80b0f 8115
4943d587
JB
8116 if (i.imm_operands
8117 || (r->reg_type.bitfield.instance == Accum && i.op[1].regs))
8118 r = i.op[1].regs;
85b80b0f 8119 /* Register goes in low 3 bits of opcode. */
4943d587
JB
8120 i.tm.base_opcode |= r->reg_num;
8121 if ((r->reg_flags & RegRex) != 0)
85b80b0f 8122 i.rex |= REX_B;
255571cd 8123 if (!quiet_warnings && i.tm.opcode_modifier.operandconstraint == UGH)
85b80b0f 8124 {
4943d587
JB
8125 /* Warn about some common errors, but press on regardless. */
8126 if (i.operands != 2)
85b80b0f
JB
8127 {
8128 /* Extraneous `l' suffix on fp insn. */
8129 as_warn (_("translating to `%s %s%s'"), i.tm.name,
8130 register_prefix, i.op[0].regs->reg_name);
8131 }
4943d587
JB
8132 else if (i.op[0].regs->reg_type.bitfield.instance != Accum)
8133 {
8134 /* Reversed arguments on faddp or fmulp. */
8135 as_warn (_("translating to `%s %s%s,%s%s'"), i.tm.name,
8136 register_prefix, i.op[!intel_syntax].regs->reg_name,
8137 register_prefix, i.op[intel_syntax].regs->reg_name);
8138 }
85b80b0f
JB
8139 }
8140 }
29b0f896 8141
514a8bb0 8142 if ((i.seg[0] || i.prefix[SEG_PREFIX])
514a8bb0 8143 && i.tm.base_opcode == 0x8d /* lea */
35648716 8144 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
514a8bb0 8145 && !is_any_vex_encoding(&i.tm))
92334ad2
JB
8146 {
8147 if (!quiet_warnings)
8148 as_warn (_("segment override on `%s' is ineffectual"), i.tm.name);
8149 if (optimize)
8150 {
8151 i.seg[0] = NULL;
8152 i.prefix[SEG_PREFIX] = 0;
8153 }
8154 }
52271982
AM
8155
8156 /* If a segment was explicitly specified, and the specified segment
b6773884
JB
8157 is neither the default nor the one already recorded from a prefix,
8158 use an opcode prefix to select it. If we never figured out what
8159 the default segment is, then default_seg will be zero at this
8160 point, and the specified segment prefix will always be used. */
8161 if (i.seg[0]
8162 && i.seg[0] != default_seg
5e042380 8163 && i386_seg_prefixes[i.seg[0]->reg_num] != i.prefix[SEG_PREFIX])
29b0f896 8164 {
5e042380 8165 if (!add_prefix (i386_seg_prefixes[i.seg[0]->reg_num]))
29b0f896
AM
8166 return 0;
8167 }
8168 return 1;
8169}
8170
a5aeccd9 8171static INLINE void set_rex_vrex (const reg_entry *r, unsigned int rex_bit,
5b7c81bd 8172 bool do_sse2avx)
a5aeccd9
JB
8173{
8174 if (r->reg_flags & RegRex)
8175 {
8176 if (i.rex & rex_bit)
8177 as_bad (_("same type of prefix used twice"));
8178 i.rex |= rex_bit;
8179 }
8180 else if (do_sse2avx && (i.rex & rex_bit) && i.vex.register_specifier)
8181 {
8182 gas_assert (i.vex.register_specifier == r);
8183 i.vex.register_specifier += 8;
8184 }
8185
8186 if (r->reg_flags & RegVRex)
8187 i.vrex |= rex_bit;
8188}
8189
5e042380 8190static const reg_entry *
e3bb37b5 8191build_modrm_byte (void)
29b0f896 8192{
5e042380 8193 const reg_entry *default_seg = NULL;
c0f3af97 8194 unsigned int source, dest;
8cd7925b 8195 int vex_3_sources;
c0f3af97 8196
8cd7925b 8197 vex_3_sources = i.tm.opcode_modifier.vexsources == VEX3SOURCES;
c0f3af97
L
8198 if (vex_3_sources)
8199 {
91d6fa6a 8200 unsigned int nds, reg_slot;
4c2c6516 8201 expressionS *exp;
c0f3af97 8202
6b8d3588 8203 dest = i.operands - 1;
c0f3af97 8204 nds = dest - 1;
922d8de8 8205
a683cc34 8206 /* There are 2 kinds of instructions:
bed3d976 8207 1. 5 operands: 4 register operands or 3 register operands
9d3bf266 8208 plus 1 memory operand plus one Imm4 operand, VexXDS, and
bed3d976 8209 VexW0 or VexW1. The destination must be either XMM, YMM or
43234a1e 8210 ZMM register.
bed3d976 8211 2. 4 operands: 4 register operands or 3 register operands
2f1bada2 8212 plus 1 memory operand, with VexXDS. */
922d8de8 8213 gas_assert ((i.reg_operands == 4
bed3d976
JB
8214 || (i.reg_operands == 3 && i.mem_operands == 1))
8215 && i.tm.opcode_modifier.vexvvvv == VEXXDS
dcd7e323 8216 && i.tm.opcode_modifier.vexw
3528c362 8217 && i.tm.operand_types[dest].bitfield.class == RegSIMD);
a683cc34 8218
48db9223
JB
8219 /* If VexW1 is set, the first non-immediate operand is the source and
8220 the second non-immediate one is encoded in the immediate operand. */
8221 if (i.tm.opcode_modifier.vexw == VEXW1)
8222 {
8223 source = i.imm_operands;
8224 reg_slot = i.imm_operands + 1;
8225 }
8226 else
8227 {
8228 source = i.imm_operands + 1;
8229 reg_slot = i.imm_operands;
8230 }
8231
a683cc34 8232 if (i.imm_operands == 0)
bed3d976
JB
8233 {
8234 /* When there is no immediate operand, generate an 8bit
8235 immediate operand to encode the first operand. */
8236 exp = &im_expressions[i.imm_operands++];
8237 i.op[i.operands].imms = exp;
be1643ff 8238 i.types[i.operands].bitfield.imm8 = 1;
bed3d976
JB
8239 i.operands++;
8240
3528c362 8241 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
bed3d976
JB
8242 exp->X_op = O_constant;
8243 exp->X_add_number = register_number (i.op[reg_slot].regs) << 4;
43234a1e
L
8244 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
8245 }
922d8de8 8246 else
bed3d976 8247 {
9d3bf266
JB
8248 gas_assert (i.imm_operands == 1);
8249 gas_assert (fits_in_imm4 (i.op[0].imms->X_add_number));
8250 gas_assert (!i.tm.opcode_modifier.immext);
a683cc34 8251
9d3bf266
JB
8252 /* Turn on Imm8 again so that output_imm will generate it. */
8253 i.types[0].bitfield.imm8 = 1;
bed3d976 8254
3528c362 8255 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
9d3bf266 8256 i.op[0].imms->X_add_number
bed3d976 8257 |= register_number (i.op[reg_slot].regs) << 4;
43234a1e 8258 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
bed3d976 8259 }
a683cc34 8260
3528c362 8261 gas_assert (i.tm.operand_types[nds].bitfield.class == RegSIMD);
dae39acc 8262 i.vex.register_specifier = i.op[nds].regs;
c0f3af97
L
8263 }
8264 else
8265 source = dest = 0;
29b0f896
AM
8266
8267 /* i.reg_operands MUST be the number of real register operands;
c0f3af97
L
8268 implicit registers do not count. If there are 3 register
8269 operands, it must be a instruction with VexNDS. For a
8270 instruction with VexNDD, the destination register is encoded
8271 in VEX prefix. If there are 4 register operands, it must be
8272 a instruction with VEX prefix and 3 sources. */
7ab9ffdd
L
8273 if (i.mem_operands == 0
8274 && ((i.reg_operands == 2
2426c15f 8275 && i.tm.opcode_modifier.vexvvvv <= VEXXDS)
7ab9ffdd 8276 || (i.reg_operands == 3
2426c15f 8277 && i.tm.opcode_modifier.vexvvvv == VEXXDS)
7ab9ffdd 8278 || (i.reg_operands == 4 && vex_3_sources)))
29b0f896 8279 {
cab737b9
L
8280 switch (i.operands)
8281 {
8282 case 2:
8283 source = 0;
8284 break;
8285 case 3:
c81128dc
L
8286 /* When there are 3 operands, one of them may be immediate,
8287 which may be the first or the last operand. Otherwise,
c0f3af97
L
8288 the first operand must be shift count register (cl) or it
8289 is an instruction with VexNDS. */
9c2799c2 8290 gas_assert (i.imm_operands == 1
7ab9ffdd 8291 || (i.imm_operands == 0
2426c15f 8292 && (i.tm.opcode_modifier.vexvvvv == VEXXDS
75e5731b
JB
8293 || (i.types[0].bitfield.instance == RegC
8294 && i.types[0].bitfield.byte))));
40fb9820 8295 if (operand_type_check (i.types[0], imm)
75e5731b
JB
8296 || (i.types[0].bitfield.instance == RegC
8297 && i.types[0].bitfield.byte))
40fb9820
L
8298 source = 1;
8299 else
8300 source = 0;
cab737b9
L
8301 break;
8302 case 4:
368d64cc
L
8303 /* When there are 4 operands, the first two must be 8bit
8304 immediate operands. The source operand will be the 3rd
c0f3af97
L
8305 one.
8306
8307 For instructions with VexNDS, if the first operand
8308 an imm8, the source operand is the 2nd one. If the last
8309 operand is imm8, the source operand is the first one. */
9c2799c2 8310 gas_assert ((i.imm_operands == 2
7ab9ffdd
L
8311 && i.types[0].bitfield.imm8
8312 && i.types[1].bitfield.imm8)
2426c15f 8313 || (i.tm.opcode_modifier.vexvvvv == VEXXDS
7ab9ffdd
L
8314 && i.imm_operands == 1
8315 && (i.types[0].bitfield.imm8
cf665fee 8316 || i.types[i.operands - 1].bitfield.imm8)));
9f2670f2
L
8317 if (i.imm_operands == 2)
8318 source = 2;
8319 else
c0f3af97
L
8320 {
8321 if (i.types[0].bitfield.imm8)
8322 source = 1;
8323 else
8324 source = 0;
8325 }
c0f3af97
L
8326 break;
8327 case 5:
cf665fee
JB
8328 gas_assert (!is_evex_encoding (&i.tm));
8329 gas_assert (i.imm_operands == 1 && vex_3_sources);
cab737b9
L
8330 break;
8331 default:
8332 abort ();
8333 }
8334
c0f3af97
L
8335 if (!vex_3_sources)
8336 {
8337 dest = source + 1;
8338
2426c15f 8339 if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
c0f3af97 8340 {
43234a1e 8341 /* For instructions with VexNDS, the register-only source
c5d0745b 8342 operand must be a 32/64bit integer, XMM, YMM, ZMM, or mask
dfd69174 8343 register. It is encoded in VEX prefix. */
f12dc422
L
8344
8345 i386_operand_type op;
8346 unsigned int vvvv;
8347
c2ecccb3 8348 /* Swap two source operands if needed. */
255571cd 8349 if (i.tm.opcode_modifier.operandconstraint == SWAP_SOURCES)
f12dc422
L
8350 {
8351 vvvv = source;
8352 source = dest;
8353 }
8354 else
8355 vvvv = dest;
8356
8357 op = i.tm.operand_types[vvvv];
c0f3af97 8358 if ((dest + 1) >= i.operands
bab6aec1 8359 || ((op.bitfield.class != Reg
dc821c5f 8360 || (!op.bitfield.dword && !op.bitfield.qword))
3528c362 8361 && op.bitfield.class != RegSIMD
c0f327b8 8362 && op.bitfield.class != RegMask))
c0f3af97 8363 abort ();
f12dc422 8364 i.vex.register_specifier = i.op[vvvv].regs;
c0f3af97
L
8365 dest++;
8366 }
8367 }
29b0f896
AM
8368
8369 i.rm.mode = 3;
dfd69174
JB
8370 /* One of the register operands will be encoded in the i.rm.reg
8371 field, the other in the combined i.rm.mode and i.rm.regmem
29b0f896
AM
8372 fields. If no form of this instruction supports a memory
8373 destination operand, then we assume the source operand may
8374 sometimes be a memory operand and so we need to store the
8375 destination in the i.rm.reg field. */
dfd69174 8376 if (!i.tm.opcode_modifier.regmem
40fb9820 8377 && operand_type_check (i.tm.operand_types[dest], anymem) == 0)
29b0f896
AM
8378 {
8379 i.rm.reg = i.op[dest].regs->reg_num;
8380 i.rm.regmem = i.op[source].regs->reg_num;
a5aeccd9 8381 set_rex_vrex (i.op[dest].regs, REX_R, i.tm.opcode_modifier.sse2avx);
5b7c81bd 8382 set_rex_vrex (i.op[source].regs, REX_B, false);
29b0f896
AM
8383 }
8384 else
8385 {
8386 i.rm.reg = i.op[source].regs->reg_num;
8387 i.rm.regmem = i.op[dest].regs->reg_num;
a5aeccd9 8388 set_rex_vrex (i.op[dest].regs, REX_B, i.tm.opcode_modifier.sse2avx);
5b7c81bd 8389 set_rex_vrex (i.op[source].regs, REX_R, false);
29b0f896 8390 }
e0c7f900 8391 if (flag_code != CODE_64BIT && (i.rex & REX_R))
c4a530c5 8392 {
4a5c67ed 8393 if (i.types[!i.tm.opcode_modifier.regmem].bitfield.class != RegCR)
c4a530c5 8394 abort ();
e0c7f900 8395 i.rex &= ~REX_R;
c4a530c5
JB
8396 add_prefix (LOCK_PREFIX_OPCODE);
8397 }
29b0f896
AM
8398 }
8399 else
8400 { /* If it's not 2 reg operands... */
c0f3af97
L
8401 unsigned int mem;
8402
29b0f896
AM
8403 if (i.mem_operands)
8404 {
8405 unsigned int fake_zero_displacement = 0;
99018f42 8406 unsigned int op;
4eed87de 8407
7ab9ffdd 8408 for (op = 0; op < i.operands; op++)
8dc0818e 8409 if (i.flags[op] & Operand_Mem)
7ab9ffdd 8410 break;
7ab9ffdd 8411 gas_assert (op < i.operands);
29b0f896 8412
63112cd6 8413 if (i.tm.opcode_modifier.sib)
6c30d220 8414 {
260cd341
LC
8415 /* The index register of VSIB shouldn't be RegIZ. */
8416 if (i.tm.opcode_modifier.sib != SIBMEM
8417 && i.index_reg->reg_num == RegIZ)
6c30d220
L
8418 abort ();
8419
8420 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8421 if (!i.base_reg)
8422 {
8423 i.sib.base = NO_BASE_REGISTER;
8424 i.sib.scale = i.log2_scale_factor;
2f2be86b 8425 i.types[op] = operand_type_and_not (i.types[op], anydisp);
a775efc8 8426 i.types[op].bitfield.disp32 = 1;
6c30d220 8427 }
260cd341
LC
8428
8429 /* Since the mandatory SIB always has index register, so
8430 the code logic remains unchanged. The non-mandatory SIB
8431 without index register is allowed and will be handled
8432 later. */
8433 if (i.index_reg)
8434 {
8435 if (i.index_reg->reg_num == RegIZ)
8436 i.sib.index = NO_INDEX_REGISTER;
8437 else
8438 i.sib.index = i.index_reg->reg_num;
5b7c81bd 8439 set_rex_vrex (i.index_reg, REX_X, false);
260cd341 8440 }
6c30d220
L
8441 }
8442
5e042380 8443 default_seg = reg_ds;
29b0f896
AM
8444
8445 if (i.base_reg == 0)
8446 {
8447 i.rm.mode = 0;
8448 if (!i.disp_operands)
9bb129e8 8449 fake_zero_displacement = 1;
29b0f896
AM
8450 if (i.index_reg == 0)
8451 {
260cd341
LC
8452 /* Both check for VSIB and mandatory non-vector SIB. */
8453 gas_assert (!i.tm.opcode_modifier.sib
8454 || i.tm.opcode_modifier.sib == SIBMEM);
29b0f896 8455 /* Operand is just <disp> */
2f2be86b 8456 i.types[op] = operand_type_and_not (i.types[op], anydisp);
20f0a1fc 8457 if (flag_code == CODE_64BIT)
29b0f896
AM
8458 {
8459 /* 64bit mode overwrites the 32bit absolute
8460 addressing by RIP relative addressing and
8461 absolute addressing is encoded by one of the
8462 redundant SIB forms. */
8463 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8464 i.sib.base = NO_BASE_REGISTER;
8465 i.sib.index = NO_INDEX_REGISTER;
a775efc8 8466 i.types[op].bitfield.disp32 = 1;
20f0a1fc 8467 }
fc225355
L
8468 else if ((flag_code == CODE_16BIT)
8469 ^ (i.prefix[ADDR_PREFIX] != 0))
20f0a1fc
NC
8470 {
8471 i.rm.regmem = NO_BASE_REGISTER_16;
2f2be86b 8472 i.types[op].bitfield.disp16 = 1;
20f0a1fc
NC
8473 }
8474 else
8475 {
8476 i.rm.regmem = NO_BASE_REGISTER;
2f2be86b 8477 i.types[op].bitfield.disp32 = 1;
29b0f896
AM
8478 }
8479 }
63112cd6 8480 else if (!i.tm.opcode_modifier.sib)
29b0f896 8481 {
6c30d220 8482 /* !i.base_reg && i.index_reg */
e968fc9b 8483 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8484 i.sib.index = NO_INDEX_REGISTER;
8485 else
8486 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8487 i.sib.base = NO_BASE_REGISTER;
8488 i.sib.scale = i.log2_scale_factor;
8489 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
2f2be86b 8490 i.types[op] = operand_type_and_not (i.types[op], anydisp);
a775efc8 8491 i.types[op].bitfield.disp32 = 1;
29b0f896 8492 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8493 i.rex |= REX_X;
29b0f896
AM
8494 }
8495 }
8496 /* RIP addressing for 64bit mode. */
e968fc9b 8497 else if (i.base_reg->reg_num == RegIP)
29b0f896 8498 {
63112cd6 8499 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896 8500 i.rm.regmem = NO_BASE_REGISTER;
40fb9820
L
8501 i.types[op].bitfield.disp8 = 0;
8502 i.types[op].bitfield.disp16 = 0;
a775efc8 8503 i.types[op].bitfield.disp32 = 1;
40fb9820 8504 i.types[op].bitfield.disp64 = 0;
71903a11 8505 i.flags[op] |= Operand_PCrel;
20f0a1fc
NC
8506 if (! i.disp_operands)
8507 fake_zero_displacement = 1;
29b0f896 8508 }
dc821c5f 8509 else if (i.base_reg->reg_type.bitfield.word)
29b0f896 8510 {
63112cd6 8511 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896
AM
8512 switch (i.base_reg->reg_num)
8513 {
8514 case 3: /* (%bx) */
8515 if (i.index_reg == 0)
8516 i.rm.regmem = 7;
8517 else /* (%bx,%si) -> 0, or (%bx,%di) -> 1 */
8518 i.rm.regmem = i.index_reg->reg_num - 6;
8519 break;
8520 case 5: /* (%bp) */
5e042380 8521 default_seg = reg_ss;
29b0f896
AM
8522 if (i.index_reg == 0)
8523 {
8524 i.rm.regmem = 6;
40fb9820 8525 if (operand_type_check (i.types[op], disp) == 0)
29b0f896
AM
8526 {
8527 /* fake (%bp) into 0(%bp) */
41eb8e88 8528 if (i.disp_encoding == disp_encoding_16bit)
1a02d6b0
L
8529 i.types[op].bitfield.disp16 = 1;
8530 else
8531 i.types[op].bitfield.disp8 = 1;
252b5132 8532 fake_zero_displacement = 1;
29b0f896
AM
8533 }
8534 }
8535 else /* (%bp,%si) -> 2, or (%bp,%di) -> 3 */
8536 i.rm.regmem = i.index_reg->reg_num - 6 + 2;
8537 break;
8538 default: /* (%si) -> 4 or (%di) -> 5 */
8539 i.rm.regmem = i.base_reg->reg_num - 6 + 4;
8540 }
41eb8e88
L
8541 if (!fake_zero_displacement
8542 && !i.disp_operands
8543 && i.disp_encoding)
8544 {
8545 fake_zero_displacement = 1;
8546 if (i.disp_encoding == disp_encoding_8bit)
8547 i.types[op].bitfield.disp8 = 1;
8548 else
8549 i.types[op].bitfield.disp16 = 1;
8550 }
29b0f896
AM
8551 i.rm.mode = mode_from_disp_size (i.types[op]);
8552 }
8553 else /* i.base_reg and 32/64 bit mode */
8554 {
a9aabc23 8555 if (operand_type_check (i.types[op], disp))
40fb9820 8556 {
73053c1f
JB
8557 i.types[op].bitfield.disp16 = 0;
8558 i.types[op].bitfield.disp64 = 0;
a775efc8 8559 i.types[op].bitfield.disp32 = 1;
40fb9820 8560 }
20f0a1fc 8561
63112cd6 8562 if (!i.tm.opcode_modifier.sib)
6c30d220 8563 i.rm.regmem = i.base_reg->reg_num;
29b0f896 8564 if ((i.base_reg->reg_flags & RegRex) != 0)
161a04f6 8565 i.rex |= REX_B;
29b0f896
AM
8566 i.sib.base = i.base_reg->reg_num;
8567 /* x86-64 ignores REX prefix bit here to avoid decoder
8568 complications. */
848930b2
JB
8569 if (!(i.base_reg->reg_flags & RegRex)
8570 && (i.base_reg->reg_num == EBP_REG_NUM
8571 || i.base_reg->reg_num == ESP_REG_NUM))
5e042380 8572 default_seg = reg_ss;
848930b2 8573 if (i.base_reg->reg_num == 5 && i.disp_operands == 0)
29b0f896 8574 {
848930b2 8575 fake_zero_displacement = 1;
1a02d6b0
L
8576 if (i.disp_encoding == disp_encoding_32bit)
8577 i.types[op].bitfield.disp32 = 1;
8578 else
8579 i.types[op].bitfield.disp8 = 1;
29b0f896
AM
8580 }
8581 i.sib.scale = i.log2_scale_factor;
8582 if (i.index_reg == 0)
8583 {
260cd341
LC
8584 /* Only check for VSIB. */
8585 gas_assert (i.tm.opcode_modifier.sib != VECSIB128
8586 && i.tm.opcode_modifier.sib != VECSIB256
8587 && i.tm.opcode_modifier.sib != VECSIB512);
8588
29b0f896
AM
8589 /* <disp>(%esp) becomes two byte modrm with no index
8590 register. We've already stored the code for esp
8591 in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
8592 Any base register besides %esp will not use the
8593 extra modrm byte. */
8594 i.sib.index = NO_INDEX_REGISTER;
29b0f896 8595 }
63112cd6 8596 else if (!i.tm.opcode_modifier.sib)
29b0f896 8597 {
e968fc9b 8598 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8599 i.sib.index = NO_INDEX_REGISTER;
8600 else
8601 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8602 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8603 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8604 i.rex |= REX_X;
29b0f896 8605 }
67a4f2b7
AO
8606
8607 if (i.disp_operands
8608 && (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
8609 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL))
8610 i.rm.mode = 0;
8611 else
a501d77e
L
8612 {
8613 if (!fake_zero_displacement
8614 && !i.disp_operands
8615 && i.disp_encoding)
8616 {
8617 fake_zero_displacement = 1;
8618 if (i.disp_encoding == disp_encoding_8bit)
8619 i.types[op].bitfield.disp8 = 1;
8620 else
8621 i.types[op].bitfield.disp32 = 1;
8622 }
8623 i.rm.mode = mode_from_disp_size (i.types[op]);
8624 }
29b0f896 8625 }
252b5132 8626
29b0f896
AM
8627 if (fake_zero_displacement)
8628 {
8629 /* Fakes a zero displacement assuming that i.types[op]
8630 holds the correct displacement size. */
8631 expressionS *exp;
8632
9c2799c2 8633 gas_assert (i.op[op].disps == 0);
29b0f896
AM
8634 exp = &disp_expressions[i.disp_operands++];
8635 i.op[op].disps = exp;
8636 exp->X_op = O_constant;
8637 exp->X_add_number = 0;
8638 exp->X_add_symbol = (symbolS *) 0;
8639 exp->X_op_symbol = (symbolS *) 0;
8640 }
c0f3af97
L
8641
8642 mem = op;
29b0f896 8643 }
c0f3af97
L
8644 else
8645 mem = ~0;
252b5132 8646
8c43a48b 8647 if (i.tm.opcode_modifier.vexsources == XOP2SOURCES)
5dd85c99
SP
8648 {
8649 if (operand_type_check (i.types[0], imm))
8650 i.vex.register_specifier = NULL;
8651 else
8652 {
8653 /* VEX.vvvv encodes one of the sources when the first
8654 operand is not an immediate. */
1ef99a7b 8655 if (i.tm.opcode_modifier.vexw == VEXW0)
5dd85c99
SP
8656 i.vex.register_specifier = i.op[0].regs;
8657 else
8658 i.vex.register_specifier = i.op[1].regs;
8659 }
8660
8661 /* Destination is a XMM register encoded in the ModRM.reg
8662 and VEX.R bit. */
8663 i.rm.reg = i.op[2].regs->reg_num;
8664 if ((i.op[2].regs->reg_flags & RegRex) != 0)
8665 i.rex |= REX_R;
8666
8667 /* ModRM.rm and VEX.B encodes the other source. */
8668 if (!i.mem_operands)
8669 {
8670 i.rm.mode = 3;
8671
1ef99a7b 8672 if (i.tm.opcode_modifier.vexw == VEXW0)
5dd85c99
SP
8673 i.rm.regmem = i.op[1].regs->reg_num;
8674 else
8675 i.rm.regmem = i.op[0].regs->reg_num;
8676
8677 if ((i.op[1].regs->reg_flags & RegRex) != 0)
8678 i.rex |= REX_B;
8679 }
8680 }
2426c15f 8681 else if (i.tm.opcode_modifier.vexvvvv == VEXLWP)
f88c9eb0
SP
8682 {
8683 i.vex.register_specifier = i.op[2].regs;
8684 if (!i.mem_operands)
8685 {
8686 i.rm.mode = 3;
8687 i.rm.regmem = i.op[1].regs->reg_num;
8688 if ((i.op[1].regs->reg_flags & RegRex) != 0)
8689 i.rex |= REX_B;
8690 }
8691 }
29b0f896
AM
8692 /* Fill in i.rm.reg or i.rm.regmem field with register operand
8693 (if any) based on i.tm.extension_opcode. Again, we must be
8694 careful to make sure that segment/control/debug/test/MMX
8695 registers are coded into the i.rm.reg field. */
f88c9eb0 8696 else if (i.reg_operands)
29b0f896 8697 {
99018f42 8698 unsigned int op;
7ab9ffdd
L
8699 unsigned int vex_reg = ~0;
8700
8701 for (op = 0; op < i.operands; op++)
921eafea
L
8702 if (i.types[op].bitfield.class == Reg
8703 || i.types[op].bitfield.class == RegBND
8704 || i.types[op].bitfield.class == RegMask
8705 || i.types[op].bitfield.class == SReg
8706 || i.types[op].bitfield.class == RegCR
8707 || i.types[op].bitfield.class == RegDR
8708 || i.types[op].bitfield.class == RegTR
8709 || i.types[op].bitfield.class == RegSIMD
8710 || i.types[op].bitfield.class == RegMMX)
8711 break;
c0209578 8712
7ab9ffdd
L
8713 if (vex_3_sources)
8714 op = dest;
2426c15f 8715 else if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
7ab9ffdd
L
8716 {
8717 /* For instructions with VexNDS, the register-only
8718 source operand is encoded in VEX prefix. */
8719 gas_assert (mem != (unsigned int) ~0);
c0f3af97 8720
a93e3234 8721 if (op > mem || i.tm.cpu_flags.bitfield.cpucmpccxadd)
c0f3af97 8722 {
7ab9ffdd
L
8723 vex_reg = op++;
8724 gas_assert (op < i.operands);
c0f3af97
L
8725 }
8726 else
c0f3af97 8727 {
f12dc422
L
8728 /* Check register-only source operand when two source
8729 operands are swapped. */
8730 if (!i.tm.operand_types[op].bitfield.baseindex
8731 && i.tm.operand_types[op + 1].bitfield.baseindex)
8732 {
8733 vex_reg = op;
8734 op += 2;
8735 gas_assert (mem == (vex_reg + 1)
8736 && op < i.operands);
8737 }
8738 else
8739 {
8740 vex_reg = op + 1;
8741 gas_assert (vex_reg < i.operands);
8742 }
c0f3af97 8743 }
7ab9ffdd 8744 }
2426c15f 8745 else if (i.tm.opcode_modifier.vexvvvv == VEXNDD)
7ab9ffdd 8746 {
f12dc422 8747 /* For instructions with VexNDD, the register destination
7ab9ffdd 8748 is encoded in VEX prefix. */
f12dc422
L
8749 if (i.mem_operands == 0)
8750 {
8751 /* There is no memory operand. */
8752 gas_assert ((op + 2) == i.operands);
8753 vex_reg = op + 1;
8754 }
8755 else
8d63c93e 8756 {
ed438a93
JB
8757 /* There are only 2 non-immediate operands. */
8758 gas_assert (op < i.imm_operands + 2
8759 && i.operands == i.imm_operands + 2);
8760 vex_reg = i.imm_operands + 1;
f12dc422 8761 }
7ab9ffdd
L
8762 }
8763 else
8764 gas_assert (op < i.operands);
99018f42 8765
7ab9ffdd
L
8766 if (vex_reg != (unsigned int) ~0)
8767 {
f12dc422 8768 i386_operand_type *type = &i.tm.operand_types[vex_reg];
7ab9ffdd 8769
bab6aec1 8770 if ((type->bitfield.class != Reg
dc821c5f 8771 || (!type->bitfield.dword && !type->bitfield.qword))
3528c362 8772 && type->bitfield.class != RegSIMD
c0f327b8 8773 && type->bitfield.class != RegMask)
7ab9ffdd 8774 abort ();
f88c9eb0 8775
7ab9ffdd
L
8776 i.vex.register_specifier = i.op[vex_reg].regs;
8777 }
8778
1b9f0c97
L
8779 /* Don't set OP operand twice. */
8780 if (vex_reg != op)
7ab9ffdd 8781 {
1b9f0c97
L
8782 /* If there is an extension opcode to put here, the
8783 register number must be put into the regmem field. */
8784 if (i.tm.extension_opcode != None)
8785 {
8786 i.rm.regmem = i.op[op].regs->reg_num;
a5aeccd9
JB
8787 set_rex_vrex (i.op[op].regs, REX_B,
8788 i.tm.opcode_modifier.sse2avx);
1b9f0c97
L
8789 }
8790 else
8791 {
8792 i.rm.reg = i.op[op].regs->reg_num;
a5aeccd9
JB
8793 set_rex_vrex (i.op[op].regs, REX_R,
8794 i.tm.opcode_modifier.sse2avx);
1b9f0c97 8795 }
7ab9ffdd 8796 }
252b5132 8797
29b0f896
AM
8798 /* Now, if no memory operand has set i.rm.mode = 0, 1, 2 we
8799 must set it to 3 to indicate this is a register operand
8800 in the regmem field. */
8801 if (!i.mem_operands)
8802 i.rm.mode = 3;
8803 }
252b5132 8804
29b0f896 8805 /* Fill in i.rm.reg field with extension opcode (if any). */
c1e679ec 8806 if (i.tm.extension_opcode != None)
29b0f896
AM
8807 i.rm.reg = i.tm.extension_opcode;
8808 }
8809 return default_seg;
8810}
252b5132 8811
48ef937e
JB
8812static INLINE void
8813frag_opcode_byte (unsigned char byte)
8814{
8815 if (now_seg != absolute_section)
8816 FRAG_APPEND_1_CHAR (byte);
8817 else
8818 ++abs_section_offset;
8819}
8820
376cd056
JB
8821static unsigned int
8822flip_code16 (unsigned int code16)
8823{
8824 gas_assert (i.tm.operands == 1);
8825
8826 return !(i.prefix[REX_PREFIX] & REX_W)
8827 && (code16 ? i.tm.operand_types[0].bitfield.disp32
376cd056
JB
8828 : i.tm.operand_types[0].bitfield.disp16)
8829 ? CODE16 : 0;
8830}
8831
29b0f896 8832static void
e3bb37b5 8833output_branch (void)
29b0f896
AM
8834{
8835 char *p;
f8a5c266 8836 int size;
29b0f896
AM
8837 int code16;
8838 int prefix;
8839 relax_substateT subtype;
8840 symbolS *sym;
8841 offsetT off;
8842
48ef937e
JB
8843 if (now_seg == absolute_section)
8844 {
8845 as_bad (_("relaxable branches not supported in absolute section"));
8846 return;
8847 }
8848
f8a5c266 8849 code16 = flag_code == CODE_16BIT ? CODE16 : 0;
1a42a9fe 8850 size = i.disp_encoding > disp_encoding_8bit ? BIG : SMALL;
29b0f896
AM
8851
8852 prefix = 0;
8853 if (i.prefix[DATA_PREFIX] != 0)
252b5132 8854 {
29b0f896
AM
8855 prefix = 1;
8856 i.prefixes -= 1;
376cd056 8857 code16 ^= flip_code16(code16);
252b5132 8858 }
29b0f896
AM
8859 /* Pentium4 branch hints. */
8860 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8861 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
2f66722d 8862 {
29b0f896
AM
8863 prefix++;
8864 i.prefixes--;
8865 }
8866 if (i.prefix[REX_PREFIX] != 0)
8867 {
8868 prefix++;
8869 i.prefixes--;
2f66722d
AM
8870 }
8871
7e8b059b
L
8872 /* BND prefixed jump. */
8873 if (i.prefix[BND_PREFIX] != 0)
8874 {
6cb0a70e
JB
8875 prefix++;
8876 i.prefixes--;
7e8b059b
L
8877 }
8878
f2810fe0
JB
8879 if (i.prefixes != 0)
8880 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
29b0f896
AM
8881
8882 /* It's always a symbol; End frag & setup for relax.
8883 Make sure there is enough room in this frag for the largest
8884 instruction we may generate in md_convert_frag. This is 2
8885 bytes for the opcode and room for the prefix and largest
8886 displacement. */
8887 frag_grow (prefix + 2 + 4);
8888 /* Prefix and 1 opcode byte go in fr_fix. */
8889 p = frag_more (prefix + 1);
8890 if (i.prefix[DATA_PREFIX] != 0)
8891 *p++ = DATA_PREFIX_OPCODE;
8892 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
8893 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
8894 *p++ = i.prefix[SEG_PREFIX];
6cb0a70e
JB
8895 if (i.prefix[BND_PREFIX] != 0)
8896 *p++ = BND_PREFIX_OPCODE;
29b0f896
AM
8897 if (i.prefix[REX_PREFIX] != 0)
8898 *p++ = i.prefix[REX_PREFIX];
8899 *p = i.tm.base_opcode;
8900
8901 if ((unsigned char) *p == JUMP_PC_RELATIVE)
f8a5c266 8902 subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, size);
40fb9820 8903 else if (cpu_arch_flags.bitfield.cpui386)
f8a5c266 8904 subtype = ENCODE_RELAX_STATE (COND_JUMP, size);
29b0f896 8905 else
f8a5c266 8906 subtype = ENCODE_RELAX_STATE (COND_JUMP86, size);
29b0f896 8907 subtype |= code16;
3e73aa7c 8908
29b0f896
AM
8909 sym = i.op[0].disps->X_add_symbol;
8910 off = i.op[0].disps->X_add_number;
3e73aa7c 8911
29b0f896
AM
8912 if (i.op[0].disps->X_op != O_constant
8913 && i.op[0].disps->X_op != O_symbol)
3e73aa7c 8914 {
29b0f896
AM
8915 /* Handle complex expressions. */
8916 sym = make_expr_symbol (i.op[0].disps);
8917 off = 0;
8918 }
3e73aa7c 8919
1ef3994a
JB
8920 frag_now->tc_frag_data.code64 = flag_code == CODE_64BIT;
8921
29b0f896
AM
8922 /* 1 possible extra opcode + 4 byte displacement go in var part.
8923 Pass reloc in fr_var. */
d258b828 8924 frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
29b0f896 8925}
3e73aa7c 8926
bd7ab16b
L
8927#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8928/* Return TRUE iff PLT32 relocation should be used for branching to
8929 symbol S. */
8930
5b7c81bd 8931static bool
bd7ab16b
L
8932need_plt32_p (symbolS *s)
8933{
8934 /* PLT32 relocation is ELF only. */
8935 if (!IS_ELF)
5b7c81bd 8936 return false;
bd7ab16b 8937
a5def729
RO
8938#ifdef TE_SOLARIS
8939 /* Don't emit PLT32 relocation on Solaris: neither native linker nor
8940 krtld support it. */
5b7c81bd 8941 return false;
a5def729
RO
8942#endif
8943
bd7ab16b
L
8944 /* Since there is no need to prepare for PLT branch on x86-64, we
8945 can generate R_X86_64_PLT32, instead of R_X86_64_PC32, which can
8946 be used as a marker for 32-bit PC-relative branches. */
8947 if (!object_64bit)
5b7c81bd 8948 return false;
bd7ab16b 8949
44365e88 8950 if (s == NULL)
5b7c81bd 8951 return false;
44365e88 8952
bd7ab16b
L
8953 /* Weak or undefined symbol need PLT32 relocation. */
8954 if (S_IS_WEAK (s) || !S_IS_DEFINED (s))
5b7c81bd 8955 return true;
bd7ab16b
L
8956
8957 /* Non-global symbol doesn't need PLT32 relocation. */
8958 if (! S_IS_EXTERNAL (s))
5b7c81bd 8959 return false;
bd7ab16b
L
8960
8961 /* Other global symbols need PLT32 relocation. NB: Symbol with
8962 non-default visibilities are treated as normal global symbol
8963 so that PLT32 relocation can be used as a marker for 32-bit
8964 PC-relative branches. It is useful for linker relaxation. */
5b7c81bd 8965 return true;
bd7ab16b
L
8966}
8967#endif
8968
29b0f896 8969static void
e3bb37b5 8970output_jump (void)
29b0f896
AM
8971{
8972 char *p;
8973 int size;
3e02c1cc 8974 fixS *fixP;
bd7ab16b 8975 bfd_reloc_code_real_type jump_reloc = i.reloc[0];
29b0f896 8976
0cfa3eb3 8977 if (i.tm.opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
8978 {
8979 /* This is a loop or jecxz type instruction. */
8980 size = 1;
8981 if (i.prefix[ADDR_PREFIX] != 0)
8982 {
48ef937e 8983 frag_opcode_byte (ADDR_PREFIX_OPCODE);
29b0f896
AM
8984 i.prefixes -= 1;
8985 }
8986 /* Pentium4 branch hints. */
8987 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8988 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
8989 {
48ef937e 8990 frag_opcode_byte (i.prefix[SEG_PREFIX]);
29b0f896 8991 i.prefixes--;
3e73aa7c
JH
8992 }
8993 }
29b0f896
AM
8994 else
8995 {
8996 int code16;
3e73aa7c 8997
29b0f896
AM
8998 code16 = 0;
8999 if (flag_code == CODE_16BIT)
9000 code16 = CODE16;
3e73aa7c 9001
29b0f896
AM
9002 if (i.prefix[DATA_PREFIX] != 0)
9003 {
48ef937e 9004 frag_opcode_byte (DATA_PREFIX_OPCODE);
29b0f896 9005 i.prefixes -= 1;
376cd056 9006 code16 ^= flip_code16(code16);
29b0f896 9007 }
252b5132 9008
29b0f896
AM
9009 size = 4;
9010 if (code16)
9011 size = 2;
9012 }
9fcc94b6 9013
6cb0a70e
JB
9014 /* BND prefixed jump. */
9015 if (i.prefix[BND_PREFIX] != 0)
29b0f896 9016 {
48ef937e 9017 frag_opcode_byte (i.prefix[BND_PREFIX]);
29b0f896
AM
9018 i.prefixes -= 1;
9019 }
252b5132 9020
6cb0a70e 9021 if (i.prefix[REX_PREFIX] != 0)
7e8b059b 9022 {
48ef937e 9023 frag_opcode_byte (i.prefix[REX_PREFIX]);
7e8b059b
L
9024 i.prefixes -= 1;
9025 }
9026
f2810fe0
JB
9027 if (i.prefixes != 0)
9028 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
e0890092 9029
48ef937e
JB
9030 if (now_seg == absolute_section)
9031 {
9a182d04 9032 abs_section_offset += i.opcode_length + size;
48ef937e
JB
9033 return;
9034 }
9035
9a182d04
JB
9036 p = frag_more (i.opcode_length + size);
9037 switch (i.opcode_length)
42164a71
L
9038 {
9039 case 2:
9040 *p++ = i.tm.base_opcode >> 8;
1a0670f3 9041 /* Fall through. */
42164a71
L
9042 case 1:
9043 *p++ = i.tm.base_opcode;
9044 break;
9045 default:
9046 abort ();
9047 }
e0890092 9048
bd7ab16b 9049#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1ef3994a
JB
9050 if (flag_code == CODE_64BIT && size == 4
9051 && jump_reloc == NO_RELOC && i.op[0].disps->X_add_number == 0
bd7ab16b
L
9052 && need_plt32_p (i.op[0].disps->X_add_symbol))
9053 jump_reloc = BFD_RELOC_X86_64_PLT32;
9054#endif
9055
9056 jump_reloc = reloc (size, 1, 1, jump_reloc);
9057
3e02c1cc 9058 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
bd7ab16b 9059 i.op[0].disps, 1, jump_reloc);
3e02c1cc 9060
eb19308f
JB
9061 /* All jumps handled here are signed, but don't unconditionally use a
9062 signed limit check for 32 and 16 bit jumps as we want to allow wrap
9063 around at 4G (outside of 64-bit mode) and 64k (except for XBEGIN)
9064 respectively. */
9065 switch (size)
9066 {
9067 case 1:
9068 fixP->fx_signed = 1;
9069 break;
9070
9071 case 2:
9072 if (i.tm.base_opcode == 0xc7f8)
9073 fixP->fx_signed = 1;
9074 break;
9075
9076 case 4:
9077 if (flag_code == CODE_64BIT)
9078 fixP->fx_signed = 1;
9079 break;
9080 }
29b0f896 9081}
e0890092 9082
29b0f896 9083static void
e3bb37b5 9084output_interseg_jump (void)
29b0f896
AM
9085{
9086 char *p;
9087 int size;
9088 int prefix;
9089 int code16;
252b5132 9090
29b0f896
AM
9091 code16 = 0;
9092 if (flag_code == CODE_16BIT)
9093 code16 = CODE16;
a217f122 9094
29b0f896
AM
9095 prefix = 0;
9096 if (i.prefix[DATA_PREFIX] != 0)
9097 {
9098 prefix = 1;
9099 i.prefixes -= 1;
9100 code16 ^= CODE16;
9101 }
6cb0a70e
JB
9102
9103 gas_assert (!i.prefix[REX_PREFIX]);
252b5132 9104
29b0f896
AM
9105 size = 4;
9106 if (code16)
9107 size = 2;
252b5132 9108
f2810fe0
JB
9109 if (i.prefixes != 0)
9110 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
252b5132 9111
48ef937e
JB
9112 if (now_seg == absolute_section)
9113 {
9114 abs_section_offset += prefix + 1 + 2 + size;
9115 return;
9116 }
9117
29b0f896
AM
9118 /* 1 opcode; 2 segment; offset */
9119 p = frag_more (prefix + 1 + 2 + size);
3e73aa7c 9120
29b0f896
AM
9121 if (i.prefix[DATA_PREFIX] != 0)
9122 *p++ = DATA_PREFIX_OPCODE;
252b5132 9123
29b0f896
AM
9124 if (i.prefix[REX_PREFIX] != 0)
9125 *p++ = i.prefix[REX_PREFIX];
252b5132 9126
29b0f896
AM
9127 *p++ = i.tm.base_opcode;
9128 if (i.op[1].imms->X_op == O_constant)
9129 {
9130 offsetT n = i.op[1].imms->X_add_number;
252b5132 9131
29b0f896
AM
9132 if (size == 2
9133 && !fits_in_unsigned_word (n)
9134 && !fits_in_signed_word (n))
9135 {
9136 as_bad (_("16-bit jump out of range"));
9137 return;
9138 }
9139 md_number_to_chars (p, n, size);
9140 }
9141 else
9142 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
d258b828 9143 i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
6d96a594
C
9144
9145 p += size;
9146 if (i.op[0].imms->X_op == O_constant)
9147 md_number_to_chars (p, (valueT) i.op[0].imms->X_add_number, 2);
9148 else
9149 fix_new_exp (frag_now, p - frag_now->fr_literal, 2,
9150 i.op[0].imms, 0, reloc (2, 0, 0, i.reloc[0]));
29b0f896 9151}
a217f122 9152
b4a3a7b4
L
9153#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9154void
9155x86_cleanup (void)
9156{
9157 char *p;
9158 asection *seg = now_seg;
9159 subsegT subseg = now_subseg;
9160 asection *sec;
9161 unsigned int alignment, align_size_1;
9162 unsigned int isa_1_descsz, feature_2_descsz, descsz;
9163 unsigned int isa_1_descsz_raw, feature_2_descsz_raw;
9164 unsigned int padding;
9165
1273b2f8 9166 if (!IS_ELF || !x86_used_note)
b4a3a7b4
L
9167 return;
9168
b4a3a7b4
L
9169 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X86;
9170
9171 /* The .note.gnu.property section layout:
9172
9173 Field Length Contents
9174 ---- ---- ----
9175 n_namsz 4 4
9176 n_descsz 4 The note descriptor size
9177 n_type 4 NT_GNU_PROPERTY_TYPE_0
9178 n_name 4 "GNU"
9179 n_desc n_descsz The program property array
9180 .... .... ....
9181 */
9182
9183 /* Create the .note.gnu.property section. */
9184 sec = subseg_new (NOTE_GNU_PROPERTY_SECTION_NAME, 0);
fd361982 9185 bfd_set_section_flags (sec,
b4a3a7b4
L
9186 (SEC_ALLOC
9187 | SEC_LOAD
9188 | SEC_DATA
9189 | SEC_HAS_CONTENTS
9190 | SEC_READONLY));
9191
9192 if (get_elf_backend_data (stdoutput)->s->elfclass == ELFCLASS64)
9193 {
9194 align_size_1 = 7;
9195 alignment = 3;
9196 }
9197 else
9198 {
9199 align_size_1 = 3;
9200 alignment = 2;
9201 }
9202
fd361982 9203 bfd_set_section_alignment (sec, alignment);
b4a3a7b4
L
9204 elf_section_type (sec) = SHT_NOTE;
9205
1273b2f8
L
9206 /* GNU_PROPERTY_X86_ISA_1_USED: 4-byte type + 4-byte data size
9207 + 4-byte data */
9208 isa_1_descsz_raw = 4 + 4 + 4;
9209 /* Align GNU_PROPERTY_X86_ISA_1_USED. */
9210 isa_1_descsz = (isa_1_descsz_raw + align_size_1) & ~align_size_1;
b4a3a7b4
L
9211
9212 feature_2_descsz_raw = isa_1_descsz;
9213 /* GNU_PROPERTY_X86_FEATURE_2_USED: 4-byte type + 4-byte data size
1273b2f8 9214 + 4-byte data */
b4a3a7b4
L
9215 feature_2_descsz_raw += 4 + 4 + 4;
9216 /* Align GNU_PROPERTY_X86_FEATURE_2_USED. */
9217 feature_2_descsz = ((feature_2_descsz_raw + align_size_1)
9218 & ~align_size_1);
9219
9220 descsz = feature_2_descsz;
9221 /* Section size: n_namsz + n_descsz + n_type + n_name + n_descsz. */
9222 p = frag_more (4 + 4 + 4 + 4 + descsz);
9223
9224 /* Write n_namsz. */
9225 md_number_to_chars (p, (valueT) 4, 4);
9226
9227 /* Write n_descsz. */
9228 md_number_to_chars (p + 4, (valueT) descsz, 4);
9229
9230 /* Write n_type. */
9231 md_number_to_chars (p + 4 * 2, (valueT) NT_GNU_PROPERTY_TYPE_0, 4);
9232
9233 /* Write n_name. */
9234 memcpy (p + 4 * 3, "GNU", 4);
9235
1273b2f8
L
9236 /* Write 4-byte type. */
9237 md_number_to_chars (p + 4 * 4,
9238 (valueT) GNU_PROPERTY_X86_ISA_1_USED, 4);
b4a3a7b4 9239
1273b2f8
L
9240 /* Write 4-byte data size. */
9241 md_number_to_chars (p + 4 * 5, (valueT) 4, 4);
b4a3a7b4 9242
1273b2f8
L
9243 /* Write 4-byte data. */
9244 md_number_to_chars (p + 4 * 6, (valueT) x86_isa_1_used, 4);
b4a3a7b4 9245
1273b2f8
L
9246 /* Zero out paddings. */
9247 padding = isa_1_descsz - isa_1_descsz_raw;
9248 if (padding)
9249 memset (p + 4 * 7, 0, padding);
b4a3a7b4
L
9250
9251 /* Write 4-byte type. */
9252 md_number_to_chars (p + isa_1_descsz + 4 * 4,
9253 (valueT) GNU_PROPERTY_X86_FEATURE_2_USED, 4);
9254
9255 /* Write 4-byte data size. */
9256 md_number_to_chars (p + isa_1_descsz + 4 * 5, (valueT) 4, 4);
9257
9258 /* Write 4-byte data. */
9259 md_number_to_chars (p + isa_1_descsz + 4 * 6,
9260 (valueT) x86_feature_2_used, 4);
9261
9262 /* Zero out paddings. */
9263 padding = feature_2_descsz - feature_2_descsz_raw;
9264 if (padding)
9265 memset (p + isa_1_descsz + 4 * 7, 0, padding);
9266
9267 /* We probably can't restore the current segment, for there likely
9268 isn't one yet... */
9269 if (seg && subseg)
9270 subseg_set (seg, subseg);
9271}
b52c4ee4
IB
9272
9273bool
9274x86_support_sframe_p (void)
9275{
9276 /* At this time, SFrame unwind is supported for AMD64 ABI only. */
9277 return (x86_elf_abi == X86_64_ABI);
9278}
9279
9280bool
9281x86_sframe_ra_tracking_p (void)
9282{
9283 /* In AMD64, return address is always stored on the stack at a fixed offset
9284 from the CFA (provided via x86_sframe_cfa_ra_offset ()).
9285 Do not track explicitly via an SFrame Frame Row Entry. */
9286 return false;
9287}
9288
9289offsetT
9290x86_sframe_cfa_ra_offset (void)
9291{
9292 gas_assert (x86_elf_abi == X86_64_ABI);
9293 return (offsetT) -8;
9294}
9295
9296unsigned char
9297x86_sframe_get_abi_arch (void)
9298{
9299 unsigned char sframe_abi_arch = 0;
9300
9301 if (x86_support_sframe_p ())
9302 {
9303 gas_assert (!target_big_endian);
9304 sframe_abi_arch = SFRAME_ABI_AMD64_ENDIAN_LITTLE;
9305 }
9306
9307 return sframe_abi_arch;
9308}
9309
b4a3a7b4
L
9310#endif
9311
9c33702b
JB
9312static unsigned int
9313encoding_length (const fragS *start_frag, offsetT start_off,
9314 const char *frag_now_ptr)
9315{
9316 unsigned int len = 0;
9317
9318 if (start_frag != frag_now)
9319 {
9320 const fragS *fr = start_frag;
9321
9322 do {
9323 len += fr->fr_fix;
9324 fr = fr->fr_next;
9325 } while (fr && fr != frag_now);
9326 }
9327
9328 return len - start_off + (frag_now_ptr - frag_now->fr_literal);
9329}
9330
e379e5f3 9331/* Return 1 for test, and, cmp, add, sub, inc and dec which may
79d72f45
HL
9332 be macro-fused with conditional jumps.
9333 NB: If TEST/AND/CMP/ADD/SUB/INC/DEC is of RIP relative address,
9334 or is one of the following format:
9335
9336 cmp m, imm
9337 add m, imm
9338 sub m, imm
9339 test m, imm
9340 and m, imm
9341 inc m
9342 dec m
9343
9344 it is unfusible. */
e379e5f3
L
9345
9346static int
79d72f45 9347maybe_fused_with_jcc_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
9348{
9349 /* No RIP address. */
9350 if (i.base_reg && i.base_reg->reg_num == RegIP)
9351 return 0;
9352
389d00a5
JB
9353 /* No opcodes outside of base encoding space. */
9354 if (i.tm.opcode_modifier.opcodespace != SPACE_BASE)
e379e5f3
L
9355 return 0;
9356
79d72f45
HL
9357 /* add, sub without add/sub m, imm. */
9358 if (i.tm.base_opcode <= 5
e379e5f3
L
9359 || (i.tm.base_opcode >= 0x28 && i.tm.base_opcode <= 0x2d)
9360 || ((i.tm.base_opcode | 3) == 0x83
79d72f45 9361 && (i.tm.extension_opcode == 0x5
e379e5f3 9362 || i.tm.extension_opcode == 0x0)))
79d72f45
HL
9363 {
9364 *mf_cmp_p = mf_cmp_alu_cmp;
9365 return !(i.mem_operands && i.imm_operands);
9366 }
e379e5f3 9367
79d72f45
HL
9368 /* and without and m, imm. */
9369 if ((i.tm.base_opcode >= 0x20 && i.tm.base_opcode <= 0x25)
9370 || ((i.tm.base_opcode | 3) == 0x83
9371 && i.tm.extension_opcode == 0x4))
9372 {
9373 *mf_cmp_p = mf_cmp_test_and;
9374 return !(i.mem_operands && i.imm_operands);
9375 }
9376
9377 /* test without test m imm. */
e379e5f3
L
9378 if ((i.tm.base_opcode | 1) == 0x85
9379 || (i.tm.base_opcode | 1) == 0xa9
9380 || ((i.tm.base_opcode | 1) == 0xf7
79d72f45
HL
9381 && i.tm.extension_opcode == 0))
9382 {
9383 *mf_cmp_p = mf_cmp_test_and;
9384 return !(i.mem_operands && i.imm_operands);
9385 }
9386
9387 /* cmp without cmp m, imm. */
9388 if ((i.tm.base_opcode >= 0x38 && i.tm.base_opcode <= 0x3d)
e379e5f3
L
9389 || ((i.tm.base_opcode | 3) == 0x83
9390 && (i.tm.extension_opcode == 0x7)))
79d72f45
HL
9391 {
9392 *mf_cmp_p = mf_cmp_alu_cmp;
9393 return !(i.mem_operands && i.imm_operands);
9394 }
e379e5f3 9395
79d72f45 9396 /* inc, dec without inc/dec m. */
e379e5f3
L
9397 if ((i.tm.cpu_flags.bitfield.cpuno64
9398 && (i.tm.base_opcode | 0xf) == 0x4f)
9399 || ((i.tm.base_opcode | 1) == 0xff
9400 && i.tm.extension_opcode <= 0x1))
79d72f45
HL
9401 {
9402 *mf_cmp_p = mf_cmp_incdec;
9403 return !i.mem_operands;
9404 }
e379e5f3
L
9405
9406 return 0;
9407}
9408
9409/* Return 1 if a FUSED_JCC_PADDING frag should be generated. */
9410
9411static int
79d72f45 9412add_fused_jcc_padding_frag_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
9413{
9414 /* NB: Don't work with COND_JUMP86 without i386. */
9415 if (!align_branch_power
9416 || now_seg == absolute_section
9417 || !cpu_arch_flags.bitfield.cpui386
9418 || !(align_branch & align_branch_fused_bit))
9419 return 0;
9420
79d72f45 9421 if (maybe_fused_with_jcc_p (mf_cmp_p))
e379e5f3
L
9422 {
9423 if (last_insn.kind == last_insn_other
9424 || last_insn.seg != now_seg)
9425 return 1;
9426 if (flag_debug)
9427 as_warn_where (last_insn.file, last_insn.line,
9428 _("`%s` skips -malign-branch-boundary on `%s`"),
9429 last_insn.name, i.tm.name);
9430 }
9431
9432 return 0;
9433}
9434
9435/* Return 1 if a BRANCH_PREFIX frag should be generated. */
9436
9437static int
9438add_branch_prefix_frag_p (void)
9439{
9440 /* NB: Don't work with COND_JUMP86 without i386. Don't add prefix
9441 to PadLock instructions since they include prefixes in opcode. */
9442 if (!align_branch_power
9443 || !align_branch_prefix_size
9444 || now_seg == absolute_section
9445 || i.tm.cpu_flags.bitfield.cpupadlock
9446 || !cpu_arch_flags.bitfield.cpui386)
9447 return 0;
9448
9449 /* Don't add prefix if it is a prefix or there is no operand in case
9450 that segment prefix is special. */
9451 if (!i.operands || i.tm.opcode_modifier.isprefix)
9452 return 0;
9453
9454 if (last_insn.kind == last_insn_other
9455 || last_insn.seg != now_seg)
9456 return 1;
9457
9458 if (flag_debug)
9459 as_warn_where (last_insn.file, last_insn.line,
9460 _("`%s` skips -malign-branch-boundary on `%s`"),
9461 last_insn.name, i.tm.name);
9462
9463 return 0;
9464}
9465
9466/* Return 1 if a BRANCH_PADDING frag should be generated. */
9467
9468static int
79d72f45
HL
9469add_branch_padding_frag_p (enum align_branch_kind *branch_p,
9470 enum mf_jcc_kind *mf_jcc_p)
e379e5f3
L
9471{
9472 int add_padding;
9473
9474 /* NB: Don't work with COND_JUMP86 without i386. */
9475 if (!align_branch_power
9476 || now_seg == absolute_section
389d00a5
JB
9477 || !cpu_arch_flags.bitfield.cpui386
9478 || i.tm.opcode_modifier.opcodespace != SPACE_BASE)
e379e5f3
L
9479 return 0;
9480
9481 add_padding = 0;
9482
9483 /* Check for jcc and direct jmp. */
9484 if (i.tm.opcode_modifier.jump == JUMP)
9485 {
9486 if (i.tm.base_opcode == JUMP_PC_RELATIVE)
9487 {
9488 *branch_p = align_branch_jmp;
9489 add_padding = align_branch & align_branch_jmp_bit;
9490 }
9491 else
9492 {
79d72f45
HL
9493 /* Because J<cc> and JN<cc> share same group in macro-fusible table,
9494 igore the lowest bit. */
9495 *mf_jcc_p = (i.tm.base_opcode & 0x0e) >> 1;
e379e5f3
L
9496 *branch_p = align_branch_jcc;
9497 if ((align_branch & align_branch_jcc_bit))
9498 add_padding = 1;
9499 }
9500 }
e379e5f3
L
9501 else if ((i.tm.base_opcode | 1) == 0xc3)
9502 {
9503 /* Near ret. */
9504 *branch_p = align_branch_ret;
9505 if ((align_branch & align_branch_ret_bit))
9506 add_padding = 1;
9507 }
9508 else
9509 {
9510 /* Check for indirect jmp, direct and indirect calls. */
9511 if (i.tm.base_opcode == 0xe8)
9512 {
9513 /* Direct call. */
9514 *branch_p = align_branch_call;
9515 if ((align_branch & align_branch_call_bit))
9516 add_padding = 1;
9517 }
9518 else if (i.tm.base_opcode == 0xff
9519 && (i.tm.extension_opcode == 2
9520 || i.tm.extension_opcode == 4))
9521 {
9522 /* Indirect call and jmp. */
9523 *branch_p = align_branch_indirect;
9524 if ((align_branch & align_branch_indirect_bit))
9525 add_padding = 1;
9526 }
9527
9528 if (add_padding
9529 && i.disp_operands
9530 && tls_get_addr
9531 && (i.op[0].disps->X_op == O_symbol
9532 || (i.op[0].disps->X_op == O_subtract
9533 && i.op[0].disps->X_op_symbol == GOT_symbol)))
9534 {
9535 symbolS *s = i.op[0].disps->X_add_symbol;
9536 /* No padding to call to global or undefined tls_get_addr. */
9537 if ((S_IS_EXTERNAL (s) || !S_IS_DEFINED (s))
9538 && strcmp (S_GET_NAME (s), tls_get_addr) == 0)
9539 return 0;
9540 }
9541 }
9542
9543 if (add_padding
9544 && last_insn.kind != last_insn_other
9545 && last_insn.seg == now_seg)
9546 {
9547 if (flag_debug)
9548 as_warn_where (last_insn.file, last_insn.line,
9549 _("`%s` skips -malign-branch-boundary on `%s`"),
9550 last_insn.name, i.tm.name);
9551 return 0;
9552 }
9553
9554 return add_padding;
9555}
9556
29b0f896 9557static void
e3bb37b5 9558output_insn (void)
29b0f896 9559{
2bbd9c25
JJ
9560 fragS *insn_start_frag;
9561 offsetT insn_start_off;
e379e5f3
L
9562 fragS *fragP = NULL;
9563 enum align_branch_kind branch = align_branch_none;
79d72f45
HL
9564 /* The initializer is arbitrary just to avoid uninitialized error.
9565 it's actually either assigned in add_branch_padding_frag_p
9566 or never be used. */
9567 enum mf_jcc_kind mf_jcc = mf_jcc_jo;
2bbd9c25 9568
b4a3a7b4 9569#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
48ef937e 9570 if (IS_ELF && x86_used_note && now_seg != absolute_section)
b4a3a7b4 9571 {
32930e4e
L
9572 if ((i.xstate & xstate_tmm) == xstate_tmm
9573 || i.tm.cpu_flags.bitfield.cpuamx_tile)
9574 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_TMM;
9575
b4a3a7b4
L
9576 if (i.tm.cpu_flags.bitfield.cpu8087
9577 || i.tm.cpu_flags.bitfield.cpu287
9578 || i.tm.cpu_flags.bitfield.cpu387
9579 || i.tm.cpu_flags.bitfield.cpu687
9580 || i.tm.cpu_flags.bitfield.cpufisttp)
9581 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X87;
014d61ea 9582
921eafea 9583 if ((i.xstate & xstate_mmx)
389d00a5
JB
9584 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
9585 && !is_any_vex_encoding (&i.tm)
9586 && (i.tm.base_opcode == 0x77 /* emms */
9587 || i.tm.base_opcode == 0x0e /* femms */)))
b4a3a7b4 9588 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MMX;
014d61ea 9589
32930e4e
L
9590 if (i.index_reg)
9591 {
9592 if (i.index_reg->reg_type.bitfield.zmmword)
9593 i.xstate |= xstate_zmm;
9594 else if (i.index_reg->reg_type.bitfield.ymmword)
9595 i.xstate |= xstate_ymm;
9596 else if (i.index_reg->reg_type.bitfield.xmmword)
9597 i.xstate |= xstate_xmm;
9598 }
014d61ea
JB
9599
9600 /* vzeroall / vzeroupper */
9601 if (i.tm.base_opcode == 0x77 && i.tm.cpu_flags.bitfield.cpuavx)
9602 i.xstate |= xstate_ymm;
9603
c4694f17 9604 if ((i.xstate & xstate_xmm)
389d00a5
JB
9605 /* ldmxcsr / stmxcsr / vldmxcsr / vstmxcsr */
9606 || (i.tm.base_opcode == 0xae
9607 && (i.tm.cpu_flags.bitfield.cpusse
9608 || i.tm.cpu_flags.bitfield.cpuavx))
c4694f17
TG
9609 || i.tm.cpu_flags.bitfield.cpuwidekl
9610 || i.tm.cpu_flags.bitfield.cpukl)
b4a3a7b4 9611 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XMM;
014d61ea 9612
921eafea 9613 if ((i.xstate & xstate_ymm) == xstate_ymm)
b4a3a7b4 9614 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_YMM;
921eafea 9615 if ((i.xstate & xstate_zmm) == xstate_zmm)
b4a3a7b4 9616 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_ZMM;
6225c532 9617 if (i.mask.reg || (i.xstate & xstate_mask) == xstate_mask)
32930e4e 9618 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MASK;
b4a3a7b4
L
9619 if (i.tm.cpu_flags.bitfield.cpufxsr)
9620 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_FXSR;
9621 if (i.tm.cpu_flags.bitfield.cpuxsave)
9622 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVE;
9623 if (i.tm.cpu_flags.bitfield.cpuxsaveopt)
9624 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT;
9625 if (i.tm.cpu_flags.bitfield.cpuxsavec)
9626 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEC;
b0ab0693
L
9627
9628 if (x86_feature_2_used
9629 || i.tm.cpu_flags.bitfield.cpucmov
9630 || i.tm.cpu_flags.bitfield.cpusyscall
389d00a5
JB
9631 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
9632 && i.tm.base_opcode == 0xc7
70e95837 9633 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
b0ab0693
L
9634 && i.tm.extension_opcode == 1) /* cmpxchg8b */)
9635 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_BASELINE;
9636 if (i.tm.cpu_flags.bitfield.cpusse3
9637 || i.tm.cpu_flags.bitfield.cpussse3
9638 || i.tm.cpu_flags.bitfield.cpusse4_1
9639 || i.tm.cpu_flags.bitfield.cpusse4_2
9640 || i.tm.cpu_flags.bitfield.cpucx16
9641 || i.tm.cpu_flags.bitfield.cpupopcnt
9642 /* LAHF-SAHF insns in 64-bit mode. */
9643 || (flag_code == CODE_64BIT
35648716
JB
9644 && (i.tm.base_opcode | 1) == 0x9f
9645 && i.tm.opcode_modifier.opcodespace == SPACE_BASE))
b0ab0693
L
9646 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V2;
9647 if (i.tm.cpu_flags.bitfield.cpuavx
9648 || i.tm.cpu_flags.bitfield.cpuavx2
a9860005
JB
9649 /* Any VEX encoded insns execpt for AVX512F, AVX512BW, AVX512DQ,
9650 XOP, FMA4, LPW, TBM, and AMX. */
b0ab0693
L
9651 || (i.tm.opcode_modifier.vex
9652 && !i.tm.cpu_flags.bitfield.cpuavx512f
9653 && !i.tm.cpu_flags.bitfield.cpuavx512bw
9654 && !i.tm.cpu_flags.bitfield.cpuavx512dq
a9860005
JB
9655 && !i.tm.cpu_flags.bitfield.cpuxop
9656 && !i.tm.cpu_flags.bitfield.cpufma4
b0ab0693
L
9657 && !i.tm.cpu_flags.bitfield.cpulwp
9658 && !i.tm.cpu_flags.bitfield.cputbm
9659 && !(x86_feature_2_used & GNU_PROPERTY_X86_FEATURE_2_TMM))
9660 || i.tm.cpu_flags.bitfield.cpuf16c
9661 || i.tm.cpu_flags.bitfield.cpufma
9662 || i.tm.cpu_flags.bitfield.cpulzcnt
9663 || i.tm.cpu_flags.bitfield.cpumovbe
9664 || i.tm.cpu_flags.bitfield.cpuxsaves
9665 || (x86_feature_2_used
9666 & (GNU_PROPERTY_X86_FEATURE_2_XSAVE
9667 | GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT
9668 | GNU_PROPERTY_X86_FEATURE_2_XSAVEC)) != 0)
9669 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V3;
9670 if (i.tm.cpu_flags.bitfield.cpuavx512f
9671 || i.tm.cpu_flags.bitfield.cpuavx512bw
9672 || i.tm.cpu_flags.bitfield.cpuavx512dq
9673 || i.tm.cpu_flags.bitfield.cpuavx512vl
a9860005
JB
9674 /* Any EVEX encoded insns except for AVX512ER, AVX512PF,
9675 AVX512-4FMAPS, and AVX512-4VNNIW. */
b0ab0693
L
9676 || (i.tm.opcode_modifier.evex
9677 && !i.tm.cpu_flags.bitfield.cpuavx512er
9678 && !i.tm.cpu_flags.bitfield.cpuavx512pf
a9860005 9679 && !i.tm.cpu_flags.bitfield.cpuavx512_4fmaps
b0ab0693
L
9680 && !i.tm.cpu_flags.bitfield.cpuavx512_4vnniw))
9681 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V4;
b4a3a7b4
L
9682 }
9683#endif
9684
29b0f896
AM
9685 /* Tie dwarf2 debug info to the address at the start of the insn.
9686 We can't do this after the insn has been output as the current
9687 frag may have been closed off. eg. by frag_var. */
9688 dwarf2_emit_insn (0);
9689
2bbd9c25
JJ
9690 insn_start_frag = frag_now;
9691 insn_start_off = frag_now_fix ();
9692
79d72f45 9693 if (add_branch_padding_frag_p (&branch, &mf_jcc))
e379e5f3
L
9694 {
9695 char *p;
9696 /* Branch can be 8 bytes. Leave some room for prefixes. */
9697 unsigned int max_branch_padding_size = 14;
9698
9699 /* Align section to boundary. */
9700 record_alignment (now_seg, align_branch_power);
9701
9702 /* Make room for padding. */
9703 frag_grow (max_branch_padding_size);
9704
9705 /* Start of the padding. */
9706 p = frag_more (0);
9707
9708 fragP = frag_now;
9709
9710 frag_var (rs_machine_dependent, max_branch_padding_size, 0,
9711 ENCODE_RELAX_STATE (BRANCH_PADDING, 0),
9712 NULL, 0, p);
9713
79d72f45 9714 fragP->tc_frag_data.mf_type = mf_jcc;
e379e5f3
L
9715 fragP->tc_frag_data.branch_type = branch;
9716 fragP->tc_frag_data.max_bytes = max_branch_padding_size;
9717 }
9718
d59a54c2
JB
9719 if (!cpu_arch_flags.bitfield.cpui386 && (flag_code != CODE_16BIT)
9720 && !pre_386_16bit_warned)
9721 {
9722 as_warn (_("use .code16 to ensure correct addressing mode"));
9723 pre_386_16bit_warned = true;
9724 }
9725
29b0f896 9726 /* Output jumps. */
0cfa3eb3 9727 if (i.tm.opcode_modifier.jump == JUMP)
29b0f896 9728 output_branch ();
0cfa3eb3
JB
9729 else if (i.tm.opcode_modifier.jump == JUMP_BYTE
9730 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896 9731 output_jump ();
0cfa3eb3 9732 else if (i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT)
29b0f896
AM
9733 output_interseg_jump ();
9734 else
9735 {
9736 /* Output normal instructions here. */
9737 char *p;
9738 unsigned char *q;
47465058 9739 unsigned int j;
79d72f45 9740 enum mf_cmp_kind mf_cmp;
4dffcebc 9741
e4e00185 9742 if (avoid_fence
389d00a5
JB
9743 && (i.tm.base_opcode == 0xaee8
9744 || i.tm.base_opcode == 0xaef0
9745 || i.tm.base_opcode == 0xaef8))
48ef937e
JB
9746 {
9747 /* Encode lfence, mfence, and sfence as
9748 f0 83 04 24 00 lock addl $0x0, (%{re}sp). */
47f4115a
JB
9749 if (flag_code == CODE_16BIT)
9750 as_bad (_("Cannot convert `%s' in 16-bit mode"), i.tm.name);
9751 else if (omit_lock_prefix)
9752 as_bad (_("Cannot convert `%s' with `-momit-lock-prefix=yes' in effect"),
9753 i.tm.name);
9754 else if (now_seg != absolute_section)
48ef937e
JB
9755 {
9756 offsetT val = 0x240483f0ULL;
9757
9758 p = frag_more (5);
9759 md_number_to_chars (p, val, 5);
9760 }
9761 else
9762 abs_section_offset += 5;
9763 return;
9764 }
e4e00185 9765
d022bddd
IT
9766 /* Some processors fail on LOCK prefix. This options makes
9767 assembler ignore LOCK prefix and serves as a workaround. */
9768 if (omit_lock_prefix)
9769 {
35648716
JB
9770 if (i.tm.base_opcode == LOCK_PREFIX_OPCODE
9771 && i.tm.opcode_modifier.isprefix)
d022bddd
IT
9772 return;
9773 i.prefix[LOCK_PREFIX] = 0;
9774 }
9775
e379e5f3
L
9776 if (branch)
9777 /* Skip if this is a branch. */
9778 ;
79d72f45 9779 else if (add_fused_jcc_padding_frag_p (&mf_cmp))
e379e5f3
L
9780 {
9781 /* Make room for padding. */
9782 frag_grow (MAX_FUSED_JCC_PADDING_SIZE);
9783 p = frag_more (0);
9784
9785 fragP = frag_now;
9786
9787 frag_var (rs_machine_dependent, MAX_FUSED_JCC_PADDING_SIZE, 0,
9788 ENCODE_RELAX_STATE (FUSED_JCC_PADDING, 0),
9789 NULL, 0, p);
9790
79d72f45 9791 fragP->tc_frag_data.mf_type = mf_cmp;
e379e5f3
L
9792 fragP->tc_frag_data.branch_type = align_branch_fused;
9793 fragP->tc_frag_data.max_bytes = MAX_FUSED_JCC_PADDING_SIZE;
9794 }
9795 else if (add_branch_prefix_frag_p ())
9796 {
9797 unsigned int max_prefix_size = align_branch_prefix_size;
9798
9799 /* Make room for padding. */
9800 frag_grow (max_prefix_size);
9801 p = frag_more (0);
9802
9803 fragP = frag_now;
9804
9805 frag_var (rs_machine_dependent, max_prefix_size, 0,
9806 ENCODE_RELAX_STATE (BRANCH_PREFIX, 0),
9807 NULL, 0, p);
9808
9809 fragP->tc_frag_data.max_bytes = max_prefix_size;
9810 }
9811
43234a1e
L
9812 /* Since the VEX/EVEX prefix contains the implicit prefix, we
9813 don't need the explicit prefix. */
cf665fee 9814 if (!is_any_vex_encoding (&i.tm))
bc4bd9ab 9815 {
7b47a312 9816 switch (i.tm.opcode_modifier.opcodeprefix)
bc4bd9ab 9817 {
7b47a312
L
9818 case PREFIX_0X66:
9819 add_prefix (0x66);
9820 break;
9821 case PREFIX_0XF2:
9822 add_prefix (0xf2);
9823 break;
9824 case PREFIX_0XF3:
8b65b895
L
9825 if (!i.tm.cpu_flags.bitfield.cpupadlock
9826 || (i.prefix[REP_PREFIX] != 0xf3))
9827 add_prefix (0xf3);
c0f3af97 9828 break;
7b47a312 9829 case PREFIX_NONE:
9a182d04 9830 switch (i.opcode_length)
c0f3af97 9831 {
7b47a312 9832 case 2:
7b47a312 9833 break;
9a182d04 9834 case 1:
7b47a312 9835 /* Check for pseudo prefixes. */
9a182d04
JB
9836 if (!i.tm.opcode_modifier.isprefix || i.tm.base_opcode)
9837 break;
7b47a312
L
9838 as_bad_where (insn_start_frag->fr_file,
9839 insn_start_frag->fr_line,
9840 _("pseudo prefix without instruction"));
9841 return;
9842 default:
9843 abort ();
4dffcebc 9844 }
c0f3af97 9845 break;
c0f3af97
L
9846 default:
9847 abort ();
bc4bd9ab 9848 }
c0f3af97 9849
6d19a37a 9850#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
cf61b747
L
9851 /* For x32, add a dummy REX_OPCODE prefix for mov/add with
9852 R_X86_64_GOTTPOFF relocation so that linker can safely
14470f07
L
9853 perform IE->LE optimization. A dummy REX_OPCODE prefix
9854 is also needed for lea with R_X86_64_GOTPC32_TLSDESC
9855 relocation for GDesc -> IE/LE optimization. */
cf61b747
L
9856 if (x86_elf_abi == X86_64_X32_ABI
9857 && i.operands == 2
14470f07
L
9858 && (i.reloc[0] == BFD_RELOC_X86_64_GOTTPOFF
9859 || i.reloc[0] == BFD_RELOC_X86_64_GOTPC32_TLSDESC)
cf61b747
L
9860 && i.prefix[REX_PREFIX] == 0)
9861 add_prefix (REX_OPCODE);
6d19a37a 9862#endif
cf61b747 9863
c0f3af97
L
9864 /* The prefix bytes. */
9865 for (j = ARRAY_SIZE (i.prefix), q = i.prefix; j > 0; j--, q++)
9866 if (*q)
48ef937e 9867 frag_opcode_byte (*q);
0f10071e 9868 }
ae5c1c7b 9869 else
c0f3af97
L
9870 {
9871 for (j = 0, q = i.prefix; j < ARRAY_SIZE (i.prefix); j++, q++)
9872 if (*q)
9873 switch (j)
9874 {
c0f3af97
L
9875 case SEG_PREFIX:
9876 case ADDR_PREFIX:
48ef937e 9877 frag_opcode_byte (*q);
c0f3af97
L
9878 break;
9879 default:
9880 /* There should be no other prefixes for instructions
9881 with VEX prefix. */
9882 abort ();
9883 }
9884
43234a1e
L
9885 /* For EVEX instructions i.vrex should become 0 after
9886 build_evex_prefix. For VEX instructions upper 16 registers
9887 aren't available, so VREX should be 0. */
9888 if (i.vrex)
9889 abort ();
c0f3af97 9890 /* Now the VEX prefix. */
48ef937e
JB
9891 if (now_seg != absolute_section)
9892 {
9893 p = frag_more (i.vex.length);
9894 for (j = 0; j < i.vex.length; j++)
9895 p[j] = i.vex.bytes[j];
9896 }
9897 else
9898 abs_section_offset += i.vex.length;
c0f3af97 9899 }
252b5132 9900
29b0f896 9901 /* Now the opcode; be careful about word order here! */
389d00a5
JB
9902 j = i.opcode_length;
9903 if (!i.vex.length)
9904 switch (i.tm.opcode_modifier.opcodespace)
9905 {
9906 case SPACE_BASE:
9907 break;
9908 case SPACE_0F:
9909 ++j;
9910 break;
9911 case SPACE_0F38:
9912 case SPACE_0F3A:
9913 j += 2;
9914 break;
9915 default:
9916 abort ();
9917 }
9918
48ef937e 9919 if (now_seg == absolute_section)
389d00a5
JB
9920 abs_section_offset += j;
9921 else if (j == 1)
29b0f896
AM
9922 {
9923 FRAG_APPEND_1_CHAR (i.tm.base_opcode);
9924 }
9925 else
9926 {
389d00a5
JB
9927 p = frag_more (j);
9928 if (!i.vex.length
9929 && i.tm.opcode_modifier.opcodespace != SPACE_BASE)
9930 {
9931 *p++ = 0x0f;
9932 if (i.tm.opcode_modifier.opcodespace != SPACE_0F)
9933 *p++ = i.tm.opcode_modifier.opcodespace == SPACE_0F38
9934 ? 0x38 : 0x3a;
9935 }
9936
9a182d04 9937 switch (i.opcode_length)
331d2d0d 9938 {
4dffcebc 9939 case 2:
389d00a5
JB
9940 /* Put out high byte first: can't use md_number_to_chars! */
9941 *p++ = (i.tm.base_opcode >> 8) & 0xff;
9942 /* Fall through. */
9943 case 1:
9944 *p = i.tm.base_opcode & 0xff;
4dffcebc
L
9945 break;
9946 default:
9947 abort ();
9948 break;
331d2d0d 9949 }
0f10071e 9950
29b0f896 9951 }
3e73aa7c 9952
29b0f896 9953 /* Now the modrm byte and sib byte (if present). */
40fb9820 9954 if (i.tm.opcode_modifier.modrm)
29b0f896 9955 {
48ef937e
JB
9956 frag_opcode_byte ((i.rm.regmem << 0)
9957 | (i.rm.reg << 3)
9958 | (i.rm.mode << 6));
29b0f896
AM
9959 /* If i.rm.regmem == ESP (4)
9960 && i.rm.mode != (Register mode)
9961 && not 16 bit
9962 ==> need second modrm byte. */
9963 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
9964 && i.rm.mode != 3
dc821c5f 9965 && !(i.base_reg && i.base_reg->reg_type.bitfield.word))
48ef937e
JB
9966 frag_opcode_byte ((i.sib.base << 0)
9967 | (i.sib.index << 3)
9968 | (i.sib.scale << 6));
29b0f896 9969 }
3e73aa7c 9970
29b0f896 9971 if (i.disp_operands)
2bbd9c25 9972 output_disp (insn_start_frag, insn_start_off);
3e73aa7c 9973
29b0f896 9974 if (i.imm_operands)
2bbd9c25 9975 output_imm (insn_start_frag, insn_start_off);
9c33702b
JB
9976
9977 /*
9978 * frag_now_fix () returning plain abs_section_offset when we're in the
9979 * absolute section, and abs_section_offset not getting updated as data
9980 * gets added to the frag breaks the logic below.
9981 */
9982 if (now_seg != absolute_section)
9983 {
9984 j = encoding_length (insn_start_frag, insn_start_off, frag_more (0));
9985 if (j > 15)
9986 as_warn (_("instruction length of %u bytes exceeds the limit of 15"),
9987 j);
e379e5f3
L
9988 else if (fragP)
9989 {
9990 /* NB: Don't add prefix with GOTPC relocation since
9991 output_disp() above depends on the fixed encoding
9992 length. Can't add prefix with TLS relocation since
9993 it breaks TLS linker optimization. */
9994 unsigned int max = i.has_gotpc_tls_reloc ? 0 : 15 - j;
9995 /* Prefix count on the current instruction. */
9996 unsigned int count = i.vex.length;
9997 unsigned int k;
9998 for (k = 0; k < ARRAY_SIZE (i.prefix); k++)
9999 /* REX byte is encoded in VEX/EVEX prefix. */
10000 if (i.prefix[k] && (k != REX_PREFIX || !i.vex.length))
10001 count++;
10002
10003 /* Count prefixes for extended opcode maps. */
10004 if (!i.vex.length)
389d00a5 10005 switch (i.tm.opcode_modifier.opcodespace)
e379e5f3 10006 {
389d00a5 10007 case SPACE_BASE:
e379e5f3 10008 break;
389d00a5
JB
10009 case SPACE_0F:
10010 count++;
e379e5f3 10011 break;
389d00a5
JB
10012 case SPACE_0F38:
10013 case SPACE_0F3A:
10014 count += 2;
e379e5f3
L
10015 break;
10016 default:
10017 abort ();
10018 }
10019
10020 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
10021 == BRANCH_PREFIX)
10022 {
10023 /* Set the maximum prefix size in BRANCH_PREFIX
10024 frag. */
10025 if (fragP->tc_frag_data.max_bytes > max)
10026 fragP->tc_frag_data.max_bytes = max;
10027 if (fragP->tc_frag_data.max_bytes > count)
10028 fragP->tc_frag_data.max_bytes -= count;
10029 else
10030 fragP->tc_frag_data.max_bytes = 0;
10031 }
10032 else
10033 {
10034 /* Remember the maximum prefix size in FUSED_JCC_PADDING
10035 frag. */
10036 unsigned int max_prefix_size;
10037 if (align_branch_prefix_size > max)
10038 max_prefix_size = max;
10039 else
10040 max_prefix_size = align_branch_prefix_size;
10041 if (max_prefix_size > count)
10042 fragP->tc_frag_data.max_prefix_length
10043 = max_prefix_size - count;
10044 }
10045
10046 /* Use existing segment prefix if possible. Use CS
10047 segment prefix in 64-bit mode. In 32-bit mode, use SS
10048 segment prefix with ESP/EBP base register and use DS
10049 segment prefix without ESP/EBP base register. */
10050 if (i.prefix[SEG_PREFIX])
10051 fragP->tc_frag_data.default_prefix = i.prefix[SEG_PREFIX];
10052 else if (flag_code == CODE_64BIT)
10053 fragP->tc_frag_data.default_prefix = CS_PREFIX_OPCODE;
10054 else if (i.base_reg
10055 && (i.base_reg->reg_num == 4
10056 || i.base_reg->reg_num == 5))
10057 fragP->tc_frag_data.default_prefix = SS_PREFIX_OPCODE;
10058 else
10059 fragP->tc_frag_data.default_prefix = DS_PREFIX_OPCODE;
10060 }
9c33702b 10061 }
29b0f896 10062 }
252b5132 10063
e379e5f3
L
10064 /* NB: Don't work with COND_JUMP86 without i386. */
10065 if (align_branch_power
10066 && now_seg != absolute_section
10067 && cpu_arch_flags.bitfield.cpui386)
10068 {
10069 /* Terminate each frag so that we can add prefix and check for
10070 fused jcc. */
10071 frag_wane (frag_now);
10072 frag_new (0);
10073 }
10074
29b0f896
AM
10075#ifdef DEBUG386
10076 if (flag_debug)
10077 {
7b81dfbb 10078 pi ("" /*line*/, &i);
29b0f896
AM
10079 }
10080#endif /* DEBUG386 */
10081}
252b5132 10082
e205caa7
L
10083/* Return the size of the displacement operand N. */
10084
10085static int
10086disp_size (unsigned int n)
10087{
10088 int size = 4;
43234a1e 10089
b5014f7a 10090 if (i.types[n].bitfield.disp64)
40fb9820
L
10091 size = 8;
10092 else if (i.types[n].bitfield.disp8)
10093 size = 1;
10094 else if (i.types[n].bitfield.disp16)
10095 size = 2;
e205caa7
L
10096 return size;
10097}
10098
10099/* Return the size of the immediate operand N. */
10100
10101static int
10102imm_size (unsigned int n)
10103{
10104 int size = 4;
40fb9820
L
10105 if (i.types[n].bitfield.imm64)
10106 size = 8;
10107 else if (i.types[n].bitfield.imm8 || i.types[n].bitfield.imm8s)
10108 size = 1;
10109 else if (i.types[n].bitfield.imm16)
10110 size = 2;
e205caa7
L
10111 return size;
10112}
10113
29b0f896 10114static void
64e74474 10115output_disp (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
10116{
10117 char *p;
10118 unsigned int n;
252b5132 10119
29b0f896
AM
10120 for (n = 0; n < i.operands; n++)
10121 {
b5014f7a 10122 if (operand_type_check (i.types[n], disp))
29b0f896 10123 {
48ef937e
JB
10124 int size = disp_size (n);
10125
10126 if (now_seg == absolute_section)
10127 abs_section_offset += size;
10128 else if (i.op[n].disps->X_op == O_constant)
29b0f896 10129 {
43234a1e 10130 offsetT val = i.op[n].disps->X_add_number;
252b5132 10131
629cfaf1
JB
10132 val = offset_in_range (val >> (size == 1 ? i.memshift : 0),
10133 size);
29b0f896
AM
10134 p = frag_more (size);
10135 md_number_to_chars (p, val, size);
10136 }
10137 else
10138 {
f86103b7 10139 enum bfd_reloc_code_real reloc_type;
a775efc8
JB
10140 bool pcrel = (i.flags[n] & Operand_PCrel) != 0;
10141 bool sign = (flag_code == CODE_64BIT && size == 4
10142 && (!want_disp32 (&i.tm)
10143 || (i.tm.opcode_modifier.jump && !i.jumpabsolute
10144 && !i.types[n].bitfield.baseindex)))
10145 || pcrel;
02a86693 10146 fixS *fixP;
29b0f896 10147
e205caa7 10148 /* We can't have 8 bit displacement here. */
9c2799c2 10149 gas_assert (!i.types[n].bitfield.disp8);
e205caa7 10150
29b0f896
AM
10151 /* The PC relative address is computed relative
10152 to the instruction boundary, so in case immediate
10153 fields follows, we need to adjust the value. */
10154 if (pcrel && i.imm_operands)
10155 {
29b0f896 10156 unsigned int n1;
e205caa7 10157 int sz = 0;
252b5132 10158
29b0f896 10159 for (n1 = 0; n1 < i.operands; n1++)
40fb9820 10160 if (operand_type_check (i.types[n1], imm))
252b5132 10161 {
e205caa7
L
10162 /* Only one immediate is allowed for PC
10163 relative address. */
9c2799c2 10164 gas_assert (sz == 0);
e205caa7
L
10165 sz = imm_size (n1);
10166 i.op[n].disps->X_add_number -= sz;
252b5132 10167 }
29b0f896 10168 /* We should find the immediate. */
9c2799c2 10169 gas_assert (sz != 0);
29b0f896 10170 }
520dc8e8 10171
29b0f896 10172 p = frag_more (size);
d258b828 10173 reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
d6ab8113 10174 if (GOT_symbol
2bbd9c25 10175 && GOT_symbol == i.op[n].disps->X_add_symbol
d6ab8113 10176 && (((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
10177 || reloc_type == BFD_RELOC_X86_64_32S
10178 || (reloc_type == BFD_RELOC_64
10179 && object_64bit))
d6ab8113
JB
10180 && (i.op[n].disps->X_op == O_symbol
10181 || (i.op[n].disps->X_op == O_add
10182 && ((symbol_get_value_expression
10183 (i.op[n].disps->X_op_symbol)->X_op)
10184 == O_subtract))))
10185 || reloc_type == BFD_RELOC_32_PCREL))
2bbd9c25 10186 {
4fa24527 10187 if (!object_64bit)
7b81dfbb
AJ
10188 {
10189 reloc_type = BFD_RELOC_386_GOTPC;
5b7c81bd 10190 i.has_gotpc_tls_reloc = true;
98da05bf 10191 i.op[n].disps->X_add_number +=
d583596c 10192 encoding_length (insn_start_frag, insn_start_off, p);
7b81dfbb
AJ
10193 }
10194 else if (reloc_type == BFD_RELOC_64)
10195 reloc_type = BFD_RELOC_X86_64_GOTPC64;
d6ab8113 10196 else
7b81dfbb
AJ
10197 /* Don't do the adjustment for x86-64, as there
10198 the pcrel addressing is relative to the _next_
10199 insn, and that is taken care of in other code. */
d6ab8113 10200 reloc_type = BFD_RELOC_X86_64_GOTPC32;
2bbd9c25 10201 }
e379e5f3
L
10202 else if (align_branch_power)
10203 {
10204 switch (reloc_type)
10205 {
10206 case BFD_RELOC_386_TLS_GD:
10207 case BFD_RELOC_386_TLS_LDM:
10208 case BFD_RELOC_386_TLS_IE:
10209 case BFD_RELOC_386_TLS_IE_32:
10210 case BFD_RELOC_386_TLS_GOTIE:
10211 case BFD_RELOC_386_TLS_GOTDESC:
10212 case BFD_RELOC_386_TLS_DESC_CALL:
10213 case BFD_RELOC_X86_64_TLSGD:
10214 case BFD_RELOC_X86_64_TLSLD:
10215 case BFD_RELOC_X86_64_GOTTPOFF:
10216 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
10217 case BFD_RELOC_X86_64_TLSDESC_CALL:
5b7c81bd 10218 i.has_gotpc_tls_reloc = true;
e379e5f3
L
10219 default:
10220 break;
10221 }
10222 }
02a86693
L
10223 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal,
10224 size, i.op[n].disps, pcrel,
10225 reloc_type);
eb19308f
JB
10226
10227 if (flag_code == CODE_64BIT && size == 4 && pcrel
10228 && !i.prefix[ADDR_PREFIX])
10229 fixP->fx_signed = 1;
10230
02a86693
L
10231 /* Check for "call/jmp *mem", "mov mem, %reg",
10232 "test %reg, mem" and "binop mem, %reg" where binop
10233 is one of adc, add, and, cmp, or, sbb, sub, xor
e60f4d3b
L
10234 instructions without data prefix. Always generate
10235 R_386_GOT32X for "sym*GOT" operand in 32-bit mode. */
10236 if (i.prefix[DATA_PREFIX] == 0
10237 && (generate_relax_relocations
10238 || (!object_64bit
10239 && i.rm.mode == 0
10240 && i.rm.regmem == 5))
0cb4071e
L
10241 && (i.rm.mode == 2
10242 || (i.rm.mode == 0 && i.rm.regmem == 5))
389d00a5 10243 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
02a86693
L
10244 && ((i.operands == 1
10245 && i.tm.base_opcode == 0xff
10246 && (i.rm.reg == 2 || i.rm.reg == 4))
10247 || (i.operands == 2
10248 && (i.tm.base_opcode == 0x8b
10249 || i.tm.base_opcode == 0x85
2ae4c703 10250 || (i.tm.base_opcode & ~0x38) == 0x03))))
02a86693
L
10251 {
10252 if (object_64bit)
10253 {
10254 fixP->fx_tcbit = i.rex != 0;
10255 if (i.base_reg
e968fc9b 10256 && (i.base_reg->reg_num == RegIP))
02a86693
L
10257 fixP->fx_tcbit2 = 1;
10258 }
10259 else
10260 fixP->fx_tcbit2 = 1;
10261 }
29b0f896
AM
10262 }
10263 }
10264 }
10265}
252b5132 10266
29b0f896 10267static void
64e74474 10268output_imm (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
10269{
10270 char *p;
10271 unsigned int n;
252b5132 10272
29b0f896
AM
10273 for (n = 0; n < i.operands; n++)
10274 {
40fb9820 10275 if (operand_type_check (i.types[n], imm))
29b0f896 10276 {
48ef937e
JB
10277 int size = imm_size (n);
10278
10279 if (now_seg == absolute_section)
10280 abs_section_offset += size;
10281 else if (i.op[n].imms->X_op == O_constant)
29b0f896 10282 {
29b0f896 10283 offsetT val;
b4cac588 10284
29b0f896
AM
10285 val = offset_in_range (i.op[n].imms->X_add_number,
10286 size);
10287 p = frag_more (size);
10288 md_number_to_chars (p, val, size);
10289 }
10290 else
10291 {
10292 /* Not absolute_section.
10293 Need a 32-bit fixup (don't support 8bit
10294 non-absolute imms). Try to support other
10295 sizes ... */
f86103b7 10296 enum bfd_reloc_code_real reloc_type;
e205caa7 10297 int sign;
29b0f896 10298
40fb9820 10299 if (i.types[n].bitfield.imm32s
a7d61044 10300 && (i.suffix == QWORD_MNEM_SUFFIX
40fb9820 10301 || (!i.suffix && i.tm.opcode_modifier.no_lsuf)))
29b0f896 10302 sign = 1;
e205caa7
L
10303 else
10304 sign = 0;
520dc8e8 10305
29b0f896 10306 p = frag_more (size);
d258b828 10307 reloc_type = reloc (size, 0, sign, i.reloc[n]);
f86103b7 10308
2bbd9c25
JJ
10309 /* This is tough to explain. We end up with this one if we
10310 * have operands that look like
10311 * "_GLOBAL_OFFSET_TABLE_+[.-.L284]". The goal here is to
10312 * obtain the absolute address of the GOT, and it is strongly
10313 * preferable from a performance point of view to avoid using
10314 * a runtime relocation for this. The actual sequence of
10315 * instructions often look something like:
10316 *
10317 * call .L66
10318 * .L66:
10319 * popl %ebx
10320 * addl $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
10321 *
10322 * The call and pop essentially return the absolute address
10323 * of the label .L66 and store it in %ebx. The linker itself
10324 * will ultimately change the first operand of the addl so
10325 * that %ebx points to the GOT, but to keep things simple, the
10326 * .o file must have this operand set so that it generates not
10327 * the absolute address of .L66, but the absolute address of
10328 * itself. This allows the linker itself simply treat a GOTPC
10329 * relocation as asking for a pcrel offset to the GOT to be
10330 * added in, and the addend of the relocation is stored in the
10331 * operand field for the instruction itself.
10332 *
10333 * Our job here is to fix the operand so that it would add
10334 * the correct offset so that %ebx would point to itself. The
10335 * thing that is tricky is that .-.L66 will point to the
10336 * beginning of the instruction, so we need to further modify
10337 * the operand so that it will point to itself. There are
10338 * other cases where you have something like:
10339 *
10340 * .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
10341 *
10342 * and here no correction would be required. Internally in
10343 * the assembler we treat operands of this form as not being
10344 * pcrel since the '.' is explicitly mentioned, and I wonder
10345 * whether it would simplify matters to do it this way. Who
10346 * knows. In earlier versions of the PIC patches, the
10347 * pcrel_adjust field was used to store the correction, but
10348 * since the expression is not pcrel, I felt it would be
10349 * confusing to do it this way. */
10350
d6ab8113 10351 if ((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
10352 || reloc_type == BFD_RELOC_X86_64_32S
10353 || reloc_type == BFD_RELOC_64)
29b0f896
AM
10354 && GOT_symbol
10355 && GOT_symbol == i.op[n].imms->X_add_symbol
10356 && (i.op[n].imms->X_op == O_symbol
10357 || (i.op[n].imms->X_op == O_add
10358 && ((symbol_get_value_expression
10359 (i.op[n].imms->X_op_symbol)->X_op)
10360 == O_subtract))))
10361 {
4fa24527 10362 if (!object_64bit)
d6ab8113 10363 reloc_type = BFD_RELOC_386_GOTPC;
7b81dfbb 10364 else if (size == 4)
d6ab8113 10365 reloc_type = BFD_RELOC_X86_64_GOTPC32;
7b81dfbb
AJ
10366 else if (size == 8)
10367 reloc_type = BFD_RELOC_X86_64_GOTPC64;
5b7c81bd 10368 i.has_gotpc_tls_reloc = true;
d583596c
JB
10369 i.op[n].imms->X_add_number +=
10370 encoding_length (insn_start_frag, insn_start_off, p);
29b0f896 10371 }
29b0f896
AM
10372 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
10373 i.op[n].imms, 0, reloc_type);
10374 }
10375 }
10376 }
252b5132
RH
10377}
10378\f
d182319b
JB
10379/* x86_cons_fix_new is called via the expression parsing code when a
10380 reloc is needed. We use this hook to get the correct .got reloc. */
d182319b
JB
10381static int cons_sign = -1;
10382
10383void
e3bb37b5 10384x86_cons_fix_new (fragS *frag, unsigned int off, unsigned int len,
62ebcb5c 10385 expressionS *exp, bfd_reloc_code_real_type r)
d182319b 10386{
d258b828 10387 r = reloc (len, 0, cons_sign, r);
d182319b
JB
10388
10389#ifdef TE_PE
10390 if (exp->X_op == O_secrel)
10391 {
10392 exp->X_op = O_symbol;
10393 r = BFD_RELOC_32_SECREL;
10394 }
145667f8
MH
10395 else if (exp->X_op == O_secidx)
10396 r = BFD_RELOC_16_SECIDX;
d182319b
JB
10397#endif
10398
10399 fix_new_exp (frag, off, len, exp, 0, r);
10400}
10401
357d1bd8
L
10402/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
10403 purpose of the `.dc.a' internal pseudo-op. */
10404
10405int
10406x86_address_bytes (void)
10407{
10408 if ((stdoutput->arch_info->mach & bfd_mach_x64_32))
10409 return 4;
10410 return stdoutput->arch_info->bits_per_address / 8;
10411}
10412
deea4973
JB
10413#if (!(defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined (OBJ_MACH_O)) \
10414 || defined (LEX_AT)) && !defined (TE_PE)
d258b828 10415# define lex_got(reloc, adjust, types) NULL
718ddfc0 10416#else
f3c180ae
AM
10417/* Parse operands of the form
10418 <symbol>@GOTOFF+<nnn>
10419 and similar .plt or .got references.
10420
10421 If we find one, set up the correct relocation in RELOC and copy the
10422 input string, minus the `@GOTOFF' into a malloc'd buffer for
10423 parsing by the calling routine. Return this buffer, and if ADJUST
10424 is non-null set it to the length of the string we removed from the
10425 input line. Otherwise return NULL. */
10426static char *
91d6fa6a 10427lex_got (enum bfd_reloc_code_real *rel,
64e74474 10428 int *adjust,
d258b828 10429 i386_operand_type *types)
f3c180ae 10430{
7b81dfbb
AJ
10431 /* Some of the relocations depend on the size of what field is to
10432 be relocated. But in our callers i386_immediate and i386_displacement
10433 we don't yet know the operand size (this will be set by insn
10434 matching). Hence we record the word32 relocation here,
10435 and adjust the reloc according to the real size in reloc(). */
145667f8
MH
10436 static const struct
10437 {
f3c180ae 10438 const char *str;
cff8d58a 10439 int len;
4fa24527 10440 const enum bfd_reloc_code_real rel[2];
40fb9820 10441 const i386_operand_type types64;
5b7c81bd 10442 bool need_GOT_symbol;
145667f8
MH
10443 }
10444 gotrel[] =
10445 {
05909f23
JB
10446
10447#define OPERAND_TYPE_IMM32_32S_DISP32 { .bitfield = \
10448 { .imm32 = 1, .imm32s = 1, .disp32 = 1 } }
10449#define OPERAND_TYPE_IMM32_32S_64_DISP32 { .bitfield = \
10450 { .imm32 = 1, .imm32s = 1, .imm64 = 1, .disp32 = 1 } }
10451#define OPERAND_TYPE_IMM32_32S_64_DISP32_64 { .bitfield = \
10452 { .imm32 = 1, .imm32s = 1, .imm64 = 1, .disp32 = 1, .disp64 = 1 } }
10453#define OPERAND_TYPE_IMM64_DISP64 { .bitfield = \
10454 { .imm64 = 1, .disp64 = 1 } }
10455
deea4973 10456#ifndef TE_PE
8ce3d284 10457#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
10458 { STRING_COMMA_LEN ("SIZE"), { BFD_RELOC_SIZE32,
10459 BFD_RELOC_SIZE32 },
05909f23 10460 { .bitfield = { .imm32 = 1, .imm64 = 1 } }, false },
8ce3d284 10461#endif
cff8d58a
L
10462 { STRING_COMMA_LEN ("PLTOFF"), { _dummy_first_bfd_reloc_code_real,
10463 BFD_RELOC_X86_64_PLTOFF64 },
05909f23 10464 { .bitfield = { .imm64 = 1 } }, true },
cff8d58a
L
10465 { STRING_COMMA_LEN ("PLT"), { BFD_RELOC_386_PLT32,
10466 BFD_RELOC_X86_64_PLT32 },
a775efc8 10467 OPERAND_TYPE_IMM32_32S_DISP32, false },
cff8d58a
L
10468 { STRING_COMMA_LEN ("GOTPLT"), { _dummy_first_bfd_reloc_code_real,
10469 BFD_RELOC_X86_64_GOTPLT64 },
5b7c81bd 10470 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
10471 { STRING_COMMA_LEN ("GOTOFF"), { BFD_RELOC_386_GOTOFF,
10472 BFD_RELOC_X86_64_GOTOFF64 },
5b7c81bd 10473 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
10474 { STRING_COMMA_LEN ("GOTPCREL"), { _dummy_first_bfd_reloc_code_real,
10475 BFD_RELOC_X86_64_GOTPCREL },
a775efc8 10476 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10477 { STRING_COMMA_LEN ("TLSGD"), { BFD_RELOC_386_TLS_GD,
10478 BFD_RELOC_X86_64_TLSGD },
a775efc8 10479 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10480 { STRING_COMMA_LEN ("TLSLDM"), { BFD_RELOC_386_TLS_LDM,
10481 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10482 OPERAND_TYPE_NONE, true },
cff8d58a
L
10483 { STRING_COMMA_LEN ("TLSLD"), { _dummy_first_bfd_reloc_code_real,
10484 BFD_RELOC_X86_64_TLSLD },
a775efc8 10485 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10486 { STRING_COMMA_LEN ("GOTTPOFF"), { BFD_RELOC_386_TLS_IE_32,
10487 BFD_RELOC_X86_64_GOTTPOFF },
a775efc8 10488 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10489 { STRING_COMMA_LEN ("TPOFF"), { BFD_RELOC_386_TLS_LE_32,
10490 BFD_RELOC_X86_64_TPOFF32 },
a775efc8 10491 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
10492 { STRING_COMMA_LEN ("NTPOFF"), { BFD_RELOC_386_TLS_LE,
10493 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10494 OPERAND_TYPE_NONE, true },
cff8d58a
L
10495 { STRING_COMMA_LEN ("DTPOFF"), { BFD_RELOC_386_TLS_LDO_32,
10496 BFD_RELOC_X86_64_DTPOFF32 },
a775efc8 10497 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
10498 { STRING_COMMA_LEN ("GOTNTPOFF"),{ BFD_RELOC_386_TLS_GOTIE,
10499 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10500 OPERAND_TYPE_NONE, true },
cff8d58a
L
10501 { STRING_COMMA_LEN ("INDNTPOFF"),{ BFD_RELOC_386_TLS_IE,
10502 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10503 OPERAND_TYPE_NONE, true },
cff8d58a
L
10504 { STRING_COMMA_LEN ("GOT"), { BFD_RELOC_386_GOT32,
10505 BFD_RELOC_X86_64_GOT32 },
a775efc8 10506 OPERAND_TYPE_IMM32_32S_64_DISP32, true },
cff8d58a
L
10507 { STRING_COMMA_LEN ("TLSDESC"), { BFD_RELOC_386_TLS_GOTDESC,
10508 BFD_RELOC_X86_64_GOTPC32_TLSDESC },
a775efc8 10509 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10510 { STRING_COMMA_LEN ("TLSCALL"), { BFD_RELOC_386_TLS_DESC_CALL,
10511 BFD_RELOC_X86_64_TLSDESC_CALL },
a775efc8 10512 OPERAND_TYPE_IMM32_32S_DISP32, true },
deea4973
JB
10513#else /* TE_PE */
10514 { STRING_COMMA_LEN ("SECREL32"), { BFD_RELOC_32_SECREL,
10515 BFD_RELOC_32_SECREL },
a775efc8 10516 OPERAND_TYPE_IMM32_32S_64_DISP32_64, false },
deea4973 10517#endif
05909f23
JB
10518
10519#undef OPERAND_TYPE_IMM32_32S_DISP32
10520#undef OPERAND_TYPE_IMM32_32S_64_DISP32
10521#undef OPERAND_TYPE_IMM32_32S_64_DISP32_64
10522#undef OPERAND_TYPE_IMM64_DISP64
10523
f3c180ae
AM
10524 };
10525 char *cp;
10526 unsigned int j;
10527
deea4973 10528#if defined (OBJ_MAYBE_ELF) && !defined (TE_PE)
718ddfc0
JB
10529 if (!IS_ELF)
10530 return NULL;
d382c579 10531#endif
718ddfc0 10532
f3c180ae 10533 for (cp = input_line_pointer; *cp != '@'; cp++)
67c11a9b 10534 if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
f3c180ae
AM
10535 return NULL;
10536
47465058 10537 for (j = 0; j < ARRAY_SIZE (gotrel); j++)
f3c180ae 10538 {
cff8d58a 10539 int len = gotrel[j].len;
28f81592 10540 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
f3c180ae 10541 {
4fa24527 10542 if (gotrel[j].rel[object_64bit] != 0)
f3c180ae 10543 {
28f81592
AM
10544 int first, second;
10545 char *tmpbuf, *past_reloc;
f3c180ae 10546
91d6fa6a 10547 *rel = gotrel[j].rel[object_64bit];
f3c180ae 10548
3956db08
JB
10549 if (types)
10550 {
10551 if (flag_code != CODE_64BIT)
40fb9820
L
10552 {
10553 types->bitfield.imm32 = 1;
10554 types->bitfield.disp32 = 1;
10555 }
3956db08
JB
10556 else
10557 *types = gotrel[j].types64;
10558 }
10559
844bf810 10560 if (gotrel[j].need_GOT_symbol && GOT_symbol == NULL)
f3c180ae
AM
10561 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
10562
28f81592 10563 /* The length of the first part of our input line. */
f3c180ae 10564 first = cp - input_line_pointer;
28f81592
AM
10565
10566 /* The second part goes from after the reloc token until
67c11a9b 10567 (and including) an end_of_line char or comma. */
28f81592 10568 past_reloc = cp + 1 + len;
67c11a9b
AM
10569 cp = past_reloc;
10570 while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
10571 ++cp;
10572 second = cp + 1 - past_reloc;
28f81592
AM
10573
10574 /* Allocate and copy string. The trailing NUL shouldn't
10575 be necessary, but be safe. */
add39d23 10576 tmpbuf = XNEWVEC (char, first + second + 2);
f3c180ae 10577 memcpy (tmpbuf, input_line_pointer, first);
0787a12d
AM
10578 if (second != 0 && *past_reloc != ' ')
10579 /* Replace the relocation token with ' ', so that
10580 errors like foo@GOTOFF1 will be detected. */
10581 tmpbuf[first++] = ' ';
af89796a
L
10582 else
10583 /* Increment length by 1 if the relocation token is
10584 removed. */
10585 len++;
10586 if (adjust)
10587 *adjust = len;
0787a12d
AM
10588 memcpy (tmpbuf + first, past_reloc, second);
10589 tmpbuf[first + second] = '\0';
f3c180ae
AM
10590 return tmpbuf;
10591 }
10592
4fa24527
JB
10593 as_bad (_("@%s reloc is not supported with %d-bit output format"),
10594 gotrel[j].str, 1 << (5 + object_64bit));
f3c180ae
AM
10595 return NULL;
10596 }
10597 }
10598
10599 /* Might be a symbol version string. Don't as_bad here. */
10600 return NULL;
10601}
4e4f7c87 10602#endif
f3c180ae 10603
62ebcb5c 10604bfd_reloc_code_real_type
e3bb37b5 10605x86_cons (expressionS *exp, int size)
f3c180ae 10606{
62ebcb5c
AM
10607 bfd_reloc_code_real_type got_reloc = NO_RELOC;
10608
2748c1b1
L
10609#if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
10610 && !defined (LEX_AT)) \
10611 || defined (TE_PE)
ee86248c
JB
10612 intel_syntax = -intel_syntax;
10613
3c7b9c2c 10614 exp->X_md = 0;
4fa24527 10615 if (size == 4 || (object_64bit && size == 8))
f3c180ae
AM
10616 {
10617 /* Handle @GOTOFF and the like in an expression. */
10618 char *save;
10619 char *gotfree_input_line;
4a57f2cf 10620 int adjust = 0;
f3c180ae
AM
10621
10622 save = input_line_pointer;
d258b828 10623 gotfree_input_line = lex_got (&got_reloc, &adjust, NULL);
f3c180ae
AM
10624 if (gotfree_input_line)
10625 input_line_pointer = gotfree_input_line;
10626
10627 expression (exp);
10628
10629 if (gotfree_input_line)
10630 {
10631 /* expression () has merrily parsed up to the end of line,
10632 or a comma - in the wrong buffer. Transfer how far
10633 input_line_pointer has moved to the right buffer. */
10634 input_line_pointer = (save
10635 + (input_line_pointer - gotfree_input_line)
10636 + adjust);
10637 free (gotfree_input_line);
3992d3b7
AM
10638 if (exp->X_op == O_constant
10639 || exp->X_op == O_absent
10640 || exp->X_op == O_illegal
0398aac5 10641 || exp->X_op == O_register
3992d3b7
AM
10642 || exp->X_op == O_big)
10643 {
10644 char c = *input_line_pointer;
10645 *input_line_pointer = 0;
10646 as_bad (_("missing or invalid expression `%s'"), save);
10647 *input_line_pointer = c;
10648 }
b9519cfe
L
10649 else if ((got_reloc == BFD_RELOC_386_PLT32
10650 || got_reloc == BFD_RELOC_X86_64_PLT32)
10651 && exp->X_op != O_symbol)
10652 {
10653 char c = *input_line_pointer;
10654 *input_line_pointer = 0;
10655 as_bad (_("invalid PLT expression `%s'"), save);
10656 *input_line_pointer = c;
10657 }
f3c180ae
AM
10658 }
10659 }
10660 else
10661 expression (exp);
ee86248c
JB
10662
10663 intel_syntax = -intel_syntax;
10664
10665 if (intel_syntax)
10666 i386_intel_simplify (exp);
2748c1b1
L
10667#else
10668 expression (exp);
10669#endif
62ebcb5c 10670
a442cac5
JB
10671 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
10672 if (size == 4 && exp->X_op == O_constant && !object_64bit)
10673 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
10674
62ebcb5c 10675 return got_reloc;
f3c180ae 10676}
f3c180ae 10677
9f32dd5b
L
10678static void
10679signed_cons (int size)
6482c264 10680{
a442cac5 10681 if (object_64bit)
d182319b
JB
10682 cons_sign = 1;
10683 cons (size);
10684 cons_sign = -1;
6482c264
NC
10685}
10686
d182319b 10687#ifdef TE_PE
6482c264 10688static void
7016a5d5 10689pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
6482c264
NC
10690{
10691 expressionS exp;
10692
10693 do
10694 {
10695 expression (&exp);
10696 if (exp.X_op == O_symbol)
10697 exp.X_op = O_secrel;
10698
10699 emit_expr (&exp, 4);
10700 }
10701 while (*input_line_pointer++ == ',');
10702
10703 input_line_pointer--;
10704 demand_empty_rest_of_line ();
10705}
145667f8
MH
10706
10707static void
10708pe_directive_secidx (int dummy ATTRIBUTE_UNUSED)
10709{
10710 expressionS exp;
10711
10712 do
10713 {
10714 expression (&exp);
10715 if (exp.X_op == O_symbol)
10716 exp.X_op = O_secidx;
10717
10718 emit_expr (&exp, 2);
10719 }
10720 while (*input_line_pointer++ == ',');
10721
10722 input_line_pointer--;
10723 demand_empty_rest_of_line ();
10724}
6482c264
NC
10725#endif
10726
7063667e
JB
10727/* Handle Rounding Control / SAE specifiers. */
10728
10729static char *
10730RC_SAE_specifier (const char *pstr)
10731{
10732 unsigned int j;
10733
10734 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); j++)
10735 {
10736 if (!strncmp (pstr, RC_NamesTable[j].name, RC_NamesTable[j].len))
10737 {
10738 if (i.rounding.type != rc_none)
10739 {
10740 as_bad (_("duplicated `{%s}'"), RC_NamesTable[j].name);
10741 return NULL;
10742 }
10743
10744 i.rounding.type = RC_NamesTable[j].type;
10745
10746 return (char *)(pstr + RC_NamesTable[j].len);
10747 }
10748 }
10749
10750 return NULL;
10751}
10752
43234a1e
L
10753/* Handle Vector operations. */
10754
10755static char *
f70c6814 10756check_VecOperations (char *op_string)
43234a1e
L
10757{
10758 const reg_entry *mask;
10759 const char *saved;
10760 char *end_op;
10761
f70c6814 10762 while (*op_string)
43234a1e
L
10763 {
10764 saved = op_string;
10765 if (*op_string == '{')
10766 {
10767 op_string++;
10768
10769 /* Check broadcasts. */
d34049e8 10770 if (startswith (op_string, "1to"))
43234a1e 10771 {
5273a3cd 10772 unsigned int bcst_type;
43234a1e 10773
5273a3cd 10774 if (i.broadcast.type)
43234a1e
L
10775 goto duplicated_vec_op;
10776
10777 op_string += 3;
10778 if (*op_string == '8')
8e6e0792 10779 bcst_type = 8;
b28d1bda 10780 else if (*op_string == '4')
8e6e0792 10781 bcst_type = 4;
b28d1bda 10782 else if (*op_string == '2')
8e6e0792 10783 bcst_type = 2;
43234a1e
L
10784 else if (*op_string == '1'
10785 && *(op_string+1) == '6')
10786 {
8e6e0792 10787 bcst_type = 16;
43234a1e
L
10788 op_string++;
10789 }
0cc78721
CL
10790 else if (*op_string == '3'
10791 && *(op_string+1) == '2')
10792 {
10793 bcst_type = 32;
10794 op_string++;
10795 }
43234a1e
L
10796 else
10797 {
10798 as_bad (_("Unsupported broadcast: `%s'"), saved);
10799 return NULL;
10800 }
10801 op_string++;
10802
5273a3cd
JB
10803 i.broadcast.type = bcst_type;
10804 i.broadcast.operand = this_operand;
43234a1e
L
10805 }
10806 /* Check masking operation. */
10807 else if ((mask = parse_register (op_string, &end_op)) != NULL)
10808 {
8a6fb3f9
JB
10809 if (mask == &bad_reg)
10810 return NULL;
10811
43234a1e 10812 /* k0 can't be used for write mask. */
f74a6307 10813 if (mask->reg_type.bitfield.class != RegMask || !mask->reg_num)
43234a1e 10814 {
6d2cd6b2
JB
10815 as_bad (_("`%s%s' can't be used for write mask"),
10816 register_prefix, mask->reg_name);
43234a1e
L
10817 return NULL;
10818 }
10819
6225c532 10820 if (!i.mask.reg)
43234a1e 10821 {
6225c532
JB
10822 i.mask.reg = mask;
10823 i.mask.operand = this_operand;
43234a1e 10824 }
6225c532
JB
10825 else if (i.mask.reg->reg_num)
10826 goto duplicated_vec_op;
43234a1e
L
10827 else
10828 {
6225c532 10829 i.mask.reg = mask;
43234a1e
L
10830
10831 /* Only "{z}" is allowed here. No need to check
10832 zeroing mask explicitly. */
6225c532 10833 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
10834 {
10835 as_bad (_("invalid write mask `%s'"), saved);
10836 return NULL;
10837 }
10838 }
10839
10840 op_string = end_op;
10841 }
10842 /* Check zeroing-flag for masking operation. */
10843 else if (*op_string == 'z')
10844 {
6225c532 10845 if (!i.mask.reg)
43234a1e 10846 {
6225c532
JB
10847 i.mask.reg = reg_k0;
10848 i.mask.zeroing = 1;
10849 i.mask.operand = this_operand;
43234a1e
L
10850 }
10851 else
10852 {
6225c532 10853 if (i.mask.zeroing)
43234a1e
L
10854 {
10855 duplicated_vec_op:
10856 as_bad (_("duplicated `%s'"), saved);
10857 return NULL;
10858 }
10859
6225c532 10860 i.mask.zeroing = 1;
43234a1e
L
10861
10862 /* Only "{%k}" is allowed here. No need to check mask
10863 register explicitly. */
6225c532 10864 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
10865 {
10866 as_bad (_("invalid zeroing-masking `%s'"),
10867 saved);
10868 return NULL;
10869 }
10870 }
10871
10872 op_string++;
10873 }
7063667e
JB
10874 else if (intel_syntax
10875 && (op_string = RC_SAE_specifier (op_string)) != NULL)
10876 i.rounding.modifier = true;
43234a1e
L
10877 else
10878 goto unknown_vec_op;
10879
10880 if (*op_string != '}')
10881 {
10882 as_bad (_("missing `}' in `%s'"), saved);
10883 return NULL;
10884 }
10885 op_string++;
0ba3a731
L
10886
10887 /* Strip whitespace since the addition of pseudo prefixes
10888 changed how the scrubber treats '{'. */
10889 if (is_space_char (*op_string))
10890 ++op_string;
10891
43234a1e
L
10892 continue;
10893 }
10894 unknown_vec_op:
10895 /* We don't know this one. */
10896 as_bad (_("unknown vector operation: `%s'"), saved);
10897 return NULL;
10898 }
10899
6225c532 10900 if (i.mask.reg && i.mask.zeroing && !i.mask.reg->reg_num)
6d2cd6b2
JB
10901 {
10902 as_bad (_("zeroing-masking only allowed with write mask"));
10903 return NULL;
10904 }
10905
43234a1e
L
10906 return op_string;
10907}
10908
252b5132 10909static int
70e41ade 10910i386_immediate (char *imm_start)
252b5132
RH
10911{
10912 char *save_input_line_pointer;
f3c180ae 10913 char *gotfree_input_line;
252b5132 10914 segT exp_seg = 0;
47926f60 10915 expressionS *exp;
40fb9820
L
10916 i386_operand_type types;
10917
0dfbf9d7 10918 operand_type_set (&types, ~0);
252b5132
RH
10919
10920 if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
10921 {
31b2323c
L
10922 as_bad (_("at most %d immediate operands are allowed"),
10923 MAX_IMMEDIATE_OPERANDS);
252b5132
RH
10924 return 0;
10925 }
10926
10927 exp = &im_expressions[i.imm_operands++];
520dc8e8 10928 i.op[this_operand].imms = exp;
252b5132
RH
10929
10930 if (is_space_char (*imm_start))
10931 ++imm_start;
10932
10933 save_input_line_pointer = input_line_pointer;
10934 input_line_pointer = imm_start;
10935
d258b828 10936 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
10937 if (gotfree_input_line)
10938 input_line_pointer = gotfree_input_line;
252b5132
RH
10939
10940 exp_seg = expression (exp);
10941
83183c0c 10942 SKIP_WHITESPACE ();
252b5132 10943 if (*input_line_pointer)
f3c180ae 10944 as_bad (_("junk `%s' after expression"), input_line_pointer);
252b5132
RH
10945
10946 input_line_pointer = save_input_line_pointer;
f3c180ae 10947 if (gotfree_input_line)
ee86248c
JB
10948 {
10949 free (gotfree_input_line);
10950
9aac24b1 10951 if (exp->X_op == O_constant)
ee86248c
JB
10952 exp->X_op = O_illegal;
10953 }
10954
9aac24b1
JB
10955 if (exp_seg == reg_section)
10956 {
10957 as_bad (_("illegal immediate register operand %s"), imm_start);
10958 return 0;
10959 }
10960
ee86248c
JB
10961 return i386_finalize_immediate (exp_seg, exp, types, imm_start);
10962}
252b5132 10963
ee86248c
JB
10964static int
10965i386_finalize_immediate (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
10966 i386_operand_type types, const char *imm_start)
10967{
10968 if (exp->X_op == O_absent || exp->X_op == O_illegal || exp->X_op == O_big)
252b5132 10969 {
313c53d1
L
10970 if (imm_start)
10971 as_bad (_("missing or invalid immediate expression `%s'"),
10972 imm_start);
3992d3b7 10973 return 0;
252b5132 10974 }
3e73aa7c 10975 else if (exp->X_op == O_constant)
252b5132 10976 {
47926f60 10977 /* Size it properly later. */
40fb9820 10978 i.types[this_operand].bitfield.imm64 = 1;
a442cac5
JB
10979
10980 /* If not 64bit, sign/zero extend val, to account for wraparound
10981 when !BFD64. */
10982 if (flag_code != CODE_64BIT)
10983 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
252b5132 10984 }
4c63da97 10985#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
f86103b7 10986 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
31312f95 10987 && exp_seg != absolute_section
47926f60 10988 && exp_seg != text_section
24eab124
AM
10989 && exp_seg != data_section
10990 && exp_seg != bss_section
10991 && exp_seg != undefined_section
f86103b7 10992 && !bfd_is_com_section (exp_seg))
252b5132 10993 {
d0b47220 10994 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
252b5132
RH
10995 return 0;
10996 }
10997#endif
10998 else
10999 {
11000 /* This is an address. The size of the address will be
24eab124 11001 determined later, depending on destination register,
3e73aa7c 11002 suffix, or the default for the section. */
40fb9820
L
11003 i.types[this_operand].bitfield.imm8 = 1;
11004 i.types[this_operand].bitfield.imm16 = 1;
11005 i.types[this_operand].bitfield.imm32 = 1;
11006 i.types[this_operand].bitfield.imm32s = 1;
11007 i.types[this_operand].bitfield.imm64 = 1;
c6fb90c8
L
11008 i.types[this_operand] = operand_type_and (i.types[this_operand],
11009 types);
252b5132
RH
11010 }
11011
11012 return 1;
11013}
11014
551c1ca1 11015static char *
e3bb37b5 11016i386_scale (char *scale)
252b5132 11017{
551c1ca1
AM
11018 offsetT val;
11019 char *save = input_line_pointer;
252b5132 11020
551c1ca1
AM
11021 input_line_pointer = scale;
11022 val = get_absolute_expression ();
11023
11024 switch (val)
252b5132 11025 {
551c1ca1 11026 case 1:
252b5132
RH
11027 i.log2_scale_factor = 0;
11028 break;
551c1ca1 11029 case 2:
252b5132
RH
11030 i.log2_scale_factor = 1;
11031 break;
551c1ca1 11032 case 4:
252b5132
RH
11033 i.log2_scale_factor = 2;
11034 break;
551c1ca1 11035 case 8:
252b5132
RH
11036 i.log2_scale_factor = 3;
11037 break;
11038 default:
a724f0f4
JB
11039 {
11040 char sep = *input_line_pointer;
11041
11042 *input_line_pointer = '\0';
11043 as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
11044 scale);
11045 *input_line_pointer = sep;
11046 input_line_pointer = save;
11047 return NULL;
11048 }
252b5132 11049 }
29b0f896 11050 if (i.log2_scale_factor != 0 && i.index_reg == 0)
252b5132
RH
11051 {
11052 as_warn (_("scale factor of %d without an index register"),
24eab124 11053 1 << i.log2_scale_factor);
252b5132 11054 i.log2_scale_factor = 0;
252b5132 11055 }
551c1ca1
AM
11056 scale = input_line_pointer;
11057 input_line_pointer = save;
11058 return scale;
252b5132
RH
11059}
11060
252b5132 11061static int
e3bb37b5 11062i386_displacement (char *disp_start, char *disp_end)
252b5132 11063{
29b0f896 11064 expressionS *exp;
252b5132
RH
11065 segT exp_seg = 0;
11066 char *save_input_line_pointer;
f3c180ae 11067 char *gotfree_input_line;
40fb9820
L
11068 int override;
11069 i386_operand_type bigdisp, types = anydisp;
3992d3b7 11070 int ret;
252b5132 11071
31b2323c
L
11072 if (i.disp_operands == MAX_MEMORY_OPERANDS)
11073 {
11074 as_bad (_("at most %d displacement operands are allowed"),
11075 MAX_MEMORY_OPERANDS);
11076 return 0;
11077 }
11078
0dfbf9d7 11079 operand_type_set (&bigdisp, 0);
6f2f06be 11080 if (i.jumpabsolute
48bcea9f 11081 || i.types[this_operand].bitfield.baseindex
0cfa3eb3
JB
11082 || (current_templates->start->opcode_modifier.jump != JUMP
11083 && current_templates->start->opcode_modifier.jump != JUMP_DWORD))
e05278af 11084 {
48bcea9f 11085 i386_addressing_mode ();
e05278af 11086 override = (i.prefix[ADDR_PREFIX] != 0);
40fb9820
L
11087 if (flag_code == CODE_64BIT)
11088 {
a775efc8 11089 bigdisp.bitfield.disp32 = 1;
40fb9820 11090 if (!override)
a775efc8 11091 bigdisp.bitfield.disp64 = 1;
40fb9820
L
11092 }
11093 else if ((flag_code == CODE_16BIT) ^ override)
40fb9820 11094 bigdisp.bitfield.disp16 = 1;
48bcea9f
JB
11095 else
11096 bigdisp.bitfield.disp32 = 1;
e05278af
JB
11097 }
11098 else
11099 {
376cd056
JB
11100 /* For PC-relative branches, the width of the displacement may be
11101 dependent upon data size, but is never dependent upon address size.
11102 Also make sure to not unintentionally match against a non-PC-relative
11103 branch template. */
11104 static templates aux_templates;
11105 const insn_template *t = current_templates->start;
5b7c81bd 11106 bool has_intel64 = false;
376cd056
JB
11107
11108 aux_templates.start = t;
11109 while (++t < current_templates->end)
11110 {
11111 if (t->opcode_modifier.jump
11112 != current_templates->start->opcode_modifier.jump)
11113 break;
4b5aaf5f 11114 if ((t->opcode_modifier.isa64 >= INTEL64))
5b7c81bd 11115 has_intel64 = true;
376cd056
JB
11116 }
11117 if (t < current_templates->end)
11118 {
11119 aux_templates.end = t;
11120 current_templates = &aux_templates;
11121 }
11122
e05278af 11123 override = (i.prefix[DATA_PREFIX] != 0);
40fb9820
L
11124 if (flag_code == CODE_64BIT)
11125 {
376cd056
JB
11126 if ((override || i.suffix == WORD_MNEM_SUFFIX)
11127 && (!intel64 || !has_intel64))
40fb9820
L
11128 bigdisp.bitfield.disp16 = 1;
11129 else
a775efc8 11130 bigdisp.bitfield.disp32 = 1;
40fb9820
L
11131 }
11132 else
e05278af
JB
11133 {
11134 if (!override)
11135 override = (i.suffix == (flag_code != CODE_16BIT
11136 ? WORD_MNEM_SUFFIX
11137 : LONG_MNEM_SUFFIX));
40fb9820
L
11138 bigdisp.bitfield.disp32 = 1;
11139 if ((flag_code == CODE_16BIT) ^ override)
11140 {
11141 bigdisp.bitfield.disp32 = 0;
11142 bigdisp.bitfield.disp16 = 1;
11143 }
e05278af 11144 }
e05278af 11145 }
c6fb90c8
L
11146 i.types[this_operand] = operand_type_or (i.types[this_operand],
11147 bigdisp);
252b5132
RH
11148
11149 exp = &disp_expressions[i.disp_operands];
520dc8e8 11150 i.op[this_operand].disps = exp;
252b5132
RH
11151 i.disp_operands++;
11152 save_input_line_pointer = input_line_pointer;
11153 input_line_pointer = disp_start;
11154 END_STRING_AND_SAVE (disp_end);
11155
11156#ifndef GCC_ASM_O_HACK
11157#define GCC_ASM_O_HACK 0
11158#endif
11159#if GCC_ASM_O_HACK
11160 END_STRING_AND_SAVE (disp_end + 1);
40fb9820 11161 if (i.types[this_operand].bitfield.baseIndex
24eab124 11162 && displacement_string_end[-1] == '+')
252b5132
RH
11163 {
11164 /* This hack is to avoid a warning when using the "o"
24eab124
AM
11165 constraint within gcc asm statements.
11166 For instance:
11167
11168 #define _set_tssldt_desc(n,addr,limit,type) \
11169 __asm__ __volatile__ ( \
11170 "movw %w2,%0\n\t" \
11171 "movw %w1,2+%0\n\t" \
11172 "rorl $16,%1\n\t" \
11173 "movb %b1,4+%0\n\t" \
11174 "movb %4,5+%0\n\t" \
11175 "movb $0,6+%0\n\t" \
11176 "movb %h1,7+%0\n\t" \
11177 "rorl $16,%1" \
11178 : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
11179
11180 This works great except that the output assembler ends
11181 up looking a bit weird if it turns out that there is
11182 no offset. You end up producing code that looks like:
11183
11184 #APP
11185 movw $235,(%eax)
11186 movw %dx,2+(%eax)
11187 rorl $16,%edx
11188 movb %dl,4+(%eax)
11189 movb $137,5+(%eax)
11190 movb $0,6+(%eax)
11191 movb %dh,7+(%eax)
11192 rorl $16,%edx
11193 #NO_APP
11194
47926f60 11195 So here we provide the missing zero. */
24eab124
AM
11196
11197 *displacement_string_end = '0';
252b5132
RH
11198 }
11199#endif
d258b828 11200 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
11201 if (gotfree_input_line)
11202 input_line_pointer = gotfree_input_line;
252b5132 11203
24eab124 11204 exp_seg = expression (exp);
252b5132 11205
636c26b0
AM
11206 SKIP_WHITESPACE ();
11207 if (*input_line_pointer)
11208 as_bad (_("junk `%s' after expression"), input_line_pointer);
11209#if GCC_ASM_O_HACK
11210 RESTORE_END_STRING (disp_end + 1);
11211#endif
636c26b0 11212 input_line_pointer = save_input_line_pointer;
636c26b0 11213 if (gotfree_input_line)
ee86248c
JB
11214 {
11215 free (gotfree_input_line);
11216
11217 if (exp->X_op == O_constant || exp->X_op == O_register)
11218 exp->X_op = O_illegal;
11219 }
11220
11221 ret = i386_finalize_displacement (exp_seg, exp, types, disp_start);
11222
11223 RESTORE_END_STRING (disp_end);
11224
11225 return ret;
11226}
11227
11228static int
11229i386_finalize_displacement (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
11230 i386_operand_type types, const char *disp_start)
11231{
ee86248c 11232 int ret = 1;
636c26b0 11233
24eab124
AM
11234 /* We do this to make sure that the section symbol is in
11235 the symbol table. We will ultimately change the relocation
47926f60 11236 to be relative to the beginning of the section. */
1ae12ab7 11237 if (i.reloc[this_operand] == BFD_RELOC_386_GOTOFF
d6ab8113
JB
11238 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
11239 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
24eab124 11240 {
636c26b0 11241 if (exp->X_op != O_symbol)
3992d3b7 11242 goto inv_disp;
636c26b0 11243
e5cb08ac 11244 if (S_IS_LOCAL (exp->X_add_symbol)
c64efb4b
L
11245 && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section
11246 && S_GET_SEGMENT (exp->X_add_symbol) != expr_section)
24eab124 11247 section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
24eab124
AM
11248 exp->X_op = O_subtract;
11249 exp->X_op_symbol = GOT_symbol;
1ae12ab7 11250 if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
29b0f896 11251 i.reloc[this_operand] = BFD_RELOC_32_PCREL;
d6ab8113
JB
11252 else if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
11253 i.reloc[this_operand] = BFD_RELOC_64;
23df1078 11254 else
29b0f896 11255 i.reloc[this_operand] = BFD_RELOC_32;
24eab124 11256 }
252b5132 11257
3992d3b7
AM
11258 else if (exp->X_op == O_absent
11259 || exp->X_op == O_illegal
ee86248c 11260 || exp->X_op == O_big)
2daf4fd8 11261 {
3992d3b7
AM
11262 inv_disp:
11263 as_bad (_("missing or invalid displacement expression `%s'"),
2daf4fd8 11264 disp_start);
3992d3b7 11265 ret = 0;
2daf4fd8
AM
11266 }
11267
a50187b2
JB
11268 else if (exp->X_op == O_constant)
11269 {
11270 /* Sizing gets taken care of by optimize_disp().
11271
11272 If not 64bit, sign/zero extend val, to account for wraparound
11273 when !BFD64. */
11274 if (flag_code != CODE_64BIT)
11275 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
11276 }
11277
4c63da97 11278#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
a50187b2 11279 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
3992d3b7
AM
11280 && exp_seg != absolute_section
11281 && exp_seg != text_section
11282 && exp_seg != data_section
11283 && exp_seg != bss_section
11284 && exp_seg != undefined_section
11285 && !bfd_is_com_section (exp_seg))
24eab124 11286 {
d0b47220 11287 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
3992d3b7 11288 ret = 0;
24eab124 11289 }
252b5132 11290#endif
3956db08 11291
a50187b2 11292 else if (current_templates->start->opcode_modifier.jump == JUMP_BYTE)
48bcea9f
JB
11293 i.types[this_operand].bitfield.disp8 = 1;
11294
40fb9820 11295 /* Check if this is a displacement only operand. */
02b83698
JB
11296 if (!i.types[this_operand].bitfield.baseindex)
11297 i.types[this_operand] =
11298 operand_type_or (operand_type_and_not (i.types[this_operand], anydisp),
11299 operand_type_and (i.types[this_operand], types));
3956db08 11300
3992d3b7 11301 return ret;
252b5132
RH
11302}
11303
2abc2bec
JB
11304/* Return the active addressing mode, taking address override and
11305 registers forming the address into consideration. Update the
11306 address override prefix if necessary. */
47926f60 11307
2abc2bec
JB
11308static enum flag_code
11309i386_addressing_mode (void)
252b5132 11310{
be05d201
L
11311 enum flag_code addr_mode;
11312
11313 if (i.prefix[ADDR_PREFIX])
11314 addr_mode = flag_code == CODE_32BIT ? CODE_16BIT : CODE_32BIT;
a23b33b3
JB
11315 else if (flag_code == CODE_16BIT
11316 && current_templates->start->cpu_flags.bitfield.cpumpx
11317 /* Avoid replacing the "16-bit addressing not allowed" diagnostic
11318 from md_assemble() by "is not a valid base/index expression"
11319 when there is a base and/or index. */
11320 && !i.types[this_operand].bitfield.baseindex)
11321 {
11322 /* MPX insn memory operands with neither base nor index must be forced
11323 to use 32-bit addressing in 16-bit mode. */
11324 addr_mode = CODE_32BIT;
11325 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
11326 ++i.prefixes;
11327 gas_assert (!i.types[this_operand].bitfield.disp16);
11328 gas_assert (!i.types[this_operand].bitfield.disp32);
11329 }
be05d201
L
11330 else
11331 {
11332 addr_mode = flag_code;
11333
24eab124 11334#if INFER_ADDR_PREFIX
be05d201
L
11335 if (i.mem_operands == 0)
11336 {
11337 /* Infer address prefix from the first memory operand. */
11338 const reg_entry *addr_reg = i.base_reg;
11339
11340 if (addr_reg == NULL)
11341 addr_reg = i.index_reg;
eecb386c 11342
be05d201
L
11343 if (addr_reg)
11344 {
e968fc9b 11345 if (addr_reg->reg_type.bitfield.dword)
be05d201
L
11346 addr_mode = CODE_32BIT;
11347 else if (flag_code != CODE_64BIT
dc821c5f 11348 && addr_reg->reg_type.bitfield.word)
be05d201
L
11349 addr_mode = CODE_16BIT;
11350
11351 if (addr_mode != flag_code)
11352 {
11353 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
11354 i.prefixes += 1;
11355 /* Change the size of any displacement too. At most one
11356 of Disp16 or Disp32 is set.
11357 FIXME. There doesn't seem to be any real need for
11358 separate Disp16 and Disp32 flags. The same goes for
11359 Imm16 and Imm32. Removing them would probably clean
11360 up the code quite a lot. */
11361 if (flag_code != CODE_64BIT
11362 && (i.types[this_operand].bitfield.disp16
11363 || i.types[this_operand].bitfield.disp32))
05909f23
JB
11364 {
11365 static const i386_operand_type disp16_32 = {
11366 .bitfield = { .disp16 = 1, .disp32 = 1 }
11367 };
11368
11369 i.types[this_operand]
11370 = operand_type_xor (i.types[this_operand], disp16_32);
11371 }
be05d201
L
11372 }
11373 }
11374 }
24eab124 11375#endif
be05d201
L
11376 }
11377
2abc2bec
JB
11378 return addr_mode;
11379}
11380
11381/* Make sure the memory operand we've been dealt is valid.
11382 Return 1 on success, 0 on a failure. */
11383
11384static int
11385i386_index_check (const char *operand_string)
11386{
11387 const char *kind = "base/index";
11388 enum flag_code addr_mode = i386_addressing_mode ();
77a19f0e 11389 const insn_template *t = current_templates->end - 1;
2abc2bec 11390
77a19f0e 11391 if (t->opcode_modifier.isstring)
fc0763e6
JB
11392 {
11393 /* Memory operands of string insns are special in that they only allow
11394 a single register (rDI, rSI, or rBX) as their memory address. */
be05d201
L
11395 const reg_entry *expected_reg;
11396 static const char *di_si[][2] =
11397 {
11398 { "esi", "edi" },
11399 { "si", "di" },
11400 { "rsi", "rdi" }
11401 };
11402 static const char *bx[] = { "ebx", "bx", "rbx" };
fc0763e6
JB
11403
11404 kind = "string address";
11405
a152332d 11406 if (t->opcode_modifier.prefixok == PrefixRep)
fc0763e6 11407 {
77a19f0e 11408 int es_op = t->opcode_modifier.isstring - IS_STRING_ES_OP0;
51c8edf6 11409 int op = 0;
fc0763e6 11410
77a19f0e 11411 if (!t->operand_types[0].bitfield.baseindex
fc0763e6 11412 || ((!i.mem_operands != !intel_syntax)
77a19f0e 11413 && t->operand_types[1].bitfield.baseindex))
51c8edf6 11414 op = 1;
fe0e921f
AM
11415 expected_reg
11416 = (const reg_entry *) str_hash_find (reg_hash,
11417 di_si[addr_mode][op == es_op]);
fc0763e6
JB
11418 }
11419 else
fe0e921f
AM
11420 expected_reg
11421 = (const reg_entry *)str_hash_find (reg_hash, bx[addr_mode]);
fc0763e6 11422
be05d201
L
11423 if (i.base_reg != expected_reg
11424 || i.index_reg
fc0763e6 11425 || operand_type_check (i.types[this_operand], disp))
fc0763e6 11426 {
be05d201
L
11427 /* The second memory operand must have the same size as
11428 the first one. */
11429 if (i.mem_operands
11430 && i.base_reg
11431 && !((addr_mode == CODE_64BIT
dc821c5f 11432 && i.base_reg->reg_type.bitfield.qword)
be05d201 11433 || (addr_mode == CODE_32BIT
dc821c5f
JB
11434 ? i.base_reg->reg_type.bitfield.dword
11435 : i.base_reg->reg_type.bitfield.word)))
be05d201
L
11436 goto bad_address;
11437
fc0763e6
JB
11438 as_warn (_("`%s' is not valid here (expected `%c%s%s%c')"),
11439 operand_string,
11440 intel_syntax ? '[' : '(',
11441 register_prefix,
be05d201 11442 expected_reg->reg_name,
fc0763e6 11443 intel_syntax ? ']' : ')');
be05d201 11444 return 1;
fc0763e6 11445 }
be05d201
L
11446 else
11447 return 1;
11448
dc1e8a47 11449 bad_address:
be05d201
L
11450 as_bad (_("`%s' is not a valid %s expression"),
11451 operand_string, kind);
11452 return 0;
3e73aa7c
JH
11453 }
11454 else
11455 {
77a19f0e
JB
11456 t = current_templates->start;
11457
be05d201
L
11458 if (addr_mode != CODE_16BIT)
11459 {
11460 /* 32-bit/64-bit checks. */
41eb8e88
L
11461 if (i.disp_encoding == disp_encoding_16bit)
11462 {
11463 bad_disp:
11464 as_bad (_("invalid `%s' prefix"),
11465 addr_mode == CODE_16BIT ? "{disp32}" : "{disp16}");
11466 return 0;
11467 }
11468
be05d201 11469 if ((i.base_reg
e968fc9b
JB
11470 && ((addr_mode == CODE_64BIT
11471 ? !i.base_reg->reg_type.bitfield.qword
11472 : !i.base_reg->reg_type.bitfield.dword)
11473 || (i.index_reg && i.base_reg->reg_num == RegIP)
11474 || i.base_reg->reg_num == RegIZ))
be05d201 11475 || (i.index_reg
1b54b8d7
JB
11476 && !i.index_reg->reg_type.bitfield.xmmword
11477 && !i.index_reg->reg_type.bitfield.ymmword
11478 && !i.index_reg->reg_type.bitfield.zmmword
be05d201 11479 && ((addr_mode == CODE_64BIT
e968fc9b
JB
11480 ? !i.index_reg->reg_type.bitfield.qword
11481 : !i.index_reg->reg_type.bitfield.dword)
be05d201
L
11482 || !i.index_reg->reg_type.bitfield.baseindex)))
11483 goto bad_address;
8178be5b 11484
260cd341 11485 /* bndmk, bndldx, bndstx and mandatory non-vector SIB have special restrictions. */
a152332d 11486 if ((t->opcode_modifier.opcodeprefix == PREFIX_0XF3
389d00a5
JB
11487 && t->opcode_modifier.opcodespace == SPACE_0F
11488 && t->base_opcode == 0x1b)
a152332d 11489 || (t->opcode_modifier.opcodeprefix == PREFIX_NONE
389d00a5
JB
11490 && t->opcode_modifier.opcodespace == SPACE_0F
11491 && (t->base_opcode & ~1) == 0x1a)
a152332d 11492 || t->opcode_modifier.sib == SIBMEM)
8178be5b
JB
11493 {
11494 /* They cannot use RIP-relative addressing. */
e968fc9b 11495 if (i.base_reg && i.base_reg->reg_num == RegIP)
8178be5b
JB
11496 {
11497 as_bad (_("`%s' cannot be used here"), operand_string);
11498 return 0;
11499 }
11500
11501 /* bndldx and bndstx ignore their scale factor. */
a152332d 11502 if (t->opcode_modifier.opcodeprefix == PREFIX_NONE
389d00a5
JB
11503 && t->opcode_modifier.opcodespace == SPACE_0F
11504 && (t->base_opcode & ~1) == 0x1a
8178be5b
JB
11505 && i.log2_scale_factor)
11506 as_warn (_("register scaling is being ignored here"));
11507 }
be05d201
L
11508 }
11509 else
3e73aa7c 11510 {
be05d201 11511 /* 16-bit checks. */
41eb8e88
L
11512 if (i.disp_encoding == disp_encoding_32bit)
11513 goto bad_disp;
11514
3e73aa7c 11515 if ((i.base_reg
dc821c5f 11516 && (!i.base_reg->reg_type.bitfield.word
40fb9820 11517 || !i.base_reg->reg_type.bitfield.baseindex))
3e73aa7c 11518 || (i.index_reg
dc821c5f 11519 && (!i.index_reg->reg_type.bitfield.word
40fb9820 11520 || !i.index_reg->reg_type.bitfield.baseindex
29b0f896
AM
11521 || !(i.base_reg
11522 && i.base_reg->reg_num < 6
11523 && i.index_reg->reg_num >= 6
11524 && i.log2_scale_factor == 0))))
be05d201 11525 goto bad_address;
3e73aa7c
JH
11526 }
11527 }
be05d201 11528 return 1;
24eab124 11529}
252b5132 11530
43234a1e
L
11531/* Handle vector immediates. */
11532
11533static int
11534RC_SAE_immediate (const char *imm_start)
11535{
43234a1e 11536 const char *pstr = imm_start;
43234a1e
L
11537
11538 if (*pstr != '{')
11539 return 0;
11540
7063667e
JB
11541 pstr = RC_SAE_specifier (pstr + 1);
11542 if (pstr == NULL)
43234a1e
L
11543 return 0;
11544
11545 if (*pstr++ != '}')
11546 {
11547 as_bad (_("Missing '}': '%s'"), imm_start);
11548 return 0;
11549 }
11550 /* RC/SAE immediate string should contain nothing more. */;
11551 if (*pstr != 0)
11552 {
11553 as_bad (_("Junk after '}': '%s'"), imm_start);
11554 return 0;
11555 }
11556
cf665fee
JB
11557 /* Internally this doesn't count as an operand. */
11558 --i.operands;
43234a1e 11559
43234a1e
L
11560 return 1;
11561}
11562
9d299bea
JB
11563static INLINE bool starts_memory_operand (char c)
11564{
014fbcda 11565 return ISDIGIT (c)
9d299bea 11566 || is_identifier_char (c)
014fbcda 11567 || strchr ("([\"+-!~", c);
9d299bea
JB
11568}
11569
fc0763e6 11570/* Parse OPERAND_STRING into the i386_insn structure I. Returns zero
47926f60 11571 on error. */
252b5132 11572
252b5132 11573static int
a7619375 11574i386_att_operand (char *operand_string)
252b5132 11575{
af6bdddf
AM
11576 const reg_entry *r;
11577 char *end_op;
24eab124 11578 char *op_string = operand_string;
252b5132 11579
24eab124 11580 if (is_space_char (*op_string))
252b5132
RH
11581 ++op_string;
11582
24eab124 11583 /* We check for an absolute prefix (differentiating,
47926f60 11584 for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
24eab124
AM
11585 if (*op_string == ABSOLUTE_PREFIX)
11586 {
11587 ++op_string;
11588 if (is_space_char (*op_string))
11589 ++op_string;
5b7c81bd 11590 i.jumpabsolute = true;
24eab124 11591 }
252b5132 11592
47926f60 11593 /* Check if operand is a register. */
4d1bb795 11594 if ((r = parse_register (op_string, &end_op)) != NULL)
24eab124 11595 {
40fb9820
L
11596 i386_operand_type temp;
11597
8a6fb3f9
JB
11598 if (r == &bad_reg)
11599 return 0;
11600
24eab124
AM
11601 /* Check for a segment override by searching for ':' after a
11602 segment register. */
11603 op_string = end_op;
11604 if (is_space_char (*op_string))
11605 ++op_string;
00cee14f 11606 if (*op_string == ':' && r->reg_type.bitfield.class == SReg)
24eab124 11607 {
5e042380 11608 i.seg[i.mem_operands] = r;
252b5132 11609
24eab124 11610 /* Skip the ':' and whitespace. */
252b5132
RH
11611 ++op_string;
11612 if (is_space_char (*op_string))
24eab124 11613 ++op_string;
252b5132 11614
47926f60 11615 /* Handle case of %es:*foo. */
c8d541e2 11616 if (!i.jumpabsolute && *op_string == ABSOLUTE_PREFIX)
24eab124
AM
11617 {
11618 ++op_string;
11619 if (is_space_char (*op_string))
11620 ++op_string;
5b7c81bd 11621 i.jumpabsolute = true;
24eab124 11622 }
c8d541e2 11623
9d299bea 11624 if (!starts_memory_operand (*op_string))
c8d541e2
JB
11625 {
11626 as_bad (_("bad memory operand `%s'"), op_string);
11627 return 0;
11628 }
24eab124
AM
11629 goto do_memory_reference;
11630 }
43234a1e
L
11631
11632 /* Handle vector operations. */
11633 if (*op_string == '{')
11634 {
f70c6814 11635 op_string = check_VecOperations (op_string);
43234a1e
L
11636 if (op_string == NULL)
11637 return 0;
11638 }
11639
24eab124
AM
11640 if (*op_string)
11641 {
d0b47220 11642 as_bad (_("junk `%s' after register"), op_string);
24eab124
AM
11643 return 0;
11644 }
40fb9820
L
11645 temp = r->reg_type;
11646 temp.bitfield.baseindex = 0;
c6fb90c8
L
11647 i.types[this_operand] = operand_type_or (i.types[this_operand],
11648 temp);
7d5e4556 11649 i.types[this_operand].bitfield.unspecified = 0;
520dc8e8 11650 i.op[this_operand].regs = r;
24eab124 11651 i.reg_operands++;
cf665fee
JB
11652
11653 /* A GPR may follow an RC or SAE immediate only if a (vector) register
11654 operand was also present earlier on. */
11655 if (i.rounding.type != rc_none && temp.bitfield.class == Reg
11656 && i.reg_operands == 1)
11657 {
11658 unsigned int j;
11659
11660 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); ++j)
11661 if (i.rounding.type == RC_NamesTable[j].type)
11662 break;
11663 as_bad (_("`%s': misplaced `{%s}'"),
11664 current_templates->start->name, RC_NamesTable[j].name);
11665 return 0;
11666 }
24eab124 11667 }
af6bdddf
AM
11668 else if (*op_string == REGISTER_PREFIX)
11669 {
11670 as_bad (_("bad register name `%s'"), op_string);
11671 return 0;
11672 }
24eab124 11673 else if (*op_string == IMMEDIATE_PREFIX)
ce8a8b2f 11674 {
24eab124 11675 ++op_string;
6f2f06be 11676 if (i.jumpabsolute)
24eab124 11677 {
d0b47220 11678 as_bad (_("immediate operand illegal with absolute jump"));
24eab124
AM
11679 return 0;
11680 }
11681 if (!i386_immediate (op_string))
11682 return 0;
cf665fee
JB
11683 if (i.rounding.type != rc_none)
11684 {
11685 as_bad (_("`%s': RC/SAE operand must follow immediate operands"),
11686 current_templates->start->name);
11687 return 0;
11688 }
24eab124 11689 }
43234a1e
L
11690 else if (RC_SAE_immediate (operand_string))
11691 {
cf665fee
JB
11692 /* If it is a RC or SAE immediate, do the necessary placement check:
11693 Only another immediate or a GPR may precede it. */
11694 if (i.mem_operands || i.reg_operands + i.imm_operands > 1
11695 || (i.reg_operands == 1
11696 && i.op[0].regs->reg_type.bitfield.class != Reg))
11697 {
11698 as_bad (_("`%s': misplaced `%s'"),
11699 current_templates->start->name, operand_string);
11700 return 0;
11701 }
43234a1e 11702 }
9d299bea 11703 else if (starts_memory_operand (*op_string))
24eab124 11704 {
47926f60 11705 /* This is a memory reference of some sort. */
af6bdddf 11706 char *base_string;
252b5132 11707
47926f60 11708 /* Start and end of displacement string expression (if found). */
eecb386c
AM
11709 char *displacement_string_start;
11710 char *displacement_string_end;
252b5132 11711
24eab124 11712 do_memory_reference:
24eab124
AM
11713 /* Check for base index form. We detect the base index form by
11714 looking for an ')' at the end of the operand, searching
11715 for the '(' matching it, and finding a REGISTER_PREFIX or ','
11716 after the '('. */
af6bdddf 11717 base_string = op_string + strlen (op_string);
c3332e24 11718
43234a1e 11719 /* Handle vector operations. */
6b5ba0d4
JB
11720 --base_string;
11721 if (is_space_char (*base_string))
11722 --base_string;
11723
11724 if (*base_string == '}')
43234a1e 11725 {
6b5ba0d4
JB
11726 char *vop_start = NULL;
11727
11728 while (base_string-- > op_string)
11729 {
11730 if (*base_string == '"')
11731 break;
11732 if (*base_string != '{')
11733 continue;
11734
11735 vop_start = base_string;
11736
11737 --base_string;
11738 if (is_space_char (*base_string))
11739 --base_string;
11740
11741 if (*base_string != '}')
11742 break;
11743
11744 vop_start = NULL;
11745 }
11746
11747 if (!vop_start)
11748 {
11749 as_bad (_("unbalanced figure braces"));
11750 return 0;
11751 }
11752
f70c6814 11753 if (check_VecOperations (vop_start) == NULL)
43234a1e 11754 return 0;
43234a1e
L
11755 }
11756
47926f60 11757 /* If we only have a displacement, set-up for it to be parsed later. */
af6bdddf
AM
11758 displacement_string_start = op_string;
11759 displacement_string_end = base_string + 1;
252b5132 11760
24eab124
AM
11761 if (*base_string == ')')
11762 {
af6bdddf 11763 char *temp_string;
e87fb6a6
JB
11764 unsigned int parens_not_balanced = 0;
11765 bool in_quotes = false;
e68c3d59 11766
24eab124 11767 /* We've already checked that the number of left & right ()'s are
e87fb6a6
JB
11768 equal, and that there's a matching set of double quotes. */
11769 end_op = base_string;
11770 for (temp_string = op_string; temp_string < end_op; temp_string++)
24eab124 11771 {
e87fb6a6
JB
11772 if (*temp_string == '\\' && temp_string[1] == '"')
11773 ++temp_string;
11774 else if (*temp_string == '"')
11775 in_quotes = !in_quotes;
11776 else if (!in_quotes)
11777 {
11778 if (*temp_string == '(' && !parens_not_balanced++)
11779 base_string = temp_string;
11780 if (*temp_string == ')')
11781 --parens_not_balanced;
11782 }
24eab124 11783 }
c3332e24 11784
af6bdddf 11785 temp_string = base_string;
c3332e24 11786
24eab124 11787 /* Skip past '(' and whitespace. */
e87fb6a6
JB
11788 gas_assert (*base_string == '(');
11789 ++base_string;
252b5132 11790 if (is_space_char (*base_string))
24eab124 11791 ++base_string;
252b5132 11792
af6bdddf 11793 if (*base_string == ','
4eed87de
AM
11794 || ((i.base_reg = parse_register (base_string, &end_op))
11795 != NULL))
252b5132 11796 {
af6bdddf 11797 displacement_string_end = temp_string;
252b5132 11798
40fb9820 11799 i.types[this_operand].bitfield.baseindex = 1;
252b5132 11800
af6bdddf 11801 if (i.base_reg)
24eab124 11802 {
8a6fb3f9
JB
11803 if (i.base_reg == &bad_reg)
11804 return 0;
24eab124
AM
11805 base_string = end_op;
11806 if (is_space_char (*base_string))
11807 ++base_string;
af6bdddf
AM
11808 }
11809
11810 /* There may be an index reg or scale factor here. */
11811 if (*base_string == ',')
11812 {
11813 ++base_string;
11814 if (is_space_char (*base_string))
11815 ++base_string;
11816
4eed87de
AM
11817 if ((i.index_reg = parse_register (base_string, &end_op))
11818 != NULL)
24eab124 11819 {
8a6fb3f9
JB
11820 if (i.index_reg == &bad_reg)
11821 return 0;
af6bdddf 11822 base_string = end_op;
24eab124
AM
11823 if (is_space_char (*base_string))
11824 ++base_string;
af6bdddf
AM
11825 if (*base_string == ',')
11826 {
11827 ++base_string;
11828 if (is_space_char (*base_string))
11829 ++base_string;
11830 }
e5cb08ac 11831 else if (*base_string != ')')
af6bdddf 11832 {
4eed87de
AM
11833 as_bad (_("expecting `,' or `)' "
11834 "after index register in `%s'"),
af6bdddf
AM
11835 operand_string);
11836 return 0;
11837 }
24eab124 11838 }
af6bdddf 11839 else if (*base_string == REGISTER_PREFIX)
24eab124 11840 {
f76bf5e0
L
11841 end_op = strchr (base_string, ',');
11842 if (end_op)
11843 *end_op = '\0';
af6bdddf 11844 as_bad (_("bad register name `%s'"), base_string);
24eab124
AM
11845 return 0;
11846 }
252b5132 11847
47926f60 11848 /* Check for scale factor. */
551c1ca1 11849 if (*base_string != ')')
af6bdddf 11850 {
551c1ca1
AM
11851 char *end_scale = i386_scale (base_string);
11852
11853 if (!end_scale)
af6bdddf 11854 return 0;
24eab124 11855
551c1ca1 11856 base_string = end_scale;
af6bdddf
AM
11857 if (is_space_char (*base_string))
11858 ++base_string;
11859 if (*base_string != ')')
11860 {
4eed87de
AM
11861 as_bad (_("expecting `)' "
11862 "after scale factor in `%s'"),
af6bdddf
AM
11863 operand_string);
11864 return 0;
11865 }
11866 }
11867 else if (!i.index_reg)
24eab124 11868 {
4eed87de
AM
11869 as_bad (_("expecting index register or scale factor "
11870 "after `,'; got '%c'"),
af6bdddf 11871 *base_string);
24eab124
AM
11872 return 0;
11873 }
11874 }
af6bdddf 11875 else if (*base_string != ')')
24eab124 11876 {
4eed87de
AM
11877 as_bad (_("expecting `,' or `)' "
11878 "after base register in `%s'"),
af6bdddf 11879 operand_string);
24eab124
AM
11880 return 0;
11881 }
c3332e24 11882 }
af6bdddf 11883 else if (*base_string == REGISTER_PREFIX)
c3332e24 11884 {
f76bf5e0
L
11885 end_op = strchr (base_string, ',');
11886 if (end_op)
11887 *end_op = '\0';
af6bdddf 11888 as_bad (_("bad register name `%s'"), base_string);
24eab124 11889 return 0;
c3332e24 11890 }
24eab124
AM
11891 }
11892
11893 /* If there's an expression beginning the operand, parse it,
11894 assuming displacement_string_start and
11895 displacement_string_end are meaningful. */
11896 if (displacement_string_start != displacement_string_end)
11897 {
11898 if (!i386_displacement (displacement_string_start,
11899 displacement_string_end))
11900 return 0;
11901 }
11902
11903 /* Special case for (%dx) while doing input/output op. */
11904 if (i.base_reg
75e5731b
JB
11905 && i.base_reg->reg_type.bitfield.instance == RegD
11906 && i.base_reg->reg_type.bitfield.word
24eab124
AM
11907 && i.index_reg == 0
11908 && i.log2_scale_factor == 0
11909 && i.seg[i.mem_operands] == 0
40fb9820 11910 && !operand_type_check (i.types[this_operand], disp))
24eab124 11911 {
2fb5be8d 11912 i.types[this_operand] = i.base_reg->reg_type;
9373f275 11913 i.input_output_operand = true;
24eab124
AM
11914 return 1;
11915 }
11916
eecb386c
AM
11917 if (i386_index_check (operand_string) == 0)
11918 return 0;
c48dadc9 11919 i.flags[this_operand] |= Operand_Mem;
24eab124
AM
11920 i.mem_operands++;
11921 }
11922 else
ce8a8b2f
AM
11923 {
11924 /* It's not a memory operand; argh! */
24eab124
AM
11925 as_bad (_("invalid char %s beginning operand %d `%s'"),
11926 output_invalid (*op_string),
11927 this_operand + 1,
11928 op_string);
11929 return 0;
11930 }
47926f60 11931 return 1; /* Normal return. */
252b5132
RH
11932}
11933\f
fa94de6b
RM
11934/* Calculate the maximum variable size (i.e., excluding fr_fix)
11935 that an rs_machine_dependent frag may reach. */
11936
11937unsigned int
11938i386_frag_max_var (fragS *frag)
11939{
11940 /* The only relaxable frags are for jumps.
11941 Unconditional jumps can grow by 4 bytes and others by 5 bytes. */
11942 gas_assert (frag->fr_type == rs_machine_dependent);
11943 return TYPE_FROM_RELAX_STATE (frag->fr_subtype) == UNCOND_JUMP ? 4 : 5;
11944}
11945
b084df0b
L
11946#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
11947static int
8dcea932 11948elf_symbol_resolved_in_segment_p (symbolS *fr_symbol, offsetT fr_var)
b084df0b
L
11949{
11950 /* STT_GNU_IFUNC symbol must go through PLT. */
11951 if ((symbol_get_bfdsym (fr_symbol)->flags
11952 & BSF_GNU_INDIRECT_FUNCTION) != 0)
11953 return 0;
11954
11955 if (!S_IS_EXTERNAL (fr_symbol))
11956 /* Symbol may be weak or local. */
11957 return !S_IS_WEAK (fr_symbol);
11958
8dcea932
L
11959 /* Global symbols with non-default visibility can't be preempted. */
11960 if (ELF_ST_VISIBILITY (S_GET_OTHER (fr_symbol)) != STV_DEFAULT)
11961 return 1;
11962
11963 if (fr_var != NO_RELOC)
11964 switch ((enum bfd_reloc_code_real) fr_var)
11965 {
11966 case BFD_RELOC_386_PLT32:
11967 case BFD_RELOC_X86_64_PLT32:
33eaf5de 11968 /* Symbol with PLT relocation may be preempted. */
8dcea932
L
11969 return 0;
11970 default:
11971 abort ();
11972 }
11973
b084df0b
L
11974 /* Global symbols with default visibility in a shared library may be
11975 preempted by another definition. */
8dcea932 11976 return !shared;
b084df0b
L
11977}
11978#endif
11979
79d72f45
HL
11980/* Table 3-2. Macro-Fusible Instructions in Haswell Microarchitecture
11981 Note also work for Skylake and Cascadelake.
11982---------------------------------------------------------------------
11983| JCC | ADD/SUB/CMP | INC/DEC | TEST/AND |
11984| ------ | ----------- | ------- | -------- |
11985| Jo | N | N | Y |
11986| Jno | N | N | Y |
11987| Jc/Jb | Y | N | Y |
11988| Jae/Jnb | Y | N | Y |
11989| Je/Jz | Y | Y | Y |
11990| Jne/Jnz | Y | Y | Y |
11991| Jna/Jbe | Y | N | Y |
11992| Ja/Jnbe | Y | N | Y |
11993| Js | N | N | Y |
11994| Jns | N | N | Y |
11995| Jp/Jpe | N | N | Y |
11996| Jnp/Jpo | N | N | Y |
11997| Jl/Jnge | Y | Y | Y |
11998| Jge/Jnl | Y | Y | Y |
11999| Jle/Jng | Y | Y | Y |
12000| Jg/Jnle | Y | Y | Y |
12001--------------------------------------------------------------------- */
12002static int
12003i386_macro_fusible_p (enum mf_cmp_kind mf_cmp, enum mf_jcc_kind mf_jcc)
12004{
12005 if (mf_cmp == mf_cmp_alu_cmp)
12006 return ((mf_jcc >= mf_jcc_jc && mf_jcc <= mf_jcc_jna)
12007 || mf_jcc == mf_jcc_jl || mf_jcc == mf_jcc_jle);
12008 if (mf_cmp == mf_cmp_incdec)
12009 return (mf_jcc == mf_jcc_je || mf_jcc == mf_jcc_jl
12010 || mf_jcc == mf_jcc_jle);
12011 if (mf_cmp == mf_cmp_test_and)
12012 return 1;
12013 return 0;
12014}
12015
e379e5f3
L
12016/* Return the next non-empty frag. */
12017
12018static fragS *
12019i386_next_non_empty_frag (fragS *fragP)
12020{
12021 /* There may be a frag with a ".fill 0" when there is no room in
12022 the current frag for frag_grow in output_insn. */
12023 for (fragP = fragP->fr_next;
12024 (fragP != NULL
12025 && fragP->fr_type == rs_fill
12026 && fragP->fr_fix == 0);
12027 fragP = fragP->fr_next)
12028 ;
12029 return fragP;
12030}
12031
12032/* Return the next jcc frag after BRANCH_PADDING. */
12033
12034static fragS *
79d72f45 12035i386_next_fusible_jcc_frag (fragS *maybe_cmp_fragP, fragS *pad_fragP)
e379e5f3 12036{
79d72f45
HL
12037 fragS *branch_fragP;
12038 if (!pad_fragP)
e379e5f3
L
12039 return NULL;
12040
79d72f45
HL
12041 if (pad_fragP->fr_type == rs_machine_dependent
12042 && (TYPE_FROM_RELAX_STATE (pad_fragP->fr_subtype)
e379e5f3
L
12043 == BRANCH_PADDING))
12044 {
79d72f45
HL
12045 branch_fragP = i386_next_non_empty_frag (pad_fragP);
12046 if (branch_fragP->fr_type != rs_machine_dependent)
e379e5f3 12047 return NULL;
79d72f45
HL
12048 if (TYPE_FROM_RELAX_STATE (branch_fragP->fr_subtype) == COND_JUMP
12049 && i386_macro_fusible_p (maybe_cmp_fragP->tc_frag_data.mf_type,
12050 pad_fragP->tc_frag_data.mf_type))
12051 return branch_fragP;
e379e5f3
L
12052 }
12053
12054 return NULL;
12055}
12056
12057/* Classify BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags. */
12058
12059static void
12060i386_classify_machine_dependent_frag (fragS *fragP)
12061{
12062 fragS *cmp_fragP;
12063 fragS *pad_fragP;
12064 fragS *branch_fragP;
12065 fragS *next_fragP;
12066 unsigned int max_prefix_length;
12067
12068 if (fragP->tc_frag_data.classified)
12069 return;
12070
12071 /* First scan for BRANCH_PADDING and FUSED_JCC_PADDING. Convert
12072 FUSED_JCC_PADDING and merge BRANCH_PADDING. */
12073 for (next_fragP = fragP;
12074 next_fragP != NULL;
12075 next_fragP = next_fragP->fr_next)
12076 {
12077 next_fragP->tc_frag_data.classified = 1;
12078 if (next_fragP->fr_type == rs_machine_dependent)
12079 switch (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype))
12080 {
12081 case BRANCH_PADDING:
12082 /* The BRANCH_PADDING frag must be followed by a branch
12083 frag. */
12084 branch_fragP = i386_next_non_empty_frag (next_fragP);
12085 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
12086 break;
12087 case FUSED_JCC_PADDING:
12088 /* Check if this is a fused jcc:
12089 FUSED_JCC_PADDING
12090 CMP like instruction
12091 BRANCH_PADDING
12092 COND_JUMP
12093 */
12094 cmp_fragP = i386_next_non_empty_frag (next_fragP);
12095 pad_fragP = i386_next_non_empty_frag (cmp_fragP);
79d72f45 12096 branch_fragP = i386_next_fusible_jcc_frag (next_fragP, pad_fragP);
e379e5f3
L
12097 if (branch_fragP)
12098 {
12099 /* The BRANCH_PADDING frag is merged with the
12100 FUSED_JCC_PADDING frag. */
12101 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
12102 /* CMP like instruction size. */
12103 next_fragP->tc_frag_data.cmp_size = cmp_fragP->fr_fix;
12104 frag_wane (pad_fragP);
12105 /* Skip to branch_fragP. */
12106 next_fragP = branch_fragP;
12107 }
12108 else if (next_fragP->tc_frag_data.max_prefix_length)
12109 {
12110 /* Turn FUSED_JCC_PADDING into BRANCH_PREFIX if it isn't
12111 a fused jcc. */
12112 next_fragP->fr_subtype
12113 = ENCODE_RELAX_STATE (BRANCH_PREFIX, 0);
12114 next_fragP->tc_frag_data.max_bytes
12115 = next_fragP->tc_frag_data.max_prefix_length;
12116 /* This will be updated in the BRANCH_PREFIX scan. */
12117 next_fragP->tc_frag_data.max_prefix_length = 0;
12118 }
12119 else
12120 frag_wane (next_fragP);
12121 break;
12122 }
12123 }
12124
12125 /* Stop if there is no BRANCH_PREFIX. */
12126 if (!align_branch_prefix_size)
12127 return;
12128
12129 /* Scan for BRANCH_PREFIX. */
12130 for (; fragP != NULL; fragP = fragP->fr_next)
12131 {
12132 if (fragP->fr_type != rs_machine_dependent
12133 || (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
12134 != BRANCH_PREFIX))
12135 continue;
12136
12137 /* Count all BRANCH_PREFIX frags before BRANCH_PADDING and
12138 COND_JUMP_PREFIX. */
12139 max_prefix_length = 0;
12140 for (next_fragP = fragP;
12141 next_fragP != NULL;
12142 next_fragP = next_fragP->fr_next)
12143 {
12144 if (next_fragP->fr_type == rs_fill)
12145 /* Skip rs_fill frags. */
12146 continue;
12147 else if (next_fragP->fr_type != rs_machine_dependent)
12148 /* Stop for all other frags. */
12149 break;
12150
12151 /* rs_machine_dependent frags. */
12152 if (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12153 == BRANCH_PREFIX)
12154 {
12155 /* Count BRANCH_PREFIX frags. */
12156 if (max_prefix_length >= MAX_FUSED_JCC_PADDING_SIZE)
12157 {
12158 max_prefix_length = MAX_FUSED_JCC_PADDING_SIZE;
12159 frag_wane (next_fragP);
12160 }
12161 else
12162 max_prefix_length
12163 += next_fragP->tc_frag_data.max_bytes;
12164 }
12165 else if ((TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12166 == BRANCH_PADDING)
12167 || (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12168 == FUSED_JCC_PADDING))
12169 {
12170 /* Stop at BRANCH_PADDING and FUSED_JCC_PADDING. */
12171 fragP->tc_frag_data.u.padding_fragP = next_fragP;
12172 break;
12173 }
12174 else
12175 /* Stop for other rs_machine_dependent frags. */
12176 break;
12177 }
12178
12179 fragP->tc_frag_data.max_prefix_length = max_prefix_length;
12180
12181 /* Skip to the next frag. */
12182 fragP = next_fragP;
12183 }
12184}
12185
12186/* Compute padding size for
12187
12188 FUSED_JCC_PADDING
12189 CMP like instruction
12190 BRANCH_PADDING
12191 COND_JUMP/UNCOND_JUMP
12192
12193 or
12194
12195 BRANCH_PADDING
12196 COND_JUMP/UNCOND_JUMP
12197 */
12198
12199static int
12200i386_branch_padding_size (fragS *fragP, offsetT address)
12201{
12202 unsigned int offset, size, padding_size;
12203 fragS *branch_fragP = fragP->tc_frag_data.u.branch_fragP;
12204
12205 /* The start address of the BRANCH_PADDING or FUSED_JCC_PADDING frag. */
12206 if (!address)
12207 address = fragP->fr_address;
12208 address += fragP->fr_fix;
12209
12210 /* CMP like instrunction size. */
12211 size = fragP->tc_frag_data.cmp_size;
12212
12213 /* The base size of the branch frag. */
12214 size += branch_fragP->fr_fix;
12215
12216 /* Add opcode and displacement bytes for the rs_machine_dependent
12217 branch frag. */
12218 if (branch_fragP->fr_type == rs_machine_dependent)
12219 size += md_relax_table[branch_fragP->fr_subtype].rlx_length;
12220
12221 /* Check if branch is within boundary and doesn't end at the last
12222 byte. */
12223 offset = address & ((1U << align_branch_power) - 1);
12224 if ((offset + size) >= (1U << align_branch_power))
12225 /* Padding needed to avoid crossing boundary. */
12226 padding_size = (1U << align_branch_power) - offset;
12227 else
12228 /* No padding needed. */
12229 padding_size = 0;
12230
12231 /* The return value may be saved in tc_frag_data.length which is
12232 unsigned byte. */
12233 if (!fits_in_unsigned_byte (padding_size))
12234 abort ();
12235
12236 return padding_size;
12237}
12238
12239/* i386_generic_table_relax_frag()
12240
12241 Handle BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags to
12242 grow/shrink padding to align branch frags. Hand others to
12243 relax_frag(). */
12244
12245long
12246i386_generic_table_relax_frag (segT segment, fragS *fragP, long stretch)
12247{
12248 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12249 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
12250 {
12251 long padding_size = i386_branch_padding_size (fragP, 0);
12252 long grow = padding_size - fragP->tc_frag_data.length;
12253
12254 /* When the BRANCH_PREFIX frag is used, the computed address
12255 must match the actual address and there should be no padding. */
12256 if (fragP->tc_frag_data.padding_address
12257 && (fragP->tc_frag_data.padding_address != fragP->fr_address
12258 || padding_size))
12259 abort ();
12260
12261 /* Update the padding size. */
12262 if (grow)
12263 fragP->tc_frag_data.length = padding_size;
12264
12265 return grow;
12266 }
12267 else if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12268 {
12269 fragS *padding_fragP, *next_fragP;
12270 long padding_size, left_size, last_size;
12271
12272 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
12273 if (!padding_fragP)
12274 /* Use the padding set by the leading BRANCH_PREFIX frag. */
12275 return (fragP->tc_frag_data.length
12276 - fragP->tc_frag_data.last_length);
12277
12278 /* Compute the relative address of the padding frag in the very
12279 first time where the BRANCH_PREFIX frag sizes are zero. */
12280 if (!fragP->tc_frag_data.padding_address)
12281 fragP->tc_frag_data.padding_address
12282 = padding_fragP->fr_address - (fragP->fr_address - stretch);
12283
12284 /* First update the last length from the previous interation. */
12285 left_size = fragP->tc_frag_data.prefix_length;
12286 for (next_fragP = fragP;
12287 next_fragP != padding_fragP;
12288 next_fragP = next_fragP->fr_next)
12289 if (next_fragP->fr_type == rs_machine_dependent
12290 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12291 == BRANCH_PREFIX))
12292 {
12293 if (left_size)
12294 {
12295 int max = next_fragP->tc_frag_data.max_bytes;
12296 if (max)
12297 {
12298 int size;
12299 if (max > left_size)
12300 size = left_size;
12301 else
12302 size = max;
12303 left_size -= size;
12304 next_fragP->tc_frag_data.last_length = size;
12305 }
12306 }
12307 else
12308 next_fragP->tc_frag_data.last_length = 0;
12309 }
12310
12311 /* Check the padding size for the padding frag. */
12312 padding_size = i386_branch_padding_size
12313 (padding_fragP, (fragP->fr_address
12314 + fragP->tc_frag_data.padding_address));
12315
12316 last_size = fragP->tc_frag_data.prefix_length;
12317 /* Check if there is change from the last interation. */
12318 if (padding_size == last_size)
12319 {
12320 /* Update the expected address of the padding frag. */
12321 padding_fragP->tc_frag_data.padding_address
12322 = (fragP->fr_address + padding_size
12323 + fragP->tc_frag_data.padding_address);
12324 return 0;
12325 }
12326
12327 if (padding_size > fragP->tc_frag_data.max_prefix_length)
12328 {
12329 /* No padding if there is no sufficient room. Clear the
12330 expected address of the padding frag. */
12331 padding_fragP->tc_frag_data.padding_address = 0;
12332 padding_size = 0;
12333 }
12334 else
12335 /* Store the expected address of the padding frag. */
12336 padding_fragP->tc_frag_data.padding_address
12337 = (fragP->fr_address + padding_size
12338 + fragP->tc_frag_data.padding_address);
12339
12340 fragP->tc_frag_data.prefix_length = padding_size;
12341
12342 /* Update the length for the current interation. */
12343 left_size = padding_size;
12344 for (next_fragP = fragP;
12345 next_fragP != padding_fragP;
12346 next_fragP = next_fragP->fr_next)
12347 if (next_fragP->fr_type == rs_machine_dependent
12348 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12349 == BRANCH_PREFIX))
12350 {
12351 if (left_size)
12352 {
12353 int max = next_fragP->tc_frag_data.max_bytes;
12354 if (max)
12355 {
12356 int size;
12357 if (max > left_size)
12358 size = left_size;
12359 else
12360 size = max;
12361 left_size -= size;
12362 next_fragP->tc_frag_data.length = size;
12363 }
12364 }
12365 else
12366 next_fragP->tc_frag_data.length = 0;
12367 }
12368
12369 return (fragP->tc_frag_data.length
12370 - fragP->tc_frag_data.last_length);
12371 }
12372 return relax_frag (segment, fragP, stretch);
12373}
12374
ee7fcc42
AM
12375/* md_estimate_size_before_relax()
12376
12377 Called just before relax() for rs_machine_dependent frags. The x86
12378 assembler uses these frags to handle variable size jump
12379 instructions.
12380
12381 Any symbol that is now undefined will not become defined.
12382 Return the correct fr_subtype in the frag.
12383 Return the initial "guess for variable size of frag" to caller.
12384 The guess is actually the growth beyond the fixed part. Whatever
12385 we do to grow the fixed or variable part contributes to our
12386 returned value. */
12387
252b5132 12388int
7016a5d5 12389md_estimate_size_before_relax (fragS *fragP, segT segment)
252b5132 12390{
e379e5f3
L
12391 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12392 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX
12393 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
12394 {
12395 i386_classify_machine_dependent_frag (fragP);
12396 return fragP->tc_frag_data.length;
12397 }
12398
252b5132 12399 /* We've already got fragP->fr_subtype right; all we have to do is
b98ef147
AM
12400 check for un-relaxable symbols. On an ELF system, we can't relax
12401 an externally visible symbol, because it may be overridden by a
12402 shared library. */
12403 if (S_GET_SEGMENT (fragP->fr_symbol) != segment
6d249963 12404#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12405 || (IS_ELF
8dcea932
L
12406 && !elf_symbol_resolved_in_segment_p (fragP->fr_symbol,
12407 fragP->fr_var))
fbeb56a4
DK
12408#endif
12409#if defined (OBJ_COFF) && defined (TE_PE)
7ab9ffdd 12410 || (OUTPUT_FLAVOR == bfd_target_coff_flavour
fbeb56a4 12411 && S_IS_WEAK (fragP->fr_symbol))
b98ef147
AM
12412#endif
12413 )
252b5132 12414 {
b98ef147
AM
12415 /* Symbol is undefined in this segment, or we need to keep a
12416 reloc so that weak symbols can be overridden. */
12417 int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
f86103b7 12418 enum bfd_reloc_code_real reloc_type;
ee7fcc42
AM
12419 unsigned char *opcode;
12420 int old_fr_fix;
eb19308f 12421 fixS *fixP = NULL;
f6af82bd 12422
ee7fcc42 12423 if (fragP->fr_var != NO_RELOC)
1e9cc1c2 12424 reloc_type = (enum bfd_reloc_code_real) fragP->fr_var;
b98ef147 12425 else if (size == 2)
f6af82bd 12426 reloc_type = BFD_RELOC_16_PCREL;
bd7ab16b 12427#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1ef3994a
JB
12428 else if (fragP->tc_frag_data.code64 && fragP->fr_offset == 0
12429 && need_plt32_p (fragP->fr_symbol))
bd7ab16b
L
12430 reloc_type = BFD_RELOC_X86_64_PLT32;
12431#endif
f6af82bd
AM
12432 else
12433 reloc_type = BFD_RELOC_32_PCREL;
252b5132 12434
ee7fcc42
AM
12435 old_fr_fix = fragP->fr_fix;
12436 opcode = (unsigned char *) fragP->fr_opcode;
12437
fddf5b5b 12438 switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
252b5132 12439 {
fddf5b5b
AM
12440 case UNCOND_JUMP:
12441 /* Make jmp (0xeb) a (d)word displacement jump. */
47926f60 12442 opcode[0] = 0xe9;
252b5132 12443 fragP->fr_fix += size;
eb19308f
JB
12444 fixP = fix_new (fragP, old_fr_fix, size,
12445 fragP->fr_symbol,
12446 fragP->fr_offset, 1,
12447 reloc_type);
252b5132
RH
12448 break;
12449
fddf5b5b 12450 case COND_JUMP86:
412167cb
AM
12451 if (size == 2
12452 && (!no_cond_jump_promotion || fragP->fr_var != NO_RELOC))
fddf5b5b
AM
12453 {
12454 /* Negate the condition, and branch past an
12455 unconditional jump. */
12456 opcode[0] ^= 1;
12457 opcode[1] = 3;
12458 /* Insert an unconditional jump. */
12459 opcode[2] = 0xe9;
12460 /* We added two extra opcode bytes, and have a two byte
12461 offset. */
12462 fragP->fr_fix += 2 + 2;
062cd5e7
AS
12463 fix_new (fragP, old_fr_fix + 2, 2,
12464 fragP->fr_symbol,
12465 fragP->fr_offset, 1,
12466 reloc_type);
fddf5b5b
AM
12467 break;
12468 }
12469 /* Fall through. */
12470
12471 case COND_JUMP:
412167cb
AM
12472 if (no_cond_jump_promotion && fragP->fr_var == NO_RELOC)
12473 {
12474 fragP->fr_fix += 1;
3e02c1cc
AM
12475 fixP = fix_new (fragP, old_fr_fix, 1,
12476 fragP->fr_symbol,
12477 fragP->fr_offset, 1,
12478 BFD_RELOC_8_PCREL);
12479 fixP->fx_signed = 1;
412167cb
AM
12480 break;
12481 }
93c2a809 12482
24eab124 12483 /* This changes the byte-displacement jump 0x7N
fddf5b5b 12484 to the (d)word-displacement jump 0x0f,0x8N. */
252b5132 12485 opcode[1] = opcode[0] + 0x10;
f6af82bd 12486 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
47926f60
KH
12487 /* We've added an opcode byte. */
12488 fragP->fr_fix += 1 + size;
eb19308f
JB
12489 fixP = fix_new (fragP, old_fr_fix + 1, size,
12490 fragP->fr_symbol,
12491 fragP->fr_offset, 1,
12492 reloc_type);
252b5132 12493 break;
fddf5b5b
AM
12494
12495 default:
12496 BAD_CASE (fragP->fr_subtype);
12497 break;
252b5132 12498 }
eb19308f
JB
12499
12500 /* All jumps handled here are signed, but don't unconditionally use a
12501 signed limit check for 32 and 16 bit jumps as we want to allow wrap
12502 around at 4G (outside of 64-bit mode) and 64k. */
12503 if (size == 4 && flag_code == CODE_64BIT)
12504 fixP->fx_signed = 1;
12505
252b5132 12506 frag_wane (fragP);
ee7fcc42 12507 return fragP->fr_fix - old_fr_fix;
252b5132 12508 }
93c2a809 12509
93c2a809
AM
12510 /* Guess size depending on current relax state. Initially the relax
12511 state will correspond to a short jump and we return 1, because
12512 the variable part of the frag (the branch offset) is one byte
12513 long. However, we can relax a section more than once and in that
12514 case we must either set fr_subtype back to the unrelaxed state,
12515 or return the value for the appropriate branch. */
12516 return md_relax_table[fragP->fr_subtype].rlx_length;
ee7fcc42
AM
12517}
12518
47926f60
KH
12519/* Called after relax() is finished.
12520
12521 In: Address of frag.
12522 fr_type == rs_machine_dependent.
12523 fr_subtype is what the address relaxed to.
12524
12525 Out: Any fixSs and constants are set up.
12526 Caller will turn frag into a ".space 0". */
12527
252b5132 12528void
7016a5d5
TG
12529md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec ATTRIBUTE_UNUSED,
12530 fragS *fragP)
252b5132 12531{
29b0f896 12532 unsigned char *opcode;
252b5132 12533 unsigned char *where_to_put_displacement = NULL;
847f7ad4
AM
12534 offsetT target_address;
12535 offsetT opcode_address;
252b5132 12536 unsigned int extension = 0;
847f7ad4 12537 offsetT displacement_from_opcode_start;
252b5132 12538
e379e5f3
L
12539 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12540 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING
12541 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12542 {
12543 /* Generate nop padding. */
12544 unsigned int size = fragP->tc_frag_data.length;
12545 if (size)
12546 {
12547 if (size > fragP->tc_frag_data.max_bytes)
12548 abort ();
12549
12550 if (flag_debug)
12551 {
12552 const char *msg;
12553 const char *branch = "branch";
12554 const char *prefix = "";
12555 fragS *padding_fragP;
12556 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
12557 == BRANCH_PREFIX)
12558 {
12559 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
12560 switch (fragP->tc_frag_data.default_prefix)
12561 {
12562 default:
12563 abort ();
12564 break;
12565 case CS_PREFIX_OPCODE:
12566 prefix = " cs";
12567 break;
12568 case DS_PREFIX_OPCODE:
12569 prefix = " ds";
12570 break;
12571 case ES_PREFIX_OPCODE:
12572 prefix = " es";
12573 break;
12574 case FS_PREFIX_OPCODE:
12575 prefix = " fs";
12576 break;
12577 case GS_PREFIX_OPCODE:
12578 prefix = " gs";
12579 break;
12580 case SS_PREFIX_OPCODE:
12581 prefix = " ss";
12582 break;
12583 }
12584 if (padding_fragP)
12585 msg = _("%s:%u: add %d%s at 0x%llx to align "
12586 "%s within %d-byte boundary\n");
12587 else
12588 msg = _("%s:%u: add additional %d%s at 0x%llx to "
12589 "align %s within %d-byte boundary\n");
12590 }
12591 else
12592 {
12593 padding_fragP = fragP;
12594 msg = _("%s:%u: add %d%s-byte nop at 0x%llx to align "
12595 "%s within %d-byte boundary\n");
12596 }
12597
12598 if (padding_fragP)
12599 switch (padding_fragP->tc_frag_data.branch_type)
12600 {
12601 case align_branch_jcc:
12602 branch = "jcc";
12603 break;
12604 case align_branch_fused:
12605 branch = "fused jcc";
12606 break;
12607 case align_branch_jmp:
12608 branch = "jmp";
12609 break;
12610 case align_branch_call:
12611 branch = "call";
12612 break;
12613 case align_branch_indirect:
12614 branch = "indiret branch";
12615 break;
12616 case align_branch_ret:
12617 branch = "ret";
12618 break;
12619 default:
12620 break;
12621 }
12622
12623 fprintf (stdout, msg,
12624 fragP->fr_file, fragP->fr_line, size, prefix,
12625 (long long) fragP->fr_address, branch,
12626 1 << align_branch_power);
12627 }
12628 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12629 memset (fragP->fr_opcode,
12630 fragP->tc_frag_data.default_prefix, size);
12631 else
12632 i386_generate_nops (fragP, (char *) fragP->fr_opcode,
12633 size, 0);
12634 fragP->fr_fix += size;
12635 }
12636 return;
12637 }
12638
252b5132
RH
12639 opcode = (unsigned char *) fragP->fr_opcode;
12640
47926f60 12641 /* Address we want to reach in file space. */
252b5132 12642 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
252b5132 12643
47926f60 12644 /* Address opcode resides at in file space. */
252b5132
RH
12645 opcode_address = fragP->fr_address + fragP->fr_fix;
12646
47926f60 12647 /* Displacement from opcode start to fill into instruction. */
252b5132
RH
12648 displacement_from_opcode_start = target_address - opcode_address;
12649
fddf5b5b 12650 if ((fragP->fr_subtype & BIG) == 0)
252b5132 12651 {
47926f60
KH
12652 /* Don't have to change opcode. */
12653 extension = 1; /* 1 opcode + 1 displacement */
252b5132 12654 where_to_put_displacement = &opcode[1];
fddf5b5b
AM
12655 }
12656 else
12657 {
12658 if (no_cond_jump_promotion
12659 && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
4eed87de
AM
12660 as_warn_where (fragP->fr_file, fragP->fr_line,
12661 _("long jump required"));
252b5132 12662
fddf5b5b
AM
12663 switch (fragP->fr_subtype)
12664 {
12665 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
12666 extension = 4; /* 1 opcode + 4 displacement */
12667 opcode[0] = 0xe9;
12668 where_to_put_displacement = &opcode[1];
12669 break;
252b5132 12670
fddf5b5b
AM
12671 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
12672 extension = 2; /* 1 opcode + 2 displacement */
12673 opcode[0] = 0xe9;
12674 where_to_put_displacement = &opcode[1];
12675 break;
252b5132 12676
fddf5b5b
AM
12677 case ENCODE_RELAX_STATE (COND_JUMP, BIG):
12678 case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
12679 extension = 5; /* 2 opcode + 4 displacement */
12680 opcode[1] = opcode[0] + 0x10;
12681 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12682 where_to_put_displacement = &opcode[2];
12683 break;
252b5132 12684
fddf5b5b
AM
12685 case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
12686 extension = 3; /* 2 opcode + 2 displacement */
12687 opcode[1] = opcode[0] + 0x10;
12688 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12689 where_to_put_displacement = &opcode[2];
12690 break;
252b5132 12691
fddf5b5b
AM
12692 case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
12693 extension = 4;
12694 opcode[0] ^= 1;
12695 opcode[1] = 3;
12696 opcode[2] = 0xe9;
12697 where_to_put_displacement = &opcode[3];
12698 break;
12699
12700 default:
12701 BAD_CASE (fragP->fr_subtype);
12702 break;
12703 }
252b5132 12704 }
fddf5b5b 12705
7b81dfbb
AJ
12706 /* If size if less then four we are sure that the operand fits,
12707 but if it's 4, then it could be that the displacement is larger
12708 then -/+ 2GB. */
12709 if (DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype) == 4
12710 && object_64bit
12711 && ((addressT) (displacement_from_opcode_start - extension
4eed87de
AM
12712 + ((addressT) 1 << 31))
12713 > (((addressT) 2 << 31) - 1)))
7b81dfbb
AJ
12714 {
12715 as_bad_where (fragP->fr_file, fragP->fr_line,
12716 _("jump target out of range"));
12717 /* Make us emit 0. */
12718 displacement_from_opcode_start = extension;
12719 }
47926f60 12720 /* Now put displacement after opcode. */
252b5132
RH
12721 md_number_to_chars ((char *) where_to_put_displacement,
12722 (valueT) (displacement_from_opcode_start - extension),
fddf5b5b 12723 DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
252b5132
RH
12724 fragP->fr_fix += extension;
12725}
12726\f
7016a5d5 12727/* Apply a fixup (fixP) to segment data, once it has been determined
252b5132
RH
12728 by our caller that we have all the info we need to fix it up.
12729
7016a5d5
TG
12730 Parameter valP is the pointer to the value of the bits.
12731
252b5132
RH
12732 On the 386, immediates, displacements, and data pointers are all in
12733 the same (little-endian) format, so we don't need to care about which
12734 we are handling. */
12735
94f592af 12736void
7016a5d5 12737md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 12738{
94f592af 12739 char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
c6682705 12740 valueT value = *valP;
252b5132 12741
f86103b7 12742#if !defined (TE_Mach)
93382f6d
AM
12743 if (fixP->fx_pcrel)
12744 {
12745 switch (fixP->fx_r_type)
12746 {
5865bb77
ILT
12747 default:
12748 break;
12749
d6ab8113
JB
12750 case BFD_RELOC_64:
12751 fixP->fx_r_type = BFD_RELOC_64_PCREL;
12752 break;
93382f6d 12753 case BFD_RELOC_32:
ae8887b5 12754 case BFD_RELOC_X86_64_32S:
93382f6d
AM
12755 fixP->fx_r_type = BFD_RELOC_32_PCREL;
12756 break;
12757 case BFD_RELOC_16:
12758 fixP->fx_r_type = BFD_RELOC_16_PCREL;
12759 break;
12760 case BFD_RELOC_8:
12761 fixP->fx_r_type = BFD_RELOC_8_PCREL;
12762 break;
12763 }
12764 }
252b5132 12765
a161fe53 12766 if (fixP->fx_addsy != NULL
31312f95 12767 && (fixP->fx_r_type == BFD_RELOC_32_PCREL
d6ab8113 12768 || fixP->fx_r_type == BFD_RELOC_64_PCREL
31312f95 12769 || fixP->fx_r_type == BFD_RELOC_16_PCREL
d258b828 12770 || fixP->fx_r_type == BFD_RELOC_8_PCREL)
31312f95 12771 && !use_rela_relocations)
252b5132 12772 {
31312f95
AM
12773 /* This is a hack. There should be a better way to handle this.
12774 This covers for the fact that bfd_install_relocation will
12775 subtract the current location (for partial_inplace, PC relative
12776 relocations); see more below. */
252b5132 12777#ifndef OBJ_AOUT
718ddfc0 12778 if (IS_ELF
252b5132
RH
12779#ifdef TE_PE
12780 || OUTPUT_FLAVOR == bfd_target_coff_flavour
12781#endif
12782 )
12783 value += fixP->fx_where + fixP->fx_frag->fr_address;
12784#endif
12785#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12786 if (IS_ELF)
252b5132 12787 {
6539b54b 12788 segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
2f66722d 12789
6539b54b 12790 if ((sym_seg == seg
2f66722d 12791 || (symbol_section_p (fixP->fx_addsy)
6539b54b 12792 && sym_seg != absolute_section))
af65af87 12793 && !generic_force_reloc (fixP))
2f66722d
AM
12794 {
12795 /* Yes, we add the values in twice. This is because
6539b54b
AM
12796 bfd_install_relocation subtracts them out again. I think
12797 bfd_install_relocation is broken, but I don't dare change
2f66722d
AM
12798 it. FIXME. */
12799 value += fixP->fx_where + fixP->fx_frag->fr_address;
12800 }
252b5132
RH
12801 }
12802#endif
12803#if defined (OBJ_COFF) && defined (TE_PE)
977cdf5a
NC
12804 /* For some reason, the PE format does not store a
12805 section address offset for a PC relative symbol. */
12806 if (S_GET_SEGMENT (fixP->fx_addsy) != seg
7be1c489 12807 || S_IS_WEAK (fixP->fx_addsy))
252b5132
RH
12808 value += md_pcrel_from (fixP);
12809#endif
12810 }
fbeb56a4 12811#if defined (OBJ_COFF) && defined (TE_PE)
f01c1a09
NC
12812 if (fixP->fx_addsy != NULL
12813 && S_IS_WEAK (fixP->fx_addsy)
12814 /* PR 16858: Do not modify weak function references. */
12815 && ! fixP->fx_pcrel)
fbeb56a4 12816 {
296a8689
NC
12817#if !defined (TE_PEP)
12818 /* For x86 PE weak function symbols are neither PC-relative
12819 nor do they set S_IS_FUNCTION. So the only reliable way
12820 to detect them is to check the flags of their containing
12821 section. */
12822 if (S_GET_SEGMENT (fixP->fx_addsy) != NULL
12823 && S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_CODE)
12824 ;
12825 else
12826#endif
fbeb56a4
DK
12827 value -= S_GET_VALUE (fixP->fx_addsy);
12828 }
12829#endif
252b5132
RH
12830
12831 /* Fix a few things - the dynamic linker expects certain values here,
0234cb7c 12832 and we must not disappoint it. */
252b5132 12833#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12834 if (IS_ELF && fixP->fx_addsy)
47926f60
KH
12835 switch (fixP->fx_r_type)
12836 {
12837 case BFD_RELOC_386_PLT32:
3e73aa7c 12838 case BFD_RELOC_X86_64_PLT32:
b9519cfe
L
12839 /* Make the jump instruction point to the address of the operand.
12840 At runtime we merely add the offset to the actual PLT entry.
12841 NB: Subtract the offset size only for jump instructions. */
12842 if (fixP->fx_pcrel)
12843 value = -4;
47926f60 12844 break;
31312f95 12845
13ae64f3
JJ
12846 case BFD_RELOC_386_TLS_GD:
12847 case BFD_RELOC_386_TLS_LDM:
13ae64f3 12848 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
12849 case BFD_RELOC_386_TLS_IE:
12850 case BFD_RELOC_386_TLS_GOTIE:
67a4f2b7 12851 case BFD_RELOC_386_TLS_GOTDESC:
bffbf940
JJ
12852 case BFD_RELOC_X86_64_TLSGD:
12853 case BFD_RELOC_X86_64_TLSLD:
12854 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7 12855 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
00f7efb6
JJ
12856 value = 0; /* Fully resolved at runtime. No addend. */
12857 /* Fallthrough */
12858 case BFD_RELOC_386_TLS_LE:
12859 case BFD_RELOC_386_TLS_LDO_32:
12860 case BFD_RELOC_386_TLS_LE_32:
12861 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 12862 case BFD_RELOC_X86_64_DTPOFF64:
00f7efb6 12863 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113 12864 case BFD_RELOC_X86_64_TPOFF64:
00f7efb6
JJ
12865 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12866 break;
12867
67a4f2b7
AO
12868 case BFD_RELOC_386_TLS_DESC_CALL:
12869 case BFD_RELOC_X86_64_TLSDESC_CALL:
12870 value = 0; /* Fully resolved at runtime. No addend. */
12871 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12872 fixP->fx_done = 0;
12873 return;
12874
47926f60
KH
12875 case BFD_RELOC_VTABLE_INHERIT:
12876 case BFD_RELOC_VTABLE_ENTRY:
12877 fixP->fx_done = 0;
94f592af 12878 return;
47926f60
KH
12879
12880 default:
12881 break;
12882 }
12883#endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) */
a442cac5
JB
12884
12885 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
12886 if (!object_64bit)
12887 value = extend_to_32bit_address (value);
12888
c6682705 12889 *valP = value;
f86103b7 12890#endif /* !defined (TE_Mach) */
3e73aa7c 12891
3e73aa7c 12892 /* Are we finished with this relocation now? */
c6682705 12893 if (fixP->fx_addsy == NULL)
b8188555
JB
12894 {
12895 fixP->fx_done = 1;
12896 switch (fixP->fx_r_type)
12897 {
12898 case BFD_RELOC_X86_64_32S:
12899 fixP->fx_signed = 1;
12900 break;
12901
12902 default:
12903 break;
12904 }
12905 }
fbeb56a4
DK
12906#if defined (OBJ_COFF) && defined (TE_PE)
12907 else if (fixP->fx_addsy != NULL && S_IS_WEAK (fixP->fx_addsy))
12908 {
12909 fixP->fx_done = 0;
12910 /* Remember value for tc_gen_reloc. */
12911 fixP->fx_addnumber = value;
12912 /* Clear out the frag for now. */
12913 value = 0;
12914 }
12915#endif
3e73aa7c
JH
12916 else if (use_rela_relocations)
12917 {
46fb6d5a
JB
12918 if (!disallow_64bit_reloc || fixP->fx_r_type == NO_RELOC)
12919 fixP->fx_no_overflow = 1;
062cd5e7
AS
12920 /* Remember value for tc_gen_reloc. */
12921 fixP->fx_addnumber = value;
3e73aa7c
JH
12922 value = 0;
12923 }
f86103b7 12924
94f592af 12925 md_number_to_chars (p, value, fixP->fx_size);
252b5132 12926}
252b5132 12927\f
6d4af3c2 12928const char *
499ac353 12929md_atof (int type, char *litP, int *sizeP)
252b5132 12930{
499ac353
NC
12931 /* This outputs the LITTLENUMs in REVERSE order;
12932 in accord with the bigendian 386. */
5b7c81bd 12933 return ieee_md_atof (type, litP, sizeP, false);
252b5132
RH
12934}
12935\f
2d545b82 12936static char output_invalid_buf[sizeof (unsigned char) * 2 + 6];
252b5132 12937
252b5132 12938static char *
e3bb37b5 12939output_invalid (int c)
252b5132 12940{
3882b010 12941 if (ISPRINT (c))
f9f21a03
L
12942 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
12943 "'%c'", c);
252b5132 12944 else
f9f21a03 12945 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
2d545b82 12946 "(0x%x)", (unsigned char) c);
252b5132
RH
12947 return output_invalid_buf;
12948}
12949
8a6fb3f9
JB
12950/* Verify that @r can be used in the current context. */
12951
5b7c81bd 12952static bool check_register (const reg_entry *r)
8a6fb3f9
JB
12953{
12954 if (allow_pseudo_reg)
5b7c81bd 12955 return true;
8a6fb3f9
JB
12956
12957 if (operand_type_all_zero (&r->reg_type))
5b7c81bd 12958 return false;
8a6fb3f9
JB
12959
12960 if ((r->reg_type.bitfield.dword
12961 || (r->reg_type.bitfield.class == SReg && r->reg_num > 3)
12962 || r->reg_type.bitfield.class == RegCR
22e00a3f 12963 || r->reg_type.bitfield.class == RegDR)
8a6fb3f9 12964 && !cpu_arch_flags.bitfield.cpui386)
5b7c81bd 12965 return false;
8a6fb3f9 12966
22e00a3f
JB
12967 if (r->reg_type.bitfield.class == RegTR
12968 && (flag_code == CODE_64BIT
12969 || !cpu_arch_flags.bitfield.cpui386
12970 || cpu_arch_isa_flags.bitfield.cpui586
12971 || cpu_arch_isa_flags.bitfield.cpui686))
5b7c81bd 12972 return false;
22e00a3f 12973
8a6fb3f9 12974 if (r->reg_type.bitfield.class == RegMMX && !cpu_arch_flags.bitfield.cpummx)
5b7c81bd 12975 return false;
8a6fb3f9
JB
12976
12977 if (!cpu_arch_flags.bitfield.cpuavx512f)
12978 {
12979 if (r->reg_type.bitfield.zmmword
12980 || r->reg_type.bitfield.class == RegMask)
5b7c81bd 12981 return false;
8a6fb3f9
JB
12982
12983 if (!cpu_arch_flags.bitfield.cpuavx)
12984 {
12985 if (r->reg_type.bitfield.ymmword)
5b7c81bd 12986 return false;
8a6fb3f9
JB
12987
12988 if (!cpu_arch_flags.bitfield.cpusse && r->reg_type.bitfield.xmmword)
5b7c81bd 12989 return false;
8a6fb3f9
JB
12990 }
12991 }
12992
260cd341
LC
12993 if (r->reg_type.bitfield.tmmword
12994 && (!cpu_arch_flags.bitfield.cpuamx_tile
12995 || flag_code != CODE_64BIT))
5b7c81bd 12996 return false;
260cd341 12997
8a6fb3f9 12998 if (r->reg_type.bitfield.class == RegBND && !cpu_arch_flags.bitfield.cpumpx)
5b7c81bd 12999 return false;
8a6fb3f9
JB
13000
13001 /* Don't allow fake index register unless allow_index_reg isn't 0. */
13002 if (!allow_index_reg && r->reg_num == RegIZ)
5b7c81bd 13003 return false;
8a6fb3f9
JB
13004
13005 /* Upper 16 vector registers are only available with VREX in 64bit
13006 mode, and require EVEX encoding. */
13007 if (r->reg_flags & RegVRex)
13008 {
13009 if (!cpu_arch_flags.bitfield.cpuavx512f
13010 || flag_code != CODE_64BIT)
5b7c81bd 13011 return false;
8a6fb3f9 13012
da4977e0
JB
13013 if (i.vec_encoding == vex_encoding_default)
13014 i.vec_encoding = vex_encoding_evex;
13015 else if (i.vec_encoding != vex_encoding_evex)
13016 i.vec_encoding = vex_encoding_error;
8a6fb3f9
JB
13017 }
13018
13019 if (((r->reg_flags & (RegRex64 | RegRex)) || r->reg_type.bitfield.qword)
13020 && (!cpu_arch_flags.bitfield.cpulm || r->reg_type.bitfield.class != RegCR)
13021 && flag_code != CODE_64BIT)
5b7c81bd 13022 return false;
8a6fb3f9
JB
13023
13024 if (r->reg_type.bitfield.class == SReg && r->reg_num == RegFlat
13025 && !intel_syntax)
5b7c81bd 13026 return false;
8a6fb3f9 13027
5b7c81bd 13028 return true;
8a6fb3f9
JB
13029}
13030
af6bdddf 13031/* REG_STRING starts *before* REGISTER_PREFIX. */
252b5132
RH
13032
13033static const reg_entry *
4d1bb795 13034parse_real_register (char *reg_string, char **end_op)
252b5132 13035{
af6bdddf
AM
13036 char *s = reg_string;
13037 char *p;
252b5132
RH
13038 char reg_name_given[MAX_REG_NAME_SIZE + 1];
13039 const reg_entry *r;
13040
13041 /* Skip possible REGISTER_PREFIX and possible whitespace. */
13042 if (*s == REGISTER_PREFIX)
13043 ++s;
13044
13045 if (is_space_char (*s))
13046 ++s;
13047
13048 p = reg_name_given;
af6bdddf 13049 while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
252b5132
RH
13050 {
13051 if (p >= reg_name_given + MAX_REG_NAME_SIZE)
af6bdddf
AM
13052 return (const reg_entry *) NULL;
13053 s++;
252b5132
RH
13054 }
13055
6588847e
DN
13056 /* For naked regs, make sure that we are not dealing with an identifier.
13057 This prevents confusing an identifier like `eax_var' with register
13058 `eax'. */
13059 if (allow_naked_reg && identifier_chars[(unsigned char) *s])
13060 return (const reg_entry *) NULL;
13061
af6bdddf 13062 *end_op = s;
252b5132 13063
629310ab 13064 r = (const reg_entry *) str_hash_find (reg_hash, reg_name_given);
252b5132 13065
5f47d35b 13066 /* Handle floating point regs, allowing spaces in the (i) part. */
6288d05f 13067 if (r == reg_st0)
5f47d35b 13068 {
0e0eea78
JB
13069 if (!cpu_arch_flags.bitfield.cpu8087
13070 && !cpu_arch_flags.bitfield.cpu287
af32b722
JB
13071 && !cpu_arch_flags.bitfield.cpu387
13072 && !allow_pseudo_reg)
0e0eea78
JB
13073 return (const reg_entry *) NULL;
13074
5f47d35b
AM
13075 if (is_space_char (*s))
13076 ++s;
13077 if (*s == '(')
13078 {
af6bdddf 13079 ++s;
5f47d35b
AM
13080 if (is_space_char (*s))
13081 ++s;
13082 if (*s >= '0' && *s <= '7')
13083 {
db557034 13084 int fpr = *s - '0';
af6bdddf 13085 ++s;
5f47d35b
AM
13086 if (is_space_char (*s))
13087 ++s;
13088 if (*s == ')')
13089 {
13090 *end_op = s + 1;
6288d05f 13091 know (r[fpr].reg_num == fpr);
db557034 13092 return r + fpr;
5f47d35b 13093 }
5f47d35b 13094 }
47926f60 13095 /* We have "%st(" then garbage. */
5f47d35b
AM
13096 return (const reg_entry *) NULL;
13097 }
13098 }
13099
8a6fb3f9 13100 return r && check_register (r) ? r : NULL;
252b5132 13101}
4d1bb795
JB
13102
13103/* REG_STRING starts *before* REGISTER_PREFIX. */
13104
13105static const reg_entry *
13106parse_register (char *reg_string, char **end_op)
13107{
13108 const reg_entry *r;
13109
13110 if (*reg_string == REGISTER_PREFIX || allow_naked_reg)
13111 r = parse_real_register (reg_string, end_op);
13112 else
13113 r = NULL;
13114 if (!r)
13115 {
13116 char *save = input_line_pointer;
13117 char c;
13118 symbolS *symbolP;
13119
13120 input_line_pointer = reg_string;
d02603dc 13121 c = get_symbol_name (&reg_string);
4d1bb795 13122 symbolP = symbol_find (reg_string);
64d23078
JB
13123 while (symbolP && S_GET_SEGMENT (symbolP) != reg_section)
13124 {
13125 const expressionS *e = symbol_get_value_expression(symbolP);
13126
13127 if (e->X_op != O_symbol || e->X_add_number)
13128 break;
13129 symbolP = e->X_add_symbol;
13130 }
4d1bb795
JB
13131 if (symbolP && S_GET_SEGMENT (symbolP) == reg_section)
13132 {
13133 const expressionS *e = symbol_get_value_expression (symbolP);
13134
3b55a1d0
JB
13135 know (e->X_op == O_register);
13136 know (e->X_add_number >= 0
13137 && (valueT) e->X_add_number < i386_regtab_size);
13138 r = i386_regtab + e->X_add_number;
13139 if (!check_register (r))
8a6fb3f9 13140 {
3b55a1d0
JB
13141 as_bad (_("register '%s%s' cannot be used here"),
13142 register_prefix, r->reg_name);
13143 r = &bad_reg;
8a6fb3f9 13144 }
3b55a1d0 13145 *end_op = input_line_pointer;
4d1bb795
JB
13146 }
13147 *input_line_pointer = c;
13148 input_line_pointer = save;
13149 }
13150 return r;
13151}
13152
13153int
13154i386_parse_name (char *name, expressionS *e, char *nextcharP)
13155{
4faaa10f 13156 const reg_entry *r = NULL;
4d1bb795
JB
13157 char *end = input_line_pointer;
13158
13159 *end = *nextcharP;
4faaa10f
JB
13160 if (*name == REGISTER_PREFIX || allow_naked_reg)
13161 r = parse_real_register (name, &input_line_pointer);
4d1bb795
JB
13162 if (r && end <= input_line_pointer)
13163 {
13164 *nextcharP = *input_line_pointer;
13165 *input_line_pointer = 0;
8a6fb3f9
JB
13166 if (r != &bad_reg)
13167 {
13168 e->X_op = O_register;
13169 e->X_add_number = r - i386_regtab;
13170 }
13171 else
13172 e->X_op = O_illegal;
4d1bb795
JB
13173 return 1;
13174 }
13175 input_line_pointer = end;
13176 *end = 0;
ee86248c 13177 return intel_syntax ? i386_intel_parse_name (name, e) : 0;
4d1bb795
JB
13178}
13179
13180void
13181md_operand (expressionS *e)
13182{
ee86248c
JB
13183 char *end;
13184 const reg_entry *r;
4d1bb795 13185
ee86248c
JB
13186 switch (*input_line_pointer)
13187 {
13188 case REGISTER_PREFIX:
13189 r = parse_real_register (input_line_pointer, &end);
4d1bb795
JB
13190 if (r)
13191 {
13192 e->X_op = O_register;
13193 e->X_add_number = r - i386_regtab;
13194 input_line_pointer = end;
13195 }
ee86248c
JB
13196 break;
13197
13198 case '[':
9c2799c2 13199 gas_assert (intel_syntax);
ee86248c
JB
13200 end = input_line_pointer++;
13201 expression (e);
13202 if (*input_line_pointer == ']')
13203 {
13204 ++input_line_pointer;
13205 e->X_op_symbol = make_expr_symbol (e);
13206 e->X_add_symbol = NULL;
13207 e->X_add_number = 0;
13208 e->X_op = O_index;
13209 }
13210 else
13211 {
13212 e->X_op = O_absent;
13213 input_line_pointer = end;
13214 }
13215 break;
4d1bb795
JB
13216 }
13217}
13218
252b5132 13219\f
4cc782b5 13220#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
b6f8c7c4 13221const char *md_shortopts = "kVQ:sqnO::";
252b5132 13222#else
b6f8c7c4 13223const char *md_shortopts = "qnO::";
252b5132 13224#endif
6e0b89ee 13225
3e73aa7c 13226#define OPTION_32 (OPTION_MD_BASE + 0)
b3b91714
AM
13227#define OPTION_64 (OPTION_MD_BASE + 1)
13228#define OPTION_DIVIDE (OPTION_MD_BASE + 2)
9103f4f4
L
13229#define OPTION_MARCH (OPTION_MD_BASE + 3)
13230#define OPTION_MTUNE (OPTION_MD_BASE + 4)
1efbbeb4
L
13231#define OPTION_MMNEMONIC (OPTION_MD_BASE + 5)
13232#define OPTION_MSYNTAX (OPTION_MD_BASE + 6)
13233#define OPTION_MINDEX_REG (OPTION_MD_BASE + 7)
13234#define OPTION_MNAKED_REG (OPTION_MD_BASE + 8)
bd5dea88 13235#define OPTION_MRELAX_RELOCATIONS (OPTION_MD_BASE + 9)
c0f3af97 13236#define OPTION_MSSE2AVX (OPTION_MD_BASE + 10)
daf50ae7 13237#define OPTION_MSSE_CHECK (OPTION_MD_BASE + 11)
7bab8ab5
JB
13238#define OPTION_MOPERAND_CHECK (OPTION_MD_BASE + 12)
13239#define OPTION_MAVXSCALAR (OPTION_MD_BASE + 13)
13240#define OPTION_X32 (OPTION_MD_BASE + 14)
7e8b059b 13241#define OPTION_MADD_BND_PREFIX (OPTION_MD_BASE + 15)
43234a1e
L
13242#define OPTION_MEVEXLIG (OPTION_MD_BASE + 16)
13243#define OPTION_MEVEXWIG (OPTION_MD_BASE + 17)
167ad85b 13244#define OPTION_MBIG_OBJ (OPTION_MD_BASE + 18)
d1982f93 13245#define OPTION_MOMIT_LOCK_PREFIX (OPTION_MD_BASE + 19)
d3d3c6db 13246#define OPTION_MEVEXRCIG (OPTION_MD_BASE + 20)
8dcea932 13247#define OPTION_MSHARED (OPTION_MD_BASE + 21)
5db04b09
L
13248#define OPTION_MAMD64 (OPTION_MD_BASE + 22)
13249#define OPTION_MINTEL64 (OPTION_MD_BASE + 23)
e4e00185 13250#define OPTION_MFENCE_AS_LOCK_ADD (OPTION_MD_BASE + 24)
b4a3a7b4 13251#define OPTION_X86_USED_NOTE (OPTION_MD_BASE + 25)
03751133 13252#define OPTION_MVEXWIG (OPTION_MD_BASE + 26)
e379e5f3
L
13253#define OPTION_MALIGN_BRANCH_BOUNDARY (OPTION_MD_BASE + 27)
13254#define OPTION_MALIGN_BRANCH_PREFIX_SIZE (OPTION_MD_BASE + 28)
13255#define OPTION_MALIGN_BRANCH (OPTION_MD_BASE + 29)
76cf450b 13256#define OPTION_MBRANCHES_WITH_32B_BOUNDARIES (OPTION_MD_BASE + 30)
ae531041
L
13257#define OPTION_MLFENCE_AFTER_LOAD (OPTION_MD_BASE + 31)
13258#define OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH (OPTION_MD_BASE + 32)
13259#define OPTION_MLFENCE_BEFORE_RET (OPTION_MD_BASE + 33)
c8480b58 13260#define OPTION_MUSE_UNALIGNED_VECTOR_MOVE (OPTION_MD_BASE + 34)
b3b91714 13261
99ad8390
NC
13262struct option md_longopts[] =
13263{
3e73aa7c 13264 {"32", no_argument, NULL, OPTION_32},
321098a5 13265#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 13266 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c 13267 {"64", no_argument, NULL, OPTION_64},
351f65ca
L
13268#endif
13269#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 13270 {"x32", no_argument, NULL, OPTION_X32},
8dcea932 13271 {"mshared", no_argument, NULL, OPTION_MSHARED},
b4a3a7b4 13272 {"mx86-used-note", required_argument, NULL, OPTION_X86_USED_NOTE},
6e0b89ee 13273#endif
b3b91714 13274 {"divide", no_argument, NULL, OPTION_DIVIDE},
9103f4f4
L
13275 {"march", required_argument, NULL, OPTION_MARCH},
13276 {"mtune", required_argument, NULL, OPTION_MTUNE},
1efbbeb4
L
13277 {"mmnemonic", required_argument, NULL, OPTION_MMNEMONIC},
13278 {"msyntax", required_argument, NULL, OPTION_MSYNTAX},
13279 {"mindex-reg", no_argument, NULL, OPTION_MINDEX_REG},
13280 {"mnaked-reg", no_argument, NULL, OPTION_MNAKED_REG},
c0f3af97 13281 {"msse2avx", no_argument, NULL, OPTION_MSSE2AVX},
c8480b58 13282 {"muse-unaligned-vector-move", no_argument, NULL, OPTION_MUSE_UNALIGNED_VECTOR_MOVE},
daf50ae7 13283 {"msse-check", required_argument, NULL, OPTION_MSSE_CHECK},
7bab8ab5 13284 {"moperand-check", required_argument, NULL, OPTION_MOPERAND_CHECK},
539f890d 13285 {"mavxscalar", required_argument, NULL, OPTION_MAVXSCALAR},
03751133 13286 {"mvexwig", required_argument, NULL, OPTION_MVEXWIG},
7e8b059b 13287 {"madd-bnd-prefix", no_argument, NULL, OPTION_MADD_BND_PREFIX},
43234a1e
L
13288 {"mevexlig", required_argument, NULL, OPTION_MEVEXLIG},
13289 {"mevexwig", required_argument, NULL, OPTION_MEVEXWIG},
167ad85b
TG
13290# if defined (TE_PE) || defined (TE_PEP)
13291 {"mbig-obj", no_argument, NULL, OPTION_MBIG_OBJ},
13292#endif
d1982f93 13293 {"momit-lock-prefix", required_argument, NULL, OPTION_MOMIT_LOCK_PREFIX},
e4e00185 13294 {"mfence-as-lock-add", required_argument, NULL, OPTION_MFENCE_AS_LOCK_ADD},
0cb4071e 13295 {"mrelax-relocations", required_argument, NULL, OPTION_MRELAX_RELOCATIONS},
d3d3c6db 13296 {"mevexrcig", required_argument, NULL, OPTION_MEVEXRCIG},
e379e5f3
L
13297 {"malign-branch-boundary", required_argument, NULL, OPTION_MALIGN_BRANCH_BOUNDARY},
13298 {"malign-branch-prefix-size", required_argument, NULL, OPTION_MALIGN_BRANCH_PREFIX_SIZE},
13299 {"malign-branch", required_argument, NULL, OPTION_MALIGN_BRANCH},
76cf450b 13300 {"mbranches-within-32B-boundaries", no_argument, NULL, OPTION_MBRANCHES_WITH_32B_BOUNDARIES},
ae531041
L
13301 {"mlfence-after-load", required_argument, NULL, OPTION_MLFENCE_AFTER_LOAD},
13302 {"mlfence-before-indirect-branch", required_argument, NULL,
13303 OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH},
13304 {"mlfence-before-ret", required_argument, NULL, OPTION_MLFENCE_BEFORE_RET},
5db04b09
L
13305 {"mamd64", no_argument, NULL, OPTION_MAMD64},
13306 {"mintel64", no_argument, NULL, OPTION_MINTEL64},
252b5132
RH
13307 {NULL, no_argument, NULL, 0}
13308};
13309size_t md_longopts_size = sizeof (md_longopts);
13310
13311int
17b9d67d 13312md_parse_option (int c, const char *arg)
252b5132 13313{
91d6fa6a 13314 unsigned int j;
e379e5f3 13315 char *arch, *next, *saved, *type;
9103f4f4 13316
252b5132
RH
13317 switch (c)
13318 {
12b55ccc
L
13319 case 'n':
13320 optimize_align_code = 0;
13321 break;
13322
a38cf1db
AM
13323 case 'q':
13324 quiet_warnings = 1;
252b5132
RH
13325 break;
13326
13327#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
a38cf1db
AM
13328 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
13329 should be emitted or not. FIXME: Not implemented. */
13330 case 'Q':
d4693039
JB
13331 if ((arg[0] != 'y' && arg[0] != 'n') || arg[1])
13332 return 0;
252b5132
RH
13333 break;
13334
13335 /* -V: SVR4 argument to print version ID. */
13336 case 'V':
13337 print_version_id ();
13338 break;
13339
a38cf1db
AM
13340 /* -k: Ignore for FreeBSD compatibility. */
13341 case 'k':
252b5132 13342 break;
4cc782b5
ILT
13343
13344 case 's':
13345 /* -s: On i386 Solaris, this tells the native assembler to use
29b0f896 13346 .stab instead of .stab.excl. We always use .stab anyhow. */
4cc782b5 13347 break;
8dcea932
L
13348
13349 case OPTION_MSHARED:
13350 shared = 1;
13351 break;
b4a3a7b4
L
13352
13353 case OPTION_X86_USED_NOTE:
13354 if (strcasecmp (arg, "yes") == 0)
13355 x86_used_note = 1;
13356 else if (strcasecmp (arg, "no") == 0)
13357 x86_used_note = 0;
13358 else
13359 as_fatal (_("invalid -mx86-used-note= option: `%s'"), arg);
13360 break;
13361
13362
99ad8390 13363#endif
321098a5 13364#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 13365 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c
JH
13366 case OPTION_64:
13367 {
13368 const char **list, **l;
13369
3e73aa7c
JH
13370 list = bfd_target_list ();
13371 for (l = list; *l != NULL; l++)
08dedd66 13372 if (startswith (*l, "elf64-x86-64")
99ad8390
NC
13373 || strcmp (*l, "coff-x86-64") == 0
13374 || strcmp (*l, "pe-x86-64") == 0
d382c579
TG
13375 || strcmp (*l, "pei-x86-64") == 0
13376 || strcmp (*l, "mach-o-x86-64") == 0)
6e0b89ee
AM
13377 {
13378 default_arch = "x86_64";
13379 break;
13380 }
3e73aa7c 13381 if (*l == NULL)
2b5d6a91 13382 as_fatal (_("no compiled in support for x86_64"));
3e73aa7c
JH
13383 free (list);
13384 }
13385 break;
13386#endif
252b5132 13387
351f65ca 13388#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 13389 case OPTION_X32:
351f65ca
L
13390 if (IS_ELF)
13391 {
13392 const char **list, **l;
13393
13394 list = bfd_target_list ();
13395 for (l = list; *l != NULL; l++)
08dedd66 13396 if (startswith (*l, "elf32-x86-64"))
351f65ca
L
13397 {
13398 default_arch = "x86_64:32";
13399 break;
13400 }
13401 if (*l == NULL)
2b5d6a91 13402 as_fatal (_("no compiled in support for 32bit x86_64"));
351f65ca
L
13403 free (list);
13404 }
13405 else
13406 as_fatal (_("32bit x86_64 is only supported for ELF"));
13407 break;
13408#endif
13409
6e0b89ee
AM
13410 case OPTION_32:
13411 default_arch = "i386";
13412 break;
13413
b3b91714
AM
13414 case OPTION_DIVIDE:
13415#ifdef SVR4_COMMENT_CHARS
13416 {
13417 char *n, *t;
13418 const char *s;
13419
add39d23 13420 n = XNEWVEC (char, strlen (i386_comment_chars) + 1);
b3b91714
AM
13421 t = n;
13422 for (s = i386_comment_chars; *s != '\0'; s++)
13423 if (*s != '/')
13424 *t++ = *s;
13425 *t = '\0';
13426 i386_comment_chars = n;
13427 }
13428#endif
13429 break;
13430
9103f4f4 13431 case OPTION_MARCH:
293f5f65
L
13432 saved = xstrdup (arg);
13433 arch = saved;
13434 /* Allow -march=+nosse. */
13435 if (*arch == '+')
13436 arch++;
6305a203 13437 do
9103f4f4 13438 {
6305a203 13439 if (*arch == '.')
2b5d6a91 13440 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
13441 next = strchr (arch, '+');
13442 if (next)
13443 *next++ = '\0';
91d6fa6a 13444 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 13445 {
ae89daec
JB
13446 if (arch == saved && cpu_arch[j].type != PROCESSOR_NONE
13447 && strcmp (arch, cpu_arch[j].name) == 0)
ccc9c027 13448 {
6305a203 13449 /* Processor. */
ae89daec 13450 if (! cpu_arch[j].enable.bitfield.cpui386)
1ded5609
JB
13451 continue;
13452
91d6fa6a 13453 cpu_arch_name = cpu_arch[j].name;
d92c7521 13454 free (cpu_sub_arch_name);
6305a203 13455 cpu_sub_arch_name = NULL;
ae89daec 13456 cpu_arch_flags = cpu_arch[j].enable;
91d6fa6a 13457 cpu_arch_isa = cpu_arch[j].type;
ae89daec 13458 cpu_arch_isa_flags = cpu_arch[j].enable;
6305a203
L
13459 if (!cpu_arch_tune_set)
13460 {
13461 cpu_arch_tune = cpu_arch_isa;
13462 cpu_arch_tune_flags = cpu_arch_isa_flags;
13463 }
13464 break;
13465 }
ae89daec
JB
13466 else if (cpu_arch[j].type == PROCESSOR_NONE
13467 && strcmp (arch, cpu_arch[j].name) == 0
13468 && !cpu_flags_all_zero (&cpu_arch[j].enable))
6305a203 13469 {
33eaf5de 13470 /* ISA extension. */
6305a203 13471 i386_cpu_flags flags;
309d3373 13472
293f5f65 13473 flags = cpu_flags_or (cpu_arch_flags,
ae89daec 13474 cpu_arch[j].enable);
81486035 13475
5b64d091 13476 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
6305a203 13477 {
ae89daec 13478 extend_cpu_sub_arch_name (arch);
6305a203 13479 cpu_arch_flags = flags;
a586129e 13480 cpu_arch_isa_flags = flags;
6305a203 13481 }
0089dace
L
13482 else
13483 cpu_arch_isa_flags
13484 = cpu_flags_or (cpu_arch_isa_flags,
ae89daec 13485 cpu_arch[j].enable);
6305a203 13486 break;
ccc9c027 13487 }
9103f4f4 13488 }
6305a203 13489
ae89daec 13490 if (j >= ARRAY_SIZE (cpu_arch) && startswith (arch, "no"))
293f5f65 13491 {
33eaf5de 13492 /* Disable an ISA extension. */
ae89daec
JB
13493 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13494 if (cpu_arch[j].type == PROCESSOR_NONE
13495 && strcmp (arch + 2, cpu_arch[j].name) == 0)
293f5f65
L
13496 {
13497 i386_cpu_flags flags;
13498
13499 flags = cpu_flags_and_not (cpu_arch_flags,
ae89daec 13500 cpu_arch[j].disable);
293f5f65
L
13501 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
13502 {
8180707f 13503 extend_cpu_sub_arch_name (arch);
293f5f65
L
13504 cpu_arch_flags = flags;
13505 cpu_arch_isa_flags = flags;
13506 }
13507 break;
13508 }
293f5f65
L
13509 }
13510
91d6fa6a 13511 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 13512 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
13513
13514 arch = next;
9103f4f4 13515 }
293f5f65
L
13516 while (next != NULL);
13517 free (saved);
9103f4f4
L
13518 break;
13519
13520 case OPTION_MTUNE:
13521 if (*arg == '.')
2b5d6a91 13522 as_fatal (_("invalid -mtune= option: `%s'"), arg);
91d6fa6a 13523 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 13524 {
ae89daec
JB
13525 if (cpu_arch[j].type != PROCESSOR_NONE
13526 && strcmp (arg, cpu_arch[j].name) == 0)
9103f4f4 13527 {
ccc9c027 13528 cpu_arch_tune_set = 1;
91d6fa6a 13529 cpu_arch_tune = cpu_arch [j].type;
ae89daec 13530 cpu_arch_tune_flags = cpu_arch[j].enable;
9103f4f4
L
13531 break;
13532 }
13533 }
91d6fa6a 13534 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 13535 as_fatal (_("invalid -mtune= option: `%s'"), arg);
9103f4f4
L
13536 break;
13537
1efbbeb4
L
13538 case OPTION_MMNEMONIC:
13539 if (strcasecmp (arg, "att") == 0)
13540 intel_mnemonic = 0;
13541 else if (strcasecmp (arg, "intel") == 0)
13542 intel_mnemonic = 1;
13543 else
2b5d6a91 13544 as_fatal (_("invalid -mmnemonic= option: `%s'"), arg);
1efbbeb4
L
13545 break;
13546
13547 case OPTION_MSYNTAX:
13548 if (strcasecmp (arg, "att") == 0)
13549 intel_syntax = 0;
13550 else if (strcasecmp (arg, "intel") == 0)
13551 intel_syntax = 1;
13552 else
2b5d6a91 13553 as_fatal (_("invalid -msyntax= option: `%s'"), arg);
1efbbeb4
L
13554 break;
13555
13556 case OPTION_MINDEX_REG:
13557 allow_index_reg = 1;
13558 break;
13559
13560 case OPTION_MNAKED_REG:
13561 allow_naked_reg = 1;
13562 break;
13563
c0f3af97
L
13564 case OPTION_MSSE2AVX:
13565 sse2avx = 1;
13566 break;
13567
c8480b58
L
13568 case OPTION_MUSE_UNALIGNED_VECTOR_MOVE:
13569 use_unaligned_vector_move = 1;
13570 break;
13571
daf50ae7
L
13572 case OPTION_MSSE_CHECK:
13573 if (strcasecmp (arg, "error") == 0)
7bab8ab5 13574 sse_check = check_error;
daf50ae7 13575 else if (strcasecmp (arg, "warning") == 0)
7bab8ab5 13576 sse_check = check_warning;
daf50ae7 13577 else if (strcasecmp (arg, "none") == 0)
7bab8ab5 13578 sse_check = check_none;
daf50ae7 13579 else
2b5d6a91 13580 as_fatal (_("invalid -msse-check= option: `%s'"), arg);
daf50ae7
L
13581 break;
13582
7bab8ab5
JB
13583 case OPTION_MOPERAND_CHECK:
13584 if (strcasecmp (arg, "error") == 0)
13585 operand_check = check_error;
13586 else if (strcasecmp (arg, "warning") == 0)
13587 operand_check = check_warning;
13588 else if (strcasecmp (arg, "none") == 0)
13589 operand_check = check_none;
13590 else
13591 as_fatal (_("invalid -moperand-check= option: `%s'"), arg);
13592 break;
13593
539f890d
L
13594 case OPTION_MAVXSCALAR:
13595 if (strcasecmp (arg, "128") == 0)
13596 avxscalar = vex128;
13597 else if (strcasecmp (arg, "256") == 0)
13598 avxscalar = vex256;
13599 else
2b5d6a91 13600 as_fatal (_("invalid -mavxscalar= option: `%s'"), arg);
539f890d
L
13601 break;
13602
03751133
L
13603 case OPTION_MVEXWIG:
13604 if (strcmp (arg, "0") == 0)
40c9c8de 13605 vexwig = vexw0;
03751133 13606 else if (strcmp (arg, "1") == 0)
40c9c8de 13607 vexwig = vexw1;
03751133
L
13608 else
13609 as_fatal (_("invalid -mvexwig= option: `%s'"), arg);
13610 break;
13611
7e8b059b
L
13612 case OPTION_MADD_BND_PREFIX:
13613 add_bnd_prefix = 1;
13614 break;
13615
43234a1e
L
13616 case OPTION_MEVEXLIG:
13617 if (strcmp (arg, "128") == 0)
13618 evexlig = evexl128;
13619 else if (strcmp (arg, "256") == 0)
13620 evexlig = evexl256;
13621 else if (strcmp (arg, "512") == 0)
13622 evexlig = evexl512;
13623 else
13624 as_fatal (_("invalid -mevexlig= option: `%s'"), arg);
13625 break;
13626
d3d3c6db
IT
13627 case OPTION_MEVEXRCIG:
13628 if (strcmp (arg, "rne") == 0)
13629 evexrcig = rne;
13630 else if (strcmp (arg, "rd") == 0)
13631 evexrcig = rd;
13632 else if (strcmp (arg, "ru") == 0)
13633 evexrcig = ru;
13634 else if (strcmp (arg, "rz") == 0)
13635 evexrcig = rz;
13636 else
13637 as_fatal (_("invalid -mevexrcig= option: `%s'"), arg);
13638 break;
13639
43234a1e
L
13640 case OPTION_MEVEXWIG:
13641 if (strcmp (arg, "0") == 0)
13642 evexwig = evexw0;
13643 else if (strcmp (arg, "1") == 0)
13644 evexwig = evexw1;
13645 else
13646 as_fatal (_("invalid -mevexwig= option: `%s'"), arg);
13647 break;
13648
167ad85b
TG
13649# if defined (TE_PE) || defined (TE_PEP)
13650 case OPTION_MBIG_OBJ:
13651 use_big_obj = 1;
13652 break;
13653#endif
13654
d1982f93 13655 case OPTION_MOMIT_LOCK_PREFIX:
d022bddd
IT
13656 if (strcasecmp (arg, "yes") == 0)
13657 omit_lock_prefix = 1;
13658 else if (strcasecmp (arg, "no") == 0)
13659 omit_lock_prefix = 0;
13660 else
13661 as_fatal (_("invalid -momit-lock-prefix= option: `%s'"), arg);
13662 break;
13663
e4e00185
AS
13664 case OPTION_MFENCE_AS_LOCK_ADD:
13665 if (strcasecmp (arg, "yes") == 0)
13666 avoid_fence = 1;
13667 else if (strcasecmp (arg, "no") == 0)
13668 avoid_fence = 0;
13669 else
13670 as_fatal (_("invalid -mfence-as-lock-add= option: `%s'"), arg);
13671 break;
13672
ae531041
L
13673 case OPTION_MLFENCE_AFTER_LOAD:
13674 if (strcasecmp (arg, "yes") == 0)
13675 lfence_after_load = 1;
13676 else if (strcasecmp (arg, "no") == 0)
13677 lfence_after_load = 0;
13678 else
13679 as_fatal (_("invalid -mlfence-after-load= option: `%s'"), arg);
13680 break;
13681
13682 case OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH:
13683 if (strcasecmp (arg, "all") == 0)
a09f656b 13684 {
13685 lfence_before_indirect_branch = lfence_branch_all;
13686 if (lfence_before_ret == lfence_before_ret_none)
13687 lfence_before_ret = lfence_before_ret_shl;
13688 }
ae531041
L
13689 else if (strcasecmp (arg, "memory") == 0)
13690 lfence_before_indirect_branch = lfence_branch_memory;
13691 else if (strcasecmp (arg, "register") == 0)
13692 lfence_before_indirect_branch = lfence_branch_register;
13693 else if (strcasecmp (arg, "none") == 0)
13694 lfence_before_indirect_branch = lfence_branch_none;
13695 else
13696 as_fatal (_("invalid -mlfence-before-indirect-branch= option: `%s'"),
13697 arg);
13698 break;
13699
13700 case OPTION_MLFENCE_BEFORE_RET:
13701 if (strcasecmp (arg, "or") == 0)
13702 lfence_before_ret = lfence_before_ret_or;
13703 else if (strcasecmp (arg, "not") == 0)
13704 lfence_before_ret = lfence_before_ret_not;
a09f656b 13705 else if (strcasecmp (arg, "shl") == 0 || strcasecmp (arg, "yes") == 0)
13706 lfence_before_ret = lfence_before_ret_shl;
ae531041
L
13707 else if (strcasecmp (arg, "none") == 0)
13708 lfence_before_ret = lfence_before_ret_none;
13709 else
13710 as_fatal (_("invalid -mlfence-before-ret= option: `%s'"),
13711 arg);
13712 break;
13713
0cb4071e
L
13714 case OPTION_MRELAX_RELOCATIONS:
13715 if (strcasecmp (arg, "yes") == 0)
13716 generate_relax_relocations = 1;
13717 else if (strcasecmp (arg, "no") == 0)
13718 generate_relax_relocations = 0;
13719 else
13720 as_fatal (_("invalid -mrelax-relocations= option: `%s'"), arg);
13721 break;
13722
e379e5f3
L
13723 case OPTION_MALIGN_BRANCH_BOUNDARY:
13724 {
13725 char *end;
13726 long int align = strtoul (arg, &end, 0);
13727 if (*end == '\0')
13728 {
13729 if (align == 0)
13730 {
13731 align_branch_power = 0;
13732 break;
13733 }
13734 else if (align >= 16)
13735 {
13736 int align_power;
13737 for (align_power = 0;
13738 (align & 1) == 0;
13739 align >>= 1, align_power++)
13740 continue;
13741 /* Limit alignment power to 31. */
13742 if (align == 1 && align_power < 32)
13743 {
13744 align_branch_power = align_power;
13745 break;
13746 }
13747 }
13748 }
13749 as_fatal (_("invalid -malign-branch-boundary= value: %s"), arg);
13750 }
13751 break;
13752
13753 case OPTION_MALIGN_BRANCH_PREFIX_SIZE:
13754 {
13755 char *end;
13756 int align = strtoul (arg, &end, 0);
13757 /* Some processors only support 5 prefixes. */
13758 if (*end == '\0' && align >= 0 && align < 6)
13759 {
13760 align_branch_prefix_size = align;
13761 break;
13762 }
13763 as_fatal (_("invalid -malign-branch-prefix-size= value: %s"),
13764 arg);
13765 }
13766 break;
13767
13768 case OPTION_MALIGN_BRANCH:
13769 align_branch = 0;
13770 saved = xstrdup (arg);
13771 type = saved;
13772 do
13773 {
13774 next = strchr (type, '+');
13775 if (next)
13776 *next++ = '\0';
13777 if (strcasecmp (type, "jcc") == 0)
13778 align_branch |= align_branch_jcc_bit;
13779 else if (strcasecmp (type, "fused") == 0)
13780 align_branch |= align_branch_fused_bit;
13781 else if (strcasecmp (type, "jmp") == 0)
13782 align_branch |= align_branch_jmp_bit;
13783 else if (strcasecmp (type, "call") == 0)
13784 align_branch |= align_branch_call_bit;
13785 else if (strcasecmp (type, "ret") == 0)
13786 align_branch |= align_branch_ret_bit;
13787 else if (strcasecmp (type, "indirect") == 0)
13788 align_branch |= align_branch_indirect_bit;
13789 else
13790 as_fatal (_("invalid -malign-branch= option: `%s'"), arg);
13791 type = next;
13792 }
13793 while (next != NULL);
13794 free (saved);
13795 break;
13796
76cf450b
L
13797 case OPTION_MBRANCHES_WITH_32B_BOUNDARIES:
13798 align_branch_power = 5;
13799 align_branch_prefix_size = 5;
13800 align_branch = (align_branch_jcc_bit
13801 | align_branch_fused_bit
13802 | align_branch_jmp_bit);
13803 break;
13804
5db04b09 13805 case OPTION_MAMD64:
4b5aaf5f 13806 isa64 = amd64;
5db04b09
L
13807 break;
13808
13809 case OPTION_MINTEL64:
4b5aaf5f 13810 isa64 = intel64;
5db04b09
L
13811 break;
13812
b6f8c7c4
L
13813 case 'O':
13814 if (arg == NULL)
13815 {
13816 optimize = 1;
13817 /* Turn off -Os. */
13818 optimize_for_space = 0;
13819 }
13820 else if (*arg == 's')
13821 {
13822 optimize_for_space = 1;
13823 /* Turn on all encoding optimizations. */
41fd2579 13824 optimize = INT_MAX;
b6f8c7c4
L
13825 }
13826 else
13827 {
13828 optimize = atoi (arg);
13829 /* Turn off -Os. */
13830 optimize_for_space = 0;
13831 }
13832 break;
13833
252b5132
RH
13834 default:
13835 return 0;
13836 }
13837 return 1;
13838}
13839
8a2c8fef
L
13840#define MESSAGE_TEMPLATE \
13841" "
13842
293f5f65
L
13843static char *
13844output_message (FILE *stream, char *p, char *message, char *start,
13845 int *left_p, const char *name, int len)
13846{
13847 int size = sizeof (MESSAGE_TEMPLATE);
13848 int left = *left_p;
13849
13850 /* Reserve 2 spaces for ", " or ",\0" */
13851 left -= len + 2;
13852
13853 /* Check if there is any room. */
13854 if (left >= 0)
13855 {
13856 if (p != start)
13857 {
13858 *p++ = ',';
13859 *p++ = ' ';
13860 }
13861 p = mempcpy (p, name, len);
13862 }
13863 else
13864 {
13865 /* Output the current message now and start a new one. */
13866 *p++ = ',';
13867 *p = '\0';
13868 fprintf (stream, "%s\n", message);
13869 p = start;
13870 left = size - (start - message) - len - 2;
13871
13872 gas_assert (left >= 0);
13873
13874 p = mempcpy (p, name, len);
13875 }
13876
13877 *left_p = left;
13878 return p;
13879}
13880
8a2c8fef 13881static void
1ded5609 13882show_arch (FILE *stream, int ext, int check)
8a2c8fef
L
13883{
13884 static char message[] = MESSAGE_TEMPLATE;
13885 char *start = message + 27;
13886 char *p;
13887 int size = sizeof (MESSAGE_TEMPLATE);
13888 int left;
13889 const char *name;
13890 int len;
13891 unsigned int j;
13892
13893 p = start;
13894 left = size - (start - message);
3ce2ebcf
JB
13895
13896 if (!ext && check)
13897 {
13898 p = output_message (stream, p, message, start, &left,
13899 STRING_COMMA_LEN ("default"));
f68697e8
JB
13900 p = output_message (stream, p, message, start, &left,
13901 STRING_COMMA_LEN ("push"));
13902 p = output_message (stream, p, message, start, &left,
13903 STRING_COMMA_LEN ("pop"));
3ce2ebcf
JB
13904 }
13905
8a2c8fef
L
13906 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13907 {
13908 /* Should it be skipped? */
13909 if (cpu_arch [j].skip)
13910 continue;
13911
13912 name = cpu_arch [j].name;
13913 len = cpu_arch [j].len;
ae89daec 13914 if (cpu_arch[j].type == PROCESSOR_NONE)
8a2c8fef
L
13915 {
13916 /* It is an extension. Skip if we aren't asked to show it. */
ae89daec 13917 if (!ext || cpu_flags_all_zero (&cpu_arch[j].enable))
8a2c8fef
L
13918 continue;
13919 }
13920 else if (ext)
13921 {
13922 /* It is an processor. Skip if we show only extension. */
13923 continue;
13924 }
ae89daec 13925 else if (check && ! cpu_arch[j].enable.bitfield.cpui386)
1ded5609
JB
13926 {
13927 /* It is an impossible processor - skip. */
13928 continue;
13929 }
8a2c8fef 13930
293f5f65 13931 p = output_message (stream, p, message, start, &left, name, len);
8a2c8fef
L
13932 }
13933
293f5f65
L
13934 /* Display disabled extensions. */
13935 if (ext)
ae89daec 13936 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
293f5f65 13937 {
ae89daec
JB
13938 char *str;
13939
13940 if (cpu_arch[j].type != PROCESSOR_NONE
13941 || !cpu_flags_all_zero (&cpu_arch[j].enable))
13942 continue;
13943 str = xasprintf ("no%s", cpu_arch[j].name);
13944 p = output_message (stream, p, message, start, &left, str,
13945 strlen (str));
13946 free (str);
293f5f65
L
13947 }
13948
8a2c8fef
L
13949 *p = '\0';
13950 fprintf (stream, "%s\n", message);
13951}
13952
252b5132 13953void
8a2c8fef 13954md_show_usage (FILE *stream)
252b5132 13955{
4cc782b5
ILT
13956#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13957 fprintf (stream, _("\
d4693039 13958 -Qy, -Qn ignored\n\
a38cf1db 13959 -V print assembler version number\n\
b3b91714
AM
13960 -k ignored\n"));
13961#endif
13962 fprintf (stream, _("\
7ebd68d1
NC
13963 -n do not optimize code alignment\n\
13964 -O{012s} attempt some code optimizations\n\
b3b91714
AM
13965 -q quieten some warnings\n"));
13966#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13967 fprintf (stream, _("\
a38cf1db 13968 -s ignored\n"));
b3b91714 13969#endif
b00af7c8
JB
13970#ifdef BFD64
13971# if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13972 fprintf (stream, _("\
13973 --32/--64/--x32 generate 32bit/64bit/x32 object\n"));
13974# elif defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O)
751d281c 13975 fprintf (stream, _("\
b00af7c8
JB
13976 --32/--64 generate 32bit/64bit object\n"));
13977# endif
751d281c 13978#endif
b3b91714
AM
13979#ifdef SVR4_COMMENT_CHARS
13980 fprintf (stream, _("\
13981 --divide do not treat `/' as a comment character\n"));
a38cf1db
AM
13982#else
13983 fprintf (stream, _("\
b3b91714 13984 --divide ignored\n"));
4cc782b5 13985#endif
9103f4f4 13986 fprintf (stream, _("\
6305a203 13987 -march=CPU[,+EXTENSION...]\n\
8a2c8fef 13988 generate code for CPU and EXTENSION, CPU is one of:\n"));
1ded5609 13989 show_arch (stream, 0, 1);
8a2c8fef 13990 fprintf (stream, _("\
ae89daec 13991 EXTENSION is combination of (possibly \"no\"-prefixed):\n"));
1ded5609 13992 show_arch (stream, 1, 0);
6305a203 13993 fprintf (stream, _("\
8a2c8fef 13994 -mtune=CPU optimize for CPU, CPU is one of:\n"));
1ded5609 13995 show_arch (stream, 0, 0);
ba104c83 13996 fprintf (stream, _("\
c0f3af97
L
13997 -msse2avx encode SSE instructions with VEX prefix\n"));
13998 fprintf (stream, _("\
c8480b58
L
13999 -muse-unaligned-vector-move\n\
14000 encode aligned vector move as unaligned vector move\n"));
14001 fprintf (stream, _("\
7c5c05ef 14002 -msse-check=[none|error|warning] (default: warning)\n\
daf50ae7
L
14003 check SSE instructions\n"));
14004 fprintf (stream, _("\
7c5c05ef 14005 -moperand-check=[none|error|warning] (default: warning)\n\
7bab8ab5
JB
14006 check operand combinations for validity\n"));
14007 fprintf (stream, _("\
7c5c05ef
L
14008 -mavxscalar=[128|256] (default: 128)\n\
14009 encode scalar AVX instructions with specific vector\n\
539f890d
L
14010 length\n"));
14011 fprintf (stream, _("\
03751133
L
14012 -mvexwig=[0|1] (default: 0)\n\
14013 encode VEX instructions with specific VEX.W value\n\
14014 for VEX.W bit ignored instructions\n"));
14015 fprintf (stream, _("\
7c5c05ef
L
14016 -mevexlig=[128|256|512] (default: 128)\n\
14017 encode scalar EVEX instructions with specific vector\n\
43234a1e
L
14018 length\n"));
14019 fprintf (stream, _("\
7c5c05ef
L
14020 -mevexwig=[0|1] (default: 0)\n\
14021 encode EVEX instructions with specific EVEX.W value\n\
43234a1e
L
14022 for EVEX.W bit ignored instructions\n"));
14023 fprintf (stream, _("\
7c5c05ef 14024 -mevexrcig=[rne|rd|ru|rz] (default: rne)\n\
d3d3c6db
IT
14025 encode EVEX instructions with specific EVEX.RC value\n\
14026 for SAE-only ignored instructions\n"));
14027 fprintf (stream, _("\
7c5c05ef
L
14028 -mmnemonic=[att|intel] "));
14029 if (SYSV386_COMPAT)
14030 fprintf (stream, _("(default: att)\n"));
14031 else
14032 fprintf (stream, _("(default: intel)\n"));
14033 fprintf (stream, _("\
14034 use AT&T/Intel mnemonic\n"));
ba104c83 14035 fprintf (stream, _("\
7c5c05ef
L
14036 -msyntax=[att|intel] (default: att)\n\
14037 use AT&T/Intel syntax\n"));
ba104c83
L
14038 fprintf (stream, _("\
14039 -mindex-reg support pseudo index registers\n"));
14040 fprintf (stream, _("\
14041 -mnaked-reg don't require `%%' prefix for registers\n"));
14042 fprintf (stream, _("\
7e8b059b 14043 -madd-bnd-prefix add BND prefix for all valid branches\n"));
b4a3a7b4 14044#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8dcea932
L
14045 fprintf (stream, _("\
14046 -mshared disable branch optimization for shared code\n"));
b4a3a7b4
L
14047 fprintf (stream, _("\
14048 -mx86-used-note=[no|yes] "));
14049 if (DEFAULT_X86_USED_NOTE)
14050 fprintf (stream, _("(default: yes)\n"));
14051 else
14052 fprintf (stream, _("(default: no)\n"));
14053 fprintf (stream, _("\
14054 generate x86 used ISA and feature properties\n"));
14055#endif
14056#if defined (TE_PE) || defined (TE_PEP)
167ad85b
TG
14057 fprintf (stream, _("\
14058 -mbig-obj generate big object files\n"));
14059#endif
d022bddd 14060 fprintf (stream, _("\
7c5c05ef 14061 -momit-lock-prefix=[no|yes] (default: no)\n\
d022bddd 14062 strip all lock prefixes\n"));
5db04b09 14063 fprintf (stream, _("\
7c5c05ef 14064 -mfence-as-lock-add=[no|yes] (default: no)\n\
e4e00185
AS
14065 encode lfence, mfence and sfence as\n\
14066 lock addl $0x0, (%%{re}sp)\n"));
14067 fprintf (stream, _("\
7c5c05ef
L
14068 -mrelax-relocations=[no|yes] "));
14069 if (DEFAULT_GENERATE_X86_RELAX_RELOCATIONS)
14070 fprintf (stream, _("(default: yes)\n"));
14071 else
14072 fprintf (stream, _("(default: no)\n"));
14073 fprintf (stream, _("\
0cb4071e
L
14074 generate relax relocations\n"));
14075 fprintf (stream, _("\
e379e5f3
L
14076 -malign-branch-boundary=NUM (default: 0)\n\
14077 align branches within NUM byte boundary\n"));
14078 fprintf (stream, _("\
14079 -malign-branch=TYPE[+TYPE...] (default: jcc+fused+jmp)\n\
14080 TYPE is combination of jcc, fused, jmp, call, ret,\n\
14081 indirect\n\
14082 specify types of branches to align\n"));
14083 fprintf (stream, _("\
14084 -malign-branch-prefix-size=NUM (default: 5)\n\
14085 align branches with NUM prefixes per instruction\n"));
14086 fprintf (stream, _("\
76cf450b
L
14087 -mbranches-within-32B-boundaries\n\
14088 align branches within 32 byte boundary\n"));
14089 fprintf (stream, _("\
ae531041
L
14090 -mlfence-after-load=[no|yes] (default: no)\n\
14091 generate lfence after load\n"));
14092 fprintf (stream, _("\
14093 -mlfence-before-indirect-branch=[none|all|register|memory] (default: none)\n\
14094 generate lfence before indirect near branch\n"));
14095 fprintf (stream, _("\
a09f656b 14096 -mlfence-before-ret=[none|or|not|shl|yes] (default: none)\n\
ae531041
L
14097 generate lfence before ret\n"));
14098 fprintf (stream, _("\
7c5c05ef 14099 -mamd64 accept only AMD64 ISA [default]\n"));
5db04b09
L
14100 fprintf (stream, _("\
14101 -mintel64 accept only Intel64 ISA\n"));
252b5132
RH
14102}
14103
3e73aa7c 14104#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
321098a5 14105 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
e57f8c65 14106 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
252b5132
RH
14107
14108/* Pick the target format to use. */
14109
47926f60 14110const char *
e3bb37b5 14111i386_target_format (void)
252b5132 14112{
d34049e8 14113 if (startswith (default_arch, "x86_64"))
351f65ca
L
14114 {
14115 update_code_flag (CODE_64BIT, 1);
14116 if (default_arch[6] == '\0')
7f56bc95 14117 x86_elf_abi = X86_64_ABI;
351f65ca 14118 else
7f56bc95 14119 x86_elf_abi = X86_64_X32_ABI;
351f65ca 14120 }
3e73aa7c 14121 else if (!strcmp (default_arch, "i386"))
78f12dd3 14122 update_code_flag (CODE_32BIT, 1);
5197d474
L
14123 else if (!strcmp (default_arch, "iamcu"))
14124 {
14125 update_code_flag (CODE_32BIT, 1);
14126 if (cpu_arch_isa == PROCESSOR_UNKNOWN)
14127 {
14128 static const i386_cpu_flags iamcu_flags = CPU_IAMCU_FLAGS;
14129 cpu_arch_name = "iamcu";
d92c7521 14130 free (cpu_sub_arch_name);
5197d474
L
14131 cpu_sub_arch_name = NULL;
14132 cpu_arch_flags = iamcu_flags;
14133 cpu_arch_isa = PROCESSOR_IAMCU;
14134 cpu_arch_isa_flags = iamcu_flags;
14135 if (!cpu_arch_tune_set)
14136 {
14137 cpu_arch_tune = cpu_arch_isa;
14138 cpu_arch_tune_flags = cpu_arch_isa_flags;
14139 }
14140 }
8d471ec1 14141 else if (cpu_arch_isa != PROCESSOR_IAMCU)
5197d474
L
14142 as_fatal (_("Intel MCU doesn't support `%s' architecture"),
14143 cpu_arch_name);
14144 }
3e73aa7c 14145 else
2b5d6a91 14146 as_fatal (_("unknown architecture"));
89507696
JB
14147
14148 if (cpu_flags_all_zero (&cpu_arch_isa_flags))
ae89daec 14149 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
89507696 14150 if (cpu_flags_all_zero (&cpu_arch_tune_flags))
ae89daec 14151 cpu_arch_tune_flags = cpu_arch[flag_code == CODE_64BIT].enable;
89507696 14152
252b5132
RH
14153 switch (OUTPUT_FLAVOR)
14154 {
9384f2ff 14155#if defined (OBJ_MAYBE_AOUT) || defined (OBJ_AOUT)
4c63da97 14156 case bfd_target_aout_flavour:
47926f60 14157 return AOUT_TARGET_FORMAT;
4c63da97 14158#endif
9384f2ff
AM
14159#if defined (OBJ_MAYBE_COFF) || defined (OBJ_COFF)
14160# if defined (TE_PE) || defined (TE_PEP)
14161 case bfd_target_coff_flavour:
167ad85b 14162 if (flag_code == CODE_64BIT)
eb19308f
JB
14163 {
14164 object_64bit = 1;
14165 return use_big_obj ? "pe-bigobj-x86-64" : "pe-x86-64";
14166 }
14167 return use_big_obj ? "pe-bigobj-i386" : "pe-i386";
9384f2ff 14168# elif defined (TE_GO32)
0561d57c
JK
14169 case bfd_target_coff_flavour:
14170 return "coff-go32";
9384f2ff 14171# else
252b5132
RH
14172 case bfd_target_coff_flavour:
14173 return "coff-i386";
9384f2ff 14174# endif
4c63da97 14175#endif
3e73aa7c 14176#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
252b5132 14177 case bfd_target_elf_flavour:
3e73aa7c 14178 {
351f65ca
L
14179 const char *format;
14180
14181 switch (x86_elf_abi)
4fa24527 14182 {
351f65ca
L
14183 default:
14184 format = ELF_TARGET_FORMAT;
e379e5f3
L
14185#ifndef TE_SOLARIS
14186 tls_get_addr = "___tls_get_addr";
14187#endif
351f65ca 14188 break;
7f56bc95 14189 case X86_64_ABI:
351f65ca 14190 use_rela_relocations = 1;
4fa24527 14191 object_64bit = 1;
e379e5f3
L
14192#ifndef TE_SOLARIS
14193 tls_get_addr = "__tls_get_addr";
14194#endif
351f65ca
L
14195 format = ELF_TARGET_FORMAT64;
14196 break;
7f56bc95 14197 case X86_64_X32_ABI:
4fa24527 14198 use_rela_relocations = 1;
351f65ca 14199 object_64bit = 1;
e379e5f3
L
14200#ifndef TE_SOLARIS
14201 tls_get_addr = "__tls_get_addr";
14202#endif
862be3fb 14203 disallow_64bit_reloc = 1;
351f65ca
L
14204 format = ELF_TARGET_FORMAT32;
14205 break;
4fa24527 14206 }
c085ab00 14207 if (cpu_arch_isa == PROCESSOR_IAMCU)
81486035
L
14208 {
14209 if (x86_elf_abi != I386_ABI)
14210 as_fatal (_("Intel MCU is 32bit only"));
14211 return ELF_TARGET_IAMCU_FORMAT;
14212 }
8a9036a4 14213 else
351f65ca 14214 return format;
3e73aa7c 14215 }
e57f8c65
TG
14216#endif
14217#if defined (OBJ_MACH_O)
14218 case bfd_target_mach_o_flavour:
d382c579
TG
14219 if (flag_code == CODE_64BIT)
14220 {
14221 use_rela_relocations = 1;
14222 object_64bit = 1;
14223 return "mach-o-x86-64";
14224 }
14225 else
14226 return "mach-o-i386";
4c63da97 14227#endif
252b5132
RH
14228 default:
14229 abort ();
14230 return NULL;
14231 }
14232}
14233
47926f60 14234#endif /* OBJ_MAYBE_ more than one */
252b5132 14235\f
252b5132 14236symbolS *
7016a5d5 14237md_undefined_symbol (char *name)
252b5132 14238{
18dc2407
ILT
14239 if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
14240 && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
14241 && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
14242 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
24eab124
AM
14243 {
14244 if (!GOT_symbol)
14245 {
14246 if (symbol_find (name))
14247 as_bad (_("GOT already in symbol table"));
14248 GOT_symbol = symbol_new (name, undefined_section,
e01e1cee 14249 &zero_address_frag, 0);
24eab124
AM
14250 };
14251 return GOT_symbol;
14252 }
252b5132
RH
14253 return 0;
14254}
14255
14256/* Round up a section size to the appropriate boundary. */
47926f60 14257
252b5132 14258valueT
7016a5d5 14259md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
252b5132 14260{
4c63da97
AM
14261#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
14262 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
14263 {
14264 /* For a.out, force the section size to be aligned. If we don't do
14265 this, BFD will align it for us, but it will not write out the
14266 final bytes of the section. This may be a bug in BFD, but it is
14267 easier to fix it here since that is how the other a.out targets
14268 work. */
14269 int align;
14270
fd361982 14271 align = bfd_section_alignment (segment);
8d3842cd 14272 size = ((size + (1 << align) - 1) & (-((valueT) 1 << align)));
4c63da97 14273 }
252b5132
RH
14274#endif
14275
14276 return size;
14277}
14278
14279/* On the i386, PC-relative offsets are relative to the start of the
14280 next instruction. That is, the address of the offset, plus its
14281 size, since the offset is always the last part of the insn. */
14282
14283long
e3bb37b5 14284md_pcrel_from (fixS *fixP)
252b5132
RH
14285{
14286 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
14287}
14288
14289#ifndef I386COFF
14290
14291static void
e3bb37b5 14292s_bss (int ignore ATTRIBUTE_UNUSED)
252b5132 14293{
29b0f896 14294 int temp;
252b5132 14295
8a75718c
JB
14296#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14297 if (IS_ELF)
14298 obj_elf_section_change_hook ();
14299#endif
252b5132
RH
14300 temp = get_absolute_expression ();
14301 subseg_set (bss_section, (subsegT) temp);
14302 demand_empty_rest_of_line ();
14303}
14304
14305#endif
14306
e379e5f3
L
14307/* Remember constant directive. */
14308
14309void
14310i386_cons_align (int ignore ATTRIBUTE_UNUSED)
14311{
14312 if (last_insn.kind != last_insn_directive
14313 && (bfd_section_flags (now_seg) & SEC_CODE))
14314 {
14315 last_insn.seg = now_seg;
14316 last_insn.kind = last_insn_directive;
14317 last_insn.name = "constant directive";
14318 last_insn.file = as_where (&last_insn.line);
ae531041
L
14319 if (lfence_before_ret != lfence_before_ret_none)
14320 {
14321 if (lfence_before_indirect_branch != lfence_branch_none)
14322 as_warn (_("constant directive skips -mlfence-before-ret "
14323 "and -mlfence-before-indirect-branch"));
14324 else
14325 as_warn (_("constant directive skips -mlfence-before-ret"));
14326 }
14327 else if (lfence_before_indirect_branch != lfence_branch_none)
14328 as_warn (_("constant directive skips -mlfence-before-indirect-branch"));
e379e5f3
L
14329 }
14330}
14331
3abbafc2 14332int
e3bb37b5 14333i386_validate_fix (fixS *fixp)
252b5132 14334{
e52a16f2
JB
14335 if (fixp->fx_addsy && S_GET_SEGMENT(fixp->fx_addsy) == reg_section)
14336 {
14337 reloc_howto_type *howto;
14338
14339 howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
14340 as_bad_where (fixp->fx_file, fixp->fx_line,
14341 _("invalid %s relocation against register"),
14342 howto ? howto->name : "<unknown>");
14343 return 0;
14344 }
14345
3abbafc2
JB
14346#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14347 if (fixp->fx_r_type == BFD_RELOC_SIZE32
14348 || fixp->fx_r_type == BFD_RELOC_SIZE64)
14349 return IS_ELF && fixp->fx_addsy
14350 && (!S_IS_DEFINED (fixp->fx_addsy)
14351 || S_IS_EXTERNAL (fixp->fx_addsy));
14352#endif
14353
02a86693 14354 if (fixp->fx_subsy)
252b5132 14355 {
02a86693 14356 if (fixp->fx_subsy == GOT_symbol)
23df1078 14357 {
02a86693
L
14358 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
14359 {
14360 if (!object_64bit)
14361 abort ();
14362#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14363 if (fixp->fx_tcbit2)
56ceb5b5
L
14364 fixp->fx_r_type = (fixp->fx_tcbit
14365 ? BFD_RELOC_X86_64_REX_GOTPCRELX
14366 : BFD_RELOC_X86_64_GOTPCRELX);
02a86693
L
14367 else
14368#endif
14369 fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
14370 }
d6ab8113 14371 else
02a86693
L
14372 {
14373 if (!object_64bit)
14374 fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
14375 else
14376 fixp->fx_r_type = BFD_RELOC_X86_64_GOTOFF64;
14377 }
14378 fixp->fx_subsy = 0;
23df1078 14379 }
252b5132 14380 }
02a86693 14381#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2585b7a5 14382 else
02a86693 14383 {
2585b7a5
L
14384 /* NB: Commit 292676c1 resolved PLT32 reloc aganst local symbol
14385 to section. Since PLT32 relocation must be against symbols,
14386 turn such PLT32 relocation into PC32 relocation. */
14387 if (fixp->fx_addsy
14388 && (fixp->fx_r_type == BFD_RELOC_386_PLT32
14389 || fixp->fx_r_type == BFD_RELOC_X86_64_PLT32)
14390 && symbol_section_p (fixp->fx_addsy))
14391 fixp->fx_r_type = BFD_RELOC_32_PCREL;
14392 if (!object_64bit)
14393 {
14394 if (fixp->fx_r_type == BFD_RELOC_386_GOT32
14395 && fixp->fx_tcbit2)
14396 fixp->fx_r_type = BFD_RELOC_386_GOT32X;
14397 }
02a86693
L
14398 }
14399#endif
3abbafc2
JB
14400
14401 return 1;
252b5132
RH
14402}
14403
252b5132 14404arelent *
7016a5d5 14405tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
14406{
14407 arelent *rel;
14408 bfd_reloc_code_real_type code;
14409
14410 switch (fixp->fx_r_type)
14411 {
8ce3d284 14412#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3abbafc2
JB
14413 symbolS *sym;
14414
8fd4256d
L
14415 case BFD_RELOC_SIZE32:
14416 case BFD_RELOC_SIZE64:
3abbafc2
JB
14417 if (fixp->fx_addsy
14418 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))
14419 && (!fixp->fx_subsy
14420 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))))
14421 sym = fixp->fx_addsy;
14422 else if (fixp->fx_subsy
14423 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))
14424 && (!fixp->fx_addsy
14425 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))))
14426 sym = fixp->fx_subsy;
14427 else
14428 sym = NULL;
14429 if (IS_ELF && sym && S_IS_DEFINED (sym) && !S_IS_EXTERNAL (sym))
8fd4256d
L
14430 {
14431 /* Resolve size relocation against local symbol to size of
14432 the symbol plus addend. */
3abbafc2 14433 valueT value = S_GET_SIZE (sym);
44f87162 14434
3abbafc2
JB
14435 if (symbol_get_bfdsym (sym)->flags & BSF_SECTION_SYM)
14436 value = bfd_section_size (S_GET_SEGMENT (sym));
14437 if (sym == fixp->fx_subsy)
14438 {
14439 value = -value;
14440 if (fixp->fx_addsy)
14441 value += S_GET_VALUE (fixp->fx_addsy);
14442 }
14443 else if (fixp->fx_subsy)
14444 value -= S_GET_VALUE (fixp->fx_subsy);
44f87162 14445 value += fixp->fx_offset;
8fd4256d 14446 if (fixp->fx_r_type == BFD_RELOC_SIZE32
d965814f 14447 && object_64bit
8fd4256d
L
14448 && !fits_in_unsigned_long (value))
14449 as_bad_where (fixp->fx_file, fixp->fx_line,
14450 _("symbol size computation overflow"));
14451 fixp->fx_addsy = NULL;
14452 fixp->fx_subsy = NULL;
14453 md_apply_fix (fixp, (valueT *) &value, NULL);
14454 return NULL;
14455 }
3abbafc2
JB
14456 if (!fixp->fx_addsy || fixp->fx_subsy)
14457 {
14458 as_bad_where (fixp->fx_file, fixp->fx_line,
14459 "unsupported expression involving @size");
14460 return NULL;
14461 }
8ce3d284 14462#endif
1a0670f3 14463 /* Fall through. */
8fd4256d 14464
3e73aa7c
JH
14465 case BFD_RELOC_X86_64_PLT32:
14466 case BFD_RELOC_X86_64_GOT32:
14467 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
14468 case BFD_RELOC_X86_64_GOTPCRELX:
14469 case BFD_RELOC_X86_64_REX_GOTPCRELX:
252b5132
RH
14470 case BFD_RELOC_386_PLT32:
14471 case BFD_RELOC_386_GOT32:
02a86693 14472 case BFD_RELOC_386_GOT32X:
252b5132
RH
14473 case BFD_RELOC_386_GOTOFF:
14474 case BFD_RELOC_386_GOTPC:
13ae64f3
JJ
14475 case BFD_RELOC_386_TLS_GD:
14476 case BFD_RELOC_386_TLS_LDM:
14477 case BFD_RELOC_386_TLS_LDO_32:
14478 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
14479 case BFD_RELOC_386_TLS_IE:
14480 case BFD_RELOC_386_TLS_GOTIE:
13ae64f3
JJ
14481 case BFD_RELOC_386_TLS_LE_32:
14482 case BFD_RELOC_386_TLS_LE:
67a4f2b7
AO
14483 case BFD_RELOC_386_TLS_GOTDESC:
14484 case BFD_RELOC_386_TLS_DESC_CALL:
bffbf940
JJ
14485 case BFD_RELOC_X86_64_TLSGD:
14486 case BFD_RELOC_X86_64_TLSLD:
14487 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 14488 case BFD_RELOC_X86_64_DTPOFF64:
bffbf940
JJ
14489 case BFD_RELOC_X86_64_GOTTPOFF:
14490 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113
JB
14491 case BFD_RELOC_X86_64_TPOFF64:
14492 case BFD_RELOC_X86_64_GOTOFF64:
14493 case BFD_RELOC_X86_64_GOTPC32:
7b81dfbb
AJ
14494 case BFD_RELOC_X86_64_GOT64:
14495 case BFD_RELOC_X86_64_GOTPCREL64:
14496 case BFD_RELOC_X86_64_GOTPC64:
14497 case BFD_RELOC_X86_64_GOTPLT64:
14498 case BFD_RELOC_X86_64_PLTOFF64:
67a4f2b7
AO
14499 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14500 case BFD_RELOC_X86_64_TLSDESC_CALL:
252b5132
RH
14501 case BFD_RELOC_RVA:
14502 case BFD_RELOC_VTABLE_ENTRY:
14503 case BFD_RELOC_VTABLE_INHERIT:
6482c264
NC
14504#ifdef TE_PE
14505 case BFD_RELOC_32_SECREL:
145667f8 14506 case BFD_RELOC_16_SECIDX:
6482c264 14507#endif
252b5132
RH
14508 code = fixp->fx_r_type;
14509 break;
dbbaec26
L
14510 case BFD_RELOC_X86_64_32S:
14511 if (!fixp->fx_pcrel)
14512 {
14513 /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32. */
14514 code = fixp->fx_r_type;
14515 break;
14516 }
1a0670f3 14517 /* Fall through. */
252b5132 14518 default:
93382f6d 14519 if (fixp->fx_pcrel)
252b5132 14520 {
93382f6d
AM
14521 switch (fixp->fx_size)
14522 {
14523 default:
b091f402
AM
14524 as_bad_where (fixp->fx_file, fixp->fx_line,
14525 _("can not do %d byte pc-relative relocation"),
14526 fixp->fx_size);
93382f6d
AM
14527 code = BFD_RELOC_32_PCREL;
14528 break;
14529 case 1: code = BFD_RELOC_8_PCREL; break;
14530 case 2: code = BFD_RELOC_16_PCREL; break;
d258b828 14531 case 4: code = BFD_RELOC_32_PCREL; break;
d6ab8113
JB
14532#ifdef BFD64
14533 case 8: code = BFD_RELOC_64_PCREL; break;
14534#endif
93382f6d
AM
14535 }
14536 }
14537 else
14538 {
14539 switch (fixp->fx_size)
14540 {
14541 default:
b091f402
AM
14542 as_bad_where (fixp->fx_file, fixp->fx_line,
14543 _("can not do %d byte relocation"),
14544 fixp->fx_size);
93382f6d
AM
14545 code = BFD_RELOC_32;
14546 break;
14547 case 1: code = BFD_RELOC_8; break;
14548 case 2: code = BFD_RELOC_16; break;
14549 case 4: code = BFD_RELOC_32; break;
937149dd 14550#ifdef BFD64
3e73aa7c 14551 case 8: code = BFD_RELOC_64; break;
937149dd 14552#endif
93382f6d 14553 }
252b5132
RH
14554 }
14555 break;
14556 }
252b5132 14557
d182319b
JB
14558 if ((code == BFD_RELOC_32
14559 || code == BFD_RELOC_32_PCREL
14560 || code == BFD_RELOC_X86_64_32S)
252b5132
RH
14561 && GOT_symbol
14562 && fixp->fx_addsy == GOT_symbol)
3e73aa7c 14563 {
4fa24527 14564 if (!object_64bit)
d6ab8113
JB
14565 code = BFD_RELOC_386_GOTPC;
14566 else
14567 code = BFD_RELOC_X86_64_GOTPC32;
3e73aa7c 14568 }
7b81dfbb
AJ
14569 if ((code == BFD_RELOC_64 || code == BFD_RELOC_64_PCREL)
14570 && GOT_symbol
14571 && fixp->fx_addsy == GOT_symbol)
14572 {
14573 code = BFD_RELOC_X86_64_GOTPC64;
14574 }
252b5132 14575
add39d23
TS
14576 rel = XNEW (arelent);
14577 rel->sym_ptr_ptr = XNEW (asymbol *);
49309057 14578 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
14579
14580 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
c87db184 14581
3e73aa7c
JH
14582 if (!use_rela_relocations)
14583 {
14584 /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
14585 vtable entry to be used in the relocation's section offset. */
14586 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
14587 rel->address = fixp->fx_offset;
fbeb56a4
DK
14588#if defined (OBJ_COFF) && defined (TE_PE)
14589 else if (fixp->fx_addsy && S_IS_WEAK (fixp->fx_addsy))
14590 rel->addend = fixp->fx_addnumber - (S_GET_VALUE (fixp->fx_addsy) * 2);
14591 else
14592#endif
c6682705 14593 rel->addend = 0;
3e73aa7c
JH
14594 }
14595 /* Use the rela in 64bit mode. */
252b5132 14596 else
3e73aa7c 14597 {
862be3fb
L
14598 if (disallow_64bit_reloc)
14599 switch (code)
14600 {
862be3fb
L
14601 case BFD_RELOC_X86_64_DTPOFF64:
14602 case BFD_RELOC_X86_64_TPOFF64:
14603 case BFD_RELOC_64_PCREL:
14604 case BFD_RELOC_X86_64_GOTOFF64:
14605 case BFD_RELOC_X86_64_GOT64:
14606 case BFD_RELOC_X86_64_GOTPCREL64:
14607 case BFD_RELOC_X86_64_GOTPC64:
14608 case BFD_RELOC_X86_64_GOTPLT64:
14609 case BFD_RELOC_X86_64_PLTOFF64:
14610 as_bad_where (fixp->fx_file, fixp->fx_line,
14611 _("cannot represent relocation type %s in x32 mode"),
14612 bfd_get_reloc_code_name (code));
14613 break;
14614 default:
14615 break;
14616 }
14617
062cd5e7
AS
14618 if (!fixp->fx_pcrel)
14619 rel->addend = fixp->fx_offset;
14620 else
14621 switch (code)
14622 {
14623 case BFD_RELOC_X86_64_PLT32:
14624 case BFD_RELOC_X86_64_GOT32:
14625 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
14626 case BFD_RELOC_X86_64_GOTPCRELX:
14627 case BFD_RELOC_X86_64_REX_GOTPCRELX:
bffbf940
JJ
14628 case BFD_RELOC_X86_64_TLSGD:
14629 case BFD_RELOC_X86_64_TLSLD:
14630 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7
AO
14631 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14632 case BFD_RELOC_X86_64_TLSDESC_CALL:
062cd5e7
AS
14633 rel->addend = fixp->fx_offset - fixp->fx_size;
14634 break;
14635 default:
14636 rel->addend = (section->vma
14637 - fixp->fx_size
14638 + fixp->fx_addnumber
14639 + md_pcrel_from (fixp));
14640 break;
14641 }
3e73aa7c
JH
14642 }
14643
252b5132
RH
14644 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
14645 if (rel->howto == NULL)
14646 {
14647 as_bad_where (fixp->fx_file, fixp->fx_line,
d0b47220 14648 _("cannot represent relocation type %s"),
252b5132
RH
14649 bfd_get_reloc_code_name (code));
14650 /* Set howto to a garbage value so that we can keep going. */
14651 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
9c2799c2 14652 gas_assert (rel->howto != NULL);
252b5132
RH
14653 }
14654
14655 return rel;
14656}
14657
ee86248c 14658#include "tc-i386-intel.c"
54cfded0 14659
a60de03c
JB
14660void
14661tc_x86_parse_to_dw2regnum (expressionS *exp)
54cfded0 14662{
a60de03c
JB
14663 int saved_naked_reg;
14664 char saved_register_dot;
54cfded0 14665
a60de03c
JB
14666 saved_naked_reg = allow_naked_reg;
14667 allow_naked_reg = 1;
14668 saved_register_dot = register_chars['.'];
14669 register_chars['.'] = '.';
14670 allow_pseudo_reg = 1;
14671 expression_and_evaluate (exp);
14672 allow_pseudo_reg = 0;
14673 register_chars['.'] = saved_register_dot;
14674 allow_naked_reg = saved_naked_reg;
14675
e96d56a1 14676 if (exp->X_op == O_register && exp->X_add_number >= 0)
54cfded0 14677 {
a60de03c
JB
14678 if ((addressT) exp->X_add_number < i386_regtab_size)
14679 {
14680 exp->X_op = O_constant;
14681 exp->X_add_number = i386_regtab[exp->X_add_number]
14682 .dw2_regnum[flag_code >> 1];
14683 }
14684 else
14685 exp->X_op = O_illegal;
54cfded0 14686 }
54cfded0
AM
14687}
14688
14689void
14690tc_x86_frame_initial_instructions (void)
14691{
a60de03c
JB
14692 static unsigned int sp_regno[2];
14693
14694 if (!sp_regno[flag_code >> 1])
14695 {
14696 char *saved_input = input_line_pointer;
14697 char sp[][4] = {"esp", "rsp"};
14698 expressionS exp;
a4447b93 14699
a60de03c
JB
14700 input_line_pointer = sp[flag_code >> 1];
14701 tc_x86_parse_to_dw2regnum (&exp);
9c2799c2 14702 gas_assert (exp.X_op == O_constant);
a60de03c
JB
14703 sp_regno[flag_code >> 1] = exp.X_add_number;
14704 input_line_pointer = saved_input;
14705 }
a4447b93 14706
61ff971f
L
14707 cfi_add_CFA_def_cfa (sp_regno[flag_code >> 1], -x86_cie_data_alignment);
14708 cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
54cfded0 14709}
d2b2c203 14710
d7921315
L
14711int
14712x86_dwarf2_addr_size (void)
14713{
14714#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
14715 if (x86_elf_abi == X86_64_X32_ABI)
14716 return 4;
14717#endif
14718 return bfd_arch_bits_per_address (stdoutput) / 8;
14719}
14720
d2b2c203
DJ
14721int
14722i386_elf_section_type (const char *str, size_t len)
14723{
14724 if (flag_code == CODE_64BIT
14725 && len == sizeof ("unwind") - 1
d34049e8 14726 && startswith (str, "unwind"))
d2b2c203
DJ
14727 return SHT_X86_64_UNWIND;
14728
14729 return -1;
14730}
bb41ade5 14731
ad5fec3b
EB
14732#ifdef TE_SOLARIS
14733void
14734i386_solaris_fix_up_eh_frame (segT sec)
14735{
14736 if (flag_code == CODE_64BIT)
14737 elf_section_type (sec) = SHT_X86_64_UNWIND;
14738}
14739#endif
14740
bb41ade5
AM
14741#ifdef TE_PE
14742void
14743tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
14744{
91d6fa6a 14745 expressionS exp;
bb41ade5 14746
91d6fa6a
NC
14747 exp.X_op = O_secrel;
14748 exp.X_add_symbol = symbol;
14749 exp.X_add_number = 0;
14750 emit_expr (&exp, size);
bb41ade5
AM
14751}
14752#endif
3b22753a
L
14753
14754#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14755/* For ELF on x86-64, add support for SHF_X86_64_LARGE. */
14756
01e1a5bc 14757bfd_vma
6d4af3c2 14758x86_64_section_letter (int letter, const char **ptr_msg)
3b22753a
L
14759{
14760 if (flag_code == CODE_64BIT)
14761 {
14762 if (letter == 'l')
14763 return SHF_X86_64_LARGE;
14764
8f3bae45 14765 *ptr_msg = _("bad .section directive: want a,l,w,x,M,S,G,T in string");
64e74474 14766 }
3b22753a 14767 else
8f3bae45 14768 *ptr_msg = _("bad .section directive: want a,w,x,M,S,G,T in string");
3b22753a
L
14769 return -1;
14770}
14771
01e1a5bc 14772bfd_vma
3b22753a
L
14773x86_64_section_word (char *str, size_t len)
14774{
08dedd66 14775 if (len == 5 && flag_code == CODE_64BIT && startswith (str, "large"))
3b22753a
L
14776 return SHF_X86_64_LARGE;
14777
14778 return -1;
14779}
14780
14781static void
14782handle_large_common (int small ATTRIBUTE_UNUSED)
14783{
14784 if (flag_code != CODE_64BIT)
14785 {
14786 s_comm_internal (0, elf_common_parse);
14787 as_warn (_(".largecomm supported only in 64bit mode, producing .comm"));
14788 }
14789 else
14790 {
14791 static segT lbss_section;
14792 asection *saved_com_section_ptr = elf_com_section_ptr;
14793 asection *saved_bss_section = bss_section;
14794
14795 if (lbss_section == NULL)
14796 {
14797 flagword applicable;
14798 segT seg = now_seg;
14799 subsegT subseg = now_subseg;
14800
14801 /* The .lbss section is for local .largecomm symbols. */
14802 lbss_section = subseg_new (".lbss", 0);
14803 applicable = bfd_applicable_section_flags (stdoutput);
fd361982 14804 bfd_set_section_flags (lbss_section, applicable & SEC_ALLOC);
3b22753a
L
14805 seg_info (lbss_section)->bss = 1;
14806
14807 subseg_set (seg, subseg);
14808 }
14809
14810 elf_com_section_ptr = &_bfd_elf_large_com_section;
14811 bss_section = lbss_section;
14812
14813 s_comm_internal (0, elf_common_parse);
14814
14815 elf_com_section_ptr = saved_com_section_ptr;
14816 bss_section = saved_bss_section;
14817 }
14818}
14819#endif /* OBJ_ELF || OBJ_MAYBE_ELF */