]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/config/tc-i386.c
Use bool in gas
[thirdparty/binutils-gdb.git] / gas / config / tc-i386.c
CommitLineData
b534c6d3 1/* tc-i386.c -- Assemble code for the Intel 80386
250d07de 2 Copyright (C) 1989-2021 Free Software Foundation, Inc.
252b5132
RH
3
4 This file is part of GAS, the GNU Assembler.
5
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
ec2655a6 8 the Free Software Foundation; either version 3, or (at your option)
252b5132
RH
9 any later version.
10
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to the Free
4b4da160
NC
18 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
19 02110-1301, USA. */
252b5132 20
47926f60
KH
21/* Intel 80386 machine specific gas.
22 Written by Eliot Dresselhaus (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);
783c187b 180static void swap_2_operands (unsigned int, unsigned 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);
5e042380 193static const reg_entry *build_modrm_byte (void);
e3bb37b5
L
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
34684862 217static const reg_entry *reg_eax;
5e042380
JB
218static const reg_entry *reg_ds;
219static const reg_entry *reg_es;
220static const reg_entry *reg_ss;
6288d05f 221static const reg_entry *reg_st0;
6225c532
JB
222static const reg_entry *reg_k0;
223
c0f3af97
L
224/* VEX prefix. */
225typedef struct
226{
43234a1e
L
227 /* VEX prefix is either 2 byte or 3 byte. EVEX is 4 byte. */
228 unsigned char bytes[4];
c0f3af97
L
229 unsigned int length;
230 /* Destination or source register specifier. */
231 const reg_entry *register_specifier;
232} vex_prefix;
233
252b5132 234/* 'md_assemble ()' gathers together information and puts it into a
47926f60 235 i386_insn. */
252b5132 236
520dc8e8
AM
237union i386_op
238 {
239 expressionS *disps;
240 expressionS *imms;
241 const reg_entry *regs;
242 };
243
a65babc9
L
244enum i386_error
245 {
86e026a4 246 operand_size_mismatch,
a65babc9
L
247 operand_type_mismatch,
248 register_type_mismatch,
249 number_of_operands_mismatch,
250 invalid_instruction_suffix,
251 bad_imm4,
a65babc9
L
252 unsupported_with_intel_mnemonic,
253 unsupported_syntax,
6c30d220 254 unsupported,
260cd341 255 invalid_sib_address,
6c30d220 256 invalid_vsib_address,
7bab8ab5 257 invalid_vector_register_set,
260cd341 258 invalid_tmm_register_set,
43234a1e
L
259 unsupported_vector_index_register,
260 unsupported_broadcast,
43234a1e
L
261 broadcast_needed,
262 unsupported_masking,
263 mask_not_on_destination,
264 no_default_mask,
265 unsupported_rc_sae,
266 rc_sae_operand_not_last_imm,
267 invalid_register_operand,
a65babc9
L
268 };
269
252b5132
RH
270struct _i386_insn
271 {
47926f60 272 /* TM holds the template for the insn were currently assembling. */
d3ce72d0 273 insn_template tm;
252b5132 274
7d5e4556
L
275 /* SUFFIX holds the instruction size suffix for byte, word, dword
276 or qword, if given. */
252b5132
RH
277 char suffix;
278
9a182d04
JB
279 /* OPCODE_LENGTH holds the number of base opcode bytes. */
280 unsigned char opcode_length;
281
47926f60 282 /* OPERANDS gives the number of given operands. */
252b5132
RH
283 unsigned int operands;
284
285 /* REG_OPERANDS, DISP_OPERANDS, MEM_OPERANDS, IMM_OPERANDS give the number
286 of given register, displacement, memory operands and immediate
47926f60 287 operands. */
252b5132
RH
288 unsigned int reg_operands, disp_operands, mem_operands, imm_operands;
289
290 /* TYPES [i] is the type (see above #defines) which tells us how to
520dc8e8 291 use OP[i] for the corresponding operand. */
40fb9820 292 i386_operand_type types[MAX_OPERANDS];
252b5132 293
520dc8e8
AM
294 /* Displacement expression, immediate expression, or register for each
295 operand. */
296 union i386_op op[MAX_OPERANDS];
252b5132 297
3e73aa7c
JH
298 /* Flags for operands. */
299 unsigned int flags[MAX_OPERANDS];
300#define Operand_PCrel 1
c48dadc9 301#define Operand_Mem 2
3e73aa7c 302
252b5132 303 /* Relocation type for operand */
f86103b7 304 enum bfd_reloc_code_real reloc[MAX_OPERANDS];
252b5132 305
252b5132
RH
306 /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
307 the base index byte below. */
308 const reg_entry *base_reg;
309 const reg_entry *index_reg;
310 unsigned int log2_scale_factor;
311
312 /* SEG gives the seg_entries of this insn. They are zero unless
47926f60 313 explicit segment overrides are given. */
5e042380 314 const reg_entry *seg[2];
252b5132 315
8325cc63
JB
316 /* Copied first memory operand string, for re-checking. */
317 char *memop1_string;
318
252b5132
RH
319 /* PREFIX holds all the given prefix opcodes (usually null).
320 PREFIXES is the number of prefix opcodes. */
321 unsigned int prefixes;
322 unsigned char prefix[MAX_PREFIXES];
323
50128d0c 324 /* Register is in low 3 bits of opcode. */
5b7c81bd 325 bool short_form;
50128d0c 326
6f2f06be 327 /* The operand to a branch insn indicates an absolute branch. */
5b7c81bd 328 bool jumpabsolute;
6f2f06be 329
921eafea
L
330 /* Extended states. */
331 enum
332 {
333 /* Use MMX state. */
334 xstate_mmx = 1 << 0,
335 /* Use XMM state. */
336 xstate_xmm = 1 << 1,
337 /* Use YMM state. */
338 xstate_ymm = 1 << 2 | xstate_xmm,
339 /* Use ZMM state. */
340 xstate_zmm = 1 << 3 | xstate_ymm,
341 /* Use TMM state. */
32930e4e
L
342 xstate_tmm = 1 << 4,
343 /* Use MASK state. */
344 xstate_mask = 1 << 5
921eafea 345 } xstate;
260cd341 346
e379e5f3 347 /* Has GOTPC or TLS relocation. */
5b7c81bd 348 bool has_gotpc_tls_reloc;
e379e5f3 349
252b5132 350 /* RM and SIB are the modrm byte and the sib byte where the
c1e679ec 351 addressing modes of this insn are encoded. */
252b5132 352 modrm_byte rm;
3e73aa7c 353 rex_byte rex;
43234a1e 354 rex_byte vrex;
252b5132 355 sib_byte sib;
c0f3af97 356 vex_prefix vex;
b6169b20 357
6225c532
JB
358 /* Masking attributes.
359
360 The struct describes masking, applied to OPERAND in the instruction.
361 REG is a pointer to the corresponding mask register. ZEROING tells
362 whether merging or zeroing mask is used. */
363 struct Mask_Operation
364 {
365 const reg_entry *reg;
366 unsigned int zeroing;
367 /* The operand where this operation is associated. */
368 unsigned int operand;
369 } mask;
43234a1e
L
370
371 /* Rounding control and SAE attributes. */
ca5312a2
JB
372 struct RC_Operation
373 {
374 enum rc_type
375 {
376 rc_none = -1,
377 rne,
378 rd,
379 ru,
380 rz,
381 saeonly
382 } type;
383
384 unsigned int operand;
385 } rounding;
43234a1e 386
5273a3cd
JB
387 /* Broadcasting attributes.
388
389 The struct describes broadcasting, applied to OPERAND. TYPE is
390 expresses the broadcast factor. */
391 struct Broadcast_Operation
392 {
393 /* Type of broadcast: {1to2}, {1to4}, {1to8}, or {1to16}. */
394 unsigned int type;
395
396 /* Index of broadcasted operand. */
397 unsigned int operand;
398
399 /* Number of bytes to broadcast. */
400 unsigned int bytes;
401 } broadcast;
43234a1e
L
402
403 /* Compressed disp8*N attribute. */
404 unsigned int memshift;
405
86fa6981
L
406 /* Prefer load or store in encoding. */
407 enum
408 {
409 dir_encoding_default = 0,
410 dir_encoding_load,
64c49ab3
JB
411 dir_encoding_store,
412 dir_encoding_swap
86fa6981 413 } dir_encoding;
891edac4 414
41eb8e88 415 /* Prefer 8bit, 16bit, 32bit displacement in encoding. */
a501d77e
L
416 enum
417 {
418 disp_encoding_default = 0,
419 disp_encoding_8bit,
41eb8e88 420 disp_encoding_16bit,
a501d77e
L
421 disp_encoding_32bit
422 } disp_encoding;
f8a5c266 423
6b6b6807 424 /* Prefer the REX byte in encoding. */
5b7c81bd 425 bool rex_encoding;
6b6b6807 426
b6f8c7c4 427 /* Disable instruction size optimization. */
5b7c81bd 428 bool no_optimize;
b6f8c7c4 429
86fa6981
L
430 /* How to encode vector instructions. */
431 enum
432 {
433 vex_encoding_default = 0,
42e04b36 434 vex_encoding_vex,
86fa6981 435 vex_encoding_vex3,
da4977e0
JB
436 vex_encoding_evex,
437 vex_encoding_error
86fa6981
L
438 } vec_encoding;
439
d5de92cf
L
440 /* REP prefix. */
441 const char *rep_prefix;
442
165de32a
L
443 /* HLE prefix. */
444 const char *hle_prefix;
42164a71 445
7e8b059b
L
446 /* Have BND prefix. */
447 const char *bnd_prefix;
448
04ef582a
L
449 /* Have NOTRACK prefix. */
450 const char *notrack_prefix;
451
891edac4 452 /* Error message. */
a65babc9 453 enum i386_error error;
252b5132
RH
454 };
455
456typedef struct _i386_insn i386_insn;
457
43234a1e
L
458/* Link RC type with corresponding string, that'll be looked for in
459 asm. */
460struct RC_name
461{
462 enum rc_type type;
463 const char *name;
464 unsigned int len;
465};
466
467static const struct RC_name RC_NamesTable[] =
468{
469 { rne, STRING_COMMA_LEN ("rn-sae") },
470 { rd, STRING_COMMA_LEN ("rd-sae") },
471 { ru, STRING_COMMA_LEN ("ru-sae") },
472 { rz, STRING_COMMA_LEN ("rz-sae") },
473 { saeonly, STRING_COMMA_LEN ("sae") },
474};
475
252b5132
RH
476/* List of chars besides those in app.c:symbol_chars that can start an
477 operand. Used to prevent the scrubber eating vital white-space. */
86fa6981 478const char extra_symbol_chars[] = "*%-([{}"
252b5132 479#ifdef LEX_AT
32137342
NC
480 "@"
481#endif
482#ifdef LEX_QM
483 "?"
252b5132 484#endif
32137342 485 ;
252b5132 486
b3983e5f
JB
487#if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
488 && !defined (TE_GNU) \
489 && !defined (TE_LINUX) \
490 && !defined (TE_FreeBSD) \
491 && !defined (TE_DragonFly) \
492 && !defined (TE_NetBSD))
252b5132 493/* This array holds the chars that always start a comment. If the
b3b91714
AM
494 pre-processor is disabled, these aren't very useful. The option
495 --divide will remove '/' from this list. */
496const char *i386_comment_chars = "#/";
497#define SVR4_COMMENT_CHARS 1
252b5132 498#define PREFIX_SEPARATOR '\\'
252b5132 499
b3b91714
AM
500#else
501const char *i386_comment_chars = "#";
502#define PREFIX_SEPARATOR '/'
503#endif
504
252b5132
RH
505/* This array holds the chars that only start a comment at the beginning of
506 a line. If the line seems to have the form '# 123 filename'
ce8a8b2f
AM
507 .line and .file directives will appear in the pre-processed output.
508 Note that input_file.c hand checks for '#' at the beginning of the
252b5132 509 first line of the input file. This is because the compiler outputs
ce8a8b2f
AM
510 #NO_APP at the beginning of its output.
511 Also note that comments started like this one will always work if
252b5132 512 '/' isn't otherwise defined. */
b3b91714 513const char line_comment_chars[] = "#/";
252b5132 514
63a0b638 515const char line_separator_chars[] = ";";
252b5132 516
ce8a8b2f
AM
517/* Chars that can be used to separate mant from exp in floating point
518 nums. */
252b5132
RH
519const char EXP_CHARS[] = "eE";
520
ce8a8b2f
AM
521/* Chars that mean this number is a floating point constant
522 As in 0f12.456
523 or 0d1.2345e12. */
252b5132
RH
524const char FLT_CHARS[] = "fFdDxX";
525
ce8a8b2f 526/* Tables for lexical analysis. */
252b5132
RH
527static char mnemonic_chars[256];
528static char register_chars[256];
529static char operand_chars[256];
530static char identifier_chars[256];
531static char digit_chars[256];
532
ce8a8b2f 533/* Lexical macros. */
252b5132
RH
534#define is_mnemonic_char(x) (mnemonic_chars[(unsigned char) x])
535#define is_operand_char(x) (operand_chars[(unsigned char) x])
536#define is_register_char(x) (register_chars[(unsigned char) x])
537#define is_space_char(x) ((x) == ' ')
538#define is_identifier_char(x) (identifier_chars[(unsigned char) x])
539#define is_digit_char(x) (digit_chars[(unsigned char) x])
540
0234cb7c 541/* All non-digit non-letter characters that may occur in an operand. */
252b5132
RH
542static char operand_special_chars[] = "%$-+(,)*._~/<>|&^!:[@]";
543
544/* md_assemble() always leaves the strings it's passed unaltered. To
545 effect this we maintain a stack of saved characters that we've smashed
546 with '\0's (indicating end of strings for various sub-fields of the
47926f60 547 assembler instruction). */
252b5132 548static char save_stack[32];
ce8a8b2f 549static char *save_stack_p;
252b5132
RH
550#define END_STRING_AND_SAVE(s) \
551 do { *save_stack_p++ = *(s); *(s) = '\0'; } while (0)
552#define RESTORE_END_STRING(s) \
553 do { *(s) = *--save_stack_p; } while (0)
554
47926f60 555/* The instruction we're assembling. */
252b5132
RH
556static i386_insn i;
557
558/* Possible templates for current insn. */
559static const templates *current_templates;
560
31b2323c
L
561/* Per instruction expressionS buffers: max displacements & immediates. */
562static expressionS disp_expressions[MAX_MEMORY_OPERANDS];
563static expressionS im_expressions[MAX_IMMEDIATE_OPERANDS];
252b5132 564
47926f60 565/* Current operand we are working on. */
ee86248c 566static int this_operand = -1;
252b5132 567
3e73aa7c
JH
568/* We support four different modes. FLAG_CODE variable is used to distinguish
569 these. */
570
571enum flag_code {
572 CODE_32BIT,
573 CODE_16BIT,
574 CODE_64BIT };
575
576static enum flag_code flag_code;
4fa24527 577static unsigned int object_64bit;
862be3fb 578static unsigned int disallow_64bit_reloc;
3e73aa7c 579static int use_rela_relocations = 0;
e379e5f3
L
580/* __tls_get_addr/___tls_get_addr symbol for TLS. */
581static const char *tls_get_addr;
3e73aa7c 582
7af8ed2d
NC
583#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
584 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
585 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
586
351f65ca
L
587/* The ELF ABI to use. */
588enum x86_elf_abi
589{
590 I386_ABI,
7f56bc95
L
591 X86_64_ABI,
592 X86_64_X32_ABI
351f65ca
L
593};
594
595static enum x86_elf_abi x86_elf_abi = I386_ABI;
7af8ed2d 596#endif
351f65ca 597
167ad85b
TG
598#if defined (TE_PE) || defined (TE_PEP)
599/* Use big object file format. */
600static int use_big_obj = 0;
601#endif
602
8dcea932
L
603#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
604/* 1 if generating code for a shared library. */
605static int shared = 0;
606#endif
607
47926f60
KH
608/* 1 for intel syntax,
609 0 if att syntax. */
610static int intel_syntax = 0;
252b5132 611
4b5aaf5f
L
612static enum x86_64_isa
613{
614 amd64 = 1, /* AMD64 ISA. */
615 intel64 /* Intel64 ISA. */
616} isa64;
e89c5eaa 617
1efbbeb4
L
618/* 1 for intel mnemonic,
619 0 if att mnemonic. */
620static int intel_mnemonic = !SYSV386_COMPAT;
621
a60de03c
JB
622/* 1 if pseudo registers are permitted. */
623static int allow_pseudo_reg = 0;
624
47926f60
KH
625/* 1 if register prefix % not required. */
626static int allow_naked_reg = 0;
252b5132 627
33eaf5de 628/* 1 if the assembler should add BND prefix for all control-transferring
7e8b059b
L
629 instructions supporting it, even if this prefix wasn't specified
630 explicitly. */
631static int add_bnd_prefix = 0;
632
ba104c83 633/* 1 if pseudo index register, eiz/riz, is allowed . */
db51cc60
L
634static int allow_index_reg = 0;
635
d022bddd
IT
636/* 1 if the assembler should ignore LOCK prefix, even if it was
637 specified explicitly. */
638static int omit_lock_prefix = 0;
639
e4e00185
AS
640/* 1 if the assembler should encode lfence, mfence, and sfence as
641 "lock addl $0, (%{re}sp)". */
642static int avoid_fence = 0;
643
ae531041
L
644/* 1 if lfence should be inserted after every load. */
645static int lfence_after_load = 0;
646
647/* Non-zero if lfence should be inserted before indirect branch. */
648static enum lfence_before_indirect_branch_kind
649 {
650 lfence_branch_none = 0,
651 lfence_branch_register,
652 lfence_branch_memory,
653 lfence_branch_all
654 }
655lfence_before_indirect_branch;
656
657/* Non-zero if lfence should be inserted before ret. */
658static enum lfence_before_ret_kind
659 {
660 lfence_before_ret_none = 0,
661 lfence_before_ret_not,
a09f656b 662 lfence_before_ret_or,
663 lfence_before_ret_shl
ae531041
L
664 }
665lfence_before_ret;
666
667/* Types of previous instruction is .byte or prefix. */
e379e5f3
L
668static struct
669 {
670 segT seg;
671 const char *file;
672 const char *name;
673 unsigned int line;
674 enum last_insn_kind
675 {
676 last_insn_other = 0,
677 last_insn_directive,
678 last_insn_prefix
679 } kind;
680 } last_insn;
681
0cb4071e
L
682/* 1 if the assembler should generate relax relocations. */
683
684static int generate_relax_relocations
685 = DEFAULT_GENERATE_X86_RELAX_RELOCATIONS;
686
7bab8ab5 687static enum check_kind
daf50ae7 688 {
7bab8ab5
JB
689 check_none = 0,
690 check_warning,
691 check_error
daf50ae7 692 }
7bab8ab5 693sse_check, operand_check = check_warning;
daf50ae7 694
e379e5f3
L
695/* Non-zero if branches should be aligned within power of 2 boundary. */
696static int align_branch_power = 0;
697
698/* Types of branches to align. */
699enum align_branch_kind
700 {
701 align_branch_none = 0,
702 align_branch_jcc = 1,
703 align_branch_fused = 2,
704 align_branch_jmp = 3,
705 align_branch_call = 4,
706 align_branch_indirect = 5,
707 align_branch_ret = 6
708 };
709
710/* Type bits of branches to align. */
711enum align_branch_bit
712 {
713 align_branch_jcc_bit = 1 << align_branch_jcc,
714 align_branch_fused_bit = 1 << align_branch_fused,
715 align_branch_jmp_bit = 1 << align_branch_jmp,
716 align_branch_call_bit = 1 << align_branch_call,
717 align_branch_indirect_bit = 1 << align_branch_indirect,
718 align_branch_ret_bit = 1 << align_branch_ret
719 };
720
721static unsigned int align_branch = (align_branch_jcc_bit
722 | align_branch_fused_bit
723 | align_branch_jmp_bit);
724
79d72f45
HL
725/* Types of condition jump used by macro-fusion. */
726enum mf_jcc_kind
727 {
728 mf_jcc_jo = 0, /* base opcode 0x70 */
729 mf_jcc_jc, /* base opcode 0x72 */
730 mf_jcc_je, /* base opcode 0x74 */
731 mf_jcc_jna, /* base opcode 0x76 */
732 mf_jcc_js, /* base opcode 0x78 */
733 mf_jcc_jp, /* base opcode 0x7a */
734 mf_jcc_jl, /* base opcode 0x7c */
735 mf_jcc_jle, /* base opcode 0x7e */
736 };
737
738/* Types of compare flag-modifying insntructions used by macro-fusion. */
739enum mf_cmp_kind
740 {
741 mf_cmp_test_and, /* test/cmp */
742 mf_cmp_alu_cmp, /* add/sub/cmp */
743 mf_cmp_incdec /* inc/dec */
744 };
745
e379e5f3
L
746/* The maximum padding size for fused jcc. CMP like instruction can
747 be 9 bytes and jcc can be 6 bytes. Leave room just in case for
748 prefixes. */
749#define MAX_FUSED_JCC_PADDING_SIZE 20
750
751/* The maximum number of prefixes added for an instruction. */
752static unsigned int align_branch_prefix_size = 5;
753
b6f8c7c4
L
754/* Optimization:
755 1. Clear the REX_W bit with register operand if possible.
756 2. Above plus use 128bit vector instruction to clear the full vector
757 register.
758 */
759static int optimize = 0;
760
761/* Optimization:
762 1. Clear the REX_W bit with register operand if possible.
763 2. Above plus use 128bit vector instruction to clear the full vector
764 register.
765 3. Above plus optimize "test{q,l,w} $imm8,%r{64,32,16}" to
766 "testb $imm7,%r8".
767 */
768static int optimize_for_space = 0;
769
2ca3ace5
L
770/* Register prefix used for error message. */
771static const char *register_prefix = "%";
772
47926f60
KH
773/* Used in 16 bit gcc mode to add an l suffix to call, ret, enter,
774 leave, push, and pop instructions so that gcc has the same stack
775 frame as in 32 bit mode. */
776static char stackop_size = '\0';
eecb386c 777
12b55ccc
L
778/* Non-zero to optimize code alignment. */
779int optimize_align_code = 1;
780
47926f60
KH
781/* Non-zero to quieten some warnings. */
782static int quiet_warnings = 0;
a38cf1db 783
47926f60
KH
784/* CPU name. */
785static const char *cpu_arch_name = NULL;
6305a203 786static char *cpu_sub_arch_name = NULL;
a38cf1db 787
47926f60 788/* CPU feature flags. */
40fb9820
L
789static i386_cpu_flags cpu_arch_flags = CPU_UNKNOWN_FLAGS;
790
ccc9c027
L
791/* If we have selected a cpu we are generating instructions for. */
792static int cpu_arch_tune_set = 0;
793
9103f4f4 794/* Cpu we are generating instructions for. */
fbf3f584 795enum processor_type cpu_arch_tune = PROCESSOR_UNKNOWN;
9103f4f4
L
796
797/* CPU feature flags of cpu we are generating instructions for. */
40fb9820 798static i386_cpu_flags cpu_arch_tune_flags;
9103f4f4 799
ccc9c027 800/* CPU instruction set architecture used. */
fbf3f584 801enum processor_type cpu_arch_isa = PROCESSOR_UNKNOWN;
ccc9c027 802
9103f4f4 803/* CPU feature flags of instruction set architecture used. */
fbf3f584 804i386_cpu_flags cpu_arch_isa_flags;
9103f4f4 805
fddf5b5b
AM
806/* If set, conditional jumps are not automatically promoted to handle
807 larger than a byte offset. */
808static unsigned int no_cond_jump_promotion = 0;
809
c0f3af97
L
810/* Encode SSE instructions with VEX prefix. */
811static unsigned int sse2avx;
812
539f890d
L
813/* Encode scalar AVX instructions with specific vector length. */
814static enum
815 {
816 vex128 = 0,
817 vex256
818 } avxscalar;
819
03751133
L
820/* Encode VEX WIG instructions with specific vex.w. */
821static enum
822 {
823 vexw0 = 0,
824 vexw1
825 } vexwig;
826
43234a1e
L
827/* Encode scalar EVEX LIG instructions with specific vector length. */
828static enum
829 {
830 evexl128 = 0,
831 evexl256,
832 evexl512
833 } evexlig;
834
835/* Encode EVEX WIG instructions with specific evex.w. */
836static enum
837 {
838 evexw0 = 0,
839 evexw1
840 } evexwig;
841
d3d3c6db
IT
842/* Value to encode in EVEX RC bits, for SAE-only instructions. */
843static enum rc_type evexrcig = rne;
844
29b0f896 845/* Pre-defined "_GLOBAL_OFFSET_TABLE_". */
87c245cc 846static symbolS *GOT_symbol;
29b0f896 847
a4447b93
RH
848/* The dwarf2 return column, adjusted for 32 or 64 bit. */
849unsigned int x86_dwarf2_return_column;
850
851/* The dwarf2 data alignment, adjusted for 32 or 64 bit. */
852int x86_cie_data_alignment;
853
252b5132 854/* Interface to relax_segment.
fddf5b5b
AM
855 There are 3 major relax states for 386 jump insns because the
856 different types of jumps add different sizes to frags when we're
e379e5f3
L
857 figuring out what sort of jump to choose to reach a given label.
858
859 BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING are used to align
860 branches which are handled by md_estimate_size_before_relax() and
861 i386_generic_table_relax_frag(). */
252b5132 862
47926f60 863/* Types. */
93c2a809
AM
864#define UNCOND_JUMP 0
865#define COND_JUMP 1
866#define COND_JUMP86 2
e379e5f3
L
867#define BRANCH_PADDING 3
868#define BRANCH_PREFIX 4
869#define FUSED_JCC_PADDING 5
fddf5b5b 870
47926f60 871/* Sizes. */
252b5132
RH
872#define CODE16 1
873#define SMALL 0
29b0f896 874#define SMALL16 (SMALL | CODE16)
252b5132 875#define BIG 2
29b0f896 876#define BIG16 (BIG | CODE16)
252b5132
RH
877
878#ifndef INLINE
879#ifdef __GNUC__
880#define INLINE __inline__
881#else
882#define INLINE
883#endif
884#endif
885
fddf5b5b
AM
886#define ENCODE_RELAX_STATE(type, size) \
887 ((relax_substateT) (((type) << 2) | (size)))
888#define TYPE_FROM_RELAX_STATE(s) \
889 ((s) >> 2)
890#define DISP_SIZE_FROM_RELAX_STATE(s) \
891 ((((s) & 3) == BIG ? 4 : (((s) & 3) == BIG16 ? 2 : 1)))
252b5132
RH
892
893/* This table is used by relax_frag to promote short jumps to long
894 ones where necessary. SMALL (short) jumps may be promoted to BIG
895 (32 bit long) ones, and SMALL16 jumps to BIG16 (16 bit long). We
896 don't allow a short jump in a 32 bit code segment to be promoted to
897 a 16 bit offset jump because it's slower (requires data size
898 prefix), and doesn't work, unless the destination is in the bottom
899 64k of the code segment (The top 16 bits of eip are zeroed). */
900
901const relax_typeS md_relax_table[] =
902{
24eab124
AM
903 /* The fields are:
904 1) most positive reach of this state,
905 2) most negative reach of this state,
93c2a809 906 3) how many bytes this mode will have in the variable part of the frag
ce8a8b2f 907 4) which index into the table to try if we can't fit into this one. */
252b5132 908
fddf5b5b 909 /* UNCOND_JUMP states. */
93c2a809
AM
910 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG)},
911 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16)},
912 /* dword jmp adds 4 bytes to frag:
913 0 extra opcode bytes, 4 displacement bytes. */
252b5132 914 {0, 0, 4, 0},
93c2a809
AM
915 /* word jmp adds 2 byte2 to frag:
916 0 extra opcode bytes, 2 displacement bytes. */
252b5132
RH
917 {0, 0, 2, 0},
918
93c2a809
AM
919 /* COND_JUMP states. */
920 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG)},
921 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG16)},
922 /* dword conditionals adds 5 bytes to frag:
923 1 extra opcode byte, 4 displacement bytes. */
924 {0, 0, 5, 0},
fddf5b5b 925 /* word conditionals add 3 bytes to frag:
93c2a809
AM
926 1 extra opcode byte, 2 displacement bytes. */
927 {0, 0, 3, 0},
928
929 /* COND_JUMP86 states. */
930 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG)},
931 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG16)},
932 /* dword conditionals adds 5 bytes to frag:
933 1 extra opcode byte, 4 displacement bytes. */
934 {0, 0, 5, 0},
935 /* word conditionals add 4 bytes to frag:
936 1 displacement byte and a 3 byte long branch insn. */
937 {0, 0, 4, 0}
252b5132
RH
938};
939
9103f4f4
L
940static const arch_entry cpu_arch[] =
941{
89507696
JB
942 /* Do not replace the first two entries - i386_target_format()
943 relies on them being there in this order. */
8a2c8fef 944 { STRING_COMMA_LEN ("generic32"), PROCESSOR_GENERIC32,
293f5f65 945 CPU_GENERIC32_FLAGS, 0 },
8a2c8fef 946 { STRING_COMMA_LEN ("generic64"), PROCESSOR_GENERIC64,
293f5f65 947 CPU_GENERIC64_FLAGS, 0 },
8a2c8fef 948 { STRING_COMMA_LEN ("i8086"), PROCESSOR_UNKNOWN,
293f5f65 949 CPU_NONE_FLAGS, 0 },
8a2c8fef 950 { STRING_COMMA_LEN ("i186"), PROCESSOR_UNKNOWN,
293f5f65 951 CPU_I186_FLAGS, 0 },
8a2c8fef 952 { STRING_COMMA_LEN ("i286"), PROCESSOR_UNKNOWN,
293f5f65 953 CPU_I286_FLAGS, 0 },
8a2c8fef 954 { STRING_COMMA_LEN ("i386"), PROCESSOR_I386,
293f5f65 955 CPU_I386_FLAGS, 0 },
8a2c8fef 956 { STRING_COMMA_LEN ("i486"), PROCESSOR_I486,
293f5f65 957 CPU_I486_FLAGS, 0 },
8a2c8fef 958 { STRING_COMMA_LEN ("i586"), PROCESSOR_PENTIUM,
293f5f65 959 CPU_I586_FLAGS, 0 },
8a2c8fef 960 { STRING_COMMA_LEN ("i686"), PROCESSOR_PENTIUMPRO,
293f5f65 961 CPU_I686_FLAGS, 0 },
8a2c8fef 962 { STRING_COMMA_LEN ("pentium"), PROCESSOR_PENTIUM,
293f5f65 963 CPU_I586_FLAGS, 0 },
8a2c8fef 964 { STRING_COMMA_LEN ("pentiumpro"), PROCESSOR_PENTIUMPRO,
293f5f65 965 CPU_PENTIUMPRO_FLAGS, 0 },
8a2c8fef 966 { STRING_COMMA_LEN ("pentiumii"), PROCESSOR_PENTIUMPRO,
293f5f65 967 CPU_P2_FLAGS, 0 },
8a2c8fef 968 { STRING_COMMA_LEN ("pentiumiii"),PROCESSOR_PENTIUMPRO,
293f5f65 969 CPU_P3_FLAGS, 0 },
8a2c8fef 970 { STRING_COMMA_LEN ("pentium4"), PROCESSOR_PENTIUM4,
293f5f65 971 CPU_P4_FLAGS, 0 },
8a2c8fef 972 { STRING_COMMA_LEN ("prescott"), PROCESSOR_NOCONA,
293f5f65 973 CPU_CORE_FLAGS, 0 },
8a2c8fef 974 { STRING_COMMA_LEN ("nocona"), PROCESSOR_NOCONA,
293f5f65 975 CPU_NOCONA_FLAGS, 0 },
8a2c8fef 976 { STRING_COMMA_LEN ("yonah"), PROCESSOR_CORE,
293f5f65 977 CPU_CORE_FLAGS, 1 },
8a2c8fef 978 { STRING_COMMA_LEN ("core"), PROCESSOR_CORE,
293f5f65 979 CPU_CORE_FLAGS, 0 },
8a2c8fef 980 { STRING_COMMA_LEN ("merom"), PROCESSOR_CORE2,
293f5f65 981 CPU_CORE2_FLAGS, 1 },
8a2c8fef 982 { STRING_COMMA_LEN ("core2"), PROCESSOR_CORE2,
293f5f65 983 CPU_CORE2_FLAGS, 0 },
8a2c8fef 984 { STRING_COMMA_LEN ("corei7"), PROCESSOR_COREI7,
293f5f65 985 CPU_COREI7_FLAGS, 0 },
8a2c8fef 986 { STRING_COMMA_LEN ("l1om"), PROCESSOR_L1OM,
293f5f65 987 CPU_L1OM_FLAGS, 0 },
7a9068fe 988 { STRING_COMMA_LEN ("k1om"), PROCESSOR_K1OM,
293f5f65 989 CPU_K1OM_FLAGS, 0 },
81486035 990 { STRING_COMMA_LEN ("iamcu"), PROCESSOR_IAMCU,
293f5f65 991 CPU_IAMCU_FLAGS, 0 },
8a2c8fef 992 { STRING_COMMA_LEN ("k6"), PROCESSOR_K6,
293f5f65 993 CPU_K6_FLAGS, 0 },
8a2c8fef 994 { STRING_COMMA_LEN ("k6_2"), PROCESSOR_K6,
293f5f65 995 CPU_K6_2_FLAGS, 0 },
8a2c8fef 996 { STRING_COMMA_LEN ("athlon"), PROCESSOR_ATHLON,
293f5f65 997 CPU_ATHLON_FLAGS, 0 },
8a2c8fef 998 { STRING_COMMA_LEN ("sledgehammer"), PROCESSOR_K8,
293f5f65 999 CPU_K8_FLAGS, 1 },
8a2c8fef 1000 { STRING_COMMA_LEN ("opteron"), PROCESSOR_K8,
293f5f65 1001 CPU_K8_FLAGS, 0 },
8a2c8fef 1002 { STRING_COMMA_LEN ("k8"), PROCESSOR_K8,
293f5f65 1003 CPU_K8_FLAGS, 0 },
8a2c8fef 1004 { STRING_COMMA_LEN ("amdfam10"), PROCESSOR_AMDFAM10,
293f5f65 1005 CPU_AMDFAM10_FLAGS, 0 },
8aedb9fe 1006 { STRING_COMMA_LEN ("bdver1"), PROCESSOR_BD,
293f5f65 1007 CPU_BDVER1_FLAGS, 0 },
8aedb9fe 1008 { STRING_COMMA_LEN ("bdver2"), PROCESSOR_BD,
293f5f65 1009 CPU_BDVER2_FLAGS, 0 },
5e5c50d3 1010 { STRING_COMMA_LEN ("bdver3"), PROCESSOR_BD,
293f5f65 1011 CPU_BDVER3_FLAGS, 0 },
c7b0bd56 1012 { STRING_COMMA_LEN ("bdver4"), PROCESSOR_BD,
293f5f65 1013 CPU_BDVER4_FLAGS, 0 },
029f3522 1014 { STRING_COMMA_LEN ("znver1"), PROCESSOR_ZNVER,
293f5f65 1015 CPU_ZNVER1_FLAGS, 0 },
a9660a6f
AP
1016 { STRING_COMMA_LEN ("znver2"), PROCESSOR_ZNVER,
1017 CPU_ZNVER2_FLAGS, 0 },
646cc3e0
GG
1018 { STRING_COMMA_LEN ("znver3"), PROCESSOR_ZNVER,
1019 CPU_ZNVER3_FLAGS, 0 },
7b458c12 1020 { STRING_COMMA_LEN ("btver1"), PROCESSOR_BT,
293f5f65 1021 CPU_BTVER1_FLAGS, 0 },
7b458c12 1022 { STRING_COMMA_LEN ("btver2"), PROCESSOR_BT,
293f5f65 1023 CPU_BTVER2_FLAGS, 0 },
8a2c8fef 1024 { STRING_COMMA_LEN (".8087"), PROCESSOR_UNKNOWN,
293f5f65 1025 CPU_8087_FLAGS, 0 },
8a2c8fef 1026 { STRING_COMMA_LEN (".287"), PROCESSOR_UNKNOWN,
293f5f65 1027 CPU_287_FLAGS, 0 },
8a2c8fef 1028 { STRING_COMMA_LEN (".387"), PROCESSOR_UNKNOWN,
293f5f65 1029 CPU_387_FLAGS, 0 },
1848e567
L
1030 { STRING_COMMA_LEN (".687"), PROCESSOR_UNKNOWN,
1031 CPU_687_FLAGS, 0 },
d871f3f4
L
1032 { STRING_COMMA_LEN (".cmov"), PROCESSOR_UNKNOWN,
1033 CPU_CMOV_FLAGS, 0 },
1034 { STRING_COMMA_LEN (".fxsr"), PROCESSOR_UNKNOWN,
1035 CPU_FXSR_FLAGS, 0 },
8a2c8fef 1036 { STRING_COMMA_LEN (".mmx"), PROCESSOR_UNKNOWN,
293f5f65 1037 CPU_MMX_FLAGS, 0 },
8a2c8fef 1038 { STRING_COMMA_LEN (".sse"), PROCESSOR_UNKNOWN,
293f5f65 1039 CPU_SSE_FLAGS, 0 },
8a2c8fef 1040 { STRING_COMMA_LEN (".sse2"), PROCESSOR_UNKNOWN,
293f5f65 1041 CPU_SSE2_FLAGS, 0 },
8a2c8fef 1042 { STRING_COMMA_LEN (".sse3"), PROCESSOR_UNKNOWN,
293f5f65 1043 CPU_SSE3_FLAGS, 0 },
af5c13b0
L
1044 { STRING_COMMA_LEN (".sse4a"), PROCESSOR_UNKNOWN,
1045 CPU_SSE4A_FLAGS, 0 },
8a2c8fef 1046 { STRING_COMMA_LEN (".ssse3"), PROCESSOR_UNKNOWN,
293f5f65 1047 CPU_SSSE3_FLAGS, 0 },
8a2c8fef 1048 { STRING_COMMA_LEN (".sse4.1"), PROCESSOR_UNKNOWN,
293f5f65 1049 CPU_SSE4_1_FLAGS, 0 },
8a2c8fef 1050 { STRING_COMMA_LEN (".sse4.2"), PROCESSOR_UNKNOWN,
293f5f65 1051 CPU_SSE4_2_FLAGS, 0 },
8a2c8fef 1052 { STRING_COMMA_LEN (".sse4"), PROCESSOR_UNKNOWN,
293f5f65 1053 CPU_SSE4_2_FLAGS, 0 },
8a2c8fef 1054 { STRING_COMMA_LEN (".avx"), PROCESSOR_UNKNOWN,
293f5f65 1055 CPU_AVX_FLAGS, 0 },
6c30d220 1056 { STRING_COMMA_LEN (".avx2"), PROCESSOR_UNKNOWN,
293f5f65 1057 CPU_AVX2_FLAGS, 0 },
43234a1e 1058 { STRING_COMMA_LEN (".avx512f"), PROCESSOR_UNKNOWN,
293f5f65 1059 CPU_AVX512F_FLAGS, 0 },
43234a1e 1060 { STRING_COMMA_LEN (".avx512cd"), PROCESSOR_UNKNOWN,
293f5f65 1061 CPU_AVX512CD_FLAGS, 0 },
43234a1e 1062 { STRING_COMMA_LEN (".avx512er"), PROCESSOR_UNKNOWN,
293f5f65 1063 CPU_AVX512ER_FLAGS, 0 },
43234a1e 1064 { STRING_COMMA_LEN (".avx512pf"), PROCESSOR_UNKNOWN,
293f5f65 1065 CPU_AVX512PF_FLAGS, 0 },
1dfc6506 1066 { STRING_COMMA_LEN (".avx512dq"), PROCESSOR_UNKNOWN,
293f5f65 1067 CPU_AVX512DQ_FLAGS, 0 },
1dfc6506 1068 { STRING_COMMA_LEN (".avx512bw"), PROCESSOR_UNKNOWN,
293f5f65 1069 CPU_AVX512BW_FLAGS, 0 },
1dfc6506 1070 { STRING_COMMA_LEN (".avx512vl"), PROCESSOR_UNKNOWN,
293f5f65 1071 CPU_AVX512VL_FLAGS, 0 },
8a2c8fef 1072 { STRING_COMMA_LEN (".vmx"), PROCESSOR_UNKNOWN,
293f5f65 1073 CPU_VMX_FLAGS, 0 },
8729a6f6 1074 { STRING_COMMA_LEN (".vmfunc"), PROCESSOR_UNKNOWN,
293f5f65 1075 CPU_VMFUNC_FLAGS, 0 },
8a2c8fef 1076 { STRING_COMMA_LEN (".smx"), PROCESSOR_UNKNOWN,
293f5f65 1077 CPU_SMX_FLAGS, 0 },
8a2c8fef 1078 { STRING_COMMA_LEN (".xsave"), PROCESSOR_UNKNOWN,
293f5f65 1079 CPU_XSAVE_FLAGS, 0 },
c7b8aa3a 1080 { STRING_COMMA_LEN (".xsaveopt"), PROCESSOR_UNKNOWN,
293f5f65 1081 CPU_XSAVEOPT_FLAGS, 0 },
1dfc6506 1082 { STRING_COMMA_LEN (".xsavec"), PROCESSOR_UNKNOWN,
293f5f65 1083 CPU_XSAVEC_FLAGS, 0 },
1dfc6506 1084 { STRING_COMMA_LEN (".xsaves"), PROCESSOR_UNKNOWN,
293f5f65 1085 CPU_XSAVES_FLAGS, 0 },
8a2c8fef 1086 { STRING_COMMA_LEN (".aes"), PROCESSOR_UNKNOWN,
293f5f65 1087 CPU_AES_FLAGS, 0 },
8a2c8fef 1088 { STRING_COMMA_LEN (".pclmul"), PROCESSOR_UNKNOWN,
293f5f65 1089 CPU_PCLMUL_FLAGS, 0 },
8a2c8fef 1090 { STRING_COMMA_LEN (".clmul"), PROCESSOR_UNKNOWN,
293f5f65 1091 CPU_PCLMUL_FLAGS, 1 },
c7b8aa3a 1092 { STRING_COMMA_LEN (".fsgsbase"), PROCESSOR_UNKNOWN,
293f5f65 1093 CPU_FSGSBASE_FLAGS, 0 },
c7b8aa3a 1094 { STRING_COMMA_LEN (".rdrnd"), PROCESSOR_UNKNOWN,
293f5f65 1095 CPU_RDRND_FLAGS, 0 },
c7b8aa3a 1096 { STRING_COMMA_LEN (".f16c"), PROCESSOR_UNKNOWN,
293f5f65 1097 CPU_F16C_FLAGS, 0 },
6c30d220 1098 { STRING_COMMA_LEN (".bmi2"), PROCESSOR_UNKNOWN,
293f5f65 1099 CPU_BMI2_FLAGS, 0 },
8a2c8fef 1100 { STRING_COMMA_LEN (".fma"), PROCESSOR_UNKNOWN,
293f5f65 1101 CPU_FMA_FLAGS, 0 },
8a2c8fef 1102 { STRING_COMMA_LEN (".fma4"), PROCESSOR_UNKNOWN,
293f5f65 1103 CPU_FMA4_FLAGS, 0 },
8a2c8fef 1104 { STRING_COMMA_LEN (".xop"), PROCESSOR_UNKNOWN,
293f5f65 1105 CPU_XOP_FLAGS, 0 },
8a2c8fef 1106 { STRING_COMMA_LEN (".lwp"), PROCESSOR_UNKNOWN,
293f5f65 1107 CPU_LWP_FLAGS, 0 },
8a2c8fef 1108 { STRING_COMMA_LEN (".movbe"), PROCESSOR_UNKNOWN,
293f5f65 1109 CPU_MOVBE_FLAGS, 0 },
60aa667e 1110 { STRING_COMMA_LEN (".cx16"), PROCESSOR_UNKNOWN,
293f5f65 1111 CPU_CX16_FLAGS, 0 },
8a2c8fef 1112 { STRING_COMMA_LEN (".ept"), PROCESSOR_UNKNOWN,
293f5f65 1113 CPU_EPT_FLAGS, 0 },
6c30d220 1114 { STRING_COMMA_LEN (".lzcnt"), PROCESSOR_UNKNOWN,
293f5f65 1115 CPU_LZCNT_FLAGS, 0 },
272a84b1
L
1116 { STRING_COMMA_LEN (".popcnt"), PROCESSOR_UNKNOWN,
1117 CPU_POPCNT_FLAGS, 0 },
42164a71 1118 { STRING_COMMA_LEN (".hle"), PROCESSOR_UNKNOWN,
293f5f65 1119 CPU_HLE_FLAGS, 0 },
42164a71 1120 { STRING_COMMA_LEN (".rtm"), PROCESSOR_UNKNOWN,
293f5f65 1121 CPU_RTM_FLAGS, 0 },
6c30d220 1122 { STRING_COMMA_LEN (".invpcid"), PROCESSOR_UNKNOWN,
293f5f65 1123 CPU_INVPCID_FLAGS, 0 },
8a2c8fef 1124 { STRING_COMMA_LEN (".clflush"), PROCESSOR_UNKNOWN,
293f5f65 1125 CPU_CLFLUSH_FLAGS, 0 },
22109423 1126 { STRING_COMMA_LEN (".nop"), PROCESSOR_UNKNOWN,
293f5f65 1127 CPU_NOP_FLAGS, 0 },
8a2c8fef 1128 { STRING_COMMA_LEN (".syscall"), PROCESSOR_UNKNOWN,
293f5f65 1129 CPU_SYSCALL_FLAGS, 0 },
8a2c8fef 1130 { STRING_COMMA_LEN (".rdtscp"), PROCESSOR_UNKNOWN,
293f5f65 1131 CPU_RDTSCP_FLAGS, 0 },
8a2c8fef 1132 { STRING_COMMA_LEN (".3dnow"), PROCESSOR_UNKNOWN,
293f5f65 1133 CPU_3DNOW_FLAGS, 0 },
8a2c8fef 1134 { STRING_COMMA_LEN (".3dnowa"), PROCESSOR_UNKNOWN,
293f5f65 1135 CPU_3DNOWA_FLAGS, 0 },
8a2c8fef 1136 { STRING_COMMA_LEN (".padlock"), PROCESSOR_UNKNOWN,
293f5f65 1137 CPU_PADLOCK_FLAGS, 0 },
8a2c8fef 1138 { STRING_COMMA_LEN (".pacifica"), PROCESSOR_UNKNOWN,
293f5f65 1139 CPU_SVME_FLAGS, 1 },
8a2c8fef 1140 { STRING_COMMA_LEN (".svme"), PROCESSOR_UNKNOWN,
293f5f65 1141 CPU_SVME_FLAGS, 0 },
8a2c8fef 1142 { STRING_COMMA_LEN (".sse4a"), PROCESSOR_UNKNOWN,
293f5f65 1143 CPU_SSE4A_FLAGS, 0 },
8a2c8fef 1144 { STRING_COMMA_LEN (".abm"), PROCESSOR_UNKNOWN,
293f5f65 1145 CPU_ABM_FLAGS, 0 },
87973e9f 1146 { STRING_COMMA_LEN (".bmi"), PROCESSOR_UNKNOWN,
293f5f65 1147 CPU_BMI_FLAGS, 0 },
2a2a0f38 1148 { STRING_COMMA_LEN (".tbm"), PROCESSOR_UNKNOWN,
293f5f65 1149 CPU_TBM_FLAGS, 0 },
e2e1fcde 1150 { STRING_COMMA_LEN (".adx"), PROCESSOR_UNKNOWN,
293f5f65 1151 CPU_ADX_FLAGS, 0 },
e2e1fcde 1152 { STRING_COMMA_LEN (".rdseed"), PROCESSOR_UNKNOWN,
293f5f65 1153 CPU_RDSEED_FLAGS, 0 },
e2e1fcde 1154 { STRING_COMMA_LEN (".prfchw"), PROCESSOR_UNKNOWN,
293f5f65 1155 CPU_PRFCHW_FLAGS, 0 },
5c111e37 1156 { STRING_COMMA_LEN (".smap"), PROCESSOR_UNKNOWN,
293f5f65 1157 CPU_SMAP_FLAGS, 0 },
7e8b059b 1158 { STRING_COMMA_LEN (".mpx"), PROCESSOR_UNKNOWN,
293f5f65 1159 CPU_MPX_FLAGS, 0 },
a0046408 1160 { STRING_COMMA_LEN (".sha"), PROCESSOR_UNKNOWN,
293f5f65 1161 CPU_SHA_FLAGS, 0 },
963f3586 1162 { STRING_COMMA_LEN (".clflushopt"), PROCESSOR_UNKNOWN,
293f5f65 1163 CPU_CLFLUSHOPT_FLAGS, 0 },
dcf893b5 1164 { STRING_COMMA_LEN (".prefetchwt1"), PROCESSOR_UNKNOWN,
293f5f65 1165 CPU_PREFETCHWT1_FLAGS, 0 },
2cf200a4 1166 { STRING_COMMA_LEN (".se1"), PROCESSOR_UNKNOWN,
293f5f65 1167 CPU_SE1_FLAGS, 0 },
c5e7287a 1168 { STRING_COMMA_LEN (".clwb"), PROCESSOR_UNKNOWN,
293f5f65 1169 CPU_CLWB_FLAGS, 0 },
2cc1b5aa 1170 { STRING_COMMA_LEN (".avx512ifma"), PROCESSOR_UNKNOWN,
293f5f65 1171 CPU_AVX512IFMA_FLAGS, 0 },
14f195c9 1172 { STRING_COMMA_LEN (".avx512vbmi"), PROCESSOR_UNKNOWN,
293f5f65 1173 CPU_AVX512VBMI_FLAGS, 0 },
920d2ddc
IT
1174 { STRING_COMMA_LEN (".avx512_4fmaps"), PROCESSOR_UNKNOWN,
1175 CPU_AVX512_4FMAPS_FLAGS, 0 },
47acf0bd
IT
1176 { STRING_COMMA_LEN (".avx512_4vnniw"), PROCESSOR_UNKNOWN,
1177 CPU_AVX512_4VNNIW_FLAGS, 0 },
620214f7
IT
1178 { STRING_COMMA_LEN (".avx512_vpopcntdq"), PROCESSOR_UNKNOWN,
1179 CPU_AVX512_VPOPCNTDQ_FLAGS, 0 },
53467f57
IT
1180 { STRING_COMMA_LEN (".avx512_vbmi2"), PROCESSOR_UNKNOWN,
1181 CPU_AVX512_VBMI2_FLAGS, 0 },
8cfcb765
IT
1182 { STRING_COMMA_LEN (".avx512_vnni"), PROCESSOR_UNKNOWN,
1183 CPU_AVX512_VNNI_FLAGS, 0 },
ee6872be
IT
1184 { STRING_COMMA_LEN (".avx512_bitalg"), PROCESSOR_UNKNOWN,
1185 CPU_AVX512_BITALG_FLAGS, 0 },
58bf9b6a
L
1186 { STRING_COMMA_LEN (".avx_vnni"), PROCESSOR_UNKNOWN,
1187 CPU_AVX_VNNI_FLAGS, 0 },
029f3522 1188 { STRING_COMMA_LEN (".clzero"), PROCESSOR_UNKNOWN,
293f5f65 1189 CPU_CLZERO_FLAGS, 0 },
9916071f 1190 { STRING_COMMA_LEN (".mwaitx"), PROCESSOR_UNKNOWN,
293f5f65 1191 CPU_MWAITX_FLAGS, 0 },
8eab4136 1192 { STRING_COMMA_LEN (".ospke"), PROCESSOR_UNKNOWN,
293f5f65 1193 CPU_OSPKE_FLAGS, 0 },
8bc52696 1194 { STRING_COMMA_LEN (".rdpid"), PROCESSOR_UNKNOWN,
293f5f65 1195 CPU_RDPID_FLAGS, 0 },
6b40c462
L
1196 { STRING_COMMA_LEN (".ptwrite"), PROCESSOR_UNKNOWN,
1197 CPU_PTWRITE_FLAGS, 0 },
d777820b
IT
1198 { STRING_COMMA_LEN (".ibt"), PROCESSOR_UNKNOWN,
1199 CPU_IBT_FLAGS, 0 },
1200 { STRING_COMMA_LEN (".shstk"), PROCESSOR_UNKNOWN,
1201 CPU_SHSTK_FLAGS, 0 },
48521003
IT
1202 { STRING_COMMA_LEN (".gfni"), PROCESSOR_UNKNOWN,
1203 CPU_GFNI_FLAGS, 0 },
8dcf1fad
IT
1204 { STRING_COMMA_LEN (".vaes"), PROCESSOR_UNKNOWN,
1205 CPU_VAES_FLAGS, 0 },
ff1982d5
IT
1206 { STRING_COMMA_LEN (".vpclmulqdq"), PROCESSOR_UNKNOWN,
1207 CPU_VPCLMULQDQ_FLAGS, 0 },
3233d7d0
IT
1208 { STRING_COMMA_LEN (".wbnoinvd"), PROCESSOR_UNKNOWN,
1209 CPU_WBNOINVD_FLAGS, 0 },
be3a8dca
IT
1210 { STRING_COMMA_LEN (".pconfig"), PROCESSOR_UNKNOWN,
1211 CPU_PCONFIG_FLAGS, 0 },
de89d0a3
IT
1212 { STRING_COMMA_LEN (".waitpkg"), PROCESSOR_UNKNOWN,
1213 CPU_WAITPKG_FLAGS, 0 },
c48935d7
IT
1214 { STRING_COMMA_LEN (".cldemote"), PROCESSOR_UNKNOWN,
1215 CPU_CLDEMOTE_FLAGS, 0 },
260cd341
LC
1216 { STRING_COMMA_LEN (".amx_int8"), PROCESSOR_UNKNOWN,
1217 CPU_AMX_INT8_FLAGS, 0 },
1218 { STRING_COMMA_LEN (".amx_bf16"), PROCESSOR_UNKNOWN,
1219 CPU_AMX_BF16_FLAGS, 0 },
1220 { STRING_COMMA_LEN (".amx_tile"), PROCESSOR_UNKNOWN,
1221 CPU_AMX_TILE_FLAGS, 0 },
c0a30a9f
L
1222 { STRING_COMMA_LEN (".movdiri"), PROCESSOR_UNKNOWN,
1223 CPU_MOVDIRI_FLAGS, 0 },
1224 { STRING_COMMA_LEN (".movdir64b"), PROCESSOR_UNKNOWN,
1225 CPU_MOVDIR64B_FLAGS, 0 },
d6aab7a1
XG
1226 { STRING_COMMA_LEN (".avx512_bf16"), PROCESSOR_UNKNOWN,
1227 CPU_AVX512_BF16_FLAGS, 0 },
9186c494
L
1228 { STRING_COMMA_LEN (".avx512_vp2intersect"), PROCESSOR_UNKNOWN,
1229 CPU_AVX512_VP2INTERSECT_FLAGS, 0 },
81d54bb7
CL
1230 { STRING_COMMA_LEN (".tdx"), PROCESSOR_UNKNOWN,
1231 CPU_TDX_FLAGS, 0 },
dd455cf5
L
1232 { STRING_COMMA_LEN (".enqcmd"), PROCESSOR_UNKNOWN,
1233 CPU_ENQCMD_FLAGS, 0 },
4b27d27c
L
1234 { STRING_COMMA_LEN (".serialize"), PROCESSOR_UNKNOWN,
1235 CPU_SERIALIZE_FLAGS, 0 },
142861df
JB
1236 { STRING_COMMA_LEN (".rdpru"), PROCESSOR_UNKNOWN,
1237 CPU_RDPRU_FLAGS, 0 },
1238 { STRING_COMMA_LEN (".mcommit"), PROCESSOR_UNKNOWN,
1239 CPU_MCOMMIT_FLAGS, 0 },
a847e322
JB
1240 { STRING_COMMA_LEN (".sev_es"), PROCESSOR_UNKNOWN,
1241 CPU_SEV_ES_FLAGS, 0 },
bb651e8b
CL
1242 { STRING_COMMA_LEN (".tsxldtrk"), PROCESSOR_UNKNOWN,
1243 CPU_TSXLDTRK_FLAGS, 0 },
c4694f17
TG
1244 { STRING_COMMA_LEN (".kl"), PROCESSOR_UNKNOWN,
1245 CPU_KL_FLAGS, 0 },
1246 { STRING_COMMA_LEN (".widekl"), PROCESSOR_UNKNOWN,
1247 CPU_WIDEKL_FLAGS, 0 },
f64c42a9
LC
1248 { STRING_COMMA_LEN (".uintr"), PROCESSOR_UNKNOWN,
1249 CPU_UINTR_FLAGS, 0 },
c1fa250a
LC
1250 { STRING_COMMA_LEN (".hreset"), PROCESSOR_UNKNOWN,
1251 CPU_HRESET_FLAGS, 0 },
293f5f65
L
1252};
1253
1254static const noarch_entry cpu_noarch[] =
1255{
1256 { STRING_COMMA_LEN ("no87"), CPU_ANY_X87_FLAGS },
1848e567
L
1257 { STRING_COMMA_LEN ("no287"), CPU_ANY_287_FLAGS },
1258 { STRING_COMMA_LEN ("no387"), CPU_ANY_387_FLAGS },
1259 { STRING_COMMA_LEN ("no687"), CPU_ANY_687_FLAGS },
d871f3f4
L
1260 { STRING_COMMA_LEN ("nocmov"), CPU_ANY_CMOV_FLAGS },
1261 { STRING_COMMA_LEN ("nofxsr"), CPU_ANY_FXSR_FLAGS },
293f5f65
L
1262 { STRING_COMMA_LEN ("nommx"), CPU_ANY_MMX_FLAGS },
1263 { STRING_COMMA_LEN ("nosse"), CPU_ANY_SSE_FLAGS },
1848e567
L
1264 { STRING_COMMA_LEN ("nosse2"), CPU_ANY_SSE2_FLAGS },
1265 { STRING_COMMA_LEN ("nosse3"), CPU_ANY_SSE3_FLAGS },
af5c13b0 1266 { STRING_COMMA_LEN ("nosse4a"), CPU_ANY_SSE4A_FLAGS },
1848e567
L
1267 { STRING_COMMA_LEN ("nossse3"), CPU_ANY_SSSE3_FLAGS },
1268 { STRING_COMMA_LEN ("nosse4.1"), CPU_ANY_SSE4_1_FLAGS },
1269 { STRING_COMMA_LEN ("nosse4.2"), CPU_ANY_SSE4_2_FLAGS },
af5c13b0 1270 { STRING_COMMA_LEN ("nosse4"), CPU_ANY_SSE4_1_FLAGS },
293f5f65 1271 { STRING_COMMA_LEN ("noavx"), CPU_ANY_AVX_FLAGS },
1848e567 1272 { STRING_COMMA_LEN ("noavx2"), CPU_ANY_AVX2_FLAGS },
144b71e2
L
1273 { STRING_COMMA_LEN ("noavx512f"), CPU_ANY_AVX512F_FLAGS },
1274 { STRING_COMMA_LEN ("noavx512cd"), CPU_ANY_AVX512CD_FLAGS },
1275 { STRING_COMMA_LEN ("noavx512er"), CPU_ANY_AVX512ER_FLAGS },
1276 { STRING_COMMA_LEN ("noavx512pf"), CPU_ANY_AVX512PF_FLAGS },
1277 { STRING_COMMA_LEN ("noavx512dq"), CPU_ANY_AVX512DQ_FLAGS },
1278 { STRING_COMMA_LEN ("noavx512bw"), CPU_ANY_AVX512BW_FLAGS },
1279 { STRING_COMMA_LEN ("noavx512vl"), CPU_ANY_AVX512VL_FLAGS },
1280 { STRING_COMMA_LEN ("noavx512ifma"), CPU_ANY_AVX512IFMA_FLAGS },
1281 { STRING_COMMA_LEN ("noavx512vbmi"), CPU_ANY_AVX512VBMI_FLAGS },
920d2ddc 1282 { STRING_COMMA_LEN ("noavx512_4fmaps"), CPU_ANY_AVX512_4FMAPS_FLAGS },
47acf0bd 1283 { STRING_COMMA_LEN ("noavx512_4vnniw"), CPU_ANY_AVX512_4VNNIW_FLAGS },
620214f7 1284 { STRING_COMMA_LEN ("noavx512_vpopcntdq"), CPU_ANY_AVX512_VPOPCNTDQ_FLAGS },
53467f57 1285 { STRING_COMMA_LEN ("noavx512_vbmi2"), CPU_ANY_AVX512_VBMI2_FLAGS },
8cfcb765 1286 { STRING_COMMA_LEN ("noavx512_vnni"), CPU_ANY_AVX512_VNNI_FLAGS },
ee6872be 1287 { STRING_COMMA_LEN ("noavx512_bitalg"), CPU_ANY_AVX512_BITALG_FLAGS },
58bf9b6a 1288 { STRING_COMMA_LEN ("noavx_vnni"), CPU_ANY_AVX_VNNI_FLAGS },
d777820b
IT
1289 { STRING_COMMA_LEN ("noibt"), CPU_ANY_IBT_FLAGS },
1290 { STRING_COMMA_LEN ("noshstk"), CPU_ANY_SHSTK_FLAGS },
260cd341
LC
1291 { STRING_COMMA_LEN ("noamx_int8"), CPU_ANY_AMX_INT8_FLAGS },
1292 { STRING_COMMA_LEN ("noamx_bf16"), CPU_ANY_AMX_BF16_FLAGS },
1293 { STRING_COMMA_LEN ("noamx_tile"), CPU_ANY_AMX_TILE_FLAGS },
c0a30a9f
L
1294 { STRING_COMMA_LEN ("nomovdiri"), CPU_ANY_MOVDIRI_FLAGS },
1295 { STRING_COMMA_LEN ("nomovdir64b"), CPU_ANY_MOVDIR64B_FLAGS },
d6aab7a1 1296 { STRING_COMMA_LEN ("noavx512_bf16"), CPU_ANY_AVX512_BF16_FLAGS },
708a2fff
CL
1297 { STRING_COMMA_LEN ("noavx512_vp2intersect"),
1298 CPU_ANY_AVX512_VP2INTERSECT_FLAGS },
81d54bb7 1299 { STRING_COMMA_LEN ("notdx"), CPU_ANY_TDX_FLAGS },
dd455cf5 1300 { STRING_COMMA_LEN ("noenqcmd"), CPU_ANY_ENQCMD_FLAGS },
4b27d27c 1301 { STRING_COMMA_LEN ("noserialize"), CPU_ANY_SERIALIZE_FLAGS },
bb651e8b 1302 { STRING_COMMA_LEN ("notsxldtrk"), CPU_ANY_TSXLDTRK_FLAGS },
c4694f17
TG
1303 { STRING_COMMA_LEN ("nokl"), CPU_ANY_KL_FLAGS },
1304 { STRING_COMMA_LEN ("nowidekl"), CPU_ANY_WIDEKL_FLAGS },
f64c42a9 1305 { STRING_COMMA_LEN ("nouintr"), CPU_ANY_UINTR_FLAGS },
c1fa250a 1306 { STRING_COMMA_LEN ("nohreset"), CPU_ANY_HRESET_FLAGS },
e413e4e9
AM
1307};
1308
704209c0 1309#ifdef I386COFF
a6c24e68
NC
1310/* Like s_lcomm_internal in gas/read.c but the alignment string
1311 is allowed to be optional. */
1312
1313static symbolS *
1314pe_lcomm_internal (int needs_align, symbolS *symbolP, addressT size)
1315{
1316 addressT align = 0;
1317
1318 SKIP_WHITESPACE ();
1319
7ab9ffdd 1320 if (needs_align
a6c24e68
NC
1321 && *input_line_pointer == ',')
1322 {
1323 align = parse_align (needs_align - 1);
7ab9ffdd 1324
a6c24e68
NC
1325 if (align == (addressT) -1)
1326 return NULL;
1327 }
1328 else
1329 {
1330 if (size >= 8)
1331 align = 3;
1332 else if (size >= 4)
1333 align = 2;
1334 else if (size >= 2)
1335 align = 1;
1336 else
1337 align = 0;
1338 }
1339
1340 bss_alloc (symbolP, size, align);
1341 return symbolP;
1342}
1343
704209c0 1344static void
a6c24e68
NC
1345pe_lcomm (int needs_align)
1346{
1347 s_comm_internal (needs_align * 2, pe_lcomm_internal);
1348}
704209c0 1349#endif
a6c24e68 1350
29b0f896
AM
1351const pseudo_typeS md_pseudo_table[] =
1352{
1353#if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
1354 {"align", s_align_bytes, 0},
1355#else
1356 {"align", s_align_ptwo, 0},
1357#endif
1358 {"arch", set_cpu_arch, 0},
1359#ifndef I386COFF
1360 {"bss", s_bss, 0},
a6c24e68
NC
1361#else
1362 {"lcomm", pe_lcomm, 1},
29b0f896
AM
1363#endif
1364 {"ffloat", float_cons, 'f'},
1365 {"dfloat", float_cons, 'd'},
1366 {"tfloat", float_cons, 'x'},
1367 {"value", cons, 2},
d182319b 1368 {"slong", signed_cons, 4},
29b0f896
AM
1369 {"noopt", s_ignore, 0},
1370 {"optim", s_ignore, 0},
1371 {"code16gcc", set_16bit_gcc_code_flag, CODE_16BIT},
1372 {"code16", set_code_flag, CODE_16BIT},
1373 {"code32", set_code_flag, CODE_32BIT},
da5f19a2 1374#ifdef BFD64
29b0f896 1375 {"code64", set_code_flag, CODE_64BIT},
da5f19a2 1376#endif
29b0f896
AM
1377 {"intel_syntax", set_intel_syntax, 1},
1378 {"att_syntax", set_intel_syntax, 0},
1efbbeb4
L
1379 {"intel_mnemonic", set_intel_mnemonic, 1},
1380 {"att_mnemonic", set_intel_mnemonic, 0},
db51cc60
L
1381 {"allow_index_reg", set_allow_index_reg, 1},
1382 {"disallow_index_reg", set_allow_index_reg, 0},
7bab8ab5
JB
1383 {"sse_check", set_check, 0},
1384 {"operand_check", set_check, 1},
3b22753a
L
1385#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1386 {"largecomm", handle_large_common, 0},
07a53e5c 1387#else
68d20676 1388 {"file", dwarf2_directive_file, 0},
07a53e5c
RH
1389 {"loc", dwarf2_directive_loc, 0},
1390 {"loc_mark_labels", dwarf2_directive_loc_mark_labels, 0},
3b22753a 1391#endif
6482c264
NC
1392#ifdef TE_PE
1393 {"secrel32", pe_directive_secrel, 0},
1394#endif
29b0f896
AM
1395 {0, 0, 0}
1396};
1397
1398/* For interface with expression (). */
1399extern char *input_line_pointer;
1400
1401/* Hash table for instruction mnemonic lookup. */
629310ab 1402static htab_t op_hash;
29b0f896
AM
1403
1404/* Hash table for register lookup. */
629310ab 1405static htab_t reg_hash;
29b0f896 1406\f
ce8a8b2f
AM
1407 /* Various efficient no-op patterns for aligning code labels.
1408 Note: Don't try to assemble the instructions in the comments.
1409 0L and 0w are not legal. */
62a02d25
L
1410static const unsigned char f32_1[] =
1411 {0x90}; /* nop */
1412static const unsigned char f32_2[] =
1413 {0x66,0x90}; /* xchg %ax,%ax */
1414static const unsigned char f32_3[] =
1415 {0x8d,0x76,0x00}; /* leal 0(%esi),%esi */
1416static const unsigned char f32_4[] =
1417 {0x8d,0x74,0x26,0x00}; /* leal 0(%esi,1),%esi */
62a02d25
L
1418static const unsigned char f32_6[] =
1419 {0x8d,0xb6,0x00,0x00,0x00,0x00}; /* leal 0L(%esi),%esi */
1420static const unsigned char f32_7[] =
1421 {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal 0L(%esi,1),%esi */
62a02d25 1422static const unsigned char f16_3[] =
3ae729d5 1423 {0x8d,0x74,0x00}; /* lea 0(%si),%si */
62a02d25 1424static const unsigned char f16_4[] =
3ae729d5
L
1425 {0x8d,0xb4,0x00,0x00}; /* lea 0W(%si),%si */
1426static const unsigned char jump_disp8[] =
1427 {0xeb}; /* jmp disp8 */
1428static const unsigned char jump32_disp32[] =
1429 {0xe9}; /* jmp disp32 */
1430static const unsigned char jump16_disp32[] =
1431 {0x66,0xe9}; /* jmp disp32 */
62a02d25
L
1432/* 32-bit NOPs patterns. */
1433static const unsigned char *const f32_patt[] = {
3ae729d5 1434 f32_1, f32_2, f32_3, f32_4, NULL, f32_6, f32_7
62a02d25
L
1435};
1436/* 16-bit NOPs patterns. */
1437static const unsigned char *const f16_patt[] = {
3ae729d5 1438 f32_1, f32_2, f16_3, f16_4
62a02d25
L
1439};
1440/* nopl (%[re]ax) */
1441static const unsigned char alt_3[] =
1442 {0x0f,0x1f,0x00};
1443/* nopl 0(%[re]ax) */
1444static const unsigned char alt_4[] =
1445 {0x0f,0x1f,0x40,0x00};
1446/* nopl 0(%[re]ax,%[re]ax,1) */
1447static const unsigned char alt_5[] =
1448 {0x0f,0x1f,0x44,0x00,0x00};
1449/* nopw 0(%[re]ax,%[re]ax,1) */
1450static const unsigned char alt_6[] =
1451 {0x66,0x0f,0x1f,0x44,0x00,0x00};
1452/* nopl 0L(%[re]ax) */
1453static const unsigned char alt_7[] =
1454 {0x0f,0x1f,0x80,0x00,0x00,0x00,0x00};
1455/* nopl 0L(%[re]ax,%[re]ax,1) */
1456static const unsigned char alt_8[] =
1457 {0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1458/* nopw 0L(%[re]ax,%[re]ax,1) */
1459static const unsigned char alt_9[] =
1460 {0x66,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1461/* nopw %cs:0L(%[re]ax,%[re]ax,1) */
1462static const unsigned char alt_10[] =
1463 {0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
3ae729d5
L
1464/* data16 nopw %cs:0L(%eax,%eax,1) */
1465static const unsigned char alt_11[] =
1466 {0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
62a02d25
L
1467/* 32-bit and 64-bit NOPs patterns. */
1468static const unsigned char *const alt_patt[] = {
1469 f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
3ae729d5 1470 alt_9, alt_10, alt_11
62a02d25
L
1471};
1472
1473/* Genenerate COUNT bytes of NOPs to WHERE from PATT with the maximum
1474 size of a single NOP instruction MAX_SINGLE_NOP_SIZE. */
1475
1476static void
1477i386_output_nops (char *where, const unsigned char *const *patt,
1478 int count, int max_single_nop_size)
1479
1480{
3ae729d5
L
1481 /* Place the longer NOP first. */
1482 int last;
1483 int offset;
3076e594
NC
1484 const unsigned char *nops;
1485
1486 if (max_single_nop_size < 1)
1487 {
1488 as_fatal (_("i386_output_nops called to generate nops of at most %d bytes!"),
1489 max_single_nop_size);
1490 return;
1491 }
1492
1493 nops = patt[max_single_nop_size - 1];
3ae729d5
L
1494
1495 /* Use the smaller one if the requsted one isn't available. */
1496 if (nops == NULL)
62a02d25 1497 {
3ae729d5
L
1498 max_single_nop_size--;
1499 nops = patt[max_single_nop_size - 1];
62a02d25
L
1500 }
1501
3ae729d5
L
1502 last = count % max_single_nop_size;
1503
1504 count -= last;
1505 for (offset = 0; offset < count; offset += max_single_nop_size)
1506 memcpy (where + offset, nops, max_single_nop_size);
1507
1508 if (last)
1509 {
1510 nops = patt[last - 1];
1511 if (nops == NULL)
1512 {
1513 /* Use the smaller one plus one-byte NOP if the needed one
1514 isn't available. */
1515 last--;
1516 nops = patt[last - 1];
1517 memcpy (where + offset, nops, last);
1518 where[offset + last] = *patt[0];
1519 }
1520 else
1521 memcpy (where + offset, nops, last);
1522 }
62a02d25
L
1523}
1524
3ae729d5
L
1525static INLINE int
1526fits_in_imm7 (offsetT num)
1527{
1528 return (num & 0x7f) == num;
1529}
1530
1531static INLINE int
1532fits_in_imm31 (offsetT num)
1533{
1534 return (num & 0x7fffffff) == num;
1535}
62a02d25
L
1536
1537/* Genenerate COUNT bytes of NOPs to WHERE with the maximum size of a
1538 single NOP instruction LIMIT. */
1539
1540void
3ae729d5 1541i386_generate_nops (fragS *fragP, char *where, offsetT count, int limit)
62a02d25 1542{
3ae729d5 1543 const unsigned char *const *patt = NULL;
62a02d25 1544 int max_single_nop_size;
3ae729d5
L
1545 /* Maximum number of NOPs before switching to jump over NOPs. */
1546 int max_number_of_nops;
62a02d25 1547
3ae729d5 1548 switch (fragP->fr_type)
62a02d25 1549 {
3ae729d5
L
1550 case rs_fill_nop:
1551 case rs_align_code:
1552 break;
e379e5f3
L
1553 case rs_machine_dependent:
1554 /* Allow NOP padding for jumps and calls. */
1555 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
1556 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
1557 break;
1558 /* Fall through. */
3ae729d5 1559 default:
62a02d25
L
1560 return;
1561 }
1562
ccc9c027
L
1563 /* We need to decide which NOP sequence to use for 32bit and
1564 64bit. When -mtune= is used:
4eed87de 1565
76bc74dc
L
1566 1. For PROCESSOR_I386, PROCESSOR_I486, PROCESSOR_PENTIUM and
1567 PROCESSOR_GENERIC32, f32_patt will be used.
80b8656c
L
1568 2. For the rest, alt_patt will be used.
1569
1570 When -mtune= isn't used, alt_patt will be used if
22109423 1571 cpu_arch_isa_flags has CpuNop. Otherwise, f32_patt will
76bc74dc 1572 be used.
ccc9c027
L
1573
1574 When -march= or .arch is used, we can't use anything beyond
1575 cpu_arch_isa_flags. */
1576
1577 if (flag_code == CODE_16BIT)
1578 {
3ae729d5
L
1579 patt = f16_patt;
1580 max_single_nop_size = sizeof (f16_patt) / sizeof (f16_patt[0]);
1581 /* Limit number of NOPs to 2 in 16-bit mode. */
1582 max_number_of_nops = 2;
252b5132 1583 }
33fef721 1584 else
ccc9c027 1585 {
fbf3f584 1586 if (fragP->tc_frag_data.isa == PROCESSOR_UNKNOWN)
ccc9c027
L
1587 {
1588 /* PROCESSOR_UNKNOWN means that all ISAs may be used. */
1589 switch (cpu_arch_tune)
1590 {
1591 case PROCESSOR_UNKNOWN:
1592 /* We use cpu_arch_isa_flags to check if we SHOULD
22109423
L
1593 optimize with nops. */
1594 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1595 patt = alt_patt;
ccc9c027
L
1596 else
1597 patt = f32_patt;
1598 break;
ccc9c027
L
1599 case PROCESSOR_PENTIUM4:
1600 case PROCESSOR_NOCONA:
ef05d495 1601 case PROCESSOR_CORE:
76bc74dc 1602 case PROCESSOR_CORE2:
bd5295b2 1603 case PROCESSOR_COREI7:
3632d14b 1604 case PROCESSOR_L1OM:
7a9068fe 1605 case PROCESSOR_K1OM:
76bc74dc 1606 case PROCESSOR_GENERIC64:
ccc9c027
L
1607 case PROCESSOR_K6:
1608 case PROCESSOR_ATHLON:
1609 case PROCESSOR_K8:
4eed87de 1610 case PROCESSOR_AMDFAM10:
8aedb9fe 1611 case PROCESSOR_BD:
029f3522 1612 case PROCESSOR_ZNVER:
7b458c12 1613 case PROCESSOR_BT:
80b8656c 1614 patt = alt_patt;
ccc9c027 1615 break;
76bc74dc 1616 case PROCESSOR_I386:
ccc9c027
L
1617 case PROCESSOR_I486:
1618 case PROCESSOR_PENTIUM:
2dde1948 1619 case PROCESSOR_PENTIUMPRO:
81486035 1620 case PROCESSOR_IAMCU:
ccc9c027
L
1621 case PROCESSOR_GENERIC32:
1622 patt = f32_patt;
1623 break;
4eed87de 1624 }
ccc9c027
L
1625 }
1626 else
1627 {
fbf3f584 1628 switch (fragP->tc_frag_data.tune)
ccc9c027
L
1629 {
1630 case PROCESSOR_UNKNOWN:
e6a14101 1631 /* When cpu_arch_isa is set, cpu_arch_tune shouldn't be
ccc9c027
L
1632 PROCESSOR_UNKNOWN. */
1633 abort ();
1634 break;
1635
76bc74dc 1636 case PROCESSOR_I386:
ccc9c027
L
1637 case PROCESSOR_I486:
1638 case PROCESSOR_PENTIUM:
81486035 1639 case PROCESSOR_IAMCU:
ccc9c027
L
1640 case PROCESSOR_K6:
1641 case PROCESSOR_ATHLON:
1642 case PROCESSOR_K8:
4eed87de 1643 case PROCESSOR_AMDFAM10:
8aedb9fe 1644 case PROCESSOR_BD:
029f3522 1645 case PROCESSOR_ZNVER:
7b458c12 1646 case PROCESSOR_BT:
ccc9c027
L
1647 case PROCESSOR_GENERIC32:
1648 /* We use cpu_arch_isa_flags to check if we CAN optimize
22109423
L
1649 with nops. */
1650 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1651 patt = alt_patt;
ccc9c027
L
1652 else
1653 patt = f32_patt;
1654 break;
76bc74dc
L
1655 case PROCESSOR_PENTIUMPRO:
1656 case PROCESSOR_PENTIUM4:
1657 case PROCESSOR_NOCONA:
1658 case PROCESSOR_CORE:
ef05d495 1659 case PROCESSOR_CORE2:
bd5295b2 1660 case PROCESSOR_COREI7:
3632d14b 1661 case PROCESSOR_L1OM:
7a9068fe 1662 case PROCESSOR_K1OM:
22109423 1663 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1664 patt = alt_patt;
ccc9c027
L
1665 else
1666 patt = f32_patt;
1667 break;
1668 case PROCESSOR_GENERIC64:
80b8656c 1669 patt = alt_patt;
ccc9c027 1670 break;
4eed87de 1671 }
ccc9c027
L
1672 }
1673
76bc74dc
L
1674 if (patt == f32_patt)
1675 {
3ae729d5
L
1676 max_single_nop_size = sizeof (f32_patt) / sizeof (f32_patt[0]);
1677 /* Limit number of NOPs to 2 for older processors. */
1678 max_number_of_nops = 2;
76bc74dc
L
1679 }
1680 else
1681 {
3ae729d5
L
1682 max_single_nop_size = sizeof (alt_patt) / sizeof (alt_patt[0]);
1683 /* Limit number of NOPs to 7 for newer processors. */
1684 max_number_of_nops = 7;
1685 }
1686 }
1687
1688 if (limit == 0)
1689 limit = max_single_nop_size;
1690
1691 if (fragP->fr_type == rs_fill_nop)
1692 {
1693 /* Output NOPs for .nop directive. */
1694 if (limit > max_single_nop_size)
1695 {
1696 as_bad_where (fragP->fr_file, fragP->fr_line,
1697 _("invalid single nop size: %d "
1698 "(expect within [0, %d])"),
1699 limit, max_single_nop_size);
1700 return;
1701 }
1702 }
e379e5f3 1703 else if (fragP->fr_type != rs_machine_dependent)
3ae729d5
L
1704 fragP->fr_var = count;
1705
1706 if ((count / max_single_nop_size) > max_number_of_nops)
1707 {
1708 /* Generate jump over NOPs. */
1709 offsetT disp = count - 2;
1710 if (fits_in_imm7 (disp))
1711 {
1712 /* Use "jmp disp8" if possible. */
1713 count = disp;
1714 where[0] = jump_disp8[0];
1715 where[1] = count;
1716 where += 2;
1717 }
1718 else
1719 {
1720 unsigned int size_of_jump;
1721
1722 if (flag_code == CODE_16BIT)
1723 {
1724 where[0] = jump16_disp32[0];
1725 where[1] = jump16_disp32[1];
1726 size_of_jump = 2;
1727 }
1728 else
1729 {
1730 where[0] = jump32_disp32[0];
1731 size_of_jump = 1;
1732 }
1733
1734 count -= size_of_jump + 4;
1735 if (!fits_in_imm31 (count))
1736 {
1737 as_bad_where (fragP->fr_file, fragP->fr_line,
1738 _("jump over nop padding out of range"));
1739 return;
1740 }
1741
1742 md_number_to_chars (where + size_of_jump, count, 4);
1743 where += size_of_jump + 4;
76bc74dc 1744 }
ccc9c027 1745 }
3ae729d5
L
1746
1747 /* Generate multiple NOPs. */
1748 i386_output_nops (where, patt, count, limit);
252b5132
RH
1749}
1750
c6fb90c8 1751static INLINE int
0dfbf9d7 1752operand_type_all_zero (const union i386_operand_type *x)
40fb9820 1753{
0dfbf9d7 1754 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1755 {
1756 case 3:
0dfbf9d7 1757 if (x->array[2])
c6fb90c8 1758 return 0;
1a0670f3 1759 /* Fall through. */
c6fb90c8 1760 case 2:
0dfbf9d7 1761 if (x->array[1])
c6fb90c8 1762 return 0;
1a0670f3 1763 /* Fall through. */
c6fb90c8 1764 case 1:
0dfbf9d7 1765 return !x->array[0];
c6fb90c8
L
1766 default:
1767 abort ();
1768 }
40fb9820
L
1769}
1770
c6fb90c8 1771static INLINE void
0dfbf9d7 1772operand_type_set (union i386_operand_type *x, unsigned int v)
40fb9820 1773{
0dfbf9d7 1774 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1775 {
1776 case 3:
0dfbf9d7 1777 x->array[2] = v;
1a0670f3 1778 /* Fall through. */
c6fb90c8 1779 case 2:
0dfbf9d7 1780 x->array[1] = v;
1a0670f3 1781 /* Fall through. */
c6fb90c8 1782 case 1:
0dfbf9d7 1783 x->array[0] = v;
1a0670f3 1784 /* Fall through. */
c6fb90c8
L
1785 break;
1786 default:
1787 abort ();
1788 }
bab6aec1
JB
1789
1790 x->bitfield.class = ClassNone;
75e5731b 1791 x->bitfield.instance = InstanceNone;
c6fb90c8 1792}
40fb9820 1793
c6fb90c8 1794static INLINE int
0dfbf9d7
L
1795operand_type_equal (const union i386_operand_type *x,
1796 const union i386_operand_type *y)
c6fb90c8 1797{
0dfbf9d7 1798 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1799 {
1800 case 3:
0dfbf9d7 1801 if (x->array[2] != y->array[2])
c6fb90c8 1802 return 0;
1a0670f3 1803 /* Fall through. */
c6fb90c8 1804 case 2:
0dfbf9d7 1805 if (x->array[1] != y->array[1])
c6fb90c8 1806 return 0;
1a0670f3 1807 /* Fall through. */
c6fb90c8 1808 case 1:
0dfbf9d7 1809 return x->array[0] == y->array[0];
c6fb90c8
L
1810 break;
1811 default:
1812 abort ();
1813 }
1814}
40fb9820 1815
0dfbf9d7
L
1816static INLINE int
1817cpu_flags_all_zero (const union i386_cpu_flags *x)
1818{
1819 switch (ARRAY_SIZE(x->array))
1820 {
53467f57
IT
1821 case 4:
1822 if (x->array[3])
1823 return 0;
1824 /* Fall through. */
0dfbf9d7
L
1825 case 3:
1826 if (x->array[2])
1827 return 0;
1a0670f3 1828 /* Fall through. */
0dfbf9d7
L
1829 case 2:
1830 if (x->array[1])
1831 return 0;
1a0670f3 1832 /* Fall through. */
0dfbf9d7
L
1833 case 1:
1834 return !x->array[0];
1835 default:
1836 abort ();
1837 }
1838}
1839
0dfbf9d7
L
1840static INLINE int
1841cpu_flags_equal (const union i386_cpu_flags *x,
1842 const union i386_cpu_flags *y)
1843{
1844 switch (ARRAY_SIZE(x->array))
1845 {
53467f57
IT
1846 case 4:
1847 if (x->array[3] != y->array[3])
1848 return 0;
1849 /* Fall through. */
0dfbf9d7
L
1850 case 3:
1851 if (x->array[2] != y->array[2])
1852 return 0;
1a0670f3 1853 /* Fall through. */
0dfbf9d7
L
1854 case 2:
1855 if (x->array[1] != y->array[1])
1856 return 0;
1a0670f3 1857 /* Fall through. */
0dfbf9d7
L
1858 case 1:
1859 return x->array[0] == y->array[0];
1860 break;
1861 default:
1862 abort ();
1863 }
1864}
c6fb90c8
L
1865
1866static INLINE int
1867cpu_flags_check_cpu64 (i386_cpu_flags f)
1868{
1869 return !((flag_code == CODE_64BIT && f.bitfield.cpuno64)
1870 || (flag_code != CODE_64BIT && f.bitfield.cpu64));
40fb9820
L
1871}
1872
c6fb90c8
L
1873static INLINE i386_cpu_flags
1874cpu_flags_and (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1875{
c6fb90c8
L
1876 switch (ARRAY_SIZE (x.array))
1877 {
53467f57
IT
1878 case 4:
1879 x.array [3] &= y.array [3];
1880 /* Fall through. */
c6fb90c8
L
1881 case 3:
1882 x.array [2] &= y.array [2];
1a0670f3 1883 /* Fall through. */
c6fb90c8
L
1884 case 2:
1885 x.array [1] &= y.array [1];
1a0670f3 1886 /* Fall through. */
c6fb90c8
L
1887 case 1:
1888 x.array [0] &= y.array [0];
1889 break;
1890 default:
1891 abort ();
1892 }
1893 return x;
1894}
40fb9820 1895
c6fb90c8
L
1896static INLINE i386_cpu_flags
1897cpu_flags_or (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1898{
c6fb90c8 1899 switch (ARRAY_SIZE (x.array))
40fb9820 1900 {
53467f57
IT
1901 case 4:
1902 x.array [3] |= y.array [3];
1903 /* Fall through. */
c6fb90c8
L
1904 case 3:
1905 x.array [2] |= y.array [2];
1a0670f3 1906 /* Fall through. */
c6fb90c8
L
1907 case 2:
1908 x.array [1] |= y.array [1];
1a0670f3 1909 /* Fall through. */
c6fb90c8
L
1910 case 1:
1911 x.array [0] |= y.array [0];
40fb9820
L
1912 break;
1913 default:
1914 abort ();
1915 }
40fb9820
L
1916 return x;
1917}
1918
309d3373
JB
1919static INLINE i386_cpu_flags
1920cpu_flags_and_not (i386_cpu_flags x, i386_cpu_flags y)
1921{
1922 switch (ARRAY_SIZE (x.array))
1923 {
53467f57
IT
1924 case 4:
1925 x.array [3] &= ~y.array [3];
1926 /* Fall through. */
309d3373
JB
1927 case 3:
1928 x.array [2] &= ~y.array [2];
1a0670f3 1929 /* Fall through. */
309d3373
JB
1930 case 2:
1931 x.array [1] &= ~y.array [1];
1a0670f3 1932 /* Fall through. */
309d3373
JB
1933 case 1:
1934 x.array [0] &= ~y.array [0];
1935 break;
1936 default:
1937 abort ();
1938 }
1939 return x;
1940}
1941
6c0946d0
JB
1942static const i386_cpu_flags avx512 = CPU_ANY_AVX512F_FLAGS;
1943
c0f3af97
L
1944#define CPU_FLAGS_ARCH_MATCH 0x1
1945#define CPU_FLAGS_64BIT_MATCH 0x2
1946
c0f3af97 1947#define CPU_FLAGS_PERFECT_MATCH \
db12e14e 1948 (CPU_FLAGS_ARCH_MATCH | CPU_FLAGS_64BIT_MATCH)
c0f3af97
L
1949
1950/* Return CPU flags match bits. */
3629bb00 1951
40fb9820 1952static int
d3ce72d0 1953cpu_flags_match (const insn_template *t)
40fb9820 1954{
c0f3af97
L
1955 i386_cpu_flags x = t->cpu_flags;
1956 int match = cpu_flags_check_cpu64 (x) ? CPU_FLAGS_64BIT_MATCH : 0;
40fb9820
L
1957
1958 x.bitfield.cpu64 = 0;
1959 x.bitfield.cpuno64 = 0;
1960
0dfbf9d7 1961 if (cpu_flags_all_zero (&x))
c0f3af97
L
1962 {
1963 /* This instruction is available on all archs. */
db12e14e 1964 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 1965 }
3629bb00
L
1966 else
1967 {
c0f3af97 1968 /* This instruction is available only on some archs. */
3629bb00
L
1969 i386_cpu_flags cpu = cpu_arch_flags;
1970
ab592e75
JB
1971 /* AVX512VL is no standalone feature - match it and then strip it. */
1972 if (x.bitfield.cpuavx512vl && !cpu.bitfield.cpuavx512vl)
1973 return match;
1974 x.bitfield.cpuavx512vl = 0;
1975
3629bb00 1976 cpu = cpu_flags_and (x, cpu);
c0f3af97
L
1977 if (!cpu_flags_all_zero (&cpu))
1978 {
57392598 1979 if (x.bitfield.cpuavx)
a5ff0eb2 1980 {
929f69fa 1981 /* We need to check a few extra flags with AVX. */
b9d49817 1982 if (cpu.bitfield.cpuavx
40d231b4
JB
1983 && (!t->opcode_modifier.sse2avx
1984 || (sse2avx && !i.prefix[DATA_PREFIX]))
b9d49817 1985 && (!x.bitfield.cpuaes || cpu.bitfield.cpuaes)
929f69fa 1986 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
b9d49817
JB
1987 && (!x.bitfield.cpupclmul || cpu.bitfield.cpupclmul))
1988 match |= CPU_FLAGS_ARCH_MATCH;
a5ff0eb2 1989 }
929f69fa
JB
1990 else if (x.bitfield.cpuavx512f)
1991 {
1992 /* We need to check a few extra flags with AVX512F. */
1993 if (cpu.bitfield.cpuavx512f
1994 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
1995 && (!x.bitfield.cpuvaes || cpu.bitfield.cpuvaes)
1996 && (!x.bitfield.cpuvpclmulqdq || cpu.bitfield.cpuvpclmulqdq))
1997 match |= CPU_FLAGS_ARCH_MATCH;
1998 }
a5ff0eb2 1999 else
db12e14e 2000 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 2001 }
3629bb00 2002 }
c0f3af97 2003 return match;
40fb9820
L
2004}
2005
c6fb90c8
L
2006static INLINE i386_operand_type
2007operand_type_and (i386_operand_type x, i386_operand_type y)
40fb9820 2008{
bab6aec1
JB
2009 if (x.bitfield.class != y.bitfield.class)
2010 x.bitfield.class = ClassNone;
75e5731b
JB
2011 if (x.bitfield.instance != y.bitfield.instance)
2012 x.bitfield.instance = InstanceNone;
bab6aec1 2013
c6fb90c8
L
2014 switch (ARRAY_SIZE (x.array))
2015 {
2016 case 3:
2017 x.array [2] &= y.array [2];
1a0670f3 2018 /* Fall through. */
c6fb90c8
L
2019 case 2:
2020 x.array [1] &= y.array [1];
1a0670f3 2021 /* Fall through. */
c6fb90c8
L
2022 case 1:
2023 x.array [0] &= y.array [0];
2024 break;
2025 default:
2026 abort ();
2027 }
2028 return x;
40fb9820
L
2029}
2030
73053c1f
JB
2031static INLINE i386_operand_type
2032operand_type_and_not (i386_operand_type x, i386_operand_type y)
2033{
bab6aec1 2034 gas_assert (y.bitfield.class == ClassNone);
75e5731b 2035 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 2036
73053c1f
JB
2037 switch (ARRAY_SIZE (x.array))
2038 {
2039 case 3:
2040 x.array [2] &= ~y.array [2];
2041 /* Fall through. */
2042 case 2:
2043 x.array [1] &= ~y.array [1];
2044 /* Fall through. */
2045 case 1:
2046 x.array [0] &= ~y.array [0];
2047 break;
2048 default:
2049 abort ();
2050 }
2051 return x;
2052}
2053
c6fb90c8
L
2054static INLINE i386_operand_type
2055operand_type_or (i386_operand_type x, i386_operand_type y)
40fb9820 2056{
bab6aec1
JB
2057 gas_assert (x.bitfield.class == ClassNone ||
2058 y.bitfield.class == ClassNone ||
2059 x.bitfield.class == y.bitfield.class);
75e5731b
JB
2060 gas_assert (x.bitfield.instance == InstanceNone ||
2061 y.bitfield.instance == InstanceNone ||
2062 x.bitfield.instance == y.bitfield.instance);
bab6aec1 2063
c6fb90c8 2064 switch (ARRAY_SIZE (x.array))
40fb9820 2065 {
c6fb90c8
L
2066 case 3:
2067 x.array [2] |= y.array [2];
1a0670f3 2068 /* Fall through. */
c6fb90c8
L
2069 case 2:
2070 x.array [1] |= y.array [1];
1a0670f3 2071 /* Fall through. */
c6fb90c8
L
2072 case 1:
2073 x.array [0] |= y.array [0];
40fb9820
L
2074 break;
2075 default:
2076 abort ();
2077 }
c6fb90c8
L
2078 return x;
2079}
40fb9820 2080
c6fb90c8
L
2081static INLINE i386_operand_type
2082operand_type_xor (i386_operand_type x, i386_operand_type y)
2083{
bab6aec1 2084 gas_assert (y.bitfield.class == ClassNone);
75e5731b 2085 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 2086
c6fb90c8
L
2087 switch (ARRAY_SIZE (x.array))
2088 {
2089 case 3:
2090 x.array [2] ^= y.array [2];
1a0670f3 2091 /* Fall through. */
c6fb90c8
L
2092 case 2:
2093 x.array [1] ^= y.array [1];
1a0670f3 2094 /* Fall through. */
c6fb90c8
L
2095 case 1:
2096 x.array [0] ^= y.array [0];
2097 break;
2098 default:
2099 abort ();
2100 }
40fb9820
L
2101 return x;
2102}
2103
40fb9820
L
2104static const i386_operand_type disp16 = OPERAND_TYPE_DISP16;
2105static const i386_operand_type disp32 = OPERAND_TYPE_DISP32;
2106static const i386_operand_type disp32s = OPERAND_TYPE_DISP32S;
2107static const i386_operand_type disp16_32 = OPERAND_TYPE_DISP16_32;
bab6aec1
JB
2108static const i386_operand_type anydisp = OPERAND_TYPE_ANYDISP;
2109static const i386_operand_type anyimm = OPERAND_TYPE_ANYIMM;
40fb9820 2110static const i386_operand_type regxmm = OPERAND_TYPE_REGXMM;
43234a1e 2111static const i386_operand_type regmask = OPERAND_TYPE_REGMASK;
40fb9820
L
2112static const i386_operand_type imm8 = OPERAND_TYPE_IMM8;
2113static const i386_operand_type imm8s = OPERAND_TYPE_IMM8S;
2114static const i386_operand_type imm16 = OPERAND_TYPE_IMM16;
2115static const i386_operand_type imm32 = OPERAND_TYPE_IMM32;
2116static const i386_operand_type imm32s = OPERAND_TYPE_IMM32S;
2117static const i386_operand_type imm64 = OPERAND_TYPE_IMM64;
2118static const i386_operand_type imm16_32 = OPERAND_TYPE_IMM16_32;
2119static const i386_operand_type imm16_32s = OPERAND_TYPE_IMM16_32S;
2120static const i386_operand_type imm16_32_32s = OPERAND_TYPE_IMM16_32_32S;
2121
2122enum operand_type
2123{
2124 reg,
40fb9820
L
2125 imm,
2126 disp,
2127 anymem
2128};
2129
c6fb90c8 2130static INLINE int
40fb9820
L
2131operand_type_check (i386_operand_type t, enum operand_type c)
2132{
2133 switch (c)
2134 {
2135 case reg:
bab6aec1 2136 return t.bitfield.class == Reg;
40fb9820 2137
40fb9820
L
2138 case imm:
2139 return (t.bitfield.imm8
2140 || t.bitfield.imm8s
2141 || t.bitfield.imm16
2142 || t.bitfield.imm32
2143 || t.bitfield.imm32s
2144 || t.bitfield.imm64);
2145
2146 case disp:
2147 return (t.bitfield.disp8
2148 || t.bitfield.disp16
2149 || t.bitfield.disp32
2150 || t.bitfield.disp32s
2151 || t.bitfield.disp64);
2152
2153 case anymem:
2154 return (t.bitfield.disp8
2155 || t.bitfield.disp16
2156 || t.bitfield.disp32
2157 || t.bitfield.disp32s
2158 || t.bitfield.disp64
2159 || t.bitfield.baseindex);
2160
2161 default:
2162 abort ();
2163 }
2cfe26b6
AM
2164
2165 return 0;
40fb9820
L
2166}
2167
7a54636a
L
2168/* Return 1 if there is no conflict in 8bit/16bit/32bit/64bit/80bit size
2169 between operand GIVEN and opeand WANTED for instruction template T. */
5c07affc
L
2170
2171static INLINE int
7a54636a
L
2172match_operand_size (const insn_template *t, unsigned int wanted,
2173 unsigned int given)
5c07affc 2174{
3ac21baa
JB
2175 return !((i.types[given].bitfield.byte
2176 && !t->operand_types[wanted].bitfield.byte)
2177 || (i.types[given].bitfield.word
2178 && !t->operand_types[wanted].bitfield.word)
2179 || (i.types[given].bitfield.dword
2180 && !t->operand_types[wanted].bitfield.dword)
2181 || (i.types[given].bitfield.qword
2182 && !t->operand_types[wanted].bitfield.qword)
2183 || (i.types[given].bitfield.tbyte
2184 && !t->operand_types[wanted].bitfield.tbyte));
5c07affc
L
2185}
2186
dd40ce22
L
2187/* Return 1 if there is no conflict in SIMD register between operand
2188 GIVEN and opeand WANTED for instruction template T. */
1b54b8d7
JB
2189
2190static INLINE int
dd40ce22
L
2191match_simd_size (const insn_template *t, unsigned int wanted,
2192 unsigned int given)
1b54b8d7 2193{
3ac21baa
JB
2194 return !((i.types[given].bitfield.xmmword
2195 && !t->operand_types[wanted].bitfield.xmmword)
2196 || (i.types[given].bitfield.ymmword
2197 && !t->operand_types[wanted].bitfield.ymmword)
2198 || (i.types[given].bitfield.zmmword
260cd341
LC
2199 && !t->operand_types[wanted].bitfield.zmmword)
2200 || (i.types[given].bitfield.tmmword
2201 && !t->operand_types[wanted].bitfield.tmmword));
1b54b8d7
JB
2202}
2203
7a54636a
L
2204/* Return 1 if there is no conflict in any size between operand GIVEN
2205 and opeand WANTED for instruction template T. */
5c07affc
L
2206
2207static INLINE int
dd40ce22
L
2208match_mem_size (const insn_template *t, unsigned int wanted,
2209 unsigned int given)
5c07affc 2210{
7a54636a 2211 return (match_operand_size (t, wanted, given)
3ac21baa 2212 && !((i.types[given].bitfield.unspecified
5273a3cd 2213 && !i.broadcast.type
3ac21baa
JB
2214 && !t->operand_types[wanted].bitfield.unspecified)
2215 || (i.types[given].bitfield.fword
2216 && !t->operand_types[wanted].bitfield.fword)
1b54b8d7
JB
2217 /* For scalar opcode templates to allow register and memory
2218 operands at the same time, some special casing is needed
d6793fa1
JB
2219 here. Also for v{,p}broadcast*, {,v}pmov{s,z}*, and
2220 down-conversion vpmov*. */
3528c362 2221 || ((t->operand_types[wanted].bitfield.class == RegSIMD
bc49bfd8
JB
2222 && t->operand_types[wanted].bitfield.byte
2223 + t->operand_types[wanted].bitfield.word
2224 + t->operand_types[wanted].bitfield.dword
2225 + t->operand_types[wanted].bitfield.qword
2226 > !!t->opcode_modifier.broadcast)
3ac21baa
JB
2227 ? (i.types[given].bitfield.xmmword
2228 || i.types[given].bitfield.ymmword
2229 || i.types[given].bitfield.zmmword)
2230 : !match_simd_size(t, wanted, given))));
5c07affc
L
2231}
2232
3ac21baa
JB
2233/* Return value has MATCH_STRAIGHT set if there is no size conflict on any
2234 operands for instruction template T, and it has MATCH_REVERSE set if there
2235 is no size conflict on any operands for the template with operands reversed
2236 (and the template allows for reversing in the first place). */
5c07affc 2237
3ac21baa
JB
2238#define MATCH_STRAIGHT 1
2239#define MATCH_REVERSE 2
2240
2241static INLINE unsigned int
d3ce72d0 2242operand_size_match (const insn_template *t)
5c07affc 2243{
3ac21baa 2244 unsigned int j, match = MATCH_STRAIGHT;
5c07affc 2245
0cfa3eb3 2246 /* Don't check non-absolute jump instructions. */
5c07affc 2247 if (t->opcode_modifier.jump
0cfa3eb3 2248 && t->opcode_modifier.jump != JUMP_ABSOLUTE)
5c07affc
L
2249 return match;
2250
2251 /* Check memory and accumulator operand size. */
2252 for (j = 0; j < i.operands; j++)
2253 {
3528c362
JB
2254 if (i.types[j].bitfield.class != Reg
2255 && i.types[j].bitfield.class != RegSIMD
601e8564 2256 && t->opcode_modifier.anysize)
5c07affc
L
2257 continue;
2258
bab6aec1 2259 if (t->operand_types[j].bitfield.class == Reg
7a54636a 2260 && !match_operand_size (t, j, j))
5c07affc
L
2261 {
2262 match = 0;
2263 break;
2264 }
2265
3528c362 2266 if (t->operand_types[j].bitfield.class == RegSIMD
3ac21baa 2267 && !match_simd_size (t, j, j))
1b54b8d7
JB
2268 {
2269 match = 0;
2270 break;
2271 }
2272
75e5731b 2273 if (t->operand_types[j].bitfield.instance == Accum
7a54636a 2274 && (!match_operand_size (t, j, j) || !match_simd_size (t, j, j)))
1b54b8d7
JB
2275 {
2276 match = 0;
2277 break;
2278 }
2279
c48dadc9 2280 if ((i.flags[j] & Operand_Mem) && !match_mem_size (t, j, j))
5c07affc
L
2281 {
2282 match = 0;
2283 break;
2284 }
2285 }
2286
3ac21baa 2287 if (!t->opcode_modifier.d)
891edac4 2288 {
dc1e8a47 2289 mismatch:
3ac21baa
JB
2290 if (!match)
2291 i.error = operand_size_mismatch;
2292 return match;
891edac4 2293 }
5c07affc
L
2294
2295 /* Check reverse. */
f5eb1d70 2296 gas_assert (i.operands >= 2 && i.operands <= 3);
5c07affc 2297
f5eb1d70 2298 for (j = 0; j < i.operands; j++)
5c07affc 2299 {
f5eb1d70
JB
2300 unsigned int given = i.operands - j - 1;
2301
bab6aec1 2302 if (t->operand_types[j].bitfield.class == Reg
f5eb1d70 2303 && !match_operand_size (t, j, given))
891edac4 2304 goto mismatch;
5c07affc 2305
3528c362 2306 if (t->operand_types[j].bitfield.class == RegSIMD
f5eb1d70 2307 && !match_simd_size (t, j, given))
dbbc8b7e
JB
2308 goto mismatch;
2309
75e5731b 2310 if (t->operand_types[j].bitfield.instance == Accum
f5eb1d70
JB
2311 && (!match_operand_size (t, j, given)
2312 || !match_simd_size (t, j, given)))
dbbc8b7e
JB
2313 goto mismatch;
2314
f5eb1d70 2315 if ((i.flags[given] & Operand_Mem) && !match_mem_size (t, j, given))
891edac4 2316 goto mismatch;
5c07affc
L
2317 }
2318
3ac21baa 2319 return match | MATCH_REVERSE;
5c07affc
L
2320}
2321
c6fb90c8 2322static INLINE int
40fb9820
L
2323operand_type_match (i386_operand_type overlap,
2324 i386_operand_type given)
2325{
2326 i386_operand_type temp = overlap;
2327
7d5e4556 2328 temp.bitfield.unspecified = 0;
5c07affc
L
2329 temp.bitfield.byte = 0;
2330 temp.bitfield.word = 0;
2331 temp.bitfield.dword = 0;
2332 temp.bitfield.fword = 0;
2333 temp.bitfield.qword = 0;
2334 temp.bitfield.tbyte = 0;
2335 temp.bitfield.xmmword = 0;
c0f3af97 2336 temp.bitfield.ymmword = 0;
43234a1e 2337 temp.bitfield.zmmword = 0;
260cd341 2338 temp.bitfield.tmmword = 0;
0dfbf9d7 2339 if (operand_type_all_zero (&temp))
891edac4 2340 goto mismatch;
40fb9820 2341
6f2f06be 2342 if (given.bitfield.baseindex == overlap.bitfield.baseindex)
891edac4
L
2343 return 1;
2344
dc1e8a47 2345 mismatch:
a65babc9 2346 i.error = operand_type_mismatch;
891edac4 2347 return 0;
40fb9820
L
2348}
2349
7d5e4556 2350/* If given types g0 and g1 are registers they must be of the same type
10c17abd 2351 unless the expected operand type register overlap is null.
5de4d9ef 2352 Some Intel syntax memory operand size checking also happens here. */
40fb9820 2353
c6fb90c8 2354static INLINE int
dc821c5f 2355operand_type_register_match (i386_operand_type g0,
40fb9820 2356 i386_operand_type t0,
40fb9820
L
2357 i386_operand_type g1,
2358 i386_operand_type t1)
2359{
bab6aec1 2360 if (g0.bitfield.class != Reg
3528c362 2361 && g0.bitfield.class != RegSIMD
10c17abd
JB
2362 && (!operand_type_check (g0, anymem)
2363 || g0.bitfield.unspecified
5de4d9ef
JB
2364 || (t0.bitfield.class != Reg
2365 && t0.bitfield.class != RegSIMD)))
40fb9820
L
2366 return 1;
2367
bab6aec1 2368 if (g1.bitfield.class != Reg
3528c362 2369 && g1.bitfield.class != RegSIMD
10c17abd
JB
2370 && (!operand_type_check (g1, anymem)
2371 || g1.bitfield.unspecified
5de4d9ef
JB
2372 || (t1.bitfield.class != Reg
2373 && t1.bitfield.class != RegSIMD)))
40fb9820
L
2374 return 1;
2375
dc821c5f
JB
2376 if (g0.bitfield.byte == g1.bitfield.byte
2377 && g0.bitfield.word == g1.bitfield.word
2378 && g0.bitfield.dword == g1.bitfield.dword
10c17abd
JB
2379 && g0.bitfield.qword == g1.bitfield.qword
2380 && g0.bitfield.xmmword == g1.bitfield.xmmword
2381 && g0.bitfield.ymmword == g1.bitfield.ymmword
2382 && g0.bitfield.zmmword == g1.bitfield.zmmword)
40fb9820
L
2383 return 1;
2384
dc821c5f
JB
2385 if (!(t0.bitfield.byte & t1.bitfield.byte)
2386 && !(t0.bitfield.word & t1.bitfield.word)
2387 && !(t0.bitfield.dword & t1.bitfield.dword)
10c17abd
JB
2388 && !(t0.bitfield.qword & t1.bitfield.qword)
2389 && !(t0.bitfield.xmmword & t1.bitfield.xmmword)
2390 && !(t0.bitfield.ymmword & t1.bitfield.ymmword)
2391 && !(t0.bitfield.zmmword & t1.bitfield.zmmword))
891edac4
L
2392 return 1;
2393
a65babc9 2394 i.error = register_type_mismatch;
891edac4
L
2395
2396 return 0;
40fb9820
L
2397}
2398
4c692bc7
JB
2399static INLINE unsigned int
2400register_number (const reg_entry *r)
2401{
2402 unsigned int nr = r->reg_num;
2403
2404 if (r->reg_flags & RegRex)
2405 nr += 8;
2406
200cbe0f
L
2407 if (r->reg_flags & RegVRex)
2408 nr += 16;
2409
4c692bc7
JB
2410 return nr;
2411}
2412
252b5132 2413static INLINE unsigned int
40fb9820 2414mode_from_disp_size (i386_operand_type t)
252b5132 2415{
b5014f7a 2416 if (t.bitfield.disp8)
40fb9820
L
2417 return 1;
2418 else if (t.bitfield.disp16
2419 || t.bitfield.disp32
2420 || t.bitfield.disp32s)
2421 return 2;
2422 else
2423 return 0;
252b5132
RH
2424}
2425
2426static INLINE int
65879393 2427fits_in_signed_byte (addressT num)
252b5132 2428{
65879393 2429 return num + 0x80 <= 0xff;
47926f60 2430}
252b5132
RH
2431
2432static INLINE int
65879393 2433fits_in_unsigned_byte (addressT num)
252b5132 2434{
65879393 2435 return num <= 0xff;
47926f60 2436}
252b5132
RH
2437
2438static INLINE int
65879393 2439fits_in_unsigned_word (addressT num)
252b5132 2440{
65879393 2441 return num <= 0xffff;
47926f60 2442}
252b5132
RH
2443
2444static INLINE int
65879393 2445fits_in_signed_word (addressT num)
252b5132 2446{
65879393 2447 return num + 0x8000 <= 0xffff;
47926f60 2448}
2a962e6d 2449
3e73aa7c 2450static INLINE int
65879393 2451fits_in_signed_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2452{
2453#ifndef BFD64
2454 return 1;
2455#else
65879393 2456 return num + 0x80000000 <= 0xffffffff;
3e73aa7c
JH
2457#endif
2458} /* fits_in_signed_long() */
2a962e6d 2459
3e73aa7c 2460static INLINE int
65879393 2461fits_in_unsigned_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2462{
2463#ifndef BFD64
2464 return 1;
2465#else
65879393 2466 return num <= 0xffffffff;
3e73aa7c
JH
2467#endif
2468} /* fits_in_unsigned_long() */
252b5132 2469
43234a1e 2470static INLINE int
b5014f7a 2471fits_in_disp8 (offsetT num)
43234a1e
L
2472{
2473 int shift = i.memshift;
2474 unsigned int mask;
2475
2476 if (shift == -1)
2477 abort ();
2478
2479 mask = (1 << shift) - 1;
2480
2481 /* Return 0 if NUM isn't properly aligned. */
2482 if ((num & mask))
2483 return 0;
2484
2485 /* Check if NUM will fit in 8bit after shift. */
2486 return fits_in_signed_byte (num >> shift);
2487}
2488
a683cc34
SP
2489static INLINE int
2490fits_in_imm4 (offsetT num)
2491{
2492 return (num & 0xf) == num;
2493}
2494
40fb9820 2495static i386_operand_type
e3bb37b5 2496smallest_imm_type (offsetT num)
252b5132 2497{
40fb9820 2498 i386_operand_type t;
7ab9ffdd 2499
0dfbf9d7 2500 operand_type_set (&t, 0);
40fb9820
L
2501 t.bitfield.imm64 = 1;
2502
2503 if (cpu_arch_tune != PROCESSOR_I486 && num == 1)
e413e4e9
AM
2504 {
2505 /* This code is disabled on the 486 because all the Imm1 forms
2506 in the opcode table are slower on the i486. They're the
2507 versions with the implicitly specified single-position
2508 displacement, which has another syntax if you really want to
2509 use that form. */
40fb9820
L
2510 t.bitfield.imm1 = 1;
2511 t.bitfield.imm8 = 1;
2512 t.bitfield.imm8s = 1;
2513 t.bitfield.imm16 = 1;
2514 t.bitfield.imm32 = 1;
2515 t.bitfield.imm32s = 1;
2516 }
2517 else if (fits_in_signed_byte (num))
2518 {
2519 t.bitfield.imm8 = 1;
2520 t.bitfield.imm8s = 1;
2521 t.bitfield.imm16 = 1;
2522 t.bitfield.imm32 = 1;
2523 t.bitfield.imm32s = 1;
2524 }
2525 else if (fits_in_unsigned_byte (num))
2526 {
2527 t.bitfield.imm8 = 1;
2528 t.bitfield.imm16 = 1;
2529 t.bitfield.imm32 = 1;
2530 t.bitfield.imm32s = 1;
2531 }
2532 else if (fits_in_signed_word (num) || fits_in_unsigned_word (num))
2533 {
2534 t.bitfield.imm16 = 1;
2535 t.bitfield.imm32 = 1;
2536 t.bitfield.imm32s = 1;
2537 }
2538 else if (fits_in_signed_long (num))
2539 {
2540 t.bitfield.imm32 = 1;
2541 t.bitfield.imm32s = 1;
2542 }
2543 else if (fits_in_unsigned_long (num))
2544 t.bitfield.imm32 = 1;
2545
2546 return t;
47926f60 2547}
252b5132 2548
847f7ad4 2549static offsetT
e3bb37b5 2550offset_in_range (offsetT val, int size)
847f7ad4 2551{
508866be 2552 addressT mask;
ba2adb93 2553
847f7ad4
AM
2554 switch (size)
2555 {
508866be
L
2556 case 1: mask = ((addressT) 1 << 8) - 1; break;
2557 case 2: mask = ((addressT) 1 << 16) - 1; break;
3b0ec529 2558 case 4: mask = ((addressT) 2 << 31) - 1; break;
3e73aa7c
JH
2559#ifdef BFD64
2560 case 8: mask = ((addressT) 2 << 63) - 1; break;
2561#endif
47926f60 2562 default: abort ();
847f7ad4
AM
2563 }
2564
47926f60 2565 if ((val & ~mask) != 0 && (val & ~mask) != ~mask)
847f7ad4
AM
2566 {
2567 char buf1[40], buf2[40];
2568
2569 sprint_value (buf1, val);
2570 sprint_value (buf2, val & mask);
2571 as_warn (_("%s shortened to %s"), buf1, buf2);
2572 }
2573 return val & mask;
2574}
2575
c32fa91d
L
2576enum PREFIX_GROUP
2577{
2578 PREFIX_EXIST = 0,
2579 PREFIX_LOCK,
2580 PREFIX_REP,
04ef582a 2581 PREFIX_DS,
c32fa91d
L
2582 PREFIX_OTHER
2583};
2584
2585/* Returns
2586 a. PREFIX_EXIST if attempting to add a prefix where one from the
2587 same class already exists.
2588 b. PREFIX_LOCK if lock prefix is added.
2589 c. PREFIX_REP if rep/repne prefix is added.
04ef582a
L
2590 d. PREFIX_DS if ds prefix is added.
2591 e. PREFIX_OTHER if other prefix is added.
c32fa91d
L
2592 */
2593
2594static enum PREFIX_GROUP
e3bb37b5 2595add_prefix (unsigned int prefix)
252b5132 2596{
c32fa91d 2597 enum PREFIX_GROUP ret = PREFIX_OTHER;
b1905489 2598 unsigned int q;
252b5132 2599
29b0f896
AM
2600 if (prefix >= REX_OPCODE && prefix < REX_OPCODE + 16
2601 && flag_code == CODE_64BIT)
b1905489 2602 {
161a04f6 2603 if ((i.prefix[REX_PREFIX] & prefix & REX_W)
44846f29
JB
2604 || (i.prefix[REX_PREFIX] & prefix & REX_R)
2605 || (i.prefix[REX_PREFIX] & prefix & REX_X)
2606 || (i.prefix[REX_PREFIX] & prefix & REX_B))
c32fa91d 2607 ret = PREFIX_EXIST;
b1905489
JB
2608 q = REX_PREFIX;
2609 }
3e73aa7c 2610 else
b1905489
JB
2611 {
2612 switch (prefix)
2613 {
2614 default:
2615 abort ();
2616
b1905489 2617 case DS_PREFIX_OPCODE:
04ef582a
L
2618 ret = PREFIX_DS;
2619 /* Fall through. */
2620 case CS_PREFIX_OPCODE:
b1905489
JB
2621 case ES_PREFIX_OPCODE:
2622 case FS_PREFIX_OPCODE:
2623 case GS_PREFIX_OPCODE:
2624 case SS_PREFIX_OPCODE:
2625 q = SEG_PREFIX;
2626 break;
2627
2628 case REPNE_PREFIX_OPCODE:
2629 case REPE_PREFIX_OPCODE:
c32fa91d
L
2630 q = REP_PREFIX;
2631 ret = PREFIX_REP;
2632 break;
2633
b1905489 2634 case LOCK_PREFIX_OPCODE:
c32fa91d
L
2635 q = LOCK_PREFIX;
2636 ret = PREFIX_LOCK;
b1905489
JB
2637 break;
2638
2639 case FWAIT_OPCODE:
2640 q = WAIT_PREFIX;
2641 break;
2642
2643 case ADDR_PREFIX_OPCODE:
2644 q = ADDR_PREFIX;
2645 break;
2646
2647 case DATA_PREFIX_OPCODE:
2648 q = DATA_PREFIX;
2649 break;
2650 }
2651 if (i.prefix[q] != 0)
c32fa91d 2652 ret = PREFIX_EXIST;
b1905489 2653 }
252b5132 2654
b1905489 2655 if (ret)
252b5132 2656 {
b1905489
JB
2657 if (!i.prefix[q])
2658 ++i.prefixes;
2659 i.prefix[q] |= prefix;
252b5132 2660 }
b1905489
JB
2661 else
2662 as_bad (_("same type of prefix used twice"));
252b5132 2663
252b5132
RH
2664 return ret;
2665}
2666
2667static void
78f12dd3 2668update_code_flag (int value, int check)
eecb386c 2669{
78f12dd3
L
2670 PRINTF_LIKE ((*as_error));
2671
1e9cc1c2 2672 flag_code = (enum flag_code) value;
40fb9820
L
2673 if (flag_code == CODE_64BIT)
2674 {
2675 cpu_arch_flags.bitfield.cpu64 = 1;
2676 cpu_arch_flags.bitfield.cpuno64 = 0;
40fb9820
L
2677 }
2678 else
2679 {
2680 cpu_arch_flags.bitfield.cpu64 = 0;
2681 cpu_arch_flags.bitfield.cpuno64 = 1;
40fb9820
L
2682 }
2683 if (value == CODE_64BIT && !cpu_arch_flags.bitfield.cpulm )
3e73aa7c 2684 {
78f12dd3
L
2685 if (check)
2686 as_error = as_fatal;
2687 else
2688 as_error = as_bad;
2689 (*as_error) (_("64bit mode not supported on `%s'."),
2690 cpu_arch_name ? cpu_arch_name : default_arch);
3e73aa7c 2691 }
40fb9820 2692 if (value == CODE_32BIT && !cpu_arch_flags.bitfield.cpui386)
3e73aa7c 2693 {
78f12dd3
L
2694 if (check)
2695 as_error = as_fatal;
2696 else
2697 as_error = as_bad;
2698 (*as_error) (_("32bit mode not supported on `%s'."),
2699 cpu_arch_name ? cpu_arch_name : default_arch);
3e73aa7c 2700 }
eecb386c
AM
2701 stackop_size = '\0';
2702}
2703
78f12dd3
L
2704static void
2705set_code_flag (int value)
2706{
2707 update_code_flag (value, 0);
2708}
2709
eecb386c 2710static void
e3bb37b5 2711set_16bit_gcc_code_flag (int new_code_flag)
252b5132 2712{
1e9cc1c2 2713 flag_code = (enum flag_code) new_code_flag;
40fb9820
L
2714 if (flag_code != CODE_16BIT)
2715 abort ();
2716 cpu_arch_flags.bitfield.cpu64 = 0;
2717 cpu_arch_flags.bitfield.cpuno64 = 1;
9306ca4a 2718 stackop_size = LONG_MNEM_SUFFIX;
252b5132
RH
2719}
2720
2721static void
e3bb37b5 2722set_intel_syntax (int syntax_flag)
252b5132
RH
2723{
2724 /* Find out if register prefixing is specified. */
2725 int ask_naked_reg = 0;
2726
2727 SKIP_WHITESPACE ();
29b0f896 2728 if (!is_end_of_line[(unsigned char) *input_line_pointer])
252b5132 2729 {
d02603dc
NC
2730 char *string;
2731 int e = get_symbol_name (&string);
252b5132 2732
47926f60 2733 if (strcmp (string, "prefix") == 0)
252b5132 2734 ask_naked_reg = 1;
47926f60 2735 else if (strcmp (string, "noprefix") == 0)
252b5132
RH
2736 ask_naked_reg = -1;
2737 else
d0b47220 2738 as_bad (_("bad argument to syntax directive."));
d02603dc 2739 (void) restore_line_pointer (e);
252b5132
RH
2740 }
2741 demand_empty_rest_of_line ();
c3332e24 2742
252b5132
RH
2743 intel_syntax = syntax_flag;
2744
2745 if (ask_naked_reg == 0)
f86103b7
AM
2746 allow_naked_reg = (intel_syntax
2747 && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
252b5132
RH
2748 else
2749 allow_naked_reg = (ask_naked_reg < 0);
9306ca4a 2750
ee86248c 2751 expr_set_rank (O_full_ptr, syntax_flag ? 10 : 0);
7ab9ffdd 2752
e4a3b5a4 2753 identifier_chars['%'] = intel_syntax && allow_naked_reg ? '%' : 0;
9306ca4a 2754 identifier_chars['$'] = intel_syntax ? '$' : 0;
e4a3b5a4 2755 register_prefix = allow_naked_reg ? "" : "%";
252b5132
RH
2756}
2757
1efbbeb4
L
2758static void
2759set_intel_mnemonic (int mnemonic_flag)
2760{
e1d4d893 2761 intel_mnemonic = mnemonic_flag;
1efbbeb4
L
2762}
2763
db51cc60
L
2764static void
2765set_allow_index_reg (int flag)
2766{
2767 allow_index_reg = flag;
2768}
2769
cb19c032 2770static void
7bab8ab5 2771set_check (int what)
cb19c032 2772{
7bab8ab5
JB
2773 enum check_kind *kind;
2774 const char *str;
2775
2776 if (what)
2777 {
2778 kind = &operand_check;
2779 str = "operand";
2780 }
2781 else
2782 {
2783 kind = &sse_check;
2784 str = "sse";
2785 }
2786
cb19c032
L
2787 SKIP_WHITESPACE ();
2788
2789 if (!is_end_of_line[(unsigned char) *input_line_pointer])
2790 {
d02603dc
NC
2791 char *string;
2792 int e = get_symbol_name (&string);
cb19c032
L
2793
2794 if (strcmp (string, "none") == 0)
7bab8ab5 2795 *kind = check_none;
cb19c032 2796 else if (strcmp (string, "warning") == 0)
7bab8ab5 2797 *kind = check_warning;
cb19c032 2798 else if (strcmp (string, "error") == 0)
7bab8ab5 2799 *kind = check_error;
cb19c032 2800 else
7bab8ab5 2801 as_bad (_("bad argument to %s_check directive."), str);
d02603dc 2802 (void) restore_line_pointer (e);
cb19c032
L
2803 }
2804 else
7bab8ab5 2805 as_bad (_("missing argument for %s_check directive"), str);
cb19c032
L
2806
2807 demand_empty_rest_of_line ();
2808}
2809
8a9036a4
L
2810static void
2811check_cpu_arch_compatible (const char *name ATTRIBUTE_UNUSED,
1e9cc1c2 2812 i386_cpu_flags new_flag ATTRIBUTE_UNUSED)
8a9036a4
L
2813{
2814#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2815 static const char *arch;
2816
2817 /* Intel LIOM is only supported on ELF. */
2818 if (!IS_ELF)
2819 return;
2820
2821 if (!arch)
2822 {
2823 /* Use cpu_arch_name if it is set in md_parse_option. Otherwise
2824 use default_arch. */
2825 arch = cpu_arch_name;
2826 if (!arch)
2827 arch = default_arch;
2828 }
2829
81486035
L
2830 /* If we are targeting Intel MCU, we must enable it. */
2831 if (get_elf_backend_data (stdoutput)->elf_machine_code != EM_IAMCU
2832 || new_flag.bitfield.cpuiamcu)
2833 return;
2834
3632d14b 2835 /* If we are targeting Intel L1OM, we must enable it. */
8a9036a4 2836 if (get_elf_backend_data (stdoutput)->elf_machine_code != EM_L1OM
1e9cc1c2 2837 || new_flag.bitfield.cpul1om)
8a9036a4 2838 return;
76ba9986 2839
7a9068fe
L
2840 /* If we are targeting Intel K1OM, we must enable it. */
2841 if (get_elf_backend_data (stdoutput)->elf_machine_code != EM_K1OM
2842 || new_flag.bitfield.cpuk1om)
2843 return;
2844
8a9036a4
L
2845 as_bad (_("`%s' is not supported on `%s'"), name, arch);
2846#endif
2847}
2848
e413e4e9 2849static void
e3bb37b5 2850set_cpu_arch (int dummy ATTRIBUTE_UNUSED)
e413e4e9 2851{
47926f60 2852 SKIP_WHITESPACE ();
e413e4e9 2853
29b0f896 2854 if (!is_end_of_line[(unsigned char) *input_line_pointer])
e413e4e9 2855 {
d02603dc
NC
2856 char *string;
2857 int e = get_symbol_name (&string);
91d6fa6a 2858 unsigned int j;
40fb9820 2859 i386_cpu_flags flags;
e413e4e9 2860
91d6fa6a 2861 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
e413e4e9 2862 {
91d6fa6a 2863 if (strcmp (string, cpu_arch[j].name) == 0)
e413e4e9 2864 {
91d6fa6a 2865 check_cpu_arch_compatible (string, cpu_arch[j].flags);
8a9036a4 2866
5c6af06e
JB
2867 if (*string != '.')
2868 {
91d6fa6a 2869 cpu_arch_name = cpu_arch[j].name;
5c6af06e 2870 cpu_sub_arch_name = NULL;
91d6fa6a 2871 cpu_arch_flags = cpu_arch[j].flags;
40fb9820
L
2872 if (flag_code == CODE_64BIT)
2873 {
2874 cpu_arch_flags.bitfield.cpu64 = 1;
2875 cpu_arch_flags.bitfield.cpuno64 = 0;
2876 }
2877 else
2878 {
2879 cpu_arch_flags.bitfield.cpu64 = 0;
2880 cpu_arch_flags.bitfield.cpuno64 = 1;
2881 }
91d6fa6a
NC
2882 cpu_arch_isa = cpu_arch[j].type;
2883 cpu_arch_isa_flags = cpu_arch[j].flags;
ccc9c027
L
2884 if (!cpu_arch_tune_set)
2885 {
2886 cpu_arch_tune = cpu_arch_isa;
2887 cpu_arch_tune_flags = cpu_arch_isa_flags;
2888 }
5c6af06e
JB
2889 break;
2890 }
40fb9820 2891
293f5f65
L
2892 flags = cpu_flags_or (cpu_arch_flags,
2893 cpu_arch[j].flags);
81486035 2894
5b64d091 2895 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
5c6af06e 2896 {
6305a203
L
2897 if (cpu_sub_arch_name)
2898 {
2899 char *name = cpu_sub_arch_name;
2900 cpu_sub_arch_name = concat (name,
91d6fa6a 2901 cpu_arch[j].name,
1bf57e9f 2902 (const char *) NULL);
6305a203
L
2903 free (name);
2904 }
2905 else
91d6fa6a 2906 cpu_sub_arch_name = xstrdup (cpu_arch[j].name);
40fb9820 2907 cpu_arch_flags = flags;
a586129e 2908 cpu_arch_isa_flags = flags;
5c6af06e 2909 }
0089dace
L
2910 else
2911 cpu_arch_isa_flags
2912 = cpu_flags_or (cpu_arch_isa_flags,
2913 cpu_arch[j].flags);
d02603dc 2914 (void) restore_line_pointer (e);
5c6af06e
JB
2915 demand_empty_rest_of_line ();
2916 return;
e413e4e9
AM
2917 }
2918 }
293f5f65
L
2919
2920 if (*string == '.' && j >= ARRAY_SIZE (cpu_arch))
2921 {
33eaf5de 2922 /* Disable an ISA extension. */
293f5f65
L
2923 for (j = 0; j < ARRAY_SIZE (cpu_noarch); j++)
2924 if (strcmp (string + 1, cpu_noarch [j].name) == 0)
2925 {
2926 flags = cpu_flags_and_not (cpu_arch_flags,
2927 cpu_noarch[j].flags);
2928 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
2929 {
2930 if (cpu_sub_arch_name)
2931 {
2932 char *name = cpu_sub_arch_name;
2933 cpu_sub_arch_name = concat (name, string,
2934 (const char *) NULL);
2935 free (name);
2936 }
2937 else
2938 cpu_sub_arch_name = xstrdup (string);
2939 cpu_arch_flags = flags;
2940 cpu_arch_isa_flags = flags;
2941 }
2942 (void) restore_line_pointer (e);
2943 demand_empty_rest_of_line ();
2944 return;
2945 }
2946
2947 j = ARRAY_SIZE (cpu_arch);
2948 }
2949
91d6fa6a 2950 if (j >= ARRAY_SIZE (cpu_arch))
e413e4e9
AM
2951 as_bad (_("no such architecture: `%s'"), string);
2952
2953 *input_line_pointer = e;
2954 }
2955 else
2956 as_bad (_("missing cpu architecture"));
2957
fddf5b5b
AM
2958 no_cond_jump_promotion = 0;
2959 if (*input_line_pointer == ','
29b0f896 2960 && !is_end_of_line[(unsigned char) input_line_pointer[1]])
fddf5b5b 2961 {
d02603dc
NC
2962 char *string;
2963 char e;
2964
2965 ++input_line_pointer;
2966 e = get_symbol_name (&string);
fddf5b5b
AM
2967
2968 if (strcmp (string, "nojumps") == 0)
2969 no_cond_jump_promotion = 1;
2970 else if (strcmp (string, "jumps") == 0)
2971 ;
2972 else
2973 as_bad (_("no such architecture modifier: `%s'"), string);
2974
d02603dc 2975 (void) restore_line_pointer (e);
fddf5b5b
AM
2976 }
2977
e413e4e9
AM
2978 demand_empty_rest_of_line ();
2979}
2980
8a9036a4
L
2981enum bfd_architecture
2982i386_arch (void)
2983{
3632d14b 2984 if (cpu_arch_isa == PROCESSOR_L1OM)
8a9036a4
L
2985 {
2986 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2987 || flag_code != CODE_64BIT)
2988 as_fatal (_("Intel L1OM is 64bit ELF only"));
2989 return bfd_arch_l1om;
2990 }
7a9068fe
L
2991 else if (cpu_arch_isa == PROCESSOR_K1OM)
2992 {
2993 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2994 || flag_code != CODE_64BIT)
2995 as_fatal (_("Intel K1OM is 64bit ELF only"));
2996 return bfd_arch_k1om;
2997 }
81486035
L
2998 else if (cpu_arch_isa == PROCESSOR_IAMCU)
2999 {
3000 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
3001 || flag_code == CODE_64BIT)
3002 as_fatal (_("Intel MCU is 32bit ELF only"));
3003 return bfd_arch_iamcu;
3004 }
8a9036a4
L
3005 else
3006 return bfd_arch_i386;
3007}
3008
b9d79e03 3009unsigned long
7016a5d5 3010i386_mach (void)
b9d79e03 3011{
351f65ca 3012 if (!strncmp (default_arch, "x86_64", 6))
8a9036a4 3013 {
3632d14b 3014 if (cpu_arch_isa == PROCESSOR_L1OM)
8a9036a4 3015 {
351f65ca
L
3016 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
3017 || default_arch[6] != '\0')
8a9036a4
L
3018 as_fatal (_("Intel L1OM is 64bit ELF only"));
3019 return bfd_mach_l1om;
3020 }
7a9068fe
L
3021 else if (cpu_arch_isa == PROCESSOR_K1OM)
3022 {
3023 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
3024 || default_arch[6] != '\0')
3025 as_fatal (_("Intel K1OM is 64bit ELF only"));
3026 return bfd_mach_k1om;
3027 }
351f65ca 3028 else if (default_arch[6] == '\0')
8a9036a4 3029 return bfd_mach_x86_64;
351f65ca
L
3030 else
3031 return bfd_mach_x64_32;
8a9036a4 3032 }
5197d474
L
3033 else if (!strcmp (default_arch, "i386")
3034 || !strcmp (default_arch, "iamcu"))
81486035
L
3035 {
3036 if (cpu_arch_isa == PROCESSOR_IAMCU)
3037 {
3038 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
3039 as_fatal (_("Intel MCU is 32bit ELF only"));
3040 return bfd_mach_i386_iamcu;
3041 }
3042 else
3043 return bfd_mach_i386_i386;
3044 }
b9d79e03 3045 else
2b5d6a91 3046 as_fatal (_("unknown architecture"));
b9d79e03 3047}
b9d79e03 3048\f
252b5132 3049void
7016a5d5 3050md_begin (void)
252b5132 3051{
86fa6981
L
3052 /* Support pseudo prefixes like {disp32}. */
3053 lex_type ['{'] = LEX_BEGIN_NAME;
3054
47926f60 3055 /* Initialize op_hash hash table. */
629310ab 3056 op_hash = str_htab_create ();
252b5132
RH
3057
3058 {
d3ce72d0 3059 const insn_template *optab;
29b0f896 3060 templates *core_optab;
252b5132 3061
47926f60
KH
3062 /* Setup for loop. */
3063 optab = i386_optab;
add39d23 3064 core_optab = XNEW (templates);
252b5132
RH
3065 core_optab->start = optab;
3066
3067 while (1)
3068 {
3069 ++optab;
3070 if (optab->name == NULL
3071 || strcmp (optab->name, (optab - 1)->name) != 0)
3072 {
3073 /* different name --> ship out current template list;
47926f60 3074 add to hash table; & begin anew. */
252b5132 3075 core_optab->end = optab;
fe0e921f
AM
3076 if (str_hash_insert (op_hash, (optab - 1)->name, core_optab, 0))
3077 as_fatal (_("duplicate %s"), (optab - 1)->name);
3078
252b5132
RH
3079 if (optab->name == NULL)
3080 break;
add39d23 3081 core_optab = XNEW (templates);
252b5132
RH
3082 core_optab->start = optab;
3083 }
3084 }
3085 }
3086
47926f60 3087 /* Initialize reg_hash hash table. */
629310ab 3088 reg_hash = str_htab_create ();
252b5132 3089 {
29b0f896 3090 const reg_entry *regtab;
c3fe08fa 3091 unsigned int regtab_size = i386_regtab_size;
252b5132 3092
c3fe08fa 3093 for (regtab = i386_regtab; regtab_size--; regtab++)
6225c532 3094 {
6288d05f
JB
3095 switch (regtab->reg_type.bitfield.class)
3096 {
3097 case Reg:
34684862
JB
3098 if (regtab->reg_type.bitfield.dword)
3099 {
3100 if (regtab->reg_type.bitfield.instance == Accum)
3101 reg_eax = regtab;
3102 }
3103 else if (regtab->reg_type.bitfield.tbyte)
6288d05f
JB
3104 {
3105 /* There's no point inserting st(<N>) in the hash table, as
3106 parentheses aren't included in register_chars[] anyway. */
3107 if (regtab->reg_type.bitfield.instance != Accum)
3108 continue;
3109 reg_st0 = regtab;
3110 }
3111 break;
3112
5e042380
JB
3113 case SReg:
3114 switch (regtab->reg_num)
3115 {
3116 case 0: reg_es = regtab; break;
3117 case 2: reg_ss = regtab; break;
3118 case 3: reg_ds = regtab; break;
3119 }
3120 break;
3121
6288d05f
JB
3122 case RegMask:
3123 if (!regtab->reg_num)
3124 reg_k0 = regtab;
3125 break;
3126 }
3127
6225c532
JB
3128 if (str_hash_insert (reg_hash, regtab->reg_name, regtab, 0) != NULL)
3129 as_fatal (_("duplicate %s"), regtab->reg_name);
6225c532 3130 }
252b5132
RH
3131 }
3132
47926f60 3133 /* Fill in lexical tables: mnemonic_chars, operand_chars. */
252b5132 3134 {
29b0f896
AM
3135 int c;
3136 char *p;
252b5132
RH
3137
3138 for (c = 0; c < 256; c++)
3139 {
3882b010 3140 if (ISDIGIT (c))
252b5132
RH
3141 {
3142 digit_chars[c] = c;
3143 mnemonic_chars[c] = c;
3144 register_chars[c] = c;
3145 operand_chars[c] = c;
3146 }
3882b010 3147 else if (ISLOWER (c))
252b5132
RH
3148 {
3149 mnemonic_chars[c] = c;
3150 register_chars[c] = c;
3151 operand_chars[c] = c;
3152 }
3882b010 3153 else if (ISUPPER (c))
252b5132 3154 {
3882b010 3155 mnemonic_chars[c] = TOLOWER (c);
252b5132
RH
3156 register_chars[c] = mnemonic_chars[c];
3157 operand_chars[c] = c;
3158 }
43234a1e 3159 else if (c == '{' || c == '}')
86fa6981
L
3160 {
3161 mnemonic_chars[c] = c;
3162 operand_chars[c] = c;
3163 }
b3983e5f
JB
3164#ifdef SVR4_COMMENT_CHARS
3165 else if (c == '\\' && strchr (i386_comment_chars, '/'))
3166 operand_chars[c] = c;
3167#endif
252b5132 3168
3882b010 3169 if (ISALPHA (c) || ISDIGIT (c))
252b5132
RH
3170 identifier_chars[c] = c;
3171 else if (c >= 128)
3172 {
3173 identifier_chars[c] = c;
3174 operand_chars[c] = c;
3175 }
3176 }
3177
3178#ifdef LEX_AT
3179 identifier_chars['@'] = '@';
32137342
NC
3180#endif
3181#ifdef LEX_QM
3182 identifier_chars['?'] = '?';
3183 operand_chars['?'] = '?';
252b5132 3184#endif
252b5132 3185 digit_chars['-'] = '-';
c0f3af97 3186 mnemonic_chars['_'] = '_';
791fe849 3187 mnemonic_chars['-'] = '-';
0003779b 3188 mnemonic_chars['.'] = '.';
252b5132
RH
3189 identifier_chars['_'] = '_';
3190 identifier_chars['.'] = '.';
3191
3192 for (p = operand_special_chars; *p != '\0'; p++)
3193 operand_chars[(unsigned char) *p] = *p;
3194 }
3195
a4447b93
RH
3196 if (flag_code == CODE_64BIT)
3197 {
ca19b261
KT
3198#if defined (OBJ_COFF) && defined (TE_PE)
3199 x86_dwarf2_return_column = (OUTPUT_FLAVOR == bfd_target_coff_flavour
3200 ? 32 : 16);
3201#else
a4447b93 3202 x86_dwarf2_return_column = 16;
ca19b261 3203#endif
61ff971f 3204 x86_cie_data_alignment = -8;
a4447b93
RH
3205 }
3206 else
3207 {
3208 x86_dwarf2_return_column = 8;
3209 x86_cie_data_alignment = -4;
3210 }
e379e5f3
L
3211
3212 /* NB: FUSED_JCC_PADDING frag must have sufficient room so that it
3213 can be turned into BRANCH_PREFIX frag. */
3214 if (align_branch_prefix_size > MAX_FUSED_JCC_PADDING_SIZE)
3215 abort ();
252b5132
RH
3216}
3217
3218void
e3bb37b5 3219i386_print_statistics (FILE *file)
252b5132 3220{
629310ab
ML
3221 htab_print_statistics (file, "i386 opcode", op_hash);
3222 htab_print_statistics (file, "i386 register", reg_hash);
252b5132
RH
3223}
3224\f
252b5132
RH
3225#ifdef DEBUG386
3226
ce8a8b2f 3227/* Debugging routines for md_assemble. */
d3ce72d0 3228static void pte (insn_template *);
40fb9820 3229static void pt (i386_operand_type);
e3bb37b5
L
3230static void pe (expressionS *);
3231static void ps (symbolS *);
252b5132
RH
3232
3233static void
2c703856 3234pi (const char *line, i386_insn *x)
252b5132 3235{
09137c09 3236 unsigned int j;
252b5132
RH
3237
3238 fprintf (stdout, "%s: template ", line);
3239 pte (&x->tm);
09f131f2
JH
3240 fprintf (stdout, " address: base %s index %s scale %x\n",
3241 x->base_reg ? x->base_reg->reg_name : "none",
3242 x->index_reg ? x->index_reg->reg_name : "none",
3243 x->log2_scale_factor);
3244 fprintf (stdout, " modrm: mode %x reg %x reg/mem %x\n",
252b5132 3245 x->rm.mode, x->rm.reg, x->rm.regmem);
09f131f2
JH
3246 fprintf (stdout, " sib: base %x index %x scale %x\n",
3247 x->sib.base, x->sib.index, x->sib.scale);
3248 fprintf (stdout, " rex: 64bit %x extX %x extY %x extZ %x\n",
161a04f6
L
3249 (x->rex & REX_W) != 0,
3250 (x->rex & REX_R) != 0,
3251 (x->rex & REX_X) != 0,
3252 (x->rex & REX_B) != 0);
09137c09 3253 for (j = 0; j < x->operands; j++)
252b5132 3254 {
09137c09
SP
3255 fprintf (stdout, " #%d: ", j + 1);
3256 pt (x->types[j]);
252b5132 3257 fprintf (stdout, "\n");
bab6aec1 3258 if (x->types[j].bitfield.class == Reg
3528c362
JB
3259 || x->types[j].bitfield.class == RegMMX
3260 || x->types[j].bitfield.class == RegSIMD
dd6b8a0b 3261 || x->types[j].bitfield.class == RegMask
00cee14f 3262 || x->types[j].bitfield.class == SReg
4a5c67ed
JB
3263 || x->types[j].bitfield.class == RegCR
3264 || x->types[j].bitfield.class == RegDR
dd6b8a0b
JB
3265 || x->types[j].bitfield.class == RegTR
3266 || x->types[j].bitfield.class == RegBND)
09137c09
SP
3267 fprintf (stdout, "%s\n", x->op[j].regs->reg_name);
3268 if (operand_type_check (x->types[j], imm))
3269 pe (x->op[j].imms);
3270 if (operand_type_check (x->types[j], disp))
3271 pe (x->op[j].disps);
252b5132
RH
3272 }
3273}
3274
3275static void
d3ce72d0 3276pte (insn_template *t)
252b5132 3277{
b933fa4b 3278 static const unsigned char opc_pfx[] = { 0, 0x66, 0xf3, 0xf2 };
441f6aca
JB
3279 static const char *const opc_spc[] = {
3280 NULL, "0f", "0f38", "0f3a", NULL, NULL, NULL, NULL,
3281 "XOP08", "XOP09", "XOP0A",
3282 };
09137c09 3283 unsigned int j;
441f6aca 3284
252b5132 3285 fprintf (stdout, " %d operands ", t->operands);
441f6aca
JB
3286 if (opc_pfx[t->opcode_modifier.opcodeprefix])
3287 fprintf (stdout, "pfx %x ", opc_pfx[t->opcode_modifier.opcodeprefix]);
3288 if (opc_spc[t->opcode_modifier.opcodespace])
3289 fprintf (stdout, "space %s ", opc_spc[t->opcode_modifier.opcodespace]);
47926f60 3290 fprintf (stdout, "opcode %x ", t->base_opcode);
252b5132
RH
3291 if (t->extension_opcode != None)
3292 fprintf (stdout, "ext %x ", t->extension_opcode);
40fb9820 3293 if (t->opcode_modifier.d)
252b5132 3294 fprintf (stdout, "D");
40fb9820 3295 if (t->opcode_modifier.w)
252b5132
RH
3296 fprintf (stdout, "W");
3297 fprintf (stdout, "\n");
09137c09 3298 for (j = 0; j < t->operands; j++)
252b5132 3299 {
09137c09
SP
3300 fprintf (stdout, " #%d type ", j + 1);
3301 pt (t->operand_types[j]);
252b5132
RH
3302 fprintf (stdout, "\n");
3303 }
3304}
3305
3306static void
e3bb37b5 3307pe (expressionS *e)
252b5132 3308{
24eab124 3309 fprintf (stdout, " operation %d\n", e->X_op);
b77ad1d4
AM
3310 fprintf (stdout, " add_number %ld (%lx)\n",
3311 (long) e->X_add_number, (long) e->X_add_number);
252b5132
RH
3312 if (e->X_add_symbol)
3313 {
3314 fprintf (stdout, " add_symbol ");
3315 ps (e->X_add_symbol);
3316 fprintf (stdout, "\n");
3317 }
3318 if (e->X_op_symbol)
3319 {
3320 fprintf (stdout, " op_symbol ");
3321 ps (e->X_op_symbol);
3322 fprintf (stdout, "\n");
3323 }
3324}
3325
3326static void
e3bb37b5 3327ps (symbolS *s)
252b5132
RH
3328{
3329 fprintf (stdout, "%s type %s%s",
3330 S_GET_NAME (s),
3331 S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
3332 segment_name (S_GET_SEGMENT (s)));
3333}
3334
7b81dfbb 3335static struct type_name
252b5132 3336 {
40fb9820
L
3337 i386_operand_type mask;
3338 const char *name;
252b5132 3339 }
7b81dfbb 3340const type_names[] =
252b5132 3341{
40fb9820
L
3342 { OPERAND_TYPE_REG8, "r8" },
3343 { OPERAND_TYPE_REG16, "r16" },
3344 { OPERAND_TYPE_REG32, "r32" },
3345 { OPERAND_TYPE_REG64, "r64" },
2c703856
JB
3346 { OPERAND_TYPE_ACC8, "acc8" },
3347 { OPERAND_TYPE_ACC16, "acc16" },
3348 { OPERAND_TYPE_ACC32, "acc32" },
3349 { OPERAND_TYPE_ACC64, "acc64" },
40fb9820
L
3350 { OPERAND_TYPE_IMM8, "i8" },
3351 { OPERAND_TYPE_IMM8, "i8s" },
3352 { OPERAND_TYPE_IMM16, "i16" },
3353 { OPERAND_TYPE_IMM32, "i32" },
3354 { OPERAND_TYPE_IMM32S, "i32s" },
3355 { OPERAND_TYPE_IMM64, "i64" },
3356 { OPERAND_TYPE_IMM1, "i1" },
3357 { OPERAND_TYPE_BASEINDEX, "BaseIndex" },
3358 { OPERAND_TYPE_DISP8, "d8" },
3359 { OPERAND_TYPE_DISP16, "d16" },
3360 { OPERAND_TYPE_DISP32, "d32" },
3361 { OPERAND_TYPE_DISP32S, "d32s" },
3362 { OPERAND_TYPE_DISP64, "d64" },
3363 { OPERAND_TYPE_INOUTPORTREG, "InOutPortReg" },
3364 { OPERAND_TYPE_SHIFTCOUNT, "ShiftCount" },
3365 { OPERAND_TYPE_CONTROL, "control reg" },
3366 { OPERAND_TYPE_TEST, "test reg" },
3367 { OPERAND_TYPE_DEBUG, "debug reg" },
3368 { OPERAND_TYPE_FLOATREG, "FReg" },
3369 { OPERAND_TYPE_FLOATACC, "FAcc" },
21df382b 3370 { OPERAND_TYPE_SREG, "SReg" },
40fb9820
L
3371 { OPERAND_TYPE_REGMMX, "rMMX" },
3372 { OPERAND_TYPE_REGXMM, "rXMM" },
0349dc08 3373 { OPERAND_TYPE_REGYMM, "rYMM" },
43234a1e 3374 { OPERAND_TYPE_REGZMM, "rZMM" },
260cd341 3375 { OPERAND_TYPE_REGTMM, "rTMM" },
43234a1e 3376 { OPERAND_TYPE_REGMASK, "Mask reg" },
252b5132
RH
3377};
3378
3379static void
40fb9820 3380pt (i386_operand_type t)
252b5132 3381{
40fb9820 3382 unsigned int j;
c6fb90c8 3383 i386_operand_type a;
252b5132 3384
40fb9820 3385 for (j = 0; j < ARRAY_SIZE (type_names); j++)
c6fb90c8
L
3386 {
3387 a = operand_type_and (t, type_names[j].mask);
2c703856 3388 if (operand_type_equal (&a, &type_names[j].mask))
c6fb90c8
L
3389 fprintf (stdout, "%s, ", type_names[j].name);
3390 }
252b5132
RH
3391 fflush (stdout);
3392}
3393
3394#endif /* DEBUG386 */
3395\f
252b5132 3396static bfd_reloc_code_real_type
3956db08 3397reloc (unsigned int size,
64e74474
AM
3398 int pcrel,
3399 int sign,
3400 bfd_reloc_code_real_type other)
252b5132 3401{
47926f60 3402 if (other != NO_RELOC)
3956db08 3403 {
91d6fa6a 3404 reloc_howto_type *rel;
3956db08
JB
3405
3406 if (size == 8)
3407 switch (other)
3408 {
64e74474
AM
3409 case BFD_RELOC_X86_64_GOT32:
3410 return BFD_RELOC_X86_64_GOT64;
3411 break;
553d1284
L
3412 case BFD_RELOC_X86_64_GOTPLT64:
3413 return BFD_RELOC_X86_64_GOTPLT64;
3414 break;
64e74474
AM
3415 case BFD_RELOC_X86_64_PLTOFF64:
3416 return BFD_RELOC_X86_64_PLTOFF64;
3417 break;
3418 case BFD_RELOC_X86_64_GOTPC32:
3419 other = BFD_RELOC_X86_64_GOTPC64;
3420 break;
3421 case BFD_RELOC_X86_64_GOTPCREL:
3422 other = BFD_RELOC_X86_64_GOTPCREL64;
3423 break;
3424 case BFD_RELOC_X86_64_TPOFF32:
3425 other = BFD_RELOC_X86_64_TPOFF64;
3426 break;
3427 case BFD_RELOC_X86_64_DTPOFF32:
3428 other = BFD_RELOC_X86_64_DTPOFF64;
3429 break;
3430 default:
3431 break;
3956db08 3432 }
e05278af 3433
8ce3d284 3434#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
3435 if (other == BFD_RELOC_SIZE32)
3436 {
3437 if (size == 8)
1ab668bf 3438 other = BFD_RELOC_SIZE64;
8fd4256d 3439 if (pcrel)
1ab668bf
AM
3440 {
3441 as_bad (_("there are no pc-relative size relocations"));
3442 return NO_RELOC;
3443 }
8fd4256d 3444 }
8ce3d284 3445#endif
8fd4256d 3446
e05278af 3447 /* Sign-checking 4-byte relocations in 16-/32-bit code is pointless. */
f2d8a97c 3448 if (size == 4 && (flag_code != CODE_64BIT || disallow_64bit_reloc))
e05278af
JB
3449 sign = -1;
3450
91d6fa6a
NC
3451 rel = bfd_reloc_type_lookup (stdoutput, other);
3452 if (!rel)
3956db08 3453 as_bad (_("unknown relocation (%u)"), other);
91d6fa6a 3454 else if (size != bfd_get_reloc_size (rel))
3956db08 3455 as_bad (_("%u-byte relocation cannot be applied to %u-byte field"),
91d6fa6a 3456 bfd_get_reloc_size (rel),
3956db08 3457 size);
91d6fa6a 3458 else if (pcrel && !rel->pc_relative)
3956db08 3459 as_bad (_("non-pc-relative relocation for pc-relative field"));
91d6fa6a 3460 else if ((rel->complain_on_overflow == complain_overflow_signed
3956db08 3461 && !sign)
91d6fa6a 3462 || (rel->complain_on_overflow == complain_overflow_unsigned
64e74474 3463 && sign > 0))
3956db08
JB
3464 as_bad (_("relocated field and relocation type differ in signedness"));
3465 else
3466 return other;
3467 return NO_RELOC;
3468 }
252b5132
RH
3469
3470 if (pcrel)
3471 {
3e73aa7c 3472 if (!sign)
3956db08 3473 as_bad (_("there are no unsigned pc-relative relocations"));
252b5132
RH
3474 switch (size)
3475 {
3476 case 1: return BFD_RELOC_8_PCREL;
3477 case 2: return BFD_RELOC_16_PCREL;
d258b828 3478 case 4: return BFD_RELOC_32_PCREL;
d6ab8113 3479 case 8: return BFD_RELOC_64_PCREL;
252b5132 3480 }
3956db08 3481 as_bad (_("cannot do %u byte pc-relative relocation"), size);
252b5132
RH
3482 }
3483 else
3484 {
3956db08 3485 if (sign > 0)
e5cb08ac 3486 switch (size)
3e73aa7c
JH
3487 {
3488 case 4: return BFD_RELOC_X86_64_32S;
3489 }
3490 else
3491 switch (size)
3492 {
3493 case 1: return BFD_RELOC_8;
3494 case 2: return BFD_RELOC_16;
3495 case 4: return BFD_RELOC_32;
3496 case 8: return BFD_RELOC_64;
3497 }
3956db08
JB
3498 as_bad (_("cannot do %s %u byte relocation"),
3499 sign > 0 ? "signed" : "unsigned", size);
252b5132
RH
3500 }
3501
0cc9e1d3 3502 return NO_RELOC;
252b5132
RH
3503}
3504
47926f60
KH
3505/* Here we decide which fixups can be adjusted to make them relative to
3506 the beginning of the section instead of the symbol. Basically we need
3507 to make sure that the dynamic relocations are done correctly, so in
3508 some cases we force the original symbol to be used. */
3509
252b5132 3510int
e3bb37b5 3511tc_i386_fix_adjustable (fixS *fixP ATTRIBUTE_UNUSED)
252b5132 3512{
6d249963 3513#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 3514 if (!IS_ELF)
31312f95
AM
3515 return 1;
3516
a161fe53
AM
3517 /* Don't adjust pc-relative references to merge sections in 64-bit
3518 mode. */
3519 if (use_rela_relocations
3520 && (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0
3521 && fixP->fx_pcrel)
252b5132 3522 return 0;
31312f95 3523
8d01d9a9
AJ
3524 /* The x86_64 GOTPCREL are represented as 32bit PCrel relocations
3525 and changed later by validate_fix. */
3526 if (GOT_symbol && fixP->fx_subsy == GOT_symbol
3527 && fixP->fx_r_type == BFD_RELOC_32_PCREL)
3528 return 0;
3529
8fd4256d
L
3530 /* Adjust_reloc_syms doesn't know about the GOT. Need to keep symbol
3531 for size relocations. */
3532 if (fixP->fx_r_type == BFD_RELOC_SIZE32
3533 || fixP->fx_r_type == BFD_RELOC_SIZE64
3534 || fixP->fx_r_type == BFD_RELOC_386_GOTOFF
252b5132 3535 || fixP->fx_r_type == BFD_RELOC_386_GOT32
02a86693 3536 || fixP->fx_r_type == BFD_RELOC_386_GOT32X
13ae64f3
JJ
3537 || fixP->fx_r_type == BFD_RELOC_386_TLS_GD
3538 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDM
3539 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDO_32
3540 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE_32
37e55690
JJ
3541 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE
3542 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTIE
13ae64f3
JJ
3543 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE_32
3544 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE
67a4f2b7
AO
3545 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTDESC
3546 || fixP->fx_r_type == BFD_RELOC_386_TLS_DESC_CALL
3e73aa7c 3547 || fixP->fx_r_type == BFD_RELOC_X86_64_GOT32
80b3ee89 3548 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCREL
56ceb5b5
L
3549 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCRELX
3550 || fixP->fx_r_type == BFD_RELOC_X86_64_REX_GOTPCRELX
bffbf940
JJ
3551 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSGD
3552 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSLD
3553 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF32
d6ab8113 3554 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF64
bffbf940
JJ
3555 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF
3556 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF32
d6ab8113
JB
3557 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF64
3558 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTOFF64
67a4f2b7
AO
3559 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
3560 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSDESC_CALL
252b5132
RH
3561 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3562 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3563 return 0;
31312f95 3564#endif
252b5132
RH
3565 return 1;
3566}
252b5132 3567
b4cac588 3568static int
e3bb37b5 3569intel_float_operand (const char *mnemonic)
252b5132 3570{
9306ca4a
JB
3571 /* Note that the value returned is meaningful only for opcodes with (memory)
3572 operands, hence the code here is free to improperly handle opcodes that
3573 have no operands (for better performance and smaller code). */
3574
3575 if (mnemonic[0] != 'f')
3576 return 0; /* non-math */
3577
3578 switch (mnemonic[1])
3579 {
3580 /* fclex, fdecstp, fdisi, femms, feni, fincstp, finit, fsetpm, and
3581 the fs segment override prefix not currently handled because no
3582 call path can make opcodes without operands get here */
3583 case 'i':
3584 return 2 /* integer op */;
3585 case 'l':
3586 if (mnemonic[2] == 'd' && (mnemonic[3] == 'c' || mnemonic[3] == 'e'))
3587 return 3; /* fldcw/fldenv */
3588 break;
3589 case 'n':
3590 if (mnemonic[2] != 'o' /* fnop */)
3591 return 3; /* non-waiting control op */
3592 break;
3593 case 'r':
3594 if (mnemonic[2] == 's')
3595 return 3; /* frstor/frstpm */
3596 break;
3597 case 's':
3598 if (mnemonic[2] == 'a')
3599 return 3; /* fsave */
3600 if (mnemonic[2] == 't')
3601 {
3602 switch (mnemonic[3])
3603 {
3604 case 'c': /* fstcw */
3605 case 'd': /* fstdw */
3606 case 'e': /* fstenv */
3607 case 's': /* fsts[gw] */
3608 return 3;
3609 }
3610 }
3611 break;
3612 case 'x':
3613 if (mnemonic[2] == 'r' || mnemonic[2] == 's')
3614 return 0; /* fxsave/fxrstor are not really math ops */
3615 break;
3616 }
252b5132 3617
9306ca4a 3618 return 1;
252b5132
RH
3619}
3620
9a182d04
JB
3621static INLINE void
3622install_template (const insn_template *t)
3623{
3624 unsigned int l;
3625
3626 i.tm = *t;
3627
3628 /* Note that for pseudo prefixes this produces a length of 1. But for them
3629 the length isn't interesting at all. */
3630 for (l = 1; l < 4; ++l)
3631 if (!(t->base_opcode >> (8 * l)))
3632 break;
3633
3634 i.opcode_length = l;
3635}
3636
c0f3af97
L
3637/* Build the VEX prefix. */
3638
3639static void
d3ce72d0 3640build_vex_prefix (const insn_template *t)
c0f3af97
L
3641{
3642 unsigned int register_specifier;
c0f3af97 3643 unsigned int vector_length;
03751133 3644 unsigned int w;
c0f3af97
L
3645
3646 /* Check register specifier. */
3647 if (i.vex.register_specifier)
43234a1e
L
3648 {
3649 register_specifier =
3650 ~register_number (i.vex.register_specifier) & 0xf;
3651 gas_assert ((i.vex.register_specifier->reg_flags & RegVRex) == 0);
3652 }
c0f3af97
L
3653 else
3654 register_specifier = 0xf;
3655
79f0fa25
L
3656 /* Use 2-byte VEX prefix by swapping destination and source operand
3657 if there are more than 1 register operand. */
3658 if (i.reg_operands > 1
3659 && i.vec_encoding != vex_encoding_vex3
86fa6981 3660 && i.dir_encoding == dir_encoding_default
fa99fab2 3661 && i.operands == i.reg_operands
dbbc8b7e 3662 && operand_type_equal (&i.types[0], &i.types[i.operands - 1])
441f6aca 3663 && i.tm.opcode_modifier.opcodespace == SPACE_0F
dbbc8b7e 3664 && (i.tm.opcode_modifier.load || i.tm.opcode_modifier.d)
fa99fab2
L
3665 && i.rex == REX_B)
3666 {
3667 unsigned int xchg = i.operands - 1;
3668 union i386_op temp_op;
3669 i386_operand_type temp_type;
3670
3671 temp_type = i.types[xchg];
3672 i.types[xchg] = i.types[0];
3673 i.types[0] = temp_type;
3674 temp_op = i.op[xchg];
3675 i.op[xchg] = i.op[0];
3676 i.op[0] = temp_op;
3677
9c2799c2 3678 gas_assert (i.rm.mode == 3);
fa99fab2
L
3679
3680 i.rex = REX_R;
3681 xchg = i.rm.regmem;
3682 i.rm.regmem = i.rm.reg;
3683 i.rm.reg = xchg;
3684
dbbc8b7e
JB
3685 if (i.tm.opcode_modifier.d)
3686 i.tm.base_opcode ^= (i.tm.base_opcode & 0xee) != 0x6e
3687 ? Opcode_SIMD_FloatD : Opcode_SIMD_IntD;
3688 else /* Use the next insn. */
9a182d04 3689 install_template (&t[1]);
fa99fab2
L
3690 }
3691
79dec6b7
JB
3692 /* Use 2-byte VEX prefix by swapping commutative source operands if there
3693 are no memory operands and at least 3 register ones. */
3694 if (i.reg_operands >= 3
3695 && i.vec_encoding != vex_encoding_vex3
3696 && i.reg_operands == i.operands - i.imm_operands
3697 && i.tm.opcode_modifier.vex
3698 && i.tm.opcode_modifier.commutative
3699 && (i.tm.opcode_modifier.sse2avx || optimize > 1)
3700 && i.rex == REX_B
3701 && i.vex.register_specifier
3702 && !(i.vex.register_specifier->reg_flags & RegRex))
3703 {
3704 unsigned int xchg = i.operands - i.reg_operands;
3705 union i386_op temp_op;
3706 i386_operand_type temp_type;
3707
441f6aca 3708 gas_assert (i.tm.opcode_modifier.opcodespace == SPACE_0F);
79dec6b7
JB
3709 gas_assert (!i.tm.opcode_modifier.sae);
3710 gas_assert (operand_type_equal (&i.types[i.operands - 2],
3711 &i.types[i.operands - 3]));
3712 gas_assert (i.rm.mode == 3);
3713
3714 temp_type = i.types[xchg];
3715 i.types[xchg] = i.types[xchg + 1];
3716 i.types[xchg + 1] = temp_type;
3717 temp_op = i.op[xchg];
3718 i.op[xchg] = i.op[xchg + 1];
3719 i.op[xchg + 1] = temp_op;
3720
3721 i.rex = 0;
3722 xchg = i.rm.regmem | 8;
3723 i.rm.regmem = ~register_specifier & 0xf;
3724 gas_assert (!(i.rm.regmem & 8));
3725 i.vex.register_specifier += xchg - i.rm.regmem;
3726 register_specifier = ~xchg & 0xf;
3727 }
3728
539f890d
L
3729 if (i.tm.opcode_modifier.vex == VEXScalar)
3730 vector_length = avxscalar;
10c17abd
JB
3731 else if (i.tm.opcode_modifier.vex == VEX256)
3732 vector_length = 1;
539f890d 3733 else
10c17abd 3734 {
56522fc5 3735 unsigned int op;
10c17abd 3736
c7213af9
L
3737 /* Determine vector length from the last multi-length vector
3738 operand. */
10c17abd 3739 vector_length = 0;
56522fc5 3740 for (op = t->operands; op--;)
10c17abd
JB
3741 if (t->operand_types[op].bitfield.xmmword
3742 && t->operand_types[op].bitfield.ymmword
3743 && i.types[op].bitfield.ymmword)
3744 {
3745 vector_length = 1;
3746 break;
3747 }
3748 }
c0f3af97 3749
03751133
L
3750 /* Check the REX.W bit and VEXW. */
3751 if (i.tm.opcode_modifier.vexw == VEXWIG)
3752 w = (vexwig == vexw1 || (i.rex & REX_W)) ? 1 : 0;
3753 else if (i.tm.opcode_modifier.vexw)
3754 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3755 else
931d03b7 3756 w = (flag_code == CODE_64BIT ? i.rex & REX_W : vexwig == vexw1) ? 1 : 0;
03751133 3757
c0f3af97 3758 /* Use 2-byte VEX prefix if possible. */
03751133
L
3759 if (w == 0
3760 && i.vec_encoding != vex_encoding_vex3
441f6aca 3761 && i.tm.opcode_modifier.opcodespace == SPACE_0F
c0f3af97
L
3762 && (i.rex & (REX_W | REX_X | REX_B)) == 0)
3763 {
3764 /* 2-byte VEX prefix. */
3765 unsigned int r;
3766
3767 i.vex.length = 2;
3768 i.vex.bytes[0] = 0xc5;
3769
3770 /* Check the REX.R bit. */
3771 r = (i.rex & REX_R) ? 0 : 1;
3772 i.vex.bytes[1] = (r << 7
3773 | register_specifier << 3
3774 | vector_length << 2
35648716 3775 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3776 }
3777 else
3778 {
3779 /* 3-byte VEX prefix. */
f88c9eb0 3780 i.vex.length = 3;
f88c9eb0 3781
441f6aca 3782 switch (i.tm.opcode_modifier.opcodespace)
5dd85c99 3783 {
441f6aca
JB
3784 case SPACE_0F:
3785 case SPACE_0F38:
3786 case SPACE_0F3A:
80de6e00 3787 i.vex.bytes[0] = 0xc4;
7f399153 3788 break;
441f6aca
JB
3789 case SPACE_XOP08:
3790 case SPACE_XOP09:
3791 case SPACE_XOP0A:
f88c9eb0 3792 i.vex.bytes[0] = 0x8f;
7f399153
L
3793 break;
3794 default:
3795 abort ();
f88c9eb0 3796 }
c0f3af97 3797
c0f3af97
L
3798 /* The high 3 bits of the second VEX byte are 1's compliment
3799 of RXB bits from REX. */
441f6aca 3800 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | i.tm.opcode_modifier.opcodespace;
c0f3af97 3801
c0f3af97
L
3802 i.vex.bytes[2] = (w << 7
3803 | register_specifier << 3
3804 | vector_length << 2
35648716 3805 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3806 }
3807}
3808
5b7c81bd 3809static INLINE bool
e771e7c9
JB
3810is_evex_encoding (const insn_template *t)
3811{
7091c612 3812 return t->opcode_modifier.evex || t->opcode_modifier.disp8memshift
e771e7c9 3813 || t->opcode_modifier.broadcast || t->opcode_modifier.masking
a80195f1 3814 || t->opcode_modifier.sae;
e771e7c9
JB
3815}
3816
5b7c81bd 3817static INLINE bool
7a8655d2
JB
3818is_any_vex_encoding (const insn_template *t)
3819{
7b47a312 3820 return t->opcode_modifier.vex || is_evex_encoding (t);
7a8655d2
JB
3821}
3822
43234a1e
L
3823/* Build the EVEX prefix. */
3824
3825static void
3826build_evex_prefix (void)
3827{
35648716 3828 unsigned int register_specifier, w;
43234a1e
L
3829 rex_byte vrex_used = 0;
3830
3831 /* Check register specifier. */
3832 if (i.vex.register_specifier)
3833 {
3834 gas_assert ((i.vrex & REX_X) == 0);
3835
3836 register_specifier = i.vex.register_specifier->reg_num;
3837 if ((i.vex.register_specifier->reg_flags & RegRex))
3838 register_specifier += 8;
3839 /* The upper 16 registers are encoded in the fourth byte of the
3840 EVEX prefix. */
3841 if (!(i.vex.register_specifier->reg_flags & RegVRex))
3842 i.vex.bytes[3] = 0x8;
3843 register_specifier = ~register_specifier & 0xf;
3844 }
3845 else
3846 {
3847 register_specifier = 0xf;
3848
3849 /* Encode upper 16 vector index register in the fourth byte of
3850 the EVEX prefix. */
3851 if (!(i.vrex & REX_X))
3852 i.vex.bytes[3] = 0x8;
3853 else
3854 vrex_used |= REX_X;
3855 }
3856
43234a1e
L
3857 /* 4 byte EVEX prefix. */
3858 i.vex.length = 4;
3859 i.vex.bytes[0] = 0x62;
3860
43234a1e
L
3861 /* The high 3 bits of the second EVEX byte are 1's compliment of RXB
3862 bits from REX. */
441f6aca
JB
3863 gas_assert (i.tm.opcode_modifier.opcodespace >= SPACE_0F);
3864 gas_assert (i.tm.opcode_modifier.opcodespace <= SPACE_0F3A);
3865 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | i.tm.opcode_modifier.opcodespace;
43234a1e
L
3866
3867 /* The fifth bit of the second EVEX byte is 1's compliment of the
3868 REX_R bit in VREX. */
3869 if (!(i.vrex & REX_R))
3870 i.vex.bytes[1] |= 0x10;
3871 else
3872 vrex_used |= REX_R;
3873
3874 if ((i.reg_operands + i.imm_operands) == i.operands)
3875 {
3876 /* When all operands are registers, the REX_X bit in REX is not
3877 used. We reuse it to encode the upper 16 registers, which is
3878 indicated by the REX_B bit in VREX. The REX_X bit is encoded
3879 as 1's compliment. */
3880 if ((i.vrex & REX_B))
3881 {
3882 vrex_used |= REX_B;
3883 i.vex.bytes[1] &= ~0x40;
3884 }
3885 }
3886
3887 /* EVEX instructions shouldn't need the REX prefix. */
3888 i.vrex &= ~vrex_used;
3889 gas_assert (i.vrex == 0);
3890
6865c043
L
3891 /* Check the REX.W bit and VEXW. */
3892 if (i.tm.opcode_modifier.vexw == VEXWIG)
3893 w = (evexwig == evexw1 || (i.rex & REX_W)) ? 1 : 0;
3894 else if (i.tm.opcode_modifier.vexw)
3895 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3896 else
931d03b7 3897 w = (flag_code == CODE_64BIT ? i.rex & REX_W : evexwig == evexw1) ? 1 : 0;
43234a1e 3898
43234a1e 3899 /* The third byte of the EVEX prefix. */
35648716
JB
3900 i.vex.bytes[2] = ((w << 7)
3901 | (register_specifier << 3)
3902 | 4 /* Encode the U bit. */
3903 | i.tm.opcode_modifier.opcodeprefix);
43234a1e
L
3904
3905 /* The fourth byte of the EVEX prefix. */
3906 /* The zeroing-masking bit. */
6225c532 3907 if (i.mask.reg && i.mask.zeroing)
43234a1e
L
3908 i.vex.bytes[3] |= 0x80;
3909
3910 /* Don't always set the broadcast bit if there is no RC. */
ca5312a2 3911 if (i.rounding.type == rc_none)
43234a1e
L
3912 {
3913 /* Encode the vector length. */
3914 unsigned int vec_length;
3915
e771e7c9
JB
3916 if (!i.tm.opcode_modifier.evex
3917 || i.tm.opcode_modifier.evex == EVEXDYN)
3918 {
56522fc5 3919 unsigned int op;
e771e7c9 3920
c7213af9
L
3921 /* Determine vector length from the last multi-length vector
3922 operand. */
56522fc5 3923 for (op = i.operands; op--;)
e771e7c9
JB
3924 if (i.tm.operand_types[op].bitfield.xmmword
3925 + i.tm.operand_types[op].bitfield.ymmword
3926 + i.tm.operand_types[op].bitfield.zmmword > 1)
3927 {
3928 if (i.types[op].bitfield.zmmword)
c7213af9
L
3929 {
3930 i.tm.opcode_modifier.evex = EVEX512;
3931 break;
3932 }
e771e7c9 3933 else if (i.types[op].bitfield.ymmword)
c7213af9
L
3934 {
3935 i.tm.opcode_modifier.evex = EVEX256;
3936 break;
3937 }
e771e7c9 3938 else if (i.types[op].bitfield.xmmword)
c7213af9
L
3939 {
3940 i.tm.opcode_modifier.evex = EVEX128;
3941 break;
3942 }
5273a3cd 3943 else if (i.broadcast.type && op == i.broadcast.operand)
625cbd7a 3944 {
5273a3cd 3945 switch (i.broadcast.bytes)
625cbd7a
JB
3946 {
3947 case 64:
3948 i.tm.opcode_modifier.evex = EVEX512;
3949 break;
3950 case 32:
3951 i.tm.opcode_modifier.evex = EVEX256;
3952 break;
3953 case 16:
3954 i.tm.opcode_modifier.evex = EVEX128;
3955 break;
3956 default:
c7213af9 3957 abort ();
625cbd7a 3958 }
c7213af9 3959 break;
625cbd7a 3960 }
e771e7c9 3961 }
c7213af9 3962
56522fc5 3963 if (op >= MAX_OPERANDS)
c7213af9 3964 abort ();
e771e7c9
JB
3965 }
3966
43234a1e
L
3967 switch (i.tm.opcode_modifier.evex)
3968 {
3969 case EVEXLIG: /* LL' is ignored */
3970 vec_length = evexlig << 5;
3971 break;
3972 case EVEX128:
3973 vec_length = 0 << 5;
3974 break;
3975 case EVEX256:
3976 vec_length = 1 << 5;
3977 break;
3978 case EVEX512:
3979 vec_length = 2 << 5;
3980 break;
3981 default:
3982 abort ();
3983 break;
3984 }
3985 i.vex.bytes[3] |= vec_length;
3986 /* Encode the broadcast bit. */
5273a3cd 3987 if (i.broadcast.type)
43234a1e
L
3988 i.vex.bytes[3] |= 0x10;
3989 }
ca5312a2
JB
3990 else if (i.rounding.type != saeonly)
3991 i.vex.bytes[3] |= 0x10 | (i.rounding.type << 5);
43234a1e 3992 else
ca5312a2 3993 i.vex.bytes[3] |= 0x10 | (evexrcig << 5);
43234a1e 3994
6225c532
JB
3995 if (i.mask.reg)
3996 i.vex.bytes[3] |= i.mask.reg->reg_num;
43234a1e
L
3997}
3998
65da13b5
L
3999static void
4000process_immext (void)
4001{
4002 expressionS *exp;
4003
c0f3af97 4004 /* These AMD 3DNow! and SSE2 instructions have an opcode suffix
65da13b5
L
4005 which is coded in the same place as an 8-bit immediate field
4006 would be. Here we fake an 8-bit immediate operand from the
4007 opcode suffix stored in tm.extension_opcode.
4008
c1e679ec 4009 AVX instructions also use this encoding, for some of
c0f3af97 4010 3 argument instructions. */
65da13b5 4011
43234a1e 4012 gas_assert (i.imm_operands <= 1
7ab9ffdd 4013 && (i.operands <= 2
7a8655d2 4014 || (is_any_vex_encoding (&i.tm)
7ab9ffdd 4015 && i.operands <= 4)));
65da13b5
L
4016
4017 exp = &im_expressions[i.imm_operands++];
4018 i.op[i.operands].imms = exp;
4019 i.types[i.operands] = imm8;
4020 i.operands++;
4021 exp->X_op = O_constant;
4022 exp->X_add_number = i.tm.extension_opcode;
4023 i.tm.extension_opcode = None;
4024}
4025
42164a71
L
4026
4027static int
4028check_hle (void)
4029{
742732c7 4030 switch (i.tm.opcode_modifier.prefixok)
42164a71
L
4031 {
4032 default:
4033 abort ();
742732c7
JB
4034 case PrefixLock:
4035 case PrefixNone:
4036 case PrefixNoTrack:
4037 case PrefixRep:
165de32a
L
4038 as_bad (_("invalid instruction `%s' after `%s'"),
4039 i.tm.name, i.hle_prefix);
42164a71 4040 return 0;
742732c7 4041 case PrefixHLELock:
42164a71
L
4042 if (i.prefix[LOCK_PREFIX])
4043 return 1;
165de32a 4044 as_bad (_("missing `lock' with `%s'"), i.hle_prefix);
42164a71 4045 return 0;
742732c7 4046 case PrefixHLEAny:
42164a71 4047 return 1;
742732c7 4048 case PrefixHLERelease:
42164a71
L
4049 if (i.prefix[HLE_PREFIX] != XRELEASE_PREFIX_OPCODE)
4050 {
4051 as_bad (_("instruction `%s' after `xacquire' not allowed"),
4052 i.tm.name);
4053 return 0;
4054 }
8dc0818e 4055 if (i.mem_operands == 0 || !(i.flags[i.operands - 1] & Operand_Mem))
42164a71
L
4056 {
4057 as_bad (_("memory destination needed for instruction `%s'"
4058 " after `xrelease'"), i.tm.name);
4059 return 0;
4060 }
4061 return 1;
4062 }
4063}
4064
b6f8c7c4
L
4065/* Try the shortest encoding by shortening operand size. */
4066
4067static void
4068optimize_encoding (void)
4069{
a0a1771e 4070 unsigned int j;
b6f8c7c4
L
4071
4072 if (optimize_for_space
389d00a5 4073 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
b6f8c7c4
L
4074 && i.reg_operands == 1
4075 && i.imm_operands == 1
4076 && !i.types[1].bitfield.byte
4077 && i.op[0].imms->X_op == O_constant
4078 && fits_in_imm7 (i.op[0].imms->X_add_number)
72aea328 4079 && (i.tm.base_opcode == 0xa8
b6f8c7c4
L
4080 || (i.tm.base_opcode == 0xf6
4081 && i.tm.extension_opcode == 0x0)))
4082 {
4083 /* Optimize: -Os:
4084 test $imm7, %r64/%r32/%r16 -> test $imm7, %r8
4085 */
4086 unsigned int base_regnum = i.op[1].regs->reg_num;
4087 if (flag_code == CODE_64BIT || base_regnum < 4)
4088 {
4089 i.types[1].bitfield.byte = 1;
4090 /* Ignore the suffix. */
4091 i.suffix = 0;
7697afb6
JB
4092 /* Convert to byte registers. */
4093 if (i.types[1].bitfield.word)
4094 j = 16;
4095 else if (i.types[1].bitfield.dword)
4096 j = 32;
4097 else
4098 j = 48;
4099 if (!(i.op[1].regs->reg_flags & RegRex) && base_regnum < 4)
4100 j += 8;
4101 i.op[1].regs -= j;
b6f8c7c4
L
4102 }
4103 }
4104 else if (flag_code == CODE_64BIT
389d00a5 4105 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
d3d50934
L
4106 && ((i.types[1].bitfield.qword
4107 && i.reg_operands == 1
b6f8c7c4
L
4108 && i.imm_operands == 1
4109 && i.op[0].imms->X_op == O_constant
507916b8 4110 && ((i.tm.base_opcode == 0xb8
b6f8c7c4
L
4111 && i.tm.extension_opcode == None
4112 && fits_in_unsigned_long (i.op[0].imms->X_add_number))
4113 || (fits_in_imm31 (i.op[0].imms->X_add_number)
72aea328
JB
4114 && ((i.tm.base_opcode == 0x24
4115 || i.tm.base_opcode == 0xa8)
b6f8c7c4
L
4116 || (i.tm.base_opcode == 0x80
4117 && i.tm.extension_opcode == 0x4)
4118 || ((i.tm.base_opcode == 0xf6
507916b8 4119 || (i.tm.base_opcode | 1) == 0xc7)
b8364fa7
JB
4120 && i.tm.extension_opcode == 0x0)))
4121 || (fits_in_imm7 (i.op[0].imms->X_add_number)
4122 && i.tm.base_opcode == 0x83
4123 && i.tm.extension_opcode == 0x4)))
d3d50934
L
4124 || (i.types[0].bitfield.qword
4125 && ((i.reg_operands == 2
4126 && i.op[0].regs == i.op[1].regs
72aea328
JB
4127 && (i.tm.base_opcode == 0x30
4128 || i.tm.base_opcode == 0x28))
d3d50934
L
4129 || (i.reg_operands == 1
4130 && i.operands == 1
72aea328 4131 && i.tm.base_opcode == 0x30)))))
b6f8c7c4
L
4132 {
4133 /* Optimize: -O:
4134 andq $imm31, %r64 -> andl $imm31, %r32
b8364fa7 4135 andq $imm7, %r64 -> andl $imm7, %r32
b6f8c7c4
L
4136 testq $imm31, %r64 -> testl $imm31, %r32
4137 xorq %r64, %r64 -> xorl %r32, %r32
4138 subq %r64, %r64 -> subl %r32, %r32
4139 movq $imm31, %r64 -> movl $imm31, %r32
4140 movq $imm32, %r64 -> movl $imm32, %r32
4141 */
4142 i.tm.opcode_modifier.norex64 = 1;
507916b8 4143 if (i.tm.base_opcode == 0xb8 || (i.tm.base_opcode | 1) == 0xc7)
b6f8c7c4
L
4144 {
4145 /* Handle
4146 movq $imm31, %r64 -> movl $imm31, %r32
4147 movq $imm32, %r64 -> movl $imm32, %r32
4148 */
4149 i.tm.operand_types[0].bitfield.imm32 = 1;
4150 i.tm.operand_types[0].bitfield.imm32s = 0;
4151 i.tm.operand_types[0].bitfield.imm64 = 0;
4152 i.types[0].bitfield.imm32 = 1;
4153 i.types[0].bitfield.imm32s = 0;
4154 i.types[0].bitfield.imm64 = 0;
4155 i.types[1].bitfield.dword = 1;
4156 i.types[1].bitfield.qword = 0;
507916b8 4157 if ((i.tm.base_opcode | 1) == 0xc7)
b6f8c7c4
L
4158 {
4159 /* Handle
4160 movq $imm31, %r64 -> movl $imm31, %r32
4161 */
507916b8 4162 i.tm.base_opcode = 0xb8;
b6f8c7c4 4163 i.tm.extension_opcode = None;
507916b8 4164 i.tm.opcode_modifier.w = 0;
b6f8c7c4
L
4165 i.tm.opcode_modifier.modrm = 0;
4166 }
4167 }
4168 }
5641ec01
JB
4169 else if (optimize > 1
4170 && !optimize_for_space
389d00a5 4171 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
5641ec01
JB
4172 && i.reg_operands == 2
4173 && i.op[0].regs == i.op[1].regs
4174 && ((i.tm.base_opcode & ~(Opcode_D | 1)) == 0x8
4175 || (i.tm.base_opcode & ~(Opcode_D | 1)) == 0x20)
4176 && (flag_code != CODE_64BIT || !i.types[0].bitfield.dword))
4177 {
4178 /* Optimize: -O2:
4179 andb %rN, %rN -> testb %rN, %rN
4180 andw %rN, %rN -> testw %rN, %rN
4181 andq %rN, %rN -> testq %rN, %rN
4182 orb %rN, %rN -> testb %rN, %rN
4183 orw %rN, %rN -> testw %rN, %rN
4184 orq %rN, %rN -> testq %rN, %rN
4185
4186 and outside of 64-bit mode
4187
4188 andl %rN, %rN -> testl %rN, %rN
4189 orl %rN, %rN -> testl %rN, %rN
4190 */
4191 i.tm.base_opcode = 0x84 | (i.tm.base_opcode & 1);
4192 }
99112332 4193 else if (i.reg_operands == 3
b6f8c7c4
L
4194 && i.op[0].regs == i.op[1].regs
4195 && !i.types[2].bitfield.xmmword
4196 && (i.tm.opcode_modifier.vex
6225c532 4197 || ((!i.mask.reg || i.mask.zeroing)
ca5312a2 4198 && i.rounding.type == rc_none
e771e7c9 4199 && is_evex_encoding (&i.tm)
80c34c38 4200 && (i.vec_encoding != vex_encoding_evex
dd22218c 4201 || cpu_arch_isa_flags.bitfield.cpuavx512vl
80c34c38 4202 || i.tm.cpu_flags.bitfield.cpuavx512vl
7091c612 4203 || (i.tm.operand_types[2].bitfield.zmmword
dd22218c 4204 && i.types[2].bitfield.ymmword))))
b6f8c7c4 4205 && ((i.tm.base_opcode == 0x55
b6f8c7c4 4206 || i.tm.base_opcode == 0x57
35648716
JB
4207 || i.tm.base_opcode == 0xdf
4208 || i.tm.base_opcode == 0xef
4209 || i.tm.base_opcode == 0xf8
4210 || i.tm.base_opcode == 0xf9
4211 || i.tm.base_opcode == 0xfa
4212 || i.tm.base_opcode == 0xfb
1424ad86 4213 || i.tm.base_opcode == 0x42
35648716 4214 || i.tm.base_opcode == 0x47)
b6f8c7c4
L
4215 && i.tm.extension_opcode == None))
4216 {
99112332 4217 /* Optimize: -O1:
8305403a
L
4218 VOP, one of vandnps, vandnpd, vxorps, vxorpd, vpsubb, vpsubd,
4219 vpsubq and vpsubw:
b6f8c7c4
L
4220 EVEX VOP %zmmM, %zmmM, %zmmN
4221 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4222 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4223 EVEX VOP %ymmM, %ymmM, %ymmN
4224 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4225 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4226 VEX VOP %ymmM, %ymmM, %ymmN
4227 -> VEX VOP %xmmM, %xmmM, %xmmN
4228 VOP, one of vpandn and vpxor:
4229 VEX VOP %ymmM, %ymmM, %ymmN
4230 -> VEX VOP %xmmM, %xmmM, %xmmN
4231 VOP, one of vpandnd and vpandnq:
4232 EVEX VOP %zmmM, %zmmM, %zmmN
4233 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4234 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4235 EVEX VOP %ymmM, %ymmM, %ymmN
4236 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4237 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4238 VOP, one of vpxord and vpxorq:
4239 EVEX VOP %zmmM, %zmmM, %zmmN
4240 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4241 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4242 EVEX VOP %ymmM, %ymmM, %ymmN
4243 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4244 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
1424ad86
JB
4245 VOP, one of kxord and kxorq:
4246 VEX VOP %kM, %kM, %kN
4247 -> VEX kxorw %kM, %kM, %kN
4248 VOP, one of kandnd and kandnq:
4249 VEX VOP %kM, %kM, %kN
4250 -> VEX kandnw %kM, %kM, %kN
b6f8c7c4 4251 */
e771e7c9 4252 if (is_evex_encoding (&i.tm))
b6f8c7c4 4253 {
7b1d7ca1 4254 if (i.vec_encoding != vex_encoding_evex)
b6f8c7c4
L
4255 {
4256 i.tm.opcode_modifier.vex = VEX128;
4257 i.tm.opcode_modifier.vexw = VEXW0;
4258 i.tm.opcode_modifier.evex = 0;
4259 }
7b1d7ca1 4260 else if (optimize > 1)
dd22218c
L
4261 i.tm.opcode_modifier.evex = EVEX128;
4262 else
4263 return;
b6f8c7c4 4264 }
f74a6307 4265 else if (i.tm.operand_types[0].bitfield.class == RegMask)
1424ad86 4266 {
35648716 4267 i.tm.opcode_modifier.opcodeprefix = PREFIX_NONE;
1424ad86
JB
4268 i.tm.opcode_modifier.vexw = VEXW0;
4269 }
b6f8c7c4
L
4270 else
4271 i.tm.opcode_modifier.vex = VEX128;
4272
4273 if (i.tm.opcode_modifier.vex)
4274 for (j = 0; j < 3; j++)
4275 {
4276 i.types[j].bitfield.xmmword = 1;
4277 i.types[j].bitfield.ymmword = 0;
4278 }
4279 }
392a5972 4280 else if (i.vec_encoding != vex_encoding_evex
97ed31ae 4281 && !i.types[0].bitfield.zmmword
392a5972 4282 && !i.types[1].bitfield.zmmword
6225c532 4283 && !i.mask.reg
5273a3cd 4284 && !i.broadcast.type
97ed31ae 4285 && is_evex_encoding (&i.tm)
35648716
JB
4286 && ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4287 || (i.tm.base_opcode & ~4) == 0xdb
4288 || (i.tm.base_opcode & ~4) == 0xeb)
97ed31ae
L
4289 && i.tm.extension_opcode == None)
4290 {
4291 /* Optimize: -O1:
4292 VOP, one of vmovdqa32, vmovdqa64, vmovdqu8, vmovdqu16,
4293 vmovdqu32 and vmovdqu64:
4294 EVEX VOP %xmmM, %xmmN
4295 -> VEX vmovdqa|vmovdqu %xmmM, %xmmN (M and N < 16)
4296 EVEX VOP %ymmM, %ymmN
4297 -> VEX vmovdqa|vmovdqu %ymmM, %ymmN (M and N < 16)
4298 EVEX VOP %xmmM, mem
4299 -> VEX vmovdqa|vmovdqu %xmmM, mem (M < 16)
4300 EVEX VOP %ymmM, mem
4301 -> VEX vmovdqa|vmovdqu %ymmM, mem (M < 16)
4302 EVEX VOP mem, %xmmN
4303 -> VEX mvmovdqa|vmovdquem, %xmmN (N < 16)
4304 EVEX VOP mem, %ymmN
4305 -> VEX vmovdqa|vmovdqu mem, %ymmN (N < 16)
a0a1771e
JB
4306 VOP, one of vpand, vpandn, vpor, vpxor:
4307 EVEX VOP{d,q} %xmmL, %xmmM, %xmmN
4308 -> VEX VOP %xmmL, %xmmM, %xmmN (L, M, and N < 16)
4309 EVEX VOP{d,q} %ymmL, %ymmM, %ymmN
4310 -> VEX VOP %ymmL, %ymmM, %ymmN (L, M, and N < 16)
4311 EVEX VOP{d,q} mem, %xmmM, %xmmN
4312 -> VEX VOP mem, %xmmM, %xmmN (M and N < 16)
4313 EVEX VOP{d,q} mem, %ymmM, %ymmN
4314 -> VEX VOP mem, %ymmM, %ymmN (M and N < 16)
97ed31ae 4315 */
a0a1771e 4316 for (j = 0; j < i.operands; j++)
392a5972
L
4317 if (operand_type_check (i.types[j], disp)
4318 && i.op[j].disps->X_op == O_constant)
4319 {
4320 /* Since the VEX prefix has 2 or 3 bytes, the EVEX prefix
4321 has 4 bytes, EVEX Disp8 has 1 byte and VEX Disp32 has 4
4322 bytes, we choose EVEX Disp8 over VEX Disp32. */
4323 int evex_disp8, vex_disp8;
4324 unsigned int memshift = i.memshift;
4325 offsetT n = i.op[j].disps->X_add_number;
4326
4327 evex_disp8 = fits_in_disp8 (n);
4328 i.memshift = 0;
4329 vex_disp8 = fits_in_disp8 (n);
4330 if (evex_disp8 != vex_disp8)
4331 {
4332 i.memshift = memshift;
4333 return;
4334 }
4335
4336 i.types[j].bitfield.disp8 = vex_disp8;
4337 break;
4338 }
35648716
JB
4339 if ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4340 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2)
4341 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
97ed31ae
L
4342 i.tm.opcode_modifier.vex
4343 = i.types[0].bitfield.ymmword ? VEX256 : VEX128;
4344 i.tm.opcode_modifier.vexw = VEXW0;
79dec6b7 4345 /* VPAND, VPOR, and VPXOR are commutative. */
35648716 4346 if (i.reg_operands == 3 && i.tm.base_opcode != 0xdf)
79dec6b7 4347 i.tm.opcode_modifier.commutative = 1;
97ed31ae
L
4348 i.tm.opcode_modifier.evex = 0;
4349 i.tm.opcode_modifier.masking = 0;
a0a1771e 4350 i.tm.opcode_modifier.broadcast = 0;
97ed31ae
L
4351 i.tm.opcode_modifier.disp8memshift = 0;
4352 i.memshift = 0;
a0a1771e
JB
4353 if (j < i.operands)
4354 i.types[j].bitfield.disp8
4355 = fits_in_disp8 (i.op[j].disps->X_add_number);
97ed31ae 4356 }
b6f8c7c4
L
4357}
4358
ae531041
L
4359/* Return non-zero for load instruction. */
4360
4361static int
4362load_insn_p (void)
4363{
4364 unsigned int dest;
4365 int any_vex_p = is_any_vex_encoding (&i.tm);
4366 unsigned int base_opcode = i.tm.base_opcode | 1;
4367
4368 if (!any_vex_p)
4369 {
a09f656b 4370 /* Anysize insns: lea, invlpg, clflush, prefetchnta, prefetcht0,
4371 prefetcht1, prefetcht2, prefetchtw, bndmk, bndcl, bndcu, bndcn,
4372 bndstx, bndldx, prefetchwt1, clflushopt, clwb, cldemote. */
4373 if (i.tm.opcode_modifier.anysize)
ae531041
L
4374 return 0;
4375
389d00a5
JB
4376 /* pop. */
4377 if (strcmp (i.tm.name, "pop") == 0)
4378 return 1;
4379 }
4380
4381 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE)
4382 {
4383 /* popf, popa. */
4384 if (i.tm.base_opcode == 0x9d
a09f656b 4385 || i.tm.base_opcode == 0x61)
ae531041
L
4386 return 1;
4387
4388 /* movs, cmps, lods, scas. */
4389 if ((i.tm.base_opcode | 0xb) == 0xaf)
4390 return 1;
4391
a09f656b 4392 /* outs, xlatb. */
4393 if (base_opcode == 0x6f
4394 || i.tm.base_opcode == 0xd7)
ae531041 4395 return 1;
a09f656b 4396 /* NB: For AMD-specific insns with implicit memory operands,
4397 they're intentionally not covered. */
ae531041
L
4398 }
4399
4400 /* No memory operand. */
4401 if (!i.mem_operands)
4402 return 0;
4403
4404 if (any_vex_p)
4405 {
4406 /* vldmxcsr. */
4407 if (i.tm.base_opcode == 0xae
4408 && i.tm.opcode_modifier.vex
441f6aca 4409 && i.tm.opcode_modifier.opcodespace == SPACE_0F
35648716 4410 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
ae531041
L
4411 && i.tm.extension_opcode == 2)
4412 return 1;
4413 }
389d00a5 4414 else if (i.tm.opcode_modifier.opcodespace == SPACE_BASE)
ae531041
L
4415 {
4416 /* test, not, neg, mul, imul, div, idiv. */
4417 if ((i.tm.base_opcode == 0xf6 || i.tm.base_opcode == 0xf7)
4418 && i.tm.extension_opcode != 1)
4419 return 1;
4420
4421 /* inc, dec. */
4422 if (base_opcode == 0xff && i.tm.extension_opcode <= 1)
4423 return 1;
4424
4425 /* add, or, adc, sbb, and, sub, xor, cmp. */
4426 if (i.tm.base_opcode >= 0x80 && i.tm.base_opcode <= 0x83)
4427 return 1;
4428
ae531041
L
4429 /* rol, ror, rcl, rcr, shl/sal, shr, sar. */
4430 if ((base_opcode == 0xc1
4431 || (i.tm.base_opcode >= 0xd0 && i.tm.base_opcode <= 0xd3))
4432 && i.tm.extension_opcode != 6)
4433 return 1;
4434
ae531041 4435 /* Check for x87 instructions. */
389d00a5 4436 if (base_opcode >= 0xd8 && base_opcode <= 0xdf)
ae531041
L
4437 {
4438 /* Skip fst, fstp, fstenv, fstcw. */
4439 if (i.tm.base_opcode == 0xd9
4440 && (i.tm.extension_opcode == 2
4441 || i.tm.extension_opcode == 3
4442 || i.tm.extension_opcode == 6
4443 || i.tm.extension_opcode == 7))
4444 return 0;
4445
4446 /* Skip fisttp, fist, fistp, fstp. */
4447 if (i.tm.base_opcode == 0xdb
4448 && (i.tm.extension_opcode == 1
4449 || i.tm.extension_opcode == 2
4450 || i.tm.extension_opcode == 3
4451 || i.tm.extension_opcode == 7))
4452 return 0;
4453
4454 /* Skip fisttp, fst, fstp, fsave, fstsw. */
4455 if (i.tm.base_opcode == 0xdd
4456 && (i.tm.extension_opcode == 1
4457 || i.tm.extension_opcode == 2
4458 || i.tm.extension_opcode == 3
4459 || i.tm.extension_opcode == 6
4460 || i.tm.extension_opcode == 7))
4461 return 0;
4462
4463 /* Skip fisttp, fist, fistp, fbstp, fistp. */
4464 if (i.tm.base_opcode == 0xdf
4465 && (i.tm.extension_opcode == 1
4466 || i.tm.extension_opcode == 2
4467 || i.tm.extension_opcode == 3
4468 || i.tm.extension_opcode == 6
4469 || i.tm.extension_opcode == 7))
4470 return 0;
4471
4472 return 1;
4473 }
4474 }
389d00a5
JB
4475 else if (i.tm.opcode_modifier.opcodespace == SPACE_0F)
4476 {
4477 /* bt, bts, btr, btc. */
4478 if (i.tm.base_opcode == 0xba
4479 && (i.tm.extension_opcode >= 4 && i.tm.extension_opcode <= 7))
4480 return 1;
4481
4482 /* cmpxchg8b, cmpxchg16b, xrstors, vmptrld. */
4483 if (i.tm.base_opcode == 0xc7
4484 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
4485 && (i.tm.extension_opcode == 1 || i.tm.extension_opcode == 3
4486 || i.tm.extension_opcode == 6))
4487 return 1;
4488
4489 /* fxrstor, ldmxcsr, xrstor. */
4490 if (i.tm.base_opcode == 0xae
4491 && (i.tm.extension_opcode == 1
4492 || i.tm.extension_opcode == 2
4493 || i.tm.extension_opcode == 5))
4494 return 1;
4495
4496 /* lgdt, lidt, lmsw. */
4497 if (i.tm.base_opcode == 0x01
4498 && (i.tm.extension_opcode == 2
4499 || i.tm.extension_opcode == 3
4500 || i.tm.extension_opcode == 6))
4501 return 1;
4502 }
ae531041
L
4503
4504 dest = i.operands - 1;
4505
4506 /* Check fake imm8 operand and 3 source operands. */
4507 if ((i.tm.opcode_modifier.immext
4508 || i.tm.opcode_modifier.vexsources == VEX3SOURCES)
4509 && i.types[dest].bitfield.imm8)
4510 dest--;
4511
389d00a5
JB
4512 /* add, or, adc, sbb, and, sub, xor, cmp, test, xchg. */
4513 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
ae531041
L
4514 && (base_opcode == 0x1
4515 || base_opcode == 0x9
4516 || base_opcode == 0x11
4517 || base_opcode == 0x19
4518 || base_opcode == 0x21
4519 || base_opcode == 0x29
4520 || base_opcode == 0x31
4521 || base_opcode == 0x39
389d00a5
JB
4522 || (base_opcode | 2) == 0x87))
4523 return 1;
4524
4525 /* xadd. */
4526 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4527 && base_opcode == 0xc1)
ae531041
L
4528 return 1;
4529
4530 /* Check for load instruction. */
4531 return (i.types[dest].bitfield.class != ClassNone
4532 || i.types[dest].bitfield.instance == Accum);
4533}
4534
4535/* Output lfence, 0xfaee8, after instruction. */
4536
4537static void
4538insert_lfence_after (void)
4539{
4540 if (lfence_after_load && load_insn_p ())
4541 {
a09f656b 4542 /* There are also two REP string instructions that require
4543 special treatment. Specifically, the compare string (CMPS)
4544 and scan string (SCAS) instructions set EFLAGS in a manner
4545 that depends on the data being compared/scanned. When used
4546 with a REP prefix, the number of iterations may therefore
4547 vary depending on this data. If the data is a program secret
4548 chosen by the adversary using an LVI method,
4549 then this data-dependent behavior may leak some aspect
4550 of the secret. */
4551 if (((i.tm.base_opcode | 0x1) == 0xa7
4552 || (i.tm.base_opcode | 0x1) == 0xaf)
4553 && i.prefix[REP_PREFIX])
4554 {
4555 as_warn (_("`%s` changes flags which would affect control flow behavior"),
4556 i.tm.name);
4557 }
ae531041
L
4558 char *p = frag_more (3);
4559 *p++ = 0xf;
4560 *p++ = 0xae;
4561 *p = 0xe8;
4562 }
4563}
4564
4565/* Output lfence, 0xfaee8, before instruction. */
4566
4567static void
4568insert_lfence_before (void)
4569{
4570 char *p;
4571
389d00a5 4572 if (i.tm.opcode_modifier.opcodespace != SPACE_BASE)
ae531041
L
4573 return;
4574
4575 if (i.tm.base_opcode == 0xff
4576 && (i.tm.extension_opcode == 2 || i.tm.extension_opcode == 4))
4577 {
4578 /* Insert lfence before indirect branch if needed. */
4579
4580 if (lfence_before_indirect_branch == lfence_branch_none)
4581 return;
4582
4583 if (i.operands != 1)
4584 abort ();
4585
4586 if (i.reg_operands == 1)
4587 {
4588 /* Indirect branch via register. Don't insert lfence with
4589 -mlfence-after-load=yes. */
4590 if (lfence_after_load
4591 || lfence_before_indirect_branch == lfence_branch_memory)
4592 return;
4593 }
4594 else if (i.mem_operands == 1
4595 && lfence_before_indirect_branch != lfence_branch_register)
4596 {
4597 as_warn (_("indirect `%s` with memory operand should be avoided"),
4598 i.tm.name);
4599 return;
4600 }
4601 else
4602 return;
4603
4604 if (last_insn.kind != last_insn_other
4605 && last_insn.seg == now_seg)
4606 {
4607 as_warn_where (last_insn.file, last_insn.line,
4608 _("`%s` skips -mlfence-before-indirect-branch on `%s`"),
4609 last_insn.name, i.tm.name);
4610 return;
4611 }
4612
4613 p = frag_more (3);
4614 *p++ = 0xf;
4615 *p++ = 0xae;
4616 *p = 0xe8;
4617 return;
4618 }
4619
503648e4 4620 /* Output or/not/shl and lfence before near ret. */
ae531041
L
4621 if (lfence_before_ret != lfence_before_ret_none
4622 && (i.tm.base_opcode == 0xc2
503648e4 4623 || i.tm.base_opcode == 0xc3))
ae531041
L
4624 {
4625 if (last_insn.kind != last_insn_other
4626 && last_insn.seg == now_seg)
4627 {
4628 as_warn_where (last_insn.file, last_insn.line,
4629 _("`%s` skips -mlfence-before-ret on `%s`"),
4630 last_insn.name, i.tm.name);
4631 return;
4632 }
a09f656b 4633
a09f656b 4634 /* Near ret ingore operand size override under CPU64. */
503648e4 4635 char prefix = flag_code == CODE_64BIT
4636 ? 0x48
4637 : i.prefix[DATA_PREFIX] ? 0x66 : 0x0;
a09f656b 4638
4639 if (lfence_before_ret == lfence_before_ret_not)
4640 {
4641 /* not: 0xf71424, may add prefix
4642 for operand size override or 64-bit code. */
4643 p = frag_more ((prefix ? 2 : 0) + 6 + 3);
4644 if (prefix)
4645 *p++ = prefix;
ae531041
L
4646 *p++ = 0xf7;
4647 *p++ = 0x14;
4648 *p++ = 0x24;
a09f656b 4649 if (prefix)
4650 *p++ = prefix;
ae531041
L
4651 *p++ = 0xf7;
4652 *p++ = 0x14;
4653 *p++ = 0x24;
4654 }
a09f656b 4655 else
4656 {
4657 p = frag_more ((prefix ? 1 : 0) + 4 + 3);
4658 if (prefix)
4659 *p++ = prefix;
4660 if (lfence_before_ret == lfence_before_ret_or)
4661 {
4662 /* or: 0x830c2400, may add prefix
4663 for operand size override or 64-bit code. */
4664 *p++ = 0x83;
4665 *p++ = 0x0c;
4666 }
4667 else
4668 {
4669 /* shl: 0xc1242400, may add prefix
4670 for operand size override or 64-bit code. */
4671 *p++ = 0xc1;
4672 *p++ = 0x24;
4673 }
4674
4675 *p++ = 0x24;
4676 *p++ = 0x0;
4677 }
4678
ae531041
L
4679 *p++ = 0xf;
4680 *p++ = 0xae;
4681 *p = 0xe8;
4682 }
4683}
4684
252b5132
RH
4685/* This is the guts of the machine-dependent assembler. LINE points to a
4686 machine dependent instruction. This function is supposed to emit
4687 the frags/bytes it assembles to. */
4688
4689void
65da13b5 4690md_assemble (char *line)
252b5132 4691{
40fb9820 4692 unsigned int j;
83b16ac6 4693 char mnemonic[MAX_MNEM_SIZE], mnem_suffix;
d3ce72d0 4694 const insn_template *t;
252b5132 4695
47926f60 4696 /* Initialize globals. */
252b5132 4697 memset (&i, '\0', sizeof (i));
ca5312a2 4698 i.rounding.type = rc_none;
252b5132 4699 for (j = 0; j < MAX_OPERANDS; j++)
1ae12ab7 4700 i.reloc[j] = NO_RELOC;
252b5132
RH
4701 memset (disp_expressions, '\0', sizeof (disp_expressions));
4702 memset (im_expressions, '\0', sizeof (im_expressions));
ce8a8b2f 4703 save_stack_p = save_stack;
252b5132
RH
4704
4705 /* First parse an instruction mnemonic & call i386_operand for the operands.
4706 We assume that the scrubber has arranged it so that line[0] is the valid
47926f60 4707 start of a (possibly prefixed) mnemonic. */
252b5132 4708
29b0f896
AM
4709 line = parse_insn (line, mnemonic);
4710 if (line == NULL)
4711 return;
83b16ac6 4712 mnem_suffix = i.suffix;
252b5132 4713
29b0f896 4714 line = parse_operands (line, mnemonic);
ee86248c 4715 this_operand = -1;
8325cc63
JB
4716 xfree (i.memop1_string);
4717 i.memop1_string = NULL;
29b0f896
AM
4718 if (line == NULL)
4719 return;
252b5132 4720
29b0f896
AM
4721 /* Now we've parsed the mnemonic into a set of templates, and have the
4722 operands at hand. */
4723
b630c145 4724 /* All Intel opcodes have reversed operands except for "bound", "enter",
c0e54661
JB
4725 "invlpg*", "monitor*", "mwait*", "tpause", "umwait", "pvalidate",
4726 "rmpadjust", and "rmpupdate". We also don't reverse intersegment "jmp"
4727 and "call" instructions with 2 immediate operands so that the immediate
4728 segment precedes the offset consistently in Intel and AT&T modes. */
4d456e3d
L
4729 if (intel_syntax
4730 && i.operands > 1
29b0f896 4731 && (strcmp (mnemonic, "bound") != 0)
c0e54661 4732 && (strncmp (mnemonic, "invlpg", 6) != 0)
eedb0f2c
JB
4733 && (strncmp (mnemonic, "monitor", 7) != 0)
4734 && (strncmp (mnemonic, "mwait", 5) != 0)
c0e54661
JB
4735 && (strcmp (mnemonic, "pvalidate") != 0)
4736 && (strncmp (mnemonic, "rmp", 3) != 0)
b630c145
JB
4737 && (strcmp (mnemonic, "tpause") != 0)
4738 && (strcmp (mnemonic, "umwait") != 0)
40fb9820
L
4739 && !(operand_type_check (i.types[0], imm)
4740 && operand_type_check (i.types[1], imm)))
29b0f896
AM
4741 swap_operands ();
4742
ec56d5c0
JB
4743 /* The order of the immediates should be reversed
4744 for 2 immediates extrq and insertq instructions */
4745 if (i.imm_operands == 2
4746 && (strcmp (mnemonic, "extrq") == 0
4747 || strcmp (mnemonic, "insertq") == 0))
4748 swap_2_operands (0, 1);
4749
29b0f896
AM
4750 if (i.imm_operands)
4751 optimize_imm ();
4752
b300c311
L
4753 /* Don't optimize displacement for movabs since it only takes 64bit
4754 displacement. */
4755 if (i.disp_operands
a501d77e 4756 && i.disp_encoding != disp_encoding_32bit
862be3fb
L
4757 && (flag_code != CODE_64BIT
4758 || strcmp (mnemonic, "movabs") != 0))
4759 optimize_disp ();
29b0f896
AM
4760
4761 /* Next, we find a template that matches the given insn,
4762 making sure the overlap of the given operands types is consistent
4763 with the template operand types. */
252b5132 4764
83b16ac6 4765 if (!(t = match_template (mnem_suffix)))
29b0f896 4766 return;
252b5132 4767
7bab8ab5 4768 if (sse_check != check_none
81f8a913 4769 && !i.tm.opcode_modifier.noavx
6e3e5c9e 4770 && !i.tm.cpu_flags.bitfield.cpuavx
569d50f1 4771 && !i.tm.cpu_flags.bitfield.cpuavx512f
daf50ae7
L
4772 && (i.tm.cpu_flags.bitfield.cpusse
4773 || i.tm.cpu_flags.bitfield.cpusse2
4774 || i.tm.cpu_flags.bitfield.cpusse3
4775 || i.tm.cpu_flags.bitfield.cpussse3
4776 || i.tm.cpu_flags.bitfield.cpusse4_1
6e3e5c9e
JB
4777 || i.tm.cpu_flags.bitfield.cpusse4_2
4778 || i.tm.cpu_flags.bitfield.cpupclmul
4779 || i.tm.cpu_flags.bitfield.cpuaes
569d50f1 4780 || i.tm.cpu_flags.bitfield.cpusha
6e3e5c9e 4781 || i.tm.cpu_flags.bitfield.cpugfni))
daf50ae7 4782 {
7bab8ab5 4783 (sse_check == check_warning
daf50ae7
L
4784 ? as_warn
4785 : as_bad) (_("SSE instruction `%s' is used"), i.tm.name);
4786 }
4787
40fb9820 4788 if (i.tm.opcode_modifier.fwait)
29b0f896
AM
4789 if (!add_prefix (FWAIT_OPCODE))
4790 return;
252b5132 4791
d5de92cf 4792 /* Check if REP prefix is OK. */
742732c7 4793 if (i.rep_prefix && i.tm.opcode_modifier.prefixok != PrefixRep)
d5de92cf
L
4794 {
4795 as_bad (_("invalid instruction `%s' after `%s'"),
4796 i.tm.name, i.rep_prefix);
4797 return;
4798 }
4799
c1ba0266
L
4800 /* Check for lock without a lockable instruction. Destination operand
4801 must be memory unless it is xchg (0x86). */
c32fa91d 4802 if (i.prefix[LOCK_PREFIX]
742732c7 4803 && (i.tm.opcode_modifier.prefixok < PrefixLock
c1ba0266
L
4804 || i.mem_operands == 0
4805 || (i.tm.base_opcode != 0x86
8dc0818e 4806 && !(i.flags[i.operands - 1] & Operand_Mem))))
c32fa91d
L
4807 {
4808 as_bad (_("expecting lockable instruction after `lock'"));
4809 return;
4810 }
4811
40d231b4
JB
4812 /* Check for data size prefix on VEX/XOP/EVEX encoded and SIMD insns. */
4813 if (i.prefix[DATA_PREFIX]
4814 && (is_any_vex_encoding (&i.tm)
4815 || i.tm.operand_types[i.imm_operands].bitfield.class >= RegMMX
4816 || i.tm.operand_types[i.imm_operands + 1].bitfield.class >= RegMMX))
7a8655d2
JB
4817 {
4818 as_bad (_("data size prefix invalid with `%s'"), i.tm.name);
4819 return;
4820 }
4821
42164a71 4822 /* Check if HLE prefix is OK. */
165de32a 4823 if (i.hle_prefix && !check_hle ())
42164a71
L
4824 return;
4825
7e8b059b
L
4826 /* Check BND prefix. */
4827 if (i.bnd_prefix && !i.tm.opcode_modifier.bndprefixok)
4828 as_bad (_("expecting valid branch instruction after `bnd'"));
4829
04ef582a 4830 /* Check NOTRACK prefix. */
742732c7 4831 if (i.notrack_prefix && i.tm.opcode_modifier.prefixok != PrefixNoTrack)
9fef80d6 4832 as_bad (_("expecting indirect branch instruction after `notrack'"));
04ef582a 4833
327e8c42
JB
4834 if (i.tm.cpu_flags.bitfield.cpumpx)
4835 {
4836 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
4837 as_bad (_("32-bit address isn't allowed in 64-bit MPX instructions."));
4838 else if (flag_code != CODE_16BIT
4839 ? i.prefix[ADDR_PREFIX]
4840 : i.mem_operands && !i.prefix[ADDR_PREFIX])
4841 as_bad (_("16-bit address isn't allowed in MPX instructions"));
4842 }
7e8b059b
L
4843
4844 /* Insert BND prefix. */
76d3a78a
JB
4845 if (add_bnd_prefix && i.tm.opcode_modifier.bndprefixok)
4846 {
4847 if (!i.prefix[BND_PREFIX])
4848 add_prefix (BND_PREFIX_OPCODE);
4849 else if (i.prefix[BND_PREFIX] != BND_PREFIX_OPCODE)
4850 {
4851 as_warn (_("replacing `rep'/`repe' prefix by `bnd'"));
4852 i.prefix[BND_PREFIX] = BND_PREFIX_OPCODE;
4853 }
4854 }
7e8b059b 4855
29b0f896 4856 /* Check string instruction segment overrides. */
51c8edf6 4857 if (i.tm.opcode_modifier.isstring >= IS_STRING_ES_OP0)
29b0f896 4858 {
51c8edf6 4859 gas_assert (i.mem_operands);
29b0f896 4860 if (!check_string ())
5dd0794d 4861 return;
fc0763e6 4862 i.disp_operands = 0;
29b0f896 4863 }
5dd0794d 4864
b6f8c7c4
L
4865 if (optimize && !i.no_optimize && i.tm.opcode_modifier.optimize)
4866 optimize_encoding ();
4867
29b0f896
AM
4868 if (!process_suffix ())
4869 return;
e413e4e9 4870
921eafea 4871 /* Update operand types and check extended states. */
bc0844ae 4872 for (j = 0; j < i.operands; j++)
921eafea
L
4873 {
4874 i.types[j] = operand_type_and (i.types[j], i.tm.operand_types[j]);
3d70986f 4875 switch (i.tm.operand_types[j].bitfield.class)
921eafea
L
4876 {
4877 default:
4878 break;
4879 case RegMMX:
4880 i.xstate |= xstate_mmx;
4881 break;
4882 case RegMask:
32930e4e 4883 i.xstate |= xstate_mask;
921eafea
L
4884 break;
4885 case RegSIMD:
3d70986f 4886 if (i.tm.operand_types[j].bitfield.tmmword)
921eafea 4887 i.xstate |= xstate_tmm;
3d70986f 4888 else if (i.tm.operand_types[j].bitfield.zmmword)
921eafea 4889 i.xstate |= xstate_zmm;
3d70986f 4890 else if (i.tm.operand_types[j].bitfield.ymmword)
921eafea 4891 i.xstate |= xstate_ymm;
3d70986f 4892 else if (i.tm.operand_types[j].bitfield.xmmword)
921eafea
L
4893 i.xstate |= xstate_xmm;
4894 break;
4895 }
4896 }
bc0844ae 4897
29b0f896
AM
4898 /* Make still unresolved immediate matches conform to size of immediate
4899 given in i.suffix. */
4900 if (!finalize_imm ())
4901 return;
252b5132 4902
40fb9820 4903 if (i.types[0].bitfield.imm1)
29b0f896 4904 i.imm_operands = 0; /* kludge for shift insns. */
252b5132 4905
9afe6eb8
L
4906 /* We only need to check those implicit registers for instructions
4907 with 3 operands or less. */
4908 if (i.operands <= 3)
4909 for (j = 0; j < i.operands; j++)
75e5731b
JB
4910 if (i.types[j].bitfield.instance != InstanceNone
4911 && !i.types[j].bitfield.xmmword)
9afe6eb8 4912 i.reg_operands--;
40fb9820 4913
29b0f896
AM
4914 /* For insns with operands there are more diddles to do to the opcode. */
4915 if (i.operands)
4916 {
4917 if (!process_operands ())
4918 return;
4919 }
8c190ce0 4920 else if (!quiet_warnings && i.tm.opcode_modifier.ugh)
29b0f896
AM
4921 {
4922 /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc. */
4923 as_warn (_("translating to `%sp'"), i.tm.name);
4924 }
252b5132 4925
7a8655d2 4926 if (is_any_vex_encoding (&i.tm))
9e5e5283 4927 {
c1dc7af5 4928 if (!cpu_arch_flags.bitfield.cpui286)
9e5e5283 4929 {
c1dc7af5 4930 as_bad (_("instruction `%s' isn't supported outside of protected mode."),
9e5e5283
L
4931 i.tm.name);
4932 return;
4933 }
c0f3af97 4934
0b9404fd
JB
4935 /* Check for explicit REX prefix. */
4936 if (i.prefix[REX_PREFIX] || i.rex_encoding)
4937 {
4938 as_bad (_("REX prefix invalid with `%s'"), i.tm.name);
4939 return;
4940 }
4941
9e5e5283
L
4942 if (i.tm.opcode_modifier.vex)
4943 build_vex_prefix (t);
4944 else
4945 build_evex_prefix ();
0b9404fd
JB
4946
4947 /* The individual REX.RXBW bits got consumed. */
4948 i.rex &= REX_OPCODE;
9e5e5283 4949 }
43234a1e 4950
5dd85c99
SP
4951 /* Handle conversion of 'int $3' --> special int3 insn. XOP or FMA4
4952 instructions may define INT_OPCODE as well, so avoid this corner
4953 case for those instructions that use MODRM. */
389d00a5
JB
4954 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
4955 && i.tm.base_opcode == INT_OPCODE
a6461c02
SP
4956 && !i.tm.opcode_modifier.modrm
4957 && i.op[0].imms->X_add_number == 3)
29b0f896
AM
4958 {
4959 i.tm.base_opcode = INT3_OPCODE;
4960 i.imm_operands = 0;
4961 }
252b5132 4962
0cfa3eb3
JB
4963 if ((i.tm.opcode_modifier.jump == JUMP
4964 || i.tm.opcode_modifier.jump == JUMP_BYTE
4965 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896
AM
4966 && i.op[0].disps->X_op == O_constant)
4967 {
4968 /* Convert "jmp constant" (and "call constant") to a jump (call) to
4969 the absolute address given by the constant. Since ix86 jumps and
4970 calls are pc relative, we need to generate a reloc. */
4971 i.op[0].disps->X_add_symbol = &abs_symbol;
4972 i.op[0].disps->X_op = O_symbol;
4973 }
252b5132 4974
29b0f896
AM
4975 /* For 8 bit registers we need an empty rex prefix. Also if the
4976 instruction already has a prefix, we need to convert old
4977 registers to new ones. */
773f551c 4978
bab6aec1 4979 if ((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte
29b0f896 4980 && (i.op[0].regs->reg_flags & RegRex64) != 0)
bab6aec1 4981 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte
29b0f896 4982 && (i.op[1].regs->reg_flags & RegRex64) != 0)
bab6aec1
JB
4983 || (((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte)
4984 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte))
29b0f896
AM
4985 && i.rex != 0))
4986 {
4987 int x;
726c5dcd 4988
29b0f896
AM
4989 i.rex |= REX_OPCODE;
4990 for (x = 0; x < 2; x++)
4991 {
4992 /* Look for 8 bit operand that uses old registers. */
bab6aec1 4993 if (i.types[x].bitfield.class == Reg && i.types[x].bitfield.byte
29b0f896 4994 && (i.op[x].regs->reg_flags & RegRex64) == 0)
773f551c 4995 {
3f93af61 4996 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
29b0f896
AM
4997 /* In case it is "hi" register, give up. */
4998 if (i.op[x].regs->reg_num > 3)
a540244d 4999 as_bad (_("can't encode register '%s%s' in an "
4eed87de 5000 "instruction requiring REX prefix."),
a540244d 5001 register_prefix, i.op[x].regs->reg_name);
773f551c 5002
29b0f896
AM
5003 /* Otherwise it is equivalent to the extended register.
5004 Since the encoding doesn't change this is merely
5005 cosmetic cleanup for debug output. */
5006
5007 i.op[x].regs = i.op[x].regs + 8;
773f551c 5008 }
29b0f896
AM
5009 }
5010 }
773f551c 5011
6b6b6807
L
5012 if (i.rex == 0 && i.rex_encoding)
5013 {
5014 /* Check if we can add a REX_OPCODE byte. Look for 8 bit operand
3f93af61 5015 that uses legacy register. If it is "hi" register, don't add
6b6b6807
L
5016 the REX_OPCODE byte. */
5017 int x;
5018 for (x = 0; x < 2; x++)
bab6aec1 5019 if (i.types[x].bitfield.class == Reg
6b6b6807
L
5020 && i.types[x].bitfield.byte
5021 && (i.op[x].regs->reg_flags & RegRex64) == 0
5022 && i.op[x].regs->reg_num > 3)
5023 {
3f93af61 5024 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
5b7c81bd 5025 i.rex_encoding = false;
6b6b6807
L
5026 break;
5027 }
5028
5029 if (i.rex_encoding)
5030 i.rex = REX_OPCODE;
5031 }
5032
7ab9ffdd 5033 if (i.rex != 0)
29b0f896
AM
5034 add_prefix (REX_OPCODE | i.rex);
5035
ae531041
L
5036 insert_lfence_before ();
5037
29b0f896
AM
5038 /* We are ready to output the insn. */
5039 output_insn ();
e379e5f3 5040
ae531041
L
5041 insert_lfence_after ();
5042
e379e5f3
L
5043 last_insn.seg = now_seg;
5044
5045 if (i.tm.opcode_modifier.isprefix)
5046 {
5047 last_insn.kind = last_insn_prefix;
5048 last_insn.name = i.tm.name;
5049 last_insn.file = as_where (&last_insn.line);
5050 }
5051 else
5052 last_insn.kind = last_insn_other;
29b0f896
AM
5053}
5054
5055static char *
e3bb37b5 5056parse_insn (char *line, char *mnemonic)
29b0f896
AM
5057{
5058 char *l = line;
5059 char *token_start = l;
5060 char *mnem_p;
5c6af06e 5061 int supported;
d3ce72d0 5062 const insn_template *t;
b6169b20 5063 char *dot_p = NULL;
29b0f896 5064
29b0f896
AM
5065 while (1)
5066 {
5067 mnem_p = mnemonic;
5068 while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
5069 {
b6169b20
L
5070 if (*mnem_p == '.')
5071 dot_p = mnem_p;
29b0f896
AM
5072 mnem_p++;
5073 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
45288df1 5074 {
29b0f896
AM
5075 as_bad (_("no such instruction: `%s'"), token_start);
5076 return NULL;
5077 }
5078 l++;
5079 }
5080 if (!is_space_char (*l)
5081 && *l != END_OF_INSN
e44823cf
JB
5082 && (intel_syntax
5083 || (*l != PREFIX_SEPARATOR
5084 && *l != ',')))
29b0f896
AM
5085 {
5086 as_bad (_("invalid character %s in mnemonic"),
5087 output_invalid (*l));
5088 return NULL;
5089 }
5090 if (token_start == l)
5091 {
e44823cf 5092 if (!intel_syntax && *l == PREFIX_SEPARATOR)
29b0f896
AM
5093 as_bad (_("expecting prefix; got nothing"));
5094 else
5095 as_bad (_("expecting mnemonic; got nothing"));
5096 return NULL;
5097 }
45288df1 5098
29b0f896 5099 /* Look up instruction (or prefix) via hash table. */
629310ab 5100 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
47926f60 5101
29b0f896
AM
5102 if (*l != END_OF_INSN
5103 && (!is_space_char (*l) || l[1] != END_OF_INSN)
5104 && current_templates
40fb9820 5105 && current_templates->start->opcode_modifier.isprefix)
29b0f896 5106 {
c6fb90c8 5107 if (!cpu_flags_check_cpu64 (current_templates->start->cpu_flags))
2dd88dca
JB
5108 {
5109 as_bad ((flag_code != CODE_64BIT
5110 ? _("`%s' is only supported in 64-bit mode")
5111 : _("`%s' is not supported in 64-bit mode")),
5112 current_templates->start->name);
5113 return NULL;
5114 }
29b0f896
AM
5115 /* If we are in 16-bit mode, do not allow addr16 or data16.
5116 Similarly, in 32-bit mode, do not allow addr32 or data32. */
673fe0f0
JB
5117 if ((current_templates->start->opcode_modifier.size == SIZE16
5118 || current_templates->start->opcode_modifier.size == SIZE32)
29b0f896 5119 && flag_code != CODE_64BIT
673fe0f0 5120 && ((current_templates->start->opcode_modifier.size == SIZE32)
29b0f896
AM
5121 ^ (flag_code == CODE_16BIT)))
5122 {
5123 as_bad (_("redundant %s prefix"),
5124 current_templates->start->name);
5125 return NULL;
45288df1 5126 }
31184569
JB
5127
5128 if (current_templates->start->base_opcode == PSEUDO_PREFIX)
29b0f896 5129 {
86fa6981 5130 /* Handle pseudo prefixes. */
31184569 5131 switch (current_templates->start->extension_opcode)
86fa6981 5132 {
41eb8e88 5133 case Prefix_Disp8:
86fa6981
L
5134 /* {disp8} */
5135 i.disp_encoding = disp_encoding_8bit;
5136 break;
41eb8e88
L
5137 case Prefix_Disp16:
5138 /* {disp16} */
5139 i.disp_encoding = disp_encoding_16bit;
5140 break;
5141 case Prefix_Disp32:
86fa6981
L
5142 /* {disp32} */
5143 i.disp_encoding = disp_encoding_32bit;
5144 break;
41eb8e88 5145 case Prefix_Load:
86fa6981
L
5146 /* {load} */
5147 i.dir_encoding = dir_encoding_load;
5148 break;
41eb8e88 5149 case Prefix_Store:
86fa6981
L
5150 /* {store} */
5151 i.dir_encoding = dir_encoding_store;
5152 break;
41eb8e88 5153 case Prefix_VEX:
42e04b36
L
5154 /* {vex} */
5155 i.vec_encoding = vex_encoding_vex;
86fa6981 5156 break;
41eb8e88 5157 case Prefix_VEX3:
86fa6981
L
5158 /* {vex3} */
5159 i.vec_encoding = vex_encoding_vex3;
5160 break;
41eb8e88 5161 case Prefix_EVEX:
86fa6981
L
5162 /* {evex} */
5163 i.vec_encoding = vex_encoding_evex;
5164 break;
41eb8e88 5165 case Prefix_REX:
6b6b6807 5166 /* {rex} */
5b7c81bd 5167 i.rex_encoding = true;
6b6b6807 5168 break;
41eb8e88 5169 case Prefix_NoOptimize:
b6f8c7c4 5170 /* {nooptimize} */
5b7c81bd 5171 i.no_optimize = true;
b6f8c7c4 5172 break;
86fa6981
L
5173 default:
5174 abort ();
5175 }
5176 }
5177 else
5178 {
5179 /* Add prefix, checking for repeated prefixes. */
4e9ac44a 5180 switch (add_prefix (current_templates->start->base_opcode))
86fa6981 5181 {
4e9ac44a
L
5182 case PREFIX_EXIST:
5183 return NULL;
5184 case PREFIX_DS:
d777820b 5185 if (current_templates->start->cpu_flags.bitfield.cpuibt)
4e9ac44a
L
5186 i.notrack_prefix = current_templates->start->name;
5187 break;
5188 case PREFIX_REP:
5189 if (current_templates->start->cpu_flags.bitfield.cpuhle)
5190 i.hle_prefix = current_templates->start->name;
5191 else if (current_templates->start->cpu_flags.bitfield.cpumpx)
5192 i.bnd_prefix = current_templates->start->name;
5193 else
5194 i.rep_prefix = current_templates->start->name;
5195 break;
5196 default:
5197 break;
86fa6981 5198 }
29b0f896
AM
5199 }
5200 /* Skip past PREFIX_SEPARATOR and reset token_start. */
5201 token_start = ++l;
5202 }
5203 else
5204 break;
5205 }
45288df1 5206
30a55f88 5207 if (!current_templates)
b6169b20 5208 {
07d5e953
JB
5209 /* Deprecated functionality (new code should use pseudo-prefixes instead):
5210 Check if we should swap operand or force 32bit displacement in
f8a5c266 5211 encoding. */
30a55f88 5212 if (mnem_p - 2 == dot_p && dot_p[1] == 's')
64c49ab3 5213 i.dir_encoding = dir_encoding_swap;
8d63c93e 5214 else if (mnem_p - 3 == dot_p
a501d77e
L
5215 && dot_p[1] == 'd'
5216 && dot_p[2] == '8')
5217 i.disp_encoding = disp_encoding_8bit;
8d63c93e 5218 else if (mnem_p - 4 == dot_p
f8a5c266
L
5219 && dot_p[1] == 'd'
5220 && dot_p[2] == '3'
5221 && dot_p[3] == '2')
a501d77e 5222 i.disp_encoding = disp_encoding_32bit;
30a55f88
L
5223 else
5224 goto check_suffix;
5225 mnem_p = dot_p;
5226 *dot_p = '\0';
629310ab 5227 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
b6169b20
L
5228 }
5229
29b0f896
AM
5230 if (!current_templates)
5231 {
dc1e8a47 5232 check_suffix:
1c529385 5233 if (mnem_p > mnemonic)
29b0f896 5234 {
1c529385
LH
5235 /* See if we can get a match by trimming off a suffix. */
5236 switch (mnem_p[-1])
29b0f896 5237 {
1c529385
LH
5238 case WORD_MNEM_SUFFIX:
5239 if (intel_syntax && (intel_float_operand (mnemonic) & 2))
29b0f896
AM
5240 i.suffix = SHORT_MNEM_SUFFIX;
5241 else
1c529385
LH
5242 /* Fall through. */
5243 case BYTE_MNEM_SUFFIX:
5244 case QWORD_MNEM_SUFFIX:
5245 i.suffix = mnem_p[-1];
29b0f896 5246 mnem_p[-1] = '\0';
fe0e921f
AM
5247 current_templates
5248 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5249 break;
5250 case SHORT_MNEM_SUFFIX:
5251 case LONG_MNEM_SUFFIX:
5252 if (!intel_syntax)
5253 {
5254 i.suffix = mnem_p[-1];
5255 mnem_p[-1] = '\0';
fe0e921f
AM
5256 current_templates
5257 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5258 }
5259 break;
5260
5261 /* Intel Syntax. */
5262 case 'd':
5263 if (intel_syntax)
5264 {
5265 if (intel_float_operand (mnemonic) == 1)
5266 i.suffix = SHORT_MNEM_SUFFIX;
5267 else
5268 i.suffix = LONG_MNEM_SUFFIX;
5269 mnem_p[-1] = '\0';
fe0e921f
AM
5270 current_templates
5271 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5272 }
5273 break;
29b0f896 5274 }
29b0f896 5275 }
1c529385 5276
29b0f896
AM
5277 if (!current_templates)
5278 {
5279 as_bad (_("no such instruction: `%s'"), token_start);
5280 return NULL;
5281 }
5282 }
252b5132 5283
0cfa3eb3
JB
5284 if (current_templates->start->opcode_modifier.jump == JUMP
5285 || current_templates->start->opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
5286 {
5287 /* Check for a branch hint. We allow ",pt" and ",pn" for
5288 predict taken and predict not taken respectively.
5289 I'm not sure that branch hints actually do anything on loop
5290 and jcxz insns (JumpByte) for current Pentium4 chips. They
5291 may work in the future and it doesn't hurt to accept them
5292 now. */
5293 if (l[0] == ',' && l[1] == 'p')
5294 {
5295 if (l[2] == 't')
5296 {
5297 if (!add_prefix (DS_PREFIX_OPCODE))
5298 return NULL;
5299 l += 3;
5300 }
5301 else if (l[2] == 'n')
5302 {
5303 if (!add_prefix (CS_PREFIX_OPCODE))
5304 return NULL;
5305 l += 3;
5306 }
5307 }
5308 }
5309 /* Any other comma loses. */
5310 if (*l == ',')
5311 {
5312 as_bad (_("invalid character %s in mnemonic"),
5313 output_invalid (*l));
5314 return NULL;
5315 }
252b5132 5316
29b0f896 5317 /* Check if instruction is supported on specified architecture. */
5c6af06e
JB
5318 supported = 0;
5319 for (t = current_templates->start; t < current_templates->end; ++t)
5320 {
c0f3af97
L
5321 supported |= cpu_flags_match (t);
5322 if (supported == CPU_FLAGS_PERFECT_MATCH)
548d0ee6
JB
5323 {
5324 if (!cpu_arch_flags.bitfield.cpui386 && (flag_code != CODE_16BIT))
5325 as_warn (_("use .code16 to ensure correct addressing mode"));
3629bb00 5326
548d0ee6
JB
5327 return l;
5328 }
29b0f896 5329 }
3629bb00 5330
548d0ee6
JB
5331 if (!(supported & CPU_FLAGS_64BIT_MATCH))
5332 as_bad (flag_code == CODE_64BIT
5333 ? _("`%s' is not supported in 64-bit mode")
5334 : _("`%s' is only supported in 64-bit mode"),
5335 current_templates->start->name);
5336 else
5337 as_bad (_("`%s' is not supported on `%s%s'"),
5338 current_templates->start->name,
5339 cpu_arch_name ? cpu_arch_name : default_arch,
5340 cpu_sub_arch_name ? cpu_sub_arch_name : "");
252b5132 5341
548d0ee6 5342 return NULL;
29b0f896 5343}
252b5132 5344
29b0f896 5345static char *
e3bb37b5 5346parse_operands (char *l, const char *mnemonic)
29b0f896
AM
5347{
5348 char *token_start;
3138f287 5349
29b0f896
AM
5350 /* 1 if operand is pending after ','. */
5351 unsigned int expecting_operand = 0;
252b5132 5352
29b0f896
AM
5353 /* Non-zero if operand parens not balanced. */
5354 unsigned int paren_not_balanced;
5355
5356 while (*l != END_OF_INSN)
5357 {
5358 /* Skip optional white space before operand. */
5359 if (is_space_char (*l))
5360 ++l;
d02603dc 5361 if (!is_operand_char (*l) && *l != END_OF_INSN && *l != '"')
29b0f896
AM
5362 {
5363 as_bad (_("invalid character %s before operand %d"),
5364 output_invalid (*l),
5365 i.operands + 1);
5366 return NULL;
5367 }
d02603dc 5368 token_start = l; /* After white space. */
29b0f896
AM
5369 paren_not_balanced = 0;
5370 while (paren_not_balanced || *l != ',')
5371 {
5372 if (*l == END_OF_INSN)
5373 {
5374 if (paren_not_balanced)
5375 {
5376 if (!intel_syntax)
5377 as_bad (_("unbalanced parenthesis in operand %d."),
5378 i.operands + 1);
5379 else
5380 as_bad (_("unbalanced brackets in operand %d."),
5381 i.operands + 1);
5382 return NULL;
5383 }
5384 else
5385 break; /* we are done */
5386 }
d02603dc 5387 else if (!is_operand_char (*l) && !is_space_char (*l) && *l != '"')
29b0f896
AM
5388 {
5389 as_bad (_("invalid character %s in operand %d"),
5390 output_invalid (*l),
5391 i.operands + 1);
5392 return NULL;
5393 }
5394 if (!intel_syntax)
5395 {
5396 if (*l == '(')
5397 ++paren_not_balanced;
5398 if (*l == ')')
5399 --paren_not_balanced;
5400 }
5401 else
5402 {
5403 if (*l == '[')
5404 ++paren_not_balanced;
5405 if (*l == ']')
5406 --paren_not_balanced;
5407 }
5408 l++;
5409 }
5410 if (l != token_start)
5411 { /* Yes, we've read in another operand. */
5412 unsigned int operand_ok;
5413 this_operand = i.operands++;
5414 if (i.operands > MAX_OPERANDS)
5415 {
5416 as_bad (_("spurious operands; (%d operands/instruction max)"),
5417 MAX_OPERANDS);
5418 return NULL;
5419 }
9d46ce34 5420 i.types[this_operand].bitfield.unspecified = 1;
29b0f896
AM
5421 /* Now parse operand adding info to 'i' as we go along. */
5422 END_STRING_AND_SAVE (l);
5423
1286ab78
L
5424 if (i.mem_operands > 1)
5425 {
5426 as_bad (_("too many memory references for `%s'"),
5427 mnemonic);
5428 return 0;
5429 }
5430
29b0f896
AM
5431 if (intel_syntax)
5432 operand_ok =
5433 i386_intel_operand (token_start,
5434 intel_float_operand (mnemonic));
5435 else
a7619375 5436 operand_ok = i386_att_operand (token_start);
29b0f896
AM
5437
5438 RESTORE_END_STRING (l);
5439 if (!operand_ok)
5440 return NULL;
5441 }
5442 else
5443 {
5444 if (expecting_operand)
5445 {
5446 expecting_operand_after_comma:
5447 as_bad (_("expecting operand after ','; got nothing"));
5448 return NULL;
5449 }
5450 if (*l == ',')
5451 {
5452 as_bad (_("expecting operand before ','; got nothing"));
5453 return NULL;
5454 }
5455 }
7f3f1ea2 5456
29b0f896
AM
5457 /* Now *l must be either ',' or END_OF_INSN. */
5458 if (*l == ',')
5459 {
5460 if (*++l == END_OF_INSN)
5461 {
5462 /* Just skip it, if it's \n complain. */
5463 goto expecting_operand_after_comma;
5464 }
5465 expecting_operand = 1;
5466 }
5467 }
5468 return l;
5469}
7f3f1ea2 5470
050dfa73 5471static void
783c187b 5472swap_2_operands (unsigned int xchg1, unsigned int xchg2)
050dfa73
MM
5473{
5474 union i386_op temp_op;
40fb9820 5475 i386_operand_type temp_type;
c48dadc9 5476 unsigned int temp_flags;
050dfa73 5477 enum bfd_reloc_code_real temp_reloc;
4eed87de 5478
050dfa73
MM
5479 temp_type = i.types[xchg2];
5480 i.types[xchg2] = i.types[xchg1];
5481 i.types[xchg1] = temp_type;
c48dadc9
JB
5482
5483 temp_flags = i.flags[xchg2];
5484 i.flags[xchg2] = i.flags[xchg1];
5485 i.flags[xchg1] = temp_flags;
5486
050dfa73
MM
5487 temp_op = i.op[xchg2];
5488 i.op[xchg2] = i.op[xchg1];
5489 i.op[xchg1] = temp_op;
c48dadc9 5490
050dfa73
MM
5491 temp_reloc = i.reloc[xchg2];
5492 i.reloc[xchg2] = i.reloc[xchg1];
5493 i.reloc[xchg1] = temp_reloc;
43234a1e 5494
6225c532 5495 if (i.mask.reg)
43234a1e 5496 {
6225c532
JB
5497 if (i.mask.operand == xchg1)
5498 i.mask.operand = xchg2;
5499 else if (i.mask.operand == xchg2)
5500 i.mask.operand = xchg1;
43234a1e 5501 }
5273a3cd 5502 if (i.broadcast.type)
43234a1e 5503 {
5273a3cd
JB
5504 if (i.broadcast.operand == xchg1)
5505 i.broadcast.operand = xchg2;
5506 else if (i.broadcast.operand == xchg2)
5507 i.broadcast.operand = xchg1;
43234a1e 5508 }
ca5312a2 5509 if (i.rounding.type != rc_none)
43234a1e 5510 {
ca5312a2
JB
5511 if (i.rounding.operand == xchg1)
5512 i.rounding.operand = xchg2;
5513 else if (i.rounding.operand == xchg2)
5514 i.rounding.operand = xchg1;
43234a1e 5515 }
050dfa73
MM
5516}
5517
29b0f896 5518static void
e3bb37b5 5519swap_operands (void)
29b0f896 5520{
b7c61d9a 5521 switch (i.operands)
050dfa73 5522 {
c0f3af97 5523 case 5:
b7c61d9a 5524 case 4:
4d456e3d 5525 swap_2_operands (1, i.operands - 2);
1a0670f3 5526 /* Fall through. */
b7c61d9a
L
5527 case 3:
5528 case 2:
4d456e3d 5529 swap_2_operands (0, i.operands - 1);
b7c61d9a
L
5530 break;
5531 default:
5532 abort ();
29b0f896 5533 }
29b0f896
AM
5534
5535 if (i.mem_operands == 2)
5536 {
5e042380 5537 const reg_entry *temp_seg;
29b0f896
AM
5538 temp_seg = i.seg[0];
5539 i.seg[0] = i.seg[1];
5540 i.seg[1] = temp_seg;
5541 }
5542}
252b5132 5543
29b0f896
AM
5544/* Try to ensure constant immediates are represented in the smallest
5545 opcode possible. */
5546static void
e3bb37b5 5547optimize_imm (void)
29b0f896
AM
5548{
5549 char guess_suffix = 0;
5550 int op;
252b5132 5551
29b0f896
AM
5552 if (i.suffix)
5553 guess_suffix = i.suffix;
5554 else if (i.reg_operands)
5555 {
5556 /* Figure out a suffix from the last register operand specified.
75e5731b
JB
5557 We can't do this properly yet, i.e. excluding special register
5558 instances, but the following works for instructions with
5559 immediates. In any case, we can't set i.suffix yet. */
29b0f896 5560 for (op = i.operands; --op >= 0;)
bab6aec1
JB
5561 if (i.types[op].bitfield.class != Reg)
5562 continue;
5563 else if (i.types[op].bitfield.byte)
7ab9ffdd 5564 {
40fb9820
L
5565 guess_suffix = BYTE_MNEM_SUFFIX;
5566 break;
5567 }
bab6aec1 5568 else if (i.types[op].bitfield.word)
252b5132 5569 {
40fb9820
L
5570 guess_suffix = WORD_MNEM_SUFFIX;
5571 break;
5572 }
bab6aec1 5573 else if (i.types[op].bitfield.dword)
40fb9820
L
5574 {
5575 guess_suffix = LONG_MNEM_SUFFIX;
5576 break;
5577 }
bab6aec1 5578 else if (i.types[op].bitfield.qword)
40fb9820
L
5579 {
5580 guess_suffix = QWORD_MNEM_SUFFIX;
29b0f896 5581 break;
252b5132 5582 }
29b0f896
AM
5583 }
5584 else if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
5585 guess_suffix = WORD_MNEM_SUFFIX;
5586
5587 for (op = i.operands; --op >= 0;)
40fb9820 5588 if (operand_type_check (i.types[op], imm))
29b0f896
AM
5589 {
5590 switch (i.op[op].imms->X_op)
252b5132 5591 {
29b0f896
AM
5592 case O_constant:
5593 /* If a suffix is given, this operand may be shortened. */
5594 switch (guess_suffix)
252b5132 5595 {
29b0f896 5596 case LONG_MNEM_SUFFIX:
40fb9820
L
5597 i.types[op].bitfield.imm32 = 1;
5598 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
5599 break;
5600 case WORD_MNEM_SUFFIX:
40fb9820
L
5601 i.types[op].bitfield.imm16 = 1;
5602 i.types[op].bitfield.imm32 = 1;
5603 i.types[op].bitfield.imm32s = 1;
5604 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
5605 break;
5606 case BYTE_MNEM_SUFFIX:
40fb9820
L
5607 i.types[op].bitfield.imm8 = 1;
5608 i.types[op].bitfield.imm8s = 1;
5609 i.types[op].bitfield.imm16 = 1;
5610 i.types[op].bitfield.imm32 = 1;
5611 i.types[op].bitfield.imm32s = 1;
5612 i.types[op].bitfield.imm64 = 1;
29b0f896 5613 break;
252b5132 5614 }
252b5132 5615
29b0f896
AM
5616 /* If this operand is at most 16 bits, convert it
5617 to a signed 16 bit number before trying to see
5618 whether it will fit in an even smaller size.
5619 This allows a 16-bit operand such as $0xffe0 to
5620 be recognised as within Imm8S range. */
40fb9820 5621 if ((i.types[op].bitfield.imm16)
29b0f896 5622 && (i.op[op].imms->X_add_number & ~(offsetT) 0xffff) == 0)
252b5132 5623 {
29b0f896
AM
5624 i.op[op].imms->X_add_number =
5625 (((i.op[op].imms->X_add_number & 0xffff) ^ 0x8000) - 0x8000);
5626 }
a28def75
L
5627#ifdef BFD64
5628 /* Store 32-bit immediate in 64-bit for 64-bit BFD. */
40fb9820 5629 if ((i.types[op].bitfield.imm32)
29b0f896
AM
5630 && ((i.op[op].imms->X_add_number & ~(((offsetT) 2 << 31) - 1))
5631 == 0))
5632 {
5633 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
5634 ^ ((offsetT) 1 << 31))
5635 - ((offsetT) 1 << 31));
5636 }
a28def75 5637#endif
40fb9820 5638 i.types[op]
c6fb90c8
L
5639 = operand_type_or (i.types[op],
5640 smallest_imm_type (i.op[op].imms->X_add_number));
252b5132 5641
29b0f896
AM
5642 /* We must avoid matching of Imm32 templates when 64bit
5643 only immediate is available. */
5644 if (guess_suffix == QWORD_MNEM_SUFFIX)
40fb9820 5645 i.types[op].bitfield.imm32 = 0;
29b0f896 5646 break;
252b5132 5647
29b0f896
AM
5648 case O_absent:
5649 case O_register:
5650 abort ();
5651
5652 /* Symbols and expressions. */
5653 default:
9cd96992
JB
5654 /* Convert symbolic operand to proper sizes for matching, but don't
5655 prevent matching a set of insns that only supports sizes other
5656 than those matching the insn suffix. */
5657 {
40fb9820 5658 i386_operand_type mask, allowed;
d3ce72d0 5659 const insn_template *t;
9cd96992 5660
0dfbf9d7
L
5661 operand_type_set (&mask, 0);
5662 operand_type_set (&allowed, 0);
40fb9820 5663
4eed87de
AM
5664 for (t = current_templates->start;
5665 t < current_templates->end;
5666 ++t)
bab6aec1
JB
5667 {
5668 allowed = operand_type_or (allowed, t->operand_types[op]);
5669 allowed = operand_type_and (allowed, anyimm);
5670 }
9cd96992
JB
5671 switch (guess_suffix)
5672 {
5673 case QWORD_MNEM_SUFFIX:
40fb9820
L
5674 mask.bitfield.imm64 = 1;
5675 mask.bitfield.imm32s = 1;
9cd96992
JB
5676 break;
5677 case LONG_MNEM_SUFFIX:
40fb9820 5678 mask.bitfield.imm32 = 1;
9cd96992
JB
5679 break;
5680 case WORD_MNEM_SUFFIX:
40fb9820 5681 mask.bitfield.imm16 = 1;
9cd96992
JB
5682 break;
5683 case BYTE_MNEM_SUFFIX:
40fb9820 5684 mask.bitfield.imm8 = 1;
9cd96992
JB
5685 break;
5686 default:
9cd96992
JB
5687 break;
5688 }
c6fb90c8 5689 allowed = operand_type_and (mask, allowed);
0dfbf9d7 5690 if (!operand_type_all_zero (&allowed))
c6fb90c8 5691 i.types[op] = operand_type_and (i.types[op], mask);
9cd96992 5692 }
29b0f896 5693 break;
252b5132 5694 }
29b0f896
AM
5695 }
5696}
47926f60 5697
29b0f896
AM
5698/* Try to use the smallest displacement type too. */
5699static void
e3bb37b5 5700optimize_disp (void)
29b0f896
AM
5701{
5702 int op;
3e73aa7c 5703
29b0f896 5704 for (op = i.operands; --op >= 0;)
40fb9820 5705 if (operand_type_check (i.types[op], disp))
252b5132 5706 {
b300c311 5707 if (i.op[op].disps->X_op == O_constant)
252b5132 5708 {
91d6fa6a 5709 offsetT op_disp = i.op[op].disps->X_add_number;
29b0f896 5710
40fb9820 5711 if (i.types[op].bitfield.disp16
91d6fa6a 5712 && (op_disp & ~(offsetT) 0xffff) == 0)
b300c311
L
5713 {
5714 /* If this operand is at most 16 bits, convert
5715 to a signed 16 bit number and don't use 64bit
5716 displacement. */
91d6fa6a 5717 op_disp = (((op_disp & 0xffff) ^ 0x8000) - 0x8000);
40fb9820 5718 i.types[op].bitfield.disp64 = 0;
b300c311 5719 }
a28def75
L
5720#ifdef BFD64
5721 /* Optimize 64-bit displacement to 32-bit for 64-bit BFD. */
40fb9820 5722 if (i.types[op].bitfield.disp32
91d6fa6a 5723 && (op_disp & ~(((offsetT) 2 << 31) - 1)) == 0)
b300c311
L
5724 {
5725 /* If this operand is at most 32 bits, convert
5726 to a signed 32 bit number and don't use 64bit
5727 displacement. */
91d6fa6a
NC
5728 op_disp &= (((offsetT) 2 << 31) - 1);
5729 op_disp = (op_disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
40fb9820 5730 i.types[op].bitfield.disp64 = 0;
b300c311 5731 }
a28def75 5732#endif
91d6fa6a 5733 if (!op_disp && i.types[op].bitfield.baseindex)
b300c311 5734 {
40fb9820
L
5735 i.types[op].bitfield.disp8 = 0;
5736 i.types[op].bitfield.disp16 = 0;
5737 i.types[op].bitfield.disp32 = 0;
5738 i.types[op].bitfield.disp32s = 0;
5739 i.types[op].bitfield.disp64 = 0;
b300c311
L
5740 i.op[op].disps = 0;
5741 i.disp_operands--;
5742 }
5743 else if (flag_code == CODE_64BIT)
5744 {
91d6fa6a 5745 if (fits_in_signed_long (op_disp))
28a9d8f5 5746 {
40fb9820
L
5747 i.types[op].bitfield.disp64 = 0;
5748 i.types[op].bitfield.disp32s = 1;
28a9d8f5 5749 }
0e1147d9 5750 if (i.prefix[ADDR_PREFIX]
91d6fa6a 5751 && fits_in_unsigned_long (op_disp))
40fb9820 5752 i.types[op].bitfield.disp32 = 1;
b300c311 5753 }
40fb9820
L
5754 if ((i.types[op].bitfield.disp32
5755 || i.types[op].bitfield.disp32s
5756 || i.types[op].bitfield.disp16)
b5014f7a 5757 && fits_in_disp8 (op_disp))
40fb9820 5758 i.types[op].bitfield.disp8 = 1;
252b5132 5759 }
67a4f2b7
AO
5760 else if (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
5761 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL)
5762 {
5763 fix_new_exp (frag_now, frag_more (0) - frag_now->fr_literal, 0,
5764 i.op[op].disps, 0, i.reloc[op]);
40fb9820
L
5765 i.types[op].bitfield.disp8 = 0;
5766 i.types[op].bitfield.disp16 = 0;
5767 i.types[op].bitfield.disp32 = 0;
5768 i.types[op].bitfield.disp32s = 0;
5769 i.types[op].bitfield.disp64 = 0;
67a4f2b7
AO
5770 }
5771 else
b300c311 5772 /* We only support 64bit displacement on constants. */
40fb9820 5773 i.types[op].bitfield.disp64 = 0;
252b5132 5774 }
29b0f896
AM
5775}
5776
4a1b91ea
L
5777/* Return 1 if there is a match in broadcast bytes between operand
5778 GIVEN and instruction template T. */
5779
5780static INLINE int
5781match_broadcast_size (const insn_template *t, unsigned int given)
5782{
5783 return ((t->opcode_modifier.broadcast == BYTE_BROADCAST
5784 && i.types[given].bitfield.byte)
5785 || (t->opcode_modifier.broadcast == WORD_BROADCAST
5786 && i.types[given].bitfield.word)
5787 || (t->opcode_modifier.broadcast == DWORD_BROADCAST
5788 && i.types[given].bitfield.dword)
5789 || (t->opcode_modifier.broadcast == QWORD_BROADCAST
5790 && i.types[given].bitfield.qword));
5791}
5792
6c30d220
L
5793/* Check if operands are valid for the instruction. */
5794
5795static int
5796check_VecOperands (const insn_template *t)
5797{
43234a1e 5798 unsigned int op;
e2195274 5799 i386_cpu_flags cpu;
e2195274
JB
5800
5801 /* Templates allowing for ZMMword as well as YMMword and/or XMMword for
5802 any one operand are implicity requiring AVX512VL support if the actual
5803 operand size is YMMword or XMMword. Since this function runs after
5804 template matching, there's no need to check for YMMword/XMMword in
5805 the template. */
5806 cpu = cpu_flags_and (t->cpu_flags, avx512);
5807 if (!cpu_flags_all_zero (&cpu)
5808 && !t->cpu_flags.bitfield.cpuavx512vl
5809 && !cpu_arch_flags.bitfield.cpuavx512vl)
5810 {
5811 for (op = 0; op < t->operands; ++op)
5812 {
5813 if (t->operand_types[op].bitfield.zmmword
5814 && (i.types[op].bitfield.ymmword
5815 || i.types[op].bitfield.xmmword))
5816 {
5817 i.error = unsupported;
5818 return 1;
5819 }
5820 }
5821 }
43234a1e 5822
6c30d220 5823 /* Without VSIB byte, we can't have a vector register for index. */
63112cd6 5824 if (!t->opcode_modifier.sib
6c30d220 5825 && i.index_reg
1b54b8d7
JB
5826 && (i.index_reg->reg_type.bitfield.xmmword
5827 || i.index_reg->reg_type.bitfield.ymmword
5828 || i.index_reg->reg_type.bitfield.zmmword))
6c30d220
L
5829 {
5830 i.error = unsupported_vector_index_register;
5831 return 1;
5832 }
5833
ad8ecc81
MZ
5834 /* Check if default mask is allowed. */
5835 if (t->opcode_modifier.nodefmask
6225c532 5836 && (!i.mask.reg || i.mask.reg->reg_num == 0))
ad8ecc81
MZ
5837 {
5838 i.error = no_default_mask;
5839 return 1;
5840 }
5841
7bab8ab5
JB
5842 /* For VSIB byte, we need a vector register for index, and all vector
5843 registers must be distinct. */
260cd341 5844 if (t->opcode_modifier.sib && t->opcode_modifier.sib != SIBMEM)
7bab8ab5
JB
5845 {
5846 if (!i.index_reg
63112cd6 5847 || !((t->opcode_modifier.sib == VECSIB128
1b54b8d7 5848 && i.index_reg->reg_type.bitfield.xmmword)
63112cd6 5849 || (t->opcode_modifier.sib == VECSIB256
1b54b8d7 5850 && i.index_reg->reg_type.bitfield.ymmword)
63112cd6 5851 || (t->opcode_modifier.sib == VECSIB512
1b54b8d7 5852 && i.index_reg->reg_type.bitfield.zmmword)))
7bab8ab5
JB
5853 {
5854 i.error = invalid_vsib_address;
5855 return 1;
5856 }
5857
6225c532
JB
5858 gas_assert (i.reg_operands == 2 || i.mask.reg);
5859 if (i.reg_operands == 2 && !i.mask.reg)
43234a1e 5860 {
3528c362 5861 gas_assert (i.types[0].bitfield.class == RegSIMD);
1b54b8d7
JB
5862 gas_assert (i.types[0].bitfield.xmmword
5863 || i.types[0].bitfield.ymmword);
3528c362 5864 gas_assert (i.types[2].bitfield.class == RegSIMD);
1b54b8d7
JB
5865 gas_assert (i.types[2].bitfield.xmmword
5866 || i.types[2].bitfield.ymmword);
43234a1e
L
5867 if (operand_check == check_none)
5868 return 0;
5869 if (register_number (i.op[0].regs)
5870 != register_number (i.index_reg)
5871 && register_number (i.op[2].regs)
5872 != register_number (i.index_reg)
5873 && register_number (i.op[0].regs)
5874 != register_number (i.op[2].regs))
5875 return 0;
5876 if (operand_check == check_error)
5877 {
5878 i.error = invalid_vector_register_set;
5879 return 1;
5880 }
5881 as_warn (_("mask, index, and destination registers should be distinct"));
5882 }
6225c532 5883 else if (i.reg_operands == 1 && i.mask.reg)
8444f82a 5884 {
3528c362 5885 if (i.types[1].bitfield.class == RegSIMD
1b54b8d7
JB
5886 && (i.types[1].bitfield.xmmword
5887 || i.types[1].bitfield.ymmword
5888 || i.types[1].bitfield.zmmword)
8444f82a
MZ
5889 && (register_number (i.op[1].regs)
5890 == register_number (i.index_reg)))
5891 {
5892 if (operand_check == check_error)
5893 {
5894 i.error = invalid_vector_register_set;
5895 return 1;
5896 }
5897 if (operand_check != check_none)
5898 as_warn (_("index and destination registers should be distinct"));
5899 }
5900 }
43234a1e 5901 }
7bab8ab5 5902
260cd341
LC
5903 /* For AMX instructions with three tmmword operands, all tmmword operand must be
5904 distinct */
5905 if (t->operand_types[0].bitfield.tmmword
5906 && i.reg_operands == 3)
5907 {
5908 if (register_number (i.op[0].regs)
5909 == register_number (i.op[1].regs)
5910 || register_number (i.op[0].regs)
5911 == register_number (i.op[2].regs)
5912 || register_number (i.op[1].regs)
5913 == register_number (i.op[2].regs))
5914 {
5915 i.error = invalid_tmm_register_set;
5916 return 1;
5917 }
5918 }
5919
43234a1e
L
5920 /* Check if broadcast is supported by the instruction and is applied
5921 to the memory operand. */
5273a3cd 5922 if (i.broadcast.type)
43234a1e 5923 {
8e6e0792 5924 i386_operand_type type, overlap;
43234a1e
L
5925
5926 /* Check if specified broadcast is supported in this instruction,
4a1b91ea 5927 and its broadcast bytes match the memory operand. */
5273a3cd 5928 op = i.broadcast.operand;
8e6e0792 5929 if (!t->opcode_modifier.broadcast
c48dadc9 5930 || !(i.flags[op] & Operand_Mem)
c39e5b26 5931 || (!i.types[op].bitfield.unspecified
4a1b91ea 5932 && !match_broadcast_size (t, op)))
43234a1e
L
5933 {
5934 bad_broadcast:
5935 i.error = unsupported_broadcast;
5936 return 1;
5937 }
8e6e0792 5938
5273a3cd
JB
5939 i.broadcast.bytes = ((1 << (t->opcode_modifier.broadcast - 1))
5940 * i.broadcast.type);
8e6e0792 5941 operand_type_set (&type, 0);
5273a3cd 5942 switch (i.broadcast.bytes)
8e6e0792 5943 {
4a1b91ea
L
5944 case 2:
5945 type.bitfield.word = 1;
5946 break;
5947 case 4:
5948 type.bitfield.dword = 1;
5949 break;
8e6e0792
JB
5950 case 8:
5951 type.bitfield.qword = 1;
5952 break;
5953 case 16:
5954 type.bitfield.xmmword = 1;
5955 break;
5956 case 32:
5957 type.bitfield.ymmword = 1;
5958 break;
5959 case 64:
5960 type.bitfield.zmmword = 1;
5961 break;
5962 default:
5963 goto bad_broadcast;
5964 }
5965
5966 overlap = operand_type_and (type, t->operand_types[op]);
bc49bfd8
JB
5967 if (t->operand_types[op].bitfield.class == RegSIMD
5968 && t->operand_types[op].bitfield.byte
5969 + t->operand_types[op].bitfield.word
5970 + t->operand_types[op].bitfield.dword
5971 + t->operand_types[op].bitfield.qword > 1)
5972 {
5973 overlap.bitfield.xmmword = 0;
5974 overlap.bitfield.ymmword = 0;
5975 overlap.bitfield.zmmword = 0;
5976 }
8e6e0792
JB
5977 if (operand_type_all_zero (&overlap))
5978 goto bad_broadcast;
5979
5980 if (t->opcode_modifier.checkregsize)
5981 {
5982 unsigned int j;
5983
e2195274 5984 type.bitfield.baseindex = 1;
8e6e0792
JB
5985 for (j = 0; j < i.operands; ++j)
5986 {
5987 if (j != op
5988 && !operand_type_register_match(i.types[j],
5989 t->operand_types[j],
5990 type,
5991 t->operand_types[op]))
5992 goto bad_broadcast;
5993 }
5994 }
43234a1e
L
5995 }
5996 /* If broadcast is supported in this instruction, we need to check if
5997 operand of one-element size isn't specified without broadcast. */
5998 else if (t->opcode_modifier.broadcast && i.mem_operands)
5999 {
6000 /* Find memory operand. */
6001 for (op = 0; op < i.operands; op++)
8dc0818e 6002 if (i.flags[op] & Operand_Mem)
43234a1e
L
6003 break;
6004 gas_assert (op < i.operands);
6005 /* Check size of the memory operand. */
4a1b91ea 6006 if (match_broadcast_size (t, op))
43234a1e
L
6007 {
6008 i.error = broadcast_needed;
6009 return 1;
6010 }
6011 }
c39e5b26
JB
6012 else
6013 op = MAX_OPERANDS - 1; /* Avoid uninitialized variable warning. */
43234a1e
L
6014
6015 /* Check if requested masking is supported. */
6225c532 6016 if (i.mask.reg)
43234a1e 6017 {
ae2387fe
JB
6018 switch (t->opcode_modifier.masking)
6019 {
6020 case BOTH_MASKING:
6021 break;
6022 case MERGING_MASKING:
6225c532 6023 if (i.mask.zeroing)
ae2387fe
JB
6024 {
6025 case 0:
6026 i.error = unsupported_masking;
6027 return 1;
6028 }
6029 break;
6030 case DYNAMIC_MASKING:
6031 /* Memory destinations allow only merging masking. */
6225c532 6032 if (i.mask.zeroing && i.mem_operands)
ae2387fe
JB
6033 {
6034 /* Find memory operand. */
6035 for (op = 0; op < i.operands; op++)
c48dadc9 6036 if (i.flags[op] & Operand_Mem)
ae2387fe
JB
6037 break;
6038 gas_assert (op < i.operands);
6039 if (op == i.operands - 1)
6040 {
6041 i.error = unsupported_masking;
6042 return 1;
6043 }
6044 }
6045 break;
6046 default:
6047 abort ();
6048 }
43234a1e
L
6049 }
6050
6051 /* Check if masking is applied to dest operand. */
6225c532 6052 if (i.mask.reg && (i.mask.operand != i.operands - 1))
43234a1e
L
6053 {
6054 i.error = mask_not_on_destination;
6055 return 1;
6056 }
6057
43234a1e 6058 /* Check RC/SAE. */
ca5312a2 6059 if (i.rounding.type != rc_none)
43234a1e 6060 {
a80195f1 6061 if (!t->opcode_modifier.sae
ca5312a2 6062 || (i.rounding.type != saeonly && !t->opcode_modifier.staticrounding))
43234a1e
L
6063 {
6064 i.error = unsupported_rc_sae;
6065 return 1;
6066 }
6067 /* If the instruction has several immediate operands and one of
6068 them is rounding, the rounding operand should be the last
6069 immediate operand. */
6070 if (i.imm_operands > 1
ca5312a2 6071 && i.rounding.operand != i.imm_operands - 1)
7bab8ab5 6072 {
43234a1e 6073 i.error = rc_sae_operand_not_last_imm;
7bab8ab5
JB
6074 return 1;
6075 }
6c30d220
L
6076 }
6077
da4977e0
JB
6078 /* Check the special Imm4 cases; must be the first operand. */
6079 if (t->cpu_flags.bitfield.cpuxop && t->operands == 5)
6080 {
6081 if (i.op[0].imms->X_op != O_constant
6082 || !fits_in_imm4 (i.op[0].imms->X_add_number))
6083 {
6084 i.error = bad_imm4;
6085 return 1;
6086 }
6087
6088 /* Turn off Imm<N> so that update_imm won't complain. */
6089 operand_type_set (&i.types[0], 0);
6090 }
6091
43234a1e 6092 /* Check vector Disp8 operand. */
b5014f7a
JB
6093 if (t->opcode_modifier.disp8memshift
6094 && i.disp_encoding != disp_encoding_32bit)
43234a1e 6095 {
5273a3cd 6096 if (i.broadcast.type)
4a1b91ea 6097 i.memshift = t->opcode_modifier.broadcast - 1;
7091c612 6098 else if (t->opcode_modifier.disp8memshift != DISP8_SHIFT_VL)
43234a1e 6099 i.memshift = t->opcode_modifier.disp8memshift;
7091c612
JB
6100 else
6101 {
6102 const i386_operand_type *type = NULL;
6103
6104 i.memshift = 0;
6105 for (op = 0; op < i.operands; op++)
8dc0818e 6106 if (i.flags[op] & Operand_Mem)
7091c612 6107 {
4174bfff
JB
6108 if (t->opcode_modifier.evex == EVEXLIG)
6109 i.memshift = 2 + (i.suffix == QWORD_MNEM_SUFFIX);
6110 else if (t->operand_types[op].bitfield.xmmword
6111 + t->operand_types[op].bitfield.ymmword
6112 + t->operand_types[op].bitfield.zmmword <= 1)
7091c612
JB
6113 type = &t->operand_types[op];
6114 else if (!i.types[op].bitfield.unspecified)
6115 type = &i.types[op];
6116 }
3528c362 6117 else if (i.types[op].bitfield.class == RegSIMD
4174bfff 6118 && t->opcode_modifier.evex != EVEXLIG)
7091c612
JB
6119 {
6120 if (i.types[op].bitfield.zmmword)
6121 i.memshift = 6;
6122 else if (i.types[op].bitfield.ymmword && i.memshift < 5)
6123 i.memshift = 5;
6124 else if (i.types[op].bitfield.xmmword && i.memshift < 4)
6125 i.memshift = 4;
6126 }
6127
6128 if (type)
6129 {
6130 if (type->bitfield.zmmword)
6131 i.memshift = 6;
6132 else if (type->bitfield.ymmword)
6133 i.memshift = 5;
6134 else if (type->bitfield.xmmword)
6135 i.memshift = 4;
6136 }
6137
6138 /* For the check in fits_in_disp8(). */
6139 if (i.memshift == 0)
6140 i.memshift = -1;
6141 }
43234a1e
L
6142
6143 for (op = 0; op < i.operands; op++)
6144 if (operand_type_check (i.types[op], disp)
6145 && i.op[op].disps->X_op == O_constant)
6146 {
b5014f7a 6147 if (fits_in_disp8 (i.op[op].disps->X_add_number))
43234a1e 6148 {
b5014f7a
JB
6149 i.types[op].bitfield.disp8 = 1;
6150 return 0;
43234a1e 6151 }
b5014f7a 6152 i.types[op].bitfield.disp8 = 0;
43234a1e
L
6153 }
6154 }
b5014f7a
JB
6155
6156 i.memshift = 0;
43234a1e 6157
6c30d220
L
6158 return 0;
6159}
6160
da4977e0 6161/* Check if encoding requirements are met by the instruction. */
a683cc34
SP
6162
6163static int
da4977e0 6164VEX_check_encoding (const insn_template *t)
a683cc34 6165{
da4977e0
JB
6166 if (i.vec_encoding == vex_encoding_error)
6167 {
6168 i.error = unsupported;
6169 return 1;
6170 }
6171
86fa6981 6172 if (i.vec_encoding == vex_encoding_evex)
43234a1e 6173 {
86fa6981 6174 /* This instruction must be encoded with EVEX prefix. */
e771e7c9 6175 if (!is_evex_encoding (t))
86fa6981
L
6176 {
6177 i.error = unsupported;
6178 return 1;
6179 }
6180 return 0;
43234a1e
L
6181 }
6182
a683cc34 6183 if (!t->opcode_modifier.vex)
86fa6981
L
6184 {
6185 /* This instruction template doesn't have VEX prefix. */
6186 if (i.vec_encoding != vex_encoding_default)
6187 {
6188 i.error = unsupported;
6189 return 1;
6190 }
6191 return 0;
6192 }
a683cc34 6193
a683cc34
SP
6194 return 0;
6195}
6196
d3ce72d0 6197static const insn_template *
83b16ac6 6198match_template (char mnem_suffix)
29b0f896
AM
6199{
6200 /* Points to template once we've found it. */
d3ce72d0 6201 const insn_template *t;
40fb9820 6202 i386_operand_type overlap0, overlap1, overlap2, overlap3;
c0f3af97 6203 i386_operand_type overlap4;
29b0f896 6204 unsigned int found_reverse_match;
dc2be329 6205 i386_opcode_modifier suffix_check;
40fb9820 6206 i386_operand_type operand_types [MAX_OPERANDS];
539e75ad 6207 int addr_prefix_disp;
45a4bb20 6208 unsigned int j, size_match, check_register;
5614d22c 6209 enum i386_error specific_error = 0;
29b0f896 6210
c0f3af97
L
6211#if MAX_OPERANDS != 5
6212# error "MAX_OPERANDS must be 5."
f48ff2ae
L
6213#endif
6214
29b0f896 6215 found_reverse_match = 0;
539e75ad 6216 addr_prefix_disp = -1;
40fb9820 6217
dc2be329 6218 /* Prepare for mnemonic suffix check. */
40fb9820 6219 memset (&suffix_check, 0, sizeof (suffix_check));
dc2be329
L
6220 switch (mnem_suffix)
6221 {
6222 case BYTE_MNEM_SUFFIX:
6223 suffix_check.no_bsuf = 1;
6224 break;
6225 case WORD_MNEM_SUFFIX:
6226 suffix_check.no_wsuf = 1;
6227 break;
6228 case SHORT_MNEM_SUFFIX:
6229 suffix_check.no_ssuf = 1;
6230 break;
6231 case LONG_MNEM_SUFFIX:
6232 suffix_check.no_lsuf = 1;
6233 break;
6234 case QWORD_MNEM_SUFFIX:
6235 suffix_check.no_qsuf = 1;
6236 break;
6237 default:
6238 /* NB: In Intel syntax, normally we can check for memory operand
6239 size when there is no mnemonic suffix. But jmp and call have
6240 2 different encodings with Dword memory operand size, one with
6241 No_ldSuf and the other without. i.suffix is set to
6242 LONG_DOUBLE_MNEM_SUFFIX to skip the one with No_ldSuf. */
6243 if (i.suffix == LONG_DOUBLE_MNEM_SUFFIX)
6244 suffix_check.no_ldsuf = 1;
83b16ac6
JB
6245 }
6246
01559ecc
L
6247 /* Must have right number of operands. */
6248 i.error = number_of_operands_mismatch;
6249
45aa61fe 6250 for (t = current_templates->start; t < current_templates->end; t++)
29b0f896 6251 {
539e75ad 6252 addr_prefix_disp = -1;
dbbc8b7e 6253 found_reverse_match = 0;
539e75ad 6254
29b0f896
AM
6255 if (i.operands != t->operands)
6256 continue;
6257
50aecf8c 6258 /* Check processor support. */
a65babc9 6259 i.error = unsupported;
45a4bb20 6260 if (cpu_flags_match (t) != CPU_FLAGS_PERFECT_MATCH)
50aecf8c
L
6261 continue;
6262
57392598
CL
6263 /* Check Pseudo Prefix. */
6264 i.error = unsupported;
6265 if (t->opcode_modifier.pseudovexprefix
6266 && !(i.vec_encoding == vex_encoding_vex
6267 || i.vec_encoding == vex_encoding_vex3))
6268 continue;
6269
e1d4d893 6270 /* Check AT&T mnemonic. */
a65babc9 6271 i.error = unsupported_with_intel_mnemonic;
e1d4d893 6272 if (intel_mnemonic && t->opcode_modifier.attmnemonic)
1efbbeb4
L
6273 continue;
6274
4b5aaf5f 6275 /* Check AT&T/Intel syntax. */
a65babc9 6276 i.error = unsupported_syntax;
5c07affc 6277 if ((intel_syntax && t->opcode_modifier.attsyntax)
4b5aaf5f 6278 || (!intel_syntax && t->opcode_modifier.intelsyntax))
1efbbeb4
L
6279 continue;
6280
4b5aaf5f
L
6281 /* Check Intel64/AMD64 ISA. */
6282 switch (isa64)
6283 {
6284 default:
6285 /* Default: Don't accept Intel64. */
6286 if (t->opcode_modifier.isa64 == INTEL64)
6287 continue;
6288 break;
6289 case amd64:
6290 /* -mamd64: Don't accept Intel64 and Intel64 only. */
6291 if (t->opcode_modifier.isa64 >= INTEL64)
6292 continue;
6293 break;
6294 case intel64:
6295 /* -mintel64: Don't accept AMD64. */
5990e377 6296 if (t->opcode_modifier.isa64 == AMD64 && flag_code == CODE_64BIT)
4b5aaf5f
L
6297 continue;
6298 break;
6299 }
6300
dc2be329 6301 /* Check the suffix. */
a65babc9 6302 i.error = invalid_instruction_suffix;
dc2be329
L
6303 if ((t->opcode_modifier.no_bsuf && suffix_check.no_bsuf)
6304 || (t->opcode_modifier.no_wsuf && suffix_check.no_wsuf)
6305 || (t->opcode_modifier.no_lsuf && suffix_check.no_lsuf)
6306 || (t->opcode_modifier.no_ssuf && suffix_check.no_ssuf)
6307 || (t->opcode_modifier.no_qsuf && suffix_check.no_qsuf)
6308 || (t->opcode_modifier.no_ldsuf && suffix_check.no_ldsuf))
83b16ac6 6309 continue;
29b0f896 6310
3ac21baa
JB
6311 size_match = operand_size_match (t);
6312 if (!size_match)
7d5e4556 6313 continue;
539e75ad 6314
6f2f06be
JB
6315 /* This is intentionally not
6316
0cfa3eb3 6317 if (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE))
6f2f06be
JB
6318
6319 as the case of a missing * on the operand is accepted (perhaps with
6320 a warning, issued further down). */
0cfa3eb3 6321 if (i.jumpabsolute && t->opcode_modifier.jump != JUMP_ABSOLUTE)
6f2f06be
JB
6322 {
6323 i.error = operand_type_mismatch;
6324 continue;
6325 }
6326
5c07affc
L
6327 for (j = 0; j < MAX_OPERANDS; j++)
6328 operand_types[j] = t->operand_types[j];
6329
e365e234
JB
6330 /* In general, don't allow
6331 - 64-bit operands outside of 64-bit mode,
6332 - 32-bit operands on pre-386. */
4873e243 6333 j = i.imm_operands + (t->operands > i.imm_operands + 1);
e365e234
JB
6334 if (((i.suffix == QWORD_MNEM_SUFFIX
6335 && flag_code != CODE_64BIT
389d00a5
JB
6336 && !(t->opcode_modifier.opcodespace == SPACE_0F
6337 && t->base_opcode == 0xc7
5e74b495 6338 && t->opcode_modifier.opcodeprefix == PREFIX_NONE
8b65b895 6339 && t->extension_opcode == 1) /* cmpxchg8b */)
e365e234
JB
6340 || (i.suffix == LONG_MNEM_SUFFIX
6341 && !cpu_arch_flags.bitfield.cpui386))
45aa61fe 6342 && (intel_syntax
3cd7f3e3 6343 ? (t->opcode_modifier.mnemonicsize != IGNORESIZE
45aa61fe
AM
6344 && !intel_float_operand (t->name))
6345 : intel_float_operand (t->name) != 2)
4873e243
JB
6346 && (t->operands == i.imm_operands
6347 || (operand_types[i.imm_operands].bitfield.class != RegMMX
6348 && operand_types[i.imm_operands].bitfield.class != RegSIMD
6349 && operand_types[i.imm_operands].bitfield.class != RegMask)
6350 || (operand_types[j].bitfield.class != RegMMX
6351 && operand_types[j].bitfield.class != RegSIMD
6352 && operand_types[j].bitfield.class != RegMask))
63112cd6 6353 && !t->opcode_modifier.sib)
192dc9c6
JB
6354 continue;
6355
29b0f896 6356 /* Do not verify operands when there are none. */
e365e234 6357 if (!t->operands)
da4977e0
JB
6358 {
6359 if (VEX_check_encoding (t))
6360 {
6361 specific_error = i.error;
6362 continue;
6363 }
6364
6365 /* We've found a match; break out of loop. */
6366 break;
6367 }
252b5132 6368
48bcea9f
JB
6369 if (!t->opcode_modifier.jump
6370 || t->opcode_modifier.jump == JUMP_ABSOLUTE)
6371 {
6372 /* There should be only one Disp operand. */
6373 for (j = 0; j < MAX_OPERANDS; j++)
6374 if (operand_type_check (operand_types[j], disp))
539e75ad 6375 break;
48bcea9f
JB
6376 if (j < MAX_OPERANDS)
6377 {
5b7c81bd 6378 bool override = (i.prefix[ADDR_PREFIX] != 0);
48bcea9f
JB
6379
6380 addr_prefix_disp = j;
6381
6382 /* Address size prefix will turn Disp64/Disp32S/Disp32/Disp16
6383 operand into Disp32/Disp32/Disp16/Disp32 operand. */
6384 switch (flag_code)
40fb9820 6385 {
48bcea9f
JB
6386 case CODE_16BIT:
6387 override = !override;
6388 /* Fall through. */
6389 case CODE_32BIT:
6390 if (operand_types[j].bitfield.disp32
6391 && operand_types[j].bitfield.disp16)
40fb9820 6392 {
48bcea9f
JB
6393 operand_types[j].bitfield.disp16 = override;
6394 operand_types[j].bitfield.disp32 = !override;
40fb9820 6395 }
48bcea9f
JB
6396 operand_types[j].bitfield.disp32s = 0;
6397 operand_types[j].bitfield.disp64 = 0;
6398 break;
6399
6400 case CODE_64BIT:
6401 if (operand_types[j].bitfield.disp32s
6402 || operand_types[j].bitfield.disp64)
40fb9820 6403 {
48bcea9f
JB
6404 operand_types[j].bitfield.disp64 &= !override;
6405 operand_types[j].bitfield.disp32s &= !override;
6406 operand_types[j].bitfield.disp32 = override;
40fb9820 6407 }
48bcea9f
JB
6408 operand_types[j].bitfield.disp16 = 0;
6409 break;
40fb9820 6410 }
539e75ad 6411 }
48bcea9f 6412 }
539e75ad 6413
02a86693 6414 /* Force 0x8b encoding for "mov foo@GOT, %eax". */
35648716
JB
6415 if (i.reloc[0] == BFD_RELOC_386_GOT32
6416 && t->base_opcode == 0xa0
6417 && t->opcode_modifier.opcodespace == SPACE_BASE)
02a86693
L
6418 continue;
6419
56ffb741 6420 /* We check register size if needed. */
e2195274
JB
6421 if (t->opcode_modifier.checkregsize)
6422 {
6423 check_register = (1 << t->operands) - 1;
5273a3cd
JB
6424 if (i.broadcast.type)
6425 check_register &= ~(1 << i.broadcast.operand);
e2195274
JB
6426 }
6427 else
6428 check_register = 0;
6429
c6fb90c8 6430 overlap0 = operand_type_and (i.types[0], operand_types[0]);
29b0f896
AM
6431 switch (t->operands)
6432 {
6433 case 1:
40fb9820 6434 if (!operand_type_match (overlap0, i.types[0]))
29b0f896
AM
6435 continue;
6436 break;
6437 case 2:
33eaf5de 6438 /* xchg %eax, %eax is a special case. It is an alias for nop
8b38ad71
L
6439 only in 32bit mode and we can use opcode 0x90. In 64bit
6440 mode, we can't use 0x90 for xchg %eax, %eax since it should
6441 zero-extend %eax to %rax. */
6442 if (flag_code == CODE_64BIT
6443 && t->base_opcode == 0x90
35648716 6444 && t->opcode_modifier.opcodespace == SPACE_BASE
75e5731b
JB
6445 && i.types[0].bitfield.instance == Accum
6446 && i.types[0].bitfield.dword
6447 && i.types[1].bitfield.instance == Accum
6448 && i.types[1].bitfield.dword)
8b38ad71 6449 continue;
1212781b
JB
6450 /* xrelease mov %eax, <disp> is another special case. It must not
6451 match the accumulator-only encoding of mov. */
6452 if (flag_code != CODE_64BIT
6453 && i.hle_prefix
6454 && t->base_opcode == 0xa0
35648716 6455 && t->opcode_modifier.opcodespace == SPACE_BASE
75e5731b 6456 && i.types[0].bitfield.instance == Accum
8dc0818e 6457 && (i.flags[1] & Operand_Mem))
1212781b 6458 continue;
f5eb1d70
JB
6459 /* Fall through. */
6460
6461 case 3:
3ac21baa
JB
6462 if (!(size_match & MATCH_STRAIGHT))
6463 goto check_reverse;
64c49ab3
JB
6464 /* Reverse direction of operands if swapping is possible in the first
6465 place (operands need to be symmetric) and
6466 - the load form is requested, and the template is a store form,
6467 - the store form is requested, and the template is a load form,
6468 - the non-default (swapped) form is requested. */
6469 overlap1 = operand_type_and (operand_types[0], operand_types[1]);
f5eb1d70 6470 if (t->opcode_modifier.d && i.reg_operands == i.operands
64c49ab3
JB
6471 && !operand_type_all_zero (&overlap1))
6472 switch (i.dir_encoding)
6473 {
6474 case dir_encoding_load:
6475 if (operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 6476 || t->opcode_modifier.regmem)
64c49ab3
JB
6477 goto check_reverse;
6478 break;
6479
6480 case dir_encoding_store:
6481 if (!operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 6482 && !t->opcode_modifier.regmem)
64c49ab3
JB
6483 goto check_reverse;
6484 break;
6485
6486 case dir_encoding_swap:
6487 goto check_reverse;
6488
6489 case dir_encoding_default:
6490 break;
6491 }
86fa6981 6492 /* If we want store form, we skip the current load. */
64c49ab3
JB
6493 if ((i.dir_encoding == dir_encoding_store
6494 || i.dir_encoding == dir_encoding_swap)
86fa6981
L
6495 && i.mem_operands == 0
6496 && t->opcode_modifier.load)
fa99fab2 6497 continue;
1a0670f3 6498 /* Fall through. */
f48ff2ae 6499 case 4:
c0f3af97 6500 case 5:
c6fb90c8 6501 overlap1 = operand_type_and (i.types[1], operand_types[1]);
40fb9820
L
6502 if (!operand_type_match (overlap0, i.types[0])
6503 || !operand_type_match (overlap1, i.types[1])
e2195274 6504 || ((check_register & 3) == 3
dc821c5f 6505 && !operand_type_register_match (i.types[0],
40fb9820 6506 operand_types[0],
dc821c5f 6507 i.types[1],
40fb9820 6508 operand_types[1])))
29b0f896
AM
6509 {
6510 /* Check if other direction is valid ... */
38e314eb 6511 if (!t->opcode_modifier.d)
29b0f896
AM
6512 continue;
6513
dc1e8a47 6514 check_reverse:
3ac21baa
JB
6515 if (!(size_match & MATCH_REVERSE))
6516 continue;
29b0f896 6517 /* Try reversing direction of operands. */
f5eb1d70
JB
6518 overlap0 = operand_type_and (i.types[0], operand_types[i.operands - 1]);
6519 overlap1 = operand_type_and (i.types[i.operands - 1], operand_types[0]);
40fb9820 6520 if (!operand_type_match (overlap0, i.types[0])
f5eb1d70 6521 || !operand_type_match (overlap1, i.types[i.operands - 1])
45664ddb 6522 || (check_register
dc821c5f 6523 && !operand_type_register_match (i.types[0],
f5eb1d70
JB
6524 operand_types[i.operands - 1],
6525 i.types[i.operands - 1],
45664ddb 6526 operand_types[0])))
29b0f896
AM
6527 {
6528 /* Does not match either direction. */
6529 continue;
6530 }
38e314eb 6531 /* found_reverse_match holds which of D or FloatR
29b0f896 6532 we've found. */
38e314eb
JB
6533 if (!t->opcode_modifier.d)
6534 found_reverse_match = 0;
6535 else if (operand_types[0].bitfield.tbyte)
8a2ed489 6536 found_reverse_match = Opcode_FloatD;
dbbc8b7e 6537 else if (operand_types[0].bitfield.xmmword
f5eb1d70 6538 || operand_types[i.operands - 1].bitfield.xmmword
3528c362
JB
6539 || operand_types[0].bitfield.class == RegMMX
6540 || operand_types[i.operands - 1].bitfield.class == RegMMX
dbbc8b7e
JB
6541 || is_any_vex_encoding(t))
6542 found_reverse_match = (t->base_opcode & 0xee) != 0x6e
6543 ? Opcode_SIMD_FloatD : Opcode_SIMD_IntD;
8a2ed489 6544 else
38e314eb 6545 found_reverse_match = Opcode_D;
40fb9820 6546 if (t->opcode_modifier.floatr)
8a2ed489 6547 found_reverse_match |= Opcode_FloatR;
29b0f896 6548 }
f48ff2ae 6549 else
29b0f896 6550 {
f48ff2ae 6551 /* Found a forward 2 operand match here. */
d1cbb4db
L
6552 switch (t->operands)
6553 {
c0f3af97
L
6554 case 5:
6555 overlap4 = operand_type_and (i.types[4],
6556 operand_types[4]);
1a0670f3 6557 /* Fall through. */
d1cbb4db 6558 case 4:
c6fb90c8
L
6559 overlap3 = operand_type_and (i.types[3],
6560 operand_types[3]);
1a0670f3 6561 /* Fall through. */
d1cbb4db 6562 case 3:
c6fb90c8
L
6563 overlap2 = operand_type_and (i.types[2],
6564 operand_types[2]);
d1cbb4db
L
6565 break;
6566 }
29b0f896 6567
f48ff2ae
L
6568 switch (t->operands)
6569 {
c0f3af97
L
6570 case 5:
6571 if (!operand_type_match (overlap4, i.types[4])
dc821c5f 6572 || !operand_type_register_match (i.types[3],
c0f3af97 6573 operand_types[3],
c0f3af97
L
6574 i.types[4],
6575 operand_types[4]))
6576 continue;
1a0670f3 6577 /* Fall through. */
f48ff2ae 6578 case 4:
40fb9820 6579 if (!operand_type_match (overlap3, i.types[3])
e2195274
JB
6580 || ((check_register & 0xa) == 0xa
6581 && !operand_type_register_match (i.types[1],
f7768225
JB
6582 operand_types[1],
6583 i.types[3],
e2195274
JB
6584 operand_types[3]))
6585 || ((check_register & 0xc) == 0xc
6586 && !operand_type_register_match (i.types[2],
6587 operand_types[2],
6588 i.types[3],
6589 operand_types[3])))
f48ff2ae 6590 continue;
1a0670f3 6591 /* Fall through. */
f48ff2ae
L
6592 case 3:
6593 /* Here we make use of the fact that there are no
23e42951 6594 reverse match 3 operand instructions. */
40fb9820 6595 if (!operand_type_match (overlap2, i.types[2])
e2195274
JB
6596 || ((check_register & 5) == 5
6597 && !operand_type_register_match (i.types[0],
23e42951
JB
6598 operand_types[0],
6599 i.types[2],
e2195274
JB
6600 operand_types[2]))
6601 || ((check_register & 6) == 6
6602 && !operand_type_register_match (i.types[1],
6603 operand_types[1],
6604 i.types[2],
6605 operand_types[2])))
f48ff2ae
L
6606 continue;
6607 break;
6608 }
29b0f896 6609 }
f48ff2ae 6610 /* Found either forward/reverse 2, 3 or 4 operand match here:
29b0f896
AM
6611 slip through to break. */
6612 }
c0f3af97 6613
da4977e0
JB
6614 /* Check if vector operands are valid. */
6615 if (check_VecOperands (t))
6616 {
6617 specific_error = i.error;
6618 continue;
6619 }
6620
6621 /* Check if VEX/EVEX encoding requirements can be satisfied. */
6622 if (VEX_check_encoding (t))
5614d22c
JB
6623 {
6624 specific_error = i.error;
6625 continue;
6626 }
a683cc34 6627
29b0f896
AM
6628 /* We've found a match; break out of loop. */
6629 break;
6630 }
6631
6632 if (t == current_templates->end)
6633 {
6634 /* We found no match. */
a65babc9 6635 const char *err_msg;
5614d22c 6636 switch (specific_error ? specific_error : i.error)
a65babc9
L
6637 {
6638 default:
6639 abort ();
86e026a4 6640 case operand_size_mismatch:
a65babc9
L
6641 err_msg = _("operand size mismatch");
6642 break;
6643 case operand_type_mismatch:
6644 err_msg = _("operand type mismatch");
6645 break;
6646 case register_type_mismatch:
6647 err_msg = _("register type mismatch");
6648 break;
6649 case number_of_operands_mismatch:
6650 err_msg = _("number of operands mismatch");
6651 break;
6652 case invalid_instruction_suffix:
6653 err_msg = _("invalid instruction suffix");
6654 break;
6655 case bad_imm4:
4a2608e3 6656 err_msg = _("constant doesn't fit in 4 bits");
a65babc9 6657 break;
a65babc9
L
6658 case unsupported_with_intel_mnemonic:
6659 err_msg = _("unsupported with Intel mnemonic");
6660 break;
6661 case unsupported_syntax:
6662 err_msg = _("unsupported syntax");
6663 break;
6664 case unsupported:
35262a23 6665 as_bad (_("unsupported instruction `%s'"),
10efe3f6
L
6666 current_templates->start->name);
6667 return NULL;
260cd341
LC
6668 case invalid_sib_address:
6669 err_msg = _("invalid SIB address");
6670 break;
6c30d220
L
6671 case invalid_vsib_address:
6672 err_msg = _("invalid VSIB address");
6673 break;
7bab8ab5
JB
6674 case invalid_vector_register_set:
6675 err_msg = _("mask, index, and destination registers must be distinct");
6676 break;
260cd341
LC
6677 case invalid_tmm_register_set:
6678 err_msg = _("all tmm registers must be distinct");
6679 break;
6c30d220
L
6680 case unsupported_vector_index_register:
6681 err_msg = _("unsupported vector index register");
6682 break;
43234a1e
L
6683 case unsupported_broadcast:
6684 err_msg = _("unsupported broadcast");
6685 break;
43234a1e
L
6686 case broadcast_needed:
6687 err_msg = _("broadcast is needed for operand of such type");
6688 break;
6689 case unsupported_masking:
6690 err_msg = _("unsupported masking");
6691 break;
6692 case mask_not_on_destination:
6693 err_msg = _("mask not on destination operand");
6694 break;
6695 case no_default_mask:
6696 err_msg = _("default mask isn't allowed");
6697 break;
6698 case unsupported_rc_sae:
6699 err_msg = _("unsupported static rounding/sae");
6700 break;
6701 case rc_sae_operand_not_last_imm:
6702 if (intel_syntax)
6703 err_msg = _("RC/SAE operand must precede immediate operands");
6704 else
6705 err_msg = _("RC/SAE operand must follow immediate operands");
6706 break;
6707 case invalid_register_operand:
6708 err_msg = _("invalid register operand");
6709 break;
a65babc9
L
6710 }
6711 as_bad (_("%s for `%s'"), err_msg,
891edac4 6712 current_templates->start->name);
fa99fab2 6713 return NULL;
29b0f896 6714 }
252b5132 6715
29b0f896
AM
6716 if (!quiet_warnings)
6717 {
6718 if (!intel_syntax
0cfa3eb3 6719 && (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE)))
6f2f06be 6720 as_warn (_("indirect %s without `*'"), t->name);
29b0f896 6721
40fb9820 6722 if (t->opcode_modifier.isprefix
3cd7f3e3 6723 && t->opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
6724 {
6725 /* Warn them that a data or address size prefix doesn't
6726 affect assembly of the next line of code. */
6727 as_warn (_("stand-alone `%s' prefix"), t->name);
6728 }
6729 }
6730
6731 /* Copy the template we found. */
9a182d04 6732 install_template (t);
539e75ad
L
6733
6734 if (addr_prefix_disp != -1)
6735 i.tm.operand_types[addr_prefix_disp]
6736 = operand_types[addr_prefix_disp];
6737
29b0f896
AM
6738 if (found_reverse_match)
6739 {
dfd69174
JB
6740 /* If we found a reverse match we must alter the opcode direction
6741 bit and clear/flip the regmem modifier one. found_reverse_match
6742 holds bits to change (different for int & float insns). */
29b0f896
AM
6743
6744 i.tm.base_opcode ^= found_reverse_match;
6745
f5eb1d70
JB
6746 i.tm.operand_types[0] = operand_types[i.operands - 1];
6747 i.tm.operand_types[i.operands - 1] = operand_types[0];
dfd69174
JB
6748
6749 /* Certain SIMD insns have their load forms specified in the opcode
6750 table, and hence we need to _set_ RegMem instead of clearing it.
6751 We need to avoid setting the bit though on insns like KMOVW. */
6752 i.tm.opcode_modifier.regmem
6753 = i.tm.opcode_modifier.modrm && i.tm.opcode_modifier.d
6754 && i.tm.operands > 2U - i.tm.opcode_modifier.sse2avx
6755 && !i.tm.opcode_modifier.regmem;
29b0f896
AM
6756 }
6757
fa99fab2 6758 return t;
29b0f896
AM
6759}
6760
6761static int
e3bb37b5 6762check_string (void)
29b0f896 6763{
51c8edf6
JB
6764 unsigned int es_op = i.tm.opcode_modifier.isstring - IS_STRING_ES_OP0;
6765 unsigned int op = i.tm.operand_types[0].bitfield.baseindex ? es_op : 0;
8dc0818e 6766
5e042380 6767 if (i.seg[op] != NULL && i.seg[op] != reg_es)
29b0f896 6768 {
51c8edf6
JB
6769 as_bad (_("`%s' operand %u must use `%ses' segment"),
6770 i.tm.name,
6771 intel_syntax ? i.tm.operands - es_op : es_op + 1,
6772 register_prefix);
6773 return 0;
29b0f896 6774 }
51c8edf6
JB
6775
6776 /* There's only ever one segment override allowed per instruction.
6777 This instruction possibly has a legal segment override on the
6778 second operand, so copy the segment to where non-string
6779 instructions store it, allowing common code. */
6780 i.seg[op] = i.seg[1];
6781
29b0f896
AM
6782 return 1;
6783}
6784
6785static int
543613e9 6786process_suffix (void)
29b0f896 6787{
5b7c81bd 6788 bool is_crc32 = false, is_movx = false;
8b65b895 6789
29b0f896
AM
6790 /* If matched instruction specifies an explicit instruction mnemonic
6791 suffix, use it. */
673fe0f0 6792 if (i.tm.opcode_modifier.size == SIZE16)
40fb9820 6793 i.suffix = WORD_MNEM_SUFFIX;
673fe0f0 6794 else if (i.tm.opcode_modifier.size == SIZE32)
40fb9820 6795 i.suffix = LONG_MNEM_SUFFIX;
673fe0f0 6796 else if (i.tm.opcode_modifier.size == SIZE64)
40fb9820 6797 i.suffix = QWORD_MNEM_SUFFIX;
13e600d0 6798 else if (i.reg_operands
c8f8eebc
JB
6799 && (i.operands > 1 || i.types[0].bitfield.class == Reg)
6800 && !i.tm.opcode_modifier.addrprefixopreg)
29b0f896 6801 {
65fca059 6802 unsigned int numop = i.operands;
389d00a5
JB
6803
6804 /* MOVSX/MOVZX */
6805 is_movx = (i.tm.opcode_modifier.opcodespace == SPACE_0F
6806 && (i.tm.base_opcode | 8) == 0xbe)
6807 || (i.tm.opcode_modifier.opcodespace == SPACE_BASE
6808 && i.tm.base_opcode == 0x63
6809 && i.tm.cpu_flags.bitfield.cpu64);
6810
8b65b895 6811 /* CRC32 */
389d00a5
JB
6812 is_crc32 = (i.tm.base_opcode == 0xf0
6813 && i.tm.opcode_modifier.opcodespace == SPACE_0F38
8b65b895 6814 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2);
65fca059
JB
6815
6816 /* movsx/movzx want only their source operand considered here, for the
6817 ambiguity checking below. The suffix will be replaced afterwards
6818 to represent the destination (register). */
389d00a5 6819 if (is_movx && (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63))
65fca059
JB
6820 --i.operands;
6821
643bb870 6822 /* crc32 needs REX.W set regardless of suffix / source operand size. */
8b65b895 6823 if (is_crc32 && i.tm.operand_types[1].bitfield.qword)
643bb870
JB
6824 i.rex |= REX_W;
6825
29b0f896 6826 /* If there's no instruction mnemonic suffix we try to invent one
13e600d0 6827 based on GPR operands. */
29b0f896
AM
6828 if (!i.suffix)
6829 {
6830 /* We take i.suffix from the last register operand specified,
6831 Destination register type is more significant than source
381d071f
L
6832 register type. crc32 in SSE4.2 prefers source register
6833 type. */
8b65b895 6834 unsigned int op = is_crc32 ? 1 : i.operands;
20592a94 6835
1a035124
JB
6836 while (op--)
6837 if (i.tm.operand_types[op].bitfield.instance == InstanceNone
6838 || i.tm.operand_types[op].bitfield.instance == Accum)
6839 {
6840 if (i.types[op].bitfield.class != Reg)
6841 continue;
6842 if (i.types[op].bitfield.byte)
6843 i.suffix = BYTE_MNEM_SUFFIX;
6844 else if (i.types[op].bitfield.word)
6845 i.suffix = WORD_MNEM_SUFFIX;
6846 else if (i.types[op].bitfield.dword)
6847 i.suffix = LONG_MNEM_SUFFIX;
6848 else if (i.types[op].bitfield.qword)
6849 i.suffix = QWORD_MNEM_SUFFIX;
6850 else
6851 continue;
6852 break;
6853 }
65fca059
JB
6854
6855 /* As an exception, movsx/movzx silently default to a byte source
6856 in AT&T mode. */
389d00a5 6857 if (is_movx && i.tm.opcode_modifier.w && !i.suffix && !intel_syntax)
65fca059 6858 i.suffix = BYTE_MNEM_SUFFIX;
29b0f896
AM
6859 }
6860 else if (i.suffix == BYTE_MNEM_SUFFIX)
6861 {
2eb952a4 6862 if (intel_syntax
3cd7f3e3 6863 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
2eb952a4
L
6864 && i.tm.opcode_modifier.no_bsuf)
6865 i.suffix = 0;
6866 else if (!check_byte_reg ())
29b0f896
AM
6867 return 0;
6868 }
6869 else if (i.suffix == LONG_MNEM_SUFFIX)
6870 {
2eb952a4 6871 if (intel_syntax
3cd7f3e3 6872 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
9f123b91
JB
6873 && i.tm.opcode_modifier.no_lsuf
6874 && !i.tm.opcode_modifier.todword
6875 && !i.tm.opcode_modifier.toqword)
2eb952a4
L
6876 i.suffix = 0;
6877 else if (!check_long_reg ())
29b0f896
AM
6878 return 0;
6879 }
6880 else if (i.suffix == QWORD_MNEM_SUFFIX)
6881 {
955e1e6a 6882 if (intel_syntax
3cd7f3e3 6883 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
9f123b91
JB
6884 && i.tm.opcode_modifier.no_qsuf
6885 && !i.tm.opcode_modifier.todword
6886 && !i.tm.opcode_modifier.toqword)
955e1e6a
L
6887 i.suffix = 0;
6888 else if (!check_qword_reg ())
29b0f896
AM
6889 return 0;
6890 }
6891 else if (i.suffix == WORD_MNEM_SUFFIX)
6892 {
2eb952a4 6893 if (intel_syntax
3cd7f3e3 6894 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
2eb952a4
L
6895 && i.tm.opcode_modifier.no_wsuf)
6896 i.suffix = 0;
6897 else if (!check_word_reg ())
29b0f896
AM
6898 return 0;
6899 }
3cd7f3e3
L
6900 else if (intel_syntax
6901 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
6902 /* Do nothing if the instruction is going to ignore the prefix. */
6903 ;
6904 else
6905 abort ();
65fca059
JB
6906
6907 /* Undo the movsx/movzx change done above. */
6908 i.operands = numop;
29b0f896 6909 }
3cd7f3e3
L
6910 else if (i.tm.opcode_modifier.mnemonicsize == DEFAULTSIZE
6911 && !i.suffix)
29b0f896 6912 {
13e600d0
JB
6913 i.suffix = stackop_size;
6914 if (stackop_size == LONG_MNEM_SUFFIX)
06f74c5c
L
6915 {
6916 /* stackop_size is set to LONG_MNEM_SUFFIX for the
6917 .code16gcc directive to support 16-bit mode with
6918 32-bit address. For IRET without a suffix, generate
6919 16-bit IRET (opcode 0xcf) to return from an interrupt
6920 handler. */
13e600d0
JB
6921 if (i.tm.base_opcode == 0xcf)
6922 {
6923 i.suffix = WORD_MNEM_SUFFIX;
6924 as_warn (_("generating 16-bit `iret' for .code16gcc directive"));
6925 }
6926 /* Warn about changed behavior for segment register push/pop. */
6927 else if ((i.tm.base_opcode | 1) == 0x07)
6928 as_warn (_("generating 32-bit `%s', unlike earlier gas versions"),
6929 i.tm.name);
06f74c5c 6930 }
29b0f896 6931 }
c006a730 6932 else if (!i.suffix
0cfa3eb3
JB
6933 && (i.tm.opcode_modifier.jump == JUMP_ABSOLUTE
6934 || i.tm.opcode_modifier.jump == JUMP_BYTE
6935 || i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT
389d00a5
JB
6936 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
6937 && i.tm.base_opcode == 0x01 /* [ls][gi]dt */
64e74474 6938 && i.tm.extension_opcode <= 3)))
9306ca4a
JB
6939 {
6940 switch (flag_code)
6941 {
6942 case CODE_64BIT:
40fb9820 6943 if (!i.tm.opcode_modifier.no_qsuf)
9306ca4a 6944 {
828c2a25
JB
6945 if (i.tm.opcode_modifier.jump == JUMP_BYTE
6946 || i.tm.opcode_modifier.no_lsuf)
6947 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a
JB
6948 break;
6949 }
1a0670f3 6950 /* Fall through. */
9306ca4a 6951 case CODE_32BIT:
40fb9820 6952 if (!i.tm.opcode_modifier.no_lsuf)
9306ca4a
JB
6953 i.suffix = LONG_MNEM_SUFFIX;
6954 break;
6955 case CODE_16BIT:
40fb9820 6956 if (!i.tm.opcode_modifier.no_wsuf)
9306ca4a
JB
6957 i.suffix = WORD_MNEM_SUFFIX;
6958 break;
6959 }
6960 }
252b5132 6961
c006a730 6962 if (!i.suffix
3cd7f3e3 6963 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8
JB
6964 /* Also cover lret/retf/iret in 64-bit mode. */
6965 || (flag_code == CODE_64BIT
6966 && !i.tm.opcode_modifier.no_lsuf
6967 && !i.tm.opcode_modifier.no_qsuf))
3cd7f3e3 6968 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
8bbb3ad8
JB
6969 /* Explicit sizing prefixes are assumed to disambiguate insns. */
6970 && !i.prefix[DATA_PREFIX] && !(i.prefix[REX_PREFIX] & REX_W)
62b3f548
JB
6971 /* Accept FLDENV et al without suffix. */
6972 && (i.tm.opcode_modifier.no_ssuf || i.tm.opcode_modifier.floatmf))
29b0f896 6973 {
6c0946d0 6974 unsigned int suffixes, evex = 0;
c006a730
JB
6975
6976 suffixes = !i.tm.opcode_modifier.no_bsuf;
6977 if (!i.tm.opcode_modifier.no_wsuf)
6978 suffixes |= 1 << 1;
6979 if (!i.tm.opcode_modifier.no_lsuf)
6980 suffixes |= 1 << 2;
6981 if (!i.tm.opcode_modifier.no_ldsuf)
6982 suffixes |= 1 << 3;
6983 if (!i.tm.opcode_modifier.no_ssuf)
6984 suffixes |= 1 << 4;
6985 if (flag_code == CODE_64BIT && !i.tm.opcode_modifier.no_qsuf)
6986 suffixes |= 1 << 5;
6987
6c0946d0
JB
6988 /* For [XYZ]MMWORD operands inspect operand sizes. While generally
6989 also suitable for AT&T syntax mode, it was requested that this be
6990 restricted to just Intel syntax. */
5273a3cd 6991 if (intel_syntax && is_any_vex_encoding (&i.tm) && !i.broadcast.type)
6c0946d0 6992 {
b9915cbc 6993 unsigned int op;
6c0946d0 6994
b9915cbc 6995 for (op = 0; op < i.tm.operands; ++op)
6c0946d0 6996 {
b9915cbc
JB
6997 if (is_evex_encoding (&i.tm)
6998 && !cpu_arch_flags.bitfield.cpuavx512vl)
6c0946d0 6999 {
b9915cbc
JB
7000 if (i.tm.operand_types[op].bitfield.ymmword)
7001 i.tm.operand_types[op].bitfield.xmmword = 0;
7002 if (i.tm.operand_types[op].bitfield.zmmword)
7003 i.tm.operand_types[op].bitfield.ymmword = 0;
7004 if (!i.tm.opcode_modifier.evex
7005 || i.tm.opcode_modifier.evex == EVEXDYN)
7006 i.tm.opcode_modifier.evex = EVEX512;
7007 }
6c0946d0 7008
b9915cbc
JB
7009 if (i.tm.operand_types[op].bitfield.xmmword
7010 + i.tm.operand_types[op].bitfield.ymmword
7011 + i.tm.operand_types[op].bitfield.zmmword < 2)
7012 continue;
6c0946d0 7013
b9915cbc
JB
7014 /* Any properly sized operand disambiguates the insn. */
7015 if (i.types[op].bitfield.xmmword
7016 || i.types[op].bitfield.ymmword
7017 || i.types[op].bitfield.zmmword)
7018 {
7019 suffixes &= ~(7 << 6);
7020 evex = 0;
7021 break;
7022 }
6c0946d0 7023
b9915cbc
JB
7024 if ((i.flags[op] & Operand_Mem)
7025 && i.tm.operand_types[op].bitfield.unspecified)
7026 {
7027 if (i.tm.operand_types[op].bitfield.xmmword)
7028 suffixes |= 1 << 6;
7029 if (i.tm.operand_types[op].bitfield.ymmword)
7030 suffixes |= 1 << 7;
7031 if (i.tm.operand_types[op].bitfield.zmmword)
7032 suffixes |= 1 << 8;
7033 if (is_evex_encoding (&i.tm))
7034 evex = EVEX512;
6c0946d0
JB
7035 }
7036 }
7037 }
7038
7039 /* Are multiple suffixes / operand sizes allowed? */
c006a730 7040 if (suffixes & (suffixes - 1))
9306ca4a 7041 {
873494c8 7042 if (intel_syntax
3cd7f3e3 7043 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8 7044 || operand_check == check_error))
9306ca4a 7045 {
c006a730 7046 as_bad (_("ambiguous operand size for `%s'"), i.tm.name);
9306ca4a
JB
7047 return 0;
7048 }
c006a730 7049 if (operand_check == check_error)
9306ca4a 7050 {
c006a730
JB
7051 as_bad (_("no instruction mnemonic suffix given and "
7052 "no register operands; can't size `%s'"), i.tm.name);
9306ca4a
JB
7053 return 0;
7054 }
c006a730 7055 if (operand_check == check_warning)
873494c8
JB
7056 as_warn (_("%s; using default for `%s'"),
7057 intel_syntax
7058 ? _("ambiguous operand size")
7059 : _("no instruction mnemonic suffix given and "
7060 "no register operands"),
7061 i.tm.name);
c006a730
JB
7062
7063 if (i.tm.opcode_modifier.floatmf)
7064 i.suffix = SHORT_MNEM_SUFFIX;
389d00a5 7065 else if (is_movx)
65fca059 7066 /* handled below */;
6c0946d0
JB
7067 else if (evex)
7068 i.tm.opcode_modifier.evex = evex;
c006a730
JB
7069 else if (flag_code == CODE_16BIT)
7070 i.suffix = WORD_MNEM_SUFFIX;
1a035124 7071 else if (!i.tm.opcode_modifier.no_lsuf)
c006a730 7072 i.suffix = LONG_MNEM_SUFFIX;
1a035124
JB
7073 else
7074 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a 7075 }
29b0f896 7076 }
252b5132 7077
389d00a5 7078 if (is_movx)
65fca059
JB
7079 {
7080 /* In Intel syntax, movsx/movzx must have a "suffix" (checked above).
7081 In AT&T syntax, if there is no suffix (warned about above), the default
7082 will be byte extension. */
7083 if (i.tm.opcode_modifier.w && i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
7084 i.tm.base_opcode |= 1;
7085
7086 /* For further processing, the suffix should represent the destination
7087 (register). This is already the case when one was used with
7088 mov[sz][bw]*, but we need to replace it for mov[sz]x, or if there was
7089 no suffix to begin with. */
7090 if (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63 || !i.suffix)
7091 {
7092 if (i.types[1].bitfield.word)
7093 i.suffix = WORD_MNEM_SUFFIX;
7094 else if (i.types[1].bitfield.qword)
7095 i.suffix = QWORD_MNEM_SUFFIX;
7096 else
7097 i.suffix = LONG_MNEM_SUFFIX;
7098
7099 i.tm.opcode_modifier.w = 0;
7100 }
7101 }
7102
50128d0c
JB
7103 if (!i.tm.opcode_modifier.modrm && i.reg_operands && i.tm.operands < 3)
7104 i.short_form = (i.tm.operand_types[0].bitfield.class == Reg)
7105 != (i.tm.operand_types[1].bitfield.class == Reg);
7106
d2224064
JB
7107 /* Change the opcode based on the operand size given by i.suffix. */
7108 switch (i.suffix)
29b0f896 7109 {
d2224064
JB
7110 /* Size floating point instruction. */
7111 case LONG_MNEM_SUFFIX:
7112 if (i.tm.opcode_modifier.floatmf)
7113 {
7114 i.tm.base_opcode ^= 4;
7115 break;
7116 }
7117 /* fall through */
7118 case WORD_MNEM_SUFFIX:
7119 case QWORD_MNEM_SUFFIX:
29b0f896 7120 /* It's not a byte, select word/dword operation. */
40fb9820 7121 if (i.tm.opcode_modifier.w)
29b0f896 7122 {
50128d0c 7123 if (i.short_form)
29b0f896
AM
7124 i.tm.base_opcode |= 8;
7125 else
7126 i.tm.base_opcode |= 1;
7127 }
d2224064
JB
7128 /* fall through */
7129 case SHORT_MNEM_SUFFIX:
29b0f896
AM
7130 /* Now select between word & dword operations via the operand
7131 size prefix, except for instructions that will ignore this
7132 prefix anyway. */
c8f8eebc 7133 if (i.suffix != QWORD_MNEM_SUFFIX
3cd7f3e3 7134 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
c8f8eebc
JB
7135 && !i.tm.opcode_modifier.floatmf
7136 && !is_any_vex_encoding (&i.tm)
7137 && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
7138 || (flag_code == CODE_64BIT
7139 && i.tm.opcode_modifier.jump == JUMP_BYTE)))
24eab124
AM
7140 {
7141 unsigned int prefix = DATA_PREFIX_OPCODE;
543613e9 7142
0cfa3eb3 7143 if (i.tm.opcode_modifier.jump == JUMP_BYTE) /* jcxz, loop */
29b0f896 7144 prefix = ADDR_PREFIX_OPCODE;
252b5132 7145
29b0f896
AM
7146 if (!add_prefix (prefix))
7147 return 0;
24eab124 7148 }
252b5132 7149
29b0f896
AM
7150 /* Set mode64 for an operand. */
7151 if (i.suffix == QWORD_MNEM_SUFFIX
9146926a 7152 && flag_code == CODE_64BIT
d2224064 7153 && !i.tm.opcode_modifier.norex64
4ed21b58 7154 && !i.tm.opcode_modifier.vexw
46e883c5 7155 /* Special case for xchg %rax,%rax. It is NOP and doesn't
d2224064
JB
7156 need rex64. */
7157 && ! (i.operands == 2
7158 && i.tm.base_opcode == 0x90
7159 && i.tm.extension_opcode == None
75e5731b
JB
7160 && i.types[0].bitfield.instance == Accum
7161 && i.types[0].bitfield.qword
7162 && i.types[1].bitfield.instance == Accum
7163 && i.types[1].bitfield.qword))
d2224064 7164 i.rex |= REX_W;
3e73aa7c 7165
d2224064 7166 break;
8bbb3ad8
JB
7167
7168 case 0:
f9a6a8f0 7169 /* Select word/dword/qword operation with explicit data sizing prefix
8bbb3ad8
JB
7170 when there are no suitable register operands. */
7171 if (i.tm.opcode_modifier.w
7172 && (i.prefix[DATA_PREFIX] || (i.prefix[REX_PREFIX] & REX_W))
7173 && (!i.reg_operands
7174 || (i.reg_operands == 1
7175 /* ShiftCount */
7176 && (i.tm.operand_types[0].bitfield.instance == RegC
7177 /* InOutPortReg */
7178 || i.tm.operand_types[0].bitfield.instance == RegD
7179 || i.tm.operand_types[1].bitfield.instance == RegD
7180 /* CRC32 */
8b65b895 7181 || is_crc32))))
8bbb3ad8
JB
7182 i.tm.base_opcode |= 1;
7183 break;
29b0f896 7184 }
7ecd2f8b 7185
c8f8eebc 7186 if (i.tm.opcode_modifier.addrprefixopreg)
c0a30a9f 7187 {
c8f8eebc
JB
7188 gas_assert (!i.suffix);
7189 gas_assert (i.reg_operands);
c0a30a9f 7190
c8f8eebc
JB
7191 if (i.tm.operand_types[0].bitfield.instance == Accum
7192 || i.operands == 1)
7193 {
7194 /* The address size override prefix changes the size of the
7195 first operand. */
7196 if (flag_code == CODE_64BIT
7197 && i.op[0].regs->reg_type.bitfield.word)
7198 {
7199 as_bad (_("16-bit addressing unavailable for `%s'"),
7200 i.tm.name);
7201 return 0;
7202 }
7203
7204 if ((flag_code == CODE_32BIT
7205 ? i.op[0].regs->reg_type.bitfield.word
7206 : i.op[0].regs->reg_type.bitfield.dword)
7207 && !add_prefix (ADDR_PREFIX_OPCODE))
7208 return 0;
7209 }
c0a30a9f
L
7210 else
7211 {
c8f8eebc
JB
7212 /* Check invalid register operand when the address size override
7213 prefix changes the size of register operands. */
7214 unsigned int op;
7215 enum { need_word, need_dword, need_qword } need;
7216
27f13469 7217 /* Check the register operand for the address size prefix if
b3a3496f 7218 the memory operand has no real registers, like symbol, DISP
829f3fe1 7219 or bogus (x32-only) symbol(%rip) when symbol(%eip) is meant. */
27f13469
L
7220 if (i.mem_operands == 1
7221 && i.reg_operands == 1
7222 && i.operands == 2
27f13469 7223 && i.types[1].bitfield.class == Reg
b3a3496f
L
7224 && (flag_code == CODE_32BIT
7225 ? i.op[1].regs->reg_type.bitfield.word
7226 : i.op[1].regs->reg_type.bitfield.dword)
7227 && ((i.base_reg == NULL && i.index_reg == NULL)
829f3fe1
JB
7228#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
7229 || (x86_elf_abi == X86_64_X32_ABI
7230 && i.base_reg
b3a3496f
L
7231 && i.base_reg->reg_num == RegIP
7232 && i.base_reg->reg_type.bitfield.qword))
829f3fe1
JB
7233#else
7234 || 0)
7235#endif
27f13469
L
7236 && !add_prefix (ADDR_PREFIX_OPCODE))
7237 return 0;
7238
c8f8eebc
JB
7239 if (flag_code == CODE_32BIT)
7240 need = i.prefix[ADDR_PREFIX] ? need_word : need_dword;
7241 else if (i.prefix[ADDR_PREFIX])
c0a30a9f
L
7242 need = need_dword;
7243 else
7244 need = flag_code == CODE_64BIT ? need_qword : need_word;
c0a30a9f 7245
c8f8eebc
JB
7246 for (op = 0; op < i.operands; op++)
7247 {
7248 if (i.types[op].bitfield.class != Reg)
7249 continue;
7250
7251 switch (need)
7252 {
7253 case need_word:
7254 if (i.op[op].regs->reg_type.bitfield.word)
7255 continue;
7256 break;
7257 case need_dword:
7258 if (i.op[op].regs->reg_type.bitfield.dword)
7259 continue;
7260 break;
7261 case need_qword:
7262 if (i.op[op].regs->reg_type.bitfield.qword)
7263 continue;
7264 break;
7265 }
7266
7267 as_bad (_("invalid register operand size for `%s'"),
7268 i.tm.name);
7269 return 0;
7270 }
7271 }
c0a30a9f
L
7272 }
7273
29b0f896
AM
7274 return 1;
7275}
3e73aa7c 7276
29b0f896 7277static int
543613e9 7278check_byte_reg (void)
29b0f896
AM
7279{
7280 int op;
543613e9 7281
29b0f896
AM
7282 for (op = i.operands; --op >= 0;)
7283 {
dc821c5f 7284 /* Skip non-register operands. */
bab6aec1 7285 if (i.types[op].bitfield.class != Reg)
dc821c5f
JB
7286 continue;
7287
29b0f896
AM
7288 /* If this is an eight bit register, it's OK. If it's the 16 or
7289 32 bit version of an eight bit register, we will just use the
7290 low portion, and that's OK too. */
dc821c5f 7291 if (i.types[op].bitfield.byte)
29b0f896
AM
7292 continue;
7293
5a819eb9 7294 /* I/O port address operands are OK too. */
75e5731b
JB
7295 if (i.tm.operand_types[op].bitfield.instance == RegD
7296 && i.tm.operand_types[op].bitfield.word)
5a819eb9
JB
7297 continue;
7298
9706160a 7299 /* crc32 only wants its source operand checked here. */
389d00a5
JB
7300 if (i.tm.base_opcode == 0xf0
7301 && i.tm.opcode_modifier.opcodespace == SPACE_0F38
8b65b895
L
7302 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2
7303 && op != 0)
9344ff29
L
7304 continue;
7305
29b0f896 7306 /* Any other register is bad. */
73c76375
JB
7307 as_bad (_("`%s%s' not allowed with `%s%c'"),
7308 register_prefix, i.op[op].regs->reg_name,
7309 i.tm.name, i.suffix);
7310 return 0;
29b0f896
AM
7311 }
7312 return 1;
7313}
7314
7315static int
e3bb37b5 7316check_long_reg (void)
29b0f896
AM
7317{
7318 int op;
7319
7320 for (op = i.operands; --op >= 0;)
dc821c5f 7321 /* Skip non-register operands. */
bab6aec1 7322 if (i.types[op].bitfield.class != Reg)
dc821c5f 7323 continue;
29b0f896
AM
7324 /* Reject eight bit registers, except where the template requires
7325 them. (eg. movzb) */
dc821c5f 7326 else if (i.types[op].bitfield.byte
bab6aec1 7327 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7328 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7329 && (i.tm.operand_types[op].bitfield.word
7330 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7331 {
a540244d
L
7332 as_bad (_("`%s%s' not allowed with `%s%c'"),
7333 register_prefix,
29b0f896
AM
7334 i.op[op].regs->reg_name,
7335 i.tm.name,
7336 i.suffix);
7337 return 0;
7338 }
be4c5e58
L
7339 /* Error if the e prefix on a general reg is missing. */
7340 else if (i.types[op].bitfield.word
bab6aec1 7341 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7342 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7343 && i.tm.operand_types[op].bitfield.dword)
29b0f896 7344 {
be4c5e58
L
7345 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7346 register_prefix, i.op[op].regs->reg_name,
7347 i.suffix);
7348 return 0;
252b5132 7349 }
e4630f71 7350 /* Warn if the r prefix on a general reg is present. */
dc821c5f 7351 else if (i.types[op].bitfield.qword
bab6aec1 7352 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7353 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7354 && i.tm.operand_types[op].bitfield.dword)
252b5132 7355 {
34828aad 7356 if (intel_syntax
65fca059 7357 && i.tm.opcode_modifier.toqword
3528c362 7358 && i.types[0].bitfield.class != RegSIMD)
34828aad 7359 {
ca61edf2 7360 /* Convert to QWORD. We want REX byte. */
34828aad
L
7361 i.suffix = QWORD_MNEM_SUFFIX;
7362 }
7363 else
7364 {
2b5d6a91 7365 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
34828aad
L
7366 register_prefix, i.op[op].regs->reg_name,
7367 i.suffix);
7368 return 0;
7369 }
29b0f896
AM
7370 }
7371 return 1;
7372}
252b5132 7373
29b0f896 7374static int
e3bb37b5 7375check_qword_reg (void)
29b0f896
AM
7376{
7377 int op;
252b5132 7378
29b0f896 7379 for (op = i.operands; --op >= 0; )
dc821c5f 7380 /* Skip non-register operands. */
bab6aec1 7381 if (i.types[op].bitfield.class != Reg)
dc821c5f 7382 continue;
29b0f896
AM
7383 /* Reject eight bit registers, except where the template requires
7384 them. (eg. movzb) */
dc821c5f 7385 else if (i.types[op].bitfield.byte
bab6aec1 7386 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7387 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7388 && (i.tm.operand_types[op].bitfield.word
7389 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7390 {
a540244d
L
7391 as_bad (_("`%s%s' not allowed with `%s%c'"),
7392 register_prefix,
29b0f896
AM
7393 i.op[op].regs->reg_name,
7394 i.tm.name,
7395 i.suffix);
7396 return 0;
7397 }
e4630f71 7398 /* Warn if the r prefix on a general reg is missing. */
dc821c5f
JB
7399 else if ((i.types[op].bitfield.word
7400 || i.types[op].bitfield.dword)
bab6aec1 7401 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7402 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7403 && i.tm.operand_types[op].bitfield.qword)
29b0f896
AM
7404 {
7405 /* Prohibit these changes in the 64bit mode, since the
7406 lowering is more complicated. */
34828aad 7407 if (intel_syntax
ca61edf2 7408 && i.tm.opcode_modifier.todword
3528c362 7409 && i.types[0].bitfield.class != RegSIMD)
34828aad 7410 {
ca61edf2 7411 /* Convert to DWORD. We don't want REX byte. */
34828aad
L
7412 i.suffix = LONG_MNEM_SUFFIX;
7413 }
7414 else
7415 {
2b5d6a91 7416 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
34828aad
L
7417 register_prefix, i.op[op].regs->reg_name,
7418 i.suffix);
7419 return 0;
7420 }
252b5132 7421 }
29b0f896
AM
7422 return 1;
7423}
252b5132 7424
29b0f896 7425static int
e3bb37b5 7426check_word_reg (void)
29b0f896
AM
7427{
7428 int op;
7429 for (op = i.operands; --op >= 0;)
dc821c5f 7430 /* Skip non-register operands. */
bab6aec1 7431 if (i.types[op].bitfield.class != Reg)
dc821c5f 7432 continue;
29b0f896
AM
7433 /* Reject eight bit registers, except where the template requires
7434 them. (eg. movzb) */
dc821c5f 7435 else if (i.types[op].bitfield.byte
bab6aec1 7436 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7437 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7438 && (i.tm.operand_types[op].bitfield.word
7439 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7440 {
a540244d
L
7441 as_bad (_("`%s%s' not allowed with `%s%c'"),
7442 register_prefix,
29b0f896
AM
7443 i.op[op].regs->reg_name,
7444 i.tm.name,
7445 i.suffix);
7446 return 0;
7447 }
9706160a
JB
7448 /* Error if the e or r prefix on a general reg is present. */
7449 else if ((i.types[op].bitfield.dword
dc821c5f 7450 || i.types[op].bitfield.qword)
bab6aec1 7451 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7452 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7453 && i.tm.operand_types[op].bitfield.word)
252b5132 7454 {
9706160a
JB
7455 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7456 register_prefix, i.op[op].regs->reg_name,
7457 i.suffix);
7458 return 0;
29b0f896
AM
7459 }
7460 return 1;
7461}
252b5132 7462
29b0f896 7463static int
40fb9820 7464update_imm (unsigned int j)
29b0f896 7465{
bc0844ae 7466 i386_operand_type overlap = i.types[j];
40fb9820
L
7467 if ((overlap.bitfield.imm8
7468 || overlap.bitfield.imm8s
7469 || overlap.bitfield.imm16
7470 || overlap.bitfield.imm32
7471 || overlap.bitfield.imm32s
7472 || overlap.bitfield.imm64)
0dfbf9d7
L
7473 && !operand_type_equal (&overlap, &imm8)
7474 && !operand_type_equal (&overlap, &imm8s)
7475 && !operand_type_equal (&overlap, &imm16)
7476 && !operand_type_equal (&overlap, &imm32)
7477 && !operand_type_equal (&overlap, &imm32s)
7478 && !operand_type_equal (&overlap, &imm64))
29b0f896
AM
7479 {
7480 if (i.suffix)
7481 {
40fb9820
L
7482 i386_operand_type temp;
7483
0dfbf9d7 7484 operand_type_set (&temp, 0);
7ab9ffdd 7485 if (i.suffix == BYTE_MNEM_SUFFIX)
40fb9820
L
7486 {
7487 temp.bitfield.imm8 = overlap.bitfield.imm8;
7488 temp.bitfield.imm8s = overlap.bitfield.imm8s;
7489 }
7490 else if (i.suffix == WORD_MNEM_SUFFIX)
7491 temp.bitfield.imm16 = overlap.bitfield.imm16;
7492 else if (i.suffix == QWORD_MNEM_SUFFIX)
7493 {
7494 temp.bitfield.imm64 = overlap.bitfield.imm64;
7495 temp.bitfield.imm32s = overlap.bitfield.imm32s;
7496 }
7497 else
7498 temp.bitfield.imm32 = overlap.bitfield.imm32;
7499 overlap = temp;
29b0f896 7500 }
0dfbf9d7
L
7501 else if (operand_type_equal (&overlap, &imm16_32_32s)
7502 || operand_type_equal (&overlap, &imm16_32)
7503 || operand_type_equal (&overlap, &imm16_32s))
29b0f896 7504 {
40fb9820 7505 if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
65da13b5 7506 overlap = imm16;
40fb9820 7507 else
65da13b5 7508 overlap = imm32s;
29b0f896 7509 }
8bbb3ad8
JB
7510 else if (i.prefix[REX_PREFIX] & REX_W)
7511 overlap = operand_type_and (overlap, imm32s);
7512 else if (i.prefix[DATA_PREFIX])
7513 overlap = operand_type_and (overlap,
7514 flag_code != CODE_16BIT ? imm16 : imm32);
0dfbf9d7
L
7515 if (!operand_type_equal (&overlap, &imm8)
7516 && !operand_type_equal (&overlap, &imm8s)
7517 && !operand_type_equal (&overlap, &imm16)
7518 && !operand_type_equal (&overlap, &imm32)
7519 && !operand_type_equal (&overlap, &imm32s)
7520 && !operand_type_equal (&overlap, &imm64))
29b0f896 7521 {
4eed87de
AM
7522 as_bad (_("no instruction mnemonic suffix given; "
7523 "can't determine immediate size"));
29b0f896
AM
7524 return 0;
7525 }
7526 }
40fb9820 7527 i.types[j] = overlap;
29b0f896 7528
40fb9820
L
7529 return 1;
7530}
7531
7532static int
7533finalize_imm (void)
7534{
bc0844ae 7535 unsigned int j, n;
29b0f896 7536
bc0844ae
L
7537 /* Update the first 2 immediate operands. */
7538 n = i.operands > 2 ? 2 : i.operands;
7539 if (n)
7540 {
7541 for (j = 0; j < n; j++)
7542 if (update_imm (j) == 0)
7543 return 0;
40fb9820 7544
bc0844ae
L
7545 /* The 3rd operand can't be immediate operand. */
7546 gas_assert (operand_type_check (i.types[2], imm) == 0);
7547 }
29b0f896
AM
7548
7549 return 1;
7550}
7551
7552static int
e3bb37b5 7553process_operands (void)
29b0f896
AM
7554{
7555 /* Default segment register this instruction will use for memory
7556 accesses. 0 means unknown. This is only for optimizing out
7557 unnecessary segment overrides. */
5e042380 7558 const reg_entry *default_seg = NULL;
29b0f896 7559
a5aeccd9
JB
7560 if (i.tm.opcode_modifier.sse2avx)
7561 {
7562 /* Legacy encoded insns allow explicit REX prefixes, so these prefixes
7563 need converting. */
7564 i.rex |= i.prefix[REX_PREFIX] & (REX_W | REX_R | REX_X | REX_B);
7565 i.prefix[REX_PREFIX] = 0;
7566 i.rex_encoding = 0;
7567 }
c423d21a
JB
7568 /* ImmExt should be processed after SSE2AVX. */
7569 else if (i.tm.opcode_modifier.immext)
7570 process_immext ();
a5aeccd9 7571
2426c15f 7572 if (i.tm.opcode_modifier.sse2avx && i.tm.opcode_modifier.vexvvvv)
29b0f896 7573 {
91d6fa6a
NC
7574 unsigned int dupl = i.operands;
7575 unsigned int dest = dupl - 1;
9fcfb3d7
L
7576 unsigned int j;
7577
c0f3af97 7578 /* The destination must be an xmm register. */
9c2799c2 7579 gas_assert (i.reg_operands
91d6fa6a 7580 && MAX_OPERANDS > dupl
7ab9ffdd 7581 && operand_type_equal (&i.types[dest], &regxmm));
c0f3af97 7582
75e5731b 7583 if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 7584 && i.tm.operand_types[0].bitfield.xmmword)
e2ec9d29 7585 {
8cd7925b 7586 if (i.tm.opcode_modifier.vexsources == VEX3SOURCES)
c0f3af97
L
7587 {
7588 /* Keep xmm0 for instructions with VEX prefix and 3
7589 sources. */
75e5731b 7590 i.tm.operand_types[0].bitfield.instance = InstanceNone;
3528c362 7591 i.tm.operand_types[0].bitfield.class = RegSIMD;
c0f3af97
L
7592 goto duplicate;
7593 }
e2ec9d29 7594 else
c0f3af97
L
7595 {
7596 /* We remove the first xmm0 and keep the number of
7597 operands unchanged, which in fact duplicates the
7598 destination. */
7599 for (j = 1; j < i.operands; j++)
7600 {
7601 i.op[j - 1] = i.op[j];
7602 i.types[j - 1] = i.types[j];
7603 i.tm.operand_types[j - 1] = i.tm.operand_types[j];
8dc0818e 7604 i.flags[j - 1] = i.flags[j];
c0f3af97
L
7605 }
7606 }
7607 }
7608 else if (i.tm.opcode_modifier.implicit1stxmm0)
7ab9ffdd 7609 {
91d6fa6a 7610 gas_assert ((MAX_OPERANDS - 1) > dupl
8cd7925b
L
7611 && (i.tm.opcode_modifier.vexsources
7612 == VEX3SOURCES));
c0f3af97
L
7613
7614 /* Add the implicit xmm0 for instructions with VEX prefix
7615 and 3 sources. */
7616 for (j = i.operands; j > 0; j--)
7617 {
7618 i.op[j] = i.op[j - 1];
7619 i.types[j] = i.types[j - 1];
7620 i.tm.operand_types[j] = i.tm.operand_types[j - 1];
8dc0818e 7621 i.flags[j] = i.flags[j - 1];
c0f3af97
L
7622 }
7623 i.op[0].regs
629310ab 7624 = (const reg_entry *) str_hash_find (reg_hash, "xmm0");
7ab9ffdd 7625 i.types[0] = regxmm;
c0f3af97
L
7626 i.tm.operand_types[0] = regxmm;
7627
7628 i.operands += 2;
7629 i.reg_operands += 2;
7630 i.tm.operands += 2;
7631
91d6fa6a 7632 dupl++;
c0f3af97 7633 dest++;
91d6fa6a
NC
7634 i.op[dupl] = i.op[dest];
7635 i.types[dupl] = i.types[dest];
7636 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 7637 i.flags[dupl] = i.flags[dest];
e2ec9d29 7638 }
c0f3af97
L
7639 else
7640 {
dc1e8a47 7641 duplicate:
c0f3af97
L
7642 i.operands++;
7643 i.reg_operands++;
7644 i.tm.operands++;
7645
91d6fa6a
NC
7646 i.op[dupl] = i.op[dest];
7647 i.types[dupl] = i.types[dest];
7648 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 7649 i.flags[dupl] = i.flags[dest];
c0f3af97
L
7650 }
7651
7652 if (i.tm.opcode_modifier.immext)
7653 process_immext ();
7654 }
75e5731b 7655 else if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 7656 && i.tm.operand_types[0].bitfield.xmmword)
c0f3af97
L
7657 {
7658 unsigned int j;
7659
9fcfb3d7
L
7660 for (j = 1; j < i.operands; j++)
7661 {
7662 i.op[j - 1] = i.op[j];
7663 i.types[j - 1] = i.types[j];
7664
7665 /* We need to adjust fields in i.tm since they are used by
7666 build_modrm_byte. */
7667 i.tm.operand_types [j - 1] = i.tm.operand_types [j];
8dc0818e
JB
7668
7669 i.flags[j - 1] = i.flags[j];
9fcfb3d7
L
7670 }
7671
e2ec9d29
L
7672 i.operands--;
7673 i.reg_operands--;
e2ec9d29
L
7674 i.tm.operands--;
7675 }
920d2ddc
IT
7676 else if (i.tm.opcode_modifier.implicitquadgroup)
7677 {
a477a8c4
JB
7678 unsigned int regnum, first_reg_in_group, last_reg_in_group;
7679
920d2ddc 7680 /* The second operand must be {x,y,z}mmN, where N is a multiple of 4. */
3528c362 7681 gas_assert (i.operands >= 2 && i.types[1].bitfield.class == RegSIMD);
a477a8c4
JB
7682 regnum = register_number (i.op[1].regs);
7683 first_reg_in_group = regnum & ~3;
7684 last_reg_in_group = first_reg_in_group + 3;
7685 if (regnum != first_reg_in_group)
7686 as_warn (_("source register `%s%s' implicitly denotes"
7687 " `%s%.3s%u' to `%s%.3s%u' source group in `%s'"),
7688 register_prefix, i.op[1].regs->reg_name,
7689 register_prefix, i.op[1].regs->reg_name, first_reg_in_group,
7690 register_prefix, i.op[1].regs->reg_name, last_reg_in_group,
7691 i.tm.name);
7692 }
e2ec9d29
L
7693 else if (i.tm.opcode_modifier.regkludge)
7694 {
7695 /* The imul $imm, %reg instruction is converted into
7696 imul $imm, %reg, %reg, and the clr %reg instruction
7697 is converted into xor %reg, %reg. */
7698
7699 unsigned int first_reg_op;
7700
7701 if (operand_type_check (i.types[0], reg))
7702 first_reg_op = 0;
7703 else
7704 first_reg_op = 1;
7705 /* Pretend we saw the extra register operand. */
9c2799c2 7706 gas_assert (i.reg_operands == 1
7ab9ffdd 7707 && i.op[first_reg_op + 1].regs == 0);
e2ec9d29
L
7708 i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
7709 i.types[first_reg_op + 1] = i.types[first_reg_op];
7710 i.operands++;
7711 i.reg_operands++;
29b0f896
AM
7712 }
7713
85b80b0f 7714 if (i.tm.opcode_modifier.modrm)
29b0f896
AM
7715 {
7716 /* The opcode is completed (modulo i.tm.extension_opcode which
52271982
AM
7717 must be put into the modrm byte). Now, we make the modrm and
7718 index base bytes based on all the info we've collected. */
29b0f896
AM
7719
7720 default_seg = build_modrm_byte ();
7721 }
00cee14f 7722 else if (i.types[0].bitfield.class == SReg)
85b80b0f
JB
7723 {
7724 if (flag_code != CODE_64BIT
7725 ? i.tm.base_opcode == POP_SEG_SHORT
7726 && i.op[0].regs->reg_num == 1
389d00a5 7727 : (i.tm.base_opcode | 1) == (POP_SEG386_SHORT & 0xff)
85b80b0f
JB
7728 && i.op[0].regs->reg_num < 4)
7729 {
7730 as_bad (_("you can't `%s %s%s'"),
7731 i.tm.name, register_prefix, i.op[0].regs->reg_name);
7732 return 0;
7733 }
389d00a5
JB
7734 if (i.op[0].regs->reg_num > 3
7735 && i.tm.opcode_modifier.opcodespace == SPACE_BASE )
85b80b0f 7736 {
389d00a5
JB
7737 i.tm.base_opcode ^= (POP_SEG_SHORT ^ POP_SEG386_SHORT) & 0xff;
7738 i.tm.opcode_modifier.opcodespace = SPACE_0F;
85b80b0f
JB
7739 }
7740 i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
7741 }
389d00a5
JB
7742 else if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
7743 && (i.tm.base_opcode & ~3) == MOV_AX_DISP32)
29b0f896 7744 {
5e042380 7745 default_seg = reg_ds;
29b0f896 7746 }
40fb9820 7747 else if (i.tm.opcode_modifier.isstring)
29b0f896
AM
7748 {
7749 /* For the string instructions that allow a segment override
7750 on one of their operands, the default segment is ds. */
5e042380 7751 default_seg = reg_ds;
29b0f896 7752 }
50128d0c 7753 else if (i.short_form)
85b80b0f
JB
7754 {
7755 /* The register or float register operand is in operand
7756 0 or 1. */
bab6aec1 7757 unsigned int op = i.tm.operand_types[0].bitfield.class != Reg;
85b80b0f
JB
7758
7759 /* Register goes in low 3 bits of opcode. */
7760 i.tm.base_opcode |= i.op[op].regs->reg_num;
7761 if ((i.op[op].regs->reg_flags & RegRex) != 0)
7762 i.rex |= REX_B;
7763 if (!quiet_warnings && i.tm.opcode_modifier.ugh)
7764 {
7765 /* Warn about some common errors, but press on regardless.
7766 The first case can be generated by gcc (<= 2.8.1). */
7767 if (i.operands == 2)
7768 {
7769 /* Reversed arguments on faddp, fsubp, etc. */
7770 as_warn (_("translating to `%s %s%s,%s%s'"), i.tm.name,
7771 register_prefix, i.op[!intel_syntax].regs->reg_name,
7772 register_prefix, i.op[intel_syntax].regs->reg_name);
7773 }
7774 else
7775 {
7776 /* Extraneous `l' suffix on fp insn. */
7777 as_warn (_("translating to `%s %s%s'"), i.tm.name,
7778 register_prefix, i.op[0].regs->reg_name);
7779 }
7780 }
7781 }
29b0f896 7782
514a8bb0 7783 if ((i.seg[0] || i.prefix[SEG_PREFIX])
514a8bb0 7784 && i.tm.base_opcode == 0x8d /* lea */
35648716 7785 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
514a8bb0 7786 && !is_any_vex_encoding(&i.tm))
92334ad2
JB
7787 {
7788 if (!quiet_warnings)
7789 as_warn (_("segment override on `%s' is ineffectual"), i.tm.name);
7790 if (optimize)
7791 {
7792 i.seg[0] = NULL;
7793 i.prefix[SEG_PREFIX] = 0;
7794 }
7795 }
52271982
AM
7796
7797 /* If a segment was explicitly specified, and the specified segment
b6773884
JB
7798 is neither the default nor the one already recorded from a prefix,
7799 use an opcode prefix to select it. If we never figured out what
7800 the default segment is, then default_seg will be zero at this
7801 point, and the specified segment prefix will always be used. */
7802 if (i.seg[0]
7803 && i.seg[0] != default_seg
5e042380 7804 && i386_seg_prefixes[i.seg[0]->reg_num] != i.prefix[SEG_PREFIX])
29b0f896 7805 {
5e042380 7806 if (!add_prefix (i386_seg_prefixes[i.seg[0]->reg_num]))
29b0f896
AM
7807 return 0;
7808 }
7809 return 1;
7810}
7811
a5aeccd9 7812static INLINE void set_rex_vrex (const reg_entry *r, unsigned int rex_bit,
5b7c81bd 7813 bool do_sse2avx)
a5aeccd9
JB
7814{
7815 if (r->reg_flags & RegRex)
7816 {
7817 if (i.rex & rex_bit)
7818 as_bad (_("same type of prefix used twice"));
7819 i.rex |= rex_bit;
7820 }
7821 else if (do_sse2avx && (i.rex & rex_bit) && i.vex.register_specifier)
7822 {
7823 gas_assert (i.vex.register_specifier == r);
7824 i.vex.register_specifier += 8;
7825 }
7826
7827 if (r->reg_flags & RegVRex)
7828 i.vrex |= rex_bit;
7829}
7830
5e042380 7831static const reg_entry *
e3bb37b5 7832build_modrm_byte (void)
29b0f896 7833{
5e042380 7834 const reg_entry *default_seg = NULL;
c0f3af97 7835 unsigned int source, dest;
8cd7925b 7836 int vex_3_sources;
c0f3af97 7837
8cd7925b 7838 vex_3_sources = i.tm.opcode_modifier.vexsources == VEX3SOURCES;
c0f3af97
L
7839 if (vex_3_sources)
7840 {
91d6fa6a 7841 unsigned int nds, reg_slot;
4c2c6516 7842 expressionS *exp;
c0f3af97 7843
6b8d3588 7844 dest = i.operands - 1;
c0f3af97 7845 nds = dest - 1;
922d8de8 7846
a683cc34 7847 /* There are 2 kinds of instructions:
bed3d976 7848 1. 5 operands: 4 register operands or 3 register operands
9d3bf266 7849 plus 1 memory operand plus one Imm4 operand, VexXDS, and
bed3d976 7850 VexW0 or VexW1. The destination must be either XMM, YMM or
43234a1e 7851 ZMM register.
bed3d976 7852 2. 4 operands: 4 register operands or 3 register operands
2f1bada2 7853 plus 1 memory operand, with VexXDS. */
922d8de8 7854 gas_assert ((i.reg_operands == 4
bed3d976
JB
7855 || (i.reg_operands == 3 && i.mem_operands == 1))
7856 && i.tm.opcode_modifier.vexvvvv == VEXXDS
dcd7e323 7857 && i.tm.opcode_modifier.vexw
3528c362 7858 && i.tm.operand_types[dest].bitfield.class == RegSIMD);
a683cc34 7859
48db9223
JB
7860 /* If VexW1 is set, the first non-immediate operand is the source and
7861 the second non-immediate one is encoded in the immediate operand. */
7862 if (i.tm.opcode_modifier.vexw == VEXW1)
7863 {
7864 source = i.imm_operands;
7865 reg_slot = i.imm_operands + 1;
7866 }
7867 else
7868 {
7869 source = i.imm_operands + 1;
7870 reg_slot = i.imm_operands;
7871 }
7872
a683cc34 7873 if (i.imm_operands == 0)
bed3d976
JB
7874 {
7875 /* When there is no immediate operand, generate an 8bit
7876 immediate operand to encode the first operand. */
7877 exp = &im_expressions[i.imm_operands++];
7878 i.op[i.operands].imms = exp;
7879 i.types[i.operands] = imm8;
7880 i.operands++;
7881
3528c362 7882 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
bed3d976
JB
7883 exp->X_op = O_constant;
7884 exp->X_add_number = register_number (i.op[reg_slot].regs) << 4;
43234a1e
L
7885 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
7886 }
922d8de8 7887 else
bed3d976 7888 {
9d3bf266
JB
7889 gas_assert (i.imm_operands == 1);
7890 gas_assert (fits_in_imm4 (i.op[0].imms->X_add_number));
7891 gas_assert (!i.tm.opcode_modifier.immext);
a683cc34 7892
9d3bf266
JB
7893 /* Turn on Imm8 again so that output_imm will generate it. */
7894 i.types[0].bitfield.imm8 = 1;
bed3d976 7895
3528c362 7896 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
9d3bf266 7897 i.op[0].imms->X_add_number
bed3d976 7898 |= register_number (i.op[reg_slot].regs) << 4;
43234a1e 7899 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
bed3d976 7900 }
a683cc34 7901
3528c362 7902 gas_assert (i.tm.operand_types[nds].bitfield.class == RegSIMD);
dae39acc 7903 i.vex.register_specifier = i.op[nds].regs;
c0f3af97
L
7904 }
7905 else
7906 source = dest = 0;
29b0f896
AM
7907
7908 /* i.reg_operands MUST be the number of real register operands;
c0f3af97
L
7909 implicit registers do not count. If there are 3 register
7910 operands, it must be a instruction with VexNDS. For a
7911 instruction with VexNDD, the destination register is encoded
7912 in VEX prefix. If there are 4 register operands, it must be
7913 a instruction with VEX prefix and 3 sources. */
7ab9ffdd
L
7914 if (i.mem_operands == 0
7915 && ((i.reg_operands == 2
2426c15f 7916 && i.tm.opcode_modifier.vexvvvv <= VEXXDS)
7ab9ffdd 7917 || (i.reg_operands == 3
2426c15f 7918 && i.tm.opcode_modifier.vexvvvv == VEXXDS)
7ab9ffdd 7919 || (i.reg_operands == 4 && vex_3_sources)))
29b0f896 7920 {
cab737b9
L
7921 switch (i.operands)
7922 {
7923 case 2:
7924 source = 0;
7925 break;
7926 case 3:
c81128dc
L
7927 /* When there are 3 operands, one of them may be immediate,
7928 which may be the first or the last operand. Otherwise,
c0f3af97
L
7929 the first operand must be shift count register (cl) or it
7930 is an instruction with VexNDS. */
9c2799c2 7931 gas_assert (i.imm_operands == 1
7ab9ffdd 7932 || (i.imm_operands == 0
2426c15f 7933 && (i.tm.opcode_modifier.vexvvvv == VEXXDS
75e5731b
JB
7934 || (i.types[0].bitfield.instance == RegC
7935 && i.types[0].bitfield.byte))));
40fb9820 7936 if (operand_type_check (i.types[0], imm)
75e5731b
JB
7937 || (i.types[0].bitfield.instance == RegC
7938 && i.types[0].bitfield.byte))
40fb9820
L
7939 source = 1;
7940 else
7941 source = 0;
cab737b9
L
7942 break;
7943 case 4:
368d64cc
L
7944 /* When there are 4 operands, the first two must be 8bit
7945 immediate operands. The source operand will be the 3rd
c0f3af97
L
7946 one.
7947
7948 For instructions with VexNDS, if the first operand
7949 an imm8, the source operand is the 2nd one. If the last
7950 operand is imm8, the source operand is the first one. */
9c2799c2 7951 gas_assert ((i.imm_operands == 2
7ab9ffdd
L
7952 && i.types[0].bitfield.imm8
7953 && i.types[1].bitfield.imm8)
2426c15f 7954 || (i.tm.opcode_modifier.vexvvvv == VEXXDS
7ab9ffdd
L
7955 && i.imm_operands == 1
7956 && (i.types[0].bitfield.imm8
43234a1e 7957 || i.types[i.operands - 1].bitfield.imm8
ca5312a2 7958 || i.rounding.type != rc_none)));
9f2670f2
L
7959 if (i.imm_operands == 2)
7960 source = 2;
7961 else
c0f3af97
L
7962 {
7963 if (i.types[0].bitfield.imm8)
7964 source = 1;
7965 else
7966 source = 0;
7967 }
c0f3af97
L
7968 break;
7969 case 5:
e771e7c9 7970 if (is_evex_encoding (&i.tm))
43234a1e
L
7971 {
7972 /* For EVEX instructions, when there are 5 operands, the
7973 first one must be immediate operand. If the second one
7974 is immediate operand, the source operand is the 3th
7975 one. If the last one is immediate operand, the source
7976 operand is the 2nd one. */
7977 gas_assert (i.imm_operands == 2
7978 && i.tm.opcode_modifier.sae
7979 && operand_type_check (i.types[0], imm));
7980 if (operand_type_check (i.types[1], imm))
7981 source = 2;
7982 else if (operand_type_check (i.types[4], imm))
7983 source = 1;
7984 else
7985 abort ();
7986 }
cab737b9
L
7987 break;
7988 default:
7989 abort ();
7990 }
7991
c0f3af97
L
7992 if (!vex_3_sources)
7993 {
7994 dest = source + 1;
7995
43234a1e
L
7996 /* RC/SAE operand could be between DEST and SRC. That happens
7997 when one operand is GPR and the other one is XMM/YMM/ZMM
7998 register. */
ca5312a2 7999 if (i.rounding.type != rc_none && i.rounding.operand == dest)
43234a1e
L
8000 dest++;
8001
2426c15f 8002 if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
c0f3af97 8003 {
43234a1e 8004 /* For instructions with VexNDS, the register-only source
c5d0745b 8005 operand must be a 32/64bit integer, XMM, YMM, ZMM, or mask
dfd69174 8006 register. It is encoded in VEX prefix. */
f12dc422
L
8007
8008 i386_operand_type op;
8009 unsigned int vvvv;
8010
c2ecccb3
L
8011 /* Swap two source operands if needed. */
8012 if (i.tm.opcode_modifier.swapsources)
f12dc422
L
8013 {
8014 vvvv = source;
8015 source = dest;
8016 }
8017 else
8018 vvvv = dest;
8019
8020 op = i.tm.operand_types[vvvv];
c0f3af97 8021 if ((dest + 1) >= i.operands
bab6aec1 8022 || ((op.bitfield.class != Reg
dc821c5f 8023 || (!op.bitfield.dword && !op.bitfield.qword))
3528c362 8024 && op.bitfield.class != RegSIMD
43234a1e 8025 && !operand_type_equal (&op, &regmask)))
c0f3af97 8026 abort ();
f12dc422 8027 i.vex.register_specifier = i.op[vvvv].regs;
c0f3af97
L
8028 dest++;
8029 }
8030 }
29b0f896
AM
8031
8032 i.rm.mode = 3;
dfd69174
JB
8033 /* One of the register operands will be encoded in the i.rm.reg
8034 field, the other in the combined i.rm.mode and i.rm.regmem
29b0f896
AM
8035 fields. If no form of this instruction supports a memory
8036 destination operand, then we assume the source operand may
8037 sometimes be a memory operand and so we need to store the
8038 destination in the i.rm.reg field. */
dfd69174 8039 if (!i.tm.opcode_modifier.regmem
40fb9820 8040 && operand_type_check (i.tm.operand_types[dest], anymem) == 0)
29b0f896
AM
8041 {
8042 i.rm.reg = i.op[dest].regs->reg_num;
8043 i.rm.regmem = i.op[source].regs->reg_num;
a5aeccd9 8044 set_rex_vrex (i.op[dest].regs, REX_R, i.tm.opcode_modifier.sse2avx);
5b7c81bd 8045 set_rex_vrex (i.op[source].regs, REX_B, false);
29b0f896
AM
8046 }
8047 else
8048 {
8049 i.rm.reg = i.op[source].regs->reg_num;
8050 i.rm.regmem = i.op[dest].regs->reg_num;
a5aeccd9 8051 set_rex_vrex (i.op[dest].regs, REX_B, i.tm.opcode_modifier.sse2avx);
5b7c81bd 8052 set_rex_vrex (i.op[source].regs, REX_R, false);
29b0f896 8053 }
e0c7f900 8054 if (flag_code != CODE_64BIT && (i.rex & REX_R))
c4a530c5 8055 {
4a5c67ed 8056 if (i.types[!i.tm.opcode_modifier.regmem].bitfield.class != RegCR)
c4a530c5 8057 abort ();
e0c7f900 8058 i.rex &= ~REX_R;
c4a530c5
JB
8059 add_prefix (LOCK_PREFIX_OPCODE);
8060 }
29b0f896
AM
8061 }
8062 else
8063 { /* If it's not 2 reg operands... */
c0f3af97
L
8064 unsigned int mem;
8065
29b0f896
AM
8066 if (i.mem_operands)
8067 {
8068 unsigned int fake_zero_displacement = 0;
99018f42 8069 unsigned int op;
4eed87de 8070
7ab9ffdd 8071 for (op = 0; op < i.operands; op++)
8dc0818e 8072 if (i.flags[op] & Operand_Mem)
7ab9ffdd 8073 break;
7ab9ffdd 8074 gas_assert (op < i.operands);
29b0f896 8075
63112cd6 8076 if (i.tm.opcode_modifier.sib)
6c30d220 8077 {
260cd341
LC
8078 /* The index register of VSIB shouldn't be RegIZ. */
8079 if (i.tm.opcode_modifier.sib != SIBMEM
8080 && i.index_reg->reg_num == RegIZ)
6c30d220
L
8081 abort ();
8082
8083 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8084 if (!i.base_reg)
8085 {
8086 i.sib.base = NO_BASE_REGISTER;
8087 i.sib.scale = i.log2_scale_factor;
8088 i.types[op].bitfield.disp8 = 0;
8089 i.types[op].bitfield.disp16 = 0;
8090 i.types[op].bitfield.disp64 = 0;
43083a50 8091 if (flag_code != CODE_64BIT || i.prefix[ADDR_PREFIX])
6c30d220
L
8092 {
8093 /* Must be 32 bit */
8094 i.types[op].bitfield.disp32 = 1;
8095 i.types[op].bitfield.disp32s = 0;
8096 }
8097 else
8098 {
8099 i.types[op].bitfield.disp32 = 0;
8100 i.types[op].bitfield.disp32s = 1;
8101 }
8102 }
260cd341
LC
8103
8104 /* Since the mandatory SIB always has index register, so
8105 the code logic remains unchanged. The non-mandatory SIB
8106 without index register is allowed and will be handled
8107 later. */
8108 if (i.index_reg)
8109 {
8110 if (i.index_reg->reg_num == RegIZ)
8111 i.sib.index = NO_INDEX_REGISTER;
8112 else
8113 i.sib.index = i.index_reg->reg_num;
5b7c81bd 8114 set_rex_vrex (i.index_reg, REX_X, false);
260cd341 8115 }
6c30d220
L
8116 }
8117
5e042380 8118 default_seg = reg_ds;
29b0f896
AM
8119
8120 if (i.base_reg == 0)
8121 {
8122 i.rm.mode = 0;
8123 if (!i.disp_operands)
9bb129e8 8124 fake_zero_displacement = 1;
29b0f896
AM
8125 if (i.index_reg == 0)
8126 {
73053c1f
JB
8127 i386_operand_type newdisp;
8128
260cd341
LC
8129 /* Both check for VSIB and mandatory non-vector SIB. */
8130 gas_assert (!i.tm.opcode_modifier.sib
8131 || i.tm.opcode_modifier.sib == SIBMEM);
29b0f896 8132 /* Operand is just <disp> */
20f0a1fc 8133 if (flag_code == CODE_64BIT)
29b0f896
AM
8134 {
8135 /* 64bit mode overwrites the 32bit absolute
8136 addressing by RIP relative addressing and
8137 absolute addressing is encoded by one of the
8138 redundant SIB forms. */
8139 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8140 i.sib.base = NO_BASE_REGISTER;
8141 i.sib.index = NO_INDEX_REGISTER;
73053c1f 8142 newdisp = (!i.prefix[ADDR_PREFIX] ? disp32s : disp32);
20f0a1fc 8143 }
fc225355
L
8144 else if ((flag_code == CODE_16BIT)
8145 ^ (i.prefix[ADDR_PREFIX] != 0))
20f0a1fc
NC
8146 {
8147 i.rm.regmem = NO_BASE_REGISTER_16;
73053c1f 8148 newdisp = disp16;
20f0a1fc
NC
8149 }
8150 else
8151 {
8152 i.rm.regmem = NO_BASE_REGISTER;
73053c1f 8153 newdisp = disp32;
29b0f896 8154 }
73053c1f
JB
8155 i.types[op] = operand_type_and_not (i.types[op], anydisp);
8156 i.types[op] = operand_type_or (i.types[op], newdisp);
29b0f896 8157 }
63112cd6 8158 else if (!i.tm.opcode_modifier.sib)
29b0f896 8159 {
6c30d220 8160 /* !i.base_reg && i.index_reg */
e968fc9b 8161 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8162 i.sib.index = NO_INDEX_REGISTER;
8163 else
8164 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8165 i.sib.base = NO_BASE_REGISTER;
8166 i.sib.scale = i.log2_scale_factor;
8167 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
40fb9820
L
8168 i.types[op].bitfield.disp8 = 0;
8169 i.types[op].bitfield.disp16 = 0;
8170 i.types[op].bitfield.disp64 = 0;
43083a50 8171 if (flag_code != CODE_64BIT || i.prefix[ADDR_PREFIX])
40fb9820
L
8172 {
8173 /* Must be 32 bit */
8174 i.types[op].bitfield.disp32 = 1;
8175 i.types[op].bitfield.disp32s = 0;
8176 }
29b0f896 8177 else
40fb9820
L
8178 {
8179 i.types[op].bitfield.disp32 = 0;
8180 i.types[op].bitfield.disp32s = 1;
8181 }
29b0f896 8182 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8183 i.rex |= REX_X;
29b0f896
AM
8184 }
8185 }
8186 /* RIP addressing for 64bit mode. */
e968fc9b 8187 else if (i.base_reg->reg_num == RegIP)
29b0f896 8188 {
63112cd6 8189 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896 8190 i.rm.regmem = NO_BASE_REGISTER;
40fb9820
L
8191 i.types[op].bitfield.disp8 = 0;
8192 i.types[op].bitfield.disp16 = 0;
8193 i.types[op].bitfield.disp32 = 0;
8194 i.types[op].bitfield.disp32s = 1;
8195 i.types[op].bitfield.disp64 = 0;
71903a11 8196 i.flags[op] |= Operand_PCrel;
20f0a1fc
NC
8197 if (! i.disp_operands)
8198 fake_zero_displacement = 1;
29b0f896 8199 }
dc821c5f 8200 else if (i.base_reg->reg_type.bitfield.word)
29b0f896 8201 {
63112cd6 8202 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896
AM
8203 switch (i.base_reg->reg_num)
8204 {
8205 case 3: /* (%bx) */
8206 if (i.index_reg == 0)
8207 i.rm.regmem = 7;
8208 else /* (%bx,%si) -> 0, or (%bx,%di) -> 1 */
8209 i.rm.regmem = i.index_reg->reg_num - 6;
8210 break;
8211 case 5: /* (%bp) */
5e042380 8212 default_seg = reg_ss;
29b0f896
AM
8213 if (i.index_reg == 0)
8214 {
8215 i.rm.regmem = 6;
40fb9820 8216 if (operand_type_check (i.types[op], disp) == 0)
29b0f896
AM
8217 {
8218 /* fake (%bp) into 0(%bp) */
41eb8e88 8219 if (i.disp_encoding == disp_encoding_16bit)
1a02d6b0
L
8220 i.types[op].bitfield.disp16 = 1;
8221 else
8222 i.types[op].bitfield.disp8 = 1;
252b5132 8223 fake_zero_displacement = 1;
29b0f896
AM
8224 }
8225 }
8226 else /* (%bp,%si) -> 2, or (%bp,%di) -> 3 */
8227 i.rm.regmem = i.index_reg->reg_num - 6 + 2;
8228 break;
8229 default: /* (%si) -> 4 or (%di) -> 5 */
8230 i.rm.regmem = i.base_reg->reg_num - 6 + 4;
8231 }
41eb8e88
L
8232 if (!fake_zero_displacement
8233 && !i.disp_operands
8234 && i.disp_encoding)
8235 {
8236 fake_zero_displacement = 1;
8237 if (i.disp_encoding == disp_encoding_8bit)
8238 i.types[op].bitfield.disp8 = 1;
8239 else
8240 i.types[op].bitfield.disp16 = 1;
8241 }
29b0f896
AM
8242 i.rm.mode = mode_from_disp_size (i.types[op]);
8243 }
8244 else /* i.base_reg and 32/64 bit mode */
8245 {
8246 if (flag_code == CODE_64BIT
40fb9820
L
8247 && operand_type_check (i.types[op], disp))
8248 {
73053c1f
JB
8249 i.types[op].bitfield.disp16 = 0;
8250 i.types[op].bitfield.disp64 = 0;
40fb9820 8251 if (i.prefix[ADDR_PREFIX] == 0)
73053c1f
JB
8252 {
8253 i.types[op].bitfield.disp32 = 0;
8254 i.types[op].bitfield.disp32s = 1;
8255 }
40fb9820 8256 else
73053c1f
JB
8257 {
8258 i.types[op].bitfield.disp32 = 1;
8259 i.types[op].bitfield.disp32s = 0;
8260 }
40fb9820 8261 }
20f0a1fc 8262
63112cd6 8263 if (!i.tm.opcode_modifier.sib)
6c30d220 8264 i.rm.regmem = i.base_reg->reg_num;
29b0f896 8265 if ((i.base_reg->reg_flags & RegRex) != 0)
161a04f6 8266 i.rex |= REX_B;
29b0f896
AM
8267 i.sib.base = i.base_reg->reg_num;
8268 /* x86-64 ignores REX prefix bit here to avoid decoder
8269 complications. */
848930b2
JB
8270 if (!(i.base_reg->reg_flags & RegRex)
8271 && (i.base_reg->reg_num == EBP_REG_NUM
8272 || i.base_reg->reg_num == ESP_REG_NUM))
5e042380 8273 default_seg = reg_ss;
848930b2 8274 if (i.base_reg->reg_num == 5 && i.disp_operands == 0)
29b0f896 8275 {
848930b2 8276 fake_zero_displacement = 1;
1a02d6b0
L
8277 if (i.disp_encoding == disp_encoding_32bit)
8278 i.types[op].bitfield.disp32 = 1;
8279 else
8280 i.types[op].bitfield.disp8 = 1;
29b0f896
AM
8281 }
8282 i.sib.scale = i.log2_scale_factor;
8283 if (i.index_reg == 0)
8284 {
260cd341
LC
8285 /* Only check for VSIB. */
8286 gas_assert (i.tm.opcode_modifier.sib != VECSIB128
8287 && i.tm.opcode_modifier.sib != VECSIB256
8288 && i.tm.opcode_modifier.sib != VECSIB512);
8289
29b0f896
AM
8290 /* <disp>(%esp) becomes two byte modrm with no index
8291 register. We've already stored the code for esp
8292 in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
8293 Any base register besides %esp will not use the
8294 extra modrm byte. */
8295 i.sib.index = NO_INDEX_REGISTER;
29b0f896 8296 }
63112cd6 8297 else if (!i.tm.opcode_modifier.sib)
29b0f896 8298 {
e968fc9b 8299 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8300 i.sib.index = NO_INDEX_REGISTER;
8301 else
8302 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8303 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8304 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8305 i.rex |= REX_X;
29b0f896 8306 }
67a4f2b7
AO
8307
8308 if (i.disp_operands
8309 && (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
8310 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL))
8311 i.rm.mode = 0;
8312 else
a501d77e
L
8313 {
8314 if (!fake_zero_displacement
8315 && !i.disp_operands
8316 && i.disp_encoding)
8317 {
8318 fake_zero_displacement = 1;
8319 if (i.disp_encoding == disp_encoding_8bit)
8320 i.types[op].bitfield.disp8 = 1;
8321 else
8322 i.types[op].bitfield.disp32 = 1;
8323 }
8324 i.rm.mode = mode_from_disp_size (i.types[op]);
8325 }
29b0f896 8326 }
252b5132 8327
29b0f896
AM
8328 if (fake_zero_displacement)
8329 {
8330 /* Fakes a zero displacement assuming that i.types[op]
8331 holds the correct displacement size. */
8332 expressionS *exp;
8333
9c2799c2 8334 gas_assert (i.op[op].disps == 0);
29b0f896
AM
8335 exp = &disp_expressions[i.disp_operands++];
8336 i.op[op].disps = exp;
8337 exp->X_op = O_constant;
8338 exp->X_add_number = 0;
8339 exp->X_add_symbol = (symbolS *) 0;
8340 exp->X_op_symbol = (symbolS *) 0;
8341 }
c0f3af97
L
8342
8343 mem = op;
29b0f896 8344 }
c0f3af97
L
8345 else
8346 mem = ~0;
252b5132 8347
8c43a48b 8348 if (i.tm.opcode_modifier.vexsources == XOP2SOURCES)
5dd85c99
SP
8349 {
8350 if (operand_type_check (i.types[0], imm))
8351 i.vex.register_specifier = NULL;
8352 else
8353 {
8354 /* VEX.vvvv encodes one of the sources when the first
8355 operand is not an immediate. */
1ef99a7b 8356 if (i.tm.opcode_modifier.vexw == VEXW0)
5dd85c99
SP
8357 i.vex.register_specifier = i.op[0].regs;
8358 else
8359 i.vex.register_specifier = i.op[1].regs;
8360 }
8361
8362 /* Destination is a XMM register encoded in the ModRM.reg
8363 and VEX.R bit. */
8364 i.rm.reg = i.op[2].regs->reg_num;
8365 if ((i.op[2].regs->reg_flags & RegRex) != 0)
8366 i.rex |= REX_R;
8367
8368 /* ModRM.rm and VEX.B encodes the other source. */
8369 if (!i.mem_operands)
8370 {
8371 i.rm.mode = 3;
8372
1ef99a7b 8373 if (i.tm.opcode_modifier.vexw == VEXW0)
5dd85c99
SP
8374 i.rm.regmem = i.op[1].regs->reg_num;
8375 else
8376 i.rm.regmem = i.op[0].regs->reg_num;
8377
8378 if ((i.op[1].regs->reg_flags & RegRex) != 0)
8379 i.rex |= REX_B;
8380 }
8381 }
2426c15f 8382 else if (i.tm.opcode_modifier.vexvvvv == VEXLWP)
f88c9eb0
SP
8383 {
8384 i.vex.register_specifier = i.op[2].regs;
8385 if (!i.mem_operands)
8386 {
8387 i.rm.mode = 3;
8388 i.rm.regmem = i.op[1].regs->reg_num;
8389 if ((i.op[1].regs->reg_flags & RegRex) != 0)
8390 i.rex |= REX_B;
8391 }
8392 }
29b0f896
AM
8393 /* Fill in i.rm.reg or i.rm.regmem field with register operand
8394 (if any) based on i.tm.extension_opcode. Again, we must be
8395 careful to make sure that segment/control/debug/test/MMX
8396 registers are coded into the i.rm.reg field. */
f88c9eb0 8397 else if (i.reg_operands)
29b0f896 8398 {
99018f42 8399 unsigned int op;
7ab9ffdd
L
8400 unsigned int vex_reg = ~0;
8401
8402 for (op = 0; op < i.operands; op++)
921eafea
L
8403 if (i.types[op].bitfield.class == Reg
8404 || i.types[op].bitfield.class == RegBND
8405 || i.types[op].bitfield.class == RegMask
8406 || i.types[op].bitfield.class == SReg
8407 || i.types[op].bitfield.class == RegCR
8408 || i.types[op].bitfield.class == RegDR
8409 || i.types[op].bitfield.class == RegTR
8410 || i.types[op].bitfield.class == RegSIMD
8411 || i.types[op].bitfield.class == RegMMX)
8412 break;
c0209578 8413
7ab9ffdd
L
8414 if (vex_3_sources)
8415 op = dest;
2426c15f 8416 else if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
7ab9ffdd
L
8417 {
8418 /* For instructions with VexNDS, the register-only
8419 source operand is encoded in VEX prefix. */
8420 gas_assert (mem != (unsigned int) ~0);
c0f3af97 8421
7ab9ffdd 8422 if (op > mem)
c0f3af97 8423 {
7ab9ffdd
L
8424 vex_reg = op++;
8425 gas_assert (op < i.operands);
c0f3af97
L
8426 }
8427 else
c0f3af97 8428 {
f12dc422
L
8429 /* Check register-only source operand when two source
8430 operands are swapped. */
8431 if (!i.tm.operand_types[op].bitfield.baseindex
8432 && i.tm.operand_types[op + 1].bitfield.baseindex)
8433 {
8434 vex_reg = op;
8435 op += 2;
8436 gas_assert (mem == (vex_reg + 1)
8437 && op < i.operands);
8438 }
8439 else
8440 {
8441 vex_reg = op + 1;
8442 gas_assert (vex_reg < i.operands);
8443 }
c0f3af97 8444 }
7ab9ffdd 8445 }
2426c15f 8446 else if (i.tm.opcode_modifier.vexvvvv == VEXNDD)
7ab9ffdd 8447 {
f12dc422 8448 /* For instructions with VexNDD, the register destination
7ab9ffdd 8449 is encoded in VEX prefix. */
f12dc422
L
8450 if (i.mem_operands == 0)
8451 {
8452 /* There is no memory operand. */
8453 gas_assert ((op + 2) == i.operands);
8454 vex_reg = op + 1;
8455 }
8456 else
8d63c93e 8457 {
ed438a93
JB
8458 /* There are only 2 non-immediate operands. */
8459 gas_assert (op < i.imm_operands + 2
8460 && i.operands == i.imm_operands + 2);
8461 vex_reg = i.imm_operands + 1;
f12dc422 8462 }
7ab9ffdd
L
8463 }
8464 else
8465 gas_assert (op < i.operands);
99018f42 8466
7ab9ffdd
L
8467 if (vex_reg != (unsigned int) ~0)
8468 {
f12dc422 8469 i386_operand_type *type = &i.tm.operand_types[vex_reg];
7ab9ffdd 8470
bab6aec1 8471 if ((type->bitfield.class != Reg
dc821c5f 8472 || (!type->bitfield.dword && !type->bitfield.qword))
3528c362 8473 && type->bitfield.class != RegSIMD
43234a1e 8474 && !operand_type_equal (type, &regmask))
7ab9ffdd 8475 abort ();
f88c9eb0 8476
7ab9ffdd
L
8477 i.vex.register_specifier = i.op[vex_reg].regs;
8478 }
8479
1b9f0c97
L
8480 /* Don't set OP operand twice. */
8481 if (vex_reg != op)
7ab9ffdd 8482 {
1b9f0c97
L
8483 /* If there is an extension opcode to put here, the
8484 register number must be put into the regmem field. */
8485 if (i.tm.extension_opcode != None)
8486 {
8487 i.rm.regmem = i.op[op].regs->reg_num;
a5aeccd9
JB
8488 set_rex_vrex (i.op[op].regs, REX_B,
8489 i.tm.opcode_modifier.sse2avx);
1b9f0c97
L
8490 }
8491 else
8492 {
8493 i.rm.reg = i.op[op].regs->reg_num;
a5aeccd9
JB
8494 set_rex_vrex (i.op[op].regs, REX_R,
8495 i.tm.opcode_modifier.sse2avx);
1b9f0c97 8496 }
7ab9ffdd 8497 }
252b5132 8498
29b0f896
AM
8499 /* Now, if no memory operand has set i.rm.mode = 0, 1, 2 we
8500 must set it to 3 to indicate this is a register operand
8501 in the regmem field. */
8502 if (!i.mem_operands)
8503 i.rm.mode = 3;
8504 }
252b5132 8505
29b0f896 8506 /* Fill in i.rm.reg field with extension opcode (if any). */
c1e679ec 8507 if (i.tm.extension_opcode != None)
29b0f896
AM
8508 i.rm.reg = i.tm.extension_opcode;
8509 }
8510 return default_seg;
8511}
252b5132 8512
48ef937e
JB
8513static INLINE void
8514frag_opcode_byte (unsigned char byte)
8515{
8516 if (now_seg != absolute_section)
8517 FRAG_APPEND_1_CHAR (byte);
8518 else
8519 ++abs_section_offset;
8520}
8521
376cd056
JB
8522static unsigned int
8523flip_code16 (unsigned int code16)
8524{
8525 gas_assert (i.tm.operands == 1);
8526
8527 return !(i.prefix[REX_PREFIX] & REX_W)
8528 && (code16 ? i.tm.operand_types[0].bitfield.disp32
8529 || i.tm.operand_types[0].bitfield.disp32s
8530 : i.tm.operand_types[0].bitfield.disp16)
8531 ? CODE16 : 0;
8532}
8533
29b0f896 8534static void
e3bb37b5 8535output_branch (void)
29b0f896
AM
8536{
8537 char *p;
f8a5c266 8538 int size;
29b0f896
AM
8539 int code16;
8540 int prefix;
8541 relax_substateT subtype;
8542 symbolS *sym;
8543 offsetT off;
8544
48ef937e
JB
8545 if (now_seg == absolute_section)
8546 {
8547 as_bad (_("relaxable branches not supported in absolute section"));
8548 return;
8549 }
8550
f8a5c266 8551 code16 = flag_code == CODE_16BIT ? CODE16 : 0;
a501d77e 8552 size = i.disp_encoding == disp_encoding_32bit ? BIG : SMALL;
29b0f896
AM
8553
8554 prefix = 0;
8555 if (i.prefix[DATA_PREFIX] != 0)
252b5132 8556 {
29b0f896
AM
8557 prefix = 1;
8558 i.prefixes -= 1;
376cd056 8559 code16 ^= flip_code16(code16);
252b5132 8560 }
29b0f896
AM
8561 /* Pentium4 branch hints. */
8562 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8563 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
2f66722d 8564 {
29b0f896
AM
8565 prefix++;
8566 i.prefixes--;
8567 }
8568 if (i.prefix[REX_PREFIX] != 0)
8569 {
8570 prefix++;
8571 i.prefixes--;
2f66722d
AM
8572 }
8573
7e8b059b
L
8574 /* BND prefixed jump. */
8575 if (i.prefix[BND_PREFIX] != 0)
8576 {
6cb0a70e
JB
8577 prefix++;
8578 i.prefixes--;
7e8b059b
L
8579 }
8580
f2810fe0
JB
8581 if (i.prefixes != 0)
8582 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
29b0f896
AM
8583
8584 /* It's always a symbol; End frag & setup for relax.
8585 Make sure there is enough room in this frag for the largest
8586 instruction we may generate in md_convert_frag. This is 2
8587 bytes for the opcode and room for the prefix and largest
8588 displacement. */
8589 frag_grow (prefix + 2 + 4);
8590 /* Prefix and 1 opcode byte go in fr_fix. */
8591 p = frag_more (prefix + 1);
8592 if (i.prefix[DATA_PREFIX] != 0)
8593 *p++ = DATA_PREFIX_OPCODE;
8594 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
8595 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
8596 *p++ = i.prefix[SEG_PREFIX];
6cb0a70e
JB
8597 if (i.prefix[BND_PREFIX] != 0)
8598 *p++ = BND_PREFIX_OPCODE;
29b0f896
AM
8599 if (i.prefix[REX_PREFIX] != 0)
8600 *p++ = i.prefix[REX_PREFIX];
8601 *p = i.tm.base_opcode;
8602
8603 if ((unsigned char) *p == JUMP_PC_RELATIVE)
f8a5c266 8604 subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, size);
40fb9820 8605 else if (cpu_arch_flags.bitfield.cpui386)
f8a5c266 8606 subtype = ENCODE_RELAX_STATE (COND_JUMP, size);
29b0f896 8607 else
f8a5c266 8608 subtype = ENCODE_RELAX_STATE (COND_JUMP86, size);
29b0f896 8609 subtype |= code16;
3e73aa7c 8610
29b0f896
AM
8611 sym = i.op[0].disps->X_add_symbol;
8612 off = i.op[0].disps->X_add_number;
3e73aa7c 8613
29b0f896
AM
8614 if (i.op[0].disps->X_op != O_constant
8615 && i.op[0].disps->X_op != O_symbol)
3e73aa7c 8616 {
29b0f896
AM
8617 /* Handle complex expressions. */
8618 sym = make_expr_symbol (i.op[0].disps);
8619 off = 0;
8620 }
3e73aa7c 8621
29b0f896
AM
8622 /* 1 possible extra opcode + 4 byte displacement go in var part.
8623 Pass reloc in fr_var. */
d258b828 8624 frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
29b0f896 8625}
3e73aa7c 8626
bd7ab16b
L
8627#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8628/* Return TRUE iff PLT32 relocation should be used for branching to
8629 symbol S. */
8630
5b7c81bd 8631static bool
bd7ab16b
L
8632need_plt32_p (symbolS *s)
8633{
8634 /* PLT32 relocation is ELF only. */
8635 if (!IS_ELF)
5b7c81bd 8636 return false;
bd7ab16b 8637
a5def729
RO
8638#ifdef TE_SOLARIS
8639 /* Don't emit PLT32 relocation on Solaris: neither native linker nor
8640 krtld support it. */
5b7c81bd 8641 return false;
a5def729
RO
8642#endif
8643
bd7ab16b
L
8644 /* Since there is no need to prepare for PLT branch on x86-64, we
8645 can generate R_X86_64_PLT32, instead of R_X86_64_PC32, which can
8646 be used as a marker for 32-bit PC-relative branches. */
8647 if (!object_64bit)
5b7c81bd 8648 return false;
bd7ab16b 8649
44365e88 8650 if (s == NULL)
5b7c81bd 8651 return false;
44365e88 8652
bd7ab16b
L
8653 /* Weak or undefined symbol need PLT32 relocation. */
8654 if (S_IS_WEAK (s) || !S_IS_DEFINED (s))
5b7c81bd 8655 return true;
bd7ab16b
L
8656
8657 /* Non-global symbol doesn't need PLT32 relocation. */
8658 if (! S_IS_EXTERNAL (s))
5b7c81bd 8659 return false;
bd7ab16b
L
8660
8661 /* Other global symbols need PLT32 relocation. NB: Symbol with
8662 non-default visibilities are treated as normal global symbol
8663 so that PLT32 relocation can be used as a marker for 32-bit
8664 PC-relative branches. It is useful for linker relaxation. */
5b7c81bd 8665 return true;
bd7ab16b
L
8666}
8667#endif
8668
29b0f896 8669static void
e3bb37b5 8670output_jump (void)
29b0f896
AM
8671{
8672 char *p;
8673 int size;
3e02c1cc 8674 fixS *fixP;
bd7ab16b 8675 bfd_reloc_code_real_type jump_reloc = i.reloc[0];
29b0f896 8676
0cfa3eb3 8677 if (i.tm.opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
8678 {
8679 /* This is a loop or jecxz type instruction. */
8680 size = 1;
8681 if (i.prefix[ADDR_PREFIX] != 0)
8682 {
48ef937e 8683 frag_opcode_byte (ADDR_PREFIX_OPCODE);
29b0f896
AM
8684 i.prefixes -= 1;
8685 }
8686 /* Pentium4 branch hints. */
8687 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8688 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
8689 {
48ef937e 8690 frag_opcode_byte (i.prefix[SEG_PREFIX]);
29b0f896 8691 i.prefixes--;
3e73aa7c
JH
8692 }
8693 }
29b0f896
AM
8694 else
8695 {
8696 int code16;
3e73aa7c 8697
29b0f896
AM
8698 code16 = 0;
8699 if (flag_code == CODE_16BIT)
8700 code16 = CODE16;
3e73aa7c 8701
29b0f896
AM
8702 if (i.prefix[DATA_PREFIX] != 0)
8703 {
48ef937e 8704 frag_opcode_byte (DATA_PREFIX_OPCODE);
29b0f896 8705 i.prefixes -= 1;
376cd056 8706 code16 ^= flip_code16(code16);
29b0f896 8707 }
252b5132 8708
29b0f896
AM
8709 size = 4;
8710 if (code16)
8711 size = 2;
8712 }
9fcc94b6 8713
6cb0a70e
JB
8714 /* BND prefixed jump. */
8715 if (i.prefix[BND_PREFIX] != 0)
29b0f896 8716 {
48ef937e 8717 frag_opcode_byte (i.prefix[BND_PREFIX]);
29b0f896
AM
8718 i.prefixes -= 1;
8719 }
252b5132 8720
6cb0a70e 8721 if (i.prefix[REX_PREFIX] != 0)
7e8b059b 8722 {
48ef937e 8723 frag_opcode_byte (i.prefix[REX_PREFIX]);
7e8b059b
L
8724 i.prefixes -= 1;
8725 }
8726
f2810fe0
JB
8727 if (i.prefixes != 0)
8728 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
e0890092 8729
48ef937e
JB
8730 if (now_seg == absolute_section)
8731 {
9a182d04 8732 abs_section_offset += i.opcode_length + size;
48ef937e
JB
8733 return;
8734 }
8735
9a182d04
JB
8736 p = frag_more (i.opcode_length + size);
8737 switch (i.opcode_length)
42164a71
L
8738 {
8739 case 2:
8740 *p++ = i.tm.base_opcode >> 8;
1a0670f3 8741 /* Fall through. */
42164a71
L
8742 case 1:
8743 *p++ = i.tm.base_opcode;
8744 break;
8745 default:
8746 abort ();
8747 }
e0890092 8748
bd7ab16b
L
8749#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8750 if (size == 4
8751 && jump_reloc == NO_RELOC
8752 && need_plt32_p (i.op[0].disps->X_add_symbol))
8753 jump_reloc = BFD_RELOC_X86_64_PLT32;
8754#endif
8755
8756 jump_reloc = reloc (size, 1, 1, jump_reloc);
8757
3e02c1cc 8758 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
bd7ab16b 8759 i.op[0].disps, 1, jump_reloc);
3e02c1cc
AM
8760
8761 /* All jumps handled here are signed, but don't use a signed limit
8762 check for 32 and 16 bit jumps as we want to allow wrap around at
8763 4G and 64k respectively. */
8764 if (size == 1)
8765 fixP->fx_signed = 1;
29b0f896 8766}
e0890092 8767
29b0f896 8768static void
e3bb37b5 8769output_interseg_jump (void)
29b0f896
AM
8770{
8771 char *p;
8772 int size;
8773 int prefix;
8774 int code16;
252b5132 8775
29b0f896
AM
8776 code16 = 0;
8777 if (flag_code == CODE_16BIT)
8778 code16 = CODE16;
a217f122 8779
29b0f896
AM
8780 prefix = 0;
8781 if (i.prefix[DATA_PREFIX] != 0)
8782 {
8783 prefix = 1;
8784 i.prefixes -= 1;
8785 code16 ^= CODE16;
8786 }
6cb0a70e
JB
8787
8788 gas_assert (!i.prefix[REX_PREFIX]);
252b5132 8789
29b0f896
AM
8790 size = 4;
8791 if (code16)
8792 size = 2;
252b5132 8793
f2810fe0
JB
8794 if (i.prefixes != 0)
8795 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
252b5132 8796
48ef937e
JB
8797 if (now_seg == absolute_section)
8798 {
8799 abs_section_offset += prefix + 1 + 2 + size;
8800 return;
8801 }
8802
29b0f896
AM
8803 /* 1 opcode; 2 segment; offset */
8804 p = frag_more (prefix + 1 + 2 + size);
3e73aa7c 8805
29b0f896
AM
8806 if (i.prefix[DATA_PREFIX] != 0)
8807 *p++ = DATA_PREFIX_OPCODE;
252b5132 8808
29b0f896
AM
8809 if (i.prefix[REX_PREFIX] != 0)
8810 *p++ = i.prefix[REX_PREFIX];
252b5132 8811
29b0f896
AM
8812 *p++ = i.tm.base_opcode;
8813 if (i.op[1].imms->X_op == O_constant)
8814 {
8815 offsetT n = i.op[1].imms->X_add_number;
252b5132 8816
29b0f896
AM
8817 if (size == 2
8818 && !fits_in_unsigned_word (n)
8819 && !fits_in_signed_word (n))
8820 {
8821 as_bad (_("16-bit jump out of range"));
8822 return;
8823 }
8824 md_number_to_chars (p, n, size);
8825 }
8826 else
8827 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
d258b828 8828 i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
6d96a594
C
8829
8830 p += size;
8831 if (i.op[0].imms->X_op == O_constant)
8832 md_number_to_chars (p, (valueT) i.op[0].imms->X_add_number, 2);
8833 else
8834 fix_new_exp (frag_now, p - frag_now->fr_literal, 2,
8835 i.op[0].imms, 0, reloc (2, 0, 0, i.reloc[0]));
29b0f896 8836}
a217f122 8837
b4a3a7b4
L
8838#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8839void
8840x86_cleanup (void)
8841{
8842 char *p;
8843 asection *seg = now_seg;
8844 subsegT subseg = now_subseg;
8845 asection *sec;
8846 unsigned int alignment, align_size_1;
8847 unsigned int isa_1_descsz, feature_2_descsz, descsz;
8848 unsigned int isa_1_descsz_raw, feature_2_descsz_raw;
8849 unsigned int padding;
8850
8851 if (!IS_ELF || !x86_used_note)
8852 return;
8853
b4a3a7b4
L
8854 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X86;
8855
8856 /* The .note.gnu.property section layout:
8857
8858 Field Length Contents
8859 ---- ---- ----
8860 n_namsz 4 4
8861 n_descsz 4 The note descriptor size
8862 n_type 4 NT_GNU_PROPERTY_TYPE_0
8863 n_name 4 "GNU"
8864 n_desc n_descsz The program property array
8865 .... .... ....
8866 */
8867
8868 /* Create the .note.gnu.property section. */
8869 sec = subseg_new (NOTE_GNU_PROPERTY_SECTION_NAME, 0);
fd361982 8870 bfd_set_section_flags (sec,
b4a3a7b4
L
8871 (SEC_ALLOC
8872 | SEC_LOAD
8873 | SEC_DATA
8874 | SEC_HAS_CONTENTS
8875 | SEC_READONLY));
8876
8877 if (get_elf_backend_data (stdoutput)->s->elfclass == ELFCLASS64)
8878 {
8879 align_size_1 = 7;
8880 alignment = 3;
8881 }
8882 else
8883 {
8884 align_size_1 = 3;
8885 alignment = 2;
8886 }
8887
fd361982 8888 bfd_set_section_alignment (sec, alignment);
b4a3a7b4
L
8889 elf_section_type (sec) = SHT_NOTE;
8890
8891 /* GNU_PROPERTY_X86_ISA_1_USED: 4-byte type + 4-byte data size
8892 + 4-byte data */
8893 isa_1_descsz_raw = 4 + 4 + 4;
8894 /* Align GNU_PROPERTY_X86_ISA_1_USED. */
8895 isa_1_descsz = (isa_1_descsz_raw + align_size_1) & ~align_size_1;
8896
8897 feature_2_descsz_raw = isa_1_descsz;
8898 /* GNU_PROPERTY_X86_FEATURE_2_USED: 4-byte type + 4-byte data size
8899 + 4-byte data */
8900 feature_2_descsz_raw += 4 + 4 + 4;
8901 /* Align GNU_PROPERTY_X86_FEATURE_2_USED. */
8902 feature_2_descsz = ((feature_2_descsz_raw + align_size_1)
8903 & ~align_size_1);
8904
8905 descsz = feature_2_descsz;
8906 /* Section size: n_namsz + n_descsz + n_type + n_name + n_descsz. */
8907 p = frag_more (4 + 4 + 4 + 4 + descsz);
8908
8909 /* Write n_namsz. */
8910 md_number_to_chars (p, (valueT) 4, 4);
8911
8912 /* Write n_descsz. */
8913 md_number_to_chars (p + 4, (valueT) descsz, 4);
8914
8915 /* Write n_type. */
8916 md_number_to_chars (p + 4 * 2, (valueT) NT_GNU_PROPERTY_TYPE_0, 4);
8917
8918 /* Write n_name. */
8919 memcpy (p + 4 * 3, "GNU", 4);
8920
8921 /* Write 4-byte type. */
8922 md_number_to_chars (p + 4 * 4,
8923 (valueT) GNU_PROPERTY_X86_ISA_1_USED, 4);
8924
8925 /* Write 4-byte data size. */
8926 md_number_to_chars (p + 4 * 5, (valueT) 4, 4);
8927
8928 /* Write 4-byte data. */
8929 md_number_to_chars (p + 4 * 6, (valueT) x86_isa_1_used, 4);
8930
8931 /* Zero out paddings. */
8932 padding = isa_1_descsz - isa_1_descsz_raw;
8933 if (padding)
8934 memset (p + 4 * 7, 0, padding);
8935
8936 /* Write 4-byte type. */
8937 md_number_to_chars (p + isa_1_descsz + 4 * 4,
8938 (valueT) GNU_PROPERTY_X86_FEATURE_2_USED, 4);
8939
8940 /* Write 4-byte data size. */
8941 md_number_to_chars (p + isa_1_descsz + 4 * 5, (valueT) 4, 4);
8942
8943 /* Write 4-byte data. */
8944 md_number_to_chars (p + isa_1_descsz + 4 * 6,
8945 (valueT) x86_feature_2_used, 4);
8946
8947 /* Zero out paddings. */
8948 padding = feature_2_descsz - feature_2_descsz_raw;
8949 if (padding)
8950 memset (p + isa_1_descsz + 4 * 7, 0, padding);
8951
8952 /* We probably can't restore the current segment, for there likely
8953 isn't one yet... */
8954 if (seg && subseg)
8955 subseg_set (seg, subseg);
8956}
8957#endif
8958
9c33702b
JB
8959static unsigned int
8960encoding_length (const fragS *start_frag, offsetT start_off,
8961 const char *frag_now_ptr)
8962{
8963 unsigned int len = 0;
8964
8965 if (start_frag != frag_now)
8966 {
8967 const fragS *fr = start_frag;
8968
8969 do {
8970 len += fr->fr_fix;
8971 fr = fr->fr_next;
8972 } while (fr && fr != frag_now);
8973 }
8974
8975 return len - start_off + (frag_now_ptr - frag_now->fr_literal);
8976}
8977
e379e5f3 8978/* Return 1 for test, and, cmp, add, sub, inc and dec which may
79d72f45
HL
8979 be macro-fused with conditional jumps.
8980 NB: If TEST/AND/CMP/ADD/SUB/INC/DEC is of RIP relative address,
8981 or is one of the following format:
8982
8983 cmp m, imm
8984 add m, imm
8985 sub m, imm
8986 test m, imm
8987 and m, imm
8988 inc m
8989 dec m
8990
8991 it is unfusible. */
e379e5f3
L
8992
8993static int
79d72f45 8994maybe_fused_with_jcc_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
8995{
8996 /* No RIP address. */
8997 if (i.base_reg && i.base_reg->reg_num == RegIP)
8998 return 0;
8999
389d00a5
JB
9000 /* No opcodes outside of base encoding space. */
9001 if (i.tm.opcode_modifier.opcodespace != SPACE_BASE)
e379e5f3
L
9002 return 0;
9003
79d72f45
HL
9004 /* add, sub without add/sub m, imm. */
9005 if (i.tm.base_opcode <= 5
e379e5f3
L
9006 || (i.tm.base_opcode >= 0x28 && i.tm.base_opcode <= 0x2d)
9007 || ((i.tm.base_opcode | 3) == 0x83
79d72f45 9008 && (i.tm.extension_opcode == 0x5
e379e5f3 9009 || i.tm.extension_opcode == 0x0)))
79d72f45
HL
9010 {
9011 *mf_cmp_p = mf_cmp_alu_cmp;
9012 return !(i.mem_operands && i.imm_operands);
9013 }
e379e5f3 9014
79d72f45
HL
9015 /* and without and m, imm. */
9016 if ((i.tm.base_opcode >= 0x20 && i.tm.base_opcode <= 0x25)
9017 || ((i.tm.base_opcode | 3) == 0x83
9018 && i.tm.extension_opcode == 0x4))
9019 {
9020 *mf_cmp_p = mf_cmp_test_and;
9021 return !(i.mem_operands && i.imm_operands);
9022 }
9023
9024 /* test without test m imm. */
e379e5f3
L
9025 if ((i.tm.base_opcode | 1) == 0x85
9026 || (i.tm.base_opcode | 1) == 0xa9
9027 || ((i.tm.base_opcode | 1) == 0xf7
79d72f45
HL
9028 && i.tm.extension_opcode == 0))
9029 {
9030 *mf_cmp_p = mf_cmp_test_and;
9031 return !(i.mem_operands && i.imm_operands);
9032 }
9033
9034 /* cmp without cmp m, imm. */
9035 if ((i.tm.base_opcode >= 0x38 && i.tm.base_opcode <= 0x3d)
e379e5f3
L
9036 || ((i.tm.base_opcode | 3) == 0x83
9037 && (i.tm.extension_opcode == 0x7)))
79d72f45
HL
9038 {
9039 *mf_cmp_p = mf_cmp_alu_cmp;
9040 return !(i.mem_operands && i.imm_operands);
9041 }
e379e5f3 9042
79d72f45 9043 /* inc, dec without inc/dec m. */
e379e5f3
L
9044 if ((i.tm.cpu_flags.bitfield.cpuno64
9045 && (i.tm.base_opcode | 0xf) == 0x4f)
9046 || ((i.tm.base_opcode | 1) == 0xff
9047 && i.tm.extension_opcode <= 0x1))
79d72f45
HL
9048 {
9049 *mf_cmp_p = mf_cmp_incdec;
9050 return !i.mem_operands;
9051 }
e379e5f3
L
9052
9053 return 0;
9054}
9055
9056/* Return 1 if a FUSED_JCC_PADDING frag should be generated. */
9057
9058static int
79d72f45 9059add_fused_jcc_padding_frag_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
9060{
9061 /* NB: Don't work with COND_JUMP86 without i386. */
9062 if (!align_branch_power
9063 || now_seg == absolute_section
9064 || !cpu_arch_flags.bitfield.cpui386
9065 || !(align_branch & align_branch_fused_bit))
9066 return 0;
9067
79d72f45 9068 if (maybe_fused_with_jcc_p (mf_cmp_p))
e379e5f3
L
9069 {
9070 if (last_insn.kind == last_insn_other
9071 || last_insn.seg != now_seg)
9072 return 1;
9073 if (flag_debug)
9074 as_warn_where (last_insn.file, last_insn.line,
9075 _("`%s` skips -malign-branch-boundary on `%s`"),
9076 last_insn.name, i.tm.name);
9077 }
9078
9079 return 0;
9080}
9081
9082/* Return 1 if a BRANCH_PREFIX frag should be generated. */
9083
9084static int
9085add_branch_prefix_frag_p (void)
9086{
9087 /* NB: Don't work with COND_JUMP86 without i386. Don't add prefix
9088 to PadLock instructions since they include prefixes in opcode. */
9089 if (!align_branch_power
9090 || !align_branch_prefix_size
9091 || now_seg == absolute_section
9092 || i.tm.cpu_flags.bitfield.cpupadlock
9093 || !cpu_arch_flags.bitfield.cpui386)
9094 return 0;
9095
9096 /* Don't add prefix if it is a prefix or there is no operand in case
9097 that segment prefix is special. */
9098 if (!i.operands || i.tm.opcode_modifier.isprefix)
9099 return 0;
9100
9101 if (last_insn.kind == last_insn_other
9102 || last_insn.seg != now_seg)
9103 return 1;
9104
9105 if (flag_debug)
9106 as_warn_where (last_insn.file, last_insn.line,
9107 _("`%s` skips -malign-branch-boundary on `%s`"),
9108 last_insn.name, i.tm.name);
9109
9110 return 0;
9111}
9112
9113/* Return 1 if a BRANCH_PADDING frag should be generated. */
9114
9115static int
79d72f45
HL
9116add_branch_padding_frag_p (enum align_branch_kind *branch_p,
9117 enum mf_jcc_kind *mf_jcc_p)
e379e5f3
L
9118{
9119 int add_padding;
9120
9121 /* NB: Don't work with COND_JUMP86 without i386. */
9122 if (!align_branch_power
9123 || now_seg == absolute_section
389d00a5
JB
9124 || !cpu_arch_flags.bitfield.cpui386
9125 || i.tm.opcode_modifier.opcodespace != SPACE_BASE)
e379e5f3
L
9126 return 0;
9127
9128 add_padding = 0;
9129
9130 /* Check for jcc and direct jmp. */
9131 if (i.tm.opcode_modifier.jump == JUMP)
9132 {
9133 if (i.tm.base_opcode == JUMP_PC_RELATIVE)
9134 {
9135 *branch_p = align_branch_jmp;
9136 add_padding = align_branch & align_branch_jmp_bit;
9137 }
9138 else
9139 {
79d72f45
HL
9140 /* Because J<cc> and JN<cc> share same group in macro-fusible table,
9141 igore the lowest bit. */
9142 *mf_jcc_p = (i.tm.base_opcode & 0x0e) >> 1;
e379e5f3
L
9143 *branch_p = align_branch_jcc;
9144 if ((align_branch & align_branch_jcc_bit))
9145 add_padding = 1;
9146 }
9147 }
e379e5f3
L
9148 else if ((i.tm.base_opcode | 1) == 0xc3)
9149 {
9150 /* Near ret. */
9151 *branch_p = align_branch_ret;
9152 if ((align_branch & align_branch_ret_bit))
9153 add_padding = 1;
9154 }
9155 else
9156 {
9157 /* Check for indirect jmp, direct and indirect calls. */
9158 if (i.tm.base_opcode == 0xe8)
9159 {
9160 /* Direct call. */
9161 *branch_p = align_branch_call;
9162 if ((align_branch & align_branch_call_bit))
9163 add_padding = 1;
9164 }
9165 else if (i.tm.base_opcode == 0xff
9166 && (i.tm.extension_opcode == 2
9167 || i.tm.extension_opcode == 4))
9168 {
9169 /* Indirect call and jmp. */
9170 *branch_p = align_branch_indirect;
9171 if ((align_branch & align_branch_indirect_bit))
9172 add_padding = 1;
9173 }
9174
9175 if (add_padding
9176 && i.disp_operands
9177 && tls_get_addr
9178 && (i.op[0].disps->X_op == O_symbol
9179 || (i.op[0].disps->X_op == O_subtract
9180 && i.op[0].disps->X_op_symbol == GOT_symbol)))
9181 {
9182 symbolS *s = i.op[0].disps->X_add_symbol;
9183 /* No padding to call to global or undefined tls_get_addr. */
9184 if ((S_IS_EXTERNAL (s) || !S_IS_DEFINED (s))
9185 && strcmp (S_GET_NAME (s), tls_get_addr) == 0)
9186 return 0;
9187 }
9188 }
9189
9190 if (add_padding
9191 && last_insn.kind != last_insn_other
9192 && last_insn.seg == now_seg)
9193 {
9194 if (flag_debug)
9195 as_warn_where (last_insn.file, last_insn.line,
9196 _("`%s` skips -malign-branch-boundary on `%s`"),
9197 last_insn.name, i.tm.name);
9198 return 0;
9199 }
9200
9201 return add_padding;
9202}
9203
29b0f896 9204static void
e3bb37b5 9205output_insn (void)
29b0f896 9206{
2bbd9c25
JJ
9207 fragS *insn_start_frag;
9208 offsetT insn_start_off;
e379e5f3
L
9209 fragS *fragP = NULL;
9210 enum align_branch_kind branch = align_branch_none;
79d72f45
HL
9211 /* The initializer is arbitrary just to avoid uninitialized error.
9212 it's actually either assigned in add_branch_padding_frag_p
9213 or never be used. */
9214 enum mf_jcc_kind mf_jcc = mf_jcc_jo;
2bbd9c25 9215
b4a3a7b4 9216#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
48ef937e 9217 if (IS_ELF && x86_used_note && now_seg != absolute_section)
b4a3a7b4 9218 {
32930e4e
L
9219 if ((i.xstate & xstate_tmm) == xstate_tmm
9220 || i.tm.cpu_flags.bitfield.cpuamx_tile)
9221 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_TMM;
9222
b4a3a7b4
L
9223 if (i.tm.cpu_flags.bitfield.cpu8087
9224 || i.tm.cpu_flags.bitfield.cpu287
9225 || i.tm.cpu_flags.bitfield.cpu387
9226 || i.tm.cpu_flags.bitfield.cpu687
9227 || i.tm.cpu_flags.bitfield.cpufisttp)
9228 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X87;
014d61ea 9229
921eafea 9230 if ((i.xstate & xstate_mmx)
389d00a5
JB
9231 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
9232 && !is_any_vex_encoding (&i.tm)
9233 && (i.tm.base_opcode == 0x77 /* emms */
9234 || i.tm.base_opcode == 0x0e /* femms */)))
b4a3a7b4 9235 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MMX;
014d61ea 9236
32930e4e
L
9237 if (i.index_reg)
9238 {
9239 if (i.index_reg->reg_type.bitfield.zmmword)
9240 i.xstate |= xstate_zmm;
9241 else if (i.index_reg->reg_type.bitfield.ymmword)
9242 i.xstate |= xstate_ymm;
9243 else if (i.index_reg->reg_type.bitfield.xmmword)
9244 i.xstate |= xstate_xmm;
9245 }
014d61ea
JB
9246
9247 /* vzeroall / vzeroupper */
9248 if (i.tm.base_opcode == 0x77 && i.tm.cpu_flags.bitfield.cpuavx)
9249 i.xstate |= xstate_ymm;
9250
c4694f17 9251 if ((i.xstate & xstate_xmm)
389d00a5
JB
9252 /* ldmxcsr / stmxcsr / vldmxcsr / vstmxcsr */
9253 || (i.tm.base_opcode == 0xae
9254 && (i.tm.cpu_flags.bitfield.cpusse
9255 || i.tm.cpu_flags.bitfield.cpuavx))
c4694f17
TG
9256 || i.tm.cpu_flags.bitfield.cpuwidekl
9257 || i.tm.cpu_flags.bitfield.cpukl)
b4a3a7b4 9258 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XMM;
014d61ea 9259
921eafea 9260 if ((i.xstate & xstate_ymm) == xstate_ymm)
b4a3a7b4 9261 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_YMM;
921eafea 9262 if ((i.xstate & xstate_zmm) == xstate_zmm)
b4a3a7b4 9263 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_ZMM;
6225c532 9264 if (i.mask.reg || (i.xstate & xstate_mask) == xstate_mask)
32930e4e 9265 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MASK;
b4a3a7b4
L
9266 if (i.tm.cpu_flags.bitfield.cpufxsr)
9267 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_FXSR;
9268 if (i.tm.cpu_flags.bitfield.cpuxsave)
9269 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVE;
9270 if (i.tm.cpu_flags.bitfield.cpuxsaveopt)
9271 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT;
9272 if (i.tm.cpu_flags.bitfield.cpuxsavec)
9273 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEC;
b0ab0693
L
9274
9275 if (x86_feature_2_used
9276 || i.tm.cpu_flags.bitfield.cpucmov
9277 || i.tm.cpu_flags.bitfield.cpusyscall
389d00a5
JB
9278 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
9279 && i.tm.base_opcode == 0xc7
70e95837 9280 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
b0ab0693
L
9281 && i.tm.extension_opcode == 1) /* cmpxchg8b */)
9282 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_BASELINE;
9283 if (i.tm.cpu_flags.bitfield.cpusse3
9284 || i.tm.cpu_flags.bitfield.cpussse3
9285 || i.tm.cpu_flags.bitfield.cpusse4_1
9286 || i.tm.cpu_flags.bitfield.cpusse4_2
9287 || i.tm.cpu_flags.bitfield.cpucx16
9288 || i.tm.cpu_flags.bitfield.cpupopcnt
9289 /* LAHF-SAHF insns in 64-bit mode. */
9290 || (flag_code == CODE_64BIT
35648716
JB
9291 && (i.tm.base_opcode | 1) == 0x9f
9292 && i.tm.opcode_modifier.opcodespace == SPACE_BASE))
b0ab0693
L
9293 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V2;
9294 if (i.tm.cpu_flags.bitfield.cpuavx
9295 || i.tm.cpu_flags.bitfield.cpuavx2
9296 /* Any VEX encoded insns execpt for CpuAVX512F, CpuAVX512BW,
9297 CpuAVX512DQ, LPW, TBM and AMX. */
9298 || (i.tm.opcode_modifier.vex
9299 && !i.tm.cpu_flags.bitfield.cpuavx512f
9300 && !i.tm.cpu_flags.bitfield.cpuavx512bw
9301 && !i.tm.cpu_flags.bitfield.cpuavx512dq
9302 && !i.tm.cpu_flags.bitfield.cpulwp
9303 && !i.tm.cpu_flags.bitfield.cputbm
9304 && !(x86_feature_2_used & GNU_PROPERTY_X86_FEATURE_2_TMM))
9305 || i.tm.cpu_flags.bitfield.cpuf16c
9306 || i.tm.cpu_flags.bitfield.cpufma
9307 || i.tm.cpu_flags.bitfield.cpulzcnt
9308 || i.tm.cpu_flags.bitfield.cpumovbe
9309 || i.tm.cpu_flags.bitfield.cpuxsaves
9310 || (x86_feature_2_used
9311 & (GNU_PROPERTY_X86_FEATURE_2_XSAVE
9312 | GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT
9313 | GNU_PROPERTY_X86_FEATURE_2_XSAVEC)) != 0)
9314 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V3;
9315 if (i.tm.cpu_flags.bitfield.cpuavx512f
9316 || i.tm.cpu_flags.bitfield.cpuavx512bw
9317 || i.tm.cpu_flags.bitfield.cpuavx512dq
9318 || i.tm.cpu_flags.bitfield.cpuavx512vl
9319 /* Any EVEX encoded insns except for AVX512ER, AVX512PF and
9320 VNNIW. */
9321 || (i.tm.opcode_modifier.evex
9322 && !i.tm.cpu_flags.bitfield.cpuavx512er
9323 && !i.tm.cpu_flags.bitfield.cpuavx512pf
9324 && !i.tm.cpu_flags.bitfield.cpuavx512_4vnniw))
9325 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V4;
b4a3a7b4
L
9326 }
9327#endif
9328
29b0f896
AM
9329 /* Tie dwarf2 debug info to the address at the start of the insn.
9330 We can't do this after the insn has been output as the current
9331 frag may have been closed off. eg. by frag_var. */
9332 dwarf2_emit_insn (0);
9333
2bbd9c25
JJ
9334 insn_start_frag = frag_now;
9335 insn_start_off = frag_now_fix ();
9336
79d72f45 9337 if (add_branch_padding_frag_p (&branch, &mf_jcc))
e379e5f3
L
9338 {
9339 char *p;
9340 /* Branch can be 8 bytes. Leave some room for prefixes. */
9341 unsigned int max_branch_padding_size = 14;
9342
9343 /* Align section to boundary. */
9344 record_alignment (now_seg, align_branch_power);
9345
9346 /* Make room for padding. */
9347 frag_grow (max_branch_padding_size);
9348
9349 /* Start of the padding. */
9350 p = frag_more (0);
9351
9352 fragP = frag_now;
9353
9354 frag_var (rs_machine_dependent, max_branch_padding_size, 0,
9355 ENCODE_RELAX_STATE (BRANCH_PADDING, 0),
9356 NULL, 0, p);
9357
79d72f45 9358 fragP->tc_frag_data.mf_type = mf_jcc;
e379e5f3
L
9359 fragP->tc_frag_data.branch_type = branch;
9360 fragP->tc_frag_data.max_bytes = max_branch_padding_size;
9361 }
9362
29b0f896 9363 /* Output jumps. */
0cfa3eb3 9364 if (i.tm.opcode_modifier.jump == JUMP)
29b0f896 9365 output_branch ();
0cfa3eb3
JB
9366 else if (i.tm.opcode_modifier.jump == JUMP_BYTE
9367 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896 9368 output_jump ();
0cfa3eb3 9369 else if (i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT)
29b0f896
AM
9370 output_interseg_jump ();
9371 else
9372 {
9373 /* Output normal instructions here. */
9374 char *p;
9375 unsigned char *q;
47465058 9376 unsigned int j;
79d72f45 9377 enum mf_cmp_kind mf_cmp;
4dffcebc 9378
e4e00185 9379 if (avoid_fence
389d00a5
JB
9380 && (i.tm.base_opcode == 0xaee8
9381 || i.tm.base_opcode == 0xaef0
9382 || i.tm.base_opcode == 0xaef8))
48ef937e
JB
9383 {
9384 /* Encode lfence, mfence, and sfence as
9385 f0 83 04 24 00 lock addl $0x0, (%{re}sp). */
9386 if (now_seg != absolute_section)
9387 {
9388 offsetT val = 0x240483f0ULL;
9389
9390 p = frag_more (5);
9391 md_number_to_chars (p, val, 5);
9392 }
9393 else
9394 abs_section_offset += 5;
9395 return;
9396 }
e4e00185 9397
d022bddd
IT
9398 /* Some processors fail on LOCK prefix. This options makes
9399 assembler ignore LOCK prefix and serves as a workaround. */
9400 if (omit_lock_prefix)
9401 {
35648716
JB
9402 if (i.tm.base_opcode == LOCK_PREFIX_OPCODE
9403 && i.tm.opcode_modifier.isprefix)
d022bddd
IT
9404 return;
9405 i.prefix[LOCK_PREFIX] = 0;
9406 }
9407
e379e5f3
L
9408 if (branch)
9409 /* Skip if this is a branch. */
9410 ;
79d72f45 9411 else if (add_fused_jcc_padding_frag_p (&mf_cmp))
e379e5f3
L
9412 {
9413 /* Make room for padding. */
9414 frag_grow (MAX_FUSED_JCC_PADDING_SIZE);
9415 p = frag_more (0);
9416
9417 fragP = frag_now;
9418
9419 frag_var (rs_machine_dependent, MAX_FUSED_JCC_PADDING_SIZE, 0,
9420 ENCODE_RELAX_STATE (FUSED_JCC_PADDING, 0),
9421 NULL, 0, p);
9422
79d72f45 9423 fragP->tc_frag_data.mf_type = mf_cmp;
e379e5f3
L
9424 fragP->tc_frag_data.branch_type = align_branch_fused;
9425 fragP->tc_frag_data.max_bytes = MAX_FUSED_JCC_PADDING_SIZE;
9426 }
9427 else if (add_branch_prefix_frag_p ())
9428 {
9429 unsigned int max_prefix_size = align_branch_prefix_size;
9430
9431 /* Make room for padding. */
9432 frag_grow (max_prefix_size);
9433 p = frag_more (0);
9434
9435 fragP = frag_now;
9436
9437 frag_var (rs_machine_dependent, max_prefix_size, 0,
9438 ENCODE_RELAX_STATE (BRANCH_PREFIX, 0),
9439 NULL, 0, p);
9440
9441 fragP->tc_frag_data.max_bytes = max_prefix_size;
9442 }
9443
43234a1e
L
9444 /* Since the VEX/EVEX prefix contains the implicit prefix, we
9445 don't need the explicit prefix. */
9446 if (!i.tm.opcode_modifier.vex && !i.tm.opcode_modifier.evex)
bc4bd9ab 9447 {
7b47a312 9448 switch (i.tm.opcode_modifier.opcodeprefix)
bc4bd9ab 9449 {
7b47a312
L
9450 case PREFIX_0X66:
9451 add_prefix (0x66);
9452 break;
9453 case PREFIX_0XF2:
9454 add_prefix (0xf2);
9455 break;
9456 case PREFIX_0XF3:
8b65b895
L
9457 if (!i.tm.cpu_flags.bitfield.cpupadlock
9458 || (i.prefix[REP_PREFIX] != 0xf3))
9459 add_prefix (0xf3);
c0f3af97 9460 break;
7b47a312 9461 case PREFIX_NONE:
9a182d04 9462 switch (i.opcode_length)
c0f3af97 9463 {
7b47a312 9464 case 2:
7b47a312 9465 break;
9a182d04 9466 case 1:
7b47a312 9467 /* Check for pseudo prefixes. */
9a182d04
JB
9468 if (!i.tm.opcode_modifier.isprefix || i.tm.base_opcode)
9469 break;
7b47a312
L
9470 as_bad_where (insn_start_frag->fr_file,
9471 insn_start_frag->fr_line,
9472 _("pseudo prefix without instruction"));
9473 return;
9474 default:
9475 abort ();
4dffcebc 9476 }
c0f3af97 9477 break;
c0f3af97
L
9478 default:
9479 abort ();
bc4bd9ab 9480 }
c0f3af97 9481
6d19a37a 9482#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
cf61b747
L
9483 /* For x32, add a dummy REX_OPCODE prefix for mov/add with
9484 R_X86_64_GOTTPOFF relocation so that linker can safely
14470f07
L
9485 perform IE->LE optimization. A dummy REX_OPCODE prefix
9486 is also needed for lea with R_X86_64_GOTPC32_TLSDESC
9487 relocation for GDesc -> IE/LE optimization. */
cf61b747
L
9488 if (x86_elf_abi == X86_64_X32_ABI
9489 && i.operands == 2
14470f07
L
9490 && (i.reloc[0] == BFD_RELOC_X86_64_GOTTPOFF
9491 || i.reloc[0] == BFD_RELOC_X86_64_GOTPC32_TLSDESC)
cf61b747
L
9492 && i.prefix[REX_PREFIX] == 0)
9493 add_prefix (REX_OPCODE);
6d19a37a 9494#endif
cf61b747 9495
c0f3af97
L
9496 /* The prefix bytes. */
9497 for (j = ARRAY_SIZE (i.prefix), q = i.prefix; j > 0; j--, q++)
9498 if (*q)
48ef937e 9499 frag_opcode_byte (*q);
0f10071e 9500 }
ae5c1c7b 9501 else
c0f3af97
L
9502 {
9503 for (j = 0, q = i.prefix; j < ARRAY_SIZE (i.prefix); j++, q++)
9504 if (*q)
9505 switch (j)
9506 {
c0f3af97
L
9507 case SEG_PREFIX:
9508 case ADDR_PREFIX:
48ef937e 9509 frag_opcode_byte (*q);
c0f3af97
L
9510 break;
9511 default:
9512 /* There should be no other prefixes for instructions
9513 with VEX prefix. */
9514 abort ();
9515 }
9516
43234a1e
L
9517 /* For EVEX instructions i.vrex should become 0 after
9518 build_evex_prefix. For VEX instructions upper 16 registers
9519 aren't available, so VREX should be 0. */
9520 if (i.vrex)
9521 abort ();
c0f3af97 9522 /* Now the VEX prefix. */
48ef937e
JB
9523 if (now_seg != absolute_section)
9524 {
9525 p = frag_more (i.vex.length);
9526 for (j = 0; j < i.vex.length; j++)
9527 p[j] = i.vex.bytes[j];
9528 }
9529 else
9530 abs_section_offset += i.vex.length;
c0f3af97 9531 }
252b5132 9532
29b0f896 9533 /* Now the opcode; be careful about word order here! */
389d00a5
JB
9534 j = i.opcode_length;
9535 if (!i.vex.length)
9536 switch (i.tm.opcode_modifier.opcodespace)
9537 {
9538 case SPACE_BASE:
9539 break;
9540 case SPACE_0F:
9541 ++j;
9542 break;
9543 case SPACE_0F38:
9544 case SPACE_0F3A:
9545 j += 2;
9546 break;
9547 default:
9548 abort ();
9549 }
9550
48ef937e 9551 if (now_seg == absolute_section)
389d00a5
JB
9552 abs_section_offset += j;
9553 else if (j == 1)
29b0f896
AM
9554 {
9555 FRAG_APPEND_1_CHAR (i.tm.base_opcode);
9556 }
9557 else
9558 {
389d00a5
JB
9559 p = frag_more (j);
9560 if (!i.vex.length
9561 && i.tm.opcode_modifier.opcodespace != SPACE_BASE)
9562 {
9563 *p++ = 0x0f;
9564 if (i.tm.opcode_modifier.opcodespace != SPACE_0F)
9565 *p++ = i.tm.opcode_modifier.opcodespace == SPACE_0F38
9566 ? 0x38 : 0x3a;
9567 }
9568
9a182d04 9569 switch (i.opcode_length)
331d2d0d 9570 {
4dffcebc 9571 case 2:
389d00a5
JB
9572 /* Put out high byte first: can't use md_number_to_chars! */
9573 *p++ = (i.tm.base_opcode >> 8) & 0xff;
9574 /* Fall through. */
9575 case 1:
9576 *p = i.tm.base_opcode & 0xff;
4dffcebc
L
9577 break;
9578 default:
9579 abort ();
9580 break;
331d2d0d 9581 }
0f10071e 9582
29b0f896 9583 }
3e73aa7c 9584
29b0f896 9585 /* Now the modrm byte and sib byte (if present). */
40fb9820 9586 if (i.tm.opcode_modifier.modrm)
29b0f896 9587 {
48ef937e
JB
9588 frag_opcode_byte ((i.rm.regmem << 0)
9589 | (i.rm.reg << 3)
9590 | (i.rm.mode << 6));
29b0f896
AM
9591 /* If i.rm.regmem == ESP (4)
9592 && i.rm.mode != (Register mode)
9593 && not 16 bit
9594 ==> need second modrm byte. */
9595 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
9596 && i.rm.mode != 3
dc821c5f 9597 && !(i.base_reg && i.base_reg->reg_type.bitfield.word))
48ef937e
JB
9598 frag_opcode_byte ((i.sib.base << 0)
9599 | (i.sib.index << 3)
9600 | (i.sib.scale << 6));
29b0f896 9601 }
3e73aa7c 9602
29b0f896 9603 if (i.disp_operands)
2bbd9c25 9604 output_disp (insn_start_frag, insn_start_off);
3e73aa7c 9605
29b0f896 9606 if (i.imm_operands)
2bbd9c25 9607 output_imm (insn_start_frag, insn_start_off);
9c33702b
JB
9608
9609 /*
9610 * frag_now_fix () returning plain abs_section_offset when we're in the
9611 * absolute section, and abs_section_offset not getting updated as data
9612 * gets added to the frag breaks the logic below.
9613 */
9614 if (now_seg != absolute_section)
9615 {
9616 j = encoding_length (insn_start_frag, insn_start_off, frag_more (0));
9617 if (j > 15)
9618 as_warn (_("instruction length of %u bytes exceeds the limit of 15"),
9619 j);
e379e5f3
L
9620 else if (fragP)
9621 {
9622 /* NB: Don't add prefix with GOTPC relocation since
9623 output_disp() above depends on the fixed encoding
9624 length. Can't add prefix with TLS relocation since
9625 it breaks TLS linker optimization. */
9626 unsigned int max = i.has_gotpc_tls_reloc ? 0 : 15 - j;
9627 /* Prefix count on the current instruction. */
9628 unsigned int count = i.vex.length;
9629 unsigned int k;
9630 for (k = 0; k < ARRAY_SIZE (i.prefix); k++)
9631 /* REX byte is encoded in VEX/EVEX prefix. */
9632 if (i.prefix[k] && (k != REX_PREFIX || !i.vex.length))
9633 count++;
9634
9635 /* Count prefixes for extended opcode maps. */
9636 if (!i.vex.length)
389d00a5 9637 switch (i.tm.opcode_modifier.opcodespace)
e379e5f3 9638 {
389d00a5 9639 case SPACE_BASE:
e379e5f3 9640 break;
389d00a5
JB
9641 case SPACE_0F:
9642 count++;
e379e5f3 9643 break;
389d00a5
JB
9644 case SPACE_0F38:
9645 case SPACE_0F3A:
9646 count += 2;
e379e5f3
L
9647 break;
9648 default:
9649 abort ();
9650 }
9651
9652 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
9653 == BRANCH_PREFIX)
9654 {
9655 /* Set the maximum prefix size in BRANCH_PREFIX
9656 frag. */
9657 if (fragP->tc_frag_data.max_bytes > max)
9658 fragP->tc_frag_data.max_bytes = max;
9659 if (fragP->tc_frag_data.max_bytes > count)
9660 fragP->tc_frag_data.max_bytes -= count;
9661 else
9662 fragP->tc_frag_data.max_bytes = 0;
9663 }
9664 else
9665 {
9666 /* Remember the maximum prefix size in FUSED_JCC_PADDING
9667 frag. */
9668 unsigned int max_prefix_size;
9669 if (align_branch_prefix_size > max)
9670 max_prefix_size = max;
9671 else
9672 max_prefix_size = align_branch_prefix_size;
9673 if (max_prefix_size > count)
9674 fragP->tc_frag_data.max_prefix_length
9675 = max_prefix_size - count;
9676 }
9677
9678 /* Use existing segment prefix if possible. Use CS
9679 segment prefix in 64-bit mode. In 32-bit mode, use SS
9680 segment prefix with ESP/EBP base register and use DS
9681 segment prefix without ESP/EBP base register. */
9682 if (i.prefix[SEG_PREFIX])
9683 fragP->tc_frag_data.default_prefix = i.prefix[SEG_PREFIX];
9684 else if (flag_code == CODE_64BIT)
9685 fragP->tc_frag_data.default_prefix = CS_PREFIX_OPCODE;
9686 else if (i.base_reg
9687 && (i.base_reg->reg_num == 4
9688 || i.base_reg->reg_num == 5))
9689 fragP->tc_frag_data.default_prefix = SS_PREFIX_OPCODE;
9690 else
9691 fragP->tc_frag_data.default_prefix = DS_PREFIX_OPCODE;
9692 }
9c33702b 9693 }
29b0f896 9694 }
252b5132 9695
e379e5f3
L
9696 /* NB: Don't work with COND_JUMP86 without i386. */
9697 if (align_branch_power
9698 && now_seg != absolute_section
9699 && cpu_arch_flags.bitfield.cpui386)
9700 {
9701 /* Terminate each frag so that we can add prefix and check for
9702 fused jcc. */
9703 frag_wane (frag_now);
9704 frag_new (0);
9705 }
9706
29b0f896
AM
9707#ifdef DEBUG386
9708 if (flag_debug)
9709 {
7b81dfbb 9710 pi ("" /*line*/, &i);
29b0f896
AM
9711 }
9712#endif /* DEBUG386 */
9713}
252b5132 9714
e205caa7
L
9715/* Return the size of the displacement operand N. */
9716
9717static int
9718disp_size (unsigned int n)
9719{
9720 int size = 4;
43234a1e 9721
b5014f7a 9722 if (i.types[n].bitfield.disp64)
40fb9820
L
9723 size = 8;
9724 else if (i.types[n].bitfield.disp8)
9725 size = 1;
9726 else if (i.types[n].bitfield.disp16)
9727 size = 2;
e205caa7
L
9728 return size;
9729}
9730
9731/* Return the size of the immediate operand N. */
9732
9733static int
9734imm_size (unsigned int n)
9735{
9736 int size = 4;
40fb9820
L
9737 if (i.types[n].bitfield.imm64)
9738 size = 8;
9739 else if (i.types[n].bitfield.imm8 || i.types[n].bitfield.imm8s)
9740 size = 1;
9741 else if (i.types[n].bitfield.imm16)
9742 size = 2;
e205caa7
L
9743 return size;
9744}
9745
29b0f896 9746static void
64e74474 9747output_disp (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
9748{
9749 char *p;
9750 unsigned int n;
252b5132 9751
29b0f896
AM
9752 for (n = 0; n < i.operands; n++)
9753 {
b5014f7a 9754 if (operand_type_check (i.types[n], disp))
29b0f896 9755 {
48ef937e
JB
9756 int size = disp_size (n);
9757
9758 if (now_seg == absolute_section)
9759 abs_section_offset += size;
9760 else if (i.op[n].disps->X_op == O_constant)
29b0f896 9761 {
43234a1e 9762 offsetT val = i.op[n].disps->X_add_number;
252b5132 9763
629cfaf1
JB
9764 val = offset_in_range (val >> (size == 1 ? i.memshift : 0),
9765 size);
29b0f896
AM
9766 p = frag_more (size);
9767 md_number_to_chars (p, val, size);
9768 }
9769 else
9770 {
f86103b7 9771 enum bfd_reloc_code_real reloc_type;
40fb9820 9772 int sign = i.types[n].bitfield.disp32s;
29b0f896 9773 int pcrel = (i.flags[n] & Operand_PCrel) != 0;
02a86693 9774 fixS *fixP;
29b0f896 9775
e205caa7 9776 /* We can't have 8 bit displacement here. */
9c2799c2 9777 gas_assert (!i.types[n].bitfield.disp8);
e205caa7 9778
29b0f896
AM
9779 /* The PC relative address is computed relative
9780 to the instruction boundary, so in case immediate
9781 fields follows, we need to adjust the value. */
9782 if (pcrel && i.imm_operands)
9783 {
29b0f896 9784 unsigned int n1;
e205caa7 9785 int sz = 0;
252b5132 9786
29b0f896 9787 for (n1 = 0; n1 < i.operands; n1++)
40fb9820 9788 if (operand_type_check (i.types[n1], imm))
252b5132 9789 {
e205caa7
L
9790 /* Only one immediate is allowed for PC
9791 relative address. */
9c2799c2 9792 gas_assert (sz == 0);
e205caa7
L
9793 sz = imm_size (n1);
9794 i.op[n].disps->X_add_number -= sz;
252b5132 9795 }
29b0f896 9796 /* We should find the immediate. */
9c2799c2 9797 gas_assert (sz != 0);
29b0f896 9798 }
520dc8e8 9799
29b0f896 9800 p = frag_more (size);
d258b828 9801 reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
d6ab8113 9802 if (GOT_symbol
2bbd9c25 9803 && GOT_symbol == i.op[n].disps->X_add_symbol
d6ab8113 9804 && (((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
9805 || reloc_type == BFD_RELOC_X86_64_32S
9806 || (reloc_type == BFD_RELOC_64
9807 && object_64bit))
d6ab8113
JB
9808 && (i.op[n].disps->X_op == O_symbol
9809 || (i.op[n].disps->X_op == O_add
9810 && ((symbol_get_value_expression
9811 (i.op[n].disps->X_op_symbol)->X_op)
9812 == O_subtract))))
9813 || reloc_type == BFD_RELOC_32_PCREL))
2bbd9c25 9814 {
4fa24527 9815 if (!object_64bit)
7b81dfbb
AJ
9816 {
9817 reloc_type = BFD_RELOC_386_GOTPC;
5b7c81bd 9818 i.has_gotpc_tls_reloc = true;
d583596c
JB
9819 i.op[n].imms->X_add_number +=
9820 encoding_length (insn_start_frag, insn_start_off, p);
7b81dfbb
AJ
9821 }
9822 else if (reloc_type == BFD_RELOC_64)
9823 reloc_type = BFD_RELOC_X86_64_GOTPC64;
d6ab8113 9824 else
7b81dfbb
AJ
9825 /* Don't do the adjustment for x86-64, as there
9826 the pcrel addressing is relative to the _next_
9827 insn, and that is taken care of in other code. */
d6ab8113 9828 reloc_type = BFD_RELOC_X86_64_GOTPC32;
2bbd9c25 9829 }
e379e5f3
L
9830 else if (align_branch_power)
9831 {
9832 switch (reloc_type)
9833 {
9834 case BFD_RELOC_386_TLS_GD:
9835 case BFD_RELOC_386_TLS_LDM:
9836 case BFD_RELOC_386_TLS_IE:
9837 case BFD_RELOC_386_TLS_IE_32:
9838 case BFD_RELOC_386_TLS_GOTIE:
9839 case BFD_RELOC_386_TLS_GOTDESC:
9840 case BFD_RELOC_386_TLS_DESC_CALL:
9841 case BFD_RELOC_X86_64_TLSGD:
9842 case BFD_RELOC_X86_64_TLSLD:
9843 case BFD_RELOC_X86_64_GOTTPOFF:
9844 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
9845 case BFD_RELOC_X86_64_TLSDESC_CALL:
5b7c81bd 9846 i.has_gotpc_tls_reloc = true;
e379e5f3
L
9847 default:
9848 break;
9849 }
9850 }
02a86693
L
9851 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal,
9852 size, i.op[n].disps, pcrel,
9853 reloc_type);
9854 /* Check for "call/jmp *mem", "mov mem, %reg",
9855 "test %reg, mem" and "binop mem, %reg" where binop
9856 is one of adc, add, and, cmp, or, sbb, sub, xor
e60f4d3b
L
9857 instructions without data prefix. Always generate
9858 R_386_GOT32X for "sym*GOT" operand in 32-bit mode. */
9859 if (i.prefix[DATA_PREFIX] == 0
9860 && (generate_relax_relocations
9861 || (!object_64bit
9862 && i.rm.mode == 0
9863 && i.rm.regmem == 5))
0cb4071e
L
9864 && (i.rm.mode == 2
9865 || (i.rm.mode == 0 && i.rm.regmem == 5))
389d00a5 9866 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
02a86693
L
9867 && ((i.operands == 1
9868 && i.tm.base_opcode == 0xff
9869 && (i.rm.reg == 2 || i.rm.reg == 4))
9870 || (i.operands == 2
9871 && (i.tm.base_opcode == 0x8b
9872 || i.tm.base_opcode == 0x85
2ae4c703 9873 || (i.tm.base_opcode & ~0x38) == 0x03))))
02a86693
L
9874 {
9875 if (object_64bit)
9876 {
9877 fixP->fx_tcbit = i.rex != 0;
9878 if (i.base_reg
e968fc9b 9879 && (i.base_reg->reg_num == RegIP))
02a86693
L
9880 fixP->fx_tcbit2 = 1;
9881 }
9882 else
9883 fixP->fx_tcbit2 = 1;
9884 }
29b0f896
AM
9885 }
9886 }
9887 }
9888}
252b5132 9889
29b0f896 9890static void
64e74474 9891output_imm (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
9892{
9893 char *p;
9894 unsigned int n;
252b5132 9895
29b0f896
AM
9896 for (n = 0; n < i.operands; n++)
9897 {
43234a1e 9898 /* Skip SAE/RC Imm operand in EVEX. They are already handled. */
ca5312a2 9899 if (i.rounding.type != rc_none && n == i.rounding.operand)
43234a1e
L
9900 continue;
9901
40fb9820 9902 if (operand_type_check (i.types[n], imm))
29b0f896 9903 {
48ef937e
JB
9904 int size = imm_size (n);
9905
9906 if (now_seg == absolute_section)
9907 abs_section_offset += size;
9908 else if (i.op[n].imms->X_op == O_constant)
29b0f896 9909 {
29b0f896 9910 offsetT val;
b4cac588 9911
29b0f896
AM
9912 val = offset_in_range (i.op[n].imms->X_add_number,
9913 size);
9914 p = frag_more (size);
9915 md_number_to_chars (p, val, size);
9916 }
9917 else
9918 {
9919 /* Not absolute_section.
9920 Need a 32-bit fixup (don't support 8bit
9921 non-absolute imms). Try to support other
9922 sizes ... */
f86103b7 9923 enum bfd_reloc_code_real reloc_type;
e205caa7 9924 int sign;
29b0f896 9925
40fb9820 9926 if (i.types[n].bitfield.imm32s
a7d61044 9927 && (i.suffix == QWORD_MNEM_SUFFIX
40fb9820 9928 || (!i.suffix && i.tm.opcode_modifier.no_lsuf)))
29b0f896 9929 sign = 1;
e205caa7
L
9930 else
9931 sign = 0;
520dc8e8 9932
29b0f896 9933 p = frag_more (size);
d258b828 9934 reloc_type = reloc (size, 0, sign, i.reloc[n]);
f86103b7 9935
2bbd9c25
JJ
9936 /* This is tough to explain. We end up with this one if we
9937 * have operands that look like
9938 * "_GLOBAL_OFFSET_TABLE_+[.-.L284]". The goal here is to
9939 * obtain the absolute address of the GOT, and it is strongly
9940 * preferable from a performance point of view to avoid using
9941 * a runtime relocation for this. The actual sequence of
9942 * instructions often look something like:
9943 *
9944 * call .L66
9945 * .L66:
9946 * popl %ebx
9947 * addl $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
9948 *
9949 * The call and pop essentially return the absolute address
9950 * of the label .L66 and store it in %ebx. The linker itself
9951 * will ultimately change the first operand of the addl so
9952 * that %ebx points to the GOT, but to keep things simple, the
9953 * .o file must have this operand set so that it generates not
9954 * the absolute address of .L66, but the absolute address of
9955 * itself. This allows the linker itself simply treat a GOTPC
9956 * relocation as asking for a pcrel offset to the GOT to be
9957 * added in, and the addend of the relocation is stored in the
9958 * operand field for the instruction itself.
9959 *
9960 * Our job here is to fix the operand so that it would add
9961 * the correct offset so that %ebx would point to itself. The
9962 * thing that is tricky is that .-.L66 will point to the
9963 * beginning of the instruction, so we need to further modify
9964 * the operand so that it will point to itself. There are
9965 * other cases where you have something like:
9966 *
9967 * .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
9968 *
9969 * and here no correction would be required. Internally in
9970 * the assembler we treat operands of this form as not being
9971 * pcrel since the '.' is explicitly mentioned, and I wonder
9972 * whether it would simplify matters to do it this way. Who
9973 * knows. In earlier versions of the PIC patches, the
9974 * pcrel_adjust field was used to store the correction, but
9975 * since the expression is not pcrel, I felt it would be
9976 * confusing to do it this way. */
9977
d6ab8113 9978 if ((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
9979 || reloc_type == BFD_RELOC_X86_64_32S
9980 || reloc_type == BFD_RELOC_64)
29b0f896
AM
9981 && GOT_symbol
9982 && GOT_symbol == i.op[n].imms->X_add_symbol
9983 && (i.op[n].imms->X_op == O_symbol
9984 || (i.op[n].imms->X_op == O_add
9985 && ((symbol_get_value_expression
9986 (i.op[n].imms->X_op_symbol)->X_op)
9987 == O_subtract))))
9988 {
4fa24527 9989 if (!object_64bit)
d6ab8113 9990 reloc_type = BFD_RELOC_386_GOTPC;
7b81dfbb 9991 else if (size == 4)
d6ab8113 9992 reloc_type = BFD_RELOC_X86_64_GOTPC32;
7b81dfbb
AJ
9993 else if (size == 8)
9994 reloc_type = BFD_RELOC_X86_64_GOTPC64;
5b7c81bd 9995 i.has_gotpc_tls_reloc = true;
d583596c
JB
9996 i.op[n].imms->X_add_number +=
9997 encoding_length (insn_start_frag, insn_start_off, p);
29b0f896 9998 }
29b0f896
AM
9999 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
10000 i.op[n].imms, 0, reloc_type);
10001 }
10002 }
10003 }
252b5132
RH
10004}
10005\f
d182319b
JB
10006/* x86_cons_fix_new is called via the expression parsing code when a
10007 reloc is needed. We use this hook to get the correct .got reloc. */
d182319b
JB
10008static int cons_sign = -1;
10009
10010void
e3bb37b5 10011x86_cons_fix_new (fragS *frag, unsigned int off, unsigned int len,
62ebcb5c 10012 expressionS *exp, bfd_reloc_code_real_type r)
d182319b 10013{
d258b828 10014 r = reloc (len, 0, cons_sign, r);
d182319b
JB
10015
10016#ifdef TE_PE
10017 if (exp->X_op == O_secrel)
10018 {
10019 exp->X_op = O_symbol;
10020 r = BFD_RELOC_32_SECREL;
10021 }
10022#endif
10023
10024 fix_new_exp (frag, off, len, exp, 0, r);
10025}
10026
357d1bd8
L
10027/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
10028 purpose of the `.dc.a' internal pseudo-op. */
10029
10030int
10031x86_address_bytes (void)
10032{
10033 if ((stdoutput->arch_info->mach & bfd_mach_x64_32))
10034 return 4;
10035 return stdoutput->arch_info->bits_per_address / 8;
10036}
10037
d382c579
TG
10038#if !(defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined (OBJ_MACH_O)) \
10039 || defined (LEX_AT)
d258b828 10040# define lex_got(reloc, adjust, types) NULL
718ddfc0 10041#else
f3c180ae
AM
10042/* Parse operands of the form
10043 <symbol>@GOTOFF+<nnn>
10044 and similar .plt or .got references.
10045
10046 If we find one, set up the correct relocation in RELOC and copy the
10047 input string, minus the `@GOTOFF' into a malloc'd buffer for
10048 parsing by the calling routine. Return this buffer, and if ADJUST
10049 is non-null set it to the length of the string we removed from the
10050 input line. Otherwise return NULL. */
10051static char *
91d6fa6a 10052lex_got (enum bfd_reloc_code_real *rel,
64e74474 10053 int *adjust,
d258b828 10054 i386_operand_type *types)
f3c180ae 10055{
7b81dfbb
AJ
10056 /* Some of the relocations depend on the size of what field is to
10057 be relocated. But in our callers i386_immediate and i386_displacement
10058 we don't yet know the operand size (this will be set by insn
10059 matching). Hence we record the word32 relocation here,
10060 and adjust the reloc according to the real size in reloc(). */
f3c180ae
AM
10061 static const struct {
10062 const char *str;
cff8d58a 10063 int len;
4fa24527 10064 const enum bfd_reloc_code_real rel[2];
40fb9820 10065 const i386_operand_type types64;
5b7c81bd 10066 bool need_GOT_symbol;
f3c180ae 10067 } gotrel[] = {
8ce3d284 10068#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
10069 { STRING_COMMA_LEN ("SIZE"), { BFD_RELOC_SIZE32,
10070 BFD_RELOC_SIZE32 },
5b7c81bd 10071 OPERAND_TYPE_IMM32_64, false },
8ce3d284 10072#endif
cff8d58a
L
10073 { STRING_COMMA_LEN ("PLTOFF"), { _dummy_first_bfd_reloc_code_real,
10074 BFD_RELOC_X86_64_PLTOFF64 },
5b7c81bd 10075 OPERAND_TYPE_IMM64, true },
cff8d58a
L
10076 { STRING_COMMA_LEN ("PLT"), { BFD_RELOC_386_PLT32,
10077 BFD_RELOC_X86_64_PLT32 },
5b7c81bd 10078 OPERAND_TYPE_IMM32_32S_DISP32, false },
cff8d58a
L
10079 { STRING_COMMA_LEN ("GOTPLT"), { _dummy_first_bfd_reloc_code_real,
10080 BFD_RELOC_X86_64_GOTPLT64 },
5b7c81bd 10081 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
10082 { STRING_COMMA_LEN ("GOTOFF"), { BFD_RELOC_386_GOTOFF,
10083 BFD_RELOC_X86_64_GOTOFF64 },
5b7c81bd 10084 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
10085 { STRING_COMMA_LEN ("GOTPCREL"), { _dummy_first_bfd_reloc_code_real,
10086 BFD_RELOC_X86_64_GOTPCREL },
5b7c81bd 10087 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10088 { STRING_COMMA_LEN ("TLSGD"), { BFD_RELOC_386_TLS_GD,
10089 BFD_RELOC_X86_64_TLSGD },
5b7c81bd 10090 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10091 { STRING_COMMA_LEN ("TLSLDM"), { BFD_RELOC_386_TLS_LDM,
10092 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10093 OPERAND_TYPE_NONE, true },
cff8d58a
L
10094 { STRING_COMMA_LEN ("TLSLD"), { _dummy_first_bfd_reloc_code_real,
10095 BFD_RELOC_X86_64_TLSLD },
5b7c81bd 10096 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10097 { STRING_COMMA_LEN ("GOTTPOFF"), { BFD_RELOC_386_TLS_IE_32,
10098 BFD_RELOC_X86_64_GOTTPOFF },
5b7c81bd 10099 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10100 { STRING_COMMA_LEN ("TPOFF"), { BFD_RELOC_386_TLS_LE_32,
10101 BFD_RELOC_X86_64_TPOFF32 },
5b7c81bd 10102 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
10103 { STRING_COMMA_LEN ("NTPOFF"), { BFD_RELOC_386_TLS_LE,
10104 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10105 OPERAND_TYPE_NONE, true },
cff8d58a
L
10106 { STRING_COMMA_LEN ("DTPOFF"), { BFD_RELOC_386_TLS_LDO_32,
10107 BFD_RELOC_X86_64_DTPOFF32 },
5b7c81bd 10108 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
10109 { STRING_COMMA_LEN ("GOTNTPOFF"),{ BFD_RELOC_386_TLS_GOTIE,
10110 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10111 OPERAND_TYPE_NONE, true },
cff8d58a
L
10112 { STRING_COMMA_LEN ("INDNTPOFF"),{ BFD_RELOC_386_TLS_IE,
10113 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10114 OPERAND_TYPE_NONE, true },
cff8d58a
L
10115 { STRING_COMMA_LEN ("GOT"), { BFD_RELOC_386_GOT32,
10116 BFD_RELOC_X86_64_GOT32 },
5b7c81bd 10117 OPERAND_TYPE_IMM32_32S_64_DISP32, true },
cff8d58a
L
10118 { STRING_COMMA_LEN ("TLSDESC"), { BFD_RELOC_386_TLS_GOTDESC,
10119 BFD_RELOC_X86_64_GOTPC32_TLSDESC },
5b7c81bd 10120 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10121 { STRING_COMMA_LEN ("TLSCALL"), { BFD_RELOC_386_TLS_DESC_CALL,
10122 BFD_RELOC_X86_64_TLSDESC_CALL },
5b7c81bd 10123 OPERAND_TYPE_IMM32_32S_DISP32, true },
f3c180ae
AM
10124 };
10125 char *cp;
10126 unsigned int j;
10127
d382c579 10128#if defined (OBJ_MAYBE_ELF)
718ddfc0
JB
10129 if (!IS_ELF)
10130 return NULL;
d382c579 10131#endif
718ddfc0 10132
f3c180ae 10133 for (cp = input_line_pointer; *cp != '@'; cp++)
67c11a9b 10134 if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
f3c180ae
AM
10135 return NULL;
10136
47465058 10137 for (j = 0; j < ARRAY_SIZE (gotrel); j++)
f3c180ae 10138 {
cff8d58a 10139 int len = gotrel[j].len;
28f81592 10140 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
f3c180ae 10141 {
4fa24527 10142 if (gotrel[j].rel[object_64bit] != 0)
f3c180ae 10143 {
28f81592
AM
10144 int first, second;
10145 char *tmpbuf, *past_reloc;
f3c180ae 10146
91d6fa6a 10147 *rel = gotrel[j].rel[object_64bit];
f3c180ae 10148
3956db08
JB
10149 if (types)
10150 {
10151 if (flag_code != CODE_64BIT)
40fb9820
L
10152 {
10153 types->bitfield.imm32 = 1;
10154 types->bitfield.disp32 = 1;
10155 }
3956db08
JB
10156 else
10157 *types = gotrel[j].types64;
10158 }
10159
844bf810 10160 if (gotrel[j].need_GOT_symbol && GOT_symbol == NULL)
f3c180ae
AM
10161 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
10162
28f81592 10163 /* The length of the first part of our input line. */
f3c180ae 10164 first = cp - input_line_pointer;
28f81592
AM
10165
10166 /* The second part goes from after the reloc token until
67c11a9b 10167 (and including) an end_of_line char or comma. */
28f81592 10168 past_reloc = cp + 1 + len;
67c11a9b
AM
10169 cp = past_reloc;
10170 while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
10171 ++cp;
10172 second = cp + 1 - past_reloc;
28f81592
AM
10173
10174 /* Allocate and copy string. The trailing NUL shouldn't
10175 be necessary, but be safe. */
add39d23 10176 tmpbuf = XNEWVEC (char, first + second + 2);
f3c180ae 10177 memcpy (tmpbuf, input_line_pointer, first);
0787a12d
AM
10178 if (second != 0 && *past_reloc != ' ')
10179 /* Replace the relocation token with ' ', so that
10180 errors like foo@GOTOFF1 will be detected. */
10181 tmpbuf[first++] = ' ';
af89796a
L
10182 else
10183 /* Increment length by 1 if the relocation token is
10184 removed. */
10185 len++;
10186 if (adjust)
10187 *adjust = len;
0787a12d
AM
10188 memcpy (tmpbuf + first, past_reloc, second);
10189 tmpbuf[first + second] = '\0';
f3c180ae
AM
10190 return tmpbuf;
10191 }
10192
4fa24527
JB
10193 as_bad (_("@%s reloc is not supported with %d-bit output format"),
10194 gotrel[j].str, 1 << (5 + object_64bit));
f3c180ae
AM
10195 return NULL;
10196 }
10197 }
10198
10199 /* Might be a symbol version string. Don't as_bad here. */
10200 return NULL;
10201}
4e4f7c87 10202#endif
f3c180ae 10203
a988325c
NC
10204#ifdef TE_PE
10205#ifdef lex_got
10206#undef lex_got
10207#endif
10208/* Parse operands of the form
10209 <symbol>@SECREL32+<nnn>
10210
10211 If we find one, set up the correct relocation in RELOC and copy the
10212 input string, minus the `@SECREL32' into a malloc'd buffer for
10213 parsing by the calling routine. Return this buffer, and if ADJUST
10214 is non-null set it to the length of the string we removed from the
34bca508
L
10215 input line. Otherwise return NULL.
10216
a988325c
NC
10217 This function is copied from the ELF version above adjusted for PE targets. */
10218
10219static char *
10220lex_got (enum bfd_reloc_code_real *rel ATTRIBUTE_UNUSED,
10221 int *adjust ATTRIBUTE_UNUSED,
d258b828 10222 i386_operand_type *types)
a988325c
NC
10223{
10224 static const struct
10225 {
10226 const char *str;
10227 int len;
10228 const enum bfd_reloc_code_real rel[2];
10229 const i386_operand_type types64;
10230 }
10231 gotrel[] =
10232 {
10233 { STRING_COMMA_LEN ("SECREL32"), { BFD_RELOC_32_SECREL,
10234 BFD_RELOC_32_SECREL },
10235 OPERAND_TYPE_IMM32_32S_64_DISP32_64 },
10236 };
10237
10238 char *cp;
10239 unsigned j;
10240
10241 for (cp = input_line_pointer; *cp != '@'; cp++)
10242 if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
10243 return NULL;
10244
10245 for (j = 0; j < ARRAY_SIZE (gotrel); j++)
10246 {
10247 int len = gotrel[j].len;
10248
10249 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
10250 {
10251 if (gotrel[j].rel[object_64bit] != 0)
10252 {
10253 int first, second;
10254 char *tmpbuf, *past_reloc;
10255
10256 *rel = gotrel[j].rel[object_64bit];
10257 if (adjust)
10258 *adjust = len;
10259
10260 if (types)
10261 {
10262 if (flag_code != CODE_64BIT)
10263 {
10264 types->bitfield.imm32 = 1;
10265 types->bitfield.disp32 = 1;
10266 }
10267 else
10268 *types = gotrel[j].types64;
10269 }
10270
10271 /* The length of the first part of our input line. */
10272 first = cp - input_line_pointer;
10273
10274 /* The second part goes from after the reloc token until
10275 (and including) an end_of_line char or comma. */
10276 past_reloc = cp + 1 + len;
10277 cp = past_reloc;
10278 while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
10279 ++cp;
10280 second = cp + 1 - past_reloc;
10281
10282 /* Allocate and copy string. The trailing NUL shouldn't
10283 be necessary, but be safe. */
add39d23 10284 tmpbuf = XNEWVEC (char, first + second + 2);
a988325c
NC
10285 memcpy (tmpbuf, input_line_pointer, first);
10286 if (second != 0 && *past_reloc != ' ')
10287 /* Replace the relocation token with ' ', so that
10288 errors like foo@SECLREL321 will be detected. */
10289 tmpbuf[first++] = ' ';
10290 memcpy (tmpbuf + first, past_reloc, second);
10291 tmpbuf[first + second] = '\0';
10292 return tmpbuf;
10293 }
10294
10295 as_bad (_("@%s reloc is not supported with %d-bit output format"),
10296 gotrel[j].str, 1 << (5 + object_64bit));
10297 return NULL;
10298 }
10299 }
10300
10301 /* Might be a symbol version string. Don't as_bad here. */
10302 return NULL;
10303}
10304
10305#endif /* TE_PE */
10306
62ebcb5c 10307bfd_reloc_code_real_type
e3bb37b5 10308x86_cons (expressionS *exp, int size)
f3c180ae 10309{
62ebcb5c
AM
10310 bfd_reloc_code_real_type got_reloc = NO_RELOC;
10311
ee86248c
JB
10312 intel_syntax = -intel_syntax;
10313
3c7b9c2c 10314 exp->X_md = 0;
4fa24527 10315 if (size == 4 || (object_64bit && size == 8))
f3c180ae
AM
10316 {
10317 /* Handle @GOTOFF and the like in an expression. */
10318 char *save;
10319 char *gotfree_input_line;
4a57f2cf 10320 int adjust = 0;
f3c180ae
AM
10321
10322 save = input_line_pointer;
d258b828 10323 gotfree_input_line = lex_got (&got_reloc, &adjust, NULL);
f3c180ae
AM
10324 if (gotfree_input_line)
10325 input_line_pointer = gotfree_input_line;
10326
10327 expression (exp);
10328
10329 if (gotfree_input_line)
10330 {
10331 /* expression () has merrily parsed up to the end of line,
10332 or a comma - in the wrong buffer. Transfer how far
10333 input_line_pointer has moved to the right buffer. */
10334 input_line_pointer = (save
10335 + (input_line_pointer - gotfree_input_line)
10336 + adjust);
10337 free (gotfree_input_line);
3992d3b7
AM
10338 if (exp->X_op == O_constant
10339 || exp->X_op == O_absent
10340 || exp->X_op == O_illegal
0398aac5 10341 || exp->X_op == O_register
3992d3b7
AM
10342 || exp->X_op == O_big)
10343 {
10344 char c = *input_line_pointer;
10345 *input_line_pointer = 0;
10346 as_bad (_("missing or invalid expression `%s'"), save);
10347 *input_line_pointer = c;
10348 }
b9519cfe
L
10349 else if ((got_reloc == BFD_RELOC_386_PLT32
10350 || got_reloc == BFD_RELOC_X86_64_PLT32)
10351 && exp->X_op != O_symbol)
10352 {
10353 char c = *input_line_pointer;
10354 *input_line_pointer = 0;
10355 as_bad (_("invalid PLT expression `%s'"), save);
10356 *input_line_pointer = c;
10357 }
f3c180ae
AM
10358 }
10359 }
10360 else
10361 expression (exp);
ee86248c
JB
10362
10363 intel_syntax = -intel_syntax;
10364
10365 if (intel_syntax)
10366 i386_intel_simplify (exp);
62ebcb5c
AM
10367
10368 return got_reloc;
f3c180ae 10369}
f3c180ae 10370
9f32dd5b
L
10371static void
10372signed_cons (int size)
6482c264 10373{
d182319b
JB
10374 if (flag_code == CODE_64BIT)
10375 cons_sign = 1;
10376 cons (size);
10377 cons_sign = -1;
6482c264
NC
10378}
10379
d182319b 10380#ifdef TE_PE
6482c264 10381static void
7016a5d5 10382pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
6482c264
NC
10383{
10384 expressionS exp;
10385
10386 do
10387 {
10388 expression (&exp);
10389 if (exp.X_op == O_symbol)
10390 exp.X_op = O_secrel;
10391
10392 emit_expr (&exp, 4);
10393 }
10394 while (*input_line_pointer++ == ',');
10395
10396 input_line_pointer--;
10397 demand_empty_rest_of_line ();
10398}
6482c264
NC
10399#endif
10400
43234a1e
L
10401/* Handle Vector operations. */
10402
10403static char *
10404check_VecOperations (char *op_string, char *op_end)
10405{
10406 const reg_entry *mask;
10407 const char *saved;
10408 char *end_op;
10409
10410 while (*op_string
10411 && (op_end == NULL || op_string < op_end))
10412 {
10413 saved = op_string;
10414 if (*op_string == '{')
10415 {
10416 op_string++;
10417
10418 /* Check broadcasts. */
10419 if (strncmp (op_string, "1to", 3) == 0)
10420 {
5273a3cd 10421 unsigned int bcst_type;
43234a1e 10422
5273a3cd 10423 if (i.broadcast.type)
43234a1e
L
10424 goto duplicated_vec_op;
10425
10426 op_string += 3;
10427 if (*op_string == '8')
8e6e0792 10428 bcst_type = 8;
b28d1bda 10429 else if (*op_string == '4')
8e6e0792 10430 bcst_type = 4;
b28d1bda 10431 else if (*op_string == '2')
8e6e0792 10432 bcst_type = 2;
43234a1e
L
10433 else if (*op_string == '1'
10434 && *(op_string+1) == '6')
10435 {
8e6e0792 10436 bcst_type = 16;
43234a1e
L
10437 op_string++;
10438 }
10439 else
10440 {
10441 as_bad (_("Unsupported broadcast: `%s'"), saved);
10442 return NULL;
10443 }
10444 op_string++;
10445
5273a3cd
JB
10446 i.broadcast.type = bcst_type;
10447 i.broadcast.operand = this_operand;
43234a1e
L
10448 }
10449 /* Check masking operation. */
10450 else if ((mask = parse_register (op_string, &end_op)) != NULL)
10451 {
8a6fb3f9
JB
10452 if (mask == &bad_reg)
10453 return NULL;
10454
43234a1e 10455 /* k0 can't be used for write mask. */
f74a6307 10456 if (mask->reg_type.bitfield.class != RegMask || !mask->reg_num)
43234a1e 10457 {
6d2cd6b2
JB
10458 as_bad (_("`%s%s' can't be used for write mask"),
10459 register_prefix, mask->reg_name);
43234a1e
L
10460 return NULL;
10461 }
10462
6225c532 10463 if (!i.mask.reg)
43234a1e 10464 {
6225c532
JB
10465 i.mask.reg = mask;
10466 i.mask.operand = this_operand;
43234a1e 10467 }
6225c532
JB
10468 else if (i.mask.reg->reg_num)
10469 goto duplicated_vec_op;
43234a1e
L
10470 else
10471 {
6225c532 10472 i.mask.reg = mask;
43234a1e
L
10473
10474 /* Only "{z}" is allowed here. No need to check
10475 zeroing mask explicitly. */
6225c532 10476 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
10477 {
10478 as_bad (_("invalid write mask `%s'"), saved);
10479 return NULL;
10480 }
10481 }
10482
10483 op_string = end_op;
10484 }
10485 /* Check zeroing-flag for masking operation. */
10486 else if (*op_string == 'z')
10487 {
6225c532 10488 if (!i.mask.reg)
43234a1e 10489 {
6225c532
JB
10490 i.mask.reg = reg_k0;
10491 i.mask.zeroing = 1;
10492 i.mask.operand = this_operand;
43234a1e
L
10493 }
10494 else
10495 {
6225c532 10496 if (i.mask.zeroing)
43234a1e
L
10497 {
10498 duplicated_vec_op:
10499 as_bad (_("duplicated `%s'"), saved);
10500 return NULL;
10501 }
10502
6225c532 10503 i.mask.zeroing = 1;
43234a1e
L
10504
10505 /* Only "{%k}" is allowed here. No need to check mask
10506 register explicitly. */
6225c532 10507 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
10508 {
10509 as_bad (_("invalid zeroing-masking `%s'"),
10510 saved);
10511 return NULL;
10512 }
10513 }
10514
10515 op_string++;
10516 }
10517 else
10518 goto unknown_vec_op;
10519
10520 if (*op_string != '}')
10521 {
10522 as_bad (_("missing `}' in `%s'"), saved);
10523 return NULL;
10524 }
10525 op_string++;
0ba3a731
L
10526
10527 /* Strip whitespace since the addition of pseudo prefixes
10528 changed how the scrubber treats '{'. */
10529 if (is_space_char (*op_string))
10530 ++op_string;
10531
43234a1e
L
10532 continue;
10533 }
10534 unknown_vec_op:
10535 /* We don't know this one. */
10536 as_bad (_("unknown vector operation: `%s'"), saved);
10537 return NULL;
10538 }
10539
6225c532 10540 if (i.mask.reg && i.mask.zeroing && !i.mask.reg->reg_num)
6d2cd6b2
JB
10541 {
10542 as_bad (_("zeroing-masking only allowed with write mask"));
10543 return NULL;
10544 }
10545
43234a1e
L
10546 return op_string;
10547}
10548
252b5132 10549static int
70e41ade 10550i386_immediate (char *imm_start)
252b5132
RH
10551{
10552 char *save_input_line_pointer;
f3c180ae 10553 char *gotfree_input_line;
252b5132 10554 segT exp_seg = 0;
47926f60 10555 expressionS *exp;
40fb9820
L
10556 i386_operand_type types;
10557
0dfbf9d7 10558 operand_type_set (&types, ~0);
252b5132
RH
10559
10560 if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
10561 {
31b2323c
L
10562 as_bad (_("at most %d immediate operands are allowed"),
10563 MAX_IMMEDIATE_OPERANDS);
252b5132
RH
10564 return 0;
10565 }
10566
10567 exp = &im_expressions[i.imm_operands++];
520dc8e8 10568 i.op[this_operand].imms = exp;
252b5132
RH
10569
10570 if (is_space_char (*imm_start))
10571 ++imm_start;
10572
10573 save_input_line_pointer = input_line_pointer;
10574 input_line_pointer = imm_start;
10575
d258b828 10576 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
10577 if (gotfree_input_line)
10578 input_line_pointer = gotfree_input_line;
252b5132
RH
10579
10580 exp_seg = expression (exp);
10581
83183c0c 10582 SKIP_WHITESPACE ();
43234a1e
L
10583
10584 /* Handle vector operations. */
10585 if (*input_line_pointer == '{')
10586 {
10587 input_line_pointer = check_VecOperations (input_line_pointer,
10588 NULL);
10589 if (input_line_pointer == NULL)
10590 return 0;
10591 }
10592
252b5132 10593 if (*input_line_pointer)
f3c180ae 10594 as_bad (_("junk `%s' after expression"), input_line_pointer);
252b5132
RH
10595
10596 input_line_pointer = save_input_line_pointer;
f3c180ae 10597 if (gotfree_input_line)
ee86248c
JB
10598 {
10599 free (gotfree_input_line);
10600
10601 if (exp->X_op == O_constant || exp->X_op == O_register)
10602 exp->X_op = O_illegal;
10603 }
10604
10605 return i386_finalize_immediate (exp_seg, exp, types, imm_start);
10606}
252b5132 10607
ee86248c
JB
10608static int
10609i386_finalize_immediate (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
10610 i386_operand_type types, const char *imm_start)
10611{
10612 if (exp->X_op == O_absent || exp->X_op == O_illegal || exp->X_op == O_big)
252b5132 10613 {
313c53d1
L
10614 if (imm_start)
10615 as_bad (_("missing or invalid immediate expression `%s'"),
10616 imm_start);
3992d3b7 10617 return 0;
252b5132 10618 }
3e73aa7c 10619 else if (exp->X_op == O_constant)
252b5132 10620 {
47926f60 10621 /* Size it properly later. */
40fb9820 10622 i.types[this_operand].bitfield.imm64 = 1;
13f864ae
L
10623 /* If not 64bit, sign extend val. */
10624 if (flag_code != CODE_64BIT
4eed87de
AM
10625 && (exp->X_add_number & ~(((addressT) 2 << 31) - 1)) == 0)
10626 exp->X_add_number
10627 = (exp->X_add_number ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
252b5132 10628 }
4c63da97 10629#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
f86103b7 10630 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
31312f95 10631 && exp_seg != absolute_section
47926f60 10632 && exp_seg != text_section
24eab124
AM
10633 && exp_seg != data_section
10634 && exp_seg != bss_section
10635 && exp_seg != undefined_section
f86103b7 10636 && !bfd_is_com_section (exp_seg))
252b5132 10637 {
d0b47220 10638 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
252b5132
RH
10639 return 0;
10640 }
10641#endif
a841bdf5 10642 else if (!intel_syntax && exp_seg == reg_section)
bb8f5920 10643 {
313c53d1
L
10644 if (imm_start)
10645 as_bad (_("illegal immediate register operand %s"), imm_start);
bb8f5920
L
10646 return 0;
10647 }
252b5132
RH
10648 else
10649 {
10650 /* This is an address. The size of the address will be
24eab124 10651 determined later, depending on destination register,
3e73aa7c 10652 suffix, or the default for the section. */
40fb9820
L
10653 i.types[this_operand].bitfield.imm8 = 1;
10654 i.types[this_operand].bitfield.imm16 = 1;
10655 i.types[this_operand].bitfield.imm32 = 1;
10656 i.types[this_operand].bitfield.imm32s = 1;
10657 i.types[this_operand].bitfield.imm64 = 1;
c6fb90c8
L
10658 i.types[this_operand] = operand_type_and (i.types[this_operand],
10659 types);
252b5132
RH
10660 }
10661
10662 return 1;
10663}
10664
551c1ca1 10665static char *
e3bb37b5 10666i386_scale (char *scale)
252b5132 10667{
551c1ca1
AM
10668 offsetT val;
10669 char *save = input_line_pointer;
252b5132 10670
551c1ca1
AM
10671 input_line_pointer = scale;
10672 val = get_absolute_expression ();
10673
10674 switch (val)
252b5132 10675 {
551c1ca1 10676 case 1:
252b5132
RH
10677 i.log2_scale_factor = 0;
10678 break;
551c1ca1 10679 case 2:
252b5132
RH
10680 i.log2_scale_factor = 1;
10681 break;
551c1ca1 10682 case 4:
252b5132
RH
10683 i.log2_scale_factor = 2;
10684 break;
551c1ca1 10685 case 8:
252b5132
RH
10686 i.log2_scale_factor = 3;
10687 break;
10688 default:
a724f0f4
JB
10689 {
10690 char sep = *input_line_pointer;
10691
10692 *input_line_pointer = '\0';
10693 as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
10694 scale);
10695 *input_line_pointer = sep;
10696 input_line_pointer = save;
10697 return NULL;
10698 }
252b5132 10699 }
29b0f896 10700 if (i.log2_scale_factor != 0 && i.index_reg == 0)
252b5132
RH
10701 {
10702 as_warn (_("scale factor of %d without an index register"),
24eab124 10703 1 << i.log2_scale_factor);
252b5132 10704 i.log2_scale_factor = 0;
252b5132 10705 }
551c1ca1
AM
10706 scale = input_line_pointer;
10707 input_line_pointer = save;
10708 return scale;
252b5132
RH
10709}
10710
252b5132 10711static int
e3bb37b5 10712i386_displacement (char *disp_start, char *disp_end)
252b5132 10713{
29b0f896 10714 expressionS *exp;
252b5132
RH
10715 segT exp_seg = 0;
10716 char *save_input_line_pointer;
f3c180ae 10717 char *gotfree_input_line;
40fb9820
L
10718 int override;
10719 i386_operand_type bigdisp, types = anydisp;
3992d3b7 10720 int ret;
252b5132 10721
31b2323c
L
10722 if (i.disp_operands == MAX_MEMORY_OPERANDS)
10723 {
10724 as_bad (_("at most %d displacement operands are allowed"),
10725 MAX_MEMORY_OPERANDS);
10726 return 0;
10727 }
10728
0dfbf9d7 10729 operand_type_set (&bigdisp, 0);
6f2f06be 10730 if (i.jumpabsolute
48bcea9f 10731 || i.types[this_operand].bitfield.baseindex
0cfa3eb3
JB
10732 || (current_templates->start->opcode_modifier.jump != JUMP
10733 && current_templates->start->opcode_modifier.jump != JUMP_DWORD))
e05278af 10734 {
48bcea9f 10735 i386_addressing_mode ();
e05278af 10736 override = (i.prefix[ADDR_PREFIX] != 0);
40fb9820
L
10737 if (flag_code == CODE_64BIT)
10738 {
10739 if (!override)
10740 {
10741 bigdisp.bitfield.disp32s = 1;
10742 bigdisp.bitfield.disp64 = 1;
10743 }
48bcea9f
JB
10744 else
10745 bigdisp.bitfield.disp32 = 1;
40fb9820
L
10746 }
10747 else if ((flag_code == CODE_16BIT) ^ override)
40fb9820 10748 bigdisp.bitfield.disp16 = 1;
48bcea9f
JB
10749 else
10750 bigdisp.bitfield.disp32 = 1;
e05278af
JB
10751 }
10752 else
10753 {
376cd056
JB
10754 /* For PC-relative branches, the width of the displacement may be
10755 dependent upon data size, but is never dependent upon address size.
10756 Also make sure to not unintentionally match against a non-PC-relative
10757 branch template. */
10758 static templates aux_templates;
10759 const insn_template *t = current_templates->start;
5b7c81bd 10760 bool has_intel64 = false;
376cd056
JB
10761
10762 aux_templates.start = t;
10763 while (++t < current_templates->end)
10764 {
10765 if (t->opcode_modifier.jump
10766 != current_templates->start->opcode_modifier.jump)
10767 break;
4b5aaf5f 10768 if ((t->opcode_modifier.isa64 >= INTEL64))
5b7c81bd 10769 has_intel64 = true;
376cd056
JB
10770 }
10771 if (t < current_templates->end)
10772 {
10773 aux_templates.end = t;
10774 current_templates = &aux_templates;
10775 }
10776
e05278af 10777 override = (i.prefix[DATA_PREFIX] != 0);
40fb9820
L
10778 if (flag_code == CODE_64BIT)
10779 {
376cd056
JB
10780 if ((override || i.suffix == WORD_MNEM_SUFFIX)
10781 && (!intel64 || !has_intel64))
40fb9820
L
10782 bigdisp.bitfield.disp16 = 1;
10783 else
48bcea9f 10784 bigdisp.bitfield.disp32s = 1;
40fb9820
L
10785 }
10786 else
e05278af
JB
10787 {
10788 if (!override)
10789 override = (i.suffix == (flag_code != CODE_16BIT
10790 ? WORD_MNEM_SUFFIX
10791 : LONG_MNEM_SUFFIX));
40fb9820
L
10792 bigdisp.bitfield.disp32 = 1;
10793 if ((flag_code == CODE_16BIT) ^ override)
10794 {
10795 bigdisp.bitfield.disp32 = 0;
10796 bigdisp.bitfield.disp16 = 1;
10797 }
e05278af 10798 }
e05278af 10799 }
c6fb90c8
L
10800 i.types[this_operand] = operand_type_or (i.types[this_operand],
10801 bigdisp);
252b5132
RH
10802
10803 exp = &disp_expressions[i.disp_operands];
520dc8e8 10804 i.op[this_operand].disps = exp;
252b5132
RH
10805 i.disp_operands++;
10806 save_input_line_pointer = input_line_pointer;
10807 input_line_pointer = disp_start;
10808 END_STRING_AND_SAVE (disp_end);
10809
10810#ifndef GCC_ASM_O_HACK
10811#define GCC_ASM_O_HACK 0
10812#endif
10813#if GCC_ASM_O_HACK
10814 END_STRING_AND_SAVE (disp_end + 1);
40fb9820 10815 if (i.types[this_operand].bitfield.baseIndex
24eab124 10816 && displacement_string_end[-1] == '+')
252b5132
RH
10817 {
10818 /* This hack is to avoid a warning when using the "o"
24eab124
AM
10819 constraint within gcc asm statements.
10820 For instance:
10821
10822 #define _set_tssldt_desc(n,addr,limit,type) \
10823 __asm__ __volatile__ ( \
10824 "movw %w2,%0\n\t" \
10825 "movw %w1,2+%0\n\t" \
10826 "rorl $16,%1\n\t" \
10827 "movb %b1,4+%0\n\t" \
10828 "movb %4,5+%0\n\t" \
10829 "movb $0,6+%0\n\t" \
10830 "movb %h1,7+%0\n\t" \
10831 "rorl $16,%1" \
10832 : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
10833
10834 This works great except that the output assembler ends
10835 up looking a bit weird if it turns out that there is
10836 no offset. You end up producing code that looks like:
10837
10838 #APP
10839 movw $235,(%eax)
10840 movw %dx,2+(%eax)
10841 rorl $16,%edx
10842 movb %dl,4+(%eax)
10843 movb $137,5+(%eax)
10844 movb $0,6+(%eax)
10845 movb %dh,7+(%eax)
10846 rorl $16,%edx
10847 #NO_APP
10848
47926f60 10849 So here we provide the missing zero. */
24eab124
AM
10850
10851 *displacement_string_end = '0';
252b5132
RH
10852 }
10853#endif
d258b828 10854 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
10855 if (gotfree_input_line)
10856 input_line_pointer = gotfree_input_line;
252b5132 10857
24eab124 10858 exp_seg = expression (exp);
252b5132 10859
636c26b0
AM
10860 SKIP_WHITESPACE ();
10861 if (*input_line_pointer)
10862 as_bad (_("junk `%s' after expression"), input_line_pointer);
10863#if GCC_ASM_O_HACK
10864 RESTORE_END_STRING (disp_end + 1);
10865#endif
636c26b0 10866 input_line_pointer = save_input_line_pointer;
636c26b0 10867 if (gotfree_input_line)
ee86248c
JB
10868 {
10869 free (gotfree_input_line);
10870
10871 if (exp->X_op == O_constant || exp->X_op == O_register)
10872 exp->X_op = O_illegal;
10873 }
10874
10875 ret = i386_finalize_displacement (exp_seg, exp, types, disp_start);
10876
10877 RESTORE_END_STRING (disp_end);
10878
10879 return ret;
10880}
10881
10882static int
10883i386_finalize_displacement (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
10884 i386_operand_type types, const char *disp_start)
10885{
10886 i386_operand_type bigdisp;
10887 int ret = 1;
636c26b0 10888
24eab124
AM
10889 /* We do this to make sure that the section symbol is in
10890 the symbol table. We will ultimately change the relocation
47926f60 10891 to be relative to the beginning of the section. */
1ae12ab7 10892 if (i.reloc[this_operand] == BFD_RELOC_386_GOTOFF
d6ab8113
JB
10893 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
10894 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
24eab124 10895 {
636c26b0 10896 if (exp->X_op != O_symbol)
3992d3b7 10897 goto inv_disp;
636c26b0 10898
e5cb08ac 10899 if (S_IS_LOCAL (exp->X_add_symbol)
c64efb4b
L
10900 && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section
10901 && S_GET_SEGMENT (exp->X_add_symbol) != expr_section)
24eab124 10902 section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
24eab124
AM
10903 exp->X_op = O_subtract;
10904 exp->X_op_symbol = GOT_symbol;
1ae12ab7 10905 if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
29b0f896 10906 i.reloc[this_operand] = BFD_RELOC_32_PCREL;
d6ab8113
JB
10907 else if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
10908 i.reloc[this_operand] = BFD_RELOC_64;
23df1078 10909 else
29b0f896 10910 i.reloc[this_operand] = BFD_RELOC_32;
24eab124 10911 }
252b5132 10912
3992d3b7
AM
10913 else if (exp->X_op == O_absent
10914 || exp->X_op == O_illegal
ee86248c 10915 || exp->X_op == O_big)
2daf4fd8 10916 {
3992d3b7
AM
10917 inv_disp:
10918 as_bad (_("missing or invalid displacement expression `%s'"),
2daf4fd8 10919 disp_start);
3992d3b7 10920 ret = 0;
2daf4fd8
AM
10921 }
10922
0e1147d9
L
10923 else if (flag_code == CODE_64BIT
10924 && !i.prefix[ADDR_PREFIX]
10925 && exp->X_op == O_constant)
10926 {
10927 /* Since displacement is signed extended to 64bit, don't allow
10928 disp32 and turn off disp32s if they are out of range. */
10929 i.types[this_operand].bitfield.disp32 = 0;
10930 if (!fits_in_signed_long (exp->X_add_number))
10931 {
10932 i.types[this_operand].bitfield.disp32s = 0;
10933 if (i.types[this_operand].bitfield.baseindex)
10934 {
10935 as_bad (_("0x%lx out range of signed 32bit displacement"),
10936 (long) exp->X_add_number);
10937 ret = 0;
10938 }
10939 }
10940 }
10941
4c63da97 10942#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
3992d3b7
AM
10943 else if (exp->X_op != O_constant
10944 && OUTPUT_FLAVOR == bfd_target_aout_flavour
10945 && exp_seg != absolute_section
10946 && exp_seg != text_section
10947 && exp_seg != data_section
10948 && exp_seg != bss_section
10949 && exp_seg != undefined_section
10950 && !bfd_is_com_section (exp_seg))
24eab124 10951 {
d0b47220 10952 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
3992d3b7 10953 ret = 0;
24eab124 10954 }
252b5132 10955#endif
3956db08 10956
48bcea9f
JB
10957 if (current_templates->start->opcode_modifier.jump == JUMP_BYTE
10958 /* Constants get taken care of by optimize_disp(). */
10959 && exp->X_op != O_constant)
10960 i.types[this_operand].bitfield.disp8 = 1;
10961
40fb9820
L
10962 /* Check if this is a displacement only operand. */
10963 bigdisp = i.types[this_operand];
10964 bigdisp.bitfield.disp8 = 0;
10965 bigdisp.bitfield.disp16 = 0;
10966 bigdisp.bitfield.disp32 = 0;
10967 bigdisp.bitfield.disp32s = 0;
10968 bigdisp.bitfield.disp64 = 0;
0dfbf9d7 10969 if (operand_type_all_zero (&bigdisp))
c6fb90c8
L
10970 i.types[this_operand] = operand_type_and (i.types[this_operand],
10971 types);
3956db08 10972
3992d3b7 10973 return ret;
252b5132
RH
10974}
10975
2abc2bec
JB
10976/* Return the active addressing mode, taking address override and
10977 registers forming the address into consideration. Update the
10978 address override prefix if necessary. */
47926f60 10979
2abc2bec
JB
10980static enum flag_code
10981i386_addressing_mode (void)
252b5132 10982{
be05d201
L
10983 enum flag_code addr_mode;
10984
10985 if (i.prefix[ADDR_PREFIX])
10986 addr_mode = flag_code == CODE_32BIT ? CODE_16BIT : CODE_32BIT;
a23b33b3
JB
10987 else if (flag_code == CODE_16BIT
10988 && current_templates->start->cpu_flags.bitfield.cpumpx
10989 /* Avoid replacing the "16-bit addressing not allowed" diagnostic
10990 from md_assemble() by "is not a valid base/index expression"
10991 when there is a base and/or index. */
10992 && !i.types[this_operand].bitfield.baseindex)
10993 {
10994 /* MPX insn memory operands with neither base nor index must be forced
10995 to use 32-bit addressing in 16-bit mode. */
10996 addr_mode = CODE_32BIT;
10997 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
10998 ++i.prefixes;
10999 gas_assert (!i.types[this_operand].bitfield.disp16);
11000 gas_assert (!i.types[this_operand].bitfield.disp32);
11001 }
be05d201
L
11002 else
11003 {
11004 addr_mode = flag_code;
11005
24eab124 11006#if INFER_ADDR_PREFIX
be05d201
L
11007 if (i.mem_operands == 0)
11008 {
11009 /* Infer address prefix from the first memory operand. */
11010 const reg_entry *addr_reg = i.base_reg;
11011
11012 if (addr_reg == NULL)
11013 addr_reg = i.index_reg;
eecb386c 11014
be05d201
L
11015 if (addr_reg)
11016 {
e968fc9b 11017 if (addr_reg->reg_type.bitfield.dword)
be05d201
L
11018 addr_mode = CODE_32BIT;
11019 else if (flag_code != CODE_64BIT
dc821c5f 11020 && addr_reg->reg_type.bitfield.word)
be05d201
L
11021 addr_mode = CODE_16BIT;
11022
11023 if (addr_mode != flag_code)
11024 {
11025 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
11026 i.prefixes += 1;
11027 /* Change the size of any displacement too. At most one
11028 of Disp16 or Disp32 is set.
11029 FIXME. There doesn't seem to be any real need for
11030 separate Disp16 and Disp32 flags. The same goes for
11031 Imm16 and Imm32. Removing them would probably clean
11032 up the code quite a lot. */
11033 if (flag_code != CODE_64BIT
11034 && (i.types[this_operand].bitfield.disp16
11035 || i.types[this_operand].bitfield.disp32))
11036 i.types[this_operand]
11037 = operand_type_xor (i.types[this_operand], disp16_32);
11038 }
11039 }
11040 }
24eab124 11041#endif
be05d201
L
11042 }
11043
2abc2bec
JB
11044 return addr_mode;
11045}
11046
11047/* Make sure the memory operand we've been dealt is valid.
11048 Return 1 on success, 0 on a failure. */
11049
11050static int
11051i386_index_check (const char *operand_string)
11052{
11053 const char *kind = "base/index";
11054 enum flag_code addr_mode = i386_addressing_mode ();
a152332d 11055 const insn_template *t = current_templates->start;
2abc2bec 11056
a152332d
JB
11057 if (t->opcode_modifier.isstring
11058 && !t->cpu_flags.bitfield.cpupadlock
fc0763e6
JB
11059 && (current_templates->end[-1].opcode_modifier.isstring
11060 || i.mem_operands))
11061 {
11062 /* Memory operands of string insns are special in that they only allow
11063 a single register (rDI, rSI, or rBX) as their memory address. */
be05d201
L
11064 const reg_entry *expected_reg;
11065 static const char *di_si[][2] =
11066 {
11067 { "esi", "edi" },
11068 { "si", "di" },
11069 { "rsi", "rdi" }
11070 };
11071 static const char *bx[] = { "ebx", "bx", "rbx" };
fc0763e6
JB
11072
11073 kind = "string address";
11074
a152332d 11075 if (t->opcode_modifier.prefixok == PrefixRep)
fc0763e6 11076 {
51c8edf6
JB
11077 int es_op = current_templates->end[-1].opcode_modifier.isstring
11078 - IS_STRING_ES_OP0;
11079 int op = 0;
fc0763e6 11080
51c8edf6 11081 if (!current_templates->end[-1].operand_types[0].bitfield.baseindex
fc0763e6
JB
11082 || ((!i.mem_operands != !intel_syntax)
11083 && current_templates->end[-1].operand_types[1]
11084 .bitfield.baseindex))
51c8edf6 11085 op = 1;
fe0e921f
AM
11086 expected_reg
11087 = (const reg_entry *) str_hash_find (reg_hash,
11088 di_si[addr_mode][op == es_op]);
fc0763e6
JB
11089 }
11090 else
fe0e921f
AM
11091 expected_reg
11092 = (const reg_entry *)str_hash_find (reg_hash, bx[addr_mode]);
fc0763e6 11093
be05d201
L
11094 if (i.base_reg != expected_reg
11095 || i.index_reg
fc0763e6 11096 || operand_type_check (i.types[this_operand], disp))
fc0763e6 11097 {
be05d201
L
11098 /* The second memory operand must have the same size as
11099 the first one. */
11100 if (i.mem_operands
11101 && i.base_reg
11102 && !((addr_mode == CODE_64BIT
dc821c5f 11103 && i.base_reg->reg_type.bitfield.qword)
be05d201 11104 || (addr_mode == CODE_32BIT
dc821c5f
JB
11105 ? i.base_reg->reg_type.bitfield.dword
11106 : i.base_reg->reg_type.bitfield.word)))
be05d201
L
11107 goto bad_address;
11108
fc0763e6
JB
11109 as_warn (_("`%s' is not valid here (expected `%c%s%s%c')"),
11110 operand_string,
11111 intel_syntax ? '[' : '(',
11112 register_prefix,
be05d201 11113 expected_reg->reg_name,
fc0763e6 11114 intel_syntax ? ']' : ')');
be05d201 11115 return 1;
fc0763e6 11116 }
be05d201
L
11117 else
11118 return 1;
11119
dc1e8a47 11120 bad_address:
be05d201
L
11121 as_bad (_("`%s' is not a valid %s expression"),
11122 operand_string, kind);
11123 return 0;
3e73aa7c
JH
11124 }
11125 else
11126 {
be05d201
L
11127 if (addr_mode != CODE_16BIT)
11128 {
11129 /* 32-bit/64-bit checks. */
41eb8e88
L
11130 if (i.disp_encoding == disp_encoding_16bit)
11131 {
11132 bad_disp:
11133 as_bad (_("invalid `%s' prefix"),
11134 addr_mode == CODE_16BIT ? "{disp32}" : "{disp16}");
11135 return 0;
11136 }
11137
be05d201 11138 if ((i.base_reg
e968fc9b
JB
11139 && ((addr_mode == CODE_64BIT
11140 ? !i.base_reg->reg_type.bitfield.qword
11141 : !i.base_reg->reg_type.bitfield.dword)
11142 || (i.index_reg && i.base_reg->reg_num == RegIP)
11143 || i.base_reg->reg_num == RegIZ))
be05d201 11144 || (i.index_reg
1b54b8d7
JB
11145 && !i.index_reg->reg_type.bitfield.xmmword
11146 && !i.index_reg->reg_type.bitfield.ymmword
11147 && !i.index_reg->reg_type.bitfield.zmmword
be05d201 11148 && ((addr_mode == CODE_64BIT
e968fc9b
JB
11149 ? !i.index_reg->reg_type.bitfield.qword
11150 : !i.index_reg->reg_type.bitfield.dword)
be05d201
L
11151 || !i.index_reg->reg_type.bitfield.baseindex)))
11152 goto bad_address;
8178be5b 11153
260cd341 11154 /* bndmk, bndldx, bndstx and mandatory non-vector SIB have special restrictions. */
a152332d 11155 if ((t->opcode_modifier.opcodeprefix == PREFIX_0XF3
389d00a5
JB
11156 && t->opcode_modifier.opcodespace == SPACE_0F
11157 && t->base_opcode == 0x1b)
a152332d 11158 || (t->opcode_modifier.opcodeprefix == PREFIX_NONE
389d00a5
JB
11159 && t->opcode_modifier.opcodespace == SPACE_0F
11160 && (t->base_opcode & ~1) == 0x1a)
a152332d 11161 || t->opcode_modifier.sib == SIBMEM)
8178be5b
JB
11162 {
11163 /* They cannot use RIP-relative addressing. */
e968fc9b 11164 if (i.base_reg && i.base_reg->reg_num == RegIP)
8178be5b
JB
11165 {
11166 as_bad (_("`%s' cannot be used here"), operand_string);
11167 return 0;
11168 }
11169
11170 /* bndldx and bndstx ignore their scale factor. */
a152332d 11171 if (t->opcode_modifier.opcodeprefix == PREFIX_NONE
389d00a5
JB
11172 && t->opcode_modifier.opcodespace == SPACE_0F
11173 && (t->base_opcode & ~1) == 0x1a
8178be5b
JB
11174 && i.log2_scale_factor)
11175 as_warn (_("register scaling is being ignored here"));
11176 }
be05d201
L
11177 }
11178 else
3e73aa7c 11179 {
be05d201 11180 /* 16-bit checks. */
41eb8e88
L
11181 if (i.disp_encoding == disp_encoding_32bit)
11182 goto bad_disp;
11183
3e73aa7c 11184 if ((i.base_reg
dc821c5f 11185 && (!i.base_reg->reg_type.bitfield.word
40fb9820 11186 || !i.base_reg->reg_type.bitfield.baseindex))
3e73aa7c 11187 || (i.index_reg
dc821c5f 11188 && (!i.index_reg->reg_type.bitfield.word
40fb9820 11189 || !i.index_reg->reg_type.bitfield.baseindex
29b0f896
AM
11190 || !(i.base_reg
11191 && i.base_reg->reg_num < 6
11192 && i.index_reg->reg_num >= 6
11193 && i.log2_scale_factor == 0))))
be05d201 11194 goto bad_address;
3e73aa7c
JH
11195 }
11196 }
be05d201 11197 return 1;
24eab124 11198}
252b5132 11199
43234a1e
L
11200/* Handle vector immediates. */
11201
11202static int
11203RC_SAE_immediate (const char *imm_start)
11204{
11205 unsigned int match_found, j;
11206 const char *pstr = imm_start;
11207 expressionS *exp;
11208
11209 if (*pstr != '{')
11210 return 0;
11211
11212 pstr++;
11213 match_found = 0;
11214 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); j++)
11215 {
11216 if (!strncmp (pstr, RC_NamesTable[j].name, RC_NamesTable[j].len))
11217 {
ca5312a2 11218 if (i.rounding.type != rc_none)
43234a1e
L
11219 {
11220 as_bad (_("duplicated `%s'"), imm_start);
11221 return 0;
11222 }
ca5312a2
JB
11223
11224 i.rounding.type = RC_NamesTable[j].type;
11225 i.rounding.operand = this_operand;
11226
43234a1e
L
11227 pstr += RC_NamesTable[j].len;
11228 match_found = 1;
11229 break;
11230 }
11231 }
11232 if (!match_found)
11233 return 0;
11234
11235 if (*pstr++ != '}')
11236 {
11237 as_bad (_("Missing '}': '%s'"), imm_start);
11238 return 0;
11239 }
11240 /* RC/SAE immediate string should contain nothing more. */;
11241 if (*pstr != 0)
11242 {
11243 as_bad (_("Junk after '}': '%s'"), imm_start);
11244 return 0;
11245 }
11246
11247 exp = &im_expressions[i.imm_operands++];
11248 i.op[this_operand].imms = exp;
11249
11250 exp->X_op = O_constant;
11251 exp->X_add_number = 0;
11252 exp->X_add_symbol = (symbolS *) 0;
11253 exp->X_op_symbol = (symbolS *) 0;
11254
11255 i.types[this_operand].bitfield.imm8 = 1;
11256 return 1;
11257}
11258
8325cc63
JB
11259/* Only string instructions can have a second memory operand, so
11260 reduce current_templates to just those if it contains any. */
11261static int
11262maybe_adjust_templates (void)
11263{
11264 const insn_template *t;
11265
11266 gas_assert (i.mem_operands == 1);
11267
11268 for (t = current_templates->start; t < current_templates->end; ++t)
11269 if (t->opcode_modifier.isstring)
11270 break;
11271
11272 if (t < current_templates->end)
11273 {
11274 static templates aux_templates;
5b7c81bd 11275 bool recheck;
8325cc63
JB
11276
11277 aux_templates.start = t;
11278 for (; t < current_templates->end; ++t)
11279 if (!t->opcode_modifier.isstring)
11280 break;
11281 aux_templates.end = t;
11282
11283 /* Determine whether to re-check the first memory operand. */
11284 recheck = (aux_templates.start != current_templates->start
11285 || t != current_templates->end);
11286
11287 current_templates = &aux_templates;
11288
11289 if (recheck)
11290 {
11291 i.mem_operands = 0;
11292 if (i.memop1_string != NULL
11293 && i386_index_check (i.memop1_string) == 0)
11294 return 0;
11295 i.mem_operands = 1;
11296 }
11297 }
11298
11299 return 1;
11300}
11301
fc0763e6 11302/* Parse OPERAND_STRING into the i386_insn structure I. Returns zero
47926f60 11303 on error. */
252b5132 11304
252b5132 11305static int
a7619375 11306i386_att_operand (char *operand_string)
252b5132 11307{
af6bdddf
AM
11308 const reg_entry *r;
11309 char *end_op;
24eab124 11310 char *op_string = operand_string;
252b5132 11311
24eab124 11312 if (is_space_char (*op_string))
252b5132
RH
11313 ++op_string;
11314
24eab124 11315 /* We check for an absolute prefix (differentiating,
47926f60 11316 for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
24eab124
AM
11317 if (*op_string == ABSOLUTE_PREFIX)
11318 {
11319 ++op_string;
11320 if (is_space_char (*op_string))
11321 ++op_string;
5b7c81bd 11322 i.jumpabsolute = true;
24eab124 11323 }
252b5132 11324
47926f60 11325 /* Check if operand is a register. */
4d1bb795 11326 if ((r = parse_register (op_string, &end_op)) != NULL)
24eab124 11327 {
40fb9820
L
11328 i386_operand_type temp;
11329
8a6fb3f9
JB
11330 if (r == &bad_reg)
11331 return 0;
11332
24eab124
AM
11333 /* Check for a segment override by searching for ':' after a
11334 segment register. */
11335 op_string = end_op;
11336 if (is_space_char (*op_string))
11337 ++op_string;
00cee14f 11338 if (*op_string == ':' && r->reg_type.bitfield.class == SReg)
24eab124 11339 {
5e042380 11340 i.seg[i.mem_operands] = r;
252b5132 11341
24eab124 11342 /* Skip the ':' and whitespace. */
252b5132
RH
11343 ++op_string;
11344 if (is_space_char (*op_string))
24eab124 11345 ++op_string;
252b5132 11346
24eab124
AM
11347 if (!is_digit_char (*op_string)
11348 && !is_identifier_char (*op_string)
11349 && *op_string != '('
11350 && *op_string != ABSOLUTE_PREFIX)
11351 {
11352 as_bad (_("bad memory operand `%s'"), op_string);
11353 return 0;
11354 }
47926f60 11355 /* Handle case of %es:*foo. */
24eab124
AM
11356 if (*op_string == ABSOLUTE_PREFIX)
11357 {
11358 ++op_string;
11359 if (is_space_char (*op_string))
11360 ++op_string;
5b7c81bd 11361 i.jumpabsolute = true;
24eab124
AM
11362 }
11363 goto do_memory_reference;
11364 }
43234a1e
L
11365
11366 /* Handle vector operations. */
11367 if (*op_string == '{')
11368 {
11369 op_string = check_VecOperations (op_string, NULL);
11370 if (op_string == NULL)
11371 return 0;
11372 }
11373
24eab124
AM
11374 if (*op_string)
11375 {
d0b47220 11376 as_bad (_("junk `%s' after register"), op_string);
24eab124
AM
11377 return 0;
11378 }
40fb9820
L
11379 temp = r->reg_type;
11380 temp.bitfield.baseindex = 0;
c6fb90c8
L
11381 i.types[this_operand] = operand_type_or (i.types[this_operand],
11382 temp);
7d5e4556 11383 i.types[this_operand].bitfield.unspecified = 0;
520dc8e8 11384 i.op[this_operand].regs = r;
24eab124
AM
11385 i.reg_operands++;
11386 }
af6bdddf
AM
11387 else if (*op_string == REGISTER_PREFIX)
11388 {
11389 as_bad (_("bad register name `%s'"), op_string);
11390 return 0;
11391 }
24eab124 11392 else if (*op_string == IMMEDIATE_PREFIX)
ce8a8b2f 11393 {
24eab124 11394 ++op_string;
6f2f06be 11395 if (i.jumpabsolute)
24eab124 11396 {
d0b47220 11397 as_bad (_("immediate operand illegal with absolute jump"));
24eab124
AM
11398 return 0;
11399 }
11400 if (!i386_immediate (op_string))
11401 return 0;
11402 }
43234a1e
L
11403 else if (RC_SAE_immediate (operand_string))
11404 {
11405 /* If it is a RC or SAE immediate, do nothing. */
11406 ;
11407 }
24eab124
AM
11408 else if (is_digit_char (*op_string)
11409 || is_identifier_char (*op_string)
d02603dc 11410 || *op_string == '"'
e5cb08ac 11411 || *op_string == '(')
24eab124 11412 {
47926f60 11413 /* This is a memory reference of some sort. */
af6bdddf 11414 char *base_string;
252b5132 11415
47926f60 11416 /* Start and end of displacement string expression (if found). */
eecb386c
AM
11417 char *displacement_string_start;
11418 char *displacement_string_end;
43234a1e 11419 char *vop_start;
252b5132 11420
24eab124 11421 do_memory_reference:
8325cc63
JB
11422 if (i.mem_operands == 1 && !maybe_adjust_templates ())
11423 return 0;
24eab124 11424 if ((i.mem_operands == 1
40fb9820 11425 && !current_templates->start->opcode_modifier.isstring)
24eab124
AM
11426 || i.mem_operands == 2)
11427 {
11428 as_bad (_("too many memory references for `%s'"),
11429 current_templates->start->name);
11430 return 0;
11431 }
252b5132 11432
24eab124
AM
11433 /* Check for base index form. We detect the base index form by
11434 looking for an ')' at the end of the operand, searching
11435 for the '(' matching it, and finding a REGISTER_PREFIX or ','
11436 after the '('. */
af6bdddf 11437 base_string = op_string + strlen (op_string);
c3332e24 11438
43234a1e
L
11439 /* Handle vector operations. */
11440 vop_start = strchr (op_string, '{');
11441 if (vop_start && vop_start < base_string)
11442 {
11443 if (check_VecOperations (vop_start, base_string) == NULL)
11444 return 0;
11445 base_string = vop_start;
11446 }
11447
af6bdddf
AM
11448 --base_string;
11449 if (is_space_char (*base_string))
11450 --base_string;
252b5132 11451
47926f60 11452 /* If we only have a displacement, set-up for it to be parsed later. */
af6bdddf
AM
11453 displacement_string_start = op_string;
11454 displacement_string_end = base_string + 1;
252b5132 11455
24eab124
AM
11456 if (*base_string == ')')
11457 {
af6bdddf 11458 char *temp_string;
24eab124
AM
11459 unsigned int parens_balanced = 1;
11460 /* We've already checked that the number of left & right ()'s are
47926f60 11461 equal, so this loop will not be infinite. */
24eab124
AM
11462 do
11463 {
11464 base_string--;
11465 if (*base_string == ')')
11466 parens_balanced++;
11467 if (*base_string == '(')
11468 parens_balanced--;
11469 }
11470 while (parens_balanced);
c3332e24 11471
af6bdddf 11472 temp_string = base_string;
c3332e24 11473
24eab124 11474 /* Skip past '(' and whitespace. */
252b5132
RH
11475 ++base_string;
11476 if (is_space_char (*base_string))
24eab124 11477 ++base_string;
252b5132 11478
af6bdddf 11479 if (*base_string == ','
4eed87de
AM
11480 || ((i.base_reg = parse_register (base_string, &end_op))
11481 != NULL))
252b5132 11482 {
af6bdddf 11483 displacement_string_end = temp_string;
252b5132 11484
40fb9820 11485 i.types[this_operand].bitfield.baseindex = 1;
252b5132 11486
af6bdddf 11487 if (i.base_reg)
24eab124 11488 {
8a6fb3f9
JB
11489 if (i.base_reg == &bad_reg)
11490 return 0;
24eab124
AM
11491 base_string = end_op;
11492 if (is_space_char (*base_string))
11493 ++base_string;
af6bdddf
AM
11494 }
11495
11496 /* There may be an index reg or scale factor here. */
11497 if (*base_string == ',')
11498 {
11499 ++base_string;
11500 if (is_space_char (*base_string))
11501 ++base_string;
11502
4eed87de
AM
11503 if ((i.index_reg = parse_register (base_string, &end_op))
11504 != NULL)
24eab124 11505 {
8a6fb3f9
JB
11506 if (i.index_reg == &bad_reg)
11507 return 0;
af6bdddf 11508 base_string = end_op;
24eab124
AM
11509 if (is_space_char (*base_string))
11510 ++base_string;
af6bdddf
AM
11511 if (*base_string == ',')
11512 {
11513 ++base_string;
11514 if (is_space_char (*base_string))
11515 ++base_string;
11516 }
e5cb08ac 11517 else if (*base_string != ')')
af6bdddf 11518 {
4eed87de
AM
11519 as_bad (_("expecting `,' or `)' "
11520 "after index register in `%s'"),
af6bdddf
AM
11521 operand_string);
11522 return 0;
11523 }
24eab124 11524 }
af6bdddf 11525 else if (*base_string == REGISTER_PREFIX)
24eab124 11526 {
f76bf5e0
L
11527 end_op = strchr (base_string, ',');
11528 if (end_op)
11529 *end_op = '\0';
af6bdddf 11530 as_bad (_("bad register name `%s'"), base_string);
24eab124
AM
11531 return 0;
11532 }
252b5132 11533
47926f60 11534 /* Check for scale factor. */
551c1ca1 11535 if (*base_string != ')')
af6bdddf 11536 {
551c1ca1
AM
11537 char *end_scale = i386_scale (base_string);
11538
11539 if (!end_scale)
af6bdddf 11540 return 0;
24eab124 11541
551c1ca1 11542 base_string = end_scale;
af6bdddf
AM
11543 if (is_space_char (*base_string))
11544 ++base_string;
11545 if (*base_string != ')')
11546 {
4eed87de
AM
11547 as_bad (_("expecting `)' "
11548 "after scale factor in `%s'"),
af6bdddf
AM
11549 operand_string);
11550 return 0;
11551 }
11552 }
11553 else if (!i.index_reg)
24eab124 11554 {
4eed87de
AM
11555 as_bad (_("expecting index register or scale factor "
11556 "after `,'; got '%c'"),
af6bdddf 11557 *base_string);
24eab124
AM
11558 return 0;
11559 }
11560 }
af6bdddf 11561 else if (*base_string != ')')
24eab124 11562 {
4eed87de
AM
11563 as_bad (_("expecting `,' or `)' "
11564 "after base register in `%s'"),
af6bdddf 11565 operand_string);
24eab124
AM
11566 return 0;
11567 }
c3332e24 11568 }
af6bdddf 11569 else if (*base_string == REGISTER_PREFIX)
c3332e24 11570 {
f76bf5e0
L
11571 end_op = strchr (base_string, ',');
11572 if (end_op)
11573 *end_op = '\0';
af6bdddf 11574 as_bad (_("bad register name `%s'"), base_string);
24eab124 11575 return 0;
c3332e24 11576 }
24eab124
AM
11577 }
11578
11579 /* If there's an expression beginning the operand, parse it,
11580 assuming displacement_string_start and
11581 displacement_string_end are meaningful. */
11582 if (displacement_string_start != displacement_string_end)
11583 {
11584 if (!i386_displacement (displacement_string_start,
11585 displacement_string_end))
11586 return 0;
11587 }
11588
11589 /* Special case for (%dx) while doing input/output op. */
11590 if (i.base_reg
75e5731b
JB
11591 && i.base_reg->reg_type.bitfield.instance == RegD
11592 && i.base_reg->reg_type.bitfield.word
24eab124
AM
11593 && i.index_reg == 0
11594 && i.log2_scale_factor == 0
11595 && i.seg[i.mem_operands] == 0
40fb9820 11596 && !operand_type_check (i.types[this_operand], disp))
24eab124 11597 {
2fb5be8d 11598 i.types[this_operand] = i.base_reg->reg_type;
24eab124
AM
11599 return 1;
11600 }
11601
eecb386c
AM
11602 if (i386_index_check (operand_string) == 0)
11603 return 0;
c48dadc9 11604 i.flags[this_operand] |= Operand_Mem;
8325cc63
JB
11605 if (i.mem_operands == 0)
11606 i.memop1_string = xstrdup (operand_string);
24eab124
AM
11607 i.mem_operands++;
11608 }
11609 else
ce8a8b2f
AM
11610 {
11611 /* It's not a memory operand; argh! */
24eab124
AM
11612 as_bad (_("invalid char %s beginning operand %d `%s'"),
11613 output_invalid (*op_string),
11614 this_operand + 1,
11615 op_string);
11616 return 0;
11617 }
47926f60 11618 return 1; /* Normal return. */
252b5132
RH
11619}
11620\f
fa94de6b
RM
11621/* Calculate the maximum variable size (i.e., excluding fr_fix)
11622 that an rs_machine_dependent frag may reach. */
11623
11624unsigned int
11625i386_frag_max_var (fragS *frag)
11626{
11627 /* The only relaxable frags are for jumps.
11628 Unconditional jumps can grow by 4 bytes and others by 5 bytes. */
11629 gas_assert (frag->fr_type == rs_machine_dependent);
11630 return TYPE_FROM_RELAX_STATE (frag->fr_subtype) == UNCOND_JUMP ? 4 : 5;
11631}
11632
b084df0b
L
11633#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
11634static int
8dcea932 11635elf_symbol_resolved_in_segment_p (symbolS *fr_symbol, offsetT fr_var)
b084df0b
L
11636{
11637 /* STT_GNU_IFUNC symbol must go through PLT. */
11638 if ((symbol_get_bfdsym (fr_symbol)->flags
11639 & BSF_GNU_INDIRECT_FUNCTION) != 0)
11640 return 0;
11641
11642 if (!S_IS_EXTERNAL (fr_symbol))
11643 /* Symbol may be weak or local. */
11644 return !S_IS_WEAK (fr_symbol);
11645
8dcea932
L
11646 /* Global symbols with non-default visibility can't be preempted. */
11647 if (ELF_ST_VISIBILITY (S_GET_OTHER (fr_symbol)) != STV_DEFAULT)
11648 return 1;
11649
11650 if (fr_var != NO_RELOC)
11651 switch ((enum bfd_reloc_code_real) fr_var)
11652 {
11653 case BFD_RELOC_386_PLT32:
11654 case BFD_RELOC_X86_64_PLT32:
33eaf5de 11655 /* Symbol with PLT relocation may be preempted. */
8dcea932
L
11656 return 0;
11657 default:
11658 abort ();
11659 }
11660
b084df0b
L
11661 /* Global symbols with default visibility in a shared library may be
11662 preempted by another definition. */
8dcea932 11663 return !shared;
b084df0b
L
11664}
11665#endif
11666
79d72f45
HL
11667/* Table 3-2. Macro-Fusible Instructions in Haswell Microarchitecture
11668 Note also work for Skylake and Cascadelake.
11669---------------------------------------------------------------------
11670| JCC | ADD/SUB/CMP | INC/DEC | TEST/AND |
11671| ------ | ----------- | ------- | -------- |
11672| Jo | N | N | Y |
11673| Jno | N | N | Y |
11674| Jc/Jb | Y | N | Y |
11675| Jae/Jnb | Y | N | Y |
11676| Je/Jz | Y | Y | Y |
11677| Jne/Jnz | Y | Y | Y |
11678| Jna/Jbe | Y | N | Y |
11679| Ja/Jnbe | Y | N | Y |
11680| Js | N | N | Y |
11681| Jns | N | N | Y |
11682| Jp/Jpe | N | N | Y |
11683| Jnp/Jpo | N | N | Y |
11684| Jl/Jnge | Y | Y | Y |
11685| Jge/Jnl | Y | Y | Y |
11686| Jle/Jng | Y | Y | Y |
11687| Jg/Jnle | Y | Y | Y |
11688--------------------------------------------------------------------- */
11689static int
11690i386_macro_fusible_p (enum mf_cmp_kind mf_cmp, enum mf_jcc_kind mf_jcc)
11691{
11692 if (mf_cmp == mf_cmp_alu_cmp)
11693 return ((mf_jcc >= mf_jcc_jc && mf_jcc <= mf_jcc_jna)
11694 || mf_jcc == mf_jcc_jl || mf_jcc == mf_jcc_jle);
11695 if (mf_cmp == mf_cmp_incdec)
11696 return (mf_jcc == mf_jcc_je || mf_jcc == mf_jcc_jl
11697 || mf_jcc == mf_jcc_jle);
11698 if (mf_cmp == mf_cmp_test_and)
11699 return 1;
11700 return 0;
11701}
11702
e379e5f3
L
11703/* Return the next non-empty frag. */
11704
11705static fragS *
11706i386_next_non_empty_frag (fragS *fragP)
11707{
11708 /* There may be a frag with a ".fill 0" when there is no room in
11709 the current frag for frag_grow in output_insn. */
11710 for (fragP = fragP->fr_next;
11711 (fragP != NULL
11712 && fragP->fr_type == rs_fill
11713 && fragP->fr_fix == 0);
11714 fragP = fragP->fr_next)
11715 ;
11716 return fragP;
11717}
11718
11719/* Return the next jcc frag after BRANCH_PADDING. */
11720
11721static fragS *
79d72f45 11722i386_next_fusible_jcc_frag (fragS *maybe_cmp_fragP, fragS *pad_fragP)
e379e5f3 11723{
79d72f45
HL
11724 fragS *branch_fragP;
11725 if (!pad_fragP)
e379e5f3
L
11726 return NULL;
11727
79d72f45
HL
11728 if (pad_fragP->fr_type == rs_machine_dependent
11729 && (TYPE_FROM_RELAX_STATE (pad_fragP->fr_subtype)
e379e5f3
L
11730 == BRANCH_PADDING))
11731 {
79d72f45
HL
11732 branch_fragP = i386_next_non_empty_frag (pad_fragP);
11733 if (branch_fragP->fr_type != rs_machine_dependent)
e379e5f3 11734 return NULL;
79d72f45
HL
11735 if (TYPE_FROM_RELAX_STATE (branch_fragP->fr_subtype) == COND_JUMP
11736 && i386_macro_fusible_p (maybe_cmp_fragP->tc_frag_data.mf_type,
11737 pad_fragP->tc_frag_data.mf_type))
11738 return branch_fragP;
e379e5f3
L
11739 }
11740
11741 return NULL;
11742}
11743
11744/* Classify BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags. */
11745
11746static void
11747i386_classify_machine_dependent_frag (fragS *fragP)
11748{
11749 fragS *cmp_fragP;
11750 fragS *pad_fragP;
11751 fragS *branch_fragP;
11752 fragS *next_fragP;
11753 unsigned int max_prefix_length;
11754
11755 if (fragP->tc_frag_data.classified)
11756 return;
11757
11758 /* First scan for BRANCH_PADDING and FUSED_JCC_PADDING. Convert
11759 FUSED_JCC_PADDING and merge BRANCH_PADDING. */
11760 for (next_fragP = fragP;
11761 next_fragP != NULL;
11762 next_fragP = next_fragP->fr_next)
11763 {
11764 next_fragP->tc_frag_data.classified = 1;
11765 if (next_fragP->fr_type == rs_machine_dependent)
11766 switch (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype))
11767 {
11768 case BRANCH_PADDING:
11769 /* The BRANCH_PADDING frag must be followed by a branch
11770 frag. */
11771 branch_fragP = i386_next_non_empty_frag (next_fragP);
11772 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
11773 break;
11774 case FUSED_JCC_PADDING:
11775 /* Check if this is a fused jcc:
11776 FUSED_JCC_PADDING
11777 CMP like instruction
11778 BRANCH_PADDING
11779 COND_JUMP
11780 */
11781 cmp_fragP = i386_next_non_empty_frag (next_fragP);
11782 pad_fragP = i386_next_non_empty_frag (cmp_fragP);
79d72f45 11783 branch_fragP = i386_next_fusible_jcc_frag (next_fragP, pad_fragP);
e379e5f3
L
11784 if (branch_fragP)
11785 {
11786 /* The BRANCH_PADDING frag is merged with the
11787 FUSED_JCC_PADDING frag. */
11788 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
11789 /* CMP like instruction size. */
11790 next_fragP->tc_frag_data.cmp_size = cmp_fragP->fr_fix;
11791 frag_wane (pad_fragP);
11792 /* Skip to branch_fragP. */
11793 next_fragP = branch_fragP;
11794 }
11795 else if (next_fragP->tc_frag_data.max_prefix_length)
11796 {
11797 /* Turn FUSED_JCC_PADDING into BRANCH_PREFIX if it isn't
11798 a fused jcc. */
11799 next_fragP->fr_subtype
11800 = ENCODE_RELAX_STATE (BRANCH_PREFIX, 0);
11801 next_fragP->tc_frag_data.max_bytes
11802 = next_fragP->tc_frag_data.max_prefix_length;
11803 /* This will be updated in the BRANCH_PREFIX scan. */
11804 next_fragP->tc_frag_data.max_prefix_length = 0;
11805 }
11806 else
11807 frag_wane (next_fragP);
11808 break;
11809 }
11810 }
11811
11812 /* Stop if there is no BRANCH_PREFIX. */
11813 if (!align_branch_prefix_size)
11814 return;
11815
11816 /* Scan for BRANCH_PREFIX. */
11817 for (; fragP != NULL; fragP = fragP->fr_next)
11818 {
11819 if (fragP->fr_type != rs_machine_dependent
11820 || (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
11821 != BRANCH_PREFIX))
11822 continue;
11823
11824 /* Count all BRANCH_PREFIX frags before BRANCH_PADDING and
11825 COND_JUMP_PREFIX. */
11826 max_prefix_length = 0;
11827 for (next_fragP = fragP;
11828 next_fragP != NULL;
11829 next_fragP = next_fragP->fr_next)
11830 {
11831 if (next_fragP->fr_type == rs_fill)
11832 /* Skip rs_fill frags. */
11833 continue;
11834 else if (next_fragP->fr_type != rs_machine_dependent)
11835 /* Stop for all other frags. */
11836 break;
11837
11838 /* rs_machine_dependent frags. */
11839 if (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
11840 == BRANCH_PREFIX)
11841 {
11842 /* Count BRANCH_PREFIX frags. */
11843 if (max_prefix_length >= MAX_FUSED_JCC_PADDING_SIZE)
11844 {
11845 max_prefix_length = MAX_FUSED_JCC_PADDING_SIZE;
11846 frag_wane (next_fragP);
11847 }
11848 else
11849 max_prefix_length
11850 += next_fragP->tc_frag_data.max_bytes;
11851 }
11852 else if ((TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
11853 == BRANCH_PADDING)
11854 || (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
11855 == FUSED_JCC_PADDING))
11856 {
11857 /* Stop at BRANCH_PADDING and FUSED_JCC_PADDING. */
11858 fragP->tc_frag_data.u.padding_fragP = next_fragP;
11859 break;
11860 }
11861 else
11862 /* Stop for other rs_machine_dependent frags. */
11863 break;
11864 }
11865
11866 fragP->tc_frag_data.max_prefix_length = max_prefix_length;
11867
11868 /* Skip to the next frag. */
11869 fragP = next_fragP;
11870 }
11871}
11872
11873/* Compute padding size for
11874
11875 FUSED_JCC_PADDING
11876 CMP like instruction
11877 BRANCH_PADDING
11878 COND_JUMP/UNCOND_JUMP
11879
11880 or
11881
11882 BRANCH_PADDING
11883 COND_JUMP/UNCOND_JUMP
11884 */
11885
11886static int
11887i386_branch_padding_size (fragS *fragP, offsetT address)
11888{
11889 unsigned int offset, size, padding_size;
11890 fragS *branch_fragP = fragP->tc_frag_data.u.branch_fragP;
11891
11892 /* The start address of the BRANCH_PADDING or FUSED_JCC_PADDING frag. */
11893 if (!address)
11894 address = fragP->fr_address;
11895 address += fragP->fr_fix;
11896
11897 /* CMP like instrunction size. */
11898 size = fragP->tc_frag_data.cmp_size;
11899
11900 /* The base size of the branch frag. */
11901 size += branch_fragP->fr_fix;
11902
11903 /* Add opcode and displacement bytes for the rs_machine_dependent
11904 branch frag. */
11905 if (branch_fragP->fr_type == rs_machine_dependent)
11906 size += md_relax_table[branch_fragP->fr_subtype].rlx_length;
11907
11908 /* Check if branch is within boundary and doesn't end at the last
11909 byte. */
11910 offset = address & ((1U << align_branch_power) - 1);
11911 if ((offset + size) >= (1U << align_branch_power))
11912 /* Padding needed to avoid crossing boundary. */
11913 padding_size = (1U << align_branch_power) - offset;
11914 else
11915 /* No padding needed. */
11916 padding_size = 0;
11917
11918 /* The return value may be saved in tc_frag_data.length which is
11919 unsigned byte. */
11920 if (!fits_in_unsigned_byte (padding_size))
11921 abort ();
11922
11923 return padding_size;
11924}
11925
11926/* i386_generic_table_relax_frag()
11927
11928 Handle BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags to
11929 grow/shrink padding to align branch frags. Hand others to
11930 relax_frag(). */
11931
11932long
11933i386_generic_table_relax_frag (segT segment, fragS *fragP, long stretch)
11934{
11935 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
11936 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
11937 {
11938 long padding_size = i386_branch_padding_size (fragP, 0);
11939 long grow = padding_size - fragP->tc_frag_data.length;
11940
11941 /* When the BRANCH_PREFIX frag is used, the computed address
11942 must match the actual address and there should be no padding. */
11943 if (fragP->tc_frag_data.padding_address
11944 && (fragP->tc_frag_data.padding_address != fragP->fr_address
11945 || padding_size))
11946 abort ();
11947
11948 /* Update the padding size. */
11949 if (grow)
11950 fragP->tc_frag_data.length = padding_size;
11951
11952 return grow;
11953 }
11954 else if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
11955 {
11956 fragS *padding_fragP, *next_fragP;
11957 long padding_size, left_size, last_size;
11958
11959 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
11960 if (!padding_fragP)
11961 /* Use the padding set by the leading BRANCH_PREFIX frag. */
11962 return (fragP->tc_frag_data.length
11963 - fragP->tc_frag_data.last_length);
11964
11965 /* Compute the relative address of the padding frag in the very
11966 first time where the BRANCH_PREFIX frag sizes are zero. */
11967 if (!fragP->tc_frag_data.padding_address)
11968 fragP->tc_frag_data.padding_address
11969 = padding_fragP->fr_address - (fragP->fr_address - stretch);
11970
11971 /* First update the last length from the previous interation. */
11972 left_size = fragP->tc_frag_data.prefix_length;
11973 for (next_fragP = fragP;
11974 next_fragP != padding_fragP;
11975 next_fragP = next_fragP->fr_next)
11976 if (next_fragP->fr_type == rs_machine_dependent
11977 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
11978 == BRANCH_PREFIX))
11979 {
11980 if (left_size)
11981 {
11982 int max = next_fragP->tc_frag_data.max_bytes;
11983 if (max)
11984 {
11985 int size;
11986 if (max > left_size)
11987 size = left_size;
11988 else
11989 size = max;
11990 left_size -= size;
11991 next_fragP->tc_frag_data.last_length = size;
11992 }
11993 }
11994 else
11995 next_fragP->tc_frag_data.last_length = 0;
11996 }
11997
11998 /* Check the padding size for the padding frag. */
11999 padding_size = i386_branch_padding_size
12000 (padding_fragP, (fragP->fr_address
12001 + fragP->tc_frag_data.padding_address));
12002
12003 last_size = fragP->tc_frag_data.prefix_length;
12004 /* Check if there is change from the last interation. */
12005 if (padding_size == last_size)
12006 {
12007 /* Update the expected address of the padding frag. */
12008 padding_fragP->tc_frag_data.padding_address
12009 = (fragP->fr_address + padding_size
12010 + fragP->tc_frag_data.padding_address);
12011 return 0;
12012 }
12013
12014 if (padding_size > fragP->tc_frag_data.max_prefix_length)
12015 {
12016 /* No padding if there is no sufficient room. Clear the
12017 expected address of the padding frag. */
12018 padding_fragP->tc_frag_data.padding_address = 0;
12019 padding_size = 0;
12020 }
12021 else
12022 /* Store the expected address of the padding frag. */
12023 padding_fragP->tc_frag_data.padding_address
12024 = (fragP->fr_address + padding_size
12025 + fragP->tc_frag_data.padding_address);
12026
12027 fragP->tc_frag_data.prefix_length = padding_size;
12028
12029 /* Update the length for the current interation. */
12030 left_size = padding_size;
12031 for (next_fragP = fragP;
12032 next_fragP != padding_fragP;
12033 next_fragP = next_fragP->fr_next)
12034 if (next_fragP->fr_type == rs_machine_dependent
12035 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12036 == BRANCH_PREFIX))
12037 {
12038 if (left_size)
12039 {
12040 int max = next_fragP->tc_frag_data.max_bytes;
12041 if (max)
12042 {
12043 int size;
12044 if (max > left_size)
12045 size = left_size;
12046 else
12047 size = max;
12048 left_size -= size;
12049 next_fragP->tc_frag_data.length = size;
12050 }
12051 }
12052 else
12053 next_fragP->tc_frag_data.length = 0;
12054 }
12055
12056 return (fragP->tc_frag_data.length
12057 - fragP->tc_frag_data.last_length);
12058 }
12059 return relax_frag (segment, fragP, stretch);
12060}
12061
ee7fcc42
AM
12062/* md_estimate_size_before_relax()
12063
12064 Called just before relax() for rs_machine_dependent frags. The x86
12065 assembler uses these frags to handle variable size jump
12066 instructions.
12067
12068 Any symbol that is now undefined will not become defined.
12069 Return the correct fr_subtype in the frag.
12070 Return the initial "guess for variable size of frag" to caller.
12071 The guess is actually the growth beyond the fixed part. Whatever
12072 we do to grow the fixed or variable part contributes to our
12073 returned value. */
12074
252b5132 12075int
7016a5d5 12076md_estimate_size_before_relax (fragS *fragP, segT segment)
252b5132 12077{
e379e5f3
L
12078 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12079 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX
12080 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
12081 {
12082 i386_classify_machine_dependent_frag (fragP);
12083 return fragP->tc_frag_data.length;
12084 }
12085
252b5132 12086 /* We've already got fragP->fr_subtype right; all we have to do is
b98ef147
AM
12087 check for un-relaxable symbols. On an ELF system, we can't relax
12088 an externally visible symbol, because it may be overridden by a
12089 shared library. */
12090 if (S_GET_SEGMENT (fragP->fr_symbol) != segment
6d249963 12091#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12092 || (IS_ELF
8dcea932
L
12093 && !elf_symbol_resolved_in_segment_p (fragP->fr_symbol,
12094 fragP->fr_var))
fbeb56a4
DK
12095#endif
12096#if defined (OBJ_COFF) && defined (TE_PE)
7ab9ffdd 12097 || (OUTPUT_FLAVOR == bfd_target_coff_flavour
fbeb56a4 12098 && S_IS_WEAK (fragP->fr_symbol))
b98ef147
AM
12099#endif
12100 )
252b5132 12101 {
b98ef147
AM
12102 /* Symbol is undefined in this segment, or we need to keep a
12103 reloc so that weak symbols can be overridden. */
12104 int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
f86103b7 12105 enum bfd_reloc_code_real reloc_type;
ee7fcc42
AM
12106 unsigned char *opcode;
12107 int old_fr_fix;
f6af82bd 12108
ee7fcc42 12109 if (fragP->fr_var != NO_RELOC)
1e9cc1c2 12110 reloc_type = (enum bfd_reloc_code_real) fragP->fr_var;
b98ef147 12111 else if (size == 2)
f6af82bd 12112 reloc_type = BFD_RELOC_16_PCREL;
bd7ab16b
L
12113#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
12114 else if (need_plt32_p (fragP->fr_symbol))
12115 reloc_type = BFD_RELOC_X86_64_PLT32;
12116#endif
f6af82bd
AM
12117 else
12118 reloc_type = BFD_RELOC_32_PCREL;
252b5132 12119
ee7fcc42
AM
12120 old_fr_fix = fragP->fr_fix;
12121 opcode = (unsigned char *) fragP->fr_opcode;
12122
fddf5b5b 12123 switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
252b5132 12124 {
fddf5b5b
AM
12125 case UNCOND_JUMP:
12126 /* Make jmp (0xeb) a (d)word displacement jump. */
47926f60 12127 opcode[0] = 0xe9;
252b5132 12128 fragP->fr_fix += size;
062cd5e7
AS
12129 fix_new (fragP, old_fr_fix, size,
12130 fragP->fr_symbol,
12131 fragP->fr_offset, 1,
12132 reloc_type);
252b5132
RH
12133 break;
12134
fddf5b5b 12135 case COND_JUMP86:
412167cb
AM
12136 if (size == 2
12137 && (!no_cond_jump_promotion || fragP->fr_var != NO_RELOC))
fddf5b5b
AM
12138 {
12139 /* Negate the condition, and branch past an
12140 unconditional jump. */
12141 opcode[0] ^= 1;
12142 opcode[1] = 3;
12143 /* Insert an unconditional jump. */
12144 opcode[2] = 0xe9;
12145 /* We added two extra opcode bytes, and have a two byte
12146 offset. */
12147 fragP->fr_fix += 2 + 2;
062cd5e7
AS
12148 fix_new (fragP, old_fr_fix + 2, 2,
12149 fragP->fr_symbol,
12150 fragP->fr_offset, 1,
12151 reloc_type);
fddf5b5b
AM
12152 break;
12153 }
12154 /* Fall through. */
12155
12156 case COND_JUMP:
412167cb
AM
12157 if (no_cond_jump_promotion && fragP->fr_var == NO_RELOC)
12158 {
3e02c1cc
AM
12159 fixS *fixP;
12160
412167cb 12161 fragP->fr_fix += 1;
3e02c1cc
AM
12162 fixP = fix_new (fragP, old_fr_fix, 1,
12163 fragP->fr_symbol,
12164 fragP->fr_offset, 1,
12165 BFD_RELOC_8_PCREL);
12166 fixP->fx_signed = 1;
412167cb
AM
12167 break;
12168 }
93c2a809 12169
24eab124 12170 /* This changes the byte-displacement jump 0x7N
fddf5b5b 12171 to the (d)word-displacement jump 0x0f,0x8N. */
252b5132 12172 opcode[1] = opcode[0] + 0x10;
f6af82bd 12173 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
47926f60
KH
12174 /* We've added an opcode byte. */
12175 fragP->fr_fix += 1 + size;
062cd5e7
AS
12176 fix_new (fragP, old_fr_fix + 1, size,
12177 fragP->fr_symbol,
12178 fragP->fr_offset, 1,
12179 reloc_type);
252b5132 12180 break;
fddf5b5b
AM
12181
12182 default:
12183 BAD_CASE (fragP->fr_subtype);
12184 break;
252b5132
RH
12185 }
12186 frag_wane (fragP);
ee7fcc42 12187 return fragP->fr_fix - old_fr_fix;
252b5132 12188 }
93c2a809 12189
93c2a809
AM
12190 /* Guess size depending on current relax state. Initially the relax
12191 state will correspond to a short jump and we return 1, because
12192 the variable part of the frag (the branch offset) is one byte
12193 long. However, we can relax a section more than once and in that
12194 case we must either set fr_subtype back to the unrelaxed state,
12195 or return the value for the appropriate branch. */
12196 return md_relax_table[fragP->fr_subtype].rlx_length;
ee7fcc42
AM
12197}
12198
47926f60
KH
12199/* Called after relax() is finished.
12200
12201 In: Address of frag.
12202 fr_type == rs_machine_dependent.
12203 fr_subtype is what the address relaxed to.
12204
12205 Out: Any fixSs and constants are set up.
12206 Caller will turn frag into a ".space 0". */
12207
252b5132 12208void
7016a5d5
TG
12209md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec ATTRIBUTE_UNUSED,
12210 fragS *fragP)
252b5132 12211{
29b0f896 12212 unsigned char *opcode;
252b5132 12213 unsigned char *where_to_put_displacement = NULL;
847f7ad4
AM
12214 offsetT target_address;
12215 offsetT opcode_address;
252b5132 12216 unsigned int extension = 0;
847f7ad4 12217 offsetT displacement_from_opcode_start;
252b5132 12218
e379e5f3
L
12219 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12220 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING
12221 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12222 {
12223 /* Generate nop padding. */
12224 unsigned int size = fragP->tc_frag_data.length;
12225 if (size)
12226 {
12227 if (size > fragP->tc_frag_data.max_bytes)
12228 abort ();
12229
12230 if (flag_debug)
12231 {
12232 const char *msg;
12233 const char *branch = "branch";
12234 const char *prefix = "";
12235 fragS *padding_fragP;
12236 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
12237 == BRANCH_PREFIX)
12238 {
12239 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
12240 switch (fragP->tc_frag_data.default_prefix)
12241 {
12242 default:
12243 abort ();
12244 break;
12245 case CS_PREFIX_OPCODE:
12246 prefix = " cs";
12247 break;
12248 case DS_PREFIX_OPCODE:
12249 prefix = " ds";
12250 break;
12251 case ES_PREFIX_OPCODE:
12252 prefix = " es";
12253 break;
12254 case FS_PREFIX_OPCODE:
12255 prefix = " fs";
12256 break;
12257 case GS_PREFIX_OPCODE:
12258 prefix = " gs";
12259 break;
12260 case SS_PREFIX_OPCODE:
12261 prefix = " ss";
12262 break;
12263 }
12264 if (padding_fragP)
12265 msg = _("%s:%u: add %d%s at 0x%llx to align "
12266 "%s within %d-byte boundary\n");
12267 else
12268 msg = _("%s:%u: add additional %d%s at 0x%llx to "
12269 "align %s within %d-byte boundary\n");
12270 }
12271 else
12272 {
12273 padding_fragP = fragP;
12274 msg = _("%s:%u: add %d%s-byte nop at 0x%llx to align "
12275 "%s within %d-byte boundary\n");
12276 }
12277
12278 if (padding_fragP)
12279 switch (padding_fragP->tc_frag_data.branch_type)
12280 {
12281 case align_branch_jcc:
12282 branch = "jcc";
12283 break;
12284 case align_branch_fused:
12285 branch = "fused jcc";
12286 break;
12287 case align_branch_jmp:
12288 branch = "jmp";
12289 break;
12290 case align_branch_call:
12291 branch = "call";
12292 break;
12293 case align_branch_indirect:
12294 branch = "indiret branch";
12295 break;
12296 case align_branch_ret:
12297 branch = "ret";
12298 break;
12299 default:
12300 break;
12301 }
12302
12303 fprintf (stdout, msg,
12304 fragP->fr_file, fragP->fr_line, size, prefix,
12305 (long long) fragP->fr_address, branch,
12306 1 << align_branch_power);
12307 }
12308 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12309 memset (fragP->fr_opcode,
12310 fragP->tc_frag_data.default_prefix, size);
12311 else
12312 i386_generate_nops (fragP, (char *) fragP->fr_opcode,
12313 size, 0);
12314 fragP->fr_fix += size;
12315 }
12316 return;
12317 }
12318
252b5132
RH
12319 opcode = (unsigned char *) fragP->fr_opcode;
12320
47926f60 12321 /* Address we want to reach in file space. */
252b5132 12322 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
252b5132 12323
47926f60 12324 /* Address opcode resides at in file space. */
252b5132
RH
12325 opcode_address = fragP->fr_address + fragP->fr_fix;
12326
47926f60 12327 /* Displacement from opcode start to fill into instruction. */
252b5132
RH
12328 displacement_from_opcode_start = target_address - opcode_address;
12329
fddf5b5b 12330 if ((fragP->fr_subtype & BIG) == 0)
252b5132 12331 {
47926f60
KH
12332 /* Don't have to change opcode. */
12333 extension = 1; /* 1 opcode + 1 displacement */
252b5132 12334 where_to_put_displacement = &opcode[1];
fddf5b5b
AM
12335 }
12336 else
12337 {
12338 if (no_cond_jump_promotion
12339 && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
4eed87de
AM
12340 as_warn_where (fragP->fr_file, fragP->fr_line,
12341 _("long jump required"));
252b5132 12342
fddf5b5b
AM
12343 switch (fragP->fr_subtype)
12344 {
12345 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
12346 extension = 4; /* 1 opcode + 4 displacement */
12347 opcode[0] = 0xe9;
12348 where_to_put_displacement = &opcode[1];
12349 break;
252b5132 12350
fddf5b5b
AM
12351 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
12352 extension = 2; /* 1 opcode + 2 displacement */
12353 opcode[0] = 0xe9;
12354 where_to_put_displacement = &opcode[1];
12355 break;
252b5132 12356
fddf5b5b
AM
12357 case ENCODE_RELAX_STATE (COND_JUMP, BIG):
12358 case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
12359 extension = 5; /* 2 opcode + 4 displacement */
12360 opcode[1] = opcode[0] + 0x10;
12361 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12362 where_to_put_displacement = &opcode[2];
12363 break;
252b5132 12364
fddf5b5b
AM
12365 case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
12366 extension = 3; /* 2 opcode + 2 displacement */
12367 opcode[1] = opcode[0] + 0x10;
12368 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12369 where_to_put_displacement = &opcode[2];
12370 break;
252b5132 12371
fddf5b5b
AM
12372 case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
12373 extension = 4;
12374 opcode[0] ^= 1;
12375 opcode[1] = 3;
12376 opcode[2] = 0xe9;
12377 where_to_put_displacement = &opcode[3];
12378 break;
12379
12380 default:
12381 BAD_CASE (fragP->fr_subtype);
12382 break;
12383 }
252b5132 12384 }
fddf5b5b 12385
7b81dfbb
AJ
12386 /* If size if less then four we are sure that the operand fits,
12387 but if it's 4, then it could be that the displacement is larger
12388 then -/+ 2GB. */
12389 if (DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype) == 4
12390 && object_64bit
12391 && ((addressT) (displacement_from_opcode_start - extension
4eed87de
AM
12392 + ((addressT) 1 << 31))
12393 > (((addressT) 2 << 31) - 1)))
7b81dfbb
AJ
12394 {
12395 as_bad_where (fragP->fr_file, fragP->fr_line,
12396 _("jump target out of range"));
12397 /* Make us emit 0. */
12398 displacement_from_opcode_start = extension;
12399 }
47926f60 12400 /* Now put displacement after opcode. */
252b5132
RH
12401 md_number_to_chars ((char *) where_to_put_displacement,
12402 (valueT) (displacement_from_opcode_start - extension),
fddf5b5b 12403 DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
252b5132
RH
12404 fragP->fr_fix += extension;
12405}
12406\f
7016a5d5 12407/* Apply a fixup (fixP) to segment data, once it has been determined
252b5132
RH
12408 by our caller that we have all the info we need to fix it up.
12409
7016a5d5
TG
12410 Parameter valP is the pointer to the value of the bits.
12411
252b5132
RH
12412 On the 386, immediates, displacements, and data pointers are all in
12413 the same (little-endian) format, so we don't need to care about which
12414 we are handling. */
12415
94f592af 12416void
7016a5d5 12417md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 12418{
94f592af 12419 char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
c6682705 12420 valueT value = *valP;
252b5132 12421
f86103b7 12422#if !defined (TE_Mach)
93382f6d
AM
12423 if (fixP->fx_pcrel)
12424 {
12425 switch (fixP->fx_r_type)
12426 {
5865bb77
ILT
12427 default:
12428 break;
12429
d6ab8113
JB
12430 case BFD_RELOC_64:
12431 fixP->fx_r_type = BFD_RELOC_64_PCREL;
12432 break;
93382f6d 12433 case BFD_RELOC_32:
ae8887b5 12434 case BFD_RELOC_X86_64_32S:
93382f6d
AM
12435 fixP->fx_r_type = BFD_RELOC_32_PCREL;
12436 break;
12437 case BFD_RELOC_16:
12438 fixP->fx_r_type = BFD_RELOC_16_PCREL;
12439 break;
12440 case BFD_RELOC_8:
12441 fixP->fx_r_type = BFD_RELOC_8_PCREL;
12442 break;
12443 }
12444 }
252b5132 12445
a161fe53 12446 if (fixP->fx_addsy != NULL
31312f95 12447 && (fixP->fx_r_type == BFD_RELOC_32_PCREL
d6ab8113 12448 || fixP->fx_r_type == BFD_RELOC_64_PCREL
31312f95 12449 || fixP->fx_r_type == BFD_RELOC_16_PCREL
d258b828 12450 || fixP->fx_r_type == BFD_RELOC_8_PCREL)
31312f95 12451 && !use_rela_relocations)
252b5132 12452 {
31312f95
AM
12453 /* This is a hack. There should be a better way to handle this.
12454 This covers for the fact that bfd_install_relocation will
12455 subtract the current location (for partial_inplace, PC relative
12456 relocations); see more below. */
252b5132 12457#ifndef OBJ_AOUT
718ddfc0 12458 if (IS_ELF
252b5132
RH
12459#ifdef TE_PE
12460 || OUTPUT_FLAVOR == bfd_target_coff_flavour
12461#endif
12462 )
12463 value += fixP->fx_where + fixP->fx_frag->fr_address;
12464#endif
12465#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12466 if (IS_ELF)
252b5132 12467 {
6539b54b 12468 segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
2f66722d 12469
6539b54b 12470 if ((sym_seg == seg
2f66722d 12471 || (symbol_section_p (fixP->fx_addsy)
6539b54b 12472 && sym_seg != absolute_section))
af65af87 12473 && !generic_force_reloc (fixP))
2f66722d
AM
12474 {
12475 /* Yes, we add the values in twice. This is because
6539b54b
AM
12476 bfd_install_relocation subtracts them out again. I think
12477 bfd_install_relocation is broken, but I don't dare change
2f66722d
AM
12478 it. FIXME. */
12479 value += fixP->fx_where + fixP->fx_frag->fr_address;
12480 }
252b5132
RH
12481 }
12482#endif
12483#if defined (OBJ_COFF) && defined (TE_PE)
977cdf5a
NC
12484 /* For some reason, the PE format does not store a
12485 section address offset for a PC relative symbol. */
12486 if (S_GET_SEGMENT (fixP->fx_addsy) != seg
7be1c489 12487 || S_IS_WEAK (fixP->fx_addsy))
252b5132
RH
12488 value += md_pcrel_from (fixP);
12489#endif
12490 }
fbeb56a4 12491#if defined (OBJ_COFF) && defined (TE_PE)
f01c1a09
NC
12492 if (fixP->fx_addsy != NULL
12493 && S_IS_WEAK (fixP->fx_addsy)
12494 /* PR 16858: Do not modify weak function references. */
12495 && ! fixP->fx_pcrel)
fbeb56a4 12496 {
296a8689
NC
12497#if !defined (TE_PEP)
12498 /* For x86 PE weak function symbols are neither PC-relative
12499 nor do they set S_IS_FUNCTION. So the only reliable way
12500 to detect them is to check the flags of their containing
12501 section. */
12502 if (S_GET_SEGMENT (fixP->fx_addsy) != NULL
12503 && S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_CODE)
12504 ;
12505 else
12506#endif
fbeb56a4
DK
12507 value -= S_GET_VALUE (fixP->fx_addsy);
12508 }
12509#endif
252b5132
RH
12510
12511 /* Fix a few things - the dynamic linker expects certain values here,
0234cb7c 12512 and we must not disappoint it. */
252b5132 12513#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12514 if (IS_ELF && fixP->fx_addsy)
47926f60
KH
12515 switch (fixP->fx_r_type)
12516 {
12517 case BFD_RELOC_386_PLT32:
3e73aa7c 12518 case BFD_RELOC_X86_64_PLT32:
b9519cfe
L
12519 /* Make the jump instruction point to the address of the operand.
12520 At runtime we merely add the offset to the actual PLT entry.
12521 NB: Subtract the offset size only for jump instructions. */
12522 if (fixP->fx_pcrel)
12523 value = -4;
47926f60 12524 break;
31312f95 12525
13ae64f3
JJ
12526 case BFD_RELOC_386_TLS_GD:
12527 case BFD_RELOC_386_TLS_LDM:
13ae64f3 12528 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
12529 case BFD_RELOC_386_TLS_IE:
12530 case BFD_RELOC_386_TLS_GOTIE:
67a4f2b7 12531 case BFD_RELOC_386_TLS_GOTDESC:
bffbf940
JJ
12532 case BFD_RELOC_X86_64_TLSGD:
12533 case BFD_RELOC_X86_64_TLSLD:
12534 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7 12535 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
00f7efb6
JJ
12536 value = 0; /* Fully resolved at runtime. No addend. */
12537 /* Fallthrough */
12538 case BFD_RELOC_386_TLS_LE:
12539 case BFD_RELOC_386_TLS_LDO_32:
12540 case BFD_RELOC_386_TLS_LE_32:
12541 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 12542 case BFD_RELOC_X86_64_DTPOFF64:
00f7efb6 12543 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113 12544 case BFD_RELOC_X86_64_TPOFF64:
00f7efb6
JJ
12545 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12546 break;
12547
67a4f2b7
AO
12548 case BFD_RELOC_386_TLS_DESC_CALL:
12549 case BFD_RELOC_X86_64_TLSDESC_CALL:
12550 value = 0; /* Fully resolved at runtime. No addend. */
12551 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12552 fixP->fx_done = 0;
12553 return;
12554
47926f60
KH
12555 case BFD_RELOC_VTABLE_INHERIT:
12556 case BFD_RELOC_VTABLE_ENTRY:
12557 fixP->fx_done = 0;
94f592af 12558 return;
47926f60
KH
12559
12560 default:
12561 break;
12562 }
12563#endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) */
c6682705 12564 *valP = value;
f86103b7 12565#endif /* !defined (TE_Mach) */
3e73aa7c 12566
3e73aa7c 12567 /* Are we finished with this relocation now? */
c6682705 12568 if (fixP->fx_addsy == NULL)
3e73aa7c 12569 fixP->fx_done = 1;
fbeb56a4
DK
12570#if defined (OBJ_COFF) && defined (TE_PE)
12571 else if (fixP->fx_addsy != NULL && S_IS_WEAK (fixP->fx_addsy))
12572 {
12573 fixP->fx_done = 0;
12574 /* Remember value for tc_gen_reloc. */
12575 fixP->fx_addnumber = value;
12576 /* Clear out the frag for now. */
12577 value = 0;
12578 }
12579#endif
3e73aa7c
JH
12580 else if (use_rela_relocations)
12581 {
12582 fixP->fx_no_overflow = 1;
062cd5e7
AS
12583 /* Remember value for tc_gen_reloc. */
12584 fixP->fx_addnumber = value;
3e73aa7c
JH
12585 value = 0;
12586 }
f86103b7 12587
94f592af 12588 md_number_to_chars (p, value, fixP->fx_size);
252b5132 12589}
252b5132 12590\f
6d4af3c2 12591const char *
499ac353 12592md_atof (int type, char *litP, int *sizeP)
252b5132 12593{
499ac353
NC
12594 /* This outputs the LITTLENUMs in REVERSE order;
12595 in accord with the bigendian 386. */
5b7c81bd 12596 return ieee_md_atof (type, litP, sizeP, false);
252b5132
RH
12597}
12598\f
2d545b82 12599static char output_invalid_buf[sizeof (unsigned char) * 2 + 6];
252b5132 12600
252b5132 12601static char *
e3bb37b5 12602output_invalid (int c)
252b5132 12603{
3882b010 12604 if (ISPRINT (c))
f9f21a03
L
12605 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
12606 "'%c'", c);
252b5132 12607 else
f9f21a03 12608 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
2d545b82 12609 "(0x%x)", (unsigned char) c);
252b5132
RH
12610 return output_invalid_buf;
12611}
12612
8a6fb3f9
JB
12613/* Verify that @r can be used in the current context. */
12614
5b7c81bd 12615static bool check_register (const reg_entry *r)
8a6fb3f9
JB
12616{
12617 if (allow_pseudo_reg)
5b7c81bd 12618 return true;
8a6fb3f9
JB
12619
12620 if (operand_type_all_zero (&r->reg_type))
5b7c81bd 12621 return false;
8a6fb3f9
JB
12622
12623 if ((r->reg_type.bitfield.dword
12624 || (r->reg_type.bitfield.class == SReg && r->reg_num > 3)
12625 || r->reg_type.bitfield.class == RegCR
22e00a3f 12626 || r->reg_type.bitfield.class == RegDR)
8a6fb3f9 12627 && !cpu_arch_flags.bitfield.cpui386)
5b7c81bd 12628 return false;
8a6fb3f9 12629
22e00a3f
JB
12630 if (r->reg_type.bitfield.class == RegTR
12631 && (flag_code == CODE_64BIT
12632 || !cpu_arch_flags.bitfield.cpui386
12633 || cpu_arch_isa_flags.bitfield.cpui586
12634 || cpu_arch_isa_flags.bitfield.cpui686))
5b7c81bd 12635 return false;
22e00a3f 12636
8a6fb3f9 12637 if (r->reg_type.bitfield.class == RegMMX && !cpu_arch_flags.bitfield.cpummx)
5b7c81bd 12638 return false;
8a6fb3f9
JB
12639
12640 if (!cpu_arch_flags.bitfield.cpuavx512f)
12641 {
12642 if (r->reg_type.bitfield.zmmword
12643 || r->reg_type.bitfield.class == RegMask)
5b7c81bd 12644 return false;
8a6fb3f9
JB
12645
12646 if (!cpu_arch_flags.bitfield.cpuavx)
12647 {
12648 if (r->reg_type.bitfield.ymmword)
5b7c81bd 12649 return false;
8a6fb3f9
JB
12650
12651 if (!cpu_arch_flags.bitfield.cpusse && r->reg_type.bitfield.xmmword)
5b7c81bd 12652 return false;
8a6fb3f9
JB
12653 }
12654 }
12655
260cd341
LC
12656 if (r->reg_type.bitfield.tmmword
12657 && (!cpu_arch_flags.bitfield.cpuamx_tile
12658 || flag_code != CODE_64BIT))
5b7c81bd 12659 return false;
260cd341 12660
8a6fb3f9 12661 if (r->reg_type.bitfield.class == RegBND && !cpu_arch_flags.bitfield.cpumpx)
5b7c81bd 12662 return false;
8a6fb3f9
JB
12663
12664 /* Don't allow fake index register unless allow_index_reg isn't 0. */
12665 if (!allow_index_reg && r->reg_num == RegIZ)
5b7c81bd 12666 return false;
8a6fb3f9
JB
12667
12668 /* Upper 16 vector registers are only available with VREX in 64bit
12669 mode, and require EVEX encoding. */
12670 if (r->reg_flags & RegVRex)
12671 {
12672 if (!cpu_arch_flags.bitfield.cpuavx512f
12673 || flag_code != CODE_64BIT)
5b7c81bd 12674 return false;
8a6fb3f9 12675
da4977e0
JB
12676 if (i.vec_encoding == vex_encoding_default)
12677 i.vec_encoding = vex_encoding_evex;
12678 else if (i.vec_encoding != vex_encoding_evex)
12679 i.vec_encoding = vex_encoding_error;
8a6fb3f9
JB
12680 }
12681
12682 if (((r->reg_flags & (RegRex64 | RegRex)) || r->reg_type.bitfield.qword)
12683 && (!cpu_arch_flags.bitfield.cpulm || r->reg_type.bitfield.class != RegCR)
12684 && flag_code != CODE_64BIT)
5b7c81bd 12685 return false;
8a6fb3f9
JB
12686
12687 if (r->reg_type.bitfield.class == SReg && r->reg_num == RegFlat
12688 && !intel_syntax)
5b7c81bd 12689 return false;
8a6fb3f9 12690
5b7c81bd 12691 return true;
8a6fb3f9
JB
12692}
12693
af6bdddf 12694/* REG_STRING starts *before* REGISTER_PREFIX. */
252b5132
RH
12695
12696static const reg_entry *
4d1bb795 12697parse_real_register (char *reg_string, char **end_op)
252b5132 12698{
af6bdddf
AM
12699 char *s = reg_string;
12700 char *p;
252b5132
RH
12701 char reg_name_given[MAX_REG_NAME_SIZE + 1];
12702 const reg_entry *r;
12703
12704 /* Skip possible REGISTER_PREFIX and possible whitespace. */
12705 if (*s == REGISTER_PREFIX)
12706 ++s;
12707
12708 if (is_space_char (*s))
12709 ++s;
12710
12711 p = reg_name_given;
af6bdddf 12712 while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
252b5132
RH
12713 {
12714 if (p >= reg_name_given + MAX_REG_NAME_SIZE)
af6bdddf
AM
12715 return (const reg_entry *) NULL;
12716 s++;
252b5132
RH
12717 }
12718
6588847e
DN
12719 /* For naked regs, make sure that we are not dealing with an identifier.
12720 This prevents confusing an identifier like `eax_var' with register
12721 `eax'. */
12722 if (allow_naked_reg && identifier_chars[(unsigned char) *s])
12723 return (const reg_entry *) NULL;
12724
af6bdddf 12725 *end_op = s;
252b5132 12726
629310ab 12727 r = (const reg_entry *) str_hash_find (reg_hash, reg_name_given);
252b5132 12728
5f47d35b 12729 /* Handle floating point regs, allowing spaces in the (i) part. */
6288d05f 12730 if (r == reg_st0)
5f47d35b 12731 {
0e0eea78
JB
12732 if (!cpu_arch_flags.bitfield.cpu8087
12733 && !cpu_arch_flags.bitfield.cpu287
af32b722
JB
12734 && !cpu_arch_flags.bitfield.cpu387
12735 && !allow_pseudo_reg)
0e0eea78
JB
12736 return (const reg_entry *) NULL;
12737
5f47d35b
AM
12738 if (is_space_char (*s))
12739 ++s;
12740 if (*s == '(')
12741 {
af6bdddf 12742 ++s;
5f47d35b
AM
12743 if (is_space_char (*s))
12744 ++s;
12745 if (*s >= '0' && *s <= '7')
12746 {
db557034 12747 int fpr = *s - '0';
af6bdddf 12748 ++s;
5f47d35b
AM
12749 if (is_space_char (*s))
12750 ++s;
12751 if (*s == ')')
12752 {
12753 *end_op = s + 1;
6288d05f 12754 know (r[fpr].reg_num == fpr);
db557034 12755 return r + fpr;
5f47d35b 12756 }
5f47d35b 12757 }
47926f60 12758 /* We have "%st(" then garbage. */
5f47d35b
AM
12759 return (const reg_entry *) NULL;
12760 }
12761 }
12762
8a6fb3f9 12763 return r && check_register (r) ? r : NULL;
252b5132 12764}
4d1bb795
JB
12765
12766/* REG_STRING starts *before* REGISTER_PREFIX. */
12767
12768static const reg_entry *
12769parse_register (char *reg_string, char **end_op)
12770{
12771 const reg_entry *r;
12772
12773 if (*reg_string == REGISTER_PREFIX || allow_naked_reg)
12774 r = parse_real_register (reg_string, end_op);
12775 else
12776 r = NULL;
12777 if (!r)
12778 {
12779 char *save = input_line_pointer;
12780 char c;
12781 symbolS *symbolP;
12782
12783 input_line_pointer = reg_string;
d02603dc 12784 c = get_symbol_name (&reg_string);
4d1bb795
JB
12785 symbolP = symbol_find (reg_string);
12786 if (symbolP && S_GET_SEGMENT (symbolP) == reg_section)
12787 {
12788 const expressionS *e = symbol_get_value_expression (symbolP);
12789
0398aac5 12790 know (e->X_op == O_register);
4eed87de 12791 know (e->X_add_number >= 0
c3fe08fa 12792 && (valueT) e->X_add_number < i386_regtab_size);
4d1bb795 12793 r = i386_regtab + e->X_add_number;
8a6fb3f9
JB
12794 if (!check_register (r))
12795 {
12796 as_bad (_("register '%s%s' cannot be used here"),
12797 register_prefix, r->reg_name);
12798 r = &bad_reg;
12799 }
4d1bb795
JB
12800 *end_op = input_line_pointer;
12801 }
12802 *input_line_pointer = c;
12803 input_line_pointer = save;
12804 }
12805 return r;
12806}
12807
12808int
12809i386_parse_name (char *name, expressionS *e, char *nextcharP)
12810{
12811 const reg_entry *r;
12812 char *end = input_line_pointer;
12813
12814 *end = *nextcharP;
12815 r = parse_register (name, &input_line_pointer);
12816 if (r && end <= input_line_pointer)
12817 {
12818 *nextcharP = *input_line_pointer;
12819 *input_line_pointer = 0;
8a6fb3f9
JB
12820 if (r != &bad_reg)
12821 {
12822 e->X_op = O_register;
12823 e->X_add_number = r - i386_regtab;
12824 }
12825 else
12826 e->X_op = O_illegal;
4d1bb795
JB
12827 return 1;
12828 }
12829 input_line_pointer = end;
12830 *end = 0;
ee86248c 12831 return intel_syntax ? i386_intel_parse_name (name, e) : 0;
4d1bb795
JB
12832}
12833
12834void
12835md_operand (expressionS *e)
12836{
ee86248c
JB
12837 char *end;
12838 const reg_entry *r;
4d1bb795 12839
ee86248c
JB
12840 switch (*input_line_pointer)
12841 {
12842 case REGISTER_PREFIX:
12843 r = parse_real_register (input_line_pointer, &end);
4d1bb795
JB
12844 if (r)
12845 {
12846 e->X_op = O_register;
12847 e->X_add_number = r - i386_regtab;
12848 input_line_pointer = end;
12849 }
ee86248c
JB
12850 break;
12851
12852 case '[':
9c2799c2 12853 gas_assert (intel_syntax);
ee86248c
JB
12854 end = input_line_pointer++;
12855 expression (e);
12856 if (*input_line_pointer == ']')
12857 {
12858 ++input_line_pointer;
12859 e->X_op_symbol = make_expr_symbol (e);
12860 e->X_add_symbol = NULL;
12861 e->X_add_number = 0;
12862 e->X_op = O_index;
12863 }
12864 else
12865 {
12866 e->X_op = O_absent;
12867 input_line_pointer = end;
12868 }
12869 break;
4d1bb795
JB
12870 }
12871}
12872
252b5132 12873\f
4cc782b5 12874#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
b6f8c7c4 12875const char *md_shortopts = "kVQ:sqnO::";
252b5132 12876#else
b6f8c7c4 12877const char *md_shortopts = "qnO::";
252b5132 12878#endif
6e0b89ee 12879
3e73aa7c 12880#define OPTION_32 (OPTION_MD_BASE + 0)
b3b91714
AM
12881#define OPTION_64 (OPTION_MD_BASE + 1)
12882#define OPTION_DIVIDE (OPTION_MD_BASE + 2)
9103f4f4
L
12883#define OPTION_MARCH (OPTION_MD_BASE + 3)
12884#define OPTION_MTUNE (OPTION_MD_BASE + 4)
1efbbeb4
L
12885#define OPTION_MMNEMONIC (OPTION_MD_BASE + 5)
12886#define OPTION_MSYNTAX (OPTION_MD_BASE + 6)
12887#define OPTION_MINDEX_REG (OPTION_MD_BASE + 7)
12888#define OPTION_MNAKED_REG (OPTION_MD_BASE + 8)
bd5dea88 12889#define OPTION_MRELAX_RELOCATIONS (OPTION_MD_BASE + 9)
c0f3af97 12890#define OPTION_MSSE2AVX (OPTION_MD_BASE + 10)
daf50ae7 12891#define OPTION_MSSE_CHECK (OPTION_MD_BASE + 11)
7bab8ab5
JB
12892#define OPTION_MOPERAND_CHECK (OPTION_MD_BASE + 12)
12893#define OPTION_MAVXSCALAR (OPTION_MD_BASE + 13)
12894#define OPTION_X32 (OPTION_MD_BASE + 14)
7e8b059b 12895#define OPTION_MADD_BND_PREFIX (OPTION_MD_BASE + 15)
43234a1e
L
12896#define OPTION_MEVEXLIG (OPTION_MD_BASE + 16)
12897#define OPTION_MEVEXWIG (OPTION_MD_BASE + 17)
167ad85b 12898#define OPTION_MBIG_OBJ (OPTION_MD_BASE + 18)
d1982f93 12899#define OPTION_MOMIT_LOCK_PREFIX (OPTION_MD_BASE + 19)
d3d3c6db 12900#define OPTION_MEVEXRCIG (OPTION_MD_BASE + 20)
8dcea932 12901#define OPTION_MSHARED (OPTION_MD_BASE + 21)
5db04b09
L
12902#define OPTION_MAMD64 (OPTION_MD_BASE + 22)
12903#define OPTION_MINTEL64 (OPTION_MD_BASE + 23)
e4e00185 12904#define OPTION_MFENCE_AS_LOCK_ADD (OPTION_MD_BASE + 24)
b4a3a7b4 12905#define OPTION_X86_USED_NOTE (OPTION_MD_BASE + 25)
03751133 12906#define OPTION_MVEXWIG (OPTION_MD_BASE + 26)
e379e5f3
L
12907#define OPTION_MALIGN_BRANCH_BOUNDARY (OPTION_MD_BASE + 27)
12908#define OPTION_MALIGN_BRANCH_PREFIX_SIZE (OPTION_MD_BASE + 28)
12909#define OPTION_MALIGN_BRANCH (OPTION_MD_BASE + 29)
76cf450b 12910#define OPTION_MBRANCHES_WITH_32B_BOUNDARIES (OPTION_MD_BASE + 30)
ae531041
L
12911#define OPTION_MLFENCE_AFTER_LOAD (OPTION_MD_BASE + 31)
12912#define OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH (OPTION_MD_BASE + 32)
12913#define OPTION_MLFENCE_BEFORE_RET (OPTION_MD_BASE + 33)
b3b91714 12914
99ad8390
NC
12915struct option md_longopts[] =
12916{
3e73aa7c 12917 {"32", no_argument, NULL, OPTION_32},
321098a5 12918#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 12919 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c 12920 {"64", no_argument, NULL, OPTION_64},
351f65ca
L
12921#endif
12922#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 12923 {"x32", no_argument, NULL, OPTION_X32},
8dcea932 12924 {"mshared", no_argument, NULL, OPTION_MSHARED},
b4a3a7b4 12925 {"mx86-used-note", required_argument, NULL, OPTION_X86_USED_NOTE},
6e0b89ee 12926#endif
b3b91714 12927 {"divide", no_argument, NULL, OPTION_DIVIDE},
9103f4f4
L
12928 {"march", required_argument, NULL, OPTION_MARCH},
12929 {"mtune", required_argument, NULL, OPTION_MTUNE},
1efbbeb4
L
12930 {"mmnemonic", required_argument, NULL, OPTION_MMNEMONIC},
12931 {"msyntax", required_argument, NULL, OPTION_MSYNTAX},
12932 {"mindex-reg", no_argument, NULL, OPTION_MINDEX_REG},
12933 {"mnaked-reg", no_argument, NULL, OPTION_MNAKED_REG},
c0f3af97 12934 {"msse2avx", no_argument, NULL, OPTION_MSSE2AVX},
daf50ae7 12935 {"msse-check", required_argument, NULL, OPTION_MSSE_CHECK},
7bab8ab5 12936 {"moperand-check", required_argument, NULL, OPTION_MOPERAND_CHECK},
539f890d 12937 {"mavxscalar", required_argument, NULL, OPTION_MAVXSCALAR},
03751133 12938 {"mvexwig", required_argument, NULL, OPTION_MVEXWIG},
7e8b059b 12939 {"madd-bnd-prefix", no_argument, NULL, OPTION_MADD_BND_PREFIX},
43234a1e
L
12940 {"mevexlig", required_argument, NULL, OPTION_MEVEXLIG},
12941 {"mevexwig", required_argument, NULL, OPTION_MEVEXWIG},
167ad85b
TG
12942# if defined (TE_PE) || defined (TE_PEP)
12943 {"mbig-obj", no_argument, NULL, OPTION_MBIG_OBJ},
12944#endif
d1982f93 12945 {"momit-lock-prefix", required_argument, NULL, OPTION_MOMIT_LOCK_PREFIX},
e4e00185 12946 {"mfence-as-lock-add", required_argument, NULL, OPTION_MFENCE_AS_LOCK_ADD},
0cb4071e 12947 {"mrelax-relocations", required_argument, NULL, OPTION_MRELAX_RELOCATIONS},
d3d3c6db 12948 {"mevexrcig", required_argument, NULL, OPTION_MEVEXRCIG},
e379e5f3
L
12949 {"malign-branch-boundary", required_argument, NULL, OPTION_MALIGN_BRANCH_BOUNDARY},
12950 {"malign-branch-prefix-size", required_argument, NULL, OPTION_MALIGN_BRANCH_PREFIX_SIZE},
12951 {"malign-branch", required_argument, NULL, OPTION_MALIGN_BRANCH},
76cf450b 12952 {"mbranches-within-32B-boundaries", no_argument, NULL, OPTION_MBRANCHES_WITH_32B_BOUNDARIES},
ae531041
L
12953 {"mlfence-after-load", required_argument, NULL, OPTION_MLFENCE_AFTER_LOAD},
12954 {"mlfence-before-indirect-branch", required_argument, NULL,
12955 OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH},
12956 {"mlfence-before-ret", required_argument, NULL, OPTION_MLFENCE_BEFORE_RET},
5db04b09
L
12957 {"mamd64", no_argument, NULL, OPTION_MAMD64},
12958 {"mintel64", no_argument, NULL, OPTION_MINTEL64},
252b5132
RH
12959 {NULL, no_argument, NULL, 0}
12960};
12961size_t md_longopts_size = sizeof (md_longopts);
12962
12963int
17b9d67d 12964md_parse_option (int c, const char *arg)
252b5132 12965{
91d6fa6a 12966 unsigned int j;
e379e5f3 12967 char *arch, *next, *saved, *type;
9103f4f4 12968
252b5132
RH
12969 switch (c)
12970 {
12b55ccc
L
12971 case 'n':
12972 optimize_align_code = 0;
12973 break;
12974
a38cf1db
AM
12975 case 'q':
12976 quiet_warnings = 1;
252b5132
RH
12977 break;
12978
12979#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
a38cf1db
AM
12980 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
12981 should be emitted or not. FIXME: Not implemented. */
12982 case 'Q':
d4693039
JB
12983 if ((arg[0] != 'y' && arg[0] != 'n') || arg[1])
12984 return 0;
252b5132
RH
12985 break;
12986
12987 /* -V: SVR4 argument to print version ID. */
12988 case 'V':
12989 print_version_id ();
12990 break;
12991
a38cf1db
AM
12992 /* -k: Ignore for FreeBSD compatibility. */
12993 case 'k':
252b5132 12994 break;
4cc782b5
ILT
12995
12996 case 's':
12997 /* -s: On i386 Solaris, this tells the native assembler to use
29b0f896 12998 .stab instead of .stab.excl. We always use .stab anyhow. */
4cc782b5 12999 break;
8dcea932
L
13000
13001 case OPTION_MSHARED:
13002 shared = 1;
13003 break;
b4a3a7b4
L
13004
13005 case OPTION_X86_USED_NOTE:
13006 if (strcasecmp (arg, "yes") == 0)
13007 x86_used_note = 1;
13008 else if (strcasecmp (arg, "no") == 0)
13009 x86_used_note = 0;
13010 else
13011 as_fatal (_("invalid -mx86-used-note= option: `%s'"), arg);
13012 break;
13013
13014
99ad8390 13015#endif
321098a5 13016#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 13017 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c
JH
13018 case OPTION_64:
13019 {
13020 const char **list, **l;
13021
3e73aa7c
JH
13022 list = bfd_target_list ();
13023 for (l = list; *l != NULL; l++)
08dedd66 13024 if (startswith (*l, "elf64-x86-64")
99ad8390
NC
13025 || strcmp (*l, "coff-x86-64") == 0
13026 || strcmp (*l, "pe-x86-64") == 0
d382c579
TG
13027 || strcmp (*l, "pei-x86-64") == 0
13028 || strcmp (*l, "mach-o-x86-64") == 0)
6e0b89ee
AM
13029 {
13030 default_arch = "x86_64";
13031 break;
13032 }
3e73aa7c 13033 if (*l == NULL)
2b5d6a91 13034 as_fatal (_("no compiled in support for x86_64"));
3e73aa7c
JH
13035 free (list);
13036 }
13037 break;
13038#endif
252b5132 13039
351f65ca 13040#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 13041 case OPTION_X32:
351f65ca
L
13042 if (IS_ELF)
13043 {
13044 const char **list, **l;
13045
13046 list = bfd_target_list ();
13047 for (l = list; *l != NULL; l++)
08dedd66 13048 if (startswith (*l, "elf32-x86-64"))
351f65ca
L
13049 {
13050 default_arch = "x86_64:32";
13051 break;
13052 }
13053 if (*l == NULL)
2b5d6a91 13054 as_fatal (_("no compiled in support for 32bit x86_64"));
351f65ca
L
13055 free (list);
13056 }
13057 else
13058 as_fatal (_("32bit x86_64 is only supported for ELF"));
13059 break;
13060#endif
13061
6e0b89ee
AM
13062 case OPTION_32:
13063 default_arch = "i386";
13064 break;
13065
b3b91714
AM
13066 case OPTION_DIVIDE:
13067#ifdef SVR4_COMMENT_CHARS
13068 {
13069 char *n, *t;
13070 const char *s;
13071
add39d23 13072 n = XNEWVEC (char, strlen (i386_comment_chars) + 1);
b3b91714
AM
13073 t = n;
13074 for (s = i386_comment_chars; *s != '\0'; s++)
13075 if (*s != '/')
13076 *t++ = *s;
13077 *t = '\0';
13078 i386_comment_chars = n;
13079 }
13080#endif
13081 break;
13082
9103f4f4 13083 case OPTION_MARCH:
293f5f65
L
13084 saved = xstrdup (arg);
13085 arch = saved;
13086 /* Allow -march=+nosse. */
13087 if (*arch == '+')
13088 arch++;
6305a203 13089 do
9103f4f4 13090 {
6305a203 13091 if (*arch == '.')
2b5d6a91 13092 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
13093 next = strchr (arch, '+');
13094 if (next)
13095 *next++ = '\0';
91d6fa6a 13096 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 13097 {
91d6fa6a 13098 if (strcmp (arch, cpu_arch [j].name) == 0)
ccc9c027 13099 {
6305a203 13100 /* Processor. */
1ded5609
JB
13101 if (! cpu_arch[j].flags.bitfield.cpui386)
13102 continue;
13103
91d6fa6a 13104 cpu_arch_name = cpu_arch[j].name;
6305a203 13105 cpu_sub_arch_name = NULL;
91d6fa6a
NC
13106 cpu_arch_flags = cpu_arch[j].flags;
13107 cpu_arch_isa = cpu_arch[j].type;
13108 cpu_arch_isa_flags = cpu_arch[j].flags;
6305a203
L
13109 if (!cpu_arch_tune_set)
13110 {
13111 cpu_arch_tune = cpu_arch_isa;
13112 cpu_arch_tune_flags = cpu_arch_isa_flags;
13113 }
13114 break;
13115 }
91d6fa6a
NC
13116 else if (*cpu_arch [j].name == '.'
13117 && strcmp (arch, cpu_arch [j].name + 1) == 0)
6305a203 13118 {
33eaf5de 13119 /* ISA extension. */
6305a203 13120 i386_cpu_flags flags;
309d3373 13121
293f5f65
L
13122 flags = cpu_flags_or (cpu_arch_flags,
13123 cpu_arch[j].flags);
81486035 13124
5b64d091 13125 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
6305a203
L
13126 {
13127 if (cpu_sub_arch_name)
13128 {
13129 char *name = cpu_sub_arch_name;
13130 cpu_sub_arch_name = concat (name,
91d6fa6a 13131 cpu_arch[j].name,
1bf57e9f 13132 (const char *) NULL);
6305a203
L
13133 free (name);
13134 }
13135 else
91d6fa6a 13136 cpu_sub_arch_name = xstrdup (cpu_arch[j].name);
6305a203 13137 cpu_arch_flags = flags;
a586129e 13138 cpu_arch_isa_flags = flags;
6305a203 13139 }
0089dace
L
13140 else
13141 cpu_arch_isa_flags
13142 = cpu_flags_or (cpu_arch_isa_flags,
13143 cpu_arch[j].flags);
6305a203 13144 break;
ccc9c027 13145 }
9103f4f4 13146 }
6305a203 13147
293f5f65
L
13148 if (j >= ARRAY_SIZE (cpu_arch))
13149 {
33eaf5de 13150 /* Disable an ISA extension. */
293f5f65
L
13151 for (j = 0; j < ARRAY_SIZE (cpu_noarch); j++)
13152 if (strcmp (arch, cpu_noarch [j].name) == 0)
13153 {
13154 i386_cpu_flags flags;
13155
13156 flags = cpu_flags_and_not (cpu_arch_flags,
13157 cpu_noarch[j].flags);
13158 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
13159 {
13160 if (cpu_sub_arch_name)
13161 {
13162 char *name = cpu_sub_arch_name;
13163 cpu_sub_arch_name = concat (arch,
13164 (const char *) NULL);
13165 free (name);
13166 }
13167 else
13168 cpu_sub_arch_name = xstrdup (arch);
13169 cpu_arch_flags = flags;
13170 cpu_arch_isa_flags = flags;
13171 }
13172 break;
13173 }
13174
13175 if (j >= ARRAY_SIZE (cpu_noarch))
13176 j = ARRAY_SIZE (cpu_arch);
13177 }
13178
91d6fa6a 13179 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 13180 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
13181
13182 arch = next;
9103f4f4 13183 }
293f5f65
L
13184 while (next != NULL);
13185 free (saved);
9103f4f4
L
13186 break;
13187
13188 case OPTION_MTUNE:
13189 if (*arg == '.')
2b5d6a91 13190 as_fatal (_("invalid -mtune= option: `%s'"), arg);
91d6fa6a 13191 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 13192 {
91d6fa6a 13193 if (strcmp (arg, cpu_arch [j].name) == 0)
9103f4f4 13194 {
ccc9c027 13195 cpu_arch_tune_set = 1;
91d6fa6a
NC
13196 cpu_arch_tune = cpu_arch [j].type;
13197 cpu_arch_tune_flags = cpu_arch[j].flags;
9103f4f4
L
13198 break;
13199 }
13200 }
91d6fa6a 13201 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 13202 as_fatal (_("invalid -mtune= option: `%s'"), arg);
9103f4f4
L
13203 break;
13204
1efbbeb4
L
13205 case OPTION_MMNEMONIC:
13206 if (strcasecmp (arg, "att") == 0)
13207 intel_mnemonic = 0;
13208 else if (strcasecmp (arg, "intel") == 0)
13209 intel_mnemonic = 1;
13210 else
2b5d6a91 13211 as_fatal (_("invalid -mmnemonic= option: `%s'"), arg);
1efbbeb4
L
13212 break;
13213
13214 case OPTION_MSYNTAX:
13215 if (strcasecmp (arg, "att") == 0)
13216 intel_syntax = 0;
13217 else if (strcasecmp (arg, "intel") == 0)
13218 intel_syntax = 1;
13219 else
2b5d6a91 13220 as_fatal (_("invalid -msyntax= option: `%s'"), arg);
1efbbeb4
L
13221 break;
13222
13223 case OPTION_MINDEX_REG:
13224 allow_index_reg = 1;
13225 break;
13226
13227 case OPTION_MNAKED_REG:
13228 allow_naked_reg = 1;
13229 break;
13230
c0f3af97
L
13231 case OPTION_MSSE2AVX:
13232 sse2avx = 1;
13233 break;
13234
daf50ae7
L
13235 case OPTION_MSSE_CHECK:
13236 if (strcasecmp (arg, "error") == 0)
7bab8ab5 13237 sse_check = check_error;
daf50ae7 13238 else if (strcasecmp (arg, "warning") == 0)
7bab8ab5 13239 sse_check = check_warning;
daf50ae7 13240 else if (strcasecmp (arg, "none") == 0)
7bab8ab5 13241 sse_check = check_none;
daf50ae7 13242 else
2b5d6a91 13243 as_fatal (_("invalid -msse-check= option: `%s'"), arg);
daf50ae7
L
13244 break;
13245
7bab8ab5
JB
13246 case OPTION_MOPERAND_CHECK:
13247 if (strcasecmp (arg, "error") == 0)
13248 operand_check = check_error;
13249 else if (strcasecmp (arg, "warning") == 0)
13250 operand_check = check_warning;
13251 else if (strcasecmp (arg, "none") == 0)
13252 operand_check = check_none;
13253 else
13254 as_fatal (_("invalid -moperand-check= option: `%s'"), arg);
13255 break;
13256
539f890d
L
13257 case OPTION_MAVXSCALAR:
13258 if (strcasecmp (arg, "128") == 0)
13259 avxscalar = vex128;
13260 else if (strcasecmp (arg, "256") == 0)
13261 avxscalar = vex256;
13262 else
2b5d6a91 13263 as_fatal (_("invalid -mavxscalar= option: `%s'"), arg);
539f890d
L
13264 break;
13265
03751133
L
13266 case OPTION_MVEXWIG:
13267 if (strcmp (arg, "0") == 0)
40c9c8de 13268 vexwig = vexw0;
03751133 13269 else if (strcmp (arg, "1") == 0)
40c9c8de 13270 vexwig = vexw1;
03751133
L
13271 else
13272 as_fatal (_("invalid -mvexwig= option: `%s'"), arg);
13273 break;
13274
7e8b059b
L
13275 case OPTION_MADD_BND_PREFIX:
13276 add_bnd_prefix = 1;
13277 break;
13278
43234a1e
L
13279 case OPTION_MEVEXLIG:
13280 if (strcmp (arg, "128") == 0)
13281 evexlig = evexl128;
13282 else if (strcmp (arg, "256") == 0)
13283 evexlig = evexl256;
13284 else if (strcmp (arg, "512") == 0)
13285 evexlig = evexl512;
13286 else
13287 as_fatal (_("invalid -mevexlig= option: `%s'"), arg);
13288 break;
13289
d3d3c6db
IT
13290 case OPTION_MEVEXRCIG:
13291 if (strcmp (arg, "rne") == 0)
13292 evexrcig = rne;
13293 else if (strcmp (arg, "rd") == 0)
13294 evexrcig = rd;
13295 else if (strcmp (arg, "ru") == 0)
13296 evexrcig = ru;
13297 else if (strcmp (arg, "rz") == 0)
13298 evexrcig = rz;
13299 else
13300 as_fatal (_("invalid -mevexrcig= option: `%s'"), arg);
13301 break;
13302
43234a1e
L
13303 case OPTION_MEVEXWIG:
13304 if (strcmp (arg, "0") == 0)
13305 evexwig = evexw0;
13306 else if (strcmp (arg, "1") == 0)
13307 evexwig = evexw1;
13308 else
13309 as_fatal (_("invalid -mevexwig= option: `%s'"), arg);
13310 break;
13311
167ad85b
TG
13312# if defined (TE_PE) || defined (TE_PEP)
13313 case OPTION_MBIG_OBJ:
13314 use_big_obj = 1;
13315 break;
13316#endif
13317
d1982f93 13318 case OPTION_MOMIT_LOCK_PREFIX:
d022bddd
IT
13319 if (strcasecmp (arg, "yes") == 0)
13320 omit_lock_prefix = 1;
13321 else if (strcasecmp (arg, "no") == 0)
13322 omit_lock_prefix = 0;
13323 else
13324 as_fatal (_("invalid -momit-lock-prefix= option: `%s'"), arg);
13325 break;
13326
e4e00185
AS
13327 case OPTION_MFENCE_AS_LOCK_ADD:
13328 if (strcasecmp (arg, "yes") == 0)
13329 avoid_fence = 1;
13330 else if (strcasecmp (arg, "no") == 0)
13331 avoid_fence = 0;
13332 else
13333 as_fatal (_("invalid -mfence-as-lock-add= option: `%s'"), arg);
13334 break;
13335
ae531041
L
13336 case OPTION_MLFENCE_AFTER_LOAD:
13337 if (strcasecmp (arg, "yes") == 0)
13338 lfence_after_load = 1;
13339 else if (strcasecmp (arg, "no") == 0)
13340 lfence_after_load = 0;
13341 else
13342 as_fatal (_("invalid -mlfence-after-load= option: `%s'"), arg);
13343 break;
13344
13345 case OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH:
13346 if (strcasecmp (arg, "all") == 0)
a09f656b 13347 {
13348 lfence_before_indirect_branch = lfence_branch_all;
13349 if (lfence_before_ret == lfence_before_ret_none)
13350 lfence_before_ret = lfence_before_ret_shl;
13351 }
ae531041
L
13352 else if (strcasecmp (arg, "memory") == 0)
13353 lfence_before_indirect_branch = lfence_branch_memory;
13354 else if (strcasecmp (arg, "register") == 0)
13355 lfence_before_indirect_branch = lfence_branch_register;
13356 else if (strcasecmp (arg, "none") == 0)
13357 lfence_before_indirect_branch = lfence_branch_none;
13358 else
13359 as_fatal (_("invalid -mlfence-before-indirect-branch= option: `%s'"),
13360 arg);
13361 break;
13362
13363 case OPTION_MLFENCE_BEFORE_RET:
13364 if (strcasecmp (arg, "or") == 0)
13365 lfence_before_ret = lfence_before_ret_or;
13366 else if (strcasecmp (arg, "not") == 0)
13367 lfence_before_ret = lfence_before_ret_not;
a09f656b 13368 else if (strcasecmp (arg, "shl") == 0 || strcasecmp (arg, "yes") == 0)
13369 lfence_before_ret = lfence_before_ret_shl;
ae531041
L
13370 else if (strcasecmp (arg, "none") == 0)
13371 lfence_before_ret = lfence_before_ret_none;
13372 else
13373 as_fatal (_("invalid -mlfence-before-ret= option: `%s'"),
13374 arg);
13375 break;
13376
0cb4071e
L
13377 case OPTION_MRELAX_RELOCATIONS:
13378 if (strcasecmp (arg, "yes") == 0)
13379 generate_relax_relocations = 1;
13380 else if (strcasecmp (arg, "no") == 0)
13381 generate_relax_relocations = 0;
13382 else
13383 as_fatal (_("invalid -mrelax-relocations= option: `%s'"), arg);
13384 break;
13385
e379e5f3
L
13386 case OPTION_MALIGN_BRANCH_BOUNDARY:
13387 {
13388 char *end;
13389 long int align = strtoul (arg, &end, 0);
13390 if (*end == '\0')
13391 {
13392 if (align == 0)
13393 {
13394 align_branch_power = 0;
13395 break;
13396 }
13397 else if (align >= 16)
13398 {
13399 int align_power;
13400 for (align_power = 0;
13401 (align & 1) == 0;
13402 align >>= 1, align_power++)
13403 continue;
13404 /* Limit alignment power to 31. */
13405 if (align == 1 && align_power < 32)
13406 {
13407 align_branch_power = align_power;
13408 break;
13409 }
13410 }
13411 }
13412 as_fatal (_("invalid -malign-branch-boundary= value: %s"), arg);
13413 }
13414 break;
13415
13416 case OPTION_MALIGN_BRANCH_PREFIX_SIZE:
13417 {
13418 char *end;
13419 int align = strtoul (arg, &end, 0);
13420 /* Some processors only support 5 prefixes. */
13421 if (*end == '\0' && align >= 0 && align < 6)
13422 {
13423 align_branch_prefix_size = align;
13424 break;
13425 }
13426 as_fatal (_("invalid -malign-branch-prefix-size= value: %s"),
13427 arg);
13428 }
13429 break;
13430
13431 case OPTION_MALIGN_BRANCH:
13432 align_branch = 0;
13433 saved = xstrdup (arg);
13434 type = saved;
13435 do
13436 {
13437 next = strchr (type, '+');
13438 if (next)
13439 *next++ = '\0';
13440 if (strcasecmp (type, "jcc") == 0)
13441 align_branch |= align_branch_jcc_bit;
13442 else if (strcasecmp (type, "fused") == 0)
13443 align_branch |= align_branch_fused_bit;
13444 else if (strcasecmp (type, "jmp") == 0)
13445 align_branch |= align_branch_jmp_bit;
13446 else if (strcasecmp (type, "call") == 0)
13447 align_branch |= align_branch_call_bit;
13448 else if (strcasecmp (type, "ret") == 0)
13449 align_branch |= align_branch_ret_bit;
13450 else if (strcasecmp (type, "indirect") == 0)
13451 align_branch |= align_branch_indirect_bit;
13452 else
13453 as_fatal (_("invalid -malign-branch= option: `%s'"), arg);
13454 type = next;
13455 }
13456 while (next != NULL);
13457 free (saved);
13458 break;
13459
76cf450b
L
13460 case OPTION_MBRANCHES_WITH_32B_BOUNDARIES:
13461 align_branch_power = 5;
13462 align_branch_prefix_size = 5;
13463 align_branch = (align_branch_jcc_bit
13464 | align_branch_fused_bit
13465 | align_branch_jmp_bit);
13466 break;
13467
5db04b09 13468 case OPTION_MAMD64:
4b5aaf5f 13469 isa64 = amd64;
5db04b09
L
13470 break;
13471
13472 case OPTION_MINTEL64:
4b5aaf5f 13473 isa64 = intel64;
5db04b09
L
13474 break;
13475
b6f8c7c4
L
13476 case 'O':
13477 if (arg == NULL)
13478 {
13479 optimize = 1;
13480 /* Turn off -Os. */
13481 optimize_for_space = 0;
13482 }
13483 else if (*arg == 's')
13484 {
13485 optimize_for_space = 1;
13486 /* Turn on all encoding optimizations. */
41fd2579 13487 optimize = INT_MAX;
b6f8c7c4
L
13488 }
13489 else
13490 {
13491 optimize = atoi (arg);
13492 /* Turn off -Os. */
13493 optimize_for_space = 0;
13494 }
13495 break;
13496
252b5132
RH
13497 default:
13498 return 0;
13499 }
13500 return 1;
13501}
13502
8a2c8fef
L
13503#define MESSAGE_TEMPLATE \
13504" "
13505
293f5f65
L
13506static char *
13507output_message (FILE *stream, char *p, char *message, char *start,
13508 int *left_p, const char *name, int len)
13509{
13510 int size = sizeof (MESSAGE_TEMPLATE);
13511 int left = *left_p;
13512
13513 /* Reserve 2 spaces for ", " or ",\0" */
13514 left -= len + 2;
13515
13516 /* Check if there is any room. */
13517 if (left >= 0)
13518 {
13519 if (p != start)
13520 {
13521 *p++ = ',';
13522 *p++ = ' ';
13523 }
13524 p = mempcpy (p, name, len);
13525 }
13526 else
13527 {
13528 /* Output the current message now and start a new one. */
13529 *p++ = ',';
13530 *p = '\0';
13531 fprintf (stream, "%s\n", message);
13532 p = start;
13533 left = size - (start - message) - len - 2;
13534
13535 gas_assert (left >= 0);
13536
13537 p = mempcpy (p, name, len);
13538 }
13539
13540 *left_p = left;
13541 return p;
13542}
13543
8a2c8fef 13544static void
1ded5609 13545show_arch (FILE *stream, int ext, int check)
8a2c8fef
L
13546{
13547 static char message[] = MESSAGE_TEMPLATE;
13548 char *start = message + 27;
13549 char *p;
13550 int size = sizeof (MESSAGE_TEMPLATE);
13551 int left;
13552 const char *name;
13553 int len;
13554 unsigned int j;
13555
13556 p = start;
13557 left = size - (start - message);
13558 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13559 {
13560 /* Should it be skipped? */
13561 if (cpu_arch [j].skip)
13562 continue;
13563
13564 name = cpu_arch [j].name;
13565 len = cpu_arch [j].len;
13566 if (*name == '.')
13567 {
13568 /* It is an extension. Skip if we aren't asked to show it. */
13569 if (ext)
13570 {
13571 name++;
13572 len--;
13573 }
13574 else
13575 continue;
13576 }
13577 else if (ext)
13578 {
13579 /* It is an processor. Skip if we show only extension. */
13580 continue;
13581 }
1ded5609
JB
13582 else if (check && ! cpu_arch[j].flags.bitfield.cpui386)
13583 {
13584 /* It is an impossible processor - skip. */
13585 continue;
13586 }
8a2c8fef 13587
293f5f65 13588 p = output_message (stream, p, message, start, &left, name, len);
8a2c8fef
L
13589 }
13590
293f5f65
L
13591 /* Display disabled extensions. */
13592 if (ext)
13593 for (j = 0; j < ARRAY_SIZE (cpu_noarch); j++)
13594 {
13595 name = cpu_noarch [j].name;
13596 len = cpu_noarch [j].len;
13597 p = output_message (stream, p, message, start, &left, name,
13598 len);
13599 }
13600
8a2c8fef
L
13601 *p = '\0';
13602 fprintf (stream, "%s\n", message);
13603}
13604
252b5132 13605void
8a2c8fef 13606md_show_usage (FILE *stream)
252b5132 13607{
4cc782b5
ILT
13608#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13609 fprintf (stream, _("\
d4693039 13610 -Qy, -Qn ignored\n\
a38cf1db 13611 -V print assembler version number\n\
b3b91714
AM
13612 -k ignored\n"));
13613#endif
13614 fprintf (stream, _("\
12b55ccc 13615 -n Do not optimize code alignment\n\
b3b91714
AM
13616 -q quieten some warnings\n"));
13617#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13618 fprintf (stream, _("\
a38cf1db 13619 -s ignored\n"));
b3b91714 13620#endif
d7f449c0
L
13621#if defined BFD64 && (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
13622 || defined (TE_PE) || defined (TE_PEP))
751d281c 13623 fprintf (stream, _("\
570561f7 13624 --32/--64/--x32 generate 32bit/64bit/x32 code\n"));
751d281c 13625#endif
b3b91714
AM
13626#ifdef SVR4_COMMENT_CHARS
13627 fprintf (stream, _("\
13628 --divide do not treat `/' as a comment character\n"));
a38cf1db
AM
13629#else
13630 fprintf (stream, _("\
b3b91714 13631 --divide ignored\n"));
4cc782b5 13632#endif
9103f4f4 13633 fprintf (stream, _("\
6305a203 13634 -march=CPU[,+EXTENSION...]\n\
8a2c8fef 13635 generate code for CPU and EXTENSION, CPU is one of:\n"));
1ded5609 13636 show_arch (stream, 0, 1);
8a2c8fef
L
13637 fprintf (stream, _("\
13638 EXTENSION is combination of:\n"));
1ded5609 13639 show_arch (stream, 1, 0);
6305a203 13640 fprintf (stream, _("\
8a2c8fef 13641 -mtune=CPU optimize for CPU, CPU is one of:\n"));
1ded5609 13642 show_arch (stream, 0, 0);
ba104c83 13643 fprintf (stream, _("\
c0f3af97
L
13644 -msse2avx encode SSE instructions with VEX prefix\n"));
13645 fprintf (stream, _("\
7c5c05ef 13646 -msse-check=[none|error|warning] (default: warning)\n\
daf50ae7
L
13647 check SSE instructions\n"));
13648 fprintf (stream, _("\
7c5c05ef 13649 -moperand-check=[none|error|warning] (default: warning)\n\
7bab8ab5
JB
13650 check operand combinations for validity\n"));
13651 fprintf (stream, _("\
7c5c05ef
L
13652 -mavxscalar=[128|256] (default: 128)\n\
13653 encode scalar AVX instructions with specific vector\n\
539f890d
L
13654 length\n"));
13655 fprintf (stream, _("\
03751133
L
13656 -mvexwig=[0|1] (default: 0)\n\
13657 encode VEX instructions with specific VEX.W value\n\
13658 for VEX.W bit ignored instructions\n"));
13659 fprintf (stream, _("\
7c5c05ef
L
13660 -mevexlig=[128|256|512] (default: 128)\n\
13661 encode scalar EVEX instructions with specific vector\n\
43234a1e
L
13662 length\n"));
13663 fprintf (stream, _("\
7c5c05ef
L
13664 -mevexwig=[0|1] (default: 0)\n\
13665 encode EVEX instructions with specific EVEX.W value\n\
43234a1e
L
13666 for EVEX.W bit ignored instructions\n"));
13667 fprintf (stream, _("\
7c5c05ef 13668 -mevexrcig=[rne|rd|ru|rz] (default: rne)\n\
d3d3c6db
IT
13669 encode EVEX instructions with specific EVEX.RC value\n\
13670 for SAE-only ignored instructions\n"));
13671 fprintf (stream, _("\
7c5c05ef
L
13672 -mmnemonic=[att|intel] "));
13673 if (SYSV386_COMPAT)
13674 fprintf (stream, _("(default: att)\n"));
13675 else
13676 fprintf (stream, _("(default: intel)\n"));
13677 fprintf (stream, _("\
13678 use AT&T/Intel mnemonic\n"));
ba104c83 13679 fprintf (stream, _("\
7c5c05ef
L
13680 -msyntax=[att|intel] (default: att)\n\
13681 use AT&T/Intel syntax\n"));
ba104c83
L
13682 fprintf (stream, _("\
13683 -mindex-reg support pseudo index registers\n"));
13684 fprintf (stream, _("\
13685 -mnaked-reg don't require `%%' prefix for registers\n"));
13686 fprintf (stream, _("\
7e8b059b 13687 -madd-bnd-prefix add BND prefix for all valid branches\n"));
b4a3a7b4 13688#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8dcea932
L
13689 fprintf (stream, _("\
13690 -mshared disable branch optimization for shared code\n"));
b4a3a7b4
L
13691 fprintf (stream, _("\
13692 -mx86-used-note=[no|yes] "));
13693 if (DEFAULT_X86_USED_NOTE)
13694 fprintf (stream, _("(default: yes)\n"));
13695 else
13696 fprintf (stream, _("(default: no)\n"));
13697 fprintf (stream, _("\
13698 generate x86 used ISA and feature properties\n"));
13699#endif
13700#if defined (TE_PE) || defined (TE_PEP)
167ad85b
TG
13701 fprintf (stream, _("\
13702 -mbig-obj generate big object files\n"));
13703#endif
d022bddd 13704 fprintf (stream, _("\
7c5c05ef 13705 -momit-lock-prefix=[no|yes] (default: no)\n\
d022bddd 13706 strip all lock prefixes\n"));
5db04b09 13707 fprintf (stream, _("\
7c5c05ef 13708 -mfence-as-lock-add=[no|yes] (default: no)\n\
e4e00185
AS
13709 encode lfence, mfence and sfence as\n\
13710 lock addl $0x0, (%%{re}sp)\n"));
13711 fprintf (stream, _("\
7c5c05ef
L
13712 -mrelax-relocations=[no|yes] "));
13713 if (DEFAULT_GENERATE_X86_RELAX_RELOCATIONS)
13714 fprintf (stream, _("(default: yes)\n"));
13715 else
13716 fprintf (stream, _("(default: no)\n"));
13717 fprintf (stream, _("\
0cb4071e
L
13718 generate relax relocations\n"));
13719 fprintf (stream, _("\
e379e5f3
L
13720 -malign-branch-boundary=NUM (default: 0)\n\
13721 align branches within NUM byte boundary\n"));
13722 fprintf (stream, _("\
13723 -malign-branch=TYPE[+TYPE...] (default: jcc+fused+jmp)\n\
13724 TYPE is combination of jcc, fused, jmp, call, ret,\n\
13725 indirect\n\
13726 specify types of branches to align\n"));
13727 fprintf (stream, _("\
13728 -malign-branch-prefix-size=NUM (default: 5)\n\
13729 align branches with NUM prefixes per instruction\n"));
13730 fprintf (stream, _("\
76cf450b
L
13731 -mbranches-within-32B-boundaries\n\
13732 align branches within 32 byte boundary\n"));
13733 fprintf (stream, _("\
ae531041
L
13734 -mlfence-after-load=[no|yes] (default: no)\n\
13735 generate lfence after load\n"));
13736 fprintf (stream, _("\
13737 -mlfence-before-indirect-branch=[none|all|register|memory] (default: none)\n\
13738 generate lfence before indirect near branch\n"));
13739 fprintf (stream, _("\
a09f656b 13740 -mlfence-before-ret=[none|or|not|shl|yes] (default: none)\n\
ae531041
L
13741 generate lfence before ret\n"));
13742 fprintf (stream, _("\
7c5c05ef 13743 -mamd64 accept only AMD64 ISA [default]\n"));
5db04b09
L
13744 fprintf (stream, _("\
13745 -mintel64 accept only Intel64 ISA\n"));
252b5132
RH
13746}
13747
3e73aa7c 13748#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
321098a5 13749 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
e57f8c65 13750 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
252b5132
RH
13751
13752/* Pick the target format to use. */
13753
47926f60 13754const char *
e3bb37b5 13755i386_target_format (void)
252b5132 13756{
351f65ca
L
13757 if (!strncmp (default_arch, "x86_64", 6))
13758 {
13759 update_code_flag (CODE_64BIT, 1);
13760 if (default_arch[6] == '\0')
7f56bc95 13761 x86_elf_abi = X86_64_ABI;
351f65ca 13762 else
7f56bc95 13763 x86_elf_abi = X86_64_X32_ABI;
351f65ca 13764 }
3e73aa7c 13765 else if (!strcmp (default_arch, "i386"))
78f12dd3 13766 update_code_flag (CODE_32BIT, 1);
5197d474
L
13767 else if (!strcmp (default_arch, "iamcu"))
13768 {
13769 update_code_flag (CODE_32BIT, 1);
13770 if (cpu_arch_isa == PROCESSOR_UNKNOWN)
13771 {
13772 static const i386_cpu_flags iamcu_flags = CPU_IAMCU_FLAGS;
13773 cpu_arch_name = "iamcu";
13774 cpu_sub_arch_name = NULL;
13775 cpu_arch_flags = iamcu_flags;
13776 cpu_arch_isa = PROCESSOR_IAMCU;
13777 cpu_arch_isa_flags = iamcu_flags;
13778 if (!cpu_arch_tune_set)
13779 {
13780 cpu_arch_tune = cpu_arch_isa;
13781 cpu_arch_tune_flags = cpu_arch_isa_flags;
13782 }
13783 }
8d471ec1 13784 else if (cpu_arch_isa != PROCESSOR_IAMCU)
5197d474
L
13785 as_fatal (_("Intel MCU doesn't support `%s' architecture"),
13786 cpu_arch_name);
13787 }
3e73aa7c 13788 else
2b5d6a91 13789 as_fatal (_("unknown architecture"));
89507696
JB
13790
13791 if (cpu_flags_all_zero (&cpu_arch_isa_flags))
13792 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].flags;
13793 if (cpu_flags_all_zero (&cpu_arch_tune_flags))
13794 cpu_arch_tune_flags = cpu_arch[flag_code == CODE_64BIT].flags;
13795
252b5132
RH
13796 switch (OUTPUT_FLAVOR)
13797 {
9384f2ff 13798#if defined (OBJ_MAYBE_AOUT) || defined (OBJ_AOUT)
4c63da97 13799 case bfd_target_aout_flavour:
47926f60 13800 return AOUT_TARGET_FORMAT;
4c63da97 13801#endif
9384f2ff
AM
13802#if defined (OBJ_MAYBE_COFF) || defined (OBJ_COFF)
13803# if defined (TE_PE) || defined (TE_PEP)
13804 case bfd_target_coff_flavour:
167ad85b
TG
13805 if (flag_code == CODE_64BIT)
13806 return use_big_obj ? "pe-bigobj-x86-64" : "pe-x86-64";
13807 else
251dae91 13808 return use_big_obj ? "pe-bigobj-i386" : "pe-i386";
9384f2ff 13809# elif defined (TE_GO32)
0561d57c
JK
13810 case bfd_target_coff_flavour:
13811 return "coff-go32";
9384f2ff 13812# else
252b5132
RH
13813 case bfd_target_coff_flavour:
13814 return "coff-i386";
9384f2ff 13815# endif
4c63da97 13816#endif
3e73aa7c 13817#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
252b5132 13818 case bfd_target_elf_flavour:
3e73aa7c 13819 {
351f65ca
L
13820 const char *format;
13821
13822 switch (x86_elf_abi)
4fa24527 13823 {
351f65ca
L
13824 default:
13825 format = ELF_TARGET_FORMAT;
e379e5f3
L
13826#ifndef TE_SOLARIS
13827 tls_get_addr = "___tls_get_addr";
13828#endif
351f65ca 13829 break;
7f56bc95 13830 case X86_64_ABI:
351f65ca 13831 use_rela_relocations = 1;
4fa24527 13832 object_64bit = 1;
e379e5f3
L
13833#ifndef TE_SOLARIS
13834 tls_get_addr = "__tls_get_addr";
13835#endif
351f65ca
L
13836 format = ELF_TARGET_FORMAT64;
13837 break;
7f56bc95 13838 case X86_64_X32_ABI:
4fa24527 13839 use_rela_relocations = 1;
351f65ca 13840 object_64bit = 1;
e379e5f3
L
13841#ifndef TE_SOLARIS
13842 tls_get_addr = "__tls_get_addr";
13843#endif
862be3fb 13844 disallow_64bit_reloc = 1;
351f65ca
L
13845 format = ELF_TARGET_FORMAT32;
13846 break;
4fa24527 13847 }
3632d14b 13848 if (cpu_arch_isa == PROCESSOR_L1OM)
8a9036a4 13849 {
7f56bc95 13850 if (x86_elf_abi != X86_64_ABI)
8a9036a4
L
13851 as_fatal (_("Intel L1OM is 64bit only"));
13852 return ELF_TARGET_L1OM_FORMAT;
13853 }
b49f93f6 13854 else if (cpu_arch_isa == PROCESSOR_K1OM)
7a9068fe
L
13855 {
13856 if (x86_elf_abi != X86_64_ABI)
13857 as_fatal (_("Intel K1OM is 64bit only"));
13858 return ELF_TARGET_K1OM_FORMAT;
13859 }
81486035
L
13860 else if (cpu_arch_isa == PROCESSOR_IAMCU)
13861 {
13862 if (x86_elf_abi != I386_ABI)
13863 as_fatal (_("Intel MCU is 32bit only"));
13864 return ELF_TARGET_IAMCU_FORMAT;
13865 }
8a9036a4 13866 else
351f65ca 13867 return format;
3e73aa7c 13868 }
e57f8c65
TG
13869#endif
13870#if defined (OBJ_MACH_O)
13871 case bfd_target_mach_o_flavour:
d382c579
TG
13872 if (flag_code == CODE_64BIT)
13873 {
13874 use_rela_relocations = 1;
13875 object_64bit = 1;
13876 return "mach-o-x86-64";
13877 }
13878 else
13879 return "mach-o-i386";
4c63da97 13880#endif
252b5132
RH
13881 default:
13882 abort ();
13883 return NULL;
13884 }
13885}
13886
47926f60 13887#endif /* OBJ_MAYBE_ more than one */
252b5132 13888\f
252b5132 13889symbolS *
7016a5d5 13890md_undefined_symbol (char *name)
252b5132 13891{
18dc2407
ILT
13892 if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
13893 && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
13894 && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
13895 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
24eab124
AM
13896 {
13897 if (!GOT_symbol)
13898 {
13899 if (symbol_find (name))
13900 as_bad (_("GOT already in symbol table"));
13901 GOT_symbol = symbol_new (name, undefined_section,
e01e1cee 13902 &zero_address_frag, 0);
24eab124
AM
13903 };
13904 return GOT_symbol;
13905 }
252b5132
RH
13906 return 0;
13907}
13908
13909/* Round up a section size to the appropriate boundary. */
47926f60 13910
252b5132 13911valueT
7016a5d5 13912md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
252b5132 13913{
4c63da97
AM
13914#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
13915 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
13916 {
13917 /* For a.out, force the section size to be aligned. If we don't do
13918 this, BFD will align it for us, but it will not write out the
13919 final bytes of the section. This may be a bug in BFD, but it is
13920 easier to fix it here since that is how the other a.out targets
13921 work. */
13922 int align;
13923
fd361982 13924 align = bfd_section_alignment (segment);
8d3842cd 13925 size = ((size + (1 << align) - 1) & (-((valueT) 1 << align)));
4c63da97 13926 }
252b5132
RH
13927#endif
13928
13929 return size;
13930}
13931
13932/* On the i386, PC-relative offsets are relative to the start of the
13933 next instruction. That is, the address of the offset, plus its
13934 size, since the offset is always the last part of the insn. */
13935
13936long
e3bb37b5 13937md_pcrel_from (fixS *fixP)
252b5132
RH
13938{
13939 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
13940}
13941
13942#ifndef I386COFF
13943
13944static void
e3bb37b5 13945s_bss (int ignore ATTRIBUTE_UNUSED)
252b5132 13946{
29b0f896 13947 int temp;
252b5132 13948
8a75718c
JB
13949#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13950 if (IS_ELF)
13951 obj_elf_section_change_hook ();
13952#endif
252b5132
RH
13953 temp = get_absolute_expression ();
13954 subseg_set (bss_section, (subsegT) temp);
13955 demand_empty_rest_of_line ();
13956}
13957
13958#endif
13959
e379e5f3
L
13960/* Remember constant directive. */
13961
13962void
13963i386_cons_align (int ignore ATTRIBUTE_UNUSED)
13964{
13965 if (last_insn.kind != last_insn_directive
13966 && (bfd_section_flags (now_seg) & SEC_CODE))
13967 {
13968 last_insn.seg = now_seg;
13969 last_insn.kind = last_insn_directive;
13970 last_insn.name = "constant directive";
13971 last_insn.file = as_where (&last_insn.line);
ae531041
L
13972 if (lfence_before_ret != lfence_before_ret_none)
13973 {
13974 if (lfence_before_indirect_branch != lfence_branch_none)
13975 as_warn (_("constant directive skips -mlfence-before-ret "
13976 "and -mlfence-before-indirect-branch"));
13977 else
13978 as_warn (_("constant directive skips -mlfence-before-ret"));
13979 }
13980 else if (lfence_before_indirect_branch != lfence_branch_none)
13981 as_warn (_("constant directive skips -mlfence-before-indirect-branch"));
e379e5f3
L
13982 }
13983}
13984
252b5132 13985void
e3bb37b5 13986i386_validate_fix (fixS *fixp)
252b5132 13987{
02a86693 13988 if (fixp->fx_subsy)
252b5132 13989 {
02a86693 13990 if (fixp->fx_subsy == GOT_symbol)
23df1078 13991 {
02a86693
L
13992 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
13993 {
13994 if (!object_64bit)
13995 abort ();
13996#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13997 if (fixp->fx_tcbit2)
56ceb5b5
L
13998 fixp->fx_r_type = (fixp->fx_tcbit
13999 ? BFD_RELOC_X86_64_REX_GOTPCRELX
14000 : BFD_RELOC_X86_64_GOTPCRELX);
02a86693
L
14001 else
14002#endif
14003 fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
14004 }
d6ab8113 14005 else
02a86693
L
14006 {
14007 if (!object_64bit)
14008 fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
14009 else
14010 fixp->fx_r_type = BFD_RELOC_X86_64_GOTOFF64;
14011 }
14012 fixp->fx_subsy = 0;
23df1078 14013 }
252b5132 14014 }
02a86693 14015#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2585b7a5 14016 else
02a86693 14017 {
2585b7a5
L
14018 /* NB: Commit 292676c1 resolved PLT32 reloc aganst local symbol
14019 to section. Since PLT32 relocation must be against symbols,
14020 turn such PLT32 relocation into PC32 relocation. */
14021 if (fixp->fx_addsy
14022 && (fixp->fx_r_type == BFD_RELOC_386_PLT32
14023 || fixp->fx_r_type == BFD_RELOC_X86_64_PLT32)
14024 && symbol_section_p (fixp->fx_addsy))
14025 fixp->fx_r_type = BFD_RELOC_32_PCREL;
14026 if (!object_64bit)
14027 {
14028 if (fixp->fx_r_type == BFD_RELOC_386_GOT32
14029 && fixp->fx_tcbit2)
14030 fixp->fx_r_type = BFD_RELOC_386_GOT32X;
14031 }
02a86693
L
14032 }
14033#endif
252b5132
RH
14034}
14035
252b5132 14036arelent *
7016a5d5 14037tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
14038{
14039 arelent *rel;
14040 bfd_reloc_code_real_type code;
14041
14042 switch (fixp->fx_r_type)
14043 {
8ce3d284 14044#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
14045 case BFD_RELOC_SIZE32:
14046 case BFD_RELOC_SIZE64:
14047 if (S_IS_DEFINED (fixp->fx_addsy)
14048 && !S_IS_EXTERNAL (fixp->fx_addsy))
14049 {
14050 /* Resolve size relocation against local symbol to size of
14051 the symbol plus addend. */
14052 valueT value = S_GET_SIZE (fixp->fx_addsy) + fixp->fx_offset;
14053 if (fixp->fx_r_type == BFD_RELOC_SIZE32
14054 && !fits_in_unsigned_long (value))
14055 as_bad_where (fixp->fx_file, fixp->fx_line,
14056 _("symbol size computation overflow"));
14057 fixp->fx_addsy = NULL;
14058 fixp->fx_subsy = NULL;
14059 md_apply_fix (fixp, (valueT *) &value, NULL);
14060 return NULL;
14061 }
8ce3d284 14062#endif
1a0670f3 14063 /* Fall through. */
8fd4256d 14064
3e73aa7c
JH
14065 case BFD_RELOC_X86_64_PLT32:
14066 case BFD_RELOC_X86_64_GOT32:
14067 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
14068 case BFD_RELOC_X86_64_GOTPCRELX:
14069 case BFD_RELOC_X86_64_REX_GOTPCRELX:
252b5132
RH
14070 case BFD_RELOC_386_PLT32:
14071 case BFD_RELOC_386_GOT32:
02a86693 14072 case BFD_RELOC_386_GOT32X:
252b5132
RH
14073 case BFD_RELOC_386_GOTOFF:
14074 case BFD_RELOC_386_GOTPC:
13ae64f3
JJ
14075 case BFD_RELOC_386_TLS_GD:
14076 case BFD_RELOC_386_TLS_LDM:
14077 case BFD_RELOC_386_TLS_LDO_32:
14078 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
14079 case BFD_RELOC_386_TLS_IE:
14080 case BFD_RELOC_386_TLS_GOTIE:
13ae64f3
JJ
14081 case BFD_RELOC_386_TLS_LE_32:
14082 case BFD_RELOC_386_TLS_LE:
67a4f2b7
AO
14083 case BFD_RELOC_386_TLS_GOTDESC:
14084 case BFD_RELOC_386_TLS_DESC_CALL:
bffbf940
JJ
14085 case BFD_RELOC_X86_64_TLSGD:
14086 case BFD_RELOC_X86_64_TLSLD:
14087 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 14088 case BFD_RELOC_X86_64_DTPOFF64:
bffbf940
JJ
14089 case BFD_RELOC_X86_64_GOTTPOFF:
14090 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113
JB
14091 case BFD_RELOC_X86_64_TPOFF64:
14092 case BFD_RELOC_X86_64_GOTOFF64:
14093 case BFD_RELOC_X86_64_GOTPC32:
7b81dfbb
AJ
14094 case BFD_RELOC_X86_64_GOT64:
14095 case BFD_RELOC_X86_64_GOTPCREL64:
14096 case BFD_RELOC_X86_64_GOTPC64:
14097 case BFD_RELOC_X86_64_GOTPLT64:
14098 case BFD_RELOC_X86_64_PLTOFF64:
67a4f2b7
AO
14099 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14100 case BFD_RELOC_X86_64_TLSDESC_CALL:
252b5132
RH
14101 case BFD_RELOC_RVA:
14102 case BFD_RELOC_VTABLE_ENTRY:
14103 case BFD_RELOC_VTABLE_INHERIT:
6482c264
NC
14104#ifdef TE_PE
14105 case BFD_RELOC_32_SECREL:
14106#endif
252b5132
RH
14107 code = fixp->fx_r_type;
14108 break;
dbbaec26
L
14109 case BFD_RELOC_X86_64_32S:
14110 if (!fixp->fx_pcrel)
14111 {
14112 /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32. */
14113 code = fixp->fx_r_type;
14114 break;
14115 }
1a0670f3 14116 /* Fall through. */
252b5132 14117 default:
93382f6d 14118 if (fixp->fx_pcrel)
252b5132 14119 {
93382f6d
AM
14120 switch (fixp->fx_size)
14121 {
14122 default:
b091f402
AM
14123 as_bad_where (fixp->fx_file, fixp->fx_line,
14124 _("can not do %d byte pc-relative relocation"),
14125 fixp->fx_size);
93382f6d
AM
14126 code = BFD_RELOC_32_PCREL;
14127 break;
14128 case 1: code = BFD_RELOC_8_PCREL; break;
14129 case 2: code = BFD_RELOC_16_PCREL; break;
d258b828 14130 case 4: code = BFD_RELOC_32_PCREL; break;
d6ab8113
JB
14131#ifdef BFD64
14132 case 8: code = BFD_RELOC_64_PCREL; break;
14133#endif
93382f6d
AM
14134 }
14135 }
14136 else
14137 {
14138 switch (fixp->fx_size)
14139 {
14140 default:
b091f402
AM
14141 as_bad_where (fixp->fx_file, fixp->fx_line,
14142 _("can not do %d byte relocation"),
14143 fixp->fx_size);
93382f6d
AM
14144 code = BFD_RELOC_32;
14145 break;
14146 case 1: code = BFD_RELOC_8; break;
14147 case 2: code = BFD_RELOC_16; break;
14148 case 4: code = BFD_RELOC_32; break;
937149dd 14149#ifdef BFD64
3e73aa7c 14150 case 8: code = BFD_RELOC_64; break;
937149dd 14151#endif
93382f6d 14152 }
252b5132
RH
14153 }
14154 break;
14155 }
252b5132 14156
d182319b
JB
14157 if ((code == BFD_RELOC_32
14158 || code == BFD_RELOC_32_PCREL
14159 || code == BFD_RELOC_X86_64_32S)
252b5132
RH
14160 && GOT_symbol
14161 && fixp->fx_addsy == GOT_symbol)
3e73aa7c 14162 {
4fa24527 14163 if (!object_64bit)
d6ab8113
JB
14164 code = BFD_RELOC_386_GOTPC;
14165 else
14166 code = BFD_RELOC_X86_64_GOTPC32;
3e73aa7c 14167 }
7b81dfbb
AJ
14168 if ((code == BFD_RELOC_64 || code == BFD_RELOC_64_PCREL)
14169 && GOT_symbol
14170 && fixp->fx_addsy == GOT_symbol)
14171 {
14172 code = BFD_RELOC_X86_64_GOTPC64;
14173 }
252b5132 14174
add39d23
TS
14175 rel = XNEW (arelent);
14176 rel->sym_ptr_ptr = XNEW (asymbol *);
49309057 14177 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
14178
14179 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
c87db184 14180
3e73aa7c
JH
14181 if (!use_rela_relocations)
14182 {
14183 /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
14184 vtable entry to be used in the relocation's section offset. */
14185 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
14186 rel->address = fixp->fx_offset;
fbeb56a4
DK
14187#if defined (OBJ_COFF) && defined (TE_PE)
14188 else if (fixp->fx_addsy && S_IS_WEAK (fixp->fx_addsy))
14189 rel->addend = fixp->fx_addnumber - (S_GET_VALUE (fixp->fx_addsy) * 2);
14190 else
14191#endif
c6682705 14192 rel->addend = 0;
3e73aa7c
JH
14193 }
14194 /* Use the rela in 64bit mode. */
252b5132 14195 else
3e73aa7c 14196 {
862be3fb
L
14197 if (disallow_64bit_reloc)
14198 switch (code)
14199 {
862be3fb
L
14200 case BFD_RELOC_X86_64_DTPOFF64:
14201 case BFD_RELOC_X86_64_TPOFF64:
14202 case BFD_RELOC_64_PCREL:
14203 case BFD_RELOC_X86_64_GOTOFF64:
14204 case BFD_RELOC_X86_64_GOT64:
14205 case BFD_RELOC_X86_64_GOTPCREL64:
14206 case BFD_RELOC_X86_64_GOTPC64:
14207 case BFD_RELOC_X86_64_GOTPLT64:
14208 case BFD_RELOC_X86_64_PLTOFF64:
14209 as_bad_where (fixp->fx_file, fixp->fx_line,
14210 _("cannot represent relocation type %s in x32 mode"),
14211 bfd_get_reloc_code_name (code));
14212 break;
14213 default:
14214 break;
14215 }
14216
062cd5e7
AS
14217 if (!fixp->fx_pcrel)
14218 rel->addend = fixp->fx_offset;
14219 else
14220 switch (code)
14221 {
14222 case BFD_RELOC_X86_64_PLT32:
14223 case BFD_RELOC_X86_64_GOT32:
14224 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
14225 case BFD_RELOC_X86_64_GOTPCRELX:
14226 case BFD_RELOC_X86_64_REX_GOTPCRELX:
bffbf940
JJ
14227 case BFD_RELOC_X86_64_TLSGD:
14228 case BFD_RELOC_X86_64_TLSLD:
14229 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7
AO
14230 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14231 case BFD_RELOC_X86_64_TLSDESC_CALL:
062cd5e7
AS
14232 rel->addend = fixp->fx_offset - fixp->fx_size;
14233 break;
14234 default:
14235 rel->addend = (section->vma
14236 - fixp->fx_size
14237 + fixp->fx_addnumber
14238 + md_pcrel_from (fixp));
14239 break;
14240 }
3e73aa7c
JH
14241 }
14242
252b5132
RH
14243 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
14244 if (rel->howto == NULL)
14245 {
14246 as_bad_where (fixp->fx_file, fixp->fx_line,
d0b47220 14247 _("cannot represent relocation type %s"),
252b5132
RH
14248 bfd_get_reloc_code_name (code));
14249 /* Set howto to a garbage value so that we can keep going. */
14250 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
9c2799c2 14251 gas_assert (rel->howto != NULL);
252b5132
RH
14252 }
14253
14254 return rel;
14255}
14256
ee86248c 14257#include "tc-i386-intel.c"
54cfded0 14258
a60de03c
JB
14259void
14260tc_x86_parse_to_dw2regnum (expressionS *exp)
54cfded0 14261{
a60de03c
JB
14262 int saved_naked_reg;
14263 char saved_register_dot;
54cfded0 14264
a60de03c
JB
14265 saved_naked_reg = allow_naked_reg;
14266 allow_naked_reg = 1;
14267 saved_register_dot = register_chars['.'];
14268 register_chars['.'] = '.';
14269 allow_pseudo_reg = 1;
14270 expression_and_evaluate (exp);
14271 allow_pseudo_reg = 0;
14272 register_chars['.'] = saved_register_dot;
14273 allow_naked_reg = saved_naked_reg;
14274
e96d56a1 14275 if (exp->X_op == O_register && exp->X_add_number >= 0)
54cfded0 14276 {
a60de03c
JB
14277 if ((addressT) exp->X_add_number < i386_regtab_size)
14278 {
14279 exp->X_op = O_constant;
14280 exp->X_add_number = i386_regtab[exp->X_add_number]
14281 .dw2_regnum[flag_code >> 1];
14282 }
14283 else
14284 exp->X_op = O_illegal;
54cfded0 14285 }
54cfded0
AM
14286}
14287
14288void
14289tc_x86_frame_initial_instructions (void)
14290{
a60de03c
JB
14291 static unsigned int sp_regno[2];
14292
14293 if (!sp_regno[flag_code >> 1])
14294 {
14295 char *saved_input = input_line_pointer;
14296 char sp[][4] = {"esp", "rsp"};
14297 expressionS exp;
a4447b93 14298
a60de03c
JB
14299 input_line_pointer = sp[flag_code >> 1];
14300 tc_x86_parse_to_dw2regnum (&exp);
9c2799c2 14301 gas_assert (exp.X_op == O_constant);
a60de03c
JB
14302 sp_regno[flag_code >> 1] = exp.X_add_number;
14303 input_line_pointer = saved_input;
14304 }
a4447b93 14305
61ff971f
L
14306 cfi_add_CFA_def_cfa (sp_regno[flag_code >> 1], -x86_cie_data_alignment);
14307 cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
54cfded0 14308}
d2b2c203 14309
d7921315
L
14310int
14311x86_dwarf2_addr_size (void)
14312{
14313#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
14314 if (x86_elf_abi == X86_64_X32_ABI)
14315 return 4;
14316#endif
14317 return bfd_arch_bits_per_address (stdoutput) / 8;
14318}
14319
d2b2c203
DJ
14320int
14321i386_elf_section_type (const char *str, size_t len)
14322{
14323 if (flag_code == CODE_64BIT
14324 && len == sizeof ("unwind") - 1
14325 && strncmp (str, "unwind", 6) == 0)
14326 return SHT_X86_64_UNWIND;
14327
14328 return -1;
14329}
bb41ade5 14330
ad5fec3b
EB
14331#ifdef TE_SOLARIS
14332void
14333i386_solaris_fix_up_eh_frame (segT sec)
14334{
14335 if (flag_code == CODE_64BIT)
14336 elf_section_type (sec) = SHT_X86_64_UNWIND;
14337}
14338#endif
14339
bb41ade5
AM
14340#ifdef TE_PE
14341void
14342tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
14343{
91d6fa6a 14344 expressionS exp;
bb41ade5 14345
91d6fa6a
NC
14346 exp.X_op = O_secrel;
14347 exp.X_add_symbol = symbol;
14348 exp.X_add_number = 0;
14349 emit_expr (&exp, size);
bb41ade5
AM
14350}
14351#endif
3b22753a
L
14352
14353#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14354/* For ELF on x86-64, add support for SHF_X86_64_LARGE. */
14355
01e1a5bc 14356bfd_vma
6d4af3c2 14357x86_64_section_letter (int letter, const char **ptr_msg)
3b22753a
L
14358{
14359 if (flag_code == CODE_64BIT)
14360 {
14361 if (letter == 'l')
14362 return SHF_X86_64_LARGE;
14363
8f3bae45 14364 *ptr_msg = _("bad .section directive: want a,l,w,x,M,S,G,T in string");
64e74474 14365 }
3b22753a 14366 else
8f3bae45 14367 *ptr_msg = _("bad .section directive: want a,w,x,M,S,G,T in string");
3b22753a
L
14368 return -1;
14369}
14370
01e1a5bc 14371bfd_vma
3b22753a
L
14372x86_64_section_word (char *str, size_t len)
14373{
08dedd66 14374 if (len == 5 && flag_code == CODE_64BIT && startswith (str, "large"))
3b22753a
L
14375 return SHF_X86_64_LARGE;
14376
14377 return -1;
14378}
14379
14380static void
14381handle_large_common (int small ATTRIBUTE_UNUSED)
14382{
14383 if (flag_code != CODE_64BIT)
14384 {
14385 s_comm_internal (0, elf_common_parse);
14386 as_warn (_(".largecomm supported only in 64bit mode, producing .comm"));
14387 }
14388 else
14389 {
14390 static segT lbss_section;
14391 asection *saved_com_section_ptr = elf_com_section_ptr;
14392 asection *saved_bss_section = bss_section;
14393
14394 if (lbss_section == NULL)
14395 {
14396 flagword applicable;
14397 segT seg = now_seg;
14398 subsegT subseg = now_subseg;
14399
14400 /* The .lbss section is for local .largecomm symbols. */
14401 lbss_section = subseg_new (".lbss", 0);
14402 applicable = bfd_applicable_section_flags (stdoutput);
fd361982 14403 bfd_set_section_flags (lbss_section, applicable & SEC_ALLOC);
3b22753a
L
14404 seg_info (lbss_section)->bss = 1;
14405
14406 subseg_set (seg, subseg);
14407 }
14408
14409 elf_com_section_ptr = &_bfd_elf_large_com_section;
14410 bss_section = lbss_section;
14411
14412 s_comm_internal (0, elf_common_parse);
14413
14414 elf_com_section_ptr = saved_com_section_ptr;
14415 bss_section = saved_bss_section;
14416 }
14417}
14418#endif /* OBJ_ELF || OBJ_MAYBE_ELF */