]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/config/tc-i386.c
x86: add missing CheckRegSize
[thirdparty/binutils-gdb.git] / gas / config / tc-i386.c
CommitLineData
b534c6d3 1/* tc-i386.c -- Assemble code for the Intel 80386
a2c58332 2 Copyright (C) 1989-2022 Free Software Foundation, Inc.
252b5132
RH
3
4 This file is part of GAS, the GNU Assembler.
5
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
ec2655a6 8 the Free Software Foundation; either version 3, or (at your option)
252b5132
RH
9 any later version.
10
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to the Free
4b4da160
NC
18 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
19 02110-1301, USA. */
252b5132 20
47926f60
KH
21/* Intel 80386 machine specific gas.
22 Written by Eliot Dresselhaus (eliot@mgm.mit.edu).
3e73aa7c 23 x86_64 support by Jan Hubicka (jh@suse.cz)
0f10071e 24 VIA PadLock support by Michal Ludvig (mludvig@suse.cz)
47926f60
KH
25 Bugs & suggestions are completely welcome. This is free software.
26 Please help us make it better. */
252b5132 27
252b5132 28#include "as.h"
3882b010 29#include "safe-ctype.h"
252b5132 30#include "subsegs.h"
316e2c05 31#include "dwarf2dbg.h"
54cfded0 32#include "dw2gencfi.h"
b52c4ee4
IB
33#include "gen-sframe.h"
34#include "sframe.h"
d2b2c203 35#include "elf/x86-64.h"
40fb9820 36#include "opcodes/i386-init.h"
41fd2579 37#include <limits.h>
41fd2579 38
c3332e24 39#ifndef INFER_ADDR_PREFIX
eecb386c 40#define INFER_ADDR_PREFIX 1
c3332e24
AM
41#endif
42
29b0f896
AM
43#ifndef DEFAULT_ARCH
44#define DEFAULT_ARCH "i386"
246fcdee 45#endif
252b5132 46
edde18a5
AM
47#ifndef INLINE
48#if __GNUC__ >= 2
49#define INLINE __inline__
50#else
51#define INLINE
52#endif
53#endif
54
6305a203
L
55/* Prefixes will be emitted in the order defined below.
56 WAIT_PREFIX must be the first prefix since FWAIT is really is an
57 instruction, and so must come before any prefixes.
58 The preferred prefix order is SEG_PREFIX, ADDR_PREFIX, DATA_PREFIX,
42164a71 59 REP_PREFIX/HLE_PREFIX, LOCK_PREFIX. */
6305a203
L
60#define WAIT_PREFIX 0
61#define SEG_PREFIX 1
62#define ADDR_PREFIX 2
63#define DATA_PREFIX 3
c32fa91d 64#define REP_PREFIX 4
42164a71 65#define HLE_PREFIX REP_PREFIX
7e8b059b 66#define BND_PREFIX REP_PREFIX
c32fa91d 67#define LOCK_PREFIX 5
4e9ac44a
L
68#define REX_PREFIX 6 /* must come last. */
69#define MAX_PREFIXES 7 /* max prefixes per opcode */
6305a203
L
70
71/* we define the syntax here (modulo base,index,scale syntax) */
72#define REGISTER_PREFIX '%'
73#define IMMEDIATE_PREFIX '$'
74#define ABSOLUTE_PREFIX '*'
75
76/* these are the instruction mnemonic suffixes in AT&T syntax or
77 memory operand size in Intel syntax. */
78#define WORD_MNEM_SUFFIX 'w'
79#define BYTE_MNEM_SUFFIX 'b'
80#define SHORT_MNEM_SUFFIX 's'
81#define LONG_MNEM_SUFFIX 'l'
82#define QWORD_MNEM_SUFFIX 'q'
6305a203
L
83/* Intel Syntax. Use a non-ascii letter since since it never appears
84 in instructions. */
85#define LONG_DOUBLE_MNEM_SUFFIX '\1'
86
87#define END_OF_INSN '\0'
88
79dec6b7
JB
89/* This matches the C -> StaticRounding alias in the opcode table. */
90#define commutative staticrounding
91
6305a203
L
92/*
93 'templates' is for grouping together 'template' structures for opcodes
94 of the same name. This is only used for storing the insns in the grand
95 ole hash table of insns.
96 The templates themselves start at START and range up to (but not including)
97 END.
98 */
99typedef struct
100{
d3ce72d0
NC
101 const insn_template *start;
102 const insn_template *end;
6305a203
L
103}
104templates;
105
106/* 386 operand encoding bytes: see 386 book for details of this. */
107typedef struct
108{
109 unsigned int regmem; /* codes register or memory operand */
110 unsigned int reg; /* codes register operand (or extended opcode) */
111 unsigned int mode; /* how to interpret regmem & reg */
112}
113modrm_byte;
114
115/* x86-64 extension prefix. */
116typedef int rex_byte;
117
6305a203
L
118/* 386 opcode byte to code indirect addressing. */
119typedef struct
120{
121 unsigned base;
122 unsigned index;
123 unsigned scale;
124}
125sib_byte;
126
6305a203
L
127/* x86 arch names, types and features */
128typedef struct
129{
130 const char *name; /* arch name */
6ceeed25
JB
131 unsigned int len:8; /* arch string length */
132 bool skip:1; /* show_arch should skip this. */
6305a203 133 enum processor_type type; /* arch type */
ae89daec
JB
134 i386_cpu_flags enable; /* cpu feature enable flags */
135 i386_cpu_flags disable; /* cpu feature disable flags */
6305a203
L
136}
137arch_entry;
138
78f12dd3 139static void update_code_flag (int, int);
e3bb37b5
L
140static void set_code_flag (int);
141static void set_16bit_gcc_code_flag (int);
142static void set_intel_syntax (int);
1efbbeb4 143static void set_intel_mnemonic (int);
db51cc60 144static void set_allow_index_reg (int);
7bab8ab5 145static void set_check (int);
e3bb37b5 146static void set_cpu_arch (int);
6482c264 147#ifdef TE_PE
e3bb37b5 148static void pe_directive_secrel (int);
145667f8 149static void pe_directive_secidx (int);
6482c264 150#endif
e3bb37b5
L
151static void signed_cons (int);
152static char *output_invalid (int c);
ee86248c
JB
153static int i386_finalize_immediate (segT, expressionS *, i386_operand_type,
154 const char *);
155static int i386_finalize_displacement (segT, expressionS *, i386_operand_type,
156 const char *);
a7619375 157static int i386_att_operand (char *);
e3bb37b5 158static int i386_intel_operand (char *, int);
ee86248c
JB
159static int i386_intel_simplify (expressionS *);
160static int i386_intel_parse_name (const char *, expressionS *);
e3bb37b5
L
161static const reg_entry *parse_register (char *, char **);
162static char *parse_insn (char *, char *);
163static char *parse_operands (char *, const char *);
164static void swap_operands (void);
783c187b 165static void swap_2_operands (unsigned int, unsigned int);
48bcea9f 166static enum flag_code i386_addressing_mode (void);
e3bb37b5
L
167static void optimize_imm (void);
168static void optimize_disp (void);
83b16ac6 169static const insn_template *match_template (char);
e3bb37b5
L
170static int check_string (void);
171static int process_suffix (void);
172static int check_byte_reg (void);
173static int check_long_reg (void);
174static int check_qword_reg (void);
175static int check_word_reg (void);
176static int finalize_imm (void);
177static int process_operands (void);
5e042380 178static const reg_entry *build_modrm_byte (void);
e3bb37b5
L
179static void output_insn (void);
180static void output_imm (fragS *, offsetT);
181static void output_disp (fragS *, offsetT);
29b0f896 182#ifndef I386COFF
e3bb37b5 183static void s_bss (int);
252b5132 184#endif
17d4e2a2
L
185#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
186static void handle_large_common (int small ATTRIBUTE_UNUSED);
b4a3a7b4
L
187
188/* GNU_PROPERTY_X86_ISA_1_USED. */
189static unsigned int x86_isa_1_used;
190/* GNU_PROPERTY_X86_FEATURE_2_USED. */
191static unsigned int x86_feature_2_used;
192/* Generate x86 used ISA and feature properties. */
193static unsigned int x86_used_note = DEFAULT_X86_USED_NOTE;
17d4e2a2 194#endif
252b5132 195
a847613f 196static const char *default_arch = DEFAULT_ARCH;
3e73aa7c 197
8a6fb3f9
JB
198/* parse_register() returns this when a register alias cannot be used. */
199static const reg_entry bad_reg = { "<bad>", OPERAND_TYPE_NONE, 0, 0,
200 { Dw2Inval, Dw2Inval } };
201
34684862 202static const reg_entry *reg_eax;
5e042380
JB
203static const reg_entry *reg_ds;
204static const reg_entry *reg_es;
205static const reg_entry *reg_ss;
6288d05f 206static const reg_entry *reg_st0;
6225c532
JB
207static const reg_entry *reg_k0;
208
c0f3af97
L
209/* VEX prefix. */
210typedef struct
211{
43234a1e
L
212 /* VEX prefix is either 2 byte or 3 byte. EVEX is 4 byte. */
213 unsigned char bytes[4];
c0f3af97
L
214 unsigned int length;
215 /* Destination or source register specifier. */
216 const reg_entry *register_specifier;
217} vex_prefix;
218
252b5132 219/* 'md_assemble ()' gathers together information and puts it into a
47926f60 220 i386_insn. */
252b5132 221
520dc8e8
AM
222union i386_op
223 {
224 expressionS *disps;
225 expressionS *imms;
226 const reg_entry *regs;
227 };
228
a65babc9
L
229enum i386_error
230 {
b4d65f2d 231 no_error, /* Must be first. */
86e026a4 232 operand_size_mismatch,
a65babc9
L
233 operand_type_mismatch,
234 register_type_mismatch,
235 number_of_operands_mismatch,
236 invalid_instruction_suffix,
237 bad_imm4,
a65babc9
L
238 unsupported_with_intel_mnemonic,
239 unsupported_syntax,
6c30d220 240 unsupported,
260cd341 241 invalid_sib_address,
6c30d220 242 invalid_vsib_address,
7bab8ab5 243 invalid_vector_register_set,
260cd341 244 invalid_tmm_register_set,
0cc78721 245 invalid_dest_and_src_register_set,
43234a1e
L
246 unsupported_vector_index_register,
247 unsupported_broadcast,
43234a1e
L
248 broadcast_needed,
249 unsupported_masking,
250 mask_not_on_destination,
251 no_default_mask,
252 unsupported_rc_sae,
43234a1e 253 invalid_register_operand,
a65babc9
L
254 };
255
252b5132
RH
256struct _i386_insn
257 {
47926f60 258 /* TM holds the template for the insn were currently assembling. */
d3ce72d0 259 insn_template tm;
252b5132 260
7d5e4556
L
261 /* SUFFIX holds the instruction size suffix for byte, word, dword
262 or qword, if given. */
252b5132
RH
263 char suffix;
264
9a182d04
JB
265 /* OPCODE_LENGTH holds the number of base opcode bytes. */
266 unsigned char opcode_length;
267
47926f60 268 /* OPERANDS gives the number of given operands. */
252b5132
RH
269 unsigned int operands;
270
271 /* REG_OPERANDS, DISP_OPERANDS, MEM_OPERANDS, IMM_OPERANDS give the number
272 of given register, displacement, memory operands and immediate
47926f60 273 operands. */
252b5132
RH
274 unsigned int reg_operands, disp_operands, mem_operands, imm_operands;
275
276 /* TYPES [i] is the type (see above #defines) which tells us how to
520dc8e8 277 use OP[i] for the corresponding operand. */
40fb9820 278 i386_operand_type types[MAX_OPERANDS];
252b5132 279
520dc8e8
AM
280 /* Displacement expression, immediate expression, or register for each
281 operand. */
282 union i386_op op[MAX_OPERANDS];
252b5132 283
3e73aa7c
JH
284 /* Flags for operands. */
285 unsigned int flags[MAX_OPERANDS];
286#define Operand_PCrel 1
c48dadc9 287#define Operand_Mem 2
3e73aa7c 288
252b5132 289 /* Relocation type for operand */
f86103b7 290 enum bfd_reloc_code_real reloc[MAX_OPERANDS];
252b5132 291
252b5132
RH
292 /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
293 the base index byte below. */
294 const reg_entry *base_reg;
295 const reg_entry *index_reg;
296 unsigned int log2_scale_factor;
297
298 /* SEG gives the seg_entries of this insn. They are zero unless
47926f60 299 explicit segment overrides are given. */
5e042380 300 const reg_entry *seg[2];
252b5132 301
8325cc63
JB
302 /* Copied first memory operand string, for re-checking. */
303 char *memop1_string;
304
252b5132
RH
305 /* PREFIX holds all the given prefix opcodes (usually null).
306 PREFIXES is the number of prefix opcodes. */
307 unsigned int prefixes;
308 unsigned char prefix[MAX_PREFIXES];
309
50128d0c 310 /* Register is in low 3 bits of opcode. */
5b7c81bd 311 bool short_form;
50128d0c 312
6f2f06be 313 /* The operand to a branch insn indicates an absolute branch. */
5b7c81bd 314 bool jumpabsolute;
6f2f06be 315
9373f275
L
316 /* There is a memory operand of (%dx) which should be only used
317 with input/output instructions. */
318 bool input_output_operand;
319
921eafea
L
320 /* Extended states. */
321 enum
322 {
323 /* Use MMX state. */
324 xstate_mmx = 1 << 0,
325 /* Use XMM state. */
326 xstate_xmm = 1 << 1,
327 /* Use YMM state. */
328 xstate_ymm = 1 << 2 | xstate_xmm,
329 /* Use ZMM state. */
330 xstate_zmm = 1 << 3 | xstate_ymm,
331 /* Use TMM state. */
32930e4e
L
332 xstate_tmm = 1 << 4,
333 /* Use MASK state. */
334 xstate_mask = 1 << 5
921eafea 335 } xstate;
260cd341 336
e379e5f3 337 /* Has GOTPC or TLS relocation. */
5b7c81bd 338 bool has_gotpc_tls_reloc;
e379e5f3 339
252b5132 340 /* RM and SIB are the modrm byte and the sib byte where the
c1e679ec 341 addressing modes of this insn are encoded. */
252b5132 342 modrm_byte rm;
3e73aa7c 343 rex_byte rex;
43234a1e 344 rex_byte vrex;
252b5132 345 sib_byte sib;
c0f3af97 346 vex_prefix vex;
b6169b20 347
6225c532
JB
348 /* Masking attributes.
349
350 The struct describes masking, applied to OPERAND in the instruction.
351 REG is a pointer to the corresponding mask register. ZEROING tells
352 whether merging or zeroing mask is used. */
353 struct Mask_Operation
354 {
355 const reg_entry *reg;
356 unsigned int zeroing;
357 /* The operand where this operation is associated. */
358 unsigned int operand;
359 } mask;
43234a1e
L
360
361 /* Rounding control and SAE attributes. */
ca5312a2
JB
362 struct RC_Operation
363 {
364 enum rc_type
365 {
366 rc_none = -1,
367 rne,
368 rd,
369 ru,
370 rz,
371 saeonly
372 } type;
7063667e
JB
373 /* In Intel syntax the operand modifier form is supposed to be used, but
374 we continue to accept the immediate forms as well. */
375 bool modifier;
ca5312a2 376 } rounding;
43234a1e 377
5273a3cd
JB
378 /* Broadcasting attributes.
379
380 The struct describes broadcasting, applied to OPERAND. TYPE is
381 expresses the broadcast factor. */
382 struct Broadcast_Operation
383 {
0cc78721 384 /* Type of broadcast: {1to2}, {1to4}, {1to8}, {1to16} or {1to32}. */
5273a3cd
JB
385 unsigned int type;
386
387 /* Index of broadcasted operand. */
388 unsigned int operand;
389
390 /* Number of bytes to broadcast. */
391 unsigned int bytes;
392 } broadcast;
43234a1e
L
393
394 /* Compressed disp8*N attribute. */
395 unsigned int memshift;
396
86fa6981
L
397 /* Prefer load or store in encoding. */
398 enum
399 {
400 dir_encoding_default = 0,
401 dir_encoding_load,
64c49ab3
JB
402 dir_encoding_store,
403 dir_encoding_swap
86fa6981 404 } dir_encoding;
891edac4 405
41eb8e88 406 /* Prefer 8bit, 16bit, 32bit displacement in encoding. */
a501d77e
L
407 enum
408 {
409 disp_encoding_default = 0,
410 disp_encoding_8bit,
41eb8e88 411 disp_encoding_16bit,
a501d77e
L
412 disp_encoding_32bit
413 } disp_encoding;
f8a5c266 414
6b6b6807 415 /* Prefer the REX byte in encoding. */
5b7c81bd 416 bool rex_encoding;
6b6b6807 417
b6f8c7c4 418 /* Disable instruction size optimization. */
5b7c81bd 419 bool no_optimize;
b6f8c7c4 420
86fa6981
L
421 /* How to encode vector instructions. */
422 enum
423 {
424 vex_encoding_default = 0,
42e04b36 425 vex_encoding_vex,
86fa6981 426 vex_encoding_vex3,
da4977e0
JB
427 vex_encoding_evex,
428 vex_encoding_error
86fa6981
L
429 } vec_encoding;
430
d5de92cf
L
431 /* REP prefix. */
432 const char *rep_prefix;
433
165de32a
L
434 /* HLE prefix. */
435 const char *hle_prefix;
42164a71 436
7e8b059b
L
437 /* Have BND prefix. */
438 const char *bnd_prefix;
439
04ef582a
L
440 /* Have NOTRACK prefix. */
441 const char *notrack_prefix;
442
891edac4 443 /* Error message. */
a65babc9 444 enum i386_error error;
252b5132
RH
445 };
446
447typedef struct _i386_insn i386_insn;
448
43234a1e
L
449/* Link RC type with corresponding string, that'll be looked for in
450 asm. */
451struct RC_name
452{
453 enum rc_type type;
454 const char *name;
455 unsigned int len;
456};
457
458static const struct RC_name RC_NamesTable[] =
459{
460 { rne, STRING_COMMA_LEN ("rn-sae") },
461 { rd, STRING_COMMA_LEN ("rd-sae") },
462 { ru, STRING_COMMA_LEN ("ru-sae") },
463 { rz, STRING_COMMA_LEN ("rz-sae") },
464 { saeonly, STRING_COMMA_LEN ("sae") },
465};
466
3bfea8ba
L
467/* To be indexed by segment register number. */
468static const unsigned char i386_seg_prefixes[] = {
469 ES_PREFIX_OPCODE,
470 CS_PREFIX_OPCODE,
471 SS_PREFIX_OPCODE,
472 DS_PREFIX_OPCODE,
473 FS_PREFIX_OPCODE,
474 GS_PREFIX_OPCODE
475};
476
252b5132
RH
477/* List of chars besides those in app.c:symbol_chars that can start an
478 operand. Used to prevent the scrubber eating vital white-space. */
86fa6981 479const char extra_symbol_chars[] = "*%-([{}"
252b5132 480#ifdef LEX_AT
32137342
NC
481 "@"
482#endif
483#ifdef LEX_QM
484 "?"
252b5132 485#endif
32137342 486 ;
252b5132 487
b3983e5f
JB
488#if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
489 && !defined (TE_GNU) \
490 && !defined (TE_LINUX) \
d85e70a3 491 && !defined (TE_Haiku) \
b3983e5f
JB
492 && !defined (TE_FreeBSD) \
493 && !defined (TE_DragonFly) \
494 && !defined (TE_NetBSD))
252b5132 495/* This array holds the chars that always start a comment. If the
b3b91714
AM
496 pre-processor is disabled, these aren't very useful. The option
497 --divide will remove '/' from this list. */
498const char *i386_comment_chars = "#/";
499#define SVR4_COMMENT_CHARS 1
252b5132 500#define PREFIX_SEPARATOR '\\'
252b5132 501
b3b91714
AM
502#else
503const char *i386_comment_chars = "#";
504#define PREFIX_SEPARATOR '/'
505#endif
506
252b5132
RH
507/* This array holds the chars that only start a comment at the beginning of
508 a line. If the line seems to have the form '# 123 filename'
ce8a8b2f
AM
509 .line and .file directives will appear in the pre-processed output.
510 Note that input_file.c hand checks for '#' at the beginning of the
252b5132 511 first line of the input file. This is because the compiler outputs
ce8a8b2f
AM
512 #NO_APP at the beginning of its output.
513 Also note that comments started like this one will always work if
252b5132 514 '/' isn't otherwise defined. */
b3b91714 515const char line_comment_chars[] = "#/";
252b5132 516
63a0b638 517const char line_separator_chars[] = ";";
252b5132 518
ce8a8b2f
AM
519/* Chars that can be used to separate mant from exp in floating point
520 nums. */
252b5132
RH
521const char EXP_CHARS[] = "eE";
522
ce8a8b2f
AM
523/* Chars that mean this number is a floating point constant
524 As in 0f12.456
525 or 0d1.2345e12. */
de133cf9 526const char FLT_CHARS[] = "fFdDxXhHbB";
252b5132 527
ce8a8b2f 528/* Tables for lexical analysis. */
252b5132
RH
529static char mnemonic_chars[256];
530static char register_chars[256];
531static char operand_chars[256];
532static char identifier_chars[256];
252b5132 533
ce8a8b2f 534/* Lexical macros. */
252b5132
RH
535#define is_mnemonic_char(x) (mnemonic_chars[(unsigned char) x])
536#define is_operand_char(x) (operand_chars[(unsigned char) x])
537#define is_register_char(x) (register_chars[(unsigned char) x])
538#define is_space_char(x) ((x) == ' ')
539#define is_identifier_char(x) (identifier_chars[(unsigned char) x])
252b5132 540
0234cb7c 541/* All non-digit non-letter characters that may occur in an operand. */
252b5132
RH
542static char operand_special_chars[] = "%$-+(,)*._~/<>|&^!:[@]";
543
544/* md_assemble() always leaves the strings it's passed unaltered. To
545 effect this we maintain a stack of saved characters that we've smashed
546 with '\0's (indicating end of strings for various sub-fields of the
47926f60 547 assembler instruction). */
252b5132 548static char save_stack[32];
ce8a8b2f 549static char *save_stack_p;
252b5132
RH
550#define END_STRING_AND_SAVE(s) \
551 do { *save_stack_p++ = *(s); *(s) = '\0'; } while (0)
552#define RESTORE_END_STRING(s) \
553 do { *(s) = *--save_stack_p; } while (0)
554
47926f60 555/* The instruction we're assembling. */
252b5132
RH
556static i386_insn i;
557
558/* Possible templates for current insn. */
559static const templates *current_templates;
560
31b2323c
L
561/* Per instruction expressionS buffers: max displacements & immediates. */
562static expressionS disp_expressions[MAX_MEMORY_OPERANDS];
563static expressionS im_expressions[MAX_IMMEDIATE_OPERANDS];
252b5132 564
47926f60 565/* Current operand we are working on. */
ee86248c 566static int this_operand = -1;
252b5132 567
3e73aa7c
JH
568/* We support four different modes. FLAG_CODE variable is used to distinguish
569 these. */
570
571enum flag_code {
572 CODE_32BIT,
573 CODE_16BIT,
574 CODE_64BIT };
575
576static enum flag_code flag_code;
4fa24527 577static unsigned int object_64bit;
862be3fb 578static unsigned int disallow_64bit_reloc;
3e73aa7c 579static int use_rela_relocations = 0;
e379e5f3
L
580/* __tls_get_addr/___tls_get_addr symbol for TLS. */
581static const char *tls_get_addr;
3e73aa7c 582
7af8ed2d
NC
583#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
584 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
585 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
586
351f65ca
L
587/* The ELF ABI to use. */
588enum x86_elf_abi
589{
590 I386_ABI,
7f56bc95
L
591 X86_64_ABI,
592 X86_64_X32_ABI
351f65ca
L
593};
594
595static enum x86_elf_abi x86_elf_abi = I386_ABI;
7af8ed2d 596#endif
351f65ca 597
167ad85b
TG
598#if defined (TE_PE) || defined (TE_PEP)
599/* Use big object file format. */
600static int use_big_obj = 0;
601#endif
602
8dcea932
L
603#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
604/* 1 if generating code for a shared library. */
605static int shared = 0;
b52c4ee4
IB
606
607unsigned int x86_sframe_cfa_sp_reg;
608/* The other CFA base register for SFrame unwind info. */
609unsigned int x86_sframe_cfa_fp_reg;
610unsigned int x86_sframe_cfa_ra_reg;
611
8dcea932
L
612#endif
613
47926f60
KH
614/* 1 for intel syntax,
615 0 if att syntax. */
616static int intel_syntax = 0;
252b5132 617
4b5aaf5f
L
618static enum x86_64_isa
619{
620 amd64 = 1, /* AMD64 ISA. */
621 intel64 /* Intel64 ISA. */
622} isa64;
e89c5eaa 623
1efbbeb4
L
624/* 1 for intel mnemonic,
625 0 if att mnemonic. */
626static int intel_mnemonic = !SYSV386_COMPAT;
627
a60de03c
JB
628/* 1 if pseudo registers are permitted. */
629static int allow_pseudo_reg = 0;
630
47926f60
KH
631/* 1 if register prefix % not required. */
632static int allow_naked_reg = 0;
252b5132 633
33eaf5de 634/* 1 if the assembler should add BND prefix for all control-transferring
7e8b059b
L
635 instructions supporting it, even if this prefix wasn't specified
636 explicitly. */
637static int add_bnd_prefix = 0;
638
ba104c83 639/* 1 if pseudo index register, eiz/riz, is allowed . */
db51cc60
L
640static int allow_index_reg = 0;
641
d022bddd
IT
642/* 1 if the assembler should ignore LOCK prefix, even if it was
643 specified explicitly. */
644static int omit_lock_prefix = 0;
645
e4e00185
AS
646/* 1 if the assembler should encode lfence, mfence, and sfence as
647 "lock addl $0, (%{re}sp)". */
648static int avoid_fence = 0;
649
ae531041
L
650/* 1 if lfence should be inserted after every load. */
651static int lfence_after_load = 0;
652
653/* Non-zero if lfence should be inserted before indirect branch. */
654static enum lfence_before_indirect_branch_kind
655 {
656 lfence_branch_none = 0,
657 lfence_branch_register,
658 lfence_branch_memory,
659 lfence_branch_all
660 }
661lfence_before_indirect_branch;
662
663/* Non-zero if lfence should be inserted before ret. */
664static enum lfence_before_ret_kind
665 {
666 lfence_before_ret_none = 0,
667 lfence_before_ret_not,
a09f656b 668 lfence_before_ret_or,
669 lfence_before_ret_shl
ae531041
L
670 }
671lfence_before_ret;
672
673/* Types of previous instruction is .byte or prefix. */
e379e5f3
L
674static struct
675 {
676 segT seg;
677 const char *file;
678 const char *name;
679 unsigned int line;
680 enum last_insn_kind
681 {
682 last_insn_other = 0,
683 last_insn_directive,
684 last_insn_prefix
685 } kind;
686 } last_insn;
687
0cb4071e
L
688/* 1 if the assembler should generate relax relocations. */
689
690static int generate_relax_relocations
691 = DEFAULT_GENERATE_X86_RELAX_RELOCATIONS;
692
7bab8ab5 693static enum check_kind
daf50ae7 694 {
7bab8ab5
JB
695 check_none = 0,
696 check_warning,
697 check_error
daf50ae7 698 }
7bab8ab5 699sse_check, operand_check = check_warning;
daf50ae7 700
e379e5f3
L
701/* Non-zero if branches should be aligned within power of 2 boundary. */
702static int align_branch_power = 0;
703
704/* Types of branches to align. */
705enum align_branch_kind
706 {
707 align_branch_none = 0,
708 align_branch_jcc = 1,
709 align_branch_fused = 2,
710 align_branch_jmp = 3,
711 align_branch_call = 4,
712 align_branch_indirect = 5,
713 align_branch_ret = 6
714 };
715
716/* Type bits of branches to align. */
717enum align_branch_bit
718 {
719 align_branch_jcc_bit = 1 << align_branch_jcc,
720 align_branch_fused_bit = 1 << align_branch_fused,
721 align_branch_jmp_bit = 1 << align_branch_jmp,
722 align_branch_call_bit = 1 << align_branch_call,
723 align_branch_indirect_bit = 1 << align_branch_indirect,
724 align_branch_ret_bit = 1 << align_branch_ret
725 };
726
727static unsigned int align_branch = (align_branch_jcc_bit
728 | align_branch_fused_bit
729 | align_branch_jmp_bit);
730
79d72f45
HL
731/* Types of condition jump used by macro-fusion. */
732enum mf_jcc_kind
733 {
734 mf_jcc_jo = 0, /* base opcode 0x70 */
735 mf_jcc_jc, /* base opcode 0x72 */
736 mf_jcc_je, /* base opcode 0x74 */
737 mf_jcc_jna, /* base opcode 0x76 */
738 mf_jcc_js, /* base opcode 0x78 */
739 mf_jcc_jp, /* base opcode 0x7a */
740 mf_jcc_jl, /* base opcode 0x7c */
741 mf_jcc_jle, /* base opcode 0x7e */
742 };
743
744/* Types of compare flag-modifying insntructions used by macro-fusion. */
745enum mf_cmp_kind
746 {
747 mf_cmp_test_and, /* test/cmp */
748 mf_cmp_alu_cmp, /* add/sub/cmp */
749 mf_cmp_incdec /* inc/dec */
750 };
751
e379e5f3
L
752/* The maximum padding size for fused jcc. CMP like instruction can
753 be 9 bytes and jcc can be 6 bytes. Leave room just in case for
754 prefixes. */
755#define MAX_FUSED_JCC_PADDING_SIZE 20
756
757/* The maximum number of prefixes added for an instruction. */
758static unsigned int align_branch_prefix_size = 5;
759
b6f8c7c4
L
760/* Optimization:
761 1. Clear the REX_W bit with register operand if possible.
762 2. Above plus use 128bit vector instruction to clear the full vector
763 register.
764 */
765static int optimize = 0;
766
767/* Optimization:
768 1. Clear the REX_W bit with register operand if possible.
769 2. Above plus use 128bit vector instruction to clear the full vector
770 register.
771 3. Above plus optimize "test{q,l,w} $imm8,%r{64,32,16}" to
772 "testb $imm7,%r8".
773 */
774static int optimize_for_space = 0;
775
2ca3ace5
L
776/* Register prefix used for error message. */
777static const char *register_prefix = "%";
778
47926f60
KH
779/* Used in 16 bit gcc mode to add an l suffix to call, ret, enter,
780 leave, push, and pop instructions so that gcc has the same stack
781 frame as in 32 bit mode. */
782static char stackop_size = '\0';
eecb386c 783
12b55ccc
L
784/* Non-zero to optimize code alignment. */
785int optimize_align_code = 1;
786
47926f60
KH
787/* Non-zero to quieten some warnings. */
788static int quiet_warnings = 0;
a38cf1db 789
d59a54c2
JB
790/* Guard to avoid repeated warnings about non-16-bit code on 16-bit CPUs. */
791static bool pre_386_16bit_warned;
792
47926f60
KH
793/* CPU name. */
794static const char *cpu_arch_name = NULL;
6305a203 795static char *cpu_sub_arch_name = NULL;
a38cf1db 796
47926f60 797/* CPU feature flags. */
40fb9820
L
798static i386_cpu_flags cpu_arch_flags = CPU_UNKNOWN_FLAGS;
799
ccc9c027
L
800/* If we have selected a cpu we are generating instructions for. */
801static int cpu_arch_tune_set = 0;
802
9103f4f4 803/* Cpu we are generating instructions for. */
fbf3f584 804enum processor_type cpu_arch_tune = PROCESSOR_UNKNOWN;
9103f4f4
L
805
806/* CPU feature flags of cpu we are generating instructions for. */
40fb9820 807static i386_cpu_flags cpu_arch_tune_flags;
9103f4f4 808
ccc9c027 809/* CPU instruction set architecture used. */
fbf3f584 810enum processor_type cpu_arch_isa = PROCESSOR_UNKNOWN;
ccc9c027 811
9103f4f4 812/* CPU feature flags of instruction set architecture used. */
fbf3f584 813i386_cpu_flags cpu_arch_isa_flags;
9103f4f4 814
fddf5b5b
AM
815/* If set, conditional jumps are not automatically promoted to handle
816 larger than a byte offset. */
f68697e8 817static bool no_cond_jump_promotion = false;
fddf5b5b 818
c0f3af97
L
819/* Encode SSE instructions with VEX prefix. */
820static unsigned int sse2avx;
821
c8480b58
L
822/* Encode aligned vector move as unaligned vector move. */
823static unsigned int use_unaligned_vector_move;
824
539f890d
L
825/* Encode scalar AVX instructions with specific vector length. */
826static enum
827 {
828 vex128 = 0,
829 vex256
830 } avxscalar;
831
03751133
L
832/* Encode VEX WIG instructions with specific vex.w. */
833static enum
834 {
835 vexw0 = 0,
836 vexw1
837 } vexwig;
838
43234a1e
L
839/* Encode scalar EVEX LIG instructions with specific vector length. */
840static enum
841 {
842 evexl128 = 0,
843 evexl256,
844 evexl512
845 } evexlig;
846
847/* Encode EVEX WIG instructions with specific evex.w. */
848static enum
849 {
850 evexw0 = 0,
851 evexw1
852 } evexwig;
853
d3d3c6db
IT
854/* Value to encode in EVEX RC bits, for SAE-only instructions. */
855static enum rc_type evexrcig = rne;
856
29b0f896 857/* Pre-defined "_GLOBAL_OFFSET_TABLE_". */
87c245cc 858static symbolS *GOT_symbol;
29b0f896 859
a4447b93
RH
860/* The dwarf2 return column, adjusted for 32 or 64 bit. */
861unsigned int x86_dwarf2_return_column;
862
863/* The dwarf2 data alignment, adjusted for 32 or 64 bit. */
864int x86_cie_data_alignment;
865
252b5132 866/* Interface to relax_segment.
fddf5b5b
AM
867 There are 3 major relax states for 386 jump insns because the
868 different types of jumps add different sizes to frags when we're
e379e5f3
L
869 figuring out what sort of jump to choose to reach a given label.
870
871 BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING are used to align
872 branches which are handled by md_estimate_size_before_relax() and
873 i386_generic_table_relax_frag(). */
252b5132 874
47926f60 875/* Types. */
93c2a809
AM
876#define UNCOND_JUMP 0
877#define COND_JUMP 1
878#define COND_JUMP86 2
e379e5f3
L
879#define BRANCH_PADDING 3
880#define BRANCH_PREFIX 4
881#define FUSED_JCC_PADDING 5
fddf5b5b 882
47926f60 883/* Sizes. */
252b5132
RH
884#define CODE16 1
885#define SMALL 0
29b0f896 886#define SMALL16 (SMALL | CODE16)
252b5132 887#define BIG 2
29b0f896 888#define BIG16 (BIG | CODE16)
252b5132
RH
889
890#ifndef INLINE
891#ifdef __GNUC__
892#define INLINE __inline__
893#else
894#define INLINE
895#endif
896#endif
897
fddf5b5b
AM
898#define ENCODE_RELAX_STATE(type, size) \
899 ((relax_substateT) (((type) << 2) | (size)))
900#define TYPE_FROM_RELAX_STATE(s) \
901 ((s) >> 2)
902#define DISP_SIZE_FROM_RELAX_STATE(s) \
903 ((((s) & 3) == BIG ? 4 : (((s) & 3) == BIG16 ? 2 : 1)))
252b5132
RH
904
905/* This table is used by relax_frag to promote short jumps to long
906 ones where necessary. SMALL (short) jumps may be promoted to BIG
907 (32 bit long) ones, and SMALL16 jumps to BIG16 (16 bit long). We
908 don't allow a short jump in a 32 bit code segment to be promoted to
909 a 16 bit offset jump because it's slower (requires data size
910 prefix), and doesn't work, unless the destination is in the bottom
911 64k of the code segment (The top 16 bits of eip are zeroed). */
912
913const relax_typeS md_relax_table[] =
914{
24eab124
AM
915 /* The fields are:
916 1) most positive reach of this state,
917 2) most negative reach of this state,
93c2a809 918 3) how many bytes this mode will have in the variable part of the frag
ce8a8b2f 919 4) which index into the table to try if we can't fit into this one. */
252b5132 920
fddf5b5b 921 /* UNCOND_JUMP states. */
93c2a809
AM
922 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG)},
923 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16)},
924 /* dword jmp adds 4 bytes to frag:
925 0 extra opcode bytes, 4 displacement bytes. */
252b5132 926 {0, 0, 4, 0},
93c2a809
AM
927 /* word jmp adds 2 byte2 to frag:
928 0 extra opcode bytes, 2 displacement bytes. */
252b5132
RH
929 {0, 0, 2, 0},
930
93c2a809
AM
931 /* COND_JUMP states. */
932 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG)},
933 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG16)},
934 /* dword conditionals adds 5 bytes to frag:
935 1 extra opcode byte, 4 displacement bytes. */
936 {0, 0, 5, 0},
fddf5b5b 937 /* word conditionals add 3 bytes to frag:
93c2a809
AM
938 1 extra opcode byte, 2 displacement bytes. */
939 {0, 0, 3, 0},
940
941 /* COND_JUMP86 states. */
942 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG)},
943 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG16)},
944 /* dword conditionals adds 5 bytes to frag:
945 1 extra opcode byte, 4 displacement bytes. */
946 {0, 0, 5, 0},
947 /* word conditionals add 4 bytes to frag:
948 1 displacement byte and a 3 byte long branch insn. */
949 {0, 0, 4, 0}
252b5132
RH
950};
951
6ceeed25 952#define ARCH(n, t, f, s) \
ae89daec
JB
953 { STRING_COMMA_LEN (#n), s, PROCESSOR_ ## t, CPU_ ## f ## _FLAGS, \
954 CPU_NONE_FLAGS }
955#define SUBARCH(n, e, d, s) \
956 { STRING_COMMA_LEN (#n), s, PROCESSOR_NONE, CPU_ ## e ## _FLAGS, \
957 CPU_ ## d ## _FLAGS }
6ceeed25 958
9103f4f4
L
959static const arch_entry cpu_arch[] =
960{
3ce2ebcf
JB
961 /* Do not replace the first two entries - i386_target_format() and
962 set_cpu_arch() rely on them being there in this order. */
6ceeed25
JB
963 ARCH (generic32, GENERIC32, GENERIC32, false),
964 ARCH (generic64, GENERIC64, GENERIC64, false),
965 ARCH (i8086, UNKNOWN, NONE, false),
966 ARCH (i186, UNKNOWN, I186, false),
967 ARCH (i286, UNKNOWN, I286, false),
968 ARCH (i386, I386, I386, false),
969 ARCH (i486, I486, I486, false),
970 ARCH (i586, PENTIUM, I586, false),
971 ARCH (i686, PENTIUMPRO, I686, false),
972 ARCH (pentium, PENTIUM, I586, false),
973 ARCH (pentiumpro, PENTIUMPRO, PENTIUMPRO, false),
974 ARCH (pentiumii, PENTIUMPRO, P2, false),
975 ARCH (pentiumiii, PENTIUMPRO, P3, false),
976 ARCH (pentium4, PENTIUM4, P4, false),
977 ARCH (prescott, NOCONA, CORE, false),
978 ARCH (nocona, NOCONA, NOCONA, false),
979 ARCH (yonah, CORE, CORE, true),
980 ARCH (core, CORE, CORE, false),
981 ARCH (merom, CORE2, CORE2, true),
982 ARCH (core2, CORE2, CORE2, false),
983 ARCH (corei7, COREI7, COREI7, false),
984 ARCH (iamcu, IAMCU, IAMCU, false),
985 ARCH (k6, K6, K6, false),
986 ARCH (k6_2, K6, K6_2, false),
987 ARCH (athlon, ATHLON, ATHLON, false),
988 ARCH (sledgehammer, K8, K8, true),
989 ARCH (opteron, K8, K8, false),
990 ARCH (k8, K8, K8, false),
991 ARCH (amdfam10, AMDFAM10, AMDFAM10, false),
992 ARCH (bdver1, BD, BDVER1, false),
993 ARCH (bdver2, BD, BDVER2, false),
994 ARCH (bdver3, BD, BDVER3, false),
995 ARCH (bdver4, BD, BDVER4, false),
996 ARCH (znver1, ZNVER, ZNVER1, false),
997 ARCH (znver2, ZNVER, ZNVER2, false),
998 ARCH (znver3, ZNVER, ZNVER3, false),
b0e8fa7f 999 ARCH (znver4, ZNVER, ZNVER4, false),
6ceeed25
JB
1000 ARCH (btver1, BT, BTVER1, false),
1001 ARCH (btver2, BT, BTVER2, false),
1002
ae89daec
JB
1003 SUBARCH (8087, 8087, ANY_X87, false),
1004 SUBARCH (87, NONE, ANY_X87, false), /* Disable only! */
1005 SUBARCH (287, 287, ANY_287, false),
1006 SUBARCH (387, 387, ANY_387, false),
1007 SUBARCH (687, 687, ANY_687, false),
1008 SUBARCH (cmov, CMOV, ANY_CMOV, false),
1009 SUBARCH (fxsr, FXSR, ANY_FXSR, false),
1010 SUBARCH (mmx, MMX, ANY_MMX, false),
1011 SUBARCH (sse, SSE, ANY_SSE, false),
1012 SUBARCH (sse2, SSE2, ANY_SSE2, false),
1013 SUBARCH (sse3, SSE3, ANY_SSE3, false),
1014 SUBARCH (sse4a, SSE4A, ANY_SSE4A, false),
1015 SUBARCH (ssse3, SSSE3, ANY_SSSE3, false),
1016 SUBARCH (sse4.1, SSE4_1, ANY_SSE4_1, false),
1017 SUBARCH (sse4.2, SSE4_2, ANY_SSE4_2, false),
1018 SUBARCH (sse4, SSE4_2, ANY_SSE4_1, false),
1019 SUBARCH (avx, AVX, ANY_AVX, false),
1020 SUBARCH (avx2, AVX2, ANY_AVX2, false),
1021 SUBARCH (avx512f, AVX512F, ANY_AVX512F, false),
1022 SUBARCH (avx512cd, AVX512CD, ANY_AVX512CD, false),
1023 SUBARCH (avx512er, AVX512ER, ANY_AVX512ER, false),
1024 SUBARCH (avx512pf, AVX512PF, ANY_AVX512PF, false),
1025 SUBARCH (avx512dq, AVX512DQ, ANY_AVX512DQ, false),
1026 SUBARCH (avx512bw, AVX512BW, ANY_AVX512BW, false),
1027 SUBARCH (avx512vl, AVX512VL, ANY_AVX512VL, false),
1028 SUBARCH (vmx, VMX, VMX, false),
1029 SUBARCH (vmfunc, VMFUNC, VMFUNC, false),
1030 SUBARCH (smx, SMX, SMX, false),
1031 SUBARCH (xsave, XSAVE, XSAVE, false),
1032 SUBARCH (xsaveopt, XSAVEOPT, XSAVEOPT, false),
1033 SUBARCH (xsavec, XSAVEC, XSAVEC, false),
1034 SUBARCH (xsaves, XSAVES, XSAVES, false),
1035 SUBARCH (aes, AES, AES, false),
1036 SUBARCH (pclmul, PCLMUL, PCLMUL, false),
1037 SUBARCH (clmul, PCLMUL, PCLMUL, true),
1038 SUBARCH (fsgsbase, FSGSBASE, FSGSBASE, false),
1039 SUBARCH (rdrnd, RDRND, RDRND, false),
1040 SUBARCH (f16c, F16C, F16C, false),
1041 SUBARCH (bmi2, BMI2, BMI2, false),
1042 SUBARCH (fma, FMA, FMA, false),
1043 SUBARCH (fma4, FMA4, FMA4, false),
1044 SUBARCH (xop, XOP, XOP, false),
1045 SUBARCH (lwp, LWP, LWP, false),
1046 SUBARCH (movbe, MOVBE, MOVBE, false),
1047 SUBARCH (cx16, CX16, CX16, false),
1048 SUBARCH (ept, EPT, EPT, false),
1049 SUBARCH (lzcnt, LZCNT, LZCNT, false),
1050 SUBARCH (popcnt, POPCNT, POPCNT, false),
1051 SUBARCH (hle, HLE, HLE, false),
1052 SUBARCH (rtm, RTM, RTM, false),
1053 SUBARCH (invpcid, INVPCID, INVPCID, false),
1054 SUBARCH (clflush, CLFLUSH, CLFLUSH, false),
1055 SUBARCH (nop, NOP, NOP, false),
1056 SUBARCH (syscall, SYSCALL, SYSCALL, false),
1057 SUBARCH (rdtscp, RDTSCP, RDTSCP, false),
1058 SUBARCH (3dnow, 3DNOW, 3DNOW, false),
1059 SUBARCH (3dnowa, 3DNOWA, 3DNOWA, false),
1060 SUBARCH (padlock, PADLOCK, PADLOCK, false),
1061 SUBARCH (pacifica, SVME, SVME, true),
1062 SUBARCH (svme, SVME, SVME, false),
ae89daec
JB
1063 SUBARCH (abm, ABM, ABM, false),
1064 SUBARCH (bmi, BMI, BMI, false),
1065 SUBARCH (tbm, TBM, TBM, false),
1066 SUBARCH (adx, ADX, ADX, false),
1067 SUBARCH (rdseed, RDSEED, RDSEED, false),
1068 SUBARCH (prfchw, PRFCHW, PRFCHW, false),
1069 SUBARCH (smap, SMAP, SMAP, false),
1070 SUBARCH (mpx, MPX, MPX, false),
1071 SUBARCH (sha, SHA, SHA, false),
1072 SUBARCH (clflushopt, CLFLUSHOPT, CLFLUSHOPT, false),
1073 SUBARCH (prefetchwt1, PREFETCHWT1, PREFETCHWT1, false),
1074 SUBARCH (se1, SE1, SE1, false),
1075 SUBARCH (clwb, CLWB, CLWB, false),
1076 SUBARCH (avx512ifma, AVX512IFMA, ANY_AVX512IFMA, false),
1077 SUBARCH (avx512vbmi, AVX512VBMI, ANY_AVX512VBMI, false),
1078 SUBARCH (avx512_4fmaps, AVX512_4FMAPS, ANY_AVX512_4FMAPS, false),
1079 SUBARCH (avx512_4vnniw, AVX512_4VNNIW, ANY_AVX512_4VNNIW, false),
1080 SUBARCH (avx512_vpopcntdq, AVX512_VPOPCNTDQ, ANY_AVX512_VPOPCNTDQ, false),
1081 SUBARCH (avx512_vbmi2, AVX512_VBMI2, ANY_AVX512_VBMI2, false),
1082 SUBARCH (avx512_vnni, AVX512_VNNI, ANY_AVX512_VNNI, false),
1083 SUBARCH (avx512_bitalg, AVX512_BITALG, ANY_AVX512_BITALG, false),
1084 SUBARCH (avx_vnni, AVX_VNNI, ANY_AVX_VNNI, false),
1085 SUBARCH (clzero, CLZERO, CLZERO, false),
1086 SUBARCH (mwaitx, MWAITX, MWAITX, false),
1087 SUBARCH (ospke, OSPKE, OSPKE, false),
1088 SUBARCH (rdpid, RDPID, RDPID, false),
1089 SUBARCH (ptwrite, PTWRITE, PTWRITE, false),
1090 SUBARCH (ibt, IBT, ANY_IBT, false),
1091 SUBARCH (shstk, SHSTK, ANY_SHSTK, false),
1092 SUBARCH (gfni, GFNI, GFNI, false),
1093 SUBARCH (vaes, VAES, VAES, false),
1094 SUBARCH (vpclmulqdq, VPCLMULQDQ, VPCLMULQDQ, false),
1095 SUBARCH (wbnoinvd, WBNOINVD, WBNOINVD, false),
1096 SUBARCH (pconfig, PCONFIG, PCONFIG, false),
1097 SUBARCH (waitpkg, WAITPKG, WAITPKG, false),
1098 SUBARCH (cldemote, CLDEMOTE, CLDEMOTE, false),
1099 SUBARCH (amx_int8, AMX_INT8, ANY_AMX_INT8, false),
1100 SUBARCH (amx_bf16, AMX_BF16, ANY_AMX_BF16, false),
68830fba 1101 SUBARCH (amx_fp16, AMX_FP16, AMX_FP16, false),
ae89daec
JB
1102 SUBARCH (amx_tile, AMX_TILE, ANY_AMX_TILE, false),
1103 SUBARCH (movdiri, MOVDIRI, ANY_MOVDIRI, false),
1104 SUBARCH (movdir64b, MOVDIR64B, ANY_MOVDIR64B, false),
1105 SUBARCH (avx512_bf16, AVX512_BF16, ANY_AVX512_BF16, false),
1106 SUBARCH (avx512_vp2intersect, AVX512_VP2INTERSECT,
1107 ANY_AVX512_VP2INTERSECT, false),
1108 SUBARCH (tdx, TDX, ANY_TDX, false),
1109 SUBARCH (enqcmd, ENQCMD, ANY_ENQCMD, false),
1110 SUBARCH (serialize, SERIALIZE, ANY_SERIALIZE, false),
1111 SUBARCH (rdpru, RDPRU, RDPRU, false),
1112 SUBARCH (mcommit, MCOMMIT, MCOMMIT, false),
1113 SUBARCH (sev_es, SEV_ES, SEV_ES, false),
1114 SUBARCH (tsxldtrk, TSXLDTRK, ANY_TSXLDTRK, false),
1115 SUBARCH (kl, KL, ANY_KL, false),
1116 SUBARCH (widekl, WIDEKL, ANY_WIDEKL, false),
1117 SUBARCH (uintr, UINTR, ANY_UINTR, false),
1118 SUBARCH (hreset, HRESET, ANY_HRESET, false),
1119 SUBARCH (avx512_fp16, AVX512_FP16, ANY_AVX512_FP16, false),
ef07be45 1120 SUBARCH (prefetchi, PREFETCHI, PREFETCHI, false),
4321af3e 1121 SUBARCH (avx_ifma, AVX_IFMA, ANY_AVX_IFMA, false),
23ae61ad 1122 SUBARCH (avx_vnni_int8, AVX_VNNI_INT8, ANY_AVX_VNNI_INT8, false),
a93e3234 1123 SUBARCH (cmpccxadd, CMPCCXADD, ANY_CMPCCXADD, false),
941f0833 1124 SUBARCH (wrmsrns, WRMSRNS, ANY_WRMSRNS, false),
2188d6ea 1125 SUBARCH (msrlist, MSRLIST, ANY_MSRLIST, false),
01d8ce74 1126 SUBARCH (avx_ne_convert, AVX_NE_CONVERT, ANY_AVX_NE_CONVERT, false),
b06311ad 1127 SUBARCH (rao_int, RAO_INT, ANY_RAO_INT, false),
b0e8fa7f 1128 SUBARCH (rmpquery, RMPQUERY, RMPQUERY, false),
293f5f65
L
1129};
1130
6ceeed25
JB
1131#undef SUBARCH
1132#undef ARCH
1133
704209c0 1134#ifdef I386COFF
a6c24e68
NC
1135/* Like s_lcomm_internal in gas/read.c but the alignment string
1136 is allowed to be optional. */
1137
1138static symbolS *
1139pe_lcomm_internal (int needs_align, symbolS *symbolP, addressT size)
1140{
1141 addressT align = 0;
1142
1143 SKIP_WHITESPACE ();
1144
7ab9ffdd 1145 if (needs_align
a6c24e68
NC
1146 && *input_line_pointer == ',')
1147 {
1148 align = parse_align (needs_align - 1);
7ab9ffdd 1149
a6c24e68
NC
1150 if (align == (addressT) -1)
1151 return NULL;
1152 }
1153 else
1154 {
1155 if (size >= 8)
1156 align = 3;
1157 else if (size >= 4)
1158 align = 2;
1159 else if (size >= 2)
1160 align = 1;
1161 else
1162 align = 0;
1163 }
1164
1165 bss_alloc (symbolP, size, align);
1166 return symbolP;
1167}
1168
704209c0 1169static void
a6c24e68
NC
1170pe_lcomm (int needs_align)
1171{
1172 s_comm_internal (needs_align * 2, pe_lcomm_internal);
1173}
704209c0 1174#endif
a6c24e68 1175
29b0f896
AM
1176const pseudo_typeS md_pseudo_table[] =
1177{
1178#if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
1179 {"align", s_align_bytes, 0},
1180#else
1181 {"align", s_align_ptwo, 0},
1182#endif
1183 {"arch", set_cpu_arch, 0},
1184#ifndef I386COFF
1185 {"bss", s_bss, 0},
a6c24e68
NC
1186#else
1187 {"lcomm", pe_lcomm, 1},
29b0f896
AM
1188#endif
1189 {"ffloat", float_cons, 'f'},
1190 {"dfloat", float_cons, 'd'},
1191 {"tfloat", float_cons, 'x'},
7d19d096 1192 {"hfloat", float_cons, 'h'},
de133cf9 1193 {"bfloat16", float_cons, 'b'},
29b0f896 1194 {"value", cons, 2},
d182319b 1195 {"slong", signed_cons, 4},
29b0f896
AM
1196 {"noopt", s_ignore, 0},
1197 {"optim", s_ignore, 0},
1198 {"code16gcc", set_16bit_gcc_code_flag, CODE_16BIT},
1199 {"code16", set_code_flag, CODE_16BIT},
1200 {"code32", set_code_flag, CODE_32BIT},
da5f19a2 1201#ifdef BFD64
29b0f896 1202 {"code64", set_code_flag, CODE_64BIT},
da5f19a2 1203#endif
29b0f896
AM
1204 {"intel_syntax", set_intel_syntax, 1},
1205 {"att_syntax", set_intel_syntax, 0},
1efbbeb4
L
1206 {"intel_mnemonic", set_intel_mnemonic, 1},
1207 {"att_mnemonic", set_intel_mnemonic, 0},
db51cc60
L
1208 {"allow_index_reg", set_allow_index_reg, 1},
1209 {"disallow_index_reg", set_allow_index_reg, 0},
7bab8ab5
JB
1210 {"sse_check", set_check, 0},
1211 {"operand_check", set_check, 1},
3b22753a
L
1212#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1213 {"largecomm", handle_large_common, 0},
07a53e5c 1214#else
68d20676 1215 {"file", dwarf2_directive_file, 0},
07a53e5c
RH
1216 {"loc", dwarf2_directive_loc, 0},
1217 {"loc_mark_labels", dwarf2_directive_loc_mark_labels, 0},
3b22753a 1218#endif
6482c264
NC
1219#ifdef TE_PE
1220 {"secrel32", pe_directive_secrel, 0},
145667f8 1221 {"secidx", pe_directive_secidx, 0},
6482c264 1222#endif
29b0f896
AM
1223 {0, 0, 0}
1224};
1225
1226/* For interface with expression (). */
1227extern char *input_line_pointer;
1228
1229/* Hash table for instruction mnemonic lookup. */
629310ab 1230static htab_t op_hash;
29b0f896
AM
1231
1232/* Hash table for register lookup. */
629310ab 1233static htab_t reg_hash;
29b0f896 1234\f
ce8a8b2f
AM
1235 /* Various efficient no-op patterns for aligning code labels.
1236 Note: Don't try to assemble the instructions in the comments.
1237 0L and 0w are not legal. */
62a02d25
L
1238static const unsigned char f32_1[] =
1239 {0x90}; /* nop */
1240static const unsigned char f32_2[] =
1241 {0x66,0x90}; /* xchg %ax,%ax */
1242static const unsigned char f32_3[] =
1243 {0x8d,0x76,0x00}; /* leal 0(%esi),%esi */
1244static const unsigned char f32_4[] =
1245 {0x8d,0x74,0x26,0x00}; /* leal 0(%esi,1),%esi */
62a02d25
L
1246static const unsigned char f32_6[] =
1247 {0x8d,0xb6,0x00,0x00,0x00,0x00}; /* leal 0L(%esi),%esi */
1248static const unsigned char f32_7[] =
1249 {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal 0L(%esi,1),%esi */
62a02d25 1250static const unsigned char f16_3[] =
3ae729d5 1251 {0x8d,0x74,0x00}; /* lea 0(%si),%si */
62a02d25 1252static const unsigned char f16_4[] =
3ae729d5
L
1253 {0x8d,0xb4,0x00,0x00}; /* lea 0W(%si),%si */
1254static const unsigned char jump_disp8[] =
1255 {0xeb}; /* jmp disp8 */
1256static const unsigned char jump32_disp32[] =
1257 {0xe9}; /* jmp disp32 */
1258static const unsigned char jump16_disp32[] =
1259 {0x66,0xe9}; /* jmp disp32 */
62a02d25
L
1260/* 32-bit NOPs patterns. */
1261static const unsigned char *const f32_patt[] = {
3ae729d5 1262 f32_1, f32_2, f32_3, f32_4, NULL, f32_6, f32_7
62a02d25
L
1263};
1264/* 16-bit NOPs patterns. */
1265static const unsigned char *const f16_patt[] = {
3ae729d5 1266 f32_1, f32_2, f16_3, f16_4
62a02d25
L
1267};
1268/* nopl (%[re]ax) */
1269static const unsigned char alt_3[] =
1270 {0x0f,0x1f,0x00};
1271/* nopl 0(%[re]ax) */
1272static const unsigned char alt_4[] =
1273 {0x0f,0x1f,0x40,0x00};
1274/* nopl 0(%[re]ax,%[re]ax,1) */
1275static const unsigned char alt_5[] =
1276 {0x0f,0x1f,0x44,0x00,0x00};
1277/* nopw 0(%[re]ax,%[re]ax,1) */
1278static const unsigned char alt_6[] =
1279 {0x66,0x0f,0x1f,0x44,0x00,0x00};
1280/* nopl 0L(%[re]ax) */
1281static const unsigned char alt_7[] =
1282 {0x0f,0x1f,0x80,0x00,0x00,0x00,0x00};
1283/* nopl 0L(%[re]ax,%[re]ax,1) */
1284static const unsigned char alt_8[] =
1285 {0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1286/* nopw 0L(%[re]ax,%[re]ax,1) */
1287static const unsigned char alt_9[] =
1288 {0x66,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1289/* nopw %cs:0L(%[re]ax,%[re]ax,1) */
1290static const unsigned char alt_10[] =
1291 {0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
3ae729d5
L
1292/* data16 nopw %cs:0L(%eax,%eax,1) */
1293static const unsigned char alt_11[] =
1294 {0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
62a02d25
L
1295/* 32-bit and 64-bit NOPs patterns. */
1296static const unsigned char *const alt_patt[] = {
1297 f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
3ae729d5 1298 alt_9, alt_10, alt_11
62a02d25
L
1299};
1300
1301/* Genenerate COUNT bytes of NOPs to WHERE from PATT with the maximum
1302 size of a single NOP instruction MAX_SINGLE_NOP_SIZE. */
1303
1304static void
1305i386_output_nops (char *where, const unsigned char *const *patt,
1306 int count, int max_single_nop_size)
1307
1308{
3ae729d5
L
1309 /* Place the longer NOP first. */
1310 int last;
1311 int offset;
3076e594
NC
1312 const unsigned char *nops;
1313
1314 if (max_single_nop_size < 1)
1315 {
1316 as_fatal (_("i386_output_nops called to generate nops of at most %d bytes!"),
1317 max_single_nop_size);
1318 return;
1319 }
1320
1321 nops = patt[max_single_nop_size - 1];
3ae729d5
L
1322
1323 /* Use the smaller one if the requsted one isn't available. */
1324 if (nops == NULL)
62a02d25 1325 {
3ae729d5
L
1326 max_single_nop_size--;
1327 nops = patt[max_single_nop_size - 1];
62a02d25
L
1328 }
1329
3ae729d5
L
1330 last = count % max_single_nop_size;
1331
1332 count -= last;
1333 for (offset = 0; offset < count; offset += max_single_nop_size)
1334 memcpy (where + offset, nops, max_single_nop_size);
1335
1336 if (last)
1337 {
1338 nops = patt[last - 1];
1339 if (nops == NULL)
1340 {
1341 /* Use the smaller one plus one-byte NOP if the needed one
1342 isn't available. */
1343 last--;
1344 nops = patt[last - 1];
1345 memcpy (where + offset, nops, last);
1346 where[offset + last] = *patt[0];
1347 }
1348 else
1349 memcpy (where + offset, nops, last);
1350 }
62a02d25
L
1351}
1352
3ae729d5
L
1353static INLINE int
1354fits_in_imm7 (offsetT num)
1355{
1356 return (num & 0x7f) == num;
1357}
1358
1359static INLINE int
1360fits_in_imm31 (offsetT num)
1361{
1362 return (num & 0x7fffffff) == num;
1363}
62a02d25
L
1364
1365/* Genenerate COUNT bytes of NOPs to WHERE with the maximum size of a
1366 single NOP instruction LIMIT. */
1367
1368void
3ae729d5 1369i386_generate_nops (fragS *fragP, char *where, offsetT count, int limit)
62a02d25 1370{
3ae729d5 1371 const unsigned char *const *patt = NULL;
62a02d25 1372 int max_single_nop_size;
3ae729d5
L
1373 /* Maximum number of NOPs before switching to jump over NOPs. */
1374 int max_number_of_nops;
62a02d25 1375
3ae729d5 1376 switch (fragP->fr_type)
62a02d25 1377 {
3ae729d5
L
1378 case rs_fill_nop:
1379 case rs_align_code:
1380 break;
e379e5f3
L
1381 case rs_machine_dependent:
1382 /* Allow NOP padding for jumps and calls. */
1383 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
1384 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
1385 break;
1386 /* Fall through. */
3ae729d5 1387 default:
62a02d25
L
1388 return;
1389 }
1390
ccc9c027
L
1391 /* We need to decide which NOP sequence to use for 32bit and
1392 64bit. When -mtune= is used:
4eed87de 1393
76bc74dc
L
1394 1. For PROCESSOR_I386, PROCESSOR_I486, PROCESSOR_PENTIUM and
1395 PROCESSOR_GENERIC32, f32_patt will be used.
80b8656c
L
1396 2. For the rest, alt_patt will be used.
1397
1398 When -mtune= isn't used, alt_patt will be used if
22109423 1399 cpu_arch_isa_flags has CpuNop. Otherwise, f32_patt will
76bc74dc 1400 be used.
ccc9c027
L
1401
1402 When -march= or .arch is used, we can't use anything beyond
1403 cpu_arch_isa_flags. */
1404
1405 if (flag_code == CODE_16BIT)
1406 {
3ae729d5
L
1407 patt = f16_patt;
1408 max_single_nop_size = sizeof (f16_patt) / sizeof (f16_patt[0]);
1409 /* Limit number of NOPs to 2 in 16-bit mode. */
1410 max_number_of_nops = 2;
252b5132 1411 }
33fef721 1412 else
ccc9c027 1413 {
fbf3f584 1414 if (fragP->tc_frag_data.isa == PROCESSOR_UNKNOWN)
ccc9c027
L
1415 {
1416 /* PROCESSOR_UNKNOWN means that all ISAs may be used. */
1417 switch (cpu_arch_tune)
1418 {
1419 case PROCESSOR_UNKNOWN:
1420 /* We use cpu_arch_isa_flags to check if we SHOULD
22109423
L
1421 optimize with nops. */
1422 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1423 patt = alt_patt;
ccc9c027
L
1424 else
1425 patt = f32_patt;
1426 break;
ccc9c027
L
1427 case PROCESSOR_PENTIUM4:
1428 case PROCESSOR_NOCONA:
ef05d495 1429 case PROCESSOR_CORE:
76bc74dc 1430 case PROCESSOR_CORE2:
bd5295b2 1431 case PROCESSOR_COREI7:
76bc74dc 1432 case PROCESSOR_GENERIC64:
ccc9c027
L
1433 case PROCESSOR_K6:
1434 case PROCESSOR_ATHLON:
1435 case PROCESSOR_K8:
4eed87de 1436 case PROCESSOR_AMDFAM10:
8aedb9fe 1437 case PROCESSOR_BD:
029f3522 1438 case PROCESSOR_ZNVER:
7b458c12 1439 case PROCESSOR_BT:
80b8656c 1440 patt = alt_patt;
ccc9c027 1441 break;
76bc74dc 1442 case PROCESSOR_I386:
ccc9c027
L
1443 case PROCESSOR_I486:
1444 case PROCESSOR_PENTIUM:
2dde1948 1445 case PROCESSOR_PENTIUMPRO:
81486035 1446 case PROCESSOR_IAMCU:
ccc9c027
L
1447 case PROCESSOR_GENERIC32:
1448 patt = f32_patt;
1449 break;
c368d2a8
JB
1450 case PROCESSOR_NONE:
1451 abort ();
4eed87de 1452 }
ccc9c027
L
1453 }
1454 else
1455 {
fbf3f584 1456 switch (fragP->tc_frag_data.tune)
ccc9c027
L
1457 {
1458 case PROCESSOR_UNKNOWN:
e6a14101 1459 /* When cpu_arch_isa is set, cpu_arch_tune shouldn't be
ccc9c027
L
1460 PROCESSOR_UNKNOWN. */
1461 abort ();
1462 break;
1463
76bc74dc 1464 case PROCESSOR_I386:
ccc9c027
L
1465 case PROCESSOR_I486:
1466 case PROCESSOR_PENTIUM:
81486035 1467 case PROCESSOR_IAMCU:
ccc9c027
L
1468 case PROCESSOR_K6:
1469 case PROCESSOR_ATHLON:
1470 case PROCESSOR_K8:
4eed87de 1471 case PROCESSOR_AMDFAM10:
8aedb9fe 1472 case PROCESSOR_BD:
029f3522 1473 case PROCESSOR_ZNVER:
7b458c12 1474 case PROCESSOR_BT:
ccc9c027
L
1475 case PROCESSOR_GENERIC32:
1476 /* We use cpu_arch_isa_flags to check if we CAN optimize
22109423
L
1477 with nops. */
1478 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1479 patt = alt_patt;
ccc9c027
L
1480 else
1481 patt = f32_patt;
1482 break;
76bc74dc
L
1483 case PROCESSOR_PENTIUMPRO:
1484 case PROCESSOR_PENTIUM4:
1485 case PROCESSOR_NOCONA:
1486 case PROCESSOR_CORE:
ef05d495 1487 case PROCESSOR_CORE2:
bd5295b2 1488 case PROCESSOR_COREI7:
22109423 1489 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1490 patt = alt_patt;
ccc9c027
L
1491 else
1492 patt = f32_patt;
1493 break;
1494 case PROCESSOR_GENERIC64:
80b8656c 1495 patt = alt_patt;
ccc9c027 1496 break;
c368d2a8
JB
1497 case PROCESSOR_NONE:
1498 abort ();
4eed87de 1499 }
ccc9c027
L
1500 }
1501
76bc74dc
L
1502 if (patt == f32_patt)
1503 {
3ae729d5
L
1504 max_single_nop_size = sizeof (f32_patt) / sizeof (f32_patt[0]);
1505 /* Limit number of NOPs to 2 for older processors. */
1506 max_number_of_nops = 2;
76bc74dc
L
1507 }
1508 else
1509 {
3ae729d5
L
1510 max_single_nop_size = sizeof (alt_patt) / sizeof (alt_patt[0]);
1511 /* Limit number of NOPs to 7 for newer processors. */
1512 max_number_of_nops = 7;
1513 }
1514 }
1515
1516 if (limit == 0)
1517 limit = max_single_nop_size;
1518
1519 if (fragP->fr_type == rs_fill_nop)
1520 {
1521 /* Output NOPs for .nop directive. */
1522 if (limit > max_single_nop_size)
1523 {
1524 as_bad_where (fragP->fr_file, fragP->fr_line,
1525 _("invalid single nop size: %d "
1526 "(expect within [0, %d])"),
1527 limit, max_single_nop_size);
1528 return;
1529 }
1530 }
e379e5f3 1531 else if (fragP->fr_type != rs_machine_dependent)
3ae729d5
L
1532 fragP->fr_var = count;
1533
1534 if ((count / max_single_nop_size) > max_number_of_nops)
1535 {
1536 /* Generate jump over NOPs. */
1537 offsetT disp = count - 2;
1538 if (fits_in_imm7 (disp))
1539 {
1540 /* Use "jmp disp8" if possible. */
1541 count = disp;
1542 where[0] = jump_disp8[0];
1543 where[1] = count;
1544 where += 2;
1545 }
1546 else
1547 {
1548 unsigned int size_of_jump;
1549
1550 if (flag_code == CODE_16BIT)
1551 {
1552 where[0] = jump16_disp32[0];
1553 where[1] = jump16_disp32[1];
1554 size_of_jump = 2;
1555 }
1556 else
1557 {
1558 where[0] = jump32_disp32[0];
1559 size_of_jump = 1;
1560 }
1561
1562 count -= size_of_jump + 4;
1563 if (!fits_in_imm31 (count))
1564 {
1565 as_bad_where (fragP->fr_file, fragP->fr_line,
1566 _("jump over nop padding out of range"));
1567 return;
1568 }
1569
1570 md_number_to_chars (where + size_of_jump, count, 4);
1571 where += size_of_jump + 4;
76bc74dc 1572 }
ccc9c027 1573 }
3ae729d5
L
1574
1575 /* Generate multiple NOPs. */
1576 i386_output_nops (where, patt, count, limit);
252b5132
RH
1577}
1578
c6fb90c8 1579static INLINE int
0dfbf9d7 1580operand_type_all_zero (const union i386_operand_type *x)
40fb9820 1581{
0dfbf9d7 1582 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1583 {
1584 case 3:
0dfbf9d7 1585 if (x->array[2])
c6fb90c8 1586 return 0;
1a0670f3 1587 /* Fall through. */
c6fb90c8 1588 case 2:
0dfbf9d7 1589 if (x->array[1])
c6fb90c8 1590 return 0;
1a0670f3 1591 /* Fall through. */
c6fb90c8 1592 case 1:
0dfbf9d7 1593 return !x->array[0];
c6fb90c8
L
1594 default:
1595 abort ();
1596 }
40fb9820
L
1597}
1598
c6fb90c8 1599static INLINE void
0dfbf9d7 1600operand_type_set (union i386_operand_type *x, unsigned int v)
40fb9820 1601{
0dfbf9d7 1602 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1603 {
1604 case 3:
0dfbf9d7 1605 x->array[2] = v;
1a0670f3 1606 /* Fall through. */
c6fb90c8 1607 case 2:
0dfbf9d7 1608 x->array[1] = v;
1a0670f3 1609 /* Fall through. */
c6fb90c8 1610 case 1:
0dfbf9d7 1611 x->array[0] = v;
1a0670f3 1612 /* Fall through. */
c6fb90c8
L
1613 break;
1614 default:
1615 abort ();
1616 }
bab6aec1
JB
1617
1618 x->bitfield.class = ClassNone;
75e5731b 1619 x->bitfield.instance = InstanceNone;
c6fb90c8 1620}
40fb9820 1621
c6fb90c8 1622static INLINE int
0dfbf9d7
L
1623operand_type_equal (const union i386_operand_type *x,
1624 const union i386_operand_type *y)
c6fb90c8 1625{
0dfbf9d7 1626 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1627 {
1628 case 3:
0dfbf9d7 1629 if (x->array[2] != y->array[2])
c6fb90c8 1630 return 0;
1a0670f3 1631 /* Fall through. */
c6fb90c8 1632 case 2:
0dfbf9d7 1633 if (x->array[1] != y->array[1])
c6fb90c8 1634 return 0;
1a0670f3 1635 /* Fall through. */
c6fb90c8 1636 case 1:
0dfbf9d7 1637 return x->array[0] == y->array[0];
c6fb90c8
L
1638 break;
1639 default:
1640 abort ();
1641 }
1642}
40fb9820 1643
0dfbf9d7
L
1644static INLINE int
1645cpu_flags_all_zero (const union i386_cpu_flags *x)
1646{
1647 switch (ARRAY_SIZE(x->array))
1648 {
75f8266a
KL
1649 case 5:
1650 if (x->array[4])
1651 return 0;
1652 /* Fall through. */
53467f57
IT
1653 case 4:
1654 if (x->array[3])
1655 return 0;
1656 /* Fall through. */
0dfbf9d7
L
1657 case 3:
1658 if (x->array[2])
1659 return 0;
1a0670f3 1660 /* Fall through. */
0dfbf9d7
L
1661 case 2:
1662 if (x->array[1])
1663 return 0;
1a0670f3 1664 /* Fall through. */
0dfbf9d7
L
1665 case 1:
1666 return !x->array[0];
1667 default:
1668 abort ();
1669 }
1670}
1671
0dfbf9d7
L
1672static INLINE int
1673cpu_flags_equal (const union i386_cpu_flags *x,
1674 const union i386_cpu_flags *y)
1675{
1676 switch (ARRAY_SIZE(x->array))
1677 {
75f8266a
KL
1678 case 5:
1679 if (x->array[4] != y->array[4])
1680 return 0;
1681 /* Fall through. */
53467f57
IT
1682 case 4:
1683 if (x->array[3] != y->array[3])
1684 return 0;
1685 /* Fall through. */
0dfbf9d7
L
1686 case 3:
1687 if (x->array[2] != y->array[2])
1688 return 0;
1a0670f3 1689 /* Fall through. */
0dfbf9d7
L
1690 case 2:
1691 if (x->array[1] != y->array[1])
1692 return 0;
1a0670f3 1693 /* Fall through. */
0dfbf9d7
L
1694 case 1:
1695 return x->array[0] == y->array[0];
1696 break;
1697 default:
1698 abort ();
1699 }
1700}
c6fb90c8
L
1701
1702static INLINE int
1703cpu_flags_check_cpu64 (i386_cpu_flags f)
1704{
1705 return !((flag_code == CODE_64BIT && f.bitfield.cpuno64)
1706 || (flag_code != CODE_64BIT && f.bitfield.cpu64));
40fb9820
L
1707}
1708
c6fb90c8
L
1709static INLINE i386_cpu_flags
1710cpu_flags_and (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1711{
c6fb90c8
L
1712 switch (ARRAY_SIZE (x.array))
1713 {
75f8266a
KL
1714 case 5:
1715 x.array [4] &= y.array [4];
1716 /* Fall through. */
53467f57
IT
1717 case 4:
1718 x.array [3] &= y.array [3];
1719 /* Fall through. */
c6fb90c8
L
1720 case 3:
1721 x.array [2] &= y.array [2];
1a0670f3 1722 /* Fall through. */
c6fb90c8
L
1723 case 2:
1724 x.array [1] &= y.array [1];
1a0670f3 1725 /* Fall through. */
c6fb90c8
L
1726 case 1:
1727 x.array [0] &= y.array [0];
1728 break;
1729 default:
1730 abort ();
1731 }
1732 return x;
1733}
40fb9820 1734
c6fb90c8
L
1735static INLINE i386_cpu_flags
1736cpu_flags_or (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1737{
c6fb90c8 1738 switch (ARRAY_SIZE (x.array))
40fb9820 1739 {
75f8266a
KL
1740 case 5:
1741 x.array [4] |= y.array [4];
1742 /* Fall through. */
53467f57
IT
1743 case 4:
1744 x.array [3] |= y.array [3];
1745 /* Fall through. */
c6fb90c8
L
1746 case 3:
1747 x.array [2] |= y.array [2];
1a0670f3 1748 /* Fall through. */
c6fb90c8
L
1749 case 2:
1750 x.array [1] |= y.array [1];
1a0670f3 1751 /* Fall through. */
c6fb90c8
L
1752 case 1:
1753 x.array [0] |= y.array [0];
40fb9820
L
1754 break;
1755 default:
1756 abort ();
1757 }
40fb9820
L
1758 return x;
1759}
1760
309d3373
JB
1761static INLINE i386_cpu_flags
1762cpu_flags_and_not (i386_cpu_flags x, i386_cpu_flags y)
1763{
1764 switch (ARRAY_SIZE (x.array))
1765 {
75f8266a
KL
1766 case 5:
1767 x.array [4] &= ~y.array [4];
1768 /* Fall through. */
53467f57
IT
1769 case 4:
1770 x.array [3] &= ~y.array [3];
1771 /* Fall through. */
309d3373
JB
1772 case 3:
1773 x.array [2] &= ~y.array [2];
1a0670f3 1774 /* Fall through. */
309d3373
JB
1775 case 2:
1776 x.array [1] &= ~y.array [1];
1a0670f3 1777 /* Fall through. */
309d3373
JB
1778 case 1:
1779 x.array [0] &= ~y.array [0];
1780 break;
1781 default:
1782 abort ();
1783 }
1784 return x;
1785}
1786
6c0946d0
JB
1787static const i386_cpu_flags avx512 = CPU_ANY_AVX512F_FLAGS;
1788
c0f3af97
L
1789#define CPU_FLAGS_ARCH_MATCH 0x1
1790#define CPU_FLAGS_64BIT_MATCH 0x2
1791
c0f3af97 1792#define CPU_FLAGS_PERFECT_MATCH \
db12e14e 1793 (CPU_FLAGS_ARCH_MATCH | CPU_FLAGS_64BIT_MATCH)
c0f3af97
L
1794
1795/* Return CPU flags match bits. */
3629bb00 1796
40fb9820 1797static int
d3ce72d0 1798cpu_flags_match (const insn_template *t)
40fb9820 1799{
c0f3af97
L
1800 i386_cpu_flags x = t->cpu_flags;
1801 int match = cpu_flags_check_cpu64 (x) ? CPU_FLAGS_64BIT_MATCH : 0;
40fb9820
L
1802
1803 x.bitfield.cpu64 = 0;
1804 x.bitfield.cpuno64 = 0;
1805
0dfbf9d7 1806 if (cpu_flags_all_zero (&x))
c0f3af97
L
1807 {
1808 /* This instruction is available on all archs. */
db12e14e 1809 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 1810 }
3629bb00
L
1811 else
1812 {
c0f3af97 1813 /* This instruction is available only on some archs. */
3629bb00
L
1814 i386_cpu_flags cpu = cpu_arch_flags;
1815
ab592e75
JB
1816 /* AVX512VL is no standalone feature - match it and then strip it. */
1817 if (x.bitfield.cpuavx512vl && !cpu.bitfield.cpuavx512vl)
1818 return match;
1819 x.bitfield.cpuavx512vl = 0;
1820
22c36940
JB
1821 /* AVX and AVX2 present at the same time express an operand size
1822 dependency - strip AVX2 for the purposes here. The operand size
1823 dependent check occurs in check_vecOperands(). */
1824 if (x.bitfield.cpuavx && x.bitfield.cpuavx2)
1825 x.bitfield.cpuavx2 = 0;
1826
3629bb00 1827 cpu = cpu_flags_and (x, cpu);
c0f3af97
L
1828 if (!cpu_flags_all_zero (&cpu))
1829 {
57392598 1830 if (x.bitfield.cpuavx)
a5ff0eb2 1831 {
929f69fa 1832 /* We need to check a few extra flags with AVX. */
b9d49817 1833 if (cpu.bitfield.cpuavx
40d231b4
JB
1834 && (!t->opcode_modifier.sse2avx
1835 || (sse2avx && !i.prefix[DATA_PREFIX]))
b9d49817 1836 && (!x.bitfield.cpuaes || cpu.bitfield.cpuaes)
929f69fa 1837 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
b9d49817
JB
1838 && (!x.bitfield.cpupclmul || cpu.bitfield.cpupclmul))
1839 match |= CPU_FLAGS_ARCH_MATCH;
a5ff0eb2 1840 }
929f69fa
JB
1841 else if (x.bitfield.cpuavx512f)
1842 {
1843 /* We need to check a few extra flags with AVX512F. */
1844 if (cpu.bitfield.cpuavx512f
1845 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
1846 && (!x.bitfield.cpuvaes || cpu.bitfield.cpuvaes)
1847 && (!x.bitfield.cpuvpclmulqdq || cpu.bitfield.cpuvpclmulqdq))
1848 match |= CPU_FLAGS_ARCH_MATCH;
1849 }
a5ff0eb2 1850 else
db12e14e 1851 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 1852 }
3629bb00 1853 }
c0f3af97 1854 return match;
40fb9820
L
1855}
1856
c6fb90c8
L
1857static INLINE i386_operand_type
1858operand_type_and (i386_operand_type x, i386_operand_type y)
40fb9820 1859{
bab6aec1
JB
1860 if (x.bitfield.class != y.bitfield.class)
1861 x.bitfield.class = ClassNone;
75e5731b
JB
1862 if (x.bitfield.instance != y.bitfield.instance)
1863 x.bitfield.instance = InstanceNone;
bab6aec1 1864
c6fb90c8
L
1865 switch (ARRAY_SIZE (x.array))
1866 {
1867 case 3:
1868 x.array [2] &= y.array [2];
1a0670f3 1869 /* Fall through. */
c6fb90c8
L
1870 case 2:
1871 x.array [1] &= y.array [1];
1a0670f3 1872 /* Fall through. */
c6fb90c8
L
1873 case 1:
1874 x.array [0] &= y.array [0];
1875 break;
1876 default:
1877 abort ();
1878 }
1879 return x;
40fb9820
L
1880}
1881
73053c1f
JB
1882static INLINE i386_operand_type
1883operand_type_and_not (i386_operand_type x, i386_operand_type y)
1884{
bab6aec1 1885 gas_assert (y.bitfield.class == ClassNone);
75e5731b 1886 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 1887
73053c1f
JB
1888 switch (ARRAY_SIZE (x.array))
1889 {
1890 case 3:
1891 x.array [2] &= ~y.array [2];
1892 /* Fall through. */
1893 case 2:
1894 x.array [1] &= ~y.array [1];
1895 /* Fall through. */
1896 case 1:
1897 x.array [0] &= ~y.array [0];
1898 break;
1899 default:
1900 abort ();
1901 }
1902 return x;
1903}
1904
c6fb90c8
L
1905static INLINE i386_operand_type
1906operand_type_or (i386_operand_type x, i386_operand_type y)
40fb9820 1907{
bab6aec1
JB
1908 gas_assert (x.bitfield.class == ClassNone ||
1909 y.bitfield.class == ClassNone ||
1910 x.bitfield.class == y.bitfield.class);
75e5731b
JB
1911 gas_assert (x.bitfield.instance == InstanceNone ||
1912 y.bitfield.instance == InstanceNone ||
1913 x.bitfield.instance == y.bitfield.instance);
bab6aec1 1914
c6fb90c8 1915 switch (ARRAY_SIZE (x.array))
40fb9820 1916 {
c6fb90c8
L
1917 case 3:
1918 x.array [2] |= y.array [2];
1a0670f3 1919 /* Fall through. */
c6fb90c8
L
1920 case 2:
1921 x.array [1] |= y.array [1];
1a0670f3 1922 /* Fall through. */
c6fb90c8
L
1923 case 1:
1924 x.array [0] |= y.array [0];
40fb9820
L
1925 break;
1926 default:
1927 abort ();
1928 }
c6fb90c8
L
1929 return x;
1930}
40fb9820 1931
c6fb90c8
L
1932static INLINE i386_operand_type
1933operand_type_xor (i386_operand_type x, i386_operand_type y)
1934{
bab6aec1 1935 gas_assert (y.bitfield.class == ClassNone);
75e5731b 1936 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 1937
c6fb90c8
L
1938 switch (ARRAY_SIZE (x.array))
1939 {
1940 case 3:
1941 x.array [2] ^= y.array [2];
1a0670f3 1942 /* Fall through. */
c6fb90c8
L
1943 case 2:
1944 x.array [1] ^= y.array [1];
1a0670f3 1945 /* Fall through. */
c6fb90c8
L
1946 case 1:
1947 x.array [0] ^= y.array [0];
1948 break;
1949 default:
1950 abort ();
1951 }
40fb9820
L
1952 return x;
1953}
1954
40fb9820 1955static const i386_operand_type disp16_32 = OPERAND_TYPE_DISP16_32;
bab6aec1 1956static const i386_operand_type anydisp = OPERAND_TYPE_ANYDISP;
40fb9820 1957static const i386_operand_type regxmm = OPERAND_TYPE_REGXMM;
40fb9820
L
1958static const i386_operand_type imm16 = OPERAND_TYPE_IMM16;
1959static const i386_operand_type imm32 = OPERAND_TYPE_IMM32;
1960static const i386_operand_type imm32s = OPERAND_TYPE_IMM32S;
40fb9820
L
1961static const i386_operand_type imm16_32 = OPERAND_TYPE_IMM16_32;
1962static const i386_operand_type imm16_32s = OPERAND_TYPE_IMM16_32S;
1963static const i386_operand_type imm16_32_32s = OPERAND_TYPE_IMM16_32_32S;
1964
1965enum operand_type
1966{
1967 reg,
40fb9820
L
1968 imm,
1969 disp,
1970 anymem
1971};
1972
c6fb90c8 1973static INLINE int
40fb9820
L
1974operand_type_check (i386_operand_type t, enum operand_type c)
1975{
1976 switch (c)
1977 {
1978 case reg:
bab6aec1 1979 return t.bitfield.class == Reg;
40fb9820 1980
40fb9820
L
1981 case imm:
1982 return (t.bitfield.imm8
1983 || t.bitfield.imm8s
1984 || t.bitfield.imm16
1985 || t.bitfield.imm32
1986 || t.bitfield.imm32s
1987 || t.bitfield.imm64);
1988
1989 case disp:
1990 return (t.bitfield.disp8
1991 || t.bitfield.disp16
1992 || t.bitfield.disp32
40fb9820
L
1993 || t.bitfield.disp64);
1994
1995 case anymem:
1996 return (t.bitfield.disp8
1997 || t.bitfield.disp16
1998 || t.bitfield.disp32
40fb9820
L
1999 || t.bitfield.disp64
2000 || t.bitfield.baseindex);
2001
2002 default:
2003 abort ();
2004 }
2cfe26b6
AM
2005
2006 return 0;
40fb9820
L
2007}
2008
7a54636a
L
2009/* Return 1 if there is no conflict in 8bit/16bit/32bit/64bit/80bit size
2010 between operand GIVEN and opeand WANTED for instruction template T. */
5c07affc
L
2011
2012static INLINE int
7a54636a
L
2013match_operand_size (const insn_template *t, unsigned int wanted,
2014 unsigned int given)
5c07affc 2015{
3ac21baa
JB
2016 return !((i.types[given].bitfield.byte
2017 && !t->operand_types[wanted].bitfield.byte)
2018 || (i.types[given].bitfield.word
2019 && !t->operand_types[wanted].bitfield.word)
2020 || (i.types[given].bitfield.dword
2021 && !t->operand_types[wanted].bitfield.dword)
2022 || (i.types[given].bitfield.qword
2023 && !t->operand_types[wanted].bitfield.qword)
2024 || (i.types[given].bitfield.tbyte
2025 && !t->operand_types[wanted].bitfield.tbyte));
5c07affc
L
2026}
2027
dd40ce22
L
2028/* Return 1 if there is no conflict in SIMD register between operand
2029 GIVEN and opeand WANTED for instruction template T. */
1b54b8d7
JB
2030
2031static INLINE int
dd40ce22
L
2032match_simd_size (const insn_template *t, unsigned int wanted,
2033 unsigned int given)
1b54b8d7 2034{
3ac21baa
JB
2035 return !((i.types[given].bitfield.xmmword
2036 && !t->operand_types[wanted].bitfield.xmmword)
2037 || (i.types[given].bitfield.ymmword
2038 && !t->operand_types[wanted].bitfield.ymmword)
2039 || (i.types[given].bitfield.zmmword
260cd341
LC
2040 && !t->operand_types[wanted].bitfield.zmmword)
2041 || (i.types[given].bitfield.tmmword
2042 && !t->operand_types[wanted].bitfield.tmmword));
1b54b8d7
JB
2043}
2044
7a54636a
L
2045/* Return 1 if there is no conflict in any size between operand GIVEN
2046 and opeand WANTED for instruction template T. */
5c07affc
L
2047
2048static INLINE int
dd40ce22
L
2049match_mem_size (const insn_template *t, unsigned int wanted,
2050 unsigned int given)
5c07affc 2051{
7a54636a 2052 return (match_operand_size (t, wanted, given)
3ac21baa 2053 && !((i.types[given].bitfield.unspecified
5273a3cd 2054 && !i.broadcast.type
a5748e0d 2055 && !i.broadcast.bytes
3ac21baa
JB
2056 && !t->operand_types[wanted].bitfield.unspecified)
2057 || (i.types[given].bitfield.fword
2058 && !t->operand_types[wanted].bitfield.fword)
1b54b8d7
JB
2059 /* For scalar opcode templates to allow register and memory
2060 operands at the same time, some special casing is needed
d6793fa1
JB
2061 here. Also for v{,p}broadcast*, {,v}pmov{s,z}*, and
2062 down-conversion vpmov*. */
3528c362 2063 || ((t->operand_types[wanted].bitfield.class == RegSIMD
bc49bfd8
JB
2064 && t->operand_types[wanted].bitfield.byte
2065 + t->operand_types[wanted].bitfield.word
2066 + t->operand_types[wanted].bitfield.dword
2067 + t->operand_types[wanted].bitfield.qword
2068 > !!t->opcode_modifier.broadcast)
3ac21baa
JB
2069 ? (i.types[given].bitfield.xmmword
2070 || i.types[given].bitfield.ymmword
2071 || i.types[given].bitfield.zmmword)
2072 : !match_simd_size(t, wanted, given))));
5c07affc
L
2073}
2074
3ac21baa
JB
2075/* Return value has MATCH_STRAIGHT set if there is no size conflict on any
2076 operands for instruction template T, and it has MATCH_REVERSE set if there
2077 is no size conflict on any operands for the template with operands reversed
2078 (and the template allows for reversing in the first place). */
5c07affc 2079
3ac21baa
JB
2080#define MATCH_STRAIGHT 1
2081#define MATCH_REVERSE 2
2082
2083static INLINE unsigned int
d3ce72d0 2084operand_size_match (const insn_template *t)
5c07affc 2085{
3ac21baa 2086 unsigned int j, match = MATCH_STRAIGHT;
5c07affc 2087
0cfa3eb3 2088 /* Don't check non-absolute jump instructions. */
5c07affc 2089 if (t->opcode_modifier.jump
0cfa3eb3 2090 && t->opcode_modifier.jump != JUMP_ABSOLUTE)
5c07affc
L
2091 return match;
2092
2093 /* Check memory and accumulator operand size. */
2094 for (j = 0; j < i.operands; j++)
2095 {
3528c362
JB
2096 if (i.types[j].bitfield.class != Reg
2097 && i.types[j].bitfield.class != RegSIMD
255571cd 2098 && t->opcode_modifier.operandconstraint == ANY_SIZE)
5c07affc
L
2099 continue;
2100
bab6aec1 2101 if (t->operand_types[j].bitfield.class == Reg
7a54636a 2102 && !match_operand_size (t, j, j))
5c07affc
L
2103 {
2104 match = 0;
2105 break;
2106 }
2107
3528c362 2108 if (t->operand_types[j].bitfield.class == RegSIMD
3ac21baa 2109 && !match_simd_size (t, j, j))
1b54b8d7
JB
2110 {
2111 match = 0;
2112 break;
2113 }
2114
75e5731b 2115 if (t->operand_types[j].bitfield.instance == Accum
7a54636a 2116 && (!match_operand_size (t, j, j) || !match_simd_size (t, j, j)))
1b54b8d7
JB
2117 {
2118 match = 0;
2119 break;
2120 }
2121
c48dadc9 2122 if ((i.flags[j] & Operand_Mem) && !match_mem_size (t, j, j))
5c07affc
L
2123 {
2124 match = 0;
2125 break;
2126 }
2127 }
2128
3ac21baa 2129 if (!t->opcode_modifier.d)
7b94647a 2130 return match;
5c07affc
L
2131
2132 /* Check reverse. */
8bd915b7
JB
2133 gas_assert ((i.operands >= 2 && i.operands <= 3)
2134 || t->opcode_modifier.vexsources);
5c07affc 2135
f5eb1d70 2136 for (j = 0; j < i.operands; j++)
5c07affc 2137 {
f5eb1d70
JB
2138 unsigned int given = i.operands - j - 1;
2139
8bd915b7
JB
2140 /* For 4- and 5-operand insns VEX.W controls just the first two
2141 register operands. */
2142 if (t->opcode_modifier.vexsources)
2143 given = j < 2 ? 1 - j : j;
2144
bab6aec1 2145 if (t->operand_types[j].bitfield.class == Reg
f5eb1d70 2146 && !match_operand_size (t, j, given))
7b94647a 2147 return match;
5c07affc 2148
3528c362 2149 if (t->operand_types[j].bitfield.class == RegSIMD
f5eb1d70 2150 && !match_simd_size (t, j, given))
7b94647a 2151 return match;
dbbc8b7e 2152
75e5731b 2153 if (t->operand_types[j].bitfield.instance == Accum
f5eb1d70
JB
2154 && (!match_operand_size (t, j, given)
2155 || !match_simd_size (t, j, given)))
7b94647a 2156 return match;
dbbc8b7e 2157
f5eb1d70 2158 if ((i.flags[given] & Operand_Mem) && !match_mem_size (t, j, given))
7b94647a 2159 return match;
5c07affc
L
2160 }
2161
3ac21baa 2162 return match | MATCH_REVERSE;
5c07affc
L
2163}
2164
c6fb90c8 2165static INLINE int
40fb9820
L
2166operand_type_match (i386_operand_type overlap,
2167 i386_operand_type given)
2168{
2169 i386_operand_type temp = overlap;
2170
7d5e4556 2171 temp.bitfield.unspecified = 0;
5c07affc
L
2172 temp.bitfield.byte = 0;
2173 temp.bitfield.word = 0;
2174 temp.bitfield.dword = 0;
2175 temp.bitfield.fword = 0;
2176 temp.bitfield.qword = 0;
2177 temp.bitfield.tbyte = 0;
2178 temp.bitfield.xmmword = 0;
c0f3af97 2179 temp.bitfield.ymmword = 0;
43234a1e 2180 temp.bitfield.zmmword = 0;
260cd341 2181 temp.bitfield.tmmword = 0;
0dfbf9d7 2182 if (operand_type_all_zero (&temp))
891edac4 2183 goto mismatch;
40fb9820 2184
6f2f06be 2185 if (given.bitfield.baseindex == overlap.bitfield.baseindex)
891edac4
L
2186 return 1;
2187
dc1e8a47 2188 mismatch:
a65babc9 2189 i.error = operand_type_mismatch;
891edac4 2190 return 0;
40fb9820
L
2191}
2192
7d5e4556 2193/* If given types g0 and g1 are registers they must be of the same type
10c17abd 2194 unless the expected operand type register overlap is null.
5de4d9ef 2195 Some Intel syntax memory operand size checking also happens here. */
40fb9820 2196
c6fb90c8 2197static INLINE int
dc821c5f 2198operand_type_register_match (i386_operand_type g0,
40fb9820 2199 i386_operand_type t0,
40fb9820
L
2200 i386_operand_type g1,
2201 i386_operand_type t1)
2202{
bab6aec1 2203 if (g0.bitfield.class != Reg
3528c362 2204 && g0.bitfield.class != RegSIMD
10c17abd
JB
2205 && (!operand_type_check (g0, anymem)
2206 || g0.bitfield.unspecified
5de4d9ef
JB
2207 || (t0.bitfield.class != Reg
2208 && t0.bitfield.class != RegSIMD)))
40fb9820
L
2209 return 1;
2210
bab6aec1 2211 if (g1.bitfield.class != Reg
3528c362 2212 && g1.bitfield.class != RegSIMD
10c17abd
JB
2213 && (!operand_type_check (g1, anymem)
2214 || g1.bitfield.unspecified
5de4d9ef
JB
2215 || (t1.bitfield.class != Reg
2216 && t1.bitfield.class != RegSIMD)))
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
252b5132 2972void
7016a5d5 2973md_begin (void)
252b5132 2974{
86fa6981
L
2975 /* Support pseudo prefixes like {disp32}. */
2976 lex_type ['{'] = LEX_BEGIN_NAME;
2977
47926f60 2978 /* Initialize op_hash hash table. */
629310ab 2979 op_hash = str_htab_create ();
252b5132
RH
2980
2981 {
d3ce72d0 2982 const insn_template *optab;
29b0f896 2983 templates *core_optab;
252b5132 2984
47926f60
KH
2985 /* Setup for loop. */
2986 optab = i386_optab;
654d6f31 2987 core_optab = notes_alloc (sizeof (*core_optab));
252b5132
RH
2988 core_optab->start = optab;
2989
2990 while (1)
2991 {
2992 ++optab;
2993 if (optab->name == NULL
2994 || strcmp (optab->name, (optab - 1)->name) != 0)
2995 {
2996 /* different name --> ship out current template list;
47926f60 2997 add to hash table; & begin anew. */
252b5132 2998 core_optab->end = optab;
fe0e921f
AM
2999 if (str_hash_insert (op_hash, (optab - 1)->name, core_optab, 0))
3000 as_fatal (_("duplicate %s"), (optab - 1)->name);
3001
252b5132
RH
3002 if (optab->name == NULL)
3003 break;
654d6f31 3004 core_optab = notes_alloc (sizeof (*core_optab));
252b5132
RH
3005 core_optab->start = optab;
3006 }
3007 }
3008 }
3009
47926f60 3010 /* Initialize reg_hash hash table. */
629310ab 3011 reg_hash = str_htab_create ();
252b5132 3012 {
29b0f896 3013 const reg_entry *regtab;
c3fe08fa 3014 unsigned int regtab_size = i386_regtab_size;
252b5132 3015
c3fe08fa 3016 for (regtab = i386_regtab; regtab_size--; regtab++)
6225c532 3017 {
6288d05f
JB
3018 switch (regtab->reg_type.bitfield.class)
3019 {
3020 case Reg:
34684862
JB
3021 if (regtab->reg_type.bitfield.dword)
3022 {
3023 if (regtab->reg_type.bitfield.instance == Accum)
3024 reg_eax = regtab;
3025 }
3026 else if (regtab->reg_type.bitfield.tbyte)
6288d05f
JB
3027 {
3028 /* There's no point inserting st(<N>) in the hash table, as
3029 parentheses aren't included in register_chars[] anyway. */
3030 if (regtab->reg_type.bitfield.instance != Accum)
3031 continue;
3032 reg_st0 = regtab;
3033 }
3034 break;
3035
5e042380
JB
3036 case SReg:
3037 switch (regtab->reg_num)
3038 {
3039 case 0: reg_es = regtab; break;
3040 case 2: reg_ss = regtab; break;
3041 case 3: reg_ds = regtab; break;
3042 }
3043 break;
3044
6288d05f
JB
3045 case RegMask:
3046 if (!regtab->reg_num)
3047 reg_k0 = regtab;
3048 break;
3049 }
3050
6225c532
JB
3051 if (str_hash_insert (reg_hash, regtab->reg_name, regtab, 0) != NULL)
3052 as_fatal (_("duplicate %s"), regtab->reg_name);
6225c532 3053 }
252b5132
RH
3054 }
3055
47926f60 3056 /* Fill in lexical tables: mnemonic_chars, operand_chars. */
252b5132 3057 {
29b0f896
AM
3058 int c;
3059 char *p;
252b5132
RH
3060
3061 for (c = 0; c < 256; c++)
3062 {
014fbcda 3063 if (ISDIGIT (c) || ISLOWER (c))
252b5132
RH
3064 {
3065 mnemonic_chars[c] = c;
3066 register_chars[c] = c;
3067 operand_chars[c] = c;
3068 }
3882b010 3069 else if (ISUPPER (c))
252b5132 3070 {
3882b010 3071 mnemonic_chars[c] = TOLOWER (c);
252b5132
RH
3072 register_chars[c] = mnemonic_chars[c];
3073 operand_chars[c] = c;
3074 }
43234a1e 3075 else if (c == '{' || c == '}')
86fa6981
L
3076 {
3077 mnemonic_chars[c] = c;
3078 operand_chars[c] = c;
3079 }
b3983e5f
JB
3080#ifdef SVR4_COMMENT_CHARS
3081 else if (c == '\\' && strchr (i386_comment_chars, '/'))
3082 operand_chars[c] = c;
3083#endif
252b5132 3084
3882b010 3085 if (ISALPHA (c) || ISDIGIT (c))
252b5132
RH
3086 identifier_chars[c] = c;
3087 else if (c >= 128)
3088 {
3089 identifier_chars[c] = c;
3090 operand_chars[c] = c;
3091 }
3092 }
3093
3094#ifdef LEX_AT
3095 identifier_chars['@'] = '@';
32137342
NC
3096#endif
3097#ifdef LEX_QM
3098 identifier_chars['?'] = '?';
3099 operand_chars['?'] = '?';
252b5132 3100#endif
c0f3af97 3101 mnemonic_chars['_'] = '_';
791fe849 3102 mnemonic_chars['-'] = '-';
0003779b 3103 mnemonic_chars['.'] = '.';
252b5132
RH
3104 identifier_chars['_'] = '_';
3105 identifier_chars['.'] = '.';
3106
3107 for (p = operand_special_chars; *p != '\0'; p++)
3108 operand_chars[(unsigned char) *p] = *p;
3109 }
3110
a4447b93
RH
3111 if (flag_code == CODE_64BIT)
3112 {
ca19b261
KT
3113#if defined (OBJ_COFF) && defined (TE_PE)
3114 x86_dwarf2_return_column = (OUTPUT_FLAVOR == bfd_target_coff_flavour
3115 ? 32 : 16);
3116#else
a4447b93 3117 x86_dwarf2_return_column = 16;
ca19b261 3118#endif
61ff971f 3119 x86_cie_data_alignment = -8;
b52c4ee4
IB
3120#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3121 x86_sframe_cfa_sp_reg = 7;
3122 x86_sframe_cfa_fp_reg = 6;
3123#endif
a4447b93
RH
3124 }
3125 else
3126 {
3127 x86_dwarf2_return_column = 8;
3128 x86_cie_data_alignment = -4;
3129 }
e379e5f3
L
3130
3131 /* NB: FUSED_JCC_PADDING frag must have sufficient room so that it
3132 can be turned into BRANCH_PREFIX frag. */
3133 if (align_branch_prefix_size > MAX_FUSED_JCC_PADDING_SIZE)
3134 abort ();
252b5132
RH
3135}
3136
3137void
e3bb37b5 3138i386_print_statistics (FILE *file)
252b5132 3139{
629310ab
ML
3140 htab_print_statistics (file, "i386 opcode", op_hash);
3141 htab_print_statistics (file, "i386 register", reg_hash);
252b5132 3142}
654d6f31
AM
3143
3144void
3145i386_md_end (void)
3146{
3147 htab_delete (op_hash);
3148 htab_delete (reg_hash);
3149}
252b5132 3150\f
252b5132
RH
3151#ifdef DEBUG386
3152
ce8a8b2f 3153/* Debugging routines for md_assemble. */
d3ce72d0 3154static void pte (insn_template *);
40fb9820 3155static void pt (i386_operand_type);
e3bb37b5
L
3156static void pe (expressionS *);
3157static void ps (symbolS *);
252b5132
RH
3158
3159static void
2c703856 3160pi (const char *line, i386_insn *x)
252b5132 3161{
09137c09 3162 unsigned int j;
252b5132
RH
3163
3164 fprintf (stdout, "%s: template ", line);
3165 pte (&x->tm);
09f131f2
JH
3166 fprintf (stdout, " address: base %s index %s scale %x\n",
3167 x->base_reg ? x->base_reg->reg_name : "none",
3168 x->index_reg ? x->index_reg->reg_name : "none",
3169 x->log2_scale_factor);
3170 fprintf (stdout, " modrm: mode %x reg %x reg/mem %x\n",
252b5132 3171 x->rm.mode, x->rm.reg, x->rm.regmem);
09f131f2
JH
3172 fprintf (stdout, " sib: base %x index %x scale %x\n",
3173 x->sib.base, x->sib.index, x->sib.scale);
3174 fprintf (stdout, " rex: 64bit %x extX %x extY %x extZ %x\n",
161a04f6
L
3175 (x->rex & REX_W) != 0,
3176 (x->rex & REX_R) != 0,
3177 (x->rex & REX_X) != 0,
3178 (x->rex & REX_B) != 0);
09137c09 3179 for (j = 0; j < x->operands; j++)
252b5132 3180 {
09137c09
SP
3181 fprintf (stdout, " #%d: ", j + 1);
3182 pt (x->types[j]);
252b5132 3183 fprintf (stdout, "\n");
bab6aec1 3184 if (x->types[j].bitfield.class == Reg
3528c362
JB
3185 || x->types[j].bitfield.class == RegMMX
3186 || x->types[j].bitfield.class == RegSIMD
dd6b8a0b 3187 || x->types[j].bitfield.class == RegMask
00cee14f 3188 || x->types[j].bitfield.class == SReg
4a5c67ed
JB
3189 || x->types[j].bitfield.class == RegCR
3190 || x->types[j].bitfield.class == RegDR
dd6b8a0b
JB
3191 || x->types[j].bitfield.class == RegTR
3192 || x->types[j].bitfield.class == RegBND)
09137c09
SP
3193 fprintf (stdout, "%s\n", x->op[j].regs->reg_name);
3194 if (operand_type_check (x->types[j], imm))
3195 pe (x->op[j].imms);
3196 if (operand_type_check (x->types[j], disp))
3197 pe (x->op[j].disps);
252b5132
RH
3198 }
3199}
3200
3201static void
d3ce72d0 3202pte (insn_template *t)
252b5132 3203{
b933fa4b 3204 static const unsigned char opc_pfx[] = { 0, 0x66, 0xf3, 0xf2 };
441f6aca 3205 static const char *const opc_spc[] = {
0cc78721 3206 NULL, "0f", "0f38", "0f3a", NULL, "evexmap5", "evexmap6", NULL,
441f6aca
JB
3207 "XOP08", "XOP09", "XOP0A",
3208 };
09137c09 3209 unsigned int j;
441f6aca 3210
252b5132 3211 fprintf (stdout, " %d operands ", t->operands);
441f6aca
JB
3212 if (opc_pfx[t->opcode_modifier.opcodeprefix])
3213 fprintf (stdout, "pfx %x ", opc_pfx[t->opcode_modifier.opcodeprefix]);
3214 if (opc_spc[t->opcode_modifier.opcodespace])
3215 fprintf (stdout, "space %s ", opc_spc[t->opcode_modifier.opcodespace]);
47926f60 3216 fprintf (stdout, "opcode %x ", t->base_opcode);
252b5132
RH
3217 if (t->extension_opcode != None)
3218 fprintf (stdout, "ext %x ", t->extension_opcode);
40fb9820 3219 if (t->opcode_modifier.d)
252b5132 3220 fprintf (stdout, "D");
40fb9820 3221 if (t->opcode_modifier.w)
252b5132
RH
3222 fprintf (stdout, "W");
3223 fprintf (stdout, "\n");
09137c09 3224 for (j = 0; j < t->operands; j++)
252b5132 3225 {
09137c09
SP
3226 fprintf (stdout, " #%d type ", j + 1);
3227 pt (t->operand_types[j]);
252b5132
RH
3228 fprintf (stdout, "\n");
3229 }
3230}
3231
3232static void
e3bb37b5 3233pe (expressionS *e)
252b5132 3234{
24eab124 3235 fprintf (stdout, " operation %d\n", e->X_op);
b8281767
AM
3236 fprintf (stdout, " add_number %" PRId64 " (%" PRIx64 ")\n",
3237 (int64_t) e->X_add_number, (uint64_t) (valueT) e->X_add_number);
252b5132
RH
3238 if (e->X_add_symbol)
3239 {
3240 fprintf (stdout, " add_symbol ");
3241 ps (e->X_add_symbol);
3242 fprintf (stdout, "\n");
3243 }
3244 if (e->X_op_symbol)
3245 {
3246 fprintf (stdout, " op_symbol ");
3247 ps (e->X_op_symbol);
3248 fprintf (stdout, "\n");
3249 }
3250}
3251
3252static void
e3bb37b5 3253ps (symbolS *s)
252b5132
RH
3254{
3255 fprintf (stdout, "%s type %s%s",
3256 S_GET_NAME (s),
3257 S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
3258 segment_name (S_GET_SEGMENT (s)));
3259}
3260
7b81dfbb 3261static struct type_name
252b5132 3262 {
40fb9820
L
3263 i386_operand_type mask;
3264 const char *name;
252b5132 3265 }
7b81dfbb 3266const type_names[] =
252b5132 3267{
40fb9820
L
3268 { OPERAND_TYPE_REG8, "r8" },
3269 { OPERAND_TYPE_REG16, "r16" },
3270 { OPERAND_TYPE_REG32, "r32" },
3271 { OPERAND_TYPE_REG64, "r64" },
2c703856
JB
3272 { OPERAND_TYPE_ACC8, "acc8" },
3273 { OPERAND_TYPE_ACC16, "acc16" },
3274 { OPERAND_TYPE_ACC32, "acc32" },
3275 { OPERAND_TYPE_ACC64, "acc64" },
40fb9820
L
3276 { OPERAND_TYPE_IMM8, "i8" },
3277 { OPERAND_TYPE_IMM8, "i8s" },
3278 { OPERAND_TYPE_IMM16, "i16" },
3279 { OPERAND_TYPE_IMM32, "i32" },
3280 { OPERAND_TYPE_IMM32S, "i32s" },
3281 { OPERAND_TYPE_IMM64, "i64" },
3282 { OPERAND_TYPE_IMM1, "i1" },
3283 { OPERAND_TYPE_BASEINDEX, "BaseIndex" },
3284 { OPERAND_TYPE_DISP8, "d8" },
3285 { OPERAND_TYPE_DISP16, "d16" },
3286 { OPERAND_TYPE_DISP32, "d32" },
40fb9820
L
3287 { OPERAND_TYPE_DISP64, "d64" },
3288 { OPERAND_TYPE_INOUTPORTREG, "InOutPortReg" },
3289 { OPERAND_TYPE_SHIFTCOUNT, "ShiftCount" },
3290 { OPERAND_TYPE_CONTROL, "control reg" },
3291 { OPERAND_TYPE_TEST, "test reg" },
3292 { OPERAND_TYPE_DEBUG, "debug reg" },
3293 { OPERAND_TYPE_FLOATREG, "FReg" },
3294 { OPERAND_TYPE_FLOATACC, "FAcc" },
21df382b 3295 { OPERAND_TYPE_SREG, "SReg" },
40fb9820
L
3296 { OPERAND_TYPE_REGMMX, "rMMX" },
3297 { OPERAND_TYPE_REGXMM, "rXMM" },
0349dc08 3298 { OPERAND_TYPE_REGYMM, "rYMM" },
43234a1e 3299 { OPERAND_TYPE_REGZMM, "rZMM" },
260cd341 3300 { OPERAND_TYPE_REGTMM, "rTMM" },
43234a1e 3301 { OPERAND_TYPE_REGMASK, "Mask reg" },
252b5132
RH
3302};
3303
3304static void
40fb9820 3305pt (i386_operand_type t)
252b5132 3306{
40fb9820 3307 unsigned int j;
c6fb90c8 3308 i386_operand_type a;
252b5132 3309
40fb9820 3310 for (j = 0; j < ARRAY_SIZE (type_names); j++)
c6fb90c8
L
3311 {
3312 a = operand_type_and (t, type_names[j].mask);
2c703856 3313 if (operand_type_equal (&a, &type_names[j].mask))
c6fb90c8
L
3314 fprintf (stdout, "%s, ", type_names[j].name);
3315 }
252b5132
RH
3316 fflush (stdout);
3317}
3318
3319#endif /* DEBUG386 */
3320\f
252b5132 3321static bfd_reloc_code_real_type
3956db08 3322reloc (unsigned int size,
64e74474
AM
3323 int pcrel,
3324 int sign,
3325 bfd_reloc_code_real_type other)
252b5132 3326{
47926f60 3327 if (other != NO_RELOC)
3956db08 3328 {
91d6fa6a 3329 reloc_howto_type *rel;
3956db08
JB
3330
3331 if (size == 8)
3332 switch (other)
3333 {
64e74474
AM
3334 case BFD_RELOC_X86_64_GOT32:
3335 return BFD_RELOC_X86_64_GOT64;
3336 break;
553d1284
L
3337 case BFD_RELOC_X86_64_GOTPLT64:
3338 return BFD_RELOC_X86_64_GOTPLT64;
3339 break;
64e74474
AM
3340 case BFD_RELOC_X86_64_PLTOFF64:
3341 return BFD_RELOC_X86_64_PLTOFF64;
3342 break;
3343 case BFD_RELOC_X86_64_GOTPC32:
3344 other = BFD_RELOC_X86_64_GOTPC64;
3345 break;
3346 case BFD_RELOC_X86_64_GOTPCREL:
3347 other = BFD_RELOC_X86_64_GOTPCREL64;
3348 break;
3349 case BFD_RELOC_X86_64_TPOFF32:
3350 other = BFD_RELOC_X86_64_TPOFF64;
3351 break;
3352 case BFD_RELOC_X86_64_DTPOFF32:
3353 other = BFD_RELOC_X86_64_DTPOFF64;
3354 break;
3355 default:
3356 break;
3956db08 3357 }
e05278af 3358
8ce3d284 3359#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
3360 if (other == BFD_RELOC_SIZE32)
3361 {
3362 if (size == 8)
1ab668bf 3363 other = BFD_RELOC_SIZE64;
8fd4256d 3364 if (pcrel)
1ab668bf
AM
3365 {
3366 as_bad (_("there are no pc-relative size relocations"));
3367 return NO_RELOC;
3368 }
8fd4256d 3369 }
8ce3d284 3370#endif
8fd4256d 3371
e05278af 3372 /* Sign-checking 4-byte relocations in 16-/32-bit code is pointless. */
f2d8a97c 3373 if (size == 4 && (flag_code != CODE_64BIT || disallow_64bit_reloc))
e05278af
JB
3374 sign = -1;
3375
91d6fa6a
NC
3376 rel = bfd_reloc_type_lookup (stdoutput, other);
3377 if (!rel)
3956db08 3378 as_bad (_("unknown relocation (%u)"), other);
91d6fa6a 3379 else if (size != bfd_get_reloc_size (rel))
3956db08 3380 as_bad (_("%u-byte relocation cannot be applied to %u-byte field"),
91d6fa6a 3381 bfd_get_reloc_size (rel),
3956db08 3382 size);
91d6fa6a 3383 else if (pcrel && !rel->pc_relative)
3956db08 3384 as_bad (_("non-pc-relative relocation for pc-relative field"));
91d6fa6a 3385 else if ((rel->complain_on_overflow == complain_overflow_signed
3956db08 3386 && !sign)
91d6fa6a 3387 || (rel->complain_on_overflow == complain_overflow_unsigned
64e74474 3388 && sign > 0))
3956db08
JB
3389 as_bad (_("relocated field and relocation type differ in signedness"));
3390 else
3391 return other;
3392 return NO_RELOC;
3393 }
252b5132
RH
3394
3395 if (pcrel)
3396 {
3e73aa7c 3397 if (!sign)
3956db08 3398 as_bad (_("there are no unsigned pc-relative relocations"));
252b5132
RH
3399 switch (size)
3400 {
3401 case 1: return BFD_RELOC_8_PCREL;
3402 case 2: return BFD_RELOC_16_PCREL;
d258b828 3403 case 4: return BFD_RELOC_32_PCREL;
d6ab8113 3404 case 8: return BFD_RELOC_64_PCREL;
252b5132 3405 }
3956db08 3406 as_bad (_("cannot do %u byte pc-relative relocation"), size);
252b5132
RH
3407 }
3408 else
3409 {
3956db08 3410 if (sign > 0)
e5cb08ac 3411 switch (size)
3e73aa7c
JH
3412 {
3413 case 4: return BFD_RELOC_X86_64_32S;
3414 }
3415 else
3416 switch (size)
3417 {
3418 case 1: return BFD_RELOC_8;
3419 case 2: return BFD_RELOC_16;
3420 case 4: return BFD_RELOC_32;
3421 case 8: return BFD_RELOC_64;
3422 }
3956db08
JB
3423 as_bad (_("cannot do %s %u byte relocation"),
3424 sign > 0 ? "signed" : "unsigned", size);
252b5132
RH
3425 }
3426
0cc9e1d3 3427 return NO_RELOC;
252b5132
RH
3428}
3429
47926f60
KH
3430/* Here we decide which fixups can be adjusted to make them relative to
3431 the beginning of the section instead of the symbol. Basically we need
3432 to make sure that the dynamic relocations are done correctly, so in
3433 some cases we force the original symbol to be used. */
3434
252b5132 3435int
e3bb37b5 3436tc_i386_fix_adjustable (fixS *fixP ATTRIBUTE_UNUSED)
252b5132 3437{
6d249963 3438#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 3439 if (!IS_ELF)
31312f95
AM
3440 return 1;
3441
a161fe53
AM
3442 /* Don't adjust pc-relative references to merge sections in 64-bit
3443 mode. */
3444 if (use_rela_relocations
3445 && (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0
3446 && fixP->fx_pcrel)
252b5132 3447 return 0;
31312f95 3448
8d01d9a9
AJ
3449 /* The x86_64 GOTPCREL are represented as 32bit PCrel relocations
3450 and changed later by validate_fix. */
3451 if (GOT_symbol && fixP->fx_subsy == GOT_symbol
3452 && fixP->fx_r_type == BFD_RELOC_32_PCREL)
3453 return 0;
3454
8fd4256d
L
3455 /* Adjust_reloc_syms doesn't know about the GOT. Need to keep symbol
3456 for size relocations. */
3457 if (fixP->fx_r_type == BFD_RELOC_SIZE32
3458 || fixP->fx_r_type == BFD_RELOC_SIZE64
3459 || fixP->fx_r_type == BFD_RELOC_386_GOTOFF
252b5132 3460 || fixP->fx_r_type == BFD_RELOC_386_GOT32
02a86693 3461 || fixP->fx_r_type == BFD_RELOC_386_GOT32X
13ae64f3
JJ
3462 || fixP->fx_r_type == BFD_RELOC_386_TLS_GD
3463 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDM
3464 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDO_32
3465 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE_32
37e55690
JJ
3466 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE
3467 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTIE
13ae64f3
JJ
3468 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE_32
3469 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE
67a4f2b7
AO
3470 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTDESC
3471 || fixP->fx_r_type == BFD_RELOC_386_TLS_DESC_CALL
3e73aa7c 3472 || fixP->fx_r_type == BFD_RELOC_X86_64_GOT32
80b3ee89 3473 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCREL
56ceb5b5
L
3474 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCRELX
3475 || fixP->fx_r_type == BFD_RELOC_X86_64_REX_GOTPCRELX
bffbf940
JJ
3476 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSGD
3477 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSLD
3478 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF32
d6ab8113 3479 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF64
bffbf940
JJ
3480 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF
3481 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF32
d6ab8113
JB
3482 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF64
3483 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTOFF64
67a4f2b7
AO
3484 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
3485 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSDESC_CALL
252b5132
RH
3486 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3487 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3488 return 0;
31312f95 3489#endif
252b5132
RH
3490 return 1;
3491}
252b5132 3492
a9aabc23
JB
3493static INLINE bool
3494want_disp32 (const insn_template *t)
3495{
3496 return flag_code != CODE_64BIT
3497 || i.prefix[ADDR_PREFIX]
3498 || (t->base_opcode == 0x8d
3499 && t->opcode_modifier.opcodespace == SPACE_BASE
fe134c65
JB
3500 && (!i.types[1].bitfield.qword
3501 || t->opcode_modifier.size == SIZE32));
a9aabc23
JB
3502}
3503
b4cac588 3504static int
e3bb37b5 3505intel_float_operand (const char *mnemonic)
252b5132 3506{
9306ca4a
JB
3507 /* Note that the value returned is meaningful only for opcodes with (memory)
3508 operands, hence the code here is free to improperly handle opcodes that
3509 have no operands (for better performance and smaller code). */
3510
3511 if (mnemonic[0] != 'f')
3512 return 0; /* non-math */
3513
3514 switch (mnemonic[1])
3515 {
3516 /* fclex, fdecstp, fdisi, femms, feni, fincstp, finit, fsetpm, and
3517 the fs segment override prefix not currently handled because no
3518 call path can make opcodes without operands get here */
3519 case 'i':
3520 return 2 /* integer op */;
3521 case 'l':
3522 if (mnemonic[2] == 'd' && (mnemonic[3] == 'c' || mnemonic[3] == 'e'))
3523 return 3; /* fldcw/fldenv */
3524 break;
3525 case 'n':
3526 if (mnemonic[2] != 'o' /* fnop */)
3527 return 3; /* non-waiting control op */
3528 break;
3529 case 'r':
3530 if (mnemonic[2] == 's')
3531 return 3; /* frstor/frstpm */
3532 break;
3533 case 's':
3534 if (mnemonic[2] == 'a')
3535 return 3; /* fsave */
3536 if (mnemonic[2] == 't')
3537 {
3538 switch (mnemonic[3])
3539 {
3540 case 'c': /* fstcw */
3541 case 'd': /* fstdw */
3542 case 'e': /* fstenv */
3543 case 's': /* fsts[gw] */
3544 return 3;
3545 }
3546 }
3547 break;
3548 case 'x':
3549 if (mnemonic[2] == 'r' || mnemonic[2] == 's')
3550 return 0; /* fxsave/fxrstor are not really math ops */
3551 break;
3552 }
252b5132 3553
9306ca4a 3554 return 1;
252b5132
RH
3555}
3556
9a182d04
JB
3557static INLINE void
3558install_template (const insn_template *t)
3559{
3560 unsigned int l;
3561
3562 i.tm = *t;
3563
3564 /* Note that for pseudo prefixes this produces a length of 1. But for them
3565 the length isn't interesting at all. */
3566 for (l = 1; l < 4; ++l)
3567 if (!(t->base_opcode >> (8 * l)))
3568 break;
3569
3570 i.opcode_length = l;
3571}
3572
c0f3af97
L
3573/* Build the VEX prefix. */
3574
3575static void
d3ce72d0 3576build_vex_prefix (const insn_template *t)
c0f3af97
L
3577{
3578 unsigned int register_specifier;
c0f3af97 3579 unsigned int vector_length;
03751133 3580 unsigned int w;
c0f3af97
L
3581
3582 /* Check register specifier. */
3583 if (i.vex.register_specifier)
43234a1e
L
3584 {
3585 register_specifier =
3586 ~register_number (i.vex.register_specifier) & 0xf;
3587 gas_assert ((i.vex.register_specifier->reg_flags & RegVRex) == 0);
3588 }
c0f3af97
L
3589 else
3590 register_specifier = 0xf;
3591
79f0fa25
L
3592 /* Use 2-byte VEX prefix by swapping destination and source operand
3593 if there are more than 1 register operand. */
3594 if (i.reg_operands > 1
3595 && i.vec_encoding != vex_encoding_vex3
86fa6981 3596 && i.dir_encoding == dir_encoding_default
fa99fab2 3597 && i.operands == i.reg_operands
dbbc8b7e 3598 && operand_type_equal (&i.types[0], &i.types[i.operands - 1])
441f6aca 3599 && i.tm.opcode_modifier.opcodespace == SPACE_0F
dbbc8b7e 3600 && (i.tm.opcode_modifier.load || i.tm.opcode_modifier.d)
fa99fab2
L
3601 && i.rex == REX_B)
3602 {
3603 unsigned int xchg = i.operands - 1;
3604 union i386_op temp_op;
3605 i386_operand_type temp_type;
3606
3607 temp_type = i.types[xchg];
3608 i.types[xchg] = i.types[0];
3609 i.types[0] = temp_type;
3610 temp_op = i.op[xchg];
3611 i.op[xchg] = i.op[0];
3612 i.op[0] = temp_op;
3613
9c2799c2 3614 gas_assert (i.rm.mode == 3);
fa99fab2
L
3615
3616 i.rex = REX_R;
3617 xchg = i.rm.regmem;
3618 i.rm.regmem = i.rm.reg;
3619 i.rm.reg = xchg;
3620
dbbc8b7e
JB
3621 if (i.tm.opcode_modifier.d)
3622 i.tm.base_opcode ^= (i.tm.base_opcode & 0xee) != 0x6e
2c735193 3623 ? Opcode_ExtD : Opcode_SIMD_IntD;
dbbc8b7e 3624 else /* Use the next insn. */
9a182d04 3625 install_template (&t[1]);
fa99fab2
L
3626 }
3627
79dec6b7
JB
3628 /* Use 2-byte VEX prefix by swapping commutative source operands if there
3629 are no memory operands and at least 3 register ones. */
3630 if (i.reg_operands >= 3
3631 && i.vec_encoding != vex_encoding_vex3
3632 && i.reg_operands == i.operands - i.imm_operands
3633 && i.tm.opcode_modifier.vex
3634 && i.tm.opcode_modifier.commutative
3635 && (i.tm.opcode_modifier.sse2avx || optimize > 1)
3636 && i.rex == REX_B
3637 && i.vex.register_specifier
3638 && !(i.vex.register_specifier->reg_flags & RegRex))
3639 {
3640 unsigned int xchg = i.operands - i.reg_operands;
3641 union i386_op temp_op;
3642 i386_operand_type temp_type;
3643
441f6aca 3644 gas_assert (i.tm.opcode_modifier.opcodespace == SPACE_0F);
79dec6b7
JB
3645 gas_assert (!i.tm.opcode_modifier.sae);
3646 gas_assert (operand_type_equal (&i.types[i.operands - 2],
3647 &i.types[i.operands - 3]));
3648 gas_assert (i.rm.mode == 3);
3649
3650 temp_type = i.types[xchg];
3651 i.types[xchg] = i.types[xchg + 1];
3652 i.types[xchg + 1] = temp_type;
3653 temp_op = i.op[xchg];
3654 i.op[xchg] = i.op[xchg + 1];
3655 i.op[xchg + 1] = temp_op;
3656
3657 i.rex = 0;
3658 xchg = i.rm.regmem | 8;
3659 i.rm.regmem = ~register_specifier & 0xf;
3660 gas_assert (!(i.rm.regmem & 8));
3661 i.vex.register_specifier += xchg - i.rm.regmem;
3662 register_specifier = ~xchg & 0xf;
3663 }
3664
539f890d
L
3665 if (i.tm.opcode_modifier.vex == VEXScalar)
3666 vector_length = avxscalar;
10c17abd
JB
3667 else if (i.tm.opcode_modifier.vex == VEX256)
3668 vector_length = 1;
539f890d 3669 else
10c17abd 3670 {
56522fc5 3671 unsigned int op;
10c17abd 3672
c7213af9
L
3673 /* Determine vector length from the last multi-length vector
3674 operand. */
10c17abd 3675 vector_length = 0;
56522fc5 3676 for (op = t->operands; op--;)
10c17abd
JB
3677 if (t->operand_types[op].bitfield.xmmword
3678 && t->operand_types[op].bitfield.ymmword
3679 && i.types[op].bitfield.ymmword)
3680 {
3681 vector_length = 1;
3682 break;
3683 }
3684 }
c0f3af97 3685
03751133
L
3686 /* Check the REX.W bit and VEXW. */
3687 if (i.tm.opcode_modifier.vexw == VEXWIG)
3688 w = (vexwig == vexw1 || (i.rex & REX_W)) ? 1 : 0;
3689 else if (i.tm.opcode_modifier.vexw)
3690 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3691 else
931d03b7 3692 w = (flag_code == CODE_64BIT ? i.rex & REX_W : vexwig == vexw1) ? 1 : 0;
03751133 3693
c0f3af97 3694 /* Use 2-byte VEX prefix if possible. */
03751133
L
3695 if (w == 0
3696 && i.vec_encoding != vex_encoding_vex3
441f6aca 3697 && i.tm.opcode_modifier.opcodespace == SPACE_0F
c0f3af97
L
3698 && (i.rex & (REX_W | REX_X | REX_B)) == 0)
3699 {
3700 /* 2-byte VEX prefix. */
3701 unsigned int r;
3702
3703 i.vex.length = 2;
3704 i.vex.bytes[0] = 0xc5;
3705
3706 /* Check the REX.R bit. */
3707 r = (i.rex & REX_R) ? 0 : 1;
3708 i.vex.bytes[1] = (r << 7
3709 | register_specifier << 3
3710 | vector_length << 2
35648716 3711 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3712 }
3713 else
3714 {
3715 /* 3-byte VEX prefix. */
f88c9eb0 3716 i.vex.length = 3;
f88c9eb0 3717
441f6aca 3718 switch (i.tm.opcode_modifier.opcodespace)
5dd85c99 3719 {
441f6aca
JB
3720 case SPACE_0F:
3721 case SPACE_0F38:
3722 case SPACE_0F3A:
80de6e00 3723 i.vex.bytes[0] = 0xc4;
7f399153 3724 break;
441f6aca
JB
3725 case SPACE_XOP08:
3726 case SPACE_XOP09:
3727 case SPACE_XOP0A:
f88c9eb0 3728 i.vex.bytes[0] = 0x8f;
7f399153
L
3729 break;
3730 default:
3731 abort ();
f88c9eb0 3732 }
c0f3af97 3733
c0f3af97
L
3734 /* The high 3 bits of the second VEX byte are 1's compliment
3735 of RXB bits from REX. */
441f6aca 3736 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | i.tm.opcode_modifier.opcodespace;
c0f3af97 3737
c0f3af97
L
3738 i.vex.bytes[2] = (w << 7
3739 | register_specifier << 3
3740 | vector_length << 2
35648716 3741 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3742 }
3743}
3744
5b7c81bd 3745static INLINE bool
e771e7c9
JB
3746is_evex_encoding (const insn_template *t)
3747{
7091c612 3748 return t->opcode_modifier.evex || t->opcode_modifier.disp8memshift
e771e7c9 3749 || t->opcode_modifier.broadcast || t->opcode_modifier.masking
a80195f1 3750 || t->opcode_modifier.sae;
e771e7c9
JB
3751}
3752
5b7c81bd 3753static INLINE bool
7a8655d2
JB
3754is_any_vex_encoding (const insn_template *t)
3755{
7b47a312 3756 return t->opcode_modifier.vex || is_evex_encoding (t);
7a8655d2
JB
3757}
3758
a5748e0d
JB
3759static unsigned int
3760get_broadcast_bytes (const insn_template *t, bool diag)
3761{
3762 unsigned int op, bytes;
3763 const i386_operand_type *types;
3764
3765 if (i.broadcast.type)
3766 return i.broadcast.bytes = ((1 << (t->opcode_modifier.broadcast - 1))
3767 * i.broadcast.type);
3768
3769 gas_assert (intel_syntax);
3770
3771 for (op = 0; op < t->operands; ++op)
3772 if (t->operand_types[op].bitfield.baseindex)
3773 break;
3774
3775 gas_assert (op < t->operands);
3776
3777 if (t->opcode_modifier.evex
3778 && t->opcode_modifier.evex != EVEXDYN)
3779 switch (i.broadcast.bytes)
3780 {
3781 case 1:
3782 if (t->operand_types[op].bitfield.word)
3783 return 2;
3784 /* Fall through. */
3785 case 2:
3786 if (t->operand_types[op].bitfield.dword)
3787 return 4;
3788 /* Fall through. */
3789 case 4:
3790 if (t->operand_types[op].bitfield.qword)
3791 return 8;
3792 /* Fall through. */
3793 case 8:
3794 if (t->operand_types[op].bitfield.xmmword)
3795 return 16;
3796 if (t->operand_types[op].bitfield.ymmword)
3797 return 32;
3798 if (t->operand_types[op].bitfield.zmmword)
3799 return 64;
3800 /* Fall through. */
3801 default:
3802 abort ();
3803 }
3804
3805 gas_assert (op + 1 < t->operands);
3806
3807 if (t->operand_types[op + 1].bitfield.xmmword
3808 + t->operand_types[op + 1].bitfield.ymmword
3809 + t->operand_types[op + 1].bitfield.zmmword > 1)
3810 {
3811 types = &i.types[op + 1];
3812 diag = false;
3813 }
3814 else /* Ambiguous - guess with a preference to non-AVX512VL forms. */
3815 types = &t->operand_types[op];
3816
3817 if (types->bitfield.zmmword)
3818 bytes = 64;
3819 else if (types->bitfield.ymmword)
3820 bytes = 32;
3821 else
3822 bytes = 16;
3823
3824 if (diag)
3825 as_warn (_("ambiguous broadcast for `%s', using %u-bit form"),
3826 t->name, bytes * 8);
3827
3828 return bytes;
3829}
3830
43234a1e
L
3831/* Build the EVEX prefix. */
3832
3833static void
3834build_evex_prefix (void)
3835{
35648716 3836 unsigned int register_specifier, w;
43234a1e
L
3837 rex_byte vrex_used = 0;
3838
3839 /* Check register specifier. */
3840 if (i.vex.register_specifier)
3841 {
3842 gas_assert ((i.vrex & REX_X) == 0);
3843
3844 register_specifier = i.vex.register_specifier->reg_num;
3845 if ((i.vex.register_specifier->reg_flags & RegRex))
3846 register_specifier += 8;
3847 /* The upper 16 registers are encoded in the fourth byte of the
3848 EVEX prefix. */
3849 if (!(i.vex.register_specifier->reg_flags & RegVRex))
3850 i.vex.bytes[3] = 0x8;
3851 register_specifier = ~register_specifier & 0xf;
3852 }
3853 else
3854 {
3855 register_specifier = 0xf;
3856
3857 /* Encode upper 16 vector index register in the fourth byte of
3858 the EVEX prefix. */
3859 if (!(i.vrex & REX_X))
3860 i.vex.bytes[3] = 0x8;
3861 else
3862 vrex_used |= REX_X;
3863 }
3864
43234a1e
L
3865 /* 4 byte EVEX prefix. */
3866 i.vex.length = 4;
3867 i.vex.bytes[0] = 0x62;
3868
43234a1e
L
3869 /* The high 3 bits of the second EVEX byte are 1's compliment of RXB
3870 bits from REX. */
441f6aca 3871 gas_assert (i.tm.opcode_modifier.opcodespace >= SPACE_0F);
0cc78721 3872 gas_assert (i.tm.opcode_modifier.opcodespace <= SPACE_EVEXMAP6);
441f6aca 3873 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | i.tm.opcode_modifier.opcodespace;
43234a1e
L
3874
3875 /* The fifth bit of the second EVEX byte is 1's compliment of the
3876 REX_R bit in VREX. */
3877 if (!(i.vrex & REX_R))
3878 i.vex.bytes[1] |= 0x10;
3879 else
3880 vrex_used |= REX_R;
3881
3882 if ((i.reg_operands + i.imm_operands) == i.operands)
3883 {
3884 /* When all operands are registers, the REX_X bit in REX is not
3885 used. We reuse it to encode the upper 16 registers, which is
3886 indicated by the REX_B bit in VREX. The REX_X bit is encoded
3887 as 1's compliment. */
3888 if ((i.vrex & REX_B))
3889 {
3890 vrex_used |= REX_B;
3891 i.vex.bytes[1] &= ~0x40;
3892 }
3893 }
3894
3895 /* EVEX instructions shouldn't need the REX prefix. */
3896 i.vrex &= ~vrex_used;
3897 gas_assert (i.vrex == 0);
3898
6865c043
L
3899 /* Check the REX.W bit and VEXW. */
3900 if (i.tm.opcode_modifier.vexw == VEXWIG)
3901 w = (evexwig == evexw1 || (i.rex & REX_W)) ? 1 : 0;
3902 else if (i.tm.opcode_modifier.vexw)
3903 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3904 else
931d03b7 3905 w = (flag_code == CODE_64BIT ? i.rex & REX_W : evexwig == evexw1) ? 1 : 0;
43234a1e 3906
43234a1e 3907 /* The third byte of the EVEX prefix. */
35648716
JB
3908 i.vex.bytes[2] = ((w << 7)
3909 | (register_specifier << 3)
3910 | 4 /* Encode the U bit. */
3911 | i.tm.opcode_modifier.opcodeprefix);
43234a1e
L
3912
3913 /* The fourth byte of the EVEX prefix. */
3914 /* The zeroing-masking bit. */
6225c532 3915 if (i.mask.reg && i.mask.zeroing)
43234a1e
L
3916 i.vex.bytes[3] |= 0x80;
3917
3918 /* Don't always set the broadcast bit if there is no RC. */
ca5312a2 3919 if (i.rounding.type == rc_none)
43234a1e
L
3920 {
3921 /* Encode the vector length. */
3922 unsigned int vec_length;
3923
e771e7c9
JB
3924 if (!i.tm.opcode_modifier.evex
3925 || i.tm.opcode_modifier.evex == EVEXDYN)
3926 {
56522fc5 3927 unsigned int op;
e771e7c9 3928
c7213af9
L
3929 /* Determine vector length from the last multi-length vector
3930 operand. */
56522fc5 3931 for (op = i.operands; op--;)
e771e7c9
JB
3932 if (i.tm.operand_types[op].bitfield.xmmword
3933 + i.tm.operand_types[op].bitfield.ymmword
3934 + i.tm.operand_types[op].bitfield.zmmword > 1)
3935 {
3936 if (i.types[op].bitfield.zmmword)
c7213af9
L
3937 {
3938 i.tm.opcode_modifier.evex = EVEX512;
3939 break;
3940 }
e771e7c9 3941 else if (i.types[op].bitfield.ymmword)
c7213af9
L
3942 {
3943 i.tm.opcode_modifier.evex = EVEX256;
3944 break;
3945 }
e771e7c9 3946 else if (i.types[op].bitfield.xmmword)
c7213af9
L
3947 {
3948 i.tm.opcode_modifier.evex = EVEX128;
3949 break;
3950 }
a5748e0d 3951 else if (i.broadcast.bytes && op == i.broadcast.operand)
625cbd7a 3952 {
a5748e0d 3953 switch (get_broadcast_bytes (&i.tm, true))
625cbd7a
JB
3954 {
3955 case 64:
3956 i.tm.opcode_modifier.evex = EVEX512;
3957 break;
3958 case 32:
3959 i.tm.opcode_modifier.evex = EVEX256;
3960 break;
3961 case 16:
3962 i.tm.opcode_modifier.evex = EVEX128;
3963 break;
3964 default:
c7213af9 3965 abort ();
625cbd7a 3966 }
c7213af9 3967 break;
625cbd7a 3968 }
e771e7c9 3969 }
c7213af9 3970
56522fc5 3971 if (op >= MAX_OPERANDS)
c7213af9 3972 abort ();
e771e7c9
JB
3973 }
3974
43234a1e
L
3975 switch (i.tm.opcode_modifier.evex)
3976 {
3977 case EVEXLIG: /* LL' is ignored */
3978 vec_length = evexlig << 5;
3979 break;
3980 case EVEX128:
3981 vec_length = 0 << 5;
3982 break;
3983 case EVEX256:
3984 vec_length = 1 << 5;
3985 break;
3986 case EVEX512:
3987 vec_length = 2 << 5;
3988 break;
3989 default:
3990 abort ();
3991 break;
3992 }
3993 i.vex.bytes[3] |= vec_length;
3994 /* Encode the broadcast bit. */
a5748e0d 3995 if (i.broadcast.bytes)
43234a1e
L
3996 i.vex.bytes[3] |= 0x10;
3997 }
ca5312a2
JB
3998 else if (i.rounding.type != saeonly)
3999 i.vex.bytes[3] |= 0x10 | (i.rounding.type << 5);
43234a1e 4000 else
ca5312a2 4001 i.vex.bytes[3] |= 0x10 | (evexrcig << 5);
43234a1e 4002
6225c532
JB
4003 if (i.mask.reg)
4004 i.vex.bytes[3] |= i.mask.reg->reg_num;
43234a1e
L
4005}
4006
65da13b5
L
4007static void
4008process_immext (void)
4009{
4010 expressionS *exp;
4011
c0f3af97 4012 /* These AMD 3DNow! and SSE2 instructions have an opcode suffix
65da13b5
L
4013 which is coded in the same place as an 8-bit immediate field
4014 would be. Here we fake an 8-bit immediate operand from the
4015 opcode suffix stored in tm.extension_opcode.
4016
c1e679ec 4017 AVX instructions also use this encoding, for some of
c0f3af97 4018 3 argument instructions. */
65da13b5 4019
43234a1e 4020 gas_assert (i.imm_operands <= 1
7ab9ffdd 4021 && (i.operands <= 2
7a8655d2 4022 || (is_any_vex_encoding (&i.tm)
7ab9ffdd 4023 && i.operands <= 4)));
65da13b5
L
4024
4025 exp = &im_expressions[i.imm_operands++];
4026 i.op[i.operands].imms = exp;
be1643ff 4027 i.types[i.operands].bitfield.imm8 = 1;
65da13b5
L
4028 i.operands++;
4029 exp->X_op = O_constant;
4030 exp->X_add_number = i.tm.extension_opcode;
4031 i.tm.extension_opcode = None;
4032}
4033
42164a71
L
4034
4035static int
4036check_hle (void)
4037{
742732c7 4038 switch (i.tm.opcode_modifier.prefixok)
42164a71
L
4039 {
4040 default:
4041 abort ();
742732c7
JB
4042 case PrefixLock:
4043 case PrefixNone:
4044 case PrefixNoTrack:
4045 case PrefixRep:
165de32a
L
4046 as_bad (_("invalid instruction `%s' after `%s'"),
4047 i.tm.name, i.hle_prefix);
42164a71 4048 return 0;
742732c7 4049 case PrefixHLELock:
42164a71
L
4050 if (i.prefix[LOCK_PREFIX])
4051 return 1;
165de32a 4052 as_bad (_("missing `lock' with `%s'"), i.hle_prefix);
42164a71 4053 return 0;
742732c7 4054 case PrefixHLEAny:
42164a71 4055 return 1;
742732c7 4056 case PrefixHLERelease:
42164a71
L
4057 if (i.prefix[HLE_PREFIX] != XRELEASE_PREFIX_OPCODE)
4058 {
4059 as_bad (_("instruction `%s' after `xacquire' not allowed"),
4060 i.tm.name);
4061 return 0;
4062 }
8dc0818e 4063 if (i.mem_operands == 0 || !(i.flags[i.operands - 1] & Operand_Mem))
42164a71
L
4064 {
4065 as_bad (_("memory destination needed for instruction `%s'"
4066 " after `xrelease'"), i.tm.name);
4067 return 0;
4068 }
4069 return 1;
4070 }
4071}
4072
c8480b58
L
4073/* Encode aligned vector move as unaligned vector move. */
4074
4075static void
4076encode_with_unaligned_vector_move (void)
4077{
4078 switch (i.tm.base_opcode)
4079 {
b3a9fe6f
L
4080 case 0x28: /* Load instructions. */
4081 case 0x29: /* Store instructions. */
c8480b58
L
4082 /* movaps/movapd/vmovaps/vmovapd. */
4083 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4084 && i.tm.opcode_modifier.opcodeprefix <= PREFIX_0X66)
b3a9fe6f 4085 i.tm.base_opcode = 0x10 | (i.tm.base_opcode & 1);
c8480b58 4086 break;
b3a9fe6f
L
4087 case 0x6f: /* Load instructions. */
4088 case 0x7f: /* Store instructions. */
c8480b58
L
4089 /* movdqa/vmovdqa/vmovdqa64/vmovdqa32. */
4090 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4091 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0X66)
4092 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
4093 break;
4094 default:
4095 break;
4096 }
4097}
4098
b6f8c7c4
L
4099/* Try the shortest encoding by shortening operand size. */
4100
4101static void
4102optimize_encoding (void)
4103{
a0a1771e 4104 unsigned int j;
b6f8c7c4 4105
fe134c65
JB
4106 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
4107 && i.tm.base_opcode == 0x8d)
4108 {
4109 /* Optimize: -O:
4110 lea symbol, %rN -> mov $symbol, %rN
4111 lea (%rM), %rN -> mov %rM, %rN
4112 lea (,%rM,1), %rN -> mov %rM, %rN
4113
4114 and in 32-bit mode for 16-bit addressing
4115
4116 lea (%rM), %rN -> movzx %rM, %rN
4117
4118 and in 64-bit mode zap 32-bit addressing in favor of using a
4119 32-bit (or less) destination.
4120 */
4121 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
4122 {
4123 if (!i.op[1].regs->reg_type.bitfield.word)
4124 i.tm.opcode_modifier.size = SIZE32;
4125 i.prefix[ADDR_PREFIX] = 0;
4126 }
4127
4128 if (!i.index_reg && !i.base_reg)
4129 {
4130 /* Handle:
4131 lea symbol, %rN -> mov $symbol, %rN
4132 */
4133 if (flag_code == CODE_64BIT)
4134 {
4135 /* Don't transform a relocation to a 16-bit one. */
4136 if (i.op[0].disps
4137 && i.op[0].disps->X_op != O_constant
4138 && i.op[1].regs->reg_type.bitfield.word)
4139 return;
4140
4141 if (!i.op[1].regs->reg_type.bitfield.qword
4142 || i.tm.opcode_modifier.size == SIZE32)
4143 {
4144 i.tm.base_opcode = 0xb8;
4145 i.tm.opcode_modifier.modrm = 0;
4146 if (!i.op[1].regs->reg_type.bitfield.word)
4147 i.types[0].bitfield.imm32 = 1;
4148 else
4149 {
4150 i.tm.opcode_modifier.size = SIZE16;
4151 i.types[0].bitfield.imm16 = 1;
4152 }
4153 }
4154 else
4155 {
4156 /* Subject to further optimization below. */
4157 i.tm.base_opcode = 0xc7;
4158 i.tm.extension_opcode = 0;
4159 i.types[0].bitfield.imm32s = 1;
4160 i.types[0].bitfield.baseindex = 0;
4161 }
4162 }
4163 /* Outside of 64-bit mode address and operand sizes have to match if
4164 a relocation is involved, as otherwise we wouldn't (currently) or
4165 even couldn't express the relocation correctly. */
4166 else if (i.op[0].disps
4167 && i.op[0].disps->X_op != O_constant
4168 && ((!i.prefix[ADDR_PREFIX])
4169 != (flag_code == CODE_32BIT
4170 ? i.op[1].regs->reg_type.bitfield.dword
4171 : i.op[1].regs->reg_type.bitfield.word)))
4172 return;
7772f168
JB
4173 /* In 16-bit mode converting LEA with 16-bit addressing and a 32-bit
4174 destination is going to grow encoding size. */
4175 else if (flag_code == CODE_16BIT
4176 && (optimize <= 1 || optimize_for_space)
4177 && !i.prefix[ADDR_PREFIX]
4178 && i.op[1].regs->reg_type.bitfield.dword)
4179 return;
fe134c65
JB
4180 else
4181 {
4182 i.tm.base_opcode = 0xb8;
4183 i.tm.opcode_modifier.modrm = 0;
4184 if (i.op[1].regs->reg_type.bitfield.dword)
4185 i.types[0].bitfield.imm32 = 1;
4186 else
4187 i.types[0].bitfield.imm16 = 1;
4188
4189 if (i.op[0].disps
4190 && i.op[0].disps->X_op == O_constant
4191 && i.op[1].regs->reg_type.bitfield.dword
60cfa10c
L
4192 /* NB: Add () to !i.prefix[ADDR_PREFIX] to silence
4193 GCC 5. */
4194 && (!i.prefix[ADDR_PREFIX]) != (flag_code == CODE_32BIT))
fe134c65
JB
4195 i.op[0].disps->X_add_number &= 0xffff;
4196 }
4197
4198 i.tm.operand_types[0] = i.types[0];
4199 i.imm_operands = 1;
4200 if (!i.op[0].imms)
4201 {
4202 i.op[0].imms = &im_expressions[0];
4203 i.op[0].imms->X_op = O_absent;
4204 }
4205 }
4206 else if (i.op[0].disps
4207 && (i.op[0].disps->X_op != O_constant
4208 || i.op[0].disps->X_add_number))
4209 return;
4210 else
4211 {
4212 /* Handle:
4213 lea (%rM), %rN -> mov %rM, %rN
4214 lea (,%rM,1), %rN -> mov %rM, %rN
4215 lea (%rM), %rN -> movzx %rM, %rN
4216 */
4217 const reg_entry *addr_reg;
4218
4219 if (!i.index_reg && i.base_reg->reg_num != RegIP)
4220 addr_reg = i.base_reg;
4221 else if (!i.base_reg
4222 && i.index_reg->reg_num != RegIZ
4223 && !i.log2_scale_factor)
4224 addr_reg = i.index_reg;
4225 else
4226 return;
4227
4228 if (addr_reg->reg_type.bitfield.word
4229 && i.op[1].regs->reg_type.bitfield.dword)
4230 {
4231 if (flag_code != CODE_32BIT)
4232 return;
4233 i.tm.opcode_modifier.opcodespace = SPACE_0F;
4234 i.tm.base_opcode = 0xb7;
4235 }
4236 else
4237 i.tm.base_opcode = 0x8b;
4238
4239 if (addr_reg->reg_type.bitfield.dword
4240 && i.op[1].regs->reg_type.bitfield.qword)
4241 i.tm.opcode_modifier.size = SIZE32;
4242
4243 i.op[0].regs = addr_reg;
4244 i.reg_operands = 2;
4245 }
4246
4247 i.mem_operands = 0;
4248 i.disp_operands = 0;
4249 i.prefix[ADDR_PREFIX] = 0;
4250 i.prefix[SEG_PREFIX] = 0;
4251 i.seg[0] = NULL;
4252 }
4253
b6f8c7c4 4254 if (optimize_for_space
389d00a5 4255 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
b6f8c7c4
L
4256 && i.reg_operands == 1
4257 && i.imm_operands == 1
4258 && !i.types[1].bitfield.byte
4259 && i.op[0].imms->X_op == O_constant
4260 && fits_in_imm7 (i.op[0].imms->X_add_number)
72aea328 4261 && (i.tm.base_opcode == 0xa8
b6f8c7c4
L
4262 || (i.tm.base_opcode == 0xf6
4263 && i.tm.extension_opcode == 0x0)))
4264 {
4265 /* Optimize: -Os:
4266 test $imm7, %r64/%r32/%r16 -> test $imm7, %r8
4267 */
4268 unsigned int base_regnum = i.op[1].regs->reg_num;
4269 if (flag_code == CODE_64BIT || base_regnum < 4)
4270 {
4271 i.types[1].bitfield.byte = 1;
4272 /* Ignore the suffix. */
4273 i.suffix = 0;
7697afb6
JB
4274 /* Convert to byte registers. */
4275 if (i.types[1].bitfield.word)
4276 j = 16;
4277 else if (i.types[1].bitfield.dword)
4278 j = 32;
4279 else
4280 j = 48;
4281 if (!(i.op[1].regs->reg_flags & RegRex) && base_regnum < 4)
4282 j += 8;
4283 i.op[1].regs -= j;
b6f8c7c4
L
4284 }
4285 }
4286 else if (flag_code == CODE_64BIT
389d00a5 4287 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
d3d50934
L
4288 && ((i.types[1].bitfield.qword
4289 && i.reg_operands == 1
b6f8c7c4
L
4290 && i.imm_operands == 1
4291 && i.op[0].imms->X_op == O_constant
507916b8 4292 && ((i.tm.base_opcode == 0xb8
b6f8c7c4
L
4293 && i.tm.extension_opcode == None
4294 && fits_in_unsigned_long (i.op[0].imms->X_add_number))
4295 || (fits_in_imm31 (i.op[0].imms->X_add_number)
72aea328
JB
4296 && ((i.tm.base_opcode == 0x24
4297 || i.tm.base_opcode == 0xa8)
b6f8c7c4
L
4298 || (i.tm.base_opcode == 0x80
4299 && i.tm.extension_opcode == 0x4)
4300 || ((i.tm.base_opcode == 0xf6
507916b8 4301 || (i.tm.base_opcode | 1) == 0xc7)
b8364fa7
JB
4302 && i.tm.extension_opcode == 0x0)))
4303 || (fits_in_imm7 (i.op[0].imms->X_add_number)
4304 && i.tm.base_opcode == 0x83
4305 && i.tm.extension_opcode == 0x4)))
d3d50934
L
4306 || (i.types[0].bitfield.qword
4307 && ((i.reg_operands == 2
4308 && i.op[0].regs == i.op[1].regs
72aea328
JB
4309 && (i.tm.base_opcode == 0x30
4310 || i.tm.base_opcode == 0x28))
d3d50934
L
4311 || (i.reg_operands == 1
4312 && i.operands == 1
72aea328 4313 && i.tm.base_opcode == 0x30)))))
b6f8c7c4
L
4314 {
4315 /* Optimize: -O:
4316 andq $imm31, %r64 -> andl $imm31, %r32
b8364fa7 4317 andq $imm7, %r64 -> andl $imm7, %r32
b6f8c7c4
L
4318 testq $imm31, %r64 -> testl $imm31, %r32
4319 xorq %r64, %r64 -> xorl %r32, %r32
4320 subq %r64, %r64 -> subl %r32, %r32
4321 movq $imm31, %r64 -> movl $imm31, %r32
4322 movq $imm32, %r64 -> movl $imm32, %r32
4323 */
4324 i.tm.opcode_modifier.norex64 = 1;
507916b8 4325 if (i.tm.base_opcode == 0xb8 || (i.tm.base_opcode | 1) == 0xc7)
b6f8c7c4
L
4326 {
4327 /* Handle
4328 movq $imm31, %r64 -> movl $imm31, %r32
4329 movq $imm32, %r64 -> movl $imm32, %r32
4330 */
4331 i.tm.operand_types[0].bitfield.imm32 = 1;
4332 i.tm.operand_types[0].bitfield.imm32s = 0;
4333 i.tm.operand_types[0].bitfield.imm64 = 0;
4334 i.types[0].bitfield.imm32 = 1;
4335 i.types[0].bitfield.imm32s = 0;
4336 i.types[0].bitfield.imm64 = 0;
4337 i.types[1].bitfield.dword = 1;
4338 i.types[1].bitfield.qword = 0;
507916b8 4339 if ((i.tm.base_opcode | 1) == 0xc7)
b6f8c7c4
L
4340 {
4341 /* Handle
4342 movq $imm31, %r64 -> movl $imm31, %r32
4343 */
507916b8 4344 i.tm.base_opcode = 0xb8;
b6f8c7c4 4345 i.tm.extension_opcode = None;
507916b8 4346 i.tm.opcode_modifier.w = 0;
b6f8c7c4
L
4347 i.tm.opcode_modifier.modrm = 0;
4348 }
4349 }
4350 }
5641ec01
JB
4351 else if (optimize > 1
4352 && !optimize_for_space
389d00a5 4353 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
5641ec01
JB
4354 && i.reg_operands == 2
4355 && i.op[0].regs == i.op[1].regs
4356 && ((i.tm.base_opcode & ~(Opcode_D | 1)) == 0x8
4357 || (i.tm.base_opcode & ~(Opcode_D | 1)) == 0x20)
4358 && (flag_code != CODE_64BIT || !i.types[0].bitfield.dword))
4359 {
4360 /* Optimize: -O2:
4361 andb %rN, %rN -> testb %rN, %rN
4362 andw %rN, %rN -> testw %rN, %rN
4363 andq %rN, %rN -> testq %rN, %rN
4364 orb %rN, %rN -> testb %rN, %rN
4365 orw %rN, %rN -> testw %rN, %rN
4366 orq %rN, %rN -> testq %rN, %rN
4367
4368 and outside of 64-bit mode
4369
4370 andl %rN, %rN -> testl %rN, %rN
4371 orl %rN, %rN -> testl %rN, %rN
4372 */
4373 i.tm.base_opcode = 0x84 | (i.tm.base_opcode & 1);
4374 }
99112332 4375 else if (i.reg_operands == 3
b6f8c7c4
L
4376 && i.op[0].regs == i.op[1].regs
4377 && !i.types[2].bitfield.xmmword
4378 && (i.tm.opcode_modifier.vex
6225c532 4379 || ((!i.mask.reg || i.mask.zeroing)
e771e7c9 4380 && is_evex_encoding (&i.tm)
80c34c38 4381 && (i.vec_encoding != vex_encoding_evex
dd22218c 4382 || cpu_arch_isa_flags.bitfield.cpuavx512vl
80c34c38 4383 || i.tm.cpu_flags.bitfield.cpuavx512vl
7091c612 4384 || (i.tm.operand_types[2].bitfield.zmmword
dd22218c 4385 && i.types[2].bitfield.ymmword))))
5844ccaa
JB
4386 && i.tm.opcode_modifier.opcodespace == SPACE_0F
4387 && ((i.tm.base_opcode | 2) == 0x57
4388 || i.tm.base_opcode == 0xdf
4389 || i.tm.base_opcode == 0xef
4390 || (i.tm.base_opcode | 3) == 0xfb
4391 || i.tm.base_opcode == 0x42
4392 || i.tm.base_opcode == 0x47))
b6f8c7c4 4393 {
99112332 4394 /* Optimize: -O1:
8305403a
L
4395 VOP, one of vandnps, vandnpd, vxorps, vxorpd, vpsubb, vpsubd,
4396 vpsubq and vpsubw:
b6f8c7c4
L
4397 EVEX VOP %zmmM, %zmmM, %zmmN
4398 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4399 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4400 EVEX VOP %ymmM, %ymmM, %ymmN
4401 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4402 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4403 VEX VOP %ymmM, %ymmM, %ymmN
4404 -> VEX VOP %xmmM, %xmmM, %xmmN
4405 VOP, one of vpandn and vpxor:
4406 VEX VOP %ymmM, %ymmM, %ymmN
4407 -> VEX VOP %xmmM, %xmmM, %xmmN
4408 VOP, one of vpandnd and vpandnq:
4409 EVEX VOP %zmmM, %zmmM, %zmmN
4410 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4411 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4412 EVEX VOP %ymmM, %ymmM, %ymmN
4413 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4414 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4415 VOP, one of vpxord and vpxorq:
4416 EVEX VOP %zmmM, %zmmM, %zmmN
4417 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4418 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4419 EVEX VOP %ymmM, %ymmM, %ymmN
4420 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4421 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
1424ad86
JB
4422 VOP, one of kxord and kxorq:
4423 VEX VOP %kM, %kM, %kN
4424 -> VEX kxorw %kM, %kM, %kN
4425 VOP, one of kandnd and kandnq:
4426 VEX VOP %kM, %kM, %kN
4427 -> VEX kandnw %kM, %kM, %kN
b6f8c7c4 4428 */
e771e7c9 4429 if (is_evex_encoding (&i.tm))
b6f8c7c4 4430 {
7b1d7ca1 4431 if (i.vec_encoding != vex_encoding_evex)
b6f8c7c4
L
4432 {
4433 i.tm.opcode_modifier.vex = VEX128;
4434 i.tm.opcode_modifier.vexw = VEXW0;
4435 i.tm.opcode_modifier.evex = 0;
4436 }
7b1d7ca1 4437 else if (optimize > 1)
dd22218c
L
4438 i.tm.opcode_modifier.evex = EVEX128;
4439 else
4440 return;
b6f8c7c4 4441 }
f74a6307 4442 else if (i.tm.operand_types[0].bitfield.class == RegMask)
1424ad86 4443 {
35648716 4444 i.tm.opcode_modifier.opcodeprefix = PREFIX_NONE;
1424ad86
JB
4445 i.tm.opcode_modifier.vexw = VEXW0;
4446 }
b6f8c7c4
L
4447 else
4448 i.tm.opcode_modifier.vex = VEX128;
4449
4450 if (i.tm.opcode_modifier.vex)
4451 for (j = 0; j < 3; j++)
4452 {
4453 i.types[j].bitfield.xmmword = 1;
4454 i.types[j].bitfield.ymmword = 0;
4455 }
4456 }
392a5972 4457 else if (i.vec_encoding != vex_encoding_evex
97ed31ae 4458 && !i.types[0].bitfield.zmmword
392a5972 4459 && !i.types[1].bitfield.zmmword
6225c532 4460 && !i.mask.reg
a5748e0d 4461 && !i.broadcast.bytes
97ed31ae 4462 && is_evex_encoding (&i.tm)
35648716
JB
4463 && ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4464 || (i.tm.base_opcode & ~4) == 0xdb
4465 || (i.tm.base_opcode & ~4) == 0xeb)
97ed31ae
L
4466 && i.tm.extension_opcode == None)
4467 {
4468 /* Optimize: -O1:
4469 VOP, one of vmovdqa32, vmovdqa64, vmovdqu8, vmovdqu16,
4470 vmovdqu32 and vmovdqu64:
4471 EVEX VOP %xmmM, %xmmN
4472 -> VEX vmovdqa|vmovdqu %xmmM, %xmmN (M and N < 16)
4473 EVEX VOP %ymmM, %ymmN
4474 -> VEX vmovdqa|vmovdqu %ymmM, %ymmN (M and N < 16)
4475 EVEX VOP %xmmM, mem
4476 -> VEX vmovdqa|vmovdqu %xmmM, mem (M < 16)
4477 EVEX VOP %ymmM, mem
4478 -> VEX vmovdqa|vmovdqu %ymmM, mem (M < 16)
4479 EVEX VOP mem, %xmmN
4480 -> VEX mvmovdqa|vmovdquem, %xmmN (N < 16)
4481 EVEX VOP mem, %ymmN
4482 -> VEX vmovdqa|vmovdqu mem, %ymmN (N < 16)
a0a1771e
JB
4483 VOP, one of vpand, vpandn, vpor, vpxor:
4484 EVEX VOP{d,q} %xmmL, %xmmM, %xmmN
4485 -> VEX VOP %xmmL, %xmmM, %xmmN (L, M, and N < 16)
4486 EVEX VOP{d,q} %ymmL, %ymmM, %ymmN
4487 -> VEX VOP %ymmL, %ymmM, %ymmN (L, M, and N < 16)
4488 EVEX VOP{d,q} mem, %xmmM, %xmmN
4489 -> VEX VOP mem, %xmmM, %xmmN (M and N < 16)
4490 EVEX VOP{d,q} mem, %ymmM, %ymmN
4491 -> VEX VOP mem, %ymmM, %ymmN (M and N < 16)
97ed31ae 4492 */
a0a1771e 4493 for (j = 0; j < i.operands; j++)
392a5972
L
4494 if (operand_type_check (i.types[j], disp)
4495 && i.op[j].disps->X_op == O_constant)
4496 {
4497 /* Since the VEX prefix has 2 or 3 bytes, the EVEX prefix
4498 has 4 bytes, EVEX Disp8 has 1 byte and VEX Disp32 has 4
4499 bytes, we choose EVEX Disp8 over VEX Disp32. */
4500 int evex_disp8, vex_disp8;
4501 unsigned int memshift = i.memshift;
4502 offsetT n = i.op[j].disps->X_add_number;
4503
4504 evex_disp8 = fits_in_disp8 (n);
4505 i.memshift = 0;
4506 vex_disp8 = fits_in_disp8 (n);
4507 if (evex_disp8 != vex_disp8)
4508 {
4509 i.memshift = memshift;
4510 return;
4511 }
4512
4513 i.types[j].bitfield.disp8 = vex_disp8;
4514 break;
4515 }
35648716
JB
4516 if ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4517 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2)
4518 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
97ed31ae
L
4519 i.tm.opcode_modifier.vex
4520 = i.types[0].bitfield.ymmword ? VEX256 : VEX128;
4521 i.tm.opcode_modifier.vexw = VEXW0;
79dec6b7 4522 /* VPAND, VPOR, and VPXOR are commutative. */
35648716 4523 if (i.reg_operands == 3 && i.tm.base_opcode != 0xdf)
79dec6b7 4524 i.tm.opcode_modifier.commutative = 1;
97ed31ae
L
4525 i.tm.opcode_modifier.evex = 0;
4526 i.tm.opcode_modifier.masking = 0;
a0a1771e 4527 i.tm.opcode_modifier.broadcast = 0;
97ed31ae
L
4528 i.tm.opcode_modifier.disp8memshift = 0;
4529 i.memshift = 0;
a0a1771e
JB
4530 if (j < i.operands)
4531 i.types[j].bitfield.disp8
4532 = fits_in_disp8 (i.op[j].disps->X_add_number);
97ed31ae 4533 }
b6f8c7c4
L
4534}
4535
ae531041
L
4536/* Return non-zero for load instruction. */
4537
4538static int
4539load_insn_p (void)
4540{
4541 unsigned int dest;
4542 int any_vex_p = is_any_vex_encoding (&i.tm);
4543 unsigned int base_opcode = i.tm.base_opcode | 1;
4544
4545 if (!any_vex_p)
4546 {
ef07be45
CL
4547 /* Anysize insns: lea, invlpg, clflush, prefetch*, bndmk, bndcl, bndcu,
4548 bndcn, bndstx, bndldx, clflushopt, clwb, cldemote. */
255571cd 4549 if (i.tm.opcode_modifier.operandconstraint == ANY_SIZE)
ae531041
L
4550 return 0;
4551
389d00a5
JB
4552 /* pop. */
4553 if (strcmp (i.tm.name, "pop") == 0)
4554 return 1;
4555 }
4556
4557 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE)
4558 {
4559 /* popf, popa. */
4560 if (i.tm.base_opcode == 0x9d
a09f656b 4561 || i.tm.base_opcode == 0x61)
ae531041
L
4562 return 1;
4563
4564 /* movs, cmps, lods, scas. */
4565 if ((i.tm.base_opcode | 0xb) == 0xaf)
4566 return 1;
4567
a09f656b 4568 /* outs, xlatb. */
4569 if (base_opcode == 0x6f
4570 || i.tm.base_opcode == 0xd7)
ae531041 4571 return 1;
a09f656b 4572 /* NB: For AMD-specific insns with implicit memory operands,
4573 they're intentionally not covered. */
ae531041
L
4574 }
4575
4576 /* No memory operand. */
4577 if (!i.mem_operands)
4578 return 0;
4579
4580 if (any_vex_p)
4581 {
4582 /* vldmxcsr. */
4583 if (i.tm.base_opcode == 0xae
4584 && i.tm.opcode_modifier.vex
441f6aca 4585 && i.tm.opcode_modifier.opcodespace == SPACE_0F
35648716 4586 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
ae531041
L
4587 && i.tm.extension_opcode == 2)
4588 return 1;
4589 }
389d00a5 4590 else if (i.tm.opcode_modifier.opcodespace == SPACE_BASE)
ae531041
L
4591 {
4592 /* test, not, neg, mul, imul, div, idiv. */
4593 if ((i.tm.base_opcode == 0xf6 || i.tm.base_opcode == 0xf7)
4594 && i.tm.extension_opcode != 1)
4595 return 1;
4596
4597 /* inc, dec. */
4598 if (base_opcode == 0xff && i.tm.extension_opcode <= 1)
4599 return 1;
4600
4601 /* add, or, adc, sbb, and, sub, xor, cmp. */
4602 if (i.tm.base_opcode >= 0x80 && i.tm.base_opcode <= 0x83)
4603 return 1;
4604
ae531041
L
4605 /* rol, ror, rcl, rcr, shl/sal, shr, sar. */
4606 if ((base_opcode == 0xc1
4607 || (i.tm.base_opcode >= 0xd0 && i.tm.base_opcode <= 0xd3))
4608 && i.tm.extension_opcode != 6)
4609 return 1;
4610
ae531041 4611 /* Check for x87 instructions. */
389d00a5 4612 if (base_opcode >= 0xd8 && base_opcode <= 0xdf)
ae531041
L
4613 {
4614 /* Skip fst, fstp, fstenv, fstcw. */
4615 if (i.tm.base_opcode == 0xd9
4616 && (i.tm.extension_opcode == 2
4617 || i.tm.extension_opcode == 3
4618 || i.tm.extension_opcode == 6
4619 || i.tm.extension_opcode == 7))
4620 return 0;
4621
4622 /* Skip fisttp, fist, fistp, fstp. */
4623 if (i.tm.base_opcode == 0xdb
4624 && (i.tm.extension_opcode == 1
4625 || i.tm.extension_opcode == 2
4626 || i.tm.extension_opcode == 3
4627 || i.tm.extension_opcode == 7))
4628 return 0;
4629
4630 /* Skip fisttp, fst, fstp, fsave, fstsw. */
4631 if (i.tm.base_opcode == 0xdd
4632 && (i.tm.extension_opcode == 1
4633 || i.tm.extension_opcode == 2
4634 || i.tm.extension_opcode == 3
4635 || i.tm.extension_opcode == 6
4636 || i.tm.extension_opcode == 7))
4637 return 0;
4638
4639 /* Skip fisttp, fist, fistp, fbstp, fistp. */
4640 if (i.tm.base_opcode == 0xdf
4641 && (i.tm.extension_opcode == 1
4642 || i.tm.extension_opcode == 2
4643 || i.tm.extension_opcode == 3
4644 || i.tm.extension_opcode == 6
4645 || i.tm.extension_opcode == 7))
4646 return 0;
4647
4648 return 1;
4649 }
4650 }
389d00a5
JB
4651 else if (i.tm.opcode_modifier.opcodespace == SPACE_0F)
4652 {
4653 /* bt, bts, btr, btc. */
4654 if (i.tm.base_opcode == 0xba
4655 && (i.tm.extension_opcode >= 4 && i.tm.extension_opcode <= 7))
4656 return 1;
4657
4658 /* cmpxchg8b, cmpxchg16b, xrstors, vmptrld. */
4659 if (i.tm.base_opcode == 0xc7
4660 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
4661 && (i.tm.extension_opcode == 1 || i.tm.extension_opcode == 3
4662 || i.tm.extension_opcode == 6))
4663 return 1;
4664
4665 /* fxrstor, ldmxcsr, xrstor. */
4666 if (i.tm.base_opcode == 0xae
4667 && (i.tm.extension_opcode == 1
4668 || i.tm.extension_opcode == 2
4669 || i.tm.extension_opcode == 5))
4670 return 1;
4671
4672 /* lgdt, lidt, lmsw. */
4673 if (i.tm.base_opcode == 0x01
4674 && (i.tm.extension_opcode == 2
4675 || i.tm.extension_opcode == 3
4676 || i.tm.extension_opcode == 6))
4677 return 1;
4678 }
ae531041
L
4679
4680 dest = i.operands - 1;
4681
4682 /* Check fake imm8 operand and 3 source operands. */
4683 if ((i.tm.opcode_modifier.immext
4684 || i.tm.opcode_modifier.vexsources == VEX3SOURCES)
4685 && i.types[dest].bitfield.imm8)
4686 dest--;
4687
389d00a5
JB
4688 /* add, or, adc, sbb, and, sub, xor, cmp, test, xchg. */
4689 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
ae531041
L
4690 && (base_opcode == 0x1
4691 || base_opcode == 0x9
4692 || base_opcode == 0x11
4693 || base_opcode == 0x19
4694 || base_opcode == 0x21
4695 || base_opcode == 0x29
4696 || base_opcode == 0x31
4697 || base_opcode == 0x39
389d00a5
JB
4698 || (base_opcode | 2) == 0x87))
4699 return 1;
4700
4701 /* xadd. */
4702 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4703 && base_opcode == 0xc1)
ae531041
L
4704 return 1;
4705
4706 /* Check for load instruction. */
4707 return (i.types[dest].bitfield.class != ClassNone
4708 || i.types[dest].bitfield.instance == Accum);
4709}
4710
4711/* Output lfence, 0xfaee8, after instruction. */
4712
4713static void
4714insert_lfence_after (void)
4715{
4716 if (lfence_after_load && load_insn_p ())
4717 {
a09f656b 4718 /* There are also two REP string instructions that require
4719 special treatment. Specifically, the compare string (CMPS)
4720 and scan string (SCAS) instructions set EFLAGS in a manner
4721 that depends on the data being compared/scanned. When used
4722 with a REP prefix, the number of iterations may therefore
4723 vary depending on this data. If the data is a program secret
4724 chosen by the adversary using an LVI method,
4725 then this data-dependent behavior may leak some aspect
4726 of the secret. */
4727 if (((i.tm.base_opcode | 0x1) == 0xa7
4728 || (i.tm.base_opcode | 0x1) == 0xaf)
4729 && i.prefix[REP_PREFIX])
4730 {
4731 as_warn (_("`%s` changes flags which would affect control flow behavior"),
4732 i.tm.name);
4733 }
ae531041
L
4734 char *p = frag_more (3);
4735 *p++ = 0xf;
4736 *p++ = 0xae;
4737 *p = 0xe8;
4738 }
4739}
4740
4741/* Output lfence, 0xfaee8, before instruction. */
4742
4743static void
4744insert_lfence_before (void)
4745{
4746 char *p;
4747
389d00a5 4748 if (i.tm.opcode_modifier.opcodespace != SPACE_BASE)
ae531041
L
4749 return;
4750
4751 if (i.tm.base_opcode == 0xff
4752 && (i.tm.extension_opcode == 2 || i.tm.extension_opcode == 4))
4753 {
4754 /* Insert lfence before indirect branch if needed. */
4755
4756 if (lfence_before_indirect_branch == lfence_branch_none)
4757 return;
4758
4759 if (i.operands != 1)
4760 abort ();
4761
4762 if (i.reg_operands == 1)
4763 {
4764 /* Indirect branch via register. Don't insert lfence with
4765 -mlfence-after-load=yes. */
4766 if (lfence_after_load
4767 || lfence_before_indirect_branch == lfence_branch_memory)
4768 return;
4769 }
4770 else if (i.mem_operands == 1
4771 && lfence_before_indirect_branch != lfence_branch_register)
4772 {
4773 as_warn (_("indirect `%s` with memory operand should be avoided"),
4774 i.tm.name);
4775 return;
4776 }
4777 else
4778 return;
4779
4780 if (last_insn.kind != last_insn_other
4781 && last_insn.seg == now_seg)
4782 {
4783 as_warn_where (last_insn.file, last_insn.line,
4784 _("`%s` skips -mlfence-before-indirect-branch on `%s`"),
4785 last_insn.name, i.tm.name);
4786 return;
4787 }
4788
4789 p = frag_more (3);
4790 *p++ = 0xf;
4791 *p++ = 0xae;
4792 *p = 0xe8;
4793 return;
4794 }
4795
503648e4 4796 /* Output or/not/shl and lfence before near ret. */
ae531041
L
4797 if (lfence_before_ret != lfence_before_ret_none
4798 && (i.tm.base_opcode == 0xc2
503648e4 4799 || i.tm.base_opcode == 0xc3))
ae531041
L
4800 {
4801 if (last_insn.kind != last_insn_other
4802 && last_insn.seg == now_seg)
4803 {
4804 as_warn_where (last_insn.file, last_insn.line,
4805 _("`%s` skips -mlfence-before-ret on `%s`"),
4806 last_insn.name, i.tm.name);
4807 return;
4808 }
a09f656b 4809
a09f656b 4810 /* Near ret ingore operand size override under CPU64. */
503648e4 4811 char prefix = flag_code == CODE_64BIT
4812 ? 0x48
4813 : i.prefix[DATA_PREFIX] ? 0x66 : 0x0;
a09f656b 4814
4815 if (lfence_before_ret == lfence_before_ret_not)
4816 {
4817 /* not: 0xf71424, may add prefix
4818 for operand size override or 64-bit code. */
4819 p = frag_more ((prefix ? 2 : 0) + 6 + 3);
4820 if (prefix)
4821 *p++ = prefix;
ae531041
L
4822 *p++ = 0xf7;
4823 *p++ = 0x14;
4824 *p++ = 0x24;
a09f656b 4825 if (prefix)
4826 *p++ = prefix;
ae531041
L
4827 *p++ = 0xf7;
4828 *p++ = 0x14;
4829 *p++ = 0x24;
4830 }
a09f656b 4831 else
4832 {
4833 p = frag_more ((prefix ? 1 : 0) + 4 + 3);
4834 if (prefix)
4835 *p++ = prefix;
4836 if (lfence_before_ret == lfence_before_ret_or)
4837 {
4838 /* or: 0x830c2400, may add prefix
4839 for operand size override or 64-bit code. */
4840 *p++ = 0x83;
4841 *p++ = 0x0c;
4842 }
4843 else
4844 {
4845 /* shl: 0xc1242400, may add prefix
4846 for operand size override or 64-bit code. */
4847 *p++ = 0xc1;
4848 *p++ = 0x24;
4849 }
4850
4851 *p++ = 0x24;
4852 *p++ = 0x0;
4853 }
4854
ae531041
L
4855 *p++ = 0xf;
4856 *p++ = 0xae;
4857 *p = 0xe8;
4858 }
4859}
4860
252b5132
RH
4861/* This is the guts of the machine-dependent assembler. LINE points to a
4862 machine dependent instruction. This function is supposed to emit
4863 the frags/bytes it assembles to. */
4864
4865void
65da13b5 4866md_assemble (char *line)
252b5132 4867{
40fb9820 4868 unsigned int j;
83b16ac6 4869 char mnemonic[MAX_MNEM_SIZE], mnem_suffix;
d3ce72d0 4870 const insn_template *t;
252b5132 4871
47926f60 4872 /* Initialize globals. */
252b5132 4873 memset (&i, '\0', sizeof (i));
ca5312a2 4874 i.rounding.type = rc_none;
252b5132 4875 for (j = 0; j < MAX_OPERANDS; j++)
1ae12ab7 4876 i.reloc[j] = NO_RELOC;
252b5132
RH
4877 memset (disp_expressions, '\0', sizeof (disp_expressions));
4878 memset (im_expressions, '\0', sizeof (im_expressions));
ce8a8b2f 4879 save_stack_p = save_stack;
252b5132
RH
4880
4881 /* First parse an instruction mnemonic & call i386_operand for the operands.
4882 We assume that the scrubber has arranged it so that line[0] is the valid
47926f60 4883 start of a (possibly prefixed) mnemonic. */
252b5132 4884
29b0f896
AM
4885 line = parse_insn (line, mnemonic);
4886 if (line == NULL)
4887 return;
83b16ac6 4888 mnem_suffix = i.suffix;
252b5132 4889
29b0f896 4890 line = parse_operands (line, mnemonic);
ee86248c 4891 this_operand = -1;
8325cc63
JB
4892 xfree (i.memop1_string);
4893 i.memop1_string = NULL;
29b0f896
AM
4894 if (line == NULL)
4895 return;
252b5132 4896
29b0f896
AM
4897 /* Now we've parsed the mnemonic into a set of templates, and have the
4898 operands at hand. */
4899
b630c145 4900 /* All Intel opcodes have reversed operands except for "bound", "enter",
c0e54661 4901 "invlpg*", "monitor*", "mwait*", "tpause", "umwait", "pvalidate",
b0e8fa7f
TJ
4902 "rmpadjust", "rmpupdate", and "rmpquery". We also don't reverse
4903 intersegment "jmp" and "call" instructions with 2 immediate operands so
4904 that the immediate segment precedes the offset consistently in Intel and
4905 AT&T modes. */
4d456e3d
L
4906 if (intel_syntax
4907 && i.operands > 1
29b0f896 4908 && (strcmp (mnemonic, "bound") != 0)
c0e54661 4909 && (strncmp (mnemonic, "invlpg", 6) != 0)
d34049e8
ML
4910 && !startswith (mnemonic, "monitor")
4911 && !startswith (mnemonic, "mwait")
c0e54661 4912 && (strcmp (mnemonic, "pvalidate") != 0)
d34049e8 4913 && !startswith (mnemonic, "rmp")
b630c145
JB
4914 && (strcmp (mnemonic, "tpause") != 0)
4915 && (strcmp (mnemonic, "umwait") != 0)
47c0279b
JB
4916 && !(i.operands == 2
4917 && operand_type_check (i.types[0], imm)
40fb9820 4918 && operand_type_check (i.types[1], imm)))
29b0f896
AM
4919 swap_operands ();
4920
ec56d5c0
JB
4921 /* The order of the immediates should be reversed
4922 for 2 immediates extrq and insertq instructions */
4923 if (i.imm_operands == 2
4924 && (strcmp (mnemonic, "extrq") == 0
4925 || strcmp (mnemonic, "insertq") == 0))
4926 swap_2_operands (0, 1);
4927
29b0f896
AM
4928 if (i.imm_operands)
4929 optimize_imm ();
4930
9386188e
JB
4931 if (i.disp_operands && !want_disp32 (current_templates->start)
4932 && (!current_templates->start->opcode_modifier.jump
4933 || i.jumpabsolute || i.types[0].bitfield.baseindex))
cce08655
JB
4934 {
4935 for (j = 0; j < i.operands; ++j)
4936 {
4937 const expressionS *exp = i.op[j].disps;
4938
4939 if (!operand_type_check (i.types[j], disp))
4940 continue;
4941
4942 if (exp->X_op != O_constant)
4943 continue;
4944
4945 /* Since displacement is signed extended to 64bit, don't allow
a775efc8 4946 disp32 if it is out of range. */
cce08655
JB
4947 if (fits_in_signed_long (exp->X_add_number))
4948 continue;
4949
a775efc8 4950 i.types[j].bitfield.disp32 = 0;
cce08655
JB
4951 if (i.types[j].bitfield.baseindex)
4952 {
f493c217
AM
4953 as_bad (_("0x%" PRIx64 " out of range of signed 32bit displacement"),
4954 (uint64_t) exp->X_add_number);
cce08655
JB
4955 return;
4956 }
4957 }
4958 }
4959
b300c311
L
4960 /* Don't optimize displacement for movabs since it only takes 64bit
4961 displacement. */
4962 if (i.disp_operands
1a42a9fe 4963 && i.disp_encoding <= disp_encoding_8bit
862be3fb
L
4964 && (flag_code != CODE_64BIT
4965 || strcmp (mnemonic, "movabs") != 0))
4966 optimize_disp ();
29b0f896
AM
4967
4968 /* Next, we find a template that matches the given insn,
4969 making sure the overlap of the given operands types is consistent
4970 with the template operand types. */
252b5132 4971
83b16ac6 4972 if (!(t = match_template (mnem_suffix)))
29b0f896 4973 return;
252b5132 4974
7bab8ab5 4975 if (sse_check != check_none
ffb86450
JB
4976 /* The opcode space check isn't strictly needed; it's there only to
4977 bypass the logic below when easily possible. */
4978 && t->opcode_modifier.opcodespace >= SPACE_0F
4979 && t->opcode_modifier.opcodespace <= SPACE_0F3A
4980 && !i.tm.cpu_flags.bitfield.cpusse4a
4981 && !is_any_vex_encoding (t))
daf50ae7 4982 {
ffb86450
JB
4983 bool simd = false;
4984
4985 for (j = 0; j < t->operands; ++j)
4986 {
4987 if (t->operand_types[j].bitfield.class == RegMMX)
4988 break;
4989 if (t->operand_types[j].bitfield.class == RegSIMD)
4990 simd = true;
4991 }
4992
4993 if (j >= t->operands && simd)
4994 (sse_check == check_warning
4995 ? as_warn
4996 : as_bad) (_("SSE instruction `%s' is used"), i.tm.name);
daf50ae7
L
4997 }
4998
40fb9820 4999 if (i.tm.opcode_modifier.fwait)
29b0f896
AM
5000 if (!add_prefix (FWAIT_OPCODE))
5001 return;
252b5132 5002
d5de92cf 5003 /* Check if REP prefix is OK. */
742732c7 5004 if (i.rep_prefix && i.tm.opcode_modifier.prefixok != PrefixRep)
d5de92cf
L
5005 {
5006 as_bad (_("invalid instruction `%s' after `%s'"),
5007 i.tm.name, i.rep_prefix);
5008 return;
5009 }
5010
c1ba0266
L
5011 /* Check for lock without a lockable instruction. Destination operand
5012 must be memory unless it is xchg (0x86). */
c32fa91d 5013 if (i.prefix[LOCK_PREFIX]
742732c7 5014 && (i.tm.opcode_modifier.prefixok < PrefixLock
c1ba0266
L
5015 || i.mem_operands == 0
5016 || (i.tm.base_opcode != 0x86
8dc0818e 5017 && !(i.flags[i.operands - 1] & Operand_Mem))))
c32fa91d
L
5018 {
5019 as_bad (_("expecting lockable instruction after `lock'"));
5020 return;
5021 }
5022
40d231b4
JB
5023 /* Check for data size prefix on VEX/XOP/EVEX encoded and SIMD insns. */
5024 if (i.prefix[DATA_PREFIX]
5025 && (is_any_vex_encoding (&i.tm)
5026 || i.tm.operand_types[i.imm_operands].bitfield.class >= RegMMX
5027 || i.tm.operand_types[i.imm_operands + 1].bitfield.class >= RegMMX))
7a8655d2
JB
5028 {
5029 as_bad (_("data size prefix invalid with `%s'"), i.tm.name);
5030 return;
5031 }
5032
42164a71 5033 /* Check if HLE prefix is OK. */
165de32a 5034 if (i.hle_prefix && !check_hle ())
42164a71
L
5035 return;
5036
7e8b059b
L
5037 /* Check BND prefix. */
5038 if (i.bnd_prefix && !i.tm.opcode_modifier.bndprefixok)
5039 as_bad (_("expecting valid branch instruction after `bnd'"));
5040
04ef582a 5041 /* Check NOTRACK prefix. */
742732c7 5042 if (i.notrack_prefix && i.tm.opcode_modifier.prefixok != PrefixNoTrack)
9fef80d6 5043 as_bad (_("expecting indirect branch instruction after `notrack'"));
04ef582a 5044
327e8c42
JB
5045 if (i.tm.cpu_flags.bitfield.cpumpx)
5046 {
5047 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
5048 as_bad (_("32-bit address isn't allowed in 64-bit MPX instructions."));
5049 else if (flag_code != CODE_16BIT
5050 ? i.prefix[ADDR_PREFIX]
5051 : i.mem_operands && !i.prefix[ADDR_PREFIX])
5052 as_bad (_("16-bit address isn't allowed in MPX instructions"));
5053 }
7e8b059b
L
5054
5055 /* Insert BND prefix. */
76d3a78a
JB
5056 if (add_bnd_prefix && i.tm.opcode_modifier.bndprefixok)
5057 {
5058 if (!i.prefix[BND_PREFIX])
5059 add_prefix (BND_PREFIX_OPCODE);
5060 else if (i.prefix[BND_PREFIX] != BND_PREFIX_OPCODE)
5061 {
5062 as_warn (_("replacing `rep'/`repe' prefix by `bnd'"));
5063 i.prefix[BND_PREFIX] = BND_PREFIX_OPCODE;
5064 }
5065 }
7e8b059b 5066
29b0f896 5067 /* Check string instruction segment overrides. */
51c8edf6 5068 if (i.tm.opcode_modifier.isstring >= IS_STRING_ES_OP0)
29b0f896 5069 {
51c8edf6 5070 gas_assert (i.mem_operands);
29b0f896 5071 if (!check_string ())
5dd0794d 5072 return;
fc0763e6 5073 i.disp_operands = 0;
29b0f896 5074 }
5dd0794d 5075
9373f275
L
5076 /* The memory operand of (%dx) should be only used with input/output
5077 instructions (base opcodes: 0x6c, 0x6e, 0xec, 0xee). */
5078 if (i.input_output_operand
5079 && ((i.tm.base_opcode | 0x82) != 0xee
5080 || i.tm.opcode_modifier.opcodespace != SPACE_BASE))
5081 {
5082 as_bad (_("input/output port address isn't allowed with `%s'"),
5083 i.tm.name);
5084 return;
5085 }
5086
b6f8c7c4
L
5087 if (optimize && !i.no_optimize && i.tm.opcode_modifier.optimize)
5088 optimize_encoding ();
5089
c8480b58
L
5090 if (use_unaligned_vector_move)
5091 encode_with_unaligned_vector_move ();
5092
29b0f896
AM
5093 if (!process_suffix ())
5094 return;
e413e4e9 5095
ef07be45
CL
5096 /* Check if IP-relative addressing requirements can be satisfied. */
5097 if (i.tm.cpu_flags.bitfield.cpuprefetchi
5098 && !(i.base_reg && i.base_reg->reg_num == RegIP))
f2462532 5099 as_warn (_("'%s' only supports RIP-relative address"), i.tm.name);
ef07be45 5100
921eafea 5101 /* Update operand types and check extended states. */
bc0844ae 5102 for (j = 0; j < i.operands; j++)
921eafea
L
5103 {
5104 i.types[j] = operand_type_and (i.types[j], i.tm.operand_types[j]);
3d70986f 5105 switch (i.tm.operand_types[j].bitfield.class)
921eafea
L
5106 {
5107 default:
5108 break;
5109 case RegMMX:
5110 i.xstate |= xstate_mmx;
5111 break;
5112 case RegMask:
32930e4e 5113 i.xstate |= xstate_mask;
921eafea
L
5114 break;
5115 case RegSIMD:
3d70986f 5116 if (i.tm.operand_types[j].bitfield.tmmword)
921eafea 5117 i.xstate |= xstate_tmm;
3d70986f 5118 else if (i.tm.operand_types[j].bitfield.zmmword)
921eafea 5119 i.xstate |= xstate_zmm;
3d70986f 5120 else if (i.tm.operand_types[j].bitfield.ymmword)
921eafea 5121 i.xstate |= xstate_ymm;
3d70986f 5122 else if (i.tm.operand_types[j].bitfield.xmmword)
921eafea
L
5123 i.xstate |= xstate_xmm;
5124 break;
5125 }
5126 }
bc0844ae 5127
29b0f896
AM
5128 /* Make still unresolved immediate matches conform to size of immediate
5129 given in i.suffix. */
5130 if (!finalize_imm ())
5131 return;
252b5132 5132
40fb9820 5133 if (i.types[0].bitfield.imm1)
29b0f896 5134 i.imm_operands = 0; /* kludge for shift insns. */
252b5132 5135
9afe6eb8
L
5136 /* We only need to check those implicit registers for instructions
5137 with 3 operands or less. */
5138 if (i.operands <= 3)
5139 for (j = 0; j < i.operands; j++)
75e5731b
JB
5140 if (i.types[j].bitfield.instance != InstanceNone
5141 && !i.types[j].bitfield.xmmword)
9afe6eb8 5142 i.reg_operands--;
40fb9820 5143
29b0f896
AM
5144 /* For insns with operands there are more diddles to do to the opcode. */
5145 if (i.operands)
5146 {
5147 if (!process_operands ())
5148 return;
5149 }
255571cd 5150 else if (!quiet_warnings && i.tm.opcode_modifier.operandconstraint == UGH)
29b0f896
AM
5151 {
5152 /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc. */
5153 as_warn (_("translating to `%sp'"), i.tm.name);
5154 }
252b5132 5155
7a8655d2 5156 if (is_any_vex_encoding (&i.tm))
9e5e5283 5157 {
c1dc7af5 5158 if (!cpu_arch_flags.bitfield.cpui286)
9e5e5283 5159 {
c1dc7af5 5160 as_bad (_("instruction `%s' isn't supported outside of protected mode."),
9e5e5283
L
5161 i.tm.name);
5162 return;
5163 }
c0f3af97 5164
0b9404fd
JB
5165 /* Check for explicit REX prefix. */
5166 if (i.prefix[REX_PREFIX] || i.rex_encoding)
5167 {
5168 as_bad (_("REX prefix invalid with `%s'"), i.tm.name);
5169 return;
5170 }
5171
9e5e5283
L
5172 if (i.tm.opcode_modifier.vex)
5173 build_vex_prefix (t);
5174 else
5175 build_evex_prefix ();
0b9404fd
JB
5176
5177 /* The individual REX.RXBW bits got consumed. */
5178 i.rex &= REX_OPCODE;
9e5e5283 5179 }
43234a1e 5180
5dd85c99
SP
5181 /* Handle conversion of 'int $3' --> special int3 insn. XOP or FMA4
5182 instructions may define INT_OPCODE as well, so avoid this corner
5183 case for those instructions that use MODRM. */
389d00a5
JB
5184 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
5185 && i.tm.base_opcode == INT_OPCODE
a6461c02
SP
5186 && !i.tm.opcode_modifier.modrm
5187 && i.op[0].imms->X_add_number == 3)
29b0f896
AM
5188 {
5189 i.tm.base_opcode = INT3_OPCODE;
5190 i.imm_operands = 0;
5191 }
252b5132 5192
0cfa3eb3
JB
5193 if ((i.tm.opcode_modifier.jump == JUMP
5194 || i.tm.opcode_modifier.jump == JUMP_BYTE
5195 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896
AM
5196 && i.op[0].disps->X_op == O_constant)
5197 {
5198 /* Convert "jmp constant" (and "call constant") to a jump (call) to
5199 the absolute address given by the constant. Since ix86 jumps and
5200 calls are pc relative, we need to generate a reloc. */
5201 i.op[0].disps->X_add_symbol = &abs_symbol;
5202 i.op[0].disps->X_op = O_symbol;
5203 }
252b5132 5204
29b0f896
AM
5205 /* For 8 bit registers we need an empty rex prefix. Also if the
5206 instruction already has a prefix, we need to convert old
5207 registers to new ones. */
773f551c 5208
bab6aec1 5209 if ((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte
29b0f896 5210 && (i.op[0].regs->reg_flags & RegRex64) != 0)
bab6aec1 5211 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte
29b0f896 5212 && (i.op[1].regs->reg_flags & RegRex64) != 0)
bab6aec1
JB
5213 || (((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte)
5214 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte))
29b0f896
AM
5215 && i.rex != 0))
5216 {
5217 int x;
726c5dcd 5218
29b0f896
AM
5219 i.rex |= REX_OPCODE;
5220 for (x = 0; x < 2; x++)
5221 {
5222 /* Look for 8 bit operand that uses old registers. */
bab6aec1 5223 if (i.types[x].bitfield.class == Reg && i.types[x].bitfield.byte
29b0f896 5224 && (i.op[x].regs->reg_flags & RegRex64) == 0)
773f551c 5225 {
3f93af61 5226 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
29b0f896
AM
5227 /* In case it is "hi" register, give up. */
5228 if (i.op[x].regs->reg_num > 3)
a540244d 5229 as_bad (_("can't encode register '%s%s' in an "
4eed87de 5230 "instruction requiring REX prefix."),
a540244d 5231 register_prefix, i.op[x].regs->reg_name);
773f551c 5232
29b0f896
AM
5233 /* Otherwise it is equivalent to the extended register.
5234 Since the encoding doesn't change this is merely
5235 cosmetic cleanup for debug output. */
5236
5237 i.op[x].regs = i.op[x].regs + 8;
773f551c 5238 }
29b0f896
AM
5239 }
5240 }
773f551c 5241
6b6b6807
L
5242 if (i.rex == 0 && i.rex_encoding)
5243 {
5244 /* Check if we can add a REX_OPCODE byte. Look for 8 bit operand
3f93af61 5245 that uses legacy register. If it is "hi" register, don't add
6b6b6807
L
5246 the REX_OPCODE byte. */
5247 int x;
5248 for (x = 0; x < 2; x++)
bab6aec1 5249 if (i.types[x].bitfield.class == Reg
6b6b6807
L
5250 && i.types[x].bitfield.byte
5251 && (i.op[x].regs->reg_flags & RegRex64) == 0
5252 && i.op[x].regs->reg_num > 3)
5253 {
3f93af61 5254 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
5b7c81bd 5255 i.rex_encoding = false;
6b6b6807
L
5256 break;
5257 }
5258
5259 if (i.rex_encoding)
5260 i.rex = REX_OPCODE;
5261 }
5262
7ab9ffdd 5263 if (i.rex != 0)
29b0f896
AM
5264 add_prefix (REX_OPCODE | i.rex);
5265
ae531041
L
5266 insert_lfence_before ();
5267
29b0f896
AM
5268 /* We are ready to output the insn. */
5269 output_insn ();
e379e5f3 5270
ae531041
L
5271 insert_lfence_after ();
5272
e379e5f3
L
5273 last_insn.seg = now_seg;
5274
5275 if (i.tm.opcode_modifier.isprefix)
5276 {
5277 last_insn.kind = last_insn_prefix;
5278 last_insn.name = i.tm.name;
5279 last_insn.file = as_where (&last_insn.line);
5280 }
5281 else
5282 last_insn.kind = last_insn_other;
29b0f896
AM
5283}
5284
5285static char *
e3bb37b5 5286parse_insn (char *line, char *mnemonic)
29b0f896
AM
5287{
5288 char *l = line;
5289 char *token_start = l;
5290 char *mnem_p;
5c6af06e 5291 int supported;
d3ce72d0 5292 const insn_template *t;
b6169b20 5293 char *dot_p = NULL;
29b0f896 5294
29b0f896
AM
5295 while (1)
5296 {
5297 mnem_p = mnemonic;
5298 while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
5299 {
b6169b20
L
5300 if (*mnem_p == '.')
5301 dot_p = mnem_p;
29b0f896
AM
5302 mnem_p++;
5303 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
45288df1 5304 {
29b0f896
AM
5305 as_bad (_("no such instruction: `%s'"), token_start);
5306 return NULL;
5307 }
5308 l++;
5309 }
5310 if (!is_space_char (*l)
5311 && *l != END_OF_INSN
e44823cf
JB
5312 && (intel_syntax
5313 || (*l != PREFIX_SEPARATOR
5314 && *l != ',')))
29b0f896
AM
5315 {
5316 as_bad (_("invalid character %s in mnemonic"),
5317 output_invalid (*l));
5318 return NULL;
5319 }
5320 if (token_start == l)
5321 {
e44823cf 5322 if (!intel_syntax && *l == PREFIX_SEPARATOR)
29b0f896
AM
5323 as_bad (_("expecting prefix; got nothing"));
5324 else
5325 as_bad (_("expecting mnemonic; got nothing"));
5326 return NULL;
5327 }
45288df1 5328
29b0f896 5329 /* Look up instruction (or prefix) via hash table. */
629310ab 5330 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
47926f60 5331
29b0f896
AM
5332 if (*l != END_OF_INSN
5333 && (!is_space_char (*l) || l[1] != END_OF_INSN)
5334 && current_templates
40fb9820 5335 && current_templates->start->opcode_modifier.isprefix)
29b0f896 5336 {
c6fb90c8 5337 if (!cpu_flags_check_cpu64 (current_templates->start->cpu_flags))
2dd88dca
JB
5338 {
5339 as_bad ((flag_code != CODE_64BIT
5340 ? _("`%s' is only supported in 64-bit mode")
5341 : _("`%s' is not supported in 64-bit mode")),
5342 current_templates->start->name);
5343 return NULL;
5344 }
29b0f896
AM
5345 /* If we are in 16-bit mode, do not allow addr16 or data16.
5346 Similarly, in 32-bit mode, do not allow addr32 or data32. */
673fe0f0
JB
5347 if ((current_templates->start->opcode_modifier.size == SIZE16
5348 || current_templates->start->opcode_modifier.size == SIZE32)
29b0f896 5349 && flag_code != CODE_64BIT
673fe0f0 5350 && ((current_templates->start->opcode_modifier.size == SIZE32)
29b0f896
AM
5351 ^ (flag_code == CODE_16BIT)))
5352 {
5353 as_bad (_("redundant %s prefix"),
5354 current_templates->start->name);
5355 return NULL;
45288df1 5356 }
31184569
JB
5357
5358 if (current_templates->start->base_opcode == PSEUDO_PREFIX)
29b0f896 5359 {
86fa6981 5360 /* Handle pseudo prefixes. */
31184569 5361 switch (current_templates->start->extension_opcode)
86fa6981 5362 {
41eb8e88 5363 case Prefix_Disp8:
86fa6981
L
5364 /* {disp8} */
5365 i.disp_encoding = disp_encoding_8bit;
5366 break;
41eb8e88
L
5367 case Prefix_Disp16:
5368 /* {disp16} */
5369 i.disp_encoding = disp_encoding_16bit;
5370 break;
5371 case Prefix_Disp32:
86fa6981
L
5372 /* {disp32} */
5373 i.disp_encoding = disp_encoding_32bit;
5374 break;
41eb8e88 5375 case Prefix_Load:
86fa6981
L
5376 /* {load} */
5377 i.dir_encoding = dir_encoding_load;
5378 break;
41eb8e88 5379 case Prefix_Store:
86fa6981
L
5380 /* {store} */
5381 i.dir_encoding = dir_encoding_store;
5382 break;
41eb8e88 5383 case Prefix_VEX:
42e04b36
L
5384 /* {vex} */
5385 i.vec_encoding = vex_encoding_vex;
86fa6981 5386 break;
41eb8e88 5387 case Prefix_VEX3:
86fa6981
L
5388 /* {vex3} */
5389 i.vec_encoding = vex_encoding_vex3;
5390 break;
41eb8e88 5391 case Prefix_EVEX:
86fa6981
L
5392 /* {evex} */
5393 i.vec_encoding = vex_encoding_evex;
5394 break;
41eb8e88 5395 case Prefix_REX:
6b6b6807 5396 /* {rex} */
5b7c81bd 5397 i.rex_encoding = true;
6b6b6807 5398 break;
41eb8e88 5399 case Prefix_NoOptimize:
b6f8c7c4 5400 /* {nooptimize} */
5b7c81bd 5401 i.no_optimize = true;
b6f8c7c4 5402 break;
86fa6981
L
5403 default:
5404 abort ();
5405 }
5406 }
5407 else
5408 {
5409 /* Add prefix, checking for repeated prefixes. */
4e9ac44a 5410 switch (add_prefix (current_templates->start->base_opcode))
86fa6981 5411 {
4e9ac44a
L
5412 case PREFIX_EXIST:
5413 return NULL;
5414 case PREFIX_DS:
d777820b 5415 if (current_templates->start->cpu_flags.bitfield.cpuibt)
4e9ac44a
L
5416 i.notrack_prefix = current_templates->start->name;
5417 break;
5418 case PREFIX_REP:
5419 if (current_templates->start->cpu_flags.bitfield.cpuhle)
5420 i.hle_prefix = current_templates->start->name;
5421 else if (current_templates->start->cpu_flags.bitfield.cpumpx)
5422 i.bnd_prefix = current_templates->start->name;
5423 else
5424 i.rep_prefix = current_templates->start->name;
5425 break;
5426 default:
5427 break;
86fa6981 5428 }
29b0f896
AM
5429 }
5430 /* Skip past PREFIX_SEPARATOR and reset token_start. */
5431 token_start = ++l;
5432 }
5433 else
5434 break;
5435 }
45288df1 5436
30a55f88 5437 if (!current_templates)
b6169b20 5438 {
07d5e953
JB
5439 /* Deprecated functionality (new code should use pseudo-prefixes instead):
5440 Check if we should swap operand or force 32bit displacement in
f8a5c266 5441 encoding. */
30a55f88 5442 if (mnem_p - 2 == dot_p && dot_p[1] == 's')
64c49ab3 5443 i.dir_encoding = dir_encoding_swap;
8d63c93e 5444 else if (mnem_p - 3 == dot_p
a501d77e
L
5445 && dot_p[1] == 'd'
5446 && dot_p[2] == '8')
5447 i.disp_encoding = disp_encoding_8bit;
8d63c93e 5448 else if (mnem_p - 4 == dot_p
f8a5c266
L
5449 && dot_p[1] == 'd'
5450 && dot_p[2] == '3'
5451 && dot_p[3] == '2')
a501d77e 5452 i.disp_encoding = disp_encoding_32bit;
30a55f88
L
5453 else
5454 goto check_suffix;
5455 mnem_p = dot_p;
5456 *dot_p = '\0';
629310ab 5457 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
b6169b20
L
5458 }
5459
29b0f896
AM
5460 if (!current_templates)
5461 {
dc1e8a47 5462 check_suffix:
1c529385 5463 if (mnem_p > mnemonic)
29b0f896 5464 {
1c529385
LH
5465 /* See if we can get a match by trimming off a suffix. */
5466 switch (mnem_p[-1])
29b0f896 5467 {
1c529385
LH
5468 case WORD_MNEM_SUFFIX:
5469 if (intel_syntax && (intel_float_operand (mnemonic) & 2))
29b0f896
AM
5470 i.suffix = SHORT_MNEM_SUFFIX;
5471 else
1c529385
LH
5472 /* Fall through. */
5473 case BYTE_MNEM_SUFFIX:
5474 case QWORD_MNEM_SUFFIX:
5475 i.suffix = mnem_p[-1];
29b0f896 5476 mnem_p[-1] = '\0';
fe0e921f
AM
5477 current_templates
5478 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5479 break;
5480 case SHORT_MNEM_SUFFIX:
5481 case LONG_MNEM_SUFFIX:
5482 if (!intel_syntax)
5483 {
5484 i.suffix = mnem_p[-1];
5485 mnem_p[-1] = '\0';
fe0e921f
AM
5486 current_templates
5487 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5488 }
5489 break;
5490
5491 /* Intel Syntax. */
5492 case 'd':
5493 if (intel_syntax)
5494 {
5495 if (intel_float_operand (mnemonic) == 1)
5496 i.suffix = SHORT_MNEM_SUFFIX;
5497 else
5498 i.suffix = LONG_MNEM_SUFFIX;
5499 mnem_p[-1] = '\0';
fe0e921f
AM
5500 current_templates
5501 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5502 }
5503 break;
29b0f896 5504 }
29b0f896 5505 }
1c529385 5506
29b0f896
AM
5507 if (!current_templates)
5508 {
5509 as_bad (_("no such instruction: `%s'"), token_start);
5510 return NULL;
5511 }
5512 }
252b5132 5513
0cfa3eb3
JB
5514 if (current_templates->start->opcode_modifier.jump == JUMP
5515 || current_templates->start->opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
5516 {
5517 /* Check for a branch hint. We allow ",pt" and ",pn" for
5518 predict taken and predict not taken respectively.
5519 I'm not sure that branch hints actually do anything on loop
5520 and jcxz insns (JumpByte) for current Pentium4 chips. They
5521 may work in the future and it doesn't hurt to accept them
5522 now. */
5523 if (l[0] == ',' && l[1] == 'p')
5524 {
5525 if (l[2] == 't')
5526 {
5527 if (!add_prefix (DS_PREFIX_OPCODE))
5528 return NULL;
5529 l += 3;
5530 }
5531 else if (l[2] == 'n')
5532 {
5533 if (!add_prefix (CS_PREFIX_OPCODE))
5534 return NULL;
5535 l += 3;
5536 }
5537 }
5538 }
5539 /* Any other comma loses. */
5540 if (*l == ',')
5541 {
5542 as_bad (_("invalid character %s in mnemonic"),
5543 output_invalid (*l));
5544 return NULL;
5545 }
252b5132 5546
29b0f896 5547 /* Check if instruction is supported on specified architecture. */
5c6af06e
JB
5548 supported = 0;
5549 for (t = current_templates->start; t < current_templates->end; ++t)
5550 {
c0f3af97
L
5551 supported |= cpu_flags_match (t);
5552 if (supported == CPU_FLAGS_PERFECT_MATCH)
d59a54c2 5553 return l;
29b0f896 5554 }
3629bb00 5555
548d0ee6
JB
5556 if (!(supported & CPU_FLAGS_64BIT_MATCH))
5557 as_bad (flag_code == CODE_64BIT
5558 ? _("`%s' is not supported in 64-bit mode")
5559 : _("`%s' is only supported in 64-bit mode"),
5560 current_templates->start->name);
5561 else
5562 as_bad (_("`%s' is not supported on `%s%s'"),
5563 current_templates->start->name,
5564 cpu_arch_name ? cpu_arch_name : default_arch,
5565 cpu_sub_arch_name ? cpu_sub_arch_name : "");
252b5132 5566
548d0ee6 5567 return NULL;
29b0f896 5568}
252b5132 5569
29b0f896 5570static char *
e3bb37b5 5571parse_operands (char *l, const char *mnemonic)
29b0f896
AM
5572{
5573 char *token_start;
3138f287 5574
29b0f896
AM
5575 /* 1 if operand is pending after ','. */
5576 unsigned int expecting_operand = 0;
252b5132 5577
29b0f896
AM
5578 while (*l != END_OF_INSN)
5579 {
e68c3d59
JB
5580 /* Non-zero if operand parens not balanced. */
5581 unsigned int paren_not_balanced = 0;
5582 /* True if inside double quotes. */
5583 bool in_quotes = false;
5584
29b0f896
AM
5585 /* Skip optional white space before operand. */
5586 if (is_space_char (*l))
5587 ++l;
d02603dc 5588 if (!is_operand_char (*l) && *l != END_OF_INSN && *l != '"')
29b0f896
AM
5589 {
5590 as_bad (_("invalid character %s before operand %d"),
5591 output_invalid (*l),
5592 i.operands + 1);
5593 return NULL;
5594 }
d02603dc 5595 token_start = l; /* After white space. */
e68c3d59 5596 while (in_quotes || paren_not_balanced || *l != ',')
29b0f896
AM
5597 {
5598 if (*l == END_OF_INSN)
5599 {
e68c3d59
JB
5600 if (in_quotes)
5601 {
5602 as_bad (_("unbalanced double quotes in operand %d."),
5603 i.operands + 1);
5604 return NULL;
5605 }
29b0f896
AM
5606 if (paren_not_balanced)
5607 {
98ff9f1c
JB
5608 know (!intel_syntax);
5609 as_bad (_("unbalanced parenthesis in operand %d."),
5610 i.operands + 1);
29b0f896
AM
5611 return NULL;
5612 }
5613 else
5614 break; /* we are done */
5615 }
e68c3d59
JB
5616 else if (*l == '\\' && l[1] == '"')
5617 ++l;
5618 else if (*l == '"')
5619 in_quotes = !in_quotes;
5620 else if (!in_quotes && !is_operand_char (*l) && !is_space_char (*l))
29b0f896
AM
5621 {
5622 as_bad (_("invalid character %s in operand %d"),
5623 output_invalid (*l),
5624 i.operands + 1);
5625 return NULL;
5626 }
e68c3d59 5627 if (!intel_syntax && !in_quotes)
29b0f896
AM
5628 {
5629 if (*l == '(')
5630 ++paren_not_balanced;
5631 if (*l == ')')
5632 --paren_not_balanced;
5633 }
29b0f896
AM
5634 l++;
5635 }
5636 if (l != token_start)
5637 { /* Yes, we've read in another operand. */
5638 unsigned int operand_ok;
5639 this_operand = i.operands++;
5640 if (i.operands > MAX_OPERANDS)
5641 {
5642 as_bad (_("spurious operands; (%d operands/instruction max)"),
5643 MAX_OPERANDS);
5644 return NULL;
5645 }
9d46ce34 5646 i.types[this_operand].bitfield.unspecified = 1;
29b0f896
AM
5647 /* Now parse operand adding info to 'i' as we go along. */
5648 END_STRING_AND_SAVE (l);
5649
1286ab78
L
5650 if (i.mem_operands > 1)
5651 {
5652 as_bad (_("too many memory references for `%s'"),
5653 mnemonic);
5654 return 0;
5655 }
5656
29b0f896
AM
5657 if (intel_syntax)
5658 operand_ok =
5659 i386_intel_operand (token_start,
5660 intel_float_operand (mnemonic));
5661 else
a7619375 5662 operand_ok = i386_att_operand (token_start);
29b0f896
AM
5663
5664 RESTORE_END_STRING (l);
5665 if (!operand_ok)
5666 return NULL;
5667 }
5668 else
5669 {
5670 if (expecting_operand)
5671 {
5672 expecting_operand_after_comma:
5673 as_bad (_("expecting operand after ','; got nothing"));
5674 return NULL;
5675 }
5676 if (*l == ',')
5677 {
5678 as_bad (_("expecting operand before ','; got nothing"));
5679 return NULL;
5680 }
5681 }
7f3f1ea2 5682
29b0f896
AM
5683 /* Now *l must be either ',' or END_OF_INSN. */
5684 if (*l == ',')
5685 {
5686 if (*++l == END_OF_INSN)
5687 {
5688 /* Just skip it, if it's \n complain. */
5689 goto expecting_operand_after_comma;
5690 }
5691 expecting_operand = 1;
5692 }
5693 }
5694 return l;
5695}
7f3f1ea2 5696
050dfa73 5697static void
783c187b 5698swap_2_operands (unsigned int xchg1, unsigned int xchg2)
050dfa73
MM
5699{
5700 union i386_op temp_op;
40fb9820 5701 i386_operand_type temp_type;
c48dadc9 5702 unsigned int temp_flags;
050dfa73 5703 enum bfd_reloc_code_real temp_reloc;
4eed87de 5704
050dfa73
MM
5705 temp_type = i.types[xchg2];
5706 i.types[xchg2] = i.types[xchg1];
5707 i.types[xchg1] = temp_type;
c48dadc9
JB
5708
5709 temp_flags = i.flags[xchg2];
5710 i.flags[xchg2] = i.flags[xchg1];
5711 i.flags[xchg1] = temp_flags;
5712
050dfa73
MM
5713 temp_op = i.op[xchg2];
5714 i.op[xchg2] = i.op[xchg1];
5715 i.op[xchg1] = temp_op;
c48dadc9 5716
050dfa73
MM
5717 temp_reloc = i.reloc[xchg2];
5718 i.reloc[xchg2] = i.reloc[xchg1];
5719 i.reloc[xchg1] = temp_reloc;
43234a1e 5720
6225c532 5721 if (i.mask.reg)
43234a1e 5722 {
6225c532
JB
5723 if (i.mask.operand == xchg1)
5724 i.mask.operand = xchg2;
5725 else if (i.mask.operand == xchg2)
5726 i.mask.operand = xchg1;
43234a1e 5727 }
a5748e0d 5728 if (i.broadcast.type || i.broadcast.bytes)
43234a1e 5729 {
5273a3cd
JB
5730 if (i.broadcast.operand == xchg1)
5731 i.broadcast.operand = xchg2;
5732 else if (i.broadcast.operand == xchg2)
5733 i.broadcast.operand = xchg1;
43234a1e 5734 }
050dfa73
MM
5735}
5736
29b0f896 5737static void
e3bb37b5 5738swap_operands (void)
29b0f896 5739{
b7c61d9a 5740 switch (i.operands)
050dfa73 5741 {
c0f3af97 5742 case 5:
b7c61d9a 5743 case 4:
4d456e3d 5744 swap_2_operands (1, i.operands - 2);
1a0670f3 5745 /* Fall through. */
b7c61d9a
L
5746 case 3:
5747 case 2:
4d456e3d 5748 swap_2_operands (0, i.operands - 1);
b7c61d9a
L
5749 break;
5750 default:
5751 abort ();
29b0f896 5752 }
29b0f896
AM
5753
5754 if (i.mem_operands == 2)
5755 {
5e042380 5756 const reg_entry *temp_seg;
29b0f896
AM
5757 temp_seg = i.seg[0];
5758 i.seg[0] = i.seg[1];
5759 i.seg[1] = temp_seg;
5760 }
5761}
252b5132 5762
29b0f896
AM
5763/* Try to ensure constant immediates are represented in the smallest
5764 opcode possible. */
5765static void
e3bb37b5 5766optimize_imm (void)
29b0f896
AM
5767{
5768 char guess_suffix = 0;
5769 int op;
252b5132 5770
29b0f896
AM
5771 if (i.suffix)
5772 guess_suffix = i.suffix;
5773 else if (i.reg_operands)
5774 {
5775 /* Figure out a suffix from the last register operand specified.
75e5731b
JB
5776 We can't do this properly yet, i.e. excluding special register
5777 instances, but the following works for instructions with
5778 immediates. In any case, we can't set i.suffix yet. */
29b0f896 5779 for (op = i.operands; --op >= 0;)
bab6aec1
JB
5780 if (i.types[op].bitfield.class != Reg)
5781 continue;
5782 else if (i.types[op].bitfield.byte)
7ab9ffdd 5783 {
40fb9820
L
5784 guess_suffix = BYTE_MNEM_SUFFIX;
5785 break;
5786 }
bab6aec1 5787 else if (i.types[op].bitfield.word)
252b5132 5788 {
40fb9820
L
5789 guess_suffix = WORD_MNEM_SUFFIX;
5790 break;
5791 }
bab6aec1 5792 else if (i.types[op].bitfield.dword)
40fb9820
L
5793 {
5794 guess_suffix = LONG_MNEM_SUFFIX;
5795 break;
5796 }
bab6aec1 5797 else if (i.types[op].bitfield.qword)
40fb9820
L
5798 {
5799 guess_suffix = QWORD_MNEM_SUFFIX;
29b0f896 5800 break;
252b5132 5801 }
29b0f896
AM
5802 }
5803 else if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
5804 guess_suffix = WORD_MNEM_SUFFIX;
5805
5806 for (op = i.operands; --op >= 0;)
40fb9820 5807 if (operand_type_check (i.types[op], imm))
29b0f896
AM
5808 {
5809 switch (i.op[op].imms->X_op)
252b5132 5810 {
29b0f896
AM
5811 case O_constant:
5812 /* If a suffix is given, this operand may be shortened. */
5813 switch (guess_suffix)
252b5132 5814 {
29b0f896 5815 case LONG_MNEM_SUFFIX:
40fb9820
L
5816 i.types[op].bitfield.imm32 = 1;
5817 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
5818 break;
5819 case WORD_MNEM_SUFFIX:
40fb9820
L
5820 i.types[op].bitfield.imm16 = 1;
5821 i.types[op].bitfield.imm32 = 1;
5822 i.types[op].bitfield.imm32s = 1;
5823 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
5824 break;
5825 case BYTE_MNEM_SUFFIX:
40fb9820
L
5826 i.types[op].bitfield.imm8 = 1;
5827 i.types[op].bitfield.imm8s = 1;
5828 i.types[op].bitfield.imm16 = 1;
5829 i.types[op].bitfield.imm32 = 1;
5830 i.types[op].bitfield.imm32s = 1;
5831 i.types[op].bitfield.imm64 = 1;
29b0f896 5832 break;
252b5132 5833 }
252b5132 5834
29b0f896
AM
5835 /* If this operand is at most 16 bits, convert it
5836 to a signed 16 bit number before trying to see
5837 whether it will fit in an even smaller size.
5838 This allows a 16-bit operand such as $0xffe0 to
5839 be recognised as within Imm8S range. */
40fb9820 5840 if ((i.types[op].bitfield.imm16)
7e96fb68 5841 && fits_in_unsigned_word (i.op[op].imms->X_add_number))
252b5132 5842 {
87ed972d
JB
5843 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
5844 ^ 0x8000) - 0x8000);
29b0f896 5845 }
a28def75
L
5846#ifdef BFD64
5847 /* Store 32-bit immediate in 64-bit for 64-bit BFD. */
40fb9820 5848 if ((i.types[op].bitfield.imm32)
7e96fb68 5849 && fits_in_unsigned_long (i.op[op].imms->X_add_number))
29b0f896
AM
5850 {
5851 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
5852 ^ ((offsetT) 1 << 31))
5853 - ((offsetT) 1 << 31));
5854 }
a28def75 5855#endif
40fb9820 5856 i.types[op]
c6fb90c8
L
5857 = operand_type_or (i.types[op],
5858 smallest_imm_type (i.op[op].imms->X_add_number));
252b5132 5859
29b0f896
AM
5860 /* We must avoid matching of Imm32 templates when 64bit
5861 only immediate is available. */
5862 if (guess_suffix == QWORD_MNEM_SUFFIX)
40fb9820 5863 i.types[op].bitfield.imm32 = 0;
29b0f896 5864 break;
252b5132 5865
29b0f896
AM
5866 case O_absent:
5867 case O_register:
5868 abort ();
5869
5870 /* Symbols and expressions. */
5871 default:
9cd96992
JB
5872 /* Convert symbolic operand to proper sizes for matching, but don't
5873 prevent matching a set of insns that only supports sizes other
5874 than those matching the insn suffix. */
5875 {
40fb9820 5876 i386_operand_type mask, allowed;
87ed972d 5877 const insn_template *t = current_templates->start;
9cd96992 5878
0dfbf9d7 5879 operand_type_set (&mask, 0);
9cd96992
JB
5880 switch (guess_suffix)
5881 {
5882 case QWORD_MNEM_SUFFIX:
40fb9820
L
5883 mask.bitfield.imm64 = 1;
5884 mask.bitfield.imm32s = 1;
9cd96992
JB
5885 break;
5886 case LONG_MNEM_SUFFIX:
40fb9820 5887 mask.bitfield.imm32 = 1;
9cd96992
JB
5888 break;
5889 case WORD_MNEM_SUFFIX:
40fb9820 5890 mask.bitfield.imm16 = 1;
9cd96992
JB
5891 break;
5892 case BYTE_MNEM_SUFFIX:
40fb9820 5893 mask.bitfield.imm8 = 1;
9cd96992
JB
5894 break;
5895 default:
9cd96992
JB
5896 break;
5897 }
8f0212ac
JB
5898
5899 allowed = operand_type_and (t->operand_types[op], mask);
5900 while (++t < current_templates->end)
5901 {
5902 allowed = operand_type_or (allowed, t->operand_types[op]);
5903 allowed = operand_type_and (allowed, mask);
5904 }
5905
0dfbf9d7 5906 if (!operand_type_all_zero (&allowed))
c6fb90c8 5907 i.types[op] = operand_type_and (i.types[op], mask);
9cd96992 5908 }
29b0f896 5909 break;
252b5132 5910 }
29b0f896
AM
5911 }
5912}
47926f60 5913
29b0f896
AM
5914/* Try to use the smallest displacement type too. */
5915static void
e3bb37b5 5916optimize_disp (void)
29b0f896
AM
5917{
5918 int op;
3e73aa7c 5919
29b0f896 5920 for (op = i.operands; --op >= 0;)
40fb9820 5921 if (operand_type_check (i.types[op], disp))
252b5132 5922 {
b300c311 5923 if (i.op[op].disps->X_op == O_constant)
252b5132 5924 {
91d6fa6a 5925 offsetT op_disp = i.op[op].disps->X_add_number;
29b0f896 5926
91d6fa6a 5927 if (!op_disp && i.types[op].bitfield.baseindex)
b300c311 5928 {
2f2be86b
JB
5929 i.types[op] = operand_type_and_not (i.types[op], anydisp);
5930 i.op[op].disps = NULL;
b300c311 5931 i.disp_operands--;
f185acdd
JB
5932 continue;
5933 }
5934
5935 if (i.types[op].bitfield.disp16
cd613c1f 5936 && fits_in_unsigned_word (op_disp))
f185acdd
JB
5937 {
5938 /* If this operand is at most 16 bits, convert
5939 to a signed 16 bit number and don't use 64bit
5940 displacement. */
5941 op_disp = ((op_disp ^ 0x8000) - 0x8000);
5942 i.types[op].bitfield.disp64 = 0;
b300c311 5943 }
f185acdd 5944
28a167a4 5945#ifdef BFD64
a50187b2 5946 /* Optimize 64-bit displacement to 32-bit for 64-bit BFD. */
a775efc8
JB
5947 if ((flag_code != CODE_64BIT
5948 ? i.types[op].bitfield.disp32
5949 : want_disp32 (current_templates->start)
5950 && (!current_templates->start->opcode_modifier.jump
5951 || i.jumpabsolute || i.types[op].bitfield.baseindex))
a50187b2 5952 && fits_in_unsigned_long (op_disp))
b300c311 5953 {
a50187b2
JB
5954 /* If this operand is at most 32 bits, convert
5955 to a signed 32 bit number and don't use 64bit
5956 displacement. */
5957 op_disp = (op_disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
5958 i.types[op].bitfield.disp64 = 0;
5959 i.types[op].bitfield.disp32 = 1;
5960 }
28a167a4 5961
a50187b2
JB
5962 if (flag_code == CODE_64BIT && fits_in_signed_long (op_disp))
5963 {
5964 i.types[op].bitfield.disp64 = 0;
a775efc8 5965 i.types[op].bitfield.disp32 = 1;
b300c311 5966 }
28a167a4 5967#endif
40fb9820 5968 if ((i.types[op].bitfield.disp32
40fb9820 5969 || i.types[op].bitfield.disp16)
b5014f7a 5970 && fits_in_disp8 (op_disp))
40fb9820 5971 i.types[op].bitfield.disp8 = 1;
77c59789
JB
5972
5973 i.op[op].disps->X_add_number = op_disp;
252b5132 5974 }
67a4f2b7
AO
5975 else if (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
5976 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL)
5977 {
5978 fix_new_exp (frag_now, frag_more (0) - frag_now->fr_literal, 0,
5979 i.op[op].disps, 0, i.reloc[op]);
2f2be86b 5980 i.types[op] = operand_type_and_not (i.types[op], anydisp);
67a4f2b7
AO
5981 }
5982 else
b300c311 5983 /* We only support 64bit displacement on constants. */
40fb9820 5984 i.types[op].bitfield.disp64 = 0;
252b5132 5985 }
29b0f896
AM
5986}
5987
4a1b91ea
L
5988/* Return 1 if there is a match in broadcast bytes between operand
5989 GIVEN and instruction template T. */
5990
5991static INLINE int
5992match_broadcast_size (const insn_template *t, unsigned int given)
5993{
5994 return ((t->opcode_modifier.broadcast == BYTE_BROADCAST
5995 && i.types[given].bitfield.byte)
5996 || (t->opcode_modifier.broadcast == WORD_BROADCAST
5997 && i.types[given].bitfield.word)
5998 || (t->opcode_modifier.broadcast == DWORD_BROADCAST
5999 && i.types[given].bitfield.dword)
6000 || (t->opcode_modifier.broadcast == QWORD_BROADCAST
6001 && i.types[given].bitfield.qword));
6002}
6003
6c30d220
L
6004/* Check if operands are valid for the instruction. */
6005
6006static int
6007check_VecOperands (const insn_template *t)
6008{
43234a1e 6009 unsigned int op;
e2195274 6010 i386_cpu_flags cpu;
e2195274
JB
6011
6012 /* Templates allowing for ZMMword as well as YMMword and/or XMMword for
6013 any one operand are implicity requiring AVX512VL support if the actual
6014 operand size is YMMword or XMMword. Since this function runs after
6015 template matching, there's no need to check for YMMword/XMMword in
6016 the template. */
6017 cpu = cpu_flags_and (t->cpu_flags, avx512);
6018 if (!cpu_flags_all_zero (&cpu)
6019 && !t->cpu_flags.bitfield.cpuavx512vl
6020 && !cpu_arch_flags.bitfield.cpuavx512vl)
6021 {
6022 for (op = 0; op < t->operands; ++op)
6023 {
6024 if (t->operand_types[op].bitfield.zmmword
6025 && (i.types[op].bitfield.ymmword
6026 || i.types[op].bitfield.xmmword))
6027 {
6028 i.error = unsupported;
6029 return 1;
6030 }
6031 }
6032 }
43234a1e 6033
22c36940
JB
6034 /* Somewhat similarly, templates specifying both AVX and AVX2 are
6035 requiring AVX2 support if the actual operand size is YMMword. */
6036 if (t->cpu_flags.bitfield.cpuavx
6037 && t->cpu_flags.bitfield.cpuavx2
6038 && !cpu_arch_flags.bitfield.cpuavx2)
6039 {
6040 for (op = 0; op < t->operands; ++op)
6041 {
6042 if (t->operand_types[op].bitfield.xmmword
6043 && i.types[op].bitfield.ymmword)
6044 {
6045 i.error = unsupported;
6046 return 1;
6047 }
6048 }
6049 }
6050
6c30d220 6051 /* Without VSIB byte, we can't have a vector register for index. */
63112cd6 6052 if (!t->opcode_modifier.sib
6c30d220 6053 && i.index_reg
1b54b8d7
JB
6054 && (i.index_reg->reg_type.bitfield.xmmword
6055 || i.index_reg->reg_type.bitfield.ymmword
6056 || i.index_reg->reg_type.bitfield.zmmword))
6c30d220
L
6057 {
6058 i.error = unsupported_vector_index_register;
6059 return 1;
6060 }
6061
ad8ecc81 6062 /* Check if default mask is allowed. */
255571cd 6063 if (t->opcode_modifier.operandconstraint == NO_DEFAULT_MASK
6225c532 6064 && (!i.mask.reg || i.mask.reg->reg_num == 0))
ad8ecc81
MZ
6065 {
6066 i.error = no_default_mask;
6067 return 1;
6068 }
6069
7bab8ab5
JB
6070 /* For VSIB byte, we need a vector register for index, and all vector
6071 registers must be distinct. */
260cd341 6072 if (t->opcode_modifier.sib && t->opcode_modifier.sib != SIBMEM)
7bab8ab5
JB
6073 {
6074 if (!i.index_reg
63112cd6 6075 || !((t->opcode_modifier.sib == VECSIB128
1b54b8d7 6076 && i.index_reg->reg_type.bitfield.xmmword)
63112cd6 6077 || (t->opcode_modifier.sib == VECSIB256
1b54b8d7 6078 && i.index_reg->reg_type.bitfield.ymmword)
63112cd6 6079 || (t->opcode_modifier.sib == VECSIB512
1b54b8d7 6080 && i.index_reg->reg_type.bitfield.zmmword)))
7bab8ab5
JB
6081 {
6082 i.error = invalid_vsib_address;
6083 return 1;
6084 }
6085
6225c532
JB
6086 gas_assert (i.reg_operands == 2 || i.mask.reg);
6087 if (i.reg_operands == 2 && !i.mask.reg)
43234a1e 6088 {
3528c362 6089 gas_assert (i.types[0].bitfield.class == RegSIMD);
1b54b8d7
JB
6090 gas_assert (i.types[0].bitfield.xmmword
6091 || i.types[0].bitfield.ymmword);
3528c362 6092 gas_assert (i.types[2].bitfield.class == RegSIMD);
1b54b8d7
JB
6093 gas_assert (i.types[2].bitfield.xmmword
6094 || i.types[2].bitfield.ymmword);
43234a1e
L
6095 if (operand_check == check_none)
6096 return 0;
6097 if (register_number (i.op[0].regs)
6098 != register_number (i.index_reg)
6099 && register_number (i.op[2].regs)
6100 != register_number (i.index_reg)
6101 && register_number (i.op[0].regs)
6102 != register_number (i.op[2].regs))
6103 return 0;
6104 if (operand_check == check_error)
6105 {
6106 i.error = invalid_vector_register_set;
6107 return 1;
6108 }
6109 as_warn (_("mask, index, and destination registers should be distinct"));
6110 }
6225c532 6111 else if (i.reg_operands == 1 && i.mask.reg)
8444f82a 6112 {
3528c362 6113 if (i.types[1].bitfield.class == RegSIMD
1b54b8d7
JB
6114 && (i.types[1].bitfield.xmmword
6115 || i.types[1].bitfield.ymmword
6116 || i.types[1].bitfield.zmmword)
8444f82a
MZ
6117 && (register_number (i.op[1].regs)
6118 == register_number (i.index_reg)))
6119 {
6120 if (operand_check == check_error)
6121 {
6122 i.error = invalid_vector_register_set;
6123 return 1;
6124 }
6125 if (operand_check != check_none)
6126 as_warn (_("index and destination registers should be distinct"));
6127 }
6128 }
43234a1e 6129 }
7bab8ab5 6130
fc141319
L
6131 /* For AMX instructions with 3 TMM register operands, all operands
6132 must be distinct. */
6133 if (i.reg_operands == 3
6134 && t->operand_types[0].bitfield.tmmword
6135 && (i.op[0].regs == i.op[1].regs
6136 || i.op[0].regs == i.op[2].regs
6137 || i.op[1].regs == i.op[2].regs))
6138 {
6139 i.error = invalid_tmm_register_set;
6140 return 1;
260cd341
LC
6141 }
6142
0cc78721
CL
6143 /* For some special instructions require that destination must be distinct
6144 from source registers. */
255571cd 6145 if (t->opcode_modifier.operandconstraint == DISTINCT_DEST)
0cc78721
CL
6146 {
6147 unsigned int dest_reg = i.operands - 1;
6148
6149 know (i.operands >= 3);
6150
6151 /* #UD if dest_reg == src1_reg or dest_reg == src2_reg. */
6152 if (i.op[dest_reg - 1].regs == i.op[dest_reg].regs
6153 || (i.reg_operands > 2
6154 && i.op[dest_reg - 2].regs == i.op[dest_reg].regs))
6155 {
6156 i.error = invalid_dest_and_src_register_set;
6157 return 1;
6158 }
6159 }
6160
43234a1e
L
6161 /* Check if broadcast is supported by the instruction and is applied
6162 to the memory operand. */
a5748e0d 6163 if (i.broadcast.type || i.broadcast.bytes)
43234a1e 6164 {
8e6e0792 6165 i386_operand_type type, overlap;
43234a1e
L
6166
6167 /* Check if specified broadcast is supported in this instruction,
4a1b91ea 6168 and its broadcast bytes match the memory operand. */
5273a3cd 6169 op = i.broadcast.operand;
8e6e0792 6170 if (!t->opcode_modifier.broadcast
c48dadc9 6171 || !(i.flags[op] & Operand_Mem)
c39e5b26 6172 || (!i.types[op].bitfield.unspecified
4a1b91ea 6173 && !match_broadcast_size (t, op)))
43234a1e
L
6174 {
6175 bad_broadcast:
6176 i.error = unsupported_broadcast;
6177 return 1;
6178 }
8e6e0792 6179
a5748e0d
JB
6180 if (i.broadcast.type)
6181 i.broadcast.bytes = ((1 << (t->opcode_modifier.broadcast - 1))
6182 * i.broadcast.type);
8e6e0792 6183 operand_type_set (&type, 0);
a5748e0d 6184 switch (get_broadcast_bytes (t, false))
8e6e0792 6185 {
4a1b91ea
L
6186 case 2:
6187 type.bitfield.word = 1;
6188 break;
6189 case 4:
6190 type.bitfield.dword = 1;
6191 break;
8e6e0792
JB
6192 case 8:
6193 type.bitfield.qword = 1;
6194 break;
6195 case 16:
6196 type.bitfield.xmmword = 1;
6197 break;
6198 case 32:
6199 type.bitfield.ymmword = 1;
6200 break;
6201 case 64:
6202 type.bitfield.zmmword = 1;
6203 break;
6204 default:
6205 goto bad_broadcast;
6206 }
6207
6208 overlap = operand_type_and (type, t->operand_types[op]);
bc49bfd8
JB
6209 if (t->operand_types[op].bitfield.class == RegSIMD
6210 && t->operand_types[op].bitfield.byte
6211 + t->operand_types[op].bitfield.word
6212 + t->operand_types[op].bitfield.dword
6213 + t->operand_types[op].bitfield.qword > 1)
6214 {
6215 overlap.bitfield.xmmword = 0;
6216 overlap.bitfield.ymmword = 0;
6217 overlap.bitfield.zmmword = 0;
6218 }
8e6e0792
JB
6219 if (operand_type_all_zero (&overlap))
6220 goto bad_broadcast;
6221
6222 if (t->opcode_modifier.checkregsize)
6223 {
6224 unsigned int j;
6225
e2195274 6226 type.bitfield.baseindex = 1;
8e6e0792
JB
6227 for (j = 0; j < i.operands; ++j)
6228 {
6229 if (j != op
6230 && !operand_type_register_match(i.types[j],
6231 t->operand_types[j],
6232 type,
6233 t->operand_types[op]))
6234 goto bad_broadcast;
6235 }
6236 }
43234a1e
L
6237 }
6238 /* If broadcast is supported in this instruction, we need to check if
6239 operand of one-element size isn't specified without broadcast. */
6240 else if (t->opcode_modifier.broadcast && i.mem_operands)
6241 {
6242 /* Find memory operand. */
6243 for (op = 0; op < i.operands; op++)
8dc0818e 6244 if (i.flags[op] & Operand_Mem)
43234a1e
L
6245 break;
6246 gas_assert (op < i.operands);
6247 /* Check size of the memory operand. */
4a1b91ea 6248 if (match_broadcast_size (t, op))
43234a1e
L
6249 {
6250 i.error = broadcast_needed;
6251 return 1;
6252 }
6253 }
c39e5b26
JB
6254 else
6255 op = MAX_OPERANDS - 1; /* Avoid uninitialized variable warning. */
43234a1e
L
6256
6257 /* Check if requested masking is supported. */
6225c532 6258 if (i.mask.reg)
43234a1e 6259 {
ae2387fe
JB
6260 switch (t->opcode_modifier.masking)
6261 {
6262 case BOTH_MASKING:
6263 break;
6264 case MERGING_MASKING:
6225c532 6265 if (i.mask.zeroing)
ae2387fe
JB
6266 {
6267 case 0:
6268 i.error = unsupported_masking;
6269 return 1;
6270 }
6271 break;
6272 case DYNAMIC_MASKING:
6273 /* Memory destinations allow only merging masking. */
6225c532 6274 if (i.mask.zeroing && i.mem_operands)
ae2387fe
JB
6275 {
6276 /* Find memory operand. */
6277 for (op = 0; op < i.operands; op++)
c48dadc9 6278 if (i.flags[op] & Operand_Mem)
ae2387fe
JB
6279 break;
6280 gas_assert (op < i.operands);
6281 if (op == i.operands - 1)
6282 {
6283 i.error = unsupported_masking;
6284 return 1;
6285 }
6286 }
6287 break;
6288 default:
6289 abort ();
6290 }
43234a1e
L
6291 }
6292
6293 /* Check if masking is applied to dest operand. */
6225c532 6294 if (i.mask.reg && (i.mask.operand != i.operands - 1))
43234a1e
L
6295 {
6296 i.error = mask_not_on_destination;
6297 return 1;
6298 }
6299
43234a1e 6300 /* Check RC/SAE. */
ca5312a2 6301 if (i.rounding.type != rc_none)
43234a1e 6302 {
a80195f1 6303 if (!t->opcode_modifier.sae
cf665fee
JB
6304 || ((i.rounding.type != saeonly) != t->opcode_modifier.staticrounding)
6305 || i.mem_operands)
43234a1e
L
6306 {
6307 i.error = unsupported_rc_sae;
6308 return 1;
6309 }
cf665fee
JB
6310
6311 /* Non-EVEX.LIG forms need to have a ZMM register as at least one
6312 operand. */
6313 if (t->opcode_modifier.evex != EVEXLIG)
7bab8ab5 6314 {
cf665fee
JB
6315 for (op = 0; op < t->operands; ++op)
6316 if (i.types[op].bitfield.zmmword)
6317 break;
6318 if (op >= t->operands)
6319 {
6320 i.error = operand_size_mismatch;
6321 return 1;
6322 }
7bab8ab5 6323 }
6c30d220
L
6324 }
6325
da4977e0
JB
6326 /* Check the special Imm4 cases; must be the first operand. */
6327 if (t->cpu_flags.bitfield.cpuxop && t->operands == 5)
6328 {
6329 if (i.op[0].imms->X_op != O_constant
6330 || !fits_in_imm4 (i.op[0].imms->X_add_number))
6331 {
6332 i.error = bad_imm4;
6333 return 1;
6334 }
6335
6336 /* Turn off Imm<N> so that update_imm won't complain. */
6337 operand_type_set (&i.types[0], 0);
6338 }
6339
43234a1e 6340 /* Check vector Disp8 operand. */
b5014f7a 6341 if (t->opcode_modifier.disp8memshift
1a42a9fe 6342 && i.disp_encoding <= disp_encoding_8bit)
43234a1e 6343 {
a5748e0d 6344 if (i.broadcast.bytes)
4a1b91ea 6345 i.memshift = t->opcode_modifier.broadcast - 1;
7091c612 6346 else if (t->opcode_modifier.disp8memshift != DISP8_SHIFT_VL)
43234a1e 6347 i.memshift = t->opcode_modifier.disp8memshift;
7091c612
JB
6348 else
6349 {
125ff819 6350 const i386_operand_type *type = NULL, *fallback = NULL;
7091c612
JB
6351
6352 i.memshift = 0;
6353 for (op = 0; op < i.operands; op++)
8dc0818e 6354 if (i.flags[op] & Operand_Mem)
7091c612 6355 {
4174bfff
JB
6356 if (t->opcode_modifier.evex == EVEXLIG)
6357 i.memshift = 2 + (i.suffix == QWORD_MNEM_SUFFIX);
6358 else if (t->operand_types[op].bitfield.xmmword
6359 + t->operand_types[op].bitfield.ymmword
6360 + t->operand_types[op].bitfield.zmmword <= 1)
7091c612
JB
6361 type = &t->operand_types[op];
6362 else if (!i.types[op].bitfield.unspecified)
6363 type = &i.types[op];
125ff819
JB
6364 else /* Ambiguities get resolved elsewhere. */
6365 fallback = &t->operand_types[op];
7091c612 6366 }
3528c362 6367 else if (i.types[op].bitfield.class == RegSIMD
4174bfff 6368 && t->opcode_modifier.evex != EVEXLIG)
7091c612
JB
6369 {
6370 if (i.types[op].bitfield.zmmword)
6371 i.memshift = 6;
6372 else if (i.types[op].bitfield.ymmword && i.memshift < 5)
6373 i.memshift = 5;
6374 else if (i.types[op].bitfield.xmmword && i.memshift < 4)
6375 i.memshift = 4;
6376 }
6377
125ff819
JB
6378 if (!type && !i.memshift)
6379 type = fallback;
7091c612
JB
6380 if (type)
6381 {
6382 if (type->bitfield.zmmword)
6383 i.memshift = 6;
6384 else if (type->bitfield.ymmword)
6385 i.memshift = 5;
6386 else if (type->bitfield.xmmword)
6387 i.memshift = 4;
6388 }
6389
6390 /* For the check in fits_in_disp8(). */
6391 if (i.memshift == 0)
6392 i.memshift = -1;
6393 }
43234a1e
L
6394
6395 for (op = 0; op < i.operands; op++)
6396 if (operand_type_check (i.types[op], disp)
6397 && i.op[op].disps->X_op == O_constant)
6398 {
b5014f7a 6399 if (fits_in_disp8 (i.op[op].disps->X_add_number))
43234a1e 6400 {
b5014f7a
JB
6401 i.types[op].bitfield.disp8 = 1;
6402 return 0;
43234a1e 6403 }
b5014f7a 6404 i.types[op].bitfield.disp8 = 0;
43234a1e
L
6405 }
6406 }
b5014f7a
JB
6407
6408 i.memshift = 0;
43234a1e 6409
6c30d220
L
6410 return 0;
6411}
6412
da4977e0 6413/* Check if encoding requirements are met by the instruction. */
a683cc34
SP
6414
6415static int
da4977e0 6416VEX_check_encoding (const insn_template *t)
a683cc34 6417{
da4977e0
JB
6418 if (i.vec_encoding == vex_encoding_error)
6419 {
6420 i.error = unsupported;
6421 return 1;
6422 }
6423
86fa6981 6424 if (i.vec_encoding == vex_encoding_evex)
43234a1e 6425 {
86fa6981 6426 /* This instruction must be encoded with EVEX prefix. */
e771e7c9 6427 if (!is_evex_encoding (t))
86fa6981
L
6428 {
6429 i.error = unsupported;
6430 return 1;
6431 }
6432 return 0;
43234a1e
L
6433 }
6434
a683cc34 6435 if (!t->opcode_modifier.vex)
86fa6981
L
6436 {
6437 /* This instruction template doesn't have VEX prefix. */
6438 if (i.vec_encoding != vex_encoding_default)
6439 {
6440 i.error = unsupported;
6441 return 1;
6442 }
6443 return 0;
6444 }
a683cc34 6445
a683cc34
SP
6446 return 0;
6447}
6448
7b94647a
JB
6449/* Helper function for the progress() macro in match_template(). */
6450static INLINE enum i386_error progress (enum i386_error new,
6451 enum i386_error last,
6452 unsigned int line, unsigned int *line_p)
6453{
6454 if (line <= *line_p)
6455 return last;
6456 *line_p = line;
6457 return new;
6458}
6459
d3ce72d0 6460static const insn_template *
83b16ac6 6461match_template (char mnem_suffix)
29b0f896
AM
6462{
6463 /* Points to template once we've found it. */
d3ce72d0 6464 const insn_template *t;
40fb9820 6465 i386_operand_type overlap0, overlap1, overlap2, overlap3;
c0f3af97 6466 i386_operand_type overlap4;
29b0f896 6467 unsigned int found_reverse_match;
dc2be329 6468 i386_opcode_modifier suffix_check;
40fb9820 6469 i386_operand_type operand_types [MAX_OPERANDS];
539e75ad 6470 int addr_prefix_disp;
7b94647a
JB
6471 unsigned int j, size_match, check_register, errline = __LINE__;
6472 enum i386_error specific_error = number_of_operands_mismatch;
6473#define progress(err) progress (err, specific_error, __LINE__, &errline)
29b0f896 6474
c0f3af97
L
6475#if MAX_OPERANDS != 5
6476# error "MAX_OPERANDS must be 5."
f48ff2ae
L
6477#endif
6478
29b0f896 6479 found_reverse_match = 0;
539e75ad 6480 addr_prefix_disp = -1;
40fb9820 6481
dc2be329 6482 /* Prepare for mnemonic suffix check. */
40fb9820 6483 memset (&suffix_check, 0, sizeof (suffix_check));
dc2be329
L
6484 switch (mnem_suffix)
6485 {
6486 case BYTE_MNEM_SUFFIX:
6487 suffix_check.no_bsuf = 1;
6488 break;
6489 case WORD_MNEM_SUFFIX:
6490 suffix_check.no_wsuf = 1;
6491 break;
6492 case SHORT_MNEM_SUFFIX:
6493 suffix_check.no_ssuf = 1;
6494 break;
6495 case LONG_MNEM_SUFFIX:
6496 suffix_check.no_lsuf = 1;
6497 break;
6498 case QWORD_MNEM_SUFFIX:
6499 suffix_check.no_qsuf = 1;
6500 break;
6501 default:
6502 /* NB: In Intel syntax, normally we can check for memory operand
6503 size when there is no mnemonic suffix. But jmp and call have
6504 2 different encodings with Dword memory operand size, one with
6505 No_ldSuf and the other without. i.suffix is set to
6506 LONG_DOUBLE_MNEM_SUFFIX to skip the one with No_ldSuf. */
6507 if (i.suffix == LONG_DOUBLE_MNEM_SUFFIX)
6508 suffix_check.no_ldsuf = 1;
83b16ac6
JB
6509 }
6510
45aa61fe 6511 for (t = current_templates->start; t < current_templates->end; t++)
29b0f896 6512 {
539e75ad 6513 addr_prefix_disp = -1;
dbbc8b7e 6514 found_reverse_match = 0;
539e75ad 6515
7b94647a 6516 /* Must have right number of operands. */
29b0f896
AM
6517 if (i.operands != t->operands)
6518 continue;
6519
50aecf8c 6520 /* Check processor support. */
7b94647a 6521 specific_error = progress (unsupported);
45a4bb20 6522 if (cpu_flags_match (t) != CPU_FLAGS_PERFECT_MATCH)
50aecf8c
L
6523 continue;
6524
e1d4d893 6525 /* Check AT&T mnemonic. */
7b94647a 6526 specific_error = progress (unsupported_with_intel_mnemonic);
e1d4d893 6527 if (intel_mnemonic && t->opcode_modifier.attmnemonic)
1efbbeb4
L
6528 continue;
6529
4b5aaf5f 6530 /* Check AT&T/Intel syntax. */
7b94647a 6531 specific_error = progress (unsupported_syntax);
5c07affc 6532 if ((intel_syntax && t->opcode_modifier.attsyntax)
4b5aaf5f 6533 || (!intel_syntax && t->opcode_modifier.intelsyntax))
1efbbeb4
L
6534 continue;
6535
4b5aaf5f
L
6536 /* Check Intel64/AMD64 ISA. */
6537 switch (isa64)
6538 {
6539 default:
6540 /* Default: Don't accept Intel64. */
6541 if (t->opcode_modifier.isa64 == INTEL64)
6542 continue;
6543 break;
6544 case amd64:
6545 /* -mamd64: Don't accept Intel64 and Intel64 only. */
6546 if (t->opcode_modifier.isa64 >= INTEL64)
6547 continue;
6548 break;
6549 case intel64:
6550 /* -mintel64: Don't accept AMD64. */
5990e377 6551 if (t->opcode_modifier.isa64 == AMD64 && flag_code == CODE_64BIT)
4b5aaf5f
L
6552 continue;
6553 break;
6554 }
6555
dc2be329 6556 /* Check the suffix. */
7b94647a 6557 specific_error = progress (invalid_instruction_suffix);
dc2be329
L
6558 if ((t->opcode_modifier.no_bsuf && suffix_check.no_bsuf)
6559 || (t->opcode_modifier.no_wsuf && suffix_check.no_wsuf)
6560 || (t->opcode_modifier.no_lsuf && suffix_check.no_lsuf)
6561 || (t->opcode_modifier.no_ssuf && suffix_check.no_ssuf)
6562 || (t->opcode_modifier.no_qsuf && suffix_check.no_qsuf)
6563 || (t->opcode_modifier.no_ldsuf && suffix_check.no_ldsuf))
83b16ac6 6564 continue;
29b0f896 6565
7b94647a 6566 specific_error = progress (operand_size_mismatch);
3ac21baa
JB
6567 size_match = operand_size_match (t);
6568 if (!size_match)
7d5e4556 6569 continue;
539e75ad 6570
6f2f06be
JB
6571 /* This is intentionally not
6572
0cfa3eb3 6573 if (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE))
6f2f06be
JB
6574
6575 as the case of a missing * on the operand is accepted (perhaps with
6576 a warning, issued further down). */
7b94647a 6577 specific_error = progress (operand_type_mismatch);
0cfa3eb3 6578 if (i.jumpabsolute && t->opcode_modifier.jump != JUMP_ABSOLUTE)
7b94647a 6579 continue;
6f2f06be 6580
5c07affc
L
6581 for (j = 0; j < MAX_OPERANDS; j++)
6582 operand_types[j] = t->operand_types[j];
6583
e365e234
JB
6584 /* In general, don't allow
6585 - 64-bit operands outside of 64-bit mode,
6586 - 32-bit operands on pre-386. */
7b94647a
JB
6587 specific_error = progress (mnem_suffix ? invalid_instruction_suffix
6588 : operand_size_mismatch);
4873e243 6589 j = i.imm_operands + (t->operands > i.imm_operands + 1);
e365e234
JB
6590 if (((i.suffix == QWORD_MNEM_SUFFIX
6591 && flag_code != CODE_64BIT
389d00a5
JB
6592 && !(t->opcode_modifier.opcodespace == SPACE_0F
6593 && t->base_opcode == 0xc7
5e74b495 6594 && t->opcode_modifier.opcodeprefix == PREFIX_NONE
8b65b895 6595 && t->extension_opcode == 1) /* cmpxchg8b */)
e365e234
JB
6596 || (i.suffix == LONG_MNEM_SUFFIX
6597 && !cpu_arch_flags.bitfield.cpui386))
45aa61fe 6598 && (intel_syntax
3cd7f3e3 6599 ? (t->opcode_modifier.mnemonicsize != IGNORESIZE
45aa61fe
AM
6600 && !intel_float_operand (t->name))
6601 : intel_float_operand (t->name) != 2)
4873e243
JB
6602 && (t->operands == i.imm_operands
6603 || (operand_types[i.imm_operands].bitfield.class != RegMMX
6604 && operand_types[i.imm_operands].bitfield.class != RegSIMD
6605 && operand_types[i.imm_operands].bitfield.class != RegMask)
6606 || (operand_types[j].bitfield.class != RegMMX
6607 && operand_types[j].bitfield.class != RegSIMD
6608 && operand_types[j].bitfield.class != RegMask))
63112cd6 6609 && !t->opcode_modifier.sib)
192dc9c6
JB
6610 continue;
6611
29b0f896 6612 /* Do not verify operands when there are none. */
e365e234 6613 if (!t->operands)
da4977e0
JB
6614 {
6615 if (VEX_check_encoding (t))
6616 {
7b94647a 6617 specific_error = progress (i.error);
da4977e0
JB
6618 continue;
6619 }
6620
6621 /* We've found a match; break out of loop. */
6622 break;
6623 }
252b5132 6624
48bcea9f
JB
6625 if (!t->opcode_modifier.jump
6626 || t->opcode_modifier.jump == JUMP_ABSOLUTE)
6627 {
6628 /* There should be only one Disp operand. */
6629 for (j = 0; j < MAX_OPERANDS; j++)
6630 if (operand_type_check (operand_types[j], disp))
539e75ad 6631 break;
48bcea9f
JB
6632 if (j < MAX_OPERANDS)
6633 {
5b7c81bd 6634 bool override = (i.prefix[ADDR_PREFIX] != 0);
48bcea9f
JB
6635
6636 addr_prefix_disp = j;
6637
a775efc8
JB
6638 /* Address size prefix will turn Disp64 operand into Disp32 and
6639 Disp32/Disp16 one into Disp16/Disp32 respectively. */
48bcea9f 6640 switch (flag_code)
40fb9820 6641 {
48bcea9f
JB
6642 case CODE_16BIT:
6643 override = !override;
6644 /* Fall through. */
6645 case CODE_32BIT:
6646 if (operand_types[j].bitfield.disp32
6647 && operand_types[j].bitfield.disp16)
40fb9820 6648 {
48bcea9f
JB
6649 operand_types[j].bitfield.disp16 = override;
6650 operand_types[j].bitfield.disp32 = !override;
40fb9820 6651 }
a775efc8 6652 gas_assert (!operand_types[j].bitfield.disp64);
48bcea9f
JB
6653 break;
6654
6655 case CODE_64BIT:
a775efc8 6656 if (operand_types[j].bitfield.disp64)
40fb9820 6657 {
a775efc8 6658 gas_assert (!operand_types[j].bitfield.disp32);
48bcea9f 6659 operand_types[j].bitfield.disp32 = override;
a775efc8 6660 operand_types[j].bitfield.disp64 = !override;
40fb9820 6661 }
48bcea9f
JB
6662 operand_types[j].bitfield.disp16 = 0;
6663 break;
40fb9820 6664 }
539e75ad 6665 }
48bcea9f 6666 }
539e75ad 6667
d7e3e627
L
6668 switch (i.reloc[0])
6669 {
6670 case BFD_RELOC_386_GOT32:
6671 /* Force 0x8b encoding for "mov foo@GOT, %eax". */
6672 if (t->base_opcode == 0xa0
6673 && t->opcode_modifier.opcodespace == SPACE_BASE)
6674 continue;
6675 break;
6676 case BFD_RELOC_386_TLS_GOTIE:
6677 case BFD_RELOC_386_TLS_LE_32:
6678 case BFD_RELOC_X86_64_GOTTPOFF:
6679 case BFD_RELOC_X86_64_TLSLD:
6680 /* Don't allow KMOV in TLS code sequences. */
6681 if (t->opcode_modifier.vex)
6682 continue;
6683 break;
6684 default:
6685 break;
6686 }
02a86693 6687
56ffb741 6688 /* We check register size if needed. */
e2195274
JB
6689 if (t->opcode_modifier.checkregsize)
6690 {
6691 check_register = (1 << t->operands) - 1;
a5748e0d 6692 if (i.broadcast.type || i.broadcast.bytes)
5273a3cd 6693 check_register &= ~(1 << i.broadcast.operand);
e2195274
JB
6694 }
6695 else
6696 check_register = 0;
6697
c6fb90c8 6698 overlap0 = operand_type_and (i.types[0], operand_types[0]);
29b0f896
AM
6699 switch (t->operands)
6700 {
6701 case 1:
40fb9820 6702 if (!operand_type_match (overlap0, i.types[0]))
29b0f896
AM
6703 continue;
6704 break;
6705 case 2:
33eaf5de 6706 /* xchg %eax, %eax is a special case. It is an alias for nop
8b38ad71
L
6707 only in 32bit mode and we can use opcode 0x90. In 64bit
6708 mode, we can't use 0x90 for xchg %eax, %eax since it should
6709 zero-extend %eax to %rax. */
6710 if (flag_code == CODE_64BIT
6711 && t->base_opcode == 0x90
35648716 6712 && t->opcode_modifier.opcodespace == SPACE_BASE
75e5731b
JB
6713 && i.types[0].bitfield.instance == Accum
6714 && i.types[0].bitfield.dword
6715 && i.types[1].bitfield.instance == Accum
6716 && i.types[1].bitfield.dword)
8b38ad71 6717 continue;
1212781b
JB
6718 /* xrelease mov %eax, <disp> is another special case. It must not
6719 match the accumulator-only encoding of mov. */
6720 if (flag_code != CODE_64BIT
6721 && i.hle_prefix
6722 && t->base_opcode == 0xa0
35648716 6723 && t->opcode_modifier.opcodespace == SPACE_BASE
75e5731b 6724 && i.types[0].bitfield.instance == Accum
8dc0818e 6725 && (i.flags[1] & Operand_Mem))
1212781b 6726 continue;
f5eb1d70
JB
6727 /* Fall through. */
6728
6729 case 3:
3ac21baa
JB
6730 if (!(size_match & MATCH_STRAIGHT))
6731 goto check_reverse;
64c49ab3
JB
6732 /* Reverse direction of operands if swapping is possible in the first
6733 place (operands need to be symmetric) and
6734 - the load form is requested, and the template is a store form,
6735 - the store form is requested, and the template is a load form,
6736 - the non-default (swapped) form is requested. */
6737 overlap1 = operand_type_and (operand_types[0], operand_types[1]);
f5eb1d70 6738 if (t->opcode_modifier.d && i.reg_operands == i.operands
64c49ab3
JB
6739 && !operand_type_all_zero (&overlap1))
6740 switch (i.dir_encoding)
6741 {
6742 case dir_encoding_load:
6743 if (operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 6744 || t->opcode_modifier.regmem)
64c49ab3
JB
6745 goto check_reverse;
6746 break;
6747
6748 case dir_encoding_store:
6749 if (!operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 6750 && !t->opcode_modifier.regmem)
64c49ab3
JB
6751 goto check_reverse;
6752 break;
6753
6754 case dir_encoding_swap:
6755 goto check_reverse;
6756
6757 case dir_encoding_default:
6758 break;
6759 }
86fa6981 6760 /* If we want store form, we skip the current load. */
64c49ab3
JB
6761 if ((i.dir_encoding == dir_encoding_store
6762 || i.dir_encoding == dir_encoding_swap)
86fa6981
L
6763 && i.mem_operands == 0
6764 && t->opcode_modifier.load)
fa99fab2 6765 continue;
1a0670f3 6766 /* Fall through. */
f48ff2ae 6767 case 4:
c0f3af97 6768 case 5:
c6fb90c8 6769 overlap1 = operand_type_and (i.types[1], operand_types[1]);
40fb9820
L
6770 if (!operand_type_match (overlap0, i.types[0])
6771 || !operand_type_match (overlap1, i.types[1])
e2195274 6772 || ((check_register & 3) == 3
dc821c5f 6773 && !operand_type_register_match (i.types[0],
40fb9820 6774 operand_types[0],
dc821c5f 6775 i.types[1],
40fb9820 6776 operand_types[1])))
29b0f896 6777 {
7b94647a
JB
6778 specific_error = progress (i.error);
6779
29b0f896 6780 /* Check if other direction is valid ... */
38e314eb 6781 if (!t->opcode_modifier.d)
29b0f896
AM
6782 continue;
6783
dc1e8a47 6784 check_reverse:
3ac21baa
JB
6785 if (!(size_match & MATCH_REVERSE))
6786 continue;
29b0f896 6787 /* Try reversing direction of operands. */
8bd915b7
JB
6788 j = t->opcode_modifier.vexsources ? 1 : i.operands - 1;
6789 overlap0 = operand_type_and (i.types[0], operand_types[j]);
6790 overlap1 = operand_type_and (i.types[j], operand_types[0]);
c975cec5
JB
6791 overlap2 = operand_type_and (i.types[1], operand_types[1]);
6792 gas_assert (t->operands != 3 || !check_register);
40fb9820 6793 if (!operand_type_match (overlap0, i.types[0])
8bd915b7 6794 || !operand_type_match (overlap1, i.types[j])
c975cec5
JB
6795 || (t->operands == 3
6796 && !operand_type_match (overlap2, i.types[1]))
45664ddb 6797 || (check_register
dc821c5f 6798 && !operand_type_register_match (i.types[0],
8bd915b7
JB
6799 operand_types[j],
6800 i.types[j],
45664ddb 6801 operand_types[0])))
29b0f896
AM
6802 {
6803 /* Does not match either direction. */
7b94647a 6804 specific_error = progress (i.error);
29b0f896
AM
6805 continue;
6806 }
38e314eb 6807 /* found_reverse_match holds which of D or FloatR
29b0f896 6808 we've found. */
38e314eb
JB
6809 if (!t->opcode_modifier.d)
6810 found_reverse_match = 0;
6811 else if (operand_types[0].bitfield.tbyte)
8a2ed489 6812 found_reverse_match = Opcode_FloatD;
8bd915b7
JB
6813 else if (t->opcode_modifier.vexsources)
6814 {
6815 found_reverse_match = Opcode_VexW;
6816 goto check_operands_345;
6817 }
2c735193
JB
6818 else if (t->opcode_modifier.opcodespace != SPACE_BASE
6819 && (t->opcode_modifier.opcodespace != SPACE_0F
6820 /* MOV to/from CR/DR/TR, as an exception, follow
6821 the base opcode space encoding model. */
6822 || (t->base_opcode | 7) != 0x27))
dbbc8b7e 6823 found_reverse_match = (t->base_opcode & 0xee) != 0x6e
2c735193 6824 ? Opcode_ExtD : Opcode_SIMD_IntD;
8a2ed489 6825 else
38e314eb 6826 found_reverse_match = Opcode_D;
40fb9820 6827 if (t->opcode_modifier.floatr)
8a2ed489 6828 found_reverse_match |= Opcode_FloatR;
29b0f896 6829 }
f48ff2ae 6830 else
29b0f896 6831 {
f48ff2ae 6832 /* Found a forward 2 operand match here. */
8bd915b7 6833 check_operands_345:
d1cbb4db
L
6834 switch (t->operands)
6835 {
c0f3af97 6836 case 5:
3d0738af 6837 overlap4 = operand_type_and (i.types[4], operand_types[4]);
c0f3af97 6838 if (!operand_type_match (overlap4, i.types[4])
dc821c5f 6839 || !operand_type_register_match (i.types[3],
c0f3af97 6840 operand_types[3],
c0f3af97
L
6841 i.types[4],
6842 operand_types[4]))
7b94647a
JB
6843 {
6844 specific_error = progress (i.error);
6845 continue;
6846 }
1a0670f3 6847 /* Fall through. */
f48ff2ae 6848 case 4:
3d0738af 6849 overlap3 = operand_type_and (i.types[3], operand_types[3]);
40fb9820 6850 if (!operand_type_match (overlap3, i.types[3])
e2195274
JB
6851 || ((check_register & 0xa) == 0xa
6852 && !operand_type_register_match (i.types[1],
f7768225
JB
6853 operand_types[1],
6854 i.types[3],
e2195274
JB
6855 operand_types[3]))
6856 || ((check_register & 0xc) == 0xc
6857 && !operand_type_register_match (i.types[2],
6858 operand_types[2],
6859 i.types[3],
6860 operand_types[3])))
7b94647a
JB
6861 {
6862 specific_error = progress (i.error);
6863 continue;
6864 }
1a0670f3 6865 /* Fall through. */
f48ff2ae 6866 case 3:
3d0738af 6867 overlap2 = operand_type_and (i.types[2], operand_types[2]);
40fb9820 6868 if (!operand_type_match (overlap2, i.types[2])
e2195274
JB
6869 || ((check_register & 5) == 5
6870 && !operand_type_register_match (i.types[0],
23e42951
JB
6871 operand_types[0],
6872 i.types[2],
e2195274
JB
6873 operand_types[2]))
6874 || ((check_register & 6) == 6
6875 && !operand_type_register_match (i.types[1],
6876 operand_types[1],
6877 i.types[2],
6878 operand_types[2])))
7b94647a
JB
6879 {
6880 specific_error = progress (i.error);
6881 continue;
6882 }
f48ff2ae
L
6883 break;
6884 }
29b0f896 6885 }
f48ff2ae 6886 /* Found either forward/reverse 2, 3 or 4 operand match here:
29b0f896
AM
6887 slip through to break. */
6888 }
c0f3af97 6889
9bb4d860
L
6890 /* Check if VEX/EVEX encoding requirements can be satisfied. */
6891 if (VEX_check_encoding (t))
da4977e0 6892 {
7b94647a 6893 specific_error = progress (i.error);
da4977e0
JB
6894 continue;
6895 }
6896
9bb4d860
L
6897 /* Check if vector operands are valid. */
6898 if (check_VecOperands (t))
5614d22c 6899 {
7b94647a 6900 specific_error = progress (i.error);
5614d22c
JB
6901 continue;
6902 }
a683cc34 6903
29b0f896
AM
6904 /* We've found a match; break out of loop. */
6905 break;
6906 }
6907
7b94647a
JB
6908#undef progress
6909
29b0f896
AM
6910 if (t == current_templates->end)
6911 {
6912 /* We found no match. */
a65babc9 6913 const char *err_msg;
7b94647a 6914 switch (specific_error)
a65babc9
L
6915 {
6916 default:
6917 abort ();
86e026a4 6918 case operand_size_mismatch:
a65babc9
L
6919 err_msg = _("operand size mismatch");
6920 break;
6921 case operand_type_mismatch:
6922 err_msg = _("operand type mismatch");
6923 break;
6924 case register_type_mismatch:
6925 err_msg = _("register type mismatch");
6926 break;
6927 case number_of_operands_mismatch:
6928 err_msg = _("number of operands mismatch");
6929 break;
6930 case invalid_instruction_suffix:
6931 err_msg = _("invalid instruction suffix");
6932 break;
6933 case bad_imm4:
4a2608e3 6934 err_msg = _("constant doesn't fit in 4 bits");
a65babc9 6935 break;
a65babc9
L
6936 case unsupported_with_intel_mnemonic:
6937 err_msg = _("unsupported with Intel mnemonic");
6938 break;
6939 case unsupported_syntax:
6940 err_msg = _("unsupported syntax");
6941 break;
6942 case unsupported:
35262a23 6943 as_bad (_("unsupported instruction `%s'"),
10efe3f6
L
6944 current_templates->start->name);
6945 return NULL;
260cd341
LC
6946 case invalid_sib_address:
6947 err_msg = _("invalid SIB address");
6948 break;
6c30d220
L
6949 case invalid_vsib_address:
6950 err_msg = _("invalid VSIB address");
6951 break;
7bab8ab5
JB
6952 case invalid_vector_register_set:
6953 err_msg = _("mask, index, and destination registers must be distinct");
6954 break;
260cd341
LC
6955 case invalid_tmm_register_set:
6956 err_msg = _("all tmm registers must be distinct");
6957 break;
0cc78721
CL
6958 case invalid_dest_and_src_register_set:
6959 err_msg = _("destination and source registers must be distinct");
6960 break;
6c30d220
L
6961 case unsupported_vector_index_register:
6962 err_msg = _("unsupported vector index register");
6963 break;
43234a1e
L
6964 case unsupported_broadcast:
6965 err_msg = _("unsupported broadcast");
6966 break;
43234a1e
L
6967 case broadcast_needed:
6968 err_msg = _("broadcast is needed for operand of such type");
6969 break;
6970 case unsupported_masking:
6971 err_msg = _("unsupported masking");
6972 break;
6973 case mask_not_on_destination:
6974 err_msg = _("mask not on destination operand");
6975 break;
6976 case no_default_mask:
6977 err_msg = _("default mask isn't allowed");
6978 break;
6979 case unsupported_rc_sae:
6980 err_msg = _("unsupported static rounding/sae");
6981 break;
43234a1e
L
6982 case invalid_register_operand:
6983 err_msg = _("invalid register operand");
6984 break;
a65babc9
L
6985 }
6986 as_bad (_("%s for `%s'"), err_msg,
891edac4 6987 current_templates->start->name);
fa99fab2 6988 return NULL;
29b0f896 6989 }
252b5132 6990
29b0f896
AM
6991 if (!quiet_warnings)
6992 {
6993 if (!intel_syntax
0cfa3eb3 6994 && (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE)))
6f2f06be 6995 as_warn (_("indirect %s without `*'"), t->name);
29b0f896 6996
40fb9820 6997 if (t->opcode_modifier.isprefix
3cd7f3e3 6998 && t->opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
6999 {
7000 /* Warn them that a data or address size prefix doesn't
7001 affect assembly of the next line of code. */
7002 as_warn (_("stand-alone `%s' prefix"), t->name);
7003 }
7004 }
7005
7006 /* Copy the template we found. */
9a182d04 7007 install_template (t);
539e75ad
L
7008
7009 if (addr_prefix_disp != -1)
7010 i.tm.operand_types[addr_prefix_disp]
7011 = operand_types[addr_prefix_disp];
7012
8bd915b7 7013 switch (found_reverse_match)
29b0f896 7014 {
8bd915b7
JB
7015 case 0:
7016 break;
7017
7018 default:
dfd69174
JB
7019 /* If we found a reverse match we must alter the opcode direction
7020 bit and clear/flip the regmem modifier one. found_reverse_match
7021 holds bits to change (different for int & float insns). */
29b0f896
AM
7022
7023 i.tm.base_opcode ^= found_reverse_match;
7024
f5eb1d70
JB
7025 i.tm.operand_types[0] = operand_types[i.operands - 1];
7026 i.tm.operand_types[i.operands - 1] = operand_types[0];
dfd69174
JB
7027
7028 /* Certain SIMD insns have their load forms specified in the opcode
7029 table, and hence we need to _set_ RegMem instead of clearing it.
7030 We need to avoid setting the bit though on insns like KMOVW. */
7031 i.tm.opcode_modifier.regmem
7032 = i.tm.opcode_modifier.modrm && i.tm.opcode_modifier.d
7033 && i.tm.operands > 2U - i.tm.opcode_modifier.sse2avx
7034 && !i.tm.opcode_modifier.regmem;
8bd915b7
JB
7035 break;
7036
7037 case Opcode_VexW:
7038 /* Only the first two register operands need reversing, alongside
7039 flipping VEX.W. */
7040 i.tm.opcode_modifier.vexw ^= VEXW0 ^ VEXW1;
7041
7042 j = i.tm.operand_types[0].bitfield.imm8;
7043 i.tm.operand_types[j] = operand_types[j + 1];
7044 i.tm.operand_types[j + 1] = operand_types[j];
7045 break;
29b0f896
AM
7046 }
7047
fa99fab2 7048 return t;
29b0f896
AM
7049}
7050
7051static int
e3bb37b5 7052check_string (void)
29b0f896 7053{
51c8edf6
JB
7054 unsigned int es_op = i.tm.opcode_modifier.isstring - IS_STRING_ES_OP0;
7055 unsigned int op = i.tm.operand_types[0].bitfield.baseindex ? es_op : 0;
8dc0818e 7056
5e042380 7057 if (i.seg[op] != NULL && i.seg[op] != reg_es)
29b0f896 7058 {
51c8edf6
JB
7059 as_bad (_("`%s' operand %u must use `%ses' segment"),
7060 i.tm.name,
7061 intel_syntax ? i.tm.operands - es_op : es_op + 1,
7062 register_prefix);
7063 return 0;
29b0f896 7064 }
51c8edf6
JB
7065
7066 /* There's only ever one segment override allowed per instruction.
7067 This instruction possibly has a legal segment override on the
7068 second operand, so copy the segment to where non-string
7069 instructions store it, allowing common code. */
7070 i.seg[op] = i.seg[1];
7071
29b0f896
AM
7072 return 1;
7073}
7074
7075static int
543613e9 7076process_suffix (void)
29b0f896 7077{
5b7c81bd 7078 bool is_crc32 = false, is_movx = false;
8b65b895 7079
29b0f896
AM
7080 /* If matched instruction specifies an explicit instruction mnemonic
7081 suffix, use it. */
673fe0f0 7082 if (i.tm.opcode_modifier.size == SIZE16)
40fb9820 7083 i.suffix = WORD_MNEM_SUFFIX;
673fe0f0 7084 else if (i.tm.opcode_modifier.size == SIZE32)
40fb9820 7085 i.suffix = LONG_MNEM_SUFFIX;
673fe0f0 7086 else if (i.tm.opcode_modifier.size == SIZE64)
40fb9820 7087 i.suffix = QWORD_MNEM_SUFFIX;
13e600d0 7088 else if (i.reg_operands
c8f8eebc 7089 && (i.operands > 1 || i.types[0].bitfield.class == Reg)
255571cd 7090 && i.tm.opcode_modifier.operandconstraint != ADDR_PREFIX_OP_REG)
29b0f896 7091 {
65fca059 7092 unsigned int numop = i.operands;
389d00a5
JB
7093
7094 /* MOVSX/MOVZX */
7095 is_movx = (i.tm.opcode_modifier.opcodespace == SPACE_0F
7096 && (i.tm.base_opcode | 8) == 0xbe)
7097 || (i.tm.opcode_modifier.opcodespace == SPACE_BASE
7098 && i.tm.base_opcode == 0x63
7099 && i.tm.cpu_flags.bitfield.cpu64);
7100
8b65b895 7101 /* CRC32 */
389d00a5
JB
7102 is_crc32 = (i.tm.base_opcode == 0xf0
7103 && i.tm.opcode_modifier.opcodespace == SPACE_0F38
8b65b895 7104 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2);
65fca059
JB
7105
7106 /* movsx/movzx want only their source operand considered here, for the
7107 ambiguity checking below. The suffix will be replaced afterwards
7108 to represent the destination (register). */
389d00a5 7109 if (is_movx && (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63))
65fca059
JB
7110 --i.operands;
7111
643bb870 7112 /* crc32 needs REX.W set regardless of suffix / source operand size. */
8b65b895 7113 if (is_crc32 && i.tm.operand_types[1].bitfield.qword)
643bb870
JB
7114 i.rex |= REX_W;
7115
29b0f896 7116 /* If there's no instruction mnemonic suffix we try to invent one
13e600d0 7117 based on GPR operands. */
29b0f896
AM
7118 if (!i.suffix)
7119 {
7120 /* We take i.suffix from the last register operand specified,
7121 Destination register type is more significant than source
381d071f
L
7122 register type. crc32 in SSE4.2 prefers source register
7123 type. */
8b65b895 7124 unsigned int op = is_crc32 ? 1 : i.operands;
20592a94 7125
1a035124
JB
7126 while (op--)
7127 if (i.tm.operand_types[op].bitfield.instance == InstanceNone
7128 || i.tm.operand_types[op].bitfield.instance == Accum)
7129 {
7130 if (i.types[op].bitfield.class != Reg)
7131 continue;
7132 if (i.types[op].bitfield.byte)
7133 i.suffix = BYTE_MNEM_SUFFIX;
7134 else if (i.types[op].bitfield.word)
7135 i.suffix = WORD_MNEM_SUFFIX;
7136 else if (i.types[op].bitfield.dword)
7137 i.suffix = LONG_MNEM_SUFFIX;
7138 else if (i.types[op].bitfield.qword)
7139 i.suffix = QWORD_MNEM_SUFFIX;
7140 else
7141 continue;
7142 break;
7143 }
65fca059
JB
7144
7145 /* As an exception, movsx/movzx silently default to a byte source
7146 in AT&T mode. */
389d00a5 7147 if (is_movx && i.tm.opcode_modifier.w && !i.suffix && !intel_syntax)
65fca059 7148 i.suffix = BYTE_MNEM_SUFFIX;
29b0f896
AM
7149 }
7150 else if (i.suffix == BYTE_MNEM_SUFFIX)
7151 {
1cb0ab18 7152 if (!check_byte_reg ())
29b0f896
AM
7153 return 0;
7154 }
7155 else if (i.suffix == LONG_MNEM_SUFFIX)
7156 {
1cb0ab18 7157 if (!check_long_reg ())
29b0f896
AM
7158 return 0;
7159 }
7160 else if (i.suffix == QWORD_MNEM_SUFFIX)
7161 {
1cb0ab18 7162 if (!check_qword_reg ())
29b0f896
AM
7163 return 0;
7164 }
7165 else if (i.suffix == WORD_MNEM_SUFFIX)
7166 {
1cb0ab18 7167 if (!check_word_reg ())
29b0f896
AM
7168 return 0;
7169 }
3cd7f3e3
L
7170 else if (intel_syntax
7171 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
7172 /* Do nothing if the instruction is going to ignore the prefix. */
7173 ;
7174 else
7175 abort ();
65fca059
JB
7176
7177 /* Undo the movsx/movzx change done above. */
7178 i.operands = numop;
29b0f896 7179 }
3cd7f3e3
L
7180 else if (i.tm.opcode_modifier.mnemonicsize == DEFAULTSIZE
7181 && !i.suffix)
29b0f896 7182 {
13e600d0
JB
7183 i.suffix = stackop_size;
7184 if (stackop_size == LONG_MNEM_SUFFIX)
06f74c5c
L
7185 {
7186 /* stackop_size is set to LONG_MNEM_SUFFIX for the
7187 .code16gcc directive to support 16-bit mode with
7188 32-bit address. For IRET without a suffix, generate
7189 16-bit IRET (opcode 0xcf) to return from an interrupt
7190 handler. */
13e600d0
JB
7191 if (i.tm.base_opcode == 0xcf)
7192 {
7193 i.suffix = WORD_MNEM_SUFFIX;
7194 as_warn (_("generating 16-bit `iret' for .code16gcc directive"));
7195 }
7196 /* Warn about changed behavior for segment register push/pop. */
7197 else if ((i.tm.base_opcode | 1) == 0x07)
7198 as_warn (_("generating 32-bit `%s', unlike earlier gas versions"),
7199 i.tm.name);
06f74c5c 7200 }
29b0f896 7201 }
c006a730 7202 else if (!i.suffix
0cfa3eb3
JB
7203 && (i.tm.opcode_modifier.jump == JUMP_ABSOLUTE
7204 || i.tm.opcode_modifier.jump == JUMP_BYTE
7205 || i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT
389d00a5
JB
7206 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
7207 && i.tm.base_opcode == 0x01 /* [ls][gi]dt */
64e74474 7208 && i.tm.extension_opcode <= 3)))
9306ca4a
JB
7209 {
7210 switch (flag_code)
7211 {
7212 case CODE_64BIT:
40fb9820 7213 if (!i.tm.opcode_modifier.no_qsuf)
9306ca4a 7214 {
828c2a25
JB
7215 if (i.tm.opcode_modifier.jump == JUMP_BYTE
7216 || i.tm.opcode_modifier.no_lsuf)
7217 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a
JB
7218 break;
7219 }
1a0670f3 7220 /* Fall through. */
9306ca4a 7221 case CODE_32BIT:
40fb9820 7222 if (!i.tm.opcode_modifier.no_lsuf)
9306ca4a
JB
7223 i.suffix = LONG_MNEM_SUFFIX;
7224 break;
7225 case CODE_16BIT:
40fb9820 7226 if (!i.tm.opcode_modifier.no_wsuf)
9306ca4a
JB
7227 i.suffix = WORD_MNEM_SUFFIX;
7228 break;
7229 }
7230 }
252b5132 7231
c006a730 7232 if (!i.suffix
3cd7f3e3 7233 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8
JB
7234 /* Also cover lret/retf/iret in 64-bit mode. */
7235 || (flag_code == CODE_64BIT
7236 && !i.tm.opcode_modifier.no_lsuf
7237 && !i.tm.opcode_modifier.no_qsuf))
3cd7f3e3 7238 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
8bbb3ad8
JB
7239 /* Explicit sizing prefixes are assumed to disambiguate insns. */
7240 && !i.prefix[DATA_PREFIX] && !(i.prefix[REX_PREFIX] & REX_W)
62b3f548
JB
7241 /* Accept FLDENV et al without suffix. */
7242 && (i.tm.opcode_modifier.no_ssuf || i.tm.opcode_modifier.floatmf))
29b0f896 7243 {
6c0946d0 7244 unsigned int suffixes, evex = 0;
c006a730
JB
7245
7246 suffixes = !i.tm.opcode_modifier.no_bsuf;
7247 if (!i.tm.opcode_modifier.no_wsuf)
7248 suffixes |= 1 << 1;
7249 if (!i.tm.opcode_modifier.no_lsuf)
7250 suffixes |= 1 << 2;
7251 if (!i.tm.opcode_modifier.no_ldsuf)
7252 suffixes |= 1 << 3;
7253 if (!i.tm.opcode_modifier.no_ssuf)
7254 suffixes |= 1 << 4;
7255 if (flag_code == CODE_64BIT && !i.tm.opcode_modifier.no_qsuf)
7256 suffixes |= 1 << 5;
7257
6c0946d0
JB
7258 /* For [XYZ]MMWORD operands inspect operand sizes. While generally
7259 also suitable for AT&T syntax mode, it was requested that this be
7260 restricted to just Intel syntax. */
a5748e0d
JB
7261 if (intel_syntax && is_any_vex_encoding (&i.tm)
7262 && !i.broadcast.type && !i.broadcast.bytes)
6c0946d0 7263 {
b9915cbc 7264 unsigned int op;
6c0946d0 7265
b9915cbc 7266 for (op = 0; op < i.tm.operands; ++op)
6c0946d0 7267 {
b9915cbc
JB
7268 if (is_evex_encoding (&i.tm)
7269 && !cpu_arch_flags.bitfield.cpuavx512vl)
6c0946d0 7270 {
b9915cbc
JB
7271 if (i.tm.operand_types[op].bitfield.ymmword)
7272 i.tm.operand_types[op].bitfield.xmmword = 0;
7273 if (i.tm.operand_types[op].bitfield.zmmword)
7274 i.tm.operand_types[op].bitfield.ymmword = 0;
7275 if (!i.tm.opcode_modifier.evex
7276 || i.tm.opcode_modifier.evex == EVEXDYN)
7277 i.tm.opcode_modifier.evex = EVEX512;
7278 }
6c0946d0 7279
b9915cbc
JB
7280 if (i.tm.operand_types[op].bitfield.xmmword
7281 + i.tm.operand_types[op].bitfield.ymmword
7282 + i.tm.operand_types[op].bitfield.zmmword < 2)
7283 continue;
6c0946d0 7284
b9915cbc
JB
7285 /* Any properly sized operand disambiguates the insn. */
7286 if (i.types[op].bitfield.xmmword
7287 || i.types[op].bitfield.ymmword
7288 || i.types[op].bitfield.zmmword)
7289 {
7290 suffixes &= ~(7 << 6);
7291 evex = 0;
7292 break;
7293 }
6c0946d0 7294
b9915cbc
JB
7295 if ((i.flags[op] & Operand_Mem)
7296 && i.tm.operand_types[op].bitfield.unspecified)
7297 {
7298 if (i.tm.operand_types[op].bitfield.xmmword)
7299 suffixes |= 1 << 6;
7300 if (i.tm.operand_types[op].bitfield.ymmword)
7301 suffixes |= 1 << 7;
7302 if (i.tm.operand_types[op].bitfield.zmmword)
7303 suffixes |= 1 << 8;
7304 if (is_evex_encoding (&i.tm))
7305 evex = EVEX512;
6c0946d0
JB
7306 }
7307 }
7308 }
7309
7310 /* Are multiple suffixes / operand sizes allowed? */
c006a730 7311 if (suffixes & (suffixes - 1))
9306ca4a 7312 {
873494c8 7313 if (intel_syntax
3cd7f3e3 7314 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8 7315 || operand_check == check_error))
9306ca4a 7316 {
c006a730 7317 as_bad (_("ambiguous operand size for `%s'"), i.tm.name);
9306ca4a
JB
7318 return 0;
7319 }
c006a730 7320 if (operand_check == check_error)
9306ca4a 7321 {
c006a730
JB
7322 as_bad (_("no instruction mnemonic suffix given and "
7323 "no register operands; can't size `%s'"), i.tm.name);
9306ca4a
JB
7324 return 0;
7325 }
c006a730 7326 if (operand_check == check_warning)
873494c8
JB
7327 as_warn (_("%s; using default for `%s'"),
7328 intel_syntax
7329 ? _("ambiguous operand size")
7330 : _("no instruction mnemonic suffix given and "
7331 "no register operands"),
7332 i.tm.name);
c006a730
JB
7333
7334 if (i.tm.opcode_modifier.floatmf)
7335 i.suffix = SHORT_MNEM_SUFFIX;
389d00a5 7336 else if (is_movx)
65fca059 7337 /* handled below */;
6c0946d0
JB
7338 else if (evex)
7339 i.tm.opcode_modifier.evex = evex;
c006a730
JB
7340 else if (flag_code == CODE_16BIT)
7341 i.suffix = WORD_MNEM_SUFFIX;
1a035124 7342 else if (!i.tm.opcode_modifier.no_lsuf)
c006a730 7343 i.suffix = LONG_MNEM_SUFFIX;
1a035124
JB
7344 else
7345 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a 7346 }
29b0f896 7347 }
252b5132 7348
389d00a5 7349 if (is_movx)
65fca059
JB
7350 {
7351 /* In Intel syntax, movsx/movzx must have a "suffix" (checked above).
7352 In AT&T syntax, if there is no suffix (warned about above), the default
7353 will be byte extension. */
7354 if (i.tm.opcode_modifier.w && i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
7355 i.tm.base_opcode |= 1;
7356
7357 /* For further processing, the suffix should represent the destination
7358 (register). This is already the case when one was used with
7359 mov[sz][bw]*, but we need to replace it for mov[sz]x, or if there was
7360 no suffix to begin with. */
7361 if (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63 || !i.suffix)
7362 {
7363 if (i.types[1].bitfield.word)
7364 i.suffix = WORD_MNEM_SUFFIX;
7365 else if (i.types[1].bitfield.qword)
7366 i.suffix = QWORD_MNEM_SUFFIX;
7367 else
7368 i.suffix = LONG_MNEM_SUFFIX;
7369
7370 i.tm.opcode_modifier.w = 0;
7371 }
7372 }
7373
50128d0c
JB
7374 if (!i.tm.opcode_modifier.modrm && i.reg_operands && i.tm.operands < 3)
7375 i.short_form = (i.tm.operand_types[0].bitfield.class == Reg)
7376 != (i.tm.operand_types[1].bitfield.class == Reg);
7377
d2224064
JB
7378 /* Change the opcode based on the operand size given by i.suffix. */
7379 switch (i.suffix)
29b0f896 7380 {
d2224064
JB
7381 /* Size floating point instruction. */
7382 case LONG_MNEM_SUFFIX:
7383 if (i.tm.opcode_modifier.floatmf)
7384 {
7385 i.tm.base_opcode ^= 4;
7386 break;
7387 }
7388 /* fall through */
7389 case WORD_MNEM_SUFFIX:
7390 case QWORD_MNEM_SUFFIX:
29b0f896 7391 /* It's not a byte, select word/dword operation. */
40fb9820 7392 if (i.tm.opcode_modifier.w)
29b0f896 7393 {
50128d0c 7394 if (i.short_form)
29b0f896
AM
7395 i.tm.base_opcode |= 8;
7396 else
7397 i.tm.base_opcode |= 1;
7398 }
d2224064
JB
7399 /* fall through */
7400 case SHORT_MNEM_SUFFIX:
29b0f896
AM
7401 /* Now select between word & dword operations via the operand
7402 size prefix, except for instructions that will ignore this
7403 prefix anyway. */
c8f8eebc 7404 if (i.suffix != QWORD_MNEM_SUFFIX
3cd7f3e3 7405 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
c8f8eebc
JB
7406 && !i.tm.opcode_modifier.floatmf
7407 && !is_any_vex_encoding (&i.tm)
7408 && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
7409 || (flag_code == CODE_64BIT
7410 && i.tm.opcode_modifier.jump == JUMP_BYTE)))
24eab124
AM
7411 {
7412 unsigned int prefix = DATA_PREFIX_OPCODE;
543613e9 7413
0cfa3eb3 7414 if (i.tm.opcode_modifier.jump == JUMP_BYTE) /* jcxz, loop */
29b0f896 7415 prefix = ADDR_PREFIX_OPCODE;
252b5132 7416
29b0f896
AM
7417 if (!add_prefix (prefix))
7418 return 0;
24eab124 7419 }
252b5132 7420
29b0f896
AM
7421 /* Set mode64 for an operand. */
7422 if (i.suffix == QWORD_MNEM_SUFFIX
9146926a 7423 && flag_code == CODE_64BIT
d2224064 7424 && !i.tm.opcode_modifier.norex64
4ed21b58 7425 && !i.tm.opcode_modifier.vexw
46e883c5 7426 /* Special case for xchg %rax,%rax. It is NOP and doesn't
d2224064
JB
7427 need rex64. */
7428 && ! (i.operands == 2
7429 && i.tm.base_opcode == 0x90
7430 && i.tm.extension_opcode == None
75e5731b
JB
7431 && i.types[0].bitfield.instance == Accum
7432 && i.types[0].bitfield.qword
7433 && i.types[1].bitfield.instance == Accum
7434 && i.types[1].bitfield.qword))
d2224064 7435 i.rex |= REX_W;
3e73aa7c 7436
d2224064 7437 break;
8bbb3ad8
JB
7438
7439 case 0:
f9a6a8f0 7440 /* Select word/dword/qword operation with explicit data sizing prefix
8bbb3ad8
JB
7441 when there are no suitable register operands. */
7442 if (i.tm.opcode_modifier.w
7443 && (i.prefix[DATA_PREFIX] || (i.prefix[REX_PREFIX] & REX_W))
7444 && (!i.reg_operands
7445 || (i.reg_operands == 1
7446 /* ShiftCount */
7447 && (i.tm.operand_types[0].bitfield.instance == RegC
7448 /* InOutPortReg */
7449 || i.tm.operand_types[0].bitfield.instance == RegD
7450 || i.tm.operand_types[1].bitfield.instance == RegD
7451 /* CRC32 */
8b65b895 7452 || is_crc32))))
8bbb3ad8
JB
7453 i.tm.base_opcode |= 1;
7454 break;
29b0f896 7455 }
7ecd2f8b 7456
255571cd 7457 if (i.tm.opcode_modifier.operandconstraint == ADDR_PREFIX_OP_REG)
c0a30a9f 7458 {
c8f8eebc
JB
7459 gas_assert (!i.suffix);
7460 gas_assert (i.reg_operands);
c0a30a9f 7461
c8f8eebc
JB
7462 if (i.tm.operand_types[0].bitfield.instance == Accum
7463 || i.operands == 1)
7464 {
7465 /* The address size override prefix changes the size of the
7466 first operand. */
7467 if (flag_code == CODE_64BIT
7468 && i.op[0].regs->reg_type.bitfield.word)
7469 {
7470 as_bad (_("16-bit addressing unavailable for `%s'"),
7471 i.tm.name);
7472 return 0;
7473 }
7474
7475 if ((flag_code == CODE_32BIT
7476 ? i.op[0].regs->reg_type.bitfield.word
7477 : i.op[0].regs->reg_type.bitfield.dword)
7478 && !add_prefix (ADDR_PREFIX_OPCODE))
7479 return 0;
7480 }
c0a30a9f
L
7481 else
7482 {
c8f8eebc
JB
7483 /* Check invalid register operand when the address size override
7484 prefix changes the size of register operands. */
7485 unsigned int op;
7486 enum { need_word, need_dword, need_qword } need;
7487
27f13469 7488 /* Check the register operand for the address size prefix if
b3a3496f 7489 the memory operand has no real registers, like symbol, DISP
829f3fe1 7490 or bogus (x32-only) symbol(%rip) when symbol(%eip) is meant. */
27f13469
L
7491 if (i.mem_operands == 1
7492 && i.reg_operands == 1
7493 && i.operands == 2
27f13469 7494 && i.types[1].bitfield.class == Reg
b3a3496f
L
7495 && (flag_code == CODE_32BIT
7496 ? i.op[1].regs->reg_type.bitfield.word
7497 : i.op[1].regs->reg_type.bitfield.dword)
7498 && ((i.base_reg == NULL && i.index_reg == NULL)
829f3fe1
JB
7499#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
7500 || (x86_elf_abi == X86_64_X32_ABI
7501 && i.base_reg
b3a3496f
L
7502 && i.base_reg->reg_num == RegIP
7503 && i.base_reg->reg_type.bitfield.qword))
829f3fe1
JB
7504#else
7505 || 0)
7506#endif
27f13469
L
7507 && !add_prefix (ADDR_PREFIX_OPCODE))
7508 return 0;
7509
c8f8eebc
JB
7510 if (flag_code == CODE_32BIT)
7511 need = i.prefix[ADDR_PREFIX] ? need_word : need_dword;
7512 else if (i.prefix[ADDR_PREFIX])
c0a30a9f
L
7513 need = need_dword;
7514 else
7515 need = flag_code == CODE_64BIT ? need_qword : need_word;
c0a30a9f 7516
c8f8eebc
JB
7517 for (op = 0; op < i.operands; op++)
7518 {
7519 if (i.types[op].bitfield.class != Reg)
7520 continue;
7521
7522 switch (need)
7523 {
7524 case need_word:
7525 if (i.op[op].regs->reg_type.bitfield.word)
7526 continue;
7527 break;
7528 case need_dword:
7529 if (i.op[op].regs->reg_type.bitfield.dword)
7530 continue;
7531 break;
7532 case need_qword:
7533 if (i.op[op].regs->reg_type.bitfield.qword)
7534 continue;
7535 break;
7536 }
7537
7538 as_bad (_("invalid register operand size for `%s'"),
7539 i.tm.name);
7540 return 0;
7541 }
7542 }
c0a30a9f
L
7543 }
7544
29b0f896
AM
7545 return 1;
7546}
3e73aa7c 7547
29b0f896 7548static int
543613e9 7549check_byte_reg (void)
29b0f896
AM
7550{
7551 int op;
543613e9 7552
29b0f896
AM
7553 for (op = i.operands; --op >= 0;)
7554 {
dc821c5f 7555 /* Skip non-register operands. */
bab6aec1 7556 if (i.types[op].bitfield.class != Reg)
dc821c5f
JB
7557 continue;
7558
29b0f896
AM
7559 /* If this is an eight bit register, it's OK. If it's the 16 or
7560 32 bit version of an eight bit register, we will just use the
7561 low portion, and that's OK too. */
dc821c5f 7562 if (i.types[op].bitfield.byte)
29b0f896
AM
7563 continue;
7564
5a819eb9 7565 /* I/O port address operands are OK too. */
75e5731b
JB
7566 if (i.tm.operand_types[op].bitfield.instance == RegD
7567 && i.tm.operand_types[op].bitfield.word)
5a819eb9
JB
7568 continue;
7569
9706160a 7570 /* crc32 only wants its source operand checked here. */
389d00a5
JB
7571 if (i.tm.base_opcode == 0xf0
7572 && i.tm.opcode_modifier.opcodespace == SPACE_0F38
8b65b895
L
7573 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2
7574 && op != 0)
9344ff29
L
7575 continue;
7576
29b0f896 7577 /* Any other register is bad. */
73c76375
JB
7578 as_bad (_("`%s%s' not allowed with `%s%c'"),
7579 register_prefix, i.op[op].regs->reg_name,
7580 i.tm.name, i.suffix);
7581 return 0;
29b0f896
AM
7582 }
7583 return 1;
7584}
7585
7586static int
e3bb37b5 7587check_long_reg (void)
29b0f896
AM
7588{
7589 int op;
7590
7591 for (op = i.operands; --op >= 0;)
dc821c5f 7592 /* Skip non-register operands. */
bab6aec1 7593 if (i.types[op].bitfield.class != Reg)
dc821c5f 7594 continue;
29b0f896
AM
7595 /* Reject eight bit registers, except where the template requires
7596 them. (eg. movzb) */
dc821c5f 7597 else if (i.types[op].bitfield.byte
bab6aec1 7598 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7599 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7600 && (i.tm.operand_types[op].bitfield.word
7601 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7602 {
a540244d
L
7603 as_bad (_("`%s%s' not allowed with `%s%c'"),
7604 register_prefix,
29b0f896
AM
7605 i.op[op].regs->reg_name,
7606 i.tm.name,
7607 i.suffix);
7608 return 0;
7609 }
be4c5e58
L
7610 /* Error if the e prefix on a general reg is missing. */
7611 else if (i.types[op].bitfield.word
bab6aec1 7612 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7613 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7614 && i.tm.operand_types[op].bitfield.dword)
29b0f896 7615 {
be4c5e58
L
7616 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7617 register_prefix, i.op[op].regs->reg_name,
7618 i.suffix);
7619 return 0;
252b5132 7620 }
e4630f71 7621 /* Warn if the r prefix on a general reg is present. */
dc821c5f 7622 else if (i.types[op].bitfield.qword
bab6aec1 7623 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7624 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7625 && i.tm.operand_types[op].bitfield.dword)
252b5132 7626 {
1cb0ab18
JB
7627 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7628 register_prefix, i.op[op].regs->reg_name, i.suffix);
7629 return 0;
29b0f896
AM
7630 }
7631 return 1;
7632}
252b5132 7633
29b0f896 7634static int
e3bb37b5 7635check_qword_reg (void)
29b0f896
AM
7636{
7637 int op;
252b5132 7638
29b0f896 7639 for (op = i.operands; --op >= 0; )
dc821c5f 7640 /* Skip non-register operands. */
bab6aec1 7641 if (i.types[op].bitfield.class != Reg)
dc821c5f 7642 continue;
29b0f896
AM
7643 /* Reject eight bit registers, except where the template requires
7644 them. (eg. movzb) */
dc821c5f 7645 else if (i.types[op].bitfield.byte
bab6aec1 7646 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7647 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7648 && (i.tm.operand_types[op].bitfield.word
7649 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7650 {
a540244d
L
7651 as_bad (_("`%s%s' not allowed with `%s%c'"),
7652 register_prefix,
29b0f896
AM
7653 i.op[op].regs->reg_name,
7654 i.tm.name,
7655 i.suffix);
7656 return 0;
7657 }
e4630f71 7658 /* Warn if the r prefix on a general reg is missing. */
dc821c5f
JB
7659 else if ((i.types[op].bitfield.word
7660 || i.types[op].bitfield.dword)
bab6aec1 7661 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7662 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7663 && i.tm.operand_types[op].bitfield.qword)
29b0f896
AM
7664 {
7665 /* Prohibit these changes in the 64bit mode, since the
7666 lowering is more complicated. */
1cb0ab18
JB
7667 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7668 register_prefix, i.op[op].regs->reg_name, i.suffix);
7669 return 0;
252b5132 7670 }
29b0f896
AM
7671 return 1;
7672}
252b5132 7673
29b0f896 7674static int
e3bb37b5 7675check_word_reg (void)
29b0f896
AM
7676{
7677 int op;
7678 for (op = i.operands; --op >= 0;)
dc821c5f 7679 /* Skip non-register operands. */
bab6aec1 7680 if (i.types[op].bitfield.class != Reg)
dc821c5f 7681 continue;
29b0f896
AM
7682 /* Reject eight bit registers, except where the template requires
7683 them. (eg. movzb) */
dc821c5f 7684 else if (i.types[op].bitfield.byte
bab6aec1 7685 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7686 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7687 && (i.tm.operand_types[op].bitfield.word
7688 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7689 {
a540244d
L
7690 as_bad (_("`%s%s' not allowed with `%s%c'"),
7691 register_prefix,
29b0f896
AM
7692 i.op[op].regs->reg_name,
7693 i.tm.name,
7694 i.suffix);
7695 return 0;
7696 }
9706160a
JB
7697 /* Error if the e or r prefix on a general reg is present. */
7698 else if ((i.types[op].bitfield.dword
dc821c5f 7699 || i.types[op].bitfield.qword)
bab6aec1 7700 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7701 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7702 && i.tm.operand_types[op].bitfield.word)
252b5132 7703 {
9706160a
JB
7704 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7705 register_prefix, i.op[op].regs->reg_name,
7706 i.suffix);
7707 return 0;
29b0f896
AM
7708 }
7709 return 1;
7710}
252b5132 7711
29b0f896 7712static int
40fb9820 7713update_imm (unsigned int j)
29b0f896 7714{
bc0844ae 7715 i386_operand_type overlap = i.types[j];
be1643ff
JB
7716 if (overlap.bitfield.imm8
7717 + overlap.bitfield.imm8s
7718 + overlap.bitfield.imm16
7719 + overlap.bitfield.imm32
7720 + overlap.bitfield.imm32s
7721 + overlap.bitfield.imm64 > 1)
29b0f896
AM
7722 {
7723 if (i.suffix)
7724 {
40fb9820
L
7725 i386_operand_type temp;
7726
0dfbf9d7 7727 operand_type_set (&temp, 0);
7ab9ffdd 7728 if (i.suffix == BYTE_MNEM_SUFFIX)
40fb9820
L
7729 {
7730 temp.bitfield.imm8 = overlap.bitfield.imm8;
7731 temp.bitfield.imm8s = overlap.bitfield.imm8s;
7732 }
7733 else if (i.suffix == WORD_MNEM_SUFFIX)
7734 temp.bitfield.imm16 = overlap.bitfield.imm16;
7735 else if (i.suffix == QWORD_MNEM_SUFFIX)
7736 {
7737 temp.bitfield.imm64 = overlap.bitfield.imm64;
7738 temp.bitfield.imm32s = overlap.bitfield.imm32s;
7739 }
7740 else
7741 temp.bitfield.imm32 = overlap.bitfield.imm32;
7742 overlap = temp;
29b0f896 7743 }
0dfbf9d7
L
7744 else if (operand_type_equal (&overlap, &imm16_32_32s)
7745 || operand_type_equal (&overlap, &imm16_32)
7746 || operand_type_equal (&overlap, &imm16_32s))
29b0f896 7747 {
40fb9820 7748 if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
65da13b5 7749 overlap = imm16;
40fb9820 7750 else
65da13b5 7751 overlap = imm32s;
29b0f896 7752 }
8bbb3ad8
JB
7753 else if (i.prefix[REX_PREFIX] & REX_W)
7754 overlap = operand_type_and (overlap, imm32s);
7755 else if (i.prefix[DATA_PREFIX])
7756 overlap = operand_type_and (overlap,
7757 flag_code != CODE_16BIT ? imm16 : imm32);
be1643ff
JB
7758 if (overlap.bitfield.imm8
7759 + overlap.bitfield.imm8s
7760 + overlap.bitfield.imm16
7761 + overlap.bitfield.imm32
7762 + overlap.bitfield.imm32s
7763 + overlap.bitfield.imm64 != 1)
29b0f896 7764 {
4eed87de
AM
7765 as_bad (_("no instruction mnemonic suffix given; "
7766 "can't determine immediate size"));
29b0f896
AM
7767 return 0;
7768 }
7769 }
40fb9820 7770 i.types[j] = overlap;
29b0f896 7771
40fb9820
L
7772 return 1;
7773}
7774
7775static int
7776finalize_imm (void)
7777{
bc0844ae 7778 unsigned int j, n;
29b0f896 7779
bc0844ae
L
7780 /* Update the first 2 immediate operands. */
7781 n = i.operands > 2 ? 2 : i.operands;
7782 if (n)
7783 {
7784 for (j = 0; j < n; j++)
7785 if (update_imm (j) == 0)
7786 return 0;
40fb9820 7787
bc0844ae
L
7788 /* The 3rd operand can't be immediate operand. */
7789 gas_assert (operand_type_check (i.types[2], imm) == 0);
7790 }
29b0f896
AM
7791
7792 return 1;
7793}
7794
7795static int
e3bb37b5 7796process_operands (void)
29b0f896
AM
7797{
7798 /* Default segment register this instruction will use for memory
7799 accesses. 0 means unknown. This is only for optimizing out
7800 unnecessary segment overrides. */
5e042380 7801 const reg_entry *default_seg = NULL;
29b0f896 7802
a5aeccd9
JB
7803 if (i.tm.opcode_modifier.sse2avx)
7804 {
7805 /* Legacy encoded insns allow explicit REX prefixes, so these prefixes
7806 need converting. */
7807 i.rex |= i.prefix[REX_PREFIX] & (REX_W | REX_R | REX_X | REX_B);
7808 i.prefix[REX_PREFIX] = 0;
7809 i.rex_encoding = 0;
7810 }
c423d21a
JB
7811 /* ImmExt should be processed after SSE2AVX. */
7812 else if (i.tm.opcode_modifier.immext)
7813 process_immext ();
a5aeccd9 7814
2426c15f 7815 if (i.tm.opcode_modifier.sse2avx && i.tm.opcode_modifier.vexvvvv)
29b0f896 7816 {
91d6fa6a
NC
7817 unsigned int dupl = i.operands;
7818 unsigned int dest = dupl - 1;
9fcfb3d7
L
7819 unsigned int j;
7820
c0f3af97 7821 /* The destination must be an xmm register. */
9c2799c2 7822 gas_assert (i.reg_operands
91d6fa6a 7823 && MAX_OPERANDS > dupl
7ab9ffdd 7824 && operand_type_equal (&i.types[dest], &regxmm));
c0f3af97 7825
75e5731b 7826 if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 7827 && i.tm.operand_types[0].bitfield.xmmword)
e2ec9d29 7828 {
8cd7925b 7829 if (i.tm.opcode_modifier.vexsources == VEX3SOURCES)
c0f3af97
L
7830 {
7831 /* Keep xmm0 for instructions with VEX prefix and 3
7832 sources. */
75e5731b 7833 i.tm.operand_types[0].bitfield.instance = InstanceNone;
3528c362 7834 i.tm.operand_types[0].bitfield.class = RegSIMD;
c0f3af97
L
7835 goto duplicate;
7836 }
e2ec9d29 7837 else
c0f3af97
L
7838 {
7839 /* We remove the first xmm0 and keep the number of
7840 operands unchanged, which in fact duplicates the
7841 destination. */
7842 for (j = 1; j < i.operands; j++)
7843 {
7844 i.op[j - 1] = i.op[j];
7845 i.types[j - 1] = i.types[j];
7846 i.tm.operand_types[j - 1] = i.tm.operand_types[j];
8dc0818e 7847 i.flags[j - 1] = i.flags[j];
c0f3af97
L
7848 }
7849 }
7850 }
255571cd 7851 else if (i.tm.opcode_modifier.operandconstraint == IMPLICIT_1ST_XMM0)
7ab9ffdd 7852 {
91d6fa6a 7853 gas_assert ((MAX_OPERANDS - 1) > dupl
8cd7925b
L
7854 && (i.tm.opcode_modifier.vexsources
7855 == VEX3SOURCES));
c0f3af97
L
7856
7857 /* Add the implicit xmm0 for instructions with VEX prefix
7858 and 3 sources. */
7859 for (j = i.operands; j > 0; j--)
7860 {
7861 i.op[j] = i.op[j - 1];
7862 i.types[j] = i.types[j - 1];
7863 i.tm.operand_types[j] = i.tm.operand_types[j - 1];
8dc0818e 7864 i.flags[j] = i.flags[j - 1];
c0f3af97
L
7865 }
7866 i.op[0].regs
629310ab 7867 = (const reg_entry *) str_hash_find (reg_hash, "xmm0");
7ab9ffdd 7868 i.types[0] = regxmm;
c0f3af97
L
7869 i.tm.operand_types[0] = regxmm;
7870
7871 i.operands += 2;
7872 i.reg_operands += 2;
7873 i.tm.operands += 2;
7874
91d6fa6a 7875 dupl++;
c0f3af97 7876 dest++;
91d6fa6a
NC
7877 i.op[dupl] = i.op[dest];
7878 i.types[dupl] = i.types[dest];
7879 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 7880 i.flags[dupl] = i.flags[dest];
e2ec9d29 7881 }
c0f3af97
L
7882 else
7883 {
dc1e8a47 7884 duplicate:
c0f3af97
L
7885 i.operands++;
7886 i.reg_operands++;
7887 i.tm.operands++;
7888
91d6fa6a
NC
7889 i.op[dupl] = i.op[dest];
7890 i.types[dupl] = i.types[dest];
7891 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 7892 i.flags[dupl] = i.flags[dest];
c0f3af97
L
7893 }
7894
7895 if (i.tm.opcode_modifier.immext)
7896 process_immext ();
7897 }
75e5731b 7898 else if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 7899 && i.tm.operand_types[0].bitfield.xmmword)
c0f3af97
L
7900 {
7901 unsigned int j;
7902
9fcfb3d7
L
7903 for (j = 1; j < i.operands; j++)
7904 {
7905 i.op[j - 1] = i.op[j];
7906 i.types[j - 1] = i.types[j];
7907
7908 /* We need to adjust fields in i.tm since they are used by
7909 build_modrm_byte. */
7910 i.tm.operand_types [j - 1] = i.tm.operand_types [j];
8dc0818e
JB
7911
7912 i.flags[j - 1] = i.flags[j];
9fcfb3d7
L
7913 }
7914
e2ec9d29
L
7915 i.operands--;
7916 i.reg_operands--;
e2ec9d29
L
7917 i.tm.operands--;
7918 }
255571cd 7919 else if (i.tm.opcode_modifier.operandconstraint == IMPLICIT_QUAD_GROUP)
920d2ddc 7920 {
a477a8c4
JB
7921 unsigned int regnum, first_reg_in_group, last_reg_in_group;
7922
920d2ddc 7923 /* The second operand must be {x,y,z}mmN, where N is a multiple of 4. */
3528c362 7924 gas_assert (i.operands >= 2 && i.types[1].bitfield.class == RegSIMD);
a477a8c4
JB
7925 regnum = register_number (i.op[1].regs);
7926 first_reg_in_group = regnum & ~3;
7927 last_reg_in_group = first_reg_in_group + 3;
7928 if (regnum != first_reg_in_group)
7929 as_warn (_("source register `%s%s' implicitly denotes"
7930 " `%s%.3s%u' to `%s%.3s%u' source group in `%s'"),
7931 register_prefix, i.op[1].regs->reg_name,
7932 register_prefix, i.op[1].regs->reg_name, first_reg_in_group,
7933 register_prefix, i.op[1].regs->reg_name, last_reg_in_group,
7934 i.tm.name);
7935 }
255571cd 7936 else if (i.tm.opcode_modifier.operandconstraint == REG_KLUDGE)
e2ec9d29
L
7937 {
7938 /* The imul $imm, %reg instruction is converted into
7939 imul $imm, %reg, %reg, and the clr %reg instruction
7940 is converted into xor %reg, %reg. */
7941
7942 unsigned int first_reg_op;
7943
7944 if (operand_type_check (i.types[0], reg))
7945 first_reg_op = 0;
7946 else
7947 first_reg_op = 1;
7948 /* Pretend we saw the extra register operand. */
9c2799c2 7949 gas_assert (i.reg_operands == 1
7ab9ffdd 7950 && i.op[first_reg_op + 1].regs == 0);
e2ec9d29
L
7951 i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
7952 i.types[first_reg_op + 1] = i.types[first_reg_op];
7953 i.operands++;
7954 i.reg_operands++;
29b0f896
AM
7955 }
7956
85b80b0f 7957 if (i.tm.opcode_modifier.modrm)
29b0f896
AM
7958 {
7959 /* The opcode is completed (modulo i.tm.extension_opcode which
52271982
AM
7960 must be put into the modrm byte). Now, we make the modrm and
7961 index base bytes based on all the info we've collected. */
29b0f896
AM
7962
7963 default_seg = build_modrm_byte ();
7964 }
00cee14f 7965 else if (i.types[0].bitfield.class == SReg)
85b80b0f
JB
7966 {
7967 if (flag_code != CODE_64BIT
7968 ? i.tm.base_opcode == POP_SEG_SHORT
7969 && i.op[0].regs->reg_num == 1
389d00a5 7970 : (i.tm.base_opcode | 1) == (POP_SEG386_SHORT & 0xff)
85b80b0f
JB
7971 && i.op[0].regs->reg_num < 4)
7972 {
7973 as_bad (_("you can't `%s %s%s'"),
7974 i.tm.name, register_prefix, i.op[0].regs->reg_name);
7975 return 0;
7976 }
389d00a5
JB
7977 if (i.op[0].regs->reg_num > 3
7978 && i.tm.opcode_modifier.opcodespace == SPACE_BASE )
85b80b0f 7979 {
389d00a5
JB
7980 i.tm.base_opcode ^= (POP_SEG_SHORT ^ POP_SEG386_SHORT) & 0xff;
7981 i.tm.opcode_modifier.opcodespace = SPACE_0F;
85b80b0f
JB
7982 }
7983 i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
7984 }
389d00a5
JB
7985 else if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
7986 && (i.tm.base_opcode & ~3) == MOV_AX_DISP32)
29b0f896 7987 {
5e042380 7988 default_seg = reg_ds;
29b0f896 7989 }
40fb9820 7990 else if (i.tm.opcode_modifier.isstring)
29b0f896
AM
7991 {
7992 /* For the string instructions that allow a segment override
7993 on one of their operands, the default segment is ds. */
5e042380 7994 default_seg = reg_ds;
29b0f896 7995 }
50128d0c 7996 else if (i.short_form)
85b80b0f
JB
7997 {
7998 /* The register or float register operand is in operand
7999 0 or 1. */
bab6aec1 8000 unsigned int op = i.tm.operand_types[0].bitfield.class != Reg;
85b80b0f
JB
8001
8002 /* Register goes in low 3 bits of opcode. */
8003 i.tm.base_opcode |= i.op[op].regs->reg_num;
8004 if ((i.op[op].regs->reg_flags & RegRex) != 0)
8005 i.rex |= REX_B;
255571cd 8006 if (!quiet_warnings && i.tm.opcode_modifier.operandconstraint == UGH)
85b80b0f
JB
8007 {
8008 /* Warn about some common errors, but press on regardless.
8009 The first case can be generated by gcc (<= 2.8.1). */
8010 if (i.operands == 2)
8011 {
8012 /* Reversed arguments on faddp, fsubp, etc. */
8013 as_warn (_("translating to `%s %s%s,%s%s'"), i.tm.name,
8014 register_prefix, i.op[!intel_syntax].regs->reg_name,
8015 register_prefix, i.op[intel_syntax].regs->reg_name);
8016 }
8017 else
8018 {
8019 /* Extraneous `l' suffix on fp insn. */
8020 as_warn (_("translating to `%s %s%s'"), i.tm.name,
8021 register_prefix, i.op[0].regs->reg_name);
8022 }
8023 }
8024 }
29b0f896 8025
514a8bb0 8026 if ((i.seg[0] || i.prefix[SEG_PREFIX])
514a8bb0 8027 && i.tm.base_opcode == 0x8d /* lea */
35648716 8028 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
514a8bb0 8029 && !is_any_vex_encoding(&i.tm))
92334ad2
JB
8030 {
8031 if (!quiet_warnings)
8032 as_warn (_("segment override on `%s' is ineffectual"), i.tm.name);
8033 if (optimize)
8034 {
8035 i.seg[0] = NULL;
8036 i.prefix[SEG_PREFIX] = 0;
8037 }
8038 }
52271982
AM
8039
8040 /* If a segment was explicitly specified, and the specified segment
b6773884
JB
8041 is neither the default nor the one already recorded from a prefix,
8042 use an opcode prefix to select it. If we never figured out what
8043 the default segment is, then default_seg will be zero at this
8044 point, and the specified segment prefix will always be used. */
8045 if (i.seg[0]
8046 && i.seg[0] != default_seg
5e042380 8047 && i386_seg_prefixes[i.seg[0]->reg_num] != i.prefix[SEG_PREFIX])
29b0f896 8048 {
5e042380 8049 if (!add_prefix (i386_seg_prefixes[i.seg[0]->reg_num]))
29b0f896
AM
8050 return 0;
8051 }
8052 return 1;
8053}
8054
a5aeccd9 8055static INLINE void set_rex_vrex (const reg_entry *r, unsigned int rex_bit,
5b7c81bd 8056 bool do_sse2avx)
a5aeccd9
JB
8057{
8058 if (r->reg_flags & RegRex)
8059 {
8060 if (i.rex & rex_bit)
8061 as_bad (_("same type of prefix used twice"));
8062 i.rex |= rex_bit;
8063 }
8064 else if (do_sse2avx && (i.rex & rex_bit) && i.vex.register_specifier)
8065 {
8066 gas_assert (i.vex.register_specifier == r);
8067 i.vex.register_specifier += 8;
8068 }
8069
8070 if (r->reg_flags & RegVRex)
8071 i.vrex |= rex_bit;
8072}
8073
5e042380 8074static const reg_entry *
e3bb37b5 8075build_modrm_byte (void)
29b0f896 8076{
5e042380 8077 const reg_entry *default_seg = NULL;
c0f3af97 8078 unsigned int source, dest;
8cd7925b 8079 int vex_3_sources;
c0f3af97 8080
8cd7925b 8081 vex_3_sources = i.tm.opcode_modifier.vexsources == VEX3SOURCES;
c0f3af97
L
8082 if (vex_3_sources)
8083 {
91d6fa6a 8084 unsigned int nds, reg_slot;
4c2c6516 8085 expressionS *exp;
c0f3af97 8086
6b8d3588 8087 dest = i.operands - 1;
c0f3af97 8088 nds = dest - 1;
922d8de8 8089
a683cc34 8090 /* There are 2 kinds of instructions:
bed3d976 8091 1. 5 operands: 4 register operands or 3 register operands
9d3bf266 8092 plus 1 memory operand plus one Imm4 operand, VexXDS, and
bed3d976 8093 VexW0 or VexW1. The destination must be either XMM, YMM or
43234a1e 8094 ZMM register.
bed3d976 8095 2. 4 operands: 4 register operands or 3 register operands
2f1bada2 8096 plus 1 memory operand, with VexXDS. */
922d8de8 8097 gas_assert ((i.reg_operands == 4
bed3d976
JB
8098 || (i.reg_operands == 3 && i.mem_operands == 1))
8099 && i.tm.opcode_modifier.vexvvvv == VEXXDS
dcd7e323 8100 && i.tm.opcode_modifier.vexw
3528c362 8101 && i.tm.operand_types[dest].bitfield.class == RegSIMD);
a683cc34 8102
48db9223
JB
8103 /* If VexW1 is set, the first non-immediate operand is the source and
8104 the second non-immediate one is encoded in the immediate operand. */
8105 if (i.tm.opcode_modifier.vexw == VEXW1)
8106 {
8107 source = i.imm_operands;
8108 reg_slot = i.imm_operands + 1;
8109 }
8110 else
8111 {
8112 source = i.imm_operands + 1;
8113 reg_slot = i.imm_operands;
8114 }
8115
a683cc34 8116 if (i.imm_operands == 0)
bed3d976
JB
8117 {
8118 /* When there is no immediate operand, generate an 8bit
8119 immediate operand to encode the first operand. */
8120 exp = &im_expressions[i.imm_operands++];
8121 i.op[i.operands].imms = exp;
be1643ff 8122 i.types[i.operands].bitfield.imm8 = 1;
bed3d976
JB
8123 i.operands++;
8124
3528c362 8125 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
bed3d976
JB
8126 exp->X_op = O_constant;
8127 exp->X_add_number = register_number (i.op[reg_slot].regs) << 4;
43234a1e
L
8128 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
8129 }
922d8de8 8130 else
bed3d976 8131 {
9d3bf266
JB
8132 gas_assert (i.imm_operands == 1);
8133 gas_assert (fits_in_imm4 (i.op[0].imms->X_add_number));
8134 gas_assert (!i.tm.opcode_modifier.immext);
a683cc34 8135
9d3bf266
JB
8136 /* Turn on Imm8 again so that output_imm will generate it. */
8137 i.types[0].bitfield.imm8 = 1;
bed3d976 8138
3528c362 8139 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
9d3bf266 8140 i.op[0].imms->X_add_number
bed3d976 8141 |= register_number (i.op[reg_slot].regs) << 4;
43234a1e 8142 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
bed3d976 8143 }
a683cc34 8144
3528c362 8145 gas_assert (i.tm.operand_types[nds].bitfield.class == RegSIMD);
dae39acc 8146 i.vex.register_specifier = i.op[nds].regs;
c0f3af97
L
8147 }
8148 else
8149 source = dest = 0;
29b0f896
AM
8150
8151 /* i.reg_operands MUST be the number of real register operands;
c0f3af97
L
8152 implicit registers do not count. If there are 3 register
8153 operands, it must be a instruction with VexNDS. For a
8154 instruction with VexNDD, the destination register is encoded
8155 in VEX prefix. If there are 4 register operands, it must be
8156 a instruction with VEX prefix and 3 sources. */
7ab9ffdd
L
8157 if (i.mem_operands == 0
8158 && ((i.reg_operands == 2
2426c15f 8159 && i.tm.opcode_modifier.vexvvvv <= VEXXDS)
7ab9ffdd 8160 || (i.reg_operands == 3
2426c15f 8161 && i.tm.opcode_modifier.vexvvvv == VEXXDS)
7ab9ffdd 8162 || (i.reg_operands == 4 && vex_3_sources)))
29b0f896 8163 {
cab737b9
L
8164 switch (i.operands)
8165 {
8166 case 2:
8167 source = 0;
8168 break;
8169 case 3:
c81128dc
L
8170 /* When there are 3 operands, one of them may be immediate,
8171 which may be the first or the last operand. Otherwise,
c0f3af97
L
8172 the first operand must be shift count register (cl) or it
8173 is an instruction with VexNDS. */
9c2799c2 8174 gas_assert (i.imm_operands == 1
7ab9ffdd 8175 || (i.imm_operands == 0
2426c15f 8176 && (i.tm.opcode_modifier.vexvvvv == VEXXDS
75e5731b
JB
8177 || (i.types[0].bitfield.instance == RegC
8178 && i.types[0].bitfield.byte))));
40fb9820 8179 if (operand_type_check (i.types[0], imm)
75e5731b
JB
8180 || (i.types[0].bitfield.instance == RegC
8181 && i.types[0].bitfield.byte))
40fb9820
L
8182 source = 1;
8183 else
8184 source = 0;
cab737b9
L
8185 break;
8186 case 4:
368d64cc
L
8187 /* When there are 4 operands, the first two must be 8bit
8188 immediate operands. The source operand will be the 3rd
c0f3af97
L
8189 one.
8190
8191 For instructions with VexNDS, if the first operand
8192 an imm8, the source operand is the 2nd one. If the last
8193 operand is imm8, the source operand is the first one. */
9c2799c2 8194 gas_assert ((i.imm_operands == 2
7ab9ffdd
L
8195 && i.types[0].bitfield.imm8
8196 && i.types[1].bitfield.imm8)
2426c15f 8197 || (i.tm.opcode_modifier.vexvvvv == VEXXDS
7ab9ffdd
L
8198 && i.imm_operands == 1
8199 && (i.types[0].bitfield.imm8
cf665fee 8200 || i.types[i.operands - 1].bitfield.imm8)));
9f2670f2
L
8201 if (i.imm_operands == 2)
8202 source = 2;
8203 else
c0f3af97
L
8204 {
8205 if (i.types[0].bitfield.imm8)
8206 source = 1;
8207 else
8208 source = 0;
8209 }
c0f3af97
L
8210 break;
8211 case 5:
cf665fee
JB
8212 gas_assert (!is_evex_encoding (&i.tm));
8213 gas_assert (i.imm_operands == 1 && vex_3_sources);
cab737b9
L
8214 break;
8215 default:
8216 abort ();
8217 }
8218
c0f3af97
L
8219 if (!vex_3_sources)
8220 {
8221 dest = source + 1;
8222
2426c15f 8223 if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
c0f3af97 8224 {
43234a1e 8225 /* For instructions with VexNDS, the register-only source
c5d0745b 8226 operand must be a 32/64bit integer, XMM, YMM, ZMM, or mask
dfd69174 8227 register. It is encoded in VEX prefix. */
f12dc422
L
8228
8229 i386_operand_type op;
8230 unsigned int vvvv;
8231
c2ecccb3 8232 /* Swap two source operands if needed. */
255571cd 8233 if (i.tm.opcode_modifier.operandconstraint == SWAP_SOURCES)
f12dc422
L
8234 {
8235 vvvv = source;
8236 source = dest;
8237 }
8238 else
8239 vvvv = dest;
8240
8241 op = i.tm.operand_types[vvvv];
c0f3af97 8242 if ((dest + 1) >= i.operands
bab6aec1 8243 || ((op.bitfield.class != Reg
dc821c5f 8244 || (!op.bitfield.dword && !op.bitfield.qword))
3528c362 8245 && op.bitfield.class != RegSIMD
c0f327b8 8246 && op.bitfield.class != RegMask))
c0f3af97 8247 abort ();
f12dc422 8248 i.vex.register_specifier = i.op[vvvv].regs;
c0f3af97
L
8249 dest++;
8250 }
8251 }
29b0f896
AM
8252
8253 i.rm.mode = 3;
dfd69174
JB
8254 /* One of the register operands will be encoded in the i.rm.reg
8255 field, the other in the combined i.rm.mode and i.rm.regmem
29b0f896
AM
8256 fields. If no form of this instruction supports a memory
8257 destination operand, then we assume the source operand may
8258 sometimes be a memory operand and so we need to store the
8259 destination in the i.rm.reg field. */
dfd69174 8260 if (!i.tm.opcode_modifier.regmem
40fb9820 8261 && operand_type_check (i.tm.operand_types[dest], anymem) == 0)
29b0f896
AM
8262 {
8263 i.rm.reg = i.op[dest].regs->reg_num;
8264 i.rm.regmem = i.op[source].regs->reg_num;
a5aeccd9 8265 set_rex_vrex (i.op[dest].regs, REX_R, i.tm.opcode_modifier.sse2avx);
5b7c81bd 8266 set_rex_vrex (i.op[source].regs, REX_B, false);
29b0f896
AM
8267 }
8268 else
8269 {
8270 i.rm.reg = i.op[source].regs->reg_num;
8271 i.rm.regmem = i.op[dest].regs->reg_num;
a5aeccd9 8272 set_rex_vrex (i.op[dest].regs, REX_B, i.tm.opcode_modifier.sse2avx);
5b7c81bd 8273 set_rex_vrex (i.op[source].regs, REX_R, false);
29b0f896 8274 }
e0c7f900 8275 if (flag_code != CODE_64BIT && (i.rex & REX_R))
c4a530c5 8276 {
4a5c67ed 8277 if (i.types[!i.tm.opcode_modifier.regmem].bitfield.class != RegCR)
c4a530c5 8278 abort ();
e0c7f900 8279 i.rex &= ~REX_R;
c4a530c5
JB
8280 add_prefix (LOCK_PREFIX_OPCODE);
8281 }
29b0f896
AM
8282 }
8283 else
8284 { /* If it's not 2 reg operands... */
c0f3af97
L
8285 unsigned int mem;
8286
29b0f896
AM
8287 if (i.mem_operands)
8288 {
8289 unsigned int fake_zero_displacement = 0;
99018f42 8290 unsigned int op;
4eed87de 8291
7ab9ffdd 8292 for (op = 0; op < i.operands; op++)
8dc0818e 8293 if (i.flags[op] & Operand_Mem)
7ab9ffdd 8294 break;
7ab9ffdd 8295 gas_assert (op < i.operands);
29b0f896 8296
63112cd6 8297 if (i.tm.opcode_modifier.sib)
6c30d220 8298 {
260cd341
LC
8299 /* The index register of VSIB shouldn't be RegIZ. */
8300 if (i.tm.opcode_modifier.sib != SIBMEM
8301 && i.index_reg->reg_num == RegIZ)
6c30d220
L
8302 abort ();
8303
8304 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8305 if (!i.base_reg)
8306 {
8307 i.sib.base = NO_BASE_REGISTER;
8308 i.sib.scale = i.log2_scale_factor;
2f2be86b 8309 i.types[op] = operand_type_and_not (i.types[op], anydisp);
a775efc8 8310 i.types[op].bitfield.disp32 = 1;
6c30d220 8311 }
260cd341
LC
8312
8313 /* Since the mandatory SIB always has index register, so
8314 the code logic remains unchanged. The non-mandatory SIB
8315 without index register is allowed and will be handled
8316 later. */
8317 if (i.index_reg)
8318 {
8319 if (i.index_reg->reg_num == RegIZ)
8320 i.sib.index = NO_INDEX_REGISTER;
8321 else
8322 i.sib.index = i.index_reg->reg_num;
5b7c81bd 8323 set_rex_vrex (i.index_reg, REX_X, false);
260cd341 8324 }
6c30d220
L
8325 }
8326
5e042380 8327 default_seg = reg_ds;
29b0f896
AM
8328
8329 if (i.base_reg == 0)
8330 {
8331 i.rm.mode = 0;
8332 if (!i.disp_operands)
9bb129e8 8333 fake_zero_displacement = 1;
29b0f896
AM
8334 if (i.index_reg == 0)
8335 {
260cd341
LC
8336 /* Both check for VSIB and mandatory non-vector SIB. */
8337 gas_assert (!i.tm.opcode_modifier.sib
8338 || i.tm.opcode_modifier.sib == SIBMEM);
29b0f896 8339 /* Operand is just <disp> */
2f2be86b 8340 i.types[op] = operand_type_and_not (i.types[op], anydisp);
20f0a1fc 8341 if (flag_code == CODE_64BIT)
29b0f896
AM
8342 {
8343 /* 64bit mode overwrites the 32bit absolute
8344 addressing by RIP relative addressing and
8345 absolute addressing is encoded by one of the
8346 redundant SIB forms. */
8347 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8348 i.sib.base = NO_BASE_REGISTER;
8349 i.sib.index = NO_INDEX_REGISTER;
a775efc8 8350 i.types[op].bitfield.disp32 = 1;
20f0a1fc 8351 }
fc225355
L
8352 else if ((flag_code == CODE_16BIT)
8353 ^ (i.prefix[ADDR_PREFIX] != 0))
20f0a1fc
NC
8354 {
8355 i.rm.regmem = NO_BASE_REGISTER_16;
2f2be86b 8356 i.types[op].bitfield.disp16 = 1;
20f0a1fc
NC
8357 }
8358 else
8359 {
8360 i.rm.regmem = NO_BASE_REGISTER;
2f2be86b 8361 i.types[op].bitfield.disp32 = 1;
29b0f896
AM
8362 }
8363 }
63112cd6 8364 else if (!i.tm.opcode_modifier.sib)
29b0f896 8365 {
6c30d220 8366 /* !i.base_reg && i.index_reg */
e968fc9b 8367 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8368 i.sib.index = NO_INDEX_REGISTER;
8369 else
8370 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8371 i.sib.base = NO_BASE_REGISTER;
8372 i.sib.scale = i.log2_scale_factor;
8373 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
2f2be86b 8374 i.types[op] = operand_type_and_not (i.types[op], anydisp);
a775efc8 8375 i.types[op].bitfield.disp32 = 1;
29b0f896 8376 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8377 i.rex |= REX_X;
29b0f896
AM
8378 }
8379 }
8380 /* RIP addressing for 64bit mode. */
e968fc9b 8381 else if (i.base_reg->reg_num == RegIP)
29b0f896 8382 {
63112cd6 8383 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896 8384 i.rm.regmem = NO_BASE_REGISTER;
40fb9820
L
8385 i.types[op].bitfield.disp8 = 0;
8386 i.types[op].bitfield.disp16 = 0;
a775efc8 8387 i.types[op].bitfield.disp32 = 1;
40fb9820 8388 i.types[op].bitfield.disp64 = 0;
71903a11 8389 i.flags[op] |= Operand_PCrel;
20f0a1fc
NC
8390 if (! i.disp_operands)
8391 fake_zero_displacement = 1;
29b0f896 8392 }
dc821c5f 8393 else if (i.base_reg->reg_type.bitfield.word)
29b0f896 8394 {
63112cd6 8395 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896
AM
8396 switch (i.base_reg->reg_num)
8397 {
8398 case 3: /* (%bx) */
8399 if (i.index_reg == 0)
8400 i.rm.regmem = 7;
8401 else /* (%bx,%si) -> 0, or (%bx,%di) -> 1 */
8402 i.rm.regmem = i.index_reg->reg_num - 6;
8403 break;
8404 case 5: /* (%bp) */
5e042380 8405 default_seg = reg_ss;
29b0f896
AM
8406 if (i.index_reg == 0)
8407 {
8408 i.rm.regmem = 6;
40fb9820 8409 if (operand_type_check (i.types[op], disp) == 0)
29b0f896
AM
8410 {
8411 /* fake (%bp) into 0(%bp) */
41eb8e88 8412 if (i.disp_encoding == disp_encoding_16bit)
1a02d6b0
L
8413 i.types[op].bitfield.disp16 = 1;
8414 else
8415 i.types[op].bitfield.disp8 = 1;
252b5132 8416 fake_zero_displacement = 1;
29b0f896
AM
8417 }
8418 }
8419 else /* (%bp,%si) -> 2, or (%bp,%di) -> 3 */
8420 i.rm.regmem = i.index_reg->reg_num - 6 + 2;
8421 break;
8422 default: /* (%si) -> 4 or (%di) -> 5 */
8423 i.rm.regmem = i.base_reg->reg_num - 6 + 4;
8424 }
41eb8e88
L
8425 if (!fake_zero_displacement
8426 && !i.disp_operands
8427 && i.disp_encoding)
8428 {
8429 fake_zero_displacement = 1;
8430 if (i.disp_encoding == disp_encoding_8bit)
8431 i.types[op].bitfield.disp8 = 1;
8432 else
8433 i.types[op].bitfield.disp16 = 1;
8434 }
29b0f896
AM
8435 i.rm.mode = mode_from_disp_size (i.types[op]);
8436 }
8437 else /* i.base_reg and 32/64 bit mode */
8438 {
a9aabc23 8439 if (operand_type_check (i.types[op], disp))
40fb9820 8440 {
73053c1f
JB
8441 i.types[op].bitfield.disp16 = 0;
8442 i.types[op].bitfield.disp64 = 0;
a775efc8 8443 i.types[op].bitfield.disp32 = 1;
40fb9820 8444 }
20f0a1fc 8445
63112cd6 8446 if (!i.tm.opcode_modifier.sib)
6c30d220 8447 i.rm.regmem = i.base_reg->reg_num;
29b0f896 8448 if ((i.base_reg->reg_flags & RegRex) != 0)
161a04f6 8449 i.rex |= REX_B;
29b0f896
AM
8450 i.sib.base = i.base_reg->reg_num;
8451 /* x86-64 ignores REX prefix bit here to avoid decoder
8452 complications. */
848930b2
JB
8453 if (!(i.base_reg->reg_flags & RegRex)
8454 && (i.base_reg->reg_num == EBP_REG_NUM
8455 || i.base_reg->reg_num == ESP_REG_NUM))
5e042380 8456 default_seg = reg_ss;
848930b2 8457 if (i.base_reg->reg_num == 5 && i.disp_operands == 0)
29b0f896 8458 {
848930b2 8459 fake_zero_displacement = 1;
1a02d6b0
L
8460 if (i.disp_encoding == disp_encoding_32bit)
8461 i.types[op].bitfield.disp32 = 1;
8462 else
8463 i.types[op].bitfield.disp8 = 1;
29b0f896
AM
8464 }
8465 i.sib.scale = i.log2_scale_factor;
8466 if (i.index_reg == 0)
8467 {
260cd341
LC
8468 /* Only check for VSIB. */
8469 gas_assert (i.tm.opcode_modifier.sib != VECSIB128
8470 && i.tm.opcode_modifier.sib != VECSIB256
8471 && i.tm.opcode_modifier.sib != VECSIB512);
8472
29b0f896
AM
8473 /* <disp>(%esp) becomes two byte modrm with no index
8474 register. We've already stored the code for esp
8475 in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
8476 Any base register besides %esp will not use the
8477 extra modrm byte. */
8478 i.sib.index = NO_INDEX_REGISTER;
29b0f896 8479 }
63112cd6 8480 else if (!i.tm.opcode_modifier.sib)
29b0f896 8481 {
e968fc9b 8482 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8483 i.sib.index = NO_INDEX_REGISTER;
8484 else
8485 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8486 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8487 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8488 i.rex |= REX_X;
29b0f896 8489 }
67a4f2b7
AO
8490
8491 if (i.disp_operands
8492 && (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
8493 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL))
8494 i.rm.mode = 0;
8495 else
a501d77e
L
8496 {
8497 if (!fake_zero_displacement
8498 && !i.disp_operands
8499 && i.disp_encoding)
8500 {
8501 fake_zero_displacement = 1;
8502 if (i.disp_encoding == disp_encoding_8bit)
8503 i.types[op].bitfield.disp8 = 1;
8504 else
8505 i.types[op].bitfield.disp32 = 1;
8506 }
8507 i.rm.mode = mode_from_disp_size (i.types[op]);
8508 }
29b0f896 8509 }
252b5132 8510
29b0f896
AM
8511 if (fake_zero_displacement)
8512 {
8513 /* Fakes a zero displacement assuming that i.types[op]
8514 holds the correct displacement size. */
8515 expressionS *exp;
8516
9c2799c2 8517 gas_assert (i.op[op].disps == 0);
29b0f896
AM
8518 exp = &disp_expressions[i.disp_operands++];
8519 i.op[op].disps = exp;
8520 exp->X_op = O_constant;
8521 exp->X_add_number = 0;
8522 exp->X_add_symbol = (symbolS *) 0;
8523 exp->X_op_symbol = (symbolS *) 0;
8524 }
c0f3af97
L
8525
8526 mem = op;
29b0f896 8527 }
c0f3af97
L
8528 else
8529 mem = ~0;
252b5132 8530
8c43a48b 8531 if (i.tm.opcode_modifier.vexsources == XOP2SOURCES)
5dd85c99
SP
8532 {
8533 if (operand_type_check (i.types[0], imm))
8534 i.vex.register_specifier = NULL;
8535 else
8536 {
8537 /* VEX.vvvv encodes one of the sources when the first
8538 operand is not an immediate. */
1ef99a7b 8539 if (i.tm.opcode_modifier.vexw == VEXW0)
5dd85c99
SP
8540 i.vex.register_specifier = i.op[0].regs;
8541 else
8542 i.vex.register_specifier = i.op[1].regs;
8543 }
8544
8545 /* Destination is a XMM register encoded in the ModRM.reg
8546 and VEX.R bit. */
8547 i.rm.reg = i.op[2].regs->reg_num;
8548 if ((i.op[2].regs->reg_flags & RegRex) != 0)
8549 i.rex |= REX_R;
8550
8551 /* ModRM.rm and VEX.B encodes the other source. */
8552 if (!i.mem_operands)
8553 {
8554 i.rm.mode = 3;
8555
1ef99a7b 8556 if (i.tm.opcode_modifier.vexw == VEXW0)
5dd85c99
SP
8557 i.rm.regmem = i.op[1].regs->reg_num;
8558 else
8559 i.rm.regmem = i.op[0].regs->reg_num;
8560
8561 if ((i.op[1].regs->reg_flags & RegRex) != 0)
8562 i.rex |= REX_B;
8563 }
8564 }
2426c15f 8565 else if (i.tm.opcode_modifier.vexvvvv == VEXLWP)
f88c9eb0
SP
8566 {
8567 i.vex.register_specifier = i.op[2].regs;
8568 if (!i.mem_operands)
8569 {
8570 i.rm.mode = 3;
8571 i.rm.regmem = i.op[1].regs->reg_num;
8572 if ((i.op[1].regs->reg_flags & RegRex) != 0)
8573 i.rex |= REX_B;
8574 }
8575 }
29b0f896
AM
8576 /* Fill in i.rm.reg or i.rm.regmem field with register operand
8577 (if any) based on i.tm.extension_opcode. Again, we must be
8578 careful to make sure that segment/control/debug/test/MMX
8579 registers are coded into the i.rm.reg field. */
f88c9eb0 8580 else if (i.reg_operands)
29b0f896 8581 {
99018f42 8582 unsigned int op;
7ab9ffdd
L
8583 unsigned int vex_reg = ~0;
8584
8585 for (op = 0; op < i.operands; op++)
921eafea
L
8586 if (i.types[op].bitfield.class == Reg
8587 || i.types[op].bitfield.class == RegBND
8588 || i.types[op].bitfield.class == RegMask
8589 || i.types[op].bitfield.class == SReg
8590 || i.types[op].bitfield.class == RegCR
8591 || i.types[op].bitfield.class == RegDR
8592 || i.types[op].bitfield.class == RegTR
8593 || i.types[op].bitfield.class == RegSIMD
8594 || i.types[op].bitfield.class == RegMMX)
8595 break;
c0209578 8596
7ab9ffdd
L
8597 if (vex_3_sources)
8598 op = dest;
2426c15f 8599 else if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
7ab9ffdd
L
8600 {
8601 /* For instructions with VexNDS, the register-only
8602 source operand is encoded in VEX prefix. */
8603 gas_assert (mem != (unsigned int) ~0);
c0f3af97 8604
a93e3234 8605 if (op > mem || i.tm.cpu_flags.bitfield.cpucmpccxadd)
c0f3af97 8606 {
7ab9ffdd
L
8607 vex_reg = op++;
8608 gas_assert (op < i.operands);
c0f3af97
L
8609 }
8610 else
c0f3af97 8611 {
f12dc422
L
8612 /* Check register-only source operand when two source
8613 operands are swapped. */
8614 if (!i.tm.operand_types[op].bitfield.baseindex
8615 && i.tm.operand_types[op + 1].bitfield.baseindex)
8616 {
8617 vex_reg = op;
8618 op += 2;
8619 gas_assert (mem == (vex_reg + 1)
8620 && op < i.operands);
8621 }
8622 else
8623 {
8624 vex_reg = op + 1;
8625 gas_assert (vex_reg < i.operands);
8626 }
c0f3af97 8627 }
7ab9ffdd 8628 }
2426c15f 8629 else if (i.tm.opcode_modifier.vexvvvv == VEXNDD)
7ab9ffdd 8630 {
f12dc422 8631 /* For instructions with VexNDD, the register destination
7ab9ffdd 8632 is encoded in VEX prefix. */
f12dc422
L
8633 if (i.mem_operands == 0)
8634 {
8635 /* There is no memory operand. */
8636 gas_assert ((op + 2) == i.operands);
8637 vex_reg = op + 1;
8638 }
8639 else
8d63c93e 8640 {
ed438a93
JB
8641 /* There are only 2 non-immediate operands. */
8642 gas_assert (op < i.imm_operands + 2
8643 && i.operands == i.imm_operands + 2);
8644 vex_reg = i.imm_operands + 1;
f12dc422 8645 }
7ab9ffdd
L
8646 }
8647 else
8648 gas_assert (op < i.operands);
99018f42 8649
7ab9ffdd
L
8650 if (vex_reg != (unsigned int) ~0)
8651 {
f12dc422 8652 i386_operand_type *type = &i.tm.operand_types[vex_reg];
7ab9ffdd 8653
bab6aec1 8654 if ((type->bitfield.class != Reg
dc821c5f 8655 || (!type->bitfield.dword && !type->bitfield.qword))
3528c362 8656 && type->bitfield.class != RegSIMD
c0f327b8 8657 && type->bitfield.class != RegMask)
7ab9ffdd 8658 abort ();
f88c9eb0 8659
7ab9ffdd
L
8660 i.vex.register_specifier = i.op[vex_reg].regs;
8661 }
8662
1b9f0c97
L
8663 /* Don't set OP operand twice. */
8664 if (vex_reg != op)
7ab9ffdd 8665 {
1b9f0c97
L
8666 /* If there is an extension opcode to put here, the
8667 register number must be put into the regmem field. */
8668 if (i.tm.extension_opcode != None)
8669 {
8670 i.rm.regmem = i.op[op].regs->reg_num;
a5aeccd9
JB
8671 set_rex_vrex (i.op[op].regs, REX_B,
8672 i.tm.opcode_modifier.sse2avx);
1b9f0c97
L
8673 }
8674 else
8675 {
8676 i.rm.reg = i.op[op].regs->reg_num;
a5aeccd9
JB
8677 set_rex_vrex (i.op[op].regs, REX_R,
8678 i.tm.opcode_modifier.sse2avx);
1b9f0c97 8679 }
7ab9ffdd 8680 }
252b5132 8681
29b0f896
AM
8682 /* Now, if no memory operand has set i.rm.mode = 0, 1, 2 we
8683 must set it to 3 to indicate this is a register operand
8684 in the regmem field. */
8685 if (!i.mem_operands)
8686 i.rm.mode = 3;
8687 }
252b5132 8688
29b0f896 8689 /* Fill in i.rm.reg field with extension opcode (if any). */
c1e679ec 8690 if (i.tm.extension_opcode != None)
29b0f896
AM
8691 i.rm.reg = i.tm.extension_opcode;
8692 }
8693 return default_seg;
8694}
252b5132 8695
48ef937e
JB
8696static INLINE void
8697frag_opcode_byte (unsigned char byte)
8698{
8699 if (now_seg != absolute_section)
8700 FRAG_APPEND_1_CHAR (byte);
8701 else
8702 ++abs_section_offset;
8703}
8704
376cd056
JB
8705static unsigned int
8706flip_code16 (unsigned int code16)
8707{
8708 gas_assert (i.tm.operands == 1);
8709
8710 return !(i.prefix[REX_PREFIX] & REX_W)
8711 && (code16 ? i.tm.operand_types[0].bitfield.disp32
376cd056
JB
8712 : i.tm.operand_types[0].bitfield.disp16)
8713 ? CODE16 : 0;
8714}
8715
29b0f896 8716static void
e3bb37b5 8717output_branch (void)
29b0f896
AM
8718{
8719 char *p;
f8a5c266 8720 int size;
29b0f896
AM
8721 int code16;
8722 int prefix;
8723 relax_substateT subtype;
8724 symbolS *sym;
8725 offsetT off;
8726
48ef937e
JB
8727 if (now_seg == absolute_section)
8728 {
8729 as_bad (_("relaxable branches not supported in absolute section"));
8730 return;
8731 }
8732
f8a5c266 8733 code16 = flag_code == CODE_16BIT ? CODE16 : 0;
1a42a9fe 8734 size = i.disp_encoding > disp_encoding_8bit ? BIG : SMALL;
29b0f896
AM
8735
8736 prefix = 0;
8737 if (i.prefix[DATA_PREFIX] != 0)
252b5132 8738 {
29b0f896
AM
8739 prefix = 1;
8740 i.prefixes -= 1;
376cd056 8741 code16 ^= flip_code16(code16);
252b5132 8742 }
29b0f896
AM
8743 /* Pentium4 branch hints. */
8744 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8745 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
2f66722d 8746 {
29b0f896
AM
8747 prefix++;
8748 i.prefixes--;
8749 }
8750 if (i.prefix[REX_PREFIX] != 0)
8751 {
8752 prefix++;
8753 i.prefixes--;
2f66722d
AM
8754 }
8755
7e8b059b
L
8756 /* BND prefixed jump. */
8757 if (i.prefix[BND_PREFIX] != 0)
8758 {
6cb0a70e
JB
8759 prefix++;
8760 i.prefixes--;
7e8b059b
L
8761 }
8762
f2810fe0
JB
8763 if (i.prefixes != 0)
8764 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
29b0f896
AM
8765
8766 /* It's always a symbol; End frag & setup for relax.
8767 Make sure there is enough room in this frag for the largest
8768 instruction we may generate in md_convert_frag. This is 2
8769 bytes for the opcode and room for the prefix and largest
8770 displacement. */
8771 frag_grow (prefix + 2 + 4);
8772 /* Prefix and 1 opcode byte go in fr_fix. */
8773 p = frag_more (prefix + 1);
8774 if (i.prefix[DATA_PREFIX] != 0)
8775 *p++ = DATA_PREFIX_OPCODE;
8776 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
8777 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
8778 *p++ = i.prefix[SEG_PREFIX];
6cb0a70e
JB
8779 if (i.prefix[BND_PREFIX] != 0)
8780 *p++ = BND_PREFIX_OPCODE;
29b0f896
AM
8781 if (i.prefix[REX_PREFIX] != 0)
8782 *p++ = i.prefix[REX_PREFIX];
8783 *p = i.tm.base_opcode;
8784
8785 if ((unsigned char) *p == JUMP_PC_RELATIVE)
f8a5c266 8786 subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, size);
40fb9820 8787 else if (cpu_arch_flags.bitfield.cpui386)
f8a5c266 8788 subtype = ENCODE_RELAX_STATE (COND_JUMP, size);
29b0f896 8789 else
f8a5c266 8790 subtype = ENCODE_RELAX_STATE (COND_JUMP86, size);
29b0f896 8791 subtype |= code16;
3e73aa7c 8792
29b0f896
AM
8793 sym = i.op[0].disps->X_add_symbol;
8794 off = i.op[0].disps->X_add_number;
3e73aa7c 8795
29b0f896
AM
8796 if (i.op[0].disps->X_op != O_constant
8797 && i.op[0].disps->X_op != O_symbol)
3e73aa7c 8798 {
29b0f896
AM
8799 /* Handle complex expressions. */
8800 sym = make_expr_symbol (i.op[0].disps);
8801 off = 0;
8802 }
3e73aa7c 8803
1ef3994a
JB
8804 frag_now->tc_frag_data.code64 = flag_code == CODE_64BIT;
8805
29b0f896
AM
8806 /* 1 possible extra opcode + 4 byte displacement go in var part.
8807 Pass reloc in fr_var. */
d258b828 8808 frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
29b0f896 8809}
3e73aa7c 8810
bd7ab16b
L
8811#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8812/* Return TRUE iff PLT32 relocation should be used for branching to
8813 symbol S. */
8814
5b7c81bd 8815static bool
bd7ab16b
L
8816need_plt32_p (symbolS *s)
8817{
8818 /* PLT32 relocation is ELF only. */
8819 if (!IS_ELF)
5b7c81bd 8820 return false;
bd7ab16b 8821
a5def729
RO
8822#ifdef TE_SOLARIS
8823 /* Don't emit PLT32 relocation on Solaris: neither native linker nor
8824 krtld support it. */
5b7c81bd 8825 return false;
a5def729
RO
8826#endif
8827
bd7ab16b
L
8828 /* Since there is no need to prepare for PLT branch on x86-64, we
8829 can generate R_X86_64_PLT32, instead of R_X86_64_PC32, which can
8830 be used as a marker for 32-bit PC-relative branches. */
8831 if (!object_64bit)
5b7c81bd 8832 return false;
bd7ab16b 8833
44365e88 8834 if (s == NULL)
5b7c81bd 8835 return false;
44365e88 8836
bd7ab16b
L
8837 /* Weak or undefined symbol need PLT32 relocation. */
8838 if (S_IS_WEAK (s) || !S_IS_DEFINED (s))
5b7c81bd 8839 return true;
bd7ab16b
L
8840
8841 /* Non-global symbol doesn't need PLT32 relocation. */
8842 if (! S_IS_EXTERNAL (s))
5b7c81bd 8843 return false;
bd7ab16b
L
8844
8845 /* Other global symbols need PLT32 relocation. NB: Symbol with
8846 non-default visibilities are treated as normal global symbol
8847 so that PLT32 relocation can be used as a marker for 32-bit
8848 PC-relative branches. It is useful for linker relaxation. */
5b7c81bd 8849 return true;
bd7ab16b
L
8850}
8851#endif
8852
29b0f896 8853static void
e3bb37b5 8854output_jump (void)
29b0f896
AM
8855{
8856 char *p;
8857 int size;
3e02c1cc 8858 fixS *fixP;
bd7ab16b 8859 bfd_reloc_code_real_type jump_reloc = i.reloc[0];
29b0f896 8860
0cfa3eb3 8861 if (i.tm.opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
8862 {
8863 /* This is a loop or jecxz type instruction. */
8864 size = 1;
8865 if (i.prefix[ADDR_PREFIX] != 0)
8866 {
48ef937e 8867 frag_opcode_byte (ADDR_PREFIX_OPCODE);
29b0f896
AM
8868 i.prefixes -= 1;
8869 }
8870 /* Pentium4 branch hints. */
8871 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8872 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
8873 {
48ef937e 8874 frag_opcode_byte (i.prefix[SEG_PREFIX]);
29b0f896 8875 i.prefixes--;
3e73aa7c
JH
8876 }
8877 }
29b0f896
AM
8878 else
8879 {
8880 int code16;
3e73aa7c 8881
29b0f896
AM
8882 code16 = 0;
8883 if (flag_code == CODE_16BIT)
8884 code16 = CODE16;
3e73aa7c 8885
29b0f896
AM
8886 if (i.prefix[DATA_PREFIX] != 0)
8887 {
48ef937e 8888 frag_opcode_byte (DATA_PREFIX_OPCODE);
29b0f896 8889 i.prefixes -= 1;
376cd056 8890 code16 ^= flip_code16(code16);
29b0f896 8891 }
252b5132 8892
29b0f896
AM
8893 size = 4;
8894 if (code16)
8895 size = 2;
8896 }
9fcc94b6 8897
6cb0a70e
JB
8898 /* BND prefixed jump. */
8899 if (i.prefix[BND_PREFIX] != 0)
29b0f896 8900 {
48ef937e 8901 frag_opcode_byte (i.prefix[BND_PREFIX]);
29b0f896
AM
8902 i.prefixes -= 1;
8903 }
252b5132 8904
6cb0a70e 8905 if (i.prefix[REX_PREFIX] != 0)
7e8b059b 8906 {
48ef937e 8907 frag_opcode_byte (i.prefix[REX_PREFIX]);
7e8b059b
L
8908 i.prefixes -= 1;
8909 }
8910
f2810fe0
JB
8911 if (i.prefixes != 0)
8912 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
e0890092 8913
48ef937e
JB
8914 if (now_seg == absolute_section)
8915 {
9a182d04 8916 abs_section_offset += i.opcode_length + size;
48ef937e
JB
8917 return;
8918 }
8919
9a182d04
JB
8920 p = frag_more (i.opcode_length + size);
8921 switch (i.opcode_length)
42164a71
L
8922 {
8923 case 2:
8924 *p++ = i.tm.base_opcode >> 8;
1a0670f3 8925 /* Fall through. */
42164a71
L
8926 case 1:
8927 *p++ = i.tm.base_opcode;
8928 break;
8929 default:
8930 abort ();
8931 }
e0890092 8932
bd7ab16b 8933#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1ef3994a
JB
8934 if (flag_code == CODE_64BIT && size == 4
8935 && jump_reloc == NO_RELOC && i.op[0].disps->X_add_number == 0
bd7ab16b
L
8936 && need_plt32_p (i.op[0].disps->X_add_symbol))
8937 jump_reloc = BFD_RELOC_X86_64_PLT32;
8938#endif
8939
8940 jump_reloc = reloc (size, 1, 1, jump_reloc);
8941
3e02c1cc 8942 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
bd7ab16b 8943 i.op[0].disps, 1, jump_reloc);
3e02c1cc 8944
eb19308f
JB
8945 /* All jumps handled here are signed, but don't unconditionally use a
8946 signed limit check for 32 and 16 bit jumps as we want to allow wrap
8947 around at 4G (outside of 64-bit mode) and 64k (except for XBEGIN)
8948 respectively. */
8949 switch (size)
8950 {
8951 case 1:
8952 fixP->fx_signed = 1;
8953 break;
8954
8955 case 2:
8956 if (i.tm.base_opcode == 0xc7f8)
8957 fixP->fx_signed = 1;
8958 break;
8959
8960 case 4:
8961 if (flag_code == CODE_64BIT)
8962 fixP->fx_signed = 1;
8963 break;
8964 }
29b0f896 8965}
e0890092 8966
29b0f896 8967static void
e3bb37b5 8968output_interseg_jump (void)
29b0f896
AM
8969{
8970 char *p;
8971 int size;
8972 int prefix;
8973 int code16;
252b5132 8974
29b0f896
AM
8975 code16 = 0;
8976 if (flag_code == CODE_16BIT)
8977 code16 = CODE16;
a217f122 8978
29b0f896
AM
8979 prefix = 0;
8980 if (i.prefix[DATA_PREFIX] != 0)
8981 {
8982 prefix = 1;
8983 i.prefixes -= 1;
8984 code16 ^= CODE16;
8985 }
6cb0a70e
JB
8986
8987 gas_assert (!i.prefix[REX_PREFIX]);
252b5132 8988
29b0f896
AM
8989 size = 4;
8990 if (code16)
8991 size = 2;
252b5132 8992
f2810fe0
JB
8993 if (i.prefixes != 0)
8994 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
252b5132 8995
48ef937e
JB
8996 if (now_seg == absolute_section)
8997 {
8998 abs_section_offset += prefix + 1 + 2 + size;
8999 return;
9000 }
9001
29b0f896
AM
9002 /* 1 opcode; 2 segment; offset */
9003 p = frag_more (prefix + 1 + 2 + size);
3e73aa7c 9004
29b0f896
AM
9005 if (i.prefix[DATA_PREFIX] != 0)
9006 *p++ = DATA_PREFIX_OPCODE;
252b5132 9007
29b0f896
AM
9008 if (i.prefix[REX_PREFIX] != 0)
9009 *p++ = i.prefix[REX_PREFIX];
252b5132 9010
29b0f896
AM
9011 *p++ = i.tm.base_opcode;
9012 if (i.op[1].imms->X_op == O_constant)
9013 {
9014 offsetT n = i.op[1].imms->X_add_number;
252b5132 9015
29b0f896
AM
9016 if (size == 2
9017 && !fits_in_unsigned_word (n)
9018 && !fits_in_signed_word (n))
9019 {
9020 as_bad (_("16-bit jump out of range"));
9021 return;
9022 }
9023 md_number_to_chars (p, n, size);
9024 }
9025 else
9026 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
d258b828 9027 i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
6d96a594
C
9028
9029 p += size;
9030 if (i.op[0].imms->X_op == O_constant)
9031 md_number_to_chars (p, (valueT) i.op[0].imms->X_add_number, 2);
9032 else
9033 fix_new_exp (frag_now, p - frag_now->fr_literal, 2,
9034 i.op[0].imms, 0, reloc (2, 0, 0, i.reloc[0]));
29b0f896 9035}
a217f122 9036
b4a3a7b4
L
9037#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9038void
9039x86_cleanup (void)
9040{
9041 char *p;
9042 asection *seg = now_seg;
9043 subsegT subseg = now_subseg;
9044 asection *sec;
9045 unsigned int alignment, align_size_1;
9046 unsigned int isa_1_descsz, feature_2_descsz, descsz;
9047 unsigned int isa_1_descsz_raw, feature_2_descsz_raw;
9048 unsigned int padding;
9049
1273b2f8 9050 if (!IS_ELF || !x86_used_note)
b4a3a7b4
L
9051 return;
9052
b4a3a7b4
L
9053 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X86;
9054
9055 /* The .note.gnu.property section layout:
9056
9057 Field Length Contents
9058 ---- ---- ----
9059 n_namsz 4 4
9060 n_descsz 4 The note descriptor size
9061 n_type 4 NT_GNU_PROPERTY_TYPE_0
9062 n_name 4 "GNU"
9063 n_desc n_descsz The program property array
9064 .... .... ....
9065 */
9066
9067 /* Create the .note.gnu.property section. */
9068 sec = subseg_new (NOTE_GNU_PROPERTY_SECTION_NAME, 0);
fd361982 9069 bfd_set_section_flags (sec,
b4a3a7b4
L
9070 (SEC_ALLOC
9071 | SEC_LOAD
9072 | SEC_DATA
9073 | SEC_HAS_CONTENTS
9074 | SEC_READONLY));
9075
9076 if (get_elf_backend_data (stdoutput)->s->elfclass == ELFCLASS64)
9077 {
9078 align_size_1 = 7;
9079 alignment = 3;
9080 }
9081 else
9082 {
9083 align_size_1 = 3;
9084 alignment = 2;
9085 }
9086
fd361982 9087 bfd_set_section_alignment (sec, alignment);
b4a3a7b4
L
9088 elf_section_type (sec) = SHT_NOTE;
9089
1273b2f8
L
9090 /* GNU_PROPERTY_X86_ISA_1_USED: 4-byte type + 4-byte data size
9091 + 4-byte data */
9092 isa_1_descsz_raw = 4 + 4 + 4;
9093 /* Align GNU_PROPERTY_X86_ISA_1_USED. */
9094 isa_1_descsz = (isa_1_descsz_raw + align_size_1) & ~align_size_1;
b4a3a7b4
L
9095
9096 feature_2_descsz_raw = isa_1_descsz;
9097 /* GNU_PROPERTY_X86_FEATURE_2_USED: 4-byte type + 4-byte data size
1273b2f8 9098 + 4-byte data */
b4a3a7b4
L
9099 feature_2_descsz_raw += 4 + 4 + 4;
9100 /* Align GNU_PROPERTY_X86_FEATURE_2_USED. */
9101 feature_2_descsz = ((feature_2_descsz_raw + align_size_1)
9102 & ~align_size_1);
9103
9104 descsz = feature_2_descsz;
9105 /* Section size: n_namsz + n_descsz + n_type + n_name + n_descsz. */
9106 p = frag_more (4 + 4 + 4 + 4 + descsz);
9107
9108 /* Write n_namsz. */
9109 md_number_to_chars (p, (valueT) 4, 4);
9110
9111 /* Write n_descsz. */
9112 md_number_to_chars (p + 4, (valueT) descsz, 4);
9113
9114 /* Write n_type. */
9115 md_number_to_chars (p + 4 * 2, (valueT) NT_GNU_PROPERTY_TYPE_0, 4);
9116
9117 /* Write n_name. */
9118 memcpy (p + 4 * 3, "GNU", 4);
9119
1273b2f8
L
9120 /* Write 4-byte type. */
9121 md_number_to_chars (p + 4 * 4,
9122 (valueT) GNU_PROPERTY_X86_ISA_1_USED, 4);
b4a3a7b4 9123
1273b2f8
L
9124 /* Write 4-byte data size. */
9125 md_number_to_chars (p + 4 * 5, (valueT) 4, 4);
b4a3a7b4 9126
1273b2f8
L
9127 /* Write 4-byte data. */
9128 md_number_to_chars (p + 4 * 6, (valueT) x86_isa_1_used, 4);
b4a3a7b4 9129
1273b2f8
L
9130 /* Zero out paddings. */
9131 padding = isa_1_descsz - isa_1_descsz_raw;
9132 if (padding)
9133 memset (p + 4 * 7, 0, padding);
b4a3a7b4
L
9134
9135 /* Write 4-byte type. */
9136 md_number_to_chars (p + isa_1_descsz + 4 * 4,
9137 (valueT) GNU_PROPERTY_X86_FEATURE_2_USED, 4);
9138
9139 /* Write 4-byte data size. */
9140 md_number_to_chars (p + isa_1_descsz + 4 * 5, (valueT) 4, 4);
9141
9142 /* Write 4-byte data. */
9143 md_number_to_chars (p + isa_1_descsz + 4 * 6,
9144 (valueT) x86_feature_2_used, 4);
9145
9146 /* Zero out paddings. */
9147 padding = feature_2_descsz - feature_2_descsz_raw;
9148 if (padding)
9149 memset (p + isa_1_descsz + 4 * 7, 0, padding);
9150
9151 /* We probably can't restore the current segment, for there likely
9152 isn't one yet... */
9153 if (seg && subseg)
9154 subseg_set (seg, subseg);
9155}
b52c4ee4
IB
9156
9157bool
9158x86_support_sframe_p (void)
9159{
9160 /* At this time, SFrame unwind is supported for AMD64 ABI only. */
9161 return (x86_elf_abi == X86_64_ABI);
9162}
9163
9164bool
9165x86_sframe_ra_tracking_p (void)
9166{
9167 /* In AMD64, return address is always stored on the stack at a fixed offset
9168 from the CFA (provided via x86_sframe_cfa_ra_offset ()).
9169 Do not track explicitly via an SFrame Frame Row Entry. */
9170 return false;
9171}
9172
9173offsetT
9174x86_sframe_cfa_ra_offset (void)
9175{
9176 gas_assert (x86_elf_abi == X86_64_ABI);
9177 return (offsetT) -8;
9178}
9179
9180unsigned char
9181x86_sframe_get_abi_arch (void)
9182{
9183 unsigned char sframe_abi_arch = 0;
9184
9185 if (x86_support_sframe_p ())
9186 {
9187 gas_assert (!target_big_endian);
9188 sframe_abi_arch = SFRAME_ABI_AMD64_ENDIAN_LITTLE;
9189 }
9190
9191 return sframe_abi_arch;
9192}
9193
b4a3a7b4
L
9194#endif
9195
9c33702b
JB
9196static unsigned int
9197encoding_length (const fragS *start_frag, offsetT start_off,
9198 const char *frag_now_ptr)
9199{
9200 unsigned int len = 0;
9201
9202 if (start_frag != frag_now)
9203 {
9204 const fragS *fr = start_frag;
9205
9206 do {
9207 len += fr->fr_fix;
9208 fr = fr->fr_next;
9209 } while (fr && fr != frag_now);
9210 }
9211
9212 return len - start_off + (frag_now_ptr - frag_now->fr_literal);
9213}
9214
e379e5f3 9215/* Return 1 for test, and, cmp, add, sub, inc and dec which may
79d72f45
HL
9216 be macro-fused with conditional jumps.
9217 NB: If TEST/AND/CMP/ADD/SUB/INC/DEC is of RIP relative address,
9218 or is one of the following format:
9219
9220 cmp m, imm
9221 add m, imm
9222 sub m, imm
9223 test m, imm
9224 and m, imm
9225 inc m
9226 dec m
9227
9228 it is unfusible. */
e379e5f3
L
9229
9230static int
79d72f45 9231maybe_fused_with_jcc_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
9232{
9233 /* No RIP address. */
9234 if (i.base_reg && i.base_reg->reg_num == RegIP)
9235 return 0;
9236
389d00a5
JB
9237 /* No opcodes outside of base encoding space. */
9238 if (i.tm.opcode_modifier.opcodespace != SPACE_BASE)
e379e5f3
L
9239 return 0;
9240
79d72f45
HL
9241 /* add, sub without add/sub m, imm. */
9242 if (i.tm.base_opcode <= 5
e379e5f3
L
9243 || (i.tm.base_opcode >= 0x28 && i.tm.base_opcode <= 0x2d)
9244 || ((i.tm.base_opcode | 3) == 0x83
79d72f45 9245 && (i.tm.extension_opcode == 0x5
e379e5f3 9246 || i.tm.extension_opcode == 0x0)))
79d72f45
HL
9247 {
9248 *mf_cmp_p = mf_cmp_alu_cmp;
9249 return !(i.mem_operands && i.imm_operands);
9250 }
e379e5f3 9251
79d72f45
HL
9252 /* and without and m, imm. */
9253 if ((i.tm.base_opcode >= 0x20 && i.tm.base_opcode <= 0x25)
9254 || ((i.tm.base_opcode | 3) == 0x83
9255 && i.tm.extension_opcode == 0x4))
9256 {
9257 *mf_cmp_p = mf_cmp_test_and;
9258 return !(i.mem_operands && i.imm_operands);
9259 }
9260
9261 /* test without test m imm. */
e379e5f3
L
9262 if ((i.tm.base_opcode | 1) == 0x85
9263 || (i.tm.base_opcode | 1) == 0xa9
9264 || ((i.tm.base_opcode | 1) == 0xf7
79d72f45
HL
9265 && i.tm.extension_opcode == 0))
9266 {
9267 *mf_cmp_p = mf_cmp_test_and;
9268 return !(i.mem_operands && i.imm_operands);
9269 }
9270
9271 /* cmp without cmp m, imm. */
9272 if ((i.tm.base_opcode >= 0x38 && i.tm.base_opcode <= 0x3d)
e379e5f3
L
9273 || ((i.tm.base_opcode | 3) == 0x83
9274 && (i.tm.extension_opcode == 0x7)))
79d72f45
HL
9275 {
9276 *mf_cmp_p = mf_cmp_alu_cmp;
9277 return !(i.mem_operands && i.imm_operands);
9278 }
e379e5f3 9279
79d72f45 9280 /* inc, dec without inc/dec m. */
e379e5f3
L
9281 if ((i.tm.cpu_flags.bitfield.cpuno64
9282 && (i.tm.base_opcode | 0xf) == 0x4f)
9283 || ((i.tm.base_opcode | 1) == 0xff
9284 && i.tm.extension_opcode <= 0x1))
79d72f45
HL
9285 {
9286 *mf_cmp_p = mf_cmp_incdec;
9287 return !i.mem_operands;
9288 }
e379e5f3
L
9289
9290 return 0;
9291}
9292
9293/* Return 1 if a FUSED_JCC_PADDING frag should be generated. */
9294
9295static int
79d72f45 9296add_fused_jcc_padding_frag_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
9297{
9298 /* NB: Don't work with COND_JUMP86 without i386. */
9299 if (!align_branch_power
9300 || now_seg == absolute_section
9301 || !cpu_arch_flags.bitfield.cpui386
9302 || !(align_branch & align_branch_fused_bit))
9303 return 0;
9304
79d72f45 9305 if (maybe_fused_with_jcc_p (mf_cmp_p))
e379e5f3
L
9306 {
9307 if (last_insn.kind == last_insn_other
9308 || last_insn.seg != now_seg)
9309 return 1;
9310 if (flag_debug)
9311 as_warn_where (last_insn.file, last_insn.line,
9312 _("`%s` skips -malign-branch-boundary on `%s`"),
9313 last_insn.name, i.tm.name);
9314 }
9315
9316 return 0;
9317}
9318
9319/* Return 1 if a BRANCH_PREFIX frag should be generated. */
9320
9321static int
9322add_branch_prefix_frag_p (void)
9323{
9324 /* NB: Don't work with COND_JUMP86 without i386. Don't add prefix
9325 to PadLock instructions since they include prefixes in opcode. */
9326 if (!align_branch_power
9327 || !align_branch_prefix_size
9328 || now_seg == absolute_section
9329 || i.tm.cpu_flags.bitfield.cpupadlock
9330 || !cpu_arch_flags.bitfield.cpui386)
9331 return 0;
9332
9333 /* Don't add prefix if it is a prefix or there is no operand in case
9334 that segment prefix is special. */
9335 if (!i.operands || i.tm.opcode_modifier.isprefix)
9336 return 0;
9337
9338 if (last_insn.kind == last_insn_other
9339 || last_insn.seg != now_seg)
9340 return 1;
9341
9342 if (flag_debug)
9343 as_warn_where (last_insn.file, last_insn.line,
9344 _("`%s` skips -malign-branch-boundary on `%s`"),
9345 last_insn.name, i.tm.name);
9346
9347 return 0;
9348}
9349
9350/* Return 1 if a BRANCH_PADDING frag should be generated. */
9351
9352static int
79d72f45
HL
9353add_branch_padding_frag_p (enum align_branch_kind *branch_p,
9354 enum mf_jcc_kind *mf_jcc_p)
e379e5f3
L
9355{
9356 int add_padding;
9357
9358 /* NB: Don't work with COND_JUMP86 without i386. */
9359 if (!align_branch_power
9360 || now_seg == absolute_section
389d00a5
JB
9361 || !cpu_arch_flags.bitfield.cpui386
9362 || i.tm.opcode_modifier.opcodespace != SPACE_BASE)
e379e5f3
L
9363 return 0;
9364
9365 add_padding = 0;
9366
9367 /* Check for jcc and direct jmp. */
9368 if (i.tm.opcode_modifier.jump == JUMP)
9369 {
9370 if (i.tm.base_opcode == JUMP_PC_RELATIVE)
9371 {
9372 *branch_p = align_branch_jmp;
9373 add_padding = align_branch & align_branch_jmp_bit;
9374 }
9375 else
9376 {
79d72f45
HL
9377 /* Because J<cc> and JN<cc> share same group in macro-fusible table,
9378 igore the lowest bit. */
9379 *mf_jcc_p = (i.tm.base_opcode & 0x0e) >> 1;
e379e5f3
L
9380 *branch_p = align_branch_jcc;
9381 if ((align_branch & align_branch_jcc_bit))
9382 add_padding = 1;
9383 }
9384 }
e379e5f3
L
9385 else if ((i.tm.base_opcode | 1) == 0xc3)
9386 {
9387 /* Near ret. */
9388 *branch_p = align_branch_ret;
9389 if ((align_branch & align_branch_ret_bit))
9390 add_padding = 1;
9391 }
9392 else
9393 {
9394 /* Check for indirect jmp, direct and indirect calls. */
9395 if (i.tm.base_opcode == 0xe8)
9396 {
9397 /* Direct call. */
9398 *branch_p = align_branch_call;
9399 if ((align_branch & align_branch_call_bit))
9400 add_padding = 1;
9401 }
9402 else if (i.tm.base_opcode == 0xff
9403 && (i.tm.extension_opcode == 2
9404 || i.tm.extension_opcode == 4))
9405 {
9406 /* Indirect call and jmp. */
9407 *branch_p = align_branch_indirect;
9408 if ((align_branch & align_branch_indirect_bit))
9409 add_padding = 1;
9410 }
9411
9412 if (add_padding
9413 && i.disp_operands
9414 && tls_get_addr
9415 && (i.op[0].disps->X_op == O_symbol
9416 || (i.op[0].disps->X_op == O_subtract
9417 && i.op[0].disps->X_op_symbol == GOT_symbol)))
9418 {
9419 symbolS *s = i.op[0].disps->X_add_symbol;
9420 /* No padding to call to global or undefined tls_get_addr. */
9421 if ((S_IS_EXTERNAL (s) || !S_IS_DEFINED (s))
9422 && strcmp (S_GET_NAME (s), tls_get_addr) == 0)
9423 return 0;
9424 }
9425 }
9426
9427 if (add_padding
9428 && last_insn.kind != last_insn_other
9429 && last_insn.seg == now_seg)
9430 {
9431 if (flag_debug)
9432 as_warn_where (last_insn.file, last_insn.line,
9433 _("`%s` skips -malign-branch-boundary on `%s`"),
9434 last_insn.name, i.tm.name);
9435 return 0;
9436 }
9437
9438 return add_padding;
9439}
9440
29b0f896 9441static void
e3bb37b5 9442output_insn (void)
29b0f896 9443{
2bbd9c25
JJ
9444 fragS *insn_start_frag;
9445 offsetT insn_start_off;
e379e5f3
L
9446 fragS *fragP = NULL;
9447 enum align_branch_kind branch = align_branch_none;
79d72f45
HL
9448 /* The initializer is arbitrary just to avoid uninitialized error.
9449 it's actually either assigned in add_branch_padding_frag_p
9450 or never be used. */
9451 enum mf_jcc_kind mf_jcc = mf_jcc_jo;
2bbd9c25 9452
b4a3a7b4 9453#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
48ef937e 9454 if (IS_ELF && x86_used_note && now_seg != absolute_section)
b4a3a7b4 9455 {
32930e4e
L
9456 if ((i.xstate & xstate_tmm) == xstate_tmm
9457 || i.tm.cpu_flags.bitfield.cpuamx_tile)
9458 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_TMM;
9459
b4a3a7b4
L
9460 if (i.tm.cpu_flags.bitfield.cpu8087
9461 || i.tm.cpu_flags.bitfield.cpu287
9462 || i.tm.cpu_flags.bitfield.cpu387
9463 || i.tm.cpu_flags.bitfield.cpu687
9464 || i.tm.cpu_flags.bitfield.cpufisttp)
9465 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X87;
014d61ea 9466
921eafea 9467 if ((i.xstate & xstate_mmx)
389d00a5
JB
9468 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
9469 && !is_any_vex_encoding (&i.tm)
9470 && (i.tm.base_opcode == 0x77 /* emms */
9471 || i.tm.base_opcode == 0x0e /* femms */)))
b4a3a7b4 9472 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MMX;
014d61ea 9473
32930e4e
L
9474 if (i.index_reg)
9475 {
9476 if (i.index_reg->reg_type.bitfield.zmmword)
9477 i.xstate |= xstate_zmm;
9478 else if (i.index_reg->reg_type.bitfield.ymmword)
9479 i.xstate |= xstate_ymm;
9480 else if (i.index_reg->reg_type.bitfield.xmmword)
9481 i.xstate |= xstate_xmm;
9482 }
014d61ea
JB
9483
9484 /* vzeroall / vzeroupper */
9485 if (i.tm.base_opcode == 0x77 && i.tm.cpu_flags.bitfield.cpuavx)
9486 i.xstate |= xstate_ymm;
9487
c4694f17 9488 if ((i.xstate & xstate_xmm)
389d00a5
JB
9489 /* ldmxcsr / stmxcsr / vldmxcsr / vstmxcsr */
9490 || (i.tm.base_opcode == 0xae
9491 && (i.tm.cpu_flags.bitfield.cpusse
9492 || i.tm.cpu_flags.bitfield.cpuavx))
c4694f17
TG
9493 || i.tm.cpu_flags.bitfield.cpuwidekl
9494 || i.tm.cpu_flags.bitfield.cpukl)
b4a3a7b4 9495 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XMM;
014d61ea 9496
921eafea 9497 if ((i.xstate & xstate_ymm) == xstate_ymm)
b4a3a7b4 9498 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_YMM;
921eafea 9499 if ((i.xstate & xstate_zmm) == xstate_zmm)
b4a3a7b4 9500 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_ZMM;
6225c532 9501 if (i.mask.reg || (i.xstate & xstate_mask) == xstate_mask)
32930e4e 9502 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MASK;
b4a3a7b4
L
9503 if (i.tm.cpu_flags.bitfield.cpufxsr)
9504 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_FXSR;
9505 if (i.tm.cpu_flags.bitfield.cpuxsave)
9506 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVE;
9507 if (i.tm.cpu_flags.bitfield.cpuxsaveopt)
9508 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT;
9509 if (i.tm.cpu_flags.bitfield.cpuxsavec)
9510 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEC;
b0ab0693
L
9511
9512 if (x86_feature_2_used
9513 || i.tm.cpu_flags.bitfield.cpucmov
9514 || i.tm.cpu_flags.bitfield.cpusyscall
389d00a5
JB
9515 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
9516 && i.tm.base_opcode == 0xc7
70e95837 9517 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
b0ab0693
L
9518 && i.tm.extension_opcode == 1) /* cmpxchg8b */)
9519 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_BASELINE;
9520 if (i.tm.cpu_flags.bitfield.cpusse3
9521 || i.tm.cpu_flags.bitfield.cpussse3
9522 || i.tm.cpu_flags.bitfield.cpusse4_1
9523 || i.tm.cpu_flags.bitfield.cpusse4_2
9524 || i.tm.cpu_flags.bitfield.cpucx16
9525 || i.tm.cpu_flags.bitfield.cpupopcnt
9526 /* LAHF-SAHF insns in 64-bit mode. */
9527 || (flag_code == CODE_64BIT
35648716
JB
9528 && (i.tm.base_opcode | 1) == 0x9f
9529 && i.tm.opcode_modifier.opcodespace == SPACE_BASE))
b0ab0693
L
9530 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V2;
9531 if (i.tm.cpu_flags.bitfield.cpuavx
9532 || i.tm.cpu_flags.bitfield.cpuavx2
a9860005
JB
9533 /* Any VEX encoded insns execpt for AVX512F, AVX512BW, AVX512DQ,
9534 XOP, FMA4, LPW, TBM, and AMX. */
b0ab0693
L
9535 || (i.tm.opcode_modifier.vex
9536 && !i.tm.cpu_flags.bitfield.cpuavx512f
9537 && !i.tm.cpu_flags.bitfield.cpuavx512bw
9538 && !i.tm.cpu_flags.bitfield.cpuavx512dq
a9860005
JB
9539 && !i.tm.cpu_flags.bitfield.cpuxop
9540 && !i.tm.cpu_flags.bitfield.cpufma4
b0ab0693
L
9541 && !i.tm.cpu_flags.bitfield.cpulwp
9542 && !i.tm.cpu_flags.bitfield.cputbm
9543 && !(x86_feature_2_used & GNU_PROPERTY_X86_FEATURE_2_TMM))
9544 || i.tm.cpu_flags.bitfield.cpuf16c
9545 || i.tm.cpu_flags.bitfield.cpufma
9546 || i.tm.cpu_flags.bitfield.cpulzcnt
9547 || i.tm.cpu_flags.bitfield.cpumovbe
9548 || i.tm.cpu_flags.bitfield.cpuxsaves
9549 || (x86_feature_2_used
9550 & (GNU_PROPERTY_X86_FEATURE_2_XSAVE
9551 | GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT
9552 | GNU_PROPERTY_X86_FEATURE_2_XSAVEC)) != 0)
9553 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V3;
9554 if (i.tm.cpu_flags.bitfield.cpuavx512f
9555 || i.tm.cpu_flags.bitfield.cpuavx512bw
9556 || i.tm.cpu_flags.bitfield.cpuavx512dq
9557 || i.tm.cpu_flags.bitfield.cpuavx512vl
a9860005
JB
9558 /* Any EVEX encoded insns except for AVX512ER, AVX512PF,
9559 AVX512-4FMAPS, and AVX512-4VNNIW. */
b0ab0693
L
9560 || (i.tm.opcode_modifier.evex
9561 && !i.tm.cpu_flags.bitfield.cpuavx512er
9562 && !i.tm.cpu_flags.bitfield.cpuavx512pf
a9860005 9563 && !i.tm.cpu_flags.bitfield.cpuavx512_4fmaps
b0ab0693
L
9564 && !i.tm.cpu_flags.bitfield.cpuavx512_4vnniw))
9565 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V4;
b4a3a7b4
L
9566 }
9567#endif
9568
29b0f896
AM
9569 /* Tie dwarf2 debug info to the address at the start of the insn.
9570 We can't do this after the insn has been output as the current
9571 frag may have been closed off. eg. by frag_var. */
9572 dwarf2_emit_insn (0);
9573
2bbd9c25
JJ
9574 insn_start_frag = frag_now;
9575 insn_start_off = frag_now_fix ();
9576
79d72f45 9577 if (add_branch_padding_frag_p (&branch, &mf_jcc))
e379e5f3
L
9578 {
9579 char *p;
9580 /* Branch can be 8 bytes. Leave some room for prefixes. */
9581 unsigned int max_branch_padding_size = 14;
9582
9583 /* Align section to boundary. */
9584 record_alignment (now_seg, align_branch_power);
9585
9586 /* Make room for padding. */
9587 frag_grow (max_branch_padding_size);
9588
9589 /* Start of the padding. */
9590 p = frag_more (0);
9591
9592 fragP = frag_now;
9593
9594 frag_var (rs_machine_dependent, max_branch_padding_size, 0,
9595 ENCODE_RELAX_STATE (BRANCH_PADDING, 0),
9596 NULL, 0, p);
9597
79d72f45 9598 fragP->tc_frag_data.mf_type = mf_jcc;
e379e5f3
L
9599 fragP->tc_frag_data.branch_type = branch;
9600 fragP->tc_frag_data.max_bytes = max_branch_padding_size;
9601 }
9602
d59a54c2
JB
9603 if (!cpu_arch_flags.bitfield.cpui386 && (flag_code != CODE_16BIT)
9604 && !pre_386_16bit_warned)
9605 {
9606 as_warn (_("use .code16 to ensure correct addressing mode"));
9607 pre_386_16bit_warned = true;
9608 }
9609
29b0f896 9610 /* Output jumps. */
0cfa3eb3 9611 if (i.tm.opcode_modifier.jump == JUMP)
29b0f896 9612 output_branch ();
0cfa3eb3
JB
9613 else if (i.tm.opcode_modifier.jump == JUMP_BYTE
9614 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896 9615 output_jump ();
0cfa3eb3 9616 else if (i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT)
29b0f896
AM
9617 output_interseg_jump ();
9618 else
9619 {
9620 /* Output normal instructions here. */
9621 char *p;
9622 unsigned char *q;
47465058 9623 unsigned int j;
79d72f45 9624 enum mf_cmp_kind mf_cmp;
4dffcebc 9625
e4e00185 9626 if (avoid_fence
389d00a5
JB
9627 && (i.tm.base_opcode == 0xaee8
9628 || i.tm.base_opcode == 0xaef0
9629 || i.tm.base_opcode == 0xaef8))
48ef937e
JB
9630 {
9631 /* Encode lfence, mfence, and sfence as
9632 f0 83 04 24 00 lock addl $0x0, (%{re}sp). */
47f4115a
JB
9633 if (flag_code == CODE_16BIT)
9634 as_bad (_("Cannot convert `%s' in 16-bit mode"), i.tm.name);
9635 else if (omit_lock_prefix)
9636 as_bad (_("Cannot convert `%s' with `-momit-lock-prefix=yes' in effect"),
9637 i.tm.name);
9638 else if (now_seg != absolute_section)
48ef937e
JB
9639 {
9640 offsetT val = 0x240483f0ULL;
9641
9642 p = frag_more (5);
9643 md_number_to_chars (p, val, 5);
9644 }
9645 else
9646 abs_section_offset += 5;
9647 return;
9648 }
e4e00185 9649
d022bddd
IT
9650 /* Some processors fail on LOCK prefix. This options makes
9651 assembler ignore LOCK prefix and serves as a workaround. */
9652 if (omit_lock_prefix)
9653 {
35648716
JB
9654 if (i.tm.base_opcode == LOCK_PREFIX_OPCODE
9655 && i.tm.opcode_modifier.isprefix)
d022bddd
IT
9656 return;
9657 i.prefix[LOCK_PREFIX] = 0;
9658 }
9659
e379e5f3
L
9660 if (branch)
9661 /* Skip if this is a branch. */
9662 ;
79d72f45 9663 else if (add_fused_jcc_padding_frag_p (&mf_cmp))
e379e5f3
L
9664 {
9665 /* Make room for padding. */
9666 frag_grow (MAX_FUSED_JCC_PADDING_SIZE);
9667 p = frag_more (0);
9668
9669 fragP = frag_now;
9670
9671 frag_var (rs_machine_dependent, MAX_FUSED_JCC_PADDING_SIZE, 0,
9672 ENCODE_RELAX_STATE (FUSED_JCC_PADDING, 0),
9673 NULL, 0, p);
9674
79d72f45 9675 fragP->tc_frag_data.mf_type = mf_cmp;
e379e5f3
L
9676 fragP->tc_frag_data.branch_type = align_branch_fused;
9677 fragP->tc_frag_data.max_bytes = MAX_FUSED_JCC_PADDING_SIZE;
9678 }
9679 else if (add_branch_prefix_frag_p ())
9680 {
9681 unsigned int max_prefix_size = align_branch_prefix_size;
9682
9683 /* Make room for padding. */
9684 frag_grow (max_prefix_size);
9685 p = frag_more (0);
9686
9687 fragP = frag_now;
9688
9689 frag_var (rs_machine_dependent, max_prefix_size, 0,
9690 ENCODE_RELAX_STATE (BRANCH_PREFIX, 0),
9691 NULL, 0, p);
9692
9693 fragP->tc_frag_data.max_bytes = max_prefix_size;
9694 }
9695
43234a1e
L
9696 /* Since the VEX/EVEX prefix contains the implicit prefix, we
9697 don't need the explicit prefix. */
cf665fee 9698 if (!is_any_vex_encoding (&i.tm))
bc4bd9ab 9699 {
7b47a312 9700 switch (i.tm.opcode_modifier.opcodeprefix)
bc4bd9ab 9701 {
7b47a312
L
9702 case PREFIX_0X66:
9703 add_prefix (0x66);
9704 break;
9705 case PREFIX_0XF2:
9706 add_prefix (0xf2);
9707 break;
9708 case PREFIX_0XF3:
8b65b895
L
9709 if (!i.tm.cpu_flags.bitfield.cpupadlock
9710 || (i.prefix[REP_PREFIX] != 0xf3))
9711 add_prefix (0xf3);
c0f3af97 9712 break;
7b47a312 9713 case PREFIX_NONE:
9a182d04 9714 switch (i.opcode_length)
c0f3af97 9715 {
7b47a312 9716 case 2:
7b47a312 9717 break;
9a182d04 9718 case 1:
7b47a312 9719 /* Check for pseudo prefixes. */
9a182d04
JB
9720 if (!i.tm.opcode_modifier.isprefix || i.tm.base_opcode)
9721 break;
7b47a312
L
9722 as_bad_where (insn_start_frag->fr_file,
9723 insn_start_frag->fr_line,
9724 _("pseudo prefix without instruction"));
9725 return;
9726 default:
9727 abort ();
4dffcebc 9728 }
c0f3af97 9729 break;
c0f3af97
L
9730 default:
9731 abort ();
bc4bd9ab 9732 }
c0f3af97 9733
6d19a37a 9734#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
cf61b747
L
9735 /* For x32, add a dummy REX_OPCODE prefix for mov/add with
9736 R_X86_64_GOTTPOFF relocation so that linker can safely
14470f07
L
9737 perform IE->LE optimization. A dummy REX_OPCODE prefix
9738 is also needed for lea with R_X86_64_GOTPC32_TLSDESC
9739 relocation for GDesc -> IE/LE optimization. */
cf61b747
L
9740 if (x86_elf_abi == X86_64_X32_ABI
9741 && i.operands == 2
14470f07
L
9742 && (i.reloc[0] == BFD_RELOC_X86_64_GOTTPOFF
9743 || i.reloc[0] == BFD_RELOC_X86_64_GOTPC32_TLSDESC)
cf61b747
L
9744 && i.prefix[REX_PREFIX] == 0)
9745 add_prefix (REX_OPCODE);
6d19a37a 9746#endif
cf61b747 9747
c0f3af97
L
9748 /* The prefix bytes. */
9749 for (j = ARRAY_SIZE (i.prefix), q = i.prefix; j > 0; j--, q++)
9750 if (*q)
48ef937e 9751 frag_opcode_byte (*q);
0f10071e 9752 }
ae5c1c7b 9753 else
c0f3af97
L
9754 {
9755 for (j = 0, q = i.prefix; j < ARRAY_SIZE (i.prefix); j++, q++)
9756 if (*q)
9757 switch (j)
9758 {
c0f3af97
L
9759 case SEG_PREFIX:
9760 case ADDR_PREFIX:
48ef937e 9761 frag_opcode_byte (*q);
c0f3af97
L
9762 break;
9763 default:
9764 /* There should be no other prefixes for instructions
9765 with VEX prefix. */
9766 abort ();
9767 }
9768
43234a1e
L
9769 /* For EVEX instructions i.vrex should become 0 after
9770 build_evex_prefix. For VEX instructions upper 16 registers
9771 aren't available, so VREX should be 0. */
9772 if (i.vrex)
9773 abort ();
c0f3af97 9774 /* Now the VEX prefix. */
48ef937e
JB
9775 if (now_seg != absolute_section)
9776 {
9777 p = frag_more (i.vex.length);
9778 for (j = 0; j < i.vex.length; j++)
9779 p[j] = i.vex.bytes[j];
9780 }
9781 else
9782 abs_section_offset += i.vex.length;
c0f3af97 9783 }
252b5132 9784
29b0f896 9785 /* Now the opcode; be careful about word order here! */
389d00a5
JB
9786 j = i.opcode_length;
9787 if (!i.vex.length)
9788 switch (i.tm.opcode_modifier.opcodespace)
9789 {
9790 case SPACE_BASE:
9791 break;
9792 case SPACE_0F:
9793 ++j;
9794 break;
9795 case SPACE_0F38:
9796 case SPACE_0F3A:
9797 j += 2;
9798 break;
9799 default:
9800 abort ();
9801 }
9802
48ef937e 9803 if (now_seg == absolute_section)
389d00a5
JB
9804 abs_section_offset += j;
9805 else if (j == 1)
29b0f896
AM
9806 {
9807 FRAG_APPEND_1_CHAR (i.tm.base_opcode);
9808 }
9809 else
9810 {
389d00a5
JB
9811 p = frag_more (j);
9812 if (!i.vex.length
9813 && i.tm.opcode_modifier.opcodespace != SPACE_BASE)
9814 {
9815 *p++ = 0x0f;
9816 if (i.tm.opcode_modifier.opcodespace != SPACE_0F)
9817 *p++ = i.tm.opcode_modifier.opcodespace == SPACE_0F38
9818 ? 0x38 : 0x3a;
9819 }
9820
9a182d04 9821 switch (i.opcode_length)
331d2d0d 9822 {
4dffcebc 9823 case 2:
389d00a5
JB
9824 /* Put out high byte first: can't use md_number_to_chars! */
9825 *p++ = (i.tm.base_opcode >> 8) & 0xff;
9826 /* Fall through. */
9827 case 1:
9828 *p = i.tm.base_opcode & 0xff;
4dffcebc
L
9829 break;
9830 default:
9831 abort ();
9832 break;
331d2d0d 9833 }
0f10071e 9834
29b0f896 9835 }
3e73aa7c 9836
29b0f896 9837 /* Now the modrm byte and sib byte (if present). */
40fb9820 9838 if (i.tm.opcode_modifier.modrm)
29b0f896 9839 {
48ef937e
JB
9840 frag_opcode_byte ((i.rm.regmem << 0)
9841 | (i.rm.reg << 3)
9842 | (i.rm.mode << 6));
29b0f896
AM
9843 /* If i.rm.regmem == ESP (4)
9844 && i.rm.mode != (Register mode)
9845 && not 16 bit
9846 ==> need second modrm byte. */
9847 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
9848 && i.rm.mode != 3
dc821c5f 9849 && !(i.base_reg && i.base_reg->reg_type.bitfield.word))
48ef937e
JB
9850 frag_opcode_byte ((i.sib.base << 0)
9851 | (i.sib.index << 3)
9852 | (i.sib.scale << 6));
29b0f896 9853 }
3e73aa7c 9854
29b0f896 9855 if (i.disp_operands)
2bbd9c25 9856 output_disp (insn_start_frag, insn_start_off);
3e73aa7c 9857
29b0f896 9858 if (i.imm_operands)
2bbd9c25 9859 output_imm (insn_start_frag, insn_start_off);
9c33702b
JB
9860
9861 /*
9862 * frag_now_fix () returning plain abs_section_offset when we're in the
9863 * absolute section, and abs_section_offset not getting updated as data
9864 * gets added to the frag breaks the logic below.
9865 */
9866 if (now_seg != absolute_section)
9867 {
9868 j = encoding_length (insn_start_frag, insn_start_off, frag_more (0));
9869 if (j > 15)
9870 as_warn (_("instruction length of %u bytes exceeds the limit of 15"),
9871 j);
e379e5f3
L
9872 else if (fragP)
9873 {
9874 /* NB: Don't add prefix with GOTPC relocation since
9875 output_disp() above depends on the fixed encoding
9876 length. Can't add prefix with TLS relocation since
9877 it breaks TLS linker optimization. */
9878 unsigned int max = i.has_gotpc_tls_reloc ? 0 : 15 - j;
9879 /* Prefix count on the current instruction. */
9880 unsigned int count = i.vex.length;
9881 unsigned int k;
9882 for (k = 0; k < ARRAY_SIZE (i.prefix); k++)
9883 /* REX byte is encoded in VEX/EVEX prefix. */
9884 if (i.prefix[k] && (k != REX_PREFIX || !i.vex.length))
9885 count++;
9886
9887 /* Count prefixes for extended opcode maps. */
9888 if (!i.vex.length)
389d00a5 9889 switch (i.tm.opcode_modifier.opcodespace)
e379e5f3 9890 {
389d00a5 9891 case SPACE_BASE:
e379e5f3 9892 break;
389d00a5
JB
9893 case SPACE_0F:
9894 count++;
e379e5f3 9895 break;
389d00a5
JB
9896 case SPACE_0F38:
9897 case SPACE_0F3A:
9898 count += 2;
e379e5f3
L
9899 break;
9900 default:
9901 abort ();
9902 }
9903
9904 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
9905 == BRANCH_PREFIX)
9906 {
9907 /* Set the maximum prefix size in BRANCH_PREFIX
9908 frag. */
9909 if (fragP->tc_frag_data.max_bytes > max)
9910 fragP->tc_frag_data.max_bytes = max;
9911 if (fragP->tc_frag_data.max_bytes > count)
9912 fragP->tc_frag_data.max_bytes -= count;
9913 else
9914 fragP->tc_frag_data.max_bytes = 0;
9915 }
9916 else
9917 {
9918 /* Remember the maximum prefix size in FUSED_JCC_PADDING
9919 frag. */
9920 unsigned int max_prefix_size;
9921 if (align_branch_prefix_size > max)
9922 max_prefix_size = max;
9923 else
9924 max_prefix_size = align_branch_prefix_size;
9925 if (max_prefix_size > count)
9926 fragP->tc_frag_data.max_prefix_length
9927 = max_prefix_size - count;
9928 }
9929
9930 /* Use existing segment prefix if possible. Use CS
9931 segment prefix in 64-bit mode. In 32-bit mode, use SS
9932 segment prefix with ESP/EBP base register and use DS
9933 segment prefix without ESP/EBP base register. */
9934 if (i.prefix[SEG_PREFIX])
9935 fragP->tc_frag_data.default_prefix = i.prefix[SEG_PREFIX];
9936 else if (flag_code == CODE_64BIT)
9937 fragP->tc_frag_data.default_prefix = CS_PREFIX_OPCODE;
9938 else if (i.base_reg
9939 && (i.base_reg->reg_num == 4
9940 || i.base_reg->reg_num == 5))
9941 fragP->tc_frag_data.default_prefix = SS_PREFIX_OPCODE;
9942 else
9943 fragP->tc_frag_data.default_prefix = DS_PREFIX_OPCODE;
9944 }
9c33702b 9945 }
29b0f896 9946 }
252b5132 9947
e379e5f3
L
9948 /* NB: Don't work with COND_JUMP86 without i386. */
9949 if (align_branch_power
9950 && now_seg != absolute_section
9951 && cpu_arch_flags.bitfield.cpui386)
9952 {
9953 /* Terminate each frag so that we can add prefix and check for
9954 fused jcc. */
9955 frag_wane (frag_now);
9956 frag_new (0);
9957 }
9958
29b0f896
AM
9959#ifdef DEBUG386
9960 if (flag_debug)
9961 {
7b81dfbb 9962 pi ("" /*line*/, &i);
29b0f896
AM
9963 }
9964#endif /* DEBUG386 */
9965}
252b5132 9966
e205caa7
L
9967/* Return the size of the displacement operand N. */
9968
9969static int
9970disp_size (unsigned int n)
9971{
9972 int size = 4;
43234a1e 9973
b5014f7a 9974 if (i.types[n].bitfield.disp64)
40fb9820
L
9975 size = 8;
9976 else if (i.types[n].bitfield.disp8)
9977 size = 1;
9978 else if (i.types[n].bitfield.disp16)
9979 size = 2;
e205caa7
L
9980 return size;
9981}
9982
9983/* Return the size of the immediate operand N. */
9984
9985static int
9986imm_size (unsigned int n)
9987{
9988 int size = 4;
40fb9820
L
9989 if (i.types[n].bitfield.imm64)
9990 size = 8;
9991 else if (i.types[n].bitfield.imm8 || i.types[n].bitfield.imm8s)
9992 size = 1;
9993 else if (i.types[n].bitfield.imm16)
9994 size = 2;
e205caa7
L
9995 return size;
9996}
9997
29b0f896 9998static void
64e74474 9999output_disp (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
10000{
10001 char *p;
10002 unsigned int n;
252b5132 10003
29b0f896
AM
10004 for (n = 0; n < i.operands; n++)
10005 {
b5014f7a 10006 if (operand_type_check (i.types[n], disp))
29b0f896 10007 {
48ef937e
JB
10008 int size = disp_size (n);
10009
10010 if (now_seg == absolute_section)
10011 abs_section_offset += size;
10012 else if (i.op[n].disps->X_op == O_constant)
29b0f896 10013 {
43234a1e 10014 offsetT val = i.op[n].disps->X_add_number;
252b5132 10015
629cfaf1
JB
10016 val = offset_in_range (val >> (size == 1 ? i.memshift : 0),
10017 size);
29b0f896
AM
10018 p = frag_more (size);
10019 md_number_to_chars (p, val, size);
10020 }
10021 else
10022 {
f86103b7 10023 enum bfd_reloc_code_real reloc_type;
a775efc8
JB
10024 bool pcrel = (i.flags[n] & Operand_PCrel) != 0;
10025 bool sign = (flag_code == CODE_64BIT && size == 4
10026 && (!want_disp32 (&i.tm)
10027 || (i.tm.opcode_modifier.jump && !i.jumpabsolute
10028 && !i.types[n].bitfield.baseindex)))
10029 || pcrel;
02a86693 10030 fixS *fixP;
29b0f896 10031
e205caa7 10032 /* We can't have 8 bit displacement here. */
9c2799c2 10033 gas_assert (!i.types[n].bitfield.disp8);
e205caa7 10034
29b0f896
AM
10035 /* The PC relative address is computed relative
10036 to the instruction boundary, so in case immediate
10037 fields follows, we need to adjust the value. */
10038 if (pcrel && i.imm_operands)
10039 {
29b0f896 10040 unsigned int n1;
e205caa7 10041 int sz = 0;
252b5132 10042
29b0f896 10043 for (n1 = 0; n1 < i.operands; n1++)
40fb9820 10044 if (operand_type_check (i.types[n1], imm))
252b5132 10045 {
e205caa7
L
10046 /* Only one immediate is allowed for PC
10047 relative address. */
9c2799c2 10048 gas_assert (sz == 0);
e205caa7
L
10049 sz = imm_size (n1);
10050 i.op[n].disps->X_add_number -= sz;
252b5132 10051 }
29b0f896 10052 /* We should find the immediate. */
9c2799c2 10053 gas_assert (sz != 0);
29b0f896 10054 }
520dc8e8 10055
29b0f896 10056 p = frag_more (size);
d258b828 10057 reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
d6ab8113 10058 if (GOT_symbol
2bbd9c25 10059 && GOT_symbol == i.op[n].disps->X_add_symbol
d6ab8113 10060 && (((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
10061 || reloc_type == BFD_RELOC_X86_64_32S
10062 || (reloc_type == BFD_RELOC_64
10063 && object_64bit))
d6ab8113
JB
10064 && (i.op[n].disps->X_op == O_symbol
10065 || (i.op[n].disps->X_op == O_add
10066 && ((symbol_get_value_expression
10067 (i.op[n].disps->X_op_symbol)->X_op)
10068 == O_subtract))))
10069 || reloc_type == BFD_RELOC_32_PCREL))
2bbd9c25 10070 {
4fa24527 10071 if (!object_64bit)
7b81dfbb
AJ
10072 {
10073 reloc_type = BFD_RELOC_386_GOTPC;
5b7c81bd 10074 i.has_gotpc_tls_reloc = true;
98da05bf 10075 i.op[n].disps->X_add_number +=
d583596c 10076 encoding_length (insn_start_frag, insn_start_off, p);
7b81dfbb
AJ
10077 }
10078 else if (reloc_type == BFD_RELOC_64)
10079 reloc_type = BFD_RELOC_X86_64_GOTPC64;
d6ab8113 10080 else
7b81dfbb
AJ
10081 /* Don't do the adjustment for x86-64, as there
10082 the pcrel addressing is relative to the _next_
10083 insn, and that is taken care of in other code. */
d6ab8113 10084 reloc_type = BFD_RELOC_X86_64_GOTPC32;
2bbd9c25 10085 }
e379e5f3
L
10086 else if (align_branch_power)
10087 {
10088 switch (reloc_type)
10089 {
10090 case BFD_RELOC_386_TLS_GD:
10091 case BFD_RELOC_386_TLS_LDM:
10092 case BFD_RELOC_386_TLS_IE:
10093 case BFD_RELOC_386_TLS_IE_32:
10094 case BFD_RELOC_386_TLS_GOTIE:
10095 case BFD_RELOC_386_TLS_GOTDESC:
10096 case BFD_RELOC_386_TLS_DESC_CALL:
10097 case BFD_RELOC_X86_64_TLSGD:
10098 case BFD_RELOC_X86_64_TLSLD:
10099 case BFD_RELOC_X86_64_GOTTPOFF:
10100 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
10101 case BFD_RELOC_X86_64_TLSDESC_CALL:
5b7c81bd 10102 i.has_gotpc_tls_reloc = true;
e379e5f3
L
10103 default:
10104 break;
10105 }
10106 }
02a86693
L
10107 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal,
10108 size, i.op[n].disps, pcrel,
10109 reloc_type);
eb19308f
JB
10110
10111 if (flag_code == CODE_64BIT && size == 4 && pcrel
10112 && !i.prefix[ADDR_PREFIX])
10113 fixP->fx_signed = 1;
10114
02a86693
L
10115 /* Check for "call/jmp *mem", "mov mem, %reg",
10116 "test %reg, mem" and "binop mem, %reg" where binop
10117 is one of adc, add, and, cmp, or, sbb, sub, xor
e60f4d3b
L
10118 instructions without data prefix. Always generate
10119 R_386_GOT32X for "sym*GOT" operand in 32-bit mode. */
10120 if (i.prefix[DATA_PREFIX] == 0
10121 && (generate_relax_relocations
10122 || (!object_64bit
10123 && i.rm.mode == 0
10124 && i.rm.regmem == 5))
0cb4071e
L
10125 && (i.rm.mode == 2
10126 || (i.rm.mode == 0 && i.rm.regmem == 5))
389d00a5 10127 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
02a86693
L
10128 && ((i.operands == 1
10129 && i.tm.base_opcode == 0xff
10130 && (i.rm.reg == 2 || i.rm.reg == 4))
10131 || (i.operands == 2
10132 && (i.tm.base_opcode == 0x8b
10133 || i.tm.base_opcode == 0x85
2ae4c703 10134 || (i.tm.base_opcode & ~0x38) == 0x03))))
02a86693
L
10135 {
10136 if (object_64bit)
10137 {
10138 fixP->fx_tcbit = i.rex != 0;
10139 if (i.base_reg
e968fc9b 10140 && (i.base_reg->reg_num == RegIP))
02a86693
L
10141 fixP->fx_tcbit2 = 1;
10142 }
10143 else
10144 fixP->fx_tcbit2 = 1;
10145 }
29b0f896
AM
10146 }
10147 }
10148 }
10149}
252b5132 10150
29b0f896 10151static void
64e74474 10152output_imm (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
10153{
10154 char *p;
10155 unsigned int n;
252b5132 10156
29b0f896
AM
10157 for (n = 0; n < i.operands; n++)
10158 {
40fb9820 10159 if (operand_type_check (i.types[n], imm))
29b0f896 10160 {
48ef937e
JB
10161 int size = imm_size (n);
10162
10163 if (now_seg == absolute_section)
10164 abs_section_offset += size;
10165 else if (i.op[n].imms->X_op == O_constant)
29b0f896 10166 {
29b0f896 10167 offsetT val;
b4cac588 10168
29b0f896
AM
10169 val = offset_in_range (i.op[n].imms->X_add_number,
10170 size);
10171 p = frag_more (size);
10172 md_number_to_chars (p, val, size);
10173 }
10174 else
10175 {
10176 /* Not absolute_section.
10177 Need a 32-bit fixup (don't support 8bit
10178 non-absolute imms). Try to support other
10179 sizes ... */
f86103b7 10180 enum bfd_reloc_code_real reloc_type;
e205caa7 10181 int sign;
29b0f896 10182
40fb9820 10183 if (i.types[n].bitfield.imm32s
a7d61044 10184 && (i.suffix == QWORD_MNEM_SUFFIX
40fb9820 10185 || (!i.suffix && i.tm.opcode_modifier.no_lsuf)))
29b0f896 10186 sign = 1;
e205caa7
L
10187 else
10188 sign = 0;
520dc8e8 10189
29b0f896 10190 p = frag_more (size);
d258b828 10191 reloc_type = reloc (size, 0, sign, i.reloc[n]);
f86103b7 10192
2bbd9c25
JJ
10193 /* This is tough to explain. We end up with this one if we
10194 * have operands that look like
10195 * "_GLOBAL_OFFSET_TABLE_+[.-.L284]". The goal here is to
10196 * obtain the absolute address of the GOT, and it is strongly
10197 * preferable from a performance point of view to avoid using
10198 * a runtime relocation for this. The actual sequence of
10199 * instructions often look something like:
10200 *
10201 * call .L66
10202 * .L66:
10203 * popl %ebx
10204 * addl $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
10205 *
10206 * The call and pop essentially return the absolute address
10207 * of the label .L66 and store it in %ebx. The linker itself
10208 * will ultimately change the first operand of the addl so
10209 * that %ebx points to the GOT, but to keep things simple, the
10210 * .o file must have this operand set so that it generates not
10211 * the absolute address of .L66, but the absolute address of
10212 * itself. This allows the linker itself simply treat a GOTPC
10213 * relocation as asking for a pcrel offset to the GOT to be
10214 * added in, and the addend of the relocation is stored in the
10215 * operand field for the instruction itself.
10216 *
10217 * Our job here is to fix the operand so that it would add
10218 * the correct offset so that %ebx would point to itself. The
10219 * thing that is tricky is that .-.L66 will point to the
10220 * beginning of the instruction, so we need to further modify
10221 * the operand so that it will point to itself. There are
10222 * other cases where you have something like:
10223 *
10224 * .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
10225 *
10226 * and here no correction would be required. Internally in
10227 * the assembler we treat operands of this form as not being
10228 * pcrel since the '.' is explicitly mentioned, and I wonder
10229 * whether it would simplify matters to do it this way. Who
10230 * knows. In earlier versions of the PIC patches, the
10231 * pcrel_adjust field was used to store the correction, but
10232 * since the expression is not pcrel, I felt it would be
10233 * confusing to do it this way. */
10234
d6ab8113 10235 if ((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
10236 || reloc_type == BFD_RELOC_X86_64_32S
10237 || reloc_type == BFD_RELOC_64)
29b0f896
AM
10238 && GOT_symbol
10239 && GOT_symbol == i.op[n].imms->X_add_symbol
10240 && (i.op[n].imms->X_op == O_symbol
10241 || (i.op[n].imms->X_op == O_add
10242 && ((symbol_get_value_expression
10243 (i.op[n].imms->X_op_symbol)->X_op)
10244 == O_subtract))))
10245 {
4fa24527 10246 if (!object_64bit)
d6ab8113 10247 reloc_type = BFD_RELOC_386_GOTPC;
7b81dfbb 10248 else if (size == 4)
d6ab8113 10249 reloc_type = BFD_RELOC_X86_64_GOTPC32;
7b81dfbb
AJ
10250 else if (size == 8)
10251 reloc_type = BFD_RELOC_X86_64_GOTPC64;
5b7c81bd 10252 i.has_gotpc_tls_reloc = true;
d583596c
JB
10253 i.op[n].imms->X_add_number +=
10254 encoding_length (insn_start_frag, insn_start_off, p);
29b0f896 10255 }
29b0f896
AM
10256 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
10257 i.op[n].imms, 0, reloc_type);
10258 }
10259 }
10260 }
252b5132
RH
10261}
10262\f
d182319b
JB
10263/* x86_cons_fix_new is called via the expression parsing code when a
10264 reloc is needed. We use this hook to get the correct .got reloc. */
d182319b
JB
10265static int cons_sign = -1;
10266
10267void
e3bb37b5 10268x86_cons_fix_new (fragS *frag, unsigned int off, unsigned int len,
62ebcb5c 10269 expressionS *exp, bfd_reloc_code_real_type r)
d182319b 10270{
d258b828 10271 r = reloc (len, 0, cons_sign, r);
d182319b
JB
10272
10273#ifdef TE_PE
10274 if (exp->X_op == O_secrel)
10275 {
10276 exp->X_op = O_symbol;
10277 r = BFD_RELOC_32_SECREL;
10278 }
145667f8
MH
10279 else if (exp->X_op == O_secidx)
10280 r = BFD_RELOC_16_SECIDX;
d182319b
JB
10281#endif
10282
10283 fix_new_exp (frag, off, len, exp, 0, r);
10284}
10285
357d1bd8
L
10286/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
10287 purpose of the `.dc.a' internal pseudo-op. */
10288
10289int
10290x86_address_bytes (void)
10291{
10292 if ((stdoutput->arch_info->mach & bfd_mach_x64_32))
10293 return 4;
10294 return stdoutput->arch_info->bits_per_address / 8;
10295}
10296
deea4973
JB
10297#if (!(defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined (OBJ_MACH_O)) \
10298 || defined (LEX_AT)) && !defined (TE_PE)
d258b828 10299# define lex_got(reloc, adjust, types) NULL
718ddfc0 10300#else
f3c180ae
AM
10301/* Parse operands of the form
10302 <symbol>@GOTOFF+<nnn>
10303 and similar .plt or .got references.
10304
10305 If we find one, set up the correct relocation in RELOC and copy the
10306 input string, minus the `@GOTOFF' into a malloc'd buffer for
10307 parsing by the calling routine. Return this buffer, and if ADJUST
10308 is non-null set it to the length of the string we removed from the
10309 input line. Otherwise return NULL. */
10310static char *
91d6fa6a 10311lex_got (enum bfd_reloc_code_real *rel,
64e74474 10312 int *adjust,
d258b828 10313 i386_operand_type *types)
f3c180ae 10314{
7b81dfbb
AJ
10315 /* Some of the relocations depend on the size of what field is to
10316 be relocated. But in our callers i386_immediate and i386_displacement
10317 we don't yet know the operand size (this will be set by insn
10318 matching). Hence we record the word32 relocation here,
10319 and adjust the reloc according to the real size in reloc(). */
145667f8
MH
10320 static const struct
10321 {
f3c180ae 10322 const char *str;
cff8d58a 10323 int len;
4fa24527 10324 const enum bfd_reloc_code_real rel[2];
40fb9820 10325 const i386_operand_type types64;
5b7c81bd 10326 bool need_GOT_symbol;
145667f8
MH
10327 }
10328 gotrel[] =
10329 {
deea4973 10330#ifndef TE_PE
8ce3d284 10331#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
10332 { STRING_COMMA_LEN ("SIZE"), { BFD_RELOC_SIZE32,
10333 BFD_RELOC_SIZE32 },
5b7c81bd 10334 OPERAND_TYPE_IMM32_64, false },
8ce3d284 10335#endif
cff8d58a
L
10336 { STRING_COMMA_LEN ("PLTOFF"), { _dummy_first_bfd_reloc_code_real,
10337 BFD_RELOC_X86_64_PLTOFF64 },
5b7c81bd 10338 OPERAND_TYPE_IMM64, true },
cff8d58a
L
10339 { STRING_COMMA_LEN ("PLT"), { BFD_RELOC_386_PLT32,
10340 BFD_RELOC_X86_64_PLT32 },
a775efc8 10341 OPERAND_TYPE_IMM32_32S_DISP32, false },
cff8d58a
L
10342 { STRING_COMMA_LEN ("GOTPLT"), { _dummy_first_bfd_reloc_code_real,
10343 BFD_RELOC_X86_64_GOTPLT64 },
5b7c81bd 10344 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
10345 { STRING_COMMA_LEN ("GOTOFF"), { BFD_RELOC_386_GOTOFF,
10346 BFD_RELOC_X86_64_GOTOFF64 },
5b7c81bd 10347 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
10348 { STRING_COMMA_LEN ("GOTPCREL"), { _dummy_first_bfd_reloc_code_real,
10349 BFD_RELOC_X86_64_GOTPCREL },
a775efc8 10350 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10351 { STRING_COMMA_LEN ("TLSGD"), { BFD_RELOC_386_TLS_GD,
10352 BFD_RELOC_X86_64_TLSGD },
a775efc8 10353 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10354 { STRING_COMMA_LEN ("TLSLDM"), { BFD_RELOC_386_TLS_LDM,
10355 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10356 OPERAND_TYPE_NONE, true },
cff8d58a
L
10357 { STRING_COMMA_LEN ("TLSLD"), { _dummy_first_bfd_reloc_code_real,
10358 BFD_RELOC_X86_64_TLSLD },
a775efc8 10359 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10360 { STRING_COMMA_LEN ("GOTTPOFF"), { BFD_RELOC_386_TLS_IE_32,
10361 BFD_RELOC_X86_64_GOTTPOFF },
a775efc8 10362 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10363 { STRING_COMMA_LEN ("TPOFF"), { BFD_RELOC_386_TLS_LE_32,
10364 BFD_RELOC_X86_64_TPOFF32 },
a775efc8 10365 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
10366 { STRING_COMMA_LEN ("NTPOFF"), { BFD_RELOC_386_TLS_LE,
10367 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10368 OPERAND_TYPE_NONE, true },
cff8d58a
L
10369 { STRING_COMMA_LEN ("DTPOFF"), { BFD_RELOC_386_TLS_LDO_32,
10370 BFD_RELOC_X86_64_DTPOFF32 },
a775efc8 10371 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
10372 { STRING_COMMA_LEN ("GOTNTPOFF"),{ BFD_RELOC_386_TLS_GOTIE,
10373 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10374 OPERAND_TYPE_NONE, true },
cff8d58a
L
10375 { STRING_COMMA_LEN ("INDNTPOFF"),{ BFD_RELOC_386_TLS_IE,
10376 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10377 OPERAND_TYPE_NONE, true },
cff8d58a
L
10378 { STRING_COMMA_LEN ("GOT"), { BFD_RELOC_386_GOT32,
10379 BFD_RELOC_X86_64_GOT32 },
a775efc8 10380 OPERAND_TYPE_IMM32_32S_64_DISP32, true },
cff8d58a
L
10381 { STRING_COMMA_LEN ("TLSDESC"), { BFD_RELOC_386_TLS_GOTDESC,
10382 BFD_RELOC_X86_64_GOTPC32_TLSDESC },
a775efc8 10383 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10384 { STRING_COMMA_LEN ("TLSCALL"), { BFD_RELOC_386_TLS_DESC_CALL,
10385 BFD_RELOC_X86_64_TLSDESC_CALL },
a775efc8 10386 OPERAND_TYPE_IMM32_32S_DISP32, true },
deea4973
JB
10387#else /* TE_PE */
10388 { STRING_COMMA_LEN ("SECREL32"), { BFD_RELOC_32_SECREL,
10389 BFD_RELOC_32_SECREL },
a775efc8 10390 OPERAND_TYPE_IMM32_32S_64_DISP32_64, false },
deea4973 10391#endif
f3c180ae
AM
10392 };
10393 char *cp;
10394 unsigned int j;
10395
deea4973 10396#if defined (OBJ_MAYBE_ELF) && !defined (TE_PE)
718ddfc0
JB
10397 if (!IS_ELF)
10398 return NULL;
d382c579 10399#endif
718ddfc0 10400
f3c180ae 10401 for (cp = input_line_pointer; *cp != '@'; cp++)
67c11a9b 10402 if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
f3c180ae
AM
10403 return NULL;
10404
47465058 10405 for (j = 0; j < ARRAY_SIZE (gotrel); j++)
f3c180ae 10406 {
cff8d58a 10407 int len = gotrel[j].len;
28f81592 10408 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
f3c180ae 10409 {
4fa24527 10410 if (gotrel[j].rel[object_64bit] != 0)
f3c180ae 10411 {
28f81592
AM
10412 int first, second;
10413 char *tmpbuf, *past_reloc;
f3c180ae 10414
91d6fa6a 10415 *rel = gotrel[j].rel[object_64bit];
f3c180ae 10416
3956db08
JB
10417 if (types)
10418 {
10419 if (flag_code != CODE_64BIT)
40fb9820
L
10420 {
10421 types->bitfield.imm32 = 1;
10422 types->bitfield.disp32 = 1;
10423 }
3956db08
JB
10424 else
10425 *types = gotrel[j].types64;
10426 }
10427
844bf810 10428 if (gotrel[j].need_GOT_symbol && GOT_symbol == NULL)
f3c180ae
AM
10429 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
10430
28f81592 10431 /* The length of the first part of our input line. */
f3c180ae 10432 first = cp - input_line_pointer;
28f81592
AM
10433
10434 /* The second part goes from after the reloc token until
67c11a9b 10435 (and including) an end_of_line char or comma. */
28f81592 10436 past_reloc = cp + 1 + len;
67c11a9b
AM
10437 cp = past_reloc;
10438 while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
10439 ++cp;
10440 second = cp + 1 - past_reloc;
28f81592
AM
10441
10442 /* Allocate and copy string. The trailing NUL shouldn't
10443 be necessary, but be safe. */
add39d23 10444 tmpbuf = XNEWVEC (char, first + second + 2);
f3c180ae 10445 memcpy (tmpbuf, input_line_pointer, first);
0787a12d
AM
10446 if (second != 0 && *past_reloc != ' ')
10447 /* Replace the relocation token with ' ', so that
10448 errors like foo@GOTOFF1 will be detected. */
10449 tmpbuf[first++] = ' ';
af89796a
L
10450 else
10451 /* Increment length by 1 if the relocation token is
10452 removed. */
10453 len++;
10454 if (adjust)
10455 *adjust = len;
0787a12d
AM
10456 memcpy (tmpbuf + first, past_reloc, second);
10457 tmpbuf[first + second] = '\0';
f3c180ae
AM
10458 return tmpbuf;
10459 }
10460
4fa24527
JB
10461 as_bad (_("@%s reloc is not supported with %d-bit output format"),
10462 gotrel[j].str, 1 << (5 + object_64bit));
f3c180ae
AM
10463 return NULL;
10464 }
10465 }
10466
10467 /* Might be a symbol version string. Don't as_bad here. */
10468 return NULL;
10469}
4e4f7c87 10470#endif
f3c180ae 10471
62ebcb5c 10472bfd_reloc_code_real_type
e3bb37b5 10473x86_cons (expressionS *exp, int size)
f3c180ae 10474{
62ebcb5c
AM
10475 bfd_reloc_code_real_type got_reloc = NO_RELOC;
10476
2748c1b1
L
10477#if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
10478 && !defined (LEX_AT)) \
10479 || defined (TE_PE)
ee86248c
JB
10480 intel_syntax = -intel_syntax;
10481
3c7b9c2c 10482 exp->X_md = 0;
4fa24527 10483 if (size == 4 || (object_64bit && size == 8))
f3c180ae
AM
10484 {
10485 /* Handle @GOTOFF and the like in an expression. */
10486 char *save;
10487 char *gotfree_input_line;
4a57f2cf 10488 int adjust = 0;
f3c180ae
AM
10489
10490 save = input_line_pointer;
d258b828 10491 gotfree_input_line = lex_got (&got_reloc, &adjust, NULL);
f3c180ae
AM
10492 if (gotfree_input_line)
10493 input_line_pointer = gotfree_input_line;
10494
10495 expression (exp);
10496
10497 if (gotfree_input_line)
10498 {
10499 /* expression () has merrily parsed up to the end of line,
10500 or a comma - in the wrong buffer. Transfer how far
10501 input_line_pointer has moved to the right buffer. */
10502 input_line_pointer = (save
10503 + (input_line_pointer - gotfree_input_line)
10504 + adjust);
10505 free (gotfree_input_line);
3992d3b7
AM
10506 if (exp->X_op == O_constant
10507 || exp->X_op == O_absent
10508 || exp->X_op == O_illegal
0398aac5 10509 || exp->X_op == O_register
3992d3b7
AM
10510 || exp->X_op == O_big)
10511 {
10512 char c = *input_line_pointer;
10513 *input_line_pointer = 0;
10514 as_bad (_("missing or invalid expression `%s'"), save);
10515 *input_line_pointer = c;
10516 }
b9519cfe
L
10517 else if ((got_reloc == BFD_RELOC_386_PLT32
10518 || got_reloc == BFD_RELOC_X86_64_PLT32)
10519 && exp->X_op != O_symbol)
10520 {
10521 char c = *input_line_pointer;
10522 *input_line_pointer = 0;
10523 as_bad (_("invalid PLT expression `%s'"), save);
10524 *input_line_pointer = c;
10525 }
f3c180ae
AM
10526 }
10527 }
10528 else
10529 expression (exp);
ee86248c
JB
10530
10531 intel_syntax = -intel_syntax;
10532
10533 if (intel_syntax)
10534 i386_intel_simplify (exp);
2748c1b1
L
10535#else
10536 expression (exp);
10537#endif
62ebcb5c 10538
a442cac5
JB
10539 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
10540 if (size == 4 && exp->X_op == O_constant && !object_64bit)
10541 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
10542
62ebcb5c 10543 return got_reloc;
f3c180ae 10544}
f3c180ae 10545
9f32dd5b
L
10546static void
10547signed_cons (int size)
6482c264 10548{
a442cac5 10549 if (object_64bit)
d182319b
JB
10550 cons_sign = 1;
10551 cons (size);
10552 cons_sign = -1;
6482c264
NC
10553}
10554
d182319b 10555#ifdef TE_PE
6482c264 10556static void
7016a5d5 10557pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
6482c264
NC
10558{
10559 expressionS exp;
10560
10561 do
10562 {
10563 expression (&exp);
10564 if (exp.X_op == O_symbol)
10565 exp.X_op = O_secrel;
10566
10567 emit_expr (&exp, 4);
10568 }
10569 while (*input_line_pointer++ == ',');
10570
10571 input_line_pointer--;
10572 demand_empty_rest_of_line ();
10573}
145667f8
MH
10574
10575static void
10576pe_directive_secidx (int dummy ATTRIBUTE_UNUSED)
10577{
10578 expressionS exp;
10579
10580 do
10581 {
10582 expression (&exp);
10583 if (exp.X_op == O_symbol)
10584 exp.X_op = O_secidx;
10585
10586 emit_expr (&exp, 2);
10587 }
10588 while (*input_line_pointer++ == ',');
10589
10590 input_line_pointer--;
10591 demand_empty_rest_of_line ();
10592}
6482c264
NC
10593#endif
10594
7063667e
JB
10595/* Handle Rounding Control / SAE specifiers. */
10596
10597static char *
10598RC_SAE_specifier (const char *pstr)
10599{
10600 unsigned int j;
10601
10602 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); j++)
10603 {
10604 if (!strncmp (pstr, RC_NamesTable[j].name, RC_NamesTable[j].len))
10605 {
10606 if (i.rounding.type != rc_none)
10607 {
10608 as_bad (_("duplicated `{%s}'"), RC_NamesTable[j].name);
10609 return NULL;
10610 }
10611
10612 i.rounding.type = RC_NamesTable[j].type;
10613
10614 return (char *)(pstr + RC_NamesTable[j].len);
10615 }
10616 }
10617
10618 return NULL;
10619}
10620
43234a1e
L
10621/* Handle Vector operations. */
10622
10623static char *
f70c6814 10624check_VecOperations (char *op_string)
43234a1e
L
10625{
10626 const reg_entry *mask;
10627 const char *saved;
10628 char *end_op;
10629
f70c6814 10630 while (*op_string)
43234a1e
L
10631 {
10632 saved = op_string;
10633 if (*op_string == '{')
10634 {
10635 op_string++;
10636
10637 /* Check broadcasts. */
d34049e8 10638 if (startswith (op_string, "1to"))
43234a1e 10639 {
5273a3cd 10640 unsigned int bcst_type;
43234a1e 10641
5273a3cd 10642 if (i.broadcast.type)
43234a1e
L
10643 goto duplicated_vec_op;
10644
10645 op_string += 3;
10646 if (*op_string == '8')
8e6e0792 10647 bcst_type = 8;
b28d1bda 10648 else if (*op_string == '4')
8e6e0792 10649 bcst_type = 4;
b28d1bda 10650 else if (*op_string == '2')
8e6e0792 10651 bcst_type = 2;
43234a1e
L
10652 else if (*op_string == '1'
10653 && *(op_string+1) == '6')
10654 {
8e6e0792 10655 bcst_type = 16;
43234a1e
L
10656 op_string++;
10657 }
0cc78721
CL
10658 else if (*op_string == '3'
10659 && *(op_string+1) == '2')
10660 {
10661 bcst_type = 32;
10662 op_string++;
10663 }
43234a1e
L
10664 else
10665 {
10666 as_bad (_("Unsupported broadcast: `%s'"), saved);
10667 return NULL;
10668 }
10669 op_string++;
10670
5273a3cd
JB
10671 i.broadcast.type = bcst_type;
10672 i.broadcast.operand = this_operand;
43234a1e
L
10673 }
10674 /* Check masking operation. */
10675 else if ((mask = parse_register (op_string, &end_op)) != NULL)
10676 {
8a6fb3f9
JB
10677 if (mask == &bad_reg)
10678 return NULL;
10679
43234a1e 10680 /* k0 can't be used for write mask. */
f74a6307 10681 if (mask->reg_type.bitfield.class != RegMask || !mask->reg_num)
43234a1e 10682 {
6d2cd6b2
JB
10683 as_bad (_("`%s%s' can't be used for write mask"),
10684 register_prefix, mask->reg_name);
43234a1e
L
10685 return NULL;
10686 }
10687
6225c532 10688 if (!i.mask.reg)
43234a1e 10689 {
6225c532
JB
10690 i.mask.reg = mask;
10691 i.mask.operand = this_operand;
43234a1e 10692 }
6225c532
JB
10693 else if (i.mask.reg->reg_num)
10694 goto duplicated_vec_op;
43234a1e
L
10695 else
10696 {
6225c532 10697 i.mask.reg = mask;
43234a1e
L
10698
10699 /* Only "{z}" is allowed here. No need to check
10700 zeroing mask explicitly. */
6225c532 10701 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
10702 {
10703 as_bad (_("invalid write mask `%s'"), saved);
10704 return NULL;
10705 }
10706 }
10707
10708 op_string = end_op;
10709 }
10710 /* Check zeroing-flag for masking operation. */
10711 else if (*op_string == 'z')
10712 {
6225c532 10713 if (!i.mask.reg)
43234a1e 10714 {
6225c532
JB
10715 i.mask.reg = reg_k0;
10716 i.mask.zeroing = 1;
10717 i.mask.operand = this_operand;
43234a1e
L
10718 }
10719 else
10720 {
6225c532 10721 if (i.mask.zeroing)
43234a1e
L
10722 {
10723 duplicated_vec_op:
10724 as_bad (_("duplicated `%s'"), saved);
10725 return NULL;
10726 }
10727
6225c532 10728 i.mask.zeroing = 1;
43234a1e
L
10729
10730 /* Only "{%k}" is allowed here. No need to check mask
10731 register explicitly. */
6225c532 10732 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
10733 {
10734 as_bad (_("invalid zeroing-masking `%s'"),
10735 saved);
10736 return NULL;
10737 }
10738 }
10739
10740 op_string++;
10741 }
7063667e
JB
10742 else if (intel_syntax
10743 && (op_string = RC_SAE_specifier (op_string)) != NULL)
10744 i.rounding.modifier = true;
43234a1e
L
10745 else
10746 goto unknown_vec_op;
10747
10748 if (*op_string != '}')
10749 {
10750 as_bad (_("missing `}' in `%s'"), saved);
10751 return NULL;
10752 }
10753 op_string++;
0ba3a731
L
10754
10755 /* Strip whitespace since the addition of pseudo prefixes
10756 changed how the scrubber treats '{'. */
10757 if (is_space_char (*op_string))
10758 ++op_string;
10759
43234a1e
L
10760 continue;
10761 }
10762 unknown_vec_op:
10763 /* We don't know this one. */
10764 as_bad (_("unknown vector operation: `%s'"), saved);
10765 return NULL;
10766 }
10767
6225c532 10768 if (i.mask.reg && i.mask.zeroing && !i.mask.reg->reg_num)
6d2cd6b2
JB
10769 {
10770 as_bad (_("zeroing-masking only allowed with write mask"));
10771 return NULL;
10772 }
10773
43234a1e
L
10774 return op_string;
10775}
10776
252b5132 10777static int
70e41ade 10778i386_immediate (char *imm_start)
252b5132
RH
10779{
10780 char *save_input_line_pointer;
f3c180ae 10781 char *gotfree_input_line;
252b5132 10782 segT exp_seg = 0;
47926f60 10783 expressionS *exp;
40fb9820
L
10784 i386_operand_type types;
10785
0dfbf9d7 10786 operand_type_set (&types, ~0);
252b5132
RH
10787
10788 if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
10789 {
31b2323c
L
10790 as_bad (_("at most %d immediate operands are allowed"),
10791 MAX_IMMEDIATE_OPERANDS);
252b5132
RH
10792 return 0;
10793 }
10794
10795 exp = &im_expressions[i.imm_operands++];
520dc8e8 10796 i.op[this_operand].imms = exp;
252b5132
RH
10797
10798 if (is_space_char (*imm_start))
10799 ++imm_start;
10800
10801 save_input_line_pointer = input_line_pointer;
10802 input_line_pointer = imm_start;
10803
d258b828 10804 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
10805 if (gotfree_input_line)
10806 input_line_pointer = gotfree_input_line;
252b5132
RH
10807
10808 exp_seg = expression (exp);
10809
83183c0c 10810 SKIP_WHITESPACE ();
252b5132 10811 if (*input_line_pointer)
f3c180ae 10812 as_bad (_("junk `%s' after expression"), input_line_pointer);
252b5132
RH
10813
10814 input_line_pointer = save_input_line_pointer;
f3c180ae 10815 if (gotfree_input_line)
ee86248c
JB
10816 {
10817 free (gotfree_input_line);
10818
9aac24b1 10819 if (exp->X_op == O_constant)
ee86248c
JB
10820 exp->X_op = O_illegal;
10821 }
10822
9aac24b1
JB
10823 if (exp_seg == reg_section)
10824 {
10825 as_bad (_("illegal immediate register operand %s"), imm_start);
10826 return 0;
10827 }
10828
ee86248c
JB
10829 return i386_finalize_immediate (exp_seg, exp, types, imm_start);
10830}
252b5132 10831
ee86248c
JB
10832static int
10833i386_finalize_immediate (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
10834 i386_operand_type types, const char *imm_start)
10835{
10836 if (exp->X_op == O_absent || exp->X_op == O_illegal || exp->X_op == O_big)
252b5132 10837 {
313c53d1
L
10838 if (imm_start)
10839 as_bad (_("missing or invalid immediate expression `%s'"),
10840 imm_start);
3992d3b7 10841 return 0;
252b5132 10842 }
3e73aa7c 10843 else if (exp->X_op == O_constant)
252b5132 10844 {
47926f60 10845 /* Size it properly later. */
40fb9820 10846 i.types[this_operand].bitfield.imm64 = 1;
a442cac5
JB
10847
10848 /* If not 64bit, sign/zero extend val, to account for wraparound
10849 when !BFD64. */
10850 if (flag_code != CODE_64BIT)
10851 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
252b5132 10852 }
4c63da97 10853#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
f86103b7 10854 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
31312f95 10855 && exp_seg != absolute_section
47926f60 10856 && exp_seg != text_section
24eab124
AM
10857 && exp_seg != data_section
10858 && exp_seg != bss_section
10859 && exp_seg != undefined_section
f86103b7 10860 && !bfd_is_com_section (exp_seg))
252b5132 10861 {
d0b47220 10862 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
252b5132
RH
10863 return 0;
10864 }
10865#endif
10866 else
10867 {
10868 /* This is an address. The size of the address will be
24eab124 10869 determined later, depending on destination register,
3e73aa7c 10870 suffix, or the default for the section. */
40fb9820
L
10871 i.types[this_operand].bitfield.imm8 = 1;
10872 i.types[this_operand].bitfield.imm16 = 1;
10873 i.types[this_operand].bitfield.imm32 = 1;
10874 i.types[this_operand].bitfield.imm32s = 1;
10875 i.types[this_operand].bitfield.imm64 = 1;
c6fb90c8
L
10876 i.types[this_operand] = operand_type_and (i.types[this_operand],
10877 types);
252b5132
RH
10878 }
10879
10880 return 1;
10881}
10882
551c1ca1 10883static char *
e3bb37b5 10884i386_scale (char *scale)
252b5132 10885{
551c1ca1
AM
10886 offsetT val;
10887 char *save = input_line_pointer;
252b5132 10888
551c1ca1
AM
10889 input_line_pointer = scale;
10890 val = get_absolute_expression ();
10891
10892 switch (val)
252b5132 10893 {
551c1ca1 10894 case 1:
252b5132
RH
10895 i.log2_scale_factor = 0;
10896 break;
551c1ca1 10897 case 2:
252b5132
RH
10898 i.log2_scale_factor = 1;
10899 break;
551c1ca1 10900 case 4:
252b5132
RH
10901 i.log2_scale_factor = 2;
10902 break;
551c1ca1 10903 case 8:
252b5132
RH
10904 i.log2_scale_factor = 3;
10905 break;
10906 default:
a724f0f4
JB
10907 {
10908 char sep = *input_line_pointer;
10909
10910 *input_line_pointer = '\0';
10911 as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
10912 scale);
10913 *input_line_pointer = sep;
10914 input_line_pointer = save;
10915 return NULL;
10916 }
252b5132 10917 }
29b0f896 10918 if (i.log2_scale_factor != 0 && i.index_reg == 0)
252b5132
RH
10919 {
10920 as_warn (_("scale factor of %d without an index register"),
24eab124 10921 1 << i.log2_scale_factor);
252b5132 10922 i.log2_scale_factor = 0;
252b5132 10923 }
551c1ca1
AM
10924 scale = input_line_pointer;
10925 input_line_pointer = save;
10926 return scale;
252b5132
RH
10927}
10928
252b5132 10929static int
e3bb37b5 10930i386_displacement (char *disp_start, char *disp_end)
252b5132 10931{
29b0f896 10932 expressionS *exp;
252b5132
RH
10933 segT exp_seg = 0;
10934 char *save_input_line_pointer;
f3c180ae 10935 char *gotfree_input_line;
40fb9820
L
10936 int override;
10937 i386_operand_type bigdisp, types = anydisp;
3992d3b7 10938 int ret;
252b5132 10939
31b2323c
L
10940 if (i.disp_operands == MAX_MEMORY_OPERANDS)
10941 {
10942 as_bad (_("at most %d displacement operands are allowed"),
10943 MAX_MEMORY_OPERANDS);
10944 return 0;
10945 }
10946
0dfbf9d7 10947 operand_type_set (&bigdisp, 0);
6f2f06be 10948 if (i.jumpabsolute
48bcea9f 10949 || i.types[this_operand].bitfield.baseindex
0cfa3eb3
JB
10950 || (current_templates->start->opcode_modifier.jump != JUMP
10951 && current_templates->start->opcode_modifier.jump != JUMP_DWORD))
e05278af 10952 {
48bcea9f 10953 i386_addressing_mode ();
e05278af 10954 override = (i.prefix[ADDR_PREFIX] != 0);
40fb9820
L
10955 if (flag_code == CODE_64BIT)
10956 {
a775efc8 10957 bigdisp.bitfield.disp32 = 1;
40fb9820 10958 if (!override)
a775efc8 10959 bigdisp.bitfield.disp64 = 1;
40fb9820
L
10960 }
10961 else if ((flag_code == CODE_16BIT) ^ override)
40fb9820 10962 bigdisp.bitfield.disp16 = 1;
48bcea9f
JB
10963 else
10964 bigdisp.bitfield.disp32 = 1;
e05278af
JB
10965 }
10966 else
10967 {
376cd056
JB
10968 /* For PC-relative branches, the width of the displacement may be
10969 dependent upon data size, but is never dependent upon address size.
10970 Also make sure to not unintentionally match against a non-PC-relative
10971 branch template. */
10972 static templates aux_templates;
10973 const insn_template *t = current_templates->start;
5b7c81bd 10974 bool has_intel64 = false;
376cd056
JB
10975
10976 aux_templates.start = t;
10977 while (++t < current_templates->end)
10978 {
10979 if (t->opcode_modifier.jump
10980 != current_templates->start->opcode_modifier.jump)
10981 break;
4b5aaf5f 10982 if ((t->opcode_modifier.isa64 >= INTEL64))
5b7c81bd 10983 has_intel64 = true;
376cd056
JB
10984 }
10985 if (t < current_templates->end)
10986 {
10987 aux_templates.end = t;
10988 current_templates = &aux_templates;
10989 }
10990
e05278af 10991 override = (i.prefix[DATA_PREFIX] != 0);
40fb9820
L
10992 if (flag_code == CODE_64BIT)
10993 {
376cd056
JB
10994 if ((override || i.suffix == WORD_MNEM_SUFFIX)
10995 && (!intel64 || !has_intel64))
40fb9820
L
10996 bigdisp.bitfield.disp16 = 1;
10997 else
a775efc8 10998 bigdisp.bitfield.disp32 = 1;
40fb9820
L
10999 }
11000 else
e05278af
JB
11001 {
11002 if (!override)
11003 override = (i.suffix == (flag_code != CODE_16BIT
11004 ? WORD_MNEM_SUFFIX
11005 : LONG_MNEM_SUFFIX));
40fb9820
L
11006 bigdisp.bitfield.disp32 = 1;
11007 if ((flag_code == CODE_16BIT) ^ override)
11008 {
11009 bigdisp.bitfield.disp32 = 0;
11010 bigdisp.bitfield.disp16 = 1;
11011 }
e05278af 11012 }
e05278af 11013 }
c6fb90c8
L
11014 i.types[this_operand] = operand_type_or (i.types[this_operand],
11015 bigdisp);
252b5132
RH
11016
11017 exp = &disp_expressions[i.disp_operands];
520dc8e8 11018 i.op[this_operand].disps = exp;
252b5132
RH
11019 i.disp_operands++;
11020 save_input_line_pointer = input_line_pointer;
11021 input_line_pointer = disp_start;
11022 END_STRING_AND_SAVE (disp_end);
11023
11024#ifndef GCC_ASM_O_HACK
11025#define GCC_ASM_O_HACK 0
11026#endif
11027#if GCC_ASM_O_HACK
11028 END_STRING_AND_SAVE (disp_end + 1);
40fb9820 11029 if (i.types[this_operand].bitfield.baseIndex
24eab124 11030 && displacement_string_end[-1] == '+')
252b5132
RH
11031 {
11032 /* This hack is to avoid a warning when using the "o"
24eab124
AM
11033 constraint within gcc asm statements.
11034 For instance:
11035
11036 #define _set_tssldt_desc(n,addr,limit,type) \
11037 __asm__ __volatile__ ( \
11038 "movw %w2,%0\n\t" \
11039 "movw %w1,2+%0\n\t" \
11040 "rorl $16,%1\n\t" \
11041 "movb %b1,4+%0\n\t" \
11042 "movb %4,5+%0\n\t" \
11043 "movb $0,6+%0\n\t" \
11044 "movb %h1,7+%0\n\t" \
11045 "rorl $16,%1" \
11046 : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
11047
11048 This works great except that the output assembler ends
11049 up looking a bit weird if it turns out that there is
11050 no offset. You end up producing code that looks like:
11051
11052 #APP
11053 movw $235,(%eax)
11054 movw %dx,2+(%eax)
11055 rorl $16,%edx
11056 movb %dl,4+(%eax)
11057 movb $137,5+(%eax)
11058 movb $0,6+(%eax)
11059 movb %dh,7+(%eax)
11060 rorl $16,%edx
11061 #NO_APP
11062
47926f60 11063 So here we provide the missing zero. */
24eab124
AM
11064
11065 *displacement_string_end = '0';
252b5132
RH
11066 }
11067#endif
d258b828 11068 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
11069 if (gotfree_input_line)
11070 input_line_pointer = gotfree_input_line;
252b5132 11071
24eab124 11072 exp_seg = expression (exp);
252b5132 11073
636c26b0
AM
11074 SKIP_WHITESPACE ();
11075 if (*input_line_pointer)
11076 as_bad (_("junk `%s' after expression"), input_line_pointer);
11077#if GCC_ASM_O_HACK
11078 RESTORE_END_STRING (disp_end + 1);
11079#endif
636c26b0 11080 input_line_pointer = save_input_line_pointer;
636c26b0 11081 if (gotfree_input_line)
ee86248c
JB
11082 {
11083 free (gotfree_input_line);
11084
11085 if (exp->X_op == O_constant || exp->X_op == O_register)
11086 exp->X_op = O_illegal;
11087 }
11088
11089 ret = i386_finalize_displacement (exp_seg, exp, types, disp_start);
11090
11091 RESTORE_END_STRING (disp_end);
11092
11093 return ret;
11094}
11095
11096static int
11097i386_finalize_displacement (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
11098 i386_operand_type types, const char *disp_start)
11099{
ee86248c 11100 int ret = 1;
636c26b0 11101
24eab124
AM
11102 /* We do this to make sure that the section symbol is in
11103 the symbol table. We will ultimately change the relocation
47926f60 11104 to be relative to the beginning of the section. */
1ae12ab7 11105 if (i.reloc[this_operand] == BFD_RELOC_386_GOTOFF
d6ab8113
JB
11106 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
11107 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
24eab124 11108 {
636c26b0 11109 if (exp->X_op != O_symbol)
3992d3b7 11110 goto inv_disp;
636c26b0 11111
e5cb08ac 11112 if (S_IS_LOCAL (exp->X_add_symbol)
c64efb4b
L
11113 && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section
11114 && S_GET_SEGMENT (exp->X_add_symbol) != expr_section)
24eab124 11115 section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
24eab124
AM
11116 exp->X_op = O_subtract;
11117 exp->X_op_symbol = GOT_symbol;
1ae12ab7 11118 if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
29b0f896 11119 i.reloc[this_operand] = BFD_RELOC_32_PCREL;
d6ab8113
JB
11120 else if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
11121 i.reloc[this_operand] = BFD_RELOC_64;
23df1078 11122 else
29b0f896 11123 i.reloc[this_operand] = BFD_RELOC_32;
24eab124 11124 }
252b5132 11125
3992d3b7
AM
11126 else if (exp->X_op == O_absent
11127 || exp->X_op == O_illegal
ee86248c 11128 || exp->X_op == O_big)
2daf4fd8 11129 {
3992d3b7
AM
11130 inv_disp:
11131 as_bad (_("missing or invalid displacement expression `%s'"),
2daf4fd8 11132 disp_start);
3992d3b7 11133 ret = 0;
2daf4fd8
AM
11134 }
11135
a50187b2
JB
11136 else if (exp->X_op == O_constant)
11137 {
11138 /* Sizing gets taken care of by optimize_disp().
11139
11140 If not 64bit, sign/zero extend val, to account for wraparound
11141 when !BFD64. */
11142 if (flag_code != CODE_64BIT)
11143 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
11144 }
11145
4c63da97 11146#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
a50187b2 11147 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
3992d3b7
AM
11148 && exp_seg != absolute_section
11149 && exp_seg != text_section
11150 && exp_seg != data_section
11151 && exp_seg != bss_section
11152 && exp_seg != undefined_section
11153 && !bfd_is_com_section (exp_seg))
24eab124 11154 {
d0b47220 11155 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
3992d3b7 11156 ret = 0;
24eab124 11157 }
252b5132 11158#endif
3956db08 11159
a50187b2 11160 else if (current_templates->start->opcode_modifier.jump == JUMP_BYTE)
48bcea9f
JB
11161 i.types[this_operand].bitfield.disp8 = 1;
11162
40fb9820 11163 /* Check if this is a displacement only operand. */
02b83698
JB
11164 if (!i.types[this_operand].bitfield.baseindex)
11165 i.types[this_operand] =
11166 operand_type_or (operand_type_and_not (i.types[this_operand], anydisp),
11167 operand_type_and (i.types[this_operand], types));
3956db08 11168
3992d3b7 11169 return ret;
252b5132
RH
11170}
11171
2abc2bec
JB
11172/* Return the active addressing mode, taking address override and
11173 registers forming the address into consideration. Update the
11174 address override prefix if necessary. */
47926f60 11175
2abc2bec
JB
11176static enum flag_code
11177i386_addressing_mode (void)
252b5132 11178{
be05d201
L
11179 enum flag_code addr_mode;
11180
11181 if (i.prefix[ADDR_PREFIX])
11182 addr_mode = flag_code == CODE_32BIT ? CODE_16BIT : CODE_32BIT;
a23b33b3
JB
11183 else if (flag_code == CODE_16BIT
11184 && current_templates->start->cpu_flags.bitfield.cpumpx
11185 /* Avoid replacing the "16-bit addressing not allowed" diagnostic
11186 from md_assemble() by "is not a valid base/index expression"
11187 when there is a base and/or index. */
11188 && !i.types[this_operand].bitfield.baseindex)
11189 {
11190 /* MPX insn memory operands with neither base nor index must be forced
11191 to use 32-bit addressing in 16-bit mode. */
11192 addr_mode = CODE_32BIT;
11193 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
11194 ++i.prefixes;
11195 gas_assert (!i.types[this_operand].bitfield.disp16);
11196 gas_assert (!i.types[this_operand].bitfield.disp32);
11197 }
be05d201
L
11198 else
11199 {
11200 addr_mode = flag_code;
11201
24eab124 11202#if INFER_ADDR_PREFIX
be05d201
L
11203 if (i.mem_operands == 0)
11204 {
11205 /* Infer address prefix from the first memory operand. */
11206 const reg_entry *addr_reg = i.base_reg;
11207
11208 if (addr_reg == NULL)
11209 addr_reg = i.index_reg;
eecb386c 11210
be05d201
L
11211 if (addr_reg)
11212 {
e968fc9b 11213 if (addr_reg->reg_type.bitfield.dword)
be05d201
L
11214 addr_mode = CODE_32BIT;
11215 else if (flag_code != CODE_64BIT
dc821c5f 11216 && addr_reg->reg_type.bitfield.word)
be05d201
L
11217 addr_mode = CODE_16BIT;
11218
11219 if (addr_mode != flag_code)
11220 {
11221 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
11222 i.prefixes += 1;
11223 /* Change the size of any displacement too. At most one
11224 of Disp16 or Disp32 is set.
11225 FIXME. There doesn't seem to be any real need for
11226 separate Disp16 and Disp32 flags. The same goes for
11227 Imm16 and Imm32. Removing them would probably clean
11228 up the code quite a lot. */
11229 if (flag_code != CODE_64BIT
11230 && (i.types[this_operand].bitfield.disp16
11231 || i.types[this_operand].bitfield.disp32))
11232 i.types[this_operand]
11233 = operand_type_xor (i.types[this_operand], disp16_32);
11234 }
11235 }
11236 }
24eab124 11237#endif
be05d201
L
11238 }
11239
2abc2bec
JB
11240 return addr_mode;
11241}
11242
11243/* Make sure the memory operand we've been dealt is valid.
11244 Return 1 on success, 0 on a failure. */
11245
11246static int
11247i386_index_check (const char *operand_string)
11248{
11249 const char *kind = "base/index";
11250 enum flag_code addr_mode = i386_addressing_mode ();
a152332d 11251 const insn_template *t = current_templates->start;
2abc2bec 11252
a152332d 11253 if (t->opcode_modifier.isstring
fc0763e6
JB
11254 && (current_templates->end[-1].opcode_modifier.isstring
11255 || i.mem_operands))
11256 {
11257 /* Memory operands of string insns are special in that they only allow
11258 a single register (rDI, rSI, or rBX) as their memory address. */
be05d201
L
11259 const reg_entry *expected_reg;
11260 static const char *di_si[][2] =
11261 {
11262 { "esi", "edi" },
11263 { "si", "di" },
11264 { "rsi", "rdi" }
11265 };
11266 static const char *bx[] = { "ebx", "bx", "rbx" };
fc0763e6
JB
11267
11268 kind = "string address";
11269
a152332d 11270 if (t->opcode_modifier.prefixok == PrefixRep)
fc0763e6 11271 {
51c8edf6
JB
11272 int es_op = current_templates->end[-1].opcode_modifier.isstring
11273 - IS_STRING_ES_OP0;
11274 int op = 0;
fc0763e6 11275
51c8edf6 11276 if (!current_templates->end[-1].operand_types[0].bitfield.baseindex
fc0763e6
JB
11277 || ((!i.mem_operands != !intel_syntax)
11278 && current_templates->end[-1].operand_types[1]
11279 .bitfield.baseindex))
51c8edf6 11280 op = 1;
fe0e921f
AM
11281 expected_reg
11282 = (const reg_entry *) str_hash_find (reg_hash,
11283 di_si[addr_mode][op == es_op]);
fc0763e6
JB
11284 }
11285 else
fe0e921f
AM
11286 expected_reg
11287 = (const reg_entry *)str_hash_find (reg_hash, bx[addr_mode]);
fc0763e6 11288
be05d201
L
11289 if (i.base_reg != expected_reg
11290 || i.index_reg
fc0763e6 11291 || operand_type_check (i.types[this_operand], disp))
fc0763e6 11292 {
be05d201
L
11293 /* The second memory operand must have the same size as
11294 the first one. */
11295 if (i.mem_operands
11296 && i.base_reg
11297 && !((addr_mode == CODE_64BIT
dc821c5f 11298 && i.base_reg->reg_type.bitfield.qword)
be05d201 11299 || (addr_mode == CODE_32BIT
dc821c5f
JB
11300 ? i.base_reg->reg_type.bitfield.dword
11301 : i.base_reg->reg_type.bitfield.word)))
be05d201
L
11302 goto bad_address;
11303
fc0763e6
JB
11304 as_warn (_("`%s' is not valid here (expected `%c%s%s%c')"),
11305 operand_string,
11306 intel_syntax ? '[' : '(',
11307 register_prefix,
be05d201 11308 expected_reg->reg_name,
fc0763e6 11309 intel_syntax ? ']' : ')');
be05d201 11310 return 1;
fc0763e6 11311 }
be05d201
L
11312 else
11313 return 1;
11314
dc1e8a47 11315 bad_address:
be05d201
L
11316 as_bad (_("`%s' is not a valid %s expression"),
11317 operand_string, kind);
11318 return 0;
3e73aa7c
JH
11319 }
11320 else
11321 {
be05d201
L
11322 if (addr_mode != CODE_16BIT)
11323 {
11324 /* 32-bit/64-bit checks. */
41eb8e88
L
11325 if (i.disp_encoding == disp_encoding_16bit)
11326 {
11327 bad_disp:
11328 as_bad (_("invalid `%s' prefix"),
11329 addr_mode == CODE_16BIT ? "{disp32}" : "{disp16}");
11330 return 0;
11331 }
11332
be05d201 11333 if ((i.base_reg
e968fc9b
JB
11334 && ((addr_mode == CODE_64BIT
11335 ? !i.base_reg->reg_type.bitfield.qword
11336 : !i.base_reg->reg_type.bitfield.dword)
11337 || (i.index_reg && i.base_reg->reg_num == RegIP)
11338 || i.base_reg->reg_num == RegIZ))
be05d201 11339 || (i.index_reg
1b54b8d7
JB
11340 && !i.index_reg->reg_type.bitfield.xmmword
11341 && !i.index_reg->reg_type.bitfield.ymmword
11342 && !i.index_reg->reg_type.bitfield.zmmword
be05d201 11343 && ((addr_mode == CODE_64BIT
e968fc9b
JB
11344 ? !i.index_reg->reg_type.bitfield.qword
11345 : !i.index_reg->reg_type.bitfield.dword)
be05d201
L
11346 || !i.index_reg->reg_type.bitfield.baseindex)))
11347 goto bad_address;
8178be5b 11348
260cd341 11349 /* bndmk, bndldx, bndstx and mandatory non-vector SIB have special restrictions. */
a152332d 11350 if ((t->opcode_modifier.opcodeprefix == PREFIX_0XF3
389d00a5
JB
11351 && t->opcode_modifier.opcodespace == SPACE_0F
11352 && t->base_opcode == 0x1b)
a152332d 11353 || (t->opcode_modifier.opcodeprefix == PREFIX_NONE
389d00a5
JB
11354 && t->opcode_modifier.opcodespace == SPACE_0F
11355 && (t->base_opcode & ~1) == 0x1a)
a152332d 11356 || t->opcode_modifier.sib == SIBMEM)
8178be5b
JB
11357 {
11358 /* They cannot use RIP-relative addressing. */
e968fc9b 11359 if (i.base_reg && i.base_reg->reg_num == RegIP)
8178be5b
JB
11360 {
11361 as_bad (_("`%s' cannot be used here"), operand_string);
11362 return 0;
11363 }
11364
11365 /* bndldx and bndstx ignore their scale factor. */
a152332d 11366 if (t->opcode_modifier.opcodeprefix == PREFIX_NONE
389d00a5
JB
11367 && t->opcode_modifier.opcodespace == SPACE_0F
11368 && (t->base_opcode & ~1) == 0x1a
8178be5b
JB
11369 && i.log2_scale_factor)
11370 as_warn (_("register scaling is being ignored here"));
11371 }
be05d201
L
11372 }
11373 else
3e73aa7c 11374 {
be05d201 11375 /* 16-bit checks. */
41eb8e88
L
11376 if (i.disp_encoding == disp_encoding_32bit)
11377 goto bad_disp;
11378
3e73aa7c 11379 if ((i.base_reg
dc821c5f 11380 && (!i.base_reg->reg_type.bitfield.word
40fb9820 11381 || !i.base_reg->reg_type.bitfield.baseindex))
3e73aa7c 11382 || (i.index_reg
dc821c5f 11383 && (!i.index_reg->reg_type.bitfield.word
40fb9820 11384 || !i.index_reg->reg_type.bitfield.baseindex
29b0f896
AM
11385 || !(i.base_reg
11386 && i.base_reg->reg_num < 6
11387 && i.index_reg->reg_num >= 6
11388 && i.log2_scale_factor == 0))))
be05d201 11389 goto bad_address;
3e73aa7c
JH
11390 }
11391 }
be05d201 11392 return 1;
24eab124 11393}
252b5132 11394
43234a1e
L
11395/* Handle vector immediates. */
11396
11397static int
11398RC_SAE_immediate (const char *imm_start)
11399{
43234a1e 11400 const char *pstr = imm_start;
43234a1e
L
11401
11402 if (*pstr != '{')
11403 return 0;
11404
7063667e
JB
11405 pstr = RC_SAE_specifier (pstr + 1);
11406 if (pstr == NULL)
43234a1e
L
11407 return 0;
11408
11409 if (*pstr++ != '}')
11410 {
11411 as_bad (_("Missing '}': '%s'"), imm_start);
11412 return 0;
11413 }
11414 /* RC/SAE immediate string should contain nothing more. */;
11415 if (*pstr != 0)
11416 {
11417 as_bad (_("Junk after '}': '%s'"), imm_start);
11418 return 0;
11419 }
11420
cf665fee
JB
11421 /* Internally this doesn't count as an operand. */
11422 --i.operands;
43234a1e 11423
43234a1e
L
11424 return 1;
11425}
11426
8325cc63
JB
11427/* Only string instructions can have a second memory operand, so
11428 reduce current_templates to just those if it contains any. */
11429static int
11430maybe_adjust_templates (void)
11431{
11432 const insn_template *t;
11433
11434 gas_assert (i.mem_operands == 1);
11435
11436 for (t = current_templates->start; t < current_templates->end; ++t)
11437 if (t->opcode_modifier.isstring)
11438 break;
11439
11440 if (t < current_templates->end)
11441 {
11442 static templates aux_templates;
5b7c81bd 11443 bool recheck;
8325cc63
JB
11444
11445 aux_templates.start = t;
11446 for (; t < current_templates->end; ++t)
11447 if (!t->opcode_modifier.isstring)
11448 break;
11449 aux_templates.end = t;
11450
11451 /* Determine whether to re-check the first memory operand. */
11452 recheck = (aux_templates.start != current_templates->start
11453 || t != current_templates->end);
11454
11455 current_templates = &aux_templates;
11456
11457 if (recheck)
11458 {
11459 i.mem_operands = 0;
11460 if (i.memop1_string != NULL
11461 && i386_index_check (i.memop1_string) == 0)
11462 return 0;
11463 i.mem_operands = 1;
11464 }
11465 }
11466
11467 return 1;
11468}
11469
9d299bea
JB
11470static INLINE bool starts_memory_operand (char c)
11471{
014fbcda 11472 return ISDIGIT (c)
9d299bea 11473 || is_identifier_char (c)
014fbcda 11474 || strchr ("([\"+-!~", c);
9d299bea
JB
11475}
11476
fc0763e6 11477/* Parse OPERAND_STRING into the i386_insn structure I. Returns zero
47926f60 11478 on error. */
252b5132 11479
252b5132 11480static int
a7619375 11481i386_att_operand (char *operand_string)
252b5132 11482{
af6bdddf
AM
11483 const reg_entry *r;
11484 char *end_op;
24eab124 11485 char *op_string = operand_string;
252b5132 11486
24eab124 11487 if (is_space_char (*op_string))
252b5132
RH
11488 ++op_string;
11489
24eab124 11490 /* We check for an absolute prefix (differentiating,
47926f60 11491 for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
24eab124
AM
11492 if (*op_string == ABSOLUTE_PREFIX)
11493 {
11494 ++op_string;
11495 if (is_space_char (*op_string))
11496 ++op_string;
5b7c81bd 11497 i.jumpabsolute = true;
24eab124 11498 }
252b5132 11499
47926f60 11500 /* Check if operand is a register. */
4d1bb795 11501 if ((r = parse_register (op_string, &end_op)) != NULL)
24eab124 11502 {
40fb9820
L
11503 i386_operand_type temp;
11504
8a6fb3f9
JB
11505 if (r == &bad_reg)
11506 return 0;
11507
24eab124
AM
11508 /* Check for a segment override by searching for ':' after a
11509 segment register. */
11510 op_string = end_op;
11511 if (is_space_char (*op_string))
11512 ++op_string;
00cee14f 11513 if (*op_string == ':' && r->reg_type.bitfield.class == SReg)
24eab124 11514 {
5e042380 11515 i.seg[i.mem_operands] = r;
252b5132 11516
24eab124 11517 /* Skip the ':' and whitespace. */
252b5132
RH
11518 ++op_string;
11519 if (is_space_char (*op_string))
24eab124 11520 ++op_string;
252b5132 11521
47926f60 11522 /* Handle case of %es:*foo. */
c8d541e2 11523 if (!i.jumpabsolute && *op_string == ABSOLUTE_PREFIX)
24eab124
AM
11524 {
11525 ++op_string;
11526 if (is_space_char (*op_string))
11527 ++op_string;
5b7c81bd 11528 i.jumpabsolute = true;
24eab124 11529 }
c8d541e2 11530
9d299bea 11531 if (!starts_memory_operand (*op_string))
c8d541e2
JB
11532 {
11533 as_bad (_("bad memory operand `%s'"), op_string);
11534 return 0;
11535 }
24eab124
AM
11536 goto do_memory_reference;
11537 }
43234a1e
L
11538
11539 /* Handle vector operations. */
11540 if (*op_string == '{')
11541 {
f70c6814 11542 op_string = check_VecOperations (op_string);
43234a1e
L
11543 if (op_string == NULL)
11544 return 0;
11545 }
11546
24eab124
AM
11547 if (*op_string)
11548 {
d0b47220 11549 as_bad (_("junk `%s' after register"), op_string);
24eab124
AM
11550 return 0;
11551 }
40fb9820
L
11552 temp = r->reg_type;
11553 temp.bitfield.baseindex = 0;
c6fb90c8
L
11554 i.types[this_operand] = operand_type_or (i.types[this_operand],
11555 temp);
7d5e4556 11556 i.types[this_operand].bitfield.unspecified = 0;
520dc8e8 11557 i.op[this_operand].regs = r;
24eab124 11558 i.reg_operands++;
cf665fee
JB
11559
11560 /* A GPR may follow an RC or SAE immediate only if a (vector) register
11561 operand was also present earlier on. */
11562 if (i.rounding.type != rc_none && temp.bitfield.class == Reg
11563 && i.reg_operands == 1)
11564 {
11565 unsigned int j;
11566
11567 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); ++j)
11568 if (i.rounding.type == RC_NamesTable[j].type)
11569 break;
11570 as_bad (_("`%s': misplaced `{%s}'"),
11571 current_templates->start->name, RC_NamesTable[j].name);
11572 return 0;
11573 }
24eab124 11574 }
af6bdddf
AM
11575 else if (*op_string == REGISTER_PREFIX)
11576 {
11577 as_bad (_("bad register name `%s'"), op_string);
11578 return 0;
11579 }
24eab124 11580 else if (*op_string == IMMEDIATE_PREFIX)
ce8a8b2f 11581 {
24eab124 11582 ++op_string;
6f2f06be 11583 if (i.jumpabsolute)
24eab124 11584 {
d0b47220 11585 as_bad (_("immediate operand illegal with absolute jump"));
24eab124
AM
11586 return 0;
11587 }
11588 if (!i386_immediate (op_string))
11589 return 0;
cf665fee
JB
11590 if (i.rounding.type != rc_none)
11591 {
11592 as_bad (_("`%s': RC/SAE operand must follow immediate operands"),
11593 current_templates->start->name);
11594 return 0;
11595 }
24eab124 11596 }
43234a1e
L
11597 else if (RC_SAE_immediate (operand_string))
11598 {
cf665fee
JB
11599 /* If it is a RC or SAE immediate, do the necessary placement check:
11600 Only another immediate or a GPR may precede it. */
11601 if (i.mem_operands || i.reg_operands + i.imm_operands > 1
11602 || (i.reg_operands == 1
11603 && i.op[0].regs->reg_type.bitfield.class != Reg))
11604 {
11605 as_bad (_("`%s': misplaced `%s'"),
11606 current_templates->start->name, operand_string);
11607 return 0;
11608 }
43234a1e 11609 }
9d299bea 11610 else if (starts_memory_operand (*op_string))
24eab124 11611 {
47926f60 11612 /* This is a memory reference of some sort. */
af6bdddf 11613 char *base_string;
252b5132 11614
47926f60 11615 /* Start and end of displacement string expression (if found). */
eecb386c
AM
11616 char *displacement_string_start;
11617 char *displacement_string_end;
252b5132 11618
24eab124 11619 do_memory_reference:
8325cc63
JB
11620 if (i.mem_operands == 1 && !maybe_adjust_templates ())
11621 return 0;
24eab124 11622 if ((i.mem_operands == 1
40fb9820 11623 && !current_templates->start->opcode_modifier.isstring)
24eab124
AM
11624 || i.mem_operands == 2)
11625 {
11626 as_bad (_("too many memory references for `%s'"),
11627 current_templates->start->name);
11628 return 0;
11629 }
252b5132 11630
24eab124
AM
11631 /* Check for base index form. We detect the base index form by
11632 looking for an ')' at the end of the operand, searching
11633 for the '(' matching it, and finding a REGISTER_PREFIX or ','
11634 after the '('. */
af6bdddf 11635 base_string = op_string + strlen (op_string);
c3332e24 11636
43234a1e 11637 /* Handle vector operations. */
6b5ba0d4
JB
11638 --base_string;
11639 if (is_space_char (*base_string))
11640 --base_string;
11641
11642 if (*base_string == '}')
43234a1e 11643 {
6b5ba0d4
JB
11644 char *vop_start = NULL;
11645
11646 while (base_string-- > op_string)
11647 {
11648 if (*base_string == '"')
11649 break;
11650 if (*base_string != '{')
11651 continue;
11652
11653 vop_start = base_string;
11654
11655 --base_string;
11656 if (is_space_char (*base_string))
11657 --base_string;
11658
11659 if (*base_string != '}')
11660 break;
11661
11662 vop_start = NULL;
11663 }
11664
11665 if (!vop_start)
11666 {
11667 as_bad (_("unbalanced figure braces"));
11668 return 0;
11669 }
11670
f70c6814 11671 if (check_VecOperations (vop_start) == NULL)
43234a1e 11672 return 0;
43234a1e
L
11673 }
11674
47926f60 11675 /* If we only have a displacement, set-up for it to be parsed later. */
af6bdddf
AM
11676 displacement_string_start = op_string;
11677 displacement_string_end = base_string + 1;
252b5132 11678
24eab124
AM
11679 if (*base_string == ')')
11680 {
af6bdddf 11681 char *temp_string;
e87fb6a6
JB
11682 unsigned int parens_not_balanced = 0;
11683 bool in_quotes = false;
e68c3d59 11684
24eab124 11685 /* We've already checked that the number of left & right ()'s are
e87fb6a6
JB
11686 equal, and that there's a matching set of double quotes. */
11687 end_op = base_string;
11688 for (temp_string = op_string; temp_string < end_op; temp_string++)
24eab124 11689 {
e87fb6a6
JB
11690 if (*temp_string == '\\' && temp_string[1] == '"')
11691 ++temp_string;
11692 else if (*temp_string == '"')
11693 in_quotes = !in_quotes;
11694 else if (!in_quotes)
11695 {
11696 if (*temp_string == '(' && !parens_not_balanced++)
11697 base_string = temp_string;
11698 if (*temp_string == ')')
11699 --parens_not_balanced;
11700 }
24eab124 11701 }
c3332e24 11702
af6bdddf 11703 temp_string = base_string;
c3332e24 11704
24eab124 11705 /* Skip past '(' and whitespace. */
e87fb6a6
JB
11706 gas_assert (*base_string == '(');
11707 ++base_string;
252b5132 11708 if (is_space_char (*base_string))
24eab124 11709 ++base_string;
252b5132 11710
af6bdddf 11711 if (*base_string == ','
4eed87de
AM
11712 || ((i.base_reg = parse_register (base_string, &end_op))
11713 != NULL))
252b5132 11714 {
af6bdddf 11715 displacement_string_end = temp_string;
252b5132 11716
40fb9820 11717 i.types[this_operand].bitfield.baseindex = 1;
252b5132 11718
af6bdddf 11719 if (i.base_reg)
24eab124 11720 {
8a6fb3f9
JB
11721 if (i.base_reg == &bad_reg)
11722 return 0;
24eab124
AM
11723 base_string = end_op;
11724 if (is_space_char (*base_string))
11725 ++base_string;
af6bdddf
AM
11726 }
11727
11728 /* There may be an index reg or scale factor here. */
11729 if (*base_string == ',')
11730 {
11731 ++base_string;
11732 if (is_space_char (*base_string))
11733 ++base_string;
11734
4eed87de
AM
11735 if ((i.index_reg = parse_register (base_string, &end_op))
11736 != NULL)
24eab124 11737 {
8a6fb3f9
JB
11738 if (i.index_reg == &bad_reg)
11739 return 0;
af6bdddf 11740 base_string = end_op;
24eab124
AM
11741 if (is_space_char (*base_string))
11742 ++base_string;
af6bdddf
AM
11743 if (*base_string == ',')
11744 {
11745 ++base_string;
11746 if (is_space_char (*base_string))
11747 ++base_string;
11748 }
e5cb08ac 11749 else if (*base_string != ')')
af6bdddf 11750 {
4eed87de
AM
11751 as_bad (_("expecting `,' or `)' "
11752 "after index register in `%s'"),
af6bdddf
AM
11753 operand_string);
11754 return 0;
11755 }
24eab124 11756 }
af6bdddf 11757 else if (*base_string == REGISTER_PREFIX)
24eab124 11758 {
f76bf5e0
L
11759 end_op = strchr (base_string, ',');
11760 if (end_op)
11761 *end_op = '\0';
af6bdddf 11762 as_bad (_("bad register name `%s'"), base_string);
24eab124
AM
11763 return 0;
11764 }
252b5132 11765
47926f60 11766 /* Check for scale factor. */
551c1ca1 11767 if (*base_string != ')')
af6bdddf 11768 {
551c1ca1
AM
11769 char *end_scale = i386_scale (base_string);
11770
11771 if (!end_scale)
af6bdddf 11772 return 0;
24eab124 11773
551c1ca1 11774 base_string = end_scale;
af6bdddf
AM
11775 if (is_space_char (*base_string))
11776 ++base_string;
11777 if (*base_string != ')')
11778 {
4eed87de
AM
11779 as_bad (_("expecting `)' "
11780 "after scale factor in `%s'"),
af6bdddf
AM
11781 operand_string);
11782 return 0;
11783 }
11784 }
11785 else if (!i.index_reg)
24eab124 11786 {
4eed87de
AM
11787 as_bad (_("expecting index register or scale factor "
11788 "after `,'; got '%c'"),
af6bdddf 11789 *base_string);
24eab124
AM
11790 return 0;
11791 }
11792 }
af6bdddf 11793 else if (*base_string != ')')
24eab124 11794 {
4eed87de
AM
11795 as_bad (_("expecting `,' or `)' "
11796 "after base register in `%s'"),
af6bdddf 11797 operand_string);
24eab124
AM
11798 return 0;
11799 }
c3332e24 11800 }
af6bdddf 11801 else if (*base_string == REGISTER_PREFIX)
c3332e24 11802 {
f76bf5e0
L
11803 end_op = strchr (base_string, ',');
11804 if (end_op)
11805 *end_op = '\0';
af6bdddf 11806 as_bad (_("bad register name `%s'"), base_string);
24eab124 11807 return 0;
c3332e24 11808 }
24eab124
AM
11809 }
11810
11811 /* If there's an expression beginning the operand, parse it,
11812 assuming displacement_string_start and
11813 displacement_string_end are meaningful. */
11814 if (displacement_string_start != displacement_string_end)
11815 {
11816 if (!i386_displacement (displacement_string_start,
11817 displacement_string_end))
11818 return 0;
11819 }
11820
11821 /* Special case for (%dx) while doing input/output op. */
11822 if (i.base_reg
75e5731b
JB
11823 && i.base_reg->reg_type.bitfield.instance == RegD
11824 && i.base_reg->reg_type.bitfield.word
24eab124
AM
11825 && i.index_reg == 0
11826 && i.log2_scale_factor == 0
11827 && i.seg[i.mem_operands] == 0
40fb9820 11828 && !operand_type_check (i.types[this_operand], disp))
24eab124 11829 {
2fb5be8d 11830 i.types[this_operand] = i.base_reg->reg_type;
9373f275 11831 i.input_output_operand = true;
24eab124
AM
11832 return 1;
11833 }
11834
eecb386c
AM
11835 if (i386_index_check (operand_string) == 0)
11836 return 0;
c48dadc9 11837 i.flags[this_operand] |= Operand_Mem;
8325cc63
JB
11838 if (i.mem_operands == 0)
11839 i.memop1_string = xstrdup (operand_string);
24eab124
AM
11840 i.mem_operands++;
11841 }
11842 else
ce8a8b2f
AM
11843 {
11844 /* It's not a memory operand; argh! */
24eab124
AM
11845 as_bad (_("invalid char %s beginning operand %d `%s'"),
11846 output_invalid (*op_string),
11847 this_operand + 1,
11848 op_string);
11849 return 0;
11850 }
47926f60 11851 return 1; /* Normal return. */
252b5132
RH
11852}
11853\f
fa94de6b
RM
11854/* Calculate the maximum variable size (i.e., excluding fr_fix)
11855 that an rs_machine_dependent frag may reach. */
11856
11857unsigned int
11858i386_frag_max_var (fragS *frag)
11859{
11860 /* The only relaxable frags are for jumps.
11861 Unconditional jumps can grow by 4 bytes and others by 5 bytes. */
11862 gas_assert (frag->fr_type == rs_machine_dependent);
11863 return TYPE_FROM_RELAX_STATE (frag->fr_subtype) == UNCOND_JUMP ? 4 : 5;
11864}
11865
b084df0b
L
11866#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
11867static int
8dcea932 11868elf_symbol_resolved_in_segment_p (symbolS *fr_symbol, offsetT fr_var)
b084df0b
L
11869{
11870 /* STT_GNU_IFUNC symbol must go through PLT. */
11871 if ((symbol_get_bfdsym (fr_symbol)->flags
11872 & BSF_GNU_INDIRECT_FUNCTION) != 0)
11873 return 0;
11874
11875 if (!S_IS_EXTERNAL (fr_symbol))
11876 /* Symbol may be weak or local. */
11877 return !S_IS_WEAK (fr_symbol);
11878
8dcea932
L
11879 /* Global symbols with non-default visibility can't be preempted. */
11880 if (ELF_ST_VISIBILITY (S_GET_OTHER (fr_symbol)) != STV_DEFAULT)
11881 return 1;
11882
11883 if (fr_var != NO_RELOC)
11884 switch ((enum bfd_reloc_code_real) fr_var)
11885 {
11886 case BFD_RELOC_386_PLT32:
11887 case BFD_RELOC_X86_64_PLT32:
33eaf5de 11888 /* Symbol with PLT relocation may be preempted. */
8dcea932
L
11889 return 0;
11890 default:
11891 abort ();
11892 }
11893
b084df0b
L
11894 /* Global symbols with default visibility in a shared library may be
11895 preempted by another definition. */
8dcea932 11896 return !shared;
b084df0b
L
11897}
11898#endif
11899
79d72f45
HL
11900/* Table 3-2. Macro-Fusible Instructions in Haswell Microarchitecture
11901 Note also work for Skylake and Cascadelake.
11902---------------------------------------------------------------------
11903| JCC | ADD/SUB/CMP | INC/DEC | TEST/AND |
11904| ------ | ----------- | ------- | -------- |
11905| Jo | N | N | Y |
11906| Jno | N | N | Y |
11907| Jc/Jb | Y | N | Y |
11908| Jae/Jnb | Y | N | Y |
11909| Je/Jz | Y | Y | Y |
11910| Jne/Jnz | Y | Y | Y |
11911| Jna/Jbe | Y | N | Y |
11912| Ja/Jnbe | Y | N | Y |
11913| Js | N | N | Y |
11914| Jns | N | N | Y |
11915| Jp/Jpe | N | N | Y |
11916| Jnp/Jpo | N | N | Y |
11917| Jl/Jnge | Y | Y | Y |
11918| Jge/Jnl | Y | Y | Y |
11919| Jle/Jng | Y | Y | Y |
11920| Jg/Jnle | Y | Y | Y |
11921--------------------------------------------------------------------- */
11922static int
11923i386_macro_fusible_p (enum mf_cmp_kind mf_cmp, enum mf_jcc_kind mf_jcc)
11924{
11925 if (mf_cmp == mf_cmp_alu_cmp)
11926 return ((mf_jcc >= mf_jcc_jc && mf_jcc <= mf_jcc_jna)
11927 || mf_jcc == mf_jcc_jl || mf_jcc == mf_jcc_jle);
11928 if (mf_cmp == mf_cmp_incdec)
11929 return (mf_jcc == mf_jcc_je || mf_jcc == mf_jcc_jl
11930 || mf_jcc == mf_jcc_jle);
11931 if (mf_cmp == mf_cmp_test_and)
11932 return 1;
11933 return 0;
11934}
11935
e379e5f3
L
11936/* Return the next non-empty frag. */
11937
11938static fragS *
11939i386_next_non_empty_frag (fragS *fragP)
11940{
11941 /* There may be a frag with a ".fill 0" when there is no room in
11942 the current frag for frag_grow in output_insn. */
11943 for (fragP = fragP->fr_next;
11944 (fragP != NULL
11945 && fragP->fr_type == rs_fill
11946 && fragP->fr_fix == 0);
11947 fragP = fragP->fr_next)
11948 ;
11949 return fragP;
11950}
11951
11952/* Return the next jcc frag after BRANCH_PADDING. */
11953
11954static fragS *
79d72f45 11955i386_next_fusible_jcc_frag (fragS *maybe_cmp_fragP, fragS *pad_fragP)
e379e5f3 11956{
79d72f45
HL
11957 fragS *branch_fragP;
11958 if (!pad_fragP)
e379e5f3
L
11959 return NULL;
11960
79d72f45
HL
11961 if (pad_fragP->fr_type == rs_machine_dependent
11962 && (TYPE_FROM_RELAX_STATE (pad_fragP->fr_subtype)
e379e5f3
L
11963 == BRANCH_PADDING))
11964 {
79d72f45
HL
11965 branch_fragP = i386_next_non_empty_frag (pad_fragP);
11966 if (branch_fragP->fr_type != rs_machine_dependent)
e379e5f3 11967 return NULL;
79d72f45
HL
11968 if (TYPE_FROM_RELAX_STATE (branch_fragP->fr_subtype) == COND_JUMP
11969 && i386_macro_fusible_p (maybe_cmp_fragP->tc_frag_data.mf_type,
11970 pad_fragP->tc_frag_data.mf_type))
11971 return branch_fragP;
e379e5f3
L
11972 }
11973
11974 return NULL;
11975}
11976
11977/* Classify BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags. */
11978
11979static void
11980i386_classify_machine_dependent_frag (fragS *fragP)
11981{
11982 fragS *cmp_fragP;
11983 fragS *pad_fragP;
11984 fragS *branch_fragP;
11985 fragS *next_fragP;
11986 unsigned int max_prefix_length;
11987
11988 if (fragP->tc_frag_data.classified)
11989 return;
11990
11991 /* First scan for BRANCH_PADDING and FUSED_JCC_PADDING. Convert
11992 FUSED_JCC_PADDING and merge BRANCH_PADDING. */
11993 for (next_fragP = fragP;
11994 next_fragP != NULL;
11995 next_fragP = next_fragP->fr_next)
11996 {
11997 next_fragP->tc_frag_data.classified = 1;
11998 if (next_fragP->fr_type == rs_machine_dependent)
11999 switch (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype))
12000 {
12001 case BRANCH_PADDING:
12002 /* The BRANCH_PADDING frag must be followed by a branch
12003 frag. */
12004 branch_fragP = i386_next_non_empty_frag (next_fragP);
12005 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
12006 break;
12007 case FUSED_JCC_PADDING:
12008 /* Check if this is a fused jcc:
12009 FUSED_JCC_PADDING
12010 CMP like instruction
12011 BRANCH_PADDING
12012 COND_JUMP
12013 */
12014 cmp_fragP = i386_next_non_empty_frag (next_fragP);
12015 pad_fragP = i386_next_non_empty_frag (cmp_fragP);
79d72f45 12016 branch_fragP = i386_next_fusible_jcc_frag (next_fragP, pad_fragP);
e379e5f3
L
12017 if (branch_fragP)
12018 {
12019 /* The BRANCH_PADDING frag is merged with the
12020 FUSED_JCC_PADDING frag. */
12021 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
12022 /* CMP like instruction size. */
12023 next_fragP->tc_frag_data.cmp_size = cmp_fragP->fr_fix;
12024 frag_wane (pad_fragP);
12025 /* Skip to branch_fragP. */
12026 next_fragP = branch_fragP;
12027 }
12028 else if (next_fragP->tc_frag_data.max_prefix_length)
12029 {
12030 /* Turn FUSED_JCC_PADDING into BRANCH_PREFIX if it isn't
12031 a fused jcc. */
12032 next_fragP->fr_subtype
12033 = ENCODE_RELAX_STATE (BRANCH_PREFIX, 0);
12034 next_fragP->tc_frag_data.max_bytes
12035 = next_fragP->tc_frag_data.max_prefix_length;
12036 /* This will be updated in the BRANCH_PREFIX scan. */
12037 next_fragP->tc_frag_data.max_prefix_length = 0;
12038 }
12039 else
12040 frag_wane (next_fragP);
12041 break;
12042 }
12043 }
12044
12045 /* Stop if there is no BRANCH_PREFIX. */
12046 if (!align_branch_prefix_size)
12047 return;
12048
12049 /* Scan for BRANCH_PREFIX. */
12050 for (; fragP != NULL; fragP = fragP->fr_next)
12051 {
12052 if (fragP->fr_type != rs_machine_dependent
12053 || (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
12054 != BRANCH_PREFIX))
12055 continue;
12056
12057 /* Count all BRANCH_PREFIX frags before BRANCH_PADDING and
12058 COND_JUMP_PREFIX. */
12059 max_prefix_length = 0;
12060 for (next_fragP = fragP;
12061 next_fragP != NULL;
12062 next_fragP = next_fragP->fr_next)
12063 {
12064 if (next_fragP->fr_type == rs_fill)
12065 /* Skip rs_fill frags. */
12066 continue;
12067 else if (next_fragP->fr_type != rs_machine_dependent)
12068 /* Stop for all other frags. */
12069 break;
12070
12071 /* rs_machine_dependent frags. */
12072 if (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12073 == BRANCH_PREFIX)
12074 {
12075 /* Count BRANCH_PREFIX frags. */
12076 if (max_prefix_length >= MAX_FUSED_JCC_PADDING_SIZE)
12077 {
12078 max_prefix_length = MAX_FUSED_JCC_PADDING_SIZE;
12079 frag_wane (next_fragP);
12080 }
12081 else
12082 max_prefix_length
12083 += next_fragP->tc_frag_data.max_bytes;
12084 }
12085 else if ((TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12086 == BRANCH_PADDING)
12087 || (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12088 == FUSED_JCC_PADDING))
12089 {
12090 /* Stop at BRANCH_PADDING and FUSED_JCC_PADDING. */
12091 fragP->tc_frag_data.u.padding_fragP = next_fragP;
12092 break;
12093 }
12094 else
12095 /* Stop for other rs_machine_dependent frags. */
12096 break;
12097 }
12098
12099 fragP->tc_frag_data.max_prefix_length = max_prefix_length;
12100
12101 /* Skip to the next frag. */
12102 fragP = next_fragP;
12103 }
12104}
12105
12106/* Compute padding size for
12107
12108 FUSED_JCC_PADDING
12109 CMP like instruction
12110 BRANCH_PADDING
12111 COND_JUMP/UNCOND_JUMP
12112
12113 or
12114
12115 BRANCH_PADDING
12116 COND_JUMP/UNCOND_JUMP
12117 */
12118
12119static int
12120i386_branch_padding_size (fragS *fragP, offsetT address)
12121{
12122 unsigned int offset, size, padding_size;
12123 fragS *branch_fragP = fragP->tc_frag_data.u.branch_fragP;
12124
12125 /* The start address of the BRANCH_PADDING or FUSED_JCC_PADDING frag. */
12126 if (!address)
12127 address = fragP->fr_address;
12128 address += fragP->fr_fix;
12129
12130 /* CMP like instrunction size. */
12131 size = fragP->tc_frag_data.cmp_size;
12132
12133 /* The base size of the branch frag. */
12134 size += branch_fragP->fr_fix;
12135
12136 /* Add opcode and displacement bytes for the rs_machine_dependent
12137 branch frag. */
12138 if (branch_fragP->fr_type == rs_machine_dependent)
12139 size += md_relax_table[branch_fragP->fr_subtype].rlx_length;
12140
12141 /* Check if branch is within boundary and doesn't end at the last
12142 byte. */
12143 offset = address & ((1U << align_branch_power) - 1);
12144 if ((offset + size) >= (1U << align_branch_power))
12145 /* Padding needed to avoid crossing boundary. */
12146 padding_size = (1U << align_branch_power) - offset;
12147 else
12148 /* No padding needed. */
12149 padding_size = 0;
12150
12151 /* The return value may be saved in tc_frag_data.length which is
12152 unsigned byte. */
12153 if (!fits_in_unsigned_byte (padding_size))
12154 abort ();
12155
12156 return padding_size;
12157}
12158
12159/* i386_generic_table_relax_frag()
12160
12161 Handle BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags to
12162 grow/shrink padding to align branch frags. Hand others to
12163 relax_frag(). */
12164
12165long
12166i386_generic_table_relax_frag (segT segment, fragS *fragP, long stretch)
12167{
12168 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12169 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
12170 {
12171 long padding_size = i386_branch_padding_size (fragP, 0);
12172 long grow = padding_size - fragP->tc_frag_data.length;
12173
12174 /* When the BRANCH_PREFIX frag is used, the computed address
12175 must match the actual address and there should be no padding. */
12176 if (fragP->tc_frag_data.padding_address
12177 && (fragP->tc_frag_data.padding_address != fragP->fr_address
12178 || padding_size))
12179 abort ();
12180
12181 /* Update the padding size. */
12182 if (grow)
12183 fragP->tc_frag_data.length = padding_size;
12184
12185 return grow;
12186 }
12187 else if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12188 {
12189 fragS *padding_fragP, *next_fragP;
12190 long padding_size, left_size, last_size;
12191
12192 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
12193 if (!padding_fragP)
12194 /* Use the padding set by the leading BRANCH_PREFIX frag. */
12195 return (fragP->tc_frag_data.length
12196 - fragP->tc_frag_data.last_length);
12197
12198 /* Compute the relative address of the padding frag in the very
12199 first time where the BRANCH_PREFIX frag sizes are zero. */
12200 if (!fragP->tc_frag_data.padding_address)
12201 fragP->tc_frag_data.padding_address
12202 = padding_fragP->fr_address - (fragP->fr_address - stretch);
12203
12204 /* First update the last length from the previous interation. */
12205 left_size = fragP->tc_frag_data.prefix_length;
12206 for (next_fragP = fragP;
12207 next_fragP != padding_fragP;
12208 next_fragP = next_fragP->fr_next)
12209 if (next_fragP->fr_type == rs_machine_dependent
12210 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12211 == BRANCH_PREFIX))
12212 {
12213 if (left_size)
12214 {
12215 int max = next_fragP->tc_frag_data.max_bytes;
12216 if (max)
12217 {
12218 int size;
12219 if (max > left_size)
12220 size = left_size;
12221 else
12222 size = max;
12223 left_size -= size;
12224 next_fragP->tc_frag_data.last_length = size;
12225 }
12226 }
12227 else
12228 next_fragP->tc_frag_data.last_length = 0;
12229 }
12230
12231 /* Check the padding size for the padding frag. */
12232 padding_size = i386_branch_padding_size
12233 (padding_fragP, (fragP->fr_address
12234 + fragP->tc_frag_data.padding_address));
12235
12236 last_size = fragP->tc_frag_data.prefix_length;
12237 /* Check if there is change from the last interation. */
12238 if (padding_size == last_size)
12239 {
12240 /* Update the expected address of the padding frag. */
12241 padding_fragP->tc_frag_data.padding_address
12242 = (fragP->fr_address + padding_size
12243 + fragP->tc_frag_data.padding_address);
12244 return 0;
12245 }
12246
12247 if (padding_size > fragP->tc_frag_data.max_prefix_length)
12248 {
12249 /* No padding if there is no sufficient room. Clear the
12250 expected address of the padding frag. */
12251 padding_fragP->tc_frag_data.padding_address = 0;
12252 padding_size = 0;
12253 }
12254 else
12255 /* Store the expected address of the padding frag. */
12256 padding_fragP->tc_frag_data.padding_address
12257 = (fragP->fr_address + padding_size
12258 + fragP->tc_frag_data.padding_address);
12259
12260 fragP->tc_frag_data.prefix_length = padding_size;
12261
12262 /* Update the length for the current interation. */
12263 left_size = padding_size;
12264 for (next_fragP = fragP;
12265 next_fragP != padding_fragP;
12266 next_fragP = next_fragP->fr_next)
12267 if (next_fragP->fr_type == rs_machine_dependent
12268 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12269 == BRANCH_PREFIX))
12270 {
12271 if (left_size)
12272 {
12273 int max = next_fragP->tc_frag_data.max_bytes;
12274 if (max)
12275 {
12276 int size;
12277 if (max > left_size)
12278 size = left_size;
12279 else
12280 size = max;
12281 left_size -= size;
12282 next_fragP->tc_frag_data.length = size;
12283 }
12284 }
12285 else
12286 next_fragP->tc_frag_data.length = 0;
12287 }
12288
12289 return (fragP->tc_frag_data.length
12290 - fragP->tc_frag_data.last_length);
12291 }
12292 return relax_frag (segment, fragP, stretch);
12293}
12294
ee7fcc42
AM
12295/* md_estimate_size_before_relax()
12296
12297 Called just before relax() for rs_machine_dependent frags. The x86
12298 assembler uses these frags to handle variable size jump
12299 instructions.
12300
12301 Any symbol that is now undefined will not become defined.
12302 Return the correct fr_subtype in the frag.
12303 Return the initial "guess for variable size of frag" to caller.
12304 The guess is actually the growth beyond the fixed part. Whatever
12305 we do to grow the fixed or variable part contributes to our
12306 returned value. */
12307
252b5132 12308int
7016a5d5 12309md_estimate_size_before_relax (fragS *fragP, segT segment)
252b5132 12310{
e379e5f3
L
12311 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12312 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX
12313 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
12314 {
12315 i386_classify_machine_dependent_frag (fragP);
12316 return fragP->tc_frag_data.length;
12317 }
12318
252b5132 12319 /* We've already got fragP->fr_subtype right; all we have to do is
b98ef147
AM
12320 check for un-relaxable symbols. On an ELF system, we can't relax
12321 an externally visible symbol, because it may be overridden by a
12322 shared library. */
12323 if (S_GET_SEGMENT (fragP->fr_symbol) != segment
6d249963 12324#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12325 || (IS_ELF
8dcea932
L
12326 && !elf_symbol_resolved_in_segment_p (fragP->fr_symbol,
12327 fragP->fr_var))
fbeb56a4
DK
12328#endif
12329#if defined (OBJ_COFF) && defined (TE_PE)
7ab9ffdd 12330 || (OUTPUT_FLAVOR == bfd_target_coff_flavour
fbeb56a4 12331 && S_IS_WEAK (fragP->fr_symbol))
b98ef147
AM
12332#endif
12333 )
252b5132 12334 {
b98ef147
AM
12335 /* Symbol is undefined in this segment, or we need to keep a
12336 reloc so that weak symbols can be overridden. */
12337 int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
f86103b7 12338 enum bfd_reloc_code_real reloc_type;
ee7fcc42
AM
12339 unsigned char *opcode;
12340 int old_fr_fix;
eb19308f 12341 fixS *fixP = NULL;
f6af82bd 12342
ee7fcc42 12343 if (fragP->fr_var != NO_RELOC)
1e9cc1c2 12344 reloc_type = (enum bfd_reloc_code_real) fragP->fr_var;
b98ef147 12345 else if (size == 2)
f6af82bd 12346 reloc_type = BFD_RELOC_16_PCREL;
bd7ab16b 12347#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1ef3994a
JB
12348 else if (fragP->tc_frag_data.code64 && fragP->fr_offset == 0
12349 && need_plt32_p (fragP->fr_symbol))
bd7ab16b
L
12350 reloc_type = BFD_RELOC_X86_64_PLT32;
12351#endif
f6af82bd
AM
12352 else
12353 reloc_type = BFD_RELOC_32_PCREL;
252b5132 12354
ee7fcc42
AM
12355 old_fr_fix = fragP->fr_fix;
12356 opcode = (unsigned char *) fragP->fr_opcode;
12357
fddf5b5b 12358 switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
252b5132 12359 {
fddf5b5b
AM
12360 case UNCOND_JUMP:
12361 /* Make jmp (0xeb) a (d)word displacement jump. */
47926f60 12362 opcode[0] = 0xe9;
252b5132 12363 fragP->fr_fix += size;
eb19308f
JB
12364 fixP = fix_new (fragP, old_fr_fix, size,
12365 fragP->fr_symbol,
12366 fragP->fr_offset, 1,
12367 reloc_type);
252b5132
RH
12368 break;
12369
fddf5b5b 12370 case COND_JUMP86:
412167cb
AM
12371 if (size == 2
12372 && (!no_cond_jump_promotion || fragP->fr_var != NO_RELOC))
fddf5b5b
AM
12373 {
12374 /* Negate the condition, and branch past an
12375 unconditional jump. */
12376 opcode[0] ^= 1;
12377 opcode[1] = 3;
12378 /* Insert an unconditional jump. */
12379 opcode[2] = 0xe9;
12380 /* We added two extra opcode bytes, and have a two byte
12381 offset. */
12382 fragP->fr_fix += 2 + 2;
062cd5e7
AS
12383 fix_new (fragP, old_fr_fix + 2, 2,
12384 fragP->fr_symbol,
12385 fragP->fr_offset, 1,
12386 reloc_type);
fddf5b5b
AM
12387 break;
12388 }
12389 /* Fall through. */
12390
12391 case COND_JUMP:
412167cb
AM
12392 if (no_cond_jump_promotion && fragP->fr_var == NO_RELOC)
12393 {
12394 fragP->fr_fix += 1;
3e02c1cc
AM
12395 fixP = fix_new (fragP, old_fr_fix, 1,
12396 fragP->fr_symbol,
12397 fragP->fr_offset, 1,
12398 BFD_RELOC_8_PCREL);
12399 fixP->fx_signed = 1;
412167cb
AM
12400 break;
12401 }
93c2a809 12402
24eab124 12403 /* This changes the byte-displacement jump 0x7N
fddf5b5b 12404 to the (d)word-displacement jump 0x0f,0x8N. */
252b5132 12405 opcode[1] = opcode[0] + 0x10;
f6af82bd 12406 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
47926f60
KH
12407 /* We've added an opcode byte. */
12408 fragP->fr_fix += 1 + size;
eb19308f
JB
12409 fixP = fix_new (fragP, old_fr_fix + 1, size,
12410 fragP->fr_symbol,
12411 fragP->fr_offset, 1,
12412 reloc_type);
252b5132 12413 break;
fddf5b5b
AM
12414
12415 default:
12416 BAD_CASE (fragP->fr_subtype);
12417 break;
252b5132 12418 }
eb19308f
JB
12419
12420 /* All jumps handled here are signed, but don't unconditionally use a
12421 signed limit check for 32 and 16 bit jumps as we want to allow wrap
12422 around at 4G (outside of 64-bit mode) and 64k. */
12423 if (size == 4 && flag_code == CODE_64BIT)
12424 fixP->fx_signed = 1;
12425
252b5132 12426 frag_wane (fragP);
ee7fcc42 12427 return fragP->fr_fix - old_fr_fix;
252b5132 12428 }
93c2a809 12429
93c2a809
AM
12430 /* Guess size depending on current relax state. Initially the relax
12431 state will correspond to a short jump and we return 1, because
12432 the variable part of the frag (the branch offset) is one byte
12433 long. However, we can relax a section more than once and in that
12434 case we must either set fr_subtype back to the unrelaxed state,
12435 or return the value for the appropriate branch. */
12436 return md_relax_table[fragP->fr_subtype].rlx_length;
ee7fcc42
AM
12437}
12438
47926f60
KH
12439/* Called after relax() is finished.
12440
12441 In: Address of frag.
12442 fr_type == rs_machine_dependent.
12443 fr_subtype is what the address relaxed to.
12444
12445 Out: Any fixSs and constants are set up.
12446 Caller will turn frag into a ".space 0". */
12447
252b5132 12448void
7016a5d5
TG
12449md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec ATTRIBUTE_UNUSED,
12450 fragS *fragP)
252b5132 12451{
29b0f896 12452 unsigned char *opcode;
252b5132 12453 unsigned char *where_to_put_displacement = NULL;
847f7ad4
AM
12454 offsetT target_address;
12455 offsetT opcode_address;
252b5132 12456 unsigned int extension = 0;
847f7ad4 12457 offsetT displacement_from_opcode_start;
252b5132 12458
e379e5f3
L
12459 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12460 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING
12461 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12462 {
12463 /* Generate nop padding. */
12464 unsigned int size = fragP->tc_frag_data.length;
12465 if (size)
12466 {
12467 if (size > fragP->tc_frag_data.max_bytes)
12468 abort ();
12469
12470 if (flag_debug)
12471 {
12472 const char *msg;
12473 const char *branch = "branch";
12474 const char *prefix = "";
12475 fragS *padding_fragP;
12476 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
12477 == BRANCH_PREFIX)
12478 {
12479 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
12480 switch (fragP->tc_frag_data.default_prefix)
12481 {
12482 default:
12483 abort ();
12484 break;
12485 case CS_PREFIX_OPCODE:
12486 prefix = " cs";
12487 break;
12488 case DS_PREFIX_OPCODE:
12489 prefix = " ds";
12490 break;
12491 case ES_PREFIX_OPCODE:
12492 prefix = " es";
12493 break;
12494 case FS_PREFIX_OPCODE:
12495 prefix = " fs";
12496 break;
12497 case GS_PREFIX_OPCODE:
12498 prefix = " gs";
12499 break;
12500 case SS_PREFIX_OPCODE:
12501 prefix = " ss";
12502 break;
12503 }
12504 if (padding_fragP)
12505 msg = _("%s:%u: add %d%s at 0x%llx to align "
12506 "%s within %d-byte boundary\n");
12507 else
12508 msg = _("%s:%u: add additional %d%s at 0x%llx to "
12509 "align %s within %d-byte boundary\n");
12510 }
12511 else
12512 {
12513 padding_fragP = fragP;
12514 msg = _("%s:%u: add %d%s-byte nop at 0x%llx to align "
12515 "%s within %d-byte boundary\n");
12516 }
12517
12518 if (padding_fragP)
12519 switch (padding_fragP->tc_frag_data.branch_type)
12520 {
12521 case align_branch_jcc:
12522 branch = "jcc";
12523 break;
12524 case align_branch_fused:
12525 branch = "fused jcc";
12526 break;
12527 case align_branch_jmp:
12528 branch = "jmp";
12529 break;
12530 case align_branch_call:
12531 branch = "call";
12532 break;
12533 case align_branch_indirect:
12534 branch = "indiret branch";
12535 break;
12536 case align_branch_ret:
12537 branch = "ret";
12538 break;
12539 default:
12540 break;
12541 }
12542
12543 fprintf (stdout, msg,
12544 fragP->fr_file, fragP->fr_line, size, prefix,
12545 (long long) fragP->fr_address, branch,
12546 1 << align_branch_power);
12547 }
12548 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12549 memset (fragP->fr_opcode,
12550 fragP->tc_frag_data.default_prefix, size);
12551 else
12552 i386_generate_nops (fragP, (char *) fragP->fr_opcode,
12553 size, 0);
12554 fragP->fr_fix += size;
12555 }
12556 return;
12557 }
12558
252b5132
RH
12559 opcode = (unsigned char *) fragP->fr_opcode;
12560
47926f60 12561 /* Address we want to reach in file space. */
252b5132 12562 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
252b5132 12563
47926f60 12564 /* Address opcode resides at in file space. */
252b5132
RH
12565 opcode_address = fragP->fr_address + fragP->fr_fix;
12566
47926f60 12567 /* Displacement from opcode start to fill into instruction. */
252b5132
RH
12568 displacement_from_opcode_start = target_address - opcode_address;
12569
fddf5b5b 12570 if ((fragP->fr_subtype & BIG) == 0)
252b5132 12571 {
47926f60
KH
12572 /* Don't have to change opcode. */
12573 extension = 1; /* 1 opcode + 1 displacement */
252b5132 12574 where_to_put_displacement = &opcode[1];
fddf5b5b
AM
12575 }
12576 else
12577 {
12578 if (no_cond_jump_promotion
12579 && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
4eed87de
AM
12580 as_warn_where (fragP->fr_file, fragP->fr_line,
12581 _("long jump required"));
252b5132 12582
fddf5b5b
AM
12583 switch (fragP->fr_subtype)
12584 {
12585 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
12586 extension = 4; /* 1 opcode + 4 displacement */
12587 opcode[0] = 0xe9;
12588 where_to_put_displacement = &opcode[1];
12589 break;
252b5132 12590
fddf5b5b
AM
12591 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
12592 extension = 2; /* 1 opcode + 2 displacement */
12593 opcode[0] = 0xe9;
12594 where_to_put_displacement = &opcode[1];
12595 break;
252b5132 12596
fddf5b5b
AM
12597 case ENCODE_RELAX_STATE (COND_JUMP, BIG):
12598 case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
12599 extension = 5; /* 2 opcode + 4 displacement */
12600 opcode[1] = opcode[0] + 0x10;
12601 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12602 where_to_put_displacement = &opcode[2];
12603 break;
252b5132 12604
fddf5b5b
AM
12605 case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
12606 extension = 3; /* 2 opcode + 2 displacement */
12607 opcode[1] = opcode[0] + 0x10;
12608 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12609 where_to_put_displacement = &opcode[2];
12610 break;
252b5132 12611
fddf5b5b
AM
12612 case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
12613 extension = 4;
12614 opcode[0] ^= 1;
12615 opcode[1] = 3;
12616 opcode[2] = 0xe9;
12617 where_to_put_displacement = &opcode[3];
12618 break;
12619
12620 default:
12621 BAD_CASE (fragP->fr_subtype);
12622 break;
12623 }
252b5132 12624 }
fddf5b5b 12625
7b81dfbb
AJ
12626 /* If size if less then four we are sure that the operand fits,
12627 but if it's 4, then it could be that the displacement is larger
12628 then -/+ 2GB. */
12629 if (DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype) == 4
12630 && object_64bit
12631 && ((addressT) (displacement_from_opcode_start - extension
4eed87de
AM
12632 + ((addressT) 1 << 31))
12633 > (((addressT) 2 << 31) - 1)))
7b81dfbb
AJ
12634 {
12635 as_bad_where (fragP->fr_file, fragP->fr_line,
12636 _("jump target out of range"));
12637 /* Make us emit 0. */
12638 displacement_from_opcode_start = extension;
12639 }
47926f60 12640 /* Now put displacement after opcode. */
252b5132
RH
12641 md_number_to_chars ((char *) where_to_put_displacement,
12642 (valueT) (displacement_from_opcode_start - extension),
fddf5b5b 12643 DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
252b5132
RH
12644 fragP->fr_fix += extension;
12645}
12646\f
7016a5d5 12647/* Apply a fixup (fixP) to segment data, once it has been determined
252b5132
RH
12648 by our caller that we have all the info we need to fix it up.
12649
7016a5d5
TG
12650 Parameter valP is the pointer to the value of the bits.
12651
252b5132
RH
12652 On the 386, immediates, displacements, and data pointers are all in
12653 the same (little-endian) format, so we don't need to care about which
12654 we are handling. */
12655
94f592af 12656void
7016a5d5 12657md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 12658{
94f592af 12659 char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
c6682705 12660 valueT value = *valP;
252b5132 12661
f86103b7 12662#if !defined (TE_Mach)
93382f6d
AM
12663 if (fixP->fx_pcrel)
12664 {
12665 switch (fixP->fx_r_type)
12666 {
5865bb77
ILT
12667 default:
12668 break;
12669
d6ab8113
JB
12670 case BFD_RELOC_64:
12671 fixP->fx_r_type = BFD_RELOC_64_PCREL;
12672 break;
93382f6d 12673 case BFD_RELOC_32:
ae8887b5 12674 case BFD_RELOC_X86_64_32S:
93382f6d
AM
12675 fixP->fx_r_type = BFD_RELOC_32_PCREL;
12676 break;
12677 case BFD_RELOC_16:
12678 fixP->fx_r_type = BFD_RELOC_16_PCREL;
12679 break;
12680 case BFD_RELOC_8:
12681 fixP->fx_r_type = BFD_RELOC_8_PCREL;
12682 break;
12683 }
12684 }
252b5132 12685
a161fe53 12686 if (fixP->fx_addsy != NULL
31312f95 12687 && (fixP->fx_r_type == BFD_RELOC_32_PCREL
d6ab8113 12688 || fixP->fx_r_type == BFD_RELOC_64_PCREL
31312f95 12689 || fixP->fx_r_type == BFD_RELOC_16_PCREL
d258b828 12690 || fixP->fx_r_type == BFD_RELOC_8_PCREL)
31312f95 12691 && !use_rela_relocations)
252b5132 12692 {
31312f95
AM
12693 /* This is a hack. There should be a better way to handle this.
12694 This covers for the fact that bfd_install_relocation will
12695 subtract the current location (for partial_inplace, PC relative
12696 relocations); see more below. */
252b5132 12697#ifndef OBJ_AOUT
718ddfc0 12698 if (IS_ELF
252b5132
RH
12699#ifdef TE_PE
12700 || OUTPUT_FLAVOR == bfd_target_coff_flavour
12701#endif
12702 )
12703 value += fixP->fx_where + fixP->fx_frag->fr_address;
12704#endif
12705#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12706 if (IS_ELF)
252b5132 12707 {
6539b54b 12708 segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
2f66722d 12709
6539b54b 12710 if ((sym_seg == seg
2f66722d 12711 || (symbol_section_p (fixP->fx_addsy)
6539b54b 12712 && sym_seg != absolute_section))
af65af87 12713 && !generic_force_reloc (fixP))
2f66722d
AM
12714 {
12715 /* Yes, we add the values in twice. This is because
6539b54b
AM
12716 bfd_install_relocation subtracts them out again. I think
12717 bfd_install_relocation is broken, but I don't dare change
2f66722d
AM
12718 it. FIXME. */
12719 value += fixP->fx_where + fixP->fx_frag->fr_address;
12720 }
252b5132
RH
12721 }
12722#endif
12723#if defined (OBJ_COFF) && defined (TE_PE)
977cdf5a
NC
12724 /* For some reason, the PE format does not store a
12725 section address offset for a PC relative symbol. */
12726 if (S_GET_SEGMENT (fixP->fx_addsy) != seg
7be1c489 12727 || S_IS_WEAK (fixP->fx_addsy))
252b5132
RH
12728 value += md_pcrel_from (fixP);
12729#endif
12730 }
fbeb56a4 12731#if defined (OBJ_COFF) && defined (TE_PE)
f01c1a09
NC
12732 if (fixP->fx_addsy != NULL
12733 && S_IS_WEAK (fixP->fx_addsy)
12734 /* PR 16858: Do not modify weak function references. */
12735 && ! fixP->fx_pcrel)
fbeb56a4 12736 {
296a8689
NC
12737#if !defined (TE_PEP)
12738 /* For x86 PE weak function symbols are neither PC-relative
12739 nor do they set S_IS_FUNCTION. So the only reliable way
12740 to detect them is to check the flags of their containing
12741 section. */
12742 if (S_GET_SEGMENT (fixP->fx_addsy) != NULL
12743 && S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_CODE)
12744 ;
12745 else
12746#endif
fbeb56a4
DK
12747 value -= S_GET_VALUE (fixP->fx_addsy);
12748 }
12749#endif
252b5132
RH
12750
12751 /* Fix a few things - the dynamic linker expects certain values here,
0234cb7c 12752 and we must not disappoint it. */
252b5132 12753#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12754 if (IS_ELF && fixP->fx_addsy)
47926f60
KH
12755 switch (fixP->fx_r_type)
12756 {
12757 case BFD_RELOC_386_PLT32:
3e73aa7c 12758 case BFD_RELOC_X86_64_PLT32:
b9519cfe
L
12759 /* Make the jump instruction point to the address of the operand.
12760 At runtime we merely add the offset to the actual PLT entry.
12761 NB: Subtract the offset size only for jump instructions. */
12762 if (fixP->fx_pcrel)
12763 value = -4;
47926f60 12764 break;
31312f95 12765
13ae64f3
JJ
12766 case BFD_RELOC_386_TLS_GD:
12767 case BFD_RELOC_386_TLS_LDM:
13ae64f3 12768 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
12769 case BFD_RELOC_386_TLS_IE:
12770 case BFD_RELOC_386_TLS_GOTIE:
67a4f2b7 12771 case BFD_RELOC_386_TLS_GOTDESC:
bffbf940
JJ
12772 case BFD_RELOC_X86_64_TLSGD:
12773 case BFD_RELOC_X86_64_TLSLD:
12774 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7 12775 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
00f7efb6
JJ
12776 value = 0; /* Fully resolved at runtime. No addend. */
12777 /* Fallthrough */
12778 case BFD_RELOC_386_TLS_LE:
12779 case BFD_RELOC_386_TLS_LDO_32:
12780 case BFD_RELOC_386_TLS_LE_32:
12781 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 12782 case BFD_RELOC_X86_64_DTPOFF64:
00f7efb6 12783 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113 12784 case BFD_RELOC_X86_64_TPOFF64:
00f7efb6
JJ
12785 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12786 break;
12787
67a4f2b7
AO
12788 case BFD_RELOC_386_TLS_DESC_CALL:
12789 case BFD_RELOC_X86_64_TLSDESC_CALL:
12790 value = 0; /* Fully resolved at runtime. No addend. */
12791 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12792 fixP->fx_done = 0;
12793 return;
12794
47926f60
KH
12795 case BFD_RELOC_VTABLE_INHERIT:
12796 case BFD_RELOC_VTABLE_ENTRY:
12797 fixP->fx_done = 0;
94f592af 12798 return;
47926f60
KH
12799
12800 default:
12801 break;
12802 }
12803#endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) */
a442cac5
JB
12804
12805 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
12806 if (!object_64bit)
12807 value = extend_to_32bit_address (value);
12808
c6682705 12809 *valP = value;
f86103b7 12810#endif /* !defined (TE_Mach) */
3e73aa7c 12811
3e73aa7c 12812 /* Are we finished with this relocation now? */
c6682705 12813 if (fixP->fx_addsy == NULL)
b8188555
JB
12814 {
12815 fixP->fx_done = 1;
12816 switch (fixP->fx_r_type)
12817 {
12818 case BFD_RELOC_X86_64_32S:
12819 fixP->fx_signed = 1;
12820 break;
12821
12822 default:
12823 break;
12824 }
12825 }
fbeb56a4
DK
12826#if defined (OBJ_COFF) && defined (TE_PE)
12827 else if (fixP->fx_addsy != NULL && S_IS_WEAK (fixP->fx_addsy))
12828 {
12829 fixP->fx_done = 0;
12830 /* Remember value for tc_gen_reloc. */
12831 fixP->fx_addnumber = value;
12832 /* Clear out the frag for now. */
12833 value = 0;
12834 }
12835#endif
3e73aa7c
JH
12836 else if (use_rela_relocations)
12837 {
46fb6d5a
JB
12838 if (!disallow_64bit_reloc || fixP->fx_r_type == NO_RELOC)
12839 fixP->fx_no_overflow = 1;
062cd5e7
AS
12840 /* Remember value for tc_gen_reloc. */
12841 fixP->fx_addnumber = value;
3e73aa7c
JH
12842 value = 0;
12843 }
f86103b7 12844
94f592af 12845 md_number_to_chars (p, value, fixP->fx_size);
252b5132 12846}
252b5132 12847\f
6d4af3c2 12848const char *
499ac353 12849md_atof (int type, char *litP, int *sizeP)
252b5132 12850{
499ac353
NC
12851 /* This outputs the LITTLENUMs in REVERSE order;
12852 in accord with the bigendian 386. */
5b7c81bd 12853 return ieee_md_atof (type, litP, sizeP, false);
252b5132
RH
12854}
12855\f
2d545b82 12856static char output_invalid_buf[sizeof (unsigned char) * 2 + 6];
252b5132 12857
252b5132 12858static char *
e3bb37b5 12859output_invalid (int c)
252b5132 12860{
3882b010 12861 if (ISPRINT (c))
f9f21a03
L
12862 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
12863 "'%c'", c);
252b5132 12864 else
f9f21a03 12865 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
2d545b82 12866 "(0x%x)", (unsigned char) c);
252b5132
RH
12867 return output_invalid_buf;
12868}
12869
8a6fb3f9
JB
12870/* Verify that @r can be used in the current context. */
12871
5b7c81bd 12872static bool check_register (const reg_entry *r)
8a6fb3f9
JB
12873{
12874 if (allow_pseudo_reg)
5b7c81bd 12875 return true;
8a6fb3f9
JB
12876
12877 if (operand_type_all_zero (&r->reg_type))
5b7c81bd 12878 return false;
8a6fb3f9
JB
12879
12880 if ((r->reg_type.bitfield.dword
12881 || (r->reg_type.bitfield.class == SReg && r->reg_num > 3)
12882 || r->reg_type.bitfield.class == RegCR
22e00a3f 12883 || r->reg_type.bitfield.class == RegDR)
8a6fb3f9 12884 && !cpu_arch_flags.bitfield.cpui386)
5b7c81bd 12885 return false;
8a6fb3f9 12886
22e00a3f
JB
12887 if (r->reg_type.bitfield.class == RegTR
12888 && (flag_code == CODE_64BIT
12889 || !cpu_arch_flags.bitfield.cpui386
12890 || cpu_arch_isa_flags.bitfield.cpui586
12891 || cpu_arch_isa_flags.bitfield.cpui686))
5b7c81bd 12892 return false;
22e00a3f 12893
8a6fb3f9 12894 if (r->reg_type.bitfield.class == RegMMX && !cpu_arch_flags.bitfield.cpummx)
5b7c81bd 12895 return false;
8a6fb3f9
JB
12896
12897 if (!cpu_arch_flags.bitfield.cpuavx512f)
12898 {
12899 if (r->reg_type.bitfield.zmmword
12900 || r->reg_type.bitfield.class == RegMask)
5b7c81bd 12901 return false;
8a6fb3f9
JB
12902
12903 if (!cpu_arch_flags.bitfield.cpuavx)
12904 {
12905 if (r->reg_type.bitfield.ymmword)
5b7c81bd 12906 return false;
8a6fb3f9
JB
12907
12908 if (!cpu_arch_flags.bitfield.cpusse && r->reg_type.bitfield.xmmword)
5b7c81bd 12909 return false;
8a6fb3f9
JB
12910 }
12911 }
12912
260cd341
LC
12913 if (r->reg_type.bitfield.tmmword
12914 && (!cpu_arch_flags.bitfield.cpuamx_tile
12915 || flag_code != CODE_64BIT))
5b7c81bd 12916 return false;
260cd341 12917
8a6fb3f9 12918 if (r->reg_type.bitfield.class == RegBND && !cpu_arch_flags.bitfield.cpumpx)
5b7c81bd 12919 return false;
8a6fb3f9
JB
12920
12921 /* Don't allow fake index register unless allow_index_reg isn't 0. */
12922 if (!allow_index_reg && r->reg_num == RegIZ)
5b7c81bd 12923 return false;
8a6fb3f9
JB
12924
12925 /* Upper 16 vector registers are only available with VREX in 64bit
12926 mode, and require EVEX encoding. */
12927 if (r->reg_flags & RegVRex)
12928 {
12929 if (!cpu_arch_flags.bitfield.cpuavx512f
12930 || flag_code != CODE_64BIT)
5b7c81bd 12931 return false;
8a6fb3f9 12932
da4977e0
JB
12933 if (i.vec_encoding == vex_encoding_default)
12934 i.vec_encoding = vex_encoding_evex;
12935 else if (i.vec_encoding != vex_encoding_evex)
12936 i.vec_encoding = vex_encoding_error;
8a6fb3f9
JB
12937 }
12938
12939 if (((r->reg_flags & (RegRex64 | RegRex)) || r->reg_type.bitfield.qword)
12940 && (!cpu_arch_flags.bitfield.cpulm || r->reg_type.bitfield.class != RegCR)
12941 && flag_code != CODE_64BIT)
5b7c81bd 12942 return false;
8a6fb3f9
JB
12943
12944 if (r->reg_type.bitfield.class == SReg && r->reg_num == RegFlat
12945 && !intel_syntax)
5b7c81bd 12946 return false;
8a6fb3f9 12947
5b7c81bd 12948 return true;
8a6fb3f9
JB
12949}
12950
af6bdddf 12951/* REG_STRING starts *before* REGISTER_PREFIX. */
252b5132
RH
12952
12953static const reg_entry *
4d1bb795 12954parse_real_register (char *reg_string, char **end_op)
252b5132 12955{
af6bdddf
AM
12956 char *s = reg_string;
12957 char *p;
252b5132
RH
12958 char reg_name_given[MAX_REG_NAME_SIZE + 1];
12959 const reg_entry *r;
12960
12961 /* Skip possible REGISTER_PREFIX and possible whitespace. */
12962 if (*s == REGISTER_PREFIX)
12963 ++s;
12964
12965 if (is_space_char (*s))
12966 ++s;
12967
12968 p = reg_name_given;
af6bdddf 12969 while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
252b5132
RH
12970 {
12971 if (p >= reg_name_given + MAX_REG_NAME_SIZE)
af6bdddf
AM
12972 return (const reg_entry *) NULL;
12973 s++;
252b5132
RH
12974 }
12975
6588847e
DN
12976 /* For naked regs, make sure that we are not dealing with an identifier.
12977 This prevents confusing an identifier like `eax_var' with register
12978 `eax'. */
12979 if (allow_naked_reg && identifier_chars[(unsigned char) *s])
12980 return (const reg_entry *) NULL;
12981
af6bdddf 12982 *end_op = s;
252b5132 12983
629310ab 12984 r = (const reg_entry *) str_hash_find (reg_hash, reg_name_given);
252b5132 12985
5f47d35b 12986 /* Handle floating point regs, allowing spaces in the (i) part. */
6288d05f 12987 if (r == reg_st0)
5f47d35b 12988 {
0e0eea78
JB
12989 if (!cpu_arch_flags.bitfield.cpu8087
12990 && !cpu_arch_flags.bitfield.cpu287
af32b722
JB
12991 && !cpu_arch_flags.bitfield.cpu387
12992 && !allow_pseudo_reg)
0e0eea78
JB
12993 return (const reg_entry *) NULL;
12994
5f47d35b
AM
12995 if (is_space_char (*s))
12996 ++s;
12997 if (*s == '(')
12998 {
af6bdddf 12999 ++s;
5f47d35b
AM
13000 if (is_space_char (*s))
13001 ++s;
13002 if (*s >= '0' && *s <= '7')
13003 {
db557034 13004 int fpr = *s - '0';
af6bdddf 13005 ++s;
5f47d35b
AM
13006 if (is_space_char (*s))
13007 ++s;
13008 if (*s == ')')
13009 {
13010 *end_op = s + 1;
6288d05f 13011 know (r[fpr].reg_num == fpr);
db557034 13012 return r + fpr;
5f47d35b 13013 }
5f47d35b 13014 }
47926f60 13015 /* We have "%st(" then garbage. */
5f47d35b
AM
13016 return (const reg_entry *) NULL;
13017 }
13018 }
13019
8a6fb3f9 13020 return r && check_register (r) ? r : NULL;
252b5132 13021}
4d1bb795
JB
13022
13023/* REG_STRING starts *before* REGISTER_PREFIX. */
13024
13025static const reg_entry *
13026parse_register (char *reg_string, char **end_op)
13027{
13028 const reg_entry *r;
13029
13030 if (*reg_string == REGISTER_PREFIX || allow_naked_reg)
13031 r = parse_real_register (reg_string, end_op);
13032 else
13033 r = NULL;
13034 if (!r)
13035 {
13036 char *save = input_line_pointer;
13037 char c;
13038 symbolS *symbolP;
13039
13040 input_line_pointer = reg_string;
d02603dc 13041 c = get_symbol_name (&reg_string);
4d1bb795 13042 symbolP = symbol_find (reg_string);
64d23078
JB
13043 while (symbolP && S_GET_SEGMENT (symbolP) != reg_section)
13044 {
13045 const expressionS *e = symbol_get_value_expression(symbolP);
13046
13047 if (e->X_op != O_symbol || e->X_add_number)
13048 break;
13049 symbolP = e->X_add_symbol;
13050 }
4d1bb795
JB
13051 if (symbolP && S_GET_SEGMENT (symbolP) == reg_section)
13052 {
13053 const expressionS *e = symbol_get_value_expression (symbolP);
13054
3b55a1d0
JB
13055 know (e->X_op == O_register);
13056 know (e->X_add_number >= 0
13057 && (valueT) e->X_add_number < i386_regtab_size);
13058 r = i386_regtab + e->X_add_number;
13059 if (!check_register (r))
8a6fb3f9 13060 {
3b55a1d0
JB
13061 as_bad (_("register '%s%s' cannot be used here"),
13062 register_prefix, r->reg_name);
13063 r = &bad_reg;
8a6fb3f9 13064 }
3b55a1d0 13065 *end_op = input_line_pointer;
4d1bb795
JB
13066 }
13067 *input_line_pointer = c;
13068 input_line_pointer = save;
13069 }
13070 return r;
13071}
13072
13073int
13074i386_parse_name (char *name, expressionS *e, char *nextcharP)
13075{
4faaa10f 13076 const reg_entry *r = NULL;
4d1bb795
JB
13077 char *end = input_line_pointer;
13078
13079 *end = *nextcharP;
4faaa10f
JB
13080 if (*name == REGISTER_PREFIX || allow_naked_reg)
13081 r = parse_real_register (name, &input_line_pointer);
4d1bb795
JB
13082 if (r && end <= input_line_pointer)
13083 {
13084 *nextcharP = *input_line_pointer;
13085 *input_line_pointer = 0;
8a6fb3f9
JB
13086 if (r != &bad_reg)
13087 {
13088 e->X_op = O_register;
13089 e->X_add_number = r - i386_regtab;
13090 }
13091 else
13092 e->X_op = O_illegal;
4d1bb795
JB
13093 return 1;
13094 }
13095 input_line_pointer = end;
13096 *end = 0;
ee86248c 13097 return intel_syntax ? i386_intel_parse_name (name, e) : 0;
4d1bb795
JB
13098}
13099
13100void
13101md_operand (expressionS *e)
13102{
ee86248c
JB
13103 char *end;
13104 const reg_entry *r;
4d1bb795 13105
ee86248c
JB
13106 switch (*input_line_pointer)
13107 {
13108 case REGISTER_PREFIX:
13109 r = parse_real_register (input_line_pointer, &end);
4d1bb795
JB
13110 if (r)
13111 {
13112 e->X_op = O_register;
13113 e->X_add_number = r - i386_regtab;
13114 input_line_pointer = end;
13115 }
ee86248c
JB
13116 break;
13117
13118 case '[':
9c2799c2 13119 gas_assert (intel_syntax);
ee86248c
JB
13120 end = input_line_pointer++;
13121 expression (e);
13122 if (*input_line_pointer == ']')
13123 {
13124 ++input_line_pointer;
13125 e->X_op_symbol = make_expr_symbol (e);
13126 e->X_add_symbol = NULL;
13127 e->X_add_number = 0;
13128 e->X_op = O_index;
13129 }
13130 else
13131 {
13132 e->X_op = O_absent;
13133 input_line_pointer = end;
13134 }
13135 break;
4d1bb795
JB
13136 }
13137}
13138
252b5132 13139\f
4cc782b5 13140#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
b6f8c7c4 13141const char *md_shortopts = "kVQ:sqnO::";
252b5132 13142#else
b6f8c7c4 13143const char *md_shortopts = "qnO::";
252b5132 13144#endif
6e0b89ee 13145
3e73aa7c 13146#define OPTION_32 (OPTION_MD_BASE + 0)
b3b91714
AM
13147#define OPTION_64 (OPTION_MD_BASE + 1)
13148#define OPTION_DIVIDE (OPTION_MD_BASE + 2)
9103f4f4
L
13149#define OPTION_MARCH (OPTION_MD_BASE + 3)
13150#define OPTION_MTUNE (OPTION_MD_BASE + 4)
1efbbeb4
L
13151#define OPTION_MMNEMONIC (OPTION_MD_BASE + 5)
13152#define OPTION_MSYNTAX (OPTION_MD_BASE + 6)
13153#define OPTION_MINDEX_REG (OPTION_MD_BASE + 7)
13154#define OPTION_MNAKED_REG (OPTION_MD_BASE + 8)
bd5dea88 13155#define OPTION_MRELAX_RELOCATIONS (OPTION_MD_BASE + 9)
c0f3af97 13156#define OPTION_MSSE2AVX (OPTION_MD_BASE + 10)
daf50ae7 13157#define OPTION_MSSE_CHECK (OPTION_MD_BASE + 11)
7bab8ab5
JB
13158#define OPTION_MOPERAND_CHECK (OPTION_MD_BASE + 12)
13159#define OPTION_MAVXSCALAR (OPTION_MD_BASE + 13)
13160#define OPTION_X32 (OPTION_MD_BASE + 14)
7e8b059b 13161#define OPTION_MADD_BND_PREFIX (OPTION_MD_BASE + 15)
43234a1e
L
13162#define OPTION_MEVEXLIG (OPTION_MD_BASE + 16)
13163#define OPTION_MEVEXWIG (OPTION_MD_BASE + 17)
167ad85b 13164#define OPTION_MBIG_OBJ (OPTION_MD_BASE + 18)
d1982f93 13165#define OPTION_MOMIT_LOCK_PREFIX (OPTION_MD_BASE + 19)
d3d3c6db 13166#define OPTION_MEVEXRCIG (OPTION_MD_BASE + 20)
8dcea932 13167#define OPTION_MSHARED (OPTION_MD_BASE + 21)
5db04b09
L
13168#define OPTION_MAMD64 (OPTION_MD_BASE + 22)
13169#define OPTION_MINTEL64 (OPTION_MD_BASE + 23)
e4e00185 13170#define OPTION_MFENCE_AS_LOCK_ADD (OPTION_MD_BASE + 24)
b4a3a7b4 13171#define OPTION_X86_USED_NOTE (OPTION_MD_BASE + 25)
03751133 13172#define OPTION_MVEXWIG (OPTION_MD_BASE + 26)
e379e5f3
L
13173#define OPTION_MALIGN_BRANCH_BOUNDARY (OPTION_MD_BASE + 27)
13174#define OPTION_MALIGN_BRANCH_PREFIX_SIZE (OPTION_MD_BASE + 28)
13175#define OPTION_MALIGN_BRANCH (OPTION_MD_BASE + 29)
76cf450b 13176#define OPTION_MBRANCHES_WITH_32B_BOUNDARIES (OPTION_MD_BASE + 30)
ae531041
L
13177#define OPTION_MLFENCE_AFTER_LOAD (OPTION_MD_BASE + 31)
13178#define OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH (OPTION_MD_BASE + 32)
13179#define OPTION_MLFENCE_BEFORE_RET (OPTION_MD_BASE + 33)
c8480b58 13180#define OPTION_MUSE_UNALIGNED_VECTOR_MOVE (OPTION_MD_BASE + 34)
b3b91714 13181
99ad8390
NC
13182struct option md_longopts[] =
13183{
3e73aa7c 13184 {"32", no_argument, NULL, OPTION_32},
321098a5 13185#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 13186 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c 13187 {"64", no_argument, NULL, OPTION_64},
351f65ca
L
13188#endif
13189#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 13190 {"x32", no_argument, NULL, OPTION_X32},
8dcea932 13191 {"mshared", no_argument, NULL, OPTION_MSHARED},
b4a3a7b4 13192 {"mx86-used-note", required_argument, NULL, OPTION_X86_USED_NOTE},
6e0b89ee 13193#endif
b3b91714 13194 {"divide", no_argument, NULL, OPTION_DIVIDE},
9103f4f4
L
13195 {"march", required_argument, NULL, OPTION_MARCH},
13196 {"mtune", required_argument, NULL, OPTION_MTUNE},
1efbbeb4
L
13197 {"mmnemonic", required_argument, NULL, OPTION_MMNEMONIC},
13198 {"msyntax", required_argument, NULL, OPTION_MSYNTAX},
13199 {"mindex-reg", no_argument, NULL, OPTION_MINDEX_REG},
13200 {"mnaked-reg", no_argument, NULL, OPTION_MNAKED_REG},
c0f3af97 13201 {"msse2avx", no_argument, NULL, OPTION_MSSE2AVX},
c8480b58 13202 {"muse-unaligned-vector-move", no_argument, NULL, OPTION_MUSE_UNALIGNED_VECTOR_MOVE},
daf50ae7 13203 {"msse-check", required_argument, NULL, OPTION_MSSE_CHECK},
7bab8ab5 13204 {"moperand-check", required_argument, NULL, OPTION_MOPERAND_CHECK},
539f890d 13205 {"mavxscalar", required_argument, NULL, OPTION_MAVXSCALAR},
03751133 13206 {"mvexwig", required_argument, NULL, OPTION_MVEXWIG},
7e8b059b 13207 {"madd-bnd-prefix", no_argument, NULL, OPTION_MADD_BND_PREFIX},
43234a1e
L
13208 {"mevexlig", required_argument, NULL, OPTION_MEVEXLIG},
13209 {"mevexwig", required_argument, NULL, OPTION_MEVEXWIG},
167ad85b
TG
13210# if defined (TE_PE) || defined (TE_PEP)
13211 {"mbig-obj", no_argument, NULL, OPTION_MBIG_OBJ},
13212#endif
d1982f93 13213 {"momit-lock-prefix", required_argument, NULL, OPTION_MOMIT_LOCK_PREFIX},
e4e00185 13214 {"mfence-as-lock-add", required_argument, NULL, OPTION_MFENCE_AS_LOCK_ADD},
0cb4071e 13215 {"mrelax-relocations", required_argument, NULL, OPTION_MRELAX_RELOCATIONS},
d3d3c6db 13216 {"mevexrcig", required_argument, NULL, OPTION_MEVEXRCIG},
e379e5f3
L
13217 {"malign-branch-boundary", required_argument, NULL, OPTION_MALIGN_BRANCH_BOUNDARY},
13218 {"malign-branch-prefix-size", required_argument, NULL, OPTION_MALIGN_BRANCH_PREFIX_SIZE},
13219 {"malign-branch", required_argument, NULL, OPTION_MALIGN_BRANCH},
76cf450b 13220 {"mbranches-within-32B-boundaries", no_argument, NULL, OPTION_MBRANCHES_WITH_32B_BOUNDARIES},
ae531041
L
13221 {"mlfence-after-load", required_argument, NULL, OPTION_MLFENCE_AFTER_LOAD},
13222 {"mlfence-before-indirect-branch", required_argument, NULL,
13223 OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH},
13224 {"mlfence-before-ret", required_argument, NULL, OPTION_MLFENCE_BEFORE_RET},
5db04b09
L
13225 {"mamd64", no_argument, NULL, OPTION_MAMD64},
13226 {"mintel64", no_argument, NULL, OPTION_MINTEL64},
252b5132
RH
13227 {NULL, no_argument, NULL, 0}
13228};
13229size_t md_longopts_size = sizeof (md_longopts);
13230
13231int
17b9d67d 13232md_parse_option (int c, const char *arg)
252b5132 13233{
91d6fa6a 13234 unsigned int j;
e379e5f3 13235 char *arch, *next, *saved, *type;
9103f4f4 13236
252b5132
RH
13237 switch (c)
13238 {
12b55ccc
L
13239 case 'n':
13240 optimize_align_code = 0;
13241 break;
13242
a38cf1db
AM
13243 case 'q':
13244 quiet_warnings = 1;
252b5132
RH
13245 break;
13246
13247#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
a38cf1db
AM
13248 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
13249 should be emitted or not. FIXME: Not implemented. */
13250 case 'Q':
d4693039
JB
13251 if ((arg[0] != 'y' && arg[0] != 'n') || arg[1])
13252 return 0;
252b5132
RH
13253 break;
13254
13255 /* -V: SVR4 argument to print version ID. */
13256 case 'V':
13257 print_version_id ();
13258 break;
13259
a38cf1db
AM
13260 /* -k: Ignore for FreeBSD compatibility. */
13261 case 'k':
252b5132 13262 break;
4cc782b5
ILT
13263
13264 case 's':
13265 /* -s: On i386 Solaris, this tells the native assembler to use
29b0f896 13266 .stab instead of .stab.excl. We always use .stab anyhow. */
4cc782b5 13267 break;
8dcea932
L
13268
13269 case OPTION_MSHARED:
13270 shared = 1;
13271 break;
b4a3a7b4
L
13272
13273 case OPTION_X86_USED_NOTE:
13274 if (strcasecmp (arg, "yes") == 0)
13275 x86_used_note = 1;
13276 else if (strcasecmp (arg, "no") == 0)
13277 x86_used_note = 0;
13278 else
13279 as_fatal (_("invalid -mx86-used-note= option: `%s'"), arg);
13280 break;
13281
13282
99ad8390 13283#endif
321098a5 13284#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 13285 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c
JH
13286 case OPTION_64:
13287 {
13288 const char **list, **l;
13289
3e73aa7c
JH
13290 list = bfd_target_list ();
13291 for (l = list; *l != NULL; l++)
08dedd66 13292 if (startswith (*l, "elf64-x86-64")
99ad8390
NC
13293 || strcmp (*l, "coff-x86-64") == 0
13294 || strcmp (*l, "pe-x86-64") == 0
d382c579
TG
13295 || strcmp (*l, "pei-x86-64") == 0
13296 || strcmp (*l, "mach-o-x86-64") == 0)
6e0b89ee
AM
13297 {
13298 default_arch = "x86_64";
13299 break;
13300 }
3e73aa7c 13301 if (*l == NULL)
2b5d6a91 13302 as_fatal (_("no compiled in support for x86_64"));
3e73aa7c
JH
13303 free (list);
13304 }
13305 break;
13306#endif
252b5132 13307
351f65ca 13308#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 13309 case OPTION_X32:
351f65ca
L
13310 if (IS_ELF)
13311 {
13312 const char **list, **l;
13313
13314 list = bfd_target_list ();
13315 for (l = list; *l != NULL; l++)
08dedd66 13316 if (startswith (*l, "elf32-x86-64"))
351f65ca
L
13317 {
13318 default_arch = "x86_64:32";
13319 break;
13320 }
13321 if (*l == NULL)
2b5d6a91 13322 as_fatal (_("no compiled in support for 32bit x86_64"));
351f65ca
L
13323 free (list);
13324 }
13325 else
13326 as_fatal (_("32bit x86_64 is only supported for ELF"));
13327 break;
13328#endif
13329
6e0b89ee
AM
13330 case OPTION_32:
13331 default_arch = "i386";
13332 break;
13333
b3b91714
AM
13334 case OPTION_DIVIDE:
13335#ifdef SVR4_COMMENT_CHARS
13336 {
13337 char *n, *t;
13338 const char *s;
13339
add39d23 13340 n = XNEWVEC (char, strlen (i386_comment_chars) + 1);
b3b91714
AM
13341 t = n;
13342 for (s = i386_comment_chars; *s != '\0'; s++)
13343 if (*s != '/')
13344 *t++ = *s;
13345 *t = '\0';
13346 i386_comment_chars = n;
13347 }
13348#endif
13349 break;
13350
9103f4f4 13351 case OPTION_MARCH:
293f5f65
L
13352 saved = xstrdup (arg);
13353 arch = saved;
13354 /* Allow -march=+nosse. */
13355 if (*arch == '+')
13356 arch++;
6305a203 13357 do
9103f4f4 13358 {
6305a203 13359 if (*arch == '.')
2b5d6a91 13360 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
13361 next = strchr (arch, '+');
13362 if (next)
13363 *next++ = '\0';
91d6fa6a 13364 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 13365 {
ae89daec
JB
13366 if (arch == saved && cpu_arch[j].type != PROCESSOR_NONE
13367 && strcmp (arch, cpu_arch[j].name) == 0)
ccc9c027 13368 {
6305a203 13369 /* Processor. */
ae89daec 13370 if (! cpu_arch[j].enable.bitfield.cpui386)
1ded5609
JB
13371 continue;
13372
91d6fa6a 13373 cpu_arch_name = cpu_arch[j].name;
d92c7521 13374 free (cpu_sub_arch_name);
6305a203 13375 cpu_sub_arch_name = NULL;
ae89daec 13376 cpu_arch_flags = cpu_arch[j].enable;
91d6fa6a 13377 cpu_arch_isa = cpu_arch[j].type;
ae89daec 13378 cpu_arch_isa_flags = cpu_arch[j].enable;
6305a203
L
13379 if (!cpu_arch_tune_set)
13380 {
13381 cpu_arch_tune = cpu_arch_isa;
13382 cpu_arch_tune_flags = cpu_arch_isa_flags;
13383 }
13384 break;
13385 }
ae89daec
JB
13386 else if (cpu_arch[j].type == PROCESSOR_NONE
13387 && strcmp (arch, cpu_arch[j].name) == 0
13388 && !cpu_flags_all_zero (&cpu_arch[j].enable))
6305a203 13389 {
33eaf5de 13390 /* ISA extension. */
6305a203 13391 i386_cpu_flags flags;
309d3373 13392
293f5f65 13393 flags = cpu_flags_or (cpu_arch_flags,
ae89daec 13394 cpu_arch[j].enable);
81486035 13395
5b64d091 13396 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
6305a203 13397 {
ae89daec 13398 extend_cpu_sub_arch_name (arch);
6305a203 13399 cpu_arch_flags = flags;
a586129e 13400 cpu_arch_isa_flags = flags;
6305a203 13401 }
0089dace
L
13402 else
13403 cpu_arch_isa_flags
13404 = cpu_flags_or (cpu_arch_isa_flags,
ae89daec 13405 cpu_arch[j].enable);
6305a203 13406 break;
ccc9c027 13407 }
9103f4f4 13408 }
6305a203 13409
ae89daec 13410 if (j >= ARRAY_SIZE (cpu_arch) && startswith (arch, "no"))
293f5f65 13411 {
33eaf5de 13412 /* Disable an ISA extension. */
ae89daec
JB
13413 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13414 if (cpu_arch[j].type == PROCESSOR_NONE
13415 && strcmp (arch + 2, cpu_arch[j].name) == 0)
293f5f65
L
13416 {
13417 i386_cpu_flags flags;
13418
13419 flags = cpu_flags_and_not (cpu_arch_flags,
ae89daec 13420 cpu_arch[j].disable);
293f5f65
L
13421 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
13422 {
8180707f 13423 extend_cpu_sub_arch_name (arch);
293f5f65
L
13424 cpu_arch_flags = flags;
13425 cpu_arch_isa_flags = flags;
13426 }
13427 break;
13428 }
293f5f65
L
13429 }
13430
91d6fa6a 13431 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 13432 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
13433
13434 arch = next;
9103f4f4 13435 }
293f5f65
L
13436 while (next != NULL);
13437 free (saved);
9103f4f4
L
13438 break;
13439
13440 case OPTION_MTUNE:
13441 if (*arg == '.')
2b5d6a91 13442 as_fatal (_("invalid -mtune= option: `%s'"), arg);
91d6fa6a 13443 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 13444 {
ae89daec
JB
13445 if (cpu_arch[j].type != PROCESSOR_NONE
13446 && strcmp (arg, cpu_arch[j].name) == 0)
9103f4f4 13447 {
ccc9c027 13448 cpu_arch_tune_set = 1;
91d6fa6a 13449 cpu_arch_tune = cpu_arch [j].type;
ae89daec 13450 cpu_arch_tune_flags = cpu_arch[j].enable;
9103f4f4
L
13451 break;
13452 }
13453 }
91d6fa6a 13454 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 13455 as_fatal (_("invalid -mtune= option: `%s'"), arg);
9103f4f4
L
13456 break;
13457
1efbbeb4
L
13458 case OPTION_MMNEMONIC:
13459 if (strcasecmp (arg, "att") == 0)
13460 intel_mnemonic = 0;
13461 else if (strcasecmp (arg, "intel") == 0)
13462 intel_mnemonic = 1;
13463 else
2b5d6a91 13464 as_fatal (_("invalid -mmnemonic= option: `%s'"), arg);
1efbbeb4
L
13465 break;
13466
13467 case OPTION_MSYNTAX:
13468 if (strcasecmp (arg, "att") == 0)
13469 intel_syntax = 0;
13470 else if (strcasecmp (arg, "intel") == 0)
13471 intel_syntax = 1;
13472 else
2b5d6a91 13473 as_fatal (_("invalid -msyntax= option: `%s'"), arg);
1efbbeb4
L
13474 break;
13475
13476 case OPTION_MINDEX_REG:
13477 allow_index_reg = 1;
13478 break;
13479
13480 case OPTION_MNAKED_REG:
13481 allow_naked_reg = 1;
13482 break;
13483
c0f3af97
L
13484 case OPTION_MSSE2AVX:
13485 sse2avx = 1;
13486 break;
13487
c8480b58
L
13488 case OPTION_MUSE_UNALIGNED_VECTOR_MOVE:
13489 use_unaligned_vector_move = 1;
13490 break;
13491
daf50ae7
L
13492 case OPTION_MSSE_CHECK:
13493 if (strcasecmp (arg, "error") == 0)
7bab8ab5 13494 sse_check = check_error;
daf50ae7 13495 else if (strcasecmp (arg, "warning") == 0)
7bab8ab5 13496 sse_check = check_warning;
daf50ae7 13497 else if (strcasecmp (arg, "none") == 0)
7bab8ab5 13498 sse_check = check_none;
daf50ae7 13499 else
2b5d6a91 13500 as_fatal (_("invalid -msse-check= option: `%s'"), arg);
daf50ae7
L
13501 break;
13502
7bab8ab5
JB
13503 case OPTION_MOPERAND_CHECK:
13504 if (strcasecmp (arg, "error") == 0)
13505 operand_check = check_error;
13506 else if (strcasecmp (arg, "warning") == 0)
13507 operand_check = check_warning;
13508 else if (strcasecmp (arg, "none") == 0)
13509 operand_check = check_none;
13510 else
13511 as_fatal (_("invalid -moperand-check= option: `%s'"), arg);
13512 break;
13513
539f890d
L
13514 case OPTION_MAVXSCALAR:
13515 if (strcasecmp (arg, "128") == 0)
13516 avxscalar = vex128;
13517 else if (strcasecmp (arg, "256") == 0)
13518 avxscalar = vex256;
13519 else
2b5d6a91 13520 as_fatal (_("invalid -mavxscalar= option: `%s'"), arg);
539f890d
L
13521 break;
13522
03751133
L
13523 case OPTION_MVEXWIG:
13524 if (strcmp (arg, "0") == 0)
40c9c8de 13525 vexwig = vexw0;
03751133 13526 else if (strcmp (arg, "1") == 0)
40c9c8de 13527 vexwig = vexw1;
03751133
L
13528 else
13529 as_fatal (_("invalid -mvexwig= option: `%s'"), arg);
13530 break;
13531
7e8b059b
L
13532 case OPTION_MADD_BND_PREFIX:
13533 add_bnd_prefix = 1;
13534 break;
13535
43234a1e
L
13536 case OPTION_MEVEXLIG:
13537 if (strcmp (arg, "128") == 0)
13538 evexlig = evexl128;
13539 else if (strcmp (arg, "256") == 0)
13540 evexlig = evexl256;
13541 else if (strcmp (arg, "512") == 0)
13542 evexlig = evexl512;
13543 else
13544 as_fatal (_("invalid -mevexlig= option: `%s'"), arg);
13545 break;
13546
d3d3c6db
IT
13547 case OPTION_MEVEXRCIG:
13548 if (strcmp (arg, "rne") == 0)
13549 evexrcig = rne;
13550 else if (strcmp (arg, "rd") == 0)
13551 evexrcig = rd;
13552 else if (strcmp (arg, "ru") == 0)
13553 evexrcig = ru;
13554 else if (strcmp (arg, "rz") == 0)
13555 evexrcig = rz;
13556 else
13557 as_fatal (_("invalid -mevexrcig= option: `%s'"), arg);
13558 break;
13559
43234a1e
L
13560 case OPTION_MEVEXWIG:
13561 if (strcmp (arg, "0") == 0)
13562 evexwig = evexw0;
13563 else if (strcmp (arg, "1") == 0)
13564 evexwig = evexw1;
13565 else
13566 as_fatal (_("invalid -mevexwig= option: `%s'"), arg);
13567 break;
13568
167ad85b
TG
13569# if defined (TE_PE) || defined (TE_PEP)
13570 case OPTION_MBIG_OBJ:
13571 use_big_obj = 1;
13572 break;
13573#endif
13574
d1982f93 13575 case OPTION_MOMIT_LOCK_PREFIX:
d022bddd
IT
13576 if (strcasecmp (arg, "yes") == 0)
13577 omit_lock_prefix = 1;
13578 else if (strcasecmp (arg, "no") == 0)
13579 omit_lock_prefix = 0;
13580 else
13581 as_fatal (_("invalid -momit-lock-prefix= option: `%s'"), arg);
13582 break;
13583
e4e00185
AS
13584 case OPTION_MFENCE_AS_LOCK_ADD:
13585 if (strcasecmp (arg, "yes") == 0)
13586 avoid_fence = 1;
13587 else if (strcasecmp (arg, "no") == 0)
13588 avoid_fence = 0;
13589 else
13590 as_fatal (_("invalid -mfence-as-lock-add= option: `%s'"), arg);
13591 break;
13592
ae531041
L
13593 case OPTION_MLFENCE_AFTER_LOAD:
13594 if (strcasecmp (arg, "yes") == 0)
13595 lfence_after_load = 1;
13596 else if (strcasecmp (arg, "no") == 0)
13597 lfence_after_load = 0;
13598 else
13599 as_fatal (_("invalid -mlfence-after-load= option: `%s'"), arg);
13600 break;
13601
13602 case OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH:
13603 if (strcasecmp (arg, "all") == 0)
a09f656b 13604 {
13605 lfence_before_indirect_branch = lfence_branch_all;
13606 if (lfence_before_ret == lfence_before_ret_none)
13607 lfence_before_ret = lfence_before_ret_shl;
13608 }
ae531041
L
13609 else if (strcasecmp (arg, "memory") == 0)
13610 lfence_before_indirect_branch = lfence_branch_memory;
13611 else if (strcasecmp (arg, "register") == 0)
13612 lfence_before_indirect_branch = lfence_branch_register;
13613 else if (strcasecmp (arg, "none") == 0)
13614 lfence_before_indirect_branch = lfence_branch_none;
13615 else
13616 as_fatal (_("invalid -mlfence-before-indirect-branch= option: `%s'"),
13617 arg);
13618 break;
13619
13620 case OPTION_MLFENCE_BEFORE_RET:
13621 if (strcasecmp (arg, "or") == 0)
13622 lfence_before_ret = lfence_before_ret_or;
13623 else if (strcasecmp (arg, "not") == 0)
13624 lfence_before_ret = lfence_before_ret_not;
a09f656b 13625 else if (strcasecmp (arg, "shl") == 0 || strcasecmp (arg, "yes") == 0)
13626 lfence_before_ret = lfence_before_ret_shl;
ae531041
L
13627 else if (strcasecmp (arg, "none") == 0)
13628 lfence_before_ret = lfence_before_ret_none;
13629 else
13630 as_fatal (_("invalid -mlfence-before-ret= option: `%s'"),
13631 arg);
13632 break;
13633
0cb4071e
L
13634 case OPTION_MRELAX_RELOCATIONS:
13635 if (strcasecmp (arg, "yes") == 0)
13636 generate_relax_relocations = 1;
13637 else if (strcasecmp (arg, "no") == 0)
13638 generate_relax_relocations = 0;
13639 else
13640 as_fatal (_("invalid -mrelax-relocations= option: `%s'"), arg);
13641 break;
13642
e379e5f3
L
13643 case OPTION_MALIGN_BRANCH_BOUNDARY:
13644 {
13645 char *end;
13646 long int align = strtoul (arg, &end, 0);
13647 if (*end == '\0')
13648 {
13649 if (align == 0)
13650 {
13651 align_branch_power = 0;
13652 break;
13653 }
13654 else if (align >= 16)
13655 {
13656 int align_power;
13657 for (align_power = 0;
13658 (align & 1) == 0;
13659 align >>= 1, align_power++)
13660 continue;
13661 /* Limit alignment power to 31. */
13662 if (align == 1 && align_power < 32)
13663 {
13664 align_branch_power = align_power;
13665 break;
13666 }
13667 }
13668 }
13669 as_fatal (_("invalid -malign-branch-boundary= value: %s"), arg);
13670 }
13671 break;
13672
13673 case OPTION_MALIGN_BRANCH_PREFIX_SIZE:
13674 {
13675 char *end;
13676 int align = strtoul (arg, &end, 0);
13677 /* Some processors only support 5 prefixes. */
13678 if (*end == '\0' && align >= 0 && align < 6)
13679 {
13680 align_branch_prefix_size = align;
13681 break;
13682 }
13683 as_fatal (_("invalid -malign-branch-prefix-size= value: %s"),
13684 arg);
13685 }
13686 break;
13687
13688 case OPTION_MALIGN_BRANCH:
13689 align_branch = 0;
13690 saved = xstrdup (arg);
13691 type = saved;
13692 do
13693 {
13694 next = strchr (type, '+');
13695 if (next)
13696 *next++ = '\0';
13697 if (strcasecmp (type, "jcc") == 0)
13698 align_branch |= align_branch_jcc_bit;
13699 else if (strcasecmp (type, "fused") == 0)
13700 align_branch |= align_branch_fused_bit;
13701 else if (strcasecmp (type, "jmp") == 0)
13702 align_branch |= align_branch_jmp_bit;
13703 else if (strcasecmp (type, "call") == 0)
13704 align_branch |= align_branch_call_bit;
13705 else if (strcasecmp (type, "ret") == 0)
13706 align_branch |= align_branch_ret_bit;
13707 else if (strcasecmp (type, "indirect") == 0)
13708 align_branch |= align_branch_indirect_bit;
13709 else
13710 as_fatal (_("invalid -malign-branch= option: `%s'"), arg);
13711 type = next;
13712 }
13713 while (next != NULL);
13714 free (saved);
13715 break;
13716
76cf450b
L
13717 case OPTION_MBRANCHES_WITH_32B_BOUNDARIES:
13718 align_branch_power = 5;
13719 align_branch_prefix_size = 5;
13720 align_branch = (align_branch_jcc_bit
13721 | align_branch_fused_bit
13722 | align_branch_jmp_bit);
13723 break;
13724
5db04b09 13725 case OPTION_MAMD64:
4b5aaf5f 13726 isa64 = amd64;
5db04b09
L
13727 break;
13728
13729 case OPTION_MINTEL64:
4b5aaf5f 13730 isa64 = intel64;
5db04b09
L
13731 break;
13732
b6f8c7c4
L
13733 case 'O':
13734 if (arg == NULL)
13735 {
13736 optimize = 1;
13737 /* Turn off -Os. */
13738 optimize_for_space = 0;
13739 }
13740 else if (*arg == 's')
13741 {
13742 optimize_for_space = 1;
13743 /* Turn on all encoding optimizations. */
41fd2579 13744 optimize = INT_MAX;
b6f8c7c4
L
13745 }
13746 else
13747 {
13748 optimize = atoi (arg);
13749 /* Turn off -Os. */
13750 optimize_for_space = 0;
13751 }
13752 break;
13753
252b5132
RH
13754 default:
13755 return 0;
13756 }
13757 return 1;
13758}
13759
8a2c8fef
L
13760#define MESSAGE_TEMPLATE \
13761" "
13762
293f5f65
L
13763static char *
13764output_message (FILE *stream, char *p, char *message, char *start,
13765 int *left_p, const char *name, int len)
13766{
13767 int size = sizeof (MESSAGE_TEMPLATE);
13768 int left = *left_p;
13769
13770 /* Reserve 2 spaces for ", " or ",\0" */
13771 left -= len + 2;
13772
13773 /* Check if there is any room. */
13774 if (left >= 0)
13775 {
13776 if (p != start)
13777 {
13778 *p++ = ',';
13779 *p++ = ' ';
13780 }
13781 p = mempcpy (p, name, len);
13782 }
13783 else
13784 {
13785 /* Output the current message now and start a new one. */
13786 *p++ = ',';
13787 *p = '\0';
13788 fprintf (stream, "%s\n", message);
13789 p = start;
13790 left = size - (start - message) - len - 2;
13791
13792 gas_assert (left >= 0);
13793
13794 p = mempcpy (p, name, len);
13795 }
13796
13797 *left_p = left;
13798 return p;
13799}
13800
8a2c8fef 13801static void
1ded5609 13802show_arch (FILE *stream, int ext, int check)
8a2c8fef
L
13803{
13804 static char message[] = MESSAGE_TEMPLATE;
13805 char *start = message + 27;
13806 char *p;
13807 int size = sizeof (MESSAGE_TEMPLATE);
13808 int left;
13809 const char *name;
13810 int len;
13811 unsigned int j;
13812
13813 p = start;
13814 left = size - (start - message);
3ce2ebcf
JB
13815
13816 if (!ext && check)
13817 {
13818 p = output_message (stream, p, message, start, &left,
13819 STRING_COMMA_LEN ("default"));
f68697e8
JB
13820 p = output_message (stream, p, message, start, &left,
13821 STRING_COMMA_LEN ("push"));
13822 p = output_message (stream, p, message, start, &left,
13823 STRING_COMMA_LEN ("pop"));
3ce2ebcf
JB
13824 }
13825
8a2c8fef
L
13826 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13827 {
13828 /* Should it be skipped? */
13829 if (cpu_arch [j].skip)
13830 continue;
13831
13832 name = cpu_arch [j].name;
13833 len = cpu_arch [j].len;
ae89daec 13834 if (cpu_arch[j].type == PROCESSOR_NONE)
8a2c8fef
L
13835 {
13836 /* It is an extension. Skip if we aren't asked to show it. */
ae89daec 13837 if (!ext || cpu_flags_all_zero (&cpu_arch[j].enable))
8a2c8fef
L
13838 continue;
13839 }
13840 else if (ext)
13841 {
13842 /* It is an processor. Skip if we show only extension. */
13843 continue;
13844 }
ae89daec 13845 else if (check && ! cpu_arch[j].enable.bitfield.cpui386)
1ded5609
JB
13846 {
13847 /* It is an impossible processor - skip. */
13848 continue;
13849 }
8a2c8fef 13850
293f5f65 13851 p = output_message (stream, p, message, start, &left, name, len);
8a2c8fef
L
13852 }
13853
293f5f65
L
13854 /* Display disabled extensions. */
13855 if (ext)
ae89daec 13856 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
293f5f65 13857 {
ae89daec
JB
13858 char *str;
13859
13860 if (cpu_arch[j].type != PROCESSOR_NONE
13861 || !cpu_flags_all_zero (&cpu_arch[j].enable))
13862 continue;
13863 str = xasprintf ("no%s", cpu_arch[j].name);
13864 p = output_message (stream, p, message, start, &left, str,
13865 strlen (str));
13866 free (str);
293f5f65
L
13867 }
13868
8a2c8fef
L
13869 *p = '\0';
13870 fprintf (stream, "%s\n", message);
13871}
13872
252b5132 13873void
8a2c8fef 13874md_show_usage (FILE *stream)
252b5132 13875{
4cc782b5
ILT
13876#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13877 fprintf (stream, _("\
d4693039 13878 -Qy, -Qn ignored\n\
a38cf1db 13879 -V print assembler version number\n\
b3b91714
AM
13880 -k ignored\n"));
13881#endif
13882 fprintf (stream, _("\
7ebd68d1
NC
13883 -n do not optimize code alignment\n\
13884 -O{012s} attempt some code optimizations\n\
b3b91714
AM
13885 -q quieten some warnings\n"));
13886#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13887 fprintf (stream, _("\
a38cf1db 13888 -s ignored\n"));
b3b91714 13889#endif
b00af7c8
JB
13890#ifdef BFD64
13891# if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13892 fprintf (stream, _("\
13893 --32/--64/--x32 generate 32bit/64bit/x32 object\n"));
13894# elif defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O)
751d281c 13895 fprintf (stream, _("\
b00af7c8
JB
13896 --32/--64 generate 32bit/64bit object\n"));
13897# endif
751d281c 13898#endif
b3b91714
AM
13899#ifdef SVR4_COMMENT_CHARS
13900 fprintf (stream, _("\
13901 --divide do not treat `/' as a comment character\n"));
a38cf1db
AM
13902#else
13903 fprintf (stream, _("\
b3b91714 13904 --divide ignored\n"));
4cc782b5 13905#endif
9103f4f4 13906 fprintf (stream, _("\
6305a203 13907 -march=CPU[,+EXTENSION...]\n\
8a2c8fef 13908 generate code for CPU and EXTENSION, CPU is one of:\n"));
1ded5609 13909 show_arch (stream, 0, 1);
8a2c8fef 13910 fprintf (stream, _("\
ae89daec 13911 EXTENSION is combination of (possibly \"no\"-prefixed):\n"));
1ded5609 13912 show_arch (stream, 1, 0);
6305a203 13913 fprintf (stream, _("\
8a2c8fef 13914 -mtune=CPU optimize for CPU, CPU is one of:\n"));
1ded5609 13915 show_arch (stream, 0, 0);
ba104c83 13916 fprintf (stream, _("\
c0f3af97
L
13917 -msse2avx encode SSE instructions with VEX prefix\n"));
13918 fprintf (stream, _("\
c8480b58
L
13919 -muse-unaligned-vector-move\n\
13920 encode aligned vector move as unaligned vector move\n"));
13921 fprintf (stream, _("\
7c5c05ef 13922 -msse-check=[none|error|warning] (default: warning)\n\
daf50ae7
L
13923 check SSE instructions\n"));
13924 fprintf (stream, _("\
7c5c05ef 13925 -moperand-check=[none|error|warning] (default: warning)\n\
7bab8ab5
JB
13926 check operand combinations for validity\n"));
13927 fprintf (stream, _("\
7c5c05ef
L
13928 -mavxscalar=[128|256] (default: 128)\n\
13929 encode scalar AVX instructions with specific vector\n\
539f890d
L
13930 length\n"));
13931 fprintf (stream, _("\
03751133
L
13932 -mvexwig=[0|1] (default: 0)\n\
13933 encode VEX instructions with specific VEX.W value\n\
13934 for VEX.W bit ignored instructions\n"));
13935 fprintf (stream, _("\
7c5c05ef
L
13936 -mevexlig=[128|256|512] (default: 128)\n\
13937 encode scalar EVEX instructions with specific vector\n\
43234a1e
L
13938 length\n"));
13939 fprintf (stream, _("\
7c5c05ef
L
13940 -mevexwig=[0|1] (default: 0)\n\
13941 encode EVEX instructions with specific EVEX.W value\n\
43234a1e
L
13942 for EVEX.W bit ignored instructions\n"));
13943 fprintf (stream, _("\
7c5c05ef 13944 -mevexrcig=[rne|rd|ru|rz] (default: rne)\n\
d3d3c6db
IT
13945 encode EVEX instructions with specific EVEX.RC value\n\
13946 for SAE-only ignored instructions\n"));
13947 fprintf (stream, _("\
7c5c05ef
L
13948 -mmnemonic=[att|intel] "));
13949 if (SYSV386_COMPAT)
13950 fprintf (stream, _("(default: att)\n"));
13951 else
13952 fprintf (stream, _("(default: intel)\n"));
13953 fprintf (stream, _("\
13954 use AT&T/Intel mnemonic\n"));
ba104c83 13955 fprintf (stream, _("\
7c5c05ef
L
13956 -msyntax=[att|intel] (default: att)\n\
13957 use AT&T/Intel syntax\n"));
ba104c83
L
13958 fprintf (stream, _("\
13959 -mindex-reg support pseudo index registers\n"));
13960 fprintf (stream, _("\
13961 -mnaked-reg don't require `%%' prefix for registers\n"));
13962 fprintf (stream, _("\
7e8b059b 13963 -madd-bnd-prefix add BND prefix for all valid branches\n"));
b4a3a7b4 13964#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8dcea932
L
13965 fprintf (stream, _("\
13966 -mshared disable branch optimization for shared code\n"));
b4a3a7b4
L
13967 fprintf (stream, _("\
13968 -mx86-used-note=[no|yes] "));
13969 if (DEFAULT_X86_USED_NOTE)
13970 fprintf (stream, _("(default: yes)\n"));
13971 else
13972 fprintf (stream, _("(default: no)\n"));
13973 fprintf (stream, _("\
13974 generate x86 used ISA and feature properties\n"));
13975#endif
13976#if defined (TE_PE) || defined (TE_PEP)
167ad85b
TG
13977 fprintf (stream, _("\
13978 -mbig-obj generate big object files\n"));
13979#endif
d022bddd 13980 fprintf (stream, _("\
7c5c05ef 13981 -momit-lock-prefix=[no|yes] (default: no)\n\
d022bddd 13982 strip all lock prefixes\n"));
5db04b09 13983 fprintf (stream, _("\
7c5c05ef 13984 -mfence-as-lock-add=[no|yes] (default: no)\n\
e4e00185
AS
13985 encode lfence, mfence and sfence as\n\
13986 lock addl $0x0, (%%{re}sp)\n"));
13987 fprintf (stream, _("\
7c5c05ef
L
13988 -mrelax-relocations=[no|yes] "));
13989 if (DEFAULT_GENERATE_X86_RELAX_RELOCATIONS)
13990 fprintf (stream, _("(default: yes)\n"));
13991 else
13992 fprintf (stream, _("(default: no)\n"));
13993 fprintf (stream, _("\
0cb4071e
L
13994 generate relax relocations\n"));
13995 fprintf (stream, _("\
e379e5f3
L
13996 -malign-branch-boundary=NUM (default: 0)\n\
13997 align branches within NUM byte boundary\n"));
13998 fprintf (stream, _("\
13999 -malign-branch=TYPE[+TYPE...] (default: jcc+fused+jmp)\n\
14000 TYPE is combination of jcc, fused, jmp, call, ret,\n\
14001 indirect\n\
14002 specify types of branches to align\n"));
14003 fprintf (stream, _("\
14004 -malign-branch-prefix-size=NUM (default: 5)\n\
14005 align branches with NUM prefixes per instruction\n"));
14006 fprintf (stream, _("\
76cf450b
L
14007 -mbranches-within-32B-boundaries\n\
14008 align branches within 32 byte boundary\n"));
14009 fprintf (stream, _("\
ae531041
L
14010 -mlfence-after-load=[no|yes] (default: no)\n\
14011 generate lfence after load\n"));
14012 fprintf (stream, _("\
14013 -mlfence-before-indirect-branch=[none|all|register|memory] (default: none)\n\
14014 generate lfence before indirect near branch\n"));
14015 fprintf (stream, _("\
a09f656b 14016 -mlfence-before-ret=[none|or|not|shl|yes] (default: none)\n\
ae531041
L
14017 generate lfence before ret\n"));
14018 fprintf (stream, _("\
7c5c05ef 14019 -mamd64 accept only AMD64 ISA [default]\n"));
5db04b09
L
14020 fprintf (stream, _("\
14021 -mintel64 accept only Intel64 ISA\n"));
252b5132
RH
14022}
14023
3e73aa7c 14024#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
321098a5 14025 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
e57f8c65 14026 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
252b5132
RH
14027
14028/* Pick the target format to use. */
14029
47926f60 14030const char *
e3bb37b5 14031i386_target_format (void)
252b5132 14032{
d34049e8 14033 if (startswith (default_arch, "x86_64"))
351f65ca
L
14034 {
14035 update_code_flag (CODE_64BIT, 1);
14036 if (default_arch[6] == '\0')
7f56bc95 14037 x86_elf_abi = X86_64_ABI;
351f65ca 14038 else
7f56bc95 14039 x86_elf_abi = X86_64_X32_ABI;
351f65ca 14040 }
3e73aa7c 14041 else if (!strcmp (default_arch, "i386"))
78f12dd3 14042 update_code_flag (CODE_32BIT, 1);
5197d474
L
14043 else if (!strcmp (default_arch, "iamcu"))
14044 {
14045 update_code_flag (CODE_32BIT, 1);
14046 if (cpu_arch_isa == PROCESSOR_UNKNOWN)
14047 {
14048 static const i386_cpu_flags iamcu_flags = CPU_IAMCU_FLAGS;
14049 cpu_arch_name = "iamcu";
d92c7521 14050 free (cpu_sub_arch_name);
5197d474
L
14051 cpu_sub_arch_name = NULL;
14052 cpu_arch_flags = iamcu_flags;
14053 cpu_arch_isa = PROCESSOR_IAMCU;
14054 cpu_arch_isa_flags = iamcu_flags;
14055 if (!cpu_arch_tune_set)
14056 {
14057 cpu_arch_tune = cpu_arch_isa;
14058 cpu_arch_tune_flags = cpu_arch_isa_flags;
14059 }
14060 }
8d471ec1 14061 else if (cpu_arch_isa != PROCESSOR_IAMCU)
5197d474
L
14062 as_fatal (_("Intel MCU doesn't support `%s' architecture"),
14063 cpu_arch_name);
14064 }
3e73aa7c 14065 else
2b5d6a91 14066 as_fatal (_("unknown architecture"));
89507696
JB
14067
14068 if (cpu_flags_all_zero (&cpu_arch_isa_flags))
ae89daec 14069 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
89507696 14070 if (cpu_flags_all_zero (&cpu_arch_tune_flags))
ae89daec 14071 cpu_arch_tune_flags = cpu_arch[flag_code == CODE_64BIT].enable;
89507696 14072
252b5132
RH
14073 switch (OUTPUT_FLAVOR)
14074 {
9384f2ff 14075#if defined (OBJ_MAYBE_AOUT) || defined (OBJ_AOUT)
4c63da97 14076 case bfd_target_aout_flavour:
47926f60 14077 return AOUT_TARGET_FORMAT;
4c63da97 14078#endif
9384f2ff
AM
14079#if defined (OBJ_MAYBE_COFF) || defined (OBJ_COFF)
14080# if defined (TE_PE) || defined (TE_PEP)
14081 case bfd_target_coff_flavour:
167ad85b 14082 if (flag_code == CODE_64BIT)
eb19308f
JB
14083 {
14084 object_64bit = 1;
14085 return use_big_obj ? "pe-bigobj-x86-64" : "pe-x86-64";
14086 }
14087 return use_big_obj ? "pe-bigobj-i386" : "pe-i386";
9384f2ff 14088# elif defined (TE_GO32)
0561d57c
JK
14089 case bfd_target_coff_flavour:
14090 return "coff-go32";
9384f2ff 14091# else
252b5132
RH
14092 case bfd_target_coff_flavour:
14093 return "coff-i386";
9384f2ff 14094# endif
4c63da97 14095#endif
3e73aa7c 14096#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
252b5132 14097 case bfd_target_elf_flavour:
3e73aa7c 14098 {
351f65ca
L
14099 const char *format;
14100
14101 switch (x86_elf_abi)
4fa24527 14102 {
351f65ca
L
14103 default:
14104 format = ELF_TARGET_FORMAT;
e379e5f3
L
14105#ifndef TE_SOLARIS
14106 tls_get_addr = "___tls_get_addr";
14107#endif
351f65ca 14108 break;
7f56bc95 14109 case X86_64_ABI:
351f65ca 14110 use_rela_relocations = 1;
4fa24527 14111 object_64bit = 1;
e379e5f3
L
14112#ifndef TE_SOLARIS
14113 tls_get_addr = "__tls_get_addr";
14114#endif
351f65ca
L
14115 format = ELF_TARGET_FORMAT64;
14116 break;
7f56bc95 14117 case X86_64_X32_ABI:
4fa24527 14118 use_rela_relocations = 1;
351f65ca 14119 object_64bit = 1;
e379e5f3
L
14120#ifndef TE_SOLARIS
14121 tls_get_addr = "__tls_get_addr";
14122#endif
862be3fb 14123 disallow_64bit_reloc = 1;
351f65ca
L
14124 format = ELF_TARGET_FORMAT32;
14125 break;
4fa24527 14126 }
c085ab00 14127 if (cpu_arch_isa == PROCESSOR_IAMCU)
81486035
L
14128 {
14129 if (x86_elf_abi != I386_ABI)
14130 as_fatal (_("Intel MCU is 32bit only"));
14131 return ELF_TARGET_IAMCU_FORMAT;
14132 }
8a9036a4 14133 else
351f65ca 14134 return format;
3e73aa7c 14135 }
e57f8c65
TG
14136#endif
14137#if defined (OBJ_MACH_O)
14138 case bfd_target_mach_o_flavour:
d382c579
TG
14139 if (flag_code == CODE_64BIT)
14140 {
14141 use_rela_relocations = 1;
14142 object_64bit = 1;
14143 return "mach-o-x86-64";
14144 }
14145 else
14146 return "mach-o-i386";
4c63da97 14147#endif
252b5132
RH
14148 default:
14149 abort ();
14150 return NULL;
14151 }
14152}
14153
47926f60 14154#endif /* OBJ_MAYBE_ more than one */
252b5132 14155\f
252b5132 14156symbolS *
7016a5d5 14157md_undefined_symbol (char *name)
252b5132 14158{
18dc2407
ILT
14159 if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
14160 && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
14161 && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
14162 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
24eab124
AM
14163 {
14164 if (!GOT_symbol)
14165 {
14166 if (symbol_find (name))
14167 as_bad (_("GOT already in symbol table"));
14168 GOT_symbol = symbol_new (name, undefined_section,
e01e1cee 14169 &zero_address_frag, 0);
24eab124
AM
14170 };
14171 return GOT_symbol;
14172 }
252b5132
RH
14173 return 0;
14174}
14175
14176/* Round up a section size to the appropriate boundary. */
47926f60 14177
252b5132 14178valueT
7016a5d5 14179md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
252b5132 14180{
4c63da97
AM
14181#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
14182 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
14183 {
14184 /* For a.out, force the section size to be aligned. If we don't do
14185 this, BFD will align it for us, but it will not write out the
14186 final bytes of the section. This may be a bug in BFD, but it is
14187 easier to fix it here since that is how the other a.out targets
14188 work. */
14189 int align;
14190
fd361982 14191 align = bfd_section_alignment (segment);
8d3842cd 14192 size = ((size + (1 << align) - 1) & (-((valueT) 1 << align)));
4c63da97 14193 }
252b5132
RH
14194#endif
14195
14196 return size;
14197}
14198
14199/* On the i386, PC-relative offsets are relative to the start of the
14200 next instruction. That is, the address of the offset, plus its
14201 size, since the offset is always the last part of the insn. */
14202
14203long
e3bb37b5 14204md_pcrel_from (fixS *fixP)
252b5132
RH
14205{
14206 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
14207}
14208
14209#ifndef I386COFF
14210
14211static void
e3bb37b5 14212s_bss (int ignore ATTRIBUTE_UNUSED)
252b5132 14213{
29b0f896 14214 int temp;
252b5132 14215
8a75718c
JB
14216#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14217 if (IS_ELF)
14218 obj_elf_section_change_hook ();
14219#endif
252b5132
RH
14220 temp = get_absolute_expression ();
14221 subseg_set (bss_section, (subsegT) temp);
14222 demand_empty_rest_of_line ();
14223}
14224
14225#endif
14226
e379e5f3
L
14227/* Remember constant directive. */
14228
14229void
14230i386_cons_align (int ignore ATTRIBUTE_UNUSED)
14231{
14232 if (last_insn.kind != last_insn_directive
14233 && (bfd_section_flags (now_seg) & SEC_CODE))
14234 {
14235 last_insn.seg = now_seg;
14236 last_insn.kind = last_insn_directive;
14237 last_insn.name = "constant directive";
14238 last_insn.file = as_where (&last_insn.line);
ae531041
L
14239 if (lfence_before_ret != lfence_before_ret_none)
14240 {
14241 if (lfence_before_indirect_branch != lfence_branch_none)
14242 as_warn (_("constant directive skips -mlfence-before-ret "
14243 "and -mlfence-before-indirect-branch"));
14244 else
14245 as_warn (_("constant directive skips -mlfence-before-ret"));
14246 }
14247 else if (lfence_before_indirect_branch != lfence_branch_none)
14248 as_warn (_("constant directive skips -mlfence-before-indirect-branch"));
e379e5f3
L
14249 }
14250}
14251
3abbafc2 14252int
e3bb37b5 14253i386_validate_fix (fixS *fixp)
252b5132 14254{
e52a16f2
JB
14255 if (fixp->fx_addsy && S_GET_SEGMENT(fixp->fx_addsy) == reg_section)
14256 {
14257 reloc_howto_type *howto;
14258
14259 howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
14260 as_bad_where (fixp->fx_file, fixp->fx_line,
14261 _("invalid %s relocation against register"),
14262 howto ? howto->name : "<unknown>");
14263 return 0;
14264 }
14265
3abbafc2
JB
14266#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14267 if (fixp->fx_r_type == BFD_RELOC_SIZE32
14268 || fixp->fx_r_type == BFD_RELOC_SIZE64)
14269 return IS_ELF && fixp->fx_addsy
14270 && (!S_IS_DEFINED (fixp->fx_addsy)
14271 || S_IS_EXTERNAL (fixp->fx_addsy));
14272#endif
14273
02a86693 14274 if (fixp->fx_subsy)
252b5132 14275 {
02a86693 14276 if (fixp->fx_subsy == GOT_symbol)
23df1078 14277 {
02a86693
L
14278 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
14279 {
14280 if (!object_64bit)
14281 abort ();
14282#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14283 if (fixp->fx_tcbit2)
56ceb5b5
L
14284 fixp->fx_r_type = (fixp->fx_tcbit
14285 ? BFD_RELOC_X86_64_REX_GOTPCRELX
14286 : BFD_RELOC_X86_64_GOTPCRELX);
02a86693
L
14287 else
14288#endif
14289 fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
14290 }
d6ab8113 14291 else
02a86693
L
14292 {
14293 if (!object_64bit)
14294 fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
14295 else
14296 fixp->fx_r_type = BFD_RELOC_X86_64_GOTOFF64;
14297 }
14298 fixp->fx_subsy = 0;
23df1078 14299 }
252b5132 14300 }
02a86693 14301#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2585b7a5 14302 else
02a86693 14303 {
2585b7a5
L
14304 /* NB: Commit 292676c1 resolved PLT32 reloc aganst local symbol
14305 to section. Since PLT32 relocation must be against symbols,
14306 turn such PLT32 relocation into PC32 relocation. */
14307 if (fixp->fx_addsy
14308 && (fixp->fx_r_type == BFD_RELOC_386_PLT32
14309 || fixp->fx_r_type == BFD_RELOC_X86_64_PLT32)
14310 && symbol_section_p (fixp->fx_addsy))
14311 fixp->fx_r_type = BFD_RELOC_32_PCREL;
14312 if (!object_64bit)
14313 {
14314 if (fixp->fx_r_type == BFD_RELOC_386_GOT32
14315 && fixp->fx_tcbit2)
14316 fixp->fx_r_type = BFD_RELOC_386_GOT32X;
14317 }
02a86693
L
14318 }
14319#endif
3abbafc2
JB
14320
14321 return 1;
252b5132
RH
14322}
14323
252b5132 14324arelent *
7016a5d5 14325tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
14326{
14327 arelent *rel;
14328 bfd_reloc_code_real_type code;
14329
14330 switch (fixp->fx_r_type)
14331 {
8ce3d284 14332#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3abbafc2
JB
14333 symbolS *sym;
14334
8fd4256d
L
14335 case BFD_RELOC_SIZE32:
14336 case BFD_RELOC_SIZE64:
3abbafc2
JB
14337 if (fixp->fx_addsy
14338 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))
14339 && (!fixp->fx_subsy
14340 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))))
14341 sym = fixp->fx_addsy;
14342 else if (fixp->fx_subsy
14343 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))
14344 && (!fixp->fx_addsy
14345 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))))
14346 sym = fixp->fx_subsy;
14347 else
14348 sym = NULL;
14349 if (IS_ELF && sym && S_IS_DEFINED (sym) && !S_IS_EXTERNAL (sym))
8fd4256d
L
14350 {
14351 /* Resolve size relocation against local symbol to size of
14352 the symbol plus addend. */
3abbafc2 14353 valueT value = S_GET_SIZE (sym);
44f87162 14354
3abbafc2
JB
14355 if (symbol_get_bfdsym (sym)->flags & BSF_SECTION_SYM)
14356 value = bfd_section_size (S_GET_SEGMENT (sym));
14357 if (sym == fixp->fx_subsy)
14358 {
14359 value = -value;
14360 if (fixp->fx_addsy)
14361 value += S_GET_VALUE (fixp->fx_addsy);
14362 }
14363 else if (fixp->fx_subsy)
14364 value -= S_GET_VALUE (fixp->fx_subsy);
44f87162 14365 value += fixp->fx_offset;
8fd4256d 14366 if (fixp->fx_r_type == BFD_RELOC_SIZE32
d965814f 14367 && object_64bit
8fd4256d
L
14368 && !fits_in_unsigned_long (value))
14369 as_bad_where (fixp->fx_file, fixp->fx_line,
14370 _("symbol size computation overflow"));
14371 fixp->fx_addsy = NULL;
14372 fixp->fx_subsy = NULL;
14373 md_apply_fix (fixp, (valueT *) &value, NULL);
14374 return NULL;
14375 }
3abbafc2
JB
14376 if (!fixp->fx_addsy || fixp->fx_subsy)
14377 {
14378 as_bad_where (fixp->fx_file, fixp->fx_line,
14379 "unsupported expression involving @size");
14380 return NULL;
14381 }
8ce3d284 14382#endif
1a0670f3 14383 /* Fall through. */
8fd4256d 14384
3e73aa7c
JH
14385 case BFD_RELOC_X86_64_PLT32:
14386 case BFD_RELOC_X86_64_GOT32:
14387 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
14388 case BFD_RELOC_X86_64_GOTPCRELX:
14389 case BFD_RELOC_X86_64_REX_GOTPCRELX:
252b5132
RH
14390 case BFD_RELOC_386_PLT32:
14391 case BFD_RELOC_386_GOT32:
02a86693 14392 case BFD_RELOC_386_GOT32X:
252b5132
RH
14393 case BFD_RELOC_386_GOTOFF:
14394 case BFD_RELOC_386_GOTPC:
13ae64f3
JJ
14395 case BFD_RELOC_386_TLS_GD:
14396 case BFD_RELOC_386_TLS_LDM:
14397 case BFD_RELOC_386_TLS_LDO_32:
14398 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
14399 case BFD_RELOC_386_TLS_IE:
14400 case BFD_RELOC_386_TLS_GOTIE:
13ae64f3
JJ
14401 case BFD_RELOC_386_TLS_LE_32:
14402 case BFD_RELOC_386_TLS_LE:
67a4f2b7
AO
14403 case BFD_RELOC_386_TLS_GOTDESC:
14404 case BFD_RELOC_386_TLS_DESC_CALL:
bffbf940
JJ
14405 case BFD_RELOC_X86_64_TLSGD:
14406 case BFD_RELOC_X86_64_TLSLD:
14407 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 14408 case BFD_RELOC_X86_64_DTPOFF64:
bffbf940
JJ
14409 case BFD_RELOC_X86_64_GOTTPOFF:
14410 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113
JB
14411 case BFD_RELOC_X86_64_TPOFF64:
14412 case BFD_RELOC_X86_64_GOTOFF64:
14413 case BFD_RELOC_X86_64_GOTPC32:
7b81dfbb
AJ
14414 case BFD_RELOC_X86_64_GOT64:
14415 case BFD_RELOC_X86_64_GOTPCREL64:
14416 case BFD_RELOC_X86_64_GOTPC64:
14417 case BFD_RELOC_X86_64_GOTPLT64:
14418 case BFD_RELOC_X86_64_PLTOFF64:
67a4f2b7
AO
14419 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14420 case BFD_RELOC_X86_64_TLSDESC_CALL:
252b5132
RH
14421 case BFD_RELOC_RVA:
14422 case BFD_RELOC_VTABLE_ENTRY:
14423 case BFD_RELOC_VTABLE_INHERIT:
6482c264
NC
14424#ifdef TE_PE
14425 case BFD_RELOC_32_SECREL:
145667f8 14426 case BFD_RELOC_16_SECIDX:
6482c264 14427#endif
252b5132
RH
14428 code = fixp->fx_r_type;
14429 break;
dbbaec26
L
14430 case BFD_RELOC_X86_64_32S:
14431 if (!fixp->fx_pcrel)
14432 {
14433 /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32. */
14434 code = fixp->fx_r_type;
14435 break;
14436 }
1a0670f3 14437 /* Fall through. */
252b5132 14438 default:
93382f6d 14439 if (fixp->fx_pcrel)
252b5132 14440 {
93382f6d
AM
14441 switch (fixp->fx_size)
14442 {
14443 default:
b091f402
AM
14444 as_bad_where (fixp->fx_file, fixp->fx_line,
14445 _("can not do %d byte pc-relative relocation"),
14446 fixp->fx_size);
93382f6d
AM
14447 code = BFD_RELOC_32_PCREL;
14448 break;
14449 case 1: code = BFD_RELOC_8_PCREL; break;
14450 case 2: code = BFD_RELOC_16_PCREL; break;
d258b828 14451 case 4: code = BFD_RELOC_32_PCREL; break;
d6ab8113
JB
14452#ifdef BFD64
14453 case 8: code = BFD_RELOC_64_PCREL; break;
14454#endif
93382f6d
AM
14455 }
14456 }
14457 else
14458 {
14459 switch (fixp->fx_size)
14460 {
14461 default:
b091f402
AM
14462 as_bad_where (fixp->fx_file, fixp->fx_line,
14463 _("can not do %d byte relocation"),
14464 fixp->fx_size);
93382f6d
AM
14465 code = BFD_RELOC_32;
14466 break;
14467 case 1: code = BFD_RELOC_8; break;
14468 case 2: code = BFD_RELOC_16; break;
14469 case 4: code = BFD_RELOC_32; break;
937149dd 14470#ifdef BFD64
3e73aa7c 14471 case 8: code = BFD_RELOC_64; break;
937149dd 14472#endif
93382f6d 14473 }
252b5132
RH
14474 }
14475 break;
14476 }
252b5132 14477
d182319b
JB
14478 if ((code == BFD_RELOC_32
14479 || code == BFD_RELOC_32_PCREL
14480 || code == BFD_RELOC_X86_64_32S)
252b5132
RH
14481 && GOT_symbol
14482 && fixp->fx_addsy == GOT_symbol)
3e73aa7c 14483 {
4fa24527 14484 if (!object_64bit)
d6ab8113
JB
14485 code = BFD_RELOC_386_GOTPC;
14486 else
14487 code = BFD_RELOC_X86_64_GOTPC32;
3e73aa7c 14488 }
7b81dfbb
AJ
14489 if ((code == BFD_RELOC_64 || code == BFD_RELOC_64_PCREL)
14490 && GOT_symbol
14491 && fixp->fx_addsy == GOT_symbol)
14492 {
14493 code = BFD_RELOC_X86_64_GOTPC64;
14494 }
252b5132 14495
add39d23
TS
14496 rel = XNEW (arelent);
14497 rel->sym_ptr_ptr = XNEW (asymbol *);
49309057 14498 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
14499
14500 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
c87db184 14501
3e73aa7c
JH
14502 if (!use_rela_relocations)
14503 {
14504 /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
14505 vtable entry to be used in the relocation's section offset. */
14506 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
14507 rel->address = fixp->fx_offset;
fbeb56a4
DK
14508#if defined (OBJ_COFF) && defined (TE_PE)
14509 else if (fixp->fx_addsy && S_IS_WEAK (fixp->fx_addsy))
14510 rel->addend = fixp->fx_addnumber - (S_GET_VALUE (fixp->fx_addsy) * 2);
14511 else
14512#endif
c6682705 14513 rel->addend = 0;
3e73aa7c
JH
14514 }
14515 /* Use the rela in 64bit mode. */
252b5132 14516 else
3e73aa7c 14517 {
862be3fb
L
14518 if (disallow_64bit_reloc)
14519 switch (code)
14520 {
862be3fb
L
14521 case BFD_RELOC_X86_64_DTPOFF64:
14522 case BFD_RELOC_X86_64_TPOFF64:
14523 case BFD_RELOC_64_PCREL:
14524 case BFD_RELOC_X86_64_GOTOFF64:
14525 case BFD_RELOC_X86_64_GOT64:
14526 case BFD_RELOC_X86_64_GOTPCREL64:
14527 case BFD_RELOC_X86_64_GOTPC64:
14528 case BFD_RELOC_X86_64_GOTPLT64:
14529 case BFD_RELOC_X86_64_PLTOFF64:
14530 as_bad_where (fixp->fx_file, fixp->fx_line,
14531 _("cannot represent relocation type %s in x32 mode"),
14532 bfd_get_reloc_code_name (code));
14533 break;
14534 default:
14535 break;
14536 }
14537
062cd5e7
AS
14538 if (!fixp->fx_pcrel)
14539 rel->addend = fixp->fx_offset;
14540 else
14541 switch (code)
14542 {
14543 case BFD_RELOC_X86_64_PLT32:
14544 case BFD_RELOC_X86_64_GOT32:
14545 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
14546 case BFD_RELOC_X86_64_GOTPCRELX:
14547 case BFD_RELOC_X86_64_REX_GOTPCRELX:
bffbf940
JJ
14548 case BFD_RELOC_X86_64_TLSGD:
14549 case BFD_RELOC_X86_64_TLSLD:
14550 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7
AO
14551 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14552 case BFD_RELOC_X86_64_TLSDESC_CALL:
062cd5e7
AS
14553 rel->addend = fixp->fx_offset - fixp->fx_size;
14554 break;
14555 default:
14556 rel->addend = (section->vma
14557 - fixp->fx_size
14558 + fixp->fx_addnumber
14559 + md_pcrel_from (fixp));
14560 break;
14561 }
3e73aa7c
JH
14562 }
14563
252b5132
RH
14564 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
14565 if (rel->howto == NULL)
14566 {
14567 as_bad_where (fixp->fx_file, fixp->fx_line,
d0b47220 14568 _("cannot represent relocation type %s"),
252b5132
RH
14569 bfd_get_reloc_code_name (code));
14570 /* Set howto to a garbage value so that we can keep going. */
14571 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
9c2799c2 14572 gas_assert (rel->howto != NULL);
252b5132
RH
14573 }
14574
14575 return rel;
14576}
14577
ee86248c 14578#include "tc-i386-intel.c"
54cfded0 14579
a60de03c
JB
14580void
14581tc_x86_parse_to_dw2regnum (expressionS *exp)
54cfded0 14582{
a60de03c
JB
14583 int saved_naked_reg;
14584 char saved_register_dot;
54cfded0 14585
a60de03c
JB
14586 saved_naked_reg = allow_naked_reg;
14587 allow_naked_reg = 1;
14588 saved_register_dot = register_chars['.'];
14589 register_chars['.'] = '.';
14590 allow_pseudo_reg = 1;
14591 expression_and_evaluate (exp);
14592 allow_pseudo_reg = 0;
14593 register_chars['.'] = saved_register_dot;
14594 allow_naked_reg = saved_naked_reg;
14595
e96d56a1 14596 if (exp->X_op == O_register && exp->X_add_number >= 0)
54cfded0 14597 {
a60de03c
JB
14598 if ((addressT) exp->X_add_number < i386_regtab_size)
14599 {
14600 exp->X_op = O_constant;
14601 exp->X_add_number = i386_regtab[exp->X_add_number]
14602 .dw2_regnum[flag_code >> 1];
14603 }
14604 else
14605 exp->X_op = O_illegal;
54cfded0 14606 }
54cfded0
AM
14607}
14608
14609void
14610tc_x86_frame_initial_instructions (void)
14611{
a60de03c
JB
14612 static unsigned int sp_regno[2];
14613
14614 if (!sp_regno[flag_code >> 1])
14615 {
14616 char *saved_input = input_line_pointer;
14617 char sp[][4] = {"esp", "rsp"};
14618 expressionS exp;
a4447b93 14619
a60de03c
JB
14620 input_line_pointer = sp[flag_code >> 1];
14621 tc_x86_parse_to_dw2regnum (&exp);
9c2799c2 14622 gas_assert (exp.X_op == O_constant);
a60de03c
JB
14623 sp_regno[flag_code >> 1] = exp.X_add_number;
14624 input_line_pointer = saved_input;
14625 }
a4447b93 14626
61ff971f
L
14627 cfi_add_CFA_def_cfa (sp_regno[flag_code >> 1], -x86_cie_data_alignment);
14628 cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
54cfded0 14629}
d2b2c203 14630
d7921315
L
14631int
14632x86_dwarf2_addr_size (void)
14633{
14634#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
14635 if (x86_elf_abi == X86_64_X32_ABI)
14636 return 4;
14637#endif
14638 return bfd_arch_bits_per_address (stdoutput) / 8;
14639}
14640
d2b2c203
DJ
14641int
14642i386_elf_section_type (const char *str, size_t len)
14643{
14644 if (flag_code == CODE_64BIT
14645 && len == sizeof ("unwind") - 1
d34049e8 14646 && startswith (str, "unwind"))
d2b2c203
DJ
14647 return SHT_X86_64_UNWIND;
14648
14649 return -1;
14650}
bb41ade5 14651
ad5fec3b
EB
14652#ifdef TE_SOLARIS
14653void
14654i386_solaris_fix_up_eh_frame (segT sec)
14655{
14656 if (flag_code == CODE_64BIT)
14657 elf_section_type (sec) = SHT_X86_64_UNWIND;
14658}
14659#endif
14660
bb41ade5
AM
14661#ifdef TE_PE
14662void
14663tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
14664{
91d6fa6a 14665 expressionS exp;
bb41ade5 14666
91d6fa6a
NC
14667 exp.X_op = O_secrel;
14668 exp.X_add_symbol = symbol;
14669 exp.X_add_number = 0;
14670 emit_expr (&exp, size);
bb41ade5
AM
14671}
14672#endif
3b22753a
L
14673
14674#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14675/* For ELF on x86-64, add support for SHF_X86_64_LARGE. */
14676
01e1a5bc 14677bfd_vma
6d4af3c2 14678x86_64_section_letter (int letter, const char **ptr_msg)
3b22753a
L
14679{
14680 if (flag_code == CODE_64BIT)
14681 {
14682 if (letter == 'l')
14683 return SHF_X86_64_LARGE;
14684
8f3bae45 14685 *ptr_msg = _("bad .section directive: want a,l,w,x,M,S,G,T in string");
64e74474 14686 }
3b22753a 14687 else
8f3bae45 14688 *ptr_msg = _("bad .section directive: want a,w,x,M,S,G,T in string");
3b22753a
L
14689 return -1;
14690}
14691
01e1a5bc 14692bfd_vma
3b22753a
L
14693x86_64_section_word (char *str, size_t len)
14694{
08dedd66 14695 if (len == 5 && flag_code == CODE_64BIT && startswith (str, "large"))
3b22753a
L
14696 return SHF_X86_64_LARGE;
14697
14698 return -1;
14699}
14700
14701static void
14702handle_large_common (int small ATTRIBUTE_UNUSED)
14703{
14704 if (flag_code != CODE_64BIT)
14705 {
14706 s_comm_internal (0, elf_common_parse);
14707 as_warn (_(".largecomm supported only in 64bit mode, producing .comm"));
14708 }
14709 else
14710 {
14711 static segT lbss_section;
14712 asection *saved_com_section_ptr = elf_com_section_ptr;
14713 asection *saved_bss_section = bss_section;
14714
14715 if (lbss_section == NULL)
14716 {
14717 flagword applicable;
14718 segT seg = now_seg;
14719 subsegT subseg = now_subseg;
14720
14721 /* The .lbss section is for local .largecomm symbols. */
14722 lbss_section = subseg_new (".lbss", 0);
14723 applicable = bfd_applicable_section_flags (stdoutput);
fd361982 14724 bfd_set_section_flags (lbss_section, applicable & SEC_ALLOC);
3b22753a
L
14725 seg_info (lbss_section)->bss = 1;
14726
14727 subseg_set (seg, subseg);
14728 }
14729
14730 elf_com_section_ptr = &_bfd_elf_large_com_section;
14731 bss_section = lbss_section;
14732
14733 s_comm_internal (0, elf_common_parse);
14734
14735 elf_com_section_ptr = saved_com_section_ptr;
14736 bss_section = saved_bss_section;
14737 }
14738}
14739#endif /* OBJ_ELF || OBJ_MAYBE_ELF */