Enable support to Intel Keylocker instructions
[deliverable/binutils-gdb.git] / gas / config / tc-i386.c
CommitLineData
b534c6d3 1/* tc-i386.c -- Assemble code for the Intel 80386
b3adc24a 2 Copyright (C) 1989-2020 Free Software Foundation, Inc.
252b5132
RH
3
4 This file is part of GAS, the GNU Assembler.
5
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
ec2655a6 8 the Free Software Foundation; either version 3, or (at your option)
252b5132
RH
9 any later version.
10
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to the Free
4b4da160
NC
18 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
19 02110-1301, USA. */
252b5132 20
47926f60
KH
21/* Intel 80386 machine specific gas.
22 Written by Eliot Dresselhaus (eliot@mgm.mit.edu).
3e73aa7c 23 x86_64 support by Jan Hubicka (jh@suse.cz)
0f10071e 24 VIA PadLock support by Michal Ludvig (mludvig@suse.cz)
47926f60
KH
25 Bugs & suggestions are completely welcome. This is free software.
26 Please help us make it better. */
252b5132 27
252b5132 28#include "as.h"
3882b010 29#include "safe-ctype.h"
252b5132 30#include "subsegs.h"
316e2c05 31#include "dwarf2dbg.h"
54cfded0 32#include "dw2gencfi.h"
d2b2c203 33#include "elf/x86-64.h"
40fb9820 34#include "opcodes/i386-init.h"
252b5132 35
41fd2579
L
36#ifdef HAVE_LIMITS_H
37#include <limits.h>
38#else
39#ifdef HAVE_SYS_PARAM_H
40#include <sys/param.h>
41#endif
42#ifndef INT_MAX
43#define INT_MAX (int) (((unsigned) (-1)) >> 1)
44#endif
45#endif
46
c3332e24 47#ifndef INFER_ADDR_PREFIX
eecb386c 48#define INFER_ADDR_PREFIX 1
c3332e24
AM
49#endif
50
29b0f896
AM
51#ifndef DEFAULT_ARCH
52#define DEFAULT_ARCH "i386"
246fcdee 53#endif
252b5132 54
edde18a5
AM
55#ifndef INLINE
56#if __GNUC__ >= 2
57#define INLINE __inline__
58#else
59#define INLINE
60#endif
61#endif
62
6305a203
L
63/* Prefixes will be emitted in the order defined below.
64 WAIT_PREFIX must be the first prefix since FWAIT is really is an
65 instruction, and so must come before any prefixes.
66 The preferred prefix order is SEG_PREFIX, ADDR_PREFIX, DATA_PREFIX,
42164a71 67 REP_PREFIX/HLE_PREFIX, LOCK_PREFIX. */
6305a203
L
68#define WAIT_PREFIX 0
69#define SEG_PREFIX 1
70#define ADDR_PREFIX 2
71#define DATA_PREFIX 3
c32fa91d 72#define REP_PREFIX 4
42164a71 73#define HLE_PREFIX REP_PREFIX
7e8b059b 74#define BND_PREFIX REP_PREFIX
c32fa91d 75#define LOCK_PREFIX 5
4e9ac44a
L
76#define REX_PREFIX 6 /* must come last. */
77#define MAX_PREFIXES 7 /* max prefixes per opcode */
6305a203
L
78
79/* we define the syntax here (modulo base,index,scale syntax) */
80#define REGISTER_PREFIX '%'
81#define IMMEDIATE_PREFIX '$'
82#define ABSOLUTE_PREFIX '*'
83
84/* these are the instruction mnemonic suffixes in AT&T syntax or
85 memory operand size in Intel syntax. */
86#define WORD_MNEM_SUFFIX 'w'
87#define BYTE_MNEM_SUFFIX 'b'
88#define SHORT_MNEM_SUFFIX 's'
89#define LONG_MNEM_SUFFIX 'l'
90#define QWORD_MNEM_SUFFIX 'q'
6305a203
L
91/* Intel Syntax. Use a non-ascii letter since since it never appears
92 in instructions. */
93#define LONG_DOUBLE_MNEM_SUFFIX '\1'
94
95#define END_OF_INSN '\0'
96
79dec6b7
JB
97/* This matches the C -> StaticRounding alias in the opcode table. */
98#define commutative staticrounding
99
6305a203
L
100/*
101 'templates' is for grouping together 'template' structures for opcodes
102 of the same name. This is only used for storing the insns in the grand
103 ole hash table of insns.
104 The templates themselves start at START and range up to (but not including)
105 END.
106 */
107typedef struct
108{
d3ce72d0
NC
109 const insn_template *start;
110 const insn_template *end;
6305a203
L
111}
112templates;
113
114/* 386 operand encoding bytes: see 386 book for details of this. */
115typedef struct
116{
117 unsigned int regmem; /* codes register or memory operand */
118 unsigned int reg; /* codes register operand (or extended opcode) */
119 unsigned int mode; /* how to interpret regmem & reg */
120}
121modrm_byte;
122
123/* x86-64 extension prefix. */
124typedef int rex_byte;
125
6305a203
L
126/* 386 opcode byte to code indirect addressing. */
127typedef struct
128{
129 unsigned base;
130 unsigned index;
131 unsigned scale;
132}
133sib_byte;
134
6305a203
L
135/* x86 arch names, types and features */
136typedef struct
137{
138 const char *name; /* arch name */
8a2c8fef 139 unsigned int len; /* arch string length */
6305a203
L
140 enum processor_type type; /* arch type */
141 i386_cpu_flags flags; /* cpu feature flags */
8a2c8fef 142 unsigned int skip; /* show_arch should skip this. */
6305a203
L
143}
144arch_entry;
145
293f5f65
L
146/* Used to turn off indicated flags. */
147typedef struct
148{
149 const char *name; /* arch name */
150 unsigned int len; /* arch string length */
151 i386_cpu_flags flags; /* cpu feature flags */
152}
153noarch_entry;
154
78f12dd3 155static void update_code_flag (int, int);
e3bb37b5
L
156static void set_code_flag (int);
157static void set_16bit_gcc_code_flag (int);
158static void set_intel_syntax (int);
1efbbeb4 159static void set_intel_mnemonic (int);
db51cc60 160static void set_allow_index_reg (int);
7bab8ab5 161static void set_check (int);
e3bb37b5 162static void set_cpu_arch (int);
6482c264 163#ifdef TE_PE
e3bb37b5 164static void pe_directive_secrel (int);
6482c264 165#endif
e3bb37b5
L
166static void signed_cons (int);
167static char *output_invalid (int c);
ee86248c
JB
168static int i386_finalize_immediate (segT, expressionS *, i386_operand_type,
169 const char *);
170static int i386_finalize_displacement (segT, expressionS *, i386_operand_type,
171 const char *);
a7619375 172static int i386_att_operand (char *);
e3bb37b5 173static int i386_intel_operand (char *, int);
ee86248c
JB
174static int i386_intel_simplify (expressionS *);
175static int i386_intel_parse_name (const char *, expressionS *);
e3bb37b5
L
176static const reg_entry *parse_register (char *, char **);
177static char *parse_insn (char *, char *);
178static char *parse_operands (char *, const char *);
179static void swap_operands (void);
4d456e3d 180static void swap_2_operands (int, int);
48bcea9f 181static enum flag_code i386_addressing_mode (void);
e3bb37b5
L
182static void optimize_imm (void);
183static void optimize_disp (void);
83b16ac6 184static const insn_template *match_template (char);
e3bb37b5
L
185static int check_string (void);
186static int process_suffix (void);
187static int check_byte_reg (void);
188static int check_long_reg (void);
189static int check_qword_reg (void);
190static int check_word_reg (void);
191static int finalize_imm (void);
192static int process_operands (void);
193static const seg_entry *build_modrm_byte (void);
194static void output_insn (void);
195static void output_imm (fragS *, offsetT);
196static void output_disp (fragS *, offsetT);
29b0f896 197#ifndef I386COFF
e3bb37b5 198static void s_bss (int);
252b5132 199#endif
17d4e2a2
L
200#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
201static void handle_large_common (int small ATTRIBUTE_UNUSED);
b4a3a7b4
L
202
203/* GNU_PROPERTY_X86_ISA_1_USED. */
204static unsigned int x86_isa_1_used;
205/* GNU_PROPERTY_X86_FEATURE_2_USED. */
206static unsigned int x86_feature_2_used;
207/* Generate x86 used ISA and feature properties. */
208static unsigned int x86_used_note = DEFAULT_X86_USED_NOTE;
17d4e2a2 209#endif
252b5132 210
a847613f 211static const char *default_arch = DEFAULT_ARCH;
3e73aa7c 212
8a6fb3f9
JB
213/* parse_register() returns this when a register alias cannot be used. */
214static const reg_entry bad_reg = { "<bad>", OPERAND_TYPE_NONE, 0, 0,
215 { Dw2Inval, Dw2Inval } };
216
43234a1e
L
217/* This struct describes rounding control and SAE in the instruction. */
218struct RC_Operation
219{
220 enum rc_type
221 {
222 rne = 0,
223 rd,
224 ru,
225 rz,
226 saeonly
227 } type;
228 int operand;
229};
230
231static struct RC_Operation rc_op;
232
233/* The struct describes masking, applied to OPERAND in the instruction.
234 MASK is a pointer to the corresponding mask register. ZEROING tells
235 whether merging or zeroing mask is used. */
236struct Mask_Operation
237{
238 const reg_entry *mask;
239 unsigned int zeroing;
240 /* The operand where this operation is associated. */
241 int operand;
242};
243
244static struct Mask_Operation mask_op;
245
246/* The struct describes broadcasting, applied to OPERAND. FACTOR is
247 broadcast factor. */
248struct Broadcast_Operation
249{
8e6e0792 250 /* Type of broadcast: {1to2}, {1to4}, {1to8}, or {1to16}. */
43234a1e
L
251 int type;
252
253 /* Index of broadcasted operand. */
254 int operand;
4a1b91ea
L
255
256 /* Number of bytes to broadcast. */
257 int bytes;
43234a1e
L
258};
259
260static struct Broadcast_Operation broadcast_op;
261
c0f3af97
L
262/* VEX prefix. */
263typedef struct
264{
43234a1e
L
265 /* VEX prefix is either 2 byte or 3 byte. EVEX is 4 byte. */
266 unsigned char bytes[4];
c0f3af97
L
267 unsigned int length;
268 /* Destination or source register specifier. */
269 const reg_entry *register_specifier;
270} vex_prefix;
271
252b5132 272/* 'md_assemble ()' gathers together information and puts it into a
47926f60 273 i386_insn. */
252b5132 274
520dc8e8
AM
275union i386_op
276 {
277 expressionS *disps;
278 expressionS *imms;
279 const reg_entry *regs;
280 };
281
a65babc9
L
282enum i386_error
283 {
86e026a4 284 operand_size_mismatch,
a65babc9
L
285 operand_type_mismatch,
286 register_type_mismatch,
287 number_of_operands_mismatch,
288 invalid_instruction_suffix,
289 bad_imm4,
a65babc9
L
290 unsupported_with_intel_mnemonic,
291 unsupported_syntax,
6c30d220 292 unsupported,
260cd341 293 invalid_sib_address,
6c30d220 294 invalid_vsib_address,
7bab8ab5 295 invalid_vector_register_set,
260cd341 296 invalid_tmm_register_set,
43234a1e
L
297 unsupported_vector_index_register,
298 unsupported_broadcast,
43234a1e
L
299 broadcast_needed,
300 unsupported_masking,
301 mask_not_on_destination,
302 no_default_mask,
303 unsupported_rc_sae,
304 rc_sae_operand_not_last_imm,
305 invalid_register_operand,
a65babc9
L
306 };
307
252b5132
RH
308struct _i386_insn
309 {
47926f60 310 /* TM holds the template for the insn were currently assembling. */
d3ce72d0 311 insn_template tm;
252b5132 312
7d5e4556
L
313 /* SUFFIX holds the instruction size suffix for byte, word, dword
314 or qword, if given. */
252b5132
RH
315 char suffix;
316
47926f60 317 /* OPERANDS gives the number of given operands. */
252b5132
RH
318 unsigned int operands;
319
320 /* REG_OPERANDS, DISP_OPERANDS, MEM_OPERANDS, IMM_OPERANDS give the number
321 of given register, displacement, memory operands and immediate
47926f60 322 operands. */
252b5132
RH
323 unsigned int reg_operands, disp_operands, mem_operands, imm_operands;
324
325 /* TYPES [i] is the type (see above #defines) which tells us how to
520dc8e8 326 use OP[i] for the corresponding operand. */
40fb9820 327 i386_operand_type types[MAX_OPERANDS];
252b5132 328
520dc8e8
AM
329 /* Displacement expression, immediate expression, or register for each
330 operand. */
331 union i386_op op[MAX_OPERANDS];
252b5132 332
3e73aa7c
JH
333 /* Flags for operands. */
334 unsigned int flags[MAX_OPERANDS];
335#define Operand_PCrel 1
c48dadc9 336#define Operand_Mem 2
3e73aa7c 337
252b5132 338 /* Relocation type for operand */
f86103b7 339 enum bfd_reloc_code_real reloc[MAX_OPERANDS];
252b5132 340
252b5132
RH
341 /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
342 the base index byte below. */
343 const reg_entry *base_reg;
344 const reg_entry *index_reg;
345 unsigned int log2_scale_factor;
346
347 /* SEG gives the seg_entries of this insn. They are zero unless
47926f60 348 explicit segment overrides are given. */
ce8a8b2f 349 const seg_entry *seg[2];
252b5132 350
8325cc63
JB
351 /* Copied first memory operand string, for re-checking. */
352 char *memop1_string;
353
252b5132
RH
354 /* PREFIX holds all the given prefix opcodes (usually null).
355 PREFIXES is the number of prefix opcodes. */
356 unsigned int prefixes;
357 unsigned char prefix[MAX_PREFIXES];
358
50128d0c
JB
359 /* Register is in low 3 bits of opcode. */
360 bfd_boolean short_form;
361
6f2f06be
JB
362 /* The operand to a branch insn indicates an absolute branch. */
363 bfd_boolean jumpabsolute;
364
921eafea
L
365 /* Extended states. */
366 enum
367 {
368 /* Use MMX state. */
369 xstate_mmx = 1 << 0,
370 /* Use XMM state. */
371 xstate_xmm = 1 << 1,
372 /* Use YMM state. */
373 xstate_ymm = 1 << 2 | xstate_xmm,
374 /* Use ZMM state. */
375 xstate_zmm = 1 << 3 | xstate_ymm,
376 /* Use TMM state. */
377 xstate_tmm = 1 << 4
378 } xstate;
260cd341 379
e379e5f3
L
380 /* Has GOTPC or TLS relocation. */
381 bfd_boolean has_gotpc_tls_reloc;
382
252b5132 383 /* RM and SIB are the modrm byte and the sib byte where the
c1e679ec 384 addressing modes of this insn are encoded. */
252b5132 385 modrm_byte rm;
3e73aa7c 386 rex_byte rex;
43234a1e 387 rex_byte vrex;
252b5132 388 sib_byte sib;
c0f3af97 389 vex_prefix vex;
b6169b20 390
43234a1e
L
391 /* Masking attributes. */
392 struct Mask_Operation *mask;
393
394 /* Rounding control and SAE attributes. */
395 struct RC_Operation *rounding;
396
397 /* Broadcasting attributes. */
398 struct Broadcast_Operation *broadcast;
399
400 /* Compressed disp8*N attribute. */
401 unsigned int memshift;
402
86fa6981
L
403 /* Prefer load or store in encoding. */
404 enum
405 {
406 dir_encoding_default = 0,
407 dir_encoding_load,
64c49ab3
JB
408 dir_encoding_store,
409 dir_encoding_swap
86fa6981 410 } dir_encoding;
891edac4 411
41eb8e88 412 /* Prefer 8bit, 16bit, 32bit displacement in encoding. */
a501d77e
L
413 enum
414 {
415 disp_encoding_default = 0,
416 disp_encoding_8bit,
41eb8e88 417 disp_encoding_16bit,
a501d77e
L
418 disp_encoding_32bit
419 } disp_encoding;
f8a5c266 420
6b6b6807
L
421 /* Prefer the REX byte in encoding. */
422 bfd_boolean rex_encoding;
423
b6f8c7c4
L
424 /* Disable instruction size optimization. */
425 bfd_boolean no_optimize;
426
86fa6981
L
427 /* How to encode vector instructions. */
428 enum
429 {
430 vex_encoding_default = 0,
42e04b36 431 vex_encoding_vex,
86fa6981 432 vex_encoding_vex3,
da4977e0
JB
433 vex_encoding_evex,
434 vex_encoding_error
86fa6981
L
435 } vec_encoding;
436
d5de92cf
L
437 /* REP prefix. */
438 const char *rep_prefix;
439
165de32a
L
440 /* HLE prefix. */
441 const char *hle_prefix;
42164a71 442
7e8b059b
L
443 /* Have BND prefix. */
444 const char *bnd_prefix;
445
04ef582a
L
446 /* Have NOTRACK prefix. */
447 const char *notrack_prefix;
448
891edac4 449 /* Error message. */
a65babc9 450 enum i386_error error;
252b5132
RH
451 };
452
453typedef struct _i386_insn i386_insn;
454
43234a1e
L
455/* Link RC type with corresponding string, that'll be looked for in
456 asm. */
457struct RC_name
458{
459 enum rc_type type;
460 const char *name;
461 unsigned int len;
462};
463
464static const struct RC_name RC_NamesTable[] =
465{
466 { rne, STRING_COMMA_LEN ("rn-sae") },
467 { rd, STRING_COMMA_LEN ("rd-sae") },
468 { ru, STRING_COMMA_LEN ("ru-sae") },
469 { rz, STRING_COMMA_LEN ("rz-sae") },
470 { saeonly, STRING_COMMA_LEN ("sae") },
471};
472
252b5132
RH
473/* List of chars besides those in app.c:symbol_chars that can start an
474 operand. Used to prevent the scrubber eating vital white-space. */
86fa6981 475const char extra_symbol_chars[] = "*%-([{}"
252b5132 476#ifdef LEX_AT
32137342
NC
477 "@"
478#endif
479#ifdef LEX_QM
480 "?"
252b5132 481#endif
32137342 482 ;
252b5132 483
b3983e5f
JB
484#if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
485 && !defined (TE_GNU) \
486 && !defined (TE_LINUX) \
487 && !defined (TE_FreeBSD) \
488 && !defined (TE_DragonFly) \
489 && !defined (TE_NetBSD))
252b5132 490/* This array holds the chars that always start a comment. If the
b3b91714
AM
491 pre-processor is disabled, these aren't very useful. The option
492 --divide will remove '/' from this list. */
493const char *i386_comment_chars = "#/";
494#define SVR4_COMMENT_CHARS 1
252b5132 495#define PREFIX_SEPARATOR '\\'
252b5132 496
b3b91714
AM
497#else
498const char *i386_comment_chars = "#";
499#define PREFIX_SEPARATOR '/'
500#endif
501
252b5132
RH
502/* This array holds the chars that only start a comment at the beginning of
503 a line. If the line seems to have the form '# 123 filename'
ce8a8b2f
AM
504 .line and .file directives will appear in the pre-processed output.
505 Note that input_file.c hand checks for '#' at the beginning of the
252b5132 506 first line of the input file. This is because the compiler outputs
ce8a8b2f
AM
507 #NO_APP at the beginning of its output.
508 Also note that comments started like this one will always work if
252b5132 509 '/' isn't otherwise defined. */
b3b91714 510const char line_comment_chars[] = "#/";
252b5132 511
63a0b638 512const char line_separator_chars[] = ";";
252b5132 513
ce8a8b2f
AM
514/* Chars that can be used to separate mant from exp in floating point
515 nums. */
252b5132
RH
516const char EXP_CHARS[] = "eE";
517
ce8a8b2f
AM
518/* Chars that mean this number is a floating point constant
519 As in 0f12.456
520 or 0d1.2345e12. */
252b5132
RH
521const char FLT_CHARS[] = "fFdDxX";
522
ce8a8b2f 523/* Tables for lexical analysis. */
252b5132
RH
524static char mnemonic_chars[256];
525static char register_chars[256];
526static char operand_chars[256];
527static char identifier_chars[256];
528static char digit_chars[256];
529
ce8a8b2f 530/* Lexical macros. */
252b5132
RH
531#define is_mnemonic_char(x) (mnemonic_chars[(unsigned char) x])
532#define is_operand_char(x) (operand_chars[(unsigned char) x])
533#define is_register_char(x) (register_chars[(unsigned char) x])
534#define is_space_char(x) ((x) == ' ')
535#define is_identifier_char(x) (identifier_chars[(unsigned char) x])
536#define is_digit_char(x) (digit_chars[(unsigned char) x])
537
0234cb7c 538/* All non-digit non-letter characters that may occur in an operand. */
252b5132
RH
539static char operand_special_chars[] = "%$-+(,)*._~/<>|&^!:[@]";
540
541/* md_assemble() always leaves the strings it's passed unaltered. To
542 effect this we maintain a stack of saved characters that we've smashed
543 with '\0's (indicating end of strings for various sub-fields of the
47926f60 544 assembler instruction). */
252b5132 545static char save_stack[32];
ce8a8b2f 546static char *save_stack_p;
252b5132
RH
547#define END_STRING_AND_SAVE(s) \
548 do { *save_stack_p++ = *(s); *(s) = '\0'; } while (0)
549#define RESTORE_END_STRING(s) \
550 do { *(s) = *--save_stack_p; } while (0)
551
47926f60 552/* The instruction we're assembling. */
252b5132
RH
553static i386_insn i;
554
555/* Possible templates for current insn. */
556static const templates *current_templates;
557
31b2323c
L
558/* Per instruction expressionS buffers: max displacements & immediates. */
559static expressionS disp_expressions[MAX_MEMORY_OPERANDS];
560static expressionS im_expressions[MAX_IMMEDIATE_OPERANDS];
252b5132 561
47926f60 562/* Current operand we are working on. */
ee86248c 563static int this_operand = -1;
252b5132 564
3e73aa7c
JH
565/* We support four different modes. FLAG_CODE variable is used to distinguish
566 these. */
567
568enum flag_code {
569 CODE_32BIT,
570 CODE_16BIT,
571 CODE_64BIT };
572
573static enum flag_code flag_code;
4fa24527 574static unsigned int object_64bit;
862be3fb 575static unsigned int disallow_64bit_reloc;
3e73aa7c 576static int use_rela_relocations = 0;
e379e5f3
L
577/* __tls_get_addr/___tls_get_addr symbol for TLS. */
578static const char *tls_get_addr;
3e73aa7c 579
7af8ed2d
NC
580#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
581 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
582 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
583
351f65ca
L
584/* The ELF ABI to use. */
585enum x86_elf_abi
586{
587 I386_ABI,
7f56bc95
L
588 X86_64_ABI,
589 X86_64_X32_ABI
351f65ca
L
590};
591
592static enum x86_elf_abi x86_elf_abi = I386_ABI;
7af8ed2d 593#endif
351f65ca 594
167ad85b
TG
595#if defined (TE_PE) || defined (TE_PEP)
596/* Use big object file format. */
597static int use_big_obj = 0;
598#endif
599
8dcea932
L
600#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
601/* 1 if generating code for a shared library. */
602static int shared = 0;
603#endif
604
47926f60
KH
605/* 1 for intel syntax,
606 0 if att syntax. */
607static int intel_syntax = 0;
252b5132 608
4b5aaf5f
L
609static enum x86_64_isa
610{
611 amd64 = 1, /* AMD64 ISA. */
612 intel64 /* Intel64 ISA. */
613} isa64;
e89c5eaa 614
1efbbeb4
L
615/* 1 for intel mnemonic,
616 0 if att mnemonic. */
617static int intel_mnemonic = !SYSV386_COMPAT;
618
a60de03c
JB
619/* 1 if pseudo registers are permitted. */
620static int allow_pseudo_reg = 0;
621
47926f60
KH
622/* 1 if register prefix % not required. */
623static int allow_naked_reg = 0;
252b5132 624
33eaf5de 625/* 1 if the assembler should add BND prefix for all control-transferring
7e8b059b
L
626 instructions supporting it, even if this prefix wasn't specified
627 explicitly. */
628static int add_bnd_prefix = 0;
629
ba104c83 630/* 1 if pseudo index register, eiz/riz, is allowed . */
db51cc60
L
631static int allow_index_reg = 0;
632
d022bddd
IT
633/* 1 if the assembler should ignore LOCK prefix, even if it was
634 specified explicitly. */
635static int omit_lock_prefix = 0;
636
e4e00185
AS
637/* 1 if the assembler should encode lfence, mfence, and sfence as
638 "lock addl $0, (%{re}sp)". */
639static int avoid_fence = 0;
640
ae531041
L
641/* 1 if lfence should be inserted after every load. */
642static int lfence_after_load = 0;
643
644/* Non-zero if lfence should be inserted before indirect branch. */
645static enum lfence_before_indirect_branch_kind
646 {
647 lfence_branch_none = 0,
648 lfence_branch_register,
649 lfence_branch_memory,
650 lfence_branch_all
651 }
652lfence_before_indirect_branch;
653
654/* Non-zero if lfence should be inserted before ret. */
655static enum lfence_before_ret_kind
656 {
657 lfence_before_ret_none = 0,
658 lfence_before_ret_not,
a09f656b 659 lfence_before_ret_or,
660 lfence_before_ret_shl
ae531041
L
661 }
662lfence_before_ret;
663
664/* Types of previous instruction is .byte or prefix. */
e379e5f3
L
665static struct
666 {
667 segT seg;
668 const char *file;
669 const char *name;
670 unsigned int line;
671 enum last_insn_kind
672 {
673 last_insn_other = 0,
674 last_insn_directive,
675 last_insn_prefix
676 } kind;
677 } last_insn;
678
0cb4071e
L
679/* 1 if the assembler should generate relax relocations. */
680
681static int generate_relax_relocations
682 = DEFAULT_GENERATE_X86_RELAX_RELOCATIONS;
683
7bab8ab5 684static enum check_kind
daf50ae7 685 {
7bab8ab5
JB
686 check_none = 0,
687 check_warning,
688 check_error
daf50ae7 689 }
7bab8ab5 690sse_check, operand_check = check_warning;
daf50ae7 691
e379e5f3
L
692/* Non-zero if branches should be aligned within power of 2 boundary. */
693static int align_branch_power = 0;
694
695/* Types of branches to align. */
696enum align_branch_kind
697 {
698 align_branch_none = 0,
699 align_branch_jcc = 1,
700 align_branch_fused = 2,
701 align_branch_jmp = 3,
702 align_branch_call = 4,
703 align_branch_indirect = 5,
704 align_branch_ret = 6
705 };
706
707/* Type bits of branches to align. */
708enum align_branch_bit
709 {
710 align_branch_jcc_bit = 1 << align_branch_jcc,
711 align_branch_fused_bit = 1 << align_branch_fused,
712 align_branch_jmp_bit = 1 << align_branch_jmp,
713 align_branch_call_bit = 1 << align_branch_call,
714 align_branch_indirect_bit = 1 << align_branch_indirect,
715 align_branch_ret_bit = 1 << align_branch_ret
716 };
717
718static unsigned int align_branch = (align_branch_jcc_bit
719 | align_branch_fused_bit
720 | align_branch_jmp_bit);
721
79d72f45
HL
722/* Types of condition jump used by macro-fusion. */
723enum mf_jcc_kind
724 {
725 mf_jcc_jo = 0, /* base opcode 0x70 */
726 mf_jcc_jc, /* base opcode 0x72 */
727 mf_jcc_je, /* base opcode 0x74 */
728 mf_jcc_jna, /* base opcode 0x76 */
729 mf_jcc_js, /* base opcode 0x78 */
730 mf_jcc_jp, /* base opcode 0x7a */
731 mf_jcc_jl, /* base opcode 0x7c */
732 mf_jcc_jle, /* base opcode 0x7e */
733 };
734
735/* Types of compare flag-modifying insntructions used by macro-fusion. */
736enum mf_cmp_kind
737 {
738 mf_cmp_test_and, /* test/cmp */
739 mf_cmp_alu_cmp, /* add/sub/cmp */
740 mf_cmp_incdec /* inc/dec */
741 };
742
e379e5f3
L
743/* The maximum padding size for fused jcc. CMP like instruction can
744 be 9 bytes and jcc can be 6 bytes. Leave room just in case for
745 prefixes. */
746#define MAX_FUSED_JCC_PADDING_SIZE 20
747
748/* The maximum number of prefixes added for an instruction. */
749static unsigned int align_branch_prefix_size = 5;
750
b6f8c7c4
L
751/* Optimization:
752 1. Clear the REX_W bit with register operand if possible.
753 2. Above plus use 128bit vector instruction to clear the full vector
754 register.
755 */
756static int optimize = 0;
757
758/* Optimization:
759 1. Clear the REX_W bit with register operand if possible.
760 2. Above plus use 128bit vector instruction to clear the full vector
761 register.
762 3. Above plus optimize "test{q,l,w} $imm8,%r{64,32,16}" to
763 "testb $imm7,%r8".
764 */
765static int optimize_for_space = 0;
766
2ca3ace5
L
767/* Register prefix used for error message. */
768static const char *register_prefix = "%";
769
47926f60
KH
770/* Used in 16 bit gcc mode to add an l suffix to call, ret, enter,
771 leave, push, and pop instructions so that gcc has the same stack
772 frame as in 32 bit mode. */
773static char stackop_size = '\0';
eecb386c 774
12b55ccc
L
775/* Non-zero to optimize code alignment. */
776int optimize_align_code = 1;
777
47926f60
KH
778/* Non-zero to quieten some warnings. */
779static int quiet_warnings = 0;
a38cf1db 780
47926f60
KH
781/* CPU name. */
782static const char *cpu_arch_name = NULL;
6305a203 783static char *cpu_sub_arch_name = NULL;
a38cf1db 784
47926f60 785/* CPU feature flags. */
40fb9820
L
786static i386_cpu_flags cpu_arch_flags = CPU_UNKNOWN_FLAGS;
787
ccc9c027
L
788/* If we have selected a cpu we are generating instructions for. */
789static int cpu_arch_tune_set = 0;
790
9103f4f4 791/* Cpu we are generating instructions for. */
fbf3f584 792enum processor_type cpu_arch_tune = PROCESSOR_UNKNOWN;
9103f4f4
L
793
794/* CPU feature flags of cpu we are generating instructions for. */
40fb9820 795static i386_cpu_flags cpu_arch_tune_flags;
9103f4f4 796
ccc9c027 797/* CPU instruction set architecture used. */
fbf3f584 798enum processor_type cpu_arch_isa = PROCESSOR_UNKNOWN;
ccc9c027 799
9103f4f4 800/* CPU feature flags of instruction set architecture used. */
fbf3f584 801i386_cpu_flags cpu_arch_isa_flags;
9103f4f4 802
fddf5b5b
AM
803/* If set, conditional jumps are not automatically promoted to handle
804 larger than a byte offset. */
805static unsigned int no_cond_jump_promotion = 0;
806
c0f3af97
L
807/* Encode SSE instructions with VEX prefix. */
808static unsigned int sse2avx;
809
539f890d
L
810/* Encode scalar AVX instructions with specific vector length. */
811static enum
812 {
813 vex128 = 0,
814 vex256
815 } avxscalar;
816
03751133
L
817/* Encode VEX WIG instructions with specific vex.w. */
818static enum
819 {
820 vexw0 = 0,
821 vexw1
822 } vexwig;
823
43234a1e
L
824/* Encode scalar EVEX LIG instructions with specific vector length. */
825static enum
826 {
827 evexl128 = 0,
828 evexl256,
829 evexl512
830 } evexlig;
831
832/* Encode EVEX WIG instructions with specific evex.w. */
833static enum
834 {
835 evexw0 = 0,
836 evexw1
837 } evexwig;
838
d3d3c6db
IT
839/* Value to encode in EVEX RC bits, for SAE-only instructions. */
840static enum rc_type evexrcig = rne;
841
29b0f896 842/* Pre-defined "_GLOBAL_OFFSET_TABLE_". */
87c245cc 843static symbolS *GOT_symbol;
29b0f896 844
a4447b93
RH
845/* The dwarf2 return column, adjusted for 32 or 64 bit. */
846unsigned int x86_dwarf2_return_column;
847
848/* The dwarf2 data alignment, adjusted for 32 or 64 bit. */
849int x86_cie_data_alignment;
850
252b5132 851/* Interface to relax_segment.
fddf5b5b
AM
852 There are 3 major relax states for 386 jump insns because the
853 different types of jumps add different sizes to frags when we're
e379e5f3
L
854 figuring out what sort of jump to choose to reach a given label.
855
856 BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING are used to align
857 branches which are handled by md_estimate_size_before_relax() and
858 i386_generic_table_relax_frag(). */
252b5132 859
47926f60 860/* Types. */
93c2a809
AM
861#define UNCOND_JUMP 0
862#define COND_JUMP 1
863#define COND_JUMP86 2
e379e5f3
L
864#define BRANCH_PADDING 3
865#define BRANCH_PREFIX 4
866#define FUSED_JCC_PADDING 5
fddf5b5b 867
47926f60 868/* Sizes. */
252b5132
RH
869#define CODE16 1
870#define SMALL 0
29b0f896 871#define SMALL16 (SMALL | CODE16)
252b5132 872#define BIG 2
29b0f896 873#define BIG16 (BIG | CODE16)
252b5132
RH
874
875#ifndef INLINE
876#ifdef __GNUC__
877#define INLINE __inline__
878#else
879#define INLINE
880#endif
881#endif
882
fddf5b5b
AM
883#define ENCODE_RELAX_STATE(type, size) \
884 ((relax_substateT) (((type) << 2) | (size)))
885#define TYPE_FROM_RELAX_STATE(s) \
886 ((s) >> 2)
887#define DISP_SIZE_FROM_RELAX_STATE(s) \
888 ((((s) & 3) == BIG ? 4 : (((s) & 3) == BIG16 ? 2 : 1)))
252b5132
RH
889
890/* This table is used by relax_frag to promote short jumps to long
891 ones where necessary. SMALL (short) jumps may be promoted to BIG
892 (32 bit long) ones, and SMALL16 jumps to BIG16 (16 bit long). We
893 don't allow a short jump in a 32 bit code segment to be promoted to
894 a 16 bit offset jump because it's slower (requires data size
895 prefix), and doesn't work, unless the destination is in the bottom
896 64k of the code segment (The top 16 bits of eip are zeroed). */
897
898const relax_typeS md_relax_table[] =
899{
24eab124
AM
900 /* The fields are:
901 1) most positive reach of this state,
902 2) most negative reach of this state,
93c2a809 903 3) how many bytes this mode will have in the variable part of the frag
ce8a8b2f 904 4) which index into the table to try if we can't fit into this one. */
252b5132 905
fddf5b5b 906 /* UNCOND_JUMP states. */
93c2a809
AM
907 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG)},
908 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16)},
909 /* dword jmp adds 4 bytes to frag:
910 0 extra opcode bytes, 4 displacement bytes. */
252b5132 911 {0, 0, 4, 0},
93c2a809
AM
912 /* word jmp adds 2 byte2 to frag:
913 0 extra opcode bytes, 2 displacement bytes. */
252b5132
RH
914 {0, 0, 2, 0},
915
93c2a809
AM
916 /* COND_JUMP states. */
917 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG)},
918 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG16)},
919 /* dword conditionals adds 5 bytes to frag:
920 1 extra opcode byte, 4 displacement bytes. */
921 {0, 0, 5, 0},
fddf5b5b 922 /* word conditionals add 3 bytes to frag:
93c2a809
AM
923 1 extra opcode byte, 2 displacement bytes. */
924 {0, 0, 3, 0},
925
926 /* COND_JUMP86 states. */
927 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG)},
928 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG16)},
929 /* dword conditionals adds 5 bytes to frag:
930 1 extra opcode byte, 4 displacement bytes. */
931 {0, 0, 5, 0},
932 /* word conditionals add 4 bytes to frag:
933 1 displacement byte and a 3 byte long branch insn. */
934 {0, 0, 4, 0}
252b5132
RH
935};
936
9103f4f4
L
937static const arch_entry cpu_arch[] =
938{
89507696
JB
939 /* Do not replace the first two entries - i386_target_format()
940 relies on them being there in this order. */
8a2c8fef 941 { STRING_COMMA_LEN ("generic32"), PROCESSOR_GENERIC32,
293f5f65 942 CPU_GENERIC32_FLAGS, 0 },
8a2c8fef 943 { STRING_COMMA_LEN ("generic64"), PROCESSOR_GENERIC64,
293f5f65 944 CPU_GENERIC64_FLAGS, 0 },
8a2c8fef 945 { STRING_COMMA_LEN ("i8086"), PROCESSOR_UNKNOWN,
293f5f65 946 CPU_NONE_FLAGS, 0 },
8a2c8fef 947 { STRING_COMMA_LEN ("i186"), PROCESSOR_UNKNOWN,
293f5f65 948 CPU_I186_FLAGS, 0 },
8a2c8fef 949 { STRING_COMMA_LEN ("i286"), PROCESSOR_UNKNOWN,
293f5f65 950 CPU_I286_FLAGS, 0 },
8a2c8fef 951 { STRING_COMMA_LEN ("i386"), PROCESSOR_I386,
293f5f65 952 CPU_I386_FLAGS, 0 },
8a2c8fef 953 { STRING_COMMA_LEN ("i486"), PROCESSOR_I486,
293f5f65 954 CPU_I486_FLAGS, 0 },
8a2c8fef 955 { STRING_COMMA_LEN ("i586"), PROCESSOR_PENTIUM,
293f5f65 956 CPU_I586_FLAGS, 0 },
8a2c8fef 957 { STRING_COMMA_LEN ("i686"), PROCESSOR_PENTIUMPRO,
293f5f65 958 CPU_I686_FLAGS, 0 },
8a2c8fef 959 { STRING_COMMA_LEN ("pentium"), PROCESSOR_PENTIUM,
293f5f65 960 CPU_I586_FLAGS, 0 },
8a2c8fef 961 { STRING_COMMA_LEN ("pentiumpro"), PROCESSOR_PENTIUMPRO,
293f5f65 962 CPU_PENTIUMPRO_FLAGS, 0 },
8a2c8fef 963 { STRING_COMMA_LEN ("pentiumii"), PROCESSOR_PENTIUMPRO,
293f5f65 964 CPU_P2_FLAGS, 0 },
8a2c8fef 965 { STRING_COMMA_LEN ("pentiumiii"),PROCESSOR_PENTIUMPRO,
293f5f65 966 CPU_P3_FLAGS, 0 },
8a2c8fef 967 { STRING_COMMA_LEN ("pentium4"), PROCESSOR_PENTIUM4,
293f5f65 968 CPU_P4_FLAGS, 0 },
8a2c8fef 969 { STRING_COMMA_LEN ("prescott"), PROCESSOR_NOCONA,
293f5f65 970 CPU_CORE_FLAGS, 0 },
8a2c8fef 971 { STRING_COMMA_LEN ("nocona"), PROCESSOR_NOCONA,
293f5f65 972 CPU_NOCONA_FLAGS, 0 },
8a2c8fef 973 { STRING_COMMA_LEN ("yonah"), PROCESSOR_CORE,
293f5f65 974 CPU_CORE_FLAGS, 1 },
8a2c8fef 975 { STRING_COMMA_LEN ("core"), PROCESSOR_CORE,
293f5f65 976 CPU_CORE_FLAGS, 0 },
8a2c8fef 977 { STRING_COMMA_LEN ("merom"), PROCESSOR_CORE2,
293f5f65 978 CPU_CORE2_FLAGS, 1 },
8a2c8fef 979 { STRING_COMMA_LEN ("core2"), PROCESSOR_CORE2,
293f5f65 980 CPU_CORE2_FLAGS, 0 },
8a2c8fef 981 { STRING_COMMA_LEN ("corei7"), PROCESSOR_COREI7,
293f5f65 982 CPU_COREI7_FLAGS, 0 },
8a2c8fef 983 { STRING_COMMA_LEN ("l1om"), PROCESSOR_L1OM,
293f5f65 984 CPU_L1OM_FLAGS, 0 },
7a9068fe 985 { STRING_COMMA_LEN ("k1om"), PROCESSOR_K1OM,
293f5f65 986 CPU_K1OM_FLAGS, 0 },
81486035 987 { STRING_COMMA_LEN ("iamcu"), PROCESSOR_IAMCU,
293f5f65 988 CPU_IAMCU_FLAGS, 0 },
8a2c8fef 989 { STRING_COMMA_LEN ("k6"), PROCESSOR_K6,
293f5f65 990 CPU_K6_FLAGS, 0 },
8a2c8fef 991 { STRING_COMMA_LEN ("k6_2"), PROCESSOR_K6,
293f5f65 992 CPU_K6_2_FLAGS, 0 },
8a2c8fef 993 { STRING_COMMA_LEN ("athlon"), PROCESSOR_ATHLON,
293f5f65 994 CPU_ATHLON_FLAGS, 0 },
8a2c8fef 995 { STRING_COMMA_LEN ("sledgehammer"), PROCESSOR_K8,
293f5f65 996 CPU_K8_FLAGS, 1 },
8a2c8fef 997 { STRING_COMMA_LEN ("opteron"), PROCESSOR_K8,
293f5f65 998 CPU_K8_FLAGS, 0 },
8a2c8fef 999 { STRING_COMMA_LEN ("k8"), PROCESSOR_K8,
293f5f65 1000 CPU_K8_FLAGS, 0 },
8a2c8fef 1001 { STRING_COMMA_LEN ("amdfam10"), PROCESSOR_AMDFAM10,
293f5f65 1002 CPU_AMDFAM10_FLAGS, 0 },
8aedb9fe 1003 { STRING_COMMA_LEN ("bdver1"), PROCESSOR_BD,
293f5f65 1004 CPU_BDVER1_FLAGS, 0 },
8aedb9fe 1005 { STRING_COMMA_LEN ("bdver2"), PROCESSOR_BD,
293f5f65 1006 CPU_BDVER2_FLAGS, 0 },
5e5c50d3 1007 { STRING_COMMA_LEN ("bdver3"), PROCESSOR_BD,
293f5f65 1008 CPU_BDVER3_FLAGS, 0 },
c7b0bd56 1009 { STRING_COMMA_LEN ("bdver4"), PROCESSOR_BD,
293f5f65 1010 CPU_BDVER4_FLAGS, 0 },
029f3522 1011 { STRING_COMMA_LEN ("znver1"), PROCESSOR_ZNVER,
293f5f65 1012 CPU_ZNVER1_FLAGS, 0 },
a9660a6f
AP
1013 { STRING_COMMA_LEN ("znver2"), PROCESSOR_ZNVER,
1014 CPU_ZNVER2_FLAGS, 0 },
7b458c12 1015 { STRING_COMMA_LEN ("btver1"), PROCESSOR_BT,
293f5f65 1016 CPU_BTVER1_FLAGS, 0 },
7b458c12 1017 { STRING_COMMA_LEN ("btver2"), PROCESSOR_BT,
293f5f65 1018 CPU_BTVER2_FLAGS, 0 },
8a2c8fef 1019 { STRING_COMMA_LEN (".8087"), PROCESSOR_UNKNOWN,
293f5f65 1020 CPU_8087_FLAGS, 0 },
8a2c8fef 1021 { STRING_COMMA_LEN (".287"), PROCESSOR_UNKNOWN,
293f5f65 1022 CPU_287_FLAGS, 0 },
8a2c8fef 1023 { STRING_COMMA_LEN (".387"), PROCESSOR_UNKNOWN,
293f5f65 1024 CPU_387_FLAGS, 0 },
1848e567
L
1025 { STRING_COMMA_LEN (".687"), PROCESSOR_UNKNOWN,
1026 CPU_687_FLAGS, 0 },
d871f3f4
L
1027 { STRING_COMMA_LEN (".cmov"), PROCESSOR_UNKNOWN,
1028 CPU_CMOV_FLAGS, 0 },
1029 { STRING_COMMA_LEN (".fxsr"), PROCESSOR_UNKNOWN,
1030 CPU_FXSR_FLAGS, 0 },
8a2c8fef 1031 { STRING_COMMA_LEN (".mmx"), PROCESSOR_UNKNOWN,
293f5f65 1032 CPU_MMX_FLAGS, 0 },
8a2c8fef 1033 { STRING_COMMA_LEN (".sse"), PROCESSOR_UNKNOWN,
293f5f65 1034 CPU_SSE_FLAGS, 0 },
8a2c8fef 1035 { STRING_COMMA_LEN (".sse2"), PROCESSOR_UNKNOWN,
293f5f65 1036 CPU_SSE2_FLAGS, 0 },
8a2c8fef 1037 { STRING_COMMA_LEN (".sse3"), PROCESSOR_UNKNOWN,
293f5f65 1038 CPU_SSE3_FLAGS, 0 },
af5c13b0
L
1039 { STRING_COMMA_LEN (".sse4a"), PROCESSOR_UNKNOWN,
1040 CPU_SSE4A_FLAGS, 0 },
8a2c8fef 1041 { STRING_COMMA_LEN (".ssse3"), PROCESSOR_UNKNOWN,
293f5f65 1042 CPU_SSSE3_FLAGS, 0 },
8a2c8fef 1043 { STRING_COMMA_LEN (".sse4.1"), PROCESSOR_UNKNOWN,
293f5f65 1044 CPU_SSE4_1_FLAGS, 0 },
8a2c8fef 1045 { STRING_COMMA_LEN (".sse4.2"), PROCESSOR_UNKNOWN,
293f5f65 1046 CPU_SSE4_2_FLAGS, 0 },
8a2c8fef 1047 { STRING_COMMA_LEN (".sse4"), PROCESSOR_UNKNOWN,
293f5f65 1048 CPU_SSE4_2_FLAGS, 0 },
8a2c8fef 1049 { STRING_COMMA_LEN (".avx"), PROCESSOR_UNKNOWN,
293f5f65 1050 CPU_AVX_FLAGS, 0 },
6c30d220 1051 { STRING_COMMA_LEN (".avx2"), PROCESSOR_UNKNOWN,
293f5f65 1052 CPU_AVX2_FLAGS, 0 },
43234a1e 1053 { STRING_COMMA_LEN (".avx512f"), PROCESSOR_UNKNOWN,
293f5f65 1054 CPU_AVX512F_FLAGS, 0 },
43234a1e 1055 { STRING_COMMA_LEN (".avx512cd"), PROCESSOR_UNKNOWN,
293f5f65 1056 CPU_AVX512CD_FLAGS, 0 },
43234a1e 1057 { STRING_COMMA_LEN (".avx512er"), PROCESSOR_UNKNOWN,
293f5f65 1058 CPU_AVX512ER_FLAGS, 0 },
43234a1e 1059 { STRING_COMMA_LEN (".avx512pf"), PROCESSOR_UNKNOWN,
293f5f65 1060 CPU_AVX512PF_FLAGS, 0 },
1dfc6506 1061 { STRING_COMMA_LEN (".avx512dq"), PROCESSOR_UNKNOWN,
293f5f65 1062 CPU_AVX512DQ_FLAGS, 0 },
1dfc6506 1063 { STRING_COMMA_LEN (".avx512bw"), PROCESSOR_UNKNOWN,
293f5f65 1064 CPU_AVX512BW_FLAGS, 0 },
1dfc6506 1065 { STRING_COMMA_LEN (".avx512vl"), PROCESSOR_UNKNOWN,
293f5f65 1066 CPU_AVX512VL_FLAGS, 0 },
8a2c8fef 1067 { STRING_COMMA_LEN (".vmx"), PROCESSOR_UNKNOWN,
293f5f65 1068 CPU_VMX_FLAGS, 0 },
8729a6f6 1069 { STRING_COMMA_LEN (".vmfunc"), PROCESSOR_UNKNOWN,
293f5f65 1070 CPU_VMFUNC_FLAGS, 0 },
8a2c8fef 1071 { STRING_COMMA_LEN (".smx"), PROCESSOR_UNKNOWN,
293f5f65 1072 CPU_SMX_FLAGS, 0 },
8a2c8fef 1073 { STRING_COMMA_LEN (".xsave"), PROCESSOR_UNKNOWN,
293f5f65 1074 CPU_XSAVE_FLAGS, 0 },
c7b8aa3a 1075 { STRING_COMMA_LEN (".xsaveopt"), PROCESSOR_UNKNOWN,
293f5f65 1076 CPU_XSAVEOPT_FLAGS, 0 },
1dfc6506 1077 { STRING_COMMA_LEN (".xsavec"), PROCESSOR_UNKNOWN,
293f5f65 1078 CPU_XSAVEC_FLAGS, 0 },
1dfc6506 1079 { STRING_COMMA_LEN (".xsaves"), PROCESSOR_UNKNOWN,
293f5f65 1080 CPU_XSAVES_FLAGS, 0 },
8a2c8fef 1081 { STRING_COMMA_LEN (".aes"), PROCESSOR_UNKNOWN,
293f5f65 1082 CPU_AES_FLAGS, 0 },
8a2c8fef 1083 { STRING_COMMA_LEN (".pclmul"), PROCESSOR_UNKNOWN,
293f5f65 1084 CPU_PCLMUL_FLAGS, 0 },
8a2c8fef 1085 { STRING_COMMA_LEN (".clmul"), PROCESSOR_UNKNOWN,
293f5f65 1086 CPU_PCLMUL_FLAGS, 1 },
c7b8aa3a 1087 { STRING_COMMA_LEN (".fsgsbase"), PROCESSOR_UNKNOWN,
293f5f65 1088 CPU_FSGSBASE_FLAGS, 0 },
c7b8aa3a 1089 { STRING_COMMA_LEN (".rdrnd"), PROCESSOR_UNKNOWN,
293f5f65 1090 CPU_RDRND_FLAGS, 0 },
c7b8aa3a 1091 { STRING_COMMA_LEN (".f16c"), PROCESSOR_UNKNOWN,
293f5f65 1092 CPU_F16C_FLAGS, 0 },
6c30d220 1093 { STRING_COMMA_LEN (".bmi2"), PROCESSOR_UNKNOWN,
293f5f65 1094 CPU_BMI2_FLAGS, 0 },
8a2c8fef 1095 { STRING_COMMA_LEN (".fma"), PROCESSOR_UNKNOWN,
293f5f65 1096 CPU_FMA_FLAGS, 0 },
8a2c8fef 1097 { STRING_COMMA_LEN (".fma4"), PROCESSOR_UNKNOWN,
293f5f65 1098 CPU_FMA4_FLAGS, 0 },
8a2c8fef 1099 { STRING_COMMA_LEN (".xop"), PROCESSOR_UNKNOWN,
293f5f65 1100 CPU_XOP_FLAGS, 0 },
8a2c8fef 1101 { STRING_COMMA_LEN (".lwp"), PROCESSOR_UNKNOWN,
293f5f65 1102 CPU_LWP_FLAGS, 0 },
8a2c8fef 1103 { STRING_COMMA_LEN (".movbe"), PROCESSOR_UNKNOWN,
293f5f65 1104 CPU_MOVBE_FLAGS, 0 },
60aa667e 1105 { STRING_COMMA_LEN (".cx16"), PROCESSOR_UNKNOWN,
293f5f65 1106 CPU_CX16_FLAGS, 0 },
8a2c8fef 1107 { STRING_COMMA_LEN (".ept"), PROCESSOR_UNKNOWN,
293f5f65 1108 CPU_EPT_FLAGS, 0 },
6c30d220 1109 { STRING_COMMA_LEN (".lzcnt"), PROCESSOR_UNKNOWN,
293f5f65 1110 CPU_LZCNT_FLAGS, 0 },
272a84b1
L
1111 { STRING_COMMA_LEN (".popcnt"), PROCESSOR_UNKNOWN,
1112 CPU_POPCNT_FLAGS, 0 },
42164a71 1113 { STRING_COMMA_LEN (".hle"), PROCESSOR_UNKNOWN,
293f5f65 1114 CPU_HLE_FLAGS, 0 },
42164a71 1115 { STRING_COMMA_LEN (".rtm"), PROCESSOR_UNKNOWN,
293f5f65 1116 CPU_RTM_FLAGS, 0 },
6c30d220 1117 { STRING_COMMA_LEN (".invpcid"), PROCESSOR_UNKNOWN,
293f5f65 1118 CPU_INVPCID_FLAGS, 0 },
8a2c8fef 1119 { STRING_COMMA_LEN (".clflush"), PROCESSOR_UNKNOWN,
293f5f65 1120 CPU_CLFLUSH_FLAGS, 0 },
22109423 1121 { STRING_COMMA_LEN (".nop"), PROCESSOR_UNKNOWN,
293f5f65 1122 CPU_NOP_FLAGS, 0 },
8a2c8fef 1123 { STRING_COMMA_LEN (".syscall"), PROCESSOR_UNKNOWN,
293f5f65 1124 CPU_SYSCALL_FLAGS, 0 },
8a2c8fef 1125 { STRING_COMMA_LEN (".rdtscp"), PROCESSOR_UNKNOWN,
293f5f65 1126 CPU_RDTSCP_FLAGS, 0 },
8a2c8fef 1127 { STRING_COMMA_LEN (".3dnow"), PROCESSOR_UNKNOWN,
293f5f65 1128 CPU_3DNOW_FLAGS, 0 },
8a2c8fef 1129 { STRING_COMMA_LEN (".3dnowa"), PROCESSOR_UNKNOWN,
293f5f65 1130 CPU_3DNOWA_FLAGS, 0 },
8a2c8fef 1131 { STRING_COMMA_LEN (".padlock"), PROCESSOR_UNKNOWN,
293f5f65 1132 CPU_PADLOCK_FLAGS, 0 },
8a2c8fef 1133 { STRING_COMMA_LEN (".pacifica"), PROCESSOR_UNKNOWN,
293f5f65 1134 CPU_SVME_FLAGS, 1 },
8a2c8fef 1135 { STRING_COMMA_LEN (".svme"), PROCESSOR_UNKNOWN,
293f5f65 1136 CPU_SVME_FLAGS, 0 },
8a2c8fef 1137 { STRING_COMMA_LEN (".sse4a"), PROCESSOR_UNKNOWN,
293f5f65 1138 CPU_SSE4A_FLAGS, 0 },
8a2c8fef 1139 { STRING_COMMA_LEN (".abm"), PROCESSOR_UNKNOWN,
293f5f65 1140 CPU_ABM_FLAGS, 0 },
87973e9f 1141 { STRING_COMMA_LEN (".bmi"), PROCESSOR_UNKNOWN,
293f5f65 1142 CPU_BMI_FLAGS, 0 },
2a2a0f38 1143 { STRING_COMMA_LEN (".tbm"), PROCESSOR_UNKNOWN,
293f5f65 1144 CPU_TBM_FLAGS, 0 },
e2e1fcde 1145 { STRING_COMMA_LEN (".adx"), PROCESSOR_UNKNOWN,
293f5f65 1146 CPU_ADX_FLAGS, 0 },
e2e1fcde 1147 { STRING_COMMA_LEN (".rdseed"), PROCESSOR_UNKNOWN,
293f5f65 1148 CPU_RDSEED_FLAGS, 0 },
e2e1fcde 1149 { STRING_COMMA_LEN (".prfchw"), PROCESSOR_UNKNOWN,
293f5f65 1150 CPU_PRFCHW_FLAGS, 0 },
5c111e37 1151 { STRING_COMMA_LEN (".smap"), PROCESSOR_UNKNOWN,
293f5f65 1152 CPU_SMAP_FLAGS, 0 },
7e8b059b 1153 { STRING_COMMA_LEN (".mpx"), PROCESSOR_UNKNOWN,
293f5f65 1154 CPU_MPX_FLAGS, 0 },
a0046408 1155 { STRING_COMMA_LEN (".sha"), PROCESSOR_UNKNOWN,
293f5f65 1156 CPU_SHA_FLAGS, 0 },
963f3586 1157 { STRING_COMMA_LEN (".clflushopt"), PROCESSOR_UNKNOWN,
293f5f65 1158 CPU_CLFLUSHOPT_FLAGS, 0 },
dcf893b5 1159 { STRING_COMMA_LEN (".prefetchwt1"), PROCESSOR_UNKNOWN,
293f5f65 1160 CPU_PREFETCHWT1_FLAGS, 0 },
2cf200a4 1161 { STRING_COMMA_LEN (".se1"), PROCESSOR_UNKNOWN,
293f5f65 1162 CPU_SE1_FLAGS, 0 },
c5e7287a 1163 { STRING_COMMA_LEN (".clwb"), PROCESSOR_UNKNOWN,
293f5f65 1164 CPU_CLWB_FLAGS, 0 },
2cc1b5aa 1165 { STRING_COMMA_LEN (".avx512ifma"), PROCESSOR_UNKNOWN,
293f5f65 1166 CPU_AVX512IFMA_FLAGS, 0 },
14f195c9 1167 { STRING_COMMA_LEN (".avx512vbmi"), PROCESSOR_UNKNOWN,
293f5f65 1168 CPU_AVX512VBMI_FLAGS, 0 },
920d2ddc
IT
1169 { STRING_COMMA_LEN (".avx512_4fmaps"), PROCESSOR_UNKNOWN,
1170 CPU_AVX512_4FMAPS_FLAGS, 0 },
47acf0bd
IT
1171 { STRING_COMMA_LEN (".avx512_4vnniw"), PROCESSOR_UNKNOWN,
1172 CPU_AVX512_4VNNIW_FLAGS, 0 },
620214f7
IT
1173 { STRING_COMMA_LEN (".avx512_vpopcntdq"), PROCESSOR_UNKNOWN,
1174 CPU_AVX512_VPOPCNTDQ_FLAGS, 0 },
53467f57
IT
1175 { STRING_COMMA_LEN (".avx512_vbmi2"), PROCESSOR_UNKNOWN,
1176 CPU_AVX512_VBMI2_FLAGS, 0 },
8cfcb765
IT
1177 { STRING_COMMA_LEN (".avx512_vnni"), PROCESSOR_UNKNOWN,
1178 CPU_AVX512_VNNI_FLAGS, 0 },
ee6872be
IT
1179 { STRING_COMMA_LEN (".avx512_bitalg"), PROCESSOR_UNKNOWN,
1180 CPU_AVX512_BITALG_FLAGS, 0 },
029f3522 1181 { STRING_COMMA_LEN (".clzero"), PROCESSOR_UNKNOWN,
293f5f65 1182 CPU_CLZERO_FLAGS, 0 },
9916071f 1183 { STRING_COMMA_LEN (".mwaitx"), PROCESSOR_UNKNOWN,
293f5f65 1184 CPU_MWAITX_FLAGS, 0 },
8eab4136 1185 { STRING_COMMA_LEN (".ospke"), PROCESSOR_UNKNOWN,
293f5f65 1186 CPU_OSPKE_FLAGS, 0 },
8bc52696 1187 { STRING_COMMA_LEN (".rdpid"), PROCESSOR_UNKNOWN,
293f5f65 1188 CPU_RDPID_FLAGS, 0 },
6b40c462
L
1189 { STRING_COMMA_LEN (".ptwrite"), PROCESSOR_UNKNOWN,
1190 CPU_PTWRITE_FLAGS, 0 },
d777820b
IT
1191 { STRING_COMMA_LEN (".ibt"), PROCESSOR_UNKNOWN,
1192 CPU_IBT_FLAGS, 0 },
1193 { STRING_COMMA_LEN (".shstk"), PROCESSOR_UNKNOWN,
1194 CPU_SHSTK_FLAGS, 0 },
48521003
IT
1195 { STRING_COMMA_LEN (".gfni"), PROCESSOR_UNKNOWN,
1196 CPU_GFNI_FLAGS, 0 },
8dcf1fad
IT
1197 { STRING_COMMA_LEN (".vaes"), PROCESSOR_UNKNOWN,
1198 CPU_VAES_FLAGS, 0 },
ff1982d5
IT
1199 { STRING_COMMA_LEN (".vpclmulqdq"), PROCESSOR_UNKNOWN,
1200 CPU_VPCLMULQDQ_FLAGS, 0 },
3233d7d0
IT
1201 { STRING_COMMA_LEN (".wbnoinvd"), PROCESSOR_UNKNOWN,
1202 CPU_WBNOINVD_FLAGS, 0 },
be3a8dca
IT
1203 { STRING_COMMA_LEN (".pconfig"), PROCESSOR_UNKNOWN,
1204 CPU_PCONFIG_FLAGS, 0 },
de89d0a3
IT
1205 { STRING_COMMA_LEN (".waitpkg"), PROCESSOR_UNKNOWN,
1206 CPU_WAITPKG_FLAGS, 0 },
c48935d7
IT
1207 { STRING_COMMA_LEN (".cldemote"), PROCESSOR_UNKNOWN,
1208 CPU_CLDEMOTE_FLAGS, 0 },
260cd341
LC
1209 { STRING_COMMA_LEN (".amx_int8"), PROCESSOR_UNKNOWN,
1210 CPU_AMX_INT8_FLAGS, 0 },
1211 { STRING_COMMA_LEN (".amx_bf16"), PROCESSOR_UNKNOWN,
1212 CPU_AMX_BF16_FLAGS, 0 },
1213 { STRING_COMMA_LEN (".amx_tile"), PROCESSOR_UNKNOWN,
1214 CPU_AMX_TILE_FLAGS, 0 },
c0a30a9f
L
1215 { STRING_COMMA_LEN (".movdiri"), PROCESSOR_UNKNOWN,
1216 CPU_MOVDIRI_FLAGS, 0 },
1217 { STRING_COMMA_LEN (".movdir64b"), PROCESSOR_UNKNOWN,
1218 CPU_MOVDIR64B_FLAGS, 0 },
d6aab7a1
XG
1219 { STRING_COMMA_LEN (".avx512_bf16"), PROCESSOR_UNKNOWN,
1220 CPU_AVX512_BF16_FLAGS, 0 },
9186c494
L
1221 { STRING_COMMA_LEN (".avx512_vp2intersect"), PROCESSOR_UNKNOWN,
1222 CPU_AVX512_VP2INTERSECT_FLAGS, 0 },
dd455cf5
L
1223 { STRING_COMMA_LEN (".enqcmd"), PROCESSOR_UNKNOWN,
1224 CPU_ENQCMD_FLAGS, 0 },
4b27d27c
L
1225 { STRING_COMMA_LEN (".serialize"), PROCESSOR_UNKNOWN,
1226 CPU_SERIALIZE_FLAGS, 0 },
142861df
JB
1227 { STRING_COMMA_LEN (".rdpru"), PROCESSOR_UNKNOWN,
1228 CPU_RDPRU_FLAGS, 0 },
1229 { STRING_COMMA_LEN (".mcommit"), PROCESSOR_UNKNOWN,
1230 CPU_MCOMMIT_FLAGS, 0 },
a847e322
JB
1231 { STRING_COMMA_LEN (".sev_es"), PROCESSOR_UNKNOWN,
1232 CPU_SEV_ES_FLAGS, 0 },
bb651e8b
CL
1233 { STRING_COMMA_LEN (".tsxldtrk"), PROCESSOR_UNKNOWN,
1234 CPU_TSXLDTRK_FLAGS, 0 },
c4694f17
TG
1235 { STRING_COMMA_LEN (".kl"), PROCESSOR_UNKNOWN,
1236 CPU_KL_FLAGS, 0 },
1237 { STRING_COMMA_LEN (".widekl"), PROCESSOR_UNKNOWN,
1238 CPU_WIDEKL_FLAGS, 0 },
293f5f65
L
1239};
1240
1241static const noarch_entry cpu_noarch[] =
1242{
1243 { STRING_COMMA_LEN ("no87"), CPU_ANY_X87_FLAGS },
1848e567
L
1244 { STRING_COMMA_LEN ("no287"), CPU_ANY_287_FLAGS },
1245 { STRING_COMMA_LEN ("no387"), CPU_ANY_387_FLAGS },
1246 { STRING_COMMA_LEN ("no687"), CPU_ANY_687_FLAGS },
d871f3f4
L
1247 { STRING_COMMA_LEN ("nocmov"), CPU_ANY_CMOV_FLAGS },
1248 { STRING_COMMA_LEN ("nofxsr"), CPU_ANY_FXSR_FLAGS },
293f5f65
L
1249 { STRING_COMMA_LEN ("nommx"), CPU_ANY_MMX_FLAGS },
1250 { STRING_COMMA_LEN ("nosse"), CPU_ANY_SSE_FLAGS },
1848e567
L
1251 { STRING_COMMA_LEN ("nosse2"), CPU_ANY_SSE2_FLAGS },
1252 { STRING_COMMA_LEN ("nosse3"), CPU_ANY_SSE3_FLAGS },
af5c13b0 1253 { STRING_COMMA_LEN ("nosse4a"), CPU_ANY_SSE4A_FLAGS },
1848e567
L
1254 { STRING_COMMA_LEN ("nossse3"), CPU_ANY_SSSE3_FLAGS },
1255 { STRING_COMMA_LEN ("nosse4.1"), CPU_ANY_SSE4_1_FLAGS },
1256 { STRING_COMMA_LEN ("nosse4.2"), CPU_ANY_SSE4_2_FLAGS },
af5c13b0 1257 { STRING_COMMA_LEN ("nosse4"), CPU_ANY_SSE4_1_FLAGS },
293f5f65 1258 { STRING_COMMA_LEN ("noavx"), CPU_ANY_AVX_FLAGS },
1848e567 1259 { STRING_COMMA_LEN ("noavx2"), CPU_ANY_AVX2_FLAGS },
144b71e2
L
1260 { STRING_COMMA_LEN ("noavx512f"), CPU_ANY_AVX512F_FLAGS },
1261 { STRING_COMMA_LEN ("noavx512cd"), CPU_ANY_AVX512CD_FLAGS },
1262 { STRING_COMMA_LEN ("noavx512er"), CPU_ANY_AVX512ER_FLAGS },
1263 { STRING_COMMA_LEN ("noavx512pf"), CPU_ANY_AVX512PF_FLAGS },
1264 { STRING_COMMA_LEN ("noavx512dq"), CPU_ANY_AVX512DQ_FLAGS },
1265 { STRING_COMMA_LEN ("noavx512bw"), CPU_ANY_AVX512BW_FLAGS },
1266 { STRING_COMMA_LEN ("noavx512vl"), CPU_ANY_AVX512VL_FLAGS },
1267 { STRING_COMMA_LEN ("noavx512ifma"), CPU_ANY_AVX512IFMA_FLAGS },
1268 { STRING_COMMA_LEN ("noavx512vbmi"), CPU_ANY_AVX512VBMI_FLAGS },
920d2ddc 1269 { STRING_COMMA_LEN ("noavx512_4fmaps"), CPU_ANY_AVX512_4FMAPS_FLAGS },
47acf0bd 1270 { STRING_COMMA_LEN ("noavx512_4vnniw"), CPU_ANY_AVX512_4VNNIW_FLAGS },
620214f7 1271 { STRING_COMMA_LEN ("noavx512_vpopcntdq"), CPU_ANY_AVX512_VPOPCNTDQ_FLAGS },
53467f57 1272 { STRING_COMMA_LEN ("noavx512_vbmi2"), CPU_ANY_AVX512_VBMI2_FLAGS },
8cfcb765 1273 { STRING_COMMA_LEN ("noavx512_vnni"), CPU_ANY_AVX512_VNNI_FLAGS },
ee6872be 1274 { STRING_COMMA_LEN ("noavx512_bitalg"), CPU_ANY_AVX512_BITALG_FLAGS },
d777820b
IT
1275 { STRING_COMMA_LEN ("noibt"), CPU_ANY_IBT_FLAGS },
1276 { STRING_COMMA_LEN ("noshstk"), CPU_ANY_SHSTK_FLAGS },
260cd341
LC
1277 { STRING_COMMA_LEN ("noamx_int8"), CPU_ANY_AMX_INT8_FLAGS },
1278 { STRING_COMMA_LEN ("noamx_bf16"), CPU_ANY_AMX_BF16_FLAGS },
1279 { STRING_COMMA_LEN ("noamx_tile"), CPU_ANY_AMX_TILE_FLAGS },
c0a30a9f
L
1280 { STRING_COMMA_LEN ("nomovdiri"), CPU_ANY_MOVDIRI_FLAGS },
1281 { STRING_COMMA_LEN ("nomovdir64b"), CPU_ANY_MOVDIR64B_FLAGS },
d6aab7a1 1282 { STRING_COMMA_LEN ("noavx512_bf16"), CPU_ANY_AVX512_BF16_FLAGS },
708a2fff
CL
1283 { STRING_COMMA_LEN ("noavx512_vp2intersect"),
1284 CPU_ANY_AVX512_VP2INTERSECT_FLAGS },
dd455cf5 1285 { STRING_COMMA_LEN ("noenqcmd"), CPU_ANY_ENQCMD_FLAGS },
4b27d27c 1286 { STRING_COMMA_LEN ("noserialize"), CPU_ANY_SERIALIZE_FLAGS },
bb651e8b 1287 { STRING_COMMA_LEN ("notsxldtrk"), CPU_ANY_TSXLDTRK_FLAGS },
c4694f17
TG
1288 { STRING_COMMA_LEN ("nokl"), CPU_ANY_KL_FLAGS },
1289 { STRING_COMMA_LEN ("nowidekl"), CPU_ANY_WIDEKL_FLAGS },
e413e4e9
AM
1290};
1291
704209c0 1292#ifdef I386COFF
a6c24e68
NC
1293/* Like s_lcomm_internal in gas/read.c but the alignment string
1294 is allowed to be optional. */
1295
1296static symbolS *
1297pe_lcomm_internal (int needs_align, symbolS *symbolP, addressT size)
1298{
1299 addressT align = 0;
1300
1301 SKIP_WHITESPACE ();
1302
7ab9ffdd 1303 if (needs_align
a6c24e68
NC
1304 && *input_line_pointer == ',')
1305 {
1306 align = parse_align (needs_align - 1);
7ab9ffdd 1307
a6c24e68
NC
1308 if (align == (addressT) -1)
1309 return NULL;
1310 }
1311 else
1312 {
1313 if (size >= 8)
1314 align = 3;
1315 else if (size >= 4)
1316 align = 2;
1317 else if (size >= 2)
1318 align = 1;
1319 else
1320 align = 0;
1321 }
1322
1323 bss_alloc (symbolP, size, align);
1324 return symbolP;
1325}
1326
704209c0 1327static void
a6c24e68
NC
1328pe_lcomm (int needs_align)
1329{
1330 s_comm_internal (needs_align * 2, pe_lcomm_internal);
1331}
704209c0 1332#endif
a6c24e68 1333
29b0f896
AM
1334const pseudo_typeS md_pseudo_table[] =
1335{
1336#if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
1337 {"align", s_align_bytes, 0},
1338#else
1339 {"align", s_align_ptwo, 0},
1340#endif
1341 {"arch", set_cpu_arch, 0},
1342#ifndef I386COFF
1343 {"bss", s_bss, 0},
a6c24e68
NC
1344#else
1345 {"lcomm", pe_lcomm, 1},
29b0f896
AM
1346#endif
1347 {"ffloat", float_cons, 'f'},
1348 {"dfloat", float_cons, 'd'},
1349 {"tfloat", float_cons, 'x'},
1350 {"value", cons, 2},
d182319b 1351 {"slong", signed_cons, 4},
29b0f896
AM
1352 {"noopt", s_ignore, 0},
1353 {"optim", s_ignore, 0},
1354 {"code16gcc", set_16bit_gcc_code_flag, CODE_16BIT},
1355 {"code16", set_code_flag, CODE_16BIT},
1356 {"code32", set_code_flag, CODE_32BIT},
da5f19a2 1357#ifdef BFD64
29b0f896 1358 {"code64", set_code_flag, CODE_64BIT},
da5f19a2 1359#endif
29b0f896
AM
1360 {"intel_syntax", set_intel_syntax, 1},
1361 {"att_syntax", set_intel_syntax, 0},
1efbbeb4
L
1362 {"intel_mnemonic", set_intel_mnemonic, 1},
1363 {"att_mnemonic", set_intel_mnemonic, 0},
db51cc60
L
1364 {"allow_index_reg", set_allow_index_reg, 1},
1365 {"disallow_index_reg", set_allow_index_reg, 0},
7bab8ab5
JB
1366 {"sse_check", set_check, 0},
1367 {"operand_check", set_check, 1},
3b22753a
L
1368#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1369 {"largecomm", handle_large_common, 0},
07a53e5c 1370#else
68d20676 1371 {"file", dwarf2_directive_file, 0},
07a53e5c
RH
1372 {"loc", dwarf2_directive_loc, 0},
1373 {"loc_mark_labels", dwarf2_directive_loc_mark_labels, 0},
3b22753a 1374#endif
6482c264
NC
1375#ifdef TE_PE
1376 {"secrel32", pe_directive_secrel, 0},
1377#endif
29b0f896
AM
1378 {0, 0, 0}
1379};
1380
1381/* For interface with expression (). */
1382extern char *input_line_pointer;
1383
1384/* Hash table for instruction mnemonic lookup. */
629310ab 1385static htab_t op_hash;
29b0f896
AM
1386
1387/* Hash table for register lookup. */
629310ab 1388static htab_t reg_hash;
29b0f896 1389\f
ce8a8b2f
AM
1390 /* Various efficient no-op patterns for aligning code labels.
1391 Note: Don't try to assemble the instructions in the comments.
1392 0L and 0w are not legal. */
62a02d25
L
1393static const unsigned char f32_1[] =
1394 {0x90}; /* nop */
1395static const unsigned char f32_2[] =
1396 {0x66,0x90}; /* xchg %ax,%ax */
1397static const unsigned char f32_3[] =
1398 {0x8d,0x76,0x00}; /* leal 0(%esi),%esi */
1399static const unsigned char f32_4[] =
1400 {0x8d,0x74,0x26,0x00}; /* leal 0(%esi,1),%esi */
62a02d25
L
1401static const unsigned char f32_6[] =
1402 {0x8d,0xb6,0x00,0x00,0x00,0x00}; /* leal 0L(%esi),%esi */
1403static const unsigned char f32_7[] =
1404 {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal 0L(%esi,1),%esi */
62a02d25 1405static const unsigned char f16_3[] =
3ae729d5 1406 {0x8d,0x74,0x00}; /* lea 0(%si),%si */
62a02d25 1407static const unsigned char f16_4[] =
3ae729d5
L
1408 {0x8d,0xb4,0x00,0x00}; /* lea 0W(%si),%si */
1409static const unsigned char jump_disp8[] =
1410 {0xeb}; /* jmp disp8 */
1411static const unsigned char jump32_disp32[] =
1412 {0xe9}; /* jmp disp32 */
1413static const unsigned char jump16_disp32[] =
1414 {0x66,0xe9}; /* jmp disp32 */
62a02d25
L
1415/* 32-bit NOPs patterns. */
1416static const unsigned char *const f32_patt[] = {
3ae729d5 1417 f32_1, f32_2, f32_3, f32_4, NULL, f32_6, f32_7
62a02d25
L
1418};
1419/* 16-bit NOPs patterns. */
1420static const unsigned char *const f16_patt[] = {
3ae729d5 1421 f32_1, f32_2, f16_3, f16_4
62a02d25
L
1422};
1423/* nopl (%[re]ax) */
1424static const unsigned char alt_3[] =
1425 {0x0f,0x1f,0x00};
1426/* nopl 0(%[re]ax) */
1427static const unsigned char alt_4[] =
1428 {0x0f,0x1f,0x40,0x00};
1429/* nopl 0(%[re]ax,%[re]ax,1) */
1430static const unsigned char alt_5[] =
1431 {0x0f,0x1f,0x44,0x00,0x00};
1432/* nopw 0(%[re]ax,%[re]ax,1) */
1433static const unsigned char alt_6[] =
1434 {0x66,0x0f,0x1f,0x44,0x00,0x00};
1435/* nopl 0L(%[re]ax) */
1436static const unsigned char alt_7[] =
1437 {0x0f,0x1f,0x80,0x00,0x00,0x00,0x00};
1438/* nopl 0L(%[re]ax,%[re]ax,1) */
1439static const unsigned char alt_8[] =
1440 {0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1441/* nopw 0L(%[re]ax,%[re]ax,1) */
1442static const unsigned char alt_9[] =
1443 {0x66,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1444/* nopw %cs:0L(%[re]ax,%[re]ax,1) */
1445static const unsigned char alt_10[] =
1446 {0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
3ae729d5
L
1447/* data16 nopw %cs:0L(%eax,%eax,1) */
1448static const unsigned char alt_11[] =
1449 {0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
62a02d25
L
1450/* 32-bit and 64-bit NOPs patterns. */
1451static const unsigned char *const alt_patt[] = {
1452 f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
3ae729d5 1453 alt_9, alt_10, alt_11
62a02d25
L
1454};
1455
1456/* Genenerate COUNT bytes of NOPs to WHERE from PATT with the maximum
1457 size of a single NOP instruction MAX_SINGLE_NOP_SIZE. */
1458
1459static void
1460i386_output_nops (char *where, const unsigned char *const *patt,
1461 int count, int max_single_nop_size)
1462
1463{
3ae729d5
L
1464 /* Place the longer NOP first. */
1465 int last;
1466 int offset;
3076e594
NC
1467 const unsigned char *nops;
1468
1469 if (max_single_nop_size < 1)
1470 {
1471 as_fatal (_("i386_output_nops called to generate nops of at most %d bytes!"),
1472 max_single_nop_size);
1473 return;
1474 }
1475
1476 nops = patt[max_single_nop_size - 1];
3ae729d5
L
1477
1478 /* Use the smaller one if the requsted one isn't available. */
1479 if (nops == NULL)
62a02d25 1480 {
3ae729d5
L
1481 max_single_nop_size--;
1482 nops = patt[max_single_nop_size - 1];
62a02d25
L
1483 }
1484
3ae729d5
L
1485 last = count % max_single_nop_size;
1486
1487 count -= last;
1488 for (offset = 0; offset < count; offset += max_single_nop_size)
1489 memcpy (where + offset, nops, max_single_nop_size);
1490
1491 if (last)
1492 {
1493 nops = patt[last - 1];
1494 if (nops == NULL)
1495 {
1496 /* Use the smaller one plus one-byte NOP if the needed one
1497 isn't available. */
1498 last--;
1499 nops = patt[last - 1];
1500 memcpy (where + offset, nops, last);
1501 where[offset + last] = *patt[0];
1502 }
1503 else
1504 memcpy (where + offset, nops, last);
1505 }
62a02d25
L
1506}
1507
3ae729d5
L
1508static INLINE int
1509fits_in_imm7 (offsetT num)
1510{
1511 return (num & 0x7f) == num;
1512}
1513
1514static INLINE int
1515fits_in_imm31 (offsetT num)
1516{
1517 return (num & 0x7fffffff) == num;
1518}
62a02d25
L
1519
1520/* Genenerate COUNT bytes of NOPs to WHERE with the maximum size of a
1521 single NOP instruction LIMIT. */
1522
1523void
3ae729d5 1524i386_generate_nops (fragS *fragP, char *where, offsetT count, int limit)
62a02d25 1525{
3ae729d5 1526 const unsigned char *const *patt = NULL;
62a02d25 1527 int max_single_nop_size;
3ae729d5
L
1528 /* Maximum number of NOPs before switching to jump over NOPs. */
1529 int max_number_of_nops;
62a02d25 1530
3ae729d5 1531 switch (fragP->fr_type)
62a02d25 1532 {
3ae729d5
L
1533 case rs_fill_nop:
1534 case rs_align_code:
1535 break;
e379e5f3
L
1536 case rs_machine_dependent:
1537 /* Allow NOP padding for jumps and calls. */
1538 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
1539 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
1540 break;
1541 /* Fall through. */
3ae729d5 1542 default:
62a02d25
L
1543 return;
1544 }
1545
ccc9c027
L
1546 /* We need to decide which NOP sequence to use for 32bit and
1547 64bit. When -mtune= is used:
4eed87de 1548
76bc74dc
L
1549 1. For PROCESSOR_I386, PROCESSOR_I486, PROCESSOR_PENTIUM and
1550 PROCESSOR_GENERIC32, f32_patt will be used.
80b8656c
L
1551 2. For the rest, alt_patt will be used.
1552
1553 When -mtune= isn't used, alt_patt will be used if
22109423 1554 cpu_arch_isa_flags has CpuNop. Otherwise, f32_patt will
76bc74dc 1555 be used.
ccc9c027
L
1556
1557 When -march= or .arch is used, we can't use anything beyond
1558 cpu_arch_isa_flags. */
1559
1560 if (flag_code == CODE_16BIT)
1561 {
3ae729d5
L
1562 patt = f16_patt;
1563 max_single_nop_size = sizeof (f16_patt) / sizeof (f16_patt[0]);
1564 /* Limit number of NOPs to 2 in 16-bit mode. */
1565 max_number_of_nops = 2;
252b5132 1566 }
33fef721 1567 else
ccc9c027 1568 {
fbf3f584 1569 if (fragP->tc_frag_data.isa == PROCESSOR_UNKNOWN)
ccc9c027
L
1570 {
1571 /* PROCESSOR_UNKNOWN means that all ISAs may be used. */
1572 switch (cpu_arch_tune)
1573 {
1574 case PROCESSOR_UNKNOWN:
1575 /* We use cpu_arch_isa_flags to check if we SHOULD
22109423
L
1576 optimize with nops. */
1577 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1578 patt = alt_patt;
ccc9c027
L
1579 else
1580 patt = f32_patt;
1581 break;
ccc9c027
L
1582 case PROCESSOR_PENTIUM4:
1583 case PROCESSOR_NOCONA:
ef05d495 1584 case PROCESSOR_CORE:
76bc74dc 1585 case PROCESSOR_CORE2:
bd5295b2 1586 case PROCESSOR_COREI7:
3632d14b 1587 case PROCESSOR_L1OM:
7a9068fe 1588 case PROCESSOR_K1OM:
76bc74dc 1589 case PROCESSOR_GENERIC64:
ccc9c027
L
1590 case PROCESSOR_K6:
1591 case PROCESSOR_ATHLON:
1592 case PROCESSOR_K8:
4eed87de 1593 case PROCESSOR_AMDFAM10:
8aedb9fe 1594 case PROCESSOR_BD:
029f3522 1595 case PROCESSOR_ZNVER:
7b458c12 1596 case PROCESSOR_BT:
80b8656c 1597 patt = alt_patt;
ccc9c027 1598 break;
76bc74dc 1599 case PROCESSOR_I386:
ccc9c027
L
1600 case PROCESSOR_I486:
1601 case PROCESSOR_PENTIUM:
2dde1948 1602 case PROCESSOR_PENTIUMPRO:
81486035 1603 case PROCESSOR_IAMCU:
ccc9c027
L
1604 case PROCESSOR_GENERIC32:
1605 patt = f32_patt;
1606 break;
4eed87de 1607 }
ccc9c027
L
1608 }
1609 else
1610 {
fbf3f584 1611 switch (fragP->tc_frag_data.tune)
ccc9c027
L
1612 {
1613 case PROCESSOR_UNKNOWN:
e6a14101 1614 /* When cpu_arch_isa is set, cpu_arch_tune shouldn't be
ccc9c027
L
1615 PROCESSOR_UNKNOWN. */
1616 abort ();
1617 break;
1618
76bc74dc 1619 case PROCESSOR_I386:
ccc9c027
L
1620 case PROCESSOR_I486:
1621 case PROCESSOR_PENTIUM:
81486035 1622 case PROCESSOR_IAMCU:
ccc9c027
L
1623 case PROCESSOR_K6:
1624 case PROCESSOR_ATHLON:
1625 case PROCESSOR_K8:
4eed87de 1626 case PROCESSOR_AMDFAM10:
8aedb9fe 1627 case PROCESSOR_BD:
029f3522 1628 case PROCESSOR_ZNVER:
7b458c12 1629 case PROCESSOR_BT:
ccc9c027
L
1630 case PROCESSOR_GENERIC32:
1631 /* We use cpu_arch_isa_flags to check if we CAN optimize
22109423
L
1632 with nops. */
1633 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1634 patt = alt_patt;
ccc9c027
L
1635 else
1636 patt = f32_patt;
1637 break;
76bc74dc
L
1638 case PROCESSOR_PENTIUMPRO:
1639 case PROCESSOR_PENTIUM4:
1640 case PROCESSOR_NOCONA:
1641 case PROCESSOR_CORE:
ef05d495 1642 case PROCESSOR_CORE2:
bd5295b2 1643 case PROCESSOR_COREI7:
3632d14b 1644 case PROCESSOR_L1OM:
7a9068fe 1645 case PROCESSOR_K1OM:
22109423 1646 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1647 patt = alt_patt;
ccc9c027
L
1648 else
1649 patt = f32_patt;
1650 break;
1651 case PROCESSOR_GENERIC64:
80b8656c 1652 patt = alt_patt;
ccc9c027 1653 break;
4eed87de 1654 }
ccc9c027
L
1655 }
1656
76bc74dc
L
1657 if (patt == f32_patt)
1658 {
3ae729d5
L
1659 max_single_nop_size = sizeof (f32_patt) / sizeof (f32_patt[0]);
1660 /* Limit number of NOPs to 2 for older processors. */
1661 max_number_of_nops = 2;
76bc74dc
L
1662 }
1663 else
1664 {
3ae729d5
L
1665 max_single_nop_size = sizeof (alt_patt) / sizeof (alt_patt[0]);
1666 /* Limit number of NOPs to 7 for newer processors. */
1667 max_number_of_nops = 7;
1668 }
1669 }
1670
1671 if (limit == 0)
1672 limit = max_single_nop_size;
1673
1674 if (fragP->fr_type == rs_fill_nop)
1675 {
1676 /* Output NOPs for .nop directive. */
1677 if (limit > max_single_nop_size)
1678 {
1679 as_bad_where (fragP->fr_file, fragP->fr_line,
1680 _("invalid single nop size: %d "
1681 "(expect within [0, %d])"),
1682 limit, max_single_nop_size);
1683 return;
1684 }
1685 }
e379e5f3 1686 else if (fragP->fr_type != rs_machine_dependent)
3ae729d5
L
1687 fragP->fr_var = count;
1688
1689 if ((count / max_single_nop_size) > max_number_of_nops)
1690 {
1691 /* Generate jump over NOPs. */
1692 offsetT disp = count - 2;
1693 if (fits_in_imm7 (disp))
1694 {
1695 /* Use "jmp disp8" if possible. */
1696 count = disp;
1697 where[0] = jump_disp8[0];
1698 where[1] = count;
1699 where += 2;
1700 }
1701 else
1702 {
1703 unsigned int size_of_jump;
1704
1705 if (flag_code == CODE_16BIT)
1706 {
1707 where[0] = jump16_disp32[0];
1708 where[1] = jump16_disp32[1];
1709 size_of_jump = 2;
1710 }
1711 else
1712 {
1713 where[0] = jump32_disp32[0];
1714 size_of_jump = 1;
1715 }
1716
1717 count -= size_of_jump + 4;
1718 if (!fits_in_imm31 (count))
1719 {
1720 as_bad_where (fragP->fr_file, fragP->fr_line,
1721 _("jump over nop padding out of range"));
1722 return;
1723 }
1724
1725 md_number_to_chars (where + size_of_jump, count, 4);
1726 where += size_of_jump + 4;
76bc74dc 1727 }
ccc9c027 1728 }
3ae729d5
L
1729
1730 /* Generate multiple NOPs. */
1731 i386_output_nops (where, patt, count, limit);
252b5132
RH
1732}
1733
c6fb90c8 1734static INLINE int
0dfbf9d7 1735operand_type_all_zero (const union i386_operand_type *x)
40fb9820 1736{
0dfbf9d7 1737 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1738 {
1739 case 3:
0dfbf9d7 1740 if (x->array[2])
c6fb90c8 1741 return 0;
1a0670f3 1742 /* Fall through. */
c6fb90c8 1743 case 2:
0dfbf9d7 1744 if (x->array[1])
c6fb90c8 1745 return 0;
1a0670f3 1746 /* Fall through. */
c6fb90c8 1747 case 1:
0dfbf9d7 1748 return !x->array[0];
c6fb90c8
L
1749 default:
1750 abort ();
1751 }
40fb9820
L
1752}
1753
c6fb90c8 1754static INLINE void
0dfbf9d7 1755operand_type_set (union i386_operand_type *x, unsigned int v)
40fb9820 1756{
0dfbf9d7 1757 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1758 {
1759 case 3:
0dfbf9d7 1760 x->array[2] = v;
1a0670f3 1761 /* Fall through. */
c6fb90c8 1762 case 2:
0dfbf9d7 1763 x->array[1] = v;
1a0670f3 1764 /* Fall through. */
c6fb90c8 1765 case 1:
0dfbf9d7 1766 x->array[0] = v;
1a0670f3 1767 /* Fall through. */
c6fb90c8
L
1768 break;
1769 default:
1770 abort ();
1771 }
bab6aec1
JB
1772
1773 x->bitfield.class = ClassNone;
75e5731b 1774 x->bitfield.instance = InstanceNone;
c6fb90c8 1775}
40fb9820 1776
c6fb90c8 1777static INLINE int
0dfbf9d7
L
1778operand_type_equal (const union i386_operand_type *x,
1779 const union i386_operand_type *y)
c6fb90c8 1780{
0dfbf9d7 1781 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1782 {
1783 case 3:
0dfbf9d7 1784 if (x->array[2] != y->array[2])
c6fb90c8 1785 return 0;
1a0670f3 1786 /* Fall through. */
c6fb90c8 1787 case 2:
0dfbf9d7 1788 if (x->array[1] != y->array[1])
c6fb90c8 1789 return 0;
1a0670f3 1790 /* Fall through. */
c6fb90c8 1791 case 1:
0dfbf9d7 1792 return x->array[0] == y->array[0];
c6fb90c8
L
1793 break;
1794 default:
1795 abort ();
1796 }
1797}
40fb9820 1798
0dfbf9d7
L
1799static INLINE int
1800cpu_flags_all_zero (const union i386_cpu_flags *x)
1801{
1802 switch (ARRAY_SIZE(x->array))
1803 {
53467f57
IT
1804 case 4:
1805 if (x->array[3])
1806 return 0;
1807 /* Fall through. */
0dfbf9d7
L
1808 case 3:
1809 if (x->array[2])
1810 return 0;
1a0670f3 1811 /* Fall through. */
0dfbf9d7
L
1812 case 2:
1813 if (x->array[1])
1814 return 0;
1a0670f3 1815 /* Fall through. */
0dfbf9d7
L
1816 case 1:
1817 return !x->array[0];
1818 default:
1819 abort ();
1820 }
1821}
1822
0dfbf9d7
L
1823static INLINE int
1824cpu_flags_equal (const union i386_cpu_flags *x,
1825 const union i386_cpu_flags *y)
1826{
1827 switch (ARRAY_SIZE(x->array))
1828 {
53467f57
IT
1829 case 4:
1830 if (x->array[3] != y->array[3])
1831 return 0;
1832 /* Fall through. */
0dfbf9d7
L
1833 case 3:
1834 if (x->array[2] != y->array[2])
1835 return 0;
1a0670f3 1836 /* Fall through. */
0dfbf9d7
L
1837 case 2:
1838 if (x->array[1] != y->array[1])
1839 return 0;
1a0670f3 1840 /* Fall through. */
0dfbf9d7
L
1841 case 1:
1842 return x->array[0] == y->array[0];
1843 break;
1844 default:
1845 abort ();
1846 }
1847}
c6fb90c8
L
1848
1849static INLINE int
1850cpu_flags_check_cpu64 (i386_cpu_flags f)
1851{
1852 return !((flag_code == CODE_64BIT && f.bitfield.cpuno64)
1853 || (flag_code != CODE_64BIT && f.bitfield.cpu64));
40fb9820
L
1854}
1855
c6fb90c8
L
1856static INLINE i386_cpu_flags
1857cpu_flags_and (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1858{
c6fb90c8
L
1859 switch (ARRAY_SIZE (x.array))
1860 {
53467f57
IT
1861 case 4:
1862 x.array [3] &= y.array [3];
1863 /* Fall through. */
c6fb90c8
L
1864 case 3:
1865 x.array [2] &= y.array [2];
1a0670f3 1866 /* Fall through. */
c6fb90c8
L
1867 case 2:
1868 x.array [1] &= y.array [1];
1a0670f3 1869 /* Fall through. */
c6fb90c8
L
1870 case 1:
1871 x.array [0] &= y.array [0];
1872 break;
1873 default:
1874 abort ();
1875 }
1876 return x;
1877}
40fb9820 1878
c6fb90c8
L
1879static INLINE i386_cpu_flags
1880cpu_flags_or (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1881{
c6fb90c8 1882 switch (ARRAY_SIZE (x.array))
40fb9820 1883 {
53467f57
IT
1884 case 4:
1885 x.array [3] |= y.array [3];
1886 /* Fall through. */
c6fb90c8
L
1887 case 3:
1888 x.array [2] |= y.array [2];
1a0670f3 1889 /* Fall through. */
c6fb90c8
L
1890 case 2:
1891 x.array [1] |= y.array [1];
1a0670f3 1892 /* Fall through. */
c6fb90c8
L
1893 case 1:
1894 x.array [0] |= y.array [0];
40fb9820
L
1895 break;
1896 default:
1897 abort ();
1898 }
40fb9820
L
1899 return x;
1900}
1901
309d3373
JB
1902static INLINE i386_cpu_flags
1903cpu_flags_and_not (i386_cpu_flags x, i386_cpu_flags y)
1904{
1905 switch (ARRAY_SIZE (x.array))
1906 {
53467f57
IT
1907 case 4:
1908 x.array [3] &= ~y.array [3];
1909 /* Fall through. */
309d3373
JB
1910 case 3:
1911 x.array [2] &= ~y.array [2];
1a0670f3 1912 /* Fall through. */
309d3373
JB
1913 case 2:
1914 x.array [1] &= ~y.array [1];
1a0670f3 1915 /* Fall through. */
309d3373
JB
1916 case 1:
1917 x.array [0] &= ~y.array [0];
1918 break;
1919 default:
1920 abort ();
1921 }
1922 return x;
1923}
1924
6c0946d0
JB
1925static const i386_cpu_flags avx512 = CPU_ANY_AVX512F_FLAGS;
1926
c0f3af97
L
1927#define CPU_FLAGS_ARCH_MATCH 0x1
1928#define CPU_FLAGS_64BIT_MATCH 0x2
1929
c0f3af97 1930#define CPU_FLAGS_PERFECT_MATCH \
db12e14e 1931 (CPU_FLAGS_ARCH_MATCH | CPU_FLAGS_64BIT_MATCH)
c0f3af97
L
1932
1933/* Return CPU flags match bits. */
3629bb00 1934
40fb9820 1935static int
d3ce72d0 1936cpu_flags_match (const insn_template *t)
40fb9820 1937{
c0f3af97
L
1938 i386_cpu_flags x = t->cpu_flags;
1939 int match = cpu_flags_check_cpu64 (x) ? CPU_FLAGS_64BIT_MATCH : 0;
40fb9820
L
1940
1941 x.bitfield.cpu64 = 0;
1942 x.bitfield.cpuno64 = 0;
1943
0dfbf9d7 1944 if (cpu_flags_all_zero (&x))
c0f3af97
L
1945 {
1946 /* This instruction is available on all archs. */
db12e14e 1947 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 1948 }
3629bb00
L
1949 else
1950 {
c0f3af97 1951 /* This instruction is available only on some archs. */
3629bb00
L
1952 i386_cpu_flags cpu = cpu_arch_flags;
1953
ab592e75
JB
1954 /* AVX512VL is no standalone feature - match it and then strip it. */
1955 if (x.bitfield.cpuavx512vl && !cpu.bitfield.cpuavx512vl)
1956 return match;
1957 x.bitfield.cpuavx512vl = 0;
1958
3629bb00 1959 cpu = cpu_flags_and (x, cpu);
c0f3af97
L
1960 if (!cpu_flags_all_zero (&cpu))
1961 {
a5ff0eb2
L
1962 if (x.bitfield.cpuavx)
1963 {
929f69fa 1964 /* We need to check a few extra flags with AVX. */
b9d49817 1965 if (cpu.bitfield.cpuavx
40d231b4
JB
1966 && (!t->opcode_modifier.sse2avx
1967 || (sse2avx && !i.prefix[DATA_PREFIX]))
b9d49817 1968 && (!x.bitfield.cpuaes || cpu.bitfield.cpuaes)
929f69fa 1969 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
b9d49817
JB
1970 && (!x.bitfield.cpupclmul || cpu.bitfield.cpupclmul))
1971 match |= CPU_FLAGS_ARCH_MATCH;
a5ff0eb2 1972 }
929f69fa
JB
1973 else if (x.bitfield.cpuavx512f)
1974 {
1975 /* We need to check a few extra flags with AVX512F. */
1976 if (cpu.bitfield.cpuavx512f
1977 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
1978 && (!x.bitfield.cpuvaes || cpu.bitfield.cpuvaes)
1979 && (!x.bitfield.cpuvpclmulqdq || cpu.bitfield.cpuvpclmulqdq))
1980 match |= CPU_FLAGS_ARCH_MATCH;
1981 }
a5ff0eb2 1982 else
db12e14e 1983 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 1984 }
3629bb00 1985 }
c0f3af97 1986 return match;
40fb9820
L
1987}
1988
c6fb90c8
L
1989static INLINE i386_operand_type
1990operand_type_and (i386_operand_type x, i386_operand_type y)
40fb9820 1991{
bab6aec1
JB
1992 if (x.bitfield.class != y.bitfield.class)
1993 x.bitfield.class = ClassNone;
75e5731b
JB
1994 if (x.bitfield.instance != y.bitfield.instance)
1995 x.bitfield.instance = InstanceNone;
bab6aec1 1996
c6fb90c8
L
1997 switch (ARRAY_SIZE (x.array))
1998 {
1999 case 3:
2000 x.array [2] &= y.array [2];
1a0670f3 2001 /* Fall through. */
c6fb90c8
L
2002 case 2:
2003 x.array [1] &= y.array [1];
1a0670f3 2004 /* Fall through. */
c6fb90c8
L
2005 case 1:
2006 x.array [0] &= y.array [0];
2007 break;
2008 default:
2009 abort ();
2010 }
2011 return x;
40fb9820
L
2012}
2013
73053c1f
JB
2014static INLINE i386_operand_type
2015operand_type_and_not (i386_operand_type x, i386_operand_type y)
2016{
bab6aec1 2017 gas_assert (y.bitfield.class == ClassNone);
75e5731b 2018 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 2019
73053c1f
JB
2020 switch (ARRAY_SIZE (x.array))
2021 {
2022 case 3:
2023 x.array [2] &= ~y.array [2];
2024 /* Fall through. */
2025 case 2:
2026 x.array [1] &= ~y.array [1];
2027 /* Fall through. */
2028 case 1:
2029 x.array [0] &= ~y.array [0];
2030 break;
2031 default:
2032 abort ();
2033 }
2034 return x;
2035}
2036
c6fb90c8
L
2037static INLINE i386_operand_type
2038operand_type_or (i386_operand_type x, i386_operand_type y)
40fb9820 2039{
bab6aec1
JB
2040 gas_assert (x.bitfield.class == ClassNone ||
2041 y.bitfield.class == ClassNone ||
2042 x.bitfield.class == y.bitfield.class);
75e5731b
JB
2043 gas_assert (x.bitfield.instance == InstanceNone ||
2044 y.bitfield.instance == InstanceNone ||
2045 x.bitfield.instance == y.bitfield.instance);
bab6aec1 2046
c6fb90c8 2047 switch (ARRAY_SIZE (x.array))
40fb9820 2048 {
c6fb90c8
L
2049 case 3:
2050 x.array [2] |= y.array [2];
1a0670f3 2051 /* Fall through. */
c6fb90c8
L
2052 case 2:
2053 x.array [1] |= y.array [1];
1a0670f3 2054 /* Fall through. */
c6fb90c8
L
2055 case 1:
2056 x.array [0] |= y.array [0];
40fb9820
L
2057 break;
2058 default:
2059 abort ();
2060 }
c6fb90c8
L
2061 return x;
2062}
40fb9820 2063
c6fb90c8
L
2064static INLINE i386_operand_type
2065operand_type_xor (i386_operand_type x, i386_operand_type y)
2066{
bab6aec1 2067 gas_assert (y.bitfield.class == ClassNone);
75e5731b 2068 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 2069
c6fb90c8
L
2070 switch (ARRAY_SIZE (x.array))
2071 {
2072 case 3:
2073 x.array [2] ^= y.array [2];
1a0670f3 2074 /* Fall through. */
c6fb90c8
L
2075 case 2:
2076 x.array [1] ^= y.array [1];
1a0670f3 2077 /* Fall through. */
c6fb90c8
L
2078 case 1:
2079 x.array [0] ^= y.array [0];
2080 break;
2081 default:
2082 abort ();
2083 }
40fb9820
L
2084 return x;
2085}
2086
40fb9820
L
2087static const i386_operand_type disp16 = OPERAND_TYPE_DISP16;
2088static const i386_operand_type disp32 = OPERAND_TYPE_DISP32;
2089static const i386_operand_type disp32s = OPERAND_TYPE_DISP32S;
2090static const i386_operand_type disp16_32 = OPERAND_TYPE_DISP16_32;
bab6aec1
JB
2091static const i386_operand_type anydisp = OPERAND_TYPE_ANYDISP;
2092static const i386_operand_type anyimm = OPERAND_TYPE_ANYIMM;
40fb9820 2093static const i386_operand_type regxmm = OPERAND_TYPE_REGXMM;
43234a1e 2094static const i386_operand_type regmask = OPERAND_TYPE_REGMASK;
40fb9820
L
2095static const i386_operand_type imm8 = OPERAND_TYPE_IMM8;
2096static const i386_operand_type imm8s = OPERAND_TYPE_IMM8S;
2097static const i386_operand_type imm16 = OPERAND_TYPE_IMM16;
2098static const i386_operand_type imm32 = OPERAND_TYPE_IMM32;
2099static const i386_operand_type imm32s = OPERAND_TYPE_IMM32S;
2100static const i386_operand_type imm64 = OPERAND_TYPE_IMM64;
2101static const i386_operand_type imm16_32 = OPERAND_TYPE_IMM16_32;
2102static const i386_operand_type imm16_32s = OPERAND_TYPE_IMM16_32S;
2103static const i386_operand_type imm16_32_32s = OPERAND_TYPE_IMM16_32_32S;
2104
2105enum operand_type
2106{
2107 reg,
40fb9820
L
2108 imm,
2109 disp,
2110 anymem
2111};
2112
c6fb90c8 2113static INLINE int
40fb9820
L
2114operand_type_check (i386_operand_type t, enum operand_type c)
2115{
2116 switch (c)
2117 {
2118 case reg:
bab6aec1 2119 return t.bitfield.class == Reg;
40fb9820 2120
40fb9820
L
2121 case imm:
2122 return (t.bitfield.imm8
2123 || t.bitfield.imm8s
2124 || t.bitfield.imm16
2125 || t.bitfield.imm32
2126 || t.bitfield.imm32s
2127 || t.bitfield.imm64);
2128
2129 case disp:
2130 return (t.bitfield.disp8
2131 || t.bitfield.disp16
2132 || t.bitfield.disp32
2133 || t.bitfield.disp32s
2134 || t.bitfield.disp64);
2135
2136 case anymem:
2137 return (t.bitfield.disp8
2138 || t.bitfield.disp16
2139 || t.bitfield.disp32
2140 || t.bitfield.disp32s
2141 || t.bitfield.disp64
2142 || t.bitfield.baseindex);
2143
2144 default:
2145 abort ();
2146 }
2cfe26b6
AM
2147
2148 return 0;
40fb9820
L
2149}
2150
7a54636a
L
2151/* Return 1 if there is no conflict in 8bit/16bit/32bit/64bit/80bit size
2152 between operand GIVEN and opeand WANTED for instruction template T. */
5c07affc
L
2153
2154static INLINE int
7a54636a
L
2155match_operand_size (const insn_template *t, unsigned int wanted,
2156 unsigned int given)
5c07affc 2157{
3ac21baa
JB
2158 return !((i.types[given].bitfield.byte
2159 && !t->operand_types[wanted].bitfield.byte)
2160 || (i.types[given].bitfield.word
2161 && !t->operand_types[wanted].bitfield.word)
2162 || (i.types[given].bitfield.dword
2163 && !t->operand_types[wanted].bitfield.dword)
2164 || (i.types[given].bitfield.qword
2165 && !t->operand_types[wanted].bitfield.qword)
2166 || (i.types[given].bitfield.tbyte
2167 && !t->operand_types[wanted].bitfield.tbyte));
5c07affc
L
2168}
2169
dd40ce22
L
2170/* Return 1 if there is no conflict in SIMD register between operand
2171 GIVEN and opeand WANTED for instruction template T. */
1b54b8d7
JB
2172
2173static INLINE int
dd40ce22
L
2174match_simd_size (const insn_template *t, unsigned int wanted,
2175 unsigned int given)
1b54b8d7 2176{
3ac21baa
JB
2177 return !((i.types[given].bitfield.xmmword
2178 && !t->operand_types[wanted].bitfield.xmmword)
2179 || (i.types[given].bitfield.ymmword
2180 && !t->operand_types[wanted].bitfield.ymmword)
2181 || (i.types[given].bitfield.zmmword
260cd341
LC
2182 && !t->operand_types[wanted].bitfield.zmmword)
2183 || (i.types[given].bitfield.tmmword
2184 && !t->operand_types[wanted].bitfield.tmmword));
1b54b8d7
JB
2185}
2186
7a54636a
L
2187/* Return 1 if there is no conflict in any size between operand GIVEN
2188 and opeand WANTED for instruction template T. */
5c07affc
L
2189
2190static INLINE int
dd40ce22
L
2191match_mem_size (const insn_template *t, unsigned int wanted,
2192 unsigned int given)
5c07affc 2193{
7a54636a 2194 return (match_operand_size (t, wanted, given)
3ac21baa 2195 && !((i.types[given].bitfield.unspecified
af508cb9 2196 && !i.broadcast
3ac21baa
JB
2197 && !t->operand_types[wanted].bitfield.unspecified)
2198 || (i.types[given].bitfield.fword
2199 && !t->operand_types[wanted].bitfield.fword)
1b54b8d7
JB
2200 /* For scalar opcode templates to allow register and memory
2201 operands at the same time, some special casing is needed
d6793fa1
JB
2202 here. Also for v{,p}broadcast*, {,v}pmov{s,z}*, and
2203 down-conversion vpmov*. */
3528c362 2204 || ((t->operand_types[wanted].bitfield.class == RegSIMD
bc49bfd8
JB
2205 && t->operand_types[wanted].bitfield.byte
2206 + t->operand_types[wanted].bitfield.word
2207 + t->operand_types[wanted].bitfield.dword
2208 + t->operand_types[wanted].bitfield.qword
2209 > !!t->opcode_modifier.broadcast)
3ac21baa
JB
2210 ? (i.types[given].bitfield.xmmword
2211 || i.types[given].bitfield.ymmword
2212 || i.types[given].bitfield.zmmword)
2213 : !match_simd_size(t, wanted, given))));
5c07affc
L
2214}
2215
3ac21baa
JB
2216/* Return value has MATCH_STRAIGHT set if there is no size conflict on any
2217 operands for instruction template T, and it has MATCH_REVERSE set if there
2218 is no size conflict on any operands for the template with operands reversed
2219 (and the template allows for reversing in the first place). */
5c07affc 2220
3ac21baa
JB
2221#define MATCH_STRAIGHT 1
2222#define MATCH_REVERSE 2
2223
2224static INLINE unsigned int
d3ce72d0 2225operand_size_match (const insn_template *t)
5c07affc 2226{
3ac21baa 2227 unsigned int j, match = MATCH_STRAIGHT;
5c07affc 2228
0cfa3eb3 2229 /* Don't check non-absolute jump instructions. */
5c07affc 2230 if (t->opcode_modifier.jump
0cfa3eb3 2231 && t->opcode_modifier.jump != JUMP_ABSOLUTE)
5c07affc
L
2232 return match;
2233
2234 /* Check memory and accumulator operand size. */
2235 for (j = 0; j < i.operands; j++)
2236 {
3528c362
JB
2237 if (i.types[j].bitfield.class != Reg
2238 && i.types[j].bitfield.class != RegSIMD
601e8564 2239 && t->opcode_modifier.anysize)
5c07affc
L
2240 continue;
2241
bab6aec1 2242 if (t->operand_types[j].bitfield.class == Reg
7a54636a 2243 && !match_operand_size (t, j, j))
5c07affc
L
2244 {
2245 match = 0;
2246 break;
2247 }
2248
3528c362 2249 if (t->operand_types[j].bitfield.class == RegSIMD
3ac21baa 2250 && !match_simd_size (t, j, j))
1b54b8d7
JB
2251 {
2252 match = 0;
2253 break;
2254 }
2255
75e5731b 2256 if (t->operand_types[j].bitfield.instance == Accum
7a54636a 2257 && (!match_operand_size (t, j, j) || !match_simd_size (t, j, j)))
1b54b8d7
JB
2258 {
2259 match = 0;
2260 break;
2261 }
2262
c48dadc9 2263 if ((i.flags[j] & Operand_Mem) && !match_mem_size (t, j, j))
5c07affc
L
2264 {
2265 match = 0;
2266 break;
2267 }
2268 }
2269
3ac21baa 2270 if (!t->opcode_modifier.d)
891edac4 2271 {
dc1e8a47 2272 mismatch:
3ac21baa
JB
2273 if (!match)
2274 i.error = operand_size_mismatch;
2275 return match;
891edac4 2276 }
5c07affc
L
2277
2278 /* Check reverse. */
f5eb1d70 2279 gas_assert (i.operands >= 2 && i.operands <= 3);
5c07affc 2280
f5eb1d70 2281 for (j = 0; j < i.operands; j++)
5c07affc 2282 {
f5eb1d70
JB
2283 unsigned int given = i.operands - j - 1;
2284
bab6aec1 2285 if (t->operand_types[j].bitfield.class == Reg
f5eb1d70 2286 && !match_operand_size (t, j, given))
891edac4 2287 goto mismatch;
5c07affc 2288
3528c362 2289 if (t->operand_types[j].bitfield.class == RegSIMD
f5eb1d70 2290 && !match_simd_size (t, j, given))
dbbc8b7e
JB
2291 goto mismatch;
2292
75e5731b 2293 if (t->operand_types[j].bitfield.instance == Accum
f5eb1d70
JB
2294 && (!match_operand_size (t, j, given)
2295 || !match_simd_size (t, j, given)))
dbbc8b7e
JB
2296 goto mismatch;
2297
f5eb1d70 2298 if ((i.flags[given] & Operand_Mem) && !match_mem_size (t, j, given))
891edac4 2299 goto mismatch;
5c07affc
L
2300 }
2301
3ac21baa 2302 return match | MATCH_REVERSE;
5c07affc
L
2303}
2304
c6fb90c8 2305static INLINE int
40fb9820
L
2306operand_type_match (i386_operand_type overlap,
2307 i386_operand_type given)
2308{
2309 i386_operand_type temp = overlap;
2310
7d5e4556 2311 temp.bitfield.unspecified = 0;
5c07affc
L
2312 temp.bitfield.byte = 0;
2313 temp.bitfield.word = 0;
2314 temp.bitfield.dword = 0;
2315 temp.bitfield.fword = 0;
2316 temp.bitfield.qword = 0;
2317 temp.bitfield.tbyte = 0;
2318 temp.bitfield.xmmword = 0;
c0f3af97 2319 temp.bitfield.ymmword = 0;
43234a1e 2320 temp.bitfield.zmmword = 0;
260cd341 2321 temp.bitfield.tmmword = 0;
0dfbf9d7 2322 if (operand_type_all_zero (&temp))
891edac4 2323 goto mismatch;
40fb9820 2324
6f2f06be 2325 if (given.bitfield.baseindex == overlap.bitfield.baseindex)
891edac4
L
2326 return 1;
2327
dc1e8a47 2328 mismatch:
a65babc9 2329 i.error = operand_type_mismatch;
891edac4 2330 return 0;
40fb9820
L
2331}
2332
7d5e4556 2333/* If given types g0 and g1 are registers they must be of the same type
10c17abd 2334 unless the expected operand type register overlap is null.
5de4d9ef 2335 Some Intel syntax memory operand size checking also happens here. */
40fb9820 2336
c6fb90c8 2337static INLINE int
dc821c5f 2338operand_type_register_match (i386_operand_type g0,
40fb9820 2339 i386_operand_type t0,
40fb9820
L
2340 i386_operand_type g1,
2341 i386_operand_type t1)
2342{
bab6aec1 2343 if (g0.bitfield.class != Reg
3528c362 2344 && g0.bitfield.class != RegSIMD
10c17abd
JB
2345 && (!operand_type_check (g0, anymem)
2346 || g0.bitfield.unspecified
5de4d9ef
JB
2347 || (t0.bitfield.class != Reg
2348 && t0.bitfield.class != RegSIMD)))
40fb9820
L
2349 return 1;
2350
bab6aec1 2351 if (g1.bitfield.class != Reg
3528c362 2352 && g1.bitfield.class != RegSIMD
10c17abd
JB
2353 && (!operand_type_check (g1, anymem)
2354 || g1.bitfield.unspecified
5de4d9ef
JB
2355 || (t1.bitfield.class != Reg
2356 && t1.bitfield.class != RegSIMD)))
40fb9820
L
2357 return 1;
2358
dc821c5f
JB
2359 if (g0.bitfield.byte == g1.bitfield.byte
2360 && g0.bitfield.word == g1.bitfield.word
2361 && g0.bitfield.dword == g1.bitfield.dword
10c17abd
JB
2362 && g0.bitfield.qword == g1.bitfield.qword
2363 && g0.bitfield.xmmword == g1.bitfield.xmmword
2364 && g0.bitfield.ymmword == g1.bitfield.ymmword
2365 && g0.bitfield.zmmword == g1.bitfield.zmmword)
40fb9820
L
2366 return 1;
2367
dc821c5f
JB
2368 if (!(t0.bitfield.byte & t1.bitfield.byte)
2369 && !(t0.bitfield.word & t1.bitfield.word)
2370 && !(t0.bitfield.dword & t1.bitfield.dword)
10c17abd
JB
2371 && !(t0.bitfield.qword & t1.bitfield.qword)
2372 && !(t0.bitfield.xmmword & t1.bitfield.xmmword)
2373 && !(t0.bitfield.ymmword & t1.bitfield.ymmword)
2374 && !(t0.bitfield.zmmword & t1.bitfield.zmmword))
891edac4
L
2375 return 1;
2376
a65babc9 2377 i.error = register_type_mismatch;
891edac4
L
2378
2379 return 0;
40fb9820
L
2380}
2381
4c692bc7
JB
2382static INLINE unsigned int
2383register_number (const reg_entry *r)
2384{
2385 unsigned int nr = r->reg_num;
2386
2387 if (r->reg_flags & RegRex)
2388 nr += 8;
2389
200cbe0f
L
2390 if (r->reg_flags & RegVRex)
2391 nr += 16;
2392
4c692bc7
JB
2393 return nr;
2394}
2395
252b5132 2396static INLINE unsigned int
40fb9820 2397mode_from_disp_size (i386_operand_type t)
252b5132 2398{
b5014f7a 2399 if (t.bitfield.disp8)
40fb9820
L
2400 return 1;
2401 else if (t.bitfield.disp16
2402 || t.bitfield.disp32
2403 || t.bitfield.disp32s)
2404 return 2;
2405 else
2406 return 0;
252b5132
RH
2407}
2408
2409static INLINE int
65879393 2410fits_in_signed_byte (addressT num)
252b5132 2411{
65879393 2412 return num + 0x80 <= 0xff;
47926f60 2413}
252b5132
RH
2414
2415static INLINE int
65879393 2416fits_in_unsigned_byte (addressT num)
252b5132 2417{
65879393 2418 return num <= 0xff;
47926f60 2419}
252b5132
RH
2420
2421static INLINE int
65879393 2422fits_in_unsigned_word (addressT num)
252b5132 2423{
65879393 2424 return num <= 0xffff;
47926f60 2425}
252b5132
RH
2426
2427static INLINE int
65879393 2428fits_in_signed_word (addressT num)
252b5132 2429{
65879393 2430 return num + 0x8000 <= 0xffff;
47926f60 2431}
2a962e6d 2432
3e73aa7c 2433static INLINE int
65879393 2434fits_in_signed_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2435{
2436#ifndef BFD64
2437 return 1;
2438#else
65879393 2439 return num + 0x80000000 <= 0xffffffff;
3e73aa7c
JH
2440#endif
2441} /* fits_in_signed_long() */
2a962e6d 2442
3e73aa7c 2443static INLINE int
65879393 2444fits_in_unsigned_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2445{
2446#ifndef BFD64
2447 return 1;
2448#else
65879393 2449 return num <= 0xffffffff;
3e73aa7c
JH
2450#endif
2451} /* fits_in_unsigned_long() */
252b5132 2452
43234a1e 2453static INLINE int
b5014f7a 2454fits_in_disp8 (offsetT num)
43234a1e
L
2455{
2456 int shift = i.memshift;
2457 unsigned int mask;
2458
2459 if (shift == -1)
2460 abort ();
2461
2462 mask = (1 << shift) - 1;
2463
2464 /* Return 0 if NUM isn't properly aligned. */
2465 if ((num & mask))
2466 return 0;
2467
2468 /* Check if NUM will fit in 8bit after shift. */
2469 return fits_in_signed_byte (num >> shift);
2470}
2471
a683cc34
SP
2472static INLINE int
2473fits_in_imm4 (offsetT num)
2474{
2475 return (num & 0xf) == num;
2476}
2477
40fb9820 2478static i386_operand_type
e3bb37b5 2479smallest_imm_type (offsetT num)
252b5132 2480{
40fb9820 2481 i386_operand_type t;
7ab9ffdd 2482
0dfbf9d7 2483 operand_type_set (&t, 0);
40fb9820
L
2484 t.bitfield.imm64 = 1;
2485
2486 if (cpu_arch_tune != PROCESSOR_I486 && num == 1)
e413e4e9
AM
2487 {
2488 /* This code is disabled on the 486 because all the Imm1 forms
2489 in the opcode table are slower on the i486. They're the
2490 versions with the implicitly specified single-position
2491 displacement, which has another syntax if you really want to
2492 use that form. */
40fb9820
L
2493 t.bitfield.imm1 = 1;
2494 t.bitfield.imm8 = 1;
2495 t.bitfield.imm8s = 1;
2496 t.bitfield.imm16 = 1;
2497 t.bitfield.imm32 = 1;
2498 t.bitfield.imm32s = 1;
2499 }
2500 else if (fits_in_signed_byte (num))
2501 {
2502 t.bitfield.imm8 = 1;
2503 t.bitfield.imm8s = 1;
2504 t.bitfield.imm16 = 1;
2505 t.bitfield.imm32 = 1;
2506 t.bitfield.imm32s = 1;
2507 }
2508 else if (fits_in_unsigned_byte (num))
2509 {
2510 t.bitfield.imm8 = 1;
2511 t.bitfield.imm16 = 1;
2512 t.bitfield.imm32 = 1;
2513 t.bitfield.imm32s = 1;
2514 }
2515 else if (fits_in_signed_word (num) || fits_in_unsigned_word (num))
2516 {
2517 t.bitfield.imm16 = 1;
2518 t.bitfield.imm32 = 1;
2519 t.bitfield.imm32s = 1;
2520 }
2521 else if (fits_in_signed_long (num))
2522 {
2523 t.bitfield.imm32 = 1;
2524 t.bitfield.imm32s = 1;
2525 }
2526 else if (fits_in_unsigned_long (num))
2527 t.bitfield.imm32 = 1;
2528
2529 return t;
47926f60 2530}
252b5132 2531
847f7ad4 2532static offsetT
e3bb37b5 2533offset_in_range (offsetT val, int size)
847f7ad4 2534{
508866be 2535 addressT mask;
ba2adb93 2536
847f7ad4
AM
2537 switch (size)
2538 {
508866be
L
2539 case 1: mask = ((addressT) 1 << 8) - 1; break;
2540 case 2: mask = ((addressT) 1 << 16) - 1; break;
3b0ec529 2541 case 4: mask = ((addressT) 2 << 31) - 1; break;
3e73aa7c
JH
2542#ifdef BFD64
2543 case 8: mask = ((addressT) 2 << 63) - 1; break;
2544#endif
47926f60 2545 default: abort ();
847f7ad4
AM
2546 }
2547
47926f60 2548 if ((val & ~mask) != 0 && (val & ~mask) != ~mask)
847f7ad4
AM
2549 {
2550 char buf1[40], buf2[40];
2551
2552 sprint_value (buf1, val);
2553 sprint_value (buf2, val & mask);
2554 as_warn (_("%s shortened to %s"), buf1, buf2);
2555 }
2556 return val & mask;
2557}
2558
c32fa91d
L
2559enum PREFIX_GROUP
2560{
2561 PREFIX_EXIST = 0,
2562 PREFIX_LOCK,
2563 PREFIX_REP,
04ef582a 2564 PREFIX_DS,
c32fa91d
L
2565 PREFIX_OTHER
2566};
2567
2568/* Returns
2569 a. PREFIX_EXIST if attempting to add a prefix where one from the
2570 same class already exists.
2571 b. PREFIX_LOCK if lock prefix is added.
2572 c. PREFIX_REP if rep/repne prefix is added.
04ef582a
L
2573 d. PREFIX_DS if ds prefix is added.
2574 e. PREFIX_OTHER if other prefix is added.
c32fa91d
L
2575 */
2576
2577static enum PREFIX_GROUP
e3bb37b5 2578add_prefix (unsigned int prefix)
252b5132 2579{
c32fa91d 2580 enum PREFIX_GROUP ret = PREFIX_OTHER;
b1905489 2581 unsigned int q;
252b5132 2582
29b0f896
AM
2583 if (prefix >= REX_OPCODE && prefix < REX_OPCODE + 16
2584 && flag_code == CODE_64BIT)
b1905489 2585 {
161a04f6 2586 if ((i.prefix[REX_PREFIX] & prefix & REX_W)
44846f29
JB
2587 || (i.prefix[REX_PREFIX] & prefix & REX_R)
2588 || (i.prefix[REX_PREFIX] & prefix & REX_X)
2589 || (i.prefix[REX_PREFIX] & prefix & REX_B))
c32fa91d 2590 ret = PREFIX_EXIST;
b1905489
JB
2591 q = REX_PREFIX;
2592 }
3e73aa7c 2593 else
b1905489
JB
2594 {
2595 switch (prefix)
2596 {
2597 default:
2598 abort ();
2599
b1905489 2600 case DS_PREFIX_OPCODE:
04ef582a
L
2601 ret = PREFIX_DS;
2602 /* Fall through. */
2603 case CS_PREFIX_OPCODE:
b1905489
JB
2604 case ES_PREFIX_OPCODE:
2605 case FS_PREFIX_OPCODE:
2606 case GS_PREFIX_OPCODE:
2607 case SS_PREFIX_OPCODE:
2608 q = SEG_PREFIX;
2609 break;
2610
2611 case REPNE_PREFIX_OPCODE:
2612 case REPE_PREFIX_OPCODE:
c32fa91d
L
2613 q = REP_PREFIX;
2614 ret = PREFIX_REP;
2615 break;
2616
b1905489 2617 case LOCK_PREFIX_OPCODE:
c32fa91d
L
2618 q = LOCK_PREFIX;
2619 ret = PREFIX_LOCK;
b1905489
JB
2620 break;
2621
2622 case FWAIT_OPCODE:
2623 q = WAIT_PREFIX;
2624 break;
2625
2626 case ADDR_PREFIX_OPCODE:
2627 q = ADDR_PREFIX;
2628 break;
2629
2630 case DATA_PREFIX_OPCODE:
2631 q = DATA_PREFIX;
2632 break;
2633 }
2634 if (i.prefix[q] != 0)
c32fa91d 2635 ret = PREFIX_EXIST;
b1905489 2636 }
252b5132 2637
b1905489 2638 if (ret)
252b5132 2639 {
b1905489
JB
2640 if (!i.prefix[q])
2641 ++i.prefixes;
2642 i.prefix[q] |= prefix;
252b5132 2643 }
b1905489
JB
2644 else
2645 as_bad (_("same type of prefix used twice"));
252b5132 2646
252b5132
RH
2647 return ret;
2648}
2649
2650static void
78f12dd3 2651update_code_flag (int value, int check)
eecb386c 2652{
78f12dd3
L
2653 PRINTF_LIKE ((*as_error));
2654
1e9cc1c2 2655 flag_code = (enum flag_code) value;
40fb9820
L
2656 if (flag_code == CODE_64BIT)
2657 {
2658 cpu_arch_flags.bitfield.cpu64 = 1;
2659 cpu_arch_flags.bitfield.cpuno64 = 0;
40fb9820
L
2660 }
2661 else
2662 {
2663 cpu_arch_flags.bitfield.cpu64 = 0;
2664 cpu_arch_flags.bitfield.cpuno64 = 1;
40fb9820
L
2665 }
2666 if (value == CODE_64BIT && !cpu_arch_flags.bitfield.cpulm )
3e73aa7c 2667 {
78f12dd3
L
2668 if (check)
2669 as_error = as_fatal;
2670 else
2671 as_error = as_bad;
2672 (*as_error) (_("64bit mode not supported on `%s'."),
2673 cpu_arch_name ? cpu_arch_name : default_arch);
3e73aa7c 2674 }
40fb9820 2675 if (value == CODE_32BIT && !cpu_arch_flags.bitfield.cpui386)
3e73aa7c 2676 {
78f12dd3
L
2677 if (check)
2678 as_error = as_fatal;
2679 else
2680 as_error = as_bad;
2681 (*as_error) (_("32bit mode not supported on `%s'."),
2682 cpu_arch_name ? cpu_arch_name : default_arch);
3e73aa7c 2683 }
eecb386c
AM
2684 stackop_size = '\0';
2685}
2686
78f12dd3
L
2687static void
2688set_code_flag (int value)
2689{
2690 update_code_flag (value, 0);
2691}
2692
eecb386c 2693static void
e3bb37b5 2694set_16bit_gcc_code_flag (int new_code_flag)
252b5132 2695{
1e9cc1c2 2696 flag_code = (enum flag_code) new_code_flag;
40fb9820
L
2697 if (flag_code != CODE_16BIT)
2698 abort ();
2699 cpu_arch_flags.bitfield.cpu64 = 0;
2700 cpu_arch_flags.bitfield.cpuno64 = 1;
9306ca4a 2701 stackop_size = LONG_MNEM_SUFFIX;
252b5132
RH
2702}
2703
2704static void
e3bb37b5 2705set_intel_syntax (int syntax_flag)
252b5132
RH
2706{
2707 /* Find out if register prefixing is specified. */
2708 int ask_naked_reg = 0;
2709
2710 SKIP_WHITESPACE ();
29b0f896 2711 if (!is_end_of_line[(unsigned char) *input_line_pointer])
252b5132 2712 {
d02603dc
NC
2713 char *string;
2714 int e = get_symbol_name (&string);
252b5132 2715
47926f60 2716 if (strcmp (string, "prefix") == 0)
252b5132 2717 ask_naked_reg = 1;
47926f60 2718 else if (strcmp (string, "noprefix") == 0)
252b5132
RH
2719 ask_naked_reg = -1;
2720 else
d0b47220 2721 as_bad (_("bad argument to syntax directive."));
d02603dc 2722 (void) restore_line_pointer (e);
252b5132
RH
2723 }
2724 demand_empty_rest_of_line ();
c3332e24 2725
252b5132
RH
2726 intel_syntax = syntax_flag;
2727
2728 if (ask_naked_reg == 0)
f86103b7
AM
2729 allow_naked_reg = (intel_syntax
2730 && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
252b5132
RH
2731 else
2732 allow_naked_reg = (ask_naked_reg < 0);
9306ca4a 2733
ee86248c 2734 expr_set_rank (O_full_ptr, syntax_flag ? 10 : 0);
7ab9ffdd 2735
e4a3b5a4 2736 identifier_chars['%'] = intel_syntax && allow_naked_reg ? '%' : 0;
9306ca4a 2737 identifier_chars['$'] = intel_syntax ? '$' : 0;
e4a3b5a4 2738 register_prefix = allow_naked_reg ? "" : "%";
252b5132
RH
2739}
2740
1efbbeb4
L
2741static void
2742set_intel_mnemonic (int mnemonic_flag)
2743{
e1d4d893 2744 intel_mnemonic = mnemonic_flag;
1efbbeb4
L
2745}
2746
db51cc60
L
2747static void
2748set_allow_index_reg (int flag)
2749{
2750 allow_index_reg = flag;
2751}
2752
cb19c032 2753static void
7bab8ab5 2754set_check (int what)
cb19c032 2755{
7bab8ab5
JB
2756 enum check_kind *kind;
2757 const char *str;
2758
2759 if (what)
2760 {
2761 kind = &operand_check;
2762 str = "operand";
2763 }
2764 else
2765 {
2766 kind = &sse_check;
2767 str = "sse";
2768 }
2769
cb19c032
L
2770 SKIP_WHITESPACE ();
2771
2772 if (!is_end_of_line[(unsigned char) *input_line_pointer])
2773 {
d02603dc
NC
2774 char *string;
2775 int e = get_symbol_name (&string);
cb19c032
L
2776
2777 if (strcmp (string, "none") == 0)
7bab8ab5 2778 *kind = check_none;
cb19c032 2779 else if (strcmp (string, "warning") == 0)
7bab8ab5 2780 *kind = check_warning;
cb19c032 2781 else if (strcmp (string, "error") == 0)
7bab8ab5 2782 *kind = check_error;
cb19c032 2783 else
7bab8ab5 2784 as_bad (_("bad argument to %s_check directive."), str);
d02603dc 2785 (void) restore_line_pointer (e);
cb19c032
L
2786 }
2787 else
7bab8ab5 2788 as_bad (_("missing argument for %s_check directive"), str);
cb19c032
L
2789
2790 demand_empty_rest_of_line ();
2791}
2792
8a9036a4
L
2793static void
2794check_cpu_arch_compatible (const char *name ATTRIBUTE_UNUSED,
1e9cc1c2 2795 i386_cpu_flags new_flag ATTRIBUTE_UNUSED)
8a9036a4
L
2796{
2797#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2798 static const char *arch;
2799
2800 /* Intel LIOM is only supported on ELF. */
2801 if (!IS_ELF)
2802 return;
2803
2804 if (!arch)
2805 {
2806 /* Use cpu_arch_name if it is set in md_parse_option. Otherwise
2807 use default_arch. */
2808 arch = cpu_arch_name;
2809 if (!arch)
2810 arch = default_arch;
2811 }
2812
81486035
L
2813 /* If we are targeting Intel MCU, we must enable it. */
2814 if (get_elf_backend_data (stdoutput)->elf_machine_code != EM_IAMCU
2815 || new_flag.bitfield.cpuiamcu)
2816 return;
2817
3632d14b 2818 /* If we are targeting Intel L1OM, we must enable it. */
8a9036a4 2819 if (get_elf_backend_data (stdoutput)->elf_machine_code != EM_L1OM
1e9cc1c2 2820 || new_flag.bitfield.cpul1om)
8a9036a4 2821 return;
76ba9986 2822
7a9068fe
L
2823 /* If we are targeting Intel K1OM, we must enable it. */
2824 if (get_elf_backend_data (stdoutput)->elf_machine_code != EM_K1OM
2825 || new_flag.bitfield.cpuk1om)
2826 return;
2827
8a9036a4
L
2828 as_bad (_("`%s' is not supported on `%s'"), name, arch);
2829#endif
2830}
2831
e413e4e9 2832static void
e3bb37b5 2833set_cpu_arch (int dummy ATTRIBUTE_UNUSED)
e413e4e9 2834{
47926f60 2835 SKIP_WHITESPACE ();
e413e4e9 2836
29b0f896 2837 if (!is_end_of_line[(unsigned char) *input_line_pointer])
e413e4e9 2838 {
d02603dc
NC
2839 char *string;
2840 int e = get_symbol_name (&string);
91d6fa6a 2841 unsigned int j;
40fb9820 2842 i386_cpu_flags flags;
e413e4e9 2843
91d6fa6a 2844 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
e413e4e9 2845 {
91d6fa6a 2846 if (strcmp (string, cpu_arch[j].name) == 0)
e413e4e9 2847 {
91d6fa6a 2848 check_cpu_arch_compatible (string, cpu_arch[j].flags);
8a9036a4 2849
5c6af06e
JB
2850 if (*string != '.')
2851 {
91d6fa6a 2852 cpu_arch_name = cpu_arch[j].name;
5c6af06e 2853 cpu_sub_arch_name = NULL;
91d6fa6a 2854 cpu_arch_flags = cpu_arch[j].flags;
40fb9820
L
2855 if (flag_code == CODE_64BIT)
2856 {
2857 cpu_arch_flags.bitfield.cpu64 = 1;
2858 cpu_arch_flags.bitfield.cpuno64 = 0;
2859 }
2860 else
2861 {
2862 cpu_arch_flags.bitfield.cpu64 = 0;
2863 cpu_arch_flags.bitfield.cpuno64 = 1;
2864 }
91d6fa6a
NC
2865 cpu_arch_isa = cpu_arch[j].type;
2866 cpu_arch_isa_flags = cpu_arch[j].flags;
ccc9c027
L
2867 if (!cpu_arch_tune_set)
2868 {
2869 cpu_arch_tune = cpu_arch_isa;
2870 cpu_arch_tune_flags = cpu_arch_isa_flags;
2871 }
5c6af06e
JB
2872 break;
2873 }
40fb9820 2874
293f5f65
L
2875 flags = cpu_flags_or (cpu_arch_flags,
2876 cpu_arch[j].flags);
81486035 2877
5b64d091 2878 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
5c6af06e 2879 {
6305a203
L
2880 if (cpu_sub_arch_name)
2881 {
2882 char *name = cpu_sub_arch_name;
2883 cpu_sub_arch_name = concat (name,
91d6fa6a 2884 cpu_arch[j].name,
1bf57e9f 2885 (const char *) NULL);
6305a203
L
2886 free (name);
2887 }
2888 else
91d6fa6a 2889 cpu_sub_arch_name = xstrdup (cpu_arch[j].name);
40fb9820 2890 cpu_arch_flags = flags;
a586129e 2891 cpu_arch_isa_flags = flags;
5c6af06e 2892 }
0089dace
L
2893 else
2894 cpu_arch_isa_flags
2895 = cpu_flags_or (cpu_arch_isa_flags,
2896 cpu_arch[j].flags);
d02603dc 2897 (void) restore_line_pointer (e);
5c6af06e
JB
2898 demand_empty_rest_of_line ();
2899 return;
e413e4e9
AM
2900 }
2901 }
293f5f65
L
2902
2903 if (*string == '.' && j >= ARRAY_SIZE (cpu_arch))
2904 {
33eaf5de 2905 /* Disable an ISA extension. */
293f5f65
L
2906 for (j = 0; j < ARRAY_SIZE (cpu_noarch); j++)
2907 if (strcmp (string + 1, cpu_noarch [j].name) == 0)
2908 {
2909 flags = cpu_flags_and_not (cpu_arch_flags,
2910 cpu_noarch[j].flags);
2911 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
2912 {
2913 if (cpu_sub_arch_name)
2914 {
2915 char *name = cpu_sub_arch_name;
2916 cpu_sub_arch_name = concat (name, string,
2917 (const char *) NULL);
2918 free (name);
2919 }
2920 else
2921 cpu_sub_arch_name = xstrdup (string);
2922 cpu_arch_flags = flags;
2923 cpu_arch_isa_flags = flags;
2924 }
2925 (void) restore_line_pointer (e);
2926 demand_empty_rest_of_line ();
2927 return;
2928 }
2929
2930 j = ARRAY_SIZE (cpu_arch);
2931 }
2932
91d6fa6a 2933 if (j >= ARRAY_SIZE (cpu_arch))
e413e4e9
AM
2934 as_bad (_("no such architecture: `%s'"), string);
2935
2936 *input_line_pointer = e;
2937 }
2938 else
2939 as_bad (_("missing cpu architecture"));
2940
fddf5b5b
AM
2941 no_cond_jump_promotion = 0;
2942 if (*input_line_pointer == ','
29b0f896 2943 && !is_end_of_line[(unsigned char) input_line_pointer[1]])
fddf5b5b 2944 {
d02603dc
NC
2945 char *string;
2946 char e;
2947
2948 ++input_line_pointer;
2949 e = get_symbol_name (&string);
fddf5b5b
AM
2950
2951 if (strcmp (string, "nojumps") == 0)
2952 no_cond_jump_promotion = 1;
2953 else if (strcmp (string, "jumps") == 0)
2954 ;
2955 else
2956 as_bad (_("no such architecture modifier: `%s'"), string);
2957
d02603dc 2958 (void) restore_line_pointer (e);
fddf5b5b
AM
2959 }
2960
e413e4e9
AM
2961 demand_empty_rest_of_line ();
2962}
2963
8a9036a4
L
2964enum bfd_architecture
2965i386_arch (void)
2966{
3632d14b 2967 if (cpu_arch_isa == PROCESSOR_L1OM)
8a9036a4
L
2968 {
2969 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2970 || flag_code != CODE_64BIT)
2971 as_fatal (_("Intel L1OM is 64bit ELF only"));
2972 return bfd_arch_l1om;
2973 }
7a9068fe
L
2974 else if (cpu_arch_isa == PROCESSOR_K1OM)
2975 {
2976 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2977 || flag_code != CODE_64BIT)
2978 as_fatal (_("Intel K1OM is 64bit ELF only"));
2979 return bfd_arch_k1om;
2980 }
81486035
L
2981 else if (cpu_arch_isa == PROCESSOR_IAMCU)
2982 {
2983 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2984 || flag_code == CODE_64BIT)
2985 as_fatal (_("Intel MCU is 32bit ELF only"));
2986 return bfd_arch_iamcu;
2987 }
8a9036a4
L
2988 else
2989 return bfd_arch_i386;
2990}
2991
b9d79e03 2992unsigned long
7016a5d5 2993i386_mach (void)
b9d79e03 2994{
351f65ca 2995 if (!strncmp (default_arch, "x86_64", 6))
8a9036a4 2996 {
3632d14b 2997 if (cpu_arch_isa == PROCESSOR_L1OM)
8a9036a4 2998 {
351f65ca
L
2999 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
3000 || default_arch[6] != '\0')
8a9036a4
L
3001 as_fatal (_("Intel L1OM is 64bit ELF only"));
3002 return bfd_mach_l1om;
3003 }
7a9068fe
L
3004 else if (cpu_arch_isa == PROCESSOR_K1OM)
3005 {
3006 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
3007 || default_arch[6] != '\0')
3008 as_fatal (_("Intel K1OM is 64bit ELF only"));
3009 return bfd_mach_k1om;
3010 }
351f65ca 3011 else if (default_arch[6] == '\0')
8a9036a4 3012 return bfd_mach_x86_64;
351f65ca
L
3013 else
3014 return bfd_mach_x64_32;
8a9036a4 3015 }
5197d474
L
3016 else if (!strcmp (default_arch, "i386")
3017 || !strcmp (default_arch, "iamcu"))
81486035
L
3018 {
3019 if (cpu_arch_isa == PROCESSOR_IAMCU)
3020 {
3021 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
3022 as_fatal (_("Intel MCU is 32bit ELF only"));
3023 return bfd_mach_i386_iamcu;
3024 }
3025 else
3026 return bfd_mach_i386_i386;
3027 }
b9d79e03 3028 else
2b5d6a91 3029 as_fatal (_("unknown architecture"));
b9d79e03 3030}
b9d79e03 3031\f
252b5132 3032void
7016a5d5 3033md_begin (void)
252b5132 3034{
86fa6981
L
3035 /* Support pseudo prefixes like {disp32}. */
3036 lex_type ['{'] = LEX_BEGIN_NAME;
3037
47926f60 3038 /* Initialize op_hash hash table. */
629310ab 3039 op_hash = str_htab_create ();
252b5132
RH
3040
3041 {
d3ce72d0 3042 const insn_template *optab;
29b0f896 3043 templates *core_optab;
252b5132 3044
47926f60
KH
3045 /* Setup for loop. */
3046 optab = i386_optab;
add39d23 3047 core_optab = XNEW (templates);
252b5132
RH
3048 core_optab->start = optab;
3049
3050 while (1)
3051 {
3052 ++optab;
3053 if (optab->name == NULL
3054 || strcmp (optab->name, (optab - 1)->name) != 0)
3055 {
3056 /* different name --> ship out current template list;
47926f60 3057 add to hash table; & begin anew. */
252b5132 3058 core_optab->end = optab;
fe0e921f
AM
3059 if (str_hash_insert (op_hash, (optab - 1)->name, core_optab, 0))
3060 as_fatal (_("duplicate %s"), (optab - 1)->name);
3061
252b5132
RH
3062 if (optab->name == NULL)
3063 break;
add39d23 3064 core_optab = XNEW (templates);
252b5132
RH
3065 core_optab->start = optab;
3066 }
3067 }
3068 }
3069
47926f60 3070 /* Initialize reg_hash hash table. */
629310ab 3071 reg_hash = str_htab_create ();
252b5132 3072 {
29b0f896 3073 const reg_entry *regtab;
c3fe08fa 3074 unsigned int regtab_size = i386_regtab_size;
252b5132 3075
c3fe08fa 3076 for (regtab = i386_regtab; regtab_size--; regtab++)
fe0e921f
AM
3077 if (str_hash_insert (reg_hash, regtab->reg_name, regtab, 0) != NULL)
3078 as_fatal (_("duplicate %s"), regtab->reg_name);
252b5132
RH
3079 }
3080
47926f60 3081 /* Fill in lexical tables: mnemonic_chars, operand_chars. */
252b5132 3082 {
29b0f896
AM
3083 int c;
3084 char *p;
252b5132
RH
3085
3086 for (c = 0; c < 256; c++)
3087 {
3882b010 3088 if (ISDIGIT (c))
252b5132
RH
3089 {
3090 digit_chars[c] = c;
3091 mnemonic_chars[c] = c;
3092 register_chars[c] = c;
3093 operand_chars[c] = c;
3094 }
3882b010 3095 else if (ISLOWER (c))
252b5132
RH
3096 {
3097 mnemonic_chars[c] = c;
3098 register_chars[c] = c;
3099 operand_chars[c] = c;
3100 }
3882b010 3101 else if (ISUPPER (c))
252b5132 3102 {
3882b010 3103 mnemonic_chars[c] = TOLOWER (c);
252b5132
RH
3104 register_chars[c] = mnemonic_chars[c];
3105 operand_chars[c] = c;
3106 }
43234a1e 3107 else if (c == '{' || c == '}')
86fa6981
L
3108 {
3109 mnemonic_chars[c] = c;
3110 operand_chars[c] = c;
3111 }
b3983e5f
JB
3112#ifdef SVR4_COMMENT_CHARS
3113 else if (c == '\\' && strchr (i386_comment_chars, '/'))
3114 operand_chars[c] = c;
3115#endif
252b5132 3116
3882b010 3117 if (ISALPHA (c) || ISDIGIT (c))
252b5132
RH
3118 identifier_chars[c] = c;
3119 else if (c >= 128)
3120 {
3121 identifier_chars[c] = c;
3122 operand_chars[c] = c;
3123 }
3124 }
3125
3126#ifdef LEX_AT
3127 identifier_chars['@'] = '@';
32137342
NC
3128#endif
3129#ifdef LEX_QM
3130 identifier_chars['?'] = '?';
3131 operand_chars['?'] = '?';
252b5132 3132#endif
252b5132 3133 digit_chars['-'] = '-';
c0f3af97 3134 mnemonic_chars['_'] = '_';
791fe849 3135 mnemonic_chars['-'] = '-';
0003779b 3136 mnemonic_chars['.'] = '.';
252b5132
RH
3137 identifier_chars['_'] = '_';
3138 identifier_chars['.'] = '.';
3139
3140 for (p = operand_special_chars; *p != '\0'; p++)
3141 operand_chars[(unsigned char) *p] = *p;
3142 }
3143
a4447b93
RH
3144 if (flag_code == CODE_64BIT)
3145 {
ca19b261
KT
3146#if defined (OBJ_COFF) && defined (TE_PE)
3147 x86_dwarf2_return_column = (OUTPUT_FLAVOR == bfd_target_coff_flavour
3148 ? 32 : 16);
3149#else
a4447b93 3150 x86_dwarf2_return_column = 16;
ca19b261 3151#endif
61ff971f 3152 x86_cie_data_alignment = -8;
a4447b93
RH
3153 }
3154 else
3155 {
3156 x86_dwarf2_return_column = 8;
3157 x86_cie_data_alignment = -4;
3158 }
e379e5f3
L
3159
3160 /* NB: FUSED_JCC_PADDING frag must have sufficient room so that it
3161 can be turned into BRANCH_PREFIX frag. */
3162 if (align_branch_prefix_size > MAX_FUSED_JCC_PADDING_SIZE)
3163 abort ();
252b5132
RH
3164}
3165
3166void
e3bb37b5 3167i386_print_statistics (FILE *file)
252b5132 3168{
629310ab
ML
3169 htab_print_statistics (file, "i386 opcode", op_hash);
3170 htab_print_statistics (file, "i386 register", reg_hash);
252b5132
RH
3171}
3172\f
252b5132
RH
3173#ifdef DEBUG386
3174
ce8a8b2f 3175/* Debugging routines for md_assemble. */
d3ce72d0 3176static void pte (insn_template *);
40fb9820 3177static void pt (i386_operand_type);
e3bb37b5
L
3178static void pe (expressionS *);
3179static void ps (symbolS *);
252b5132
RH
3180
3181static void
2c703856 3182pi (const char *line, i386_insn *x)
252b5132 3183{
09137c09 3184 unsigned int j;
252b5132
RH
3185
3186 fprintf (stdout, "%s: template ", line);
3187 pte (&x->tm);
09f131f2
JH
3188 fprintf (stdout, " address: base %s index %s scale %x\n",
3189 x->base_reg ? x->base_reg->reg_name : "none",
3190 x->index_reg ? x->index_reg->reg_name : "none",
3191 x->log2_scale_factor);
3192 fprintf (stdout, " modrm: mode %x reg %x reg/mem %x\n",
252b5132 3193 x->rm.mode, x->rm.reg, x->rm.regmem);
09f131f2
JH
3194 fprintf (stdout, " sib: base %x index %x scale %x\n",
3195 x->sib.base, x->sib.index, x->sib.scale);
3196 fprintf (stdout, " rex: 64bit %x extX %x extY %x extZ %x\n",
161a04f6
L
3197 (x->rex & REX_W) != 0,
3198 (x->rex & REX_R) != 0,
3199 (x->rex & REX_X) != 0,
3200 (x->rex & REX_B) != 0);
09137c09 3201 for (j = 0; j < x->operands; j++)
252b5132 3202 {
09137c09
SP
3203 fprintf (stdout, " #%d: ", j + 1);
3204 pt (x->types[j]);
252b5132 3205 fprintf (stdout, "\n");
bab6aec1 3206 if (x->types[j].bitfield.class == Reg
3528c362
JB
3207 || x->types[j].bitfield.class == RegMMX
3208 || x->types[j].bitfield.class == RegSIMD
dd6b8a0b 3209 || x->types[j].bitfield.class == RegMask
00cee14f 3210 || x->types[j].bitfield.class == SReg
4a5c67ed
JB
3211 || x->types[j].bitfield.class == RegCR
3212 || x->types[j].bitfield.class == RegDR
dd6b8a0b
JB
3213 || x->types[j].bitfield.class == RegTR
3214 || x->types[j].bitfield.class == RegBND)
09137c09
SP
3215 fprintf (stdout, "%s\n", x->op[j].regs->reg_name);
3216 if (operand_type_check (x->types[j], imm))
3217 pe (x->op[j].imms);
3218 if (operand_type_check (x->types[j], disp))
3219 pe (x->op[j].disps);
252b5132
RH
3220 }
3221}
3222
3223static void
d3ce72d0 3224pte (insn_template *t)
252b5132 3225{
09137c09 3226 unsigned int j;
252b5132 3227 fprintf (stdout, " %d operands ", t->operands);
47926f60 3228 fprintf (stdout, "opcode %x ", t->base_opcode);
252b5132
RH
3229 if (t->extension_opcode != None)
3230 fprintf (stdout, "ext %x ", t->extension_opcode);
40fb9820 3231 if (t->opcode_modifier.d)
252b5132 3232 fprintf (stdout, "D");
40fb9820 3233 if (t->opcode_modifier.w)
252b5132
RH
3234 fprintf (stdout, "W");
3235 fprintf (stdout, "\n");
09137c09 3236 for (j = 0; j < t->operands; j++)
252b5132 3237 {
09137c09
SP
3238 fprintf (stdout, " #%d type ", j + 1);
3239 pt (t->operand_types[j]);
252b5132
RH
3240 fprintf (stdout, "\n");
3241 }
3242}
3243
3244static void
e3bb37b5 3245pe (expressionS *e)
252b5132 3246{
24eab124 3247 fprintf (stdout, " operation %d\n", e->X_op);
b77ad1d4
AM
3248 fprintf (stdout, " add_number %ld (%lx)\n",
3249 (long) e->X_add_number, (long) e->X_add_number);
252b5132
RH
3250 if (e->X_add_symbol)
3251 {
3252 fprintf (stdout, " add_symbol ");
3253 ps (e->X_add_symbol);
3254 fprintf (stdout, "\n");
3255 }
3256 if (e->X_op_symbol)
3257 {
3258 fprintf (stdout, " op_symbol ");
3259 ps (e->X_op_symbol);
3260 fprintf (stdout, "\n");
3261 }
3262}
3263
3264static void
e3bb37b5 3265ps (symbolS *s)
252b5132
RH
3266{
3267 fprintf (stdout, "%s type %s%s",
3268 S_GET_NAME (s),
3269 S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
3270 segment_name (S_GET_SEGMENT (s)));
3271}
3272
7b81dfbb 3273static struct type_name
252b5132 3274 {
40fb9820
L
3275 i386_operand_type mask;
3276 const char *name;
252b5132 3277 }
7b81dfbb 3278const type_names[] =
252b5132 3279{
40fb9820
L
3280 { OPERAND_TYPE_REG8, "r8" },
3281 { OPERAND_TYPE_REG16, "r16" },
3282 { OPERAND_TYPE_REG32, "r32" },
3283 { OPERAND_TYPE_REG64, "r64" },
2c703856
JB
3284 { OPERAND_TYPE_ACC8, "acc8" },
3285 { OPERAND_TYPE_ACC16, "acc16" },
3286 { OPERAND_TYPE_ACC32, "acc32" },
3287 { OPERAND_TYPE_ACC64, "acc64" },
40fb9820
L
3288 { OPERAND_TYPE_IMM8, "i8" },
3289 { OPERAND_TYPE_IMM8, "i8s" },
3290 { OPERAND_TYPE_IMM16, "i16" },
3291 { OPERAND_TYPE_IMM32, "i32" },
3292 { OPERAND_TYPE_IMM32S, "i32s" },
3293 { OPERAND_TYPE_IMM64, "i64" },
3294 { OPERAND_TYPE_IMM1, "i1" },
3295 { OPERAND_TYPE_BASEINDEX, "BaseIndex" },
3296 { OPERAND_TYPE_DISP8, "d8" },
3297 { OPERAND_TYPE_DISP16, "d16" },
3298 { OPERAND_TYPE_DISP32, "d32" },
3299 { OPERAND_TYPE_DISP32S, "d32s" },
3300 { OPERAND_TYPE_DISP64, "d64" },
3301 { OPERAND_TYPE_INOUTPORTREG, "InOutPortReg" },
3302 { OPERAND_TYPE_SHIFTCOUNT, "ShiftCount" },
3303 { OPERAND_TYPE_CONTROL, "control reg" },
3304 { OPERAND_TYPE_TEST, "test reg" },
3305 { OPERAND_TYPE_DEBUG, "debug reg" },
3306 { OPERAND_TYPE_FLOATREG, "FReg" },
3307 { OPERAND_TYPE_FLOATACC, "FAcc" },
21df382b 3308 { OPERAND_TYPE_SREG, "SReg" },
40fb9820
L
3309 { OPERAND_TYPE_REGMMX, "rMMX" },
3310 { OPERAND_TYPE_REGXMM, "rXMM" },
0349dc08 3311 { OPERAND_TYPE_REGYMM, "rYMM" },
43234a1e 3312 { OPERAND_TYPE_REGZMM, "rZMM" },
260cd341 3313 { OPERAND_TYPE_REGTMM, "rTMM" },
43234a1e 3314 { OPERAND_TYPE_REGMASK, "Mask reg" },
252b5132
RH
3315};
3316
3317static void
40fb9820 3318pt (i386_operand_type t)
252b5132 3319{
40fb9820 3320 unsigned int j;
c6fb90c8 3321 i386_operand_type a;
252b5132 3322
40fb9820 3323 for (j = 0; j < ARRAY_SIZE (type_names); j++)
c6fb90c8
L
3324 {
3325 a = operand_type_and (t, type_names[j].mask);
2c703856 3326 if (operand_type_equal (&a, &type_names[j].mask))
c6fb90c8
L
3327 fprintf (stdout, "%s, ", type_names[j].name);
3328 }
252b5132
RH
3329 fflush (stdout);
3330}
3331
3332#endif /* DEBUG386 */
3333\f
252b5132 3334static bfd_reloc_code_real_type
3956db08 3335reloc (unsigned int size,
64e74474
AM
3336 int pcrel,
3337 int sign,
3338 bfd_reloc_code_real_type other)
252b5132 3339{
47926f60 3340 if (other != NO_RELOC)
3956db08 3341 {
91d6fa6a 3342 reloc_howto_type *rel;
3956db08
JB
3343
3344 if (size == 8)
3345 switch (other)
3346 {
64e74474
AM
3347 case BFD_RELOC_X86_64_GOT32:
3348 return BFD_RELOC_X86_64_GOT64;
3349 break;
553d1284
L
3350 case BFD_RELOC_X86_64_GOTPLT64:
3351 return BFD_RELOC_X86_64_GOTPLT64;
3352 break;
64e74474
AM
3353 case BFD_RELOC_X86_64_PLTOFF64:
3354 return BFD_RELOC_X86_64_PLTOFF64;
3355 break;
3356 case BFD_RELOC_X86_64_GOTPC32:
3357 other = BFD_RELOC_X86_64_GOTPC64;
3358 break;
3359 case BFD_RELOC_X86_64_GOTPCREL:
3360 other = BFD_RELOC_X86_64_GOTPCREL64;
3361 break;
3362 case BFD_RELOC_X86_64_TPOFF32:
3363 other = BFD_RELOC_X86_64_TPOFF64;
3364 break;
3365 case BFD_RELOC_X86_64_DTPOFF32:
3366 other = BFD_RELOC_X86_64_DTPOFF64;
3367 break;
3368 default:
3369 break;
3956db08 3370 }
e05278af 3371
8ce3d284 3372#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
3373 if (other == BFD_RELOC_SIZE32)
3374 {
3375 if (size == 8)
1ab668bf 3376 other = BFD_RELOC_SIZE64;
8fd4256d 3377 if (pcrel)
1ab668bf
AM
3378 {
3379 as_bad (_("there are no pc-relative size relocations"));
3380 return NO_RELOC;
3381 }
8fd4256d 3382 }
8ce3d284 3383#endif
8fd4256d 3384
e05278af 3385 /* Sign-checking 4-byte relocations in 16-/32-bit code is pointless. */
f2d8a97c 3386 if (size == 4 && (flag_code != CODE_64BIT || disallow_64bit_reloc))
e05278af
JB
3387 sign = -1;
3388
91d6fa6a
NC
3389 rel = bfd_reloc_type_lookup (stdoutput, other);
3390 if (!rel)
3956db08 3391 as_bad (_("unknown relocation (%u)"), other);
91d6fa6a 3392 else if (size != bfd_get_reloc_size (rel))
3956db08 3393 as_bad (_("%u-byte relocation cannot be applied to %u-byte field"),
91d6fa6a 3394 bfd_get_reloc_size (rel),
3956db08 3395 size);
91d6fa6a 3396 else if (pcrel && !rel->pc_relative)
3956db08 3397 as_bad (_("non-pc-relative relocation for pc-relative field"));
91d6fa6a 3398 else if ((rel->complain_on_overflow == complain_overflow_signed
3956db08 3399 && !sign)
91d6fa6a 3400 || (rel->complain_on_overflow == complain_overflow_unsigned
64e74474 3401 && sign > 0))
3956db08
JB
3402 as_bad (_("relocated field and relocation type differ in signedness"));
3403 else
3404 return other;
3405 return NO_RELOC;
3406 }
252b5132
RH
3407
3408 if (pcrel)
3409 {
3e73aa7c 3410 if (!sign)
3956db08 3411 as_bad (_("there are no unsigned pc-relative relocations"));
252b5132
RH
3412 switch (size)
3413 {
3414 case 1: return BFD_RELOC_8_PCREL;
3415 case 2: return BFD_RELOC_16_PCREL;
d258b828 3416 case 4: return BFD_RELOC_32_PCREL;
d6ab8113 3417 case 8: return BFD_RELOC_64_PCREL;
252b5132 3418 }
3956db08 3419 as_bad (_("cannot do %u byte pc-relative relocation"), size);
252b5132
RH
3420 }
3421 else
3422 {
3956db08 3423 if (sign > 0)
e5cb08ac 3424 switch (size)
3e73aa7c
JH
3425 {
3426 case 4: return BFD_RELOC_X86_64_32S;
3427 }
3428 else
3429 switch (size)
3430 {
3431 case 1: return BFD_RELOC_8;
3432 case 2: return BFD_RELOC_16;
3433 case 4: return BFD_RELOC_32;
3434 case 8: return BFD_RELOC_64;
3435 }
3956db08
JB
3436 as_bad (_("cannot do %s %u byte relocation"),
3437 sign > 0 ? "signed" : "unsigned", size);
252b5132
RH
3438 }
3439
0cc9e1d3 3440 return NO_RELOC;
252b5132
RH
3441}
3442
47926f60
KH
3443/* Here we decide which fixups can be adjusted to make them relative to
3444 the beginning of the section instead of the symbol. Basically we need
3445 to make sure that the dynamic relocations are done correctly, so in
3446 some cases we force the original symbol to be used. */
3447
252b5132 3448int
e3bb37b5 3449tc_i386_fix_adjustable (fixS *fixP ATTRIBUTE_UNUSED)
252b5132 3450{
6d249963 3451#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 3452 if (!IS_ELF)
31312f95
AM
3453 return 1;
3454
a161fe53
AM
3455 /* Don't adjust pc-relative references to merge sections in 64-bit
3456 mode. */
3457 if (use_rela_relocations
3458 && (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0
3459 && fixP->fx_pcrel)
252b5132 3460 return 0;
31312f95 3461
8d01d9a9
AJ
3462 /* The x86_64 GOTPCREL are represented as 32bit PCrel relocations
3463 and changed later by validate_fix. */
3464 if (GOT_symbol && fixP->fx_subsy == GOT_symbol
3465 && fixP->fx_r_type == BFD_RELOC_32_PCREL)
3466 return 0;
3467
8fd4256d
L
3468 /* Adjust_reloc_syms doesn't know about the GOT. Need to keep symbol
3469 for size relocations. */
3470 if (fixP->fx_r_type == BFD_RELOC_SIZE32
3471 || fixP->fx_r_type == BFD_RELOC_SIZE64
3472 || fixP->fx_r_type == BFD_RELOC_386_GOTOFF
252b5132 3473 || fixP->fx_r_type == BFD_RELOC_386_GOT32
02a86693 3474 || fixP->fx_r_type == BFD_RELOC_386_GOT32X
13ae64f3
JJ
3475 || fixP->fx_r_type == BFD_RELOC_386_TLS_GD
3476 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDM
3477 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDO_32
3478 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE_32
37e55690
JJ
3479 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE
3480 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTIE
13ae64f3
JJ
3481 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE_32
3482 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE
67a4f2b7
AO
3483 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTDESC
3484 || fixP->fx_r_type == BFD_RELOC_386_TLS_DESC_CALL
3e73aa7c 3485 || fixP->fx_r_type == BFD_RELOC_X86_64_GOT32
80b3ee89 3486 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCREL
56ceb5b5
L
3487 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCRELX
3488 || fixP->fx_r_type == BFD_RELOC_X86_64_REX_GOTPCRELX
bffbf940
JJ
3489 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSGD
3490 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSLD
3491 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF32
d6ab8113 3492 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF64
bffbf940
JJ
3493 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF
3494 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF32
d6ab8113
JB
3495 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF64
3496 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTOFF64
67a4f2b7
AO
3497 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
3498 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSDESC_CALL
252b5132
RH
3499 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3500 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3501 return 0;
31312f95 3502#endif
252b5132
RH
3503 return 1;
3504}
252b5132 3505
b4cac588 3506static int
e3bb37b5 3507intel_float_operand (const char *mnemonic)
252b5132 3508{
9306ca4a
JB
3509 /* Note that the value returned is meaningful only for opcodes with (memory)
3510 operands, hence the code here is free to improperly handle opcodes that
3511 have no operands (for better performance and smaller code). */
3512
3513 if (mnemonic[0] != 'f')
3514 return 0; /* non-math */
3515
3516 switch (mnemonic[1])
3517 {
3518 /* fclex, fdecstp, fdisi, femms, feni, fincstp, finit, fsetpm, and
3519 the fs segment override prefix not currently handled because no
3520 call path can make opcodes without operands get here */
3521 case 'i':
3522 return 2 /* integer op */;
3523 case 'l':
3524 if (mnemonic[2] == 'd' && (mnemonic[3] == 'c' || mnemonic[3] == 'e'))
3525 return 3; /* fldcw/fldenv */
3526 break;
3527 case 'n':
3528 if (mnemonic[2] != 'o' /* fnop */)
3529 return 3; /* non-waiting control op */
3530 break;
3531 case 'r':
3532 if (mnemonic[2] == 's')
3533 return 3; /* frstor/frstpm */
3534 break;
3535 case 's':
3536 if (mnemonic[2] == 'a')
3537 return 3; /* fsave */
3538 if (mnemonic[2] == 't')
3539 {
3540 switch (mnemonic[3])
3541 {
3542 case 'c': /* fstcw */
3543 case 'd': /* fstdw */
3544 case 'e': /* fstenv */
3545 case 's': /* fsts[gw] */
3546 return 3;
3547 }
3548 }
3549 break;
3550 case 'x':
3551 if (mnemonic[2] == 'r' || mnemonic[2] == 's')
3552 return 0; /* fxsave/fxrstor are not really math ops */
3553 break;
3554 }
252b5132 3555
9306ca4a 3556 return 1;
252b5132
RH
3557}
3558
c0f3af97
L
3559/* Build the VEX prefix. */
3560
3561static void
d3ce72d0 3562build_vex_prefix (const insn_template *t)
c0f3af97
L
3563{
3564 unsigned int register_specifier;
3565 unsigned int implied_prefix;
3566 unsigned int vector_length;
03751133 3567 unsigned int w;
c0f3af97
L
3568
3569 /* Check register specifier. */
3570 if (i.vex.register_specifier)
43234a1e
L
3571 {
3572 register_specifier =
3573 ~register_number (i.vex.register_specifier) & 0xf;
3574 gas_assert ((i.vex.register_specifier->reg_flags & RegVRex) == 0);
3575 }
c0f3af97
L
3576 else
3577 register_specifier = 0xf;
3578
79f0fa25
L
3579 /* Use 2-byte VEX prefix by swapping destination and source operand
3580 if there are more than 1 register operand. */
3581 if (i.reg_operands > 1
3582 && i.vec_encoding != vex_encoding_vex3
86fa6981 3583 && i.dir_encoding == dir_encoding_default
fa99fab2 3584 && i.operands == i.reg_operands
dbbc8b7e 3585 && operand_type_equal (&i.types[0], &i.types[i.operands - 1])
7f399153 3586 && i.tm.opcode_modifier.vexopcode == VEX0F
dbbc8b7e 3587 && (i.tm.opcode_modifier.load || i.tm.opcode_modifier.d)
fa99fab2
L
3588 && i.rex == REX_B)
3589 {
3590 unsigned int xchg = i.operands - 1;
3591 union i386_op temp_op;
3592 i386_operand_type temp_type;
3593
3594 temp_type = i.types[xchg];
3595 i.types[xchg] = i.types[0];
3596 i.types[0] = temp_type;
3597 temp_op = i.op[xchg];
3598 i.op[xchg] = i.op[0];
3599 i.op[0] = temp_op;
3600
9c2799c2 3601 gas_assert (i.rm.mode == 3);
fa99fab2
L
3602
3603 i.rex = REX_R;
3604 xchg = i.rm.regmem;
3605 i.rm.regmem = i.rm.reg;
3606 i.rm.reg = xchg;
3607
dbbc8b7e
JB
3608 if (i.tm.opcode_modifier.d)
3609 i.tm.base_opcode ^= (i.tm.base_opcode & 0xee) != 0x6e
3610 ? Opcode_SIMD_FloatD : Opcode_SIMD_IntD;
3611 else /* Use the next insn. */
3612 i.tm = t[1];
fa99fab2
L
3613 }
3614
79dec6b7
JB
3615 /* Use 2-byte VEX prefix by swapping commutative source operands if there
3616 are no memory operands and at least 3 register ones. */
3617 if (i.reg_operands >= 3
3618 && i.vec_encoding != vex_encoding_vex3
3619 && i.reg_operands == i.operands - i.imm_operands
3620 && i.tm.opcode_modifier.vex
3621 && i.tm.opcode_modifier.commutative
3622 && (i.tm.opcode_modifier.sse2avx || optimize > 1)
3623 && i.rex == REX_B
3624 && i.vex.register_specifier
3625 && !(i.vex.register_specifier->reg_flags & RegRex))
3626 {
3627 unsigned int xchg = i.operands - i.reg_operands;
3628 union i386_op temp_op;
3629 i386_operand_type temp_type;
3630
3631 gas_assert (i.tm.opcode_modifier.vexopcode == VEX0F);
3632 gas_assert (!i.tm.opcode_modifier.sae);
3633 gas_assert (operand_type_equal (&i.types[i.operands - 2],
3634 &i.types[i.operands - 3]));
3635 gas_assert (i.rm.mode == 3);
3636
3637 temp_type = i.types[xchg];
3638 i.types[xchg] = i.types[xchg + 1];
3639 i.types[xchg + 1] = temp_type;
3640 temp_op = i.op[xchg];
3641 i.op[xchg] = i.op[xchg + 1];
3642 i.op[xchg + 1] = temp_op;
3643
3644 i.rex = 0;
3645 xchg = i.rm.regmem | 8;
3646 i.rm.regmem = ~register_specifier & 0xf;
3647 gas_assert (!(i.rm.regmem & 8));
3648 i.vex.register_specifier += xchg - i.rm.regmem;
3649 register_specifier = ~xchg & 0xf;
3650 }
3651
539f890d
L
3652 if (i.tm.opcode_modifier.vex == VEXScalar)
3653 vector_length = avxscalar;
10c17abd
JB
3654 else if (i.tm.opcode_modifier.vex == VEX256)
3655 vector_length = 1;
539f890d 3656 else
10c17abd 3657 {
56522fc5 3658 unsigned int op;
10c17abd 3659
c7213af9
L
3660 /* Determine vector length from the last multi-length vector
3661 operand. */
10c17abd 3662 vector_length = 0;
56522fc5 3663 for (op = t->operands; op--;)
10c17abd
JB
3664 if (t->operand_types[op].bitfield.xmmword
3665 && t->operand_types[op].bitfield.ymmword
3666 && i.types[op].bitfield.ymmword)
3667 {
3668 vector_length = 1;
3669 break;
3670 }
3671 }
c0f3af97 3672
8c190ce0 3673 switch ((i.tm.base_opcode >> (i.tm.opcode_length << 3)) & 0xff)
c0f3af97
L
3674 {
3675 case 0:
3676 implied_prefix = 0;
3677 break;
3678 case DATA_PREFIX_OPCODE:
3679 implied_prefix = 1;
3680 break;
3681 case REPE_PREFIX_OPCODE:
3682 implied_prefix = 2;
3683 break;
3684 case REPNE_PREFIX_OPCODE:
3685 implied_prefix = 3;
3686 break;
3687 default:
3688 abort ();
3689 }
3690
03751133
L
3691 /* Check the REX.W bit and VEXW. */
3692 if (i.tm.opcode_modifier.vexw == VEXWIG)
3693 w = (vexwig == vexw1 || (i.rex & REX_W)) ? 1 : 0;
3694 else if (i.tm.opcode_modifier.vexw)
3695 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3696 else
931d03b7 3697 w = (flag_code == CODE_64BIT ? i.rex & REX_W : vexwig == vexw1) ? 1 : 0;
03751133 3698
c0f3af97 3699 /* Use 2-byte VEX prefix if possible. */
03751133
L
3700 if (w == 0
3701 && i.vec_encoding != vex_encoding_vex3
86fa6981 3702 && i.tm.opcode_modifier.vexopcode == VEX0F
c0f3af97
L
3703 && (i.rex & (REX_W | REX_X | REX_B)) == 0)
3704 {
3705 /* 2-byte VEX prefix. */
3706 unsigned int r;
3707
3708 i.vex.length = 2;
3709 i.vex.bytes[0] = 0xc5;
3710
3711 /* Check the REX.R bit. */
3712 r = (i.rex & REX_R) ? 0 : 1;
3713 i.vex.bytes[1] = (r << 7
3714 | register_specifier << 3
3715 | vector_length << 2
3716 | implied_prefix);
3717 }
3718 else
3719 {
3720 /* 3-byte VEX prefix. */
03751133 3721 unsigned int m;
c0f3af97 3722
f88c9eb0 3723 i.vex.length = 3;
f88c9eb0 3724
7f399153 3725 switch (i.tm.opcode_modifier.vexopcode)
5dd85c99 3726 {
7f399153
L
3727 case VEX0F:
3728 m = 0x1;
80de6e00 3729 i.vex.bytes[0] = 0xc4;
7f399153
L
3730 break;
3731 case VEX0F38:
3732 m = 0x2;
80de6e00 3733 i.vex.bytes[0] = 0xc4;
7f399153
L
3734 break;
3735 case VEX0F3A:
3736 m = 0x3;
80de6e00 3737 i.vex.bytes[0] = 0xc4;
7f399153
L
3738 break;
3739 case XOP08:
5dd85c99
SP
3740 m = 0x8;
3741 i.vex.bytes[0] = 0x8f;
7f399153
L
3742 break;
3743 case XOP09:
f88c9eb0
SP
3744 m = 0x9;
3745 i.vex.bytes[0] = 0x8f;
7f399153
L
3746 break;
3747 case XOP0A:
f88c9eb0
SP
3748 m = 0xa;
3749 i.vex.bytes[0] = 0x8f;
7f399153
L
3750 break;
3751 default:
3752 abort ();
f88c9eb0 3753 }
c0f3af97 3754
c0f3af97
L
3755 /* The high 3 bits of the second VEX byte are 1's compliment
3756 of RXB bits from REX. */
3757 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | m;
3758
c0f3af97
L
3759 i.vex.bytes[2] = (w << 7
3760 | register_specifier << 3
3761 | vector_length << 2
3762 | implied_prefix);
3763 }
3764}
3765
e771e7c9
JB
3766static INLINE bfd_boolean
3767is_evex_encoding (const insn_template *t)
3768{
7091c612 3769 return t->opcode_modifier.evex || t->opcode_modifier.disp8memshift
e771e7c9 3770 || t->opcode_modifier.broadcast || t->opcode_modifier.masking
a80195f1 3771 || t->opcode_modifier.sae;
e771e7c9
JB
3772}
3773
7a8655d2
JB
3774static INLINE bfd_boolean
3775is_any_vex_encoding (const insn_template *t)
3776{
3777 return t->opcode_modifier.vex || t->opcode_modifier.vexopcode
3778 || is_evex_encoding (t);
3779}
3780
43234a1e
L
3781/* Build the EVEX prefix. */
3782
3783static void
3784build_evex_prefix (void)
3785{
3786 unsigned int register_specifier;
3787 unsigned int implied_prefix;
3788 unsigned int m, w;
3789 rex_byte vrex_used = 0;
3790
3791 /* Check register specifier. */
3792 if (i.vex.register_specifier)
3793 {
3794 gas_assert ((i.vrex & REX_X) == 0);
3795
3796 register_specifier = i.vex.register_specifier->reg_num;
3797 if ((i.vex.register_specifier->reg_flags & RegRex))
3798 register_specifier += 8;
3799 /* The upper 16 registers are encoded in the fourth byte of the
3800 EVEX prefix. */
3801 if (!(i.vex.register_specifier->reg_flags & RegVRex))
3802 i.vex.bytes[3] = 0x8;
3803 register_specifier = ~register_specifier & 0xf;
3804 }
3805 else
3806 {
3807 register_specifier = 0xf;
3808
3809 /* Encode upper 16 vector index register in the fourth byte of
3810 the EVEX prefix. */
3811 if (!(i.vrex & REX_X))
3812 i.vex.bytes[3] = 0x8;
3813 else
3814 vrex_used |= REX_X;
3815 }
3816
3817 switch ((i.tm.base_opcode >> 8) & 0xff)
3818 {
3819 case 0:
3820 implied_prefix = 0;
3821 break;
3822 case DATA_PREFIX_OPCODE:
3823 implied_prefix = 1;
3824 break;
3825 case REPE_PREFIX_OPCODE:
3826 implied_prefix = 2;
3827 break;
3828 case REPNE_PREFIX_OPCODE:
3829 implied_prefix = 3;
3830 break;
3831 default:
3832 abort ();
3833 }
3834
3835 /* 4 byte EVEX prefix. */
3836 i.vex.length = 4;
3837 i.vex.bytes[0] = 0x62;
3838
3839 /* mmmm bits. */
3840 switch (i.tm.opcode_modifier.vexopcode)
3841 {
3842 case VEX0F:
3843 m = 1;
3844 break;
3845 case VEX0F38:
3846 m = 2;
3847 break;
3848 case VEX0F3A:
3849 m = 3;
3850 break;
3851 default:
3852 abort ();
3853 break;
3854 }
3855
3856 /* The high 3 bits of the second EVEX byte are 1's compliment of RXB
3857 bits from REX. */
3858 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | m;
3859
3860 /* The fifth bit of the second EVEX byte is 1's compliment of the
3861 REX_R bit in VREX. */
3862 if (!(i.vrex & REX_R))
3863 i.vex.bytes[1] |= 0x10;
3864 else
3865 vrex_used |= REX_R;
3866
3867 if ((i.reg_operands + i.imm_operands) == i.operands)
3868 {
3869 /* When all operands are registers, the REX_X bit in REX is not
3870 used. We reuse it to encode the upper 16 registers, which is
3871 indicated by the REX_B bit in VREX. The REX_X bit is encoded
3872 as 1's compliment. */
3873 if ((i.vrex & REX_B))
3874 {
3875 vrex_used |= REX_B;
3876 i.vex.bytes[1] &= ~0x40;
3877 }
3878 }
3879
3880 /* EVEX instructions shouldn't need the REX prefix. */
3881 i.vrex &= ~vrex_used;
3882 gas_assert (i.vrex == 0);
3883
6865c043
L
3884 /* Check the REX.W bit and VEXW. */
3885 if (i.tm.opcode_modifier.vexw == VEXWIG)
3886 w = (evexwig == evexw1 || (i.rex & REX_W)) ? 1 : 0;
3887 else if (i.tm.opcode_modifier.vexw)
3888 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3889 else
931d03b7 3890 w = (flag_code == CODE_64BIT ? i.rex & REX_W : evexwig == evexw1) ? 1 : 0;
43234a1e
L
3891
3892 /* Encode the U bit. */
3893 implied_prefix |= 0x4;
3894
3895 /* The third byte of the EVEX prefix. */
3896 i.vex.bytes[2] = (w << 7 | register_specifier << 3 | implied_prefix);
3897
3898 /* The fourth byte of the EVEX prefix. */
3899 /* The zeroing-masking bit. */
3900 if (i.mask && i.mask->zeroing)
3901 i.vex.bytes[3] |= 0x80;
3902
3903 /* Don't always set the broadcast bit if there is no RC. */
3904 if (!i.rounding)
3905 {
3906 /* Encode the vector length. */
3907 unsigned int vec_length;
3908
e771e7c9
JB
3909 if (!i.tm.opcode_modifier.evex
3910 || i.tm.opcode_modifier.evex == EVEXDYN)
3911 {
56522fc5 3912 unsigned int op;
e771e7c9 3913
c7213af9
L
3914 /* Determine vector length from the last multi-length vector
3915 operand. */
56522fc5 3916 for (op = i.operands; op--;)
e771e7c9
JB
3917 if (i.tm.operand_types[op].bitfield.xmmword
3918 + i.tm.operand_types[op].bitfield.ymmword
3919 + i.tm.operand_types[op].bitfield.zmmword > 1)
3920 {
3921 if (i.types[op].bitfield.zmmword)
c7213af9
L
3922 {
3923 i.tm.opcode_modifier.evex = EVEX512;
3924 break;
3925 }
e771e7c9 3926 else if (i.types[op].bitfield.ymmword)
c7213af9
L
3927 {
3928 i.tm.opcode_modifier.evex = EVEX256;
3929 break;
3930 }
e771e7c9 3931 else if (i.types[op].bitfield.xmmword)
c7213af9
L
3932 {
3933 i.tm.opcode_modifier.evex = EVEX128;
3934 break;
3935 }
625cbd7a
JB
3936 else if (i.broadcast && (int) op == i.broadcast->operand)
3937 {
4a1b91ea 3938 switch (i.broadcast->bytes)
625cbd7a
JB
3939 {
3940 case 64:
3941 i.tm.opcode_modifier.evex = EVEX512;
3942 break;
3943 case 32:
3944 i.tm.opcode_modifier.evex = EVEX256;
3945 break;
3946 case 16:
3947 i.tm.opcode_modifier.evex = EVEX128;
3948 break;
3949 default:
c7213af9 3950 abort ();
625cbd7a 3951 }
c7213af9 3952 break;
625cbd7a 3953 }
e771e7c9 3954 }
c7213af9 3955
56522fc5 3956 if (op >= MAX_OPERANDS)
c7213af9 3957 abort ();
e771e7c9
JB
3958 }
3959
43234a1e
L
3960 switch (i.tm.opcode_modifier.evex)
3961 {
3962 case EVEXLIG: /* LL' is ignored */
3963 vec_length = evexlig << 5;
3964 break;
3965 case EVEX128:
3966 vec_length = 0 << 5;
3967 break;
3968 case EVEX256:
3969 vec_length = 1 << 5;
3970 break;
3971 case EVEX512:
3972 vec_length = 2 << 5;
3973 break;
3974 default:
3975 abort ();
3976 break;
3977 }
3978 i.vex.bytes[3] |= vec_length;
3979 /* Encode the broadcast bit. */
3980 if (i.broadcast)
3981 i.vex.bytes[3] |= 0x10;
3982 }
3983 else
3984 {
3985 if (i.rounding->type != saeonly)
3986 i.vex.bytes[3] |= 0x10 | (i.rounding->type << 5);
3987 else
d3d3c6db 3988 i.vex.bytes[3] |= 0x10 | (evexrcig << 5);
43234a1e
L
3989 }
3990
3991 if (i.mask && i.mask->mask)
3992 i.vex.bytes[3] |= i.mask->mask->reg_num;
3993}
3994
65da13b5
L
3995static void
3996process_immext (void)
3997{
3998 expressionS *exp;
3999
c0f3af97 4000 /* These AMD 3DNow! and SSE2 instructions have an opcode suffix
65da13b5
L
4001 which is coded in the same place as an 8-bit immediate field
4002 would be. Here we fake an 8-bit immediate operand from the
4003 opcode suffix stored in tm.extension_opcode.
4004
c1e679ec 4005 AVX instructions also use this encoding, for some of
c0f3af97 4006 3 argument instructions. */
65da13b5 4007
43234a1e 4008 gas_assert (i.imm_operands <= 1
7ab9ffdd 4009 && (i.operands <= 2
7a8655d2 4010 || (is_any_vex_encoding (&i.tm)
7ab9ffdd 4011 && i.operands <= 4)));
65da13b5
L
4012
4013 exp = &im_expressions[i.imm_operands++];
4014 i.op[i.operands].imms = exp;
4015 i.types[i.operands] = imm8;
4016 i.operands++;
4017 exp->X_op = O_constant;
4018 exp->X_add_number = i.tm.extension_opcode;
4019 i.tm.extension_opcode = None;
4020}
4021
42164a71
L
4022
4023static int
4024check_hle (void)
4025{
4026 switch (i.tm.opcode_modifier.hleprefixok)
4027 {
4028 default:
4029 abort ();
82c2def5 4030 case HLEPrefixNone:
165de32a
L
4031 as_bad (_("invalid instruction `%s' after `%s'"),
4032 i.tm.name, i.hle_prefix);
42164a71 4033 return 0;
82c2def5 4034 case HLEPrefixLock:
42164a71
L
4035 if (i.prefix[LOCK_PREFIX])
4036 return 1;
165de32a 4037 as_bad (_("missing `lock' with `%s'"), i.hle_prefix);
42164a71 4038 return 0;
82c2def5 4039 case HLEPrefixAny:
42164a71 4040 return 1;
82c2def5 4041 case HLEPrefixRelease:
42164a71
L
4042 if (i.prefix[HLE_PREFIX] != XRELEASE_PREFIX_OPCODE)
4043 {
4044 as_bad (_("instruction `%s' after `xacquire' not allowed"),
4045 i.tm.name);
4046 return 0;
4047 }
8dc0818e 4048 if (i.mem_operands == 0 || !(i.flags[i.operands - 1] & Operand_Mem))
42164a71
L
4049 {
4050 as_bad (_("memory destination needed for instruction `%s'"
4051 " after `xrelease'"), i.tm.name);
4052 return 0;
4053 }
4054 return 1;
4055 }
4056}
4057
b6f8c7c4
L
4058/* Try the shortest encoding by shortening operand size. */
4059
4060static void
4061optimize_encoding (void)
4062{
a0a1771e 4063 unsigned int j;
b6f8c7c4
L
4064
4065 if (optimize_for_space
72aea328 4066 && !is_any_vex_encoding (&i.tm)
b6f8c7c4
L
4067 && i.reg_operands == 1
4068 && i.imm_operands == 1
4069 && !i.types[1].bitfield.byte
4070 && i.op[0].imms->X_op == O_constant
4071 && fits_in_imm7 (i.op[0].imms->X_add_number)
72aea328 4072 && (i.tm.base_opcode == 0xa8
b6f8c7c4
L
4073 || (i.tm.base_opcode == 0xf6
4074 && i.tm.extension_opcode == 0x0)))
4075 {
4076 /* Optimize: -Os:
4077 test $imm7, %r64/%r32/%r16 -> test $imm7, %r8
4078 */
4079 unsigned int base_regnum = i.op[1].regs->reg_num;
4080 if (flag_code == CODE_64BIT || base_regnum < 4)
4081 {
4082 i.types[1].bitfield.byte = 1;
4083 /* Ignore the suffix. */
4084 i.suffix = 0;
7697afb6
JB
4085 /* Convert to byte registers. */
4086 if (i.types[1].bitfield.word)
4087 j = 16;
4088 else if (i.types[1].bitfield.dword)
4089 j = 32;
4090 else
4091 j = 48;
4092 if (!(i.op[1].regs->reg_flags & RegRex) && base_regnum < 4)
4093 j += 8;
4094 i.op[1].regs -= j;
b6f8c7c4
L
4095 }
4096 }
4097 else if (flag_code == CODE_64BIT
72aea328 4098 && !is_any_vex_encoding (&i.tm)
d3d50934
L
4099 && ((i.types[1].bitfield.qword
4100 && i.reg_operands == 1
b6f8c7c4
L
4101 && i.imm_operands == 1
4102 && i.op[0].imms->X_op == O_constant
507916b8 4103 && ((i.tm.base_opcode == 0xb8
b6f8c7c4
L
4104 && i.tm.extension_opcode == None
4105 && fits_in_unsigned_long (i.op[0].imms->X_add_number))
4106 || (fits_in_imm31 (i.op[0].imms->X_add_number)
72aea328
JB
4107 && ((i.tm.base_opcode == 0x24
4108 || i.tm.base_opcode == 0xa8)
b6f8c7c4
L
4109 || (i.tm.base_opcode == 0x80
4110 && i.tm.extension_opcode == 0x4)
4111 || ((i.tm.base_opcode == 0xf6
507916b8 4112 || (i.tm.base_opcode | 1) == 0xc7)
b8364fa7
JB
4113 && i.tm.extension_opcode == 0x0)))
4114 || (fits_in_imm7 (i.op[0].imms->X_add_number)
4115 && i.tm.base_opcode == 0x83
4116 && i.tm.extension_opcode == 0x4)))
d3d50934
L
4117 || (i.types[0].bitfield.qword
4118 && ((i.reg_operands == 2
4119 && i.op[0].regs == i.op[1].regs
72aea328
JB
4120 && (i.tm.base_opcode == 0x30
4121 || i.tm.base_opcode == 0x28))
d3d50934
L
4122 || (i.reg_operands == 1
4123 && i.operands == 1
72aea328 4124 && i.tm.base_opcode == 0x30)))))
b6f8c7c4
L
4125 {
4126 /* Optimize: -O:
4127 andq $imm31, %r64 -> andl $imm31, %r32
b8364fa7 4128 andq $imm7, %r64 -> andl $imm7, %r32
b6f8c7c4
L
4129 testq $imm31, %r64 -> testl $imm31, %r32
4130 xorq %r64, %r64 -> xorl %r32, %r32
4131 subq %r64, %r64 -> subl %r32, %r32
4132 movq $imm31, %r64 -> movl $imm31, %r32
4133 movq $imm32, %r64 -> movl $imm32, %r32
4134 */
4135 i.tm.opcode_modifier.norex64 = 1;
507916b8 4136 if (i.tm.base_opcode == 0xb8 || (i.tm.base_opcode | 1) == 0xc7)
b6f8c7c4
L
4137 {
4138 /* Handle
4139 movq $imm31, %r64 -> movl $imm31, %r32
4140 movq $imm32, %r64 -> movl $imm32, %r32
4141 */
4142 i.tm.operand_types[0].bitfield.imm32 = 1;
4143 i.tm.operand_types[0].bitfield.imm32s = 0;
4144 i.tm.operand_types[0].bitfield.imm64 = 0;
4145 i.types[0].bitfield.imm32 = 1;
4146 i.types[0].bitfield.imm32s = 0;
4147 i.types[0].bitfield.imm64 = 0;
4148 i.types[1].bitfield.dword = 1;
4149 i.types[1].bitfield.qword = 0;
507916b8 4150 if ((i.tm.base_opcode | 1) == 0xc7)
b6f8c7c4
L
4151 {
4152 /* Handle
4153 movq $imm31, %r64 -> movl $imm31, %r32
4154 */
507916b8 4155 i.tm.base_opcode = 0xb8;
b6f8c7c4 4156 i.tm.extension_opcode = None;
507916b8 4157 i.tm.opcode_modifier.w = 0;
b6f8c7c4
L
4158 i.tm.opcode_modifier.modrm = 0;
4159 }
4160 }
4161 }
5641ec01
JB
4162 else if (optimize > 1
4163 && !optimize_for_space
72aea328 4164 && !is_any_vex_encoding (&i.tm)
5641ec01
JB
4165 && i.reg_operands == 2
4166 && i.op[0].regs == i.op[1].regs
4167 && ((i.tm.base_opcode & ~(Opcode_D | 1)) == 0x8
4168 || (i.tm.base_opcode & ~(Opcode_D | 1)) == 0x20)
4169 && (flag_code != CODE_64BIT || !i.types[0].bitfield.dword))
4170 {
4171 /* Optimize: -O2:
4172 andb %rN, %rN -> testb %rN, %rN
4173 andw %rN, %rN -> testw %rN, %rN
4174 andq %rN, %rN -> testq %rN, %rN
4175 orb %rN, %rN -> testb %rN, %rN
4176 orw %rN, %rN -> testw %rN, %rN
4177 orq %rN, %rN -> testq %rN, %rN
4178
4179 and outside of 64-bit mode
4180
4181 andl %rN, %rN -> testl %rN, %rN
4182 orl %rN, %rN -> testl %rN, %rN
4183 */
4184 i.tm.base_opcode = 0x84 | (i.tm.base_opcode & 1);
4185 }
99112332 4186 else if (i.reg_operands == 3
b6f8c7c4
L
4187 && i.op[0].regs == i.op[1].regs
4188 && !i.types[2].bitfield.xmmword
4189 && (i.tm.opcode_modifier.vex
7a69eac3 4190 || ((!i.mask || i.mask->zeroing)
b6f8c7c4 4191 && !i.rounding
e771e7c9 4192 && is_evex_encoding (&i.tm)
80c34c38 4193 && (i.vec_encoding != vex_encoding_evex
dd22218c 4194 || cpu_arch_isa_flags.bitfield.cpuavx512vl
80c34c38 4195 || i.tm.cpu_flags.bitfield.cpuavx512vl
7091c612 4196 || (i.tm.operand_types[2].bitfield.zmmword
dd22218c 4197 && i.types[2].bitfield.ymmword))))
b6f8c7c4
L
4198 && ((i.tm.base_opcode == 0x55
4199 || i.tm.base_opcode == 0x6655
4200 || i.tm.base_opcode == 0x66df
4201 || i.tm.base_opcode == 0x57
4202 || i.tm.base_opcode == 0x6657
8305403a
L
4203 || i.tm.base_opcode == 0x66ef
4204 || i.tm.base_opcode == 0x66f8
4205 || i.tm.base_opcode == 0x66f9
4206 || i.tm.base_opcode == 0x66fa
1424ad86
JB
4207 || i.tm.base_opcode == 0x66fb
4208 || i.tm.base_opcode == 0x42
4209 || i.tm.base_opcode == 0x6642
4210 || i.tm.base_opcode == 0x47
4211 || i.tm.base_opcode == 0x6647)
b6f8c7c4
L
4212 && i.tm.extension_opcode == None))
4213 {
99112332 4214 /* Optimize: -O1:
8305403a
L
4215 VOP, one of vandnps, vandnpd, vxorps, vxorpd, vpsubb, vpsubd,
4216 vpsubq and vpsubw:
b6f8c7c4
L
4217 EVEX VOP %zmmM, %zmmM, %zmmN
4218 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4219 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4220 EVEX VOP %ymmM, %ymmM, %ymmN
4221 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4222 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4223 VEX VOP %ymmM, %ymmM, %ymmN
4224 -> VEX VOP %xmmM, %xmmM, %xmmN
4225 VOP, one of vpandn and vpxor:
4226 VEX VOP %ymmM, %ymmM, %ymmN
4227 -> VEX VOP %xmmM, %xmmM, %xmmN
4228 VOP, one of vpandnd and vpandnq:
4229 EVEX VOP %zmmM, %zmmM, %zmmN
4230 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4231 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4232 EVEX VOP %ymmM, %ymmM, %ymmN
4233 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4234 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4235 VOP, one of vpxord and vpxorq:
4236 EVEX VOP %zmmM, %zmmM, %zmmN
4237 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4238 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4239 EVEX VOP %ymmM, %ymmM, %ymmN
4240 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4241 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
1424ad86
JB
4242 VOP, one of kxord and kxorq:
4243 VEX VOP %kM, %kM, %kN
4244 -> VEX kxorw %kM, %kM, %kN
4245 VOP, one of kandnd and kandnq:
4246 VEX VOP %kM, %kM, %kN
4247 -> VEX kandnw %kM, %kM, %kN
b6f8c7c4 4248 */
e771e7c9 4249 if (is_evex_encoding (&i.tm))
b6f8c7c4 4250 {
7b1d7ca1 4251 if (i.vec_encoding != vex_encoding_evex)
b6f8c7c4
L
4252 {
4253 i.tm.opcode_modifier.vex = VEX128;
4254 i.tm.opcode_modifier.vexw = VEXW0;
4255 i.tm.opcode_modifier.evex = 0;
4256 }
7b1d7ca1 4257 else if (optimize > 1)
dd22218c
L
4258 i.tm.opcode_modifier.evex = EVEX128;
4259 else
4260 return;
b6f8c7c4 4261 }
f74a6307 4262 else if (i.tm.operand_types[0].bitfield.class == RegMask)
1424ad86
JB
4263 {
4264 i.tm.base_opcode &= 0xff;
4265 i.tm.opcode_modifier.vexw = VEXW0;
4266 }
b6f8c7c4
L
4267 else
4268 i.tm.opcode_modifier.vex = VEX128;
4269
4270 if (i.tm.opcode_modifier.vex)
4271 for (j = 0; j < 3; j++)
4272 {
4273 i.types[j].bitfield.xmmword = 1;
4274 i.types[j].bitfield.ymmword = 0;
4275 }
4276 }
392a5972 4277 else if (i.vec_encoding != vex_encoding_evex
97ed31ae 4278 && !i.types[0].bitfield.zmmword
392a5972 4279 && !i.types[1].bitfield.zmmword
97ed31ae 4280 && !i.mask
a0a1771e 4281 && !i.broadcast
97ed31ae 4282 && is_evex_encoding (&i.tm)
392a5972
L
4283 && ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x666f
4284 || (i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0xf36f
a0a1771e
JB
4285 || (i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0xf26f
4286 || (i.tm.base_opcode & ~4) == 0x66db
4287 || (i.tm.base_opcode & ~4) == 0x66eb)
97ed31ae
L
4288 && i.tm.extension_opcode == None)
4289 {
4290 /* Optimize: -O1:
4291 VOP, one of vmovdqa32, vmovdqa64, vmovdqu8, vmovdqu16,
4292 vmovdqu32 and vmovdqu64:
4293 EVEX VOP %xmmM, %xmmN
4294 -> VEX vmovdqa|vmovdqu %xmmM, %xmmN (M and N < 16)
4295 EVEX VOP %ymmM, %ymmN
4296 -> VEX vmovdqa|vmovdqu %ymmM, %ymmN (M and N < 16)
4297 EVEX VOP %xmmM, mem
4298 -> VEX vmovdqa|vmovdqu %xmmM, mem (M < 16)
4299 EVEX VOP %ymmM, mem
4300 -> VEX vmovdqa|vmovdqu %ymmM, mem (M < 16)
4301 EVEX VOP mem, %xmmN
4302 -> VEX mvmovdqa|vmovdquem, %xmmN (N < 16)
4303 EVEX VOP mem, %ymmN
4304 -> VEX vmovdqa|vmovdqu mem, %ymmN (N < 16)
a0a1771e
JB
4305 VOP, one of vpand, vpandn, vpor, vpxor:
4306 EVEX VOP{d,q} %xmmL, %xmmM, %xmmN
4307 -> VEX VOP %xmmL, %xmmM, %xmmN (L, M, and N < 16)
4308 EVEX VOP{d,q} %ymmL, %ymmM, %ymmN
4309 -> VEX VOP %ymmL, %ymmM, %ymmN (L, M, and N < 16)
4310 EVEX VOP{d,q} mem, %xmmM, %xmmN
4311 -> VEX VOP mem, %xmmM, %xmmN (M and N < 16)
4312 EVEX VOP{d,q} mem, %ymmM, %ymmN
4313 -> VEX VOP mem, %ymmM, %ymmN (M and N < 16)
97ed31ae 4314 */
a0a1771e 4315 for (j = 0; j < i.operands; j++)
392a5972
L
4316 if (operand_type_check (i.types[j], disp)
4317 && i.op[j].disps->X_op == O_constant)
4318 {
4319 /* Since the VEX prefix has 2 or 3 bytes, the EVEX prefix
4320 has 4 bytes, EVEX Disp8 has 1 byte and VEX Disp32 has 4
4321 bytes, we choose EVEX Disp8 over VEX Disp32. */
4322 int evex_disp8, vex_disp8;
4323 unsigned int memshift = i.memshift;
4324 offsetT n = i.op[j].disps->X_add_number;
4325
4326 evex_disp8 = fits_in_disp8 (n);
4327 i.memshift = 0;
4328 vex_disp8 = fits_in_disp8 (n);
4329 if (evex_disp8 != vex_disp8)
4330 {
4331 i.memshift = memshift;
4332 return;
4333 }
4334
4335 i.types[j].bitfield.disp8 = vex_disp8;
4336 break;
4337 }
4338 if ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0xf26f)
4339 i.tm.base_opcode ^= 0xf36f ^ 0xf26f;
97ed31ae
L
4340 i.tm.opcode_modifier.vex
4341 = i.types[0].bitfield.ymmword ? VEX256 : VEX128;
4342 i.tm.opcode_modifier.vexw = VEXW0;
79dec6b7
JB
4343 /* VPAND, VPOR, and VPXOR are commutative. */
4344 if (i.reg_operands == 3 && i.tm.base_opcode != 0x66df)
4345 i.tm.opcode_modifier.commutative = 1;
97ed31ae
L
4346 i.tm.opcode_modifier.evex = 0;
4347 i.tm.opcode_modifier.masking = 0;
a0a1771e 4348 i.tm.opcode_modifier.broadcast = 0;
97ed31ae
L
4349 i.tm.opcode_modifier.disp8memshift = 0;
4350 i.memshift = 0;
a0a1771e
JB
4351 if (j < i.operands)
4352 i.types[j].bitfield.disp8
4353 = fits_in_disp8 (i.op[j].disps->X_add_number);
97ed31ae 4354 }
b6f8c7c4
L
4355}
4356
ae531041
L
4357/* Return non-zero for load instruction. */
4358
4359static int
4360load_insn_p (void)
4361{
4362 unsigned int dest;
4363 int any_vex_p = is_any_vex_encoding (&i.tm);
4364 unsigned int base_opcode = i.tm.base_opcode | 1;
4365
4366 if (!any_vex_p)
4367 {
a09f656b 4368 /* Anysize insns: lea, invlpg, clflush, prefetchnta, prefetcht0,
4369 prefetcht1, prefetcht2, prefetchtw, bndmk, bndcl, bndcu, bndcn,
4370 bndstx, bndldx, prefetchwt1, clflushopt, clwb, cldemote. */
4371 if (i.tm.opcode_modifier.anysize)
ae531041
L
4372 return 0;
4373
a09f656b 4374 /* pop, popf, popa. */
4375 if (strcmp (i.tm.name, "pop") == 0
4376 || i.tm.base_opcode == 0x9d
4377 || i.tm.base_opcode == 0x61)
ae531041
L
4378 return 1;
4379
4380 /* movs, cmps, lods, scas. */
4381 if ((i.tm.base_opcode | 0xb) == 0xaf)
4382 return 1;
4383
a09f656b 4384 /* outs, xlatb. */
4385 if (base_opcode == 0x6f
4386 || i.tm.base_opcode == 0xd7)
ae531041 4387 return 1;
a09f656b 4388 /* NB: For AMD-specific insns with implicit memory operands,
4389 they're intentionally not covered. */
ae531041
L
4390 }
4391
4392 /* No memory operand. */
4393 if (!i.mem_operands)
4394 return 0;
4395
4396 if (any_vex_p)
4397 {
4398 /* vldmxcsr. */
4399 if (i.tm.base_opcode == 0xae
4400 && i.tm.opcode_modifier.vex
4401 && i.tm.opcode_modifier.vexopcode == VEX0F
4402 && i.tm.extension_opcode == 2)
4403 return 1;
4404 }
4405 else
4406 {
4407 /* test, not, neg, mul, imul, div, idiv. */
4408 if ((i.tm.base_opcode == 0xf6 || i.tm.base_opcode == 0xf7)
4409 && i.tm.extension_opcode != 1)
4410 return 1;
4411
4412 /* inc, dec. */
4413 if (base_opcode == 0xff && i.tm.extension_opcode <= 1)
4414 return 1;
4415
4416 /* add, or, adc, sbb, and, sub, xor, cmp. */
4417 if (i.tm.base_opcode >= 0x80 && i.tm.base_opcode <= 0x83)
4418 return 1;
4419
4420 /* bt, bts, btr, btc. */
4421 if (i.tm.base_opcode == 0xfba
4422 && (i.tm.extension_opcode >= 4 && i.tm.extension_opcode <= 7))
4423 return 1;
4424
4425 /* rol, ror, rcl, rcr, shl/sal, shr, sar. */
4426 if ((base_opcode == 0xc1
4427 || (i.tm.base_opcode >= 0xd0 && i.tm.base_opcode <= 0xd3))
4428 && i.tm.extension_opcode != 6)
4429 return 1;
4430
4431 /* cmpxchg8b, cmpxchg16b, xrstors. */
4432 if (i.tm.base_opcode == 0xfc7
4433 && (i.tm.extension_opcode == 1 || i.tm.extension_opcode == 3))
4434 return 1;
4435
4436 /* fxrstor, ldmxcsr, xrstor. */
4437 if (i.tm.base_opcode == 0xfae
4438 && (i.tm.extension_opcode == 1
4439 || i.tm.extension_opcode == 2
4440 || i.tm.extension_opcode == 5))
4441 return 1;
4442
4443 /* lgdt, lidt, lmsw. */
4444 if (i.tm.base_opcode == 0xf01
4445 && (i.tm.extension_opcode == 2
4446 || i.tm.extension_opcode == 3
4447 || i.tm.extension_opcode == 6))
4448 return 1;
4449
4450 /* vmptrld */
4451 if (i.tm.base_opcode == 0xfc7
4452 && i.tm.extension_opcode == 6)
4453 return 1;
4454
4455 /* Check for x87 instructions. */
4456 if (i.tm.base_opcode >= 0xd8 && i.tm.base_opcode <= 0xdf)
4457 {
4458 /* Skip fst, fstp, fstenv, fstcw. */
4459 if (i.tm.base_opcode == 0xd9
4460 && (i.tm.extension_opcode == 2
4461 || i.tm.extension_opcode == 3
4462 || i.tm.extension_opcode == 6
4463 || i.tm.extension_opcode == 7))
4464 return 0;
4465
4466 /* Skip fisttp, fist, fistp, fstp. */
4467 if (i.tm.base_opcode == 0xdb
4468 && (i.tm.extension_opcode == 1
4469 || i.tm.extension_opcode == 2
4470 || i.tm.extension_opcode == 3
4471 || i.tm.extension_opcode == 7))
4472 return 0;
4473
4474 /* Skip fisttp, fst, fstp, fsave, fstsw. */
4475 if (i.tm.base_opcode == 0xdd
4476 && (i.tm.extension_opcode == 1
4477 || i.tm.extension_opcode == 2
4478 || i.tm.extension_opcode == 3
4479 || i.tm.extension_opcode == 6
4480 || i.tm.extension_opcode == 7))
4481 return 0;
4482
4483 /* Skip fisttp, fist, fistp, fbstp, fistp. */
4484 if (i.tm.base_opcode == 0xdf
4485 && (i.tm.extension_opcode == 1
4486 || i.tm.extension_opcode == 2
4487 || i.tm.extension_opcode == 3
4488 || i.tm.extension_opcode == 6
4489 || i.tm.extension_opcode == 7))
4490 return 0;
4491
4492 return 1;
4493 }
4494 }
4495
4496 dest = i.operands - 1;
4497
4498 /* Check fake imm8 operand and 3 source operands. */
4499 if ((i.tm.opcode_modifier.immext
4500 || i.tm.opcode_modifier.vexsources == VEX3SOURCES)
4501 && i.types[dest].bitfield.imm8)
4502 dest--;
4503
4504 /* add, or, adc, sbb, and, sub, xor, cmp, test, xchg, xadd */
4505 if (!any_vex_p
4506 && (base_opcode == 0x1
4507 || base_opcode == 0x9
4508 || base_opcode == 0x11
4509 || base_opcode == 0x19
4510 || base_opcode == 0x21
4511 || base_opcode == 0x29
4512 || base_opcode == 0x31
4513 || base_opcode == 0x39
4514 || (i.tm.base_opcode >= 0x84 && i.tm.base_opcode <= 0x87)
4515 || base_opcode == 0xfc1))
4516 return 1;
4517
4518 /* Check for load instruction. */
4519 return (i.types[dest].bitfield.class != ClassNone
4520 || i.types[dest].bitfield.instance == Accum);
4521}
4522
4523/* Output lfence, 0xfaee8, after instruction. */
4524
4525static void
4526insert_lfence_after (void)
4527{
4528 if (lfence_after_load && load_insn_p ())
4529 {
a09f656b 4530 /* There are also two REP string instructions that require
4531 special treatment. Specifically, the compare string (CMPS)
4532 and scan string (SCAS) instructions set EFLAGS in a manner
4533 that depends on the data being compared/scanned. When used
4534 with a REP prefix, the number of iterations may therefore
4535 vary depending on this data. If the data is a program secret
4536 chosen by the adversary using an LVI method,
4537 then this data-dependent behavior may leak some aspect
4538 of the secret. */
4539 if (((i.tm.base_opcode | 0x1) == 0xa7
4540 || (i.tm.base_opcode | 0x1) == 0xaf)
4541 && i.prefix[REP_PREFIX])
4542 {
4543 as_warn (_("`%s` changes flags which would affect control flow behavior"),
4544 i.tm.name);
4545 }
ae531041
L
4546 char *p = frag_more (3);
4547 *p++ = 0xf;
4548 *p++ = 0xae;
4549 *p = 0xe8;
4550 }
4551}
4552
4553/* Output lfence, 0xfaee8, before instruction. */
4554
4555static void
4556insert_lfence_before (void)
4557{
4558 char *p;
4559
4560 if (is_any_vex_encoding (&i.tm))
4561 return;
4562
4563 if (i.tm.base_opcode == 0xff
4564 && (i.tm.extension_opcode == 2 || i.tm.extension_opcode == 4))
4565 {
4566 /* Insert lfence before indirect branch if needed. */
4567
4568 if (lfence_before_indirect_branch == lfence_branch_none)
4569 return;
4570
4571 if (i.operands != 1)
4572 abort ();
4573
4574 if (i.reg_operands == 1)
4575 {
4576 /* Indirect branch via register. Don't insert lfence with
4577 -mlfence-after-load=yes. */
4578 if (lfence_after_load
4579 || lfence_before_indirect_branch == lfence_branch_memory)
4580 return;
4581 }
4582 else if (i.mem_operands == 1
4583 && lfence_before_indirect_branch != lfence_branch_register)
4584 {
4585 as_warn (_("indirect `%s` with memory operand should be avoided"),
4586 i.tm.name);
4587 return;
4588 }
4589 else
4590 return;
4591
4592 if (last_insn.kind != last_insn_other
4593 && last_insn.seg == now_seg)
4594 {
4595 as_warn_where (last_insn.file, last_insn.line,
4596 _("`%s` skips -mlfence-before-indirect-branch on `%s`"),
4597 last_insn.name, i.tm.name);
4598 return;
4599 }
4600
4601 p = frag_more (3);
4602 *p++ = 0xf;
4603 *p++ = 0xae;
4604 *p = 0xe8;
4605 return;
4606 }
4607
503648e4 4608 /* Output or/not/shl and lfence before near ret. */
ae531041
L
4609 if (lfence_before_ret != lfence_before_ret_none
4610 && (i.tm.base_opcode == 0xc2
503648e4 4611 || i.tm.base_opcode == 0xc3))
ae531041
L
4612 {
4613 if (last_insn.kind != last_insn_other
4614 && last_insn.seg == now_seg)
4615 {
4616 as_warn_where (last_insn.file, last_insn.line,
4617 _("`%s` skips -mlfence-before-ret on `%s`"),
4618 last_insn.name, i.tm.name);
4619 return;
4620 }
a09f656b 4621
a09f656b 4622 /* Near ret ingore operand size override under CPU64. */
503648e4 4623 char prefix = flag_code == CODE_64BIT
4624 ? 0x48
4625 : i.prefix[DATA_PREFIX] ? 0x66 : 0x0;
a09f656b 4626
4627 if (lfence_before_ret == lfence_before_ret_not)
4628 {
4629 /* not: 0xf71424, may add prefix
4630 for operand size override or 64-bit code. */
4631 p = frag_more ((prefix ? 2 : 0) + 6 + 3);
4632 if (prefix)
4633 *p++ = prefix;
ae531041
L
4634 *p++ = 0xf7;
4635 *p++ = 0x14;
4636 *p++ = 0x24;
a09f656b 4637 if (prefix)
4638 *p++ = prefix;
ae531041
L
4639 *p++ = 0xf7;
4640 *p++ = 0x14;
4641 *p++ = 0x24;
4642 }
a09f656b 4643 else
4644 {
4645 p = frag_more ((prefix ? 1 : 0) + 4 + 3);
4646 if (prefix)
4647 *p++ = prefix;
4648 if (lfence_before_ret == lfence_before_ret_or)
4649 {
4650 /* or: 0x830c2400, may add prefix
4651 for operand size override or 64-bit code. */
4652 *p++ = 0x83;
4653 *p++ = 0x0c;
4654 }
4655 else
4656 {
4657 /* shl: 0xc1242400, may add prefix
4658 for operand size override or 64-bit code. */
4659 *p++ = 0xc1;
4660 *p++ = 0x24;
4661 }
4662
4663 *p++ = 0x24;
4664 *p++ = 0x0;
4665 }
4666
ae531041
L
4667 *p++ = 0xf;
4668 *p++ = 0xae;
4669 *p = 0xe8;
4670 }
4671}
4672
252b5132
RH
4673/* This is the guts of the machine-dependent assembler. LINE points to a
4674 machine dependent instruction. This function is supposed to emit
4675 the frags/bytes it assembles to. */
4676
4677void
65da13b5 4678md_assemble (char *line)
252b5132 4679{
40fb9820 4680 unsigned int j;
83b16ac6 4681 char mnemonic[MAX_MNEM_SIZE], mnem_suffix;
d3ce72d0 4682 const insn_template *t;
252b5132 4683
47926f60 4684 /* Initialize globals. */
252b5132
RH
4685 memset (&i, '\0', sizeof (i));
4686 for (j = 0; j < MAX_OPERANDS; j++)
1ae12ab7 4687 i.reloc[j] = NO_RELOC;
252b5132
RH
4688 memset (disp_expressions, '\0', sizeof (disp_expressions));
4689 memset (im_expressions, '\0', sizeof (im_expressions));
ce8a8b2f 4690 save_stack_p = save_stack;
252b5132
RH
4691
4692 /* First parse an instruction mnemonic & call i386_operand for the operands.
4693 We assume that the scrubber has arranged it so that line[0] is the valid
47926f60 4694 start of a (possibly prefixed) mnemonic. */
252b5132 4695
29b0f896
AM
4696 line = parse_insn (line, mnemonic);
4697 if (line == NULL)
4698 return;
83b16ac6 4699 mnem_suffix = i.suffix;
252b5132 4700
29b0f896 4701 line = parse_operands (line, mnemonic);
ee86248c 4702 this_operand = -1;
8325cc63
JB
4703 xfree (i.memop1_string);
4704 i.memop1_string = NULL;
29b0f896
AM
4705 if (line == NULL)
4706 return;
252b5132 4707
29b0f896
AM
4708 /* Now we've parsed the mnemonic into a set of templates, and have the
4709 operands at hand. */
4710
b630c145
JB
4711 /* All Intel opcodes have reversed operands except for "bound", "enter",
4712 "monitor*", "mwait*", "tpause", and "umwait". We also don't reverse
4713 intersegment "jmp" and "call" instructions with 2 immediate operands so
4714 that the immediate segment precedes the offset, as it does when in AT&T
4715 mode. */
4d456e3d
L
4716 if (intel_syntax
4717 && i.operands > 1
29b0f896 4718 && (strcmp (mnemonic, "bound") != 0)
30123838 4719 && (strcmp (mnemonic, "invlpga") != 0)
eedb0f2c
JB
4720 && (strncmp (mnemonic, "monitor", 7) != 0)
4721 && (strncmp (mnemonic, "mwait", 5) != 0)
b630c145
JB
4722 && (strcmp (mnemonic, "tpause") != 0)
4723 && (strcmp (mnemonic, "umwait") != 0)
40fb9820
L
4724 && !(operand_type_check (i.types[0], imm)
4725 && operand_type_check (i.types[1], imm)))
29b0f896
AM
4726 swap_operands ();
4727
ec56d5c0
JB
4728 /* The order of the immediates should be reversed
4729 for 2 immediates extrq and insertq instructions */
4730 if (i.imm_operands == 2
4731 && (strcmp (mnemonic, "extrq") == 0
4732 || strcmp (mnemonic, "insertq") == 0))
4733 swap_2_operands (0, 1);
4734
29b0f896
AM
4735 if (i.imm_operands)
4736 optimize_imm ();
4737
b300c311
L
4738 /* Don't optimize displacement for movabs since it only takes 64bit
4739 displacement. */
4740 if (i.disp_operands
a501d77e 4741 && i.disp_encoding != disp_encoding_32bit
862be3fb
L
4742 && (flag_code != CODE_64BIT
4743 || strcmp (mnemonic, "movabs") != 0))
4744 optimize_disp ();
29b0f896
AM
4745
4746 /* Next, we find a template that matches the given insn,
4747 making sure the overlap of the given operands types is consistent
4748 with the template operand types. */
252b5132 4749
83b16ac6 4750 if (!(t = match_template (mnem_suffix)))
29b0f896 4751 return;
252b5132 4752
7bab8ab5 4753 if (sse_check != check_none
81f8a913 4754 && !i.tm.opcode_modifier.noavx
6e3e5c9e 4755 && !i.tm.cpu_flags.bitfield.cpuavx
569d50f1 4756 && !i.tm.cpu_flags.bitfield.cpuavx512f
daf50ae7
L
4757 && (i.tm.cpu_flags.bitfield.cpusse
4758 || i.tm.cpu_flags.bitfield.cpusse2
4759 || i.tm.cpu_flags.bitfield.cpusse3
4760 || i.tm.cpu_flags.bitfield.cpussse3
4761 || i.tm.cpu_flags.bitfield.cpusse4_1
6e3e5c9e
JB
4762 || i.tm.cpu_flags.bitfield.cpusse4_2
4763 || i.tm.cpu_flags.bitfield.cpupclmul
4764 || i.tm.cpu_flags.bitfield.cpuaes
569d50f1 4765 || i.tm.cpu_flags.bitfield.cpusha
6e3e5c9e 4766 || i.tm.cpu_flags.bitfield.cpugfni))
daf50ae7 4767 {
7bab8ab5 4768 (sse_check == check_warning
daf50ae7
L
4769 ? as_warn
4770 : as_bad) (_("SSE instruction `%s' is used"), i.tm.name);
4771 }
4772
40fb9820 4773 if (i.tm.opcode_modifier.fwait)
29b0f896
AM
4774 if (!add_prefix (FWAIT_OPCODE))
4775 return;
252b5132 4776
d5de92cf
L
4777 /* Check if REP prefix is OK. */
4778 if (i.rep_prefix && !i.tm.opcode_modifier.repprefixok)
4779 {
4780 as_bad (_("invalid instruction `%s' after `%s'"),
4781 i.tm.name, i.rep_prefix);
4782 return;
4783 }
4784
c1ba0266
L
4785 /* Check for lock without a lockable instruction. Destination operand
4786 must be memory unless it is xchg (0x86). */
c32fa91d
L
4787 if (i.prefix[LOCK_PREFIX]
4788 && (!i.tm.opcode_modifier.islockable
c1ba0266
L
4789 || i.mem_operands == 0
4790 || (i.tm.base_opcode != 0x86
8dc0818e 4791 && !(i.flags[i.operands - 1] & Operand_Mem))))
c32fa91d
L
4792 {
4793 as_bad (_("expecting lockable instruction after `lock'"));
4794 return;
4795 }
4796
40d231b4
JB
4797 /* Check for data size prefix on VEX/XOP/EVEX encoded and SIMD insns. */
4798 if (i.prefix[DATA_PREFIX]
4799 && (is_any_vex_encoding (&i.tm)
4800 || i.tm.operand_types[i.imm_operands].bitfield.class >= RegMMX
4801 || i.tm.operand_types[i.imm_operands + 1].bitfield.class >= RegMMX))
7a8655d2
JB
4802 {
4803 as_bad (_("data size prefix invalid with `%s'"), i.tm.name);
4804 return;
4805 }
4806
42164a71 4807 /* Check if HLE prefix is OK. */
165de32a 4808 if (i.hle_prefix && !check_hle ())
42164a71
L
4809 return;
4810
7e8b059b
L
4811 /* Check BND prefix. */
4812 if (i.bnd_prefix && !i.tm.opcode_modifier.bndprefixok)
4813 as_bad (_("expecting valid branch instruction after `bnd'"));
4814
04ef582a 4815 /* Check NOTRACK prefix. */
9fef80d6
L
4816 if (i.notrack_prefix && !i.tm.opcode_modifier.notrackprefixok)
4817 as_bad (_("expecting indirect branch instruction after `notrack'"));
04ef582a 4818
327e8c42
JB
4819 if (i.tm.cpu_flags.bitfield.cpumpx)
4820 {
4821 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
4822 as_bad (_("32-bit address isn't allowed in 64-bit MPX instructions."));
4823 else if (flag_code != CODE_16BIT
4824 ? i.prefix[ADDR_PREFIX]
4825 : i.mem_operands && !i.prefix[ADDR_PREFIX])
4826 as_bad (_("16-bit address isn't allowed in MPX instructions"));
4827 }
7e8b059b
L
4828
4829 /* Insert BND prefix. */
76d3a78a
JB
4830 if (add_bnd_prefix && i.tm.opcode_modifier.bndprefixok)
4831 {
4832 if (!i.prefix[BND_PREFIX])
4833 add_prefix (BND_PREFIX_OPCODE);
4834 else if (i.prefix[BND_PREFIX] != BND_PREFIX_OPCODE)
4835 {
4836 as_warn (_("replacing `rep'/`repe' prefix by `bnd'"));
4837 i.prefix[BND_PREFIX] = BND_PREFIX_OPCODE;
4838 }
4839 }
7e8b059b 4840
29b0f896 4841 /* Check string instruction segment overrides. */
51c8edf6 4842 if (i.tm.opcode_modifier.isstring >= IS_STRING_ES_OP0)
29b0f896 4843 {
51c8edf6 4844 gas_assert (i.mem_operands);
29b0f896 4845 if (!check_string ())
5dd0794d 4846 return;
fc0763e6 4847 i.disp_operands = 0;
29b0f896 4848 }
5dd0794d 4849
b6f8c7c4
L
4850 if (optimize && !i.no_optimize && i.tm.opcode_modifier.optimize)
4851 optimize_encoding ();
4852
29b0f896
AM
4853 if (!process_suffix ())
4854 return;
e413e4e9 4855
921eafea 4856 /* Update operand types and check extended states. */
bc0844ae 4857 for (j = 0; j < i.operands; j++)
921eafea
L
4858 {
4859 i.types[j] = operand_type_and (i.types[j], i.tm.operand_types[j]);
4860 switch (i.tm.operand_types[j].bitfield.class)
4861 {
4862 default:
4863 break;
4864 case RegMMX:
4865 i.xstate |= xstate_mmx;
4866 break;
4867 case RegMask:
4868 i.xstate |= xstate_zmm;
4869 break;
4870 case RegSIMD:
4871 if (i.tm.operand_types[j].bitfield.tmmword)
4872 i.xstate |= xstate_tmm;
4873 else if (i.tm.operand_types[j].bitfield.zmmword)
4874 i.xstate |= xstate_zmm;
4875 else if (i.tm.operand_types[j].bitfield.ymmword)
4876 i.xstate |= xstate_ymm;
4877 else if (i.tm.operand_types[j].bitfield.xmmword)
4878 i.xstate |= xstate_xmm;
4879 break;
4880 }
4881 }
bc0844ae 4882
29b0f896
AM
4883 /* Make still unresolved immediate matches conform to size of immediate
4884 given in i.suffix. */
4885 if (!finalize_imm ())
4886 return;
252b5132 4887
40fb9820 4888 if (i.types[0].bitfield.imm1)
29b0f896 4889 i.imm_operands = 0; /* kludge for shift insns. */
252b5132 4890
9afe6eb8
L
4891 /* We only need to check those implicit registers for instructions
4892 with 3 operands or less. */
4893 if (i.operands <= 3)
4894 for (j = 0; j < i.operands; j++)
75e5731b
JB
4895 if (i.types[j].bitfield.instance != InstanceNone
4896 && !i.types[j].bitfield.xmmword)
9afe6eb8 4897 i.reg_operands--;
40fb9820 4898
29b0f896
AM
4899 /* For insns with operands there are more diddles to do to the opcode. */
4900 if (i.operands)
4901 {
4902 if (!process_operands ())
4903 return;
4904 }
8c190ce0 4905 else if (!quiet_warnings && i.tm.opcode_modifier.ugh)
29b0f896
AM
4906 {
4907 /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc. */
4908 as_warn (_("translating to `%sp'"), i.tm.name);
4909 }
252b5132 4910
7a8655d2 4911 if (is_any_vex_encoding (&i.tm))
9e5e5283 4912 {
c1dc7af5 4913 if (!cpu_arch_flags.bitfield.cpui286)
9e5e5283 4914 {
c1dc7af5 4915 as_bad (_("instruction `%s' isn't supported outside of protected mode."),
9e5e5283
L
4916 i.tm.name);
4917 return;
4918 }
c0f3af97 4919
0b9404fd
JB
4920 /* Check for explicit REX prefix. */
4921 if (i.prefix[REX_PREFIX] || i.rex_encoding)
4922 {
4923 as_bad (_("REX prefix invalid with `%s'"), i.tm.name);
4924 return;
4925 }
4926
9e5e5283
L
4927 if (i.tm.opcode_modifier.vex)
4928 build_vex_prefix (t);
4929 else
4930 build_evex_prefix ();
0b9404fd
JB
4931
4932 /* The individual REX.RXBW bits got consumed. */
4933 i.rex &= REX_OPCODE;
9e5e5283 4934 }
43234a1e 4935
5dd85c99
SP
4936 /* Handle conversion of 'int $3' --> special int3 insn. XOP or FMA4
4937 instructions may define INT_OPCODE as well, so avoid this corner
4938 case for those instructions that use MODRM. */
4939 if (i.tm.base_opcode == INT_OPCODE
a6461c02
SP
4940 && !i.tm.opcode_modifier.modrm
4941 && i.op[0].imms->X_add_number == 3)
29b0f896
AM
4942 {
4943 i.tm.base_opcode = INT3_OPCODE;
4944 i.imm_operands = 0;
4945 }
252b5132 4946
0cfa3eb3
JB
4947 if ((i.tm.opcode_modifier.jump == JUMP
4948 || i.tm.opcode_modifier.jump == JUMP_BYTE
4949 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896
AM
4950 && i.op[0].disps->X_op == O_constant)
4951 {
4952 /* Convert "jmp constant" (and "call constant") to a jump (call) to
4953 the absolute address given by the constant. Since ix86 jumps and
4954 calls are pc relative, we need to generate a reloc. */
4955 i.op[0].disps->X_add_symbol = &abs_symbol;
4956 i.op[0].disps->X_op = O_symbol;
4957 }
252b5132 4958
29b0f896
AM
4959 /* For 8 bit registers we need an empty rex prefix. Also if the
4960 instruction already has a prefix, we need to convert old
4961 registers to new ones. */
773f551c 4962
bab6aec1 4963 if ((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte
29b0f896 4964 && (i.op[0].regs->reg_flags & RegRex64) != 0)
bab6aec1 4965 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte
29b0f896 4966 && (i.op[1].regs->reg_flags & RegRex64) != 0)
bab6aec1
JB
4967 || (((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte)
4968 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte))
29b0f896
AM
4969 && i.rex != 0))
4970 {
4971 int x;
726c5dcd 4972
29b0f896
AM
4973 i.rex |= REX_OPCODE;
4974 for (x = 0; x < 2; x++)
4975 {
4976 /* Look for 8 bit operand that uses old registers. */
bab6aec1 4977 if (i.types[x].bitfield.class == Reg && i.types[x].bitfield.byte
29b0f896 4978 && (i.op[x].regs->reg_flags & RegRex64) == 0)
773f551c 4979 {
3f93af61 4980 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
29b0f896
AM
4981 /* In case it is "hi" register, give up. */
4982 if (i.op[x].regs->reg_num > 3)
a540244d 4983 as_bad (_("can't encode register '%s%s' in an "
4eed87de 4984 "instruction requiring REX prefix."),
a540244d 4985 register_prefix, i.op[x].regs->reg_name);
773f551c 4986
29b0f896
AM
4987 /* Otherwise it is equivalent to the extended register.
4988 Since the encoding doesn't change this is merely
4989 cosmetic cleanup for debug output. */
4990
4991 i.op[x].regs = i.op[x].regs + 8;
773f551c 4992 }
29b0f896
AM
4993 }
4994 }
773f551c 4995
6b6b6807
L
4996 if (i.rex == 0 && i.rex_encoding)
4997 {
4998 /* Check if we can add a REX_OPCODE byte. Look for 8 bit operand
3f93af61 4999 that uses legacy register. If it is "hi" register, don't add
6b6b6807
L
5000 the REX_OPCODE byte. */
5001 int x;
5002 for (x = 0; x < 2; x++)
bab6aec1 5003 if (i.types[x].bitfield.class == Reg
6b6b6807
L
5004 && i.types[x].bitfield.byte
5005 && (i.op[x].regs->reg_flags & RegRex64) == 0
5006 && i.op[x].regs->reg_num > 3)
5007 {
3f93af61 5008 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
6b6b6807
L
5009 i.rex_encoding = FALSE;
5010 break;
5011 }
5012
5013 if (i.rex_encoding)
5014 i.rex = REX_OPCODE;
5015 }
5016
7ab9ffdd 5017 if (i.rex != 0)
29b0f896
AM
5018 add_prefix (REX_OPCODE | i.rex);
5019
ae531041
L
5020 insert_lfence_before ();
5021
29b0f896
AM
5022 /* We are ready to output the insn. */
5023 output_insn ();
e379e5f3 5024
ae531041
L
5025 insert_lfence_after ();
5026
e379e5f3
L
5027 last_insn.seg = now_seg;
5028
5029 if (i.tm.opcode_modifier.isprefix)
5030 {
5031 last_insn.kind = last_insn_prefix;
5032 last_insn.name = i.tm.name;
5033 last_insn.file = as_where (&last_insn.line);
5034 }
5035 else
5036 last_insn.kind = last_insn_other;
29b0f896
AM
5037}
5038
5039static char *
e3bb37b5 5040parse_insn (char *line, char *mnemonic)
29b0f896
AM
5041{
5042 char *l = line;
5043 char *token_start = l;
5044 char *mnem_p;
5c6af06e 5045 int supported;
d3ce72d0 5046 const insn_template *t;
b6169b20 5047 char *dot_p = NULL;
29b0f896 5048
29b0f896
AM
5049 while (1)
5050 {
5051 mnem_p = mnemonic;
5052 while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
5053 {
b6169b20
L
5054 if (*mnem_p == '.')
5055 dot_p = mnem_p;
29b0f896
AM
5056 mnem_p++;
5057 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
45288df1 5058 {
29b0f896
AM
5059 as_bad (_("no such instruction: `%s'"), token_start);
5060 return NULL;
5061 }
5062 l++;
5063 }
5064 if (!is_space_char (*l)
5065 && *l != END_OF_INSN
e44823cf
JB
5066 && (intel_syntax
5067 || (*l != PREFIX_SEPARATOR
5068 && *l != ',')))
29b0f896
AM
5069 {
5070 as_bad (_("invalid character %s in mnemonic"),
5071 output_invalid (*l));
5072 return NULL;
5073 }
5074 if (token_start == l)
5075 {
e44823cf 5076 if (!intel_syntax && *l == PREFIX_SEPARATOR)
29b0f896
AM
5077 as_bad (_("expecting prefix; got nothing"));
5078 else
5079 as_bad (_("expecting mnemonic; got nothing"));
5080 return NULL;
5081 }
45288df1 5082
29b0f896 5083 /* Look up instruction (or prefix) via hash table. */
629310ab 5084 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
47926f60 5085
29b0f896
AM
5086 if (*l != END_OF_INSN
5087 && (!is_space_char (*l) || l[1] != END_OF_INSN)
5088 && current_templates
40fb9820 5089 && current_templates->start->opcode_modifier.isprefix)
29b0f896 5090 {
c6fb90c8 5091 if (!cpu_flags_check_cpu64 (current_templates->start->cpu_flags))
2dd88dca
JB
5092 {
5093 as_bad ((flag_code != CODE_64BIT
5094 ? _("`%s' is only supported in 64-bit mode")
5095 : _("`%s' is not supported in 64-bit mode")),
5096 current_templates->start->name);
5097 return NULL;
5098 }
29b0f896
AM
5099 /* If we are in 16-bit mode, do not allow addr16 or data16.
5100 Similarly, in 32-bit mode, do not allow addr32 or data32. */
673fe0f0
JB
5101 if ((current_templates->start->opcode_modifier.size == SIZE16
5102 || current_templates->start->opcode_modifier.size == SIZE32)
29b0f896 5103 && flag_code != CODE_64BIT
673fe0f0 5104 && ((current_templates->start->opcode_modifier.size == SIZE32)
29b0f896
AM
5105 ^ (flag_code == CODE_16BIT)))
5106 {
5107 as_bad (_("redundant %s prefix"),
5108 current_templates->start->name);
5109 return NULL;
45288df1 5110 }
86fa6981 5111 if (current_templates->start->opcode_length == 0)
29b0f896 5112 {
86fa6981
L
5113 /* Handle pseudo prefixes. */
5114 switch (current_templates->start->base_opcode)
5115 {
41eb8e88 5116 case Prefix_Disp8:
86fa6981
L
5117 /* {disp8} */
5118 i.disp_encoding = disp_encoding_8bit;
5119 break;
41eb8e88
L
5120 case Prefix_Disp16:
5121 /* {disp16} */
5122 i.disp_encoding = disp_encoding_16bit;
5123 break;
5124 case Prefix_Disp32:
86fa6981
L
5125 /* {disp32} */
5126 i.disp_encoding = disp_encoding_32bit;
5127 break;
41eb8e88 5128 case Prefix_Load:
86fa6981
L
5129 /* {load} */
5130 i.dir_encoding = dir_encoding_load;
5131 break;
41eb8e88 5132 case Prefix_Store:
86fa6981
L
5133 /* {store} */
5134 i.dir_encoding = dir_encoding_store;
5135 break;
41eb8e88 5136 case Prefix_VEX:
42e04b36
L
5137 /* {vex} */
5138 i.vec_encoding = vex_encoding_vex;
86fa6981 5139 break;
41eb8e88 5140 case Prefix_VEX3:
86fa6981
L
5141 /* {vex3} */
5142 i.vec_encoding = vex_encoding_vex3;
5143 break;
41eb8e88 5144 case Prefix_EVEX:
86fa6981
L
5145 /* {evex} */
5146 i.vec_encoding = vex_encoding_evex;
5147 break;
41eb8e88 5148 case Prefix_REX:
6b6b6807
L
5149 /* {rex} */
5150 i.rex_encoding = TRUE;
5151 break;
41eb8e88 5152 case Prefix_NoOptimize:
b6f8c7c4
L
5153 /* {nooptimize} */
5154 i.no_optimize = TRUE;
5155 break;
86fa6981
L
5156 default:
5157 abort ();
5158 }
5159 }
5160 else
5161 {
5162 /* Add prefix, checking for repeated prefixes. */
4e9ac44a 5163 switch (add_prefix (current_templates->start->base_opcode))
86fa6981 5164 {
4e9ac44a
L
5165 case PREFIX_EXIST:
5166 return NULL;
5167 case PREFIX_DS:
d777820b 5168 if (current_templates->start->cpu_flags.bitfield.cpuibt)
4e9ac44a
L
5169 i.notrack_prefix = current_templates->start->name;
5170 break;
5171 case PREFIX_REP:
5172 if (current_templates->start->cpu_flags.bitfield.cpuhle)
5173 i.hle_prefix = current_templates->start->name;
5174 else if (current_templates->start->cpu_flags.bitfield.cpumpx)
5175 i.bnd_prefix = current_templates->start->name;
5176 else
5177 i.rep_prefix = current_templates->start->name;
5178 break;
5179 default:
5180 break;
86fa6981 5181 }
29b0f896
AM
5182 }
5183 /* Skip past PREFIX_SEPARATOR and reset token_start. */
5184 token_start = ++l;
5185 }
5186 else
5187 break;
5188 }
45288df1 5189
30a55f88 5190 if (!current_templates)
b6169b20 5191 {
07d5e953
JB
5192 /* Deprecated functionality (new code should use pseudo-prefixes instead):
5193 Check if we should swap operand or force 32bit displacement in
f8a5c266 5194 encoding. */
30a55f88 5195 if (mnem_p - 2 == dot_p && dot_p[1] == 's')
64c49ab3 5196 i.dir_encoding = dir_encoding_swap;
8d63c93e 5197 else if (mnem_p - 3 == dot_p
a501d77e
L
5198 && dot_p[1] == 'd'
5199 && dot_p[2] == '8')
5200 i.disp_encoding = disp_encoding_8bit;
8d63c93e 5201 else if (mnem_p - 4 == dot_p
f8a5c266
L
5202 && dot_p[1] == 'd'
5203 && dot_p[2] == '3'
5204 && dot_p[3] == '2')
a501d77e 5205 i.disp_encoding = disp_encoding_32bit;
30a55f88
L
5206 else
5207 goto check_suffix;
5208 mnem_p = dot_p;
5209 *dot_p = '\0';
629310ab 5210 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
b6169b20
L
5211 }
5212
29b0f896
AM
5213 if (!current_templates)
5214 {
dc1e8a47 5215 check_suffix:
1c529385 5216 if (mnem_p > mnemonic)
29b0f896 5217 {
1c529385
LH
5218 /* See if we can get a match by trimming off a suffix. */
5219 switch (mnem_p[-1])
29b0f896 5220 {
1c529385
LH
5221 case WORD_MNEM_SUFFIX:
5222 if (intel_syntax && (intel_float_operand (mnemonic) & 2))
29b0f896
AM
5223 i.suffix = SHORT_MNEM_SUFFIX;
5224 else
1c529385
LH
5225 /* Fall through. */
5226 case BYTE_MNEM_SUFFIX:
5227 case QWORD_MNEM_SUFFIX:
5228 i.suffix = mnem_p[-1];
29b0f896 5229 mnem_p[-1] = '\0';
fe0e921f
AM
5230 current_templates
5231 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5232 break;
5233 case SHORT_MNEM_SUFFIX:
5234 case LONG_MNEM_SUFFIX:
5235 if (!intel_syntax)
5236 {
5237 i.suffix = mnem_p[-1];
5238 mnem_p[-1] = '\0';
fe0e921f
AM
5239 current_templates
5240 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5241 }
5242 break;
5243
5244 /* Intel Syntax. */
5245 case 'd':
5246 if (intel_syntax)
5247 {
5248 if (intel_float_operand (mnemonic) == 1)
5249 i.suffix = SHORT_MNEM_SUFFIX;
5250 else
5251 i.suffix = LONG_MNEM_SUFFIX;
5252 mnem_p[-1] = '\0';
fe0e921f
AM
5253 current_templates
5254 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5255 }
5256 break;
29b0f896 5257 }
29b0f896 5258 }
1c529385 5259
29b0f896
AM
5260 if (!current_templates)
5261 {
5262 as_bad (_("no such instruction: `%s'"), token_start);
5263 return NULL;
5264 }
5265 }
252b5132 5266
0cfa3eb3
JB
5267 if (current_templates->start->opcode_modifier.jump == JUMP
5268 || current_templates->start->opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
5269 {
5270 /* Check for a branch hint. We allow ",pt" and ",pn" for
5271 predict taken and predict not taken respectively.
5272 I'm not sure that branch hints actually do anything on loop
5273 and jcxz insns (JumpByte) for current Pentium4 chips. They
5274 may work in the future and it doesn't hurt to accept them
5275 now. */
5276 if (l[0] == ',' && l[1] == 'p')
5277 {
5278 if (l[2] == 't')
5279 {
5280 if (!add_prefix (DS_PREFIX_OPCODE))
5281 return NULL;
5282 l += 3;
5283 }
5284 else if (l[2] == 'n')
5285 {
5286 if (!add_prefix (CS_PREFIX_OPCODE))
5287 return NULL;
5288 l += 3;
5289 }
5290 }
5291 }
5292 /* Any other comma loses. */
5293 if (*l == ',')
5294 {
5295 as_bad (_("invalid character %s in mnemonic"),
5296 output_invalid (*l));
5297 return NULL;
5298 }
252b5132 5299
29b0f896 5300 /* Check if instruction is supported on specified architecture. */
5c6af06e
JB
5301 supported = 0;
5302 for (t = current_templates->start; t < current_templates->end; ++t)
5303 {
c0f3af97
L
5304 supported |= cpu_flags_match (t);
5305 if (supported == CPU_FLAGS_PERFECT_MATCH)
548d0ee6
JB
5306 {
5307 if (!cpu_arch_flags.bitfield.cpui386 && (flag_code != CODE_16BIT))
5308 as_warn (_("use .code16 to ensure correct addressing mode"));
3629bb00 5309
548d0ee6
JB
5310 return l;
5311 }
29b0f896 5312 }
3629bb00 5313
548d0ee6
JB
5314 if (!(supported & CPU_FLAGS_64BIT_MATCH))
5315 as_bad (flag_code == CODE_64BIT
5316 ? _("`%s' is not supported in 64-bit mode")
5317 : _("`%s' is only supported in 64-bit mode"),
5318 current_templates->start->name);
5319 else
5320 as_bad (_("`%s' is not supported on `%s%s'"),
5321 current_templates->start->name,
5322 cpu_arch_name ? cpu_arch_name : default_arch,
5323 cpu_sub_arch_name ? cpu_sub_arch_name : "");
252b5132 5324
548d0ee6 5325 return NULL;
29b0f896 5326}
252b5132 5327
29b0f896 5328static char *
e3bb37b5 5329parse_operands (char *l, const char *mnemonic)
29b0f896
AM
5330{
5331 char *token_start;
3138f287 5332
29b0f896
AM
5333 /* 1 if operand is pending after ','. */
5334 unsigned int expecting_operand = 0;
252b5132 5335
29b0f896
AM
5336 /* Non-zero if operand parens not balanced. */
5337 unsigned int paren_not_balanced;
5338
5339 while (*l != END_OF_INSN)
5340 {
5341 /* Skip optional white space before operand. */
5342 if (is_space_char (*l))
5343 ++l;
d02603dc 5344 if (!is_operand_char (*l) && *l != END_OF_INSN && *l != '"')
29b0f896
AM
5345 {
5346 as_bad (_("invalid character %s before operand %d"),
5347 output_invalid (*l),
5348 i.operands + 1);
5349 return NULL;
5350 }
d02603dc 5351 token_start = l; /* After white space. */
29b0f896
AM
5352 paren_not_balanced = 0;
5353 while (paren_not_balanced || *l != ',')
5354 {
5355 if (*l == END_OF_INSN)
5356 {
5357 if (paren_not_balanced)
5358 {
5359 if (!intel_syntax)
5360 as_bad (_("unbalanced parenthesis in operand %d."),
5361 i.operands + 1);
5362 else
5363 as_bad (_("unbalanced brackets in operand %d."),
5364 i.operands + 1);
5365 return NULL;
5366 }
5367 else
5368 break; /* we are done */
5369 }
d02603dc 5370 else if (!is_operand_char (*l) && !is_space_char (*l) && *l != '"')
29b0f896
AM
5371 {
5372 as_bad (_("invalid character %s in operand %d"),
5373 output_invalid (*l),
5374 i.operands + 1);
5375 return NULL;
5376 }
5377 if (!intel_syntax)
5378 {
5379 if (*l == '(')
5380 ++paren_not_balanced;
5381 if (*l == ')')
5382 --paren_not_balanced;
5383 }
5384 else
5385 {
5386 if (*l == '[')
5387 ++paren_not_balanced;
5388 if (*l == ']')
5389 --paren_not_balanced;
5390 }
5391 l++;
5392 }
5393 if (l != token_start)
5394 { /* Yes, we've read in another operand. */
5395 unsigned int operand_ok;
5396 this_operand = i.operands++;
5397 if (i.operands > MAX_OPERANDS)
5398 {
5399 as_bad (_("spurious operands; (%d operands/instruction max)"),
5400 MAX_OPERANDS);
5401 return NULL;
5402 }
9d46ce34 5403 i.types[this_operand].bitfield.unspecified = 1;
29b0f896
AM
5404 /* Now parse operand adding info to 'i' as we go along. */
5405 END_STRING_AND_SAVE (l);
5406
1286ab78
L
5407 if (i.mem_operands > 1)
5408 {
5409 as_bad (_("too many memory references for `%s'"),
5410 mnemonic);
5411 return 0;
5412 }
5413
29b0f896
AM
5414 if (intel_syntax)
5415 operand_ok =
5416 i386_intel_operand (token_start,
5417 intel_float_operand (mnemonic));
5418 else
a7619375 5419 operand_ok = i386_att_operand (token_start);
29b0f896
AM
5420
5421 RESTORE_END_STRING (l);
5422 if (!operand_ok)
5423 return NULL;
5424 }
5425 else
5426 {
5427 if (expecting_operand)
5428 {
5429 expecting_operand_after_comma:
5430 as_bad (_("expecting operand after ','; got nothing"));
5431 return NULL;
5432 }
5433 if (*l == ',')
5434 {
5435 as_bad (_("expecting operand before ','; got nothing"));
5436 return NULL;
5437 }
5438 }
7f3f1ea2 5439
29b0f896
AM
5440 /* Now *l must be either ',' or END_OF_INSN. */
5441 if (*l == ',')
5442 {
5443 if (*++l == END_OF_INSN)
5444 {
5445 /* Just skip it, if it's \n complain. */
5446 goto expecting_operand_after_comma;
5447 }
5448 expecting_operand = 1;
5449 }
5450 }
5451 return l;
5452}
7f3f1ea2 5453
050dfa73 5454static void
4d456e3d 5455swap_2_operands (int xchg1, int xchg2)
050dfa73
MM
5456{
5457 union i386_op temp_op;
40fb9820 5458 i386_operand_type temp_type;
c48dadc9 5459 unsigned int temp_flags;
050dfa73 5460 enum bfd_reloc_code_real temp_reloc;
4eed87de 5461
050dfa73
MM
5462 temp_type = i.types[xchg2];
5463 i.types[xchg2] = i.types[xchg1];
5464 i.types[xchg1] = temp_type;
c48dadc9
JB
5465
5466 temp_flags = i.flags[xchg2];
5467 i.flags[xchg2] = i.flags[xchg1];
5468 i.flags[xchg1] = temp_flags;
5469
050dfa73
MM
5470 temp_op = i.op[xchg2];
5471 i.op[xchg2] = i.op[xchg1];
5472 i.op[xchg1] = temp_op;
c48dadc9 5473
050dfa73
MM
5474 temp_reloc = i.reloc[xchg2];
5475 i.reloc[xchg2] = i.reloc[xchg1];
5476 i.reloc[xchg1] = temp_reloc;
43234a1e
L
5477
5478 if (i.mask)
5479 {
5480 if (i.mask->operand == xchg1)
5481 i.mask->operand = xchg2;
5482 else if (i.mask->operand == xchg2)
5483 i.mask->operand = xchg1;
5484 }
5485 if (i.broadcast)
5486 {
5487 if (i.broadcast->operand == xchg1)
5488 i.broadcast->operand = xchg2;
5489 else if (i.broadcast->operand == xchg2)
5490 i.broadcast->operand = xchg1;
5491 }
5492 if (i.rounding)
5493 {
5494 if (i.rounding->operand == xchg1)
5495 i.rounding->operand = xchg2;
5496 else if (i.rounding->operand == xchg2)
5497 i.rounding->operand = xchg1;
5498 }
050dfa73
MM
5499}
5500
29b0f896 5501static void
e3bb37b5 5502swap_operands (void)
29b0f896 5503{
b7c61d9a 5504 switch (i.operands)
050dfa73 5505 {
c0f3af97 5506 case 5:
b7c61d9a 5507 case 4:
4d456e3d 5508 swap_2_operands (1, i.operands - 2);
1a0670f3 5509 /* Fall through. */
b7c61d9a
L
5510 case 3:
5511 case 2:
4d456e3d 5512 swap_2_operands (0, i.operands - 1);
b7c61d9a
L
5513 break;
5514 default:
5515 abort ();
29b0f896 5516 }
29b0f896
AM
5517
5518 if (i.mem_operands == 2)
5519 {
5520 const seg_entry *temp_seg;
5521 temp_seg = i.seg[0];
5522 i.seg[0] = i.seg[1];
5523 i.seg[1] = temp_seg;
5524 }
5525}
252b5132 5526
29b0f896
AM
5527/* Try to ensure constant immediates are represented in the smallest
5528 opcode possible. */
5529static void
e3bb37b5 5530optimize_imm (void)
29b0f896
AM
5531{
5532 char guess_suffix = 0;
5533 int op;
252b5132 5534
29b0f896
AM
5535 if (i.suffix)
5536 guess_suffix = i.suffix;
5537 else if (i.reg_operands)
5538 {
5539 /* Figure out a suffix from the last register operand specified.
75e5731b
JB
5540 We can't do this properly yet, i.e. excluding special register
5541 instances, but the following works for instructions with
5542 immediates. In any case, we can't set i.suffix yet. */
29b0f896 5543 for (op = i.operands; --op >= 0;)
bab6aec1
JB
5544 if (i.types[op].bitfield.class != Reg)
5545 continue;
5546 else if (i.types[op].bitfield.byte)
7ab9ffdd 5547 {
40fb9820
L
5548 guess_suffix = BYTE_MNEM_SUFFIX;
5549 break;
5550 }
bab6aec1 5551 else if (i.types[op].bitfield.word)
252b5132 5552 {
40fb9820
L
5553 guess_suffix = WORD_MNEM_SUFFIX;
5554 break;
5555 }
bab6aec1 5556 else if (i.types[op].bitfield.dword)
40fb9820
L
5557 {
5558 guess_suffix = LONG_MNEM_SUFFIX;
5559 break;
5560 }
bab6aec1 5561 else if (i.types[op].bitfield.qword)
40fb9820
L
5562 {
5563 guess_suffix = QWORD_MNEM_SUFFIX;
29b0f896 5564 break;
252b5132 5565 }
29b0f896
AM
5566 }
5567 else if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
5568 guess_suffix = WORD_MNEM_SUFFIX;
5569
5570 for (op = i.operands; --op >= 0;)
40fb9820 5571 if (operand_type_check (i.types[op], imm))
29b0f896
AM
5572 {
5573 switch (i.op[op].imms->X_op)
252b5132 5574 {
29b0f896
AM
5575 case O_constant:
5576 /* If a suffix is given, this operand may be shortened. */
5577 switch (guess_suffix)
252b5132 5578 {
29b0f896 5579 case LONG_MNEM_SUFFIX:
40fb9820
L
5580 i.types[op].bitfield.imm32 = 1;
5581 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
5582 break;
5583 case WORD_MNEM_SUFFIX:
40fb9820
L
5584 i.types[op].bitfield.imm16 = 1;
5585 i.types[op].bitfield.imm32 = 1;
5586 i.types[op].bitfield.imm32s = 1;
5587 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
5588 break;
5589 case BYTE_MNEM_SUFFIX:
40fb9820
L
5590 i.types[op].bitfield.imm8 = 1;
5591 i.types[op].bitfield.imm8s = 1;
5592 i.types[op].bitfield.imm16 = 1;
5593 i.types[op].bitfield.imm32 = 1;
5594 i.types[op].bitfield.imm32s = 1;
5595 i.types[op].bitfield.imm64 = 1;
29b0f896 5596 break;
252b5132 5597 }
252b5132 5598
29b0f896
AM
5599 /* If this operand is at most 16 bits, convert it
5600 to a signed 16 bit number before trying to see
5601 whether it will fit in an even smaller size.
5602 This allows a 16-bit operand such as $0xffe0 to
5603 be recognised as within Imm8S range. */
40fb9820 5604 if ((i.types[op].bitfield.imm16)
29b0f896 5605 && (i.op[op].imms->X_add_number & ~(offsetT) 0xffff) == 0)
252b5132 5606 {
29b0f896
AM
5607 i.op[op].imms->X_add_number =
5608 (((i.op[op].imms->X_add_number & 0xffff) ^ 0x8000) - 0x8000);
5609 }
a28def75
L
5610#ifdef BFD64
5611 /* Store 32-bit immediate in 64-bit for 64-bit BFD. */
40fb9820 5612 if ((i.types[op].bitfield.imm32)
29b0f896
AM
5613 && ((i.op[op].imms->X_add_number & ~(((offsetT) 2 << 31) - 1))
5614 == 0))
5615 {
5616 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
5617 ^ ((offsetT) 1 << 31))
5618 - ((offsetT) 1 << 31));
5619 }
a28def75 5620#endif
40fb9820 5621 i.types[op]
c6fb90c8
L
5622 = operand_type_or (i.types[op],
5623 smallest_imm_type (i.op[op].imms->X_add_number));
252b5132 5624
29b0f896
AM
5625 /* We must avoid matching of Imm32 templates when 64bit
5626 only immediate is available. */
5627 if (guess_suffix == QWORD_MNEM_SUFFIX)
40fb9820 5628 i.types[op].bitfield.imm32 = 0;
29b0f896 5629 break;
252b5132 5630
29b0f896
AM
5631 case O_absent:
5632 case O_register:
5633 abort ();
5634
5635 /* Symbols and expressions. */
5636 default:
9cd96992
JB
5637 /* Convert symbolic operand to proper sizes for matching, but don't
5638 prevent matching a set of insns that only supports sizes other
5639 than those matching the insn suffix. */
5640 {
40fb9820 5641 i386_operand_type mask, allowed;
d3ce72d0 5642 const insn_template *t;
9cd96992 5643
0dfbf9d7
L
5644 operand_type_set (&mask, 0);
5645 operand_type_set (&allowed, 0);
40fb9820 5646
4eed87de
AM
5647 for (t = current_templates->start;
5648 t < current_templates->end;
5649 ++t)
bab6aec1
JB
5650 {
5651 allowed = operand_type_or (allowed, t->operand_types[op]);
5652 allowed = operand_type_and (allowed, anyimm);
5653 }
9cd96992
JB
5654 switch (guess_suffix)
5655 {
5656 case QWORD_MNEM_SUFFIX:
40fb9820
L
5657 mask.bitfield.imm64 = 1;
5658 mask.bitfield.imm32s = 1;
9cd96992
JB
5659 break;
5660 case LONG_MNEM_SUFFIX:
40fb9820 5661 mask.bitfield.imm32 = 1;
9cd96992
JB
5662 break;
5663 case WORD_MNEM_SUFFIX:
40fb9820 5664 mask.bitfield.imm16 = 1;
9cd96992
JB
5665 break;
5666 case BYTE_MNEM_SUFFIX:
40fb9820 5667 mask.bitfield.imm8 = 1;
9cd96992
JB
5668 break;
5669 default:
9cd96992
JB
5670 break;
5671 }
c6fb90c8 5672 allowed = operand_type_and (mask, allowed);
0dfbf9d7 5673 if (!operand_type_all_zero (&allowed))
c6fb90c8 5674 i.types[op] = operand_type_and (i.types[op], mask);
9cd96992 5675 }
29b0f896 5676 break;
252b5132 5677 }
29b0f896
AM
5678 }
5679}
47926f60 5680
29b0f896
AM
5681/* Try to use the smallest displacement type too. */
5682static void
e3bb37b5 5683optimize_disp (void)
29b0f896
AM
5684{
5685 int op;
3e73aa7c 5686
29b0f896 5687 for (op = i.operands; --op >= 0;)
40fb9820 5688 if (operand_type_check (i.types[op], disp))
252b5132 5689 {
b300c311 5690 if (i.op[op].disps->X_op == O_constant)
252b5132 5691 {
91d6fa6a 5692 offsetT op_disp = i.op[op].disps->X_add_number;
29b0f896 5693
40fb9820 5694 if (i.types[op].bitfield.disp16
91d6fa6a 5695 && (op_disp & ~(offsetT) 0xffff) == 0)
b300c311
L
5696 {
5697 /* If this operand is at most 16 bits, convert
5698 to a signed 16 bit number and don't use 64bit
5699 displacement. */
91d6fa6a 5700 op_disp = (((op_disp & 0xffff) ^ 0x8000) - 0x8000);
40fb9820 5701 i.types[op].bitfield.disp64 = 0;
b300c311 5702 }
a28def75
L
5703#ifdef BFD64
5704 /* Optimize 64-bit displacement to 32-bit for 64-bit BFD. */
40fb9820 5705 if (i.types[op].bitfield.disp32
91d6fa6a 5706 && (op_disp & ~(((offsetT) 2 << 31) - 1)) == 0)
b300c311
L
5707 {
5708 /* If this operand is at most 32 bits, convert
5709 to a signed 32 bit number and don't use 64bit
5710 displacement. */
91d6fa6a
NC
5711 op_disp &= (((offsetT) 2 << 31) - 1);
5712 op_disp = (op_disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
40fb9820 5713 i.types[op].bitfield.disp64 = 0;
b300c311 5714 }
a28def75 5715#endif
91d6fa6a 5716 if (!op_disp && i.types[op].bitfield.baseindex)
b300c311 5717 {
40fb9820
L
5718 i.types[op].bitfield.disp8 = 0;
5719 i.types[op].bitfield.disp16 = 0;
5720 i.types[op].bitfield.disp32 = 0;
5721 i.types[op].bitfield.disp32s = 0;
5722 i.types[op].bitfield.disp64 = 0;
b300c311
L
5723 i.op[op].disps = 0;
5724 i.disp_operands--;
5725 }
5726 else if (flag_code == CODE_64BIT)
5727 {
91d6fa6a 5728 if (fits_in_signed_long (op_disp))
28a9d8f5 5729 {
40fb9820
L
5730 i.types[op].bitfield.disp64 = 0;
5731 i.types[op].bitfield.disp32s = 1;
28a9d8f5 5732 }
0e1147d9 5733 if (i.prefix[ADDR_PREFIX]
91d6fa6a 5734 && fits_in_unsigned_long (op_disp))
40fb9820 5735 i.types[op].bitfield.disp32 = 1;
b300c311 5736 }
40fb9820
L
5737 if ((i.types[op].bitfield.disp32
5738 || i.types[op].bitfield.disp32s
5739 || i.types[op].bitfield.disp16)
b5014f7a 5740 && fits_in_disp8 (op_disp))
40fb9820 5741 i.types[op].bitfield.disp8 = 1;
252b5132 5742 }
67a4f2b7
AO
5743 else if (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
5744 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL)
5745 {
5746 fix_new_exp (frag_now, frag_more (0) - frag_now->fr_literal, 0,
5747 i.op[op].disps, 0, i.reloc[op]);
40fb9820
L
5748 i.types[op].bitfield.disp8 = 0;
5749 i.types[op].bitfield.disp16 = 0;
5750 i.types[op].bitfield.disp32 = 0;
5751 i.types[op].bitfield.disp32s = 0;
5752 i.types[op].bitfield.disp64 = 0;
67a4f2b7
AO
5753 }
5754 else
b300c311 5755 /* We only support 64bit displacement on constants. */
40fb9820 5756 i.types[op].bitfield.disp64 = 0;
252b5132 5757 }
29b0f896
AM
5758}
5759
4a1b91ea
L
5760/* Return 1 if there is a match in broadcast bytes between operand
5761 GIVEN and instruction template T. */
5762
5763static INLINE int
5764match_broadcast_size (const insn_template *t, unsigned int given)
5765{
5766 return ((t->opcode_modifier.broadcast == BYTE_BROADCAST
5767 && i.types[given].bitfield.byte)
5768 || (t->opcode_modifier.broadcast == WORD_BROADCAST
5769 && i.types[given].bitfield.word)
5770 || (t->opcode_modifier.broadcast == DWORD_BROADCAST
5771 && i.types[given].bitfield.dword)
5772 || (t->opcode_modifier.broadcast == QWORD_BROADCAST
5773 && i.types[given].bitfield.qword));
5774}
5775
6c30d220
L
5776/* Check if operands are valid for the instruction. */
5777
5778static int
5779check_VecOperands (const insn_template *t)
5780{
43234a1e 5781 unsigned int op;
e2195274 5782 i386_cpu_flags cpu;
e2195274
JB
5783
5784 /* Templates allowing for ZMMword as well as YMMword and/or XMMword for
5785 any one operand are implicity requiring AVX512VL support if the actual
5786 operand size is YMMword or XMMword. Since this function runs after
5787 template matching, there's no need to check for YMMword/XMMword in
5788 the template. */
5789 cpu = cpu_flags_and (t->cpu_flags, avx512);
5790 if (!cpu_flags_all_zero (&cpu)
5791 && !t->cpu_flags.bitfield.cpuavx512vl
5792 && !cpu_arch_flags.bitfield.cpuavx512vl)
5793 {
5794 for (op = 0; op < t->operands; ++op)
5795 {
5796 if (t->operand_types[op].bitfield.zmmword
5797 && (i.types[op].bitfield.ymmword
5798 || i.types[op].bitfield.xmmword))
5799 {
5800 i.error = unsupported;
5801 return 1;
5802 }
5803 }
5804 }
43234a1e 5805
6c30d220 5806 /* Without VSIB byte, we can't have a vector register for index. */
63112cd6 5807 if (!t->opcode_modifier.sib
6c30d220 5808 && i.index_reg
1b54b8d7
JB
5809 && (i.index_reg->reg_type.bitfield.xmmword
5810 || i.index_reg->reg_type.bitfield.ymmword
5811 || i.index_reg->reg_type.bitfield.zmmword))
6c30d220
L
5812 {
5813 i.error = unsupported_vector_index_register;
5814 return 1;
5815 }
5816
ad8ecc81
MZ
5817 /* Check if default mask is allowed. */
5818 if (t->opcode_modifier.nodefmask
5819 && (!i.mask || i.mask->mask->reg_num == 0))
5820 {
5821 i.error = no_default_mask;
5822 return 1;
5823 }
5824
7bab8ab5
JB
5825 /* For VSIB byte, we need a vector register for index, and all vector
5826 registers must be distinct. */
260cd341 5827 if (t->opcode_modifier.sib && t->opcode_modifier.sib != SIBMEM)
7bab8ab5
JB
5828 {
5829 if (!i.index_reg
63112cd6 5830 || !((t->opcode_modifier.sib == VECSIB128
1b54b8d7 5831 && i.index_reg->reg_type.bitfield.xmmword)
63112cd6 5832 || (t->opcode_modifier.sib == VECSIB256
1b54b8d7 5833 && i.index_reg->reg_type.bitfield.ymmword)
63112cd6 5834 || (t->opcode_modifier.sib == VECSIB512
1b54b8d7 5835 && i.index_reg->reg_type.bitfield.zmmword)))
7bab8ab5
JB
5836 {
5837 i.error = invalid_vsib_address;
5838 return 1;
5839 }
5840
43234a1e
L
5841 gas_assert (i.reg_operands == 2 || i.mask);
5842 if (i.reg_operands == 2 && !i.mask)
5843 {
3528c362 5844 gas_assert (i.types[0].bitfield.class == RegSIMD);
1b54b8d7
JB
5845 gas_assert (i.types[0].bitfield.xmmword
5846 || i.types[0].bitfield.ymmword);
3528c362 5847 gas_assert (i.types[2].bitfield.class == RegSIMD);
1b54b8d7
JB
5848 gas_assert (i.types[2].bitfield.xmmword
5849 || i.types[2].bitfield.ymmword);
43234a1e
L
5850 if (operand_check == check_none)
5851 return 0;
5852 if (register_number (i.op[0].regs)
5853 != register_number (i.index_reg)
5854 && register_number (i.op[2].regs)
5855 != register_number (i.index_reg)
5856 && register_number (i.op[0].regs)
5857 != register_number (i.op[2].regs))
5858 return 0;
5859 if (operand_check == check_error)
5860 {
5861 i.error = invalid_vector_register_set;
5862 return 1;
5863 }
5864 as_warn (_("mask, index, and destination registers should be distinct"));
5865 }
8444f82a
MZ
5866 else if (i.reg_operands == 1 && i.mask)
5867 {
3528c362 5868 if (i.types[1].bitfield.class == RegSIMD
1b54b8d7
JB
5869 && (i.types[1].bitfield.xmmword
5870 || i.types[1].bitfield.ymmword
5871 || i.types[1].bitfield.zmmword)
8444f82a
MZ
5872 && (register_number (i.op[1].regs)
5873 == register_number (i.index_reg)))
5874 {
5875 if (operand_check == check_error)
5876 {
5877 i.error = invalid_vector_register_set;
5878 return 1;
5879 }
5880 if (operand_check != check_none)
5881 as_warn (_("index and destination registers should be distinct"));
5882 }
5883 }
43234a1e 5884 }
7bab8ab5 5885
260cd341
LC
5886 /* For AMX instructions with three tmmword operands, all tmmword operand must be
5887 distinct */
5888 if (t->operand_types[0].bitfield.tmmword
5889 && i.reg_operands == 3)
5890 {
5891 if (register_number (i.op[0].regs)
5892 == register_number (i.op[1].regs)
5893 || register_number (i.op[0].regs)
5894 == register_number (i.op[2].regs)
5895 || register_number (i.op[1].regs)
5896 == register_number (i.op[2].regs))
5897 {
5898 i.error = invalid_tmm_register_set;
5899 return 1;
5900 }
5901 }
5902
43234a1e
L
5903 /* Check if broadcast is supported by the instruction and is applied
5904 to the memory operand. */
5905 if (i.broadcast)
5906 {
8e6e0792 5907 i386_operand_type type, overlap;
43234a1e
L
5908
5909 /* Check if specified broadcast is supported in this instruction,
4a1b91ea 5910 and its broadcast bytes match the memory operand. */
32546502 5911 op = i.broadcast->operand;
8e6e0792 5912 if (!t->opcode_modifier.broadcast
c48dadc9 5913 || !(i.flags[op] & Operand_Mem)
c39e5b26 5914 || (!i.types[op].bitfield.unspecified
4a1b91ea 5915 && !match_broadcast_size (t, op)))
43234a1e
L
5916 {
5917 bad_broadcast:
5918 i.error = unsupported_broadcast;
5919 return 1;
5920 }
8e6e0792 5921
4a1b91ea
L
5922 i.broadcast->bytes = ((1 << (t->opcode_modifier.broadcast - 1))
5923 * i.broadcast->type);
8e6e0792 5924 operand_type_set (&type, 0);
4a1b91ea 5925 switch (i.broadcast->bytes)
8e6e0792 5926 {
4a1b91ea
L
5927 case 2:
5928 type.bitfield.word = 1;
5929 break;
5930 case 4:
5931 type.bitfield.dword = 1;
5932 break;
8e6e0792
JB
5933 case 8:
5934 type.bitfield.qword = 1;
5935 break;
5936 case 16:
5937 type.bitfield.xmmword = 1;
5938 break;
5939 case 32:
5940 type.bitfield.ymmword = 1;
5941 break;
5942 case 64:
5943 type.bitfield.zmmword = 1;
5944 break;
5945 default:
5946 goto bad_broadcast;
5947 }
5948
5949 overlap = operand_type_and (type, t->operand_types[op]);
bc49bfd8
JB
5950 if (t->operand_types[op].bitfield.class == RegSIMD
5951 && t->operand_types[op].bitfield.byte
5952 + t->operand_types[op].bitfield.word
5953 + t->operand_types[op].bitfield.dword
5954 + t->operand_types[op].bitfield.qword > 1)
5955 {
5956 overlap.bitfield.xmmword = 0;
5957 overlap.bitfield.ymmword = 0;
5958 overlap.bitfield.zmmword = 0;
5959 }
8e6e0792
JB
5960 if (operand_type_all_zero (&overlap))
5961 goto bad_broadcast;
5962
5963 if (t->opcode_modifier.checkregsize)
5964 {
5965 unsigned int j;
5966
e2195274 5967 type.bitfield.baseindex = 1;
8e6e0792
JB
5968 for (j = 0; j < i.operands; ++j)
5969 {
5970 if (j != op
5971 && !operand_type_register_match(i.types[j],
5972 t->operand_types[j],
5973 type,
5974 t->operand_types[op]))
5975 goto bad_broadcast;
5976 }
5977 }
43234a1e
L
5978 }
5979 /* If broadcast is supported in this instruction, we need to check if
5980 operand of one-element size isn't specified without broadcast. */
5981 else if (t->opcode_modifier.broadcast && i.mem_operands)
5982 {
5983 /* Find memory operand. */
5984 for (op = 0; op < i.operands; op++)
8dc0818e 5985 if (i.flags[op] & Operand_Mem)
43234a1e
L
5986 break;
5987 gas_assert (op < i.operands);
5988 /* Check size of the memory operand. */
4a1b91ea 5989 if (match_broadcast_size (t, op))
43234a1e
L
5990 {
5991 i.error = broadcast_needed;
5992 return 1;
5993 }
5994 }
c39e5b26
JB
5995 else
5996 op = MAX_OPERANDS - 1; /* Avoid uninitialized variable warning. */
43234a1e
L
5997
5998 /* Check if requested masking is supported. */
ae2387fe 5999 if (i.mask)
43234a1e 6000 {
ae2387fe
JB
6001 switch (t->opcode_modifier.masking)
6002 {
6003 case BOTH_MASKING:
6004 break;
6005 case MERGING_MASKING:
6006 if (i.mask->zeroing)
6007 {
6008 case 0:
6009 i.error = unsupported_masking;
6010 return 1;
6011 }
6012 break;
6013 case DYNAMIC_MASKING:
6014 /* Memory destinations allow only merging masking. */
6015 if (i.mask->zeroing && i.mem_operands)
6016 {
6017 /* Find memory operand. */
6018 for (op = 0; op < i.operands; op++)
c48dadc9 6019 if (i.flags[op] & Operand_Mem)
ae2387fe
JB
6020 break;
6021 gas_assert (op < i.operands);
6022 if (op == i.operands - 1)
6023 {
6024 i.error = unsupported_masking;
6025 return 1;
6026 }
6027 }
6028 break;
6029 default:
6030 abort ();
6031 }
43234a1e
L
6032 }
6033
6034 /* Check if masking is applied to dest operand. */
6035 if (i.mask && (i.mask->operand != (int) (i.operands - 1)))
6036 {
6037 i.error = mask_not_on_destination;
6038 return 1;
6039 }
6040
43234a1e
L
6041 /* Check RC/SAE. */
6042 if (i.rounding)
6043 {
a80195f1
JB
6044 if (!t->opcode_modifier.sae
6045 || (i.rounding->type != saeonly && !t->opcode_modifier.staticrounding))
43234a1e
L
6046 {
6047 i.error = unsupported_rc_sae;
6048 return 1;
6049 }
6050 /* If the instruction has several immediate operands and one of
6051 them is rounding, the rounding operand should be the last
6052 immediate operand. */
6053 if (i.imm_operands > 1
6054 && i.rounding->operand != (int) (i.imm_operands - 1))
7bab8ab5 6055 {
43234a1e 6056 i.error = rc_sae_operand_not_last_imm;
7bab8ab5
JB
6057 return 1;
6058 }
6c30d220
L
6059 }
6060
da4977e0
JB
6061 /* Check the special Imm4 cases; must be the first operand. */
6062 if (t->cpu_flags.bitfield.cpuxop && t->operands == 5)
6063 {
6064 if (i.op[0].imms->X_op != O_constant
6065 || !fits_in_imm4 (i.op[0].imms->X_add_number))
6066 {
6067 i.error = bad_imm4;
6068 return 1;
6069 }
6070
6071 /* Turn off Imm<N> so that update_imm won't complain. */
6072 operand_type_set (&i.types[0], 0);
6073 }
6074
43234a1e 6075 /* Check vector Disp8 operand. */
b5014f7a
JB
6076 if (t->opcode_modifier.disp8memshift
6077 && i.disp_encoding != disp_encoding_32bit)
43234a1e
L
6078 {
6079 if (i.broadcast)
4a1b91ea 6080 i.memshift = t->opcode_modifier.broadcast - 1;
7091c612 6081 else if (t->opcode_modifier.disp8memshift != DISP8_SHIFT_VL)
43234a1e 6082 i.memshift = t->opcode_modifier.disp8memshift;
7091c612
JB
6083 else
6084 {
6085 const i386_operand_type *type = NULL;
6086
6087 i.memshift = 0;
6088 for (op = 0; op < i.operands; op++)
8dc0818e 6089 if (i.flags[op] & Operand_Mem)
7091c612 6090 {
4174bfff
JB
6091 if (t->opcode_modifier.evex == EVEXLIG)
6092 i.memshift = 2 + (i.suffix == QWORD_MNEM_SUFFIX);
6093 else if (t->operand_types[op].bitfield.xmmword
6094 + t->operand_types[op].bitfield.ymmword
6095 + t->operand_types[op].bitfield.zmmword <= 1)
7091c612
JB
6096 type = &t->operand_types[op];
6097 else if (!i.types[op].bitfield.unspecified)
6098 type = &i.types[op];
6099 }
3528c362 6100 else if (i.types[op].bitfield.class == RegSIMD
4174bfff 6101 && t->opcode_modifier.evex != EVEXLIG)
7091c612
JB
6102 {
6103 if (i.types[op].bitfield.zmmword)
6104 i.memshift = 6;
6105 else if (i.types[op].bitfield.ymmword && i.memshift < 5)
6106 i.memshift = 5;
6107 else if (i.types[op].bitfield.xmmword && i.memshift < 4)
6108 i.memshift = 4;
6109 }
6110
6111 if (type)
6112 {
6113 if (type->bitfield.zmmword)
6114 i.memshift = 6;
6115 else if (type->bitfield.ymmword)
6116 i.memshift = 5;
6117 else if (type->bitfield.xmmword)
6118 i.memshift = 4;
6119 }
6120
6121 /* For the check in fits_in_disp8(). */
6122 if (i.memshift == 0)
6123 i.memshift = -1;
6124 }
43234a1e
L
6125
6126 for (op = 0; op < i.operands; op++)
6127 if (operand_type_check (i.types[op], disp)
6128 && i.op[op].disps->X_op == O_constant)
6129 {
b5014f7a 6130 if (fits_in_disp8 (i.op[op].disps->X_add_number))
43234a1e 6131 {
b5014f7a
JB
6132 i.types[op].bitfield.disp8 = 1;
6133 return 0;
43234a1e 6134 }
b5014f7a 6135 i.types[op].bitfield.disp8 = 0;
43234a1e
L
6136 }
6137 }
b5014f7a
JB
6138
6139 i.memshift = 0;
43234a1e 6140
6c30d220
L
6141 return 0;
6142}
6143
da4977e0 6144/* Check if encoding requirements are met by the instruction. */
a683cc34
SP
6145
6146static int
da4977e0 6147VEX_check_encoding (const insn_template *t)
a683cc34 6148{
da4977e0
JB
6149 if (i.vec_encoding == vex_encoding_error)
6150 {
6151 i.error = unsupported;
6152 return 1;
6153 }
6154
86fa6981 6155 if (i.vec_encoding == vex_encoding_evex)
43234a1e 6156 {
86fa6981 6157 /* This instruction must be encoded with EVEX prefix. */
e771e7c9 6158 if (!is_evex_encoding (t))
86fa6981
L
6159 {
6160 i.error = unsupported;
6161 return 1;
6162 }
6163 return 0;
43234a1e
L
6164 }
6165
a683cc34 6166 if (!t->opcode_modifier.vex)
86fa6981
L
6167 {
6168 /* This instruction template doesn't have VEX prefix. */
6169 if (i.vec_encoding != vex_encoding_default)
6170 {
6171 i.error = unsupported;
6172 return 1;
6173 }
6174 return 0;
6175 }
a683cc34 6176
a683cc34
SP
6177 return 0;
6178}
6179
d3ce72d0 6180static const insn_template *
83b16ac6 6181match_template (char mnem_suffix)
29b0f896
AM
6182{
6183 /* Points to template once we've found it. */
d3ce72d0 6184 const insn_template *t;
40fb9820 6185 i386_operand_type overlap0, overlap1, overlap2, overlap3;
c0f3af97 6186 i386_operand_type overlap4;
29b0f896 6187 unsigned int found_reverse_match;
dc2be329 6188 i386_opcode_modifier suffix_check;
40fb9820 6189 i386_operand_type operand_types [MAX_OPERANDS];
539e75ad 6190 int addr_prefix_disp;
45a4bb20 6191 unsigned int j, size_match, check_register;
5614d22c 6192 enum i386_error specific_error = 0;
29b0f896 6193
c0f3af97
L
6194#if MAX_OPERANDS != 5
6195# error "MAX_OPERANDS must be 5."
f48ff2ae
L
6196#endif
6197
29b0f896 6198 found_reverse_match = 0;
539e75ad 6199 addr_prefix_disp = -1;
40fb9820 6200
dc2be329 6201 /* Prepare for mnemonic suffix check. */
40fb9820 6202 memset (&suffix_check, 0, sizeof (suffix_check));
dc2be329
L
6203 switch (mnem_suffix)
6204 {
6205 case BYTE_MNEM_SUFFIX:
6206 suffix_check.no_bsuf = 1;
6207 break;
6208 case WORD_MNEM_SUFFIX:
6209 suffix_check.no_wsuf = 1;
6210 break;
6211 case SHORT_MNEM_SUFFIX:
6212 suffix_check.no_ssuf = 1;
6213 break;
6214 case LONG_MNEM_SUFFIX:
6215 suffix_check.no_lsuf = 1;
6216 break;
6217 case QWORD_MNEM_SUFFIX:
6218 suffix_check.no_qsuf = 1;
6219 break;
6220 default:
6221 /* NB: In Intel syntax, normally we can check for memory operand
6222 size when there is no mnemonic suffix. But jmp and call have
6223 2 different encodings with Dword memory operand size, one with
6224 No_ldSuf and the other without. i.suffix is set to
6225 LONG_DOUBLE_MNEM_SUFFIX to skip the one with No_ldSuf. */
6226 if (i.suffix == LONG_DOUBLE_MNEM_SUFFIX)
6227 suffix_check.no_ldsuf = 1;
83b16ac6
JB
6228 }
6229
01559ecc
L
6230 /* Must have right number of operands. */
6231 i.error = number_of_operands_mismatch;
6232
45aa61fe 6233 for (t = current_templates->start; t < current_templates->end; t++)
29b0f896 6234 {
539e75ad 6235 addr_prefix_disp = -1;
dbbc8b7e 6236 found_reverse_match = 0;
539e75ad 6237
29b0f896
AM
6238 if (i.operands != t->operands)
6239 continue;
6240
50aecf8c 6241 /* Check processor support. */
a65babc9 6242 i.error = unsupported;
45a4bb20 6243 if (cpu_flags_match (t) != CPU_FLAGS_PERFECT_MATCH)
50aecf8c
L
6244 continue;
6245
e1d4d893 6246 /* Check AT&T mnemonic. */
a65babc9 6247 i.error = unsupported_with_intel_mnemonic;
e1d4d893 6248 if (intel_mnemonic && t->opcode_modifier.attmnemonic)
1efbbeb4
L
6249 continue;
6250
4b5aaf5f 6251 /* Check AT&T/Intel syntax. */
a65babc9 6252 i.error = unsupported_syntax;
5c07affc 6253 if ((intel_syntax && t->opcode_modifier.attsyntax)
4b5aaf5f 6254 || (!intel_syntax && t->opcode_modifier.intelsyntax))
1efbbeb4
L
6255 continue;
6256
4b5aaf5f
L
6257 /* Check Intel64/AMD64 ISA. */
6258 switch (isa64)
6259 {
6260 default:
6261 /* Default: Don't accept Intel64. */
6262 if (t->opcode_modifier.isa64 == INTEL64)
6263 continue;
6264 break;
6265 case amd64:
6266 /* -mamd64: Don't accept Intel64 and Intel64 only. */
6267 if (t->opcode_modifier.isa64 >= INTEL64)
6268 continue;
6269 break;
6270 case intel64:
6271 /* -mintel64: Don't accept AMD64. */
5990e377 6272 if (t->opcode_modifier.isa64 == AMD64 && flag_code == CODE_64BIT)
4b5aaf5f
L
6273 continue;
6274 break;
6275 }
6276
dc2be329 6277 /* Check the suffix. */
a65babc9 6278 i.error = invalid_instruction_suffix;
dc2be329
L
6279 if ((t->opcode_modifier.no_bsuf && suffix_check.no_bsuf)
6280 || (t->opcode_modifier.no_wsuf && suffix_check.no_wsuf)
6281 || (t->opcode_modifier.no_lsuf && suffix_check.no_lsuf)
6282 || (t->opcode_modifier.no_ssuf && suffix_check.no_ssuf)
6283 || (t->opcode_modifier.no_qsuf && suffix_check.no_qsuf)
6284 || (t->opcode_modifier.no_ldsuf && suffix_check.no_ldsuf))
83b16ac6 6285 continue;
29b0f896 6286
3ac21baa
JB
6287 size_match = operand_size_match (t);
6288 if (!size_match)
7d5e4556 6289 continue;
539e75ad 6290
6f2f06be
JB
6291 /* This is intentionally not
6292
0cfa3eb3 6293 if (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE))
6f2f06be
JB
6294
6295 as the case of a missing * on the operand is accepted (perhaps with
6296 a warning, issued further down). */
0cfa3eb3 6297 if (i.jumpabsolute && t->opcode_modifier.jump != JUMP_ABSOLUTE)
6f2f06be
JB
6298 {
6299 i.error = operand_type_mismatch;
6300 continue;
6301 }
6302
5c07affc
L
6303 for (j = 0; j < MAX_OPERANDS; j++)
6304 operand_types[j] = t->operand_types[j];
6305
e365e234
JB
6306 /* In general, don't allow
6307 - 64-bit operands outside of 64-bit mode,
6308 - 32-bit operands on pre-386. */
4873e243 6309 j = i.imm_operands + (t->operands > i.imm_operands + 1);
e365e234
JB
6310 if (((i.suffix == QWORD_MNEM_SUFFIX
6311 && flag_code != CODE_64BIT
6312 && (t->base_opcode != 0x0fc7
6313 || t->extension_opcode != 1 /* cmpxchg8b */))
6314 || (i.suffix == LONG_MNEM_SUFFIX
6315 && !cpu_arch_flags.bitfield.cpui386))
45aa61fe 6316 && (intel_syntax
3cd7f3e3 6317 ? (t->opcode_modifier.mnemonicsize != IGNORESIZE
45aa61fe
AM
6318 && !intel_float_operand (t->name))
6319 : intel_float_operand (t->name) != 2)
4873e243
JB
6320 && (t->operands == i.imm_operands
6321 || (operand_types[i.imm_operands].bitfield.class != RegMMX
6322 && operand_types[i.imm_operands].bitfield.class != RegSIMD
6323 && operand_types[i.imm_operands].bitfield.class != RegMask)
6324 || (operand_types[j].bitfield.class != RegMMX
6325 && operand_types[j].bitfield.class != RegSIMD
6326 && operand_types[j].bitfield.class != RegMask))
63112cd6 6327 && !t->opcode_modifier.sib)
192dc9c6
JB
6328 continue;
6329
29b0f896 6330 /* Do not verify operands when there are none. */
e365e234 6331 if (!t->operands)
da4977e0
JB
6332 {
6333 if (VEX_check_encoding (t))
6334 {
6335 specific_error = i.error;
6336 continue;
6337 }
6338
6339 /* We've found a match; break out of loop. */
6340 break;
6341 }
252b5132 6342
48bcea9f
JB
6343 if (!t->opcode_modifier.jump
6344 || t->opcode_modifier.jump == JUMP_ABSOLUTE)
6345 {
6346 /* There should be only one Disp operand. */
6347 for (j = 0; j < MAX_OPERANDS; j++)
6348 if (operand_type_check (operand_types[j], disp))
539e75ad 6349 break;
48bcea9f
JB
6350 if (j < MAX_OPERANDS)
6351 {
6352 bfd_boolean override = (i.prefix[ADDR_PREFIX] != 0);
6353
6354 addr_prefix_disp = j;
6355
6356 /* Address size prefix will turn Disp64/Disp32S/Disp32/Disp16
6357 operand into Disp32/Disp32/Disp16/Disp32 operand. */
6358 switch (flag_code)
40fb9820 6359 {
48bcea9f
JB
6360 case CODE_16BIT:
6361 override = !override;
6362 /* Fall through. */
6363 case CODE_32BIT:
6364 if (operand_types[j].bitfield.disp32
6365 && operand_types[j].bitfield.disp16)
40fb9820 6366 {
48bcea9f
JB
6367 operand_types[j].bitfield.disp16 = override;
6368 operand_types[j].bitfield.disp32 = !override;
40fb9820 6369 }
48bcea9f
JB
6370 operand_types[j].bitfield.disp32s = 0;
6371 operand_types[j].bitfield.disp64 = 0;
6372 break;
6373
6374 case CODE_64BIT:
6375 if (operand_types[j].bitfield.disp32s
6376 || operand_types[j].bitfield.disp64)
40fb9820 6377 {
48bcea9f
JB
6378 operand_types[j].bitfield.disp64 &= !override;
6379 operand_types[j].bitfield.disp32s &= !override;
6380 operand_types[j].bitfield.disp32 = override;
40fb9820 6381 }
48bcea9f
JB
6382 operand_types[j].bitfield.disp16 = 0;
6383 break;
40fb9820 6384 }
539e75ad 6385 }
48bcea9f 6386 }
539e75ad 6387
02a86693
L
6388 /* Force 0x8b encoding for "mov foo@GOT, %eax". */
6389 if (i.reloc[0] == BFD_RELOC_386_GOT32 && t->base_opcode == 0xa0)
6390 continue;
6391
56ffb741 6392 /* We check register size if needed. */
e2195274
JB
6393 if (t->opcode_modifier.checkregsize)
6394 {
6395 check_register = (1 << t->operands) - 1;
6396 if (i.broadcast)
6397 check_register &= ~(1 << i.broadcast->operand);
6398 }
6399 else
6400 check_register = 0;
6401
c6fb90c8 6402 overlap0 = operand_type_and (i.types[0], operand_types[0]);
29b0f896
AM
6403 switch (t->operands)
6404 {
6405 case 1:
40fb9820 6406 if (!operand_type_match (overlap0, i.types[0]))
29b0f896
AM
6407 continue;
6408 break;
6409 case 2:
33eaf5de 6410 /* xchg %eax, %eax is a special case. It is an alias for nop
8b38ad71
L
6411 only in 32bit mode and we can use opcode 0x90. In 64bit
6412 mode, we can't use 0x90 for xchg %eax, %eax since it should
6413 zero-extend %eax to %rax. */
6414 if (flag_code == CODE_64BIT
6415 && t->base_opcode == 0x90
75e5731b
JB
6416 && i.types[0].bitfield.instance == Accum
6417 && i.types[0].bitfield.dword
6418 && i.types[1].bitfield.instance == Accum
6419 && i.types[1].bitfield.dword)
8b38ad71 6420 continue;
1212781b
JB
6421 /* xrelease mov %eax, <disp> is another special case. It must not
6422 match the accumulator-only encoding of mov. */
6423 if (flag_code != CODE_64BIT
6424 && i.hle_prefix
6425 && t->base_opcode == 0xa0
75e5731b 6426 && i.types[0].bitfield.instance == Accum
8dc0818e 6427 && (i.flags[1] & Operand_Mem))
1212781b 6428 continue;
f5eb1d70
JB
6429 /* Fall through. */
6430
6431 case 3:
3ac21baa
JB
6432 if (!(size_match & MATCH_STRAIGHT))
6433 goto check_reverse;
64c49ab3
JB
6434 /* Reverse direction of operands if swapping is possible in the first
6435 place (operands need to be symmetric) and
6436 - the load form is requested, and the template is a store form,
6437 - the store form is requested, and the template is a load form,
6438 - the non-default (swapped) form is requested. */
6439 overlap1 = operand_type_and (operand_types[0], operand_types[1]);
f5eb1d70 6440 if (t->opcode_modifier.d && i.reg_operands == i.operands
64c49ab3
JB
6441 && !operand_type_all_zero (&overlap1))
6442 switch (i.dir_encoding)
6443 {
6444 case dir_encoding_load:
6445 if (operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 6446 || t->opcode_modifier.regmem)
64c49ab3
JB
6447 goto check_reverse;
6448 break;
6449
6450 case dir_encoding_store:
6451 if (!operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 6452 && !t->opcode_modifier.regmem)
64c49ab3
JB
6453 goto check_reverse;
6454 break;
6455
6456 case dir_encoding_swap:
6457 goto check_reverse;
6458
6459 case dir_encoding_default:
6460 break;
6461 }
86fa6981 6462 /* If we want store form, we skip the current load. */
64c49ab3
JB
6463 if ((i.dir_encoding == dir_encoding_store
6464 || i.dir_encoding == dir_encoding_swap)
86fa6981
L
6465 && i.mem_operands == 0
6466 && t->opcode_modifier.load)
fa99fab2 6467 continue;
1a0670f3 6468 /* Fall through. */
f48ff2ae 6469 case 4:
c0f3af97 6470 case 5:
c6fb90c8 6471 overlap1 = operand_type_and (i.types[1], operand_types[1]);
40fb9820
L
6472 if (!operand_type_match (overlap0, i.types[0])
6473 || !operand_type_match (overlap1, i.types[1])
e2195274 6474 || ((check_register & 3) == 3
dc821c5f 6475 && !operand_type_register_match (i.types[0],
40fb9820 6476 operand_types[0],
dc821c5f 6477 i.types[1],
40fb9820 6478 operand_types[1])))
29b0f896
AM
6479 {
6480 /* Check if other direction is valid ... */
38e314eb 6481 if (!t->opcode_modifier.d)
29b0f896
AM
6482 continue;
6483
dc1e8a47 6484 check_reverse:
3ac21baa
JB
6485 if (!(size_match & MATCH_REVERSE))
6486 continue;
29b0f896 6487 /* Try reversing direction of operands. */
f5eb1d70
JB
6488 overlap0 = operand_type_and (i.types[0], operand_types[i.operands - 1]);
6489 overlap1 = operand_type_and (i.types[i.operands - 1], operand_types[0]);
40fb9820 6490 if (!operand_type_match (overlap0, i.types[0])
f5eb1d70 6491 || !operand_type_match (overlap1, i.types[i.operands - 1])
45664ddb 6492 || (check_register
dc821c5f 6493 && !operand_type_register_match (i.types[0],
f5eb1d70
JB
6494 operand_types[i.operands - 1],
6495 i.types[i.operands - 1],
45664ddb 6496 operand_types[0])))
29b0f896
AM
6497 {
6498 /* Does not match either direction. */
6499 continue;
6500 }
38e314eb 6501 /* found_reverse_match holds which of D or FloatR
29b0f896 6502 we've found. */
38e314eb
JB
6503 if (!t->opcode_modifier.d)
6504 found_reverse_match = 0;
6505 else if (operand_types[0].bitfield.tbyte)
8a2ed489 6506 found_reverse_match = Opcode_FloatD;
dbbc8b7e 6507 else if (operand_types[0].bitfield.xmmword
f5eb1d70 6508 || operand_types[i.operands - 1].bitfield.xmmword
3528c362
JB
6509 || operand_types[0].bitfield.class == RegMMX
6510 || operand_types[i.operands - 1].bitfield.class == RegMMX
dbbc8b7e
JB
6511 || is_any_vex_encoding(t))
6512 found_reverse_match = (t->base_opcode & 0xee) != 0x6e
6513 ? Opcode_SIMD_FloatD : Opcode_SIMD_IntD;
8a2ed489 6514 else
38e314eb 6515 found_reverse_match = Opcode_D;
40fb9820 6516 if (t->opcode_modifier.floatr)
8a2ed489 6517 found_reverse_match |= Opcode_FloatR;
29b0f896 6518 }
f48ff2ae 6519 else
29b0f896 6520 {
f48ff2ae 6521 /* Found a forward 2 operand match here. */
d1cbb4db
L
6522 switch (t->operands)
6523 {
c0f3af97
L
6524 case 5:
6525 overlap4 = operand_type_and (i.types[4],
6526 operand_types[4]);
1a0670f3 6527 /* Fall through. */
d1cbb4db 6528 case 4:
c6fb90c8
L
6529 overlap3 = operand_type_and (i.types[3],
6530 operand_types[3]);
1a0670f3 6531 /* Fall through. */
d1cbb4db 6532 case 3:
c6fb90c8
L
6533 overlap2 = operand_type_and (i.types[2],
6534 operand_types[2]);
d1cbb4db
L
6535 break;
6536 }
29b0f896 6537
f48ff2ae
L
6538 switch (t->operands)
6539 {
c0f3af97
L
6540 case 5:
6541 if (!operand_type_match (overlap4, i.types[4])
dc821c5f 6542 || !operand_type_register_match (i.types[3],
c0f3af97 6543 operand_types[3],
c0f3af97
L
6544 i.types[4],
6545 operand_types[4]))
6546 continue;
1a0670f3 6547 /* Fall through. */
f48ff2ae 6548 case 4:
40fb9820 6549 if (!operand_type_match (overlap3, i.types[3])
e2195274
JB
6550 || ((check_register & 0xa) == 0xa
6551 && !operand_type_register_match (i.types[1],
f7768225
JB
6552 operand_types[1],
6553 i.types[3],
e2195274
JB
6554 operand_types[3]))
6555 || ((check_register & 0xc) == 0xc
6556 && !operand_type_register_match (i.types[2],
6557 operand_types[2],
6558 i.types[3],
6559 operand_types[3])))
f48ff2ae 6560 continue;
1a0670f3 6561 /* Fall through. */
f48ff2ae
L
6562 case 3:
6563 /* Here we make use of the fact that there are no
23e42951 6564 reverse match 3 operand instructions. */
40fb9820 6565 if (!operand_type_match (overlap2, i.types[2])
e2195274
JB
6566 || ((check_register & 5) == 5
6567 && !operand_type_register_match (i.types[0],
23e42951
JB
6568 operand_types[0],
6569 i.types[2],
e2195274
JB
6570 operand_types[2]))
6571 || ((check_register & 6) == 6
6572 && !operand_type_register_match (i.types[1],
6573 operand_types[1],
6574 i.types[2],
6575 operand_types[2])))
f48ff2ae
L
6576 continue;
6577 break;
6578 }
29b0f896 6579 }
f48ff2ae 6580 /* Found either forward/reverse 2, 3 or 4 operand match here:
29b0f896
AM
6581 slip through to break. */
6582 }
c0f3af97 6583
da4977e0
JB
6584 /* Check if vector operands are valid. */
6585 if (check_VecOperands (t))
6586 {
6587 specific_error = i.error;
6588 continue;
6589 }
6590
6591 /* Check if VEX/EVEX encoding requirements can be satisfied. */
6592 if (VEX_check_encoding (t))
5614d22c
JB
6593 {
6594 specific_error = i.error;
6595 continue;
6596 }
a683cc34 6597
29b0f896
AM
6598 /* We've found a match; break out of loop. */
6599 break;
6600 }
6601
6602 if (t == current_templates->end)
6603 {
6604 /* We found no match. */
a65babc9 6605 const char *err_msg;
5614d22c 6606 switch (specific_error ? specific_error : i.error)
a65babc9
L
6607 {
6608 default:
6609 abort ();
86e026a4 6610 case operand_size_mismatch:
a65babc9
L
6611 err_msg = _("operand size mismatch");
6612 break;
6613 case operand_type_mismatch:
6614 err_msg = _("operand type mismatch");
6615 break;
6616 case register_type_mismatch:
6617 err_msg = _("register type mismatch");
6618 break;
6619 case number_of_operands_mismatch:
6620 err_msg = _("number of operands mismatch");
6621 break;
6622 case invalid_instruction_suffix:
6623 err_msg = _("invalid instruction suffix");
6624 break;
6625 case bad_imm4:
4a2608e3 6626 err_msg = _("constant doesn't fit in 4 bits");
a65babc9 6627 break;
a65babc9
L
6628 case unsupported_with_intel_mnemonic:
6629 err_msg = _("unsupported with Intel mnemonic");
6630 break;
6631 case unsupported_syntax:
6632 err_msg = _("unsupported syntax");
6633 break;
6634 case unsupported:
35262a23 6635 as_bad (_("unsupported instruction `%s'"),
10efe3f6
L
6636 current_templates->start->name);
6637 return NULL;
260cd341
LC
6638 case invalid_sib_address:
6639 err_msg = _("invalid SIB address");
6640 break;
6c30d220
L
6641 case invalid_vsib_address:
6642 err_msg = _("invalid VSIB address");
6643 break;
7bab8ab5
JB
6644 case invalid_vector_register_set:
6645 err_msg = _("mask, index, and destination registers must be distinct");
6646 break;
260cd341
LC
6647 case invalid_tmm_register_set:
6648 err_msg = _("all tmm registers must be distinct");
6649 break;
6c30d220
L
6650 case unsupported_vector_index_register:
6651 err_msg = _("unsupported vector index register");
6652 break;
43234a1e
L
6653 case unsupported_broadcast:
6654 err_msg = _("unsupported broadcast");
6655 break;
43234a1e
L
6656 case broadcast_needed:
6657 err_msg = _("broadcast is needed for operand of such type");
6658 break;
6659 case unsupported_masking:
6660 err_msg = _("unsupported masking");
6661 break;
6662 case mask_not_on_destination:
6663 err_msg = _("mask not on destination operand");
6664 break;
6665 case no_default_mask:
6666 err_msg = _("default mask isn't allowed");
6667 break;
6668 case unsupported_rc_sae:
6669 err_msg = _("unsupported static rounding/sae");
6670 break;
6671 case rc_sae_operand_not_last_imm:
6672 if (intel_syntax)
6673 err_msg = _("RC/SAE operand must precede immediate operands");
6674 else
6675 err_msg = _("RC/SAE operand must follow immediate operands");
6676 break;
6677 case invalid_register_operand:
6678 err_msg = _("invalid register operand");
6679 break;
a65babc9
L
6680 }
6681 as_bad (_("%s for `%s'"), err_msg,
891edac4 6682 current_templates->start->name);
fa99fab2 6683 return NULL;
29b0f896 6684 }
252b5132 6685
29b0f896
AM
6686 if (!quiet_warnings)
6687 {
6688 if (!intel_syntax
0cfa3eb3 6689 && (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE)))
6f2f06be 6690 as_warn (_("indirect %s without `*'"), t->name);
29b0f896 6691
40fb9820 6692 if (t->opcode_modifier.isprefix
3cd7f3e3 6693 && t->opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
6694 {
6695 /* Warn them that a data or address size prefix doesn't
6696 affect assembly of the next line of code. */
6697 as_warn (_("stand-alone `%s' prefix"), t->name);
6698 }
6699 }
6700
6701 /* Copy the template we found. */
6702 i.tm = *t;
539e75ad
L
6703
6704 if (addr_prefix_disp != -1)
6705 i.tm.operand_types[addr_prefix_disp]
6706 = operand_types[addr_prefix_disp];
6707
29b0f896
AM
6708 if (found_reverse_match)
6709 {
dfd69174
JB
6710 /* If we found a reverse match we must alter the opcode direction
6711 bit and clear/flip the regmem modifier one. found_reverse_match
6712 holds bits to change (different for int & float insns). */
29b0f896
AM
6713
6714 i.tm.base_opcode ^= found_reverse_match;
6715
f5eb1d70
JB
6716 i.tm.operand_types[0] = operand_types[i.operands - 1];
6717 i.tm.operand_types[i.operands - 1] = operand_types[0];
dfd69174
JB
6718
6719 /* Certain SIMD insns have their load forms specified in the opcode
6720 table, and hence we need to _set_ RegMem instead of clearing it.
6721 We need to avoid setting the bit though on insns like KMOVW. */
6722 i.tm.opcode_modifier.regmem
6723 = i.tm.opcode_modifier.modrm && i.tm.opcode_modifier.d
6724 && i.tm.operands > 2U - i.tm.opcode_modifier.sse2avx
6725 && !i.tm.opcode_modifier.regmem;
29b0f896
AM
6726 }
6727
fa99fab2 6728 return t;
29b0f896
AM
6729}
6730
6731static int
e3bb37b5 6732check_string (void)
29b0f896 6733{
51c8edf6
JB
6734 unsigned int es_op = i.tm.opcode_modifier.isstring - IS_STRING_ES_OP0;
6735 unsigned int op = i.tm.operand_types[0].bitfield.baseindex ? es_op : 0;
8dc0818e 6736
51c8edf6 6737 if (i.seg[op] != NULL && i.seg[op] != &es)
29b0f896 6738 {
51c8edf6
JB
6739 as_bad (_("`%s' operand %u must use `%ses' segment"),
6740 i.tm.name,
6741 intel_syntax ? i.tm.operands - es_op : es_op + 1,
6742 register_prefix);
6743 return 0;
29b0f896 6744 }
51c8edf6
JB
6745
6746 /* There's only ever one segment override allowed per instruction.
6747 This instruction possibly has a legal segment override on the
6748 second operand, so copy the segment to where non-string
6749 instructions store it, allowing common code. */
6750 i.seg[op] = i.seg[1];
6751
29b0f896
AM
6752 return 1;
6753}
6754
6755static int
543613e9 6756process_suffix (void)
29b0f896
AM
6757{
6758 /* If matched instruction specifies an explicit instruction mnemonic
6759 suffix, use it. */
673fe0f0 6760 if (i.tm.opcode_modifier.size == SIZE16)
40fb9820 6761 i.suffix = WORD_MNEM_SUFFIX;
673fe0f0 6762 else if (i.tm.opcode_modifier.size == SIZE32)
40fb9820 6763 i.suffix = LONG_MNEM_SUFFIX;
673fe0f0 6764 else if (i.tm.opcode_modifier.size == SIZE64)
40fb9820 6765 i.suffix = QWORD_MNEM_SUFFIX;
13e600d0 6766 else if (i.reg_operands
c8f8eebc
JB
6767 && (i.operands > 1 || i.types[0].bitfield.class == Reg)
6768 && !i.tm.opcode_modifier.addrprefixopreg)
29b0f896 6769 {
65fca059
JB
6770 unsigned int numop = i.operands;
6771
6772 /* movsx/movzx want only their source operand considered here, for the
6773 ambiguity checking below. The suffix will be replaced afterwards
6774 to represent the destination (register). */
6775 if (((i.tm.base_opcode | 8) == 0xfbe && i.tm.opcode_modifier.w)
6776 || (i.tm.base_opcode == 0x63 && i.tm.cpu_flags.bitfield.cpu64))
6777 --i.operands;
6778
643bb870
JB
6779 /* crc32 needs REX.W set regardless of suffix / source operand size. */
6780 if (i.tm.base_opcode == 0xf20f38f0
6781 && i.tm.operand_types[1].bitfield.qword)
6782 i.rex |= REX_W;
6783
29b0f896 6784 /* If there's no instruction mnemonic suffix we try to invent one
13e600d0 6785 based on GPR operands. */
29b0f896
AM
6786 if (!i.suffix)
6787 {
6788 /* We take i.suffix from the last register operand specified,
6789 Destination register type is more significant than source
381d071f
L
6790 register type. crc32 in SSE4.2 prefers source register
6791 type. */
1a035124 6792 unsigned int op = i.tm.base_opcode != 0xf20f38f0 ? i.operands : 1;
20592a94 6793
1a035124
JB
6794 while (op--)
6795 if (i.tm.operand_types[op].bitfield.instance == InstanceNone
6796 || i.tm.operand_types[op].bitfield.instance == Accum)
6797 {
6798 if (i.types[op].bitfield.class != Reg)
6799 continue;
6800 if (i.types[op].bitfield.byte)
6801 i.suffix = BYTE_MNEM_SUFFIX;
6802 else if (i.types[op].bitfield.word)
6803 i.suffix = WORD_MNEM_SUFFIX;
6804 else if (i.types[op].bitfield.dword)
6805 i.suffix = LONG_MNEM_SUFFIX;
6806 else if (i.types[op].bitfield.qword)
6807 i.suffix = QWORD_MNEM_SUFFIX;
6808 else
6809 continue;
6810 break;
6811 }
65fca059
JB
6812
6813 /* As an exception, movsx/movzx silently default to a byte source
6814 in AT&T mode. */
6815 if ((i.tm.base_opcode | 8) == 0xfbe && i.tm.opcode_modifier.w
6816 && !i.suffix && !intel_syntax)
6817 i.suffix = BYTE_MNEM_SUFFIX;
29b0f896
AM
6818 }
6819 else if (i.suffix == BYTE_MNEM_SUFFIX)
6820 {
2eb952a4 6821 if (intel_syntax
3cd7f3e3 6822 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
2eb952a4
L
6823 && i.tm.opcode_modifier.no_bsuf)
6824 i.suffix = 0;
6825 else if (!check_byte_reg ())
29b0f896
AM
6826 return 0;
6827 }
6828 else if (i.suffix == LONG_MNEM_SUFFIX)
6829 {
2eb952a4 6830 if (intel_syntax
3cd7f3e3 6831 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
9f123b91
JB
6832 && i.tm.opcode_modifier.no_lsuf
6833 && !i.tm.opcode_modifier.todword
6834 && !i.tm.opcode_modifier.toqword)
2eb952a4
L
6835 i.suffix = 0;
6836 else if (!check_long_reg ())
29b0f896
AM
6837 return 0;
6838 }
6839 else if (i.suffix == QWORD_MNEM_SUFFIX)
6840 {
955e1e6a 6841 if (intel_syntax
3cd7f3e3 6842 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
9f123b91
JB
6843 && i.tm.opcode_modifier.no_qsuf
6844 && !i.tm.opcode_modifier.todword
6845 && !i.tm.opcode_modifier.toqword)
955e1e6a
L
6846 i.suffix = 0;
6847 else if (!check_qword_reg ())
29b0f896
AM
6848 return 0;
6849 }
6850 else if (i.suffix == WORD_MNEM_SUFFIX)
6851 {
2eb952a4 6852 if (intel_syntax
3cd7f3e3 6853 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
2eb952a4
L
6854 && i.tm.opcode_modifier.no_wsuf)
6855 i.suffix = 0;
6856 else if (!check_word_reg ())
29b0f896
AM
6857 return 0;
6858 }
3cd7f3e3
L
6859 else if (intel_syntax
6860 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
6861 /* Do nothing if the instruction is going to ignore the prefix. */
6862 ;
6863 else
6864 abort ();
65fca059
JB
6865
6866 /* Undo the movsx/movzx change done above. */
6867 i.operands = numop;
29b0f896 6868 }
3cd7f3e3
L
6869 else if (i.tm.opcode_modifier.mnemonicsize == DEFAULTSIZE
6870 && !i.suffix)
29b0f896 6871 {
13e600d0
JB
6872 i.suffix = stackop_size;
6873 if (stackop_size == LONG_MNEM_SUFFIX)
06f74c5c
L
6874 {
6875 /* stackop_size is set to LONG_MNEM_SUFFIX for the
6876 .code16gcc directive to support 16-bit mode with
6877 32-bit address. For IRET without a suffix, generate
6878 16-bit IRET (opcode 0xcf) to return from an interrupt
6879 handler. */
13e600d0
JB
6880 if (i.tm.base_opcode == 0xcf)
6881 {
6882 i.suffix = WORD_MNEM_SUFFIX;
6883 as_warn (_("generating 16-bit `iret' for .code16gcc directive"));
6884 }
6885 /* Warn about changed behavior for segment register push/pop. */
6886 else if ((i.tm.base_opcode | 1) == 0x07)
6887 as_warn (_("generating 32-bit `%s', unlike earlier gas versions"),
6888 i.tm.name);
06f74c5c 6889 }
29b0f896 6890 }
c006a730 6891 else if (!i.suffix
0cfa3eb3
JB
6892 && (i.tm.opcode_modifier.jump == JUMP_ABSOLUTE
6893 || i.tm.opcode_modifier.jump == JUMP_BYTE
6894 || i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT
64e74474
AM
6895 || (i.tm.base_opcode == 0x0f01 /* [ls][gi]dt */
6896 && i.tm.extension_opcode <= 3)))
9306ca4a
JB
6897 {
6898 switch (flag_code)
6899 {
6900 case CODE_64BIT:
40fb9820 6901 if (!i.tm.opcode_modifier.no_qsuf)
9306ca4a 6902 {
828c2a25
JB
6903 if (i.tm.opcode_modifier.jump == JUMP_BYTE
6904 || i.tm.opcode_modifier.no_lsuf)
6905 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a
JB
6906 break;
6907 }
1a0670f3 6908 /* Fall through. */
9306ca4a 6909 case CODE_32BIT:
40fb9820 6910 if (!i.tm.opcode_modifier.no_lsuf)
9306ca4a
JB
6911 i.suffix = LONG_MNEM_SUFFIX;
6912 break;
6913 case CODE_16BIT:
40fb9820 6914 if (!i.tm.opcode_modifier.no_wsuf)
9306ca4a
JB
6915 i.suffix = WORD_MNEM_SUFFIX;
6916 break;
6917 }
6918 }
252b5132 6919
c006a730 6920 if (!i.suffix
3cd7f3e3 6921 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8
JB
6922 /* Also cover lret/retf/iret in 64-bit mode. */
6923 || (flag_code == CODE_64BIT
6924 && !i.tm.opcode_modifier.no_lsuf
6925 && !i.tm.opcode_modifier.no_qsuf))
3cd7f3e3 6926 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
8bbb3ad8
JB
6927 /* Explicit sizing prefixes are assumed to disambiguate insns. */
6928 && !i.prefix[DATA_PREFIX] && !(i.prefix[REX_PREFIX] & REX_W)
62b3f548
JB
6929 /* Accept FLDENV et al without suffix. */
6930 && (i.tm.opcode_modifier.no_ssuf || i.tm.opcode_modifier.floatmf))
29b0f896 6931 {
6c0946d0 6932 unsigned int suffixes, evex = 0;
c006a730
JB
6933
6934 suffixes = !i.tm.opcode_modifier.no_bsuf;
6935 if (!i.tm.opcode_modifier.no_wsuf)
6936 suffixes |= 1 << 1;
6937 if (!i.tm.opcode_modifier.no_lsuf)
6938 suffixes |= 1 << 2;
6939 if (!i.tm.opcode_modifier.no_ldsuf)
6940 suffixes |= 1 << 3;
6941 if (!i.tm.opcode_modifier.no_ssuf)
6942 suffixes |= 1 << 4;
6943 if (flag_code == CODE_64BIT && !i.tm.opcode_modifier.no_qsuf)
6944 suffixes |= 1 << 5;
6945
6c0946d0
JB
6946 /* For [XYZ]MMWORD operands inspect operand sizes. While generally
6947 also suitable for AT&T syntax mode, it was requested that this be
6948 restricted to just Intel syntax. */
b9915cbc 6949 if (intel_syntax && is_any_vex_encoding (&i.tm) && !i.broadcast)
6c0946d0 6950 {
b9915cbc 6951 unsigned int op;
6c0946d0 6952
b9915cbc 6953 for (op = 0; op < i.tm.operands; ++op)
6c0946d0 6954 {
b9915cbc
JB
6955 if (is_evex_encoding (&i.tm)
6956 && !cpu_arch_flags.bitfield.cpuavx512vl)
6c0946d0 6957 {
b9915cbc
JB
6958 if (i.tm.operand_types[op].bitfield.ymmword)
6959 i.tm.operand_types[op].bitfield.xmmword = 0;
6960 if (i.tm.operand_types[op].bitfield.zmmword)
6961 i.tm.operand_types[op].bitfield.ymmword = 0;
6962 if (!i.tm.opcode_modifier.evex
6963 || i.tm.opcode_modifier.evex == EVEXDYN)
6964 i.tm.opcode_modifier.evex = EVEX512;
6965 }
6c0946d0 6966
b9915cbc
JB
6967 if (i.tm.operand_types[op].bitfield.xmmword
6968 + i.tm.operand_types[op].bitfield.ymmword
6969 + i.tm.operand_types[op].bitfield.zmmword < 2)
6970 continue;
6c0946d0 6971
b9915cbc
JB
6972 /* Any properly sized operand disambiguates the insn. */
6973 if (i.types[op].bitfield.xmmword
6974 || i.types[op].bitfield.ymmword
6975 || i.types[op].bitfield.zmmword)
6976 {
6977 suffixes &= ~(7 << 6);
6978 evex = 0;
6979 break;
6980 }
6c0946d0 6981
b9915cbc
JB
6982 if ((i.flags[op] & Operand_Mem)
6983 && i.tm.operand_types[op].bitfield.unspecified)
6984 {
6985 if (i.tm.operand_types[op].bitfield.xmmword)
6986 suffixes |= 1 << 6;
6987 if (i.tm.operand_types[op].bitfield.ymmword)
6988 suffixes |= 1 << 7;
6989 if (i.tm.operand_types[op].bitfield.zmmword)
6990 suffixes |= 1 << 8;
6991 if (is_evex_encoding (&i.tm))
6992 evex = EVEX512;
6c0946d0
JB
6993 }
6994 }
6995 }
6996
6997 /* Are multiple suffixes / operand sizes allowed? */
c006a730 6998 if (suffixes & (suffixes - 1))
9306ca4a 6999 {
873494c8 7000 if (intel_syntax
3cd7f3e3 7001 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8 7002 || operand_check == check_error))
9306ca4a 7003 {
c006a730 7004 as_bad (_("ambiguous operand size for `%s'"), i.tm.name);
9306ca4a
JB
7005 return 0;
7006 }
c006a730 7007 if (operand_check == check_error)
9306ca4a 7008 {
c006a730
JB
7009 as_bad (_("no instruction mnemonic suffix given and "
7010 "no register operands; can't size `%s'"), i.tm.name);
9306ca4a
JB
7011 return 0;
7012 }
c006a730 7013 if (operand_check == check_warning)
873494c8
JB
7014 as_warn (_("%s; using default for `%s'"),
7015 intel_syntax
7016 ? _("ambiguous operand size")
7017 : _("no instruction mnemonic suffix given and "
7018 "no register operands"),
7019 i.tm.name);
c006a730
JB
7020
7021 if (i.tm.opcode_modifier.floatmf)
7022 i.suffix = SHORT_MNEM_SUFFIX;
65fca059
JB
7023 else if ((i.tm.base_opcode | 8) == 0xfbe
7024 || (i.tm.base_opcode == 0x63
7025 && i.tm.cpu_flags.bitfield.cpu64))
7026 /* handled below */;
6c0946d0
JB
7027 else if (evex)
7028 i.tm.opcode_modifier.evex = evex;
c006a730
JB
7029 else if (flag_code == CODE_16BIT)
7030 i.suffix = WORD_MNEM_SUFFIX;
1a035124 7031 else if (!i.tm.opcode_modifier.no_lsuf)
c006a730 7032 i.suffix = LONG_MNEM_SUFFIX;
1a035124
JB
7033 else
7034 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a 7035 }
29b0f896 7036 }
252b5132 7037
65fca059
JB
7038 if ((i.tm.base_opcode | 8) == 0xfbe
7039 || (i.tm.base_opcode == 0x63 && i.tm.cpu_flags.bitfield.cpu64))
7040 {
7041 /* In Intel syntax, movsx/movzx must have a "suffix" (checked above).
7042 In AT&T syntax, if there is no suffix (warned about above), the default
7043 will be byte extension. */
7044 if (i.tm.opcode_modifier.w && i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
7045 i.tm.base_opcode |= 1;
7046
7047 /* For further processing, the suffix should represent the destination
7048 (register). This is already the case when one was used with
7049 mov[sz][bw]*, but we need to replace it for mov[sz]x, or if there was
7050 no suffix to begin with. */
7051 if (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63 || !i.suffix)
7052 {
7053 if (i.types[1].bitfield.word)
7054 i.suffix = WORD_MNEM_SUFFIX;
7055 else if (i.types[1].bitfield.qword)
7056 i.suffix = QWORD_MNEM_SUFFIX;
7057 else
7058 i.suffix = LONG_MNEM_SUFFIX;
7059
7060 i.tm.opcode_modifier.w = 0;
7061 }
7062 }
7063
50128d0c
JB
7064 if (!i.tm.opcode_modifier.modrm && i.reg_operands && i.tm.operands < 3)
7065 i.short_form = (i.tm.operand_types[0].bitfield.class == Reg)
7066 != (i.tm.operand_types[1].bitfield.class == Reg);
7067
d2224064
JB
7068 /* Change the opcode based on the operand size given by i.suffix. */
7069 switch (i.suffix)
29b0f896 7070 {
d2224064
JB
7071 /* Size floating point instruction. */
7072 case LONG_MNEM_SUFFIX:
7073 if (i.tm.opcode_modifier.floatmf)
7074 {
7075 i.tm.base_opcode ^= 4;
7076 break;
7077 }
7078 /* fall through */
7079 case WORD_MNEM_SUFFIX:
7080 case QWORD_MNEM_SUFFIX:
29b0f896 7081 /* It's not a byte, select word/dword operation. */
40fb9820 7082 if (i.tm.opcode_modifier.w)
29b0f896 7083 {
50128d0c 7084 if (i.short_form)
29b0f896
AM
7085 i.tm.base_opcode |= 8;
7086 else
7087 i.tm.base_opcode |= 1;
7088 }
d2224064
JB
7089 /* fall through */
7090 case SHORT_MNEM_SUFFIX:
29b0f896
AM
7091 /* Now select between word & dword operations via the operand
7092 size prefix, except for instructions that will ignore this
7093 prefix anyway. */
c8f8eebc 7094 if (i.suffix != QWORD_MNEM_SUFFIX
3cd7f3e3 7095 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
c8f8eebc
JB
7096 && !i.tm.opcode_modifier.floatmf
7097 && !is_any_vex_encoding (&i.tm)
7098 && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
7099 || (flag_code == CODE_64BIT
7100 && i.tm.opcode_modifier.jump == JUMP_BYTE)))
24eab124
AM
7101 {
7102 unsigned int prefix = DATA_PREFIX_OPCODE;
543613e9 7103
0cfa3eb3 7104 if (i.tm.opcode_modifier.jump == JUMP_BYTE) /* jcxz, loop */
29b0f896 7105 prefix = ADDR_PREFIX_OPCODE;
252b5132 7106
29b0f896
AM
7107 if (!add_prefix (prefix))
7108 return 0;
24eab124 7109 }
252b5132 7110
29b0f896
AM
7111 /* Set mode64 for an operand. */
7112 if (i.suffix == QWORD_MNEM_SUFFIX
9146926a 7113 && flag_code == CODE_64BIT
d2224064 7114 && !i.tm.opcode_modifier.norex64
4ed21b58 7115 && !i.tm.opcode_modifier.vexw
46e883c5 7116 /* Special case for xchg %rax,%rax. It is NOP and doesn't
d2224064
JB
7117 need rex64. */
7118 && ! (i.operands == 2
7119 && i.tm.base_opcode == 0x90
7120 && i.tm.extension_opcode == None
75e5731b
JB
7121 && i.types[0].bitfield.instance == Accum
7122 && i.types[0].bitfield.qword
7123 && i.types[1].bitfield.instance == Accum
7124 && i.types[1].bitfield.qword))
d2224064 7125 i.rex |= REX_W;
3e73aa7c 7126
d2224064 7127 break;
8bbb3ad8
JB
7128
7129 case 0:
7130 /* Select word/dword/qword operation with explict data sizing prefix
7131 when there are no suitable register operands. */
7132 if (i.tm.opcode_modifier.w
7133 && (i.prefix[DATA_PREFIX] || (i.prefix[REX_PREFIX] & REX_W))
7134 && (!i.reg_operands
7135 || (i.reg_operands == 1
7136 /* ShiftCount */
7137 && (i.tm.operand_types[0].bitfield.instance == RegC
7138 /* InOutPortReg */
7139 || i.tm.operand_types[0].bitfield.instance == RegD
7140 || i.tm.operand_types[1].bitfield.instance == RegD
7141 /* CRC32 */
7142 || i.tm.base_opcode == 0xf20f38f0))))
7143 i.tm.base_opcode |= 1;
7144 break;
29b0f896 7145 }
7ecd2f8b 7146
c8f8eebc 7147 if (i.tm.opcode_modifier.addrprefixopreg)
c0a30a9f 7148 {
c8f8eebc
JB
7149 gas_assert (!i.suffix);
7150 gas_assert (i.reg_operands);
c0a30a9f 7151
c8f8eebc
JB
7152 if (i.tm.operand_types[0].bitfield.instance == Accum
7153 || i.operands == 1)
7154 {
7155 /* The address size override prefix changes the size of the
7156 first operand. */
7157 if (flag_code == CODE_64BIT
7158 && i.op[0].regs->reg_type.bitfield.word)
7159 {
7160 as_bad (_("16-bit addressing unavailable for `%s'"),
7161 i.tm.name);
7162 return 0;
7163 }
7164
7165 if ((flag_code == CODE_32BIT
7166 ? i.op[0].regs->reg_type.bitfield.word
7167 : i.op[0].regs->reg_type.bitfield.dword)
7168 && !add_prefix (ADDR_PREFIX_OPCODE))
7169 return 0;
7170 }
c0a30a9f
L
7171 else
7172 {
c8f8eebc
JB
7173 /* Check invalid register operand when the address size override
7174 prefix changes the size of register operands. */
7175 unsigned int op;
7176 enum { need_word, need_dword, need_qword } need;
7177
7178 if (flag_code == CODE_32BIT)
7179 need = i.prefix[ADDR_PREFIX] ? need_word : need_dword;
7180 else if (i.prefix[ADDR_PREFIX])
c0a30a9f
L
7181 need = need_dword;
7182 else
7183 need = flag_code == CODE_64BIT ? need_qword : need_word;
c0a30a9f 7184
c8f8eebc
JB
7185 for (op = 0; op < i.operands; op++)
7186 {
7187 if (i.types[op].bitfield.class != Reg)
7188 continue;
7189
7190 switch (need)
7191 {
7192 case need_word:
7193 if (i.op[op].regs->reg_type.bitfield.word)
7194 continue;
7195 break;
7196 case need_dword:
7197 if (i.op[op].regs->reg_type.bitfield.dword)
7198 continue;
7199 break;
7200 case need_qword:
7201 if (i.op[op].regs->reg_type.bitfield.qword)
7202 continue;
7203 break;
7204 }
7205
7206 as_bad (_("invalid register operand size for `%s'"),
7207 i.tm.name);
7208 return 0;
7209 }
7210 }
c0a30a9f
L
7211 }
7212
29b0f896
AM
7213 return 1;
7214}
3e73aa7c 7215
29b0f896 7216static int
543613e9 7217check_byte_reg (void)
29b0f896
AM
7218{
7219 int op;
543613e9 7220
29b0f896
AM
7221 for (op = i.operands; --op >= 0;)
7222 {
dc821c5f 7223 /* Skip non-register operands. */
bab6aec1 7224 if (i.types[op].bitfield.class != Reg)
dc821c5f
JB
7225 continue;
7226
29b0f896
AM
7227 /* If this is an eight bit register, it's OK. If it's the 16 or
7228 32 bit version of an eight bit register, we will just use the
7229 low portion, and that's OK too. */
dc821c5f 7230 if (i.types[op].bitfield.byte)
29b0f896
AM
7231 continue;
7232
5a819eb9 7233 /* I/O port address operands are OK too. */
75e5731b
JB
7234 if (i.tm.operand_types[op].bitfield.instance == RegD
7235 && i.tm.operand_types[op].bitfield.word)
5a819eb9
JB
7236 continue;
7237
9706160a
JB
7238 /* crc32 only wants its source operand checked here. */
7239 if (i.tm.base_opcode == 0xf20f38f0 && op)
9344ff29
L
7240 continue;
7241
29b0f896 7242 /* Any other register is bad. */
73c76375
JB
7243 as_bad (_("`%s%s' not allowed with `%s%c'"),
7244 register_prefix, i.op[op].regs->reg_name,
7245 i.tm.name, i.suffix);
7246 return 0;
29b0f896
AM
7247 }
7248 return 1;
7249}
7250
7251static int
e3bb37b5 7252check_long_reg (void)
29b0f896
AM
7253{
7254 int op;
7255
7256 for (op = i.operands; --op >= 0;)
dc821c5f 7257 /* Skip non-register operands. */
bab6aec1 7258 if (i.types[op].bitfield.class != Reg)
dc821c5f 7259 continue;
29b0f896
AM
7260 /* Reject eight bit registers, except where the template requires
7261 them. (eg. movzb) */
dc821c5f 7262 else if (i.types[op].bitfield.byte
bab6aec1 7263 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7264 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7265 && (i.tm.operand_types[op].bitfield.word
7266 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7267 {
a540244d
L
7268 as_bad (_("`%s%s' not allowed with `%s%c'"),
7269 register_prefix,
29b0f896
AM
7270 i.op[op].regs->reg_name,
7271 i.tm.name,
7272 i.suffix);
7273 return 0;
7274 }
be4c5e58
L
7275 /* Error if the e prefix on a general reg is missing. */
7276 else if (i.types[op].bitfield.word
bab6aec1 7277 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7278 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7279 && i.tm.operand_types[op].bitfield.dword)
29b0f896 7280 {
be4c5e58
L
7281 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7282 register_prefix, i.op[op].regs->reg_name,
7283 i.suffix);
7284 return 0;
252b5132 7285 }
e4630f71 7286 /* Warn if the r prefix on a general reg is present. */
dc821c5f 7287 else if (i.types[op].bitfield.qword
bab6aec1 7288 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7289 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7290 && i.tm.operand_types[op].bitfield.dword)
252b5132 7291 {
34828aad 7292 if (intel_syntax
65fca059 7293 && i.tm.opcode_modifier.toqword
3528c362 7294 && i.types[0].bitfield.class != RegSIMD)
34828aad 7295 {
ca61edf2 7296 /* Convert to QWORD. We want REX byte. */
34828aad
L
7297 i.suffix = QWORD_MNEM_SUFFIX;
7298 }
7299 else
7300 {
2b5d6a91 7301 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
34828aad
L
7302 register_prefix, i.op[op].regs->reg_name,
7303 i.suffix);
7304 return 0;
7305 }
29b0f896
AM
7306 }
7307 return 1;
7308}
252b5132 7309
29b0f896 7310static int
e3bb37b5 7311check_qword_reg (void)
29b0f896
AM
7312{
7313 int op;
252b5132 7314
29b0f896 7315 for (op = i.operands; --op >= 0; )
dc821c5f 7316 /* Skip non-register operands. */
bab6aec1 7317 if (i.types[op].bitfield.class != Reg)
dc821c5f 7318 continue;
29b0f896
AM
7319 /* Reject eight bit registers, except where the template requires
7320 them. (eg. movzb) */
dc821c5f 7321 else if (i.types[op].bitfield.byte
bab6aec1 7322 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7323 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7324 && (i.tm.operand_types[op].bitfield.word
7325 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7326 {
a540244d
L
7327 as_bad (_("`%s%s' not allowed with `%s%c'"),
7328 register_prefix,
29b0f896
AM
7329 i.op[op].regs->reg_name,
7330 i.tm.name,
7331 i.suffix);
7332 return 0;
7333 }
e4630f71 7334 /* Warn if the r prefix on a general reg is missing. */
dc821c5f
JB
7335 else if ((i.types[op].bitfield.word
7336 || i.types[op].bitfield.dword)
bab6aec1 7337 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7338 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7339 && i.tm.operand_types[op].bitfield.qword)
29b0f896
AM
7340 {
7341 /* Prohibit these changes in the 64bit mode, since the
7342 lowering is more complicated. */
34828aad 7343 if (intel_syntax
ca61edf2 7344 && i.tm.opcode_modifier.todword
3528c362 7345 && i.types[0].bitfield.class != RegSIMD)
34828aad 7346 {
ca61edf2 7347 /* Convert to DWORD. We don't want REX byte. */
34828aad
L
7348 i.suffix = LONG_MNEM_SUFFIX;
7349 }
7350 else
7351 {
2b5d6a91 7352 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
34828aad
L
7353 register_prefix, i.op[op].regs->reg_name,
7354 i.suffix);
7355 return 0;
7356 }
252b5132 7357 }
29b0f896
AM
7358 return 1;
7359}
252b5132 7360
29b0f896 7361static int
e3bb37b5 7362check_word_reg (void)
29b0f896
AM
7363{
7364 int op;
7365 for (op = i.operands; --op >= 0;)
dc821c5f 7366 /* Skip non-register operands. */
bab6aec1 7367 if (i.types[op].bitfield.class != Reg)
dc821c5f 7368 continue;
29b0f896
AM
7369 /* Reject eight bit registers, except where the template requires
7370 them. (eg. movzb) */
dc821c5f 7371 else if (i.types[op].bitfield.byte
bab6aec1 7372 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7373 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7374 && (i.tm.operand_types[op].bitfield.word
7375 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7376 {
a540244d
L
7377 as_bad (_("`%s%s' not allowed with `%s%c'"),
7378 register_prefix,
29b0f896
AM
7379 i.op[op].regs->reg_name,
7380 i.tm.name,
7381 i.suffix);
7382 return 0;
7383 }
9706160a
JB
7384 /* Error if the e or r prefix on a general reg is present. */
7385 else if ((i.types[op].bitfield.dword
dc821c5f 7386 || i.types[op].bitfield.qword)
bab6aec1 7387 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7388 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7389 && i.tm.operand_types[op].bitfield.word)
252b5132 7390 {
9706160a
JB
7391 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7392 register_prefix, i.op[op].regs->reg_name,
7393 i.suffix);
7394 return 0;
29b0f896
AM
7395 }
7396 return 1;
7397}
252b5132 7398
29b0f896 7399static int
40fb9820 7400update_imm (unsigned int j)
29b0f896 7401{
bc0844ae 7402 i386_operand_type overlap = i.types[j];
40fb9820
L
7403 if ((overlap.bitfield.imm8
7404 || overlap.bitfield.imm8s
7405 || overlap.bitfield.imm16
7406 || overlap.bitfield.imm32
7407 || overlap.bitfield.imm32s
7408 || overlap.bitfield.imm64)
0dfbf9d7
L
7409 && !operand_type_equal (&overlap, &imm8)
7410 && !operand_type_equal (&overlap, &imm8s)
7411 && !operand_type_equal (&overlap, &imm16)
7412 && !operand_type_equal (&overlap, &imm32)
7413 && !operand_type_equal (&overlap, &imm32s)
7414 && !operand_type_equal (&overlap, &imm64))
29b0f896
AM
7415 {
7416 if (i.suffix)
7417 {
40fb9820
L
7418 i386_operand_type temp;
7419
0dfbf9d7 7420 operand_type_set (&temp, 0);
7ab9ffdd 7421 if (i.suffix == BYTE_MNEM_SUFFIX)
40fb9820
L
7422 {
7423 temp.bitfield.imm8 = overlap.bitfield.imm8;
7424 temp.bitfield.imm8s = overlap.bitfield.imm8s;
7425 }
7426 else if (i.suffix == WORD_MNEM_SUFFIX)
7427 temp.bitfield.imm16 = overlap.bitfield.imm16;
7428 else if (i.suffix == QWORD_MNEM_SUFFIX)
7429 {
7430 temp.bitfield.imm64 = overlap.bitfield.imm64;
7431 temp.bitfield.imm32s = overlap.bitfield.imm32s;
7432 }
7433 else
7434 temp.bitfield.imm32 = overlap.bitfield.imm32;
7435 overlap = temp;
29b0f896 7436 }
0dfbf9d7
L
7437 else if (operand_type_equal (&overlap, &imm16_32_32s)
7438 || operand_type_equal (&overlap, &imm16_32)
7439 || operand_type_equal (&overlap, &imm16_32s))
29b0f896 7440 {
40fb9820 7441 if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
65da13b5 7442 overlap = imm16;
40fb9820 7443 else
65da13b5 7444 overlap = imm32s;
29b0f896 7445 }
8bbb3ad8
JB
7446 else if (i.prefix[REX_PREFIX] & REX_W)
7447 overlap = operand_type_and (overlap, imm32s);
7448 else if (i.prefix[DATA_PREFIX])
7449 overlap = operand_type_and (overlap,
7450 flag_code != CODE_16BIT ? imm16 : imm32);
0dfbf9d7
L
7451 if (!operand_type_equal (&overlap, &imm8)
7452 && !operand_type_equal (&overlap, &imm8s)
7453 && !operand_type_equal (&overlap, &imm16)
7454 && !operand_type_equal (&overlap, &imm32)
7455 && !operand_type_equal (&overlap, &imm32s)
7456 && !operand_type_equal (&overlap, &imm64))
29b0f896 7457 {
4eed87de
AM
7458 as_bad (_("no instruction mnemonic suffix given; "
7459 "can't determine immediate size"));
29b0f896
AM
7460 return 0;
7461 }
7462 }
40fb9820 7463 i.types[j] = overlap;
29b0f896 7464
40fb9820
L
7465 return 1;
7466}
7467
7468static int
7469finalize_imm (void)
7470{
bc0844ae 7471 unsigned int j, n;
29b0f896 7472
bc0844ae
L
7473 /* Update the first 2 immediate operands. */
7474 n = i.operands > 2 ? 2 : i.operands;
7475 if (n)
7476 {
7477 for (j = 0; j < n; j++)
7478 if (update_imm (j) == 0)
7479 return 0;
40fb9820 7480
bc0844ae
L
7481 /* The 3rd operand can't be immediate operand. */
7482 gas_assert (operand_type_check (i.types[2], imm) == 0);
7483 }
29b0f896
AM
7484
7485 return 1;
7486}
7487
7488static int
e3bb37b5 7489process_operands (void)
29b0f896
AM
7490{
7491 /* Default segment register this instruction will use for memory
7492 accesses. 0 means unknown. This is only for optimizing out
7493 unnecessary segment overrides. */
7494 const seg_entry *default_seg = 0;
7495
a5aeccd9
JB
7496 if (i.tm.opcode_modifier.sse2avx)
7497 {
7498 /* Legacy encoded insns allow explicit REX prefixes, so these prefixes
7499 need converting. */
7500 i.rex |= i.prefix[REX_PREFIX] & (REX_W | REX_R | REX_X | REX_B);
7501 i.prefix[REX_PREFIX] = 0;
7502 i.rex_encoding = 0;
7503 }
c423d21a
JB
7504 /* ImmExt should be processed after SSE2AVX. */
7505 else if (i.tm.opcode_modifier.immext)
7506 process_immext ();
a5aeccd9 7507
2426c15f 7508 if (i.tm.opcode_modifier.sse2avx && i.tm.opcode_modifier.vexvvvv)
29b0f896 7509 {
91d6fa6a
NC
7510 unsigned int dupl = i.operands;
7511 unsigned int dest = dupl - 1;
9fcfb3d7
L
7512 unsigned int j;
7513
c0f3af97 7514 /* The destination must be an xmm register. */
9c2799c2 7515 gas_assert (i.reg_operands
91d6fa6a 7516 && MAX_OPERANDS > dupl
7ab9ffdd 7517 && operand_type_equal (&i.types[dest], &regxmm));
c0f3af97 7518
75e5731b 7519 if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 7520 && i.tm.operand_types[0].bitfield.xmmword)
e2ec9d29 7521 {
8cd7925b 7522 if (i.tm.opcode_modifier.vexsources == VEX3SOURCES)
c0f3af97
L
7523 {
7524 /* Keep xmm0 for instructions with VEX prefix and 3
7525 sources. */
75e5731b 7526 i.tm.operand_types[0].bitfield.instance = InstanceNone;
3528c362 7527 i.tm.operand_types[0].bitfield.class = RegSIMD;
c0f3af97
L
7528 goto duplicate;
7529 }
e2ec9d29 7530 else
c0f3af97
L
7531 {
7532 /* We remove the first xmm0 and keep the number of
7533 operands unchanged, which in fact duplicates the
7534 destination. */
7535 for (j = 1; j < i.operands; j++)
7536 {
7537 i.op[j - 1] = i.op[j];
7538 i.types[j - 1] = i.types[j];
7539 i.tm.operand_types[j - 1] = i.tm.operand_types[j];
8dc0818e 7540 i.flags[j - 1] = i.flags[j];
c0f3af97
L
7541 }
7542 }
7543 }
7544 else if (i.tm.opcode_modifier.implicit1stxmm0)
7ab9ffdd 7545 {
91d6fa6a 7546 gas_assert ((MAX_OPERANDS - 1) > dupl
8cd7925b
L
7547 && (i.tm.opcode_modifier.vexsources
7548 == VEX3SOURCES));
c0f3af97
L
7549
7550 /* Add the implicit xmm0 for instructions with VEX prefix
7551 and 3 sources. */
7552 for (j = i.operands; j > 0; j--)
7553 {
7554 i.op[j] = i.op[j - 1];
7555 i.types[j] = i.types[j - 1];
7556 i.tm.operand_types[j] = i.tm.operand_types[j - 1];
8dc0818e 7557 i.flags[j] = i.flags[j - 1];
c0f3af97
L
7558 }
7559 i.op[0].regs
629310ab 7560 = (const reg_entry *) str_hash_find (reg_hash, "xmm0");
7ab9ffdd 7561 i.types[0] = regxmm;
c0f3af97
L
7562 i.tm.operand_types[0] = regxmm;
7563
7564 i.operands += 2;
7565 i.reg_operands += 2;
7566 i.tm.operands += 2;
7567
91d6fa6a 7568 dupl++;
c0f3af97 7569 dest++;
91d6fa6a
NC
7570 i.op[dupl] = i.op[dest];
7571 i.types[dupl] = i.types[dest];
7572 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 7573 i.flags[dupl] = i.flags[dest];
e2ec9d29 7574 }
c0f3af97
L
7575 else
7576 {
dc1e8a47 7577 duplicate:
c0f3af97
L
7578 i.operands++;
7579 i.reg_operands++;
7580 i.tm.operands++;
7581
91d6fa6a
NC
7582 i.op[dupl] = i.op[dest];
7583 i.types[dupl] = i.types[dest];
7584 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 7585 i.flags[dupl] = i.flags[dest];
c0f3af97
L
7586 }
7587
7588 if (i.tm.opcode_modifier.immext)
7589 process_immext ();
7590 }
75e5731b 7591 else if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 7592 && i.tm.operand_types[0].bitfield.xmmword)
c0f3af97
L
7593 {
7594 unsigned int j;
7595
9fcfb3d7
L
7596 for (j = 1; j < i.operands; j++)
7597 {
7598 i.op[j - 1] = i.op[j];
7599 i.types[j - 1] = i.types[j];
7600
7601 /* We need to adjust fields in i.tm since they are used by
7602 build_modrm_byte. */
7603 i.tm.operand_types [j - 1] = i.tm.operand_types [j];
8dc0818e
JB
7604
7605 i.flags[j - 1] = i.flags[j];
9fcfb3d7
L
7606 }
7607
e2ec9d29
L
7608 i.operands--;
7609 i.reg_operands--;
e2ec9d29
L
7610 i.tm.operands--;
7611 }
920d2ddc
IT
7612 else if (i.tm.opcode_modifier.implicitquadgroup)
7613 {
a477a8c4
JB
7614 unsigned int regnum, first_reg_in_group, last_reg_in_group;
7615
920d2ddc 7616 /* The second operand must be {x,y,z}mmN, where N is a multiple of 4. */
3528c362 7617 gas_assert (i.operands >= 2 && i.types[1].bitfield.class == RegSIMD);
a477a8c4
JB
7618 regnum = register_number (i.op[1].regs);
7619 first_reg_in_group = regnum & ~3;
7620 last_reg_in_group = first_reg_in_group + 3;
7621 if (regnum != first_reg_in_group)
7622 as_warn (_("source register `%s%s' implicitly denotes"
7623 " `%s%.3s%u' to `%s%.3s%u' source group in `%s'"),
7624 register_prefix, i.op[1].regs->reg_name,
7625 register_prefix, i.op[1].regs->reg_name, first_reg_in_group,
7626 register_prefix, i.op[1].regs->reg_name, last_reg_in_group,
7627 i.tm.name);
7628 }
e2ec9d29
L
7629 else if (i.tm.opcode_modifier.regkludge)
7630 {
7631 /* The imul $imm, %reg instruction is converted into
7632 imul $imm, %reg, %reg, and the clr %reg instruction
7633 is converted into xor %reg, %reg. */
7634
7635 unsigned int first_reg_op;
7636
7637 if (operand_type_check (i.types[0], reg))
7638 first_reg_op = 0;
7639 else
7640 first_reg_op = 1;
7641 /* Pretend we saw the extra register operand. */
9c2799c2 7642 gas_assert (i.reg_operands == 1
7ab9ffdd 7643 && i.op[first_reg_op + 1].regs == 0);
e2ec9d29
L
7644 i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
7645 i.types[first_reg_op + 1] = i.types[first_reg_op];
7646 i.operands++;
7647 i.reg_operands++;
29b0f896
AM
7648 }
7649
85b80b0f 7650 if (i.tm.opcode_modifier.modrm)
29b0f896
AM
7651 {
7652 /* The opcode is completed (modulo i.tm.extension_opcode which
52271982
AM
7653 must be put into the modrm byte). Now, we make the modrm and
7654 index base bytes based on all the info we've collected. */
29b0f896
AM
7655
7656 default_seg = build_modrm_byte ();
7657 }
00cee14f 7658 else if (i.types[0].bitfield.class == SReg)
85b80b0f
JB
7659 {
7660 if (flag_code != CODE_64BIT
7661 ? i.tm.base_opcode == POP_SEG_SHORT
7662 && i.op[0].regs->reg_num == 1
7663 : (i.tm.base_opcode | 1) == POP_SEG386_SHORT
7664 && i.op[0].regs->reg_num < 4)
7665 {
7666 as_bad (_("you can't `%s %s%s'"),
7667 i.tm.name, register_prefix, i.op[0].regs->reg_name);
7668 return 0;
7669 }
7670 if ( i.op[0].regs->reg_num > 3 && i.tm.opcode_length == 1 )
7671 {
7672 i.tm.base_opcode ^= POP_SEG_SHORT ^ POP_SEG386_SHORT;
7673 i.tm.opcode_length = 2;
7674 }
7675 i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
7676 }
8a2ed489 7677 else if ((i.tm.base_opcode & ~0x3) == MOV_AX_DISP32)
29b0f896
AM
7678 {
7679 default_seg = &ds;
7680 }
40fb9820 7681 else if (i.tm.opcode_modifier.isstring)
29b0f896
AM
7682 {
7683 /* For the string instructions that allow a segment override
7684 on one of their operands, the default segment is ds. */
7685 default_seg = &ds;
7686 }
50128d0c 7687 else if (i.short_form)
85b80b0f
JB
7688 {
7689 /* The register or float register operand is in operand
7690 0 or 1. */
bab6aec1 7691 unsigned int op = i.tm.operand_types[0].bitfield.class != Reg;
85b80b0f
JB
7692
7693 /* Register goes in low 3 bits of opcode. */
7694 i.tm.base_opcode |= i.op[op].regs->reg_num;
7695 if ((i.op[op].regs->reg_flags & RegRex) != 0)
7696 i.rex |= REX_B;
7697 if (!quiet_warnings && i.tm.opcode_modifier.ugh)
7698 {
7699 /* Warn about some common errors, but press on regardless.
7700 The first case can be generated by gcc (<= 2.8.1). */
7701 if (i.operands == 2)
7702 {
7703 /* Reversed arguments on faddp, fsubp, etc. */
7704 as_warn (_("translating to `%s %s%s,%s%s'"), i.tm.name,
7705 register_prefix, i.op[!intel_syntax].regs->reg_name,
7706 register_prefix, i.op[intel_syntax].regs->reg_name);
7707 }
7708 else
7709 {
7710 /* Extraneous `l' suffix on fp insn. */
7711 as_warn (_("translating to `%s %s%s'"), i.tm.name,
7712 register_prefix, i.op[0].regs->reg_name);
7713 }
7714 }
7715 }
29b0f896 7716
514a8bb0 7717 if ((i.seg[0] || i.prefix[SEG_PREFIX])
514a8bb0
JB
7718 && i.tm.base_opcode == 0x8d /* lea */
7719 && !is_any_vex_encoding(&i.tm))
92334ad2
JB
7720 {
7721 if (!quiet_warnings)
7722 as_warn (_("segment override on `%s' is ineffectual"), i.tm.name);
7723 if (optimize)
7724 {
7725 i.seg[0] = NULL;
7726 i.prefix[SEG_PREFIX] = 0;
7727 }
7728 }
52271982
AM
7729
7730 /* If a segment was explicitly specified, and the specified segment
b6773884
JB
7731 is neither the default nor the one already recorded from a prefix,
7732 use an opcode prefix to select it. If we never figured out what
7733 the default segment is, then default_seg will be zero at this
7734 point, and the specified segment prefix will always be used. */
7735 if (i.seg[0]
7736 && i.seg[0] != default_seg
7737 && i.seg[0]->seg_prefix != i.prefix[SEG_PREFIX])
29b0f896
AM
7738 {
7739 if (!add_prefix (i.seg[0]->seg_prefix))
7740 return 0;
7741 }
7742 return 1;
7743}
7744
a5aeccd9
JB
7745static INLINE void set_rex_vrex (const reg_entry *r, unsigned int rex_bit,
7746 bfd_boolean do_sse2avx)
7747{
7748 if (r->reg_flags & RegRex)
7749 {
7750 if (i.rex & rex_bit)
7751 as_bad (_("same type of prefix used twice"));
7752 i.rex |= rex_bit;
7753 }
7754 else if (do_sse2avx && (i.rex & rex_bit) && i.vex.register_specifier)
7755 {
7756 gas_assert (i.vex.register_specifier == r);
7757 i.vex.register_specifier += 8;
7758 }
7759
7760 if (r->reg_flags & RegVRex)
7761 i.vrex |= rex_bit;
7762}
7763
29b0f896 7764static const seg_entry *
e3bb37b5 7765build_modrm_byte (void)
29b0f896
AM
7766{
7767 const seg_entry *default_seg = 0;
c0f3af97 7768 unsigned int source, dest;
8cd7925b 7769 int vex_3_sources;
c0f3af97 7770
8cd7925b 7771 vex_3_sources = i.tm.opcode_modifier.vexsources == VEX3SOURCES;
c0f3af97
L
7772 if (vex_3_sources)
7773 {
91d6fa6a 7774 unsigned int nds, reg_slot;
4c2c6516 7775 expressionS *exp;
c0f3af97 7776
6b8d3588 7777 dest = i.operands - 1;
c0f3af97 7778 nds = dest - 1;
922d8de8 7779
a683cc34 7780 /* There are 2 kinds of instructions:
bed3d976 7781 1. 5 operands: 4 register operands or 3 register operands
9d3bf266 7782 plus 1 memory operand plus one Imm4 operand, VexXDS, and
bed3d976 7783 VexW0 or VexW1. The destination must be either XMM, YMM or
43234a1e 7784 ZMM register.
bed3d976 7785 2. 4 operands: 4 register operands or 3 register operands
2f1bada2 7786 plus 1 memory operand, with VexXDS. */
922d8de8 7787 gas_assert ((i.reg_operands == 4
bed3d976
JB
7788 || (i.reg_operands == 3 && i.mem_operands == 1))
7789 && i.tm.opcode_modifier.vexvvvv == VEXXDS
dcd7e323 7790 && i.tm.opcode_modifier.vexw
3528c362 7791 && i.tm.operand_types[dest].bitfield.class == RegSIMD);
a683cc34 7792
48db9223
JB
7793 /* If VexW1 is set, the first non-immediate operand is the source and
7794 the second non-immediate one is encoded in the immediate operand. */
7795 if (i.tm.opcode_modifier.vexw == VEXW1)
7796 {
7797 source = i.imm_operands;
7798 reg_slot = i.imm_operands + 1;
7799 }
7800 else
7801 {
7802 source = i.imm_operands + 1;
7803 reg_slot = i.imm_operands;
7804 }
7805
a683cc34 7806 if (i.imm_operands == 0)
bed3d976
JB
7807 {
7808 /* When there is no immediate operand, generate an 8bit
7809 immediate operand to encode the first operand. */
7810 exp = &im_expressions[i.imm_operands++];
7811 i.op[i.operands].imms = exp;
7812 i.types[i.operands] = imm8;
7813 i.operands++;
7814
3528c362 7815 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
bed3d976
JB
7816 exp->X_op = O_constant;
7817 exp->X_add_number = register_number (i.op[reg_slot].regs) << 4;
43234a1e
L
7818 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
7819 }
922d8de8 7820 else
bed3d976 7821 {
9d3bf266
JB
7822 gas_assert (i.imm_operands == 1);
7823 gas_assert (fits_in_imm4 (i.op[0].imms->X_add_number));
7824 gas_assert (!i.tm.opcode_modifier.immext);
a683cc34 7825
9d3bf266
JB
7826 /* Turn on Imm8 again so that output_imm will generate it. */
7827 i.types[0].bitfield.imm8 = 1;
bed3d976 7828
3528c362 7829 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
9d3bf266 7830 i.op[0].imms->X_add_number
bed3d976 7831 |= register_number (i.op[reg_slot].regs) << 4;
43234a1e 7832 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
bed3d976 7833 }
a683cc34 7834
3528c362 7835 gas_assert (i.tm.operand_types[nds].bitfield.class == RegSIMD);
dae39acc 7836 i.vex.register_specifier = i.op[nds].regs;
c0f3af97
L
7837 }
7838 else
7839 source = dest = 0;
29b0f896
AM
7840
7841 /* i.reg_operands MUST be the number of real register operands;
c0f3af97
L
7842 implicit registers do not count. If there are 3 register
7843 operands, it must be a instruction with VexNDS. For a
7844 instruction with VexNDD, the destination register is encoded
7845 in VEX prefix. If there are 4 register operands, it must be
7846 a instruction with VEX prefix and 3 sources. */
7ab9ffdd
L
7847 if (i.mem_operands == 0
7848 && ((i.reg_operands == 2
2426c15f 7849 && i.tm.opcode_modifier.vexvvvv <= VEXXDS)
7ab9ffdd 7850 || (i.reg_operands == 3
2426c15f 7851 && i.tm.opcode_modifier.vexvvvv == VEXXDS)
7ab9ffdd 7852 || (i.reg_operands == 4 && vex_3_sources)))
29b0f896 7853 {
cab737b9
L
7854 switch (i.operands)
7855 {
7856 case 2:
7857 source = 0;
7858 break;
7859 case 3:
c81128dc
L
7860 /* When there are 3 operands, one of them may be immediate,
7861 which may be the first or the last operand. Otherwise,
c0f3af97
L
7862 the first operand must be shift count register (cl) or it
7863 is an instruction with VexNDS. */
9c2799c2 7864 gas_assert (i.imm_operands == 1
7ab9ffdd 7865 || (i.imm_operands == 0
2426c15f 7866 && (i.tm.opcode_modifier.vexvvvv == VEXXDS
75e5731b
JB
7867 || (i.types[0].bitfield.instance == RegC
7868 && i.types[0].bitfield.byte))));
40fb9820 7869 if (operand_type_check (i.types[0], imm)
75e5731b
JB
7870 || (i.types[0].bitfield.instance == RegC
7871 && i.types[0].bitfield.byte))
40fb9820
L
7872 source = 1;
7873 else
7874 source = 0;
cab737b9
L
7875 break;
7876 case 4:
368d64cc
L
7877 /* When there are 4 operands, the first two must be 8bit
7878 immediate operands. The source operand will be the 3rd
c0f3af97
L
7879 one.
7880
7881 For instructions with VexNDS, if the first operand
7882 an imm8, the source operand is the 2nd one. If the last
7883 operand is imm8, the source operand is the first one. */
9c2799c2 7884 gas_assert ((i.imm_operands == 2
7ab9ffdd
L
7885 && i.types[0].bitfield.imm8
7886 && i.types[1].bitfield.imm8)
2426c15f 7887 || (i.tm.opcode_modifier.vexvvvv == VEXXDS
7ab9ffdd
L
7888 && i.imm_operands == 1
7889 && (i.types[0].bitfield.imm8
43234a1e
L
7890 || i.types[i.operands - 1].bitfield.imm8
7891 || i.rounding)));
9f2670f2
L
7892 if (i.imm_operands == 2)
7893 source = 2;
7894 else
c0f3af97
L
7895 {
7896 if (i.types[0].bitfield.imm8)
7897 source = 1;
7898 else
7899 source = 0;
7900 }
c0f3af97
L
7901 break;
7902 case 5:
e771e7c9 7903 if (is_evex_encoding (&i.tm))
43234a1e
L
7904 {
7905 /* For EVEX instructions, when there are 5 operands, the
7906 first one must be immediate operand. If the second one
7907 is immediate operand, the source operand is the 3th
7908 one. If the last one is immediate operand, the source
7909 operand is the 2nd one. */
7910 gas_assert (i.imm_operands == 2
7911 && i.tm.opcode_modifier.sae
7912 && operand_type_check (i.types[0], imm));
7913 if (operand_type_check (i.types[1], imm))
7914 source = 2;
7915 else if (operand_type_check (i.types[4], imm))
7916 source = 1;
7917 else
7918 abort ();
7919 }
cab737b9
L
7920 break;
7921 default:
7922 abort ();
7923 }
7924
c0f3af97
L
7925 if (!vex_3_sources)
7926 {
7927 dest = source + 1;
7928
43234a1e
L
7929 /* RC/SAE operand could be between DEST and SRC. That happens
7930 when one operand is GPR and the other one is XMM/YMM/ZMM
7931 register. */
7932 if (i.rounding && i.rounding->operand == (int) dest)
7933 dest++;
7934
2426c15f 7935 if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
c0f3af97 7936 {
43234a1e 7937 /* For instructions with VexNDS, the register-only source
c5d0745b 7938 operand must be a 32/64bit integer, XMM, YMM, ZMM, or mask
dfd69174 7939 register. It is encoded in VEX prefix. */
f12dc422
L
7940
7941 i386_operand_type op;
7942 unsigned int vvvv;
7943
c2ecccb3
L
7944 /* Swap two source operands if needed. */
7945 if (i.tm.opcode_modifier.swapsources)
f12dc422
L
7946 {
7947 vvvv = source;
7948 source = dest;
7949 }
7950 else
7951 vvvv = dest;
7952
7953 op = i.tm.operand_types[vvvv];
c0f3af97 7954 if ((dest + 1) >= i.operands
bab6aec1 7955 || ((op.bitfield.class != Reg
dc821c5f 7956 || (!op.bitfield.dword && !op.bitfield.qword))
3528c362 7957 && op.bitfield.class != RegSIMD
43234a1e 7958 && !operand_type_equal (&op, &regmask)))
c0f3af97 7959 abort ();
f12dc422 7960 i.vex.register_specifier = i.op[vvvv].regs;
c0f3af97
L
7961 dest++;
7962 }
7963 }
29b0f896
AM
7964
7965 i.rm.mode = 3;
dfd69174
JB
7966 /* One of the register operands will be encoded in the i.rm.reg
7967 field, the other in the combined i.rm.mode and i.rm.regmem
29b0f896
AM
7968 fields. If no form of this instruction supports a memory
7969 destination operand, then we assume the source operand may
7970 sometimes be a memory operand and so we need to store the
7971 destination in the i.rm.reg field. */
dfd69174 7972 if (!i.tm.opcode_modifier.regmem
40fb9820 7973 && operand_type_check (i.tm.operand_types[dest], anymem) == 0)
29b0f896
AM
7974 {
7975 i.rm.reg = i.op[dest].regs->reg_num;
7976 i.rm.regmem = i.op[source].regs->reg_num;
a5aeccd9
JB
7977 set_rex_vrex (i.op[dest].regs, REX_R, i.tm.opcode_modifier.sse2avx);
7978 set_rex_vrex (i.op[source].regs, REX_B, FALSE);
29b0f896
AM
7979 }
7980 else
7981 {
7982 i.rm.reg = i.op[source].regs->reg_num;
7983 i.rm.regmem = i.op[dest].regs->reg_num;
a5aeccd9
JB
7984 set_rex_vrex (i.op[dest].regs, REX_B, i.tm.opcode_modifier.sse2avx);
7985 set_rex_vrex (i.op[source].regs, REX_R, FALSE);
29b0f896 7986 }
e0c7f900 7987 if (flag_code != CODE_64BIT && (i.rex & REX_R))
c4a530c5 7988 {
4a5c67ed 7989 if (i.types[!i.tm.opcode_modifier.regmem].bitfield.class != RegCR)
c4a530c5 7990 abort ();
e0c7f900 7991 i.rex &= ~REX_R;
c4a530c5
JB
7992 add_prefix (LOCK_PREFIX_OPCODE);
7993 }
29b0f896
AM
7994 }
7995 else
7996 { /* If it's not 2 reg operands... */
c0f3af97
L
7997 unsigned int mem;
7998
29b0f896
AM
7999 if (i.mem_operands)
8000 {
8001 unsigned int fake_zero_displacement = 0;
99018f42 8002 unsigned int op;
4eed87de 8003
7ab9ffdd 8004 for (op = 0; op < i.operands; op++)
8dc0818e 8005 if (i.flags[op] & Operand_Mem)
7ab9ffdd 8006 break;
7ab9ffdd 8007 gas_assert (op < i.operands);
29b0f896 8008
63112cd6 8009 if (i.tm.opcode_modifier.sib)
6c30d220 8010 {
260cd341
LC
8011 /* The index register of VSIB shouldn't be RegIZ. */
8012 if (i.tm.opcode_modifier.sib != SIBMEM
8013 && i.index_reg->reg_num == RegIZ)
6c30d220
L
8014 abort ();
8015
8016 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8017 if (!i.base_reg)
8018 {
8019 i.sib.base = NO_BASE_REGISTER;
8020 i.sib.scale = i.log2_scale_factor;
8021 i.types[op].bitfield.disp8 = 0;
8022 i.types[op].bitfield.disp16 = 0;
8023 i.types[op].bitfield.disp64 = 0;
43083a50 8024 if (flag_code != CODE_64BIT || i.prefix[ADDR_PREFIX])
6c30d220
L
8025 {
8026 /* Must be 32 bit */
8027 i.types[op].bitfield.disp32 = 1;
8028 i.types[op].bitfield.disp32s = 0;
8029 }
8030 else
8031 {
8032 i.types[op].bitfield.disp32 = 0;
8033 i.types[op].bitfield.disp32s = 1;
8034 }
8035 }
260cd341
LC
8036
8037 /* Since the mandatory SIB always has index register, so
8038 the code logic remains unchanged. The non-mandatory SIB
8039 without index register is allowed and will be handled
8040 later. */
8041 if (i.index_reg)
8042 {
8043 if (i.index_reg->reg_num == RegIZ)
8044 i.sib.index = NO_INDEX_REGISTER;
8045 else
8046 i.sib.index = i.index_reg->reg_num;
8047 set_rex_vrex (i.index_reg, REX_X, FALSE);
8048 }
6c30d220
L
8049 }
8050
29b0f896
AM
8051 default_seg = &ds;
8052
8053 if (i.base_reg == 0)
8054 {
8055 i.rm.mode = 0;
8056 if (!i.disp_operands)
9bb129e8 8057 fake_zero_displacement = 1;
29b0f896
AM
8058 if (i.index_reg == 0)
8059 {
73053c1f
JB
8060 i386_operand_type newdisp;
8061
260cd341
LC
8062 /* Both check for VSIB and mandatory non-vector SIB. */
8063 gas_assert (!i.tm.opcode_modifier.sib
8064 || i.tm.opcode_modifier.sib == SIBMEM);
29b0f896 8065 /* Operand is just <disp> */
20f0a1fc 8066 if (flag_code == CODE_64BIT)
29b0f896
AM
8067 {
8068 /* 64bit mode overwrites the 32bit absolute
8069 addressing by RIP relative addressing and
8070 absolute addressing is encoded by one of the
8071 redundant SIB forms. */
8072 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8073 i.sib.base = NO_BASE_REGISTER;
8074 i.sib.index = NO_INDEX_REGISTER;
73053c1f 8075 newdisp = (!i.prefix[ADDR_PREFIX] ? disp32s : disp32);
20f0a1fc 8076 }
fc225355
L
8077 else if ((flag_code == CODE_16BIT)
8078 ^ (i.prefix[ADDR_PREFIX] != 0))
20f0a1fc
NC
8079 {
8080 i.rm.regmem = NO_BASE_REGISTER_16;
73053c1f 8081 newdisp = disp16;
20f0a1fc
NC
8082 }
8083 else
8084 {
8085 i.rm.regmem = NO_BASE_REGISTER;
73053c1f 8086 newdisp = disp32;
29b0f896 8087 }
73053c1f
JB
8088 i.types[op] = operand_type_and_not (i.types[op], anydisp);
8089 i.types[op] = operand_type_or (i.types[op], newdisp);
29b0f896 8090 }
63112cd6 8091 else if (!i.tm.opcode_modifier.sib)
29b0f896 8092 {
6c30d220 8093 /* !i.base_reg && i.index_reg */
e968fc9b 8094 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8095 i.sib.index = NO_INDEX_REGISTER;
8096 else
8097 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8098 i.sib.base = NO_BASE_REGISTER;
8099 i.sib.scale = i.log2_scale_factor;
8100 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
40fb9820
L
8101 i.types[op].bitfield.disp8 = 0;
8102 i.types[op].bitfield.disp16 = 0;
8103 i.types[op].bitfield.disp64 = 0;
43083a50 8104 if (flag_code != CODE_64BIT || i.prefix[ADDR_PREFIX])
40fb9820
L
8105 {
8106 /* Must be 32 bit */
8107 i.types[op].bitfield.disp32 = 1;
8108 i.types[op].bitfield.disp32s = 0;
8109 }
29b0f896 8110 else
40fb9820
L
8111 {
8112 i.types[op].bitfield.disp32 = 0;
8113 i.types[op].bitfield.disp32s = 1;
8114 }
29b0f896 8115 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8116 i.rex |= REX_X;
29b0f896
AM
8117 }
8118 }
8119 /* RIP addressing for 64bit mode. */
e968fc9b 8120 else if (i.base_reg->reg_num == RegIP)
29b0f896 8121 {
63112cd6 8122 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896 8123 i.rm.regmem = NO_BASE_REGISTER;
40fb9820
L
8124 i.types[op].bitfield.disp8 = 0;
8125 i.types[op].bitfield.disp16 = 0;
8126 i.types[op].bitfield.disp32 = 0;
8127 i.types[op].bitfield.disp32s = 1;
8128 i.types[op].bitfield.disp64 = 0;
71903a11 8129 i.flags[op] |= Operand_PCrel;
20f0a1fc
NC
8130 if (! i.disp_operands)
8131 fake_zero_displacement = 1;
29b0f896 8132 }
dc821c5f 8133 else if (i.base_reg->reg_type.bitfield.word)
29b0f896 8134 {
63112cd6 8135 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896
AM
8136 switch (i.base_reg->reg_num)
8137 {
8138 case 3: /* (%bx) */
8139 if (i.index_reg == 0)
8140 i.rm.regmem = 7;
8141 else /* (%bx,%si) -> 0, or (%bx,%di) -> 1 */
8142 i.rm.regmem = i.index_reg->reg_num - 6;
8143 break;
8144 case 5: /* (%bp) */
8145 default_seg = &ss;
8146 if (i.index_reg == 0)
8147 {
8148 i.rm.regmem = 6;
40fb9820 8149 if (operand_type_check (i.types[op], disp) == 0)
29b0f896
AM
8150 {
8151 /* fake (%bp) into 0(%bp) */
41eb8e88 8152 if (i.disp_encoding == disp_encoding_16bit)
1a02d6b0
L
8153 i.types[op].bitfield.disp16 = 1;
8154 else
8155 i.types[op].bitfield.disp8 = 1;
252b5132 8156 fake_zero_displacement = 1;
29b0f896
AM
8157 }
8158 }
8159 else /* (%bp,%si) -> 2, or (%bp,%di) -> 3 */
8160 i.rm.regmem = i.index_reg->reg_num - 6 + 2;
8161 break;
8162 default: /* (%si) -> 4 or (%di) -> 5 */
8163 i.rm.regmem = i.base_reg->reg_num - 6 + 4;
8164 }
41eb8e88
L
8165 if (!fake_zero_displacement
8166 && !i.disp_operands
8167 && i.disp_encoding)
8168 {
8169 fake_zero_displacement = 1;
8170 if (i.disp_encoding == disp_encoding_8bit)
8171 i.types[op].bitfield.disp8 = 1;
8172 else
8173 i.types[op].bitfield.disp16 = 1;
8174 }
29b0f896
AM
8175 i.rm.mode = mode_from_disp_size (i.types[op]);
8176 }
8177 else /* i.base_reg and 32/64 bit mode */
8178 {
8179 if (flag_code == CODE_64BIT
40fb9820
L
8180 && operand_type_check (i.types[op], disp))
8181 {
73053c1f
JB
8182 i.types[op].bitfield.disp16 = 0;
8183 i.types[op].bitfield.disp64 = 0;
40fb9820 8184 if (i.prefix[ADDR_PREFIX] == 0)
73053c1f
JB
8185 {
8186 i.types[op].bitfield.disp32 = 0;
8187 i.types[op].bitfield.disp32s = 1;
8188 }
40fb9820 8189 else
73053c1f
JB
8190 {
8191 i.types[op].bitfield.disp32 = 1;
8192 i.types[op].bitfield.disp32s = 0;
8193 }
40fb9820 8194 }
20f0a1fc 8195
63112cd6 8196 if (!i.tm.opcode_modifier.sib)
6c30d220 8197 i.rm.regmem = i.base_reg->reg_num;
29b0f896 8198 if ((i.base_reg->reg_flags & RegRex) != 0)
161a04f6 8199 i.rex |= REX_B;
29b0f896
AM
8200 i.sib.base = i.base_reg->reg_num;
8201 /* x86-64 ignores REX prefix bit here to avoid decoder
8202 complications. */
848930b2
JB
8203 if (!(i.base_reg->reg_flags & RegRex)
8204 && (i.base_reg->reg_num == EBP_REG_NUM
8205 || i.base_reg->reg_num == ESP_REG_NUM))
29b0f896 8206 default_seg = &ss;
848930b2 8207 if (i.base_reg->reg_num == 5 && i.disp_operands == 0)
29b0f896 8208 {
848930b2 8209 fake_zero_displacement = 1;
1a02d6b0
L
8210 if (i.disp_encoding == disp_encoding_32bit)
8211 i.types[op].bitfield.disp32 = 1;
8212 else
8213 i.types[op].bitfield.disp8 = 1;
29b0f896
AM
8214 }
8215 i.sib.scale = i.log2_scale_factor;
8216 if (i.index_reg == 0)
8217 {
260cd341
LC
8218 /* Only check for VSIB. */
8219 gas_assert (i.tm.opcode_modifier.sib != VECSIB128
8220 && i.tm.opcode_modifier.sib != VECSIB256
8221 && i.tm.opcode_modifier.sib != VECSIB512);
8222
29b0f896
AM
8223 /* <disp>(%esp) becomes two byte modrm with no index
8224 register. We've already stored the code for esp
8225 in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
8226 Any base register besides %esp will not use the
8227 extra modrm byte. */
8228 i.sib.index = NO_INDEX_REGISTER;
29b0f896 8229 }
63112cd6 8230 else if (!i.tm.opcode_modifier.sib)
29b0f896 8231 {
e968fc9b 8232 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8233 i.sib.index = NO_INDEX_REGISTER;
8234 else
8235 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8236 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8237 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8238 i.rex |= REX_X;
29b0f896 8239 }
67a4f2b7
AO
8240
8241 if (i.disp_operands
8242 && (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
8243 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL))
8244 i.rm.mode = 0;
8245 else
a501d77e
L
8246 {
8247 if (!fake_zero_displacement
8248 && !i.disp_operands
8249 && i.disp_encoding)
8250 {
8251 fake_zero_displacement = 1;
8252 if (i.disp_encoding == disp_encoding_8bit)
8253 i.types[op].bitfield.disp8 = 1;
8254 else
8255 i.types[op].bitfield.disp32 = 1;
8256 }
8257 i.rm.mode = mode_from_disp_size (i.types[op]);
8258 }
29b0f896 8259 }
252b5132 8260
29b0f896
AM
8261 if (fake_zero_displacement)
8262 {
8263 /* Fakes a zero displacement assuming that i.types[op]
8264 holds the correct displacement size. */
8265 expressionS *exp;
8266
9c2799c2 8267 gas_assert (i.op[op].disps == 0);
29b0f896
AM
8268 exp = &disp_expressions[i.disp_operands++];
8269 i.op[op].disps = exp;
8270 exp->X_op = O_constant;
8271 exp->X_add_number = 0;
8272 exp->X_add_symbol = (symbolS *) 0;
8273 exp->X_op_symbol = (symbolS *) 0;
8274 }
c0f3af97
L
8275
8276 mem = op;
29b0f896 8277 }
c0f3af97
L
8278 else
8279 mem = ~0;
252b5132 8280
8c43a48b 8281 if (i.tm.opcode_modifier.vexsources == XOP2SOURCES)
5dd85c99
SP
8282 {
8283 if (operand_type_check (i.types[0], imm))
8284 i.vex.register_specifier = NULL;
8285 else
8286 {
8287 /* VEX.vvvv encodes one of the sources when the first
8288 operand is not an immediate. */
1ef99a7b 8289 if (i.tm.opcode_modifier.vexw == VEXW0)
5dd85c99
SP
8290 i.vex.register_specifier = i.op[0].regs;
8291 else
8292 i.vex.register_specifier = i.op[1].regs;
8293 }
8294
8295 /* Destination is a XMM register encoded in the ModRM.reg
8296 and VEX.R bit. */
8297 i.rm.reg = i.op[2].regs->reg_num;
8298 if ((i.op[2].regs->reg_flags & RegRex) != 0)
8299 i.rex |= REX_R;
8300
8301 /* ModRM.rm and VEX.B encodes the other source. */
8302 if (!i.mem_operands)
8303 {
8304 i.rm.mode = 3;
8305
1ef99a7b 8306 if (i.tm.opcode_modifier.vexw == VEXW0)
5dd85c99
SP
8307 i.rm.regmem = i.op[1].regs->reg_num;
8308 else
8309 i.rm.regmem = i.op[0].regs->reg_num;
8310
8311 if ((i.op[1].regs->reg_flags & RegRex) != 0)
8312 i.rex |= REX_B;
8313 }
8314 }
2426c15f 8315 else if (i.tm.opcode_modifier.vexvvvv == VEXLWP)
f88c9eb0
SP
8316 {
8317 i.vex.register_specifier = i.op[2].regs;
8318 if (!i.mem_operands)
8319 {
8320 i.rm.mode = 3;
8321 i.rm.regmem = i.op[1].regs->reg_num;
8322 if ((i.op[1].regs->reg_flags & RegRex) != 0)
8323 i.rex |= REX_B;
8324 }
8325 }
29b0f896
AM
8326 /* Fill in i.rm.reg or i.rm.regmem field with register operand
8327 (if any) based on i.tm.extension_opcode. Again, we must be
8328 careful to make sure that segment/control/debug/test/MMX
8329 registers are coded into the i.rm.reg field. */
f88c9eb0 8330 else if (i.reg_operands)
29b0f896 8331 {
99018f42 8332 unsigned int op;
7ab9ffdd
L
8333 unsigned int vex_reg = ~0;
8334
8335 for (op = 0; op < i.operands; op++)
921eafea
L
8336 if (i.types[op].bitfield.class == Reg
8337 || i.types[op].bitfield.class == RegBND
8338 || i.types[op].bitfield.class == RegMask
8339 || i.types[op].bitfield.class == SReg
8340 || i.types[op].bitfield.class == RegCR
8341 || i.types[op].bitfield.class == RegDR
8342 || i.types[op].bitfield.class == RegTR
8343 || i.types[op].bitfield.class == RegSIMD
8344 || i.types[op].bitfield.class == RegMMX)
8345 break;
c0209578 8346
7ab9ffdd
L
8347 if (vex_3_sources)
8348 op = dest;
2426c15f 8349 else if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
7ab9ffdd
L
8350 {
8351 /* For instructions with VexNDS, the register-only
8352 source operand is encoded in VEX prefix. */
8353 gas_assert (mem != (unsigned int) ~0);
c0f3af97 8354
7ab9ffdd 8355 if (op > mem)
c0f3af97 8356 {
7ab9ffdd
L
8357 vex_reg = op++;
8358 gas_assert (op < i.operands);
c0f3af97
L
8359 }
8360 else
c0f3af97 8361 {
f12dc422
L
8362 /* Check register-only source operand when two source
8363 operands are swapped. */
8364 if (!i.tm.operand_types[op].bitfield.baseindex
8365 && i.tm.operand_types[op + 1].bitfield.baseindex)
8366 {
8367 vex_reg = op;
8368 op += 2;
8369 gas_assert (mem == (vex_reg + 1)
8370 && op < i.operands);
8371 }
8372 else
8373 {
8374 vex_reg = op + 1;
8375 gas_assert (vex_reg < i.operands);
8376 }
c0f3af97 8377 }
7ab9ffdd 8378 }
2426c15f 8379 else if (i.tm.opcode_modifier.vexvvvv == VEXNDD)
7ab9ffdd 8380 {
f12dc422 8381 /* For instructions with VexNDD, the register destination
7ab9ffdd 8382 is encoded in VEX prefix. */
f12dc422
L
8383 if (i.mem_operands == 0)
8384 {
8385 /* There is no memory operand. */
8386 gas_assert ((op + 2) == i.operands);
8387 vex_reg = op + 1;
8388 }
8389 else
8d63c93e 8390 {
ed438a93
JB
8391 /* There are only 2 non-immediate operands. */
8392 gas_assert (op < i.imm_operands + 2
8393 && i.operands == i.imm_operands + 2);
8394 vex_reg = i.imm_operands + 1;
f12dc422 8395 }
7ab9ffdd
L
8396 }
8397 else
8398 gas_assert (op < i.operands);
99018f42 8399
7ab9ffdd
L
8400 if (vex_reg != (unsigned int) ~0)
8401 {
f12dc422 8402 i386_operand_type *type = &i.tm.operand_types[vex_reg];
7ab9ffdd 8403
bab6aec1 8404 if ((type->bitfield.class != Reg
dc821c5f 8405 || (!type->bitfield.dword && !type->bitfield.qword))
3528c362 8406 && type->bitfield.class != RegSIMD
43234a1e 8407 && !operand_type_equal (type, &regmask))
7ab9ffdd 8408 abort ();
f88c9eb0 8409
7ab9ffdd
L
8410 i.vex.register_specifier = i.op[vex_reg].regs;
8411 }
8412
1b9f0c97
L
8413 /* Don't set OP operand twice. */
8414 if (vex_reg != op)
7ab9ffdd 8415 {
1b9f0c97
L
8416 /* If there is an extension opcode to put here, the
8417 register number must be put into the regmem field. */
8418 if (i.tm.extension_opcode != None)
8419 {
8420 i.rm.regmem = i.op[op].regs->reg_num;
a5aeccd9
JB
8421 set_rex_vrex (i.op[op].regs, REX_B,
8422 i.tm.opcode_modifier.sse2avx);
1b9f0c97
L
8423 }
8424 else
8425 {
8426 i.rm.reg = i.op[op].regs->reg_num;
a5aeccd9
JB
8427 set_rex_vrex (i.op[op].regs, REX_R,
8428 i.tm.opcode_modifier.sse2avx);
1b9f0c97 8429 }
7ab9ffdd 8430 }
252b5132 8431
29b0f896
AM
8432 /* Now, if no memory operand has set i.rm.mode = 0, 1, 2 we
8433 must set it to 3 to indicate this is a register operand
8434 in the regmem field. */
8435 if (!i.mem_operands)
8436 i.rm.mode = 3;
8437 }
252b5132 8438
29b0f896 8439 /* Fill in i.rm.reg field with extension opcode (if any). */
c1e679ec 8440 if (i.tm.extension_opcode != None)
29b0f896
AM
8441 i.rm.reg = i.tm.extension_opcode;
8442 }
8443 return default_seg;
8444}
252b5132 8445
48ef937e
JB
8446static INLINE void
8447frag_opcode_byte (unsigned char byte)
8448{
8449 if (now_seg != absolute_section)
8450 FRAG_APPEND_1_CHAR (byte);
8451 else
8452 ++abs_section_offset;
8453}
8454
376cd056
JB
8455static unsigned int
8456flip_code16 (unsigned int code16)
8457{
8458 gas_assert (i.tm.operands == 1);
8459
8460 return !(i.prefix[REX_PREFIX] & REX_W)
8461 && (code16 ? i.tm.operand_types[0].bitfield.disp32
8462 || i.tm.operand_types[0].bitfield.disp32s
8463 : i.tm.operand_types[0].bitfield.disp16)
8464 ? CODE16 : 0;
8465}
8466
29b0f896 8467static void
e3bb37b5 8468output_branch (void)
29b0f896
AM
8469{
8470 char *p;
f8a5c266 8471 int size;
29b0f896
AM
8472 int code16;
8473 int prefix;
8474 relax_substateT subtype;
8475 symbolS *sym;
8476 offsetT off;
8477
48ef937e
JB
8478 if (now_seg == absolute_section)
8479 {
8480 as_bad (_("relaxable branches not supported in absolute section"));
8481 return;
8482 }
8483
f8a5c266 8484 code16 = flag_code == CODE_16BIT ? CODE16 : 0;
a501d77e 8485 size = i.disp_encoding == disp_encoding_32bit ? BIG : SMALL;
29b0f896
AM
8486
8487 prefix = 0;
8488 if (i.prefix[DATA_PREFIX] != 0)
252b5132 8489 {
29b0f896
AM
8490 prefix = 1;
8491 i.prefixes -= 1;
376cd056 8492 code16 ^= flip_code16(code16);
252b5132 8493 }
29b0f896
AM
8494 /* Pentium4 branch hints. */
8495 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8496 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
2f66722d 8497 {
29b0f896
AM
8498 prefix++;
8499 i.prefixes--;
8500 }
8501 if (i.prefix[REX_PREFIX] != 0)
8502 {
8503 prefix++;
8504 i.prefixes--;
2f66722d
AM
8505 }
8506
7e8b059b
L
8507 /* BND prefixed jump. */
8508 if (i.prefix[BND_PREFIX] != 0)
8509 {
6cb0a70e
JB
8510 prefix++;
8511 i.prefixes--;
7e8b059b
L
8512 }
8513
f2810fe0
JB
8514 if (i.prefixes != 0)
8515 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
29b0f896
AM
8516
8517 /* It's always a symbol; End frag & setup for relax.
8518 Make sure there is enough room in this frag for the largest
8519 instruction we may generate in md_convert_frag. This is 2
8520 bytes for the opcode and room for the prefix and largest
8521 displacement. */
8522 frag_grow (prefix + 2 + 4);
8523 /* Prefix and 1 opcode byte go in fr_fix. */
8524 p = frag_more (prefix + 1);
8525 if (i.prefix[DATA_PREFIX] != 0)
8526 *p++ = DATA_PREFIX_OPCODE;
8527 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
8528 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
8529 *p++ = i.prefix[SEG_PREFIX];
6cb0a70e
JB
8530 if (i.prefix[BND_PREFIX] != 0)
8531 *p++ = BND_PREFIX_OPCODE;
29b0f896
AM
8532 if (i.prefix[REX_PREFIX] != 0)
8533 *p++ = i.prefix[REX_PREFIX];
8534 *p = i.tm.base_opcode;
8535
8536 if ((unsigned char) *p == JUMP_PC_RELATIVE)
f8a5c266 8537 subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, size);
40fb9820 8538 else if (cpu_arch_flags.bitfield.cpui386)
f8a5c266 8539 subtype = ENCODE_RELAX_STATE (COND_JUMP, size);
29b0f896 8540 else
f8a5c266 8541 subtype = ENCODE_RELAX_STATE (COND_JUMP86, size);
29b0f896 8542 subtype |= code16;
3e73aa7c 8543
29b0f896
AM
8544 sym = i.op[0].disps->X_add_symbol;
8545 off = i.op[0].disps->X_add_number;
3e73aa7c 8546
29b0f896
AM
8547 if (i.op[0].disps->X_op != O_constant
8548 && i.op[0].disps->X_op != O_symbol)
3e73aa7c 8549 {
29b0f896
AM
8550 /* Handle complex expressions. */
8551 sym = make_expr_symbol (i.op[0].disps);
8552 off = 0;
8553 }
3e73aa7c 8554
29b0f896
AM
8555 /* 1 possible extra opcode + 4 byte displacement go in var part.
8556 Pass reloc in fr_var. */
d258b828 8557 frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
29b0f896 8558}
3e73aa7c 8559
bd7ab16b
L
8560#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8561/* Return TRUE iff PLT32 relocation should be used for branching to
8562 symbol S. */
8563
8564static bfd_boolean
8565need_plt32_p (symbolS *s)
8566{
8567 /* PLT32 relocation is ELF only. */
8568 if (!IS_ELF)
8569 return FALSE;
8570
a5def729
RO
8571#ifdef TE_SOLARIS
8572 /* Don't emit PLT32 relocation on Solaris: neither native linker nor
8573 krtld support it. */
8574 return FALSE;
8575#endif
8576
bd7ab16b
L
8577 /* Since there is no need to prepare for PLT branch on x86-64, we
8578 can generate R_X86_64_PLT32, instead of R_X86_64_PC32, which can
8579 be used as a marker for 32-bit PC-relative branches. */
8580 if (!object_64bit)
8581 return FALSE;
8582
8583 /* Weak or undefined symbol need PLT32 relocation. */
8584 if (S_IS_WEAK (s) || !S_IS_DEFINED (s))
8585 return TRUE;
8586
8587 /* Non-global symbol doesn't need PLT32 relocation. */
8588 if (! S_IS_EXTERNAL (s))
8589 return FALSE;
8590
8591 /* Other global symbols need PLT32 relocation. NB: Symbol with
8592 non-default visibilities are treated as normal global symbol
8593 so that PLT32 relocation can be used as a marker for 32-bit
8594 PC-relative branches. It is useful for linker relaxation. */
8595 return TRUE;
8596}
8597#endif
8598
29b0f896 8599static void
e3bb37b5 8600output_jump (void)
29b0f896
AM
8601{
8602 char *p;
8603 int size;
3e02c1cc 8604 fixS *fixP;
bd7ab16b 8605 bfd_reloc_code_real_type jump_reloc = i.reloc[0];
29b0f896 8606
0cfa3eb3 8607 if (i.tm.opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
8608 {
8609 /* This is a loop or jecxz type instruction. */
8610 size = 1;
8611 if (i.prefix[ADDR_PREFIX] != 0)
8612 {
48ef937e 8613 frag_opcode_byte (ADDR_PREFIX_OPCODE);
29b0f896
AM
8614 i.prefixes -= 1;
8615 }
8616 /* Pentium4 branch hints. */
8617 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8618 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
8619 {
48ef937e 8620 frag_opcode_byte (i.prefix[SEG_PREFIX]);
29b0f896 8621 i.prefixes--;
3e73aa7c
JH
8622 }
8623 }
29b0f896
AM
8624 else
8625 {
8626 int code16;
3e73aa7c 8627
29b0f896
AM
8628 code16 = 0;
8629 if (flag_code == CODE_16BIT)
8630 code16 = CODE16;
3e73aa7c 8631
29b0f896
AM
8632 if (i.prefix[DATA_PREFIX] != 0)
8633 {
48ef937e 8634 frag_opcode_byte (DATA_PREFIX_OPCODE);
29b0f896 8635 i.prefixes -= 1;
376cd056 8636 code16 ^= flip_code16(code16);
29b0f896 8637 }
252b5132 8638
29b0f896
AM
8639 size = 4;
8640 if (code16)
8641 size = 2;
8642 }
9fcc94b6 8643
6cb0a70e
JB
8644 /* BND prefixed jump. */
8645 if (i.prefix[BND_PREFIX] != 0)
29b0f896 8646 {
48ef937e 8647 frag_opcode_byte (i.prefix[BND_PREFIX]);
29b0f896
AM
8648 i.prefixes -= 1;
8649 }
252b5132 8650
6cb0a70e 8651 if (i.prefix[REX_PREFIX] != 0)
7e8b059b 8652 {
48ef937e 8653 frag_opcode_byte (i.prefix[REX_PREFIX]);
7e8b059b
L
8654 i.prefixes -= 1;
8655 }
8656
f2810fe0
JB
8657 if (i.prefixes != 0)
8658 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
e0890092 8659
48ef937e
JB
8660 if (now_seg == absolute_section)
8661 {
8662 abs_section_offset += i.tm.opcode_length + size;
8663 return;
8664 }
8665
42164a71
L
8666 p = frag_more (i.tm.opcode_length + size);
8667 switch (i.tm.opcode_length)
8668 {
8669 case 2:
8670 *p++ = i.tm.base_opcode >> 8;
1a0670f3 8671 /* Fall through. */
42164a71
L
8672 case 1:
8673 *p++ = i.tm.base_opcode;
8674 break;
8675 default:
8676 abort ();
8677 }
e0890092 8678
bd7ab16b
L
8679#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8680 if (size == 4
8681 && jump_reloc == NO_RELOC
8682 && need_plt32_p (i.op[0].disps->X_add_symbol))
8683 jump_reloc = BFD_RELOC_X86_64_PLT32;
8684#endif
8685
8686 jump_reloc = reloc (size, 1, 1, jump_reloc);
8687
3e02c1cc 8688 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
bd7ab16b 8689 i.op[0].disps, 1, jump_reloc);
3e02c1cc
AM
8690
8691 /* All jumps handled here are signed, but don't use a signed limit
8692 check for 32 and 16 bit jumps as we want to allow wrap around at
8693 4G and 64k respectively. */
8694 if (size == 1)
8695 fixP->fx_signed = 1;
29b0f896 8696}
e0890092 8697
29b0f896 8698static void
e3bb37b5 8699output_interseg_jump (void)
29b0f896
AM
8700{
8701 char *p;
8702 int size;
8703 int prefix;
8704 int code16;
252b5132 8705
29b0f896
AM
8706 code16 = 0;
8707 if (flag_code == CODE_16BIT)
8708 code16 = CODE16;
a217f122 8709
29b0f896
AM
8710 prefix = 0;
8711 if (i.prefix[DATA_PREFIX] != 0)
8712 {
8713 prefix = 1;
8714 i.prefixes -= 1;
8715 code16 ^= CODE16;
8716 }
6cb0a70e
JB
8717
8718 gas_assert (!i.prefix[REX_PREFIX]);
252b5132 8719
29b0f896
AM
8720 size = 4;
8721 if (code16)
8722 size = 2;
252b5132 8723
f2810fe0
JB
8724 if (i.prefixes != 0)
8725 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
252b5132 8726
48ef937e
JB
8727 if (now_seg == absolute_section)
8728 {
8729 abs_section_offset += prefix + 1 + 2 + size;
8730 return;
8731 }
8732
29b0f896
AM
8733 /* 1 opcode; 2 segment; offset */
8734 p = frag_more (prefix + 1 + 2 + size);
3e73aa7c 8735
29b0f896
AM
8736 if (i.prefix[DATA_PREFIX] != 0)
8737 *p++ = DATA_PREFIX_OPCODE;
252b5132 8738
29b0f896
AM
8739 if (i.prefix[REX_PREFIX] != 0)
8740 *p++ = i.prefix[REX_PREFIX];
252b5132 8741
29b0f896
AM
8742 *p++ = i.tm.base_opcode;
8743 if (i.op[1].imms->X_op == O_constant)
8744 {
8745 offsetT n = i.op[1].imms->X_add_number;
252b5132 8746
29b0f896
AM
8747 if (size == 2
8748 && !fits_in_unsigned_word (n)
8749 && !fits_in_signed_word (n))
8750 {
8751 as_bad (_("16-bit jump out of range"));
8752 return;
8753 }
8754 md_number_to_chars (p, n, size);
8755 }
8756 else
8757 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
d258b828 8758 i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
29b0f896
AM
8759 if (i.op[0].imms->X_op != O_constant)
8760 as_bad (_("can't handle non absolute segment in `%s'"),
8761 i.tm.name);
8762 md_number_to_chars (p + size, (valueT) i.op[0].imms->X_add_number, 2);
8763}
a217f122 8764
b4a3a7b4
L
8765#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8766void
8767x86_cleanup (void)
8768{
8769 char *p;
8770 asection *seg = now_seg;
8771 subsegT subseg = now_subseg;
8772 asection *sec;
8773 unsigned int alignment, align_size_1;
8774 unsigned int isa_1_descsz, feature_2_descsz, descsz;
8775 unsigned int isa_1_descsz_raw, feature_2_descsz_raw;
8776 unsigned int padding;
8777
8778 if (!IS_ELF || !x86_used_note)
8779 return;
8780
b4a3a7b4
L
8781 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X86;
8782
8783 /* The .note.gnu.property section layout:
8784
8785 Field Length Contents
8786 ---- ---- ----
8787 n_namsz 4 4
8788 n_descsz 4 The note descriptor size
8789 n_type 4 NT_GNU_PROPERTY_TYPE_0
8790 n_name 4 "GNU"
8791 n_desc n_descsz The program property array
8792 .... .... ....
8793 */
8794
8795 /* Create the .note.gnu.property section. */
8796 sec = subseg_new (NOTE_GNU_PROPERTY_SECTION_NAME, 0);
fd361982 8797 bfd_set_section_flags (sec,
b4a3a7b4
L
8798 (SEC_ALLOC
8799 | SEC_LOAD
8800 | SEC_DATA
8801 | SEC_HAS_CONTENTS
8802 | SEC_READONLY));
8803
8804 if (get_elf_backend_data (stdoutput)->s->elfclass == ELFCLASS64)
8805 {
8806 align_size_1 = 7;
8807 alignment = 3;
8808 }
8809 else
8810 {
8811 align_size_1 = 3;
8812 alignment = 2;
8813 }
8814
fd361982 8815 bfd_set_section_alignment (sec, alignment);
b4a3a7b4
L
8816 elf_section_type (sec) = SHT_NOTE;
8817
8818 /* GNU_PROPERTY_X86_ISA_1_USED: 4-byte type + 4-byte data size
8819 + 4-byte data */
8820 isa_1_descsz_raw = 4 + 4 + 4;
8821 /* Align GNU_PROPERTY_X86_ISA_1_USED. */
8822 isa_1_descsz = (isa_1_descsz_raw + align_size_1) & ~align_size_1;
8823
8824 feature_2_descsz_raw = isa_1_descsz;
8825 /* GNU_PROPERTY_X86_FEATURE_2_USED: 4-byte type + 4-byte data size
8826 + 4-byte data */
8827 feature_2_descsz_raw += 4 + 4 + 4;
8828 /* Align GNU_PROPERTY_X86_FEATURE_2_USED. */
8829 feature_2_descsz = ((feature_2_descsz_raw + align_size_1)
8830 & ~align_size_1);
8831
8832 descsz = feature_2_descsz;
8833 /* Section size: n_namsz + n_descsz + n_type + n_name + n_descsz. */
8834 p = frag_more (4 + 4 + 4 + 4 + descsz);
8835
8836 /* Write n_namsz. */
8837 md_number_to_chars (p, (valueT) 4, 4);
8838
8839 /* Write n_descsz. */
8840 md_number_to_chars (p + 4, (valueT) descsz, 4);
8841
8842 /* Write n_type. */
8843 md_number_to_chars (p + 4 * 2, (valueT) NT_GNU_PROPERTY_TYPE_0, 4);
8844
8845 /* Write n_name. */
8846 memcpy (p + 4 * 3, "GNU", 4);
8847
8848 /* Write 4-byte type. */
8849 md_number_to_chars (p + 4 * 4,
8850 (valueT) GNU_PROPERTY_X86_ISA_1_USED, 4);
8851
8852 /* Write 4-byte data size. */
8853 md_number_to_chars (p + 4 * 5, (valueT) 4, 4);
8854
8855 /* Write 4-byte data. */
8856 md_number_to_chars (p + 4 * 6, (valueT) x86_isa_1_used, 4);
8857
8858 /* Zero out paddings. */
8859 padding = isa_1_descsz - isa_1_descsz_raw;
8860 if (padding)
8861 memset (p + 4 * 7, 0, padding);
8862
8863 /* Write 4-byte type. */
8864 md_number_to_chars (p + isa_1_descsz + 4 * 4,
8865 (valueT) GNU_PROPERTY_X86_FEATURE_2_USED, 4);
8866
8867 /* Write 4-byte data size. */
8868 md_number_to_chars (p + isa_1_descsz + 4 * 5, (valueT) 4, 4);
8869
8870 /* Write 4-byte data. */
8871 md_number_to_chars (p + isa_1_descsz + 4 * 6,
8872 (valueT) x86_feature_2_used, 4);
8873
8874 /* Zero out paddings. */
8875 padding = feature_2_descsz - feature_2_descsz_raw;
8876 if (padding)
8877 memset (p + isa_1_descsz + 4 * 7, 0, padding);
8878
8879 /* We probably can't restore the current segment, for there likely
8880 isn't one yet... */
8881 if (seg && subseg)
8882 subseg_set (seg, subseg);
8883}
8884#endif
8885
9c33702b
JB
8886static unsigned int
8887encoding_length (const fragS *start_frag, offsetT start_off,
8888 const char *frag_now_ptr)
8889{
8890 unsigned int len = 0;
8891
8892 if (start_frag != frag_now)
8893 {
8894 const fragS *fr = start_frag;
8895
8896 do {
8897 len += fr->fr_fix;
8898 fr = fr->fr_next;
8899 } while (fr && fr != frag_now);
8900 }
8901
8902 return len - start_off + (frag_now_ptr - frag_now->fr_literal);
8903}
8904
e379e5f3 8905/* Return 1 for test, and, cmp, add, sub, inc and dec which may
79d72f45
HL
8906 be macro-fused with conditional jumps.
8907 NB: If TEST/AND/CMP/ADD/SUB/INC/DEC is of RIP relative address,
8908 or is one of the following format:
8909
8910 cmp m, imm
8911 add m, imm
8912 sub m, imm
8913 test m, imm
8914 and m, imm
8915 inc m
8916 dec m
8917
8918 it is unfusible. */
e379e5f3
L
8919
8920static int
79d72f45 8921maybe_fused_with_jcc_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
8922{
8923 /* No RIP address. */
8924 if (i.base_reg && i.base_reg->reg_num == RegIP)
8925 return 0;
8926
8927 /* No VEX/EVEX encoding. */
8928 if (is_any_vex_encoding (&i.tm))
8929 return 0;
8930
79d72f45
HL
8931 /* add, sub without add/sub m, imm. */
8932 if (i.tm.base_opcode <= 5
e379e5f3
L
8933 || (i.tm.base_opcode >= 0x28 && i.tm.base_opcode <= 0x2d)
8934 || ((i.tm.base_opcode | 3) == 0x83
79d72f45 8935 && (i.tm.extension_opcode == 0x5
e379e5f3 8936 || i.tm.extension_opcode == 0x0)))
79d72f45
HL
8937 {
8938 *mf_cmp_p = mf_cmp_alu_cmp;
8939 return !(i.mem_operands && i.imm_operands);
8940 }
e379e5f3 8941
79d72f45
HL
8942 /* and without and m, imm. */
8943 if ((i.tm.base_opcode >= 0x20 && i.tm.base_opcode <= 0x25)
8944 || ((i.tm.base_opcode | 3) == 0x83
8945 && i.tm.extension_opcode == 0x4))
8946 {
8947 *mf_cmp_p = mf_cmp_test_and;
8948 return !(i.mem_operands && i.imm_operands);
8949 }
8950
8951 /* test without test m imm. */
e379e5f3
L
8952 if ((i.tm.base_opcode | 1) == 0x85
8953 || (i.tm.base_opcode | 1) == 0xa9
8954 || ((i.tm.base_opcode | 1) == 0xf7
79d72f45
HL
8955 && i.tm.extension_opcode == 0))
8956 {
8957 *mf_cmp_p = mf_cmp_test_and;
8958 return !(i.mem_operands && i.imm_operands);
8959 }
8960
8961 /* cmp without cmp m, imm. */
8962 if ((i.tm.base_opcode >= 0x38 && i.tm.base_opcode <= 0x3d)
e379e5f3
L
8963 || ((i.tm.base_opcode | 3) == 0x83
8964 && (i.tm.extension_opcode == 0x7)))
79d72f45
HL
8965 {
8966 *mf_cmp_p = mf_cmp_alu_cmp;
8967 return !(i.mem_operands && i.imm_operands);
8968 }
e379e5f3 8969
79d72f45 8970 /* inc, dec without inc/dec m. */
e379e5f3
L
8971 if ((i.tm.cpu_flags.bitfield.cpuno64
8972 && (i.tm.base_opcode | 0xf) == 0x4f)
8973 || ((i.tm.base_opcode | 1) == 0xff
8974 && i.tm.extension_opcode <= 0x1))
79d72f45
HL
8975 {
8976 *mf_cmp_p = mf_cmp_incdec;
8977 return !i.mem_operands;
8978 }
e379e5f3
L
8979
8980 return 0;
8981}
8982
8983/* Return 1 if a FUSED_JCC_PADDING frag should be generated. */
8984
8985static int
79d72f45 8986add_fused_jcc_padding_frag_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
8987{
8988 /* NB: Don't work with COND_JUMP86 without i386. */
8989 if (!align_branch_power
8990 || now_seg == absolute_section
8991 || !cpu_arch_flags.bitfield.cpui386
8992 || !(align_branch & align_branch_fused_bit))
8993 return 0;
8994
79d72f45 8995 if (maybe_fused_with_jcc_p (mf_cmp_p))
e379e5f3
L
8996 {
8997 if (last_insn.kind == last_insn_other
8998 || last_insn.seg != now_seg)
8999 return 1;
9000 if (flag_debug)
9001 as_warn_where (last_insn.file, last_insn.line,
9002 _("`%s` skips -malign-branch-boundary on `%s`"),
9003 last_insn.name, i.tm.name);
9004 }
9005
9006 return 0;
9007}
9008
9009/* Return 1 if a BRANCH_PREFIX frag should be generated. */
9010
9011static int
9012add_branch_prefix_frag_p (void)
9013{
9014 /* NB: Don't work with COND_JUMP86 without i386. Don't add prefix
9015 to PadLock instructions since they include prefixes in opcode. */
9016 if (!align_branch_power
9017 || !align_branch_prefix_size
9018 || now_seg == absolute_section
9019 || i.tm.cpu_flags.bitfield.cpupadlock
9020 || !cpu_arch_flags.bitfield.cpui386)
9021 return 0;
9022
9023 /* Don't add prefix if it is a prefix or there is no operand in case
9024 that segment prefix is special. */
9025 if (!i.operands || i.tm.opcode_modifier.isprefix)
9026 return 0;
9027
9028 if (last_insn.kind == last_insn_other
9029 || last_insn.seg != now_seg)
9030 return 1;
9031
9032 if (flag_debug)
9033 as_warn_where (last_insn.file, last_insn.line,
9034 _("`%s` skips -malign-branch-boundary on `%s`"),
9035 last_insn.name, i.tm.name);
9036
9037 return 0;
9038}
9039
9040/* Return 1 if a BRANCH_PADDING frag should be generated. */
9041
9042static int
79d72f45
HL
9043add_branch_padding_frag_p (enum align_branch_kind *branch_p,
9044 enum mf_jcc_kind *mf_jcc_p)
e379e5f3
L
9045{
9046 int add_padding;
9047
9048 /* NB: Don't work with COND_JUMP86 without i386. */
9049 if (!align_branch_power
9050 || now_seg == absolute_section
9051 || !cpu_arch_flags.bitfield.cpui386)
9052 return 0;
9053
9054 add_padding = 0;
9055
9056 /* Check for jcc and direct jmp. */
9057 if (i.tm.opcode_modifier.jump == JUMP)
9058 {
9059 if (i.tm.base_opcode == JUMP_PC_RELATIVE)
9060 {
9061 *branch_p = align_branch_jmp;
9062 add_padding = align_branch & align_branch_jmp_bit;
9063 }
9064 else
9065 {
79d72f45
HL
9066 /* Because J<cc> and JN<cc> share same group in macro-fusible table,
9067 igore the lowest bit. */
9068 *mf_jcc_p = (i.tm.base_opcode & 0x0e) >> 1;
e379e5f3
L
9069 *branch_p = align_branch_jcc;
9070 if ((align_branch & align_branch_jcc_bit))
9071 add_padding = 1;
9072 }
9073 }
9074 else if (is_any_vex_encoding (&i.tm))
9075 return 0;
9076 else if ((i.tm.base_opcode | 1) == 0xc3)
9077 {
9078 /* Near ret. */
9079 *branch_p = align_branch_ret;
9080 if ((align_branch & align_branch_ret_bit))
9081 add_padding = 1;
9082 }
9083 else
9084 {
9085 /* Check for indirect jmp, direct and indirect calls. */
9086 if (i.tm.base_opcode == 0xe8)
9087 {
9088 /* Direct call. */
9089 *branch_p = align_branch_call;
9090 if ((align_branch & align_branch_call_bit))
9091 add_padding = 1;
9092 }
9093 else if (i.tm.base_opcode == 0xff
9094 && (i.tm.extension_opcode == 2
9095 || i.tm.extension_opcode == 4))
9096 {
9097 /* Indirect call and jmp. */
9098 *branch_p = align_branch_indirect;
9099 if ((align_branch & align_branch_indirect_bit))
9100 add_padding = 1;
9101 }
9102
9103 if (add_padding
9104 && i.disp_operands
9105 && tls_get_addr
9106 && (i.op[0].disps->X_op == O_symbol
9107 || (i.op[0].disps->X_op == O_subtract
9108 && i.op[0].disps->X_op_symbol == GOT_symbol)))
9109 {
9110 symbolS *s = i.op[0].disps->X_add_symbol;
9111 /* No padding to call to global or undefined tls_get_addr. */
9112 if ((S_IS_EXTERNAL (s) || !S_IS_DEFINED (s))
9113 && strcmp (S_GET_NAME (s), tls_get_addr) == 0)
9114 return 0;
9115 }
9116 }
9117
9118 if (add_padding
9119 && last_insn.kind != last_insn_other
9120 && last_insn.seg == now_seg)
9121 {
9122 if (flag_debug)
9123 as_warn_where (last_insn.file, last_insn.line,
9124 _("`%s` skips -malign-branch-boundary on `%s`"),
9125 last_insn.name, i.tm.name);
9126 return 0;
9127 }
9128
9129 return add_padding;
9130}
9131
29b0f896 9132static void
e3bb37b5 9133output_insn (void)
29b0f896 9134{
2bbd9c25
JJ
9135 fragS *insn_start_frag;
9136 offsetT insn_start_off;
e379e5f3
L
9137 fragS *fragP = NULL;
9138 enum align_branch_kind branch = align_branch_none;
79d72f45
HL
9139 /* The initializer is arbitrary just to avoid uninitialized error.
9140 it's actually either assigned in add_branch_padding_frag_p
9141 or never be used. */
9142 enum mf_jcc_kind mf_jcc = mf_jcc_jo;
2bbd9c25 9143
b4a3a7b4 9144#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
48ef937e 9145 if (IS_ELF && x86_used_note && now_seg != absolute_section)
b4a3a7b4
L
9146 {
9147 if (i.tm.cpu_flags.bitfield.cpucmov)
9148 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_CMOV;
9149 if (i.tm.cpu_flags.bitfield.cpusse)
9150 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_SSE;
9151 if (i.tm.cpu_flags.bitfield.cpusse2)
9152 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_SSE2;
9153 if (i.tm.cpu_flags.bitfield.cpusse3)
9154 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_SSE3;
9155 if (i.tm.cpu_flags.bitfield.cpussse3)
9156 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_SSSE3;
9157 if (i.tm.cpu_flags.bitfield.cpusse4_1)
9158 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_SSE4_1;
9159 if (i.tm.cpu_flags.bitfield.cpusse4_2)
9160 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_SSE4_2;
9161 if (i.tm.cpu_flags.bitfield.cpuavx)
9162 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_AVX;
9163 if (i.tm.cpu_flags.bitfield.cpuavx2)
9164 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_AVX2;
9165 if (i.tm.cpu_flags.bitfield.cpufma)
9166 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_FMA;
9167 if (i.tm.cpu_flags.bitfield.cpuavx512f)
9168 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_AVX512F;
9169 if (i.tm.cpu_flags.bitfield.cpuavx512cd)
9170 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_AVX512CD;
9171 if (i.tm.cpu_flags.bitfield.cpuavx512er)
9172 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_AVX512ER;
9173 if (i.tm.cpu_flags.bitfield.cpuavx512pf)
9174 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_AVX512PF;
9175 if (i.tm.cpu_flags.bitfield.cpuavx512vl)
9176 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_AVX512VL;
9177 if (i.tm.cpu_flags.bitfield.cpuavx512dq)
9178 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_AVX512DQ;
9179 if (i.tm.cpu_flags.bitfield.cpuavx512bw)
9180 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_AVX512BW;
9181 if (i.tm.cpu_flags.bitfield.cpuavx512_4fmaps)
9182 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_AVX512_4FMAPS;
9183 if (i.tm.cpu_flags.bitfield.cpuavx512_4vnniw)
9184 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_AVX512_4VNNIW;
9185 if (i.tm.cpu_flags.bitfield.cpuavx512_bitalg)
9186 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_AVX512_BITALG;
9187 if (i.tm.cpu_flags.bitfield.cpuavx512ifma)
9188 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_AVX512_IFMA;
9189 if (i.tm.cpu_flags.bitfield.cpuavx512vbmi)
9190 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_AVX512_VBMI;
9191 if (i.tm.cpu_flags.bitfield.cpuavx512_vbmi2)
9192 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_AVX512_VBMI2;
9193 if (i.tm.cpu_flags.bitfield.cpuavx512_vnni)
9194 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_AVX512_VNNI;
462cac58
L
9195 if (i.tm.cpu_flags.bitfield.cpuavx512_bf16)
9196 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_AVX512_BF16;
b4a3a7b4
L
9197
9198 if (i.tm.cpu_flags.bitfield.cpu8087
9199 || i.tm.cpu_flags.bitfield.cpu287
9200 || i.tm.cpu_flags.bitfield.cpu387
9201 || i.tm.cpu_flags.bitfield.cpu687
9202 || i.tm.cpu_flags.bitfield.cpufisttp)
9203 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X87;
921eafea 9204 if ((i.xstate & xstate_mmx)
319ff62c 9205 || i.tm.base_opcode == 0xf77 /* emms */
921eafea 9206 || i.tm.base_opcode == 0xf0e /* femms */)
b4a3a7b4 9207 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MMX;
c4694f17
TG
9208 if ((i.xstate & xstate_xmm)
9209 || i.tm.cpu_flags.bitfield.cpuwidekl
9210 || i.tm.cpu_flags.bitfield.cpukl)
b4a3a7b4 9211 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XMM;
921eafea 9212 if ((i.xstate & xstate_ymm) == xstate_ymm)
b4a3a7b4 9213 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_YMM;
921eafea 9214 if ((i.xstate & xstate_zmm) == xstate_zmm)
b4a3a7b4
L
9215 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_ZMM;
9216 if (i.tm.cpu_flags.bitfield.cpufxsr)
9217 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_FXSR;
9218 if (i.tm.cpu_flags.bitfield.cpuxsave)
9219 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVE;
9220 if (i.tm.cpu_flags.bitfield.cpuxsaveopt)
9221 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT;
9222 if (i.tm.cpu_flags.bitfield.cpuxsavec)
9223 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEC;
a308b89d
L
9224
9225 if ((i.xstate & xstate_tmm) == xstate_tmm
9226 || i.tm.cpu_flags.bitfield.cpuamx_tile)
9227 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_TMM;
b4a3a7b4
L
9228 }
9229#endif
9230
29b0f896
AM
9231 /* Tie dwarf2 debug info to the address at the start of the insn.
9232 We can't do this after the insn has been output as the current
9233 frag may have been closed off. eg. by frag_var. */
9234 dwarf2_emit_insn (0);
9235
2bbd9c25
JJ
9236 insn_start_frag = frag_now;
9237 insn_start_off = frag_now_fix ();
9238
79d72f45 9239 if (add_branch_padding_frag_p (&branch, &mf_jcc))
e379e5f3
L
9240 {
9241 char *p;
9242 /* Branch can be 8 bytes. Leave some room for prefixes. */
9243 unsigned int max_branch_padding_size = 14;
9244
9245 /* Align section to boundary. */
9246 record_alignment (now_seg, align_branch_power);
9247
9248 /* Make room for padding. */
9249 frag_grow (max_branch_padding_size);
9250
9251 /* Start of the padding. */
9252 p = frag_more (0);
9253
9254 fragP = frag_now;
9255
9256 frag_var (rs_machine_dependent, max_branch_padding_size, 0,
9257 ENCODE_RELAX_STATE (BRANCH_PADDING, 0),
9258 NULL, 0, p);
9259
79d72f45 9260 fragP->tc_frag_data.mf_type = mf_jcc;
e379e5f3
L
9261 fragP->tc_frag_data.branch_type = branch;
9262 fragP->tc_frag_data.max_bytes = max_branch_padding_size;
9263 }
9264
29b0f896 9265 /* Output jumps. */
0cfa3eb3 9266 if (i.tm.opcode_modifier.jump == JUMP)
29b0f896 9267 output_branch ();
0cfa3eb3
JB
9268 else if (i.tm.opcode_modifier.jump == JUMP_BYTE
9269 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896 9270 output_jump ();
0cfa3eb3 9271 else if (i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT)
29b0f896
AM
9272 output_interseg_jump ();
9273 else
9274 {
9275 /* Output normal instructions here. */
9276 char *p;
9277 unsigned char *q;
47465058 9278 unsigned int j;
331d2d0d 9279 unsigned int prefix;
79d72f45 9280 enum mf_cmp_kind mf_cmp;
4dffcebc 9281
e4e00185 9282 if (avoid_fence
c3949f43
JB
9283 && (i.tm.base_opcode == 0xfaee8
9284 || i.tm.base_opcode == 0xfaef0
9285 || i.tm.base_opcode == 0xfaef8))
48ef937e
JB
9286 {
9287 /* Encode lfence, mfence, and sfence as
9288 f0 83 04 24 00 lock addl $0x0, (%{re}sp). */
9289 if (now_seg != absolute_section)
9290 {
9291 offsetT val = 0x240483f0ULL;
9292
9293 p = frag_more (5);
9294 md_number_to_chars (p, val, 5);
9295 }
9296 else
9297 abs_section_offset += 5;
9298 return;
9299 }
e4e00185 9300
d022bddd
IT
9301 /* Some processors fail on LOCK prefix. This options makes
9302 assembler ignore LOCK prefix and serves as a workaround. */
9303 if (omit_lock_prefix)
9304 {
9305 if (i.tm.base_opcode == LOCK_PREFIX_OPCODE)
9306 return;
9307 i.prefix[LOCK_PREFIX] = 0;
9308 }
9309
e379e5f3
L
9310 if (branch)
9311 /* Skip if this is a branch. */
9312 ;
79d72f45 9313 else if (add_fused_jcc_padding_frag_p (&mf_cmp))
e379e5f3
L
9314 {
9315 /* Make room for padding. */
9316 frag_grow (MAX_FUSED_JCC_PADDING_SIZE);
9317 p = frag_more (0);
9318
9319 fragP = frag_now;
9320
9321 frag_var (rs_machine_dependent, MAX_FUSED_JCC_PADDING_SIZE, 0,
9322 ENCODE_RELAX_STATE (FUSED_JCC_PADDING, 0),
9323 NULL, 0, p);
9324
79d72f45 9325 fragP->tc_frag_data.mf_type = mf_cmp;
e379e5f3
L
9326 fragP->tc_frag_data.branch_type = align_branch_fused;
9327 fragP->tc_frag_data.max_bytes = MAX_FUSED_JCC_PADDING_SIZE;
9328 }
9329 else if (add_branch_prefix_frag_p ())
9330 {
9331 unsigned int max_prefix_size = align_branch_prefix_size;
9332
9333 /* Make room for padding. */
9334 frag_grow (max_prefix_size);
9335 p = frag_more (0);
9336
9337 fragP = frag_now;
9338
9339 frag_var (rs_machine_dependent, max_prefix_size, 0,
9340 ENCODE_RELAX_STATE (BRANCH_PREFIX, 0),
9341 NULL, 0, p);
9342
9343 fragP->tc_frag_data.max_bytes = max_prefix_size;
9344 }
9345
43234a1e
L
9346 /* Since the VEX/EVEX prefix contains the implicit prefix, we
9347 don't need the explicit prefix. */
9348 if (!i.tm.opcode_modifier.vex && !i.tm.opcode_modifier.evex)
bc4bd9ab 9349 {
c0f3af97 9350 switch (i.tm.opcode_length)
bc4bd9ab 9351 {
c0f3af97
L
9352 case 3:
9353 if (i.tm.base_opcode & 0xff000000)
4dffcebc 9354 {
c0f3af97 9355 prefix = (i.tm.base_opcode >> 24) & 0xff;
c3949f43
JB
9356 if (!i.tm.cpu_flags.bitfield.cpupadlock
9357 || prefix != REPE_PREFIX_OPCODE
9358 || (i.prefix[REP_PREFIX] != REPE_PREFIX_OPCODE))
9359 add_prefix (prefix);
c0f3af97
L
9360 }
9361 break;
9362 case 2:
9363 if ((i.tm.base_opcode & 0xff0000) != 0)
9364 {
9365 prefix = (i.tm.base_opcode >> 16) & 0xff;
c3949f43 9366 add_prefix (prefix);
4dffcebc 9367 }
c0f3af97
L
9368 break;
9369 case 1:
9370 break;
390c91cf
L
9371 case 0:
9372 /* Check for pseudo prefixes. */
9373 as_bad_where (insn_start_frag->fr_file,
9374 insn_start_frag->fr_line,
9375 _("pseudo prefix without instruction"));
9376 return;
c0f3af97
L
9377 default:
9378 abort ();
bc4bd9ab 9379 }
c0f3af97 9380
6d19a37a 9381#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
cf61b747
L
9382 /* For x32, add a dummy REX_OPCODE prefix for mov/add with
9383 R_X86_64_GOTTPOFF relocation so that linker can safely
14470f07
L
9384 perform IE->LE optimization. A dummy REX_OPCODE prefix
9385 is also needed for lea with R_X86_64_GOTPC32_TLSDESC
9386 relocation for GDesc -> IE/LE optimization. */
cf61b747
L
9387 if (x86_elf_abi == X86_64_X32_ABI
9388 && i.operands == 2
14470f07
L
9389 && (i.reloc[0] == BFD_RELOC_X86_64_GOTTPOFF
9390 || i.reloc[0] == BFD_RELOC_X86_64_GOTPC32_TLSDESC)
cf61b747
L
9391 && i.prefix[REX_PREFIX] == 0)
9392 add_prefix (REX_OPCODE);
6d19a37a 9393#endif
cf61b747 9394
c0f3af97
L
9395 /* The prefix bytes. */
9396 for (j = ARRAY_SIZE (i.prefix), q = i.prefix; j > 0; j--, q++)
9397 if (*q)
48ef937e 9398 frag_opcode_byte (*q);
0f10071e 9399 }
ae5c1c7b 9400 else
c0f3af97
L
9401 {
9402 for (j = 0, q = i.prefix; j < ARRAY_SIZE (i.prefix); j++, q++)
9403 if (*q)
9404 switch (j)
9405 {
c0f3af97
L
9406 case SEG_PREFIX:
9407 case ADDR_PREFIX:
48ef937e 9408 frag_opcode_byte (*q);
c0f3af97
L
9409 break;
9410 default:
9411 /* There should be no other prefixes for instructions
9412 with VEX prefix. */
9413 abort ();
9414 }
9415
43234a1e
L
9416 /* For EVEX instructions i.vrex should become 0 after
9417 build_evex_prefix. For VEX instructions upper 16 registers
9418 aren't available, so VREX should be 0. */
9419 if (i.vrex)
9420 abort ();
c0f3af97 9421 /* Now the VEX prefix. */
48ef937e
JB
9422 if (now_seg != absolute_section)
9423 {
9424 p = frag_more (i.vex.length);
9425 for (j = 0; j < i.vex.length; j++)
9426 p[j] = i.vex.bytes[j];
9427 }
9428 else
9429 abs_section_offset += i.vex.length;
c0f3af97 9430 }
252b5132 9431
29b0f896 9432 /* Now the opcode; be careful about word order here! */
48ef937e
JB
9433 if (now_seg == absolute_section)
9434 abs_section_offset += i.tm.opcode_length;
9435 else if (i.tm.opcode_length == 1)
29b0f896
AM
9436 {
9437 FRAG_APPEND_1_CHAR (i.tm.base_opcode);
9438 }
9439 else
9440 {
4dffcebc 9441 switch (i.tm.opcode_length)
331d2d0d 9442 {
43234a1e
L
9443 case 4:
9444 p = frag_more (4);
9445 *p++ = (i.tm.base_opcode >> 24) & 0xff;
9446 *p++ = (i.tm.base_opcode >> 16) & 0xff;
9447 break;
4dffcebc 9448 case 3:
331d2d0d
L
9449 p = frag_more (3);
9450 *p++ = (i.tm.base_opcode >> 16) & 0xff;
4dffcebc
L
9451 break;
9452 case 2:
9453 p = frag_more (2);
9454 break;
9455 default:
9456 abort ();
9457 break;
331d2d0d 9458 }
0f10071e 9459
29b0f896
AM
9460 /* Put out high byte first: can't use md_number_to_chars! */
9461 *p++ = (i.tm.base_opcode >> 8) & 0xff;
9462 *p = i.tm.base_opcode & 0xff;
9463 }
3e73aa7c 9464
29b0f896 9465 /* Now the modrm byte and sib byte (if present). */
40fb9820 9466 if (i.tm.opcode_modifier.modrm)
29b0f896 9467 {
48ef937e
JB
9468 frag_opcode_byte ((i.rm.regmem << 0)
9469 | (i.rm.reg << 3)
9470 | (i.rm.mode << 6));
29b0f896
AM
9471 /* If i.rm.regmem == ESP (4)
9472 && i.rm.mode != (Register mode)
9473 && not 16 bit
9474 ==> need second modrm byte. */
9475 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
9476 && i.rm.mode != 3
dc821c5f 9477 && !(i.base_reg && i.base_reg->reg_type.bitfield.word))
48ef937e
JB
9478 frag_opcode_byte ((i.sib.base << 0)
9479 | (i.sib.index << 3)
9480 | (i.sib.scale << 6));
29b0f896 9481 }
3e73aa7c 9482
29b0f896 9483 if (i.disp_operands)
2bbd9c25 9484 output_disp (insn_start_frag, insn_start_off);
3e73aa7c 9485
29b0f896 9486 if (i.imm_operands)
2bbd9c25 9487 output_imm (insn_start_frag, insn_start_off);
9c33702b
JB
9488
9489 /*
9490 * frag_now_fix () returning plain abs_section_offset when we're in the
9491 * absolute section, and abs_section_offset not getting updated as data
9492 * gets added to the frag breaks the logic below.
9493 */
9494 if (now_seg != absolute_section)
9495 {
9496 j = encoding_length (insn_start_frag, insn_start_off, frag_more (0));
9497 if (j > 15)
9498 as_warn (_("instruction length of %u bytes exceeds the limit of 15"),
9499 j);
e379e5f3
L
9500 else if (fragP)
9501 {
9502 /* NB: Don't add prefix with GOTPC relocation since
9503 output_disp() above depends on the fixed encoding
9504 length. Can't add prefix with TLS relocation since
9505 it breaks TLS linker optimization. */
9506 unsigned int max = i.has_gotpc_tls_reloc ? 0 : 15 - j;
9507 /* Prefix count on the current instruction. */
9508 unsigned int count = i.vex.length;
9509 unsigned int k;
9510 for (k = 0; k < ARRAY_SIZE (i.prefix); k++)
9511 /* REX byte is encoded in VEX/EVEX prefix. */
9512 if (i.prefix[k] && (k != REX_PREFIX || !i.vex.length))
9513 count++;
9514
9515 /* Count prefixes for extended opcode maps. */
9516 if (!i.vex.length)
9517 switch (i.tm.opcode_length)
9518 {
9519 case 3:
9520 if (((i.tm.base_opcode >> 16) & 0xff) == 0xf)
9521 {
9522 count++;
9523 switch ((i.tm.base_opcode >> 8) & 0xff)
9524 {
9525 case 0x38:
9526 case 0x3a:
9527 count++;
9528 break;
9529 default:
9530 break;
9531 }
9532 }
9533 break;
9534 case 2:
9535 if (((i.tm.base_opcode >> 8) & 0xff) == 0xf)
9536 count++;
9537 break;
9538 case 1:
9539 break;
9540 default:
9541 abort ();
9542 }
9543
9544 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
9545 == BRANCH_PREFIX)
9546 {
9547 /* Set the maximum prefix size in BRANCH_PREFIX
9548 frag. */
9549 if (fragP->tc_frag_data.max_bytes > max)
9550 fragP->tc_frag_data.max_bytes = max;
9551 if (fragP->tc_frag_data.max_bytes > count)
9552 fragP->tc_frag_data.max_bytes -= count;
9553 else
9554 fragP->tc_frag_data.max_bytes = 0;
9555 }
9556 else
9557 {
9558 /* Remember the maximum prefix size in FUSED_JCC_PADDING
9559 frag. */
9560 unsigned int max_prefix_size;
9561 if (align_branch_prefix_size > max)
9562 max_prefix_size = max;
9563 else
9564 max_prefix_size = align_branch_prefix_size;
9565 if (max_prefix_size > count)
9566 fragP->tc_frag_data.max_prefix_length
9567 = max_prefix_size - count;
9568 }
9569
9570 /* Use existing segment prefix if possible. Use CS
9571 segment prefix in 64-bit mode. In 32-bit mode, use SS
9572 segment prefix with ESP/EBP base register and use DS
9573 segment prefix without ESP/EBP base register. */
9574 if (i.prefix[SEG_PREFIX])
9575 fragP->tc_frag_data.default_prefix = i.prefix[SEG_PREFIX];
9576 else if (flag_code == CODE_64BIT)
9577 fragP->tc_frag_data.default_prefix = CS_PREFIX_OPCODE;
9578 else if (i.base_reg
9579 && (i.base_reg->reg_num == 4
9580 || i.base_reg->reg_num == 5))
9581 fragP->tc_frag_data.default_prefix = SS_PREFIX_OPCODE;
9582 else
9583 fragP->tc_frag_data.default_prefix = DS_PREFIX_OPCODE;
9584 }
9c33702b 9585 }
29b0f896 9586 }
252b5132 9587
e379e5f3
L
9588 /* NB: Don't work with COND_JUMP86 without i386. */
9589 if (align_branch_power
9590 && now_seg != absolute_section
9591 && cpu_arch_flags.bitfield.cpui386)
9592 {
9593 /* Terminate each frag so that we can add prefix and check for
9594 fused jcc. */
9595 frag_wane (frag_now);
9596 frag_new (0);
9597 }
9598
29b0f896
AM
9599#ifdef DEBUG386
9600 if (flag_debug)
9601 {
7b81dfbb 9602 pi ("" /*line*/, &i);
29b0f896
AM
9603 }
9604#endif /* DEBUG386 */
9605}
252b5132 9606
e205caa7
L
9607/* Return the size of the displacement operand N. */
9608
9609static int
9610disp_size (unsigned int n)
9611{
9612 int size = 4;
43234a1e 9613
b5014f7a 9614 if (i.types[n].bitfield.disp64)
40fb9820
L
9615 size = 8;
9616 else if (i.types[n].bitfield.disp8)
9617 size = 1;
9618 else if (i.types[n].bitfield.disp16)
9619 size = 2;
e205caa7
L
9620 return size;
9621}
9622
9623/* Return the size of the immediate operand N. */
9624
9625static int
9626imm_size (unsigned int n)
9627{
9628 int size = 4;
40fb9820
L
9629 if (i.types[n].bitfield.imm64)
9630 size = 8;
9631 else if (i.types[n].bitfield.imm8 || i.types[n].bitfield.imm8s)
9632 size = 1;
9633 else if (i.types[n].bitfield.imm16)
9634 size = 2;
e205caa7
L
9635 return size;
9636}
9637
29b0f896 9638static void
64e74474 9639output_disp (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
9640{
9641 char *p;
9642 unsigned int n;
252b5132 9643
29b0f896
AM
9644 for (n = 0; n < i.operands; n++)
9645 {
b5014f7a 9646 if (operand_type_check (i.types[n], disp))
29b0f896 9647 {
48ef937e
JB
9648 int size = disp_size (n);
9649
9650 if (now_seg == absolute_section)
9651 abs_section_offset += size;
9652 else if (i.op[n].disps->X_op == O_constant)
29b0f896 9653 {
43234a1e 9654 offsetT val = i.op[n].disps->X_add_number;
252b5132 9655
629cfaf1
JB
9656 val = offset_in_range (val >> (size == 1 ? i.memshift : 0),
9657 size);
29b0f896
AM
9658 p = frag_more (size);
9659 md_number_to_chars (p, val, size);
9660 }
9661 else
9662 {
f86103b7 9663 enum bfd_reloc_code_real reloc_type;
40fb9820 9664 int sign = i.types[n].bitfield.disp32s;
29b0f896 9665 int pcrel = (i.flags[n] & Operand_PCrel) != 0;
02a86693 9666 fixS *fixP;
29b0f896 9667
e205caa7 9668 /* We can't have 8 bit displacement here. */
9c2799c2 9669 gas_assert (!i.types[n].bitfield.disp8);
e205caa7 9670
29b0f896
AM
9671 /* The PC relative address is computed relative
9672 to the instruction boundary, so in case immediate
9673 fields follows, we need to adjust the value. */
9674 if (pcrel && i.imm_operands)
9675 {
29b0f896 9676 unsigned int n1;
e205caa7 9677 int sz = 0;
252b5132 9678
29b0f896 9679 for (n1 = 0; n1 < i.operands; n1++)
40fb9820 9680 if (operand_type_check (i.types[n1], imm))
252b5132 9681 {
e205caa7
L
9682 /* Only one immediate is allowed for PC
9683 relative address. */
9c2799c2 9684 gas_assert (sz == 0);
e205caa7
L
9685 sz = imm_size (n1);
9686 i.op[n].disps->X_add_number -= sz;
252b5132 9687 }
29b0f896 9688 /* We should find the immediate. */
9c2799c2 9689 gas_assert (sz != 0);
29b0f896 9690 }
520dc8e8 9691
29b0f896 9692 p = frag_more (size);
d258b828 9693 reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
d6ab8113 9694 if (GOT_symbol
2bbd9c25 9695 && GOT_symbol == i.op[n].disps->X_add_symbol
d6ab8113 9696 && (((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
9697 || reloc_type == BFD_RELOC_X86_64_32S
9698 || (reloc_type == BFD_RELOC_64
9699 && object_64bit))
d6ab8113
JB
9700 && (i.op[n].disps->X_op == O_symbol
9701 || (i.op[n].disps->X_op == O_add
9702 && ((symbol_get_value_expression
9703 (i.op[n].disps->X_op_symbol)->X_op)
9704 == O_subtract))))
9705 || reloc_type == BFD_RELOC_32_PCREL))
2bbd9c25 9706 {
4fa24527 9707 if (!object_64bit)
7b81dfbb
AJ
9708 {
9709 reloc_type = BFD_RELOC_386_GOTPC;
e379e5f3 9710 i.has_gotpc_tls_reloc = TRUE;
d583596c
JB
9711 i.op[n].imms->X_add_number +=
9712 encoding_length (insn_start_frag, insn_start_off, p);
7b81dfbb
AJ
9713 }
9714 else if (reloc_type == BFD_RELOC_64)
9715 reloc_type = BFD_RELOC_X86_64_GOTPC64;
d6ab8113 9716 else
7b81dfbb
AJ
9717 /* Don't do the adjustment for x86-64, as there
9718 the pcrel addressing is relative to the _next_
9719 insn, and that is taken care of in other code. */
d6ab8113 9720 reloc_type = BFD_RELOC_X86_64_GOTPC32;
2bbd9c25 9721 }
e379e5f3
L
9722 else if (align_branch_power)
9723 {
9724 switch (reloc_type)
9725 {
9726 case BFD_RELOC_386_TLS_GD:
9727 case BFD_RELOC_386_TLS_LDM:
9728 case BFD_RELOC_386_TLS_IE:
9729 case BFD_RELOC_386_TLS_IE_32:
9730 case BFD_RELOC_386_TLS_GOTIE:
9731 case BFD_RELOC_386_TLS_GOTDESC:
9732 case BFD_RELOC_386_TLS_DESC_CALL:
9733 case BFD_RELOC_X86_64_TLSGD:
9734 case BFD_RELOC_X86_64_TLSLD:
9735 case BFD_RELOC_X86_64_GOTTPOFF:
9736 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
9737 case BFD_RELOC_X86_64_TLSDESC_CALL:
9738 i.has_gotpc_tls_reloc = TRUE;
9739 default:
9740 break;
9741 }
9742 }
02a86693
L
9743 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal,
9744 size, i.op[n].disps, pcrel,
9745 reloc_type);
9746 /* Check for "call/jmp *mem", "mov mem, %reg",
9747 "test %reg, mem" and "binop mem, %reg" where binop
9748 is one of adc, add, and, cmp, or, sbb, sub, xor
e60f4d3b
L
9749 instructions without data prefix. Always generate
9750 R_386_GOT32X for "sym*GOT" operand in 32-bit mode. */
9751 if (i.prefix[DATA_PREFIX] == 0
9752 && (generate_relax_relocations
9753 || (!object_64bit
9754 && i.rm.mode == 0
9755 && i.rm.regmem == 5))
0cb4071e
L
9756 && (i.rm.mode == 2
9757 || (i.rm.mode == 0 && i.rm.regmem == 5))
2ae4c703 9758 && !is_any_vex_encoding(&i.tm)
02a86693
L
9759 && ((i.operands == 1
9760 && i.tm.base_opcode == 0xff
9761 && (i.rm.reg == 2 || i.rm.reg == 4))
9762 || (i.operands == 2
9763 && (i.tm.base_opcode == 0x8b
9764 || i.tm.base_opcode == 0x85
2ae4c703 9765 || (i.tm.base_opcode & ~0x38) == 0x03))))
02a86693
L
9766 {
9767 if (object_64bit)
9768 {
9769 fixP->fx_tcbit = i.rex != 0;
9770 if (i.base_reg
e968fc9b 9771 && (i.base_reg->reg_num == RegIP))
02a86693
L
9772 fixP->fx_tcbit2 = 1;
9773 }
9774 else
9775 fixP->fx_tcbit2 = 1;
9776 }
29b0f896
AM
9777 }
9778 }
9779 }
9780}
252b5132 9781
29b0f896 9782static void
64e74474 9783output_imm (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
9784{
9785 char *p;
9786 unsigned int n;
252b5132 9787
29b0f896
AM
9788 for (n = 0; n < i.operands; n++)
9789 {
43234a1e
L
9790 /* Skip SAE/RC Imm operand in EVEX. They are already handled. */
9791 if (i.rounding && (int) n == i.rounding->operand)
9792 continue;
9793
40fb9820 9794 if (operand_type_check (i.types[n], imm))
29b0f896 9795 {
48ef937e
JB
9796 int size = imm_size (n);
9797
9798 if (now_seg == absolute_section)
9799 abs_section_offset += size;
9800 else if (i.op[n].imms->X_op == O_constant)
29b0f896 9801 {
29b0f896 9802 offsetT val;
b4cac588 9803
29b0f896
AM
9804 val = offset_in_range (i.op[n].imms->X_add_number,
9805 size);
9806 p = frag_more (size);
9807 md_number_to_chars (p, val, size);
9808 }
9809 else
9810 {
9811 /* Not absolute_section.
9812 Need a 32-bit fixup (don't support 8bit
9813 non-absolute imms). Try to support other
9814 sizes ... */
f86103b7 9815 enum bfd_reloc_code_real reloc_type;
e205caa7 9816 int sign;
29b0f896 9817
40fb9820 9818 if (i.types[n].bitfield.imm32s
a7d61044 9819 && (i.suffix == QWORD_MNEM_SUFFIX
40fb9820 9820 || (!i.suffix && i.tm.opcode_modifier.no_lsuf)))
29b0f896 9821 sign = 1;
e205caa7
L
9822 else
9823 sign = 0;
520dc8e8 9824
29b0f896 9825 p = frag_more (size);
d258b828 9826 reloc_type = reloc (size, 0, sign, i.reloc[n]);
f86103b7 9827
2bbd9c25
JJ
9828 /* This is tough to explain. We end up with this one if we
9829 * have operands that look like
9830 * "_GLOBAL_OFFSET_TABLE_+[.-.L284]". The goal here is to
9831 * obtain the absolute address of the GOT, and it is strongly
9832 * preferable from a performance point of view to avoid using
9833 * a runtime relocation for this. The actual sequence of
9834 * instructions often look something like:
9835 *
9836 * call .L66
9837 * .L66:
9838 * popl %ebx
9839 * addl $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
9840 *
9841 * The call and pop essentially return the absolute address
9842 * of the label .L66 and store it in %ebx. The linker itself
9843 * will ultimately change the first operand of the addl so
9844 * that %ebx points to the GOT, but to keep things simple, the
9845 * .o file must have this operand set so that it generates not
9846 * the absolute address of .L66, but the absolute address of
9847 * itself. This allows the linker itself simply treat a GOTPC
9848 * relocation as asking for a pcrel offset to the GOT to be
9849 * added in, and the addend of the relocation is stored in the
9850 * operand field for the instruction itself.
9851 *
9852 * Our job here is to fix the operand so that it would add
9853 * the correct offset so that %ebx would point to itself. The
9854 * thing that is tricky is that .-.L66 will point to the
9855 * beginning of the instruction, so we need to further modify
9856 * the operand so that it will point to itself. There are
9857 * other cases where you have something like:
9858 *
9859 * .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
9860 *
9861 * and here no correction would be required. Internally in
9862 * the assembler we treat operands of this form as not being
9863 * pcrel since the '.' is explicitly mentioned, and I wonder
9864 * whether it would simplify matters to do it this way. Who
9865 * knows. In earlier versions of the PIC patches, the
9866 * pcrel_adjust field was used to store the correction, but
9867 * since the expression is not pcrel, I felt it would be
9868 * confusing to do it this way. */
9869
d6ab8113 9870 if ((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
9871 || reloc_type == BFD_RELOC_X86_64_32S
9872 || reloc_type == BFD_RELOC_64)
29b0f896
AM
9873 && GOT_symbol
9874 && GOT_symbol == i.op[n].imms->X_add_symbol
9875 && (i.op[n].imms->X_op == O_symbol
9876 || (i.op[n].imms->X_op == O_add
9877 && ((symbol_get_value_expression
9878 (i.op[n].imms->X_op_symbol)->X_op)
9879 == O_subtract))))
9880 {
4fa24527 9881 if (!object_64bit)
d6ab8113 9882 reloc_type = BFD_RELOC_386_GOTPC;
7b81dfbb 9883 else if (size == 4)
d6ab8113 9884 reloc_type = BFD_RELOC_X86_64_GOTPC32;
7b81dfbb
AJ
9885 else if (size == 8)
9886 reloc_type = BFD_RELOC_X86_64_GOTPC64;
e379e5f3 9887 i.has_gotpc_tls_reloc = TRUE;
d583596c
JB
9888 i.op[n].imms->X_add_number +=
9889 encoding_length (insn_start_frag, insn_start_off, p);
29b0f896 9890 }
29b0f896
AM
9891 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
9892 i.op[n].imms, 0, reloc_type);
9893 }
9894 }
9895 }
252b5132
RH
9896}
9897\f
d182319b
JB
9898/* x86_cons_fix_new is called via the expression parsing code when a
9899 reloc is needed. We use this hook to get the correct .got reloc. */
d182319b
JB
9900static int cons_sign = -1;
9901
9902void
e3bb37b5 9903x86_cons_fix_new (fragS *frag, unsigned int off, unsigned int len,
62ebcb5c 9904 expressionS *exp, bfd_reloc_code_real_type r)
d182319b 9905{
d258b828 9906 r = reloc (len, 0, cons_sign, r);
d182319b
JB
9907
9908#ifdef TE_PE
9909 if (exp->X_op == O_secrel)
9910 {
9911 exp->X_op = O_symbol;
9912 r = BFD_RELOC_32_SECREL;
9913 }
9914#endif
9915
9916 fix_new_exp (frag, off, len, exp, 0, r);
9917}
9918
357d1bd8
L
9919/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
9920 purpose of the `.dc.a' internal pseudo-op. */
9921
9922int
9923x86_address_bytes (void)
9924{
9925 if ((stdoutput->arch_info->mach & bfd_mach_x64_32))
9926 return 4;
9927 return stdoutput->arch_info->bits_per_address / 8;
9928}
9929
d382c579
TG
9930#if !(defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined (OBJ_MACH_O)) \
9931 || defined (LEX_AT)
d258b828 9932# define lex_got(reloc, adjust, types) NULL
718ddfc0 9933#else
f3c180ae
AM
9934/* Parse operands of the form
9935 <symbol>@GOTOFF+<nnn>
9936 and similar .plt or .got references.
9937
9938 If we find one, set up the correct relocation in RELOC and copy the
9939 input string, minus the `@GOTOFF' into a malloc'd buffer for
9940 parsing by the calling routine. Return this buffer, and if ADJUST
9941 is non-null set it to the length of the string we removed from the
9942 input line. Otherwise return NULL. */
9943static char *
91d6fa6a 9944lex_got (enum bfd_reloc_code_real *rel,
64e74474 9945 int *adjust,
d258b828 9946 i386_operand_type *types)
f3c180ae 9947{
7b81dfbb
AJ
9948 /* Some of the relocations depend on the size of what field is to
9949 be relocated. But in our callers i386_immediate and i386_displacement
9950 we don't yet know the operand size (this will be set by insn
9951 matching). Hence we record the word32 relocation here,
9952 and adjust the reloc according to the real size in reloc(). */
f3c180ae
AM
9953 static const struct {
9954 const char *str;
cff8d58a 9955 int len;
4fa24527 9956 const enum bfd_reloc_code_real rel[2];
40fb9820 9957 const i386_operand_type types64;
f3c180ae 9958 } gotrel[] = {
8ce3d284 9959#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
9960 { STRING_COMMA_LEN ("SIZE"), { BFD_RELOC_SIZE32,
9961 BFD_RELOC_SIZE32 },
9962 OPERAND_TYPE_IMM32_64 },
8ce3d284 9963#endif
cff8d58a
L
9964 { STRING_COMMA_LEN ("PLTOFF"), { _dummy_first_bfd_reloc_code_real,
9965 BFD_RELOC_X86_64_PLTOFF64 },
40fb9820 9966 OPERAND_TYPE_IMM64 },
cff8d58a
L
9967 { STRING_COMMA_LEN ("PLT"), { BFD_RELOC_386_PLT32,
9968 BFD_RELOC_X86_64_PLT32 },
40fb9820 9969 OPERAND_TYPE_IMM32_32S_DISP32 },
cff8d58a
L
9970 { STRING_COMMA_LEN ("GOTPLT"), { _dummy_first_bfd_reloc_code_real,
9971 BFD_RELOC_X86_64_GOTPLT64 },
40fb9820 9972 OPERAND_TYPE_IMM64_DISP64 },
cff8d58a
L
9973 { STRING_COMMA_LEN ("GOTOFF"), { BFD_RELOC_386_GOTOFF,
9974 BFD_RELOC_X86_64_GOTOFF64 },
40fb9820 9975 OPERAND_TYPE_IMM64_DISP64 },
cff8d58a
L
9976 { STRING_COMMA_LEN ("GOTPCREL"), { _dummy_first_bfd_reloc_code_real,
9977 BFD_RELOC_X86_64_GOTPCREL },
40fb9820 9978 OPERAND_TYPE_IMM32_32S_DISP32 },
cff8d58a
L
9979 { STRING_COMMA_LEN ("TLSGD"), { BFD_RELOC_386_TLS_GD,
9980 BFD_RELOC_X86_64_TLSGD },
40fb9820 9981 OPERAND_TYPE_IMM32_32S_DISP32 },
cff8d58a
L
9982 { STRING_COMMA_LEN ("TLSLDM"), { BFD_RELOC_386_TLS_LDM,
9983 _dummy_first_bfd_reloc_code_real },
40fb9820 9984 OPERAND_TYPE_NONE },
cff8d58a
L
9985 { STRING_COMMA_LEN ("TLSLD"), { _dummy_first_bfd_reloc_code_real,
9986 BFD_RELOC_X86_64_TLSLD },
40fb9820 9987 OPERAND_TYPE_IMM32_32S_DISP32 },
cff8d58a
L
9988 { STRING_COMMA_LEN ("GOTTPOFF"), { BFD_RELOC_386_TLS_IE_32,
9989 BFD_RELOC_X86_64_GOTTPOFF },
40fb9820 9990 OPERAND_TYPE_IMM32_32S_DISP32 },
cff8d58a
L
9991 { STRING_COMMA_LEN ("TPOFF"), { BFD_RELOC_386_TLS_LE_32,
9992 BFD_RELOC_X86_64_TPOFF32 },
40fb9820 9993 OPERAND_TYPE_IMM32_32S_64_DISP32_64 },
cff8d58a
L
9994 { STRING_COMMA_LEN ("NTPOFF"), { BFD_RELOC_386_TLS_LE,
9995 _dummy_first_bfd_reloc_code_real },
40fb9820 9996 OPERAND_TYPE_NONE },
cff8d58a
L
9997 { STRING_COMMA_LEN ("DTPOFF"), { BFD_RELOC_386_TLS_LDO_32,
9998 BFD_RELOC_X86_64_DTPOFF32 },
40fb9820 9999 OPERAND_TYPE_IMM32_32S_64_DISP32_64 },
cff8d58a
L
10000 { STRING_COMMA_LEN ("GOTNTPOFF"),{ BFD_RELOC_386_TLS_GOTIE,
10001 _dummy_first_bfd_reloc_code_real },
40fb9820 10002 OPERAND_TYPE_NONE },
cff8d58a
L
10003 { STRING_COMMA_LEN ("INDNTPOFF"),{ BFD_RELOC_386_TLS_IE,
10004 _dummy_first_bfd_reloc_code_real },
40fb9820 10005 OPERAND_TYPE_NONE },
cff8d58a
L
10006 { STRING_COMMA_LEN ("GOT"), { BFD_RELOC_386_GOT32,
10007 BFD_RELOC_X86_64_GOT32 },
40fb9820 10008 OPERAND_TYPE_IMM32_32S_64_DISP32 },
cff8d58a
L
10009 { STRING_COMMA_LEN ("TLSDESC"), { BFD_RELOC_386_TLS_GOTDESC,
10010 BFD_RELOC_X86_64_GOTPC32_TLSDESC },
40fb9820 10011 OPERAND_TYPE_IMM32_32S_DISP32 },
cff8d58a
L
10012 { STRING_COMMA_LEN ("TLSCALL"), { BFD_RELOC_386_TLS_DESC_CALL,
10013 BFD_RELOC_X86_64_TLSDESC_CALL },
40fb9820 10014 OPERAND_TYPE_IMM32_32S_DISP32 },
f3c180ae
AM
10015 };
10016 char *cp;
10017 unsigned int j;
10018
d382c579 10019#if defined (OBJ_MAYBE_ELF)
718ddfc0
JB
10020 if (!IS_ELF)
10021 return NULL;
d382c579 10022#endif
718ddfc0 10023
f3c180ae 10024 for (cp = input_line_pointer; *cp != '@'; cp++)
67c11a9b 10025 if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
f3c180ae
AM
10026 return NULL;
10027
47465058 10028 for (j = 0; j < ARRAY_SIZE (gotrel); j++)
f3c180ae 10029 {
cff8d58a 10030 int len = gotrel[j].len;
28f81592 10031 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
f3c180ae 10032 {
4fa24527 10033 if (gotrel[j].rel[object_64bit] != 0)
f3c180ae 10034 {
28f81592
AM
10035 int first, second;
10036 char *tmpbuf, *past_reloc;
f3c180ae 10037
91d6fa6a 10038 *rel = gotrel[j].rel[object_64bit];
f3c180ae 10039
3956db08
JB
10040 if (types)
10041 {
10042 if (flag_code != CODE_64BIT)
40fb9820
L
10043 {
10044 types->bitfield.imm32 = 1;
10045 types->bitfield.disp32 = 1;
10046 }
3956db08
JB
10047 else
10048 *types = gotrel[j].types64;
10049 }
10050
8fd4256d 10051 if (j != 0 && GOT_symbol == NULL)
f3c180ae
AM
10052 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
10053
28f81592 10054 /* The length of the first part of our input line. */
f3c180ae 10055 first = cp - input_line_pointer;
28f81592
AM
10056
10057 /* The second part goes from after the reloc token until
67c11a9b 10058 (and including) an end_of_line char or comma. */
28f81592 10059 past_reloc = cp + 1 + len;
67c11a9b
AM
10060 cp = past_reloc;
10061 while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
10062 ++cp;
10063 second = cp + 1 - past_reloc;
28f81592
AM
10064
10065 /* Allocate and copy string. The trailing NUL shouldn't
10066 be necessary, but be safe. */
add39d23 10067 tmpbuf = XNEWVEC (char, first + second + 2);
f3c180ae 10068 memcpy (tmpbuf, input_line_pointer, first);
0787a12d
AM
10069 if (second != 0 && *past_reloc != ' ')
10070 /* Replace the relocation token with ' ', so that
10071 errors like foo@GOTOFF1 will be detected. */
10072 tmpbuf[first++] = ' ';
af89796a
L
10073 else
10074 /* Increment length by 1 if the relocation token is
10075 removed. */
10076 len++;
10077 if (adjust)
10078 *adjust = len;
0787a12d
AM
10079 memcpy (tmpbuf + first, past_reloc, second);
10080 tmpbuf[first + second] = '\0';
f3c180ae
AM
10081 return tmpbuf;
10082 }
10083
4fa24527
JB
10084 as_bad (_("@%s reloc is not supported with %d-bit output format"),
10085 gotrel[j].str, 1 << (5 + object_64bit));
f3c180ae
AM
10086 return NULL;
10087 }
10088 }
10089
10090 /* Might be a symbol version string. Don't as_bad here. */
10091 return NULL;
10092}
4e4f7c87 10093#endif
f3c180ae 10094
a988325c
NC
10095#ifdef TE_PE
10096#ifdef lex_got
10097#undef lex_got
10098#endif
10099/* Parse operands of the form
10100 <symbol>@SECREL32+<nnn>
10101
10102 If we find one, set up the correct relocation in RELOC and copy the
10103 input string, minus the `@SECREL32' into a malloc'd buffer for
10104 parsing by the calling routine. Return this buffer, and if ADJUST
10105 is non-null set it to the length of the string we removed from the
34bca508
L
10106 input line. Otherwise return NULL.
10107
a988325c
NC
10108 This function is copied from the ELF version above adjusted for PE targets. */
10109
10110static char *
10111lex_got (enum bfd_reloc_code_real *rel ATTRIBUTE_UNUSED,
10112 int *adjust ATTRIBUTE_UNUSED,
d258b828 10113 i386_operand_type *types)
a988325c
NC
10114{
10115 static const struct
10116 {
10117 const char *str;
10118 int len;
10119 const enum bfd_reloc_code_real rel[2];
10120 const i386_operand_type types64;
10121 }
10122 gotrel[] =
10123 {
10124 { STRING_COMMA_LEN ("SECREL32"), { BFD_RELOC_32_SECREL,
10125 BFD_RELOC_32_SECREL },
10126 OPERAND_TYPE_IMM32_32S_64_DISP32_64 },
10127 };
10128
10129 char *cp;
10130 unsigned j;
10131
10132 for (cp = input_line_pointer; *cp != '@'; cp++)
10133 if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
10134 return NULL;
10135
10136 for (j = 0; j < ARRAY_SIZE (gotrel); j++)
10137 {
10138 int len = gotrel[j].len;
10139
10140 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
10141 {
10142 if (gotrel[j].rel[object_64bit] != 0)
10143 {
10144 int first, second;
10145 char *tmpbuf, *past_reloc;
10146
10147 *rel = gotrel[j].rel[object_64bit];
10148 if (adjust)
10149 *adjust = len;
10150
10151 if (types)
10152 {
10153 if (flag_code != CODE_64BIT)
10154 {
10155 types->bitfield.imm32 = 1;
10156 types->bitfield.disp32 = 1;
10157 }
10158 else
10159 *types = gotrel[j].types64;
10160 }
10161
10162 /* The length of the first part of our input line. */
10163 first = cp - input_line_pointer;
10164
10165 /* The second part goes from after the reloc token until
10166 (and including) an end_of_line char or comma. */
10167 past_reloc = cp + 1 + len;
10168 cp = past_reloc;
10169 while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
10170 ++cp;
10171 second = cp + 1 - past_reloc;
10172
10173 /* Allocate and copy string. The trailing NUL shouldn't
10174 be necessary, but be safe. */
add39d23 10175 tmpbuf = XNEWVEC (char, first + second + 2);
a988325c
NC
10176 memcpy (tmpbuf, input_line_pointer, first);
10177 if (second != 0 && *past_reloc != ' ')
10178 /* Replace the relocation token with ' ', so that
10179 errors like foo@SECLREL321 will be detected. */
10180 tmpbuf[first++] = ' ';
10181 memcpy (tmpbuf + first, past_reloc, second);
10182 tmpbuf[first + second] = '\0';
10183 return tmpbuf;
10184 }
10185
10186 as_bad (_("@%s reloc is not supported with %d-bit output format"),
10187 gotrel[j].str, 1 << (5 + object_64bit));
10188 return NULL;
10189 }
10190 }
10191
10192 /* Might be a symbol version string. Don't as_bad here. */
10193 return NULL;
10194}
10195
10196#endif /* TE_PE */
10197
62ebcb5c 10198bfd_reloc_code_real_type
e3bb37b5 10199x86_cons (expressionS *exp, int size)
f3c180ae 10200{
62ebcb5c
AM
10201 bfd_reloc_code_real_type got_reloc = NO_RELOC;
10202
ee86248c
JB
10203 intel_syntax = -intel_syntax;
10204
3c7b9c2c 10205 exp->X_md = 0;
4fa24527 10206 if (size == 4 || (object_64bit && size == 8))
f3c180ae
AM
10207 {
10208 /* Handle @GOTOFF and the like in an expression. */
10209 char *save;
10210 char *gotfree_input_line;
4a57f2cf 10211 int adjust = 0;
f3c180ae
AM
10212
10213 save = input_line_pointer;
d258b828 10214 gotfree_input_line = lex_got (&got_reloc, &adjust, NULL);
f3c180ae
AM
10215 if (gotfree_input_line)
10216 input_line_pointer = gotfree_input_line;
10217
10218 expression (exp);
10219
10220 if (gotfree_input_line)
10221 {
10222 /* expression () has merrily parsed up to the end of line,
10223 or a comma - in the wrong buffer. Transfer how far
10224 input_line_pointer has moved to the right buffer. */
10225 input_line_pointer = (save
10226 + (input_line_pointer - gotfree_input_line)
10227 + adjust);
10228 free (gotfree_input_line);
3992d3b7
AM
10229 if (exp->X_op == O_constant
10230 || exp->X_op == O_absent
10231 || exp->X_op == O_illegal
0398aac5 10232 || exp->X_op == O_register
3992d3b7
AM
10233 || exp->X_op == O_big)
10234 {
10235 char c = *input_line_pointer;
10236 *input_line_pointer = 0;
10237 as_bad (_("missing or invalid expression `%s'"), save);
10238 *input_line_pointer = c;
10239 }
b9519cfe
L
10240 else if ((got_reloc == BFD_RELOC_386_PLT32
10241 || got_reloc == BFD_RELOC_X86_64_PLT32)
10242 && exp->X_op != O_symbol)
10243 {
10244 char c = *input_line_pointer;
10245 *input_line_pointer = 0;
10246 as_bad (_("invalid PLT expression `%s'"), save);
10247 *input_line_pointer = c;
10248 }
f3c180ae
AM
10249 }
10250 }
10251 else
10252 expression (exp);
ee86248c
JB
10253
10254 intel_syntax = -intel_syntax;
10255
10256 if (intel_syntax)
10257 i386_intel_simplify (exp);
62ebcb5c
AM
10258
10259 return got_reloc;
f3c180ae 10260}
f3c180ae 10261
9f32dd5b
L
10262static void
10263signed_cons (int size)
6482c264 10264{
d182319b
JB
10265 if (flag_code == CODE_64BIT)
10266 cons_sign = 1;
10267 cons (size);
10268 cons_sign = -1;
6482c264
NC
10269}
10270
d182319b 10271#ifdef TE_PE
6482c264 10272static void
7016a5d5 10273pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
6482c264
NC
10274{
10275 expressionS exp;
10276
10277 do
10278 {
10279 expression (&exp);
10280 if (exp.X_op == O_symbol)
10281 exp.X_op = O_secrel;
10282
10283 emit_expr (&exp, 4);
10284 }
10285 while (*input_line_pointer++ == ',');
10286
10287 input_line_pointer--;
10288 demand_empty_rest_of_line ();
10289}
6482c264
NC
10290#endif
10291
43234a1e
L
10292/* Handle Vector operations. */
10293
10294static char *
10295check_VecOperations (char *op_string, char *op_end)
10296{
10297 const reg_entry *mask;
10298 const char *saved;
10299 char *end_op;
10300
10301 while (*op_string
10302 && (op_end == NULL || op_string < op_end))
10303 {
10304 saved = op_string;
10305 if (*op_string == '{')
10306 {
10307 op_string++;
10308
10309 /* Check broadcasts. */
10310 if (strncmp (op_string, "1to", 3) == 0)
10311 {
10312 int bcst_type;
10313
10314 if (i.broadcast)
10315 goto duplicated_vec_op;
10316
10317 op_string += 3;
10318 if (*op_string == '8')
8e6e0792 10319 bcst_type = 8;
b28d1bda 10320 else if (*op_string == '4')
8e6e0792 10321 bcst_type = 4;
b28d1bda 10322 else if (*op_string == '2')
8e6e0792 10323 bcst_type = 2;
43234a1e
L
10324 else if (*op_string == '1'
10325 && *(op_string+1) == '6')
10326 {
8e6e0792 10327 bcst_type = 16;
43234a1e
L
10328 op_string++;
10329 }
10330 else
10331 {
10332 as_bad (_("Unsupported broadcast: `%s'"), saved);
10333 return NULL;
10334 }
10335 op_string++;
10336
10337 broadcast_op.type = bcst_type;
10338 broadcast_op.operand = this_operand;
1f75763a 10339 broadcast_op.bytes = 0;
43234a1e
L
10340 i.broadcast = &broadcast_op;
10341 }
10342 /* Check masking operation. */
10343 else if ((mask = parse_register (op_string, &end_op)) != NULL)
10344 {
8a6fb3f9
JB
10345 if (mask == &bad_reg)
10346 return NULL;
10347
43234a1e 10348 /* k0 can't be used for write mask. */
f74a6307 10349 if (mask->reg_type.bitfield.class != RegMask || !mask->reg_num)
43234a1e 10350 {
6d2cd6b2
JB
10351 as_bad (_("`%s%s' can't be used for write mask"),
10352 register_prefix, mask->reg_name);
43234a1e
L
10353 return NULL;
10354 }
10355
10356 if (!i.mask)
10357 {
10358 mask_op.mask = mask;
10359 mask_op.zeroing = 0;
10360 mask_op.operand = this_operand;
10361 i.mask = &mask_op;
10362 }
10363 else
10364 {
10365 if (i.mask->mask)
10366 goto duplicated_vec_op;
10367
10368 i.mask->mask = mask;
10369
10370 /* Only "{z}" is allowed here. No need to check
10371 zeroing mask explicitly. */
10372 if (i.mask->operand != this_operand)
10373 {
10374 as_bad (_("invalid write mask `%s'"), saved);
10375 return NULL;
10376 }
10377 }
10378
10379 op_string = end_op;
10380 }
10381 /* Check zeroing-flag for masking operation. */
10382 else if (*op_string == 'z')
10383 {
10384 if (!i.mask)
10385 {
10386 mask_op.mask = NULL;
10387 mask_op.zeroing = 1;
10388 mask_op.operand = this_operand;
10389 i.mask = &mask_op;
10390 }
10391 else
10392 {
10393 if (i.mask->zeroing)
10394 {
10395 duplicated_vec_op:
10396 as_bad (_("duplicated `%s'"), saved);
10397 return NULL;
10398 }
10399
10400 i.mask->zeroing = 1;
10401
10402 /* Only "{%k}" is allowed here. No need to check mask
10403 register explicitly. */
10404 if (i.mask->operand != this_operand)
10405 {
10406 as_bad (_("invalid zeroing-masking `%s'"),
10407 saved);
10408 return NULL;
10409 }
10410 }
10411
10412 op_string++;
10413 }
10414 else
10415 goto unknown_vec_op;
10416
10417 if (*op_string != '}')
10418 {
10419 as_bad (_("missing `}' in `%s'"), saved);
10420 return NULL;
10421 }
10422 op_string++;
0ba3a731
L
10423
10424 /* Strip whitespace since the addition of pseudo prefixes
10425 changed how the scrubber treats '{'. */
10426 if (is_space_char (*op_string))
10427 ++op_string;
10428
43234a1e
L
10429 continue;
10430 }
10431 unknown_vec_op:
10432 /* We don't know this one. */
10433 as_bad (_("unknown vector operation: `%s'"), saved);
10434 return NULL;
10435 }
10436
6d2cd6b2
JB
10437 if (i.mask && i.mask->zeroing && !i.mask->mask)
10438 {
10439 as_bad (_("zeroing-masking only allowed with write mask"));
10440 return NULL;
10441 }
10442
43234a1e
L
10443 return op_string;
10444}
10445
252b5132 10446static int
70e41ade 10447i386_immediate (char *imm_start)
252b5132
RH
10448{
10449 char *save_input_line_pointer;
f3c180ae 10450 char *gotfree_input_line;
252b5132 10451 segT exp_seg = 0;
47926f60 10452 expressionS *exp;
40fb9820
L
10453 i386_operand_type types;
10454
0dfbf9d7 10455 operand_type_set (&types, ~0);
252b5132
RH
10456
10457 if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
10458 {
31b2323c
L
10459 as_bad (_("at most %d immediate operands are allowed"),
10460 MAX_IMMEDIATE_OPERANDS);
252b5132
RH
10461 return 0;
10462 }
10463
10464 exp = &im_expressions[i.imm_operands++];
520dc8e8 10465 i.op[this_operand].imms = exp;
252b5132
RH
10466
10467 if (is_space_char (*imm_start))
10468 ++imm_start;
10469
10470 save_input_line_pointer = input_line_pointer;
10471 input_line_pointer = imm_start;
10472
d258b828 10473 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
10474 if (gotfree_input_line)
10475 input_line_pointer = gotfree_input_line;
252b5132
RH
10476
10477 exp_seg = expression (exp);
10478
83183c0c 10479 SKIP_WHITESPACE ();
43234a1e
L
10480
10481 /* Handle vector operations. */
10482 if (*input_line_pointer == '{')
10483 {
10484 input_line_pointer = check_VecOperations (input_line_pointer,
10485 NULL);
10486 if (input_line_pointer == NULL)
10487 return 0;
10488 }
10489
252b5132 10490 if (*input_line_pointer)
f3c180ae 10491 as_bad (_("junk `%s' after expression"), input_line_pointer);
252b5132
RH
10492
10493 input_line_pointer = save_input_line_pointer;
f3c180ae 10494 if (gotfree_input_line)
ee86248c
JB
10495 {
10496 free (gotfree_input_line);
10497
10498 if (exp->X_op == O_constant || exp->X_op == O_register)
10499 exp->X_op = O_illegal;
10500 }
10501
10502 return i386_finalize_immediate (exp_seg, exp, types, imm_start);
10503}
252b5132 10504
ee86248c
JB
10505static int
10506i386_finalize_immediate (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
10507 i386_operand_type types, const char *imm_start)
10508{
10509 if (exp->X_op == O_absent || exp->X_op == O_illegal || exp->X_op == O_big)
252b5132 10510 {
313c53d1
L
10511 if (imm_start)
10512 as_bad (_("missing or invalid immediate expression `%s'"),
10513 imm_start);
3992d3b7 10514 return 0;
252b5132 10515 }
3e73aa7c 10516 else if (exp->X_op == O_constant)
252b5132 10517 {
47926f60 10518 /* Size it properly later. */
40fb9820 10519 i.types[this_operand].bitfield.imm64 = 1;
13f864ae
L
10520 /* If not 64bit, sign extend val. */
10521 if (flag_code != CODE_64BIT
4eed87de
AM
10522 && (exp->X_add_number & ~(((addressT) 2 << 31) - 1)) == 0)
10523 exp->X_add_number
10524 = (exp->X_add_number ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
252b5132 10525 }
4c63da97 10526#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
f86103b7 10527 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
31312f95 10528 && exp_seg != absolute_section
47926f60 10529 && exp_seg != text_section
24eab124
AM
10530 && exp_seg != data_section
10531 && exp_seg != bss_section
10532 && exp_seg != undefined_section
f86103b7 10533 && !bfd_is_com_section (exp_seg))
252b5132 10534 {
d0b47220 10535 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
252b5132
RH
10536 return 0;
10537 }
10538#endif
a841bdf5 10539 else if (!intel_syntax && exp_seg == reg_section)
bb8f5920 10540 {
313c53d1
L
10541 if (imm_start)
10542 as_bad (_("illegal immediate register operand %s"), imm_start);
bb8f5920
L
10543 return 0;
10544 }
252b5132
RH
10545 else
10546 {
10547 /* This is an address. The size of the address will be
24eab124 10548 determined later, depending on destination register,
3e73aa7c 10549 suffix, or the default for the section. */
40fb9820
L
10550 i.types[this_operand].bitfield.imm8 = 1;
10551 i.types[this_operand].bitfield.imm16 = 1;
10552 i.types[this_operand].bitfield.imm32 = 1;
10553 i.types[this_operand].bitfield.imm32s = 1;
10554 i.types[this_operand].bitfield.imm64 = 1;
c6fb90c8
L
10555 i.types[this_operand] = operand_type_and (i.types[this_operand],
10556 types);
252b5132
RH
10557 }
10558
10559 return 1;
10560}
10561
551c1ca1 10562static char *
e3bb37b5 10563i386_scale (char *scale)
252b5132 10564{
551c1ca1
AM
10565 offsetT val;
10566 char *save = input_line_pointer;
252b5132 10567
551c1ca1
AM
10568 input_line_pointer = scale;
10569 val = get_absolute_expression ();
10570
10571 switch (val)
252b5132 10572 {
551c1ca1 10573 case 1:
252b5132
RH
10574 i.log2_scale_factor = 0;
10575 break;
551c1ca1 10576 case 2:
252b5132
RH
10577 i.log2_scale_factor = 1;
10578 break;
551c1ca1 10579 case 4:
252b5132
RH
10580 i.log2_scale_factor = 2;
10581 break;
551c1ca1 10582 case 8:
252b5132
RH
10583 i.log2_scale_factor = 3;
10584 break;
10585 default:
a724f0f4
JB
10586 {
10587 char sep = *input_line_pointer;
10588
10589 *input_line_pointer = '\0';
10590 as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
10591 scale);
10592 *input_line_pointer = sep;
10593 input_line_pointer = save;
10594 return NULL;
10595 }
252b5132 10596 }
29b0f896 10597 if (i.log2_scale_factor != 0 && i.index_reg == 0)
252b5132
RH
10598 {
10599 as_warn (_("scale factor of %d without an index register"),
24eab124 10600 1 << i.log2_scale_factor);
252b5132 10601 i.log2_scale_factor = 0;
252b5132 10602 }
551c1ca1
AM
10603 scale = input_line_pointer;
10604 input_line_pointer = save;
10605 return scale;
252b5132
RH
10606}
10607
252b5132 10608static int
e3bb37b5 10609i386_displacement (char *disp_start, char *disp_end)
252b5132 10610{
29b0f896 10611 expressionS *exp;
252b5132
RH
10612 segT exp_seg = 0;
10613 char *save_input_line_pointer;
f3c180ae 10614 char *gotfree_input_line;
40fb9820
L
10615 int override;
10616 i386_operand_type bigdisp, types = anydisp;
3992d3b7 10617 int ret;
252b5132 10618
31b2323c
L
10619 if (i.disp_operands == MAX_MEMORY_OPERANDS)
10620 {
10621 as_bad (_("at most %d displacement operands are allowed"),
10622 MAX_MEMORY_OPERANDS);
10623 return 0;
10624 }
10625
0dfbf9d7 10626 operand_type_set (&bigdisp, 0);
6f2f06be 10627 if (i.jumpabsolute
48bcea9f 10628 || i.types[this_operand].bitfield.baseindex
0cfa3eb3
JB
10629 || (current_templates->start->opcode_modifier.jump != JUMP
10630 && current_templates->start->opcode_modifier.jump != JUMP_DWORD))
e05278af 10631 {
48bcea9f 10632 i386_addressing_mode ();
e05278af 10633 override = (i.prefix[ADDR_PREFIX] != 0);
40fb9820
L
10634 if (flag_code == CODE_64BIT)
10635 {
10636 if (!override)
10637 {
10638 bigdisp.bitfield.disp32s = 1;
10639 bigdisp.bitfield.disp64 = 1;
10640 }
48bcea9f
JB
10641 else
10642 bigdisp.bitfield.disp32 = 1;
40fb9820
L
10643 }
10644 else if ((flag_code == CODE_16BIT) ^ override)
40fb9820 10645 bigdisp.bitfield.disp16 = 1;
48bcea9f
JB
10646 else
10647 bigdisp.bitfield.disp32 = 1;
e05278af
JB
10648 }
10649 else
10650 {
376cd056
JB
10651 /* For PC-relative branches, the width of the displacement may be
10652 dependent upon data size, but is never dependent upon address size.
10653 Also make sure to not unintentionally match against a non-PC-relative
10654 branch template. */
10655 static templates aux_templates;
10656 const insn_template *t = current_templates->start;
10657 bfd_boolean has_intel64 = FALSE;
10658
10659 aux_templates.start = t;
10660 while (++t < current_templates->end)
10661 {
10662 if (t->opcode_modifier.jump
10663 != current_templates->start->opcode_modifier.jump)
10664 break;
4b5aaf5f 10665 if ((t->opcode_modifier.isa64 >= INTEL64))
376cd056
JB
10666 has_intel64 = TRUE;
10667 }
10668 if (t < current_templates->end)
10669 {
10670 aux_templates.end = t;
10671 current_templates = &aux_templates;
10672 }
10673
e05278af 10674 override = (i.prefix[DATA_PREFIX] != 0);
40fb9820
L
10675 if (flag_code == CODE_64BIT)
10676 {
376cd056
JB
10677 if ((override || i.suffix == WORD_MNEM_SUFFIX)
10678 && (!intel64 || !has_intel64))
40fb9820
L
10679 bigdisp.bitfield.disp16 = 1;
10680 else
48bcea9f 10681 bigdisp.bitfield.disp32s = 1;
40fb9820
L
10682 }
10683 else
e05278af
JB
10684 {
10685 if (!override)
10686 override = (i.suffix == (flag_code != CODE_16BIT
10687 ? WORD_MNEM_SUFFIX
10688 : LONG_MNEM_SUFFIX));
40fb9820
L
10689 bigdisp.bitfield.disp32 = 1;
10690 if ((flag_code == CODE_16BIT) ^ override)
10691 {
10692 bigdisp.bitfield.disp32 = 0;
10693 bigdisp.bitfield.disp16 = 1;
10694 }
e05278af 10695 }
e05278af 10696 }
c6fb90c8
L
10697 i.types[this_operand] = operand_type_or (i.types[this_operand],
10698 bigdisp);
252b5132
RH
10699
10700 exp = &disp_expressions[i.disp_operands];
520dc8e8 10701 i.op[this_operand].disps = exp;
252b5132
RH
10702 i.disp_operands++;
10703 save_input_line_pointer = input_line_pointer;
10704 input_line_pointer = disp_start;
10705 END_STRING_AND_SAVE (disp_end);
10706
10707#ifndef GCC_ASM_O_HACK
10708#define GCC_ASM_O_HACK 0
10709#endif
10710#if GCC_ASM_O_HACK
10711 END_STRING_AND_SAVE (disp_end + 1);
40fb9820 10712 if (i.types[this_operand].bitfield.baseIndex
24eab124 10713 && displacement_string_end[-1] == '+')
252b5132
RH
10714 {
10715 /* This hack is to avoid a warning when using the "o"
24eab124
AM
10716 constraint within gcc asm statements.
10717 For instance:
10718
10719 #define _set_tssldt_desc(n,addr,limit,type) \
10720 __asm__ __volatile__ ( \
10721 "movw %w2,%0\n\t" \
10722 "movw %w1,2+%0\n\t" \
10723 "rorl $16,%1\n\t" \
10724 "movb %b1,4+%0\n\t" \
10725 "movb %4,5+%0\n\t" \
10726 "movb $0,6+%0\n\t" \
10727 "movb %h1,7+%0\n\t" \
10728 "rorl $16,%1" \
10729 : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
10730
10731 This works great except that the output assembler ends
10732 up looking a bit weird if it turns out that there is
10733 no offset. You end up producing code that looks like:
10734
10735 #APP
10736 movw $235,(%eax)
10737 movw %dx,2+(%eax)
10738 rorl $16,%edx
10739 movb %dl,4+(%eax)
10740 movb $137,5+(%eax)
10741 movb $0,6+(%eax)
10742 movb %dh,7+(%eax)
10743 rorl $16,%edx
10744 #NO_APP
10745
47926f60 10746 So here we provide the missing zero. */
24eab124
AM
10747
10748 *displacement_string_end = '0';
252b5132
RH
10749 }
10750#endif
d258b828 10751 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
10752 if (gotfree_input_line)
10753 input_line_pointer = gotfree_input_line;
252b5132 10754
24eab124 10755 exp_seg = expression (exp);
252b5132 10756
636c26b0
AM
10757 SKIP_WHITESPACE ();
10758 if (*input_line_pointer)
10759 as_bad (_("junk `%s' after expression"), input_line_pointer);
10760#if GCC_ASM_O_HACK
10761 RESTORE_END_STRING (disp_end + 1);
10762#endif
636c26b0 10763 input_line_pointer = save_input_line_pointer;
636c26b0 10764 if (gotfree_input_line)
ee86248c
JB
10765 {
10766 free (gotfree_input_line);
10767
10768 if (exp->X_op == O_constant || exp->X_op == O_register)
10769 exp->X_op = O_illegal;
10770 }
10771
10772 ret = i386_finalize_displacement (exp_seg, exp, types, disp_start);
10773
10774 RESTORE_END_STRING (disp_end);
10775
10776 return ret;
10777}
10778
10779static int
10780i386_finalize_displacement (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
10781 i386_operand_type types, const char *disp_start)
10782{
10783 i386_operand_type bigdisp;
10784 int ret = 1;
636c26b0 10785
24eab124
AM
10786 /* We do this to make sure that the section symbol is in
10787 the symbol table. We will ultimately change the relocation
47926f60 10788 to be relative to the beginning of the section. */
1ae12ab7 10789 if (i.reloc[this_operand] == BFD_RELOC_386_GOTOFF
d6ab8113
JB
10790 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
10791 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
24eab124 10792 {
636c26b0 10793 if (exp->X_op != O_symbol)
3992d3b7 10794 goto inv_disp;
636c26b0 10795
e5cb08ac 10796 if (S_IS_LOCAL (exp->X_add_symbol)
c64efb4b
L
10797 && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section
10798 && S_GET_SEGMENT (exp->X_add_symbol) != expr_section)
24eab124 10799 section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
24eab124
AM
10800 exp->X_op = O_subtract;
10801 exp->X_op_symbol = GOT_symbol;
1ae12ab7 10802 if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
29b0f896 10803 i.reloc[this_operand] = BFD_RELOC_32_PCREL;
d6ab8113
JB
10804 else if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
10805 i.reloc[this_operand] = BFD_RELOC_64;
23df1078 10806 else
29b0f896 10807 i.reloc[this_operand] = BFD_RELOC_32;
24eab124 10808 }
252b5132 10809
3992d3b7
AM
10810 else if (exp->X_op == O_absent
10811 || exp->X_op == O_illegal
ee86248c 10812 || exp->X_op == O_big)
2daf4fd8 10813 {
3992d3b7
AM
10814 inv_disp:
10815 as_bad (_("missing or invalid displacement expression `%s'"),
2daf4fd8 10816 disp_start);
3992d3b7 10817 ret = 0;
2daf4fd8
AM
10818 }
10819
0e1147d9
L
10820 else if (flag_code == CODE_64BIT
10821 && !i.prefix[ADDR_PREFIX]
10822 && exp->X_op == O_constant)
10823 {
10824 /* Since displacement is signed extended to 64bit, don't allow
10825 disp32 and turn off disp32s if they are out of range. */
10826 i.types[this_operand].bitfield.disp32 = 0;
10827 if (!fits_in_signed_long (exp->X_add_number))
10828 {
10829 i.types[this_operand].bitfield.disp32s = 0;
10830 if (i.types[this_operand].bitfield.baseindex)
10831 {
10832 as_bad (_("0x%lx out range of signed 32bit displacement"),
10833 (long) exp->X_add_number);
10834 ret = 0;
10835 }
10836 }
10837 }
10838
4c63da97 10839#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
3992d3b7
AM
10840 else if (exp->X_op != O_constant
10841 && OUTPUT_FLAVOR == bfd_target_aout_flavour
10842 && exp_seg != absolute_section
10843 && exp_seg != text_section
10844 && exp_seg != data_section
10845 && exp_seg != bss_section
10846 && exp_seg != undefined_section
10847 && !bfd_is_com_section (exp_seg))
24eab124 10848 {
d0b47220 10849 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
3992d3b7 10850 ret = 0;
24eab124 10851 }
252b5132 10852#endif
3956db08 10853
48bcea9f
JB
10854 if (current_templates->start->opcode_modifier.jump == JUMP_BYTE
10855 /* Constants get taken care of by optimize_disp(). */
10856 && exp->X_op != O_constant)
10857 i.types[this_operand].bitfield.disp8 = 1;
10858
40fb9820
L
10859 /* Check if this is a displacement only operand. */
10860 bigdisp = i.types[this_operand];
10861 bigdisp.bitfield.disp8 = 0;
10862 bigdisp.bitfield.disp16 = 0;
10863 bigdisp.bitfield.disp32 = 0;
10864 bigdisp.bitfield.disp32s = 0;
10865 bigdisp.bitfield.disp64 = 0;
0dfbf9d7 10866 if (operand_type_all_zero (&bigdisp))
c6fb90c8
L
10867 i.types[this_operand] = operand_type_and (i.types[this_operand],
10868 types);
3956db08 10869
3992d3b7 10870 return ret;
252b5132
RH
10871}
10872
2abc2bec
JB
10873/* Return the active addressing mode, taking address override and
10874 registers forming the address into consideration. Update the
10875 address override prefix if necessary. */
47926f60 10876
2abc2bec
JB
10877static enum flag_code
10878i386_addressing_mode (void)
252b5132 10879{
be05d201
L
10880 enum flag_code addr_mode;
10881
10882 if (i.prefix[ADDR_PREFIX])
10883 addr_mode = flag_code == CODE_32BIT ? CODE_16BIT : CODE_32BIT;
a23b33b3
JB
10884 else if (flag_code == CODE_16BIT
10885 && current_templates->start->cpu_flags.bitfield.cpumpx
10886 /* Avoid replacing the "16-bit addressing not allowed" diagnostic
10887 from md_assemble() by "is not a valid base/index expression"
10888 when there is a base and/or index. */
10889 && !i.types[this_operand].bitfield.baseindex)
10890 {
10891 /* MPX insn memory operands with neither base nor index must be forced
10892 to use 32-bit addressing in 16-bit mode. */
10893 addr_mode = CODE_32BIT;
10894 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
10895 ++i.prefixes;
10896 gas_assert (!i.types[this_operand].bitfield.disp16);
10897 gas_assert (!i.types[this_operand].bitfield.disp32);
10898 }
be05d201
L
10899 else
10900 {
10901 addr_mode = flag_code;
10902
24eab124 10903#if INFER_ADDR_PREFIX
be05d201
L
10904 if (i.mem_operands == 0)
10905 {
10906 /* Infer address prefix from the first memory operand. */
10907 const reg_entry *addr_reg = i.base_reg;
10908
10909 if (addr_reg == NULL)
10910 addr_reg = i.index_reg;
eecb386c 10911
be05d201
L
10912 if (addr_reg)
10913 {
e968fc9b 10914 if (addr_reg->reg_type.bitfield.dword)
be05d201
L
10915 addr_mode = CODE_32BIT;
10916 else if (flag_code != CODE_64BIT
dc821c5f 10917 && addr_reg->reg_type.bitfield.word)
be05d201
L
10918 addr_mode = CODE_16BIT;
10919
10920 if (addr_mode != flag_code)
10921 {
10922 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
10923 i.prefixes += 1;
10924 /* Change the size of any displacement too. At most one
10925 of Disp16 or Disp32 is set.
10926 FIXME. There doesn't seem to be any real need for
10927 separate Disp16 and Disp32 flags. The same goes for
10928 Imm16 and Imm32. Removing them would probably clean
10929 up the code quite a lot. */
10930 if (flag_code != CODE_64BIT
10931 && (i.types[this_operand].bitfield.disp16
10932 || i.types[this_operand].bitfield.disp32))
10933 i.types[this_operand]
10934 = operand_type_xor (i.types[this_operand], disp16_32);
10935 }
10936 }
10937 }
24eab124 10938#endif
be05d201
L
10939 }
10940
2abc2bec
JB
10941 return addr_mode;
10942}
10943
10944/* Make sure the memory operand we've been dealt is valid.
10945 Return 1 on success, 0 on a failure. */
10946
10947static int
10948i386_index_check (const char *operand_string)
10949{
10950 const char *kind = "base/index";
10951 enum flag_code addr_mode = i386_addressing_mode ();
10952
fc0763e6 10953 if (current_templates->start->opcode_modifier.isstring
c3949f43 10954 && !current_templates->start->cpu_flags.bitfield.cpupadlock
fc0763e6
JB
10955 && (current_templates->end[-1].opcode_modifier.isstring
10956 || i.mem_operands))
10957 {
10958 /* Memory operands of string insns are special in that they only allow
10959 a single register (rDI, rSI, or rBX) as their memory address. */
be05d201
L
10960 const reg_entry *expected_reg;
10961 static const char *di_si[][2] =
10962 {
10963 { "esi", "edi" },
10964 { "si", "di" },
10965 { "rsi", "rdi" }
10966 };
10967 static const char *bx[] = { "ebx", "bx", "rbx" };
fc0763e6
JB
10968
10969 kind = "string address";
10970
8325cc63 10971 if (current_templates->start->opcode_modifier.repprefixok)
fc0763e6 10972 {
51c8edf6
JB
10973 int es_op = current_templates->end[-1].opcode_modifier.isstring
10974 - IS_STRING_ES_OP0;
10975 int op = 0;
fc0763e6 10976
51c8edf6 10977 if (!current_templates->end[-1].operand_types[0].bitfield.baseindex
fc0763e6
JB
10978 || ((!i.mem_operands != !intel_syntax)
10979 && current_templates->end[-1].operand_types[1]
10980 .bitfield.baseindex))
51c8edf6 10981 op = 1;
fe0e921f
AM
10982 expected_reg
10983 = (const reg_entry *) str_hash_find (reg_hash,
10984 di_si[addr_mode][op == es_op]);
fc0763e6
JB
10985 }
10986 else
fe0e921f
AM
10987 expected_reg
10988 = (const reg_entry *)str_hash_find (reg_hash, bx[addr_mode]);
fc0763e6 10989
be05d201
L
10990 if (i.base_reg != expected_reg
10991 || i.index_reg
fc0763e6 10992 || operand_type_check (i.types[this_operand], disp))
fc0763e6 10993 {
be05d201
L
10994 /* The second memory operand must have the same size as
10995 the first one. */
10996 if (i.mem_operands
10997 && i.base_reg
10998 && !((addr_mode == CODE_64BIT
dc821c5f 10999 && i.base_reg->reg_type.bitfield.qword)
be05d201 11000 || (addr_mode == CODE_32BIT
dc821c5f
JB
11001 ? i.base_reg->reg_type.bitfield.dword
11002 : i.base_reg->reg_type.bitfield.word)))
be05d201
L
11003 goto bad_address;
11004
fc0763e6
JB
11005 as_warn (_("`%s' is not valid here (expected `%c%s%s%c')"),
11006 operand_string,
11007 intel_syntax ? '[' : '(',
11008 register_prefix,
be05d201 11009 expected_reg->reg_name,
fc0763e6 11010 intel_syntax ? ']' : ')');
be05d201 11011 return 1;
fc0763e6 11012 }
be05d201
L
11013 else
11014 return 1;
11015
dc1e8a47 11016 bad_address:
be05d201
L
11017 as_bad (_("`%s' is not a valid %s expression"),
11018 operand_string, kind);
11019 return 0;
3e73aa7c
JH
11020 }
11021 else
11022 {
be05d201
L
11023 if (addr_mode != CODE_16BIT)
11024 {
11025 /* 32-bit/64-bit checks. */
41eb8e88
L
11026 if (i.disp_encoding == disp_encoding_16bit)
11027 {
11028 bad_disp:
11029 as_bad (_("invalid `%s' prefix"),
11030 addr_mode == CODE_16BIT ? "{disp32}" : "{disp16}");
11031 return 0;
11032 }
11033
be05d201 11034 if ((i.base_reg
e968fc9b
JB
11035 && ((addr_mode == CODE_64BIT
11036 ? !i.base_reg->reg_type.bitfield.qword
11037 : !i.base_reg->reg_type.bitfield.dword)
11038 || (i.index_reg && i.base_reg->reg_num == RegIP)
11039 || i.base_reg->reg_num == RegIZ))
be05d201 11040 || (i.index_reg
1b54b8d7
JB
11041 && !i.index_reg->reg_type.bitfield.xmmword
11042 && !i.index_reg->reg_type.bitfield.ymmword
11043 && !i.index_reg->reg_type.bitfield.zmmword
be05d201 11044 && ((addr_mode == CODE_64BIT
e968fc9b
JB
11045 ? !i.index_reg->reg_type.bitfield.qword
11046 : !i.index_reg->reg_type.bitfield.dword)
be05d201
L
11047 || !i.index_reg->reg_type.bitfield.baseindex)))
11048 goto bad_address;
8178be5b 11049
260cd341 11050 /* bndmk, bndldx, bndstx and mandatory non-vector SIB have special restrictions. */
8178be5b 11051 if (current_templates->start->base_opcode == 0xf30f1b
260cd341
LC
11052 || (current_templates->start->base_opcode & ~1) == 0x0f1a
11053 || current_templates->start->opcode_modifier.sib == SIBMEM)
8178be5b
JB
11054 {
11055 /* They cannot use RIP-relative addressing. */
e968fc9b 11056 if (i.base_reg && i.base_reg->reg_num == RegIP)
8178be5b
JB
11057 {
11058 as_bad (_("`%s' cannot be used here"), operand_string);
11059 return 0;
11060 }
11061
11062 /* bndldx and bndstx ignore their scale factor. */
260cd341 11063 if ((current_templates->start->base_opcode & ~1) == 0x0f1a
8178be5b
JB
11064 && i.log2_scale_factor)
11065 as_warn (_("register scaling is being ignored here"));
11066 }
be05d201
L
11067 }
11068 else
3e73aa7c 11069 {
be05d201 11070 /* 16-bit checks. */
41eb8e88
L
11071 if (i.disp_encoding == disp_encoding_32bit)
11072 goto bad_disp;
11073
3e73aa7c 11074 if ((i.base_reg
dc821c5f 11075 && (!i.base_reg->reg_type.bitfield.word
40fb9820 11076 || !i.base_reg->reg_type.bitfield.baseindex))
3e73aa7c 11077 || (i.index_reg
dc821c5f 11078 && (!i.index_reg->reg_type.bitfield.word
40fb9820 11079 || !i.index_reg->reg_type.bitfield.baseindex
29b0f896
AM
11080 || !(i.base_reg
11081 && i.base_reg->reg_num < 6
11082 && i.index_reg->reg_num >= 6
11083 && i.log2_scale_factor == 0))))
be05d201 11084 goto bad_address;
3e73aa7c
JH
11085 }
11086 }
be05d201 11087 return 1;
24eab124 11088}
252b5132 11089
43234a1e
L
11090/* Handle vector immediates. */
11091
11092static int
11093RC_SAE_immediate (const char *imm_start)
11094{
11095 unsigned int match_found, j;
11096 const char *pstr = imm_start;
11097 expressionS *exp;
11098
11099 if (*pstr != '{')
11100 return 0;
11101
11102 pstr++;
11103 match_found = 0;
11104 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); j++)
11105 {
11106 if (!strncmp (pstr, RC_NamesTable[j].name, RC_NamesTable[j].len))
11107 {
11108 if (!i.rounding)
11109 {
11110 rc_op.type = RC_NamesTable[j].type;
11111 rc_op.operand = this_operand;
11112 i.rounding = &rc_op;
11113 }
11114 else
11115 {
11116 as_bad (_("duplicated `%s'"), imm_start);
11117 return 0;
11118 }
11119 pstr += RC_NamesTable[j].len;
11120 match_found = 1;
11121 break;
11122 }
11123 }
11124 if (!match_found)
11125 return 0;
11126
11127 if (*pstr++ != '}')
11128 {
11129 as_bad (_("Missing '}': '%s'"), imm_start);
11130 return 0;
11131 }
11132 /* RC/SAE immediate string should contain nothing more. */;
11133 if (*pstr != 0)
11134 {
11135 as_bad (_("Junk after '}': '%s'"), imm_start);
11136 return 0;
11137 }
11138
11139 exp = &im_expressions[i.imm_operands++];
11140 i.op[this_operand].imms = exp;
11141
11142 exp->X_op = O_constant;
11143 exp->X_add_number = 0;
11144 exp->X_add_symbol = (symbolS *) 0;
11145 exp->X_op_symbol = (symbolS *) 0;
11146
11147 i.types[this_operand].bitfield.imm8 = 1;
11148 return 1;
11149}
11150
8325cc63
JB
11151/* Only string instructions can have a second memory operand, so
11152 reduce current_templates to just those if it contains any. */
11153static int
11154maybe_adjust_templates (void)
11155{
11156 const insn_template *t;
11157
11158 gas_assert (i.mem_operands == 1);
11159
11160 for (t = current_templates->start; t < current_templates->end; ++t)
11161 if (t->opcode_modifier.isstring)
11162 break;
11163
11164 if (t < current_templates->end)
11165 {
11166 static templates aux_templates;
11167 bfd_boolean recheck;
11168
11169 aux_templates.start = t;
11170 for (; t < current_templates->end; ++t)
11171 if (!t->opcode_modifier.isstring)
11172 break;
11173 aux_templates.end = t;
11174
11175 /* Determine whether to re-check the first memory operand. */
11176 recheck = (aux_templates.start != current_templates->start
11177 || t != current_templates->end);
11178
11179 current_templates = &aux_templates;
11180
11181 if (recheck)
11182 {
11183 i.mem_operands = 0;
11184 if (i.memop1_string != NULL
11185 && i386_index_check (i.memop1_string) == 0)
11186 return 0;
11187 i.mem_operands = 1;
11188 }
11189 }
11190
11191 return 1;
11192}
11193
fc0763e6 11194/* Parse OPERAND_STRING into the i386_insn structure I. Returns zero
47926f60 11195 on error. */
252b5132 11196
252b5132 11197static int
a7619375 11198i386_att_operand (char *operand_string)
252b5132 11199{
af6bdddf
AM
11200 const reg_entry *r;
11201 char *end_op;
24eab124 11202 char *op_string = operand_string;
252b5132 11203
24eab124 11204 if (is_space_char (*op_string))
252b5132
RH
11205 ++op_string;
11206
24eab124 11207 /* We check for an absolute prefix (differentiating,
47926f60 11208 for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
24eab124
AM
11209 if (*op_string == ABSOLUTE_PREFIX)
11210 {
11211 ++op_string;
11212 if (is_space_char (*op_string))
11213 ++op_string;
6f2f06be 11214 i.jumpabsolute = TRUE;
24eab124 11215 }
252b5132 11216
47926f60 11217 /* Check if operand is a register. */
4d1bb795 11218 if ((r = parse_register (op_string, &end_op)) != NULL)
24eab124 11219 {
40fb9820
L
11220 i386_operand_type temp;
11221
8a6fb3f9
JB
11222 if (r == &bad_reg)
11223 return 0;
11224
24eab124
AM
11225 /* Check for a segment override by searching for ':' after a
11226 segment register. */
11227 op_string = end_op;
11228 if (is_space_char (*op_string))
11229 ++op_string;
00cee14f 11230 if (*op_string == ':' && r->reg_type.bitfield.class == SReg)
24eab124
AM
11231 {
11232 switch (r->reg_num)
11233 {
11234 case 0:
11235 i.seg[i.mem_operands] = &es;
11236 break;
11237 case 1:
11238 i.seg[i.mem_operands] = &cs;
11239 break;
11240 case 2:
11241 i.seg[i.mem_operands] = &ss;
11242 break;
11243 case 3:
11244 i.seg[i.mem_operands] = &ds;
11245 break;
11246 case 4:
11247 i.seg[i.mem_operands] = &fs;
11248 break;
11249 case 5:
11250 i.seg[i.mem_operands] = &gs;
11251 break;
11252 }
252b5132 11253
24eab124 11254 /* Skip the ':' and whitespace. */
252b5132
RH
11255 ++op_string;
11256 if (is_space_char (*op_string))
24eab124 11257 ++op_string;
252b5132 11258
24eab124
AM
11259 if (!is_digit_char (*op_string)
11260 && !is_identifier_char (*op_string)
11261 && *op_string != '('
11262 && *op_string != ABSOLUTE_PREFIX)
11263 {
11264 as_bad (_("bad memory operand `%s'"), op_string);
11265 return 0;
11266 }
47926f60 11267 /* Handle case of %es:*foo. */
24eab124
AM
11268 if (*op_string == ABSOLUTE_PREFIX)
11269 {
11270 ++op_string;
11271 if (is_space_char (*op_string))
11272 ++op_string;
6f2f06be 11273 i.jumpabsolute = TRUE;
24eab124
AM
11274 }
11275 goto do_memory_reference;
11276 }
43234a1e
L
11277
11278 /* Handle vector operations. */
11279 if (*op_string == '{')
11280 {
11281 op_string = check_VecOperations (op_string, NULL);
11282 if (op_string == NULL)
11283 return 0;
11284 }
11285
24eab124
AM
11286 if (*op_string)
11287 {
d0b47220 11288 as_bad (_("junk `%s' after register"), op_string);
24eab124
AM
11289 return 0;
11290 }
40fb9820
L
11291 temp = r->reg_type;
11292 temp.bitfield.baseindex = 0;
c6fb90c8
L
11293 i.types[this_operand] = operand_type_or (i.types[this_operand],
11294 temp);
7d5e4556 11295 i.types[this_operand].bitfield.unspecified = 0;
520dc8e8 11296 i.op[this_operand].regs = r;
24eab124
AM
11297 i.reg_operands++;
11298 }
af6bdddf
AM
11299 else if (*op_string == REGISTER_PREFIX)
11300 {
11301 as_bad (_("bad register name `%s'"), op_string);
11302 return 0;
11303 }
24eab124 11304 else if (*op_string == IMMEDIATE_PREFIX)
ce8a8b2f 11305 {
24eab124 11306 ++op_string;
6f2f06be 11307 if (i.jumpabsolute)
24eab124 11308 {
d0b47220 11309 as_bad (_("immediate operand illegal with absolute jump"));
24eab124
AM
11310 return 0;
11311 }
11312 if (!i386_immediate (op_string))
11313 return 0;
11314 }
43234a1e
L
11315 else if (RC_SAE_immediate (operand_string))
11316 {
11317 /* If it is a RC or SAE immediate, do nothing. */
11318 ;
11319 }
24eab124
AM
11320 else if (is_digit_char (*op_string)
11321 || is_identifier_char (*op_string)
d02603dc 11322 || *op_string == '"'
e5cb08ac 11323 || *op_string == '(')
24eab124 11324 {
47926f60 11325 /* This is a memory reference of some sort. */
af6bdddf 11326 char *base_string;
252b5132 11327
47926f60 11328 /* Start and end of displacement string expression (if found). */
eecb386c
AM
11329 char *displacement_string_start;
11330 char *displacement_string_end;
43234a1e 11331 char *vop_start;
252b5132 11332
24eab124 11333 do_memory_reference:
8325cc63
JB
11334 if (i.mem_operands == 1 && !maybe_adjust_templates ())
11335 return 0;
24eab124 11336 if ((i.mem_operands == 1
40fb9820 11337 && !current_templates->start->opcode_modifier.isstring)
24eab124
AM
11338 || i.mem_operands == 2)
11339 {
11340 as_bad (_("too many memory references for `%s'"),
11341 current_templates->start->name);
11342 return 0;
11343 }
252b5132 11344
24eab124
AM
11345 /* Check for base index form. We detect the base index form by
11346 looking for an ')' at the end of the operand, searching
11347 for the '(' matching it, and finding a REGISTER_PREFIX or ','
11348 after the '('. */
af6bdddf 11349 base_string = op_string + strlen (op_string);
c3332e24 11350
43234a1e
L
11351 /* Handle vector operations. */
11352 vop_start = strchr (op_string, '{');
11353 if (vop_start && vop_start < base_string)
11354 {
11355 if (check_VecOperations (vop_start, base_string) == NULL)
11356 return 0;
11357 base_string = vop_start;
11358 }
11359
af6bdddf
AM
11360 --base_string;
11361 if (is_space_char (*base_string))
11362 --base_string;
252b5132 11363
47926f60 11364 /* If we only have a displacement, set-up for it to be parsed later. */
af6bdddf
AM
11365 displacement_string_start = op_string;
11366 displacement_string_end = base_string + 1;
252b5132 11367
24eab124
AM
11368 if (*base_string == ')')
11369 {
af6bdddf 11370 char *temp_string;
24eab124
AM
11371 unsigned int parens_balanced = 1;
11372 /* We've already checked that the number of left & right ()'s are
47926f60 11373 equal, so this loop will not be infinite. */
24eab124
AM
11374 do
11375 {
11376 base_string--;
11377 if (*base_string == ')')
11378 parens_balanced++;
11379 if (*base_string == '(')
11380 parens_balanced--;
11381 }
11382 while (parens_balanced);
c3332e24 11383
af6bdddf 11384 temp_string = base_string;
c3332e24 11385
24eab124 11386 /* Skip past '(' and whitespace. */
252b5132
RH
11387 ++base_string;
11388 if (is_space_char (*base_string))
24eab124 11389 ++base_string;
252b5132 11390
af6bdddf 11391 if (*base_string == ','
4eed87de
AM
11392 || ((i.base_reg = parse_register (base_string, &end_op))
11393 != NULL))
252b5132 11394 {
af6bdddf 11395 displacement_string_end = temp_string;
252b5132 11396
40fb9820 11397 i.types[this_operand].bitfield.baseindex = 1;
252b5132 11398
af6bdddf 11399 if (i.base_reg)
24eab124 11400 {
8a6fb3f9
JB
11401 if (i.base_reg == &bad_reg)
11402 return 0;
24eab124
AM
11403 base_string = end_op;
11404 if (is_space_char (*base_string))
11405 ++base_string;
af6bdddf
AM
11406 }
11407
11408 /* There may be an index reg or scale factor here. */
11409 if (*base_string == ',')
11410 {
11411 ++base_string;
11412 if (is_space_char (*base_string))
11413 ++base_string;
11414
4eed87de
AM
11415 if ((i.index_reg = parse_register (base_string, &end_op))
11416 != NULL)
24eab124 11417 {
8a6fb3f9
JB
11418 if (i.index_reg == &bad_reg)
11419 return 0;
af6bdddf 11420 base_string = end_op;
24eab124
AM
11421 if (is_space_char (*base_string))
11422 ++base_string;
af6bdddf
AM
11423 if (*base_string == ',')
11424 {
11425 ++base_string;
11426 if (is_space_char (*base_string))
11427 ++base_string;
11428 }
e5cb08ac 11429 else if (*base_string != ')')
af6bdddf 11430 {
4eed87de
AM
11431 as_bad (_("expecting `,' or `)' "
11432 "after index register in `%s'"),
af6bdddf
AM
11433 operand_string);
11434 return 0;
11435 }
24eab124 11436 }
af6bdddf 11437 else if (*base_string == REGISTER_PREFIX)
24eab124 11438 {
f76bf5e0
L
11439 end_op = strchr (base_string, ',');
11440 if (end_op)
11441 *end_op = '\0';
af6bdddf 11442 as_bad (_("bad register name `%s'"), base_string);
24eab124
AM
11443 return 0;
11444 }
252b5132 11445
47926f60 11446 /* Check for scale factor. */
551c1ca1 11447 if (*base_string != ')')
af6bdddf 11448 {
551c1ca1
AM
11449 char *end_scale = i386_scale (base_string);
11450
11451 if (!end_scale)
af6bdddf 11452 return 0;
24eab124 11453
551c1ca1 11454 base_string = end_scale;
af6bdddf
AM
11455 if (is_space_char (*base_string))
11456 ++base_string;
11457 if (*base_string != ')')
11458 {
4eed87de
AM
11459 as_bad (_("expecting `)' "
11460 "after scale factor in `%s'"),
af6bdddf
AM
11461 operand_string);
11462 return 0;
11463 }
11464 }
11465 else if (!i.index_reg)
24eab124 11466 {
4eed87de
AM
11467 as_bad (_("expecting index register or scale factor "
11468 "after `,'; got '%c'"),
af6bdddf 11469 *base_string);
24eab124
AM
11470 return 0;
11471 }
11472 }
af6bdddf 11473 else if (*base_string != ')')
24eab124 11474 {
4eed87de
AM
11475 as_bad (_("expecting `,' or `)' "
11476 "after base register in `%s'"),
af6bdddf 11477 operand_string);
24eab124
AM
11478 return 0;
11479 }
c3332e24 11480 }
af6bdddf 11481 else if (*base_string == REGISTER_PREFIX)
c3332e24 11482 {
f76bf5e0
L
11483 end_op = strchr (base_string, ',');
11484 if (end_op)
11485 *end_op = '\0';
af6bdddf 11486 as_bad (_("bad register name `%s'"), base_string);
24eab124 11487 return 0;
c3332e24 11488 }
24eab124
AM
11489 }
11490
11491 /* If there's an expression beginning the operand, parse it,
11492 assuming displacement_string_start and
11493 displacement_string_end are meaningful. */
11494 if (displacement_string_start != displacement_string_end)
11495 {
11496 if (!i386_displacement (displacement_string_start,
11497 displacement_string_end))
11498 return 0;
11499 }
11500
11501 /* Special case for (%dx) while doing input/output op. */
11502 if (i.base_reg
75e5731b
JB
11503 && i.base_reg->reg_type.bitfield.instance == RegD
11504 && i.base_reg->reg_type.bitfield.word
24eab124
AM
11505 && i.index_reg == 0
11506 && i.log2_scale_factor == 0
11507 && i.seg[i.mem_operands] == 0
40fb9820 11508 && !operand_type_check (i.types[this_operand], disp))
24eab124 11509 {
2fb5be8d 11510 i.types[this_operand] = i.base_reg->reg_type;
24eab124
AM
11511 return 1;
11512 }
11513
eecb386c
AM
11514 if (i386_index_check (operand_string) == 0)
11515 return 0;
c48dadc9 11516 i.flags[this_operand] |= Operand_Mem;
8325cc63
JB
11517 if (i.mem_operands == 0)
11518 i.memop1_string = xstrdup (operand_string);
24eab124
AM
11519 i.mem_operands++;
11520 }
11521 else
ce8a8b2f
AM
11522 {
11523 /* It's not a memory operand; argh! */
24eab124
AM
11524 as_bad (_("invalid char %s beginning operand %d `%s'"),
11525 output_invalid (*op_string),
11526 this_operand + 1,
11527 op_string);
11528 return 0;
11529 }
47926f60 11530 return 1; /* Normal return. */
252b5132
RH
11531}
11532\f
fa94de6b
RM
11533/* Calculate the maximum variable size (i.e., excluding fr_fix)
11534 that an rs_machine_dependent frag may reach. */
11535
11536unsigned int
11537i386_frag_max_var (fragS *frag)
11538{
11539 /* The only relaxable frags are for jumps.
11540 Unconditional jumps can grow by 4 bytes and others by 5 bytes. */
11541 gas_assert (frag->fr_type == rs_machine_dependent);
11542 return TYPE_FROM_RELAX_STATE (frag->fr_subtype) == UNCOND_JUMP ? 4 : 5;
11543}
11544
b084df0b
L
11545#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
11546static int
8dcea932 11547elf_symbol_resolved_in_segment_p (symbolS *fr_symbol, offsetT fr_var)
b084df0b
L
11548{
11549 /* STT_GNU_IFUNC symbol must go through PLT. */
11550 if ((symbol_get_bfdsym (fr_symbol)->flags
11551 & BSF_GNU_INDIRECT_FUNCTION) != 0)
11552 return 0;
11553
11554 if (!S_IS_EXTERNAL (fr_symbol))
11555 /* Symbol may be weak or local. */
11556 return !S_IS_WEAK (fr_symbol);
11557
8dcea932
L
11558 /* Global symbols with non-default visibility can't be preempted. */
11559 if (ELF_ST_VISIBILITY (S_GET_OTHER (fr_symbol)) != STV_DEFAULT)
11560 return 1;
11561
11562 if (fr_var != NO_RELOC)
11563 switch ((enum bfd_reloc_code_real) fr_var)
11564 {
11565 case BFD_RELOC_386_PLT32:
11566 case BFD_RELOC_X86_64_PLT32:
33eaf5de 11567 /* Symbol with PLT relocation may be preempted. */
8dcea932
L
11568 return 0;
11569 default:
11570 abort ();
11571 }
11572
b084df0b
L
11573 /* Global symbols with default visibility in a shared library may be
11574 preempted by another definition. */
8dcea932 11575 return !shared;
b084df0b
L
11576}
11577#endif
11578
79d72f45
HL
11579/* Table 3-2. Macro-Fusible Instructions in Haswell Microarchitecture
11580 Note also work for Skylake and Cascadelake.
11581---------------------------------------------------------------------
11582| JCC | ADD/SUB/CMP | INC/DEC | TEST/AND |
11583| ------ | ----------- | ------- | -------- |
11584| Jo | N | N | Y |
11585| Jno | N | N | Y |
11586| Jc/Jb | Y | N | Y |
11587| Jae/Jnb | Y | N | Y |
11588| Je/Jz | Y | Y | Y |
11589| Jne/Jnz | Y | Y | Y |
11590| Jna/Jbe | Y | N | Y |
11591| Ja/Jnbe | Y | N | Y |
11592| Js | N | N | Y |
11593| Jns | N | N | Y |
11594| Jp/Jpe | N | N | Y |
11595| Jnp/Jpo | N | N | Y |
11596| Jl/Jnge | Y | Y | Y |
11597| Jge/Jnl | Y | Y | Y |
11598| Jle/Jng | Y | Y | Y |
11599| Jg/Jnle | Y | Y | Y |
11600--------------------------------------------------------------------- */
11601static int
11602i386_macro_fusible_p (enum mf_cmp_kind mf_cmp, enum mf_jcc_kind mf_jcc)
11603{
11604 if (mf_cmp == mf_cmp_alu_cmp)
11605 return ((mf_jcc >= mf_jcc_jc && mf_jcc <= mf_jcc_jna)
11606 || mf_jcc == mf_jcc_jl || mf_jcc == mf_jcc_jle);
11607 if (mf_cmp == mf_cmp_incdec)
11608 return (mf_jcc == mf_jcc_je || mf_jcc == mf_jcc_jl
11609 || mf_jcc == mf_jcc_jle);
11610 if (mf_cmp == mf_cmp_test_and)
11611 return 1;
11612 return 0;
11613}
11614
e379e5f3
L
11615/* Return the next non-empty frag. */
11616
11617static fragS *
11618i386_next_non_empty_frag (fragS *fragP)
11619{
11620 /* There may be a frag with a ".fill 0" when there is no room in
11621 the current frag for frag_grow in output_insn. */
11622 for (fragP = fragP->fr_next;
11623 (fragP != NULL
11624 && fragP->fr_type == rs_fill
11625 && fragP->fr_fix == 0);
11626 fragP = fragP->fr_next)
11627 ;
11628 return fragP;
11629}
11630
11631/* Return the next jcc frag after BRANCH_PADDING. */
11632
11633static fragS *
79d72f45 11634i386_next_fusible_jcc_frag (fragS *maybe_cmp_fragP, fragS *pad_fragP)
e379e5f3 11635{
79d72f45
HL
11636 fragS *branch_fragP;
11637 if (!pad_fragP)
e379e5f3
L
11638 return NULL;
11639
79d72f45
HL
11640 if (pad_fragP->fr_type == rs_machine_dependent
11641 && (TYPE_FROM_RELAX_STATE (pad_fragP->fr_subtype)
e379e5f3
L
11642 == BRANCH_PADDING))
11643 {
79d72f45
HL
11644 branch_fragP = i386_next_non_empty_frag (pad_fragP);
11645 if (branch_fragP->fr_type != rs_machine_dependent)
e379e5f3 11646 return NULL;
79d72f45
HL
11647 if (TYPE_FROM_RELAX_STATE (branch_fragP->fr_subtype) == COND_JUMP
11648 && i386_macro_fusible_p (maybe_cmp_fragP->tc_frag_data.mf_type,
11649 pad_fragP->tc_frag_data.mf_type))
11650 return branch_fragP;
e379e5f3
L
11651 }
11652
11653 return NULL;
11654}
11655
11656/* Classify BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags. */
11657
11658static void
11659i386_classify_machine_dependent_frag (fragS *fragP)
11660{
11661 fragS *cmp_fragP;
11662 fragS *pad_fragP;
11663 fragS *branch_fragP;
11664 fragS *next_fragP;
11665 unsigned int max_prefix_length;
11666
11667 if (fragP->tc_frag_data.classified)
11668 return;
11669
11670 /* First scan for BRANCH_PADDING and FUSED_JCC_PADDING. Convert
11671 FUSED_JCC_PADDING and merge BRANCH_PADDING. */
11672 for (next_fragP = fragP;
11673 next_fragP != NULL;
11674 next_fragP = next_fragP->fr_next)
11675 {
11676 next_fragP->tc_frag_data.classified = 1;
11677 if (next_fragP->fr_type == rs_machine_dependent)
11678 switch (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype))
11679 {
11680 case BRANCH_PADDING:
11681 /* The BRANCH_PADDING frag must be followed by a branch
11682 frag. */
11683 branch_fragP = i386_next_non_empty_frag (next_fragP);
11684 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
11685 break;
11686 case FUSED_JCC_PADDING:
11687 /* Check if this is a fused jcc:
11688 FUSED_JCC_PADDING
11689 CMP like instruction
11690 BRANCH_PADDING
11691 COND_JUMP
11692 */
11693 cmp_fragP = i386_next_non_empty_frag (next_fragP);
11694 pad_fragP = i386_next_non_empty_frag (cmp_fragP);
79d72f45 11695 branch_fragP = i386_next_fusible_jcc_frag (next_fragP, pad_fragP);
e379e5f3
L
11696 if (branch_fragP)
11697 {
11698 /* The BRANCH_PADDING frag is merged with the
11699 FUSED_JCC_PADDING frag. */
11700 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
11701 /* CMP like instruction size. */
11702 next_fragP->tc_frag_data.cmp_size = cmp_fragP->fr_fix;
11703 frag_wane (pad_fragP);
11704 /* Skip to branch_fragP. */
11705 next_fragP = branch_fragP;
11706 }
11707 else if (next_fragP->tc_frag_data.max_prefix_length)
11708 {
11709 /* Turn FUSED_JCC_PADDING into BRANCH_PREFIX if it isn't
11710 a fused jcc. */
11711 next_fragP->fr_subtype
11712 = ENCODE_RELAX_STATE (BRANCH_PREFIX, 0);
11713 next_fragP->tc_frag_data.max_bytes
11714 = next_fragP->tc_frag_data.max_prefix_length;
11715 /* This will be updated in the BRANCH_PREFIX scan. */
11716 next_fragP->tc_frag_data.max_prefix_length = 0;
11717 }
11718 else
11719 frag_wane (next_fragP);
11720 break;
11721 }
11722 }
11723
11724 /* Stop if there is no BRANCH_PREFIX. */
11725 if (!align_branch_prefix_size)
11726 return;
11727
11728 /* Scan for BRANCH_PREFIX. */
11729 for (; fragP != NULL; fragP = fragP->fr_next)
11730 {
11731 if (fragP->fr_type != rs_machine_dependent
11732 || (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
11733 != BRANCH_PREFIX))
11734 continue;
11735
11736 /* Count all BRANCH_PREFIX frags before BRANCH_PADDING and
11737 COND_JUMP_PREFIX. */
11738 max_prefix_length = 0;
11739 for (next_fragP = fragP;
11740 next_fragP != NULL;
11741 next_fragP = next_fragP->fr_next)
11742 {
11743 if (next_fragP->fr_type == rs_fill)
11744 /* Skip rs_fill frags. */
11745 continue;
11746 else if (next_fragP->fr_type != rs_machine_dependent)
11747 /* Stop for all other frags. */
11748 break;
11749
11750 /* rs_machine_dependent frags. */
11751 if (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
11752 == BRANCH_PREFIX)
11753 {
11754 /* Count BRANCH_PREFIX frags. */
11755 if (max_prefix_length >= MAX_FUSED_JCC_PADDING_SIZE)
11756 {
11757 max_prefix_length = MAX_FUSED_JCC_PADDING_SIZE;
11758 frag_wane (next_fragP);
11759 }
11760 else
11761 max_prefix_length
11762 += next_fragP->tc_frag_data.max_bytes;
11763 }
11764 else if ((TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
11765 == BRANCH_PADDING)
11766 || (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
11767 == FUSED_JCC_PADDING))
11768 {
11769 /* Stop at BRANCH_PADDING and FUSED_JCC_PADDING. */
11770 fragP->tc_frag_data.u.padding_fragP = next_fragP;
11771 break;
11772 }
11773 else
11774 /* Stop for other rs_machine_dependent frags. */
11775 break;
11776 }
11777
11778 fragP->tc_frag_data.max_prefix_length = max_prefix_length;
11779
11780 /* Skip to the next frag. */
11781 fragP = next_fragP;
11782 }
11783}
11784
11785/* Compute padding size for
11786
11787 FUSED_JCC_PADDING
11788 CMP like instruction
11789 BRANCH_PADDING
11790 COND_JUMP/UNCOND_JUMP
11791
11792 or
11793
11794 BRANCH_PADDING
11795 COND_JUMP/UNCOND_JUMP
11796 */
11797
11798static int
11799i386_branch_padding_size (fragS *fragP, offsetT address)
11800{
11801 unsigned int offset, size, padding_size;
11802 fragS *branch_fragP = fragP->tc_frag_data.u.branch_fragP;
11803
11804 /* The start address of the BRANCH_PADDING or FUSED_JCC_PADDING frag. */
11805 if (!address)
11806 address = fragP->fr_address;
11807 address += fragP->fr_fix;
11808
11809 /* CMP like instrunction size. */
11810 size = fragP->tc_frag_data.cmp_size;
11811
11812 /* The base size of the branch frag. */
11813 size += branch_fragP->fr_fix;
11814
11815 /* Add opcode and displacement bytes for the rs_machine_dependent
11816 branch frag. */
11817 if (branch_fragP->fr_type == rs_machine_dependent)
11818 size += md_relax_table[branch_fragP->fr_subtype].rlx_length;
11819
11820 /* Check if branch is within boundary and doesn't end at the last
11821 byte. */
11822 offset = address & ((1U << align_branch_power) - 1);
11823 if ((offset + size) >= (1U << align_branch_power))
11824 /* Padding needed to avoid crossing boundary. */
11825 padding_size = (1U << align_branch_power) - offset;
11826 else
11827 /* No padding needed. */
11828 padding_size = 0;
11829
11830 /* The return value may be saved in tc_frag_data.length which is
11831 unsigned byte. */
11832 if (!fits_in_unsigned_byte (padding_size))
11833 abort ();
11834
11835 return padding_size;
11836}
11837
11838/* i386_generic_table_relax_frag()
11839
11840 Handle BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags to
11841 grow/shrink padding to align branch frags. Hand others to
11842 relax_frag(). */
11843
11844long
11845i386_generic_table_relax_frag (segT segment, fragS *fragP, long stretch)
11846{
11847 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
11848 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
11849 {
11850 long padding_size = i386_branch_padding_size (fragP, 0);
11851 long grow = padding_size - fragP->tc_frag_data.length;
11852
11853 /* When the BRANCH_PREFIX frag is used, the computed address
11854 must match the actual address and there should be no padding. */
11855 if (fragP->tc_frag_data.padding_address
11856 && (fragP->tc_frag_data.padding_address != fragP->fr_address
11857 || padding_size))
11858 abort ();
11859
11860 /* Update the padding size. */
11861 if (grow)
11862 fragP->tc_frag_data.length = padding_size;
11863
11864 return grow;
11865 }
11866 else if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
11867 {
11868 fragS *padding_fragP, *next_fragP;
11869 long padding_size, left_size, last_size;
11870
11871 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
11872 if (!padding_fragP)
11873 /* Use the padding set by the leading BRANCH_PREFIX frag. */
11874 return (fragP->tc_frag_data.length
11875 - fragP->tc_frag_data.last_length);
11876
11877 /* Compute the relative address of the padding frag in the very
11878 first time where the BRANCH_PREFIX frag sizes are zero. */
11879 if (!fragP->tc_frag_data.padding_address)
11880 fragP->tc_frag_data.padding_address
11881 = padding_fragP->fr_address - (fragP->fr_address - stretch);
11882
11883 /* First update the last length from the previous interation. */
11884 left_size = fragP->tc_frag_data.prefix_length;
11885 for (next_fragP = fragP;
11886 next_fragP != padding_fragP;
11887 next_fragP = next_fragP->fr_next)
11888 if (next_fragP->fr_type == rs_machine_dependent
11889 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
11890 == BRANCH_PREFIX))
11891 {
11892 if (left_size)
11893 {
11894 int max = next_fragP->tc_frag_data.max_bytes;
11895 if (max)
11896 {
11897 int size;
11898 if (max > left_size)
11899 size = left_size;
11900 else
11901 size = max;
11902 left_size -= size;
11903 next_fragP->tc_frag_data.last_length = size;
11904 }
11905 }
11906 else
11907 next_fragP->tc_frag_data.last_length = 0;
11908 }
11909
11910 /* Check the padding size for the padding frag. */
11911 padding_size = i386_branch_padding_size
11912 (padding_fragP, (fragP->fr_address
11913 + fragP->tc_frag_data.padding_address));
11914
11915 last_size = fragP->tc_frag_data.prefix_length;
11916 /* Check if there is change from the last interation. */
11917 if (padding_size == last_size)
11918 {
11919 /* Update the expected address of the padding frag. */
11920 padding_fragP->tc_frag_data.padding_address
11921 = (fragP->fr_address + padding_size
11922 + fragP->tc_frag_data.padding_address);
11923 return 0;
11924 }
11925
11926 if (padding_size > fragP->tc_frag_data.max_prefix_length)
11927 {
11928 /* No padding if there is no sufficient room. Clear the
11929 expected address of the padding frag. */
11930 padding_fragP->tc_frag_data.padding_address = 0;
11931 padding_size = 0;
11932 }
11933 else
11934 /* Store the expected address of the padding frag. */
11935 padding_fragP->tc_frag_data.padding_address
11936 = (fragP->fr_address + padding_size
11937 + fragP->tc_frag_data.padding_address);
11938
11939 fragP->tc_frag_data.prefix_length = padding_size;
11940
11941 /* Update the length for the current interation. */
11942 left_size = padding_size;
11943 for (next_fragP = fragP;
11944 next_fragP != padding_fragP;
11945 next_fragP = next_fragP->fr_next)
11946 if (next_fragP->fr_type == rs_machine_dependent
11947 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
11948 == BRANCH_PREFIX))
11949 {
11950 if (left_size)
11951 {
11952 int max = next_fragP->tc_frag_data.max_bytes;
11953 if (max)
11954 {
11955 int size;
11956 if (max > left_size)
11957 size = left_size;
11958 else
11959 size = max;
11960 left_size -= size;
11961 next_fragP->tc_frag_data.length = size;
11962 }
11963 }
11964 else
11965 next_fragP->tc_frag_data.length = 0;
11966 }
11967
11968 return (fragP->tc_frag_data.length
11969 - fragP->tc_frag_data.last_length);
11970 }
11971 return relax_frag (segment, fragP, stretch);
11972}
11973
ee7fcc42
AM
11974/* md_estimate_size_before_relax()
11975
11976 Called just before relax() for rs_machine_dependent frags. The x86
11977 assembler uses these frags to handle variable size jump
11978 instructions.
11979
11980 Any symbol that is now undefined will not become defined.
11981 Return the correct fr_subtype in the frag.
11982 Return the initial "guess for variable size of frag" to caller.
11983 The guess is actually the growth beyond the fixed part. Whatever
11984 we do to grow the fixed or variable part contributes to our
11985 returned value. */
11986
252b5132 11987int
7016a5d5 11988md_estimate_size_before_relax (fragS *fragP, segT segment)
252b5132 11989{
e379e5f3
L
11990 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
11991 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX
11992 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
11993 {
11994 i386_classify_machine_dependent_frag (fragP);
11995 return fragP->tc_frag_data.length;
11996 }
11997
252b5132 11998 /* We've already got fragP->fr_subtype right; all we have to do is
b98ef147
AM
11999 check for un-relaxable symbols. On an ELF system, we can't relax
12000 an externally visible symbol, because it may be overridden by a
12001 shared library. */
12002 if (S_GET_SEGMENT (fragP->fr_symbol) != segment
6d249963 12003#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12004 || (IS_ELF
8dcea932
L
12005 && !elf_symbol_resolved_in_segment_p (fragP->fr_symbol,
12006 fragP->fr_var))
fbeb56a4
DK
12007#endif
12008#if defined (OBJ_COFF) && defined (TE_PE)
7ab9ffdd 12009 || (OUTPUT_FLAVOR == bfd_target_coff_flavour
fbeb56a4 12010 && S_IS_WEAK (fragP->fr_symbol))
b98ef147
AM
12011#endif
12012 )
252b5132 12013 {
b98ef147
AM
12014 /* Symbol is undefined in this segment, or we need to keep a
12015 reloc so that weak symbols can be overridden. */
12016 int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
f86103b7 12017 enum bfd_reloc_code_real reloc_type;
ee7fcc42
AM
12018 unsigned char *opcode;
12019 int old_fr_fix;
f6af82bd 12020
ee7fcc42 12021 if (fragP->fr_var != NO_RELOC)
1e9cc1c2 12022 reloc_type = (enum bfd_reloc_code_real) fragP->fr_var;
b98ef147 12023 else if (size == 2)
f6af82bd 12024 reloc_type = BFD_RELOC_16_PCREL;
bd7ab16b
L
12025#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
12026 else if (need_plt32_p (fragP->fr_symbol))
12027 reloc_type = BFD_RELOC_X86_64_PLT32;
12028#endif
f6af82bd
AM
12029 else
12030 reloc_type = BFD_RELOC_32_PCREL;
252b5132 12031
ee7fcc42
AM
12032 old_fr_fix = fragP->fr_fix;
12033 opcode = (unsigned char *) fragP->fr_opcode;
12034
fddf5b5b 12035 switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
252b5132 12036 {
fddf5b5b
AM
12037 case UNCOND_JUMP:
12038 /* Make jmp (0xeb) a (d)word displacement jump. */
47926f60 12039 opcode[0] = 0xe9;
252b5132 12040 fragP->fr_fix += size;
062cd5e7
AS
12041 fix_new (fragP, old_fr_fix, size,
12042 fragP->fr_symbol,
12043 fragP->fr_offset, 1,
12044 reloc_type);
252b5132
RH
12045 break;
12046
fddf5b5b 12047 case COND_JUMP86:
412167cb
AM
12048 if (size == 2
12049 && (!no_cond_jump_promotion || fragP->fr_var != NO_RELOC))
fddf5b5b
AM
12050 {
12051 /* Negate the condition, and branch past an
12052 unconditional jump. */
12053 opcode[0] ^= 1;
12054 opcode[1] = 3;
12055 /* Insert an unconditional jump. */
12056 opcode[2] = 0xe9;
12057 /* We added two extra opcode bytes, and have a two byte
12058 offset. */
12059 fragP->fr_fix += 2 + 2;
062cd5e7
AS
12060 fix_new (fragP, old_fr_fix + 2, 2,
12061 fragP->fr_symbol,
12062 fragP->fr_offset, 1,
12063 reloc_type);
fddf5b5b
AM
12064 break;
12065 }
12066 /* Fall through. */
12067
12068 case COND_JUMP:
412167cb
AM
12069 if (no_cond_jump_promotion && fragP->fr_var == NO_RELOC)
12070 {
3e02c1cc
AM
12071 fixS *fixP;
12072
412167cb 12073 fragP->fr_fix += 1;
3e02c1cc
AM
12074 fixP = fix_new (fragP, old_fr_fix, 1,
12075 fragP->fr_symbol,
12076 fragP->fr_offset, 1,
12077 BFD_RELOC_8_PCREL);
12078 fixP->fx_signed = 1;
412167cb
AM
12079 break;
12080 }
93c2a809 12081
24eab124 12082 /* This changes the byte-displacement jump 0x7N
fddf5b5b 12083 to the (d)word-displacement jump 0x0f,0x8N. */
252b5132 12084 opcode[1] = opcode[0] + 0x10;
f6af82bd 12085 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
47926f60
KH
12086 /* We've added an opcode byte. */
12087 fragP->fr_fix += 1 + size;
062cd5e7
AS
12088 fix_new (fragP, old_fr_fix + 1, size,
12089 fragP->fr_symbol,
12090 fragP->fr_offset, 1,
12091 reloc_type);
252b5132 12092 break;
fddf5b5b
AM
12093
12094 default:
12095 BAD_CASE (fragP->fr_subtype);
12096 break;
252b5132
RH
12097 }
12098 frag_wane (fragP);
ee7fcc42 12099 return fragP->fr_fix - old_fr_fix;
252b5132 12100 }
93c2a809 12101
93c2a809
AM
12102 /* Guess size depending on current relax state. Initially the relax
12103 state will correspond to a short jump and we return 1, because
12104 the variable part of the frag (the branch offset) is one byte
12105 long. However, we can relax a section more than once and in that
12106 case we must either set fr_subtype back to the unrelaxed state,
12107 or return the value for the appropriate branch. */
12108 return md_relax_table[fragP->fr_subtype].rlx_length;
ee7fcc42
AM
12109}
12110
47926f60
KH
12111/* Called after relax() is finished.
12112
12113 In: Address of frag.
12114 fr_type == rs_machine_dependent.
12115 fr_subtype is what the address relaxed to.
12116
12117 Out: Any fixSs and constants are set up.
12118 Caller will turn frag into a ".space 0". */
12119
252b5132 12120void
7016a5d5
TG
12121md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec ATTRIBUTE_UNUSED,
12122 fragS *fragP)
252b5132 12123{
29b0f896 12124 unsigned char *opcode;
252b5132 12125 unsigned char *where_to_put_displacement = NULL;
847f7ad4
AM
12126 offsetT target_address;
12127 offsetT opcode_address;
252b5132 12128 unsigned int extension = 0;
847f7ad4 12129 offsetT displacement_from_opcode_start;
252b5132 12130
e379e5f3
L
12131 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12132 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING
12133 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12134 {
12135 /* Generate nop padding. */
12136 unsigned int size = fragP->tc_frag_data.length;
12137 if (size)
12138 {
12139 if (size > fragP->tc_frag_data.max_bytes)
12140 abort ();
12141
12142 if (flag_debug)
12143 {
12144 const char *msg;
12145 const char *branch = "branch";
12146 const char *prefix = "";
12147 fragS *padding_fragP;
12148 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
12149 == BRANCH_PREFIX)
12150 {
12151 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
12152 switch (fragP->tc_frag_data.default_prefix)
12153 {
12154 default:
12155 abort ();
12156 break;
12157 case CS_PREFIX_OPCODE:
12158 prefix = " cs";
12159 break;
12160 case DS_PREFIX_OPCODE:
12161 prefix = " ds";
12162 break;
12163 case ES_PREFIX_OPCODE:
12164 prefix = " es";
12165 break;
12166 case FS_PREFIX_OPCODE:
12167 prefix = " fs";
12168 break;
12169 case GS_PREFIX_OPCODE:
12170 prefix = " gs";
12171 break;
12172 case SS_PREFIX_OPCODE:
12173 prefix = " ss";
12174 break;
12175 }
12176 if (padding_fragP)
12177 msg = _("%s:%u: add %d%s at 0x%llx to align "
12178 "%s within %d-byte boundary\n");
12179 else
12180 msg = _("%s:%u: add additional %d%s at 0x%llx to "
12181 "align %s within %d-byte boundary\n");
12182 }
12183 else
12184 {
12185 padding_fragP = fragP;
12186 msg = _("%s:%u: add %d%s-byte nop at 0x%llx to align "
12187 "%s within %d-byte boundary\n");
12188 }
12189
12190 if (padding_fragP)
12191 switch (padding_fragP->tc_frag_data.branch_type)
12192 {
12193 case align_branch_jcc:
12194 branch = "jcc";
12195 break;
12196 case align_branch_fused:
12197 branch = "fused jcc";
12198 break;
12199 case align_branch_jmp:
12200 branch = "jmp";
12201 break;
12202 case align_branch_call:
12203 branch = "call";
12204 break;
12205 case align_branch_indirect:
12206 branch = "indiret branch";
12207 break;
12208 case align_branch_ret:
12209 branch = "ret";
12210 break;
12211 default:
12212 break;
12213 }
12214
12215 fprintf (stdout, msg,
12216 fragP->fr_file, fragP->fr_line, size, prefix,
12217 (long long) fragP->fr_address, branch,
12218 1 << align_branch_power);
12219 }
12220 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12221 memset (fragP->fr_opcode,
12222 fragP->tc_frag_data.default_prefix, size);
12223 else
12224 i386_generate_nops (fragP, (char *) fragP->fr_opcode,
12225 size, 0);
12226 fragP->fr_fix += size;
12227 }
12228 return;
12229 }
12230
252b5132
RH
12231 opcode = (unsigned char *) fragP->fr_opcode;
12232
47926f60 12233 /* Address we want to reach in file space. */
252b5132 12234 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
252b5132 12235
47926f60 12236 /* Address opcode resides at in file space. */
252b5132
RH
12237 opcode_address = fragP->fr_address + fragP->fr_fix;
12238
47926f60 12239 /* Displacement from opcode start to fill into instruction. */
252b5132
RH
12240 displacement_from_opcode_start = target_address - opcode_address;
12241
fddf5b5b 12242 if ((fragP->fr_subtype & BIG) == 0)
252b5132 12243 {
47926f60
KH
12244 /* Don't have to change opcode. */
12245 extension = 1; /* 1 opcode + 1 displacement */
252b5132 12246 where_to_put_displacement = &opcode[1];
fddf5b5b
AM
12247 }
12248 else
12249 {
12250 if (no_cond_jump_promotion
12251 && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
4eed87de
AM
12252 as_warn_where (fragP->fr_file, fragP->fr_line,
12253 _("long jump required"));
252b5132 12254
fddf5b5b
AM
12255 switch (fragP->fr_subtype)
12256 {
12257 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
12258 extension = 4; /* 1 opcode + 4 displacement */
12259 opcode[0] = 0xe9;
12260 where_to_put_displacement = &opcode[1];
12261 break;
252b5132 12262
fddf5b5b
AM
12263 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
12264 extension = 2; /* 1 opcode + 2 displacement */
12265 opcode[0] = 0xe9;
12266 where_to_put_displacement = &opcode[1];
12267 break;
252b5132 12268
fddf5b5b
AM
12269 case ENCODE_RELAX_STATE (COND_JUMP, BIG):
12270 case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
12271 extension = 5; /* 2 opcode + 4 displacement */
12272 opcode[1] = opcode[0] + 0x10;
12273 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12274 where_to_put_displacement = &opcode[2];
12275 break;
252b5132 12276
fddf5b5b
AM
12277 case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
12278 extension = 3; /* 2 opcode + 2 displacement */
12279 opcode[1] = opcode[0] + 0x10;
12280 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12281 where_to_put_displacement = &opcode[2];
12282 break;
252b5132 12283
fddf5b5b
AM
12284 case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
12285 extension = 4;
12286 opcode[0] ^= 1;
12287 opcode[1] = 3;
12288 opcode[2] = 0xe9;
12289 where_to_put_displacement = &opcode[3];
12290 break;
12291
12292 default:
12293 BAD_CASE (fragP->fr_subtype);
12294 break;
12295 }
252b5132 12296 }
fddf5b5b 12297
7b81dfbb
AJ
12298 /* If size if less then four we are sure that the operand fits,
12299 but if it's 4, then it could be that the displacement is larger
12300 then -/+ 2GB. */
12301 if (DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype) == 4
12302 && object_64bit
12303 && ((addressT) (displacement_from_opcode_start - extension
4eed87de
AM
12304 + ((addressT) 1 << 31))
12305 > (((addressT) 2 << 31) - 1)))
7b81dfbb
AJ
12306 {
12307 as_bad_where (fragP->fr_file, fragP->fr_line,
12308 _("jump target out of range"));
12309 /* Make us emit 0. */
12310 displacement_from_opcode_start = extension;
12311 }
47926f60 12312 /* Now put displacement after opcode. */
252b5132
RH
12313 md_number_to_chars ((char *) where_to_put_displacement,
12314 (valueT) (displacement_from_opcode_start - extension),
fddf5b5b 12315 DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
252b5132
RH
12316 fragP->fr_fix += extension;
12317}
12318\f
7016a5d5 12319/* Apply a fixup (fixP) to segment data, once it has been determined
252b5132
RH
12320 by our caller that we have all the info we need to fix it up.
12321
7016a5d5
TG
12322 Parameter valP is the pointer to the value of the bits.
12323
252b5132
RH
12324 On the 386, immediates, displacements, and data pointers are all in
12325 the same (little-endian) format, so we don't need to care about which
12326 we are handling. */
12327
94f592af 12328void
7016a5d5 12329md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 12330{
94f592af 12331 char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
c6682705 12332 valueT value = *valP;
252b5132 12333
f86103b7 12334#if !defined (TE_Mach)
93382f6d
AM
12335 if (fixP->fx_pcrel)
12336 {
12337 switch (fixP->fx_r_type)
12338 {
5865bb77
ILT
12339 default:
12340 break;
12341
d6ab8113
JB
12342 case BFD_RELOC_64:
12343 fixP->fx_r_type = BFD_RELOC_64_PCREL;
12344 break;
93382f6d 12345 case BFD_RELOC_32:
ae8887b5 12346 case BFD_RELOC_X86_64_32S:
93382f6d
AM
12347 fixP->fx_r_type = BFD_RELOC_32_PCREL;
12348 break;
12349 case BFD_RELOC_16:
12350 fixP->fx_r_type = BFD_RELOC_16_PCREL;
12351 break;
12352 case BFD_RELOC_8:
12353 fixP->fx_r_type = BFD_RELOC_8_PCREL;
12354 break;
12355 }
12356 }
252b5132 12357
a161fe53 12358 if (fixP->fx_addsy != NULL
31312f95 12359 && (fixP->fx_r_type == BFD_RELOC_32_PCREL
d6ab8113 12360 || fixP->fx_r_type == BFD_RELOC_64_PCREL
31312f95 12361 || fixP->fx_r_type == BFD_RELOC_16_PCREL
d258b828 12362 || fixP->fx_r_type == BFD_RELOC_8_PCREL)
31312f95 12363 && !use_rela_relocations)
252b5132 12364 {
31312f95
AM
12365 /* This is a hack. There should be a better way to handle this.
12366 This covers for the fact that bfd_install_relocation will
12367 subtract the current location (for partial_inplace, PC relative
12368 relocations); see more below. */
252b5132 12369#ifndef OBJ_AOUT
718ddfc0 12370 if (IS_ELF
252b5132
RH
12371#ifdef TE_PE
12372 || OUTPUT_FLAVOR == bfd_target_coff_flavour
12373#endif
12374 )
12375 value += fixP->fx_where + fixP->fx_frag->fr_address;
12376#endif
12377#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12378 if (IS_ELF)
252b5132 12379 {
6539b54b 12380 segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
2f66722d 12381
6539b54b 12382 if ((sym_seg == seg
2f66722d 12383 || (symbol_section_p (fixP->fx_addsy)
6539b54b 12384 && sym_seg != absolute_section))
af65af87 12385 && !generic_force_reloc (fixP))
2f66722d
AM
12386 {
12387 /* Yes, we add the values in twice. This is because
6539b54b
AM
12388 bfd_install_relocation subtracts them out again. I think
12389 bfd_install_relocation is broken, but I don't dare change
2f66722d
AM
12390 it. FIXME. */
12391 value += fixP->fx_where + fixP->fx_frag->fr_address;
12392 }
252b5132
RH
12393 }
12394#endif
12395#if defined (OBJ_COFF) && defined (TE_PE)
977cdf5a
NC
12396 /* For some reason, the PE format does not store a
12397 section address offset for a PC relative symbol. */
12398 if (S_GET_SEGMENT (fixP->fx_addsy) != seg
7be1c489 12399 || S_IS_WEAK (fixP->fx_addsy))
252b5132
RH
12400 value += md_pcrel_from (fixP);
12401#endif
12402 }
fbeb56a4 12403#if defined (OBJ_COFF) && defined (TE_PE)
f01c1a09
NC
12404 if (fixP->fx_addsy != NULL
12405 && S_IS_WEAK (fixP->fx_addsy)
12406 /* PR 16858: Do not modify weak function references. */
12407 && ! fixP->fx_pcrel)
fbeb56a4 12408 {
296a8689
NC
12409#if !defined (TE_PEP)
12410 /* For x86 PE weak function symbols are neither PC-relative
12411 nor do they set S_IS_FUNCTION. So the only reliable way
12412 to detect them is to check the flags of their containing
12413 section. */
12414 if (S_GET_SEGMENT (fixP->fx_addsy) != NULL
12415 && S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_CODE)
12416 ;
12417 else
12418#endif
fbeb56a4
DK
12419 value -= S_GET_VALUE (fixP->fx_addsy);
12420 }
12421#endif
252b5132
RH
12422
12423 /* Fix a few things - the dynamic linker expects certain values here,
0234cb7c 12424 and we must not disappoint it. */
252b5132 12425#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12426 if (IS_ELF && fixP->fx_addsy)
47926f60
KH
12427 switch (fixP->fx_r_type)
12428 {
12429 case BFD_RELOC_386_PLT32:
3e73aa7c 12430 case BFD_RELOC_X86_64_PLT32:
b9519cfe
L
12431 /* Make the jump instruction point to the address of the operand.
12432 At runtime we merely add the offset to the actual PLT entry.
12433 NB: Subtract the offset size only for jump instructions. */
12434 if (fixP->fx_pcrel)
12435 value = -4;
47926f60 12436 break;
31312f95 12437
13ae64f3
JJ
12438 case BFD_RELOC_386_TLS_GD:
12439 case BFD_RELOC_386_TLS_LDM:
13ae64f3 12440 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
12441 case BFD_RELOC_386_TLS_IE:
12442 case BFD_RELOC_386_TLS_GOTIE:
67a4f2b7 12443 case BFD_RELOC_386_TLS_GOTDESC:
bffbf940
JJ
12444 case BFD_RELOC_X86_64_TLSGD:
12445 case BFD_RELOC_X86_64_TLSLD:
12446 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7 12447 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
00f7efb6
JJ
12448 value = 0; /* Fully resolved at runtime. No addend. */
12449 /* Fallthrough */
12450 case BFD_RELOC_386_TLS_LE:
12451 case BFD_RELOC_386_TLS_LDO_32:
12452 case BFD_RELOC_386_TLS_LE_32:
12453 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 12454 case BFD_RELOC_X86_64_DTPOFF64:
00f7efb6 12455 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113 12456 case BFD_RELOC_X86_64_TPOFF64:
00f7efb6
JJ
12457 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12458 break;
12459
67a4f2b7
AO
12460 case BFD_RELOC_386_TLS_DESC_CALL:
12461 case BFD_RELOC_X86_64_TLSDESC_CALL:
12462 value = 0; /* Fully resolved at runtime. No addend. */
12463 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12464 fixP->fx_done = 0;
12465 return;
12466
47926f60
KH
12467 case BFD_RELOC_VTABLE_INHERIT:
12468 case BFD_RELOC_VTABLE_ENTRY:
12469 fixP->fx_done = 0;
94f592af 12470 return;
47926f60
KH
12471
12472 default:
12473 break;
12474 }
12475#endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) */
c6682705 12476 *valP = value;
f86103b7 12477#endif /* !defined (TE_Mach) */
3e73aa7c 12478
3e73aa7c 12479 /* Are we finished with this relocation now? */
c6682705 12480 if (fixP->fx_addsy == NULL)
3e73aa7c 12481 fixP->fx_done = 1;
fbeb56a4
DK
12482#if defined (OBJ_COFF) && defined (TE_PE)
12483 else if (fixP->fx_addsy != NULL && S_IS_WEAK (fixP->fx_addsy))
12484 {
12485 fixP->fx_done = 0;
12486 /* Remember value for tc_gen_reloc. */
12487 fixP->fx_addnumber = value;
12488 /* Clear out the frag for now. */
12489 value = 0;
12490 }
12491#endif
3e73aa7c
JH
12492 else if (use_rela_relocations)
12493 {
12494 fixP->fx_no_overflow = 1;
062cd5e7
AS
12495 /* Remember value for tc_gen_reloc. */
12496 fixP->fx_addnumber = value;
3e73aa7c
JH
12497 value = 0;
12498 }
f86103b7 12499
94f592af 12500 md_number_to_chars (p, value, fixP->fx_size);
252b5132 12501}
252b5132 12502\f
6d4af3c2 12503const char *
499ac353 12504md_atof (int type, char *litP, int *sizeP)
252b5132 12505{
499ac353
NC
12506 /* This outputs the LITTLENUMs in REVERSE order;
12507 in accord with the bigendian 386. */
12508 return ieee_md_atof (type, litP, sizeP, FALSE);
252b5132
RH
12509}
12510\f
2d545b82 12511static char output_invalid_buf[sizeof (unsigned char) * 2 + 6];
252b5132 12512
252b5132 12513static char *
e3bb37b5 12514output_invalid (int c)
252b5132 12515{
3882b010 12516 if (ISPRINT (c))
f9f21a03
L
12517 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
12518 "'%c'", c);
252b5132 12519 else
f9f21a03 12520 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
2d545b82 12521 "(0x%x)", (unsigned char) c);
252b5132
RH
12522 return output_invalid_buf;
12523}
12524
8a6fb3f9
JB
12525/* Verify that @r can be used in the current context. */
12526
12527static bfd_boolean check_register (const reg_entry *r)
12528{
12529 if (allow_pseudo_reg)
12530 return TRUE;
12531
12532 if (operand_type_all_zero (&r->reg_type))
12533 return FALSE;
12534
12535 if ((r->reg_type.bitfield.dword
12536 || (r->reg_type.bitfield.class == SReg && r->reg_num > 3)
12537 || r->reg_type.bitfield.class == RegCR
22e00a3f 12538 || r->reg_type.bitfield.class == RegDR)
8a6fb3f9
JB
12539 && !cpu_arch_flags.bitfield.cpui386)
12540 return FALSE;
12541
22e00a3f
JB
12542 if (r->reg_type.bitfield.class == RegTR
12543 && (flag_code == CODE_64BIT
12544 || !cpu_arch_flags.bitfield.cpui386
12545 || cpu_arch_isa_flags.bitfield.cpui586
12546 || cpu_arch_isa_flags.bitfield.cpui686))
12547 return FALSE;
12548
8a6fb3f9
JB
12549 if (r->reg_type.bitfield.class == RegMMX && !cpu_arch_flags.bitfield.cpummx)
12550 return FALSE;
12551
12552 if (!cpu_arch_flags.bitfield.cpuavx512f)
12553 {
12554 if (r->reg_type.bitfield.zmmword
12555 || r->reg_type.bitfield.class == RegMask)
12556 return FALSE;
12557
12558 if (!cpu_arch_flags.bitfield.cpuavx)
12559 {
12560 if (r->reg_type.bitfield.ymmword)
12561 return FALSE;
12562
12563 if (!cpu_arch_flags.bitfield.cpusse && r->reg_type.bitfield.xmmword)
12564 return FALSE;
12565 }
12566 }
12567
260cd341
LC
12568 if (r->reg_type.bitfield.tmmword
12569 && (!cpu_arch_flags.bitfield.cpuamx_tile
12570 || flag_code != CODE_64BIT))
12571 return FALSE;
12572
8a6fb3f9
JB
12573 if (r->reg_type.bitfield.class == RegBND && !cpu_arch_flags.bitfield.cpumpx)
12574 return FALSE;
12575
12576 /* Don't allow fake index register unless allow_index_reg isn't 0. */
12577 if (!allow_index_reg && r->reg_num == RegIZ)
12578 return FALSE;
12579
12580 /* Upper 16 vector registers are only available with VREX in 64bit
12581 mode, and require EVEX encoding. */
12582 if (r->reg_flags & RegVRex)
12583 {
12584 if (!cpu_arch_flags.bitfield.cpuavx512f
12585 || flag_code != CODE_64BIT)
12586 return FALSE;
12587
da4977e0
JB
12588 if (i.vec_encoding == vex_encoding_default)
12589 i.vec_encoding = vex_encoding_evex;
12590 else if (i.vec_encoding != vex_encoding_evex)
12591 i.vec_encoding = vex_encoding_error;
8a6fb3f9
JB
12592 }
12593
12594 if (((r->reg_flags & (RegRex64 | RegRex)) || r->reg_type.bitfield.qword)
12595 && (!cpu_arch_flags.bitfield.cpulm || r->reg_type.bitfield.class != RegCR)
12596 && flag_code != CODE_64BIT)
12597 return FALSE;
12598
12599 if (r->reg_type.bitfield.class == SReg && r->reg_num == RegFlat
12600 && !intel_syntax)
12601 return FALSE;
12602
12603 return TRUE;
12604}
12605
af6bdddf 12606/* REG_STRING starts *before* REGISTER_PREFIX. */
252b5132
RH
12607
12608static const reg_entry *
4d1bb795 12609parse_real_register (char *reg_string, char **end_op)
252b5132 12610{
af6bdddf
AM
12611 char *s = reg_string;
12612 char *p;
252b5132
RH
12613 char reg_name_given[MAX_REG_NAME_SIZE + 1];
12614 const reg_entry *r;
12615
12616 /* Skip possible REGISTER_PREFIX and possible whitespace. */
12617 if (*s == REGISTER_PREFIX)
12618 ++s;
12619
12620 if (is_space_char (*s))
12621 ++s;
12622
12623 p = reg_name_given;
af6bdddf 12624 while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
252b5132
RH
12625 {
12626 if (p >= reg_name_given + MAX_REG_NAME_SIZE)
af6bdddf
AM
12627 return (const reg_entry *) NULL;
12628 s++;
252b5132
RH
12629 }
12630
6588847e
DN
12631 /* For naked regs, make sure that we are not dealing with an identifier.
12632 This prevents confusing an identifier like `eax_var' with register
12633 `eax'. */
12634 if (allow_naked_reg && identifier_chars[(unsigned char) *s])
12635 return (const reg_entry *) NULL;
12636
af6bdddf 12637 *end_op = s;
252b5132 12638
629310ab 12639 r = (const reg_entry *) str_hash_find (reg_hash, reg_name_given);
252b5132 12640
5f47d35b 12641 /* Handle floating point regs, allowing spaces in the (i) part. */
47926f60 12642 if (r == i386_regtab /* %st is first entry of table */)
5f47d35b 12643 {
0e0eea78
JB
12644 if (!cpu_arch_flags.bitfield.cpu8087
12645 && !cpu_arch_flags.bitfield.cpu287
af32b722
JB
12646 && !cpu_arch_flags.bitfield.cpu387
12647 && !allow_pseudo_reg)
0e0eea78
JB
12648 return (const reg_entry *) NULL;
12649
5f47d35b
AM
12650 if (is_space_char (*s))
12651 ++s;
12652 if (*s == '(')
12653 {
af6bdddf 12654 ++s;
5f47d35b
AM
12655 if (is_space_char (*s))
12656 ++s;
12657 if (*s >= '0' && *s <= '7')
12658 {
db557034 12659 int fpr = *s - '0';
af6bdddf 12660 ++s;
5f47d35b
AM
12661 if (is_space_char (*s))
12662 ++s;
12663 if (*s == ')')
12664 {
12665 *end_op = s + 1;
629310ab 12666 r = (const reg_entry *) str_hash_find (reg_hash, "st(0)");
db557034
AM
12667 know (r);
12668 return r + fpr;
5f47d35b 12669 }
5f47d35b 12670 }
47926f60 12671 /* We have "%st(" then garbage. */
5f47d35b
AM
12672 return (const reg_entry *) NULL;
12673 }
12674 }
12675
8a6fb3f9 12676 return r && check_register (r) ? r : NULL;
252b5132 12677}
4d1bb795
JB
12678
12679/* REG_STRING starts *before* REGISTER_PREFIX. */
12680
12681static const reg_entry *
12682parse_register (char *reg_string, char **end_op)
12683{
12684 const reg_entry *r;
12685
12686 if (*reg_string == REGISTER_PREFIX || allow_naked_reg)
12687 r = parse_real_register (reg_string, end_op);
12688 else
12689 r = NULL;
12690 if (!r)
12691 {
12692 char *save = input_line_pointer;
12693 char c;
12694 symbolS *symbolP;
12695
12696 input_line_pointer = reg_string;
d02603dc 12697 c = get_symbol_name (&reg_string);
4d1bb795
JB
12698 symbolP = symbol_find (reg_string);
12699 if (symbolP && S_GET_SEGMENT (symbolP) == reg_section)
12700 {
12701 const expressionS *e = symbol_get_value_expression (symbolP);
12702
0398aac5 12703 know (e->X_op == O_register);
4eed87de 12704 know (e->X_add_number >= 0
c3fe08fa 12705 && (valueT) e->X_add_number < i386_regtab_size);
4d1bb795 12706 r = i386_regtab + e->X_add_number;
8a6fb3f9
JB
12707 if (!check_register (r))
12708 {
12709 as_bad (_("register '%s%s' cannot be used here"),
12710 register_prefix, r->reg_name);
12711 r = &bad_reg;
12712 }
4d1bb795
JB
12713 *end_op = input_line_pointer;
12714 }
12715 *input_line_pointer = c;
12716 input_line_pointer = save;
12717 }
12718 return r;
12719}
12720
12721int
12722i386_parse_name (char *name, expressionS *e, char *nextcharP)
12723{
12724 const reg_entry *r;
12725 char *end = input_line_pointer;
12726
12727 *end = *nextcharP;
12728 r = parse_register (name, &input_line_pointer);
12729 if (r && end <= input_line_pointer)
12730 {
12731 *nextcharP = *input_line_pointer;
12732 *input_line_pointer = 0;
8a6fb3f9
JB
12733 if (r != &bad_reg)
12734 {
12735 e->X_op = O_register;
12736 e->X_add_number = r - i386_regtab;
12737 }
12738 else
12739 e->X_op = O_illegal;
4d1bb795
JB
12740 return 1;
12741 }
12742 input_line_pointer = end;
12743 *end = 0;
ee86248c 12744 return intel_syntax ? i386_intel_parse_name (name, e) : 0;
4d1bb795
JB
12745}
12746
12747void
12748md_operand (expressionS *e)
12749{
ee86248c
JB
12750 char *end;
12751 const reg_entry *r;
4d1bb795 12752
ee86248c
JB
12753 switch (*input_line_pointer)
12754 {
12755 case REGISTER_PREFIX:
12756 r = parse_real_register (input_line_pointer, &end);
4d1bb795
JB
12757 if (r)
12758 {
12759 e->X_op = O_register;
12760 e->X_add_number = r - i386_regtab;
12761 input_line_pointer = end;
12762 }
ee86248c
JB
12763 break;
12764
12765 case '[':
9c2799c2 12766 gas_assert (intel_syntax);
ee86248c
JB
12767 end = input_line_pointer++;
12768 expression (e);
12769 if (*input_line_pointer == ']')
12770 {
12771 ++input_line_pointer;
12772 e->X_op_symbol = make_expr_symbol (e);
12773 e->X_add_symbol = NULL;
12774 e->X_add_number = 0;
12775 e->X_op = O_index;
12776 }
12777 else
12778 {
12779 e->X_op = O_absent;
12780 input_line_pointer = end;
12781 }
12782 break;
4d1bb795
JB
12783 }
12784}
12785
252b5132 12786\f
4cc782b5 12787#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
b6f8c7c4 12788const char *md_shortopts = "kVQ:sqnO::";
252b5132 12789#else
b6f8c7c4 12790const char *md_shortopts = "qnO::";
252b5132 12791#endif
6e0b89ee 12792
3e73aa7c 12793#define OPTION_32 (OPTION_MD_BASE + 0)
b3b91714
AM
12794#define OPTION_64 (OPTION_MD_BASE + 1)
12795#define OPTION_DIVIDE (OPTION_MD_BASE + 2)
9103f4f4
L
12796#define OPTION_MARCH (OPTION_MD_BASE + 3)
12797#define OPTION_MTUNE (OPTION_MD_BASE + 4)
1efbbeb4
L
12798#define OPTION_MMNEMONIC (OPTION_MD_BASE + 5)
12799#define OPTION_MSYNTAX (OPTION_MD_BASE + 6)
12800#define OPTION_MINDEX_REG (OPTION_MD_BASE + 7)
12801#define OPTION_MNAKED_REG (OPTION_MD_BASE + 8)
bd5dea88 12802#define OPTION_MRELAX_RELOCATIONS (OPTION_MD_BASE + 9)
c0f3af97 12803#define OPTION_MSSE2AVX (OPTION_MD_BASE + 10)
daf50ae7 12804#define OPTION_MSSE_CHECK (OPTION_MD_BASE + 11)
7bab8ab5
JB
12805#define OPTION_MOPERAND_CHECK (OPTION_MD_BASE + 12)
12806#define OPTION_MAVXSCALAR (OPTION_MD_BASE + 13)
12807#define OPTION_X32 (OPTION_MD_BASE + 14)
7e8b059b 12808#define OPTION_MADD_BND_PREFIX (OPTION_MD_BASE + 15)
43234a1e
L
12809#define OPTION_MEVEXLIG (OPTION_MD_BASE + 16)
12810#define OPTION_MEVEXWIG (OPTION_MD_BASE + 17)
167ad85b 12811#define OPTION_MBIG_OBJ (OPTION_MD_BASE + 18)
d1982f93 12812#define OPTION_MOMIT_LOCK_PREFIX (OPTION_MD_BASE + 19)
d3d3c6db 12813#define OPTION_MEVEXRCIG (OPTION_MD_BASE + 20)
8dcea932 12814#define OPTION_MSHARED (OPTION_MD_BASE + 21)
5db04b09
L
12815#define OPTION_MAMD64 (OPTION_MD_BASE + 22)
12816#define OPTION_MINTEL64 (OPTION_MD_BASE + 23)
e4e00185 12817#define OPTION_MFENCE_AS_LOCK_ADD (OPTION_MD_BASE + 24)
b4a3a7b4 12818#define OPTION_X86_USED_NOTE (OPTION_MD_BASE + 25)
03751133 12819#define OPTION_MVEXWIG (OPTION_MD_BASE + 26)
e379e5f3
L
12820#define OPTION_MALIGN_BRANCH_BOUNDARY (OPTION_MD_BASE + 27)
12821#define OPTION_MALIGN_BRANCH_PREFIX_SIZE (OPTION_MD_BASE + 28)
12822#define OPTION_MALIGN_BRANCH (OPTION_MD_BASE + 29)
76cf450b 12823#define OPTION_MBRANCHES_WITH_32B_BOUNDARIES (OPTION_MD_BASE + 30)
ae531041
L
12824#define OPTION_MLFENCE_AFTER_LOAD (OPTION_MD_BASE + 31)
12825#define OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH (OPTION_MD_BASE + 32)
12826#define OPTION_MLFENCE_BEFORE_RET (OPTION_MD_BASE + 33)
b3b91714 12827
99ad8390
NC
12828struct option md_longopts[] =
12829{
3e73aa7c 12830 {"32", no_argument, NULL, OPTION_32},
321098a5 12831#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 12832 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c 12833 {"64", no_argument, NULL, OPTION_64},
351f65ca
L
12834#endif
12835#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 12836 {"x32", no_argument, NULL, OPTION_X32},
8dcea932 12837 {"mshared", no_argument, NULL, OPTION_MSHARED},
b4a3a7b4 12838 {"mx86-used-note", required_argument, NULL, OPTION_X86_USED_NOTE},
6e0b89ee 12839#endif
b3b91714 12840 {"divide", no_argument, NULL, OPTION_DIVIDE},
9103f4f4
L
12841 {"march", required_argument, NULL, OPTION_MARCH},
12842 {"mtune", required_argument, NULL, OPTION_MTUNE},
1efbbeb4
L
12843 {"mmnemonic", required_argument, NULL, OPTION_MMNEMONIC},
12844 {"msyntax", required_argument, NULL, OPTION_MSYNTAX},
12845 {"mindex-reg", no_argument, NULL, OPTION_MINDEX_REG},
12846 {"mnaked-reg", no_argument, NULL, OPTION_MNAKED_REG},
c0f3af97 12847 {"msse2avx", no_argument, NULL, OPTION_MSSE2AVX},
daf50ae7 12848 {"msse-check", required_argument, NULL, OPTION_MSSE_CHECK},
7bab8ab5 12849 {"moperand-check", required_argument, NULL, OPTION_MOPERAND_CHECK},
539f890d 12850 {"mavxscalar", required_argument, NULL, OPTION_MAVXSCALAR},
03751133 12851 {"mvexwig", required_argument, NULL, OPTION_MVEXWIG},
7e8b059b 12852 {"madd-bnd-prefix", no_argument, NULL, OPTION_MADD_BND_PREFIX},
43234a1e
L
12853 {"mevexlig", required_argument, NULL, OPTION_MEVEXLIG},
12854 {"mevexwig", required_argument, NULL, OPTION_MEVEXWIG},
167ad85b
TG
12855# if defined (TE_PE) || defined (TE_PEP)
12856 {"mbig-obj", no_argument, NULL, OPTION_MBIG_OBJ},
12857#endif
d1982f93 12858 {"momit-lock-prefix", required_argument, NULL, OPTION_MOMIT_LOCK_PREFIX},
e4e00185 12859 {"mfence-as-lock-add", required_argument, NULL, OPTION_MFENCE_AS_LOCK_ADD},
0cb4071e 12860 {"mrelax-relocations", required_argument, NULL, OPTION_MRELAX_RELOCATIONS},
d3d3c6db 12861 {"mevexrcig", required_argument, NULL, OPTION_MEVEXRCIG},
e379e5f3
L
12862 {"malign-branch-boundary", required_argument, NULL, OPTION_MALIGN_BRANCH_BOUNDARY},
12863 {"malign-branch-prefix-size", required_argument, NULL, OPTION_MALIGN_BRANCH_PREFIX_SIZE},
12864 {"malign-branch", required_argument, NULL, OPTION_MALIGN_BRANCH},
76cf450b 12865 {"mbranches-within-32B-boundaries", no_argument, NULL, OPTION_MBRANCHES_WITH_32B_BOUNDARIES},
ae531041
L
12866 {"mlfence-after-load", required_argument, NULL, OPTION_MLFENCE_AFTER_LOAD},
12867 {"mlfence-before-indirect-branch", required_argument, NULL,
12868 OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH},
12869 {"mlfence-before-ret", required_argument, NULL, OPTION_MLFENCE_BEFORE_RET},
5db04b09
L
12870 {"mamd64", no_argument, NULL, OPTION_MAMD64},
12871 {"mintel64", no_argument, NULL, OPTION_MINTEL64},
252b5132
RH
12872 {NULL, no_argument, NULL, 0}
12873};
12874size_t md_longopts_size = sizeof (md_longopts);
12875
12876int
17b9d67d 12877md_parse_option (int c, const char *arg)
252b5132 12878{
91d6fa6a 12879 unsigned int j;
e379e5f3 12880 char *arch, *next, *saved, *type;
9103f4f4 12881
252b5132
RH
12882 switch (c)
12883 {
12b55ccc
L
12884 case 'n':
12885 optimize_align_code = 0;
12886 break;
12887
a38cf1db
AM
12888 case 'q':
12889 quiet_warnings = 1;
252b5132
RH
12890 break;
12891
12892#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
a38cf1db
AM
12893 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
12894 should be emitted or not. FIXME: Not implemented. */
12895 case 'Q':
d4693039
JB
12896 if ((arg[0] != 'y' && arg[0] != 'n') || arg[1])
12897 return 0;
252b5132
RH
12898 break;
12899
12900 /* -V: SVR4 argument to print version ID. */
12901 case 'V':
12902 print_version_id ();
12903 break;
12904
a38cf1db
AM
12905 /* -k: Ignore for FreeBSD compatibility. */
12906 case 'k':
252b5132 12907 break;
4cc782b5
ILT
12908
12909 case 's':
12910 /* -s: On i386 Solaris, this tells the native assembler to use
29b0f896 12911 .stab instead of .stab.excl. We always use .stab anyhow. */
4cc782b5 12912 break;
8dcea932
L
12913
12914 case OPTION_MSHARED:
12915 shared = 1;
12916 break;
b4a3a7b4
L
12917
12918 case OPTION_X86_USED_NOTE:
12919 if (strcasecmp (arg, "yes") == 0)
12920 x86_used_note = 1;
12921 else if (strcasecmp (arg, "no") == 0)
12922 x86_used_note = 0;
12923 else
12924 as_fatal (_("invalid -mx86-used-note= option: `%s'"), arg);
12925 break;
12926
12927
99ad8390 12928#endif
321098a5 12929#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 12930 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c
JH
12931 case OPTION_64:
12932 {
12933 const char **list, **l;
12934
3e73aa7c
JH
12935 list = bfd_target_list ();
12936 for (l = list; *l != NULL; l++)
8620418b 12937 if (CONST_STRNEQ (*l, "elf64-x86-64")
99ad8390
NC
12938 || strcmp (*l, "coff-x86-64") == 0
12939 || strcmp (*l, "pe-x86-64") == 0
d382c579
TG
12940 || strcmp (*l, "pei-x86-64") == 0
12941 || strcmp (*l, "mach-o-x86-64") == 0)
6e0b89ee
AM
12942 {
12943 default_arch = "x86_64";
12944 break;
12945 }
3e73aa7c 12946 if (*l == NULL)
2b5d6a91 12947 as_fatal (_("no compiled in support for x86_64"));
3e73aa7c
JH
12948 free (list);
12949 }
12950 break;
12951#endif
252b5132 12952
351f65ca 12953#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 12954 case OPTION_X32:
351f65ca
L
12955 if (IS_ELF)
12956 {
12957 const char **list, **l;
12958
12959 list = bfd_target_list ();
12960 for (l = list; *l != NULL; l++)
12961 if (CONST_STRNEQ (*l, "elf32-x86-64"))
12962 {
12963 default_arch = "x86_64:32";
12964 break;
12965 }
12966 if (*l == NULL)
2b5d6a91 12967 as_fatal (_("no compiled in support for 32bit x86_64"));
351f65ca
L
12968 free (list);
12969 }
12970 else
12971 as_fatal (_("32bit x86_64 is only supported for ELF"));
12972 break;
12973#endif
12974
6e0b89ee
AM
12975 case OPTION_32:
12976 default_arch = "i386";
12977 break;
12978
b3b91714
AM
12979 case OPTION_DIVIDE:
12980#ifdef SVR4_COMMENT_CHARS
12981 {
12982 char *n, *t;
12983 const char *s;
12984
add39d23 12985 n = XNEWVEC (char, strlen (i386_comment_chars) + 1);
b3b91714
AM
12986 t = n;
12987 for (s = i386_comment_chars; *s != '\0'; s++)
12988 if (*s != '/')
12989 *t++ = *s;
12990 *t = '\0';
12991 i386_comment_chars = n;
12992 }
12993#endif
12994 break;
12995
9103f4f4 12996 case OPTION_MARCH:
293f5f65
L
12997 saved = xstrdup (arg);
12998 arch = saved;
12999 /* Allow -march=+nosse. */
13000 if (*arch == '+')
13001 arch++;
6305a203 13002 do
9103f4f4 13003 {
6305a203 13004 if (*arch == '.')
2b5d6a91 13005 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
13006 next = strchr (arch, '+');
13007 if (next)
13008 *next++ = '\0';
91d6fa6a 13009 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 13010 {
91d6fa6a 13011 if (strcmp (arch, cpu_arch [j].name) == 0)
ccc9c027 13012 {
6305a203 13013 /* Processor. */
1ded5609
JB
13014 if (! cpu_arch[j].flags.bitfield.cpui386)
13015 continue;
13016
91d6fa6a 13017 cpu_arch_name = cpu_arch[j].name;
6305a203 13018 cpu_sub_arch_name = NULL;
91d6fa6a
NC
13019 cpu_arch_flags = cpu_arch[j].flags;
13020 cpu_arch_isa = cpu_arch[j].type;
13021 cpu_arch_isa_flags = cpu_arch[j].flags;
6305a203
L
13022 if (!cpu_arch_tune_set)
13023 {
13024 cpu_arch_tune = cpu_arch_isa;
13025 cpu_arch_tune_flags = cpu_arch_isa_flags;
13026 }
13027 break;
13028 }
91d6fa6a
NC
13029 else if (*cpu_arch [j].name == '.'
13030 && strcmp (arch, cpu_arch [j].name + 1) == 0)
6305a203 13031 {
33eaf5de 13032 /* ISA extension. */
6305a203 13033 i386_cpu_flags flags;
309d3373 13034
293f5f65
L
13035 flags = cpu_flags_or (cpu_arch_flags,
13036 cpu_arch[j].flags);
81486035 13037
5b64d091 13038 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
6305a203
L
13039 {
13040 if (cpu_sub_arch_name)
13041 {
13042 char *name = cpu_sub_arch_name;
13043 cpu_sub_arch_name = concat (name,
91d6fa6a 13044 cpu_arch[j].name,
1bf57e9f 13045 (const char *) NULL);
6305a203
L
13046 free (name);
13047 }
13048 else
91d6fa6a 13049 cpu_sub_arch_name = xstrdup (cpu_arch[j].name);
6305a203 13050 cpu_arch_flags = flags;
a586129e 13051 cpu_arch_isa_flags = flags;
6305a203 13052 }
0089dace
L
13053 else
13054 cpu_arch_isa_flags
13055 = cpu_flags_or (cpu_arch_isa_flags,
13056 cpu_arch[j].flags);
6305a203 13057 break;
ccc9c027 13058 }
9103f4f4 13059 }
6305a203 13060
293f5f65
L
13061 if (j >= ARRAY_SIZE (cpu_arch))
13062 {
33eaf5de 13063 /* Disable an ISA extension. */
293f5f65
L
13064 for (j = 0; j < ARRAY_SIZE (cpu_noarch); j++)
13065 if (strcmp (arch, cpu_noarch [j].name) == 0)
13066 {
13067 i386_cpu_flags flags;
13068
13069 flags = cpu_flags_and_not (cpu_arch_flags,
13070 cpu_noarch[j].flags);
13071 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
13072 {
13073 if (cpu_sub_arch_name)
13074 {
13075 char *name = cpu_sub_arch_name;
13076 cpu_sub_arch_name = concat (arch,
13077 (const char *) NULL);
13078 free (name);
13079 }
13080 else
13081 cpu_sub_arch_name = xstrdup (arch);
13082 cpu_arch_flags = flags;
13083 cpu_arch_isa_flags = flags;
13084 }
13085 break;
13086 }
13087
13088 if (j >= ARRAY_SIZE (cpu_noarch))
13089 j = ARRAY_SIZE (cpu_arch);
13090 }
13091
91d6fa6a 13092 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 13093 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
13094
13095 arch = next;
9103f4f4 13096 }
293f5f65
L
13097 while (next != NULL);
13098 free (saved);
9103f4f4
L
13099 break;
13100
13101 case OPTION_MTUNE:
13102 if (*arg == '.')
2b5d6a91 13103 as_fatal (_("invalid -mtune= option: `%s'"), arg);
91d6fa6a 13104 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 13105 {
91d6fa6a 13106 if (strcmp (arg, cpu_arch [j].name) == 0)
9103f4f4 13107 {
ccc9c027 13108 cpu_arch_tune_set = 1;
91d6fa6a
NC
13109 cpu_arch_tune = cpu_arch [j].type;
13110 cpu_arch_tune_flags = cpu_arch[j].flags;
9103f4f4
L
13111 break;
13112 }
13113 }
91d6fa6a 13114 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 13115 as_fatal (_("invalid -mtune= option: `%s'"), arg);
9103f4f4
L
13116 break;
13117
1efbbeb4
L
13118 case OPTION_MMNEMONIC:
13119 if (strcasecmp (arg, "att") == 0)
13120 intel_mnemonic = 0;
13121 else if (strcasecmp (arg, "intel") == 0)
13122 intel_mnemonic = 1;
13123 else
2b5d6a91 13124 as_fatal (_("invalid -mmnemonic= option: `%s'"), arg);
1efbbeb4
L
13125 break;
13126
13127 case OPTION_MSYNTAX:
13128 if (strcasecmp (arg, "att") == 0)
13129 intel_syntax = 0;
13130 else if (strcasecmp (arg, "intel") == 0)
13131 intel_syntax = 1;
13132 else
2b5d6a91 13133 as_fatal (_("invalid -msyntax= option: `%s'"), arg);
1efbbeb4
L
13134 break;
13135
13136 case OPTION_MINDEX_REG:
13137 allow_index_reg = 1;
13138 break;
13139
13140 case OPTION_MNAKED_REG:
13141 allow_naked_reg = 1;
13142 break;
13143
c0f3af97
L
13144 case OPTION_MSSE2AVX:
13145 sse2avx = 1;
13146 break;
13147
daf50ae7
L
13148 case OPTION_MSSE_CHECK:
13149 if (strcasecmp (arg, "error") == 0)
7bab8ab5 13150 sse_check = check_error;
daf50ae7 13151 else if (strcasecmp (arg, "warning") == 0)
7bab8ab5 13152 sse_check = check_warning;
daf50ae7 13153 else if (strcasecmp (arg, "none") == 0)
7bab8ab5 13154 sse_check = check_none;
daf50ae7 13155 else
2b5d6a91 13156 as_fatal (_("invalid -msse-check= option: `%s'"), arg);
daf50ae7
L
13157 break;
13158
7bab8ab5
JB
13159 case OPTION_MOPERAND_CHECK:
13160 if (strcasecmp (arg, "error") == 0)
13161 operand_check = check_error;
13162 else if (strcasecmp (arg, "warning") == 0)
13163 operand_check = check_warning;
13164 else if (strcasecmp (arg, "none") == 0)
13165 operand_check = check_none;
13166 else
13167 as_fatal (_("invalid -moperand-check= option: `%s'"), arg);
13168 break;
13169
539f890d
L
13170 case OPTION_MAVXSCALAR:
13171 if (strcasecmp (arg, "128") == 0)
13172 avxscalar = vex128;
13173 else if (strcasecmp (arg, "256") == 0)
13174 avxscalar = vex256;
13175 else
2b5d6a91 13176 as_fatal (_("invalid -mavxscalar= option: `%s'"), arg);
539f890d
L
13177 break;
13178
03751133
L
13179 case OPTION_MVEXWIG:
13180 if (strcmp (arg, "0") == 0)
40c9c8de 13181 vexwig = vexw0;
03751133 13182 else if (strcmp (arg, "1") == 0)
40c9c8de 13183 vexwig = vexw1;
03751133
L
13184 else
13185 as_fatal (_("invalid -mvexwig= option: `%s'"), arg);
13186 break;
13187
7e8b059b
L
13188 case OPTION_MADD_BND_PREFIX:
13189 add_bnd_prefix = 1;
13190 break;
13191
43234a1e
L
13192 case OPTION_MEVEXLIG:
13193 if (strcmp (arg, "128") == 0)
13194 evexlig = evexl128;
13195 else if (strcmp (arg, "256") == 0)
13196 evexlig = evexl256;
13197 else if (strcmp (arg, "512") == 0)
13198 evexlig = evexl512;
13199 else
13200 as_fatal (_("invalid -mevexlig= option: `%s'"), arg);
13201 break;
13202
d3d3c6db
IT
13203 case OPTION_MEVEXRCIG:
13204 if (strcmp (arg, "rne") == 0)
13205 evexrcig = rne;
13206 else if (strcmp (arg, "rd") == 0)
13207 evexrcig = rd;
13208 else if (strcmp (arg, "ru") == 0)
13209 evexrcig = ru;
13210 else if (strcmp (arg, "rz") == 0)
13211 evexrcig = rz;
13212 else
13213 as_fatal (_("invalid -mevexrcig= option: `%s'"), arg);
13214 break;
13215
43234a1e
L
13216 case OPTION_MEVEXWIG:
13217 if (strcmp (arg, "0") == 0)
13218 evexwig = evexw0;
13219 else if (strcmp (arg, "1") == 0)
13220 evexwig = evexw1;
13221 else
13222 as_fatal (_("invalid -mevexwig= option: `%s'"), arg);
13223 break;
13224
167ad85b
TG
13225# if defined (TE_PE) || defined (TE_PEP)
13226 case OPTION_MBIG_OBJ:
13227 use_big_obj = 1;
13228 break;
13229#endif
13230
d1982f93 13231 case OPTION_MOMIT_LOCK_PREFIX:
d022bddd
IT
13232 if (strcasecmp (arg, "yes") == 0)
13233 omit_lock_prefix = 1;
13234 else if (strcasecmp (arg, "no") == 0)
13235 omit_lock_prefix = 0;
13236 else
13237 as_fatal (_("invalid -momit-lock-prefix= option: `%s'"), arg);
13238 break;
13239
e4e00185
AS
13240 case OPTION_MFENCE_AS_LOCK_ADD:
13241 if (strcasecmp (arg, "yes") == 0)
13242 avoid_fence = 1;
13243 else if (strcasecmp (arg, "no") == 0)
13244 avoid_fence = 0;
13245 else
13246 as_fatal (_("invalid -mfence-as-lock-add= option: `%s'"), arg);
13247 break;
13248
ae531041
L
13249 case OPTION_MLFENCE_AFTER_LOAD:
13250 if (strcasecmp (arg, "yes") == 0)
13251 lfence_after_load = 1;
13252 else if (strcasecmp (arg, "no") == 0)
13253 lfence_after_load = 0;
13254 else
13255 as_fatal (_("invalid -mlfence-after-load= option: `%s'"), arg);
13256 break;
13257
13258 case OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH:
13259 if (strcasecmp (arg, "all") == 0)
a09f656b 13260 {
13261 lfence_before_indirect_branch = lfence_branch_all;
13262 if (lfence_before_ret == lfence_before_ret_none)
13263 lfence_before_ret = lfence_before_ret_shl;
13264 }
ae531041
L
13265 else if (strcasecmp (arg, "memory") == 0)
13266 lfence_before_indirect_branch = lfence_branch_memory;
13267 else if (strcasecmp (arg, "register") == 0)
13268 lfence_before_indirect_branch = lfence_branch_register;
13269 else if (strcasecmp (arg, "none") == 0)
13270 lfence_before_indirect_branch = lfence_branch_none;
13271 else
13272 as_fatal (_("invalid -mlfence-before-indirect-branch= option: `%s'"),
13273 arg);
13274 break;
13275
13276 case OPTION_MLFENCE_BEFORE_RET:
13277 if (strcasecmp (arg, "or") == 0)
13278 lfence_before_ret = lfence_before_ret_or;
13279 else if (strcasecmp (arg, "not") == 0)
13280 lfence_before_ret = lfence_before_ret_not;
a09f656b 13281 else if (strcasecmp (arg, "shl") == 0 || strcasecmp (arg, "yes") == 0)
13282 lfence_before_ret = lfence_before_ret_shl;
ae531041
L
13283 else if (strcasecmp (arg, "none") == 0)
13284 lfence_before_ret = lfence_before_ret_none;
13285 else
13286 as_fatal (_("invalid -mlfence-before-ret= option: `%s'"),
13287 arg);
13288 break;
13289
0cb4071e
L
13290 case OPTION_MRELAX_RELOCATIONS:
13291 if (strcasecmp (arg, "yes") == 0)
13292 generate_relax_relocations = 1;
13293 else if (strcasecmp (arg, "no") == 0)
13294 generate_relax_relocations = 0;
13295 else
13296 as_fatal (_("invalid -mrelax-relocations= option: `%s'"), arg);
13297 break;
13298
e379e5f3
L
13299 case OPTION_MALIGN_BRANCH_BOUNDARY:
13300 {
13301 char *end;
13302 long int align = strtoul (arg, &end, 0);
13303 if (*end == '\0')
13304 {
13305 if (align == 0)
13306 {
13307 align_branch_power = 0;
13308 break;
13309 }
13310 else if (align >= 16)
13311 {
13312 int align_power;
13313 for (align_power = 0;
13314 (align & 1) == 0;
13315 align >>= 1, align_power++)
13316 continue;
13317 /* Limit alignment power to 31. */
13318 if (align == 1 && align_power < 32)
13319 {
13320 align_branch_power = align_power;
13321 break;
13322 }
13323 }
13324 }
13325 as_fatal (_("invalid -malign-branch-boundary= value: %s"), arg);
13326 }
13327 break;
13328
13329 case OPTION_MALIGN_BRANCH_PREFIX_SIZE:
13330 {
13331 char *end;
13332 int align = strtoul (arg, &end, 0);
13333 /* Some processors only support 5 prefixes. */
13334 if (*end == '\0' && align >= 0 && align < 6)
13335 {
13336 align_branch_prefix_size = align;
13337 break;
13338 }
13339 as_fatal (_("invalid -malign-branch-prefix-size= value: %s"),
13340 arg);
13341 }
13342 break;
13343
13344 case OPTION_MALIGN_BRANCH:
13345 align_branch = 0;
13346 saved = xstrdup (arg);
13347 type = saved;
13348 do
13349 {
13350 next = strchr (type, '+');
13351 if (next)
13352 *next++ = '\0';
13353 if (strcasecmp (type, "jcc") == 0)
13354 align_branch |= align_branch_jcc_bit;
13355 else if (strcasecmp (type, "fused") == 0)
13356 align_branch |= align_branch_fused_bit;
13357 else if (strcasecmp (type, "jmp") == 0)
13358 align_branch |= align_branch_jmp_bit;
13359 else if (strcasecmp (type, "call") == 0)
13360 align_branch |= align_branch_call_bit;
13361 else if (strcasecmp (type, "ret") == 0)
13362 align_branch |= align_branch_ret_bit;
13363 else if (strcasecmp (type, "indirect") == 0)
13364 align_branch |= align_branch_indirect_bit;
13365 else
13366 as_fatal (_("invalid -malign-branch= option: `%s'"), arg);
13367 type = next;
13368 }
13369 while (next != NULL);
13370 free (saved);
13371 break;
13372
76cf450b
L
13373 case OPTION_MBRANCHES_WITH_32B_BOUNDARIES:
13374 align_branch_power = 5;
13375 align_branch_prefix_size = 5;
13376 align_branch = (align_branch_jcc_bit
13377 | align_branch_fused_bit
13378 | align_branch_jmp_bit);
13379 break;
13380
5db04b09 13381 case OPTION_MAMD64:
4b5aaf5f 13382 isa64 = amd64;
5db04b09
L
13383 break;
13384
13385 case OPTION_MINTEL64:
4b5aaf5f 13386 isa64 = intel64;
5db04b09
L
13387 break;
13388
b6f8c7c4
L
13389 case 'O':
13390 if (arg == NULL)
13391 {
13392 optimize = 1;
13393 /* Turn off -Os. */
13394 optimize_for_space = 0;
13395 }
13396 else if (*arg == 's')
13397 {
13398 optimize_for_space = 1;
13399 /* Turn on all encoding optimizations. */
41fd2579 13400 optimize = INT_MAX;
b6f8c7c4
L
13401 }
13402 else
13403 {
13404 optimize = atoi (arg);
13405 /* Turn off -Os. */
13406 optimize_for_space = 0;
13407 }
13408 break;
13409
252b5132
RH
13410 default:
13411 return 0;
13412 }
13413 return 1;
13414}
13415
8a2c8fef
L
13416#define MESSAGE_TEMPLATE \
13417" "
13418
293f5f65
L
13419static char *
13420output_message (FILE *stream, char *p, char *message, char *start,
13421 int *left_p, const char *name, int len)
13422{
13423 int size = sizeof (MESSAGE_TEMPLATE);
13424 int left = *left_p;
13425
13426 /* Reserve 2 spaces for ", " or ",\0" */
13427 left -= len + 2;
13428
13429 /* Check if there is any room. */
13430 if (left >= 0)
13431 {
13432 if (p != start)
13433 {
13434 *p++ = ',';
13435 *p++ = ' ';
13436 }
13437 p = mempcpy (p, name, len);
13438 }
13439 else
13440 {
13441 /* Output the current message now and start a new one. */
13442 *p++ = ',';
13443 *p = '\0';
13444 fprintf (stream, "%s\n", message);
13445 p = start;
13446 left = size - (start - message) - len - 2;
13447
13448 gas_assert (left >= 0);
13449
13450 p = mempcpy (p, name, len);
13451 }
13452
13453 *left_p = left;
13454 return p;
13455}
13456
8a2c8fef 13457static void
1ded5609 13458show_arch (FILE *stream, int ext, int check)
8a2c8fef
L
13459{
13460 static char message[] = MESSAGE_TEMPLATE;
13461 char *start = message + 27;
13462 char *p;
13463 int size = sizeof (MESSAGE_TEMPLATE);
13464 int left;
13465 const char *name;
13466 int len;
13467 unsigned int j;
13468
13469 p = start;
13470 left = size - (start - message);
13471 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13472 {
13473 /* Should it be skipped? */
13474 if (cpu_arch [j].skip)
13475 continue;
13476
13477 name = cpu_arch [j].name;
13478 len = cpu_arch [j].len;
13479 if (*name == '.')
13480 {
13481 /* It is an extension. Skip if we aren't asked to show it. */
13482 if (ext)
13483 {
13484 name++;
13485 len--;
13486 }
13487 else
13488 continue;
13489 }
13490 else if (ext)
13491 {
13492 /* It is an processor. Skip if we show only extension. */
13493 continue;
13494 }
1ded5609
JB
13495 else if (check && ! cpu_arch[j].flags.bitfield.cpui386)
13496 {
13497 /* It is an impossible processor - skip. */
13498 continue;
13499 }
8a2c8fef 13500
293f5f65 13501 p = output_message (stream, p, message, start, &left, name, len);
8a2c8fef
L
13502 }
13503
293f5f65
L
13504 /* Display disabled extensions. */
13505 if (ext)
13506 for (j = 0; j < ARRAY_SIZE (cpu_noarch); j++)
13507 {
13508 name = cpu_noarch [j].name;
13509 len = cpu_noarch [j].len;
13510 p = output_message (stream, p, message, start, &left, name,
13511 len);
13512 }
13513
8a2c8fef
L
13514 *p = '\0';
13515 fprintf (stream, "%s\n", message);
13516}
13517
252b5132 13518void
8a2c8fef 13519md_show_usage (FILE *stream)
252b5132 13520{
4cc782b5
ILT
13521#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13522 fprintf (stream, _("\
d4693039 13523 -Qy, -Qn ignored\n\
a38cf1db 13524 -V print assembler version number\n\
b3b91714
AM
13525 -k ignored\n"));
13526#endif
13527 fprintf (stream, _("\
12b55ccc 13528 -n Do not optimize code alignment\n\
b3b91714
AM
13529 -q quieten some warnings\n"));
13530#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13531 fprintf (stream, _("\
a38cf1db 13532 -s ignored\n"));
b3b91714 13533#endif
d7f449c0
L
13534#if defined BFD64 && (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
13535 || defined (TE_PE) || defined (TE_PEP))
751d281c 13536 fprintf (stream, _("\
570561f7 13537 --32/--64/--x32 generate 32bit/64bit/x32 code\n"));
751d281c 13538#endif
b3b91714
AM
13539#ifdef SVR4_COMMENT_CHARS
13540 fprintf (stream, _("\
13541 --divide do not treat `/' as a comment character\n"));
a38cf1db
AM
13542#else
13543 fprintf (stream, _("\
b3b91714 13544 --divide ignored\n"));
4cc782b5 13545#endif
9103f4f4 13546 fprintf (stream, _("\
6305a203 13547 -march=CPU[,+EXTENSION...]\n\
8a2c8fef 13548 generate code for CPU and EXTENSION, CPU is one of:\n"));
1ded5609 13549 show_arch (stream, 0, 1);
8a2c8fef
L
13550 fprintf (stream, _("\
13551 EXTENSION is combination of:\n"));
1ded5609 13552 show_arch (stream, 1, 0);
6305a203 13553 fprintf (stream, _("\
8a2c8fef 13554 -mtune=CPU optimize for CPU, CPU is one of:\n"));
1ded5609 13555 show_arch (stream, 0, 0);
ba104c83 13556 fprintf (stream, _("\
c0f3af97
L
13557 -msse2avx encode SSE instructions with VEX prefix\n"));
13558 fprintf (stream, _("\
7c5c05ef 13559 -msse-check=[none|error|warning] (default: warning)\n\
daf50ae7
L
13560 check SSE instructions\n"));
13561 fprintf (stream, _("\
7c5c05ef 13562 -moperand-check=[none|error|warning] (default: warning)\n\
7bab8ab5
JB
13563 check operand combinations for validity\n"));
13564 fprintf (stream, _("\
7c5c05ef
L
13565 -mavxscalar=[128|256] (default: 128)\n\
13566 encode scalar AVX instructions with specific vector\n\
539f890d
L
13567 length\n"));
13568 fprintf (stream, _("\
03751133
L
13569 -mvexwig=[0|1] (default: 0)\n\
13570 encode VEX instructions with specific VEX.W value\n\
13571 for VEX.W bit ignored instructions\n"));
13572 fprintf (stream, _("\
7c5c05ef
L
13573 -mevexlig=[128|256|512] (default: 128)\n\
13574 encode scalar EVEX instructions with specific vector\n\
43234a1e
L
13575 length\n"));
13576 fprintf (stream, _("\
7c5c05ef
L
13577 -mevexwig=[0|1] (default: 0)\n\
13578 encode EVEX instructions with specific EVEX.W value\n\
43234a1e
L
13579 for EVEX.W bit ignored instructions\n"));
13580 fprintf (stream, _("\
7c5c05ef 13581 -mevexrcig=[rne|rd|ru|rz] (default: rne)\n\
d3d3c6db
IT
13582 encode EVEX instructions with specific EVEX.RC value\n\
13583 for SAE-only ignored instructions\n"));
13584 fprintf (stream, _("\
7c5c05ef
L
13585 -mmnemonic=[att|intel] "));
13586 if (SYSV386_COMPAT)
13587 fprintf (stream, _("(default: att)\n"));
13588 else
13589 fprintf (stream, _("(default: intel)\n"));
13590 fprintf (stream, _("\
13591 use AT&T/Intel mnemonic\n"));
ba104c83 13592 fprintf (stream, _("\
7c5c05ef
L
13593 -msyntax=[att|intel] (default: att)\n\
13594 use AT&T/Intel syntax\n"));
ba104c83
L
13595 fprintf (stream, _("\
13596 -mindex-reg support pseudo index registers\n"));
13597 fprintf (stream, _("\
13598 -mnaked-reg don't require `%%' prefix for registers\n"));
13599 fprintf (stream, _("\
7e8b059b 13600 -madd-bnd-prefix add BND prefix for all valid branches\n"));
b4a3a7b4 13601#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8dcea932
L
13602 fprintf (stream, _("\
13603 -mshared disable branch optimization for shared code\n"));
b4a3a7b4
L
13604 fprintf (stream, _("\
13605 -mx86-used-note=[no|yes] "));
13606 if (DEFAULT_X86_USED_NOTE)
13607 fprintf (stream, _("(default: yes)\n"));
13608 else
13609 fprintf (stream, _("(default: no)\n"));
13610 fprintf (stream, _("\
13611 generate x86 used ISA and feature properties\n"));
13612#endif
13613#if defined (TE_PE) || defined (TE_PEP)
167ad85b
TG
13614 fprintf (stream, _("\
13615 -mbig-obj generate big object files\n"));
13616#endif
d022bddd 13617 fprintf (stream, _("\
7c5c05ef 13618 -momit-lock-prefix=[no|yes] (default: no)\n\
d022bddd 13619 strip all lock prefixes\n"));
5db04b09 13620 fprintf (stream, _("\
7c5c05ef 13621 -mfence-as-lock-add=[no|yes] (default: no)\n\
e4e00185
AS
13622 encode lfence, mfence and sfence as\n\
13623 lock addl $0x0, (%%{re}sp)\n"));
13624 fprintf (stream, _("\
7c5c05ef
L
13625 -mrelax-relocations=[no|yes] "));
13626 if (DEFAULT_GENERATE_X86_RELAX_RELOCATIONS)
13627 fprintf (stream, _("(default: yes)\n"));
13628 else
13629 fprintf (stream, _("(default: no)\n"));
13630 fprintf (stream, _("\
0cb4071e
L
13631 generate relax relocations\n"));
13632 fprintf (stream, _("\
e379e5f3
L
13633 -malign-branch-boundary=NUM (default: 0)\n\
13634 align branches within NUM byte boundary\n"));
13635 fprintf (stream, _("\
13636 -malign-branch=TYPE[+TYPE...] (default: jcc+fused+jmp)\n\
13637 TYPE is combination of jcc, fused, jmp, call, ret,\n\
13638 indirect\n\
13639 specify types of branches to align\n"));
13640 fprintf (stream, _("\
13641 -malign-branch-prefix-size=NUM (default: 5)\n\
13642 align branches with NUM prefixes per instruction\n"));
13643 fprintf (stream, _("\
76cf450b
L
13644 -mbranches-within-32B-boundaries\n\
13645 align branches within 32 byte boundary\n"));
13646 fprintf (stream, _("\
ae531041
L
13647 -mlfence-after-load=[no|yes] (default: no)\n\
13648 generate lfence after load\n"));
13649 fprintf (stream, _("\
13650 -mlfence-before-indirect-branch=[none|all|register|memory] (default: none)\n\
13651 generate lfence before indirect near branch\n"));
13652 fprintf (stream, _("\
a09f656b 13653 -mlfence-before-ret=[none|or|not|shl|yes] (default: none)\n\
ae531041
L
13654 generate lfence before ret\n"));
13655 fprintf (stream, _("\
7c5c05ef 13656 -mamd64 accept only AMD64 ISA [default]\n"));
5db04b09
L
13657 fprintf (stream, _("\
13658 -mintel64 accept only Intel64 ISA\n"));
252b5132
RH
13659}
13660
3e73aa7c 13661#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
321098a5 13662 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
e57f8c65 13663 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
252b5132
RH
13664
13665/* Pick the target format to use. */
13666
47926f60 13667const char *
e3bb37b5 13668i386_target_format (void)
252b5132 13669{
351f65ca
L
13670 if (!strncmp (default_arch, "x86_64", 6))
13671 {
13672 update_code_flag (CODE_64BIT, 1);
13673 if (default_arch[6] == '\0')
7f56bc95 13674 x86_elf_abi = X86_64_ABI;
351f65ca 13675 else
7f56bc95 13676 x86_elf_abi = X86_64_X32_ABI;
351f65ca 13677 }
3e73aa7c 13678 else if (!strcmp (default_arch, "i386"))
78f12dd3 13679 update_code_flag (CODE_32BIT, 1);
5197d474
L
13680 else if (!strcmp (default_arch, "iamcu"))
13681 {
13682 update_code_flag (CODE_32BIT, 1);
13683 if (cpu_arch_isa == PROCESSOR_UNKNOWN)
13684 {
13685 static const i386_cpu_flags iamcu_flags = CPU_IAMCU_FLAGS;
13686 cpu_arch_name = "iamcu";
13687 cpu_sub_arch_name = NULL;
13688 cpu_arch_flags = iamcu_flags;
13689 cpu_arch_isa = PROCESSOR_IAMCU;
13690 cpu_arch_isa_flags = iamcu_flags;
13691 if (!cpu_arch_tune_set)
13692 {
13693 cpu_arch_tune = cpu_arch_isa;
13694 cpu_arch_tune_flags = cpu_arch_isa_flags;
13695 }
13696 }
8d471ec1 13697 else if (cpu_arch_isa != PROCESSOR_IAMCU)
5197d474
L
13698 as_fatal (_("Intel MCU doesn't support `%s' architecture"),
13699 cpu_arch_name);
13700 }
3e73aa7c 13701 else
2b5d6a91 13702 as_fatal (_("unknown architecture"));
89507696
JB
13703
13704 if (cpu_flags_all_zero (&cpu_arch_isa_flags))
13705 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].flags;
13706 if (cpu_flags_all_zero (&cpu_arch_tune_flags))
13707 cpu_arch_tune_flags = cpu_arch[flag_code == CODE_64BIT].flags;
13708
252b5132
RH
13709 switch (OUTPUT_FLAVOR)
13710 {
9384f2ff 13711#if defined (OBJ_MAYBE_AOUT) || defined (OBJ_AOUT)
4c63da97 13712 case bfd_target_aout_flavour:
47926f60 13713 return AOUT_TARGET_FORMAT;
4c63da97 13714#endif
9384f2ff
AM
13715#if defined (OBJ_MAYBE_COFF) || defined (OBJ_COFF)
13716# if defined (TE_PE) || defined (TE_PEP)
13717 case bfd_target_coff_flavour:
167ad85b
TG
13718 if (flag_code == CODE_64BIT)
13719 return use_big_obj ? "pe-bigobj-x86-64" : "pe-x86-64";
13720 else
251dae91 13721 return use_big_obj ? "pe-bigobj-i386" : "pe-i386";
9384f2ff 13722# elif defined (TE_GO32)
0561d57c
JK
13723 case bfd_target_coff_flavour:
13724 return "coff-go32";
9384f2ff 13725# else
252b5132
RH
13726 case bfd_target_coff_flavour:
13727 return "coff-i386";
9384f2ff 13728# endif
4c63da97 13729#endif
3e73aa7c 13730#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
252b5132 13731 case bfd_target_elf_flavour:
3e73aa7c 13732 {
351f65ca
L
13733 const char *format;
13734
13735 switch (x86_elf_abi)
4fa24527 13736 {
351f65ca
L
13737 default:
13738 format = ELF_TARGET_FORMAT;
e379e5f3
L
13739#ifndef TE_SOLARIS
13740 tls_get_addr = "___tls_get_addr";
13741#endif
351f65ca 13742 break;
7f56bc95 13743 case X86_64_ABI:
351f65ca 13744 use_rela_relocations = 1;
4fa24527 13745 object_64bit = 1;
e379e5f3
L
13746#ifndef TE_SOLARIS
13747 tls_get_addr = "__tls_get_addr";
13748#endif
351f65ca
L
13749 format = ELF_TARGET_FORMAT64;
13750 break;
7f56bc95 13751 case X86_64_X32_ABI:
4fa24527 13752 use_rela_relocations = 1;
351f65ca 13753 object_64bit = 1;
e379e5f3
L
13754#ifndef TE_SOLARIS
13755 tls_get_addr = "__tls_get_addr";
13756#endif
862be3fb 13757 disallow_64bit_reloc = 1;
351f65ca
L
13758 format = ELF_TARGET_FORMAT32;
13759 break;
4fa24527 13760 }
3632d14b 13761 if (cpu_arch_isa == PROCESSOR_L1OM)
8a9036a4 13762 {
7f56bc95 13763 if (x86_elf_abi != X86_64_ABI)
8a9036a4
L
13764 as_fatal (_("Intel L1OM is 64bit only"));
13765 return ELF_TARGET_L1OM_FORMAT;
13766 }
b49f93f6 13767 else if (cpu_arch_isa == PROCESSOR_K1OM)
7a9068fe
L
13768 {
13769 if (x86_elf_abi != X86_64_ABI)
13770 as_fatal (_("Intel K1OM is 64bit only"));
13771 return ELF_TARGET_K1OM_FORMAT;
13772 }
81486035
L
13773 else if (cpu_arch_isa == PROCESSOR_IAMCU)
13774 {
13775 if (x86_elf_abi != I386_ABI)
13776 as_fatal (_("Intel MCU is 32bit only"));
13777 return ELF_TARGET_IAMCU_FORMAT;
13778 }
8a9036a4 13779 else
351f65ca 13780 return format;
3e73aa7c 13781 }
e57f8c65
TG
13782#endif
13783#if defined (OBJ_MACH_O)
13784 case bfd_target_mach_o_flavour:
d382c579
TG
13785 if (flag_code == CODE_64BIT)
13786 {
13787 use_rela_relocations = 1;
13788 object_64bit = 1;
13789 return "mach-o-x86-64";
13790 }
13791 else
13792 return "mach-o-i386";
4c63da97 13793#endif
252b5132
RH
13794 default:
13795 abort ();
13796 return NULL;
13797 }
13798}
13799
47926f60 13800#endif /* OBJ_MAYBE_ more than one */
252b5132 13801\f
252b5132 13802symbolS *
7016a5d5 13803md_undefined_symbol (char *name)
252b5132 13804{
18dc2407
ILT
13805 if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
13806 && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
13807 && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
13808 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
24eab124
AM
13809 {
13810 if (!GOT_symbol)
13811 {
13812 if (symbol_find (name))
13813 as_bad (_("GOT already in symbol table"));
13814 GOT_symbol = symbol_new (name, undefined_section,
e01e1cee 13815 &zero_address_frag, 0);
24eab124
AM
13816 };
13817 return GOT_symbol;
13818 }
252b5132
RH
13819 return 0;
13820}
13821
13822/* Round up a section size to the appropriate boundary. */
47926f60 13823
252b5132 13824valueT
7016a5d5 13825md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
252b5132 13826{
4c63da97
AM
13827#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
13828 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
13829 {
13830 /* For a.out, force the section size to be aligned. If we don't do
13831 this, BFD will align it for us, but it will not write out the
13832 final bytes of the section. This may be a bug in BFD, but it is
13833 easier to fix it here since that is how the other a.out targets
13834 work. */
13835 int align;
13836
fd361982 13837 align = bfd_section_alignment (segment);
8d3842cd 13838 size = ((size + (1 << align) - 1) & (-((valueT) 1 << align)));
4c63da97 13839 }
252b5132
RH
13840#endif
13841
13842 return size;
13843}
13844
13845/* On the i386, PC-relative offsets are relative to the start of the
13846 next instruction. That is, the address of the offset, plus its
13847 size, since the offset is always the last part of the insn. */
13848
13849long
e3bb37b5 13850md_pcrel_from (fixS *fixP)
252b5132
RH
13851{
13852 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
13853}
13854
13855#ifndef I386COFF
13856
13857static void
e3bb37b5 13858s_bss (int ignore ATTRIBUTE_UNUSED)
252b5132 13859{
29b0f896 13860 int temp;
252b5132 13861
8a75718c
JB
13862#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13863 if (IS_ELF)
13864 obj_elf_section_change_hook ();
13865#endif
252b5132
RH
13866 temp = get_absolute_expression ();
13867 subseg_set (bss_section, (subsegT) temp);
13868 demand_empty_rest_of_line ();
13869}
13870
13871#endif
13872
e379e5f3
L
13873/* Remember constant directive. */
13874
13875void
13876i386_cons_align (int ignore ATTRIBUTE_UNUSED)
13877{
13878 if (last_insn.kind != last_insn_directive
13879 && (bfd_section_flags (now_seg) & SEC_CODE))
13880 {
13881 last_insn.seg = now_seg;
13882 last_insn.kind = last_insn_directive;
13883 last_insn.name = "constant directive";
13884 last_insn.file = as_where (&last_insn.line);
ae531041
L
13885 if (lfence_before_ret != lfence_before_ret_none)
13886 {
13887 if (lfence_before_indirect_branch != lfence_branch_none)
13888 as_warn (_("constant directive skips -mlfence-before-ret "
13889 "and -mlfence-before-indirect-branch"));
13890 else
13891 as_warn (_("constant directive skips -mlfence-before-ret"));
13892 }
13893 else if (lfence_before_indirect_branch != lfence_branch_none)
13894 as_warn (_("constant directive skips -mlfence-before-indirect-branch"));
e379e5f3
L
13895 }
13896}
13897
252b5132 13898void
e3bb37b5 13899i386_validate_fix (fixS *fixp)
252b5132 13900{
02a86693 13901 if (fixp->fx_subsy)
252b5132 13902 {
02a86693 13903 if (fixp->fx_subsy == GOT_symbol)
23df1078 13904 {
02a86693
L
13905 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
13906 {
13907 if (!object_64bit)
13908 abort ();
13909#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13910 if (fixp->fx_tcbit2)
56ceb5b5
L
13911 fixp->fx_r_type = (fixp->fx_tcbit
13912 ? BFD_RELOC_X86_64_REX_GOTPCRELX
13913 : BFD_RELOC_X86_64_GOTPCRELX);
02a86693
L
13914 else
13915#endif
13916 fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
13917 }
d6ab8113 13918 else
02a86693
L
13919 {
13920 if (!object_64bit)
13921 fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
13922 else
13923 fixp->fx_r_type = BFD_RELOC_X86_64_GOTOFF64;
13924 }
13925 fixp->fx_subsy = 0;
23df1078 13926 }
252b5132 13927 }
02a86693 13928#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2585b7a5 13929 else
02a86693 13930 {
2585b7a5
L
13931 /* NB: Commit 292676c1 resolved PLT32 reloc aganst local symbol
13932 to section. Since PLT32 relocation must be against symbols,
13933 turn such PLT32 relocation into PC32 relocation. */
13934 if (fixp->fx_addsy
13935 && (fixp->fx_r_type == BFD_RELOC_386_PLT32
13936 || fixp->fx_r_type == BFD_RELOC_X86_64_PLT32)
13937 && symbol_section_p (fixp->fx_addsy))
13938 fixp->fx_r_type = BFD_RELOC_32_PCREL;
13939 if (!object_64bit)
13940 {
13941 if (fixp->fx_r_type == BFD_RELOC_386_GOT32
13942 && fixp->fx_tcbit2)
13943 fixp->fx_r_type = BFD_RELOC_386_GOT32X;
13944 }
02a86693
L
13945 }
13946#endif
252b5132
RH
13947}
13948
252b5132 13949arelent *
7016a5d5 13950tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
13951{
13952 arelent *rel;
13953 bfd_reloc_code_real_type code;
13954
13955 switch (fixp->fx_r_type)
13956 {
8ce3d284 13957#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
13958 case BFD_RELOC_SIZE32:
13959 case BFD_RELOC_SIZE64:
13960 if (S_IS_DEFINED (fixp->fx_addsy)
13961 && !S_IS_EXTERNAL (fixp->fx_addsy))
13962 {
13963 /* Resolve size relocation against local symbol to size of
13964 the symbol plus addend. */
13965 valueT value = S_GET_SIZE (fixp->fx_addsy) + fixp->fx_offset;
13966 if (fixp->fx_r_type == BFD_RELOC_SIZE32
13967 && !fits_in_unsigned_long (value))
13968 as_bad_where (fixp->fx_file, fixp->fx_line,
13969 _("symbol size computation overflow"));
13970 fixp->fx_addsy = NULL;
13971 fixp->fx_subsy = NULL;
13972 md_apply_fix (fixp, (valueT *) &value, NULL);
13973 return NULL;
13974 }
8ce3d284 13975#endif
1a0670f3 13976 /* Fall through. */
8fd4256d 13977
3e73aa7c
JH
13978 case BFD_RELOC_X86_64_PLT32:
13979 case BFD_RELOC_X86_64_GOT32:
13980 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
13981 case BFD_RELOC_X86_64_GOTPCRELX:
13982 case BFD_RELOC_X86_64_REX_GOTPCRELX:
252b5132
RH
13983 case BFD_RELOC_386_PLT32:
13984 case BFD_RELOC_386_GOT32:
02a86693 13985 case BFD_RELOC_386_GOT32X:
252b5132
RH
13986 case BFD_RELOC_386_GOTOFF:
13987 case BFD_RELOC_386_GOTPC:
13ae64f3
JJ
13988 case BFD_RELOC_386_TLS_GD:
13989 case BFD_RELOC_386_TLS_LDM:
13990 case BFD_RELOC_386_TLS_LDO_32:
13991 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
13992 case BFD_RELOC_386_TLS_IE:
13993 case BFD_RELOC_386_TLS_GOTIE:
13ae64f3
JJ
13994 case BFD_RELOC_386_TLS_LE_32:
13995 case BFD_RELOC_386_TLS_LE:
67a4f2b7
AO
13996 case BFD_RELOC_386_TLS_GOTDESC:
13997 case BFD_RELOC_386_TLS_DESC_CALL:
bffbf940
JJ
13998 case BFD_RELOC_X86_64_TLSGD:
13999 case BFD_RELOC_X86_64_TLSLD:
14000 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 14001 case BFD_RELOC_X86_64_DTPOFF64:
bffbf940
JJ
14002 case BFD_RELOC_X86_64_GOTTPOFF:
14003 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113
JB
14004 case BFD_RELOC_X86_64_TPOFF64:
14005 case BFD_RELOC_X86_64_GOTOFF64:
14006 case BFD_RELOC_X86_64_GOTPC32:
7b81dfbb
AJ
14007 case BFD_RELOC_X86_64_GOT64:
14008 case BFD_RELOC_X86_64_GOTPCREL64:
14009 case BFD_RELOC_X86_64_GOTPC64:
14010 case BFD_RELOC_X86_64_GOTPLT64:
14011 case BFD_RELOC_X86_64_PLTOFF64:
67a4f2b7
AO
14012 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14013 case BFD_RELOC_X86_64_TLSDESC_CALL:
252b5132
RH
14014 case BFD_RELOC_RVA:
14015 case BFD_RELOC_VTABLE_ENTRY:
14016 case BFD_RELOC_VTABLE_INHERIT:
6482c264
NC
14017#ifdef TE_PE
14018 case BFD_RELOC_32_SECREL:
14019#endif
252b5132
RH
14020 code = fixp->fx_r_type;
14021 break;
dbbaec26
L
14022 case BFD_RELOC_X86_64_32S:
14023 if (!fixp->fx_pcrel)
14024 {
14025 /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32. */
14026 code = fixp->fx_r_type;
14027 break;
14028 }
1a0670f3 14029 /* Fall through. */
252b5132 14030 default:
93382f6d 14031 if (fixp->fx_pcrel)
252b5132 14032 {
93382f6d
AM
14033 switch (fixp->fx_size)
14034 {
14035 default:
b091f402
AM
14036 as_bad_where (fixp->fx_file, fixp->fx_line,
14037 _("can not do %d byte pc-relative relocation"),
14038 fixp->fx_size);
93382f6d
AM
14039 code = BFD_RELOC_32_PCREL;
14040 break;
14041 case 1: code = BFD_RELOC_8_PCREL; break;
14042 case 2: code = BFD_RELOC_16_PCREL; break;
d258b828 14043 case 4: code = BFD_RELOC_32_PCREL; break;
d6ab8113
JB
14044#ifdef BFD64
14045 case 8: code = BFD_RELOC_64_PCREL; break;
14046#endif
93382f6d
AM
14047 }
14048 }
14049 else
14050 {
14051 switch (fixp->fx_size)
14052 {
14053 default:
b091f402
AM
14054 as_bad_where (fixp->fx_file, fixp->fx_line,
14055 _("can not do %d byte relocation"),
14056 fixp->fx_size);
93382f6d
AM
14057 code = BFD_RELOC_32;
14058 break;
14059 case 1: code = BFD_RELOC_8; break;
14060 case 2: code = BFD_RELOC_16; break;
14061 case 4: code = BFD_RELOC_32; break;
937149dd 14062#ifdef BFD64
3e73aa7c 14063 case 8: code = BFD_RELOC_64; break;
937149dd 14064#endif
93382f6d 14065 }
252b5132
RH
14066 }
14067 break;
14068 }
252b5132 14069
d182319b
JB
14070 if ((code == BFD_RELOC_32
14071 || code == BFD_RELOC_32_PCREL
14072 || code == BFD_RELOC_X86_64_32S)
252b5132
RH
14073 && GOT_symbol
14074 && fixp->fx_addsy == GOT_symbol)
3e73aa7c 14075 {
4fa24527 14076 if (!object_64bit)
d6ab8113
JB
14077 code = BFD_RELOC_386_GOTPC;
14078 else
14079 code = BFD_RELOC_X86_64_GOTPC32;
3e73aa7c 14080 }
7b81dfbb
AJ
14081 if ((code == BFD_RELOC_64 || code == BFD_RELOC_64_PCREL)
14082 && GOT_symbol
14083 && fixp->fx_addsy == GOT_symbol)
14084 {
14085 code = BFD_RELOC_X86_64_GOTPC64;
14086 }
252b5132 14087
add39d23
TS
14088 rel = XNEW (arelent);
14089 rel->sym_ptr_ptr = XNEW (asymbol *);
49309057 14090 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
14091
14092 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
c87db184 14093
3e73aa7c
JH
14094 if (!use_rela_relocations)
14095 {
14096 /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
14097 vtable entry to be used in the relocation's section offset. */
14098 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
14099 rel->address = fixp->fx_offset;
fbeb56a4
DK
14100#if defined (OBJ_COFF) && defined (TE_PE)
14101 else if (fixp->fx_addsy && S_IS_WEAK (fixp->fx_addsy))
14102 rel->addend = fixp->fx_addnumber - (S_GET_VALUE (fixp->fx_addsy) * 2);
14103 else
14104#endif
c6682705 14105 rel->addend = 0;
3e73aa7c
JH
14106 }
14107 /* Use the rela in 64bit mode. */
252b5132 14108 else
3e73aa7c 14109 {
862be3fb
L
14110 if (disallow_64bit_reloc)
14111 switch (code)
14112 {
862be3fb
L
14113 case BFD_RELOC_X86_64_DTPOFF64:
14114 case BFD_RELOC_X86_64_TPOFF64:
14115 case BFD_RELOC_64_PCREL:
14116 case BFD_RELOC_X86_64_GOTOFF64:
14117 case BFD_RELOC_X86_64_GOT64:
14118 case BFD_RELOC_X86_64_GOTPCREL64:
14119 case BFD_RELOC_X86_64_GOTPC64:
14120 case BFD_RELOC_X86_64_GOTPLT64:
14121 case BFD_RELOC_X86_64_PLTOFF64:
14122 as_bad_where (fixp->fx_file, fixp->fx_line,
14123 _("cannot represent relocation type %s in x32 mode"),
14124 bfd_get_reloc_code_name (code));
14125 break;
14126 default:
14127 break;
14128 }
14129
062cd5e7
AS
14130 if (!fixp->fx_pcrel)
14131 rel->addend = fixp->fx_offset;
14132 else
14133 switch (code)
14134 {
14135 case BFD_RELOC_X86_64_PLT32:
14136 case BFD_RELOC_X86_64_GOT32:
14137 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
14138 case BFD_RELOC_X86_64_GOTPCRELX:
14139 case BFD_RELOC_X86_64_REX_GOTPCRELX:
bffbf940
JJ
14140 case BFD_RELOC_X86_64_TLSGD:
14141 case BFD_RELOC_X86_64_TLSLD:
14142 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7
AO
14143 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14144 case BFD_RELOC_X86_64_TLSDESC_CALL:
062cd5e7
AS
14145 rel->addend = fixp->fx_offset - fixp->fx_size;
14146 break;
14147 default:
14148 rel->addend = (section->vma
14149 - fixp->fx_size
14150 + fixp->fx_addnumber
14151 + md_pcrel_from (fixp));
14152 break;
14153 }
3e73aa7c
JH
14154 }
14155
252b5132
RH
14156 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
14157 if (rel->howto == NULL)
14158 {
14159 as_bad_where (fixp->fx_file, fixp->fx_line,
d0b47220 14160 _("cannot represent relocation type %s"),
252b5132
RH
14161 bfd_get_reloc_code_name (code));
14162 /* Set howto to a garbage value so that we can keep going. */
14163 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
9c2799c2 14164 gas_assert (rel->howto != NULL);
252b5132
RH
14165 }
14166
14167 return rel;
14168}
14169
ee86248c 14170#include "tc-i386-intel.c"
54cfded0 14171
a60de03c
JB
14172void
14173tc_x86_parse_to_dw2regnum (expressionS *exp)
54cfded0 14174{
a60de03c
JB
14175 int saved_naked_reg;
14176 char saved_register_dot;
54cfded0 14177
a60de03c
JB
14178 saved_naked_reg = allow_naked_reg;
14179 allow_naked_reg = 1;
14180 saved_register_dot = register_chars['.'];
14181 register_chars['.'] = '.';
14182 allow_pseudo_reg = 1;
14183 expression_and_evaluate (exp);
14184 allow_pseudo_reg = 0;
14185 register_chars['.'] = saved_register_dot;
14186 allow_naked_reg = saved_naked_reg;
14187
e96d56a1 14188 if (exp->X_op == O_register && exp->X_add_number >= 0)
54cfded0 14189 {
a60de03c
JB
14190 if ((addressT) exp->X_add_number < i386_regtab_size)
14191 {
14192 exp->X_op = O_constant;
14193 exp->X_add_number = i386_regtab[exp->X_add_number]
14194 .dw2_regnum[flag_code >> 1];
14195 }
14196 else
14197 exp->X_op = O_illegal;
54cfded0 14198 }
54cfded0
AM
14199}
14200
14201void
14202tc_x86_frame_initial_instructions (void)
14203{
a60de03c
JB
14204 static unsigned int sp_regno[2];
14205
14206 if (!sp_regno[flag_code >> 1])
14207 {
14208 char *saved_input = input_line_pointer;
14209 char sp[][4] = {"esp", "rsp"};
14210 expressionS exp;
a4447b93 14211
a60de03c
JB
14212 input_line_pointer = sp[flag_code >> 1];
14213 tc_x86_parse_to_dw2regnum (&exp);
9c2799c2 14214 gas_assert (exp.X_op == O_constant);
a60de03c
JB
14215 sp_regno[flag_code >> 1] = exp.X_add_number;
14216 input_line_pointer = saved_input;
14217 }
a4447b93 14218
61ff971f
L
14219 cfi_add_CFA_def_cfa (sp_regno[flag_code >> 1], -x86_cie_data_alignment);
14220 cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
54cfded0 14221}
d2b2c203 14222
d7921315
L
14223int
14224x86_dwarf2_addr_size (void)
14225{
14226#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
14227 if (x86_elf_abi == X86_64_X32_ABI)
14228 return 4;
14229#endif
14230 return bfd_arch_bits_per_address (stdoutput) / 8;
14231}
14232
d2b2c203
DJ
14233int
14234i386_elf_section_type (const char *str, size_t len)
14235{
14236 if (flag_code == CODE_64BIT
14237 && len == sizeof ("unwind") - 1
14238 && strncmp (str, "unwind", 6) == 0)
14239 return SHT_X86_64_UNWIND;
14240
14241 return -1;
14242}
bb41ade5 14243
ad5fec3b
EB
14244#ifdef TE_SOLARIS
14245void
14246i386_solaris_fix_up_eh_frame (segT sec)
14247{
14248 if (flag_code == CODE_64BIT)
14249 elf_section_type (sec) = SHT_X86_64_UNWIND;
14250}
14251#endif
14252
bb41ade5
AM
14253#ifdef TE_PE
14254void
14255tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
14256{
91d6fa6a 14257 expressionS exp;
bb41ade5 14258
91d6fa6a
NC
14259 exp.X_op = O_secrel;
14260 exp.X_add_symbol = symbol;
14261 exp.X_add_number = 0;
14262 emit_expr (&exp, size);
bb41ade5
AM
14263}
14264#endif
3b22753a
L
14265
14266#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14267/* For ELF on x86-64, add support for SHF_X86_64_LARGE. */
14268
01e1a5bc 14269bfd_vma
6d4af3c2 14270x86_64_section_letter (int letter, const char **ptr_msg)
3b22753a
L
14271{
14272 if (flag_code == CODE_64BIT)
14273 {
14274 if (letter == 'l')
14275 return SHF_X86_64_LARGE;
14276
8f3bae45 14277 *ptr_msg = _("bad .section directive: want a,l,w,x,M,S,G,T in string");
64e74474 14278 }
3b22753a 14279 else
8f3bae45 14280 *ptr_msg = _("bad .section directive: want a,w,x,M,S,G,T in string");
3b22753a
L
14281 return -1;
14282}
14283
01e1a5bc 14284bfd_vma
3b22753a
L
14285x86_64_section_word (char *str, size_t len)
14286{
8620418b 14287 if (len == 5 && flag_code == CODE_64BIT && CONST_STRNEQ (str, "large"))
3b22753a
L
14288 return SHF_X86_64_LARGE;
14289
14290 return -1;
14291}
14292
14293static void
14294handle_large_common (int small ATTRIBUTE_UNUSED)
14295{
14296 if (flag_code != CODE_64BIT)
14297 {
14298 s_comm_internal (0, elf_common_parse);
14299 as_warn (_(".largecomm supported only in 64bit mode, producing .comm"));
14300 }
14301 else
14302 {
14303 static segT lbss_section;
14304 asection *saved_com_section_ptr = elf_com_section_ptr;
14305 asection *saved_bss_section = bss_section;
14306
14307 if (lbss_section == NULL)
14308 {
14309 flagword applicable;
14310 segT seg = now_seg;
14311 subsegT subseg = now_subseg;
14312
14313 /* The .lbss section is for local .largecomm symbols. */
14314 lbss_section = subseg_new (".lbss", 0);
14315 applicable = bfd_applicable_section_flags (stdoutput);
fd361982 14316 bfd_set_section_flags (lbss_section, applicable & SEC_ALLOC);
3b22753a
L
14317 seg_info (lbss_section)->bss = 1;
14318
14319 subseg_set (seg, subseg);
14320 }
14321
14322 elf_com_section_ptr = &_bfd_elf_large_com_section;
14323 bss_section = lbss_section;
14324
14325 s_comm_internal (0, elf_common_parse);
14326
14327 elf_com_section_ptr = saved_com_section_ptr;
14328 bss_section = saved_bss_section;
14329 }
14330}
14331#endif /* OBJ_ELF || OBJ_MAYBE_ELF */
This page took 2.756891 seconds and 4 git commands to generate.