]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/config/tc-i386.c
x86: drop NoAVX from POPCNT
[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"
d2b2c203 33#include "elf/x86-64.h"
40fb9820 34#include "opcodes/i386-init.h"
41fd2579 35#include <limits.h>
41fd2579 36
c3332e24 37#ifndef INFER_ADDR_PREFIX
eecb386c 38#define INFER_ADDR_PREFIX 1
c3332e24
AM
39#endif
40
29b0f896
AM
41#ifndef DEFAULT_ARCH
42#define DEFAULT_ARCH "i386"
246fcdee 43#endif
252b5132 44
edde18a5
AM
45#ifndef INLINE
46#if __GNUC__ >= 2
47#define INLINE __inline__
48#else
49#define INLINE
50#endif
51#endif
52
6305a203
L
53/* Prefixes will be emitted in the order defined below.
54 WAIT_PREFIX must be the first prefix since FWAIT is really is an
55 instruction, and so must come before any prefixes.
56 The preferred prefix order is SEG_PREFIX, ADDR_PREFIX, DATA_PREFIX,
42164a71 57 REP_PREFIX/HLE_PREFIX, LOCK_PREFIX. */
6305a203
L
58#define WAIT_PREFIX 0
59#define SEG_PREFIX 1
60#define ADDR_PREFIX 2
61#define DATA_PREFIX 3
c32fa91d 62#define REP_PREFIX 4
42164a71 63#define HLE_PREFIX REP_PREFIX
7e8b059b 64#define BND_PREFIX REP_PREFIX
c32fa91d 65#define LOCK_PREFIX 5
4e9ac44a
L
66#define REX_PREFIX 6 /* must come last. */
67#define MAX_PREFIXES 7 /* max prefixes per opcode */
6305a203
L
68
69/* we define the syntax here (modulo base,index,scale syntax) */
70#define REGISTER_PREFIX '%'
71#define IMMEDIATE_PREFIX '$'
72#define ABSOLUTE_PREFIX '*'
73
74/* these are the instruction mnemonic suffixes in AT&T syntax or
75 memory operand size in Intel syntax. */
76#define WORD_MNEM_SUFFIX 'w'
77#define BYTE_MNEM_SUFFIX 'b'
78#define SHORT_MNEM_SUFFIX 's'
79#define LONG_MNEM_SUFFIX 'l'
80#define QWORD_MNEM_SUFFIX 'q'
6305a203
L
81/* Intel Syntax. Use a non-ascii letter since since it never appears
82 in instructions. */
83#define LONG_DOUBLE_MNEM_SUFFIX '\1'
84
85#define END_OF_INSN '\0'
86
79dec6b7
JB
87/* This matches the C -> StaticRounding alias in the opcode table. */
88#define commutative staticrounding
89
6305a203
L
90/*
91 'templates' is for grouping together 'template' structures for opcodes
92 of the same name. This is only used for storing the insns in the grand
93 ole hash table of insns.
94 The templates themselves start at START and range up to (but not including)
95 END.
96 */
97typedef struct
98{
d3ce72d0
NC
99 const insn_template *start;
100 const insn_template *end;
6305a203
L
101}
102templates;
103
104/* 386 operand encoding bytes: see 386 book for details of this. */
105typedef struct
106{
107 unsigned int regmem; /* codes register or memory operand */
108 unsigned int reg; /* codes register operand (or extended opcode) */
109 unsigned int mode; /* how to interpret regmem & reg */
110}
111modrm_byte;
112
113/* x86-64 extension prefix. */
114typedef int rex_byte;
115
6305a203
L
116/* 386 opcode byte to code indirect addressing. */
117typedef struct
118{
119 unsigned base;
120 unsigned index;
121 unsigned scale;
122}
123sib_byte;
124
6305a203
L
125/* x86 arch names, types and features */
126typedef struct
127{
128 const char *name; /* arch name */
8a2c8fef 129 unsigned int len; /* arch string length */
6305a203
L
130 enum processor_type type; /* arch type */
131 i386_cpu_flags flags; /* cpu feature flags */
8a2c8fef 132 unsigned int skip; /* show_arch should skip this. */
6305a203
L
133}
134arch_entry;
135
293f5f65
L
136/* Used to turn off indicated flags. */
137typedef struct
138{
139 const char *name; /* arch name */
140 unsigned int len; /* arch string length */
141 i386_cpu_flags flags; /* cpu feature flags */
142}
143noarch_entry;
144
78f12dd3 145static void update_code_flag (int, int);
e3bb37b5
L
146static void set_code_flag (int);
147static void set_16bit_gcc_code_flag (int);
148static void set_intel_syntax (int);
1efbbeb4 149static void set_intel_mnemonic (int);
db51cc60 150static void set_allow_index_reg (int);
7bab8ab5 151static void set_check (int);
e3bb37b5 152static void set_cpu_arch (int);
6482c264 153#ifdef TE_PE
e3bb37b5 154static void pe_directive_secrel (int);
6482c264 155#endif
e3bb37b5
L
156static void signed_cons (int);
157static char *output_invalid (int c);
ee86248c
JB
158static int i386_finalize_immediate (segT, expressionS *, i386_operand_type,
159 const char *);
160static int i386_finalize_displacement (segT, expressionS *, i386_operand_type,
161 const char *);
a7619375 162static int i386_att_operand (char *);
e3bb37b5 163static int i386_intel_operand (char *, int);
ee86248c
JB
164static int i386_intel_simplify (expressionS *);
165static int i386_intel_parse_name (const char *, expressionS *);
e3bb37b5
L
166static const reg_entry *parse_register (char *, char **);
167static char *parse_insn (char *, char *);
168static char *parse_operands (char *, const char *);
169static void swap_operands (void);
783c187b 170static void swap_2_operands (unsigned int, unsigned int);
48bcea9f 171static enum flag_code i386_addressing_mode (void);
e3bb37b5
L
172static void optimize_imm (void);
173static void optimize_disp (void);
83b16ac6 174static const insn_template *match_template (char);
e3bb37b5
L
175static int check_string (void);
176static int process_suffix (void);
177static int check_byte_reg (void);
178static int check_long_reg (void);
179static int check_qword_reg (void);
180static int check_word_reg (void);
181static int finalize_imm (void);
182static int process_operands (void);
5e042380 183static const reg_entry *build_modrm_byte (void);
e3bb37b5
L
184static void output_insn (void);
185static void output_imm (fragS *, offsetT);
186static void output_disp (fragS *, offsetT);
29b0f896 187#ifndef I386COFF
e3bb37b5 188static void s_bss (int);
252b5132 189#endif
17d4e2a2
L
190#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
191static void handle_large_common (int small ATTRIBUTE_UNUSED);
b4a3a7b4
L
192
193/* GNU_PROPERTY_X86_ISA_1_USED. */
194static unsigned int x86_isa_1_used;
195/* GNU_PROPERTY_X86_FEATURE_2_USED. */
196static unsigned int x86_feature_2_used;
197/* Generate x86 used ISA and feature properties. */
198static unsigned int x86_used_note = DEFAULT_X86_USED_NOTE;
17d4e2a2 199#endif
252b5132 200
a847613f 201static const char *default_arch = DEFAULT_ARCH;
3e73aa7c 202
8a6fb3f9
JB
203/* parse_register() returns this when a register alias cannot be used. */
204static const reg_entry bad_reg = { "<bad>", OPERAND_TYPE_NONE, 0, 0,
205 { Dw2Inval, Dw2Inval } };
206
34684862 207static const reg_entry *reg_eax;
5e042380
JB
208static const reg_entry *reg_ds;
209static const reg_entry *reg_es;
210static const reg_entry *reg_ss;
6288d05f 211static const reg_entry *reg_st0;
6225c532
JB
212static const reg_entry *reg_k0;
213
c0f3af97
L
214/* VEX prefix. */
215typedef struct
216{
43234a1e
L
217 /* VEX prefix is either 2 byte or 3 byte. EVEX is 4 byte. */
218 unsigned char bytes[4];
c0f3af97
L
219 unsigned int length;
220 /* Destination or source register specifier. */
221 const reg_entry *register_specifier;
222} vex_prefix;
223
252b5132 224/* 'md_assemble ()' gathers together information and puts it into a
47926f60 225 i386_insn. */
252b5132 226
520dc8e8
AM
227union i386_op
228 {
229 expressionS *disps;
230 expressionS *imms;
231 const reg_entry *regs;
232 };
233
a65babc9
L
234enum i386_error
235 {
86e026a4 236 operand_size_mismatch,
a65babc9
L
237 operand_type_mismatch,
238 register_type_mismatch,
239 number_of_operands_mismatch,
240 invalid_instruction_suffix,
241 bad_imm4,
a65babc9
L
242 unsupported_with_intel_mnemonic,
243 unsupported_syntax,
6c30d220 244 unsupported,
260cd341 245 invalid_sib_address,
6c30d220 246 invalid_vsib_address,
7bab8ab5 247 invalid_vector_register_set,
260cd341 248 invalid_tmm_register_set,
0cc78721 249 invalid_dest_and_src_register_set,
43234a1e
L
250 unsupported_vector_index_register,
251 unsupported_broadcast,
43234a1e
L
252 broadcast_needed,
253 unsupported_masking,
254 mask_not_on_destination,
255 no_default_mask,
256 unsupported_rc_sae,
257 rc_sae_operand_not_last_imm,
258 invalid_register_operand,
a65babc9
L
259 };
260
252b5132
RH
261struct _i386_insn
262 {
47926f60 263 /* TM holds the template for the insn were currently assembling. */
d3ce72d0 264 insn_template tm;
252b5132 265
7d5e4556
L
266 /* SUFFIX holds the instruction size suffix for byte, word, dword
267 or qword, if given. */
252b5132
RH
268 char suffix;
269
9a182d04
JB
270 /* OPCODE_LENGTH holds the number of base opcode bytes. */
271 unsigned char opcode_length;
272
47926f60 273 /* OPERANDS gives the number of given operands. */
252b5132
RH
274 unsigned int operands;
275
276 /* REG_OPERANDS, DISP_OPERANDS, MEM_OPERANDS, IMM_OPERANDS give the number
277 of given register, displacement, memory operands and immediate
47926f60 278 operands. */
252b5132
RH
279 unsigned int reg_operands, disp_operands, mem_operands, imm_operands;
280
281 /* TYPES [i] is the type (see above #defines) which tells us how to
520dc8e8 282 use OP[i] for the corresponding operand. */
40fb9820 283 i386_operand_type types[MAX_OPERANDS];
252b5132 284
520dc8e8
AM
285 /* Displacement expression, immediate expression, or register for each
286 operand. */
287 union i386_op op[MAX_OPERANDS];
252b5132 288
3e73aa7c
JH
289 /* Flags for operands. */
290 unsigned int flags[MAX_OPERANDS];
291#define Operand_PCrel 1
c48dadc9 292#define Operand_Mem 2
3e73aa7c 293
252b5132 294 /* Relocation type for operand */
f86103b7 295 enum bfd_reloc_code_real reloc[MAX_OPERANDS];
252b5132 296
252b5132
RH
297 /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
298 the base index byte below. */
299 const reg_entry *base_reg;
300 const reg_entry *index_reg;
301 unsigned int log2_scale_factor;
302
303 /* SEG gives the seg_entries of this insn. They are zero unless
47926f60 304 explicit segment overrides are given. */
5e042380 305 const reg_entry *seg[2];
252b5132 306
8325cc63
JB
307 /* Copied first memory operand string, for re-checking. */
308 char *memop1_string;
309
252b5132
RH
310 /* PREFIX holds all the given prefix opcodes (usually null).
311 PREFIXES is the number of prefix opcodes. */
312 unsigned int prefixes;
313 unsigned char prefix[MAX_PREFIXES];
314
50128d0c 315 /* Register is in low 3 bits of opcode. */
5b7c81bd 316 bool short_form;
50128d0c 317
6f2f06be 318 /* The operand to a branch insn indicates an absolute branch. */
5b7c81bd 319 bool jumpabsolute;
6f2f06be 320
921eafea
L
321 /* Extended states. */
322 enum
323 {
324 /* Use MMX state. */
325 xstate_mmx = 1 << 0,
326 /* Use XMM state. */
327 xstate_xmm = 1 << 1,
328 /* Use YMM state. */
329 xstate_ymm = 1 << 2 | xstate_xmm,
330 /* Use ZMM state. */
331 xstate_zmm = 1 << 3 | xstate_ymm,
332 /* Use TMM state. */
32930e4e
L
333 xstate_tmm = 1 << 4,
334 /* Use MASK state. */
335 xstate_mask = 1 << 5
921eafea 336 } xstate;
260cd341 337
e379e5f3 338 /* Has GOTPC or TLS relocation. */
5b7c81bd 339 bool has_gotpc_tls_reloc;
e379e5f3 340
252b5132 341 /* RM and SIB are the modrm byte and the sib byte where the
c1e679ec 342 addressing modes of this insn are encoded. */
252b5132 343 modrm_byte rm;
3e73aa7c 344 rex_byte rex;
43234a1e 345 rex_byte vrex;
252b5132 346 sib_byte sib;
c0f3af97 347 vex_prefix vex;
b6169b20 348
6225c532
JB
349 /* Masking attributes.
350
351 The struct describes masking, applied to OPERAND in the instruction.
352 REG is a pointer to the corresponding mask register. ZEROING tells
353 whether merging or zeroing mask is used. */
354 struct Mask_Operation
355 {
356 const reg_entry *reg;
357 unsigned int zeroing;
358 /* The operand where this operation is associated. */
359 unsigned int operand;
360 } mask;
43234a1e
L
361
362 /* Rounding control and SAE attributes. */
ca5312a2
JB
363 struct RC_Operation
364 {
365 enum rc_type
366 {
367 rc_none = -1,
368 rne,
369 rd,
370 ru,
371 rz,
372 saeonly
373 } type;
374
375 unsigned int operand;
376 } rounding;
43234a1e 377
5273a3cd
JB
378 /* Broadcasting attributes.
379
380 The struct describes broadcasting, applied to OPERAND. TYPE is
381 expresses the broadcast factor. */
382 struct Broadcast_Operation
383 {
0cc78721 384 /* Type of broadcast: {1to2}, {1to4}, {1to8}, {1to16} or {1to32}. */
5273a3cd
JB
385 unsigned int type;
386
387 /* Index of broadcasted operand. */
388 unsigned int operand;
389
390 /* Number of bytes to broadcast. */
391 unsigned int bytes;
392 } broadcast;
43234a1e
L
393
394 /* Compressed disp8*N attribute. */
395 unsigned int memshift;
396
86fa6981
L
397 /* Prefer load or store in encoding. */
398 enum
399 {
400 dir_encoding_default = 0,
401 dir_encoding_load,
64c49ab3
JB
402 dir_encoding_store,
403 dir_encoding_swap
86fa6981 404 } dir_encoding;
891edac4 405
41eb8e88 406 /* Prefer 8bit, 16bit, 32bit displacement in encoding. */
a501d77e
L
407 enum
408 {
409 disp_encoding_default = 0,
410 disp_encoding_8bit,
41eb8e88 411 disp_encoding_16bit,
a501d77e
L
412 disp_encoding_32bit
413 } disp_encoding;
f8a5c266 414
6b6b6807 415 /* Prefer the REX byte in encoding. */
5b7c81bd 416 bool rex_encoding;
6b6b6807 417
b6f8c7c4 418 /* Disable instruction size optimization. */
5b7c81bd 419 bool no_optimize;
b6f8c7c4 420
86fa6981
L
421 /* How to encode vector instructions. */
422 enum
423 {
424 vex_encoding_default = 0,
42e04b36 425 vex_encoding_vex,
86fa6981 426 vex_encoding_vex3,
da4977e0
JB
427 vex_encoding_evex,
428 vex_encoding_error
86fa6981
L
429 } vec_encoding;
430
d5de92cf
L
431 /* REP prefix. */
432 const char *rep_prefix;
433
165de32a
L
434 /* HLE prefix. */
435 const char *hle_prefix;
42164a71 436
7e8b059b
L
437 /* Have BND prefix. */
438 const char *bnd_prefix;
439
04ef582a
L
440 /* Have NOTRACK prefix. */
441 const char *notrack_prefix;
442
891edac4 443 /* Error message. */
a65babc9 444 enum i386_error error;
252b5132
RH
445 };
446
447typedef struct _i386_insn i386_insn;
448
43234a1e
L
449/* Link RC type with corresponding string, that'll be looked for in
450 asm. */
451struct RC_name
452{
453 enum rc_type type;
454 const char *name;
455 unsigned int len;
456};
457
458static const struct RC_name RC_NamesTable[] =
459{
460 { rne, STRING_COMMA_LEN ("rn-sae") },
461 { rd, STRING_COMMA_LEN ("rd-sae") },
462 { ru, STRING_COMMA_LEN ("ru-sae") },
463 { rz, STRING_COMMA_LEN ("rz-sae") },
464 { saeonly, STRING_COMMA_LEN ("sae") },
465};
466
252b5132
RH
467/* List of chars besides those in app.c:symbol_chars that can start an
468 operand. Used to prevent the scrubber eating vital white-space. */
86fa6981 469const char extra_symbol_chars[] = "*%-([{}"
252b5132 470#ifdef LEX_AT
32137342
NC
471 "@"
472#endif
473#ifdef LEX_QM
474 "?"
252b5132 475#endif
32137342 476 ;
252b5132 477
b3983e5f
JB
478#if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
479 && !defined (TE_GNU) \
480 && !defined (TE_LINUX) \
d85e70a3 481 && !defined (TE_Haiku) \
b3983e5f
JB
482 && !defined (TE_FreeBSD) \
483 && !defined (TE_DragonFly) \
484 && !defined (TE_NetBSD))
252b5132 485/* This array holds the chars that always start a comment. If the
b3b91714
AM
486 pre-processor is disabled, these aren't very useful. The option
487 --divide will remove '/' from this list. */
488const char *i386_comment_chars = "#/";
489#define SVR4_COMMENT_CHARS 1
252b5132 490#define PREFIX_SEPARATOR '\\'
252b5132 491
b3b91714
AM
492#else
493const char *i386_comment_chars = "#";
494#define PREFIX_SEPARATOR '/'
495#endif
496
252b5132
RH
497/* This array holds the chars that only start a comment at the beginning of
498 a line. If the line seems to have the form '# 123 filename'
ce8a8b2f
AM
499 .line and .file directives will appear in the pre-processed output.
500 Note that input_file.c hand checks for '#' at the beginning of the
252b5132 501 first line of the input file. This is because the compiler outputs
ce8a8b2f
AM
502 #NO_APP at the beginning of its output.
503 Also note that comments started like this one will always work if
252b5132 504 '/' isn't otherwise defined. */
b3b91714 505const char line_comment_chars[] = "#/";
252b5132 506
63a0b638 507const char line_separator_chars[] = ";";
252b5132 508
ce8a8b2f
AM
509/* Chars that can be used to separate mant from exp in floating point
510 nums. */
252b5132
RH
511const char EXP_CHARS[] = "eE";
512
ce8a8b2f
AM
513/* Chars that mean this number is a floating point constant
514 As in 0f12.456
515 or 0d1.2345e12. */
de133cf9 516const char FLT_CHARS[] = "fFdDxXhHbB";
252b5132 517
ce8a8b2f 518/* Tables for lexical analysis. */
252b5132
RH
519static char mnemonic_chars[256];
520static char register_chars[256];
521static char operand_chars[256];
522static char identifier_chars[256];
252b5132 523
ce8a8b2f 524/* Lexical macros. */
252b5132
RH
525#define is_mnemonic_char(x) (mnemonic_chars[(unsigned char) x])
526#define is_operand_char(x) (operand_chars[(unsigned char) x])
527#define is_register_char(x) (register_chars[(unsigned char) x])
528#define is_space_char(x) ((x) == ' ')
529#define is_identifier_char(x) (identifier_chars[(unsigned char) x])
252b5132 530
0234cb7c 531/* All non-digit non-letter characters that may occur in an operand. */
252b5132
RH
532static char operand_special_chars[] = "%$-+(,)*._~/<>|&^!:[@]";
533
534/* md_assemble() always leaves the strings it's passed unaltered. To
535 effect this we maintain a stack of saved characters that we've smashed
536 with '\0's (indicating end of strings for various sub-fields of the
47926f60 537 assembler instruction). */
252b5132 538static char save_stack[32];
ce8a8b2f 539static char *save_stack_p;
252b5132
RH
540#define END_STRING_AND_SAVE(s) \
541 do { *save_stack_p++ = *(s); *(s) = '\0'; } while (0)
542#define RESTORE_END_STRING(s) \
543 do { *(s) = *--save_stack_p; } while (0)
544
47926f60 545/* The instruction we're assembling. */
252b5132
RH
546static i386_insn i;
547
548/* Possible templates for current insn. */
549static const templates *current_templates;
550
31b2323c
L
551/* Per instruction expressionS buffers: max displacements & immediates. */
552static expressionS disp_expressions[MAX_MEMORY_OPERANDS];
553static expressionS im_expressions[MAX_IMMEDIATE_OPERANDS];
252b5132 554
47926f60 555/* Current operand we are working on. */
ee86248c 556static int this_operand = -1;
252b5132 557
3e73aa7c
JH
558/* We support four different modes. FLAG_CODE variable is used to distinguish
559 these. */
560
561enum flag_code {
562 CODE_32BIT,
563 CODE_16BIT,
564 CODE_64BIT };
565
566static enum flag_code flag_code;
4fa24527 567static unsigned int object_64bit;
862be3fb 568static unsigned int disallow_64bit_reloc;
3e73aa7c 569static int use_rela_relocations = 0;
e379e5f3
L
570/* __tls_get_addr/___tls_get_addr symbol for TLS. */
571static const char *tls_get_addr;
3e73aa7c 572
7af8ed2d
NC
573#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
574 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
575 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
576
351f65ca
L
577/* The ELF ABI to use. */
578enum x86_elf_abi
579{
580 I386_ABI,
7f56bc95
L
581 X86_64_ABI,
582 X86_64_X32_ABI
351f65ca
L
583};
584
585static enum x86_elf_abi x86_elf_abi = I386_ABI;
7af8ed2d 586#endif
351f65ca 587
167ad85b
TG
588#if defined (TE_PE) || defined (TE_PEP)
589/* Use big object file format. */
590static int use_big_obj = 0;
591#endif
592
8dcea932
L
593#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
594/* 1 if generating code for a shared library. */
595static int shared = 0;
596#endif
597
47926f60
KH
598/* 1 for intel syntax,
599 0 if att syntax. */
600static int intel_syntax = 0;
252b5132 601
4b5aaf5f
L
602static enum x86_64_isa
603{
604 amd64 = 1, /* AMD64 ISA. */
605 intel64 /* Intel64 ISA. */
606} isa64;
e89c5eaa 607
1efbbeb4
L
608/* 1 for intel mnemonic,
609 0 if att mnemonic. */
610static int intel_mnemonic = !SYSV386_COMPAT;
611
a60de03c
JB
612/* 1 if pseudo registers are permitted. */
613static int allow_pseudo_reg = 0;
614
47926f60
KH
615/* 1 if register prefix % not required. */
616static int allow_naked_reg = 0;
252b5132 617
33eaf5de 618/* 1 if the assembler should add BND prefix for all control-transferring
7e8b059b
L
619 instructions supporting it, even if this prefix wasn't specified
620 explicitly. */
621static int add_bnd_prefix = 0;
622
ba104c83 623/* 1 if pseudo index register, eiz/riz, is allowed . */
db51cc60
L
624static int allow_index_reg = 0;
625
d022bddd
IT
626/* 1 if the assembler should ignore LOCK prefix, even if it was
627 specified explicitly. */
628static int omit_lock_prefix = 0;
629
e4e00185
AS
630/* 1 if the assembler should encode lfence, mfence, and sfence as
631 "lock addl $0, (%{re}sp)". */
632static int avoid_fence = 0;
633
ae531041
L
634/* 1 if lfence should be inserted after every load. */
635static int lfence_after_load = 0;
636
637/* Non-zero if lfence should be inserted before indirect branch. */
638static enum lfence_before_indirect_branch_kind
639 {
640 lfence_branch_none = 0,
641 lfence_branch_register,
642 lfence_branch_memory,
643 lfence_branch_all
644 }
645lfence_before_indirect_branch;
646
647/* Non-zero if lfence should be inserted before ret. */
648static enum lfence_before_ret_kind
649 {
650 lfence_before_ret_none = 0,
651 lfence_before_ret_not,
a09f656b 652 lfence_before_ret_or,
653 lfence_before_ret_shl
ae531041
L
654 }
655lfence_before_ret;
656
657/* Types of previous instruction is .byte or prefix. */
e379e5f3
L
658static struct
659 {
660 segT seg;
661 const char *file;
662 const char *name;
663 unsigned int line;
664 enum last_insn_kind
665 {
666 last_insn_other = 0,
667 last_insn_directive,
668 last_insn_prefix
669 } kind;
670 } last_insn;
671
0cb4071e
L
672/* 1 if the assembler should generate relax relocations. */
673
674static int generate_relax_relocations
675 = DEFAULT_GENERATE_X86_RELAX_RELOCATIONS;
676
7bab8ab5 677static enum check_kind
daf50ae7 678 {
7bab8ab5
JB
679 check_none = 0,
680 check_warning,
681 check_error
daf50ae7 682 }
7bab8ab5 683sse_check, operand_check = check_warning;
daf50ae7 684
e379e5f3
L
685/* Non-zero if branches should be aligned within power of 2 boundary. */
686static int align_branch_power = 0;
687
688/* Types of branches to align. */
689enum align_branch_kind
690 {
691 align_branch_none = 0,
692 align_branch_jcc = 1,
693 align_branch_fused = 2,
694 align_branch_jmp = 3,
695 align_branch_call = 4,
696 align_branch_indirect = 5,
697 align_branch_ret = 6
698 };
699
700/* Type bits of branches to align. */
701enum align_branch_bit
702 {
703 align_branch_jcc_bit = 1 << align_branch_jcc,
704 align_branch_fused_bit = 1 << align_branch_fused,
705 align_branch_jmp_bit = 1 << align_branch_jmp,
706 align_branch_call_bit = 1 << align_branch_call,
707 align_branch_indirect_bit = 1 << align_branch_indirect,
708 align_branch_ret_bit = 1 << align_branch_ret
709 };
710
711static unsigned int align_branch = (align_branch_jcc_bit
712 | align_branch_fused_bit
713 | align_branch_jmp_bit);
714
79d72f45
HL
715/* Types of condition jump used by macro-fusion. */
716enum mf_jcc_kind
717 {
718 mf_jcc_jo = 0, /* base opcode 0x70 */
719 mf_jcc_jc, /* base opcode 0x72 */
720 mf_jcc_je, /* base opcode 0x74 */
721 mf_jcc_jna, /* base opcode 0x76 */
722 mf_jcc_js, /* base opcode 0x78 */
723 mf_jcc_jp, /* base opcode 0x7a */
724 mf_jcc_jl, /* base opcode 0x7c */
725 mf_jcc_jle, /* base opcode 0x7e */
726 };
727
728/* Types of compare flag-modifying insntructions used by macro-fusion. */
729enum mf_cmp_kind
730 {
731 mf_cmp_test_and, /* test/cmp */
732 mf_cmp_alu_cmp, /* add/sub/cmp */
733 mf_cmp_incdec /* inc/dec */
734 };
735
e379e5f3
L
736/* The maximum padding size for fused jcc. CMP like instruction can
737 be 9 bytes and jcc can be 6 bytes. Leave room just in case for
738 prefixes. */
739#define MAX_FUSED_JCC_PADDING_SIZE 20
740
741/* The maximum number of prefixes added for an instruction. */
742static unsigned int align_branch_prefix_size = 5;
743
b6f8c7c4
L
744/* Optimization:
745 1. Clear the REX_W bit with register operand if possible.
746 2. Above plus use 128bit vector instruction to clear the full vector
747 register.
748 */
749static int optimize = 0;
750
751/* Optimization:
752 1. Clear the REX_W bit with register operand if possible.
753 2. Above plus use 128bit vector instruction to clear the full vector
754 register.
755 3. Above plus optimize "test{q,l,w} $imm8,%r{64,32,16}" to
756 "testb $imm7,%r8".
757 */
758static int optimize_for_space = 0;
759
2ca3ace5
L
760/* Register prefix used for error message. */
761static const char *register_prefix = "%";
762
47926f60
KH
763/* Used in 16 bit gcc mode to add an l suffix to call, ret, enter,
764 leave, push, and pop instructions so that gcc has the same stack
765 frame as in 32 bit mode. */
766static char stackop_size = '\0';
eecb386c 767
12b55ccc
L
768/* Non-zero to optimize code alignment. */
769int optimize_align_code = 1;
770
47926f60
KH
771/* Non-zero to quieten some warnings. */
772static int quiet_warnings = 0;
a38cf1db 773
47926f60
KH
774/* CPU name. */
775static const char *cpu_arch_name = NULL;
6305a203 776static char *cpu_sub_arch_name = NULL;
a38cf1db 777
47926f60 778/* CPU feature flags. */
40fb9820
L
779static i386_cpu_flags cpu_arch_flags = CPU_UNKNOWN_FLAGS;
780
ccc9c027
L
781/* If we have selected a cpu we are generating instructions for. */
782static int cpu_arch_tune_set = 0;
783
9103f4f4 784/* Cpu we are generating instructions for. */
fbf3f584 785enum processor_type cpu_arch_tune = PROCESSOR_UNKNOWN;
9103f4f4
L
786
787/* CPU feature flags of cpu we are generating instructions for. */
40fb9820 788static i386_cpu_flags cpu_arch_tune_flags;
9103f4f4 789
ccc9c027 790/* CPU instruction set architecture used. */
fbf3f584 791enum processor_type cpu_arch_isa = PROCESSOR_UNKNOWN;
ccc9c027 792
9103f4f4 793/* CPU feature flags of instruction set architecture used. */
fbf3f584 794i386_cpu_flags cpu_arch_isa_flags;
9103f4f4 795
fddf5b5b
AM
796/* If set, conditional jumps are not automatically promoted to handle
797 larger than a byte offset. */
798static unsigned int no_cond_jump_promotion = 0;
799
c0f3af97
L
800/* Encode SSE instructions with VEX prefix. */
801static unsigned int sse2avx;
802
c8480b58
L
803/* Encode aligned vector move as unaligned vector move. */
804static unsigned int use_unaligned_vector_move;
805
539f890d
L
806/* Encode scalar AVX instructions with specific vector length. */
807static enum
808 {
809 vex128 = 0,
810 vex256
811 } avxscalar;
812
03751133
L
813/* Encode VEX WIG instructions with specific vex.w. */
814static enum
815 {
816 vexw0 = 0,
817 vexw1
818 } vexwig;
819
43234a1e
L
820/* Encode scalar EVEX LIG instructions with specific vector length. */
821static enum
822 {
823 evexl128 = 0,
824 evexl256,
825 evexl512
826 } evexlig;
827
828/* Encode EVEX WIG instructions with specific evex.w. */
829static enum
830 {
831 evexw0 = 0,
832 evexw1
833 } evexwig;
834
d3d3c6db
IT
835/* Value to encode in EVEX RC bits, for SAE-only instructions. */
836static enum rc_type evexrcig = rne;
837
29b0f896 838/* Pre-defined "_GLOBAL_OFFSET_TABLE_". */
87c245cc 839static symbolS *GOT_symbol;
29b0f896 840
a4447b93
RH
841/* The dwarf2 return column, adjusted for 32 or 64 bit. */
842unsigned int x86_dwarf2_return_column;
843
844/* The dwarf2 data alignment, adjusted for 32 or 64 bit. */
845int x86_cie_data_alignment;
846
252b5132 847/* Interface to relax_segment.
fddf5b5b
AM
848 There are 3 major relax states for 386 jump insns because the
849 different types of jumps add different sizes to frags when we're
e379e5f3
L
850 figuring out what sort of jump to choose to reach a given label.
851
852 BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING are used to align
853 branches which are handled by md_estimate_size_before_relax() and
854 i386_generic_table_relax_frag(). */
252b5132 855
47926f60 856/* Types. */
93c2a809
AM
857#define UNCOND_JUMP 0
858#define COND_JUMP 1
859#define COND_JUMP86 2
e379e5f3
L
860#define BRANCH_PADDING 3
861#define BRANCH_PREFIX 4
862#define FUSED_JCC_PADDING 5
fddf5b5b 863
47926f60 864/* Sizes. */
252b5132
RH
865#define CODE16 1
866#define SMALL 0
29b0f896 867#define SMALL16 (SMALL | CODE16)
252b5132 868#define BIG 2
29b0f896 869#define BIG16 (BIG | CODE16)
252b5132
RH
870
871#ifndef INLINE
872#ifdef __GNUC__
873#define INLINE __inline__
874#else
875#define INLINE
876#endif
877#endif
878
fddf5b5b
AM
879#define ENCODE_RELAX_STATE(type, size) \
880 ((relax_substateT) (((type) << 2) | (size)))
881#define TYPE_FROM_RELAX_STATE(s) \
882 ((s) >> 2)
883#define DISP_SIZE_FROM_RELAX_STATE(s) \
884 ((((s) & 3) == BIG ? 4 : (((s) & 3) == BIG16 ? 2 : 1)))
252b5132
RH
885
886/* This table is used by relax_frag to promote short jumps to long
887 ones where necessary. SMALL (short) jumps may be promoted to BIG
888 (32 bit long) ones, and SMALL16 jumps to BIG16 (16 bit long). We
889 don't allow a short jump in a 32 bit code segment to be promoted to
890 a 16 bit offset jump because it's slower (requires data size
891 prefix), and doesn't work, unless the destination is in the bottom
892 64k of the code segment (The top 16 bits of eip are zeroed). */
893
894const relax_typeS md_relax_table[] =
895{
24eab124
AM
896 /* The fields are:
897 1) most positive reach of this state,
898 2) most negative reach of this state,
93c2a809 899 3) how many bytes this mode will have in the variable part of the frag
ce8a8b2f 900 4) which index into the table to try if we can't fit into this one. */
252b5132 901
fddf5b5b 902 /* UNCOND_JUMP states. */
93c2a809
AM
903 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG)},
904 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16)},
905 /* dword jmp adds 4 bytes to frag:
906 0 extra opcode bytes, 4 displacement bytes. */
252b5132 907 {0, 0, 4, 0},
93c2a809
AM
908 /* word jmp adds 2 byte2 to frag:
909 0 extra opcode bytes, 2 displacement bytes. */
252b5132
RH
910 {0, 0, 2, 0},
911
93c2a809
AM
912 /* COND_JUMP states. */
913 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG)},
914 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG16)},
915 /* dword conditionals adds 5 bytes to frag:
916 1 extra opcode byte, 4 displacement bytes. */
917 {0, 0, 5, 0},
fddf5b5b 918 /* word conditionals add 3 bytes to frag:
93c2a809
AM
919 1 extra opcode byte, 2 displacement bytes. */
920 {0, 0, 3, 0},
921
922 /* COND_JUMP86 states. */
923 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG)},
924 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG16)},
925 /* dword conditionals adds 5 bytes to frag:
926 1 extra opcode byte, 4 displacement bytes. */
927 {0, 0, 5, 0},
928 /* word conditionals add 4 bytes to frag:
929 1 displacement byte and a 3 byte long branch insn. */
930 {0, 0, 4, 0}
252b5132
RH
931};
932
9103f4f4
L
933static const arch_entry cpu_arch[] =
934{
89507696
JB
935 /* Do not replace the first two entries - i386_target_format()
936 relies on them being there in this order. */
8a2c8fef 937 { STRING_COMMA_LEN ("generic32"), PROCESSOR_GENERIC32,
293f5f65 938 CPU_GENERIC32_FLAGS, 0 },
8a2c8fef 939 { STRING_COMMA_LEN ("generic64"), PROCESSOR_GENERIC64,
293f5f65 940 CPU_GENERIC64_FLAGS, 0 },
8a2c8fef 941 { STRING_COMMA_LEN ("i8086"), PROCESSOR_UNKNOWN,
293f5f65 942 CPU_NONE_FLAGS, 0 },
8a2c8fef 943 { STRING_COMMA_LEN ("i186"), PROCESSOR_UNKNOWN,
293f5f65 944 CPU_I186_FLAGS, 0 },
8a2c8fef 945 { STRING_COMMA_LEN ("i286"), PROCESSOR_UNKNOWN,
293f5f65 946 CPU_I286_FLAGS, 0 },
8a2c8fef 947 { STRING_COMMA_LEN ("i386"), PROCESSOR_I386,
293f5f65 948 CPU_I386_FLAGS, 0 },
8a2c8fef 949 { STRING_COMMA_LEN ("i486"), PROCESSOR_I486,
293f5f65 950 CPU_I486_FLAGS, 0 },
8a2c8fef 951 { STRING_COMMA_LEN ("i586"), PROCESSOR_PENTIUM,
293f5f65 952 CPU_I586_FLAGS, 0 },
8a2c8fef 953 { STRING_COMMA_LEN ("i686"), PROCESSOR_PENTIUMPRO,
293f5f65 954 CPU_I686_FLAGS, 0 },
8a2c8fef 955 { STRING_COMMA_LEN ("pentium"), PROCESSOR_PENTIUM,
293f5f65 956 CPU_I586_FLAGS, 0 },
8a2c8fef 957 { STRING_COMMA_LEN ("pentiumpro"), PROCESSOR_PENTIUMPRO,
293f5f65 958 CPU_PENTIUMPRO_FLAGS, 0 },
8a2c8fef 959 { STRING_COMMA_LEN ("pentiumii"), PROCESSOR_PENTIUMPRO,
293f5f65 960 CPU_P2_FLAGS, 0 },
8a2c8fef 961 { STRING_COMMA_LEN ("pentiumiii"),PROCESSOR_PENTIUMPRO,
293f5f65 962 CPU_P3_FLAGS, 0 },
8a2c8fef 963 { STRING_COMMA_LEN ("pentium4"), PROCESSOR_PENTIUM4,
293f5f65 964 CPU_P4_FLAGS, 0 },
8a2c8fef 965 { STRING_COMMA_LEN ("prescott"), PROCESSOR_NOCONA,
293f5f65 966 CPU_CORE_FLAGS, 0 },
8a2c8fef 967 { STRING_COMMA_LEN ("nocona"), PROCESSOR_NOCONA,
293f5f65 968 CPU_NOCONA_FLAGS, 0 },
8a2c8fef 969 { STRING_COMMA_LEN ("yonah"), PROCESSOR_CORE,
293f5f65 970 CPU_CORE_FLAGS, 1 },
8a2c8fef 971 { STRING_COMMA_LEN ("core"), PROCESSOR_CORE,
293f5f65 972 CPU_CORE_FLAGS, 0 },
8a2c8fef 973 { STRING_COMMA_LEN ("merom"), PROCESSOR_CORE2,
293f5f65 974 CPU_CORE2_FLAGS, 1 },
8a2c8fef 975 { STRING_COMMA_LEN ("core2"), PROCESSOR_CORE2,
293f5f65 976 CPU_CORE2_FLAGS, 0 },
8a2c8fef 977 { STRING_COMMA_LEN ("corei7"), PROCESSOR_COREI7,
293f5f65 978 CPU_COREI7_FLAGS, 0 },
8a2c8fef 979 { STRING_COMMA_LEN ("l1om"), PROCESSOR_L1OM,
293f5f65 980 CPU_L1OM_FLAGS, 0 },
7a9068fe 981 { STRING_COMMA_LEN ("k1om"), PROCESSOR_K1OM,
293f5f65 982 CPU_K1OM_FLAGS, 0 },
81486035 983 { STRING_COMMA_LEN ("iamcu"), PROCESSOR_IAMCU,
293f5f65 984 CPU_IAMCU_FLAGS, 0 },
8a2c8fef 985 { STRING_COMMA_LEN ("k6"), PROCESSOR_K6,
293f5f65 986 CPU_K6_FLAGS, 0 },
8a2c8fef 987 { STRING_COMMA_LEN ("k6_2"), PROCESSOR_K6,
293f5f65 988 CPU_K6_2_FLAGS, 0 },
8a2c8fef 989 { STRING_COMMA_LEN ("athlon"), PROCESSOR_ATHLON,
293f5f65 990 CPU_ATHLON_FLAGS, 0 },
8a2c8fef 991 { STRING_COMMA_LEN ("sledgehammer"), PROCESSOR_K8,
293f5f65 992 CPU_K8_FLAGS, 1 },
8a2c8fef 993 { STRING_COMMA_LEN ("opteron"), PROCESSOR_K8,
293f5f65 994 CPU_K8_FLAGS, 0 },
8a2c8fef 995 { STRING_COMMA_LEN ("k8"), PROCESSOR_K8,
293f5f65 996 CPU_K8_FLAGS, 0 },
8a2c8fef 997 { STRING_COMMA_LEN ("amdfam10"), PROCESSOR_AMDFAM10,
293f5f65 998 CPU_AMDFAM10_FLAGS, 0 },
8aedb9fe 999 { STRING_COMMA_LEN ("bdver1"), PROCESSOR_BD,
293f5f65 1000 CPU_BDVER1_FLAGS, 0 },
8aedb9fe 1001 { STRING_COMMA_LEN ("bdver2"), PROCESSOR_BD,
293f5f65 1002 CPU_BDVER2_FLAGS, 0 },
5e5c50d3 1003 { STRING_COMMA_LEN ("bdver3"), PROCESSOR_BD,
293f5f65 1004 CPU_BDVER3_FLAGS, 0 },
c7b0bd56 1005 { STRING_COMMA_LEN ("bdver4"), PROCESSOR_BD,
293f5f65 1006 CPU_BDVER4_FLAGS, 0 },
029f3522 1007 { STRING_COMMA_LEN ("znver1"), PROCESSOR_ZNVER,
293f5f65 1008 CPU_ZNVER1_FLAGS, 0 },
a9660a6f
AP
1009 { STRING_COMMA_LEN ("znver2"), PROCESSOR_ZNVER,
1010 CPU_ZNVER2_FLAGS, 0 },
646cc3e0
GG
1011 { STRING_COMMA_LEN ("znver3"), PROCESSOR_ZNVER,
1012 CPU_ZNVER3_FLAGS, 0 },
7b458c12 1013 { STRING_COMMA_LEN ("btver1"), PROCESSOR_BT,
293f5f65 1014 CPU_BTVER1_FLAGS, 0 },
7b458c12 1015 { STRING_COMMA_LEN ("btver2"), PROCESSOR_BT,
293f5f65 1016 CPU_BTVER2_FLAGS, 0 },
8a2c8fef 1017 { STRING_COMMA_LEN (".8087"), PROCESSOR_UNKNOWN,
293f5f65 1018 CPU_8087_FLAGS, 0 },
8a2c8fef 1019 { STRING_COMMA_LEN (".287"), PROCESSOR_UNKNOWN,
293f5f65 1020 CPU_287_FLAGS, 0 },
8a2c8fef 1021 { STRING_COMMA_LEN (".387"), PROCESSOR_UNKNOWN,
293f5f65 1022 CPU_387_FLAGS, 0 },
1848e567
L
1023 { STRING_COMMA_LEN (".687"), PROCESSOR_UNKNOWN,
1024 CPU_687_FLAGS, 0 },
d871f3f4
L
1025 { STRING_COMMA_LEN (".cmov"), PROCESSOR_UNKNOWN,
1026 CPU_CMOV_FLAGS, 0 },
1027 { STRING_COMMA_LEN (".fxsr"), PROCESSOR_UNKNOWN,
1028 CPU_FXSR_FLAGS, 0 },
8a2c8fef 1029 { STRING_COMMA_LEN (".mmx"), PROCESSOR_UNKNOWN,
293f5f65 1030 CPU_MMX_FLAGS, 0 },
8a2c8fef 1031 { STRING_COMMA_LEN (".sse"), PROCESSOR_UNKNOWN,
293f5f65 1032 CPU_SSE_FLAGS, 0 },
8a2c8fef 1033 { STRING_COMMA_LEN (".sse2"), PROCESSOR_UNKNOWN,
293f5f65 1034 CPU_SSE2_FLAGS, 0 },
8a2c8fef 1035 { STRING_COMMA_LEN (".sse3"), PROCESSOR_UNKNOWN,
293f5f65 1036 CPU_SSE3_FLAGS, 0 },
af5c13b0
L
1037 { STRING_COMMA_LEN (".sse4a"), PROCESSOR_UNKNOWN,
1038 CPU_SSE4A_FLAGS, 0 },
8a2c8fef 1039 { STRING_COMMA_LEN (".ssse3"), PROCESSOR_UNKNOWN,
293f5f65 1040 CPU_SSSE3_FLAGS, 0 },
8a2c8fef 1041 { STRING_COMMA_LEN (".sse4.1"), PROCESSOR_UNKNOWN,
293f5f65 1042 CPU_SSE4_1_FLAGS, 0 },
8a2c8fef 1043 { STRING_COMMA_LEN (".sse4.2"), PROCESSOR_UNKNOWN,
293f5f65 1044 CPU_SSE4_2_FLAGS, 0 },
8a2c8fef 1045 { STRING_COMMA_LEN (".sse4"), PROCESSOR_UNKNOWN,
293f5f65 1046 CPU_SSE4_2_FLAGS, 0 },
8a2c8fef 1047 { STRING_COMMA_LEN (".avx"), PROCESSOR_UNKNOWN,
293f5f65 1048 CPU_AVX_FLAGS, 0 },
6c30d220 1049 { STRING_COMMA_LEN (".avx2"), PROCESSOR_UNKNOWN,
293f5f65 1050 CPU_AVX2_FLAGS, 0 },
43234a1e 1051 { STRING_COMMA_LEN (".avx512f"), PROCESSOR_UNKNOWN,
293f5f65 1052 CPU_AVX512F_FLAGS, 0 },
43234a1e 1053 { STRING_COMMA_LEN (".avx512cd"), PROCESSOR_UNKNOWN,
293f5f65 1054 CPU_AVX512CD_FLAGS, 0 },
43234a1e 1055 { STRING_COMMA_LEN (".avx512er"), PROCESSOR_UNKNOWN,
293f5f65 1056 CPU_AVX512ER_FLAGS, 0 },
43234a1e 1057 { STRING_COMMA_LEN (".avx512pf"), PROCESSOR_UNKNOWN,
293f5f65 1058 CPU_AVX512PF_FLAGS, 0 },
1dfc6506 1059 { STRING_COMMA_LEN (".avx512dq"), PROCESSOR_UNKNOWN,
293f5f65 1060 CPU_AVX512DQ_FLAGS, 0 },
1dfc6506 1061 { STRING_COMMA_LEN (".avx512bw"), PROCESSOR_UNKNOWN,
293f5f65 1062 CPU_AVX512BW_FLAGS, 0 },
1dfc6506 1063 { STRING_COMMA_LEN (".avx512vl"), PROCESSOR_UNKNOWN,
293f5f65 1064 CPU_AVX512VL_FLAGS, 0 },
8a2c8fef 1065 { STRING_COMMA_LEN (".vmx"), PROCESSOR_UNKNOWN,
293f5f65 1066 CPU_VMX_FLAGS, 0 },
8729a6f6 1067 { STRING_COMMA_LEN (".vmfunc"), PROCESSOR_UNKNOWN,
293f5f65 1068 CPU_VMFUNC_FLAGS, 0 },
8a2c8fef 1069 { STRING_COMMA_LEN (".smx"), PROCESSOR_UNKNOWN,
293f5f65 1070 CPU_SMX_FLAGS, 0 },
8a2c8fef 1071 { STRING_COMMA_LEN (".xsave"), PROCESSOR_UNKNOWN,
293f5f65 1072 CPU_XSAVE_FLAGS, 0 },
c7b8aa3a 1073 { STRING_COMMA_LEN (".xsaveopt"), PROCESSOR_UNKNOWN,
293f5f65 1074 CPU_XSAVEOPT_FLAGS, 0 },
1dfc6506 1075 { STRING_COMMA_LEN (".xsavec"), PROCESSOR_UNKNOWN,
293f5f65 1076 CPU_XSAVEC_FLAGS, 0 },
1dfc6506 1077 { STRING_COMMA_LEN (".xsaves"), PROCESSOR_UNKNOWN,
293f5f65 1078 CPU_XSAVES_FLAGS, 0 },
8a2c8fef 1079 { STRING_COMMA_LEN (".aes"), PROCESSOR_UNKNOWN,
293f5f65 1080 CPU_AES_FLAGS, 0 },
8a2c8fef 1081 { STRING_COMMA_LEN (".pclmul"), PROCESSOR_UNKNOWN,
293f5f65 1082 CPU_PCLMUL_FLAGS, 0 },
8a2c8fef 1083 { STRING_COMMA_LEN (".clmul"), PROCESSOR_UNKNOWN,
293f5f65 1084 CPU_PCLMUL_FLAGS, 1 },
c7b8aa3a 1085 { STRING_COMMA_LEN (".fsgsbase"), PROCESSOR_UNKNOWN,
293f5f65 1086 CPU_FSGSBASE_FLAGS, 0 },
c7b8aa3a 1087 { STRING_COMMA_LEN (".rdrnd"), PROCESSOR_UNKNOWN,
293f5f65 1088 CPU_RDRND_FLAGS, 0 },
c7b8aa3a 1089 { STRING_COMMA_LEN (".f16c"), PROCESSOR_UNKNOWN,
293f5f65 1090 CPU_F16C_FLAGS, 0 },
6c30d220 1091 { STRING_COMMA_LEN (".bmi2"), PROCESSOR_UNKNOWN,
293f5f65 1092 CPU_BMI2_FLAGS, 0 },
8a2c8fef 1093 { STRING_COMMA_LEN (".fma"), PROCESSOR_UNKNOWN,
293f5f65 1094 CPU_FMA_FLAGS, 0 },
8a2c8fef 1095 { STRING_COMMA_LEN (".fma4"), PROCESSOR_UNKNOWN,
293f5f65 1096 CPU_FMA4_FLAGS, 0 },
8a2c8fef 1097 { STRING_COMMA_LEN (".xop"), PROCESSOR_UNKNOWN,
293f5f65 1098 CPU_XOP_FLAGS, 0 },
8a2c8fef 1099 { STRING_COMMA_LEN (".lwp"), PROCESSOR_UNKNOWN,
293f5f65 1100 CPU_LWP_FLAGS, 0 },
8a2c8fef 1101 { STRING_COMMA_LEN (".movbe"), PROCESSOR_UNKNOWN,
293f5f65 1102 CPU_MOVBE_FLAGS, 0 },
60aa667e 1103 { STRING_COMMA_LEN (".cx16"), PROCESSOR_UNKNOWN,
293f5f65 1104 CPU_CX16_FLAGS, 0 },
8a2c8fef 1105 { STRING_COMMA_LEN (".ept"), PROCESSOR_UNKNOWN,
293f5f65 1106 CPU_EPT_FLAGS, 0 },
6c30d220 1107 { STRING_COMMA_LEN (".lzcnt"), PROCESSOR_UNKNOWN,
293f5f65 1108 CPU_LZCNT_FLAGS, 0 },
272a84b1
L
1109 { STRING_COMMA_LEN (".popcnt"), PROCESSOR_UNKNOWN,
1110 CPU_POPCNT_FLAGS, 0 },
42164a71 1111 { STRING_COMMA_LEN (".hle"), PROCESSOR_UNKNOWN,
293f5f65 1112 CPU_HLE_FLAGS, 0 },
42164a71 1113 { STRING_COMMA_LEN (".rtm"), PROCESSOR_UNKNOWN,
293f5f65 1114 CPU_RTM_FLAGS, 0 },
6c30d220 1115 { STRING_COMMA_LEN (".invpcid"), PROCESSOR_UNKNOWN,
293f5f65 1116 CPU_INVPCID_FLAGS, 0 },
8a2c8fef 1117 { STRING_COMMA_LEN (".clflush"), PROCESSOR_UNKNOWN,
293f5f65 1118 CPU_CLFLUSH_FLAGS, 0 },
22109423 1119 { STRING_COMMA_LEN (".nop"), PROCESSOR_UNKNOWN,
293f5f65 1120 CPU_NOP_FLAGS, 0 },
8a2c8fef 1121 { STRING_COMMA_LEN (".syscall"), PROCESSOR_UNKNOWN,
293f5f65 1122 CPU_SYSCALL_FLAGS, 0 },
8a2c8fef 1123 { STRING_COMMA_LEN (".rdtscp"), PROCESSOR_UNKNOWN,
293f5f65 1124 CPU_RDTSCP_FLAGS, 0 },
8a2c8fef 1125 { STRING_COMMA_LEN (".3dnow"), PROCESSOR_UNKNOWN,
293f5f65 1126 CPU_3DNOW_FLAGS, 0 },
8a2c8fef 1127 { STRING_COMMA_LEN (".3dnowa"), PROCESSOR_UNKNOWN,
293f5f65 1128 CPU_3DNOWA_FLAGS, 0 },
8a2c8fef 1129 { STRING_COMMA_LEN (".padlock"), PROCESSOR_UNKNOWN,
293f5f65 1130 CPU_PADLOCK_FLAGS, 0 },
8a2c8fef 1131 { STRING_COMMA_LEN (".pacifica"), PROCESSOR_UNKNOWN,
293f5f65 1132 CPU_SVME_FLAGS, 1 },
8a2c8fef 1133 { STRING_COMMA_LEN (".svme"), PROCESSOR_UNKNOWN,
293f5f65 1134 CPU_SVME_FLAGS, 0 },
8a2c8fef 1135 { STRING_COMMA_LEN (".sse4a"), PROCESSOR_UNKNOWN,
293f5f65 1136 CPU_SSE4A_FLAGS, 0 },
8a2c8fef 1137 { STRING_COMMA_LEN (".abm"), PROCESSOR_UNKNOWN,
293f5f65 1138 CPU_ABM_FLAGS, 0 },
87973e9f 1139 { STRING_COMMA_LEN (".bmi"), PROCESSOR_UNKNOWN,
293f5f65 1140 CPU_BMI_FLAGS, 0 },
2a2a0f38 1141 { STRING_COMMA_LEN (".tbm"), PROCESSOR_UNKNOWN,
293f5f65 1142 CPU_TBM_FLAGS, 0 },
e2e1fcde 1143 { STRING_COMMA_LEN (".adx"), PROCESSOR_UNKNOWN,
293f5f65 1144 CPU_ADX_FLAGS, 0 },
e2e1fcde 1145 { STRING_COMMA_LEN (".rdseed"), PROCESSOR_UNKNOWN,
293f5f65 1146 CPU_RDSEED_FLAGS, 0 },
e2e1fcde 1147 { STRING_COMMA_LEN (".prfchw"), PROCESSOR_UNKNOWN,
293f5f65 1148 CPU_PRFCHW_FLAGS, 0 },
5c111e37 1149 { STRING_COMMA_LEN (".smap"), PROCESSOR_UNKNOWN,
293f5f65 1150 CPU_SMAP_FLAGS, 0 },
7e8b059b 1151 { STRING_COMMA_LEN (".mpx"), PROCESSOR_UNKNOWN,
293f5f65 1152 CPU_MPX_FLAGS, 0 },
a0046408 1153 { STRING_COMMA_LEN (".sha"), PROCESSOR_UNKNOWN,
293f5f65 1154 CPU_SHA_FLAGS, 0 },
963f3586 1155 { STRING_COMMA_LEN (".clflushopt"), PROCESSOR_UNKNOWN,
293f5f65 1156 CPU_CLFLUSHOPT_FLAGS, 0 },
dcf893b5 1157 { STRING_COMMA_LEN (".prefetchwt1"), PROCESSOR_UNKNOWN,
293f5f65 1158 CPU_PREFETCHWT1_FLAGS, 0 },
2cf200a4 1159 { STRING_COMMA_LEN (".se1"), PROCESSOR_UNKNOWN,
293f5f65 1160 CPU_SE1_FLAGS, 0 },
c5e7287a 1161 { STRING_COMMA_LEN (".clwb"), PROCESSOR_UNKNOWN,
293f5f65 1162 CPU_CLWB_FLAGS, 0 },
2cc1b5aa 1163 { STRING_COMMA_LEN (".avx512ifma"), PROCESSOR_UNKNOWN,
293f5f65 1164 CPU_AVX512IFMA_FLAGS, 0 },
14f195c9 1165 { STRING_COMMA_LEN (".avx512vbmi"), PROCESSOR_UNKNOWN,
293f5f65 1166 CPU_AVX512VBMI_FLAGS, 0 },
920d2ddc
IT
1167 { STRING_COMMA_LEN (".avx512_4fmaps"), PROCESSOR_UNKNOWN,
1168 CPU_AVX512_4FMAPS_FLAGS, 0 },
47acf0bd
IT
1169 { STRING_COMMA_LEN (".avx512_4vnniw"), PROCESSOR_UNKNOWN,
1170 CPU_AVX512_4VNNIW_FLAGS, 0 },
620214f7
IT
1171 { STRING_COMMA_LEN (".avx512_vpopcntdq"), PROCESSOR_UNKNOWN,
1172 CPU_AVX512_VPOPCNTDQ_FLAGS, 0 },
53467f57
IT
1173 { STRING_COMMA_LEN (".avx512_vbmi2"), PROCESSOR_UNKNOWN,
1174 CPU_AVX512_VBMI2_FLAGS, 0 },
8cfcb765
IT
1175 { STRING_COMMA_LEN (".avx512_vnni"), PROCESSOR_UNKNOWN,
1176 CPU_AVX512_VNNI_FLAGS, 0 },
ee6872be
IT
1177 { STRING_COMMA_LEN (".avx512_bitalg"), PROCESSOR_UNKNOWN,
1178 CPU_AVX512_BITALG_FLAGS, 0 },
58bf9b6a
L
1179 { STRING_COMMA_LEN (".avx_vnni"), PROCESSOR_UNKNOWN,
1180 CPU_AVX_VNNI_FLAGS, 0 },
029f3522 1181 { STRING_COMMA_LEN (".clzero"), PROCESSOR_UNKNOWN,
293f5f65 1182 CPU_CLZERO_FLAGS, 0 },
9916071f 1183 { STRING_COMMA_LEN (".mwaitx"), PROCESSOR_UNKNOWN,
293f5f65 1184 CPU_MWAITX_FLAGS, 0 },
8eab4136 1185 { STRING_COMMA_LEN (".ospke"), PROCESSOR_UNKNOWN,
293f5f65 1186 CPU_OSPKE_FLAGS, 0 },
8bc52696 1187 { STRING_COMMA_LEN (".rdpid"), PROCESSOR_UNKNOWN,
293f5f65 1188 CPU_RDPID_FLAGS, 0 },
6b40c462
L
1189 { STRING_COMMA_LEN (".ptwrite"), PROCESSOR_UNKNOWN,
1190 CPU_PTWRITE_FLAGS, 0 },
d777820b
IT
1191 { STRING_COMMA_LEN (".ibt"), PROCESSOR_UNKNOWN,
1192 CPU_IBT_FLAGS, 0 },
1193 { STRING_COMMA_LEN (".shstk"), PROCESSOR_UNKNOWN,
1194 CPU_SHSTK_FLAGS, 0 },
48521003
IT
1195 { STRING_COMMA_LEN (".gfni"), PROCESSOR_UNKNOWN,
1196 CPU_GFNI_FLAGS, 0 },
8dcf1fad
IT
1197 { STRING_COMMA_LEN (".vaes"), PROCESSOR_UNKNOWN,
1198 CPU_VAES_FLAGS, 0 },
ff1982d5
IT
1199 { STRING_COMMA_LEN (".vpclmulqdq"), PROCESSOR_UNKNOWN,
1200 CPU_VPCLMULQDQ_FLAGS, 0 },
3233d7d0
IT
1201 { STRING_COMMA_LEN (".wbnoinvd"), PROCESSOR_UNKNOWN,
1202 CPU_WBNOINVD_FLAGS, 0 },
be3a8dca
IT
1203 { STRING_COMMA_LEN (".pconfig"), PROCESSOR_UNKNOWN,
1204 CPU_PCONFIG_FLAGS, 0 },
de89d0a3
IT
1205 { STRING_COMMA_LEN (".waitpkg"), PROCESSOR_UNKNOWN,
1206 CPU_WAITPKG_FLAGS, 0 },
c48935d7
IT
1207 { STRING_COMMA_LEN (".cldemote"), PROCESSOR_UNKNOWN,
1208 CPU_CLDEMOTE_FLAGS, 0 },
260cd341
LC
1209 { STRING_COMMA_LEN (".amx_int8"), PROCESSOR_UNKNOWN,
1210 CPU_AMX_INT8_FLAGS, 0 },
1211 { STRING_COMMA_LEN (".amx_bf16"), PROCESSOR_UNKNOWN,
1212 CPU_AMX_BF16_FLAGS, 0 },
1213 { STRING_COMMA_LEN (".amx_tile"), PROCESSOR_UNKNOWN,
1214 CPU_AMX_TILE_FLAGS, 0 },
c0a30a9f
L
1215 { STRING_COMMA_LEN (".movdiri"), PROCESSOR_UNKNOWN,
1216 CPU_MOVDIRI_FLAGS, 0 },
1217 { STRING_COMMA_LEN (".movdir64b"), PROCESSOR_UNKNOWN,
1218 CPU_MOVDIR64B_FLAGS, 0 },
d6aab7a1
XG
1219 { STRING_COMMA_LEN (".avx512_bf16"), PROCESSOR_UNKNOWN,
1220 CPU_AVX512_BF16_FLAGS, 0 },
9186c494
L
1221 { STRING_COMMA_LEN (".avx512_vp2intersect"), PROCESSOR_UNKNOWN,
1222 CPU_AVX512_VP2INTERSECT_FLAGS, 0 },
81d54bb7
CL
1223 { STRING_COMMA_LEN (".tdx"), PROCESSOR_UNKNOWN,
1224 CPU_TDX_FLAGS, 0 },
dd455cf5
L
1225 { STRING_COMMA_LEN (".enqcmd"), PROCESSOR_UNKNOWN,
1226 CPU_ENQCMD_FLAGS, 0 },
4b27d27c
L
1227 { STRING_COMMA_LEN (".serialize"), PROCESSOR_UNKNOWN,
1228 CPU_SERIALIZE_FLAGS, 0 },
142861df
JB
1229 { STRING_COMMA_LEN (".rdpru"), PROCESSOR_UNKNOWN,
1230 CPU_RDPRU_FLAGS, 0 },
1231 { STRING_COMMA_LEN (".mcommit"), PROCESSOR_UNKNOWN,
1232 CPU_MCOMMIT_FLAGS, 0 },
a847e322
JB
1233 { STRING_COMMA_LEN (".sev_es"), PROCESSOR_UNKNOWN,
1234 CPU_SEV_ES_FLAGS, 0 },
bb651e8b
CL
1235 { STRING_COMMA_LEN (".tsxldtrk"), PROCESSOR_UNKNOWN,
1236 CPU_TSXLDTRK_FLAGS, 0 },
c4694f17
TG
1237 { STRING_COMMA_LEN (".kl"), PROCESSOR_UNKNOWN,
1238 CPU_KL_FLAGS, 0 },
1239 { STRING_COMMA_LEN (".widekl"), PROCESSOR_UNKNOWN,
1240 CPU_WIDEKL_FLAGS, 0 },
f64c42a9
LC
1241 { STRING_COMMA_LEN (".uintr"), PROCESSOR_UNKNOWN,
1242 CPU_UINTR_FLAGS, 0 },
c1fa250a
LC
1243 { STRING_COMMA_LEN (".hreset"), PROCESSOR_UNKNOWN,
1244 CPU_HRESET_FLAGS, 0 },
0cc78721
CL
1245 { STRING_COMMA_LEN (".avx512_fp16"), PROCESSOR_UNKNOWN,
1246 CPU_AVX512_FP16_FLAGS, 0 },
293f5f65
L
1247};
1248
1249static const noarch_entry cpu_noarch[] =
1250{
1251 { STRING_COMMA_LEN ("no87"), CPU_ANY_X87_FLAGS },
1848e567
L
1252 { STRING_COMMA_LEN ("no287"), CPU_ANY_287_FLAGS },
1253 { STRING_COMMA_LEN ("no387"), CPU_ANY_387_FLAGS },
1254 { STRING_COMMA_LEN ("no687"), CPU_ANY_687_FLAGS },
d871f3f4
L
1255 { STRING_COMMA_LEN ("nocmov"), CPU_ANY_CMOV_FLAGS },
1256 { STRING_COMMA_LEN ("nofxsr"), CPU_ANY_FXSR_FLAGS },
293f5f65
L
1257 { STRING_COMMA_LEN ("nommx"), CPU_ANY_MMX_FLAGS },
1258 { STRING_COMMA_LEN ("nosse"), CPU_ANY_SSE_FLAGS },
1848e567
L
1259 { STRING_COMMA_LEN ("nosse2"), CPU_ANY_SSE2_FLAGS },
1260 { STRING_COMMA_LEN ("nosse3"), CPU_ANY_SSE3_FLAGS },
af5c13b0 1261 { STRING_COMMA_LEN ("nosse4a"), CPU_ANY_SSE4A_FLAGS },
1848e567
L
1262 { STRING_COMMA_LEN ("nossse3"), CPU_ANY_SSSE3_FLAGS },
1263 { STRING_COMMA_LEN ("nosse4.1"), CPU_ANY_SSE4_1_FLAGS },
1264 { STRING_COMMA_LEN ("nosse4.2"), CPU_ANY_SSE4_2_FLAGS },
af5c13b0 1265 { STRING_COMMA_LEN ("nosse4"), CPU_ANY_SSE4_1_FLAGS },
293f5f65 1266 { STRING_COMMA_LEN ("noavx"), CPU_ANY_AVX_FLAGS },
1848e567 1267 { STRING_COMMA_LEN ("noavx2"), CPU_ANY_AVX2_FLAGS },
144b71e2
L
1268 { STRING_COMMA_LEN ("noavx512f"), CPU_ANY_AVX512F_FLAGS },
1269 { STRING_COMMA_LEN ("noavx512cd"), CPU_ANY_AVX512CD_FLAGS },
1270 { STRING_COMMA_LEN ("noavx512er"), CPU_ANY_AVX512ER_FLAGS },
1271 { STRING_COMMA_LEN ("noavx512pf"), CPU_ANY_AVX512PF_FLAGS },
1272 { STRING_COMMA_LEN ("noavx512dq"), CPU_ANY_AVX512DQ_FLAGS },
1273 { STRING_COMMA_LEN ("noavx512bw"), CPU_ANY_AVX512BW_FLAGS },
1274 { STRING_COMMA_LEN ("noavx512vl"), CPU_ANY_AVX512VL_FLAGS },
1275 { STRING_COMMA_LEN ("noavx512ifma"), CPU_ANY_AVX512IFMA_FLAGS },
1276 { STRING_COMMA_LEN ("noavx512vbmi"), CPU_ANY_AVX512VBMI_FLAGS },
920d2ddc 1277 { STRING_COMMA_LEN ("noavx512_4fmaps"), CPU_ANY_AVX512_4FMAPS_FLAGS },
47acf0bd 1278 { STRING_COMMA_LEN ("noavx512_4vnniw"), CPU_ANY_AVX512_4VNNIW_FLAGS },
620214f7 1279 { STRING_COMMA_LEN ("noavx512_vpopcntdq"), CPU_ANY_AVX512_VPOPCNTDQ_FLAGS },
53467f57 1280 { STRING_COMMA_LEN ("noavx512_vbmi2"), CPU_ANY_AVX512_VBMI2_FLAGS },
8cfcb765 1281 { STRING_COMMA_LEN ("noavx512_vnni"), CPU_ANY_AVX512_VNNI_FLAGS },
ee6872be 1282 { STRING_COMMA_LEN ("noavx512_bitalg"), CPU_ANY_AVX512_BITALG_FLAGS },
58bf9b6a 1283 { STRING_COMMA_LEN ("noavx_vnni"), CPU_ANY_AVX_VNNI_FLAGS },
d777820b
IT
1284 { STRING_COMMA_LEN ("noibt"), CPU_ANY_IBT_FLAGS },
1285 { STRING_COMMA_LEN ("noshstk"), CPU_ANY_SHSTK_FLAGS },
260cd341
LC
1286 { STRING_COMMA_LEN ("noamx_int8"), CPU_ANY_AMX_INT8_FLAGS },
1287 { STRING_COMMA_LEN ("noamx_bf16"), CPU_ANY_AMX_BF16_FLAGS },
1288 { STRING_COMMA_LEN ("noamx_tile"), CPU_ANY_AMX_TILE_FLAGS },
c0a30a9f
L
1289 { STRING_COMMA_LEN ("nomovdiri"), CPU_ANY_MOVDIRI_FLAGS },
1290 { STRING_COMMA_LEN ("nomovdir64b"), CPU_ANY_MOVDIR64B_FLAGS },
d6aab7a1 1291 { STRING_COMMA_LEN ("noavx512_bf16"), CPU_ANY_AVX512_BF16_FLAGS },
708a2fff
CL
1292 { STRING_COMMA_LEN ("noavx512_vp2intersect"),
1293 CPU_ANY_AVX512_VP2INTERSECT_FLAGS },
81d54bb7 1294 { STRING_COMMA_LEN ("notdx"), CPU_ANY_TDX_FLAGS },
dd455cf5 1295 { STRING_COMMA_LEN ("noenqcmd"), CPU_ANY_ENQCMD_FLAGS },
4b27d27c 1296 { STRING_COMMA_LEN ("noserialize"), CPU_ANY_SERIALIZE_FLAGS },
bb651e8b 1297 { STRING_COMMA_LEN ("notsxldtrk"), CPU_ANY_TSXLDTRK_FLAGS },
c4694f17
TG
1298 { STRING_COMMA_LEN ("nokl"), CPU_ANY_KL_FLAGS },
1299 { STRING_COMMA_LEN ("nowidekl"), CPU_ANY_WIDEKL_FLAGS },
f64c42a9 1300 { STRING_COMMA_LEN ("nouintr"), CPU_ANY_UINTR_FLAGS },
c1fa250a 1301 { STRING_COMMA_LEN ("nohreset"), CPU_ANY_HRESET_FLAGS },
0cc78721 1302 { STRING_COMMA_LEN ("noavx512_fp16"), CPU_ANY_AVX512_FP16_FLAGS },
e413e4e9
AM
1303};
1304
704209c0 1305#ifdef I386COFF
a6c24e68
NC
1306/* Like s_lcomm_internal in gas/read.c but the alignment string
1307 is allowed to be optional. */
1308
1309static symbolS *
1310pe_lcomm_internal (int needs_align, symbolS *symbolP, addressT size)
1311{
1312 addressT align = 0;
1313
1314 SKIP_WHITESPACE ();
1315
7ab9ffdd 1316 if (needs_align
a6c24e68
NC
1317 && *input_line_pointer == ',')
1318 {
1319 align = parse_align (needs_align - 1);
7ab9ffdd 1320
a6c24e68
NC
1321 if (align == (addressT) -1)
1322 return NULL;
1323 }
1324 else
1325 {
1326 if (size >= 8)
1327 align = 3;
1328 else if (size >= 4)
1329 align = 2;
1330 else if (size >= 2)
1331 align = 1;
1332 else
1333 align = 0;
1334 }
1335
1336 bss_alloc (symbolP, size, align);
1337 return symbolP;
1338}
1339
704209c0 1340static void
a6c24e68
NC
1341pe_lcomm (int needs_align)
1342{
1343 s_comm_internal (needs_align * 2, pe_lcomm_internal);
1344}
704209c0 1345#endif
a6c24e68 1346
29b0f896
AM
1347const pseudo_typeS md_pseudo_table[] =
1348{
1349#if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
1350 {"align", s_align_bytes, 0},
1351#else
1352 {"align", s_align_ptwo, 0},
1353#endif
1354 {"arch", set_cpu_arch, 0},
1355#ifndef I386COFF
1356 {"bss", s_bss, 0},
a6c24e68
NC
1357#else
1358 {"lcomm", pe_lcomm, 1},
29b0f896
AM
1359#endif
1360 {"ffloat", float_cons, 'f'},
1361 {"dfloat", float_cons, 'd'},
1362 {"tfloat", float_cons, 'x'},
7d19d096 1363 {"hfloat", float_cons, 'h'},
de133cf9 1364 {"bfloat16", float_cons, 'b'},
29b0f896 1365 {"value", cons, 2},
d182319b 1366 {"slong", signed_cons, 4},
29b0f896
AM
1367 {"noopt", s_ignore, 0},
1368 {"optim", s_ignore, 0},
1369 {"code16gcc", set_16bit_gcc_code_flag, CODE_16BIT},
1370 {"code16", set_code_flag, CODE_16BIT},
1371 {"code32", set_code_flag, CODE_32BIT},
da5f19a2 1372#ifdef BFD64
29b0f896 1373 {"code64", set_code_flag, CODE_64BIT},
da5f19a2 1374#endif
29b0f896
AM
1375 {"intel_syntax", set_intel_syntax, 1},
1376 {"att_syntax", set_intel_syntax, 0},
1efbbeb4
L
1377 {"intel_mnemonic", set_intel_mnemonic, 1},
1378 {"att_mnemonic", set_intel_mnemonic, 0},
db51cc60
L
1379 {"allow_index_reg", set_allow_index_reg, 1},
1380 {"disallow_index_reg", set_allow_index_reg, 0},
7bab8ab5
JB
1381 {"sse_check", set_check, 0},
1382 {"operand_check", set_check, 1},
3b22753a
L
1383#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1384 {"largecomm", handle_large_common, 0},
07a53e5c 1385#else
68d20676 1386 {"file", dwarf2_directive_file, 0},
07a53e5c
RH
1387 {"loc", dwarf2_directive_loc, 0},
1388 {"loc_mark_labels", dwarf2_directive_loc_mark_labels, 0},
3b22753a 1389#endif
6482c264
NC
1390#ifdef TE_PE
1391 {"secrel32", pe_directive_secrel, 0},
1392#endif
29b0f896
AM
1393 {0, 0, 0}
1394};
1395
1396/* For interface with expression (). */
1397extern char *input_line_pointer;
1398
1399/* Hash table for instruction mnemonic lookup. */
629310ab 1400static htab_t op_hash;
29b0f896
AM
1401
1402/* Hash table for register lookup. */
629310ab 1403static htab_t reg_hash;
29b0f896 1404\f
ce8a8b2f
AM
1405 /* Various efficient no-op patterns for aligning code labels.
1406 Note: Don't try to assemble the instructions in the comments.
1407 0L and 0w are not legal. */
62a02d25
L
1408static const unsigned char f32_1[] =
1409 {0x90}; /* nop */
1410static const unsigned char f32_2[] =
1411 {0x66,0x90}; /* xchg %ax,%ax */
1412static const unsigned char f32_3[] =
1413 {0x8d,0x76,0x00}; /* leal 0(%esi),%esi */
1414static const unsigned char f32_4[] =
1415 {0x8d,0x74,0x26,0x00}; /* leal 0(%esi,1),%esi */
62a02d25
L
1416static const unsigned char f32_6[] =
1417 {0x8d,0xb6,0x00,0x00,0x00,0x00}; /* leal 0L(%esi),%esi */
1418static const unsigned char f32_7[] =
1419 {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal 0L(%esi,1),%esi */
62a02d25 1420static const unsigned char f16_3[] =
3ae729d5 1421 {0x8d,0x74,0x00}; /* lea 0(%si),%si */
62a02d25 1422static const unsigned char f16_4[] =
3ae729d5
L
1423 {0x8d,0xb4,0x00,0x00}; /* lea 0W(%si),%si */
1424static const unsigned char jump_disp8[] =
1425 {0xeb}; /* jmp disp8 */
1426static const unsigned char jump32_disp32[] =
1427 {0xe9}; /* jmp disp32 */
1428static const unsigned char jump16_disp32[] =
1429 {0x66,0xe9}; /* jmp disp32 */
62a02d25
L
1430/* 32-bit NOPs patterns. */
1431static const unsigned char *const f32_patt[] = {
3ae729d5 1432 f32_1, f32_2, f32_3, f32_4, NULL, f32_6, f32_7
62a02d25
L
1433};
1434/* 16-bit NOPs patterns. */
1435static const unsigned char *const f16_patt[] = {
3ae729d5 1436 f32_1, f32_2, f16_3, f16_4
62a02d25
L
1437};
1438/* nopl (%[re]ax) */
1439static const unsigned char alt_3[] =
1440 {0x0f,0x1f,0x00};
1441/* nopl 0(%[re]ax) */
1442static const unsigned char alt_4[] =
1443 {0x0f,0x1f,0x40,0x00};
1444/* nopl 0(%[re]ax,%[re]ax,1) */
1445static const unsigned char alt_5[] =
1446 {0x0f,0x1f,0x44,0x00,0x00};
1447/* nopw 0(%[re]ax,%[re]ax,1) */
1448static const unsigned char alt_6[] =
1449 {0x66,0x0f,0x1f,0x44,0x00,0x00};
1450/* nopl 0L(%[re]ax) */
1451static const unsigned char alt_7[] =
1452 {0x0f,0x1f,0x80,0x00,0x00,0x00,0x00};
1453/* nopl 0L(%[re]ax,%[re]ax,1) */
1454static const unsigned char alt_8[] =
1455 {0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1456/* nopw 0L(%[re]ax,%[re]ax,1) */
1457static const unsigned char alt_9[] =
1458 {0x66,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1459/* nopw %cs:0L(%[re]ax,%[re]ax,1) */
1460static const unsigned char alt_10[] =
1461 {0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
3ae729d5
L
1462/* data16 nopw %cs:0L(%eax,%eax,1) */
1463static const unsigned char alt_11[] =
1464 {0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
62a02d25
L
1465/* 32-bit and 64-bit NOPs patterns. */
1466static const unsigned char *const alt_patt[] = {
1467 f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
3ae729d5 1468 alt_9, alt_10, alt_11
62a02d25
L
1469};
1470
1471/* Genenerate COUNT bytes of NOPs to WHERE from PATT with the maximum
1472 size of a single NOP instruction MAX_SINGLE_NOP_SIZE. */
1473
1474static void
1475i386_output_nops (char *where, const unsigned char *const *patt,
1476 int count, int max_single_nop_size)
1477
1478{
3ae729d5
L
1479 /* Place the longer NOP first. */
1480 int last;
1481 int offset;
3076e594
NC
1482 const unsigned char *nops;
1483
1484 if (max_single_nop_size < 1)
1485 {
1486 as_fatal (_("i386_output_nops called to generate nops of at most %d bytes!"),
1487 max_single_nop_size);
1488 return;
1489 }
1490
1491 nops = patt[max_single_nop_size - 1];
3ae729d5
L
1492
1493 /* Use the smaller one if the requsted one isn't available. */
1494 if (nops == NULL)
62a02d25 1495 {
3ae729d5
L
1496 max_single_nop_size--;
1497 nops = patt[max_single_nop_size - 1];
62a02d25
L
1498 }
1499
3ae729d5
L
1500 last = count % max_single_nop_size;
1501
1502 count -= last;
1503 for (offset = 0; offset < count; offset += max_single_nop_size)
1504 memcpy (where + offset, nops, max_single_nop_size);
1505
1506 if (last)
1507 {
1508 nops = patt[last - 1];
1509 if (nops == NULL)
1510 {
1511 /* Use the smaller one plus one-byte NOP if the needed one
1512 isn't available. */
1513 last--;
1514 nops = patt[last - 1];
1515 memcpy (where + offset, nops, last);
1516 where[offset + last] = *patt[0];
1517 }
1518 else
1519 memcpy (where + offset, nops, last);
1520 }
62a02d25
L
1521}
1522
3ae729d5
L
1523static INLINE int
1524fits_in_imm7 (offsetT num)
1525{
1526 return (num & 0x7f) == num;
1527}
1528
1529static INLINE int
1530fits_in_imm31 (offsetT num)
1531{
1532 return (num & 0x7fffffff) == num;
1533}
62a02d25
L
1534
1535/* Genenerate COUNT bytes of NOPs to WHERE with the maximum size of a
1536 single NOP instruction LIMIT. */
1537
1538void
3ae729d5 1539i386_generate_nops (fragS *fragP, char *where, offsetT count, int limit)
62a02d25 1540{
3ae729d5 1541 const unsigned char *const *patt = NULL;
62a02d25 1542 int max_single_nop_size;
3ae729d5
L
1543 /* Maximum number of NOPs before switching to jump over NOPs. */
1544 int max_number_of_nops;
62a02d25 1545
3ae729d5 1546 switch (fragP->fr_type)
62a02d25 1547 {
3ae729d5
L
1548 case rs_fill_nop:
1549 case rs_align_code:
1550 break;
e379e5f3
L
1551 case rs_machine_dependent:
1552 /* Allow NOP padding for jumps and calls. */
1553 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
1554 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
1555 break;
1556 /* Fall through. */
3ae729d5 1557 default:
62a02d25
L
1558 return;
1559 }
1560
ccc9c027
L
1561 /* We need to decide which NOP sequence to use for 32bit and
1562 64bit. When -mtune= is used:
4eed87de 1563
76bc74dc
L
1564 1. For PROCESSOR_I386, PROCESSOR_I486, PROCESSOR_PENTIUM and
1565 PROCESSOR_GENERIC32, f32_patt will be used.
80b8656c
L
1566 2. For the rest, alt_patt will be used.
1567
1568 When -mtune= isn't used, alt_patt will be used if
22109423 1569 cpu_arch_isa_flags has CpuNop. Otherwise, f32_patt will
76bc74dc 1570 be used.
ccc9c027
L
1571
1572 When -march= or .arch is used, we can't use anything beyond
1573 cpu_arch_isa_flags. */
1574
1575 if (flag_code == CODE_16BIT)
1576 {
3ae729d5
L
1577 patt = f16_patt;
1578 max_single_nop_size = sizeof (f16_patt) / sizeof (f16_patt[0]);
1579 /* Limit number of NOPs to 2 in 16-bit mode. */
1580 max_number_of_nops = 2;
252b5132 1581 }
33fef721 1582 else
ccc9c027 1583 {
fbf3f584 1584 if (fragP->tc_frag_data.isa == PROCESSOR_UNKNOWN)
ccc9c027
L
1585 {
1586 /* PROCESSOR_UNKNOWN means that all ISAs may be used. */
1587 switch (cpu_arch_tune)
1588 {
1589 case PROCESSOR_UNKNOWN:
1590 /* We use cpu_arch_isa_flags to check if we SHOULD
22109423
L
1591 optimize with nops. */
1592 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1593 patt = alt_patt;
ccc9c027
L
1594 else
1595 patt = f32_patt;
1596 break;
ccc9c027
L
1597 case PROCESSOR_PENTIUM4:
1598 case PROCESSOR_NOCONA:
ef05d495 1599 case PROCESSOR_CORE:
76bc74dc 1600 case PROCESSOR_CORE2:
bd5295b2 1601 case PROCESSOR_COREI7:
3632d14b 1602 case PROCESSOR_L1OM:
7a9068fe 1603 case PROCESSOR_K1OM:
76bc74dc 1604 case PROCESSOR_GENERIC64:
ccc9c027
L
1605 case PROCESSOR_K6:
1606 case PROCESSOR_ATHLON:
1607 case PROCESSOR_K8:
4eed87de 1608 case PROCESSOR_AMDFAM10:
8aedb9fe 1609 case PROCESSOR_BD:
029f3522 1610 case PROCESSOR_ZNVER:
7b458c12 1611 case PROCESSOR_BT:
80b8656c 1612 patt = alt_patt;
ccc9c027 1613 break;
76bc74dc 1614 case PROCESSOR_I386:
ccc9c027
L
1615 case PROCESSOR_I486:
1616 case PROCESSOR_PENTIUM:
2dde1948 1617 case PROCESSOR_PENTIUMPRO:
81486035 1618 case PROCESSOR_IAMCU:
ccc9c027
L
1619 case PROCESSOR_GENERIC32:
1620 patt = f32_patt;
1621 break;
4eed87de 1622 }
ccc9c027
L
1623 }
1624 else
1625 {
fbf3f584 1626 switch (fragP->tc_frag_data.tune)
ccc9c027
L
1627 {
1628 case PROCESSOR_UNKNOWN:
e6a14101 1629 /* When cpu_arch_isa is set, cpu_arch_tune shouldn't be
ccc9c027
L
1630 PROCESSOR_UNKNOWN. */
1631 abort ();
1632 break;
1633
76bc74dc 1634 case PROCESSOR_I386:
ccc9c027
L
1635 case PROCESSOR_I486:
1636 case PROCESSOR_PENTIUM:
81486035 1637 case PROCESSOR_IAMCU:
ccc9c027
L
1638 case PROCESSOR_K6:
1639 case PROCESSOR_ATHLON:
1640 case PROCESSOR_K8:
4eed87de 1641 case PROCESSOR_AMDFAM10:
8aedb9fe 1642 case PROCESSOR_BD:
029f3522 1643 case PROCESSOR_ZNVER:
7b458c12 1644 case PROCESSOR_BT:
ccc9c027
L
1645 case PROCESSOR_GENERIC32:
1646 /* We use cpu_arch_isa_flags to check if we CAN optimize
22109423
L
1647 with nops. */
1648 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1649 patt = alt_patt;
ccc9c027
L
1650 else
1651 patt = f32_patt;
1652 break;
76bc74dc
L
1653 case PROCESSOR_PENTIUMPRO:
1654 case PROCESSOR_PENTIUM4:
1655 case PROCESSOR_NOCONA:
1656 case PROCESSOR_CORE:
ef05d495 1657 case PROCESSOR_CORE2:
bd5295b2 1658 case PROCESSOR_COREI7:
3632d14b 1659 case PROCESSOR_L1OM:
7a9068fe 1660 case PROCESSOR_K1OM:
22109423 1661 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1662 patt = alt_patt;
ccc9c027
L
1663 else
1664 patt = f32_patt;
1665 break;
1666 case PROCESSOR_GENERIC64:
80b8656c 1667 patt = alt_patt;
ccc9c027 1668 break;
4eed87de 1669 }
ccc9c027
L
1670 }
1671
76bc74dc
L
1672 if (patt == f32_patt)
1673 {
3ae729d5
L
1674 max_single_nop_size = sizeof (f32_patt) / sizeof (f32_patt[0]);
1675 /* Limit number of NOPs to 2 for older processors. */
1676 max_number_of_nops = 2;
76bc74dc
L
1677 }
1678 else
1679 {
3ae729d5
L
1680 max_single_nop_size = sizeof (alt_patt) / sizeof (alt_patt[0]);
1681 /* Limit number of NOPs to 7 for newer processors. */
1682 max_number_of_nops = 7;
1683 }
1684 }
1685
1686 if (limit == 0)
1687 limit = max_single_nop_size;
1688
1689 if (fragP->fr_type == rs_fill_nop)
1690 {
1691 /* Output NOPs for .nop directive. */
1692 if (limit > max_single_nop_size)
1693 {
1694 as_bad_where (fragP->fr_file, fragP->fr_line,
1695 _("invalid single nop size: %d "
1696 "(expect within [0, %d])"),
1697 limit, max_single_nop_size);
1698 return;
1699 }
1700 }
e379e5f3 1701 else if (fragP->fr_type != rs_machine_dependent)
3ae729d5
L
1702 fragP->fr_var = count;
1703
1704 if ((count / max_single_nop_size) > max_number_of_nops)
1705 {
1706 /* Generate jump over NOPs. */
1707 offsetT disp = count - 2;
1708 if (fits_in_imm7 (disp))
1709 {
1710 /* Use "jmp disp8" if possible. */
1711 count = disp;
1712 where[0] = jump_disp8[0];
1713 where[1] = count;
1714 where += 2;
1715 }
1716 else
1717 {
1718 unsigned int size_of_jump;
1719
1720 if (flag_code == CODE_16BIT)
1721 {
1722 where[0] = jump16_disp32[0];
1723 where[1] = jump16_disp32[1];
1724 size_of_jump = 2;
1725 }
1726 else
1727 {
1728 where[0] = jump32_disp32[0];
1729 size_of_jump = 1;
1730 }
1731
1732 count -= size_of_jump + 4;
1733 if (!fits_in_imm31 (count))
1734 {
1735 as_bad_where (fragP->fr_file, fragP->fr_line,
1736 _("jump over nop padding out of range"));
1737 return;
1738 }
1739
1740 md_number_to_chars (where + size_of_jump, count, 4);
1741 where += size_of_jump + 4;
76bc74dc 1742 }
ccc9c027 1743 }
3ae729d5
L
1744
1745 /* Generate multiple NOPs. */
1746 i386_output_nops (where, patt, count, limit);
252b5132
RH
1747}
1748
c6fb90c8 1749static INLINE int
0dfbf9d7 1750operand_type_all_zero (const union i386_operand_type *x)
40fb9820 1751{
0dfbf9d7 1752 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1753 {
1754 case 3:
0dfbf9d7 1755 if (x->array[2])
c6fb90c8 1756 return 0;
1a0670f3 1757 /* Fall through. */
c6fb90c8 1758 case 2:
0dfbf9d7 1759 if (x->array[1])
c6fb90c8 1760 return 0;
1a0670f3 1761 /* Fall through. */
c6fb90c8 1762 case 1:
0dfbf9d7 1763 return !x->array[0];
c6fb90c8
L
1764 default:
1765 abort ();
1766 }
40fb9820
L
1767}
1768
c6fb90c8 1769static INLINE void
0dfbf9d7 1770operand_type_set (union i386_operand_type *x, unsigned int v)
40fb9820 1771{
0dfbf9d7 1772 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1773 {
1774 case 3:
0dfbf9d7 1775 x->array[2] = v;
1a0670f3 1776 /* Fall through. */
c6fb90c8 1777 case 2:
0dfbf9d7 1778 x->array[1] = v;
1a0670f3 1779 /* Fall through. */
c6fb90c8 1780 case 1:
0dfbf9d7 1781 x->array[0] = v;
1a0670f3 1782 /* Fall through. */
c6fb90c8
L
1783 break;
1784 default:
1785 abort ();
1786 }
bab6aec1
JB
1787
1788 x->bitfield.class = ClassNone;
75e5731b 1789 x->bitfield.instance = InstanceNone;
c6fb90c8 1790}
40fb9820 1791
c6fb90c8 1792static INLINE int
0dfbf9d7
L
1793operand_type_equal (const union i386_operand_type *x,
1794 const union i386_operand_type *y)
c6fb90c8 1795{
0dfbf9d7 1796 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1797 {
1798 case 3:
0dfbf9d7 1799 if (x->array[2] != y->array[2])
c6fb90c8 1800 return 0;
1a0670f3 1801 /* Fall through. */
c6fb90c8 1802 case 2:
0dfbf9d7 1803 if (x->array[1] != y->array[1])
c6fb90c8 1804 return 0;
1a0670f3 1805 /* Fall through. */
c6fb90c8 1806 case 1:
0dfbf9d7 1807 return x->array[0] == y->array[0];
c6fb90c8
L
1808 break;
1809 default:
1810 abort ();
1811 }
1812}
40fb9820 1813
0dfbf9d7
L
1814static INLINE int
1815cpu_flags_all_zero (const union i386_cpu_flags *x)
1816{
1817 switch (ARRAY_SIZE(x->array))
1818 {
53467f57
IT
1819 case 4:
1820 if (x->array[3])
1821 return 0;
1822 /* Fall through. */
0dfbf9d7
L
1823 case 3:
1824 if (x->array[2])
1825 return 0;
1a0670f3 1826 /* Fall through. */
0dfbf9d7
L
1827 case 2:
1828 if (x->array[1])
1829 return 0;
1a0670f3 1830 /* Fall through. */
0dfbf9d7
L
1831 case 1:
1832 return !x->array[0];
1833 default:
1834 abort ();
1835 }
1836}
1837
0dfbf9d7
L
1838static INLINE int
1839cpu_flags_equal (const union i386_cpu_flags *x,
1840 const union i386_cpu_flags *y)
1841{
1842 switch (ARRAY_SIZE(x->array))
1843 {
53467f57
IT
1844 case 4:
1845 if (x->array[3] != y->array[3])
1846 return 0;
1847 /* Fall through. */
0dfbf9d7
L
1848 case 3:
1849 if (x->array[2] != y->array[2])
1850 return 0;
1a0670f3 1851 /* Fall through. */
0dfbf9d7
L
1852 case 2:
1853 if (x->array[1] != y->array[1])
1854 return 0;
1a0670f3 1855 /* Fall through. */
0dfbf9d7
L
1856 case 1:
1857 return x->array[0] == y->array[0];
1858 break;
1859 default:
1860 abort ();
1861 }
1862}
c6fb90c8
L
1863
1864static INLINE int
1865cpu_flags_check_cpu64 (i386_cpu_flags f)
1866{
1867 return !((flag_code == CODE_64BIT && f.bitfield.cpuno64)
1868 || (flag_code != CODE_64BIT && f.bitfield.cpu64));
40fb9820
L
1869}
1870
c6fb90c8
L
1871static INLINE i386_cpu_flags
1872cpu_flags_and (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1873{
c6fb90c8
L
1874 switch (ARRAY_SIZE (x.array))
1875 {
53467f57
IT
1876 case 4:
1877 x.array [3] &= y.array [3];
1878 /* Fall through. */
c6fb90c8
L
1879 case 3:
1880 x.array [2] &= y.array [2];
1a0670f3 1881 /* Fall through. */
c6fb90c8
L
1882 case 2:
1883 x.array [1] &= y.array [1];
1a0670f3 1884 /* Fall through. */
c6fb90c8
L
1885 case 1:
1886 x.array [0] &= y.array [0];
1887 break;
1888 default:
1889 abort ();
1890 }
1891 return x;
1892}
40fb9820 1893
c6fb90c8
L
1894static INLINE i386_cpu_flags
1895cpu_flags_or (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1896{
c6fb90c8 1897 switch (ARRAY_SIZE (x.array))
40fb9820 1898 {
53467f57
IT
1899 case 4:
1900 x.array [3] |= y.array [3];
1901 /* Fall through. */
c6fb90c8
L
1902 case 3:
1903 x.array [2] |= y.array [2];
1a0670f3 1904 /* Fall through. */
c6fb90c8
L
1905 case 2:
1906 x.array [1] |= y.array [1];
1a0670f3 1907 /* Fall through. */
c6fb90c8
L
1908 case 1:
1909 x.array [0] |= y.array [0];
40fb9820
L
1910 break;
1911 default:
1912 abort ();
1913 }
40fb9820
L
1914 return x;
1915}
1916
309d3373
JB
1917static INLINE i386_cpu_flags
1918cpu_flags_and_not (i386_cpu_flags x, i386_cpu_flags y)
1919{
1920 switch (ARRAY_SIZE (x.array))
1921 {
53467f57
IT
1922 case 4:
1923 x.array [3] &= ~y.array [3];
1924 /* Fall through. */
309d3373
JB
1925 case 3:
1926 x.array [2] &= ~y.array [2];
1a0670f3 1927 /* Fall through. */
309d3373
JB
1928 case 2:
1929 x.array [1] &= ~y.array [1];
1a0670f3 1930 /* Fall through. */
309d3373
JB
1931 case 1:
1932 x.array [0] &= ~y.array [0];
1933 break;
1934 default:
1935 abort ();
1936 }
1937 return x;
1938}
1939
6c0946d0
JB
1940static const i386_cpu_flags avx512 = CPU_ANY_AVX512F_FLAGS;
1941
c0f3af97
L
1942#define CPU_FLAGS_ARCH_MATCH 0x1
1943#define CPU_FLAGS_64BIT_MATCH 0x2
1944
c0f3af97 1945#define CPU_FLAGS_PERFECT_MATCH \
db12e14e 1946 (CPU_FLAGS_ARCH_MATCH | CPU_FLAGS_64BIT_MATCH)
c0f3af97
L
1947
1948/* Return CPU flags match bits. */
3629bb00 1949
40fb9820 1950static int
d3ce72d0 1951cpu_flags_match (const insn_template *t)
40fb9820 1952{
c0f3af97
L
1953 i386_cpu_flags x = t->cpu_flags;
1954 int match = cpu_flags_check_cpu64 (x) ? CPU_FLAGS_64BIT_MATCH : 0;
40fb9820
L
1955
1956 x.bitfield.cpu64 = 0;
1957 x.bitfield.cpuno64 = 0;
1958
0dfbf9d7 1959 if (cpu_flags_all_zero (&x))
c0f3af97
L
1960 {
1961 /* This instruction is available on all archs. */
db12e14e 1962 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 1963 }
3629bb00
L
1964 else
1965 {
c0f3af97 1966 /* This instruction is available only on some archs. */
3629bb00
L
1967 i386_cpu_flags cpu = cpu_arch_flags;
1968
ab592e75
JB
1969 /* AVX512VL is no standalone feature - match it and then strip it. */
1970 if (x.bitfield.cpuavx512vl && !cpu.bitfield.cpuavx512vl)
1971 return match;
1972 x.bitfield.cpuavx512vl = 0;
1973
3629bb00 1974 cpu = cpu_flags_and (x, cpu);
c0f3af97
L
1975 if (!cpu_flags_all_zero (&cpu))
1976 {
57392598 1977 if (x.bitfield.cpuavx)
a5ff0eb2 1978 {
929f69fa 1979 /* We need to check a few extra flags with AVX. */
b9d49817 1980 if (cpu.bitfield.cpuavx
40d231b4
JB
1981 && (!t->opcode_modifier.sse2avx
1982 || (sse2avx && !i.prefix[DATA_PREFIX]))
b9d49817 1983 && (!x.bitfield.cpuaes || cpu.bitfield.cpuaes)
929f69fa 1984 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
b9d49817
JB
1985 && (!x.bitfield.cpupclmul || cpu.bitfield.cpupclmul))
1986 match |= CPU_FLAGS_ARCH_MATCH;
a5ff0eb2 1987 }
929f69fa
JB
1988 else if (x.bitfield.cpuavx512f)
1989 {
1990 /* We need to check a few extra flags with AVX512F. */
1991 if (cpu.bitfield.cpuavx512f
1992 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
1993 && (!x.bitfield.cpuvaes || cpu.bitfield.cpuvaes)
1994 && (!x.bitfield.cpuvpclmulqdq || cpu.bitfield.cpuvpclmulqdq))
1995 match |= CPU_FLAGS_ARCH_MATCH;
1996 }
a5ff0eb2 1997 else
db12e14e 1998 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 1999 }
3629bb00 2000 }
c0f3af97 2001 return match;
40fb9820
L
2002}
2003
c6fb90c8
L
2004static INLINE i386_operand_type
2005operand_type_and (i386_operand_type x, i386_operand_type y)
40fb9820 2006{
bab6aec1
JB
2007 if (x.bitfield.class != y.bitfield.class)
2008 x.bitfield.class = ClassNone;
75e5731b
JB
2009 if (x.bitfield.instance != y.bitfield.instance)
2010 x.bitfield.instance = InstanceNone;
bab6aec1 2011
c6fb90c8
L
2012 switch (ARRAY_SIZE (x.array))
2013 {
2014 case 3:
2015 x.array [2] &= y.array [2];
1a0670f3 2016 /* Fall through. */
c6fb90c8
L
2017 case 2:
2018 x.array [1] &= y.array [1];
1a0670f3 2019 /* Fall through. */
c6fb90c8
L
2020 case 1:
2021 x.array [0] &= y.array [0];
2022 break;
2023 default:
2024 abort ();
2025 }
2026 return x;
40fb9820
L
2027}
2028
73053c1f
JB
2029static INLINE i386_operand_type
2030operand_type_and_not (i386_operand_type x, i386_operand_type y)
2031{
bab6aec1 2032 gas_assert (y.bitfield.class == ClassNone);
75e5731b 2033 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 2034
73053c1f
JB
2035 switch (ARRAY_SIZE (x.array))
2036 {
2037 case 3:
2038 x.array [2] &= ~y.array [2];
2039 /* Fall through. */
2040 case 2:
2041 x.array [1] &= ~y.array [1];
2042 /* Fall through. */
2043 case 1:
2044 x.array [0] &= ~y.array [0];
2045 break;
2046 default:
2047 abort ();
2048 }
2049 return x;
2050}
2051
c6fb90c8
L
2052static INLINE i386_operand_type
2053operand_type_or (i386_operand_type x, i386_operand_type y)
40fb9820 2054{
bab6aec1
JB
2055 gas_assert (x.bitfield.class == ClassNone ||
2056 y.bitfield.class == ClassNone ||
2057 x.bitfield.class == y.bitfield.class);
75e5731b
JB
2058 gas_assert (x.bitfield.instance == InstanceNone ||
2059 y.bitfield.instance == InstanceNone ||
2060 x.bitfield.instance == y.bitfield.instance);
bab6aec1 2061
c6fb90c8 2062 switch (ARRAY_SIZE (x.array))
40fb9820 2063 {
c6fb90c8
L
2064 case 3:
2065 x.array [2] |= y.array [2];
1a0670f3 2066 /* Fall through. */
c6fb90c8
L
2067 case 2:
2068 x.array [1] |= y.array [1];
1a0670f3 2069 /* Fall through. */
c6fb90c8
L
2070 case 1:
2071 x.array [0] |= y.array [0];
40fb9820
L
2072 break;
2073 default:
2074 abort ();
2075 }
c6fb90c8
L
2076 return x;
2077}
40fb9820 2078
c6fb90c8
L
2079static INLINE i386_operand_type
2080operand_type_xor (i386_operand_type x, i386_operand_type y)
2081{
bab6aec1 2082 gas_assert (y.bitfield.class == ClassNone);
75e5731b 2083 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 2084
c6fb90c8
L
2085 switch (ARRAY_SIZE (x.array))
2086 {
2087 case 3:
2088 x.array [2] ^= y.array [2];
1a0670f3 2089 /* Fall through. */
c6fb90c8
L
2090 case 2:
2091 x.array [1] ^= y.array [1];
1a0670f3 2092 /* Fall through. */
c6fb90c8
L
2093 case 1:
2094 x.array [0] ^= y.array [0];
2095 break;
2096 default:
2097 abort ();
2098 }
40fb9820
L
2099 return x;
2100}
2101
40fb9820 2102static const i386_operand_type disp16_32 = OPERAND_TYPE_DISP16_32;
bab6aec1
JB
2103static const i386_operand_type anydisp = OPERAND_TYPE_ANYDISP;
2104static const i386_operand_type anyimm = OPERAND_TYPE_ANYIMM;
40fb9820 2105static const i386_operand_type regxmm = OPERAND_TYPE_REGXMM;
43234a1e 2106static const i386_operand_type regmask = OPERAND_TYPE_REGMASK;
40fb9820
L
2107static const i386_operand_type imm8 = OPERAND_TYPE_IMM8;
2108static const i386_operand_type imm8s = OPERAND_TYPE_IMM8S;
2109static const i386_operand_type imm16 = OPERAND_TYPE_IMM16;
2110static const i386_operand_type imm32 = OPERAND_TYPE_IMM32;
2111static const i386_operand_type imm32s = OPERAND_TYPE_IMM32S;
2112static const i386_operand_type imm64 = OPERAND_TYPE_IMM64;
2113static const i386_operand_type imm16_32 = OPERAND_TYPE_IMM16_32;
2114static const i386_operand_type imm16_32s = OPERAND_TYPE_IMM16_32S;
2115static const i386_operand_type imm16_32_32s = OPERAND_TYPE_IMM16_32_32S;
2116
2117enum operand_type
2118{
2119 reg,
40fb9820
L
2120 imm,
2121 disp,
2122 anymem
2123};
2124
c6fb90c8 2125static INLINE int
40fb9820
L
2126operand_type_check (i386_operand_type t, enum operand_type c)
2127{
2128 switch (c)
2129 {
2130 case reg:
bab6aec1 2131 return t.bitfield.class == Reg;
40fb9820 2132
40fb9820
L
2133 case imm:
2134 return (t.bitfield.imm8
2135 || t.bitfield.imm8s
2136 || t.bitfield.imm16
2137 || t.bitfield.imm32
2138 || t.bitfield.imm32s
2139 || t.bitfield.imm64);
2140
2141 case disp:
2142 return (t.bitfield.disp8
2143 || t.bitfield.disp16
2144 || t.bitfield.disp32
2145 || t.bitfield.disp32s
2146 || t.bitfield.disp64);
2147
2148 case anymem:
2149 return (t.bitfield.disp8
2150 || t.bitfield.disp16
2151 || t.bitfield.disp32
2152 || t.bitfield.disp32s
2153 || t.bitfield.disp64
2154 || t.bitfield.baseindex);
2155
2156 default:
2157 abort ();
2158 }
2cfe26b6
AM
2159
2160 return 0;
40fb9820
L
2161}
2162
7a54636a
L
2163/* Return 1 if there is no conflict in 8bit/16bit/32bit/64bit/80bit size
2164 between operand GIVEN and opeand WANTED for instruction template T. */
5c07affc
L
2165
2166static INLINE int
7a54636a
L
2167match_operand_size (const insn_template *t, unsigned int wanted,
2168 unsigned int given)
5c07affc 2169{
3ac21baa
JB
2170 return !((i.types[given].bitfield.byte
2171 && !t->operand_types[wanted].bitfield.byte)
2172 || (i.types[given].bitfield.word
2173 && !t->operand_types[wanted].bitfield.word)
2174 || (i.types[given].bitfield.dword
2175 && !t->operand_types[wanted].bitfield.dword)
2176 || (i.types[given].bitfield.qword
2177 && !t->operand_types[wanted].bitfield.qword)
2178 || (i.types[given].bitfield.tbyte
2179 && !t->operand_types[wanted].bitfield.tbyte));
5c07affc
L
2180}
2181
dd40ce22
L
2182/* Return 1 if there is no conflict in SIMD register between operand
2183 GIVEN and opeand WANTED for instruction template T. */
1b54b8d7
JB
2184
2185static INLINE int
dd40ce22
L
2186match_simd_size (const insn_template *t, unsigned int wanted,
2187 unsigned int given)
1b54b8d7 2188{
3ac21baa
JB
2189 return !((i.types[given].bitfield.xmmword
2190 && !t->operand_types[wanted].bitfield.xmmword)
2191 || (i.types[given].bitfield.ymmword
2192 && !t->operand_types[wanted].bitfield.ymmword)
2193 || (i.types[given].bitfield.zmmword
260cd341
LC
2194 && !t->operand_types[wanted].bitfield.zmmword)
2195 || (i.types[given].bitfield.tmmword
2196 && !t->operand_types[wanted].bitfield.tmmword));
1b54b8d7
JB
2197}
2198
7a54636a
L
2199/* Return 1 if there is no conflict in any size between operand GIVEN
2200 and opeand WANTED for instruction template T. */
5c07affc
L
2201
2202static INLINE int
dd40ce22
L
2203match_mem_size (const insn_template *t, unsigned int wanted,
2204 unsigned int given)
5c07affc 2205{
7a54636a 2206 return (match_operand_size (t, wanted, given)
3ac21baa 2207 && !((i.types[given].bitfield.unspecified
5273a3cd 2208 && !i.broadcast.type
3ac21baa
JB
2209 && !t->operand_types[wanted].bitfield.unspecified)
2210 || (i.types[given].bitfield.fword
2211 && !t->operand_types[wanted].bitfield.fword)
1b54b8d7
JB
2212 /* For scalar opcode templates to allow register and memory
2213 operands at the same time, some special casing is needed
d6793fa1
JB
2214 here. Also for v{,p}broadcast*, {,v}pmov{s,z}*, and
2215 down-conversion vpmov*. */
3528c362 2216 || ((t->operand_types[wanted].bitfield.class == RegSIMD
bc49bfd8
JB
2217 && t->operand_types[wanted].bitfield.byte
2218 + t->operand_types[wanted].bitfield.word
2219 + t->operand_types[wanted].bitfield.dword
2220 + t->operand_types[wanted].bitfield.qword
2221 > !!t->opcode_modifier.broadcast)
3ac21baa
JB
2222 ? (i.types[given].bitfield.xmmword
2223 || i.types[given].bitfield.ymmword
2224 || i.types[given].bitfield.zmmword)
2225 : !match_simd_size(t, wanted, given))));
5c07affc
L
2226}
2227
3ac21baa
JB
2228/* Return value has MATCH_STRAIGHT set if there is no size conflict on any
2229 operands for instruction template T, and it has MATCH_REVERSE set if there
2230 is no size conflict on any operands for the template with operands reversed
2231 (and the template allows for reversing in the first place). */
5c07affc 2232
3ac21baa
JB
2233#define MATCH_STRAIGHT 1
2234#define MATCH_REVERSE 2
2235
2236static INLINE unsigned int
d3ce72d0 2237operand_size_match (const insn_template *t)
5c07affc 2238{
3ac21baa 2239 unsigned int j, match = MATCH_STRAIGHT;
5c07affc 2240
0cfa3eb3 2241 /* Don't check non-absolute jump instructions. */
5c07affc 2242 if (t->opcode_modifier.jump
0cfa3eb3 2243 && t->opcode_modifier.jump != JUMP_ABSOLUTE)
5c07affc
L
2244 return match;
2245
2246 /* Check memory and accumulator operand size. */
2247 for (j = 0; j < i.operands; j++)
2248 {
3528c362
JB
2249 if (i.types[j].bitfield.class != Reg
2250 && i.types[j].bitfield.class != RegSIMD
601e8564 2251 && t->opcode_modifier.anysize)
5c07affc
L
2252 continue;
2253
bab6aec1 2254 if (t->operand_types[j].bitfield.class == Reg
7a54636a 2255 && !match_operand_size (t, j, j))
5c07affc
L
2256 {
2257 match = 0;
2258 break;
2259 }
2260
3528c362 2261 if (t->operand_types[j].bitfield.class == RegSIMD
3ac21baa 2262 && !match_simd_size (t, j, j))
1b54b8d7
JB
2263 {
2264 match = 0;
2265 break;
2266 }
2267
75e5731b 2268 if (t->operand_types[j].bitfield.instance == Accum
7a54636a 2269 && (!match_operand_size (t, j, j) || !match_simd_size (t, j, j)))
1b54b8d7
JB
2270 {
2271 match = 0;
2272 break;
2273 }
2274
c48dadc9 2275 if ((i.flags[j] & Operand_Mem) && !match_mem_size (t, j, j))
5c07affc
L
2276 {
2277 match = 0;
2278 break;
2279 }
2280 }
2281
3ac21baa 2282 if (!t->opcode_modifier.d)
891edac4 2283 {
dc1e8a47 2284 mismatch:
3ac21baa
JB
2285 if (!match)
2286 i.error = operand_size_mismatch;
2287 return match;
891edac4 2288 }
5c07affc
L
2289
2290 /* Check reverse. */
f5eb1d70 2291 gas_assert (i.operands >= 2 && i.operands <= 3);
5c07affc 2292
f5eb1d70 2293 for (j = 0; j < i.operands; j++)
5c07affc 2294 {
f5eb1d70
JB
2295 unsigned int given = i.operands - j - 1;
2296
bab6aec1 2297 if (t->operand_types[j].bitfield.class == Reg
f5eb1d70 2298 && !match_operand_size (t, j, given))
891edac4 2299 goto mismatch;
5c07affc 2300
3528c362 2301 if (t->operand_types[j].bitfield.class == RegSIMD
f5eb1d70 2302 && !match_simd_size (t, j, given))
dbbc8b7e
JB
2303 goto mismatch;
2304
75e5731b 2305 if (t->operand_types[j].bitfield.instance == Accum
f5eb1d70
JB
2306 && (!match_operand_size (t, j, given)
2307 || !match_simd_size (t, j, given)))
dbbc8b7e
JB
2308 goto mismatch;
2309
f5eb1d70 2310 if ((i.flags[given] & Operand_Mem) && !match_mem_size (t, j, given))
891edac4 2311 goto mismatch;
5c07affc
L
2312 }
2313
3ac21baa 2314 return match | MATCH_REVERSE;
5c07affc
L
2315}
2316
c6fb90c8 2317static INLINE int
40fb9820
L
2318operand_type_match (i386_operand_type overlap,
2319 i386_operand_type given)
2320{
2321 i386_operand_type temp = overlap;
2322
7d5e4556 2323 temp.bitfield.unspecified = 0;
5c07affc
L
2324 temp.bitfield.byte = 0;
2325 temp.bitfield.word = 0;
2326 temp.bitfield.dword = 0;
2327 temp.bitfield.fword = 0;
2328 temp.bitfield.qword = 0;
2329 temp.bitfield.tbyte = 0;
2330 temp.bitfield.xmmword = 0;
c0f3af97 2331 temp.bitfield.ymmword = 0;
43234a1e 2332 temp.bitfield.zmmword = 0;
260cd341 2333 temp.bitfield.tmmword = 0;
0dfbf9d7 2334 if (operand_type_all_zero (&temp))
891edac4 2335 goto mismatch;
40fb9820 2336
6f2f06be 2337 if (given.bitfield.baseindex == overlap.bitfield.baseindex)
891edac4
L
2338 return 1;
2339
dc1e8a47 2340 mismatch:
a65babc9 2341 i.error = operand_type_mismatch;
891edac4 2342 return 0;
40fb9820
L
2343}
2344
7d5e4556 2345/* If given types g0 and g1 are registers they must be of the same type
10c17abd 2346 unless the expected operand type register overlap is null.
5de4d9ef 2347 Some Intel syntax memory operand size checking also happens here. */
40fb9820 2348
c6fb90c8 2349static INLINE int
dc821c5f 2350operand_type_register_match (i386_operand_type g0,
40fb9820 2351 i386_operand_type t0,
40fb9820
L
2352 i386_operand_type g1,
2353 i386_operand_type t1)
2354{
bab6aec1 2355 if (g0.bitfield.class != Reg
3528c362 2356 && g0.bitfield.class != RegSIMD
10c17abd
JB
2357 && (!operand_type_check (g0, anymem)
2358 || g0.bitfield.unspecified
5de4d9ef
JB
2359 || (t0.bitfield.class != Reg
2360 && t0.bitfield.class != RegSIMD)))
40fb9820
L
2361 return 1;
2362
bab6aec1 2363 if (g1.bitfield.class != Reg
3528c362 2364 && g1.bitfield.class != RegSIMD
10c17abd
JB
2365 && (!operand_type_check (g1, anymem)
2366 || g1.bitfield.unspecified
5de4d9ef
JB
2367 || (t1.bitfield.class != Reg
2368 && t1.bitfield.class != RegSIMD)))
40fb9820
L
2369 return 1;
2370
dc821c5f
JB
2371 if (g0.bitfield.byte == g1.bitfield.byte
2372 && g0.bitfield.word == g1.bitfield.word
2373 && g0.bitfield.dword == g1.bitfield.dword
10c17abd
JB
2374 && g0.bitfield.qword == g1.bitfield.qword
2375 && g0.bitfield.xmmword == g1.bitfield.xmmword
2376 && g0.bitfield.ymmword == g1.bitfield.ymmword
2377 && g0.bitfield.zmmword == g1.bitfield.zmmword)
40fb9820
L
2378 return 1;
2379
dc821c5f
JB
2380 if (!(t0.bitfield.byte & t1.bitfield.byte)
2381 && !(t0.bitfield.word & t1.bitfield.word)
2382 && !(t0.bitfield.dword & t1.bitfield.dword)
10c17abd
JB
2383 && !(t0.bitfield.qword & t1.bitfield.qword)
2384 && !(t0.bitfield.xmmword & t1.bitfield.xmmword)
2385 && !(t0.bitfield.ymmword & t1.bitfield.ymmword)
2386 && !(t0.bitfield.zmmword & t1.bitfield.zmmword))
891edac4
L
2387 return 1;
2388
a65babc9 2389 i.error = register_type_mismatch;
891edac4
L
2390
2391 return 0;
40fb9820
L
2392}
2393
4c692bc7
JB
2394static INLINE unsigned int
2395register_number (const reg_entry *r)
2396{
2397 unsigned int nr = r->reg_num;
2398
2399 if (r->reg_flags & RegRex)
2400 nr += 8;
2401
200cbe0f
L
2402 if (r->reg_flags & RegVRex)
2403 nr += 16;
2404
4c692bc7
JB
2405 return nr;
2406}
2407
252b5132 2408static INLINE unsigned int
40fb9820 2409mode_from_disp_size (i386_operand_type t)
252b5132 2410{
b5014f7a 2411 if (t.bitfield.disp8)
40fb9820
L
2412 return 1;
2413 else if (t.bitfield.disp16
2414 || t.bitfield.disp32
2415 || t.bitfield.disp32s)
2416 return 2;
2417 else
2418 return 0;
252b5132
RH
2419}
2420
2421static INLINE int
65879393 2422fits_in_signed_byte (addressT num)
252b5132 2423{
65879393 2424 return num + 0x80 <= 0xff;
47926f60 2425}
252b5132
RH
2426
2427static INLINE int
65879393 2428fits_in_unsigned_byte (addressT num)
252b5132 2429{
65879393 2430 return num <= 0xff;
47926f60 2431}
252b5132
RH
2432
2433static INLINE int
65879393 2434fits_in_unsigned_word (addressT num)
252b5132 2435{
65879393 2436 return num <= 0xffff;
47926f60 2437}
252b5132
RH
2438
2439static INLINE int
65879393 2440fits_in_signed_word (addressT num)
252b5132 2441{
65879393 2442 return num + 0x8000 <= 0xffff;
47926f60 2443}
2a962e6d 2444
3e73aa7c 2445static INLINE int
65879393 2446fits_in_signed_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2447{
2448#ifndef BFD64
2449 return 1;
2450#else
65879393 2451 return num + 0x80000000 <= 0xffffffff;
3e73aa7c
JH
2452#endif
2453} /* fits_in_signed_long() */
2a962e6d 2454
3e73aa7c 2455static INLINE int
65879393 2456fits_in_unsigned_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2457{
2458#ifndef BFD64
2459 return 1;
2460#else
65879393 2461 return num <= 0xffffffff;
3e73aa7c
JH
2462#endif
2463} /* fits_in_unsigned_long() */
252b5132 2464
a442cac5
JB
2465static INLINE valueT extend_to_32bit_address (addressT num)
2466{
2467#ifdef BFD64
2468 if (fits_in_unsigned_long(num))
2469 return (num ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
2470
2471 if (!fits_in_signed_long (num))
2472 return num & 0xffffffff;
2473#endif
2474
2475 return num;
2476}
2477
43234a1e 2478static INLINE int
b5014f7a 2479fits_in_disp8 (offsetT num)
43234a1e
L
2480{
2481 int shift = i.memshift;
2482 unsigned int mask;
2483
2484 if (shift == -1)
2485 abort ();
2486
2487 mask = (1 << shift) - 1;
2488
2489 /* Return 0 if NUM isn't properly aligned. */
2490 if ((num & mask))
2491 return 0;
2492
2493 /* Check if NUM will fit in 8bit after shift. */
2494 return fits_in_signed_byte (num >> shift);
2495}
2496
a683cc34
SP
2497static INLINE int
2498fits_in_imm4 (offsetT num)
2499{
2500 return (num & 0xf) == num;
2501}
2502
40fb9820 2503static i386_operand_type
e3bb37b5 2504smallest_imm_type (offsetT num)
252b5132 2505{
40fb9820 2506 i386_operand_type t;
7ab9ffdd 2507
0dfbf9d7 2508 operand_type_set (&t, 0);
40fb9820
L
2509 t.bitfield.imm64 = 1;
2510
2511 if (cpu_arch_tune != PROCESSOR_I486 && num == 1)
e413e4e9
AM
2512 {
2513 /* This code is disabled on the 486 because all the Imm1 forms
2514 in the opcode table are slower on the i486. They're the
2515 versions with the implicitly specified single-position
2516 displacement, which has another syntax if you really want to
2517 use that form. */
40fb9820
L
2518 t.bitfield.imm1 = 1;
2519 t.bitfield.imm8 = 1;
2520 t.bitfield.imm8s = 1;
2521 t.bitfield.imm16 = 1;
2522 t.bitfield.imm32 = 1;
2523 t.bitfield.imm32s = 1;
2524 }
2525 else if (fits_in_signed_byte (num))
2526 {
2527 t.bitfield.imm8 = 1;
2528 t.bitfield.imm8s = 1;
2529 t.bitfield.imm16 = 1;
2530 t.bitfield.imm32 = 1;
2531 t.bitfield.imm32s = 1;
2532 }
2533 else if (fits_in_unsigned_byte (num))
2534 {
2535 t.bitfield.imm8 = 1;
2536 t.bitfield.imm16 = 1;
2537 t.bitfield.imm32 = 1;
2538 t.bitfield.imm32s = 1;
2539 }
2540 else if (fits_in_signed_word (num) || fits_in_unsigned_word (num))
2541 {
2542 t.bitfield.imm16 = 1;
2543 t.bitfield.imm32 = 1;
2544 t.bitfield.imm32s = 1;
2545 }
2546 else if (fits_in_signed_long (num))
2547 {
2548 t.bitfield.imm32 = 1;
2549 t.bitfield.imm32s = 1;
2550 }
2551 else if (fits_in_unsigned_long (num))
2552 t.bitfield.imm32 = 1;
2553
2554 return t;
47926f60 2555}
252b5132 2556
847f7ad4 2557static offsetT
e3bb37b5 2558offset_in_range (offsetT val, int size)
847f7ad4 2559{
508866be 2560 addressT mask;
ba2adb93 2561
847f7ad4
AM
2562 switch (size)
2563 {
508866be
L
2564 case 1: mask = ((addressT) 1 << 8) - 1; break;
2565 case 2: mask = ((addressT) 1 << 16) - 1; break;
3e73aa7c 2566#ifdef BFD64
64965897 2567 case 4: mask = ((addressT) 1 << 32) - 1; break;
3e73aa7c 2568#endif
64965897 2569 case sizeof (val): return val;
47926f60 2570 default: abort ();
847f7ad4
AM
2571 }
2572
4fe51f7d 2573 if ((val & ~mask) != 0 && (-val & ~mask) != 0)
ffa5352c
NC
2574 {
2575 char val_buf[128];
2576 char masked_buf[128];
2577
2578 /* Coded this way in order to ease translation. */
2579 sprintf_vma (val_buf, val);
2580 sprintf_vma (masked_buf, val & mask);
2581 as_warn (_("0x%s shortened to 0x%s"), val_buf, masked_buf);
2582 }
847f7ad4 2583
847f7ad4
AM
2584 return val & mask;
2585}
2586
c32fa91d
L
2587enum PREFIX_GROUP
2588{
2589 PREFIX_EXIST = 0,
2590 PREFIX_LOCK,
2591 PREFIX_REP,
04ef582a 2592 PREFIX_DS,
c32fa91d
L
2593 PREFIX_OTHER
2594};
2595
2596/* Returns
2597 a. PREFIX_EXIST if attempting to add a prefix where one from the
2598 same class already exists.
2599 b. PREFIX_LOCK if lock prefix is added.
2600 c. PREFIX_REP if rep/repne prefix is added.
04ef582a
L
2601 d. PREFIX_DS if ds prefix is added.
2602 e. PREFIX_OTHER if other prefix is added.
c32fa91d
L
2603 */
2604
2605static enum PREFIX_GROUP
e3bb37b5 2606add_prefix (unsigned int prefix)
252b5132 2607{
c32fa91d 2608 enum PREFIX_GROUP ret = PREFIX_OTHER;
b1905489 2609 unsigned int q;
252b5132 2610
29b0f896
AM
2611 if (prefix >= REX_OPCODE && prefix < REX_OPCODE + 16
2612 && flag_code == CODE_64BIT)
b1905489 2613 {
161a04f6 2614 if ((i.prefix[REX_PREFIX] & prefix & REX_W)
44846f29
JB
2615 || (i.prefix[REX_PREFIX] & prefix & REX_R)
2616 || (i.prefix[REX_PREFIX] & prefix & REX_X)
2617 || (i.prefix[REX_PREFIX] & prefix & REX_B))
c32fa91d 2618 ret = PREFIX_EXIST;
b1905489
JB
2619 q = REX_PREFIX;
2620 }
3e73aa7c 2621 else
b1905489
JB
2622 {
2623 switch (prefix)
2624 {
2625 default:
2626 abort ();
2627
b1905489 2628 case DS_PREFIX_OPCODE:
04ef582a
L
2629 ret = PREFIX_DS;
2630 /* Fall through. */
2631 case CS_PREFIX_OPCODE:
b1905489
JB
2632 case ES_PREFIX_OPCODE:
2633 case FS_PREFIX_OPCODE:
2634 case GS_PREFIX_OPCODE:
2635 case SS_PREFIX_OPCODE:
2636 q = SEG_PREFIX;
2637 break;
2638
2639 case REPNE_PREFIX_OPCODE:
2640 case REPE_PREFIX_OPCODE:
c32fa91d
L
2641 q = REP_PREFIX;
2642 ret = PREFIX_REP;
2643 break;
2644
b1905489 2645 case LOCK_PREFIX_OPCODE:
c32fa91d
L
2646 q = LOCK_PREFIX;
2647 ret = PREFIX_LOCK;
b1905489
JB
2648 break;
2649
2650 case FWAIT_OPCODE:
2651 q = WAIT_PREFIX;
2652 break;
2653
2654 case ADDR_PREFIX_OPCODE:
2655 q = ADDR_PREFIX;
2656 break;
2657
2658 case DATA_PREFIX_OPCODE:
2659 q = DATA_PREFIX;
2660 break;
2661 }
2662 if (i.prefix[q] != 0)
c32fa91d 2663 ret = PREFIX_EXIST;
b1905489 2664 }
252b5132 2665
b1905489 2666 if (ret)
252b5132 2667 {
b1905489
JB
2668 if (!i.prefix[q])
2669 ++i.prefixes;
2670 i.prefix[q] |= prefix;
252b5132 2671 }
b1905489
JB
2672 else
2673 as_bad (_("same type of prefix used twice"));
252b5132 2674
252b5132
RH
2675 return ret;
2676}
2677
2678static void
78f12dd3 2679update_code_flag (int value, int check)
eecb386c 2680{
78f12dd3
L
2681 PRINTF_LIKE ((*as_error));
2682
1e9cc1c2 2683 flag_code = (enum flag_code) value;
40fb9820
L
2684 if (flag_code == CODE_64BIT)
2685 {
2686 cpu_arch_flags.bitfield.cpu64 = 1;
2687 cpu_arch_flags.bitfield.cpuno64 = 0;
40fb9820
L
2688 }
2689 else
2690 {
2691 cpu_arch_flags.bitfield.cpu64 = 0;
2692 cpu_arch_flags.bitfield.cpuno64 = 1;
40fb9820
L
2693 }
2694 if (value == CODE_64BIT && !cpu_arch_flags.bitfield.cpulm )
3e73aa7c 2695 {
78f12dd3
L
2696 if (check)
2697 as_error = as_fatal;
2698 else
2699 as_error = as_bad;
2700 (*as_error) (_("64bit mode not supported on `%s'."),
2701 cpu_arch_name ? cpu_arch_name : default_arch);
3e73aa7c 2702 }
40fb9820 2703 if (value == CODE_32BIT && !cpu_arch_flags.bitfield.cpui386)
3e73aa7c 2704 {
78f12dd3
L
2705 if (check)
2706 as_error = as_fatal;
2707 else
2708 as_error = as_bad;
2709 (*as_error) (_("32bit mode not supported on `%s'."),
2710 cpu_arch_name ? cpu_arch_name : default_arch);
3e73aa7c 2711 }
eecb386c
AM
2712 stackop_size = '\0';
2713}
2714
78f12dd3
L
2715static void
2716set_code_flag (int value)
2717{
2718 update_code_flag (value, 0);
2719}
2720
eecb386c 2721static void
e3bb37b5 2722set_16bit_gcc_code_flag (int new_code_flag)
252b5132 2723{
1e9cc1c2 2724 flag_code = (enum flag_code) new_code_flag;
40fb9820
L
2725 if (flag_code != CODE_16BIT)
2726 abort ();
2727 cpu_arch_flags.bitfield.cpu64 = 0;
2728 cpu_arch_flags.bitfield.cpuno64 = 1;
9306ca4a 2729 stackop_size = LONG_MNEM_SUFFIX;
252b5132
RH
2730}
2731
2732static void
e3bb37b5 2733set_intel_syntax (int syntax_flag)
252b5132
RH
2734{
2735 /* Find out if register prefixing is specified. */
2736 int ask_naked_reg = 0;
2737
2738 SKIP_WHITESPACE ();
29b0f896 2739 if (!is_end_of_line[(unsigned char) *input_line_pointer])
252b5132 2740 {
d02603dc
NC
2741 char *string;
2742 int e = get_symbol_name (&string);
252b5132 2743
47926f60 2744 if (strcmp (string, "prefix") == 0)
252b5132 2745 ask_naked_reg = 1;
47926f60 2746 else if (strcmp (string, "noprefix") == 0)
252b5132
RH
2747 ask_naked_reg = -1;
2748 else
d0b47220 2749 as_bad (_("bad argument to syntax directive."));
d02603dc 2750 (void) restore_line_pointer (e);
252b5132
RH
2751 }
2752 demand_empty_rest_of_line ();
c3332e24 2753
252b5132
RH
2754 intel_syntax = syntax_flag;
2755
2756 if (ask_naked_reg == 0)
f86103b7
AM
2757 allow_naked_reg = (intel_syntax
2758 && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
252b5132
RH
2759 else
2760 allow_naked_reg = (ask_naked_reg < 0);
9306ca4a 2761
ee86248c 2762 expr_set_rank (O_full_ptr, syntax_flag ? 10 : 0);
7ab9ffdd 2763
e4a3b5a4 2764 identifier_chars['%'] = intel_syntax && allow_naked_reg ? '%' : 0;
9306ca4a 2765 identifier_chars['$'] = intel_syntax ? '$' : 0;
e4a3b5a4 2766 register_prefix = allow_naked_reg ? "" : "%";
252b5132
RH
2767}
2768
1efbbeb4
L
2769static void
2770set_intel_mnemonic (int mnemonic_flag)
2771{
e1d4d893 2772 intel_mnemonic = mnemonic_flag;
1efbbeb4
L
2773}
2774
db51cc60
L
2775static void
2776set_allow_index_reg (int flag)
2777{
2778 allow_index_reg = flag;
2779}
2780
cb19c032 2781static void
7bab8ab5 2782set_check (int what)
cb19c032 2783{
7bab8ab5
JB
2784 enum check_kind *kind;
2785 const char *str;
2786
2787 if (what)
2788 {
2789 kind = &operand_check;
2790 str = "operand";
2791 }
2792 else
2793 {
2794 kind = &sse_check;
2795 str = "sse";
2796 }
2797
cb19c032
L
2798 SKIP_WHITESPACE ();
2799
2800 if (!is_end_of_line[(unsigned char) *input_line_pointer])
2801 {
d02603dc
NC
2802 char *string;
2803 int e = get_symbol_name (&string);
cb19c032
L
2804
2805 if (strcmp (string, "none") == 0)
7bab8ab5 2806 *kind = check_none;
cb19c032 2807 else if (strcmp (string, "warning") == 0)
7bab8ab5 2808 *kind = check_warning;
cb19c032 2809 else if (strcmp (string, "error") == 0)
7bab8ab5 2810 *kind = check_error;
cb19c032 2811 else
7bab8ab5 2812 as_bad (_("bad argument to %s_check directive."), str);
d02603dc 2813 (void) restore_line_pointer (e);
cb19c032
L
2814 }
2815 else
7bab8ab5 2816 as_bad (_("missing argument for %s_check directive"), str);
cb19c032
L
2817
2818 demand_empty_rest_of_line ();
2819}
2820
8a9036a4
L
2821static void
2822check_cpu_arch_compatible (const char *name ATTRIBUTE_UNUSED,
1e9cc1c2 2823 i386_cpu_flags new_flag ATTRIBUTE_UNUSED)
8a9036a4
L
2824{
2825#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2826 static const char *arch;
2827
2828 /* Intel LIOM is only supported on ELF. */
2829 if (!IS_ELF)
2830 return;
2831
2832 if (!arch)
2833 {
2834 /* Use cpu_arch_name if it is set in md_parse_option. Otherwise
2835 use default_arch. */
2836 arch = cpu_arch_name;
2837 if (!arch)
2838 arch = default_arch;
2839 }
2840
81486035
L
2841 /* If we are targeting Intel MCU, we must enable it. */
2842 if (get_elf_backend_data (stdoutput)->elf_machine_code != EM_IAMCU
2843 || new_flag.bitfield.cpuiamcu)
2844 return;
2845
3632d14b 2846 /* If we are targeting Intel L1OM, we must enable it. */
8a9036a4 2847 if (get_elf_backend_data (stdoutput)->elf_machine_code != EM_L1OM
1e9cc1c2 2848 || new_flag.bitfield.cpul1om)
8a9036a4 2849 return;
76ba9986 2850
7a9068fe
L
2851 /* If we are targeting Intel K1OM, we must enable it. */
2852 if (get_elf_backend_data (stdoutput)->elf_machine_code != EM_K1OM
2853 || new_flag.bitfield.cpuk1om)
2854 return;
2855
8a9036a4
L
2856 as_bad (_("`%s' is not supported on `%s'"), name, arch);
2857#endif
2858}
2859
e413e4e9 2860static void
e3bb37b5 2861set_cpu_arch (int dummy ATTRIBUTE_UNUSED)
e413e4e9 2862{
47926f60 2863 SKIP_WHITESPACE ();
e413e4e9 2864
29b0f896 2865 if (!is_end_of_line[(unsigned char) *input_line_pointer])
e413e4e9 2866 {
d02603dc
NC
2867 char *string;
2868 int e = get_symbol_name (&string);
91d6fa6a 2869 unsigned int j;
40fb9820 2870 i386_cpu_flags flags;
e413e4e9 2871
91d6fa6a 2872 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
e413e4e9 2873 {
91d6fa6a 2874 if (strcmp (string, cpu_arch[j].name) == 0)
e413e4e9 2875 {
91d6fa6a 2876 check_cpu_arch_compatible (string, cpu_arch[j].flags);
8a9036a4 2877
5c6af06e
JB
2878 if (*string != '.')
2879 {
91d6fa6a 2880 cpu_arch_name = cpu_arch[j].name;
5c6af06e 2881 cpu_sub_arch_name = NULL;
91d6fa6a 2882 cpu_arch_flags = cpu_arch[j].flags;
40fb9820
L
2883 if (flag_code == CODE_64BIT)
2884 {
2885 cpu_arch_flags.bitfield.cpu64 = 1;
2886 cpu_arch_flags.bitfield.cpuno64 = 0;
2887 }
2888 else
2889 {
2890 cpu_arch_flags.bitfield.cpu64 = 0;
2891 cpu_arch_flags.bitfield.cpuno64 = 1;
2892 }
91d6fa6a
NC
2893 cpu_arch_isa = cpu_arch[j].type;
2894 cpu_arch_isa_flags = cpu_arch[j].flags;
ccc9c027
L
2895 if (!cpu_arch_tune_set)
2896 {
2897 cpu_arch_tune = cpu_arch_isa;
2898 cpu_arch_tune_flags = cpu_arch_isa_flags;
2899 }
5c6af06e
JB
2900 break;
2901 }
40fb9820 2902
293f5f65
L
2903 flags = cpu_flags_or (cpu_arch_flags,
2904 cpu_arch[j].flags);
81486035 2905
5b64d091 2906 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
5c6af06e 2907 {
6305a203
L
2908 if (cpu_sub_arch_name)
2909 {
2910 char *name = cpu_sub_arch_name;
2911 cpu_sub_arch_name = concat (name,
91d6fa6a 2912 cpu_arch[j].name,
1bf57e9f 2913 (const char *) NULL);
6305a203
L
2914 free (name);
2915 }
2916 else
91d6fa6a 2917 cpu_sub_arch_name = xstrdup (cpu_arch[j].name);
40fb9820 2918 cpu_arch_flags = flags;
a586129e 2919 cpu_arch_isa_flags = flags;
5c6af06e 2920 }
0089dace
L
2921 else
2922 cpu_arch_isa_flags
2923 = cpu_flags_or (cpu_arch_isa_flags,
2924 cpu_arch[j].flags);
d02603dc 2925 (void) restore_line_pointer (e);
5c6af06e
JB
2926 demand_empty_rest_of_line ();
2927 return;
e413e4e9
AM
2928 }
2929 }
293f5f65
L
2930
2931 if (*string == '.' && j >= ARRAY_SIZE (cpu_arch))
2932 {
33eaf5de 2933 /* Disable an ISA extension. */
293f5f65
L
2934 for (j = 0; j < ARRAY_SIZE (cpu_noarch); j++)
2935 if (strcmp (string + 1, cpu_noarch [j].name) == 0)
2936 {
2937 flags = cpu_flags_and_not (cpu_arch_flags,
2938 cpu_noarch[j].flags);
2939 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
2940 {
2941 if (cpu_sub_arch_name)
2942 {
2943 char *name = cpu_sub_arch_name;
2944 cpu_sub_arch_name = concat (name, string,
2945 (const char *) NULL);
2946 free (name);
2947 }
2948 else
2949 cpu_sub_arch_name = xstrdup (string);
2950 cpu_arch_flags = flags;
2951 cpu_arch_isa_flags = flags;
2952 }
2953 (void) restore_line_pointer (e);
2954 demand_empty_rest_of_line ();
2955 return;
2956 }
2957
2958 j = ARRAY_SIZE (cpu_arch);
2959 }
2960
91d6fa6a 2961 if (j >= ARRAY_SIZE (cpu_arch))
e413e4e9
AM
2962 as_bad (_("no such architecture: `%s'"), string);
2963
2964 *input_line_pointer = e;
2965 }
2966 else
2967 as_bad (_("missing cpu architecture"));
2968
fddf5b5b
AM
2969 no_cond_jump_promotion = 0;
2970 if (*input_line_pointer == ','
29b0f896 2971 && !is_end_of_line[(unsigned char) input_line_pointer[1]])
fddf5b5b 2972 {
d02603dc
NC
2973 char *string;
2974 char e;
2975
2976 ++input_line_pointer;
2977 e = get_symbol_name (&string);
fddf5b5b
AM
2978
2979 if (strcmp (string, "nojumps") == 0)
2980 no_cond_jump_promotion = 1;
2981 else if (strcmp (string, "jumps") == 0)
2982 ;
2983 else
2984 as_bad (_("no such architecture modifier: `%s'"), string);
2985
d02603dc 2986 (void) restore_line_pointer (e);
fddf5b5b
AM
2987 }
2988
e413e4e9
AM
2989 demand_empty_rest_of_line ();
2990}
2991
8a9036a4
L
2992enum bfd_architecture
2993i386_arch (void)
2994{
3632d14b 2995 if (cpu_arch_isa == PROCESSOR_L1OM)
8a9036a4
L
2996 {
2997 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2998 || flag_code != CODE_64BIT)
2999 as_fatal (_("Intel L1OM is 64bit ELF only"));
3000 return bfd_arch_l1om;
3001 }
7a9068fe
L
3002 else if (cpu_arch_isa == PROCESSOR_K1OM)
3003 {
3004 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
3005 || flag_code != CODE_64BIT)
3006 as_fatal (_("Intel K1OM is 64bit ELF only"));
3007 return bfd_arch_k1om;
3008 }
81486035
L
3009 else if (cpu_arch_isa == PROCESSOR_IAMCU)
3010 {
3011 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
3012 || flag_code == CODE_64BIT)
3013 as_fatal (_("Intel MCU is 32bit ELF only"));
3014 return bfd_arch_iamcu;
3015 }
8a9036a4
L
3016 else
3017 return bfd_arch_i386;
3018}
3019
b9d79e03 3020unsigned long
7016a5d5 3021i386_mach (void)
b9d79e03 3022{
d34049e8 3023 if (startswith (default_arch, "x86_64"))
8a9036a4 3024 {
3632d14b 3025 if (cpu_arch_isa == PROCESSOR_L1OM)
8a9036a4 3026 {
351f65ca
L
3027 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
3028 || default_arch[6] != '\0')
8a9036a4
L
3029 as_fatal (_("Intel L1OM is 64bit ELF only"));
3030 return bfd_mach_l1om;
3031 }
7a9068fe
L
3032 else if (cpu_arch_isa == PROCESSOR_K1OM)
3033 {
3034 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
3035 || default_arch[6] != '\0')
3036 as_fatal (_("Intel K1OM is 64bit ELF only"));
3037 return bfd_mach_k1om;
3038 }
351f65ca 3039 else if (default_arch[6] == '\0')
8a9036a4 3040 return bfd_mach_x86_64;
351f65ca
L
3041 else
3042 return bfd_mach_x64_32;
8a9036a4 3043 }
5197d474
L
3044 else if (!strcmp (default_arch, "i386")
3045 || !strcmp (default_arch, "iamcu"))
81486035
L
3046 {
3047 if (cpu_arch_isa == PROCESSOR_IAMCU)
3048 {
3049 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
3050 as_fatal (_("Intel MCU is 32bit ELF only"));
3051 return bfd_mach_i386_iamcu;
3052 }
3053 else
3054 return bfd_mach_i386_i386;
3055 }
b9d79e03 3056 else
2b5d6a91 3057 as_fatal (_("unknown architecture"));
b9d79e03 3058}
b9d79e03 3059\f
252b5132 3060void
7016a5d5 3061md_begin (void)
252b5132 3062{
86fa6981
L
3063 /* Support pseudo prefixes like {disp32}. */
3064 lex_type ['{'] = LEX_BEGIN_NAME;
3065
47926f60 3066 /* Initialize op_hash hash table. */
629310ab 3067 op_hash = str_htab_create ();
252b5132
RH
3068
3069 {
d3ce72d0 3070 const insn_template *optab;
29b0f896 3071 templates *core_optab;
252b5132 3072
47926f60
KH
3073 /* Setup for loop. */
3074 optab = i386_optab;
add39d23 3075 core_optab = XNEW (templates);
252b5132
RH
3076 core_optab->start = optab;
3077
3078 while (1)
3079 {
3080 ++optab;
3081 if (optab->name == NULL
3082 || strcmp (optab->name, (optab - 1)->name) != 0)
3083 {
3084 /* different name --> ship out current template list;
47926f60 3085 add to hash table; & begin anew. */
252b5132 3086 core_optab->end = optab;
fe0e921f
AM
3087 if (str_hash_insert (op_hash, (optab - 1)->name, core_optab, 0))
3088 as_fatal (_("duplicate %s"), (optab - 1)->name);
3089
252b5132
RH
3090 if (optab->name == NULL)
3091 break;
add39d23 3092 core_optab = XNEW (templates);
252b5132
RH
3093 core_optab->start = optab;
3094 }
3095 }
3096 }
3097
47926f60 3098 /* Initialize reg_hash hash table. */
629310ab 3099 reg_hash = str_htab_create ();
252b5132 3100 {
29b0f896 3101 const reg_entry *regtab;
c3fe08fa 3102 unsigned int regtab_size = i386_regtab_size;
252b5132 3103
c3fe08fa 3104 for (regtab = i386_regtab; regtab_size--; regtab++)
6225c532 3105 {
6288d05f
JB
3106 switch (regtab->reg_type.bitfield.class)
3107 {
3108 case Reg:
34684862
JB
3109 if (regtab->reg_type.bitfield.dword)
3110 {
3111 if (regtab->reg_type.bitfield.instance == Accum)
3112 reg_eax = regtab;
3113 }
3114 else if (regtab->reg_type.bitfield.tbyte)
6288d05f
JB
3115 {
3116 /* There's no point inserting st(<N>) in the hash table, as
3117 parentheses aren't included in register_chars[] anyway. */
3118 if (regtab->reg_type.bitfield.instance != Accum)
3119 continue;
3120 reg_st0 = regtab;
3121 }
3122 break;
3123
5e042380
JB
3124 case SReg:
3125 switch (regtab->reg_num)
3126 {
3127 case 0: reg_es = regtab; break;
3128 case 2: reg_ss = regtab; break;
3129 case 3: reg_ds = regtab; break;
3130 }
3131 break;
3132
6288d05f
JB
3133 case RegMask:
3134 if (!regtab->reg_num)
3135 reg_k0 = regtab;
3136 break;
3137 }
3138
6225c532
JB
3139 if (str_hash_insert (reg_hash, regtab->reg_name, regtab, 0) != NULL)
3140 as_fatal (_("duplicate %s"), regtab->reg_name);
6225c532 3141 }
252b5132
RH
3142 }
3143
47926f60 3144 /* Fill in lexical tables: mnemonic_chars, operand_chars. */
252b5132 3145 {
29b0f896
AM
3146 int c;
3147 char *p;
252b5132
RH
3148
3149 for (c = 0; c < 256; c++)
3150 {
014fbcda 3151 if (ISDIGIT (c) || ISLOWER (c))
252b5132
RH
3152 {
3153 mnemonic_chars[c] = c;
3154 register_chars[c] = c;
3155 operand_chars[c] = c;
3156 }
3882b010 3157 else if (ISUPPER (c))
252b5132 3158 {
3882b010 3159 mnemonic_chars[c] = TOLOWER (c);
252b5132
RH
3160 register_chars[c] = mnemonic_chars[c];
3161 operand_chars[c] = c;
3162 }
43234a1e 3163 else if (c == '{' || c == '}')
86fa6981
L
3164 {
3165 mnemonic_chars[c] = c;
3166 operand_chars[c] = c;
3167 }
b3983e5f
JB
3168#ifdef SVR4_COMMENT_CHARS
3169 else if (c == '\\' && strchr (i386_comment_chars, '/'))
3170 operand_chars[c] = c;
3171#endif
252b5132 3172
3882b010 3173 if (ISALPHA (c) || ISDIGIT (c))
252b5132
RH
3174 identifier_chars[c] = c;
3175 else if (c >= 128)
3176 {
3177 identifier_chars[c] = c;
3178 operand_chars[c] = c;
3179 }
3180 }
3181
3182#ifdef LEX_AT
3183 identifier_chars['@'] = '@';
32137342
NC
3184#endif
3185#ifdef LEX_QM
3186 identifier_chars['?'] = '?';
3187 operand_chars['?'] = '?';
252b5132 3188#endif
c0f3af97 3189 mnemonic_chars['_'] = '_';
791fe849 3190 mnemonic_chars['-'] = '-';
0003779b 3191 mnemonic_chars['.'] = '.';
252b5132
RH
3192 identifier_chars['_'] = '_';
3193 identifier_chars['.'] = '.';
3194
3195 for (p = operand_special_chars; *p != '\0'; p++)
3196 operand_chars[(unsigned char) *p] = *p;
3197 }
3198
a4447b93
RH
3199 if (flag_code == CODE_64BIT)
3200 {
ca19b261
KT
3201#if defined (OBJ_COFF) && defined (TE_PE)
3202 x86_dwarf2_return_column = (OUTPUT_FLAVOR == bfd_target_coff_flavour
3203 ? 32 : 16);
3204#else
a4447b93 3205 x86_dwarf2_return_column = 16;
ca19b261 3206#endif
61ff971f 3207 x86_cie_data_alignment = -8;
a4447b93
RH
3208 }
3209 else
3210 {
3211 x86_dwarf2_return_column = 8;
3212 x86_cie_data_alignment = -4;
3213 }
e379e5f3
L
3214
3215 /* NB: FUSED_JCC_PADDING frag must have sufficient room so that it
3216 can be turned into BRANCH_PREFIX frag. */
3217 if (align_branch_prefix_size > MAX_FUSED_JCC_PADDING_SIZE)
3218 abort ();
252b5132
RH
3219}
3220
3221void
e3bb37b5 3222i386_print_statistics (FILE *file)
252b5132 3223{
629310ab
ML
3224 htab_print_statistics (file, "i386 opcode", op_hash);
3225 htab_print_statistics (file, "i386 register", reg_hash);
252b5132
RH
3226}
3227\f
252b5132
RH
3228#ifdef DEBUG386
3229
ce8a8b2f 3230/* Debugging routines for md_assemble. */
d3ce72d0 3231static void pte (insn_template *);
40fb9820 3232static void pt (i386_operand_type);
e3bb37b5
L
3233static void pe (expressionS *);
3234static void ps (symbolS *);
252b5132
RH
3235
3236static void
2c703856 3237pi (const char *line, i386_insn *x)
252b5132 3238{
09137c09 3239 unsigned int j;
252b5132
RH
3240
3241 fprintf (stdout, "%s: template ", line);
3242 pte (&x->tm);
09f131f2
JH
3243 fprintf (stdout, " address: base %s index %s scale %x\n",
3244 x->base_reg ? x->base_reg->reg_name : "none",
3245 x->index_reg ? x->index_reg->reg_name : "none",
3246 x->log2_scale_factor);
3247 fprintf (stdout, " modrm: mode %x reg %x reg/mem %x\n",
252b5132 3248 x->rm.mode, x->rm.reg, x->rm.regmem);
09f131f2
JH
3249 fprintf (stdout, " sib: base %x index %x scale %x\n",
3250 x->sib.base, x->sib.index, x->sib.scale);
3251 fprintf (stdout, " rex: 64bit %x extX %x extY %x extZ %x\n",
161a04f6
L
3252 (x->rex & REX_W) != 0,
3253 (x->rex & REX_R) != 0,
3254 (x->rex & REX_X) != 0,
3255 (x->rex & REX_B) != 0);
09137c09 3256 for (j = 0; j < x->operands; j++)
252b5132 3257 {
09137c09
SP
3258 fprintf (stdout, " #%d: ", j + 1);
3259 pt (x->types[j]);
252b5132 3260 fprintf (stdout, "\n");
bab6aec1 3261 if (x->types[j].bitfield.class == Reg
3528c362
JB
3262 || x->types[j].bitfield.class == RegMMX
3263 || x->types[j].bitfield.class == RegSIMD
dd6b8a0b 3264 || x->types[j].bitfield.class == RegMask
00cee14f 3265 || x->types[j].bitfield.class == SReg
4a5c67ed
JB
3266 || x->types[j].bitfield.class == RegCR
3267 || x->types[j].bitfield.class == RegDR
dd6b8a0b
JB
3268 || x->types[j].bitfield.class == RegTR
3269 || x->types[j].bitfield.class == RegBND)
09137c09
SP
3270 fprintf (stdout, "%s\n", x->op[j].regs->reg_name);
3271 if (operand_type_check (x->types[j], imm))
3272 pe (x->op[j].imms);
3273 if (operand_type_check (x->types[j], disp))
3274 pe (x->op[j].disps);
252b5132
RH
3275 }
3276}
3277
3278static void
d3ce72d0 3279pte (insn_template *t)
252b5132 3280{
b933fa4b 3281 static const unsigned char opc_pfx[] = { 0, 0x66, 0xf3, 0xf2 };
441f6aca 3282 static const char *const opc_spc[] = {
0cc78721 3283 NULL, "0f", "0f38", "0f3a", NULL, "evexmap5", "evexmap6", NULL,
441f6aca
JB
3284 "XOP08", "XOP09", "XOP0A",
3285 };
09137c09 3286 unsigned int j;
441f6aca 3287
252b5132 3288 fprintf (stdout, " %d operands ", t->operands);
441f6aca
JB
3289 if (opc_pfx[t->opcode_modifier.opcodeprefix])
3290 fprintf (stdout, "pfx %x ", opc_pfx[t->opcode_modifier.opcodeprefix]);
3291 if (opc_spc[t->opcode_modifier.opcodespace])
3292 fprintf (stdout, "space %s ", opc_spc[t->opcode_modifier.opcodespace]);
47926f60 3293 fprintf (stdout, "opcode %x ", t->base_opcode);
252b5132
RH
3294 if (t->extension_opcode != None)
3295 fprintf (stdout, "ext %x ", t->extension_opcode);
40fb9820 3296 if (t->opcode_modifier.d)
252b5132 3297 fprintf (stdout, "D");
40fb9820 3298 if (t->opcode_modifier.w)
252b5132
RH
3299 fprintf (stdout, "W");
3300 fprintf (stdout, "\n");
09137c09 3301 for (j = 0; j < t->operands; j++)
252b5132 3302 {
09137c09
SP
3303 fprintf (stdout, " #%d type ", j + 1);
3304 pt (t->operand_types[j]);
252b5132
RH
3305 fprintf (stdout, "\n");
3306 }
3307}
3308
3309static void
e3bb37b5 3310pe (expressionS *e)
252b5132 3311{
24eab124 3312 fprintf (stdout, " operation %d\n", e->X_op);
7b025ee8
JB
3313 fprintf (stdout, " add_number %" BFD_VMA_FMT "d (%" BFD_VMA_FMT "x)\n",
3314 e->X_add_number, e->X_add_number);
252b5132
RH
3315 if (e->X_add_symbol)
3316 {
3317 fprintf (stdout, " add_symbol ");
3318 ps (e->X_add_symbol);
3319 fprintf (stdout, "\n");
3320 }
3321 if (e->X_op_symbol)
3322 {
3323 fprintf (stdout, " op_symbol ");
3324 ps (e->X_op_symbol);
3325 fprintf (stdout, "\n");
3326 }
3327}
3328
3329static void
e3bb37b5 3330ps (symbolS *s)
252b5132
RH
3331{
3332 fprintf (stdout, "%s type %s%s",
3333 S_GET_NAME (s),
3334 S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
3335 segment_name (S_GET_SEGMENT (s)));
3336}
3337
7b81dfbb 3338static struct type_name
252b5132 3339 {
40fb9820
L
3340 i386_operand_type mask;
3341 const char *name;
252b5132 3342 }
7b81dfbb 3343const type_names[] =
252b5132 3344{
40fb9820
L
3345 { OPERAND_TYPE_REG8, "r8" },
3346 { OPERAND_TYPE_REG16, "r16" },
3347 { OPERAND_TYPE_REG32, "r32" },
3348 { OPERAND_TYPE_REG64, "r64" },
2c703856
JB
3349 { OPERAND_TYPE_ACC8, "acc8" },
3350 { OPERAND_TYPE_ACC16, "acc16" },
3351 { OPERAND_TYPE_ACC32, "acc32" },
3352 { OPERAND_TYPE_ACC64, "acc64" },
40fb9820
L
3353 { OPERAND_TYPE_IMM8, "i8" },
3354 { OPERAND_TYPE_IMM8, "i8s" },
3355 { OPERAND_TYPE_IMM16, "i16" },
3356 { OPERAND_TYPE_IMM32, "i32" },
3357 { OPERAND_TYPE_IMM32S, "i32s" },
3358 { OPERAND_TYPE_IMM64, "i64" },
3359 { OPERAND_TYPE_IMM1, "i1" },
3360 { OPERAND_TYPE_BASEINDEX, "BaseIndex" },
3361 { OPERAND_TYPE_DISP8, "d8" },
3362 { OPERAND_TYPE_DISP16, "d16" },
3363 { OPERAND_TYPE_DISP32, "d32" },
3364 { OPERAND_TYPE_DISP32S, "d32s" },
3365 { OPERAND_TYPE_DISP64, "d64" },
3366 { OPERAND_TYPE_INOUTPORTREG, "InOutPortReg" },
3367 { OPERAND_TYPE_SHIFTCOUNT, "ShiftCount" },
3368 { OPERAND_TYPE_CONTROL, "control reg" },
3369 { OPERAND_TYPE_TEST, "test reg" },
3370 { OPERAND_TYPE_DEBUG, "debug reg" },
3371 { OPERAND_TYPE_FLOATREG, "FReg" },
3372 { OPERAND_TYPE_FLOATACC, "FAcc" },
21df382b 3373 { OPERAND_TYPE_SREG, "SReg" },
40fb9820
L
3374 { OPERAND_TYPE_REGMMX, "rMMX" },
3375 { OPERAND_TYPE_REGXMM, "rXMM" },
0349dc08 3376 { OPERAND_TYPE_REGYMM, "rYMM" },
43234a1e 3377 { OPERAND_TYPE_REGZMM, "rZMM" },
260cd341 3378 { OPERAND_TYPE_REGTMM, "rTMM" },
43234a1e 3379 { OPERAND_TYPE_REGMASK, "Mask reg" },
252b5132
RH
3380};
3381
3382static void
40fb9820 3383pt (i386_operand_type t)
252b5132 3384{
40fb9820 3385 unsigned int j;
c6fb90c8 3386 i386_operand_type a;
252b5132 3387
40fb9820 3388 for (j = 0; j < ARRAY_SIZE (type_names); j++)
c6fb90c8
L
3389 {
3390 a = operand_type_and (t, type_names[j].mask);
2c703856 3391 if (operand_type_equal (&a, &type_names[j].mask))
c6fb90c8
L
3392 fprintf (stdout, "%s, ", type_names[j].name);
3393 }
252b5132
RH
3394 fflush (stdout);
3395}
3396
3397#endif /* DEBUG386 */
3398\f
252b5132 3399static bfd_reloc_code_real_type
3956db08 3400reloc (unsigned int size,
64e74474
AM
3401 int pcrel,
3402 int sign,
3403 bfd_reloc_code_real_type other)
252b5132 3404{
47926f60 3405 if (other != NO_RELOC)
3956db08 3406 {
91d6fa6a 3407 reloc_howto_type *rel;
3956db08
JB
3408
3409 if (size == 8)
3410 switch (other)
3411 {
64e74474
AM
3412 case BFD_RELOC_X86_64_GOT32:
3413 return BFD_RELOC_X86_64_GOT64;
3414 break;
553d1284
L
3415 case BFD_RELOC_X86_64_GOTPLT64:
3416 return BFD_RELOC_X86_64_GOTPLT64;
3417 break;
64e74474
AM
3418 case BFD_RELOC_X86_64_PLTOFF64:
3419 return BFD_RELOC_X86_64_PLTOFF64;
3420 break;
3421 case BFD_RELOC_X86_64_GOTPC32:
3422 other = BFD_RELOC_X86_64_GOTPC64;
3423 break;
3424 case BFD_RELOC_X86_64_GOTPCREL:
3425 other = BFD_RELOC_X86_64_GOTPCREL64;
3426 break;
3427 case BFD_RELOC_X86_64_TPOFF32:
3428 other = BFD_RELOC_X86_64_TPOFF64;
3429 break;
3430 case BFD_RELOC_X86_64_DTPOFF32:
3431 other = BFD_RELOC_X86_64_DTPOFF64;
3432 break;
3433 default:
3434 break;
3956db08 3435 }
e05278af 3436
8ce3d284 3437#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
3438 if (other == BFD_RELOC_SIZE32)
3439 {
3440 if (size == 8)
1ab668bf 3441 other = BFD_RELOC_SIZE64;
8fd4256d 3442 if (pcrel)
1ab668bf
AM
3443 {
3444 as_bad (_("there are no pc-relative size relocations"));
3445 return NO_RELOC;
3446 }
8fd4256d 3447 }
8ce3d284 3448#endif
8fd4256d 3449
e05278af 3450 /* Sign-checking 4-byte relocations in 16-/32-bit code is pointless. */
f2d8a97c 3451 if (size == 4 && (flag_code != CODE_64BIT || disallow_64bit_reloc))
e05278af
JB
3452 sign = -1;
3453
91d6fa6a
NC
3454 rel = bfd_reloc_type_lookup (stdoutput, other);
3455 if (!rel)
3956db08 3456 as_bad (_("unknown relocation (%u)"), other);
91d6fa6a 3457 else if (size != bfd_get_reloc_size (rel))
3956db08 3458 as_bad (_("%u-byte relocation cannot be applied to %u-byte field"),
91d6fa6a 3459 bfd_get_reloc_size (rel),
3956db08 3460 size);
91d6fa6a 3461 else if (pcrel && !rel->pc_relative)
3956db08 3462 as_bad (_("non-pc-relative relocation for pc-relative field"));
91d6fa6a 3463 else if ((rel->complain_on_overflow == complain_overflow_signed
3956db08 3464 && !sign)
91d6fa6a 3465 || (rel->complain_on_overflow == complain_overflow_unsigned
64e74474 3466 && sign > 0))
3956db08
JB
3467 as_bad (_("relocated field and relocation type differ in signedness"));
3468 else
3469 return other;
3470 return NO_RELOC;
3471 }
252b5132
RH
3472
3473 if (pcrel)
3474 {
3e73aa7c 3475 if (!sign)
3956db08 3476 as_bad (_("there are no unsigned pc-relative relocations"));
252b5132
RH
3477 switch (size)
3478 {
3479 case 1: return BFD_RELOC_8_PCREL;
3480 case 2: return BFD_RELOC_16_PCREL;
d258b828 3481 case 4: return BFD_RELOC_32_PCREL;
d6ab8113 3482 case 8: return BFD_RELOC_64_PCREL;
252b5132 3483 }
3956db08 3484 as_bad (_("cannot do %u byte pc-relative relocation"), size);
252b5132
RH
3485 }
3486 else
3487 {
3956db08 3488 if (sign > 0)
e5cb08ac 3489 switch (size)
3e73aa7c
JH
3490 {
3491 case 4: return BFD_RELOC_X86_64_32S;
3492 }
3493 else
3494 switch (size)
3495 {
3496 case 1: return BFD_RELOC_8;
3497 case 2: return BFD_RELOC_16;
3498 case 4: return BFD_RELOC_32;
3499 case 8: return BFD_RELOC_64;
3500 }
3956db08
JB
3501 as_bad (_("cannot do %s %u byte relocation"),
3502 sign > 0 ? "signed" : "unsigned", size);
252b5132
RH
3503 }
3504
0cc9e1d3 3505 return NO_RELOC;
252b5132
RH
3506}
3507
47926f60
KH
3508/* Here we decide which fixups can be adjusted to make them relative to
3509 the beginning of the section instead of the symbol. Basically we need
3510 to make sure that the dynamic relocations are done correctly, so in
3511 some cases we force the original symbol to be used. */
3512
252b5132 3513int
e3bb37b5 3514tc_i386_fix_adjustable (fixS *fixP ATTRIBUTE_UNUSED)
252b5132 3515{
6d249963 3516#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 3517 if (!IS_ELF)
31312f95
AM
3518 return 1;
3519
a161fe53
AM
3520 /* Don't adjust pc-relative references to merge sections in 64-bit
3521 mode. */
3522 if (use_rela_relocations
3523 && (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0
3524 && fixP->fx_pcrel)
252b5132 3525 return 0;
31312f95 3526
8d01d9a9
AJ
3527 /* The x86_64 GOTPCREL are represented as 32bit PCrel relocations
3528 and changed later by validate_fix. */
3529 if (GOT_symbol && fixP->fx_subsy == GOT_symbol
3530 && fixP->fx_r_type == BFD_RELOC_32_PCREL)
3531 return 0;
3532
8fd4256d
L
3533 /* Adjust_reloc_syms doesn't know about the GOT. Need to keep symbol
3534 for size relocations. */
3535 if (fixP->fx_r_type == BFD_RELOC_SIZE32
3536 || fixP->fx_r_type == BFD_RELOC_SIZE64
3537 || fixP->fx_r_type == BFD_RELOC_386_GOTOFF
252b5132 3538 || fixP->fx_r_type == BFD_RELOC_386_GOT32
02a86693 3539 || fixP->fx_r_type == BFD_RELOC_386_GOT32X
13ae64f3
JJ
3540 || fixP->fx_r_type == BFD_RELOC_386_TLS_GD
3541 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDM
3542 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDO_32
3543 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE_32
37e55690
JJ
3544 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE
3545 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTIE
13ae64f3
JJ
3546 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE_32
3547 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE
67a4f2b7
AO
3548 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTDESC
3549 || fixP->fx_r_type == BFD_RELOC_386_TLS_DESC_CALL
3e73aa7c 3550 || fixP->fx_r_type == BFD_RELOC_X86_64_GOT32
80b3ee89 3551 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCREL
56ceb5b5
L
3552 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCRELX
3553 || fixP->fx_r_type == BFD_RELOC_X86_64_REX_GOTPCRELX
bffbf940
JJ
3554 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSGD
3555 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSLD
3556 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF32
d6ab8113 3557 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF64
bffbf940
JJ
3558 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF
3559 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF32
d6ab8113
JB
3560 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF64
3561 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTOFF64
67a4f2b7
AO
3562 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
3563 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSDESC_CALL
252b5132
RH
3564 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3565 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3566 return 0;
31312f95 3567#endif
252b5132
RH
3568 return 1;
3569}
252b5132 3570
a9aabc23
JB
3571static INLINE bool
3572want_disp32 (const insn_template *t)
3573{
3574 return flag_code != CODE_64BIT
3575 || i.prefix[ADDR_PREFIX]
3576 || (t->base_opcode == 0x8d
3577 && t->opcode_modifier.opcodespace == SPACE_BASE
fe134c65
JB
3578 && (!i.types[1].bitfield.qword
3579 || t->opcode_modifier.size == SIZE32));
a9aabc23
JB
3580}
3581
b4cac588 3582static int
e3bb37b5 3583intel_float_operand (const char *mnemonic)
252b5132 3584{
9306ca4a
JB
3585 /* Note that the value returned is meaningful only for opcodes with (memory)
3586 operands, hence the code here is free to improperly handle opcodes that
3587 have no operands (for better performance and smaller code). */
3588
3589 if (mnemonic[0] != 'f')
3590 return 0; /* non-math */
3591
3592 switch (mnemonic[1])
3593 {
3594 /* fclex, fdecstp, fdisi, femms, feni, fincstp, finit, fsetpm, and
3595 the fs segment override prefix not currently handled because no
3596 call path can make opcodes without operands get here */
3597 case 'i':
3598 return 2 /* integer op */;
3599 case 'l':
3600 if (mnemonic[2] == 'd' && (mnemonic[3] == 'c' || mnemonic[3] == 'e'))
3601 return 3; /* fldcw/fldenv */
3602 break;
3603 case 'n':
3604 if (mnemonic[2] != 'o' /* fnop */)
3605 return 3; /* non-waiting control op */
3606 break;
3607 case 'r':
3608 if (mnemonic[2] == 's')
3609 return 3; /* frstor/frstpm */
3610 break;
3611 case 's':
3612 if (mnemonic[2] == 'a')
3613 return 3; /* fsave */
3614 if (mnemonic[2] == 't')
3615 {
3616 switch (mnemonic[3])
3617 {
3618 case 'c': /* fstcw */
3619 case 'd': /* fstdw */
3620 case 'e': /* fstenv */
3621 case 's': /* fsts[gw] */
3622 return 3;
3623 }
3624 }
3625 break;
3626 case 'x':
3627 if (mnemonic[2] == 'r' || mnemonic[2] == 's')
3628 return 0; /* fxsave/fxrstor are not really math ops */
3629 break;
3630 }
252b5132 3631
9306ca4a 3632 return 1;
252b5132
RH
3633}
3634
9a182d04
JB
3635static INLINE void
3636install_template (const insn_template *t)
3637{
3638 unsigned int l;
3639
3640 i.tm = *t;
3641
3642 /* Note that for pseudo prefixes this produces a length of 1. But for them
3643 the length isn't interesting at all. */
3644 for (l = 1; l < 4; ++l)
3645 if (!(t->base_opcode >> (8 * l)))
3646 break;
3647
3648 i.opcode_length = l;
3649}
3650
c0f3af97
L
3651/* Build the VEX prefix. */
3652
3653static void
d3ce72d0 3654build_vex_prefix (const insn_template *t)
c0f3af97
L
3655{
3656 unsigned int register_specifier;
c0f3af97 3657 unsigned int vector_length;
03751133 3658 unsigned int w;
c0f3af97
L
3659
3660 /* Check register specifier. */
3661 if (i.vex.register_specifier)
43234a1e
L
3662 {
3663 register_specifier =
3664 ~register_number (i.vex.register_specifier) & 0xf;
3665 gas_assert ((i.vex.register_specifier->reg_flags & RegVRex) == 0);
3666 }
c0f3af97
L
3667 else
3668 register_specifier = 0xf;
3669
79f0fa25
L
3670 /* Use 2-byte VEX prefix by swapping destination and source operand
3671 if there are more than 1 register operand. */
3672 if (i.reg_operands > 1
3673 && i.vec_encoding != vex_encoding_vex3
86fa6981 3674 && i.dir_encoding == dir_encoding_default
fa99fab2 3675 && i.operands == i.reg_operands
dbbc8b7e 3676 && operand_type_equal (&i.types[0], &i.types[i.operands - 1])
441f6aca 3677 && i.tm.opcode_modifier.opcodespace == SPACE_0F
dbbc8b7e 3678 && (i.tm.opcode_modifier.load || i.tm.opcode_modifier.d)
fa99fab2
L
3679 && i.rex == REX_B)
3680 {
3681 unsigned int xchg = i.operands - 1;
3682 union i386_op temp_op;
3683 i386_operand_type temp_type;
3684
3685 temp_type = i.types[xchg];
3686 i.types[xchg] = i.types[0];
3687 i.types[0] = temp_type;
3688 temp_op = i.op[xchg];
3689 i.op[xchg] = i.op[0];
3690 i.op[0] = temp_op;
3691
9c2799c2 3692 gas_assert (i.rm.mode == 3);
fa99fab2
L
3693
3694 i.rex = REX_R;
3695 xchg = i.rm.regmem;
3696 i.rm.regmem = i.rm.reg;
3697 i.rm.reg = xchg;
3698
dbbc8b7e
JB
3699 if (i.tm.opcode_modifier.d)
3700 i.tm.base_opcode ^= (i.tm.base_opcode & 0xee) != 0x6e
3701 ? Opcode_SIMD_FloatD : Opcode_SIMD_IntD;
3702 else /* Use the next insn. */
9a182d04 3703 install_template (&t[1]);
fa99fab2
L
3704 }
3705
79dec6b7
JB
3706 /* Use 2-byte VEX prefix by swapping commutative source operands if there
3707 are no memory operands and at least 3 register ones. */
3708 if (i.reg_operands >= 3
3709 && i.vec_encoding != vex_encoding_vex3
3710 && i.reg_operands == i.operands - i.imm_operands
3711 && i.tm.opcode_modifier.vex
3712 && i.tm.opcode_modifier.commutative
3713 && (i.tm.opcode_modifier.sse2avx || optimize > 1)
3714 && i.rex == REX_B
3715 && i.vex.register_specifier
3716 && !(i.vex.register_specifier->reg_flags & RegRex))
3717 {
3718 unsigned int xchg = i.operands - i.reg_operands;
3719 union i386_op temp_op;
3720 i386_operand_type temp_type;
3721
441f6aca 3722 gas_assert (i.tm.opcode_modifier.opcodespace == SPACE_0F);
79dec6b7
JB
3723 gas_assert (!i.tm.opcode_modifier.sae);
3724 gas_assert (operand_type_equal (&i.types[i.operands - 2],
3725 &i.types[i.operands - 3]));
3726 gas_assert (i.rm.mode == 3);
3727
3728 temp_type = i.types[xchg];
3729 i.types[xchg] = i.types[xchg + 1];
3730 i.types[xchg + 1] = temp_type;
3731 temp_op = i.op[xchg];
3732 i.op[xchg] = i.op[xchg + 1];
3733 i.op[xchg + 1] = temp_op;
3734
3735 i.rex = 0;
3736 xchg = i.rm.regmem | 8;
3737 i.rm.regmem = ~register_specifier & 0xf;
3738 gas_assert (!(i.rm.regmem & 8));
3739 i.vex.register_specifier += xchg - i.rm.regmem;
3740 register_specifier = ~xchg & 0xf;
3741 }
3742
539f890d
L
3743 if (i.tm.opcode_modifier.vex == VEXScalar)
3744 vector_length = avxscalar;
10c17abd
JB
3745 else if (i.tm.opcode_modifier.vex == VEX256)
3746 vector_length = 1;
539f890d 3747 else
10c17abd 3748 {
56522fc5 3749 unsigned int op;
10c17abd 3750
c7213af9
L
3751 /* Determine vector length from the last multi-length vector
3752 operand. */
10c17abd 3753 vector_length = 0;
56522fc5 3754 for (op = t->operands; op--;)
10c17abd
JB
3755 if (t->operand_types[op].bitfield.xmmword
3756 && t->operand_types[op].bitfield.ymmword
3757 && i.types[op].bitfield.ymmword)
3758 {
3759 vector_length = 1;
3760 break;
3761 }
3762 }
c0f3af97 3763
03751133
L
3764 /* Check the REX.W bit and VEXW. */
3765 if (i.tm.opcode_modifier.vexw == VEXWIG)
3766 w = (vexwig == vexw1 || (i.rex & REX_W)) ? 1 : 0;
3767 else if (i.tm.opcode_modifier.vexw)
3768 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3769 else
931d03b7 3770 w = (flag_code == CODE_64BIT ? i.rex & REX_W : vexwig == vexw1) ? 1 : 0;
03751133 3771
c0f3af97 3772 /* Use 2-byte VEX prefix if possible. */
03751133
L
3773 if (w == 0
3774 && i.vec_encoding != vex_encoding_vex3
441f6aca 3775 && i.tm.opcode_modifier.opcodespace == SPACE_0F
c0f3af97
L
3776 && (i.rex & (REX_W | REX_X | REX_B)) == 0)
3777 {
3778 /* 2-byte VEX prefix. */
3779 unsigned int r;
3780
3781 i.vex.length = 2;
3782 i.vex.bytes[0] = 0xc5;
3783
3784 /* Check the REX.R bit. */
3785 r = (i.rex & REX_R) ? 0 : 1;
3786 i.vex.bytes[1] = (r << 7
3787 | register_specifier << 3
3788 | vector_length << 2
35648716 3789 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3790 }
3791 else
3792 {
3793 /* 3-byte VEX prefix. */
f88c9eb0 3794 i.vex.length = 3;
f88c9eb0 3795
441f6aca 3796 switch (i.tm.opcode_modifier.opcodespace)
5dd85c99 3797 {
441f6aca
JB
3798 case SPACE_0F:
3799 case SPACE_0F38:
3800 case SPACE_0F3A:
80de6e00 3801 i.vex.bytes[0] = 0xc4;
7f399153 3802 break;
441f6aca
JB
3803 case SPACE_XOP08:
3804 case SPACE_XOP09:
3805 case SPACE_XOP0A:
f88c9eb0 3806 i.vex.bytes[0] = 0x8f;
7f399153
L
3807 break;
3808 default:
3809 abort ();
f88c9eb0 3810 }
c0f3af97 3811
c0f3af97
L
3812 /* The high 3 bits of the second VEX byte are 1's compliment
3813 of RXB bits from REX. */
441f6aca 3814 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | i.tm.opcode_modifier.opcodespace;
c0f3af97 3815
c0f3af97
L
3816 i.vex.bytes[2] = (w << 7
3817 | register_specifier << 3
3818 | vector_length << 2
35648716 3819 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3820 }
3821}
3822
5b7c81bd 3823static INLINE bool
e771e7c9
JB
3824is_evex_encoding (const insn_template *t)
3825{
7091c612 3826 return t->opcode_modifier.evex || t->opcode_modifier.disp8memshift
e771e7c9 3827 || t->opcode_modifier.broadcast || t->opcode_modifier.masking
a80195f1 3828 || t->opcode_modifier.sae;
e771e7c9
JB
3829}
3830
5b7c81bd 3831static INLINE bool
7a8655d2
JB
3832is_any_vex_encoding (const insn_template *t)
3833{
7b47a312 3834 return t->opcode_modifier.vex || is_evex_encoding (t);
7a8655d2
JB
3835}
3836
43234a1e
L
3837/* Build the EVEX prefix. */
3838
3839static void
3840build_evex_prefix (void)
3841{
35648716 3842 unsigned int register_specifier, w;
43234a1e
L
3843 rex_byte vrex_used = 0;
3844
3845 /* Check register specifier. */
3846 if (i.vex.register_specifier)
3847 {
3848 gas_assert ((i.vrex & REX_X) == 0);
3849
3850 register_specifier = i.vex.register_specifier->reg_num;
3851 if ((i.vex.register_specifier->reg_flags & RegRex))
3852 register_specifier += 8;
3853 /* The upper 16 registers are encoded in the fourth byte of the
3854 EVEX prefix. */
3855 if (!(i.vex.register_specifier->reg_flags & RegVRex))
3856 i.vex.bytes[3] = 0x8;
3857 register_specifier = ~register_specifier & 0xf;
3858 }
3859 else
3860 {
3861 register_specifier = 0xf;
3862
3863 /* Encode upper 16 vector index register in the fourth byte of
3864 the EVEX prefix. */
3865 if (!(i.vrex & REX_X))
3866 i.vex.bytes[3] = 0x8;
3867 else
3868 vrex_used |= REX_X;
3869 }
3870
43234a1e
L
3871 /* 4 byte EVEX prefix. */
3872 i.vex.length = 4;
3873 i.vex.bytes[0] = 0x62;
3874
43234a1e
L
3875 /* The high 3 bits of the second EVEX byte are 1's compliment of RXB
3876 bits from REX. */
441f6aca 3877 gas_assert (i.tm.opcode_modifier.opcodespace >= SPACE_0F);
0cc78721 3878 gas_assert (i.tm.opcode_modifier.opcodespace <= SPACE_EVEXMAP6);
441f6aca 3879 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | i.tm.opcode_modifier.opcodespace;
43234a1e
L
3880
3881 /* The fifth bit of the second EVEX byte is 1's compliment of the
3882 REX_R bit in VREX. */
3883 if (!(i.vrex & REX_R))
3884 i.vex.bytes[1] |= 0x10;
3885 else
3886 vrex_used |= REX_R;
3887
3888 if ((i.reg_operands + i.imm_operands) == i.operands)
3889 {
3890 /* When all operands are registers, the REX_X bit in REX is not
3891 used. We reuse it to encode the upper 16 registers, which is
3892 indicated by the REX_B bit in VREX. The REX_X bit is encoded
3893 as 1's compliment. */
3894 if ((i.vrex & REX_B))
3895 {
3896 vrex_used |= REX_B;
3897 i.vex.bytes[1] &= ~0x40;
3898 }
3899 }
3900
3901 /* EVEX instructions shouldn't need the REX prefix. */
3902 i.vrex &= ~vrex_used;
3903 gas_assert (i.vrex == 0);
3904
6865c043
L
3905 /* Check the REX.W bit and VEXW. */
3906 if (i.tm.opcode_modifier.vexw == VEXWIG)
3907 w = (evexwig == evexw1 || (i.rex & REX_W)) ? 1 : 0;
3908 else if (i.tm.opcode_modifier.vexw)
3909 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3910 else
931d03b7 3911 w = (flag_code == CODE_64BIT ? i.rex & REX_W : evexwig == evexw1) ? 1 : 0;
43234a1e 3912
43234a1e 3913 /* The third byte of the EVEX prefix. */
35648716
JB
3914 i.vex.bytes[2] = ((w << 7)
3915 | (register_specifier << 3)
3916 | 4 /* Encode the U bit. */
3917 | i.tm.opcode_modifier.opcodeprefix);
43234a1e
L
3918
3919 /* The fourth byte of the EVEX prefix. */
3920 /* The zeroing-masking bit. */
6225c532 3921 if (i.mask.reg && i.mask.zeroing)
43234a1e
L
3922 i.vex.bytes[3] |= 0x80;
3923
3924 /* Don't always set the broadcast bit if there is no RC. */
ca5312a2 3925 if (i.rounding.type == rc_none)
43234a1e
L
3926 {
3927 /* Encode the vector length. */
3928 unsigned int vec_length;
3929
e771e7c9
JB
3930 if (!i.tm.opcode_modifier.evex
3931 || i.tm.opcode_modifier.evex == EVEXDYN)
3932 {
56522fc5 3933 unsigned int op;
e771e7c9 3934
c7213af9
L
3935 /* Determine vector length from the last multi-length vector
3936 operand. */
56522fc5 3937 for (op = i.operands; op--;)
e771e7c9
JB
3938 if (i.tm.operand_types[op].bitfield.xmmword
3939 + i.tm.operand_types[op].bitfield.ymmword
3940 + i.tm.operand_types[op].bitfield.zmmword > 1)
3941 {
3942 if (i.types[op].bitfield.zmmword)
c7213af9
L
3943 {
3944 i.tm.opcode_modifier.evex = EVEX512;
3945 break;
3946 }
e771e7c9 3947 else if (i.types[op].bitfield.ymmword)
c7213af9
L
3948 {
3949 i.tm.opcode_modifier.evex = EVEX256;
3950 break;
3951 }
e771e7c9 3952 else if (i.types[op].bitfield.xmmword)
c7213af9
L
3953 {
3954 i.tm.opcode_modifier.evex = EVEX128;
3955 break;
3956 }
5273a3cd 3957 else if (i.broadcast.type && op == i.broadcast.operand)
625cbd7a 3958 {
5273a3cd 3959 switch (i.broadcast.bytes)
625cbd7a
JB
3960 {
3961 case 64:
3962 i.tm.opcode_modifier.evex = EVEX512;
3963 break;
3964 case 32:
3965 i.tm.opcode_modifier.evex = EVEX256;
3966 break;
3967 case 16:
3968 i.tm.opcode_modifier.evex = EVEX128;
3969 break;
3970 default:
c7213af9 3971 abort ();
625cbd7a 3972 }
c7213af9 3973 break;
625cbd7a 3974 }
e771e7c9 3975 }
c7213af9 3976
56522fc5 3977 if (op >= MAX_OPERANDS)
c7213af9 3978 abort ();
e771e7c9
JB
3979 }
3980
43234a1e
L
3981 switch (i.tm.opcode_modifier.evex)
3982 {
3983 case EVEXLIG: /* LL' is ignored */
3984 vec_length = evexlig << 5;
3985 break;
3986 case EVEX128:
3987 vec_length = 0 << 5;
3988 break;
3989 case EVEX256:
3990 vec_length = 1 << 5;
3991 break;
3992 case EVEX512:
3993 vec_length = 2 << 5;
3994 break;
3995 default:
3996 abort ();
3997 break;
3998 }
3999 i.vex.bytes[3] |= vec_length;
4000 /* Encode the broadcast bit. */
5273a3cd 4001 if (i.broadcast.type)
43234a1e
L
4002 i.vex.bytes[3] |= 0x10;
4003 }
ca5312a2
JB
4004 else if (i.rounding.type != saeonly)
4005 i.vex.bytes[3] |= 0x10 | (i.rounding.type << 5);
43234a1e 4006 else
ca5312a2 4007 i.vex.bytes[3] |= 0x10 | (evexrcig << 5);
43234a1e 4008
6225c532
JB
4009 if (i.mask.reg)
4010 i.vex.bytes[3] |= i.mask.reg->reg_num;
43234a1e
L
4011}
4012
65da13b5
L
4013static void
4014process_immext (void)
4015{
4016 expressionS *exp;
4017
c0f3af97 4018 /* These AMD 3DNow! and SSE2 instructions have an opcode suffix
65da13b5
L
4019 which is coded in the same place as an 8-bit immediate field
4020 would be. Here we fake an 8-bit immediate operand from the
4021 opcode suffix stored in tm.extension_opcode.
4022
c1e679ec 4023 AVX instructions also use this encoding, for some of
c0f3af97 4024 3 argument instructions. */
65da13b5 4025
43234a1e 4026 gas_assert (i.imm_operands <= 1
7ab9ffdd 4027 && (i.operands <= 2
7a8655d2 4028 || (is_any_vex_encoding (&i.tm)
7ab9ffdd 4029 && i.operands <= 4)));
65da13b5
L
4030
4031 exp = &im_expressions[i.imm_operands++];
4032 i.op[i.operands].imms = exp;
4033 i.types[i.operands] = imm8;
4034 i.operands++;
4035 exp->X_op = O_constant;
4036 exp->X_add_number = i.tm.extension_opcode;
4037 i.tm.extension_opcode = None;
4038}
4039
42164a71
L
4040
4041static int
4042check_hle (void)
4043{
742732c7 4044 switch (i.tm.opcode_modifier.prefixok)
42164a71
L
4045 {
4046 default:
4047 abort ();
742732c7
JB
4048 case PrefixLock:
4049 case PrefixNone:
4050 case PrefixNoTrack:
4051 case PrefixRep:
165de32a
L
4052 as_bad (_("invalid instruction `%s' after `%s'"),
4053 i.tm.name, i.hle_prefix);
42164a71 4054 return 0;
742732c7 4055 case PrefixHLELock:
42164a71
L
4056 if (i.prefix[LOCK_PREFIX])
4057 return 1;
165de32a 4058 as_bad (_("missing `lock' with `%s'"), i.hle_prefix);
42164a71 4059 return 0;
742732c7 4060 case PrefixHLEAny:
42164a71 4061 return 1;
742732c7 4062 case PrefixHLERelease:
42164a71
L
4063 if (i.prefix[HLE_PREFIX] != XRELEASE_PREFIX_OPCODE)
4064 {
4065 as_bad (_("instruction `%s' after `xacquire' not allowed"),
4066 i.tm.name);
4067 return 0;
4068 }
8dc0818e 4069 if (i.mem_operands == 0 || !(i.flags[i.operands - 1] & Operand_Mem))
42164a71
L
4070 {
4071 as_bad (_("memory destination needed for instruction `%s'"
4072 " after `xrelease'"), i.tm.name);
4073 return 0;
4074 }
4075 return 1;
4076 }
4077}
4078
c8480b58
L
4079/* Encode aligned vector move as unaligned vector move. */
4080
4081static void
4082encode_with_unaligned_vector_move (void)
4083{
4084 switch (i.tm.base_opcode)
4085 {
b3a9fe6f
L
4086 case 0x28: /* Load instructions. */
4087 case 0x29: /* Store instructions. */
c8480b58
L
4088 /* movaps/movapd/vmovaps/vmovapd. */
4089 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4090 && i.tm.opcode_modifier.opcodeprefix <= PREFIX_0X66)
b3a9fe6f 4091 i.tm.base_opcode = 0x10 | (i.tm.base_opcode & 1);
c8480b58 4092 break;
b3a9fe6f
L
4093 case 0x6f: /* Load instructions. */
4094 case 0x7f: /* Store instructions. */
c8480b58
L
4095 /* movdqa/vmovdqa/vmovdqa64/vmovdqa32. */
4096 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4097 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0X66)
4098 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
4099 break;
4100 default:
4101 break;
4102 }
4103}
4104
b6f8c7c4
L
4105/* Try the shortest encoding by shortening operand size. */
4106
4107static void
4108optimize_encoding (void)
4109{
a0a1771e 4110 unsigned int j;
b6f8c7c4 4111
fe134c65
JB
4112 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
4113 && i.tm.base_opcode == 0x8d)
4114 {
4115 /* Optimize: -O:
4116 lea symbol, %rN -> mov $symbol, %rN
4117 lea (%rM), %rN -> mov %rM, %rN
4118 lea (,%rM,1), %rN -> mov %rM, %rN
4119
4120 and in 32-bit mode for 16-bit addressing
4121
4122 lea (%rM), %rN -> movzx %rM, %rN
4123
4124 and in 64-bit mode zap 32-bit addressing in favor of using a
4125 32-bit (or less) destination.
4126 */
4127 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
4128 {
4129 if (!i.op[1].regs->reg_type.bitfield.word)
4130 i.tm.opcode_modifier.size = SIZE32;
4131 i.prefix[ADDR_PREFIX] = 0;
4132 }
4133
4134 if (!i.index_reg && !i.base_reg)
4135 {
4136 /* Handle:
4137 lea symbol, %rN -> mov $symbol, %rN
4138 */
4139 if (flag_code == CODE_64BIT)
4140 {
4141 /* Don't transform a relocation to a 16-bit one. */
4142 if (i.op[0].disps
4143 && i.op[0].disps->X_op != O_constant
4144 && i.op[1].regs->reg_type.bitfield.word)
4145 return;
4146
4147 if (!i.op[1].regs->reg_type.bitfield.qword
4148 || i.tm.opcode_modifier.size == SIZE32)
4149 {
4150 i.tm.base_opcode = 0xb8;
4151 i.tm.opcode_modifier.modrm = 0;
4152 if (!i.op[1].regs->reg_type.bitfield.word)
4153 i.types[0].bitfield.imm32 = 1;
4154 else
4155 {
4156 i.tm.opcode_modifier.size = SIZE16;
4157 i.types[0].bitfield.imm16 = 1;
4158 }
4159 }
4160 else
4161 {
4162 /* Subject to further optimization below. */
4163 i.tm.base_opcode = 0xc7;
4164 i.tm.extension_opcode = 0;
4165 i.types[0].bitfield.imm32s = 1;
4166 i.types[0].bitfield.baseindex = 0;
4167 }
4168 }
4169 /* Outside of 64-bit mode address and operand sizes have to match if
4170 a relocation is involved, as otherwise we wouldn't (currently) or
4171 even couldn't express the relocation correctly. */
4172 else if (i.op[0].disps
4173 && i.op[0].disps->X_op != O_constant
4174 && ((!i.prefix[ADDR_PREFIX])
4175 != (flag_code == CODE_32BIT
4176 ? i.op[1].regs->reg_type.bitfield.dword
4177 : i.op[1].regs->reg_type.bitfield.word)))
4178 return;
7772f168
JB
4179 /* In 16-bit mode converting LEA with 16-bit addressing and a 32-bit
4180 destination is going to grow encoding size. */
4181 else if (flag_code == CODE_16BIT
4182 && (optimize <= 1 || optimize_for_space)
4183 && !i.prefix[ADDR_PREFIX]
4184 && i.op[1].regs->reg_type.bitfield.dword)
4185 return;
fe134c65
JB
4186 else
4187 {
4188 i.tm.base_opcode = 0xb8;
4189 i.tm.opcode_modifier.modrm = 0;
4190 if (i.op[1].regs->reg_type.bitfield.dword)
4191 i.types[0].bitfield.imm32 = 1;
4192 else
4193 i.types[0].bitfield.imm16 = 1;
4194
4195 if (i.op[0].disps
4196 && i.op[0].disps->X_op == O_constant
4197 && i.op[1].regs->reg_type.bitfield.dword
60cfa10c
L
4198 /* NB: Add () to !i.prefix[ADDR_PREFIX] to silence
4199 GCC 5. */
4200 && (!i.prefix[ADDR_PREFIX]) != (flag_code == CODE_32BIT))
fe134c65
JB
4201 i.op[0].disps->X_add_number &= 0xffff;
4202 }
4203
4204 i.tm.operand_types[0] = i.types[0];
4205 i.imm_operands = 1;
4206 if (!i.op[0].imms)
4207 {
4208 i.op[0].imms = &im_expressions[0];
4209 i.op[0].imms->X_op = O_absent;
4210 }
4211 }
4212 else if (i.op[0].disps
4213 && (i.op[0].disps->X_op != O_constant
4214 || i.op[0].disps->X_add_number))
4215 return;
4216 else
4217 {
4218 /* Handle:
4219 lea (%rM), %rN -> mov %rM, %rN
4220 lea (,%rM,1), %rN -> mov %rM, %rN
4221 lea (%rM), %rN -> movzx %rM, %rN
4222 */
4223 const reg_entry *addr_reg;
4224
4225 if (!i.index_reg && i.base_reg->reg_num != RegIP)
4226 addr_reg = i.base_reg;
4227 else if (!i.base_reg
4228 && i.index_reg->reg_num != RegIZ
4229 && !i.log2_scale_factor)
4230 addr_reg = i.index_reg;
4231 else
4232 return;
4233
4234 if (addr_reg->reg_type.bitfield.word
4235 && i.op[1].regs->reg_type.bitfield.dword)
4236 {
4237 if (flag_code != CODE_32BIT)
4238 return;
4239 i.tm.opcode_modifier.opcodespace = SPACE_0F;
4240 i.tm.base_opcode = 0xb7;
4241 }
4242 else
4243 i.tm.base_opcode = 0x8b;
4244
4245 if (addr_reg->reg_type.bitfield.dword
4246 && i.op[1].regs->reg_type.bitfield.qword)
4247 i.tm.opcode_modifier.size = SIZE32;
4248
4249 i.op[0].regs = addr_reg;
4250 i.reg_operands = 2;
4251 }
4252
4253 i.mem_operands = 0;
4254 i.disp_operands = 0;
4255 i.prefix[ADDR_PREFIX] = 0;
4256 i.prefix[SEG_PREFIX] = 0;
4257 i.seg[0] = NULL;
4258 }
4259
b6f8c7c4 4260 if (optimize_for_space
389d00a5 4261 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
b6f8c7c4
L
4262 && i.reg_operands == 1
4263 && i.imm_operands == 1
4264 && !i.types[1].bitfield.byte
4265 && i.op[0].imms->X_op == O_constant
4266 && fits_in_imm7 (i.op[0].imms->X_add_number)
72aea328 4267 && (i.tm.base_opcode == 0xa8
b6f8c7c4
L
4268 || (i.tm.base_opcode == 0xf6
4269 && i.tm.extension_opcode == 0x0)))
4270 {
4271 /* Optimize: -Os:
4272 test $imm7, %r64/%r32/%r16 -> test $imm7, %r8
4273 */
4274 unsigned int base_regnum = i.op[1].regs->reg_num;
4275 if (flag_code == CODE_64BIT || base_regnum < 4)
4276 {
4277 i.types[1].bitfield.byte = 1;
4278 /* Ignore the suffix. */
4279 i.suffix = 0;
7697afb6
JB
4280 /* Convert to byte registers. */
4281 if (i.types[1].bitfield.word)
4282 j = 16;
4283 else if (i.types[1].bitfield.dword)
4284 j = 32;
4285 else
4286 j = 48;
4287 if (!(i.op[1].regs->reg_flags & RegRex) && base_regnum < 4)
4288 j += 8;
4289 i.op[1].regs -= j;
b6f8c7c4
L
4290 }
4291 }
4292 else if (flag_code == CODE_64BIT
389d00a5 4293 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
d3d50934
L
4294 && ((i.types[1].bitfield.qword
4295 && i.reg_operands == 1
b6f8c7c4
L
4296 && i.imm_operands == 1
4297 && i.op[0].imms->X_op == O_constant
507916b8 4298 && ((i.tm.base_opcode == 0xb8
b6f8c7c4
L
4299 && i.tm.extension_opcode == None
4300 && fits_in_unsigned_long (i.op[0].imms->X_add_number))
4301 || (fits_in_imm31 (i.op[0].imms->X_add_number)
72aea328
JB
4302 && ((i.tm.base_opcode == 0x24
4303 || i.tm.base_opcode == 0xa8)
b6f8c7c4
L
4304 || (i.tm.base_opcode == 0x80
4305 && i.tm.extension_opcode == 0x4)
4306 || ((i.tm.base_opcode == 0xf6
507916b8 4307 || (i.tm.base_opcode | 1) == 0xc7)
b8364fa7
JB
4308 && i.tm.extension_opcode == 0x0)))
4309 || (fits_in_imm7 (i.op[0].imms->X_add_number)
4310 && i.tm.base_opcode == 0x83
4311 && i.tm.extension_opcode == 0x4)))
d3d50934
L
4312 || (i.types[0].bitfield.qword
4313 && ((i.reg_operands == 2
4314 && i.op[0].regs == i.op[1].regs
72aea328
JB
4315 && (i.tm.base_opcode == 0x30
4316 || i.tm.base_opcode == 0x28))
d3d50934
L
4317 || (i.reg_operands == 1
4318 && i.operands == 1
72aea328 4319 && i.tm.base_opcode == 0x30)))))
b6f8c7c4
L
4320 {
4321 /* Optimize: -O:
4322 andq $imm31, %r64 -> andl $imm31, %r32
b8364fa7 4323 andq $imm7, %r64 -> andl $imm7, %r32
b6f8c7c4
L
4324 testq $imm31, %r64 -> testl $imm31, %r32
4325 xorq %r64, %r64 -> xorl %r32, %r32
4326 subq %r64, %r64 -> subl %r32, %r32
4327 movq $imm31, %r64 -> movl $imm31, %r32
4328 movq $imm32, %r64 -> movl $imm32, %r32
4329 */
4330 i.tm.opcode_modifier.norex64 = 1;
507916b8 4331 if (i.tm.base_opcode == 0xb8 || (i.tm.base_opcode | 1) == 0xc7)
b6f8c7c4
L
4332 {
4333 /* Handle
4334 movq $imm31, %r64 -> movl $imm31, %r32
4335 movq $imm32, %r64 -> movl $imm32, %r32
4336 */
4337 i.tm.operand_types[0].bitfield.imm32 = 1;
4338 i.tm.operand_types[0].bitfield.imm32s = 0;
4339 i.tm.operand_types[0].bitfield.imm64 = 0;
4340 i.types[0].bitfield.imm32 = 1;
4341 i.types[0].bitfield.imm32s = 0;
4342 i.types[0].bitfield.imm64 = 0;
4343 i.types[1].bitfield.dword = 1;
4344 i.types[1].bitfield.qword = 0;
507916b8 4345 if ((i.tm.base_opcode | 1) == 0xc7)
b6f8c7c4
L
4346 {
4347 /* Handle
4348 movq $imm31, %r64 -> movl $imm31, %r32
4349 */
507916b8 4350 i.tm.base_opcode = 0xb8;
b6f8c7c4 4351 i.tm.extension_opcode = None;
507916b8 4352 i.tm.opcode_modifier.w = 0;
b6f8c7c4
L
4353 i.tm.opcode_modifier.modrm = 0;
4354 }
4355 }
4356 }
5641ec01
JB
4357 else if (optimize > 1
4358 && !optimize_for_space
389d00a5 4359 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
5641ec01
JB
4360 && i.reg_operands == 2
4361 && i.op[0].regs == i.op[1].regs
4362 && ((i.tm.base_opcode & ~(Opcode_D | 1)) == 0x8
4363 || (i.tm.base_opcode & ~(Opcode_D | 1)) == 0x20)
4364 && (flag_code != CODE_64BIT || !i.types[0].bitfield.dword))
4365 {
4366 /* Optimize: -O2:
4367 andb %rN, %rN -> testb %rN, %rN
4368 andw %rN, %rN -> testw %rN, %rN
4369 andq %rN, %rN -> testq %rN, %rN
4370 orb %rN, %rN -> testb %rN, %rN
4371 orw %rN, %rN -> testw %rN, %rN
4372 orq %rN, %rN -> testq %rN, %rN
4373
4374 and outside of 64-bit mode
4375
4376 andl %rN, %rN -> testl %rN, %rN
4377 orl %rN, %rN -> testl %rN, %rN
4378 */
4379 i.tm.base_opcode = 0x84 | (i.tm.base_opcode & 1);
4380 }
99112332 4381 else if (i.reg_operands == 3
b6f8c7c4
L
4382 && i.op[0].regs == i.op[1].regs
4383 && !i.types[2].bitfield.xmmword
4384 && (i.tm.opcode_modifier.vex
6225c532 4385 || ((!i.mask.reg || i.mask.zeroing)
ca5312a2 4386 && i.rounding.type == rc_none
e771e7c9 4387 && is_evex_encoding (&i.tm)
80c34c38 4388 && (i.vec_encoding != vex_encoding_evex
dd22218c 4389 || cpu_arch_isa_flags.bitfield.cpuavx512vl
80c34c38 4390 || i.tm.cpu_flags.bitfield.cpuavx512vl
7091c612 4391 || (i.tm.operand_types[2].bitfield.zmmword
dd22218c 4392 && i.types[2].bitfield.ymmword))))
b6f8c7c4 4393 && ((i.tm.base_opcode == 0x55
b6f8c7c4 4394 || i.tm.base_opcode == 0x57
35648716
JB
4395 || i.tm.base_opcode == 0xdf
4396 || i.tm.base_opcode == 0xef
4397 || i.tm.base_opcode == 0xf8
4398 || i.tm.base_opcode == 0xf9
4399 || i.tm.base_opcode == 0xfa
4400 || i.tm.base_opcode == 0xfb
1424ad86 4401 || i.tm.base_opcode == 0x42
35648716 4402 || i.tm.base_opcode == 0x47)
b6f8c7c4
L
4403 && i.tm.extension_opcode == None))
4404 {
99112332 4405 /* Optimize: -O1:
8305403a
L
4406 VOP, one of vandnps, vandnpd, vxorps, vxorpd, vpsubb, vpsubd,
4407 vpsubq and vpsubw:
b6f8c7c4
L
4408 EVEX VOP %zmmM, %zmmM, %zmmN
4409 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4410 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4411 EVEX VOP %ymmM, %ymmM, %ymmN
4412 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4413 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4414 VEX VOP %ymmM, %ymmM, %ymmN
4415 -> VEX VOP %xmmM, %xmmM, %xmmN
4416 VOP, one of vpandn and vpxor:
4417 VEX VOP %ymmM, %ymmM, %ymmN
4418 -> VEX VOP %xmmM, %xmmM, %xmmN
4419 VOP, one of vpandnd and vpandnq:
4420 EVEX VOP %zmmM, %zmmM, %zmmN
4421 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4422 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4423 EVEX VOP %ymmM, %ymmM, %ymmN
4424 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4425 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4426 VOP, one of vpxord and vpxorq:
4427 EVEX VOP %zmmM, %zmmM, %zmmN
4428 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4429 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4430 EVEX VOP %ymmM, %ymmM, %ymmN
4431 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4432 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
1424ad86
JB
4433 VOP, one of kxord and kxorq:
4434 VEX VOP %kM, %kM, %kN
4435 -> VEX kxorw %kM, %kM, %kN
4436 VOP, one of kandnd and kandnq:
4437 VEX VOP %kM, %kM, %kN
4438 -> VEX kandnw %kM, %kM, %kN
b6f8c7c4 4439 */
e771e7c9 4440 if (is_evex_encoding (&i.tm))
b6f8c7c4 4441 {
7b1d7ca1 4442 if (i.vec_encoding != vex_encoding_evex)
b6f8c7c4
L
4443 {
4444 i.tm.opcode_modifier.vex = VEX128;
4445 i.tm.opcode_modifier.vexw = VEXW0;
4446 i.tm.opcode_modifier.evex = 0;
4447 }
7b1d7ca1 4448 else if (optimize > 1)
dd22218c
L
4449 i.tm.opcode_modifier.evex = EVEX128;
4450 else
4451 return;
b6f8c7c4 4452 }
f74a6307 4453 else if (i.tm.operand_types[0].bitfield.class == RegMask)
1424ad86 4454 {
35648716 4455 i.tm.opcode_modifier.opcodeprefix = PREFIX_NONE;
1424ad86
JB
4456 i.tm.opcode_modifier.vexw = VEXW0;
4457 }
b6f8c7c4
L
4458 else
4459 i.tm.opcode_modifier.vex = VEX128;
4460
4461 if (i.tm.opcode_modifier.vex)
4462 for (j = 0; j < 3; j++)
4463 {
4464 i.types[j].bitfield.xmmword = 1;
4465 i.types[j].bitfield.ymmword = 0;
4466 }
4467 }
392a5972 4468 else if (i.vec_encoding != vex_encoding_evex
97ed31ae 4469 && !i.types[0].bitfield.zmmword
392a5972 4470 && !i.types[1].bitfield.zmmword
6225c532 4471 && !i.mask.reg
5273a3cd 4472 && !i.broadcast.type
97ed31ae 4473 && is_evex_encoding (&i.tm)
35648716
JB
4474 && ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4475 || (i.tm.base_opcode & ~4) == 0xdb
4476 || (i.tm.base_opcode & ~4) == 0xeb)
97ed31ae
L
4477 && i.tm.extension_opcode == None)
4478 {
4479 /* Optimize: -O1:
4480 VOP, one of vmovdqa32, vmovdqa64, vmovdqu8, vmovdqu16,
4481 vmovdqu32 and vmovdqu64:
4482 EVEX VOP %xmmM, %xmmN
4483 -> VEX vmovdqa|vmovdqu %xmmM, %xmmN (M and N < 16)
4484 EVEX VOP %ymmM, %ymmN
4485 -> VEX vmovdqa|vmovdqu %ymmM, %ymmN (M and N < 16)
4486 EVEX VOP %xmmM, mem
4487 -> VEX vmovdqa|vmovdqu %xmmM, mem (M < 16)
4488 EVEX VOP %ymmM, mem
4489 -> VEX vmovdqa|vmovdqu %ymmM, mem (M < 16)
4490 EVEX VOP mem, %xmmN
4491 -> VEX mvmovdqa|vmovdquem, %xmmN (N < 16)
4492 EVEX VOP mem, %ymmN
4493 -> VEX vmovdqa|vmovdqu mem, %ymmN (N < 16)
a0a1771e
JB
4494 VOP, one of vpand, vpandn, vpor, vpxor:
4495 EVEX VOP{d,q} %xmmL, %xmmM, %xmmN
4496 -> VEX VOP %xmmL, %xmmM, %xmmN (L, M, and N < 16)
4497 EVEX VOP{d,q} %ymmL, %ymmM, %ymmN
4498 -> VEX VOP %ymmL, %ymmM, %ymmN (L, M, and N < 16)
4499 EVEX VOP{d,q} mem, %xmmM, %xmmN
4500 -> VEX VOP mem, %xmmM, %xmmN (M and N < 16)
4501 EVEX VOP{d,q} mem, %ymmM, %ymmN
4502 -> VEX VOP mem, %ymmM, %ymmN (M and N < 16)
97ed31ae 4503 */
a0a1771e 4504 for (j = 0; j < i.operands; j++)
392a5972
L
4505 if (operand_type_check (i.types[j], disp)
4506 && i.op[j].disps->X_op == O_constant)
4507 {
4508 /* Since the VEX prefix has 2 or 3 bytes, the EVEX prefix
4509 has 4 bytes, EVEX Disp8 has 1 byte and VEX Disp32 has 4
4510 bytes, we choose EVEX Disp8 over VEX Disp32. */
4511 int evex_disp8, vex_disp8;
4512 unsigned int memshift = i.memshift;
4513 offsetT n = i.op[j].disps->X_add_number;
4514
4515 evex_disp8 = fits_in_disp8 (n);
4516 i.memshift = 0;
4517 vex_disp8 = fits_in_disp8 (n);
4518 if (evex_disp8 != vex_disp8)
4519 {
4520 i.memshift = memshift;
4521 return;
4522 }
4523
4524 i.types[j].bitfield.disp8 = vex_disp8;
4525 break;
4526 }
35648716
JB
4527 if ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4528 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2)
4529 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
97ed31ae
L
4530 i.tm.opcode_modifier.vex
4531 = i.types[0].bitfield.ymmword ? VEX256 : VEX128;
4532 i.tm.opcode_modifier.vexw = VEXW0;
79dec6b7 4533 /* VPAND, VPOR, and VPXOR are commutative. */
35648716 4534 if (i.reg_operands == 3 && i.tm.base_opcode != 0xdf)
79dec6b7 4535 i.tm.opcode_modifier.commutative = 1;
97ed31ae
L
4536 i.tm.opcode_modifier.evex = 0;
4537 i.tm.opcode_modifier.masking = 0;
a0a1771e 4538 i.tm.opcode_modifier.broadcast = 0;
97ed31ae
L
4539 i.tm.opcode_modifier.disp8memshift = 0;
4540 i.memshift = 0;
a0a1771e
JB
4541 if (j < i.operands)
4542 i.types[j].bitfield.disp8
4543 = fits_in_disp8 (i.op[j].disps->X_add_number);
97ed31ae 4544 }
b6f8c7c4
L
4545}
4546
ae531041
L
4547/* Return non-zero for load instruction. */
4548
4549static int
4550load_insn_p (void)
4551{
4552 unsigned int dest;
4553 int any_vex_p = is_any_vex_encoding (&i.tm);
4554 unsigned int base_opcode = i.tm.base_opcode | 1;
4555
4556 if (!any_vex_p)
4557 {
a09f656b 4558 /* Anysize insns: lea, invlpg, clflush, prefetchnta, prefetcht0,
4559 prefetcht1, prefetcht2, prefetchtw, bndmk, bndcl, bndcu, bndcn,
4560 bndstx, bndldx, prefetchwt1, clflushopt, clwb, cldemote. */
4561 if (i.tm.opcode_modifier.anysize)
ae531041
L
4562 return 0;
4563
389d00a5
JB
4564 /* pop. */
4565 if (strcmp (i.tm.name, "pop") == 0)
4566 return 1;
4567 }
4568
4569 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE)
4570 {
4571 /* popf, popa. */
4572 if (i.tm.base_opcode == 0x9d
a09f656b 4573 || i.tm.base_opcode == 0x61)
ae531041
L
4574 return 1;
4575
4576 /* movs, cmps, lods, scas. */
4577 if ((i.tm.base_opcode | 0xb) == 0xaf)
4578 return 1;
4579
a09f656b 4580 /* outs, xlatb. */
4581 if (base_opcode == 0x6f
4582 || i.tm.base_opcode == 0xd7)
ae531041 4583 return 1;
a09f656b 4584 /* NB: For AMD-specific insns with implicit memory operands,
4585 they're intentionally not covered. */
ae531041
L
4586 }
4587
4588 /* No memory operand. */
4589 if (!i.mem_operands)
4590 return 0;
4591
4592 if (any_vex_p)
4593 {
4594 /* vldmxcsr. */
4595 if (i.tm.base_opcode == 0xae
4596 && i.tm.opcode_modifier.vex
441f6aca 4597 && i.tm.opcode_modifier.opcodespace == SPACE_0F
35648716 4598 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
ae531041
L
4599 && i.tm.extension_opcode == 2)
4600 return 1;
4601 }
389d00a5 4602 else if (i.tm.opcode_modifier.opcodespace == SPACE_BASE)
ae531041
L
4603 {
4604 /* test, not, neg, mul, imul, div, idiv. */
4605 if ((i.tm.base_opcode == 0xf6 || i.tm.base_opcode == 0xf7)
4606 && i.tm.extension_opcode != 1)
4607 return 1;
4608
4609 /* inc, dec. */
4610 if (base_opcode == 0xff && i.tm.extension_opcode <= 1)
4611 return 1;
4612
4613 /* add, or, adc, sbb, and, sub, xor, cmp. */
4614 if (i.tm.base_opcode >= 0x80 && i.tm.base_opcode <= 0x83)
4615 return 1;
4616
ae531041
L
4617 /* rol, ror, rcl, rcr, shl/sal, shr, sar. */
4618 if ((base_opcode == 0xc1
4619 || (i.tm.base_opcode >= 0xd0 && i.tm.base_opcode <= 0xd3))
4620 && i.tm.extension_opcode != 6)
4621 return 1;
4622
ae531041 4623 /* Check for x87 instructions. */
389d00a5 4624 if (base_opcode >= 0xd8 && base_opcode <= 0xdf)
ae531041
L
4625 {
4626 /* Skip fst, fstp, fstenv, fstcw. */
4627 if (i.tm.base_opcode == 0xd9
4628 && (i.tm.extension_opcode == 2
4629 || i.tm.extension_opcode == 3
4630 || i.tm.extension_opcode == 6
4631 || i.tm.extension_opcode == 7))
4632 return 0;
4633
4634 /* Skip fisttp, fist, fistp, fstp. */
4635 if (i.tm.base_opcode == 0xdb
4636 && (i.tm.extension_opcode == 1
4637 || i.tm.extension_opcode == 2
4638 || i.tm.extension_opcode == 3
4639 || i.tm.extension_opcode == 7))
4640 return 0;
4641
4642 /* Skip fisttp, fst, fstp, fsave, fstsw. */
4643 if (i.tm.base_opcode == 0xdd
4644 && (i.tm.extension_opcode == 1
4645 || i.tm.extension_opcode == 2
4646 || i.tm.extension_opcode == 3
4647 || i.tm.extension_opcode == 6
4648 || i.tm.extension_opcode == 7))
4649 return 0;
4650
4651 /* Skip fisttp, fist, fistp, fbstp, fistp. */
4652 if (i.tm.base_opcode == 0xdf
4653 && (i.tm.extension_opcode == 1
4654 || i.tm.extension_opcode == 2
4655 || i.tm.extension_opcode == 3
4656 || i.tm.extension_opcode == 6
4657 || i.tm.extension_opcode == 7))
4658 return 0;
4659
4660 return 1;
4661 }
4662 }
389d00a5
JB
4663 else if (i.tm.opcode_modifier.opcodespace == SPACE_0F)
4664 {
4665 /* bt, bts, btr, btc. */
4666 if (i.tm.base_opcode == 0xba
4667 && (i.tm.extension_opcode >= 4 && i.tm.extension_opcode <= 7))
4668 return 1;
4669
4670 /* cmpxchg8b, cmpxchg16b, xrstors, vmptrld. */
4671 if (i.tm.base_opcode == 0xc7
4672 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
4673 && (i.tm.extension_opcode == 1 || i.tm.extension_opcode == 3
4674 || i.tm.extension_opcode == 6))
4675 return 1;
4676
4677 /* fxrstor, ldmxcsr, xrstor. */
4678 if (i.tm.base_opcode == 0xae
4679 && (i.tm.extension_opcode == 1
4680 || i.tm.extension_opcode == 2
4681 || i.tm.extension_opcode == 5))
4682 return 1;
4683
4684 /* lgdt, lidt, lmsw. */
4685 if (i.tm.base_opcode == 0x01
4686 && (i.tm.extension_opcode == 2
4687 || i.tm.extension_opcode == 3
4688 || i.tm.extension_opcode == 6))
4689 return 1;
4690 }
ae531041
L
4691
4692 dest = i.operands - 1;
4693
4694 /* Check fake imm8 operand and 3 source operands. */
4695 if ((i.tm.opcode_modifier.immext
4696 || i.tm.opcode_modifier.vexsources == VEX3SOURCES)
4697 && i.types[dest].bitfield.imm8)
4698 dest--;
4699
389d00a5
JB
4700 /* add, or, adc, sbb, and, sub, xor, cmp, test, xchg. */
4701 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
ae531041
L
4702 && (base_opcode == 0x1
4703 || base_opcode == 0x9
4704 || base_opcode == 0x11
4705 || base_opcode == 0x19
4706 || base_opcode == 0x21
4707 || base_opcode == 0x29
4708 || base_opcode == 0x31
4709 || base_opcode == 0x39
389d00a5
JB
4710 || (base_opcode | 2) == 0x87))
4711 return 1;
4712
4713 /* xadd. */
4714 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4715 && base_opcode == 0xc1)
ae531041
L
4716 return 1;
4717
4718 /* Check for load instruction. */
4719 return (i.types[dest].bitfield.class != ClassNone
4720 || i.types[dest].bitfield.instance == Accum);
4721}
4722
4723/* Output lfence, 0xfaee8, after instruction. */
4724
4725static void
4726insert_lfence_after (void)
4727{
4728 if (lfence_after_load && load_insn_p ())
4729 {
a09f656b 4730 /* There are also two REP string instructions that require
4731 special treatment. Specifically, the compare string (CMPS)
4732 and scan string (SCAS) instructions set EFLAGS in a manner
4733 that depends on the data being compared/scanned. When used
4734 with a REP prefix, the number of iterations may therefore
4735 vary depending on this data. If the data is a program secret
4736 chosen by the adversary using an LVI method,
4737 then this data-dependent behavior may leak some aspect
4738 of the secret. */
4739 if (((i.tm.base_opcode | 0x1) == 0xa7
4740 || (i.tm.base_opcode | 0x1) == 0xaf)
4741 && i.prefix[REP_PREFIX])
4742 {
4743 as_warn (_("`%s` changes flags which would affect control flow behavior"),
4744 i.tm.name);
4745 }
ae531041
L
4746 char *p = frag_more (3);
4747 *p++ = 0xf;
4748 *p++ = 0xae;
4749 *p = 0xe8;
4750 }
4751}
4752
4753/* Output lfence, 0xfaee8, before instruction. */
4754
4755static void
4756insert_lfence_before (void)
4757{
4758 char *p;
4759
389d00a5 4760 if (i.tm.opcode_modifier.opcodespace != SPACE_BASE)
ae531041
L
4761 return;
4762
4763 if (i.tm.base_opcode == 0xff
4764 && (i.tm.extension_opcode == 2 || i.tm.extension_opcode == 4))
4765 {
4766 /* Insert lfence before indirect branch if needed. */
4767
4768 if (lfence_before_indirect_branch == lfence_branch_none)
4769 return;
4770
4771 if (i.operands != 1)
4772 abort ();
4773
4774 if (i.reg_operands == 1)
4775 {
4776 /* Indirect branch via register. Don't insert lfence with
4777 -mlfence-after-load=yes. */
4778 if (lfence_after_load
4779 || lfence_before_indirect_branch == lfence_branch_memory)
4780 return;
4781 }
4782 else if (i.mem_operands == 1
4783 && lfence_before_indirect_branch != lfence_branch_register)
4784 {
4785 as_warn (_("indirect `%s` with memory operand should be avoided"),
4786 i.tm.name);
4787 return;
4788 }
4789 else
4790 return;
4791
4792 if (last_insn.kind != last_insn_other
4793 && last_insn.seg == now_seg)
4794 {
4795 as_warn_where (last_insn.file, last_insn.line,
4796 _("`%s` skips -mlfence-before-indirect-branch on `%s`"),
4797 last_insn.name, i.tm.name);
4798 return;
4799 }
4800
4801 p = frag_more (3);
4802 *p++ = 0xf;
4803 *p++ = 0xae;
4804 *p = 0xe8;
4805 return;
4806 }
4807
503648e4 4808 /* Output or/not/shl and lfence before near ret. */
ae531041
L
4809 if (lfence_before_ret != lfence_before_ret_none
4810 && (i.tm.base_opcode == 0xc2
503648e4 4811 || i.tm.base_opcode == 0xc3))
ae531041
L
4812 {
4813 if (last_insn.kind != last_insn_other
4814 && last_insn.seg == now_seg)
4815 {
4816 as_warn_where (last_insn.file, last_insn.line,
4817 _("`%s` skips -mlfence-before-ret on `%s`"),
4818 last_insn.name, i.tm.name);
4819 return;
4820 }
a09f656b 4821
a09f656b 4822 /* Near ret ingore operand size override under CPU64. */
503648e4 4823 char prefix = flag_code == CODE_64BIT
4824 ? 0x48
4825 : i.prefix[DATA_PREFIX] ? 0x66 : 0x0;
a09f656b 4826
4827 if (lfence_before_ret == lfence_before_ret_not)
4828 {
4829 /* not: 0xf71424, may add prefix
4830 for operand size override or 64-bit code. */
4831 p = frag_more ((prefix ? 2 : 0) + 6 + 3);
4832 if (prefix)
4833 *p++ = prefix;
ae531041
L
4834 *p++ = 0xf7;
4835 *p++ = 0x14;
4836 *p++ = 0x24;
a09f656b 4837 if (prefix)
4838 *p++ = prefix;
ae531041
L
4839 *p++ = 0xf7;
4840 *p++ = 0x14;
4841 *p++ = 0x24;
4842 }
a09f656b 4843 else
4844 {
4845 p = frag_more ((prefix ? 1 : 0) + 4 + 3);
4846 if (prefix)
4847 *p++ = prefix;
4848 if (lfence_before_ret == lfence_before_ret_or)
4849 {
4850 /* or: 0x830c2400, may add prefix
4851 for operand size override or 64-bit code. */
4852 *p++ = 0x83;
4853 *p++ = 0x0c;
4854 }
4855 else
4856 {
4857 /* shl: 0xc1242400, may add prefix
4858 for operand size override or 64-bit code. */
4859 *p++ = 0xc1;
4860 *p++ = 0x24;
4861 }
4862
4863 *p++ = 0x24;
4864 *p++ = 0x0;
4865 }
4866
ae531041
L
4867 *p++ = 0xf;
4868 *p++ = 0xae;
4869 *p = 0xe8;
4870 }
4871}
4872
252b5132
RH
4873/* This is the guts of the machine-dependent assembler. LINE points to a
4874 machine dependent instruction. This function is supposed to emit
4875 the frags/bytes it assembles to. */
4876
4877void
65da13b5 4878md_assemble (char *line)
252b5132 4879{
40fb9820 4880 unsigned int j;
83b16ac6 4881 char mnemonic[MAX_MNEM_SIZE], mnem_suffix;
d3ce72d0 4882 const insn_template *t;
252b5132 4883
47926f60 4884 /* Initialize globals. */
252b5132 4885 memset (&i, '\0', sizeof (i));
ca5312a2 4886 i.rounding.type = rc_none;
252b5132 4887 for (j = 0; j < MAX_OPERANDS; j++)
1ae12ab7 4888 i.reloc[j] = NO_RELOC;
252b5132
RH
4889 memset (disp_expressions, '\0', sizeof (disp_expressions));
4890 memset (im_expressions, '\0', sizeof (im_expressions));
ce8a8b2f 4891 save_stack_p = save_stack;
252b5132
RH
4892
4893 /* First parse an instruction mnemonic & call i386_operand for the operands.
4894 We assume that the scrubber has arranged it so that line[0] is the valid
47926f60 4895 start of a (possibly prefixed) mnemonic. */
252b5132 4896
29b0f896
AM
4897 line = parse_insn (line, mnemonic);
4898 if (line == NULL)
4899 return;
83b16ac6 4900 mnem_suffix = i.suffix;
252b5132 4901
29b0f896 4902 line = parse_operands (line, mnemonic);
ee86248c 4903 this_operand = -1;
8325cc63
JB
4904 xfree (i.memop1_string);
4905 i.memop1_string = NULL;
29b0f896
AM
4906 if (line == NULL)
4907 return;
252b5132 4908
29b0f896
AM
4909 /* Now we've parsed the mnemonic into a set of templates, and have the
4910 operands at hand. */
4911
b630c145 4912 /* All Intel opcodes have reversed operands except for "bound", "enter",
c0e54661
JB
4913 "invlpg*", "monitor*", "mwait*", "tpause", "umwait", "pvalidate",
4914 "rmpadjust", and "rmpupdate". We also don't reverse intersegment "jmp"
4915 and "call" instructions with 2 immediate operands so that the immediate
4916 segment precedes the offset consistently in Intel and AT&T modes. */
4d456e3d
L
4917 if (intel_syntax
4918 && i.operands > 1
29b0f896 4919 && (strcmp (mnemonic, "bound") != 0)
c0e54661 4920 && (strncmp (mnemonic, "invlpg", 6) != 0)
d34049e8
ML
4921 && !startswith (mnemonic, "monitor")
4922 && !startswith (mnemonic, "mwait")
c0e54661 4923 && (strcmp (mnemonic, "pvalidate") != 0)
d34049e8 4924 && !startswith (mnemonic, "rmp")
b630c145
JB
4925 && (strcmp (mnemonic, "tpause") != 0)
4926 && (strcmp (mnemonic, "umwait") != 0)
40fb9820
L
4927 && !(operand_type_check (i.types[0], imm)
4928 && operand_type_check (i.types[1], imm)))
29b0f896
AM
4929 swap_operands ();
4930
ec56d5c0
JB
4931 /* The order of the immediates should be reversed
4932 for 2 immediates extrq and insertq instructions */
4933 if (i.imm_operands == 2
4934 && (strcmp (mnemonic, "extrq") == 0
4935 || strcmp (mnemonic, "insertq") == 0))
4936 swap_2_operands (0, 1);
4937
29b0f896
AM
4938 if (i.imm_operands)
4939 optimize_imm ();
4940
a9aabc23 4941 if (i.disp_operands && !want_disp32 (current_templates->start))
cce08655
JB
4942 {
4943 for (j = 0; j < i.operands; ++j)
4944 {
4945 const expressionS *exp = i.op[j].disps;
4946
4947 if (!operand_type_check (i.types[j], disp))
4948 continue;
4949
4950 if (exp->X_op != O_constant)
4951 continue;
4952
4953 /* Since displacement is signed extended to 64bit, don't allow
4954 disp32 and turn off disp32s if they are out of range. */
4955 i.types[j].bitfield.disp32 = 0;
4956 if (fits_in_signed_long (exp->X_add_number))
4957 continue;
4958
4959 i.types[j].bitfield.disp32s = 0;
4960 if (i.types[j].bitfield.baseindex)
4961 {
ffa5352c
NC
4962 char number_buf[128];
4963
4964 /* Coded this way in order to allow for ease of translation. */
4965 sprintf_vma (number_buf, exp->X_add_number);
4966 as_bad (_("0x%s out of range of signed 32bit displacement"),
4967 number_buf);
cce08655
JB
4968 return;
4969 }
4970 }
4971 }
4972
b300c311
L
4973 /* Don't optimize displacement for movabs since it only takes 64bit
4974 displacement. */
4975 if (i.disp_operands
a501d77e 4976 && i.disp_encoding != disp_encoding_32bit
862be3fb
L
4977 && (flag_code != CODE_64BIT
4978 || strcmp (mnemonic, "movabs") != 0))
4979 optimize_disp ();
29b0f896
AM
4980
4981 /* Next, we find a template that matches the given insn,
4982 making sure the overlap of the given operands types is consistent
4983 with the template operand types. */
252b5132 4984
83b16ac6 4985 if (!(t = match_template (mnem_suffix)))
29b0f896 4986 return;
252b5132 4987
7bab8ab5 4988 if (sse_check != check_none
81f8a913 4989 && !i.tm.opcode_modifier.noavx
6e3e5c9e 4990 && !i.tm.cpu_flags.bitfield.cpuavx
569d50f1 4991 && !i.tm.cpu_flags.bitfield.cpuavx512f
daf50ae7
L
4992 && (i.tm.cpu_flags.bitfield.cpusse
4993 || i.tm.cpu_flags.bitfield.cpusse2
4994 || i.tm.cpu_flags.bitfield.cpusse3
4995 || i.tm.cpu_flags.bitfield.cpussse3
4996 || i.tm.cpu_flags.bitfield.cpusse4_1
6e3e5c9e
JB
4997 || i.tm.cpu_flags.bitfield.cpusse4_2
4998 || i.tm.cpu_flags.bitfield.cpupclmul
4999 || i.tm.cpu_flags.bitfield.cpuaes
569d50f1 5000 || i.tm.cpu_flags.bitfield.cpusha
6e3e5c9e 5001 || i.tm.cpu_flags.bitfield.cpugfni))
daf50ae7 5002 {
7bab8ab5 5003 (sse_check == check_warning
daf50ae7
L
5004 ? as_warn
5005 : as_bad) (_("SSE instruction `%s' is used"), i.tm.name);
5006 }
5007
40fb9820 5008 if (i.tm.opcode_modifier.fwait)
29b0f896
AM
5009 if (!add_prefix (FWAIT_OPCODE))
5010 return;
252b5132 5011
d5de92cf 5012 /* Check if REP prefix is OK. */
742732c7 5013 if (i.rep_prefix && i.tm.opcode_modifier.prefixok != PrefixRep)
d5de92cf
L
5014 {
5015 as_bad (_("invalid instruction `%s' after `%s'"),
5016 i.tm.name, i.rep_prefix);
5017 return;
5018 }
5019
c1ba0266
L
5020 /* Check for lock without a lockable instruction. Destination operand
5021 must be memory unless it is xchg (0x86). */
c32fa91d 5022 if (i.prefix[LOCK_PREFIX]
742732c7 5023 && (i.tm.opcode_modifier.prefixok < PrefixLock
c1ba0266
L
5024 || i.mem_operands == 0
5025 || (i.tm.base_opcode != 0x86
8dc0818e 5026 && !(i.flags[i.operands - 1] & Operand_Mem))))
c32fa91d
L
5027 {
5028 as_bad (_("expecting lockable instruction after `lock'"));
5029 return;
5030 }
5031
40d231b4
JB
5032 /* Check for data size prefix on VEX/XOP/EVEX encoded and SIMD insns. */
5033 if (i.prefix[DATA_PREFIX]
5034 && (is_any_vex_encoding (&i.tm)
5035 || i.tm.operand_types[i.imm_operands].bitfield.class >= RegMMX
5036 || i.tm.operand_types[i.imm_operands + 1].bitfield.class >= RegMMX))
7a8655d2
JB
5037 {
5038 as_bad (_("data size prefix invalid with `%s'"), i.tm.name);
5039 return;
5040 }
5041
42164a71 5042 /* Check if HLE prefix is OK. */
165de32a 5043 if (i.hle_prefix && !check_hle ())
42164a71
L
5044 return;
5045
7e8b059b
L
5046 /* Check BND prefix. */
5047 if (i.bnd_prefix && !i.tm.opcode_modifier.bndprefixok)
5048 as_bad (_("expecting valid branch instruction after `bnd'"));
5049
04ef582a 5050 /* Check NOTRACK prefix. */
742732c7 5051 if (i.notrack_prefix && i.tm.opcode_modifier.prefixok != PrefixNoTrack)
9fef80d6 5052 as_bad (_("expecting indirect branch instruction after `notrack'"));
04ef582a 5053
327e8c42
JB
5054 if (i.tm.cpu_flags.bitfield.cpumpx)
5055 {
5056 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
5057 as_bad (_("32-bit address isn't allowed in 64-bit MPX instructions."));
5058 else if (flag_code != CODE_16BIT
5059 ? i.prefix[ADDR_PREFIX]
5060 : i.mem_operands && !i.prefix[ADDR_PREFIX])
5061 as_bad (_("16-bit address isn't allowed in MPX instructions"));
5062 }
7e8b059b
L
5063
5064 /* Insert BND prefix. */
76d3a78a
JB
5065 if (add_bnd_prefix && i.tm.opcode_modifier.bndprefixok)
5066 {
5067 if (!i.prefix[BND_PREFIX])
5068 add_prefix (BND_PREFIX_OPCODE);
5069 else if (i.prefix[BND_PREFIX] != BND_PREFIX_OPCODE)
5070 {
5071 as_warn (_("replacing `rep'/`repe' prefix by `bnd'"));
5072 i.prefix[BND_PREFIX] = BND_PREFIX_OPCODE;
5073 }
5074 }
7e8b059b 5075
29b0f896 5076 /* Check string instruction segment overrides. */
51c8edf6 5077 if (i.tm.opcode_modifier.isstring >= IS_STRING_ES_OP0)
29b0f896 5078 {
51c8edf6 5079 gas_assert (i.mem_operands);
29b0f896 5080 if (!check_string ())
5dd0794d 5081 return;
fc0763e6 5082 i.disp_operands = 0;
29b0f896 5083 }
5dd0794d 5084
b6f8c7c4
L
5085 if (optimize && !i.no_optimize && i.tm.opcode_modifier.optimize)
5086 optimize_encoding ();
5087
c8480b58
L
5088 if (use_unaligned_vector_move)
5089 encode_with_unaligned_vector_move ();
5090
29b0f896
AM
5091 if (!process_suffix ())
5092 return;
e413e4e9 5093
921eafea 5094 /* Update operand types and check extended states. */
bc0844ae 5095 for (j = 0; j < i.operands; j++)
921eafea
L
5096 {
5097 i.types[j] = operand_type_and (i.types[j], i.tm.operand_types[j]);
3d70986f 5098 switch (i.tm.operand_types[j].bitfield.class)
921eafea
L
5099 {
5100 default:
5101 break;
5102 case RegMMX:
5103 i.xstate |= xstate_mmx;
5104 break;
5105 case RegMask:
32930e4e 5106 i.xstate |= xstate_mask;
921eafea
L
5107 break;
5108 case RegSIMD:
3d70986f 5109 if (i.tm.operand_types[j].bitfield.tmmword)
921eafea 5110 i.xstate |= xstate_tmm;
3d70986f 5111 else if (i.tm.operand_types[j].bitfield.zmmword)
921eafea 5112 i.xstate |= xstate_zmm;
3d70986f 5113 else if (i.tm.operand_types[j].bitfield.ymmword)
921eafea 5114 i.xstate |= xstate_ymm;
3d70986f 5115 else if (i.tm.operand_types[j].bitfield.xmmword)
921eafea
L
5116 i.xstate |= xstate_xmm;
5117 break;
5118 }
5119 }
bc0844ae 5120
29b0f896
AM
5121 /* Make still unresolved immediate matches conform to size of immediate
5122 given in i.suffix. */
5123 if (!finalize_imm ())
5124 return;
252b5132 5125
40fb9820 5126 if (i.types[0].bitfield.imm1)
29b0f896 5127 i.imm_operands = 0; /* kludge for shift insns. */
252b5132 5128
9afe6eb8
L
5129 /* We only need to check those implicit registers for instructions
5130 with 3 operands or less. */
5131 if (i.operands <= 3)
5132 for (j = 0; j < i.operands; j++)
75e5731b
JB
5133 if (i.types[j].bitfield.instance != InstanceNone
5134 && !i.types[j].bitfield.xmmword)
9afe6eb8 5135 i.reg_operands--;
40fb9820 5136
29b0f896
AM
5137 /* For insns with operands there are more diddles to do to the opcode. */
5138 if (i.operands)
5139 {
5140 if (!process_operands ())
5141 return;
5142 }
8c190ce0 5143 else if (!quiet_warnings && i.tm.opcode_modifier.ugh)
29b0f896
AM
5144 {
5145 /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc. */
5146 as_warn (_("translating to `%sp'"), i.tm.name);
5147 }
252b5132 5148
7a8655d2 5149 if (is_any_vex_encoding (&i.tm))
9e5e5283 5150 {
c1dc7af5 5151 if (!cpu_arch_flags.bitfield.cpui286)
9e5e5283 5152 {
c1dc7af5 5153 as_bad (_("instruction `%s' isn't supported outside of protected mode."),
9e5e5283
L
5154 i.tm.name);
5155 return;
5156 }
c0f3af97 5157
0b9404fd
JB
5158 /* Check for explicit REX prefix. */
5159 if (i.prefix[REX_PREFIX] || i.rex_encoding)
5160 {
5161 as_bad (_("REX prefix invalid with `%s'"), i.tm.name);
5162 return;
5163 }
5164
9e5e5283
L
5165 if (i.tm.opcode_modifier.vex)
5166 build_vex_prefix (t);
5167 else
5168 build_evex_prefix ();
0b9404fd
JB
5169
5170 /* The individual REX.RXBW bits got consumed. */
5171 i.rex &= REX_OPCODE;
9e5e5283 5172 }
43234a1e 5173
5dd85c99
SP
5174 /* Handle conversion of 'int $3' --> special int3 insn. XOP or FMA4
5175 instructions may define INT_OPCODE as well, so avoid this corner
5176 case for those instructions that use MODRM. */
389d00a5
JB
5177 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
5178 && i.tm.base_opcode == INT_OPCODE
a6461c02
SP
5179 && !i.tm.opcode_modifier.modrm
5180 && i.op[0].imms->X_add_number == 3)
29b0f896
AM
5181 {
5182 i.tm.base_opcode = INT3_OPCODE;
5183 i.imm_operands = 0;
5184 }
252b5132 5185
0cfa3eb3
JB
5186 if ((i.tm.opcode_modifier.jump == JUMP
5187 || i.tm.opcode_modifier.jump == JUMP_BYTE
5188 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896
AM
5189 && i.op[0].disps->X_op == O_constant)
5190 {
5191 /* Convert "jmp constant" (and "call constant") to a jump (call) to
5192 the absolute address given by the constant. Since ix86 jumps and
5193 calls are pc relative, we need to generate a reloc. */
5194 i.op[0].disps->X_add_symbol = &abs_symbol;
5195 i.op[0].disps->X_op = O_symbol;
5196 }
252b5132 5197
29b0f896
AM
5198 /* For 8 bit registers we need an empty rex prefix. Also if the
5199 instruction already has a prefix, we need to convert old
5200 registers to new ones. */
773f551c 5201
bab6aec1 5202 if ((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte
29b0f896 5203 && (i.op[0].regs->reg_flags & RegRex64) != 0)
bab6aec1 5204 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte
29b0f896 5205 && (i.op[1].regs->reg_flags & RegRex64) != 0)
bab6aec1
JB
5206 || (((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte)
5207 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte))
29b0f896
AM
5208 && i.rex != 0))
5209 {
5210 int x;
726c5dcd 5211
29b0f896
AM
5212 i.rex |= REX_OPCODE;
5213 for (x = 0; x < 2; x++)
5214 {
5215 /* Look for 8 bit operand that uses old registers. */
bab6aec1 5216 if (i.types[x].bitfield.class == Reg && i.types[x].bitfield.byte
29b0f896 5217 && (i.op[x].regs->reg_flags & RegRex64) == 0)
773f551c 5218 {
3f93af61 5219 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
29b0f896
AM
5220 /* In case it is "hi" register, give up. */
5221 if (i.op[x].regs->reg_num > 3)
a540244d 5222 as_bad (_("can't encode register '%s%s' in an "
4eed87de 5223 "instruction requiring REX prefix."),
a540244d 5224 register_prefix, i.op[x].regs->reg_name);
773f551c 5225
29b0f896
AM
5226 /* Otherwise it is equivalent to the extended register.
5227 Since the encoding doesn't change this is merely
5228 cosmetic cleanup for debug output. */
5229
5230 i.op[x].regs = i.op[x].regs + 8;
773f551c 5231 }
29b0f896
AM
5232 }
5233 }
773f551c 5234
6b6b6807
L
5235 if (i.rex == 0 && i.rex_encoding)
5236 {
5237 /* Check if we can add a REX_OPCODE byte. Look for 8 bit operand
3f93af61 5238 that uses legacy register. If it is "hi" register, don't add
6b6b6807
L
5239 the REX_OPCODE byte. */
5240 int x;
5241 for (x = 0; x < 2; x++)
bab6aec1 5242 if (i.types[x].bitfield.class == Reg
6b6b6807
L
5243 && i.types[x].bitfield.byte
5244 && (i.op[x].regs->reg_flags & RegRex64) == 0
5245 && i.op[x].regs->reg_num > 3)
5246 {
3f93af61 5247 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
5b7c81bd 5248 i.rex_encoding = false;
6b6b6807
L
5249 break;
5250 }
5251
5252 if (i.rex_encoding)
5253 i.rex = REX_OPCODE;
5254 }
5255
7ab9ffdd 5256 if (i.rex != 0)
29b0f896
AM
5257 add_prefix (REX_OPCODE | i.rex);
5258
ae531041
L
5259 insert_lfence_before ();
5260
29b0f896
AM
5261 /* We are ready to output the insn. */
5262 output_insn ();
e379e5f3 5263
ae531041
L
5264 insert_lfence_after ();
5265
e379e5f3
L
5266 last_insn.seg = now_seg;
5267
5268 if (i.tm.opcode_modifier.isprefix)
5269 {
5270 last_insn.kind = last_insn_prefix;
5271 last_insn.name = i.tm.name;
5272 last_insn.file = as_where (&last_insn.line);
5273 }
5274 else
5275 last_insn.kind = last_insn_other;
29b0f896
AM
5276}
5277
5278static char *
e3bb37b5 5279parse_insn (char *line, char *mnemonic)
29b0f896
AM
5280{
5281 char *l = line;
5282 char *token_start = l;
5283 char *mnem_p;
5c6af06e 5284 int supported;
d3ce72d0 5285 const insn_template *t;
b6169b20 5286 char *dot_p = NULL;
29b0f896 5287
29b0f896
AM
5288 while (1)
5289 {
5290 mnem_p = mnemonic;
5291 while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
5292 {
b6169b20
L
5293 if (*mnem_p == '.')
5294 dot_p = mnem_p;
29b0f896
AM
5295 mnem_p++;
5296 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
45288df1 5297 {
29b0f896
AM
5298 as_bad (_("no such instruction: `%s'"), token_start);
5299 return NULL;
5300 }
5301 l++;
5302 }
5303 if (!is_space_char (*l)
5304 && *l != END_OF_INSN
e44823cf
JB
5305 && (intel_syntax
5306 || (*l != PREFIX_SEPARATOR
5307 && *l != ',')))
29b0f896
AM
5308 {
5309 as_bad (_("invalid character %s in mnemonic"),
5310 output_invalid (*l));
5311 return NULL;
5312 }
5313 if (token_start == l)
5314 {
e44823cf 5315 if (!intel_syntax && *l == PREFIX_SEPARATOR)
29b0f896
AM
5316 as_bad (_("expecting prefix; got nothing"));
5317 else
5318 as_bad (_("expecting mnemonic; got nothing"));
5319 return NULL;
5320 }
45288df1 5321
29b0f896 5322 /* Look up instruction (or prefix) via hash table. */
629310ab 5323 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
47926f60 5324
29b0f896
AM
5325 if (*l != END_OF_INSN
5326 && (!is_space_char (*l) || l[1] != END_OF_INSN)
5327 && current_templates
40fb9820 5328 && current_templates->start->opcode_modifier.isprefix)
29b0f896 5329 {
c6fb90c8 5330 if (!cpu_flags_check_cpu64 (current_templates->start->cpu_flags))
2dd88dca
JB
5331 {
5332 as_bad ((flag_code != CODE_64BIT
5333 ? _("`%s' is only supported in 64-bit mode")
5334 : _("`%s' is not supported in 64-bit mode")),
5335 current_templates->start->name);
5336 return NULL;
5337 }
29b0f896
AM
5338 /* If we are in 16-bit mode, do not allow addr16 or data16.
5339 Similarly, in 32-bit mode, do not allow addr32 or data32. */
673fe0f0
JB
5340 if ((current_templates->start->opcode_modifier.size == SIZE16
5341 || current_templates->start->opcode_modifier.size == SIZE32)
29b0f896 5342 && flag_code != CODE_64BIT
673fe0f0 5343 && ((current_templates->start->opcode_modifier.size == SIZE32)
29b0f896
AM
5344 ^ (flag_code == CODE_16BIT)))
5345 {
5346 as_bad (_("redundant %s prefix"),
5347 current_templates->start->name);
5348 return NULL;
45288df1 5349 }
31184569
JB
5350
5351 if (current_templates->start->base_opcode == PSEUDO_PREFIX)
29b0f896 5352 {
86fa6981 5353 /* Handle pseudo prefixes. */
31184569 5354 switch (current_templates->start->extension_opcode)
86fa6981 5355 {
41eb8e88 5356 case Prefix_Disp8:
86fa6981
L
5357 /* {disp8} */
5358 i.disp_encoding = disp_encoding_8bit;
5359 break;
41eb8e88
L
5360 case Prefix_Disp16:
5361 /* {disp16} */
5362 i.disp_encoding = disp_encoding_16bit;
5363 break;
5364 case Prefix_Disp32:
86fa6981
L
5365 /* {disp32} */
5366 i.disp_encoding = disp_encoding_32bit;
5367 break;
41eb8e88 5368 case Prefix_Load:
86fa6981
L
5369 /* {load} */
5370 i.dir_encoding = dir_encoding_load;
5371 break;
41eb8e88 5372 case Prefix_Store:
86fa6981
L
5373 /* {store} */
5374 i.dir_encoding = dir_encoding_store;
5375 break;
41eb8e88 5376 case Prefix_VEX:
42e04b36
L
5377 /* {vex} */
5378 i.vec_encoding = vex_encoding_vex;
86fa6981 5379 break;
41eb8e88 5380 case Prefix_VEX3:
86fa6981
L
5381 /* {vex3} */
5382 i.vec_encoding = vex_encoding_vex3;
5383 break;
41eb8e88 5384 case Prefix_EVEX:
86fa6981
L
5385 /* {evex} */
5386 i.vec_encoding = vex_encoding_evex;
5387 break;
41eb8e88 5388 case Prefix_REX:
6b6b6807 5389 /* {rex} */
5b7c81bd 5390 i.rex_encoding = true;
6b6b6807 5391 break;
41eb8e88 5392 case Prefix_NoOptimize:
b6f8c7c4 5393 /* {nooptimize} */
5b7c81bd 5394 i.no_optimize = true;
b6f8c7c4 5395 break;
86fa6981
L
5396 default:
5397 abort ();
5398 }
5399 }
5400 else
5401 {
5402 /* Add prefix, checking for repeated prefixes. */
4e9ac44a 5403 switch (add_prefix (current_templates->start->base_opcode))
86fa6981 5404 {
4e9ac44a
L
5405 case PREFIX_EXIST:
5406 return NULL;
5407 case PREFIX_DS:
d777820b 5408 if (current_templates->start->cpu_flags.bitfield.cpuibt)
4e9ac44a
L
5409 i.notrack_prefix = current_templates->start->name;
5410 break;
5411 case PREFIX_REP:
5412 if (current_templates->start->cpu_flags.bitfield.cpuhle)
5413 i.hle_prefix = current_templates->start->name;
5414 else if (current_templates->start->cpu_flags.bitfield.cpumpx)
5415 i.bnd_prefix = current_templates->start->name;
5416 else
5417 i.rep_prefix = current_templates->start->name;
5418 break;
5419 default:
5420 break;
86fa6981 5421 }
29b0f896
AM
5422 }
5423 /* Skip past PREFIX_SEPARATOR and reset token_start. */
5424 token_start = ++l;
5425 }
5426 else
5427 break;
5428 }
45288df1 5429
30a55f88 5430 if (!current_templates)
b6169b20 5431 {
07d5e953
JB
5432 /* Deprecated functionality (new code should use pseudo-prefixes instead):
5433 Check if we should swap operand or force 32bit displacement in
f8a5c266 5434 encoding. */
30a55f88 5435 if (mnem_p - 2 == dot_p && dot_p[1] == 's')
64c49ab3 5436 i.dir_encoding = dir_encoding_swap;
8d63c93e 5437 else if (mnem_p - 3 == dot_p
a501d77e
L
5438 && dot_p[1] == 'd'
5439 && dot_p[2] == '8')
5440 i.disp_encoding = disp_encoding_8bit;
8d63c93e 5441 else if (mnem_p - 4 == dot_p
f8a5c266
L
5442 && dot_p[1] == 'd'
5443 && dot_p[2] == '3'
5444 && dot_p[3] == '2')
a501d77e 5445 i.disp_encoding = disp_encoding_32bit;
30a55f88
L
5446 else
5447 goto check_suffix;
5448 mnem_p = dot_p;
5449 *dot_p = '\0';
629310ab 5450 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
b6169b20
L
5451 }
5452
29b0f896
AM
5453 if (!current_templates)
5454 {
dc1e8a47 5455 check_suffix:
1c529385 5456 if (mnem_p > mnemonic)
29b0f896 5457 {
1c529385
LH
5458 /* See if we can get a match by trimming off a suffix. */
5459 switch (mnem_p[-1])
29b0f896 5460 {
1c529385
LH
5461 case WORD_MNEM_SUFFIX:
5462 if (intel_syntax && (intel_float_operand (mnemonic) & 2))
29b0f896
AM
5463 i.suffix = SHORT_MNEM_SUFFIX;
5464 else
1c529385
LH
5465 /* Fall through. */
5466 case BYTE_MNEM_SUFFIX:
5467 case QWORD_MNEM_SUFFIX:
5468 i.suffix = mnem_p[-1];
29b0f896 5469 mnem_p[-1] = '\0';
fe0e921f
AM
5470 current_templates
5471 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5472 break;
5473 case SHORT_MNEM_SUFFIX:
5474 case LONG_MNEM_SUFFIX:
5475 if (!intel_syntax)
5476 {
5477 i.suffix = mnem_p[-1];
5478 mnem_p[-1] = '\0';
fe0e921f
AM
5479 current_templates
5480 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5481 }
5482 break;
5483
5484 /* Intel Syntax. */
5485 case 'd':
5486 if (intel_syntax)
5487 {
5488 if (intel_float_operand (mnemonic) == 1)
5489 i.suffix = SHORT_MNEM_SUFFIX;
5490 else
5491 i.suffix = LONG_MNEM_SUFFIX;
5492 mnem_p[-1] = '\0';
fe0e921f
AM
5493 current_templates
5494 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5495 }
5496 break;
29b0f896 5497 }
29b0f896 5498 }
1c529385 5499
29b0f896
AM
5500 if (!current_templates)
5501 {
5502 as_bad (_("no such instruction: `%s'"), token_start);
5503 return NULL;
5504 }
5505 }
252b5132 5506
0cfa3eb3
JB
5507 if (current_templates->start->opcode_modifier.jump == JUMP
5508 || current_templates->start->opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
5509 {
5510 /* Check for a branch hint. We allow ",pt" and ",pn" for
5511 predict taken and predict not taken respectively.
5512 I'm not sure that branch hints actually do anything on loop
5513 and jcxz insns (JumpByte) for current Pentium4 chips. They
5514 may work in the future and it doesn't hurt to accept them
5515 now. */
5516 if (l[0] == ',' && l[1] == 'p')
5517 {
5518 if (l[2] == 't')
5519 {
5520 if (!add_prefix (DS_PREFIX_OPCODE))
5521 return NULL;
5522 l += 3;
5523 }
5524 else if (l[2] == 'n')
5525 {
5526 if (!add_prefix (CS_PREFIX_OPCODE))
5527 return NULL;
5528 l += 3;
5529 }
5530 }
5531 }
5532 /* Any other comma loses. */
5533 if (*l == ',')
5534 {
5535 as_bad (_("invalid character %s in mnemonic"),
5536 output_invalid (*l));
5537 return NULL;
5538 }
252b5132 5539
29b0f896 5540 /* Check if instruction is supported on specified architecture. */
5c6af06e
JB
5541 supported = 0;
5542 for (t = current_templates->start; t < current_templates->end; ++t)
5543 {
c0f3af97
L
5544 supported |= cpu_flags_match (t);
5545 if (supported == CPU_FLAGS_PERFECT_MATCH)
548d0ee6
JB
5546 {
5547 if (!cpu_arch_flags.bitfield.cpui386 && (flag_code != CODE_16BIT))
5548 as_warn (_("use .code16 to ensure correct addressing mode"));
3629bb00 5549
548d0ee6
JB
5550 return l;
5551 }
29b0f896 5552 }
3629bb00 5553
548d0ee6
JB
5554 if (!(supported & CPU_FLAGS_64BIT_MATCH))
5555 as_bad (flag_code == CODE_64BIT
5556 ? _("`%s' is not supported in 64-bit mode")
5557 : _("`%s' is only supported in 64-bit mode"),
5558 current_templates->start->name);
5559 else
5560 as_bad (_("`%s' is not supported on `%s%s'"),
5561 current_templates->start->name,
5562 cpu_arch_name ? cpu_arch_name : default_arch,
5563 cpu_sub_arch_name ? cpu_sub_arch_name : "");
252b5132 5564
548d0ee6 5565 return NULL;
29b0f896 5566}
252b5132 5567
29b0f896 5568static char *
e3bb37b5 5569parse_operands (char *l, const char *mnemonic)
29b0f896
AM
5570{
5571 char *token_start;
3138f287 5572
29b0f896
AM
5573 /* 1 if operand is pending after ','. */
5574 unsigned int expecting_operand = 0;
252b5132 5575
29b0f896
AM
5576 while (*l != END_OF_INSN)
5577 {
e68c3d59
JB
5578 /* Non-zero if operand parens not balanced. */
5579 unsigned int paren_not_balanced = 0;
5580 /* True if inside double quotes. */
5581 bool in_quotes = false;
5582
29b0f896
AM
5583 /* Skip optional white space before operand. */
5584 if (is_space_char (*l))
5585 ++l;
d02603dc 5586 if (!is_operand_char (*l) && *l != END_OF_INSN && *l != '"')
29b0f896
AM
5587 {
5588 as_bad (_("invalid character %s before operand %d"),
5589 output_invalid (*l),
5590 i.operands + 1);
5591 return NULL;
5592 }
d02603dc 5593 token_start = l; /* After white space. */
e68c3d59 5594 while (in_quotes || paren_not_balanced || *l != ',')
29b0f896
AM
5595 {
5596 if (*l == END_OF_INSN)
5597 {
e68c3d59
JB
5598 if (in_quotes)
5599 {
5600 as_bad (_("unbalanced double quotes in operand %d."),
5601 i.operands + 1);
5602 return NULL;
5603 }
29b0f896
AM
5604 if (paren_not_balanced)
5605 {
98ff9f1c
JB
5606 know (!intel_syntax);
5607 as_bad (_("unbalanced parenthesis in operand %d."),
5608 i.operands + 1);
29b0f896
AM
5609 return NULL;
5610 }
5611 else
5612 break; /* we are done */
5613 }
e68c3d59
JB
5614 else if (*l == '\\' && l[1] == '"')
5615 ++l;
5616 else if (*l == '"')
5617 in_quotes = !in_quotes;
5618 else if (!in_quotes && !is_operand_char (*l) && !is_space_char (*l))
29b0f896
AM
5619 {
5620 as_bad (_("invalid character %s in operand %d"),
5621 output_invalid (*l),
5622 i.operands + 1);
5623 return NULL;
5624 }
e68c3d59 5625 if (!intel_syntax && !in_quotes)
29b0f896
AM
5626 {
5627 if (*l == '(')
5628 ++paren_not_balanced;
5629 if (*l == ')')
5630 --paren_not_balanced;
5631 }
29b0f896
AM
5632 l++;
5633 }
5634 if (l != token_start)
5635 { /* Yes, we've read in another operand. */
5636 unsigned int operand_ok;
5637 this_operand = i.operands++;
5638 if (i.operands > MAX_OPERANDS)
5639 {
5640 as_bad (_("spurious operands; (%d operands/instruction max)"),
5641 MAX_OPERANDS);
5642 return NULL;
5643 }
9d46ce34 5644 i.types[this_operand].bitfield.unspecified = 1;
29b0f896
AM
5645 /* Now parse operand adding info to 'i' as we go along. */
5646 END_STRING_AND_SAVE (l);
5647
1286ab78
L
5648 if (i.mem_operands > 1)
5649 {
5650 as_bad (_("too many memory references for `%s'"),
5651 mnemonic);
5652 return 0;
5653 }
5654
29b0f896
AM
5655 if (intel_syntax)
5656 operand_ok =
5657 i386_intel_operand (token_start,
5658 intel_float_operand (mnemonic));
5659 else
a7619375 5660 operand_ok = i386_att_operand (token_start);
29b0f896
AM
5661
5662 RESTORE_END_STRING (l);
5663 if (!operand_ok)
5664 return NULL;
5665 }
5666 else
5667 {
5668 if (expecting_operand)
5669 {
5670 expecting_operand_after_comma:
5671 as_bad (_("expecting operand after ','; got nothing"));
5672 return NULL;
5673 }
5674 if (*l == ',')
5675 {
5676 as_bad (_("expecting operand before ','; got nothing"));
5677 return NULL;
5678 }
5679 }
7f3f1ea2 5680
29b0f896
AM
5681 /* Now *l must be either ',' or END_OF_INSN. */
5682 if (*l == ',')
5683 {
5684 if (*++l == END_OF_INSN)
5685 {
5686 /* Just skip it, if it's \n complain. */
5687 goto expecting_operand_after_comma;
5688 }
5689 expecting_operand = 1;
5690 }
5691 }
5692 return l;
5693}
7f3f1ea2 5694
050dfa73 5695static void
783c187b 5696swap_2_operands (unsigned int xchg1, unsigned int xchg2)
050dfa73
MM
5697{
5698 union i386_op temp_op;
40fb9820 5699 i386_operand_type temp_type;
c48dadc9 5700 unsigned int temp_flags;
050dfa73 5701 enum bfd_reloc_code_real temp_reloc;
4eed87de 5702
050dfa73
MM
5703 temp_type = i.types[xchg2];
5704 i.types[xchg2] = i.types[xchg1];
5705 i.types[xchg1] = temp_type;
c48dadc9
JB
5706
5707 temp_flags = i.flags[xchg2];
5708 i.flags[xchg2] = i.flags[xchg1];
5709 i.flags[xchg1] = temp_flags;
5710
050dfa73
MM
5711 temp_op = i.op[xchg2];
5712 i.op[xchg2] = i.op[xchg1];
5713 i.op[xchg1] = temp_op;
c48dadc9 5714
050dfa73
MM
5715 temp_reloc = i.reloc[xchg2];
5716 i.reloc[xchg2] = i.reloc[xchg1];
5717 i.reloc[xchg1] = temp_reloc;
43234a1e 5718
6225c532 5719 if (i.mask.reg)
43234a1e 5720 {
6225c532
JB
5721 if (i.mask.operand == xchg1)
5722 i.mask.operand = xchg2;
5723 else if (i.mask.operand == xchg2)
5724 i.mask.operand = xchg1;
43234a1e 5725 }
5273a3cd 5726 if (i.broadcast.type)
43234a1e 5727 {
5273a3cd
JB
5728 if (i.broadcast.operand == xchg1)
5729 i.broadcast.operand = xchg2;
5730 else if (i.broadcast.operand == xchg2)
5731 i.broadcast.operand = xchg1;
43234a1e 5732 }
ca5312a2 5733 if (i.rounding.type != rc_none)
43234a1e 5734 {
ca5312a2
JB
5735 if (i.rounding.operand == xchg1)
5736 i.rounding.operand = xchg2;
5737 else if (i.rounding.operand == xchg2)
5738 i.rounding.operand = xchg1;
43234a1e 5739 }
050dfa73
MM
5740}
5741
29b0f896 5742static void
e3bb37b5 5743swap_operands (void)
29b0f896 5744{
b7c61d9a 5745 switch (i.operands)
050dfa73 5746 {
c0f3af97 5747 case 5:
b7c61d9a 5748 case 4:
4d456e3d 5749 swap_2_operands (1, i.operands - 2);
1a0670f3 5750 /* Fall through. */
b7c61d9a
L
5751 case 3:
5752 case 2:
4d456e3d 5753 swap_2_operands (0, i.operands - 1);
b7c61d9a
L
5754 break;
5755 default:
5756 abort ();
29b0f896 5757 }
29b0f896
AM
5758
5759 if (i.mem_operands == 2)
5760 {
5e042380 5761 const reg_entry *temp_seg;
29b0f896
AM
5762 temp_seg = i.seg[0];
5763 i.seg[0] = i.seg[1];
5764 i.seg[1] = temp_seg;
5765 }
5766}
252b5132 5767
29b0f896
AM
5768/* Try to ensure constant immediates are represented in the smallest
5769 opcode possible. */
5770static void
e3bb37b5 5771optimize_imm (void)
29b0f896
AM
5772{
5773 char guess_suffix = 0;
5774 int op;
252b5132 5775
29b0f896
AM
5776 if (i.suffix)
5777 guess_suffix = i.suffix;
5778 else if (i.reg_operands)
5779 {
5780 /* Figure out a suffix from the last register operand specified.
75e5731b
JB
5781 We can't do this properly yet, i.e. excluding special register
5782 instances, but the following works for instructions with
5783 immediates. In any case, we can't set i.suffix yet. */
29b0f896 5784 for (op = i.operands; --op >= 0;)
bab6aec1
JB
5785 if (i.types[op].bitfield.class != Reg)
5786 continue;
5787 else if (i.types[op].bitfield.byte)
7ab9ffdd 5788 {
40fb9820
L
5789 guess_suffix = BYTE_MNEM_SUFFIX;
5790 break;
5791 }
bab6aec1 5792 else if (i.types[op].bitfield.word)
252b5132 5793 {
40fb9820
L
5794 guess_suffix = WORD_MNEM_SUFFIX;
5795 break;
5796 }
bab6aec1 5797 else if (i.types[op].bitfield.dword)
40fb9820
L
5798 {
5799 guess_suffix = LONG_MNEM_SUFFIX;
5800 break;
5801 }
bab6aec1 5802 else if (i.types[op].bitfield.qword)
40fb9820
L
5803 {
5804 guess_suffix = QWORD_MNEM_SUFFIX;
29b0f896 5805 break;
252b5132 5806 }
29b0f896
AM
5807 }
5808 else if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
5809 guess_suffix = WORD_MNEM_SUFFIX;
5810
5811 for (op = i.operands; --op >= 0;)
40fb9820 5812 if (operand_type_check (i.types[op], imm))
29b0f896
AM
5813 {
5814 switch (i.op[op].imms->X_op)
252b5132 5815 {
29b0f896
AM
5816 case O_constant:
5817 /* If a suffix is given, this operand may be shortened. */
5818 switch (guess_suffix)
252b5132 5819 {
29b0f896 5820 case LONG_MNEM_SUFFIX:
40fb9820
L
5821 i.types[op].bitfield.imm32 = 1;
5822 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
5823 break;
5824 case WORD_MNEM_SUFFIX:
40fb9820
L
5825 i.types[op].bitfield.imm16 = 1;
5826 i.types[op].bitfield.imm32 = 1;
5827 i.types[op].bitfield.imm32s = 1;
5828 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
5829 break;
5830 case BYTE_MNEM_SUFFIX:
40fb9820
L
5831 i.types[op].bitfield.imm8 = 1;
5832 i.types[op].bitfield.imm8s = 1;
5833 i.types[op].bitfield.imm16 = 1;
5834 i.types[op].bitfield.imm32 = 1;
5835 i.types[op].bitfield.imm32s = 1;
5836 i.types[op].bitfield.imm64 = 1;
29b0f896 5837 break;
252b5132 5838 }
252b5132 5839
29b0f896
AM
5840 /* If this operand is at most 16 bits, convert it
5841 to a signed 16 bit number before trying to see
5842 whether it will fit in an even smaller size.
5843 This allows a 16-bit operand such as $0xffe0 to
5844 be recognised as within Imm8S range. */
40fb9820 5845 if ((i.types[op].bitfield.imm16)
7e96fb68 5846 && fits_in_unsigned_word (i.op[op].imms->X_add_number))
252b5132 5847 {
87ed972d
JB
5848 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
5849 ^ 0x8000) - 0x8000);
29b0f896 5850 }
a28def75
L
5851#ifdef BFD64
5852 /* Store 32-bit immediate in 64-bit for 64-bit BFD. */
40fb9820 5853 if ((i.types[op].bitfield.imm32)
7e96fb68 5854 && fits_in_unsigned_long (i.op[op].imms->X_add_number))
29b0f896
AM
5855 {
5856 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
5857 ^ ((offsetT) 1 << 31))
5858 - ((offsetT) 1 << 31));
5859 }
a28def75 5860#endif
40fb9820 5861 i.types[op]
c6fb90c8
L
5862 = operand_type_or (i.types[op],
5863 smallest_imm_type (i.op[op].imms->X_add_number));
252b5132 5864
29b0f896
AM
5865 /* We must avoid matching of Imm32 templates when 64bit
5866 only immediate is available. */
5867 if (guess_suffix == QWORD_MNEM_SUFFIX)
40fb9820 5868 i.types[op].bitfield.imm32 = 0;
29b0f896 5869 break;
252b5132 5870
29b0f896
AM
5871 case O_absent:
5872 case O_register:
5873 abort ();
5874
5875 /* Symbols and expressions. */
5876 default:
9cd96992
JB
5877 /* Convert symbolic operand to proper sizes for matching, but don't
5878 prevent matching a set of insns that only supports sizes other
5879 than those matching the insn suffix. */
5880 {
40fb9820 5881 i386_operand_type mask, allowed;
87ed972d 5882 const insn_template *t = current_templates->start;
9cd96992 5883
0dfbf9d7 5884 operand_type_set (&mask, 0);
87ed972d 5885 allowed = t->operand_types[op];
40fb9820 5886
87ed972d 5887 while (++t < current_templates->end)
bab6aec1 5888 {
bab6aec1 5889 allowed = operand_type_and (allowed, anyimm);
87ed972d 5890 allowed = operand_type_or (allowed, t->operand_types[op]);
bab6aec1 5891 }
9cd96992
JB
5892 switch (guess_suffix)
5893 {
5894 case QWORD_MNEM_SUFFIX:
40fb9820
L
5895 mask.bitfield.imm64 = 1;
5896 mask.bitfield.imm32s = 1;
9cd96992
JB
5897 break;
5898 case LONG_MNEM_SUFFIX:
40fb9820 5899 mask.bitfield.imm32 = 1;
9cd96992
JB
5900 break;
5901 case WORD_MNEM_SUFFIX:
40fb9820 5902 mask.bitfield.imm16 = 1;
9cd96992
JB
5903 break;
5904 case BYTE_MNEM_SUFFIX:
40fb9820 5905 mask.bitfield.imm8 = 1;
9cd96992
JB
5906 break;
5907 default:
9cd96992
JB
5908 break;
5909 }
c6fb90c8 5910 allowed = operand_type_and (mask, allowed);
0dfbf9d7 5911 if (!operand_type_all_zero (&allowed))
c6fb90c8 5912 i.types[op] = operand_type_and (i.types[op], mask);
9cd96992 5913 }
29b0f896 5914 break;
252b5132 5915 }
29b0f896
AM
5916 }
5917}
47926f60 5918
29b0f896
AM
5919/* Try to use the smallest displacement type too. */
5920static void
e3bb37b5 5921optimize_disp (void)
29b0f896
AM
5922{
5923 int op;
3e73aa7c 5924
29b0f896 5925 for (op = i.operands; --op >= 0;)
40fb9820 5926 if (operand_type_check (i.types[op], disp))
252b5132 5927 {
b300c311 5928 if (i.op[op].disps->X_op == O_constant)
252b5132 5929 {
91d6fa6a 5930 offsetT op_disp = i.op[op].disps->X_add_number;
29b0f896 5931
91d6fa6a 5932 if (!op_disp && i.types[op].bitfield.baseindex)
b300c311 5933 {
2f2be86b
JB
5934 i.types[op] = operand_type_and_not (i.types[op], anydisp);
5935 i.op[op].disps = NULL;
b300c311 5936 i.disp_operands--;
f185acdd
JB
5937 continue;
5938 }
5939
5940 if (i.types[op].bitfield.disp16
cd613c1f 5941 && fits_in_unsigned_word (op_disp))
f185acdd
JB
5942 {
5943 /* If this operand is at most 16 bits, convert
5944 to a signed 16 bit number and don't use 64bit
5945 displacement. */
5946 op_disp = ((op_disp ^ 0x8000) - 0x8000);
5947 i.types[op].bitfield.disp64 = 0;
b300c311 5948 }
f185acdd 5949
28a167a4 5950#ifdef BFD64
a50187b2
JB
5951 /* Optimize 64-bit displacement to 32-bit for 64-bit BFD. */
5952 if ((i.types[op].bitfield.disp32
5953 || (flag_code == CODE_64BIT
5954 && want_disp32 (current_templates->start)))
5955 && fits_in_unsigned_long (op_disp))
b300c311 5956 {
a50187b2
JB
5957 /* If this operand is at most 32 bits, convert
5958 to a signed 32 bit number and don't use 64bit
5959 displacement. */
5960 op_disp = (op_disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
5961 i.types[op].bitfield.disp64 = 0;
5962 i.types[op].bitfield.disp32 = 1;
5963 }
28a167a4 5964
a50187b2
JB
5965 if (flag_code == CODE_64BIT && fits_in_signed_long (op_disp))
5966 {
5967 i.types[op].bitfield.disp64 = 0;
5968 i.types[op].bitfield.disp32s = 1;
b300c311 5969 }
28a167a4 5970#endif
40fb9820
L
5971 if ((i.types[op].bitfield.disp32
5972 || i.types[op].bitfield.disp32s
5973 || i.types[op].bitfield.disp16)
b5014f7a 5974 && fits_in_disp8 (op_disp))
40fb9820 5975 i.types[op].bitfield.disp8 = 1;
77c59789
JB
5976
5977 i.op[op].disps->X_add_number = op_disp;
252b5132 5978 }
67a4f2b7
AO
5979 else if (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
5980 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL)
5981 {
5982 fix_new_exp (frag_now, frag_more (0) - frag_now->fr_literal, 0,
5983 i.op[op].disps, 0, i.reloc[op]);
2f2be86b 5984 i.types[op] = operand_type_and_not (i.types[op], anydisp);
67a4f2b7
AO
5985 }
5986 else
b300c311 5987 /* We only support 64bit displacement on constants. */
40fb9820 5988 i.types[op].bitfield.disp64 = 0;
252b5132 5989 }
29b0f896
AM
5990}
5991
4a1b91ea
L
5992/* Return 1 if there is a match in broadcast bytes between operand
5993 GIVEN and instruction template T. */
5994
5995static INLINE int
5996match_broadcast_size (const insn_template *t, unsigned int given)
5997{
5998 return ((t->opcode_modifier.broadcast == BYTE_BROADCAST
5999 && i.types[given].bitfield.byte)
6000 || (t->opcode_modifier.broadcast == WORD_BROADCAST
6001 && i.types[given].bitfield.word)
6002 || (t->opcode_modifier.broadcast == DWORD_BROADCAST
6003 && i.types[given].bitfield.dword)
6004 || (t->opcode_modifier.broadcast == QWORD_BROADCAST
6005 && i.types[given].bitfield.qword));
6006}
6007
6c30d220
L
6008/* Check if operands are valid for the instruction. */
6009
6010static int
6011check_VecOperands (const insn_template *t)
6012{
43234a1e 6013 unsigned int op;
e2195274 6014 i386_cpu_flags cpu;
e2195274
JB
6015
6016 /* Templates allowing for ZMMword as well as YMMword and/or XMMword for
6017 any one operand are implicity requiring AVX512VL support if the actual
6018 operand size is YMMword or XMMword. Since this function runs after
6019 template matching, there's no need to check for YMMword/XMMword in
6020 the template. */
6021 cpu = cpu_flags_and (t->cpu_flags, avx512);
6022 if (!cpu_flags_all_zero (&cpu)
6023 && !t->cpu_flags.bitfield.cpuavx512vl
6024 && !cpu_arch_flags.bitfield.cpuavx512vl)
6025 {
6026 for (op = 0; op < t->operands; ++op)
6027 {
6028 if (t->operand_types[op].bitfield.zmmword
6029 && (i.types[op].bitfield.ymmword
6030 || i.types[op].bitfield.xmmword))
6031 {
6032 i.error = unsupported;
6033 return 1;
6034 }
6035 }
6036 }
43234a1e 6037
6c30d220 6038 /* Without VSIB byte, we can't have a vector register for index. */
63112cd6 6039 if (!t->opcode_modifier.sib
6c30d220 6040 && i.index_reg
1b54b8d7
JB
6041 && (i.index_reg->reg_type.bitfield.xmmword
6042 || i.index_reg->reg_type.bitfield.ymmword
6043 || i.index_reg->reg_type.bitfield.zmmword))
6c30d220
L
6044 {
6045 i.error = unsupported_vector_index_register;
6046 return 1;
6047 }
6048
ad8ecc81
MZ
6049 /* Check if default mask is allowed. */
6050 if (t->opcode_modifier.nodefmask
6225c532 6051 && (!i.mask.reg || i.mask.reg->reg_num == 0))
ad8ecc81
MZ
6052 {
6053 i.error = no_default_mask;
6054 return 1;
6055 }
6056
7bab8ab5
JB
6057 /* For VSIB byte, we need a vector register for index, and all vector
6058 registers must be distinct. */
260cd341 6059 if (t->opcode_modifier.sib && t->opcode_modifier.sib != SIBMEM)
7bab8ab5
JB
6060 {
6061 if (!i.index_reg
63112cd6 6062 || !((t->opcode_modifier.sib == VECSIB128
1b54b8d7 6063 && i.index_reg->reg_type.bitfield.xmmword)
63112cd6 6064 || (t->opcode_modifier.sib == VECSIB256
1b54b8d7 6065 && i.index_reg->reg_type.bitfield.ymmword)
63112cd6 6066 || (t->opcode_modifier.sib == VECSIB512
1b54b8d7 6067 && i.index_reg->reg_type.bitfield.zmmword)))
7bab8ab5
JB
6068 {
6069 i.error = invalid_vsib_address;
6070 return 1;
6071 }
6072
6225c532
JB
6073 gas_assert (i.reg_operands == 2 || i.mask.reg);
6074 if (i.reg_operands == 2 && !i.mask.reg)
43234a1e 6075 {
3528c362 6076 gas_assert (i.types[0].bitfield.class == RegSIMD);
1b54b8d7
JB
6077 gas_assert (i.types[0].bitfield.xmmword
6078 || i.types[0].bitfield.ymmword);
3528c362 6079 gas_assert (i.types[2].bitfield.class == RegSIMD);
1b54b8d7
JB
6080 gas_assert (i.types[2].bitfield.xmmword
6081 || i.types[2].bitfield.ymmword);
43234a1e
L
6082 if (operand_check == check_none)
6083 return 0;
6084 if (register_number (i.op[0].regs)
6085 != register_number (i.index_reg)
6086 && register_number (i.op[2].regs)
6087 != register_number (i.index_reg)
6088 && register_number (i.op[0].regs)
6089 != register_number (i.op[2].regs))
6090 return 0;
6091 if (operand_check == check_error)
6092 {
6093 i.error = invalid_vector_register_set;
6094 return 1;
6095 }
6096 as_warn (_("mask, index, and destination registers should be distinct"));
6097 }
6225c532 6098 else if (i.reg_operands == 1 && i.mask.reg)
8444f82a 6099 {
3528c362 6100 if (i.types[1].bitfield.class == RegSIMD
1b54b8d7
JB
6101 && (i.types[1].bitfield.xmmword
6102 || i.types[1].bitfield.ymmword
6103 || i.types[1].bitfield.zmmword)
8444f82a
MZ
6104 && (register_number (i.op[1].regs)
6105 == register_number (i.index_reg)))
6106 {
6107 if (operand_check == check_error)
6108 {
6109 i.error = invalid_vector_register_set;
6110 return 1;
6111 }
6112 if (operand_check != check_none)
6113 as_warn (_("index and destination registers should be distinct"));
6114 }
6115 }
43234a1e 6116 }
7bab8ab5 6117
fc141319
L
6118 /* For AMX instructions with 3 TMM register operands, all operands
6119 must be distinct. */
6120 if (i.reg_operands == 3
6121 && t->operand_types[0].bitfield.tmmword
6122 && (i.op[0].regs == i.op[1].regs
6123 || i.op[0].regs == i.op[2].regs
6124 || i.op[1].regs == i.op[2].regs))
6125 {
6126 i.error = invalid_tmm_register_set;
6127 return 1;
260cd341
LC
6128 }
6129
0cc78721
CL
6130 /* For some special instructions require that destination must be distinct
6131 from source registers. */
6132 if (t->opcode_modifier.distinctdest)
6133 {
6134 unsigned int dest_reg = i.operands - 1;
6135
6136 know (i.operands >= 3);
6137
6138 /* #UD if dest_reg == src1_reg or dest_reg == src2_reg. */
6139 if (i.op[dest_reg - 1].regs == i.op[dest_reg].regs
6140 || (i.reg_operands > 2
6141 && i.op[dest_reg - 2].regs == i.op[dest_reg].regs))
6142 {
6143 i.error = invalid_dest_and_src_register_set;
6144 return 1;
6145 }
6146 }
6147
43234a1e
L
6148 /* Check if broadcast is supported by the instruction and is applied
6149 to the memory operand. */
5273a3cd 6150 if (i.broadcast.type)
43234a1e 6151 {
8e6e0792 6152 i386_operand_type type, overlap;
43234a1e
L
6153
6154 /* Check if specified broadcast is supported in this instruction,
4a1b91ea 6155 and its broadcast bytes match the memory operand. */
5273a3cd 6156 op = i.broadcast.operand;
8e6e0792 6157 if (!t->opcode_modifier.broadcast
c48dadc9 6158 || !(i.flags[op] & Operand_Mem)
c39e5b26 6159 || (!i.types[op].bitfield.unspecified
4a1b91ea 6160 && !match_broadcast_size (t, op)))
43234a1e
L
6161 {
6162 bad_broadcast:
6163 i.error = unsupported_broadcast;
6164 return 1;
6165 }
8e6e0792 6166
5273a3cd
JB
6167 i.broadcast.bytes = ((1 << (t->opcode_modifier.broadcast - 1))
6168 * i.broadcast.type);
8e6e0792 6169 operand_type_set (&type, 0);
5273a3cd 6170 switch (i.broadcast.bytes)
8e6e0792 6171 {
4a1b91ea
L
6172 case 2:
6173 type.bitfield.word = 1;
6174 break;
6175 case 4:
6176 type.bitfield.dword = 1;
6177 break;
8e6e0792
JB
6178 case 8:
6179 type.bitfield.qword = 1;
6180 break;
6181 case 16:
6182 type.bitfield.xmmword = 1;
6183 break;
6184 case 32:
6185 type.bitfield.ymmword = 1;
6186 break;
6187 case 64:
6188 type.bitfield.zmmword = 1;
6189 break;
6190 default:
6191 goto bad_broadcast;
6192 }
6193
6194 overlap = operand_type_and (type, t->operand_types[op]);
bc49bfd8
JB
6195 if (t->operand_types[op].bitfield.class == RegSIMD
6196 && t->operand_types[op].bitfield.byte
6197 + t->operand_types[op].bitfield.word
6198 + t->operand_types[op].bitfield.dword
6199 + t->operand_types[op].bitfield.qword > 1)
6200 {
6201 overlap.bitfield.xmmword = 0;
6202 overlap.bitfield.ymmword = 0;
6203 overlap.bitfield.zmmword = 0;
6204 }
8e6e0792
JB
6205 if (operand_type_all_zero (&overlap))
6206 goto bad_broadcast;
6207
6208 if (t->opcode_modifier.checkregsize)
6209 {
6210 unsigned int j;
6211
e2195274 6212 type.bitfield.baseindex = 1;
8e6e0792
JB
6213 for (j = 0; j < i.operands; ++j)
6214 {
6215 if (j != op
6216 && !operand_type_register_match(i.types[j],
6217 t->operand_types[j],
6218 type,
6219 t->operand_types[op]))
6220 goto bad_broadcast;
6221 }
6222 }
43234a1e
L
6223 }
6224 /* If broadcast is supported in this instruction, we need to check if
6225 operand of one-element size isn't specified without broadcast. */
6226 else if (t->opcode_modifier.broadcast && i.mem_operands)
6227 {
6228 /* Find memory operand. */
6229 for (op = 0; op < i.operands; op++)
8dc0818e 6230 if (i.flags[op] & Operand_Mem)
43234a1e
L
6231 break;
6232 gas_assert (op < i.operands);
6233 /* Check size of the memory operand. */
4a1b91ea 6234 if (match_broadcast_size (t, op))
43234a1e
L
6235 {
6236 i.error = broadcast_needed;
6237 return 1;
6238 }
6239 }
c39e5b26
JB
6240 else
6241 op = MAX_OPERANDS - 1; /* Avoid uninitialized variable warning. */
43234a1e
L
6242
6243 /* Check if requested masking is supported. */
6225c532 6244 if (i.mask.reg)
43234a1e 6245 {
ae2387fe
JB
6246 switch (t->opcode_modifier.masking)
6247 {
6248 case BOTH_MASKING:
6249 break;
6250 case MERGING_MASKING:
6225c532 6251 if (i.mask.zeroing)
ae2387fe
JB
6252 {
6253 case 0:
6254 i.error = unsupported_masking;
6255 return 1;
6256 }
6257 break;
6258 case DYNAMIC_MASKING:
6259 /* Memory destinations allow only merging masking. */
6225c532 6260 if (i.mask.zeroing && i.mem_operands)
ae2387fe
JB
6261 {
6262 /* Find memory operand. */
6263 for (op = 0; op < i.operands; op++)
c48dadc9 6264 if (i.flags[op] & Operand_Mem)
ae2387fe
JB
6265 break;
6266 gas_assert (op < i.operands);
6267 if (op == i.operands - 1)
6268 {
6269 i.error = unsupported_masking;
6270 return 1;
6271 }
6272 }
6273 break;
6274 default:
6275 abort ();
6276 }
43234a1e
L
6277 }
6278
6279 /* Check if masking is applied to dest operand. */
6225c532 6280 if (i.mask.reg && (i.mask.operand != i.operands - 1))
43234a1e
L
6281 {
6282 i.error = mask_not_on_destination;
6283 return 1;
6284 }
6285
43234a1e 6286 /* Check RC/SAE. */
ca5312a2 6287 if (i.rounding.type != rc_none)
43234a1e 6288 {
a80195f1 6289 if (!t->opcode_modifier.sae
ca5312a2 6290 || (i.rounding.type != saeonly && !t->opcode_modifier.staticrounding))
43234a1e
L
6291 {
6292 i.error = unsupported_rc_sae;
6293 return 1;
6294 }
6295 /* If the instruction has several immediate operands and one of
6296 them is rounding, the rounding operand should be the last
6297 immediate operand. */
6298 if (i.imm_operands > 1
ca5312a2 6299 && i.rounding.operand != i.imm_operands - 1)
7bab8ab5 6300 {
43234a1e 6301 i.error = rc_sae_operand_not_last_imm;
7bab8ab5
JB
6302 return 1;
6303 }
6c30d220
L
6304 }
6305
da4977e0
JB
6306 /* Check the special Imm4 cases; must be the first operand. */
6307 if (t->cpu_flags.bitfield.cpuxop && t->operands == 5)
6308 {
6309 if (i.op[0].imms->X_op != O_constant
6310 || !fits_in_imm4 (i.op[0].imms->X_add_number))
6311 {
6312 i.error = bad_imm4;
6313 return 1;
6314 }
6315
6316 /* Turn off Imm<N> so that update_imm won't complain. */
6317 operand_type_set (&i.types[0], 0);
6318 }
6319
43234a1e 6320 /* Check vector Disp8 operand. */
b5014f7a
JB
6321 if (t->opcode_modifier.disp8memshift
6322 && i.disp_encoding != disp_encoding_32bit)
43234a1e 6323 {
5273a3cd 6324 if (i.broadcast.type)
4a1b91ea 6325 i.memshift = t->opcode_modifier.broadcast - 1;
7091c612 6326 else if (t->opcode_modifier.disp8memshift != DISP8_SHIFT_VL)
43234a1e 6327 i.memshift = t->opcode_modifier.disp8memshift;
7091c612
JB
6328 else
6329 {
125ff819 6330 const i386_operand_type *type = NULL, *fallback = NULL;
7091c612
JB
6331
6332 i.memshift = 0;
6333 for (op = 0; op < i.operands; op++)
8dc0818e 6334 if (i.flags[op] & Operand_Mem)
7091c612 6335 {
4174bfff
JB
6336 if (t->opcode_modifier.evex == EVEXLIG)
6337 i.memshift = 2 + (i.suffix == QWORD_MNEM_SUFFIX);
6338 else if (t->operand_types[op].bitfield.xmmword
6339 + t->operand_types[op].bitfield.ymmword
6340 + t->operand_types[op].bitfield.zmmword <= 1)
7091c612
JB
6341 type = &t->operand_types[op];
6342 else if (!i.types[op].bitfield.unspecified)
6343 type = &i.types[op];
125ff819
JB
6344 else /* Ambiguities get resolved elsewhere. */
6345 fallback = &t->operand_types[op];
7091c612 6346 }
3528c362 6347 else if (i.types[op].bitfield.class == RegSIMD
4174bfff 6348 && t->opcode_modifier.evex != EVEXLIG)
7091c612
JB
6349 {
6350 if (i.types[op].bitfield.zmmword)
6351 i.memshift = 6;
6352 else if (i.types[op].bitfield.ymmword && i.memshift < 5)
6353 i.memshift = 5;
6354 else if (i.types[op].bitfield.xmmword && i.memshift < 4)
6355 i.memshift = 4;
6356 }
6357
125ff819
JB
6358 if (!type && !i.memshift)
6359 type = fallback;
7091c612
JB
6360 if (type)
6361 {
6362 if (type->bitfield.zmmword)
6363 i.memshift = 6;
6364 else if (type->bitfield.ymmword)
6365 i.memshift = 5;
6366 else if (type->bitfield.xmmword)
6367 i.memshift = 4;
6368 }
6369
6370 /* For the check in fits_in_disp8(). */
6371 if (i.memshift == 0)
6372 i.memshift = -1;
6373 }
43234a1e
L
6374
6375 for (op = 0; op < i.operands; op++)
6376 if (operand_type_check (i.types[op], disp)
6377 && i.op[op].disps->X_op == O_constant)
6378 {
b5014f7a 6379 if (fits_in_disp8 (i.op[op].disps->X_add_number))
43234a1e 6380 {
b5014f7a
JB
6381 i.types[op].bitfield.disp8 = 1;
6382 return 0;
43234a1e 6383 }
b5014f7a 6384 i.types[op].bitfield.disp8 = 0;
43234a1e
L
6385 }
6386 }
b5014f7a
JB
6387
6388 i.memshift = 0;
43234a1e 6389
6c30d220
L
6390 return 0;
6391}
6392
da4977e0 6393/* Check if encoding requirements are met by the instruction. */
a683cc34
SP
6394
6395static int
da4977e0 6396VEX_check_encoding (const insn_template *t)
a683cc34 6397{
da4977e0
JB
6398 if (i.vec_encoding == vex_encoding_error)
6399 {
6400 i.error = unsupported;
6401 return 1;
6402 }
6403
86fa6981 6404 if (i.vec_encoding == vex_encoding_evex)
43234a1e 6405 {
86fa6981 6406 /* This instruction must be encoded with EVEX prefix. */
e771e7c9 6407 if (!is_evex_encoding (t))
86fa6981
L
6408 {
6409 i.error = unsupported;
6410 return 1;
6411 }
6412 return 0;
43234a1e
L
6413 }
6414
a683cc34 6415 if (!t->opcode_modifier.vex)
86fa6981
L
6416 {
6417 /* This instruction template doesn't have VEX prefix. */
6418 if (i.vec_encoding != vex_encoding_default)
6419 {
6420 i.error = unsupported;
6421 return 1;
6422 }
6423 return 0;
6424 }
a683cc34 6425
a683cc34
SP
6426 return 0;
6427}
6428
d3ce72d0 6429static const insn_template *
83b16ac6 6430match_template (char mnem_suffix)
29b0f896
AM
6431{
6432 /* Points to template once we've found it. */
d3ce72d0 6433 const insn_template *t;
40fb9820 6434 i386_operand_type overlap0, overlap1, overlap2, overlap3;
c0f3af97 6435 i386_operand_type overlap4;
29b0f896 6436 unsigned int found_reverse_match;
dc2be329 6437 i386_opcode_modifier suffix_check;
40fb9820 6438 i386_operand_type operand_types [MAX_OPERANDS];
539e75ad 6439 int addr_prefix_disp;
45a4bb20 6440 unsigned int j, size_match, check_register;
5614d22c 6441 enum i386_error specific_error = 0;
29b0f896 6442
c0f3af97
L
6443#if MAX_OPERANDS != 5
6444# error "MAX_OPERANDS must be 5."
f48ff2ae
L
6445#endif
6446
29b0f896 6447 found_reverse_match = 0;
539e75ad 6448 addr_prefix_disp = -1;
40fb9820 6449
dc2be329 6450 /* Prepare for mnemonic suffix check. */
40fb9820 6451 memset (&suffix_check, 0, sizeof (suffix_check));
dc2be329
L
6452 switch (mnem_suffix)
6453 {
6454 case BYTE_MNEM_SUFFIX:
6455 suffix_check.no_bsuf = 1;
6456 break;
6457 case WORD_MNEM_SUFFIX:
6458 suffix_check.no_wsuf = 1;
6459 break;
6460 case SHORT_MNEM_SUFFIX:
6461 suffix_check.no_ssuf = 1;
6462 break;
6463 case LONG_MNEM_SUFFIX:
6464 suffix_check.no_lsuf = 1;
6465 break;
6466 case QWORD_MNEM_SUFFIX:
6467 suffix_check.no_qsuf = 1;
6468 break;
6469 default:
6470 /* NB: In Intel syntax, normally we can check for memory operand
6471 size when there is no mnemonic suffix. But jmp and call have
6472 2 different encodings with Dword memory operand size, one with
6473 No_ldSuf and the other without. i.suffix is set to
6474 LONG_DOUBLE_MNEM_SUFFIX to skip the one with No_ldSuf. */
6475 if (i.suffix == LONG_DOUBLE_MNEM_SUFFIX)
6476 suffix_check.no_ldsuf = 1;
83b16ac6
JB
6477 }
6478
01559ecc
L
6479 /* Must have right number of operands. */
6480 i.error = number_of_operands_mismatch;
6481
45aa61fe 6482 for (t = current_templates->start; t < current_templates->end; t++)
29b0f896 6483 {
539e75ad 6484 addr_prefix_disp = -1;
dbbc8b7e 6485 found_reverse_match = 0;
539e75ad 6486
29b0f896
AM
6487 if (i.operands != t->operands)
6488 continue;
6489
50aecf8c 6490 /* Check processor support. */
a65babc9 6491 i.error = unsupported;
45a4bb20 6492 if (cpu_flags_match (t) != CPU_FLAGS_PERFECT_MATCH)
50aecf8c
L
6493 continue;
6494
57392598
CL
6495 /* Check Pseudo Prefix. */
6496 i.error = unsupported;
6497 if (t->opcode_modifier.pseudovexprefix
6498 && !(i.vec_encoding == vex_encoding_vex
6499 || i.vec_encoding == vex_encoding_vex3))
6500 continue;
6501
e1d4d893 6502 /* Check AT&T mnemonic. */
a65babc9 6503 i.error = unsupported_with_intel_mnemonic;
e1d4d893 6504 if (intel_mnemonic && t->opcode_modifier.attmnemonic)
1efbbeb4
L
6505 continue;
6506
4b5aaf5f 6507 /* Check AT&T/Intel syntax. */
a65babc9 6508 i.error = unsupported_syntax;
5c07affc 6509 if ((intel_syntax && t->opcode_modifier.attsyntax)
4b5aaf5f 6510 || (!intel_syntax && t->opcode_modifier.intelsyntax))
1efbbeb4
L
6511 continue;
6512
4b5aaf5f
L
6513 /* Check Intel64/AMD64 ISA. */
6514 switch (isa64)
6515 {
6516 default:
6517 /* Default: Don't accept Intel64. */
6518 if (t->opcode_modifier.isa64 == INTEL64)
6519 continue;
6520 break;
6521 case amd64:
6522 /* -mamd64: Don't accept Intel64 and Intel64 only. */
6523 if (t->opcode_modifier.isa64 >= INTEL64)
6524 continue;
6525 break;
6526 case intel64:
6527 /* -mintel64: Don't accept AMD64. */
5990e377 6528 if (t->opcode_modifier.isa64 == AMD64 && flag_code == CODE_64BIT)
4b5aaf5f
L
6529 continue;
6530 break;
6531 }
6532
dc2be329 6533 /* Check the suffix. */
a65babc9 6534 i.error = invalid_instruction_suffix;
dc2be329
L
6535 if ((t->opcode_modifier.no_bsuf && suffix_check.no_bsuf)
6536 || (t->opcode_modifier.no_wsuf && suffix_check.no_wsuf)
6537 || (t->opcode_modifier.no_lsuf && suffix_check.no_lsuf)
6538 || (t->opcode_modifier.no_ssuf && suffix_check.no_ssuf)
6539 || (t->opcode_modifier.no_qsuf && suffix_check.no_qsuf)
6540 || (t->opcode_modifier.no_ldsuf && suffix_check.no_ldsuf))
83b16ac6 6541 continue;
29b0f896 6542
3ac21baa
JB
6543 size_match = operand_size_match (t);
6544 if (!size_match)
7d5e4556 6545 continue;
539e75ad 6546
6f2f06be
JB
6547 /* This is intentionally not
6548
0cfa3eb3 6549 if (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE))
6f2f06be
JB
6550
6551 as the case of a missing * on the operand is accepted (perhaps with
6552 a warning, issued further down). */
0cfa3eb3 6553 if (i.jumpabsolute && t->opcode_modifier.jump != JUMP_ABSOLUTE)
6f2f06be
JB
6554 {
6555 i.error = operand_type_mismatch;
6556 continue;
6557 }
6558
5c07affc
L
6559 for (j = 0; j < MAX_OPERANDS; j++)
6560 operand_types[j] = t->operand_types[j];
6561
e365e234
JB
6562 /* In general, don't allow
6563 - 64-bit operands outside of 64-bit mode,
6564 - 32-bit operands on pre-386. */
4873e243 6565 j = i.imm_operands + (t->operands > i.imm_operands + 1);
e365e234
JB
6566 if (((i.suffix == QWORD_MNEM_SUFFIX
6567 && flag_code != CODE_64BIT
389d00a5
JB
6568 && !(t->opcode_modifier.opcodespace == SPACE_0F
6569 && t->base_opcode == 0xc7
5e74b495 6570 && t->opcode_modifier.opcodeprefix == PREFIX_NONE
8b65b895 6571 && t->extension_opcode == 1) /* cmpxchg8b */)
e365e234
JB
6572 || (i.suffix == LONG_MNEM_SUFFIX
6573 && !cpu_arch_flags.bitfield.cpui386))
45aa61fe 6574 && (intel_syntax
3cd7f3e3 6575 ? (t->opcode_modifier.mnemonicsize != IGNORESIZE
45aa61fe
AM
6576 && !intel_float_operand (t->name))
6577 : intel_float_operand (t->name) != 2)
4873e243
JB
6578 && (t->operands == i.imm_operands
6579 || (operand_types[i.imm_operands].bitfield.class != RegMMX
6580 && operand_types[i.imm_operands].bitfield.class != RegSIMD
6581 && operand_types[i.imm_operands].bitfield.class != RegMask)
6582 || (operand_types[j].bitfield.class != RegMMX
6583 && operand_types[j].bitfield.class != RegSIMD
6584 && operand_types[j].bitfield.class != RegMask))
63112cd6 6585 && !t->opcode_modifier.sib)
192dc9c6
JB
6586 continue;
6587
29b0f896 6588 /* Do not verify operands when there are none. */
e365e234 6589 if (!t->operands)
da4977e0
JB
6590 {
6591 if (VEX_check_encoding (t))
6592 {
6593 specific_error = i.error;
6594 continue;
6595 }
6596
6597 /* We've found a match; break out of loop. */
6598 break;
6599 }
252b5132 6600
48bcea9f
JB
6601 if (!t->opcode_modifier.jump
6602 || t->opcode_modifier.jump == JUMP_ABSOLUTE)
6603 {
6604 /* There should be only one Disp operand. */
6605 for (j = 0; j < MAX_OPERANDS; j++)
6606 if (operand_type_check (operand_types[j], disp))
539e75ad 6607 break;
48bcea9f
JB
6608 if (j < MAX_OPERANDS)
6609 {
5b7c81bd 6610 bool override = (i.prefix[ADDR_PREFIX] != 0);
48bcea9f
JB
6611
6612 addr_prefix_disp = j;
6613
6614 /* Address size prefix will turn Disp64/Disp32S/Disp32/Disp16
6615 operand into Disp32/Disp32/Disp16/Disp32 operand. */
6616 switch (flag_code)
40fb9820 6617 {
48bcea9f
JB
6618 case CODE_16BIT:
6619 override = !override;
6620 /* Fall through. */
6621 case CODE_32BIT:
6622 if (operand_types[j].bitfield.disp32
6623 && operand_types[j].bitfield.disp16)
40fb9820 6624 {
48bcea9f
JB
6625 operand_types[j].bitfield.disp16 = override;
6626 operand_types[j].bitfield.disp32 = !override;
40fb9820 6627 }
48bcea9f
JB
6628 operand_types[j].bitfield.disp32s = 0;
6629 operand_types[j].bitfield.disp64 = 0;
6630 break;
6631
6632 case CODE_64BIT:
6633 if (operand_types[j].bitfield.disp32s
6634 || operand_types[j].bitfield.disp64)
40fb9820 6635 {
48bcea9f
JB
6636 operand_types[j].bitfield.disp64 &= !override;
6637 operand_types[j].bitfield.disp32s &= !override;
6638 operand_types[j].bitfield.disp32 = override;
40fb9820 6639 }
48bcea9f
JB
6640 operand_types[j].bitfield.disp16 = 0;
6641 break;
40fb9820 6642 }
539e75ad 6643 }
48bcea9f 6644 }
539e75ad 6645
d7e3e627
L
6646 switch (i.reloc[0])
6647 {
6648 case BFD_RELOC_386_GOT32:
6649 /* Force 0x8b encoding for "mov foo@GOT, %eax". */
6650 if (t->base_opcode == 0xa0
6651 && t->opcode_modifier.opcodespace == SPACE_BASE)
6652 continue;
6653 break;
6654 case BFD_RELOC_386_TLS_GOTIE:
6655 case BFD_RELOC_386_TLS_LE_32:
6656 case BFD_RELOC_X86_64_GOTTPOFF:
6657 case BFD_RELOC_X86_64_TLSLD:
6658 /* Don't allow KMOV in TLS code sequences. */
6659 if (t->opcode_modifier.vex)
6660 continue;
6661 break;
6662 default:
6663 break;
6664 }
02a86693 6665
56ffb741 6666 /* We check register size if needed. */
e2195274
JB
6667 if (t->opcode_modifier.checkregsize)
6668 {
6669 check_register = (1 << t->operands) - 1;
5273a3cd
JB
6670 if (i.broadcast.type)
6671 check_register &= ~(1 << i.broadcast.operand);
e2195274
JB
6672 }
6673 else
6674 check_register = 0;
6675
c6fb90c8 6676 overlap0 = operand_type_and (i.types[0], operand_types[0]);
29b0f896
AM
6677 switch (t->operands)
6678 {
6679 case 1:
40fb9820 6680 if (!operand_type_match (overlap0, i.types[0]))
29b0f896
AM
6681 continue;
6682 break;
6683 case 2:
33eaf5de 6684 /* xchg %eax, %eax is a special case. It is an alias for nop
8b38ad71
L
6685 only in 32bit mode and we can use opcode 0x90. In 64bit
6686 mode, we can't use 0x90 for xchg %eax, %eax since it should
6687 zero-extend %eax to %rax. */
6688 if (flag_code == CODE_64BIT
6689 && t->base_opcode == 0x90
35648716 6690 && t->opcode_modifier.opcodespace == SPACE_BASE
75e5731b
JB
6691 && i.types[0].bitfield.instance == Accum
6692 && i.types[0].bitfield.dword
6693 && i.types[1].bitfield.instance == Accum
6694 && i.types[1].bitfield.dword)
8b38ad71 6695 continue;
1212781b
JB
6696 /* xrelease mov %eax, <disp> is another special case. It must not
6697 match the accumulator-only encoding of mov. */
6698 if (flag_code != CODE_64BIT
6699 && i.hle_prefix
6700 && t->base_opcode == 0xa0
35648716 6701 && t->opcode_modifier.opcodespace == SPACE_BASE
75e5731b 6702 && i.types[0].bitfield.instance == Accum
8dc0818e 6703 && (i.flags[1] & Operand_Mem))
1212781b 6704 continue;
f5eb1d70
JB
6705 /* Fall through. */
6706
6707 case 3:
3ac21baa
JB
6708 if (!(size_match & MATCH_STRAIGHT))
6709 goto check_reverse;
64c49ab3
JB
6710 /* Reverse direction of operands if swapping is possible in the first
6711 place (operands need to be symmetric) and
6712 - the load form is requested, and the template is a store form,
6713 - the store form is requested, and the template is a load form,
6714 - the non-default (swapped) form is requested. */
6715 overlap1 = operand_type_and (operand_types[0], operand_types[1]);
f5eb1d70 6716 if (t->opcode_modifier.d && i.reg_operands == i.operands
64c49ab3
JB
6717 && !operand_type_all_zero (&overlap1))
6718 switch (i.dir_encoding)
6719 {
6720 case dir_encoding_load:
6721 if (operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 6722 || t->opcode_modifier.regmem)
64c49ab3
JB
6723 goto check_reverse;
6724 break;
6725
6726 case dir_encoding_store:
6727 if (!operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 6728 && !t->opcode_modifier.regmem)
64c49ab3
JB
6729 goto check_reverse;
6730 break;
6731
6732 case dir_encoding_swap:
6733 goto check_reverse;
6734
6735 case dir_encoding_default:
6736 break;
6737 }
86fa6981 6738 /* If we want store form, we skip the current load. */
64c49ab3
JB
6739 if ((i.dir_encoding == dir_encoding_store
6740 || i.dir_encoding == dir_encoding_swap)
86fa6981
L
6741 && i.mem_operands == 0
6742 && t->opcode_modifier.load)
fa99fab2 6743 continue;
1a0670f3 6744 /* Fall through. */
f48ff2ae 6745 case 4:
c0f3af97 6746 case 5:
c6fb90c8 6747 overlap1 = operand_type_and (i.types[1], operand_types[1]);
40fb9820
L
6748 if (!operand_type_match (overlap0, i.types[0])
6749 || !operand_type_match (overlap1, i.types[1])
e2195274 6750 || ((check_register & 3) == 3
dc821c5f 6751 && !operand_type_register_match (i.types[0],
40fb9820 6752 operand_types[0],
dc821c5f 6753 i.types[1],
40fb9820 6754 operand_types[1])))
29b0f896
AM
6755 {
6756 /* Check if other direction is valid ... */
38e314eb 6757 if (!t->opcode_modifier.d)
29b0f896
AM
6758 continue;
6759
dc1e8a47 6760 check_reverse:
3ac21baa
JB
6761 if (!(size_match & MATCH_REVERSE))
6762 continue;
29b0f896 6763 /* Try reversing direction of operands. */
f5eb1d70
JB
6764 overlap0 = operand_type_and (i.types[0], operand_types[i.operands - 1]);
6765 overlap1 = operand_type_and (i.types[i.operands - 1], operand_types[0]);
40fb9820 6766 if (!operand_type_match (overlap0, i.types[0])
f5eb1d70 6767 || !operand_type_match (overlap1, i.types[i.operands - 1])
45664ddb 6768 || (check_register
dc821c5f 6769 && !operand_type_register_match (i.types[0],
f5eb1d70
JB
6770 operand_types[i.operands - 1],
6771 i.types[i.operands - 1],
45664ddb 6772 operand_types[0])))
29b0f896
AM
6773 {
6774 /* Does not match either direction. */
6775 continue;
6776 }
38e314eb 6777 /* found_reverse_match holds which of D or FloatR
29b0f896 6778 we've found. */
38e314eb
JB
6779 if (!t->opcode_modifier.d)
6780 found_reverse_match = 0;
6781 else if (operand_types[0].bitfield.tbyte)
8a2ed489 6782 found_reverse_match = Opcode_FloatD;
dbbc8b7e 6783 else if (operand_types[0].bitfield.xmmword
f5eb1d70 6784 || operand_types[i.operands - 1].bitfield.xmmword
3528c362
JB
6785 || operand_types[0].bitfield.class == RegMMX
6786 || operand_types[i.operands - 1].bitfield.class == RegMMX
dbbc8b7e
JB
6787 || is_any_vex_encoding(t))
6788 found_reverse_match = (t->base_opcode & 0xee) != 0x6e
6789 ? Opcode_SIMD_FloatD : Opcode_SIMD_IntD;
8a2ed489 6790 else
38e314eb 6791 found_reverse_match = Opcode_D;
40fb9820 6792 if (t->opcode_modifier.floatr)
8a2ed489 6793 found_reverse_match |= Opcode_FloatR;
29b0f896 6794 }
f48ff2ae 6795 else
29b0f896 6796 {
f48ff2ae 6797 /* Found a forward 2 operand match here. */
d1cbb4db
L
6798 switch (t->operands)
6799 {
c0f3af97
L
6800 case 5:
6801 overlap4 = operand_type_and (i.types[4],
6802 operand_types[4]);
1a0670f3 6803 /* Fall through. */
d1cbb4db 6804 case 4:
c6fb90c8
L
6805 overlap3 = operand_type_and (i.types[3],
6806 operand_types[3]);
1a0670f3 6807 /* Fall through. */
d1cbb4db 6808 case 3:
c6fb90c8
L
6809 overlap2 = operand_type_and (i.types[2],
6810 operand_types[2]);
d1cbb4db
L
6811 break;
6812 }
29b0f896 6813
f48ff2ae
L
6814 switch (t->operands)
6815 {
c0f3af97
L
6816 case 5:
6817 if (!operand_type_match (overlap4, i.types[4])
dc821c5f 6818 || !operand_type_register_match (i.types[3],
c0f3af97 6819 operand_types[3],
c0f3af97
L
6820 i.types[4],
6821 operand_types[4]))
6822 continue;
1a0670f3 6823 /* Fall through. */
f48ff2ae 6824 case 4:
40fb9820 6825 if (!operand_type_match (overlap3, i.types[3])
e2195274
JB
6826 || ((check_register & 0xa) == 0xa
6827 && !operand_type_register_match (i.types[1],
f7768225
JB
6828 operand_types[1],
6829 i.types[3],
e2195274
JB
6830 operand_types[3]))
6831 || ((check_register & 0xc) == 0xc
6832 && !operand_type_register_match (i.types[2],
6833 operand_types[2],
6834 i.types[3],
6835 operand_types[3])))
f48ff2ae 6836 continue;
1a0670f3 6837 /* Fall through. */
f48ff2ae
L
6838 case 3:
6839 /* Here we make use of the fact that there are no
23e42951 6840 reverse match 3 operand instructions. */
40fb9820 6841 if (!operand_type_match (overlap2, i.types[2])
e2195274
JB
6842 || ((check_register & 5) == 5
6843 && !operand_type_register_match (i.types[0],
23e42951
JB
6844 operand_types[0],
6845 i.types[2],
e2195274
JB
6846 operand_types[2]))
6847 || ((check_register & 6) == 6
6848 && !operand_type_register_match (i.types[1],
6849 operand_types[1],
6850 i.types[2],
6851 operand_types[2])))
f48ff2ae
L
6852 continue;
6853 break;
6854 }
29b0f896 6855 }
f48ff2ae 6856 /* Found either forward/reverse 2, 3 or 4 operand match here:
29b0f896
AM
6857 slip through to break. */
6858 }
c0f3af97 6859
da4977e0
JB
6860 /* Check if vector operands are valid. */
6861 if (check_VecOperands (t))
6862 {
6863 specific_error = i.error;
6864 continue;
6865 }
6866
6867 /* Check if VEX/EVEX encoding requirements can be satisfied. */
6868 if (VEX_check_encoding (t))
5614d22c
JB
6869 {
6870 specific_error = i.error;
6871 continue;
6872 }
a683cc34 6873
29b0f896
AM
6874 /* We've found a match; break out of loop. */
6875 break;
6876 }
6877
6878 if (t == current_templates->end)
6879 {
6880 /* We found no match. */
a65babc9 6881 const char *err_msg;
5614d22c 6882 switch (specific_error ? specific_error : i.error)
a65babc9
L
6883 {
6884 default:
6885 abort ();
86e026a4 6886 case operand_size_mismatch:
a65babc9
L
6887 err_msg = _("operand size mismatch");
6888 break;
6889 case operand_type_mismatch:
6890 err_msg = _("operand type mismatch");
6891 break;
6892 case register_type_mismatch:
6893 err_msg = _("register type mismatch");
6894 break;
6895 case number_of_operands_mismatch:
6896 err_msg = _("number of operands mismatch");
6897 break;
6898 case invalid_instruction_suffix:
6899 err_msg = _("invalid instruction suffix");
6900 break;
6901 case bad_imm4:
4a2608e3 6902 err_msg = _("constant doesn't fit in 4 bits");
a65babc9 6903 break;
a65babc9
L
6904 case unsupported_with_intel_mnemonic:
6905 err_msg = _("unsupported with Intel mnemonic");
6906 break;
6907 case unsupported_syntax:
6908 err_msg = _("unsupported syntax");
6909 break;
6910 case unsupported:
35262a23 6911 as_bad (_("unsupported instruction `%s'"),
10efe3f6
L
6912 current_templates->start->name);
6913 return NULL;
260cd341
LC
6914 case invalid_sib_address:
6915 err_msg = _("invalid SIB address");
6916 break;
6c30d220
L
6917 case invalid_vsib_address:
6918 err_msg = _("invalid VSIB address");
6919 break;
7bab8ab5
JB
6920 case invalid_vector_register_set:
6921 err_msg = _("mask, index, and destination registers must be distinct");
6922 break;
260cd341
LC
6923 case invalid_tmm_register_set:
6924 err_msg = _("all tmm registers must be distinct");
6925 break;
0cc78721
CL
6926 case invalid_dest_and_src_register_set:
6927 err_msg = _("destination and source registers must be distinct");
6928 break;
6c30d220
L
6929 case unsupported_vector_index_register:
6930 err_msg = _("unsupported vector index register");
6931 break;
43234a1e
L
6932 case unsupported_broadcast:
6933 err_msg = _("unsupported broadcast");
6934 break;
43234a1e
L
6935 case broadcast_needed:
6936 err_msg = _("broadcast is needed for operand of such type");
6937 break;
6938 case unsupported_masking:
6939 err_msg = _("unsupported masking");
6940 break;
6941 case mask_not_on_destination:
6942 err_msg = _("mask not on destination operand");
6943 break;
6944 case no_default_mask:
6945 err_msg = _("default mask isn't allowed");
6946 break;
6947 case unsupported_rc_sae:
6948 err_msg = _("unsupported static rounding/sae");
6949 break;
6950 case rc_sae_operand_not_last_imm:
6951 if (intel_syntax)
6952 err_msg = _("RC/SAE operand must precede immediate operands");
6953 else
6954 err_msg = _("RC/SAE operand must follow immediate operands");
6955 break;
6956 case invalid_register_operand:
6957 err_msg = _("invalid register operand");
6958 break;
a65babc9
L
6959 }
6960 as_bad (_("%s for `%s'"), err_msg,
891edac4 6961 current_templates->start->name);
fa99fab2 6962 return NULL;
29b0f896 6963 }
252b5132 6964
29b0f896
AM
6965 if (!quiet_warnings)
6966 {
6967 if (!intel_syntax
0cfa3eb3 6968 && (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE)))
6f2f06be 6969 as_warn (_("indirect %s without `*'"), t->name);
29b0f896 6970
40fb9820 6971 if (t->opcode_modifier.isprefix
3cd7f3e3 6972 && t->opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
6973 {
6974 /* Warn them that a data or address size prefix doesn't
6975 affect assembly of the next line of code. */
6976 as_warn (_("stand-alone `%s' prefix"), t->name);
6977 }
6978 }
6979
6980 /* Copy the template we found. */
9a182d04 6981 install_template (t);
539e75ad
L
6982
6983 if (addr_prefix_disp != -1)
6984 i.tm.operand_types[addr_prefix_disp]
6985 = operand_types[addr_prefix_disp];
6986
29b0f896
AM
6987 if (found_reverse_match)
6988 {
dfd69174
JB
6989 /* If we found a reverse match we must alter the opcode direction
6990 bit and clear/flip the regmem modifier one. found_reverse_match
6991 holds bits to change (different for int & float insns). */
29b0f896
AM
6992
6993 i.tm.base_opcode ^= found_reverse_match;
6994
f5eb1d70
JB
6995 i.tm.operand_types[0] = operand_types[i.operands - 1];
6996 i.tm.operand_types[i.operands - 1] = operand_types[0];
dfd69174
JB
6997
6998 /* Certain SIMD insns have their load forms specified in the opcode
6999 table, and hence we need to _set_ RegMem instead of clearing it.
7000 We need to avoid setting the bit though on insns like KMOVW. */
7001 i.tm.opcode_modifier.regmem
7002 = i.tm.opcode_modifier.modrm && i.tm.opcode_modifier.d
7003 && i.tm.operands > 2U - i.tm.opcode_modifier.sse2avx
7004 && !i.tm.opcode_modifier.regmem;
29b0f896
AM
7005 }
7006
fa99fab2 7007 return t;
29b0f896
AM
7008}
7009
7010static int
e3bb37b5 7011check_string (void)
29b0f896 7012{
51c8edf6
JB
7013 unsigned int es_op = i.tm.opcode_modifier.isstring - IS_STRING_ES_OP0;
7014 unsigned int op = i.tm.operand_types[0].bitfield.baseindex ? es_op : 0;
8dc0818e 7015
5e042380 7016 if (i.seg[op] != NULL && i.seg[op] != reg_es)
29b0f896 7017 {
51c8edf6
JB
7018 as_bad (_("`%s' operand %u must use `%ses' segment"),
7019 i.tm.name,
7020 intel_syntax ? i.tm.operands - es_op : es_op + 1,
7021 register_prefix);
7022 return 0;
29b0f896 7023 }
51c8edf6
JB
7024
7025 /* There's only ever one segment override allowed per instruction.
7026 This instruction possibly has a legal segment override on the
7027 second operand, so copy the segment to where non-string
7028 instructions store it, allowing common code. */
7029 i.seg[op] = i.seg[1];
7030
29b0f896
AM
7031 return 1;
7032}
7033
7034static int
543613e9 7035process_suffix (void)
29b0f896 7036{
5b7c81bd 7037 bool is_crc32 = false, is_movx = false;
8b65b895 7038
29b0f896
AM
7039 /* If matched instruction specifies an explicit instruction mnemonic
7040 suffix, use it. */
673fe0f0 7041 if (i.tm.opcode_modifier.size == SIZE16)
40fb9820 7042 i.suffix = WORD_MNEM_SUFFIX;
673fe0f0 7043 else if (i.tm.opcode_modifier.size == SIZE32)
40fb9820 7044 i.suffix = LONG_MNEM_SUFFIX;
673fe0f0 7045 else if (i.tm.opcode_modifier.size == SIZE64)
40fb9820 7046 i.suffix = QWORD_MNEM_SUFFIX;
13e600d0 7047 else if (i.reg_operands
c8f8eebc
JB
7048 && (i.operands > 1 || i.types[0].bitfield.class == Reg)
7049 && !i.tm.opcode_modifier.addrprefixopreg)
29b0f896 7050 {
65fca059 7051 unsigned int numop = i.operands;
389d00a5
JB
7052
7053 /* MOVSX/MOVZX */
7054 is_movx = (i.tm.opcode_modifier.opcodespace == SPACE_0F
7055 && (i.tm.base_opcode | 8) == 0xbe)
7056 || (i.tm.opcode_modifier.opcodespace == SPACE_BASE
7057 && i.tm.base_opcode == 0x63
7058 && i.tm.cpu_flags.bitfield.cpu64);
7059
8b65b895 7060 /* CRC32 */
389d00a5
JB
7061 is_crc32 = (i.tm.base_opcode == 0xf0
7062 && i.tm.opcode_modifier.opcodespace == SPACE_0F38
8b65b895 7063 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2);
65fca059
JB
7064
7065 /* movsx/movzx want only their source operand considered here, for the
7066 ambiguity checking below. The suffix will be replaced afterwards
7067 to represent the destination (register). */
389d00a5 7068 if (is_movx && (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63))
65fca059
JB
7069 --i.operands;
7070
643bb870 7071 /* crc32 needs REX.W set regardless of suffix / source operand size. */
8b65b895 7072 if (is_crc32 && i.tm.operand_types[1].bitfield.qword)
643bb870
JB
7073 i.rex |= REX_W;
7074
29b0f896 7075 /* If there's no instruction mnemonic suffix we try to invent one
13e600d0 7076 based on GPR operands. */
29b0f896
AM
7077 if (!i.suffix)
7078 {
7079 /* We take i.suffix from the last register operand specified,
7080 Destination register type is more significant than source
381d071f
L
7081 register type. crc32 in SSE4.2 prefers source register
7082 type. */
8b65b895 7083 unsigned int op = is_crc32 ? 1 : i.operands;
20592a94 7084
1a035124
JB
7085 while (op--)
7086 if (i.tm.operand_types[op].bitfield.instance == InstanceNone
7087 || i.tm.operand_types[op].bitfield.instance == Accum)
7088 {
7089 if (i.types[op].bitfield.class != Reg)
7090 continue;
7091 if (i.types[op].bitfield.byte)
7092 i.suffix = BYTE_MNEM_SUFFIX;
7093 else if (i.types[op].bitfield.word)
7094 i.suffix = WORD_MNEM_SUFFIX;
7095 else if (i.types[op].bitfield.dword)
7096 i.suffix = LONG_MNEM_SUFFIX;
7097 else if (i.types[op].bitfield.qword)
7098 i.suffix = QWORD_MNEM_SUFFIX;
7099 else
7100 continue;
7101 break;
7102 }
65fca059
JB
7103
7104 /* As an exception, movsx/movzx silently default to a byte source
7105 in AT&T mode. */
389d00a5 7106 if (is_movx && i.tm.opcode_modifier.w && !i.suffix && !intel_syntax)
65fca059 7107 i.suffix = BYTE_MNEM_SUFFIX;
29b0f896
AM
7108 }
7109 else if (i.suffix == BYTE_MNEM_SUFFIX)
7110 {
2eb952a4 7111 if (intel_syntax
3cd7f3e3 7112 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
2eb952a4
L
7113 && i.tm.opcode_modifier.no_bsuf)
7114 i.suffix = 0;
7115 else if (!check_byte_reg ())
29b0f896
AM
7116 return 0;
7117 }
7118 else if (i.suffix == LONG_MNEM_SUFFIX)
7119 {
2eb952a4 7120 if (intel_syntax
3cd7f3e3 7121 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
9f123b91
JB
7122 && i.tm.opcode_modifier.no_lsuf
7123 && !i.tm.opcode_modifier.todword
7124 && !i.tm.opcode_modifier.toqword)
2eb952a4
L
7125 i.suffix = 0;
7126 else if (!check_long_reg ())
29b0f896
AM
7127 return 0;
7128 }
7129 else if (i.suffix == QWORD_MNEM_SUFFIX)
7130 {
955e1e6a 7131 if (intel_syntax
3cd7f3e3 7132 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
9f123b91
JB
7133 && i.tm.opcode_modifier.no_qsuf
7134 && !i.tm.opcode_modifier.todword
7135 && !i.tm.opcode_modifier.toqword)
955e1e6a
L
7136 i.suffix = 0;
7137 else if (!check_qword_reg ())
29b0f896
AM
7138 return 0;
7139 }
7140 else if (i.suffix == WORD_MNEM_SUFFIX)
7141 {
2eb952a4 7142 if (intel_syntax
3cd7f3e3 7143 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
2eb952a4
L
7144 && i.tm.opcode_modifier.no_wsuf)
7145 i.suffix = 0;
7146 else if (!check_word_reg ())
29b0f896
AM
7147 return 0;
7148 }
3cd7f3e3
L
7149 else if (intel_syntax
7150 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
7151 /* Do nothing if the instruction is going to ignore the prefix. */
7152 ;
7153 else
7154 abort ();
65fca059
JB
7155
7156 /* Undo the movsx/movzx change done above. */
7157 i.operands = numop;
29b0f896 7158 }
3cd7f3e3
L
7159 else if (i.tm.opcode_modifier.mnemonicsize == DEFAULTSIZE
7160 && !i.suffix)
29b0f896 7161 {
13e600d0
JB
7162 i.suffix = stackop_size;
7163 if (stackop_size == LONG_MNEM_SUFFIX)
06f74c5c
L
7164 {
7165 /* stackop_size is set to LONG_MNEM_SUFFIX for the
7166 .code16gcc directive to support 16-bit mode with
7167 32-bit address. For IRET without a suffix, generate
7168 16-bit IRET (opcode 0xcf) to return from an interrupt
7169 handler. */
13e600d0
JB
7170 if (i.tm.base_opcode == 0xcf)
7171 {
7172 i.suffix = WORD_MNEM_SUFFIX;
7173 as_warn (_("generating 16-bit `iret' for .code16gcc directive"));
7174 }
7175 /* Warn about changed behavior for segment register push/pop. */
7176 else if ((i.tm.base_opcode | 1) == 0x07)
7177 as_warn (_("generating 32-bit `%s', unlike earlier gas versions"),
7178 i.tm.name);
06f74c5c 7179 }
29b0f896 7180 }
c006a730 7181 else if (!i.suffix
0cfa3eb3
JB
7182 && (i.tm.opcode_modifier.jump == JUMP_ABSOLUTE
7183 || i.tm.opcode_modifier.jump == JUMP_BYTE
7184 || i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT
389d00a5
JB
7185 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
7186 && i.tm.base_opcode == 0x01 /* [ls][gi]dt */
64e74474 7187 && i.tm.extension_opcode <= 3)))
9306ca4a
JB
7188 {
7189 switch (flag_code)
7190 {
7191 case CODE_64BIT:
40fb9820 7192 if (!i.tm.opcode_modifier.no_qsuf)
9306ca4a 7193 {
828c2a25
JB
7194 if (i.tm.opcode_modifier.jump == JUMP_BYTE
7195 || i.tm.opcode_modifier.no_lsuf)
7196 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a
JB
7197 break;
7198 }
1a0670f3 7199 /* Fall through. */
9306ca4a 7200 case CODE_32BIT:
40fb9820 7201 if (!i.tm.opcode_modifier.no_lsuf)
9306ca4a
JB
7202 i.suffix = LONG_MNEM_SUFFIX;
7203 break;
7204 case CODE_16BIT:
40fb9820 7205 if (!i.tm.opcode_modifier.no_wsuf)
9306ca4a
JB
7206 i.suffix = WORD_MNEM_SUFFIX;
7207 break;
7208 }
7209 }
252b5132 7210
c006a730 7211 if (!i.suffix
3cd7f3e3 7212 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8
JB
7213 /* Also cover lret/retf/iret in 64-bit mode. */
7214 || (flag_code == CODE_64BIT
7215 && !i.tm.opcode_modifier.no_lsuf
7216 && !i.tm.opcode_modifier.no_qsuf))
3cd7f3e3 7217 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
8bbb3ad8
JB
7218 /* Explicit sizing prefixes are assumed to disambiguate insns. */
7219 && !i.prefix[DATA_PREFIX] && !(i.prefix[REX_PREFIX] & REX_W)
62b3f548
JB
7220 /* Accept FLDENV et al without suffix. */
7221 && (i.tm.opcode_modifier.no_ssuf || i.tm.opcode_modifier.floatmf))
29b0f896 7222 {
6c0946d0 7223 unsigned int suffixes, evex = 0;
c006a730
JB
7224
7225 suffixes = !i.tm.opcode_modifier.no_bsuf;
7226 if (!i.tm.opcode_modifier.no_wsuf)
7227 suffixes |= 1 << 1;
7228 if (!i.tm.opcode_modifier.no_lsuf)
7229 suffixes |= 1 << 2;
7230 if (!i.tm.opcode_modifier.no_ldsuf)
7231 suffixes |= 1 << 3;
7232 if (!i.tm.opcode_modifier.no_ssuf)
7233 suffixes |= 1 << 4;
7234 if (flag_code == CODE_64BIT && !i.tm.opcode_modifier.no_qsuf)
7235 suffixes |= 1 << 5;
7236
6c0946d0
JB
7237 /* For [XYZ]MMWORD operands inspect operand sizes. While generally
7238 also suitable for AT&T syntax mode, it was requested that this be
7239 restricted to just Intel syntax. */
5273a3cd 7240 if (intel_syntax && is_any_vex_encoding (&i.tm) && !i.broadcast.type)
6c0946d0 7241 {
b9915cbc 7242 unsigned int op;
6c0946d0 7243
b9915cbc 7244 for (op = 0; op < i.tm.operands; ++op)
6c0946d0 7245 {
b9915cbc
JB
7246 if (is_evex_encoding (&i.tm)
7247 && !cpu_arch_flags.bitfield.cpuavx512vl)
6c0946d0 7248 {
b9915cbc
JB
7249 if (i.tm.operand_types[op].bitfield.ymmword)
7250 i.tm.operand_types[op].bitfield.xmmword = 0;
7251 if (i.tm.operand_types[op].bitfield.zmmword)
7252 i.tm.operand_types[op].bitfield.ymmword = 0;
7253 if (!i.tm.opcode_modifier.evex
7254 || i.tm.opcode_modifier.evex == EVEXDYN)
7255 i.tm.opcode_modifier.evex = EVEX512;
7256 }
6c0946d0 7257
b9915cbc
JB
7258 if (i.tm.operand_types[op].bitfield.xmmword
7259 + i.tm.operand_types[op].bitfield.ymmword
7260 + i.tm.operand_types[op].bitfield.zmmword < 2)
7261 continue;
6c0946d0 7262
b9915cbc
JB
7263 /* Any properly sized operand disambiguates the insn. */
7264 if (i.types[op].bitfield.xmmword
7265 || i.types[op].bitfield.ymmword
7266 || i.types[op].bitfield.zmmword)
7267 {
7268 suffixes &= ~(7 << 6);
7269 evex = 0;
7270 break;
7271 }
6c0946d0 7272
b9915cbc
JB
7273 if ((i.flags[op] & Operand_Mem)
7274 && i.tm.operand_types[op].bitfield.unspecified)
7275 {
7276 if (i.tm.operand_types[op].bitfield.xmmword)
7277 suffixes |= 1 << 6;
7278 if (i.tm.operand_types[op].bitfield.ymmword)
7279 suffixes |= 1 << 7;
7280 if (i.tm.operand_types[op].bitfield.zmmword)
7281 suffixes |= 1 << 8;
7282 if (is_evex_encoding (&i.tm))
7283 evex = EVEX512;
6c0946d0
JB
7284 }
7285 }
7286 }
7287
7288 /* Are multiple suffixes / operand sizes allowed? */
c006a730 7289 if (suffixes & (suffixes - 1))
9306ca4a 7290 {
873494c8 7291 if (intel_syntax
3cd7f3e3 7292 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8 7293 || operand_check == check_error))
9306ca4a 7294 {
c006a730 7295 as_bad (_("ambiguous operand size for `%s'"), i.tm.name);
9306ca4a
JB
7296 return 0;
7297 }
c006a730 7298 if (operand_check == check_error)
9306ca4a 7299 {
c006a730
JB
7300 as_bad (_("no instruction mnemonic suffix given and "
7301 "no register operands; can't size `%s'"), i.tm.name);
9306ca4a
JB
7302 return 0;
7303 }
c006a730 7304 if (operand_check == check_warning)
873494c8
JB
7305 as_warn (_("%s; using default for `%s'"),
7306 intel_syntax
7307 ? _("ambiguous operand size")
7308 : _("no instruction mnemonic suffix given and "
7309 "no register operands"),
7310 i.tm.name);
c006a730
JB
7311
7312 if (i.tm.opcode_modifier.floatmf)
7313 i.suffix = SHORT_MNEM_SUFFIX;
389d00a5 7314 else if (is_movx)
65fca059 7315 /* handled below */;
6c0946d0
JB
7316 else if (evex)
7317 i.tm.opcode_modifier.evex = evex;
c006a730
JB
7318 else if (flag_code == CODE_16BIT)
7319 i.suffix = WORD_MNEM_SUFFIX;
1a035124 7320 else if (!i.tm.opcode_modifier.no_lsuf)
c006a730 7321 i.suffix = LONG_MNEM_SUFFIX;
1a035124
JB
7322 else
7323 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a 7324 }
29b0f896 7325 }
252b5132 7326
389d00a5 7327 if (is_movx)
65fca059
JB
7328 {
7329 /* In Intel syntax, movsx/movzx must have a "suffix" (checked above).
7330 In AT&T syntax, if there is no suffix (warned about above), the default
7331 will be byte extension. */
7332 if (i.tm.opcode_modifier.w && i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
7333 i.tm.base_opcode |= 1;
7334
7335 /* For further processing, the suffix should represent the destination
7336 (register). This is already the case when one was used with
7337 mov[sz][bw]*, but we need to replace it for mov[sz]x, or if there was
7338 no suffix to begin with. */
7339 if (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63 || !i.suffix)
7340 {
7341 if (i.types[1].bitfield.word)
7342 i.suffix = WORD_MNEM_SUFFIX;
7343 else if (i.types[1].bitfield.qword)
7344 i.suffix = QWORD_MNEM_SUFFIX;
7345 else
7346 i.suffix = LONG_MNEM_SUFFIX;
7347
7348 i.tm.opcode_modifier.w = 0;
7349 }
7350 }
7351
50128d0c
JB
7352 if (!i.tm.opcode_modifier.modrm && i.reg_operands && i.tm.operands < 3)
7353 i.short_form = (i.tm.operand_types[0].bitfield.class == Reg)
7354 != (i.tm.operand_types[1].bitfield.class == Reg);
7355
d2224064
JB
7356 /* Change the opcode based on the operand size given by i.suffix. */
7357 switch (i.suffix)
29b0f896 7358 {
d2224064
JB
7359 /* Size floating point instruction. */
7360 case LONG_MNEM_SUFFIX:
7361 if (i.tm.opcode_modifier.floatmf)
7362 {
7363 i.tm.base_opcode ^= 4;
7364 break;
7365 }
7366 /* fall through */
7367 case WORD_MNEM_SUFFIX:
7368 case QWORD_MNEM_SUFFIX:
29b0f896 7369 /* It's not a byte, select word/dword operation. */
40fb9820 7370 if (i.tm.opcode_modifier.w)
29b0f896 7371 {
50128d0c 7372 if (i.short_form)
29b0f896
AM
7373 i.tm.base_opcode |= 8;
7374 else
7375 i.tm.base_opcode |= 1;
7376 }
d2224064
JB
7377 /* fall through */
7378 case SHORT_MNEM_SUFFIX:
29b0f896
AM
7379 /* Now select between word & dword operations via the operand
7380 size prefix, except for instructions that will ignore this
7381 prefix anyway. */
c8f8eebc 7382 if (i.suffix != QWORD_MNEM_SUFFIX
3cd7f3e3 7383 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
c8f8eebc
JB
7384 && !i.tm.opcode_modifier.floatmf
7385 && !is_any_vex_encoding (&i.tm)
7386 && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
7387 || (flag_code == CODE_64BIT
7388 && i.tm.opcode_modifier.jump == JUMP_BYTE)))
24eab124
AM
7389 {
7390 unsigned int prefix = DATA_PREFIX_OPCODE;
543613e9 7391
0cfa3eb3 7392 if (i.tm.opcode_modifier.jump == JUMP_BYTE) /* jcxz, loop */
29b0f896 7393 prefix = ADDR_PREFIX_OPCODE;
252b5132 7394
29b0f896
AM
7395 if (!add_prefix (prefix))
7396 return 0;
24eab124 7397 }
252b5132 7398
29b0f896
AM
7399 /* Set mode64 for an operand. */
7400 if (i.suffix == QWORD_MNEM_SUFFIX
9146926a 7401 && flag_code == CODE_64BIT
d2224064 7402 && !i.tm.opcode_modifier.norex64
4ed21b58 7403 && !i.tm.opcode_modifier.vexw
46e883c5 7404 /* Special case for xchg %rax,%rax. It is NOP and doesn't
d2224064
JB
7405 need rex64. */
7406 && ! (i.operands == 2
7407 && i.tm.base_opcode == 0x90
7408 && i.tm.extension_opcode == None
75e5731b
JB
7409 && i.types[0].bitfield.instance == Accum
7410 && i.types[0].bitfield.qword
7411 && i.types[1].bitfield.instance == Accum
7412 && i.types[1].bitfield.qword))
d2224064 7413 i.rex |= REX_W;
3e73aa7c 7414
d2224064 7415 break;
8bbb3ad8
JB
7416
7417 case 0:
f9a6a8f0 7418 /* Select word/dword/qword operation with explicit data sizing prefix
8bbb3ad8
JB
7419 when there are no suitable register operands. */
7420 if (i.tm.opcode_modifier.w
7421 && (i.prefix[DATA_PREFIX] || (i.prefix[REX_PREFIX] & REX_W))
7422 && (!i.reg_operands
7423 || (i.reg_operands == 1
7424 /* ShiftCount */
7425 && (i.tm.operand_types[0].bitfield.instance == RegC
7426 /* InOutPortReg */
7427 || i.tm.operand_types[0].bitfield.instance == RegD
7428 || i.tm.operand_types[1].bitfield.instance == RegD
7429 /* CRC32 */
8b65b895 7430 || is_crc32))))
8bbb3ad8
JB
7431 i.tm.base_opcode |= 1;
7432 break;
29b0f896 7433 }
7ecd2f8b 7434
c8f8eebc 7435 if (i.tm.opcode_modifier.addrprefixopreg)
c0a30a9f 7436 {
c8f8eebc
JB
7437 gas_assert (!i.suffix);
7438 gas_assert (i.reg_operands);
c0a30a9f 7439
c8f8eebc
JB
7440 if (i.tm.operand_types[0].bitfield.instance == Accum
7441 || i.operands == 1)
7442 {
7443 /* The address size override prefix changes the size of the
7444 first operand. */
7445 if (flag_code == CODE_64BIT
7446 && i.op[0].regs->reg_type.bitfield.word)
7447 {
7448 as_bad (_("16-bit addressing unavailable for `%s'"),
7449 i.tm.name);
7450 return 0;
7451 }
7452
7453 if ((flag_code == CODE_32BIT
7454 ? i.op[0].regs->reg_type.bitfield.word
7455 : i.op[0].regs->reg_type.bitfield.dword)
7456 && !add_prefix (ADDR_PREFIX_OPCODE))
7457 return 0;
7458 }
c0a30a9f
L
7459 else
7460 {
c8f8eebc
JB
7461 /* Check invalid register operand when the address size override
7462 prefix changes the size of register operands. */
7463 unsigned int op;
7464 enum { need_word, need_dword, need_qword } need;
7465
27f13469 7466 /* Check the register operand for the address size prefix if
b3a3496f 7467 the memory operand has no real registers, like symbol, DISP
829f3fe1 7468 or bogus (x32-only) symbol(%rip) when symbol(%eip) is meant. */
27f13469
L
7469 if (i.mem_operands == 1
7470 && i.reg_operands == 1
7471 && i.operands == 2
27f13469 7472 && i.types[1].bitfield.class == Reg
b3a3496f
L
7473 && (flag_code == CODE_32BIT
7474 ? i.op[1].regs->reg_type.bitfield.word
7475 : i.op[1].regs->reg_type.bitfield.dword)
7476 && ((i.base_reg == NULL && i.index_reg == NULL)
829f3fe1
JB
7477#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
7478 || (x86_elf_abi == X86_64_X32_ABI
7479 && i.base_reg
b3a3496f
L
7480 && i.base_reg->reg_num == RegIP
7481 && i.base_reg->reg_type.bitfield.qword))
829f3fe1
JB
7482#else
7483 || 0)
7484#endif
27f13469
L
7485 && !add_prefix (ADDR_PREFIX_OPCODE))
7486 return 0;
7487
c8f8eebc
JB
7488 if (flag_code == CODE_32BIT)
7489 need = i.prefix[ADDR_PREFIX] ? need_word : need_dword;
7490 else if (i.prefix[ADDR_PREFIX])
c0a30a9f
L
7491 need = need_dword;
7492 else
7493 need = flag_code == CODE_64BIT ? need_qword : need_word;
c0a30a9f 7494
c8f8eebc
JB
7495 for (op = 0; op < i.operands; op++)
7496 {
7497 if (i.types[op].bitfield.class != Reg)
7498 continue;
7499
7500 switch (need)
7501 {
7502 case need_word:
7503 if (i.op[op].regs->reg_type.bitfield.word)
7504 continue;
7505 break;
7506 case need_dword:
7507 if (i.op[op].regs->reg_type.bitfield.dword)
7508 continue;
7509 break;
7510 case need_qword:
7511 if (i.op[op].regs->reg_type.bitfield.qword)
7512 continue;
7513 break;
7514 }
7515
7516 as_bad (_("invalid register operand size for `%s'"),
7517 i.tm.name);
7518 return 0;
7519 }
7520 }
c0a30a9f
L
7521 }
7522
29b0f896
AM
7523 return 1;
7524}
3e73aa7c 7525
29b0f896 7526static int
543613e9 7527check_byte_reg (void)
29b0f896
AM
7528{
7529 int op;
543613e9 7530
29b0f896
AM
7531 for (op = i.operands; --op >= 0;)
7532 {
dc821c5f 7533 /* Skip non-register operands. */
bab6aec1 7534 if (i.types[op].bitfield.class != Reg)
dc821c5f
JB
7535 continue;
7536
29b0f896
AM
7537 /* If this is an eight bit register, it's OK. If it's the 16 or
7538 32 bit version of an eight bit register, we will just use the
7539 low portion, and that's OK too. */
dc821c5f 7540 if (i.types[op].bitfield.byte)
29b0f896
AM
7541 continue;
7542
5a819eb9 7543 /* I/O port address operands are OK too. */
75e5731b
JB
7544 if (i.tm.operand_types[op].bitfield.instance == RegD
7545 && i.tm.operand_types[op].bitfield.word)
5a819eb9
JB
7546 continue;
7547
9706160a 7548 /* crc32 only wants its source operand checked here. */
389d00a5
JB
7549 if (i.tm.base_opcode == 0xf0
7550 && i.tm.opcode_modifier.opcodespace == SPACE_0F38
8b65b895
L
7551 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2
7552 && op != 0)
9344ff29
L
7553 continue;
7554
29b0f896 7555 /* Any other register is bad. */
73c76375
JB
7556 as_bad (_("`%s%s' not allowed with `%s%c'"),
7557 register_prefix, i.op[op].regs->reg_name,
7558 i.tm.name, i.suffix);
7559 return 0;
29b0f896
AM
7560 }
7561 return 1;
7562}
7563
7564static int
e3bb37b5 7565check_long_reg (void)
29b0f896
AM
7566{
7567 int op;
7568
7569 for (op = i.operands; --op >= 0;)
dc821c5f 7570 /* Skip non-register operands. */
bab6aec1 7571 if (i.types[op].bitfield.class != Reg)
dc821c5f 7572 continue;
29b0f896
AM
7573 /* Reject eight bit registers, except where the template requires
7574 them. (eg. movzb) */
dc821c5f 7575 else if (i.types[op].bitfield.byte
bab6aec1 7576 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7577 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7578 && (i.tm.operand_types[op].bitfield.word
7579 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7580 {
a540244d
L
7581 as_bad (_("`%s%s' not allowed with `%s%c'"),
7582 register_prefix,
29b0f896
AM
7583 i.op[op].regs->reg_name,
7584 i.tm.name,
7585 i.suffix);
7586 return 0;
7587 }
be4c5e58
L
7588 /* Error if the e prefix on a general reg is missing. */
7589 else if (i.types[op].bitfield.word
bab6aec1 7590 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7591 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7592 && i.tm.operand_types[op].bitfield.dword)
29b0f896 7593 {
be4c5e58
L
7594 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7595 register_prefix, i.op[op].regs->reg_name,
7596 i.suffix);
7597 return 0;
252b5132 7598 }
e4630f71 7599 /* Warn if the r prefix on a general reg is present. */
dc821c5f 7600 else if (i.types[op].bitfield.qword
bab6aec1 7601 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7602 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7603 && i.tm.operand_types[op].bitfield.dword)
252b5132 7604 {
34828aad 7605 if (intel_syntax
65fca059 7606 && i.tm.opcode_modifier.toqword
3528c362 7607 && i.types[0].bitfield.class != RegSIMD)
34828aad 7608 {
ca61edf2 7609 /* Convert to QWORD. We want REX byte. */
34828aad
L
7610 i.suffix = QWORD_MNEM_SUFFIX;
7611 }
7612 else
7613 {
2b5d6a91 7614 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
34828aad
L
7615 register_prefix, i.op[op].regs->reg_name,
7616 i.suffix);
7617 return 0;
7618 }
29b0f896
AM
7619 }
7620 return 1;
7621}
252b5132 7622
29b0f896 7623static int
e3bb37b5 7624check_qword_reg (void)
29b0f896
AM
7625{
7626 int op;
252b5132 7627
29b0f896 7628 for (op = i.operands; --op >= 0; )
dc821c5f 7629 /* Skip non-register operands. */
bab6aec1 7630 if (i.types[op].bitfield.class != Reg)
dc821c5f 7631 continue;
29b0f896
AM
7632 /* Reject eight bit registers, except where the template requires
7633 them. (eg. movzb) */
dc821c5f 7634 else if (i.types[op].bitfield.byte
bab6aec1 7635 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7636 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7637 && (i.tm.operand_types[op].bitfield.word
7638 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7639 {
a540244d
L
7640 as_bad (_("`%s%s' not allowed with `%s%c'"),
7641 register_prefix,
29b0f896
AM
7642 i.op[op].regs->reg_name,
7643 i.tm.name,
7644 i.suffix);
7645 return 0;
7646 }
e4630f71 7647 /* Warn if the r prefix on a general reg is missing. */
dc821c5f
JB
7648 else if ((i.types[op].bitfield.word
7649 || i.types[op].bitfield.dword)
bab6aec1 7650 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7651 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7652 && i.tm.operand_types[op].bitfield.qword)
29b0f896
AM
7653 {
7654 /* Prohibit these changes in the 64bit mode, since the
7655 lowering is more complicated. */
34828aad 7656 if (intel_syntax
ca61edf2 7657 && i.tm.opcode_modifier.todword
3528c362 7658 && i.types[0].bitfield.class != RegSIMD)
34828aad 7659 {
ca61edf2 7660 /* Convert to DWORD. We don't want REX byte. */
34828aad
L
7661 i.suffix = LONG_MNEM_SUFFIX;
7662 }
7663 else
7664 {
2b5d6a91 7665 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
34828aad
L
7666 register_prefix, i.op[op].regs->reg_name,
7667 i.suffix);
7668 return 0;
7669 }
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 7708 }
0cc78721
CL
7709 /* For some instructions need encode as EVEX.W=1 without explicit VexW1. */
7710 else if (i.types[op].bitfield.qword
7711 && intel_syntax
7712 && i.tm.opcode_modifier.toqword)
7713 {
7714 /* Convert to QWORD. We want EVEX.W byte. */
7715 i.suffix = QWORD_MNEM_SUFFIX;
7716 }
29b0f896
AM
7717 return 1;
7718}
252b5132 7719
29b0f896 7720static int
40fb9820 7721update_imm (unsigned int j)
29b0f896 7722{
bc0844ae 7723 i386_operand_type overlap = i.types[j];
40fb9820
L
7724 if ((overlap.bitfield.imm8
7725 || overlap.bitfield.imm8s
7726 || overlap.bitfield.imm16
7727 || overlap.bitfield.imm32
7728 || overlap.bitfield.imm32s
7729 || overlap.bitfield.imm64)
0dfbf9d7
L
7730 && !operand_type_equal (&overlap, &imm8)
7731 && !operand_type_equal (&overlap, &imm8s)
7732 && !operand_type_equal (&overlap, &imm16)
7733 && !operand_type_equal (&overlap, &imm32)
7734 && !operand_type_equal (&overlap, &imm32s)
7735 && !operand_type_equal (&overlap, &imm64))
29b0f896
AM
7736 {
7737 if (i.suffix)
7738 {
40fb9820
L
7739 i386_operand_type temp;
7740
0dfbf9d7 7741 operand_type_set (&temp, 0);
7ab9ffdd 7742 if (i.suffix == BYTE_MNEM_SUFFIX)
40fb9820
L
7743 {
7744 temp.bitfield.imm8 = overlap.bitfield.imm8;
7745 temp.bitfield.imm8s = overlap.bitfield.imm8s;
7746 }
7747 else if (i.suffix == WORD_MNEM_SUFFIX)
7748 temp.bitfield.imm16 = overlap.bitfield.imm16;
7749 else if (i.suffix == QWORD_MNEM_SUFFIX)
7750 {
7751 temp.bitfield.imm64 = overlap.bitfield.imm64;
7752 temp.bitfield.imm32s = overlap.bitfield.imm32s;
7753 }
7754 else
7755 temp.bitfield.imm32 = overlap.bitfield.imm32;
7756 overlap = temp;
29b0f896 7757 }
0dfbf9d7
L
7758 else if (operand_type_equal (&overlap, &imm16_32_32s)
7759 || operand_type_equal (&overlap, &imm16_32)
7760 || operand_type_equal (&overlap, &imm16_32s))
29b0f896 7761 {
40fb9820 7762 if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
65da13b5 7763 overlap = imm16;
40fb9820 7764 else
65da13b5 7765 overlap = imm32s;
29b0f896 7766 }
8bbb3ad8
JB
7767 else if (i.prefix[REX_PREFIX] & REX_W)
7768 overlap = operand_type_and (overlap, imm32s);
7769 else if (i.prefix[DATA_PREFIX])
7770 overlap = operand_type_and (overlap,
7771 flag_code != CODE_16BIT ? imm16 : imm32);
0dfbf9d7
L
7772 if (!operand_type_equal (&overlap, &imm8)
7773 && !operand_type_equal (&overlap, &imm8s)
7774 && !operand_type_equal (&overlap, &imm16)
7775 && !operand_type_equal (&overlap, &imm32)
7776 && !operand_type_equal (&overlap, &imm32s)
7777 && !operand_type_equal (&overlap, &imm64))
29b0f896 7778 {
4eed87de
AM
7779 as_bad (_("no instruction mnemonic suffix given; "
7780 "can't determine immediate size"));
29b0f896
AM
7781 return 0;
7782 }
7783 }
40fb9820 7784 i.types[j] = overlap;
29b0f896 7785
40fb9820
L
7786 return 1;
7787}
7788
7789static int
7790finalize_imm (void)
7791{
bc0844ae 7792 unsigned int j, n;
29b0f896 7793
bc0844ae
L
7794 /* Update the first 2 immediate operands. */
7795 n = i.operands > 2 ? 2 : i.operands;
7796 if (n)
7797 {
7798 for (j = 0; j < n; j++)
7799 if (update_imm (j) == 0)
7800 return 0;
40fb9820 7801
bc0844ae
L
7802 /* The 3rd operand can't be immediate operand. */
7803 gas_assert (operand_type_check (i.types[2], imm) == 0);
7804 }
29b0f896
AM
7805
7806 return 1;
7807}
7808
7809static int
e3bb37b5 7810process_operands (void)
29b0f896
AM
7811{
7812 /* Default segment register this instruction will use for memory
7813 accesses. 0 means unknown. This is only for optimizing out
7814 unnecessary segment overrides. */
5e042380 7815 const reg_entry *default_seg = NULL;
29b0f896 7816
a5aeccd9
JB
7817 if (i.tm.opcode_modifier.sse2avx)
7818 {
7819 /* Legacy encoded insns allow explicit REX prefixes, so these prefixes
7820 need converting. */
7821 i.rex |= i.prefix[REX_PREFIX] & (REX_W | REX_R | REX_X | REX_B);
7822 i.prefix[REX_PREFIX] = 0;
7823 i.rex_encoding = 0;
7824 }
c423d21a
JB
7825 /* ImmExt should be processed after SSE2AVX. */
7826 else if (i.tm.opcode_modifier.immext)
7827 process_immext ();
a5aeccd9 7828
2426c15f 7829 if (i.tm.opcode_modifier.sse2avx && i.tm.opcode_modifier.vexvvvv)
29b0f896 7830 {
91d6fa6a
NC
7831 unsigned int dupl = i.operands;
7832 unsigned int dest = dupl - 1;
9fcfb3d7
L
7833 unsigned int j;
7834
c0f3af97 7835 /* The destination must be an xmm register. */
9c2799c2 7836 gas_assert (i.reg_operands
91d6fa6a 7837 && MAX_OPERANDS > dupl
7ab9ffdd 7838 && operand_type_equal (&i.types[dest], &regxmm));
c0f3af97 7839
75e5731b 7840 if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 7841 && i.tm.operand_types[0].bitfield.xmmword)
e2ec9d29 7842 {
8cd7925b 7843 if (i.tm.opcode_modifier.vexsources == VEX3SOURCES)
c0f3af97
L
7844 {
7845 /* Keep xmm0 for instructions with VEX prefix and 3
7846 sources. */
75e5731b 7847 i.tm.operand_types[0].bitfield.instance = InstanceNone;
3528c362 7848 i.tm.operand_types[0].bitfield.class = RegSIMD;
c0f3af97
L
7849 goto duplicate;
7850 }
e2ec9d29 7851 else
c0f3af97
L
7852 {
7853 /* We remove the first xmm0 and keep the number of
7854 operands unchanged, which in fact duplicates the
7855 destination. */
7856 for (j = 1; j < i.operands; j++)
7857 {
7858 i.op[j - 1] = i.op[j];
7859 i.types[j - 1] = i.types[j];
7860 i.tm.operand_types[j - 1] = i.tm.operand_types[j];
8dc0818e 7861 i.flags[j - 1] = i.flags[j];
c0f3af97
L
7862 }
7863 }
7864 }
7865 else if (i.tm.opcode_modifier.implicit1stxmm0)
7ab9ffdd 7866 {
91d6fa6a 7867 gas_assert ((MAX_OPERANDS - 1) > dupl
8cd7925b
L
7868 && (i.tm.opcode_modifier.vexsources
7869 == VEX3SOURCES));
c0f3af97
L
7870
7871 /* Add the implicit xmm0 for instructions with VEX prefix
7872 and 3 sources. */
7873 for (j = i.operands; j > 0; j--)
7874 {
7875 i.op[j] = i.op[j - 1];
7876 i.types[j] = i.types[j - 1];
7877 i.tm.operand_types[j] = i.tm.operand_types[j - 1];
8dc0818e 7878 i.flags[j] = i.flags[j - 1];
c0f3af97
L
7879 }
7880 i.op[0].regs
629310ab 7881 = (const reg_entry *) str_hash_find (reg_hash, "xmm0");
7ab9ffdd 7882 i.types[0] = regxmm;
c0f3af97
L
7883 i.tm.operand_types[0] = regxmm;
7884
7885 i.operands += 2;
7886 i.reg_operands += 2;
7887 i.tm.operands += 2;
7888
91d6fa6a 7889 dupl++;
c0f3af97 7890 dest++;
91d6fa6a
NC
7891 i.op[dupl] = i.op[dest];
7892 i.types[dupl] = i.types[dest];
7893 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 7894 i.flags[dupl] = i.flags[dest];
e2ec9d29 7895 }
c0f3af97
L
7896 else
7897 {
dc1e8a47 7898 duplicate:
c0f3af97
L
7899 i.operands++;
7900 i.reg_operands++;
7901 i.tm.operands++;
7902
91d6fa6a
NC
7903 i.op[dupl] = i.op[dest];
7904 i.types[dupl] = i.types[dest];
7905 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 7906 i.flags[dupl] = i.flags[dest];
c0f3af97
L
7907 }
7908
7909 if (i.tm.opcode_modifier.immext)
7910 process_immext ();
7911 }
75e5731b 7912 else if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 7913 && i.tm.operand_types[0].bitfield.xmmword)
c0f3af97
L
7914 {
7915 unsigned int j;
7916
9fcfb3d7
L
7917 for (j = 1; j < i.operands; j++)
7918 {
7919 i.op[j - 1] = i.op[j];
7920 i.types[j - 1] = i.types[j];
7921
7922 /* We need to adjust fields in i.tm since they are used by
7923 build_modrm_byte. */
7924 i.tm.operand_types [j - 1] = i.tm.operand_types [j];
8dc0818e
JB
7925
7926 i.flags[j - 1] = i.flags[j];
9fcfb3d7
L
7927 }
7928
e2ec9d29
L
7929 i.operands--;
7930 i.reg_operands--;
e2ec9d29
L
7931 i.tm.operands--;
7932 }
920d2ddc
IT
7933 else if (i.tm.opcode_modifier.implicitquadgroup)
7934 {
a477a8c4
JB
7935 unsigned int regnum, first_reg_in_group, last_reg_in_group;
7936
920d2ddc 7937 /* The second operand must be {x,y,z}mmN, where N is a multiple of 4. */
3528c362 7938 gas_assert (i.operands >= 2 && i.types[1].bitfield.class == RegSIMD);
a477a8c4
JB
7939 regnum = register_number (i.op[1].regs);
7940 first_reg_in_group = regnum & ~3;
7941 last_reg_in_group = first_reg_in_group + 3;
7942 if (regnum != first_reg_in_group)
7943 as_warn (_("source register `%s%s' implicitly denotes"
7944 " `%s%.3s%u' to `%s%.3s%u' source group in `%s'"),
7945 register_prefix, i.op[1].regs->reg_name,
7946 register_prefix, i.op[1].regs->reg_name, first_reg_in_group,
7947 register_prefix, i.op[1].regs->reg_name, last_reg_in_group,
7948 i.tm.name);
7949 }
e2ec9d29
L
7950 else if (i.tm.opcode_modifier.regkludge)
7951 {
7952 /* The imul $imm, %reg instruction is converted into
7953 imul $imm, %reg, %reg, and the clr %reg instruction
7954 is converted into xor %reg, %reg. */
7955
7956 unsigned int first_reg_op;
7957
7958 if (operand_type_check (i.types[0], reg))
7959 first_reg_op = 0;
7960 else
7961 first_reg_op = 1;
7962 /* Pretend we saw the extra register operand. */
9c2799c2 7963 gas_assert (i.reg_operands == 1
7ab9ffdd 7964 && i.op[first_reg_op + 1].regs == 0);
e2ec9d29
L
7965 i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
7966 i.types[first_reg_op + 1] = i.types[first_reg_op];
7967 i.operands++;
7968 i.reg_operands++;
29b0f896
AM
7969 }
7970
85b80b0f 7971 if (i.tm.opcode_modifier.modrm)
29b0f896
AM
7972 {
7973 /* The opcode is completed (modulo i.tm.extension_opcode which
52271982
AM
7974 must be put into the modrm byte). Now, we make the modrm and
7975 index base bytes based on all the info we've collected. */
29b0f896
AM
7976
7977 default_seg = build_modrm_byte ();
7978 }
00cee14f 7979 else if (i.types[0].bitfield.class == SReg)
85b80b0f
JB
7980 {
7981 if (flag_code != CODE_64BIT
7982 ? i.tm.base_opcode == POP_SEG_SHORT
7983 && i.op[0].regs->reg_num == 1
389d00a5 7984 : (i.tm.base_opcode | 1) == (POP_SEG386_SHORT & 0xff)
85b80b0f
JB
7985 && i.op[0].regs->reg_num < 4)
7986 {
7987 as_bad (_("you can't `%s %s%s'"),
7988 i.tm.name, register_prefix, i.op[0].regs->reg_name);
7989 return 0;
7990 }
389d00a5
JB
7991 if (i.op[0].regs->reg_num > 3
7992 && i.tm.opcode_modifier.opcodespace == SPACE_BASE )
85b80b0f 7993 {
389d00a5
JB
7994 i.tm.base_opcode ^= (POP_SEG_SHORT ^ POP_SEG386_SHORT) & 0xff;
7995 i.tm.opcode_modifier.opcodespace = SPACE_0F;
85b80b0f
JB
7996 }
7997 i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
7998 }
389d00a5
JB
7999 else if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
8000 && (i.tm.base_opcode & ~3) == MOV_AX_DISP32)
29b0f896 8001 {
5e042380 8002 default_seg = reg_ds;
29b0f896 8003 }
40fb9820 8004 else if (i.tm.opcode_modifier.isstring)
29b0f896
AM
8005 {
8006 /* For the string instructions that allow a segment override
8007 on one of their operands, the default segment is ds. */
5e042380 8008 default_seg = reg_ds;
29b0f896 8009 }
50128d0c 8010 else if (i.short_form)
85b80b0f
JB
8011 {
8012 /* The register or float register operand is in operand
8013 0 or 1. */
bab6aec1 8014 unsigned int op = i.tm.operand_types[0].bitfield.class != Reg;
85b80b0f
JB
8015
8016 /* Register goes in low 3 bits of opcode. */
8017 i.tm.base_opcode |= i.op[op].regs->reg_num;
8018 if ((i.op[op].regs->reg_flags & RegRex) != 0)
8019 i.rex |= REX_B;
8020 if (!quiet_warnings && i.tm.opcode_modifier.ugh)
8021 {
8022 /* Warn about some common errors, but press on regardless.
8023 The first case can be generated by gcc (<= 2.8.1). */
8024 if (i.operands == 2)
8025 {
8026 /* Reversed arguments on faddp, fsubp, etc. */
8027 as_warn (_("translating to `%s %s%s,%s%s'"), i.tm.name,
8028 register_prefix, i.op[!intel_syntax].regs->reg_name,
8029 register_prefix, i.op[intel_syntax].regs->reg_name);
8030 }
8031 else
8032 {
8033 /* Extraneous `l' suffix on fp insn. */
8034 as_warn (_("translating to `%s %s%s'"), i.tm.name,
8035 register_prefix, i.op[0].regs->reg_name);
8036 }
8037 }
8038 }
29b0f896 8039
514a8bb0 8040 if ((i.seg[0] || i.prefix[SEG_PREFIX])
514a8bb0 8041 && i.tm.base_opcode == 0x8d /* lea */
35648716 8042 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
514a8bb0 8043 && !is_any_vex_encoding(&i.tm))
92334ad2
JB
8044 {
8045 if (!quiet_warnings)
8046 as_warn (_("segment override on `%s' is ineffectual"), i.tm.name);
8047 if (optimize)
8048 {
8049 i.seg[0] = NULL;
8050 i.prefix[SEG_PREFIX] = 0;
8051 }
8052 }
52271982
AM
8053
8054 /* If a segment was explicitly specified, and the specified segment
b6773884
JB
8055 is neither the default nor the one already recorded from a prefix,
8056 use an opcode prefix to select it. If we never figured out what
8057 the default segment is, then default_seg will be zero at this
8058 point, and the specified segment prefix will always be used. */
8059 if (i.seg[0]
8060 && i.seg[0] != default_seg
5e042380 8061 && i386_seg_prefixes[i.seg[0]->reg_num] != i.prefix[SEG_PREFIX])
29b0f896 8062 {
5e042380 8063 if (!add_prefix (i386_seg_prefixes[i.seg[0]->reg_num]))
29b0f896
AM
8064 return 0;
8065 }
8066 return 1;
8067}
8068
a5aeccd9 8069static INLINE void set_rex_vrex (const reg_entry *r, unsigned int rex_bit,
5b7c81bd 8070 bool do_sse2avx)
a5aeccd9
JB
8071{
8072 if (r->reg_flags & RegRex)
8073 {
8074 if (i.rex & rex_bit)
8075 as_bad (_("same type of prefix used twice"));
8076 i.rex |= rex_bit;
8077 }
8078 else if (do_sse2avx && (i.rex & rex_bit) && i.vex.register_specifier)
8079 {
8080 gas_assert (i.vex.register_specifier == r);
8081 i.vex.register_specifier += 8;
8082 }
8083
8084 if (r->reg_flags & RegVRex)
8085 i.vrex |= rex_bit;
8086}
8087
5e042380 8088static const reg_entry *
e3bb37b5 8089build_modrm_byte (void)
29b0f896 8090{
5e042380 8091 const reg_entry *default_seg = NULL;
c0f3af97 8092 unsigned int source, dest;
8cd7925b 8093 int vex_3_sources;
c0f3af97 8094
8cd7925b 8095 vex_3_sources = i.tm.opcode_modifier.vexsources == VEX3SOURCES;
c0f3af97
L
8096 if (vex_3_sources)
8097 {
91d6fa6a 8098 unsigned int nds, reg_slot;
4c2c6516 8099 expressionS *exp;
c0f3af97 8100
6b8d3588 8101 dest = i.operands - 1;
c0f3af97 8102 nds = dest - 1;
922d8de8 8103
a683cc34 8104 /* There are 2 kinds of instructions:
bed3d976 8105 1. 5 operands: 4 register operands or 3 register operands
9d3bf266 8106 plus 1 memory operand plus one Imm4 operand, VexXDS, and
bed3d976 8107 VexW0 or VexW1. The destination must be either XMM, YMM or
43234a1e 8108 ZMM register.
bed3d976 8109 2. 4 operands: 4 register operands or 3 register operands
2f1bada2 8110 plus 1 memory operand, with VexXDS. */
922d8de8 8111 gas_assert ((i.reg_operands == 4
bed3d976
JB
8112 || (i.reg_operands == 3 && i.mem_operands == 1))
8113 && i.tm.opcode_modifier.vexvvvv == VEXXDS
dcd7e323 8114 && i.tm.opcode_modifier.vexw
3528c362 8115 && i.tm.operand_types[dest].bitfield.class == RegSIMD);
a683cc34 8116
48db9223
JB
8117 /* If VexW1 is set, the first non-immediate operand is the source and
8118 the second non-immediate one is encoded in the immediate operand. */
8119 if (i.tm.opcode_modifier.vexw == VEXW1)
8120 {
8121 source = i.imm_operands;
8122 reg_slot = i.imm_operands + 1;
8123 }
8124 else
8125 {
8126 source = i.imm_operands + 1;
8127 reg_slot = i.imm_operands;
8128 }
8129
a683cc34 8130 if (i.imm_operands == 0)
bed3d976
JB
8131 {
8132 /* When there is no immediate operand, generate an 8bit
8133 immediate operand to encode the first operand. */
8134 exp = &im_expressions[i.imm_operands++];
8135 i.op[i.operands].imms = exp;
8136 i.types[i.operands] = imm8;
8137 i.operands++;
8138
3528c362 8139 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
bed3d976
JB
8140 exp->X_op = O_constant;
8141 exp->X_add_number = register_number (i.op[reg_slot].regs) << 4;
43234a1e
L
8142 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
8143 }
922d8de8 8144 else
bed3d976 8145 {
9d3bf266
JB
8146 gas_assert (i.imm_operands == 1);
8147 gas_assert (fits_in_imm4 (i.op[0].imms->X_add_number));
8148 gas_assert (!i.tm.opcode_modifier.immext);
a683cc34 8149
9d3bf266
JB
8150 /* Turn on Imm8 again so that output_imm will generate it. */
8151 i.types[0].bitfield.imm8 = 1;
bed3d976 8152
3528c362 8153 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
9d3bf266 8154 i.op[0].imms->X_add_number
bed3d976 8155 |= register_number (i.op[reg_slot].regs) << 4;
43234a1e 8156 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
bed3d976 8157 }
a683cc34 8158
3528c362 8159 gas_assert (i.tm.operand_types[nds].bitfield.class == RegSIMD);
dae39acc 8160 i.vex.register_specifier = i.op[nds].regs;
c0f3af97
L
8161 }
8162 else
8163 source = dest = 0;
29b0f896
AM
8164
8165 /* i.reg_operands MUST be the number of real register operands;
c0f3af97
L
8166 implicit registers do not count. If there are 3 register
8167 operands, it must be a instruction with VexNDS. For a
8168 instruction with VexNDD, the destination register is encoded
8169 in VEX prefix. If there are 4 register operands, it must be
8170 a instruction with VEX prefix and 3 sources. */
7ab9ffdd
L
8171 if (i.mem_operands == 0
8172 && ((i.reg_operands == 2
2426c15f 8173 && i.tm.opcode_modifier.vexvvvv <= VEXXDS)
7ab9ffdd 8174 || (i.reg_operands == 3
2426c15f 8175 && i.tm.opcode_modifier.vexvvvv == VEXXDS)
7ab9ffdd 8176 || (i.reg_operands == 4 && vex_3_sources)))
29b0f896 8177 {
cab737b9
L
8178 switch (i.operands)
8179 {
8180 case 2:
8181 source = 0;
8182 break;
8183 case 3:
c81128dc
L
8184 /* When there are 3 operands, one of them may be immediate,
8185 which may be the first or the last operand. Otherwise,
c0f3af97
L
8186 the first operand must be shift count register (cl) or it
8187 is an instruction with VexNDS. */
9c2799c2 8188 gas_assert (i.imm_operands == 1
7ab9ffdd 8189 || (i.imm_operands == 0
2426c15f 8190 && (i.tm.opcode_modifier.vexvvvv == VEXXDS
75e5731b
JB
8191 || (i.types[0].bitfield.instance == RegC
8192 && i.types[0].bitfield.byte))));
40fb9820 8193 if (operand_type_check (i.types[0], imm)
75e5731b
JB
8194 || (i.types[0].bitfield.instance == RegC
8195 && i.types[0].bitfield.byte))
40fb9820
L
8196 source = 1;
8197 else
8198 source = 0;
cab737b9
L
8199 break;
8200 case 4:
368d64cc
L
8201 /* When there are 4 operands, the first two must be 8bit
8202 immediate operands. The source operand will be the 3rd
c0f3af97
L
8203 one.
8204
8205 For instructions with VexNDS, if the first operand
8206 an imm8, the source operand is the 2nd one. If the last
8207 operand is imm8, the source operand is the first one. */
9c2799c2 8208 gas_assert ((i.imm_operands == 2
7ab9ffdd
L
8209 && i.types[0].bitfield.imm8
8210 && i.types[1].bitfield.imm8)
2426c15f 8211 || (i.tm.opcode_modifier.vexvvvv == VEXXDS
7ab9ffdd
L
8212 && i.imm_operands == 1
8213 && (i.types[0].bitfield.imm8
43234a1e 8214 || i.types[i.operands - 1].bitfield.imm8
ca5312a2 8215 || i.rounding.type != rc_none)));
9f2670f2
L
8216 if (i.imm_operands == 2)
8217 source = 2;
8218 else
c0f3af97
L
8219 {
8220 if (i.types[0].bitfield.imm8)
8221 source = 1;
8222 else
8223 source = 0;
8224 }
c0f3af97
L
8225 break;
8226 case 5:
e771e7c9 8227 if (is_evex_encoding (&i.tm))
43234a1e
L
8228 {
8229 /* For EVEX instructions, when there are 5 operands, the
8230 first one must be immediate operand. If the second one
8231 is immediate operand, the source operand is the 3th
8232 one. If the last one is immediate operand, the source
8233 operand is the 2nd one. */
8234 gas_assert (i.imm_operands == 2
8235 && i.tm.opcode_modifier.sae
8236 && operand_type_check (i.types[0], imm));
8237 if (operand_type_check (i.types[1], imm))
8238 source = 2;
8239 else if (operand_type_check (i.types[4], imm))
8240 source = 1;
8241 else
8242 abort ();
8243 }
cab737b9
L
8244 break;
8245 default:
8246 abort ();
8247 }
8248
c0f3af97
L
8249 if (!vex_3_sources)
8250 {
8251 dest = source + 1;
8252
43234a1e
L
8253 /* RC/SAE operand could be between DEST and SRC. That happens
8254 when one operand is GPR and the other one is XMM/YMM/ZMM
8255 register. */
ca5312a2 8256 if (i.rounding.type != rc_none && i.rounding.operand == dest)
43234a1e
L
8257 dest++;
8258
2426c15f 8259 if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
c0f3af97 8260 {
43234a1e 8261 /* For instructions with VexNDS, the register-only source
c5d0745b 8262 operand must be a 32/64bit integer, XMM, YMM, ZMM, or mask
dfd69174 8263 register. It is encoded in VEX prefix. */
f12dc422
L
8264
8265 i386_operand_type op;
8266 unsigned int vvvv;
8267
c2ecccb3
L
8268 /* Swap two source operands if needed. */
8269 if (i.tm.opcode_modifier.swapsources)
f12dc422
L
8270 {
8271 vvvv = source;
8272 source = dest;
8273 }
8274 else
8275 vvvv = dest;
8276
8277 op = i.tm.operand_types[vvvv];
c0f3af97 8278 if ((dest + 1) >= i.operands
bab6aec1 8279 || ((op.bitfield.class != Reg
dc821c5f 8280 || (!op.bitfield.dword && !op.bitfield.qword))
3528c362 8281 && op.bitfield.class != RegSIMD
43234a1e 8282 && !operand_type_equal (&op, &regmask)))
c0f3af97 8283 abort ();
f12dc422 8284 i.vex.register_specifier = i.op[vvvv].regs;
c0f3af97
L
8285 dest++;
8286 }
8287 }
29b0f896
AM
8288
8289 i.rm.mode = 3;
dfd69174
JB
8290 /* One of the register operands will be encoded in the i.rm.reg
8291 field, the other in the combined i.rm.mode and i.rm.regmem
29b0f896
AM
8292 fields. If no form of this instruction supports a memory
8293 destination operand, then we assume the source operand may
8294 sometimes be a memory operand and so we need to store the
8295 destination in the i.rm.reg field. */
dfd69174 8296 if (!i.tm.opcode_modifier.regmem
40fb9820 8297 && operand_type_check (i.tm.operand_types[dest], anymem) == 0)
29b0f896
AM
8298 {
8299 i.rm.reg = i.op[dest].regs->reg_num;
8300 i.rm.regmem = i.op[source].regs->reg_num;
a5aeccd9 8301 set_rex_vrex (i.op[dest].regs, REX_R, i.tm.opcode_modifier.sse2avx);
5b7c81bd 8302 set_rex_vrex (i.op[source].regs, REX_B, false);
29b0f896
AM
8303 }
8304 else
8305 {
8306 i.rm.reg = i.op[source].regs->reg_num;
8307 i.rm.regmem = i.op[dest].regs->reg_num;
a5aeccd9 8308 set_rex_vrex (i.op[dest].regs, REX_B, i.tm.opcode_modifier.sse2avx);
5b7c81bd 8309 set_rex_vrex (i.op[source].regs, REX_R, false);
29b0f896 8310 }
e0c7f900 8311 if (flag_code != CODE_64BIT && (i.rex & REX_R))
c4a530c5 8312 {
4a5c67ed 8313 if (i.types[!i.tm.opcode_modifier.regmem].bitfield.class != RegCR)
c4a530c5 8314 abort ();
e0c7f900 8315 i.rex &= ~REX_R;
c4a530c5
JB
8316 add_prefix (LOCK_PREFIX_OPCODE);
8317 }
29b0f896
AM
8318 }
8319 else
8320 { /* If it's not 2 reg operands... */
c0f3af97
L
8321 unsigned int mem;
8322
29b0f896
AM
8323 if (i.mem_operands)
8324 {
8325 unsigned int fake_zero_displacement = 0;
99018f42 8326 unsigned int op;
4eed87de 8327
7ab9ffdd 8328 for (op = 0; op < i.operands; op++)
8dc0818e 8329 if (i.flags[op] & Operand_Mem)
7ab9ffdd 8330 break;
7ab9ffdd 8331 gas_assert (op < i.operands);
29b0f896 8332
63112cd6 8333 if (i.tm.opcode_modifier.sib)
6c30d220 8334 {
260cd341
LC
8335 /* The index register of VSIB shouldn't be RegIZ. */
8336 if (i.tm.opcode_modifier.sib != SIBMEM
8337 && i.index_reg->reg_num == RegIZ)
6c30d220
L
8338 abort ();
8339
8340 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8341 if (!i.base_reg)
8342 {
8343 i.sib.base = NO_BASE_REGISTER;
8344 i.sib.scale = i.log2_scale_factor;
2f2be86b 8345 i.types[op] = operand_type_and_not (i.types[op], anydisp);
a9aabc23 8346 if (want_disp32 (&i.tm))
2f2be86b 8347 i.types[op].bitfield.disp32 = 1;
6c30d220 8348 else
2f2be86b 8349 i.types[op].bitfield.disp32s = 1;
6c30d220 8350 }
260cd341
LC
8351
8352 /* Since the mandatory SIB always has index register, so
8353 the code logic remains unchanged. The non-mandatory SIB
8354 without index register is allowed and will be handled
8355 later. */
8356 if (i.index_reg)
8357 {
8358 if (i.index_reg->reg_num == RegIZ)
8359 i.sib.index = NO_INDEX_REGISTER;
8360 else
8361 i.sib.index = i.index_reg->reg_num;
5b7c81bd 8362 set_rex_vrex (i.index_reg, REX_X, false);
260cd341 8363 }
6c30d220
L
8364 }
8365
5e042380 8366 default_seg = reg_ds;
29b0f896
AM
8367
8368 if (i.base_reg == 0)
8369 {
8370 i.rm.mode = 0;
8371 if (!i.disp_operands)
9bb129e8 8372 fake_zero_displacement = 1;
29b0f896
AM
8373 if (i.index_reg == 0)
8374 {
260cd341
LC
8375 /* Both check for VSIB and mandatory non-vector SIB. */
8376 gas_assert (!i.tm.opcode_modifier.sib
8377 || i.tm.opcode_modifier.sib == SIBMEM);
29b0f896 8378 /* Operand is just <disp> */
2f2be86b 8379 i.types[op] = operand_type_and_not (i.types[op], anydisp);
20f0a1fc 8380 if (flag_code == CODE_64BIT)
29b0f896
AM
8381 {
8382 /* 64bit mode overwrites the 32bit absolute
8383 addressing by RIP relative addressing and
8384 absolute addressing is encoded by one of the
8385 redundant SIB forms. */
8386 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8387 i.sib.base = NO_BASE_REGISTER;
8388 i.sib.index = NO_INDEX_REGISTER;
2f2be86b
JB
8389 if (want_disp32 (&i.tm))
8390 i.types[op].bitfield.disp32 = 1;
8391 else
8392 i.types[op].bitfield.disp32s = 1;
20f0a1fc 8393 }
fc225355
L
8394 else if ((flag_code == CODE_16BIT)
8395 ^ (i.prefix[ADDR_PREFIX] != 0))
20f0a1fc
NC
8396 {
8397 i.rm.regmem = NO_BASE_REGISTER_16;
2f2be86b 8398 i.types[op].bitfield.disp16 = 1;
20f0a1fc
NC
8399 }
8400 else
8401 {
8402 i.rm.regmem = NO_BASE_REGISTER;
2f2be86b 8403 i.types[op].bitfield.disp32 = 1;
29b0f896
AM
8404 }
8405 }
63112cd6 8406 else if (!i.tm.opcode_modifier.sib)
29b0f896 8407 {
6c30d220 8408 /* !i.base_reg && i.index_reg */
e968fc9b 8409 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8410 i.sib.index = NO_INDEX_REGISTER;
8411 else
8412 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8413 i.sib.base = NO_BASE_REGISTER;
8414 i.sib.scale = i.log2_scale_factor;
8415 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
2f2be86b 8416 i.types[op] = operand_type_and_not (i.types[op], anydisp);
a9aabc23 8417 if (want_disp32 (&i.tm))
2f2be86b 8418 i.types[op].bitfield.disp32 = 1;
29b0f896 8419 else
2f2be86b 8420 i.types[op].bitfield.disp32s = 1;
29b0f896 8421 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8422 i.rex |= REX_X;
29b0f896
AM
8423 }
8424 }
8425 /* RIP addressing for 64bit mode. */
e968fc9b 8426 else if (i.base_reg->reg_num == RegIP)
29b0f896 8427 {
63112cd6 8428 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896 8429 i.rm.regmem = NO_BASE_REGISTER;
40fb9820
L
8430 i.types[op].bitfield.disp8 = 0;
8431 i.types[op].bitfield.disp16 = 0;
8432 i.types[op].bitfield.disp32 = 0;
8433 i.types[op].bitfield.disp32s = 1;
8434 i.types[op].bitfield.disp64 = 0;
71903a11 8435 i.flags[op] |= Operand_PCrel;
20f0a1fc
NC
8436 if (! i.disp_operands)
8437 fake_zero_displacement = 1;
29b0f896 8438 }
dc821c5f 8439 else if (i.base_reg->reg_type.bitfield.word)
29b0f896 8440 {
63112cd6 8441 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896
AM
8442 switch (i.base_reg->reg_num)
8443 {
8444 case 3: /* (%bx) */
8445 if (i.index_reg == 0)
8446 i.rm.regmem = 7;
8447 else /* (%bx,%si) -> 0, or (%bx,%di) -> 1 */
8448 i.rm.regmem = i.index_reg->reg_num - 6;
8449 break;
8450 case 5: /* (%bp) */
5e042380 8451 default_seg = reg_ss;
29b0f896
AM
8452 if (i.index_reg == 0)
8453 {
8454 i.rm.regmem = 6;
40fb9820 8455 if (operand_type_check (i.types[op], disp) == 0)
29b0f896
AM
8456 {
8457 /* fake (%bp) into 0(%bp) */
41eb8e88 8458 if (i.disp_encoding == disp_encoding_16bit)
1a02d6b0
L
8459 i.types[op].bitfield.disp16 = 1;
8460 else
8461 i.types[op].bitfield.disp8 = 1;
252b5132 8462 fake_zero_displacement = 1;
29b0f896
AM
8463 }
8464 }
8465 else /* (%bp,%si) -> 2, or (%bp,%di) -> 3 */
8466 i.rm.regmem = i.index_reg->reg_num - 6 + 2;
8467 break;
8468 default: /* (%si) -> 4 or (%di) -> 5 */
8469 i.rm.regmem = i.base_reg->reg_num - 6 + 4;
8470 }
41eb8e88
L
8471 if (!fake_zero_displacement
8472 && !i.disp_operands
8473 && i.disp_encoding)
8474 {
8475 fake_zero_displacement = 1;
8476 if (i.disp_encoding == disp_encoding_8bit)
8477 i.types[op].bitfield.disp8 = 1;
8478 else
8479 i.types[op].bitfield.disp16 = 1;
8480 }
29b0f896
AM
8481 i.rm.mode = mode_from_disp_size (i.types[op]);
8482 }
8483 else /* i.base_reg and 32/64 bit mode */
8484 {
a9aabc23 8485 if (operand_type_check (i.types[op], disp))
40fb9820 8486 {
73053c1f
JB
8487 i.types[op].bitfield.disp16 = 0;
8488 i.types[op].bitfield.disp64 = 0;
a9aabc23 8489 if (!want_disp32 (&i.tm))
73053c1f
JB
8490 {
8491 i.types[op].bitfield.disp32 = 0;
8492 i.types[op].bitfield.disp32s = 1;
8493 }
40fb9820 8494 else
73053c1f
JB
8495 {
8496 i.types[op].bitfield.disp32 = 1;
8497 i.types[op].bitfield.disp32s = 0;
8498 }
40fb9820 8499 }
20f0a1fc 8500
63112cd6 8501 if (!i.tm.opcode_modifier.sib)
6c30d220 8502 i.rm.regmem = i.base_reg->reg_num;
29b0f896 8503 if ((i.base_reg->reg_flags & RegRex) != 0)
161a04f6 8504 i.rex |= REX_B;
29b0f896
AM
8505 i.sib.base = i.base_reg->reg_num;
8506 /* x86-64 ignores REX prefix bit here to avoid decoder
8507 complications. */
848930b2
JB
8508 if (!(i.base_reg->reg_flags & RegRex)
8509 && (i.base_reg->reg_num == EBP_REG_NUM
8510 || i.base_reg->reg_num == ESP_REG_NUM))
5e042380 8511 default_seg = reg_ss;
848930b2 8512 if (i.base_reg->reg_num == 5 && i.disp_operands == 0)
29b0f896 8513 {
848930b2 8514 fake_zero_displacement = 1;
1a02d6b0
L
8515 if (i.disp_encoding == disp_encoding_32bit)
8516 i.types[op].bitfield.disp32 = 1;
8517 else
8518 i.types[op].bitfield.disp8 = 1;
29b0f896
AM
8519 }
8520 i.sib.scale = i.log2_scale_factor;
8521 if (i.index_reg == 0)
8522 {
260cd341
LC
8523 /* Only check for VSIB. */
8524 gas_assert (i.tm.opcode_modifier.sib != VECSIB128
8525 && i.tm.opcode_modifier.sib != VECSIB256
8526 && i.tm.opcode_modifier.sib != VECSIB512);
8527
29b0f896
AM
8528 /* <disp>(%esp) becomes two byte modrm with no index
8529 register. We've already stored the code for esp
8530 in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
8531 Any base register besides %esp will not use the
8532 extra modrm byte. */
8533 i.sib.index = NO_INDEX_REGISTER;
29b0f896 8534 }
63112cd6 8535 else if (!i.tm.opcode_modifier.sib)
29b0f896 8536 {
e968fc9b 8537 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8538 i.sib.index = NO_INDEX_REGISTER;
8539 else
8540 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8541 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8542 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8543 i.rex |= REX_X;
29b0f896 8544 }
67a4f2b7
AO
8545
8546 if (i.disp_operands
8547 && (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
8548 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL))
8549 i.rm.mode = 0;
8550 else
a501d77e
L
8551 {
8552 if (!fake_zero_displacement
8553 && !i.disp_operands
8554 && i.disp_encoding)
8555 {
8556 fake_zero_displacement = 1;
8557 if (i.disp_encoding == disp_encoding_8bit)
8558 i.types[op].bitfield.disp8 = 1;
8559 else
8560 i.types[op].bitfield.disp32 = 1;
8561 }
8562 i.rm.mode = mode_from_disp_size (i.types[op]);
8563 }
29b0f896 8564 }
252b5132 8565
29b0f896
AM
8566 if (fake_zero_displacement)
8567 {
8568 /* Fakes a zero displacement assuming that i.types[op]
8569 holds the correct displacement size. */
8570 expressionS *exp;
8571
9c2799c2 8572 gas_assert (i.op[op].disps == 0);
29b0f896
AM
8573 exp = &disp_expressions[i.disp_operands++];
8574 i.op[op].disps = exp;
8575 exp->X_op = O_constant;
8576 exp->X_add_number = 0;
8577 exp->X_add_symbol = (symbolS *) 0;
8578 exp->X_op_symbol = (symbolS *) 0;
8579 }
c0f3af97
L
8580
8581 mem = op;
29b0f896 8582 }
c0f3af97
L
8583 else
8584 mem = ~0;
252b5132 8585
8c43a48b 8586 if (i.tm.opcode_modifier.vexsources == XOP2SOURCES)
5dd85c99
SP
8587 {
8588 if (operand_type_check (i.types[0], imm))
8589 i.vex.register_specifier = NULL;
8590 else
8591 {
8592 /* VEX.vvvv encodes one of the sources when the first
8593 operand is not an immediate. */
1ef99a7b 8594 if (i.tm.opcode_modifier.vexw == VEXW0)
5dd85c99
SP
8595 i.vex.register_specifier = i.op[0].regs;
8596 else
8597 i.vex.register_specifier = i.op[1].regs;
8598 }
8599
8600 /* Destination is a XMM register encoded in the ModRM.reg
8601 and VEX.R bit. */
8602 i.rm.reg = i.op[2].regs->reg_num;
8603 if ((i.op[2].regs->reg_flags & RegRex) != 0)
8604 i.rex |= REX_R;
8605
8606 /* ModRM.rm and VEX.B encodes the other source. */
8607 if (!i.mem_operands)
8608 {
8609 i.rm.mode = 3;
8610
1ef99a7b 8611 if (i.tm.opcode_modifier.vexw == VEXW0)
5dd85c99
SP
8612 i.rm.regmem = i.op[1].regs->reg_num;
8613 else
8614 i.rm.regmem = i.op[0].regs->reg_num;
8615
8616 if ((i.op[1].regs->reg_flags & RegRex) != 0)
8617 i.rex |= REX_B;
8618 }
8619 }
2426c15f 8620 else if (i.tm.opcode_modifier.vexvvvv == VEXLWP)
f88c9eb0
SP
8621 {
8622 i.vex.register_specifier = i.op[2].regs;
8623 if (!i.mem_operands)
8624 {
8625 i.rm.mode = 3;
8626 i.rm.regmem = i.op[1].regs->reg_num;
8627 if ((i.op[1].regs->reg_flags & RegRex) != 0)
8628 i.rex |= REX_B;
8629 }
8630 }
29b0f896
AM
8631 /* Fill in i.rm.reg or i.rm.regmem field with register operand
8632 (if any) based on i.tm.extension_opcode. Again, we must be
8633 careful to make sure that segment/control/debug/test/MMX
8634 registers are coded into the i.rm.reg field. */
f88c9eb0 8635 else if (i.reg_operands)
29b0f896 8636 {
99018f42 8637 unsigned int op;
7ab9ffdd
L
8638 unsigned int vex_reg = ~0;
8639
8640 for (op = 0; op < i.operands; op++)
921eafea
L
8641 if (i.types[op].bitfield.class == Reg
8642 || i.types[op].bitfield.class == RegBND
8643 || i.types[op].bitfield.class == RegMask
8644 || i.types[op].bitfield.class == SReg
8645 || i.types[op].bitfield.class == RegCR
8646 || i.types[op].bitfield.class == RegDR
8647 || i.types[op].bitfield.class == RegTR
8648 || i.types[op].bitfield.class == RegSIMD
8649 || i.types[op].bitfield.class == RegMMX)
8650 break;
c0209578 8651
7ab9ffdd
L
8652 if (vex_3_sources)
8653 op = dest;
2426c15f 8654 else if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
7ab9ffdd
L
8655 {
8656 /* For instructions with VexNDS, the register-only
8657 source operand is encoded in VEX prefix. */
8658 gas_assert (mem != (unsigned int) ~0);
c0f3af97 8659
7ab9ffdd 8660 if (op > mem)
c0f3af97 8661 {
7ab9ffdd
L
8662 vex_reg = op++;
8663 gas_assert (op < i.operands);
c0f3af97
L
8664 }
8665 else
c0f3af97 8666 {
f12dc422
L
8667 /* Check register-only source operand when two source
8668 operands are swapped. */
8669 if (!i.tm.operand_types[op].bitfield.baseindex
8670 && i.tm.operand_types[op + 1].bitfield.baseindex)
8671 {
8672 vex_reg = op;
8673 op += 2;
8674 gas_assert (mem == (vex_reg + 1)
8675 && op < i.operands);
8676 }
8677 else
8678 {
8679 vex_reg = op + 1;
8680 gas_assert (vex_reg < i.operands);
8681 }
c0f3af97 8682 }
7ab9ffdd 8683 }
2426c15f 8684 else if (i.tm.opcode_modifier.vexvvvv == VEXNDD)
7ab9ffdd 8685 {
f12dc422 8686 /* For instructions with VexNDD, the register destination
7ab9ffdd 8687 is encoded in VEX prefix. */
f12dc422
L
8688 if (i.mem_operands == 0)
8689 {
8690 /* There is no memory operand. */
8691 gas_assert ((op + 2) == i.operands);
8692 vex_reg = op + 1;
8693 }
8694 else
8d63c93e 8695 {
ed438a93
JB
8696 /* There are only 2 non-immediate operands. */
8697 gas_assert (op < i.imm_operands + 2
8698 && i.operands == i.imm_operands + 2);
8699 vex_reg = i.imm_operands + 1;
f12dc422 8700 }
7ab9ffdd
L
8701 }
8702 else
8703 gas_assert (op < i.operands);
99018f42 8704
7ab9ffdd
L
8705 if (vex_reg != (unsigned int) ~0)
8706 {
f12dc422 8707 i386_operand_type *type = &i.tm.operand_types[vex_reg];
7ab9ffdd 8708
bab6aec1 8709 if ((type->bitfield.class != Reg
dc821c5f 8710 || (!type->bitfield.dword && !type->bitfield.qword))
3528c362 8711 && type->bitfield.class != RegSIMD
43234a1e 8712 && !operand_type_equal (type, &regmask))
7ab9ffdd 8713 abort ();
f88c9eb0 8714
7ab9ffdd
L
8715 i.vex.register_specifier = i.op[vex_reg].regs;
8716 }
8717
1b9f0c97
L
8718 /* Don't set OP operand twice. */
8719 if (vex_reg != op)
7ab9ffdd 8720 {
1b9f0c97
L
8721 /* If there is an extension opcode to put here, the
8722 register number must be put into the regmem field. */
8723 if (i.tm.extension_opcode != None)
8724 {
8725 i.rm.regmem = i.op[op].regs->reg_num;
a5aeccd9
JB
8726 set_rex_vrex (i.op[op].regs, REX_B,
8727 i.tm.opcode_modifier.sse2avx);
1b9f0c97
L
8728 }
8729 else
8730 {
8731 i.rm.reg = i.op[op].regs->reg_num;
a5aeccd9
JB
8732 set_rex_vrex (i.op[op].regs, REX_R,
8733 i.tm.opcode_modifier.sse2avx);
1b9f0c97 8734 }
7ab9ffdd 8735 }
252b5132 8736
29b0f896
AM
8737 /* Now, if no memory operand has set i.rm.mode = 0, 1, 2 we
8738 must set it to 3 to indicate this is a register operand
8739 in the regmem field. */
8740 if (!i.mem_operands)
8741 i.rm.mode = 3;
8742 }
252b5132 8743
29b0f896 8744 /* Fill in i.rm.reg field with extension opcode (if any). */
c1e679ec 8745 if (i.tm.extension_opcode != None)
29b0f896
AM
8746 i.rm.reg = i.tm.extension_opcode;
8747 }
8748 return default_seg;
8749}
252b5132 8750
48ef937e
JB
8751static INLINE void
8752frag_opcode_byte (unsigned char byte)
8753{
8754 if (now_seg != absolute_section)
8755 FRAG_APPEND_1_CHAR (byte);
8756 else
8757 ++abs_section_offset;
8758}
8759
376cd056
JB
8760static unsigned int
8761flip_code16 (unsigned int code16)
8762{
8763 gas_assert (i.tm.operands == 1);
8764
8765 return !(i.prefix[REX_PREFIX] & REX_W)
8766 && (code16 ? i.tm.operand_types[0].bitfield.disp32
8767 || i.tm.operand_types[0].bitfield.disp32s
8768 : i.tm.operand_types[0].bitfield.disp16)
8769 ? CODE16 : 0;
8770}
8771
29b0f896 8772static void
e3bb37b5 8773output_branch (void)
29b0f896
AM
8774{
8775 char *p;
f8a5c266 8776 int size;
29b0f896
AM
8777 int code16;
8778 int prefix;
8779 relax_substateT subtype;
8780 symbolS *sym;
8781 offsetT off;
8782
48ef937e
JB
8783 if (now_seg == absolute_section)
8784 {
8785 as_bad (_("relaxable branches not supported in absolute section"));
8786 return;
8787 }
8788
f8a5c266 8789 code16 = flag_code == CODE_16BIT ? CODE16 : 0;
a501d77e 8790 size = i.disp_encoding == disp_encoding_32bit ? BIG : SMALL;
29b0f896
AM
8791
8792 prefix = 0;
8793 if (i.prefix[DATA_PREFIX] != 0)
252b5132 8794 {
29b0f896
AM
8795 prefix = 1;
8796 i.prefixes -= 1;
376cd056 8797 code16 ^= flip_code16(code16);
252b5132 8798 }
29b0f896
AM
8799 /* Pentium4 branch hints. */
8800 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8801 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
2f66722d 8802 {
29b0f896
AM
8803 prefix++;
8804 i.prefixes--;
8805 }
8806 if (i.prefix[REX_PREFIX] != 0)
8807 {
8808 prefix++;
8809 i.prefixes--;
2f66722d
AM
8810 }
8811
7e8b059b
L
8812 /* BND prefixed jump. */
8813 if (i.prefix[BND_PREFIX] != 0)
8814 {
6cb0a70e
JB
8815 prefix++;
8816 i.prefixes--;
7e8b059b
L
8817 }
8818
f2810fe0
JB
8819 if (i.prefixes != 0)
8820 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
29b0f896
AM
8821
8822 /* It's always a symbol; End frag & setup for relax.
8823 Make sure there is enough room in this frag for the largest
8824 instruction we may generate in md_convert_frag. This is 2
8825 bytes for the opcode and room for the prefix and largest
8826 displacement. */
8827 frag_grow (prefix + 2 + 4);
8828 /* Prefix and 1 opcode byte go in fr_fix. */
8829 p = frag_more (prefix + 1);
8830 if (i.prefix[DATA_PREFIX] != 0)
8831 *p++ = DATA_PREFIX_OPCODE;
8832 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
8833 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
8834 *p++ = i.prefix[SEG_PREFIX];
6cb0a70e
JB
8835 if (i.prefix[BND_PREFIX] != 0)
8836 *p++ = BND_PREFIX_OPCODE;
29b0f896
AM
8837 if (i.prefix[REX_PREFIX] != 0)
8838 *p++ = i.prefix[REX_PREFIX];
8839 *p = i.tm.base_opcode;
8840
8841 if ((unsigned char) *p == JUMP_PC_RELATIVE)
f8a5c266 8842 subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, size);
40fb9820 8843 else if (cpu_arch_flags.bitfield.cpui386)
f8a5c266 8844 subtype = ENCODE_RELAX_STATE (COND_JUMP, size);
29b0f896 8845 else
f8a5c266 8846 subtype = ENCODE_RELAX_STATE (COND_JUMP86, size);
29b0f896 8847 subtype |= code16;
3e73aa7c 8848
29b0f896
AM
8849 sym = i.op[0].disps->X_add_symbol;
8850 off = i.op[0].disps->X_add_number;
3e73aa7c 8851
29b0f896
AM
8852 if (i.op[0].disps->X_op != O_constant
8853 && i.op[0].disps->X_op != O_symbol)
3e73aa7c 8854 {
29b0f896
AM
8855 /* Handle complex expressions. */
8856 sym = make_expr_symbol (i.op[0].disps);
8857 off = 0;
8858 }
3e73aa7c 8859
1ef3994a
JB
8860 frag_now->tc_frag_data.code64 = flag_code == CODE_64BIT;
8861
29b0f896
AM
8862 /* 1 possible extra opcode + 4 byte displacement go in var part.
8863 Pass reloc in fr_var. */
d258b828 8864 frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
29b0f896 8865}
3e73aa7c 8866
bd7ab16b
L
8867#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8868/* Return TRUE iff PLT32 relocation should be used for branching to
8869 symbol S. */
8870
5b7c81bd 8871static bool
bd7ab16b
L
8872need_plt32_p (symbolS *s)
8873{
8874 /* PLT32 relocation is ELF only. */
8875 if (!IS_ELF)
5b7c81bd 8876 return false;
bd7ab16b 8877
a5def729
RO
8878#ifdef TE_SOLARIS
8879 /* Don't emit PLT32 relocation on Solaris: neither native linker nor
8880 krtld support it. */
5b7c81bd 8881 return false;
a5def729
RO
8882#endif
8883
bd7ab16b
L
8884 /* Since there is no need to prepare for PLT branch on x86-64, we
8885 can generate R_X86_64_PLT32, instead of R_X86_64_PC32, which can
8886 be used as a marker for 32-bit PC-relative branches. */
8887 if (!object_64bit)
5b7c81bd 8888 return false;
bd7ab16b 8889
44365e88 8890 if (s == NULL)
5b7c81bd 8891 return false;
44365e88 8892
bd7ab16b
L
8893 /* Weak or undefined symbol need PLT32 relocation. */
8894 if (S_IS_WEAK (s) || !S_IS_DEFINED (s))
5b7c81bd 8895 return true;
bd7ab16b
L
8896
8897 /* Non-global symbol doesn't need PLT32 relocation. */
8898 if (! S_IS_EXTERNAL (s))
5b7c81bd 8899 return false;
bd7ab16b
L
8900
8901 /* Other global symbols need PLT32 relocation. NB: Symbol with
8902 non-default visibilities are treated as normal global symbol
8903 so that PLT32 relocation can be used as a marker for 32-bit
8904 PC-relative branches. It is useful for linker relaxation. */
5b7c81bd 8905 return true;
bd7ab16b
L
8906}
8907#endif
8908
29b0f896 8909static void
e3bb37b5 8910output_jump (void)
29b0f896
AM
8911{
8912 char *p;
8913 int size;
3e02c1cc 8914 fixS *fixP;
bd7ab16b 8915 bfd_reloc_code_real_type jump_reloc = i.reloc[0];
29b0f896 8916
0cfa3eb3 8917 if (i.tm.opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
8918 {
8919 /* This is a loop or jecxz type instruction. */
8920 size = 1;
8921 if (i.prefix[ADDR_PREFIX] != 0)
8922 {
48ef937e 8923 frag_opcode_byte (ADDR_PREFIX_OPCODE);
29b0f896
AM
8924 i.prefixes -= 1;
8925 }
8926 /* Pentium4 branch hints. */
8927 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8928 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
8929 {
48ef937e 8930 frag_opcode_byte (i.prefix[SEG_PREFIX]);
29b0f896 8931 i.prefixes--;
3e73aa7c
JH
8932 }
8933 }
29b0f896
AM
8934 else
8935 {
8936 int code16;
3e73aa7c 8937
29b0f896
AM
8938 code16 = 0;
8939 if (flag_code == CODE_16BIT)
8940 code16 = CODE16;
3e73aa7c 8941
29b0f896
AM
8942 if (i.prefix[DATA_PREFIX] != 0)
8943 {
48ef937e 8944 frag_opcode_byte (DATA_PREFIX_OPCODE);
29b0f896 8945 i.prefixes -= 1;
376cd056 8946 code16 ^= flip_code16(code16);
29b0f896 8947 }
252b5132 8948
29b0f896
AM
8949 size = 4;
8950 if (code16)
8951 size = 2;
8952 }
9fcc94b6 8953
6cb0a70e
JB
8954 /* BND prefixed jump. */
8955 if (i.prefix[BND_PREFIX] != 0)
29b0f896 8956 {
48ef937e 8957 frag_opcode_byte (i.prefix[BND_PREFIX]);
29b0f896
AM
8958 i.prefixes -= 1;
8959 }
252b5132 8960
6cb0a70e 8961 if (i.prefix[REX_PREFIX] != 0)
7e8b059b 8962 {
48ef937e 8963 frag_opcode_byte (i.prefix[REX_PREFIX]);
7e8b059b
L
8964 i.prefixes -= 1;
8965 }
8966
f2810fe0
JB
8967 if (i.prefixes != 0)
8968 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
e0890092 8969
48ef937e
JB
8970 if (now_seg == absolute_section)
8971 {
9a182d04 8972 abs_section_offset += i.opcode_length + size;
48ef937e
JB
8973 return;
8974 }
8975
9a182d04
JB
8976 p = frag_more (i.opcode_length + size);
8977 switch (i.opcode_length)
42164a71
L
8978 {
8979 case 2:
8980 *p++ = i.tm.base_opcode >> 8;
1a0670f3 8981 /* Fall through. */
42164a71
L
8982 case 1:
8983 *p++ = i.tm.base_opcode;
8984 break;
8985 default:
8986 abort ();
8987 }
e0890092 8988
bd7ab16b 8989#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1ef3994a
JB
8990 if (flag_code == CODE_64BIT && size == 4
8991 && jump_reloc == NO_RELOC && i.op[0].disps->X_add_number == 0
bd7ab16b
L
8992 && need_plt32_p (i.op[0].disps->X_add_symbol))
8993 jump_reloc = BFD_RELOC_X86_64_PLT32;
8994#endif
8995
8996 jump_reloc = reloc (size, 1, 1, jump_reloc);
8997
3e02c1cc 8998 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
bd7ab16b 8999 i.op[0].disps, 1, jump_reloc);
3e02c1cc 9000
eb19308f
JB
9001 /* All jumps handled here are signed, but don't unconditionally use a
9002 signed limit check for 32 and 16 bit jumps as we want to allow wrap
9003 around at 4G (outside of 64-bit mode) and 64k (except for XBEGIN)
9004 respectively. */
9005 switch (size)
9006 {
9007 case 1:
9008 fixP->fx_signed = 1;
9009 break;
9010
9011 case 2:
9012 if (i.tm.base_opcode == 0xc7f8)
9013 fixP->fx_signed = 1;
9014 break;
9015
9016 case 4:
9017 if (flag_code == CODE_64BIT)
9018 fixP->fx_signed = 1;
9019 break;
9020 }
29b0f896 9021}
e0890092 9022
29b0f896 9023static void
e3bb37b5 9024output_interseg_jump (void)
29b0f896
AM
9025{
9026 char *p;
9027 int size;
9028 int prefix;
9029 int code16;
252b5132 9030
29b0f896
AM
9031 code16 = 0;
9032 if (flag_code == CODE_16BIT)
9033 code16 = CODE16;
a217f122 9034
29b0f896
AM
9035 prefix = 0;
9036 if (i.prefix[DATA_PREFIX] != 0)
9037 {
9038 prefix = 1;
9039 i.prefixes -= 1;
9040 code16 ^= CODE16;
9041 }
6cb0a70e
JB
9042
9043 gas_assert (!i.prefix[REX_PREFIX]);
252b5132 9044
29b0f896
AM
9045 size = 4;
9046 if (code16)
9047 size = 2;
252b5132 9048
f2810fe0
JB
9049 if (i.prefixes != 0)
9050 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
252b5132 9051
48ef937e
JB
9052 if (now_seg == absolute_section)
9053 {
9054 abs_section_offset += prefix + 1 + 2 + size;
9055 return;
9056 }
9057
29b0f896
AM
9058 /* 1 opcode; 2 segment; offset */
9059 p = frag_more (prefix + 1 + 2 + size);
3e73aa7c 9060
29b0f896
AM
9061 if (i.prefix[DATA_PREFIX] != 0)
9062 *p++ = DATA_PREFIX_OPCODE;
252b5132 9063
29b0f896
AM
9064 if (i.prefix[REX_PREFIX] != 0)
9065 *p++ = i.prefix[REX_PREFIX];
252b5132 9066
29b0f896
AM
9067 *p++ = i.tm.base_opcode;
9068 if (i.op[1].imms->X_op == O_constant)
9069 {
9070 offsetT n = i.op[1].imms->X_add_number;
252b5132 9071
29b0f896
AM
9072 if (size == 2
9073 && !fits_in_unsigned_word (n)
9074 && !fits_in_signed_word (n))
9075 {
9076 as_bad (_("16-bit jump out of range"));
9077 return;
9078 }
9079 md_number_to_chars (p, n, size);
9080 }
9081 else
9082 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
d258b828 9083 i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
6d96a594
C
9084
9085 p += size;
9086 if (i.op[0].imms->X_op == O_constant)
9087 md_number_to_chars (p, (valueT) i.op[0].imms->X_add_number, 2);
9088 else
9089 fix_new_exp (frag_now, p - frag_now->fr_literal, 2,
9090 i.op[0].imms, 0, reloc (2, 0, 0, i.reloc[0]));
29b0f896 9091}
a217f122 9092
b4a3a7b4
L
9093#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9094void
9095x86_cleanup (void)
9096{
9097 char *p;
9098 asection *seg = now_seg;
9099 subsegT subseg = now_subseg;
9100 asection *sec;
9101 unsigned int alignment, align_size_1;
9102 unsigned int isa_1_descsz, feature_2_descsz, descsz;
9103 unsigned int isa_1_descsz_raw, feature_2_descsz_raw;
9104 unsigned int padding;
9105
1273b2f8 9106 if (!IS_ELF || !x86_used_note)
b4a3a7b4
L
9107 return;
9108
b4a3a7b4
L
9109 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X86;
9110
9111 /* The .note.gnu.property section layout:
9112
9113 Field Length Contents
9114 ---- ---- ----
9115 n_namsz 4 4
9116 n_descsz 4 The note descriptor size
9117 n_type 4 NT_GNU_PROPERTY_TYPE_0
9118 n_name 4 "GNU"
9119 n_desc n_descsz The program property array
9120 .... .... ....
9121 */
9122
9123 /* Create the .note.gnu.property section. */
9124 sec = subseg_new (NOTE_GNU_PROPERTY_SECTION_NAME, 0);
fd361982 9125 bfd_set_section_flags (sec,
b4a3a7b4
L
9126 (SEC_ALLOC
9127 | SEC_LOAD
9128 | SEC_DATA
9129 | SEC_HAS_CONTENTS
9130 | SEC_READONLY));
9131
9132 if (get_elf_backend_data (stdoutput)->s->elfclass == ELFCLASS64)
9133 {
9134 align_size_1 = 7;
9135 alignment = 3;
9136 }
9137 else
9138 {
9139 align_size_1 = 3;
9140 alignment = 2;
9141 }
9142
fd361982 9143 bfd_set_section_alignment (sec, alignment);
b4a3a7b4
L
9144 elf_section_type (sec) = SHT_NOTE;
9145
1273b2f8
L
9146 /* GNU_PROPERTY_X86_ISA_1_USED: 4-byte type + 4-byte data size
9147 + 4-byte data */
9148 isa_1_descsz_raw = 4 + 4 + 4;
9149 /* Align GNU_PROPERTY_X86_ISA_1_USED. */
9150 isa_1_descsz = (isa_1_descsz_raw + align_size_1) & ~align_size_1;
b4a3a7b4
L
9151
9152 feature_2_descsz_raw = isa_1_descsz;
9153 /* GNU_PROPERTY_X86_FEATURE_2_USED: 4-byte type + 4-byte data size
1273b2f8 9154 + 4-byte data */
b4a3a7b4
L
9155 feature_2_descsz_raw += 4 + 4 + 4;
9156 /* Align GNU_PROPERTY_X86_FEATURE_2_USED. */
9157 feature_2_descsz = ((feature_2_descsz_raw + align_size_1)
9158 & ~align_size_1);
9159
9160 descsz = feature_2_descsz;
9161 /* Section size: n_namsz + n_descsz + n_type + n_name + n_descsz. */
9162 p = frag_more (4 + 4 + 4 + 4 + descsz);
9163
9164 /* Write n_namsz. */
9165 md_number_to_chars (p, (valueT) 4, 4);
9166
9167 /* Write n_descsz. */
9168 md_number_to_chars (p + 4, (valueT) descsz, 4);
9169
9170 /* Write n_type. */
9171 md_number_to_chars (p + 4 * 2, (valueT) NT_GNU_PROPERTY_TYPE_0, 4);
9172
9173 /* Write n_name. */
9174 memcpy (p + 4 * 3, "GNU", 4);
9175
1273b2f8
L
9176 /* Write 4-byte type. */
9177 md_number_to_chars (p + 4 * 4,
9178 (valueT) GNU_PROPERTY_X86_ISA_1_USED, 4);
b4a3a7b4 9179
1273b2f8
L
9180 /* Write 4-byte data size. */
9181 md_number_to_chars (p + 4 * 5, (valueT) 4, 4);
b4a3a7b4 9182
1273b2f8
L
9183 /* Write 4-byte data. */
9184 md_number_to_chars (p + 4 * 6, (valueT) x86_isa_1_used, 4);
b4a3a7b4 9185
1273b2f8
L
9186 /* Zero out paddings. */
9187 padding = isa_1_descsz - isa_1_descsz_raw;
9188 if (padding)
9189 memset (p + 4 * 7, 0, padding);
b4a3a7b4
L
9190
9191 /* Write 4-byte type. */
9192 md_number_to_chars (p + isa_1_descsz + 4 * 4,
9193 (valueT) GNU_PROPERTY_X86_FEATURE_2_USED, 4);
9194
9195 /* Write 4-byte data size. */
9196 md_number_to_chars (p + isa_1_descsz + 4 * 5, (valueT) 4, 4);
9197
9198 /* Write 4-byte data. */
9199 md_number_to_chars (p + isa_1_descsz + 4 * 6,
9200 (valueT) x86_feature_2_used, 4);
9201
9202 /* Zero out paddings. */
9203 padding = feature_2_descsz - feature_2_descsz_raw;
9204 if (padding)
9205 memset (p + isa_1_descsz + 4 * 7, 0, padding);
9206
9207 /* We probably can't restore the current segment, for there likely
9208 isn't one yet... */
9209 if (seg && subseg)
9210 subseg_set (seg, subseg);
9211}
9212#endif
9213
9c33702b
JB
9214static unsigned int
9215encoding_length (const fragS *start_frag, offsetT start_off,
9216 const char *frag_now_ptr)
9217{
9218 unsigned int len = 0;
9219
9220 if (start_frag != frag_now)
9221 {
9222 const fragS *fr = start_frag;
9223
9224 do {
9225 len += fr->fr_fix;
9226 fr = fr->fr_next;
9227 } while (fr && fr != frag_now);
9228 }
9229
9230 return len - start_off + (frag_now_ptr - frag_now->fr_literal);
9231}
9232
e379e5f3 9233/* Return 1 for test, and, cmp, add, sub, inc and dec which may
79d72f45
HL
9234 be macro-fused with conditional jumps.
9235 NB: If TEST/AND/CMP/ADD/SUB/INC/DEC is of RIP relative address,
9236 or is one of the following format:
9237
9238 cmp m, imm
9239 add m, imm
9240 sub m, imm
9241 test m, imm
9242 and m, imm
9243 inc m
9244 dec m
9245
9246 it is unfusible. */
e379e5f3
L
9247
9248static int
79d72f45 9249maybe_fused_with_jcc_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
9250{
9251 /* No RIP address. */
9252 if (i.base_reg && i.base_reg->reg_num == RegIP)
9253 return 0;
9254
389d00a5
JB
9255 /* No opcodes outside of base encoding space. */
9256 if (i.tm.opcode_modifier.opcodespace != SPACE_BASE)
e379e5f3
L
9257 return 0;
9258
79d72f45
HL
9259 /* add, sub without add/sub m, imm. */
9260 if (i.tm.base_opcode <= 5
e379e5f3
L
9261 || (i.tm.base_opcode >= 0x28 && i.tm.base_opcode <= 0x2d)
9262 || ((i.tm.base_opcode | 3) == 0x83
79d72f45 9263 && (i.tm.extension_opcode == 0x5
e379e5f3 9264 || i.tm.extension_opcode == 0x0)))
79d72f45
HL
9265 {
9266 *mf_cmp_p = mf_cmp_alu_cmp;
9267 return !(i.mem_operands && i.imm_operands);
9268 }
e379e5f3 9269
79d72f45
HL
9270 /* and without and m, imm. */
9271 if ((i.tm.base_opcode >= 0x20 && i.tm.base_opcode <= 0x25)
9272 || ((i.tm.base_opcode | 3) == 0x83
9273 && i.tm.extension_opcode == 0x4))
9274 {
9275 *mf_cmp_p = mf_cmp_test_and;
9276 return !(i.mem_operands && i.imm_operands);
9277 }
9278
9279 /* test without test m imm. */
e379e5f3
L
9280 if ((i.tm.base_opcode | 1) == 0x85
9281 || (i.tm.base_opcode | 1) == 0xa9
9282 || ((i.tm.base_opcode | 1) == 0xf7
79d72f45
HL
9283 && i.tm.extension_opcode == 0))
9284 {
9285 *mf_cmp_p = mf_cmp_test_and;
9286 return !(i.mem_operands && i.imm_operands);
9287 }
9288
9289 /* cmp without cmp m, imm. */
9290 if ((i.tm.base_opcode >= 0x38 && i.tm.base_opcode <= 0x3d)
e379e5f3
L
9291 || ((i.tm.base_opcode | 3) == 0x83
9292 && (i.tm.extension_opcode == 0x7)))
79d72f45
HL
9293 {
9294 *mf_cmp_p = mf_cmp_alu_cmp;
9295 return !(i.mem_operands && i.imm_operands);
9296 }
e379e5f3 9297
79d72f45 9298 /* inc, dec without inc/dec m. */
e379e5f3
L
9299 if ((i.tm.cpu_flags.bitfield.cpuno64
9300 && (i.tm.base_opcode | 0xf) == 0x4f)
9301 || ((i.tm.base_opcode | 1) == 0xff
9302 && i.tm.extension_opcode <= 0x1))
79d72f45
HL
9303 {
9304 *mf_cmp_p = mf_cmp_incdec;
9305 return !i.mem_operands;
9306 }
e379e5f3
L
9307
9308 return 0;
9309}
9310
9311/* Return 1 if a FUSED_JCC_PADDING frag should be generated. */
9312
9313static int
79d72f45 9314add_fused_jcc_padding_frag_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
9315{
9316 /* NB: Don't work with COND_JUMP86 without i386. */
9317 if (!align_branch_power
9318 || now_seg == absolute_section
9319 || !cpu_arch_flags.bitfield.cpui386
9320 || !(align_branch & align_branch_fused_bit))
9321 return 0;
9322
79d72f45 9323 if (maybe_fused_with_jcc_p (mf_cmp_p))
e379e5f3
L
9324 {
9325 if (last_insn.kind == last_insn_other
9326 || last_insn.seg != now_seg)
9327 return 1;
9328 if (flag_debug)
9329 as_warn_where (last_insn.file, last_insn.line,
9330 _("`%s` skips -malign-branch-boundary on `%s`"),
9331 last_insn.name, i.tm.name);
9332 }
9333
9334 return 0;
9335}
9336
9337/* Return 1 if a BRANCH_PREFIX frag should be generated. */
9338
9339static int
9340add_branch_prefix_frag_p (void)
9341{
9342 /* NB: Don't work with COND_JUMP86 without i386. Don't add prefix
9343 to PadLock instructions since they include prefixes in opcode. */
9344 if (!align_branch_power
9345 || !align_branch_prefix_size
9346 || now_seg == absolute_section
9347 || i.tm.cpu_flags.bitfield.cpupadlock
9348 || !cpu_arch_flags.bitfield.cpui386)
9349 return 0;
9350
9351 /* Don't add prefix if it is a prefix or there is no operand in case
9352 that segment prefix is special. */
9353 if (!i.operands || i.tm.opcode_modifier.isprefix)
9354 return 0;
9355
9356 if (last_insn.kind == last_insn_other
9357 || last_insn.seg != now_seg)
9358 return 1;
9359
9360 if (flag_debug)
9361 as_warn_where (last_insn.file, last_insn.line,
9362 _("`%s` skips -malign-branch-boundary on `%s`"),
9363 last_insn.name, i.tm.name);
9364
9365 return 0;
9366}
9367
9368/* Return 1 if a BRANCH_PADDING frag should be generated. */
9369
9370static int
79d72f45
HL
9371add_branch_padding_frag_p (enum align_branch_kind *branch_p,
9372 enum mf_jcc_kind *mf_jcc_p)
e379e5f3
L
9373{
9374 int add_padding;
9375
9376 /* NB: Don't work with COND_JUMP86 without i386. */
9377 if (!align_branch_power
9378 || now_seg == absolute_section
389d00a5
JB
9379 || !cpu_arch_flags.bitfield.cpui386
9380 || i.tm.opcode_modifier.opcodespace != SPACE_BASE)
e379e5f3
L
9381 return 0;
9382
9383 add_padding = 0;
9384
9385 /* Check for jcc and direct jmp. */
9386 if (i.tm.opcode_modifier.jump == JUMP)
9387 {
9388 if (i.tm.base_opcode == JUMP_PC_RELATIVE)
9389 {
9390 *branch_p = align_branch_jmp;
9391 add_padding = align_branch & align_branch_jmp_bit;
9392 }
9393 else
9394 {
79d72f45
HL
9395 /* Because J<cc> and JN<cc> share same group in macro-fusible table,
9396 igore the lowest bit. */
9397 *mf_jcc_p = (i.tm.base_opcode & 0x0e) >> 1;
e379e5f3
L
9398 *branch_p = align_branch_jcc;
9399 if ((align_branch & align_branch_jcc_bit))
9400 add_padding = 1;
9401 }
9402 }
e379e5f3
L
9403 else if ((i.tm.base_opcode | 1) == 0xc3)
9404 {
9405 /* Near ret. */
9406 *branch_p = align_branch_ret;
9407 if ((align_branch & align_branch_ret_bit))
9408 add_padding = 1;
9409 }
9410 else
9411 {
9412 /* Check for indirect jmp, direct and indirect calls. */
9413 if (i.tm.base_opcode == 0xe8)
9414 {
9415 /* Direct call. */
9416 *branch_p = align_branch_call;
9417 if ((align_branch & align_branch_call_bit))
9418 add_padding = 1;
9419 }
9420 else if (i.tm.base_opcode == 0xff
9421 && (i.tm.extension_opcode == 2
9422 || i.tm.extension_opcode == 4))
9423 {
9424 /* Indirect call and jmp. */
9425 *branch_p = align_branch_indirect;
9426 if ((align_branch & align_branch_indirect_bit))
9427 add_padding = 1;
9428 }
9429
9430 if (add_padding
9431 && i.disp_operands
9432 && tls_get_addr
9433 && (i.op[0].disps->X_op == O_symbol
9434 || (i.op[0].disps->X_op == O_subtract
9435 && i.op[0].disps->X_op_symbol == GOT_symbol)))
9436 {
9437 symbolS *s = i.op[0].disps->X_add_symbol;
9438 /* No padding to call to global or undefined tls_get_addr. */
9439 if ((S_IS_EXTERNAL (s) || !S_IS_DEFINED (s))
9440 && strcmp (S_GET_NAME (s), tls_get_addr) == 0)
9441 return 0;
9442 }
9443 }
9444
9445 if (add_padding
9446 && last_insn.kind != last_insn_other
9447 && last_insn.seg == now_seg)
9448 {
9449 if (flag_debug)
9450 as_warn_where (last_insn.file, last_insn.line,
9451 _("`%s` skips -malign-branch-boundary on `%s`"),
9452 last_insn.name, i.tm.name);
9453 return 0;
9454 }
9455
9456 return add_padding;
9457}
9458
29b0f896 9459static void
e3bb37b5 9460output_insn (void)
29b0f896 9461{
2bbd9c25
JJ
9462 fragS *insn_start_frag;
9463 offsetT insn_start_off;
e379e5f3
L
9464 fragS *fragP = NULL;
9465 enum align_branch_kind branch = align_branch_none;
79d72f45
HL
9466 /* The initializer is arbitrary just to avoid uninitialized error.
9467 it's actually either assigned in add_branch_padding_frag_p
9468 or never be used. */
9469 enum mf_jcc_kind mf_jcc = mf_jcc_jo;
2bbd9c25 9470
b4a3a7b4 9471#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
48ef937e 9472 if (IS_ELF && x86_used_note && now_seg != absolute_section)
b4a3a7b4 9473 {
32930e4e
L
9474 if ((i.xstate & xstate_tmm) == xstate_tmm
9475 || i.tm.cpu_flags.bitfield.cpuamx_tile)
9476 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_TMM;
9477
b4a3a7b4
L
9478 if (i.tm.cpu_flags.bitfield.cpu8087
9479 || i.tm.cpu_flags.bitfield.cpu287
9480 || i.tm.cpu_flags.bitfield.cpu387
9481 || i.tm.cpu_flags.bitfield.cpu687
9482 || i.tm.cpu_flags.bitfield.cpufisttp)
9483 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X87;
014d61ea 9484
921eafea 9485 if ((i.xstate & xstate_mmx)
389d00a5
JB
9486 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
9487 && !is_any_vex_encoding (&i.tm)
9488 && (i.tm.base_opcode == 0x77 /* emms */
9489 || i.tm.base_opcode == 0x0e /* femms */)))
b4a3a7b4 9490 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MMX;
014d61ea 9491
32930e4e
L
9492 if (i.index_reg)
9493 {
9494 if (i.index_reg->reg_type.bitfield.zmmword)
9495 i.xstate |= xstate_zmm;
9496 else if (i.index_reg->reg_type.bitfield.ymmword)
9497 i.xstate |= xstate_ymm;
9498 else if (i.index_reg->reg_type.bitfield.xmmword)
9499 i.xstate |= xstate_xmm;
9500 }
014d61ea
JB
9501
9502 /* vzeroall / vzeroupper */
9503 if (i.tm.base_opcode == 0x77 && i.tm.cpu_flags.bitfield.cpuavx)
9504 i.xstate |= xstate_ymm;
9505
c4694f17 9506 if ((i.xstate & xstate_xmm)
389d00a5
JB
9507 /* ldmxcsr / stmxcsr / vldmxcsr / vstmxcsr */
9508 || (i.tm.base_opcode == 0xae
9509 && (i.tm.cpu_flags.bitfield.cpusse
9510 || i.tm.cpu_flags.bitfield.cpuavx))
c4694f17
TG
9511 || i.tm.cpu_flags.bitfield.cpuwidekl
9512 || i.tm.cpu_flags.bitfield.cpukl)
b4a3a7b4 9513 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XMM;
014d61ea 9514
921eafea 9515 if ((i.xstate & xstate_ymm) == xstate_ymm)
b4a3a7b4 9516 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_YMM;
921eafea 9517 if ((i.xstate & xstate_zmm) == xstate_zmm)
b4a3a7b4 9518 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_ZMM;
6225c532 9519 if (i.mask.reg || (i.xstate & xstate_mask) == xstate_mask)
32930e4e 9520 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MASK;
b4a3a7b4
L
9521 if (i.tm.cpu_flags.bitfield.cpufxsr)
9522 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_FXSR;
9523 if (i.tm.cpu_flags.bitfield.cpuxsave)
9524 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVE;
9525 if (i.tm.cpu_flags.bitfield.cpuxsaveopt)
9526 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT;
9527 if (i.tm.cpu_flags.bitfield.cpuxsavec)
9528 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEC;
b0ab0693
L
9529
9530 if (x86_feature_2_used
9531 || i.tm.cpu_flags.bitfield.cpucmov
9532 || i.tm.cpu_flags.bitfield.cpusyscall
389d00a5
JB
9533 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
9534 && i.tm.base_opcode == 0xc7
70e95837 9535 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
b0ab0693
L
9536 && i.tm.extension_opcode == 1) /* cmpxchg8b */)
9537 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_BASELINE;
9538 if (i.tm.cpu_flags.bitfield.cpusse3
9539 || i.tm.cpu_flags.bitfield.cpussse3
9540 || i.tm.cpu_flags.bitfield.cpusse4_1
9541 || i.tm.cpu_flags.bitfield.cpusse4_2
9542 || i.tm.cpu_flags.bitfield.cpucx16
9543 || i.tm.cpu_flags.bitfield.cpupopcnt
9544 /* LAHF-SAHF insns in 64-bit mode. */
9545 || (flag_code == CODE_64BIT
35648716
JB
9546 && (i.tm.base_opcode | 1) == 0x9f
9547 && i.tm.opcode_modifier.opcodespace == SPACE_BASE))
b0ab0693
L
9548 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V2;
9549 if (i.tm.cpu_flags.bitfield.cpuavx
9550 || i.tm.cpu_flags.bitfield.cpuavx2
9551 /* Any VEX encoded insns execpt for CpuAVX512F, CpuAVX512BW,
9552 CpuAVX512DQ, LPW, TBM and AMX. */
9553 || (i.tm.opcode_modifier.vex
9554 && !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.cpulwp
9558 && !i.tm.cpu_flags.bitfield.cputbm
9559 && !(x86_feature_2_used & GNU_PROPERTY_X86_FEATURE_2_TMM))
9560 || i.tm.cpu_flags.bitfield.cpuf16c
9561 || i.tm.cpu_flags.bitfield.cpufma
9562 || i.tm.cpu_flags.bitfield.cpulzcnt
9563 || i.tm.cpu_flags.bitfield.cpumovbe
9564 || i.tm.cpu_flags.bitfield.cpuxsaves
9565 || (x86_feature_2_used
9566 & (GNU_PROPERTY_X86_FEATURE_2_XSAVE
9567 | GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT
9568 | GNU_PROPERTY_X86_FEATURE_2_XSAVEC)) != 0)
9569 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V3;
9570 if (i.tm.cpu_flags.bitfield.cpuavx512f
9571 || i.tm.cpu_flags.bitfield.cpuavx512bw
9572 || i.tm.cpu_flags.bitfield.cpuavx512dq
9573 || i.tm.cpu_flags.bitfield.cpuavx512vl
9574 /* Any EVEX encoded insns except for AVX512ER, AVX512PF and
9575 VNNIW. */
9576 || (i.tm.opcode_modifier.evex
9577 && !i.tm.cpu_flags.bitfield.cpuavx512er
9578 && !i.tm.cpu_flags.bitfield.cpuavx512pf
9579 && !i.tm.cpu_flags.bitfield.cpuavx512_4vnniw))
9580 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V4;
b4a3a7b4
L
9581 }
9582#endif
9583
29b0f896
AM
9584 /* Tie dwarf2 debug info to the address at the start of the insn.
9585 We can't do this after the insn has been output as the current
9586 frag may have been closed off. eg. by frag_var. */
9587 dwarf2_emit_insn (0);
9588
2bbd9c25
JJ
9589 insn_start_frag = frag_now;
9590 insn_start_off = frag_now_fix ();
9591
79d72f45 9592 if (add_branch_padding_frag_p (&branch, &mf_jcc))
e379e5f3
L
9593 {
9594 char *p;
9595 /* Branch can be 8 bytes. Leave some room for prefixes. */
9596 unsigned int max_branch_padding_size = 14;
9597
9598 /* Align section to boundary. */
9599 record_alignment (now_seg, align_branch_power);
9600
9601 /* Make room for padding. */
9602 frag_grow (max_branch_padding_size);
9603
9604 /* Start of the padding. */
9605 p = frag_more (0);
9606
9607 fragP = frag_now;
9608
9609 frag_var (rs_machine_dependent, max_branch_padding_size, 0,
9610 ENCODE_RELAX_STATE (BRANCH_PADDING, 0),
9611 NULL, 0, p);
9612
79d72f45 9613 fragP->tc_frag_data.mf_type = mf_jcc;
e379e5f3
L
9614 fragP->tc_frag_data.branch_type = branch;
9615 fragP->tc_frag_data.max_bytes = max_branch_padding_size;
9616 }
9617
29b0f896 9618 /* Output jumps. */
0cfa3eb3 9619 if (i.tm.opcode_modifier.jump == JUMP)
29b0f896 9620 output_branch ();
0cfa3eb3
JB
9621 else if (i.tm.opcode_modifier.jump == JUMP_BYTE
9622 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896 9623 output_jump ();
0cfa3eb3 9624 else if (i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT)
29b0f896
AM
9625 output_interseg_jump ();
9626 else
9627 {
9628 /* Output normal instructions here. */
9629 char *p;
9630 unsigned char *q;
47465058 9631 unsigned int j;
79d72f45 9632 enum mf_cmp_kind mf_cmp;
4dffcebc 9633
e4e00185 9634 if (avoid_fence
389d00a5
JB
9635 && (i.tm.base_opcode == 0xaee8
9636 || i.tm.base_opcode == 0xaef0
9637 || i.tm.base_opcode == 0xaef8))
48ef937e
JB
9638 {
9639 /* Encode lfence, mfence, and sfence as
9640 f0 83 04 24 00 lock addl $0x0, (%{re}sp). */
47f4115a
JB
9641 if (flag_code == CODE_16BIT)
9642 as_bad (_("Cannot convert `%s' in 16-bit mode"), i.tm.name);
9643 else if (omit_lock_prefix)
9644 as_bad (_("Cannot convert `%s' with `-momit-lock-prefix=yes' in effect"),
9645 i.tm.name);
9646 else if (now_seg != absolute_section)
48ef937e
JB
9647 {
9648 offsetT val = 0x240483f0ULL;
9649
9650 p = frag_more (5);
9651 md_number_to_chars (p, val, 5);
9652 }
9653 else
9654 abs_section_offset += 5;
9655 return;
9656 }
e4e00185 9657
d022bddd
IT
9658 /* Some processors fail on LOCK prefix. This options makes
9659 assembler ignore LOCK prefix and serves as a workaround. */
9660 if (omit_lock_prefix)
9661 {
35648716
JB
9662 if (i.tm.base_opcode == LOCK_PREFIX_OPCODE
9663 && i.tm.opcode_modifier.isprefix)
d022bddd
IT
9664 return;
9665 i.prefix[LOCK_PREFIX] = 0;
9666 }
9667
e379e5f3
L
9668 if (branch)
9669 /* Skip if this is a branch. */
9670 ;
79d72f45 9671 else if (add_fused_jcc_padding_frag_p (&mf_cmp))
e379e5f3
L
9672 {
9673 /* Make room for padding. */
9674 frag_grow (MAX_FUSED_JCC_PADDING_SIZE);
9675 p = frag_more (0);
9676
9677 fragP = frag_now;
9678
9679 frag_var (rs_machine_dependent, MAX_FUSED_JCC_PADDING_SIZE, 0,
9680 ENCODE_RELAX_STATE (FUSED_JCC_PADDING, 0),
9681 NULL, 0, p);
9682
79d72f45 9683 fragP->tc_frag_data.mf_type = mf_cmp;
e379e5f3
L
9684 fragP->tc_frag_data.branch_type = align_branch_fused;
9685 fragP->tc_frag_data.max_bytes = MAX_FUSED_JCC_PADDING_SIZE;
9686 }
9687 else if (add_branch_prefix_frag_p ())
9688 {
9689 unsigned int max_prefix_size = align_branch_prefix_size;
9690
9691 /* Make room for padding. */
9692 frag_grow (max_prefix_size);
9693 p = frag_more (0);
9694
9695 fragP = frag_now;
9696
9697 frag_var (rs_machine_dependent, max_prefix_size, 0,
9698 ENCODE_RELAX_STATE (BRANCH_PREFIX, 0),
9699 NULL, 0, p);
9700
9701 fragP->tc_frag_data.max_bytes = max_prefix_size;
9702 }
9703
43234a1e
L
9704 /* Since the VEX/EVEX prefix contains the implicit prefix, we
9705 don't need the explicit prefix. */
9706 if (!i.tm.opcode_modifier.vex && !i.tm.opcode_modifier.evex)
bc4bd9ab 9707 {
7b47a312 9708 switch (i.tm.opcode_modifier.opcodeprefix)
bc4bd9ab 9709 {
7b47a312
L
9710 case PREFIX_0X66:
9711 add_prefix (0x66);
9712 break;
9713 case PREFIX_0XF2:
9714 add_prefix (0xf2);
9715 break;
9716 case PREFIX_0XF3:
8b65b895
L
9717 if (!i.tm.cpu_flags.bitfield.cpupadlock
9718 || (i.prefix[REP_PREFIX] != 0xf3))
9719 add_prefix (0xf3);
c0f3af97 9720 break;
7b47a312 9721 case PREFIX_NONE:
9a182d04 9722 switch (i.opcode_length)
c0f3af97 9723 {
7b47a312 9724 case 2:
7b47a312 9725 break;
9a182d04 9726 case 1:
7b47a312 9727 /* Check for pseudo prefixes. */
9a182d04
JB
9728 if (!i.tm.opcode_modifier.isprefix || i.tm.base_opcode)
9729 break;
7b47a312
L
9730 as_bad_where (insn_start_frag->fr_file,
9731 insn_start_frag->fr_line,
9732 _("pseudo prefix without instruction"));
9733 return;
9734 default:
9735 abort ();
4dffcebc 9736 }
c0f3af97 9737 break;
c0f3af97
L
9738 default:
9739 abort ();
bc4bd9ab 9740 }
c0f3af97 9741
6d19a37a 9742#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
cf61b747
L
9743 /* For x32, add a dummy REX_OPCODE prefix for mov/add with
9744 R_X86_64_GOTTPOFF relocation so that linker can safely
14470f07
L
9745 perform IE->LE optimization. A dummy REX_OPCODE prefix
9746 is also needed for lea with R_X86_64_GOTPC32_TLSDESC
9747 relocation for GDesc -> IE/LE optimization. */
cf61b747
L
9748 if (x86_elf_abi == X86_64_X32_ABI
9749 && i.operands == 2
14470f07
L
9750 && (i.reloc[0] == BFD_RELOC_X86_64_GOTTPOFF
9751 || i.reloc[0] == BFD_RELOC_X86_64_GOTPC32_TLSDESC)
cf61b747
L
9752 && i.prefix[REX_PREFIX] == 0)
9753 add_prefix (REX_OPCODE);
6d19a37a 9754#endif
cf61b747 9755
c0f3af97
L
9756 /* The prefix bytes. */
9757 for (j = ARRAY_SIZE (i.prefix), q = i.prefix; j > 0; j--, q++)
9758 if (*q)
48ef937e 9759 frag_opcode_byte (*q);
0f10071e 9760 }
ae5c1c7b 9761 else
c0f3af97
L
9762 {
9763 for (j = 0, q = i.prefix; j < ARRAY_SIZE (i.prefix); j++, q++)
9764 if (*q)
9765 switch (j)
9766 {
c0f3af97
L
9767 case SEG_PREFIX:
9768 case ADDR_PREFIX:
48ef937e 9769 frag_opcode_byte (*q);
c0f3af97
L
9770 break;
9771 default:
9772 /* There should be no other prefixes for instructions
9773 with VEX prefix. */
9774 abort ();
9775 }
9776
43234a1e
L
9777 /* For EVEX instructions i.vrex should become 0 after
9778 build_evex_prefix. For VEX instructions upper 16 registers
9779 aren't available, so VREX should be 0. */
9780 if (i.vrex)
9781 abort ();
c0f3af97 9782 /* Now the VEX prefix. */
48ef937e
JB
9783 if (now_seg != absolute_section)
9784 {
9785 p = frag_more (i.vex.length);
9786 for (j = 0; j < i.vex.length; j++)
9787 p[j] = i.vex.bytes[j];
9788 }
9789 else
9790 abs_section_offset += i.vex.length;
c0f3af97 9791 }
252b5132 9792
29b0f896 9793 /* Now the opcode; be careful about word order here! */
389d00a5
JB
9794 j = i.opcode_length;
9795 if (!i.vex.length)
9796 switch (i.tm.opcode_modifier.opcodespace)
9797 {
9798 case SPACE_BASE:
9799 break;
9800 case SPACE_0F:
9801 ++j;
9802 break;
9803 case SPACE_0F38:
9804 case SPACE_0F3A:
9805 j += 2;
9806 break;
9807 default:
9808 abort ();
9809 }
9810
48ef937e 9811 if (now_seg == absolute_section)
389d00a5
JB
9812 abs_section_offset += j;
9813 else if (j == 1)
29b0f896
AM
9814 {
9815 FRAG_APPEND_1_CHAR (i.tm.base_opcode);
9816 }
9817 else
9818 {
389d00a5
JB
9819 p = frag_more (j);
9820 if (!i.vex.length
9821 && i.tm.opcode_modifier.opcodespace != SPACE_BASE)
9822 {
9823 *p++ = 0x0f;
9824 if (i.tm.opcode_modifier.opcodespace != SPACE_0F)
9825 *p++ = i.tm.opcode_modifier.opcodespace == SPACE_0F38
9826 ? 0x38 : 0x3a;
9827 }
9828
9a182d04 9829 switch (i.opcode_length)
331d2d0d 9830 {
4dffcebc 9831 case 2:
389d00a5
JB
9832 /* Put out high byte first: can't use md_number_to_chars! */
9833 *p++ = (i.tm.base_opcode >> 8) & 0xff;
9834 /* Fall through. */
9835 case 1:
9836 *p = i.tm.base_opcode & 0xff;
4dffcebc
L
9837 break;
9838 default:
9839 abort ();
9840 break;
331d2d0d 9841 }
0f10071e 9842
29b0f896 9843 }
3e73aa7c 9844
29b0f896 9845 /* Now the modrm byte and sib byte (if present). */
40fb9820 9846 if (i.tm.opcode_modifier.modrm)
29b0f896 9847 {
48ef937e
JB
9848 frag_opcode_byte ((i.rm.regmem << 0)
9849 | (i.rm.reg << 3)
9850 | (i.rm.mode << 6));
29b0f896
AM
9851 /* If i.rm.regmem == ESP (4)
9852 && i.rm.mode != (Register mode)
9853 && not 16 bit
9854 ==> need second modrm byte. */
9855 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
9856 && i.rm.mode != 3
dc821c5f 9857 && !(i.base_reg && i.base_reg->reg_type.bitfield.word))
48ef937e
JB
9858 frag_opcode_byte ((i.sib.base << 0)
9859 | (i.sib.index << 3)
9860 | (i.sib.scale << 6));
29b0f896 9861 }
3e73aa7c 9862
29b0f896 9863 if (i.disp_operands)
2bbd9c25 9864 output_disp (insn_start_frag, insn_start_off);
3e73aa7c 9865
29b0f896 9866 if (i.imm_operands)
2bbd9c25 9867 output_imm (insn_start_frag, insn_start_off);
9c33702b
JB
9868
9869 /*
9870 * frag_now_fix () returning plain abs_section_offset when we're in the
9871 * absolute section, and abs_section_offset not getting updated as data
9872 * gets added to the frag breaks the logic below.
9873 */
9874 if (now_seg != absolute_section)
9875 {
9876 j = encoding_length (insn_start_frag, insn_start_off, frag_more (0));
9877 if (j > 15)
9878 as_warn (_("instruction length of %u bytes exceeds the limit of 15"),
9879 j);
e379e5f3
L
9880 else if (fragP)
9881 {
9882 /* NB: Don't add prefix with GOTPC relocation since
9883 output_disp() above depends on the fixed encoding
9884 length. Can't add prefix with TLS relocation since
9885 it breaks TLS linker optimization. */
9886 unsigned int max = i.has_gotpc_tls_reloc ? 0 : 15 - j;
9887 /* Prefix count on the current instruction. */
9888 unsigned int count = i.vex.length;
9889 unsigned int k;
9890 for (k = 0; k < ARRAY_SIZE (i.prefix); k++)
9891 /* REX byte is encoded in VEX/EVEX prefix. */
9892 if (i.prefix[k] && (k != REX_PREFIX || !i.vex.length))
9893 count++;
9894
9895 /* Count prefixes for extended opcode maps. */
9896 if (!i.vex.length)
389d00a5 9897 switch (i.tm.opcode_modifier.opcodespace)
e379e5f3 9898 {
389d00a5 9899 case SPACE_BASE:
e379e5f3 9900 break;
389d00a5
JB
9901 case SPACE_0F:
9902 count++;
e379e5f3 9903 break;
389d00a5
JB
9904 case SPACE_0F38:
9905 case SPACE_0F3A:
9906 count += 2;
e379e5f3
L
9907 break;
9908 default:
9909 abort ();
9910 }
9911
9912 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
9913 == BRANCH_PREFIX)
9914 {
9915 /* Set the maximum prefix size in BRANCH_PREFIX
9916 frag. */
9917 if (fragP->tc_frag_data.max_bytes > max)
9918 fragP->tc_frag_data.max_bytes = max;
9919 if (fragP->tc_frag_data.max_bytes > count)
9920 fragP->tc_frag_data.max_bytes -= count;
9921 else
9922 fragP->tc_frag_data.max_bytes = 0;
9923 }
9924 else
9925 {
9926 /* Remember the maximum prefix size in FUSED_JCC_PADDING
9927 frag. */
9928 unsigned int max_prefix_size;
9929 if (align_branch_prefix_size > max)
9930 max_prefix_size = max;
9931 else
9932 max_prefix_size = align_branch_prefix_size;
9933 if (max_prefix_size > count)
9934 fragP->tc_frag_data.max_prefix_length
9935 = max_prefix_size - count;
9936 }
9937
9938 /* Use existing segment prefix if possible. Use CS
9939 segment prefix in 64-bit mode. In 32-bit mode, use SS
9940 segment prefix with ESP/EBP base register and use DS
9941 segment prefix without ESP/EBP base register. */
9942 if (i.prefix[SEG_PREFIX])
9943 fragP->tc_frag_data.default_prefix = i.prefix[SEG_PREFIX];
9944 else if (flag_code == CODE_64BIT)
9945 fragP->tc_frag_data.default_prefix = CS_PREFIX_OPCODE;
9946 else if (i.base_reg
9947 && (i.base_reg->reg_num == 4
9948 || i.base_reg->reg_num == 5))
9949 fragP->tc_frag_data.default_prefix = SS_PREFIX_OPCODE;
9950 else
9951 fragP->tc_frag_data.default_prefix = DS_PREFIX_OPCODE;
9952 }
9c33702b 9953 }
29b0f896 9954 }
252b5132 9955
e379e5f3
L
9956 /* NB: Don't work with COND_JUMP86 without i386. */
9957 if (align_branch_power
9958 && now_seg != absolute_section
9959 && cpu_arch_flags.bitfield.cpui386)
9960 {
9961 /* Terminate each frag so that we can add prefix and check for
9962 fused jcc. */
9963 frag_wane (frag_now);
9964 frag_new (0);
9965 }
9966
29b0f896
AM
9967#ifdef DEBUG386
9968 if (flag_debug)
9969 {
7b81dfbb 9970 pi ("" /*line*/, &i);
29b0f896
AM
9971 }
9972#endif /* DEBUG386 */
9973}
252b5132 9974
e205caa7
L
9975/* Return the size of the displacement operand N. */
9976
9977static int
9978disp_size (unsigned int n)
9979{
9980 int size = 4;
43234a1e 9981
b5014f7a 9982 if (i.types[n].bitfield.disp64)
40fb9820
L
9983 size = 8;
9984 else if (i.types[n].bitfield.disp8)
9985 size = 1;
9986 else if (i.types[n].bitfield.disp16)
9987 size = 2;
e205caa7
L
9988 return size;
9989}
9990
9991/* Return the size of the immediate operand N. */
9992
9993static int
9994imm_size (unsigned int n)
9995{
9996 int size = 4;
40fb9820
L
9997 if (i.types[n].bitfield.imm64)
9998 size = 8;
9999 else if (i.types[n].bitfield.imm8 || i.types[n].bitfield.imm8s)
10000 size = 1;
10001 else if (i.types[n].bitfield.imm16)
10002 size = 2;
e205caa7
L
10003 return size;
10004}
10005
29b0f896 10006static void
64e74474 10007output_disp (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
10008{
10009 char *p;
10010 unsigned int n;
252b5132 10011
29b0f896
AM
10012 for (n = 0; n < i.operands; n++)
10013 {
b5014f7a 10014 if (operand_type_check (i.types[n], disp))
29b0f896 10015 {
48ef937e
JB
10016 int size = disp_size (n);
10017
10018 if (now_seg == absolute_section)
10019 abs_section_offset += size;
10020 else if (i.op[n].disps->X_op == O_constant)
29b0f896 10021 {
43234a1e 10022 offsetT val = i.op[n].disps->X_add_number;
252b5132 10023
629cfaf1
JB
10024 val = offset_in_range (val >> (size == 1 ? i.memshift : 0),
10025 size);
29b0f896
AM
10026 p = frag_more (size);
10027 md_number_to_chars (p, val, size);
10028 }
10029 else
10030 {
f86103b7 10031 enum bfd_reloc_code_real reloc_type;
40fb9820 10032 int sign = i.types[n].bitfield.disp32s;
29b0f896 10033 int pcrel = (i.flags[n] & Operand_PCrel) != 0;
02a86693 10034 fixS *fixP;
29b0f896 10035
e205caa7 10036 /* We can't have 8 bit displacement here. */
9c2799c2 10037 gas_assert (!i.types[n].bitfield.disp8);
e205caa7 10038
29b0f896
AM
10039 /* The PC relative address is computed relative
10040 to the instruction boundary, so in case immediate
10041 fields follows, we need to adjust the value. */
10042 if (pcrel && i.imm_operands)
10043 {
29b0f896 10044 unsigned int n1;
e205caa7 10045 int sz = 0;
252b5132 10046
29b0f896 10047 for (n1 = 0; n1 < i.operands; n1++)
40fb9820 10048 if (operand_type_check (i.types[n1], imm))
252b5132 10049 {
e205caa7
L
10050 /* Only one immediate is allowed for PC
10051 relative address. */
9c2799c2 10052 gas_assert (sz == 0);
e205caa7
L
10053 sz = imm_size (n1);
10054 i.op[n].disps->X_add_number -= sz;
252b5132 10055 }
29b0f896 10056 /* We should find the immediate. */
9c2799c2 10057 gas_assert (sz != 0);
29b0f896 10058 }
520dc8e8 10059
29b0f896 10060 p = frag_more (size);
d258b828 10061 reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
d6ab8113 10062 if (GOT_symbol
2bbd9c25 10063 && GOT_symbol == i.op[n].disps->X_add_symbol
d6ab8113 10064 && (((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
10065 || reloc_type == BFD_RELOC_X86_64_32S
10066 || (reloc_type == BFD_RELOC_64
10067 && object_64bit))
d6ab8113
JB
10068 && (i.op[n].disps->X_op == O_symbol
10069 || (i.op[n].disps->X_op == O_add
10070 && ((symbol_get_value_expression
10071 (i.op[n].disps->X_op_symbol)->X_op)
10072 == O_subtract))))
10073 || reloc_type == BFD_RELOC_32_PCREL))
2bbd9c25 10074 {
4fa24527 10075 if (!object_64bit)
7b81dfbb
AJ
10076 {
10077 reloc_type = BFD_RELOC_386_GOTPC;
5b7c81bd 10078 i.has_gotpc_tls_reloc = true;
98da05bf 10079 i.op[n].disps->X_add_number +=
d583596c 10080 encoding_length (insn_start_frag, insn_start_off, p);
7b81dfbb
AJ
10081 }
10082 else if (reloc_type == BFD_RELOC_64)
10083 reloc_type = BFD_RELOC_X86_64_GOTPC64;
d6ab8113 10084 else
7b81dfbb
AJ
10085 /* Don't do the adjustment for x86-64, as there
10086 the pcrel addressing is relative to the _next_
10087 insn, and that is taken care of in other code. */
d6ab8113 10088 reloc_type = BFD_RELOC_X86_64_GOTPC32;
2bbd9c25 10089 }
e379e5f3
L
10090 else if (align_branch_power)
10091 {
10092 switch (reloc_type)
10093 {
10094 case BFD_RELOC_386_TLS_GD:
10095 case BFD_RELOC_386_TLS_LDM:
10096 case BFD_RELOC_386_TLS_IE:
10097 case BFD_RELOC_386_TLS_IE_32:
10098 case BFD_RELOC_386_TLS_GOTIE:
10099 case BFD_RELOC_386_TLS_GOTDESC:
10100 case BFD_RELOC_386_TLS_DESC_CALL:
10101 case BFD_RELOC_X86_64_TLSGD:
10102 case BFD_RELOC_X86_64_TLSLD:
10103 case BFD_RELOC_X86_64_GOTTPOFF:
10104 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
10105 case BFD_RELOC_X86_64_TLSDESC_CALL:
5b7c81bd 10106 i.has_gotpc_tls_reloc = true;
e379e5f3
L
10107 default:
10108 break;
10109 }
10110 }
02a86693
L
10111 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal,
10112 size, i.op[n].disps, pcrel,
10113 reloc_type);
eb19308f
JB
10114
10115 if (flag_code == CODE_64BIT && size == 4 && pcrel
10116 && !i.prefix[ADDR_PREFIX])
10117 fixP->fx_signed = 1;
10118
02a86693
L
10119 /* Check for "call/jmp *mem", "mov mem, %reg",
10120 "test %reg, mem" and "binop mem, %reg" where binop
10121 is one of adc, add, and, cmp, or, sbb, sub, xor
e60f4d3b
L
10122 instructions without data prefix. Always generate
10123 R_386_GOT32X for "sym*GOT" operand in 32-bit mode. */
10124 if (i.prefix[DATA_PREFIX] == 0
10125 && (generate_relax_relocations
10126 || (!object_64bit
10127 && i.rm.mode == 0
10128 && i.rm.regmem == 5))
0cb4071e
L
10129 && (i.rm.mode == 2
10130 || (i.rm.mode == 0 && i.rm.regmem == 5))
389d00a5 10131 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
02a86693
L
10132 && ((i.operands == 1
10133 && i.tm.base_opcode == 0xff
10134 && (i.rm.reg == 2 || i.rm.reg == 4))
10135 || (i.operands == 2
10136 && (i.tm.base_opcode == 0x8b
10137 || i.tm.base_opcode == 0x85
2ae4c703 10138 || (i.tm.base_opcode & ~0x38) == 0x03))))
02a86693
L
10139 {
10140 if (object_64bit)
10141 {
10142 fixP->fx_tcbit = i.rex != 0;
10143 if (i.base_reg
e968fc9b 10144 && (i.base_reg->reg_num == RegIP))
02a86693
L
10145 fixP->fx_tcbit2 = 1;
10146 }
10147 else
10148 fixP->fx_tcbit2 = 1;
10149 }
29b0f896
AM
10150 }
10151 }
10152 }
10153}
252b5132 10154
29b0f896 10155static void
64e74474 10156output_imm (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
10157{
10158 char *p;
10159 unsigned int n;
252b5132 10160
29b0f896
AM
10161 for (n = 0; n < i.operands; n++)
10162 {
43234a1e 10163 /* Skip SAE/RC Imm operand in EVEX. They are already handled. */
ca5312a2 10164 if (i.rounding.type != rc_none && n == i.rounding.operand)
43234a1e
L
10165 continue;
10166
40fb9820 10167 if (operand_type_check (i.types[n], imm))
29b0f896 10168 {
48ef937e
JB
10169 int size = imm_size (n);
10170
10171 if (now_seg == absolute_section)
10172 abs_section_offset += size;
10173 else if (i.op[n].imms->X_op == O_constant)
29b0f896 10174 {
29b0f896 10175 offsetT val;
b4cac588 10176
29b0f896
AM
10177 val = offset_in_range (i.op[n].imms->X_add_number,
10178 size);
10179 p = frag_more (size);
10180 md_number_to_chars (p, val, size);
10181 }
10182 else
10183 {
10184 /* Not absolute_section.
10185 Need a 32-bit fixup (don't support 8bit
10186 non-absolute imms). Try to support other
10187 sizes ... */
f86103b7 10188 enum bfd_reloc_code_real reloc_type;
e205caa7 10189 int sign;
29b0f896 10190
40fb9820 10191 if (i.types[n].bitfield.imm32s
a7d61044 10192 && (i.suffix == QWORD_MNEM_SUFFIX
40fb9820 10193 || (!i.suffix && i.tm.opcode_modifier.no_lsuf)))
29b0f896 10194 sign = 1;
e205caa7
L
10195 else
10196 sign = 0;
520dc8e8 10197
29b0f896 10198 p = frag_more (size);
d258b828 10199 reloc_type = reloc (size, 0, sign, i.reloc[n]);
f86103b7 10200
2bbd9c25
JJ
10201 /* This is tough to explain. We end up with this one if we
10202 * have operands that look like
10203 * "_GLOBAL_OFFSET_TABLE_+[.-.L284]". The goal here is to
10204 * obtain the absolute address of the GOT, and it is strongly
10205 * preferable from a performance point of view to avoid using
10206 * a runtime relocation for this. The actual sequence of
10207 * instructions often look something like:
10208 *
10209 * call .L66
10210 * .L66:
10211 * popl %ebx
10212 * addl $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
10213 *
10214 * The call and pop essentially return the absolute address
10215 * of the label .L66 and store it in %ebx. The linker itself
10216 * will ultimately change the first operand of the addl so
10217 * that %ebx points to the GOT, but to keep things simple, the
10218 * .o file must have this operand set so that it generates not
10219 * the absolute address of .L66, but the absolute address of
10220 * itself. This allows the linker itself simply treat a GOTPC
10221 * relocation as asking for a pcrel offset to the GOT to be
10222 * added in, and the addend of the relocation is stored in the
10223 * operand field for the instruction itself.
10224 *
10225 * Our job here is to fix the operand so that it would add
10226 * the correct offset so that %ebx would point to itself. The
10227 * thing that is tricky is that .-.L66 will point to the
10228 * beginning of the instruction, so we need to further modify
10229 * the operand so that it will point to itself. There are
10230 * other cases where you have something like:
10231 *
10232 * .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
10233 *
10234 * and here no correction would be required. Internally in
10235 * the assembler we treat operands of this form as not being
10236 * pcrel since the '.' is explicitly mentioned, and I wonder
10237 * whether it would simplify matters to do it this way. Who
10238 * knows. In earlier versions of the PIC patches, the
10239 * pcrel_adjust field was used to store the correction, but
10240 * since the expression is not pcrel, I felt it would be
10241 * confusing to do it this way. */
10242
d6ab8113 10243 if ((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
10244 || reloc_type == BFD_RELOC_X86_64_32S
10245 || reloc_type == BFD_RELOC_64)
29b0f896
AM
10246 && GOT_symbol
10247 && GOT_symbol == i.op[n].imms->X_add_symbol
10248 && (i.op[n].imms->X_op == O_symbol
10249 || (i.op[n].imms->X_op == O_add
10250 && ((symbol_get_value_expression
10251 (i.op[n].imms->X_op_symbol)->X_op)
10252 == O_subtract))))
10253 {
4fa24527 10254 if (!object_64bit)
d6ab8113 10255 reloc_type = BFD_RELOC_386_GOTPC;
7b81dfbb 10256 else if (size == 4)
d6ab8113 10257 reloc_type = BFD_RELOC_X86_64_GOTPC32;
7b81dfbb
AJ
10258 else if (size == 8)
10259 reloc_type = BFD_RELOC_X86_64_GOTPC64;
5b7c81bd 10260 i.has_gotpc_tls_reloc = true;
d583596c
JB
10261 i.op[n].imms->X_add_number +=
10262 encoding_length (insn_start_frag, insn_start_off, p);
29b0f896 10263 }
29b0f896
AM
10264 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
10265 i.op[n].imms, 0, reloc_type);
10266 }
10267 }
10268 }
252b5132
RH
10269}
10270\f
d182319b
JB
10271/* x86_cons_fix_new is called via the expression parsing code when a
10272 reloc is needed. We use this hook to get the correct .got reloc. */
d182319b
JB
10273static int cons_sign = -1;
10274
10275void
e3bb37b5 10276x86_cons_fix_new (fragS *frag, unsigned int off, unsigned int len,
62ebcb5c 10277 expressionS *exp, bfd_reloc_code_real_type r)
d182319b 10278{
d258b828 10279 r = reloc (len, 0, cons_sign, r);
d182319b
JB
10280
10281#ifdef TE_PE
10282 if (exp->X_op == O_secrel)
10283 {
10284 exp->X_op = O_symbol;
10285 r = BFD_RELOC_32_SECREL;
10286 }
10287#endif
10288
10289 fix_new_exp (frag, off, len, exp, 0, r);
10290}
10291
357d1bd8
L
10292/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
10293 purpose of the `.dc.a' internal pseudo-op. */
10294
10295int
10296x86_address_bytes (void)
10297{
10298 if ((stdoutput->arch_info->mach & bfd_mach_x64_32))
10299 return 4;
10300 return stdoutput->arch_info->bits_per_address / 8;
10301}
10302
deea4973
JB
10303#if (!(defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined (OBJ_MACH_O)) \
10304 || defined (LEX_AT)) && !defined (TE_PE)
d258b828 10305# define lex_got(reloc, adjust, types) NULL
718ddfc0 10306#else
f3c180ae
AM
10307/* Parse operands of the form
10308 <symbol>@GOTOFF+<nnn>
10309 and similar .plt or .got references.
10310
10311 If we find one, set up the correct relocation in RELOC and copy the
10312 input string, minus the `@GOTOFF' into a malloc'd buffer for
10313 parsing by the calling routine. Return this buffer, and if ADJUST
10314 is non-null set it to the length of the string we removed from the
10315 input line. Otherwise return NULL. */
10316static char *
91d6fa6a 10317lex_got (enum bfd_reloc_code_real *rel,
64e74474 10318 int *adjust,
d258b828 10319 i386_operand_type *types)
f3c180ae 10320{
7b81dfbb
AJ
10321 /* Some of the relocations depend on the size of what field is to
10322 be relocated. But in our callers i386_immediate and i386_displacement
10323 we don't yet know the operand size (this will be set by insn
10324 matching). Hence we record the word32 relocation here,
10325 and adjust the reloc according to the real size in reloc(). */
f3c180ae
AM
10326 static const struct {
10327 const char *str;
cff8d58a 10328 int len;
4fa24527 10329 const enum bfd_reloc_code_real rel[2];
40fb9820 10330 const i386_operand_type types64;
5b7c81bd 10331 bool need_GOT_symbol;
f3c180ae 10332 } gotrel[] = {
deea4973 10333#ifndef TE_PE
8ce3d284 10334#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
10335 { STRING_COMMA_LEN ("SIZE"), { BFD_RELOC_SIZE32,
10336 BFD_RELOC_SIZE32 },
5b7c81bd 10337 OPERAND_TYPE_IMM32_64, false },
8ce3d284 10338#endif
cff8d58a
L
10339 { STRING_COMMA_LEN ("PLTOFF"), { _dummy_first_bfd_reloc_code_real,
10340 BFD_RELOC_X86_64_PLTOFF64 },
5b7c81bd 10341 OPERAND_TYPE_IMM64, true },
cff8d58a
L
10342 { STRING_COMMA_LEN ("PLT"), { BFD_RELOC_386_PLT32,
10343 BFD_RELOC_X86_64_PLT32 },
5b7c81bd 10344 OPERAND_TYPE_IMM32_32S_DISP32, false },
cff8d58a
L
10345 { STRING_COMMA_LEN ("GOTPLT"), { _dummy_first_bfd_reloc_code_real,
10346 BFD_RELOC_X86_64_GOTPLT64 },
5b7c81bd 10347 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
10348 { STRING_COMMA_LEN ("GOTOFF"), { BFD_RELOC_386_GOTOFF,
10349 BFD_RELOC_X86_64_GOTOFF64 },
5b7c81bd 10350 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
10351 { STRING_COMMA_LEN ("GOTPCREL"), { _dummy_first_bfd_reloc_code_real,
10352 BFD_RELOC_X86_64_GOTPCREL },
5b7c81bd 10353 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10354 { STRING_COMMA_LEN ("TLSGD"), { BFD_RELOC_386_TLS_GD,
10355 BFD_RELOC_X86_64_TLSGD },
5b7c81bd 10356 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10357 { STRING_COMMA_LEN ("TLSLDM"), { BFD_RELOC_386_TLS_LDM,
10358 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10359 OPERAND_TYPE_NONE, true },
cff8d58a
L
10360 { STRING_COMMA_LEN ("TLSLD"), { _dummy_first_bfd_reloc_code_real,
10361 BFD_RELOC_X86_64_TLSLD },
5b7c81bd 10362 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10363 { STRING_COMMA_LEN ("GOTTPOFF"), { BFD_RELOC_386_TLS_IE_32,
10364 BFD_RELOC_X86_64_GOTTPOFF },
5b7c81bd 10365 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10366 { STRING_COMMA_LEN ("TPOFF"), { BFD_RELOC_386_TLS_LE_32,
10367 BFD_RELOC_X86_64_TPOFF32 },
5b7c81bd 10368 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
10369 { STRING_COMMA_LEN ("NTPOFF"), { BFD_RELOC_386_TLS_LE,
10370 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10371 OPERAND_TYPE_NONE, true },
cff8d58a
L
10372 { STRING_COMMA_LEN ("DTPOFF"), { BFD_RELOC_386_TLS_LDO_32,
10373 BFD_RELOC_X86_64_DTPOFF32 },
5b7c81bd 10374 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
10375 { STRING_COMMA_LEN ("GOTNTPOFF"),{ BFD_RELOC_386_TLS_GOTIE,
10376 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10377 OPERAND_TYPE_NONE, true },
cff8d58a
L
10378 { STRING_COMMA_LEN ("INDNTPOFF"),{ BFD_RELOC_386_TLS_IE,
10379 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10380 OPERAND_TYPE_NONE, true },
cff8d58a
L
10381 { STRING_COMMA_LEN ("GOT"), { BFD_RELOC_386_GOT32,
10382 BFD_RELOC_X86_64_GOT32 },
5b7c81bd 10383 OPERAND_TYPE_IMM32_32S_64_DISP32, true },
cff8d58a
L
10384 { STRING_COMMA_LEN ("TLSDESC"), { BFD_RELOC_386_TLS_GOTDESC,
10385 BFD_RELOC_X86_64_GOTPC32_TLSDESC },
5b7c81bd 10386 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10387 { STRING_COMMA_LEN ("TLSCALL"), { BFD_RELOC_386_TLS_DESC_CALL,
10388 BFD_RELOC_X86_64_TLSDESC_CALL },
5b7c81bd 10389 OPERAND_TYPE_IMM32_32S_DISP32, true },
deea4973
JB
10390#else /* TE_PE */
10391 { STRING_COMMA_LEN ("SECREL32"), { BFD_RELOC_32_SECREL,
10392 BFD_RELOC_32_SECREL },
10393 OPERAND_TYPE_IMM32_32S_64_DISP32_64, false },
10394#endif
f3c180ae
AM
10395 };
10396 char *cp;
10397 unsigned int j;
10398
deea4973 10399#if defined (OBJ_MAYBE_ELF) && !defined (TE_PE)
718ddfc0
JB
10400 if (!IS_ELF)
10401 return NULL;
d382c579 10402#endif
718ddfc0 10403
f3c180ae 10404 for (cp = input_line_pointer; *cp != '@'; cp++)
67c11a9b 10405 if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
f3c180ae
AM
10406 return NULL;
10407
47465058 10408 for (j = 0; j < ARRAY_SIZE (gotrel); j++)
f3c180ae 10409 {
cff8d58a 10410 int len = gotrel[j].len;
28f81592 10411 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
f3c180ae 10412 {
4fa24527 10413 if (gotrel[j].rel[object_64bit] != 0)
f3c180ae 10414 {
28f81592
AM
10415 int first, second;
10416 char *tmpbuf, *past_reloc;
f3c180ae 10417
91d6fa6a 10418 *rel = gotrel[j].rel[object_64bit];
f3c180ae 10419
3956db08
JB
10420 if (types)
10421 {
10422 if (flag_code != CODE_64BIT)
40fb9820
L
10423 {
10424 types->bitfield.imm32 = 1;
10425 types->bitfield.disp32 = 1;
10426 }
3956db08
JB
10427 else
10428 *types = gotrel[j].types64;
10429 }
10430
844bf810 10431 if (gotrel[j].need_GOT_symbol && GOT_symbol == NULL)
f3c180ae
AM
10432 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
10433
28f81592 10434 /* The length of the first part of our input line. */
f3c180ae 10435 first = cp - input_line_pointer;
28f81592
AM
10436
10437 /* The second part goes from after the reloc token until
67c11a9b 10438 (and including) an end_of_line char or comma. */
28f81592 10439 past_reloc = cp + 1 + len;
67c11a9b
AM
10440 cp = past_reloc;
10441 while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
10442 ++cp;
10443 second = cp + 1 - past_reloc;
28f81592
AM
10444
10445 /* Allocate and copy string. The trailing NUL shouldn't
10446 be necessary, but be safe. */
add39d23 10447 tmpbuf = XNEWVEC (char, first + second + 2);
f3c180ae 10448 memcpy (tmpbuf, input_line_pointer, first);
0787a12d
AM
10449 if (second != 0 && *past_reloc != ' ')
10450 /* Replace the relocation token with ' ', so that
10451 errors like foo@GOTOFF1 will be detected. */
10452 tmpbuf[first++] = ' ';
af89796a
L
10453 else
10454 /* Increment length by 1 if the relocation token is
10455 removed. */
10456 len++;
10457 if (adjust)
10458 *adjust = len;
0787a12d
AM
10459 memcpy (tmpbuf + first, past_reloc, second);
10460 tmpbuf[first + second] = '\0';
f3c180ae
AM
10461 return tmpbuf;
10462 }
10463
4fa24527
JB
10464 as_bad (_("@%s reloc is not supported with %d-bit output format"),
10465 gotrel[j].str, 1 << (5 + object_64bit));
f3c180ae
AM
10466 return NULL;
10467 }
10468 }
10469
10470 /* Might be a symbol version string. Don't as_bad here. */
10471 return NULL;
10472}
4e4f7c87 10473#endif
f3c180ae 10474
62ebcb5c 10475bfd_reloc_code_real_type
e3bb37b5 10476x86_cons (expressionS *exp, int size)
f3c180ae 10477{
62ebcb5c
AM
10478 bfd_reloc_code_real_type got_reloc = NO_RELOC;
10479
2748c1b1
L
10480#if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
10481 && !defined (LEX_AT)) \
10482 || defined (TE_PE)
ee86248c
JB
10483 intel_syntax = -intel_syntax;
10484
3c7b9c2c 10485 exp->X_md = 0;
4fa24527 10486 if (size == 4 || (object_64bit && size == 8))
f3c180ae
AM
10487 {
10488 /* Handle @GOTOFF and the like in an expression. */
10489 char *save;
10490 char *gotfree_input_line;
4a57f2cf 10491 int adjust = 0;
f3c180ae
AM
10492
10493 save = input_line_pointer;
d258b828 10494 gotfree_input_line = lex_got (&got_reloc, &adjust, NULL);
f3c180ae
AM
10495 if (gotfree_input_line)
10496 input_line_pointer = gotfree_input_line;
10497
10498 expression (exp);
10499
10500 if (gotfree_input_line)
10501 {
10502 /* expression () has merrily parsed up to the end of line,
10503 or a comma - in the wrong buffer. Transfer how far
10504 input_line_pointer has moved to the right buffer. */
10505 input_line_pointer = (save
10506 + (input_line_pointer - gotfree_input_line)
10507 + adjust);
10508 free (gotfree_input_line);
3992d3b7
AM
10509 if (exp->X_op == O_constant
10510 || exp->X_op == O_absent
10511 || exp->X_op == O_illegal
0398aac5 10512 || exp->X_op == O_register
3992d3b7
AM
10513 || exp->X_op == O_big)
10514 {
10515 char c = *input_line_pointer;
10516 *input_line_pointer = 0;
10517 as_bad (_("missing or invalid expression `%s'"), save);
10518 *input_line_pointer = c;
10519 }
b9519cfe
L
10520 else if ((got_reloc == BFD_RELOC_386_PLT32
10521 || got_reloc == BFD_RELOC_X86_64_PLT32)
10522 && exp->X_op != O_symbol)
10523 {
10524 char c = *input_line_pointer;
10525 *input_line_pointer = 0;
10526 as_bad (_("invalid PLT expression `%s'"), save);
10527 *input_line_pointer = c;
10528 }
f3c180ae
AM
10529 }
10530 }
10531 else
10532 expression (exp);
ee86248c
JB
10533
10534 intel_syntax = -intel_syntax;
10535
10536 if (intel_syntax)
10537 i386_intel_simplify (exp);
2748c1b1
L
10538#else
10539 expression (exp);
10540#endif
62ebcb5c 10541
a442cac5
JB
10542 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
10543 if (size == 4 && exp->X_op == O_constant && !object_64bit)
10544 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
10545
62ebcb5c 10546 return got_reloc;
f3c180ae 10547}
f3c180ae 10548
9f32dd5b
L
10549static void
10550signed_cons (int size)
6482c264 10551{
a442cac5 10552 if (object_64bit)
d182319b
JB
10553 cons_sign = 1;
10554 cons (size);
10555 cons_sign = -1;
6482c264
NC
10556}
10557
d182319b 10558#ifdef TE_PE
6482c264 10559static void
7016a5d5 10560pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
6482c264
NC
10561{
10562 expressionS exp;
10563
10564 do
10565 {
10566 expression (&exp);
10567 if (exp.X_op == O_symbol)
10568 exp.X_op = O_secrel;
10569
10570 emit_expr (&exp, 4);
10571 }
10572 while (*input_line_pointer++ == ',');
10573
10574 input_line_pointer--;
10575 demand_empty_rest_of_line ();
10576}
6482c264
NC
10577#endif
10578
43234a1e
L
10579/* Handle Vector operations. */
10580
10581static char *
f70c6814 10582check_VecOperations (char *op_string)
43234a1e
L
10583{
10584 const reg_entry *mask;
10585 const char *saved;
10586 char *end_op;
10587
f70c6814 10588 while (*op_string)
43234a1e
L
10589 {
10590 saved = op_string;
10591 if (*op_string == '{')
10592 {
10593 op_string++;
10594
10595 /* Check broadcasts. */
d34049e8 10596 if (startswith (op_string, "1to"))
43234a1e 10597 {
5273a3cd 10598 unsigned int bcst_type;
43234a1e 10599
5273a3cd 10600 if (i.broadcast.type)
43234a1e
L
10601 goto duplicated_vec_op;
10602
10603 op_string += 3;
10604 if (*op_string == '8')
8e6e0792 10605 bcst_type = 8;
b28d1bda 10606 else if (*op_string == '4')
8e6e0792 10607 bcst_type = 4;
b28d1bda 10608 else if (*op_string == '2')
8e6e0792 10609 bcst_type = 2;
43234a1e
L
10610 else if (*op_string == '1'
10611 && *(op_string+1) == '6')
10612 {
8e6e0792 10613 bcst_type = 16;
43234a1e
L
10614 op_string++;
10615 }
0cc78721
CL
10616 else if (*op_string == '3'
10617 && *(op_string+1) == '2')
10618 {
10619 bcst_type = 32;
10620 op_string++;
10621 }
43234a1e
L
10622 else
10623 {
10624 as_bad (_("Unsupported broadcast: `%s'"), saved);
10625 return NULL;
10626 }
10627 op_string++;
10628
5273a3cd
JB
10629 i.broadcast.type = bcst_type;
10630 i.broadcast.operand = this_operand;
43234a1e
L
10631 }
10632 /* Check masking operation. */
10633 else if ((mask = parse_register (op_string, &end_op)) != NULL)
10634 {
8a6fb3f9
JB
10635 if (mask == &bad_reg)
10636 return NULL;
10637
43234a1e 10638 /* k0 can't be used for write mask. */
f74a6307 10639 if (mask->reg_type.bitfield.class != RegMask || !mask->reg_num)
43234a1e 10640 {
6d2cd6b2
JB
10641 as_bad (_("`%s%s' can't be used for write mask"),
10642 register_prefix, mask->reg_name);
43234a1e
L
10643 return NULL;
10644 }
10645
6225c532 10646 if (!i.mask.reg)
43234a1e 10647 {
6225c532
JB
10648 i.mask.reg = mask;
10649 i.mask.operand = this_operand;
43234a1e 10650 }
6225c532
JB
10651 else if (i.mask.reg->reg_num)
10652 goto duplicated_vec_op;
43234a1e
L
10653 else
10654 {
6225c532 10655 i.mask.reg = mask;
43234a1e
L
10656
10657 /* Only "{z}" is allowed here. No need to check
10658 zeroing mask explicitly. */
6225c532 10659 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
10660 {
10661 as_bad (_("invalid write mask `%s'"), saved);
10662 return NULL;
10663 }
10664 }
10665
10666 op_string = end_op;
10667 }
10668 /* Check zeroing-flag for masking operation. */
10669 else if (*op_string == 'z')
10670 {
6225c532 10671 if (!i.mask.reg)
43234a1e 10672 {
6225c532
JB
10673 i.mask.reg = reg_k0;
10674 i.mask.zeroing = 1;
10675 i.mask.operand = this_operand;
43234a1e
L
10676 }
10677 else
10678 {
6225c532 10679 if (i.mask.zeroing)
43234a1e
L
10680 {
10681 duplicated_vec_op:
10682 as_bad (_("duplicated `%s'"), saved);
10683 return NULL;
10684 }
10685
6225c532 10686 i.mask.zeroing = 1;
43234a1e
L
10687
10688 /* Only "{%k}" is allowed here. No need to check mask
10689 register explicitly. */
6225c532 10690 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
10691 {
10692 as_bad (_("invalid zeroing-masking `%s'"),
10693 saved);
10694 return NULL;
10695 }
10696 }
10697
10698 op_string++;
10699 }
10700 else
10701 goto unknown_vec_op;
10702
10703 if (*op_string != '}')
10704 {
10705 as_bad (_("missing `}' in `%s'"), saved);
10706 return NULL;
10707 }
10708 op_string++;
0ba3a731
L
10709
10710 /* Strip whitespace since the addition of pseudo prefixes
10711 changed how the scrubber treats '{'. */
10712 if (is_space_char (*op_string))
10713 ++op_string;
10714
43234a1e
L
10715 continue;
10716 }
10717 unknown_vec_op:
10718 /* We don't know this one. */
10719 as_bad (_("unknown vector operation: `%s'"), saved);
10720 return NULL;
10721 }
10722
6225c532 10723 if (i.mask.reg && i.mask.zeroing && !i.mask.reg->reg_num)
6d2cd6b2
JB
10724 {
10725 as_bad (_("zeroing-masking only allowed with write mask"));
10726 return NULL;
10727 }
10728
43234a1e
L
10729 return op_string;
10730}
10731
252b5132 10732static int
70e41ade 10733i386_immediate (char *imm_start)
252b5132
RH
10734{
10735 char *save_input_line_pointer;
f3c180ae 10736 char *gotfree_input_line;
252b5132 10737 segT exp_seg = 0;
47926f60 10738 expressionS *exp;
40fb9820
L
10739 i386_operand_type types;
10740
0dfbf9d7 10741 operand_type_set (&types, ~0);
252b5132
RH
10742
10743 if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
10744 {
31b2323c
L
10745 as_bad (_("at most %d immediate operands are allowed"),
10746 MAX_IMMEDIATE_OPERANDS);
252b5132
RH
10747 return 0;
10748 }
10749
10750 exp = &im_expressions[i.imm_operands++];
520dc8e8 10751 i.op[this_operand].imms = exp;
252b5132
RH
10752
10753 if (is_space_char (*imm_start))
10754 ++imm_start;
10755
10756 save_input_line_pointer = input_line_pointer;
10757 input_line_pointer = imm_start;
10758
d258b828 10759 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
10760 if (gotfree_input_line)
10761 input_line_pointer = gotfree_input_line;
252b5132
RH
10762
10763 exp_seg = expression (exp);
10764
83183c0c 10765 SKIP_WHITESPACE ();
252b5132 10766 if (*input_line_pointer)
f3c180ae 10767 as_bad (_("junk `%s' after expression"), input_line_pointer);
252b5132
RH
10768
10769 input_line_pointer = save_input_line_pointer;
f3c180ae 10770 if (gotfree_input_line)
ee86248c
JB
10771 {
10772 free (gotfree_input_line);
10773
9aac24b1 10774 if (exp->X_op == O_constant)
ee86248c
JB
10775 exp->X_op = O_illegal;
10776 }
10777
9aac24b1
JB
10778 if (exp_seg == reg_section)
10779 {
10780 as_bad (_("illegal immediate register operand %s"), imm_start);
10781 return 0;
10782 }
10783
ee86248c
JB
10784 return i386_finalize_immediate (exp_seg, exp, types, imm_start);
10785}
252b5132 10786
ee86248c
JB
10787static int
10788i386_finalize_immediate (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
10789 i386_operand_type types, const char *imm_start)
10790{
10791 if (exp->X_op == O_absent || exp->X_op == O_illegal || exp->X_op == O_big)
252b5132 10792 {
313c53d1
L
10793 if (imm_start)
10794 as_bad (_("missing or invalid immediate expression `%s'"),
10795 imm_start);
3992d3b7 10796 return 0;
252b5132 10797 }
3e73aa7c 10798 else if (exp->X_op == O_constant)
252b5132 10799 {
47926f60 10800 /* Size it properly later. */
40fb9820 10801 i.types[this_operand].bitfield.imm64 = 1;
a442cac5
JB
10802
10803 /* If not 64bit, sign/zero extend val, to account for wraparound
10804 when !BFD64. */
10805 if (flag_code != CODE_64BIT)
10806 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
252b5132 10807 }
4c63da97 10808#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
f86103b7 10809 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
31312f95 10810 && exp_seg != absolute_section
47926f60 10811 && exp_seg != text_section
24eab124
AM
10812 && exp_seg != data_section
10813 && exp_seg != bss_section
10814 && exp_seg != undefined_section
f86103b7 10815 && !bfd_is_com_section (exp_seg))
252b5132 10816 {
d0b47220 10817 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
252b5132
RH
10818 return 0;
10819 }
10820#endif
10821 else
10822 {
10823 /* This is an address. The size of the address will be
24eab124 10824 determined later, depending on destination register,
3e73aa7c 10825 suffix, or the default for the section. */
40fb9820
L
10826 i.types[this_operand].bitfield.imm8 = 1;
10827 i.types[this_operand].bitfield.imm16 = 1;
10828 i.types[this_operand].bitfield.imm32 = 1;
10829 i.types[this_operand].bitfield.imm32s = 1;
10830 i.types[this_operand].bitfield.imm64 = 1;
c6fb90c8
L
10831 i.types[this_operand] = operand_type_and (i.types[this_operand],
10832 types);
252b5132
RH
10833 }
10834
10835 return 1;
10836}
10837
551c1ca1 10838static char *
e3bb37b5 10839i386_scale (char *scale)
252b5132 10840{
551c1ca1
AM
10841 offsetT val;
10842 char *save = input_line_pointer;
252b5132 10843
551c1ca1
AM
10844 input_line_pointer = scale;
10845 val = get_absolute_expression ();
10846
10847 switch (val)
252b5132 10848 {
551c1ca1 10849 case 1:
252b5132
RH
10850 i.log2_scale_factor = 0;
10851 break;
551c1ca1 10852 case 2:
252b5132
RH
10853 i.log2_scale_factor = 1;
10854 break;
551c1ca1 10855 case 4:
252b5132
RH
10856 i.log2_scale_factor = 2;
10857 break;
551c1ca1 10858 case 8:
252b5132
RH
10859 i.log2_scale_factor = 3;
10860 break;
10861 default:
a724f0f4
JB
10862 {
10863 char sep = *input_line_pointer;
10864
10865 *input_line_pointer = '\0';
10866 as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
10867 scale);
10868 *input_line_pointer = sep;
10869 input_line_pointer = save;
10870 return NULL;
10871 }
252b5132 10872 }
29b0f896 10873 if (i.log2_scale_factor != 0 && i.index_reg == 0)
252b5132
RH
10874 {
10875 as_warn (_("scale factor of %d without an index register"),
24eab124 10876 1 << i.log2_scale_factor);
252b5132 10877 i.log2_scale_factor = 0;
252b5132 10878 }
551c1ca1
AM
10879 scale = input_line_pointer;
10880 input_line_pointer = save;
10881 return scale;
252b5132
RH
10882}
10883
252b5132 10884static int
e3bb37b5 10885i386_displacement (char *disp_start, char *disp_end)
252b5132 10886{
29b0f896 10887 expressionS *exp;
252b5132
RH
10888 segT exp_seg = 0;
10889 char *save_input_line_pointer;
f3c180ae 10890 char *gotfree_input_line;
40fb9820
L
10891 int override;
10892 i386_operand_type bigdisp, types = anydisp;
3992d3b7 10893 int ret;
252b5132 10894
31b2323c
L
10895 if (i.disp_operands == MAX_MEMORY_OPERANDS)
10896 {
10897 as_bad (_("at most %d displacement operands are allowed"),
10898 MAX_MEMORY_OPERANDS);
10899 return 0;
10900 }
10901
0dfbf9d7 10902 operand_type_set (&bigdisp, 0);
6f2f06be 10903 if (i.jumpabsolute
48bcea9f 10904 || i.types[this_operand].bitfield.baseindex
0cfa3eb3
JB
10905 || (current_templates->start->opcode_modifier.jump != JUMP
10906 && current_templates->start->opcode_modifier.jump != JUMP_DWORD))
e05278af 10907 {
48bcea9f 10908 i386_addressing_mode ();
e05278af 10909 override = (i.prefix[ADDR_PREFIX] != 0);
40fb9820
L
10910 if (flag_code == CODE_64BIT)
10911 {
10912 if (!override)
10913 {
10914 bigdisp.bitfield.disp32s = 1;
10915 bigdisp.bitfield.disp64 = 1;
10916 }
48bcea9f
JB
10917 else
10918 bigdisp.bitfield.disp32 = 1;
40fb9820
L
10919 }
10920 else if ((flag_code == CODE_16BIT) ^ override)
40fb9820 10921 bigdisp.bitfield.disp16 = 1;
48bcea9f
JB
10922 else
10923 bigdisp.bitfield.disp32 = 1;
e05278af
JB
10924 }
10925 else
10926 {
376cd056
JB
10927 /* For PC-relative branches, the width of the displacement may be
10928 dependent upon data size, but is never dependent upon address size.
10929 Also make sure to not unintentionally match against a non-PC-relative
10930 branch template. */
10931 static templates aux_templates;
10932 const insn_template *t = current_templates->start;
5b7c81bd 10933 bool has_intel64 = false;
376cd056
JB
10934
10935 aux_templates.start = t;
10936 while (++t < current_templates->end)
10937 {
10938 if (t->opcode_modifier.jump
10939 != current_templates->start->opcode_modifier.jump)
10940 break;
4b5aaf5f 10941 if ((t->opcode_modifier.isa64 >= INTEL64))
5b7c81bd 10942 has_intel64 = true;
376cd056
JB
10943 }
10944 if (t < current_templates->end)
10945 {
10946 aux_templates.end = t;
10947 current_templates = &aux_templates;
10948 }
10949
e05278af 10950 override = (i.prefix[DATA_PREFIX] != 0);
40fb9820
L
10951 if (flag_code == CODE_64BIT)
10952 {
376cd056
JB
10953 if ((override || i.suffix == WORD_MNEM_SUFFIX)
10954 && (!intel64 || !has_intel64))
40fb9820
L
10955 bigdisp.bitfield.disp16 = 1;
10956 else
48bcea9f 10957 bigdisp.bitfield.disp32s = 1;
40fb9820
L
10958 }
10959 else
e05278af
JB
10960 {
10961 if (!override)
10962 override = (i.suffix == (flag_code != CODE_16BIT
10963 ? WORD_MNEM_SUFFIX
10964 : LONG_MNEM_SUFFIX));
40fb9820
L
10965 bigdisp.bitfield.disp32 = 1;
10966 if ((flag_code == CODE_16BIT) ^ override)
10967 {
10968 bigdisp.bitfield.disp32 = 0;
10969 bigdisp.bitfield.disp16 = 1;
10970 }
e05278af 10971 }
e05278af 10972 }
c6fb90c8
L
10973 i.types[this_operand] = operand_type_or (i.types[this_operand],
10974 bigdisp);
252b5132
RH
10975
10976 exp = &disp_expressions[i.disp_operands];
520dc8e8 10977 i.op[this_operand].disps = exp;
252b5132
RH
10978 i.disp_operands++;
10979 save_input_line_pointer = input_line_pointer;
10980 input_line_pointer = disp_start;
10981 END_STRING_AND_SAVE (disp_end);
10982
10983#ifndef GCC_ASM_O_HACK
10984#define GCC_ASM_O_HACK 0
10985#endif
10986#if GCC_ASM_O_HACK
10987 END_STRING_AND_SAVE (disp_end + 1);
40fb9820 10988 if (i.types[this_operand].bitfield.baseIndex
24eab124 10989 && displacement_string_end[-1] == '+')
252b5132
RH
10990 {
10991 /* This hack is to avoid a warning when using the "o"
24eab124
AM
10992 constraint within gcc asm statements.
10993 For instance:
10994
10995 #define _set_tssldt_desc(n,addr,limit,type) \
10996 __asm__ __volatile__ ( \
10997 "movw %w2,%0\n\t" \
10998 "movw %w1,2+%0\n\t" \
10999 "rorl $16,%1\n\t" \
11000 "movb %b1,4+%0\n\t" \
11001 "movb %4,5+%0\n\t" \
11002 "movb $0,6+%0\n\t" \
11003 "movb %h1,7+%0\n\t" \
11004 "rorl $16,%1" \
11005 : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
11006
11007 This works great except that the output assembler ends
11008 up looking a bit weird if it turns out that there is
11009 no offset. You end up producing code that looks like:
11010
11011 #APP
11012 movw $235,(%eax)
11013 movw %dx,2+(%eax)
11014 rorl $16,%edx
11015 movb %dl,4+(%eax)
11016 movb $137,5+(%eax)
11017 movb $0,6+(%eax)
11018 movb %dh,7+(%eax)
11019 rorl $16,%edx
11020 #NO_APP
11021
47926f60 11022 So here we provide the missing zero. */
24eab124
AM
11023
11024 *displacement_string_end = '0';
252b5132
RH
11025 }
11026#endif
d258b828 11027 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
11028 if (gotfree_input_line)
11029 input_line_pointer = gotfree_input_line;
252b5132 11030
24eab124 11031 exp_seg = expression (exp);
252b5132 11032
636c26b0
AM
11033 SKIP_WHITESPACE ();
11034 if (*input_line_pointer)
11035 as_bad (_("junk `%s' after expression"), input_line_pointer);
11036#if GCC_ASM_O_HACK
11037 RESTORE_END_STRING (disp_end + 1);
11038#endif
636c26b0 11039 input_line_pointer = save_input_line_pointer;
636c26b0 11040 if (gotfree_input_line)
ee86248c
JB
11041 {
11042 free (gotfree_input_line);
11043
11044 if (exp->X_op == O_constant || exp->X_op == O_register)
11045 exp->X_op = O_illegal;
11046 }
11047
11048 ret = i386_finalize_displacement (exp_seg, exp, types, disp_start);
11049
11050 RESTORE_END_STRING (disp_end);
11051
11052 return ret;
11053}
11054
11055static int
11056i386_finalize_displacement (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
11057 i386_operand_type types, const char *disp_start)
11058{
11059 i386_operand_type bigdisp;
11060 int ret = 1;
636c26b0 11061
24eab124
AM
11062 /* We do this to make sure that the section symbol is in
11063 the symbol table. We will ultimately change the relocation
47926f60 11064 to be relative to the beginning of the section. */
1ae12ab7 11065 if (i.reloc[this_operand] == BFD_RELOC_386_GOTOFF
d6ab8113
JB
11066 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
11067 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
24eab124 11068 {
636c26b0 11069 if (exp->X_op != O_symbol)
3992d3b7 11070 goto inv_disp;
636c26b0 11071
e5cb08ac 11072 if (S_IS_LOCAL (exp->X_add_symbol)
c64efb4b
L
11073 && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section
11074 && S_GET_SEGMENT (exp->X_add_symbol) != expr_section)
24eab124 11075 section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
24eab124
AM
11076 exp->X_op = O_subtract;
11077 exp->X_op_symbol = GOT_symbol;
1ae12ab7 11078 if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
29b0f896 11079 i.reloc[this_operand] = BFD_RELOC_32_PCREL;
d6ab8113
JB
11080 else if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
11081 i.reloc[this_operand] = BFD_RELOC_64;
23df1078 11082 else
29b0f896 11083 i.reloc[this_operand] = BFD_RELOC_32;
24eab124 11084 }
252b5132 11085
3992d3b7
AM
11086 else if (exp->X_op == O_absent
11087 || exp->X_op == O_illegal
ee86248c 11088 || exp->X_op == O_big)
2daf4fd8 11089 {
3992d3b7
AM
11090 inv_disp:
11091 as_bad (_("missing or invalid displacement expression `%s'"),
2daf4fd8 11092 disp_start);
3992d3b7 11093 ret = 0;
2daf4fd8
AM
11094 }
11095
a50187b2
JB
11096 else if (exp->X_op == O_constant)
11097 {
11098 /* Sizing gets taken care of by optimize_disp().
11099
11100 If not 64bit, sign/zero extend val, to account for wraparound
11101 when !BFD64. */
11102 if (flag_code != CODE_64BIT)
11103 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
11104 }
11105
4c63da97 11106#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
a50187b2 11107 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
3992d3b7
AM
11108 && exp_seg != absolute_section
11109 && exp_seg != text_section
11110 && exp_seg != data_section
11111 && exp_seg != bss_section
11112 && exp_seg != undefined_section
11113 && !bfd_is_com_section (exp_seg))
24eab124 11114 {
d0b47220 11115 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
3992d3b7 11116 ret = 0;
24eab124 11117 }
252b5132 11118#endif
3956db08 11119
a50187b2 11120 else if (current_templates->start->opcode_modifier.jump == JUMP_BYTE)
48bcea9f
JB
11121 i.types[this_operand].bitfield.disp8 = 1;
11122
40fb9820 11123 /* Check if this is a displacement only operand. */
2f2be86b 11124 bigdisp = operand_type_and_not (i.types[this_operand], anydisp);
0dfbf9d7 11125 if (operand_type_all_zero (&bigdisp))
c6fb90c8
L
11126 i.types[this_operand] = operand_type_and (i.types[this_operand],
11127 types);
3956db08 11128
3992d3b7 11129 return ret;
252b5132
RH
11130}
11131
2abc2bec
JB
11132/* Return the active addressing mode, taking address override and
11133 registers forming the address into consideration. Update the
11134 address override prefix if necessary. */
47926f60 11135
2abc2bec
JB
11136static enum flag_code
11137i386_addressing_mode (void)
252b5132 11138{
be05d201
L
11139 enum flag_code addr_mode;
11140
11141 if (i.prefix[ADDR_PREFIX])
11142 addr_mode = flag_code == CODE_32BIT ? CODE_16BIT : CODE_32BIT;
a23b33b3
JB
11143 else if (flag_code == CODE_16BIT
11144 && current_templates->start->cpu_flags.bitfield.cpumpx
11145 /* Avoid replacing the "16-bit addressing not allowed" diagnostic
11146 from md_assemble() by "is not a valid base/index expression"
11147 when there is a base and/or index. */
11148 && !i.types[this_operand].bitfield.baseindex)
11149 {
11150 /* MPX insn memory operands with neither base nor index must be forced
11151 to use 32-bit addressing in 16-bit mode. */
11152 addr_mode = CODE_32BIT;
11153 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
11154 ++i.prefixes;
11155 gas_assert (!i.types[this_operand].bitfield.disp16);
11156 gas_assert (!i.types[this_operand].bitfield.disp32);
11157 }
be05d201
L
11158 else
11159 {
11160 addr_mode = flag_code;
11161
24eab124 11162#if INFER_ADDR_PREFIX
be05d201
L
11163 if (i.mem_operands == 0)
11164 {
11165 /* Infer address prefix from the first memory operand. */
11166 const reg_entry *addr_reg = i.base_reg;
11167
11168 if (addr_reg == NULL)
11169 addr_reg = i.index_reg;
eecb386c 11170
be05d201
L
11171 if (addr_reg)
11172 {
e968fc9b 11173 if (addr_reg->reg_type.bitfield.dword)
be05d201
L
11174 addr_mode = CODE_32BIT;
11175 else if (flag_code != CODE_64BIT
dc821c5f 11176 && addr_reg->reg_type.bitfield.word)
be05d201
L
11177 addr_mode = CODE_16BIT;
11178
11179 if (addr_mode != flag_code)
11180 {
11181 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
11182 i.prefixes += 1;
11183 /* Change the size of any displacement too. At most one
11184 of Disp16 or Disp32 is set.
11185 FIXME. There doesn't seem to be any real need for
11186 separate Disp16 and Disp32 flags. The same goes for
11187 Imm16 and Imm32. Removing them would probably clean
11188 up the code quite a lot. */
11189 if (flag_code != CODE_64BIT
11190 && (i.types[this_operand].bitfield.disp16
11191 || i.types[this_operand].bitfield.disp32))
11192 i.types[this_operand]
11193 = operand_type_xor (i.types[this_operand], disp16_32);
11194 }
11195 }
11196 }
24eab124 11197#endif
be05d201
L
11198 }
11199
2abc2bec
JB
11200 return addr_mode;
11201}
11202
11203/* Make sure the memory operand we've been dealt is valid.
11204 Return 1 on success, 0 on a failure. */
11205
11206static int
11207i386_index_check (const char *operand_string)
11208{
11209 const char *kind = "base/index";
11210 enum flag_code addr_mode = i386_addressing_mode ();
a152332d 11211 const insn_template *t = current_templates->start;
2abc2bec 11212
a152332d
JB
11213 if (t->opcode_modifier.isstring
11214 && !t->cpu_flags.bitfield.cpupadlock
fc0763e6
JB
11215 && (current_templates->end[-1].opcode_modifier.isstring
11216 || i.mem_operands))
11217 {
11218 /* Memory operands of string insns are special in that they only allow
11219 a single register (rDI, rSI, or rBX) as their memory address. */
be05d201
L
11220 const reg_entry *expected_reg;
11221 static const char *di_si[][2] =
11222 {
11223 { "esi", "edi" },
11224 { "si", "di" },
11225 { "rsi", "rdi" }
11226 };
11227 static const char *bx[] = { "ebx", "bx", "rbx" };
fc0763e6
JB
11228
11229 kind = "string address";
11230
a152332d 11231 if (t->opcode_modifier.prefixok == PrefixRep)
fc0763e6 11232 {
51c8edf6
JB
11233 int es_op = current_templates->end[-1].opcode_modifier.isstring
11234 - IS_STRING_ES_OP0;
11235 int op = 0;
fc0763e6 11236
51c8edf6 11237 if (!current_templates->end[-1].operand_types[0].bitfield.baseindex
fc0763e6
JB
11238 || ((!i.mem_operands != !intel_syntax)
11239 && current_templates->end[-1].operand_types[1]
11240 .bitfield.baseindex))
51c8edf6 11241 op = 1;
fe0e921f
AM
11242 expected_reg
11243 = (const reg_entry *) str_hash_find (reg_hash,
11244 di_si[addr_mode][op == es_op]);
fc0763e6
JB
11245 }
11246 else
fe0e921f
AM
11247 expected_reg
11248 = (const reg_entry *)str_hash_find (reg_hash, bx[addr_mode]);
fc0763e6 11249
be05d201
L
11250 if (i.base_reg != expected_reg
11251 || i.index_reg
fc0763e6 11252 || operand_type_check (i.types[this_operand], disp))
fc0763e6 11253 {
be05d201
L
11254 /* The second memory operand must have the same size as
11255 the first one. */
11256 if (i.mem_operands
11257 && i.base_reg
11258 && !((addr_mode == CODE_64BIT
dc821c5f 11259 && i.base_reg->reg_type.bitfield.qword)
be05d201 11260 || (addr_mode == CODE_32BIT
dc821c5f
JB
11261 ? i.base_reg->reg_type.bitfield.dword
11262 : i.base_reg->reg_type.bitfield.word)))
be05d201
L
11263 goto bad_address;
11264
fc0763e6
JB
11265 as_warn (_("`%s' is not valid here (expected `%c%s%s%c')"),
11266 operand_string,
11267 intel_syntax ? '[' : '(',
11268 register_prefix,
be05d201 11269 expected_reg->reg_name,
fc0763e6 11270 intel_syntax ? ']' : ')');
be05d201 11271 return 1;
fc0763e6 11272 }
be05d201
L
11273 else
11274 return 1;
11275
dc1e8a47 11276 bad_address:
be05d201
L
11277 as_bad (_("`%s' is not a valid %s expression"),
11278 operand_string, kind);
11279 return 0;
3e73aa7c
JH
11280 }
11281 else
11282 {
be05d201
L
11283 if (addr_mode != CODE_16BIT)
11284 {
11285 /* 32-bit/64-bit checks. */
41eb8e88
L
11286 if (i.disp_encoding == disp_encoding_16bit)
11287 {
11288 bad_disp:
11289 as_bad (_("invalid `%s' prefix"),
11290 addr_mode == CODE_16BIT ? "{disp32}" : "{disp16}");
11291 return 0;
11292 }
11293
be05d201 11294 if ((i.base_reg
e968fc9b
JB
11295 && ((addr_mode == CODE_64BIT
11296 ? !i.base_reg->reg_type.bitfield.qword
11297 : !i.base_reg->reg_type.bitfield.dword)
11298 || (i.index_reg && i.base_reg->reg_num == RegIP)
11299 || i.base_reg->reg_num == RegIZ))
be05d201 11300 || (i.index_reg
1b54b8d7
JB
11301 && !i.index_reg->reg_type.bitfield.xmmword
11302 && !i.index_reg->reg_type.bitfield.ymmword
11303 && !i.index_reg->reg_type.bitfield.zmmword
be05d201 11304 && ((addr_mode == CODE_64BIT
e968fc9b
JB
11305 ? !i.index_reg->reg_type.bitfield.qword
11306 : !i.index_reg->reg_type.bitfield.dword)
be05d201
L
11307 || !i.index_reg->reg_type.bitfield.baseindex)))
11308 goto bad_address;
8178be5b 11309
260cd341 11310 /* bndmk, bndldx, bndstx and mandatory non-vector SIB have special restrictions. */
a152332d 11311 if ((t->opcode_modifier.opcodeprefix == PREFIX_0XF3
389d00a5
JB
11312 && t->opcode_modifier.opcodespace == SPACE_0F
11313 && t->base_opcode == 0x1b)
a152332d 11314 || (t->opcode_modifier.opcodeprefix == PREFIX_NONE
389d00a5
JB
11315 && t->opcode_modifier.opcodespace == SPACE_0F
11316 && (t->base_opcode & ~1) == 0x1a)
a152332d 11317 || t->opcode_modifier.sib == SIBMEM)
8178be5b
JB
11318 {
11319 /* They cannot use RIP-relative addressing. */
e968fc9b 11320 if (i.base_reg && i.base_reg->reg_num == RegIP)
8178be5b
JB
11321 {
11322 as_bad (_("`%s' cannot be used here"), operand_string);
11323 return 0;
11324 }
11325
11326 /* bndldx and bndstx ignore their scale factor. */
a152332d 11327 if (t->opcode_modifier.opcodeprefix == PREFIX_NONE
389d00a5
JB
11328 && t->opcode_modifier.opcodespace == SPACE_0F
11329 && (t->base_opcode & ~1) == 0x1a
8178be5b
JB
11330 && i.log2_scale_factor)
11331 as_warn (_("register scaling is being ignored here"));
11332 }
be05d201
L
11333 }
11334 else
3e73aa7c 11335 {
be05d201 11336 /* 16-bit checks. */
41eb8e88
L
11337 if (i.disp_encoding == disp_encoding_32bit)
11338 goto bad_disp;
11339
3e73aa7c 11340 if ((i.base_reg
dc821c5f 11341 && (!i.base_reg->reg_type.bitfield.word
40fb9820 11342 || !i.base_reg->reg_type.bitfield.baseindex))
3e73aa7c 11343 || (i.index_reg
dc821c5f 11344 && (!i.index_reg->reg_type.bitfield.word
40fb9820 11345 || !i.index_reg->reg_type.bitfield.baseindex
29b0f896
AM
11346 || !(i.base_reg
11347 && i.base_reg->reg_num < 6
11348 && i.index_reg->reg_num >= 6
11349 && i.log2_scale_factor == 0))))
be05d201 11350 goto bad_address;
3e73aa7c
JH
11351 }
11352 }
be05d201 11353 return 1;
24eab124 11354}
252b5132 11355
43234a1e
L
11356/* Handle vector immediates. */
11357
11358static int
11359RC_SAE_immediate (const char *imm_start)
11360{
11361 unsigned int match_found, j;
11362 const char *pstr = imm_start;
11363 expressionS *exp;
11364
11365 if (*pstr != '{')
11366 return 0;
11367
11368 pstr++;
11369 match_found = 0;
11370 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); j++)
11371 {
11372 if (!strncmp (pstr, RC_NamesTable[j].name, RC_NamesTable[j].len))
11373 {
ca5312a2 11374 if (i.rounding.type != rc_none)
43234a1e
L
11375 {
11376 as_bad (_("duplicated `%s'"), imm_start);
11377 return 0;
11378 }
ca5312a2
JB
11379
11380 i.rounding.type = RC_NamesTable[j].type;
11381 i.rounding.operand = this_operand;
11382
43234a1e
L
11383 pstr += RC_NamesTable[j].len;
11384 match_found = 1;
11385 break;
11386 }
11387 }
11388 if (!match_found)
11389 return 0;
11390
11391 if (*pstr++ != '}')
11392 {
11393 as_bad (_("Missing '}': '%s'"), imm_start);
11394 return 0;
11395 }
11396 /* RC/SAE immediate string should contain nothing more. */;
11397 if (*pstr != 0)
11398 {
11399 as_bad (_("Junk after '}': '%s'"), imm_start);
11400 return 0;
11401 }
11402
11403 exp = &im_expressions[i.imm_operands++];
11404 i.op[this_operand].imms = exp;
11405
11406 exp->X_op = O_constant;
11407 exp->X_add_number = 0;
11408 exp->X_add_symbol = (symbolS *) 0;
11409 exp->X_op_symbol = (symbolS *) 0;
11410
11411 i.types[this_operand].bitfield.imm8 = 1;
11412 return 1;
11413}
11414
8325cc63
JB
11415/* Only string instructions can have a second memory operand, so
11416 reduce current_templates to just those if it contains any. */
11417static int
11418maybe_adjust_templates (void)
11419{
11420 const insn_template *t;
11421
11422 gas_assert (i.mem_operands == 1);
11423
11424 for (t = current_templates->start; t < current_templates->end; ++t)
11425 if (t->opcode_modifier.isstring)
11426 break;
11427
11428 if (t < current_templates->end)
11429 {
11430 static templates aux_templates;
5b7c81bd 11431 bool recheck;
8325cc63
JB
11432
11433 aux_templates.start = t;
11434 for (; t < current_templates->end; ++t)
11435 if (!t->opcode_modifier.isstring)
11436 break;
11437 aux_templates.end = t;
11438
11439 /* Determine whether to re-check the first memory operand. */
11440 recheck = (aux_templates.start != current_templates->start
11441 || t != current_templates->end);
11442
11443 current_templates = &aux_templates;
11444
11445 if (recheck)
11446 {
11447 i.mem_operands = 0;
11448 if (i.memop1_string != NULL
11449 && i386_index_check (i.memop1_string) == 0)
11450 return 0;
11451 i.mem_operands = 1;
11452 }
11453 }
11454
11455 return 1;
11456}
11457
9d299bea
JB
11458static INLINE bool starts_memory_operand (char c)
11459{
014fbcda 11460 return ISDIGIT (c)
9d299bea 11461 || is_identifier_char (c)
014fbcda 11462 || strchr ("([\"+-!~", c);
9d299bea
JB
11463}
11464
fc0763e6 11465/* Parse OPERAND_STRING into the i386_insn structure I. Returns zero
47926f60 11466 on error. */
252b5132 11467
252b5132 11468static int
a7619375 11469i386_att_operand (char *operand_string)
252b5132 11470{
af6bdddf
AM
11471 const reg_entry *r;
11472 char *end_op;
24eab124 11473 char *op_string = operand_string;
252b5132 11474
24eab124 11475 if (is_space_char (*op_string))
252b5132
RH
11476 ++op_string;
11477
24eab124 11478 /* We check for an absolute prefix (differentiating,
47926f60 11479 for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
24eab124
AM
11480 if (*op_string == ABSOLUTE_PREFIX)
11481 {
11482 ++op_string;
11483 if (is_space_char (*op_string))
11484 ++op_string;
5b7c81bd 11485 i.jumpabsolute = true;
24eab124 11486 }
252b5132 11487
47926f60 11488 /* Check if operand is a register. */
4d1bb795 11489 if ((r = parse_register (op_string, &end_op)) != NULL)
24eab124 11490 {
40fb9820
L
11491 i386_operand_type temp;
11492
8a6fb3f9
JB
11493 if (r == &bad_reg)
11494 return 0;
11495
24eab124
AM
11496 /* Check for a segment override by searching for ':' after a
11497 segment register. */
11498 op_string = end_op;
11499 if (is_space_char (*op_string))
11500 ++op_string;
00cee14f 11501 if (*op_string == ':' && r->reg_type.bitfield.class == SReg)
24eab124 11502 {
5e042380 11503 i.seg[i.mem_operands] = r;
252b5132 11504
24eab124 11505 /* Skip the ':' and whitespace. */
252b5132
RH
11506 ++op_string;
11507 if (is_space_char (*op_string))
24eab124 11508 ++op_string;
252b5132 11509
47926f60 11510 /* Handle case of %es:*foo. */
c8d541e2 11511 if (!i.jumpabsolute && *op_string == ABSOLUTE_PREFIX)
24eab124
AM
11512 {
11513 ++op_string;
11514 if (is_space_char (*op_string))
11515 ++op_string;
5b7c81bd 11516 i.jumpabsolute = true;
24eab124 11517 }
c8d541e2 11518
9d299bea 11519 if (!starts_memory_operand (*op_string))
c8d541e2
JB
11520 {
11521 as_bad (_("bad memory operand `%s'"), op_string);
11522 return 0;
11523 }
24eab124
AM
11524 goto do_memory_reference;
11525 }
43234a1e
L
11526
11527 /* Handle vector operations. */
11528 if (*op_string == '{')
11529 {
f70c6814 11530 op_string = check_VecOperations (op_string);
43234a1e
L
11531 if (op_string == NULL)
11532 return 0;
11533 }
11534
24eab124
AM
11535 if (*op_string)
11536 {
d0b47220 11537 as_bad (_("junk `%s' after register"), op_string);
24eab124
AM
11538 return 0;
11539 }
40fb9820
L
11540 temp = r->reg_type;
11541 temp.bitfield.baseindex = 0;
c6fb90c8
L
11542 i.types[this_operand] = operand_type_or (i.types[this_operand],
11543 temp);
7d5e4556 11544 i.types[this_operand].bitfield.unspecified = 0;
520dc8e8 11545 i.op[this_operand].regs = r;
24eab124
AM
11546 i.reg_operands++;
11547 }
af6bdddf
AM
11548 else if (*op_string == REGISTER_PREFIX)
11549 {
11550 as_bad (_("bad register name `%s'"), op_string);
11551 return 0;
11552 }
24eab124 11553 else if (*op_string == IMMEDIATE_PREFIX)
ce8a8b2f 11554 {
24eab124 11555 ++op_string;
6f2f06be 11556 if (i.jumpabsolute)
24eab124 11557 {
d0b47220 11558 as_bad (_("immediate operand illegal with absolute jump"));
24eab124
AM
11559 return 0;
11560 }
11561 if (!i386_immediate (op_string))
11562 return 0;
11563 }
43234a1e
L
11564 else if (RC_SAE_immediate (operand_string))
11565 {
11566 /* If it is a RC or SAE immediate, do nothing. */
11567 ;
11568 }
9d299bea 11569 else if (starts_memory_operand (*op_string))
24eab124 11570 {
47926f60 11571 /* This is a memory reference of some sort. */
af6bdddf 11572 char *base_string;
252b5132 11573
47926f60 11574 /* Start and end of displacement string expression (if found). */
eecb386c
AM
11575 char *displacement_string_start;
11576 char *displacement_string_end;
252b5132 11577
24eab124 11578 do_memory_reference:
8325cc63
JB
11579 if (i.mem_operands == 1 && !maybe_adjust_templates ())
11580 return 0;
24eab124 11581 if ((i.mem_operands == 1
40fb9820 11582 && !current_templates->start->opcode_modifier.isstring)
24eab124
AM
11583 || i.mem_operands == 2)
11584 {
11585 as_bad (_("too many memory references for `%s'"),
11586 current_templates->start->name);
11587 return 0;
11588 }
252b5132 11589
24eab124
AM
11590 /* Check for base index form. We detect the base index form by
11591 looking for an ')' at the end of the operand, searching
11592 for the '(' matching it, and finding a REGISTER_PREFIX or ','
11593 after the '('. */
af6bdddf 11594 base_string = op_string + strlen (op_string);
c3332e24 11595
43234a1e 11596 /* Handle vector operations. */
6b5ba0d4
JB
11597 --base_string;
11598 if (is_space_char (*base_string))
11599 --base_string;
11600
11601 if (*base_string == '}')
43234a1e 11602 {
6b5ba0d4
JB
11603 char *vop_start = NULL;
11604
11605 while (base_string-- > op_string)
11606 {
11607 if (*base_string == '"')
11608 break;
11609 if (*base_string != '{')
11610 continue;
11611
11612 vop_start = base_string;
11613
11614 --base_string;
11615 if (is_space_char (*base_string))
11616 --base_string;
11617
11618 if (*base_string != '}')
11619 break;
11620
11621 vop_start = NULL;
11622 }
11623
11624 if (!vop_start)
11625 {
11626 as_bad (_("unbalanced figure braces"));
11627 return 0;
11628 }
11629
f70c6814 11630 if (check_VecOperations (vop_start) == NULL)
43234a1e 11631 return 0;
43234a1e
L
11632 }
11633
47926f60 11634 /* If we only have a displacement, set-up for it to be parsed later. */
af6bdddf
AM
11635 displacement_string_start = op_string;
11636 displacement_string_end = base_string + 1;
252b5132 11637
24eab124
AM
11638 if (*base_string == ')')
11639 {
af6bdddf 11640 char *temp_string;
cc0f9635 11641 unsigned int parens_not_balanced = 1;
e68c3d59 11642
24eab124 11643 /* We've already checked that the number of left & right ()'s are
47926f60 11644 equal, so this loop will not be infinite. */
24eab124
AM
11645 do
11646 {
11647 base_string--;
cc0f9635
JB
11648 if (*base_string == ')')
11649 parens_not_balanced++;
11650 if (*base_string == '(')
11651 parens_not_balanced--;
24eab124 11652 }
cc0f9635 11653 while (parens_not_balanced && *base_string != '"');
c3332e24 11654
af6bdddf 11655 temp_string = base_string;
c3332e24 11656
24eab124 11657 /* Skip past '(' and whitespace. */
e68c3d59
JB
11658 if (*base_string == '(')
11659 ++base_string;
252b5132 11660 if (is_space_char (*base_string))
24eab124 11661 ++base_string;
252b5132 11662
af6bdddf 11663 if (*base_string == ','
4eed87de
AM
11664 || ((i.base_reg = parse_register (base_string, &end_op))
11665 != NULL))
252b5132 11666 {
af6bdddf 11667 displacement_string_end = temp_string;
252b5132 11668
40fb9820 11669 i.types[this_operand].bitfield.baseindex = 1;
252b5132 11670
af6bdddf 11671 if (i.base_reg)
24eab124 11672 {
8a6fb3f9
JB
11673 if (i.base_reg == &bad_reg)
11674 return 0;
24eab124
AM
11675 base_string = end_op;
11676 if (is_space_char (*base_string))
11677 ++base_string;
af6bdddf
AM
11678 }
11679
11680 /* There may be an index reg or scale factor here. */
11681 if (*base_string == ',')
11682 {
11683 ++base_string;
11684 if (is_space_char (*base_string))
11685 ++base_string;
11686
4eed87de
AM
11687 if ((i.index_reg = parse_register (base_string, &end_op))
11688 != NULL)
24eab124 11689 {
8a6fb3f9
JB
11690 if (i.index_reg == &bad_reg)
11691 return 0;
af6bdddf 11692 base_string = end_op;
24eab124
AM
11693 if (is_space_char (*base_string))
11694 ++base_string;
af6bdddf
AM
11695 if (*base_string == ',')
11696 {
11697 ++base_string;
11698 if (is_space_char (*base_string))
11699 ++base_string;
11700 }
e5cb08ac 11701 else if (*base_string != ')')
af6bdddf 11702 {
4eed87de
AM
11703 as_bad (_("expecting `,' or `)' "
11704 "after index register in `%s'"),
af6bdddf
AM
11705 operand_string);
11706 return 0;
11707 }
24eab124 11708 }
af6bdddf 11709 else if (*base_string == REGISTER_PREFIX)
24eab124 11710 {
f76bf5e0
L
11711 end_op = strchr (base_string, ',');
11712 if (end_op)
11713 *end_op = '\0';
af6bdddf 11714 as_bad (_("bad register name `%s'"), base_string);
24eab124
AM
11715 return 0;
11716 }
252b5132 11717
47926f60 11718 /* Check for scale factor. */
551c1ca1 11719 if (*base_string != ')')
af6bdddf 11720 {
551c1ca1
AM
11721 char *end_scale = i386_scale (base_string);
11722
11723 if (!end_scale)
af6bdddf 11724 return 0;
24eab124 11725
551c1ca1 11726 base_string = end_scale;
af6bdddf
AM
11727 if (is_space_char (*base_string))
11728 ++base_string;
11729 if (*base_string != ')')
11730 {
4eed87de
AM
11731 as_bad (_("expecting `)' "
11732 "after scale factor in `%s'"),
af6bdddf
AM
11733 operand_string);
11734 return 0;
11735 }
11736 }
11737 else if (!i.index_reg)
24eab124 11738 {
4eed87de
AM
11739 as_bad (_("expecting index register or scale factor "
11740 "after `,'; got '%c'"),
af6bdddf 11741 *base_string);
24eab124
AM
11742 return 0;
11743 }
11744 }
af6bdddf 11745 else if (*base_string != ')')
24eab124 11746 {
4eed87de
AM
11747 as_bad (_("expecting `,' or `)' "
11748 "after base register in `%s'"),
af6bdddf 11749 operand_string);
24eab124
AM
11750 return 0;
11751 }
c3332e24 11752 }
af6bdddf 11753 else if (*base_string == REGISTER_PREFIX)
c3332e24 11754 {
f76bf5e0
L
11755 end_op = strchr (base_string, ',');
11756 if (end_op)
11757 *end_op = '\0';
af6bdddf 11758 as_bad (_("bad register name `%s'"), base_string);
24eab124 11759 return 0;
c3332e24 11760 }
24eab124
AM
11761 }
11762
11763 /* If there's an expression beginning the operand, parse it,
11764 assuming displacement_string_start and
11765 displacement_string_end are meaningful. */
11766 if (displacement_string_start != displacement_string_end)
11767 {
11768 if (!i386_displacement (displacement_string_start,
11769 displacement_string_end))
11770 return 0;
11771 }
11772
11773 /* Special case for (%dx) while doing input/output op. */
11774 if (i.base_reg
75e5731b
JB
11775 && i.base_reg->reg_type.bitfield.instance == RegD
11776 && i.base_reg->reg_type.bitfield.word
24eab124
AM
11777 && i.index_reg == 0
11778 && i.log2_scale_factor == 0
11779 && i.seg[i.mem_operands] == 0
40fb9820 11780 && !operand_type_check (i.types[this_operand], disp))
24eab124 11781 {
2fb5be8d 11782 i.types[this_operand] = i.base_reg->reg_type;
24eab124
AM
11783 return 1;
11784 }
11785
eecb386c
AM
11786 if (i386_index_check (operand_string) == 0)
11787 return 0;
c48dadc9 11788 i.flags[this_operand] |= Operand_Mem;
8325cc63
JB
11789 if (i.mem_operands == 0)
11790 i.memop1_string = xstrdup (operand_string);
24eab124
AM
11791 i.mem_operands++;
11792 }
11793 else
ce8a8b2f
AM
11794 {
11795 /* It's not a memory operand; argh! */
24eab124
AM
11796 as_bad (_("invalid char %s beginning operand %d `%s'"),
11797 output_invalid (*op_string),
11798 this_operand + 1,
11799 op_string);
11800 return 0;
11801 }
47926f60 11802 return 1; /* Normal return. */
252b5132
RH
11803}
11804\f
fa94de6b
RM
11805/* Calculate the maximum variable size (i.e., excluding fr_fix)
11806 that an rs_machine_dependent frag may reach. */
11807
11808unsigned int
11809i386_frag_max_var (fragS *frag)
11810{
11811 /* The only relaxable frags are for jumps.
11812 Unconditional jumps can grow by 4 bytes and others by 5 bytes. */
11813 gas_assert (frag->fr_type == rs_machine_dependent);
11814 return TYPE_FROM_RELAX_STATE (frag->fr_subtype) == UNCOND_JUMP ? 4 : 5;
11815}
11816
b084df0b
L
11817#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
11818static int
8dcea932 11819elf_symbol_resolved_in_segment_p (symbolS *fr_symbol, offsetT fr_var)
b084df0b
L
11820{
11821 /* STT_GNU_IFUNC symbol must go through PLT. */
11822 if ((symbol_get_bfdsym (fr_symbol)->flags
11823 & BSF_GNU_INDIRECT_FUNCTION) != 0)
11824 return 0;
11825
11826 if (!S_IS_EXTERNAL (fr_symbol))
11827 /* Symbol may be weak or local. */
11828 return !S_IS_WEAK (fr_symbol);
11829
8dcea932
L
11830 /* Global symbols with non-default visibility can't be preempted. */
11831 if (ELF_ST_VISIBILITY (S_GET_OTHER (fr_symbol)) != STV_DEFAULT)
11832 return 1;
11833
11834 if (fr_var != NO_RELOC)
11835 switch ((enum bfd_reloc_code_real) fr_var)
11836 {
11837 case BFD_RELOC_386_PLT32:
11838 case BFD_RELOC_X86_64_PLT32:
33eaf5de 11839 /* Symbol with PLT relocation may be preempted. */
8dcea932
L
11840 return 0;
11841 default:
11842 abort ();
11843 }
11844
b084df0b
L
11845 /* Global symbols with default visibility in a shared library may be
11846 preempted by another definition. */
8dcea932 11847 return !shared;
b084df0b
L
11848}
11849#endif
11850
79d72f45
HL
11851/* Table 3-2. Macro-Fusible Instructions in Haswell Microarchitecture
11852 Note also work for Skylake and Cascadelake.
11853---------------------------------------------------------------------
11854| JCC | ADD/SUB/CMP | INC/DEC | TEST/AND |
11855| ------ | ----------- | ------- | -------- |
11856| Jo | N | N | Y |
11857| Jno | N | N | Y |
11858| Jc/Jb | Y | N | Y |
11859| Jae/Jnb | Y | N | Y |
11860| Je/Jz | Y | Y | Y |
11861| Jne/Jnz | Y | Y | Y |
11862| Jna/Jbe | Y | N | Y |
11863| Ja/Jnbe | Y | N | Y |
11864| Js | N | N | Y |
11865| Jns | N | N | Y |
11866| Jp/Jpe | N | N | Y |
11867| Jnp/Jpo | N | N | Y |
11868| Jl/Jnge | Y | Y | Y |
11869| Jge/Jnl | Y | Y | Y |
11870| Jle/Jng | Y | Y | Y |
11871| Jg/Jnle | Y | Y | Y |
11872--------------------------------------------------------------------- */
11873static int
11874i386_macro_fusible_p (enum mf_cmp_kind mf_cmp, enum mf_jcc_kind mf_jcc)
11875{
11876 if (mf_cmp == mf_cmp_alu_cmp)
11877 return ((mf_jcc >= mf_jcc_jc && mf_jcc <= mf_jcc_jna)
11878 || mf_jcc == mf_jcc_jl || mf_jcc == mf_jcc_jle);
11879 if (mf_cmp == mf_cmp_incdec)
11880 return (mf_jcc == mf_jcc_je || mf_jcc == mf_jcc_jl
11881 || mf_jcc == mf_jcc_jle);
11882 if (mf_cmp == mf_cmp_test_and)
11883 return 1;
11884 return 0;
11885}
11886
e379e5f3
L
11887/* Return the next non-empty frag. */
11888
11889static fragS *
11890i386_next_non_empty_frag (fragS *fragP)
11891{
11892 /* There may be a frag with a ".fill 0" when there is no room in
11893 the current frag for frag_grow in output_insn. */
11894 for (fragP = fragP->fr_next;
11895 (fragP != NULL
11896 && fragP->fr_type == rs_fill
11897 && fragP->fr_fix == 0);
11898 fragP = fragP->fr_next)
11899 ;
11900 return fragP;
11901}
11902
11903/* Return the next jcc frag after BRANCH_PADDING. */
11904
11905static fragS *
79d72f45 11906i386_next_fusible_jcc_frag (fragS *maybe_cmp_fragP, fragS *pad_fragP)
e379e5f3 11907{
79d72f45
HL
11908 fragS *branch_fragP;
11909 if (!pad_fragP)
e379e5f3
L
11910 return NULL;
11911
79d72f45
HL
11912 if (pad_fragP->fr_type == rs_machine_dependent
11913 && (TYPE_FROM_RELAX_STATE (pad_fragP->fr_subtype)
e379e5f3
L
11914 == BRANCH_PADDING))
11915 {
79d72f45
HL
11916 branch_fragP = i386_next_non_empty_frag (pad_fragP);
11917 if (branch_fragP->fr_type != rs_machine_dependent)
e379e5f3 11918 return NULL;
79d72f45
HL
11919 if (TYPE_FROM_RELAX_STATE (branch_fragP->fr_subtype) == COND_JUMP
11920 && i386_macro_fusible_p (maybe_cmp_fragP->tc_frag_data.mf_type,
11921 pad_fragP->tc_frag_data.mf_type))
11922 return branch_fragP;
e379e5f3
L
11923 }
11924
11925 return NULL;
11926}
11927
11928/* Classify BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags. */
11929
11930static void
11931i386_classify_machine_dependent_frag (fragS *fragP)
11932{
11933 fragS *cmp_fragP;
11934 fragS *pad_fragP;
11935 fragS *branch_fragP;
11936 fragS *next_fragP;
11937 unsigned int max_prefix_length;
11938
11939 if (fragP->tc_frag_data.classified)
11940 return;
11941
11942 /* First scan for BRANCH_PADDING and FUSED_JCC_PADDING. Convert
11943 FUSED_JCC_PADDING and merge BRANCH_PADDING. */
11944 for (next_fragP = fragP;
11945 next_fragP != NULL;
11946 next_fragP = next_fragP->fr_next)
11947 {
11948 next_fragP->tc_frag_data.classified = 1;
11949 if (next_fragP->fr_type == rs_machine_dependent)
11950 switch (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype))
11951 {
11952 case BRANCH_PADDING:
11953 /* The BRANCH_PADDING frag must be followed by a branch
11954 frag. */
11955 branch_fragP = i386_next_non_empty_frag (next_fragP);
11956 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
11957 break;
11958 case FUSED_JCC_PADDING:
11959 /* Check if this is a fused jcc:
11960 FUSED_JCC_PADDING
11961 CMP like instruction
11962 BRANCH_PADDING
11963 COND_JUMP
11964 */
11965 cmp_fragP = i386_next_non_empty_frag (next_fragP);
11966 pad_fragP = i386_next_non_empty_frag (cmp_fragP);
79d72f45 11967 branch_fragP = i386_next_fusible_jcc_frag (next_fragP, pad_fragP);
e379e5f3
L
11968 if (branch_fragP)
11969 {
11970 /* The BRANCH_PADDING frag is merged with the
11971 FUSED_JCC_PADDING frag. */
11972 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
11973 /* CMP like instruction size. */
11974 next_fragP->tc_frag_data.cmp_size = cmp_fragP->fr_fix;
11975 frag_wane (pad_fragP);
11976 /* Skip to branch_fragP. */
11977 next_fragP = branch_fragP;
11978 }
11979 else if (next_fragP->tc_frag_data.max_prefix_length)
11980 {
11981 /* Turn FUSED_JCC_PADDING into BRANCH_PREFIX if it isn't
11982 a fused jcc. */
11983 next_fragP->fr_subtype
11984 = ENCODE_RELAX_STATE (BRANCH_PREFIX, 0);
11985 next_fragP->tc_frag_data.max_bytes
11986 = next_fragP->tc_frag_data.max_prefix_length;
11987 /* This will be updated in the BRANCH_PREFIX scan. */
11988 next_fragP->tc_frag_data.max_prefix_length = 0;
11989 }
11990 else
11991 frag_wane (next_fragP);
11992 break;
11993 }
11994 }
11995
11996 /* Stop if there is no BRANCH_PREFIX. */
11997 if (!align_branch_prefix_size)
11998 return;
11999
12000 /* Scan for BRANCH_PREFIX. */
12001 for (; fragP != NULL; fragP = fragP->fr_next)
12002 {
12003 if (fragP->fr_type != rs_machine_dependent
12004 || (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
12005 != BRANCH_PREFIX))
12006 continue;
12007
12008 /* Count all BRANCH_PREFIX frags before BRANCH_PADDING and
12009 COND_JUMP_PREFIX. */
12010 max_prefix_length = 0;
12011 for (next_fragP = fragP;
12012 next_fragP != NULL;
12013 next_fragP = next_fragP->fr_next)
12014 {
12015 if (next_fragP->fr_type == rs_fill)
12016 /* Skip rs_fill frags. */
12017 continue;
12018 else if (next_fragP->fr_type != rs_machine_dependent)
12019 /* Stop for all other frags. */
12020 break;
12021
12022 /* rs_machine_dependent frags. */
12023 if (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12024 == BRANCH_PREFIX)
12025 {
12026 /* Count BRANCH_PREFIX frags. */
12027 if (max_prefix_length >= MAX_FUSED_JCC_PADDING_SIZE)
12028 {
12029 max_prefix_length = MAX_FUSED_JCC_PADDING_SIZE;
12030 frag_wane (next_fragP);
12031 }
12032 else
12033 max_prefix_length
12034 += next_fragP->tc_frag_data.max_bytes;
12035 }
12036 else if ((TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12037 == BRANCH_PADDING)
12038 || (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12039 == FUSED_JCC_PADDING))
12040 {
12041 /* Stop at BRANCH_PADDING and FUSED_JCC_PADDING. */
12042 fragP->tc_frag_data.u.padding_fragP = next_fragP;
12043 break;
12044 }
12045 else
12046 /* Stop for other rs_machine_dependent frags. */
12047 break;
12048 }
12049
12050 fragP->tc_frag_data.max_prefix_length = max_prefix_length;
12051
12052 /* Skip to the next frag. */
12053 fragP = next_fragP;
12054 }
12055}
12056
12057/* Compute padding size for
12058
12059 FUSED_JCC_PADDING
12060 CMP like instruction
12061 BRANCH_PADDING
12062 COND_JUMP/UNCOND_JUMP
12063
12064 or
12065
12066 BRANCH_PADDING
12067 COND_JUMP/UNCOND_JUMP
12068 */
12069
12070static int
12071i386_branch_padding_size (fragS *fragP, offsetT address)
12072{
12073 unsigned int offset, size, padding_size;
12074 fragS *branch_fragP = fragP->tc_frag_data.u.branch_fragP;
12075
12076 /* The start address of the BRANCH_PADDING or FUSED_JCC_PADDING frag. */
12077 if (!address)
12078 address = fragP->fr_address;
12079 address += fragP->fr_fix;
12080
12081 /* CMP like instrunction size. */
12082 size = fragP->tc_frag_data.cmp_size;
12083
12084 /* The base size of the branch frag. */
12085 size += branch_fragP->fr_fix;
12086
12087 /* Add opcode and displacement bytes for the rs_machine_dependent
12088 branch frag. */
12089 if (branch_fragP->fr_type == rs_machine_dependent)
12090 size += md_relax_table[branch_fragP->fr_subtype].rlx_length;
12091
12092 /* Check if branch is within boundary and doesn't end at the last
12093 byte. */
12094 offset = address & ((1U << align_branch_power) - 1);
12095 if ((offset + size) >= (1U << align_branch_power))
12096 /* Padding needed to avoid crossing boundary. */
12097 padding_size = (1U << align_branch_power) - offset;
12098 else
12099 /* No padding needed. */
12100 padding_size = 0;
12101
12102 /* The return value may be saved in tc_frag_data.length which is
12103 unsigned byte. */
12104 if (!fits_in_unsigned_byte (padding_size))
12105 abort ();
12106
12107 return padding_size;
12108}
12109
12110/* i386_generic_table_relax_frag()
12111
12112 Handle BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags to
12113 grow/shrink padding to align branch frags. Hand others to
12114 relax_frag(). */
12115
12116long
12117i386_generic_table_relax_frag (segT segment, fragS *fragP, long stretch)
12118{
12119 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12120 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
12121 {
12122 long padding_size = i386_branch_padding_size (fragP, 0);
12123 long grow = padding_size - fragP->tc_frag_data.length;
12124
12125 /* When the BRANCH_PREFIX frag is used, the computed address
12126 must match the actual address and there should be no padding. */
12127 if (fragP->tc_frag_data.padding_address
12128 && (fragP->tc_frag_data.padding_address != fragP->fr_address
12129 || padding_size))
12130 abort ();
12131
12132 /* Update the padding size. */
12133 if (grow)
12134 fragP->tc_frag_data.length = padding_size;
12135
12136 return grow;
12137 }
12138 else if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12139 {
12140 fragS *padding_fragP, *next_fragP;
12141 long padding_size, left_size, last_size;
12142
12143 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
12144 if (!padding_fragP)
12145 /* Use the padding set by the leading BRANCH_PREFIX frag. */
12146 return (fragP->tc_frag_data.length
12147 - fragP->tc_frag_data.last_length);
12148
12149 /* Compute the relative address of the padding frag in the very
12150 first time where the BRANCH_PREFIX frag sizes are zero. */
12151 if (!fragP->tc_frag_data.padding_address)
12152 fragP->tc_frag_data.padding_address
12153 = padding_fragP->fr_address - (fragP->fr_address - stretch);
12154
12155 /* First update the last length from the previous interation. */
12156 left_size = fragP->tc_frag_data.prefix_length;
12157 for (next_fragP = fragP;
12158 next_fragP != padding_fragP;
12159 next_fragP = next_fragP->fr_next)
12160 if (next_fragP->fr_type == rs_machine_dependent
12161 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12162 == BRANCH_PREFIX))
12163 {
12164 if (left_size)
12165 {
12166 int max = next_fragP->tc_frag_data.max_bytes;
12167 if (max)
12168 {
12169 int size;
12170 if (max > left_size)
12171 size = left_size;
12172 else
12173 size = max;
12174 left_size -= size;
12175 next_fragP->tc_frag_data.last_length = size;
12176 }
12177 }
12178 else
12179 next_fragP->tc_frag_data.last_length = 0;
12180 }
12181
12182 /* Check the padding size for the padding frag. */
12183 padding_size = i386_branch_padding_size
12184 (padding_fragP, (fragP->fr_address
12185 + fragP->tc_frag_data.padding_address));
12186
12187 last_size = fragP->tc_frag_data.prefix_length;
12188 /* Check if there is change from the last interation. */
12189 if (padding_size == last_size)
12190 {
12191 /* Update the expected address of the padding frag. */
12192 padding_fragP->tc_frag_data.padding_address
12193 = (fragP->fr_address + padding_size
12194 + fragP->tc_frag_data.padding_address);
12195 return 0;
12196 }
12197
12198 if (padding_size > fragP->tc_frag_data.max_prefix_length)
12199 {
12200 /* No padding if there is no sufficient room. Clear the
12201 expected address of the padding frag. */
12202 padding_fragP->tc_frag_data.padding_address = 0;
12203 padding_size = 0;
12204 }
12205 else
12206 /* Store the expected address of the padding frag. */
12207 padding_fragP->tc_frag_data.padding_address
12208 = (fragP->fr_address + padding_size
12209 + fragP->tc_frag_data.padding_address);
12210
12211 fragP->tc_frag_data.prefix_length = padding_size;
12212
12213 /* Update the length for the current interation. */
12214 left_size = padding_size;
12215 for (next_fragP = fragP;
12216 next_fragP != padding_fragP;
12217 next_fragP = next_fragP->fr_next)
12218 if (next_fragP->fr_type == rs_machine_dependent
12219 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12220 == BRANCH_PREFIX))
12221 {
12222 if (left_size)
12223 {
12224 int max = next_fragP->tc_frag_data.max_bytes;
12225 if (max)
12226 {
12227 int size;
12228 if (max > left_size)
12229 size = left_size;
12230 else
12231 size = max;
12232 left_size -= size;
12233 next_fragP->tc_frag_data.length = size;
12234 }
12235 }
12236 else
12237 next_fragP->tc_frag_data.length = 0;
12238 }
12239
12240 return (fragP->tc_frag_data.length
12241 - fragP->tc_frag_data.last_length);
12242 }
12243 return relax_frag (segment, fragP, stretch);
12244}
12245
ee7fcc42
AM
12246/* md_estimate_size_before_relax()
12247
12248 Called just before relax() for rs_machine_dependent frags. The x86
12249 assembler uses these frags to handle variable size jump
12250 instructions.
12251
12252 Any symbol that is now undefined will not become defined.
12253 Return the correct fr_subtype in the frag.
12254 Return the initial "guess for variable size of frag" to caller.
12255 The guess is actually the growth beyond the fixed part. Whatever
12256 we do to grow the fixed or variable part contributes to our
12257 returned value. */
12258
252b5132 12259int
7016a5d5 12260md_estimate_size_before_relax (fragS *fragP, segT segment)
252b5132 12261{
e379e5f3
L
12262 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12263 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX
12264 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
12265 {
12266 i386_classify_machine_dependent_frag (fragP);
12267 return fragP->tc_frag_data.length;
12268 }
12269
252b5132 12270 /* We've already got fragP->fr_subtype right; all we have to do is
b98ef147
AM
12271 check for un-relaxable symbols. On an ELF system, we can't relax
12272 an externally visible symbol, because it may be overridden by a
12273 shared library. */
12274 if (S_GET_SEGMENT (fragP->fr_symbol) != segment
6d249963 12275#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12276 || (IS_ELF
8dcea932
L
12277 && !elf_symbol_resolved_in_segment_p (fragP->fr_symbol,
12278 fragP->fr_var))
fbeb56a4
DK
12279#endif
12280#if defined (OBJ_COFF) && defined (TE_PE)
7ab9ffdd 12281 || (OUTPUT_FLAVOR == bfd_target_coff_flavour
fbeb56a4 12282 && S_IS_WEAK (fragP->fr_symbol))
b98ef147
AM
12283#endif
12284 )
252b5132 12285 {
b98ef147
AM
12286 /* Symbol is undefined in this segment, or we need to keep a
12287 reloc so that weak symbols can be overridden. */
12288 int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
f86103b7 12289 enum bfd_reloc_code_real reloc_type;
ee7fcc42
AM
12290 unsigned char *opcode;
12291 int old_fr_fix;
eb19308f 12292 fixS *fixP = NULL;
f6af82bd 12293
ee7fcc42 12294 if (fragP->fr_var != NO_RELOC)
1e9cc1c2 12295 reloc_type = (enum bfd_reloc_code_real) fragP->fr_var;
b98ef147 12296 else if (size == 2)
f6af82bd 12297 reloc_type = BFD_RELOC_16_PCREL;
bd7ab16b 12298#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1ef3994a
JB
12299 else if (fragP->tc_frag_data.code64 && fragP->fr_offset == 0
12300 && need_plt32_p (fragP->fr_symbol))
bd7ab16b
L
12301 reloc_type = BFD_RELOC_X86_64_PLT32;
12302#endif
f6af82bd
AM
12303 else
12304 reloc_type = BFD_RELOC_32_PCREL;
252b5132 12305
ee7fcc42
AM
12306 old_fr_fix = fragP->fr_fix;
12307 opcode = (unsigned char *) fragP->fr_opcode;
12308
fddf5b5b 12309 switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
252b5132 12310 {
fddf5b5b
AM
12311 case UNCOND_JUMP:
12312 /* Make jmp (0xeb) a (d)word displacement jump. */
47926f60 12313 opcode[0] = 0xe9;
252b5132 12314 fragP->fr_fix += size;
eb19308f
JB
12315 fixP = fix_new (fragP, old_fr_fix, size,
12316 fragP->fr_symbol,
12317 fragP->fr_offset, 1,
12318 reloc_type);
252b5132
RH
12319 break;
12320
fddf5b5b 12321 case COND_JUMP86:
412167cb
AM
12322 if (size == 2
12323 && (!no_cond_jump_promotion || fragP->fr_var != NO_RELOC))
fddf5b5b
AM
12324 {
12325 /* Negate the condition, and branch past an
12326 unconditional jump. */
12327 opcode[0] ^= 1;
12328 opcode[1] = 3;
12329 /* Insert an unconditional jump. */
12330 opcode[2] = 0xe9;
12331 /* We added two extra opcode bytes, and have a two byte
12332 offset. */
12333 fragP->fr_fix += 2 + 2;
062cd5e7
AS
12334 fix_new (fragP, old_fr_fix + 2, 2,
12335 fragP->fr_symbol,
12336 fragP->fr_offset, 1,
12337 reloc_type);
fddf5b5b
AM
12338 break;
12339 }
12340 /* Fall through. */
12341
12342 case COND_JUMP:
412167cb
AM
12343 if (no_cond_jump_promotion && fragP->fr_var == NO_RELOC)
12344 {
12345 fragP->fr_fix += 1;
3e02c1cc
AM
12346 fixP = fix_new (fragP, old_fr_fix, 1,
12347 fragP->fr_symbol,
12348 fragP->fr_offset, 1,
12349 BFD_RELOC_8_PCREL);
12350 fixP->fx_signed = 1;
412167cb
AM
12351 break;
12352 }
93c2a809 12353
24eab124 12354 /* This changes the byte-displacement jump 0x7N
fddf5b5b 12355 to the (d)word-displacement jump 0x0f,0x8N. */
252b5132 12356 opcode[1] = opcode[0] + 0x10;
f6af82bd 12357 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
47926f60
KH
12358 /* We've added an opcode byte. */
12359 fragP->fr_fix += 1 + size;
eb19308f
JB
12360 fixP = fix_new (fragP, old_fr_fix + 1, size,
12361 fragP->fr_symbol,
12362 fragP->fr_offset, 1,
12363 reloc_type);
252b5132 12364 break;
fddf5b5b
AM
12365
12366 default:
12367 BAD_CASE (fragP->fr_subtype);
12368 break;
252b5132 12369 }
eb19308f
JB
12370
12371 /* All jumps handled here are signed, but don't unconditionally use a
12372 signed limit check for 32 and 16 bit jumps as we want to allow wrap
12373 around at 4G (outside of 64-bit mode) and 64k. */
12374 if (size == 4 && flag_code == CODE_64BIT)
12375 fixP->fx_signed = 1;
12376
252b5132 12377 frag_wane (fragP);
ee7fcc42 12378 return fragP->fr_fix - old_fr_fix;
252b5132 12379 }
93c2a809 12380
93c2a809
AM
12381 /* Guess size depending on current relax state. Initially the relax
12382 state will correspond to a short jump and we return 1, because
12383 the variable part of the frag (the branch offset) is one byte
12384 long. However, we can relax a section more than once and in that
12385 case we must either set fr_subtype back to the unrelaxed state,
12386 or return the value for the appropriate branch. */
12387 return md_relax_table[fragP->fr_subtype].rlx_length;
ee7fcc42
AM
12388}
12389
47926f60
KH
12390/* Called after relax() is finished.
12391
12392 In: Address of frag.
12393 fr_type == rs_machine_dependent.
12394 fr_subtype is what the address relaxed to.
12395
12396 Out: Any fixSs and constants are set up.
12397 Caller will turn frag into a ".space 0". */
12398
252b5132 12399void
7016a5d5
TG
12400md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec ATTRIBUTE_UNUSED,
12401 fragS *fragP)
252b5132 12402{
29b0f896 12403 unsigned char *opcode;
252b5132 12404 unsigned char *where_to_put_displacement = NULL;
847f7ad4
AM
12405 offsetT target_address;
12406 offsetT opcode_address;
252b5132 12407 unsigned int extension = 0;
847f7ad4 12408 offsetT displacement_from_opcode_start;
252b5132 12409
e379e5f3
L
12410 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12411 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING
12412 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12413 {
12414 /* Generate nop padding. */
12415 unsigned int size = fragP->tc_frag_data.length;
12416 if (size)
12417 {
12418 if (size > fragP->tc_frag_data.max_bytes)
12419 abort ();
12420
12421 if (flag_debug)
12422 {
12423 const char *msg;
12424 const char *branch = "branch";
12425 const char *prefix = "";
12426 fragS *padding_fragP;
12427 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
12428 == BRANCH_PREFIX)
12429 {
12430 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
12431 switch (fragP->tc_frag_data.default_prefix)
12432 {
12433 default:
12434 abort ();
12435 break;
12436 case CS_PREFIX_OPCODE:
12437 prefix = " cs";
12438 break;
12439 case DS_PREFIX_OPCODE:
12440 prefix = " ds";
12441 break;
12442 case ES_PREFIX_OPCODE:
12443 prefix = " es";
12444 break;
12445 case FS_PREFIX_OPCODE:
12446 prefix = " fs";
12447 break;
12448 case GS_PREFIX_OPCODE:
12449 prefix = " gs";
12450 break;
12451 case SS_PREFIX_OPCODE:
12452 prefix = " ss";
12453 break;
12454 }
12455 if (padding_fragP)
12456 msg = _("%s:%u: add %d%s at 0x%llx to align "
12457 "%s within %d-byte boundary\n");
12458 else
12459 msg = _("%s:%u: add additional %d%s at 0x%llx to "
12460 "align %s within %d-byte boundary\n");
12461 }
12462 else
12463 {
12464 padding_fragP = fragP;
12465 msg = _("%s:%u: add %d%s-byte nop at 0x%llx to align "
12466 "%s within %d-byte boundary\n");
12467 }
12468
12469 if (padding_fragP)
12470 switch (padding_fragP->tc_frag_data.branch_type)
12471 {
12472 case align_branch_jcc:
12473 branch = "jcc";
12474 break;
12475 case align_branch_fused:
12476 branch = "fused jcc";
12477 break;
12478 case align_branch_jmp:
12479 branch = "jmp";
12480 break;
12481 case align_branch_call:
12482 branch = "call";
12483 break;
12484 case align_branch_indirect:
12485 branch = "indiret branch";
12486 break;
12487 case align_branch_ret:
12488 branch = "ret";
12489 break;
12490 default:
12491 break;
12492 }
12493
12494 fprintf (stdout, msg,
12495 fragP->fr_file, fragP->fr_line, size, prefix,
12496 (long long) fragP->fr_address, branch,
12497 1 << align_branch_power);
12498 }
12499 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12500 memset (fragP->fr_opcode,
12501 fragP->tc_frag_data.default_prefix, size);
12502 else
12503 i386_generate_nops (fragP, (char *) fragP->fr_opcode,
12504 size, 0);
12505 fragP->fr_fix += size;
12506 }
12507 return;
12508 }
12509
252b5132
RH
12510 opcode = (unsigned char *) fragP->fr_opcode;
12511
47926f60 12512 /* Address we want to reach in file space. */
252b5132 12513 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
252b5132 12514
47926f60 12515 /* Address opcode resides at in file space. */
252b5132
RH
12516 opcode_address = fragP->fr_address + fragP->fr_fix;
12517
47926f60 12518 /* Displacement from opcode start to fill into instruction. */
252b5132
RH
12519 displacement_from_opcode_start = target_address - opcode_address;
12520
fddf5b5b 12521 if ((fragP->fr_subtype & BIG) == 0)
252b5132 12522 {
47926f60
KH
12523 /* Don't have to change opcode. */
12524 extension = 1; /* 1 opcode + 1 displacement */
252b5132 12525 where_to_put_displacement = &opcode[1];
fddf5b5b
AM
12526 }
12527 else
12528 {
12529 if (no_cond_jump_promotion
12530 && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
4eed87de
AM
12531 as_warn_where (fragP->fr_file, fragP->fr_line,
12532 _("long jump required"));
252b5132 12533
fddf5b5b
AM
12534 switch (fragP->fr_subtype)
12535 {
12536 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
12537 extension = 4; /* 1 opcode + 4 displacement */
12538 opcode[0] = 0xe9;
12539 where_to_put_displacement = &opcode[1];
12540 break;
252b5132 12541
fddf5b5b
AM
12542 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
12543 extension = 2; /* 1 opcode + 2 displacement */
12544 opcode[0] = 0xe9;
12545 where_to_put_displacement = &opcode[1];
12546 break;
252b5132 12547
fddf5b5b
AM
12548 case ENCODE_RELAX_STATE (COND_JUMP, BIG):
12549 case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
12550 extension = 5; /* 2 opcode + 4 displacement */
12551 opcode[1] = opcode[0] + 0x10;
12552 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12553 where_to_put_displacement = &opcode[2];
12554 break;
252b5132 12555
fddf5b5b
AM
12556 case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
12557 extension = 3; /* 2 opcode + 2 displacement */
12558 opcode[1] = opcode[0] + 0x10;
12559 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12560 where_to_put_displacement = &opcode[2];
12561 break;
252b5132 12562
fddf5b5b
AM
12563 case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
12564 extension = 4;
12565 opcode[0] ^= 1;
12566 opcode[1] = 3;
12567 opcode[2] = 0xe9;
12568 where_to_put_displacement = &opcode[3];
12569 break;
12570
12571 default:
12572 BAD_CASE (fragP->fr_subtype);
12573 break;
12574 }
252b5132 12575 }
fddf5b5b 12576
7b81dfbb
AJ
12577 /* If size if less then four we are sure that the operand fits,
12578 but if it's 4, then it could be that the displacement is larger
12579 then -/+ 2GB. */
12580 if (DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype) == 4
12581 && object_64bit
12582 && ((addressT) (displacement_from_opcode_start - extension
4eed87de
AM
12583 + ((addressT) 1 << 31))
12584 > (((addressT) 2 << 31) - 1)))
7b81dfbb
AJ
12585 {
12586 as_bad_where (fragP->fr_file, fragP->fr_line,
12587 _("jump target out of range"));
12588 /* Make us emit 0. */
12589 displacement_from_opcode_start = extension;
12590 }
47926f60 12591 /* Now put displacement after opcode. */
252b5132
RH
12592 md_number_to_chars ((char *) where_to_put_displacement,
12593 (valueT) (displacement_from_opcode_start - extension),
fddf5b5b 12594 DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
252b5132
RH
12595 fragP->fr_fix += extension;
12596}
12597\f
7016a5d5 12598/* Apply a fixup (fixP) to segment data, once it has been determined
252b5132
RH
12599 by our caller that we have all the info we need to fix it up.
12600
7016a5d5
TG
12601 Parameter valP is the pointer to the value of the bits.
12602
252b5132
RH
12603 On the 386, immediates, displacements, and data pointers are all in
12604 the same (little-endian) format, so we don't need to care about which
12605 we are handling. */
12606
94f592af 12607void
7016a5d5 12608md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 12609{
94f592af 12610 char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
c6682705 12611 valueT value = *valP;
252b5132 12612
f86103b7 12613#if !defined (TE_Mach)
93382f6d
AM
12614 if (fixP->fx_pcrel)
12615 {
12616 switch (fixP->fx_r_type)
12617 {
5865bb77
ILT
12618 default:
12619 break;
12620
d6ab8113
JB
12621 case BFD_RELOC_64:
12622 fixP->fx_r_type = BFD_RELOC_64_PCREL;
12623 break;
93382f6d 12624 case BFD_RELOC_32:
ae8887b5 12625 case BFD_RELOC_X86_64_32S:
93382f6d
AM
12626 fixP->fx_r_type = BFD_RELOC_32_PCREL;
12627 break;
12628 case BFD_RELOC_16:
12629 fixP->fx_r_type = BFD_RELOC_16_PCREL;
12630 break;
12631 case BFD_RELOC_8:
12632 fixP->fx_r_type = BFD_RELOC_8_PCREL;
12633 break;
12634 }
12635 }
252b5132 12636
a161fe53 12637 if (fixP->fx_addsy != NULL
31312f95 12638 && (fixP->fx_r_type == BFD_RELOC_32_PCREL
d6ab8113 12639 || fixP->fx_r_type == BFD_RELOC_64_PCREL
31312f95 12640 || fixP->fx_r_type == BFD_RELOC_16_PCREL
d258b828 12641 || fixP->fx_r_type == BFD_RELOC_8_PCREL)
31312f95 12642 && !use_rela_relocations)
252b5132 12643 {
31312f95
AM
12644 /* This is a hack. There should be a better way to handle this.
12645 This covers for the fact that bfd_install_relocation will
12646 subtract the current location (for partial_inplace, PC relative
12647 relocations); see more below. */
252b5132 12648#ifndef OBJ_AOUT
718ddfc0 12649 if (IS_ELF
252b5132
RH
12650#ifdef TE_PE
12651 || OUTPUT_FLAVOR == bfd_target_coff_flavour
12652#endif
12653 )
12654 value += fixP->fx_where + fixP->fx_frag->fr_address;
12655#endif
12656#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12657 if (IS_ELF)
252b5132 12658 {
6539b54b 12659 segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
2f66722d 12660
6539b54b 12661 if ((sym_seg == seg
2f66722d 12662 || (symbol_section_p (fixP->fx_addsy)
6539b54b 12663 && sym_seg != absolute_section))
af65af87 12664 && !generic_force_reloc (fixP))
2f66722d
AM
12665 {
12666 /* Yes, we add the values in twice. This is because
6539b54b
AM
12667 bfd_install_relocation subtracts them out again. I think
12668 bfd_install_relocation is broken, but I don't dare change
2f66722d
AM
12669 it. FIXME. */
12670 value += fixP->fx_where + fixP->fx_frag->fr_address;
12671 }
252b5132
RH
12672 }
12673#endif
12674#if defined (OBJ_COFF) && defined (TE_PE)
977cdf5a
NC
12675 /* For some reason, the PE format does not store a
12676 section address offset for a PC relative symbol. */
12677 if (S_GET_SEGMENT (fixP->fx_addsy) != seg
7be1c489 12678 || S_IS_WEAK (fixP->fx_addsy))
252b5132
RH
12679 value += md_pcrel_from (fixP);
12680#endif
12681 }
fbeb56a4 12682#if defined (OBJ_COFF) && defined (TE_PE)
f01c1a09
NC
12683 if (fixP->fx_addsy != NULL
12684 && S_IS_WEAK (fixP->fx_addsy)
12685 /* PR 16858: Do not modify weak function references. */
12686 && ! fixP->fx_pcrel)
fbeb56a4 12687 {
296a8689
NC
12688#if !defined (TE_PEP)
12689 /* For x86 PE weak function symbols are neither PC-relative
12690 nor do they set S_IS_FUNCTION. So the only reliable way
12691 to detect them is to check the flags of their containing
12692 section. */
12693 if (S_GET_SEGMENT (fixP->fx_addsy) != NULL
12694 && S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_CODE)
12695 ;
12696 else
12697#endif
fbeb56a4
DK
12698 value -= S_GET_VALUE (fixP->fx_addsy);
12699 }
12700#endif
252b5132
RH
12701
12702 /* Fix a few things - the dynamic linker expects certain values here,
0234cb7c 12703 and we must not disappoint it. */
252b5132 12704#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12705 if (IS_ELF && fixP->fx_addsy)
47926f60
KH
12706 switch (fixP->fx_r_type)
12707 {
12708 case BFD_RELOC_386_PLT32:
3e73aa7c 12709 case BFD_RELOC_X86_64_PLT32:
b9519cfe
L
12710 /* Make the jump instruction point to the address of the operand.
12711 At runtime we merely add the offset to the actual PLT entry.
12712 NB: Subtract the offset size only for jump instructions. */
12713 if (fixP->fx_pcrel)
12714 value = -4;
47926f60 12715 break;
31312f95 12716
13ae64f3
JJ
12717 case BFD_RELOC_386_TLS_GD:
12718 case BFD_RELOC_386_TLS_LDM:
13ae64f3 12719 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
12720 case BFD_RELOC_386_TLS_IE:
12721 case BFD_RELOC_386_TLS_GOTIE:
67a4f2b7 12722 case BFD_RELOC_386_TLS_GOTDESC:
bffbf940
JJ
12723 case BFD_RELOC_X86_64_TLSGD:
12724 case BFD_RELOC_X86_64_TLSLD:
12725 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7 12726 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
00f7efb6
JJ
12727 value = 0; /* Fully resolved at runtime. No addend. */
12728 /* Fallthrough */
12729 case BFD_RELOC_386_TLS_LE:
12730 case BFD_RELOC_386_TLS_LDO_32:
12731 case BFD_RELOC_386_TLS_LE_32:
12732 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 12733 case BFD_RELOC_X86_64_DTPOFF64:
00f7efb6 12734 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113 12735 case BFD_RELOC_X86_64_TPOFF64:
00f7efb6
JJ
12736 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12737 break;
12738
67a4f2b7
AO
12739 case BFD_RELOC_386_TLS_DESC_CALL:
12740 case BFD_RELOC_X86_64_TLSDESC_CALL:
12741 value = 0; /* Fully resolved at runtime. No addend. */
12742 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12743 fixP->fx_done = 0;
12744 return;
12745
47926f60
KH
12746 case BFD_RELOC_VTABLE_INHERIT:
12747 case BFD_RELOC_VTABLE_ENTRY:
12748 fixP->fx_done = 0;
94f592af 12749 return;
47926f60
KH
12750
12751 default:
12752 break;
12753 }
12754#endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) */
a442cac5
JB
12755
12756 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
12757 if (!object_64bit)
12758 value = extend_to_32bit_address (value);
12759
c6682705 12760 *valP = value;
f86103b7 12761#endif /* !defined (TE_Mach) */
3e73aa7c 12762
3e73aa7c 12763 /* Are we finished with this relocation now? */
c6682705 12764 if (fixP->fx_addsy == NULL)
b8188555
JB
12765 {
12766 fixP->fx_done = 1;
12767 switch (fixP->fx_r_type)
12768 {
12769 case BFD_RELOC_X86_64_32S:
12770 fixP->fx_signed = 1;
12771 break;
12772
12773 default:
12774 break;
12775 }
12776 }
fbeb56a4
DK
12777#if defined (OBJ_COFF) && defined (TE_PE)
12778 else if (fixP->fx_addsy != NULL && S_IS_WEAK (fixP->fx_addsy))
12779 {
12780 fixP->fx_done = 0;
12781 /* Remember value for tc_gen_reloc. */
12782 fixP->fx_addnumber = value;
12783 /* Clear out the frag for now. */
12784 value = 0;
12785 }
12786#endif
3e73aa7c
JH
12787 else if (use_rela_relocations)
12788 {
12789 fixP->fx_no_overflow = 1;
062cd5e7
AS
12790 /* Remember value for tc_gen_reloc. */
12791 fixP->fx_addnumber = value;
3e73aa7c
JH
12792 value = 0;
12793 }
f86103b7 12794
94f592af 12795 md_number_to_chars (p, value, fixP->fx_size);
252b5132 12796}
252b5132 12797\f
6d4af3c2 12798const char *
499ac353 12799md_atof (int type, char *litP, int *sizeP)
252b5132 12800{
499ac353
NC
12801 /* This outputs the LITTLENUMs in REVERSE order;
12802 in accord with the bigendian 386. */
5b7c81bd 12803 return ieee_md_atof (type, litP, sizeP, false);
252b5132
RH
12804}
12805\f
2d545b82 12806static char output_invalid_buf[sizeof (unsigned char) * 2 + 6];
252b5132 12807
252b5132 12808static char *
e3bb37b5 12809output_invalid (int c)
252b5132 12810{
3882b010 12811 if (ISPRINT (c))
f9f21a03
L
12812 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
12813 "'%c'", c);
252b5132 12814 else
f9f21a03 12815 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
2d545b82 12816 "(0x%x)", (unsigned char) c);
252b5132
RH
12817 return output_invalid_buf;
12818}
12819
8a6fb3f9
JB
12820/* Verify that @r can be used in the current context. */
12821
5b7c81bd 12822static bool check_register (const reg_entry *r)
8a6fb3f9
JB
12823{
12824 if (allow_pseudo_reg)
5b7c81bd 12825 return true;
8a6fb3f9
JB
12826
12827 if (operand_type_all_zero (&r->reg_type))
5b7c81bd 12828 return false;
8a6fb3f9
JB
12829
12830 if ((r->reg_type.bitfield.dword
12831 || (r->reg_type.bitfield.class == SReg && r->reg_num > 3)
12832 || r->reg_type.bitfield.class == RegCR
22e00a3f 12833 || r->reg_type.bitfield.class == RegDR)
8a6fb3f9 12834 && !cpu_arch_flags.bitfield.cpui386)
5b7c81bd 12835 return false;
8a6fb3f9 12836
22e00a3f
JB
12837 if (r->reg_type.bitfield.class == RegTR
12838 && (flag_code == CODE_64BIT
12839 || !cpu_arch_flags.bitfield.cpui386
12840 || cpu_arch_isa_flags.bitfield.cpui586
12841 || cpu_arch_isa_flags.bitfield.cpui686))
5b7c81bd 12842 return false;
22e00a3f 12843
8a6fb3f9 12844 if (r->reg_type.bitfield.class == RegMMX && !cpu_arch_flags.bitfield.cpummx)
5b7c81bd 12845 return false;
8a6fb3f9
JB
12846
12847 if (!cpu_arch_flags.bitfield.cpuavx512f)
12848 {
12849 if (r->reg_type.bitfield.zmmword
12850 || r->reg_type.bitfield.class == RegMask)
5b7c81bd 12851 return false;
8a6fb3f9
JB
12852
12853 if (!cpu_arch_flags.bitfield.cpuavx)
12854 {
12855 if (r->reg_type.bitfield.ymmword)
5b7c81bd 12856 return false;
8a6fb3f9
JB
12857
12858 if (!cpu_arch_flags.bitfield.cpusse && r->reg_type.bitfield.xmmword)
5b7c81bd 12859 return false;
8a6fb3f9
JB
12860 }
12861 }
12862
260cd341
LC
12863 if (r->reg_type.bitfield.tmmword
12864 && (!cpu_arch_flags.bitfield.cpuamx_tile
12865 || flag_code != CODE_64BIT))
5b7c81bd 12866 return false;
260cd341 12867
8a6fb3f9 12868 if (r->reg_type.bitfield.class == RegBND && !cpu_arch_flags.bitfield.cpumpx)
5b7c81bd 12869 return false;
8a6fb3f9
JB
12870
12871 /* Don't allow fake index register unless allow_index_reg isn't 0. */
12872 if (!allow_index_reg && r->reg_num == RegIZ)
5b7c81bd 12873 return false;
8a6fb3f9
JB
12874
12875 /* Upper 16 vector registers are only available with VREX in 64bit
12876 mode, and require EVEX encoding. */
12877 if (r->reg_flags & RegVRex)
12878 {
12879 if (!cpu_arch_flags.bitfield.cpuavx512f
12880 || flag_code != CODE_64BIT)
5b7c81bd 12881 return false;
8a6fb3f9 12882
da4977e0
JB
12883 if (i.vec_encoding == vex_encoding_default)
12884 i.vec_encoding = vex_encoding_evex;
12885 else if (i.vec_encoding != vex_encoding_evex)
12886 i.vec_encoding = vex_encoding_error;
8a6fb3f9
JB
12887 }
12888
12889 if (((r->reg_flags & (RegRex64 | RegRex)) || r->reg_type.bitfield.qword)
12890 && (!cpu_arch_flags.bitfield.cpulm || r->reg_type.bitfield.class != RegCR)
12891 && flag_code != CODE_64BIT)
5b7c81bd 12892 return false;
8a6fb3f9
JB
12893
12894 if (r->reg_type.bitfield.class == SReg && r->reg_num == RegFlat
12895 && !intel_syntax)
5b7c81bd 12896 return false;
8a6fb3f9 12897
5b7c81bd 12898 return true;
8a6fb3f9
JB
12899}
12900
af6bdddf 12901/* REG_STRING starts *before* REGISTER_PREFIX. */
252b5132
RH
12902
12903static const reg_entry *
4d1bb795 12904parse_real_register (char *reg_string, char **end_op)
252b5132 12905{
af6bdddf
AM
12906 char *s = reg_string;
12907 char *p;
252b5132
RH
12908 char reg_name_given[MAX_REG_NAME_SIZE + 1];
12909 const reg_entry *r;
12910
12911 /* Skip possible REGISTER_PREFIX and possible whitespace. */
12912 if (*s == REGISTER_PREFIX)
12913 ++s;
12914
12915 if (is_space_char (*s))
12916 ++s;
12917
12918 p = reg_name_given;
af6bdddf 12919 while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
252b5132
RH
12920 {
12921 if (p >= reg_name_given + MAX_REG_NAME_SIZE)
af6bdddf
AM
12922 return (const reg_entry *) NULL;
12923 s++;
252b5132
RH
12924 }
12925
6588847e
DN
12926 /* For naked regs, make sure that we are not dealing with an identifier.
12927 This prevents confusing an identifier like `eax_var' with register
12928 `eax'. */
12929 if (allow_naked_reg && identifier_chars[(unsigned char) *s])
12930 return (const reg_entry *) NULL;
12931
af6bdddf 12932 *end_op = s;
252b5132 12933
629310ab 12934 r = (const reg_entry *) str_hash_find (reg_hash, reg_name_given);
252b5132 12935
5f47d35b 12936 /* Handle floating point regs, allowing spaces in the (i) part. */
6288d05f 12937 if (r == reg_st0)
5f47d35b 12938 {
0e0eea78
JB
12939 if (!cpu_arch_flags.bitfield.cpu8087
12940 && !cpu_arch_flags.bitfield.cpu287
af32b722
JB
12941 && !cpu_arch_flags.bitfield.cpu387
12942 && !allow_pseudo_reg)
0e0eea78
JB
12943 return (const reg_entry *) NULL;
12944
5f47d35b
AM
12945 if (is_space_char (*s))
12946 ++s;
12947 if (*s == '(')
12948 {
af6bdddf 12949 ++s;
5f47d35b
AM
12950 if (is_space_char (*s))
12951 ++s;
12952 if (*s >= '0' && *s <= '7')
12953 {
db557034 12954 int fpr = *s - '0';
af6bdddf 12955 ++s;
5f47d35b
AM
12956 if (is_space_char (*s))
12957 ++s;
12958 if (*s == ')')
12959 {
12960 *end_op = s + 1;
6288d05f 12961 know (r[fpr].reg_num == fpr);
db557034 12962 return r + fpr;
5f47d35b 12963 }
5f47d35b 12964 }
47926f60 12965 /* We have "%st(" then garbage. */
5f47d35b
AM
12966 return (const reg_entry *) NULL;
12967 }
12968 }
12969
8a6fb3f9 12970 return r && check_register (r) ? r : NULL;
252b5132 12971}
4d1bb795
JB
12972
12973/* REG_STRING starts *before* REGISTER_PREFIX. */
12974
12975static const reg_entry *
12976parse_register (char *reg_string, char **end_op)
12977{
12978 const reg_entry *r;
12979
12980 if (*reg_string == REGISTER_PREFIX || allow_naked_reg)
12981 r = parse_real_register (reg_string, end_op);
12982 else
12983 r = NULL;
12984 if (!r)
12985 {
12986 char *save = input_line_pointer;
12987 char c;
12988 symbolS *symbolP;
12989
12990 input_line_pointer = reg_string;
d02603dc 12991 c = get_symbol_name (&reg_string);
4d1bb795
JB
12992 symbolP = symbol_find (reg_string);
12993 if (symbolP && S_GET_SEGMENT (symbolP) == reg_section)
12994 {
12995 const expressionS *e = symbol_get_value_expression (symbolP);
12996
0398aac5 12997 know (e->X_op == O_register);
4eed87de 12998 know (e->X_add_number >= 0
c3fe08fa 12999 && (valueT) e->X_add_number < i386_regtab_size);
4d1bb795 13000 r = i386_regtab + e->X_add_number;
8a6fb3f9
JB
13001 if (!check_register (r))
13002 {
13003 as_bad (_("register '%s%s' cannot be used here"),
13004 register_prefix, r->reg_name);
13005 r = &bad_reg;
13006 }
4d1bb795
JB
13007 *end_op = input_line_pointer;
13008 }
13009 *input_line_pointer = c;
13010 input_line_pointer = save;
13011 }
13012 return r;
13013}
13014
13015int
13016i386_parse_name (char *name, expressionS *e, char *nextcharP)
13017{
13018 const reg_entry *r;
13019 char *end = input_line_pointer;
13020
13021 *end = *nextcharP;
13022 r = parse_register (name, &input_line_pointer);
13023 if (r && end <= input_line_pointer)
13024 {
13025 *nextcharP = *input_line_pointer;
13026 *input_line_pointer = 0;
8a6fb3f9
JB
13027 if (r != &bad_reg)
13028 {
13029 e->X_op = O_register;
13030 e->X_add_number = r - i386_regtab;
13031 }
13032 else
13033 e->X_op = O_illegal;
4d1bb795
JB
13034 return 1;
13035 }
13036 input_line_pointer = end;
13037 *end = 0;
ee86248c 13038 return intel_syntax ? i386_intel_parse_name (name, e) : 0;
4d1bb795
JB
13039}
13040
13041void
13042md_operand (expressionS *e)
13043{
ee86248c
JB
13044 char *end;
13045 const reg_entry *r;
4d1bb795 13046
ee86248c
JB
13047 switch (*input_line_pointer)
13048 {
13049 case REGISTER_PREFIX:
13050 r = parse_real_register (input_line_pointer, &end);
4d1bb795
JB
13051 if (r)
13052 {
13053 e->X_op = O_register;
13054 e->X_add_number = r - i386_regtab;
13055 input_line_pointer = end;
13056 }
ee86248c
JB
13057 break;
13058
13059 case '[':
9c2799c2 13060 gas_assert (intel_syntax);
ee86248c
JB
13061 end = input_line_pointer++;
13062 expression (e);
13063 if (*input_line_pointer == ']')
13064 {
13065 ++input_line_pointer;
13066 e->X_op_symbol = make_expr_symbol (e);
13067 e->X_add_symbol = NULL;
13068 e->X_add_number = 0;
13069 e->X_op = O_index;
13070 }
13071 else
13072 {
13073 e->X_op = O_absent;
13074 input_line_pointer = end;
13075 }
13076 break;
4d1bb795
JB
13077 }
13078}
13079
252b5132 13080\f
4cc782b5 13081#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
b6f8c7c4 13082const char *md_shortopts = "kVQ:sqnO::";
252b5132 13083#else
b6f8c7c4 13084const char *md_shortopts = "qnO::";
252b5132 13085#endif
6e0b89ee 13086
3e73aa7c 13087#define OPTION_32 (OPTION_MD_BASE + 0)
b3b91714
AM
13088#define OPTION_64 (OPTION_MD_BASE + 1)
13089#define OPTION_DIVIDE (OPTION_MD_BASE + 2)
9103f4f4
L
13090#define OPTION_MARCH (OPTION_MD_BASE + 3)
13091#define OPTION_MTUNE (OPTION_MD_BASE + 4)
1efbbeb4
L
13092#define OPTION_MMNEMONIC (OPTION_MD_BASE + 5)
13093#define OPTION_MSYNTAX (OPTION_MD_BASE + 6)
13094#define OPTION_MINDEX_REG (OPTION_MD_BASE + 7)
13095#define OPTION_MNAKED_REG (OPTION_MD_BASE + 8)
bd5dea88 13096#define OPTION_MRELAX_RELOCATIONS (OPTION_MD_BASE + 9)
c0f3af97 13097#define OPTION_MSSE2AVX (OPTION_MD_BASE + 10)
daf50ae7 13098#define OPTION_MSSE_CHECK (OPTION_MD_BASE + 11)
7bab8ab5
JB
13099#define OPTION_MOPERAND_CHECK (OPTION_MD_BASE + 12)
13100#define OPTION_MAVXSCALAR (OPTION_MD_BASE + 13)
13101#define OPTION_X32 (OPTION_MD_BASE + 14)
7e8b059b 13102#define OPTION_MADD_BND_PREFIX (OPTION_MD_BASE + 15)
43234a1e
L
13103#define OPTION_MEVEXLIG (OPTION_MD_BASE + 16)
13104#define OPTION_MEVEXWIG (OPTION_MD_BASE + 17)
167ad85b 13105#define OPTION_MBIG_OBJ (OPTION_MD_BASE + 18)
d1982f93 13106#define OPTION_MOMIT_LOCK_PREFIX (OPTION_MD_BASE + 19)
d3d3c6db 13107#define OPTION_MEVEXRCIG (OPTION_MD_BASE + 20)
8dcea932 13108#define OPTION_MSHARED (OPTION_MD_BASE + 21)
5db04b09
L
13109#define OPTION_MAMD64 (OPTION_MD_BASE + 22)
13110#define OPTION_MINTEL64 (OPTION_MD_BASE + 23)
e4e00185 13111#define OPTION_MFENCE_AS_LOCK_ADD (OPTION_MD_BASE + 24)
b4a3a7b4 13112#define OPTION_X86_USED_NOTE (OPTION_MD_BASE + 25)
03751133 13113#define OPTION_MVEXWIG (OPTION_MD_BASE + 26)
e379e5f3
L
13114#define OPTION_MALIGN_BRANCH_BOUNDARY (OPTION_MD_BASE + 27)
13115#define OPTION_MALIGN_BRANCH_PREFIX_SIZE (OPTION_MD_BASE + 28)
13116#define OPTION_MALIGN_BRANCH (OPTION_MD_BASE + 29)
76cf450b 13117#define OPTION_MBRANCHES_WITH_32B_BOUNDARIES (OPTION_MD_BASE + 30)
ae531041
L
13118#define OPTION_MLFENCE_AFTER_LOAD (OPTION_MD_BASE + 31)
13119#define OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH (OPTION_MD_BASE + 32)
13120#define OPTION_MLFENCE_BEFORE_RET (OPTION_MD_BASE + 33)
c8480b58 13121#define OPTION_MUSE_UNALIGNED_VECTOR_MOVE (OPTION_MD_BASE + 34)
b3b91714 13122
99ad8390
NC
13123struct option md_longopts[] =
13124{
3e73aa7c 13125 {"32", no_argument, NULL, OPTION_32},
321098a5 13126#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 13127 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c 13128 {"64", no_argument, NULL, OPTION_64},
351f65ca
L
13129#endif
13130#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 13131 {"x32", no_argument, NULL, OPTION_X32},
8dcea932 13132 {"mshared", no_argument, NULL, OPTION_MSHARED},
b4a3a7b4 13133 {"mx86-used-note", required_argument, NULL, OPTION_X86_USED_NOTE},
6e0b89ee 13134#endif
b3b91714 13135 {"divide", no_argument, NULL, OPTION_DIVIDE},
9103f4f4
L
13136 {"march", required_argument, NULL, OPTION_MARCH},
13137 {"mtune", required_argument, NULL, OPTION_MTUNE},
1efbbeb4
L
13138 {"mmnemonic", required_argument, NULL, OPTION_MMNEMONIC},
13139 {"msyntax", required_argument, NULL, OPTION_MSYNTAX},
13140 {"mindex-reg", no_argument, NULL, OPTION_MINDEX_REG},
13141 {"mnaked-reg", no_argument, NULL, OPTION_MNAKED_REG},
c0f3af97 13142 {"msse2avx", no_argument, NULL, OPTION_MSSE2AVX},
c8480b58 13143 {"muse-unaligned-vector-move", no_argument, NULL, OPTION_MUSE_UNALIGNED_VECTOR_MOVE},
daf50ae7 13144 {"msse-check", required_argument, NULL, OPTION_MSSE_CHECK},
7bab8ab5 13145 {"moperand-check", required_argument, NULL, OPTION_MOPERAND_CHECK},
539f890d 13146 {"mavxscalar", required_argument, NULL, OPTION_MAVXSCALAR},
03751133 13147 {"mvexwig", required_argument, NULL, OPTION_MVEXWIG},
7e8b059b 13148 {"madd-bnd-prefix", no_argument, NULL, OPTION_MADD_BND_PREFIX},
43234a1e
L
13149 {"mevexlig", required_argument, NULL, OPTION_MEVEXLIG},
13150 {"mevexwig", required_argument, NULL, OPTION_MEVEXWIG},
167ad85b
TG
13151# if defined (TE_PE) || defined (TE_PEP)
13152 {"mbig-obj", no_argument, NULL, OPTION_MBIG_OBJ},
13153#endif
d1982f93 13154 {"momit-lock-prefix", required_argument, NULL, OPTION_MOMIT_LOCK_PREFIX},
e4e00185 13155 {"mfence-as-lock-add", required_argument, NULL, OPTION_MFENCE_AS_LOCK_ADD},
0cb4071e 13156 {"mrelax-relocations", required_argument, NULL, OPTION_MRELAX_RELOCATIONS},
d3d3c6db 13157 {"mevexrcig", required_argument, NULL, OPTION_MEVEXRCIG},
e379e5f3
L
13158 {"malign-branch-boundary", required_argument, NULL, OPTION_MALIGN_BRANCH_BOUNDARY},
13159 {"malign-branch-prefix-size", required_argument, NULL, OPTION_MALIGN_BRANCH_PREFIX_SIZE},
13160 {"malign-branch", required_argument, NULL, OPTION_MALIGN_BRANCH},
76cf450b 13161 {"mbranches-within-32B-boundaries", no_argument, NULL, OPTION_MBRANCHES_WITH_32B_BOUNDARIES},
ae531041
L
13162 {"mlfence-after-load", required_argument, NULL, OPTION_MLFENCE_AFTER_LOAD},
13163 {"mlfence-before-indirect-branch", required_argument, NULL,
13164 OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH},
13165 {"mlfence-before-ret", required_argument, NULL, OPTION_MLFENCE_BEFORE_RET},
5db04b09
L
13166 {"mamd64", no_argument, NULL, OPTION_MAMD64},
13167 {"mintel64", no_argument, NULL, OPTION_MINTEL64},
252b5132
RH
13168 {NULL, no_argument, NULL, 0}
13169};
13170size_t md_longopts_size = sizeof (md_longopts);
13171
13172int
17b9d67d 13173md_parse_option (int c, const char *arg)
252b5132 13174{
91d6fa6a 13175 unsigned int j;
e379e5f3 13176 char *arch, *next, *saved, *type;
9103f4f4 13177
252b5132
RH
13178 switch (c)
13179 {
12b55ccc
L
13180 case 'n':
13181 optimize_align_code = 0;
13182 break;
13183
a38cf1db
AM
13184 case 'q':
13185 quiet_warnings = 1;
252b5132
RH
13186 break;
13187
13188#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
a38cf1db
AM
13189 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
13190 should be emitted or not. FIXME: Not implemented. */
13191 case 'Q':
d4693039
JB
13192 if ((arg[0] != 'y' && arg[0] != 'n') || arg[1])
13193 return 0;
252b5132
RH
13194 break;
13195
13196 /* -V: SVR4 argument to print version ID. */
13197 case 'V':
13198 print_version_id ();
13199 break;
13200
a38cf1db
AM
13201 /* -k: Ignore for FreeBSD compatibility. */
13202 case 'k':
252b5132 13203 break;
4cc782b5
ILT
13204
13205 case 's':
13206 /* -s: On i386 Solaris, this tells the native assembler to use
29b0f896 13207 .stab instead of .stab.excl. We always use .stab anyhow. */
4cc782b5 13208 break;
8dcea932
L
13209
13210 case OPTION_MSHARED:
13211 shared = 1;
13212 break;
b4a3a7b4
L
13213
13214 case OPTION_X86_USED_NOTE:
13215 if (strcasecmp (arg, "yes") == 0)
13216 x86_used_note = 1;
13217 else if (strcasecmp (arg, "no") == 0)
13218 x86_used_note = 0;
13219 else
13220 as_fatal (_("invalid -mx86-used-note= option: `%s'"), arg);
13221 break;
13222
13223
99ad8390 13224#endif
321098a5 13225#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 13226 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c
JH
13227 case OPTION_64:
13228 {
13229 const char **list, **l;
13230
3e73aa7c
JH
13231 list = bfd_target_list ();
13232 for (l = list; *l != NULL; l++)
08dedd66 13233 if (startswith (*l, "elf64-x86-64")
99ad8390
NC
13234 || strcmp (*l, "coff-x86-64") == 0
13235 || strcmp (*l, "pe-x86-64") == 0
d382c579
TG
13236 || strcmp (*l, "pei-x86-64") == 0
13237 || strcmp (*l, "mach-o-x86-64") == 0)
6e0b89ee
AM
13238 {
13239 default_arch = "x86_64";
13240 break;
13241 }
3e73aa7c 13242 if (*l == NULL)
2b5d6a91 13243 as_fatal (_("no compiled in support for x86_64"));
3e73aa7c
JH
13244 free (list);
13245 }
13246 break;
13247#endif
252b5132 13248
351f65ca 13249#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 13250 case OPTION_X32:
351f65ca
L
13251 if (IS_ELF)
13252 {
13253 const char **list, **l;
13254
13255 list = bfd_target_list ();
13256 for (l = list; *l != NULL; l++)
08dedd66 13257 if (startswith (*l, "elf32-x86-64"))
351f65ca
L
13258 {
13259 default_arch = "x86_64:32";
13260 break;
13261 }
13262 if (*l == NULL)
2b5d6a91 13263 as_fatal (_("no compiled in support for 32bit x86_64"));
351f65ca
L
13264 free (list);
13265 }
13266 else
13267 as_fatal (_("32bit x86_64 is only supported for ELF"));
13268 break;
13269#endif
13270
6e0b89ee
AM
13271 case OPTION_32:
13272 default_arch = "i386";
13273 break;
13274
b3b91714
AM
13275 case OPTION_DIVIDE:
13276#ifdef SVR4_COMMENT_CHARS
13277 {
13278 char *n, *t;
13279 const char *s;
13280
add39d23 13281 n = XNEWVEC (char, strlen (i386_comment_chars) + 1);
b3b91714
AM
13282 t = n;
13283 for (s = i386_comment_chars; *s != '\0'; s++)
13284 if (*s != '/')
13285 *t++ = *s;
13286 *t = '\0';
13287 i386_comment_chars = n;
13288 }
13289#endif
13290 break;
13291
9103f4f4 13292 case OPTION_MARCH:
293f5f65
L
13293 saved = xstrdup (arg);
13294 arch = saved;
13295 /* Allow -march=+nosse. */
13296 if (*arch == '+')
13297 arch++;
6305a203 13298 do
9103f4f4 13299 {
6305a203 13300 if (*arch == '.')
2b5d6a91 13301 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
13302 next = strchr (arch, '+');
13303 if (next)
13304 *next++ = '\0';
91d6fa6a 13305 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 13306 {
91d6fa6a 13307 if (strcmp (arch, cpu_arch [j].name) == 0)
ccc9c027 13308 {
6305a203 13309 /* Processor. */
1ded5609
JB
13310 if (! cpu_arch[j].flags.bitfield.cpui386)
13311 continue;
13312
91d6fa6a 13313 cpu_arch_name = cpu_arch[j].name;
6305a203 13314 cpu_sub_arch_name = NULL;
91d6fa6a
NC
13315 cpu_arch_flags = cpu_arch[j].flags;
13316 cpu_arch_isa = cpu_arch[j].type;
13317 cpu_arch_isa_flags = cpu_arch[j].flags;
6305a203
L
13318 if (!cpu_arch_tune_set)
13319 {
13320 cpu_arch_tune = cpu_arch_isa;
13321 cpu_arch_tune_flags = cpu_arch_isa_flags;
13322 }
13323 break;
13324 }
91d6fa6a
NC
13325 else if (*cpu_arch [j].name == '.'
13326 && strcmp (arch, cpu_arch [j].name + 1) == 0)
6305a203 13327 {
33eaf5de 13328 /* ISA extension. */
6305a203 13329 i386_cpu_flags flags;
309d3373 13330
293f5f65
L
13331 flags = cpu_flags_or (cpu_arch_flags,
13332 cpu_arch[j].flags);
81486035 13333
5b64d091 13334 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
6305a203
L
13335 {
13336 if (cpu_sub_arch_name)
13337 {
13338 char *name = cpu_sub_arch_name;
13339 cpu_sub_arch_name = concat (name,
91d6fa6a 13340 cpu_arch[j].name,
1bf57e9f 13341 (const char *) NULL);
6305a203
L
13342 free (name);
13343 }
13344 else
91d6fa6a 13345 cpu_sub_arch_name = xstrdup (cpu_arch[j].name);
6305a203 13346 cpu_arch_flags = flags;
a586129e 13347 cpu_arch_isa_flags = flags;
6305a203 13348 }
0089dace
L
13349 else
13350 cpu_arch_isa_flags
13351 = cpu_flags_or (cpu_arch_isa_flags,
13352 cpu_arch[j].flags);
6305a203 13353 break;
ccc9c027 13354 }
9103f4f4 13355 }
6305a203 13356
293f5f65
L
13357 if (j >= ARRAY_SIZE (cpu_arch))
13358 {
33eaf5de 13359 /* Disable an ISA extension. */
293f5f65
L
13360 for (j = 0; j < ARRAY_SIZE (cpu_noarch); j++)
13361 if (strcmp (arch, cpu_noarch [j].name) == 0)
13362 {
13363 i386_cpu_flags flags;
13364
13365 flags = cpu_flags_and_not (cpu_arch_flags,
13366 cpu_noarch[j].flags);
13367 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
13368 {
13369 if (cpu_sub_arch_name)
13370 {
13371 char *name = cpu_sub_arch_name;
13372 cpu_sub_arch_name = concat (arch,
13373 (const char *) NULL);
13374 free (name);
13375 }
13376 else
13377 cpu_sub_arch_name = xstrdup (arch);
13378 cpu_arch_flags = flags;
13379 cpu_arch_isa_flags = flags;
13380 }
13381 break;
13382 }
13383
13384 if (j >= ARRAY_SIZE (cpu_noarch))
13385 j = ARRAY_SIZE (cpu_arch);
13386 }
13387
91d6fa6a 13388 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 13389 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
13390
13391 arch = next;
9103f4f4 13392 }
293f5f65
L
13393 while (next != NULL);
13394 free (saved);
9103f4f4
L
13395 break;
13396
13397 case OPTION_MTUNE:
13398 if (*arg == '.')
2b5d6a91 13399 as_fatal (_("invalid -mtune= option: `%s'"), arg);
91d6fa6a 13400 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 13401 {
91d6fa6a 13402 if (strcmp (arg, cpu_arch [j].name) == 0)
9103f4f4 13403 {
ccc9c027 13404 cpu_arch_tune_set = 1;
91d6fa6a
NC
13405 cpu_arch_tune = cpu_arch [j].type;
13406 cpu_arch_tune_flags = cpu_arch[j].flags;
9103f4f4
L
13407 break;
13408 }
13409 }
91d6fa6a 13410 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 13411 as_fatal (_("invalid -mtune= option: `%s'"), arg);
9103f4f4
L
13412 break;
13413
1efbbeb4
L
13414 case OPTION_MMNEMONIC:
13415 if (strcasecmp (arg, "att") == 0)
13416 intel_mnemonic = 0;
13417 else if (strcasecmp (arg, "intel") == 0)
13418 intel_mnemonic = 1;
13419 else
2b5d6a91 13420 as_fatal (_("invalid -mmnemonic= option: `%s'"), arg);
1efbbeb4
L
13421 break;
13422
13423 case OPTION_MSYNTAX:
13424 if (strcasecmp (arg, "att") == 0)
13425 intel_syntax = 0;
13426 else if (strcasecmp (arg, "intel") == 0)
13427 intel_syntax = 1;
13428 else
2b5d6a91 13429 as_fatal (_("invalid -msyntax= option: `%s'"), arg);
1efbbeb4
L
13430 break;
13431
13432 case OPTION_MINDEX_REG:
13433 allow_index_reg = 1;
13434 break;
13435
13436 case OPTION_MNAKED_REG:
13437 allow_naked_reg = 1;
13438 break;
13439
c0f3af97
L
13440 case OPTION_MSSE2AVX:
13441 sse2avx = 1;
13442 break;
13443
c8480b58
L
13444 case OPTION_MUSE_UNALIGNED_VECTOR_MOVE:
13445 use_unaligned_vector_move = 1;
13446 break;
13447
daf50ae7
L
13448 case OPTION_MSSE_CHECK:
13449 if (strcasecmp (arg, "error") == 0)
7bab8ab5 13450 sse_check = check_error;
daf50ae7 13451 else if (strcasecmp (arg, "warning") == 0)
7bab8ab5 13452 sse_check = check_warning;
daf50ae7 13453 else if (strcasecmp (arg, "none") == 0)
7bab8ab5 13454 sse_check = check_none;
daf50ae7 13455 else
2b5d6a91 13456 as_fatal (_("invalid -msse-check= option: `%s'"), arg);
daf50ae7
L
13457 break;
13458
7bab8ab5
JB
13459 case OPTION_MOPERAND_CHECK:
13460 if (strcasecmp (arg, "error") == 0)
13461 operand_check = check_error;
13462 else if (strcasecmp (arg, "warning") == 0)
13463 operand_check = check_warning;
13464 else if (strcasecmp (arg, "none") == 0)
13465 operand_check = check_none;
13466 else
13467 as_fatal (_("invalid -moperand-check= option: `%s'"), arg);
13468 break;
13469
539f890d
L
13470 case OPTION_MAVXSCALAR:
13471 if (strcasecmp (arg, "128") == 0)
13472 avxscalar = vex128;
13473 else if (strcasecmp (arg, "256") == 0)
13474 avxscalar = vex256;
13475 else
2b5d6a91 13476 as_fatal (_("invalid -mavxscalar= option: `%s'"), arg);
539f890d
L
13477 break;
13478
03751133
L
13479 case OPTION_MVEXWIG:
13480 if (strcmp (arg, "0") == 0)
40c9c8de 13481 vexwig = vexw0;
03751133 13482 else if (strcmp (arg, "1") == 0)
40c9c8de 13483 vexwig = vexw1;
03751133
L
13484 else
13485 as_fatal (_("invalid -mvexwig= option: `%s'"), arg);
13486 break;
13487
7e8b059b
L
13488 case OPTION_MADD_BND_PREFIX:
13489 add_bnd_prefix = 1;
13490 break;
13491
43234a1e
L
13492 case OPTION_MEVEXLIG:
13493 if (strcmp (arg, "128") == 0)
13494 evexlig = evexl128;
13495 else if (strcmp (arg, "256") == 0)
13496 evexlig = evexl256;
13497 else if (strcmp (arg, "512") == 0)
13498 evexlig = evexl512;
13499 else
13500 as_fatal (_("invalid -mevexlig= option: `%s'"), arg);
13501 break;
13502
d3d3c6db
IT
13503 case OPTION_MEVEXRCIG:
13504 if (strcmp (arg, "rne") == 0)
13505 evexrcig = rne;
13506 else if (strcmp (arg, "rd") == 0)
13507 evexrcig = rd;
13508 else if (strcmp (arg, "ru") == 0)
13509 evexrcig = ru;
13510 else if (strcmp (arg, "rz") == 0)
13511 evexrcig = rz;
13512 else
13513 as_fatal (_("invalid -mevexrcig= option: `%s'"), arg);
13514 break;
13515
43234a1e
L
13516 case OPTION_MEVEXWIG:
13517 if (strcmp (arg, "0") == 0)
13518 evexwig = evexw0;
13519 else if (strcmp (arg, "1") == 0)
13520 evexwig = evexw1;
13521 else
13522 as_fatal (_("invalid -mevexwig= option: `%s'"), arg);
13523 break;
13524
167ad85b
TG
13525# if defined (TE_PE) || defined (TE_PEP)
13526 case OPTION_MBIG_OBJ:
13527 use_big_obj = 1;
13528 break;
13529#endif
13530
d1982f93 13531 case OPTION_MOMIT_LOCK_PREFIX:
d022bddd
IT
13532 if (strcasecmp (arg, "yes") == 0)
13533 omit_lock_prefix = 1;
13534 else if (strcasecmp (arg, "no") == 0)
13535 omit_lock_prefix = 0;
13536 else
13537 as_fatal (_("invalid -momit-lock-prefix= option: `%s'"), arg);
13538 break;
13539
e4e00185
AS
13540 case OPTION_MFENCE_AS_LOCK_ADD:
13541 if (strcasecmp (arg, "yes") == 0)
13542 avoid_fence = 1;
13543 else if (strcasecmp (arg, "no") == 0)
13544 avoid_fence = 0;
13545 else
13546 as_fatal (_("invalid -mfence-as-lock-add= option: `%s'"), arg);
13547 break;
13548
ae531041
L
13549 case OPTION_MLFENCE_AFTER_LOAD:
13550 if (strcasecmp (arg, "yes") == 0)
13551 lfence_after_load = 1;
13552 else if (strcasecmp (arg, "no") == 0)
13553 lfence_after_load = 0;
13554 else
13555 as_fatal (_("invalid -mlfence-after-load= option: `%s'"), arg);
13556 break;
13557
13558 case OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH:
13559 if (strcasecmp (arg, "all") == 0)
a09f656b 13560 {
13561 lfence_before_indirect_branch = lfence_branch_all;
13562 if (lfence_before_ret == lfence_before_ret_none)
13563 lfence_before_ret = lfence_before_ret_shl;
13564 }
ae531041
L
13565 else if (strcasecmp (arg, "memory") == 0)
13566 lfence_before_indirect_branch = lfence_branch_memory;
13567 else if (strcasecmp (arg, "register") == 0)
13568 lfence_before_indirect_branch = lfence_branch_register;
13569 else if (strcasecmp (arg, "none") == 0)
13570 lfence_before_indirect_branch = lfence_branch_none;
13571 else
13572 as_fatal (_("invalid -mlfence-before-indirect-branch= option: `%s'"),
13573 arg);
13574 break;
13575
13576 case OPTION_MLFENCE_BEFORE_RET:
13577 if (strcasecmp (arg, "or") == 0)
13578 lfence_before_ret = lfence_before_ret_or;
13579 else if (strcasecmp (arg, "not") == 0)
13580 lfence_before_ret = lfence_before_ret_not;
a09f656b 13581 else if (strcasecmp (arg, "shl") == 0 || strcasecmp (arg, "yes") == 0)
13582 lfence_before_ret = lfence_before_ret_shl;
ae531041
L
13583 else if (strcasecmp (arg, "none") == 0)
13584 lfence_before_ret = lfence_before_ret_none;
13585 else
13586 as_fatal (_("invalid -mlfence-before-ret= option: `%s'"),
13587 arg);
13588 break;
13589
0cb4071e
L
13590 case OPTION_MRELAX_RELOCATIONS:
13591 if (strcasecmp (arg, "yes") == 0)
13592 generate_relax_relocations = 1;
13593 else if (strcasecmp (arg, "no") == 0)
13594 generate_relax_relocations = 0;
13595 else
13596 as_fatal (_("invalid -mrelax-relocations= option: `%s'"), arg);
13597 break;
13598
e379e5f3
L
13599 case OPTION_MALIGN_BRANCH_BOUNDARY:
13600 {
13601 char *end;
13602 long int align = strtoul (arg, &end, 0);
13603 if (*end == '\0')
13604 {
13605 if (align == 0)
13606 {
13607 align_branch_power = 0;
13608 break;
13609 }
13610 else if (align >= 16)
13611 {
13612 int align_power;
13613 for (align_power = 0;
13614 (align & 1) == 0;
13615 align >>= 1, align_power++)
13616 continue;
13617 /* Limit alignment power to 31. */
13618 if (align == 1 && align_power < 32)
13619 {
13620 align_branch_power = align_power;
13621 break;
13622 }
13623 }
13624 }
13625 as_fatal (_("invalid -malign-branch-boundary= value: %s"), arg);
13626 }
13627 break;
13628
13629 case OPTION_MALIGN_BRANCH_PREFIX_SIZE:
13630 {
13631 char *end;
13632 int align = strtoul (arg, &end, 0);
13633 /* Some processors only support 5 prefixes. */
13634 if (*end == '\0' && align >= 0 && align < 6)
13635 {
13636 align_branch_prefix_size = align;
13637 break;
13638 }
13639 as_fatal (_("invalid -malign-branch-prefix-size= value: %s"),
13640 arg);
13641 }
13642 break;
13643
13644 case OPTION_MALIGN_BRANCH:
13645 align_branch = 0;
13646 saved = xstrdup (arg);
13647 type = saved;
13648 do
13649 {
13650 next = strchr (type, '+');
13651 if (next)
13652 *next++ = '\0';
13653 if (strcasecmp (type, "jcc") == 0)
13654 align_branch |= align_branch_jcc_bit;
13655 else if (strcasecmp (type, "fused") == 0)
13656 align_branch |= align_branch_fused_bit;
13657 else if (strcasecmp (type, "jmp") == 0)
13658 align_branch |= align_branch_jmp_bit;
13659 else if (strcasecmp (type, "call") == 0)
13660 align_branch |= align_branch_call_bit;
13661 else if (strcasecmp (type, "ret") == 0)
13662 align_branch |= align_branch_ret_bit;
13663 else if (strcasecmp (type, "indirect") == 0)
13664 align_branch |= align_branch_indirect_bit;
13665 else
13666 as_fatal (_("invalid -malign-branch= option: `%s'"), arg);
13667 type = next;
13668 }
13669 while (next != NULL);
13670 free (saved);
13671 break;
13672
76cf450b
L
13673 case OPTION_MBRANCHES_WITH_32B_BOUNDARIES:
13674 align_branch_power = 5;
13675 align_branch_prefix_size = 5;
13676 align_branch = (align_branch_jcc_bit
13677 | align_branch_fused_bit
13678 | align_branch_jmp_bit);
13679 break;
13680
5db04b09 13681 case OPTION_MAMD64:
4b5aaf5f 13682 isa64 = amd64;
5db04b09
L
13683 break;
13684
13685 case OPTION_MINTEL64:
4b5aaf5f 13686 isa64 = intel64;
5db04b09
L
13687 break;
13688
b6f8c7c4
L
13689 case 'O':
13690 if (arg == NULL)
13691 {
13692 optimize = 1;
13693 /* Turn off -Os. */
13694 optimize_for_space = 0;
13695 }
13696 else if (*arg == 's')
13697 {
13698 optimize_for_space = 1;
13699 /* Turn on all encoding optimizations. */
41fd2579 13700 optimize = INT_MAX;
b6f8c7c4
L
13701 }
13702 else
13703 {
13704 optimize = atoi (arg);
13705 /* Turn off -Os. */
13706 optimize_for_space = 0;
13707 }
13708 break;
13709
252b5132
RH
13710 default:
13711 return 0;
13712 }
13713 return 1;
13714}
13715
8a2c8fef
L
13716#define MESSAGE_TEMPLATE \
13717" "
13718
293f5f65
L
13719static char *
13720output_message (FILE *stream, char *p, char *message, char *start,
13721 int *left_p, const char *name, int len)
13722{
13723 int size = sizeof (MESSAGE_TEMPLATE);
13724 int left = *left_p;
13725
13726 /* Reserve 2 spaces for ", " or ",\0" */
13727 left -= len + 2;
13728
13729 /* Check if there is any room. */
13730 if (left >= 0)
13731 {
13732 if (p != start)
13733 {
13734 *p++ = ',';
13735 *p++ = ' ';
13736 }
13737 p = mempcpy (p, name, len);
13738 }
13739 else
13740 {
13741 /* Output the current message now and start a new one. */
13742 *p++ = ',';
13743 *p = '\0';
13744 fprintf (stream, "%s\n", message);
13745 p = start;
13746 left = size - (start - message) - len - 2;
13747
13748 gas_assert (left >= 0);
13749
13750 p = mempcpy (p, name, len);
13751 }
13752
13753 *left_p = left;
13754 return p;
13755}
13756
8a2c8fef 13757static void
1ded5609 13758show_arch (FILE *stream, int ext, int check)
8a2c8fef
L
13759{
13760 static char message[] = MESSAGE_TEMPLATE;
13761 char *start = message + 27;
13762 char *p;
13763 int size = sizeof (MESSAGE_TEMPLATE);
13764 int left;
13765 const char *name;
13766 int len;
13767 unsigned int j;
13768
13769 p = start;
13770 left = size - (start - message);
13771 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13772 {
13773 /* Should it be skipped? */
13774 if (cpu_arch [j].skip)
13775 continue;
13776
13777 name = cpu_arch [j].name;
13778 len = cpu_arch [j].len;
13779 if (*name == '.')
13780 {
13781 /* It is an extension. Skip if we aren't asked to show it. */
13782 if (ext)
13783 {
13784 name++;
13785 len--;
13786 }
13787 else
13788 continue;
13789 }
13790 else if (ext)
13791 {
13792 /* It is an processor. Skip if we show only extension. */
13793 continue;
13794 }
1ded5609
JB
13795 else if (check && ! cpu_arch[j].flags.bitfield.cpui386)
13796 {
13797 /* It is an impossible processor - skip. */
13798 continue;
13799 }
8a2c8fef 13800
293f5f65 13801 p = output_message (stream, p, message, start, &left, name, len);
8a2c8fef
L
13802 }
13803
293f5f65
L
13804 /* Display disabled extensions. */
13805 if (ext)
13806 for (j = 0; j < ARRAY_SIZE (cpu_noarch); j++)
13807 {
13808 name = cpu_noarch [j].name;
13809 len = cpu_noarch [j].len;
13810 p = output_message (stream, p, message, start, &left, name,
13811 len);
13812 }
13813
8a2c8fef
L
13814 *p = '\0';
13815 fprintf (stream, "%s\n", message);
13816}
13817
252b5132 13818void
8a2c8fef 13819md_show_usage (FILE *stream)
252b5132 13820{
4cc782b5
ILT
13821#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13822 fprintf (stream, _("\
d4693039 13823 -Qy, -Qn ignored\n\
a38cf1db 13824 -V print assembler version number\n\
b3b91714
AM
13825 -k ignored\n"));
13826#endif
13827 fprintf (stream, _("\
12b55ccc 13828 -n Do not optimize code alignment\n\
b3b91714
AM
13829 -q quieten some warnings\n"));
13830#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13831 fprintf (stream, _("\
a38cf1db 13832 -s ignored\n"));
b3b91714 13833#endif
b00af7c8
JB
13834#ifdef BFD64
13835# if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13836 fprintf (stream, _("\
13837 --32/--64/--x32 generate 32bit/64bit/x32 object\n"));
13838# elif defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O)
751d281c 13839 fprintf (stream, _("\
b00af7c8
JB
13840 --32/--64 generate 32bit/64bit object\n"));
13841# endif
751d281c 13842#endif
b3b91714
AM
13843#ifdef SVR4_COMMENT_CHARS
13844 fprintf (stream, _("\
13845 --divide do not treat `/' as a comment character\n"));
a38cf1db
AM
13846#else
13847 fprintf (stream, _("\
b3b91714 13848 --divide ignored\n"));
4cc782b5 13849#endif
9103f4f4 13850 fprintf (stream, _("\
6305a203 13851 -march=CPU[,+EXTENSION...]\n\
8a2c8fef 13852 generate code for CPU and EXTENSION, CPU is one of:\n"));
1ded5609 13853 show_arch (stream, 0, 1);
8a2c8fef
L
13854 fprintf (stream, _("\
13855 EXTENSION is combination of:\n"));
1ded5609 13856 show_arch (stream, 1, 0);
6305a203 13857 fprintf (stream, _("\
8a2c8fef 13858 -mtune=CPU optimize for CPU, CPU is one of:\n"));
1ded5609 13859 show_arch (stream, 0, 0);
ba104c83 13860 fprintf (stream, _("\
c0f3af97
L
13861 -msse2avx encode SSE instructions with VEX prefix\n"));
13862 fprintf (stream, _("\
c8480b58
L
13863 -muse-unaligned-vector-move\n\
13864 encode aligned vector move as unaligned vector move\n"));
13865 fprintf (stream, _("\
7c5c05ef 13866 -msse-check=[none|error|warning] (default: warning)\n\
daf50ae7
L
13867 check SSE instructions\n"));
13868 fprintf (stream, _("\
7c5c05ef 13869 -moperand-check=[none|error|warning] (default: warning)\n\
7bab8ab5
JB
13870 check operand combinations for validity\n"));
13871 fprintf (stream, _("\
7c5c05ef
L
13872 -mavxscalar=[128|256] (default: 128)\n\
13873 encode scalar AVX instructions with specific vector\n\
539f890d
L
13874 length\n"));
13875 fprintf (stream, _("\
03751133
L
13876 -mvexwig=[0|1] (default: 0)\n\
13877 encode VEX instructions with specific VEX.W value\n\
13878 for VEX.W bit ignored instructions\n"));
13879 fprintf (stream, _("\
7c5c05ef
L
13880 -mevexlig=[128|256|512] (default: 128)\n\
13881 encode scalar EVEX instructions with specific vector\n\
43234a1e
L
13882 length\n"));
13883 fprintf (stream, _("\
7c5c05ef
L
13884 -mevexwig=[0|1] (default: 0)\n\
13885 encode EVEX instructions with specific EVEX.W value\n\
43234a1e
L
13886 for EVEX.W bit ignored instructions\n"));
13887 fprintf (stream, _("\
7c5c05ef 13888 -mevexrcig=[rne|rd|ru|rz] (default: rne)\n\
d3d3c6db
IT
13889 encode EVEX instructions with specific EVEX.RC value\n\
13890 for SAE-only ignored instructions\n"));
13891 fprintf (stream, _("\
7c5c05ef
L
13892 -mmnemonic=[att|intel] "));
13893 if (SYSV386_COMPAT)
13894 fprintf (stream, _("(default: att)\n"));
13895 else
13896 fprintf (stream, _("(default: intel)\n"));
13897 fprintf (stream, _("\
13898 use AT&T/Intel mnemonic\n"));
ba104c83 13899 fprintf (stream, _("\
7c5c05ef
L
13900 -msyntax=[att|intel] (default: att)\n\
13901 use AT&T/Intel syntax\n"));
ba104c83
L
13902 fprintf (stream, _("\
13903 -mindex-reg support pseudo index registers\n"));
13904 fprintf (stream, _("\
13905 -mnaked-reg don't require `%%' prefix for registers\n"));
13906 fprintf (stream, _("\
7e8b059b 13907 -madd-bnd-prefix add BND prefix for all valid branches\n"));
b4a3a7b4 13908#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8dcea932
L
13909 fprintf (stream, _("\
13910 -mshared disable branch optimization for shared code\n"));
b4a3a7b4
L
13911 fprintf (stream, _("\
13912 -mx86-used-note=[no|yes] "));
13913 if (DEFAULT_X86_USED_NOTE)
13914 fprintf (stream, _("(default: yes)\n"));
13915 else
13916 fprintf (stream, _("(default: no)\n"));
13917 fprintf (stream, _("\
13918 generate x86 used ISA and feature properties\n"));
13919#endif
13920#if defined (TE_PE) || defined (TE_PEP)
167ad85b
TG
13921 fprintf (stream, _("\
13922 -mbig-obj generate big object files\n"));
13923#endif
d022bddd 13924 fprintf (stream, _("\
7c5c05ef 13925 -momit-lock-prefix=[no|yes] (default: no)\n\
d022bddd 13926 strip all lock prefixes\n"));
5db04b09 13927 fprintf (stream, _("\
7c5c05ef 13928 -mfence-as-lock-add=[no|yes] (default: no)\n\
e4e00185
AS
13929 encode lfence, mfence and sfence as\n\
13930 lock addl $0x0, (%%{re}sp)\n"));
13931 fprintf (stream, _("\
7c5c05ef
L
13932 -mrelax-relocations=[no|yes] "));
13933 if (DEFAULT_GENERATE_X86_RELAX_RELOCATIONS)
13934 fprintf (stream, _("(default: yes)\n"));
13935 else
13936 fprintf (stream, _("(default: no)\n"));
13937 fprintf (stream, _("\
0cb4071e
L
13938 generate relax relocations\n"));
13939 fprintf (stream, _("\
e379e5f3
L
13940 -malign-branch-boundary=NUM (default: 0)\n\
13941 align branches within NUM byte boundary\n"));
13942 fprintf (stream, _("\
13943 -malign-branch=TYPE[+TYPE...] (default: jcc+fused+jmp)\n\
13944 TYPE is combination of jcc, fused, jmp, call, ret,\n\
13945 indirect\n\
13946 specify types of branches to align\n"));
13947 fprintf (stream, _("\
13948 -malign-branch-prefix-size=NUM (default: 5)\n\
13949 align branches with NUM prefixes per instruction\n"));
13950 fprintf (stream, _("\
76cf450b
L
13951 -mbranches-within-32B-boundaries\n\
13952 align branches within 32 byte boundary\n"));
13953 fprintf (stream, _("\
ae531041
L
13954 -mlfence-after-load=[no|yes] (default: no)\n\
13955 generate lfence after load\n"));
13956 fprintf (stream, _("\
13957 -mlfence-before-indirect-branch=[none|all|register|memory] (default: none)\n\
13958 generate lfence before indirect near branch\n"));
13959 fprintf (stream, _("\
a09f656b 13960 -mlfence-before-ret=[none|or|not|shl|yes] (default: none)\n\
ae531041
L
13961 generate lfence before ret\n"));
13962 fprintf (stream, _("\
7c5c05ef 13963 -mamd64 accept only AMD64 ISA [default]\n"));
5db04b09
L
13964 fprintf (stream, _("\
13965 -mintel64 accept only Intel64 ISA\n"));
252b5132
RH
13966}
13967
3e73aa7c 13968#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
321098a5 13969 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
e57f8c65 13970 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
252b5132
RH
13971
13972/* Pick the target format to use. */
13973
47926f60 13974const char *
e3bb37b5 13975i386_target_format (void)
252b5132 13976{
d34049e8 13977 if (startswith (default_arch, "x86_64"))
351f65ca
L
13978 {
13979 update_code_flag (CODE_64BIT, 1);
13980 if (default_arch[6] == '\0')
7f56bc95 13981 x86_elf_abi = X86_64_ABI;
351f65ca 13982 else
7f56bc95 13983 x86_elf_abi = X86_64_X32_ABI;
351f65ca 13984 }
3e73aa7c 13985 else if (!strcmp (default_arch, "i386"))
78f12dd3 13986 update_code_flag (CODE_32BIT, 1);
5197d474
L
13987 else if (!strcmp (default_arch, "iamcu"))
13988 {
13989 update_code_flag (CODE_32BIT, 1);
13990 if (cpu_arch_isa == PROCESSOR_UNKNOWN)
13991 {
13992 static const i386_cpu_flags iamcu_flags = CPU_IAMCU_FLAGS;
13993 cpu_arch_name = "iamcu";
13994 cpu_sub_arch_name = NULL;
13995 cpu_arch_flags = iamcu_flags;
13996 cpu_arch_isa = PROCESSOR_IAMCU;
13997 cpu_arch_isa_flags = iamcu_flags;
13998 if (!cpu_arch_tune_set)
13999 {
14000 cpu_arch_tune = cpu_arch_isa;
14001 cpu_arch_tune_flags = cpu_arch_isa_flags;
14002 }
14003 }
8d471ec1 14004 else if (cpu_arch_isa != PROCESSOR_IAMCU)
5197d474
L
14005 as_fatal (_("Intel MCU doesn't support `%s' architecture"),
14006 cpu_arch_name);
14007 }
3e73aa7c 14008 else
2b5d6a91 14009 as_fatal (_("unknown architecture"));
89507696
JB
14010
14011 if (cpu_flags_all_zero (&cpu_arch_isa_flags))
14012 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].flags;
14013 if (cpu_flags_all_zero (&cpu_arch_tune_flags))
14014 cpu_arch_tune_flags = cpu_arch[flag_code == CODE_64BIT].flags;
14015
252b5132
RH
14016 switch (OUTPUT_FLAVOR)
14017 {
9384f2ff 14018#if defined (OBJ_MAYBE_AOUT) || defined (OBJ_AOUT)
4c63da97 14019 case bfd_target_aout_flavour:
47926f60 14020 return AOUT_TARGET_FORMAT;
4c63da97 14021#endif
9384f2ff
AM
14022#if defined (OBJ_MAYBE_COFF) || defined (OBJ_COFF)
14023# if defined (TE_PE) || defined (TE_PEP)
14024 case bfd_target_coff_flavour:
167ad85b 14025 if (flag_code == CODE_64BIT)
eb19308f
JB
14026 {
14027 object_64bit = 1;
14028 return use_big_obj ? "pe-bigobj-x86-64" : "pe-x86-64";
14029 }
14030 return use_big_obj ? "pe-bigobj-i386" : "pe-i386";
9384f2ff 14031# elif defined (TE_GO32)
0561d57c
JK
14032 case bfd_target_coff_flavour:
14033 return "coff-go32";
9384f2ff 14034# else
252b5132
RH
14035 case bfd_target_coff_flavour:
14036 return "coff-i386";
9384f2ff 14037# endif
4c63da97 14038#endif
3e73aa7c 14039#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
252b5132 14040 case bfd_target_elf_flavour:
3e73aa7c 14041 {
351f65ca
L
14042 const char *format;
14043
14044 switch (x86_elf_abi)
4fa24527 14045 {
351f65ca
L
14046 default:
14047 format = ELF_TARGET_FORMAT;
e379e5f3
L
14048#ifndef TE_SOLARIS
14049 tls_get_addr = "___tls_get_addr";
14050#endif
351f65ca 14051 break;
7f56bc95 14052 case X86_64_ABI:
351f65ca 14053 use_rela_relocations = 1;
4fa24527 14054 object_64bit = 1;
e379e5f3
L
14055#ifndef TE_SOLARIS
14056 tls_get_addr = "__tls_get_addr";
14057#endif
351f65ca
L
14058 format = ELF_TARGET_FORMAT64;
14059 break;
7f56bc95 14060 case X86_64_X32_ABI:
4fa24527 14061 use_rela_relocations = 1;
351f65ca 14062 object_64bit = 1;
e379e5f3
L
14063#ifndef TE_SOLARIS
14064 tls_get_addr = "__tls_get_addr";
14065#endif
862be3fb 14066 disallow_64bit_reloc = 1;
351f65ca
L
14067 format = ELF_TARGET_FORMAT32;
14068 break;
4fa24527 14069 }
3632d14b 14070 if (cpu_arch_isa == PROCESSOR_L1OM)
8a9036a4 14071 {
7f56bc95 14072 if (x86_elf_abi != X86_64_ABI)
8a9036a4
L
14073 as_fatal (_("Intel L1OM is 64bit only"));
14074 return ELF_TARGET_L1OM_FORMAT;
14075 }
b49f93f6 14076 else if (cpu_arch_isa == PROCESSOR_K1OM)
7a9068fe
L
14077 {
14078 if (x86_elf_abi != X86_64_ABI)
14079 as_fatal (_("Intel K1OM is 64bit only"));
14080 return ELF_TARGET_K1OM_FORMAT;
14081 }
81486035
L
14082 else if (cpu_arch_isa == PROCESSOR_IAMCU)
14083 {
14084 if (x86_elf_abi != I386_ABI)
14085 as_fatal (_("Intel MCU is 32bit only"));
14086 return ELF_TARGET_IAMCU_FORMAT;
14087 }
8a9036a4 14088 else
351f65ca 14089 return format;
3e73aa7c 14090 }
e57f8c65
TG
14091#endif
14092#if defined (OBJ_MACH_O)
14093 case bfd_target_mach_o_flavour:
d382c579
TG
14094 if (flag_code == CODE_64BIT)
14095 {
14096 use_rela_relocations = 1;
14097 object_64bit = 1;
14098 return "mach-o-x86-64";
14099 }
14100 else
14101 return "mach-o-i386";
4c63da97 14102#endif
252b5132
RH
14103 default:
14104 abort ();
14105 return NULL;
14106 }
14107}
14108
47926f60 14109#endif /* OBJ_MAYBE_ more than one */
252b5132 14110\f
252b5132 14111symbolS *
7016a5d5 14112md_undefined_symbol (char *name)
252b5132 14113{
18dc2407
ILT
14114 if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
14115 && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
14116 && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
14117 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
24eab124
AM
14118 {
14119 if (!GOT_symbol)
14120 {
14121 if (symbol_find (name))
14122 as_bad (_("GOT already in symbol table"));
14123 GOT_symbol = symbol_new (name, undefined_section,
e01e1cee 14124 &zero_address_frag, 0);
24eab124
AM
14125 };
14126 return GOT_symbol;
14127 }
252b5132
RH
14128 return 0;
14129}
14130
14131/* Round up a section size to the appropriate boundary. */
47926f60 14132
252b5132 14133valueT
7016a5d5 14134md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
252b5132 14135{
4c63da97
AM
14136#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
14137 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
14138 {
14139 /* For a.out, force the section size to be aligned. If we don't do
14140 this, BFD will align it for us, but it will not write out the
14141 final bytes of the section. This may be a bug in BFD, but it is
14142 easier to fix it here since that is how the other a.out targets
14143 work. */
14144 int align;
14145
fd361982 14146 align = bfd_section_alignment (segment);
8d3842cd 14147 size = ((size + (1 << align) - 1) & (-((valueT) 1 << align)));
4c63da97 14148 }
252b5132
RH
14149#endif
14150
14151 return size;
14152}
14153
14154/* On the i386, PC-relative offsets are relative to the start of the
14155 next instruction. That is, the address of the offset, plus its
14156 size, since the offset is always the last part of the insn. */
14157
14158long
e3bb37b5 14159md_pcrel_from (fixS *fixP)
252b5132
RH
14160{
14161 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
14162}
14163
14164#ifndef I386COFF
14165
14166static void
e3bb37b5 14167s_bss (int ignore ATTRIBUTE_UNUSED)
252b5132 14168{
29b0f896 14169 int temp;
252b5132 14170
8a75718c
JB
14171#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14172 if (IS_ELF)
14173 obj_elf_section_change_hook ();
14174#endif
252b5132
RH
14175 temp = get_absolute_expression ();
14176 subseg_set (bss_section, (subsegT) temp);
14177 demand_empty_rest_of_line ();
14178}
14179
14180#endif
14181
e379e5f3
L
14182/* Remember constant directive. */
14183
14184void
14185i386_cons_align (int ignore ATTRIBUTE_UNUSED)
14186{
14187 if (last_insn.kind != last_insn_directive
14188 && (bfd_section_flags (now_seg) & SEC_CODE))
14189 {
14190 last_insn.seg = now_seg;
14191 last_insn.kind = last_insn_directive;
14192 last_insn.name = "constant directive";
14193 last_insn.file = as_where (&last_insn.line);
ae531041
L
14194 if (lfence_before_ret != lfence_before_ret_none)
14195 {
14196 if (lfence_before_indirect_branch != lfence_branch_none)
14197 as_warn (_("constant directive skips -mlfence-before-ret "
14198 "and -mlfence-before-indirect-branch"));
14199 else
14200 as_warn (_("constant directive skips -mlfence-before-ret"));
14201 }
14202 else if (lfence_before_indirect_branch != lfence_branch_none)
14203 as_warn (_("constant directive skips -mlfence-before-indirect-branch"));
e379e5f3
L
14204 }
14205}
14206
3abbafc2 14207int
e3bb37b5 14208i386_validate_fix (fixS *fixp)
252b5132 14209{
3abbafc2
JB
14210#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14211 if (fixp->fx_r_type == BFD_RELOC_SIZE32
14212 || fixp->fx_r_type == BFD_RELOC_SIZE64)
14213 return IS_ELF && fixp->fx_addsy
14214 && (!S_IS_DEFINED (fixp->fx_addsy)
14215 || S_IS_EXTERNAL (fixp->fx_addsy));
14216#endif
14217
02a86693 14218 if (fixp->fx_subsy)
252b5132 14219 {
02a86693 14220 if (fixp->fx_subsy == GOT_symbol)
23df1078 14221 {
02a86693
L
14222 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
14223 {
14224 if (!object_64bit)
14225 abort ();
14226#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14227 if (fixp->fx_tcbit2)
56ceb5b5
L
14228 fixp->fx_r_type = (fixp->fx_tcbit
14229 ? BFD_RELOC_X86_64_REX_GOTPCRELX
14230 : BFD_RELOC_X86_64_GOTPCRELX);
02a86693
L
14231 else
14232#endif
14233 fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
14234 }
d6ab8113 14235 else
02a86693
L
14236 {
14237 if (!object_64bit)
14238 fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
14239 else
14240 fixp->fx_r_type = BFD_RELOC_X86_64_GOTOFF64;
14241 }
14242 fixp->fx_subsy = 0;
23df1078 14243 }
252b5132 14244 }
02a86693 14245#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2585b7a5 14246 else
02a86693 14247 {
2585b7a5
L
14248 /* NB: Commit 292676c1 resolved PLT32 reloc aganst local symbol
14249 to section. Since PLT32 relocation must be against symbols,
14250 turn such PLT32 relocation into PC32 relocation. */
14251 if (fixp->fx_addsy
14252 && (fixp->fx_r_type == BFD_RELOC_386_PLT32
14253 || fixp->fx_r_type == BFD_RELOC_X86_64_PLT32)
14254 && symbol_section_p (fixp->fx_addsy))
14255 fixp->fx_r_type = BFD_RELOC_32_PCREL;
14256 if (!object_64bit)
14257 {
14258 if (fixp->fx_r_type == BFD_RELOC_386_GOT32
14259 && fixp->fx_tcbit2)
14260 fixp->fx_r_type = BFD_RELOC_386_GOT32X;
14261 }
02a86693
L
14262 }
14263#endif
3abbafc2
JB
14264
14265 return 1;
252b5132
RH
14266}
14267
252b5132 14268arelent *
7016a5d5 14269tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
14270{
14271 arelent *rel;
14272 bfd_reloc_code_real_type code;
14273
14274 switch (fixp->fx_r_type)
14275 {
8ce3d284 14276#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3abbafc2
JB
14277 symbolS *sym;
14278
8fd4256d
L
14279 case BFD_RELOC_SIZE32:
14280 case BFD_RELOC_SIZE64:
3abbafc2
JB
14281 if (fixp->fx_addsy
14282 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))
14283 && (!fixp->fx_subsy
14284 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))))
14285 sym = fixp->fx_addsy;
14286 else if (fixp->fx_subsy
14287 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))
14288 && (!fixp->fx_addsy
14289 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))))
14290 sym = fixp->fx_subsy;
14291 else
14292 sym = NULL;
14293 if (IS_ELF && sym && S_IS_DEFINED (sym) && !S_IS_EXTERNAL (sym))
8fd4256d
L
14294 {
14295 /* Resolve size relocation against local symbol to size of
14296 the symbol plus addend. */
3abbafc2 14297 valueT value = S_GET_SIZE (sym);
44f87162 14298
3abbafc2
JB
14299 if (symbol_get_bfdsym (sym)->flags & BSF_SECTION_SYM)
14300 value = bfd_section_size (S_GET_SEGMENT (sym));
14301 if (sym == fixp->fx_subsy)
14302 {
14303 value = -value;
14304 if (fixp->fx_addsy)
14305 value += S_GET_VALUE (fixp->fx_addsy);
14306 }
14307 else if (fixp->fx_subsy)
14308 value -= S_GET_VALUE (fixp->fx_subsy);
44f87162 14309 value += fixp->fx_offset;
8fd4256d 14310 if (fixp->fx_r_type == BFD_RELOC_SIZE32
d965814f 14311 && object_64bit
8fd4256d
L
14312 && !fits_in_unsigned_long (value))
14313 as_bad_where (fixp->fx_file, fixp->fx_line,
14314 _("symbol size computation overflow"));
14315 fixp->fx_addsy = NULL;
14316 fixp->fx_subsy = NULL;
14317 md_apply_fix (fixp, (valueT *) &value, NULL);
14318 return NULL;
14319 }
3abbafc2
JB
14320 if (!fixp->fx_addsy || fixp->fx_subsy)
14321 {
14322 as_bad_where (fixp->fx_file, fixp->fx_line,
14323 "unsupported expression involving @size");
14324 return NULL;
14325 }
8ce3d284 14326#endif
1a0670f3 14327 /* Fall through. */
8fd4256d 14328
3e73aa7c
JH
14329 case BFD_RELOC_X86_64_PLT32:
14330 case BFD_RELOC_X86_64_GOT32:
14331 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
14332 case BFD_RELOC_X86_64_GOTPCRELX:
14333 case BFD_RELOC_X86_64_REX_GOTPCRELX:
252b5132
RH
14334 case BFD_RELOC_386_PLT32:
14335 case BFD_RELOC_386_GOT32:
02a86693 14336 case BFD_RELOC_386_GOT32X:
252b5132
RH
14337 case BFD_RELOC_386_GOTOFF:
14338 case BFD_RELOC_386_GOTPC:
13ae64f3
JJ
14339 case BFD_RELOC_386_TLS_GD:
14340 case BFD_RELOC_386_TLS_LDM:
14341 case BFD_RELOC_386_TLS_LDO_32:
14342 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
14343 case BFD_RELOC_386_TLS_IE:
14344 case BFD_RELOC_386_TLS_GOTIE:
13ae64f3
JJ
14345 case BFD_RELOC_386_TLS_LE_32:
14346 case BFD_RELOC_386_TLS_LE:
67a4f2b7
AO
14347 case BFD_RELOC_386_TLS_GOTDESC:
14348 case BFD_RELOC_386_TLS_DESC_CALL:
bffbf940
JJ
14349 case BFD_RELOC_X86_64_TLSGD:
14350 case BFD_RELOC_X86_64_TLSLD:
14351 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 14352 case BFD_RELOC_X86_64_DTPOFF64:
bffbf940
JJ
14353 case BFD_RELOC_X86_64_GOTTPOFF:
14354 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113
JB
14355 case BFD_RELOC_X86_64_TPOFF64:
14356 case BFD_RELOC_X86_64_GOTOFF64:
14357 case BFD_RELOC_X86_64_GOTPC32:
7b81dfbb
AJ
14358 case BFD_RELOC_X86_64_GOT64:
14359 case BFD_RELOC_X86_64_GOTPCREL64:
14360 case BFD_RELOC_X86_64_GOTPC64:
14361 case BFD_RELOC_X86_64_GOTPLT64:
14362 case BFD_RELOC_X86_64_PLTOFF64:
67a4f2b7
AO
14363 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14364 case BFD_RELOC_X86_64_TLSDESC_CALL:
252b5132
RH
14365 case BFD_RELOC_RVA:
14366 case BFD_RELOC_VTABLE_ENTRY:
14367 case BFD_RELOC_VTABLE_INHERIT:
6482c264
NC
14368#ifdef TE_PE
14369 case BFD_RELOC_32_SECREL:
14370#endif
252b5132
RH
14371 code = fixp->fx_r_type;
14372 break;
dbbaec26
L
14373 case BFD_RELOC_X86_64_32S:
14374 if (!fixp->fx_pcrel)
14375 {
14376 /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32. */
14377 code = fixp->fx_r_type;
14378 break;
14379 }
1a0670f3 14380 /* Fall through. */
252b5132 14381 default:
93382f6d 14382 if (fixp->fx_pcrel)
252b5132 14383 {
93382f6d
AM
14384 switch (fixp->fx_size)
14385 {
14386 default:
b091f402
AM
14387 as_bad_where (fixp->fx_file, fixp->fx_line,
14388 _("can not do %d byte pc-relative relocation"),
14389 fixp->fx_size);
93382f6d
AM
14390 code = BFD_RELOC_32_PCREL;
14391 break;
14392 case 1: code = BFD_RELOC_8_PCREL; break;
14393 case 2: code = BFD_RELOC_16_PCREL; break;
d258b828 14394 case 4: code = BFD_RELOC_32_PCREL; break;
d6ab8113
JB
14395#ifdef BFD64
14396 case 8: code = BFD_RELOC_64_PCREL; break;
14397#endif
93382f6d
AM
14398 }
14399 }
14400 else
14401 {
14402 switch (fixp->fx_size)
14403 {
14404 default:
b091f402
AM
14405 as_bad_where (fixp->fx_file, fixp->fx_line,
14406 _("can not do %d byte relocation"),
14407 fixp->fx_size);
93382f6d
AM
14408 code = BFD_RELOC_32;
14409 break;
14410 case 1: code = BFD_RELOC_8; break;
14411 case 2: code = BFD_RELOC_16; break;
14412 case 4: code = BFD_RELOC_32; break;
937149dd 14413#ifdef BFD64
3e73aa7c 14414 case 8: code = BFD_RELOC_64; break;
937149dd 14415#endif
93382f6d 14416 }
252b5132
RH
14417 }
14418 break;
14419 }
252b5132 14420
d182319b
JB
14421 if ((code == BFD_RELOC_32
14422 || code == BFD_RELOC_32_PCREL
14423 || code == BFD_RELOC_X86_64_32S)
252b5132
RH
14424 && GOT_symbol
14425 && fixp->fx_addsy == GOT_symbol)
3e73aa7c 14426 {
4fa24527 14427 if (!object_64bit)
d6ab8113
JB
14428 code = BFD_RELOC_386_GOTPC;
14429 else
14430 code = BFD_RELOC_X86_64_GOTPC32;
3e73aa7c 14431 }
7b81dfbb
AJ
14432 if ((code == BFD_RELOC_64 || code == BFD_RELOC_64_PCREL)
14433 && GOT_symbol
14434 && fixp->fx_addsy == GOT_symbol)
14435 {
14436 code = BFD_RELOC_X86_64_GOTPC64;
14437 }
252b5132 14438
add39d23
TS
14439 rel = XNEW (arelent);
14440 rel->sym_ptr_ptr = XNEW (asymbol *);
49309057 14441 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
14442
14443 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
c87db184 14444
3e73aa7c
JH
14445 if (!use_rela_relocations)
14446 {
14447 /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
14448 vtable entry to be used in the relocation's section offset. */
14449 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
14450 rel->address = fixp->fx_offset;
fbeb56a4
DK
14451#if defined (OBJ_COFF) && defined (TE_PE)
14452 else if (fixp->fx_addsy && S_IS_WEAK (fixp->fx_addsy))
14453 rel->addend = fixp->fx_addnumber - (S_GET_VALUE (fixp->fx_addsy) * 2);
14454 else
14455#endif
c6682705 14456 rel->addend = 0;
3e73aa7c
JH
14457 }
14458 /* Use the rela in 64bit mode. */
252b5132 14459 else
3e73aa7c 14460 {
862be3fb
L
14461 if (disallow_64bit_reloc)
14462 switch (code)
14463 {
862be3fb
L
14464 case BFD_RELOC_X86_64_DTPOFF64:
14465 case BFD_RELOC_X86_64_TPOFF64:
14466 case BFD_RELOC_64_PCREL:
14467 case BFD_RELOC_X86_64_GOTOFF64:
14468 case BFD_RELOC_X86_64_GOT64:
14469 case BFD_RELOC_X86_64_GOTPCREL64:
14470 case BFD_RELOC_X86_64_GOTPC64:
14471 case BFD_RELOC_X86_64_GOTPLT64:
14472 case BFD_RELOC_X86_64_PLTOFF64:
14473 as_bad_where (fixp->fx_file, fixp->fx_line,
14474 _("cannot represent relocation type %s in x32 mode"),
14475 bfd_get_reloc_code_name (code));
14476 break;
14477 default:
14478 break;
14479 }
14480
062cd5e7
AS
14481 if (!fixp->fx_pcrel)
14482 rel->addend = fixp->fx_offset;
14483 else
14484 switch (code)
14485 {
14486 case BFD_RELOC_X86_64_PLT32:
14487 case BFD_RELOC_X86_64_GOT32:
14488 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
14489 case BFD_RELOC_X86_64_GOTPCRELX:
14490 case BFD_RELOC_X86_64_REX_GOTPCRELX:
bffbf940
JJ
14491 case BFD_RELOC_X86_64_TLSGD:
14492 case BFD_RELOC_X86_64_TLSLD:
14493 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7
AO
14494 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14495 case BFD_RELOC_X86_64_TLSDESC_CALL:
062cd5e7
AS
14496 rel->addend = fixp->fx_offset - fixp->fx_size;
14497 break;
14498 default:
14499 rel->addend = (section->vma
14500 - fixp->fx_size
14501 + fixp->fx_addnumber
14502 + md_pcrel_from (fixp));
14503 break;
14504 }
3e73aa7c
JH
14505 }
14506
252b5132
RH
14507 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
14508 if (rel->howto == NULL)
14509 {
14510 as_bad_where (fixp->fx_file, fixp->fx_line,
d0b47220 14511 _("cannot represent relocation type %s"),
252b5132
RH
14512 bfd_get_reloc_code_name (code));
14513 /* Set howto to a garbage value so that we can keep going. */
14514 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
9c2799c2 14515 gas_assert (rel->howto != NULL);
252b5132
RH
14516 }
14517
14518 return rel;
14519}
14520
ee86248c 14521#include "tc-i386-intel.c"
54cfded0 14522
a60de03c
JB
14523void
14524tc_x86_parse_to_dw2regnum (expressionS *exp)
54cfded0 14525{
a60de03c
JB
14526 int saved_naked_reg;
14527 char saved_register_dot;
54cfded0 14528
a60de03c
JB
14529 saved_naked_reg = allow_naked_reg;
14530 allow_naked_reg = 1;
14531 saved_register_dot = register_chars['.'];
14532 register_chars['.'] = '.';
14533 allow_pseudo_reg = 1;
14534 expression_and_evaluate (exp);
14535 allow_pseudo_reg = 0;
14536 register_chars['.'] = saved_register_dot;
14537 allow_naked_reg = saved_naked_reg;
14538
e96d56a1 14539 if (exp->X_op == O_register && exp->X_add_number >= 0)
54cfded0 14540 {
a60de03c
JB
14541 if ((addressT) exp->X_add_number < i386_regtab_size)
14542 {
14543 exp->X_op = O_constant;
14544 exp->X_add_number = i386_regtab[exp->X_add_number]
14545 .dw2_regnum[flag_code >> 1];
14546 }
14547 else
14548 exp->X_op = O_illegal;
54cfded0 14549 }
54cfded0
AM
14550}
14551
14552void
14553tc_x86_frame_initial_instructions (void)
14554{
a60de03c
JB
14555 static unsigned int sp_regno[2];
14556
14557 if (!sp_regno[flag_code >> 1])
14558 {
14559 char *saved_input = input_line_pointer;
14560 char sp[][4] = {"esp", "rsp"};
14561 expressionS exp;
a4447b93 14562
a60de03c
JB
14563 input_line_pointer = sp[flag_code >> 1];
14564 tc_x86_parse_to_dw2regnum (&exp);
9c2799c2 14565 gas_assert (exp.X_op == O_constant);
a60de03c
JB
14566 sp_regno[flag_code >> 1] = exp.X_add_number;
14567 input_line_pointer = saved_input;
14568 }
a4447b93 14569
61ff971f
L
14570 cfi_add_CFA_def_cfa (sp_regno[flag_code >> 1], -x86_cie_data_alignment);
14571 cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
54cfded0 14572}
d2b2c203 14573
d7921315
L
14574int
14575x86_dwarf2_addr_size (void)
14576{
14577#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
14578 if (x86_elf_abi == X86_64_X32_ABI)
14579 return 4;
14580#endif
14581 return bfd_arch_bits_per_address (stdoutput) / 8;
14582}
14583
d2b2c203
DJ
14584int
14585i386_elf_section_type (const char *str, size_t len)
14586{
14587 if (flag_code == CODE_64BIT
14588 && len == sizeof ("unwind") - 1
d34049e8 14589 && startswith (str, "unwind"))
d2b2c203
DJ
14590 return SHT_X86_64_UNWIND;
14591
14592 return -1;
14593}
bb41ade5 14594
ad5fec3b
EB
14595#ifdef TE_SOLARIS
14596void
14597i386_solaris_fix_up_eh_frame (segT sec)
14598{
14599 if (flag_code == CODE_64BIT)
14600 elf_section_type (sec) = SHT_X86_64_UNWIND;
14601}
14602#endif
14603
bb41ade5
AM
14604#ifdef TE_PE
14605void
14606tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
14607{
91d6fa6a 14608 expressionS exp;
bb41ade5 14609
91d6fa6a
NC
14610 exp.X_op = O_secrel;
14611 exp.X_add_symbol = symbol;
14612 exp.X_add_number = 0;
14613 emit_expr (&exp, size);
bb41ade5
AM
14614}
14615#endif
3b22753a
L
14616
14617#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14618/* For ELF on x86-64, add support for SHF_X86_64_LARGE. */
14619
01e1a5bc 14620bfd_vma
6d4af3c2 14621x86_64_section_letter (int letter, const char **ptr_msg)
3b22753a
L
14622{
14623 if (flag_code == CODE_64BIT)
14624 {
14625 if (letter == 'l')
14626 return SHF_X86_64_LARGE;
14627
8f3bae45 14628 *ptr_msg = _("bad .section directive: want a,l,w,x,M,S,G,T in string");
64e74474 14629 }
3b22753a 14630 else
8f3bae45 14631 *ptr_msg = _("bad .section directive: want a,w,x,M,S,G,T in string");
3b22753a
L
14632 return -1;
14633}
14634
01e1a5bc 14635bfd_vma
3b22753a
L
14636x86_64_section_word (char *str, size_t len)
14637{
08dedd66 14638 if (len == 5 && flag_code == CODE_64BIT && startswith (str, "large"))
3b22753a
L
14639 return SHF_X86_64_LARGE;
14640
14641 return -1;
14642}
14643
14644static void
14645handle_large_common (int small ATTRIBUTE_UNUSED)
14646{
14647 if (flag_code != CODE_64BIT)
14648 {
14649 s_comm_internal (0, elf_common_parse);
14650 as_warn (_(".largecomm supported only in 64bit mode, producing .comm"));
14651 }
14652 else
14653 {
14654 static segT lbss_section;
14655 asection *saved_com_section_ptr = elf_com_section_ptr;
14656 asection *saved_bss_section = bss_section;
14657
14658 if (lbss_section == NULL)
14659 {
14660 flagword applicable;
14661 segT seg = now_seg;
14662 subsegT subseg = now_subseg;
14663
14664 /* The .lbss section is for local .largecomm symbols. */
14665 lbss_section = subseg_new (".lbss", 0);
14666 applicable = bfd_applicable_section_flags (stdoutput);
fd361982 14667 bfd_set_section_flags (lbss_section, applicable & SEC_ALLOC);
3b22753a
L
14668 seg_info (lbss_section)->bss = 1;
14669
14670 subseg_set (seg, subseg);
14671 }
14672
14673 elf_com_section_ptr = &_bfd_elf_large_com_section;
14674 bss_section = lbss_section;
14675
14676 s_comm_internal (0, elf_common_parse);
14677
14678 elf_com_section_ptr = saved_com_section_ptr;
14679 bss_section = saved_bss_section;
14680 }
14681}
14682#endif /* OBJ_ELF || OBJ_MAYBE_ELF */