]> Git Repo - binutils.git/blame - gas/config/tc-i386.c
x86: allow unary operators to start a memory operand
[binutils.git] / gas / config / tc-i386.c
CommitLineData
b534c6d3 1/* tc-i386.c -- Assemble code for the Intel 80386
250d07de 2 Copyright (C) 1989-2021 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 ([email protected]).
3e73aa7c 23 x86_64 support by Jan Hubicka ([email protected])
0f10071e 24 VIA PadLock support by Michal Ludvig ([email protected])
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,
43234a1e
L
249 unsupported_vector_index_register,
250 unsupported_broadcast,
43234a1e
L
251 broadcast_needed,
252 unsupported_masking,
253 mask_not_on_destination,
254 no_default_mask,
255 unsupported_rc_sae,
256 rc_sae_operand_not_last_imm,
257 invalid_register_operand,
a65babc9
L
258 };
259
252b5132
RH
260struct _i386_insn
261 {
47926f60 262 /* TM holds the template for the insn were currently assembling. */
d3ce72d0 263 insn_template tm;
252b5132 264
7d5e4556
L
265 /* SUFFIX holds the instruction size suffix for byte, word, dword
266 or qword, if given. */
252b5132
RH
267 char suffix;
268
9a182d04
JB
269 /* OPCODE_LENGTH holds the number of base opcode bytes. */
270 unsigned char opcode_length;
271
47926f60 272 /* OPERANDS gives the number of given operands. */
252b5132
RH
273 unsigned int operands;
274
275 /* REG_OPERANDS, DISP_OPERANDS, MEM_OPERANDS, IMM_OPERANDS give the number
276 of given register, displacement, memory operands and immediate
47926f60 277 operands. */
252b5132
RH
278 unsigned int reg_operands, disp_operands, mem_operands, imm_operands;
279
280 /* TYPES [i] is the type (see above #defines) which tells us how to
520dc8e8 281 use OP[i] for the corresponding operand. */
40fb9820 282 i386_operand_type types[MAX_OPERANDS];
252b5132 283
520dc8e8
AM
284 /* Displacement expression, immediate expression, or register for each
285 operand. */
286 union i386_op op[MAX_OPERANDS];
252b5132 287
3e73aa7c
JH
288 /* Flags for operands. */
289 unsigned int flags[MAX_OPERANDS];
290#define Operand_PCrel 1
c48dadc9 291#define Operand_Mem 2
3e73aa7c 292
252b5132 293 /* Relocation type for operand */
f86103b7 294 enum bfd_reloc_code_real reloc[MAX_OPERANDS];
252b5132 295
252b5132
RH
296 /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
297 the base index byte below. */
298 const reg_entry *base_reg;
299 const reg_entry *index_reg;
300 unsigned int log2_scale_factor;
301
302 /* SEG gives the seg_entries of this insn. They are zero unless
47926f60 303 explicit segment overrides are given. */
5e042380 304 const reg_entry *seg[2];
252b5132 305
8325cc63
JB
306 /* Copied first memory operand string, for re-checking. */
307 char *memop1_string;
308
252b5132
RH
309 /* PREFIX holds all the given prefix opcodes (usually null).
310 PREFIXES is the number of prefix opcodes. */
311 unsigned int prefixes;
312 unsigned char prefix[MAX_PREFIXES];
313
50128d0c 314 /* Register is in low 3 bits of opcode. */
5b7c81bd 315 bool short_form;
50128d0c 316
6f2f06be 317 /* The operand to a branch insn indicates an absolute branch. */
5b7c81bd 318 bool jumpabsolute;
6f2f06be 319
921eafea
L
320 /* Extended states. */
321 enum
322 {
323 /* Use MMX state. */
324 xstate_mmx = 1 << 0,
325 /* Use XMM state. */
326 xstate_xmm = 1 << 1,
327 /* Use YMM state. */
328 xstate_ymm = 1 << 2 | xstate_xmm,
329 /* Use ZMM state. */
330 xstate_zmm = 1 << 3 | xstate_ymm,
331 /* Use TMM state. */
32930e4e
L
332 xstate_tmm = 1 << 4,
333 /* Use MASK state. */
334 xstate_mask = 1 << 5
921eafea 335 } xstate;
260cd341 336
e379e5f3 337 /* Has GOTPC or TLS relocation. */
5b7c81bd 338 bool has_gotpc_tls_reloc;
e379e5f3 339
252b5132 340 /* RM and SIB are the modrm byte and the sib byte where the
c1e679ec 341 addressing modes of this insn are encoded. */
252b5132 342 modrm_byte rm;
3e73aa7c 343 rex_byte rex;
43234a1e 344 rex_byte vrex;
252b5132 345 sib_byte sib;
c0f3af97 346 vex_prefix vex;
b6169b20 347
6225c532
JB
348 /* Masking attributes.
349
350 The struct describes masking, applied to OPERAND in the instruction.
351 REG is a pointer to the corresponding mask register. ZEROING tells
352 whether merging or zeroing mask is used. */
353 struct Mask_Operation
354 {
355 const reg_entry *reg;
356 unsigned int zeroing;
357 /* The operand where this operation is associated. */
358 unsigned int operand;
359 } mask;
43234a1e
L
360
361 /* Rounding control and SAE attributes. */
ca5312a2
JB
362 struct RC_Operation
363 {
364 enum rc_type
365 {
366 rc_none = -1,
367 rne,
368 rd,
369 ru,
370 rz,
371 saeonly
372 } type;
373
374 unsigned int operand;
375 } rounding;
43234a1e 376
5273a3cd
JB
377 /* Broadcasting attributes.
378
379 The struct describes broadcasting, applied to OPERAND. TYPE is
380 expresses the broadcast factor. */
381 struct Broadcast_Operation
382 {
383 /* Type of broadcast: {1to2}, {1to4}, {1to8}, or {1to16}. */
384 unsigned int type;
385
386 /* Index of broadcasted operand. */
387 unsigned int operand;
388
389 /* Number of bytes to broadcast. */
390 unsigned int bytes;
391 } broadcast;
43234a1e
L
392
393 /* Compressed disp8*N attribute. */
394 unsigned int memshift;
395
86fa6981
L
396 /* Prefer load or store in encoding. */
397 enum
398 {
399 dir_encoding_default = 0,
400 dir_encoding_load,
64c49ab3
JB
401 dir_encoding_store,
402 dir_encoding_swap
86fa6981 403 } dir_encoding;
891edac4 404
41eb8e88 405 /* Prefer 8bit, 16bit, 32bit displacement in encoding. */
a501d77e
L
406 enum
407 {
408 disp_encoding_default = 0,
409 disp_encoding_8bit,
41eb8e88 410 disp_encoding_16bit,
a501d77e
L
411 disp_encoding_32bit
412 } disp_encoding;
f8a5c266 413
6b6b6807 414 /* Prefer the REX byte in encoding. */
5b7c81bd 415 bool rex_encoding;
6b6b6807 416
b6f8c7c4 417 /* Disable instruction size optimization. */
5b7c81bd 418 bool no_optimize;
b6f8c7c4 419
86fa6981
L
420 /* How to encode vector instructions. */
421 enum
422 {
423 vex_encoding_default = 0,
42e04b36 424 vex_encoding_vex,
86fa6981 425 vex_encoding_vex3,
da4977e0
JB
426 vex_encoding_evex,
427 vex_encoding_error
86fa6981
L
428 } vec_encoding;
429
d5de92cf
L
430 /* REP prefix. */
431 const char *rep_prefix;
432
165de32a
L
433 /* HLE prefix. */
434 const char *hle_prefix;
42164a71 435
7e8b059b
L
436 /* Have BND prefix. */
437 const char *bnd_prefix;
438
04ef582a
L
439 /* Have NOTRACK prefix. */
440 const char *notrack_prefix;
441
891edac4 442 /* Error message. */
a65babc9 443 enum i386_error error;
252b5132
RH
444 };
445
446typedef struct _i386_insn i386_insn;
447
43234a1e
L
448/* Link RC type with corresponding string, that'll be looked for in
449 asm. */
450struct RC_name
451{
452 enum rc_type type;
453 const char *name;
454 unsigned int len;
455};
456
457static const struct RC_name RC_NamesTable[] =
458{
459 { rne, STRING_COMMA_LEN ("rn-sae") },
460 { rd, STRING_COMMA_LEN ("rd-sae") },
461 { ru, STRING_COMMA_LEN ("ru-sae") },
462 { rz, STRING_COMMA_LEN ("rz-sae") },
463 { saeonly, STRING_COMMA_LEN ("sae") },
464};
465
252b5132
RH
466/* List of chars besides those in app.c:symbol_chars that can start an
467 operand. Used to prevent the scrubber eating vital white-space. */
86fa6981 468const char extra_symbol_chars[] = "*%-([{}"
252b5132 469#ifdef LEX_AT
32137342
NC
470 "@"
471#endif
472#ifdef LEX_QM
473 "?"
252b5132 474#endif
32137342 475 ;
252b5132 476
b3983e5f
JB
477#if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
478 && !defined (TE_GNU) \
479 && !defined (TE_LINUX) \
480 && !defined (TE_FreeBSD) \
481 && !defined (TE_DragonFly) \
482 && !defined (TE_NetBSD))
252b5132 483/* This array holds the chars that always start a comment. If the
b3b91714
AM
484 pre-processor is disabled, these aren't very useful. The option
485 --divide will remove '/' from this list. */
486const char *i386_comment_chars = "#/";
487#define SVR4_COMMENT_CHARS 1
252b5132 488#define PREFIX_SEPARATOR '\\'
252b5132 489
b3b91714
AM
490#else
491const char *i386_comment_chars = "#";
492#define PREFIX_SEPARATOR '/'
493#endif
494
252b5132
RH
495/* This array holds the chars that only start a comment at the beginning of
496 a line. If the line seems to have the form '# 123 filename'
ce8a8b2f
AM
497 .line and .file directives will appear in the pre-processed output.
498 Note that input_file.c hand checks for '#' at the beginning of the
252b5132 499 first line of the input file. This is because the compiler outputs
ce8a8b2f
AM
500 #NO_APP at the beginning of its output.
501 Also note that comments started like this one will always work if
252b5132 502 '/' isn't otherwise defined. */
b3b91714 503const char line_comment_chars[] = "#/";
252b5132 504
63a0b638 505const char line_separator_chars[] = ";";
252b5132 506
ce8a8b2f
AM
507/* Chars that can be used to separate mant from exp in floating point
508 nums. */
252b5132
RH
509const char EXP_CHARS[] = "eE";
510
ce8a8b2f
AM
511/* Chars that mean this number is a floating point constant
512 As in 0f12.456
513 or 0d1.2345e12. */
252b5132
RH
514const char FLT_CHARS[] = "fFdDxX";
515
ce8a8b2f 516/* Tables for lexical analysis. */
252b5132
RH
517static char mnemonic_chars[256];
518static char register_chars[256];
519static char operand_chars[256];
520static char identifier_chars[256];
252b5132 521
ce8a8b2f 522/* Lexical macros. */
252b5132
RH
523#define is_mnemonic_char(x) (mnemonic_chars[(unsigned char) x])
524#define is_operand_char(x) (operand_chars[(unsigned char) x])
525#define is_register_char(x) (register_chars[(unsigned char) x])
526#define is_space_char(x) ((x) == ' ')
527#define is_identifier_char(x) (identifier_chars[(unsigned char) x])
252b5132 528
0234cb7c 529/* All non-digit non-letter characters that may occur in an operand. */
252b5132
RH
530static char operand_special_chars[] = "%$-+(,)*._~/<>|&^!:[@]";
531
532/* md_assemble() always leaves the strings it's passed unaltered. To
533 effect this we maintain a stack of saved characters that we've smashed
534 with '\0's (indicating end of strings for various sub-fields of the
47926f60 535 assembler instruction). */
252b5132 536static char save_stack[32];
ce8a8b2f 537static char *save_stack_p;
252b5132
RH
538#define END_STRING_AND_SAVE(s) \
539 do { *save_stack_p++ = *(s); *(s) = '\0'; } while (0)
540#define RESTORE_END_STRING(s) \
541 do { *(s) = *--save_stack_p; } while (0)
542
47926f60 543/* The instruction we're assembling. */
252b5132
RH
544static i386_insn i;
545
546/* Possible templates for current insn. */
547static const templates *current_templates;
548
31b2323c
L
549/* Per instruction expressionS buffers: max displacements & immediates. */
550static expressionS disp_expressions[MAX_MEMORY_OPERANDS];
551static expressionS im_expressions[MAX_IMMEDIATE_OPERANDS];
252b5132 552
47926f60 553/* Current operand we are working on. */
ee86248c 554static int this_operand = -1;
252b5132 555
3e73aa7c
JH
556/* We support four different modes. FLAG_CODE variable is used to distinguish
557 these. */
558
559enum flag_code {
560 CODE_32BIT,
561 CODE_16BIT,
562 CODE_64BIT };
563
564static enum flag_code flag_code;
4fa24527 565static unsigned int object_64bit;
862be3fb 566static unsigned int disallow_64bit_reloc;
3e73aa7c 567static int use_rela_relocations = 0;
e379e5f3
L
568/* __tls_get_addr/___tls_get_addr symbol for TLS. */
569static const char *tls_get_addr;
3e73aa7c 570
7af8ed2d
NC
571#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
572 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
573 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
574
351f65ca
L
575/* The ELF ABI to use. */
576enum x86_elf_abi
577{
578 I386_ABI,
7f56bc95
L
579 X86_64_ABI,
580 X86_64_X32_ABI
351f65ca
L
581};
582
583static enum x86_elf_abi x86_elf_abi = I386_ABI;
7af8ed2d 584#endif
351f65ca 585
167ad85b
TG
586#if defined (TE_PE) || defined (TE_PEP)
587/* Use big object file format. */
588static int use_big_obj = 0;
589#endif
590
8dcea932
L
591#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
592/* 1 if generating code for a shared library. */
593static int shared = 0;
594#endif
595
47926f60
KH
596/* 1 for intel syntax,
597 0 if att syntax. */
598static int intel_syntax = 0;
252b5132 599
4b5aaf5f
L
600static enum x86_64_isa
601{
602 amd64 = 1, /* AMD64 ISA. */
603 intel64 /* Intel64 ISA. */
604} isa64;
e89c5eaa 605
1efbbeb4
L
606/* 1 for intel mnemonic,
607 0 if att mnemonic. */
608static int intel_mnemonic = !SYSV386_COMPAT;
609
a60de03c
JB
610/* 1 if pseudo registers are permitted. */
611static int allow_pseudo_reg = 0;
612
47926f60
KH
613/* 1 if register prefix % not required. */
614static int allow_naked_reg = 0;
252b5132 615
33eaf5de 616/* 1 if the assembler should add BND prefix for all control-transferring
7e8b059b
L
617 instructions supporting it, even if this prefix wasn't specified
618 explicitly. */
619static int add_bnd_prefix = 0;
620
ba104c83 621/* 1 if pseudo index register, eiz/riz, is allowed . */
db51cc60
L
622static int allow_index_reg = 0;
623
d022bddd
IT
624/* 1 if the assembler should ignore LOCK prefix, even if it was
625 specified explicitly. */
626static int omit_lock_prefix = 0;
627
e4e00185
AS
628/* 1 if the assembler should encode lfence, mfence, and sfence as
629 "lock addl $0, (%{re}sp)". */
630static int avoid_fence = 0;
631
ae531041
L
632/* 1 if lfence should be inserted after every load. */
633static int lfence_after_load = 0;
634
635/* Non-zero if lfence should be inserted before indirect branch. */
636static enum lfence_before_indirect_branch_kind
637 {
638 lfence_branch_none = 0,
639 lfence_branch_register,
640 lfence_branch_memory,
641 lfence_branch_all
642 }
643lfence_before_indirect_branch;
644
645/* Non-zero if lfence should be inserted before ret. */
646static enum lfence_before_ret_kind
647 {
648 lfence_before_ret_none = 0,
649 lfence_before_ret_not,
a09f656b 650 lfence_before_ret_or,
651 lfence_before_ret_shl
ae531041
L
652 }
653lfence_before_ret;
654
655/* Types of previous instruction is .byte or prefix. */
e379e5f3
L
656static struct
657 {
658 segT seg;
659 const char *file;
660 const char *name;
661 unsigned int line;
662 enum last_insn_kind
663 {
664 last_insn_other = 0,
665 last_insn_directive,
666 last_insn_prefix
667 } kind;
668 } last_insn;
669
0cb4071e
L
670/* 1 if the assembler should generate relax relocations. */
671
672static int generate_relax_relocations
673 = DEFAULT_GENERATE_X86_RELAX_RELOCATIONS;
674
7bab8ab5 675static enum check_kind
daf50ae7 676 {
7bab8ab5
JB
677 check_none = 0,
678 check_warning,
679 check_error
daf50ae7 680 }
7bab8ab5 681sse_check, operand_check = check_warning;
daf50ae7 682
e379e5f3
L
683/* Non-zero if branches should be aligned within power of 2 boundary. */
684static int align_branch_power = 0;
685
686/* Types of branches to align. */
687enum align_branch_kind
688 {
689 align_branch_none = 0,
690 align_branch_jcc = 1,
691 align_branch_fused = 2,
692 align_branch_jmp = 3,
693 align_branch_call = 4,
694 align_branch_indirect = 5,
695 align_branch_ret = 6
696 };
697
698/* Type bits of branches to align. */
699enum align_branch_bit
700 {
701 align_branch_jcc_bit = 1 << align_branch_jcc,
702 align_branch_fused_bit = 1 << align_branch_fused,
703 align_branch_jmp_bit = 1 << align_branch_jmp,
704 align_branch_call_bit = 1 << align_branch_call,
705 align_branch_indirect_bit = 1 << align_branch_indirect,
706 align_branch_ret_bit = 1 << align_branch_ret
707 };
708
709static unsigned int align_branch = (align_branch_jcc_bit
710 | align_branch_fused_bit
711 | align_branch_jmp_bit);
712
79d72f45
HL
713/* Types of condition jump used by macro-fusion. */
714enum mf_jcc_kind
715 {
716 mf_jcc_jo = 0, /* base opcode 0x70 */
717 mf_jcc_jc, /* base opcode 0x72 */
718 mf_jcc_je, /* base opcode 0x74 */
719 mf_jcc_jna, /* base opcode 0x76 */
720 mf_jcc_js, /* base opcode 0x78 */
721 mf_jcc_jp, /* base opcode 0x7a */
722 mf_jcc_jl, /* base opcode 0x7c */
723 mf_jcc_jle, /* base opcode 0x7e */
724 };
725
726/* Types of compare flag-modifying insntructions used by macro-fusion. */
727enum mf_cmp_kind
728 {
729 mf_cmp_test_and, /* test/cmp */
730 mf_cmp_alu_cmp, /* add/sub/cmp */
731 mf_cmp_incdec /* inc/dec */
732 };
733
e379e5f3
L
734/* The maximum padding size for fused jcc. CMP like instruction can
735 be 9 bytes and jcc can be 6 bytes. Leave room just in case for
736 prefixes. */
737#define MAX_FUSED_JCC_PADDING_SIZE 20
738
739/* The maximum number of prefixes added for an instruction. */
740static unsigned int align_branch_prefix_size = 5;
741
b6f8c7c4
L
742/* Optimization:
743 1. Clear the REX_W bit with register operand if possible.
744 2. Above plus use 128bit vector instruction to clear the full vector
745 register.
746 */
747static int optimize = 0;
748
749/* Optimization:
750 1. Clear the REX_W bit with register operand if possible.
751 2. Above plus use 128bit vector instruction to clear the full vector
752 register.
753 3. Above plus optimize "test{q,l,w} $imm8,%r{64,32,16}" to
754 "testb $imm7,%r8".
755 */
756static int optimize_for_space = 0;
757
2ca3ace5
L
758/* Register prefix used for error message. */
759static const char *register_prefix = "%";
760
47926f60
KH
761/* Used in 16 bit gcc mode to add an l suffix to call, ret, enter,
762 leave, push, and pop instructions so that gcc has the same stack
763 frame as in 32 bit mode. */
764static char stackop_size = '\0';
eecb386c 765
12b55ccc
L
766/* Non-zero to optimize code alignment. */
767int optimize_align_code = 1;
768
47926f60
KH
769/* Non-zero to quieten some warnings. */
770static int quiet_warnings = 0;
a38cf1db 771
47926f60
KH
772/* CPU name. */
773static const char *cpu_arch_name = NULL;
6305a203 774static char *cpu_sub_arch_name = NULL;
a38cf1db 775
47926f60 776/* CPU feature flags. */
40fb9820
L
777static i386_cpu_flags cpu_arch_flags = CPU_UNKNOWN_FLAGS;
778
ccc9c027
L
779/* If we have selected a cpu we are generating instructions for. */
780static int cpu_arch_tune_set = 0;
781
9103f4f4 782/* Cpu we are generating instructions for. */
fbf3f584 783enum processor_type cpu_arch_tune = PROCESSOR_UNKNOWN;
9103f4f4
L
784
785/* CPU feature flags of cpu we are generating instructions for. */
40fb9820 786static i386_cpu_flags cpu_arch_tune_flags;
9103f4f4 787
ccc9c027 788/* CPU instruction set architecture used. */
fbf3f584 789enum processor_type cpu_arch_isa = PROCESSOR_UNKNOWN;
ccc9c027 790
9103f4f4 791/* CPU feature flags of instruction set architecture used. */
fbf3f584 792i386_cpu_flags cpu_arch_isa_flags;
9103f4f4 793
fddf5b5b
AM
794/* If set, conditional jumps are not automatically promoted to handle
795 larger than a byte offset. */
796static unsigned int no_cond_jump_promotion = 0;
797
c0f3af97
L
798/* Encode SSE instructions with VEX prefix. */
799static unsigned int sse2avx;
800
539f890d
L
801/* Encode scalar AVX instructions with specific vector length. */
802static enum
803 {
804 vex128 = 0,
805 vex256
806 } avxscalar;
807
03751133
L
808/* Encode VEX WIG instructions with specific vex.w. */
809static enum
810 {
811 vexw0 = 0,
812 vexw1
813 } vexwig;
814
43234a1e
L
815/* Encode scalar EVEX LIG instructions with specific vector length. */
816static enum
817 {
818 evexl128 = 0,
819 evexl256,
820 evexl512
821 } evexlig;
822
823/* Encode EVEX WIG instructions with specific evex.w. */
824static enum
825 {
826 evexw0 = 0,
827 evexw1
828 } evexwig;
829
d3d3c6db
IT
830/* Value to encode in EVEX RC bits, for SAE-only instructions. */
831static enum rc_type evexrcig = rne;
832
29b0f896 833/* Pre-defined "_GLOBAL_OFFSET_TABLE_". */
87c245cc 834static symbolS *GOT_symbol;
29b0f896 835
a4447b93
RH
836/* The dwarf2 return column, adjusted for 32 or 64 bit. */
837unsigned int x86_dwarf2_return_column;
838
839/* The dwarf2 data alignment, adjusted for 32 or 64 bit. */
840int x86_cie_data_alignment;
841
252b5132 842/* Interface to relax_segment.
fddf5b5b
AM
843 There are 3 major relax states for 386 jump insns because the
844 different types of jumps add different sizes to frags when we're
e379e5f3
L
845 figuring out what sort of jump to choose to reach a given label.
846
847 BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING are used to align
848 branches which are handled by md_estimate_size_before_relax() and
849 i386_generic_table_relax_frag(). */
252b5132 850
47926f60 851/* Types. */
93c2a809
AM
852#define UNCOND_JUMP 0
853#define COND_JUMP 1
854#define COND_JUMP86 2
e379e5f3
L
855#define BRANCH_PADDING 3
856#define BRANCH_PREFIX 4
857#define FUSED_JCC_PADDING 5
fddf5b5b 858
47926f60 859/* Sizes. */
252b5132
RH
860#define CODE16 1
861#define SMALL 0
29b0f896 862#define SMALL16 (SMALL | CODE16)
252b5132 863#define BIG 2
29b0f896 864#define BIG16 (BIG | CODE16)
252b5132
RH
865
866#ifndef INLINE
867#ifdef __GNUC__
868#define INLINE __inline__
869#else
870#define INLINE
871#endif
872#endif
873
fddf5b5b
AM
874#define ENCODE_RELAX_STATE(type, size) \
875 ((relax_substateT) (((type) << 2) | (size)))
876#define TYPE_FROM_RELAX_STATE(s) \
877 ((s) >> 2)
878#define DISP_SIZE_FROM_RELAX_STATE(s) \
879 ((((s) & 3) == BIG ? 4 : (((s) & 3) == BIG16 ? 2 : 1)))
252b5132
RH
880
881/* This table is used by relax_frag to promote short jumps to long
882 ones where necessary. SMALL (short) jumps may be promoted to BIG
883 (32 bit long) ones, and SMALL16 jumps to BIG16 (16 bit long). We
884 don't allow a short jump in a 32 bit code segment to be promoted to
885 a 16 bit offset jump because it's slower (requires data size
886 prefix), and doesn't work, unless the destination is in the bottom
887 64k of the code segment (The top 16 bits of eip are zeroed). */
888
889const relax_typeS md_relax_table[] =
890{
24eab124
AM
891 /* The fields are:
892 1) most positive reach of this state,
893 2) most negative reach of this state,
93c2a809 894 3) how many bytes this mode will have in the variable part of the frag
ce8a8b2f 895 4) which index into the table to try if we can't fit into this one. */
252b5132 896
fddf5b5b 897 /* UNCOND_JUMP states. */
93c2a809
AM
898 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG)},
899 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16)},
900 /* dword jmp adds 4 bytes to frag:
901 0 extra opcode bytes, 4 displacement bytes. */
252b5132 902 {0, 0, 4, 0},
93c2a809
AM
903 /* word jmp adds 2 byte2 to frag:
904 0 extra opcode bytes, 2 displacement bytes. */
252b5132
RH
905 {0, 0, 2, 0},
906
93c2a809
AM
907 /* COND_JUMP states. */
908 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG)},
909 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG16)},
910 /* dword conditionals adds 5 bytes to frag:
911 1 extra opcode byte, 4 displacement bytes. */
912 {0, 0, 5, 0},
fddf5b5b 913 /* word conditionals add 3 bytes to frag:
93c2a809
AM
914 1 extra opcode byte, 2 displacement bytes. */
915 {0, 0, 3, 0},
916
917 /* COND_JUMP86 states. */
918 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG)},
919 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG16)},
920 /* dword conditionals adds 5 bytes to frag:
921 1 extra opcode byte, 4 displacement bytes. */
922 {0, 0, 5, 0},
923 /* word conditionals add 4 bytes to frag:
924 1 displacement byte and a 3 byte long branch insn. */
925 {0, 0, 4, 0}
252b5132
RH
926};
927
9103f4f4
L
928static const arch_entry cpu_arch[] =
929{
89507696
JB
930 /* Do not replace the first two entries - i386_target_format()
931 relies on them being there in this order. */
8a2c8fef 932 { STRING_COMMA_LEN ("generic32"), PROCESSOR_GENERIC32,
293f5f65 933 CPU_GENERIC32_FLAGS, 0 },
8a2c8fef 934 { STRING_COMMA_LEN ("generic64"), PROCESSOR_GENERIC64,
293f5f65 935 CPU_GENERIC64_FLAGS, 0 },
8a2c8fef 936 { STRING_COMMA_LEN ("i8086"), PROCESSOR_UNKNOWN,
293f5f65 937 CPU_NONE_FLAGS, 0 },
8a2c8fef 938 { STRING_COMMA_LEN ("i186"), PROCESSOR_UNKNOWN,
293f5f65 939 CPU_I186_FLAGS, 0 },
8a2c8fef 940 { STRING_COMMA_LEN ("i286"), PROCESSOR_UNKNOWN,
293f5f65 941 CPU_I286_FLAGS, 0 },
8a2c8fef 942 { STRING_COMMA_LEN ("i386"), PROCESSOR_I386,
293f5f65 943 CPU_I386_FLAGS, 0 },
8a2c8fef 944 { STRING_COMMA_LEN ("i486"), PROCESSOR_I486,
293f5f65 945 CPU_I486_FLAGS, 0 },
8a2c8fef 946 { STRING_COMMA_LEN ("i586"), PROCESSOR_PENTIUM,
293f5f65 947 CPU_I586_FLAGS, 0 },
8a2c8fef 948 { STRING_COMMA_LEN ("i686"), PROCESSOR_PENTIUMPRO,
293f5f65 949 CPU_I686_FLAGS, 0 },
8a2c8fef 950 { STRING_COMMA_LEN ("pentium"), PROCESSOR_PENTIUM,
293f5f65 951 CPU_I586_FLAGS, 0 },
8a2c8fef 952 { STRING_COMMA_LEN ("pentiumpro"), PROCESSOR_PENTIUMPRO,
293f5f65 953 CPU_PENTIUMPRO_FLAGS, 0 },
8a2c8fef 954 { STRING_COMMA_LEN ("pentiumii"), PROCESSOR_PENTIUMPRO,
293f5f65 955 CPU_P2_FLAGS, 0 },
8a2c8fef 956 { STRING_COMMA_LEN ("pentiumiii"),PROCESSOR_PENTIUMPRO,
293f5f65 957 CPU_P3_FLAGS, 0 },
8a2c8fef 958 { STRING_COMMA_LEN ("pentium4"), PROCESSOR_PENTIUM4,
293f5f65 959 CPU_P4_FLAGS, 0 },
8a2c8fef 960 { STRING_COMMA_LEN ("prescott"), PROCESSOR_NOCONA,
293f5f65 961 CPU_CORE_FLAGS, 0 },
8a2c8fef 962 { STRING_COMMA_LEN ("nocona"), PROCESSOR_NOCONA,
293f5f65 963 CPU_NOCONA_FLAGS, 0 },
8a2c8fef 964 { STRING_COMMA_LEN ("yonah"), PROCESSOR_CORE,
293f5f65 965 CPU_CORE_FLAGS, 1 },
8a2c8fef 966 { STRING_COMMA_LEN ("core"), PROCESSOR_CORE,
293f5f65 967 CPU_CORE_FLAGS, 0 },
8a2c8fef 968 { STRING_COMMA_LEN ("merom"), PROCESSOR_CORE2,
293f5f65 969 CPU_CORE2_FLAGS, 1 },
8a2c8fef 970 { STRING_COMMA_LEN ("core2"), PROCESSOR_CORE2,
293f5f65 971 CPU_CORE2_FLAGS, 0 },
8a2c8fef 972 { STRING_COMMA_LEN ("corei7"), PROCESSOR_COREI7,
293f5f65 973 CPU_COREI7_FLAGS, 0 },
8a2c8fef 974 { STRING_COMMA_LEN ("l1om"), PROCESSOR_L1OM,
293f5f65 975 CPU_L1OM_FLAGS, 0 },
7a9068fe 976 { STRING_COMMA_LEN ("k1om"), PROCESSOR_K1OM,
293f5f65 977 CPU_K1OM_FLAGS, 0 },
81486035 978 { STRING_COMMA_LEN ("iamcu"), PROCESSOR_IAMCU,
293f5f65 979 CPU_IAMCU_FLAGS, 0 },
8a2c8fef 980 { STRING_COMMA_LEN ("k6"), PROCESSOR_K6,
293f5f65 981 CPU_K6_FLAGS, 0 },
8a2c8fef 982 { STRING_COMMA_LEN ("k6_2"), PROCESSOR_K6,
293f5f65 983 CPU_K6_2_FLAGS, 0 },
8a2c8fef 984 { STRING_COMMA_LEN ("athlon"), PROCESSOR_ATHLON,
293f5f65 985 CPU_ATHLON_FLAGS, 0 },
8a2c8fef 986 { STRING_COMMA_LEN ("sledgehammer"), PROCESSOR_K8,
293f5f65 987 CPU_K8_FLAGS, 1 },
8a2c8fef 988 { STRING_COMMA_LEN ("opteron"), PROCESSOR_K8,
293f5f65 989 CPU_K8_FLAGS, 0 },
8a2c8fef 990 { STRING_COMMA_LEN ("k8"), PROCESSOR_K8,
293f5f65 991 CPU_K8_FLAGS, 0 },
8a2c8fef 992 { STRING_COMMA_LEN ("amdfam10"), PROCESSOR_AMDFAM10,
293f5f65 993 CPU_AMDFAM10_FLAGS, 0 },
8aedb9fe 994 { STRING_COMMA_LEN ("bdver1"), PROCESSOR_BD,
293f5f65 995 CPU_BDVER1_FLAGS, 0 },
8aedb9fe 996 { STRING_COMMA_LEN ("bdver2"), PROCESSOR_BD,
293f5f65 997 CPU_BDVER2_FLAGS, 0 },
5e5c50d3 998 { STRING_COMMA_LEN ("bdver3"), PROCESSOR_BD,
293f5f65 999 CPU_BDVER3_FLAGS, 0 },
c7b0bd56 1000 { STRING_COMMA_LEN ("bdver4"), PROCESSOR_BD,
293f5f65 1001 CPU_BDVER4_FLAGS, 0 },
029f3522 1002 { STRING_COMMA_LEN ("znver1"), PROCESSOR_ZNVER,
293f5f65 1003 CPU_ZNVER1_FLAGS, 0 },
a9660a6f
AP
1004 { STRING_COMMA_LEN ("znver2"), PROCESSOR_ZNVER,
1005 CPU_ZNVER2_FLAGS, 0 },
646cc3e0
GG
1006 { STRING_COMMA_LEN ("znver3"), PROCESSOR_ZNVER,
1007 CPU_ZNVER3_FLAGS, 0 },
7b458c12 1008 { STRING_COMMA_LEN ("btver1"), PROCESSOR_BT,
293f5f65 1009 CPU_BTVER1_FLAGS, 0 },
7b458c12 1010 { STRING_COMMA_LEN ("btver2"), PROCESSOR_BT,
293f5f65 1011 CPU_BTVER2_FLAGS, 0 },
8a2c8fef 1012 { STRING_COMMA_LEN (".8087"), PROCESSOR_UNKNOWN,
293f5f65 1013 CPU_8087_FLAGS, 0 },
8a2c8fef 1014 { STRING_COMMA_LEN (".287"), PROCESSOR_UNKNOWN,
293f5f65 1015 CPU_287_FLAGS, 0 },
8a2c8fef 1016 { STRING_COMMA_LEN (".387"), PROCESSOR_UNKNOWN,
293f5f65 1017 CPU_387_FLAGS, 0 },
1848e567
L
1018 { STRING_COMMA_LEN (".687"), PROCESSOR_UNKNOWN,
1019 CPU_687_FLAGS, 0 },
d871f3f4
L
1020 { STRING_COMMA_LEN (".cmov"), PROCESSOR_UNKNOWN,
1021 CPU_CMOV_FLAGS, 0 },
1022 { STRING_COMMA_LEN (".fxsr"), PROCESSOR_UNKNOWN,
1023 CPU_FXSR_FLAGS, 0 },
8a2c8fef 1024 { STRING_COMMA_LEN (".mmx"), PROCESSOR_UNKNOWN,
293f5f65 1025 CPU_MMX_FLAGS, 0 },
8a2c8fef 1026 { STRING_COMMA_LEN (".sse"), PROCESSOR_UNKNOWN,
293f5f65 1027 CPU_SSE_FLAGS, 0 },
8a2c8fef 1028 { STRING_COMMA_LEN (".sse2"), PROCESSOR_UNKNOWN,
293f5f65 1029 CPU_SSE2_FLAGS, 0 },
8a2c8fef 1030 { STRING_COMMA_LEN (".sse3"), PROCESSOR_UNKNOWN,
293f5f65 1031 CPU_SSE3_FLAGS, 0 },
af5c13b0
L
1032 { STRING_COMMA_LEN (".sse4a"), PROCESSOR_UNKNOWN,
1033 CPU_SSE4A_FLAGS, 0 },
8a2c8fef 1034 { STRING_COMMA_LEN (".ssse3"), PROCESSOR_UNKNOWN,
293f5f65 1035 CPU_SSSE3_FLAGS, 0 },
8a2c8fef 1036 { STRING_COMMA_LEN (".sse4.1"), PROCESSOR_UNKNOWN,
293f5f65 1037 CPU_SSE4_1_FLAGS, 0 },
8a2c8fef 1038 { STRING_COMMA_LEN (".sse4.2"), PROCESSOR_UNKNOWN,
293f5f65 1039 CPU_SSE4_2_FLAGS, 0 },
8a2c8fef 1040 { STRING_COMMA_LEN (".sse4"), PROCESSOR_UNKNOWN,
293f5f65 1041 CPU_SSE4_2_FLAGS, 0 },
8a2c8fef 1042 { STRING_COMMA_LEN (".avx"), PROCESSOR_UNKNOWN,
293f5f65 1043 CPU_AVX_FLAGS, 0 },
6c30d220 1044 { STRING_COMMA_LEN (".avx2"), PROCESSOR_UNKNOWN,
293f5f65 1045 CPU_AVX2_FLAGS, 0 },
43234a1e 1046 { STRING_COMMA_LEN (".avx512f"), PROCESSOR_UNKNOWN,
293f5f65 1047 CPU_AVX512F_FLAGS, 0 },
43234a1e 1048 { STRING_COMMA_LEN (".avx512cd"), PROCESSOR_UNKNOWN,
293f5f65 1049 CPU_AVX512CD_FLAGS, 0 },
43234a1e 1050 { STRING_COMMA_LEN (".avx512er"), PROCESSOR_UNKNOWN,
293f5f65 1051 CPU_AVX512ER_FLAGS, 0 },
43234a1e 1052 { STRING_COMMA_LEN (".avx512pf"), PROCESSOR_UNKNOWN,
293f5f65 1053 CPU_AVX512PF_FLAGS, 0 },
1dfc6506 1054 { STRING_COMMA_LEN (".avx512dq"), PROCESSOR_UNKNOWN,
293f5f65 1055 CPU_AVX512DQ_FLAGS, 0 },
1dfc6506 1056 { STRING_COMMA_LEN (".avx512bw"), PROCESSOR_UNKNOWN,
293f5f65 1057 CPU_AVX512BW_FLAGS, 0 },
1dfc6506 1058 { STRING_COMMA_LEN (".avx512vl"), PROCESSOR_UNKNOWN,
293f5f65 1059 CPU_AVX512VL_FLAGS, 0 },
8a2c8fef 1060 { STRING_COMMA_LEN (".vmx"), PROCESSOR_UNKNOWN,
293f5f65 1061 CPU_VMX_FLAGS, 0 },
8729a6f6 1062 { STRING_COMMA_LEN (".vmfunc"), PROCESSOR_UNKNOWN,
293f5f65 1063 CPU_VMFUNC_FLAGS, 0 },
8a2c8fef 1064 { STRING_COMMA_LEN (".smx"), PROCESSOR_UNKNOWN,
293f5f65 1065 CPU_SMX_FLAGS, 0 },
8a2c8fef 1066 { STRING_COMMA_LEN (".xsave"), PROCESSOR_UNKNOWN,
293f5f65 1067 CPU_XSAVE_FLAGS, 0 },
c7b8aa3a 1068 { STRING_COMMA_LEN (".xsaveopt"), PROCESSOR_UNKNOWN,
293f5f65 1069 CPU_XSAVEOPT_FLAGS, 0 },
1dfc6506 1070 { STRING_COMMA_LEN (".xsavec"), PROCESSOR_UNKNOWN,
293f5f65 1071 CPU_XSAVEC_FLAGS, 0 },
1dfc6506 1072 { STRING_COMMA_LEN (".xsaves"), PROCESSOR_UNKNOWN,
293f5f65 1073 CPU_XSAVES_FLAGS, 0 },
8a2c8fef 1074 { STRING_COMMA_LEN (".aes"), PROCESSOR_UNKNOWN,
293f5f65 1075 CPU_AES_FLAGS, 0 },
8a2c8fef 1076 { STRING_COMMA_LEN (".pclmul"), PROCESSOR_UNKNOWN,
293f5f65 1077 CPU_PCLMUL_FLAGS, 0 },
8a2c8fef 1078 { STRING_COMMA_LEN (".clmul"), PROCESSOR_UNKNOWN,
293f5f65 1079 CPU_PCLMUL_FLAGS, 1 },
c7b8aa3a 1080 { STRING_COMMA_LEN (".fsgsbase"), PROCESSOR_UNKNOWN,
293f5f65 1081 CPU_FSGSBASE_FLAGS, 0 },
c7b8aa3a 1082 { STRING_COMMA_LEN (".rdrnd"), PROCESSOR_UNKNOWN,
293f5f65 1083 CPU_RDRND_FLAGS, 0 },
c7b8aa3a 1084 { STRING_COMMA_LEN (".f16c"), PROCESSOR_UNKNOWN,
293f5f65 1085 CPU_F16C_FLAGS, 0 },
6c30d220 1086 { STRING_COMMA_LEN (".bmi2"), PROCESSOR_UNKNOWN,
293f5f65 1087 CPU_BMI2_FLAGS, 0 },
8a2c8fef 1088 { STRING_COMMA_LEN (".fma"), PROCESSOR_UNKNOWN,
293f5f65 1089 CPU_FMA_FLAGS, 0 },
8a2c8fef 1090 { STRING_COMMA_LEN (".fma4"), PROCESSOR_UNKNOWN,
293f5f65 1091 CPU_FMA4_FLAGS, 0 },
8a2c8fef 1092 { STRING_COMMA_LEN (".xop"), PROCESSOR_UNKNOWN,
293f5f65 1093 CPU_XOP_FLAGS, 0 },
8a2c8fef 1094 { STRING_COMMA_LEN (".lwp"), PROCESSOR_UNKNOWN,
293f5f65 1095 CPU_LWP_FLAGS, 0 },
8a2c8fef 1096 { STRING_COMMA_LEN (".movbe"), PROCESSOR_UNKNOWN,
293f5f65 1097 CPU_MOVBE_FLAGS, 0 },
60aa667e 1098 { STRING_COMMA_LEN (".cx16"), PROCESSOR_UNKNOWN,
293f5f65 1099 CPU_CX16_FLAGS, 0 },
8a2c8fef 1100 { STRING_COMMA_LEN (".ept"), PROCESSOR_UNKNOWN,
293f5f65 1101 CPU_EPT_FLAGS, 0 },
6c30d220 1102 { STRING_COMMA_LEN (".lzcnt"), PROCESSOR_UNKNOWN,
293f5f65 1103 CPU_LZCNT_FLAGS, 0 },
272a84b1
L
1104 { STRING_COMMA_LEN (".popcnt"), PROCESSOR_UNKNOWN,
1105 CPU_POPCNT_FLAGS, 0 },
42164a71 1106 { STRING_COMMA_LEN (".hle"), PROCESSOR_UNKNOWN,
293f5f65 1107 CPU_HLE_FLAGS, 0 },
42164a71 1108 { STRING_COMMA_LEN (".rtm"), PROCESSOR_UNKNOWN,
293f5f65 1109 CPU_RTM_FLAGS, 0 },
6c30d220 1110 { STRING_COMMA_LEN (".invpcid"), PROCESSOR_UNKNOWN,
293f5f65 1111 CPU_INVPCID_FLAGS, 0 },
8a2c8fef 1112 { STRING_COMMA_LEN (".clflush"), PROCESSOR_UNKNOWN,
293f5f65 1113 CPU_CLFLUSH_FLAGS, 0 },
22109423 1114 { STRING_COMMA_LEN (".nop"), PROCESSOR_UNKNOWN,
293f5f65 1115 CPU_NOP_FLAGS, 0 },
8a2c8fef 1116 { STRING_COMMA_LEN (".syscall"), PROCESSOR_UNKNOWN,
293f5f65 1117 CPU_SYSCALL_FLAGS, 0 },
8a2c8fef 1118 { STRING_COMMA_LEN (".rdtscp"), PROCESSOR_UNKNOWN,
293f5f65 1119 CPU_RDTSCP_FLAGS, 0 },
8a2c8fef 1120 { STRING_COMMA_LEN (".3dnow"), PROCESSOR_UNKNOWN,
293f5f65 1121 CPU_3DNOW_FLAGS, 0 },
8a2c8fef 1122 { STRING_COMMA_LEN (".3dnowa"), PROCESSOR_UNKNOWN,
293f5f65 1123 CPU_3DNOWA_FLAGS, 0 },
8a2c8fef 1124 { STRING_COMMA_LEN (".padlock"), PROCESSOR_UNKNOWN,
293f5f65 1125 CPU_PADLOCK_FLAGS, 0 },
8a2c8fef 1126 { STRING_COMMA_LEN (".pacifica"), PROCESSOR_UNKNOWN,
293f5f65 1127 CPU_SVME_FLAGS, 1 },
8a2c8fef 1128 { STRING_COMMA_LEN (".svme"), PROCESSOR_UNKNOWN,
293f5f65 1129 CPU_SVME_FLAGS, 0 },
8a2c8fef 1130 { STRING_COMMA_LEN (".sse4a"), PROCESSOR_UNKNOWN,
293f5f65 1131 CPU_SSE4A_FLAGS, 0 },
8a2c8fef 1132 { STRING_COMMA_LEN (".abm"), PROCESSOR_UNKNOWN,
293f5f65 1133 CPU_ABM_FLAGS, 0 },
87973e9f 1134 { STRING_COMMA_LEN (".bmi"), PROCESSOR_UNKNOWN,
293f5f65 1135 CPU_BMI_FLAGS, 0 },
2a2a0f38 1136 { STRING_COMMA_LEN (".tbm"), PROCESSOR_UNKNOWN,
293f5f65 1137 CPU_TBM_FLAGS, 0 },
e2e1fcde 1138 { STRING_COMMA_LEN (".adx"), PROCESSOR_UNKNOWN,
293f5f65 1139 CPU_ADX_FLAGS, 0 },
e2e1fcde 1140 { STRING_COMMA_LEN (".rdseed"), PROCESSOR_UNKNOWN,
293f5f65 1141 CPU_RDSEED_FLAGS, 0 },
e2e1fcde 1142 { STRING_COMMA_LEN (".prfchw"), PROCESSOR_UNKNOWN,
293f5f65 1143 CPU_PRFCHW_FLAGS, 0 },
5c111e37 1144 { STRING_COMMA_LEN (".smap"), PROCESSOR_UNKNOWN,
293f5f65 1145 CPU_SMAP_FLAGS, 0 },
7e8b059b 1146 { STRING_COMMA_LEN (".mpx"), PROCESSOR_UNKNOWN,
293f5f65 1147 CPU_MPX_FLAGS, 0 },
a0046408 1148 { STRING_COMMA_LEN (".sha"), PROCESSOR_UNKNOWN,
293f5f65 1149 CPU_SHA_FLAGS, 0 },
963f3586 1150 { STRING_COMMA_LEN (".clflushopt"), PROCESSOR_UNKNOWN,
293f5f65 1151 CPU_CLFLUSHOPT_FLAGS, 0 },
dcf893b5 1152 { STRING_COMMA_LEN (".prefetchwt1"), PROCESSOR_UNKNOWN,
293f5f65 1153 CPU_PREFETCHWT1_FLAGS, 0 },
2cf200a4 1154 { STRING_COMMA_LEN (".se1"), PROCESSOR_UNKNOWN,
293f5f65 1155 CPU_SE1_FLAGS, 0 },
c5e7287a 1156 { STRING_COMMA_LEN (".clwb"), PROCESSOR_UNKNOWN,
293f5f65 1157 CPU_CLWB_FLAGS, 0 },
2cc1b5aa 1158 { STRING_COMMA_LEN (".avx512ifma"), PROCESSOR_UNKNOWN,
293f5f65 1159 CPU_AVX512IFMA_FLAGS, 0 },
14f195c9 1160 { STRING_COMMA_LEN (".avx512vbmi"), PROCESSOR_UNKNOWN,
293f5f65 1161 CPU_AVX512VBMI_FLAGS, 0 },
920d2ddc
IT
1162 { STRING_COMMA_LEN (".avx512_4fmaps"), PROCESSOR_UNKNOWN,
1163 CPU_AVX512_4FMAPS_FLAGS, 0 },
47acf0bd
IT
1164 { STRING_COMMA_LEN (".avx512_4vnniw"), PROCESSOR_UNKNOWN,
1165 CPU_AVX512_4VNNIW_FLAGS, 0 },
620214f7
IT
1166 { STRING_COMMA_LEN (".avx512_vpopcntdq"), PROCESSOR_UNKNOWN,
1167 CPU_AVX512_VPOPCNTDQ_FLAGS, 0 },
53467f57
IT
1168 { STRING_COMMA_LEN (".avx512_vbmi2"), PROCESSOR_UNKNOWN,
1169 CPU_AVX512_VBMI2_FLAGS, 0 },
8cfcb765
IT
1170 { STRING_COMMA_LEN (".avx512_vnni"), PROCESSOR_UNKNOWN,
1171 CPU_AVX512_VNNI_FLAGS, 0 },
ee6872be
IT
1172 { STRING_COMMA_LEN (".avx512_bitalg"), PROCESSOR_UNKNOWN,
1173 CPU_AVX512_BITALG_FLAGS, 0 },
58bf9b6a
L
1174 { STRING_COMMA_LEN (".avx_vnni"), PROCESSOR_UNKNOWN,
1175 CPU_AVX_VNNI_FLAGS, 0 },
029f3522 1176 { STRING_COMMA_LEN (".clzero"), PROCESSOR_UNKNOWN,
293f5f65 1177 CPU_CLZERO_FLAGS, 0 },
9916071f 1178 { STRING_COMMA_LEN (".mwaitx"), PROCESSOR_UNKNOWN,
293f5f65 1179 CPU_MWAITX_FLAGS, 0 },
8eab4136 1180 { STRING_COMMA_LEN (".ospke"), PROCESSOR_UNKNOWN,
293f5f65 1181 CPU_OSPKE_FLAGS, 0 },
8bc52696 1182 { STRING_COMMA_LEN (".rdpid"), PROCESSOR_UNKNOWN,
293f5f65 1183 CPU_RDPID_FLAGS, 0 },
6b40c462
L
1184 { STRING_COMMA_LEN (".ptwrite"), PROCESSOR_UNKNOWN,
1185 CPU_PTWRITE_FLAGS, 0 },
d777820b
IT
1186 { STRING_COMMA_LEN (".ibt"), PROCESSOR_UNKNOWN,
1187 CPU_IBT_FLAGS, 0 },
1188 { STRING_COMMA_LEN (".shstk"), PROCESSOR_UNKNOWN,
1189 CPU_SHSTK_FLAGS, 0 },
48521003
IT
1190 { STRING_COMMA_LEN (".gfni"), PROCESSOR_UNKNOWN,
1191 CPU_GFNI_FLAGS, 0 },
8dcf1fad
IT
1192 { STRING_COMMA_LEN (".vaes"), PROCESSOR_UNKNOWN,
1193 CPU_VAES_FLAGS, 0 },
ff1982d5
IT
1194 { STRING_COMMA_LEN (".vpclmulqdq"), PROCESSOR_UNKNOWN,
1195 CPU_VPCLMULQDQ_FLAGS, 0 },
3233d7d0
IT
1196 { STRING_COMMA_LEN (".wbnoinvd"), PROCESSOR_UNKNOWN,
1197 CPU_WBNOINVD_FLAGS, 0 },
be3a8dca
IT
1198 { STRING_COMMA_LEN (".pconfig"), PROCESSOR_UNKNOWN,
1199 CPU_PCONFIG_FLAGS, 0 },
de89d0a3
IT
1200 { STRING_COMMA_LEN (".waitpkg"), PROCESSOR_UNKNOWN,
1201 CPU_WAITPKG_FLAGS, 0 },
c48935d7
IT
1202 { STRING_COMMA_LEN (".cldemote"), PROCESSOR_UNKNOWN,
1203 CPU_CLDEMOTE_FLAGS, 0 },
260cd341
LC
1204 { STRING_COMMA_LEN (".amx_int8"), PROCESSOR_UNKNOWN,
1205 CPU_AMX_INT8_FLAGS, 0 },
1206 { STRING_COMMA_LEN (".amx_bf16"), PROCESSOR_UNKNOWN,
1207 CPU_AMX_BF16_FLAGS, 0 },
1208 { STRING_COMMA_LEN (".amx_tile"), PROCESSOR_UNKNOWN,
1209 CPU_AMX_TILE_FLAGS, 0 },
c0a30a9f
L
1210 { STRING_COMMA_LEN (".movdiri"), PROCESSOR_UNKNOWN,
1211 CPU_MOVDIRI_FLAGS, 0 },
1212 { STRING_COMMA_LEN (".movdir64b"), PROCESSOR_UNKNOWN,
1213 CPU_MOVDIR64B_FLAGS, 0 },
d6aab7a1
XG
1214 { STRING_COMMA_LEN (".avx512_bf16"), PROCESSOR_UNKNOWN,
1215 CPU_AVX512_BF16_FLAGS, 0 },
9186c494
L
1216 { STRING_COMMA_LEN (".avx512_vp2intersect"), PROCESSOR_UNKNOWN,
1217 CPU_AVX512_VP2INTERSECT_FLAGS, 0 },
81d54bb7
CL
1218 { STRING_COMMA_LEN (".tdx"), PROCESSOR_UNKNOWN,
1219 CPU_TDX_FLAGS, 0 },
dd455cf5
L
1220 { STRING_COMMA_LEN (".enqcmd"), PROCESSOR_UNKNOWN,
1221 CPU_ENQCMD_FLAGS, 0 },
4b27d27c
L
1222 { STRING_COMMA_LEN (".serialize"), PROCESSOR_UNKNOWN,
1223 CPU_SERIALIZE_FLAGS, 0 },
142861df
JB
1224 { STRING_COMMA_LEN (".rdpru"), PROCESSOR_UNKNOWN,
1225 CPU_RDPRU_FLAGS, 0 },
1226 { STRING_COMMA_LEN (".mcommit"), PROCESSOR_UNKNOWN,
1227 CPU_MCOMMIT_FLAGS, 0 },
a847e322
JB
1228 { STRING_COMMA_LEN (".sev_es"), PROCESSOR_UNKNOWN,
1229 CPU_SEV_ES_FLAGS, 0 },
bb651e8b
CL
1230 { STRING_COMMA_LEN (".tsxldtrk"), PROCESSOR_UNKNOWN,
1231 CPU_TSXLDTRK_FLAGS, 0 },
c4694f17
TG
1232 { STRING_COMMA_LEN (".kl"), PROCESSOR_UNKNOWN,
1233 CPU_KL_FLAGS, 0 },
1234 { STRING_COMMA_LEN (".widekl"), PROCESSOR_UNKNOWN,
1235 CPU_WIDEKL_FLAGS, 0 },
f64c42a9
LC
1236 { STRING_COMMA_LEN (".uintr"), PROCESSOR_UNKNOWN,
1237 CPU_UINTR_FLAGS, 0 },
c1fa250a
LC
1238 { STRING_COMMA_LEN (".hreset"), PROCESSOR_UNKNOWN,
1239 CPU_HRESET_FLAGS, 0 },
293f5f65
L
1240};
1241
1242static const noarch_entry cpu_noarch[] =
1243{
1244 { STRING_COMMA_LEN ("no87"), CPU_ANY_X87_FLAGS },
1848e567
L
1245 { STRING_COMMA_LEN ("no287"), CPU_ANY_287_FLAGS },
1246 { STRING_COMMA_LEN ("no387"), CPU_ANY_387_FLAGS },
1247 { STRING_COMMA_LEN ("no687"), CPU_ANY_687_FLAGS },
d871f3f4
L
1248 { STRING_COMMA_LEN ("nocmov"), CPU_ANY_CMOV_FLAGS },
1249 { STRING_COMMA_LEN ("nofxsr"), CPU_ANY_FXSR_FLAGS },
293f5f65
L
1250 { STRING_COMMA_LEN ("nommx"), CPU_ANY_MMX_FLAGS },
1251 { STRING_COMMA_LEN ("nosse"), CPU_ANY_SSE_FLAGS },
1848e567
L
1252 { STRING_COMMA_LEN ("nosse2"), CPU_ANY_SSE2_FLAGS },
1253 { STRING_COMMA_LEN ("nosse3"), CPU_ANY_SSE3_FLAGS },
af5c13b0 1254 { STRING_COMMA_LEN ("nosse4a"), CPU_ANY_SSE4A_FLAGS },
1848e567
L
1255 { STRING_COMMA_LEN ("nossse3"), CPU_ANY_SSSE3_FLAGS },
1256 { STRING_COMMA_LEN ("nosse4.1"), CPU_ANY_SSE4_1_FLAGS },
1257 { STRING_COMMA_LEN ("nosse4.2"), CPU_ANY_SSE4_2_FLAGS },
af5c13b0 1258 { STRING_COMMA_LEN ("nosse4"), CPU_ANY_SSE4_1_FLAGS },
293f5f65 1259 { STRING_COMMA_LEN ("noavx"), CPU_ANY_AVX_FLAGS },
1848e567 1260 { STRING_COMMA_LEN ("noavx2"), CPU_ANY_AVX2_FLAGS },
144b71e2
L
1261 { STRING_COMMA_LEN ("noavx512f"), CPU_ANY_AVX512F_FLAGS },
1262 { STRING_COMMA_LEN ("noavx512cd"), CPU_ANY_AVX512CD_FLAGS },
1263 { STRING_COMMA_LEN ("noavx512er"), CPU_ANY_AVX512ER_FLAGS },
1264 { STRING_COMMA_LEN ("noavx512pf"), CPU_ANY_AVX512PF_FLAGS },
1265 { STRING_COMMA_LEN ("noavx512dq"), CPU_ANY_AVX512DQ_FLAGS },
1266 { STRING_COMMA_LEN ("noavx512bw"), CPU_ANY_AVX512BW_FLAGS },
1267 { STRING_COMMA_LEN ("noavx512vl"), CPU_ANY_AVX512VL_FLAGS },
1268 { STRING_COMMA_LEN ("noavx512ifma"), CPU_ANY_AVX512IFMA_FLAGS },
1269 { STRING_COMMA_LEN ("noavx512vbmi"), CPU_ANY_AVX512VBMI_FLAGS },
920d2ddc 1270 { STRING_COMMA_LEN ("noavx512_4fmaps"), CPU_ANY_AVX512_4FMAPS_FLAGS },
47acf0bd 1271 { STRING_COMMA_LEN ("noavx512_4vnniw"), CPU_ANY_AVX512_4VNNIW_FLAGS },
620214f7 1272 { STRING_COMMA_LEN ("noavx512_vpopcntdq"), CPU_ANY_AVX512_VPOPCNTDQ_FLAGS },
53467f57 1273 { STRING_COMMA_LEN ("noavx512_vbmi2"), CPU_ANY_AVX512_VBMI2_FLAGS },
8cfcb765 1274 { STRING_COMMA_LEN ("noavx512_vnni"), CPU_ANY_AVX512_VNNI_FLAGS },
ee6872be 1275 { STRING_COMMA_LEN ("noavx512_bitalg"), CPU_ANY_AVX512_BITALG_FLAGS },
58bf9b6a 1276 { STRING_COMMA_LEN ("noavx_vnni"), CPU_ANY_AVX_VNNI_FLAGS },
d777820b
IT
1277 { STRING_COMMA_LEN ("noibt"), CPU_ANY_IBT_FLAGS },
1278 { STRING_COMMA_LEN ("noshstk"), CPU_ANY_SHSTK_FLAGS },
260cd341
LC
1279 { STRING_COMMA_LEN ("noamx_int8"), CPU_ANY_AMX_INT8_FLAGS },
1280 { STRING_COMMA_LEN ("noamx_bf16"), CPU_ANY_AMX_BF16_FLAGS },
1281 { STRING_COMMA_LEN ("noamx_tile"), CPU_ANY_AMX_TILE_FLAGS },
c0a30a9f
L
1282 { STRING_COMMA_LEN ("nomovdiri"), CPU_ANY_MOVDIRI_FLAGS },
1283 { STRING_COMMA_LEN ("nomovdir64b"), CPU_ANY_MOVDIR64B_FLAGS },
d6aab7a1 1284 { STRING_COMMA_LEN ("noavx512_bf16"), CPU_ANY_AVX512_BF16_FLAGS },
708a2fff
CL
1285 { STRING_COMMA_LEN ("noavx512_vp2intersect"),
1286 CPU_ANY_AVX512_VP2INTERSECT_FLAGS },
81d54bb7 1287 { STRING_COMMA_LEN ("notdx"), CPU_ANY_TDX_FLAGS },
dd455cf5 1288 { STRING_COMMA_LEN ("noenqcmd"), CPU_ANY_ENQCMD_FLAGS },
4b27d27c 1289 { STRING_COMMA_LEN ("noserialize"), CPU_ANY_SERIALIZE_FLAGS },
bb651e8b 1290 { STRING_COMMA_LEN ("notsxldtrk"), CPU_ANY_TSXLDTRK_FLAGS },
c4694f17
TG
1291 { STRING_COMMA_LEN ("nokl"), CPU_ANY_KL_FLAGS },
1292 { STRING_COMMA_LEN ("nowidekl"), CPU_ANY_WIDEKL_FLAGS },
f64c42a9 1293 { STRING_COMMA_LEN ("nouintr"), CPU_ANY_UINTR_FLAGS },
c1fa250a 1294 { STRING_COMMA_LEN ("nohreset"), CPU_ANY_HRESET_FLAGS },
e413e4e9
AM
1295};
1296
704209c0 1297#ifdef I386COFF
a6c24e68
NC
1298/* Like s_lcomm_internal in gas/read.c but the alignment string
1299 is allowed to be optional. */
1300
1301static symbolS *
1302pe_lcomm_internal (int needs_align, symbolS *symbolP, addressT size)
1303{
1304 addressT align = 0;
1305
1306 SKIP_WHITESPACE ();
1307
7ab9ffdd 1308 if (needs_align
a6c24e68
NC
1309 && *input_line_pointer == ',')
1310 {
1311 align = parse_align (needs_align - 1);
7ab9ffdd 1312
a6c24e68
NC
1313 if (align == (addressT) -1)
1314 return NULL;
1315 }
1316 else
1317 {
1318 if (size >= 8)
1319 align = 3;
1320 else if (size >= 4)
1321 align = 2;
1322 else if (size >= 2)
1323 align = 1;
1324 else
1325 align = 0;
1326 }
1327
1328 bss_alloc (symbolP, size, align);
1329 return symbolP;
1330}
1331
704209c0 1332static void
a6c24e68
NC
1333pe_lcomm (int needs_align)
1334{
1335 s_comm_internal (needs_align * 2, pe_lcomm_internal);
1336}
704209c0 1337#endif
a6c24e68 1338
29b0f896
AM
1339const pseudo_typeS md_pseudo_table[] =
1340{
1341#if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
1342 {"align", s_align_bytes, 0},
1343#else
1344 {"align", s_align_ptwo, 0},
1345#endif
1346 {"arch", set_cpu_arch, 0},
1347#ifndef I386COFF
1348 {"bss", s_bss, 0},
a6c24e68
NC
1349#else
1350 {"lcomm", pe_lcomm, 1},
29b0f896
AM
1351#endif
1352 {"ffloat", float_cons, 'f'},
1353 {"dfloat", float_cons, 'd'},
1354 {"tfloat", float_cons, 'x'},
1355 {"value", cons, 2},
d182319b 1356 {"slong", signed_cons, 4},
29b0f896
AM
1357 {"noopt", s_ignore, 0},
1358 {"optim", s_ignore, 0},
1359 {"code16gcc", set_16bit_gcc_code_flag, CODE_16BIT},
1360 {"code16", set_code_flag, CODE_16BIT},
1361 {"code32", set_code_flag, CODE_32BIT},
da5f19a2 1362#ifdef BFD64
29b0f896 1363 {"code64", set_code_flag, CODE_64BIT},
da5f19a2 1364#endif
29b0f896
AM
1365 {"intel_syntax", set_intel_syntax, 1},
1366 {"att_syntax", set_intel_syntax, 0},
1efbbeb4
L
1367 {"intel_mnemonic", set_intel_mnemonic, 1},
1368 {"att_mnemonic", set_intel_mnemonic, 0},
db51cc60
L
1369 {"allow_index_reg", set_allow_index_reg, 1},
1370 {"disallow_index_reg", set_allow_index_reg, 0},
7bab8ab5
JB
1371 {"sse_check", set_check, 0},
1372 {"operand_check", set_check, 1},
3b22753a
L
1373#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1374 {"largecomm", handle_large_common, 0},
07a53e5c 1375#else
68d20676 1376 {"file", dwarf2_directive_file, 0},
07a53e5c
RH
1377 {"loc", dwarf2_directive_loc, 0},
1378 {"loc_mark_labels", dwarf2_directive_loc_mark_labels, 0},
3b22753a 1379#endif
6482c264
NC
1380#ifdef TE_PE
1381 {"secrel32", pe_directive_secrel, 0},
1382#endif
29b0f896
AM
1383 {0, 0, 0}
1384};
1385
1386/* For interface with expression (). */
1387extern char *input_line_pointer;
1388
1389/* Hash table for instruction mnemonic lookup. */
629310ab 1390static htab_t op_hash;
29b0f896
AM
1391
1392/* Hash table for register lookup. */
629310ab 1393static htab_t reg_hash;
29b0f896 1394\f
ce8a8b2f
AM
1395 /* Various efficient no-op patterns for aligning code labels.
1396 Note: Don't try to assemble the instructions in the comments.
1397 0L and 0w are not legal. */
62a02d25
L
1398static const unsigned char f32_1[] =
1399 {0x90}; /* nop */
1400static const unsigned char f32_2[] =
1401 {0x66,0x90}; /* xchg %ax,%ax */
1402static const unsigned char f32_3[] =
1403 {0x8d,0x76,0x00}; /* leal 0(%esi),%esi */
1404static const unsigned char f32_4[] =
1405 {0x8d,0x74,0x26,0x00}; /* leal 0(%esi,1),%esi */
62a02d25
L
1406static const unsigned char f32_6[] =
1407 {0x8d,0xb6,0x00,0x00,0x00,0x00}; /* leal 0L(%esi),%esi */
1408static const unsigned char f32_7[] =
1409 {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal 0L(%esi,1),%esi */
62a02d25 1410static const unsigned char f16_3[] =
3ae729d5 1411 {0x8d,0x74,0x00}; /* lea 0(%si),%si */
62a02d25 1412static const unsigned char f16_4[] =
3ae729d5
L
1413 {0x8d,0xb4,0x00,0x00}; /* lea 0W(%si),%si */
1414static const unsigned char jump_disp8[] =
1415 {0xeb}; /* jmp disp8 */
1416static const unsigned char jump32_disp32[] =
1417 {0xe9}; /* jmp disp32 */
1418static const unsigned char jump16_disp32[] =
1419 {0x66,0xe9}; /* jmp disp32 */
62a02d25
L
1420/* 32-bit NOPs patterns. */
1421static const unsigned char *const f32_patt[] = {
3ae729d5 1422 f32_1, f32_2, f32_3, f32_4, NULL, f32_6, f32_7
62a02d25
L
1423};
1424/* 16-bit NOPs patterns. */
1425static const unsigned char *const f16_patt[] = {
3ae729d5 1426 f32_1, f32_2, f16_3, f16_4
62a02d25
L
1427};
1428/* nopl (%[re]ax) */
1429static const unsigned char alt_3[] =
1430 {0x0f,0x1f,0x00};
1431/* nopl 0(%[re]ax) */
1432static const unsigned char alt_4[] =
1433 {0x0f,0x1f,0x40,0x00};
1434/* nopl 0(%[re]ax,%[re]ax,1) */
1435static const unsigned char alt_5[] =
1436 {0x0f,0x1f,0x44,0x00,0x00};
1437/* nopw 0(%[re]ax,%[re]ax,1) */
1438static const unsigned char alt_6[] =
1439 {0x66,0x0f,0x1f,0x44,0x00,0x00};
1440/* nopl 0L(%[re]ax) */
1441static const unsigned char alt_7[] =
1442 {0x0f,0x1f,0x80,0x00,0x00,0x00,0x00};
1443/* nopl 0L(%[re]ax,%[re]ax,1) */
1444static const unsigned char alt_8[] =
1445 {0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1446/* nopw 0L(%[re]ax,%[re]ax,1) */
1447static const unsigned char alt_9[] =
1448 {0x66,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1449/* nopw %cs:0L(%[re]ax,%[re]ax,1) */
1450static const unsigned char alt_10[] =
1451 {0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
3ae729d5
L
1452/* data16 nopw %cs:0L(%eax,%eax,1) */
1453static const unsigned char alt_11[] =
1454 {0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
62a02d25
L
1455/* 32-bit and 64-bit NOPs patterns. */
1456static const unsigned char *const alt_patt[] = {
1457 f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
3ae729d5 1458 alt_9, alt_10, alt_11
62a02d25
L
1459};
1460
1461/* Genenerate COUNT bytes of NOPs to WHERE from PATT with the maximum
1462 size of a single NOP instruction MAX_SINGLE_NOP_SIZE. */
1463
1464static void
1465i386_output_nops (char *where, const unsigned char *const *patt,
1466 int count, int max_single_nop_size)
1467
1468{
3ae729d5
L
1469 /* Place the longer NOP first. */
1470 int last;
1471 int offset;
3076e594
NC
1472 const unsigned char *nops;
1473
1474 if (max_single_nop_size < 1)
1475 {
1476 as_fatal (_("i386_output_nops called to generate nops of at most %d bytes!"),
1477 max_single_nop_size);
1478 return;
1479 }
1480
1481 nops = patt[max_single_nop_size - 1];
3ae729d5
L
1482
1483 /* Use the smaller one if the requsted one isn't available. */
1484 if (nops == NULL)
62a02d25 1485 {
3ae729d5
L
1486 max_single_nop_size--;
1487 nops = patt[max_single_nop_size - 1];
62a02d25
L
1488 }
1489
3ae729d5
L
1490 last = count % max_single_nop_size;
1491
1492 count -= last;
1493 for (offset = 0; offset < count; offset += max_single_nop_size)
1494 memcpy (where + offset, nops, max_single_nop_size);
1495
1496 if (last)
1497 {
1498 nops = patt[last - 1];
1499 if (nops == NULL)
1500 {
1501 /* Use the smaller one plus one-byte NOP if the needed one
1502 isn't available. */
1503 last--;
1504 nops = patt[last - 1];
1505 memcpy (where + offset, nops, last);
1506 where[offset + last] = *patt[0];
1507 }
1508 else
1509 memcpy (where + offset, nops, last);
1510 }
62a02d25
L
1511}
1512
3ae729d5
L
1513static INLINE int
1514fits_in_imm7 (offsetT num)
1515{
1516 return (num & 0x7f) == num;
1517}
1518
1519static INLINE int
1520fits_in_imm31 (offsetT num)
1521{
1522 return (num & 0x7fffffff) == num;
1523}
62a02d25
L
1524
1525/* Genenerate COUNT bytes of NOPs to WHERE with the maximum size of a
1526 single NOP instruction LIMIT. */
1527
1528void
3ae729d5 1529i386_generate_nops (fragS *fragP, char *where, offsetT count, int limit)
62a02d25 1530{
3ae729d5 1531 const unsigned char *const *patt = NULL;
62a02d25 1532 int max_single_nop_size;
3ae729d5
L
1533 /* Maximum number of NOPs before switching to jump over NOPs. */
1534 int max_number_of_nops;
62a02d25 1535
3ae729d5 1536 switch (fragP->fr_type)
62a02d25 1537 {
3ae729d5
L
1538 case rs_fill_nop:
1539 case rs_align_code:
1540 break;
e379e5f3
L
1541 case rs_machine_dependent:
1542 /* Allow NOP padding for jumps and calls. */
1543 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
1544 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
1545 break;
1546 /* Fall through. */
3ae729d5 1547 default:
62a02d25
L
1548 return;
1549 }
1550
ccc9c027
L
1551 /* We need to decide which NOP sequence to use for 32bit and
1552 64bit. When -mtune= is used:
4eed87de 1553
76bc74dc
L
1554 1. For PROCESSOR_I386, PROCESSOR_I486, PROCESSOR_PENTIUM and
1555 PROCESSOR_GENERIC32, f32_patt will be used.
80b8656c
L
1556 2. For the rest, alt_patt will be used.
1557
1558 When -mtune= isn't used, alt_patt will be used if
22109423 1559 cpu_arch_isa_flags has CpuNop. Otherwise, f32_patt will
76bc74dc 1560 be used.
ccc9c027
L
1561
1562 When -march= or .arch is used, we can't use anything beyond
1563 cpu_arch_isa_flags. */
1564
1565 if (flag_code == CODE_16BIT)
1566 {
3ae729d5
L
1567 patt = f16_patt;
1568 max_single_nop_size = sizeof (f16_patt) / sizeof (f16_patt[0]);
1569 /* Limit number of NOPs to 2 in 16-bit mode. */
1570 max_number_of_nops = 2;
252b5132 1571 }
33fef721 1572 else
ccc9c027 1573 {
fbf3f584 1574 if (fragP->tc_frag_data.isa == PROCESSOR_UNKNOWN)
ccc9c027
L
1575 {
1576 /* PROCESSOR_UNKNOWN means that all ISAs may be used. */
1577 switch (cpu_arch_tune)
1578 {
1579 case PROCESSOR_UNKNOWN:
1580 /* We use cpu_arch_isa_flags to check if we SHOULD
22109423
L
1581 optimize with nops. */
1582 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1583 patt = alt_patt;
ccc9c027
L
1584 else
1585 patt = f32_patt;
1586 break;
ccc9c027
L
1587 case PROCESSOR_PENTIUM4:
1588 case PROCESSOR_NOCONA:
ef05d495 1589 case PROCESSOR_CORE:
76bc74dc 1590 case PROCESSOR_CORE2:
bd5295b2 1591 case PROCESSOR_COREI7:
3632d14b 1592 case PROCESSOR_L1OM:
7a9068fe 1593 case PROCESSOR_K1OM:
76bc74dc 1594 case PROCESSOR_GENERIC64:
ccc9c027
L
1595 case PROCESSOR_K6:
1596 case PROCESSOR_ATHLON:
1597 case PROCESSOR_K8:
4eed87de 1598 case PROCESSOR_AMDFAM10:
8aedb9fe 1599 case PROCESSOR_BD:
029f3522 1600 case PROCESSOR_ZNVER:
7b458c12 1601 case PROCESSOR_BT:
80b8656c 1602 patt = alt_patt;
ccc9c027 1603 break;
76bc74dc 1604 case PROCESSOR_I386:
ccc9c027
L
1605 case PROCESSOR_I486:
1606 case PROCESSOR_PENTIUM:
2dde1948 1607 case PROCESSOR_PENTIUMPRO:
81486035 1608 case PROCESSOR_IAMCU:
ccc9c027
L
1609 case PROCESSOR_GENERIC32:
1610 patt = f32_patt;
1611 break;
4eed87de 1612 }
ccc9c027
L
1613 }
1614 else
1615 {
fbf3f584 1616 switch (fragP->tc_frag_data.tune)
ccc9c027
L
1617 {
1618 case PROCESSOR_UNKNOWN:
e6a14101 1619 /* When cpu_arch_isa is set, cpu_arch_tune shouldn't be
ccc9c027
L
1620 PROCESSOR_UNKNOWN. */
1621 abort ();
1622 break;
1623
76bc74dc 1624 case PROCESSOR_I386:
ccc9c027
L
1625 case PROCESSOR_I486:
1626 case PROCESSOR_PENTIUM:
81486035 1627 case PROCESSOR_IAMCU:
ccc9c027
L
1628 case PROCESSOR_K6:
1629 case PROCESSOR_ATHLON:
1630 case PROCESSOR_K8:
4eed87de 1631 case PROCESSOR_AMDFAM10:
8aedb9fe 1632 case PROCESSOR_BD:
029f3522 1633 case PROCESSOR_ZNVER:
7b458c12 1634 case PROCESSOR_BT:
ccc9c027
L
1635 case PROCESSOR_GENERIC32:
1636 /* We use cpu_arch_isa_flags to check if we CAN optimize
22109423
L
1637 with nops. */
1638 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1639 patt = alt_patt;
ccc9c027
L
1640 else
1641 patt = f32_patt;
1642 break;
76bc74dc
L
1643 case PROCESSOR_PENTIUMPRO:
1644 case PROCESSOR_PENTIUM4:
1645 case PROCESSOR_NOCONA:
1646 case PROCESSOR_CORE:
ef05d495 1647 case PROCESSOR_CORE2:
bd5295b2 1648 case PROCESSOR_COREI7:
3632d14b 1649 case PROCESSOR_L1OM:
7a9068fe 1650 case PROCESSOR_K1OM:
22109423 1651 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1652 patt = alt_patt;
ccc9c027
L
1653 else
1654 patt = f32_patt;
1655 break;
1656 case PROCESSOR_GENERIC64:
80b8656c 1657 patt = alt_patt;
ccc9c027 1658 break;
4eed87de 1659 }
ccc9c027
L
1660 }
1661
76bc74dc
L
1662 if (patt == f32_patt)
1663 {
3ae729d5
L
1664 max_single_nop_size = sizeof (f32_patt) / sizeof (f32_patt[0]);
1665 /* Limit number of NOPs to 2 for older processors. */
1666 max_number_of_nops = 2;
76bc74dc
L
1667 }
1668 else
1669 {
3ae729d5
L
1670 max_single_nop_size = sizeof (alt_patt) / sizeof (alt_patt[0]);
1671 /* Limit number of NOPs to 7 for newer processors. */
1672 max_number_of_nops = 7;
1673 }
1674 }
1675
1676 if (limit == 0)
1677 limit = max_single_nop_size;
1678
1679 if (fragP->fr_type == rs_fill_nop)
1680 {
1681 /* Output NOPs for .nop directive. */
1682 if (limit > max_single_nop_size)
1683 {
1684 as_bad_where (fragP->fr_file, fragP->fr_line,
1685 _("invalid single nop size: %d "
1686 "(expect within [0, %d])"),
1687 limit, max_single_nop_size);
1688 return;
1689 }
1690 }
e379e5f3 1691 else if (fragP->fr_type != rs_machine_dependent)
3ae729d5
L
1692 fragP->fr_var = count;
1693
1694 if ((count / max_single_nop_size) > max_number_of_nops)
1695 {
1696 /* Generate jump over NOPs. */
1697 offsetT disp = count - 2;
1698 if (fits_in_imm7 (disp))
1699 {
1700 /* Use "jmp disp8" if possible. */
1701 count = disp;
1702 where[0] = jump_disp8[0];
1703 where[1] = count;
1704 where += 2;
1705 }
1706 else
1707 {
1708 unsigned int size_of_jump;
1709
1710 if (flag_code == CODE_16BIT)
1711 {
1712 where[0] = jump16_disp32[0];
1713 where[1] = jump16_disp32[1];
1714 size_of_jump = 2;
1715 }
1716 else
1717 {
1718 where[0] = jump32_disp32[0];
1719 size_of_jump = 1;
1720 }
1721
1722 count -= size_of_jump + 4;
1723 if (!fits_in_imm31 (count))
1724 {
1725 as_bad_where (fragP->fr_file, fragP->fr_line,
1726 _("jump over nop padding out of range"));
1727 return;
1728 }
1729
1730 md_number_to_chars (where + size_of_jump, count, 4);
1731 where += size_of_jump + 4;
76bc74dc 1732 }
ccc9c027 1733 }
3ae729d5
L
1734
1735 /* Generate multiple NOPs. */
1736 i386_output_nops (where, patt, count, limit);
252b5132
RH
1737}
1738
c6fb90c8 1739static INLINE int
0dfbf9d7 1740operand_type_all_zero (const union i386_operand_type *x)
40fb9820 1741{
0dfbf9d7 1742 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1743 {
1744 case 3:
0dfbf9d7 1745 if (x->array[2])
c6fb90c8 1746 return 0;
1a0670f3 1747 /* Fall through. */
c6fb90c8 1748 case 2:
0dfbf9d7 1749 if (x->array[1])
c6fb90c8 1750 return 0;
1a0670f3 1751 /* Fall through. */
c6fb90c8 1752 case 1:
0dfbf9d7 1753 return !x->array[0];
c6fb90c8
L
1754 default:
1755 abort ();
1756 }
40fb9820
L
1757}
1758
c6fb90c8 1759static INLINE void
0dfbf9d7 1760operand_type_set (union i386_operand_type *x, unsigned int v)
40fb9820 1761{
0dfbf9d7 1762 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1763 {
1764 case 3:
0dfbf9d7 1765 x->array[2] = v;
1a0670f3 1766 /* Fall through. */
c6fb90c8 1767 case 2:
0dfbf9d7 1768 x->array[1] = v;
1a0670f3 1769 /* Fall through. */
c6fb90c8 1770 case 1:
0dfbf9d7 1771 x->array[0] = v;
1a0670f3 1772 /* Fall through. */
c6fb90c8
L
1773 break;
1774 default:
1775 abort ();
1776 }
bab6aec1
JB
1777
1778 x->bitfield.class = ClassNone;
75e5731b 1779 x->bitfield.instance = InstanceNone;
c6fb90c8 1780}
40fb9820 1781
c6fb90c8 1782static INLINE int
0dfbf9d7
L
1783operand_type_equal (const union i386_operand_type *x,
1784 const union i386_operand_type *y)
c6fb90c8 1785{
0dfbf9d7 1786 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1787 {
1788 case 3:
0dfbf9d7 1789 if (x->array[2] != y->array[2])
c6fb90c8 1790 return 0;
1a0670f3 1791 /* Fall through. */
c6fb90c8 1792 case 2:
0dfbf9d7 1793 if (x->array[1] != y->array[1])
c6fb90c8 1794 return 0;
1a0670f3 1795 /* Fall through. */
c6fb90c8 1796 case 1:
0dfbf9d7 1797 return x->array[0] == y->array[0];
c6fb90c8
L
1798 break;
1799 default:
1800 abort ();
1801 }
1802}
40fb9820 1803
0dfbf9d7
L
1804static INLINE int
1805cpu_flags_all_zero (const union i386_cpu_flags *x)
1806{
1807 switch (ARRAY_SIZE(x->array))
1808 {
53467f57
IT
1809 case 4:
1810 if (x->array[3])
1811 return 0;
1812 /* Fall through. */
0dfbf9d7
L
1813 case 3:
1814 if (x->array[2])
1815 return 0;
1a0670f3 1816 /* Fall through. */
0dfbf9d7
L
1817 case 2:
1818 if (x->array[1])
1819 return 0;
1a0670f3 1820 /* Fall through. */
0dfbf9d7
L
1821 case 1:
1822 return !x->array[0];
1823 default:
1824 abort ();
1825 }
1826}
1827
0dfbf9d7
L
1828static INLINE int
1829cpu_flags_equal (const union i386_cpu_flags *x,
1830 const union i386_cpu_flags *y)
1831{
1832 switch (ARRAY_SIZE(x->array))
1833 {
53467f57
IT
1834 case 4:
1835 if (x->array[3] != y->array[3])
1836 return 0;
1837 /* Fall through. */
0dfbf9d7
L
1838 case 3:
1839 if (x->array[2] != y->array[2])
1840 return 0;
1a0670f3 1841 /* Fall through. */
0dfbf9d7
L
1842 case 2:
1843 if (x->array[1] != y->array[1])
1844 return 0;
1a0670f3 1845 /* Fall through. */
0dfbf9d7
L
1846 case 1:
1847 return x->array[0] == y->array[0];
1848 break;
1849 default:
1850 abort ();
1851 }
1852}
c6fb90c8
L
1853
1854static INLINE int
1855cpu_flags_check_cpu64 (i386_cpu_flags f)
1856{
1857 return !((flag_code == CODE_64BIT && f.bitfield.cpuno64)
1858 || (flag_code != CODE_64BIT && f.bitfield.cpu64));
40fb9820
L
1859}
1860
c6fb90c8
L
1861static INLINE i386_cpu_flags
1862cpu_flags_and (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1863{
c6fb90c8
L
1864 switch (ARRAY_SIZE (x.array))
1865 {
53467f57
IT
1866 case 4:
1867 x.array [3] &= y.array [3];
1868 /* Fall through. */
c6fb90c8
L
1869 case 3:
1870 x.array [2] &= y.array [2];
1a0670f3 1871 /* Fall through. */
c6fb90c8
L
1872 case 2:
1873 x.array [1] &= y.array [1];
1a0670f3 1874 /* Fall through. */
c6fb90c8
L
1875 case 1:
1876 x.array [0] &= y.array [0];
1877 break;
1878 default:
1879 abort ();
1880 }
1881 return x;
1882}
40fb9820 1883
c6fb90c8
L
1884static INLINE i386_cpu_flags
1885cpu_flags_or (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1886{
c6fb90c8 1887 switch (ARRAY_SIZE (x.array))
40fb9820 1888 {
53467f57
IT
1889 case 4:
1890 x.array [3] |= y.array [3];
1891 /* Fall through. */
c6fb90c8
L
1892 case 3:
1893 x.array [2] |= y.array [2];
1a0670f3 1894 /* Fall through. */
c6fb90c8
L
1895 case 2:
1896 x.array [1] |= y.array [1];
1a0670f3 1897 /* Fall through. */
c6fb90c8
L
1898 case 1:
1899 x.array [0] |= y.array [0];
40fb9820
L
1900 break;
1901 default:
1902 abort ();
1903 }
40fb9820
L
1904 return x;
1905}
1906
309d3373
JB
1907static INLINE i386_cpu_flags
1908cpu_flags_and_not (i386_cpu_flags x, i386_cpu_flags y)
1909{
1910 switch (ARRAY_SIZE (x.array))
1911 {
53467f57
IT
1912 case 4:
1913 x.array [3] &= ~y.array [3];
1914 /* Fall through. */
309d3373
JB
1915 case 3:
1916 x.array [2] &= ~y.array [2];
1a0670f3 1917 /* Fall through. */
309d3373
JB
1918 case 2:
1919 x.array [1] &= ~y.array [1];
1a0670f3 1920 /* Fall through. */
309d3373
JB
1921 case 1:
1922 x.array [0] &= ~y.array [0];
1923 break;
1924 default:
1925 abort ();
1926 }
1927 return x;
1928}
1929
6c0946d0
JB
1930static const i386_cpu_flags avx512 = CPU_ANY_AVX512F_FLAGS;
1931
c0f3af97
L
1932#define CPU_FLAGS_ARCH_MATCH 0x1
1933#define CPU_FLAGS_64BIT_MATCH 0x2
1934
c0f3af97 1935#define CPU_FLAGS_PERFECT_MATCH \
db12e14e 1936 (CPU_FLAGS_ARCH_MATCH | CPU_FLAGS_64BIT_MATCH)
c0f3af97
L
1937
1938/* Return CPU flags match bits. */
3629bb00 1939
40fb9820 1940static int
d3ce72d0 1941cpu_flags_match (const insn_template *t)
40fb9820 1942{
c0f3af97
L
1943 i386_cpu_flags x = t->cpu_flags;
1944 int match = cpu_flags_check_cpu64 (x) ? CPU_FLAGS_64BIT_MATCH : 0;
40fb9820
L
1945
1946 x.bitfield.cpu64 = 0;
1947 x.bitfield.cpuno64 = 0;
1948
0dfbf9d7 1949 if (cpu_flags_all_zero (&x))
c0f3af97
L
1950 {
1951 /* This instruction is available on all archs. */
db12e14e 1952 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 1953 }
3629bb00
L
1954 else
1955 {
c0f3af97 1956 /* This instruction is available only on some archs. */
3629bb00
L
1957 i386_cpu_flags cpu = cpu_arch_flags;
1958
ab592e75
JB
1959 /* AVX512VL is no standalone feature - match it and then strip it. */
1960 if (x.bitfield.cpuavx512vl && !cpu.bitfield.cpuavx512vl)
1961 return match;
1962 x.bitfield.cpuavx512vl = 0;
1963
3629bb00 1964 cpu = cpu_flags_and (x, cpu);
c0f3af97
L
1965 if (!cpu_flags_all_zero (&cpu))
1966 {
57392598 1967 if (x.bitfield.cpuavx)
a5ff0eb2 1968 {
929f69fa 1969 /* We need to check a few extra flags with AVX. */
b9d49817 1970 if (cpu.bitfield.cpuavx
40d231b4
JB
1971 && (!t->opcode_modifier.sse2avx
1972 || (sse2avx && !i.prefix[DATA_PREFIX]))
b9d49817 1973 && (!x.bitfield.cpuaes || cpu.bitfield.cpuaes)
929f69fa 1974 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
b9d49817
JB
1975 && (!x.bitfield.cpupclmul || cpu.bitfield.cpupclmul))
1976 match |= CPU_FLAGS_ARCH_MATCH;
a5ff0eb2 1977 }
929f69fa
JB
1978 else if (x.bitfield.cpuavx512f)
1979 {
1980 /* We need to check a few extra flags with AVX512F. */
1981 if (cpu.bitfield.cpuavx512f
1982 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
1983 && (!x.bitfield.cpuvaes || cpu.bitfield.cpuvaes)
1984 && (!x.bitfield.cpuvpclmulqdq || cpu.bitfield.cpuvpclmulqdq))
1985 match |= CPU_FLAGS_ARCH_MATCH;
1986 }
a5ff0eb2 1987 else
db12e14e 1988 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 1989 }
3629bb00 1990 }
c0f3af97 1991 return match;
40fb9820
L
1992}
1993
c6fb90c8
L
1994static INLINE i386_operand_type
1995operand_type_and (i386_operand_type x, i386_operand_type y)
40fb9820 1996{
bab6aec1
JB
1997 if (x.bitfield.class != y.bitfield.class)
1998 x.bitfield.class = ClassNone;
75e5731b
JB
1999 if (x.bitfield.instance != y.bitfield.instance)
2000 x.bitfield.instance = InstanceNone;
bab6aec1 2001
c6fb90c8
L
2002 switch (ARRAY_SIZE (x.array))
2003 {
2004 case 3:
2005 x.array [2] &= y.array [2];
1a0670f3 2006 /* Fall through. */
c6fb90c8
L
2007 case 2:
2008 x.array [1] &= y.array [1];
1a0670f3 2009 /* Fall through. */
c6fb90c8
L
2010 case 1:
2011 x.array [0] &= y.array [0];
2012 break;
2013 default:
2014 abort ();
2015 }
2016 return x;
40fb9820
L
2017}
2018
73053c1f
JB
2019static INLINE i386_operand_type
2020operand_type_and_not (i386_operand_type x, i386_operand_type y)
2021{
bab6aec1 2022 gas_assert (y.bitfield.class == ClassNone);
75e5731b 2023 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 2024
73053c1f
JB
2025 switch (ARRAY_SIZE (x.array))
2026 {
2027 case 3:
2028 x.array [2] &= ~y.array [2];
2029 /* Fall through. */
2030 case 2:
2031 x.array [1] &= ~y.array [1];
2032 /* Fall through. */
2033 case 1:
2034 x.array [0] &= ~y.array [0];
2035 break;
2036 default:
2037 abort ();
2038 }
2039 return x;
2040}
2041
c6fb90c8
L
2042static INLINE i386_operand_type
2043operand_type_or (i386_operand_type x, i386_operand_type y)
40fb9820 2044{
bab6aec1
JB
2045 gas_assert (x.bitfield.class == ClassNone ||
2046 y.bitfield.class == ClassNone ||
2047 x.bitfield.class == y.bitfield.class);
75e5731b
JB
2048 gas_assert (x.bitfield.instance == InstanceNone ||
2049 y.bitfield.instance == InstanceNone ||
2050 x.bitfield.instance == y.bitfield.instance);
bab6aec1 2051
c6fb90c8 2052 switch (ARRAY_SIZE (x.array))
40fb9820 2053 {
c6fb90c8
L
2054 case 3:
2055 x.array [2] |= y.array [2];
1a0670f3 2056 /* Fall through. */
c6fb90c8
L
2057 case 2:
2058 x.array [1] |= y.array [1];
1a0670f3 2059 /* Fall through. */
c6fb90c8
L
2060 case 1:
2061 x.array [0] |= y.array [0];
40fb9820
L
2062 break;
2063 default:
2064 abort ();
2065 }
c6fb90c8
L
2066 return x;
2067}
40fb9820 2068
c6fb90c8
L
2069static INLINE i386_operand_type
2070operand_type_xor (i386_operand_type x, i386_operand_type y)
2071{
bab6aec1 2072 gas_assert (y.bitfield.class == ClassNone);
75e5731b 2073 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 2074
c6fb90c8
L
2075 switch (ARRAY_SIZE (x.array))
2076 {
2077 case 3:
2078 x.array [2] ^= y.array [2];
1a0670f3 2079 /* Fall through. */
c6fb90c8
L
2080 case 2:
2081 x.array [1] ^= y.array [1];
1a0670f3 2082 /* Fall through. */
c6fb90c8
L
2083 case 1:
2084 x.array [0] ^= y.array [0];
2085 break;
2086 default:
2087 abort ();
2088 }
40fb9820
L
2089 return x;
2090}
2091
40fb9820
L
2092static const i386_operand_type disp16 = OPERAND_TYPE_DISP16;
2093static const i386_operand_type disp32 = OPERAND_TYPE_DISP32;
2094static const i386_operand_type disp32s = OPERAND_TYPE_DISP32S;
2095static const i386_operand_type disp16_32 = OPERAND_TYPE_DISP16_32;
bab6aec1
JB
2096static const i386_operand_type anydisp = OPERAND_TYPE_ANYDISP;
2097static const i386_operand_type anyimm = OPERAND_TYPE_ANYIMM;
40fb9820 2098static const i386_operand_type regxmm = OPERAND_TYPE_REGXMM;
43234a1e 2099static const i386_operand_type regmask = OPERAND_TYPE_REGMASK;
40fb9820
L
2100static const i386_operand_type imm8 = OPERAND_TYPE_IMM8;
2101static const i386_operand_type imm8s = OPERAND_TYPE_IMM8S;
2102static const i386_operand_type imm16 = OPERAND_TYPE_IMM16;
2103static const i386_operand_type imm32 = OPERAND_TYPE_IMM32;
2104static const i386_operand_type imm32s = OPERAND_TYPE_IMM32S;
2105static const i386_operand_type imm64 = OPERAND_TYPE_IMM64;
2106static const i386_operand_type imm16_32 = OPERAND_TYPE_IMM16_32;
2107static const i386_operand_type imm16_32s = OPERAND_TYPE_IMM16_32S;
2108static const i386_operand_type imm16_32_32s = OPERAND_TYPE_IMM16_32_32S;
2109
2110enum operand_type
2111{
2112 reg,
40fb9820
L
2113 imm,
2114 disp,
2115 anymem
2116};
2117
c6fb90c8 2118static INLINE int
40fb9820
L
2119operand_type_check (i386_operand_type t, enum operand_type c)
2120{
2121 switch (c)
2122 {
2123 case reg:
bab6aec1 2124 return t.bitfield.class == Reg;
40fb9820 2125
40fb9820
L
2126 case imm:
2127 return (t.bitfield.imm8
2128 || t.bitfield.imm8s
2129 || t.bitfield.imm16
2130 || t.bitfield.imm32
2131 || t.bitfield.imm32s
2132 || t.bitfield.imm64);
2133
2134 case disp:
2135 return (t.bitfield.disp8
2136 || t.bitfield.disp16
2137 || t.bitfield.disp32
2138 || t.bitfield.disp32s
2139 || t.bitfield.disp64);
2140
2141 case anymem:
2142 return (t.bitfield.disp8
2143 || t.bitfield.disp16
2144 || t.bitfield.disp32
2145 || t.bitfield.disp32s
2146 || t.bitfield.disp64
2147 || t.bitfield.baseindex);
2148
2149 default:
2150 abort ();
2151 }
2cfe26b6
AM
2152
2153 return 0;
40fb9820
L
2154}
2155
7a54636a
L
2156/* Return 1 if there is no conflict in 8bit/16bit/32bit/64bit/80bit size
2157 between operand GIVEN and opeand WANTED for instruction template T. */
5c07affc
L
2158
2159static INLINE int
7a54636a
L
2160match_operand_size (const insn_template *t, unsigned int wanted,
2161 unsigned int given)
5c07affc 2162{
3ac21baa
JB
2163 return !((i.types[given].bitfield.byte
2164 && !t->operand_types[wanted].bitfield.byte)
2165 || (i.types[given].bitfield.word
2166 && !t->operand_types[wanted].bitfield.word)
2167 || (i.types[given].bitfield.dword
2168 && !t->operand_types[wanted].bitfield.dword)
2169 || (i.types[given].bitfield.qword
2170 && !t->operand_types[wanted].bitfield.qword)
2171 || (i.types[given].bitfield.tbyte
2172 && !t->operand_types[wanted].bitfield.tbyte));
5c07affc
L
2173}
2174
dd40ce22
L
2175/* Return 1 if there is no conflict in SIMD register between operand
2176 GIVEN and opeand WANTED for instruction template T. */
1b54b8d7
JB
2177
2178static INLINE int
dd40ce22
L
2179match_simd_size (const insn_template *t, unsigned int wanted,
2180 unsigned int given)
1b54b8d7 2181{
3ac21baa
JB
2182 return !((i.types[given].bitfield.xmmword
2183 && !t->operand_types[wanted].bitfield.xmmword)
2184 || (i.types[given].bitfield.ymmword
2185 && !t->operand_types[wanted].bitfield.ymmword)
2186 || (i.types[given].bitfield.zmmword
260cd341
LC
2187 && !t->operand_types[wanted].bitfield.zmmword)
2188 || (i.types[given].bitfield.tmmword
2189 && !t->operand_types[wanted].bitfield.tmmword));
1b54b8d7
JB
2190}
2191
7a54636a
L
2192/* Return 1 if there is no conflict in any size between operand GIVEN
2193 and opeand WANTED for instruction template T. */
5c07affc
L
2194
2195static INLINE int
dd40ce22
L
2196match_mem_size (const insn_template *t, unsigned int wanted,
2197 unsigned int given)
5c07affc 2198{
7a54636a 2199 return (match_operand_size (t, wanted, given)
3ac21baa 2200 && !((i.types[given].bitfield.unspecified
5273a3cd 2201 && !i.broadcast.type
3ac21baa
JB
2202 && !t->operand_types[wanted].bitfield.unspecified)
2203 || (i.types[given].bitfield.fword
2204 && !t->operand_types[wanted].bitfield.fword)
1b54b8d7
JB
2205 /* For scalar opcode templates to allow register and memory
2206 operands at the same time, some special casing is needed
d6793fa1
JB
2207 here. Also for v{,p}broadcast*, {,v}pmov{s,z}*, and
2208 down-conversion vpmov*. */
3528c362 2209 || ((t->operand_types[wanted].bitfield.class == RegSIMD
bc49bfd8
JB
2210 && t->operand_types[wanted].bitfield.byte
2211 + t->operand_types[wanted].bitfield.word
2212 + t->operand_types[wanted].bitfield.dword
2213 + t->operand_types[wanted].bitfield.qword
2214 > !!t->opcode_modifier.broadcast)
3ac21baa
JB
2215 ? (i.types[given].bitfield.xmmword
2216 || i.types[given].bitfield.ymmword
2217 || i.types[given].bitfield.zmmword)
2218 : !match_simd_size(t, wanted, given))));
5c07affc
L
2219}
2220
3ac21baa
JB
2221/* Return value has MATCH_STRAIGHT set if there is no size conflict on any
2222 operands for instruction template T, and it has MATCH_REVERSE set if there
2223 is no size conflict on any operands for the template with operands reversed
2224 (and the template allows for reversing in the first place). */
5c07affc 2225
3ac21baa
JB
2226#define MATCH_STRAIGHT 1
2227#define MATCH_REVERSE 2
2228
2229static INLINE unsigned int
d3ce72d0 2230operand_size_match (const insn_template *t)
5c07affc 2231{
3ac21baa 2232 unsigned int j, match = MATCH_STRAIGHT;
5c07affc 2233
0cfa3eb3 2234 /* Don't check non-absolute jump instructions. */
5c07affc 2235 if (t->opcode_modifier.jump
0cfa3eb3 2236 && t->opcode_modifier.jump != JUMP_ABSOLUTE)
5c07affc
L
2237 return match;
2238
2239 /* Check memory and accumulator operand size. */
2240 for (j = 0; j < i.operands; j++)
2241 {
3528c362
JB
2242 if (i.types[j].bitfield.class != Reg
2243 && i.types[j].bitfield.class != RegSIMD
601e8564 2244 && t->opcode_modifier.anysize)
5c07affc
L
2245 continue;
2246
bab6aec1 2247 if (t->operand_types[j].bitfield.class == Reg
7a54636a 2248 && !match_operand_size (t, j, j))
5c07affc
L
2249 {
2250 match = 0;
2251 break;
2252 }
2253
3528c362 2254 if (t->operand_types[j].bitfield.class == RegSIMD
3ac21baa 2255 && !match_simd_size (t, j, j))
1b54b8d7
JB
2256 {
2257 match = 0;
2258 break;
2259 }
2260
75e5731b 2261 if (t->operand_types[j].bitfield.instance == Accum
7a54636a 2262 && (!match_operand_size (t, j, j) || !match_simd_size (t, j, j)))
1b54b8d7
JB
2263 {
2264 match = 0;
2265 break;
2266 }
2267
c48dadc9 2268 if ((i.flags[j] & Operand_Mem) && !match_mem_size (t, j, j))
5c07affc
L
2269 {
2270 match = 0;
2271 break;
2272 }
2273 }
2274
3ac21baa 2275 if (!t->opcode_modifier.d)
891edac4 2276 {
dc1e8a47 2277 mismatch:
3ac21baa
JB
2278 if (!match)
2279 i.error = operand_size_mismatch;
2280 return match;
891edac4 2281 }
5c07affc
L
2282
2283 /* Check reverse. */
f5eb1d70 2284 gas_assert (i.operands >= 2 && i.operands <= 3);
5c07affc 2285
f5eb1d70 2286 for (j = 0; j < i.operands; j++)
5c07affc 2287 {
f5eb1d70
JB
2288 unsigned int given = i.operands - j - 1;
2289
bab6aec1 2290 if (t->operand_types[j].bitfield.class == Reg
f5eb1d70 2291 && !match_operand_size (t, j, given))
891edac4 2292 goto mismatch;
5c07affc 2293
3528c362 2294 if (t->operand_types[j].bitfield.class == RegSIMD
f5eb1d70 2295 && !match_simd_size (t, j, given))
dbbc8b7e
JB
2296 goto mismatch;
2297
75e5731b 2298 if (t->operand_types[j].bitfield.instance == Accum
f5eb1d70
JB
2299 && (!match_operand_size (t, j, given)
2300 || !match_simd_size (t, j, given)))
dbbc8b7e
JB
2301 goto mismatch;
2302
f5eb1d70 2303 if ((i.flags[given] & Operand_Mem) && !match_mem_size (t, j, given))
891edac4 2304 goto mismatch;
5c07affc
L
2305 }
2306
3ac21baa 2307 return match | MATCH_REVERSE;
5c07affc
L
2308}
2309
c6fb90c8 2310static INLINE int
40fb9820
L
2311operand_type_match (i386_operand_type overlap,
2312 i386_operand_type given)
2313{
2314 i386_operand_type temp = overlap;
2315
7d5e4556 2316 temp.bitfield.unspecified = 0;
5c07affc
L
2317 temp.bitfield.byte = 0;
2318 temp.bitfield.word = 0;
2319 temp.bitfield.dword = 0;
2320 temp.bitfield.fword = 0;
2321 temp.bitfield.qword = 0;
2322 temp.bitfield.tbyte = 0;
2323 temp.bitfield.xmmword = 0;
c0f3af97 2324 temp.bitfield.ymmword = 0;
43234a1e 2325 temp.bitfield.zmmword = 0;
260cd341 2326 temp.bitfield.tmmword = 0;
0dfbf9d7 2327 if (operand_type_all_zero (&temp))
891edac4 2328 goto mismatch;
40fb9820 2329
6f2f06be 2330 if (given.bitfield.baseindex == overlap.bitfield.baseindex)
891edac4
L
2331 return 1;
2332
dc1e8a47 2333 mismatch:
a65babc9 2334 i.error = operand_type_mismatch;
891edac4 2335 return 0;
40fb9820
L
2336}
2337
7d5e4556 2338/* If given types g0 and g1 are registers they must be of the same type
10c17abd 2339 unless the expected operand type register overlap is null.
5de4d9ef 2340 Some Intel syntax memory operand size checking also happens here. */
40fb9820 2341
c6fb90c8 2342static INLINE int
dc821c5f 2343operand_type_register_match (i386_operand_type g0,
40fb9820 2344 i386_operand_type t0,
40fb9820
L
2345 i386_operand_type g1,
2346 i386_operand_type t1)
2347{
bab6aec1 2348 if (g0.bitfield.class != Reg
3528c362 2349 && g0.bitfield.class != RegSIMD
10c17abd
JB
2350 && (!operand_type_check (g0, anymem)
2351 || g0.bitfield.unspecified
5de4d9ef
JB
2352 || (t0.bitfield.class != Reg
2353 && t0.bitfield.class != RegSIMD)))
40fb9820
L
2354 return 1;
2355
bab6aec1 2356 if (g1.bitfield.class != Reg
3528c362 2357 && g1.bitfield.class != RegSIMD
10c17abd
JB
2358 && (!operand_type_check (g1, anymem)
2359 || g1.bitfield.unspecified
5de4d9ef
JB
2360 || (t1.bitfield.class != Reg
2361 && t1.bitfield.class != RegSIMD)))
40fb9820
L
2362 return 1;
2363
dc821c5f
JB
2364 if (g0.bitfield.byte == g1.bitfield.byte
2365 && g0.bitfield.word == g1.bitfield.word
2366 && g0.bitfield.dword == g1.bitfield.dword
10c17abd
JB
2367 && g0.bitfield.qword == g1.bitfield.qword
2368 && g0.bitfield.xmmword == g1.bitfield.xmmword
2369 && g0.bitfield.ymmword == g1.bitfield.ymmword
2370 && g0.bitfield.zmmword == g1.bitfield.zmmword)
40fb9820
L
2371 return 1;
2372
dc821c5f
JB
2373 if (!(t0.bitfield.byte & t1.bitfield.byte)
2374 && !(t0.bitfield.word & t1.bitfield.word)
2375 && !(t0.bitfield.dword & t1.bitfield.dword)
10c17abd
JB
2376 && !(t0.bitfield.qword & t1.bitfield.qword)
2377 && !(t0.bitfield.xmmword & t1.bitfield.xmmword)
2378 && !(t0.bitfield.ymmword & t1.bitfield.ymmword)
2379 && !(t0.bitfield.zmmword & t1.bitfield.zmmword))
891edac4
L
2380 return 1;
2381
a65babc9 2382 i.error = register_type_mismatch;
891edac4
L
2383
2384 return 0;
40fb9820
L
2385}
2386
4c692bc7
JB
2387static INLINE unsigned int
2388register_number (const reg_entry *r)
2389{
2390 unsigned int nr = r->reg_num;
2391
2392 if (r->reg_flags & RegRex)
2393 nr += 8;
2394
200cbe0f
L
2395 if (r->reg_flags & RegVRex)
2396 nr += 16;
2397
4c692bc7
JB
2398 return nr;
2399}
2400
252b5132 2401static INLINE unsigned int
40fb9820 2402mode_from_disp_size (i386_operand_type t)
252b5132 2403{
b5014f7a 2404 if (t.bitfield.disp8)
40fb9820
L
2405 return 1;
2406 else if (t.bitfield.disp16
2407 || t.bitfield.disp32
2408 || t.bitfield.disp32s)
2409 return 2;
2410 else
2411 return 0;
252b5132
RH
2412}
2413
2414static INLINE int
65879393 2415fits_in_signed_byte (addressT num)
252b5132 2416{
65879393 2417 return num + 0x80 <= 0xff;
47926f60 2418}
252b5132
RH
2419
2420static INLINE int
65879393 2421fits_in_unsigned_byte (addressT num)
252b5132 2422{
65879393 2423 return num <= 0xff;
47926f60 2424}
252b5132
RH
2425
2426static INLINE int
65879393 2427fits_in_unsigned_word (addressT num)
252b5132 2428{
65879393 2429 return num <= 0xffff;
47926f60 2430}
252b5132
RH
2431
2432static INLINE int
65879393 2433fits_in_signed_word (addressT num)
252b5132 2434{
65879393 2435 return num + 0x8000 <= 0xffff;
47926f60 2436}
2a962e6d 2437
3e73aa7c 2438static INLINE int
65879393 2439fits_in_signed_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2440{
2441#ifndef BFD64
2442 return 1;
2443#else
65879393 2444 return num + 0x80000000 <= 0xffffffff;
3e73aa7c
JH
2445#endif
2446} /* fits_in_signed_long() */
2a962e6d 2447
3e73aa7c 2448static INLINE int
65879393 2449fits_in_unsigned_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2450{
2451#ifndef BFD64
2452 return 1;
2453#else
65879393 2454 return num <= 0xffffffff;
3e73aa7c
JH
2455#endif
2456} /* fits_in_unsigned_long() */
252b5132 2457
a442cac5
JB
2458static INLINE valueT extend_to_32bit_address (addressT num)
2459{
2460#ifdef BFD64
2461 if (fits_in_unsigned_long(num))
2462 return (num ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
2463
2464 if (!fits_in_signed_long (num))
2465 return num & 0xffffffff;
2466#endif
2467
2468 return num;
2469}
2470
43234a1e 2471static INLINE int
b5014f7a 2472fits_in_disp8 (offsetT num)
43234a1e
L
2473{
2474 int shift = i.memshift;
2475 unsigned int mask;
2476
2477 if (shift == -1)
2478 abort ();
2479
2480 mask = (1 << shift) - 1;
2481
2482 /* Return 0 if NUM isn't properly aligned. */
2483 if ((num & mask))
2484 return 0;
2485
2486 /* Check if NUM will fit in 8bit after shift. */
2487 return fits_in_signed_byte (num >> shift);
2488}
2489
a683cc34
SP
2490static INLINE int
2491fits_in_imm4 (offsetT num)
2492{
2493 return (num & 0xf) == num;
2494}
2495
40fb9820 2496static i386_operand_type
e3bb37b5 2497smallest_imm_type (offsetT num)
252b5132 2498{
40fb9820 2499 i386_operand_type t;
7ab9ffdd 2500
0dfbf9d7 2501 operand_type_set (&t, 0);
40fb9820
L
2502 t.bitfield.imm64 = 1;
2503
2504 if (cpu_arch_tune != PROCESSOR_I486 && num == 1)
e413e4e9
AM
2505 {
2506 /* This code is disabled on the 486 because all the Imm1 forms
2507 in the opcode table are slower on the i486. They're the
2508 versions with the implicitly specified single-position
2509 displacement, which has another syntax if you really want to
2510 use that form. */
40fb9820
L
2511 t.bitfield.imm1 = 1;
2512 t.bitfield.imm8 = 1;
2513 t.bitfield.imm8s = 1;
2514 t.bitfield.imm16 = 1;
2515 t.bitfield.imm32 = 1;
2516 t.bitfield.imm32s = 1;
2517 }
2518 else if (fits_in_signed_byte (num))
2519 {
2520 t.bitfield.imm8 = 1;
2521 t.bitfield.imm8s = 1;
2522 t.bitfield.imm16 = 1;
2523 t.bitfield.imm32 = 1;
2524 t.bitfield.imm32s = 1;
2525 }
2526 else if (fits_in_unsigned_byte (num))
2527 {
2528 t.bitfield.imm8 = 1;
2529 t.bitfield.imm16 = 1;
2530 t.bitfield.imm32 = 1;
2531 t.bitfield.imm32s = 1;
2532 }
2533 else if (fits_in_signed_word (num) || fits_in_unsigned_word (num))
2534 {
2535 t.bitfield.imm16 = 1;
2536 t.bitfield.imm32 = 1;
2537 t.bitfield.imm32s = 1;
2538 }
2539 else if (fits_in_signed_long (num))
2540 {
2541 t.bitfield.imm32 = 1;
2542 t.bitfield.imm32s = 1;
2543 }
2544 else if (fits_in_unsigned_long (num))
2545 t.bitfield.imm32 = 1;
2546
2547 return t;
47926f60 2548}
252b5132 2549
847f7ad4 2550static offsetT
e3bb37b5 2551offset_in_range (offsetT val, int size)
847f7ad4 2552{
508866be 2553 addressT mask;
ba2adb93 2554
847f7ad4
AM
2555 switch (size)
2556 {
508866be
L
2557 case 1: mask = ((addressT) 1 << 8) - 1; break;
2558 case 2: mask = ((addressT) 1 << 16) - 1; break;
3b0ec529 2559 case 4: mask = ((addressT) 2 << 31) - 1; break;
3e73aa7c
JH
2560#ifdef BFD64
2561 case 8: mask = ((addressT) 2 << 63) - 1; break;
2562#endif
47926f60 2563 default: abort ();
847f7ad4
AM
2564 }
2565
47926f60 2566 if ((val & ~mask) != 0 && (val & ~mask) != ~mask)
847f7ad4
AM
2567 {
2568 char buf1[40], buf2[40];
2569
9a8041fd
JB
2570 bfd_sprintf_vma (stdoutput, buf1, val);
2571 bfd_sprintf_vma (stdoutput, buf2, val & mask);
847f7ad4
AM
2572 as_warn (_("%s shortened to %s"), buf1, buf2);
2573 }
2574 return val & mask;
2575}
2576
c32fa91d
L
2577enum PREFIX_GROUP
2578{
2579 PREFIX_EXIST = 0,
2580 PREFIX_LOCK,
2581 PREFIX_REP,
04ef582a 2582 PREFIX_DS,
c32fa91d
L
2583 PREFIX_OTHER
2584};
2585
2586/* Returns
2587 a. PREFIX_EXIST if attempting to add a prefix where one from the
2588 same class already exists.
2589 b. PREFIX_LOCK if lock prefix is added.
2590 c. PREFIX_REP if rep/repne prefix is added.
04ef582a
L
2591 d. PREFIX_DS if ds prefix is added.
2592 e. PREFIX_OTHER if other prefix is added.
c32fa91d
L
2593 */
2594
2595static enum PREFIX_GROUP
e3bb37b5 2596add_prefix (unsigned int prefix)
252b5132 2597{
c32fa91d 2598 enum PREFIX_GROUP ret = PREFIX_OTHER;
b1905489 2599 unsigned int q;
252b5132 2600
29b0f896
AM
2601 if (prefix >= REX_OPCODE && prefix < REX_OPCODE + 16
2602 && flag_code == CODE_64BIT)
b1905489 2603 {
161a04f6 2604 if ((i.prefix[REX_PREFIX] & prefix & REX_W)
44846f29
JB
2605 || (i.prefix[REX_PREFIX] & prefix & REX_R)
2606 || (i.prefix[REX_PREFIX] & prefix & REX_X)
2607 || (i.prefix[REX_PREFIX] & prefix & REX_B))
c32fa91d 2608 ret = PREFIX_EXIST;
b1905489
JB
2609 q = REX_PREFIX;
2610 }
3e73aa7c 2611 else
b1905489
JB
2612 {
2613 switch (prefix)
2614 {
2615 default:
2616 abort ();
2617
b1905489 2618 case DS_PREFIX_OPCODE:
04ef582a
L
2619 ret = PREFIX_DS;
2620 /* Fall through. */
2621 case CS_PREFIX_OPCODE:
b1905489
JB
2622 case ES_PREFIX_OPCODE:
2623 case FS_PREFIX_OPCODE:
2624 case GS_PREFIX_OPCODE:
2625 case SS_PREFIX_OPCODE:
2626 q = SEG_PREFIX;
2627 break;
2628
2629 case REPNE_PREFIX_OPCODE:
2630 case REPE_PREFIX_OPCODE:
c32fa91d
L
2631 q = REP_PREFIX;
2632 ret = PREFIX_REP;
2633 break;
2634
b1905489 2635 case LOCK_PREFIX_OPCODE:
c32fa91d
L
2636 q = LOCK_PREFIX;
2637 ret = PREFIX_LOCK;
b1905489
JB
2638 break;
2639
2640 case FWAIT_OPCODE:
2641 q = WAIT_PREFIX;
2642 break;
2643
2644 case ADDR_PREFIX_OPCODE:
2645 q = ADDR_PREFIX;
2646 break;
2647
2648 case DATA_PREFIX_OPCODE:
2649 q = DATA_PREFIX;
2650 break;
2651 }
2652 if (i.prefix[q] != 0)
c32fa91d 2653 ret = PREFIX_EXIST;
b1905489 2654 }
252b5132 2655
b1905489 2656 if (ret)
252b5132 2657 {
b1905489
JB
2658 if (!i.prefix[q])
2659 ++i.prefixes;
2660 i.prefix[q] |= prefix;
252b5132 2661 }
b1905489
JB
2662 else
2663 as_bad (_("same type of prefix used twice"));
252b5132 2664
252b5132
RH
2665 return ret;
2666}
2667
2668static void
78f12dd3 2669update_code_flag (int value, int check)
eecb386c 2670{
78f12dd3
L
2671 PRINTF_LIKE ((*as_error));
2672
1e9cc1c2 2673 flag_code = (enum flag_code) value;
40fb9820
L
2674 if (flag_code == CODE_64BIT)
2675 {
2676 cpu_arch_flags.bitfield.cpu64 = 1;
2677 cpu_arch_flags.bitfield.cpuno64 = 0;
40fb9820
L
2678 }
2679 else
2680 {
2681 cpu_arch_flags.bitfield.cpu64 = 0;
2682 cpu_arch_flags.bitfield.cpuno64 = 1;
40fb9820
L
2683 }
2684 if (value == CODE_64BIT && !cpu_arch_flags.bitfield.cpulm )
3e73aa7c 2685 {
78f12dd3
L
2686 if (check)
2687 as_error = as_fatal;
2688 else
2689 as_error = as_bad;
2690 (*as_error) (_("64bit mode not supported on `%s'."),
2691 cpu_arch_name ? cpu_arch_name : default_arch);
3e73aa7c 2692 }
40fb9820 2693 if (value == CODE_32BIT && !cpu_arch_flags.bitfield.cpui386)
3e73aa7c 2694 {
78f12dd3
L
2695 if (check)
2696 as_error = as_fatal;
2697 else
2698 as_error = as_bad;
2699 (*as_error) (_("32bit mode not supported on `%s'."),
2700 cpu_arch_name ? cpu_arch_name : default_arch);
3e73aa7c 2701 }
eecb386c
AM
2702 stackop_size = '\0';
2703}
2704
78f12dd3
L
2705static void
2706set_code_flag (int value)
2707{
2708 update_code_flag (value, 0);
2709}
2710
eecb386c 2711static void
e3bb37b5 2712set_16bit_gcc_code_flag (int new_code_flag)
252b5132 2713{
1e9cc1c2 2714 flag_code = (enum flag_code) new_code_flag;
40fb9820
L
2715 if (flag_code != CODE_16BIT)
2716 abort ();
2717 cpu_arch_flags.bitfield.cpu64 = 0;
2718 cpu_arch_flags.bitfield.cpuno64 = 1;
9306ca4a 2719 stackop_size = LONG_MNEM_SUFFIX;
252b5132
RH
2720}
2721
2722static void
e3bb37b5 2723set_intel_syntax (int syntax_flag)
252b5132
RH
2724{
2725 /* Find out if register prefixing is specified. */
2726 int ask_naked_reg = 0;
2727
2728 SKIP_WHITESPACE ();
29b0f896 2729 if (!is_end_of_line[(unsigned char) *input_line_pointer])
252b5132 2730 {
d02603dc
NC
2731 char *string;
2732 int e = get_symbol_name (&string);
252b5132 2733
47926f60 2734 if (strcmp (string, "prefix") == 0)
252b5132 2735 ask_naked_reg = 1;
47926f60 2736 else if (strcmp (string, "noprefix") == 0)
252b5132
RH
2737 ask_naked_reg = -1;
2738 else
d0b47220 2739 as_bad (_("bad argument to syntax directive."));
d02603dc 2740 (void) restore_line_pointer (e);
252b5132
RH
2741 }
2742 demand_empty_rest_of_line ();
c3332e24 2743
252b5132
RH
2744 intel_syntax = syntax_flag;
2745
2746 if (ask_naked_reg == 0)
f86103b7
AM
2747 allow_naked_reg = (intel_syntax
2748 && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
252b5132
RH
2749 else
2750 allow_naked_reg = (ask_naked_reg < 0);
9306ca4a 2751
ee86248c 2752 expr_set_rank (O_full_ptr, syntax_flag ? 10 : 0);
7ab9ffdd 2753
e4a3b5a4 2754 identifier_chars['%'] = intel_syntax && allow_naked_reg ? '%' : 0;
9306ca4a 2755 identifier_chars['$'] = intel_syntax ? '$' : 0;
e4a3b5a4 2756 register_prefix = allow_naked_reg ? "" : "%";
252b5132
RH
2757}
2758
1efbbeb4
L
2759static void
2760set_intel_mnemonic (int mnemonic_flag)
2761{
e1d4d893 2762 intel_mnemonic = mnemonic_flag;
1efbbeb4
L
2763}
2764
db51cc60
L
2765static void
2766set_allow_index_reg (int flag)
2767{
2768 allow_index_reg = flag;
2769}
2770
cb19c032 2771static void
7bab8ab5 2772set_check (int what)
cb19c032 2773{
7bab8ab5
JB
2774 enum check_kind *kind;
2775 const char *str;
2776
2777 if (what)
2778 {
2779 kind = &operand_check;
2780 str = "operand";
2781 }
2782 else
2783 {
2784 kind = &sse_check;
2785 str = "sse";
2786 }
2787
cb19c032
L
2788 SKIP_WHITESPACE ();
2789
2790 if (!is_end_of_line[(unsigned char) *input_line_pointer])
2791 {
d02603dc
NC
2792 char *string;
2793 int e = get_symbol_name (&string);
cb19c032
L
2794
2795 if (strcmp (string, "none") == 0)
7bab8ab5 2796 *kind = check_none;
cb19c032 2797 else if (strcmp (string, "warning") == 0)
7bab8ab5 2798 *kind = check_warning;
cb19c032 2799 else if (strcmp (string, "error") == 0)
7bab8ab5 2800 *kind = check_error;
cb19c032 2801 else
7bab8ab5 2802 as_bad (_("bad argument to %s_check directive."), str);
d02603dc 2803 (void) restore_line_pointer (e);
cb19c032
L
2804 }
2805 else
7bab8ab5 2806 as_bad (_("missing argument for %s_check directive"), str);
cb19c032
L
2807
2808 demand_empty_rest_of_line ();
2809}
2810
8a9036a4
L
2811static void
2812check_cpu_arch_compatible (const char *name ATTRIBUTE_UNUSED,
1e9cc1c2 2813 i386_cpu_flags new_flag ATTRIBUTE_UNUSED)
8a9036a4
L
2814{
2815#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2816 static const char *arch;
2817
2818 /* Intel LIOM is only supported on ELF. */
2819 if (!IS_ELF)
2820 return;
2821
2822 if (!arch)
2823 {
2824 /* Use cpu_arch_name if it is set in md_parse_option. Otherwise
2825 use default_arch. */
2826 arch = cpu_arch_name;
2827 if (!arch)
2828 arch = default_arch;
2829 }
2830
81486035
L
2831 /* If we are targeting Intel MCU, we must enable it. */
2832 if (get_elf_backend_data (stdoutput)->elf_machine_code != EM_IAMCU
2833 || new_flag.bitfield.cpuiamcu)
2834 return;
2835
3632d14b 2836 /* If we are targeting Intel L1OM, we must enable it. */
8a9036a4 2837 if (get_elf_backend_data (stdoutput)->elf_machine_code != EM_L1OM
1e9cc1c2 2838 || new_flag.bitfield.cpul1om)
8a9036a4 2839 return;
76ba9986 2840
7a9068fe
L
2841 /* If we are targeting Intel K1OM, we must enable it. */
2842 if (get_elf_backend_data (stdoutput)->elf_machine_code != EM_K1OM
2843 || new_flag.bitfield.cpuk1om)
2844 return;
2845
8a9036a4
L
2846 as_bad (_("`%s' is not supported on `%s'"), name, arch);
2847#endif
2848}
2849
e413e4e9 2850static void
e3bb37b5 2851set_cpu_arch (int dummy ATTRIBUTE_UNUSED)
e413e4e9 2852{
47926f60 2853 SKIP_WHITESPACE ();
e413e4e9 2854
29b0f896 2855 if (!is_end_of_line[(unsigned char) *input_line_pointer])
e413e4e9 2856 {
d02603dc
NC
2857 char *string;
2858 int e = get_symbol_name (&string);
91d6fa6a 2859 unsigned int j;
40fb9820 2860 i386_cpu_flags flags;
e413e4e9 2861
91d6fa6a 2862 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
e413e4e9 2863 {
91d6fa6a 2864 if (strcmp (string, cpu_arch[j].name) == 0)
e413e4e9 2865 {
91d6fa6a 2866 check_cpu_arch_compatible (string, cpu_arch[j].flags);
8a9036a4 2867
5c6af06e
JB
2868 if (*string != '.')
2869 {
91d6fa6a 2870 cpu_arch_name = cpu_arch[j].name;
5c6af06e 2871 cpu_sub_arch_name = NULL;
91d6fa6a 2872 cpu_arch_flags = cpu_arch[j].flags;
40fb9820
L
2873 if (flag_code == CODE_64BIT)
2874 {
2875 cpu_arch_flags.bitfield.cpu64 = 1;
2876 cpu_arch_flags.bitfield.cpuno64 = 0;
2877 }
2878 else
2879 {
2880 cpu_arch_flags.bitfield.cpu64 = 0;
2881 cpu_arch_flags.bitfield.cpuno64 = 1;
2882 }
91d6fa6a
NC
2883 cpu_arch_isa = cpu_arch[j].type;
2884 cpu_arch_isa_flags = cpu_arch[j].flags;
ccc9c027
L
2885 if (!cpu_arch_tune_set)
2886 {
2887 cpu_arch_tune = cpu_arch_isa;
2888 cpu_arch_tune_flags = cpu_arch_isa_flags;
2889 }
5c6af06e
JB
2890 break;
2891 }
40fb9820 2892
293f5f65
L
2893 flags = cpu_flags_or (cpu_arch_flags,
2894 cpu_arch[j].flags);
81486035 2895
5b64d091 2896 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
5c6af06e 2897 {
6305a203
L
2898 if (cpu_sub_arch_name)
2899 {
2900 char *name = cpu_sub_arch_name;
2901 cpu_sub_arch_name = concat (name,
91d6fa6a 2902 cpu_arch[j].name,
1bf57e9f 2903 (const char *) NULL);
6305a203
L
2904 free (name);
2905 }
2906 else
91d6fa6a 2907 cpu_sub_arch_name = xstrdup (cpu_arch[j].name);
40fb9820 2908 cpu_arch_flags = flags;
a586129e 2909 cpu_arch_isa_flags = flags;
5c6af06e 2910 }
0089dace
L
2911 else
2912 cpu_arch_isa_flags
2913 = cpu_flags_or (cpu_arch_isa_flags,
2914 cpu_arch[j].flags);
d02603dc 2915 (void) restore_line_pointer (e);
5c6af06e
JB
2916 demand_empty_rest_of_line ();
2917 return;
e413e4e9
AM
2918 }
2919 }
293f5f65
L
2920
2921 if (*string == '.' && j >= ARRAY_SIZE (cpu_arch))
2922 {
33eaf5de 2923 /* Disable an ISA extension. */
293f5f65
L
2924 for (j = 0; j < ARRAY_SIZE (cpu_noarch); j++)
2925 if (strcmp (string + 1, cpu_noarch [j].name) == 0)
2926 {
2927 flags = cpu_flags_and_not (cpu_arch_flags,
2928 cpu_noarch[j].flags);
2929 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
2930 {
2931 if (cpu_sub_arch_name)
2932 {
2933 char *name = cpu_sub_arch_name;
2934 cpu_sub_arch_name = concat (name, string,
2935 (const char *) NULL);
2936 free (name);
2937 }
2938 else
2939 cpu_sub_arch_name = xstrdup (string);
2940 cpu_arch_flags = flags;
2941 cpu_arch_isa_flags = flags;
2942 }
2943 (void) restore_line_pointer (e);
2944 demand_empty_rest_of_line ();
2945 return;
2946 }
2947
2948 j = ARRAY_SIZE (cpu_arch);
2949 }
2950
91d6fa6a 2951 if (j >= ARRAY_SIZE (cpu_arch))
e413e4e9
AM
2952 as_bad (_("no such architecture: `%s'"), string);
2953
2954 *input_line_pointer = e;
2955 }
2956 else
2957 as_bad (_("missing cpu architecture"));
2958
fddf5b5b
AM
2959 no_cond_jump_promotion = 0;
2960 if (*input_line_pointer == ','
29b0f896 2961 && !is_end_of_line[(unsigned char) input_line_pointer[1]])
fddf5b5b 2962 {
d02603dc
NC
2963 char *string;
2964 char e;
2965
2966 ++input_line_pointer;
2967 e = get_symbol_name (&string);
fddf5b5b
AM
2968
2969 if (strcmp (string, "nojumps") == 0)
2970 no_cond_jump_promotion = 1;
2971 else if (strcmp (string, "jumps") == 0)
2972 ;
2973 else
2974 as_bad (_("no such architecture modifier: `%s'"), string);
2975
d02603dc 2976 (void) restore_line_pointer (e);
fddf5b5b
AM
2977 }
2978
e413e4e9
AM
2979 demand_empty_rest_of_line ();
2980}
2981
8a9036a4
L
2982enum bfd_architecture
2983i386_arch (void)
2984{
3632d14b 2985 if (cpu_arch_isa == PROCESSOR_L1OM)
8a9036a4
L
2986 {
2987 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2988 || flag_code != CODE_64BIT)
2989 as_fatal (_("Intel L1OM is 64bit ELF only"));
2990 return bfd_arch_l1om;
2991 }
7a9068fe
L
2992 else if (cpu_arch_isa == PROCESSOR_K1OM)
2993 {
2994 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2995 || flag_code != CODE_64BIT)
2996 as_fatal (_("Intel K1OM is 64bit ELF only"));
2997 return bfd_arch_k1om;
2998 }
81486035
L
2999 else if (cpu_arch_isa == PROCESSOR_IAMCU)
3000 {
3001 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
3002 || flag_code == CODE_64BIT)
3003 as_fatal (_("Intel MCU is 32bit ELF only"));
3004 return bfd_arch_iamcu;
3005 }
8a9036a4
L
3006 else
3007 return bfd_arch_i386;
3008}
3009
b9d79e03 3010unsigned long
7016a5d5 3011i386_mach (void)
b9d79e03 3012{
d34049e8 3013 if (startswith (default_arch, "x86_64"))
8a9036a4 3014 {
3632d14b 3015 if (cpu_arch_isa == PROCESSOR_L1OM)
8a9036a4 3016 {
351f65ca
L
3017 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
3018 || default_arch[6] != '\0')
8a9036a4
L
3019 as_fatal (_("Intel L1OM is 64bit ELF only"));
3020 return bfd_mach_l1om;
3021 }
7a9068fe
L
3022 else if (cpu_arch_isa == PROCESSOR_K1OM)
3023 {
3024 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
3025 || default_arch[6] != '\0')
3026 as_fatal (_("Intel K1OM is 64bit ELF only"));
3027 return bfd_mach_k1om;
3028 }
351f65ca 3029 else if (default_arch[6] == '\0')
8a9036a4 3030 return bfd_mach_x86_64;
351f65ca
L
3031 else
3032 return bfd_mach_x64_32;
8a9036a4 3033 }
5197d474
L
3034 else if (!strcmp (default_arch, "i386")
3035 || !strcmp (default_arch, "iamcu"))
81486035
L
3036 {
3037 if (cpu_arch_isa == PROCESSOR_IAMCU)
3038 {
3039 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
3040 as_fatal (_("Intel MCU is 32bit ELF only"));
3041 return bfd_mach_i386_iamcu;
3042 }
3043 else
3044 return bfd_mach_i386_i386;
3045 }
b9d79e03 3046 else
2b5d6a91 3047 as_fatal (_("unknown architecture"));
b9d79e03 3048}
b9d79e03 3049\f
252b5132 3050void
7016a5d5 3051md_begin (void)
252b5132 3052{
86fa6981
L
3053 /* Support pseudo prefixes like {disp32}. */
3054 lex_type ['{'] = LEX_BEGIN_NAME;
3055
47926f60 3056 /* Initialize op_hash hash table. */
629310ab 3057 op_hash = str_htab_create ();
252b5132
RH
3058
3059 {
d3ce72d0 3060 const insn_template *optab;
29b0f896 3061 templates *core_optab;
252b5132 3062
47926f60
KH
3063 /* Setup for loop. */
3064 optab = i386_optab;
add39d23 3065 core_optab = XNEW (templates);
252b5132
RH
3066 core_optab->start = optab;
3067
3068 while (1)
3069 {
3070 ++optab;
3071 if (optab->name == NULL
3072 || strcmp (optab->name, (optab - 1)->name) != 0)
3073 {
3074 /* different name --> ship out current template list;
47926f60 3075 add to hash table; & begin anew. */
252b5132 3076 core_optab->end = optab;
fe0e921f
AM
3077 if (str_hash_insert (op_hash, (optab - 1)->name, core_optab, 0))
3078 as_fatal (_("duplicate %s"), (optab - 1)->name);
3079
252b5132
RH
3080 if (optab->name == NULL)
3081 break;
add39d23 3082 core_optab = XNEW (templates);
252b5132
RH
3083 core_optab->start = optab;
3084 }
3085 }
3086 }
3087
47926f60 3088 /* Initialize reg_hash hash table. */
629310ab 3089 reg_hash = str_htab_create ();
252b5132 3090 {
29b0f896 3091 const reg_entry *regtab;
c3fe08fa 3092 unsigned int regtab_size = i386_regtab_size;
252b5132 3093
c3fe08fa 3094 for (regtab = i386_regtab; regtab_size--; regtab++)
6225c532 3095 {
6288d05f
JB
3096 switch (regtab->reg_type.bitfield.class)
3097 {
3098 case Reg:
34684862
JB
3099 if (regtab->reg_type.bitfield.dword)
3100 {
3101 if (regtab->reg_type.bitfield.instance == Accum)
3102 reg_eax = regtab;
3103 }
3104 else if (regtab->reg_type.bitfield.tbyte)
6288d05f
JB
3105 {
3106 /* There's no point inserting st(<N>) in the hash table, as
3107 parentheses aren't included in register_chars[] anyway. */
3108 if (regtab->reg_type.bitfield.instance != Accum)
3109 continue;
3110 reg_st0 = regtab;
3111 }
3112 break;
3113
5e042380
JB
3114 case SReg:
3115 switch (regtab->reg_num)
3116 {
3117 case 0: reg_es = regtab; break;
3118 case 2: reg_ss = regtab; break;
3119 case 3: reg_ds = regtab; break;
3120 }
3121 break;
3122
6288d05f
JB
3123 case RegMask:
3124 if (!regtab->reg_num)
3125 reg_k0 = regtab;
3126 break;
3127 }
3128
6225c532
JB
3129 if (str_hash_insert (reg_hash, regtab->reg_name, regtab, 0) != NULL)
3130 as_fatal (_("duplicate %s"), regtab->reg_name);
6225c532 3131 }
252b5132
RH
3132 }
3133
47926f60 3134 /* Fill in lexical tables: mnemonic_chars, operand_chars. */
252b5132 3135 {
29b0f896
AM
3136 int c;
3137 char *p;
252b5132
RH
3138
3139 for (c = 0; c < 256; c++)
3140 {
014fbcda 3141 if (ISDIGIT (c) || ISLOWER (c))
252b5132
RH
3142 {
3143 mnemonic_chars[c] = c;
3144 register_chars[c] = c;
3145 operand_chars[c] = c;
3146 }
3882b010 3147 else if (ISUPPER (c))
252b5132 3148 {
3882b010 3149 mnemonic_chars[c] = TOLOWER (c);
252b5132
RH
3150 register_chars[c] = mnemonic_chars[c];
3151 operand_chars[c] = c;
3152 }
43234a1e 3153 else if (c == '{' || c == '}')
86fa6981
L
3154 {
3155 mnemonic_chars[c] = c;
3156 operand_chars[c] = c;
3157 }
b3983e5f
JB
3158#ifdef SVR4_COMMENT_CHARS
3159 else if (c == '\\' && strchr (i386_comment_chars, '/'))
3160 operand_chars[c] = c;
3161#endif
252b5132 3162
3882b010 3163 if (ISALPHA (c) || ISDIGIT (c))
252b5132
RH
3164 identifier_chars[c] = c;
3165 else if (c >= 128)
3166 {
3167 identifier_chars[c] = c;
3168 operand_chars[c] = c;
3169 }
3170 }
3171
3172#ifdef LEX_AT
3173 identifier_chars['@'] = '@';
32137342
NC
3174#endif
3175#ifdef LEX_QM
3176 identifier_chars['?'] = '?';
3177 operand_chars['?'] = '?';
252b5132 3178#endif
c0f3af97 3179 mnemonic_chars['_'] = '_';
791fe849 3180 mnemonic_chars['-'] = '-';
0003779b 3181 mnemonic_chars['.'] = '.';
252b5132
RH
3182 identifier_chars['_'] = '_';
3183 identifier_chars['.'] = '.';
3184
3185 for (p = operand_special_chars; *p != '\0'; p++)
3186 operand_chars[(unsigned char) *p] = *p;
3187 }
3188
a4447b93
RH
3189 if (flag_code == CODE_64BIT)
3190 {
ca19b261
KT
3191#if defined (OBJ_COFF) && defined (TE_PE)
3192 x86_dwarf2_return_column = (OUTPUT_FLAVOR == bfd_target_coff_flavour
3193 ? 32 : 16);
3194#else
a4447b93 3195 x86_dwarf2_return_column = 16;
ca19b261 3196#endif
61ff971f 3197 x86_cie_data_alignment = -8;
a4447b93
RH
3198 }
3199 else
3200 {
3201 x86_dwarf2_return_column = 8;
3202 x86_cie_data_alignment = -4;
3203 }
e379e5f3
L
3204
3205 /* NB: FUSED_JCC_PADDING frag must have sufficient room so that it
3206 can be turned into BRANCH_PREFIX frag. */
3207 if (align_branch_prefix_size > MAX_FUSED_JCC_PADDING_SIZE)
3208 abort ();
252b5132
RH
3209}
3210
3211void
e3bb37b5 3212i386_print_statistics (FILE *file)
252b5132 3213{
629310ab
ML
3214 htab_print_statistics (file, "i386 opcode", op_hash);
3215 htab_print_statistics (file, "i386 register", reg_hash);
252b5132
RH
3216}
3217\f
252b5132
RH
3218#ifdef DEBUG386
3219
ce8a8b2f 3220/* Debugging routines for md_assemble. */
d3ce72d0 3221static void pte (insn_template *);
40fb9820 3222static void pt (i386_operand_type);
e3bb37b5
L
3223static void pe (expressionS *);
3224static void ps (symbolS *);
252b5132
RH
3225
3226static void
2c703856 3227pi (const char *line, i386_insn *x)
252b5132 3228{
09137c09 3229 unsigned int j;
252b5132
RH
3230
3231 fprintf (stdout, "%s: template ", line);
3232 pte (&x->tm);
09f131f2
JH
3233 fprintf (stdout, " address: base %s index %s scale %x\n",
3234 x->base_reg ? x->base_reg->reg_name : "none",
3235 x->index_reg ? x->index_reg->reg_name : "none",
3236 x->log2_scale_factor);
3237 fprintf (stdout, " modrm: mode %x reg %x reg/mem %x\n",
252b5132 3238 x->rm.mode, x->rm.reg, x->rm.regmem);
09f131f2
JH
3239 fprintf (stdout, " sib: base %x index %x scale %x\n",
3240 x->sib.base, x->sib.index, x->sib.scale);
3241 fprintf (stdout, " rex: 64bit %x extX %x extY %x extZ %x\n",
161a04f6
L
3242 (x->rex & REX_W) != 0,
3243 (x->rex & REX_R) != 0,
3244 (x->rex & REX_X) != 0,
3245 (x->rex & REX_B) != 0);
09137c09 3246 for (j = 0; j < x->operands; j++)
252b5132 3247 {
09137c09
SP
3248 fprintf (stdout, " #%d: ", j + 1);
3249 pt (x->types[j]);
252b5132 3250 fprintf (stdout, "\n");
bab6aec1 3251 if (x->types[j].bitfield.class == Reg
3528c362
JB
3252 || x->types[j].bitfield.class == RegMMX
3253 || x->types[j].bitfield.class == RegSIMD
dd6b8a0b 3254 || x->types[j].bitfield.class == RegMask
00cee14f 3255 || x->types[j].bitfield.class == SReg
4a5c67ed
JB
3256 || x->types[j].bitfield.class == RegCR
3257 || x->types[j].bitfield.class == RegDR
dd6b8a0b
JB
3258 || x->types[j].bitfield.class == RegTR
3259 || x->types[j].bitfield.class == RegBND)
09137c09
SP
3260 fprintf (stdout, "%s\n", x->op[j].regs->reg_name);
3261 if (operand_type_check (x->types[j], imm))
3262 pe (x->op[j].imms);
3263 if (operand_type_check (x->types[j], disp))
3264 pe (x->op[j].disps);
252b5132
RH
3265 }
3266}
3267
3268static void
d3ce72d0 3269pte (insn_template *t)
252b5132 3270{
b933fa4b 3271 static const unsigned char opc_pfx[] = { 0, 0x66, 0xf3, 0xf2 };
441f6aca
JB
3272 static const char *const opc_spc[] = {
3273 NULL, "0f", "0f38", "0f3a", NULL, NULL, NULL, NULL,
3274 "XOP08", "XOP09", "XOP0A",
3275 };
09137c09 3276 unsigned int j;
441f6aca 3277
252b5132 3278 fprintf (stdout, " %d operands ", t->operands);
441f6aca
JB
3279 if (opc_pfx[t->opcode_modifier.opcodeprefix])
3280 fprintf (stdout, "pfx %x ", opc_pfx[t->opcode_modifier.opcodeprefix]);
3281 if (opc_spc[t->opcode_modifier.opcodespace])
3282 fprintf (stdout, "space %s ", opc_spc[t->opcode_modifier.opcodespace]);
47926f60 3283 fprintf (stdout, "opcode %x ", t->base_opcode);
252b5132
RH
3284 if (t->extension_opcode != None)
3285 fprintf (stdout, "ext %x ", t->extension_opcode);
40fb9820 3286 if (t->opcode_modifier.d)
252b5132 3287 fprintf (stdout, "D");
40fb9820 3288 if (t->opcode_modifier.w)
252b5132
RH
3289 fprintf (stdout, "W");
3290 fprintf (stdout, "\n");
09137c09 3291 for (j = 0; j < t->operands; j++)
252b5132 3292 {
09137c09
SP
3293 fprintf (stdout, " #%d type ", j + 1);
3294 pt (t->operand_types[j]);
252b5132
RH
3295 fprintf (stdout, "\n");
3296 }
3297}
3298
3299static void
e3bb37b5 3300pe (expressionS *e)
252b5132 3301{
24eab124 3302 fprintf (stdout, " operation %d\n", e->X_op);
7b025ee8
JB
3303 fprintf (stdout, " add_number %" BFD_VMA_FMT "d (%" BFD_VMA_FMT "x)\n",
3304 e->X_add_number, e->X_add_number);
252b5132
RH
3305 if (e->X_add_symbol)
3306 {
3307 fprintf (stdout, " add_symbol ");
3308 ps (e->X_add_symbol);
3309 fprintf (stdout, "\n");
3310 }
3311 if (e->X_op_symbol)
3312 {
3313 fprintf (stdout, " op_symbol ");
3314 ps (e->X_op_symbol);
3315 fprintf (stdout, "\n");
3316 }
3317}
3318
3319static void
e3bb37b5 3320ps (symbolS *s)
252b5132
RH
3321{
3322 fprintf (stdout, "%s type %s%s",
3323 S_GET_NAME (s),
3324 S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
3325 segment_name (S_GET_SEGMENT (s)));
3326}
3327
7b81dfbb 3328static struct type_name
252b5132 3329 {
40fb9820
L
3330 i386_operand_type mask;
3331 const char *name;
252b5132 3332 }
7b81dfbb 3333const type_names[] =
252b5132 3334{
40fb9820
L
3335 { OPERAND_TYPE_REG8, "r8" },
3336 { OPERAND_TYPE_REG16, "r16" },
3337 { OPERAND_TYPE_REG32, "r32" },
3338 { OPERAND_TYPE_REG64, "r64" },
2c703856
JB
3339 { OPERAND_TYPE_ACC8, "acc8" },
3340 { OPERAND_TYPE_ACC16, "acc16" },
3341 { OPERAND_TYPE_ACC32, "acc32" },
3342 { OPERAND_TYPE_ACC64, "acc64" },
40fb9820
L
3343 { OPERAND_TYPE_IMM8, "i8" },
3344 { OPERAND_TYPE_IMM8, "i8s" },
3345 { OPERAND_TYPE_IMM16, "i16" },
3346 { OPERAND_TYPE_IMM32, "i32" },
3347 { OPERAND_TYPE_IMM32S, "i32s" },
3348 { OPERAND_TYPE_IMM64, "i64" },
3349 { OPERAND_TYPE_IMM1, "i1" },
3350 { OPERAND_TYPE_BASEINDEX, "BaseIndex" },
3351 { OPERAND_TYPE_DISP8, "d8" },
3352 { OPERAND_TYPE_DISP16, "d16" },
3353 { OPERAND_TYPE_DISP32, "d32" },
3354 { OPERAND_TYPE_DISP32S, "d32s" },
3355 { OPERAND_TYPE_DISP64, "d64" },
3356 { OPERAND_TYPE_INOUTPORTREG, "InOutPortReg" },
3357 { OPERAND_TYPE_SHIFTCOUNT, "ShiftCount" },
3358 { OPERAND_TYPE_CONTROL, "control reg" },
3359 { OPERAND_TYPE_TEST, "test reg" },
3360 { OPERAND_TYPE_DEBUG, "debug reg" },
3361 { OPERAND_TYPE_FLOATREG, "FReg" },
3362 { OPERAND_TYPE_FLOATACC, "FAcc" },
21df382b 3363 { OPERAND_TYPE_SREG, "SReg" },
40fb9820
L
3364 { OPERAND_TYPE_REGMMX, "rMMX" },
3365 { OPERAND_TYPE_REGXMM, "rXMM" },
0349dc08 3366 { OPERAND_TYPE_REGYMM, "rYMM" },
43234a1e 3367 { OPERAND_TYPE_REGZMM, "rZMM" },
260cd341 3368 { OPERAND_TYPE_REGTMM, "rTMM" },
43234a1e 3369 { OPERAND_TYPE_REGMASK, "Mask reg" },
252b5132
RH
3370};
3371
3372static void
40fb9820 3373pt (i386_operand_type t)
252b5132 3374{
40fb9820 3375 unsigned int j;
c6fb90c8 3376 i386_operand_type a;
252b5132 3377
40fb9820 3378 for (j = 0; j < ARRAY_SIZE (type_names); j++)
c6fb90c8
L
3379 {
3380 a = operand_type_and (t, type_names[j].mask);
2c703856 3381 if (operand_type_equal (&a, &type_names[j].mask))
c6fb90c8
L
3382 fprintf (stdout, "%s, ", type_names[j].name);
3383 }
252b5132
RH
3384 fflush (stdout);
3385}
3386
3387#endif /* DEBUG386 */
3388\f
252b5132 3389static bfd_reloc_code_real_type
3956db08 3390reloc (unsigned int size,
64e74474
AM
3391 int pcrel,
3392 int sign,
3393 bfd_reloc_code_real_type other)
252b5132 3394{
47926f60 3395 if (other != NO_RELOC)
3956db08 3396 {
91d6fa6a 3397 reloc_howto_type *rel;
3956db08
JB
3398
3399 if (size == 8)
3400 switch (other)
3401 {
64e74474
AM
3402 case BFD_RELOC_X86_64_GOT32:
3403 return BFD_RELOC_X86_64_GOT64;
3404 break;
553d1284
L
3405 case BFD_RELOC_X86_64_GOTPLT64:
3406 return BFD_RELOC_X86_64_GOTPLT64;
3407 break;
64e74474
AM
3408 case BFD_RELOC_X86_64_PLTOFF64:
3409 return BFD_RELOC_X86_64_PLTOFF64;
3410 break;
3411 case BFD_RELOC_X86_64_GOTPC32:
3412 other = BFD_RELOC_X86_64_GOTPC64;
3413 break;
3414 case BFD_RELOC_X86_64_GOTPCREL:
3415 other = BFD_RELOC_X86_64_GOTPCREL64;
3416 break;
3417 case BFD_RELOC_X86_64_TPOFF32:
3418 other = BFD_RELOC_X86_64_TPOFF64;
3419 break;
3420 case BFD_RELOC_X86_64_DTPOFF32:
3421 other = BFD_RELOC_X86_64_DTPOFF64;
3422 break;
3423 default:
3424 break;
3956db08 3425 }
e05278af 3426
8ce3d284 3427#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
3428 if (other == BFD_RELOC_SIZE32)
3429 {
3430 if (size == 8)
1ab668bf 3431 other = BFD_RELOC_SIZE64;
8fd4256d 3432 if (pcrel)
1ab668bf
AM
3433 {
3434 as_bad (_("there are no pc-relative size relocations"));
3435 return NO_RELOC;
3436 }
8fd4256d 3437 }
8ce3d284 3438#endif
8fd4256d 3439
e05278af 3440 /* Sign-checking 4-byte relocations in 16-/32-bit code is pointless. */
f2d8a97c 3441 if (size == 4 && (flag_code != CODE_64BIT || disallow_64bit_reloc))
e05278af
JB
3442 sign = -1;
3443
91d6fa6a
NC
3444 rel = bfd_reloc_type_lookup (stdoutput, other);
3445 if (!rel)
3956db08 3446 as_bad (_("unknown relocation (%u)"), other);
91d6fa6a 3447 else if (size != bfd_get_reloc_size (rel))
3956db08 3448 as_bad (_("%u-byte relocation cannot be applied to %u-byte field"),
91d6fa6a 3449 bfd_get_reloc_size (rel),
3956db08 3450 size);
91d6fa6a 3451 else if (pcrel && !rel->pc_relative)
3956db08 3452 as_bad (_("non-pc-relative relocation for pc-relative field"));
91d6fa6a 3453 else if ((rel->complain_on_overflow == complain_overflow_signed
3956db08 3454 && !sign)
91d6fa6a 3455 || (rel->complain_on_overflow == complain_overflow_unsigned
64e74474 3456 && sign > 0))
3956db08
JB
3457 as_bad (_("relocated field and relocation type differ in signedness"));
3458 else
3459 return other;
3460 return NO_RELOC;
3461 }
252b5132
RH
3462
3463 if (pcrel)
3464 {
3e73aa7c 3465 if (!sign)
3956db08 3466 as_bad (_("there are no unsigned pc-relative relocations"));
252b5132
RH
3467 switch (size)
3468 {
3469 case 1: return BFD_RELOC_8_PCREL;
3470 case 2: return BFD_RELOC_16_PCREL;
d258b828 3471 case 4: return BFD_RELOC_32_PCREL;
d6ab8113 3472 case 8: return BFD_RELOC_64_PCREL;
252b5132 3473 }
3956db08 3474 as_bad (_("cannot do %u byte pc-relative relocation"), size);
252b5132
RH
3475 }
3476 else
3477 {
3956db08 3478 if (sign > 0)
e5cb08ac 3479 switch (size)
3e73aa7c
JH
3480 {
3481 case 4: return BFD_RELOC_X86_64_32S;
3482 }
3483 else
3484 switch (size)
3485 {
3486 case 1: return BFD_RELOC_8;
3487 case 2: return BFD_RELOC_16;
3488 case 4: return BFD_RELOC_32;
3489 case 8: return BFD_RELOC_64;
3490 }
3956db08
JB
3491 as_bad (_("cannot do %s %u byte relocation"),
3492 sign > 0 ? "signed" : "unsigned", size);
252b5132
RH
3493 }
3494
0cc9e1d3 3495 return NO_RELOC;
252b5132
RH
3496}
3497
47926f60
KH
3498/* Here we decide which fixups can be adjusted to make them relative to
3499 the beginning of the section instead of the symbol. Basically we need
3500 to make sure that the dynamic relocations are done correctly, so in
3501 some cases we force the original symbol to be used. */
3502
252b5132 3503int
e3bb37b5 3504tc_i386_fix_adjustable (fixS *fixP ATTRIBUTE_UNUSED)
252b5132 3505{
6d249963 3506#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 3507 if (!IS_ELF)
31312f95
AM
3508 return 1;
3509
a161fe53
AM
3510 /* Don't adjust pc-relative references to merge sections in 64-bit
3511 mode. */
3512 if (use_rela_relocations
3513 && (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0
3514 && fixP->fx_pcrel)
252b5132 3515 return 0;
31312f95 3516
8d01d9a9
AJ
3517 /* The x86_64 GOTPCREL are represented as 32bit PCrel relocations
3518 and changed later by validate_fix. */
3519 if (GOT_symbol && fixP->fx_subsy == GOT_symbol
3520 && fixP->fx_r_type == BFD_RELOC_32_PCREL)
3521 return 0;
3522
8fd4256d
L
3523 /* Adjust_reloc_syms doesn't know about the GOT. Need to keep symbol
3524 for size relocations. */
3525 if (fixP->fx_r_type == BFD_RELOC_SIZE32
3526 || fixP->fx_r_type == BFD_RELOC_SIZE64
3527 || fixP->fx_r_type == BFD_RELOC_386_GOTOFF
252b5132 3528 || fixP->fx_r_type == BFD_RELOC_386_GOT32
02a86693 3529 || fixP->fx_r_type == BFD_RELOC_386_GOT32X
13ae64f3
JJ
3530 || fixP->fx_r_type == BFD_RELOC_386_TLS_GD
3531 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDM
3532 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDO_32
3533 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE_32
37e55690
JJ
3534 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE
3535 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTIE
13ae64f3
JJ
3536 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE_32
3537 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE
67a4f2b7
AO
3538 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTDESC
3539 || fixP->fx_r_type == BFD_RELOC_386_TLS_DESC_CALL
3e73aa7c 3540 || fixP->fx_r_type == BFD_RELOC_X86_64_GOT32
80b3ee89 3541 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCREL
56ceb5b5
L
3542 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCRELX
3543 || fixP->fx_r_type == BFD_RELOC_X86_64_REX_GOTPCRELX
bffbf940
JJ
3544 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSGD
3545 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSLD
3546 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF32
d6ab8113 3547 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF64
bffbf940
JJ
3548 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF
3549 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF32
d6ab8113
JB
3550 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF64
3551 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTOFF64
67a4f2b7
AO
3552 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
3553 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSDESC_CALL
252b5132
RH
3554 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3555 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3556 return 0;
31312f95 3557#endif
252b5132
RH
3558 return 1;
3559}
252b5132 3560
a9aabc23
JB
3561static INLINE bool
3562want_disp32 (const insn_template *t)
3563{
3564 return flag_code != CODE_64BIT
3565 || i.prefix[ADDR_PREFIX]
3566 || (t->base_opcode == 0x8d
3567 && t->opcode_modifier.opcodespace == SPACE_BASE
fe134c65
JB
3568 && (!i.types[1].bitfield.qword
3569 || t->opcode_modifier.size == SIZE32));
a9aabc23
JB
3570}
3571
b4cac588 3572static int
e3bb37b5 3573intel_float_operand (const char *mnemonic)
252b5132 3574{
9306ca4a
JB
3575 /* Note that the value returned is meaningful only for opcodes with (memory)
3576 operands, hence the code here is free to improperly handle opcodes that
3577 have no operands (for better performance and smaller code). */
3578
3579 if (mnemonic[0] != 'f')
3580 return 0; /* non-math */
3581
3582 switch (mnemonic[1])
3583 {
3584 /* fclex, fdecstp, fdisi, femms, feni, fincstp, finit, fsetpm, and
3585 the fs segment override prefix not currently handled because no
3586 call path can make opcodes without operands get here */
3587 case 'i':
3588 return 2 /* integer op */;
3589 case 'l':
3590 if (mnemonic[2] == 'd' && (mnemonic[3] == 'c' || mnemonic[3] == 'e'))
3591 return 3; /* fldcw/fldenv */
3592 break;
3593 case 'n':
3594 if (mnemonic[2] != 'o' /* fnop */)
3595 return 3; /* non-waiting control op */
3596 break;
3597 case 'r':
3598 if (mnemonic[2] == 's')
3599 return 3; /* frstor/frstpm */
3600 break;
3601 case 's':
3602 if (mnemonic[2] == 'a')
3603 return 3; /* fsave */
3604 if (mnemonic[2] == 't')
3605 {
3606 switch (mnemonic[3])
3607 {
3608 case 'c': /* fstcw */
3609 case 'd': /* fstdw */
3610 case 'e': /* fstenv */
3611 case 's': /* fsts[gw] */
3612 return 3;
3613 }
3614 }
3615 break;
3616 case 'x':
3617 if (mnemonic[2] == 'r' || mnemonic[2] == 's')
3618 return 0; /* fxsave/fxrstor are not really math ops */
3619 break;
3620 }
252b5132 3621
9306ca4a 3622 return 1;
252b5132
RH
3623}
3624
9a182d04
JB
3625static INLINE void
3626install_template (const insn_template *t)
3627{
3628 unsigned int l;
3629
3630 i.tm = *t;
3631
3632 /* Note that for pseudo prefixes this produces a length of 1. But for them
3633 the length isn't interesting at all. */
3634 for (l = 1; l < 4; ++l)
3635 if (!(t->base_opcode >> (8 * l)))
3636 break;
3637
3638 i.opcode_length = l;
3639}
3640
c0f3af97
L
3641/* Build the VEX prefix. */
3642
3643static void
d3ce72d0 3644build_vex_prefix (const insn_template *t)
c0f3af97
L
3645{
3646 unsigned int register_specifier;
c0f3af97 3647 unsigned int vector_length;
03751133 3648 unsigned int w;
c0f3af97
L
3649
3650 /* Check register specifier. */
3651 if (i.vex.register_specifier)
43234a1e
L
3652 {
3653 register_specifier =
3654 ~register_number (i.vex.register_specifier) & 0xf;
3655 gas_assert ((i.vex.register_specifier->reg_flags & RegVRex) == 0);
3656 }
c0f3af97
L
3657 else
3658 register_specifier = 0xf;
3659
79f0fa25
L
3660 /* Use 2-byte VEX prefix by swapping destination and source operand
3661 if there are more than 1 register operand. */
3662 if (i.reg_operands > 1
3663 && i.vec_encoding != vex_encoding_vex3
86fa6981 3664 && i.dir_encoding == dir_encoding_default
fa99fab2 3665 && i.operands == i.reg_operands
dbbc8b7e 3666 && operand_type_equal (&i.types[0], &i.types[i.operands - 1])
441f6aca 3667 && i.tm.opcode_modifier.opcodespace == SPACE_0F
dbbc8b7e 3668 && (i.tm.opcode_modifier.load || i.tm.opcode_modifier.d)
fa99fab2
L
3669 && i.rex == REX_B)
3670 {
3671 unsigned int xchg = i.operands - 1;
3672 union i386_op temp_op;
3673 i386_operand_type temp_type;
3674
3675 temp_type = i.types[xchg];
3676 i.types[xchg] = i.types[0];
3677 i.types[0] = temp_type;
3678 temp_op = i.op[xchg];
3679 i.op[xchg] = i.op[0];
3680 i.op[0] = temp_op;
3681
9c2799c2 3682 gas_assert (i.rm.mode == 3);
fa99fab2
L
3683
3684 i.rex = REX_R;
3685 xchg = i.rm.regmem;
3686 i.rm.regmem = i.rm.reg;
3687 i.rm.reg = xchg;
3688
dbbc8b7e
JB
3689 if (i.tm.opcode_modifier.d)
3690 i.tm.base_opcode ^= (i.tm.base_opcode & 0xee) != 0x6e
3691 ? Opcode_SIMD_FloatD : Opcode_SIMD_IntD;
3692 else /* Use the next insn. */
9a182d04 3693 install_template (&t[1]);
fa99fab2
L
3694 }
3695
79dec6b7
JB
3696 /* Use 2-byte VEX prefix by swapping commutative source operands if there
3697 are no memory operands and at least 3 register ones. */
3698 if (i.reg_operands >= 3
3699 && i.vec_encoding != vex_encoding_vex3
3700 && i.reg_operands == i.operands - i.imm_operands
3701 && i.tm.opcode_modifier.vex
3702 && i.tm.opcode_modifier.commutative
3703 && (i.tm.opcode_modifier.sse2avx || optimize > 1)
3704 && i.rex == REX_B
3705 && i.vex.register_specifier
3706 && !(i.vex.register_specifier->reg_flags & RegRex))
3707 {
3708 unsigned int xchg = i.operands - i.reg_operands;
3709 union i386_op temp_op;
3710 i386_operand_type temp_type;
3711
441f6aca 3712 gas_assert (i.tm.opcode_modifier.opcodespace == SPACE_0F);
79dec6b7
JB
3713 gas_assert (!i.tm.opcode_modifier.sae);
3714 gas_assert (operand_type_equal (&i.types[i.operands - 2],
3715 &i.types[i.operands - 3]));
3716 gas_assert (i.rm.mode == 3);
3717
3718 temp_type = i.types[xchg];
3719 i.types[xchg] = i.types[xchg + 1];
3720 i.types[xchg + 1] = temp_type;
3721 temp_op = i.op[xchg];
3722 i.op[xchg] = i.op[xchg + 1];
3723 i.op[xchg + 1] = temp_op;
3724
3725 i.rex = 0;
3726 xchg = i.rm.regmem | 8;
3727 i.rm.regmem = ~register_specifier & 0xf;
3728 gas_assert (!(i.rm.regmem & 8));
3729 i.vex.register_specifier += xchg - i.rm.regmem;
3730 register_specifier = ~xchg & 0xf;
3731 }
3732
539f890d
L
3733 if (i.tm.opcode_modifier.vex == VEXScalar)
3734 vector_length = avxscalar;
10c17abd
JB
3735 else if (i.tm.opcode_modifier.vex == VEX256)
3736 vector_length = 1;
539f890d 3737 else
10c17abd 3738 {
56522fc5 3739 unsigned int op;
10c17abd 3740
c7213af9
L
3741 /* Determine vector length from the last multi-length vector
3742 operand. */
10c17abd 3743 vector_length = 0;
56522fc5 3744 for (op = t->operands; op--;)
10c17abd
JB
3745 if (t->operand_types[op].bitfield.xmmword
3746 && t->operand_types[op].bitfield.ymmword
3747 && i.types[op].bitfield.ymmword)
3748 {
3749 vector_length = 1;
3750 break;
3751 }
3752 }
c0f3af97 3753
03751133
L
3754 /* Check the REX.W bit and VEXW. */
3755 if (i.tm.opcode_modifier.vexw == VEXWIG)
3756 w = (vexwig == vexw1 || (i.rex & REX_W)) ? 1 : 0;
3757 else if (i.tm.opcode_modifier.vexw)
3758 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3759 else
931d03b7 3760 w = (flag_code == CODE_64BIT ? i.rex & REX_W : vexwig == vexw1) ? 1 : 0;
03751133 3761
c0f3af97 3762 /* Use 2-byte VEX prefix if possible. */
03751133
L
3763 if (w == 0
3764 && i.vec_encoding != vex_encoding_vex3
441f6aca 3765 && i.tm.opcode_modifier.opcodespace == SPACE_0F
c0f3af97
L
3766 && (i.rex & (REX_W | REX_X | REX_B)) == 0)
3767 {
3768 /* 2-byte VEX prefix. */
3769 unsigned int r;
3770
3771 i.vex.length = 2;
3772 i.vex.bytes[0] = 0xc5;
3773
3774 /* Check the REX.R bit. */
3775 r = (i.rex & REX_R) ? 0 : 1;
3776 i.vex.bytes[1] = (r << 7
3777 | register_specifier << 3
3778 | vector_length << 2
35648716 3779 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3780 }
3781 else
3782 {
3783 /* 3-byte VEX prefix. */
f88c9eb0 3784 i.vex.length = 3;
f88c9eb0 3785
441f6aca 3786 switch (i.tm.opcode_modifier.opcodespace)
5dd85c99 3787 {
441f6aca
JB
3788 case SPACE_0F:
3789 case SPACE_0F38:
3790 case SPACE_0F3A:
80de6e00 3791 i.vex.bytes[0] = 0xc4;
7f399153 3792 break;
441f6aca
JB
3793 case SPACE_XOP08:
3794 case SPACE_XOP09:
3795 case SPACE_XOP0A:
f88c9eb0 3796 i.vex.bytes[0] = 0x8f;
7f399153
L
3797 break;
3798 default:
3799 abort ();
f88c9eb0 3800 }
c0f3af97 3801
c0f3af97
L
3802 /* The high 3 bits of the second VEX byte are 1's compliment
3803 of RXB bits from REX. */
441f6aca 3804 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | i.tm.opcode_modifier.opcodespace;
c0f3af97 3805
c0f3af97
L
3806 i.vex.bytes[2] = (w << 7
3807 | register_specifier << 3
3808 | vector_length << 2
35648716 3809 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3810 }
3811}
3812
5b7c81bd 3813static INLINE bool
e771e7c9
JB
3814is_evex_encoding (const insn_template *t)
3815{
7091c612 3816 return t->opcode_modifier.evex || t->opcode_modifier.disp8memshift
e771e7c9 3817 || t->opcode_modifier.broadcast || t->opcode_modifier.masking
a80195f1 3818 || t->opcode_modifier.sae;
e771e7c9
JB
3819}
3820
5b7c81bd 3821static INLINE bool
7a8655d2
JB
3822is_any_vex_encoding (const insn_template *t)
3823{
7b47a312 3824 return t->opcode_modifier.vex || is_evex_encoding (t);
7a8655d2
JB
3825}
3826
43234a1e
L
3827/* Build the EVEX prefix. */
3828
3829static void
3830build_evex_prefix (void)
3831{
35648716 3832 unsigned int register_specifier, w;
43234a1e
L
3833 rex_byte vrex_used = 0;
3834
3835 /* Check register specifier. */
3836 if (i.vex.register_specifier)
3837 {
3838 gas_assert ((i.vrex & REX_X) == 0);
3839
3840 register_specifier = i.vex.register_specifier->reg_num;
3841 if ((i.vex.register_specifier->reg_flags & RegRex))
3842 register_specifier += 8;
3843 /* The upper 16 registers are encoded in the fourth byte of the
3844 EVEX prefix. */
3845 if (!(i.vex.register_specifier->reg_flags & RegVRex))
3846 i.vex.bytes[3] = 0x8;
3847 register_specifier = ~register_specifier & 0xf;
3848 }
3849 else
3850 {
3851 register_specifier = 0xf;
3852
3853 /* Encode upper 16 vector index register in the fourth byte of
3854 the EVEX prefix. */
3855 if (!(i.vrex & REX_X))
3856 i.vex.bytes[3] = 0x8;
3857 else
3858 vrex_used |= REX_X;
3859 }
3860
43234a1e
L
3861 /* 4 byte EVEX prefix. */
3862 i.vex.length = 4;
3863 i.vex.bytes[0] = 0x62;
3864
43234a1e
L
3865 /* The high 3 bits of the second EVEX byte are 1's compliment of RXB
3866 bits from REX. */
441f6aca
JB
3867 gas_assert (i.tm.opcode_modifier.opcodespace >= SPACE_0F);
3868 gas_assert (i.tm.opcode_modifier.opcodespace <= SPACE_0F3A);
3869 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | i.tm.opcode_modifier.opcodespace;
43234a1e
L
3870
3871 /* The fifth bit of the second EVEX byte is 1's compliment of the
3872 REX_R bit in VREX. */
3873 if (!(i.vrex & REX_R))
3874 i.vex.bytes[1] |= 0x10;
3875 else
3876 vrex_used |= REX_R;
3877
3878 if ((i.reg_operands + i.imm_operands) == i.operands)
3879 {
3880 /* When all operands are registers, the REX_X bit in REX is not
3881 used. We reuse it to encode the upper 16 registers, which is
3882 indicated by the REX_B bit in VREX. The REX_X bit is encoded
3883 as 1's compliment. */
3884 if ((i.vrex & REX_B))
3885 {
3886 vrex_used |= REX_B;
3887 i.vex.bytes[1] &= ~0x40;
3888 }
3889 }
3890
3891 /* EVEX instructions shouldn't need the REX prefix. */
3892 i.vrex &= ~vrex_used;
3893 gas_assert (i.vrex == 0);
3894
6865c043
L
3895 /* Check the REX.W bit and VEXW. */
3896 if (i.tm.opcode_modifier.vexw == VEXWIG)
3897 w = (evexwig == evexw1 || (i.rex & REX_W)) ? 1 : 0;
3898 else if (i.tm.opcode_modifier.vexw)
3899 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3900 else
931d03b7 3901 w = (flag_code == CODE_64BIT ? i.rex & REX_W : evexwig == evexw1) ? 1 : 0;
43234a1e 3902
43234a1e 3903 /* The third byte of the EVEX prefix. */
35648716
JB
3904 i.vex.bytes[2] = ((w << 7)
3905 | (register_specifier << 3)
3906 | 4 /* Encode the U bit. */
3907 | i.tm.opcode_modifier.opcodeprefix);
43234a1e
L
3908
3909 /* The fourth byte of the EVEX prefix. */
3910 /* The zeroing-masking bit. */
6225c532 3911 if (i.mask.reg && i.mask.zeroing)
43234a1e
L
3912 i.vex.bytes[3] |= 0x80;
3913
3914 /* Don't always set the broadcast bit if there is no RC. */
ca5312a2 3915 if (i.rounding.type == rc_none)
43234a1e
L
3916 {
3917 /* Encode the vector length. */
3918 unsigned int vec_length;
3919
e771e7c9
JB
3920 if (!i.tm.opcode_modifier.evex
3921 || i.tm.opcode_modifier.evex == EVEXDYN)
3922 {
56522fc5 3923 unsigned int op;
e771e7c9 3924
c7213af9
L
3925 /* Determine vector length from the last multi-length vector
3926 operand. */
56522fc5 3927 for (op = i.operands; op--;)
e771e7c9
JB
3928 if (i.tm.operand_types[op].bitfield.xmmword
3929 + i.tm.operand_types[op].bitfield.ymmword
3930 + i.tm.operand_types[op].bitfield.zmmword > 1)
3931 {
3932 if (i.types[op].bitfield.zmmword)
c7213af9
L
3933 {
3934 i.tm.opcode_modifier.evex = EVEX512;
3935 break;
3936 }
e771e7c9 3937 else if (i.types[op].bitfield.ymmword)
c7213af9
L
3938 {
3939 i.tm.opcode_modifier.evex = EVEX256;
3940 break;
3941 }
e771e7c9 3942 else if (i.types[op].bitfield.xmmword)
c7213af9
L
3943 {
3944 i.tm.opcode_modifier.evex = EVEX128;
3945 break;
3946 }
5273a3cd 3947 else if (i.broadcast.type && op == i.broadcast.operand)
625cbd7a 3948 {
5273a3cd 3949 switch (i.broadcast.bytes)
625cbd7a
JB
3950 {
3951 case 64:
3952 i.tm.opcode_modifier.evex = EVEX512;
3953 break;
3954 case 32:
3955 i.tm.opcode_modifier.evex = EVEX256;
3956 break;
3957 case 16:
3958 i.tm.opcode_modifier.evex = EVEX128;
3959 break;
3960 default:
c7213af9 3961 abort ();
625cbd7a 3962 }
c7213af9 3963 break;
625cbd7a 3964 }
e771e7c9 3965 }
c7213af9 3966
56522fc5 3967 if (op >= MAX_OPERANDS)
c7213af9 3968 abort ();
e771e7c9
JB
3969 }
3970
43234a1e
L
3971 switch (i.tm.opcode_modifier.evex)
3972 {
3973 case EVEXLIG: /* LL' is ignored */
3974 vec_length = evexlig << 5;
3975 break;
3976 case EVEX128:
3977 vec_length = 0 << 5;
3978 break;
3979 case EVEX256:
3980 vec_length = 1 << 5;
3981 break;
3982 case EVEX512:
3983 vec_length = 2 << 5;
3984 break;
3985 default:
3986 abort ();
3987 break;
3988 }
3989 i.vex.bytes[3] |= vec_length;
3990 /* Encode the broadcast bit. */
5273a3cd 3991 if (i.broadcast.type)
43234a1e
L
3992 i.vex.bytes[3] |= 0x10;
3993 }
ca5312a2
JB
3994 else if (i.rounding.type != saeonly)
3995 i.vex.bytes[3] |= 0x10 | (i.rounding.type << 5);
43234a1e 3996 else
ca5312a2 3997 i.vex.bytes[3] |= 0x10 | (evexrcig << 5);
43234a1e 3998
6225c532
JB
3999 if (i.mask.reg)
4000 i.vex.bytes[3] |= i.mask.reg->reg_num;
43234a1e
L
4001}
4002
65da13b5
L
4003static void
4004process_immext (void)
4005{
4006 expressionS *exp;
4007
c0f3af97 4008 /* These AMD 3DNow! and SSE2 instructions have an opcode suffix
65da13b5
L
4009 which is coded in the same place as an 8-bit immediate field
4010 would be. Here we fake an 8-bit immediate operand from the
4011 opcode suffix stored in tm.extension_opcode.
4012
c1e679ec 4013 AVX instructions also use this encoding, for some of
c0f3af97 4014 3 argument instructions. */
65da13b5 4015
43234a1e 4016 gas_assert (i.imm_operands <= 1
7ab9ffdd 4017 && (i.operands <= 2
7a8655d2 4018 || (is_any_vex_encoding (&i.tm)
7ab9ffdd 4019 && i.operands <= 4)));
65da13b5
L
4020
4021 exp = &im_expressions[i.imm_operands++];
4022 i.op[i.operands].imms = exp;
4023 i.types[i.operands] = imm8;
4024 i.operands++;
4025 exp->X_op = O_constant;
4026 exp->X_add_number = i.tm.extension_opcode;
4027 i.tm.extension_opcode = None;
4028}
4029
42164a71
L
4030
4031static int
4032check_hle (void)
4033{
742732c7 4034 switch (i.tm.opcode_modifier.prefixok)
42164a71
L
4035 {
4036 default:
4037 abort ();
742732c7
JB
4038 case PrefixLock:
4039 case PrefixNone:
4040 case PrefixNoTrack:
4041 case PrefixRep:
165de32a
L
4042 as_bad (_("invalid instruction `%s' after `%s'"),
4043 i.tm.name, i.hle_prefix);
42164a71 4044 return 0;
742732c7 4045 case PrefixHLELock:
42164a71
L
4046 if (i.prefix[LOCK_PREFIX])
4047 return 1;
165de32a 4048 as_bad (_("missing `lock' with `%s'"), i.hle_prefix);
42164a71 4049 return 0;
742732c7 4050 case PrefixHLEAny:
42164a71 4051 return 1;
742732c7 4052 case PrefixHLERelease:
42164a71
L
4053 if (i.prefix[HLE_PREFIX] != XRELEASE_PREFIX_OPCODE)
4054 {
4055 as_bad (_("instruction `%s' after `xacquire' not allowed"),
4056 i.tm.name);
4057 return 0;
4058 }
8dc0818e 4059 if (i.mem_operands == 0 || !(i.flags[i.operands - 1] & Operand_Mem))
42164a71
L
4060 {
4061 as_bad (_("memory destination needed for instruction `%s'"
4062 " after `xrelease'"), i.tm.name);
4063 return 0;
4064 }
4065 return 1;
4066 }
4067}
4068
b6f8c7c4
L
4069/* Try the shortest encoding by shortening operand size. */
4070
4071static void
4072optimize_encoding (void)
4073{
a0a1771e 4074 unsigned int j;
b6f8c7c4 4075
fe134c65
JB
4076 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
4077 && i.tm.base_opcode == 0x8d)
4078 {
4079 /* Optimize: -O:
4080 lea symbol, %rN -> mov $symbol, %rN
4081 lea (%rM), %rN -> mov %rM, %rN
4082 lea (,%rM,1), %rN -> mov %rM, %rN
4083
4084 and in 32-bit mode for 16-bit addressing
4085
4086 lea (%rM), %rN -> movzx %rM, %rN
4087
4088 and in 64-bit mode zap 32-bit addressing in favor of using a
4089 32-bit (or less) destination.
4090 */
4091 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
4092 {
4093 if (!i.op[1].regs->reg_type.bitfield.word)
4094 i.tm.opcode_modifier.size = SIZE32;
4095 i.prefix[ADDR_PREFIX] = 0;
4096 }
4097
4098 if (!i.index_reg && !i.base_reg)
4099 {
4100 /* Handle:
4101 lea symbol, %rN -> mov $symbol, %rN
4102 */
4103 if (flag_code == CODE_64BIT)
4104 {
4105 /* Don't transform a relocation to a 16-bit one. */
4106 if (i.op[0].disps
4107 && i.op[0].disps->X_op != O_constant
4108 && i.op[1].regs->reg_type.bitfield.word)
4109 return;
4110
4111 if (!i.op[1].regs->reg_type.bitfield.qword
4112 || i.tm.opcode_modifier.size == SIZE32)
4113 {
4114 i.tm.base_opcode = 0xb8;
4115 i.tm.opcode_modifier.modrm = 0;
4116 if (!i.op[1].regs->reg_type.bitfield.word)
4117 i.types[0].bitfield.imm32 = 1;
4118 else
4119 {
4120 i.tm.opcode_modifier.size = SIZE16;
4121 i.types[0].bitfield.imm16 = 1;
4122 }
4123 }
4124 else
4125 {
4126 /* Subject to further optimization below. */
4127 i.tm.base_opcode = 0xc7;
4128 i.tm.extension_opcode = 0;
4129 i.types[0].bitfield.imm32s = 1;
4130 i.types[0].bitfield.baseindex = 0;
4131 }
4132 }
4133 /* Outside of 64-bit mode address and operand sizes have to match if
4134 a relocation is involved, as otherwise we wouldn't (currently) or
4135 even couldn't express the relocation correctly. */
4136 else if (i.op[0].disps
4137 && i.op[0].disps->X_op != O_constant
4138 && ((!i.prefix[ADDR_PREFIX])
4139 != (flag_code == CODE_32BIT
4140 ? i.op[1].regs->reg_type.bitfield.dword
4141 : i.op[1].regs->reg_type.bitfield.word)))
4142 return;
4143 else
4144 {
4145 i.tm.base_opcode = 0xb8;
4146 i.tm.opcode_modifier.modrm = 0;
4147 if (i.op[1].regs->reg_type.bitfield.dword)
4148 i.types[0].bitfield.imm32 = 1;
4149 else
4150 i.types[0].bitfield.imm16 = 1;
4151
4152 if (i.op[0].disps
4153 && i.op[0].disps->X_op == O_constant
4154 && i.op[1].regs->reg_type.bitfield.dword
60cfa10c
L
4155 /* NB: Add () to !i.prefix[ADDR_PREFIX] to silence
4156 GCC 5. */
4157 && (!i.prefix[ADDR_PREFIX]) != (flag_code == CODE_32BIT))
fe134c65
JB
4158 i.op[0].disps->X_add_number &= 0xffff;
4159 }
4160
4161 i.tm.operand_types[0] = i.types[0];
4162 i.imm_operands = 1;
4163 if (!i.op[0].imms)
4164 {
4165 i.op[0].imms = &im_expressions[0];
4166 i.op[0].imms->X_op = O_absent;
4167 }
4168 }
4169 else if (i.op[0].disps
4170 && (i.op[0].disps->X_op != O_constant
4171 || i.op[0].disps->X_add_number))
4172 return;
4173 else
4174 {
4175 /* Handle:
4176 lea (%rM), %rN -> mov %rM, %rN
4177 lea (,%rM,1), %rN -> mov %rM, %rN
4178 lea (%rM), %rN -> movzx %rM, %rN
4179 */
4180 const reg_entry *addr_reg;
4181
4182 if (!i.index_reg && i.base_reg->reg_num != RegIP)
4183 addr_reg = i.base_reg;
4184 else if (!i.base_reg
4185 && i.index_reg->reg_num != RegIZ
4186 && !i.log2_scale_factor)
4187 addr_reg = i.index_reg;
4188 else
4189 return;
4190
4191 if (addr_reg->reg_type.bitfield.word
4192 && i.op[1].regs->reg_type.bitfield.dword)
4193 {
4194 if (flag_code != CODE_32BIT)
4195 return;
4196 i.tm.opcode_modifier.opcodespace = SPACE_0F;
4197 i.tm.base_opcode = 0xb7;
4198 }
4199 else
4200 i.tm.base_opcode = 0x8b;
4201
4202 if (addr_reg->reg_type.bitfield.dword
4203 && i.op[1].regs->reg_type.bitfield.qword)
4204 i.tm.opcode_modifier.size = SIZE32;
4205
4206 i.op[0].regs = addr_reg;
4207 i.reg_operands = 2;
4208 }
4209
4210 i.mem_operands = 0;
4211 i.disp_operands = 0;
4212 i.prefix[ADDR_PREFIX] = 0;
4213 i.prefix[SEG_PREFIX] = 0;
4214 i.seg[0] = NULL;
4215 }
4216
b6f8c7c4 4217 if (optimize_for_space
389d00a5 4218 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
b6f8c7c4
L
4219 && i.reg_operands == 1
4220 && i.imm_operands == 1
4221 && !i.types[1].bitfield.byte
4222 && i.op[0].imms->X_op == O_constant
4223 && fits_in_imm7 (i.op[0].imms->X_add_number)
72aea328 4224 && (i.tm.base_opcode == 0xa8
b6f8c7c4
L
4225 || (i.tm.base_opcode == 0xf6
4226 && i.tm.extension_opcode == 0x0)))
4227 {
4228 /* Optimize: -Os:
4229 test $imm7, %r64/%r32/%r16 -> test $imm7, %r8
4230 */
4231 unsigned int base_regnum = i.op[1].regs->reg_num;
4232 if (flag_code == CODE_64BIT || base_regnum < 4)
4233 {
4234 i.types[1].bitfield.byte = 1;
4235 /* Ignore the suffix. */
4236 i.suffix = 0;
7697afb6
JB
4237 /* Convert to byte registers. */
4238 if (i.types[1].bitfield.word)
4239 j = 16;
4240 else if (i.types[1].bitfield.dword)
4241 j = 32;
4242 else
4243 j = 48;
4244 if (!(i.op[1].regs->reg_flags & RegRex) && base_regnum < 4)
4245 j += 8;
4246 i.op[1].regs -= j;
b6f8c7c4
L
4247 }
4248 }
4249 else if (flag_code == CODE_64BIT
389d00a5 4250 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
d3d50934
L
4251 && ((i.types[1].bitfield.qword
4252 && i.reg_operands == 1
b6f8c7c4
L
4253 && i.imm_operands == 1
4254 && i.op[0].imms->X_op == O_constant
507916b8 4255 && ((i.tm.base_opcode == 0xb8
b6f8c7c4
L
4256 && i.tm.extension_opcode == None
4257 && fits_in_unsigned_long (i.op[0].imms->X_add_number))
4258 || (fits_in_imm31 (i.op[0].imms->X_add_number)
72aea328
JB
4259 && ((i.tm.base_opcode == 0x24
4260 || i.tm.base_opcode == 0xa8)
b6f8c7c4
L
4261 || (i.tm.base_opcode == 0x80
4262 && i.tm.extension_opcode == 0x4)
4263 || ((i.tm.base_opcode == 0xf6
507916b8 4264 || (i.tm.base_opcode | 1) == 0xc7)
b8364fa7
JB
4265 && i.tm.extension_opcode == 0x0)))
4266 || (fits_in_imm7 (i.op[0].imms->X_add_number)
4267 && i.tm.base_opcode == 0x83
4268 && i.tm.extension_opcode == 0x4)))
d3d50934
L
4269 || (i.types[0].bitfield.qword
4270 && ((i.reg_operands == 2
4271 && i.op[0].regs == i.op[1].regs
72aea328
JB
4272 && (i.tm.base_opcode == 0x30
4273 || i.tm.base_opcode == 0x28))
d3d50934
L
4274 || (i.reg_operands == 1
4275 && i.operands == 1
72aea328 4276 && i.tm.base_opcode == 0x30)))))
b6f8c7c4
L
4277 {
4278 /* Optimize: -O:
4279 andq $imm31, %r64 -> andl $imm31, %r32
b8364fa7 4280 andq $imm7, %r64 -> andl $imm7, %r32
b6f8c7c4
L
4281 testq $imm31, %r64 -> testl $imm31, %r32
4282 xorq %r64, %r64 -> xorl %r32, %r32
4283 subq %r64, %r64 -> subl %r32, %r32
4284 movq $imm31, %r64 -> movl $imm31, %r32
4285 movq $imm32, %r64 -> movl $imm32, %r32
4286 */
4287 i.tm.opcode_modifier.norex64 = 1;
507916b8 4288 if (i.tm.base_opcode == 0xb8 || (i.tm.base_opcode | 1) == 0xc7)
b6f8c7c4
L
4289 {
4290 /* Handle
4291 movq $imm31, %r64 -> movl $imm31, %r32
4292 movq $imm32, %r64 -> movl $imm32, %r32
4293 */
4294 i.tm.operand_types[0].bitfield.imm32 = 1;
4295 i.tm.operand_types[0].bitfield.imm32s = 0;
4296 i.tm.operand_types[0].bitfield.imm64 = 0;
4297 i.types[0].bitfield.imm32 = 1;
4298 i.types[0].bitfield.imm32s = 0;
4299 i.types[0].bitfield.imm64 = 0;
4300 i.types[1].bitfield.dword = 1;
4301 i.types[1].bitfield.qword = 0;
507916b8 4302 if ((i.tm.base_opcode | 1) == 0xc7)
b6f8c7c4
L
4303 {
4304 /* Handle
4305 movq $imm31, %r64 -> movl $imm31, %r32
4306 */
507916b8 4307 i.tm.base_opcode = 0xb8;
b6f8c7c4 4308 i.tm.extension_opcode = None;
507916b8 4309 i.tm.opcode_modifier.w = 0;
b6f8c7c4
L
4310 i.tm.opcode_modifier.modrm = 0;
4311 }
4312 }
4313 }
5641ec01
JB
4314 else if (optimize > 1
4315 && !optimize_for_space
389d00a5 4316 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
5641ec01
JB
4317 && i.reg_operands == 2
4318 && i.op[0].regs == i.op[1].regs
4319 && ((i.tm.base_opcode & ~(Opcode_D | 1)) == 0x8
4320 || (i.tm.base_opcode & ~(Opcode_D | 1)) == 0x20)
4321 && (flag_code != CODE_64BIT || !i.types[0].bitfield.dword))
4322 {
4323 /* Optimize: -O2:
4324 andb %rN, %rN -> testb %rN, %rN
4325 andw %rN, %rN -> testw %rN, %rN
4326 andq %rN, %rN -> testq %rN, %rN
4327 orb %rN, %rN -> testb %rN, %rN
4328 orw %rN, %rN -> testw %rN, %rN
4329 orq %rN, %rN -> testq %rN, %rN
4330
4331 and outside of 64-bit mode
4332
4333 andl %rN, %rN -> testl %rN, %rN
4334 orl %rN, %rN -> testl %rN, %rN
4335 */
4336 i.tm.base_opcode = 0x84 | (i.tm.base_opcode & 1);
4337 }
99112332 4338 else if (i.reg_operands == 3
b6f8c7c4
L
4339 && i.op[0].regs == i.op[1].regs
4340 && !i.types[2].bitfield.xmmword
4341 && (i.tm.opcode_modifier.vex
6225c532 4342 || ((!i.mask.reg || i.mask.zeroing)
ca5312a2 4343 && i.rounding.type == rc_none
e771e7c9 4344 && is_evex_encoding (&i.tm)
80c34c38 4345 && (i.vec_encoding != vex_encoding_evex
dd22218c 4346 || cpu_arch_isa_flags.bitfield.cpuavx512vl
80c34c38 4347 || i.tm.cpu_flags.bitfield.cpuavx512vl
7091c612 4348 || (i.tm.operand_types[2].bitfield.zmmword
dd22218c 4349 && i.types[2].bitfield.ymmword))))
b6f8c7c4 4350 && ((i.tm.base_opcode == 0x55
b6f8c7c4 4351 || i.tm.base_opcode == 0x57
35648716
JB
4352 || i.tm.base_opcode == 0xdf
4353 || i.tm.base_opcode == 0xef
4354 || i.tm.base_opcode == 0xf8
4355 || i.tm.base_opcode == 0xf9
4356 || i.tm.base_opcode == 0xfa
4357 || i.tm.base_opcode == 0xfb
1424ad86 4358 || i.tm.base_opcode == 0x42
35648716 4359 || i.tm.base_opcode == 0x47)
b6f8c7c4
L
4360 && i.tm.extension_opcode == None))
4361 {
99112332 4362 /* Optimize: -O1:
8305403a
L
4363 VOP, one of vandnps, vandnpd, vxorps, vxorpd, vpsubb, vpsubd,
4364 vpsubq and vpsubw:
b6f8c7c4
L
4365 EVEX VOP %zmmM, %zmmM, %zmmN
4366 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4367 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4368 EVEX VOP %ymmM, %ymmM, %ymmN
4369 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4370 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4371 VEX VOP %ymmM, %ymmM, %ymmN
4372 -> VEX VOP %xmmM, %xmmM, %xmmN
4373 VOP, one of vpandn and vpxor:
4374 VEX VOP %ymmM, %ymmM, %ymmN
4375 -> VEX VOP %xmmM, %xmmM, %xmmN
4376 VOP, one of vpandnd and vpandnq:
4377 EVEX VOP %zmmM, %zmmM, %zmmN
4378 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4379 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4380 EVEX VOP %ymmM, %ymmM, %ymmN
4381 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4382 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4383 VOP, one of vpxord and vpxorq:
4384 EVEX VOP %zmmM, %zmmM, %zmmN
4385 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4386 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4387 EVEX VOP %ymmM, %ymmM, %ymmN
4388 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4389 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
1424ad86
JB
4390 VOP, one of kxord and kxorq:
4391 VEX VOP %kM, %kM, %kN
4392 -> VEX kxorw %kM, %kM, %kN
4393 VOP, one of kandnd and kandnq:
4394 VEX VOP %kM, %kM, %kN
4395 -> VEX kandnw %kM, %kM, %kN
b6f8c7c4 4396 */
e771e7c9 4397 if (is_evex_encoding (&i.tm))
b6f8c7c4 4398 {
7b1d7ca1 4399 if (i.vec_encoding != vex_encoding_evex)
b6f8c7c4
L
4400 {
4401 i.tm.opcode_modifier.vex = VEX128;
4402 i.tm.opcode_modifier.vexw = VEXW0;
4403 i.tm.opcode_modifier.evex = 0;
4404 }
7b1d7ca1 4405 else if (optimize > 1)
dd22218c
L
4406 i.tm.opcode_modifier.evex = EVEX128;
4407 else
4408 return;
b6f8c7c4 4409 }
f74a6307 4410 else if (i.tm.operand_types[0].bitfield.class == RegMask)
1424ad86 4411 {
35648716 4412 i.tm.opcode_modifier.opcodeprefix = PREFIX_NONE;
1424ad86
JB
4413 i.tm.opcode_modifier.vexw = VEXW0;
4414 }
b6f8c7c4
L
4415 else
4416 i.tm.opcode_modifier.vex = VEX128;
4417
4418 if (i.tm.opcode_modifier.vex)
4419 for (j = 0; j < 3; j++)
4420 {
4421 i.types[j].bitfield.xmmword = 1;
4422 i.types[j].bitfield.ymmword = 0;
4423 }
4424 }
392a5972 4425 else if (i.vec_encoding != vex_encoding_evex
97ed31ae 4426 && !i.types[0].bitfield.zmmword
392a5972 4427 && !i.types[1].bitfield.zmmword
6225c532 4428 && !i.mask.reg
5273a3cd 4429 && !i.broadcast.type
97ed31ae 4430 && is_evex_encoding (&i.tm)
35648716
JB
4431 && ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4432 || (i.tm.base_opcode & ~4) == 0xdb
4433 || (i.tm.base_opcode & ~4) == 0xeb)
97ed31ae
L
4434 && i.tm.extension_opcode == None)
4435 {
4436 /* Optimize: -O1:
4437 VOP, one of vmovdqa32, vmovdqa64, vmovdqu8, vmovdqu16,
4438 vmovdqu32 and vmovdqu64:
4439 EVEX VOP %xmmM, %xmmN
4440 -> VEX vmovdqa|vmovdqu %xmmM, %xmmN (M and N < 16)
4441 EVEX VOP %ymmM, %ymmN
4442 -> VEX vmovdqa|vmovdqu %ymmM, %ymmN (M and N < 16)
4443 EVEX VOP %xmmM, mem
4444 -> VEX vmovdqa|vmovdqu %xmmM, mem (M < 16)
4445 EVEX VOP %ymmM, mem
4446 -> VEX vmovdqa|vmovdqu %ymmM, mem (M < 16)
4447 EVEX VOP mem, %xmmN
4448 -> VEX mvmovdqa|vmovdquem, %xmmN (N < 16)
4449 EVEX VOP mem, %ymmN
4450 -> VEX vmovdqa|vmovdqu mem, %ymmN (N < 16)
a0a1771e
JB
4451 VOP, one of vpand, vpandn, vpor, vpxor:
4452 EVEX VOP{d,q} %xmmL, %xmmM, %xmmN
4453 -> VEX VOP %xmmL, %xmmM, %xmmN (L, M, and N < 16)
4454 EVEX VOP{d,q} %ymmL, %ymmM, %ymmN
4455 -> VEX VOP %ymmL, %ymmM, %ymmN (L, M, and N < 16)
4456 EVEX VOP{d,q} mem, %xmmM, %xmmN
4457 -> VEX VOP mem, %xmmM, %xmmN (M and N < 16)
4458 EVEX VOP{d,q} mem, %ymmM, %ymmN
4459 -> VEX VOP mem, %ymmM, %ymmN (M and N < 16)
97ed31ae 4460 */
a0a1771e 4461 for (j = 0; j < i.operands; j++)
392a5972
L
4462 if (operand_type_check (i.types[j], disp)
4463 && i.op[j].disps->X_op == O_constant)
4464 {
4465 /* Since the VEX prefix has 2 or 3 bytes, the EVEX prefix
4466 has 4 bytes, EVEX Disp8 has 1 byte and VEX Disp32 has 4
4467 bytes, we choose EVEX Disp8 over VEX Disp32. */
4468 int evex_disp8, vex_disp8;
4469 unsigned int memshift = i.memshift;
4470 offsetT n = i.op[j].disps->X_add_number;
4471
4472 evex_disp8 = fits_in_disp8 (n);
4473 i.memshift = 0;
4474 vex_disp8 = fits_in_disp8 (n);
4475 if (evex_disp8 != vex_disp8)
4476 {
4477 i.memshift = memshift;
4478 return;
4479 }
4480
4481 i.types[j].bitfield.disp8 = vex_disp8;
4482 break;
4483 }
35648716
JB
4484 if ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4485 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2)
4486 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
97ed31ae
L
4487 i.tm.opcode_modifier.vex
4488 = i.types[0].bitfield.ymmword ? VEX256 : VEX128;
4489 i.tm.opcode_modifier.vexw = VEXW0;
79dec6b7 4490 /* VPAND, VPOR, and VPXOR are commutative. */
35648716 4491 if (i.reg_operands == 3 && i.tm.base_opcode != 0xdf)
79dec6b7 4492 i.tm.opcode_modifier.commutative = 1;
97ed31ae
L
4493 i.tm.opcode_modifier.evex = 0;
4494 i.tm.opcode_modifier.masking = 0;
a0a1771e 4495 i.tm.opcode_modifier.broadcast = 0;
97ed31ae
L
4496 i.tm.opcode_modifier.disp8memshift = 0;
4497 i.memshift = 0;
a0a1771e
JB
4498 if (j < i.operands)
4499 i.types[j].bitfield.disp8
4500 = fits_in_disp8 (i.op[j].disps->X_add_number);
97ed31ae 4501 }
b6f8c7c4
L
4502}
4503
ae531041
L
4504/* Return non-zero for load instruction. */
4505
4506static int
4507load_insn_p (void)
4508{
4509 unsigned int dest;
4510 int any_vex_p = is_any_vex_encoding (&i.tm);
4511 unsigned int base_opcode = i.tm.base_opcode | 1;
4512
4513 if (!any_vex_p)
4514 {
a09f656b 4515 /* Anysize insns: lea, invlpg, clflush, prefetchnta, prefetcht0,
4516 prefetcht1, prefetcht2, prefetchtw, bndmk, bndcl, bndcu, bndcn,
4517 bndstx, bndldx, prefetchwt1, clflushopt, clwb, cldemote. */
4518 if (i.tm.opcode_modifier.anysize)
ae531041
L
4519 return 0;
4520
389d00a5
JB
4521 /* pop. */
4522 if (strcmp (i.tm.name, "pop") == 0)
4523 return 1;
4524 }
4525
4526 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE)
4527 {
4528 /* popf, popa. */
4529 if (i.tm.base_opcode == 0x9d
a09f656b 4530 || i.tm.base_opcode == 0x61)
ae531041
L
4531 return 1;
4532
4533 /* movs, cmps, lods, scas. */
4534 if ((i.tm.base_opcode | 0xb) == 0xaf)
4535 return 1;
4536
a09f656b 4537 /* outs, xlatb. */
4538 if (base_opcode == 0x6f
4539 || i.tm.base_opcode == 0xd7)
ae531041 4540 return 1;
a09f656b 4541 /* NB: For AMD-specific insns with implicit memory operands,
4542 they're intentionally not covered. */
ae531041
L
4543 }
4544
4545 /* No memory operand. */
4546 if (!i.mem_operands)
4547 return 0;
4548
4549 if (any_vex_p)
4550 {
4551 /* vldmxcsr. */
4552 if (i.tm.base_opcode == 0xae
4553 && i.tm.opcode_modifier.vex
441f6aca 4554 && i.tm.opcode_modifier.opcodespace == SPACE_0F
35648716 4555 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
ae531041
L
4556 && i.tm.extension_opcode == 2)
4557 return 1;
4558 }
389d00a5 4559 else if (i.tm.opcode_modifier.opcodespace == SPACE_BASE)
ae531041
L
4560 {
4561 /* test, not, neg, mul, imul, div, idiv. */
4562 if ((i.tm.base_opcode == 0xf6 || i.tm.base_opcode == 0xf7)
4563 && i.tm.extension_opcode != 1)
4564 return 1;
4565
4566 /* inc, dec. */
4567 if (base_opcode == 0xff && i.tm.extension_opcode <= 1)
4568 return 1;
4569
4570 /* add, or, adc, sbb, and, sub, xor, cmp. */
4571 if (i.tm.base_opcode >= 0x80 && i.tm.base_opcode <= 0x83)
4572 return 1;
4573
ae531041
L
4574 /* rol, ror, rcl, rcr, shl/sal, shr, sar. */
4575 if ((base_opcode == 0xc1
4576 || (i.tm.base_opcode >= 0xd0 && i.tm.base_opcode <= 0xd3))
4577 && i.tm.extension_opcode != 6)
4578 return 1;
4579
ae531041 4580 /* Check for x87 instructions. */
389d00a5 4581 if (base_opcode >= 0xd8 && base_opcode <= 0xdf)
ae531041
L
4582 {
4583 /* Skip fst, fstp, fstenv, fstcw. */
4584 if (i.tm.base_opcode == 0xd9
4585 && (i.tm.extension_opcode == 2
4586 || i.tm.extension_opcode == 3
4587 || i.tm.extension_opcode == 6
4588 || i.tm.extension_opcode == 7))
4589 return 0;
4590
4591 /* Skip fisttp, fist, fistp, fstp. */
4592 if (i.tm.base_opcode == 0xdb
4593 && (i.tm.extension_opcode == 1
4594 || i.tm.extension_opcode == 2
4595 || i.tm.extension_opcode == 3
4596 || i.tm.extension_opcode == 7))
4597 return 0;
4598
4599 /* Skip fisttp, fst, fstp, fsave, fstsw. */
4600 if (i.tm.base_opcode == 0xdd
4601 && (i.tm.extension_opcode == 1
4602 || i.tm.extension_opcode == 2
4603 || i.tm.extension_opcode == 3
4604 || i.tm.extension_opcode == 6
4605 || i.tm.extension_opcode == 7))
4606 return 0;
4607
4608 /* Skip fisttp, fist, fistp, fbstp, fistp. */
4609 if (i.tm.base_opcode == 0xdf
4610 && (i.tm.extension_opcode == 1
4611 || i.tm.extension_opcode == 2
4612 || i.tm.extension_opcode == 3
4613 || i.tm.extension_opcode == 6
4614 || i.tm.extension_opcode == 7))
4615 return 0;
4616
4617 return 1;
4618 }
4619 }
389d00a5
JB
4620 else if (i.tm.opcode_modifier.opcodespace == SPACE_0F)
4621 {
4622 /* bt, bts, btr, btc. */
4623 if (i.tm.base_opcode == 0xba
4624 && (i.tm.extension_opcode >= 4 && i.tm.extension_opcode <= 7))
4625 return 1;
4626
4627 /* cmpxchg8b, cmpxchg16b, xrstors, vmptrld. */
4628 if (i.tm.base_opcode == 0xc7
4629 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
4630 && (i.tm.extension_opcode == 1 || i.tm.extension_opcode == 3
4631 || i.tm.extension_opcode == 6))
4632 return 1;
4633
4634 /* fxrstor, ldmxcsr, xrstor. */
4635 if (i.tm.base_opcode == 0xae
4636 && (i.tm.extension_opcode == 1
4637 || i.tm.extension_opcode == 2
4638 || i.tm.extension_opcode == 5))
4639 return 1;
4640
4641 /* lgdt, lidt, lmsw. */
4642 if (i.tm.base_opcode == 0x01
4643 && (i.tm.extension_opcode == 2
4644 || i.tm.extension_opcode == 3
4645 || i.tm.extension_opcode == 6))
4646 return 1;
4647 }
ae531041
L
4648
4649 dest = i.operands - 1;
4650
4651 /* Check fake imm8 operand and 3 source operands. */
4652 if ((i.tm.opcode_modifier.immext
4653 || i.tm.opcode_modifier.vexsources == VEX3SOURCES)
4654 && i.types[dest].bitfield.imm8)
4655 dest--;
4656
389d00a5
JB
4657 /* add, or, adc, sbb, and, sub, xor, cmp, test, xchg. */
4658 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
ae531041
L
4659 && (base_opcode == 0x1
4660 || base_opcode == 0x9
4661 || base_opcode == 0x11
4662 || base_opcode == 0x19
4663 || base_opcode == 0x21
4664 || base_opcode == 0x29
4665 || base_opcode == 0x31
4666 || base_opcode == 0x39
389d00a5
JB
4667 || (base_opcode | 2) == 0x87))
4668 return 1;
4669
4670 /* xadd. */
4671 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4672 && base_opcode == 0xc1)
ae531041
L
4673 return 1;
4674
4675 /* Check for load instruction. */
4676 return (i.types[dest].bitfield.class != ClassNone
4677 || i.types[dest].bitfield.instance == Accum);
4678}
4679
4680/* Output lfence, 0xfaee8, after instruction. */
4681
4682static void
4683insert_lfence_after (void)
4684{
4685 if (lfence_after_load && load_insn_p ())
4686 {
a09f656b 4687 /* There are also two REP string instructions that require
4688 special treatment. Specifically, the compare string (CMPS)
4689 and scan string (SCAS) instructions set EFLAGS in a manner
4690 that depends on the data being compared/scanned. When used
4691 with a REP prefix, the number of iterations may therefore
4692 vary depending on this data. If the data is a program secret
4693 chosen by the adversary using an LVI method,
4694 then this data-dependent behavior may leak some aspect
4695 of the secret. */
4696 if (((i.tm.base_opcode | 0x1) == 0xa7
4697 || (i.tm.base_opcode | 0x1) == 0xaf)
4698 && i.prefix[REP_PREFIX])
4699 {
4700 as_warn (_("`%s` changes flags which would affect control flow behavior"),
4701 i.tm.name);
4702 }
ae531041
L
4703 char *p = frag_more (3);
4704 *p++ = 0xf;
4705 *p++ = 0xae;
4706 *p = 0xe8;
4707 }
4708}
4709
4710/* Output lfence, 0xfaee8, before instruction. */
4711
4712static void
4713insert_lfence_before (void)
4714{
4715 char *p;
4716
389d00a5 4717 if (i.tm.opcode_modifier.opcodespace != SPACE_BASE)
ae531041
L
4718 return;
4719
4720 if (i.tm.base_opcode == 0xff
4721 && (i.tm.extension_opcode == 2 || i.tm.extension_opcode == 4))
4722 {
4723 /* Insert lfence before indirect branch if needed. */
4724
4725 if (lfence_before_indirect_branch == lfence_branch_none)
4726 return;
4727
4728 if (i.operands != 1)
4729 abort ();
4730
4731 if (i.reg_operands == 1)
4732 {
4733 /* Indirect branch via register. Don't insert lfence with
4734 -mlfence-after-load=yes. */
4735 if (lfence_after_load
4736 || lfence_before_indirect_branch == lfence_branch_memory)
4737 return;
4738 }
4739 else if (i.mem_operands == 1
4740 && lfence_before_indirect_branch != lfence_branch_register)
4741 {
4742 as_warn (_("indirect `%s` with memory operand should be avoided"),
4743 i.tm.name);
4744 return;
4745 }
4746 else
4747 return;
4748
4749 if (last_insn.kind != last_insn_other
4750 && last_insn.seg == now_seg)
4751 {
4752 as_warn_where (last_insn.file, last_insn.line,
4753 _("`%s` skips -mlfence-before-indirect-branch on `%s`"),
4754 last_insn.name, i.tm.name);
4755 return;
4756 }
4757
4758 p = frag_more (3);
4759 *p++ = 0xf;
4760 *p++ = 0xae;
4761 *p = 0xe8;
4762 return;
4763 }
4764
503648e4 4765 /* Output or/not/shl and lfence before near ret. */
ae531041
L
4766 if (lfence_before_ret != lfence_before_ret_none
4767 && (i.tm.base_opcode == 0xc2
503648e4 4768 || i.tm.base_opcode == 0xc3))
ae531041
L
4769 {
4770 if (last_insn.kind != last_insn_other
4771 && last_insn.seg == now_seg)
4772 {
4773 as_warn_where (last_insn.file, last_insn.line,
4774 _("`%s` skips -mlfence-before-ret on `%s`"),
4775 last_insn.name, i.tm.name);
4776 return;
4777 }
a09f656b 4778
a09f656b 4779 /* Near ret ingore operand size override under CPU64. */
503648e4 4780 char prefix = flag_code == CODE_64BIT
4781 ? 0x48
4782 : i.prefix[DATA_PREFIX] ? 0x66 : 0x0;
a09f656b 4783
4784 if (lfence_before_ret == lfence_before_ret_not)
4785 {
4786 /* not: 0xf71424, may add prefix
4787 for operand size override or 64-bit code. */
4788 p = frag_more ((prefix ? 2 : 0) + 6 + 3);
4789 if (prefix)
4790 *p++ = prefix;
ae531041
L
4791 *p++ = 0xf7;
4792 *p++ = 0x14;
4793 *p++ = 0x24;
a09f656b 4794 if (prefix)
4795 *p++ = prefix;
ae531041
L
4796 *p++ = 0xf7;
4797 *p++ = 0x14;
4798 *p++ = 0x24;
4799 }
a09f656b 4800 else
4801 {
4802 p = frag_more ((prefix ? 1 : 0) + 4 + 3);
4803 if (prefix)
4804 *p++ = prefix;
4805 if (lfence_before_ret == lfence_before_ret_or)
4806 {
4807 /* or: 0x830c2400, may add prefix
4808 for operand size override or 64-bit code. */
4809 *p++ = 0x83;
4810 *p++ = 0x0c;
4811 }
4812 else
4813 {
4814 /* shl: 0xc1242400, may add prefix
4815 for operand size override or 64-bit code. */
4816 *p++ = 0xc1;
4817 *p++ = 0x24;
4818 }
4819
4820 *p++ = 0x24;
4821 *p++ = 0x0;
4822 }
4823
ae531041
L
4824 *p++ = 0xf;
4825 *p++ = 0xae;
4826 *p = 0xe8;
4827 }
4828}
4829
252b5132
RH
4830/* This is the guts of the machine-dependent assembler. LINE points to a
4831 machine dependent instruction. This function is supposed to emit
4832 the frags/bytes it assembles to. */
4833
4834void
65da13b5 4835md_assemble (char *line)
252b5132 4836{
40fb9820 4837 unsigned int j;
83b16ac6 4838 char mnemonic[MAX_MNEM_SIZE], mnem_suffix;
d3ce72d0 4839 const insn_template *t;
252b5132 4840
47926f60 4841 /* Initialize globals. */
252b5132 4842 memset (&i, '\0', sizeof (i));
ca5312a2 4843 i.rounding.type = rc_none;
252b5132 4844 for (j = 0; j < MAX_OPERANDS; j++)
1ae12ab7 4845 i.reloc[j] = NO_RELOC;
252b5132
RH
4846 memset (disp_expressions, '\0', sizeof (disp_expressions));
4847 memset (im_expressions, '\0', sizeof (im_expressions));
ce8a8b2f 4848 save_stack_p = save_stack;
252b5132
RH
4849
4850 /* First parse an instruction mnemonic & call i386_operand for the operands.
4851 We assume that the scrubber has arranged it so that line[0] is the valid
47926f60 4852 start of a (possibly prefixed) mnemonic. */
252b5132 4853
29b0f896
AM
4854 line = parse_insn (line, mnemonic);
4855 if (line == NULL)
4856 return;
83b16ac6 4857 mnem_suffix = i.suffix;
252b5132 4858
29b0f896 4859 line = parse_operands (line, mnemonic);
ee86248c 4860 this_operand = -1;
8325cc63
JB
4861 xfree (i.memop1_string);
4862 i.memop1_string = NULL;
29b0f896
AM
4863 if (line == NULL)
4864 return;
252b5132 4865
29b0f896
AM
4866 /* Now we've parsed the mnemonic into a set of templates, and have the
4867 operands at hand. */
4868
b630c145 4869 /* All Intel opcodes have reversed operands except for "bound", "enter",
c0e54661
JB
4870 "invlpg*", "monitor*", "mwait*", "tpause", "umwait", "pvalidate",
4871 "rmpadjust", and "rmpupdate". We also don't reverse intersegment "jmp"
4872 and "call" instructions with 2 immediate operands so that the immediate
4873 segment precedes the offset consistently in Intel and AT&T modes. */
4d456e3d
L
4874 if (intel_syntax
4875 && i.operands > 1
29b0f896 4876 && (strcmp (mnemonic, "bound") != 0)
c0e54661 4877 && (strncmp (mnemonic, "invlpg", 6) != 0)
d34049e8
ML
4878 && !startswith (mnemonic, "monitor")
4879 && !startswith (mnemonic, "mwait")
c0e54661 4880 && (strcmp (mnemonic, "pvalidate") != 0)
d34049e8 4881 && !startswith (mnemonic, "rmp")
b630c145
JB
4882 && (strcmp (mnemonic, "tpause") != 0)
4883 && (strcmp (mnemonic, "umwait") != 0)
40fb9820
L
4884 && !(operand_type_check (i.types[0], imm)
4885 && operand_type_check (i.types[1], imm)))
29b0f896
AM
4886 swap_operands ();
4887
ec56d5c0
JB
4888 /* The order of the immediates should be reversed
4889 for 2 immediates extrq and insertq instructions */
4890 if (i.imm_operands == 2
4891 && (strcmp (mnemonic, "extrq") == 0
4892 || strcmp (mnemonic, "insertq") == 0))
4893 swap_2_operands (0, 1);
4894
29b0f896
AM
4895 if (i.imm_operands)
4896 optimize_imm ();
4897
a9aabc23 4898 if (i.disp_operands && !want_disp32 (current_templates->start))
cce08655
JB
4899 {
4900 for (j = 0; j < i.operands; ++j)
4901 {
4902 const expressionS *exp = i.op[j].disps;
4903
4904 if (!operand_type_check (i.types[j], disp))
4905 continue;
4906
4907 if (exp->X_op != O_constant)
4908 continue;
4909
4910 /* Since displacement is signed extended to 64bit, don't allow
4911 disp32 and turn off disp32s if they are out of range. */
4912 i.types[j].bitfield.disp32 = 0;
4913 if (fits_in_signed_long (exp->X_add_number))
4914 continue;
4915
4916 i.types[j].bitfield.disp32s = 0;
4917 if (i.types[j].bitfield.baseindex)
4918 {
4919 as_bad (_("0x%" BFD_VMA_FMT "x out of range of signed 32bit displacement"),
4920 exp->X_add_number);
4921 return;
4922 }
4923 }
4924 }
4925
b300c311
L
4926 /* Don't optimize displacement for movabs since it only takes 64bit
4927 displacement. */
4928 if (i.disp_operands
a501d77e 4929 && i.disp_encoding != disp_encoding_32bit
862be3fb
L
4930 && (flag_code != CODE_64BIT
4931 || strcmp (mnemonic, "movabs") != 0))
4932 optimize_disp ();
29b0f896
AM
4933
4934 /* Next, we find a template that matches the given insn,
4935 making sure the overlap of the given operands types is consistent
4936 with the template operand types. */
252b5132 4937
83b16ac6 4938 if (!(t = match_template (mnem_suffix)))
29b0f896 4939 return;
252b5132 4940
7bab8ab5 4941 if (sse_check != check_none
81f8a913 4942 && !i.tm.opcode_modifier.noavx
6e3e5c9e 4943 && !i.tm.cpu_flags.bitfield.cpuavx
569d50f1 4944 && !i.tm.cpu_flags.bitfield.cpuavx512f
daf50ae7
L
4945 && (i.tm.cpu_flags.bitfield.cpusse
4946 || i.tm.cpu_flags.bitfield.cpusse2
4947 || i.tm.cpu_flags.bitfield.cpusse3
4948 || i.tm.cpu_flags.bitfield.cpussse3
4949 || i.tm.cpu_flags.bitfield.cpusse4_1
6e3e5c9e
JB
4950 || i.tm.cpu_flags.bitfield.cpusse4_2
4951 || i.tm.cpu_flags.bitfield.cpupclmul
4952 || i.tm.cpu_flags.bitfield.cpuaes
569d50f1 4953 || i.tm.cpu_flags.bitfield.cpusha
6e3e5c9e 4954 || i.tm.cpu_flags.bitfield.cpugfni))
daf50ae7 4955 {
7bab8ab5 4956 (sse_check == check_warning
daf50ae7
L
4957 ? as_warn
4958 : as_bad) (_("SSE instruction `%s' is used"), i.tm.name);
4959 }
4960
40fb9820 4961 if (i.tm.opcode_modifier.fwait)
29b0f896
AM
4962 if (!add_prefix (FWAIT_OPCODE))
4963 return;
252b5132 4964
d5de92cf 4965 /* Check if REP prefix is OK. */
742732c7 4966 if (i.rep_prefix && i.tm.opcode_modifier.prefixok != PrefixRep)
d5de92cf
L
4967 {
4968 as_bad (_("invalid instruction `%s' after `%s'"),
4969 i.tm.name, i.rep_prefix);
4970 return;
4971 }
4972
c1ba0266
L
4973 /* Check for lock without a lockable instruction. Destination operand
4974 must be memory unless it is xchg (0x86). */
c32fa91d 4975 if (i.prefix[LOCK_PREFIX]
742732c7 4976 && (i.tm.opcode_modifier.prefixok < PrefixLock
c1ba0266
L
4977 || i.mem_operands == 0
4978 || (i.tm.base_opcode != 0x86
8dc0818e 4979 && !(i.flags[i.operands - 1] & Operand_Mem))))
c32fa91d
L
4980 {
4981 as_bad (_("expecting lockable instruction after `lock'"));
4982 return;
4983 }
4984
40d231b4
JB
4985 /* Check for data size prefix on VEX/XOP/EVEX encoded and SIMD insns. */
4986 if (i.prefix[DATA_PREFIX]
4987 && (is_any_vex_encoding (&i.tm)
4988 || i.tm.operand_types[i.imm_operands].bitfield.class >= RegMMX
4989 || i.tm.operand_types[i.imm_operands + 1].bitfield.class >= RegMMX))
7a8655d2
JB
4990 {
4991 as_bad (_("data size prefix invalid with `%s'"), i.tm.name);
4992 return;
4993 }
4994
42164a71 4995 /* Check if HLE prefix is OK. */
165de32a 4996 if (i.hle_prefix && !check_hle ())
42164a71
L
4997 return;
4998
7e8b059b
L
4999 /* Check BND prefix. */
5000 if (i.bnd_prefix && !i.tm.opcode_modifier.bndprefixok)
5001 as_bad (_("expecting valid branch instruction after `bnd'"));
5002
04ef582a 5003 /* Check NOTRACK prefix. */
742732c7 5004 if (i.notrack_prefix && i.tm.opcode_modifier.prefixok != PrefixNoTrack)
9fef80d6 5005 as_bad (_("expecting indirect branch instruction after `notrack'"));
04ef582a 5006
327e8c42
JB
5007 if (i.tm.cpu_flags.bitfield.cpumpx)
5008 {
5009 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
5010 as_bad (_("32-bit address isn't allowed in 64-bit MPX instructions."));
5011 else if (flag_code != CODE_16BIT
5012 ? i.prefix[ADDR_PREFIX]
5013 : i.mem_operands && !i.prefix[ADDR_PREFIX])
5014 as_bad (_("16-bit address isn't allowed in MPX instructions"));
5015 }
7e8b059b
L
5016
5017 /* Insert BND prefix. */
76d3a78a
JB
5018 if (add_bnd_prefix && i.tm.opcode_modifier.bndprefixok)
5019 {
5020 if (!i.prefix[BND_PREFIX])
5021 add_prefix (BND_PREFIX_OPCODE);
5022 else if (i.prefix[BND_PREFIX] != BND_PREFIX_OPCODE)
5023 {
5024 as_warn (_("replacing `rep'/`repe' prefix by `bnd'"));
5025 i.prefix[BND_PREFIX] = BND_PREFIX_OPCODE;
5026 }
5027 }
7e8b059b 5028
29b0f896 5029 /* Check string instruction segment overrides. */
51c8edf6 5030 if (i.tm.opcode_modifier.isstring >= IS_STRING_ES_OP0)
29b0f896 5031 {
51c8edf6 5032 gas_assert (i.mem_operands);
29b0f896 5033 if (!check_string ())
5dd0794d 5034 return;
fc0763e6 5035 i.disp_operands = 0;
29b0f896 5036 }
5dd0794d 5037
b6f8c7c4
L
5038 if (optimize && !i.no_optimize && i.tm.opcode_modifier.optimize)
5039 optimize_encoding ();
5040
29b0f896
AM
5041 if (!process_suffix ())
5042 return;
e413e4e9 5043
921eafea 5044 /* Update operand types and check extended states. */
bc0844ae 5045 for (j = 0; j < i.operands; j++)
921eafea
L
5046 {
5047 i.types[j] = operand_type_and (i.types[j], i.tm.operand_types[j]);
3d70986f 5048 switch (i.tm.operand_types[j].bitfield.class)
921eafea
L
5049 {
5050 default:
5051 break;
5052 case RegMMX:
5053 i.xstate |= xstate_mmx;
5054 break;
5055 case RegMask:
32930e4e 5056 i.xstate |= xstate_mask;
921eafea
L
5057 break;
5058 case RegSIMD:
3d70986f 5059 if (i.tm.operand_types[j].bitfield.tmmword)
921eafea 5060 i.xstate |= xstate_tmm;
3d70986f 5061 else if (i.tm.operand_types[j].bitfield.zmmword)
921eafea 5062 i.xstate |= xstate_zmm;
3d70986f 5063 else if (i.tm.operand_types[j].bitfield.ymmword)
921eafea 5064 i.xstate |= xstate_ymm;
3d70986f 5065 else if (i.tm.operand_types[j].bitfield.xmmword)
921eafea
L
5066 i.xstate |= xstate_xmm;
5067 break;
5068 }
5069 }
bc0844ae 5070
29b0f896
AM
5071 /* Make still unresolved immediate matches conform to size of immediate
5072 given in i.suffix. */
5073 if (!finalize_imm ())
5074 return;
252b5132 5075
40fb9820 5076 if (i.types[0].bitfield.imm1)
29b0f896 5077 i.imm_operands = 0; /* kludge for shift insns. */
252b5132 5078
9afe6eb8
L
5079 /* We only need to check those implicit registers for instructions
5080 with 3 operands or less. */
5081 if (i.operands <= 3)
5082 for (j = 0; j < i.operands; j++)
75e5731b
JB
5083 if (i.types[j].bitfield.instance != InstanceNone
5084 && !i.types[j].bitfield.xmmword)
9afe6eb8 5085 i.reg_operands--;
40fb9820 5086
29b0f896
AM
5087 /* For insns with operands there are more diddles to do to the opcode. */
5088 if (i.operands)
5089 {
5090 if (!process_operands ())
5091 return;
5092 }
8c190ce0 5093 else if (!quiet_warnings && i.tm.opcode_modifier.ugh)
29b0f896
AM
5094 {
5095 /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc. */
5096 as_warn (_("translating to `%sp'"), i.tm.name);
5097 }
252b5132 5098
7a8655d2 5099 if (is_any_vex_encoding (&i.tm))
9e5e5283 5100 {
c1dc7af5 5101 if (!cpu_arch_flags.bitfield.cpui286)
9e5e5283 5102 {
c1dc7af5 5103 as_bad (_("instruction `%s' isn't supported outside of protected mode."),
9e5e5283
L
5104 i.tm.name);
5105 return;
5106 }
c0f3af97 5107
0b9404fd
JB
5108 /* Check for explicit REX prefix. */
5109 if (i.prefix[REX_PREFIX] || i.rex_encoding)
5110 {
5111 as_bad (_("REX prefix invalid with `%s'"), i.tm.name);
5112 return;
5113 }
5114
9e5e5283
L
5115 if (i.tm.opcode_modifier.vex)
5116 build_vex_prefix (t);
5117 else
5118 build_evex_prefix ();
0b9404fd
JB
5119
5120 /* The individual REX.RXBW bits got consumed. */
5121 i.rex &= REX_OPCODE;
9e5e5283 5122 }
43234a1e 5123
5dd85c99
SP
5124 /* Handle conversion of 'int $3' --> special int3 insn. XOP or FMA4
5125 instructions may define INT_OPCODE as well, so avoid this corner
5126 case for those instructions that use MODRM. */
389d00a5
JB
5127 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
5128 && i.tm.base_opcode == INT_OPCODE
a6461c02
SP
5129 && !i.tm.opcode_modifier.modrm
5130 && i.op[0].imms->X_add_number == 3)
29b0f896
AM
5131 {
5132 i.tm.base_opcode = INT3_OPCODE;
5133 i.imm_operands = 0;
5134 }
252b5132 5135
0cfa3eb3
JB
5136 if ((i.tm.opcode_modifier.jump == JUMP
5137 || i.tm.opcode_modifier.jump == JUMP_BYTE
5138 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896
AM
5139 && i.op[0].disps->X_op == O_constant)
5140 {
5141 /* Convert "jmp constant" (and "call constant") to a jump (call) to
5142 the absolute address given by the constant. Since ix86 jumps and
5143 calls are pc relative, we need to generate a reloc. */
5144 i.op[0].disps->X_add_symbol = &abs_symbol;
5145 i.op[0].disps->X_op = O_symbol;
5146 }
252b5132 5147
29b0f896
AM
5148 /* For 8 bit registers we need an empty rex prefix. Also if the
5149 instruction already has a prefix, we need to convert old
5150 registers to new ones. */
773f551c 5151
bab6aec1 5152 if ((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte
29b0f896 5153 && (i.op[0].regs->reg_flags & RegRex64) != 0)
bab6aec1 5154 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte
29b0f896 5155 && (i.op[1].regs->reg_flags & RegRex64) != 0)
bab6aec1
JB
5156 || (((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte)
5157 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte))
29b0f896
AM
5158 && i.rex != 0))
5159 {
5160 int x;
726c5dcd 5161
29b0f896
AM
5162 i.rex |= REX_OPCODE;
5163 for (x = 0; x < 2; x++)
5164 {
5165 /* Look for 8 bit operand that uses old registers. */
bab6aec1 5166 if (i.types[x].bitfield.class == Reg && i.types[x].bitfield.byte
29b0f896 5167 && (i.op[x].regs->reg_flags & RegRex64) == 0)
773f551c 5168 {
3f93af61 5169 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
29b0f896
AM
5170 /* In case it is "hi" register, give up. */
5171 if (i.op[x].regs->reg_num > 3)
a540244d 5172 as_bad (_("can't encode register '%s%s' in an "
4eed87de 5173 "instruction requiring REX prefix."),
a540244d 5174 register_prefix, i.op[x].regs->reg_name);
773f551c 5175
29b0f896
AM
5176 /* Otherwise it is equivalent to the extended register.
5177 Since the encoding doesn't change this is merely
5178 cosmetic cleanup for debug output. */
5179
5180 i.op[x].regs = i.op[x].regs + 8;
773f551c 5181 }
29b0f896
AM
5182 }
5183 }
773f551c 5184
6b6b6807
L
5185 if (i.rex == 0 && i.rex_encoding)
5186 {
5187 /* Check if we can add a REX_OPCODE byte. Look for 8 bit operand
3f93af61 5188 that uses legacy register. If it is "hi" register, don't add
6b6b6807
L
5189 the REX_OPCODE byte. */
5190 int x;
5191 for (x = 0; x < 2; x++)
bab6aec1 5192 if (i.types[x].bitfield.class == Reg
6b6b6807
L
5193 && i.types[x].bitfield.byte
5194 && (i.op[x].regs->reg_flags & RegRex64) == 0
5195 && i.op[x].regs->reg_num > 3)
5196 {
3f93af61 5197 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
5b7c81bd 5198 i.rex_encoding = false;
6b6b6807
L
5199 break;
5200 }
5201
5202 if (i.rex_encoding)
5203 i.rex = REX_OPCODE;
5204 }
5205
7ab9ffdd 5206 if (i.rex != 0)
29b0f896
AM
5207 add_prefix (REX_OPCODE | i.rex);
5208
ae531041
L
5209 insert_lfence_before ();
5210
29b0f896
AM
5211 /* We are ready to output the insn. */
5212 output_insn ();
e379e5f3 5213
ae531041
L
5214 insert_lfence_after ();
5215
e379e5f3
L
5216 last_insn.seg = now_seg;
5217
5218 if (i.tm.opcode_modifier.isprefix)
5219 {
5220 last_insn.kind = last_insn_prefix;
5221 last_insn.name = i.tm.name;
5222 last_insn.file = as_where (&last_insn.line);
5223 }
5224 else
5225 last_insn.kind = last_insn_other;
29b0f896
AM
5226}
5227
5228static char *
e3bb37b5 5229parse_insn (char *line, char *mnemonic)
29b0f896
AM
5230{
5231 char *l = line;
5232 char *token_start = l;
5233 char *mnem_p;
5c6af06e 5234 int supported;
d3ce72d0 5235 const insn_template *t;
b6169b20 5236 char *dot_p = NULL;
29b0f896 5237
29b0f896
AM
5238 while (1)
5239 {
5240 mnem_p = mnemonic;
5241 while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
5242 {
b6169b20
L
5243 if (*mnem_p == '.')
5244 dot_p = mnem_p;
29b0f896
AM
5245 mnem_p++;
5246 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
45288df1 5247 {
29b0f896
AM
5248 as_bad (_("no such instruction: `%s'"), token_start);
5249 return NULL;
5250 }
5251 l++;
5252 }
5253 if (!is_space_char (*l)
5254 && *l != END_OF_INSN
e44823cf
JB
5255 && (intel_syntax
5256 || (*l != PREFIX_SEPARATOR
5257 && *l != ',')))
29b0f896
AM
5258 {
5259 as_bad (_("invalid character %s in mnemonic"),
5260 output_invalid (*l));
5261 return NULL;
5262 }
5263 if (token_start == l)
5264 {
e44823cf 5265 if (!intel_syntax && *l == PREFIX_SEPARATOR)
29b0f896
AM
5266 as_bad (_("expecting prefix; got nothing"));
5267 else
5268 as_bad (_("expecting mnemonic; got nothing"));
5269 return NULL;
5270 }
45288df1 5271
29b0f896 5272 /* Look up instruction (or prefix) via hash table. */
629310ab 5273 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
47926f60 5274
29b0f896
AM
5275 if (*l != END_OF_INSN
5276 && (!is_space_char (*l) || l[1] != END_OF_INSN)
5277 && current_templates
40fb9820 5278 && current_templates->start->opcode_modifier.isprefix)
29b0f896 5279 {
c6fb90c8 5280 if (!cpu_flags_check_cpu64 (current_templates->start->cpu_flags))
2dd88dca
JB
5281 {
5282 as_bad ((flag_code != CODE_64BIT
5283 ? _("`%s' is only supported in 64-bit mode")
5284 : _("`%s' is not supported in 64-bit mode")),
5285 current_templates->start->name);
5286 return NULL;
5287 }
29b0f896
AM
5288 /* If we are in 16-bit mode, do not allow addr16 or data16.
5289 Similarly, in 32-bit mode, do not allow addr32 or data32. */
673fe0f0
JB
5290 if ((current_templates->start->opcode_modifier.size == SIZE16
5291 || current_templates->start->opcode_modifier.size == SIZE32)
29b0f896 5292 && flag_code != CODE_64BIT
673fe0f0 5293 && ((current_templates->start->opcode_modifier.size == SIZE32)
29b0f896
AM
5294 ^ (flag_code == CODE_16BIT)))
5295 {
5296 as_bad (_("redundant %s prefix"),
5297 current_templates->start->name);
5298 return NULL;
45288df1 5299 }
31184569
JB
5300
5301 if (current_templates->start->base_opcode == PSEUDO_PREFIX)
29b0f896 5302 {
86fa6981 5303 /* Handle pseudo prefixes. */
31184569 5304 switch (current_templates->start->extension_opcode)
86fa6981 5305 {
41eb8e88 5306 case Prefix_Disp8:
86fa6981
L
5307 /* {disp8} */
5308 i.disp_encoding = disp_encoding_8bit;
5309 break;
41eb8e88
L
5310 case Prefix_Disp16:
5311 /* {disp16} */
5312 i.disp_encoding = disp_encoding_16bit;
5313 break;
5314 case Prefix_Disp32:
86fa6981
L
5315 /* {disp32} */
5316 i.disp_encoding = disp_encoding_32bit;
5317 break;
41eb8e88 5318 case Prefix_Load:
86fa6981
L
5319 /* {load} */
5320 i.dir_encoding = dir_encoding_load;
5321 break;
41eb8e88 5322 case Prefix_Store:
86fa6981
L
5323 /* {store} */
5324 i.dir_encoding = dir_encoding_store;
5325 break;
41eb8e88 5326 case Prefix_VEX:
42e04b36
L
5327 /* {vex} */
5328 i.vec_encoding = vex_encoding_vex;
86fa6981 5329 break;
41eb8e88 5330 case Prefix_VEX3:
86fa6981
L
5331 /* {vex3} */
5332 i.vec_encoding = vex_encoding_vex3;
5333 break;
41eb8e88 5334 case Prefix_EVEX:
86fa6981
L
5335 /* {evex} */
5336 i.vec_encoding = vex_encoding_evex;
5337 break;
41eb8e88 5338 case Prefix_REX:
6b6b6807 5339 /* {rex} */
5b7c81bd 5340 i.rex_encoding = true;
6b6b6807 5341 break;
41eb8e88 5342 case Prefix_NoOptimize:
b6f8c7c4 5343 /* {nooptimize} */
5b7c81bd 5344 i.no_optimize = true;
b6f8c7c4 5345 break;
86fa6981
L
5346 default:
5347 abort ();
5348 }
5349 }
5350 else
5351 {
5352 /* Add prefix, checking for repeated prefixes. */
4e9ac44a 5353 switch (add_prefix (current_templates->start->base_opcode))
86fa6981 5354 {
4e9ac44a
L
5355 case PREFIX_EXIST:
5356 return NULL;
5357 case PREFIX_DS:
d777820b 5358 if (current_templates->start->cpu_flags.bitfield.cpuibt)
4e9ac44a
L
5359 i.notrack_prefix = current_templates->start->name;
5360 break;
5361 case PREFIX_REP:
5362 if (current_templates->start->cpu_flags.bitfield.cpuhle)
5363 i.hle_prefix = current_templates->start->name;
5364 else if (current_templates->start->cpu_flags.bitfield.cpumpx)
5365 i.bnd_prefix = current_templates->start->name;
5366 else
5367 i.rep_prefix = current_templates->start->name;
5368 break;
5369 default:
5370 break;
86fa6981 5371 }
29b0f896
AM
5372 }
5373 /* Skip past PREFIX_SEPARATOR and reset token_start. */
5374 token_start = ++l;
5375 }
5376 else
5377 break;
5378 }
45288df1 5379
30a55f88 5380 if (!current_templates)
b6169b20 5381 {
07d5e953
JB
5382 /* Deprecated functionality (new code should use pseudo-prefixes instead):
5383 Check if we should swap operand or force 32bit displacement in
f8a5c266 5384 encoding. */
30a55f88 5385 if (mnem_p - 2 == dot_p && dot_p[1] == 's')
64c49ab3 5386 i.dir_encoding = dir_encoding_swap;
8d63c93e 5387 else if (mnem_p - 3 == dot_p
a501d77e
L
5388 && dot_p[1] == 'd'
5389 && dot_p[2] == '8')
5390 i.disp_encoding = disp_encoding_8bit;
8d63c93e 5391 else if (mnem_p - 4 == dot_p
f8a5c266
L
5392 && dot_p[1] == 'd'
5393 && dot_p[2] == '3'
5394 && dot_p[3] == '2')
a501d77e 5395 i.disp_encoding = disp_encoding_32bit;
30a55f88
L
5396 else
5397 goto check_suffix;
5398 mnem_p = dot_p;
5399 *dot_p = '\0';
629310ab 5400 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
b6169b20
L
5401 }
5402
29b0f896
AM
5403 if (!current_templates)
5404 {
dc1e8a47 5405 check_suffix:
1c529385 5406 if (mnem_p > mnemonic)
29b0f896 5407 {
1c529385
LH
5408 /* See if we can get a match by trimming off a suffix. */
5409 switch (mnem_p[-1])
29b0f896 5410 {
1c529385
LH
5411 case WORD_MNEM_SUFFIX:
5412 if (intel_syntax && (intel_float_operand (mnemonic) & 2))
29b0f896
AM
5413 i.suffix = SHORT_MNEM_SUFFIX;
5414 else
1c529385
LH
5415 /* Fall through. */
5416 case BYTE_MNEM_SUFFIX:
5417 case QWORD_MNEM_SUFFIX:
5418 i.suffix = mnem_p[-1];
29b0f896 5419 mnem_p[-1] = '\0';
fe0e921f
AM
5420 current_templates
5421 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5422 break;
5423 case SHORT_MNEM_SUFFIX:
5424 case LONG_MNEM_SUFFIX:
5425 if (!intel_syntax)
5426 {
5427 i.suffix = mnem_p[-1];
5428 mnem_p[-1] = '\0';
fe0e921f
AM
5429 current_templates
5430 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5431 }
5432 break;
5433
5434 /* Intel Syntax. */
5435 case 'd':
5436 if (intel_syntax)
5437 {
5438 if (intel_float_operand (mnemonic) == 1)
5439 i.suffix = SHORT_MNEM_SUFFIX;
5440 else
5441 i.suffix = LONG_MNEM_SUFFIX;
5442 mnem_p[-1] = '\0';
fe0e921f
AM
5443 current_templates
5444 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5445 }
5446 break;
29b0f896 5447 }
29b0f896 5448 }
1c529385 5449
29b0f896
AM
5450 if (!current_templates)
5451 {
5452 as_bad (_("no such instruction: `%s'"), token_start);
5453 return NULL;
5454 }
5455 }
252b5132 5456
0cfa3eb3
JB
5457 if (current_templates->start->opcode_modifier.jump == JUMP
5458 || current_templates->start->opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
5459 {
5460 /* Check for a branch hint. We allow ",pt" and ",pn" for
5461 predict taken and predict not taken respectively.
5462 I'm not sure that branch hints actually do anything on loop
5463 and jcxz insns (JumpByte) for current Pentium4 chips. They
5464 may work in the future and it doesn't hurt to accept them
5465 now. */
5466 if (l[0] == ',' && l[1] == 'p')
5467 {
5468 if (l[2] == 't')
5469 {
5470 if (!add_prefix (DS_PREFIX_OPCODE))
5471 return NULL;
5472 l += 3;
5473 }
5474 else if (l[2] == 'n')
5475 {
5476 if (!add_prefix (CS_PREFIX_OPCODE))
5477 return NULL;
5478 l += 3;
5479 }
5480 }
5481 }
5482 /* Any other comma loses. */
5483 if (*l == ',')
5484 {
5485 as_bad (_("invalid character %s in mnemonic"),
5486 output_invalid (*l));
5487 return NULL;
5488 }
252b5132 5489
29b0f896 5490 /* Check if instruction is supported on specified architecture. */
5c6af06e
JB
5491 supported = 0;
5492 for (t = current_templates->start; t < current_templates->end; ++t)
5493 {
c0f3af97
L
5494 supported |= cpu_flags_match (t);
5495 if (supported == CPU_FLAGS_PERFECT_MATCH)
548d0ee6
JB
5496 {
5497 if (!cpu_arch_flags.bitfield.cpui386 && (flag_code != CODE_16BIT))
5498 as_warn (_("use .code16 to ensure correct addressing mode"));
3629bb00 5499
548d0ee6
JB
5500 return l;
5501 }
29b0f896 5502 }
3629bb00 5503
548d0ee6
JB
5504 if (!(supported & CPU_FLAGS_64BIT_MATCH))
5505 as_bad (flag_code == CODE_64BIT
5506 ? _("`%s' is not supported in 64-bit mode")
5507 : _("`%s' is only supported in 64-bit mode"),
5508 current_templates->start->name);
5509 else
5510 as_bad (_("`%s' is not supported on `%s%s'"),
5511 current_templates->start->name,
5512 cpu_arch_name ? cpu_arch_name : default_arch,
5513 cpu_sub_arch_name ? cpu_sub_arch_name : "");
252b5132 5514
548d0ee6 5515 return NULL;
29b0f896 5516}
252b5132 5517
29b0f896 5518static char *
e3bb37b5 5519parse_operands (char *l, const char *mnemonic)
29b0f896
AM
5520{
5521 char *token_start;
3138f287 5522
29b0f896
AM
5523 /* 1 if operand is pending after ','. */
5524 unsigned int expecting_operand = 0;
252b5132 5525
29b0f896
AM
5526 /* Non-zero if operand parens not balanced. */
5527 unsigned int paren_not_balanced;
5528
5529 while (*l != END_OF_INSN)
5530 {
5531 /* Skip optional white space before operand. */
5532 if (is_space_char (*l))
5533 ++l;
d02603dc 5534 if (!is_operand_char (*l) && *l != END_OF_INSN && *l != '"')
29b0f896
AM
5535 {
5536 as_bad (_("invalid character %s before operand %d"),
5537 output_invalid (*l),
5538 i.operands + 1);
5539 return NULL;
5540 }
d02603dc 5541 token_start = l; /* After white space. */
29b0f896
AM
5542 paren_not_balanced = 0;
5543 while (paren_not_balanced || *l != ',')
5544 {
5545 if (*l == END_OF_INSN)
5546 {
5547 if (paren_not_balanced)
5548 {
98ff9f1c
JB
5549 know (!intel_syntax);
5550 as_bad (_("unbalanced parenthesis in operand %d."),
5551 i.operands + 1);
29b0f896
AM
5552 return NULL;
5553 }
5554 else
5555 break; /* we are done */
5556 }
d02603dc 5557 else if (!is_operand_char (*l) && !is_space_char (*l) && *l != '"')
29b0f896
AM
5558 {
5559 as_bad (_("invalid character %s in operand %d"),
5560 output_invalid (*l),
5561 i.operands + 1);
5562 return NULL;
5563 }
5564 if (!intel_syntax)
5565 {
5566 if (*l == '(')
5567 ++paren_not_balanced;
5568 if (*l == ')')
5569 --paren_not_balanced;
5570 }
29b0f896
AM
5571 l++;
5572 }
5573 if (l != token_start)
5574 { /* Yes, we've read in another operand. */
5575 unsigned int operand_ok;
5576 this_operand = i.operands++;
5577 if (i.operands > MAX_OPERANDS)
5578 {
5579 as_bad (_("spurious operands; (%d operands/instruction max)"),
5580 MAX_OPERANDS);
5581 return NULL;
5582 }
9d46ce34 5583 i.types[this_operand].bitfield.unspecified = 1;
29b0f896
AM
5584 /* Now parse operand adding info to 'i' as we go along. */
5585 END_STRING_AND_SAVE (l);
5586
1286ab78
L
5587 if (i.mem_operands > 1)
5588 {
5589 as_bad (_("too many memory references for `%s'"),
5590 mnemonic);
5591 return 0;
5592 }
5593
29b0f896
AM
5594 if (intel_syntax)
5595 operand_ok =
5596 i386_intel_operand (token_start,
5597 intel_float_operand (mnemonic));
5598 else
a7619375 5599 operand_ok = i386_att_operand (token_start);
29b0f896
AM
5600
5601 RESTORE_END_STRING (l);
5602 if (!operand_ok)
5603 return NULL;
5604 }
5605 else
5606 {
5607 if (expecting_operand)
5608 {
5609 expecting_operand_after_comma:
5610 as_bad (_("expecting operand after ','; got nothing"));
5611 return NULL;
5612 }
5613 if (*l == ',')
5614 {
5615 as_bad (_("expecting operand before ','; got nothing"));
5616 return NULL;
5617 }
5618 }
7f3f1ea2 5619
29b0f896
AM
5620 /* Now *l must be either ',' or END_OF_INSN. */
5621 if (*l == ',')
5622 {
5623 if (*++l == END_OF_INSN)
5624 {
5625 /* Just skip it, if it's \n complain. */
5626 goto expecting_operand_after_comma;
5627 }
5628 expecting_operand = 1;
5629 }
5630 }
5631 return l;
5632}
7f3f1ea2 5633
050dfa73 5634static void
783c187b 5635swap_2_operands (unsigned int xchg1, unsigned int xchg2)
050dfa73
MM
5636{
5637 union i386_op temp_op;
40fb9820 5638 i386_operand_type temp_type;
c48dadc9 5639 unsigned int temp_flags;
050dfa73 5640 enum bfd_reloc_code_real temp_reloc;
4eed87de 5641
050dfa73
MM
5642 temp_type = i.types[xchg2];
5643 i.types[xchg2] = i.types[xchg1];
5644 i.types[xchg1] = temp_type;
c48dadc9
JB
5645
5646 temp_flags = i.flags[xchg2];
5647 i.flags[xchg2] = i.flags[xchg1];
5648 i.flags[xchg1] = temp_flags;
5649
050dfa73
MM
5650 temp_op = i.op[xchg2];
5651 i.op[xchg2] = i.op[xchg1];
5652 i.op[xchg1] = temp_op;
c48dadc9 5653
050dfa73
MM
5654 temp_reloc = i.reloc[xchg2];
5655 i.reloc[xchg2] = i.reloc[xchg1];
5656 i.reloc[xchg1] = temp_reloc;
43234a1e 5657
6225c532 5658 if (i.mask.reg)
43234a1e 5659 {
6225c532
JB
5660 if (i.mask.operand == xchg1)
5661 i.mask.operand = xchg2;
5662 else if (i.mask.operand == xchg2)
5663 i.mask.operand = xchg1;
43234a1e 5664 }
5273a3cd 5665 if (i.broadcast.type)
43234a1e 5666 {
5273a3cd
JB
5667 if (i.broadcast.operand == xchg1)
5668 i.broadcast.operand = xchg2;
5669 else if (i.broadcast.operand == xchg2)
5670 i.broadcast.operand = xchg1;
43234a1e 5671 }
ca5312a2 5672 if (i.rounding.type != rc_none)
43234a1e 5673 {
ca5312a2
JB
5674 if (i.rounding.operand == xchg1)
5675 i.rounding.operand = xchg2;
5676 else if (i.rounding.operand == xchg2)
5677 i.rounding.operand = xchg1;
43234a1e 5678 }
050dfa73
MM
5679}
5680
29b0f896 5681static void
e3bb37b5 5682swap_operands (void)
29b0f896 5683{
b7c61d9a 5684 switch (i.operands)
050dfa73 5685 {
c0f3af97 5686 case 5:
b7c61d9a 5687 case 4:
4d456e3d 5688 swap_2_operands (1, i.operands - 2);
1a0670f3 5689 /* Fall through. */
b7c61d9a
L
5690 case 3:
5691 case 2:
4d456e3d 5692 swap_2_operands (0, i.operands - 1);
b7c61d9a
L
5693 break;
5694 default:
5695 abort ();
29b0f896 5696 }
29b0f896
AM
5697
5698 if (i.mem_operands == 2)
5699 {
5e042380 5700 const reg_entry *temp_seg;
29b0f896
AM
5701 temp_seg = i.seg[0];
5702 i.seg[0] = i.seg[1];
5703 i.seg[1] = temp_seg;
5704 }
5705}
252b5132 5706
29b0f896
AM
5707/* Try to ensure constant immediates are represented in the smallest
5708 opcode possible. */
5709static void
e3bb37b5 5710optimize_imm (void)
29b0f896
AM
5711{
5712 char guess_suffix = 0;
5713 int op;
252b5132 5714
29b0f896
AM
5715 if (i.suffix)
5716 guess_suffix = i.suffix;
5717 else if (i.reg_operands)
5718 {
5719 /* Figure out a suffix from the last register operand specified.
75e5731b
JB
5720 We can't do this properly yet, i.e. excluding special register
5721 instances, but the following works for instructions with
5722 immediates. In any case, we can't set i.suffix yet. */
29b0f896 5723 for (op = i.operands; --op >= 0;)
bab6aec1
JB
5724 if (i.types[op].bitfield.class != Reg)
5725 continue;
5726 else if (i.types[op].bitfield.byte)
7ab9ffdd 5727 {
40fb9820
L
5728 guess_suffix = BYTE_MNEM_SUFFIX;
5729 break;
5730 }
bab6aec1 5731 else if (i.types[op].bitfield.word)
252b5132 5732 {
40fb9820
L
5733 guess_suffix = WORD_MNEM_SUFFIX;
5734 break;
5735 }
bab6aec1 5736 else if (i.types[op].bitfield.dword)
40fb9820
L
5737 {
5738 guess_suffix = LONG_MNEM_SUFFIX;
5739 break;
5740 }
bab6aec1 5741 else if (i.types[op].bitfield.qword)
40fb9820
L
5742 {
5743 guess_suffix = QWORD_MNEM_SUFFIX;
29b0f896 5744 break;
252b5132 5745 }
29b0f896
AM
5746 }
5747 else if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
5748 guess_suffix = WORD_MNEM_SUFFIX;
5749
5750 for (op = i.operands; --op >= 0;)
40fb9820 5751 if (operand_type_check (i.types[op], imm))
29b0f896
AM
5752 {
5753 switch (i.op[op].imms->X_op)
252b5132 5754 {
29b0f896
AM
5755 case O_constant:
5756 /* If a suffix is given, this operand may be shortened. */
5757 switch (guess_suffix)
252b5132 5758 {
29b0f896 5759 case LONG_MNEM_SUFFIX:
40fb9820
L
5760 i.types[op].bitfield.imm32 = 1;
5761 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
5762 break;
5763 case WORD_MNEM_SUFFIX:
40fb9820
L
5764 i.types[op].bitfield.imm16 = 1;
5765 i.types[op].bitfield.imm32 = 1;
5766 i.types[op].bitfield.imm32s = 1;
5767 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
5768 break;
5769 case BYTE_MNEM_SUFFIX:
40fb9820
L
5770 i.types[op].bitfield.imm8 = 1;
5771 i.types[op].bitfield.imm8s = 1;
5772 i.types[op].bitfield.imm16 = 1;
5773 i.types[op].bitfield.imm32 = 1;
5774 i.types[op].bitfield.imm32s = 1;
5775 i.types[op].bitfield.imm64 = 1;
29b0f896 5776 break;
252b5132 5777 }
252b5132 5778
29b0f896
AM
5779 /* If this operand is at most 16 bits, convert it
5780 to a signed 16 bit number before trying to see
5781 whether it will fit in an even smaller size.
5782 This allows a 16-bit operand such as $0xffe0 to
5783 be recognised as within Imm8S range. */
40fb9820 5784 if ((i.types[op].bitfield.imm16)
29b0f896 5785 && (i.op[op].imms->X_add_number & ~(offsetT) 0xffff) == 0)
252b5132 5786 {
87ed972d
JB
5787 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
5788 ^ 0x8000) - 0x8000);
29b0f896 5789 }
a28def75
L
5790#ifdef BFD64
5791 /* Store 32-bit immediate in 64-bit for 64-bit BFD. */
40fb9820 5792 if ((i.types[op].bitfield.imm32)
29b0f896
AM
5793 && ((i.op[op].imms->X_add_number & ~(((offsetT) 2 << 31) - 1))
5794 == 0))
5795 {
5796 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
5797 ^ ((offsetT) 1 << 31))
5798 - ((offsetT) 1 << 31));
5799 }
a28def75 5800#endif
40fb9820 5801 i.types[op]
c6fb90c8
L
5802 = operand_type_or (i.types[op],
5803 smallest_imm_type (i.op[op].imms->X_add_number));
252b5132 5804
29b0f896
AM
5805 /* We must avoid matching of Imm32 templates when 64bit
5806 only immediate is available. */
5807 if (guess_suffix == QWORD_MNEM_SUFFIX)
40fb9820 5808 i.types[op].bitfield.imm32 = 0;
29b0f896 5809 break;
252b5132 5810
29b0f896
AM
5811 case O_absent:
5812 case O_register:
5813 abort ();
5814
5815 /* Symbols and expressions. */
5816 default:
9cd96992
JB
5817 /* Convert symbolic operand to proper sizes for matching, but don't
5818 prevent matching a set of insns that only supports sizes other
5819 than those matching the insn suffix. */
5820 {
40fb9820 5821 i386_operand_type mask, allowed;
87ed972d 5822 const insn_template *t = current_templates->start;
9cd96992 5823
0dfbf9d7 5824 operand_type_set (&mask, 0);
87ed972d 5825 allowed = t->operand_types[op];
40fb9820 5826
87ed972d 5827 while (++t < current_templates->end)
bab6aec1 5828 {
bab6aec1 5829 allowed = operand_type_and (allowed, anyimm);
87ed972d 5830 allowed = operand_type_or (allowed, t->operand_types[op]);
bab6aec1 5831 }
9cd96992
JB
5832 switch (guess_suffix)
5833 {
5834 case QWORD_MNEM_SUFFIX:
40fb9820
L
5835 mask.bitfield.imm64 = 1;
5836 mask.bitfield.imm32s = 1;
9cd96992
JB
5837 break;
5838 case LONG_MNEM_SUFFIX:
40fb9820 5839 mask.bitfield.imm32 = 1;
9cd96992
JB
5840 break;
5841 case WORD_MNEM_SUFFIX:
40fb9820 5842 mask.bitfield.imm16 = 1;
9cd96992
JB
5843 break;
5844 case BYTE_MNEM_SUFFIX:
40fb9820 5845 mask.bitfield.imm8 = 1;
9cd96992
JB
5846 break;
5847 default:
9cd96992
JB
5848 break;
5849 }
c6fb90c8 5850 allowed = operand_type_and (mask, allowed);
0dfbf9d7 5851 if (!operand_type_all_zero (&allowed))
c6fb90c8 5852 i.types[op] = operand_type_and (i.types[op], mask);
9cd96992 5853 }
29b0f896 5854 break;
252b5132 5855 }
29b0f896
AM
5856 }
5857}
47926f60 5858
29b0f896
AM
5859/* Try to use the smallest displacement type too. */
5860static void
e3bb37b5 5861optimize_disp (void)
29b0f896
AM
5862{
5863 int op;
3e73aa7c 5864
29b0f896 5865 for (op = i.operands; --op >= 0;)
40fb9820 5866 if (operand_type_check (i.types[op], disp))
252b5132 5867 {
b300c311 5868 if (i.op[op].disps->X_op == O_constant)
252b5132 5869 {
91d6fa6a 5870 offsetT op_disp = i.op[op].disps->X_add_number;
29b0f896 5871
40fb9820 5872 if (i.types[op].bitfield.disp16
91d6fa6a 5873 && (op_disp & ~(offsetT) 0xffff) == 0)
b300c311
L
5874 {
5875 /* If this operand is at most 16 bits, convert
5876 to a signed 16 bit number and don't use 64bit
5877 displacement. */
91d6fa6a 5878 op_disp = (((op_disp & 0xffff) ^ 0x8000) - 0x8000);
40fb9820 5879 i.types[op].bitfield.disp64 = 0;
b300c311 5880 }
91d6fa6a 5881 if (!op_disp && i.types[op].bitfield.baseindex)
b300c311 5882 {
40fb9820
L
5883 i.types[op].bitfield.disp8 = 0;
5884 i.types[op].bitfield.disp16 = 0;
5885 i.types[op].bitfield.disp32 = 0;
5886 i.types[op].bitfield.disp32s = 0;
5887 i.types[op].bitfield.disp64 = 0;
b300c311
L
5888 i.op[op].disps = 0;
5889 i.disp_operands--;
5890 }
28a167a4 5891#ifdef BFD64
b300c311
L
5892 else if (flag_code == CODE_64BIT)
5893 {
a9aabc23 5894 if (want_disp32 (current_templates->start)
28a167a4
JB
5895 && fits_in_unsigned_long (op_disp))
5896 i.types[op].bitfield.disp32 = 1;
5897
5898 /* Optimize 64-bit displacement to 32-bit for 64-bit BFD. */
5899 if (i.types[op].bitfield.disp32
5900 && (op_disp & ~(((offsetT) 2 << 31) - 1)) == 0)
5901 {
5902 /* If this operand is at most 32 bits, convert
5903 to a signed 32 bit number and don't use 64bit
5904 displacement. */
5905 op_disp &= (((offsetT) 2 << 31) - 1);
5906 op_disp = (op_disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
5907 i.types[op].bitfield.disp64 = 0;
5908 }
5909
91d6fa6a 5910 if (fits_in_signed_long (op_disp))
28a9d8f5 5911 {
40fb9820
L
5912 i.types[op].bitfield.disp64 = 0;
5913 i.types[op].bitfield.disp32s = 1;
28a9d8f5 5914 }
b300c311 5915 }
28a167a4 5916#endif
40fb9820
L
5917 if ((i.types[op].bitfield.disp32
5918 || i.types[op].bitfield.disp32s
5919 || i.types[op].bitfield.disp16)
b5014f7a 5920 && fits_in_disp8 (op_disp))
40fb9820 5921 i.types[op].bitfield.disp8 = 1;
252b5132 5922 }
67a4f2b7
AO
5923 else if (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
5924 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL)
5925 {
5926 fix_new_exp (frag_now, frag_more (0) - frag_now->fr_literal, 0,
5927 i.op[op].disps, 0, i.reloc[op]);
40fb9820
L
5928 i.types[op].bitfield.disp8 = 0;
5929 i.types[op].bitfield.disp16 = 0;
5930 i.types[op].bitfield.disp32 = 0;
5931 i.types[op].bitfield.disp32s = 0;
5932 i.types[op].bitfield.disp64 = 0;
67a4f2b7
AO
5933 }
5934 else
b300c311 5935 /* We only support 64bit displacement on constants. */
40fb9820 5936 i.types[op].bitfield.disp64 = 0;
252b5132 5937 }
29b0f896
AM
5938}
5939
4a1b91ea
L
5940/* Return 1 if there is a match in broadcast bytes between operand
5941 GIVEN and instruction template T. */
5942
5943static INLINE int
5944match_broadcast_size (const insn_template *t, unsigned int given)
5945{
5946 return ((t->opcode_modifier.broadcast == BYTE_BROADCAST
5947 && i.types[given].bitfield.byte)
5948 || (t->opcode_modifier.broadcast == WORD_BROADCAST
5949 && i.types[given].bitfield.word)
5950 || (t->opcode_modifier.broadcast == DWORD_BROADCAST
5951 && i.types[given].bitfield.dword)
5952 || (t->opcode_modifier.broadcast == QWORD_BROADCAST
5953 && i.types[given].bitfield.qword));
5954}
5955
6c30d220
L
5956/* Check if operands are valid for the instruction. */
5957
5958static int
5959check_VecOperands (const insn_template *t)
5960{
43234a1e 5961 unsigned int op;
e2195274 5962 i386_cpu_flags cpu;
e2195274
JB
5963
5964 /* Templates allowing for ZMMword as well as YMMword and/or XMMword for
5965 any one operand are implicity requiring AVX512VL support if the actual
5966 operand size is YMMword or XMMword. Since this function runs after
5967 template matching, there's no need to check for YMMword/XMMword in
5968 the template. */
5969 cpu = cpu_flags_and (t->cpu_flags, avx512);
5970 if (!cpu_flags_all_zero (&cpu)
5971 && !t->cpu_flags.bitfield.cpuavx512vl
5972 && !cpu_arch_flags.bitfield.cpuavx512vl)
5973 {
5974 for (op = 0; op < t->operands; ++op)
5975 {
5976 if (t->operand_types[op].bitfield.zmmword
5977 && (i.types[op].bitfield.ymmword
5978 || i.types[op].bitfield.xmmword))
5979 {
5980 i.error = unsupported;
5981 return 1;
5982 }
5983 }
5984 }
43234a1e 5985
6c30d220 5986 /* Without VSIB byte, we can't have a vector register for index. */
63112cd6 5987 if (!t->opcode_modifier.sib
6c30d220 5988 && i.index_reg
1b54b8d7
JB
5989 && (i.index_reg->reg_type.bitfield.xmmword
5990 || i.index_reg->reg_type.bitfield.ymmword
5991 || i.index_reg->reg_type.bitfield.zmmword))
6c30d220
L
5992 {
5993 i.error = unsupported_vector_index_register;
5994 return 1;
5995 }
5996
ad8ecc81
MZ
5997 /* Check if default mask is allowed. */
5998 if (t->opcode_modifier.nodefmask
6225c532 5999 && (!i.mask.reg || i.mask.reg->reg_num == 0))
ad8ecc81
MZ
6000 {
6001 i.error = no_default_mask;
6002 return 1;
6003 }
6004
7bab8ab5
JB
6005 /* For VSIB byte, we need a vector register for index, and all vector
6006 registers must be distinct. */
260cd341 6007 if (t->opcode_modifier.sib && t->opcode_modifier.sib != SIBMEM)
7bab8ab5
JB
6008 {
6009 if (!i.index_reg
63112cd6 6010 || !((t->opcode_modifier.sib == VECSIB128
1b54b8d7 6011 && i.index_reg->reg_type.bitfield.xmmword)
63112cd6 6012 || (t->opcode_modifier.sib == VECSIB256
1b54b8d7 6013 && i.index_reg->reg_type.bitfield.ymmword)
63112cd6 6014 || (t->opcode_modifier.sib == VECSIB512
1b54b8d7 6015 && i.index_reg->reg_type.bitfield.zmmword)))
7bab8ab5
JB
6016 {
6017 i.error = invalid_vsib_address;
6018 return 1;
6019 }
6020
6225c532
JB
6021 gas_assert (i.reg_operands == 2 || i.mask.reg);
6022 if (i.reg_operands == 2 && !i.mask.reg)
43234a1e 6023 {
3528c362 6024 gas_assert (i.types[0].bitfield.class == RegSIMD);
1b54b8d7
JB
6025 gas_assert (i.types[0].bitfield.xmmword
6026 || i.types[0].bitfield.ymmword);
3528c362 6027 gas_assert (i.types[2].bitfield.class == RegSIMD);
1b54b8d7
JB
6028 gas_assert (i.types[2].bitfield.xmmword
6029 || i.types[2].bitfield.ymmword);
43234a1e
L
6030 if (operand_check == check_none)
6031 return 0;
6032 if (register_number (i.op[0].regs)
6033 != register_number (i.index_reg)
6034 && register_number (i.op[2].regs)
6035 != register_number (i.index_reg)
6036 && register_number (i.op[0].regs)
6037 != register_number (i.op[2].regs))
6038 return 0;
6039 if (operand_check == check_error)
6040 {
6041 i.error = invalid_vector_register_set;
6042 return 1;
6043 }
6044 as_warn (_("mask, index, and destination registers should be distinct"));
6045 }
6225c532 6046 else if (i.reg_operands == 1 && i.mask.reg)
8444f82a 6047 {
3528c362 6048 if (i.types[1].bitfield.class == RegSIMD
1b54b8d7
JB
6049 && (i.types[1].bitfield.xmmword
6050 || i.types[1].bitfield.ymmword
6051 || i.types[1].bitfield.zmmword)
8444f82a
MZ
6052 && (register_number (i.op[1].regs)
6053 == register_number (i.index_reg)))
6054 {
6055 if (operand_check == check_error)
6056 {
6057 i.error = invalid_vector_register_set;
6058 return 1;
6059 }
6060 if (operand_check != check_none)
6061 as_warn (_("index and destination registers should be distinct"));
6062 }
6063 }
43234a1e 6064 }
7bab8ab5 6065
260cd341
LC
6066 /* For AMX instructions with three tmmword operands, all tmmword operand must be
6067 distinct */
6068 if (t->operand_types[0].bitfield.tmmword
6069 && i.reg_operands == 3)
6070 {
6071 if (register_number (i.op[0].regs)
6072 == register_number (i.op[1].regs)
6073 || register_number (i.op[0].regs)
6074 == register_number (i.op[2].regs)
6075 || register_number (i.op[1].regs)
6076 == register_number (i.op[2].regs))
6077 {
6078 i.error = invalid_tmm_register_set;
6079 return 1;
6080 }
6081 }
6082
43234a1e
L
6083 /* Check if broadcast is supported by the instruction and is applied
6084 to the memory operand. */
5273a3cd 6085 if (i.broadcast.type)
43234a1e 6086 {
8e6e0792 6087 i386_operand_type type, overlap;
43234a1e
L
6088
6089 /* Check if specified broadcast is supported in this instruction,
4a1b91ea 6090 and its broadcast bytes match the memory operand. */
5273a3cd 6091 op = i.broadcast.operand;
8e6e0792 6092 if (!t->opcode_modifier.broadcast
c48dadc9 6093 || !(i.flags[op] & Operand_Mem)
c39e5b26 6094 || (!i.types[op].bitfield.unspecified
4a1b91ea 6095 && !match_broadcast_size (t, op)))
43234a1e
L
6096 {
6097 bad_broadcast:
6098 i.error = unsupported_broadcast;
6099 return 1;
6100 }
8e6e0792 6101
5273a3cd
JB
6102 i.broadcast.bytes = ((1 << (t->opcode_modifier.broadcast - 1))
6103 * i.broadcast.type);
8e6e0792 6104 operand_type_set (&type, 0);
5273a3cd 6105 switch (i.broadcast.bytes)
8e6e0792 6106 {
4a1b91ea
L
6107 case 2:
6108 type.bitfield.word = 1;
6109 break;
6110 case 4:
6111 type.bitfield.dword = 1;
6112 break;
8e6e0792
JB
6113 case 8:
6114 type.bitfield.qword = 1;
6115 break;
6116 case 16:
6117 type.bitfield.xmmword = 1;
6118 break;
6119 case 32:
6120 type.bitfield.ymmword = 1;
6121 break;
6122 case 64:
6123 type.bitfield.zmmword = 1;
6124 break;
6125 default:
6126 goto bad_broadcast;
6127 }
6128
6129 overlap = operand_type_and (type, t->operand_types[op]);
bc49bfd8
JB
6130 if (t->operand_types[op].bitfield.class == RegSIMD
6131 && t->operand_types[op].bitfield.byte
6132 + t->operand_types[op].bitfield.word
6133 + t->operand_types[op].bitfield.dword
6134 + t->operand_types[op].bitfield.qword > 1)
6135 {
6136 overlap.bitfield.xmmword = 0;
6137 overlap.bitfield.ymmword = 0;
6138 overlap.bitfield.zmmword = 0;
6139 }
8e6e0792
JB
6140 if (operand_type_all_zero (&overlap))
6141 goto bad_broadcast;
6142
6143 if (t->opcode_modifier.checkregsize)
6144 {
6145 unsigned int j;
6146
e2195274 6147 type.bitfield.baseindex = 1;
8e6e0792
JB
6148 for (j = 0; j < i.operands; ++j)
6149 {
6150 if (j != op
6151 && !operand_type_register_match(i.types[j],
6152 t->operand_types[j],
6153 type,
6154 t->operand_types[op]))
6155 goto bad_broadcast;
6156 }
6157 }
43234a1e
L
6158 }
6159 /* If broadcast is supported in this instruction, we need to check if
6160 operand of one-element size isn't specified without broadcast. */
6161 else if (t->opcode_modifier.broadcast && i.mem_operands)
6162 {
6163 /* Find memory operand. */
6164 for (op = 0; op < i.operands; op++)
8dc0818e 6165 if (i.flags[op] & Operand_Mem)
43234a1e
L
6166 break;
6167 gas_assert (op < i.operands);
6168 /* Check size of the memory operand. */
4a1b91ea 6169 if (match_broadcast_size (t, op))
43234a1e
L
6170 {
6171 i.error = broadcast_needed;
6172 return 1;
6173 }
6174 }
c39e5b26
JB
6175 else
6176 op = MAX_OPERANDS - 1; /* Avoid uninitialized variable warning. */
43234a1e
L
6177
6178 /* Check if requested masking is supported. */
6225c532 6179 if (i.mask.reg)
43234a1e 6180 {
ae2387fe
JB
6181 switch (t->opcode_modifier.masking)
6182 {
6183 case BOTH_MASKING:
6184 break;
6185 case MERGING_MASKING:
6225c532 6186 if (i.mask.zeroing)
ae2387fe
JB
6187 {
6188 case 0:
6189 i.error = unsupported_masking;
6190 return 1;
6191 }
6192 break;
6193 case DYNAMIC_MASKING:
6194 /* Memory destinations allow only merging masking. */
6225c532 6195 if (i.mask.zeroing && i.mem_operands)
ae2387fe
JB
6196 {
6197 /* Find memory operand. */
6198 for (op = 0; op < i.operands; op++)
c48dadc9 6199 if (i.flags[op] & Operand_Mem)
ae2387fe
JB
6200 break;
6201 gas_assert (op < i.operands);
6202 if (op == i.operands - 1)
6203 {
6204 i.error = unsupported_masking;
6205 return 1;
6206 }
6207 }
6208 break;
6209 default:
6210 abort ();
6211 }
43234a1e
L
6212 }
6213
6214 /* Check if masking is applied to dest operand. */
6225c532 6215 if (i.mask.reg && (i.mask.operand != i.operands - 1))
43234a1e
L
6216 {
6217 i.error = mask_not_on_destination;
6218 return 1;
6219 }
6220
43234a1e 6221 /* Check RC/SAE. */
ca5312a2 6222 if (i.rounding.type != rc_none)
43234a1e 6223 {
a80195f1 6224 if (!t->opcode_modifier.sae
ca5312a2 6225 || (i.rounding.type != saeonly && !t->opcode_modifier.staticrounding))
43234a1e
L
6226 {
6227 i.error = unsupported_rc_sae;
6228 return 1;
6229 }
6230 /* If the instruction has several immediate operands and one of
6231 them is rounding, the rounding operand should be the last
6232 immediate operand. */
6233 if (i.imm_operands > 1
ca5312a2 6234 && i.rounding.operand != i.imm_operands - 1)
7bab8ab5 6235 {
43234a1e 6236 i.error = rc_sae_operand_not_last_imm;
7bab8ab5
JB
6237 return 1;
6238 }
6c30d220
L
6239 }
6240
da4977e0
JB
6241 /* Check the special Imm4 cases; must be the first operand. */
6242 if (t->cpu_flags.bitfield.cpuxop && t->operands == 5)
6243 {
6244 if (i.op[0].imms->X_op != O_constant
6245 || !fits_in_imm4 (i.op[0].imms->X_add_number))
6246 {
6247 i.error = bad_imm4;
6248 return 1;
6249 }
6250
6251 /* Turn off Imm<N> so that update_imm won't complain. */
6252 operand_type_set (&i.types[0], 0);
6253 }
6254
43234a1e 6255 /* Check vector Disp8 operand. */
b5014f7a
JB
6256 if (t->opcode_modifier.disp8memshift
6257 && i.disp_encoding != disp_encoding_32bit)
43234a1e 6258 {
5273a3cd 6259 if (i.broadcast.type)
4a1b91ea 6260 i.memshift = t->opcode_modifier.broadcast - 1;
7091c612 6261 else if (t->opcode_modifier.disp8memshift != DISP8_SHIFT_VL)
43234a1e 6262 i.memshift = t->opcode_modifier.disp8memshift;
7091c612
JB
6263 else
6264 {
6265 const i386_operand_type *type = NULL;
6266
6267 i.memshift = 0;
6268 for (op = 0; op < i.operands; op++)
8dc0818e 6269 if (i.flags[op] & Operand_Mem)
7091c612 6270 {
4174bfff
JB
6271 if (t->opcode_modifier.evex == EVEXLIG)
6272 i.memshift = 2 + (i.suffix == QWORD_MNEM_SUFFIX);
6273 else if (t->operand_types[op].bitfield.xmmword
6274 + t->operand_types[op].bitfield.ymmword
6275 + t->operand_types[op].bitfield.zmmword <= 1)
7091c612
JB
6276 type = &t->operand_types[op];
6277 else if (!i.types[op].bitfield.unspecified)
6278 type = &i.types[op];
6279 }
3528c362 6280 else if (i.types[op].bitfield.class == RegSIMD
4174bfff 6281 && t->opcode_modifier.evex != EVEXLIG)
7091c612
JB
6282 {
6283 if (i.types[op].bitfield.zmmword)
6284 i.memshift = 6;
6285 else if (i.types[op].bitfield.ymmword && i.memshift < 5)
6286 i.memshift = 5;
6287 else if (i.types[op].bitfield.xmmword && i.memshift < 4)
6288 i.memshift = 4;
6289 }
6290
6291 if (type)
6292 {
6293 if (type->bitfield.zmmword)
6294 i.memshift = 6;
6295 else if (type->bitfield.ymmword)
6296 i.memshift = 5;
6297 else if (type->bitfield.xmmword)
6298 i.memshift = 4;
6299 }
6300
6301 /* For the check in fits_in_disp8(). */
6302 if (i.memshift == 0)
6303 i.memshift = -1;
6304 }
43234a1e
L
6305
6306 for (op = 0; op < i.operands; op++)
6307 if (operand_type_check (i.types[op], disp)
6308 && i.op[op].disps->X_op == O_constant)
6309 {
b5014f7a 6310 if (fits_in_disp8 (i.op[op].disps->X_add_number))
43234a1e 6311 {
b5014f7a
JB
6312 i.types[op].bitfield.disp8 = 1;
6313 return 0;
43234a1e 6314 }
b5014f7a 6315 i.types[op].bitfield.disp8 = 0;
43234a1e
L
6316 }
6317 }
b5014f7a
JB
6318
6319 i.memshift = 0;
43234a1e 6320
6c30d220
L
6321 return 0;
6322}
6323
da4977e0 6324/* Check if encoding requirements are met by the instruction. */
a683cc34
SP
6325
6326static int
da4977e0 6327VEX_check_encoding (const insn_template *t)
a683cc34 6328{
da4977e0
JB
6329 if (i.vec_encoding == vex_encoding_error)
6330 {
6331 i.error = unsupported;
6332 return 1;
6333 }
6334
86fa6981 6335 if (i.vec_encoding == vex_encoding_evex)
43234a1e 6336 {
86fa6981 6337 /* This instruction must be encoded with EVEX prefix. */
e771e7c9 6338 if (!is_evex_encoding (t))
86fa6981
L
6339 {
6340 i.error = unsupported;
6341 return 1;
6342 }
6343 return 0;
43234a1e
L
6344 }
6345
a683cc34 6346 if (!t->opcode_modifier.vex)
86fa6981
L
6347 {
6348 /* This instruction template doesn't have VEX prefix. */
6349 if (i.vec_encoding != vex_encoding_default)
6350 {
6351 i.error = unsupported;
6352 return 1;
6353 }
6354 return 0;
6355 }
a683cc34 6356
a683cc34
SP
6357 return 0;
6358}
6359
d3ce72d0 6360static const insn_template *
83b16ac6 6361match_template (char mnem_suffix)
29b0f896
AM
6362{
6363 /* Points to template once we've found it. */
d3ce72d0 6364 const insn_template *t;
40fb9820 6365 i386_operand_type overlap0, overlap1, overlap2, overlap3;
c0f3af97 6366 i386_operand_type overlap4;
29b0f896 6367 unsigned int found_reverse_match;
dc2be329 6368 i386_opcode_modifier suffix_check;
40fb9820 6369 i386_operand_type operand_types [MAX_OPERANDS];
539e75ad 6370 int addr_prefix_disp;
45a4bb20 6371 unsigned int j, size_match, check_register;
5614d22c 6372 enum i386_error specific_error = 0;
29b0f896 6373
c0f3af97
L
6374#if MAX_OPERANDS != 5
6375# error "MAX_OPERANDS must be 5."
f48ff2ae
L
6376#endif
6377
29b0f896 6378 found_reverse_match = 0;
539e75ad 6379 addr_prefix_disp = -1;
40fb9820 6380
dc2be329 6381 /* Prepare for mnemonic suffix check. */
40fb9820 6382 memset (&suffix_check, 0, sizeof (suffix_check));
dc2be329
L
6383 switch (mnem_suffix)
6384 {
6385 case BYTE_MNEM_SUFFIX:
6386 suffix_check.no_bsuf = 1;
6387 break;
6388 case WORD_MNEM_SUFFIX:
6389 suffix_check.no_wsuf = 1;
6390 break;
6391 case SHORT_MNEM_SUFFIX:
6392 suffix_check.no_ssuf = 1;
6393 break;
6394 case LONG_MNEM_SUFFIX:
6395 suffix_check.no_lsuf = 1;
6396 break;
6397 case QWORD_MNEM_SUFFIX:
6398 suffix_check.no_qsuf = 1;
6399 break;
6400 default:
6401 /* NB: In Intel syntax, normally we can check for memory operand
6402 size when there is no mnemonic suffix. But jmp and call have
6403 2 different encodings with Dword memory operand size, one with
6404 No_ldSuf and the other without. i.suffix is set to
6405 LONG_DOUBLE_MNEM_SUFFIX to skip the one with No_ldSuf. */
6406 if (i.suffix == LONG_DOUBLE_MNEM_SUFFIX)
6407 suffix_check.no_ldsuf = 1;
83b16ac6
JB
6408 }
6409
01559ecc
L
6410 /* Must have right number of operands. */
6411 i.error = number_of_operands_mismatch;
6412
45aa61fe 6413 for (t = current_templates->start; t < current_templates->end; t++)
29b0f896 6414 {
539e75ad 6415 addr_prefix_disp = -1;
dbbc8b7e 6416 found_reverse_match = 0;
539e75ad 6417
29b0f896
AM
6418 if (i.operands != t->operands)
6419 continue;
6420
50aecf8c 6421 /* Check processor support. */
a65babc9 6422 i.error = unsupported;
45a4bb20 6423 if (cpu_flags_match (t) != CPU_FLAGS_PERFECT_MATCH)
50aecf8c
L
6424 continue;
6425
57392598
CL
6426 /* Check Pseudo Prefix. */
6427 i.error = unsupported;
6428 if (t->opcode_modifier.pseudovexprefix
6429 && !(i.vec_encoding == vex_encoding_vex
6430 || i.vec_encoding == vex_encoding_vex3))
6431 continue;
6432
e1d4d893 6433 /* Check AT&T mnemonic. */
a65babc9 6434 i.error = unsupported_with_intel_mnemonic;
e1d4d893 6435 if (intel_mnemonic && t->opcode_modifier.attmnemonic)
1efbbeb4
L
6436 continue;
6437
4b5aaf5f 6438 /* Check AT&T/Intel syntax. */
a65babc9 6439 i.error = unsupported_syntax;
5c07affc 6440 if ((intel_syntax && t->opcode_modifier.attsyntax)
4b5aaf5f 6441 || (!intel_syntax && t->opcode_modifier.intelsyntax))
1efbbeb4
L
6442 continue;
6443
4b5aaf5f
L
6444 /* Check Intel64/AMD64 ISA. */
6445 switch (isa64)
6446 {
6447 default:
6448 /* Default: Don't accept Intel64. */
6449 if (t->opcode_modifier.isa64 == INTEL64)
6450 continue;
6451 break;
6452 case amd64:
6453 /* -mamd64: Don't accept Intel64 and Intel64 only. */
6454 if (t->opcode_modifier.isa64 >= INTEL64)
6455 continue;
6456 break;
6457 case intel64:
6458 /* -mintel64: Don't accept AMD64. */
5990e377 6459 if (t->opcode_modifier.isa64 == AMD64 && flag_code == CODE_64BIT)
4b5aaf5f
L
6460 continue;
6461 break;
6462 }
6463
dc2be329 6464 /* Check the suffix. */
a65babc9 6465 i.error = invalid_instruction_suffix;
dc2be329
L
6466 if ((t->opcode_modifier.no_bsuf && suffix_check.no_bsuf)
6467 || (t->opcode_modifier.no_wsuf && suffix_check.no_wsuf)
6468 || (t->opcode_modifier.no_lsuf && suffix_check.no_lsuf)
6469 || (t->opcode_modifier.no_ssuf && suffix_check.no_ssuf)
6470 || (t->opcode_modifier.no_qsuf && suffix_check.no_qsuf)
6471 || (t->opcode_modifier.no_ldsuf && suffix_check.no_ldsuf))
83b16ac6 6472 continue;
29b0f896 6473
3ac21baa
JB
6474 size_match = operand_size_match (t);
6475 if (!size_match)
7d5e4556 6476 continue;
539e75ad 6477
6f2f06be
JB
6478 /* This is intentionally not
6479
0cfa3eb3 6480 if (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE))
6f2f06be
JB
6481
6482 as the case of a missing * on the operand is accepted (perhaps with
6483 a warning, issued further down). */
0cfa3eb3 6484 if (i.jumpabsolute && t->opcode_modifier.jump != JUMP_ABSOLUTE)
6f2f06be
JB
6485 {
6486 i.error = operand_type_mismatch;
6487 continue;
6488 }
6489
5c07affc
L
6490 for (j = 0; j < MAX_OPERANDS; j++)
6491 operand_types[j] = t->operand_types[j];
6492
e365e234
JB
6493 /* In general, don't allow
6494 - 64-bit operands outside of 64-bit mode,
6495 - 32-bit operands on pre-386. */
4873e243 6496 j = i.imm_operands + (t->operands > i.imm_operands + 1);
e365e234
JB
6497 if (((i.suffix == QWORD_MNEM_SUFFIX
6498 && flag_code != CODE_64BIT
389d00a5
JB
6499 && !(t->opcode_modifier.opcodespace == SPACE_0F
6500 && t->base_opcode == 0xc7
5e74b495 6501 && t->opcode_modifier.opcodeprefix == PREFIX_NONE
8b65b895 6502 && t->extension_opcode == 1) /* cmpxchg8b */)
e365e234
JB
6503 || (i.suffix == LONG_MNEM_SUFFIX
6504 && !cpu_arch_flags.bitfield.cpui386))
45aa61fe 6505 && (intel_syntax
3cd7f3e3 6506 ? (t->opcode_modifier.mnemonicsize != IGNORESIZE
45aa61fe
AM
6507 && !intel_float_operand (t->name))
6508 : intel_float_operand (t->name) != 2)
4873e243
JB
6509 && (t->operands == i.imm_operands
6510 || (operand_types[i.imm_operands].bitfield.class != RegMMX
6511 && operand_types[i.imm_operands].bitfield.class != RegSIMD
6512 && operand_types[i.imm_operands].bitfield.class != RegMask)
6513 || (operand_types[j].bitfield.class != RegMMX
6514 && operand_types[j].bitfield.class != RegSIMD
6515 && operand_types[j].bitfield.class != RegMask))
63112cd6 6516 && !t->opcode_modifier.sib)
192dc9c6
JB
6517 continue;
6518
29b0f896 6519 /* Do not verify operands when there are none. */
e365e234 6520 if (!t->operands)
da4977e0
JB
6521 {
6522 if (VEX_check_encoding (t))
6523 {
6524 specific_error = i.error;
6525 continue;
6526 }
6527
6528 /* We've found a match; break out of loop. */
6529 break;
6530 }
252b5132 6531
48bcea9f
JB
6532 if (!t->opcode_modifier.jump
6533 || t->opcode_modifier.jump == JUMP_ABSOLUTE)
6534 {
6535 /* There should be only one Disp operand. */
6536 for (j = 0; j < MAX_OPERANDS; j++)
6537 if (operand_type_check (operand_types[j], disp))
539e75ad 6538 break;
48bcea9f
JB
6539 if (j < MAX_OPERANDS)
6540 {
5b7c81bd 6541 bool override = (i.prefix[ADDR_PREFIX] != 0);
48bcea9f
JB
6542
6543 addr_prefix_disp = j;
6544
6545 /* Address size prefix will turn Disp64/Disp32S/Disp32/Disp16
6546 operand into Disp32/Disp32/Disp16/Disp32 operand. */
6547 switch (flag_code)
40fb9820 6548 {
48bcea9f
JB
6549 case CODE_16BIT:
6550 override = !override;
6551 /* Fall through. */
6552 case CODE_32BIT:
6553 if (operand_types[j].bitfield.disp32
6554 && operand_types[j].bitfield.disp16)
40fb9820 6555 {
48bcea9f
JB
6556 operand_types[j].bitfield.disp16 = override;
6557 operand_types[j].bitfield.disp32 = !override;
40fb9820 6558 }
48bcea9f
JB
6559 operand_types[j].bitfield.disp32s = 0;
6560 operand_types[j].bitfield.disp64 = 0;
6561 break;
6562
6563 case CODE_64BIT:
6564 if (operand_types[j].bitfield.disp32s
6565 || operand_types[j].bitfield.disp64)
40fb9820 6566 {
48bcea9f
JB
6567 operand_types[j].bitfield.disp64 &= !override;
6568 operand_types[j].bitfield.disp32s &= !override;
6569 operand_types[j].bitfield.disp32 = override;
40fb9820 6570 }
48bcea9f
JB
6571 operand_types[j].bitfield.disp16 = 0;
6572 break;
40fb9820 6573 }
539e75ad 6574 }
48bcea9f 6575 }
539e75ad 6576
02a86693 6577 /* Force 0x8b encoding for "mov foo@GOT, %eax". */
35648716
JB
6578 if (i.reloc[0] == BFD_RELOC_386_GOT32
6579 && t->base_opcode == 0xa0
6580 && t->opcode_modifier.opcodespace == SPACE_BASE)
02a86693
L
6581 continue;
6582
56ffb741 6583 /* We check register size if needed. */
e2195274
JB
6584 if (t->opcode_modifier.checkregsize)
6585 {
6586 check_register = (1 << t->operands) - 1;
5273a3cd
JB
6587 if (i.broadcast.type)
6588 check_register &= ~(1 << i.broadcast.operand);
e2195274
JB
6589 }
6590 else
6591 check_register = 0;
6592
c6fb90c8 6593 overlap0 = operand_type_and (i.types[0], operand_types[0]);
29b0f896
AM
6594 switch (t->operands)
6595 {
6596 case 1:
40fb9820 6597 if (!operand_type_match (overlap0, i.types[0]))
29b0f896
AM
6598 continue;
6599 break;
6600 case 2:
33eaf5de 6601 /* xchg %eax, %eax is a special case. It is an alias for nop
8b38ad71
L
6602 only in 32bit mode and we can use opcode 0x90. In 64bit
6603 mode, we can't use 0x90 for xchg %eax, %eax since it should
6604 zero-extend %eax to %rax. */
6605 if (flag_code == CODE_64BIT
6606 && t->base_opcode == 0x90
35648716 6607 && t->opcode_modifier.opcodespace == SPACE_BASE
75e5731b
JB
6608 && i.types[0].bitfield.instance == Accum
6609 && i.types[0].bitfield.dword
6610 && i.types[1].bitfield.instance == Accum
6611 && i.types[1].bitfield.dword)
8b38ad71 6612 continue;
1212781b
JB
6613 /* xrelease mov %eax, <disp> is another special case. It must not
6614 match the accumulator-only encoding of mov. */
6615 if (flag_code != CODE_64BIT
6616 && i.hle_prefix
6617 && t->base_opcode == 0xa0
35648716 6618 && t->opcode_modifier.opcodespace == SPACE_BASE
75e5731b 6619 && i.types[0].bitfield.instance == Accum
8dc0818e 6620 && (i.flags[1] & Operand_Mem))
1212781b 6621 continue;
f5eb1d70
JB
6622 /* Fall through. */
6623
6624 case 3:
3ac21baa
JB
6625 if (!(size_match & MATCH_STRAIGHT))
6626 goto check_reverse;
64c49ab3
JB
6627 /* Reverse direction of operands if swapping is possible in the first
6628 place (operands need to be symmetric) and
6629 - the load form is requested, and the template is a store form,
6630 - the store form is requested, and the template is a load form,
6631 - the non-default (swapped) form is requested. */
6632 overlap1 = operand_type_and (operand_types[0], operand_types[1]);
f5eb1d70 6633 if (t->opcode_modifier.d && i.reg_operands == i.operands
64c49ab3
JB
6634 && !operand_type_all_zero (&overlap1))
6635 switch (i.dir_encoding)
6636 {
6637 case dir_encoding_load:
6638 if (operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 6639 || t->opcode_modifier.regmem)
64c49ab3
JB
6640 goto check_reverse;
6641 break;
6642
6643 case dir_encoding_store:
6644 if (!operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 6645 && !t->opcode_modifier.regmem)
64c49ab3
JB
6646 goto check_reverse;
6647 break;
6648
6649 case dir_encoding_swap:
6650 goto check_reverse;
6651
6652 case dir_encoding_default:
6653 break;
6654 }
86fa6981 6655 /* If we want store form, we skip the current load. */
64c49ab3
JB
6656 if ((i.dir_encoding == dir_encoding_store
6657 || i.dir_encoding == dir_encoding_swap)
86fa6981
L
6658 && i.mem_operands == 0
6659 && t->opcode_modifier.load)
fa99fab2 6660 continue;
1a0670f3 6661 /* Fall through. */
f48ff2ae 6662 case 4:
c0f3af97 6663 case 5:
c6fb90c8 6664 overlap1 = operand_type_and (i.types[1], operand_types[1]);
40fb9820
L
6665 if (!operand_type_match (overlap0, i.types[0])
6666 || !operand_type_match (overlap1, i.types[1])
e2195274 6667 || ((check_register & 3) == 3
dc821c5f 6668 && !operand_type_register_match (i.types[0],
40fb9820 6669 operand_types[0],
dc821c5f 6670 i.types[1],
40fb9820 6671 operand_types[1])))
29b0f896
AM
6672 {
6673 /* Check if other direction is valid ... */
38e314eb 6674 if (!t->opcode_modifier.d)
29b0f896
AM
6675 continue;
6676
dc1e8a47 6677 check_reverse:
3ac21baa
JB
6678 if (!(size_match & MATCH_REVERSE))
6679 continue;
29b0f896 6680 /* Try reversing direction of operands. */
f5eb1d70
JB
6681 overlap0 = operand_type_and (i.types[0], operand_types[i.operands - 1]);
6682 overlap1 = operand_type_and (i.types[i.operands - 1], operand_types[0]);
40fb9820 6683 if (!operand_type_match (overlap0, i.types[0])
f5eb1d70 6684 || !operand_type_match (overlap1, i.types[i.operands - 1])
45664ddb 6685 || (check_register
dc821c5f 6686 && !operand_type_register_match (i.types[0],
f5eb1d70
JB
6687 operand_types[i.operands - 1],
6688 i.types[i.operands - 1],
45664ddb 6689 operand_types[0])))
29b0f896
AM
6690 {
6691 /* Does not match either direction. */
6692 continue;
6693 }
38e314eb 6694 /* found_reverse_match holds which of D or FloatR
29b0f896 6695 we've found. */
38e314eb
JB
6696 if (!t->opcode_modifier.d)
6697 found_reverse_match = 0;
6698 else if (operand_types[0].bitfield.tbyte)
8a2ed489 6699 found_reverse_match = Opcode_FloatD;
dbbc8b7e 6700 else if (operand_types[0].bitfield.xmmword
f5eb1d70 6701 || operand_types[i.operands - 1].bitfield.xmmword
3528c362
JB
6702 || operand_types[0].bitfield.class == RegMMX
6703 || operand_types[i.operands - 1].bitfield.class == RegMMX
dbbc8b7e
JB
6704 || is_any_vex_encoding(t))
6705 found_reverse_match = (t->base_opcode & 0xee) != 0x6e
6706 ? Opcode_SIMD_FloatD : Opcode_SIMD_IntD;
8a2ed489 6707 else
38e314eb 6708 found_reverse_match = Opcode_D;
40fb9820 6709 if (t->opcode_modifier.floatr)
8a2ed489 6710 found_reverse_match |= Opcode_FloatR;
29b0f896 6711 }
f48ff2ae 6712 else
29b0f896 6713 {
f48ff2ae 6714 /* Found a forward 2 operand match here. */
d1cbb4db
L
6715 switch (t->operands)
6716 {
c0f3af97
L
6717 case 5:
6718 overlap4 = operand_type_and (i.types[4],
6719 operand_types[4]);
1a0670f3 6720 /* Fall through. */
d1cbb4db 6721 case 4:
c6fb90c8
L
6722 overlap3 = operand_type_and (i.types[3],
6723 operand_types[3]);
1a0670f3 6724 /* Fall through. */
d1cbb4db 6725 case 3:
c6fb90c8
L
6726 overlap2 = operand_type_and (i.types[2],
6727 operand_types[2]);
d1cbb4db
L
6728 break;
6729 }
29b0f896 6730
f48ff2ae
L
6731 switch (t->operands)
6732 {
c0f3af97
L
6733 case 5:
6734 if (!operand_type_match (overlap4, i.types[4])
dc821c5f 6735 || !operand_type_register_match (i.types[3],
c0f3af97 6736 operand_types[3],
c0f3af97
L
6737 i.types[4],
6738 operand_types[4]))
6739 continue;
1a0670f3 6740 /* Fall through. */
f48ff2ae 6741 case 4:
40fb9820 6742 if (!operand_type_match (overlap3, i.types[3])
e2195274
JB
6743 || ((check_register & 0xa) == 0xa
6744 && !operand_type_register_match (i.types[1],
f7768225
JB
6745 operand_types[1],
6746 i.types[3],
e2195274
JB
6747 operand_types[3]))
6748 || ((check_register & 0xc) == 0xc
6749 && !operand_type_register_match (i.types[2],
6750 operand_types[2],
6751 i.types[3],
6752 operand_types[3])))
f48ff2ae 6753 continue;
1a0670f3 6754 /* Fall through. */
f48ff2ae
L
6755 case 3:
6756 /* Here we make use of the fact that there are no
23e42951 6757 reverse match 3 operand instructions. */
40fb9820 6758 if (!operand_type_match (overlap2, i.types[2])
e2195274
JB
6759 || ((check_register & 5) == 5
6760 && !operand_type_register_match (i.types[0],
23e42951
JB
6761 operand_types[0],
6762 i.types[2],
e2195274
JB
6763 operand_types[2]))
6764 || ((check_register & 6) == 6
6765 && !operand_type_register_match (i.types[1],
6766 operand_types[1],
6767 i.types[2],
6768 operand_types[2])))
f48ff2ae
L
6769 continue;
6770 break;
6771 }
29b0f896 6772 }
f48ff2ae 6773 /* Found either forward/reverse 2, 3 or 4 operand match here:
29b0f896
AM
6774 slip through to break. */
6775 }
c0f3af97 6776
da4977e0
JB
6777 /* Check if vector operands are valid. */
6778 if (check_VecOperands (t))
6779 {
6780 specific_error = i.error;
6781 continue;
6782 }
6783
6784 /* Check if VEX/EVEX encoding requirements can be satisfied. */
6785 if (VEX_check_encoding (t))
5614d22c
JB
6786 {
6787 specific_error = i.error;
6788 continue;
6789 }
a683cc34 6790
29b0f896
AM
6791 /* We've found a match; break out of loop. */
6792 break;
6793 }
6794
6795 if (t == current_templates->end)
6796 {
6797 /* We found no match. */
a65babc9 6798 const char *err_msg;
5614d22c 6799 switch (specific_error ? specific_error : i.error)
a65babc9
L
6800 {
6801 default:
6802 abort ();
86e026a4 6803 case operand_size_mismatch:
a65babc9
L
6804 err_msg = _("operand size mismatch");
6805 break;
6806 case operand_type_mismatch:
6807 err_msg = _("operand type mismatch");
6808 break;
6809 case register_type_mismatch:
6810 err_msg = _("register type mismatch");
6811 break;
6812 case number_of_operands_mismatch:
6813 err_msg = _("number of operands mismatch");
6814 break;
6815 case invalid_instruction_suffix:
6816 err_msg = _("invalid instruction suffix");
6817 break;
6818 case bad_imm4:
4a2608e3 6819 err_msg = _("constant doesn't fit in 4 bits");
a65babc9 6820 break;
a65babc9
L
6821 case unsupported_with_intel_mnemonic:
6822 err_msg = _("unsupported with Intel mnemonic");
6823 break;
6824 case unsupported_syntax:
6825 err_msg = _("unsupported syntax");
6826 break;
6827 case unsupported:
35262a23 6828 as_bad (_("unsupported instruction `%s'"),
10efe3f6
L
6829 current_templates->start->name);
6830 return NULL;
260cd341
LC
6831 case invalid_sib_address:
6832 err_msg = _("invalid SIB address");
6833 break;
6c30d220
L
6834 case invalid_vsib_address:
6835 err_msg = _("invalid VSIB address");
6836 break;
7bab8ab5
JB
6837 case invalid_vector_register_set:
6838 err_msg = _("mask, index, and destination registers must be distinct");
6839 break;
260cd341
LC
6840 case invalid_tmm_register_set:
6841 err_msg = _("all tmm registers must be distinct");
6842 break;
6c30d220
L
6843 case unsupported_vector_index_register:
6844 err_msg = _("unsupported vector index register");
6845 break;
43234a1e
L
6846 case unsupported_broadcast:
6847 err_msg = _("unsupported broadcast");
6848 break;
43234a1e
L
6849 case broadcast_needed:
6850 err_msg = _("broadcast is needed for operand of such type");
6851 break;
6852 case unsupported_masking:
6853 err_msg = _("unsupported masking");
6854 break;
6855 case mask_not_on_destination:
6856 err_msg = _("mask not on destination operand");
6857 break;
6858 case no_default_mask:
6859 err_msg = _("default mask isn't allowed");
6860 break;
6861 case unsupported_rc_sae:
6862 err_msg = _("unsupported static rounding/sae");
6863 break;
6864 case rc_sae_operand_not_last_imm:
6865 if (intel_syntax)
6866 err_msg = _("RC/SAE operand must precede immediate operands");
6867 else
6868 err_msg = _("RC/SAE operand must follow immediate operands");
6869 break;
6870 case invalid_register_operand:
6871 err_msg = _("invalid register operand");
6872 break;
a65babc9
L
6873 }
6874 as_bad (_("%s for `%s'"), err_msg,
891edac4 6875 current_templates->start->name);
fa99fab2 6876 return NULL;
29b0f896 6877 }
252b5132 6878
29b0f896
AM
6879 if (!quiet_warnings)
6880 {
6881 if (!intel_syntax
0cfa3eb3 6882 && (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE)))
6f2f06be 6883 as_warn (_("indirect %s without `*'"), t->name);
29b0f896 6884
40fb9820 6885 if (t->opcode_modifier.isprefix
3cd7f3e3 6886 && t->opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
6887 {
6888 /* Warn them that a data or address size prefix doesn't
6889 affect assembly of the next line of code. */
6890 as_warn (_("stand-alone `%s' prefix"), t->name);
6891 }
6892 }
6893
6894 /* Copy the template we found. */
9a182d04 6895 install_template (t);
539e75ad
L
6896
6897 if (addr_prefix_disp != -1)
6898 i.tm.operand_types[addr_prefix_disp]
6899 = operand_types[addr_prefix_disp];
6900
29b0f896
AM
6901 if (found_reverse_match)
6902 {
dfd69174
JB
6903 /* If we found a reverse match we must alter the opcode direction
6904 bit and clear/flip the regmem modifier one. found_reverse_match
6905 holds bits to change (different for int & float insns). */
29b0f896
AM
6906
6907 i.tm.base_opcode ^= found_reverse_match;
6908
f5eb1d70
JB
6909 i.tm.operand_types[0] = operand_types[i.operands - 1];
6910 i.tm.operand_types[i.operands - 1] = operand_types[0];
dfd69174
JB
6911
6912 /* Certain SIMD insns have their load forms specified in the opcode
6913 table, and hence we need to _set_ RegMem instead of clearing it.
6914 We need to avoid setting the bit though on insns like KMOVW. */
6915 i.tm.opcode_modifier.regmem
6916 = i.tm.opcode_modifier.modrm && i.tm.opcode_modifier.d
6917 && i.tm.operands > 2U - i.tm.opcode_modifier.sse2avx
6918 && !i.tm.opcode_modifier.regmem;
29b0f896
AM
6919 }
6920
fa99fab2 6921 return t;
29b0f896
AM
6922}
6923
6924static int
e3bb37b5 6925check_string (void)
29b0f896 6926{
51c8edf6
JB
6927 unsigned int es_op = i.tm.opcode_modifier.isstring - IS_STRING_ES_OP0;
6928 unsigned int op = i.tm.operand_types[0].bitfield.baseindex ? es_op : 0;
8dc0818e 6929
5e042380 6930 if (i.seg[op] != NULL && i.seg[op] != reg_es)
29b0f896 6931 {
51c8edf6
JB
6932 as_bad (_("`%s' operand %u must use `%ses' segment"),
6933 i.tm.name,
6934 intel_syntax ? i.tm.operands - es_op : es_op + 1,
6935 register_prefix);
6936 return 0;
29b0f896 6937 }
51c8edf6
JB
6938
6939 /* There's only ever one segment override allowed per instruction.
6940 This instruction possibly has a legal segment override on the
6941 second operand, so copy the segment to where non-string
6942 instructions store it, allowing common code. */
6943 i.seg[op] = i.seg[1];
6944
29b0f896
AM
6945 return 1;
6946}
6947
6948static int
543613e9 6949process_suffix (void)
29b0f896 6950{
5b7c81bd 6951 bool is_crc32 = false, is_movx = false;
8b65b895 6952
29b0f896
AM
6953 /* If matched instruction specifies an explicit instruction mnemonic
6954 suffix, use it. */
673fe0f0 6955 if (i.tm.opcode_modifier.size == SIZE16)
40fb9820 6956 i.suffix = WORD_MNEM_SUFFIX;
673fe0f0 6957 else if (i.tm.opcode_modifier.size == SIZE32)
40fb9820 6958 i.suffix = LONG_MNEM_SUFFIX;
673fe0f0 6959 else if (i.tm.opcode_modifier.size == SIZE64)
40fb9820 6960 i.suffix = QWORD_MNEM_SUFFIX;
13e600d0 6961 else if (i.reg_operands
c8f8eebc
JB
6962 && (i.operands > 1 || i.types[0].bitfield.class == Reg)
6963 && !i.tm.opcode_modifier.addrprefixopreg)
29b0f896 6964 {
65fca059 6965 unsigned int numop = i.operands;
389d00a5
JB
6966
6967 /* MOVSX/MOVZX */
6968 is_movx = (i.tm.opcode_modifier.opcodespace == SPACE_0F
6969 && (i.tm.base_opcode | 8) == 0xbe)
6970 || (i.tm.opcode_modifier.opcodespace == SPACE_BASE
6971 && i.tm.base_opcode == 0x63
6972 && i.tm.cpu_flags.bitfield.cpu64);
6973
8b65b895 6974 /* CRC32 */
389d00a5
JB
6975 is_crc32 = (i.tm.base_opcode == 0xf0
6976 && i.tm.opcode_modifier.opcodespace == SPACE_0F38
8b65b895 6977 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2);
65fca059
JB
6978
6979 /* movsx/movzx want only their source operand considered here, for the
6980 ambiguity checking below. The suffix will be replaced afterwards
6981 to represent the destination (register). */
389d00a5 6982 if (is_movx && (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63))
65fca059
JB
6983 --i.operands;
6984
643bb870 6985 /* crc32 needs REX.W set regardless of suffix / source operand size. */
8b65b895 6986 if (is_crc32 && i.tm.operand_types[1].bitfield.qword)
643bb870
JB
6987 i.rex |= REX_W;
6988
29b0f896 6989 /* If there's no instruction mnemonic suffix we try to invent one
13e600d0 6990 based on GPR operands. */
29b0f896
AM
6991 if (!i.suffix)
6992 {
6993 /* We take i.suffix from the last register operand specified,
6994 Destination register type is more significant than source
381d071f
L
6995 register type. crc32 in SSE4.2 prefers source register
6996 type. */
8b65b895 6997 unsigned int op = is_crc32 ? 1 : i.operands;
20592a94 6998
1a035124
JB
6999 while (op--)
7000 if (i.tm.operand_types[op].bitfield.instance == InstanceNone
7001 || i.tm.operand_types[op].bitfield.instance == Accum)
7002 {
7003 if (i.types[op].bitfield.class != Reg)
7004 continue;
7005 if (i.types[op].bitfield.byte)
7006 i.suffix = BYTE_MNEM_SUFFIX;
7007 else if (i.types[op].bitfield.word)
7008 i.suffix = WORD_MNEM_SUFFIX;
7009 else if (i.types[op].bitfield.dword)
7010 i.suffix = LONG_MNEM_SUFFIX;
7011 else if (i.types[op].bitfield.qword)
7012 i.suffix = QWORD_MNEM_SUFFIX;
7013 else
7014 continue;
7015 break;
7016 }
65fca059
JB
7017
7018 /* As an exception, movsx/movzx silently default to a byte source
7019 in AT&T mode. */
389d00a5 7020 if (is_movx && i.tm.opcode_modifier.w && !i.suffix && !intel_syntax)
65fca059 7021 i.suffix = BYTE_MNEM_SUFFIX;
29b0f896
AM
7022 }
7023 else if (i.suffix == BYTE_MNEM_SUFFIX)
7024 {
2eb952a4 7025 if (intel_syntax
3cd7f3e3 7026 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
2eb952a4
L
7027 && i.tm.opcode_modifier.no_bsuf)
7028 i.suffix = 0;
7029 else if (!check_byte_reg ())
29b0f896
AM
7030 return 0;
7031 }
7032 else if (i.suffix == LONG_MNEM_SUFFIX)
7033 {
2eb952a4 7034 if (intel_syntax
3cd7f3e3 7035 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
9f123b91
JB
7036 && i.tm.opcode_modifier.no_lsuf
7037 && !i.tm.opcode_modifier.todword
7038 && !i.tm.opcode_modifier.toqword)
2eb952a4
L
7039 i.suffix = 0;
7040 else if (!check_long_reg ())
29b0f896
AM
7041 return 0;
7042 }
7043 else if (i.suffix == QWORD_MNEM_SUFFIX)
7044 {
955e1e6a 7045 if (intel_syntax
3cd7f3e3 7046 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
9f123b91
JB
7047 && i.tm.opcode_modifier.no_qsuf
7048 && !i.tm.opcode_modifier.todword
7049 && !i.tm.opcode_modifier.toqword)
955e1e6a
L
7050 i.suffix = 0;
7051 else if (!check_qword_reg ())
29b0f896
AM
7052 return 0;
7053 }
7054 else if (i.suffix == WORD_MNEM_SUFFIX)
7055 {
2eb952a4 7056 if (intel_syntax
3cd7f3e3 7057 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
2eb952a4
L
7058 && i.tm.opcode_modifier.no_wsuf)
7059 i.suffix = 0;
7060 else if (!check_word_reg ())
29b0f896
AM
7061 return 0;
7062 }
3cd7f3e3
L
7063 else if (intel_syntax
7064 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
7065 /* Do nothing if the instruction is going to ignore the prefix. */
7066 ;
7067 else
7068 abort ();
65fca059
JB
7069
7070 /* Undo the movsx/movzx change done above. */
7071 i.operands = numop;
29b0f896 7072 }
3cd7f3e3
L
7073 else if (i.tm.opcode_modifier.mnemonicsize == DEFAULTSIZE
7074 && !i.suffix)
29b0f896 7075 {
13e600d0
JB
7076 i.suffix = stackop_size;
7077 if (stackop_size == LONG_MNEM_SUFFIX)
06f74c5c
L
7078 {
7079 /* stackop_size is set to LONG_MNEM_SUFFIX for the
7080 .code16gcc directive to support 16-bit mode with
7081 32-bit address. For IRET without a suffix, generate
7082 16-bit IRET (opcode 0xcf) to return from an interrupt
7083 handler. */
13e600d0
JB
7084 if (i.tm.base_opcode == 0xcf)
7085 {
7086 i.suffix = WORD_MNEM_SUFFIX;
7087 as_warn (_("generating 16-bit `iret' for .code16gcc directive"));
7088 }
7089 /* Warn about changed behavior for segment register push/pop. */
7090 else if ((i.tm.base_opcode | 1) == 0x07)
7091 as_warn (_("generating 32-bit `%s', unlike earlier gas versions"),
7092 i.tm.name);
06f74c5c 7093 }
29b0f896 7094 }
c006a730 7095 else if (!i.suffix
0cfa3eb3
JB
7096 && (i.tm.opcode_modifier.jump == JUMP_ABSOLUTE
7097 || i.tm.opcode_modifier.jump == JUMP_BYTE
7098 || i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT
389d00a5
JB
7099 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
7100 && i.tm.base_opcode == 0x01 /* [ls][gi]dt */
64e74474 7101 && i.tm.extension_opcode <= 3)))
9306ca4a
JB
7102 {
7103 switch (flag_code)
7104 {
7105 case CODE_64BIT:
40fb9820 7106 if (!i.tm.opcode_modifier.no_qsuf)
9306ca4a 7107 {
828c2a25
JB
7108 if (i.tm.opcode_modifier.jump == JUMP_BYTE
7109 || i.tm.opcode_modifier.no_lsuf)
7110 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a
JB
7111 break;
7112 }
1a0670f3 7113 /* Fall through. */
9306ca4a 7114 case CODE_32BIT:
40fb9820 7115 if (!i.tm.opcode_modifier.no_lsuf)
9306ca4a
JB
7116 i.suffix = LONG_MNEM_SUFFIX;
7117 break;
7118 case CODE_16BIT:
40fb9820 7119 if (!i.tm.opcode_modifier.no_wsuf)
9306ca4a
JB
7120 i.suffix = WORD_MNEM_SUFFIX;
7121 break;
7122 }
7123 }
252b5132 7124
c006a730 7125 if (!i.suffix
3cd7f3e3 7126 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8
JB
7127 /* Also cover lret/retf/iret in 64-bit mode. */
7128 || (flag_code == CODE_64BIT
7129 && !i.tm.opcode_modifier.no_lsuf
7130 && !i.tm.opcode_modifier.no_qsuf))
3cd7f3e3 7131 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
8bbb3ad8
JB
7132 /* Explicit sizing prefixes are assumed to disambiguate insns. */
7133 && !i.prefix[DATA_PREFIX] && !(i.prefix[REX_PREFIX] & REX_W)
62b3f548
JB
7134 /* Accept FLDENV et al without suffix. */
7135 && (i.tm.opcode_modifier.no_ssuf || i.tm.opcode_modifier.floatmf))
29b0f896 7136 {
6c0946d0 7137 unsigned int suffixes, evex = 0;
c006a730
JB
7138
7139 suffixes = !i.tm.opcode_modifier.no_bsuf;
7140 if (!i.tm.opcode_modifier.no_wsuf)
7141 suffixes |= 1 << 1;
7142 if (!i.tm.opcode_modifier.no_lsuf)
7143 suffixes |= 1 << 2;
7144 if (!i.tm.opcode_modifier.no_ldsuf)
7145 suffixes |= 1 << 3;
7146 if (!i.tm.opcode_modifier.no_ssuf)
7147 suffixes |= 1 << 4;
7148 if (flag_code == CODE_64BIT && !i.tm.opcode_modifier.no_qsuf)
7149 suffixes |= 1 << 5;
7150
6c0946d0
JB
7151 /* For [XYZ]MMWORD operands inspect operand sizes. While generally
7152 also suitable for AT&T syntax mode, it was requested that this be
7153 restricted to just Intel syntax. */
5273a3cd 7154 if (intel_syntax && is_any_vex_encoding (&i.tm) && !i.broadcast.type)
6c0946d0 7155 {
b9915cbc 7156 unsigned int op;
6c0946d0 7157
b9915cbc 7158 for (op = 0; op < i.tm.operands; ++op)
6c0946d0 7159 {
b9915cbc
JB
7160 if (is_evex_encoding (&i.tm)
7161 && !cpu_arch_flags.bitfield.cpuavx512vl)
6c0946d0 7162 {
b9915cbc
JB
7163 if (i.tm.operand_types[op].bitfield.ymmword)
7164 i.tm.operand_types[op].bitfield.xmmword = 0;
7165 if (i.tm.operand_types[op].bitfield.zmmword)
7166 i.tm.operand_types[op].bitfield.ymmword = 0;
7167 if (!i.tm.opcode_modifier.evex
7168 || i.tm.opcode_modifier.evex == EVEXDYN)
7169 i.tm.opcode_modifier.evex = EVEX512;
7170 }
6c0946d0 7171
b9915cbc
JB
7172 if (i.tm.operand_types[op].bitfield.xmmword
7173 + i.tm.operand_types[op].bitfield.ymmword
7174 + i.tm.operand_types[op].bitfield.zmmword < 2)
7175 continue;
6c0946d0 7176
b9915cbc
JB
7177 /* Any properly sized operand disambiguates the insn. */
7178 if (i.types[op].bitfield.xmmword
7179 || i.types[op].bitfield.ymmword
7180 || i.types[op].bitfield.zmmword)
7181 {
7182 suffixes &= ~(7 << 6);
7183 evex = 0;
7184 break;
7185 }
6c0946d0 7186
b9915cbc
JB
7187 if ((i.flags[op] & Operand_Mem)
7188 && i.tm.operand_types[op].bitfield.unspecified)
7189 {
7190 if (i.tm.operand_types[op].bitfield.xmmword)
7191 suffixes |= 1 << 6;
7192 if (i.tm.operand_types[op].bitfield.ymmword)
7193 suffixes |= 1 << 7;
7194 if (i.tm.operand_types[op].bitfield.zmmword)
7195 suffixes |= 1 << 8;
7196 if (is_evex_encoding (&i.tm))
7197 evex = EVEX512;
6c0946d0
JB
7198 }
7199 }
7200 }
7201
7202 /* Are multiple suffixes / operand sizes allowed? */
c006a730 7203 if (suffixes & (suffixes - 1))
9306ca4a 7204 {
873494c8 7205 if (intel_syntax
3cd7f3e3 7206 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8 7207 || operand_check == check_error))
9306ca4a 7208 {
c006a730 7209 as_bad (_("ambiguous operand size for `%s'"), i.tm.name);
9306ca4a
JB
7210 return 0;
7211 }
c006a730 7212 if (operand_check == check_error)
9306ca4a 7213 {
c006a730
JB
7214 as_bad (_("no instruction mnemonic suffix given and "
7215 "no register operands; can't size `%s'"), i.tm.name);
9306ca4a
JB
7216 return 0;
7217 }
c006a730 7218 if (operand_check == check_warning)
873494c8
JB
7219 as_warn (_("%s; using default for `%s'"),
7220 intel_syntax
7221 ? _("ambiguous operand size")
7222 : _("no instruction mnemonic suffix given and "
7223 "no register operands"),
7224 i.tm.name);
c006a730
JB
7225
7226 if (i.tm.opcode_modifier.floatmf)
7227 i.suffix = SHORT_MNEM_SUFFIX;
389d00a5 7228 else if (is_movx)
65fca059 7229 /* handled below */;
6c0946d0
JB
7230 else if (evex)
7231 i.tm.opcode_modifier.evex = evex;
c006a730
JB
7232 else if (flag_code == CODE_16BIT)
7233 i.suffix = WORD_MNEM_SUFFIX;
1a035124 7234 else if (!i.tm.opcode_modifier.no_lsuf)
c006a730 7235 i.suffix = LONG_MNEM_SUFFIX;
1a035124
JB
7236 else
7237 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a 7238 }
29b0f896 7239 }
252b5132 7240
389d00a5 7241 if (is_movx)
65fca059
JB
7242 {
7243 /* In Intel syntax, movsx/movzx must have a "suffix" (checked above).
7244 In AT&T syntax, if there is no suffix (warned about above), the default
7245 will be byte extension. */
7246 if (i.tm.opcode_modifier.w && i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
7247 i.tm.base_opcode |= 1;
7248
7249 /* For further processing, the suffix should represent the destination
7250 (register). This is already the case when one was used with
7251 mov[sz][bw]*, but we need to replace it for mov[sz]x, or if there was
7252 no suffix to begin with. */
7253 if (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63 || !i.suffix)
7254 {
7255 if (i.types[1].bitfield.word)
7256 i.suffix = WORD_MNEM_SUFFIX;
7257 else if (i.types[1].bitfield.qword)
7258 i.suffix = QWORD_MNEM_SUFFIX;
7259 else
7260 i.suffix = LONG_MNEM_SUFFIX;
7261
7262 i.tm.opcode_modifier.w = 0;
7263 }
7264 }
7265
50128d0c
JB
7266 if (!i.tm.opcode_modifier.modrm && i.reg_operands && i.tm.operands < 3)
7267 i.short_form = (i.tm.operand_types[0].bitfield.class == Reg)
7268 != (i.tm.operand_types[1].bitfield.class == Reg);
7269
d2224064
JB
7270 /* Change the opcode based on the operand size given by i.suffix. */
7271 switch (i.suffix)
29b0f896 7272 {
d2224064
JB
7273 /* Size floating point instruction. */
7274 case LONG_MNEM_SUFFIX:
7275 if (i.tm.opcode_modifier.floatmf)
7276 {
7277 i.tm.base_opcode ^= 4;
7278 break;
7279 }
7280 /* fall through */
7281 case WORD_MNEM_SUFFIX:
7282 case QWORD_MNEM_SUFFIX:
29b0f896 7283 /* It's not a byte, select word/dword operation. */
40fb9820 7284 if (i.tm.opcode_modifier.w)
29b0f896 7285 {
50128d0c 7286 if (i.short_form)
29b0f896
AM
7287 i.tm.base_opcode |= 8;
7288 else
7289 i.tm.base_opcode |= 1;
7290 }
d2224064
JB
7291 /* fall through */
7292 case SHORT_MNEM_SUFFIX:
29b0f896
AM
7293 /* Now select between word & dword operations via the operand
7294 size prefix, except for instructions that will ignore this
7295 prefix anyway. */
c8f8eebc 7296 if (i.suffix != QWORD_MNEM_SUFFIX
3cd7f3e3 7297 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
c8f8eebc
JB
7298 && !i.tm.opcode_modifier.floatmf
7299 && !is_any_vex_encoding (&i.tm)
7300 && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
7301 || (flag_code == CODE_64BIT
7302 && i.tm.opcode_modifier.jump == JUMP_BYTE)))
24eab124
AM
7303 {
7304 unsigned int prefix = DATA_PREFIX_OPCODE;
543613e9 7305
0cfa3eb3 7306 if (i.tm.opcode_modifier.jump == JUMP_BYTE) /* jcxz, loop */
29b0f896 7307 prefix = ADDR_PREFIX_OPCODE;
252b5132 7308
29b0f896
AM
7309 if (!add_prefix (prefix))
7310 return 0;
24eab124 7311 }
252b5132 7312
29b0f896
AM
7313 /* Set mode64 for an operand. */
7314 if (i.suffix == QWORD_MNEM_SUFFIX
9146926a 7315 && flag_code == CODE_64BIT
d2224064 7316 && !i.tm.opcode_modifier.norex64
4ed21b58 7317 && !i.tm.opcode_modifier.vexw
46e883c5 7318 /* Special case for xchg %rax,%rax. It is NOP and doesn't
d2224064
JB
7319 need rex64. */
7320 && ! (i.operands == 2
7321 && i.tm.base_opcode == 0x90
7322 && i.tm.extension_opcode == None
75e5731b
JB
7323 && i.types[0].bitfield.instance == Accum
7324 && i.types[0].bitfield.qword
7325 && i.types[1].bitfield.instance == Accum
7326 && i.types[1].bitfield.qword))
d2224064 7327 i.rex |= REX_W;
3e73aa7c 7328
d2224064 7329 break;
8bbb3ad8
JB
7330
7331 case 0:
f9a6a8f0 7332 /* Select word/dword/qword operation with explicit data sizing prefix
8bbb3ad8
JB
7333 when there are no suitable register operands. */
7334 if (i.tm.opcode_modifier.w
7335 && (i.prefix[DATA_PREFIX] || (i.prefix[REX_PREFIX] & REX_W))
7336 && (!i.reg_operands
7337 || (i.reg_operands == 1
7338 /* ShiftCount */
7339 && (i.tm.operand_types[0].bitfield.instance == RegC
7340 /* InOutPortReg */
7341 || i.tm.operand_types[0].bitfield.instance == RegD
7342 || i.tm.operand_types[1].bitfield.instance == RegD
7343 /* CRC32 */
8b65b895 7344 || is_crc32))))
8bbb3ad8
JB
7345 i.tm.base_opcode |= 1;
7346 break;
29b0f896 7347 }
7ecd2f8b 7348
c8f8eebc 7349 if (i.tm.opcode_modifier.addrprefixopreg)
c0a30a9f 7350 {
c8f8eebc
JB
7351 gas_assert (!i.suffix);
7352 gas_assert (i.reg_operands);
c0a30a9f 7353
c8f8eebc
JB
7354 if (i.tm.operand_types[0].bitfield.instance == Accum
7355 || i.operands == 1)
7356 {
7357 /* The address size override prefix changes the size of the
7358 first operand. */
7359 if (flag_code == CODE_64BIT
7360 && i.op[0].regs->reg_type.bitfield.word)
7361 {
7362 as_bad (_("16-bit addressing unavailable for `%s'"),
7363 i.tm.name);
7364 return 0;
7365 }
7366
7367 if ((flag_code == CODE_32BIT
7368 ? i.op[0].regs->reg_type.bitfield.word
7369 : i.op[0].regs->reg_type.bitfield.dword)
7370 && !add_prefix (ADDR_PREFIX_OPCODE))
7371 return 0;
7372 }
c0a30a9f
L
7373 else
7374 {
c8f8eebc
JB
7375 /* Check invalid register operand when the address size override
7376 prefix changes the size of register operands. */
7377 unsigned int op;
7378 enum { need_word, need_dword, need_qword } need;
7379
27f13469 7380 /* Check the register operand for the address size prefix if
b3a3496f 7381 the memory operand has no real registers, like symbol, DISP
829f3fe1 7382 or bogus (x32-only) symbol(%rip) when symbol(%eip) is meant. */
27f13469
L
7383 if (i.mem_operands == 1
7384 && i.reg_operands == 1
7385 && i.operands == 2
27f13469 7386 && i.types[1].bitfield.class == Reg
b3a3496f
L
7387 && (flag_code == CODE_32BIT
7388 ? i.op[1].regs->reg_type.bitfield.word
7389 : i.op[1].regs->reg_type.bitfield.dword)
7390 && ((i.base_reg == NULL && i.index_reg == NULL)
829f3fe1
JB
7391#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
7392 || (x86_elf_abi == X86_64_X32_ABI
7393 && i.base_reg
b3a3496f
L
7394 && i.base_reg->reg_num == RegIP
7395 && i.base_reg->reg_type.bitfield.qword))
829f3fe1
JB
7396#else
7397 || 0)
7398#endif
27f13469
L
7399 && !add_prefix (ADDR_PREFIX_OPCODE))
7400 return 0;
7401
c8f8eebc
JB
7402 if (flag_code == CODE_32BIT)
7403 need = i.prefix[ADDR_PREFIX] ? need_word : need_dword;
7404 else if (i.prefix[ADDR_PREFIX])
c0a30a9f
L
7405 need = need_dword;
7406 else
7407 need = flag_code == CODE_64BIT ? need_qword : need_word;
c0a30a9f 7408
c8f8eebc
JB
7409 for (op = 0; op < i.operands; op++)
7410 {
7411 if (i.types[op].bitfield.class != Reg)
7412 continue;
7413
7414 switch (need)
7415 {
7416 case need_word:
7417 if (i.op[op].regs->reg_type.bitfield.word)
7418 continue;
7419 break;
7420 case need_dword:
7421 if (i.op[op].regs->reg_type.bitfield.dword)
7422 continue;
7423 break;
7424 case need_qword:
7425 if (i.op[op].regs->reg_type.bitfield.qword)
7426 continue;
7427 break;
7428 }
7429
7430 as_bad (_("invalid register operand size for `%s'"),
7431 i.tm.name);
7432 return 0;
7433 }
7434 }
c0a30a9f
L
7435 }
7436
29b0f896
AM
7437 return 1;
7438}
3e73aa7c 7439
29b0f896 7440static int
543613e9 7441check_byte_reg (void)
29b0f896
AM
7442{
7443 int op;
543613e9 7444
29b0f896
AM
7445 for (op = i.operands; --op >= 0;)
7446 {
dc821c5f 7447 /* Skip non-register operands. */
bab6aec1 7448 if (i.types[op].bitfield.class != Reg)
dc821c5f
JB
7449 continue;
7450
29b0f896
AM
7451 /* If this is an eight bit register, it's OK. If it's the 16 or
7452 32 bit version of an eight bit register, we will just use the
7453 low portion, and that's OK too. */
dc821c5f 7454 if (i.types[op].bitfield.byte)
29b0f896
AM
7455 continue;
7456
5a819eb9 7457 /* I/O port address operands are OK too. */
75e5731b
JB
7458 if (i.tm.operand_types[op].bitfield.instance == RegD
7459 && i.tm.operand_types[op].bitfield.word)
5a819eb9
JB
7460 continue;
7461
9706160a 7462 /* crc32 only wants its source operand checked here. */
389d00a5
JB
7463 if (i.tm.base_opcode == 0xf0
7464 && i.tm.opcode_modifier.opcodespace == SPACE_0F38
8b65b895
L
7465 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2
7466 && op != 0)
9344ff29
L
7467 continue;
7468
29b0f896 7469 /* Any other register is bad. */
73c76375
JB
7470 as_bad (_("`%s%s' not allowed with `%s%c'"),
7471 register_prefix, i.op[op].regs->reg_name,
7472 i.tm.name, i.suffix);
7473 return 0;
29b0f896
AM
7474 }
7475 return 1;
7476}
7477
7478static int
e3bb37b5 7479check_long_reg (void)
29b0f896
AM
7480{
7481 int op;
7482
7483 for (op = i.operands; --op >= 0;)
dc821c5f 7484 /* Skip non-register operands. */
bab6aec1 7485 if (i.types[op].bitfield.class != Reg)
dc821c5f 7486 continue;
29b0f896
AM
7487 /* Reject eight bit registers, except where the template requires
7488 them. (eg. movzb) */
dc821c5f 7489 else if (i.types[op].bitfield.byte
bab6aec1 7490 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7491 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7492 && (i.tm.operand_types[op].bitfield.word
7493 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7494 {
a540244d
L
7495 as_bad (_("`%s%s' not allowed with `%s%c'"),
7496 register_prefix,
29b0f896
AM
7497 i.op[op].regs->reg_name,
7498 i.tm.name,
7499 i.suffix);
7500 return 0;
7501 }
be4c5e58
L
7502 /* Error if the e prefix on a general reg is missing. */
7503 else if (i.types[op].bitfield.word
bab6aec1 7504 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7505 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7506 && i.tm.operand_types[op].bitfield.dword)
29b0f896 7507 {
be4c5e58
L
7508 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7509 register_prefix, i.op[op].regs->reg_name,
7510 i.suffix);
7511 return 0;
252b5132 7512 }
e4630f71 7513 /* Warn if the r prefix on a general reg is present. */
dc821c5f 7514 else if (i.types[op].bitfield.qword
bab6aec1 7515 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7516 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7517 && i.tm.operand_types[op].bitfield.dword)
252b5132 7518 {
34828aad 7519 if (intel_syntax
65fca059 7520 && i.tm.opcode_modifier.toqword
3528c362 7521 && i.types[0].bitfield.class != RegSIMD)
34828aad 7522 {
ca61edf2 7523 /* Convert to QWORD. We want REX byte. */
34828aad
L
7524 i.suffix = QWORD_MNEM_SUFFIX;
7525 }
7526 else
7527 {
2b5d6a91 7528 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
34828aad
L
7529 register_prefix, i.op[op].regs->reg_name,
7530 i.suffix);
7531 return 0;
7532 }
29b0f896
AM
7533 }
7534 return 1;
7535}
252b5132 7536
29b0f896 7537static int
e3bb37b5 7538check_qword_reg (void)
29b0f896
AM
7539{
7540 int op;
252b5132 7541
29b0f896 7542 for (op = i.operands; --op >= 0; )
dc821c5f 7543 /* Skip non-register operands. */
bab6aec1 7544 if (i.types[op].bitfield.class != Reg)
dc821c5f 7545 continue;
29b0f896
AM
7546 /* Reject eight bit registers, except where the template requires
7547 them. (eg. movzb) */
dc821c5f 7548 else if (i.types[op].bitfield.byte
bab6aec1 7549 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7550 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7551 && (i.tm.operand_types[op].bitfield.word
7552 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7553 {
a540244d
L
7554 as_bad (_("`%s%s' not allowed with `%s%c'"),
7555 register_prefix,
29b0f896
AM
7556 i.op[op].regs->reg_name,
7557 i.tm.name,
7558 i.suffix);
7559 return 0;
7560 }
e4630f71 7561 /* Warn if the r prefix on a general reg is missing. */
dc821c5f
JB
7562 else if ((i.types[op].bitfield.word
7563 || i.types[op].bitfield.dword)
bab6aec1 7564 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7565 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7566 && i.tm.operand_types[op].bitfield.qword)
29b0f896
AM
7567 {
7568 /* Prohibit these changes in the 64bit mode, since the
7569 lowering is more complicated. */
34828aad 7570 if (intel_syntax
ca61edf2 7571 && i.tm.opcode_modifier.todword
3528c362 7572 && i.types[0].bitfield.class != RegSIMD)
34828aad 7573 {
ca61edf2 7574 /* Convert to DWORD. We don't want REX byte. */
34828aad
L
7575 i.suffix = LONG_MNEM_SUFFIX;
7576 }
7577 else
7578 {
2b5d6a91 7579 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
34828aad
L
7580 register_prefix, i.op[op].regs->reg_name,
7581 i.suffix);
7582 return 0;
7583 }
252b5132 7584 }
29b0f896
AM
7585 return 1;
7586}
252b5132 7587
29b0f896 7588static int
e3bb37b5 7589check_word_reg (void)
29b0f896
AM
7590{
7591 int op;
7592 for (op = i.operands; --op >= 0;)
dc821c5f 7593 /* Skip non-register operands. */
bab6aec1 7594 if (i.types[op].bitfield.class != Reg)
dc821c5f 7595 continue;
29b0f896
AM
7596 /* Reject eight bit registers, except where the template requires
7597 them. (eg. movzb) */
dc821c5f 7598 else if (i.types[op].bitfield.byte
bab6aec1 7599 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7600 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7601 && (i.tm.operand_types[op].bitfield.word
7602 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7603 {
a540244d
L
7604 as_bad (_("`%s%s' not allowed with `%s%c'"),
7605 register_prefix,
29b0f896
AM
7606 i.op[op].regs->reg_name,
7607 i.tm.name,
7608 i.suffix);
7609 return 0;
7610 }
9706160a
JB
7611 /* Error if the e or r prefix on a general reg is present. */
7612 else if ((i.types[op].bitfield.dword
dc821c5f 7613 || i.types[op].bitfield.qword)
bab6aec1 7614 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7615 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7616 && i.tm.operand_types[op].bitfield.word)
252b5132 7617 {
9706160a
JB
7618 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7619 register_prefix, i.op[op].regs->reg_name,
7620 i.suffix);
7621 return 0;
29b0f896
AM
7622 }
7623 return 1;
7624}
252b5132 7625
29b0f896 7626static int
40fb9820 7627update_imm (unsigned int j)
29b0f896 7628{
bc0844ae 7629 i386_operand_type overlap = i.types[j];
40fb9820
L
7630 if ((overlap.bitfield.imm8
7631 || overlap.bitfield.imm8s
7632 || overlap.bitfield.imm16
7633 || overlap.bitfield.imm32
7634 || overlap.bitfield.imm32s
7635 || overlap.bitfield.imm64)
0dfbf9d7
L
7636 && !operand_type_equal (&overlap, &imm8)
7637 && !operand_type_equal (&overlap, &imm8s)
7638 && !operand_type_equal (&overlap, &imm16)
7639 && !operand_type_equal (&overlap, &imm32)
7640 && !operand_type_equal (&overlap, &imm32s)
7641 && !operand_type_equal (&overlap, &imm64))
29b0f896
AM
7642 {
7643 if (i.suffix)
7644 {
40fb9820
L
7645 i386_operand_type temp;
7646
0dfbf9d7 7647 operand_type_set (&temp, 0);
7ab9ffdd 7648 if (i.suffix == BYTE_MNEM_SUFFIX)
40fb9820
L
7649 {
7650 temp.bitfield.imm8 = overlap.bitfield.imm8;
7651 temp.bitfield.imm8s = overlap.bitfield.imm8s;
7652 }
7653 else if (i.suffix == WORD_MNEM_SUFFIX)
7654 temp.bitfield.imm16 = overlap.bitfield.imm16;
7655 else if (i.suffix == QWORD_MNEM_SUFFIX)
7656 {
7657 temp.bitfield.imm64 = overlap.bitfield.imm64;
7658 temp.bitfield.imm32s = overlap.bitfield.imm32s;
7659 }
7660 else
7661 temp.bitfield.imm32 = overlap.bitfield.imm32;
7662 overlap = temp;
29b0f896 7663 }
0dfbf9d7
L
7664 else if (operand_type_equal (&overlap, &imm16_32_32s)
7665 || operand_type_equal (&overlap, &imm16_32)
7666 || operand_type_equal (&overlap, &imm16_32s))
29b0f896 7667 {
40fb9820 7668 if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
65da13b5 7669 overlap = imm16;
40fb9820 7670 else
65da13b5 7671 overlap = imm32s;
29b0f896 7672 }
8bbb3ad8
JB
7673 else if (i.prefix[REX_PREFIX] & REX_W)
7674 overlap = operand_type_and (overlap, imm32s);
7675 else if (i.prefix[DATA_PREFIX])
7676 overlap = operand_type_and (overlap,
7677 flag_code != CODE_16BIT ? imm16 : imm32);
0dfbf9d7
L
7678 if (!operand_type_equal (&overlap, &imm8)
7679 && !operand_type_equal (&overlap, &imm8s)
7680 && !operand_type_equal (&overlap, &imm16)
7681 && !operand_type_equal (&overlap, &imm32)
7682 && !operand_type_equal (&overlap, &imm32s)
7683 && !operand_type_equal (&overlap, &imm64))
29b0f896 7684 {
4eed87de
AM
7685 as_bad (_("no instruction mnemonic suffix given; "
7686 "can't determine immediate size"));
29b0f896
AM
7687 return 0;
7688 }
7689 }
40fb9820 7690 i.types[j] = overlap;
29b0f896 7691
40fb9820
L
7692 return 1;
7693}
7694
7695static int
7696finalize_imm (void)
7697{
bc0844ae 7698 unsigned int j, n;
29b0f896 7699
bc0844ae
L
7700 /* Update the first 2 immediate operands. */
7701 n = i.operands > 2 ? 2 : i.operands;
7702 if (n)
7703 {
7704 for (j = 0; j < n; j++)
7705 if (update_imm (j) == 0)
7706 return 0;
40fb9820 7707
bc0844ae
L
7708 /* The 3rd operand can't be immediate operand. */
7709 gas_assert (operand_type_check (i.types[2], imm) == 0);
7710 }
29b0f896
AM
7711
7712 return 1;
7713}
7714
7715static int
e3bb37b5 7716process_operands (void)
29b0f896
AM
7717{
7718 /* Default segment register this instruction will use for memory
7719 accesses. 0 means unknown. This is only for optimizing out
7720 unnecessary segment overrides. */
5e042380 7721 const reg_entry *default_seg = NULL;
29b0f896 7722
a5aeccd9
JB
7723 if (i.tm.opcode_modifier.sse2avx)
7724 {
7725 /* Legacy encoded insns allow explicit REX prefixes, so these prefixes
7726 need converting. */
7727 i.rex |= i.prefix[REX_PREFIX] & (REX_W | REX_R | REX_X | REX_B);
7728 i.prefix[REX_PREFIX] = 0;
7729 i.rex_encoding = 0;
7730 }
c423d21a
JB
7731 /* ImmExt should be processed after SSE2AVX. */
7732 else if (i.tm.opcode_modifier.immext)
7733 process_immext ();
a5aeccd9 7734
2426c15f 7735 if (i.tm.opcode_modifier.sse2avx && i.tm.opcode_modifier.vexvvvv)
29b0f896 7736 {
91d6fa6a
NC
7737 unsigned int dupl = i.operands;
7738 unsigned int dest = dupl - 1;
9fcfb3d7
L
7739 unsigned int j;
7740
c0f3af97 7741 /* The destination must be an xmm register. */
9c2799c2 7742 gas_assert (i.reg_operands
91d6fa6a 7743 && MAX_OPERANDS > dupl
7ab9ffdd 7744 && operand_type_equal (&i.types[dest], &regxmm));
c0f3af97 7745
75e5731b 7746 if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 7747 && i.tm.operand_types[0].bitfield.xmmword)
e2ec9d29 7748 {
8cd7925b 7749 if (i.tm.opcode_modifier.vexsources == VEX3SOURCES)
c0f3af97
L
7750 {
7751 /* Keep xmm0 for instructions with VEX prefix and 3
7752 sources. */
75e5731b 7753 i.tm.operand_types[0].bitfield.instance = InstanceNone;
3528c362 7754 i.tm.operand_types[0].bitfield.class = RegSIMD;
c0f3af97
L
7755 goto duplicate;
7756 }
e2ec9d29 7757 else
c0f3af97
L
7758 {
7759 /* We remove the first xmm0 and keep the number of
7760 operands unchanged, which in fact duplicates the
7761 destination. */
7762 for (j = 1; j < i.operands; j++)
7763 {
7764 i.op[j - 1] = i.op[j];
7765 i.types[j - 1] = i.types[j];
7766 i.tm.operand_types[j - 1] = i.tm.operand_types[j];
8dc0818e 7767 i.flags[j - 1] = i.flags[j];
c0f3af97
L
7768 }
7769 }
7770 }
7771 else if (i.tm.opcode_modifier.implicit1stxmm0)
7ab9ffdd 7772 {
91d6fa6a 7773 gas_assert ((MAX_OPERANDS - 1) > dupl
8cd7925b
L
7774 && (i.tm.opcode_modifier.vexsources
7775 == VEX3SOURCES));
c0f3af97
L
7776
7777 /* Add the implicit xmm0 for instructions with VEX prefix
7778 and 3 sources. */
7779 for (j = i.operands; j > 0; j--)
7780 {
7781 i.op[j] = i.op[j - 1];
7782 i.types[j] = i.types[j - 1];
7783 i.tm.operand_types[j] = i.tm.operand_types[j - 1];
8dc0818e 7784 i.flags[j] = i.flags[j - 1];
c0f3af97
L
7785 }
7786 i.op[0].regs
629310ab 7787 = (const reg_entry *) str_hash_find (reg_hash, "xmm0");
7ab9ffdd 7788 i.types[0] = regxmm;
c0f3af97
L
7789 i.tm.operand_types[0] = regxmm;
7790
7791 i.operands += 2;
7792 i.reg_operands += 2;
7793 i.tm.operands += 2;
7794
91d6fa6a 7795 dupl++;
c0f3af97 7796 dest++;
91d6fa6a
NC
7797 i.op[dupl] = i.op[dest];
7798 i.types[dupl] = i.types[dest];
7799 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 7800 i.flags[dupl] = i.flags[dest];
e2ec9d29 7801 }
c0f3af97
L
7802 else
7803 {
dc1e8a47 7804 duplicate:
c0f3af97
L
7805 i.operands++;
7806 i.reg_operands++;
7807 i.tm.operands++;
7808
91d6fa6a
NC
7809 i.op[dupl] = i.op[dest];
7810 i.types[dupl] = i.types[dest];
7811 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 7812 i.flags[dupl] = i.flags[dest];
c0f3af97
L
7813 }
7814
7815 if (i.tm.opcode_modifier.immext)
7816 process_immext ();
7817 }
75e5731b 7818 else if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 7819 && i.tm.operand_types[0].bitfield.xmmword)
c0f3af97
L
7820 {
7821 unsigned int j;
7822
9fcfb3d7
L
7823 for (j = 1; j < i.operands; j++)
7824 {
7825 i.op[j - 1] = i.op[j];
7826 i.types[j - 1] = i.types[j];
7827
7828 /* We need to adjust fields in i.tm since they are used by
7829 build_modrm_byte. */
7830 i.tm.operand_types [j - 1] = i.tm.operand_types [j];
8dc0818e
JB
7831
7832 i.flags[j - 1] = i.flags[j];
9fcfb3d7
L
7833 }
7834
e2ec9d29
L
7835 i.operands--;
7836 i.reg_operands--;
e2ec9d29
L
7837 i.tm.operands--;
7838 }
920d2ddc
IT
7839 else if (i.tm.opcode_modifier.implicitquadgroup)
7840 {
a477a8c4
JB
7841 unsigned int regnum, first_reg_in_group, last_reg_in_group;
7842
920d2ddc 7843 /* The second operand must be {x,y,z}mmN, where N is a multiple of 4. */
3528c362 7844 gas_assert (i.operands >= 2 && i.types[1].bitfield.class == RegSIMD);
a477a8c4
JB
7845 regnum = register_number (i.op[1].regs);
7846 first_reg_in_group = regnum & ~3;
7847 last_reg_in_group = first_reg_in_group + 3;
7848 if (regnum != first_reg_in_group)
7849 as_warn (_("source register `%s%s' implicitly denotes"
7850 " `%s%.3s%u' to `%s%.3s%u' source group in `%s'"),
7851 register_prefix, i.op[1].regs->reg_name,
7852 register_prefix, i.op[1].regs->reg_name, first_reg_in_group,
7853 register_prefix, i.op[1].regs->reg_name, last_reg_in_group,
7854 i.tm.name);
7855 }
e2ec9d29
L
7856 else if (i.tm.opcode_modifier.regkludge)
7857 {
7858 /* The imul $imm, %reg instruction is converted into
7859 imul $imm, %reg, %reg, and the clr %reg instruction
7860 is converted into xor %reg, %reg. */
7861
7862 unsigned int first_reg_op;
7863
7864 if (operand_type_check (i.types[0], reg))
7865 first_reg_op = 0;
7866 else
7867 first_reg_op = 1;
7868 /* Pretend we saw the extra register operand. */
9c2799c2 7869 gas_assert (i.reg_operands == 1
7ab9ffdd 7870 && i.op[first_reg_op + 1].regs == 0);
e2ec9d29
L
7871 i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
7872 i.types[first_reg_op + 1] = i.types[first_reg_op];
7873 i.operands++;
7874 i.reg_operands++;
29b0f896
AM
7875 }
7876
85b80b0f 7877 if (i.tm.opcode_modifier.modrm)
29b0f896
AM
7878 {
7879 /* The opcode is completed (modulo i.tm.extension_opcode which
52271982
AM
7880 must be put into the modrm byte). Now, we make the modrm and
7881 index base bytes based on all the info we've collected. */
29b0f896
AM
7882
7883 default_seg = build_modrm_byte ();
7884 }
00cee14f 7885 else if (i.types[0].bitfield.class == SReg)
85b80b0f
JB
7886 {
7887 if (flag_code != CODE_64BIT
7888 ? i.tm.base_opcode == POP_SEG_SHORT
7889 && i.op[0].regs->reg_num == 1
389d00a5 7890 : (i.tm.base_opcode | 1) == (POP_SEG386_SHORT & 0xff)
85b80b0f
JB
7891 && i.op[0].regs->reg_num < 4)
7892 {
7893 as_bad (_("you can't `%s %s%s'"),
7894 i.tm.name, register_prefix, i.op[0].regs->reg_name);
7895 return 0;
7896 }
389d00a5
JB
7897 if (i.op[0].regs->reg_num > 3
7898 && i.tm.opcode_modifier.opcodespace == SPACE_BASE )
85b80b0f 7899 {
389d00a5
JB
7900 i.tm.base_opcode ^= (POP_SEG_SHORT ^ POP_SEG386_SHORT) & 0xff;
7901 i.tm.opcode_modifier.opcodespace = SPACE_0F;
85b80b0f
JB
7902 }
7903 i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
7904 }
389d00a5
JB
7905 else if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
7906 && (i.tm.base_opcode & ~3) == MOV_AX_DISP32)
29b0f896 7907 {
5e042380 7908 default_seg = reg_ds;
29b0f896 7909 }
40fb9820 7910 else if (i.tm.opcode_modifier.isstring)
29b0f896
AM
7911 {
7912 /* For the string instructions that allow a segment override
7913 on one of their operands, the default segment is ds. */
5e042380 7914 default_seg = reg_ds;
29b0f896 7915 }
50128d0c 7916 else if (i.short_form)
85b80b0f
JB
7917 {
7918 /* The register or float register operand is in operand
7919 0 or 1. */
bab6aec1 7920 unsigned int op = i.tm.operand_types[0].bitfield.class != Reg;
85b80b0f
JB
7921
7922 /* Register goes in low 3 bits of opcode. */
7923 i.tm.base_opcode |= i.op[op].regs->reg_num;
7924 if ((i.op[op].regs->reg_flags & RegRex) != 0)
7925 i.rex |= REX_B;
7926 if (!quiet_warnings && i.tm.opcode_modifier.ugh)
7927 {
7928 /* Warn about some common errors, but press on regardless.
7929 The first case can be generated by gcc (<= 2.8.1). */
7930 if (i.operands == 2)
7931 {
7932 /* Reversed arguments on faddp, fsubp, etc. */
7933 as_warn (_("translating to `%s %s%s,%s%s'"), i.tm.name,
7934 register_prefix, i.op[!intel_syntax].regs->reg_name,
7935 register_prefix, i.op[intel_syntax].regs->reg_name);
7936 }
7937 else
7938 {
7939 /* Extraneous `l' suffix on fp insn. */
7940 as_warn (_("translating to `%s %s%s'"), i.tm.name,
7941 register_prefix, i.op[0].regs->reg_name);
7942 }
7943 }
7944 }
29b0f896 7945
514a8bb0 7946 if ((i.seg[0] || i.prefix[SEG_PREFIX])
514a8bb0 7947 && i.tm.base_opcode == 0x8d /* lea */
35648716 7948 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
514a8bb0 7949 && !is_any_vex_encoding(&i.tm))
92334ad2
JB
7950 {
7951 if (!quiet_warnings)
7952 as_warn (_("segment override on `%s' is ineffectual"), i.tm.name);
7953 if (optimize)
7954 {
7955 i.seg[0] = NULL;
7956 i.prefix[SEG_PREFIX] = 0;
7957 }
7958 }
52271982
AM
7959
7960 /* If a segment was explicitly specified, and the specified segment
b6773884
JB
7961 is neither the default nor the one already recorded from a prefix,
7962 use an opcode prefix to select it. If we never figured out what
7963 the default segment is, then default_seg will be zero at this
7964 point, and the specified segment prefix will always be used. */
7965 if (i.seg[0]
7966 && i.seg[0] != default_seg
5e042380 7967 && i386_seg_prefixes[i.seg[0]->reg_num] != i.prefix[SEG_PREFIX])
29b0f896 7968 {
5e042380 7969 if (!add_prefix (i386_seg_prefixes[i.seg[0]->reg_num]))
29b0f896
AM
7970 return 0;
7971 }
7972 return 1;
7973}
7974
a5aeccd9 7975static INLINE void set_rex_vrex (const reg_entry *r, unsigned int rex_bit,
5b7c81bd 7976 bool do_sse2avx)
a5aeccd9
JB
7977{
7978 if (r->reg_flags & RegRex)
7979 {
7980 if (i.rex & rex_bit)
7981 as_bad (_("same type of prefix used twice"));
7982 i.rex |= rex_bit;
7983 }
7984 else if (do_sse2avx && (i.rex & rex_bit) && i.vex.register_specifier)
7985 {
7986 gas_assert (i.vex.register_specifier == r);
7987 i.vex.register_specifier += 8;
7988 }
7989
7990 if (r->reg_flags & RegVRex)
7991 i.vrex |= rex_bit;
7992}
7993
5e042380 7994static const reg_entry *
e3bb37b5 7995build_modrm_byte (void)
29b0f896 7996{
5e042380 7997 const reg_entry *default_seg = NULL;
c0f3af97 7998 unsigned int source, dest;
8cd7925b 7999 int vex_3_sources;
c0f3af97 8000
8cd7925b 8001 vex_3_sources = i.tm.opcode_modifier.vexsources == VEX3SOURCES;
c0f3af97
L
8002 if (vex_3_sources)
8003 {
91d6fa6a 8004 unsigned int nds, reg_slot;
4c2c6516 8005 expressionS *exp;
c0f3af97 8006
6b8d3588 8007 dest = i.operands - 1;
c0f3af97 8008 nds = dest - 1;
922d8de8 8009
a683cc34 8010 /* There are 2 kinds of instructions:
bed3d976 8011 1. 5 operands: 4 register operands or 3 register operands
9d3bf266 8012 plus 1 memory operand plus one Imm4 operand, VexXDS, and
bed3d976 8013 VexW0 or VexW1. The destination must be either XMM, YMM or
43234a1e 8014 ZMM register.
bed3d976 8015 2. 4 operands: 4 register operands or 3 register operands
2f1bada2 8016 plus 1 memory operand, with VexXDS. */
922d8de8 8017 gas_assert ((i.reg_operands == 4
bed3d976
JB
8018 || (i.reg_operands == 3 && i.mem_operands == 1))
8019 && i.tm.opcode_modifier.vexvvvv == VEXXDS
dcd7e323 8020 && i.tm.opcode_modifier.vexw
3528c362 8021 && i.tm.operand_types[dest].bitfield.class == RegSIMD);
a683cc34 8022
48db9223
JB
8023 /* If VexW1 is set, the first non-immediate operand is the source and
8024 the second non-immediate one is encoded in the immediate operand. */
8025 if (i.tm.opcode_modifier.vexw == VEXW1)
8026 {
8027 source = i.imm_operands;
8028 reg_slot = i.imm_operands + 1;
8029 }
8030 else
8031 {
8032 source = i.imm_operands + 1;
8033 reg_slot = i.imm_operands;
8034 }
8035
a683cc34 8036 if (i.imm_operands == 0)
bed3d976
JB
8037 {
8038 /* When there is no immediate operand, generate an 8bit
8039 immediate operand to encode the first operand. */
8040 exp = &im_expressions[i.imm_operands++];
8041 i.op[i.operands].imms = exp;
8042 i.types[i.operands] = imm8;
8043 i.operands++;
8044
3528c362 8045 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
bed3d976
JB
8046 exp->X_op = O_constant;
8047 exp->X_add_number = register_number (i.op[reg_slot].regs) << 4;
43234a1e
L
8048 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
8049 }
922d8de8 8050 else
bed3d976 8051 {
9d3bf266
JB
8052 gas_assert (i.imm_operands == 1);
8053 gas_assert (fits_in_imm4 (i.op[0].imms->X_add_number));
8054 gas_assert (!i.tm.opcode_modifier.immext);
a683cc34 8055
9d3bf266
JB
8056 /* Turn on Imm8 again so that output_imm will generate it. */
8057 i.types[0].bitfield.imm8 = 1;
bed3d976 8058
3528c362 8059 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
9d3bf266 8060 i.op[0].imms->X_add_number
bed3d976 8061 |= register_number (i.op[reg_slot].regs) << 4;
43234a1e 8062 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
bed3d976 8063 }
a683cc34 8064
3528c362 8065 gas_assert (i.tm.operand_types[nds].bitfield.class == RegSIMD);
dae39acc 8066 i.vex.register_specifier = i.op[nds].regs;
c0f3af97
L
8067 }
8068 else
8069 source = dest = 0;
29b0f896
AM
8070
8071 /* i.reg_operands MUST be the number of real register operands;
c0f3af97
L
8072 implicit registers do not count. If there are 3 register
8073 operands, it must be a instruction with VexNDS. For a
8074 instruction with VexNDD, the destination register is encoded
8075 in VEX prefix. If there are 4 register operands, it must be
8076 a instruction with VEX prefix and 3 sources. */
7ab9ffdd
L
8077 if (i.mem_operands == 0
8078 && ((i.reg_operands == 2
2426c15f 8079 && i.tm.opcode_modifier.vexvvvv <= VEXXDS)
7ab9ffdd 8080 || (i.reg_operands == 3
2426c15f 8081 && i.tm.opcode_modifier.vexvvvv == VEXXDS)
7ab9ffdd 8082 || (i.reg_operands == 4 && vex_3_sources)))
29b0f896 8083 {
cab737b9
L
8084 switch (i.operands)
8085 {
8086 case 2:
8087 source = 0;
8088 break;
8089 case 3:
c81128dc
L
8090 /* When there are 3 operands, one of them may be immediate,
8091 which may be the first or the last operand. Otherwise,
c0f3af97
L
8092 the first operand must be shift count register (cl) or it
8093 is an instruction with VexNDS. */
9c2799c2 8094 gas_assert (i.imm_operands == 1
7ab9ffdd 8095 || (i.imm_operands == 0
2426c15f 8096 && (i.tm.opcode_modifier.vexvvvv == VEXXDS
75e5731b
JB
8097 || (i.types[0].bitfield.instance == RegC
8098 && i.types[0].bitfield.byte))));
40fb9820 8099 if (operand_type_check (i.types[0], imm)
75e5731b
JB
8100 || (i.types[0].bitfield.instance == RegC
8101 && i.types[0].bitfield.byte))
40fb9820
L
8102 source = 1;
8103 else
8104 source = 0;
cab737b9
L
8105 break;
8106 case 4:
368d64cc
L
8107 /* When there are 4 operands, the first two must be 8bit
8108 immediate operands. The source operand will be the 3rd
c0f3af97
L
8109 one.
8110
8111 For instructions with VexNDS, if the first operand
8112 an imm8, the source operand is the 2nd one. If the last
8113 operand is imm8, the source operand is the first one. */
9c2799c2 8114 gas_assert ((i.imm_operands == 2
7ab9ffdd
L
8115 && i.types[0].bitfield.imm8
8116 && i.types[1].bitfield.imm8)
2426c15f 8117 || (i.tm.opcode_modifier.vexvvvv == VEXXDS
7ab9ffdd
L
8118 && i.imm_operands == 1
8119 && (i.types[0].bitfield.imm8
43234a1e 8120 || i.types[i.operands - 1].bitfield.imm8
ca5312a2 8121 || i.rounding.type != rc_none)));
9f2670f2
L
8122 if (i.imm_operands == 2)
8123 source = 2;
8124 else
c0f3af97
L
8125 {
8126 if (i.types[0].bitfield.imm8)
8127 source = 1;
8128 else
8129 source = 0;
8130 }
c0f3af97
L
8131 break;
8132 case 5:
e771e7c9 8133 if (is_evex_encoding (&i.tm))
43234a1e
L
8134 {
8135 /* For EVEX instructions, when there are 5 operands, the
8136 first one must be immediate operand. If the second one
8137 is immediate operand, the source operand is the 3th
8138 one. If the last one is immediate operand, the source
8139 operand is the 2nd one. */
8140 gas_assert (i.imm_operands == 2
8141 && i.tm.opcode_modifier.sae
8142 && operand_type_check (i.types[0], imm));
8143 if (operand_type_check (i.types[1], imm))
8144 source = 2;
8145 else if (operand_type_check (i.types[4], imm))
8146 source = 1;
8147 else
8148 abort ();
8149 }
cab737b9
L
8150 break;
8151 default:
8152 abort ();
8153 }
8154
c0f3af97
L
8155 if (!vex_3_sources)
8156 {
8157 dest = source + 1;
8158
43234a1e
L
8159 /* RC/SAE operand could be between DEST and SRC. That happens
8160 when one operand is GPR and the other one is XMM/YMM/ZMM
8161 register. */
ca5312a2 8162 if (i.rounding.type != rc_none && i.rounding.operand == dest)
43234a1e
L
8163 dest++;
8164
2426c15f 8165 if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
c0f3af97 8166 {
43234a1e 8167 /* For instructions with VexNDS, the register-only source
c5d0745b 8168 operand must be a 32/64bit integer, XMM, YMM, ZMM, or mask
dfd69174 8169 register. It is encoded in VEX prefix. */
f12dc422
L
8170
8171 i386_operand_type op;
8172 unsigned int vvvv;
8173
c2ecccb3
L
8174 /* Swap two source operands if needed. */
8175 if (i.tm.opcode_modifier.swapsources)
f12dc422
L
8176 {
8177 vvvv = source;
8178 source = dest;
8179 }
8180 else
8181 vvvv = dest;
8182
8183 op = i.tm.operand_types[vvvv];
c0f3af97 8184 if ((dest + 1) >= i.operands
bab6aec1 8185 || ((op.bitfield.class != Reg
dc821c5f 8186 || (!op.bitfield.dword && !op.bitfield.qword))
3528c362 8187 && op.bitfield.class != RegSIMD
43234a1e 8188 && !operand_type_equal (&op, &regmask)))
c0f3af97 8189 abort ();
f12dc422 8190 i.vex.register_specifier = i.op[vvvv].regs;
c0f3af97
L
8191 dest++;
8192 }
8193 }
29b0f896
AM
8194
8195 i.rm.mode = 3;
dfd69174
JB
8196 /* One of the register operands will be encoded in the i.rm.reg
8197 field, the other in the combined i.rm.mode and i.rm.regmem
29b0f896
AM
8198 fields. If no form of this instruction supports a memory
8199 destination operand, then we assume the source operand may
8200 sometimes be a memory operand and so we need to store the
8201 destination in the i.rm.reg field. */
dfd69174 8202 if (!i.tm.opcode_modifier.regmem
40fb9820 8203 && operand_type_check (i.tm.operand_types[dest], anymem) == 0)
29b0f896
AM
8204 {
8205 i.rm.reg = i.op[dest].regs->reg_num;
8206 i.rm.regmem = i.op[source].regs->reg_num;
a5aeccd9 8207 set_rex_vrex (i.op[dest].regs, REX_R, i.tm.opcode_modifier.sse2avx);
5b7c81bd 8208 set_rex_vrex (i.op[source].regs, REX_B, false);
29b0f896
AM
8209 }
8210 else
8211 {
8212 i.rm.reg = i.op[source].regs->reg_num;
8213 i.rm.regmem = i.op[dest].regs->reg_num;
a5aeccd9 8214 set_rex_vrex (i.op[dest].regs, REX_B, i.tm.opcode_modifier.sse2avx);
5b7c81bd 8215 set_rex_vrex (i.op[source].regs, REX_R, false);
29b0f896 8216 }
e0c7f900 8217 if (flag_code != CODE_64BIT && (i.rex & REX_R))
c4a530c5 8218 {
4a5c67ed 8219 if (i.types[!i.tm.opcode_modifier.regmem].bitfield.class != RegCR)
c4a530c5 8220 abort ();
e0c7f900 8221 i.rex &= ~REX_R;
c4a530c5
JB
8222 add_prefix (LOCK_PREFIX_OPCODE);
8223 }
29b0f896
AM
8224 }
8225 else
8226 { /* If it's not 2 reg operands... */
c0f3af97
L
8227 unsigned int mem;
8228
29b0f896
AM
8229 if (i.mem_operands)
8230 {
8231 unsigned int fake_zero_displacement = 0;
99018f42 8232 unsigned int op;
4eed87de 8233
7ab9ffdd 8234 for (op = 0; op < i.operands; op++)
8dc0818e 8235 if (i.flags[op] & Operand_Mem)
7ab9ffdd 8236 break;
7ab9ffdd 8237 gas_assert (op < i.operands);
29b0f896 8238
63112cd6 8239 if (i.tm.opcode_modifier.sib)
6c30d220 8240 {
260cd341
LC
8241 /* The index register of VSIB shouldn't be RegIZ. */
8242 if (i.tm.opcode_modifier.sib != SIBMEM
8243 && i.index_reg->reg_num == RegIZ)
6c30d220
L
8244 abort ();
8245
8246 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8247 if (!i.base_reg)
8248 {
8249 i.sib.base = NO_BASE_REGISTER;
8250 i.sib.scale = i.log2_scale_factor;
8251 i.types[op].bitfield.disp8 = 0;
8252 i.types[op].bitfield.disp16 = 0;
8253 i.types[op].bitfield.disp64 = 0;
a9aabc23 8254 if (want_disp32 (&i.tm))
6c30d220
L
8255 {
8256 /* Must be 32 bit */
8257 i.types[op].bitfield.disp32 = 1;
8258 i.types[op].bitfield.disp32s = 0;
8259 }
8260 else
8261 {
8262 i.types[op].bitfield.disp32 = 0;
8263 i.types[op].bitfield.disp32s = 1;
8264 }
8265 }
260cd341
LC
8266
8267 /* Since the mandatory SIB always has index register, so
8268 the code logic remains unchanged. The non-mandatory SIB
8269 without index register is allowed and will be handled
8270 later. */
8271 if (i.index_reg)
8272 {
8273 if (i.index_reg->reg_num == RegIZ)
8274 i.sib.index = NO_INDEX_REGISTER;
8275 else
8276 i.sib.index = i.index_reg->reg_num;
5b7c81bd 8277 set_rex_vrex (i.index_reg, REX_X, false);
260cd341 8278 }
6c30d220
L
8279 }
8280
5e042380 8281 default_seg = reg_ds;
29b0f896
AM
8282
8283 if (i.base_reg == 0)
8284 {
8285 i.rm.mode = 0;
8286 if (!i.disp_operands)
9bb129e8 8287 fake_zero_displacement = 1;
29b0f896
AM
8288 if (i.index_reg == 0)
8289 {
73053c1f
JB
8290 i386_operand_type newdisp;
8291
260cd341
LC
8292 /* Both check for VSIB and mandatory non-vector SIB. */
8293 gas_assert (!i.tm.opcode_modifier.sib
8294 || i.tm.opcode_modifier.sib == SIBMEM);
29b0f896 8295 /* Operand is just <disp> */
20f0a1fc 8296 if (flag_code == CODE_64BIT)
29b0f896
AM
8297 {
8298 /* 64bit mode overwrites the 32bit absolute
8299 addressing by RIP relative addressing and
8300 absolute addressing is encoded by one of the
8301 redundant SIB forms. */
8302 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8303 i.sib.base = NO_BASE_REGISTER;
8304 i.sib.index = NO_INDEX_REGISTER;
a9aabc23 8305 newdisp = (want_disp32(&i.tm) ? disp32 : disp32s);
20f0a1fc 8306 }
fc225355
L
8307 else if ((flag_code == CODE_16BIT)
8308 ^ (i.prefix[ADDR_PREFIX] != 0))
20f0a1fc
NC
8309 {
8310 i.rm.regmem = NO_BASE_REGISTER_16;
73053c1f 8311 newdisp = disp16;
20f0a1fc
NC
8312 }
8313 else
8314 {
8315 i.rm.regmem = NO_BASE_REGISTER;
73053c1f 8316 newdisp = disp32;
29b0f896 8317 }
73053c1f
JB
8318 i.types[op] = operand_type_and_not (i.types[op], anydisp);
8319 i.types[op] = operand_type_or (i.types[op], newdisp);
29b0f896 8320 }
63112cd6 8321 else if (!i.tm.opcode_modifier.sib)
29b0f896 8322 {
6c30d220 8323 /* !i.base_reg && i.index_reg */
e968fc9b 8324 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8325 i.sib.index = NO_INDEX_REGISTER;
8326 else
8327 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8328 i.sib.base = NO_BASE_REGISTER;
8329 i.sib.scale = i.log2_scale_factor;
8330 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
40fb9820
L
8331 i.types[op].bitfield.disp8 = 0;
8332 i.types[op].bitfield.disp16 = 0;
8333 i.types[op].bitfield.disp64 = 0;
a9aabc23 8334 if (want_disp32 (&i.tm))
40fb9820
L
8335 {
8336 /* Must be 32 bit */
8337 i.types[op].bitfield.disp32 = 1;
8338 i.types[op].bitfield.disp32s = 0;
8339 }
29b0f896 8340 else
40fb9820
L
8341 {
8342 i.types[op].bitfield.disp32 = 0;
8343 i.types[op].bitfield.disp32s = 1;
8344 }
29b0f896 8345 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8346 i.rex |= REX_X;
29b0f896
AM
8347 }
8348 }
8349 /* RIP addressing for 64bit mode. */
e968fc9b 8350 else if (i.base_reg->reg_num == RegIP)
29b0f896 8351 {
63112cd6 8352 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896 8353 i.rm.regmem = NO_BASE_REGISTER;
40fb9820
L
8354 i.types[op].bitfield.disp8 = 0;
8355 i.types[op].bitfield.disp16 = 0;
8356 i.types[op].bitfield.disp32 = 0;
8357 i.types[op].bitfield.disp32s = 1;
8358 i.types[op].bitfield.disp64 = 0;
71903a11 8359 i.flags[op] |= Operand_PCrel;
20f0a1fc
NC
8360 if (! i.disp_operands)
8361 fake_zero_displacement = 1;
29b0f896 8362 }
dc821c5f 8363 else if (i.base_reg->reg_type.bitfield.word)
29b0f896 8364 {
63112cd6 8365 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896
AM
8366 switch (i.base_reg->reg_num)
8367 {
8368 case 3: /* (%bx) */
8369 if (i.index_reg == 0)
8370 i.rm.regmem = 7;
8371 else /* (%bx,%si) -> 0, or (%bx,%di) -> 1 */
8372 i.rm.regmem = i.index_reg->reg_num - 6;
8373 break;
8374 case 5: /* (%bp) */
5e042380 8375 default_seg = reg_ss;
29b0f896
AM
8376 if (i.index_reg == 0)
8377 {
8378 i.rm.regmem = 6;
40fb9820 8379 if (operand_type_check (i.types[op], disp) == 0)
29b0f896
AM
8380 {
8381 /* fake (%bp) into 0(%bp) */
41eb8e88 8382 if (i.disp_encoding == disp_encoding_16bit)
1a02d6b0
L
8383 i.types[op].bitfield.disp16 = 1;
8384 else
8385 i.types[op].bitfield.disp8 = 1;
252b5132 8386 fake_zero_displacement = 1;
29b0f896
AM
8387 }
8388 }
8389 else /* (%bp,%si) -> 2, or (%bp,%di) -> 3 */
8390 i.rm.regmem = i.index_reg->reg_num - 6 + 2;
8391 break;
8392 default: /* (%si) -> 4 or (%di) -> 5 */
8393 i.rm.regmem = i.base_reg->reg_num - 6 + 4;
8394 }
41eb8e88
L
8395 if (!fake_zero_displacement
8396 && !i.disp_operands
8397 && i.disp_encoding)
8398 {
8399 fake_zero_displacement = 1;
8400 if (i.disp_encoding == disp_encoding_8bit)
8401 i.types[op].bitfield.disp8 = 1;
8402 else
8403 i.types[op].bitfield.disp16 = 1;
8404 }
29b0f896
AM
8405 i.rm.mode = mode_from_disp_size (i.types[op]);
8406 }
8407 else /* i.base_reg and 32/64 bit mode */
8408 {
a9aabc23 8409 if (operand_type_check (i.types[op], disp))
40fb9820 8410 {
73053c1f
JB
8411 i.types[op].bitfield.disp16 = 0;
8412 i.types[op].bitfield.disp64 = 0;
a9aabc23 8413 if (!want_disp32 (&i.tm))
73053c1f
JB
8414 {
8415 i.types[op].bitfield.disp32 = 0;
8416 i.types[op].bitfield.disp32s = 1;
8417 }
40fb9820 8418 else
73053c1f
JB
8419 {
8420 i.types[op].bitfield.disp32 = 1;
8421 i.types[op].bitfield.disp32s = 0;
8422 }
40fb9820 8423 }
20f0a1fc 8424
63112cd6 8425 if (!i.tm.opcode_modifier.sib)
6c30d220 8426 i.rm.regmem = i.base_reg->reg_num;
29b0f896 8427 if ((i.base_reg->reg_flags & RegRex) != 0)
161a04f6 8428 i.rex |= REX_B;
29b0f896
AM
8429 i.sib.base = i.base_reg->reg_num;
8430 /* x86-64 ignores REX prefix bit here to avoid decoder
8431 complications. */
848930b2
JB
8432 if (!(i.base_reg->reg_flags & RegRex)
8433 && (i.base_reg->reg_num == EBP_REG_NUM
8434 || i.base_reg->reg_num == ESP_REG_NUM))
5e042380 8435 default_seg = reg_ss;
848930b2 8436 if (i.base_reg->reg_num == 5 && i.disp_operands == 0)
29b0f896 8437 {
848930b2 8438 fake_zero_displacement = 1;
1a02d6b0
L
8439 if (i.disp_encoding == disp_encoding_32bit)
8440 i.types[op].bitfield.disp32 = 1;
8441 else
8442 i.types[op].bitfield.disp8 = 1;
29b0f896
AM
8443 }
8444 i.sib.scale = i.log2_scale_factor;
8445 if (i.index_reg == 0)
8446 {
260cd341
LC
8447 /* Only check for VSIB. */
8448 gas_assert (i.tm.opcode_modifier.sib != VECSIB128
8449 && i.tm.opcode_modifier.sib != VECSIB256
8450 && i.tm.opcode_modifier.sib != VECSIB512);
8451
29b0f896
AM
8452 /* <disp>(%esp) becomes two byte modrm with no index
8453 register. We've already stored the code for esp
8454 in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
8455 Any base register besides %esp will not use the
8456 extra modrm byte. */
8457 i.sib.index = NO_INDEX_REGISTER;
29b0f896 8458 }
63112cd6 8459 else if (!i.tm.opcode_modifier.sib)
29b0f896 8460 {
e968fc9b 8461 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8462 i.sib.index = NO_INDEX_REGISTER;
8463 else
8464 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8465 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8466 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8467 i.rex |= REX_X;
29b0f896 8468 }
67a4f2b7
AO
8469
8470 if (i.disp_operands
8471 && (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
8472 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL))
8473 i.rm.mode = 0;
8474 else
a501d77e
L
8475 {
8476 if (!fake_zero_displacement
8477 && !i.disp_operands
8478 && i.disp_encoding)
8479 {
8480 fake_zero_displacement = 1;
8481 if (i.disp_encoding == disp_encoding_8bit)
8482 i.types[op].bitfield.disp8 = 1;
8483 else
8484 i.types[op].bitfield.disp32 = 1;
8485 }
8486 i.rm.mode = mode_from_disp_size (i.types[op]);
8487 }
29b0f896 8488 }
252b5132 8489
29b0f896
AM
8490 if (fake_zero_displacement)
8491 {
8492 /* Fakes a zero displacement assuming that i.types[op]
8493 holds the correct displacement size. */
8494 expressionS *exp;
8495
9c2799c2 8496 gas_assert (i.op[op].disps == 0);
29b0f896
AM
8497 exp = &disp_expressions[i.disp_operands++];
8498 i.op[op].disps = exp;
8499 exp->X_op = O_constant;
8500 exp->X_add_number = 0;
8501 exp->X_add_symbol = (symbolS *) 0;
8502 exp->X_op_symbol = (symbolS *) 0;
8503 }
c0f3af97
L
8504
8505 mem = op;
29b0f896 8506 }
c0f3af97
L
8507 else
8508 mem = ~0;
252b5132 8509
8c43a48b 8510 if (i.tm.opcode_modifier.vexsources == XOP2SOURCES)
5dd85c99
SP
8511 {
8512 if (operand_type_check (i.types[0], imm))
8513 i.vex.register_specifier = NULL;
8514 else
8515 {
8516 /* VEX.vvvv encodes one of the sources when the first
8517 operand is not an immediate. */
1ef99a7b 8518 if (i.tm.opcode_modifier.vexw == VEXW0)
5dd85c99
SP
8519 i.vex.register_specifier = i.op[0].regs;
8520 else
8521 i.vex.register_specifier = i.op[1].regs;
8522 }
8523
8524 /* Destination is a XMM register encoded in the ModRM.reg
8525 and VEX.R bit. */
8526 i.rm.reg = i.op[2].regs->reg_num;
8527 if ((i.op[2].regs->reg_flags & RegRex) != 0)
8528 i.rex |= REX_R;
8529
8530 /* ModRM.rm and VEX.B encodes the other source. */
8531 if (!i.mem_operands)
8532 {
8533 i.rm.mode = 3;
8534
1ef99a7b 8535 if (i.tm.opcode_modifier.vexw == VEXW0)
5dd85c99
SP
8536 i.rm.regmem = i.op[1].regs->reg_num;
8537 else
8538 i.rm.regmem = i.op[0].regs->reg_num;
8539
8540 if ((i.op[1].regs->reg_flags & RegRex) != 0)
8541 i.rex |= REX_B;
8542 }
8543 }
2426c15f 8544 else if (i.tm.opcode_modifier.vexvvvv == VEXLWP)
f88c9eb0
SP
8545 {
8546 i.vex.register_specifier = i.op[2].regs;
8547 if (!i.mem_operands)
8548 {
8549 i.rm.mode = 3;
8550 i.rm.regmem = i.op[1].regs->reg_num;
8551 if ((i.op[1].regs->reg_flags & RegRex) != 0)
8552 i.rex |= REX_B;
8553 }
8554 }
29b0f896
AM
8555 /* Fill in i.rm.reg or i.rm.regmem field with register operand
8556 (if any) based on i.tm.extension_opcode. Again, we must be
8557 careful to make sure that segment/control/debug/test/MMX
8558 registers are coded into the i.rm.reg field. */
f88c9eb0 8559 else if (i.reg_operands)
29b0f896 8560 {
99018f42 8561 unsigned int op;
7ab9ffdd
L
8562 unsigned int vex_reg = ~0;
8563
8564 for (op = 0; op < i.operands; op++)
921eafea
L
8565 if (i.types[op].bitfield.class == Reg
8566 || i.types[op].bitfield.class == RegBND
8567 || i.types[op].bitfield.class == RegMask
8568 || i.types[op].bitfield.class == SReg
8569 || i.types[op].bitfield.class == RegCR
8570 || i.types[op].bitfield.class == RegDR
8571 || i.types[op].bitfield.class == RegTR
8572 || i.types[op].bitfield.class == RegSIMD
8573 || i.types[op].bitfield.class == RegMMX)
8574 break;
c0209578 8575
7ab9ffdd
L
8576 if (vex_3_sources)
8577 op = dest;
2426c15f 8578 else if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
7ab9ffdd
L
8579 {
8580 /* For instructions with VexNDS, the register-only
8581 source operand is encoded in VEX prefix. */
8582 gas_assert (mem != (unsigned int) ~0);
c0f3af97 8583
7ab9ffdd 8584 if (op > mem)
c0f3af97 8585 {
7ab9ffdd
L
8586 vex_reg = op++;
8587 gas_assert (op < i.operands);
c0f3af97
L
8588 }
8589 else
c0f3af97 8590 {
f12dc422
L
8591 /* Check register-only source operand when two source
8592 operands are swapped. */
8593 if (!i.tm.operand_types[op].bitfield.baseindex
8594 && i.tm.operand_types[op + 1].bitfield.baseindex)
8595 {
8596 vex_reg = op;
8597 op += 2;
8598 gas_assert (mem == (vex_reg + 1)
8599 && op < i.operands);
8600 }
8601 else
8602 {
8603 vex_reg = op + 1;
8604 gas_assert (vex_reg < i.operands);
8605 }
c0f3af97 8606 }
7ab9ffdd 8607 }
2426c15f 8608 else if (i.tm.opcode_modifier.vexvvvv == VEXNDD)
7ab9ffdd 8609 {
f12dc422 8610 /* For instructions with VexNDD, the register destination
7ab9ffdd 8611 is encoded in VEX prefix. */
f12dc422
L
8612 if (i.mem_operands == 0)
8613 {
8614 /* There is no memory operand. */
8615 gas_assert ((op + 2) == i.operands);
8616 vex_reg = op + 1;
8617 }
8618 else
8d63c93e 8619 {
ed438a93
JB
8620 /* There are only 2 non-immediate operands. */
8621 gas_assert (op < i.imm_operands + 2
8622 && i.operands == i.imm_operands + 2);
8623 vex_reg = i.imm_operands + 1;
f12dc422 8624 }
7ab9ffdd
L
8625 }
8626 else
8627 gas_assert (op < i.operands);
99018f42 8628
7ab9ffdd
L
8629 if (vex_reg != (unsigned int) ~0)
8630 {
f12dc422 8631 i386_operand_type *type = &i.tm.operand_types[vex_reg];
7ab9ffdd 8632
bab6aec1 8633 if ((type->bitfield.class != Reg
dc821c5f 8634 || (!type->bitfield.dword && !type->bitfield.qword))
3528c362 8635 && type->bitfield.class != RegSIMD
43234a1e 8636 && !operand_type_equal (type, &regmask))
7ab9ffdd 8637 abort ();
f88c9eb0 8638
7ab9ffdd
L
8639 i.vex.register_specifier = i.op[vex_reg].regs;
8640 }
8641
1b9f0c97
L
8642 /* Don't set OP operand twice. */
8643 if (vex_reg != op)
7ab9ffdd 8644 {
1b9f0c97
L
8645 /* If there is an extension opcode to put here, the
8646 register number must be put into the regmem field. */
8647 if (i.tm.extension_opcode != None)
8648 {
8649 i.rm.regmem = i.op[op].regs->reg_num;
a5aeccd9
JB
8650 set_rex_vrex (i.op[op].regs, REX_B,
8651 i.tm.opcode_modifier.sse2avx);
1b9f0c97
L
8652 }
8653 else
8654 {
8655 i.rm.reg = i.op[op].regs->reg_num;
a5aeccd9
JB
8656 set_rex_vrex (i.op[op].regs, REX_R,
8657 i.tm.opcode_modifier.sse2avx);
1b9f0c97 8658 }
7ab9ffdd 8659 }
252b5132 8660
29b0f896
AM
8661 /* Now, if no memory operand has set i.rm.mode = 0, 1, 2 we
8662 must set it to 3 to indicate this is a register operand
8663 in the regmem field. */
8664 if (!i.mem_operands)
8665 i.rm.mode = 3;
8666 }
252b5132 8667
29b0f896 8668 /* Fill in i.rm.reg field with extension opcode (if any). */
c1e679ec 8669 if (i.tm.extension_opcode != None)
29b0f896
AM
8670 i.rm.reg = i.tm.extension_opcode;
8671 }
8672 return default_seg;
8673}
252b5132 8674
48ef937e
JB
8675static INLINE void
8676frag_opcode_byte (unsigned char byte)
8677{
8678 if (now_seg != absolute_section)
8679 FRAG_APPEND_1_CHAR (byte);
8680 else
8681 ++abs_section_offset;
8682}
8683
376cd056
JB
8684static unsigned int
8685flip_code16 (unsigned int code16)
8686{
8687 gas_assert (i.tm.operands == 1);
8688
8689 return !(i.prefix[REX_PREFIX] & REX_W)
8690 && (code16 ? i.tm.operand_types[0].bitfield.disp32
8691 || i.tm.operand_types[0].bitfield.disp32s
8692 : i.tm.operand_types[0].bitfield.disp16)
8693 ? CODE16 : 0;
8694}
8695
29b0f896 8696static void
e3bb37b5 8697output_branch (void)
29b0f896
AM
8698{
8699 char *p;
f8a5c266 8700 int size;
29b0f896
AM
8701 int code16;
8702 int prefix;
8703 relax_substateT subtype;
8704 symbolS *sym;
8705 offsetT off;
8706
48ef937e
JB
8707 if (now_seg == absolute_section)
8708 {
8709 as_bad (_("relaxable branches not supported in absolute section"));
8710 return;
8711 }
8712
f8a5c266 8713 code16 = flag_code == CODE_16BIT ? CODE16 : 0;
a501d77e 8714 size = i.disp_encoding == disp_encoding_32bit ? BIG : SMALL;
29b0f896
AM
8715
8716 prefix = 0;
8717 if (i.prefix[DATA_PREFIX] != 0)
252b5132 8718 {
29b0f896
AM
8719 prefix = 1;
8720 i.prefixes -= 1;
376cd056 8721 code16 ^= flip_code16(code16);
252b5132 8722 }
29b0f896
AM
8723 /* Pentium4 branch hints. */
8724 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8725 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
2f66722d 8726 {
29b0f896
AM
8727 prefix++;
8728 i.prefixes--;
8729 }
8730 if (i.prefix[REX_PREFIX] != 0)
8731 {
8732 prefix++;
8733 i.prefixes--;
2f66722d
AM
8734 }
8735
7e8b059b
L
8736 /* BND prefixed jump. */
8737 if (i.prefix[BND_PREFIX] != 0)
8738 {
6cb0a70e
JB
8739 prefix++;
8740 i.prefixes--;
7e8b059b
L
8741 }
8742
f2810fe0
JB
8743 if (i.prefixes != 0)
8744 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
29b0f896
AM
8745
8746 /* It's always a symbol; End frag & setup for relax.
8747 Make sure there is enough room in this frag for the largest
8748 instruction we may generate in md_convert_frag. This is 2
8749 bytes for the opcode and room for the prefix and largest
8750 displacement. */
8751 frag_grow (prefix + 2 + 4);
8752 /* Prefix and 1 opcode byte go in fr_fix. */
8753 p = frag_more (prefix + 1);
8754 if (i.prefix[DATA_PREFIX] != 0)
8755 *p++ = DATA_PREFIX_OPCODE;
8756 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
8757 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
8758 *p++ = i.prefix[SEG_PREFIX];
6cb0a70e
JB
8759 if (i.prefix[BND_PREFIX] != 0)
8760 *p++ = BND_PREFIX_OPCODE;
29b0f896
AM
8761 if (i.prefix[REX_PREFIX] != 0)
8762 *p++ = i.prefix[REX_PREFIX];
8763 *p = i.tm.base_opcode;
8764
8765 if ((unsigned char) *p == JUMP_PC_RELATIVE)
f8a5c266 8766 subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, size);
40fb9820 8767 else if (cpu_arch_flags.bitfield.cpui386)
f8a5c266 8768 subtype = ENCODE_RELAX_STATE (COND_JUMP, size);
29b0f896 8769 else
f8a5c266 8770 subtype = ENCODE_RELAX_STATE (COND_JUMP86, size);
29b0f896 8771 subtype |= code16;
3e73aa7c 8772
29b0f896
AM
8773 sym = i.op[0].disps->X_add_symbol;
8774 off = i.op[0].disps->X_add_number;
3e73aa7c 8775
29b0f896
AM
8776 if (i.op[0].disps->X_op != O_constant
8777 && i.op[0].disps->X_op != O_symbol)
3e73aa7c 8778 {
29b0f896
AM
8779 /* Handle complex expressions. */
8780 sym = make_expr_symbol (i.op[0].disps);
8781 off = 0;
8782 }
3e73aa7c 8783
29b0f896
AM
8784 /* 1 possible extra opcode + 4 byte displacement go in var part.
8785 Pass reloc in fr_var. */
d258b828 8786 frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
29b0f896 8787}
3e73aa7c 8788
bd7ab16b
L
8789#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8790/* Return TRUE iff PLT32 relocation should be used for branching to
8791 symbol S. */
8792
5b7c81bd 8793static bool
bd7ab16b
L
8794need_plt32_p (symbolS *s)
8795{
8796 /* PLT32 relocation is ELF only. */
8797 if (!IS_ELF)
5b7c81bd 8798 return false;
bd7ab16b 8799
a5def729
RO
8800#ifdef TE_SOLARIS
8801 /* Don't emit PLT32 relocation on Solaris: neither native linker nor
8802 krtld support it. */
5b7c81bd 8803 return false;
a5def729
RO
8804#endif
8805
bd7ab16b
L
8806 /* Since there is no need to prepare for PLT branch on x86-64, we
8807 can generate R_X86_64_PLT32, instead of R_X86_64_PC32, which can
8808 be used as a marker for 32-bit PC-relative branches. */
8809 if (!object_64bit)
5b7c81bd 8810 return false;
bd7ab16b 8811
44365e88 8812 if (s == NULL)
5b7c81bd 8813 return false;
44365e88 8814
bd7ab16b
L
8815 /* Weak or undefined symbol need PLT32 relocation. */
8816 if (S_IS_WEAK (s) || !S_IS_DEFINED (s))
5b7c81bd 8817 return true;
bd7ab16b
L
8818
8819 /* Non-global symbol doesn't need PLT32 relocation. */
8820 if (! S_IS_EXTERNAL (s))
5b7c81bd 8821 return false;
bd7ab16b
L
8822
8823 /* Other global symbols need PLT32 relocation. NB: Symbol with
8824 non-default visibilities are treated as normal global symbol
8825 so that PLT32 relocation can be used as a marker for 32-bit
8826 PC-relative branches. It is useful for linker relaxation. */
5b7c81bd 8827 return true;
bd7ab16b
L
8828}
8829#endif
8830
29b0f896 8831static void
e3bb37b5 8832output_jump (void)
29b0f896
AM
8833{
8834 char *p;
8835 int size;
3e02c1cc 8836 fixS *fixP;
bd7ab16b 8837 bfd_reloc_code_real_type jump_reloc = i.reloc[0];
29b0f896 8838
0cfa3eb3 8839 if (i.tm.opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
8840 {
8841 /* This is a loop or jecxz type instruction. */
8842 size = 1;
8843 if (i.prefix[ADDR_PREFIX] != 0)
8844 {
48ef937e 8845 frag_opcode_byte (ADDR_PREFIX_OPCODE);
29b0f896
AM
8846 i.prefixes -= 1;
8847 }
8848 /* Pentium4 branch hints. */
8849 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8850 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
8851 {
48ef937e 8852 frag_opcode_byte (i.prefix[SEG_PREFIX]);
29b0f896 8853 i.prefixes--;
3e73aa7c
JH
8854 }
8855 }
29b0f896
AM
8856 else
8857 {
8858 int code16;
3e73aa7c 8859
29b0f896
AM
8860 code16 = 0;
8861 if (flag_code == CODE_16BIT)
8862 code16 = CODE16;
3e73aa7c 8863
29b0f896
AM
8864 if (i.prefix[DATA_PREFIX] != 0)
8865 {
48ef937e 8866 frag_opcode_byte (DATA_PREFIX_OPCODE);
29b0f896 8867 i.prefixes -= 1;
376cd056 8868 code16 ^= flip_code16(code16);
29b0f896 8869 }
252b5132 8870
29b0f896
AM
8871 size = 4;
8872 if (code16)
8873 size = 2;
8874 }
9fcc94b6 8875
6cb0a70e
JB
8876 /* BND prefixed jump. */
8877 if (i.prefix[BND_PREFIX] != 0)
29b0f896 8878 {
48ef937e 8879 frag_opcode_byte (i.prefix[BND_PREFIX]);
29b0f896
AM
8880 i.prefixes -= 1;
8881 }
252b5132 8882
6cb0a70e 8883 if (i.prefix[REX_PREFIX] != 0)
7e8b059b 8884 {
48ef937e 8885 frag_opcode_byte (i.prefix[REX_PREFIX]);
7e8b059b
L
8886 i.prefixes -= 1;
8887 }
8888
f2810fe0
JB
8889 if (i.prefixes != 0)
8890 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
e0890092 8891
48ef937e
JB
8892 if (now_seg == absolute_section)
8893 {
9a182d04 8894 abs_section_offset += i.opcode_length + size;
48ef937e
JB
8895 return;
8896 }
8897
9a182d04
JB
8898 p = frag_more (i.opcode_length + size);
8899 switch (i.opcode_length)
42164a71
L
8900 {
8901 case 2:
8902 *p++ = i.tm.base_opcode >> 8;
1a0670f3 8903 /* Fall through. */
42164a71
L
8904 case 1:
8905 *p++ = i.tm.base_opcode;
8906 break;
8907 default:
8908 abort ();
8909 }
e0890092 8910
bd7ab16b
L
8911#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8912 if (size == 4
8913 && jump_reloc == NO_RELOC
8914 && need_plt32_p (i.op[0].disps->X_add_symbol))
8915 jump_reloc = BFD_RELOC_X86_64_PLT32;
8916#endif
8917
8918 jump_reloc = reloc (size, 1, 1, jump_reloc);
8919
3e02c1cc 8920 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
bd7ab16b 8921 i.op[0].disps, 1, jump_reloc);
3e02c1cc 8922
eb19308f
JB
8923 /* All jumps handled here are signed, but don't unconditionally use a
8924 signed limit check for 32 and 16 bit jumps as we want to allow wrap
8925 around at 4G (outside of 64-bit mode) and 64k (except for XBEGIN)
8926 respectively. */
8927 switch (size)
8928 {
8929 case 1:
8930 fixP->fx_signed = 1;
8931 break;
8932
8933 case 2:
8934 if (i.tm.base_opcode == 0xc7f8)
8935 fixP->fx_signed = 1;
8936 break;
8937
8938 case 4:
8939 if (flag_code == CODE_64BIT)
8940 fixP->fx_signed = 1;
8941 break;
8942 }
29b0f896 8943}
e0890092 8944
29b0f896 8945static void
e3bb37b5 8946output_interseg_jump (void)
29b0f896
AM
8947{
8948 char *p;
8949 int size;
8950 int prefix;
8951 int code16;
252b5132 8952
29b0f896
AM
8953 code16 = 0;
8954 if (flag_code == CODE_16BIT)
8955 code16 = CODE16;
a217f122 8956
29b0f896
AM
8957 prefix = 0;
8958 if (i.prefix[DATA_PREFIX] != 0)
8959 {
8960 prefix = 1;
8961 i.prefixes -= 1;
8962 code16 ^= CODE16;
8963 }
6cb0a70e
JB
8964
8965 gas_assert (!i.prefix[REX_PREFIX]);
252b5132 8966
29b0f896
AM
8967 size = 4;
8968 if (code16)
8969 size = 2;
252b5132 8970
f2810fe0
JB
8971 if (i.prefixes != 0)
8972 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
252b5132 8973
48ef937e
JB
8974 if (now_seg == absolute_section)
8975 {
8976 abs_section_offset += prefix + 1 + 2 + size;
8977 return;
8978 }
8979
29b0f896
AM
8980 /* 1 opcode; 2 segment; offset */
8981 p = frag_more (prefix + 1 + 2 + size);
3e73aa7c 8982
29b0f896
AM
8983 if (i.prefix[DATA_PREFIX] != 0)
8984 *p++ = DATA_PREFIX_OPCODE;
252b5132 8985
29b0f896
AM
8986 if (i.prefix[REX_PREFIX] != 0)
8987 *p++ = i.prefix[REX_PREFIX];
252b5132 8988
29b0f896
AM
8989 *p++ = i.tm.base_opcode;
8990 if (i.op[1].imms->X_op == O_constant)
8991 {
8992 offsetT n = i.op[1].imms->X_add_number;
252b5132 8993
29b0f896
AM
8994 if (size == 2
8995 && !fits_in_unsigned_word (n)
8996 && !fits_in_signed_word (n))
8997 {
8998 as_bad (_("16-bit jump out of range"));
8999 return;
9000 }
9001 md_number_to_chars (p, n, size);
9002 }
9003 else
9004 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
d258b828 9005 i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
6d96a594
C
9006
9007 p += size;
9008 if (i.op[0].imms->X_op == O_constant)
9009 md_number_to_chars (p, (valueT) i.op[0].imms->X_add_number, 2);
9010 else
9011 fix_new_exp (frag_now, p - frag_now->fr_literal, 2,
9012 i.op[0].imms, 0, reloc (2, 0, 0, i.reloc[0]));
29b0f896 9013}
a217f122 9014
b4a3a7b4
L
9015#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9016void
9017x86_cleanup (void)
9018{
9019 char *p;
9020 asection *seg = now_seg;
9021 subsegT subseg = now_subseg;
9022 asection *sec;
9023 unsigned int alignment, align_size_1;
9024 unsigned int isa_1_descsz, feature_2_descsz, descsz;
9025 unsigned int isa_1_descsz_raw, feature_2_descsz_raw;
9026 unsigned int padding;
9027
1273b2f8 9028 if (!IS_ELF || !x86_used_note)
b4a3a7b4
L
9029 return;
9030
b4a3a7b4
L
9031 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X86;
9032
9033 /* The .note.gnu.property section layout:
9034
9035 Field Length Contents
9036 ---- ---- ----
9037 n_namsz 4 4
9038 n_descsz 4 The note descriptor size
9039 n_type 4 NT_GNU_PROPERTY_TYPE_0
9040 n_name 4 "GNU"
9041 n_desc n_descsz The program property array
9042 .... .... ....
9043 */
9044
9045 /* Create the .note.gnu.property section. */
9046 sec = subseg_new (NOTE_GNU_PROPERTY_SECTION_NAME, 0);
fd361982 9047 bfd_set_section_flags (sec,
b4a3a7b4
L
9048 (SEC_ALLOC
9049 | SEC_LOAD
9050 | SEC_DATA
9051 | SEC_HAS_CONTENTS
9052 | SEC_READONLY));
9053
9054 if (get_elf_backend_data (stdoutput)->s->elfclass == ELFCLASS64)
9055 {
9056 align_size_1 = 7;
9057 alignment = 3;
9058 }
9059 else
9060 {
9061 align_size_1 = 3;
9062 alignment = 2;
9063 }
9064
fd361982 9065 bfd_set_section_alignment (sec, alignment);
b4a3a7b4
L
9066 elf_section_type (sec) = SHT_NOTE;
9067
1273b2f8
L
9068 /* GNU_PROPERTY_X86_ISA_1_USED: 4-byte type + 4-byte data size
9069 + 4-byte data */
9070 isa_1_descsz_raw = 4 + 4 + 4;
9071 /* Align GNU_PROPERTY_X86_ISA_1_USED. */
9072 isa_1_descsz = (isa_1_descsz_raw + align_size_1) & ~align_size_1;
b4a3a7b4
L
9073
9074 feature_2_descsz_raw = isa_1_descsz;
9075 /* GNU_PROPERTY_X86_FEATURE_2_USED: 4-byte type + 4-byte data size
1273b2f8 9076 + 4-byte data */
b4a3a7b4
L
9077 feature_2_descsz_raw += 4 + 4 + 4;
9078 /* Align GNU_PROPERTY_X86_FEATURE_2_USED. */
9079 feature_2_descsz = ((feature_2_descsz_raw + align_size_1)
9080 & ~align_size_1);
9081
9082 descsz = feature_2_descsz;
9083 /* Section size: n_namsz + n_descsz + n_type + n_name + n_descsz. */
9084 p = frag_more (4 + 4 + 4 + 4 + descsz);
9085
9086 /* Write n_namsz. */
9087 md_number_to_chars (p, (valueT) 4, 4);
9088
9089 /* Write n_descsz. */
9090 md_number_to_chars (p + 4, (valueT) descsz, 4);
9091
9092 /* Write n_type. */
9093 md_number_to_chars (p + 4 * 2, (valueT) NT_GNU_PROPERTY_TYPE_0, 4);
9094
9095 /* Write n_name. */
9096 memcpy (p + 4 * 3, "GNU", 4);
9097
1273b2f8
L
9098 /* Write 4-byte type. */
9099 md_number_to_chars (p + 4 * 4,
9100 (valueT) GNU_PROPERTY_X86_ISA_1_USED, 4);
b4a3a7b4 9101
1273b2f8
L
9102 /* Write 4-byte data size. */
9103 md_number_to_chars (p + 4 * 5, (valueT) 4, 4);
b4a3a7b4 9104
1273b2f8
L
9105 /* Write 4-byte data. */
9106 md_number_to_chars (p + 4 * 6, (valueT) x86_isa_1_used, 4);
b4a3a7b4 9107
1273b2f8
L
9108 /* Zero out paddings. */
9109 padding = isa_1_descsz - isa_1_descsz_raw;
9110 if (padding)
9111 memset (p + 4 * 7, 0, padding);
b4a3a7b4
L
9112
9113 /* Write 4-byte type. */
9114 md_number_to_chars (p + isa_1_descsz + 4 * 4,
9115 (valueT) GNU_PROPERTY_X86_FEATURE_2_USED, 4);
9116
9117 /* Write 4-byte data size. */
9118 md_number_to_chars (p + isa_1_descsz + 4 * 5, (valueT) 4, 4);
9119
9120 /* Write 4-byte data. */
9121 md_number_to_chars (p + isa_1_descsz + 4 * 6,
9122 (valueT) x86_feature_2_used, 4);
9123
9124 /* Zero out paddings. */
9125 padding = feature_2_descsz - feature_2_descsz_raw;
9126 if (padding)
9127 memset (p + isa_1_descsz + 4 * 7, 0, padding);
9128
9129 /* We probably can't restore the current segment, for there likely
9130 isn't one yet... */
9131 if (seg && subseg)
9132 subseg_set (seg, subseg);
9133}
9134#endif
9135
9c33702b
JB
9136static unsigned int
9137encoding_length (const fragS *start_frag, offsetT start_off,
9138 const char *frag_now_ptr)
9139{
9140 unsigned int len = 0;
9141
9142 if (start_frag != frag_now)
9143 {
9144 const fragS *fr = start_frag;
9145
9146 do {
9147 len += fr->fr_fix;
9148 fr = fr->fr_next;
9149 } while (fr && fr != frag_now);
9150 }
9151
9152 return len - start_off + (frag_now_ptr - frag_now->fr_literal);
9153}
9154
e379e5f3 9155/* Return 1 for test, and, cmp, add, sub, inc and dec which may
79d72f45
HL
9156 be macro-fused with conditional jumps.
9157 NB: If TEST/AND/CMP/ADD/SUB/INC/DEC is of RIP relative address,
9158 or is one of the following format:
9159
9160 cmp m, imm
9161 add m, imm
9162 sub m, imm
9163 test m, imm
9164 and m, imm
9165 inc m
9166 dec m
9167
9168 it is unfusible. */
e379e5f3
L
9169
9170static int
79d72f45 9171maybe_fused_with_jcc_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
9172{
9173 /* No RIP address. */
9174 if (i.base_reg && i.base_reg->reg_num == RegIP)
9175 return 0;
9176
389d00a5
JB
9177 /* No opcodes outside of base encoding space. */
9178 if (i.tm.opcode_modifier.opcodespace != SPACE_BASE)
e379e5f3
L
9179 return 0;
9180
79d72f45
HL
9181 /* add, sub without add/sub m, imm. */
9182 if (i.tm.base_opcode <= 5
e379e5f3
L
9183 || (i.tm.base_opcode >= 0x28 && i.tm.base_opcode <= 0x2d)
9184 || ((i.tm.base_opcode | 3) == 0x83
79d72f45 9185 && (i.tm.extension_opcode == 0x5
e379e5f3 9186 || i.tm.extension_opcode == 0x0)))
79d72f45
HL
9187 {
9188 *mf_cmp_p = mf_cmp_alu_cmp;
9189 return !(i.mem_operands && i.imm_operands);
9190 }
e379e5f3 9191
79d72f45
HL
9192 /* and without and m, imm. */
9193 if ((i.tm.base_opcode >= 0x20 && i.tm.base_opcode <= 0x25)
9194 || ((i.tm.base_opcode | 3) == 0x83
9195 && i.tm.extension_opcode == 0x4))
9196 {
9197 *mf_cmp_p = mf_cmp_test_and;
9198 return !(i.mem_operands && i.imm_operands);
9199 }
9200
9201 /* test without test m imm. */
e379e5f3
L
9202 if ((i.tm.base_opcode | 1) == 0x85
9203 || (i.tm.base_opcode | 1) == 0xa9
9204 || ((i.tm.base_opcode | 1) == 0xf7
79d72f45
HL
9205 && i.tm.extension_opcode == 0))
9206 {
9207 *mf_cmp_p = mf_cmp_test_and;
9208 return !(i.mem_operands && i.imm_operands);
9209 }
9210
9211 /* cmp without cmp m, imm. */
9212 if ((i.tm.base_opcode >= 0x38 && i.tm.base_opcode <= 0x3d)
e379e5f3
L
9213 || ((i.tm.base_opcode | 3) == 0x83
9214 && (i.tm.extension_opcode == 0x7)))
79d72f45
HL
9215 {
9216 *mf_cmp_p = mf_cmp_alu_cmp;
9217 return !(i.mem_operands && i.imm_operands);
9218 }
e379e5f3 9219
79d72f45 9220 /* inc, dec without inc/dec m. */
e379e5f3
L
9221 if ((i.tm.cpu_flags.bitfield.cpuno64
9222 && (i.tm.base_opcode | 0xf) == 0x4f)
9223 || ((i.tm.base_opcode | 1) == 0xff
9224 && i.tm.extension_opcode <= 0x1))
79d72f45
HL
9225 {
9226 *mf_cmp_p = mf_cmp_incdec;
9227 return !i.mem_operands;
9228 }
e379e5f3
L
9229
9230 return 0;
9231}
9232
9233/* Return 1 if a FUSED_JCC_PADDING frag should be generated. */
9234
9235static int
79d72f45 9236add_fused_jcc_padding_frag_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
9237{
9238 /* NB: Don't work with COND_JUMP86 without i386. */
9239 if (!align_branch_power
9240 || now_seg == absolute_section
9241 || !cpu_arch_flags.bitfield.cpui386
9242 || !(align_branch & align_branch_fused_bit))
9243 return 0;
9244
79d72f45 9245 if (maybe_fused_with_jcc_p (mf_cmp_p))
e379e5f3
L
9246 {
9247 if (last_insn.kind == last_insn_other
9248 || last_insn.seg != now_seg)
9249 return 1;
9250 if (flag_debug)
9251 as_warn_where (last_insn.file, last_insn.line,
9252 _("`%s` skips -malign-branch-boundary on `%s`"),
9253 last_insn.name, i.tm.name);
9254 }
9255
9256 return 0;
9257}
9258
9259/* Return 1 if a BRANCH_PREFIX frag should be generated. */
9260
9261static int
9262add_branch_prefix_frag_p (void)
9263{
9264 /* NB: Don't work with COND_JUMP86 without i386. Don't add prefix
9265 to PadLock instructions since they include prefixes in opcode. */
9266 if (!align_branch_power
9267 || !align_branch_prefix_size
9268 || now_seg == absolute_section
9269 || i.tm.cpu_flags.bitfield.cpupadlock
9270 || !cpu_arch_flags.bitfield.cpui386)
9271 return 0;
9272
9273 /* Don't add prefix if it is a prefix or there is no operand in case
9274 that segment prefix is special. */
9275 if (!i.operands || i.tm.opcode_modifier.isprefix)
9276 return 0;
9277
9278 if (last_insn.kind == last_insn_other
9279 || last_insn.seg != now_seg)
9280 return 1;
9281
9282 if (flag_debug)
9283 as_warn_where (last_insn.file, last_insn.line,
9284 _("`%s` skips -malign-branch-boundary on `%s`"),
9285 last_insn.name, i.tm.name);
9286
9287 return 0;
9288}
9289
9290/* Return 1 if a BRANCH_PADDING frag should be generated. */
9291
9292static int
79d72f45
HL
9293add_branch_padding_frag_p (enum align_branch_kind *branch_p,
9294 enum mf_jcc_kind *mf_jcc_p)
e379e5f3
L
9295{
9296 int add_padding;
9297
9298 /* NB: Don't work with COND_JUMP86 without i386. */
9299 if (!align_branch_power
9300 || now_seg == absolute_section
389d00a5
JB
9301 || !cpu_arch_flags.bitfield.cpui386
9302 || i.tm.opcode_modifier.opcodespace != SPACE_BASE)
e379e5f3
L
9303 return 0;
9304
9305 add_padding = 0;
9306
9307 /* Check for jcc and direct jmp. */
9308 if (i.tm.opcode_modifier.jump == JUMP)
9309 {
9310 if (i.tm.base_opcode == JUMP_PC_RELATIVE)
9311 {
9312 *branch_p = align_branch_jmp;
9313 add_padding = align_branch & align_branch_jmp_bit;
9314 }
9315 else
9316 {
79d72f45
HL
9317 /* Because J<cc> and JN<cc> share same group in macro-fusible table,
9318 igore the lowest bit. */
9319 *mf_jcc_p = (i.tm.base_opcode & 0x0e) >> 1;
e379e5f3
L
9320 *branch_p = align_branch_jcc;
9321 if ((align_branch & align_branch_jcc_bit))
9322 add_padding = 1;
9323 }
9324 }
e379e5f3
L
9325 else if ((i.tm.base_opcode | 1) == 0xc3)
9326 {
9327 /* Near ret. */
9328 *branch_p = align_branch_ret;
9329 if ((align_branch & align_branch_ret_bit))
9330 add_padding = 1;
9331 }
9332 else
9333 {
9334 /* Check for indirect jmp, direct and indirect calls. */
9335 if (i.tm.base_opcode == 0xe8)
9336 {
9337 /* Direct call. */
9338 *branch_p = align_branch_call;
9339 if ((align_branch & align_branch_call_bit))
9340 add_padding = 1;
9341 }
9342 else if (i.tm.base_opcode == 0xff
9343 && (i.tm.extension_opcode == 2
9344 || i.tm.extension_opcode == 4))
9345 {
9346 /* Indirect call and jmp. */
9347 *branch_p = align_branch_indirect;
9348 if ((align_branch & align_branch_indirect_bit))
9349 add_padding = 1;
9350 }
9351
9352 if (add_padding
9353 && i.disp_operands
9354 && tls_get_addr
9355 && (i.op[0].disps->X_op == O_symbol
9356 || (i.op[0].disps->X_op == O_subtract
9357 && i.op[0].disps->X_op_symbol == GOT_symbol)))
9358 {
9359 symbolS *s = i.op[0].disps->X_add_symbol;
9360 /* No padding to call to global or undefined tls_get_addr. */
9361 if ((S_IS_EXTERNAL (s) || !S_IS_DEFINED (s))
9362 && strcmp (S_GET_NAME (s), tls_get_addr) == 0)
9363 return 0;
9364 }
9365 }
9366
9367 if (add_padding
9368 && last_insn.kind != last_insn_other
9369 && last_insn.seg == now_seg)
9370 {
9371 if (flag_debug)
9372 as_warn_where (last_insn.file, last_insn.line,
9373 _("`%s` skips -malign-branch-boundary on `%s`"),
9374 last_insn.name, i.tm.name);
9375 return 0;
9376 }
9377
9378 return add_padding;
9379}
9380
29b0f896 9381static void
e3bb37b5 9382output_insn (void)
29b0f896 9383{
2bbd9c25
JJ
9384 fragS *insn_start_frag;
9385 offsetT insn_start_off;
e379e5f3
L
9386 fragS *fragP = NULL;
9387 enum align_branch_kind branch = align_branch_none;
79d72f45
HL
9388 /* The initializer is arbitrary just to avoid uninitialized error.
9389 it's actually either assigned in add_branch_padding_frag_p
9390 or never be used. */
9391 enum mf_jcc_kind mf_jcc = mf_jcc_jo;
2bbd9c25 9392
b4a3a7b4 9393#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
48ef937e 9394 if (IS_ELF && x86_used_note && now_seg != absolute_section)
b4a3a7b4 9395 {
32930e4e
L
9396 if ((i.xstate & xstate_tmm) == xstate_tmm
9397 || i.tm.cpu_flags.bitfield.cpuamx_tile)
9398 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_TMM;
9399
b4a3a7b4
L
9400 if (i.tm.cpu_flags.bitfield.cpu8087
9401 || i.tm.cpu_flags.bitfield.cpu287
9402 || i.tm.cpu_flags.bitfield.cpu387
9403 || i.tm.cpu_flags.bitfield.cpu687
9404 || i.tm.cpu_flags.bitfield.cpufisttp)
9405 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X87;
014d61ea 9406
921eafea 9407 if ((i.xstate & xstate_mmx)
389d00a5
JB
9408 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
9409 && !is_any_vex_encoding (&i.tm)
9410 && (i.tm.base_opcode == 0x77 /* emms */
9411 || i.tm.base_opcode == 0x0e /* femms */)))
b4a3a7b4 9412 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MMX;
014d61ea 9413
32930e4e
L
9414 if (i.index_reg)
9415 {
9416 if (i.index_reg->reg_type.bitfield.zmmword)
9417 i.xstate |= xstate_zmm;
9418 else if (i.index_reg->reg_type.bitfield.ymmword)
9419 i.xstate |= xstate_ymm;
9420 else if (i.index_reg->reg_type.bitfield.xmmword)
9421 i.xstate |= xstate_xmm;
9422 }
014d61ea
JB
9423
9424 /* vzeroall / vzeroupper */
9425 if (i.tm.base_opcode == 0x77 && i.tm.cpu_flags.bitfield.cpuavx)
9426 i.xstate |= xstate_ymm;
9427
c4694f17 9428 if ((i.xstate & xstate_xmm)
389d00a5
JB
9429 /* ldmxcsr / stmxcsr / vldmxcsr / vstmxcsr */
9430 || (i.tm.base_opcode == 0xae
9431 && (i.tm.cpu_flags.bitfield.cpusse
9432 || i.tm.cpu_flags.bitfield.cpuavx))
c4694f17
TG
9433 || i.tm.cpu_flags.bitfield.cpuwidekl
9434 || i.tm.cpu_flags.bitfield.cpukl)
b4a3a7b4 9435 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XMM;
014d61ea 9436
921eafea 9437 if ((i.xstate & xstate_ymm) == xstate_ymm)
b4a3a7b4 9438 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_YMM;
921eafea 9439 if ((i.xstate & xstate_zmm) == xstate_zmm)
b4a3a7b4 9440 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_ZMM;
6225c532 9441 if (i.mask.reg || (i.xstate & xstate_mask) == xstate_mask)
32930e4e 9442 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MASK;
b4a3a7b4
L
9443 if (i.tm.cpu_flags.bitfield.cpufxsr)
9444 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_FXSR;
9445 if (i.tm.cpu_flags.bitfield.cpuxsave)
9446 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVE;
9447 if (i.tm.cpu_flags.bitfield.cpuxsaveopt)
9448 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT;
9449 if (i.tm.cpu_flags.bitfield.cpuxsavec)
9450 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEC;
b0ab0693
L
9451
9452 if (x86_feature_2_used
9453 || i.tm.cpu_flags.bitfield.cpucmov
9454 || i.tm.cpu_flags.bitfield.cpusyscall
389d00a5
JB
9455 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
9456 && i.tm.base_opcode == 0xc7
70e95837 9457 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
b0ab0693
L
9458 && i.tm.extension_opcode == 1) /* cmpxchg8b */)
9459 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_BASELINE;
9460 if (i.tm.cpu_flags.bitfield.cpusse3
9461 || i.tm.cpu_flags.bitfield.cpussse3
9462 || i.tm.cpu_flags.bitfield.cpusse4_1
9463 || i.tm.cpu_flags.bitfield.cpusse4_2
9464 || i.tm.cpu_flags.bitfield.cpucx16
9465 || i.tm.cpu_flags.bitfield.cpupopcnt
9466 /* LAHF-SAHF insns in 64-bit mode. */
9467 || (flag_code == CODE_64BIT
35648716
JB
9468 && (i.tm.base_opcode | 1) == 0x9f
9469 && i.tm.opcode_modifier.opcodespace == SPACE_BASE))
b0ab0693
L
9470 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V2;
9471 if (i.tm.cpu_flags.bitfield.cpuavx
9472 || i.tm.cpu_flags.bitfield.cpuavx2
9473 /* Any VEX encoded insns execpt for CpuAVX512F, CpuAVX512BW,
9474 CpuAVX512DQ, LPW, TBM and AMX. */
9475 || (i.tm.opcode_modifier.vex
9476 && !i.tm.cpu_flags.bitfield.cpuavx512f
9477 && !i.tm.cpu_flags.bitfield.cpuavx512bw
9478 && !i.tm.cpu_flags.bitfield.cpuavx512dq
9479 && !i.tm.cpu_flags.bitfield.cpulwp
9480 && !i.tm.cpu_flags.bitfield.cputbm
9481 && !(x86_feature_2_used & GNU_PROPERTY_X86_FEATURE_2_TMM))
9482 || i.tm.cpu_flags.bitfield.cpuf16c
9483 || i.tm.cpu_flags.bitfield.cpufma
9484 || i.tm.cpu_flags.bitfield.cpulzcnt
9485 || i.tm.cpu_flags.bitfield.cpumovbe
9486 || i.tm.cpu_flags.bitfield.cpuxsaves
9487 || (x86_feature_2_used
9488 & (GNU_PROPERTY_X86_FEATURE_2_XSAVE
9489 | GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT
9490 | GNU_PROPERTY_X86_FEATURE_2_XSAVEC)) != 0)
9491 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V3;
9492 if (i.tm.cpu_flags.bitfield.cpuavx512f
9493 || i.tm.cpu_flags.bitfield.cpuavx512bw
9494 || i.tm.cpu_flags.bitfield.cpuavx512dq
9495 || i.tm.cpu_flags.bitfield.cpuavx512vl
9496 /* Any EVEX encoded insns except for AVX512ER, AVX512PF and
9497 VNNIW. */
9498 || (i.tm.opcode_modifier.evex
9499 && !i.tm.cpu_flags.bitfield.cpuavx512er
9500 && !i.tm.cpu_flags.bitfield.cpuavx512pf
9501 && !i.tm.cpu_flags.bitfield.cpuavx512_4vnniw))
9502 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V4;
b4a3a7b4
L
9503 }
9504#endif
9505
29b0f896
AM
9506 /* Tie dwarf2 debug info to the address at the start of the insn.
9507 We can't do this after the insn has been output as the current
9508 frag may have been closed off. eg. by frag_var. */
9509 dwarf2_emit_insn (0);
9510
2bbd9c25
JJ
9511 insn_start_frag = frag_now;
9512 insn_start_off = frag_now_fix ();
9513
79d72f45 9514 if (add_branch_padding_frag_p (&branch, &mf_jcc))
e379e5f3
L
9515 {
9516 char *p;
9517 /* Branch can be 8 bytes. Leave some room for prefixes. */
9518 unsigned int max_branch_padding_size = 14;
9519
9520 /* Align section to boundary. */
9521 record_alignment (now_seg, align_branch_power);
9522
9523 /* Make room for padding. */
9524 frag_grow (max_branch_padding_size);
9525
9526 /* Start of the padding. */
9527 p = frag_more (0);
9528
9529 fragP = frag_now;
9530
9531 frag_var (rs_machine_dependent, max_branch_padding_size, 0,
9532 ENCODE_RELAX_STATE (BRANCH_PADDING, 0),
9533 NULL, 0, p);
9534
79d72f45 9535 fragP->tc_frag_data.mf_type = mf_jcc;
e379e5f3
L
9536 fragP->tc_frag_data.branch_type = branch;
9537 fragP->tc_frag_data.max_bytes = max_branch_padding_size;
9538 }
9539
29b0f896 9540 /* Output jumps. */
0cfa3eb3 9541 if (i.tm.opcode_modifier.jump == JUMP)
29b0f896 9542 output_branch ();
0cfa3eb3
JB
9543 else if (i.tm.opcode_modifier.jump == JUMP_BYTE
9544 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896 9545 output_jump ();
0cfa3eb3 9546 else if (i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT)
29b0f896
AM
9547 output_interseg_jump ();
9548 else
9549 {
9550 /* Output normal instructions here. */
9551 char *p;
9552 unsigned char *q;
47465058 9553 unsigned int j;
79d72f45 9554 enum mf_cmp_kind mf_cmp;
4dffcebc 9555
e4e00185 9556 if (avoid_fence
389d00a5
JB
9557 && (i.tm.base_opcode == 0xaee8
9558 || i.tm.base_opcode == 0xaef0
9559 || i.tm.base_opcode == 0xaef8))
48ef937e
JB
9560 {
9561 /* Encode lfence, mfence, and sfence as
9562 f0 83 04 24 00 lock addl $0x0, (%{re}sp). */
9563 if (now_seg != absolute_section)
9564 {
9565 offsetT val = 0x240483f0ULL;
9566
9567 p = frag_more (5);
9568 md_number_to_chars (p, val, 5);
9569 }
9570 else
9571 abs_section_offset += 5;
9572 return;
9573 }
e4e00185 9574
d022bddd
IT
9575 /* Some processors fail on LOCK prefix. This options makes
9576 assembler ignore LOCK prefix and serves as a workaround. */
9577 if (omit_lock_prefix)
9578 {
35648716
JB
9579 if (i.tm.base_opcode == LOCK_PREFIX_OPCODE
9580 && i.tm.opcode_modifier.isprefix)
d022bddd
IT
9581 return;
9582 i.prefix[LOCK_PREFIX] = 0;
9583 }
9584
e379e5f3
L
9585 if (branch)
9586 /* Skip if this is a branch. */
9587 ;
79d72f45 9588 else if (add_fused_jcc_padding_frag_p (&mf_cmp))
e379e5f3
L
9589 {
9590 /* Make room for padding. */
9591 frag_grow (MAX_FUSED_JCC_PADDING_SIZE);
9592 p = frag_more (0);
9593
9594 fragP = frag_now;
9595
9596 frag_var (rs_machine_dependent, MAX_FUSED_JCC_PADDING_SIZE, 0,
9597 ENCODE_RELAX_STATE (FUSED_JCC_PADDING, 0),
9598 NULL, 0, p);
9599
79d72f45 9600 fragP->tc_frag_data.mf_type = mf_cmp;
e379e5f3
L
9601 fragP->tc_frag_data.branch_type = align_branch_fused;
9602 fragP->tc_frag_data.max_bytes = MAX_FUSED_JCC_PADDING_SIZE;
9603 }
9604 else if (add_branch_prefix_frag_p ())
9605 {
9606 unsigned int max_prefix_size = align_branch_prefix_size;
9607
9608 /* Make room for padding. */
9609 frag_grow (max_prefix_size);
9610 p = frag_more (0);
9611
9612 fragP = frag_now;
9613
9614 frag_var (rs_machine_dependent, max_prefix_size, 0,
9615 ENCODE_RELAX_STATE (BRANCH_PREFIX, 0),
9616 NULL, 0, p);
9617
9618 fragP->tc_frag_data.max_bytes = max_prefix_size;
9619 }
9620
43234a1e
L
9621 /* Since the VEX/EVEX prefix contains the implicit prefix, we
9622 don't need the explicit prefix. */
9623 if (!i.tm.opcode_modifier.vex && !i.tm.opcode_modifier.evex)
bc4bd9ab 9624 {
7b47a312 9625 switch (i.tm.opcode_modifier.opcodeprefix)
bc4bd9ab 9626 {
7b47a312
L
9627 case PREFIX_0X66:
9628 add_prefix (0x66);
9629 break;
9630 case PREFIX_0XF2:
9631 add_prefix (0xf2);
9632 break;
9633 case PREFIX_0XF3:
8b65b895
L
9634 if (!i.tm.cpu_flags.bitfield.cpupadlock
9635 || (i.prefix[REP_PREFIX] != 0xf3))
9636 add_prefix (0xf3);
c0f3af97 9637 break;
7b47a312 9638 case PREFIX_NONE:
9a182d04 9639 switch (i.opcode_length)
c0f3af97 9640 {
7b47a312 9641 case 2:
7b47a312 9642 break;
9a182d04 9643 case 1:
7b47a312 9644 /* Check for pseudo prefixes. */
9a182d04
JB
9645 if (!i.tm.opcode_modifier.isprefix || i.tm.base_opcode)
9646 break;
7b47a312
L
9647 as_bad_where (insn_start_frag->fr_file,
9648 insn_start_frag->fr_line,
9649 _("pseudo prefix without instruction"));
9650 return;
9651 default:
9652 abort ();
4dffcebc 9653 }
c0f3af97 9654 break;
c0f3af97
L
9655 default:
9656 abort ();
bc4bd9ab 9657 }
c0f3af97 9658
6d19a37a 9659#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
cf61b747
L
9660 /* For x32, add a dummy REX_OPCODE prefix for mov/add with
9661 R_X86_64_GOTTPOFF relocation so that linker can safely
14470f07
L
9662 perform IE->LE optimization. A dummy REX_OPCODE prefix
9663 is also needed for lea with R_X86_64_GOTPC32_TLSDESC
9664 relocation for GDesc -> IE/LE optimization. */
cf61b747
L
9665 if (x86_elf_abi == X86_64_X32_ABI
9666 && i.operands == 2
14470f07
L
9667 && (i.reloc[0] == BFD_RELOC_X86_64_GOTTPOFF
9668 || i.reloc[0] == BFD_RELOC_X86_64_GOTPC32_TLSDESC)
cf61b747
L
9669 && i.prefix[REX_PREFIX] == 0)
9670 add_prefix (REX_OPCODE);
6d19a37a 9671#endif
cf61b747 9672
c0f3af97
L
9673 /* The prefix bytes. */
9674 for (j = ARRAY_SIZE (i.prefix), q = i.prefix; j > 0; j--, q++)
9675 if (*q)
48ef937e 9676 frag_opcode_byte (*q);
0f10071e 9677 }
ae5c1c7b 9678 else
c0f3af97
L
9679 {
9680 for (j = 0, q = i.prefix; j < ARRAY_SIZE (i.prefix); j++, q++)
9681 if (*q)
9682 switch (j)
9683 {
c0f3af97
L
9684 case SEG_PREFIX:
9685 case ADDR_PREFIX:
48ef937e 9686 frag_opcode_byte (*q);
c0f3af97
L
9687 break;
9688 default:
9689 /* There should be no other prefixes for instructions
9690 with VEX prefix. */
9691 abort ();
9692 }
9693
43234a1e
L
9694 /* For EVEX instructions i.vrex should become 0 after
9695 build_evex_prefix. For VEX instructions upper 16 registers
9696 aren't available, so VREX should be 0. */
9697 if (i.vrex)
9698 abort ();
c0f3af97 9699 /* Now the VEX prefix. */
48ef937e
JB
9700 if (now_seg != absolute_section)
9701 {
9702 p = frag_more (i.vex.length);
9703 for (j = 0; j < i.vex.length; j++)
9704 p[j] = i.vex.bytes[j];
9705 }
9706 else
9707 abs_section_offset += i.vex.length;
c0f3af97 9708 }
252b5132 9709
29b0f896 9710 /* Now the opcode; be careful about word order here! */
389d00a5
JB
9711 j = i.opcode_length;
9712 if (!i.vex.length)
9713 switch (i.tm.opcode_modifier.opcodespace)
9714 {
9715 case SPACE_BASE:
9716 break;
9717 case SPACE_0F:
9718 ++j;
9719 break;
9720 case SPACE_0F38:
9721 case SPACE_0F3A:
9722 j += 2;
9723 break;
9724 default:
9725 abort ();
9726 }
9727
48ef937e 9728 if (now_seg == absolute_section)
389d00a5
JB
9729 abs_section_offset += j;
9730 else if (j == 1)
29b0f896
AM
9731 {
9732 FRAG_APPEND_1_CHAR (i.tm.base_opcode);
9733 }
9734 else
9735 {
389d00a5
JB
9736 p = frag_more (j);
9737 if (!i.vex.length
9738 && i.tm.opcode_modifier.opcodespace != SPACE_BASE)
9739 {
9740 *p++ = 0x0f;
9741 if (i.tm.opcode_modifier.opcodespace != SPACE_0F)
9742 *p++ = i.tm.opcode_modifier.opcodespace == SPACE_0F38
9743 ? 0x38 : 0x3a;
9744 }
9745
9a182d04 9746 switch (i.opcode_length)
331d2d0d 9747 {
4dffcebc 9748 case 2:
389d00a5
JB
9749 /* Put out high byte first: can't use md_number_to_chars! */
9750 *p++ = (i.tm.base_opcode >> 8) & 0xff;
9751 /* Fall through. */
9752 case 1:
9753 *p = i.tm.base_opcode & 0xff;
4dffcebc
L
9754 break;
9755 default:
9756 abort ();
9757 break;
331d2d0d 9758 }
0f10071e 9759
29b0f896 9760 }
3e73aa7c 9761
29b0f896 9762 /* Now the modrm byte and sib byte (if present). */
40fb9820 9763 if (i.tm.opcode_modifier.modrm)
29b0f896 9764 {
48ef937e
JB
9765 frag_opcode_byte ((i.rm.regmem << 0)
9766 | (i.rm.reg << 3)
9767 | (i.rm.mode << 6));
29b0f896
AM
9768 /* If i.rm.regmem == ESP (4)
9769 && i.rm.mode != (Register mode)
9770 && not 16 bit
9771 ==> need second modrm byte. */
9772 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
9773 && i.rm.mode != 3
dc821c5f 9774 && !(i.base_reg && i.base_reg->reg_type.bitfield.word))
48ef937e
JB
9775 frag_opcode_byte ((i.sib.base << 0)
9776 | (i.sib.index << 3)
9777 | (i.sib.scale << 6));
29b0f896 9778 }
3e73aa7c 9779
29b0f896 9780 if (i.disp_operands)
2bbd9c25 9781 output_disp (insn_start_frag, insn_start_off);
3e73aa7c 9782
29b0f896 9783 if (i.imm_operands)
2bbd9c25 9784 output_imm (insn_start_frag, insn_start_off);
9c33702b
JB
9785
9786 /*
9787 * frag_now_fix () returning plain abs_section_offset when we're in the
9788 * absolute section, and abs_section_offset not getting updated as data
9789 * gets added to the frag breaks the logic below.
9790 */
9791 if (now_seg != absolute_section)
9792 {
9793 j = encoding_length (insn_start_frag, insn_start_off, frag_more (0));
9794 if (j > 15)
9795 as_warn (_("instruction length of %u bytes exceeds the limit of 15"),
9796 j);
e379e5f3
L
9797 else if (fragP)
9798 {
9799 /* NB: Don't add prefix with GOTPC relocation since
9800 output_disp() above depends on the fixed encoding
9801 length. Can't add prefix with TLS relocation since
9802 it breaks TLS linker optimization. */
9803 unsigned int max = i.has_gotpc_tls_reloc ? 0 : 15 - j;
9804 /* Prefix count on the current instruction. */
9805 unsigned int count = i.vex.length;
9806 unsigned int k;
9807 for (k = 0; k < ARRAY_SIZE (i.prefix); k++)
9808 /* REX byte is encoded in VEX/EVEX prefix. */
9809 if (i.prefix[k] && (k != REX_PREFIX || !i.vex.length))
9810 count++;
9811
9812 /* Count prefixes for extended opcode maps. */
9813 if (!i.vex.length)
389d00a5 9814 switch (i.tm.opcode_modifier.opcodespace)
e379e5f3 9815 {
389d00a5 9816 case SPACE_BASE:
e379e5f3 9817 break;
389d00a5
JB
9818 case SPACE_0F:
9819 count++;
e379e5f3 9820 break;
389d00a5
JB
9821 case SPACE_0F38:
9822 case SPACE_0F3A:
9823 count += 2;
e379e5f3
L
9824 break;
9825 default:
9826 abort ();
9827 }
9828
9829 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
9830 == BRANCH_PREFIX)
9831 {
9832 /* Set the maximum prefix size in BRANCH_PREFIX
9833 frag. */
9834 if (fragP->tc_frag_data.max_bytes > max)
9835 fragP->tc_frag_data.max_bytes = max;
9836 if (fragP->tc_frag_data.max_bytes > count)
9837 fragP->tc_frag_data.max_bytes -= count;
9838 else
9839 fragP->tc_frag_data.max_bytes = 0;
9840 }
9841 else
9842 {
9843 /* Remember the maximum prefix size in FUSED_JCC_PADDING
9844 frag. */
9845 unsigned int max_prefix_size;
9846 if (align_branch_prefix_size > max)
9847 max_prefix_size = max;
9848 else
9849 max_prefix_size = align_branch_prefix_size;
9850 if (max_prefix_size > count)
9851 fragP->tc_frag_data.max_prefix_length
9852 = max_prefix_size - count;
9853 }
9854
9855 /* Use existing segment prefix if possible. Use CS
9856 segment prefix in 64-bit mode. In 32-bit mode, use SS
9857 segment prefix with ESP/EBP base register and use DS
9858 segment prefix without ESP/EBP base register. */
9859 if (i.prefix[SEG_PREFIX])
9860 fragP->tc_frag_data.default_prefix = i.prefix[SEG_PREFIX];
9861 else if (flag_code == CODE_64BIT)
9862 fragP->tc_frag_data.default_prefix = CS_PREFIX_OPCODE;
9863 else if (i.base_reg
9864 && (i.base_reg->reg_num == 4
9865 || i.base_reg->reg_num == 5))
9866 fragP->tc_frag_data.default_prefix = SS_PREFIX_OPCODE;
9867 else
9868 fragP->tc_frag_data.default_prefix = DS_PREFIX_OPCODE;
9869 }
9c33702b 9870 }
29b0f896 9871 }
252b5132 9872
e379e5f3
L
9873 /* NB: Don't work with COND_JUMP86 without i386. */
9874 if (align_branch_power
9875 && now_seg != absolute_section
9876 && cpu_arch_flags.bitfield.cpui386)
9877 {
9878 /* Terminate each frag so that we can add prefix and check for
9879 fused jcc. */
9880 frag_wane (frag_now);
9881 frag_new (0);
9882 }
9883
29b0f896
AM
9884#ifdef DEBUG386
9885 if (flag_debug)
9886 {
7b81dfbb 9887 pi ("" /*line*/, &i);
29b0f896
AM
9888 }
9889#endif /* DEBUG386 */
9890}
252b5132 9891
e205caa7
L
9892/* Return the size of the displacement operand N. */
9893
9894static int
9895disp_size (unsigned int n)
9896{
9897 int size = 4;
43234a1e 9898
b5014f7a 9899 if (i.types[n].bitfield.disp64)
40fb9820
L
9900 size = 8;
9901 else if (i.types[n].bitfield.disp8)
9902 size = 1;
9903 else if (i.types[n].bitfield.disp16)
9904 size = 2;
e205caa7
L
9905 return size;
9906}
9907
9908/* Return the size of the immediate operand N. */
9909
9910static int
9911imm_size (unsigned int n)
9912{
9913 int size = 4;
40fb9820
L
9914 if (i.types[n].bitfield.imm64)
9915 size = 8;
9916 else if (i.types[n].bitfield.imm8 || i.types[n].bitfield.imm8s)
9917 size = 1;
9918 else if (i.types[n].bitfield.imm16)
9919 size = 2;
e205caa7
L
9920 return size;
9921}
9922
29b0f896 9923static void
64e74474 9924output_disp (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
9925{
9926 char *p;
9927 unsigned int n;
252b5132 9928
29b0f896
AM
9929 for (n = 0; n < i.operands; n++)
9930 {
b5014f7a 9931 if (operand_type_check (i.types[n], disp))
29b0f896 9932 {
48ef937e
JB
9933 int size = disp_size (n);
9934
9935 if (now_seg == absolute_section)
9936 abs_section_offset += size;
9937 else if (i.op[n].disps->X_op == O_constant)
29b0f896 9938 {
43234a1e 9939 offsetT val = i.op[n].disps->X_add_number;
252b5132 9940
629cfaf1
JB
9941 val = offset_in_range (val >> (size == 1 ? i.memshift : 0),
9942 size);
29b0f896
AM
9943 p = frag_more (size);
9944 md_number_to_chars (p, val, size);
9945 }
9946 else
9947 {
f86103b7 9948 enum bfd_reloc_code_real reloc_type;
40fb9820 9949 int sign = i.types[n].bitfield.disp32s;
29b0f896 9950 int pcrel = (i.flags[n] & Operand_PCrel) != 0;
02a86693 9951 fixS *fixP;
29b0f896 9952
e205caa7 9953 /* We can't have 8 bit displacement here. */
9c2799c2 9954 gas_assert (!i.types[n].bitfield.disp8);
e205caa7 9955
29b0f896
AM
9956 /* The PC relative address is computed relative
9957 to the instruction boundary, so in case immediate
9958 fields follows, we need to adjust the value. */
9959 if (pcrel && i.imm_operands)
9960 {
29b0f896 9961 unsigned int n1;
e205caa7 9962 int sz = 0;
252b5132 9963
29b0f896 9964 for (n1 = 0; n1 < i.operands; n1++)
40fb9820 9965 if (operand_type_check (i.types[n1], imm))
252b5132 9966 {
e205caa7
L
9967 /* Only one immediate is allowed for PC
9968 relative address. */
9c2799c2 9969 gas_assert (sz == 0);
e205caa7
L
9970 sz = imm_size (n1);
9971 i.op[n].disps->X_add_number -= sz;
252b5132 9972 }
29b0f896 9973 /* We should find the immediate. */
9c2799c2 9974 gas_assert (sz != 0);
29b0f896 9975 }
520dc8e8 9976
29b0f896 9977 p = frag_more (size);
d258b828 9978 reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
d6ab8113 9979 if (GOT_symbol
2bbd9c25 9980 && GOT_symbol == i.op[n].disps->X_add_symbol
d6ab8113 9981 && (((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
9982 || reloc_type == BFD_RELOC_X86_64_32S
9983 || (reloc_type == BFD_RELOC_64
9984 && object_64bit))
d6ab8113
JB
9985 && (i.op[n].disps->X_op == O_symbol
9986 || (i.op[n].disps->X_op == O_add
9987 && ((symbol_get_value_expression
9988 (i.op[n].disps->X_op_symbol)->X_op)
9989 == O_subtract))))
9990 || reloc_type == BFD_RELOC_32_PCREL))
2bbd9c25 9991 {
4fa24527 9992 if (!object_64bit)
7b81dfbb
AJ
9993 {
9994 reloc_type = BFD_RELOC_386_GOTPC;
5b7c81bd 9995 i.has_gotpc_tls_reloc = true;
98da05bf 9996 i.op[n].disps->X_add_number +=
d583596c 9997 encoding_length (insn_start_frag, insn_start_off, p);
7b81dfbb
AJ
9998 }
9999 else if (reloc_type == BFD_RELOC_64)
10000 reloc_type = BFD_RELOC_X86_64_GOTPC64;
d6ab8113 10001 else
7b81dfbb
AJ
10002 /* Don't do the adjustment for x86-64, as there
10003 the pcrel addressing is relative to the _next_
10004 insn, and that is taken care of in other code. */
d6ab8113 10005 reloc_type = BFD_RELOC_X86_64_GOTPC32;
2bbd9c25 10006 }
e379e5f3
L
10007 else if (align_branch_power)
10008 {
10009 switch (reloc_type)
10010 {
10011 case BFD_RELOC_386_TLS_GD:
10012 case BFD_RELOC_386_TLS_LDM:
10013 case BFD_RELOC_386_TLS_IE:
10014 case BFD_RELOC_386_TLS_IE_32:
10015 case BFD_RELOC_386_TLS_GOTIE:
10016 case BFD_RELOC_386_TLS_GOTDESC:
10017 case BFD_RELOC_386_TLS_DESC_CALL:
10018 case BFD_RELOC_X86_64_TLSGD:
10019 case BFD_RELOC_X86_64_TLSLD:
10020 case BFD_RELOC_X86_64_GOTTPOFF:
10021 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
10022 case BFD_RELOC_X86_64_TLSDESC_CALL:
5b7c81bd 10023 i.has_gotpc_tls_reloc = true;
e379e5f3
L
10024 default:
10025 break;
10026 }
10027 }
02a86693
L
10028 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal,
10029 size, i.op[n].disps, pcrel,
10030 reloc_type);
eb19308f
JB
10031
10032 if (flag_code == CODE_64BIT && size == 4 && pcrel
10033 && !i.prefix[ADDR_PREFIX])
10034 fixP->fx_signed = 1;
10035
02a86693
L
10036 /* Check for "call/jmp *mem", "mov mem, %reg",
10037 "test %reg, mem" and "binop mem, %reg" where binop
10038 is one of adc, add, and, cmp, or, sbb, sub, xor
e60f4d3b
L
10039 instructions without data prefix. Always generate
10040 R_386_GOT32X for "sym*GOT" operand in 32-bit mode. */
10041 if (i.prefix[DATA_PREFIX] == 0
10042 && (generate_relax_relocations
10043 || (!object_64bit
10044 && i.rm.mode == 0
10045 && i.rm.regmem == 5))
0cb4071e
L
10046 && (i.rm.mode == 2
10047 || (i.rm.mode == 0 && i.rm.regmem == 5))
389d00a5 10048 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
02a86693
L
10049 && ((i.operands == 1
10050 && i.tm.base_opcode == 0xff
10051 && (i.rm.reg == 2 || i.rm.reg == 4))
10052 || (i.operands == 2
10053 && (i.tm.base_opcode == 0x8b
10054 || i.tm.base_opcode == 0x85
2ae4c703 10055 || (i.tm.base_opcode & ~0x38) == 0x03))))
02a86693
L
10056 {
10057 if (object_64bit)
10058 {
10059 fixP->fx_tcbit = i.rex != 0;
10060 if (i.base_reg
e968fc9b 10061 && (i.base_reg->reg_num == RegIP))
02a86693
L
10062 fixP->fx_tcbit2 = 1;
10063 }
10064 else
10065 fixP->fx_tcbit2 = 1;
10066 }
29b0f896
AM
10067 }
10068 }
10069 }
10070}
252b5132 10071
29b0f896 10072static void
64e74474 10073output_imm (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
10074{
10075 char *p;
10076 unsigned int n;
252b5132 10077
29b0f896
AM
10078 for (n = 0; n < i.operands; n++)
10079 {
43234a1e 10080 /* Skip SAE/RC Imm operand in EVEX. They are already handled. */
ca5312a2 10081 if (i.rounding.type != rc_none && n == i.rounding.operand)
43234a1e
L
10082 continue;
10083
40fb9820 10084 if (operand_type_check (i.types[n], imm))
29b0f896 10085 {
48ef937e
JB
10086 int size = imm_size (n);
10087
10088 if (now_seg == absolute_section)
10089 abs_section_offset += size;
10090 else if (i.op[n].imms->X_op == O_constant)
29b0f896 10091 {
29b0f896 10092 offsetT val;
b4cac588 10093
29b0f896
AM
10094 val = offset_in_range (i.op[n].imms->X_add_number,
10095 size);
10096 p = frag_more (size);
10097 md_number_to_chars (p, val, size);
10098 }
10099 else
10100 {
10101 /* Not absolute_section.
10102 Need a 32-bit fixup (don't support 8bit
10103 non-absolute imms). Try to support other
10104 sizes ... */
f86103b7 10105 enum bfd_reloc_code_real reloc_type;
e205caa7 10106 int sign;
29b0f896 10107
40fb9820 10108 if (i.types[n].bitfield.imm32s
a7d61044 10109 && (i.suffix == QWORD_MNEM_SUFFIX
40fb9820 10110 || (!i.suffix && i.tm.opcode_modifier.no_lsuf)))
29b0f896 10111 sign = 1;
e205caa7
L
10112 else
10113 sign = 0;
520dc8e8 10114
29b0f896 10115 p = frag_more (size);
d258b828 10116 reloc_type = reloc (size, 0, sign, i.reloc[n]);
f86103b7 10117
2bbd9c25
JJ
10118 /* This is tough to explain. We end up with this one if we
10119 * have operands that look like
10120 * "_GLOBAL_OFFSET_TABLE_+[.-.L284]". The goal here is to
10121 * obtain the absolute address of the GOT, and it is strongly
10122 * preferable from a performance point of view to avoid using
10123 * a runtime relocation for this. The actual sequence of
10124 * instructions often look something like:
10125 *
10126 * call .L66
10127 * .L66:
10128 * popl %ebx
10129 * addl $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
10130 *
10131 * The call and pop essentially return the absolute address
10132 * of the label .L66 and store it in %ebx. The linker itself
10133 * will ultimately change the first operand of the addl so
10134 * that %ebx points to the GOT, but to keep things simple, the
10135 * .o file must have this operand set so that it generates not
10136 * the absolute address of .L66, but the absolute address of
10137 * itself. This allows the linker itself simply treat a GOTPC
10138 * relocation as asking for a pcrel offset to the GOT to be
10139 * added in, and the addend of the relocation is stored in the
10140 * operand field for the instruction itself.
10141 *
10142 * Our job here is to fix the operand so that it would add
10143 * the correct offset so that %ebx would point to itself. The
10144 * thing that is tricky is that .-.L66 will point to the
10145 * beginning of the instruction, so we need to further modify
10146 * the operand so that it will point to itself. There are
10147 * other cases where you have something like:
10148 *
10149 * .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
10150 *
10151 * and here no correction would be required. Internally in
10152 * the assembler we treat operands of this form as not being
10153 * pcrel since the '.' is explicitly mentioned, and I wonder
10154 * whether it would simplify matters to do it this way. Who
10155 * knows. In earlier versions of the PIC patches, the
10156 * pcrel_adjust field was used to store the correction, but
10157 * since the expression is not pcrel, I felt it would be
10158 * confusing to do it this way. */
10159
d6ab8113 10160 if ((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
10161 || reloc_type == BFD_RELOC_X86_64_32S
10162 || reloc_type == BFD_RELOC_64)
29b0f896
AM
10163 && GOT_symbol
10164 && GOT_symbol == i.op[n].imms->X_add_symbol
10165 && (i.op[n].imms->X_op == O_symbol
10166 || (i.op[n].imms->X_op == O_add
10167 && ((symbol_get_value_expression
10168 (i.op[n].imms->X_op_symbol)->X_op)
10169 == O_subtract))))
10170 {
4fa24527 10171 if (!object_64bit)
d6ab8113 10172 reloc_type = BFD_RELOC_386_GOTPC;
7b81dfbb 10173 else if (size == 4)
d6ab8113 10174 reloc_type = BFD_RELOC_X86_64_GOTPC32;
7b81dfbb
AJ
10175 else if (size == 8)
10176 reloc_type = BFD_RELOC_X86_64_GOTPC64;
5b7c81bd 10177 i.has_gotpc_tls_reloc = true;
d583596c
JB
10178 i.op[n].imms->X_add_number +=
10179 encoding_length (insn_start_frag, insn_start_off, p);
29b0f896 10180 }
29b0f896
AM
10181 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
10182 i.op[n].imms, 0, reloc_type);
10183 }
10184 }
10185 }
252b5132
RH
10186}
10187\f
d182319b
JB
10188/* x86_cons_fix_new is called via the expression parsing code when a
10189 reloc is needed. We use this hook to get the correct .got reloc. */
d182319b
JB
10190static int cons_sign = -1;
10191
10192void
e3bb37b5 10193x86_cons_fix_new (fragS *frag, unsigned int off, unsigned int len,
62ebcb5c 10194 expressionS *exp, bfd_reloc_code_real_type r)
d182319b 10195{
d258b828 10196 r = reloc (len, 0, cons_sign, r);
d182319b
JB
10197
10198#ifdef TE_PE
10199 if (exp->X_op == O_secrel)
10200 {
10201 exp->X_op = O_symbol;
10202 r = BFD_RELOC_32_SECREL;
10203 }
10204#endif
10205
10206 fix_new_exp (frag, off, len, exp, 0, r);
10207}
10208
357d1bd8
L
10209/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
10210 purpose of the `.dc.a' internal pseudo-op. */
10211
10212int
10213x86_address_bytes (void)
10214{
10215 if ((stdoutput->arch_info->mach & bfd_mach_x64_32))
10216 return 4;
10217 return stdoutput->arch_info->bits_per_address / 8;
10218}
10219
deea4973
JB
10220#if (!(defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined (OBJ_MACH_O)) \
10221 || defined (LEX_AT)) && !defined (TE_PE)
d258b828 10222# define lex_got(reloc, adjust, types) NULL
718ddfc0 10223#else
f3c180ae
AM
10224/* Parse operands of the form
10225 <symbol>@GOTOFF+<nnn>
10226 and similar .plt or .got references.
10227
10228 If we find one, set up the correct relocation in RELOC and copy the
10229 input string, minus the `@GOTOFF' into a malloc'd buffer for
10230 parsing by the calling routine. Return this buffer, and if ADJUST
10231 is non-null set it to the length of the string we removed from the
10232 input line. Otherwise return NULL. */
10233static char *
91d6fa6a 10234lex_got (enum bfd_reloc_code_real *rel,
64e74474 10235 int *adjust,
d258b828 10236 i386_operand_type *types)
f3c180ae 10237{
7b81dfbb
AJ
10238 /* Some of the relocations depend on the size of what field is to
10239 be relocated. But in our callers i386_immediate and i386_displacement
10240 we don't yet know the operand size (this will be set by insn
10241 matching). Hence we record the word32 relocation here,
10242 and adjust the reloc according to the real size in reloc(). */
f3c180ae
AM
10243 static const struct {
10244 const char *str;
cff8d58a 10245 int len;
4fa24527 10246 const enum bfd_reloc_code_real rel[2];
40fb9820 10247 const i386_operand_type types64;
5b7c81bd 10248 bool need_GOT_symbol;
f3c180ae 10249 } gotrel[] = {
deea4973 10250#ifndef TE_PE
8ce3d284 10251#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
10252 { STRING_COMMA_LEN ("SIZE"), { BFD_RELOC_SIZE32,
10253 BFD_RELOC_SIZE32 },
5b7c81bd 10254 OPERAND_TYPE_IMM32_64, false },
8ce3d284 10255#endif
cff8d58a
L
10256 { STRING_COMMA_LEN ("PLTOFF"), { _dummy_first_bfd_reloc_code_real,
10257 BFD_RELOC_X86_64_PLTOFF64 },
5b7c81bd 10258 OPERAND_TYPE_IMM64, true },
cff8d58a
L
10259 { STRING_COMMA_LEN ("PLT"), { BFD_RELOC_386_PLT32,
10260 BFD_RELOC_X86_64_PLT32 },
5b7c81bd 10261 OPERAND_TYPE_IMM32_32S_DISP32, false },
cff8d58a
L
10262 { STRING_COMMA_LEN ("GOTPLT"), { _dummy_first_bfd_reloc_code_real,
10263 BFD_RELOC_X86_64_GOTPLT64 },
5b7c81bd 10264 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
10265 { STRING_COMMA_LEN ("GOTOFF"), { BFD_RELOC_386_GOTOFF,
10266 BFD_RELOC_X86_64_GOTOFF64 },
5b7c81bd 10267 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
10268 { STRING_COMMA_LEN ("GOTPCREL"), { _dummy_first_bfd_reloc_code_real,
10269 BFD_RELOC_X86_64_GOTPCREL },
5b7c81bd 10270 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10271 { STRING_COMMA_LEN ("TLSGD"), { BFD_RELOC_386_TLS_GD,
10272 BFD_RELOC_X86_64_TLSGD },
5b7c81bd 10273 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10274 { STRING_COMMA_LEN ("TLSLDM"), { BFD_RELOC_386_TLS_LDM,
10275 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10276 OPERAND_TYPE_NONE, true },
cff8d58a
L
10277 { STRING_COMMA_LEN ("TLSLD"), { _dummy_first_bfd_reloc_code_real,
10278 BFD_RELOC_X86_64_TLSLD },
5b7c81bd 10279 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10280 { STRING_COMMA_LEN ("GOTTPOFF"), { BFD_RELOC_386_TLS_IE_32,
10281 BFD_RELOC_X86_64_GOTTPOFF },
5b7c81bd 10282 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10283 { STRING_COMMA_LEN ("TPOFF"), { BFD_RELOC_386_TLS_LE_32,
10284 BFD_RELOC_X86_64_TPOFF32 },
5b7c81bd 10285 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
10286 { STRING_COMMA_LEN ("NTPOFF"), { BFD_RELOC_386_TLS_LE,
10287 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10288 OPERAND_TYPE_NONE, true },
cff8d58a
L
10289 { STRING_COMMA_LEN ("DTPOFF"), { BFD_RELOC_386_TLS_LDO_32,
10290 BFD_RELOC_X86_64_DTPOFF32 },
5b7c81bd 10291 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
10292 { STRING_COMMA_LEN ("GOTNTPOFF"),{ BFD_RELOC_386_TLS_GOTIE,
10293 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10294 OPERAND_TYPE_NONE, true },
cff8d58a
L
10295 { STRING_COMMA_LEN ("INDNTPOFF"),{ BFD_RELOC_386_TLS_IE,
10296 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10297 OPERAND_TYPE_NONE, true },
cff8d58a
L
10298 { STRING_COMMA_LEN ("GOT"), { BFD_RELOC_386_GOT32,
10299 BFD_RELOC_X86_64_GOT32 },
5b7c81bd 10300 OPERAND_TYPE_IMM32_32S_64_DISP32, true },
cff8d58a
L
10301 { STRING_COMMA_LEN ("TLSDESC"), { BFD_RELOC_386_TLS_GOTDESC,
10302 BFD_RELOC_X86_64_GOTPC32_TLSDESC },
5b7c81bd 10303 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10304 { STRING_COMMA_LEN ("TLSCALL"), { BFD_RELOC_386_TLS_DESC_CALL,
10305 BFD_RELOC_X86_64_TLSDESC_CALL },
5b7c81bd 10306 OPERAND_TYPE_IMM32_32S_DISP32, true },
deea4973
JB
10307#else /* TE_PE */
10308 { STRING_COMMA_LEN ("SECREL32"), { BFD_RELOC_32_SECREL,
10309 BFD_RELOC_32_SECREL },
10310 OPERAND_TYPE_IMM32_32S_64_DISP32_64, false },
10311#endif
f3c180ae
AM
10312 };
10313 char *cp;
10314 unsigned int j;
10315
deea4973 10316#if defined (OBJ_MAYBE_ELF) && !defined (TE_PE)
718ddfc0
JB
10317 if (!IS_ELF)
10318 return NULL;
d382c579 10319#endif
718ddfc0 10320
f3c180ae 10321 for (cp = input_line_pointer; *cp != '@'; cp++)
67c11a9b 10322 if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
f3c180ae
AM
10323 return NULL;
10324
47465058 10325 for (j = 0; j < ARRAY_SIZE (gotrel); j++)
f3c180ae 10326 {
cff8d58a 10327 int len = gotrel[j].len;
28f81592 10328 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
f3c180ae 10329 {
4fa24527 10330 if (gotrel[j].rel[object_64bit] != 0)
f3c180ae 10331 {
28f81592
AM
10332 int first, second;
10333 char *tmpbuf, *past_reloc;
f3c180ae 10334
91d6fa6a 10335 *rel = gotrel[j].rel[object_64bit];
f3c180ae 10336
3956db08
JB
10337 if (types)
10338 {
10339 if (flag_code != CODE_64BIT)
40fb9820
L
10340 {
10341 types->bitfield.imm32 = 1;
10342 types->bitfield.disp32 = 1;
10343 }
3956db08
JB
10344 else
10345 *types = gotrel[j].types64;
10346 }
10347
844bf810 10348 if (gotrel[j].need_GOT_symbol && GOT_symbol == NULL)
f3c180ae
AM
10349 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
10350
28f81592 10351 /* The length of the first part of our input line. */
f3c180ae 10352 first = cp - input_line_pointer;
28f81592
AM
10353
10354 /* The second part goes from after the reloc token until
67c11a9b 10355 (and including) an end_of_line char or comma. */
28f81592 10356 past_reloc = cp + 1 + len;
67c11a9b
AM
10357 cp = past_reloc;
10358 while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
10359 ++cp;
10360 second = cp + 1 - past_reloc;
28f81592
AM
10361
10362 /* Allocate and copy string. The trailing NUL shouldn't
10363 be necessary, but be safe. */
add39d23 10364 tmpbuf = XNEWVEC (char, first + second + 2);
f3c180ae 10365 memcpy (tmpbuf, input_line_pointer, first);
0787a12d
AM
10366 if (second != 0 && *past_reloc != ' ')
10367 /* Replace the relocation token with ' ', so that
10368 errors like foo@GOTOFF1 will be detected. */
10369 tmpbuf[first++] = ' ';
af89796a
L
10370 else
10371 /* Increment length by 1 if the relocation token is
10372 removed. */
10373 len++;
10374 if (adjust)
10375 *adjust = len;
0787a12d
AM
10376 memcpy (tmpbuf + first, past_reloc, second);
10377 tmpbuf[first + second] = '\0';
f3c180ae
AM
10378 return tmpbuf;
10379 }
10380
4fa24527
JB
10381 as_bad (_("@%s reloc is not supported with %d-bit output format"),
10382 gotrel[j].str, 1 << (5 + object_64bit));
f3c180ae
AM
10383 return NULL;
10384 }
10385 }
10386
10387 /* Might be a symbol version string. Don't as_bad here. */
10388 return NULL;
10389}
4e4f7c87 10390#endif
f3c180ae 10391
62ebcb5c 10392bfd_reloc_code_real_type
e3bb37b5 10393x86_cons (expressionS *exp, int size)
f3c180ae 10394{
62ebcb5c
AM
10395 bfd_reloc_code_real_type got_reloc = NO_RELOC;
10396
ee86248c
JB
10397 intel_syntax = -intel_syntax;
10398
3c7b9c2c 10399 exp->X_md = 0;
4fa24527 10400 if (size == 4 || (object_64bit && size == 8))
f3c180ae
AM
10401 {
10402 /* Handle @GOTOFF and the like in an expression. */
10403 char *save;
10404 char *gotfree_input_line;
4a57f2cf 10405 int adjust = 0;
f3c180ae
AM
10406
10407 save = input_line_pointer;
d258b828 10408 gotfree_input_line = lex_got (&got_reloc, &adjust, NULL);
f3c180ae
AM
10409 if (gotfree_input_line)
10410 input_line_pointer = gotfree_input_line;
10411
10412 expression (exp);
10413
10414 if (gotfree_input_line)
10415 {
10416 /* expression () has merrily parsed up to the end of line,
10417 or a comma - in the wrong buffer. Transfer how far
10418 input_line_pointer has moved to the right buffer. */
10419 input_line_pointer = (save
10420 + (input_line_pointer - gotfree_input_line)
10421 + adjust);
10422 free (gotfree_input_line);
3992d3b7
AM
10423 if (exp->X_op == O_constant
10424 || exp->X_op == O_absent
10425 || exp->X_op == O_illegal
0398aac5 10426 || exp->X_op == O_register
3992d3b7
AM
10427 || exp->X_op == O_big)
10428 {
10429 char c = *input_line_pointer;
10430 *input_line_pointer = 0;
10431 as_bad (_("missing or invalid expression `%s'"), save);
10432 *input_line_pointer = c;
10433 }
b9519cfe
L
10434 else if ((got_reloc == BFD_RELOC_386_PLT32
10435 || got_reloc == BFD_RELOC_X86_64_PLT32)
10436 && exp->X_op != O_symbol)
10437 {
10438 char c = *input_line_pointer;
10439 *input_line_pointer = 0;
10440 as_bad (_("invalid PLT expression `%s'"), save);
10441 *input_line_pointer = c;
10442 }
f3c180ae
AM
10443 }
10444 }
10445 else
10446 expression (exp);
ee86248c
JB
10447
10448 intel_syntax = -intel_syntax;
10449
10450 if (intel_syntax)
10451 i386_intel_simplify (exp);
62ebcb5c 10452
a442cac5
JB
10453 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
10454 if (size == 4 && exp->X_op == O_constant && !object_64bit)
10455 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
10456
62ebcb5c 10457 return got_reloc;
f3c180ae 10458}
f3c180ae 10459
9f32dd5b
L
10460static void
10461signed_cons (int size)
6482c264 10462{
a442cac5 10463 if (object_64bit)
d182319b
JB
10464 cons_sign = 1;
10465 cons (size);
10466 cons_sign = -1;
6482c264
NC
10467}
10468
d182319b 10469#ifdef TE_PE
6482c264 10470static void
7016a5d5 10471pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
6482c264
NC
10472{
10473 expressionS exp;
10474
10475 do
10476 {
10477 expression (&exp);
10478 if (exp.X_op == O_symbol)
10479 exp.X_op = O_secrel;
10480
10481 emit_expr (&exp, 4);
10482 }
10483 while (*input_line_pointer++ == ',');
10484
10485 input_line_pointer--;
10486 demand_empty_rest_of_line ();
10487}
6482c264
NC
10488#endif
10489
43234a1e
L
10490/* Handle Vector operations. */
10491
10492static char *
f70c6814 10493check_VecOperations (char *op_string)
43234a1e
L
10494{
10495 const reg_entry *mask;
10496 const char *saved;
10497 char *end_op;
10498
f70c6814 10499 while (*op_string)
43234a1e
L
10500 {
10501 saved = op_string;
10502 if (*op_string == '{')
10503 {
10504 op_string++;
10505
10506 /* Check broadcasts. */
d34049e8 10507 if (startswith (op_string, "1to"))
43234a1e 10508 {
5273a3cd 10509 unsigned int bcst_type;
43234a1e 10510
5273a3cd 10511 if (i.broadcast.type)
43234a1e
L
10512 goto duplicated_vec_op;
10513
10514 op_string += 3;
10515 if (*op_string == '8')
8e6e0792 10516 bcst_type = 8;
b28d1bda 10517 else if (*op_string == '4')
8e6e0792 10518 bcst_type = 4;
b28d1bda 10519 else if (*op_string == '2')
8e6e0792 10520 bcst_type = 2;
43234a1e
L
10521 else if (*op_string == '1'
10522 && *(op_string+1) == '6')
10523 {
8e6e0792 10524 bcst_type = 16;
43234a1e
L
10525 op_string++;
10526 }
10527 else
10528 {
10529 as_bad (_("Unsupported broadcast: `%s'"), saved);
10530 return NULL;
10531 }
10532 op_string++;
10533
5273a3cd
JB
10534 i.broadcast.type = bcst_type;
10535 i.broadcast.operand = this_operand;
43234a1e
L
10536 }
10537 /* Check masking operation. */
10538 else if ((mask = parse_register (op_string, &end_op)) != NULL)
10539 {
8a6fb3f9
JB
10540 if (mask == &bad_reg)
10541 return NULL;
10542
43234a1e 10543 /* k0 can't be used for write mask. */
f74a6307 10544 if (mask->reg_type.bitfield.class != RegMask || !mask->reg_num)
43234a1e 10545 {
6d2cd6b2
JB
10546 as_bad (_("`%s%s' can't be used for write mask"),
10547 register_prefix, mask->reg_name);
43234a1e
L
10548 return NULL;
10549 }
10550
6225c532 10551 if (!i.mask.reg)
43234a1e 10552 {
6225c532
JB
10553 i.mask.reg = mask;
10554 i.mask.operand = this_operand;
43234a1e 10555 }
6225c532
JB
10556 else if (i.mask.reg->reg_num)
10557 goto duplicated_vec_op;
43234a1e
L
10558 else
10559 {
6225c532 10560 i.mask.reg = mask;
43234a1e
L
10561
10562 /* Only "{z}" is allowed here. No need to check
10563 zeroing mask explicitly. */
6225c532 10564 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
10565 {
10566 as_bad (_("invalid write mask `%s'"), saved);
10567 return NULL;
10568 }
10569 }
10570
10571 op_string = end_op;
10572 }
10573 /* Check zeroing-flag for masking operation. */
10574 else if (*op_string == 'z')
10575 {
6225c532 10576 if (!i.mask.reg)
43234a1e 10577 {
6225c532
JB
10578 i.mask.reg = reg_k0;
10579 i.mask.zeroing = 1;
10580 i.mask.operand = this_operand;
43234a1e
L
10581 }
10582 else
10583 {
6225c532 10584 if (i.mask.zeroing)
43234a1e
L
10585 {
10586 duplicated_vec_op:
10587 as_bad (_("duplicated `%s'"), saved);
10588 return NULL;
10589 }
10590
6225c532 10591 i.mask.zeroing = 1;
43234a1e
L
10592
10593 /* Only "{%k}" is allowed here. No need to check mask
10594 register explicitly. */
6225c532 10595 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
10596 {
10597 as_bad (_("invalid zeroing-masking `%s'"),
10598 saved);
10599 return NULL;
10600 }
10601 }
10602
10603 op_string++;
10604 }
10605 else
10606 goto unknown_vec_op;
10607
10608 if (*op_string != '}')
10609 {
10610 as_bad (_("missing `}' in `%s'"), saved);
10611 return NULL;
10612 }
10613 op_string++;
0ba3a731
L
10614
10615 /* Strip whitespace since the addition of pseudo prefixes
10616 changed how the scrubber treats '{'. */
10617 if (is_space_char (*op_string))
10618 ++op_string;
10619
43234a1e
L
10620 continue;
10621 }
10622 unknown_vec_op:
10623 /* We don't know this one. */
10624 as_bad (_("unknown vector operation: `%s'"), saved);
10625 return NULL;
10626 }
10627
6225c532 10628 if (i.mask.reg && i.mask.zeroing && !i.mask.reg->reg_num)
6d2cd6b2
JB
10629 {
10630 as_bad (_("zeroing-masking only allowed with write mask"));
10631 return NULL;
10632 }
10633
43234a1e
L
10634 return op_string;
10635}
10636
252b5132 10637static int
70e41ade 10638i386_immediate (char *imm_start)
252b5132
RH
10639{
10640 char *save_input_line_pointer;
f3c180ae 10641 char *gotfree_input_line;
252b5132 10642 segT exp_seg = 0;
47926f60 10643 expressionS *exp;
40fb9820
L
10644 i386_operand_type types;
10645
0dfbf9d7 10646 operand_type_set (&types, ~0);
252b5132
RH
10647
10648 if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
10649 {
31b2323c
L
10650 as_bad (_("at most %d immediate operands are allowed"),
10651 MAX_IMMEDIATE_OPERANDS);
252b5132
RH
10652 return 0;
10653 }
10654
10655 exp = &im_expressions[i.imm_operands++];
520dc8e8 10656 i.op[this_operand].imms = exp;
252b5132
RH
10657
10658 if (is_space_char (*imm_start))
10659 ++imm_start;
10660
10661 save_input_line_pointer = input_line_pointer;
10662 input_line_pointer = imm_start;
10663
d258b828 10664 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
10665 if (gotfree_input_line)
10666 input_line_pointer = gotfree_input_line;
252b5132
RH
10667
10668 exp_seg = expression (exp);
10669
83183c0c 10670 SKIP_WHITESPACE ();
252b5132 10671 if (*input_line_pointer)
f3c180ae 10672 as_bad (_("junk `%s' after expression"), input_line_pointer);
252b5132
RH
10673
10674 input_line_pointer = save_input_line_pointer;
f3c180ae 10675 if (gotfree_input_line)
ee86248c
JB
10676 {
10677 free (gotfree_input_line);
10678
9aac24b1 10679 if (exp->X_op == O_constant)
ee86248c
JB
10680 exp->X_op = O_illegal;
10681 }
10682
9aac24b1
JB
10683 if (exp_seg == reg_section)
10684 {
10685 as_bad (_("illegal immediate register operand %s"), imm_start);
10686 return 0;
10687 }
10688
ee86248c
JB
10689 return i386_finalize_immediate (exp_seg, exp, types, imm_start);
10690}
252b5132 10691
ee86248c
JB
10692static int
10693i386_finalize_immediate (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
10694 i386_operand_type types, const char *imm_start)
10695{
10696 if (exp->X_op == O_absent || exp->X_op == O_illegal || exp->X_op == O_big)
252b5132 10697 {
313c53d1
L
10698 if (imm_start)
10699 as_bad (_("missing or invalid immediate expression `%s'"),
10700 imm_start);
3992d3b7 10701 return 0;
252b5132 10702 }
3e73aa7c 10703 else if (exp->X_op == O_constant)
252b5132 10704 {
47926f60 10705 /* Size it properly later. */
40fb9820 10706 i.types[this_operand].bitfield.imm64 = 1;
a442cac5
JB
10707
10708 /* If not 64bit, sign/zero extend val, to account for wraparound
10709 when !BFD64. */
10710 if (flag_code != CODE_64BIT)
10711 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
252b5132 10712 }
4c63da97 10713#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
f86103b7 10714 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
31312f95 10715 && exp_seg != absolute_section
47926f60 10716 && exp_seg != text_section
24eab124
AM
10717 && exp_seg != data_section
10718 && exp_seg != bss_section
10719 && exp_seg != undefined_section
f86103b7 10720 && !bfd_is_com_section (exp_seg))
252b5132 10721 {
d0b47220 10722 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
252b5132
RH
10723 return 0;
10724 }
10725#endif
10726 else
10727 {
10728 /* This is an address. The size of the address will be
24eab124 10729 determined later, depending on destination register,
3e73aa7c 10730 suffix, or the default for the section. */
40fb9820
L
10731 i.types[this_operand].bitfield.imm8 = 1;
10732 i.types[this_operand].bitfield.imm16 = 1;
10733 i.types[this_operand].bitfield.imm32 = 1;
10734 i.types[this_operand].bitfield.imm32s = 1;
10735 i.types[this_operand].bitfield.imm64 = 1;
c6fb90c8
L
10736 i.types[this_operand] = operand_type_and (i.types[this_operand],
10737 types);
252b5132
RH
10738 }
10739
10740 return 1;
10741}
10742
551c1ca1 10743static char *
e3bb37b5 10744i386_scale (char *scale)
252b5132 10745{
551c1ca1
AM
10746 offsetT val;
10747 char *save = input_line_pointer;
252b5132 10748
551c1ca1
AM
10749 input_line_pointer = scale;
10750 val = get_absolute_expression ();
10751
10752 switch (val)
252b5132 10753 {
551c1ca1 10754 case 1:
252b5132
RH
10755 i.log2_scale_factor = 0;
10756 break;
551c1ca1 10757 case 2:
252b5132
RH
10758 i.log2_scale_factor = 1;
10759 break;
551c1ca1 10760 case 4:
252b5132
RH
10761 i.log2_scale_factor = 2;
10762 break;
551c1ca1 10763 case 8:
252b5132
RH
10764 i.log2_scale_factor = 3;
10765 break;
10766 default:
a724f0f4
JB
10767 {
10768 char sep = *input_line_pointer;
10769
10770 *input_line_pointer = '\0';
10771 as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
10772 scale);
10773 *input_line_pointer = sep;
10774 input_line_pointer = save;
10775 return NULL;
10776 }
252b5132 10777 }
29b0f896 10778 if (i.log2_scale_factor != 0 && i.index_reg == 0)
252b5132
RH
10779 {
10780 as_warn (_("scale factor of %d without an index register"),
24eab124 10781 1 << i.log2_scale_factor);
252b5132 10782 i.log2_scale_factor = 0;
252b5132 10783 }
551c1ca1
AM
10784 scale = input_line_pointer;
10785 input_line_pointer = save;
10786 return scale;
252b5132
RH
10787}
10788
252b5132 10789static int
e3bb37b5 10790i386_displacement (char *disp_start, char *disp_end)
252b5132 10791{
29b0f896 10792 expressionS *exp;
252b5132
RH
10793 segT exp_seg = 0;
10794 char *save_input_line_pointer;
f3c180ae 10795 char *gotfree_input_line;
40fb9820
L
10796 int override;
10797 i386_operand_type bigdisp, types = anydisp;
3992d3b7 10798 int ret;
252b5132 10799
31b2323c
L
10800 if (i.disp_operands == MAX_MEMORY_OPERANDS)
10801 {
10802 as_bad (_("at most %d displacement operands are allowed"),
10803 MAX_MEMORY_OPERANDS);
10804 return 0;
10805 }
10806
0dfbf9d7 10807 operand_type_set (&bigdisp, 0);
6f2f06be 10808 if (i.jumpabsolute
48bcea9f 10809 || i.types[this_operand].bitfield.baseindex
0cfa3eb3
JB
10810 || (current_templates->start->opcode_modifier.jump != JUMP
10811 && current_templates->start->opcode_modifier.jump != JUMP_DWORD))
e05278af 10812 {
48bcea9f 10813 i386_addressing_mode ();
e05278af 10814 override = (i.prefix[ADDR_PREFIX] != 0);
40fb9820
L
10815 if (flag_code == CODE_64BIT)
10816 {
10817 if (!override)
10818 {
10819 bigdisp.bitfield.disp32s = 1;
10820 bigdisp.bitfield.disp64 = 1;
10821 }
48bcea9f
JB
10822 else
10823 bigdisp.bitfield.disp32 = 1;
40fb9820
L
10824 }
10825 else if ((flag_code == CODE_16BIT) ^ override)
40fb9820 10826 bigdisp.bitfield.disp16 = 1;
48bcea9f
JB
10827 else
10828 bigdisp.bitfield.disp32 = 1;
e05278af
JB
10829 }
10830 else
10831 {
376cd056
JB
10832 /* For PC-relative branches, the width of the displacement may be
10833 dependent upon data size, but is never dependent upon address size.
10834 Also make sure to not unintentionally match against a non-PC-relative
10835 branch template. */
10836 static templates aux_templates;
10837 const insn_template *t = current_templates->start;
5b7c81bd 10838 bool has_intel64 = false;
376cd056
JB
10839
10840 aux_templates.start = t;
10841 while (++t < current_templates->end)
10842 {
10843 if (t->opcode_modifier.jump
10844 != current_templates->start->opcode_modifier.jump)
10845 break;
4b5aaf5f 10846 if ((t->opcode_modifier.isa64 >= INTEL64))
5b7c81bd 10847 has_intel64 = true;
376cd056
JB
10848 }
10849 if (t < current_templates->end)
10850 {
10851 aux_templates.end = t;
10852 current_templates = &aux_templates;
10853 }
10854
e05278af 10855 override = (i.prefix[DATA_PREFIX] != 0);
40fb9820
L
10856 if (flag_code == CODE_64BIT)
10857 {
376cd056
JB
10858 if ((override || i.suffix == WORD_MNEM_SUFFIX)
10859 && (!intel64 || !has_intel64))
40fb9820
L
10860 bigdisp.bitfield.disp16 = 1;
10861 else
48bcea9f 10862 bigdisp.bitfield.disp32s = 1;
40fb9820
L
10863 }
10864 else
e05278af
JB
10865 {
10866 if (!override)
10867 override = (i.suffix == (flag_code != CODE_16BIT
10868 ? WORD_MNEM_SUFFIX
10869 : LONG_MNEM_SUFFIX));
40fb9820
L
10870 bigdisp.bitfield.disp32 = 1;
10871 if ((flag_code == CODE_16BIT) ^ override)
10872 {
10873 bigdisp.bitfield.disp32 = 0;
10874 bigdisp.bitfield.disp16 = 1;
10875 }
e05278af 10876 }
e05278af 10877 }
c6fb90c8
L
10878 i.types[this_operand] = operand_type_or (i.types[this_operand],
10879 bigdisp);
252b5132
RH
10880
10881 exp = &disp_expressions[i.disp_operands];
520dc8e8 10882 i.op[this_operand].disps = exp;
252b5132
RH
10883 i.disp_operands++;
10884 save_input_line_pointer = input_line_pointer;
10885 input_line_pointer = disp_start;
10886 END_STRING_AND_SAVE (disp_end);
10887
10888#ifndef GCC_ASM_O_HACK
10889#define GCC_ASM_O_HACK 0
10890#endif
10891#if GCC_ASM_O_HACK
10892 END_STRING_AND_SAVE (disp_end + 1);
40fb9820 10893 if (i.types[this_operand].bitfield.baseIndex
24eab124 10894 && displacement_string_end[-1] == '+')
252b5132
RH
10895 {
10896 /* This hack is to avoid a warning when using the "o"
24eab124
AM
10897 constraint within gcc asm statements.
10898 For instance:
10899
10900 #define _set_tssldt_desc(n,addr,limit,type) \
10901 __asm__ __volatile__ ( \
10902 "movw %w2,%0\n\t" \
10903 "movw %w1,2+%0\n\t" \
10904 "rorl $16,%1\n\t" \
10905 "movb %b1,4+%0\n\t" \
10906 "movb %4,5+%0\n\t" \
10907 "movb $0,6+%0\n\t" \
10908 "movb %h1,7+%0\n\t" \
10909 "rorl $16,%1" \
10910 : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
10911
10912 This works great except that the output assembler ends
10913 up looking a bit weird if it turns out that there is
10914 no offset. You end up producing code that looks like:
10915
10916 #APP
10917 movw $235,(%eax)
10918 movw %dx,2+(%eax)
10919 rorl $16,%edx
10920 movb %dl,4+(%eax)
10921 movb $137,5+(%eax)
10922 movb $0,6+(%eax)
10923 movb %dh,7+(%eax)
10924 rorl $16,%edx
10925 #NO_APP
10926
47926f60 10927 So here we provide the missing zero. */
24eab124
AM
10928
10929 *displacement_string_end = '0';
252b5132
RH
10930 }
10931#endif
d258b828 10932 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
10933 if (gotfree_input_line)
10934 input_line_pointer = gotfree_input_line;
252b5132 10935
24eab124 10936 exp_seg = expression (exp);
252b5132 10937
636c26b0
AM
10938 SKIP_WHITESPACE ();
10939 if (*input_line_pointer)
10940 as_bad (_("junk `%s' after expression"), input_line_pointer);
10941#if GCC_ASM_O_HACK
10942 RESTORE_END_STRING (disp_end + 1);
10943#endif
636c26b0 10944 input_line_pointer = save_input_line_pointer;
636c26b0 10945 if (gotfree_input_line)
ee86248c
JB
10946 {
10947 free (gotfree_input_line);
10948
10949 if (exp->X_op == O_constant || exp->X_op == O_register)
10950 exp->X_op = O_illegal;
10951 }
10952
10953 ret = i386_finalize_displacement (exp_seg, exp, types, disp_start);
10954
10955 RESTORE_END_STRING (disp_end);
10956
10957 return ret;
10958}
10959
10960static int
10961i386_finalize_displacement (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
10962 i386_operand_type types, const char *disp_start)
10963{
10964 i386_operand_type bigdisp;
10965 int ret = 1;
636c26b0 10966
24eab124
AM
10967 /* We do this to make sure that the section symbol is in
10968 the symbol table. We will ultimately change the relocation
47926f60 10969 to be relative to the beginning of the section. */
1ae12ab7 10970 if (i.reloc[this_operand] == BFD_RELOC_386_GOTOFF
d6ab8113
JB
10971 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
10972 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
24eab124 10973 {
636c26b0 10974 if (exp->X_op != O_symbol)
3992d3b7 10975 goto inv_disp;
636c26b0 10976
e5cb08ac 10977 if (S_IS_LOCAL (exp->X_add_symbol)
c64efb4b
L
10978 && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section
10979 && S_GET_SEGMENT (exp->X_add_symbol) != expr_section)
24eab124 10980 section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
24eab124
AM
10981 exp->X_op = O_subtract;
10982 exp->X_op_symbol = GOT_symbol;
1ae12ab7 10983 if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
29b0f896 10984 i.reloc[this_operand] = BFD_RELOC_32_PCREL;
d6ab8113
JB
10985 else if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
10986 i.reloc[this_operand] = BFD_RELOC_64;
23df1078 10987 else
29b0f896 10988 i.reloc[this_operand] = BFD_RELOC_32;
24eab124 10989 }
252b5132 10990
3992d3b7
AM
10991 else if (exp->X_op == O_absent
10992 || exp->X_op == O_illegal
ee86248c 10993 || exp->X_op == O_big)
2daf4fd8 10994 {
3992d3b7
AM
10995 inv_disp:
10996 as_bad (_("missing or invalid displacement expression `%s'"),
2daf4fd8 10997 disp_start);
3992d3b7 10998 ret = 0;
2daf4fd8
AM
10999 }
11000
4c63da97 11001#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
3992d3b7
AM
11002 else if (exp->X_op != O_constant
11003 && OUTPUT_FLAVOR == bfd_target_aout_flavour
11004 && exp_seg != absolute_section
11005 && exp_seg != text_section
11006 && exp_seg != data_section
11007 && exp_seg != bss_section
11008 && exp_seg != undefined_section
11009 && !bfd_is_com_section (exp_seg))
24eab124 11010 {
d0b47220 11011 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
3992d3b7 11012 ret = 0;
24eab124 11013 }
252b5132 11014#endif
3956db08 11015
48bcea9f
JB
11016 if (current_templates->start->opcode_modifier.jump == JUMP_BYTE
11017 /* Constants get taken care of by optimize_disp(). */
11018 && exp->X_op != O_constant)
11019 i.types[this_operand].bitfield.disp8 = 1;
11020
40fb9820
L
11021 /* Check if this is a displacement only operand. */
11022 bigdisp = i.types[this_operand];
11023 bigdisp.bitfield.disp8 = 0;
11024 bigdisp.bitfield.disp16 = 0;
11025 bigdisp.bitfield.disp32 = 0;
11026 bigdisp.bitfield.disp32s = 0;
11027 bigdisp.bitfield.disp64 = 0;
0dfbf9d7 11028 if (operand_type_all_zero (&bigdisp))
c6fb90c8
L
11029 i.types[this_operand] = operand_type_and (i.types[this_operand],
11030 types);
3956db08 11031
3992d3b7 11032 return ret;
252b5132
RH
11033}
11034
2abc2bec
JB
11035/* Return the active addressing mode, taking address override and
11036 registers forming the address into consideration. Update the
11037 address override prefix if necessary. */
47926f60 11038
2abc2bec
JB
11039static enum flag_code
11040i386_addressing_mode (void)
252b5132 11041{
be05d201
L
11042 enum flag_code addr_mode;
11043
11044 if (i.prefix[ADDR_PREFIX])
11045 addr_mode = flag_code == CODE_32BIT ? CODE_16BIT : CODE_32BIT;
a23b33b3
JB
11046 else if (flag_code == CODE_16BIT
11047 && current_templates->start->cpu_flags.bitfield.cpumpx
11048 /* Avoid replacing the "16-bit addressing not allowed" diagnostic
11049 from md_assemble() by "is not a valid base/index expression"
11050 when there is a base and/or index. */
11051 && !i.types[this_operand].bitfield.baseindex)
11052 {
11053 /* MPX insn memory operands with neither base nor index must be forced
11054 to use 32-bit addressing in 16-bit mode. */
11055 addr_mode = CODE_32BIT;
11056 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
11057 ++i.prefixes;
11058 gas_assert (!i.types[this_operand].bitfield.disp16);
11059 gas_assert (!i.types[this_operand].bitfield.disp32);
11060 }
be05d201
L
11061 else
11062 {
11063 addr_mode = flag_code;
11064
24eab124 11065#if INFER_ADDR_PREFIX
be05d201
L
11066 if (i.mem_operands == 0)
11067 {
11068 /* Infer address prefix from the first memory operand. */
11069 const reg_entry *addr_reg = i.base_reg;
11070
11071 if (addr_reg == NULL)
11072 addr_reg = i.index_reg;
eecb386c 11073
be05d201
L
11074 if (addr_reg)
11075 {
e968fc9b 11076 if (addr_reg->reg_type.bitfield.dword)
be05d201
L
11077 addr_mode = CODE_32BIT;
11078 else if (flag_code != CODE_64BIT
dc821c5f 11079 && addr_reg->reg_type.bitfield.word)
be05d201
L
11080 addr_mode = CODE_16BIT;
11081
11082 if (addr_mode != flag_code)
11083 {
11084 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
11085 i.prefixes += 1;
11086 /* Change the size of any displacement too. At most one
11087 of Disp16 or Disp32 is set.
11088 FIXME. There doesn't seem to be any real need for
11089 separate Disp16 and Disp32 flags. The same goes for
11090 Imm16 and Imm32. Removing them would probably clean
11091 up the code quite a lot. */
11092 if (flag_code != CODE_64BIT
11093 && (i.types[this_operand].bitfield.disp16
11094 || i.types[this_operand].bitfield.disp32))
11095 i.types[this_operand]
11096 = operand_type_xor (i.types[this_operand], disp16_32);
11097 }
11098 }
11099 }
24eab124 11100#endif
be05d201
L
11101 }
11102
2abc2bec
JB
11103 return addr_mode;
11104}
11105
11106/* Make sure the memory operand we've been dealt is valid.
11107 Return 1 on success, 0 on a failure. */
11108
11109static int
11110i386_index_check (const char *operand_string)
11111{
11112 const char *kind = "base/index";
11113 enum flag_code addr_mode = i386_addressing_mode ();
a152332d 11114 const insn_template *t = current_templates->start;
2abc2bec 11115
a152332d
JB
11116 if (t->opcode_modifier.isstring
11117 && !t->cpu_flags.bitfield.cpupadlock
fc0763e6
JB
11118 && (current_templates->end[-1].opcode_modifier.isstring
11119 || i.mem_operands))
11120 {
11121 /* Memory operands of string insns are special in that they only allow
11122 a single register (rDI, rSI, or rBX) as their memory address. */
be05d201
L
11123 const reg_entry *expected_reg;
11124 static const char *di_si[][2] =
11125 {
11126 { "esi", "edi" },
11127 { "si", "di" },
11128 { "rsi", "rdi" }
11129 };
11130 static const char *bx[] = { "ebx", "bx", "rbx" };
fc0763e6
JB
11131
11132 kind = "string address";
11133
a152332d 11134 if (t->opcode_modifier.prefixok == PrefixRep)
fc0763e6 11135 {
51c8edf6
JB
11136 int es_op = current_templates->end[-1].opcode_modifier.isstring
11137 - IS_STRING_ES_OP0;
11138 int op = 0;
fc0763e6 11139
51c8edf6 11140 if (!current_templates->end[-1].operand_types[0].bitfield.baseindex
fc0763e6
JB
11141 || ((!i.mem_operands != !intel_syntax)
11142 && current_templates->end[-1].operand_types[1]
11143 .bitfield.baseindex))
51c8edf6 11144 op = 1;
fe0e921f
AM
11145 expected_reg
11146 = (const reg_entry *) str_hash_find (reg_hash,
11147 di_si[addr_mode][op == es_op]);
fc0763e6
JB
11148 }
11149 else
fe0e921f
AM
11150 expected_reg
11151 = (const reg_entry *)str_hash_find (reg_hash, bx[addr_mode]);
fc0763e6 11152
be05d201
L
11153 if (i.base_reg != expected_reg
11154 || i.index_reg
fc0763e6 11155 || operand_type_check (i.types[this_operand], disp))
fc0763e6 11156 {
be05d201
L
11157 /* The second memory operand must have the same size as
11158 the first one. */
11159 if (i.mem_operands
11160 && i.base_reg
11161 && !((addr_mode == CODE_64BIT
dc821c5f 11162 && i.base_reg->reg_type.bitfield.qword)
be05d201 11163 || (addr_mode == CODE_32BIT
dc821c5f
JB
11164 ? i.base_reg->reg_type.bitfield.dword
11165 : i.base_reg->reg_type.bitfield.word)))
be05d201
L
11166 goto bad_address;
11167
fc0763e6
JB
11168 as_warn (_("`%s' is not valid here (expected `%c%s%s%c')"),
11169 operand_string,
11170 intel_syntax ? '[' : '(',
11171 register_prefix,
be05d201 11172 expected_reg->reg_name,
fc0763e6 11173 intel_syntax ? ']' : ')');
be05d201 11174 return 1;
fc0763e6 11175 }
be05d201
L
11176 else
11177 return 1;
11178
dc1e8a47 11179 bad_address:
be05d201
L
11180 as_bad (_("`%s' is not a valid %s expression"),
11181 operand_string, kind);
11182 return 0;
3e73aa7c
JH
11183 }
11184 else
11185 {
be05d201
L
11186 if (addr_mode != CODE_16BIT)
11187 {
11188 /* 32-bit/64-bit checks. */
41eb8e88
L
11189 if (i.disp_encoding == disp_encoding_16bit)
11190 {
11191 bad_disp:
11192 as_bad (_("invalid `%s' prefix"),
11193 addr_mode == CODE_16BIT ? "{disp32}" : "{disp16}");
11194 return 0;
11195 }
11196
be05d201 11197 if ((i.base_reg
e968fc9b
JB
11198 && ((addr_mode == CODE_64BIT
11199 ? !i.base_reg->reg_type.bitfield.qword
11200 : !i.base_reg->reg_type.bitfield.dword)
11201 || (i.index_reg && i.base_reg->reg_num == RegIP)
11202 || i.base_reg->reg_num == RegIZ))
be05d201 11203 || (i.index_reg
1b54b8d7
JB
11204 && !i.index_reg->reg_type.bitfield.xmmword
11205 && !i.index_reg->reg_type.bitfield.ymmword
11206 && !i.index_reg->reg_type.bitfield.zmmword
be05d201 11207 && ((addr_mode == CODE_64BIT
e968fc9b
JB
11208 ? !i.index_reg->reg_type.bitfield.qword
11209 : !i.index_reg->reg_type.bitfield.dword)
be05d201
L
11210 || !i.index_reg->reg_type.bitfield.baseindex)))
11211 goto bad_address;
8178be5b 11212
260cd341 11213 /* bndmk, bndldx, bndstx and mandatory non-vector SIB have special restrictions. */
a152332d 11214 if ((t->opcode_modifier.opcodeprefix == PREFIX_0XF3
389d00a5
JB
11215 && t->opcode_modifier.opcodespace == SPACE_0F
11216 && t->base_opcode == 0x1b)
a152332d 11217 || (t->opcode_modifier.opcodeprefix == PREFIX_NONE
389d00a5
JB
11218 && t->opcode_modifier.opcodespace == SPACE_0F
11219 && (t->base_opcode & ~1) == 0x1a)
a152332d 11220 || t->opcode_modifier.sib == SIBMEM)
8178be5b
JB
11221 {
11222 /* They cannot use RIP-relative addressing. */
e968fc9b 11223 if (i.base_reg && i.base_reg->reg_num == RegIP)
8178be5b
JB
11224 {
11225 as_bad (_("`%s' cannot be used here"), operand_string);
11226 return 0;
11227 }
11228
11229 /* bndldx and bndstx ignore their scale factor. */
a152332d 11230 if (t->opcode_modifier.opcodeprefix == PREFIX_NONE
389d00a5
JB
11231 && t->opcode_modifier.opcodespace == SPACE_0F
11232 && (t->base_opcode & ~1) == 0x1a
8178be5b
JB
11233 && i.log2_scale_factor)
11234 as_warn (_("register scaling is being ignored here"));
11235 }
be05d201
L
11236 }
11237 else
3e73aa7c 11238 {
be05d201 11239 /* 16-bit checks. */
41eb8e88
L
11240 if (i.disp_encoding == disp_encoding_32bit)
11241 goto bad_disp;
11242
3e73aa7c 11243 if ((i.base_reg
dc821c5f 11244 && (!i.base_reg->reg_type.bitfield.word
40fb9820 11245 || !i.base_reg->reg_type.bitfield.baseindex))
3e73aa7c 11246 || (i.index_reg
dc821c5f 11247 && (!i.index_reg->reg_type.bitfield.word
40fb9820 11248 || !i.index_reg->reg_type.bitfield.baseindex
29b0f896
AM
11249 || !(i.base_reg
11250 && i.base_reg->reg_num < 6
11251 && i.index_reg->reg_num >= 6
11252 && i.log2_scale_factor == 0))))
be05d201 11253 goto bad_address;
3e73aa7c
JH
11254 }
11255 }
be05d201 11256 return 1;
24eab124 11257}
252b5132 11258
43234a1e
L
11259/* Handle vector immediates. */
11260
11261static int
11262RC_SAE_immediate (const char *imm_start)
11263{
11264 unsigned int match_found, j;
11265 const char *pstr = imm_start;
11266 expressionS *exp;
11267
11268 if (*pstr != '{')
11269 return 0;
11270
11271 pstr++;
11272 match_found = 0;
11273 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); j++)
11274 {
11275 if (!strncmp (pstr, RC_NamesTable[j].name, RC_NamesTable[j].len))
11276 {
ca5312a2 11277 if (i.rounding.type != rc_none)
43234a1e
L
11278 {
11279 as_bad (_("duplicated `%s'"), imm_start);
11280 return 0;
11281 }
ca5312a2
JB
11282
11283 i.rounding.type = RC_NamesTable[j].type;
11284 i.rounding.operand = this_operand;
11285
43234a1e
L
11286 pstr += RC_NamesTable[j].len;
11287 match_found = 1;
11288 break;
11289 }
11290 }
11291 if (!match_found)
11292 return 0;
11293
11294 if (*pstr++ != '}')
11295 {
11296 as_bad (_("Missing '}': '%s'"), imm_start);
11297 return 0;
11298 }
11299 /* RC/SAE immediate string should contain nothing more. */;
11300 if (*pstr != 0)
11301 {
11302 as_bad (_("Junk after '}': '%s'"), imm_start);
11303 return 0;
11304 }
11305
11306 exp = &im_expressions[i.imm_operands++];
11307 i.op[this_operand].imms = exp;
11308
11309 exp->X_op = O_constant;
11310 exp->X_add_number = 0;
11311 exp->X_add_symbol = (symbolS *) 0;
11312 exp->X_op_symbol = (symbolS *) 0;
11313
11314 i.types[this_operand].bitfield.imm8 = 1;
11315 return 1;
11316}
11317
8325cc63
JB
11318/* Only string instructions can have a second memory operand, so
11319 reduce current_templates to just those if it contains any. */
11320static int
11321maybe_adjust_templates (void)
11322{
11323 const insn_template *t;
11324
11325 gas_assert (i.mem_operands == 1);
11326
11327 for (t = current_templates->start; t < current_templates->end; ++t)
11328 if (t->opcode_modifier.isstring)
11329 break;
11330
11331 if (t < current_templates->end)
11332 {
11333 static templates aux_templates;
5b7c81bd 11334 bool recheck;
8325cc63
JB
11335
11336 aux_templates.start = t;
11337 for (; t < current_templates->end; ++t)
11338 if (!t->opcode_modifier.isstring)
11339 break;
11340 aux_templates.end = t;
11341
11342 /* Determine whether to re-check the first memory operand. */
11343 recheck = (aux_templates.start != current_templates->start
11344 || t != current_templates->end);
11345
11346 current_templates = &aux_templates;
11347
11348 if (recheck)
11349 {
11350 i.mem_operands = 0;
11351 if (i.memop1_string != NULL
11352 && i386_index_check (i.memop1_string) == 0)
11353 return 0;
11354 i.mem_operands = 1;
11355 }
11356 }
11357
11358 return 1;
11359}
11360
9d299bea
JB
11361static INLINE bool starts_memory_operand (char c)
11362{
014fbcda 11363 return ISDIGIT (c)
9d299bea 11364 || is_identifier_char (c)
014fbcda 11365 || strchr ("([\"+-!~", c);
9d299bea
JB
11366}
11367
fc0763e6 11368/* Parse OPERAND_STRING into the i386_insn structure I. Returns zero
47926f60 11369 on error. */
252b5132 11370
252b5132 11371static int
a7619375 11372i386_att_operand (char *operand_string)
252b5132 11373{
af6bdddf
AM
11374 const reg_entry *r;
11375 char *end_op;
24eab124 11376 char *op_string = operand_string;
252b5132 11377
24eab124 11378 if (is_space_char (*op_string))
252b5132
RH
11379 ++op_string;
11380
24eab124 11381 /* We check for an absolute prefix (differentiating,
47926f60 11382 for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
24eab124
AM
11383 if (*op_string == ABSOLUTE_PREFIX)
11384 {
11385 ++op_string;
11386 if (is_space_char (*op_string))
11387 ++op_string;
5b7c81bd 11388 i.jumpabsolute = true;
24eab124 11389 }
252b5132 11390
47926f60 11391 /* Check if operand is a register. */
4d1bb795 11392 if ((r = parse_register (op_string, &end_op)) != NULL)
24eab124 11393 {
40fb9820
L
11394 i386_operand_type temp;
11395
8a6fb3f9
JB
11396 if (r == &bad_reg)
11397 return 0;
11398
24eab124
AM
11399 /* Check for a segment override by searching for ':' after a
11400 segment register. */
11401 op_string = end_op;
11402 if (is_space_char (*op_string))
11403 ++op_string;
00cee14f 11404 if (*op_string == ':' && r->reg_type.bitfield.class == SReg)
24eab124 11405 {
5e042380 11406 i.seg[i.mem_operands] = r;
252b5132 11407
24eab124 11408 /* Skip the ':' and whitespace. */
252b5132
RH
11409 ++op_string;
11410 if (is_space_char (*op_string))
24eab124 11411 ++op_string;
252b5132 11412
47926f60 11413 /* Handle case of %es:*foo. */
c8d541e2 11414 if (!i.jumpabsolute && *op_string == ABSOLUTE_PREFIX)
24eab124
AM
11415 {
11416 ++op_string;
11417 if (is_space_char (*op_string))
11418 ++op_string;
5b7c81bd 11419 i.jumpabsolute = true;
24eab124 11420 }
c8d541e2 11421
9d299bea 11422 if (!starts_memory_operand (*op_string))
c8d541e2
JB
11423 {
11424 as_bad (_("bad memory operand `%s'"), op_string);
11425 return 0;
11426 }
24eab124
AM
11427 goto do_memory_reference;
11428 }
43234a1e
L
11429
11430 /* Handle vector operations. */
11431 if (*op_string == '{')
11432 {
f70c6814 11433 op_string = check_VecOperations (op_string);
43234a1e
L
11434 if (op_string == NULL)
11435 return 0;
11436 }
11437
24eab124
AM
11438 if (*op_string)
11439 {
d0b47220 11440 as_bad (_("junk `%s' after register"), op_string);
24eab124
AM
11441 return 0;
11442 }
40fb9820
L
11443 temp = r->reg_type;
11444 temp.bitfield.baseindex = 0;
c6fb90c8
L
11445 i.types[this_operand] = operand_type_or (i.types[this_operand],
11446 temp);
7d5e4556 11447 i.types[this_operand].bitfield.unspecified = 0;
520dc8e8 11448 i.op[this_operand].regs = r;
24eab124
AM
11449 i.reg_operands++;
11450 }
af6bdddf
AM
11451 else if (*op_string == REGISTER_PREFIX)
11452 {
11453 as_bad (_("bad register name `%s'"), op_string);
11454 return 0;
11455 }
24eab124 11456 else if (*op_string == IMMEDIATE_PREFIX)
ce8a8b2f 11457 {
24eab124 11458 ++op_string;
6f2f06be 11459 if (i.jumpabsolute)
24eab124 11460 {
d0b47220 11461 as_bad (_("immediate operand illegal with absolute jump"));
24eab124
AM
11462 return 0;
11463 }
11464 if (!i386_immediate (op_string))
11465 return 0;
11466 }
43234a1e
L
11467 else if (RC_SAE_immediate (operand_string))
11468 {
11469 /* If it is a RC or SAE immediate, do nothing. */
11470 ;
11471 }
9d299bea 11472 else if (starts_memory_operand (*op_string))
24eab124 11473 {
47926f60 11474 /* This is a memory reference of some sort. */
af6bdddf 11475 char *base_string;
252b5132 11476
47926f60 11477 /* Start and end of displacement string expression (if found). */
eecb386c
AM
11478 char *displacement_string_start;
11479 char *displacement_string_end;
43234a1e 11480 char *vop_start;
252b5132 11481
24eab124 11482 do_memory_reference:
8325cc63
JB
11483 if (i.mem_operands == 1 && !maybe_adjust_templates ())
11484 return 0;
24eab124 11485 if ((i.mem_operands == 1
40fb9820 11486 && !current_templates->start->opcode_modifier.isstring)
24eab124
AM
11487 || i.mem_operands == 2)
11488 {
11489 as_bad (_("too many memory references for `%s'"),
11490 current_templates->start->name);
11491 return 0;
11492 }
252b5132 11493
24eab124
AM
11494 /* Check for base index form. We detect the base index form by
11495 looking for an ')' at the end of the operand, searching
11496 for the '(' matching it, and finding a REGISTER_PREFIX or ','
11497 after the '('. */
af6bdddf 11498 base_string = op_string + strlen (op_string);
c3332e24 11499
43234a1e
L
11500 /* Handle vector operations. */
11501 vop_start = strchr (op_string, '{');
11502 if (vop_start && vop_start < base_string)
11503 {
f70c6814 11504 if (check_VecOperations (vop_start) == NULL)
43234a1e
L
11505 return 0;
11506 base_string = vop_start;
11507 }
11508
af6bdddf
AM
11509 --base_string;
11510 if (is_space_char (*base_string))
11511 --base_string;
252b5132 11512
47926f60 11513 /* If we only have a displacement, set-up for it to be parsed later. */
af6bdddf
AM
11514 displacement_string_start = op_string;
11515 displacement_string_end = base_string + 1;
252b5132 11516
24eab124
AM
11517 if (*base_string == ')')
11518 {
af6bdddf 11519 char *temp_string;
24eab124
AM
11520 unsigned int parens_balanced = 1;
11521 /* We've already checked that the number of left & right ()'s are
47926f60 11522 equal, so this loop will not be infinite. */
24eab124
AM
11523 do
11524 {
11525 base_string--;
11526 if (*base_string == ')')
11527 parens_balanced++;
11528 if (*base_string == '(')
11529 parens_balanced--;
11530 }
11531 while (parens_balanced);
c3332e24 11532
af6bdddf 11533 temp_string = base_string;
c3332e24 11534
24eab124 11535 /* Skip past '(' and whitespace. */
252b5132
RH
11536 ++base_string;
11537 if (is_space_char (*base_string))
24eab124 11538 ++base_string;
252b5132 11539
af6bdddf 11540 if (*base_string == ','
4eed87de
AM
11541 || ((i.base_reg = parse_register (base_string, &end_op))
11542 != NULL))
252b5132 11543 {
af6bdddf 11544 displacement_string_end = temp_string;
252b5132 11545
40fb9820 11546 i.types[this_operand].bitfield.baseindex = 1;
252b5132 11547
af6bdddf 11548 if (i.base_reg)
24eab124 11549 {
8a6fb3f9
JB
11550 if (i.base_reg == &bad_reg)
11551 return 0;
24eab124
AM
11552 base_string = end_op;
11553 if (is_space_char (*base_string))
11554 ++base_string;
af6bdddf
AM
11555 }
11556
11557 /* There may be an index reg or scale factor here. */
11558 if (*base_string == ',')
11559 {
11560 ++base_string;
11561 if (is_space_char (*base_string))
11562 ++base_string;
11563
4eed87de
AM
11564 if ((i.index_reg = parse_register (base_string, &end_op))
11565 != NULL)
24eab124 11566 {
8a6fb3f9
JB
11567 if (i.index_reg == &bad_reg)
11568 return 0;
af6bdddf 11569 base_string = end_op;
24eab124
AM
11570 if (is_space_char (*base_string))
11571 ++base_string;
af6bdddf
AM
11572 if (*base_string == ',')
11573 {
11574 ++base_string;
11575 if (is_space_char (*base_string))
11576 ++base_string;
11577 }
e5cb08ac 11578 else if (*base_string != ')')
af6bdddf 11579 {
4eed87de
AM
11580 as_bad (_("expecting `,' or `)' "
11581 "after index register in `%s'"),
af6bdddf
AM
11582 operand_string);
11583 return 0;
11584 }
24eab124 11585 }
af6bdddf 11586 else if (*base_string == REGISTER_PREFIX)
24eab124 11587 {
f76bf5e0
L
11588 end_op = strchr (base_string, ',');
11589 if (end_op)
11590 *end_op = '\0';
af6bdddf 11591 as_bad (_("bad register name `%s'"), base_string);
24eab124
AM
11592 return 0;
11593 }
252b5132 11594
47926f60 11595 /* Check for scale factor. */
551c1ca1 11596 if (*base_string != ')')
af6bdddf 11597 {
551c1ca1
AM
11598 char *end_scale = i386_scale (base_string);
11599
11600 if (!end_scale)
af6bdddf 11601 return 0;
24eab124 11602
551c1ca1 11603 base_string = end_scale;
af6bdddf
AM
11604 if (is_space_char (*base_string))
11605 ++base_string;
11606 if (*base_string != ')')
11607 {
4eed87de
AM
11608 as_bad (_("expecting `)' "
11609 "after scale factor in `%s'"),
af6bdddf
AM
11610 operand_string);
11611 return 0;
11612 }
11613 }
11614 else if (!i.index_reg)
24eab124 11615 {
4eed87de
AM
11616 as_bad (_("expecting index register or scale factor "
11617 "after `,'; got '%c'"),
af6bdddf 11618 *base_string);
24eab124
AM
11619 return 0;
11620 }
11621 }
af6bdddf 11622 else if (*base_string != ')')
24eab124 11623 {
4eed87de
AM
11624 as_bad (_("expecting `,' or `)' "
11625 "after base register in `%s'"),
af6bdddf 11626 operand_string);
24eab124
AM
11627 return 0;
11628 }
c3332e24 11629 }
af6bdddf 11630 else if (*base_string == REGISTER_PREFIX)
c3332e24 11631 {
f76bf5e0
L
11632 end_op = strchr (base_string, ',');
11633 if (end_op)
11634 *end_op = '\0';
af6bdddf 11635 as_bad (_("bad register name `%s'"), base_string);
24eab124 11636 return 0;
c3332e24 11637 }
24eab124
AM
11638 }
11639
11640 /* If there's an expression beginning the operand, parse it,
11641 assuming displacement_string_start and
11642 displacement_string_end are meaningful. */
11643 if (displacement_string_start != displacement_string_end)
11644 {
11645 if (!i386_displacement (displacement_string_start,
11646 displacement_string_end))
11647 return 0;
11648 }
11649
11650 /* Special case for (%dx) while doing input/output op. */
11651 if (i.base_reg
75e5731b
JB
11652 && i.base_reg->reg_type.bitfield.instance == RegD
11653 && i.base_reg->reg_type.bitfield.word
24eab124
AM
11654 && i.index_reg == 0
11655 && i.log2_scale_factor == 0
11656 && i.seg[i.mem_operands] == 0
40fb9820 11657 && !operand_type_check (i.types[this_operand], disp))
24eab124 11658 {
2fb5be8d 11659 i.types[this_operand] = i.base_reg->reg_type;
24eab124
AM
11660 return 1;
11661 }
11662
eecb386c
AM
11663 if (i386_index_check (operand_string) == 0)
11664 return 0;
c48dadc9 11665 i.flags[this_operand] |= Operand_Mem;
8325cc63
JB
11666 if (i.mem_operands == 0)
11667 i.memop1_string = xstrdup (operand_string);
24eab124
AM
11668 i.mem_operands++;
11669 }
11670 else
ce8a8b2f
AM
11671 {
11672 /* It's not a memory operand; argh! */
24eab124
AM
11673 as_bad (_("invalid char %s beginning operand %d `%s'"),
11674 output_invalid (*op_string),
11675 this_operand + 1,
11676 op_string);
11677 return 0;
11678 }
47926f60 11679 return 1; /* Normal return. */
252b5132
RH
11680}
11681\f
fa94de6b
RM
11682/* Calculate the maximum variable size (i.e., excluding fr_fix)
11683 that an rs_machine_dependent frag may reach. */
11684
11685unsigned int
11686i386_frag_max_var (fragS *frag)
11687{
11688 /* The only relaxable frags are for jumps.
11689 Unconditional jumps can grow by 4 bytes and others by 5 bytes. */
11690 gas_assert (frag->fr_type == rs_machine_dependent);
11691 return TYPE_FROM_RELAX_STATE (frag->fr_subtype) == UNCOND_JUMP ? 4 : 5;
11692}
11693
b084df0b
L
11694#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
11695static int
8dcea932 11696elf_symbol_resolved_in_segment_p (symbolS *fr_symbol, offsetT fr_var)
b084df0b
L
11697{
11698 /* STT_GNU_IFUNC symbol must go through PLT. */
11699 if ((symbol_get_bfdsym (fr_symbol)->flags
11700 & BSF_GNU_INDIRECT_FUNCTION) != 0)
11701 return 0;
11702
11703 if (!S_IS_EXTERNAL (fr_symbol))
11704 /* Symbol may be weak or local. */
11705 return !S_IS_WEAK (fr_symbol);
11706
8dcea932
L
11707 /* Global symbols with non-default visibility can't be preempted. */
11708 if (ELF_ST_VISIBILITY (S_GET_OTHER (fr_symbol)) != STV_DEFAULT)
11709 return 1;
11710
11711 if (fr_var != NO_RELOC)
11712 switch ((enum bfd_reloc_code_real) fr_var)
11713 {
11714 case BFD_RELOC_386_PLT32:
11715 case BFD_RELOC_X86_64_PLT32:
33eaf5de 11716 /* Symbol with PLT relocation may be preempted. */
8dcea932
L
11717 return 0;
11718 default:
11719 abort ();
11720 }
11721
b084df0b
L
11722 /* Global symbols with default visibility in a shared library may be
11723 preempted by another definition. */
8dcea932 11724 return !shared;
b084df0b
L
11725}
11726#endif
11727
79d72f45
HL
11728/* Table 3-2. Macro-Fusible Instructions in Haswell Microarchitecture
11729 Note also work for Skylake and Cascadelake.
11730---------------------------------------------------------------------
11731| JCC | ADD/SUB/CMP | INC/DEC | TEST/AND |
11732| ------ | ----------- | ------- | -------- |
11733| Jo | N | N | Y |
11734| Jno | N | N | Y |
11735| Jc/Jb | Y | N | Y |
11736| Jae/Jnb | Y | N | Y |
11737| Je/Jz | Y | Y | Y |
11738| Jne/Jnz | Y | Y | Y |
11739| Jna/Jbe | Y | N | Y |
11740| Ja/Jnbe | Y | N | Y |
11741| Js | N | N | Y |
11742| Jns | N | N | Y |
11743| Jp/Jpe | N | N | Y |
11744| Jnp/Jpo | N | N | Y |
11745| Jl/Jnge | Y | Y | Y |
11746| Jge/Jnl | Y | Y | Y |
11747| Jle/Jng | Y | Y | Y |
11748| Jg/Jnle | Y | Y | Y |
11749--------------------------------------------------------------------- */
11750static int
11751i386_macro_fusible_p (enum mf_cmp_kind mf_cmp, enum mf_jcc_kind mf_jcc)
11752{
11753 if (mf_cmp == mf_cmp_alu_cmp)
11754 return ((mf_jcc >= mf_jcc_jc && mf_jcc <= mf_jcc_jna)
11755 || mf_jcc == mf_jcc_jl || mf_jcc == mf_jcc_jle);
11756 if (mf_cmp == mf_cmp_incdec)
11757 return (mf_jcc == mf_jcc_je || mf_jcc == mf_jcc_jl
11758 || mf_jcc == mf_jcc_jle);
11759 if (mf_cmp == mf_cmp_test_and)
11760 return 1;
11761 return 0;
11762}
11763
e379e5f3
L
11764/* Return the next non-empty frag. */
11765
11766static fragS *
11767i386_next_non_empty_frag (fragS *fragP)
11768{
11769 /* There may be a frag with a ".fill 0" when there is no room in
11770 the current frag for frag_grow in output_insn. */
11771 for (fragP = fragP->fr_next;
11772 (fragP != NULL
11773 && fragP->fr_type == rs_fill
11774 && fragP->fr_fix == 0);
11775 fragP = fragP->fr_next)
11776 ;
11777 return fragP;
11778}
11779
11780/* Return the next jcc frag after BRANCH_PADDING. */
11781
11782static fragS *
79d72f45 11783i386_next_fusible_jcc_frag (fragS *maybe_cmp_fragP, fragS *pad_fragP)
e379e5f3 11784{
79d72f45
HL
11785 fragS *branch_fragP;
11786 if (!pad_fragP)
e379e5f3
L
11787 return NULL;
11788
79d72f45
HL
11789 if (pad_fragP->fr_type == rs_machine_dependent
11790 && (TYPE_FROM_RELAX_STATE (pad_fragP->fr_subtype)
e379e5f3
L
11791 == BRANCH_PADDING))
11792 {
79d72f45
HL
11793 branch_fragP = i386_next_non_empty_frag (pad_fragP);
11794 if (branch_fragP->fr_type != rs_machine_dependent)
e379e5f3 11795 return NULL;
79d72f45
HL
11796 if (TYPE_FROM_RELAX_STATE (branch_fragP->fr_subtype) == COND_JUMP
11797 && i386_macro_fusible_p (maybe_cmp_fragP->tc_frag_data.mf_type,
11798 pad_fragP->tc_frag_data.mf_type))
11799 return branch_fragP;
e379e5f3
L
11800 }
11801
11802 return NULL;
11803}
11804
11805/* Classify BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags. */
11806
11807static void
11808i386_classify_machine_dependent_frag (fragS *fragP)
11809{
11810 fragS *cmp_fragP;
11811 fragS *pad_fragP;
11812 fragS *branch_fragP;
11813 fragS *next_fragP;
11814 unsigned int max_prefix_length;
11815
11816 if (fragP->tc_frag_data.classified)
11817 return;
11818
11819 /* First scan for BRANCH_PADDING and FUSED_JCC_PADDING. Convert
11820 FUSED_JCC_PADDING and merge BRANCH_PADDING. */
11821 for (next_fragP = fragP;
11822 next_fragP != NULL;
11823 next_fragP = next_fragP->fr_next)
11824 {
11825 next_fragP->tc_frag_data.classified = 1;
11826 if (next_fragP->fr_type == rs_machine_dependent)
11827 switch (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype))
11828 {
11829 case BRANCH_PADDING:
11830 /* The BRANCH_PADDING frag must be followed by a branch
11831 frag. */
11832 branch_fragP = i386_next_non_empty_frag (next_fragP);
11833 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
11834 break;
11835 case FUSED_JCC_PADDING:
11836 /* Check if this is a fused jcc:
11837 FUSED_JCC_PADDING
11838 CMP like instruction
11839 BRANCH_PADDING
11840 COND_JUMP
11841 */
11842 cmp_fragP = i386_next_non_empty_frag (next_fragP);
11843 pad_fragP = i386_next_non_empty_frag (cmp_fragP);
79d72f45 11844 branch_fragP = i386_next_fusible_jcc_frag (next_fragP, pad_fragP);
e379e5f3
L
11845 if (branch_fragP)
11846 {
11847 /* The BRANCH_PADDING frag is merged with the
11848 FUSED_JCC_PADDING frag. */
11849 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
11850 /* CMP like instruction size. */
11851 next_fragP->tc_frag_data.cmp_size = cmp_fragP->fr_fix;
11852 frag_wane (pad_fragP);
11853 /* Skip to branch_fragP. */
11854 next_fragP = branch_fragP;
11855 }
11856 else if (next_fragP->tc_frag_data.max_prefix_length)
11857 {
11858 /* Turn FUSED_JCC_PADDING into BRANCH_PREFIX if it isn't
11859 a fused jcc. */
11860 next_fragP->fr_subtype
11861 = ENCODE_RELAX_STATE (BRANCH_PREFIX, 0);
11862 next_fragP->tc_frag_data.max_bytes
11863 = next_fragP->tc_frag_data.max_prefix_length;
11864 /* This will be updated in the BRANCH_PREFIX scan. */
11865 next_fragP->tc_frag_data.max_prefix_length = 0;
11866 }
11867 else
11868 frag_wane (next_fragP);
11869 break;
11870 }
11871 }
11872
11873 /* Stop if there is no BRANCH_PREFIX. */
11874 if (!align_branch_prefix_size)
11875 return;
11876
11877 /* Scan for BRANCH_PREFIX. */
11878 for (; fragP != NULL; fragP = fragP->fr_next)
11879 {
11880 if (fragP->fr_type != rs_machine_dependent
11881 || (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
11882 != BRANCH_PREFIX))
11883 continue;
11884
11885 /* Count all BRANCH_PREFIX frags before BRANCH_PADDING and
11886 COND_JUMP_PREFIX. */
11887 max_prefix_length = 0;
11888 for (next_fragP = fragP;
11889 next_fragP != NULL;
11890 next_fragP = next_fragP->fr_next)
11891 {
11892 if (next_fragP->fr_type == rs_fill)
11893 /* Skip rs_fill frags. */
11894 continue;
11895 else if (next_fragP->fr_type != rs_machine_dependent)
11896 /* Stop for all other frags. */
11897 break;
11898
11899 /* rs_machine_dependent frags. */
11900 if (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
11901 == BRANCH_PREFIX)
11902 {
11903 /* Count BRANCH_PREFIX frags. */
11904 if (max_prefix_length >= MAX_FUSED_JCC_PADDING_SIZE)
11905 {
11906 max_prefix_length = MAX_FUSED_JCC_PADDING_SIZE;
11907 frag_wane (next_fragP);
11908 }
11909 else
11910 max_prefix_length
11911 += next_fragP->tc_frag_data.max_bytes;
11912 }
11913 else if ((TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
11914 == BRANCH_PADDING)
11915 || (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
11916 == FUSED_JCC_PADDING))
11917 {
11918 /* Stop at BRANCH_PADDING and FUSED_JCC_PADDING. */
11919 fragP->tc_frag_data.u.padding_fragP = next_fragP;
11920 break;
11921 }
11922 else
11923 /* Stop for other rs_machine_dependent frags. */
11924 break;
11925 }
11926
11927 fragP->tc_frag_data.max_prefix_length = max_prefix_length;
11928
11929 /* Skip to the next frag. */
11930 fragP = next_fragP;
11931 }
11932}
11933
11934/* Compute padding size for
11935
11936 FUSED_JCC_PADDING
11937 CMP like instruction
11938 BRANCH_PADDING
11939 COND_JUMP/UNCOND_JUMP
11940
11941 or
11942
11943 BRANCH_PADDING
11944 COND_JUMP/UNCOND_JUMP
11945 */
11946
11947static int
11948i386_branch_padding_size (fragS *fragP, offsetT address)
11949{
11950 unsigned int offset, size, padding_size;
11951 fragS *branch_fragP = fragP->tc_frag_data.u.branch_fragP;
11952
11953 /* The start address of the BRANCH_PADDING or FUSED_JCC_PADDING frag. */
11954 if (!address)
11955 address = fragP->fr_address;
11956 address += fragP->fr_fix;
11957
11958 /* CMP like instrunction size. */
11959 size = fragP->tc_frag_data.cmp_size;
11960
11961 /* The base size of the branch frag. */
11962 size += branch_fragP->fr_fix;
11963
11964 /* Add opcode and displacement bytes for the rs_machine_dependent
11965 branch frag. */
11966 if (branch_fragP->fr_type == rs_machine_dependent)
11967 size += md_relax_table[branch_fragP->fr_subtype].rlx_length;
11968
11969 /* Check if branch is within boundary and doesn't end at the last
11970 byte. */
11971 offset = address & ((1U << align_branch_power) - 1);
11972 if ((offset + size) >= (1U << align_branch_power))
11973 /* Padding needed to avoid crossing boundary. */
11974 padding_size = (1U << align_branch_power) - offset;
11975 else
11976 /* No padding needed. */
11977 padding_size = 0;
11978
11979 /* The return value may be saved in tc_frag_data.length which is
11980 unsigned byte. */
11981 if (!fits_in_unsigned_byte (padding_size))
11982 abort ();
11983
11984 return padding_size;
11985}
11986
11987/* i386_generic_table_relax_frag()
11988
11989 Handle BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags to
11990 grow/shrink padding to align branch frags. Hand others to
11991 relax_frag(). */
11992
11993long
11994i386_generic_table_relax_frag (segT segment, fragS *fragP, long stretch)
11995{
11996 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
11997 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
11998 {
11999 long padding_size = i386_branch_padding_size (fragP, 0);
12000 long grow = padding_size - fragP->tc_frag_data.length;
12001
12002 /* When the BRANCH_PREFIX frag is used, the computed address
12003 must match the actual address and there should be no padding. */
12004 if (fragP->tc_frag_data.padding_address
12005 && (fragP->tc_frag_data.padding_address != fragP->fr_address
12006 || padding_size))
12007 abort ();
12008
12009 /* Update the padding size. */
12010 if (grow)
12011 fragP->tc_frag_data.length = padding_size;
12012
12013 return grow;
12014 }
12015 else if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12016 {
12017 fragS *padding_fragP, *next_fragP;
12018 long padding_size, left_size, last_size;
12019
12020 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
12021 if (!padding_fragP)
12022 /* Use the padding set by the leading BRANCH_PREFIX frag. */
12023 return (fragP->tc_frag_data.length
12024 - fragP->tc_frag_data.last_length);
12025
12026 /* Compute the relative address of the padding frag in the very
12027 first time where the BRANCH_PREFIX frag sizes are zero. */
12028 if (!fragP->tc_frag_data.padding_address)
12029 fragP->tc_frag_data.padding_address
12030 = padding_fragP->fr_address - (fragP->fr_address - stretch);
12031
12032 /* First update the last length from the previous interation. */
12033 left_size = fragP->tc_frag_data.prefix_length;
12034 for (next_fragP = fragP;
12035 next_fragP != padding_fragP;
12036 next_fragP = next_fragP->fr_next)
12037 if (next_fragP->fr_type == rs_machine_dependent
12038 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12039 == BRANCH_PREFIX))
12040 {
12041 if (left_size)
12042 {
12043 int max = next_fragP->tc_frag_data.max_bytes;
12044 if (max)
12045 {
12046 int size;
12047 if (max > left_size)
12048 size = left_size;
12049 else
12050 size = max;
12051 left_size -= size;
12052 next_fragP->tc_frag_data.last_length = size;
12053 }
12054 }
12055 else
12056 next_fragP->tc_frag_data.last_length = 0;
12057 }
12058
12059 /* Check the padding size for the padding frag. */
12060 padding_size = i386_branch_padding_size
12061 (padding_fragP, (fragP->fr_address
12062 + fragP->tc_frag_data.padding_address));
12063
12064 last_size = fragP->tc_frag_data.prefix_length;
12065 /* Check if there is change from the last interation. */
12066 if (padding_size == last_size)
12067 {
12068 /* Update the expected address of the padding frag. */
12069 padding_fragP->tc_frag_data.padding_address
12070 = (fragP->fr_address + padding_size
12071 + fragP->tc_frag_data.padding_address);
12072 return 0;
12073 }
12074
12075 if (padding_size > fragP->tc_frag_data.max_prefix_length)
12076 {
12077 /* No padding if there is no sufficient room. Clear the
12078 expected address of the padding frag. */
12079 padding_fragP->tc_frag_data.padding_address = 0;
12080 padding_size = 0;
12081 }
12082 else
12083 /* Store the expected address of the padding frag. */
12084 padding_fragP->tc_frag_data.padding_address
12085 = (fragP->fr_address + padding_size
12086 + fragP->tc_frag_data.padding_address);
12087
12088 fragP->tc_frag_data.prefix_length = padding_size;
12089
12090 /* Update the length for the current interation. */
12091 left_size = padding_size;
12092 for (next_fragP = fragP;
12093 next_fragP != padding_fragP;
12094 next_fragP = next_fragP->fr_next)
12095 if (next_fragP->fr_type == rs_machine_dependent
12096 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12097 == BRANCH_PREFIX))
12098 {
12099 if (left_size)
12100 {
12101 int max = next_fragP->tc_frag_data.max_bytes;
12102 if (max)
12103 {
12104 int size;
12105 if (max > left_size)
12106 size = left_size;
12107 else
12108 size = max;
12109 left_size -= size;
12110 next_fragP->tc_frag_data.length = size;
12111 }
12112 }
12113 else
12114 next_fragP->tc_frag_data.length = 0;
12115 }
12116
12117 return (fragP->tc_frag_data.length
12118 - fragP->tc_frag_data.last_length);
12119 }
12120 return relax_frag (segment, fragP, stretch);
12121}
12122
ee7fcc42
AM
12123/* md_estimate_size_before_relax()
12124
12125 Called just before relax() for rs_machine_dependent frags. The x86
12126 assembler uses these frags to handle variable size jump
12127 instructions.
12128
12129 Any symbol that is now undefined will not become defined.
12130 Return the correct fr_subtype in the frag.
12131 Return the initial "guess for variable size of frag" to caller.
12132 The guess is actually the growth beyond the fixed part. Whatever
12133 we do to grow the fixed or variable part contributes to our
12134 returned value. */
12135
252b5132 12136int
7016a5d5 12137md_estimate_size_before_relax (fragS *fragP, segT segment)
252b5132 12138{
e379e5f3
L
12139 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12140 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX
12141 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
12142 {
12143 i386_classify_machine_dependent_frag (fragP);
12144 return fragP->tc_frag_data.length;
12145 }
12146
252b5132 12147 /* We've already got fragP->fr_subtype right; all we have to do is
b98ef147
AM
12148 check for un-relaxable symbols. On an ELF system, we can't relax
12149 an externally visible symbol, because it may be overridden by a
12150 shared library. */
12151 if (S_GET_SEGMENT (fragP->fr_symbol) != segment
6d249963 12152#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12153 || (IS_ELF
8dcea932
L
12154 && !elf_symbol_resolved_in_segment_p (fragP->fr_symbol,
12155 fragP->fr_var))
fbeb56a4
DK
12156#endif
12157#if defined (OBJ_COFF) && defined (TE_PE)
7ab9ffdd 12158 || (OUTPUT_FLAVOR == bfd_target_coff_flavour
fbeb56a4 12159 && S_IS_WEAK (fragP->fr_symbol))
b98ef147
AM
12160#endif
12161 )
252b5132 12162 {
b98ef147
AM
12163 /* Symbol is undefined in this segment, or we need to keep a
12164 reloc so that weak symbols can be overridden. */
12165 int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
f86103b7 12166 enum bfd_reloc_code_real reloc_type;
ee7fcc42
AM
12167 unsigned char *opcode;
12168 int old_fr_fix;
eb19308f 12169 fixS *fixP = NULL;
f6af82bd 12170
ee7fcc42 12171 if (fragP->fr_var != NO_RELOC)
1e9cc1c2 12172 reloc_type = (enum bfd_reloc_code_real) fragP->fr_var;
b98ef147 12173 else if (size == 2)
f6af82bd 12174 reloc_type = BFD_RELOC_16_PCREL;
bd7ab16b
L
12175#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
12176 else if (need_plt32_p (fragP->fr_symbol))
12177 reloc_type = BFD_RELOC_X86_64_PLT32;
12178#endif
f6af82bd
AM
12179 else
12180 reloc_type = BFD_RELOC_32_PCREL;
252b5132 12181
ee7fcc42
AM
12182 old_fr_fix = fragP->fr_fix;
12183 opcode = (unsigned char *) fragP->fr_opcode;
12184
fddf5b5b 12185 switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
252b5132 12186 {
fddf5b5b
AM
12187 case UNCOND_JUMP:
12188 /* Make jmp (0xeb) a (d)word displacement jump. */
47926f60 12189 opcode[0] = 0xe9;
252b5132 12190 fragP->fr_fix += size;
eb19308f
JB
12191 fixP = fix_new (fragP, old_fr_fix, size,
12192 fragP->fr_symbol,
12193 fragP->fr_offset, 1,
12194 reloc_type);
252b5132
RH
12195 break;
12196
fddf5b5b 12197 case COND_JUMP86:
412167cb
AM
12198 if (size == 2
12199 && (!no_cond_jump_promotion || fragP->fr_var != NO_RELOC))
fddf5b5b
AM
12200 {
12201 /* Negate the condition, and branch past an
12202 unconditional jump. */
12203 opcode[0] ^= 1;
12204 opcode[1] = 3;
12205 /* Insert an unconditional jump. */
12206 opcode[2] = 0xe9;
12207 /* We added two extra opcode bytes, and have a two byte
12208 offset. */
12209 fragP->fr_fix += 2 + 2;
062cd5e7
AS
12210 fix_new (fragP, old_fr_fix + 2, 2,
12211 fragP->fr_symbol,
12212 fragP->fr_offset, 1,
12213 reloc_type);
fddf5b5b
AM
12214 break;
12215 }
12216 /* Fall through. */
12217
12218 case COND_JUMP:
412167cb
AM
12219 if (no_cond_jump_promotion && fragP->fr_var == NO_RELOC)
12220 {
12221 fragP->fr_fix += 1;
3e02c1cc
AM
12222 fixP = fix_new (fragP, old_fr_fix, 1,
12223 fragP->fr_symbol,
12224 fragP->fr_offset, 1,
12225 BFD_RELOC_8_PCREL);
12226 fixP->fx_signed = 1;
412167cb
AM
12227 break;
12228 }
93c2a809 12229
24eab124 12230 /* This changes the byte-displacement jump 0x7N
fddf5b5b 12231 to the (d)word-displacement jump 0x0f,0x8N. */
252b5132 12232 opcode[1] = opcode[0] + 0x10;
f6af82bd 12233 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
47926f60
KH
12234 /* We've added an opcode byte. */
12235 fragP->fr_fix += 1 + size;
eb19308f
JB
12236 fixP = fix_new (fragP, old_fr_fix + 1, size,
12237 fragP->fr_symbol,
12238 fragP->fr_offset, 1,
12239 reloc_type);
252b5132 12240 break;
fddf5b5b
AM
12241
12242 default:
12243 BAD_CASE (fragP->fr_subtype);
12244 break;
252b5132 12245 }
eb19308f
JB
12246
12247 /* All jumps handled here are signed, but don't unconditionally use a
12248 signed limit check for 32 and 16 bit jumps as we want to allow wrap
12249 around at 4G (outside of 64-bit mode) and 64k. */
12250 if (size == 4 && flag_code == CODE_64BIT)
12251 fixP->fx_signed = 1;
12252
252b5132 12253 frag_wane (fragP);
ee7fcc42 12254 return fragP->fr_fix - old_fr_fix;
252b5132 12255 }
93c2a809 12256
93c2a809
AM
12257 /* Guess size depending on current relax state. Initially the relax
12258 state will correspond to a short jump and we return 1, because
12259 the variable part of the frag (the branch offset) is one byte
12260 long. However, we can relax a section more than once and in that
12261 case we must either set fr_subtype back to the unrelaxed state,
12262 or return the value for the appropriate branch. */
12263 return md_relax_table[fragP->fr_subtype].rlx_length;
ee7fcc42
AM
12264}
12265
47926f60
KH
12266/* Called after relax() is finished.
12267
12268 In: Address of frag.
12269 fr_type == rs_machine_dependent.
12270 fr_subtype is what the address relaxed to.
12271
12272 Out: Any fixSs and constants are set up.
12273 Caller will turn frag into a ".space 0". */
12274
252b5132 12275void
7016a5d5
TG
12276md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec ATTRIBUTE_UNUSED,
12277 fragS *fragP)
252b5132 12278{
29b0f896 12279 unsigned char *opcode;
252b5132 12280 unsigned char *where_to_put_displacement = NULL;
847f7ad4
AM
12281 offsetT target_address;
12282 offsetT opcode_address;
252b5132 12283 unsigned int extension = 0;
847f7ad4 12284 offsetT displacement_from_opcode_start;
252b5132 12285
e379e5f3
L
12286 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12287 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING
12288 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12289 {
12290 /* Generate nop padding. */
12291 unsigned int size = fragP->tc_frag_data.length;
12292 if (size)
12293 {
12294 if (size > fragP->tc_frag_data.max_bytes)
12295 abort ();
12296
12297 if (flag_debug)
12298 {
12299 const char *msg;
12300 const char *branch = "branch";
12301 const char *prefix = "";
12302 fragS *padding_fragP;
12303 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
12304 == BRANCH_PREFIX)
12305 {
12306 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
12307 switch (fragP->tc_frag_data.default_prefix)
12308 {
12309 default:
12310 abort ();
12311 break;
12312 case CS_PREFIX_OPCODE:
12313 prefix = " cs";
12314 break;
12315 case DS_PREFIX_OPCODE:
12316 prefix = " ds";
12317 break;
12318 case ES_PREFIX_OPCODE:
12319 prefix = " es";
12320 break;
12321 case FS_PREFIX_OPCODE:
12322 prefix = " fs";
12323 break;
12324 case GS_PREFIX_OPCODE:
12325 prefix = " gs";
12326 break;
12327 case SS_PREFIX_OPCODE:
12328 prefix = " ss";
12329 break;
12330 }
12331 if (padding_fragP)
12332 msg = _("%s:%u: add %d%s at 0x%llx to align "
12333 "%s within %d-byte boundary\n");
12334 else
12335 msg = _("%s:%u: add additional %d%s at 0x%llx to "
12336 "align %s within %d-byte boundary\n");
12337 }
12338 else
12339 {
12340 padding_fragP = fragP;
12341 msg = _("%s:%u: add %d%s-byte nop at 0x%llx to align "
12342 "%s within %d-byte boundary\n");
12343 }
12344
12345 if (padding_fragP)
12346 switch (padding_fragP->tc_frag_data.branch_type)
12347 {
12348 case align_branch_jcc:
12349 branch = "jcc";
12350 break;
12351 case align_branch_fused:
12352 branch = "fused jcc";
12353 break;
12354 case align_branch_jmp:
12355 branch = "jmp";
12356 break;
12357 case align_branch_call:
12358 branch = "call";
12359 break;
12360 case align_branch_indirect:
12361 branch = "indiret branch";
12362 break;
12363 case align_branch_ret:
12364 branch = "ret";
12365 break;
12366 default:
12367 break;
12368 }
12369
12370 fprintf (stdout, msg,
12371 fragP->fr_file, fragP->fr_line, size, prefix,
12372 (long long) fragP->fr_address, branch,
12373 1 << align_branch_power);
12374 }
12375 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12376 memset (fragP->fr_opcode,
12377 fragP->tc_frag_data.default_prefix, size);
12378 else
12379 i386_generate_nops (fragP, (char *) fragP->fr_opcode,
12380 size, 0);
12381 fragP->fr_fix += size;
12382 }
12383 return;
12384 }
12385
252b5132
RH
12386 opcode = (unsigned char *) fragP->fr_opcode;
12387
47926f60 12388 /* Address we want to reach in file space. */
252b5132 12389 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
252b5132 12390
47926f60 12391 /* Address opcode resides at in file space. */
252b5132
RH
12392 opcode_address = fragP->fr_address + fragP->fr_fix;
12393
47926f60 12394 /* Displacement from opcode start to fill into instruction. */
252b5132
RH
12395 displacement_from_opcode_start = target_address - opcode_address;
12396
fddf5b5b 12397 if ((fragP->fr_subtype & BIG) == 0)
252b5132 12398 {
47926f60
KH
12399 /* Don't have to change opcode. */
12400 extension = 1; /* 1 opcode + 1 displacement */
252b5132 12401 where_to_put_displacement = &opcode[1];
fddf5b5b
AM
12402 }
12403 else
12404 {
12405 if (no_cond_jump_promotion
12406 && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
4eed87de
AM
12407 as_warn_where (fragP->fr_file, fragP->fr_line,
12408 _("long jump required"));
252b5132 12409
fddf5b5b
AM
12410 switch (fragP->fr_subtype)
12411 {
12412 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
12413 extension = 4; /* 1 opcode + 4 displacement */
12414 opcode[0] = 0xe9;
12415 where_to_put_displacement = &opcode[1];
12416 break;
252b5132 12417
fddf5b5b
AM
12418 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
12419 extension = 2; /* 1 opcode + 2 displacement */
12420 opcode[0] = 0xe9;
12421 where_to_put_displacement = &opcode[1];
12422 break;
252b5132 12423
fddf5b5b
AM
12424 case ENCODE_RELAX_STATE (COND_JUMP, BIG):
12425 case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
12426 extension = 5; /* 2 opcode + 4 displacement */
12427 opcode[1] = opcode[0] + 0x10;
12428 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12429 where_to_put_displacement = &opcode[2];
12430 break;
252b5132 12431
fddf5b5b
AM
12432 case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
12433 extension = 3; /* 2 opcode + 2 displacement */
12434 opcode[1] = opcode[0] + 0x10;
12435 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12436 where_to_put_displacement = &opcode[2];
12437 break;
252b5132 12438
fddf5b5b
AM
12439 case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
12440 extension = 4;
12441 opcode[0] ^= 1;
12442 opcode[1] = 3;
12443 opcode[2] = 0xe9;
12444 where_to_put_displacement = &opcode[3];
12445 break;
12446
12447 default:
12448 BAD_CASE (fragP->fr_subtype);
12449 break;
12450 }
252b5132 12451 }
fddf5b5b 12452
7b81dfbb
AJ
12453 /* If size if less then four we are sure that the operand fits,
12454 but if it's 4, then it could be that the displacement is larger
12455 then -/+ 2GB. */
12456 if (DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype) == 4
12457 && object_64bit
12458 && ((addressT) (displacement_from_opcode_start - extension
4eed87de
AM
12459 + ((addressT) 1 << 31))
12460 > (((addressT) 2 << 31) - 1)))
7b81dfbb
AJ
12461 {
12462 as_bad_where (fragP->fr_file, fragP->fr_line,
12463 _("jump target out of range"));
12464 /* Make us emit 0. */
12465 displacement_from_opcode_start = extension;
12466 }
47926f60 12467 /* Now put displacement after opcode. */
252b5132
RH
12468 md_number_to_chars ((char *) where_to_put_displacement,
12469 (valueT) (displacement_from_opcode_start - extension),
fddf5b5b 12470 DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
252b5132
RH
12471 fragP->fr_fix += extension;
12472}
12473\f
7016a5d5 12474/* Apply a fixup (fixP) to segment data, once it has been determined
252b5132
RH
12475 by our caller that we have all the info we need to fix it up.
12476
7016a5d5
TG
12477 Parameter valP is the pointer to the value of the bits.
12478
252b5132
RH
12479 On the 386, immediates, displacements, and data pointers are all in
12480 the same (little-endian) format, so we don't need to care about which
12481 we are handling. */
12482
94f592af 12483void
7016a5d5 12484md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 12485{
94f592af 12486 char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
c6682705 12487 valueT value = *valP;
252b5132 12488
f86103b7 12489#if !defined (TE_Mach)
93382f6d
AM
12490 if (fixP->fx_pcrel)
12491 {
12492 switch (fixP->fx_r_type)
12493 {
5865bb77
ILT
12494 default:
12495 break;
12496
d6ab8113
JB
12497 case BFD_RELOC_64:
12498 fixP->fx_r_type = BFD_RELOC_64_PCREL;
12499 break;
93382f6d 12500 case BFD_RELOC_32:
ae8887b5 12501 case BFD_RELOC_X86_64_32S:
93382f6d
AM
12502 fixP->fx_r_type = BFD_RELOC_32_PCREL;
12503 break;
12504 case BFD_RELOC_16:
12505 fixP->fx_r_type = BFD_RELOC_16_PCREL;
12506 break;
12507 case BFD_RELOC_8:
12508 fixP->fx_r_type = BFD_RELOC_8_PCREL;
12509 break;
12510 }
12511 }
252b5132 12512
a161fe53 12513 if (fixP->fx_addsy != NULL
31312f95 12514 && (fixP->fx_r_type == BFD_RELOC_32_PCREL
d6ab8113 12515 || fixP->fx_r_type == BFD_RELOC_64_PCREL
31312f95 12516 || fixP->fx_r_type == BFD_RELOC_16_PCREL
d258b828 12517 || fixP->fx_r_type == BFD_RELOC_8_PCREL)
31312f95 12518 && !use_rela_relocations)
252b5132 12519 {
31312f95
AM
12520 /* This is a hack. There should be a better way to handle this.
12521 This covers for the fact that bfd_install_relocation will
12522 subtract the current location (for partial_inplace, PC relative
12523 relocations); see more below. */
252b5132 12524#ifndef OBJ_AOUT
718ddfc0 12525 if (IS_ELF
252b5132
RH
12526#ifdef TE_PE
12527 || OUTPUT_FLAVOR == bfd_target_coff_flavour
12528#endif
12529 )
12530 value += fixP->fx_where + fixP->fx_frag->fr_address;
12531#endif
12532#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12533 if (IS_ELF)
252b5132 12534 {
6539b54b 12535 segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
2f66722d 12536
6539b54b 12537 if ((sym_seg == seg
2f66722d 12538 || (symbol_section_p (fixP->fx_addsy)
6539b54b 12539 && sym_seg != absolute_section))
af65af87 12540 && !generic_force_reloc (fixP))
2f66722d
AM
12541 {
12542 /* Yes, we add the values in twice. This is because
6539b54b
AM
12543 bfd_install_relocation subtracts them out again. I think
12544 bfd_install_relocation is broken, but I don't dare change
2f66722d
AM
12545 it. FIXME. */
12546 value += fixP->fx_where + fixP->fx_frag->fr_address;
12547 }
252b5132
RH
12548 }
12549#endif
12550#if defined (OBJ_COFF) && defined (TE_PE)
977cdf5a
NC
12551 /* For some reason, the PE format does not store a
12552 section address offset for a PC relative symbol. */
12553 if (S_GET_SEGMENT (fixP->fx_addsy) != seg
7be1c489 12554 || S_IS_WEAK (fixP->fx_addsy))
252b5132
RH
12555 value += md_pcrel_from (fixP);
12556#endif
12557 }
fbeb56a4 12558#if defined (OBJ_COFF) && defined (TE_PE)
f01c1a09
NC
12559 if (fixP->fx_addsy != NULL
12560 && S_IS_WEAK (fixP->fx_addsy)
12561 /* PR 16858: Do not modify weak function references. */
12562 && ! fixP->fx_pcrel)
fbeb56a4 12563 {
296a8689
NC
12564#if !defined (TE_PEP)
12565 /* For x86 PE weak function symbols are neither PC-relative
12566 nor do they set S_IS_FUNCTION. So the only reliable way
12567 to detect them is to check the flags of their containing
12568 section. */
12569 if (S_GET_SEGMENT (fixP->fx_addsy) != NULL
12570 && S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_CODE)
12571 ;
12572 else
12573#endif
fbeb56a4
DK
12574 value -= S_GET_VALUE (fixP->fx_addsy);
12575 }
12576#endif
252b5132
RH
12577
12578 /* Fix a few things - the dynamic linker expects certain values here,
0234cb7c 12579 and we must not disappoint it. */
252b5132 12580#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12581 if (IS_ELF && fixP->fx_addsy)
47926f60
KH
12582 switch (fixP->fx_r_type)
12583 {
12584 case BFD_RELOC_386_PLT32:
3e73aa7c 12585 case BFD_RELOC_X86_64_PLT32:
b9519cfe
L
12586 /* Make the jump instruction point to the address of the operand.
12587 At runtime we merely add the offset to the actual PLT entry.
12588 NB: Subtract the offset size only for jump instructions. */
12589 if (fixP->fx_pcrel)
12590 value = -4;
47926f60 12591 break;
31312f95 12592
13ae64f3
JJ
12593 case BFD_RELOC_386_TLS_GD:
12594 case BFD_RELOC_386_TLS_LDM:
13ae64f3 12595 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
12596 case BFD_RELOC_386_TLS_IE:
12597 case BFD_RELOC_386_TLS_GOTIE:
67a4f2b7 12598 case BFD_RELOC_386_TLS_GOTDESC:
bffbf940
JJ
12599 case BFD_RELOC_X86_64_TLSGD:
12600 case BFD_RELOC_X86_64_TLSLD:
12601 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7 12602 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
00f7efb6
JJ
12603 value = 0; /* Fully resolved at runtime. No addend. */
12604 /* Fallthrough */
12605 case BFD_RELOC_386_TLS_LE:
12606 case BFD_RELOC_386_TLS_LDO_32:
12607 case BFD_RELOC_386_TLS_LE_32:
12608 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 12609 case BFD_RELOC_X86_64_DTPOFF64:
00f7efb6 12610 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113 12611 case BFD_RELOC_X86_64_TPOFF64:
00f7efb6
JJ
12612 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12613 break;
12614
67a4f2b7
AO
12615 case BFD_RELOC_386_TLS_DESC_CALL:
12616 case BFD_RELOC_X86_64_TLSDESC_CALL:
12617 value = 0; /* Fully resolved at runtime. No addend. */
12618 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12619 fixP->fx_done = 0;
12620 return;
12621
47926f60
KH
12622 case BFD_RELOC_VTABLE_INHERIT:
12623 case BFD_RELOC_VTABLE_ENTRY:
12624 fixP->fx_done = 0;
94f592af 12625 return;
47926f60
KH
12626
12627 default:
12628 break;
12629 }
12630#endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) */
a442cac5
JB
12631
12632 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
12633 if (!object_64bit)
12634 value = extend_to_32bit_address (value);
12635
c6682705 12636 *valP = value;
f86103b7 12637#endif /* !defined (TE_Mach) */
3e73aa7c 12638
3e73aa7c 12639 /* Are we finished with this relocation now? */
c6682705 12640 if (fixP->fx_addsy == NULL)
b8188555
JB
12641 {
12642 fixP->fx_done = 1;
12643 switch (fixP->fx_r_type)
12644 {
12645 case BFD_RELOC_X86_64_32S:
12646 fixP->fx_signed = 1;
12647 break;
12648
12649 default:
12650 break;
12651 }
12652 }
fbeb56a4
DK
12653#if defined (OBJ_COFF) && defined (TE_PE)
12654 else if (fixP->fx_addsy != NULL && S_IS_WEAK (fixP->fx_addsy))
12655 {
12656 fixP->fx_done = 0;
12657 /* Remember value for tc_gen_reloc. */
12658 fixP->fx_addnumber = value;
12659 /* Clear out the frag for now. */
12660 value = 0;
12661 }
12662#endif
3e73aa7c
JH
12663 else if (use_rela_relocations)
12664 {
12665 fixP->fx_no_overflow = 1;
062cd5e7
AS
12666 /* Remember value for tc_gen_reloc. */
12667 fixP->fx_addnumber = value;
3e73aa7c
JH
12668 value = 0;
12669 }
f86103b7 12670
94f592af 12671 md_number_to_chars (p, value, fixP->fx_size);
252b5132 12672}
252b5132 12673\f
6d4af3c2 12674const char *
499ac353 12675md_atof (int type, char *litP, int *sizeP)
252b5132 12676{
499ac353
NC
12677 /* This outputs the LITTLENUMs in REVERSE order;
12678 in accord with the bigendian 386. */
5b7c81bd 12679 return ieee_md_atof (type, litP, sizeP, false);
252b5132
RH
12680}
12681\f
2d545b82 12682static char output_invalid_buf[sizeof (unsigned char) * 2 + 6];
252b5132 12683
252b5132 12684static char *
e3bb37b5 12685output_invalid (int c)
252b5132 12686{
3882b010 12687 if (ISPRINT (c))
f9f21a03
L
12688 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
12689 "'%c'", c);
252b5132 12690 else
f9f21a03 12691 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
2d545b82 12692 "(0x%x)", (unsigned char) c);
252b5132
RH
12693 return output_invalid_buf;
12694}
12695
8a6fb3f9
JB
12696/* Verify that @r can be used in the current context. */
12697
5b7c81bd 12698static bool check_register (const reg_entry *r)
8a6fb3f9
JB
12699{
12700 if (allow_pseudo_reg)
5b7c81bd 12701 return true;
8a6fb3f9
JB
12702
12703 if (operand_type_all_zero (&r->reg_type))
5b7c81bd 12704 return false;
8a6fb3f9
JB
12705
12706 if ((r->reg_type.bitfield.dword
12707 || (r->reg_type.bitfield.class == SReg && r->reg_num > 3)
12708 || r->reg_type.bitfield.class == RegCR
22e00a3f 12709 || r->reg_type.bitfield.class == RegDR)
8a6fb3f9 12710 && !cpu_arch_flags.bitfield.cpui386)
5b7c81bd 12711 return false;
8a6fb3f9 12712
22e00a3f
JB
12713 if (r->reg_type.bitfield.class == RegTR
12714 && (flag_code == CODE_64BIT
12715 || !cpu_arch_flags.bitfield.cpui386
12716 || cpu_arch_isa_flags.bitfield.cpui586
12717 || cpu_arch_isa_flags.bitfield.cpui686))
5b7c81bd 12718 return false;
22e00a3f 12719
8a6fb3f9 12720 if (r->reg_type.bitfield.class == RegMMX && !cpu_arch_flags.bitfield.cpummx)
5b7c81bd 12721 return false;
8a6fb3f9
JB
12722
12723 if (!cpu_arch_flags.bitfield.cpuavx512f)
12724 {
12725 if (r->reg_type.bitfield.zmmword
12726 || r->reg_type.bitfield.class == RegMask)
5b7c81bd 12727 return false;
8a6fb3f9
JB
12728
12729 if (!cpu_arch_flags.bitfield.cpuavx)
12730 {
12731 if (r->reg_type.bitfield.ymmword)
5b7c81bd 12732 return false;
8a6fb3f9
JB
12733
12734 if (!cpu_arch_flags.bitfield.cpusse && r->reg_type.bitfield.xmmword)
5b7c81bd 12735 return false;
8a6fb3f9
JB
12736 }
12737 }
12738
260cd341
LC
12739 if (r->reg_type.bitfield.tmmword
12740 && (!cpu_arch_flags.bitfield.cpuamx_tile
12741 || flag_code != CODE_64BIT))
5b7c81bd 12742 return false;
260cd341 12743
8a6fb3f9 12744 if (r->reg_type.bitfield.class == RegBND && !cpu_arch_flags.bitfield.cpumpx)
5b7c81bd 12745 return false;
8a6fb3f9
JB
12746
12747 /* Don't allow fake index register unless allow_index_reg isn't 0. */
12748 if (!allow_index_reg && r->reg_num == RegIZ)
5b7c81bd 12749 return false;
8a6fb3f9
JB
12750
12751 /* Upper 16 vector registers are only available with VREX in 64bit
12752 mode, and require EVEX encoding. */
12753 if (r->reg_flags & RegVRex)
12754 {
12755 if (!cpu_arch_flags.bitfield.cpuavx512f
12756 || flag_code != CODE_64BIT)
5b7c81bd 12757 return false;
8a6fb3f9 12758
da4977e0
JB
12759 if (i.vec_encoding == vex_encoding_default)
12760 i.vec_encoding = vex_encoding_evex;
12761 else if (i.vec_encoding != vex_encoding_evex)
12762 i.vec_encoding = vex_encoding_error;
8a6fb3f9
JB
12763 }
12764
12765 if (((r->reg_flags & (RegRex64 | RegRex)) || r->reg_type.bitfield.qword)
12766 && (!cpu_arch_flags.bitfield.cpulm || r->reg_type.bitfield.class != RegCR)
12767 && flag_code != CODE_64BIT)
5b7c81bd 12768 return false;
8a6fb3f9
JB
12769
12770 if (r->reg_type.bitfield.class == SReg && r->reg_num == RegFlat
12771 && !intel_syntax)
5b7c81bd 12772 return false;
8a6fb3f9 12773
5b7c81bd 12774 return true;
8a6fb3f9
JB
12775}
12776
af6bdddf 12777/* REG_STRING starts *before* REGISTER_PREFIX. */
252b5132
RH
12778
12779static const reg_entry *
4d1bb795 12780parse_real_register (char *reg_string, char **end_op)
252b5132 12781{
af6bdddf
AM
12782 char *s = reg_string;
12783 char *p;
252b5132
RH
12784 char reg_name_given[MAX_REG_NAME_SIZE + 1];
12785 const reg_entry *r;
12786
12787 /* Skip possible REGISTER_PREFIX and possible whitespace. */
12788 if (*s == REGISTER_PREFIX)
12789 ++s;
12790
12791 if (is_space_char (*s))
12792 ++s;
12793
12794 p = reg_name_given;
af6bdddf 12795 while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
252b5132
RH
12796 {
12797 if (p >= reg_name_given + MAX_REG_NAME_SIZE)
af6bdddf
AM
12798 return (const reg_entry *) NULL;
12799 s++;
252b5132
RH
12800 }
12801
6588847e
DN
12802 /* For naked regs, make sure that we are not dealing with an identifier.
12803 This prevents confusing an identifier like `eax_var' with register
12804 `eax'. */
12805 if (allow_naked_reg && identifier_chars[(unsigned char) *s])
12806 return (const reg_entry *) NULL;
12807
af6bdddf 12808 *end_op = s;
252b5132 12809
629310ab 12810 r = (const reg_entry *) str_hash_find (reg_hash, reg_name_given);
252b5132 12811
5f47d35b 12812 /* Handle floating point regs, allowing spaces in the (i) part. */
6288d05f 12813 if (r == reg_st0)
5f47d35b 12814 {
0e0eea78
JB
12815 if (!cpu_arch_flags.bitfield.cpu8087
12816 && !cpu_arch_flags.bitfield.cpu287
af32b722
JB
12817 && !cpu_arch_flags.bitfield.cpu387
12818 && !allow_pseudo_reg)
0e0eea78
JB
12819 return (const reg_entry *) NULL;
12820
5f47d35b
AM
12821 if (is_space_char (*s))
12822 ++s;
12823 if (*s == '(')
12824 {
af6bdddf 12825 ++s;
5f47d35b
AM
12826 if (is_space_char (*s))
12827 ++s;
12828 if (*s >= '0' && *s <= '7')
12829 {
db557034 12830 int fpr = *s - '0';
af6bdddf 12831 ++s;
5f47d35b
AM
12832 if (is_space_char (*s))
12833 ++s;
12834 if (*s == ')')
12835 {
12836 *end_op = s + 1;
6288d05f 12837 know (r[fpr].reg_num == fpr);
db557034 12838 return r + fpr;
5f47d35b 12839 }
5f47d35b 12840 }
47926f60 12841 /* We have "%st(" then garbage. */
5f47d35b
AM
12842 return (const reg_entry *) NULL;
12843 }
12844 }
12845
8a6fb3f9 12846 return r && check_register (r) ? r : NULL;
252b5132 12847}
4d1bb795
JB
12848
12849/* REG_STRING starts *before* REGISTER_PREFIX. */
12850
12851static const reg_entry *
12852parse_register (char *reg_string, char **end_op)
12853{
12854 const reg_entry *r;
12855
12856 if (*reg_string == REGISTER_PREFIX || allow_naked_reg)
12857 r = parse_real_register (reg_string, end_op);
12858 else
12859 r = NULL;
12860 if (!r)
12861 {
12862 char *save = input_line_pointer;
12863 char c;
12864 symbolS *symbolP;
12865
12866 input_line_pointer = reg_string;
d02603dc 12867 c = get_symbol_name (&reg_string);
4d1bb795
JB
12868 symbolP = symbol_find (reg_string);
12869 if (symbolP && S_GET_SEGMENT (symbolP) == reg_section)
12870 {
12871 const expressionS *e = symbol_get_value_expression (symbolP);
12872
0398aac5 12873 know (e->X_op == O_register);
4eed87de 12874 know (e->X_add_number >= 0
c3fe08fa 12875 && (valueT) e->X_add_number < i386_regtab_size);
4d1bb795 12876 r = i386_regtab + e->X_add_number;
8a6fb3f9
JB
12877 if (!check_register (r))
12878 {
12879 as_bad (_("register '%s%s' cannot be used here"),
12880 register_prefix, r->reg_name);
12881 r = &bad_reg;
12882 }
4d1bb795
JB
12883 *end_op = input_line_pointer;
12884 }
12885 *input_line_pointer = c;
12886 input_line_pointer = save;
12887 }
12888 return r;
12889}
12890
12891int
12892i386_parse_name (char *name, expressionS *e, char *nextcharP)
12893{
12894 const reg_entry *r;
12895 char *end = input_line_pointer;
12896
12897 *end = *nextcharP;
12898 r = parse_register (name, &input_line_pointer);
12899 if (r && end <= input_line_pointer)
12900 {
12901 *nextcharP = *input_line_pointer;
12902 *input_line_pointer = 0;
8a6fb3f9
JB
12903 if (r != &bad_reg)
12904 {
12905 e->X_op = O_register;
12906 e->X_add_number = r - i386_regtab;
12907 }
12908 else
12909 e->X_op = O_illegal;
4d1bb795
JB
12910 return 1;
12911 }
12912 input_line_pointer = end;
12913 *end = 0;
ee86248c 12914 return intel_syntax ? i386_intel_parse_name (name, e) : 0;
4d1bb795
JB
12915}
12916
12917void
12918md_operand (expressionS *e)
12919{
ee86248c
JB
12920 char *end;
12921 const reg_entry *r;
4d1bb795 12922
ee86248c
JB
12923 switch (*input_line_pointer)
12924 {
12925 case REGISTER_PREFIX:
12926 r = parse_real_register (input_line_pointer, &end);
4d1bb795
JB
12927 if (r)
12928 {
12929 e->X_op = O_register;
12930 e->X_add_number = r - i386_regtab;
12931 input_line_pointer = end;
12932 }
ee86248c
JB
12933 break;
12934
12935 case '[':
9c2799c2 12936 gas_assert (intel_syntax);
ee86248c
JB
12937 end = input_line_pointer++;
12938 expression (e);
12939 if (*input_line_pointer == ']')
12940 {
12941 ++input_line_pointer;
12942 e->X_op_symbol = make_expr_symbol (e);
12943 e->X_add_symbol = NULL;
12944 e->X_add_number = 0;
12945 e->X_op = O_index;
12946 }
12947 else
12948 {
12949 e->X_op = O_absent;
12950 input_line_pointer = end;
12951 }
12952 break;
4d1bb795
JB
12953 }
12954}
12955
252b5132 12956\f
4cc782b5 12957#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
b6f8c7c4 12958const char *md_shortopts = "kVQ:sqnO::";
252b5132 12959#else
b6f8c7c4 12960const char *md_shortopts = "qnO::";
252b5132 12961#endif
6e0b89ee 12962
3e73aa7c 12963#define OPTION_32 (OPTION_MD_BASE + 0)
b3b91714
AM
12964#define OPTION_64 (OPTION_MD_BASE + 1)
12965#define OPTION_DIVIDE (OPTION_MD_BASE + 2)
9103f4f4
L
12966#define OPTION_MARCH (OPTION_MD_BASE + 3)
12967#define OPTION_MTUNE (OPTION_MD_BASE + 4)
1efbbeb4
L
12968#define OPTION_MMNEMONIC (OPTION_MD_BASE + 5)
12969#define OPTION_MSYNTAX (OPTION_MD_BASE + 6)
12970#define OPTION_MINDEX_REG (OPTION_MD_BASE + 7)
12971#define OPTION_MNAKED_REG (OPTION_MD_BASE + 8)
bd5dea88 12972#define OPTION_MRELAX_RELOCATIONS (OPTION_MD_BASE + 9)
c0f3af97 12973#define OPTION_MSSE2AVX (OPTION_MD_BASE + 10)
daf50ae7 12974#define OPTION_MSSE_CHECK (OPTION_MD_BASE + 11)
7bab8ab5
JB
12975#define OPTION_MOPERAND_CHECK (OPTION_MD_BASE + 12)
12976#define OPTION_MAVXSCALAR (OPTION_MD_BASE + 13)
12977#define OPTION_X32 (OPTION_MD_BASE + 14)
7e8b059b 12978#define OPTION_MADD_BND_PREFIX (OPTION_MD_BASE + 15)
43234a1e
L
12979#define OPTION_MEVEXLIG (OPTION_MD_BASE + 16)
12980#define OPTION_MEVEXWIG (OPTION_MD_BASE + 17)
167ad85b 12981#define OPTION_MBIG_OBJ (OPTION_MD_BASE + 18)
d1982f93 12982#define OPTION_MOMIT_LOCK_PREFIX (OPTION_MD_BASE + 19)
d3d3c6db 12983#define OPTION_MEVEXRCIG (OPTION_MD_BASE + 20)
8dcea932 12984#define OPTION_MSHARED (OPTION_MD_BASE + 21)
5db04b09
L
12985#define OPTION_MAMD64 (OPTION_MD_BASE + 22)
12986#define OPTION_MINTEL64 (OPTION_MD_BASE + 23)
e4e00185 12987#define OPTION_MFENCE_AS_LOCK_ADD (OPTION_MD_BASE + 24)
b4a3a7b4 12988#define OPTION_X86_USED_NOTE (OPTION_MD_BASE + 25)
03751133 12989#define OPTION_MVEXWIG (OPTION_MD_BASE + 26)
e379e5f3
L
12990#define OPTION_MALIGN_BRANCH_BOUNDARY (OPTION_MD_BASE + 27)
12991#define OPTION_MALIGN_BRANCH_PREFIX_SIZE (OPTION_MD_BASE + 28)
12992#define OPTION_MALIGN_BRANCH (OPTION_MD_BASE + 29)
76cf450b 12993#define OPTION_MBRANCHES_WITH_32B_BOUNDARIES (OPTION_MD_BASE + 30)
ae531041
L
12994#define OPTION_MLFENCE_AFTER_LOAD (OPTION_MD_BASE + 31)
12995#define OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH (OPTION_MD_BASE + 32)
12996#define OPTION_MLFENCE_BEFORE_RET (OPTION_MD_BASE + 33)
b3b91714 12997
99ad8390
NC
12998struct option md_longopts[] =
12999{
3e73aa7c 13000 {"32", no_argument, NULL, OPTION_32},
321098a5 13001#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 13002 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c 13003 {"64", no_argument, NULL, OPTION_64},
351f65ca
L
13004#endif
13005#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 13006 {"x32", no_argument, NULL, OPTION_X32},
8dcea932 13007 {"mshared", no_argument, NULL, OPTION_MSHARED},
b4a3a7b4 13008 {"mx86-used-note", required_argument, NULL, OPTION_X86_USED_NOTE},
6e0b89ee 13009#endif
b3b91714 13010 {"divide", no_argument, NULL, OPTION_DIVIDE},
9103f4f4
L
13011 {"march", required_argument, NULL, OPTION_MARCH},
13012 {"mtune", required_argument, NULL, OPTION_MTUNE},
1efbbeb4
L
13013 {"mmnemonic", required_argument, NULL, OPTION_MMNEMONIC},
13014 {"msyntax", required_argument, NULL, OPTION_MSYNTAX},
13015 {"mindex-reg", no_argument, NULL, OPTION_MINDEX_REG},
13016 {"mnaked-reg", no_argument, NULL, OPTION_MNAKED_REG},
c0f3af97 13017 {"msse2avx", no_argument, NULL, OPTION_MSSE2AVX},
daf50ae7 13018 {"msse-check", required_argument, NULL, OPTION_MSSE_CHECK},
7bab8ab5 13019 {"moperand-check", required_argument, NULL, OPTION_MOPERAND_CHECK},
539f890d 13020 {"mavxscalar", required_argument, NULL, OPTION_MAVXSCALAR},
03751133 13021 {"mvexwig", required_argument, NULL, OPTION_MVEXWIG},
7e8b059b 13022 {"madd-bnd-prefix", no_argument, NULL, OPTION_MADD_BND_PREFIX},
43234a1e
L
13023 {"mevexlig", required_argument, NULL, OPTION_MEVEXLIG},
13024 {"mevexwig", required_argument, NULL, OPTION_MEVEXWIG},
167ad85b
TG
13025# if defined (TE_PE) || defined (TE_PEP)
13026 {"mbig-obj", no_argument, NULL, OPTION_MBIG_OBJ},
13027#endif
d1982f93 13028 {"momit-lock-prefix", required_argument, NULL, OPTION_MOMIT_LOCK_PREFIX},
e4e00185 13029 {"mfence-as-lock-add", required_argument, NULL, OPTION_MFENCE_AS_LOCK_ADD},
0cb4071e 13030 {"mrelax-relocations", required_argument, NULL, OPTION_MRELAX_RELOCATIONS},
d3d3c6db 13031 {"mevexrcig", required_argument, NULL, OPTION_MEVEXRCIG},
e379e5f3
L
13032 {"malign-branch-boundary", required_argument, NULL, OPTION_MALIGN_BRANCH_BOUNDARY},
13033 {"malign-branch-prefix-size", required_argument, NULL, OPTION_MALIGN_BRANCH_PREFIX_SIZE},
13034 {"malign-branch", required_argument, NULL, OPTION_MALIGN_BRANCH},
76cf450b 13035 {"mbranches-within-32B-boundaries", no_argument, NULL, OPTION_MBRANCHES_WITH_32B_BOUNDARIES},
ae531041
L
13036 {"mlfence-after-load", required_argument, NULL, OPTION_MLFENCE_AFTER_LOAD},
13037 {"mlfence-before-indirect-branch", required_argument, NULL,
13038 OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH},
13039 {"mlfence-before-ret", required_argument, NULL, OPTION_MLFENCE_BEFORE_RET},
5db04b09
L
13040 {"mamd64", no_argument, NULL, OPTION_MAMD64},
13041 {"mintel64", no_argument, NULL, OPTION_MINTEL64},
252b5132
RH
13042 {NULL, no_argument, NULL, 0}
13043};
13044size_t md_longopts_size = sizeof (md_longopts);
13045
13046int
17b9d67d 13047md_parse_option (int c, const char *arg)
252b5132 13048{
91d6fa6a 13049 unsigned int j;
e379e5f3 13050 char *arch, *next, *saved, *type;
9103f4f4 13051
252b5132
RH
13052 switch (c)
13053 {
12b55ccc
L
13054 case 'n':
13055 optimize_align_code = 0;
13056 break;
13057
a38cf1db
AM
13058 case 'q':
13059 quiet_warnings = 1;
252b5132
RH
13060 break;
13061
13062#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
a38cf1db
AM
13063 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
13064 should be emitted or not. FIXME: Not implemented. */
13065 case 'Q':
d4693039
JB
13066 if ((arg[0] != 'y' && arg[0] != 'n') || arg[1])
13067 return 0;
252b5132
RH
13068 break;
13069
13070 /* -V: SVR4 argument to print version ID. */
13071 case 'V':
13072 print_version_id ();
13073 break;
13074
a38cf1db
AM
13075 /* -k: Ignore for FreeBSD compatibility. */
13076 case 'k':
252b5132 13077 break;
4cc782b5
ILT
13078
13079 case 's':
13080 /* -s: On i386 Solaris, this tells the native assembler to use
29b0f896 13081 .stab instead of .stab.excl. We always use .stab anyhow. */
4cc782b5 13082 break;
8dcea932
L
13083
13084 case OPTION_MSHARED:
13085 shared = 1;
13086 break;
b4a3a7b4
L
13087
13088 case OPTION_X86_USED_NOTE:
13089 if (strcasecmp (arg, "yes") == 0)
13090 x86_used_note = 1;
13091 else if (strcasecmp (arg, "no") == 0)
13092 x86_used_note = 0;
13093 else
13094 as_fatal (_("invalid -mx86-used-note= option: `%s'"), arg);
13095 break;
13096
13097
99ad8390 13098#endif
321098a5 13099#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 13100 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c
JH
13101 case OPTION_64:
13102 {
13103 const char **list, **l;
13104
3e73aa7c
JH
13105 list = bfd_target_list ();
13106 for (l = list; *l != NULL; l++)
08dedd66 13107 if (startswith (*l, "elf64-x86-64")
99ad8390
NC
13108 || strcmp (*l, "coff-x86-64") == 0
13109 || strcmp (*l, "pe-x86-64") == 0
d382c579
TG
13110 || strcmp (*l, "pei-x86-64") == 0
13111 || strcmp (*l, "mach-o-x86-64") == 0)
6e0b89ee
AM
13112 {
13113 default_arch = "x86_64";
13114 break;
13115 }
3e73aa7c 13116 if (*l == NULL)
2b5d6a91 13117 as_fatal (_("no compiled in support for x86_64"));
3e73aa7c
JH
13118 free (list);
13119 }
13120 break;
13121#endif
252b5132 13122
351f65ca 13123#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 13124 case OPTION_X32:
351f65ca
L
13125 if (IS_ELF)
13126 {
13127 const char **list, **l;
13128
13129 list = bfd_target_list ();
13130 for (l = list; *l != NULL; l++)
08dedd66 13131 if (startswith (*l, "elf32-x86-64"))
351f65ca
L
13132 {
13133 default_arch = "x86_64:32";
13134 break;
13135 }
13136 if (*l == NULL)
2b5d6a91 13137 as_fatal (_("no compiled in support for 32bit x86_64"));
351f65ca
L
13138 free (list);
13139 }
13140 else
13141 as_fatal (_("32bit x86_64 is only supported for ELF"));
13142 break;
13143#endif
13144
6e0b89ee
AM
13145 case OPTION_32:
13146 default_arch = "i386";
13147 break;
13148
b3b91714
AM
13149 case OPTION_DIVIDE:
13150#ifdef SVR4_COMMENT_CHARS
13151 {
13152 char *n, *t;
13153 const char *s;
13154
add39d23 13155 n = XNEWVEC (char, strlen (i386_comment_chars) + 1);
b3b91714
AM
13156 t = n;
13157 for (s = i386_comment_chars; *s != '\0'; s++)
13158 if (*s != '/')
13159 *t++ = *s;
13160 *t = '\0';
13161 i386_comment_chars = n;
13162 }
13163#endif
13164 break;
13165
9103f4f4 13166 case OPTION_MARCH:
293f5f65
L
13167 saved = xstrdup (arg);
13168 arch = saved;
13169 /* Allow -march=+nosse. */
13170 if (*arch == '+')
13171 arch++;
6305a203 13172 do
9103f4f4 13173 {
6305a203 13174 if (*arch == '.')
2b5d6a91 13175 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
13176 next = strchr (arch, '+');
13177 if (next)
13178 *next++ = '\0';
91d6fa6a 13179 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 13180 {
91d6fa6a 13181 if (strcmp (arch, cpu_arch [j].name) == 0)
ccc9c027 13182 {
6305a203 13183 /* Processor. */
1ded5609
JB
13184 if (! cpu_arch[j].flags.bitfield.cpui386)
13185 continue;
13186
91d6fa6a 13187 cpu_arch_name = cpu_arch[j].name;
6305a203 13188 cpu_sub_arch_name = NULL;
91d6fa6a
NC
13189 cpu_arch_flags = cpu_arch[j].flags;
13190 cpu_arch_isa = cpu_arch[j].type;
13191 cpu_arch_isa_flags = cpu_arch[j].flags;
6305a203
L
13192 if (!cpu_arch_tune_set)
13193 {
13194 cpu_arch_tune = cpu_arch_isa;
13195 cpu_arch_tune_flags = cpu_arch_isa_flags;
13196 }
13197 break;
13198 }
91d6fa6a
NC
13199 else if (*cpu_arch [j].name == '.'
13200 && strcmp (arch, cpu_arch [j].name + 1) == 0)
6305a203 13201 {
33eaf5de 13202 /* ISA extension. */
6305a203 13203 i386_cpu_flags flags;
309d3373 13204
293f5f65
L
13205 flags = cpu_flags_or (cpu_arch_flags,
13206 cpu_arch[j].flags);
81486035 13207
5b64d091 13208 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
6305a203
L
13209 {
13210 if (cpu_sub_arch_name)
13211 {
13212 char *name = cpu_sub_arch_name;
13213 cpu_sub_arch_name = concat (name,
91d6fa6a 13214 cpu_arch[j].name,
1bf57e9f 13215 (const char *) NULL);
6305a203
L
13216 free (name);
13217 }
13218 else
91d6fa6a 13219 cpu_sub_arch_name = xstrdup (cpu_arch[j].name);
6305a203 13220 cpu_arch_flags = flags;
a586129e 13221 cpu_arch_isa_flags = flags;
6305a203 13222 }
0089dace
L
13223 else
13224 cpu_arch_isa_flags
13225 = cpu_flags_or (cpu_arch_isa_flags,
13226 cpu_arch[j].flags);
6305a203 13227 break;
ccc9c027 13228 }
9103f4f4 13229 }
6305a203 13230
293f5f65
L
13231 if (j >= ARRAY_SIZE (cpu_arch))
13232 {
33eaf5de 13233 /* Disable an ISA extension. */
293f5f65
L
13234 for (j = 0; j < ARRAY_SIZE (cpu_noarch); j++)
13235 if (strcmp (arch, cpu_noarch [j].name) == 0)
13236 {
13237 i386_cpu_flags flags;
13238
13239 flags = cpu_flags_and_not (cpu_arch_flags,
13240 cpu_noarch[j].flags);
13241 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
13242 {
13243 if (cpu_sub_arch_name)
13244 {
13245 char *name = cpu_sub_arch_name;
13246 cpu_sub_arch_name = concat (arch,
13247 (const char *) NULL);
13248 free (name);
13249 }
13250 else
13251 cpu_sub_arch_name = xstrdup (arch);
13252 cpu_arch_flags = flags;
13253 cpu_arch_isa_flags = flags;
13254 }
13255 break;
13256 }
13257
13258 if (j >= ARRAY_SIZE (cpu_noarch))
13259 j = ARRAY_SIZE (cpu_arch);
13260 }
13261
91d6fa6a 13262 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 13263 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
13264
13265 arch = next;
9103f4f4 13266 }
293f5f65
L
13267 while (next != NULL);
13268 free (saved);
9103f4f4
L
13269 break;
13270
13271 case OPTION_MTUNE:
13272 if (*arg == '.')
2b5d6a91 13273 as_fatal (_("invalid -mtune= option: `%s'"), arg);
91d6fa6a 13274 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 13275 {
91d6fa6a 13276 if (strcmp (arg, cpu_arch [j].name) == 0)
9103f4f4 13277 {
ccc9c027 13278 cpu_arch_tune_set = 1;
91d6fa6a
NC
13279 cpu_arch_tune = cpu_arch [j].type;
13280 cpu_arch_tune_flags = cpu_arch[j].flags;
9103f4f4
L
13281 break;
13282 }
13283 }
91d6fa6a 13284 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 13285 as_fatal (_("invalid -mtune= option: `%s'"), arg);
9103f4f4
L
13286 break;
13287
1efbbeb4
L
13288 case OPTION_MMNEMONIC:
13289 if (strcasecmp (arg, "att") == 0)
13290 intel_mnemonic = 0;
13291 else if (strcasecmp (arg, "intel") == 0)
13292 intel_mnemonic = 1;
13293 else
2b5d6a91 13294 as_fatal (_("invalid -mmnemonic= option: `%s'"), arg);
1efbbeb4
L
13295 break;
13296
13297 case OPTION_MSYNTAX:
13298 if (strcasecmp (arg, "att") == 0)
13299 intel_syntax = 0;
13300 else if (strcasecmp (arg, "intel") == 0)
13301 intel_syntax = 1;
13302 else
2b5d6a91 13303 as_fatal (_("invalid -msyntax= option: `%s'"), arg);
1efbbeb4
L
13304 break;
13305
13306 case OPTION_MINDEX_REG:
13307 allow_index_reg = 1;
13308 break;
13309
13310 case OPTION_MNAKED_REG:
13311 allow_naked_reg = 1;
13312 break;
13313
c0f3af97
L
13314 case OPTION_MSSE2AVX:
13315 sse2avx = 1;
13316 break;
13317
daf50ae7
L
13318 case OPTION_MSSE_CHECK:
13319 if (strcasecmp (arg, "error") == 0)
7bab8ab5 13320 sse_check = check_error;
daf50ae7 13321 else if (strcasecmp (arg, "warning") == 0)
7bab8ab5 13322 sse_check = check_warning;
daf50ae7 13323 else if (strcasecmp (arg, "none") == 0)
7bab8ab5 13324 sse_check = check_none;
daf50ae7 13325 else
2b5d6a91 13326 as_fatal (_("invalid -msse-check= option: `%s'"), arg);
daf50ae7
L
13327 break;
13328
7bab8ab5
JB
13329 case OPTION_MOPERAND_CHECK:
13330 if (strcasecmp (arg, "error") == 0)
13331 operand_check = check_error;
13332 else if (strcasecmp (arg, "warning") == 0)
13333 operand_check = check_warning;
13334 else if (strcasecmp (arg, "none") == 0)
13335 operand_check = check_none;
13336 else
13337 as_fatal (_("invalid -moperand-check= option: `%s'"), arg);
13338 break;
13339
539f890d
L
13340 case OPTION_MAVXSCALAR:
13341 if (strcasecmp (arg, "128") == 0)
13342 avxscalar = vex128;
13343 else if (strcasecmp (arg, "256") == 0)
13344 avxscalar = vex256;
13345 else
2b5d6a91 13346 as_fatal (_("invalid -mavxscalar= option: `%s'"), arg);
539f890d
L
13347 break;
13348
03751133
L
13349 case OPTION_MVEXWIG:
13350 if (strcmp (arg, "0") == 0)
40c9c8de 13351 vexwig = vexw0;
03751133 13352 else if (strcmp (arg, "1") == 0)
40c9c8de 13353 vexwig = vexw1;
03751133
L
13354 else
13355 as_fatal (_("invalid -mvexwig= option: `%s'"), arg);
13356 break;
13357
7e8b059b
L
13358 case OPTION_MADD_BND_PREFIX:
13359 add_bnd_prefix = 1;
13360 break;
13361
43234a1e
L
13362 case OPTION_MEVEXLIG:
13363 if (strcmp (arg, "128") == 0)
13364 evexlig = evexl128;
13365 else if (strcmp (arg, "256") == 0)
13366 evexlig = evexl256;
13367 else if (strcmp (arg, "512") == 0)
13368 evexlig = evexl512;
13369 else
13370 as_fatal (_("invalid -mevexlig= option: `%s'"), arg);
13371 break;
13372
d3d3c6db
IT
13373 case OPTION_MEVEXRCIG:
13374 if (strcmp (arg, "rne") == 0)
13375 evexrcig = rne;
13376 else if (strcmp (arg, "rd") == 0)
13377 evexrcig = rd;
13378 else if (strcmp (arg, "ru") == 0)
13379 evexrcig = ru;
13380 else if (strcmp (arg, "rz") == 0)
13381 evexrcig = rz;
13382 else
13383 as_fatal (_("invalid -mevexrcig= option: `%s'"), arg);
13384 break;
13385
43234a1e
L
13386 case OPTION_MEVEXWIG:
13387 if (strcmp (arg, "0") == 0)
13388 evexwig = evexw0;
13389 else if (strcmp (arg, "1") == 0)
13390 evexwig = evexw1;
13391 else
13392 as_fatal (_("invalid -mevexwig= option: `%s'"), arg);
13393 break;
13394
167ad85b
TG
13395# if defined (TE_PE) || defined (TE_PEP)
13396 case OPTION_MBIG_OBJ:
13397 use_big_obj = 1;
13398 break;
13399#endif
13400
d1982f93 13401 case OPTION_MOMIT_LOCK_PREFIX:
d022bddd
IT
13402 if (strcasecmp (arg, "yes") == 0)
13403 omit_lock_prefix = 1;
13404 else if (strcasecmp (arg, "no") == 0)
13405 omit_lock_prefix = 0;
13406 else
13407 as_fatal (_("invalid -momit-lock-prefix= option: `%s'"), arg);
13408 break;
13409
e4e00185
AS
13410 case OPTION_MFENCE_AS_LOCK_ADD:
13411 if (strcasecmp (arg, "yes") == 0)
13412 avoid_fence = 1;
13413 else if (strcasecmp (arg, "no") == 0)
13414 avoid_fence = 0;
13415 else
13416 as_fatal (_("invalid -mfence-as-lock-add= option: `%s'"), arg);
13417 break;
13418
ae531041
L
13419 case OPTION_MLFENCE_AFTER_LOAD:
13420 if (strcasecmp (arg, "yes") == 0)
13421 lfence_after_load = 1;
13422 else if (strcasecmp (arg, "no") == 0)
13423 lfence_after_load = 0;
13424 else
13425 as_fatal (_("invalid -mlfence-after-load= option: `%s'"), arg);
13426 break;
13427
13428 case OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH:
13429 if (strcasecmp (arg, "all") == 0)
a09f656b 13430 {
13431 lfence_before_indirect_branch = lfence_branch_all;
13432 if (lfence_before_ret == lfence_before_ret_none)
13433 lfence_before_ret = lfence_before_ret_shl;
13434 }
ae531041
L
13435 else if (strcasecmp (arg, "memory") == 0)
13436 lfence_before_indirect_branch = lfence_branch_memory;
13437 else if (strcasecmp (arg, "register") == 0)
13438 lfence_before_indirect_branch = lfence_branch_register;
13439 else if (strcasecmp (arg, "none") == 0)
13440 lfence_before_indirect_branch = lfence_branch_none;
13441 else
13442 as_fatal (_("invalid -mlfence-before-indirect-branch= option: `%s'"),
13443 arg);
13444 break;
13445
13446 case OPTION_MLFENCE_BEFORE_RET:
13447 if (strcasecmp (arg, "or") == 0)
13448 lfence_before_ret = lfence_before_ret_or;
13449 else if (strcasecmp (arg, "not") == 0)
13450 lfence_before_ret = lfence_before_ret_not;
a09f656b 13451 else if (strcasecmp (arg, "shl") == 0 || strcasecmp (arg, "yes") == 0)
13452 lfence_before_ret = lfence_before_ret_shl;
ae531041
L
13453 else if (strcasecmp (arg, "none") == 0)
13454 lfence_before_ret = lfence_before_ret_none;
13455 else
13456 as_fatal (_("invalid -mlfence-before-ret= option: `%s'"),
13457 arg);
13458 break;
13459
0cb4071e
L
13460 case OPTION_MRELAX_RELOCATIONS:
13461 if (strcasecmp (arg, "yes") == 0)
13462 generate_relax_relocations = 1;
13463 else if (strcasecmp (arg, "no") == 0)
13464 generate_relax_relocations = 0;
13465 else
13466 as_fatal (_("invalid -mrelax-relocations= option: `%s'"), arg);
13467 break;
13468
e379e5f3
L
13469 case OPTION_MALIGN_BRANCH_BOUNDARY:
13470 {
13471 char *end;
13472 long int align = strtoul (arg, &end, 0);
13473 if (*end == '\0')
13474 {
13475 if (align == 0)
13476 {
13477 align_branch_power = 0;
13478 break;
13479 }
13480 else if (align >= 16)
13481 {
13482 int align_power;
13483 for (align_power = 0;
13484 (align & 1) == 0;
13485 align >>= 1, align_power++)
13486 continue;
13487 /* Limit alignment power to 31. */
13488 if (align == 1 && align_power < 32)
13489 {
13490 align_branch_power = align_power;
13491 break;
13492 }
13493 }
13494 }
13495 as_fatal (_("invalid -malign-branch-boundary= value: %s"), arg);
13496 }
13497 break;
13498
13499 case OPTION_MALIGN_BRANCH_PREFIX_SIZE:
13500 {
13501 char *end;
13502 int align = strtoul (arg, &end, 0);
13503 /* Some processors only support 5 prefixes. */
13504 if (*end == '\0' && align >= 0 && align < 6)
13505 {
13506 align_branch_prefix_size = align;
13507 break;
13508 }
13509 as_fatal (_("invalid -malign-branch-prefix-size= value: %s"),
13510 arg);
13511 }
13512 break;
13513
13514 case OPTION_MALIGN_BRANCH:
13515 align_branch = 0;
13516 saved = xstrdup (arg);
13517 type = saved;
13518 do
13519 {
13520 next = strchr (type, '+');
13521 if (next)
13522 *next++ = '\0';
13523 if (strcasecmp (type, "jcc") == 0)
13524 align_branch |= align_branch_jcc_bit;
13525 else if (strcasecmp (type, "fused") == 0)
13526 align_branch |= align_branch_fused_bit;
13527 else if (strcasecmp (type, "jmp") == 0)
13528 align_branch |= align_branch_jmp_bit;
13529 else if (strcasecmp (type, "call") == 0)
13530 align_branch |= align_branch_call_bit;
13531 else if (strcasecmp (type, "ret") == 0)
13532 align_branch |= align_branch_ret_bit;
13533 else if (strcasecmp (type, "indirect") == 0)
13534 align_branch |= align_branch_indirect_bit;
13535 else
13536 as_fatal (_("invalid -malign-branch= option: `%s'"), arg);
13537 type = next;
13538 }
13539 while (next != NULL);
13540 free (saved);
13541 break;
13542
76cf450b
L
13543 case OPTION_MBRANCHES_WITH_32B_BOUNDARIES:
13544 align_branch_power = 5;
13545 align_branch_prefix_size = 5;
13546 align_branch = (align_branch_jcc_bit
13547 | align_branch_fused_bit
13548 | align_branch_jmp_bit);
13549 break;
13550
5db04b09 13551 case OPTION_MAMD64:
4b5aaf5f 13552 isa64 = amd64;
5db04b09
L
13553 break;
13554
13555 case OPTION_MINTEL64:
4b5aaf5f 13556 isa64 = intel64;
5db04b09
L
13557 break;
13558
b6f8c7c4
L
13559 case 'O':
13560 if (arg == NULL)
13561 {
13562 optimize = 1;
13563 /* Turn off -Os. */
13564 optimize_for_space = 0;
13565 }
13566 else if (*arg == 's')
13567 {
13568 optimize_for_space = 1;
13569 /* Turn on all encoding optimizations. */
41fd2579 13570 optimize = INT_MAX;
b6f8c7c4
L
13571 }
13572 else
13573 {
13574 optimize = atoi (arg);
13575 /* Turn off -Os. */
13576 optimize_for_space = 0;
13577 }
13578 break;
13579
252b5132
RH
13580 default:
13581 return 0;
13582 }
13583 return 1;
13584}
13585
8a2c8fef
L
13586#define MESSAGE_TEMPLATE \
13587" "
13588
293f5f65
L
13589static char *
13590output_message (FILE *stream, char *p, char *message, char *start,
13591 int *left_p, const char *name, int len)
13592{
13593 int size = sizeof (MESSAGE_TEMPLATE);
13594 int left = *left_p;
13595
13596 /* Reserve 2 spaces for ", " or ",\0" */
13597 left -= len + 2;
13598
13599 /* Check if there is any room. */
13600 if (left >= 0)
13601 {
13602 if (p != start)
13603 {
13604 *p++ = ',';
13605 *p++ = ' ';
13606 }
13607 p = mempcpy (p, name, len);
13608 }
13609 else
13610 {
13611 /* Output the current message now and start a new one. */
13612 *p++ = ',';
13613 *p = '\0';
13614 fprintf (stream, "%s\n", message);
13615 p = start;
13616 left = size - (start - message) - len - 2;
13617
13618 gas_assert (left >= 0);
13619
13620 p = mempcpy (p, name, len);
13621 }
13622
13623 *left_p = left;
13624 return p;
13625}
13626
8a2c8fef 13627static void
1ded5609 13628show_arch (FILE *stream, int ext, int check)
8a2c8fef
L
13629{
13630 static char message[] = MESSAGE_TEMPLATE;
13631 char *start = message + 27;
13632 char *p;
13633 int size = sizeof (MESSAGE_TEMPLATE);
13634 int left;
13635 const char *name;
13636 int len;
13637 unsigned int j;
13638
13639 p = start;
13640 left = size - (start - message);
13641 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13642 {
13643 /* Should it be skipped? */
13644 if (cpu_arch [j].skip)
13645 continue;
13646
13647 name = cpu_arch [j].name;
13648 len = cpu_arch [j].len;
13649 if (*name == '.')
13650 {
13651 /* It is an extension. Skip if we aren't asked to show it. */
13652 if (ext)
13653 {
13654 name++;
13655 len--;
13656 }
13657 else
13658 continue;
13659 }
13660 else if (ext)
13661 {
13662 /* It is an processor. Skip if we show only extension. */
13663 continue;
13664 }
1ded5609
JB
13665 else if (check && ! cpu_arch[j].flags.bitfield.cpui386)
13666 {
13667 /* It is an impossible processor - skip. */
13668 continue;
13669 }
8a2c8fef 13670
293f5f65 13671 p = output_message (stream, p, message, start, &left, name, len);
8a2c8fef
L
13672 }
13673
293f5f65
L
13674 /* Display disabled extensions. */
13675 if (ext)
13676 for (j = 0; j < ARRAY_SIZE (cpu_noarch); j++)
13677 {
13678 name = cpu_noarch [j].name;
13679 len = cpu_noarch [j].len;
13680 p = output_message (stream, p, message, start, &left, name,
13681 len);
13682 }
13683
8a2c8fef
L
13684 *p = '\0';
13685 fprintf (stream, "%s\n", message);
13686}
13687
252b5132 13688void
8a2c8fef 13689md_show_usage (FILE *stream)
252b5132 13690{
4cc782b5
ILT
13691#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13692 fprintf (stream, _("\
d4693039 13693 -Qy, -Qn ignored\n\
a38cf1db 13694 -V print assembler version number\n\
b3b91714
AM
13695 -k ignored\n"));
13696#endif
13697 fprintf (stream, _("\
12b55ccc 13698 -n Do not optimize code alignment\n\
b3b91714
AM
13699 -q quieten some warnings\n"));
13700#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13701 fprintf (stream, _("\
a38cf1db 13702 -s ignored\n"));
b3b91714 13703#endif
d7f449c0
L
13704#if defined BFD64 && (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
13705 || defined (TE_PE) || defined (TE_PEP))
751d281c 13706 fprintf (stream, _("\
570561f7 13707 --32/--64/--x32 generate 32bit/64bit/x32 code\n"));
751d281c 13708#endif
b3b91714
AM
13709#ifdef SVR4_COMMENT_CHARS
13710 fprintf (stream, _("\
13711 --divide do not treat `/' as a comment character\n"));
a38cf1db
AM
13712#else
13713 fprintf (stream, _("\
b3b91714 13714 --divide ignored\n"));
4cc782b5 13715#endif
9103f4f4 13716 fprintf (stream, _("\
6305a203 13717 -march=CPU[,+EXTENSION...]\n\
8a2c8fef 13718 generate code for CPU and EXTENSION, CPU is one of:\n"));
1ded5609 13719 show_arch (stream, 0, 1);
8a2c8fef
L
13720 fprintf (stream, _("\
13721 EXTENSION is combination of:\n"));
1ded5609 13722 show_arch (stream, 1, 0);
6305a203 13723 fprintf (stream, _("\
8a2c8fef 13724 -mtune=CPU optimize for CPU, CPU is one of:\n"));
1ded5609 13725 show_arch (stream, 0, 0);
ba104c83 13726 fprintf (stream, _("\
c0f3af97
L
13727 -msse2avx encode SSE instructions with VEX prefix\n"));
13728 fprintf (stream, _("\
7c5c05ef 13729 -msse-check=[none|error|warning] (default: warning)\n\
daf50ae7
L
13730 check SSE instructions\n"));
13731 fprintf (stream, _("\
7c5c05ef 13732 -moperand-check=[none|error|warning] (default: warning)\n\
7bab8ab5
JB
13733 check operand combinations for validity\n"));
13734 fprintf (stream, _("\
7c5c05ef
L
13735 -mavxscalar=[128|256] (default: 128)\n\
13736 encode scalar AVX instructions with specific vector\n\
539f890d
L
13737 length\n"));
13738 fprintf (stream, _("\
03751133
L
13739 -mvexwig=[0|1] (default: 0)\n\
13740 encode VEX instructions with specific VEX.W value\n\
13741 for VEX.W bit ignored instructions\n"));
13742 fprintf (stream, _("\
7c5c05ef
L
13743 -mevexlig=[128|256|512] (default: 128)\n\
13744 encode scalar EVEX instructions with specific vector\n\
43234a1e
L
13745 length\n"));
13746 fprintf (stream, _("\
7c5c05ef
L
13747 -mevexwig=[0|1] (default: 0)\n\
13748 encode EVEX instructions with specific EVEX.W value\n\
43234a1e
L
13749 for EVEX.W bit ignored instructions\n"));
13750 fprintf (stream, _("\
7c5c05ef 13751 -mevexrcig=[rne|rd|ru|rz] (default: rne)\n\
d3d3c6db
IT
13752 encode EVEX instructions with specific EVEX.RC value\n\
13753 for SAE-only ignored instructions\n"));
13754 fprintf (stream, _("\
7c5c05ef
L
13755 -mmnemonic=[att|intel] "));
13756 if (SYSV386_COMPAT)
13757 fprintf (stream, _("(default: att)\n"));
13758 else
13759 fprintf (stream, _("(default: intel)\n"));
13760 fprintf (stream, _("\
13761 use AT&T/Intel mnemonic\n"));
ba104c83 13762 fprintf (stream, _("\
7c5c05ef
L
13763 -msyntax=[att|intel] (default: att)\n\
13764 use AT&T/Intel syntax\n"));
ba104c83
L
13765 fprintf (stream, _("\
13766 -mindex-reg support pseudo index registers\n"));
13767 fprintf (stream, _("\
13768 -mnaked-reg don't require `%%' prefix for registers\n"));
13769 fprintf (stream, _("\
7e8b059b 13770 -madd-bnd-prefix add BND prefix for all valid branches\n"));
b4a3a7b4 13771#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8dcea932
L
13772 fprintf (stream, _("\
13773 -mshared disable branch optimization for shared code\n"));
b4a3a7b4
L
13774 fprintf (stream, _("\
13775 -mx86-used-note=[no|yes] "));
13776 if (DEFAULT_X86_USED_NOTE)
13777 fprintf (stream, _("(default: yes)\n"));
13778 else
13779 fprintf (stream, _("(default: no)\n"));
13780 fprintf (stream, _("\
13781 generate x86 used ISA and feature properties\n"));
13782#endif
13783#if defined (TE_PE) || defined (TE_PEP)
167ad85b
TG
13784 fprintf (stream, _("\
13785 -mbig-obj generate big object files\n"));
13786#endif
d022bddd 13787 fprintf (stream, _("\
7c5c05ef 13788 -momit-lock-prefix=[no|yes] (default: no)\n\
d022bddd 13789 strip all lock prefixes\n"));
5db04b09 13790 fprintf (stream, _("\
7c5c05ef 13791 -mfence-as-lock-add=[no|yes] (default: no)\n\
e4e00185
AS
13792 encode lfence, mfence and sfence as\n\
13793 lock addl $0x0, (%%{re}sp)\n"));
13794 fprintf (stream, _("\
7c5c05ef
L
13795 -mrelax-relocations=[no|yes] "));
13796 if (DEFAULT_GENERATE_X86_RELAX_RELOCATIONS)
13797 fprintf (stream, _("(default: yes)\n"));
13798 else
13799 fprintf (stream, _("(default: no)\n"));
13800 fprintf (stream, _("\
0cb4071e
L
13801 generate relax relocations\n"));
13802 fprintf (stream, _("\
e379e5f3
L
13803 -malign-branch-boundary=NUM (default: 0)\n\
13804 align branches within NUM byte boundary\n"));
13805 fprintf (stream, _("\
13806 -malign-branch=TYPE[+TYPE...] (default: jcc+fused+jmp)\n\
13807 TYPE is combination of jcc, fused, jmp, call, ret,\n\
13808 indirect\n\
13809 specify types of branches to align\n"));
13810 fprintf (stream, _("\
13811 -malign-branch-prefix-size=NUM (default: 5)\n\
13812 align branches with NUM prefixes per instruction\n"));
13813 fprintf (stream, _("\
76cf450b
L
13814 -mbranches-within-32B-boundaries\n\
13815 align branches within 32 byte boundary\n"));
13816 fprintf (stream, _("\
ae531041
L
13817 -mlfence-after-load=[no|yes] (default: no)\n\
13818 generate lfence after load\n"));
13819 fprintf (stream, _("\
13820 -mlfence-before-indirect-branch=[none|all|register|memory] (default: none)\n\
13821 generate lfence before indirect near branch\n"));
13822 fprintf (stream, _("\
a09f656b 13823 -mlfence-before-ret=[none|or|not|shl|yes] (default: none)\n\
ae531041
L
13824 generate lfence before ret\n"));
13825 fprintf (stream, _("\
7c5c05ef 13826 -mamd64 accept only AMD64 ISA [default]\n"));
5db04b09
L
13827 fprintf (stream, _("\
13828 -mintel64 accept only Intel64 ISA\n"));
252b5132
RH
13829}
13830
3e73aa7c 13831#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
321098a5 13832 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
e57f8c65 13833 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
252b5132
RH
13834
13835/* Pick the target format to use. */
13836
47926f60 13837const char *
e3bb37b5 13838i386_target_format (void)
252b5132 13839{
d34049e8 13840 if (startswith (default_arch, "x86_64"))
351f65ca
L
13841 {
13842 update_code_flag (CODE_64BIT, 1);
13843 if (default_arch[6] == '\0')
7f56bc95 13844 x86_elf_abi = X86_64_ABI;
351f65ca 13845 else
7f56bc95 13846 x86_elf_abi = X86_64_X32_ABI;
351f65ca 13847 }
3e73aa7c 13848 else if (!strcmp (default_arch, "i386"))
78f12dd3 13849 update_code_flag (CODE_32BIT, 1);
5197d474
L
13850 else if (!strcmp (default_arch, "iamcu"))
13851 {
13852 update_code_flag (CODE_32BIT, 1);
13853 if (cpu_arch_isa == PROCESSOR_UNKNOWN)
13854 {
13855 static const i386_cpu_flags iamcu_flags = CPU_IAMCU_FLAGS;
13856 cpu_arch_name = "iamcu";
13857 cpu_sub_arch_name = NULL;
13858 cpu_arch_flags = iamcu_flags;
13859 cpu_arch_isa = PROCESSOR_IAMCU;
13860 cpu_arch_isa_flags = iamcu_flags;
13861 if (!cpu_arch_tune_set)
13862 {
13863 cpu_arch_tune = cpu_arch_isa;
13864 cpu_arch_tune_flags = cpu_arch_isa_flags;
13865 }
13866 }
8d471ec1 13867 else if (cpu_arch_isa != PROCESSOR_IAMCU)
5197d474
L
13868 as_fatal (_("Intel MCU doesn't support `%s' architecture"),
13869 cpu_arch_name);
13870 }
3e73aa7c 13871 else
2b5d6a91 13872 as_fatal (_("unknown architecture"));
89507696
JB
13873
13874 if (cpu_flags_all_zero (&cpu_arch_isa_flags))
13875 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].flags;
13876 if (cpu_flags_all_zero (&cpu_arch_tune_flags))
13877 cpu_arch_tune_flags = cpu_arch[flag_code == CODE_64BIT].flags;
13878
252b5132
RH
13879 switch (OUTPUT_FLAVOR)
13880 {
9384f2ff 13881#if defined (OBJ_MAYBE_AOUT) || defined (OBJ_AOUT)
4c63da97 13882 case bfd_target_aout_flavour:
47926f60 13883 return AOUT_TARGET_FORMAT;
4c63da97 13884#endif
9384f2ff
AM
13885#if defined (OBJ_MAYBE_COFF) || defined (OBJ_COFF)
13886# if defined (TE_PE) || defined (TE_PEP)
13887 case bfd_target_coff_flavour:
167ad85b 13888 if (flag_code == CODE_64BIT)
eb19308f
JB
13889 {
13890 object_64bit = 1;
13891 return use_big_obj ? "pe-bigobj-x86-64" : "pe-x86-64";
13892 }
13893 return use_big_obj ? "pe-bigobj-i386" : "pe-i386";
9384f2ff 13894# elif defined (TE_GO32)
0561d57c
JK
13895 case bfd_target_coff_flavour:
13896 return "coff-go32";
9384f2ff 13897# else
252b5132
RH
13898 case bfd_target_coff_flavour:
13899 return "coff-i386";
9384f2ff 13900# endif
4c63da97 13901#endif
3e73aa7c 13902#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
252b5132 13903 case bfd_target_elf_flavour:
3e73aa7c 13904 {
351f65ca
L
13905 const char *format;
13906
13907 switch (x86_elf_abi)
4fa24527 13908 {
351f65ca
L
13909 default:
13910 format = ELF_TARGET_FORMAT;
e379e5f3
L
13911#ifndef TE_SOLARIS
13912 tls_get_addr = "___tls_get_addr";
13913#endif
351f65ca 13914 break;
7f56bc95 13915 case X86_64_ABI:
351f65ca 13916 use_rela_relocations = 1;
4fa24527 13917 object_64bit = 1;
e379e5f3
L
13918#ifndef TE_SOLARIS
13919 tls_get_addr = "__tls_get_addr";
13920#endif
351f65ca
L
13921 format = ELF_TARGET_FORMAT64;
13922 break;
7f56bc95 13923 case X86_64_X32_ABI:
4fa24527 13924 use_rela_relocations = 1;
351f65ca 13925 object_64bit = 1;
e379e5f3
L
13926#ifndef TE_SOLARIS
13927 tls_get_addr = "__tls_get_addr";
13928#endif
862be3fb 13929 disallow_64bit_reloc = 1;
351f65ca
L
13930 format = ELF_TARGET_FORMAT32;
13931 break;
4fa24527 13932 }
3632d14b 13933 if (cpu_arch_isa == PROCESSOR_L1OM)
8a9036a4 13934 {
7f56bc95 13935 if (x86_elf_abi != X86_64_ABI)
8a9036a4
L
13936 as_fatal (_("Intel L1OM is 64bit only"));
13937 return ELF_TARGET_L1OM_FORMAT;
13938 }
b49f93f6 13939 else if (cpu_arch_isa == PROCESSOR_K1OM)
7a9068fe
L
13940 {
13941 if (x86_elf_abi != X86_64_ABI)
13942 as_fatal (_("Intel K1OM is 64bit only"));
13943 return ELF_TARGET_K1OM_FORMAT;
13944 }
81486035
L
13945 else if (cpu_arch_isa == PROCESSOR_IAMCU)
13946 {
13947 if (x86_elf_abi != I386_ABI)
13948 as_fatal (_("Intel MCU is 32bit only"));
13949 return ELF_TARGET_IAMCU_FORMAT;
13950 }
8a9036a4 13951 else
351f65ca 13952 return format;
3e73aa7c 13953 }
e57f8c65
TG
13954#endif
13955#if defined (OBJ_MACH_O)
13956 case bfd_target_mach_o_flavour:
d382c579
TG
13957 if (flag_code == CODE_64BIT)
13958 {
13959 use_rela_relocations = 1;
13960 object_64bit = 1;
13961 return "mach-o-x86-64";
13962 }
13963 else
13964 return "mach-o-i386";
4c63da97 13965#endif
252b5132
RH
13966 default:
13967 abort ();
13968 return NULL;
13969 }
13970}
13971
47926f60 13972#endif /* OBJ_MAYBE_ more than one */
252b5132 13973\f
252b5132 13974symbolS *
7016a5d5 13975md_undefined_symbol (char *name)
252b5132 13976{
18dc2407
ILT
13977 if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
13978 && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
13979 && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
13980 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
24eab124
AM
13981 {
13982 if (!GOT_symbol)
13983 {
13984 if (symbol_find (name))
13985 as_bad (_("GOT already in symbol table"));
13986 GOT_symbol = symbol_new (name, undefined_section,
e01e1cee 13987 &zero_address_frag, 0);
24eab124
AM
13988 };
13989 return GOT_symbol;
13990 }
252b5132
RH
13991 return 0;
13992}
13993
13994/* Round up a section size to the appropriate boundary. */
47926f60 13995
252b5132 13996valueT
7016a5d5 13997md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
252b5132 13998{
4c63da97
AM
13999#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
14000 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
14001 {
14002 /* For a.out, force the section size to be aligned. If we don't do
14003 this, BFD will align it for us, but it will not write out the
14004 final bytes of the section. This may be a bug in BFD, but it is
14005 easier to fix it here since that is how the other a.out targets
14006 work. */
14007 int align;
14008
fd361982 14009 align = bfd_section_alignment (segment);
8d3842cd 14010 size = ((size + (1 << align) - 1) & (-((valueT) 1 << align)));
4c63da97 14011 }
252b5132
RH
14012#endif
14013
14014 return size;
14015}
14016
14017/* On the i386, PC-relative offsets are relative to the start of the
14018 next instruction. That is, the address of the offset, plus its
14019 size, since the offset is always the last part of the insn. */
14020
14021long
e3bb37b5 14022md_pcrel_from (fixS *fixP)
252b5132
RH
14023{
14024 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
14025}
14026
14027#ifndef I386COFF
14028
14029static void
e3bb37b5 14030s_bss (int ignore ATTRIBUTE_UNUSED)
252b5132 14031{
29b0f896 14032 int temp;
252b5132 14033
8a75718c
JB
14034#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14035 if (IS_ELF)
14036 obj_elf_section_change_hook ();
14037#endif
252b5132
RH
14038 temp = get_absolute_expression ();
14039 subseg_set (bss_section, (subsegT) temp);
14040 demand_empty_rest_of_line ();
14041}
14042
14043#endif
14044
e379e5f3
L
14045/* Remember constant directive. */
14046
14047void
14048i386_cons_align (int ignore ATTRIBUTE_UNUSED)
14049{
14050 if (last_insn.kind != last_insn_directive
14051 && (bfd_section_flags (now_seg) & SEC_CODE))
14052 {
14053 last_insn.seg = now_seg;
14054 last_insn.kind = last_insn_directive;
14055 last_insn.name = "constant directive";
14056 last_insn.file = as_where (&last_insn.line);
ae531041
L
14057 if (lfence_before_ret != lfence_before_ret_none)
14058 {
14059 if (lfence_before_indirect_branch != lfence_branch_none)
14060 as_warn (_("constant directive skips -mlfence-before-ret "
14061 "and -mlfence-before-indirect-branch"));
14062 else
14063 as_warn (_("constant directive skips -mlfence-before-ret"));
14064 }
14065 else if (lfence_before_indirect_branch != lfence_branch_none)
14066 as_warn (_("constant directive skips -mlfence-before-indirect-branch"));
e379e5f3
L
14067 }
14068}
14069
3abbafc2 14070int
e3bb37b5 14071i386_validate_fix (fixS *fixp)
252b5132 14072{
3abbafc2
JB
14073#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14074 if (fixp->fx_r_type == BFD_RELOC_SIZE32
14075 || fixp->fx_r_type == BFD_RELOC_SIZE64)
14076 return IS_ELF && fixp->fx_addsy
14077 && (!S_IS_DEFINED (fixp->fx_addsy)
14078 || S_IS_EXTERNAL (fixp->fx_addsy));
14079#endif
14080
02a86693 14081 if (fixp->fx_subsy)
252b5132 14082 {
02a86693 14083 if (fixp->fx_subsy == GOT_symbol)
23df1078 14084 {
02a86693
L
14085 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
14086 {
14087 if (!object_64bit)
14088 abort ();
14089#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14090 if (fixp->fx_tcbit2)
56ceb5b5
L
14091 fixp->fx_r_type = (fixp->fx_tcbit
14092 ? BFD_RELOC_X86_64_REX_GOTPCRELX
14093 : BFD_RELOC_X86_64_GOTPCRELX);
02a86693
L
14094 else
14095#endif
14096 fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
14097 }
d6ab8113 14098 else
02a86693
L
14099 {
14100 if (!object_64bit)
14101 fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
14102 else
14103 fixp->fx_r_type = BFD_RELOC_X86_64_GOTOFF64;
14104 }
14105 fixp->fx_subsy = 0;
23df1078 14106 }
252b5132 14107 }
02a86693 14108#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2585b7a5 14109 else
02a86693 14110 {
2585b7a5
L
14111 /* NB: Commit 292676c1 resolved PLT32 reloc aganst local symbol
14112 to section. Since PLT32 relocation must be against symbols,
14113 turn such PLT32 relocation into PC32 relocation. */
14114 if (fixp->fx_addsy
14115 && (fixp->fx_r_type == BFD_RELOC_386_PLT32
14116 || fixp->fx_r_type == BFD_RELOC_X86_64_PLT32)
14117 && symbol_section_p (fixp->fx_addsy))
14118 fixp->fx_r_type = BFD_RELOC_32_PCREL;
14119 if (!object_64bit)
14120 {
14121 if (fixp->fx_r_type == BFD_RELOC_386_GOT32
14122 && fixp->fx_tcbit2)
14123 fixp->fx_r_type = BFD_RELOC_386_GOT32X;
14124 }
02a86693
L
14125 }
14126#endif
3abbafc2
JB
14127
14128 return 1;
252b5132
RH
14129}
14130
252b5132 14131arelent *
7016a5d5 14132tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
14133{
14134 arelent *rel;
14135 bfd_reloc_code_real_type code;
14136
14137 switch (fixp->fx_r_type)
14138 {
8ce3d284 14139#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3abbafc2
JB
14140 symbolS *sym;
14141
8fd4256d
L
14142 case BFD_RELOC_SIZE32:
14143 case BFD_RELOC_SIZE64:
3abbafc2
JB
14144 if (fixp->fx_addsy
14145 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))
14146 && (!fixp->fx_subsy
14147 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))))
14148 sym = fixp->fx_addsy;
14149 else if (fixp->fx_subsy
14150 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))
14151 && (!fixp->fx_addsy
14152 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))))
14153 sym = fixp->fx_subsy;
14154 else
14155 sym = NULL;
14156 if (IS_ELF && sym && S_IS_DEFINED (sym) && !S_IS_EXTERNAL (sym))
8fd4256d
L
14157 {
14158 /* Resolve size relocation against local symbol to size of
14159 the symbol plus addend. */
3abbafc2 14160 valueT value = S_GET_SIZE (sym);
44f87162 14161
3abbafc2
JB
14162 if (symbol_get_bfdsym (sym)->flags & BSF_SECTION_SYM)
14163 value = bfd_section_size (S_GET_SEGMENT (sym));
14164 if (sym == fixp->fx_subsy)
14165 {
14166 value = -value;
14167 if (fixp->fx_addsy)
14168 value += S_GET_VALUE (fixp->fx_addsy);
14169 }
14170 else if (fixp->fx_subsy)
14171 value -= S_GET_VALUE (fixp->fx_subsy);
44f87162 14172 value += fixp->fx_offset;
8fd4256d 14173 if (fixp->fx_r_type == BFD_RELOC_SIZE32
d965814f 14174 && object_64bit
8fd4256d
L
14175 && !fits_in_unsigned_long (value))
14176 as_bad_where (fixp->fx_file, fixp->fx_line,
14177 _("symbol size computation overflow"));
14178 fixp->fx_addsy = NULL;
14179 fixp->fx_subsy = NULL;
14180 md_apply_fix (fixp, (valueT *) &value, NULL);
14181 return NULL;
14182 }
3abbafc2
JB
14183 if (!fixp->fx_addsy || fixp->fx_subsy)
14184 {
14185 as_bad_where (fixp->fx_file, fixp->fx_line,
14186 "unsupported expression involving @size");
14187 return NULL;
14188 }
8ce3d284 14189#endif
1a0670f3 14190 /* Fall through. */
8fd4256d 14191
3e73aa7c
JH
14192 case BFD_RELOC_X86_64_PLT32:
14193 case BFD_RELOC_X86_64_GOT32:
14194 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
14195 case BFD_RELOC_X86_64_GOTPCRELX:
14196 case BFD_RELOC_X86_64_REX_GOTPCRELX:
252b5132
RH
14197 case BFD_RELOC_386_PLT32:
14198 case BFD_RELOC_386_GOT32:
02a86693 14199 case BFD_RELOC_386_GOT32X:
252b5132
RH
14200 case BFD_RELOC_386_GOTOFF:
14201 case BFD_RELOC_386_GOTPC:
13ae64f3
JJ
14202 case BFD_RELOC_386_TLS_GD:
14203 case BFD_RELOC_386_TLS_LDM:
14204 case BFD_RELOC_386_TLS_LDO_32:
14205 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
14206 case BFD_RELOC_386_TLS_IE:
14207 case BFD_RELOC_386_TLS_GOTIE:
13ae64f3
JJ
14208 case BFD_RELOC_386_TLS_LE_32:
14209 case BFD_RELOC_386_TLS_LE:
67a4f2b7
AO
14210 case BFD_RELOC_386_TLS_GOTDESC:
14211 case BFD_RELOC_386_TLS_DESC_CALL:
bffbf940
JJ
14212 case BFD_RELOC_X86_64_TLSGD:
14213 case BFD_RELOC_X86_64_TLSLD:
14214 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 14215 case BFD_RELOC_X86_64_DTPOFF64:
bffbf940
JJ
14216 case BFD_RELOC_X86_64_GOTTPOFF:
14217 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113
JB
14218 case BFD_RELOC_X86_64_TPOFF64:
14219 case BFD_RELOC_X86_64_GOTOFF64:
14220 case BFD_RELOC_X86_64_GOTPC32:
7b81dfbb
AJ
14221 case BFD_RELOC_X86_64_GOT64:
14222 case BFD_RELOC_X86_64_GOTPCREL64:
14223 case BFD_RELOC_X86_64_GOTPC64:
14224 case BFD_RELOC_X86_64_GOTPLT64:
14225 case BFD_RELOC_X86_64_PLTOFF64:
67a4f2b7
AO
14226 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14227 case BFD_RELOC_X86_64_TLSDESC_CALL:
252b5132
RH
14228 case BFD_RELOC_RVA:
14229 case BFD_RELOC_VTABLE_ENTRY:
14230 case BFD_RELOC_VTABLE_INHERIT:
6482c264
NC
14231#ifdef TE_PE
14232 case BFD_RELOC_32_SECREL:
14233#endif
252b5132
RH
14234 code = fixp->fx_r_type;
14235 break;
dbbaec26
L
14236 case BFD_RELOC_X86_64_32S:
14237 if (!fixp->fx_pcrel)
14238 {
14239 /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32. */
14240 code = fixp->fx_r_type;
14241 break;
14242 }
1a0670f3 14243 /* Fall through. */
252b5132 14244 default:
93382f6d 14245 if (fixp->fx_pcrel)
252b5132 14246 {
93382f6d
AM
14247 switch (fixp->fx_size)
14248 {
14249 default:
b091f402
AM
14250 as_bad_where (fixp->fx_file, fixp->fx_line,
14251 _("can not do %d byte pc-relative relocation"),
14252 fixp->fx_size);
93382f6d
AM
14253 code = BFD_RELOC_32_PCREL;
14254 break;
14255 case 1: code = BFD_RELOC_8_PCREL; break;
14256 case 2: code = BFD_RELOC_16_PCREL; break;
d258b828 14257 case 4: code = BFD_RELOC_32_PCREL; break;
d6ab8113
JB
14258#ifdef BFD64
14259 case 8: code = BFD_RELOC_64_PCREL; break;
14260#endif
93382f6d
AM
14261 }
14262 }
14263 else
14264 {
14265 switch (fixp->fx_size)
14266 {
14267 default:
b091f402
AM
14268 as_bad_where (fixp->fx_file, fixp->fx_line,
14269 _("can not do %d byte relocation"),
14270 fixp->fx_size);
93382f6d
AM
14271 code = BFD_RELOC_32;
14272 break;
14273 case 1: code = BFD_RELOC_8; break;
14274 case 2: code = BFD_RELOC_16; break;
14275 case 4: code = BFD_RELOC_32; break;
937149dd 14276#ifdef BFD64
3e73aa7c 14277 case 8: code = BFD_RELOC_64; break;
937149dd 14278#endif
93382f6d 14279 }
252b5132
RH
14280 }
14281 break;
14282 }
252b5132 14283
d182319b
JB
14284 if ((code == BFD_RELOC_32
14285 || code == BFD_RELOC_32_PCREL
14286 || code == BFD_RELOC_X86_64_32S)
252b5132
RH
14287 && GOT_symbol
14288 && fixp->fx_addsy == GOT_symbol)
3e73aa7c 14289 {
4fa24527 14290 if (!object_64bit)
d6ab8113
JB
14291 code = BFD_RELOC_386_GOTPC;
14292 else
14293 code = BFD_RELOC_X86_64_GOTPC32;
3e73aa7c 14294 }
7b81dfbb
AJ
14295 if ((code == BFD_RELOC_64 || code == BFD_RELOC_64_PCREL)
14296 && GOT_symbol
14297 && fixp->fx_addsy == GOT_symbol)
14298 {
14299 code = BFD_RELOC_X86_64_GOTPC64;
14300 }
252b5132 14301
add39d23
TS
14302 rel = XNEW (arelent);
14303 rel->sym_ptr_ptr = XNEW (asymbol *);
49309057 14304 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
14305
14306 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
c87db184 14307
3e73aa7c
JH
14308 if (!use_rela_relocations)
14309 {
14310 /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
14311 vtable entry to be used in the relocation's section offset. */
14312 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
14313 rel->address = fixp->fx_offset;
fbeb56a4
DK
14314#if defined (OBJ_COFF) && defined (TE_PE)
14315 else if (fixp->fx_addsy && S_IS_WEAK (fixp->fx_addsy))
14316 rel->addend = fixp->fx_addnumber - (S_GET_VALUE (fixp->fx_addsy) * 2);
14317 else
14318#endif
c6682705 14319 rel->addend = 0;
3e73aa7c
JH
14320 }
14321 /* Use the rela in 64bit mode. */
252b5132 14322 else
3e73aa7c 14323 {
862be3fb
L
14324 if (disallow_64bit_reloc)
14325 switch (code)
14326 {
862be3fb
L
14327 case BFD_RELOC_X86_64_DTPOFF64:
14328 case BFD_RELOC_X86_64_TPOFF64:
14329 case BFD_RELOC_64_PCREL:
14330 case BFD_RELOC_X86_64_GOTOFF64:
14331 case BFD_RELOC_X86_64_GOT64:
14332 case BFD_RELOC_X86_64_GOTPCREL64:
14333 case BFD_RELOC_X86_64_GOTPC64:
14334 case BFD_RELOC_X86_64_GOTPLT64:
14335 case BFD_RELOC_X86_64_PLTOFF64:
14336 as_bad_where (fixp->fx_file, fixp->fx_line,
14337 _("cannot represent relocation type %s in x32 mode"),
14338 bfd_get_reloc_code_name (code));
14339 break;
14340 default:
14341 break;
14342 }
14343
062cd5e7
AS
14344 if (!fixp->fx_pcrel)
14345 rel->addend = fixp->fx_offset;
14346 else
14347 switch (code)
14348 {
14349 case BFD_RELOC_X86_64_PLT32:
14350 case BFD_RELOC_X86_64_GOT32:
14351 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
14352 case BFD_RELOC_X86_64_GOTPCRELX:
14353 case BFD_RELOC_X86_64_REX_GOTPCRELX:
bffbf940
JJ
14354 case BFD_RELOC_X86_64_TLSGD:
14355 case BFD_RELOC_X86_64_TLSLD:
14356 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7
AO
14357 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14358 case BFD_RELOC_X86_64_TLSDESC_CALL:
062cd5e7
AS
14359 rel->addend = fixp->fx_offset - fixp->fx_size;
14360 break;
14361 default:
14362 rel->addend = (section->vma
14363 - fixp->fx_size
14364 + fixp->fx_addnumber
14365 + md_pcrel_from (fixp));
14366 break;
14367 }
3e73aa7c
JH
14368 }
14369
252b5132
RH
14370 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
14371 if (rel->howto == NULL)
14372 {
14373 as_bad_where (fixp->fx_file, fixp->fx_line,
d0b47220 14374 _("cannot represent relocation type %s"),
252b5132
RH
14375 bfd_get_reloc_code_name (code));
14376 /* Set howto to a garbage value so that we can keep going. */
14377 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
9c2799c2 14378 gas_assert (rel->howto != NULL);
252b5132
RH
14379 }
14380
14381 return rel;
14382}
14383
ee86248c 14384#include "tc-i386-intel.c"
54cfded0 14385
a60de03c
JB
14386void
14387tc_x86_parse_to_dw2regnum (expressionS *exp)
54cfded0 14388{
a60de03c
JB
14389 int saved_naked_reg;
14390 char saved_register_dot;
54cfded0 14391
a60de03c
JB
14392 saved_naked_reg = allow_naked_reg;
14393 allow_naked_reg = 1;
14394 saved_register_dot = register_chars['.'];
14395 register_chars['.'] = '.';
14396 allow_pseudo_reg = 1;
14397 expression_and_evaluate (exp);
14398 allow_pseudo_reg = 0;
14399 register_chars['.'] = saved_register_dot;
14400 allow_naked_reg = saved_naked_reg;
14401
e96d56a1 14402 if (exp->X_op == O_register && exp->X_add_number >= 0)
54cfded0 14403 {
a60de03c
JB
14404 if ((addressT) exp->X_add_number < i386_regtab_size)
14405 {
14406 exp->X_op = O_constant;
14407 exp->X_add_number = i386_regtab[exp->X_add_number]
14408 .dw2_regnum[flag_code >> 1];
14409 }
14410 else
14411 exp->X_op = O_illegal;
54cfded0 14412 }
54cfded0
AM
14413}
14414
14415void
14416tc_x86_frame_initial_instructions (void)
14417{
a60de03c
JB
14418 static unsigned int sp_regno[2];
14419
14420 if (!sp_regno[flag_code >> 1])
14421 {
14422 char *saved_input = input_line_pointer;
14423 char sp[][4] = {"esp", "rsp"};
14424 expressionS exp;
a4447b93 14425
a60de03c
JB
14426 input_line_pointer = sp[flag_code >> 1];
14427 tc_x86_parse_to_dw2regnum (&exp);
9c2799c2 14428 gas_assert (exp.X_op == O_constant);
a60de03c
JB
14429 sp_regno[flag_code >> 1] = exp.X_add_number;
14430 input_line_pointer = saved_input;
14431 }
a4447b93 14432
61ff971f
L
14433 cfi_add_CFA_def_cfa (sp_regno[flag_code >> 1], -x86_cie_data_alignment);
14434 cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
54cfded0 14435}
d2b2c203 14436
d7921315
L
14437int
14438x86_dwarf2_addr_size (void)
14439{
14440#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
14441 if (x86_elf_abi == X86_64_X32_ABI)
14442 return 4;
14443#endif
14444 return bfd_arch_bits_per_address (stdoutput) / 8;
14445}
14446
d2b2c203
DJ
14447int
14448i386_elf_section_type (const char *str, size_t len)
14449{
14450 if (flag_code == CODE_64BIT
14451 && len == sizeof ("unwind") - 1
d34049e8 14452 && startswith (str, "unwind"))
d2b2c203
DJ
14453 return SHT_X86_64_UNWIND;
14454
14455 return -1;
14456}
bb41ade5 14457
ad5fec3b
EB
14458#ifdef TE_SOLARIS
14459void
14460i386_solaris_fix_up_eh_frame (segT sec)
14461{
14462 if (flag_code == CODE_64BIT)
14463 elf_section_type (sec) = SHT_X86_64_UNWIND;
14464}
14465#endif
14466
bb41ade5
AM
14467#ifdef TE_PE
14468void
14469tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
14470{
91d6fa6a 14471 expressionS exp;
bb41ade5 14472
91d6fa6a
NC
14473 exp.X_op = O_secrel;
14474 exp.X_add_symbol = symbol;
14475 exp.X_add_number = 0;
14476 emit_expr (&exp, size);
bb41ade5
AM
14477}
14478#endif
3b22753a
L
14479
14480#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14481/* For ELF on x86-64, add support for SHF_X86_64_LARGE. */
14482
01e1a5bc 14483bfd_vma
6d4af3c2 14484x86_64_section_letter (int letter, const char **ptr_msg)
3b22753a
L
14485{
14486 if (flag_code == CODE_64BIT)
14487 {
14488 if (letter == 'l')
14489 return SHF_X86_64_LARGE;
14490
8f3bae45 14491 *ptr_msg = _("bad .section directive: want a,l,w,x,M,S,G,T in string");
64e74474 14492 }
3b22753a 14493 else
8f3bae45 14494 *ptr_msg = _("bad .section directive: want a,w,x,M,S,G,T in string");
3b22753a
L
14495 return -1;
14496}
14497
01e1a5bc 14498bfd_vma
3b22753a
L
14499x86_64_section_word (char *str, size_t len)
14500{
08dedd66 14501 if (len == 5 && flag_code == CODE_64BIT && startswith (str, "large"))
3b22753a
L
14502 return SHF_X86_64_LARGE;
14503
14504 return -1;
14505}
14506
14507static void
14508handle_large_common (int small ATTRIBUTE_UNUSED)
14509{
14510 if (flag_code != CODE_64BIT)
14511 {
14512 s_comm_internal (0, elf_common_parse);
14513 as_warn (_(".largecomm supported only in 64bit mode, producing .comm"));
14514 }
14515 else
14516 {
14517 static segT lbss_section;
14518 asection *saved_com_section_ptr = elf_com_section_ptr;
14519 asection *saved_bss_section = bss_section;
14520
14521 if (lbss_section == NULL)
14522 {
14523 flagword applicable;
14524 segT seg = now_seg;
14525 subsegT subseg = now_subseg;
14526
14527 /* The .lbss section is for local .largecomm symbols. */
14528 lbss_section = subseg_new (".lbss", 0);
14529 applicable = bfd_applicable_section_flags (stdoutput);
fd361982 14530 bfd_set_section_flags (lbss_section, applicable & SEC_ALLOC);
3b22753a
L
14531 seg_info (lbss_section)->bss = 1;
14532
14533 subseg_set (seg, subseg);
14534 }
14535
14536 elf_com_section_ptr = &_bfd_elf_large_com_section;
14537 bss_section = lbss_section;
14538
14539 s_comm_internal (0, elf_common_parse);
14540
14541 elf_com_section_ptr = saved_com_section_ptr;
14542 bss_section = saved_bss_section;
14543 }
14544}
14545#endif /* OBJ_ELF || OBJ_MAYBE_ELF */
This page took 5.765819 seconds and 4 git commands to generate.