]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/config/tc-i386.c
x86: integrate mask_op into struct _i386_insn
[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);
193static const seg_entry *build_modrm_byte (void);
194static void output_insn (void);
195static void output_imm (fragS *, offsetT);
196static void output_disp (fragS *, offsetT);
29b0f896 197#ifndef I386COFF
e3bb37b5 198static void s_bss (int);
252b5132 199#endif
17d4e2a2
L
200#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
201static void handle_large_common (int small ATTRIBUTE_UNUSED);
b4a3a7b4
L
202
203/* GNU_PROPERTY_X86_ISA_1_USED. */
204static unsigned int x86_isa_1_used;
205/* GNU_PROPERTY_X86_FEATURE_2_USED. */
206static unsigned int x86_feature_2_used;
207/* Generate x86 used ISA and feature properties. */
208static unsigned int x86_used_note = DEFAULT_X86_USED_NOTE;
17d4e2a2 209#endif
252b5132 210
a847613f 211static const char *default_arch = DEFAULT_ARCH;
3e73aa7c 212
8a6fb3f9
JB
213/* parse_register() returns this when a register alias cannot be used. */
214static const reg_entry bad_reg = { "<bad>", OPERAND_TYPE_NONE, 0, 0,
215 { Dw2Inval, Dw2Inval } };
216
6225c532
JB
217static const reg_entry *reg_k0;
218
43234a1e
L
219/* This struct describes rounding control and SAE in the instruction. */
220struct RC_Operation
221{
222 enum rc_type
223 {
224 rne = 0,
225 rd,
226 ru,
227 rz,
228 saeonly
229 } type;
783c187b 230 unsigned int operand;
43234a1e
L
231};
232
233static struct RC_Operation rc_op;
234
43234a1e
L
235/* The struct describes broadcasting, applied to OPERAND. FACTOR is
236 broadcast factor. */
237struct Broadcast_Operation
238{
8e6e0792 239 /* Type of broadcast: {1to2}, {1to4}, {1to8}, or {1to16}. */
43234a1e
L
240 int type;
241
242 /* Index of broadcasted operand. */
783c187b 243 unsigned int operand;
4a1b91ea
L
244
245 /* Number of bytes to broadcast. */
246 int bytes;
43234a1e
L
247};
248
249static struct Broadcast_Operation broadcast_op;
250
c0f3af97
L
251/* VEX prefix. */
252typedef struct
253{
43234a1e
L
254 /* VEX prefix is either 2 byte or 3 byte. EVEX is 4 byte. */
255 unsigned char bytes[4];
c0f3af97
L
256 unsigned int length;
257 /* Destination or source register specifier. */
258 const reg_entry *register_specifier;
259} vex_prefix;
260
252b5132 261/* 'md_assemble ()' gathers together information and puts it into a
47926f60 262 i386_insn. */
252b5132 263
520dc8e8
AM
264union i386_op
265 {
266 expressionS *disps;
267 expressionS *imms;
268 const reg_entry *regs;
269 };
270
a65babc9
L
271enum i386_error
272 {
86e026a4 273 operand_size_mismatch,
a65babc9
L
274 operand_type_mismatch,
275 register_type_mismatch,
276 number_of_operands_mismatch,
277 invalid_instruction_suffix,
278 bad_imm4,
a65babc9
L
279 unsupported_with_intel_mnemonic,
280 unsupported_syntax,
6c30d220 281 unsupported,
260cd341 282 invalid_sib_address,
6c30d220 283 invalid_vsib_address,
7bab8ab5 284 invalid_vector_register_set,
260cd341 285 invalid_tmm_register_set,
43234a1e
L
286 unsupported_vector_index_register,
287 unsupported_broadcast,
43234a1e
L
288 broadcast_needed,
289 unsupported_masking,
290 mask_not_on_destination,
291 no_default_mask,
292 unsupported_rc_sae,
293 rc_sae_operand_not_last_imm,
294 invalid_register_operand,
a65babc9
L
295 };
296
252b5132
RH
297struct _i386_insn
298 {
47926f60 299 /* TM holds the template for the insn were currently assembling. */
d3ce72d0 300 insn_template tm;
252b5132 301
7d5e4556
L
302 /* SUFFIX holds the instruction size suffix for byte, word, dword
303 or qword, if given. */
252b5132
RH
304 char suffix;
305
9a182d04
JB
306 /* OPCODE_LENGTH holds the number of base opcode bytes. */
307 unsigned char opcode_length;
308
47926f60 309 /* OPERANDS gives the number of given operands. */
252b5132
RH
310 unsigned int operands;
311
312 /* REG_OPERANDS, DISP_OPERANDS, MEM_OPERANDS, IMM_OPERANDS give the number
313 of given register, displacement, memory operands and immediate
47926f60 314 operands. */
252b5132
RH
315 unsigned int reg_operands, disp_operands, mem_operands, imm_operands;
316
317 /* TYPES [i] is the type (see above #defines) which tells us how to
520dc8e8 318 use OP[i] for the corresponding operand. */
40fb9820 319 i386_operand_type types[MAX_OPERANDS];
252b5132 320
520dc8e8
AM
321 /* Displacement expression, immediate expression, or register for each
322 operand. */
323 union i386_op op[MAX_OPERANDS];
252b5132 324
3e73aa7c
JH
325 /* Flags for operands. */
326 unsigned int flags[MAX_OPERANDS];
327#define Operand_PCrel 1
c48dadc9 328#define Operand_Mem 2
3e73aa7c 329
252b5132 330 /* Relocation type for operand */
f86103b7 331 enum bfd_reloc_code_real reloc[MAX_OPERANDS];
252b5132 332
252b5132
RH
333 /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
334 the base index byte below. */
335 const reg_entry *base_reg;
336 const reg_entry *index_reg;
337 unsigned int log2_scale_factor;
338
339 /* SEG gives the seg_entries of this insn. They are zero unless
47926f60 340 explicit segment overrides are given. */
ce8a8b2f 341 const seg_entry *seg[2];
252b5132 342
8325cc63
JB
343 /* Copied first memory operand string, for re-checking. */
344 char *memop1_string;
345
252b5132
RH
346 /* PREFIX holds all the given prefix opcodes (usually null).
347 PREFIXES is the number of prefix opcodes. */
348 unsigned int prefixes;
349 unsigned char prefix[MAX_PREFIXES];
350
50128d0c
JB
351 /* Register is in low 3 bits of opcode. */
352 bfd_boolean short_form;
353
6f2f06be
JB
354 /* The operand to a branch insn indicates an absolute branch. */
355 bfd_boolean jumpabsolute;
356
921eafea
L
357 /* Extended states. */
358 enum
359 {
360 /* Use MMX state. */
361 xstate_mmx = 1 << 0,
362 /* Use XMM state. */
363 xstate_xmm = 1 << 1,
364 /* Use YMM state. */
365 xstate_ymm = 1 << 2 | xstate_xmm,
366 /* Use ZMM state. */
367 xstate_zmm = 1 << 3 | xstate_ymm,
368 /* Use TMM state. */
32930e4e
L
369 xstate_tmm = 1 << 4,
370 /* Use MASK state. */
371 xstate_mask = 1 << 5
921eafea 372 } xstate;
260cd341 373
e379e5f3
L
374 /* Has GOTPC or TLS relocation. */
375 bfd_boolean has_gotpc_tls_reloc;
376
252b5132 377 /* RM and SIB are the modrm byte and the sib byte where the
c1e679ec 378 addressing modes of this insn are encoded. */
252b5132 379 modrm_byte rm;
3e73aa7c 380 rex_byte rex;
43234a1e 381 rex_byte vrex;
252b5132 382 sib_byte sib;
c0f3af97 383 vex_prefix vex;
b6169b20 384
6225c532
JB
385 /* Masking attributes.
386
387 The struct describes masking, applied to OPERAND in the instruction.
388 REG is a pointer to the corresponding mask register. ZEROING tells
389 whether merging or zeroing mask is used. */
390 struct Mask_Operation
391 {
392 const reg_entry *reg;
393 unsigned int zeroing;
394 /* The operand where this operation is associated. */
395 unsigned int operand;
396 } mask;
43234a1e
L
397
398 /* Rounding control and SAE attributes. */
399 struct RC_Operation *rounding;
400
401 /* Broadcasting attributes. */
402 struct Broadcast_Operation *broadcast;
403
404 /* Compressed disp8*N attribute. */
405 unsigned int memshift;
406
86fa6981
L
407 /* Prefer load or store in encoding. */
408 enum
409 {
410 dir_encoding_default = 0,
411 dir_encoding_load,
64c49ab3
JB
412 dir_encoding_store,
413 dir_encoding_swap
86fa6981 414 } dir_encoding;
891edac4 415
41eb8e88 416 /* Prefer 8bit, 16bit, 32bit displacement in encoding. */
a501d77e
L
417 enum
418 {
419 disp_encoding_default = 0,
420 disp_encoding_8bit,
41eb8e88 421 disp_encoding_16bit,
a501d77e
L
422 disp_encoding_32bit
423 } disp_encoding;
f8a5c266 424
6b6b6807
L
425 /* Prefer the REX byte in encoding. */
426 bfd_boolean rex_encoding;
427
b6f8c7c4
L
428 /* Disable instruction size optimization. */
429 bfd_boolean no_optimize;
430
86fa6981
L
431 /* How to encode vector instructions. */
432 enum
433 {
434 vex_encoding_default = 0,
42e04b36 435 vex_encoding_vex,
86fa6981 436 vex_encoding_vex3,
da4977e0
JB
437 vex_encoding_evex,
438 vex_encoding_error
86fa6981
L
439 } vec_encoding;
440
d5de92cf
L
441 /* REP prefix. */
442 const char *rep_prefix;
443
165de32a
L
444 /* HLE prefix. */
445 const char *hle_prefix;
42164a71 446
7e8b059b
L
447 /* Have BND prefix. */
448 const char *bnd_prefix;
449
04ef582a
L
450 /* Have NOTRACK prefix. */
451 const char *notrack_prefix;
452
891edac4 453 /* Error message. */
a65babc9 454 enum i386_error error;
252b5132
RH
455 };
456
457typedef struct _i386_insn i386_insn;
458
43234a1e
L
459/* Link RC type with corresponding string, that'll be looked for in
460 asm. */
461struct RC_name
462{
463 enum rc_type type;
464 const char *name;
465 unsigned int len;
466};
467
468static const struct RC_name RC_NamesTable[] =
469{
470 { rne, STRING_COMMA_LEN ("rn-sae") },
471 { rd, STRING_COMMA_LEN ("rd-sae") },
472 { ru, STRING_COMMA_LEN ("ru-sae") },
473 { rz, STRING_COMMA_LEN ("rz-sae") },
474 { saeonly, STRING_COMMA_LEN ("sae") },
475};
476
252b5132
RH
477/* List of chars besides those in app.c:symbol_chars that can start an
478 operand. Used to prevent the scrubber eating vital white-space. */
86fa6981 479const char extra_symbol_chars[] = "*%-([{}"
252b5132 480#ifdef LEX_AT
32137342
NC
481 "@"
482#endif
483#ifdef LEX_QM
484 "?"
252b5132 485#endif
32137342 486 ;
252b5132 487
b3983e5f
JB
488#if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
489 && !defined (TE_GNU) \
490 && !defined (TE_LINUX) \
491 && !defined (TE_FreeBSD) \
492 && !defined (TE_DragonFly) \
493 && !defined (TE_NetBSD))
252b5132 494/* This array holds the chars that always start a comment. If the
b3b91714
AM
495 pre-processor is disabled, these aren't very useful. The option
496 --divide will remove '/' from this list. */
497const char *i386_comment_chars = "#/";
498#define SVR4_COMMENT_CHARS 1
252b5132 499#define PREFIX_SEPARATOR '\\'
252b5132 500
b3b91714
AM
501#else
502const char *i386_comment_chars = "#";
503#define PREFIX_SEPARATOR '/'
504#endif
505
252b5132
RH
506/* This array holds the chars that only start a comment at the beginning of
507 a line. If the line seems to have the form '# 123 filename'
ce8a8b2f
AM
508 .line and .file directives will appear in the pre-processed output.
509 Note that input_file.c hand checks for '#' at the beginning of the
252b5132 510 first line of the input file. This is because the compiler outputs
ce8a8b2f
AM
511 #NO_APP at the beginning of its output.
512 Also note that comments started like this one will always work if
252b5132 513 '/' isn't otherwise defined. */
b3b91714 514const char line_comment_chars[] = "#/";
252b5132 515
63a0b638 516const char line_separator_chars[] = ";";
252b5132 517
ce8a8b2f
AM
518/* Chars that can be used to separate mant from exp in floating point
519 nums. */
252b5132
RH
520const char EXP_CHARS[] = "eE";
521
ce8a8b2f
AM
522/* Chars that mean this number is a floating point constant
523 As in 0f12.456
524 or 0d1.2345e12. */
252b5132
RH
525const char FLT_CHARS[] = "fFdDxX";
526
ce8a8b2f 527/* Tables for lexical analysis. */
252b5132
RH
528static char mnemonic_chars[256];
529static char register_chars[256];
530static char operand_chars[256];
531static char identifier_chars[256];
532static char digit_chars[256];
533
ce8a8b2f 534/* Lexical macros. */
252b5132
RH
535#define is_mnemonic_char(x) (mnemonic_chars[(unsigned char) x])
536#define is_operand_char(x) (operand_chars[(unsigned char) x])
537#define is_register_char(x) (register_chars[(unsigned char) x])
538#define is_space_char(x) ((x) == ' ')
539#define is_identifier_char(x) (identifier_chars[(unsigned char) x])
540#define is_digit_char(x) (digit_chars[(unsigned char) x])
541
0234cb7c 542/* All non-digit non-letter characters that may occur in an operand. */
252b5132
RH
543static char operand_special_chars[] = "%$-+(,)*._~/<>|&^!:[@]";
544
545/* md_assemble() always leaves the strings it's passed unaltered. To
546 effect this we maintain a stack of saved characters that we've smashed
547 with '\0's (indicating end of strings for various sub-fields of the
47926f60 548 assembler instruction). */
252b5132 549static char save_stack[32];
ce8a8b2f 550static char *save_stack_p;
252b5132
RH
551#define END_STRING_AND_SAVE(s) \
552 do { *save_stack_p++ = *(s); *(s) = '\0'; } while (0)
553#define RESTORE_END_STRING(s) \
554 do { *(s) = *--save_stack_p; } while (0)
555
47926f60 556/* The instruction we're assembling. */
252b5132
RH
557static i386_insn i;
558
559/* Possible templates for current insn. */
560static const templates *current_templates;
561
31b2323c
L
562/* Per instruction expressionS buffers: max displacements & immediates. */
563static expressionS disp_expressions[MAX_MEMORY_OPERANDS];
564static expressionS im_expressions[MAX_IMMEDIATE_OPERANDS];
252b5132 565
47926f60 566/* Current operand we are working on. */
ee86248c 567static int this_operand = -1;
252b5132 568
3e73aa7c
JH
569/* We support four different modes. FLAG_CODE variable is used to distinguish
570 these. */
571
572enum flag_code {
573 CODE_32BIT,
574 CODE_16BIT,
575 CODE_64BIT };
576
577static enum flag_code flag_code;
4fa24527 578static unsigned int object_64bit;
862be3fb 579static unsigned int disallow_64bit_reloc;
3e73aa7c 580static int use_rela_relocations = 0;
e379e5f3
L
581/* __tls_get_addr/___tls_get_addr symbol for TLS. */
582static const char *tls_get_addr;
3e73aa7c 583
7af8ed2d
NC
584#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
585 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
586 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
587
351f65ca
L
588/* The ELF ABI to use. */
589enum x86_elf_abi
590{
591 I386_ABI,
7f56bc95
L
592 X86_64_ABI,
593 X86_64_X32_ABI
351f65ca
L
594};
595
596static enum x86_elf_abi x86_elf_abi = I386_ABI;
7af8ed2d 597#endif
351f65ca 598
167ad85b
TG
599#if defined (TE_PE) || defined (TE_PEP)
600/* Use big object file format. */
601static int use_big_obj = 0;
602#endif
603
8dcea932
L
604#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
605/* 1 if generating code for a shared library. */
606static int shared = 0;
607#endif
608
47926f60
KH
609/* 1 for intel syntax,
610 0 if att syntax. */
611static int intel_syntax = 0;
252b5132 612
4b5aaf5f
L
613static enum x86_64_isa
614{
615 amd64 = 1, /* AMD64 ISA. */
616 intel64 /* Intel64 ISA. */
617} isa64;
e89c5eaa 618
1efbbeb4
L
619/* 1 for intel mnemonic,
620 0 if att mnemonic. */
621static int intel_mnemonic = !SYSV386_COMPAT;
622
a60de03c
JB
623/* 1 if pseudo registers are permitted. */
624static int allow_pseudo_reg = 0;
625
47926f60
KH
626/* 1 if register prefix % not required. */
627static int allow_naked_reg = 0;
252b5132 628
33eaf5de 629/* 1 if the assembler should add BND prefix for all control-transferring
7e8b059b
L
630 instructions supporting it, even if this prefix wasn't specified
631 explicitly. */
632static int add_bnd_prefix = 0;
633
ba104c83 634/* 1 if pseudo index register, eiz/riz, is allowed . */
db51cc60
L
635static int allow_index_reg = 0;
636
d022bddd
IT
637/* 1 if the assembler should ignore LOCK prefix, even if it was
638 specified explicitly. */
639static int omit_lock_prefix = 0;
640
e4e00185
AS
641/* 1 if the assembler should encode lfence, mfence, and sfence as
642 "lock addl $0, (%{re}sp)". */
643static int avoid_fence = 0;
644
ae531041
L
645/* 1 if lfence should be inserted after every load. */
646static int lfence_after_load = 0;
647
648/* Non-zero if lfence should be inserted before indirect branch. */
649static enum lfence_before_indirect_branch_kind
650 {
651 lfence_branch_none = 0,
652 lfence_branch_register,
653 lfence_branch_memory,
654 lfence_branch_all
655 }
656lfence_before_indirect_branch;
657
658/* Non-zero if lfence should be inserted before ret. */
659static enum lfence_before_ret_kind
660 {
661 lfence_before_ret_none = 0,
662 lfence_before_ret_not,
a09f656b 663 lfence_before_ret_or,
664 lfence_before_ret_shl
ae531041
L
665 }
666lfence_before_ret;
667
668/* Types of previous instruction is .byte or prefix. */
e379e5f3
L
669static struct
670 {
671 segT seg;
672 const char *file;
673 const char *name;
674 unsigned int line;
675 enum last_insn_kind
676 {
677 last_insn_other = 0,
678 last_insn_directive,
679 last_insn_prefix
680 } kind;
681 } last_insn;
682
0cb4071e
L
683/* 1 if the assembler should generate relax relocations. */
684
685static int generate_relax_relocations
686 = DEFAULT_GENERATE_X86_RELAX_RELOCATIONS;
687
7bab8ab5 688static enum check_kind
daf50ae7 689 {
7bab8ab5
JB
690 check_none = 0,
691 check_warning,
692 check_error
daf50ae7 693 }
7bab8ab5 694sse_check, operand_check = check_warning;
daf50ae7 695
e379e5f3
L
696/* Non-zero if branches should be aligned within power of 2 boundary. */
697static int align_branch_power = 0;
698
699/* Types of branches to align. */
700enum align_branch_kind
701 {
702 align_branch_none = 0,
703 align_branch_jcc = 1,
704 align_branch_fused = 2,
705 align_branch_jmp = 3,
706 align_branch_call = 4,
707 align_branch_indirect = 5,
708 align_branch_ret = 6
709 };
710
711/* Type bits of branches to align. */
712enum align_branch_bit
713 {
714 align_branch_jcc_bit = 1 << align_branch_jcc,
715 align_branch_fused_bit = 1 << align_branch_fused,
716 align_branch_jmp_bit = 1 << align_branch_jmp,
717 align_branch_call_bit = 1 << align_branch_call,
718 align_branch_indirect_bit = 1 << align_branch_indirect,
719 align_branch_ret_bit = 1 << align_branch_ret
720 };
721
722static unsigned int align_branch = (align_branch_jcc_bit
723 | align_branch_fused_bit
724 | align_branch_jmp_bit);
725
79d72f45
HL
726/* Types of condition jump used by macro-fusion. */
727enum mf_jcc_kind
728 {
729 mf_jcc_jo = 0, /* base opcode 0x70 */
730 mf_jcc_jc, /* base opcode 0x72 */
731 mf_jcc_je, /* base opcode 0x74 */
732 mf_jcc_jna, /* base opcode 0x76 */
733 mf_jcc_js, /* base opcode 0x78 */
734 mf_jcc_jp, /* base opcode 0x7a */
735 mf_jcc_jl, /* base opcode 0x7c */
736 mf_jcc_jle, /* base opcode 0x7e */
737 };
738
739/* Types of compare flag-modifying insntructions used by macro-fusion. */
740enum mf_cmp_kind
741 {
742 mf_cmp_test_and, /* test/cmp */
743 mf_cmp_alu_cmp, /* add/sub/cmp */
744 mf_cmp_incdec /* inc/dec */
745 };
746
e379e5f3
L
747/* The maximum padding size for fused jcc. CMP like instruction can
748 be 9 bytes and jcc can be 6 bytes. Leave room just in case for
749 prefixes. */
750#define MAX_FUSED_JCC_PADDING_SIZE 20
751
752/* The maximum number of prefixes added for an instruction. */
753static unsigned int align_branch_prefix_size = 5;
754
b6f8c7c4
L
755/* Optimization:
756 1. Clear the REX_W bit with register operand if possible.
757 2. Above plus use 128bit vector instruction to clear the full vector
758 register.
759 */
760static int optimize = 0;
761
762/* Optimization:
763 1. Clear the REX_W bit with register operand if possible.
764 2. Above plus use 128bit vector instruction to clear the full vector
765 register.
766 3. Above plus optimize "test{q,l,w} $imm8,%r{64,32,16}" to
767 "testb $imm7,%r8".
768 */
769static int optimize_for_space = 0;
770
2ca3ace5
L
771/* Register prefix used for error message. */
772static const char *register_prefix = "%";
773
47926f60
KH
774/* Used in 16 bit gcc mode to add an l suffix to call, ret, enter,
775 leave, push, and pop instructions so that gcc has the same stack
776 frame as in 32 bit mode. */
777static char stackop_size = '\0';
eecb386c 778
12b55ccc
L
779/* Non-zero to optimize code alignment. */
780int optimize_align_code = 1;
781
47926f60
KH
782/* Non-zero to quieten some warnings. */
783static int quiet_warnings = 0;
a38cf1db 784
47926f60
KH
785/* CPU name. */
786static const char *cpu_arch_name = NULL;
6305a203 787static char *cpu_sub_arch_name = NULL;
a38cf1db 788
47926f60 789/* CPU feature flags. */
40fb9820
L
790static i386_cpu_flags cpu_arch_flags = CPU_UNKNOWN_FLAGS;
791
ccc9c027
L
792/* If we have selected a cpu we are generating instructions for. */
793static int cpu_arch_tune_set = 0;
794
9103f4f4 795/* Cpu we are generating instructions for. */
fbf3f584 796enum processor_type cpu_arch_tune = PROCESSOR_UNKNOWN;
9103f4f4
L
797
798/* CPU feature flags of cpu we are generating instructions for. */
40fb9820 799static i386_cpu_flags cpu_arch_tune_flags;
9103f4f4 800
ccc9c027 801/* CPU instruction set architecture used. */
fbf3f584 802enum processor_type cpu_arch_isa = PROCESSOR_UNKNOWN;
ccc9c027 803
9103f4f4 804/* CPU feature flags of instruction set architecture used. */
fbf3f584 805i386_cpu_flags cpu_arch_isa_flags;
9103f4f4 806
fddf5b5b
AM
807/* If set, conditional jumps are not automatically promoted to handle
808 larger than a byte offset. */
809static unsigned int no_cond_jump_promotion = 0;
810
c0f3af97
L
811/* Encode SSE instructions with VEX prefix. */
812static unsigned int sse2avx;
813
539f890d
L
814/* Encode scalar AVX instructions with specific vector length. */
815static enum
816 {
817 vex128 = 0,
818 vex256
819 } avxscalar;
820
03751133
L
821/* Encode VEX WIG instructions with specific vex.w. */
822static enum
823 {
824 vexw0 = 0,
825 vexw1
826 } vexwig;
827
43234a1e
L
828/* Encode scalar EVEX LIG instructions with specific vector length. */
829static enum
830 {
831 evexl128 = 0,
832 evexl256,
833 evexl512
834 } evexlig;
835
836/* Encode EVEX WIG instructions with specific evex.w. */
837static enum
838 {
839 evexw0 = 0,
840 evexw1
841 } evexwig;
842
d3d3c6db
IT
843/* Value to encode in EVEX RC bits, for SAE-only instructions. */
844static enum rc_type evexrcig = rne;
845
29b0f896 846/* Pre-defined "_GLOBAL_OFFSET_TABLE_". */
87c245cc 847static symbolS *GOT_symbol;
29b0f896 848
a4447b93
RH
849/* The dwarf2 return column, adjusted for 32 or 64 bit. */
850unsigned int x86_dwarf2_return_column;
851
852/* The dwarf2 data alignment, adjusted for 32 or 64 bit. */
853int x86_cie_data_alignment;
854
252b5132 855/* Interface to relax_segment.
fddf5b5b
AM
856 There are 3 major relax states for 386 jump insns because the
857 different types of jumps add different sizes to frags when we're
e379e5f3
L
858 figuring out what sort of jump to choose to reach a given label.
859
860 BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING are used to align
861 branches which are handled by md_estimate_size_before_relax() and
862 i386_generic_table_relax_frag(). */
252b5132 863
47926f60 864/* Types. */
93c2a809
AM
865#define UNCOND_JUMP 0
866#define COND_JUMP 1
867#define COND_JUMP86 2
e379e5f3
L
868#define BRANCH_PADDING 3
869#define BRANCH_PREFIX 4
870#define FUSED_JCC_PADDING 5
fddf5b5b 871
47926f60 872/* Sizes. */
252b5132
RH
873#define CODE16 1
874#define SMALL 0
29b0f896 875#define SMALL16 (SMALL | CODE16)
252b5132 876#define BIG 2
29b0f896 877#define BIG16 (BIG | CODE16)
252b5132
RH
878
879#ifndef INLINE
880#ifdef __GNUC__
881#define INLINE __inline__
882#else
883#define INLINE
884#endif
885#endif
886
fddf5b5b
AM
887#define ENCODE_RELAX_STATE(type, size) \
888 ((relax_substateT) (((type) << 2) | (size)))
889#define TYPE_FROM_RELAX_STATE(s) \
890 ((s) >> 2)
891#define DISP_SIZE_FROM_RELAX_STATE(s) \
892 ((((s) & 3) == BIG ? 4 : (((s) & 3) == BIG16 ? 2 : 1)))
252b5132
RH
893
894/* This table is used by relax_frag to promote short jumps to long
895 ones where necessary. SMALL (short) jumps may be promoted to BIG
896 (32 bit long) ones, and SMALL16 jumps to BIG16 (16 bit long). We
897 don't allow a short jump in a 32 bit code segment to be promoted to
898 a 16 bit offset jump because it's slower (requires data size
899 prefix), and doesn't work, unless the destination is in the bottom
900 64k of the code segment (The top 16 bits of eip are zeroed). */
901
902const relax_typeS md_relax_table[] =
903{
24eab124
AM
904 /* The fields are:
905 1) most positive reach of this state,
906 2) most negative reach of this state,
93c2a809 907 3) how many bytes this mode will have in the variable part of the frag
ce8a8b2f 908 4) which index into the table to try if we can't fit into this one. */
252b5132 909
fddf5b5b 910 /* UNCOND_JUMP states. */
93c2a809
AM
911 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG)},
912 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16)},
913 /* dword jmp adds 4 bytes to frag:
914 0 extra opcode bytes, 4 displacement bytes. */
252b5132 915 {0, 0, 4, 0},
93c2a809
AM
916 /* word jmp adds 2 byte2 to frag:
917 0 extra opcode bytes, 2 displacement bytes. */
252b5132
RH
918 {0, 0, 2, 0},
919
93c2a809
AM
920 /* COND_JUMP states. */
921 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG)},
922 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG16)},
923 /* dword conditionals adds 5 bytes to frag:
924 1 extra opcode byte, 4 displacement bytes. */
925 {0, 0, 5, 0},
fddf5b5b 926 /* word conditionals add 3 bytes to frag:
93c2a809
AM
927 1 extra opcode byte, 2 displacement bytes. */
928 {0, 0, 3, 0},
929
930 /* COND_JUMP86 states. */
931 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG)},
932 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG16)},
933 /* dword conditionals adds 5 bytes to frag:
934 1 extra opcode byte, 4 displacement bytes. */
935 {0, 0, 5, 0},
936 /* word conditionals add 4 bytes to frag:
937 1 displacement byte and a 3 byte long branch insn. */
938 {0, 0, 4, 0}
252b5132
RH
939};
940
9103f4f4
L
941static const arch_entry cpu_arch[] =
942{
89507696
JB
943 /* Do not replace the first two entries - i386_target_format()
944 relies on them being there in this order. */
8a2c8fef 945 { STRING_COMMA_LEN ("generic32"), PROCESSOR_GENERIC32,
293f5f65 946 CPU_GENERIC32_FLAGS, 0 },
8a2c8fef 947 { STRING_COMMA_LEN ("generic64"), PROCESSOR_GENERIC64,
293f5f65 948 CPU_GENERIC64_FLAGS, 0 },
8a2c8fef 949 { STRING_COMMA_LEN ("i8086"), PROCESSOR_UNKNOWN,
293f5f65 950 CPU_NONE_FLAGS, 0 },
8a2c8fef 951 { STRING_COMMA_LEN ("i186"), PROCESSOR_UNKNOWN,
293f5f65 952 CPU_I186_FLAGS, 0 },
8a2c8fef 953 { STRING_COMMA_LEN ("i286"), PROCESSOR_UNKNOWN,
293f5f65 954 CPU_I286_FLAGS, 0 },
8a2c8fef 955 { STRING_COMMA_LEN ("i386"), PROCESSOR_I386,
293f5f65 956 CPU_I386_FLAGS, 0 },
8a2c8fef 957 { STRING_COMMA_LEN ("i486"), PROCESSOR_I486,
293f5f65 958 CPU_I486_FLAGS, 0 },
8a2c8fef 959 { STRING_COMMA_LEN ("i586"), PROCESSOR_PENTIUM,
293f5f65 960 CPU_I586_FLAGS, 0 },
8a2c8fef 961 { STRING_COMMA_LEN ("i686"), PROCESSOR_PENTIUMPRO,
293f5f65 962 CPU_I686_FLAGS, 0 },
8a2c8fef 963 { STRING_COMMA_LEN ("pentium"), PROCESSOR_PENTIUM,
293f5f65 964 CPU_I586_FLAGS, 0 },
8a2c8fef 965 { STRING_COMMA_LEN ("pentiumpro"), PROCESSOR_PENTIUMPRO,
293f5f65 966 CPU_PENTIUMPRO_FLAGS, 0 },
8a2c8fef 967 { STRING_COMMA_LEN ("pentiumii"), PROCESSOR_PENTIUMPRO,
293f5f65 968 CPU_P2_FLAGS, 0 },
8a2c8fef 969 { STRING_COMMA_LEN ("pentiumiii"),PROCESSOR_PENTIUMPRO,
293f5f65 970 CPU_P3_FLAGS, 0 },
8a2c8fef 971 { STRING_COMMA_LEN ("pentium4"), PROCESSOR_PENTIUM4,
293f5f65 972 CPU_P4_FLAGS, 0 },
8a2c8fef 973 { STRING_COMMA_LEN ("prescott"), PROCESSOR_NOCONA,
293f5f65 974 CPU_CORE_FLAGS, 0 },
8a2c8fef 975 { STRING_COMMA_LEN ("nocona"), PROCESSOR_NOCONA,
293f5f65 976 CPU_NOCONA_FLAGS, 0 },
8a2c8fef 977 { STRING_COMMA_LEN ("yonah"), PROCESSOR_CORE,
293f5f65 978 CPU_CORE_FLAGS, 1 },
8a2c8fef 979 { STRING_COMMA_LEN ("core"), PROCESSOR_CORE,
293f5f65 980 CPU_CORE_FLAGS, 0 },
8a2c8fef 981 { STRING_COMMA_LEN ("merom"), PROCESSOR_CORE2,
293f5f65 982 CPU_CORE2_FLAGS, 1 },
8a2c8fef 983 { STRING_COMMA_LEN ("core2"), PROCESSOR_CORE2,
293f5f65 984 CPU_CORE2_FLAGS, 0 },
8a2c8fef 985 { STRING_COMMA_LEN ("corei7"), PROCESSOR_COREI7,
293f5f65 986 CPU_COREI7_FLAGS, 0 },
8a2c8fef 987 { STRING_COMMA_LEN ("l1om"), PROCESSOR_L1OM,
293f5f65 988 CPU_L1OM_FLAGS, 0 },
7a9068fe 989 { STRING_COMMA_LEN ("k1om"), PROCESSOR_K1OM,
293f5f65 990 CPU_K1OM_FLAGS, 0 },
81486035 991 { STRING_COMMA_LEN ("iamcu"), PROCESSOR_IAMCU,
293f5f65 992 CPU_IAMCU_FLAGS, 0 },
8a2c8fef 993 { STRING_COMMA_LEN ("k6"), PROCESSOR_K6,
293f5f65 994 CPU_K6_FLAGS, 0 },
8a2c8fef 995 { STRING_COMMA_LEN ("k6_2"), PROCESSOR_K6,
293f5f65 996 CPU_K6_2_FLAGS, 0 },
8a2c8fef 997 { STRING_COMMA_LEN ("athlon"), PROCESSOR_ATHLON,
293f5f65 998 CPU_ATHLON_FLAGS, 0 },
8a2c8fef 999 { STRING_COMMA_LEN ("sledgehammer"), PROCESSOR_K8,
293f5f65 1000 CPU_K8_FLAGS, 1 },
8a2c8fef 1001 { STRING_COMMA_LEN ("opteron"), PROCESSOR_K8,
293f5f65 1002 CPU_K8_FLAGS, 0 },
8a2c8fef 1003 { STRING_COMMA_LEN ("k8"), PROCESSOR_K8,
293f5f65 1004 CPU_K8_FLAGS, 0 },
8a2c8fef 1005 { STRING_COMMA_LEN ("amdfam10"), PROCESSOR_AMDFAM10,
293f5f65 1006 CPU_AMDFAM10_FLAGS, 0 },
8aedb9fe 1007 { STRING_COMMA_LEN ("bdver1"), PROCESSOR_BD,
293f5f65 1008 CPU_BDVER1_FLAGS, 0 },
8aedb9fe 1009 { STRING_COMMA_LEN ("bdver2"), PROCESSOR_BD,
293f5f65 1010 CPU_BDVER2_FLAGS, 0 },
5e5c50d3 1011 { STRING_COMMA_LEN ("bdver3"), PROCESSOR_BD,
293f5f65 1012 CPU_BDVER3_FLAGS, 0 },
c7b0bd56 1013 { STRING_COMMA_LEN ("bdver4"), PROCESSOR_BD,
293f5f65 1014 CPU_BDVER4_FLAGS, 0 },
029f3522 1015 { STRING_COMMA_LEN ("znver1"), PROCESSOR_ZNVER,
293f5f65 1016 CPU_ZNVER1_FLAGS, 0 },
a9660a6f
AP
1017 { STRING_COMMA_LEN ("znver2"), PROCESSOR_ZNVER,
1018 CPU_ZNVER2_FLAGS, 0 },
646cc3e0
GG
1019 { STRING_COMMA_LEN ("znver3"), PROCESSOR_ZNVER,
1020 CPU_ZNVER3_FLAGS, 0 },
7b458c12 1021 { STRING_COMMA_LEN ("btver1"), PROCESSOR_BT,
293f5f65 1022 CPU_BTVER1_FLAGS, 0 },
7b458c12 1023 { STRING_COMMA_LEN ("btver2"), PROCESSOR_BT,
293f5f65 1024 CPU_BTVER2_FLAGS, 0 },
8a2c8fef 1025 { STRING_COMMA_LEN (".8087"), PROCESSOR_UNKNOWN,
293f5f65 1026 CPU_8087_FLAGS, 0 },
8a2c8fef 1027 { STRING_COMMA_LEN (".287"), PROCESSOR_UNKNOWN,
293f5f65 1028 CPU_287_FLAGS, 0 },
8a2c8fef 1029 { STRING_COMMA_LEN (".387"), PROCESSOR_UNKNOWN,
293f5f65 1030 CPU_387_FLAGS, 0 },
1848e567
L
1031 { STRING_COMMA_LEN (".687"), PROCESSOR_UNKNOWN,
1032 CPU_687_FLAGS, 0 },
d871f3f4
L
1033 { STRING_COMMA_LEN (".cmov"), PROCESSOR_UNKNOWN,
1034 CPU_CMOV_FLAGS, 0 },
1035 { STRING_COMMA_LEN (".fxsr"), PROCESSOR_UNKNOWN,
1036 CPU_FXSR_FLAGS, 0 },
8a2c8fef 1037 { STRING_COMMA_LEN (".mmx"), PROCESSOR_UNKNOWN,
293f5f65 1038 CPU_MMX_FLAGS, 0 },
8a2c8fef 1039 { STRING_COMMA_LEN (".sse"), PROCESSOR_UNKNOWN,
293f5f65 1040 CPU_SSE_FLAGS, 0 },
8a2c8fef 1041 { STRING_COMMA_LEN (".sse2"), PROCESSOR_UNKNOWN,
293f5f65 1042 CPU_SSE2_FLAGS, 0 },
8a2c8fef 1043 { STRING_COMMA_LEN (".sse3"), PROCESSOR_UNKNOWN,
293f5f65 1044 CPU_SSE3_FLAGS, 0 },
af5c13b0
L
1045 { STRING_COMMA_LEN (".sse4a"), PROCESSOR_UNKNOWN,
1046 CPU_SSE4A_FLAGS, 0 },
8a2c8fef 1047 { STRING_COMMA_LEN (".ssse3"), PROCESSOR_UNKNOWN,
293f5f65 1048 CPU_SSSE3_FLAGS, 0 },
8a2c8fef 1049 { STRING_COMMA_LEN (".sse4.1"), PROCESSOR_UNKNOWN,
293f5f65 1050 CPU_SSE4_1_FLAGS, 0 },
8a2c8fef 1051 { STRING_COMMA_LEN (".sse4.2"), PROCESSOR_UNKNOWN,
293f5f65 1052 CPU_SSE4_2_FLAGS, 0 },
8a2c8fef 1053 { STRING_COMMA_LEN (".sse4"), PROCESSOR_UNKNOWN,
293f5f65 1054 CPU_SSE4_2_FLAGS, 0 },
8a2c8fef 1055 { STRING_COMMA_LEN (".avx"), PROCESSOR_UNKNOWN,
293f5f65 1056 CPU_AVX_FLAGS, 0 },
6c30d220 1057 { STRING_COMMA_LEN (".avx2"), PROCESSOR_UNKNOWN,
293f5f65 1058 CPU_AVX2_FLAGS, 0 },
43234a1e 1059 { STRING_COMMA_LEN (".avx512f"), PROCESSOR_UNKNOWN,
293f5f65 1060 CPU_AVX512F_FLAGS, 0 },
43234a1e 1061 { STRING_COMMA_LEN (".avx512cd"), PROCESSOR_UNKNOWN,
293f5f65 1062 CPU_AVX512CD_FLAGS, 0 },
43234a1e 1063 { STRING_COMMA_LEN (".avx512er"), PROCESSOR_UNKNOWN,
293f5f65 1064 CPU_AVX512ER_FLAGS, 0 },
43234a1e 1065 { STRING_COMMA_LEN (".avx512pf"), PROCESSOR_UNKNOWN,
293f5f65 1066 CPU_AVX512PF_FLAGS, 0 },
1dfc6506 1067 { STRING_COMMA_LEN (".avx512dq"), PROCESSOR_UNKNOWN,
293f5f65 1068 CPU_AVX512DQ_FLAGS, 0 },
1dfc6506 1069 { STRING_COMMA_LEN (".avx512bw"), PROCESSOR_UNKNOWN,
293f5f65 1070 CPU_AVX512BW_FLAGS, 0 },
1dfc6506 1071 { STRING_COMMA_LEN (".avx512vl"), PROCESSOR_UNKNOWN,
293f5f65 1072 CPU_AVX512VL_FLAGS, 0 },
8a2c8fef 1073 { STRING_COMMA_LEN (".vmx"), PROCESSOR_UNKNOWN,
293f5f65 1074 CPU_VMX_FLAGS, 0 },
8729a6f6 1075 { STRING_COMMA_LEN (".vmfunc"), PROCESSOR_UNKNOWN,
293f5f65 1076 CPU_VMFUNC_FLAGS, 0 },
8a2c8fef 1077 { STRING_COMMA_LEN (".smx"), PROCESSOR_UNKNOWN,
293f5f65 1078 CPU_SMX_FLAGS, 0 },
8a2c8fef 1079 { STRING_COMMA_LEN (".xsave"), PROCESSOR_UNKNOWN,
293f5f65 1080 CPU_XSAVE_FLAGS, 0 },
c7b8aa3a 1081 { STRING_COMMA_LEN (".xsaveopt"), PROCESSOR_UNKNOWN,
293f5f65 1082 CPU_XSAVEOPT_FLAGS, 0 },
1dfc6506 1083 { STRING_COMMA_LEN (".xsavec"), PROCESSOR_UNKNOWN,
293f5f65 1084 CPU_XSAVEC_FLAGS, 0 },
1dfc6506 1085 { STRING_COMMA_LEN (".xsaves"), PROCESSOR_UNKNOWN,
293f5f65 1086 CPU_XSAVES_FLAGS, 0 },
8a2c8fef 1087 { STRING_COMMA_LEN (".aes"), PROCESSOR_UNKNOWN,
293f5f65 1088 CPU_AES_FLAGS, 0 },
8a2c8fef 1089 { STRING_COMMA_LEN (".pclmul"), PROCESSOR_UNKNOWN,
293f5f65 1090 CPU_PCLMUL_FLAGS, 0 },
8a2c8fef 1091 { STRING_COMMA_LEN (".clmul"), PROCESSOR_UNKNOWN,
293f5f65 1092 CPU_PCLMUL_FLAGS, 1 },
c7b8aa3a 1093 { STRING_COMMA_LEN (".fsgsbase"), PROCESSOR_UNKNOWN,
293f5f65 1094 CPU_FSGSBASE_FLAGS, 0 },
c7b8aa3a 1095 { STRING_COMMA_LEN (".rdrnd"), PROCESSOR_UNKNOWN,
293f5f65 1096 CPU_RDRND_FLAGS, 0 },
c7b8aa3a 1097 { STRING_COMMA_LEN (".f16c"), PROCESSOR_UNKNOWN,
293f5f65 1098 CPU_F16C_FLAGS, 0 },
6c30d220 1099 { STRING_COMMA_LEN (".bmi2"), PROCESSOR_UNKNOWN,
293f5f65 1100 CPU_BMI2_FLAGS, 0 },
8a2c8fef 1101 { STRING_COMMA_LEN (".fma"), PROCESSOR_UNKNOWN,
293f5f65 1102 CPU_FMA_FLAGS, 0 },
8a2c8fef 1103 { STRING_COMMA_LEN (".fma4"), PROCESSOR_UNKNOWN,
293f5f65 1104 CPU_FMA4_FLAGS, 0 },
8a2c8fef 1105 { STRING_COMMA_LEN (".xop"), PROCESSOR_UNKNOWN,
293f5f65 1106 CPU_XOP_FLAGS, 0 },
8a2c8fef 1107 { STRING_COMMA_LEN (".lwp"), PROCESSOR_UNKNOWN,
293f5f65 1108 CPU_LWP_FLAGS, 0 },
8a2c8fef 1109 { STRING_COMMA_LEN (".movbe"), PROCESSOR_UNKNOWN,
293f5f65 1110 CPU_MOVBE_FLAGS, 0 },
60aa667e 1111 { STRING_COMMA_LEN (".cx16"), PROCESSOR_UNKNOWN,
293f5f65 1112 CPU_CX16_FLAGS, 0 },
8a2c8fef 1113 { STRING_COMMA_LEN (".ept"), PROCESSOR_UNKNOWN,
293f5f65 1114 CPU_EPT_FLAGS, 0 },
6c30d220 1115 { STRING_COMMA_LEN (".lzcnt"), PROCESSOR_UNKNOWN,
293f5f65 1116 CPU_LZCNT_FLAGS, 0 },
272a84b1
L
1117 { STRING_COMMA_LEN (".popcnt"), PROCESSOR_UNKNOWN,
1118 CPU_POPCNT_FLAGS, 0 },
42164a71 1119 { STRING_COMMA_LEN (".hle"), PROCESSOR_UNKNOWN,
293f5f65 1120 CPU_HLE_FLAGS, 0 },
42164a71 1121 { STRING_COMMA_LEN (".rtm"), PROCESSOR_UNKNOWN,
293f5f65 1122 CPU_RTM_FLAGS, 0 },
6c30d220 1123 { STRING_COMMA_LEN (".invpcid"), PROCESSOR_UNKNOWN,
293f5f65 1124 CPU_INVPCID_FLAGS, 0 },
8a2c8fef 1125 { STRING_COMMA_LEN (".clflush"), PROCESSOR_UNKNOWN,
293f5f65 1126 CPU_CLFLUSH_FLAGS, 0 },
22109423 1127 { STRING_COMMA_LEN (".nop"), PROCESSOR_UNKNOWN,
293f5f65 1128 CPU_NOP_FLAGS, 0 },
8a2c8fef 1129 { STRING_COMMA_LEN (".syscall"), PROCESSOR_UNKNOWN,
293f5f65 1130 CPU_SYSCALL_FLAGS, 0 },
8a2c8fef 1131 { STRING_COMMA_LEN (".rdtscp"), PROCESSOR_UNKNOWN,
293f5f65 1132 CPU_RDTSCP_FLAGS, 0 },
8a2c8fef 1133 { STRING_COMMA_LEN (".3dnow"), PROCESSOR_UNKNOWN,
293f5f65 1134 CPU_3DNOW_FLAGS, 0 },
8a2c8fef 1135 { STRING_COMMA_LEN (".3dnowa"), PROCESSOR_UNKNOWN,
293f5f65 1136 CPU_3DNOWA_FLAGS, 0 },
8a2c8fef 1137 { STRING_COMMA_LEN (".padlock"), PROCESSOR_UNKNOWN,
293f5f65 1138 CPU_PADLOCK_FLAGS, 0 },
8a2c8fef 1139 { STRING_COMMA_LEN (".pacifica"), PROCESSOR_UNKNOWN,
293f5f65 1140 CPU_SVME_FLAGS, 1 },
8a2c8fef 1141 { STRING_COMMA_LEN (".svme"), PROCESSOR_UNKNOWN,
293f5f65 1142 CPU_SVME_FLAGS, 0 },
8a2c8fef 1143 { STRING_COMMA_LEN (".sse4a"), PROCESSOR_UNKNOWN,
293f5f65 1144 CPU_SSE4A_FLAGS, 0 },
8a2c8fef 1145 { STRING_COMMA_LEN (".abm"), PROCESSOR_UNKNOWN,
293f5f65 1146 CPU_ABM_FLAGS, 0 },
87973e9f 1147 { STRING_COMMA_LEN (".bmi"), PROCESSOR_UNKNOWN,
293f5f65 1148 CPU_BMI_FLAGS, 0 },
2a2a0f38 1149 { STRING_COMMA_LEN (".tbm"), PROCESSOR_UNKNOWN,
293f5f65 1150 CPU_TBM_FLAGS, 0 },
e2e1fcde 1151 { STRING_COMMA_LEN (".adx"), PROCESSOR_UNKNOWN,
293f5f65 1152 CPU_ADX_FLAGS, 0 },
e2e1fcde 1153 { STRING_COMMA_LEN (".rdseed"), PROCESSOR_UNKNOWN,
293f5f65 1154 CPU_RDSEED_FLAGS, 0 },
e2e1fcde 1155 { STRING_COMMA_LEN (".prfchw"), PROCESSOR_UNKNOWN,
293f5f65 1156 CPU_PRFCHW_FLAGS, 0 },
5c111e37 1157 { STRING_COMMA_LEN (".smap"), PROCESSOR_UNKNOWN,
293f5f65 1158 CPU_SMAP_FLAGS, 0 },
7e8b059b 1159 { STRING_COMMA_LEN (".mpx"), PROCESSOR_UNKNOWN,
293f5f65 1160 CPU_MPX_FLAGS, 0 },
a0046408 1161 { STRING_COMMA_LEN (".sha"), PROCESSOR_UNKNOWN,
293f5f65 1162 CPU_SHA_FLAGS, 0 },
963f3586 1163 { STRING_COMMA_LEN (".clflushopt"), PROCESSOR_UNKNOWN,
293f5f65 1164 CPU_CLFLUSHOPT_FLAGS, 0 },
dcf893b5 1165 { STRING_COMMA_LEN (".prefetchwt1"), PROCESSOR_UNKNOWN,
293f5f65 1166 CPU_PREFETCHWT1_FLAGS, 0 },
2cf200a4 1167 { STRING_COMMA_LEN (".se1"), PROCESSOR_UNKNOWN,
293f5f65 1168 CPU_SE1_FLAGS, 0 },
c5e7287a 1169 { STRING_COMMA_LEN (".clwb"), PROCESSOR_UNKNOWN,
293f5f65 1170 CPU_CLWB_FLAGS, 0 },
2cc1b5aa 1171 { STRING_COMMA_LEN (".avx512ifma"), PROCESSOR_UNKNOWN,
293f5f65 1172 CPU_AVX512IFMA_FLAGS, 0 },
14f195c9 1173 { STRING_COMMA_LEN (".avx512vbmi"), PROCESSOR_UNKNOWN,
293f5f65 1174 CPU_AVX512VBMI_FLAGS, 0 },
920d2ddc
IT
1175 { STRING_COMMA_LEN (".avx512_4fmaps"), PROCESSOR_UNKNOWN,
1176 CPU_AVX512_4FMAPS_FLAGS, 0 },
47acf0bd
IT
1177 { STRING_COMMA_LEN (".avx512_4vnniw"), PROCESSOR_UNKNOWN,
1178 CPU_AVX512_4VNNIW_FLAGS, 0 },
620214f7
IT
1179 { STRING_COMMA_LEN (".avx512_vpopcntdq"), PROCESSOR_UNKNOWN,
1180 CPU_AVX512_VPOPCNTDQ_FLAGS, 0 },
53467f57
IT
1181 { STRING_COMMA_LEN (".avx512_vbmi2"), PROCESSOR_UNKNOWN,
1182 CPU_AVX512_VBMI2_FLAGS, 0 },
8cfcb765
IT
1183 { STRING_COMMA_LEN (".avx512_vnni"), PROCESSOR_UNKNOWN,
1184 CPU_AVX512_VNNI_FLAGS, 0 },
ee6872be
IT
1185 { STRING_COMMA_LEN (".avx512_bitalg"), PROCESSOR_UNKNOWN,
1186 CPU_AVX512_BITALG_FLAGS, 0 },
58bf9b6a
L
1187 { STRING_COMMA_LEN (".avx_vnni"), PROCESSOR_UNKNOWN,
1188 CPU_AVX_VNNI_FLAGS, 0 },
029f3522 1189 { STRING_COMMA_LEN (".clzero"), PROCESSOR_UNKNOWN,
293f5f65 1190 CPU_CLZERO_FLAGS, 0 },
9916071f 1191 { STRING_COMMA_LEN (".mwaitx"), PROCESSOR_UNKNOWN,
293f5f65 1192 CPU_MWAITX_FLAGS, 0 },
8eab4136 1193 { STRING_COMMA_LEN (".ospke"), PROCESSOR_UNKNOWN,
293f5f65 1194 CPU_OSPKE_FLAGS, 0 },
8bc52696 1195 { STRING_COMMA_LEN (".rdpid"), PROCESSOR_UNKNOWN,
293f5f65 1196 CPU_RDPID_FLAGS, 0 },
6b40c462
L
1197 { STRING_COMMA_LEN (".ptwrite"), PROCESSOR_UNKNOWN,
1198 CPU_PTWRITE_FLAGS, 0 },
d777820b
IT
1199 { STRING_COMMA_LEN (".ibt"), PROCESSOR_UNKNOWN,
1200 CPU_IBT_FLAGS, 0 },
1201 { STRING_COMMA_LEN (".shstk"), PROCESSOR_UNKNOWN,
1202 CPU_SHSTK_FLAGS, 0 },
48521003
IT
1203 { STRING_COMMA_LEN (".gfni"), PROCESSOR_UNKNOWN,
1204 CPU_GFNI_FLAGS, 0 },
8dcf1fad
IT
1205 { STRING_COMMA_LEN (".vaes"), PROCESSOR_UNKNOWN,
1206 CPU_VAES_FLAGS, 0 },
ff1982d5
IT
1207 { STRING_COMMA_LEN (".vpclmulqdq"), PROCESSOR_UNKNOWN,
1208 CPU_VPCLMULQDQ_FLAGS, 0 },
3233d7d0
IT
1209 { STRING_COMMA_LEN (".wbnoinvd"), PROCESSOR_UNKNOWN,
1210 CPU_WBNOINVD_FLAGS, 0 },
be3a8dca
IT
1211 { STRING_COMMA_LEN (".pconfig"), PROCESSOR_UNKNOWN,
1212 CPU_PCONFIG_FLAGS, 0 },
de89d0a3
IT
1213 { STRING_COMMA_LEN (".waitpkg"), PROCESSOR_UNKNOWN,
1214 CPU_WAITPKG_FLAGS, 0 },
c48935d7
IT
1215 { STRING_COMMA_LEN (".cldemote"), PROCESSOR_UNKNOWN,
1216 CPU_CLDEMOTE_FLAGS, 0 },
260cd341
LC
1217 { STRING_COMMA_LEN (".amx_int8"), PROCESSOR_UNKNOWN,
1218 CPU_AMX_INT8_FLAGS, 0 },
1219 { STRING_COMMA_LEN (".amx_bf16"), PROCESSOR_UNKNOWN,
1220 CPU_AMX_BF16_FLAGS, 0 },
1221 { STRING_COMMA_LEN (".amx_tile"), PROCESSOR_UNKNOWN,
1222 CPU_AMX_TILE_FLAGS, 0 },
c0a30a9f
L
1223 { STRING_COMMA_LEN (".movdiri"), PROCESSOR_UNKNOWN,
1224 CPU_MOVDIRI_FLAGS, 0 },
1225 { STRING_COMMA_LEN (".movdir64b"), PROCESSOR_UNKNOWN,
1226 CPU_MOVDIR64B_FLAGS, 0 },
d6aab7a1
XG
1227 { STRING_COMMA_LEN (".avx512_bf16"), PROCESSOR_UNKNOWN,
1228 CPU_AVX512_BF16_FLAGS, 0 },
9186c494
L
1229 { STRING_COMMA_LEN (".avx512_vp2intersect"), PROCESSOR_UNKNOWN,
1230 CPU_AVX512_VP2INTERSECT_FLAGS, 0 },
81d54bb7
CL
1231 { STRING_COMMA_LEN (".tdx"), PROCESSOR_UNKNOWN,
1232 CPU_TDX_FLAGS, 0 },
dd455cf5
L
1233 { STRING_COMMA_LEN (".enqcmd"), PROCESSOR_UNKNOWN,
1234 CPU_ENQCMD_FLAGS, 0 },
4b27d27c
L
1235 { STRING_COMMA_LEN (".serialize"), PROCESSOR_UNKNOWN,
1236 CPU_SERIALIZE_FLAGS, 0 },
142861df
JB
1237 { STRING_COMMA_LEN (".rdpru"), PROCESSOR_UNKNOWN,
1238 CPU_RDPRU_FLAGS, 0 },
1239 { STRING_COMMA_LEN (".mcommit"), PROCESSOR_UNKNOWN,
1240 CPU_MCOMMIT_FLAGS, 0 },
a847e322
JB
1241 { STRING_COMMA_LEN (".sev_es"), PROCESSOR_UNKNOWN,
1242 CPU_SEV_ES_FLAGS, 0 },
bb651e8b
CL
1243 { STRING_COMMA_LEN (".tsxldtrk"), PROCESSOR_UNKNOWN,
1244 CPU_TSXLDTRK_FLAGS, 0 },
c4694f17
TG
1245 { STRING_COMMA_LEN (".kl"), PROCESSOR_UNKNOWN,
1246 CPU_KL_FLAGS, 0 },
1247 { STRING_COMMA_LEN (".widekl"), PROCESSOR_UNKNOWN,
1248 CPU_WIDEKL_FLAGS, 0 },
f64c42a9
LC
1249 { STRING_COMMA_LEN (".uintr"), PROCESSOR_UNKNOWN,
1250 CPU_UINTR_FLAGS, 0 },
c1fa250a
LC
1251 { STRING_COMMA_LEN (".hreset"), PROCESSOR_UNKNOWN,
1252 CPU_HRESET_FLAGS, 0 },
293f5f65
L
1253};
1254
1255static const noarch_entry cpu_noarch[] =
1256{
1257 { STRING_COMMA_LEN ("no87"), CPU_ANY_X87_FLAGS },
1848e567
L
1258 { STRING_COMMA_LEN ("no287"), CPU_ANY_287_FLAGS },
1259 { STRING_COMMA_LEN ("no387"), CPU_ANY_387_FLAGS },
1260 { STRING_COMMA_LEN ("no687"), CPU_ANY_687_FLAGS },
d871f3f4
L
1261 { STRING_COMMA_LEN ("nocmov"), CPU_ANY_CMOV_FLAGS },
1262 { STRING_COMMA_LEN ("nofxsr"), CPU_ANY_FXSR_FLAGS },
293f5f65
L
1263 { STRING_COMMA_LEN ("nommx"), CPU_ANY_MMX_FLAGS },
1264 { STRING_COMMA_LEN ("nosse"), CPU_ANY_SSE_FLAGS },
1848e567
L
1265 { STRING_COMMA_LEN ("nosse2"), CPU_ANY_SSE2_FLAGS },
1266 { STRING_COMMA_LEN ("nosse3"), CPU_ANY_SSE3_FLAGS },
af5c13b0 1267 { STRING_COMMA_LEN ("nosse4a"), CPU_ANY_SSE4A_FLAGS },
1848e567
L
1268 { STRING_COMMA_LEN ("nossse3"), CPU_ANY_SSSE3_FLAGS },
1269 { STRING_COMMA_LEN ("nosse4.1"), CPU_ANY_SSE4_1_FLAGS },
1270 { STRING_COMMA_LEN ("nosse4.2"), CPU_ANY_SSE4_2_FLAGS },
af5c13b0 1271 { STRING_COMMA_LEN ("nosse4"), CPU_ANY_SSE4_1_FLAGS },
293f5f65 1272 { STRING_COMMA_LEN ("noavx"), CPU_ANY_AVX_FLAGS },
1848e567 1273 { STRING_COMMA_LEN ("noavx2"), CPU_ANY_AVX2_FLAGS },
144b71e2
L
1274 { STRING_COMMA_LEN ("noavx512f"), CPU_ANY_AVX512F_FLAGS },
1275 { STRING_COMMA_LEN ("noavx512cd"), CPU_ANY_AVX512CD_FLAGS },
1276 { STRING_COMMA_LEN ("noavx512er"), CPU_ANY_AVX512ER_FLAGS },
1277 { STRING_COMMA_LEN ("noavx512pf"), CPU_ANY_AVX512PF_FLAGS },
1278 { STRING_COMMA_LEN ("noavx512dq"), CPU_ANY_AVX512DQ_FLAGS },
1279 { STRING_COMMA_LEN ("noavx512bw"), CPU_ANY_AVX512BW_FLAGS },
1280 { STRING_COMMA_LEN ("noavx512vl"), CPU_ANY_AVX512VL_FLAGS },
1281 { STRING_COMMA_LEN ("noavx512ifma"), CPU_ANY_AVX512IFMA_FLAGS },
1282 { STRING_COMMA_LEN ("noavx512vbmi"), CPU_ANY_AVX512VBMI_FLAGS },
920d2ddc 1283 { STRING_COMMA_LEN ("noavx512_4fmaps"), CPU_ANY_AVX512_4FMAPS_FLAGS },
47acf0bd 1284 { STRING_COMMA_LEN ("noavx512_4vnniw"), CPU_ANY_AVX512_4VNNIW_FLAGS },
620214f7 1285 { STRING_COMMA_LEN ("noavx512_vpopcntdq"), CPU_ANY_AVX512_VPOPCNTDQ_FLAGS },
53467f57 1286 { STRING_COMMA_LEN ("noavx512_vbmi2"), CPU_ANY_AVX512_VBMI2_FLAGS },
8cfcb765 1287 { STRING_COMMA_LEN ("noavx512_vnni"), CPU_ANY_AVX512_VNNI_FLAGS },
ee6872be 1288 { STRING_COMMA_LEN ("noavx512_bitalg"), CPU_ANY_AVX512_BITALG_FLAGS },
58bf9b6a 1289 { STRING_COMMA_LEN ("noavx_vnni"), CPU_ANY_AVX_VNNI_FLAGS },
d777820b
IT
1290 { STRING_COMMA_LEN ("noibt"), CPU_ANY_IBT_FLAGS },
1291 { STRING_COMMA_LEN ("noshstk"), CPU_ANY_SHSTK_FLAGS },
260cd341
LC
1292 { STRING_COMMA_LEN ("noamx_int8"), CPU_ANY_AMX_INT8_FLAGS },
1293 { STRING_COMMA_LEN ("noamx_bf16"), CPU_ANY_AMX_BF16_FLAGS },
1294 { STRING_COMMA_LEN ("noamx_tile"), CPU_ANY_AMX_TILE_FLAGS },
c0a30a9f
L
1295 { STRING_COMMA_LEN ("nomovdiri"), CPU_ANY_MOVDIRI_FLAGS },
1296 { STRING_COMMA_LEN ("nomovdir64b"), CPU_ANY_MOVDIR64B_FLAGS },
d6aab7a1 1297 { STRING_COMMA_LEN ("noavx512_bf16"), CPU_ANY_AVX512_BF16_FLAGS },
708a2fff
CL
1298 { STRING_COMMA_LEN ("noavx512_vp2intersect"),
1299 CPU_ANY_AVX512_VP2INTERSECT_FLAGS },
81d54bb7 1300 { STRING_COMMA_LEN ("notdx"), CPU_ANY_TDX_FLAGS },
dd455cf5 1301 { STRING_COMMA_LEN ("noenqcmd"), CPU_ANY_ENQCMD_FLAGS },
4b27d27c 1302 { STRING_COMMA_LEN ("noserialize"), CPU_ANY_SERIALIZE_FLAGS },
bb651e8b 1303 { STRING_COMMA_LEN ("notsxldtrk"), CPU_ANY_TSXLDTRK_FLAGS },
c4694f17
TG
1304 { STRING_COMMA_LEN ("nokl"), CPU_ANY_KL_FLAGS },
1305 { STRING_COMMA_LEN ("nowidekl"), CPU_ANY_WIDEKL_FLAGS },
f64c42a9 1306 { STRING_COMMA_LEN ("nouintr"), CPU_ANY_UINTR_FLAGS },
c1fa250a 1307 { STRING_COMMA_LEN ("nohreset"), CPU_ANY_HRESET_FLAGS },
e413e4e9
AM
1308};
1309
704209c0 1310#ifdef I386COFF
a6c24e68
NC
1311/* Like s_lcomm_internal in gas/read.c but the alignment string
1312 is allowed to be optional. */
1313
1314static symbolS *
1315pe_lcomm_internal (int needs_align, symbolS *symbolP, addressT size)
1316{
1317 addressT align = 0;
1318
1319 SKIP_WHITESPACE ();
1320
7ab9ffdd 1321 if (needs_align
a6c24e68
NC
1322 && *input_line_pointer == ',')
1323 {
1324 align = parse_align (needs_align - 1);
7ab9ffdd 1325
a6c24e68
NC
1326 if (align == (addressT) -1)
1327 return NULL;
1328 }
1329 else
1330 {
1331 if (size >= 8)
1332 align = 3;
1333 else if (size >= 4)
1334 align = 2;
1335 else if (size >= 2)
1336 align = 1;
1337 else
1338 align = 0;
1339 }
1340
1341 bss_alloc (symbolP, size, align);
1342 return symbolP;
1343}
1344
704209c0 1345static void
a6c24e68
NC
1346pe_lcomm (int needs_align)
1347{
1348 s_comm_internal (needs_align * 2, pe_lcomm_internal);
1349}
704209c0 1350#endif
a6c24e68 1351
29b0f896
AM
1352const pseudo_typeS md_pseudo_table[] =
1353{
1354#if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
1355 {"align", s_align_bytes, 0},
1356#else
1357 {"align", s_align_ptwo, 0},
1358#endif
1359 {"arch", set_cpu_arch, 0},
1360#ifndef I386COFF
1361 {"bss", s_bss, 0},
a6c24e68
NC
1362#else
1363 {"lcomm", pe_lcomm, 1},
29b0f896
AM
1364#endif
1365 {"ffloat", float_cons, 'f'},
1366 {"dfloat", float_cons, 'd'},
1367 {"tfloat", float_cons, 'x'},
1368 {"value", cons, 2},
d182319b 1369 {"slong", signed_cons, 4},
29b0f896
AM
1370 {"noopt", s_ignore, 0},
1371 {"optim", s_ignore, 0},
1372 {"code16gcc", set_16bit_gcc_code_flag, CODE_16BIT},
1373 {"code16", set_code_flag, CODE_16BIT},
1374 {"code32", set_code_flag, CODE_32BIT},
da5f19a2 1375#ifdef BFD64
29b0f896 1376 {"code64", set_code_flag, CODE_64BIT},
da5f19a2 1377#endif
29b0f896
AM
1378 {"intel_syntax", set_intel_syntax, 1},
1379 {"att_syntax", set_intel_syntax, 0},
1efbbeb4
L
1380 {"intel_mnemonic", set_intel_mnemonic, 1},
1381 {"att_mnemonic", set_intel_mnemonic, 0},
db51cc60
L
1382 {"allow_index_reg", set_allow_index_reg, 1},
1383 {"disallow_index_reg", set_allow_index_reg, 0},
7bab8ab5
JB
1384 {"sse_check", set_check, 0},
1385 {"operand_check", set_check, 1},
3b22753a
L
1386#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1387 {"largecomm", handle_large_common, 0},
07a53e5c 1388#else
68d20676 1389 {"file", dwarf2_directive_file, 0},
07a53e5c
RH
1390 {"loc", dwarf2_directive_loc, 0},
1391 {"loc_mark_labels", dwarf2_directive_loc_mark_labels, 0},
3b22753a 1392#endif
6482c264
NC
1393#ifdef TE_PE
1394 {"secrel32", pe_directive_secrel, 0},
1395#endif
29b0f896
AM
1396 {0, 0, 0}
1397};
1398
1399/* For interface with expression (). */
1400extern char *input_line_pointer;
1401
1402/* Hash table for instruction mnemonic lookup. */
629310ab 1403static htab_t op_hash;
29b0f896
AM
1404
1405/* Hash table for register lookup. */
629310ab 1406static htab_t reg_hash;
29b0f896 1407\f
ce8a8b2f
AM
1408 /* Various efficient no-op patterns for aligning code labels.
1409 Note: Don't try to assemble the instructions in the comments.
1410 0L and 0w are not legal. */
62a02d25
L
1411static const unsigned char f32_1[] =
1412 {0x90}; /* nop */
1413static const unsigned char f32_2[] =
1414 {0x66,0x90}; /* xchg %ax,%ax */
1415static const unsigned char f32_3[] =
1416 {0x8d,0x76,0x00}; /* leal 0(%esi),%esi */
1417static const unsigned char f32_4[] =
1418 {0x8d,0x74,0x26,0x00}; /* leal 0(%esi,1),%esi */
62a02d25
L
1419static const unsigned char f32_6[] =
1420 {0x8d,0xb6,0x00,0x00,0x00,0x00}; /* leal 0L(%esi),%esi */
1421static const unsigned char f32_7[] =
1422 {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal 0L(%esi,1),%esi */
62a02d25 1423static const unsigned char f16_3[] =
3ae729d5 1424 {0x8d,0x74,0x00}; /* lea 0(%si),%si */
62a02d25 1425static const unsigned char f16_4[] =
3ae729d5
L
1426 {0x8d,0xb4,0x00,0x00}; /* lea 0W(%si),%si */
1427static const unsigned char jump_disp8[] =
1428 {0xeb}; /* jmp disp8 */
1429static const unsigned char jump32_disp32[] =
1430 {0xe9}; /* jmp disp32 */
1431static const unsigned char jump16_disp32[] =
1432 {0x66,0xe9}; /* jmp disp32 */
62a02d25
L
1433/* 32-bit NOPs patterns. */
1434static const unsigned char *const f32_patt[] = {
3ae729d5 1435 f32_1, f32_2, f32_3, f32_4, NULL, f32_6, f32_7
62a02d25
L
1436};
1437/* 16-bit NOPs patterns. */
1438static const unsigned char *const f16_patt[] = {
3ae729d5 1439 f32_1, f32_2, f16_3, f16_4
62a02d25
L
1440};
1441/* nopl (%[re]ax) */
1442static const unsigned char alt_3[] =
1443 {0x0f,0x1f,0x00};
1444/* nopl 0(%[re]ax) */
1445static const unsigned char alt_4[] =
1446 {0x0f,0x1f,0x40,0x00};
1447/* nopl 0(%[re]ax,%[re]ax,1) */
1448static const unsigned char alt_5[] =
1449 {0x0f,0x1f,0x44,0x00,0x00};
1450/* nopw 0(%[re]ax,%[re]ax,1) */
1451static const unsigned char alt_6[] =
1452 {0x66,0x0f,0x1f,0x44,0x00,0x00};
1453/* nopl 0L(%[re]ax) */
1454static const unsigned char alt_7[] =
1455 {0x0f,0x1f,0x80,0x00,0x00,0x00,0x00};
1456/* nopl 0L(%[re]ax,%[re]ax,1) */
1457static const unsigned char alt_8[] =
1458 {0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1459/* nopw 0L(%[re]ax,%[re]ax,1) */
1460static const unsigned char alt_9[] =
1461 {0x66,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1462/* nopw %cs:0L(%[re]ax,%[re]ax,1) */
1463static const unsigned char alt_10[] =
1464 {0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
3ae729d5
L
1465/* data16 nopw %cs:0L(%eax,%eax,1) */
1466static const unsigned char alt_11[] =
1467 {0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
62a02d25
L
1468/* 32-bit and 64-bit NOPs patterns. */
1469static const unsigned char *const alt_patt[] = {
1470 f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
3ae729d5 1471 alt_9, alt_10, alt_11
62a02d25
L
1472};
1473
1474/* Genenerate COUNT bytes of NOPs to WHERE from PATT with the maximum
1475 size of a single NOP instruction MAX_SINGLE_NOP_SIZE. */
1476
1477static void
1478i386_output_nops (char *where, const unsigned char *const *patt,
1479 int count, int max_single_nop_size)
1480
1481{
3ae729d5
L
1482 /* Place the longer NOP first. */
1483 int last;
1484 int offset;
3076e594
NC
1485 const unsigned char *nops;
1486
1487 if (max_single_nop_size < 1)
1488 {
1489 as_fatal (_("i386_output_nops called to generate nops of at most %d bytes!"),
1490 max_single_nop_size);
1491 return;
1492 }
1493
1494 nops = patt[max_single_nop_size - 1];
3ae729d5
L
1495
1496 /* Use the smaller one if the requsted one isn't available. */
1497 if (nops == NULL)
62a02d25 1498 {
3ae729d5
L
1499 max_single_nop_size--;
1500 nops = patt[max_single_nop_size - 1];
62a02d25
L
1501 }
1502
3ae729d5
L
1503 last = count % max_single_nop_size;
1504
1505 count -= last;
1506 for (offset = 0; offset < count; offset += max_single_nop_size)
1507 memcpy (where + offset, nops, max_single_nop_size);
1508
1509 if (last)
1510 {
1511 nops = patt[last - 1];
1512 if (nops == NULL)
1513 {
1514 /* Use the smaller one plus one-byte NOP if the needed one
1515 isn't available. */
1516 last--;
1517 nops = patt[last - 1];
1518 memcpy (where + offset, nops, last);
1519 where[offset + last] = *patt[0];
1520 }
1521 else
1522 memcpy (where + offset, nops, last);
1523 }
62a02d25
L
1524}
1525
3ae729d5
L
1526static INLINE int
1527fits_in_imm7 (offsetT num)
1528{
1529 return (num & 0x7f) == num;
1530}
1531
1532static INLINE int
1533fits_in_imm31 (offsetT num)
1534{
1535 return (num & 0x7fffffff) == num;
1536}
62a02d25
L
1537
1538/* Genenerate COUNT bytes of NOPs to WHERE with the maximum size of a
1539 single NOP instruction LIMIT. */
1540
1541void
3ae729d5 1542i386_generate_nops (fragS *fragP, char *where, offsetT count, int limit)
62a02d25 1543{
3ae729d5 1544 const unsigned char *const *patt = NULL;
62a02d25 1545 int max_single_nop_size;
3ae729d5
L
1546 /* Maximum number of NOPs before switching to jump over NOPs. */
1547 int max_number_of_nops;
62a02d25 1548
3ae729d5 1549 switch (fragP->fr_type)
62a02d25 1550 {
3ae729d5
L
1551 case rs_fill_nop:
1552 case rs_align_code:
1553 break;
e379e5f3
L
1554 case rs_machine_dependent:
1555 /* Allow NOP padding for jumps and calls. */
1556 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
1557 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
1558 break;
1559 /* Fall through. */
3ae729d5 1560 default:
62a02d25
L
1561 return;
1562 }
1563
ccc9c027
L
1564 /* We need to decide which NOP sequence to use for 32bit and
1565 64bit. When -mtune= is used:
4eed87de 1566
76bc74dc
L
1567 1. For PROCESSOR_I386, PROCESSOR_I486, PROCESSOR_PENTIUM and
1568 PROCESSOR_GENERIC32, f32_patt will be used.
80b8656c
L
1569 2. For the rest, alt_patt will be used.
1570
1571 When -mtune= isn't used, alt_patt will be used if
22109423 1572 cpu_arch_isa_flags has CpuNop. Otherwise, f32_patt will
76bc74dc 1573 be used.
ccc9c027
L
1574
1575 When -march= or .arch is used, we can't use anything beyond
1576 cpu_arch_isa_flags. */
1577
1578 if (flag_code == CODE_16BIT)
1579 {
3ae729d5
L
1580 patt = f16_patt;
1581 max_single_nop_size = sizeof (f16_patt) / sizeof (f16_patt[0]);
1582 /* Limit number of NOPs to 2 in 16-bit mode. */
1583 max_number_of_nops = 2;
252b5132 1584 }
33fef721 1585 else
ccc9c027 1586 {
fbf3f584 1587 if (fragP->tc_frag_data.isa == PROCESSOR_UNKNOWN)
ccc9c027
L
1588 {
1589 /* PROCESSOR_UNKNOWN means that all ISAs may be used. */
1590 switch (cpu_arch_tune)
1591 {
1592 case PROCESSOR_UNKNOWN:
1593 /* We use cpu_arch_isa_flags to check if we SHOULD
22109423
L
1594 optimize with nops. */
1595 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1596 patt = alt_patt;
ccc9c027
L
1597 else
1598 patt = f32_patt;
1599 break;
ccc9c027
L
1600 case PROCESSOR_PENTIUM4:
1601 case PROCESSOR_NOCONA:
ef05d495 1602 case PROCESSOR_CORE:
76bc74dc 1603 case PROCESSOR_CORE2:
bd5295b2 1604 case PROCESSOR_COREI7:
3632d14b 1605 case PROCESSOR_L1OM:
7a9068fe 1606 case PROCESSOR_K1OM:
76bc74dc 1607 case PROCESSOR_GENERIC64:
ccc9c027
L
1608 case PROCESSOR_K6:
1609 case PROCESSOR_ATHLON:
1610 case PROCESSOR_K8:
4eed87de 1611 case PROCESSOR_AMDFAM10:
8aedb9fe 1612 case PROCESSOR_BD:
029f3522 1613 case PROCESSOR_ZNVER:
7b458c12 1614 case PROCESSOR_BT:
80b8656c 1615 patt = alt_patt;
ccc9c027 1616 break;
76bc74dc 1617 case PROCESSOR_I386:
ccc9c027
L
1618 case PROCESSOR_I486:
1619 case PROCESSOR_PENTIUM:
2dde1948 1620 case PROCESSOR_PENTIUMPRO:
81486035 1621 case PROCESSOR_IAMCU:
ccc9c027
L
1622 case PROCESSOR_GENERIC32:
1623 patt = f32_patt;
1624 break;
4eed87de 1625 }
ccc9c027
L
1626 }
1627 else
1628 {
fbf3f584 1629 switch (fragP->tc_frag_data.tune)
ccc9c027
L
1630 {
1631 case PROCESSOR_UNKNOWN:
e6a14101 1632 /* When cpu_arch_isa is set, cpu_arch_tune shouldn't be
ccc9c027
L
1633 PROCESSOR_UNKNOWN. */
1634 abort ();
1635 break;
1636
76bc74dc 1637 case PROCESSOR_I386:
ccc9c027
L
1638 case PROCESSOR_I486:
1639 case PROCESSOR_PENTIUM:
81486035 1640 case PROCESSOR_IAMCU:
ccc9c027
L
1641 case PROCESSOR_K6:
1642 case PROCESSOR_ATHLON:
1643 case PROCESSOR_K8:
4eed87de 1644 case PROCESSOR_AMDFAM10:
8aedb9fe 1645 case PROCESSOR_BD:
029f3522 1646 case PROCESSOR_ZNVER:
7b458c12 1647 case PROCESSOR_BT:
ccc9c027
L
1648 case PROCESSOR_GENERIC32:
1649 /* We use cpu_arch_isa_flags to check if we CAN optimize
22109423
L
1650 with nops. */
1651 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1652 patt = alt_patt;
ccc9c027
L
1653 else
1654 patt = f32_patt;
1655 break;
76bc74dc
L
1656 case PROCESSOR_PENTIUMPRO:
1657 case PROCESSOR_PENTIUM4:
1658 case PROCESSOR_NOCONA:
1659 case PROCESSOR_CORE:
ef05d495 1660 case PROCESSOR_CORE2:
bd5295b2 1661 case PROCESSOR_COREI7:
3632d14b 1662 case PROCESSOR_L1OM:
7a9068fe 1663 case PROCESSOR_K1OM:
22109423 1664 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1665 patt = alt_patt;
ccc9c027
L
1666 else
1667 patt = f32_patt;
1668 break;
1669 case PROCESSOR_GENERIC64:
80b8656c 1670 patt = alt_patt;
ccc9c027 1671 break;
4eed87de 1672 }
ccc9c027
L
1673 }
1674
76bc74dc
L
1675 if (patt == f32_patt)
1676 {
3ae729d5
L
1677 max_single_nop_size = sizeof (f32_patt) / sizeof (f32_patt[0]);
1678 /* Limit number of NOPs to 2 for older processors. */
1679 max_number_of_nops = 2;
76bc74dc
L
1680 }
1681 else
1682 {
3ae729d5
L
1683 max_single_nop_size = sizeof (alt_patt) / sizeof (alt_patt[0]);
1684 /* Limit number of NOPs to 7 for newer processors. */
1685 max_number_of_nops = 7;
1686 }
1687 }
1688
1689 if (limit == 0)
1690 limit = max_single_nop_size;
1691
1692 if (fragP->fr_type == rs_fill_nop)
1693 {
1694 /* Output NOPs for .nop directive. */
1695 if (limit > max_single_nop_size)
1696 {
1697 as_bad_where (fragP->fr_file, fragP->fr_line,
1698 _("invalid single nop size: %d "
1699 "(expect within [0, %d])"),
1700 limit, max_single_nop_size);
1701 return;
1702 }
1703 }
e379e5f3 1704 else if (fragP->fr_type != rs_machine_dependent)
3ae729d5
L
1705 fragP->fr_var = count;
1706
1707 if ((count / max_single_nop_size) > max_number_of_nops)
1708 {
1709 /* Generate jump over NOPs. */
1710 offsetT disp = count - 2;
1711 if (fits_in_imm7 (disp))
1712 {
1713 /* Use "jmp disp8" if possible. */
1714 count = disp;
1715 where[0] = jump_disp8[0];
1716 where[1] = count;
1717 where += 2;
1718 }
1719 else
1720 {
1721 unsigned int size_of_jump;
1722
1723 if (flag_code == CODE_16BIT)
1724 {
1725 where[0] = jump16_disp32[0];
1726 where[1] = jump16_disp32[1];
1727 size_of_jump = 2;
1728 }
1729 else
1730 {
1731 where[0] = jump32_disp32[0];
1732 size_of_jump = 1;
1733 }
1734
1735 count -= size_of_jump + 4;
1736 if (!fits_in_imm31 (count))
1737 {
1738 as_bad_where (fragP->fr_file, fragP->fr_line,
1739 _("jump over nop padding out of range"));
1740 return;
1741 }
1742
1743 md_number_to_chars (where + size_of_jump, count, 4);
1744 where += size_of_jump + 4;
76bc74dc 1745 }
ccc9c027 1746 }
3ae729d5
L
1747
1748 /* Generate multiple NOPs. */
1749 i386_output_nops (where, patt, count, limit);
252b5132
RH
1750}
1751
c6fb90c8 1752static INLINE int
0dfbf9d7 1753operand_type_all_zero (const union i386_operand_type *x)
40fb9820 1754{
0dfbf9d7 1755 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1756 {
1757 case 3:
0dfbf9d7 1758 if (x->array[2])
c6fb90c8 1759 return 0;
1a0670f3 1760 /* Fall through. */
c6fb90c8 1761 case 2:
0dfbf9d7 1762 if (x->array[1])
c6fb90c8 1763 return 0;
1a0670f3 1764 /* Fall through. */
c6fb90c8 1765 case 1:
0dfbf9d7 1766 return !x->array[0];
c6fb90c8
L
1767 default:
1768 abort ();
1769 }
40fb9820
L
1770}
1771
c6fb90c8 1772static INLINE void
0dfbf9d7 1773operand_type_set (union i386_operand_type *x, unsigned int v)
40fb9820 1774{
0dfbf9d7 1775 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1776 {
1777 case 3:
0dfbf9d7 1778 x->array[2] = v;
1a0670f3 1779 /* Fall through. */
c6fb90c8 1780 case 2:
0dfbf9d7 1781 x->array[1] = v;
1a0670f3 1782 /* Fall through. */
c6fb90c8 1783 case 1:
0dfbf9d7 1784 x->array[0] = v;
1a0670f3 1785 /* Fall through. */
c6fb90c8
L
1786 break;
1787 default:
1788 abort ();
1789 }
bab6aec1
JB
1790
1791 x->bitfield.class = ClassNone;
75e5731b 1792 x->bitfield.instance = InstanceNone;
c6fb90c8 1793}
40fb9820 1794
c6fb90c8 1795static INLINE int
0dfbf9d7
L
1796operand_type_equal (const union i386_operand_type *x,
1797 const union i386_operand_type *y)
c6fb90c8 1798{
0dfbf9d7 1799 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1800 {
1801 case 3:
0dfbf9d7 1802 if (x->array[2] != y->array[2])
c6fb90c8 1803 return 0;
1a0670f3 1804 /* Fall through. */
c6fb90c8 1805 case 2:
0dfbf9d7 1806 if (x->array[1] != y->array[1])
c6fb90c8 1807 return 0;
1a0670f3 1808 /* Fall through. */
c6fb90c8 1809 case 1:
0dfbf9d7 1810 return x->array[0] == y->array[0];
c6fb90c8
L
1811 break;
1812 default:
1813 abort ();
1814 }
1815}
40fb9820 1816
0dfbf9d7
L
1817static INLINE int
1818cpu_flags_all_zero (const union i386_cpu_flags *x)
1819{
1820 switch (ARRAY_SIZE(x->array))
1821 {
53467f57
IT
1822 case 4:
1823 if (x->array[3])
1824 return 0;
1825 /* Fall through. */
0dfbf9d7
L
1826 case 3:
1827 if (x->array[2])
1828 return 0;
1a0670f3 1829 /* Fall through. */
0dfbf9d7
L
1830 case 2:
1831 if (x->array[1])
1832 return 0;
1a0670f3 1833 /* Fall through. */
0dfbf9d7
L
1834 case 1:
1835 return !x->array[0];
1836 default:
1837 abort ();
1838 }
1839}
1840
0dfbf9d7
L
1841static INLINE int
1842cpu_flags_equal (const union i386_cpu_flags *x,
1843 const union i386_cpu_flags *y)
1844{
1845 switch (ARRAY_SIZE(x->array))
1846 {
53467f57
IT
1847 case 4:
1848 if (x->array[3] != y->array[3])
1849 return 0;
1850 /* Fall through. */
0dfbf9d7
L
1851 case 3:
1852 if (x->array[2] != y->array[2])
1853 return 0;
1a0670f3 1854 /* Fall through. */
0dfbf9d7
L
1855 case 2:
1856 if (x->array[1] != y->array[1])
1857 return 0;
1a0670f3 1858 /* Fall through. */
0dfbf9d7
L
1859 case 1:
1860 return x->array[0] == y->array[0];
1861 break;
1862 default:
1863 abort ();
1864 }
1865}
c6fb90c8
L
1866
1867static INLINE int
1868cpu_flags_check_cpu64 (i386_cpu_flags f)
1869{
1870 return !((flag_code == CODE_64BIT && f.bitfield.cpuno64)
1871 || (flag_code != CODE_64BIT && f.bitfield.cpu64));
40fb9820
L
1872}
1873
c6fb90c8
L
1874static INLINE i386_cpu_flags
1875cpu_flags_and (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1876{
c6fb90c8
L
1877 switch (ARRAY_SIZE (x.array))
1878 {
53467f57
IT
1879 case 4:
1880 x.array [3] &= y.array [3];
1881 /* Fall through. */
c6fb90c8
L
1882 case 3:
1883 x.array [2] &= y.array [2];
1a0670f3 1884 /* Fall through. */
c6fb90c8
L
1885 case 2:
1886 x.array [1] &= y.array [1];
1a0670f3 1887 /* Fall through. */
c6fb90c8
L
1888 case 1:
1889 x.array [0] &= y.array [0];
1890 break;
1891 default:
1892 abort ();
1893 }
1894 return x;
1895}
40fb9820 1896
c6fb90c8
L
1897static INLINE i386_cpu_flags
1898cpu_flags_or (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1899{
c6fb90c8 1900 switch (ARRAY_SIZE (x.array))
40fb9820 1901 {
53467f57
IT
1902 case 4:
1903 x.array [3] |= y.array [3];
1904 /* Fall through. */
c6fb90c8
L
1905 case 3:
1906 x.array [2] |= y.array [2];
1a0670f3 1907 /* Fall through. */
c6fb90c8
L
1908 case 2:
1909 x.array [1] |= y.array [1];
1a0670f3 1910 /* Fall through. */
c6fb90c8
L
1911 case 1:
1912 x.array [0] |= y.array [0];
40fb9820
L
1913 break;
1914 default:
1915 abort ();
1916 }
40fb9820
L
1917 return x;
1918}
1919
309d3373
JB
1920static INLINE i386_cpu_flags
1921cpu_flags_and_not (i386_cpu_flags x, i386_cpu_flags y)
1922{
1923 switch (ARRAY_SIZE (x.array))
1924 {
53467f57
IT
1925 case 4:
1926 x.array [3] &= ~y.array [3];
1927 /* Fall through. */
309d3373
JB
1928 case 3:
1929 x.array [2] &= ~y.array [2];
1a0670f3 1930 /* Fall through. */
309d3373
JB
1931 case 2:
1932 x.array [1] &= ~y.array [1];
1a0670f3 1933 /* Fall through. */
309d3373
JB
1934 case 1:
1935 x.array [0] &= ~y.array [0];
1936 break;
1937 default:
1938 abort ();
1939 }
1940 return x;
1941}
1942
6c0946d0
JB
1943static const i386_cpu_flags avx512 = CPU_ANY_AVX512F_FLAGS;
1944
c0f3af97
L
1945#define CPU_FLAGS_ARCH_MATCH 0x1
1946#define CPU_FLAGS_64BIT_MATCH 0x2
1947
c0f3af97 1948#define CPU_FLAGS_PERFECT_MATCH \
db12e14e 1949 (CPU_FLAGS_ARCH_MATCH | CPU_FLAGS_64BIT_MATCH)
c0f3af97
L
1950
1951/* Return CPU flags match bits. */
3629bb00 1952
40fb9820 1953static int
d3ce72d0 1954cpu_flags_match (const insn_template *t)
40fb9820 1955{
c0f3af97
L
1956 i386_cpu_flags x = t->cpu_flags;
1957 int match = cpu_flags_check_cpu64 (x) ? CPU_FLAGS_64BIT_MATCH : 0;
40fb9820
L
1958
1959 x.bitfield.cpu64 = 0;
1960 x.bitfield.cpuno64 = 0;
1961
0dfbf9d7 1962 if (cpu_flags_all_zero (&x))
c0f3af97
L
1963 {
1964 /* This instruction is available on all archs. */
db12e14e 1965 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 1966 }
3629bb00
L
1967 else
1968 {
c0f3af97 1969 /* This instruction is available only on some archs. */
3629bb00
L
1970 i386_cpu_flags cpu = cpu_arch_flags;
1971
ab592e75
JB
1972 /* AVX512VL is no standalone feature - match it and then strip it. */
1973 if (x.bitfield.cpuavx512vl && !cpu.bitfield.cpuavx512vl)
1974 return match;
1975 x.bitfield.cpuavx512vl = 0;
1976
3629bb00 1977 cpu = cpu_flags_and (x, cpu);
c0f3af97
L
1978 if (!cpu_flags_all_zero (&cpu))
1979 {
57392598 1980 if (x.bitfield.cpuavx)
a5ff0eb2 1981 {
929f69fa 1982 /* We need to check a few extra flags with AVX. */
b9d49817 1983 if (cpu.bitfield.cpuavx
40d231b4
JB
1984 && (!t->opcode_modifier.sse2avx
1985 || (sse2avx && !i.prefix[DATA_PREFIX]))
b9d49817 1986 && (!x.bitfield.cpuaes || cpu.bitfield.cpuaes)
929f69fa 1987 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
b9d49817
JB
1988 && (!x.bitfield.cpupclmul || cpu.bitfield.cpupclmul))
1989 match |= CPU_FLAGS_ARCH_MATCH;
a5ff0eb2 1990 }
929f69fa
JB
1991 else if (x.bitfield.cpuavx512f)
1992 {
1993 /* We need to check a few extra flags with AVX512F. */
1994 if (cpu.bitfield.cpuavx512f
1995 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
1996 && (!x.bitfield.cpuvaes || cpu.bitfield.cpuvaes)
1997 && (!x.bitfield.cpuvpclmulqdq || cpu.bitfield.cpuvpclmulqdq))
1998 match |= CPU_FLAGS_ARCH_MATCH;
1999 }
a5ff0eb2 2000 else
db12e14e 2001 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 2002 }
3629bb00 2003 }
c0f3af97 2004 return match;
40fb9820
L
2005}
2006
c6fb90c8
L
2007static INLINE i386_operand_type
2008operand_type_and (i386_operand_type x, i386_operand_type y)
40fb9820 2009{
bab6aec1
JB
2010 if (x.bitfield.class != y.bitfield.class)
2011 x.bitfield.class = ClassNone;
75e5731b
JB
2012 if (x.bitfield.instance != y.bitfield.instance)
2013 x.bitfield.instance = InstanceNone;
bab6aec1 2014
c6fb90c8
L
2015 switch (ARRAY_SIZE (x.array))
2016 {
2017 case 3:
2018 x.array [2] &= y.array [2];
1a0670f3 2019 /* Fall through. */
c6fb90c8
L
2020 case 2:
2021 x.array [1] &= y.array [1];
1a0670f3 2022 /* Fall through. */
c6fb90c8
L
2023 case 1:
2024 x.array [0] &= y.array [0];
2025 break;
2026 default:
2027 abort ();
2028 }
2029 return x;
40fb9820
L
2030}
2031
73053c1f
JB
2032static INLINE i386_operand_type
2033operand_type_and_not (i386_operand_type x, i386_operand_type y)
2034{
bab6aec1 2035 gas_assert (y.bitfield.class == ClassNone);
75e5731b 2036 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 2037
73053c1f
JB
2038 switch (ARRAY_SIZE (x.array))
2039 {
2040 case 3:
2041 x.array [2] &= ~y.array [2];
2042 /* Fall through. */
2043 case 2:
2044 x.array [1] &= ~y.array [1];
2045 /* Fall through. */
2046 case 1:
2047 x.array [0] &= ~y.array [0];
2048 break;
2049 default:
2050 abort ();
2051 }
2052 return x;
2053}
2054
c6fb90c8
L
2055static INLINE i386_operand_type
2056operand_type_or (i386_operand_type x, i386_operand_type y)
40fb9820 2057{
bab6aec1
JB
2058 gas_assert (x.bitfield.class == ClassNone ||
2059 y.bitfield.class == ClassNone ||
2060 x.bitfield.class == y.bitfield.class);
75e5731b
JB
2061 gas_assert (x.bitfield.instance == InstanceNone ||
2062 y.bitfield.instance == InstanceNone ||
2063 x.bitfield.instance == y.bitfield.instance);
bab6aec1 2064
c6fb90c8 2065 switch (ARRAY_SIZE (x.array))
40fb9820 2066 {
c6fb90c8
L
2067 case 3:
2068 x.array [2] |= y.array [2];
1a0670f3 2069 /* Fall through. */
c6fb90c8
L
2070 case 2:
2071 x.array [1] |= y.array [1];
1a0670f3 2072 /* Fall through. */
c6fb90c8
L
2073 case 1:
2074 x.array [0] |= y.array [0];
40fb9820
L
2075 break;
2076 default:
2077 abort ();
2078 }
c6fb90c8
L
2079 return x;
2080}
40fb9820 2081
c6fb90c8
L
2082static INLINE i386_operand_type
2083operand_type_xor (i386_operand_type x, i386_operand_type y)
2084{
bab6aec1 2085 gas_assert (y.bitfield.class == ClassNone);
75e5731b 2086 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 2087
c6fb90c8
L
2088 switch (ARRAY_SIZE (x.array))
2089 {
2090 case 3:
2091 x.array [2] ^= y.array [2];
1a0670f3 2092 /* Fall through. */
c6fb90c8
L
2093 case 2:
2094 x.array [1] ^= y.array [1];
1a0670f3 2095 /* Fall through. */
c6fb90c8
L
2096 case 1:
2097 x.array [0] ^= y.array [0];
2098 break;
2099 default:
2100 abort ();
2101 }
40fb9820
L
2102 return x;
2103}
2104
40fb9820
L
2105static const i386_operand_type disp16 = OPERAND_TYPE_DISP16;
2106static const i386_operand_type disp32 = OPERAND_TYPE_DISP32;
2107static const i386_operand_type disp32s = OPERAND_TYPE_DISP32S;
2108static const i386_operand_type disp16_32 = OPERAND_TYPE_DISP16_32;
bab6aec1
JB
2109static const i386_operand_type anydisp = OPERAND_TYPE_ANYDISP;
2110static const i386_operand_type anyimm = OPERAND_TYPE_ANYIMM;
40fb9820 2111static const i386_operand_type regxmm = OPERAND_TYPE_REGXMM;
43234a1e 2112static const i386_operand_type regmask = OPERAND_TYPE_REGMASK;
40fb9820
L
2113static const i386_operand_type imm8 = OPERAND_TYPE_IMM8;
2114static const i386_operand_type imm8s = OPERAND_TYPE_IMM8S;
2115static const i386_operand_type imm16 = OPERAND_TYPE_IMM16;
2116static const i386_operand_type imm32 = OPERAND_TYPE_IMM32;
2117static const i386_operand_type imm32s = OPERAND_TYPE_IMM32S;
2118static const i386_operand_type imm64 = OPERAND_TYPE_IMM64;
2119static const i386_operand_type imm16_32 = OPERAND_TYPE_IMM16_32;
2120static const i386_operand_type imm16_32s = OPERAND_TYPE_IMM16_32S;
2121static const i386_operand_type imm16_32_32s = OPERAND_TYPE_IMM16_32_32S;
2122
2123enum operand_type
2124{
2125 reg,
40fb9820
L
2126 imm,
2127 disp,
2128 anymem
2129};
2130
c6fb90c8 2131static INLINE int
40fb9820
L
2132operand_type_check (i386_operand_type t, enum operand_type c)
2133{
2134 switch (c)
2135 {
2136 case reg:
bab6aec1 2137 return t.bitfield.class == Reg;
40fb9820 2138
40fb9820
L
2139 case imm:
2140 return (t.bitfield.imm8
2141 || t.bitfield.imm8s
2142 || t.bitfield.imm16
2143 || t.bitfield.imm32
2144 || t.bitfield.imm32s
2145 || t.bitfield.imm64);
2146
2147 case disp:
2148 return (t.bitfield.disp8
2149 || t.bitfield.disp16
2150 || t.bitfield.disp32
2151 || t.bitfield.disp32s
2152 || t.bitfield.disp64);
2153
2154 case anymem:
2155 return (t.bitfield.disp8
2156 || t.bitfield.disp16
2157 || t.bitfield.disp32
2158 || t.bitfield.disp32s
2159 || t.bitfield.disp64
2160 || t.bitfield.baseindex);
2161
2162 default:
2163 abort ();
2164 }
2cfe26b6
AM
2165
2166 return 0;
40fb9820
L
2167}
2168
7a54636a
L
2169/* Return 1 if there is no conflict in 8bit/16bit/32bit/64bit/80bit size
2170 between operand GIVEN and opeand WANTED for instruction template T. */
5c07affc
L
2171
2172static INLINE int
7a54636a
L
2173match_operand_size (const insn_template *t, unsigned int wanted,
2174 unsigned int given)
5c07affc 2175{
3ac21baa
JB
2176 return !((i.types[given].bitfield.byte
2177 && !t->operand_types[wanted].bitfield.byte)
2178 || (i.types[given].bitfield.word
2179 && !t->operand_types[wanted].bitfield.word)
2180 || (i.types[given].bitfield.dword
2181 && !t->operand_types[wanted].bitfield.dword)
2182 || (i.types[given].bitfield.qword
2183 && !t->operand_types[wanted].bitfield.qword)
2184 || (i.types[given].bitfield.tbyte
2185 && !t->operand_types[wanted].bitfield.tbyte));
5c07affc
L
2186}
2187
dd40ce22
L
2188/* Return 1 if there is no conflict in SIMD register between operand
2189 GIVEN and opeand WANTED for instruction template T. */
1b54b8d7
JB
2190
2191static INLINE int
dd40ce22
L
2192match_simd_size (const insn_template *t, unsigned int wanted,
2193 unsigned int given)
1b54b8d7 2194{
3ac21baa
JB
2195 return !((i.types[given].bitfield.xmmword
2196 && !t->operand_types[wanted].bitfield.xmmword)
2197 || (i.types[given].bitfield.ymmword
2198 && !t->operand_types[wanted].bitfield.ymmword)
2199 || (i.types[given].bitfield.zmmword
260cd341
LC
2200 && !t->operand_types[wanted].bitfield.zmmword)
2201 || (i.types[given].bitfield.tmmword
2202 && !t->operand_types[wanted].bitfield.tmmword));
1b54b8d7
JB
2203}
2204
7a54636a
L
2205/* Return 1 if there is no conflict in any size between operand GIVEN
2206 and opeand WANTED for instruction template T. */
5c07affc
L
2207
2208static INLINE int
dd40ce22
L
2209match_mem_size (const insn_template *t, unsigned int wanted,
2210 unsigned int given)
5c07affc 2211{
7a54636a 2212 return (match_operand_size (t, wanted, given)
3ac21baa 2213 && !((i.types[given].bitfield.unspecified
af508cb9 2214 && !i.broadcast
3ac21baa
JB
2215 && !t->operand_types[wanted].bitfield.unspecified)
2216 || (i.types[given].bitfield.fword
2217 && !t->operand_types[wanted].bitfield.fword)
1b54b8d7
JB
2218 /* For scalar opcode templates to allow register and memory
2219 operands at the same time, some special casing is needed
d6793fa1
JB
2220 here. Also for v{,p}broadcast*, {,v}pmov{s,z}*, and
2221 down-conversion vpmov*. */
3528c362 2222 || ((t->operand_types[wanted].bitfield.class == RegSIMD
bc49bfd8
JB
2223 && t->operand_types[wanted].bitfield.byte
2224 + t->operand_types[wanted].bitfield.word
2225 + t->operand_types[wanted].bitfield.dword
2226 + t->operand_types[wanted].bitfield.qword
2227 > !!t->opcode_modifier.broadcast)
3ac21baa
JB
2228 ? (i.types[given].bitfield.xmmword
2229 || i.types[given].bitfield.ymmword
2230 || i.types[given].bitfield.zmmword)
2231 : !match_simd_size(t, wanted, given))));
5c07affc
L
2232}
2233
3ac21baa
JB
2234/* Return value has MATCH_STRAIGHT set if there is no size conflict on any
2235 operands for instruction template T, and it has MATCH_REVERSE set if there
2236 is no size conflict on any operands for the template with operands reversed
2237 (and the template allows for reversing in the first place). */
5c07affc 2238
3ac21baa
JB
2239#define MATCH_STRAIGHT 1
2240#define MATCH_REVERSE 2
2241
2242static INLINE unsigned int
d3ce72d0 2243operand_size_match (const insn_template *t)
5c07affc 2244{
3ac21baa 2245 unsigned int j, match = MATCH_STRAIGHT;
5c07affc 2246
0cfa3eb3 2247 /* Don't check non-absolute jump instructions. */
5c07affc 2248 if (t->opcode_modifier.jump
0cfa3eb3 2249 && t->opcode_modifier.jump != JUMP_ABSOLUTE)
5c07affc
L
2250 return match;
2251
2252 /* Check memory and accumulator operand size. */
2253 for (j = 0; j < i.operands; j++)
2254 {
3528c362
JB
2255 if (i.types[j].bitfield.class != Reg
2256 && i.types[j].bitfield.class != RegSIMD
601e8564 2257 && t->opcode_modifier.anysize)
5c07affc
L
2258 continue;
2259
bab6aec1 2260 if (t->operand_types[j].bitfield.class == Reg
7a54636a 2261 && !match_operand_size (t, j, j))
5c07affc
L
2262 {
2263 match = 0;
2264 break;
2265 }
2266
3528c362 2267 if (t->operand_types[j].bitfield.class == RegSIMD
3ac21baa 2268 && !match_simd_size (t, j, j))
1b54b8d7
JB
2269 {
2270 match = 0;
2271 break;
2272 }
2273
75e5731b 2274 if (t->operand_types[j].bitfield.instance == Accum
7a54636a 2275 && (!match_operand_size (t, j, j) || !match_simd_size (t, j, j)))
1b54b8d7
JB
2276 {
2277 match = 0;
2278 break;
2279 }
2280
c48dadc9 2281 if ((i.flags[j] & Operand_Mem) && !match_mem_size (t, j, j))
5c07affc
L
2282 {
2283 match = 0;
2284 break;
2285 }
2286 }
2287
3ac21baa 2288 if (!t->opcode_modifier.d)
891edac4 2289 {
dc1e8a47 2290 mismatch:
3ac21baa
JB
2291 if (!match)
2292 i.error = operand_size_mismatch;
2293 return match;
891edac4 2294 }
5c07affc
L
2295
2296 /* Check reverse. */
f5eb1d70 2297 gas_assert (i.operands >= 2 && i.operands <= 3);
5c07affc 2298
f5eb1d70 2299 for (j = 0; j < i.operands; j++)
5c07affc 2300 {
f5eb1d70
JB
2301 unsigned int given = i.operands - j - 1;
2302
bab6aec1 2303 if (t->operand_types[j].bitfield.class == Reg
f5eb1d70 2304 && !match_operand_size (t, j, given))
891edac4 2305 goto mismatch;
5c07affc 2306
3528c362 2307 if (t->operand_types[j].bitfield.class == RegSIMD
f5eb1d70 2308 && !match_simd_size (t, j, given))
dbbc8b7e
JB
2309 goto mismatch;
2310
75e5731b 2311 if (t->operand_types[j].bitfield.instance == Accum
f5eb1d70
JB
2312 && (!match_operand_size (t, j, given)
2313 || !match_simd_size (t, j, given)))
dbbc8b7e
JB
2314 goto mismatch;
2315
f5eb1d70 2316 if ((i.flags[given] & Operand_Mem) && !match_mem_size (t, j, given))
891edac4 2317 goto mismatch;
5c07affc
L
2318 }
2319
3ac21baa 2320 return match | MATCH_REVERSE;
5c07affc
L
2321}
2322
c6fb90c8 2323static INLINE int
40fb9820
L
2324operand_type_match (i386_operand_type overlap,
2325 i386_operand_type given)
2326{
2327 i386_operand_type temp = overlap;
2328
7d5e4556 2329 temp.bitfield.unspecified = 0;
5c07affc
L
2330 temp.bitfield.byte = 0;
2331 temp.bitfield.word = 0;
2332 temp.bitfield.dword = 0;
2333 temp.bitfield.fword = 0;
2334 temp.bitfield.qword = 0;
2335 temp.bitfield.tbyte = 0;
2336 temp.bitfield.xmmword = 0;
c0f3af97 2337 temp.bitfield.ymmword = 0;
43234a1e 2338 temp.bitfield.zmmword = 0;
260cd341 2339 temp.bitfield.tmmword = 0;
0dfbf9d7 2340 if (operand_type_all_zero (&temp))
891edac4 2341 goto mismatch;
40fb9820 2342
6f2f06be 2343 if (given.bitfield.baseindex == overlap.bitfield.baseindex)
891edac4
L
2344 return 1;
2345
dc1e8a47 2346 mismatch:
a65babc9 2347 i.error = operand_type_mismatch;
891edac4 2348 return 0;
40fb9820
L
2349}
2350
7d5e4556 2351/* If given types g0 and g1 are registers they must be of the same type
10c17abd 2352 unless the expected operand type register overlap is null.
5de4d9ef 2353 Some Intel syntax memory operand size checking also happens here. */
40fb9820 2354
c6fb90c8 2355static INLINE int
dc821c5f 2356operand_type_register_match (i386_operand_type g0,
40fb9820 2357 i386_operand_type t0,
40fb9820
L
2358 i386_operand_type g1,
2359 i386_operand_type t1)
2360{
bab6aec1 2361 if (g0.bitfield.class != Reg
3528c362 2362 && g0.bitfield.class != RegSIMD
10c17abd
JB
2363 && (!operand_type_check (g0, anymem)
2364 || g0.bitfield.unspecified
5de4d9ef
JB
2365 || (t0.bitfield.class != Reg
2366 && t0.bitfield.class != RegSIMD)))
40fb9820
L
2367 return 1;
2368
bab6aec1 2369 if (g1.bitfield.class != Reg
3528c362 2370 && g1.bitfield.class != RegSIMD
10c17abd
JB
2371 && (!operand_type_check (g1, anymem)
2372 || g1.bitfield.unspecified
5de4d9ef
JB
2373 || (t1.bitfield.class != Reg
2374 && t1.bitfield.class != RegSIMD)))
40fb9820
L
2375 return 1;
2376
dc821c5f
JB
2377 if (g0.bitfield.byte == g1.bitfield.byte
2378 && g0.bitfield.word == g1.bitfield.word
2379 && g0.bitfield.dword == g1.bitfield.dword
10c17abd
JB
2380 && g0.bitfield.qword == g1.bitfield.qword
2381 && g0.bitfield.xmmword == g1.bitfield.xmmword
2382 && g0.bitfield.ymmword == g1.bitfield.ymmword
2383 && g0.bitfield.zmmword == g1.bitfield.zmmword)
40fb9820
L
2384 return 1;
2385
dc821c5f
JB
2386 if (!(t0.bitfield.byte & t1.bitfield.byte)
2387 && !(t0.bitfield.word & t1.bitfield.word)
2388 && !(t0.bitfield.dword & t1.bitfield.dword)
10c17abd
JB
2389 && !(t0.bitfield.qword & t1.bitfield.qword)
2390 && !(t0.bitfield.xmmword & t1.bitfield.xmmword)
2391 && !(t0.bitfield.ymmword & t1.bitfield.ymmword)
2392 && !(t0.bitfield.zmmword & t1.bitfield.zmmword))
891edac4
L
2393 return 1;
2394
a65babc9 2395 i.error = register_type_mismatch;
891edac4
L
2396
2397 return 0;
40fb9820
L
2398}
2399
4c692bc7
JB
2400static INLINE unsigned int
2401register_number (const reg_entry *r)
2402{
2403 unsigned int nr = r->reg_num;
2404
2405 if (r->reg_flags & RegRex)
2406 nr += 8;
2407
200cbe0f
L
2408 if (r->reg_flags & RegVRex)
2409 nr += 16;
2410
4c692bc7
JB
2411 return nr;
2412}
2413
252b5132 2414static INLINE unsigned int
40fb9820 2415mode_from_disp_size (i386_operand_type t)
252b5132 2416{
b5014f7a 2417 if (t.bitfield.disp8)
40fb9820
L
2418 return 1;
2419 else if (t.bitfield.disp16
2420 || t.bitfield.disp32
2421 || t.bitfield.disp32s)
2422 return 2;
2423 else
2424 return 0;
252b5132
RH
2425}
2426
2427static INLINE int
65879393 2428fits_in_signed_byte (addressT num)
252b5132 2429{
65879393 2430 return num + 0x80 <= 0xff;
47926f60 2431}
252b5132
RH
2432
2433static INLINE int
65879393 2434fits_in_unsigned_byte (addressT num)
252b5132 2435{
65879393 2436 return num <= 0xff;
47926f60 2437}
252b5132
RH
2438
2439static INLINE int
65879393 2440fits_in_unsigned_word (addressT num)
252b5132 2441{
65879393 2442 return num <= 0xffff;
47926f60 2443}
252b5132
RH
2444
2445static INLINE int
65879393 2446fits_in_signed_word (addressT num)
252b5132 2447{
65879393 2448 return num + 0x8000 <= 0xffff;
47926f60 2449}
2a962e6d 2450
3e73aa7c 2451static INLINE int
65879393 2452fits_in_signed_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2453{
2454#ifndef BFD64
2455 return 1;
2456#else
65879393 2457 return num + 0x80000000 <= 0xffffffff;
3e73aa7c
JH
2458#endif
2459} /* fits_in_signed_long() */
2a962e6d 2460
3e73aa7c 2461static INLINE int
65879393 2462fits_in_unsigned_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2463{
2464#ifndef BFD64
2465 return 1;
2466#else
65879393 2467 return num <= 0xffffffff;
3e73aa7c
JH
2468#endif
2469} /* fits_in_unsigned_long() */
252b5132 2470
43234a1e 2471static INLINE int
b5014f7a 2472fits_in_disp8 (offsetT num)
43234a1e
L
2473{
2474 int shift = i.memshift;
2475 unsigned int mask;
2476
2477 if (shift == -1)
2478 abort ();
2479
2480 mask = (1 << shift) - 1;
2481
2482 /* Return 0 if NUM isn't properly aligned. */
2483 if ((num & mask))
2484 return 0;
2485
2486 /* Check if NUM will fit in 8bit after shift. */
2487 return fits_in_signed_byte (num >> shift);
2488}
2489
a683cc34
SP
2490static INLINE int
2491fits_in_imm4 (offsetT num)
2492{
2493 return (num & 0xf) == num;
2494}
2495
40fb9820 2496static i386_operand_type
e3bb37b5 2497smallest_imm_type (offsetT num)
252b5132 2498{
40fb9820 2499 i386_operand_type t;
7ab9ffdd 2500
0dfbf9d7 2501 operand_type_set (&t, 0);
40fb9820
L
2502 t.bitfield.imm64 = 1;
2503
2504 if (cpu_arch_tune != PROCESSOR_I486 && num == 1)
e413e4e9
AM
2505 {
2506 /* This code is disabled on the 486 because all the Imm1 forms
2507 in the opcode table are slower on the i486. They're the
2508 versions with the implicitly specified single-position
2509 displacement, which has another syntax if you really want to
2510 use that form. */
40fb9820
L
2511 t.bitfield.imm1 = 1;
2512 t.bitfield.imm8 = 1;
2513 t.bitfield.imm8s = 1;
2514 t.bitfield.imm16 = 1;
2515 t.bitfield.imm32 = 1;
2516 t.bitfield.imm32s = 1;
2517 }
2518 else if (fits_in_signed_byte (num))
2519 {
2520 t.bitfield.imm8 = 1;
2521 t.bitfield.imm8s = 1;
2522 t.bitfield.imm16 = 1;
2523 t.bitfield.imm32 = 1;
2524 t.bitfield.imm32s = 1;
2525 }
2526 else if (fits_in_unsigned_byte (num))
2527 {
2528 t.bitfield.imm8 = 1;
2529 t.bitfield.imm16 = 1;
2530 t.bitfield.imm32 = 1;
2531 t.bitfield.imm32s = 1;
2532 }
2533 else if (fits_in_signed_word (num) || fits_in_unsigned_word (num))
2534 {
2535 t.bitfield.imm16 = 1;
2536 t.bitfield.imm32 = 1;
2537 t.bitfield.imm32s = 1;
2538 }
2539 else if (fits_in_signed_long (num))
2540 {
2541 t.bitfield.imm32 = 1;
2542 t.bitfield.imm32s = 1;
2543 }
2544 else if (fits_in_unsigned_long (num))
2545 t.bitfield.imm32 = 1;
2546
2547 return t;
47926f60 2548}
252b5132 2549
847f7ad4 2550static offsetT
e3bb37b5 2551offset_in_range (offsetT val, int size)
847f7ad4 2552{
508866be 2553 addressT mask;
ba2adb93 2554
847f7ad4
AM
2555 switch (size)
2556 {
508866be
L
2557 case 1: mask = ((addressT) 1 << 8) - 1; break;
2558 case 2: mask = ((addressT) 1 << 16) - 1; break;
3b0ec529 2559 case 4: mask = ((addressT) 2 << 31) - 1; break;
3e73aa7c
JH
2560#ifdef BFD64
2561 case 8: mask = ((addressT) 2 << 63) - 1; break;
2562#endif
47926f60 2563 default: abort ();
847f7ad4
AM
2564 }
2565
47926f60 2566 if ((val & ~mask) != 0 && (val & ~mask) != ~mask)
847f7ad4
AM
2567 {
2568 char buf1[40], buf2[40];
2569
2570 sprint_value (buf1, val);
2571 sprint_value (buf2, val & mask);
2572 as_warn (_("%s shortened to %s"), buf1, buf2);
2573 }
2574 return val & mask;
2575}
2576
c32fa91d
L
2577enum PREFIX_GROUP
2578{
2579 PREFIX_EXIST = 0,
2580 PREFIX_LOCK,
2581 PREFIX_REP,
04ef582a 2582 PREFIX_DS,
c32fa91d
L
2583 PREFIX_OTHER
2584};
2585
2586/* Returns
2587 a. PREFIX_EXIST if attempting to add a prefix where one from the
2588 same class already exists.
2589 b. PREFIX_LOCK if lock prefix is added.
2590 c. PREFIX_REP if rep/repne prefix is added.
04ef582a
L
2591 d. PREFIX_DS if ds prefix is added.
2592 e. PREFIX_OTHER if other prefix is added.
c32fa91d
L
2593 */
2594
2595static enum PREFIX_GROUP
e3bb37b5 2596add_prefix (unsigned int prefix)
252b5132 2597{
c32fa91d 2598 enum PREFIX_GROUP ret = PREFIX_OTHER;
b1905489 2599 unsigned int q;
252b5132 2600
29b0f896
AM
2601 if (prefix >= REX_OPCODE && prefix < REX_OPCODE + 16
2602 && flag_code == CODE_64BIT)
b1905489 2603 {
161a04f6 2604 if ((i.prefix[REX_PREFIX] & prefix & REX_W)
44846f29
JB
2605 || (i.prefix[REX_PREFIX] & prefix & REX_R)
2606 || (i.prefix[REX_PREFIX] & prefix & REX_X)
2607 || (i.prefix[REX_PREFIX] & prefix & REX_B))
c32fa91d 2608 ret = PREFIX_EXIST;
b1905489
JB
2609 q = REX_PREFIX;
2610 }
3e73aa7c 2611 else
b1905489
JB
2612 {
2613 switch (prefix)
2614 {
2615 default:
2616 abort ();
2617
b1905489 2618 case DS_PREFIX_OPCODE:
04ef582a
L
2619 ret = PREFIX_DS;
2620 /* Fall through. */
2621 case CS_PREFIX_OPCODE:
b1905489
JB
2622 case ES_PREFIX_OPCODE:
2623 case FS_PREFIX_OPCODE:
2624 case GS_PREFIX_OPCODE:
2625 case SS_PREFIX_OPCODE:
2626 q = SEG_PREFIX;
2627 break;
2628
2629 case REPNE_PREFIX_OPCODE:
2630 case REPE_PREFIX_OPCODE:
c32fa91d
L
2631 q = REP_PREFIX;
2632 ret = PREFIX_REP;
2633 break;
2634
b1905489 2635 case LOCK_PREFIX_OPCODE:
c32fa91d
L
2636 q = LOCK_PREFIX;
2637 ret = PREFIX_LOCK;
b1905489
JB
2638 break;
2639
2640 case FWAIT_OPCODE:
2641 q = WAIT_PREFIX;
2642 break;
2643
2644 case ADDR_PREFIX_OPCODE:
2645 q = ADDR_PREFIX;
2646 break;
2647
2648 case DATA_PREFIX_OPCODE:
2649 q = DATA_PREFIX;
2650 break;
2651 }
2652 if (i.prefix[q] != 0)
c32fa91d 2653 ret = PREFIX_EXIST;
b1905489 2654 }
252b5132 2655
b1905489 2656 if (ret)
252b5132 2657 {
b1905489
JB
2658 if (!i.prefix[q])
2659 ++i.prefixes;
2660 i.prefix[q] |= prefix;
252b5132 2661 }
b1905489
JB
2662 else
2663 as_bad (_("same type of prefix used twice"));
252b5132 2664
252b5132
RH
2665 return ret;
2666}
2667
2668static void
78f12dd3 2669update_code_flag (int value, int check)
eecb386c 2670{
78f12dd3
L
2671 PRINTF_LIKE ((*as_error));
2672
1e9cc1c2 2673 flag_code = (enum flag_code) value;
40fb9820
L
2674 if (flag_code == CODE_64BIT)
2675 {
2676 cpu_arch_flags.bitfield.cpu64 = 1;
2677 cpu_arch_flags.bitfield.cpuno64 = 0;
40fb9820
L
2678 }
2679 else
2680 {
2681 cpu_arch_flags.bitfield.cpu64 = 0;
2682 cpu_arch_flags.bitfield.cpuno64 = 1;
40fb9820
L
2683 }
2684 if (value == CODE_64BIT && !cpu_arch_flags.bitfield.cpulm )
3e73aa7c 2685 {
78f12dd3
L
2686 if (check)
2687 as_error = as_fatal;
2688 else
2689 as_error = as_bad;
2690 (*as_error) (_("64bit mode not supported on `%s'."),
2691 cpu_arch_name ? cpu_arch_name : default_arch);
3e73aa7c 2692 }
40fb9820 2693 if (value == CODE_32BIT && !cpu_arch_flags.bitfield.cpui386)
3e73aa7c 2694 {
78f12dd3
L
2695 if (check)
2696 as_error = as_fatal;
2697 else
2698 as_error = as_bad;
2699 (*as_error) (_("32bit mode not supported on `%s'."),
2700 cpu_arch_name ? cpu_arch_name : default_arch);
3e73aa7c 2701 }
eecb386c
AM
2702 stackop_size = '\0';
2703}
2704
78f12dd3
L
2705static void
2706set_code_flag (int value)
2707{
2708 update_code_flag (value, 0);
2709}
2710
eecb386c 2711static void
e3bb37b5 2712set_16bit_gcc_code_flag (int new_code_flag)
252b5132 2713{
1e9cc1c2 2714 flag_code = (enum flag_code) new_code_flag;
40fb9820
L
2715 if (flag_code != CODE_16BIT)
2716 abort ();
2717 cpu_arch_flags.bitfield.cpu64 = 0;
2718 cpu_arch_flags.bitfield.cpuno64 = 1;
9306ca4a 2719 stackop_size = LONG_MNEM_SUFFIX;
252b5132
RH
2720}
2721
2722static void
e3bb37b5 2723set_intel_syntax (int syntax_flag)
252b5132
RH
2724{
2725 /* Find out if register prefixing is specified. */
2726 int ask_naked_reg = 0;
2727
2728 SKIP_WHITESPACE ();
29b0f896 2729 if (!is_end_of_line[(unsigned char) *input_line_pointer])
252b5132 2730 {
d02603dc
NC
2731 char *string;
2732 int e = get_symbol_name (&string);
252b5132 2733
47926f60 2734 if (strcmp (string, "prefix") == 0)
252b5132 2735 ask_naked_reg = 1;
47926f60 2736 else if (strcmp (string, "noprefix") == 0)
252b5132
RH
2737 ask_naked_reg = -1;
2738 else
d0b47220 2739 as_bad (_("bad argument to syntax directive."));
d02603dc 2740 (void) restore_line_pointer (e);
252b5132
RH
2741 }
2742 demand_empty_rest_of_line ();
c3332e24 2743
252b5132
RH
2744 intel_syntax = syntax_flag;
2745
2746 if (ask_naked_reg == 0)
f86103b7
AM
2747 allow_naked_reg = (intel_syntax
2748 && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
252b5132
RH
2749 else
2750 allow_naked_reg = (ask_naked_reg < 0);
9306ca4a 2751
ee86248c 2752 expr_set_rank (O_full_ptr, syntax_flag ? 10 : 0);
7ab9ffdd 2753
e4a3b5a4 2754 identifier_chars['%'] = intel_syntax && allow_naked_reg ? '%' : 0;
9306ca4a 2755 identifier_chars['$'] = intel_syntax ? '$' : 0;
e4a3b5a4 2756 register_prefix = allow_naked_reg ? "" : "%";
252b5132
RH
2757}
2758
1efbbeb4
L
2759static void
2760set_intel_mnemonic (int mnemonic_flag)
2761{
e1d4d893 2762 intel_mnemonic = mnemonic_flag;
1efbbeb4
L
2763}
2764
db51cc60
L
2765static void
2766set_allow_index_reg (int flag)
2767{
2768 allow_index_reg = flag;
2769}
2770
cb19c032 2771static void
7bab8ab5 2772set_check (int what)
cb19c032 2773{
7bab8ab5
JB
2774 enum check_kind *kind;
2775 const char *str;
2776
2777 if (what)
2778 {
2779 kind = &operand_check;
2780 str = "operand";
2781 }
2782 else
2783 {
2784 kind = &sse_check;
2785 str = "sse";
2786 }
2787
cb19c032
L
2788 SKIP_WHITESPACE ();
2789
2790 if (!is_end_of_line[(unsigned char) *input_line_pointer])
2791 {
d02603dc
NC
2792 char *string;
2793 int e = get_symbol_name (&string);
cb19c032
L
2794
2795 if (strcmp (string, "none") == 0)
7bab8ab5 2796 *kind = check_none;
cb19c032 2797 else if (strcmp (string, "warning") == 0)
7bab8ab5 2798 *kind = check_warning;
cb19c032 2799 else if (strcmp (string, "error") == 0)
7bab8ab5 2800 *kind = check_error;
cb19c032 2801 else
7bab8ab5 2802 as_bad (_("bad argument to %s_check directive."), str);
d02603dc 2803 (void) restore_line_pointer (e);
cb19c032
L
2804 }
2805 else
7bab8ab5 2806 as_bad (_("missing argument for %s_check directive"), str);
cb19c032
L
2807
2808 demand_empty_rest_of_line ();
2809}
2810
8a9036a4
L
2811static void
2812check_cpu_arch_compatible (const char *name ATTRIBUTE_UNUSED,
1e9cc1c2 2813 i386_cpu_flags new_flag ATTRIBUTE_UNUSED)
8a9036a4
L
2814{
2815#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2816 static const char *arch;
2817
2818 /* Intel LIOM is only supported on ELF. */
2819 if (!IS_ELF)
2820 return;
2821
2822 if (!arch)
2823 {
2824 /* Use cpu_arch_name if it is set in md_parse_option. Otherwise
2825 use default_arch. */
2826 arch = cpu_arch_name;
2827 if (!arch)
2828 arch = default_arch;
2829 }
2830
81486035
L
2831 /* If we are targeting Intel MCU, we must enable it. */
2832 if (get_elf_backend_data (stdoutput)->elf_machine_code != EM_IAMCU
2833 || new_flag.bitfield.cpuiamcu)
2834 return;
2835
3632d14b 2836 /* If we are targeting Intel L1OM, we must enable it. */
8a9036a4 2837 if (get_elf_backend_data (stdoutput)->elf_machine_code != EM_L1OM
1e9cc1c2 2838 || new_flag.bitfield.cpul1om)
8a9036a4 2839 return;
76ba9986 2840
7a9068fe
L
2841 /* If we are targeting Intel K1OM, we must enable it. */
2842 if (get_elf_backend_data (stdoutput)->elf_machine_code != EM_K1OM
2843 || new_flag.bitfield.cpuk1om)
2844 return;
2845
8a9036a4
L
2846 as_bad (_("`%s' is not supported on `%s'"), name, arch);
2847#endif
2848}
2849
e413e4e9 2850static void
e3bb37b5 2851set_cpu_arch (int dummy ATTRIBUTE_UNUSED)
e413e4e9 2852{
47926f60 2853 SKIP_WHITESPACE ();
e413e4e9 2854
29b0f896 2855 if (!is_end_of_line[(unsigned char) *input_line_pointer])
e413e4e9 2856 {
d02603dc
NC
2857 char *string;
2858 int e = get_symbol_name (&string);
91d6fa6a 2859 unsigned int j;
40fb9820 2860 i386_cpu_flags flags;
e413e4e9 2861
91d6fa6a 2862 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
e413e4e9 2863 {
91d6fa6a 2864 if (strcmp (string, cpu_arch[j].name) == 0)
e413e4e9 2865 {
91d6fa6a 2866 check_cpu_arch_compatible (string, cpu_arch[j].flags);
8a9036a4 2867
5c6af06e
JB
2868 if (*string != '.')
2869 {
91d6fa6a 2870 cpu_arch_name = cpu_arch[j].name;
5c6af06e 2871 cpu_sub_arch_name = NULL;
91d6fa6a 2872 cpu_arch_flags = cpu_arch[j].flags;
40fb9820
L
2873 if (flag_code == CODE_64BIT)
2874 {
2875 cpu_arch_flags.bitfield.cpu64 = 1;
2876 cpu_arch_flags.bitfield.cpuno64 = 0;
2877 }
2878 else
2879 {
2880 cpu_arch_flags.bitfield.cpu64 = 0;
2881 cpu_arch_flags.bitfield.cpuno64 = 1;
2882 }
91d6fa6a
NC
2883 cpu_arch_isa = cpu_arch[j].type;
2884 cpu_arch_isa_flags = cpu_arch[j].flags;
ccc9c027
L
2885 if (!cpu_arch_tune_set)
2886 {
2887 cpu_arch_tune = cpu_arch_isa;
2888 cpu_arch_tune_flags = cpu_arch_isa_flags;
2889 }
5c6af06e
JB
2890 break;
2891 }
40fb9820 2892
293f5f65
L
2893 flags = cpu_flags_or (cpu_arch_flags,
2894 cpu_arch[j].flags);
81486035 2895
5b64d091 2896 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
5c6af06e 2897 {
6305a203
L
2898 if (cpu_sub_arch_name)
2899 {
2900 char *name = cpu_sub_arch_name;
2901 cpu_sub_arch_name = concat (name,
91d6fa6a 2902 cpu_arch[j].name,
1bf57e9f 2903 (const char *) NULL);
6305a203
L
2904 free (name);
2905 }
2906 else
91d6fa6a 2907 cpu_sub_arch_name = xstrdup (cpu_arch[j].name);
40fb9820 2908 cpu_arch_flags = flags;
a586129e 2909 cpu_arch_isa_flags = flags;
5c6af06e 2910 }
0089dace
L
2911 else
2912 cpu_arch_isa_flags
2913 = cpu_flags_or (cpu_arch_isa_flags,
2914 cpu_arch[j].flags);
d02603dc 2915 (void) restore_line_pointer (e);
5c6af06e
JB
2916 demand_empty_rest_of_line ();
2917 return;
e413e4e9
AM
2918 }
2919 }
293f5f65
L
2920
2921 if (*string == '.' && j >= ARRAY_SIZE (cpu_arch))
2922 {
33eaf5de 2923 /* Disable an ISA extension. */
293f5f65
L
2924 for (j = 0; j < ARRAY_SIZE (cpu_noarch); j++)
2925 if (strcmp (string + 1, cpu_noarch [j].name) == 0)
2926 {
2927 flags = cpu_flags_and_not (cpu_arch_flags,
2928 cpu_noarch[j].flags);
2929 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
2930 {
2931 if (cpu_sub_arch_name)
2932 {
2933 char *name = cpu_sub_arch_name;
2934 cpu_sub_arch_name = concat (name, string,
2935 (const char *) NULL);
2936 free (name);
2937 }
2938 else
2939 cpu_sub_arch_name = xstrdup (string);
2940 cpu_arch_flags = flags;
2941 cpu_arch_isa_flags = flags;
2942 }
2943 (void) restore_line_pointer (e);
2944 demand_empty_rest_of_line ();
2945 return;
2946 }
2947
2948 j = ARRAY_SIZE (cpu_arch);
2949 }
2950
91d6fa6a 2951 if (j >= ARRAY_SIZE (cpu_arch))
e413e4e9
AM
2952 as_bad (_("no such architecture: `%s'"), string);
2953
2954 *input_line_pointer = e;
2955 }
2956 else
2957 as_bad (_("missing cpu architecture"));
2958
fddf5b5b
AM
2959 no_cond_jump_promotion = 0;
2960 if (*input_line_pointer == ','
29b0f896 2961 && !is_end_of_line[(unsigned char) input_line_pointer[1]])
fddf5b5b 2962 {
d02603dc
NC
2963 char *string;
2964 char e;
2965
2966 ++input_line_pointer;
2967 e = get_symbol_name (&string);
fddf5b5b
AM
2968
2969 if (strcmp (string, "nojumps") == 0)
2970 no_cond_jump_promotion = 1;
2971 else if (strcmp (string, "jumps") == 0)
2972 ;
2973 else
2974 as_bad (_("no such architecture modifier: `%s'"), string);
2975
d02603dc 2976 (void) restore_line_pointer (e);
fddf5b5b
AM
2977 }
2978
e413e4e9
AM
2979 demand_empty_rest_of_line ();
2980}
2981
8a9036a4
L
2982enum bfd_architecture
2983i386_arch (void)
2984{
3632d14b 2985 if (cpu_arch_isa == PROCESSOR_L1OM)
8a9036a4
L
2986 {
2987 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2988 || flag_code != CODE_64BIT)
2989 as_fatal (_("Intel L1OM is 64bit ELF only"));
2990 return bfd_arch_l1om;
2991 }
7a9068fe
L
2992 else if (cpu_arch_isa == PROCESSOR_K1OM)
2993 {
2994 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2995 || flag_code != CODE_64BIT)
2996 as_fatal (_("Intel K1OM is 64bit ELF only"));
2997 return bfd_arch_k1om;
2998 }
81486035
L
2999 else if (cpu_arch_isa == PROCESSOR_IAMCU)
3000 {
3001 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
3002 || flag_code == CODE_64BIT)
3003 as_fatal (_("Intel MCU is 32bit ELF only"));
3004 return bfd_arch_iamcu;
3005 }
8a9036a4
L
3006 else
3007 return bfd_arch_i386;
3008}
3009
b9d79e03 3010unsigned long
7016a5d5 3011i386_mach (void)
b9d79e03 3012{
351f65ca 3013 if (!strncmp (default_arch, "x86_64", 6))
8a9036a4 3014 {
3632d14b 3015 if (cpu_arch_isa == PROCESSOR_L1OM)
8a9036a4 3016 {
351f65ca
L
3017 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
3018 || default_arch[6] != '\0')
8a9036a4
L
3019 as_fatal (_("Intel L1OM is 64bit ELF only"));
3020 return bfd_mach_l1om;
3021 }
7a9068fe
L
3022 else if (cpu_arch_isa == PROCESSOR_K1OM)
3023 {
3024 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
3025 || default_arch[6] != '\0')
3026 as_fatal (_("Intel K1OM is 64bit ELF only"));
3027 return bfd_mach_k1om;
3028 }
351f65ca 3029 else if (default_arch[6] == '\0')
8a9036a4 3030 return bfd_mach_x86_64;
351f65ca
L
3031 else
3032 return bfd_mach_x64_32;
8a9036a4 3033 }
5197d474
L
3034 else if (!strcmp (default_arch, "i386")
3035 || !strcmp (default_arch, "iamcu"))
81486035
L
3036 {
3037 if (cpu_arch_isa == PROCESSOR_IAMCU)
3038 {
3039 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
3040 as_fatal (_("Intel MCU is 32bit ELF only"));
3041 return bfd_mach_i386_iamcu;
3042 }
3043 else
3044 return bfd_mach_i386_i386;
3045 }
b9d79e03 3046 else
2b5d6a91 3047 as_fatal (_("unknown architecture"));
b9d79e03 3048}
b9d79e03 3049\f
252b5132 3050void
7016a5d5 3051md_begin (void)
252b5132 3052{
86fa6981
L
3053 /* Support pseudo prefixes like {disp32}. */
3054 lex_type ['{'] = LEX_BEGIN_NAME;
3055
47926f60 3056 /* Initialize op_hash hash table. */
629310ab 3057 op_hash = str_htab_create ();
252b5132
RH
3058
3059 {
d3ce72d0 3060 const insn_template *optab;
29b0f896 3061 templates *core_optab;
252b5132 3062
47926f60
KH
3063 /* Setup for loop. */
3064 optab = i386_optab;
add39d23 3065 core_optab = XNEW (templates);
252b5132
RH
3066 core_optab->start = optab;
3067
3068 while (1)
3069 {
3070 ++optab;
3071 if (optab->name == NULL
3072 || strcmp (optab->name, (optab - 1)->name) != 0)
3073 {
3074 /* different name --> ship out current template list;
47926f60 3075 add to hash table; & begin anew. */
252b5132 3076 core_optab->end = optab;
fe0e921f
AM
3077 if (str_hash_insert (op_hash, (optab - 1)->name, core_optab, 0))
3078 as_fatal (_("duplicate %s"), (optab - 1)->name);
3079
252b5132
RH
3080 if (optab->name == NULL)
3081 break;
add39d23 3082 core_optab = XNEW (templates);
252b5132
RH
3083 core_optab->start = optab;
3084 }
3085 }
3086 }
3087
47926f60 3088 /* Initialize reg_hash hash table. */
629310ab 3089 reg_hash = str_htab_create ();
252b5132 3090 {
29b0f896 3091 const reg_entry *regtab;
c3fe08fa 3092 unsigned int regtab_size = i386_regtab_size;
252b5132 3093
c3fe08fa 3094 for (regtab = i386_regtab; regtab_size--; regtab++)
6225c532
JB
3095 {
3096 if (str_hash_insert (reg_hash, regtab->reg_name, regtab, 0) != NULL)
3097 as_fatal (_("duplicate %s"), regtab->reg_name);
3098
3099 if (regtab->reg_type.bitfield.class == RegMask && !regtab->reg_num)
3100 reg_k0 = regtab;
3101 }
252b5132
RH
3102 }
3103
47926f60 3104 /* Fill in lexical tables: mnemonic_chars, operand_chars. */
252b5132 3105 {
29b0f896
AM
3106 int c;
3107 char *p;
252b5132
RH
3108
3109 for (c = 0; c < 256; c++)
3110 {
3882b010 3111 if (ISDIGIT (c))
252b5132
RH
3112 {
3113 digit_chars[c] = c;
3114 mnemonic_chars[c] = c;
3115 register_chars[c] = c;
3116 operand_chars[c] = c;
3117 }
3882b010 3118 else if (ISLOWER (c))
252b5132
RH
3119 {
3120 mnemonic_chars[c] = c;
3121 register_chars[c] = c;
3122 operand_chars[c] = c;
3123 }
3882b010 3124 else if (ISUPPER (c))
252b5132 3125 {
3882b010 3126 mnemonic_chars[c] = TOLOWER (c);
252b5132
RH
3127 register_chars[c] = mnemonic_chars[c];
3128 operand_chars[c] = c;
3129 }
43234a1e 3130 else if (c == '{' || c == '}')
86fa6981
L
3131 {
3132 mnemonic_chars[c] = c;
3133 operand_chars[c] = c;
3134 }
b3983e5f
JB
3135#ifdef SVR4_COMMENT_CHARS
3136 else if (c == '\\' && strchr (i386_comment_chars, '/'))
3137 operand_chars[c] = c;
3138#endif
252b5132 3139
3882b010 3140 if (ISALPHA (c) || ISDIGIT (c))
252b5132
RH
3141 identifier_chars[c] = c;
3142 else if (c >= 128)
3143 {
3144 identifier_chars[c] = c;
3145 operand_chars[c] = c;
3146 }
3147 }
3148
3149#ifdef LEX_AT
3150 identifier_chars['@'] = '@';
32137342
NC
3151#endif
3152#ifdef LEX_QM
3153 identifier_chars['?'] = '?';
3154 operand_chars['?'] = '?';
252b5132 3155#endif
252b5132 3156 digit_chars['-'] = '-';
c0f3af97 3157 mnemonic_chars['_'] = '_';
791fe849 3158 mnemonic_chars['-'] = '-';
0003779b 3159 mnemonic_chars['.'] = '.';
252b5132
RH
3160 identifier_chars['_'] = '_';
3161 identifier_chars['.'] = '.';
3162
3163 for (p = operand_special_chars; *p != '\0'; p++)
3164 operand_chars[(unsigned char) *p] = *p;
3165 }
3166
a4447b93
RH
3167 if (flag_code == CODE_64BIT)
3168 {
ca19b261
KT
3169#if defined (OBJ_COFF) && defined (TE_PE)
3170 x86_dwarf2_return_column = (OUTPUT_FLAVOR == bfd_target_coff_flavour
3171 ? 32 : 16);
3172#else
a4447b93 3173 x86_dwarf2_return_column = 16;
ca19b261 3174#endif
61ff971f 3175 x86_cie_data_alignment = -8;
a4447b93
RH
3176 }
3177 else
3178 {
3179 x86_dwarf2_return_column = 8;
3180 x86_cie_data_alignment = -4;
3181 }
e379e5f3
L
3182
3183 /* NB: FUSED_JCC_PADDING frag must have sufficient room so that it
3184 can be turned into BRANCH_PREFIX frag. */
3185 if (align_branch_prefix_size > MAX_FUSED_JCC_PADDING_SIZE)
3186 abort ();
252b5132
RH
3187}
3188
3189void
e3bb37b5 3190i386_print_statistics (FILE *file)
252b5132 3191{
629310ab
ML
3192 htab_print_statistics (file, "i386 opcode", op_hash);
3193 htab_print_statistics (file, "i386 register", reg_hash);
252b5132
RH
3194}
3195\f
252b5132
RH
3196#ifdef DEBUG386
3197
ce8a8b2f 3198/* Debugging routines for md_assemble. */
d3ce72d0 3199static void pte (insn_template *);
40fb9820 3200static void pt (i386_operand_type);
e3bb37b5
L
3201static void pe (expressionS *);
3202static void ps (symbolS *);
252b5132
RH
3203
3204static void
2c703856 3205pi (const char *line, i386_insn *x)
252b5132 3206{
09137c09 3207 unsigned int j;
252b5132
RH
3208
3209 fprintf (stdout, "%s: template ", line);
3210 pte (&x->tm);
09f131f2
JH
3211 fprintf (stdout, " address: base %s index %s scale %x\n",
3212 x->base_reg ? x->base_reg->reg_name : "none",
3213 x->index_reg ? x->index_reg->reg_name : "none",
3214 x->log2_scale_factor);
3215 fprintf (stdout, " modrm: mode %x reg %x reg/mem %x\n",
252b5132 3216 x->rm.mode, x->rm.reg, x->rm.regmem);
09f131f2
JH
3217 fprintf (stdout, " sib: base %x index %x scale %x\n",
3218 x->sib.base, x->sib.index, x->sib.scale);
3219 fprintf (stdout, " rex: 64bit %x extX %x extY %x extZ %x\n",
161a04f6
L
3220 (x->rex & REX_W) != 0,
3221 (x->rex & REX_R) != 0,
3222 (x->rex & REX_X) != 0,
3223 (x->rex & REX_B) != 0);
09137c09 3224 for (j = 0; j < x->operands; j++)
252b5132 3225 {
09137c09
SP
3226 fprintf (stdout, " #%d: ", j + 1);
3227 pt (x->types[j]);
252b5132 3228 fprintf (stdout, "\n");
bab6aec1 3229 if (x->types[j].bitfield.class == Reg
3528c362
JB
3230 || x->types[j].bitfield.class == RegMMX
3231 || x->types[j].bitfield.class == RegSIMD
dd6b8a0b 3232 || x->types[j].bitfield.class == RegMask
00cee14f 3233 || x->types[j].bitfield.class == SReg
4a5c67ed
JB
3234 || x->types[j].bitfield.class == RegCR
3235 || x->types[j].bitfield.class == RegDR
dd6b8a0b
JB
3236 || x->types[j].bitfield.class == RegTR
3237 || x->types[j].bitfield.class == RegBND)
09137c09
SP
3238 fprintf (stdout, "%s\n", x->op[j].regs->reg_name);
3239 if (operand_type_check (x->types[j], imm))
3240 pe (x->op[j].imms);
3241 if (operand_type_check (x->types[j], disp))
3242 pe (x->op[j].disps);
252b5132
RH
3243 }
3244}
3245
3246static void
d3ce72d0 3247pte (insn_template *t)
252b5132 3248{
b933fa4b 3249 static const unsigned char opc_pfx[] = { 0, 0x66, 0xf3, 0xf2 };
441f6aca
JB
3250 static const char *const opc_spc[] = {
3251 NULL, "0f", "0f38", "0f3a", NULL, NULL, NULL, NULL,
3252 "XOP08", "XOP09", "XOP0A",
3253 };
09137c09 3254 unsigned int j;
441f6aca 3255
252b5132 3256 fprintf (stdout, " %d operands ", t->operands);
441f6aca
JB
3257 if (opc_pfx[t->opcode_modifier.opcodeprefix])
3258 fprintf (stdout, "pfx %x ", opc_pfx[t->opcode_modifier.opcodeprefix]);
3259 if (opc_spc[t->opcode_modifier.opcodespace])
3260 fprintf (stdout, "space %s ", opc_spc[t->opcode_modifier.opcodespace]);
47926f60 3261 fprintf (stdout, "opcode %x ", t->base_opcode);
252b5132
RH
3262 if (t->extension_opcode != None)
3263 fprintf (stdout, "ext %x ", t->extension_opcode);
40fb9820 3264 if (t->opcode_modifier.d)
252b5132 3265 fprintf (stdout, "D");
40fb9820 3266 if (t->opcode_modifier.w)
252b5132
RH
3267 fprintf (stdout, "W");
3268 fprintf (stdout, "\n");
09137c09 3269 for (j = 0; j < t->operands; j++)
252b5132 3270 {
09137c09
SP
3271 fprintf (stdout, " #%d type ", j + 1);
3272 pt (t->operand_types[j]);
252b5132
RH
3273 fprintf (stdout, "\n");
3274 }
3275}
3276
3277static void
e3bb37b5 3278pe (expressionS *e)
252b5132 3279{
24eab124 3280 fprintf (stdout, " operation %d\n", e->X_op);
b77ad1d4
AM
3281 fprintf (stdout, " add_number %ld (%lx)\n",
3282 (long) e->X_add_number, (long) e->X_add_number);
252b5132
RH
3283 if (e->X_add_symbol)
3284 {
3285 fprintf (stdout, " add_symbol ");
3286 ps (e->X_add_symbol);
3287 fprintf (stdout, "\n");
3288 }
3289 if (e->X_op_symbol)
3290 {
3291 fprintf (stdout, " op_symbol ");
3292 ps (e->X_op_symbol);
3293 fprintf (stdout, "\n");
3294 }
3295}
3296
3297static void
e3bb37b5 3298ps (symbolS *s)
252b5132
RH
3299{
3300 fprintf (stdout, "%s type %s%s",
3301 S_GET_NAME (s),
3302 S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
3303 segment_name (S_GET_SEGMENT (s)));
3304}
3305
7b81dfbb 3306static struct type_name
252b5132 3307 {
40fb9820
L
3308 i386_operand_type mask;
3309 const char *name;
252b5132 3310 }
7b81dfbb 3311const type_names[] =
252b5132 3312{
40fb9820
L
3313 { OPERAND_TYPE_REG8, "r8" },
3314 { OPERAND_TYPE_REG16, "r16" },
3315 { OPERAND_TYPE_REG32, "r32" },
3316 { OPERAND_TYPE_REG64, "r64" },
2c703856
JB
3317 { OPERAND_TYPE_ACC8, "acc8" },
3318 { OPERAND_TYPE_ACC16, "acc16" },
3319 { OPERAND_TYPE_ACC32, "acc32" },
3320 { OPERAND_TYPE_ACC64, "acc64" },
40fb9820
L
3321 { OPERAND_TYPE_IMM8, "i8" },
3322 { OPERAND_TYPE_IMM8, "i8s" },
3323 { OPERAND_TYPE_IMM16, "i16" },
3324 { OPERAND_TYPE_IMM32, "i32" },
3325 { OPERAND_TYPE_IMM32S, "i32s" },
3326 { OPERAND_TYPE_IMM64, "i64" },
3327 { OPERAND_TYPE_IMM1, "i1" },
3328 { OPERAND_TYPE_BASEINDEX, "BaseIndex" },
3329 { OPERAND_TYPE_DISP8, "d8" },
3330 { OPERAND_TYPE_DISP16, "d16" },
3331 { OPERAND_TYPE_DISP32, "d32" },
3332 { OPERAND_TYPE_DISP32S, "d32s" },
3333 { OPERAND_TYPE_DISP64, "d64" },
3334 { OPERAND_TYPE_INOUTPORTREG, "InOutPortReg" },
3335 { OPERAND_TYPE_SHIFTCOUNT, "ShiftCount" },
3336 { OPERAND_TYPE_CONTROL, "control reg" },
3337 { OPERAND_TYPE_TEST, "test reg" },
3338 { OPERAND_TYPE_DEBUG, "debug reg" },
3339 { OPERAND_TYPE_FLOATREG, "FReg" },
3340 { OPERAND_TYPE_FLOATACC, "FAcc" },
21df382b 3341 { OPERAND_TYPE_SREG, "SReg" },
40fb9820
L
3342 { OPERAND_TYPE_REGMMX, "rMMX" },
3343 { OPERAND_TYPE_REGXMM, "rXMM" },
0349dc08 3344 { OPERAND_TYPE_REGYMM, "rYMM" },
43234a1e 3345 { OPERAND_TYPE_REGZMM, "rZMM" },
260cd341 3346 { OPERAND_TYPE_REGTMM, "rTMM" },
43234a1e 3347 { OPERAND_TYPE_REGMASK, "Mask reg" },
252b5132
RH
3348};
3349
3350static void
40fb9820 3351pt (i386_operand_type t)
252b5132 3352{
40fb9820 3353 unsigned int j;
c6fb90c8 3354 i386_operand_type a;
252b5132 3355
40fb9820 3356 for (j = 0; j < ARRAY_SIZE (type_names); j++)
c6fb90c8
L
3357 {
3358 a = operand_type_and (t, type_names[j].mask);
2c703856 3359 if (operand_type_equal (&a, &type_names[j].mask))
c6fb90c8
L
3360 fprintf (stdout, "%s, ", type_names[j].name);
3361 }
252b5132
RH
3362 fflush (stdout);
3363}
3364
3365#endif /* DEBUG386 */
3366\f
252b5132 3367static bfd_reloc_code_real_type
3956db08 3368reloc (unsigned int size,
64e74474
AM
3369 int pcrel,
3370 int sign,
3371 bfd_reloc_code_real_type other)
252b5132 3372{
47926f60 3373 if (other != NO_RELOC)
3956db08 3374 {
91d6fa6a 3375 reloc_howto_type *rel;
3956db08
JB
3376
3377 if (size == 8)
3378 switch (other)
3379 {
64e74474
AM
3380 case BFD_RELOC_X86_64_GOT32:
3381 return BFD_RELOC_X86_64_GOT64;
3382 break;
553d1284
L
3383 case BFD_RELOC_X86_64_GOTPLT64:
3384 return BFD_RELOC_X86_64_GOTPLT64;
3385 break;
64e74474
AM
3386 case BFD_RELOC_X86_64_PLTOFF64:
3387 return BFD_RELOC_X86_64_PLTOFF64;
3388 break;
3389 case BFD_RELOC_X86_64_GOTPC32:
3390 other = BFD_RELOC_X86_64_GOTPC64;
3391 break;
3392 case BFD_RELOC_X86_64_GOTPCREL:
3393 other = BFD_RELOC_X86_64_GOTPCREL64;
3394 break;
3395 case BFD_RELOC_X86_64_TPOFF32:
3396 other = BFD_RELOC_X86_64_TPOFF64;
3397 break;
3398 case BFD_RELOC_X86_64_DTPOFF32:
3399 other = BFD_RELOC_X86_64_DTPOFF64;
3400 break;
3401 default:
3402 break;
3956db08 3403 }
e05278af 3404
8ce3d284 3405#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
3406 if (other == BFD_RELOC_SIZE32)
3407 {
3408 if (size == 8)
1ab668bf 3409 other = BFD_RELOC_SIZE64;
8fd4256d 3410 if (pcrel)
1ab668bf
AM
3411 {
3412 as_bad (_("there are no pc-relative size relocations"));
3413 return NO_RELOC;
3414 }
8fd4256d 3415 }
8ce3d284 3416#endif
8fd4256d 3417
e05278af 3418 /* Sign-checking 4-byte relocations in 16-/32-bit code is pointless. */
f2d8a97c 3419 if (size == 4 && (flag_code != CODE_64BIT || disallow_64bit_reloc))
e05278af
JB
3420 sign = -1;
3421
91d6fa6a
NC
3422 rel = bfd_reloc_type_lookup (stdoutput, other);
3423 if (!rel)
3956db08 3424 as_bad (_("unknown relocation (%u)"), other);
91d6fa6a 3425 else if (size != bfd_get_reloc_size (rel))
3956db08 3426 as_bad (_("%u-byte relocation cannot be applied to %u-byte field"),
91d6fa6a 3427 bfd_get_reloc_size (rel),
3956db08 3428 size);
91d6fa6a 3429 else if (pcrel && !rel->pc_relative)
3956db08 3430 as_bad (_("non-pc-relative relocation for pc-relative field"));
91d6fa6a 3431 else if ((rel->complain_on_overflow == complain_overflow_signed
3956db08 3432 && !sign)
91d6fa6a 3433 || (rel->complain_on_overflow == complain_overflow_unsigned
64e74474 3434 && sign > 0))
3956db08
JB
3435 as_bad (_("relocated field and relocation type differ in signedness"));
3436 else
3437 return other;
3438 return NO_RELOC;
3439 }
252b5132
RH
3440
3441 if (pcrel)
3442 {
3e73aa7c 3443 if (!sign)
3956db08 3444 as_bad (_("there are no unsigned pc-relative relocations"));
252b5132
RH
3445 switch (size)
3446 {
3447 case 1: return BFD_RELOC_8_PCREL;
3448 case 2: return BFD_RELOC_16_PCREL;
d258b828 3449 case 4: return BFD_RELOC_32_PCREL;
d6ab8113 3450 case 8: return BFD_RELOC_64_PCREL;
252b5132 3451 }
3956db08 3452 as_bad (_("cannot do %u byte pc-relative relocation"), size);
252b5132
RH
3453 }
3454 else
3455 {
3956db08 3456 if (sign > 0)
e5cb08ac 3457 switch (size)
3e73aa7c
JH
3458 {
3459 case 4: return BFD_RELOC_X86_64_32S;
3460 }
3461 else
3462 switch (size)
3463 {
3464 case 1: return BFD_RELOC_8;
3465 case 2: return BFD_RELOC_16;
3466 case 4: return BFD_RELOC_32;
3467 case 8: return BFD_RELOC_64;
3468 }
3956db08
JB
3469 as_bad (_("cannot do %s %u byte relocation"),
3470 sign > 0 ? "signed" : "unsigned", size);
252b5132
RH
3471 }
3472
0cc9e1d3 3473 return NO_RELOC;
252b5132
RH
3474}
3475
47926f60
KH
3476/* Here we decide which fixups can be adjusted to make them relative to
3477 the beginning of the section instead of the symbol. Basically we need
3478 to make sure that the dynamic relocations are done correctly, so in
3479 some cases we force the original symbol to be used. */
3480
252b5132 3481int
e3bb37b5 3482tc_i386_fix_adjustable (fixS *fixP ATTRIBUTE_UNUSED)
252b5132 3483{
6d249963 3484#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 3485 if (!IS_ELF)
31312f95
AM
3486 return 1;
3487
a161fe53
AM
3488 /* Don't adjust pc-relative references to merge sections in 64-bit
3489 mode. */
3490 if (use_rela_relocations
3491 && (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0
3492 && fixP->fx_pcrel)
252b5132 3493 return 0;
31312f95 3494
8d01d9a9
AJ
3495 /* The x86_64 GOTPCREL are represented as 32bit PCrel relocations
3496 and changed later by validate_fix. */
3497 if (GOT_symbol && fixP->fx_subsy == GOT_symbol
3498 && fixP->fx_r_type == BFD_RELOC_32_PCREL)
3499 return 0;
3500
8fd4256d
L
3501 /* Adjust_reloc_syms doesn't know about the GOT. Need to keep symbol
3502 for size relocations. */
3503 if (fixP->fx_r_type == BFD_RELOC_SIZE32
3504 || fixP->fx_r_type == BFD_RELOC_SIZE64
3505 || fixP->fx_r_type == BFD_RELOC_386_GOTOFF
252b5132 3506 || fixP->fx_r_type == BFD_RELOC_386_GOT32
02a86693 3507 || fixP->fx_r_type == BFD_RELOC_386_GOT32X
13ae64f3
JJ
3508 || fixP->fx_r_type == BFD_RELOC_386_TLS_GD
3509 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDM
3510 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDO_32
3511 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE_32
37e55690
JJ
3512 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE
3513 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTIE
13ae64f3
JJ
3514 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE_32
3515 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE
67a4f2b7
AO
3516 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTDESC
3517 || fixP->fx_r_type == BFD_RELOC_386_TLS_DESC_CALL
3e73aa7c 3518 || fixP->fx_r_type == BFD_RELOC_X86_64_GOT32
80b3ee89 3519 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCREL
56ceb5b5
L
3520 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCRELX
3521 || fixP->fx_r_type == BFD_RELOC_X86_64_REX_GOTPCRELX
bffbf940
JJ
3522 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSGD
3523 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSLD
3524 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF32
d6ab8113 3525 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF64
bffbf940
JJ
3526 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF
3527 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF32
d6ab8113
JB
3528 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF64
3529 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTOFF64
67a4f2b7
AO
3530 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
3531 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSDESC_CALL
252b5132
RH
3532 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3533 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3534 return 0;
31312f95 3535#endif
252b5132
RH
3536 return 1;
3537}
252b5132 3538
b4cac588 3539static int
e3bb37b5 3540intel_float_operand (const char *mnemonic)
252b5132 3541{
9306ca4a
JB
3542 /* Note that the value returned is meaningful only for opcodes with (memory)
3543 operands, hence the code here is free to improperly handle opcodes that
3544 have no operands (for better performance and smaller code). */
3545
3546 if (mnemonic[0] != 'f')
3547 return 0; /* non-math */
3548
3549 switch (mnemonic[1])
3550 {
3551 /* fclex, fdecstp, fdisi, femms, feni, fincstp, finit, fsetpm, and
3552 the fs segment override prefix not currently handled because no
3553 call path can make opcodes without operands get here */
3554 case 'i':
3555 return 2 /* integer op */;
3556 case 'l':
3557 if (mnemonic[2] == 'd' && (mnemonic[3] == 'c' || mnemonic[3] == 'e'))
3558 return 3; /* fldcw/fldenv */
3559 break;
3560 case 'n':
3561 if (mnemonic[2] != 'o' /* fnop */)
3562 return 3; /* non-waiting control op */
3563 break;
3564 case 'r':
3565 if (mnemonic[2] == 's')
3566 return 3; /* frstor/frstpm */
3567 break;
3568 case 's':
3569 if (mnemonic[2] == 'a')
3570 return 3; /* fsave */
3571 if (mnemonic[2] == 't')
3572 {
3573 switch (mnemonic[3])
3574 {
3575 case 'c': /* fstcw */
3576 case 'd': /* fstdw */
3577 case 'e': /* fstenv */
3578 case 's': /* fsts[gw] */
3579 return 3;
3580 }
3581 }
3582 break;
3583 case 'x':
3584 if (mnemonic[2] == 'r' || mnemonic[2] == 's')
3585 return 0; /* fxsave/fxrstor are not really math ops */
3586 break;
3587 }
252b5132 3588
9306ca4a 3589 return 1;
252b5132
RH
3590}
3591
9a182d04
JB
3592static INLINE void
3593install_template (const insn_template *t)
3594{
3595 unsigned int l;
3596
3597 i.tm = *t;
3598
3599 /* Note that for pseudo prefixes this produces a length of 1. But for them
3600 the length isn't interesting at all. */
3601 for (l = 1; l < 4; ++l)
3602 if (!(t->base_opcode >> (8 * l)))
3603 break;
3604
3605 i.opcode_length = l;
3606}
3607
c0f3af97
L
3608/* Build the VEX prefix. */
3609
3610static void
d3ce72d0 3611build_vex_prefix (const insn_template *t)
c0f3af97
L
3612{
3613 unsigned int register_specifier;
c0f3af97 3614 unsigned int vector_length;
03751133 3615 unsigned int w;
c0f3af97
L
3616
3617 /* Check register specifier. */
3618 if (i.vex.register_specifier)
43234a1e
L
3619 {
3620 register_specifier =
3621 ~register_number (i.vex.register_specifier) & 0xf;
3622 gas_assert ((i.vex.register_specifier->reg_flags & RegVRex) == 0);
3623 }
c0f3af97
L
3624 else
3625 register_specifier = 0xf;
3626
79f0fa25
L
3627 /* Use 2-byte VEX prefix by swapping destination and source operand
3628 if there are more than 1 register operand. */
3629 if (i.reg_operands > 1
3630 && i.vec_encoding != vex_encoding_vex3
86fa6981 3631 && i.dir_encoding == dir_encoding_default
fa99fab2 3632 && i.operands == i.reg_operands
dbbc8b7e 3633 && operand_type_equal (&i.types[0], &i.types[i.operands - 1])
441f6aca 3634 && i.tm.opcode_modifier.opcodespace == SPACE_0F
dbbc8b7e 3635 && (i.tm.opcode_modifier.load || i.tm.opcode_modifier.d)
fa99fab2
L
3636 && i.rex == REX_B)
3637 {
3638 unsigned int xchg = i.operands - 1;
3639 union i386_op temp_op;
3640 i386_operand_type temp_type;
3641
3642 temp_type = i.types[xchg];
3643 i.types[xchg] = i.types[0];
3644 i.types[0] = temp_type;
3645 temp_op = i.op[xchg];
3646 i.op[xchg] = i.op[0];
3647 i.op[0] = temp_op;
3648
9c2799c2 3649 gas_assert (i.rm.mode == 3);
fa99fab2
L
3650
3651 i.rex = REX_R;
3652 xchg = i.rm.regmem;
3653 i.rm.regmem = i.rm.reg;
3654 i.rm.reg = xchg;
3655
dbbc8b7e
JB
3656 if (i.tm.opcode_modifier.d)
3657 i.tm.base_opcode ^= (i.tm.base_opcode & 0xee) != 0x6e
3658 ? Opcode_SIMD_FloatD : Opcode_SIMD_IntD;
3659 else /* Use the next insn. */
9a182d04 3660 install_template (&t[1]);
fa99fab2
L
3661 }
3662
79dec6b7
JB
3663 /* Use 2-byte VEX prefix by swapping commutative source operands if there
3664 are no memory operands and at least 3 register ones. */
3665 if (i.reg_operands >= 3
3666 && i.vec_encoding != vex_encoding_vex3
3667 && i.reg_operands == i.operands - i.imm_operands
3668 && i.tm.opcode_modifier.vex
3669 && i.tm.opcode_modifier.commutative
3670 && (i.tm.opcode_modifier.sse2avx || optimize > 1)
3671 && i.rex == REX_B
3672 && i.vex.register_specifier
3673 && !(i.vex.register_specifier->reg_flags & RegRex))
3674 {
3675 unsigned int xchg = i.operands - i.reg_operands;
3676 union i386_op temp_op;
3677 i386_operand_type temp_type;
3678
441f6aca 3679 gas_assert (i.tm.opcode_modifier.opcodespace == SPACE_0F);
79dec6b7
JB
3680 gas_assert (!i.tm.opcode_modifier.sae);
3681 gas_assert (operand_type_equal (&i.types[i.operands - 2],
3682 &i.types[i.operands - 3]));
3683 gas_assert (i.rm.mode == 3);
3684
3685 temp_type = i.types[xchg];
3686 i.types[xchg] = i.types[xchg + 1];
3687 i.types[xchg + 1] = temp_type;
3688 temp_op = i.op[xchg];
3689 i.op[xchg] = i.op[xchg + 1];
3690 i.op[xchg + 1] = temp_op;
3691
3692 i.rex = 0;
3693 xchg = i.rm.regmem | 8;
3694 i.rm.regmem = ~register_specifier & 0xf;
3695 gas_assert (!(i.rm.regmem & 8));
3696 i.vex.register_specifier += xchg - i.rm.regmem;
3697 register_specifier = ~xchg & 0xf;
3698 }
3699
539f890d
L
3700 if (i.tm.opcode_modifier.vex == VEXScalar)
3701 vector_length = avxscalar;
10c17abd
JB
3702 else if (i.tm.opcode_modifier.vex == VEX256)
3703 vector_length = 1;
539f890d 3704 else
10c17abd 3705 {
56522fc5 3706 unsigned int op;
10c17abd 3707
c7213af9
L
3708 /* Determine vector length from the last multi-length vector
3709 operand. */
10c17abd 3710 vector_length = 0;
56522fc5 3711 for (op = t->operands; op--;)
10c17abd
JB
3712 if (t->operand_types[op].bitfield.xmmword
3713 && t->operand_types[op].bitfield.ymmword
3714 && i.types[op].bitfield.ymmword)
3715 {
3716 vector_length = 1;
3717 break;
3718 }
3719 }
c0f3af97 3720
03751133
L
3721 /* Check the REX.W bit and VEXW. */
3722 if (i.tm.opcode_modifier.vexw == VEXWIG)
3723 w = (vexwig == vexw1 || (i.rex & REX_W)) ? 1 : 0;
3724 else if (i.tm.opcode_modifier.vexw)
3725 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3726 else
931d03b7 3727 w = (flag_code == CODE_64BIT ? i.rex & REX_W : vexwig == vexw1) ? 1 : 0;
03751133 3728
c0f3af97 3729 /* Use 2-byte VEX prefix if possible. */
03751133
L
3730 if (w == 0
3731 && i.vec_encoding != vex_encoding_vex3
441f6aca 3732 && i.tm.opcode_modifier.opcodespace == SPACE_0F
c0f3af97
L
3733 && (i.rex & (REX_W | REX_X | REX_B)) == 0)
3734 {
3735 /* 2-byte VEX prefix. */
3736 unsigned int r;
3737
3738 i.vex.length = 2;
3739 i.vex.bytes[0] = 0xc5;
3740
3741 /* Check the REX.R bit. */
3742 r = (i.rex & REX_R) ? 0 : 1;
3743 i.vex.bytes[1] = (r << 7
3744 | register_specifier << 3
3745 | vector_length << 2
35648716 3746 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3747 }
3748 else
3749 {
3750 /* 3-byte VEX prefix. */
f88c9eb0 3751 i.vex.length = 3;
f88c9eb0 3752
441f6aca 3753 switch (i.tm.opcode_modifier.opcodespace)
5dd85c99 3754 {
441f6aca
JB
3755 case SPACE_0F:
3756 case SPACE_0F38:
3757 case SPACE_0F3A:
80de6e00 3758 i.vex.bytes[0] = 0xc4;
7f399153 3759 break;
441f6aca
JB
3760 case SPACE_XOP08:
3761 case SPACE_XOP09:
3762 case SPACE_XOP0A:
f88c9eb0 3763 i.vex.bytes[0] = 0x8f;
7f399153
L
3764 break;
3765 default:
3766 abort ();
f88c9eb0 3767 }
c0f3af97 3768
c0f3af97
L
3769 /* The high 3 bits of the second VEX byte are 1's compliment
3770 of RXB bits from REX. */
441f6aca 3771 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | i.tm.opcode_modifier.opcodespace;
c0f3af97 3772
c0f3af97
L
3773 i.vex.bytes[2] = (w << 7
3774 | register_specifier << 3
3775 | vector_length << 2
35648716 3776 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3777 }
3778}
3779
e771e7c9
JB
3780static INLINE bfd_boolean
3781is_evex_encoding (const insn_template *t)
3782{
7091c612 3783 return t->opcode_modifier.evex || t->opcode_modifier.disp8memshift
e771e7c9 3784 || t->opcode_modifier.broadcast || t->opcode_modifier.masking
a80195f1 3785 || t->opcode_modifier.sae;
e771e7c9
JB
3786}
3787
7a8655d2
JB
3788static INLINE bfd_boolean
3789is_any_vex_encoding (const insn_template *t)
3790{
7b47a312 3791 return t->opcode_modifier.vex || is_evex_encoding (t);
7a8655d2
JB
3792}
3793
43234a1e
L
3794/* Build the EVEX prefix. */
3795
3796static void
3797build_evex_prefix (void)
3798{
35648716 3799 unsigned int register_specifier, w;
43234a1e
L
3800 rex_byte vrex_used = 0;
3801
3802 /* Check register specifier. */
3803 if (i.vex.register_specifier)
3804 {
3805 gas_assert ((i.vrex & REX_X) == 0);
3806
3807 register_specifier = i.vex.register_specifier->reg_num;
3808 if ((i.vex.register_specifier->reg_flags & RegRex))
3809 register_specifier += 8;
3810 /* The upper 16 registers are encoded in the fourth byte of the
3811 EVEX prefix. */
3812 if (!(i.vex.register_specifier->reg_flags & RegVRex))
3813 i.vex.bytes[3] = 0x8;
3814 register_specifier = ~register_specifier & 0xf;
3815 }
3816 else
3817 {
3818 register_specifier = 0xf;
3819
3820 /* Encode upper 16 vector index register in the fourth byte of
3821 the EVEX prefix. */
3822 if (!(i.vrex & REX_X))
3823 i.vex.bytes[3] = 0x8;
3824 else
3825 vrex_used |= REX_X;
3826 }
3827
43234a1e
L
3828 /* 4 byte EVEX prefix. */
3829 i.vex.length = 4;
3830 i.vex.bytes[0] = 0x62;
3831
43234a1e
L
3832 /* The high 3 bits of the second EVEX byte are 1's compliment of RXB
3833 bits from REX. */
441f6aca
JB
3834 gas_assert (i.tm.opcode_modifier.opcodespace >= SPACE_0F);
3835 gas_assert (i.tm.opcode_modifier.opcodespace <= SPACE_0F3A);
3836 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | i.tm.opcode_modifier.opcodespace;
43234a1e
L
3837
3838 /* The fifth bit of the second EVEX byte is 1's compliment of the
3839 REX_R bit in VREX. */
3840 if (!(i.vrex & REX_R))
3841 i.vex.bytes[1] |= 0x10;
3842 else
3843 vrex_used |= REX_R;
3844
3845 if ((i.reg_operands + i.imm_operands) == i.operands)
3846 {
3847 /* When all operands are registers, the REX_X bit in REX is not
3848 used. We reuse it to encode the upper 16 registers, which is
3849 indicated by the REX_B bit in VREX. The REX_X bit is encoded
3850 as 1's compliment. */
3851 if ((i.vrex & REX_B))
3852 {
3853 vrex_used |= REX_B;
3854 i.vex.bytes[1] &= ~0x40;
3855 }
3856 }
3857
3858 /* EVEX instructions shouldn't need the REX prefix. */
3859 i.vrex &= ~vrex_used;
3860 gas_assert (i.vrex == 0);
3861
6865c043
L
3862 /* Check the REX.W bit and VEXW. */
3863 if (i.tm.opcode_modifier.vexw == VEXWIG)
3864 w = (evexwig == evexw1 || (i.rex & REX_W)) ? 1 : 0;
3865 else if (i.tm.opcode_modifier.vexw)
3866 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3867 else
931d03b7 3868 w = (flag_code == CODE_64BIT ? i.rex & REX_W : evexwig == evexw1) ? 1 : 0;
43234a1e 3869
43234a1e 3870 /* The third byte of the EVEX prefix. */
35648716
JB
3871 i.vex.bytes[2] = ((w << 7)
3872 | (register_specifier << 3)
3873 | 4 /* Encode the U bit. */
3874 | i.tm.opcode_modifier.opcodeprefix);
43234a1e
L
3875
3876 /* The fourth byte of the EVEX prefix. */
3877 /* The zeroing-masking bit. */
6225c532 3878 if (i.mask.reg && i.mask.zeroing)
43234a1e
L
3879 i.vex.bytes[3] |= 0x80;
3880
3881 /* Don't always set the broadcast bit if there is no RC. */
3882 if (!i.rounding)
3883 {
3884 /* Encode the vector length. */
3885 unsigned int vec_length;
3886
e771e7c9
JB
3887 if (!i.tm.opcode_modifier.evex
3888 || i.tm.opcode_modifier.evex == EVEXDYN)
3889 {
56522fc5 3890 unsigned int op;
e771e7c9 3891
c7213af9
L
3892 /* Determine vector length from the last multi-length vector
3893 operand. */
56522fc5 3894 for (op = i.operands; op--;)
e771e7c9
JB
3895 if (i.tm.operand_types[op].bitfield.xmmword
3896 + i.tm.operand_types[op].bitfield.ymmword
3897 + i.tm.operand_types[op].bitfield.zmmword > 1)
3898 {
3899 if (i.types[op].bitfield.zmmword)
c7213af9
L
3900 {
3901 i.tm.opcode_modifier.evex = EVEX512;
3902 break;
3903 }
e771e7c9 3904 else if (i.types[op].bitfield.ymmword)
c7213af9
L
3905 {
3906 i.tm.opcode_modifier.evex = EVEX256;
3907 break;
3908 }
e771e7c9 3909 else if (i.types[op].bitfield.xmmword)
c7213af9
L
3910 {
3911 i.tm.opcode_modifier.evex = EVEX128;
3912 break;
3913 }
783c187b 3914 else if (i.broadcast && op == i.broadcast->operand)
625cbd7a 3915 {
4a1b91ea 3916 switch (i.broadcast->bytes)
625cbd7a
JB
3917 {
3918 case 64:
3919 i.tm.opcode_modifier.evex = EVEX512;
3920 break;
3921 case 32:
3922 i.tm.opcode_modifier.evex = EVEX256;
3923 break;
3924 case 16:
3925 i.tm.opcode_modifier.evex = EVEX128;
3926 break;
3927 default:
c7213af9 3928 abort ();
625cbd7a 3929 }
c7213af9 3930 break;
625cbd7a 3931 }
e771e7c9 3932 }
c7213af9 3933
56522fc5 3934 if (op >= MAX_OPERANDS)
c7213af9 3935 abort ();
e771e7c9
JB
3936 }
3937
43234a1e
L
3938 switch (i.tm.opcode_modifier.evex)
3939 {
3940 case EVEXLIG: /* LL' is ignored */
3941 vec_length = evexlig << 5;
3942 break;
3943 case EVEX128:
3944 vec_length = 0 << 5;
3945 break;
3946 case EVEX256:
3947 vec_length = 1 << 5;
3948 break;
3949 case EVEX512:
3950 vec_length = 2 << 5;
3951 break;
3952 default:
3953 abort ();
3954 break;
3955 }
3956 i.vex.bytes[3] |= vec_length;
3957 /* Encode the broadcast bit. */
3958 if (i.broadcast)
3959 i.vex.bytes[3] |= 0x10;
3960 }
3961 else
3962 {
3963 if (i.rounding->type != saeonly)
3964 i.vex.bytes[3] |= 0x10 | (i.rounding->type << 5);
3965 else
d3d3c6db 3966 i.vex.bytes[3] |= 0x10 | (evexrcig << 5);
43234a1e
L
3967 }
3968
6225c532
JB
3969 if (i.mask.reg)
3970 i.vex.bytes[3] |= i.mask.reg->reg_num;
43234a1e
L
3971}
3972
65da13b5
L
3973static void
3974process_immext (void)
3975{
3976 expressionS *exp;
3977
c0f3af97 3978 /* These AMD 3DNow! and SSE2 instructions have an opcode suffix
65da13b5
L
3979 which is coded in the same place as an 8-bit immediate field
3980 would be. Here we fake an 8-bit immediate operand from the
3981 opcode suffix stored in tm.extension_opcode.
3982
c1e679ec 3983 AVX instructions also use this encoding, for some of
c0f3af97 3984 3 argument instructions. */
65da13b5 3985
43234a1e 3986 gas_assert (i.imm_operands <= 1
7ab9ffdd 3987 && (i.operands <= 2
7a8655d2 3988 || (is_any_vex_encoding (&i.tm)
7ab9ffdd 3989 && i.operands <= 4)));
65da13b5
L
3990
3991 exp = &im_expressions[i.imm_operands++];
3992 i.op[i.operands].imms = exp;
3993 i.types[i.operands] = imm8;
3994 i.operands++;
3995 exp->X_op = O_constant;
3996 exp->X_add_number = i.tm.extension_opcode;
3997 i.tm.extension_opcode = None;
3998}
3999
42164a71
L
4000
4001static int
4002check_hle (void)
4003{
742732c7 4004 switch (i.tm.opcode_modifier.prefixok)
42164a71
L
4005 {
4006 default:
4007 abort ();
742732c7
JB
4008 case PrefixLock:
4009 case PrefixNone:
4010 case PrefixNoTrack:
4011 case PrefixRep:
165de32a
L
4012 as_bad (_("invalid instruction `%s' after `%s'"),
4013 i.tm.name, i.hle_prefix);
42164a71 4014 return 0;
742732c7 4015 case PrefixHLELock:
42164a71
L
4016 if (i.prefix[LOCK_PREFIX])
4017 return 1;
165de32a 4018 as_bad (_("missing `lock' with `%s'"), i.hle_prefix);
42164a71 4019 return 0;
742732c7 4020 case PrefixHLEAny:
42164a71 4021 return 1;
742732c7 4022 case PrefixHLERelease:
42164a71
L
4023 if (i.prefix[HLE_PREFIX] != XRELEASE_PREFIX_OPCODE)
4024 {
4025 as_bad (_("instruction `%s' after `xacquire' not allowed"),
4026 i.tm.name);
4027 return 0;
4028 }
8dc0818e 4029 if (i.mem_operands == 0 || !(i.flags[i.operands - 1] & Operand_Mem))
42164a71
L
4030 {
4031 as_bad (_("memory destination needed for instruction `%s'"
4032 " after `xrelease'"), i.tm.name);
4033 return 0;
4034 }
4035 return 1;
4036 }
4037}
4038
b6f8c7c4
L
4039/* Try the shortest encoding by shortening operand size. */
4040
4041static void
4042optimize_encoding (void)
4043{
a0a1771e 4044 unsigned int j;
b6f8c7c4
L
4045
4046 if (optimize_for_space
389d00a5 4047 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
b6f8c7c4
L
4048 && i.reg_operands == 1
4049 && i.imm_operands == 1
4050 && !i.types[1].bitfield.byte
4051 && i.op[0].imms->X_op == O_constant
4052 && fits_in_imm7 (i.op[0].imms->X_add_number)
72aea328 4053 && (i.tm.base_opcode == 0xa8
b6f8c7c4
L
4054 || (i.tm.base_opcode == 0xf6
4055 && i.tm.extension_opcode == 0x0)))
4056 {
4057 /* Optimize: -Os:
4058 test $imm7, %r64/%r32/%r16 -> test $imm7, %r8
4059 */
4060 unsigned int base_regnum = i.op[1].regs->reg_num;
4061 if (flag_code == CODE_64BIT || base_regnum < 4)
4062 {
4063 i.types[1].bitfield.byte = 1;
4064 /* Ignore the suffix. */
4065 i.suffix = 0;
7697afb6
JB
4066 /* Convert to byte registers. */
4067 if (i.types[1].bitfield.word)
4068 j = 16;
4069 else if (i.types[1].bitfield.dword)
4070 j = 32;
4071 else
4072 j = 48;
4073 if (!(i.op[1].regs->reg_flags & RegRex) && base_regnum < 4)
4074 j += 8;
4075 i.op[1].regs -= j;
b6f8c7c4
L
4076 }
4077 }
4078 else if (flag_code == CODE_64BIT
389d00a5 4079 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
d3d50934
L
4080 && ((i.types[1].bitfield.qword
4081 && i.reg_operands == 1
b6f8c7c4
L
4082 && i.imm_operands == 1
4083 && i.op[0].imms->X_op == O_constant
507916b8 4084 && ((i.tm.base_opcode == 0xb8
b6f8c7c4
L
4085 && i.tm.extension_opcode == None
4086 && fits_in_unsigned_long (i.op[0].imms->X_add_number))
4087 || (fits_in_imm31 (i.op[0].imms->X_add_number)
72aea328
JB
4088 && ((i.tm.base_opcode == 0x24
4089 || i.tm.base_opcode == 0xa8)
b6f8c7c4
L
4090 || (i.tm.base_opcode == 0x80
4091 && i.tm.extension_opcode == 0x4)
4092 || ((i.tm.base_opcode == 0xf6
507916b8 4093 || (i.tm.base_opcode | 1) == 0xc7)
b8364fa7
JB
4094 && i.tm.extension_opcode == 0x0)))
4095 || (fits_in_imm7 (i.op[0].imms->X_add_number)
4096 && i.tm.base_opcode == 0x83
4097 && i.tm.extension_opcode == 0x4)))
d3d50934
L
4098 || (i.types[0].bitfield.qword
4099 && ((i.reg_operands == 2
4100 && i.op[0].regs == i.op[1].regs
72aea328
JB
4101 && (i.tm.base_opcode == 0x30
4102 || i.tm.base_opcode == 0x28))
d3d50934
L
4103 || (i.reg_operands == 1
4104 && i.operands == 1
72aea328 4105 && i.tm.base_opcode == 0x30)))))
b6f8c7c4
L
4106 {
4107 /* Optimize: -O:
4108 andq $imm31, %r64 -> andl $imm31, %r32
b8364fa7 4109 andq $imm7, %r64 -> andl $imm7, %r32
b6f8c7c4
L
4110 testq $imm31, %r64 -> testl $imm31, %r32
4111 xorq %r64, %r64 -> xorl %r32, %r32
4112 subq %r64, %r64 -> subl %r32, %r32
4113 movq $imm31, %r64 -> movl $imm31, %r32
4114 movq $imm32, %r64 -> movl $imm32, %r32
4115 */
4116 i.tm.opcode_modifier.norex64 = 1;
507916b8 4117 if (i.tm.base_opcode == 0xb8 || (i.tm.base_opcode | 1) == 0xc7)
b6f8c7c4
L
4118 {
4119 /* Handle
4120 movq $imm31, %r64 -> movl $imm31, %r32
4121 movq $imm32, %r64 -> movl $imm32, %r32
4122 */
4123 i.tm.operand_types[0].bitfield.imm32 = 1;
4124 i.tm.operand_types[0].bitfield.imm32s = 0;
4125 i.tm.operand_types[0].bitfield.imm64 = 0;
4126 i.types[0].bitfield.imm32 = 1;
4127 i.types[0].bitfield.imm32s = 0;
4128 i.types[0].bitfield.imm64 = 0;
4129 i.types[1].bitfield.dword = 1;
4130 i.types[1].bitfield.qword = 0;
507916b8 4131 if ((i.tm.base_opcode | 1) == 0xc7)
b6f8c7c4
L
4132 {
4133 /* Handle
4134 movq $imm31, %r64 -> movl $imm31, %r32
4135 */
507916b8 4136 i.tm.base_opcode = 0xb8;
b6f8c7c4 4137 i.tm.extension_opcode = None;
507916b8 4138 i.tm.opcode_modifier.w = 0;
b6f8c7c4
L
4139 i.tm.opcode_modifier.modrm = 0;
4140 }
4141 }
4142 }
5641ec01
JB
4143 else if (optimize > 1
4144 && !optimize_for_space
389d00a5 4145 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
5641ec01
JB
4146 && i.reg_operands == 2
4147 && i.op[0].regs == i.op[1].regs
4148 && ((i.tm.base_opcode & ~(Opcode_D | 1)) == 0x8
4149 || (i.tm.base_opcode & ~(Opcode_D | 1)) == 0x20)
4150 && (flag_code != CODE_64BIT || !i.types[0].bitfield.dword))
4151 {
4152 /* Optimize: -O2:
4153 andb %rN, %rN -> testb %rN, %rN
4154 andw %rN, %rN -> testw %rN, %rN
4155 andq %rN, %rN -> testq %rN, %rN
4156 orb %rN, %rN -> testb %rN, %rN
4157 orw %rN, %rN -> testw %rN, %rN
4158 orq %rN, %rN -> testq %rN, %rN
4159
4160 and outside of 64-bit mode
4161
4162 andl %rN, %rN -> testl %rN, %rN
4163 orl %rN, %rN -> testl %rN, %rN
4164 */
4165 i.tm.base_opcode = 0x84 | (i.tm.base_opcode & 1);
4166 }
99112332 4167 else if (i.reg_operands == 3
b6f8c7c4
L
4168 && i.op[0].regs == i.op[1].regs
4169 && !i.types[2].bitfield.xmmword
4170 && (i.tm.opcode_modifier.vex
6225c532 4171 || ((!i.mask.reg || i.mask.zeroing)
b6f8c7c4 4172 && !i.rounding
e771e7c9 4173 && is_evex_encoding (&i.tm)
80c34c38 4174 && (i.vec_encoding != vex_encoding_evex
dd22218c 4175 || cpu_arch_isa_flags.bitfield.cpuavx512vl
80c34c38 4176 || i.tm.cpu_flags.bitfield.cpuavx512vl
7091c612 4177 || (i.tm.operand_types[2].bitfield.zmmword
dd22218c 4178 && i.types[2].bitfield.ymmword))))
b6f8c7c4 4179 && ((i.tm.base_opcode == 0x55
b6f8c7c4 4180 || i.tm.base_opcode == 0x57
35648716
JB
4181 || i.tm.base_opcode == 0xdf
4182 || i.tm.base_opcode == 0xef
4183 || i.tm.base_opcode == 0xf8
4184 || i.tm.base_opcode == 0xf9
4185 || i.tm.base_opcode == 0xfa
4186 || i.tm.base_opcode == 0xfb
1424ad86 4187 || i.tm.base_opcode == 0x42
35648716 4188 || i.tm.base_opcode == 0x47)
b6f8c7c4
L
4189 && i.tm.extension_opcode == None))
4190 {
99112332 4191 /* Optimize: -O1:
8305403a
L
4192 VOP, one of vandnps, vandnpd, vxorps, vxorpd, vpsubb, vpsubd,
4193 vpsubq and vpsubw:
b6f8c7c4
L
4194 EVEX VOP %zmmM, %zmmM, %zmmN
4195 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4196 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4197 EVEX VOP %ymmM, %ymmM, %ymmN
4198 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4199 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4200 VEX VOP %ymmM, %ymmM, %ymmN
4201 -> VEX VOP %xmmM, %xmmM, %xmmN
4202 VOP, one of vpandn and vpxor:
4203 VEX VOP %ymmM, %ymmM, %ymmN
4204 -> VEX VOP %xmmM, %xmmM, %xmmN
4205 VOP, one of vpandnd and vpandnq:
4206 EVEX VOP %zmmM, %zmmM, %zmmN
4207 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4208 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4209 EVEX VOP %ymmM, %ymmM, %ymmN
4210 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4211 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4212 VOP, one of vpxord and vpxorq:
4213 EVEX VOP %zmmM, %zmmM, %zmmN
4214 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4215 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4216 EVEX VOP %ymmM, %ymmM, %ymmN
4217 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4218 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
1424ad86
JB
4219 VOP, one of kxord and kxorq:
4220 VEX VOP %kM, %kM, %kN
4221 -> VEX kxorw %kM, %kM, %kN
4222 VOP, one of kandnd and kandnq:
4223 VEX VOP %kM, %kM, %kN
4224 -> VEX kandnw %kM, %kM, %kN
b6f8c7c4 4225 */
e771e7c9 4226 if (is_evex_encoding (&i.tm))
b6f8c7c4 4227 {
7b1d7ca1 4228 if (i.vec_encoding != vex_encoding_evex)
b6f8c7c4
L
4229 {
4230 i.tm.opcode_modifier.vex = VEX128;
4231 i.tm.opcode_modifier.vexw = VEXW0;
4232 i.tm.opcode_modifier.evex = 0;
4233 }
7b1d7ca1 4234 else if (optimize > 1)
dd22218c
L
4235 i.tm.opcode_modifier.evex = EVEX128;
4236 else
4237 return;
b6f8c7c4 4238 }
f74a6307 4239 else if (i.tm.operand_types[0].bitfield.class == RegMask)
1424ad86 4240 {
35648716 4241 i.tm.opcode_modifier.opcodeprefix = PREFIX_NONE;
1424ad86
JB
4242 i.tm.opcode_modifier.vexw = VEXW0;
4243 }
b6f8c7c4
L
4244 else
4245 i.tm.opcode_modifier.vex = VEX128;
4246
4247 if (i.tm.opcode_modifier.vex)
4248 for (j = 0; j < 3; j++)
4249 {
4250 i.types[j].bitfield.xmmword = 1;
4251 i.types[j].bitfield.ymmword = 0;
4252 }
4253 }
392a5972 4254 else if (i.vec_encoding != vex_encoding_evex
97ed31ae 4255 && !i.types[0].bitfield.zmmword
392a5972 4256 && !i.types[1].bitfield.zmmword
6225c532 4257 && !i.mask.reg
a0a1771e 4258 && !i.broadcast
97ed31ae 4259 && is_evex_encoding (&i.tm)
35648716
JB
4260 && ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4261 || (i.tm.base_opcode & ~4) == 0xdb
4262 || (i.tm.base_opcode & ~4) == 0xeb)
97ed31ae
L
4263 && i.tm.extension_opcode == None)
4264 {
4265 /* Optimize: -O1:
4266 VOP, one of vmovdqa32, vmovdqa64, vmovdqu8, vmovdqu16,
4267 vmovdqu32 and vmovdqu64:
4268 EVEX VOP %xmmM, %xmmN
4269 -> VEX vmovdqa|vmovdqu %xmmM, %xmmN (M and N < 16)
4270 EVEX VOP %ymmM, %ymmN
4271 -> VEX vmovdqa|vmovdqu %ymmM, %ymmN (M and N < 16)
4272 EVEX VOP %xmmM, mem
4273 -> VEX vmovdqa|vmovdqu %xmmM, mem (M < 16)
4274 EVEX VOP %ymmM, mem
4275 -> VEX vmovdqa|vmovdqu %ymmM, mem (M < 16)
4276 EVEX VOP mem, %xmmN
4277 -> VEX mvmovdqa|vmovdquem, %xmmN (N < 16)
4278 EVEX VOP mem, %ymmN
4279 -> VEX vmovdqa|vmovdqu mem, %ymmN (N < 16)
a0a1771e
JB
4280 VOP, one of vpand, vpandn, vpor, vpxor:
4281 EVEX VOP{d,q} %xmmL, %xmmM, %xmmN
4282 -> VEX VOP %xmmL, %xmmM, %xmmN (L, M, and N < 16)
4283 EVEX VOP{d,q} %ymmL, %ymmM, %ymmN
4284 -> VEX VOP %ymmL, %ymmM, %ymmN (L, M, and N < 16)
4285 EVEX VOP{d,q} mem, %xmmM, %xmmN
4286 -> VEX VOP mem, %xmmM, %xmmN (M and N < 16)
4287 EVEX VOP{d,q} mem, %ymmM, %ymmN
4288 -> VEX VOP mem, %ymmM, %ymmN (M and N < 16)
97ed31ae 4289 */
a0a1771e 4290 for (j = 0; j < i.operands; j++)
392a5972
L
4291 if (operand_type_check (i.types[j], disp)
4292 && i.op[j].disps->X_op == O_constant)
4293 {
4294 /* Since the VEX prefix has 2 or 3 bytes, the EVEX prefix
4295 has 4 bytes, EVEX Disp8 has 1 byte and VEX Disp32 has 4
4296 bytes, we choose EVEX Disp8 over VEX Disp32. */
4297 int evex_disp8, vex_disp8;
4298 unsigned int memshift = i.memshift;
4299 offsetT n = i.op[j].disps->X_add_number;
4300
4301 evex_disp8 = fits_in_disp8 (n);
4302 i.memshift = 0;
4303 vex_disp8 = fits_in_disp8 (n);
4304 if (evex_disp8 != vex_disp8)
4305 {
4306 i.memshift = memshift;
4307 return;
4308 }
4309
4310 i.types[j].bitfield.disp8 = vex_disp8;
4311 break;
4312 }
35648716
JB
4313 if ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4314 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2)
4315 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
97ed31ae
L
4316 i.tm.opcode_modifier.vex
4317 = i.types[0].bitfield.ymmword ? VEX256 : VEX128;
4318 i.tm.opcode_modifier.vexw = VEXW0;
79dec6b7 4319 /* VPAND, VPOR, and VPXOR are commutative. */
35648716 4320 if (i.reg_operands == 3 && i.tm.base_opcode != 0xdf)
79dec6b7 4321 i.tm.opcode_modifier.commutative = 1;
97ed31ae
L
4322 i.tm.opcode_modifier.evex = 0;
4323 i.tm.opcode_modifier.masking = 0;
a0a1771e 4324 i.tm.opcode_modifier.broadcast = 0;
97ed31ae
L
4325 i.tm.opcode_modifier.disp8memshift = 0;
4326 i.memshift = 0;
a0a1771e
JB
4327 if (j < i.operands)
4328 i.types[j].bitfield.disp8
4329 = fits_in_disp8 (i.op[j].disps->X_add_number);
97ed31ae 4330 }
b6f8c7c4
L
4331}
4332
ae531041
L
4333/* Return non-zero for load instruction. */
4334
4335static int
4336load_insn_p (void)
4337{
4338 unsigned int dest;
4339 int any_vex_p = is_any_vex_encoding (&i.tm);
4340 unsigned int base_opcode = i.tm.base_opcode | 1;
4341
4342 if (!any_vex_p)
4343 {
a09f656b 4344 /* Anysize insns: lea, invlpg, clflush, prefetchnta, prefetcht0,
4345 prefetcht1, prefetcht2, prefetchtw, bndmk, bndcl, bndcu, bndcn,
4346 bndstx, bndldx, prefetchwt1, clflushopt, clwb, cldemote. */
4347 if (i.tm.opcode_modifier.anysize)
ae531041
L
4348 return 0;
4349
389d00a5
JB
4350 /* pop. */
4351 if (strcmp (i.tm.name, "pop") == 0)
4352 return 1;
4353 }
4354
4355 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE)
4356 {
4357 /* popf, popa. */
4358 if (i.tm.base_opcode == 0x9d
a09f656b 4359 || i.tm.base_opcode == 0x61)
ae531041
L
4360 return 1;
4361
4362 /* movs, cmps, lods, scas. */
4363 if ((i.tm.base_opcode | 0xb) == 0xaf)
4364 return 1;
4365
a09f656b 4366 /* outs, xlatb. */
4367 if (base_opcode == 0x6f
4368 || i.tm.base_opcode == 0xd7)
ae531041 4369 return 1;
a09f656b 4370 /* NB: For AMD-specific insns with implicit memory operands,
4371 they're intentionally not covered. */
ae531041
L
4372 }
4373
4374 /* No memory operand. */
4375 if (!i.mem_operands)
4376 return 0;
4377
4378 if (any_vex_p)
4379 {
4380 /* vldmxcsr. */
4381 if (i.tm.base_opcode == 0xae
4382 && i.tm.opcode_modifier.vex
441f6aca 4383 && i.tm.opcode_modifier.opcodespace == SPACE_0F
35648716 4384 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
ae531041
L
4385 && i.tm.extension_opcode == 2)
4386 return 1;
4387 }
389d00a5 4388 else if (i.tm.opcode_modifier.opcodespace == SPACE_BASE)
ae531041
L
4389 {
4390 /* test, not, neg, mul, imul, div, idiv. */
4391 if ((i.tm.base_opcode == 0xf6 || i.tm.base_opcode == 0xf7)
4392 && i.tm.extension_opcode != 1)
4393 return 1;
4394
4395 /* inc, dec. */
4396 if (base_opcode == 0xff && i.tm.extension_opcode <= 1)
4397 return 1;
4398
4399 /* add, or, adc, sbb, and, sub, xor, cmp. */
4400 if (i.tm.base_opcode >= 0x80 && i.tm.base_opcode <= 0x83)
4401 return 1;
4402
ae531041
L
4403 /* rol, ror, rcl, rcr, shl/sal, shr, sar. */
4404 if ((base_opcode == 0xc1
4405 || (i.tm.base_opcode >= 0xd0 && i.tm.base_opcode <= 0xd3))
4406 && i.tm.extension_opcode != 6)
4407 return 1;
4408
ae531041 4409 /* Check for x87 instructions. */
389d00a5 4410 if (base_opcode >= 0xd8 && base_opcode <= 0xdf)
ae531041
L
4411 {
4412 /* Skip fst, fstp, fstenv, fstcw. */
4413 if (i.tm.base_opcode == 0xd9
4414 && (i.tm.extension_opcode == 2
4415 || i.tm.extension_opcode == 3
4416 || i.tm.extension_opcode == 6
4417 || i.tm.extension_opcode == 7))
4418 return 0;
4419
4420 /* Skip fisttp, fist, fistp, fstp. */
4421 if (i.tm.base_opcode == 0xdb
4422 && (i.tm.extension_opcode == 1
4423 || i.tm.extension_opcode == 2
4424 || i.tm.extension_opcode == 3
4425 || i.tm.extension_opcode == 7))
4426 return 0;
4427
4428 /* Skip fisttp, fst, fstp, fsave, fstsw. */
4429 if (i.tm.base_opcode == 0xdd
4430 && (i.tm.extension_opcode == 1
4431 || i.tm.extension_opcode == 2
4432 || i.tm.extension_opcode == 3
4433 || i.tm.extension_opcode == 6
4434 || i.tm.extension_opcode == 7))
4435 return 0;
4436
4437 /* Skip fisttp, fist, fistp, fbstp, fistp. */
4438 if (i.tm.base_opcode == 0xdf
4439 && (i.tm.extension_opcode == 1
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 return 1;
4447 }
4448 }
389d00a5
JB
4449 else if (i.tm.opcode_modifier.opcodespace == SPACE_0F)
4450 {
4451 /* bt, bts, btr, btc. */
4452 if (i.tm.base_opcode == 0xba
4453 && (i.tm.extension_opcode >= 4 && i.tm.extension_opcode <= 7))
4454 return 1;
4455
4456 /* cmpxchg8b, cmpxchg16b, xrstors, vmptrld. */
4457 if (i.tm.base_opcode == 0xc7
4458 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
4459 && (i.tm.extension_opcode == 1 || i.tm.extension_opcode == 3
4460 || i.tm.extension_opcode == 6))
4461 return 1;
4462
4463 /* fxrstor, ldmxcsr, xrstor. */
4464 if (i.tm.base_opcode == 0xae
4465 && (i.tm.extension_opcode == 1
4466 || i.tm.extension_opcode == 2
4467 || i.tm.extension_opcode == 5))
4468 return 1;
4469
4470 /* lgdt, lidt, lmsw. */
4471 if (i.tm.base_opcode == 0x01
4472 && (i.tm.extension_opcode == 2
4473 || i.tm.extension_opcode == 3
4474 || i.tm.extension_opcode == 6))
4475 return 1;
4476 }
ae531041
L
4477
4478 dest = i.operands - 1;
4479
4480 /* Check fake imm8 operand and 3 source operands. */
4481 if ((i.tm.opcode_modifier.immext
4482 || i.tm.opcode_modifier.vexsources == VEX3SOURCES)
4483 && i.types[dest].bitfield.imm8)
4484 dest--;
4485
389d00a5
JB
4486 /* add, or, adc, sbb, and, sub, xor, cmp, test, xchg. */
4487 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
ae531041
L
4488 && (base_opcode == 0x1
4489 || base_opcode == 0x9
4490 || base_opcode == 0x11
4491 || base_opcode == 0x19
4492 || base_opcode == 0x21
4493 || base_opcode == 0x29
4494 || base_opcode == 0x31
4495 || base_opcode == 0x39
389d00a5
JB
4496 || (base_opcode | 2) == 0x87))
4497 return 1;
4498
4499 /* xadd. */
4500 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4501 && base_opcode == 0xc1)
ae531041
L
4502 return 1;
4503
4504 /* Check for load instruction. */
4505 return (i.types[dest].bitfield.class != ClassNone
4506 || i.types[dest].bitfield.instance == Accum);
4507}
4508
4509/* Output lfence, 0xfaee8, after instruction. */
4510
4511static void
4512insert_lfence_after (void)
4513{
4514 if (lfence_after_load && load_insn_p ())
4515 {
a09f656b 4516 /* There are also two REP string instructions that require
4517 special treatment. Specifically, the compare string (CMPS)
4518 and scan string (SCAS) instructions set EFLAGS in a manner
4519 that depends on the data being compared/scanned. When used
4520 with a REP prefix, the number of iterations may therefore
4521 vary depending on this data. If the data is a program secret
4522 chosen by the adversary using an LVI method,
4523 then this data-dependent behavior may leak some aspect
4524 of the secret. */
4525 if (((i.tm.base_opcode | 0x1) == 0xa7
4526 || (i.tm.base_opcode | 0x1) == 0xaf)
4527 && i.prefix[REP_PREFIX])
4528 {
4529 as_warn (_("`%s` changes flags which would affect control flow behavior"),
4530 i.tm.name);
4531 }
ae531041
L
4532 char *p = frag_more (3);
4533 *p++ = 0xf;
4534 *p++ = 0xae;
4535 *p = 0xe8;
4536 }
4537}
4538
4539/* Output lfence, 0xfaee8, before instruction. */
4540
4541static void
4542insert_lfence_before (void)
4543{
4544 char *p;
4545
389d00a5 4546 if (i.tm.opcode_modifier.opcodespace != SPACE_BASE)
ae531041
L
4547 return;
4548
4549 if (i.tm.base_opcode == 0xff
4550 && (i.tm.extension_opcode == 2 || i.tm.extension_opcode == 4))
4551 {
4552 /* Insert lfence before indirect branch if needed. */
4553
4554 if (lfence_before_indirect_branch == lfence_branch_none)
4555 return;
4556
4557 if (i.operands != 1)
4558 abort ();
4559
4560 if (i.reg_operands == 1)
4561 {
4562 /* Indirect branch via register. Don't insert lfence with
4563 -mlfence-after-load=yes. */
4564 if (lfence_after_load
4565 || lfence_before_indirect_branch == lfence_branch_memory)
4566 return;
4567 }
4568 else if (i.mem_operands == 1
4569 && lfence_before_indirect_branch != lfence_branch_register)
4570 {
4571 as_warn (_("indirect `%s` with memory operand should be avoided"),
4572 i.tm.name);
4573 return;
4574 }
4575 else
4576 return;
4577
4578 if (last_insn.kind != last_insn_other
4579 && last_insn.seg == now_seg)
4580 {
4581 as_warn_where (last_insn.file, last_insn.line,
4582 _("`%s` skips -mlfence-before-indirect-branch on `%s`"),
4583 last_insn.name, i.tm.name);
4584 return;
4585 }
4586
4587 p = frag_more (3);
4588 *p++ = 0xf;
4589 *p++ = 0xae;
4590 *p = 0xe8;
4591 return;
4592 }
4593
503648e4 4594 /* Output or/not/shl and lfence before near ret. */
ae531041
L
4595 if (lfence_before_ret != lfence_before_ret_none
4596 && (i.tm.base_opcode == 0xc2
503648e4 4597 || i.tm.base_opcode == 0xc3))
ae531041
L
4598 {
4599 if (last_insn.kind != last_insn_other
4600 && last_insn.seg == now_seg)
4601 {
4602 as_warn_where (last_insn.file, last_insn.line,
4603 _("`%s` skips -mlfence-before-ret on `%s`"),
4604 last_insn.name, i.tm.name);
4605 return;
4606 }
a09f656b 4607
a09f656b 4608 /* Near ret ingore operand size override under CPU64. */
503648e4 4609 char prefix = flag_code == CODE_64BIT
4610 ? 0x48
4611 : i.prefix[DATA_PREFIX] ? 0x66 : 0x0;
a09f656b 4612
4613 if (lfence_before_ret == lfence_before_ret_not)
4614 {
4615 /* not: 0xf71424, may add prefix
4616 for operand size override or 64-bit code. */
4617 p = frag_more ((prefix ? 2 : 0) + 6 + 3);
4618 if (prefix)
4619 *p++ = prefix;
ae531041
L
4620 *p++ = 0xf7;
4621 *p++ = 0x14;
4622 *p++ = 0x24;
a09f656b 4623 if (prefix)
4624 *p++ = prefix;
ae531041
L
4625 *p++ = 0xf7;
4626 *p++ = 0x14;
4627 *p++ = 0x24;
4628 }
a09f656b 4629 else
4630 {
4631 p = frag_more ((prefix ? 1 : 0) + 4 + 3);
4632 if (prefix)
4633 *p++ = prefix;
4634 if (lfence_before_ret == lfence_before_ret_or)
4635 {
4636 /* or: 0x830c2400, may add prefix
4637 for operand size override or 64-bit code. */
4638 *p++ = 0x83;
4639 *p++ = 0x0c;
4640 }
4641 else
4642 {
4643 /* shl: 0xc1242400, may add prefix
4644 for operand size override or 64-bit code. */
4645 *p++ = 0xc1;
4646 *p++ = 0x24;
4647 }
4648
4649 *p++ = 0x24;
4650 *p++ = 0x0;
4651 }
4652
ae531041
L
4653 *p++ = 0xf;
4654 *p++ = 0xae;
4655 *p = 0xe8;
4656 }
4657}
4658
252b5132
RH
4659/* This is the guts of the machine-dependent assembler. LINE points to a
4660 machine dependent instruction. This function is supposed to emit
4661 the frags/bytes it assembles to. */
4662
4663void
65da13b5 4664md_assemble (char *line)
252b5132 4665{
40fb9820 4666 unsigned int j;
83b16ac6 4667 char mnemonic[MAX_MNEM_SIZE], mnem_suffix;
d3ce72d0 4668 const insn_template *t;
252b5132 4669
47926f60 4670 /* Initialize globals. */
252b5132
RH
4671 memset (&i, '\0', sizeof (i));
4672 for (j = 0; j < MAX_OPERANDS; j++)
1ae12ab7 4673 i.reloc[j] = NO_RELOC;
252b5132
RH
4674 memset (disp_expressions, '\0', sizeof (disp_expressions));
4675 memset (im_expressions, '\0', sizeof (im_expressions));
ce8a8b2f 4676 save_stack_p = save_stack;
252b5132
RH
4677
4678 /* First parse an instruction mnemonic & call i386_operand for the operands.
4679 We assume that the scrubber has arranged it so that line[0] is the valid
47926f60 4680 start of a (possibly prefixed) mnemonic. */
252b5132 4681
29b0f896
AM
4682 line = parse_insn (line, mnemonic);
4683 if (line == NULL)
4684 return;
83b16ac6 4685 mnem_suffix = i.suffix;
252b5132 4686
29b0f896 4687 line = parse_operands (line, mnemonic);
ee86248c 4688 this_operand = -1;
8325cc63
JB
4689 xfree (i.memop1_string);
4690 i.memop1_string = NULL;
29b0f896
AM
4691 if (line == NULL)
4692 return;
252b5132 4693
29b0f896
AM
4694 /* Now we've parsed the mnemonic into a set of templates, and have the
4695 operands at hand. */
4696
b630c145 4697 /* All Intel opcodes have reversed operands except for "bound", "enter",
c0e54661
JB
4698 "invlpg*", "monitor*", "mwait*", "tpause", "umwait", "pvalidate",
4699 "rmpadjust", and "rmpupdate". We also don't reverse intersegment "jmp"
4700 and "call" instructions with 2 immediate operands so that the immediate
4701 segment precedes the offset consistently in Intel and AT&T modes. */
4d456e3d
L
4702 if (intel_syntax
4703 && i.operands > 1
29b0f896 4704 && (strcmp (mnemonic, "bound") != 0)
c0e54661 4705 && (strncmp (mnemonic, "invlpg", 6) != 0)
eedb0f2c
JB
4706 && (strncmp (mnemonic, "monitor", 7) != 0)
4707 && (strncmp (mnemonic, "mwait", 5) != 0)
c0e54661
JB
4708 && (strcmp (mnemonic, "pvalidate") != 0)
4709 && (strncmp (mnemonic, "rmp", 3) != 0)
b630c145
JB
4710 && (strcmp (mnemonic, "tpause") != 0)
4711 && (strcmp (mnemonic, "umwait") != 0)
40fb9820
L
4712 && !(operand_type_check (i.types[0], imm)
4713 && operand_type_check (i.types[1], imm)))
29b0f896
AM
4714 swap_operands ();
4715
ec56d5c0
JB
4716 /* The order of the immediates should be reversed
4717 for 2 immediates extrq and insertq instructions */
4718 if (i.imm_operands == 2
4719 && (strcmp (mnemonic, "extrq") == 0
4720 || strcmp (mnemonic, "insertq") == 0))
4721 swap_2_operands (0, 1);
4722
29b0f896
AM
4723 if (i.imm_operands)
4724 optimize_imm ();
4725
b300c311
L
4726 /* Don't optimize displacement for movabs since it only takes 64bit
4727 displacement. */
4728 if (i.disp_operands
a501d77e 4729 && i.disp_encoding != disp_encoding_32bit
862be3fb
L
4730 && (flag_code != CODE_64BIT
4731 || strcmp (mnemonic, "movabs") != 0))
4732 optimize_disp ();
29b0f896
AM
4733
4734 /* Next, we find a template that matches the given insn,
4735 making sure the overlap of the given operands types is consistent
4736 with the template operand types. */
252b5132 4737
83b16ac6 4738 if (!(t = match_template (mnem_suffix)))
29b0f896 4739 return;
252b5132 4740
7bab8ab5 4741 if (sse_check != check_none
81f8a913 4742 && !i.tm.opcode_modifier.noavx
6e3e5c9e 4743 && !i.tm.cpu_flags.bitfield.cpuavx
569d50f1 4744 && !i.tm.cpu_flags.bitfield.cpuavx512f
daf50ae7
L
4745 && (i.tm.cpu_flags.bitfield.cpusse
4746 || i.tm.cpu_flags.bitfield.cpusse2
4747 || i.tm.cpu_flags.bitfield.cpusse3
4748 || i.tm.cpu_flags.bitfield.cpussse3
4749 || i.tm.cpu_flags.bitfield.cpusse4_1
6e3e5c9e
JB
4750 || i.tm.cpu_flags.bitfield.cpusse4_2
4751 || i.tm.cpu_flags.bitfield.cpupclmul
4752 || i.tm.cpu_flags.bitfield.cpuaes
569d50f1 4753 || i.tm.cpu_flags.bitfield.cpusha
6e3e5c9e 4754 || i.tm.cpu_flags.bitfield.cpugfni))
daf50ae7 4755 {
7bab8ab5 4756 (sse_check == check_warning
daf50ae7
L
4757 ? as_warn
4758 : as_bad) (_("SSE instruction `%s' is used"), i.tm.name);
4759 }
4760
40fb9820 4761 if (i.tm.opcode_modifier.fwait)
29b0f896
AM
4762 if (!add_prefix (FWAIT_OPCODE))
4763 return;
252b5132 4764
d5de92cf 4765 /* Check if REP prefix is OK. */
742732c7 4766 if (i.rep_prefix && i.tm.opcode_modifier.prefixok != PrefixRep)
d5de92cf
L
4767 {
4768 as_bad (_("invalid instruction `%s' after `%s'"),
4769 i.tm.name, i.rep_prefix);
4770 return;
4771 }
4772
c1ba0266
L
4773 /* Check for lock without a lockable instruction. Destination operand
4774 must be memory unless it is xchg (0x86). */
c32fa91d 4775 if (i.prefix[LOCK_PREFIX]
742732c7 4776 && (i.tm.opcode_modifier.prefixok < PrefixLock
c1ba0266
L
4777 || i.mem_operands == 0
4778 || (i.tm.base_opcode != 0x86
8dc0818e 4779 && !(i.flags[i.operands - 1] & Operand_Mem))))
c32fa91d
L
4780 {
4781 as_bad (_("expecting lockable instruction after `lock'"));
4782 return;
4783 }
4784
40d231b4
JB
4785 /* Check for data size prefix on VEX/XOP/EVEX encoded and SIMD insns. */
4786 if (i.prefix[DATA_PREFIX]
4787 && (is_any_vex_encoding (&i.tm)
4788 || i.tm.operand_types[i.imm_operands].bitfield.class >= RegMMX
4789 || i.tm.operand_types[i.imm_operands + 1].bitfield.class >= RegMMX))
7a8655d2
JB
4790 {
4791 as_bad (_("data size prefix invalid with `%s'"), i.tm.name);
4792 return;
4793 }
4794
42164a71 4795 /* Check if HLE prefix is OK. */
165de32a 4796 if (i.hle_prefix && !check_hle ())
42164a71
L
4797 return;
4798
7e8b059b
L
4799 /* Check BND prefix. */
4800 if (i.bnd_prefix && !i.tm.opcode_modifier.bndprefixok)
4801 as_bad (_("expecting valid branch instruction after `bnd'"));
4802
04ef582a 4803 /* Check NOTRACK prefix. */
742732c7 4804 if (i.notrack_prefix && i.tm.opcode_modifier.prefixok != PrefixNoTrack)
9fef80d6 4805 as_bad (_("expecting indirect branch instruction after `notrack'"));
04ef582a 4806
327e8c42
JB
4807 if (i.tm.cpu_flags.bitfield.cpumpx)
4808 {
4809 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
4810 as_bad (_("32-bit address isn't allowed in 64-bit MPX instructions."));
4811 else if (flag_code != CODE_16BIT
4812 ? i.prefix[ADDR_PREFIX]
4813 : i.mem_operands && !i.prefix[ADDR_PREFIX])
4814 as_bad (_("16-bit address isn't allowed in MPX instructions"));
4815 }
7e8b059b
L
4816
4817 /* Insert BND prefix. */
76d3a78a
JB
4818 if (add_bnd_prefix && i.tm.opcode_modifier.bndprefixok)
4819 {
4820 if (!i.prefix[BND_PREFIX])
4821 add_prefix (BND_PREFIX_OPCODE);
4822 else if (i.prefix[BND_PREFIX] != BND_PREFIX_OPCODE)
4823 {
4824 as_warn (_("replacing `rep'/`repe' prefix by `bnd'"));
4825 i.prefix[BND_PREFIX] = BND_PREFIX_OPCODE;
4826 }
4827 }
7e8b059b 4828
29b0f896 4829 /* Check string instruction segment overrides. */
51c8edf6 4830 if (i.tm.opcode_modifier.isstring >= IS_STRING_ES_OP0)
29b0f896 4831 {
51c8edf6 4832 gas_assert (i.mem_operands);
29b0f896 4833 if (!check_string ())
5dd0794d 4834 return;
fc0763e6 4835 i.disp_operands = 0;
29b0f896 4836 }
5dd0794d 4837
b6f8c7c4
L
4838 if (optimize && !i.no_optimize && i.tm.opcode_modifier.optimize)
4839 optimize_encoding ();
4840
29b0f896
AM
4841 if (!process_suffix ())
4842 return;
e413e4e9 4843
921eafea 4844 /* Update operand types and check extended states. */
bc0844ae 4845 for (j = 0; j < i.operands; j++)
921eafea
L
4846 {
4847 i.types[j] = operand_type_and (i.types[j], i.tm.operand_types[j]);
3d70986f 4848 switch (i.tm.operand_types[j].bitfield.class)
921eafea
L
4849 {
4850 default:
4851 break;
4852 case RegMMX:
4853 i.xstate |= xstate_mmx;
4854 break;
4855 case RegMask:
32930e4e 4856 i.xstate |= xstate_mask;
921eafea
L
4857 break;
4858 case RegSIMD:
3d70986f 4859 if (i.tm.operand_types[j].bitfield.tmmword)
921eafea 4860 i.xstate |= xstate_tmm;
3d70986f 4861 else if (i.tm.operand_types[j].bitfield.zmmword)
921eafea 4862 i.xstate |= xstate_zmm;
3d70986f 4863 else if (i.tm.operand_types[j].bitfield.ymmword)
921eafea 4864 i.xstate |= xstate_ymm;
3d70986f 4865 else if (i.tm.operand_types[j].bitfield.xmmword)
921eafea
L
4866 i.xstate |= xstate_xmm;
4867 break;
4868 }
4869 }
bc0844ae 4870
29b0f896
AM
4871 /* Make still unresolved immediate matches conform to size of immediate
4872 given in i.suffix. */
4873 if (!finalize_imm ())
4874 return;
252b5132 4875
40fb9820 4876 if (i.types[0].bitfield.imm1)
29b0f896 4877 i.imm_operands = 0; /* kludge for shift insns. */
252b5132 4878
9afe6eb8
L
4879 /* We only need to check those implicit registers for instructions
4880 with 3 operands or less. */
4881 if (i.operands <= 3)
4882 for (j = 0; j < i.operands; j++)
75e5731b
JB
4883 if (i.types[j].bitfield.instance != InstanceNone
4884 && !i.types[j].bitfield.xmmword)
9afe6eb8 4885 i.reg_operands--;
40fb9820 4886
29b0f896
AM
4887 /* For insns with operands there are more diddles to do to the opcode. */
4888 if (i.operands)
4889 {
4890 if (!process_operands ())
4891 return;
4892 }
8c190ce0 4893 else if (!quiet_warnings && i.tm.opcode_modifier.ugh)
29b0f896
AM
4894 {
4895 /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc. */
4896 as_warn (_("translating to `%sp'"), i.tm.name);
4897 }
252b5132 4898
7a8655d2 4899 if (is_any_vex_encoding (&i.tm))
9e5e5283 4900 {
c1dc7af5 4901 if (!cpu_arch_flags.bitfield.cpui286)
9e5e5283 4902 {
c1dc7af5 4903 as_bad (_("instruction `%s' isn't supported outside of protected mode."),
9e5e5283
L
4904 i.tm.name);
4905 return;
4906 }
c0f3af97 4907
0b9404fd
JB
4908 /* Check for explicit REX prefix. */
4909 if (i.prefix[REX_PREFIX] || i.rex_encoding)
4910 {
4911 as_bad (_("REX prefix invalid with `%s'"), i.tm.name);
4912 return;
4913 }
4914
9e5e5283
L
4915 if (i.tm.opcode_modifier.vex)
4916 build_vex_prefix (t);
4917 else
4918 build_evex_prefix ();
0b9404fd
JB
4919
4920 /* The individual REX.RXBW bits got consumed. */
4921 i.rex &= REX_OPCODE;
9e5e5283 4922 }
43234a1e 4923
5dd85c99
SP
4924 /* Handle conversion of 'int $3' --> special int3 insn. XOP or FMA4
4925 instructions may define INT_OPCODE as well, so avoid this corner
4926 case for those instructions that use MODRM. */
389d00a5
JB
4927 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
4928 && i.tm.base_opcode == INT_OPCODE
a6461c02
SP
4929 && !i.tm.opcode_modifier.modrm
4930 && i.op[0].imms->X_add_number == 3)
29b0f896
AM
4931 {
4932 i.tm.base_opcode = INT3_OPCODE;
4933 i.imm_operands = 0;
4934 }
252b5132 4935
0cfa3eb3
JB
4936 if ((i.tm.opcode_modifier.jump == JUMP
4937 || i.tm.opcode_modifier.jump == JUMP_BYTE
4938 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896
AM
4939 && i.op[0].disps->X_op == O_constant)
4940 {
4941 /* Convert "jmp constant" (and "call constant") to a jump (call) to
4942 the absolute address given by the constant. Since ix86 jumps and
4943 calls are pc relative, we need to generate a reloc. */
4944 i.op[0].disps->X_add_symbol = &abs_symbol;
4945 i.op[0].disps->X_op = O_symbol;
4946 }
252b5132 4947
29b0f896
AM
4948 /* For 8 bit registers we need an empty rex prefix. Also if the
4949 instruction already has a prefix, we need to convert old
4950 registers to new ones. */
773f551c 4951
bab6aec1 4952 if ((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte
29b0f896 4953 && (i.op[0].regs->reg_flags & RegRex64) != 0)
bab6aec1 4954 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte
29b0f896 4955 && (i.op[1].regs->reg_flags & RegRex64) != 0)
bab6aec1
JB
4956 || (((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte)
4957 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte))
29b0f896
AM
4958 && i.rex != 0))
4959 {
4960 int x;
726c5dcd 4961
29b0f896
AM
4962 i.rex |= REX_OPCODE;
4963 for (x = 0; x < 2; x++)
4964 {
4965 /* Look for 8 bit operand that uses old registers. */
bab6aec1 4966 if (i.types[x].bitfield.class == Reg && i.types[x].bitfield.byte
29b0f896 4967 && (i.op[x].regs->reg_flags & RegRex64) == 0)
773f551c 4968 {
3f93af61 4969 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
29b0f896
AM
4970 /* In case it is "hi" register, give up. */
4971 if (i.op[x].regs->reg_num > 3)
a540244d 4972 as_bad (_("can't encode register '%s%s' in an "
4eed87de 4973 "instruction requiring REX prefix."),
a540244d 4974 register_prefix, i.op[x].regs->reg_name);
773f551c 4975
29b0f896
AM
4976 /* Otherwise it is equivalent to the extended register.
4977 Since the encoding doesn't change this is merely
4978 cosmetic cleanup for debug output. */
4979
4980 i.op[x].regs = i.op[x].regs + 8;
773f551c 4981 }
29b0f896
AM
4982 }
4983 }
773f551c 4984
6b6b6807
L
4985 if (i.rex == 0 && i.rex_encoding)
4986 {
4987 /* Check if we can add a REX_OPCODE byte. Look for 8 bit operand
3f93af61 4988 that uses legacy register. If it is "hi" register, don't add
6b6b6807
L
4989 the REX_OPCODE byte. */
4990 int x;
4991 for (x = 0; x < 2; x++)
bab6aec1 4992 if (i.types[x].bitfield.class == Reg
6b6b6807
L
4993 && i.types[x].bitfield.byte
4994 && (i.op[x].regs->reg_flags & RegRex64) == 0
4995 && i.op[x].regs->reg_num > 3)
4996 {
3f93af61 4997 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
6b6b6807
L
4998 i.rex_encoding = FALSE;
4999 break;
5000 }
5001
5002 if (i.rex_encoding)
5003 i.rex = REX_OPCODE;
5004 }
5005
7ab9ffdd 5006 if (i.rex != 0)
29b0f896
AM
5007 add_prefix (REX_OPCODE | i.rex);
5008
ae531041
L
5009 insert_lfence_before ();
5010
29b0f896
AM
5011 /* We are ready to output the insn. */
5012 output_insn ();
e379e5f3 5013
ae531041
L
5014 insert_lfence_after ();
5015
e379e5f3
L
5016 last_insn.seg = now_seg;
5017
5018 if (i.tm.opcode_modifier.isprefix)
5019 {
5020 last_insn.kind = last_insn_prefix;
5021 last_insn.name = i.tm.name;
5022 last_insn.file = as_where (&last_insn.line);
5023 }
5024 else
5025 last_insn.kind = last_insn_other;
29b0f896
AM
5026}
5027
5028static char *
e3bb37b5 5029parse_insn (char *line, char *mnemonic)
29b0f896
AM
5030{
5031 char *l = line;
5032 char *token_start = l;
5033 char *mnem_p;
5c6af06e 5034 int supported;
d3ce72d0 5035 const insn_template *t;
b6169b20 5036 char *dot_p = NULL;
29b0f896 5037
29b0f896
AM
5038 while (1)
5039 {
5040 mnem_p = mnemonic;
5041 while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
5042 {
b6169b20
L
5043 if (*mnem_p == '.')
5044 dot_p = mnem_p;
29b0f896
AM
5045 mnem_p++;
5046 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
45288df1 5047 {
29b0f896
AM
5048 as_bad (_("no such instruction: `%s'"), token_start);
5049 return NULL;
5050 }
5051 l++;
5052 }
5053 if (!is_space_char (*l)
5054 && *l != END_OF_INSN
e44823cf
JB
5055 && (intel_syntax
5056 || (*l != PREFIX_SEPARATOR
5057 && *l != ',')))
29b0f896
AM
5058 {
5059 as_bad (_("invalid character %s in mnemonic"),
5060 output_invalid (*l));
5061 return NULL;
5062 }
5063 if (token_start == l)
5064 {
e44823cf 5065 if (!intel_syntax && *l == PREFIX_SEPARATOR)
29b0f896
AM
5066 as_bad (_("expecting prefix; got nothing"));
5067 else
5068 as_bad (_("expecting mnemonic; got nothing"));
5069 return NULL;
5070 }
45288df1 5071
29b0f896 5072 /* Look up instruction (or prefix) via hash table. */
629310ab 5073 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
47926f60 5074
29b0f896
AM
5075 if (*l != END_OF_INSN
5076 && (!is_space_char (*l) || l[1] != END_OF_INSN)
5077 && current_templates
40fb9820 5078 && current_templates->start->opcode_modifier.isprefix)
29b0f896 5079 {
c6fb90c8 5080 if (!cpu_flags_check_cpu64 (current_templates->start->cpu_flags))
2dd88dca
JB
5081 {
5082 as_bad ((flag_code != CODE_64BIT
5083 ? _("`%s' is only supported in 64-bit mode")
5084 : _("`%s' is not supported in 64-bit mode")),
5085 current_templates->start->name);
5086 return NULL;
5087 }
29b0f896
AM
5088 /* If we are in 16-bit mode, do not allow addr16 or data16.
5089 Similarly, in 32-bit mode, do not allow addr32 or data32. */
673fe0f0
JB
5090 if ((current_templates->start->opcode_modifier.size == SIZE16
5091 || current_templates->start->opcode_modifier.size == SIZE32)
29b0f896 5092 && flag_code != CODE_64BIT
673fe0f0 5093 && ((current_templates->start->opcode_modifier.size == SIZE32)
29b0f896
AM
5094 ^ (flag_code == CODE_16BIT)))
5095 {
5096 as_bad (_("redundant %s prefix"),
5097 current_templates->start->name);
5098 return NULL;
45288df1 5099 }
31184569
JB
5100
5101 if (current_templates->start->base_opcode == PSEUDO_PREFIX)
29b0f896 5102 {
86fa6981 5103 /* Handle pseudo prefixes. */
31184569 5104 switch (current_templates->start->extension_opcode)
86fa6981 5105 {
41eb8e88 5106 case Prefix_Disp8:
86fa6981
L
5107 /* {disp8} */
5108 i.disp_encoding = disp_encoding_8bit;
5109 break;
41eb8e88
L
5110 case Prefix_Disp16:
5111 /* {disp16} */
5112 i.disp_encoding = disp_encoding_16bit;
5113 break;
5114 case Prefix_Disp32:
86fa6981
L
5115 /* {disp32} */
5116 i.disp_encoding = disp_encoding_32bit;
5117 break;
41eb8e88 5118 case Prefix_Load:
86fa6981
L
5119 /* {load} */
5120 i.dir_encoding = dir_encoding_load;
5121 break;
41eb8e88 5122 case Prefix_Store:
86fa6981
L
5123 /* {store} */
5124 i.dir_encoding = dir_encoding_store;
5125 break;
41eb8e88 5126 case Prefix_VEX:
42e04b36
L
5127 /* {vex} */
5128 i.vec_encoding = vex_encoding_vex;
86fa6981 5129 break;
41eb8e88 5130 case Prefix_VEX3:
86fa6981
L
5131 /* {vex3} */
5132 i.vec_encoding = vex_encoding_vex3;
5133 break;
41eb8e88 5134 case Prefix_EVEX:
86fa6981
L
5135 /* {evex} */
5136 i.vec_encoding = vex_encoding_evex;
5137 break;
41eb8e88 5138 case Prefix_REX:
6b6b6807
L
5139 /* {rex} */
5140 i.rex_encoding = TRUE;
5141 break;
41eb8e88 5142 case Prefix_NoOptimize:
b6f8c7c4
L
5143 /* {nooptimize} */
5144 i.no_optimize = TRUE;
5145 break;
86fa6981
L
5146 default:
5147 abort ();
5148 }
5149 }
5150 else
5151 {
5152 /* Add prefix, checking for repeated prefixes. */
4e9ac44a 5153 switch (add_prefix (current_templates->start->base_opcode))
86fa6981 5154 {
4e9ac44a
L
5155 case PREFIX_EXIST:
5156 return NULL;
5157 case PREFIX_DS:
d777820b 5158 if (current_templates->start->cpu_flags.bitfield.cpuibt)
4e9ac44a
L
5159 i.notrack_prefix = current_templates->start->name;
5160 break;
5161 case PREFIX_REP:
5162 if (current_templates->start->cpu_flags.bitfield.cpuhle)
5163 i.hle_prefix = current_templates->start->name;
5164 else if (current_templates->start->cpu_flags.bitfield.cpumpx)
5165 i.bnd_prefix = current_templates->start->name;
5166 else
5167 i.rep_prefix = current_templates->start->name;
5168 break;
5169 default:
5170 break;
86fa6981 5171 }
29b0f896
AM
5172 }
5173 /* Skip past PREFIX_SEPARATOR and reset token_start. */
5174 token_start = ++l;
5175 }
5176 else
5177 break;
5178 }
45288df1 5179
30a55f88 5180 if (!current_templates)
b6169b20 5181 {
07d5e953
JB
5182 /* Deprecated functionality (new code should use pseudo-prefixes instead):
5183 Check if we should swap operand or force 32bit displacement in
f8a5c266 5184 encoding. */
30a55f88 5185 if (mnem_p - 2 == dot_p && dot_p[1] == 's')
64c49ab3 5186 i.dir_encoding = dir_encoding_swap;
8d63c93e 5187 else if (mnem_p - 3 == dot_p
a501d77e
L
5188 && dot_p[1] == 'd'
5189 && dot_p[2] == '8')
5190 i.disp_encoding = disp_encoding_8bit;
8d63c93e 5191 else if (mnem_p - 4 == dot_p
f8a5c266
L
5192 && dot_p[1] == 'd'
5193 && dot_p[2] == '3'
5194 && dot_p[3] == '2')
a501d77e 5195 i.disp_encoding = disp_encoding_32bit;
30a55f88
L
5196 else
5197 goto check_suffix;
5198 mnem_p = dot_p;
5199 *dot_p = '\0';
629310ab 5200 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
b6169b20
L
5201 }
5202
29b0f896
AM
5203 if (!current_templates)
5204 {
dc1e8a47 5205 check_suffix:
1c529385 5206 if (mnem_p > mnemonic)
29b0f896 5207 {
1c529385
LH
5208 /* See if we can get a match by trimming off a suffix. */
5209 switch (mnem_p[-1])
29b0f896 5210 {
1c529385
LH
5211 case WORD_MNEM_SUFFIX:
5212 if (intel_syntax && (intel_float_operand (mnemonic) & 2))
29b0f896
AM
5213 i.suffix = SHORT_MNEM_SUFFIX;
5214 else
1c529385
LH
5215 /* Fall through. */
5216 case BYTE_MNEM_SUFFIX:
5217 case QWORD_MNEM_SUFFIX:
5218 i.suffix = mnem_p[-1];
29b0f896 5219 mnem_p[-1] = '\0';
fe0e921f
AM
5220 current_templates
5221 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5222 break;
5223 case SHORT_MNEM_SUFFIX:
5224 case LONG_MNEM_SUFFIX:
5225 if (!intel_syntax)
5226 {
5227 i.suffix = mnem_p[-1];
5228 mnem_p[-1] = '\0';
fe0e921f
AM
5229 current_templates
5230 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5231 }
5232 break;
5233
5234 /* Intel Syntax. */
5235 case 'd':
5236 if (intel_syntax)
5237 {
5238 if (intel_float_operand (mnemonic) == 1)
5239 i.suffix = SHORT_MNEM_SUFFIX;
5240 else
5241 i.suffix = LONG_MNEM_SUFFIX;
5242 mnem_p[-1] = '\0';
fe0e921f
AM
5243 current_templates
5244 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5245 }
5246 break;
29b0f896 5247 }
29b0f896 5248 }
1c529385 5249
29b0f896
AM
5250 if (!current_templates)
5251 {
5252 as_bad (_("no such instruction: `%s'"), token_start);
5253 return NULL;
5254 }
5255 }
252b5132 5256
0cfa3eb3
JB
5257 if (current_templates->start->opcode_modifier.jump == JUMP
5258 || current_templates->start->opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
5259 {
5260 /* Check for a branch hint. We allow ",pt" and ",pn" for
5261 predict taken and predict not taken respectively.
5262 I'm not sure that branch hints actually do anything on loop
5263 and jcxz insns (JumpByte) for current Pentium4 chips. They
5264 may work in the future and it doesn't hurt to accept them
5265 now. */
5266 if (l[0] == ',' && l[1] == 'p')
5267 {
5268 if (l[2] == 't')
5269 {
5270 if (!add_prefix (DS_PREFIX_OPCODE))
5271 return NULL;
5272 l += 3;
5273 }
5274 else if (l[2] == 'n')
5275 {
5276 if (!add_prefix (CS_PREFIX_OPCODE))
5277 return NULL;
5278 l += 3;
5279 }
5280 }
5281 }
5282 /* Any other comma loses. */
5283 if (*l == ',')
5284 {
5285 as_bad (_("invalid character %s in mnemonic"),
5286 output_invalid (*l));
5287 return NULL;
5288 }
252b5132 5289
29b0f896 5290 /* Check if instruction is supported on specified architecture. */
5c6af06e
JB
5291 supported = 0;
5292 for (t = current_templates->start; t < current_templates->end; ++t)
5293 {
c0f3af97
L
5294 supported |= cpu_flags_match (t);
5295 if (supported == CPU_FLAGS_PERFECT_MATCH)
548d0ee6
JB
5296 {
5297 if (!cpu_arch_flags.bitfield.cpui386 && (flag_code != CODE_16BIT))
5298 as_warn (_("use .code16 to ensure correct addressing mode"));
3629bb00 5299
548d0ee6
JB
5300 return l;
5301 }
29b0f896 5302 }
3629bb00 5303
548d0ee6
JB
5304 if (!(supported & CPU_FLAGS_64BIT_MATCH))
5305 as_bad (flag_code == CODE_64BIT
5306 ? _("`%s' is not supported in 64-bit mode")
5307 : _("`%s' is only supported in 64-bit mode"),
5308 current_templates->start->name);
5309 else
5310 as_bad (_("`%s' is not supported on `%s%s'"),
5311 current_templates->start->name,
5312 cpu_arch_name ? cpu_arch_name : default_arch,
5313 cpu_sub_arch_name ? cpu_sub_arch_name : "");
252b5132 5314
548d0ee6 5315 return NULL;
29b0f896 5316}
252b5132 5317
29b0f896 5318static char *
e3bb37b5 5319parse_operands (char *l, const char *mnemonic)
29b0f896
AM
5320{
5321 char *token_start;
3138f287 5322
29b0f896
AM
5323 /* 1 if operand is pending after ','. */
5324 unsigned int expecting_operand = 0;
252b5132 5325
29b0f896
AM
5326 /* Non-zero if operand parens not balanced. */
5327 unsigned int paren_not_balanced;
5328
5329 while (*l != END_OF_INSN)
5330 {
5331 /* Skip optional white space before operand. */
5332 if (is_space_char (*l))
5333 ++l;
d02603dc 5334 if (!is_operand_char (*l) && *l != END_OF_INSN && *l != '"')
29b0f896
AM
5335 {
5336 as_bad (_("invalid character %s before operand %d"),
5337 output_invalid (*l),
5338 i.operands + 1);
5339 return NULL;
5340 }
d02603dc 5341 token_start = l; /* After white space. */
29b0f896
AM
5342 paren_not_balanced = 0;
5343 while (paren_not_balanced || *l != ',')
5344 {
5345 if (*l == END_OF_INSN)
5346 {
5347 if (paren_not_balanced)
5348 {
5349 if (!intel_syntax)
5350 as_bad (_("unbalanced parenthesis in operand %d."),
5351 i.operands + 1);
5352 else
5353 as_bad (_("unbalanced brackets in operand %d."),
5354 i.operands + 1);
5355 return NULL;
5356 }
5357 else
5358 break; /* we are done */
5359 }
d02603dc 5360 else if (!is_operand_char (*l) && !is_space_char (*l) && *l != '"')
29b0f896
AM
5361 {
5362 as_bad (_("invalid character %s in operand %d"),
5363 output_invalid (*l),
5364 i.operands + 1);
5365 return NULL;
5366 }
5367 if (!intel_syntax)
5368 {
5369 if (*l == '(')
5370 ++paren_not_balanced;
5371 if (*l == ')')
5372 --paren_not_balanced;
5373 }
5374 else
5375 {
5376 if (*l == '[')
5377 ++paren_not_balanced;
5378 if (*l == ']')
5379 --paren_not_balanced;
5380 }
5381 l++;
5382 }
5383 if (l != token_start)
5384 { /* Yes, we've read in another operand. */
5385 unsigned int operand_ok;
5386 this_operand = i.operands++;
5387 if (i.operands > MAX_OPERANDS)
5388 {
5389 as_bad (_("spurious operands; (%d operands/instruction max)"),
5390 MAX_OPERANDS);
5391 return NULL;
5392 }
9d46ce34 5393 i.types[this_operand].bitfield.unspecified = 1;
29b0f896
AM
5394 /* Now parse operand adding info to 'i' as we go along. */
5395 END_STRING_AND_SAVE (l);
5396
1286ab78
L
5397 if (i.mem_operands > 1)
5398 {
5399 as_bad (_("too many memory references for `%s'"),
5400 mnemonic);
5401 return 0;
5402 }
5403
29b0f896
AM
5404 if (intel_syntax)
5405 operand_ok =
5406 i386_intel_operand (token_start,
5407 intel_float_operand (mnemonic));
5408 else
a7619375 5409 operand_ok = i386_att_operand (token_start);
29b0f896
AM
5410
5411 RESTORE_END_STRING (l);
5412 if (!operand_ok)
5413 return NULL;
5414 }
5415 else
5416 {
5417 if (expecting_operand)
5418 {
5419 expecting_operand_after_comma:
5420 as_bad (_("expecting operand after ','; got nothing"));
5421 return NULL;
5422 }
5423 if (*l == ',')
5424 {
5425 as_bad (_("expecting operand before ','; got nothing"));
5426 return NULL;
5427 }
5428 }
7f3f1ea2 5429
29b0f896
AM
5430 /* Now *l must be either ',' or END_OF_INSN. */
5431 if (*l == ',')
5432 {
5433 if (*++l == END_OF_INSN)
5434 {
5435 /* Just skip it, if it's \n complain. */
5436 goto expecting_operand_after_comma;
5437 }
5438 expecting_operand = 1;
5439 }
5440 }
5441 return l;
5442}
7f3f1ea2 5443
050dfa73 5444static void
783c187b 5445swap_2_operands (unsigned int xchg1, unsigned int xchg2)
050dfa73
MM
5446{
5447 union i386_op temp_op;
40fb9820 5448 i386_operand_type temp_type;
c48dadc9 5449 unsigned int temp_flags;
050dfa73 5450 enum bfd_reloc_code_real temp_reloc;
4eed87de 5451
050dfa73
MM
5452 temp_type = i.types[xchg2];
5453 i.types[xchg2] = i.types[xchg1];
5454 i.types[xchg1] = temp_type;
c48dadc9
JB
5455
5456 temp_flags = i.flags[xchg2];
5457 i.flags[xchg2] = i.flags[xchg1];
5458 i.flags[xchg1] = temp_flags;
5459
050dfa73
MM
5460 temp_op = i.op[xchg2];
5461 i.op[xchg2] = i.op[xchg1];
5462 i.op[xchg1] = temp_op;
c48dadc9 5463
050dfa73
MM
5464 temp_reloc = i.reloc[xchg2];
5465 i.reloc[xchg2] = i.reloc[xchg1];
5466 i.reloc[xchg1] = temp_reloc;
43234a1e 5467
6225c532 5468 if (i.mask.reg)
43234a1e 5469 {
6225c532
JB
5470 if (i.mask.operand == xchg1)
5471 i.mask.operand = xchg2;
5472 else if (i.mask.operand == xchg2)
5473 i.mask.operand = xchg1;
43234a1e
L
5474 }
5475 if (i.broadcast)
5476 {
5477 if (i.broadcast->operand == xchg1)
5478 i.broadcast->operand = xchg2;
5479 else if (i.broadcast->operand == xchg2)
5480 i.broadcast->operand = xchg1;
5481 }
5482 if (i.rounding)
5483 {
5484 if (i.rounding->operand == xchg1)
5485 i.rounding->operand = xchg2;
5486 else if (i.rounding->operand == xchg2)
5487 i.rounding->operand = xchg1;
5488 }
050dfa73
MM
5489}
5490
29b0f896 5491static void
e3bb37b5 5492swap_operands (void)
29b0f896 5493{
b7c61d9a 5494 switch (i.operands)
050dfa73 5495 {
c0f3af97 5496 case 5:
b7c61d9a 5497 case 4:
4d456e3d 5498 swap_2_operands (1, i.operands - 2);
1a0670f3 5499 /* Fall through. */
b7c61d9a
L
5500 case 3:
5501 case 2:
4d456e3d 5502 swap_2_operands (0, i.operands - 1);
b7c61d9a
L
5503 break;
5504 default:
5505 abort ();
29b0f896 5506 }
29b0f896
AM
5507
5508 if (i.mem_operands == 2)
5509 {
5510 const seg_entry *temp_seg;
5511 temp_seg = i.seg[0];
5512 i.seg[0] = i.seg[1];
5513 i.seg[1] = temp_seg;
5514 }
5515}
252b5132 5516
29b0f896
AM
5517/* Try to ensure constant immediates are represented in the smallest
5518 opcode possible. */
5519static void
e3bb37b5 5520optimize_imm (void)
29b0f896
AM
5521{
5522 char guess_suffix = 0;
5523 int op;
252b5132 5524
29b0f896
AM
5525 if (i.suffix)
5526 guess_suffix = i.suffix;
5527 else if (i.reg_operands)
5528 {
5529 /* Figure out a suffix from the last register operand specified.
75e5731b
JB
5530 We can't do this properly yet, i.e. excluding special register
5531 instances, but the following works for instructions with
5532 immediates. In any case, we can't set i.suffix yet. */
29b0f896 5533 for (op = i.operands; --op >= 0;)
bab6aec1
JB
5534 if (i.types[op].bitfield.class != Reg)
5535 continue;
5536 else if (i.types[op].bitfield.byte)
7ab9ffdd 5537 {
40fb9820
L
5538 guess_suffix = BYTE_MNEM_SUFFIX;
5539 break;
5540 }
bab6aec1 5541 else if (i.types[op].bitfield.word)
252b5132 5542 {
40fb9820
L
5543 guess_suffix = WORD_MNEM_SUFFIX;
5544 break;
5545 }
bab6aec1 5546 else if (i.types[op].bitfield.dword)
40fb9820
L
5547 {
5548 guess_suffix = LONG_MNEM_SUFFIX;
5549 break;
5550 }
bab6aec1 5551 else if (i.types[op].bitfield.qword)
40fb9820
L
5552 {
5553 guess_suffix = QWORD_MNEM_SUFFIX;
29b0f896 5554 break;
252b5132 5555 }
29b0f896
AM
5556 }
5557 else if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
5558 guess_suffix = WORD_MNEM_SUFFIX;
5559
5560 for (op = i.operands; --op >= 0;)
40fb9820 5561 if (operand_type_check (i.types[op], imm))
29b0f896
AM
5562 {
5563 switch (i.op[op].imms->X_op)
252b5132 5564 {
29b0f896
AM
5565 case O_constant:
5566 /* If a suffix is given, this operand may be shortened. */
5567 switch (guess_suffix)
252b5132 5568 {
29b0f896 5569 case LONG_MNEM_SUFFIX:
40fb9820
L
5570 i.types[op].bitfield.imm32 = 1;
5571 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
5572 break;
5573 case WORD_MNEM_SUFFIX:
40fb9820
L
5574 i.types[op].bitfield.imm16 = 1;
5575 i.types[op].bitfield.imm32 = 1;
5576 i.types[op].bitfield.imm32s = 1;
5577 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
5578 break;
5579 case BYTE_MNEM_SUFFIX:
40fb9820
L
5580 i.types[op].bitfield.imm8 = 1;
5581 i.types[op].bitfield.imm8s = 1;
5582 i.types[op].bitfield.imm16 = 1;
5583 i.types[op].bitfield.imm32 = 1;
5584 i.types[op].bitfield.imm32s = 1;
5585 i.types[op].bitfield.imm64 = 1;
29b0f896 5586 break;
252b5132 5587 }
252b5132 5588
29b0f896
AM
5589 /* If this operand is at most 16 bits, convert it
5590 to a signed 16 bit number before trying to see
5591 whether it will fit in an even smaller size.
5592 This allows a 16-bit operand such as $0xffe0 to
5593 be recognised as within Imm8S range. */
40fb9820 5594 if ((i.types[op].bitfield.imm16)
29b0f896 5595 && (i.op[op].imms->X_add_number & ~(offsetT) 0xffff) == 0)
252b5132 5596 {
29b0f896
AM
5597 i.op[op].imms->X_add_number =
5598 (((i.op[op].imms->X_add_number & 0xffff) ^ 0x8000) - 0x8000);
5599 }
a28def75
L
5600#ifdef BFD64
5601 /* Store 32-bit immediate in 64-bit for 64-bit BFD. */
40fb9820 5602 if ((i.types[op].bitfield.imm32)
29b0f896
AM
5603 && ((i.op[op].imms->X_add_number & ~(((offsetT) 2 << 31) - 1))
5604 == 0))
5605 {
5606 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
5607 ^ ((offsetT) 1 << 31))
5608 - ((offsetT) 1 << 31));
5609 }
a28def75 5610#endif
40fb9820 5611 i.types[op]
c6fb90c8
L
5612 = operand_type_or (i.types[op],
5613 smallest_imm_type (i.op[op].imms->X_add_number));
252b5132 5614
29b0f896
AM
5615 /* We must avoid matching of Imm32 templates when 64bit
5616 only immediate is available. */
5617 if (guess_suffix == QWORD_MNEM_SUFFIX)
40fb9820 5618 i.types[op].bitfield.imm32 = 0;
29b0f896 5619 break;
252b5132 5620
29b0f896
AM
5621 case O_absent:
5622 case O_register:
5623 abort ();
5624
5625 /* Symbols and expressions. */
5626 default:
9cd96992
JB
5627 /* Convert symbolic operand to proper sizes for matching, but don't
5628 prevent matching a set of insns that only supports sizes other
5629 than those matching the insn suffix. */
5630 {
40fb9820 5631 i386_operand_type mask, allowed;
d3ce72d0 5632 const insn_template *t;
9cd96992 5633
0dfbf9d7
L
5634 operand_type_set (&mask, 0);
5635 operand_type_set (&allowed, 0);
40fb9820 5636
4eed87de
AM
5637 for (t = current_templates->start;
5638 t < current_templates->end;
5639 ++t)
bab6aec1
JB
5640 {
5641 allowed = operand_type_or (allowed, t->operand_types[op]);
5642 allowed = operand_type_and (allowed, anyimm);
5643 }
9cd96992
JB
5644 switch (guess_suffix)
5645 {
5646 case QWORD_MNEM_SUFFIX:
40fb9820
L
5647 mask.bitfield.imm64 = 1;
5648 mask.bitfield.imm32s = 1;
9cd96992
JB
5649 break;
5650 case LONG_MNEM_SUFFIX:
40fb9820 5651 mask.bitfield.imm32 = 1;
9cd96992
JB
5652 break;
5653 case WORD_MNEM_SUFFIX:
40fb9820 5654 mask.bitfield.imm16 = 1;
9cd96992
JB
5655 break;
5656 case BYTE_MNEM_SUFFIX:
40fb9820 5657 mask.bitfield.imm8 = 1;
9cd96992
JB
5658 break;
5659 default:
9cd96992
JB
5660 break;
5661 }
c6fb90c8 5662 allowed = operand_type_and (mask, allowed);
0dfbf9d7 5663 if (!operand_type_all_zero (&allowed))
c6fb90c8 5664 i.types[op] = operand_type_and (i.types[op], mask);
9cd96992 5665 }
29b0f896 5666 break;
252b5132 5667 }
29b0f896
AM
5668 }
5669}
47926f60 5670
29b0f896
AM
5671/* Try to use the smallest displacement type too. */
5672static void
e3bb37b5 5673optimize_disp (void)
29b0f896
AM
5674{
5675 int op;
3e73aa7c 5676
29b0f896 5677 for (op = i.operands; --op >= 0;)
40fb9820 5678 if (operand_type_check (i.types[op], disp))
252b5132 5679 {
b300c311 5680 if (i.op[op].disps->X_op == O_constant)
252b5132 5681 {
91d6fa6a 5682 offsetT op_disp = i.op[op].disps->X_add_number;
29b0f896 5683
40fb9820 5684 if (i.types[op].bitfield.disp16
91d6fa6a 5685 && (op_disp & ~(offsetT) 0xffff) == 0)
b300c311
L
5686 {
5687 /* If this operand is at most 16 bits, convert
5688 to a signed 16 bit number and don't use 64bit
5689 displacement. */
91d6fa6a 5690 op_disp = (((op_disp & 0xffff) ^ 0x8000) - 0x8000);
40fb9820 5691 i.types[op].bitfield.disp64 = 0;
b300c311 5692 }
a28def75
L
5693#ifdef BFD64
5694 /* Optimize 64-bit displacement to 32-bit for 64-bit BFD. */
40fb9820 5695 if (i.types[op].bitfield.disp32
91d6fa6a 5696 && (op_disp & ~(((offsetT) 2 << 31) - 1)) == 0)
b300c311
L
5697 {
5698 /* If this operand is at most 32 bits, convert
5699 to a signed 32 bit number and don't use 64bit
5700 displacement. */
91d6fa6a
NC
5701 op_disp &= (((offsetT) 2 << 31) - 1);
5702 op_disp = (op_disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
40fb9820 5703 i.types[op].bitfield.disp64 = 0;
b300c311 5704 }
a28def75 5705#endif
91d6fa6a 5706 if (!op_disp && i.types[op].bitfield.baseindex)
b300c311 5707 {
40fb9820
L
5708 i.types[op].bitfield.disp8 = 0;
5709 i.types[op].bitfield.disp16 = 0;
5710 i.types[op].bitfield.disp32 = 0;
5711 i.types[op].bitfield.disp32s = 0;
5712 i.types[op].bitfield.disp64 = 0;
b300c311
L
5713 i.op[op].disps = 0;
5714 i.disp_operands--;
5715 }
5716 else if (flag_code == CODE_64BIT)
5717 {
91d6fa6a 5718 if (fits_in_signed_long (op_disp))
28a9d8f5 5719 {
40fb9820
L
5720 i.types[op].bitfield.disp64 = 0;
5721 i.types[op].bitfield.disp32s = 1;
28a9d8f5 5722 }
0e1147d9 5723 if (i.prefix[ADDR_PREFIX]
91d6fa6a 5724 && fits_in_unsigned_long (op_disp))
40fb9820 5725 i.types[op].bitfield.disp32 = 1;
b300c311 5726 }
40fb9820
L
5727 if ((i.types[op].bitfield.disp32
5728 || i.types[op].bitfield.disp32s
5729 || i.types[op].bitfield.disp16)
b5014f7a 5730 && fits_in_disp8 (op_disp))
40fb9820 5731 i.types[op].bitfield.disp8 = 1;
252b5132 5732 }
67a4f2b7
AO
5733 else if (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
5734 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL)
5735 {
5736 fix_new_exp (frag_now, frag_more (0) - frag_now->fr_literal, 0,
5737 i.op[op].disps, 0, i.reloc[op]);
40fb9820
L
5738 i.types[op].bitfield.disp8 = 0;
5739 i.types[op].bitfield.disp16 = 0;
5740 i.types[op].bitfield.disp32 = 0;
5741 i.types[op].bitfield.disp32s = 0;
5742 i.types[op].bitfield.disp64 = 0;
67a4f2b7
AO
5743 }
5744 else
b300c311 5745 /* We only support 64bit displacement on constants. */
40fb9820 5746 i.types[op].bitfield.disp64 = 0;
252b5132 5747 }
29b0f896
AM
5748}
5749
4a1b91ea
L
5750/* Return 1 if there is a match in broadcast bytes between operand
5751 GIVEN and instruction template T. */
5752
5753static INLINE int
5754match_broadcast_size (const insn_template *t, unsigned int given)
5755{
5756 return ((t->opcode_modifier.broadcast == BYTE_BROADCAST
5757 && i.types[given].bitfield.byte)
5758 || (t->opcode_modifier.broadcast == WORD_BROADCAST
5759 && i.types[given].bitfield.word)
5760 || (t->opcode_modifier.broadcast == DWORD_BROADCAST
5761 && i.types[given].bitfield.dword)
5762 || (t->opcode_modifier.broadcast == QWORD_BROADCAST
5763 && i.types[given].bitfield.qword));
5764}
5765
6c30d220
L
5766/* Check if operands are valid for the instruction. */
5767
5768static int
5769check_VecOperands (const insn_template *t)
5770{
43234a1e 5771 unsigned int op;
e2195274 5772 i386_cpu_flags cpu;
e2195274
JB
5773
5774 /* Templates allowing for ZMMword as well as YMMword and/or XMMword for
5775 any one operand are implicity requiring AVX512VL support if the actual
5776 operand size is YMMword or XMMword. Since this function runs after
5777 template matching, there's no need to check for YMMword/XMMword in
5778 the template. */
5779 cpu = cpu_flags_and (t->cpu_flags, avx512);
5780 if (!cpu_flags_all_zero (&cpu)
5781 && !t->cpu_flags.bitfield.cpuavx512vl
5782 && !cpu_arch_flags.bitfield.cpuavx512vl)
5783 {
5784 for (op = 0; op < t->operands; ++op)
5785 {
5786 if (t->operand_types[op].bitfield.zmmword
5787 && (i.types[op].bitfield.ymmword
5788 || i.types[op].bitfield.xmmword))
5789 {
5790 i.error = unsupported;
5791 return 1;
5792 }
5793 }
5794 }
43234a1e 5795
6c30d220 5796 /* Without VSIB byte, we can't have a vector register for index. */
63112cd6 5797 if (!t->opcode_modifier.sib
6c30d220 5798 && i.index_reg
1b54b8d7
JB
5799 && (i.index_reg->reg_type.bitfield.xmmword
5800 || i.index_reg->reg_type.bitfield.ymmword
5801 || i.index_reg->reg_type.bitfield.zmmword))
6c30d220
L
5802 {
5803 i.error = unsupported_vector_index_register;
5804 return 1;
5805 }
5806
ad8ecc81
MZ
5807 /* Check if default mask is allowed. */
5808 if (t->opcode_modifier.nodefmask
6225c532 5809 && (!i.mask.reg || i.mask.reg->reg_num == 0))
ad8ecc81
MZ
5810 {
5811 i.error = no_default_mask;
5812 return 1;
5813 }
5814
7bab8ab5
JB
5815 /* For VSIB byte, we need a vector register for index, and all vector
5816 registers must be distinct. */
260cd341 5817 if (t->opcode_modifier.sib && t->opcode_modifier.sib != SIBMEM)
7bab8ab5
JB
5818 {
5819 if (!i.index_reg
63112cd6 5820 || !((t->opcode_modifier.sib == VECSIB128
1b54b8d7 5821 && i.index_reg->reg_type.bitfield.xmmword)
63112cd6 5822 || (t->opcode_modifier.sib == VECSIB256
1b54b8d7 5823 && i.index_reg->reg_type.bitfield.ymmword)
63112cd6 5824 || (t->opcode_modifier.sib == VECSIB512
1b54b8d7 5825 && i.index_reg->reg_type.bitfield.zmmword)))
7bab8ab5
JB
5826 {
5827 i.error = invalid_vsib_address;
5828 return 1;
5829 }
5830
6225c532
JB
5831 gas_assert (i.reg_operands == 2 || i.mask.reg);
5832 if (i.reg_operands == 2 && !i.mask.reg)
43234a1e 5833 {
3528c362 5834 gas_assert (i.types[0].bitfield.class == RegSIMD);
1b54b8d7
JB
5835 gas_assert (i.types[0].bitfield.xmmword
5836 || i.types[0].bitfield.ymmword);
3528c362 5837 gas_assert (i.types[2].bitfield.class == RegSIMD);
1b54b8d7
JB
5838 gas_assert (i.types[2].bitfield.xmmword
5839 || i.types[2].bitfield.ymmword);
43234a1e
L
5840 if (operand_check == check_none)
5841 return 0;
5842 if (register_number (i.op[0].regs)
5843 != register_number (i.index_reg)
5844 && register_number (i.op[2].regs)
5845 != register_number (i.index_reg)
5846 && register_number (i.op[0].regs)
5847 != register_number (i.op[2].regs))
5848 return 0;
5849 if (operand_check == check_error)
5850 {
5851 i.error = invalid_vector_register_set;
5852 return 1;
5853 }
5854 as_warn (_("mask, index, and destination registers should be distinct"));
5855 }
6225c532 5856 else if (i.reg_operands == 1 && i.mask.reg)
8444f82a 5857 {
3528c362 5858 if (i.types[1].bitfield.class == RegSIMD
1b54b8d7
JB
5859 && (i.types[1].bitfield.xmmword
5860 || i.types[1].bitfield.ymmword
5861 || i.types[1].bitfield.zmmword)
8444f82a
MZ
5862 && (register_number (i.op[1].regs)
5863 == register_number (i.index_reg)))
5864 {
5865 if (operand_check == check_error)
5866 {
5867 i.error = invalid_vector_register_set;
5868 return 1;
5869 }
5870 if (operand_check != check_none)
5871 as_warn (_("index and destination registers should be distinct"));
5872 }
5873 }
43234a1e 5874 }
7bab8ab5 5875
260cd341
LC
5876 /* For AMX instructions with three tmmword operands, all tmmword operand must be
5877 distinct */
5878 if (t->operand_types[0].bitfield.tmmword
5879 && i.reg_operands == 3)
5880 {
5881 if (register_number (i.op[0].regs)
5882 == register_number (i.op[1].regs)
5883 || register_number (i.op[0].regs)
5884 == register_number (i.op[2].regs)
5885 || register_number (i.op[1].regs)
5886 == register_number (i.op[2].regs))
5887 {
5888 i.error = invalid_tmm_register_set;
5889 return 1;
5890 }
5891 }
5892
43234a1e
L
5893 /* Check if broadcast is supported by the instruction and is applied
5894 to the memory operand. */
5895 if (i.broadcast)
5896 {
8e6e0792 5897 i386_operand_type type, overlap;
43234a1e
L
5898
5899 /* Check if specified broadcast is supported in this instruction,
4a1b91ea 5900 and its broadcast bytes match the memory operand. */
32546502 5901 op = i.broadcast->operand;
8e6e0792 5902 if (!t->opcode_modifier.broadcast
c48dadc9 5903 || !(i.flags[op] & Operand_Mem)
c39e5b26 5904 || (!i.types[op].bitfield.unspecified
4a1b91ea 5905 && !match_broadcast_size (t, op)))
43234a1e
L
5906 {
5907 bad_broadcast:
5908 i.error = unsupported_broadcast;
5909 return 1;
5910 }
8e6e0792 5911
4a1b91ea
L
5912 i.broadcast->bytes = ((1 << (t->opcode_modifier.broadcast - 1))
5913 * i.broadcast->type);
8e6e0792 5914 operand_type_set (&type, 0);
4a1b91ea 5915 switch (i.broadcast->bytes)
8e6e0792 5916 {
4a1b91ea
L
5917 case 2:
5918 type.bitfield.word = 1;
5919 break;
5920 case 4:
5921 type.bitfield.dword = 1;
5922 break;
8e6e0792
JB
5923 case 8:
5924 type.bitfield.qword = 1;
5925 break;
5926 case 16:
5927 type.bitfield.xmmword = 1;
5928 break;
5929 case 32:
5930 type.bitfield.ymmword = 1;
5931 break;
5932 case 64:
5933 type.bitfield.zmmword = 1;
5934 break;
5935 default:
5936 goto bad_broadcast;
5937 }
5938
5939 overlap = operand_type_and (type, t->operand_types[op]);
bc49bfd8
JB
5940 if (t->operand_types[op].bitfield.class == RegSIMD
5941 && t->operand_types[op].bitfield.byte
5942 + t->operand_types[op].bitfield.word
5943 + t->operand_types[op].bitfield.dword
5944 + t->operand_types[op].bitfield.qword > 1)
5945 {
5946 overlap.bitfield.xmmword = 0;
5947 overlap.bitfield.ymmword = 0;
5948 overlap.bitfield.zmmword = 0;
5949 }
8e6e0792
JB
5950 if (operand_type_all_zero (&overlap))
5951 goto bad_broadcast;
5952
5953 if (t->opcode_modifier.checkregsize)
5954 {
5955 unsigned int j;
5956
e2195274 5957 type.bitfield.baseindex = 1;
8e6e0792
JB
5958 for (j = 0; j < i.operands; ++j)
5959 {
5960 if (j != op
5961 && !operand_type_register_match(i.types[j],
5962 t->operand_types[j],
5963 type,
5964 t->operand_types[op]))
5965 goto bad_broadcast;
5966 }
5967 }
43234a1e
L
5968 }
5969 /* If broadcast is supported in this instruction, we need to check if
5970 operand of one-element size isn't specified without broadcast. */
5971 else if (t->opcode_modifier.broadcast && i.mem_operands)
5972 {
5973 /* Find memory operand. */
5974 for (op = 0; op < i.operands; op++)
8dc0818e 5975 if (i.flags[op] & Operand_Mem)
43234a1e
L
5976 break;
5977 gas_assert (op < i.operands);
5978 /* Check size of the memory operand. */
4a1b91ea 5979 if (match_broadcast_size (t, op))
43234a1e
L
5980 {
5981 i.error = broadcast_needed;
5982 return 1;
5983 }
5984 }
c39e5b26
JB
5985 else
5986 op = MAX_OPERANDS - 1; /* Avoid uninitialized variable warning. */
43234a1e
L
5987
5988 /* Check if requested masking is supported. */
6225c532 5989 if (i.mask.reg)
43234a1e 5990 {
ae2387fe
JB
5991 switch (t->opcode_modifier.masking)
5992 {
5993 case BOTH_MASKING:
5994 break;
5995 case MERGING_MASKING:
6225c532 5996 if (i.mask.zeroing)
ae2387fe
JB
5997 {
5998 case 0:
5999 i.error = unsupported_masking;
6000 return 1;
6001 }
6002 break;
6003 case DYNAMIC_MASKING:
6004 /* Memory destinations allow only merging masking. */
6225c532 6005 if (i.mask.zeroing && i.mem_operands)
ae2387fe
JB
6006 {
6007 /* Find memory operand. */
6008 for (op = 0; op < i.operands; op++)
c48dadc9 6009 if (i.flags[op] & Operand_Mem)
ae2387fe
JB
6010 break;
6011 gas_assert (op < i.operands);
6012 if (op == i.operands - 1)
6013 {
6014 i.error = unsupported_masking;
6015 return 1;
6016 }
6017 }
6018 break;
6019 default:
6020 abort ();
6021 }
43234a1e
L
6022 }
6023
6024 /* Check if masking is applied to dest operand. */
6225c532 6025 if (i.mask.reg && (i.mask.operand != i.operands - 1))
43234a1e
L
6026 {
6027 i.error = mask_not_on_destination;
6028 return 1;
6029 }
6030
43234a1e
L
6031 /* Check RC/SAE. */
6032 if (i.rounding)
6033 {
a80195f1
JB
6034 if (!t->opcode_modifier.sae
6035 || (i.rounding->type != saeonly && !t->opcode_modifier.staticrounding))
43234a1e
L
6036 {
6037 i.error = unsupported_rc_sae;
6038 return 1;
6039 }
6040 /* If the instruction has several immediate operands and one of
6041 them is rounding, the rounding operand should be the last
6042 immediate operand. */
6043 if (i.imm_operands > 1
783c187b 6044 && i.rounding->operand != i.imm_operands - 1)
7bab8ab5 6045 {
43234a1e 6046 i.error = rc_sae_operand_not_last_imm;
7bab8ab5
JB
6047 return 1;
6048 }
6c30d220
L
6049 }
6050
da4977e0
JB
6051 /* Check the special Imm4 cases; must be the first operand. */
6052 if (t->cpu_flags.bitfield.cpuxop && t->operands == 5)
6053 {
6054 if (i.op[0].imms->X_op != O_constant
6055 || !fits_in_imm4 (i.op[0].imms->X_add_number))
6056 {
6057 i.error = bad_imm4;
6058 return 1;
6059 }
6060
6061 /* Turn off Imm<N> so that update_imm won't complain. */
6062 operand_type_set (&i.types[0], 0);
6063 }
6064
43234a1e 6065 /* Check vector Disp8 operand. */
b5014f7a
JB
6066 if (t->opcode_modifier.disp8memshift
6067 && i.disp_encoding != disp_encoding_32bit)
43234a1e
L
6068 {
6069 if (i.broadcast)
4a1b91ea 6070 i.memshift = t->opcode_modifier.broadcast - 1;
7091c612 6071 else if (t->opcode_modifier.disp8memshift != DISP8_SHIFT_VL)
43234a1e 6072 i.memshift = t->opcode_modifier.disp8memshift;
7091c612
JB
6073 else
6074 {
6075 const i386_operand_type *type = NULL;
6076
6077 i.memshift = 0;
6078 for (op = 0; op < i.operands; op++)
8dc0818e 6079 if (i.flags[op] & Operand_Mem)
7091c612 6080 {
4174bfff
JB
6081 if (t->opcode_modifier.evex == EVEXLIG)
6082 i.memshift = 2 + (i.suffix == QWORD_MNEM_SUFFIX);
6083 else if (t->operand_types[op].bitfield.xmmword
6084 + t->operand_types[op].bitfield.ymmword
6085 + t->operand_types[op].bitfield.zmmword <= 1)
7091c612
JB
6086 type = &t->operand_types[op];
6087 else if (!i.types[op].bitfield.unspecified)
6088 type = &i.types[op];
6089 }
3528c362 6090 else if (i.types[op].bitfield.class == RegSIMD
4174bfff 6091 && t->opcode_modifier.evex != EVEXLIG)
7091c612
JB
6092 {
6093 if (i.types[op].bitfield.zmmword)
6094 i.memshift = 6;
6095 else if (i.types[op].bitfield.ymmword && i.memshift < 5)
6096 i.memshift = 5;
6097 else if (i.types[op].bitfield.xmmword && i.memshift < 4)
6098 i.memshift = 4;
6099 }
6100
6101 if (type)
6102 {
6103 if (type->bitfield.zmmword)
6104 i.memshift = 6;
6105 else if (type->bitfield.ymmword)
6106 i.memshift = 5;
6107 else if (type->bitfield.xmmword)
6108 i.memshift = 4;
6109 }
6110
6111 /* For the check in fits_in_disp8(). */
6112 if (i.memshift == 0)
6113 i.memshift = -1;
6114 }
43234a1e
L
6115
6116 for (op = 0; op < i.operands; op++)
6117 if (operand_type_check (i.types[op], disp)
6118 && i.op[op].disps->X_op == O_constant)
6119 {
b5014f7a 6120 if (fits_in_disp8 (i.op[op].disps->X_add_number))
43234a1e 6121 {
b5014f7a
JB
6122 i.types[op].bitfield.disp8 = 1;
6123 return 0;
43234a1e 6124 }
b5014f7a 6125 i.types[op].bitfield.disp8 = 0;
43234a1e
L
6126 }
6127 }
b5014f7a
JB
6128
6129 i.memshift = 0;
43234a1e 6130
6c30d220
L
6131 return 0;
6132}
6133
da4977e0 6134/* Check if encoding requirements are met by the instruction. */
a683cc34
SP
6135
6136static int
da4977e0 6137VEX_check_encoding (const insn_template *t)
a683cc34 6138{
da4977e0
JB
6139 if (i.vec_encoding == vex_encoding_error)
6140 {
6141 i.error = unsupported;
6142 return 1;
6143 }
6144
86fa6981 6145 if (i.vec_encoding == vex_encoding_evex)
43234a1e 6146 {
86fa6981 6147 /* This instruction must be encoded with EVEX prefix. */
e771e7c9 6148 if (!is_evex_encoding (t))
86fa6981
L
6149 {
6150 i.error = unsupported;
6151 return 1;
6152 }
6153 return 0;
43234a1e
L
6154 }
6155
a683cc34 6156 if (!t->opcode_modifier.vex)
86fa6981
L
6157 {
6158 /* This instruction template doesn't have VEX prefix. */
6159 if (i.vec_encoding != vex_encoding_default)
6160 {
6161 i.error = unsupported;
6162 return 1;
6163 }
6164 return 0;
6165 }
a683cc34 6166
a683cc34
SP
6167 return 0;
6168}
6169
d3ce72d0 6170static const insn_template *
83b16ac6 6171match_template (char mnem_suffix)
29b0f896
AM
6172{
6173 /* Points to template once we've found it. */
d3ce72d0 6174 const insn_template *t;
40fb9820 6175 i386_operand_type overlap0, overlap1, overlap2, overlap3;
c0f3af97 6176 i386_operand_type overlap4;
29b0f896 6177 unsigned int found_reverse_match;
dc2be329 6178 i386_opcode_modifier suffix_check;
40fb9820 6179 i386_operand_type operand_types [MAX_OPERANDS];
539e75ad 6180 int addr_prefix_disp;
45a4bb20 6181 unsigned int j, size_match, check_register;
5614d22c 6182 enum i386_error specific_error = 0;
29b0f896 6183
c0f3af97
L
6184#if MAX_OPERANDS != 5
6185# error "MAX_OPERANDS must be 5."
f48ff2ae
L
6186#endif
6187
29b0f896 6188 found_reverse_match = 0;
539e75ad 6189 addr_prefix_disp = -1;
40fb9820 6190
dc2be329 6191 /* Prepare for mnemonic suffix check. */
40fb9820 6192 memset (&suffix_check, 0, sizeof (suffix_check));
dc2be329
L
6193 switch (mnem_suffix)
6194 {
6195 case BYTE_MNEM_SUFFIX:
6196 suffix_check.no_bsuf = 1;
6197 break;
6198 case WORD_MNEM_SUFFIX:
6199 suffix_check.no_wsuf = 1;
6200 break;
6201 case SHORT_MNEM_SUFFIX:
6202 suffix_check.no_ssuf = 1;
6203 break;
6204 case LONG_MNEM_SUFFIX:
6205 suffix_check.no_lsuf = 1;
6206 break;
6207 case QWORD_MNEM_SUFFIX:
6208 suffix_check.no_qsuf = 1;
6209 break;
6210 default:
6211 /* NB: In Intel syntax, normally we can check for memory operand
6212 size when there is no mnemonic suffix. But jmp and call have
6213 2 different encodings with Dword memory operand size, one with
6214 No_ldSuf and the other without. i.suffix is set to
6215 LONG_DOUBLE_MNEM_SUFFIX to skip the one with No_ldSuf. */
6216 if (i.suffix == LONG_DOUBLE_MNEM_SUFFIX)
6217 suffix_check.no_ldsuf = 1;
83b16ac6
JB
6218 }
6219
01559ecc
L
6220 /* Must have right number of operands. */
6221 i.error = number_of_operands_mismatch;
6222
45aa61fe 6223 for (t = current_templates->start; t < current_templates->end; t++)
29b0f896 6224 {
539e75ad 6225 addr_prefix_disp = -1;
dbbc8b7e 6226 found_reverse_match = 0;
539e75ad 6227
29b0f896
AM
6228 if (i.operands != t->operands)
6229 continue;
6230
50aecf8c 6231 /* Check processor support. */
a65babc9 6232 i.error = unsupported;
45a4bb20 6233 if (cpu_flags_match (t) != CPU_FLAGS_PERFECT_MATCH)
50aecf8c
L
6234 continue;
6235
57392598
CL
6236 /* Check Pseudo Prefix. */
6237 i.error = unsupported;
6238 if (t->opcode_modifier.pseudovexprefix
6239 && !(i.vec_encoding == vex_encoding_vex
6240 || i.vec_encoding == vex_encoding_vex3))
6241 continue;
6242
e1d4d893 6243 /* Check AT&T mnemonic. */
a65babc9 6244 i.error = unsupported_with_intel_mnemonic;
e1d4d893 6245 if (intel_mnemonic && t->opcode_modifier.attmnemonic)
1efbbeb4
L
6246 continue;
6247
4b5aaf5f 6248 /* Check AT&T/Intel syntax. */
a65babc9 6249 i.error = unsupported_syntax;
5c07affc 6250 if ((intel_syntax && t->opcode_modifier.attsyntax)
4b5aaf5f 6251 || (!intel_syntax && t->opcode_modifier.intelsyntax))
1efbbeb4
L
6252 continue;
6253
4b5aaf5f
L
6254 /* Check Intel64/AMD64 ISA. */
6255 switch (isa64)
6256 {
6257 default:
6258 /* Default: Don't accept Intel64. */
6259 if (t->opcode_modifier.isa64 == INTEL64)
6260 continue;
6261 break;
6262 case amd64:
6263 /* -mamd64: Don't accept Intel64 and Intel64 only. */
6264 if (t->opcode_modifier.isa64 >= INTEL64)
6265 continue;
6266 break;
6267 case intel64:
6268 /* -mintel64: Don't accept AMD64. */
5990e377 6269 if (t->opcode_modifier.isa64 == AMD64 && flag_code == CODE_64BIT)
4b5aaf5f
L
6270 continue;
6271 break;
6272 }
6273
dc2be329 6274 /* Check the suffix. */
a65babc9 6275 i.error = invalid_instruction_suffix;
dc2be329
L
6276 if ((t->opcode_modifier.no_bsuf && suffix_check.no_bsuf)
6277 || (t->opcode_modifier.no_wsuf && suffix_check.no_wsuf)
6278 || (t->opcode_modifier.no_lsuf && suffix_check.no_lsuf)
6279 || (t->opcode_modifier.no_ssuf && suffix_check.no_ssuf)
6280 || (t->opcode_modifier.no_qsuf && suffix_check.no_qsuf)
6281 || (t->opcode_modifier.no_ldsuf && suffix_check.no_ldsuf))
83b16ac6 6282 continue;
29b0f896 6283
3ac21baa
JB
6284 size_match = operand_size_match (t);
6285 if (!size_match)
7d5e4556 6286 continue;
539e75ad 6287
6f2f06be
JB
6288 /* This is intentionally not
6289
0cfa3eb3 6290 if (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE))
6f2f06be
JB
6291
6292 as the case of a missing * on the operand is accepted (perhaps with
6293 a warning, issued further down). */
0cfa3eb3 6294 if (i.jumpabsolute && t->opcode_modifier.jump != JUMP_ABSOLUTE)
6f2f06be
JB
6295 {
6296 i.error = operand_type_mismatch;
6297 continue;
6298 }
6299
5c07affc
L
6300 for (j = 0; j < MAX_OPERANDS; j++)
6301 operand_types[j] = t->operand_types[j];
6302
e365e234
JB
6303 /* In general, don't allow
6304 - 64-bit operands outside of 64-bit mode,
6305 - 32-bit operands on pre-386. */
4873e243 6306 j = i.imm_operands + (t->operands > i.imm_operands + 1);
e365e234
JB
6307 if (((i.suffix == QWORD_MNEM_SUFFIX
6308 && flag_code != CODE_64BIT
389d00a5
JB
6309 && !(t->opcode_modifier.opcodespace == SPACE_0F
6310 && t->base_opcode == 0xc7
5e74b495 6311 && t->opcode_modifier.opcodeprefix == PREFIX_NONE
8b65b895 6312 && t->extension_opcode == 1) /* cmpxchg8b */)
e365e234
JB
6313 || (i.suffix == LONG_MNEM_SUFFIX
6314 && !cpu_arch_flags.bitfield.cpui386))
45aa61fe 6315 && (intel_syntax
3cd7f3e3 6316 ? (t->opcode_modifier.mnemonicsize != IGNORESIZE
45aa61fe
AM
6317 && !intel_float_operand (t->name))
6318 : intel_float_operand (t->name) != 2)
4873e243
JB
6319 && (t->operands == i.imm_operands
6320 || (operand_types[i.imm_operands].bitfield.class != RegMMX
6321 && operand_types[i.imm_operands].bitfield.class != RegSIMD
6322 && operand_types[i.imm_operands].bitfield.class != RegMask)
6323 || (operand_types[j].bitfield.class != RegMMX
6324 && operand_types[j].bitfield.class != RegSIMD
6325 && operand_types[j].bitfield.class != RegMask))
63112cd6 6326 && !t->opcode_modifier.sib)
192dc9c6
JB
6327 continue;
6328
29b0f896 6329 /* Do not verify operands when there are none. */
e365e234 6330 if (!t->operands)
da4977e0
JB
6331 {
6332 if (VEX_check_encoding (t))
6333 {
6334 specific_error = i.error;
6335 continue;
6336 }
6337
6338 /* We've found a match; break out of loop. */
6339 break;
6340 }
252b5132 6341
48bcea9f
JB
6342 if (!t->opcode_modifier.jump
6343 || t->opcode_modifier.jump == JUMP_ABSOLUTE)
6344 {
6345 /* There should be only one Disp operand. */
6346 for (j = 0; j < MAX_OPERANDS; j++)
6347 if (operand_type_check (operand_types[j], disp))
539e75ad 6348 break;
48bcea9f
JB
6349 if (j < MAX_OPERANDS)
6350 {
6351 bfd_boolean override = (i.prefix[ADDR_PREFIX] != 0);
6352
6353 addr_prefix_disp = j;
6354
6355 /* Address size prefix will turn Disp64/Disp32S/Disp32/Disp16
6356 operand into Disp32/Disp32/Disp16/Disp32 operand. */
6357 switch (flag_code)
40fb9820 6358 {
48bcea9f
JB
6359 case CODE_16BIT:
6360 override = !override;
6361 /* Fall through. */
6362 case CODE_32BIT:
6363 if (operand_types[j].bitfield.disp32
6364 && operand_types[j].bitfield.disp16)
40fb9820 6365 {
48bcea9f
JB
6366 operand_types[j].bitfield.disp16 = override;
6367 operand_types[j].bitfield.disp32 = !override;
40fb9820 6368 }
48bcea9f
JB
6369 operand_types[j].bitfield.disp32s = 0;
6370 operand_types[j].bitfield.disp64 = 0;
6371 break;
6372
6373 case CODE_64BIT:
6374 if (operand_types[j].bitfield.disp32s
6375 || operand_types[j].bitfield.disp64)
40fb9820 6376 {
48bcea9f
JB
6377 operand_types[j].bitfield.disp64 &= !override;
6378 operand_types[j].bitfield.disp32s &= !override;
6379 operand_types[j].bitfield.disp32 = override;
40fb9820 6380 }
48bcea9f
JB
6381 operand_types[j].bitfield.disp16 = 0;
6382 break;
40fb9820 6383 }
539e75ad 6384 }
48bcea9f 6385 }
539e75ad 6386
02a86693 6387 /* Force 0x8b encoding for "mov foo@GOT, %eax". */
35648716
JB
6388 if (i.reloc[0] == BFD_RELOC_386_GOT32
6389 && t->base_opcode == 0xa0
6390 && t->opcode_modifier.opcodespace == SPACE_BASE)
02a86693
L
6391 continue;
6392
56ffb741 6393 /* We check register size if needed. */
e2195274
JB
6394 if (t->opcode_modifier.checkregsize)
6395 {
6396 check_register = (1 << t->operands) - 1;
6397 if (i.broadcast)
6398 check_register &= ~(1 << i.broadcast->operand);
6399 }
6400 else
6401 check_register = 0;
6402
c6fb90c8 6403 overlap0 = operand_type_and (i.types[0], operand_types[0]);
29b0f896
AM
6404 switch (t->operands)
6405 {
6406 case 1:
40fb9820 6407 if (!operand_type_match (overlap0, i.types[0]))
29b0f896
AM
6408 continue;
6409 break;
6410 case 2:
33eaf5de 6411 /* xchg %eax, %eax is a special case. It is an alias for nop
8b38ad71
L
6412 only in 32bit mode and we can use opcode 0x90. In 64bit
6413 mode, we can't use 0x90 for xchg %eax, %eax since it should
6414 zero-extend %eax to %rax. */
6415 if (flag_code == CODE_64BIT
6416 && t->base_opcode == 0x90
35648716 6417 && t->opcode_modifier.opcodespace == SPACE_BASE
75e5731b
JB
6418 && i.types[0].bitfield.instance == Accum
6419 && i.types[0].bitfield.dword
6420 && i.types[1].bitfield.instance == Accum
6421 && i.types[1].bitfield.dword)
8b38ad71 6422 continue;
1212781b
JB
6423 /* xrelease mov %eax, <disp> is another special case. It must not
6424 match the accumulator-only encoding of mov. */
6425 if (flag_code != CODE_64BIT
6426 && i.hle_prefix
6427 && t->base_opcode == 0xa0
35648716 6428 && t->opcode_modifier.opcodespace == SPACE_BASE
75e5731b 6429 && i.types[0].bitfield.instance == Accum
8dc0818e 6430 && (i.flags[1] & Operand_Mem))
1212781b 6431 continue;
f5eb1d70
JB
6432 /* Fall through. */
6433
6434 case 3:
3ac21baa
JB
6435 if (!(size_match & MATCH_STRAIGHT))
6436 goto check_reverse;
64c49ab3
JB
6437 /* Reverse direction of operands if swapping is possible in the first
6438 place (operands need to be symmetric) and
6439 - the load form is requested, and the template is a store form,
6440 - the store form is requested, and the template is a load form,
6441 - the non-default (swapped) form is requested. */
6442 overlap1 = operand_type_and (operand_types[0], operand_types[1]);
f5eb1d70 6443 if (t->opcode_modifier.d && i.reg_operands == i.operands
64c49ab3
JB
6444 && !operand_type_all_zero (&overlap1))
6445 switch (i.dir_encoding)
6446 {
6447 case dir_encoding_load:
6448 if (operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 6449 || t->opcode_modifier.regmem)
64c49ab3
JB
6450 goto check_reverse;
6451 break;
6452
6453 case dir_encoding_store:
6454 if (!operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 6455 && !t->opcode_modifier.regmem)
64c49ab3
JB
6456 goto check_reverse;
6457 break;
6458
6459 case dir_encoding_swap:
6460 goto check_reverse;
6461
6462 case dir_encoding_default:
6463 break;
6464 }
86fa6981 6465 /* If we want store form, we skip the current load. */
64c49ab3
JB
6466 if ((i.dir_encoding == dir_encoding_store
6467 || i.dir_encoding == dir_encoding_swap)
86fa6981
L
6468 && i.mem_operands == 0
6469 && t->opcode_modifier.load)
fa99fab2 6470 continue;
1a0670f3 6471 /* Fall through. */
f48ff2ae 6472 case 4:
c0f3af97 6473 case 5:
c6fb90c8 6474 overlap1 = operand_type_and (i.types[1], operand_types[1]);
40fb9820
L
6475 if (!operand_type_match (overlap0, i.types[0])
6476 || !operand_type_match (overlap1, i.types[1])
e2195274 6477 || ((check_register & 3) == 3
dc821c5f 6478 && !operand_type_register_match (i.types[0],
40fb9820 6479 operand_types[0],
dc821c5f 6480 i.types[1],
40fb9820 6481 operand_types[1])))
29b0f896
AM
6482 {
6483 /* Check if other direction is valid ... */
38e314eb 6484 if (!t->opcode_modifier.d)
29b0f896
AM
6485 continue;
6486
dc1e8a47 6487 check_reverse:
3ac21baa
JB
6488 if (!(size_match & MATCH_REVERSE))
6489 continue;
29b0f896 6490 /* Try reversing direction of operands. */
f5eb1d70
JB
6491 overlap0 = operand_type_and (i.types[0], operand_types[i.operands - 1]);
6492 overlap1 = operand_type_and (i.types[i.operands - 1], operand_types[0]);
40fb9820 6493 if (!operand_type_match (overlap0, i.types[0])
f5eb1d70 6494 || !operand_type_match (overlap1, i.types[i.operands - 1])
45664ddb 6495 || (check_register
dc821c5f 6496 && !operand_type_register_match (i.types[0],
f5eb1d70
JB
6497 operand_types[i.operands - 1],
6498 i.types[i.operands - 1],
45664ddb 6499 operand_types[0])))
29b0f896
AM
6500 {
6501 /* Does not match either direction. */
6502 continue;
6503 }
38e314eb 6504 /* found_reverse_match holds which of D or FloatR
29b0f896 6505 we've found. */
38e314eb
JB
6506 if (!t->opcode_modifier.d)
6507 found_reverse_match = 0;
6508 else if (operand_types[0].bitfield.tbyte)
8a2ed489 6509 found_reverse_match = Opcode_FloatD;
dbbc8b7e 6510 else if (operand_types[0].bitfield.xmmword
f5eb1d70 6511 || operand_types[i.operands - 1].bitfield.xmmword
3528c362
JB
6512 || operand_types[0].bitfield.class == RegMMX
6513 || operand_types[i.operands - 1].bitfield.class == RegMMX
dbbc8b7e
JB
6514 || is_any_vex_encoding(t))
6515 found_reverse_match = (t->base_opcode & 0xee) != 0x6e
6516 ? Opcode_SIMD_FloatD : Opcode_SIMD_IntD;
8a2ed489 6517 else
38e314eb 6518 found_reverse_match = Opcode_D;
40fb9820 6519 if (t->opcode_modifier.floatr)
8a2ed489 6520 found_reverse_match |= Opcode_FloatR;
29b0f896 6521 }
f48ff2ae 6522 else
29b0f896 6523 {
f48ff2ae 6524 /* Found a forward 2 operand match here. */
d1cbb4db
L
6525 switch (t->operands)
6526 {
c0f3af97
L
6527 case 5:
6528 overlap4 = operand_type_and (i.types[4],
6529 operand_types[4]);
1a0670f3 6530 /* Fall through. */
d1cbb4db 6531 case 4:
c6fb90c8
L
6532 overlap3 = operand_type_and (i.types[3],
6533 operand_types[3]);
1a0670f3 6534 /* Fall through. */
d1cbb4db 6535 case 3:
c6fb90c8
L
6536 overlap2 = operand_type_and (i.types[2],
6537 operand_types[2]);
d1cbb4db
L
6538 break;
6539 }
29b0f896 6540
f48ff2ae
L
6541 switch (t->operands)
6542 {
c0f3af97
L
6543 case 5:
6544 if (!operand_type_match (overlap4, i.types[4])
dc821c5f 6545 || !operand_type_register_match (i.types[3],
c0f3af97 6546 operand_types[3],
c0f3af97
L
6547 i.types[4],
6548 operand_types[4]))
6549 continue;
1a0670f3 6550 /* Fall through. */
f48ff2ae 6551 case 4:
40fb9820 6552 if (!operand_type_match (overlap3, i.types[3])
e2195274
JB
6553 || ((check_register & 0xa) == 0xa
6554 && !operand_type_register_match (i.types[1],
f7768225
JB
6555 operand_types[1],
6556 i.types[3],
e2195274
JB
6557 operand_types[3]))
6558 || ((check_register & 0xc) == 0xc
6559 && !operand_type_register_match (i.types[2],
6560 operand_types[2],
6561 i.types[3],
6562 operand_types[3])))
f48ff2ae 6563 continue;
1a0670f3 6564 /* Fall through. */
f48ff2ae
L
6565 case 3:
6566 /* Here we make use of the fact that there are no
23e42951 6567 reverse match 3 operand instructions. */
40fb9820 6568 if (!operand_type_match (overlap2, i.types[2])
e2195274
JB
6569 || ((check_register & 5) == 5
6570 && !operand_type_register_match (i.types[0],
23e42951
JB
6571 operand_types[0],
6572 i.types[2],
e2195274
JB
6573 operand_types[2]))
6574 || ((check_register & 6) == 6
6575 && !operand_type_register_match (i.types[1],
6576 operand_types[1],
6577 i.types[2],
6578 operand_types[2])))
f48ff2ae
L
6579 continue;
6580 break;
6581 }
29b0f896 6582 }
f48ff2ae 6583 /* Found either forward/reverse 2, 3 or 4 operand match here:
29b0f896
AM
6584 slip through to break. */
6585 }
c0f3af97 6586
da4977e0
JB
6587 /* Check if vector operands are valid. */
6588 if (check_VecOperands (t))
6589 {
6590 specific_error = i.error;
6591 continue;
6592 }
6593
6594 /* Check if VEX/EVEX encoding requirements can be satisfied. */
6595 if (VEX_check_encoding (t))
5614d22c
JB
6596 {
6597 specific_error = i.error;
6598 continue;
6599 }
a683cc34 6600
29b0f896
AM
6601 /* We've found a match; break out of loop. */
6602 break;
6603 }
6604
6605 if (t == current_templates->end)
6606 {
6607 /* We found no match. */
a65babc9 6608 const char *err_msg;
5614d22c 6609 switch (specific_error ? specific_error : i.error)
a65babc9
L
6610 {
6611 default:
6612 abort ();
86e026a4 6613 case operand_size_mismatch:
a65babc9
L
6614 err_msg = _("operand size mismatch");
6615 break;
6616 case operand_type_mismatch:
6617 err_msg = _("operand type mismatch");
6618 break;
6619 case register_type_mismatch:
6620 err_msg = _("register type mismatch");
6621 break;
6622 case number_of_operands_mismatch:
6623 err_msg = _("number of operands mismatch");
6624 break;
6625 case invalid_instruction_suffix:
6626 err_msg = _("invalid instruction suffix");
6627 break;
6628 case bad_imm4:
4a2608e3 6629 err_msg = _("constant doesn't fit in 4 bits");
a65babc9 6630 break;
a65babc9
L
6631 case unsupported_with_intel_mnemonic:
6632 err_msg = _("unsupported with Intel mnemonic");
6633 break;
6634 case unsupported_syntax:
6635 err_msg = _("unsupported syntax");
6636 break;
6637 case unsupported:
35262a23 6638 as_bad (_("unsupported instruction `%s'"),
10efe3f6
L
6639 current_templates->start->name);
6640 return NULL;
260cd341
LC
6641 case invalid_sib_address:
6642 err_msg = _("invalid SIB address");
6643 break;
6c30d220
L
6644 case invalid_vsib_address:
6645 err_msg = _("invalid VSIB address");
6646 break;
7bab8ab5
JB
6647 case invalid_vector_register_set:
6648 err_msg = _("mask, index, and destination registers must be distinct");
6649 break;
260cd341
LC
6650 case invalid_tmm_register_set:
6651 err_msg = _("all tmm registers must be distinct");
6652 break;
6c30d220
L
6653 case unsupported_vector_index_register:
6654 err_msg = _("unsupported vector index register");
6655 break;
43234a1e
L
6656 case unsupported_broadcast:
6657 err_msg = _("unsupported broadcast");
6658 break;
43234a1e
L
6659 case broadcast_needed:
6660 err_msg = _("broadcast is needed for operand of such type");
6661 break;
6662 case unsupported_masking:
6663 err_msg = _("unsupported masking");
6664 break;
6665 case mask_not_on_destination:
6666 err_msg = _("mask not on destination operand");
6667 break;
6668 case no_default_mask:
6669 err_msg = _("default mask isn't allowed");
6670 break;
6671 case unsupported_rc_sae:
6672 err_msg = _("unsupported static rounding/sae");
6673 break;
6674 case rc_sae_operand_not_last_imm:
6675 if (intel_syntax)
6676 err_msg = _("RC/SAE operand must precede immediate operands");
6677 else
6678 err_msg = _("RC/SAE operand must follow immediate operands");
6679 break;
6680 case invalid_register_operand:
6681 err_msg = _("invalid register operand");
6682 break;
a65babc9
L
6683 }
6684 as_bad (_("%s for `%s'"), err_msg,
891edac4 6685 current_templates->start->name);
fa99fab2 6686 return NULL;
29b0f896 6687 }
252b5132 6688
29b0f896
AM
6689 if (!quiet_warnings)
6690 {
6691 if (!intel_syntax
0cfa3eb3 6692 && (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE)))
6f2f06be 6693 as_warn (_("indirect %s without `*'"), t->name);
29b0f896 6694
40fb9820 6695 if (t->opcode_modifier.isprefix
3cd7f3e3 6696 && t->opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
6697 {
6698 /* Warn them that a data or address size prefix doesn't
6699 affect assembly of the next line of code. */
6700 as_warn (_("stand-alone `%s' prefix"), t->name);
6701 }
6702 }
6703
6704 /* Copy the template we found. */
9a182d04 6705 install_template (t);
539e75ad
L
6706
6707 if (addr_prefix_disp != -1)
6708 i.tm.operand_types[addr_prefix_disp]
6709 = operand_types[addr_prefix_disp];
6710
29b0f896
AM
6711 if (found_reverse_match)
6712 {
dfd69174
JB
6713 /* If we found a reverse match we must alter the opcode direction
6714 bit and clear/flip the regmem modifier one. found_reverse_match
6715 holds bits to change (different for int & float insns). */
29b0f896
AM
6716
6717 i.tm.base_opcode ^= found_reverse_match;
6718
f5eb1d70
JB
6719 i.tm.operand_types[0] = operand_types[i.operands - 1];
6720 i.tm.operand_types[i.operands - 1] = operand_types[0];
dfd69174
JB
6721
6722 /* Certain SIMD insns have their load forms specified in the opcode
6723 table, and hence we need to _set_ RegMem instead of clearing it.
6724 We need to avoid setting the bit though on insns like KMOVW. */
6725 i.tm.opcode_modifier.regmem
6726 = i.tm.opcode_modifier.modrm && i.tm.opcode_modifier.d
6727 && i.tm.operands > 2U - i.tm.opcode_modifier.sse2avx
6728 && !i.tm.opcode_modifier.regmem;
29b0f896
AM
6729 }
6730
fa99fab2 6731 return t;
29b0f896
AM
6732}
6733
6734static int
e3bb37b5 6735check_string (void)
29b0f896 6736{
51c8edf6
JB
6737 unsigned int es_op = i.tm.opcode_modifier.isstring - IS_STRING_ES_OP0;
6738 unsigned int op = i.tm.operand_types[0].bitfield.baseindex ? es_op : 0;
8dc0818e 6739
51c8edf6 6740 if (i.seg[op] != NULL && i.seg[op] != &es)
29b0f896 6741 {
51c8edf6
JB
6742 as_bad (_("`%s' operand %u must use `%ses' segment"),
6743 i.tm.name,
6744 intel_syntax ? i.tm.operands - es_op : es_op + 1,
6745 register_prefix);
6746 return 0;
29b0f896 6747 }
51c8edf6
JB
6748
6749 /* There's only ever one segment override allowed per instruction.
6750 This instruction possibly has a legal segment override on the
6751 second operand, so copy the segment to where non-string
6752 instructions store it, allowing common code. */
6753 i.seg[op] = i.seg[1];
6754
29b0f896
AM
6755 return 1;
6756}
6757
6758static int
543613e9 6759process_suffix (void)
29b0f896 6760{
389d00a5 6761 bfd_boolean is_crc32 = FALSE, is_movx = FALSE;
8b65b895 6762
29b0f896
AM
6763 /* If matched instruction specifies an explicit instruction mnemonic
6764 suffix, use it. */
673fe0f0 6765 if (i.tm.opcode_modifier.size == SIZE16)
40fb9820 6766 i.suffix = WORD_MNEM_SUFFIX;
673fe0f0 6767 else if (i.tm.opcode_modifier.size == SIZE32)
40fb9820 6768 i.suffix = LONG_MNEM_SUFFIX;
673fe0f0 6769 else if (i.tm.opcode_modifier.size == SIZE64)
40fb9820 6770 i.suffix = QWORD_MNEM_SUFFIX;
13e600d0 6771 else if (i.reg_operands
c8f8eebc
JB
6772 && (i.operands > 1 || i.types[0].bitfield.class == Reg)
6773 && !i.tm.opcode_modifier.addrprefixopreg)
29b0f896 6774 {
65fca059 6775 unsigned int numop = i.operands;
389d00a5
JB
6776
6777 /* MOVSX/MOVZX */
6778 is_movx = (i.tm.opcode_modifier.opcodespace == SPACE_0F
6779 && (i.tm.base_opcode | 8) == 0xbe)
6780 || (i.tm.opcode_modifier.opcodespace == SPACE_BASE
6781 && i.tm.base_opcode == 0x63
6782 && i.tm.cpu_flags.bitfield.cpu64);
6783
8b65b895 6784 /* CRC32 */
389d00a5
JB
6785 is_crc32 = (i.tm.base_opcode == 0xf0
6786 && i.tm.opcode_modifier.opcodespace == SPACE_0F38
8b65b895 6787 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2);
65fca059
JB
6788
6789 /* movsx/movzx want only their source operand considered here, for the
6790 ambiguity checking below. The suffix will be replaced afterwards
6791 to represent the destination (register). */
389d00a5 6792 if (is_movx && (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63))
65fca059
JB
6793 --i.operands;
6794
643bb870 6795 /* crc32 needs REX.W set regardless of suffix / source operand size. */
8b65b895 6796 if (is_crc32 && i.tm.operand_types[1].bitfield.qword)
643bb870
JB
6797 i.rex |= REX_W;
6798
29b0f896 6799 /* If there's no instruction mnemonic suffix we try to invent one
13e600d0 6800 based on GPR operands. */
29b0f896
AM
6801 if (!i.suffix)
6802 {
6803 /* We take i.suffix from the last register operand specified,
6804 Destination register type is more significant than source
381d071f
L
6805 register type. crc32 in SSE4.2 prefers source register
6806 type. */
8b65b895 6807 unsigned int op = is_crc32 ? 1 : i.operands;
20592a94 6808
1a035124
JB
6809 while (op--)
6810 if (i.tm.operand_types[op].bitfield.instance == InstanceNone
6811 || i.tm.operand_types[op].bitfield.instance == Accum)
6812 {
6813 if (i.types[op].bitfield.class != Reg)
6814 continue;
6815 if (i.types[op].bitfield.byte)
6816 i.suffix = BYTE_MNEM_SUFFIX;
6817 else if (i.types[op].bitfield.word)
6818 i.suffix = WORD_MNEM_SUFFIX;
6819 else if (i.types[op].bitfield.dword)
6820 i.suffix = LONG_MNEM_SUFFIX;
6821 else if (i.types[op].bitfield.qword)
6822 i.suffix = QWORD_MNEM_SUFFIX;
6823 else
6824 continue;
6825 break;
6826 }
65fca059
JB
6827
6828 /* As an exception, movsx/movzx silently default to a byte source
6829 in AT&T mode. */
389d00a5 6830 if (is_movx && i.tm.opcode_modifier.w && !i.suffix && !intel_syntax)
65fca059 6831 i.suffix = BYTE_MNEM_SUFFIX;
29b0f896
AM
6832 }
6833 else if (i.suffix == BYTE_MNEM_SUFFIX)
6834 {
2eb952a4 6835 if (intel_syntax
3cd7f3e3 6836 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
2eb952a4
L
6837 && i.tm.opcode_modifier.no_bsuf)
6838 i.suffix = 0;
6839 else if (!check_byte_reg ())
29b0f896
AM
6840 return 0;
6841 }
6842 else if (i.suffix == LONG_MNEM_SUFFIX)
6843 {
2eb952a4 6844 if (intel_syntax
3cd7f3e3 6845 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
9f123b91
JB
6846 && i.tm.opcode_modifier.no_lsuf
6847 && !i.tm.opcode_modifier.todword
6848 && !i.tm.opcode_modifier.toqword)
2eb952a4
L
6849 i.suffix = 0;
6850 else if (!check_long_reg ())
29b0f896
AM
6851 return 0;
6852 }
6853 else if (i.suffix == QWORD_MNEM_SUFFIX)
6854 {
955e1e6a 6855 if (intel_syntax
3cd7f3e3 6856 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
9f123b91
JB
6857 && i.tm.opcode_modifier.no_qsuf
6858 && !i.tm.opcode_modifier.todword
6859 && !i.tm.opcode_modifier.toqword)
955e1e6a
L
6860 i.suffix = 0;
6861 else if (!check_qword_reg ())
29b0f896
AM
6862 return 0;
6863 }
6864 else if (i.suffix == WORD_MNEM_SUFFIX)
6865 {
2eb952a4 6866 if (intel_syntax
3cd7f3e3 6867 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
2eb952a4
L
6868 && i.tm.opcode_modifier.no_wsuf)
6869 i.suffix = 0;
6870 else if (!check_word_reg ())
29b0f896
AM
6871 return 0;
6872 }
3cd7f3e3
L
6873 else if (intel_syntax
6874 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
6875 /* Do nothing if the instruction is going to ignore the prefix. */
6876 ;
6877 else
6878 abort ();
65fca059
JB
6879
6880 /* Undo the movsx/movzx change done above. */
6881 i.operands = numop;
29b0f896 6882 }
3cd7f3e3
L
6883 else if (i.tm.opcode_modifier.mnemonicsize == DEFAULTSIZE
6884 && !i.suffix)
29b0f896 6885 {
13e600d0
JB
6886 i.suffix = stackop_size;
6887 if (stackop_size == LONG_MNEM_SUFFIX)
06f74c5c
L
6888 {
6889 /* stackop_size is set to LONG_MNEM_SUFFIX for the
6890 .code16gcc directive to support 16-bit mode with
6891 32-bit address. For IRET without a suffix, generate
6892 16-bit IRET (opcode 0xcf) to return from an interrupt
6893 handler. */
13e600d0
JB
6894 if (i.tm.base_opcode == 0xcf)
6895 {
6896 i.suffix = WORD_MNEM_SUFFIX;
6897 as_warn (_("generating 16-bit `iret' for .code16gcc directive"));
6898 }
6899 /* Warn about changed behavior for segment register push/pop. */
6900 else if ((i.tm.base_opcode | 1) == 0x07)
6901 as_warn (_("generating 32-bit `%s', unlike earlier gas versions"),
6902 i.tm.name);
06f74c5c 6903 }
29b0f896 6904 }
c006a730 6905 else if (!i.suffix
0cfa3eb3
JB
6906 && (i.tm.opcode_modifier.jump == JUMP_ABSOLUTE
6907 || i.tm.opcode_modifier.jump == JUMP_BYTE
6908 || i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT
389d00a5
JB
6909 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
6910 && i.tm.base_opcode == 0x01 /* [ls][gi]dt */
64e74474 6911 && i.tm.extension_opcode <= 3)))
9306ca4a
JB
6912 {
6913 switch (flag_code)
6914 {
6915 case CODE_64BIT:
40fb9820 6916 if (!i.tm.opcode_modifier.no_qsuf)
9306ca4a 6917 {
828c2a25
JB
6918 if (i.tm.opcode_modifier.jump == JUMP_BYTE
6919 || i.tm.opcode_modifier.no_lsuf)
6920 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a
JB
6921 break;
6922 }
1a0670f3 6923 /* Fall through. */
9306ca4a 6924 case CODE_32BIT:
40fb9820 6925 if (!i.tm.opcode_modifier.no_lsuf)
9306ca4a
JB
6926 i.suffix = LONG_MNEM_SUFFIX;
6927 break;
6928 case CODE_16BIT:
40fb9820 6929 if (!i.tm.opcode_modifier.no_wsuf)
9306ca4a
JB
6930 i.suffix = WORD_MNEM_SUFFIX;
6931 break;
6932 }
6933 }
252b5132 6934
c006a730 6935 if (!i.suffix
3cd7f3e3 6936 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8
JB
6937 /* Also cover lret/retf/iret in 64-bit mode. */
6938 || (flag_code == CODE_64BIT
6939 && !i.tm.opcode_modifier.no_lsuf
6940 && !i.tm.opcode_modifier.no_qsuf))
3cd7f3e3 6941 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
8bbb3ad8
JB
6942 /* Explicit sizing prefixes are assumed to disambiguate insns. */
6943 && !i.prefix[DATA_PREFIX] && !(i.prefix[REX_PREFIX] & REX_W)
62b3f548
JB
6944 /* Accept FLDENV et al without suffix. */
6945 && (i.tm.opcode_modifier.no_ssuf || i.tm.opcode_modifier.floatmf))
29b0f896 6946 {
6c0946d0 6947 unsigned int suffixes, evex = 0;
c006a730
JB
6948
6949 suffixes = !i.tm.opcode_modifier.no_bsuf;
6950 if (!i.tm.opcode_modifier.no_wsuf)
6951 suffixes |= 1 << 1;
6952 if (!i.tm.opcode_modifier.no_lsuf)
6953 suffixes |= 1 << 2;
6954 if (!i.tm.opcode_modifier.no_ldsuf)
6955 suffixes |= 1 << 3;
6956 if (!i.tm.opcode_modifier.no_ssuf)
6957 suffixes |= 1 << 4;
6958 if (flag_code == CODE_64BIT && !i.tm.opcode_modifier.no_qsuf)
6959 suffixes |= 1 << 5;
6960
6c0946d0
JB
6961 /* For [XYZ]MMWORD operands inspect operand sizes. While generally
6962 also suitable for AT&T syntax mode, it was requested that this be
6963 restricted to just Intel syntax. */
b9915cbc 6964 if (intel_syntax && is_any_vex_encoding (&i.tm) && !i.broadcast)
6c0946d0 6965 {
b9915cbc 6966 unsigned int op;
6c0946d0 6967
b9915cbc 6968 for (op = 0; op < i.tm.operands; ++op)
6c0946d0 6969 {
b9915cbc
JB
6970 if (is_evex_encoding (&i.tm)
6971 && !cpu_arch_flags.bitfield.cpuavx512vl)
6c0946d0 6972 {
b9915cbc
JB
6973 if (i.tm.operand_types[op].bitfield.ymmword)
6974 i.tm.operand_types[op].bitfield.xmmword = 0;
6975 if (i.tm.operand_types[op].bitfield.zmmword)
6976 i.tm.operand_types[op].bitfield.ymmword = 0;
6977 if (!i.tm.opcode_modifier.evex
6978 || i.tm.opcode_modifier.evex == EVEXDYN)
6979 i.tm.opcode_modifier.evex = EVEX512;
6980 }
6c0946d0 6981
b9915cbc
JB
6982 if (i.tm.operand_types[op].bitfield.xmmword
6983 + i.tm.operand_types[op].bitfield.ymmword
6984 + i.tm.operand_types[op].bitfield.zmmword < 2)
6985 continue;
6c0946d0 6986
b9915cbc
JB
6987 /* Any properly sized operand disambiguates the insn. */
6988 if (i.types[op].bitfield.xmmword
6989 || i.types[op].bitfield.ymmword
6990 || i.types[op].bitfield.zmmword)
6991 {
6992 suffixes &= ~(7 << 6);
6993 evex = 0;
6994 break;
6995 }
6c0946d0 6996
b9915cbc
JB
6997 if ((i.flags[op] & Operand_Mem)
6998 && i.tm.operand_types[op].bitfield.unspecified)
6999 {
7000 if (i.tm.operand_types[op].bitfield.xmmword)
7001 suffixes |= 1 << 6;
7002 if (i.tm.operand_types[op].bitfield.ymmword)
7003 suffixes |= 1 << 7;
7004 if (i.tm.operand_types[op].bitfield.zmmword)
7005 suffixes |= 1 << 8;
7006 if (is_evex_encoding (&i.tm))
7007 evex = EVEX512;
6c0946d0
JB
7008 }
7009 }
7010 }
7011
7012 /* Are multiple suffixes / operand sizes allowed? */
c006a730 7013 if (suffixes & (suffixes - 1))
9306ca4a 7014 {
873494c8 7015 if (intel_syntax
3cd7f3e3 7016 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8 7017 || operand_check == check_error))
9306ca4a 7018 {
c006a730 7019 as_bad (_("ambiguous operand size for `%s'"), i.tm.name);
9306ca4a
JB
7020 return 0;
7021 }
c006a730 7022 if (operand_check == check_error)
9306ca4a 7023 {
c006a730
JB
7024 as_bad (_("no instruction mnemonic suffix given and "
7025 "no register operands; can't size `%s'"), i.tm.name);
9306ca4a
JB
7026 return 0;
7027 }
c006a730 7028 if (operand_check == check_warning)
873494c8
JB
7029 as_warn (_("%s; using default for `%s'"),
7030 intel_syntax
7031 ? _("ambiguous operand size")
7032 : _("no instruction mnemonic suffix given and "
7033 "no register operands"),
7034 i.tm.name);
c006a730
JB
7035
7036 if (i.tm.opcode_modifier.floatmf)
7037 i.suffix = SHORT_MNEM_SUFFIX;
389d00a5 7038 else if (is_movx)
65fca059 7039 /* handled below */;
6c0946d0
JB
7040 else if (evex)
7041 i.tm.opcode_modifier.evex = evex;
c006a730
JB
7042 else if (flag_code == CODE_16BIT)
7043 i.suffix = WORD_MNEM_SUFFIX;
1a035124 7044 else if (!i.tm.opcode_modifier.no_lsuf)
c006a730 7045 i.suffix = LONG_MNEM_SUFFIX;
1a035124
JB
7046 else
7047 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a 7048 }
29b0f896 7049 }
252b5132 7050
389d00a5 7051 if (is_movx)
65fca059
JB
7052 {
7053 /* In Intel syntax, movsx/movzx must have a "suffix" (checked above).
7054 In AT&T syntax, if there is no suffix (warned about above), the default
7055 will be byte extension. */
7056 if (i.tm.opcode_modifier.w && i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
7057 i.tm.base_opcode |= 1;
7058
7059 /* For further processing, the suffix should represent the destination
7060 (register). This is already the case when one was used with
7061 mov[sz][bw]*, but we need to replace it for mov[sz]x, or if there was
7062 no suffix to begin with. */
7063 if (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63 || !i.suffix)
7064 {
7065 if (i.types[1].bitfield.word)
7066 i.suffix = WORD_MNEM_SUFFIX;
7067 else if (i.types[1].bitfield.qword)
7068 i.suffix = QWORD_MNEM_SUFFIX;
7069 else
7070 i.suffix = LONG_MNEM_SUFFIX;
7071
7072 i.tm.opcode_modifier.w = 0;
7073 }
7074 }
7075
50128d0c
JB
7076 if (!i.tm.opcode_modifier.modrm && i.reg_operands && i.tm.operands < 3)
7077 i.short_form = (i.tm.operand_types[0].bitfield.class == Reg)
7078 != (i.tm.operand_types[1].bitfield.class == Reg);
7079
d2224064
JB
7080 /* Change the opcode based on the operand size given by i.suffix. */
7081 switch (i.suffix)
29b0f896 7082 {
d2224064
JB
7083 /* Size floating point instruction. */
7084 case LONG_MNEM_SUFFIX:
7085 if (i.tm.opcode_modifier.floatmf)
7086 {
7087 i.tm.base_opcode ^= 4;
7088 break;
7089 }
7090 /* fall through */
7091 case WORD_MNEM_SUFFIX:
7092 case QWORD_MNEM_SUFFIX:
29b0f896 7093 /* It's not a byte, select word/dword operation. */
40fb9820 7094 if (i.tm.opcode_modifier.w)
29b0f896 7095 {
50128d0c 7096 if (i.short_form)
29b0f896
AM
7097 i.tm.base_opcode |= 8;
7098 else
7099 i.tm.base_opcode |= 1;
7100 }
d2224064
JB
7101 /* fall through */
7102 case SHORT_MNEM_SUFFIX:
29b0f896
AM
7103 /* Now select between word & dword operations via the operand
7104 size prefix, except for instructions that will ignore this
7105 prefix anyway. */
c8f8eebc 7106 if (i.suffix != QWORD_MNEM_SUFFIX
3cd7f3e3 7107 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
c8f8eebc
JB
7108 && !i.tm.opcode_modifier.floatmf
7109 && !is_any_vex_encoding (&i.tm)
7110 && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
7111 || (flag_code == CODE_64BIT
7112 && i.tm.opcode_modifier.jump == JUMP_BYTE)))
24eab124
AM
7113 {
7114 unsigned int prefix = DATA_PREFIX_OPCODE;
543613e9 7115
0cfa3eb3 7116 if (i.tm.opcode_modifier.jump == JUMP_BYTE) /* jcxz, loop */
29b0f896 7117 prefix = ADDR_PREFIX_OPCODE;
252b5132 7118
29b0f896
AM
7119 if (!add_prefix (prefix))
7120 return 0;
24eab124 7121 }
252b5132 7122
29b0f896
AM
7123 /* Set mode64 for an operand. */
7124 if (i.suffix == QWORD_MNEM_SUFFIX
9146926a 7125 && flag_code == CODE_64BIT
d2224064 7126 && !i.tm.opcode_modifier.norex64
4ed21b58 7127 && !i.tm.opcode_modifier.vexw
46e883c5 7128 /* Special case for xchg %rax,%rax. It is NOP and doesn't
d2224064
JB
7129 need rex64. */
7130 && ! (i.operands == 2
7131 && i.tm.base_opcode == 0x90
7132 && i.tm.extension_opcode == None
75e5731b
JB
7133 && i.types[0].bitfield.instance == Accum
7134 && i.types[0].bitfield.qword
7135 && i.types[1].bitfield.instance == Accum
7136 && i.types[1].bitfield.qword))
d2224064 7137 i.rex |= REX_W;
3e73aa7c 7138
d2224064 7139 break;
8bbb3ad8
JB
7140
7141 case 0:
f9a6a8f0 7142 /* Select word/dword/qword operation with explicit data sizing prefix
8bbb3ad8
JB
7143 when there are no suitable register operands. */
7144 if (i.tm.opcode_modifier.w
7145 && (i.prefix[DATA_PREFIX] || (i.prefix[REX_PREFIX] & REX_W))
7146 && (!i.reg_operands
7147 || (i.reg_operands == 1
7148 /* ShiftCount */
7149 && (i.tm.operand_types[0].bitfield.instance == RegC
7150 /* InOutPortReg */
7151 || i.tm.operand_types[0].bitfield.instance == RegD
7152 || i.tm.operand_types[1].bitfield.instance == RegD
7153 /* CRC32 */
8b65b895 7154 || is_crc32))))
8bbb3ad8
JB
7155 i.tm.base_opcode |= 1;
7156 break;
29b0f896 7157 }
7ecd2f8b 7158
c8f8eebc 7159 if (i.tm.opcode_modifier.addrprefixopreg)
c0a30a9f 7160 {
c8f8eebc
JB
7161 gas_assert (!i.suffix);
7162 gas_assert (i.reg_operands);
c0a30a9f 7163
c8f8eebc
JB
7164 if (i.tm.operand_types[0].bitfield.instance == Accum
7165 || i.operands == 1)
7166 {
7167 /* The address size override prefix changes the size of the
7168 first operand. */
7169 if (flag_code == CODE_64BIT
7170 && i.op[0].regs->reg_type.bitfield.word)
7171 {
7172 as_bad (_("16-bit addressing unavailable for `%s'"),
7173 i.tm.name);
7174 return 0;
7175 }
7176
7177 if ((flag_code == CODE_32BIT
7178 ? i.op[0].regs->reg_type.bitfield.word
7179 : i.op[0].regs->reg_type.bitfield.dword)
7180 && !add_prefix (ADDR_PREFIX_OPCODE))
7181 return 0;
7182 }
c0a30a9f
L
7183 else
7184 {
c8f8eebc
JB
7185 /* Check invalid register operand when the address size override
7186 prefix changes the size of register operands. */
7187 unsigned int op;
7188 enum { need_word, need_dword, need_qword } need;
7189
27f13469 7190 /* Check the register operand for the address size prefix if
b3a3496f 7191 the memory operand has no real registers, like symbol, DISP
829f3fe1 7192 or bogus (x32-only) symbol(%rip) when symbol(%eip) is meant. */
27f13469
L
7193 if (i.mem_operands == 1
7194 && i.reg_operands == 1
7195 && i.operands == 2
27f13469 7196 && i.types[1].bitfield.class == Reg
b3a3496f
L
7197 && (flag_code == CODE_32BIT
7198 ? i.op[1].regs->reg_type.bitfield.word
7199 : i.op[1].regs->reg_type.bitfield.dword)
7200 && ((i.base_reg == NULL && i.index_reg == NULL)
829f3fe1
JB
7201#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
7202 || (x86_elf_abi == X86_64_X32_ABI
7203 && i.base_reg
b3a3496f
L
7204 && i.base_reg->reg_num == RegIP
7205 && i.base_reg->reg_type.bitfield.qword))
829f3fe1
JB
7206#else
7207 || 0)
7208#endif
27f13469
L
7209 && !add_prefix (ADDR_PREFIX_OPCODE))
7210 return 0;
7211
c8f8eebc
JB
7212 if (flag_code == CODE_32BIT)
7213 need = i.prefix[ADDR_PREFIX] ? need_word : need_dword;
7214 else if (i.prefix[ADDR_PREFIX])
c0a30a9f
L
7215 need = need_dword;
7216 else
7217 need = flag_code == CODE_64BIT ? need_qword : need_word;
c0a30a9f 7218
c8f8eebc
JB
7219 for (op = 0; op < i.operands; op++)
7220 {
7221 if (i.types[op].bitfield.class != Reg)
7222 continue;
7223
7224 switch (need)
7225 {
7226 case need_word:
7227 if (i.op[op].regs->reg_type.bitfield.word)
7228 continue;
7229 break;
7230 case need_dword:
7231 if (i.op[op].regs->reg_type.bitfield.dword)
7232 continue;
7233 break;
7234 case need_qword:
7235 if (i.op[op].regs->reg_type.bitfield.qword)
7236 continue;
7237 break;
7238 }
7239
7240 as_bad (_("invalid register operand size for `%s'"),
7241 i.tm.name);
7242 return 0;
7243 }
7244 }
c0a30a9f
L
7245 }
7246
29b0f896
AM
7247 return 1;
7248}
3e73aa7c 7249
29b0f896 7250static int
543613e9 7251check_byte_reg (void)
29b0f896
AM
7252{
7253 int op;
543613e9 7254
29b0f896
AM
7255 for (op = i.operands; --op >= 0;)
7256 {
dc821c5f 7257 /* Skip non-register operands. */
bab6aec1 7258 if (i.types[op].bitfield.class != Reg)
dc821c5f
JB
7259 continue;
7260
29b0f896
AM
7261 /* If this is an eight bit register, it's OK. If it's the 16 or
7262 32 bit version of an eight bit register, we will just use the
7263 low portion, and that's OK too. */
dc821c5f 7264 if (i.types[op].bitfield.byte)
29b0f896
AM
7265 continue;
7266
5a819eb9 7267 /* I/O port address operands are OK too. */
75e5731b
JB
7268 if (i.tm.operand_types[op].bitfield.instance == RegD
7269 && i.tm.operand_types[op].bitfield.word)
5a819eb9
JB
7270 continue;
7271
9706160a 7272 /* crc32 only wants its source operand checked here. */
389d00a5
JB
7273 if (i.tm.base_opcode == 0xf0
7274 && i.tm.opcode_modifier.opcodespace == SPACE_0F38
8b65b895
L
7275 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2
7276 && op != 0)
9344ff29
L
7277 continue;
7278
29b0f896 7279 /* Any other register is bad. */
73c76375
JB
7280 as_bad (_("`%s%s' not allowed with `%s%c'"),
7281 register_prefix, i.op[op].regs->reg_name,
7282 i.tm.name, i.suffix);
7283 return 0;
29b0f896
AM
7284 }
7285 return 1;
7286}
7287
7288static int
e3bb37b5 7289check_long_reg (void)
29b0f896
AM
7290{
7291 int op;
7292
7293 for (op = i.operands; --op >= 0;)
dc821c5f 7294 /* Skip non-register operands. */
bab6aec1 7295 if (i.types[op].bitfield.class != Reg)
dc821c5f 7296 continue;
29b0f896
AM
7297 /* Reject eight bit registers, except where the template requires
7298 them. (eg. movzb) */
dc821c5f 7299 else if (i.types[op].bitfield.byte
bab6aec1 7300 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7301 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7302 && (i.tm.operand_types[op].bitfield.word
7303 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7304 {
a540244d
L
7305 as_bad (_("`%s%s' not allowed with `%s%c'"),
7306 register_prefix,
29b0f896
AM
7307 i.op[op].regs->reg_name,
7308 i.tm.name,
7309 i.suffix);
7310 return 0;
7311 }
be4c5e58
L
7312 /* Error if the e prefix on a general reg is missing. */
7313 else if (i.types[op].bitfield.word
bab6aec1 7314 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7315 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7316 && i.tm.operand_types[op].bitfield.dword)
29b0f896 7317 {
be4c5e58
L
7318 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7319 register_prefix, i.op[op].regs->reg_name,
7320 i.suffix);
7321 return 0;
252b5132 7322 }
e4630f71 7323 /* Warn if the r prefix on a general reg is present. */
dc821c5f 7324 else if (i.types[op].bitfield.qword
bab6aec1 7325 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7326 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7327 && i.tm.operand_types[op].bitfield.dword)
252b5132 7328 {
34828aad 7329 if (intel_syntax
65fca059 7330 && i.tm.opcode_modifier.toqword
3528c362 7331 && i.types[0].bitfield.class != RegSIMD)
34828aad 7332 {
ca61edf2 7333 /* Convert to QWORD. We want REX byte. */
34828aad
L
7334 i.suffix = QWORD_MNEM_SUFFIX;
7335 }
7336 else
7337 {
2b5d6a91 7338 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
34828aad
L
7339 register_prefix, i.op[op].regs->reg_name,
7340 i.suffix);
7341 return 0;
7342 }
29b0f896
AM
7343 }
7344 return 1;
7345}
252b5132 7346
29b0f896 7347static int
e3bb37b5 7348check_qword_reg (void)
29b0f896
AM
7349{
7350 int op;
252b5132 7351
29b0f896 7352 for (op = i.operands; --op >= 0; )
dc821c5f 7353 /* Skip non-register operands. */
bab6aec1 7354 if (i.types[op].bitfield.class != Reg)
dc821c5f 7355 continue;
29b0f896
AM
7356 /* Reject eight bit registers, except where the template requires
7357 them. (eg. movzb) */
dc821c5f 7358 else if (i.types[op].bitfield.byte
bab6aec1 7359 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7360 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7361 && (i.tm.operand_types[op].bitfield.word
7362 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7363 {
a540244d
L
7364 as_bad (_("`%s%s' not allowed with `%s%c'"),
7365 register_prefix,
29b0f896
AM
7366 i.op[op].regs->reg_name,
7367 i.tm.name,
7368 i.suffix);
7369 return 0;
7370 }
e4630f71 7371 /* Warn if the r prefix on a general reg is missing. */
dc821c5f
JB
7372 else if ((i.types[op].bitfield.word
7373 || i.types[op].bitfield.dword)
bab6aec1 7374 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7375 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7376 && i.tm.operand_types[op].bitfield.qword)
29b0f896
AM
7377 {
7378 /* Prohibit these changes in the 64bit mode, since the
7379 lowering is more complicated. */
34828aad 7380 if (intel_syntax
ca61edf2 7381 && i.tm.opcode_modifier.todword
3528c362 7382 && i.types[0].bitfield.class != RegSIMD)
34828aad 7383 {
ca61edf2 7384 /* Convert to DWORD. We don't want REX byte. */
34828aad
L
7385 i.suffix = LONG_MNEM_SUFFIX;
7386 }
7387 else
7388 {
2b5d6a91 7389 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
34828aad
L
7390 register_prefix, i.op[op].regs->reg_name,
7391 i.suffix);
7392 return 0;
7393 }
252b5132 7394 }
29b0f896
AM
7395 return 1;
7396}
252b5132 7397
29b0f896 7398static int
e3bb37b5 7399check_word_reg (void)
29b0f896
AM
7400{
7401 int op;
7402 for (op = i.operands; --op >= 0;)
dc821c5f 7403 /* Skip non-register operands. */
bab6aec1 7404 if (i.types[op].bitfield.class != Reg)
dc821c5f 7405 continue;
29b0f896
AM
7406 /* Reject eight bit registers, except where the template requires
7407 them. (eg. movzb) */
dc821c5f 7408 else if (i.types[op].bitfield.byte
bab6aec1 7409 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7410 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7411 && (i.tm.operand_types[op].bitfield.word
7412 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7413 {
a540244d
L
7414 as_bad (_("`%s%s' not allowed with `%s%c'"),
7415 register_prefix,
29b0f896
AM
7416 i.op[op].regs->reg_name,
7417 i.tm.name,
7418 i.suffix);
7419 return 0;
7420 }
9706160a
JB
7421 /* Error if the e or r prefix on a general reg is present. */
7422 else if ((i.types[op].bitfield.dword
dc821c5f 7423 || i.types[op].bitfield.qword)
bab6aec1 7424 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7425 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7426 && i.tm.operand_types[op].bitfield.word)
252b5132 7427 {
9706160a
JB
7428 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7429 register_prefix, i.op[op].regs->reg_name,
7430 i.suffix);
7431 return 0;
29b0f896
AM
7432 }
7433 return 1;
7434}
252b5132 7435
29b0f896 7436static int
40fb9820 7437update_imm (unsigned int j)
29b0f896 7438{
bc0844ae 7439 i386_operand_type overlap = i.types[j];
40fb9820
L
7440 if ((overlap.bitfield.imm8
7441 || overlap.bitfield.imm8s
7442 || overlap.bitfield.imm16
7443 || overlap.bitfield.imm32
7444 || overlap.bitfield.imm32s
7445 || overlap.bitfield.imm64)
0dfbf9d7
L
7446 && !operand_type_equal (&overlap, &imm8)
7447 && !operand_type_equal (&overlap, &imm8s)
7448 && !operand_type_equal (&overlap, &imm16)
7449 && !operand_type_equal (&overlap, &imm32)
7450 && !operand_type_equal (&overlap, &imm32s)
7451 && !operand_type_equal (&overlap, &imm64))
29b0f896
AM
7452 {
7453 if (i.suffix)
7454 {
40fb9820
L
7455 i386_operand_type temp;
7456
0dfbf9d7 7457 operand_type_set (&temp, 0);
7ab9ffdd 7458 if (i.suffix == BYTE_MNEM_SUFFIX)
40fb9820
L
7459 {
7460 temp.bitfield.imm8 = overlap.bitfield.imm8;
7461 temp.bitfield.imm8s = overlap.bitfield.imm8s;
7462 }
7463 else if (i.suffix == WORD_MNEM_SUFFIX)
7464 temp.bitfield.imm16 = overlap.bitfield.imm16;
7465 else if (i.suffix == QWORD_MNEM_SUFFIX)
7466 {
7467 temp.bitfield.imm64 = overlap.bitfield.imm64;
7468 temp.bitfield.imm32s = overlap.bitfield.imm32s;
7469 }
7470 else
7471 temp.bitfield.imm32 = overlap.bitfield.imm32;
7472 overlap = temp;
29b0f896 7473 }
0dfbf9d7
L
7474 else if (operand_type_equal (&overlap, &imm16_32_32s)
7475 || operand_type_equal (&overlap, &imm16_32)
7476 || operand_type_equal (&overlap, &imm16_32s))
29b0f896 7477 {
40fb9820 7478 if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
65da13b5 7479 overlap = imm16;
40fb9820 7480 else
65da13b5 7481 overlap = imm32s;
29b0f896 7482 }
8bbb3ad8
JB
7483 else if (i.prefix[REX_PREFIX] & REX_W)
7484 overlap = operand_type_and (overlap, imm32s);
7485 else if (i.prefix[DATA_PREFIX])
7486 overlap = operand_type_and (overlap,
7487 flag_code != CODE_16BIT ? imm16 : imm32);
0dfbf9d7
L
7488 if (!operand_type_equal (&overlap, &imm8)
7489 && !operand_type_equal (&overlap, &imm8s)
7490 && !operand_type_equal (&overlap, &imm16)
7491 && !operand_type_equal (&overlap, &imm32)
7492 && !operand_type_equal (&overlap, &imm32s)
7493 && !operand_type_equal (&overlap, &imm64))
29b0f896 7494 {
4eed87de
AM
7495 as_bad (_("no instruction mnemonic suffix given; "
7496 "can't determine immediate size"));
29b0f896
AM
7497 return 0;
7498 }
7499 }
40fb9820 7500 i.types[j] = overlap;
29b0f896 7501
40fb9820
L
7502 return 1;
7503}
7504
7505static int
7506finalize_imm (void)
7507{
bc0844ae 7508 unsigned int j, n;
29b0f896 7509
bc0844ae
L
7510 /* Update the first 2 immediate operands. */
7511 n = i.operands > 2 ? 2 : i.operands;
7512 if (n)
7513 {
7514 for (j = 0; j < n; j++)
7515 if (update_imm (j) == 0)
7516 return 0;
40fb9820 7517
bc0844ae
L
7518 /* The 3rd operand can't be immediate operand. */
7519 gas_assert (operand_type_check (i.types[2], imm) == 0);
7520 }
29b0f896
AM
7521
7522 return 1;
7523}
7524
7525static int
e3bb37b5 7526process_operands (void)
29b0f896
AM
7527{
7528 /* Default segment register this instruction will use for memory
7529 accesses. 0 means unknown. This is only for optimizing out
7530 unnecessary segment overrides. */
7531 const seg_entry *default_seg = 0;
7532
a5aeccd9
JB
7533 if (i.tm.opcode_modifier.sse2avx)
7534 {
7535 /* Legacy encoded insns allow explicit REX prefixes, so these prefixes
7536 need converting. */
7537 i.rex |= i.prefix[REX_PREFIX] & (REX_W | REX_R | REX_X | REX_B);
7538 i.prefix[REX_PREFIX] = 0;
7539 i.rex_encoding = 0;
7540 }
c423d21a
JB
7541 /* ImmExt should be processed after SSE2AVX. */
7542 else if (i.tm.opcode_modifier.immext)
7543 process_immext ();
a5aeccd9 7544
2426c15f 7545 if (i.tm.opcode_modifier.sse2avx && i.tm.opcode_modifier.vexvvvv)
29b0f896 7546 {
91d6fa6a
NC
7547 unsigned int dupl = i.operands;
7548 unsigned int dest = dupl - 1;
9fcfb3d7
L
7549 unsigned int j;
7550
c0f3af97 7551 /* The destination must be an xmm register. */
9c2799c2 7552 gas_assert (i.reg_operands
91d6fa6a 7553 && MAX_OPERANDS > dupl
7ab9ffdd 7554 && operand_type_equal (&i.types[dest], &regxmm));
c0f3af97 7555
75e5731b 7556 if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 7557 && i.tm.operand_types[0].bitfield.xmmword)
e2ec9d29 7558 {
8cd7925b 7559 if (i.tm.opcode_modifier.vexsources == VEX3SOURCES)
c0f3af97
L
7560 {
7561 /* Keep xmm0 for instructions with VEX prefix and 3
7562 sources. */
75e5731b 7563 i.tm.operand_types[0].bitfield.instance = InstanceNone;
3528c362 7564 i.tm.operand_types[0].bitfield.class = RegSIMD;
c0f3af97
L
7565 goto duplicate;
7566 }
e2ec9d29 7567 else
c0f3af97
L
7568 {
7569 /* We remove the first xmm0 and keep the number of
7570 operands unchanged, which in fact duplicates the
7571 destination. */
7572 for (j = 1; j < i.operands; j++)
7573 {
7574 i.op[j - 1] = i.op[j];
7575 i.types[j - 1] = i.types[j];
7576 i.tm.operand_types[j - 1] = i.tm.operand_types[j];
8dc0818e 7577 i.flags[j - 1] = i.flags[j];
c0f3af97
L
7578 }
7579 }
7580 }
7581 else if (i.tm.opcode_modifier.implicit1stxmm0)
7ab9ffdd 7582 {
91d6fa6a 7583 gas_assert ((MAX_OPERANDS - 1) > dupl
8cd7925b
L
7584 && (i.tm.opcode_modifier.vexsources
7585 == VEX3SOURCES));
c0f3af97
L
7586
7587 /* Add the implicit xmm0 for instructions with VEX prefix
7588 and 3 sources. */
7589 for (j = i.operands; j > 0; j--)
7590 {
7591 i.op[j] = i.op[j - 1];
7592 i.types[j] = i.types[j - 1];
7593 i.tm.operand_types[j] = i.tm.operand_types[j - 1];
8dc0818e 7594 i.flags[j] = i.flags[j - 1];
c0f3af97
L
7595 }
7596 i.op[0].regs
629310ab 7597 = (const reg_entry *) str_hash_find (reg_hash, "xmm0");
7ab9ffdd 7598 i.types[0] = regxmm;
c0f3af97
L
7599 i.tm.operand_types[0] = regxmm;
7600
7601 i.operands += 2;
7602 i.reg_operands += 2;
7603 i.tm.operands += 2;
7604
91d6fa6a 7605 dupl++;
c0f3af97 7606 dest++;
91d6fa6a
NC
7607 i.op[dupl] = i.op[dest];
7608 i.types[dupl] = i.types[dest];
7609 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 7610 i.flags[dupl] = i.flags[dest];
e2ec9d29 7611 }
c0f3af97
L
7612 else
7613 {
dc1e8a47 7614 duplicate:
c0f3af97
L
7615 i.operands++;
7616 i.reg_operands++;
7617 i.tm.operands++;
7618
91d6fa6a
NC
7619 i.op[dupl] = i.op[dest];
7620 i.types[dupl] = i.types[dest];
7621 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 7622 i.flags[dupl] = i.flags[dest];
c0f3af97
L
7623 }
7624
7625 if (i.tm.opcode_modifier.immext)
7626 process_immext ();
7627 }
75e5731b 7628 else if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 7629 && i.tm.operand_types[0].bitfield.xmmword)
c0f3af97
L
7630 {
7631 unsigned int j;
7632
9fcfb3d7
L
7633 for (j = 1; j < i.operands; j++)
7634 {
7635 i.op[j - 1] = i.op[j];
7636 i.types[j - 1] = i.types[j];
7637
7638 /* We need to adjust fields in i.tm since they are used by
7639 build_modrm_byte. */
7640 i.tm.operand_types [j - 1] = i.tm.operand_types [j];
8dc0818e
JB
7641
7642 i.flags[j - 1] = i.flags[j];
9fcfb3d7
L
7643 }
7644
e2ec9d29
L
7645 i.operands--;
7646 i.reg_operands--;
e2ec9d29
L
7647 i.tm.operands--;
7648 }
920d2ddc
IT
7649 else if (i.tm.opcode_modifier.implicitquadgroup)
7650 {
a477a8c4
JB
7651 unsigned int regnum, first_reg_in_group, last_reg_in_group;
7652
920d2ddc 7653 /* The second operand must be {x,y,z}mmN, where N is a multiple of 4. */
3528c362 7654 gas_assert (i.operands >= 2 && i.types[1].bitfield.class == RegSIMD);
a477a8c4
JB
7655 regnum = register_number (i.op[1].regs);
7656 first_reg_in_group = regnum & ~3;
7657 last_reg_in_group = first_reg_in_group + 3;
7658 if (regnum != first_reg_in_group)
7659 as_warn (_("source register `%s%s' implicitly denotes"
7660 " `%s%.3s%u' to `%s%.3s%u' source group in `%s'"),
7661 register_prefix, i.op[1].regs->reg_name,
7662 register_prefix, i.op[1].regs->reg_name, first_reg_in_group,
7663 register_prefix, i.op[1].regs->reg_name, last_reg_in_group,
7664 i.tm.name);
7665 }
e2ec9d29
L
7666 else if (i.tm.opcode_modifier.regkludge)
7667 {
7668 /* The imul $imm, %reg instruction is converted into
7669 imul $imm, %reg, %reg, and the clr %reg instruction
7670 is converted into xor %reg, %reg. */
7671
7672 unsigned int first_reg_op;
7673
7674 if (operand_type_check (i.types[0], reg))
7675 first_reg_op = 0;
7676 else
7677 first_reg_op = 1;
7678 /* Pretend we saw the extra register operand. */
9c2799c2 7679 gas_assert (i.reg_operands == 1
7ab9ffdd 7680 && i.op[first_reg_op + 1].regs == 0);
e2ec9d29
L
7681 i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
7682 i.types[first_reg_op + 1] = i.types[first_reg_op];
7683 i.operands++;
7684 i.reg_operands++;
29b0f896
AM
7685 }
7686
85b80b0f 7687 if (i.tm.opcode_modifier.modrm)
29b0f896
AM
7688 {
7689 /* The opcode is completed (modulo i.tm.extension_opcode which
52271982
AM
7690 must be put into the modrm byte). Now, we make the modrm and
7691 index base bytes based on all the info we've collected. */
29b0f896
AM
7692
7693 default_seg = build_modrm_byte ();
7694 }
00cee14f 7695 else if (i.types[0].bitfield.class == SReg)
85b80b0f
JB
7696 {
7697 if (flag_code != CODE_64BIT
7698 ? i.tm.base_opcode == POP_SEG_SHORT
7699 && i.op[0].regs->reg_num == 1
389d00a5 7700 : (i.tm.base_opcode | 1) == (POP_SEG386_SHORT & 0xff)
85b80b0f
JB
7701 && i.op[0].regs->reg_num < 4)
7702 {
7703 as_bad (_("you can't `%s %s%s'"),
7704 i.tm.name, register_prefix, i.op[0].regs->reg_name);
7705 return 0;
7706 }
389d00a5
JB
7707 if (i.op[0].regs->reg_num > 3
7708 && i.tm.opcode_modifier.opcodespace == SPACE_BASE )
85b80b0f 7709 {
389d00a5
JB
7710 i.tm.base_opcode ^= (POP_SEG_SHORT ^ POP_SEG386_SHORT) & 0xff;
7711 i.tm.opcode_modifier.opcodespace = SPACE_0F;
85b80b0f
JB
7712 }
7713 i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
7714 }
389d00a5
JB
7715 else if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
7716 && (i.tm.base_opcode & ~3) == MOV_AX_DISP32)
29b0f896
AM
7717 {
7718 default_seg = &ds;
7719 }
40fb9820 7720 else if (i.tm.opcode_modifier.isstring)
29b0f896
AM
7721 {
7722 /* For the string instructions that allow a segment override
7723 on one of their operands, the default segment is ds. */
7724 default_seg = &ds;
7725 }
50128d0c 7726 else if (i.short_form)
85b80b0f
JB
7727 {
7728 /* The register or float register operand is in operand
7729 0 or 1. */
bab6aec1 7730 unsigned int op = i.tm.operand_types[0].bitfield.class != Reg;
85b80b0f
JB
7731
7732 /* Register goes in low 3 bits of opcode. */
7733 i.tm.base_opcode |= i.op[op].regs->reg_num;
7734 if ((i.op[op].regs->reg_flags & RegRex) != 0)
7735 i.rex |= REX_B;
7736 if (!quiet_warnings && i.tm.opcode_modifier.ugh)
7737 {
7738 /* Warn about some common errors, but press on regardless.
7739 The first case can be generated by gcc (<= 2.8.1). */
7740 if (i.operands == 2)
7741 {
7742 /* Reversed arguments on faddp, fsubp, etc. */
7743 as_warn (_("translating to `%s %s%s,%s%s'"), i.tm.name,
7744 register_prefix, i.op[!intel_syntax].regs->reg_name,
7745 register_prefix, i.op[intel_syntax].regs->reg_name);
7746 }
7747 else
7748 {
7749 /* Extraneous `l' suffix on fp insn. */
7750 as_warn (_("translating to `%s %s%s'"), i.tm.name,
7751 register_prefix, i.op[0].regs->reg_name);
7752 }
7753 }
7754 }
29b0f896 7755
514a8bb0 7756 if ((i.seg[0] || i.prefix[SEG_PREFIX])
514a8bb0 7757 && i.tm.base_opcode == 0x8d /* lea */
35648716 7758 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
514a8bb0 7759 && !is_any_vex_encoding(&i.tm))
92334ad2
JB
7760 {
7761 if (!quiet_warnings)
7762 as_warn (_("segment override on `%s' is ineffectual"), i.tm.name);
7763 if (optimize)
7764 {
7765 i.seg[0] = NULL;
7766 i.prefix[SEG_PREFIX] = 0;
7767 }
7768 }
52271982
AM
7769
7770 /* If a segment was explicitly specified, and the specified segment
b6773884
JB
7771 is neither the default nor the one already recorded from a prefix,
7772 use an opcode prefix to select it. If we never figured out what
7773 the default segment is, then default_seg will be zero at this
7774 point, and the specified segment prefix will always be used. */
7775 if (i.seg[0]
7776 && i.seg[0] != default_seg
7777 && i.seg[0]->seg_prefix != i.prefix[SEG_PREFIX])
29b0f896
AM
7778 {
7779 if (!add_prefix (i.seg[0]->seg_prefix))
7780 return 0;
7781 }
7782 return 1;
7783}
7784
a5aeccd9
JB
7785static INLINE void set_rex_vrex (const reg_entry *r, unsigned int rex_bit,
7786 bfd_boolean do_sse2avx)
7787{
7788 if (r->reg_flags & RegRex)
7789 {
7790 if (i.rex & rex_bit)
7791 as_bad (_("same type of prefix used twice"));
7792 i.rex |= rex_bit;
7793 }
7794 else if (do_sse2avx && (i.rex & rex_bit) && i.vex.register_specifier)
7795 {
7796 gas_assert (i.vex.register_specifier == r);
7797 i.vex.register_specifier += 8;
7798 }
7799
7800 if (r->reg_flags & RegVRex)
7801 i.vrex |= rex_bit;
7802}
7803
29b0f896 7804static const seg_entry *
e3bb37b5 7805build_modrm_byte (void)
29b0f896
AM
7806{
7807 const seg_entry *default_seg = 0;
c0f3af97 7808 unsigned int source, dest;
8cd7925b 7809 int vex_3_sources;
c0f3af97 7810
8cd7925b 7811 vex_3_sources = i.tm.opcode_modifier.vexsources == VEX3SOURCES;
c0f3af97
L
7812 if (vex_3_sources)
7813 {
91d6fa6a 7814 unsigned int nds, reg_slot;
4c2c6516 7815 expressionS *exp;
c0f3af97 7816
6b8d3588 7817 dest = i.operands - 1;
c0f3af97 7818 nds = dest - 1;
922d8de8 7819
a683cc34 7820 /* There are 2 kinds of instructions:
bed3d976 7821 1. 5 operands: 4 register operands or 3 register operands
9d3bf266 7822 plus 1 memory operand plus one Imm4 operand, VexXDS, and
bed3d976 7823 VexW0 or VexW1. The destination must be either XMM, YMM or
43234a1e 7824 ZMM register.
bed3d976 7825 2. 4 operands: 4 register operands or 3 register operands
2f1bada2 7826 plus 1 memory operand, with VexXDS. */
922d8de8 7827 gas_assert ((i.reg_operands == 4
bed3d976
JB
7828 || (i.reg_operands == 3 && i.mem_operands == 1))
7829 && i.tm.opcode_modifier.vexvvvv == VEXXDS
dcd7e323 7830 && i.tm.opcode_modifier.vexw
3528c362 7831 && i.tm.operand_types[dest].bitfield.class == RegSIMD);
a683cc34 7832
48db9223
JB
7833 /* If VexW1 is set, the first non-immediate operand is the source and
7834 the second non-immediate one is encoded in the immediate operand. */
7835 if (i.tm.opcode_modifier.vexw == VEXW1)
7836 {
7837 source = i.imm_operands;
7838 reg_slot = i.imm_operands + 1;
7839 }
7840 else
7841 {
7842 source = i.imm_operands + 1;
7843 reg_slot = i.imm_operands;
7844 }
7845
a683cc34 7846 if (i.imm_operands == 0)
bed3d976
JB
7847 {
7848 /* When there is no immediate operand, generate an 8bit
7849 immediate operand to encode the first operand. */
7850 exp = &im_expressions[i.imm_operands++];
7851 i.op[i.operands].imms = exp;
7852 i.types[i.operands] = imm8;
7853 i.operands++;
7854
3528c362 7855 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
bed3d976
JB
7856 exp->X_op = O_constant;
7857 exp->X_add_number = register_number (i.op[reg_slot].regs) << 4;
43234a1e
L
7858 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
7859 }
922d8de8 7860 else
bed3d976 7861 {
9d3bf266
JB
7862 gas_assert (i.imm_operands == 1);
7863 gas_assert (fits_in_imm4 (i.op[0].imms->X_add_number));
7864 gas_assert (!i.tm.opcode_modifier.immext);
a683cc34 7865
9d3bf266
JB
7866 /* Turn on Imm8 again so that output_imm will generate it. */
7867 i.types[0].bitfield.imm8 = 1;
bed3d976 7868
3528c362 7869 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
9d3bf266 7870 i.op[0].imms->X_add_number
bed3d976 7871 |= register_number (i.op[reg_slot].regs) << 4;
43234a1e 7872 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
bed3d976 7873 }
a683cc34 7874
3528c362 7875 gas_assert (i.tm.operand_types[nds].bitfield.class == RegSIMD);
dae39acc 7876 i.vex.register_specifier = i.op[nds].regs;
c0f3af97
L
7877 }
7878 else
7879 source = dest = 0;
29b0f896
AM
7880
7881 /* i.reg_operands MUST be the number of real register operands;
c0f3af97
L
7882 implicit registers do not count. If there are 3 register
7883 operands, it must be a instruction with VexNDS. For a
7884 instruction with VexNDD, the destination register is encoded
7885 in VEX prefix. If there are 4 register operands, it must be
7886 a instruction with VEX prefix and 3 sources. */
7ab9ffdd
L
7887 if (i.mem_operands == 0
7888 && ((i.reg_operands == 2
2426c15f 7889 && i.tm.opcode_modifier.vexvvvv <= VEXXDS)
7ab9ffdd 7890 || (i.reg_operands == 3
2426c15f 7891 && i.tm.opcode_modifier.vexvvvv == VEXXDS)
7ab9ffdd 7892 || (i.reg_operands == 4 && vex_3_sources)))
29b0f896 7893 {
cab737b9
L
7894 switch (i.operands)
7895 {
7896 case 2:
7897 source = 0;
7898 break;
7899 case 3:
c81128dc
L
7900 /* When there are 3 operands, one of them may be immediate,
7901 which may be the first or the last operand. Otherwise,
c0f3af97
L
7902 the first operand must be shift count register (cl) or it
7903 is an instruction with VexNDS. */
9c2799c2 7904 gas_assert (i.imm_operands == 1
7ab9ffdd 7905 || (i.imm_operands == 0
2426c15f 7906 && (i.tm.opcode_modifier.vexvvvv == VEXXDS
75e5731b
JB
7907 || (i.types[0].bitfield.instance == RegC
7908 && i.types[0].bitfield.byte))));
40fb9820 7909 if (operand_type_check (i.types[0], imm)
75e5731b
JB
7910 || (i.types[0].bitfield.instance == RegC
7911 && i.types[0].bitfield.byte))
40fb9820
L
7912 source = 1;
7913 else
7914 source = 0;
cab737b9
L
7915 break;
7916 case 4:
368d64cc
L
7917 /* When there are 4 operands, the first two must be 8bit
7918 immediate operands. The source operand will be the 3rd
c0f3af97
L
7919 one.
7920
7921 For instructions with VexNDS, if the first operand
7922 an imm8, the source operand is the 2nd one. If the last
7923 operand is imm8, the source operand is the first one. */
9c2799c2 7924 gas_assert ((i.imm_operands == 2
7ab9ffdd
L
7925 && i.types[0].bitfield.imm8
7926 && i.types[1].bitfield.imm8)
2426c15f 7927 || (i.tm.opcode_modifier.vexvvvv == VEXXDS
7ab9ffdd
L
7928 && i.imm_operands == 1
7929 && (i.types[0].bitfield.imm8
43234a1e
L
7930 || i.types[i.operands - 1].bitfield.imm8
7931 || i.rounding)));
9f2670f2
L
7932 if (i.imm_operands == 2)
7933 source = 2;
7934 else
c0f3af97
L
7935 {
7936 if (i.types[0].bitfield.imm8)
7937 source = 1;
7938 else
7939 source = 0;
7940 }
c0f3af97
L
7941 break;
7942 case 5:
e771e7c9 7943 if (is_evex_encoding (&i.tm))
43234a1e
L
7944 {
7945 /* For EVEX instructions, when there are 5 operands, the
7946 first one must be immediate operand. If the second one
7947 is immediate operand, the source operand is the 3th
7948 one. If the last one is immediate operand, the source
7949 operand is the 2nd one. */
7950 gas_assert (i.imm_operands == 2
7951 && i.tm.opcode_modifier.sae
7952 && operand_type_check (i.types[0], imm));
7953 if (operand_type_check (i.types[1], imm))
7954 source = 2;
7955 else if (operand_type_check (i.types[4], imm))
7956 source = 1;
7957 else
7958 abort ();
7959 }
cab737b9
L
7960 break;
7961 default:
7962 abort ();
7963 }
7964
c0f3af97
L
7965 if (!vex_3_sources)
7966 {
7967 dest = source + 1;
7968
43234a1e
L
7969 /* RC/SAE operand could be between DEST and SRC. That happens
7970 when one operand is GPR and the other one is XMM/YMM/ZMM
7971 register. */
783c187b 7972 if (i.rounding && i.rounding->operand == dest)
43234a1e
L
7973 dest++;
7974
2426c15f 7975 if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
c0f3af97 7976 {
43234a1e 7977 /* For instructions with VexNDS, the register-only source
c5d0745b 7978 operand must be a 32/64bit integer, XMM, YMM, ZMM, or mask
dfd69174 7979 register. It is encoded in VEX prefix. */
f12dc422
L
7980
7981 i386_operand_type op;
7982 unsigned int vvvv;
7983
c2ecccb3
L
7984 /* Swap two source operands if needed. */
7985 if (i.tm.opcode_modifier.swapsources)
f12dc422
L
7986 {
7987 vvvv = source;
7988 source = dest;
7989 }
7990 else
7991 vvvv = dest;
7992
7993 op = i.tm.operand_types[vvvv];
c0f3af97 7994 if ((dest + 1) >= i.operands
bab6aec1 7995 || ((op.bitfield.class != Reg
dc821c5f 7996 || (!op.bitfield.dword && !op.bitfield.qword))
3528c362 7997 && op.bitfield.class != RegSIMD
43234a1e 7998 && !operand_type_equal (&op, &regmask)))
c0f3af97 7999 abort ();
f12dc422 8000 i.vex.register_specifier = i.op[vvvv].regs;
c0f3af97
L
8001 dest++;
8002 }
8003 }
29b0f896
AM
8004
8005 i.rm.mode = 3;
dfd69174
JB
8006 /* One of the register operands will be encoded in the i.rm.reg
8007 field, the other in the combined i.rm.mode and i.rm.regmem
29b0f896
AM
8008 fields. If no form of this instruction supports a memory
8009 destination operand, then we assume the source operand may
8010 sometimes be a memory operand and so we need to store the
8011 destination in the i.rm.reg field. */
dfd69174 8012 if (!i.tm.opcode_modifier.regmem
40fb9820 8013 && operand_type_check (i.tm.operand_types[dest], anymem) == 0)
29b0f896
AM
8014 {
8015 i.rm.reg = i.op[dest].regs->reg_num;
8016 i.rm.regmem = i.op[source].regs->reg_num;
a5aeccd9
JB
8017 set_rex_vrex (i.op[dest].regs, REX_R, i.tm.opcode_modifier.sse2avx);
8018 set_rex_vrex (i.op[source].regs, REX_B, FALSE);
29b0f896
AM
8019 }
8020 else
8021 {
8022 i.rm.reg = i.op[source].regs->reg_num;
8023 i.rm.regmem = i.op[dest].regs->reg_num;
a5aeccd9
JB
8024 set_rex_vrex (i.op[dest].regs, REX_B, i.tm.opcode_modifier.sse2avx);
8025 set_rex_vrex (i.op[source].regs, REX_R, FALSE);
29b0f896 8026 }
e0c7f900 8027 if (flag_code != CODE_64BIT && (i.rex & REX_R))
c4a530c5 8028 {
4a5c67ed 8029 if (i.types[!i.tm.opcode_modifier.regmem].bitfield.class != RegCR)
c4a530c5 8030 abort ();
e0c7f900 8031 i.rex &= ~REX_R;
c4a530c5
JB
8032 add_prefix (LOCK_PREFIX_OPCODE);
8033 }
29b0f896
AM
8034 }
8035 else
8036 { /* If it's not 2 reg operands... */
c0f3af97
L
8037 unsigned int mem;
8038
29b0f896
AM
8039 if (i.mem_operands)
8040 {
8041 unsigned int fake_zero_displacement = 0;
99018f42 8042 unsigned int op;
4eed87de 8043
7ab9ffdd 8044 for (op = 0; op < i.operands; op++)
8dc0818e 8045 if (i.flags[op] & Operand_Mem)
7ab9ffdd 8046 break;
7ab9ffdd 8047 gas_assert (op < i.operands);
29b0f896 8048
63112cd6 8049 if (i.tm.opcode_modifier.sib)
6c30d220 8050 {
260cd341
LC
8051 /* The index register of VSIB shouldn't be RegIZ. */
8052 if (i.tm.opcode_modifier.sib != SIBMEM
8053 && i.index_reg->reg_num == RegIZ)
6c30d220
L
8054 abort ();
8055
8056 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8057 if (!i.base_reg)
8058 {
8059 i.sib.base = NO_BASE_REGISTER;
8060 i.sib.scale = i.log2_scale_factor;
8061 i.types[op].bitfield.disp8 = 0;
8062 i.types[op].bitfield.disp16 = 0;
8063 i.types[op].bitfield.disp64 = 0;
43083a50 8064 if (flag_code != CODE_64BIT || i.prefix[ADDR_PREFIX])
6c30d220
L
8065 {
8066 /* Must be 32 bit */
8067 i.types[op].bitfield.disp32 = 1;
8068 i.types[op].bitfield.disp32s = 0;
8069 }
8070 else
8071 {
8072 i.types[op].bitfield.disp32 = 0;
8073 i.types[op].bitfield.disp32s = 1;
8074 }
8075 }
260cd341
LC
8076
8077 /* Since the mandatory SIB always has index register, so
8078 the code logic remains unchanged. The non-mandatory SIB
8079 without index register is allowed and will be handled
8080 later. */
8081 if (i.index_reg)
8082 {
8083 if (i.index_reg->reg_num == RegIZ)
8084 i.sib.index = NO_INDEX_REGISTER;
8085 else
8086 i.sib.index = i.index_reg->reg_num;
8087 set_rex_vrex (i.index_reg, REX_X, FALSE);
8088 }
6c30d220
L
8089 }
8090
29b0f896
AM
8091 default_seg = &ds;
8092
8093 if (i.base_reg == 0)
8094 {
8095 i.rm.mode = 0;
8096 if (!i.disp_operands)
9bb129e8 8097 fake_zero_displacement = 1;
29b0f896
AM
8098 if (i.index_reg == 0)
8099 {
73053c1f
JB
8100 i386_operand_type newdisp;
8101
260cd341
LC
8102 /* Both check for VSIB and mandatory non-vector SIB. */
8103 gas_assert (!i.tm.opcode_modifier.sib
8104 || i.tm.opcode_modifier.sib == SIBMEM);
29b0f896 8105 /* Operand is just <disp> */
20f0a1fc 8106 if (flag_code == CODE_64BIT)
29b0f896
AM
8107 {
8108 /* 64bit mode overwrites the 32bit absolute
8109 addressing by RIP relative addressing and
8110 absolute addressing is encoded by one of the
8111 redundant SIB forms. */
8112 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8113 i.sib.base = NO_BASE_REGISTER;
8114 i.sib.index = NO_INDEX_REGISTER;
73053c1f 8115 newdisp = (!i.prefix[ADDR_PREFIX] ? disp32s : disp32);
20f0a1fc 8116 }
fc225355
L
8117 else if ((flag_code == CODE_16BIT)
8118 ^ (i.prefix[ADDR_PREFIX] != 0))
20f0a1fc
NC
8119 {
8120 i.rm.regmem = NO_BASE_REGISTER_16;
73053c1f 8121 newdisp = disp16;
20f0a1fc
NC
8122 }
8123 else
8124 {
8125 i.rm.regmem = NO_BASE_REGISTER;
73053c1f 8126 newdisp = disp32;
29b0f896 8127 }
73053c1f
JB
8128 i.types[op] = operand_type_and_not (i.types[op], anydisp);
8129 i.types[op] = operand_type_or (i.types[op], newdisp);
29b0f896 8130 }
63112cd6 8131 else if (!i.tm.opcode_modifier.sib)
29b0f896 8132 {
6c30d220 8133 /* !i.base_reg && i.index_reg */
e968fc9b 8134 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8135 i.sib.index = NO_INDEX_REGISTER;
8136 else
8137 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8138 i.sib.base = NO_BASE_REGISTER;
8139 i.sib.scale = i.log2_scale_factor;
8140 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
40fb9820
L
8141 i.types[op].bitfield.disp8 = 0;
8142 i.types[op].bitfield.disp16 = 0;
8143 i.types[op].bitfield.disp64 = 0;
43083a50 8144 if (flag_code != CODE_64BIT || i.prefix[ADDR_PREFIX])
40fb9820
L
8145 {
8146 /* Must be 32 bit */
8147 i.types[op].bitfield.disp32 = 1;
8148 i.types[op].bitfield.disp32s = 0;
8149 }
29b0f896 8150 else
40fb9820
L
8151 {
8152 i.types[op].bitfield.disp32 = 0;
8153 i.types[op].bitfield.disp32s = 1;
8154 }
29b0f896 8155 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8156 i.rex |= REX_X;
29b0f896
AM
8157 }
8158 }
8159 /* RIP addressing for 64bit mode. */
e968fc9b 8160 else if (i.base_reg->reg_num == RegIP)
29b0f896 8161 {
63112cd6 8162 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896 8163 i.rm.regmem = NO_BASE_REGISTER;
40fb9820
L
8164 i.types[op].bitfield.disp8 = 0;
8165 i.types[op].bitfield.disp16 = 0;
8166 i.types[op].bitfield.disp32 = 0;
8167 i.types[op].bitfield.disp32s = 1;
8168 i.types[op].bitfield.disp64 = 0;
71903a11 8169 i.flags[op] |= Operand_PCrel;
20f0a1fc
NC
8170 if (! i.disp_operands)
8171 fake_zero_displacement = 1;
29b0f896 8172 }
dc821c5f 8173 else if (i.base_reg->reg_type.bitfield.word)
29b0f896 8174 {
63112cd6 8175 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896
AM
8176 switch (i.base_reg->reg_num)
8177 {
8178 case 3: /* (%bx) */
8179 if (i.index_reg == 0)
8180 i.rm.regmem = 7;
8181 else /* (%bx,%si) -> 0, or (%bx,%di) -> 1 */
8182 i.rm.regmem = i.index_reg->reg_num - 6;
8183 break;
8184 case 5: /* (%bp) */
8185 default_seg = &ss;
8186 if (i.index_reg == 0)
8187 {
8188 i.rm.regmem = 6;
40fb9820 8189 if (operand_type_check (i.types[op], disp) == 0)
29b0f896
AM
8190 {
8191 /* fake (%bp) into 0(%bp) */
41eb8e88 8192 if (i.disp_encoding == disp_encoding_16bit)
1a02d6b0
L
8193 i.types[op].bitfield.disp16 = 1;
8194 else
8195 i.types[op].bitfield.disp8 = 1;
252b5132 8196 fake_zero_displacement = 1;
29b0f896
AM
8197 }
8198 }
8199 else /* (%bp,%si) -> 2, or (%bp,%di) -> 3 */
8200 i.rm.regmem = i.index_reg->reg_num - 6 + 2;
8201 break;
8202 default: /* (%si) -> 4 or (%di) -> 5 */
8203 i.rm.regmem = i.base_reg->reg_num - 6 + 4;
8204 }
41eb8e88
L
8205 if (!fake_zero_displacement
8206 && !i.disp_operands
8207 && i.disp_encoding)
8208 {
8209 fake_zero_displacement = 1;
8210 if (i.disp_encoding == disp_encoding_8bit)
8211 i.types[op].bitfield.disp8 = 1;
8212 else
8213 i.types[op].bitfield.disp16 = 1;
8214 }
29b0f896
AM
8215 i.rm.mode = mode_from_disp_size (i.types[op]);
8216 }
8217 else /* i.base_reg and 32/64 bit mode */
8218 {
8219 if (flag_code == CODE_64BIT
40fb9820
L
8220 && operand_type_check (i.types[op], disp))
8221 {
73053c1f
JB
8222 i.types[op].bitfield.disp16 = 0;
8223 i.types[op].bitfield.disp64 = 0;
40fb9820 8224 if (i.prefix[ADDR_PREFIX] == 0)
73053c1f
JB
8225 {
8226 i.types[op].bitfield.disp32 = 0;
8227 i.types[op].bitfield.disp32s = 1;
8228 }
40fb9820 8229 else
73053c1f
JB
8230 {
8231 i.types[op].bitfield.disp32 = 1;
8232 i.types[op].bitfield.disp32s = 0;
8233 }
40fb9820 8234 }
20f0a1fc 8235
63112cd6 8236 if (!i.tm.opcode_modifier.sib)
6c30d220 8237 i.rm.regmem = i.base_reg->reg_num;
29b0f896 8238 if ((i.base_reg->reg_flags & RegRex) != 0)
161a04f6 8239 i.rex |= REX_B;
29b0f896
AM
8240 i.sib.base = i.base_reg->reg_num;
8241 /* x86-64 ignores REX prefix bit here to avoid decoder
8242 complications. */
848930b2
JB
8243 if (!(i.base_reg->reg_flags & RegRex)
8244 && (i.base_reg->reg_num == EBP_REG_NUM
8245 || i.base_reg->reg_num == ESP_REG_NUM))
29b0f896 8246 default_seg = &ss;
848930b2 8247 if (i.base_reg->reg_num == 5 && i.disp_operands == 0)
29b0f896 8248 {
848930b2 8249 fake_zero_displacement = 1;
1a02d6b0
L
8250 if (i.disp_encoding == disp_encoding_32bit)
8251 i.types[op].bitfield.disp32 = 1;
8252 else
8253 i.types[op].bitfield.disp8 = 1;
29b0f896
AM
8254 }
8255 i.sib.scale = i.log2_scale_factor;
8256 if (i.index_reg == 0)
8257 {
260cd341
LC
8258 /* Only check for VSIB. */
8259 gas_assert (i.tm.opcode_modifier.sib != VECSIB128
8260 && i.tm.opcode_modifier.sib != VECSIB256
8261 && i.tm.opcode_modifier.sib != VECSIB512);
8262
29b0f896
AM
8263 /* <disp>(%esp) becomes two byte modrm with no index
8264 register. We've already stored the code for esp
8265 in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
8266 Any base register besides %esp will not use the
8267 extra modrm byte. */
8268 i.sib.index = NO_INDEX_REGISTER;
29b0f896 8269 }
63112cd6 8270 else if (!i.tm.opcode_modifier.sib)
29b0f896 8271 {
e968fc9b 8272 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8273 i.sib.index = NO_INDEX_REGISTER;
8274 else
8275 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8276 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8277 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8278 i.rex |= REX_X;
29b0f896 8279 }
67a4f2b7
AO
8280
8281 if (i.disp_operands
8282 && (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
8283 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL))
8284 i.rm.mode = 0;
8285 else
a501d77e
L
8286 {
8287 if (!fake_zero_displacement
8288 && !i.disp_operands
8289 && i.disp_encoding)
8290 {
8291 fake_zero_displacement = 1;
8292 if (i.disp_encoding == disp_encoding_8bit)
8293 i.types[op].bitfield.disp8 = 1;
8294 else
8295 i.types[op].bitfield.disp32 = 1;
8296 }
8297 i.rm.mode = mode_from_disp_size (i.types[op]);
8298 }
29b0f896 8299 }
252b5132 8300
29b0f896
AM
8301 if (fake_zero_displacement)
8302 {
8303 /* Fakes a zero displacement assuming that i.types[op]
8304 holds the correct displacement size. */
8305 expressionS *exp;
8306
9c2799c2 8307 gas_assert (i.op[op].disps == 0);
29b0f896
AM
8308 exp = &disp_expressions[i.disp_operands++];
8309 i.op[op].disps = exp;
8310 exp->X_op = O_constant;
8311 exp->X_add_number = 0;
8312 exp->X_add_symbol = (symbolS *) 0;
8313 exp->X_op_symbol = (symbolS *) 0;
8314 }
c0f3af97
L
8315
8316 mem = op;
29b0f896 8317 }
c0f3af97
L
8318 else
8319 mem = ~0;
252b5132 8320
8c43a48b 8321 if (i.tm.opcode_modifier.vexsources == XOP2SOURCES)
5dd85c99
SP
8322 {
8323 if (operand_type_check (i.types[0], imm))
8324 i.vex.register_specifier = NULL;
8325 else
8326 {
8327 /* VEX.vvvv encodes one of the sources when the first
8328 operand is not an immediate. */
1ef99a7b 8329 if (i.tm.opcode_modifier.vexw == VEXW0)
5dd85c99
SP
8330 i.vex.register_specifier = i.op[0].regs;
8331 else
8332 i.vex.register_specifier = i.op[1].regs;
8333 }
8334
8335 /* Destination is a XMM register encoded in the ModRM.reg
8336 and VEX.R bit. */
8337 i.rm.reg = i.op[2].regs->reg_num;
8338 if ((i.op[2].regs->reg_flags & RegRex) != 0)
8339 i.rex |= REX_R;
8340
8341 /* ModRM.rm and VEX.B encodes the other source. */
8342 if (!i.mem_operands)
8343 {
8344 i.rm.mode = 3;
8345
1ef99a7b 8346 if (i.tm.opcode_modifier.vexw == VEXW0)
5dd85c99
SP
8347 i.rm.regmem = i.op[1].regs->reg_num;
8348 else
8349 i.rm.regmem = i.op[0].regs->reg_num;
8350
8351 if ((i.op[1].regs->reg_flags & RegRex) != 0)
8352 i.rex |= REX_B;
8353 }
8354 }
2426c15f 8355 else if (i.tm.opcode_modifier.vexvvvv == VEXLWP)
f88c9eb0
SP
8356 {
8357 i.vex.register_specifier = i.op[2].regs;
8358 if (!i.mem_operands)
8359 {
8360 i.rm.mode = 3;
8361 i.rm.regmem = i.op[1].regs->reg_num;
8362 if ((i.op[1].regs->reg_flags & RegRex) != 0)
8363 i.rex |= REX_B;
8364 }
8365 }
29b0f896
AM
8366 /* Fill in i.rm.reg or i.rm.regmem field with register operand
8367 (if any) based on i.tm.extension_opcode. Again, we must be
8368 careful to make sure that segment/control/debug/test/MMX
8369 registers are coded into the i.rm.reg field. */
f88c9eb0 8370 else if (i.reg_operands)
29b0f896 8371 {
99018f42 8372 unsigned int op;
7ab9ffdd
L
8373 unsigned int vex_reg = ~0;
8374
8375 for (op = 0; op < i.operands; op++)
921eafea
L
8376 if (i.types[op].bitfield.class == Reg
8377 || i.types[op].bitfield.class == RegBND
8378 || i.types[op].bitfield.class == RegMask
8379 || i.types[op].bitfield.class == SReg
8380 || i.types[op].bitfield.class == RegCR
8381 || i.types[op].bitfield.class == RegDR
8382 || i.types[op].bitfield.class == RegTR
8383 || i.types[op].bitfield.class == RegSIMD
8384 || i.types[op].bitfield.class == RegMMX)
8385 break;
c0209578 8386
7ab9ffdd
L
8387 if (vex_3_sources)
8388 op = dest;
2426c15f 8389 else if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
7ab9ffdd
L
8390 {
8391 /* For instructions with VexNDS, the register-only
8392 source operand is encoded in VEX prefix. */
8393 gas_assert (mem != (unsigned int) ~0);
c0f3af97 8394
7ab9ffdd 8395 if (op > mem)
c0f3af97 8396 {
7ab9ffdd
L
8397 vex_reg = op++;
8398 gas_assert (op < i.operands);
c0f3af97
L
8399 }
8400 else
c0f3af97 8401 {
f12dc422
L
8402 /* Check register-only source operand when two source
8403 operands are swapped. */
8404 if (!i.tm.operand_types[op].bitfield.baseindex
8405 && i.tm.operand_types[op + 1].bitfield.baseindex)
8406 {
8407 vex_reg = op;
8408 op += 2;
8409 gas_assert (mem == (vex_reg + 1)
8410 && op < i.operands);
8411 }
8412 else
8413 {
8414 vex_reg = op + 1;
8415 gas_assert (vex_reg < i.operands);
8416 }
c0f3af97 8417 }
7ab9ffdd 8418 }
2426c15f 8419 else if (i.tm.opcode_modifier.vexvvvv == VEXNDD)
7ab9ffdd 8420 {
f12dc422 8421 /* For instructions with VexNDD, the register destination
7ab9ffdd 8422 is encoded in VEX prefix. */
f12dc422
L
8423 if (i.mem_operands == 0)
8424 {
8425 /* There is no memory operand. */
8426 gas_assert ((op + 2) == i.operands);
8427 vex_reg = op + 1;
8428 }
8429 else
8d63c93e 8430 {
ed438a93
JB
8431 /* There are only 2 non-immediate operands. */
8432 gas_assert (op < i.imm_operands + 2
8433 && i.operands == i.imm_operands + 2);
8434 vex_reg = i.imm_operands + 1;
f12dc422 8435 }
7ab9ffdd
L
8436 }
8437 else
8438 gas_assert (op < i.operands);
99018f42 8439
7ab9ffdd
L
8440 if (vex_reg != (unsigned int) ~0)
8441 {
f12dc422 8442 i386_operand_type *type = &i.tm.operand_types[vex_reg];
7ab9ffdd 8443
bab6aec1 8444 if ((type->bitfield.class != Reg
dc821c5f 8445 || (!type->bitfield.dword && !type->bitfield.qword))
3528c362 8446 && type->bitfield.class != RegSIMD
43234a1e 8447 && !operand_type_equal (type, &regmask))
7ab9ffdd 8448 abort ();
f88c9eb0 8449
7ab9ffdd
L
8450 i.vex.register_specifier = i.op[vex_reg].regs;
8451 }
8452
1b9f0c97
L
8453 /* Don't set OP operand twice. */
8454 if (vex_reg != op)
7ab9ffdd 8455 {
1b9f0c97
L
8456 /* If there is an extension opcode to put here, the
8457 register number must be put into the regmem field. */
8458 if (i.tm.extension_opcode != None)
8459 {
8460 i.rm.regmem = i.op[op].regs->reg_num;
a5aeccd9
JB
8461 set_rex_vrex (i.op[op].regs, REX_B,
8462 i.tm.opcode_modifier.sse2avx);
1b9f0c97
L
8463 }
8464 else
8465 {
8466 i.rm.reg = i.op[op].regs->reg_num;
a5aeccd9
JB
8467 set_rex_vrex (i.op[op].regs, REX_R,
8468 i.tm.opcode_modifier.sse2avx);
1b9f0c97 8469 }
7ab9ffdd 8470 }
252b5132 8471
29b0f896
AM
8472 /* Now, if no memory operand has set i.rm.mode = 0, 1, 2 we
8473 must set it to 3 to indicate this is a register operand
8474 in the regmem field. */
8475 if (!i.mem_operands)
8476 i.rm.mode = 3;
8477 }
252b5132 8478
29b0f896 8479 /* Fill in i.rm.reg field with extension opcode (if any). */
c1e679ec 8480 if (i.tm.extension_opcode != None)
29b0f896
AM
8481 i.rm.reg = i.tm.extension_opcode;
8482 }
8483 return default_seg;
8484}
252b5132 8485
48ef937e
JB
8486static INLINE void
8487frag_opcode_byte (unsigned char byte)
8488{
8489 if (now_seg != absolute_section)
8490 FRAG_APPEND_1_CHAR (byte);
8491 else
8492 ++abs_section_offset;
8493}
8494
376cd056
JB
8495static unsigned int
8496flip_code16 (unsigned int code16)
8497{
8498 gas_assert (i.tm.operands == 1);
8499
8500 return !(i.prefix[REX_PREFIX] & REX_W)
8501 && (code16 ? i.tm.operand_types[0].bitfield.disp32
8502 || i.tm.operand_types[0].bitfield.disp32s
8503 : i.tm.operand_types[0].bitfield.disp16)
8504 ? CODE16 : 0;
8505}
8506
29b0f896 8507static void
e3bb37b5 8508output_branch (void)
29b0f896
AM
8509{
8510 char *p;
f8a5c266 8511 int size;
29b0f896
AM
8512 int code16;
8513 int prefix;
8514 relax_substateT subtype;
8515 symbolS *sym;
8516 offsetT off;
8517
48ef937e
JB
8518 if (now_seg == absolute_section)
8519 {
8520 as_bad (_("relaxable branches not supported in absolute section"));
8521 return;
8522 }
8523
f8a5c266 8524 code16 = flag_code == CODE_16BIT ? CODE16 : 0;
a501d77e 8525 size = i.disp_encoding == disp_encoding_32bit ? BIG : SMALL;
29b0f896
AM
8526
8527 prefix = 0;
8528 if (i.prefix[DATA_PREFIX] != 0)
252b5132 8529 {
29b0f896
AM
8530 prefix = 1;
8531 i.prefixes -= 1;
376cd056 8532 code16 ^= flip_code16(code16);
252b5132 8533 }
29b0f896
AM
8534 /* Pentium4 branch hints. */
8535 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8536 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
2f66722d 8537 {
29b0f896
AM
8538 prefix++;
8539 i.prefixes--;
8540 }
8541 if (i.prefix[REX_PREFIX] != 0)
8542 {
8543 prefix++;
8544 i.prefixes--;
2f66722d
AM
8545 }
8546
7e8b059b
L
8547 /* BND prefixed jump. */
8548 if (i.prefix[BND_PREFIX] != 0)
8549 {
6cb0a70e
JB
8550 prefix++;
8551 i.prefixes--;
7e8b059b
L
8552 }
8553
f2810fe0
JB
8554 if (i.prefixes != 0)
8555 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
29b0f896
AM
8556
8557 /* It's always a symbol; End frag & setup for relax.
8558 Make sure there is enough room in this frag for the largest
8559 instruction we may generate in md_convert_frag. This is 2
8560 bytes for the opcode and room for the prefix and largest
8561 displacement. */
8562 frag_grow (prefix + 2 + 4);
8563 /* Prefix and 1 opcode byte go in fr_fix. */
8564 p = frag_more (prefix + 1);
8565 if (i.prefix[DATA_PREFIX] != 0)
8566 *p++ = DATA_PREFIX_OPCODE;
8567 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
8568 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
8569 *p++ = i.prefix[SEG_PREFIX];
6cb0a70e
JB
8570 if (i.prefix[BND_PREFIX] != 0)
8571 *p++ = BND_PREFIX_OPCODE;
29b0f896
AM
8572 if (i.prefix[REX_PREFIX] != 0)
8573 *p++ = i.prefix[REX_PREFIX];
8574 *p = i.tm.base_opcode;
8575
8576 if ((unsigned char) *p == JUMP_PC_RELATIVE)
f8a5c266 8577 subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, size);
40fb9820 8578 else if (cpu_arch_flags.bitfield.cpui386)
f8a5c266 8579 subtype = ENCODE_RELAX_STATE (COND_JUMP, size);
29b0f896 8580 else
f8a5c266 8581 subtype = ENCODE_RELAX_STATE (COND_JUMP86, size);
29b0f896 8582 subtype |= code16;
3e73aa7c 8583
29b0f896
AM
8584 sym = i.op[0].disps->X_add_symbol;
8585 off = i.op[0].disps->X_add_number;
3e73aa7c 8586
29b0f896
AM
8587 if (i.op[0].disps->X_op != O_constant
8588 && i.op[0].disps->X_op != O_symbol)
3e73aa7c 8589 {
29b0f896
AM
8590 /* Handle complex expressions. */
8591 sym = make_expr_symbol (i.op[0].disps);
8592 off = 0;
8593 }
3e73aa7c 8594
29b0f896
AM
8595 /* 1 possible extra opcode + 4 byte displacement go in var part.
8596 Pass reloc in fr_var. */
d258b828 8597 frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
29b0f896 8598}
3e73aa7c 8599
bd7ab16b
L
8600#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8601/* Return TRUE iff PLT32 relocation should be used for branching to
8602 symbol S. */
8603
8604static bfd_boolean
8605need_plt32_p (symbolS *s)
8606{
8607 /* PLT32 relocation is ELF only. */
8608 if (!IS_ELF)
8609 return FALSE;
8610
a5def729
RO
8611#ifdef TE_SOLARIS
8612 /* Don't emit PLT32 relocation on Solaris: neither native linker nor
8613 krtld support it. */
8614 return FALSE;
8615#endif
8616
bd7ab16b
L
8617 /* Since there is no need to prepare for PLT branch on x86-64, we
8618 can generate R_X86_64_PLT32, instead of R_X86_64_PC32, which can
8619 be used as a marker for 32-bit PC-relative branches. */
8620 if (!object_64bit)
8621 return FALSE;
8622
44365e88
AM
8623 if (s == NULL)
8624 return FALSE;
8625
bd7ab16b
L
8626 /* Weak or undefined symbol need PLT32 relocation. */
8627 if (S_IS_WEAK (s) || !S_IS_DEFINED (s))
8628 return TRUE;
8629
8630 /* Non-global symbol doesn't need PLT32 relocation. */
8631 if (! S_IS_EXTERNAL (s))
8632 return FALSE;
8633
8634 /* Other global symbols need PLT32 relocation. NB: Symbol with
8635 non-default visibilities are treated as normal global symbol
8636 so that PLT32 relocation can be used as a marker for 32-bit
8637 PC-relative branches. It is useful for linker relaxation. */
8638 return TRUE;
8639}
8640#endif
8641
29b0f896 8642static void
e3bb37b5 8643output_jump (void)
29b0f896
AM
8644{
8645 char *p;
8646 int size;
3e02c1cc 8647 fixS *fixP;
bd7ab16b 8648 bfd_reloc_code_real_type jump_reloc = i.reloc[0];
29b0f896 8649
0cfa3eb3 8650 if (i.tm.opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
8651 {
8652 /* This is a loop or jecxz type instruction. */
8653 size = 1;
8654 if (i.prefix[ADDR_PREFIX] != 0)
8655 {
48ef937e 8656 frag_opcode_byte (ADDR_PREFIX_OPCODE);
29b0f896
AM
8657 i.prefixes -= 1;
8658 }
8659 /* Pentium4 branch hints. */
8660 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8661 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
8662 {
48ef937e 8663 frag_opcode_byte (i.prefix[SEG_PREFIX]);
29b0f896 8664 i.prefixes--;
3e73aa7c
JH
8665 }
8666 }
29b0f896
AM
8667 else
8668 {
8669 int code16;
3e73aa7c 8670
29b0f896
AM
8671 code16 = 0;
8672 if (flag_code == CODE_16BIT)
8673 code16 = CODE16;
3e73aa7c 8674
29b0f896
AM
8675 if (i.prefix[DATA_PREFIX] != 0)
8676 {
48ef937e 8677 frag_opcode_byte (DATA_PREFIX_OPCODE);
29b0f896 8678 i.prefixes -= 1;
376cd056 8679 code16 ^= flip_code16(code16);
29b0f896 8680 }
252b5132 8681
29b0f896
AM
8682 size = 4;
8683 if (code16)
8684 size = 2;
8685 }
9fcc94b6 8686
6cb0a70e
JB
8687 /* BND prefixed jump. */
8688 if (i.prefix[BND_PREFIX] != 0)
29b0f896 8689 {
48ef937e 8690 frag_opcode_byte (i.prefix[BND_PREFIX]);
29b0f896
AM
8691 i.prefixes -= 1;
8692 }
252b5132 8693
6cb0a70e 8694 if (i.prefix[REX_PREFIX] != 0)
7e8b059b 8695 {
48ef937e 8696 frag_opcode_byte (i.prefix[REX_PREFIX]);
7e8b059b
L
8697 i.prefixes -= 1;
8698 }
8699
f2810fe0
JB
8700 if (i.prefixes != 0)
8701 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
e0890092 8702
48ef937e
JB
8703 if (now_seg == absolute_section)
8704 {
9a182d04 8705 abs_section_offset += i.opcode_length + size;
48ef937e
JB
8706 return;
8707 }
8708
9a182d04
JB
8709 p = frag_more (i.opcode_length + size);
8710 switch (i.opcode_length)
42164a71
L
8711 {
8712 case 2:
8713 *p++ = i.tm.base_opcode >> 8;
1a0670f3 8714 /* Fall through. */
42164a71
L
8715 case 1:
8716 *p++ = i.tm.base_opcode;
8717 break;
8718 default:
8719 abort ();
8720 }
e0890092 8721
bd7ab16b
L
8722#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8723 if (size == 4
8724 && jump_reloc == NO_RELOC
8725 && need_plt32_p (i.op[0].disps->X_add_symbol))
8726 jump_reloc = BFD_RELOC_X86_64_PLT32;
8727#endif
8728
8729 jump_reloc = reloc (size, 1, 1, jump_reloc);
8730
3e02c1cc 8731 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
bd7ab16b 8732 i.op[0].disps, 1, jump_reloc);
3e02c1cc
AM
8733
8734 /* All jumps handled here are signed, but don't use a signed limit
8735 check for 32 and 16 bit jumps as we want to allow wrap around at
8736 4G and 64k respectively. */
8737 if (size == 1)
8738 fixP->fx_signed = 1;
29b0f896 8739}
e0890092 8740
29b0f896 8741static void
e3bb37b5 8742output_interseg_jump (void)
29b0f896
AM
8743{
8744 char *p;
8745 int size;
8746 int prefix;
8747 int code16;
252b5132 8748
29b0f896
AM
8749 code16 = 0;
8750 if (flag_code == CODE_16BIT)
8751 code16 = CODE16;
a217f122 8752
29b0f896
AM
8753 prefix = 0;
8754 if (i.prefix[DATA_PREFIX] != 0)
8755 {
8756 prefix = 1;
8757 i.prefixes -= 1;
8758 code16 ^= CODE16;
8759 }
6cb0a70e
JB
8760
8761 gas_assert (!i.prefix[REX_PREFIX]);
252b5132 8762
29b0f896
AM
8763 size = 4;
8764 if (code16)
8765 size = 2;
252b5132 8766
f2810fe0
JB
8767 if (i.prefixes != 0)
8768 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
252b5132 8769
48ef937e
JB
8770 if (now_seg == absolute_section)
8771 {
8772 abs_section_offset += prefix + 1 + 2 + size;
8773 return;
8774 }
8775
29b0f896
AM
8776 /* 1 opcode; 2 segment; offset */
8777 p = frag_more (prefix + 1 + 2 + size);
3e73aa7c 8778
29b0f896
AM
8779 if (i.prefix[DATA_PREFIX] != 0)
8780 *p++ = DATA_PREFIX_OPCODE;
252b5132 8781
29b0f896
AM
8782 if (i.prefix[REX_PREFIX] != 0)
8783 *p++ = i.prefix[REX_PREFIX];
252b5132 8784
29b0f896
AM
8785 *p++ = i.tm.base_opcode;
8786 if (i.op[1].imms->X_op == O_constant)
8787 {
8788 offsetT n = i.op[1].imms->X_add_number;
252b5132 8789
29b0f896
AM
8790 if (size == 2
8791 && !fits_in_unsigned_word (n)
8792 && !fits_in_signed_word (n))
8793 {
8794 as_bad (_("16-bit jump out of range"));
8795 return;
8796 }
8797 md_number_to_chars (p, n, size);
8798 }
8799 else
8800 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
d258b828 8801 i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
6d96a594
C
8802
8803 p += size;
8804 if (i.op[0].imms->X_op == O_constant)
8805 md_number_to_chars (p, (valueT) i.op[0].imms->X_add_number, 2);
8806 else
8807 fix_new_exp (frag_now, p - frag_now->fr_literal, 2,
8808 i.op[0].imms, 0, reloc (2, 0, 0, i.reloc[0]));
29b0f896 8809}
a217f122 8810
b4a3a7b4
L
8811#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8812void
8813x86_cleanup (void)
8814{
8815 char *p;
8816 asection *seg = now_seg;
8817 subsegT subseg = now_subseg;
8818 asection *sec;
8819 unsigned int alignment, align_size_1;
8820 unsigned int isa_1_descsz, feature_2_descsz, descsz;
8821 unsigned int isa_1_descsz_raw, feature_2_descsz_raw;
8822 unsigned int padding;
8823
8824 if (!IS_ELF || !x86_used_note)
8825 return;
8826
b4a3a7b4
L
8827 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X86;
8828
8829 /* The .note.gnu.property section layout:
8830
8831 Field Length Contents
8832 ---- ---- ----
8833 n_namsz 4 4
8834 n_descsz 4 The note descriptor size
8835 n_type 4 NT_GNU_PROPERTY_TYPE_0
8836 n_name 4 "GNU"
8837 n_desc n_descsz The program property array
8838 .... .... ....
8839 */
8840
8841 /* Create the .note.gnu.property section. */
8842 sec = subseg_new (NOTE_GNU_PROPERTY_SECTION_NAME, 0);
fd361982 8843 bfd_set_section_flags (sec,
b4a3a7b4
L
8844 (SEC_ALLOC
8845 | SEC_LOAD
8846 | SEC_DATA
8847 | SEC_HAS_CONTENTS
8848 | SEC_READONLY));
8849
8850 if (get_elf_backend_data (stdoutput)->s->elfclass == ELFCLASS64)
8851 {
8852 align_size_1 = 7;
8853 alignment = 3;
8854 }
8855 else
8856 {
8857 align_size_1 = 3;
8858 alignment = 2;
8859 }
8860
fd361982 8861 bfd_set_section_alignment (sec, alignment);
b4a3a7b4
L
8862 elf_section_type (sec) = SHT_NOTE;
8863
8864 /* GNU_PROPERTY_X86_ISA_1_USED: 4-byte type + 4-byte data size
8865 + 4-byte data */
8866 isa_1_descsz_raw = 4 + 4 + 4;
8867 /* Align GNU_PROPERTY_X86_ISA_1_USED. */
8868 isa_1_descsz = (isa_1_descsz_raw + align_size_1) & ~align_size_1;
8869
8870 feature_2_descsz_raw = isa_1_descsz;
8871 /* GNU_PROPERTY_X86_FEATURE_2_USED: 4-byte type + 4-byte data size
8872 + 4-byte data */
8873 feature_2_descsz_raw += 4 + 4 + 4;
8874 /* Align GNU_PROPERTY_X86_FEATURE_2_USED. */
8875 feature_2_descsz = ((feature_2_descsz_raw + align_size_1)
8876 & ~align_size_1);
8877
8878 descsz = feature_2_descsz;
8879 /* Section size: n_namsz + n_descsz + n_type + n_name + n_descsz. */
8880 p = frag_more (4 + 4 + 4 + 4 + descsz);
8881
8882 /* Write n_namsz. */
8883 md_number_to_chars (p, (valueT) 4, 4);
8884
8885 /* Write n_descsz. */
8886 md_number_to_chars (p + 4, (valueT) descsz, 4);
8887
8888 /* Write n_type. */
8889 md_number_to_chars (p + 4 * 2, (valueT) NT_GNU_PROPERTY_TYPE_0, 4);
8890
8891 /* Write n_name. */
8892 memcpy (p + 4 * 3, "GNU", 4);
8893
8894 /* Write 4-byte type. */
8895 md_number_to_chars (p + 4 * 4,
8896 (valueT) GNU_PROPERTY_X86_ISA_1_USED, 4);
8897
8898 /* Write 4-byte data size. */
8899 md_number_to_chars (p + 4 * 5, (valueT) 4, 4);
8900
8901 /* Write 4-byte data. */
8902 md_number_to_chars (p + 4 * 6, (valueT) x86_isa_1_used, 4);
8903
8904 /* Zero out paddings. */
8905 padding = isa_1_descsz - isa_1_descsz_raw;
8906 if (padding)
8907 memset (p + 4 * 7, 0, padding);
8908
8909 /* Write 4-byte type. */
8910 md_number_to_chars (p + isa_1_descsz + 4 * 4,
8911 (valueT) GNU_PROPERTY_X86_FEATURE_2_USED, 4);
8912
8913 /* Write 4-byte data size. */
8914 md_number_to_chars (p + isa_1_descsz + 4 * 5, (valueT) 4, 4);
8915
8916 /* Write 4-byte data. */
8917 md_number_to_chars (p + isa_1_descsz + 4 * 6,
8918 (valueT) x86_feature_2_used, 4);
8919
8920 /* Zero out paddings. */
8921 padding = feature_2_descsz - feature_2_descsz_raw;
8922 if (padding)
8923 memset (p + isa_1_descsz + 4 * 7, 0, padding);
8924
8925 /* We probably can't restore the current segment, for there likely
8926 isn't one yet... */
8927 if (seg && subseg)
8928 subseg_set (seg, subseg);
8929}
8930#endif
8931
9c33702b
JB
8932static unsigned int
8933encoding_length (const fragS *start_frag, offsetT start_off,
8934 const char *frag_now_ptr)
8935{
8936 unsigned int len = 0;
8937
8938 if (start_frag != frag_now)
8939 {
8940 const fragS *fr = start_frag;
8941
8942 do {
8943 len += fr->fr_fix;
8944 fr = fr->fr_next;
8945 } while (fr && fr != frag_now);
8946 }
8947
8948 return len - start_off + (frag_now_ptr - frag_now->fr_literal);
8949}
8950
e379e5f3 8951/* Return 1 for test, and, cmp, add, sub, inc and dec which may
79d72f45
HL
8952 be macro-fused with conditional jumps.
8953 NB: If TEST/AND/CMP/ADD/SUB/INC/DEC is of RIP relative address,
8954 or is one of the following format:
8955
8956 cmp m, imm
8957 add m, imm
8958 sub m, imm
8959 test m, imm
8960 and m, imm
8961 inc m
8962 dec m
8963
8964 it is unfusible. */
e379e5f3
L
8965
8966static int
79d72f45 8967maybe_fused_with_jcc_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
8968{
8969 /* No RIP address. */
8970 if (i.base_reg && i.base_reg->reg_num == RegIP)
8971 return 0;
8972
389d00a5
JB
8973 /* No opcodes outside of base encoding space. */
8974 if (i.tm.opcode_modifier.opcodespace != SPACE_BASE)
e379e5f3
L
8975 return 0;
8976
79d72f45
HL
8977 /* add, sub without add/sub m, imm. */
8978 if (i.tm.base_opcode <= 5
e379e5f3
L
8979 || (i.tm.base_opcode >= 0x28 && i.tm.base_opcode <= 0x2d)
8980 || ((i.tm.base_opcode | 3) == 0x83
79d72f45 8981 && (i.tm.extension_opcode == 0x5
e379e5f3 8982 || i.tm.extension_opcode == 0x0)))
79d72f45
HL
8983 {
8984 *mf_cmp_p = mf_cmp_alu_cmp;
8985 return !(i.mem_operands && i.imm_operands);
8986 }
e379e5f3 8987
79d72f45
HL
8988 /* and without and m, imm. */
8989 if ((i.tm.base_opcode >= 0x20 && i.tm.base_opcode <= 0x25)
8990 || ((i.tm.base_opcode | 3) == 0x83
8991 && i.tm.extension_opcode == 0x4))
8992 {
8993 *mf_cmp_p = mf_cmp_test_and;
8994 return !(i.mem_operands && i.imm_operands);
8995 }
8996
8997 /* test without test m imm. */
e379e5f3
L
8998 if ((i.tm.base_opcode | 1) == 0x85
8999 || (i.tm.base_opcode | 1) == 0xa9
9000 || ((i.tm.base_opcode | 1) == 0xf7
79d72f45
HL
9001 && i.tm.extension_opcode == 0))
9002 {
9003 *mf_cmp_p = mf_cmp_test_and;
9004 return !(i.mem_operands && i.imm_operands);
9005 }
9006
9007 /* cmp without cmp m, imm. */
9008 if ((i.tm.base_opcode >= 0x38 && i.tm.base_opcode <= 0x3d)
e379e5f3
L
9009 || ((i.tm.base_opcode | 3) == 0x83
9010 && (i.tm.extension_opcode == 0x7)))
79d72f45
HL
9011 {
9012 *mf_cmp_p = mf_cmp_alu_cmp;
9013 return !(i.mem_operands && i.imm_operands);
9014 }
e379e5f3 9015
79d72f45 9016 /* inc, dec without inc/dec m. */
e379e5f3
L
9017 if ((i.tm.cpu_flags.bitfield.cpuno64
9018 && (i.tm.base_opcode | 0xf) == 0x4f)
9019 || ((i.tm.base_opcode | 1) == 0xff
9020 && i.tm.extension_opcode <= 0x1))
79d72f45
HL
9021 {
9022 *mf_cmp_p = mf_cmp_incdec;
9023 return !i.mem_operands;
9024 }
e379e5f3
L
9025
9026 return 0;
9027}
9028
9029/* Return 1 if a FUSED_JCC_PADDING frag should be generated. */
9030
9031static int
79d72f45 9032add_fused_jcc_padding_frag_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
9033{
9034 /* NB: Don't work with COND_JUMP86 without i386. */
9035 if (!align_branch_power
9036 || now_seg == absolute_section
9037 || !cpu_arch_flags.bitfield.cpui386
9038 || !(align_branch & align_branch_fused_bit))
9039 return 0;
9040
79d72f45 9041 if (maybe_fused_with_jcc_p (mf_cmp_p))
e379e5f3
L
9042 {
9043 if (last_insn.kind == last_insn_other
9044 || last_insn.seg != now_seg)
9045 return 1;
9046 if (flag_debug)
9047 as_warn_where (last_insn.file, last_insn.line,
9048 _("`%s` skips -malign-branch-boundary on `%s`"),
9049 last_insn.name, i.tm.name);
9050 }
9051
9052 return 0;
9053}
9054
9055/* Return 1 if a BRANCH_PREFIX frag should be generated. */
9056
9057static int
9058add_branch_prefix_frag_p (void)
9059{
9060 /* NB: Don't work with COND_JUMP86 without i386. Don't add prefix
9061 to PadLock instructions since they include prefixes in opcode. */
9062 if (!align_branch_power
9063 || !align_branch_prefix_size
9064 || now_seg == absolute_section
9065 || i.tm.cpu_flags.bitfield.cpupadlock
9066 || !cpu_arch_flags.bitfield.cpui386)
9067 return 0;
9068
9069 /* Don't add prefix if it is a prefix or there is no operand in case
9070 that segment prefix is special. */
9071 if (!i.operands || i.tm.opcode_modifier.isprefix)
9072 return 0;
9073
9074 if (last_insn.kind == last_insn_other
9075 || last_insn.seg != now_seg)
9076 return 1;
9077
9078 if (flag_debug)
9079 as_warn_where (last_insn.file, last_insn.line,
9080 _("`%s` skips -malign-branch-boundary on `%s`"),
9081 last_insn.name, i.tm.name);
9082
9083 return 0;
9084}
9085
9086/* Return 1 if a BRANCH_PADDING frag should be generated. */
9087
9088static int
79d72f45
HL
9089add_branch_padding_frag_p (enum align_branch_kind *branch_p,
9090 enum mf_jcc_kind *mf_jcc_p)
e379e5f3
L
9091{
9092 int add_padding;
9093
9094 /* NB: Don't work with COND_JUMP86 without i386. */
9095 if (!align_branch_power
9096 || now_seg == absolute_section
389d00a5
JB
9097 || !cpu_arch_flags.bitfield.cpui386
9098 || i.tm.opcode_modifier.opcodespace != SPACE_BASE)
e379e5f3
L
9099 return 0;
9100
9101 add_padding = 0;
9102
9103 /* Check for jcc and direct jmp. */
9104 if (i.tm.opcode_modifier.jump == JUMP)
9105 {
9106 if (i.tm.base_opcode == JUMP_PC_RELATIVE)
9107 {
9108 *branch_p = align_branch_jmp;
9109 add_padding = align_branch & align_branch_jmp_bit;
9110 }
9111 else
9112 {
79d72f45
HL
9113 /* Because J<cc> and JN<cc> share same group in macro-fusible table,
9114 igore the lowest bit. */
9115 *mf_jcc_p = (i.tm.base_opcode & 0x0e) >> 1;
e379e5f3
L
9116 *branch_p = align_branch_jcc;
9117 if ((align_branch & align_branch_jcc_bit))
9118 add_padding = 1;
9119 }
9120 }
e379e5f3
L
9121 else if ((i.tm.base_opcode | 1) == 0xc3)
9122 {
9123 /* Near ret. */
9124 *branch_p = align_branch_ret;
9125 if ((align_branch & align_branch_ret_bit))
9126 add_padding = 1;
9127 }
9128 else
9129 {
9130 /* Check for indirect jmp, direct and indirect calls. */
9131 if (i.tm.base_opcode == 0xe8)
9132 {
9133 /* Direct call. */
9134 *branch_p = align_branch_call;
9135 if ((align_branch & align_branch_call_bit))
9136 add_padding = 1;
9137 }
9138 else if (i.tm.base_opcode == 0xff
9139 && (i.tm.extension_opcode == 2
9140 || i.tm.extension_opcode == 4))
9141 {
9142 /* Indirect call and jmp. */
9143 *branch_p = align_branch_indirect;
9144 if ((align_branch & align_branch_indirect_bit))
9145 add_padding = 1;
9146 }
9147
9148 if (add_padding
9149 && i.disp_operands
9150 && tls_get_addr
9151 && (i.op[0].disps->X_op == O_symbol
9152 || (i.op[0].disps->X_op == O_subtract
9153 && i.op[0].disps->X_op_symbol == GOT_symbol)))
9154 {
9155 symbolS *s = i.op[0].disps->X_add_symbol;
9156 /* No padding to call to global or undefined tls_get_addr. */
9157 if ((S_IS_EXTERNAL (s) || !S_IS_DEFINED (s))
9158 && strcmp (S_GET_NAME (s), tls_get_addr) == 0)
9159 return 0;
9160 }
9161 }
9162
9163 if (add_padding
9164 && last_insn.kind != last_insn_other
9165 && last_insn.seg == now_seg)
9166 {
9167 if (flag_debug)
9168 as_warn_where (last_insn.file, last_insn.line,
9169 _("`%s` skips -malign-branch-boundary on `%s`"),
9170 last_insn.name, i.tm.name);
9171 return 0;
9172 }
9173
9174 return add_padding;
9175}
9176
29b0f896 9177static void
e3bb37b5 9178output_insn (void)
29b0f896 9179{
2bbd9c25
JJ
9180 fragS *insn_start_frag;
9181 offsetT insn_start_off;
e379e5f3
L
9182 fragS *fragP = NULL;
9183 enum align_branch_kind branch = align_branch_none;
79d72f45
HL
9184 /* The initializer is arbitrary just to avoid uninitialized error.
9185 it's actually either assigned in add_branch_padding_frag_p
9186 or never be used. */
9187 enum mf_jcc_kind mf_jcc = mf_jcc_jo;
2bbd9c25 9188
b4a3a7b4 9189#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
48ef937e 9190 if (IS_ELF && x86_used_note && now_seg != absolute_section)
b4a3a7b4 9191 {
32930e4e
L
9192 if ((i.xstate & xstate_tmm) == xstate_tmm
9193 || i.tm.cpu_flags.bitfield.cpuamx_tile)
9194 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_TMM;
9195
b4a3a7b4
L
9196 if (i.tm.cpu_flags.bitfield.cpu8087
9197 || i.tm.cpu_flags.bitfield.cpu287
9198 || i.tm.cpu_flags.bitfield.cpu387
9199 || i.tm.cpu_flags.bitfield.cpu687
9200 || i.tm.cpu_flags.bitfield.cpufisttp)
9201 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X87;
014d61ea 9202
921eafea 9203 if ((i.xstate & xstate_mmx)
389d00a5
JB
9204 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
9205 && !is_any_vex_encoding (&i.tm)
9206 && (i.tm.base_opcode == 0x77 /* emms */
9207 || i.tm.base_opcode == 0x0e /* femms */)))
b4a3a7b4 9208 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MMX;
014d61ea 9209
32930e4e
L
9210 if (i.index_reg)
9211 {
9212 if (i.index_reg->reg_type.bitfield.zmmword)
9213 i.xstate |= xstate_zmm;
9214 else if (i.index_reg->reg_type.bitfield.ymmword)
9215 i.xstate |= xstate_ymm;
9216 else if (i.index_reg->reg_type.bitfield.xmmword)
9217 i.xstate |= xstate_xmm;
9218 }
014d61ea
JB
9219
9220 /* vzeroall / vzeroupper */
9221 if (i.tm.base_opcode == 0x77 && i.tm.cpu_flags.bitfield.cpuavx)
9222 i.xstate |= xstate_ymm;
9223
c4694f17 9224 if ((i.xstate & xstate_xmm)
389d00a5
JB
9225 /* ldmxcsr / stmxcsr / vldmxcsr / vstmxcsr */
9226 || (i.tm.base_opcode == 0xae
9227 && (i.tm.cpu_flags.bitfield.cpusse
9228 || i.tm.cpu_flags.bitfield.cpuavx))
c4694f17
TG
9229 || i.tm.cpu_flags.bitfield.cpuwidekl
9230 || i.tm.cpu_flags.bitfield.cpukl)
b4a3a7b4 9231 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XMM;
014d61ea 9232
921eafea 9233 if ((i.xstate & xstate_ymm) == xstate_ymm)
b4a3a7b4 9234 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_YMM;
921eafea 9235 if ((i.xstate & xstate_zmm) == xstate_zmm)
b4a3a7b4 9236 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_ZMM;
6225c532 9237 if (i.mask.reg || (i.xstate & xstate_mask) == xstate_mask)
32930e4e 9238 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MASK;
b4a3a7b4
L
9239 if (i.tm.cpu_flags.bitfield.cpufxsr)
9240 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_FXSR;
9241 if (i.tm.cpu_flags.bitfield.cpuxsave)
9242 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVE;
9243 if (i.tm.cpu_flags.bitfield.cpuxsaveopt)
9244 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT;
9245 if (i.tm.cpu_flags.bitfield.cpuxsavec)
9246 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEC;
b0ab0693
L
9247
9248 if (x86_feature_2_used
9249 || i.tm.cpu_flags.bitfield.cpucmov
9250 || i.tm.cpu_flags.bitfield.cpusyscall
389d00a5
JB
9251 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
9252 && i.tm.base_opcode == 0xc7
70e95837 9253 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
b0ab0693
L
9254 && i.tm.extension_opcode == 1) /* cmpxchg8b */)
9255 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_BASELINE;
9256 if (i.tm.cpu_flags.bitfield.cpusse3
9257 || i.tm.cpu_flags.bitfield.cpussse3
9258 || i.tm.cpu_flags.bitfield.cpusse4_1
9259 || i.tm.cpu_flags.bitfield.cpusse4_2
9260 || i.tm.cpu_flags.bitfield.cpucx16
9261 || i.tm.cpu_flags.bitfield.cpupopcnt
9262 /* LAHF-SAHF insns in 64-bit mode. */
9263 || (flag_code == CODE_64BIT
35648716
JB
9264 && (i.tm.base_opcode | 1) == 0x9f
9265 && i.tm.opcode_modifier.opcodespace == SPACE_BASE))
b0ab0693
L
9266 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V2;
9267 if (i.tm.cpu_flags.bitfield.cpuavx
9268 || i.tm.cpu_flags.bitfield.cpuavx2
9269 /* Any VEX encoded insns execpt for CpuAVX512F, CpuAVX512BW,
9270 CpuAVX512DQ, LPW, TBM and AMX. */
9271 || (i.tm.opcode_modifier.vex
9272 && !i.tm.cpu_flags.bitfield.cpuavx512f
9273 && !i.tm.cpu_flags.bitfield.cpuavx512bw
9274 && !i.tm.cpu_flags.bitfield.cpuavx512dq
9275 && !i.tm.cpu_flags.bitfield.cpulwp
9276 && !i.tm.cpu_flags.bitfield.cputbm
9277 && !(x86_feature_2_used & GNU_PROPERTY_X86_FEATURE_2_TMM))
9278 || i.tm.cpu_flags.bitfield.cpuf16c
9279 || i.tm.cpu_flags.bitfield.cpufma
9280 || i.tm.cpu_flags.bitfield.cpulzcnt
9281 || i.tm.cpu_flags.bitfield.cpumovbe
9282 || i.tm.cpu_flags.bitfield.cpuxsaves
9283 || (x86_feature_2_used
9284 & (GNU_PROPERTY_X86_FEATURE_2_XSAVE
9285 | GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT
9286 | GNU_PROPERTY_X86_FEATURE_2_XSAVEC)) != 0)
9287 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V3;
9288 if (i.tm.cpu_flags.bitfield.cpuavx512f
9289 || i.tm.cpu_flags.bitfield.cpuavx512bw
9290 || i.tm.cpu_flags.bitfield.cpuavx512dq
9291 || i.tm.cpu_flags.bitfield.cpuavx512vl
9292 /* Any EVEX encoded insns except for AVX512ER, AVX512PF and
9293 VNNIW. */
9294 || (i.tm.opcode_modifier.evex
9295 && !i.tm.cpu_flags.bitfield.cpuavx512er
9296 && !i.tm.cpu_flags.bitfield.cpuavx512pf
9297 && !i.tm.cpu_flags.bitfield.cpuavx512_4vnniw))
9298 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V4;
b4a3a7b4
L
9299 }
9300#endif
9301
29b0f896
AM
9302 /* Tie dwarf2 debug info to the address at the start of the insn.
9303 We can't do this after the insn has been output as the current
9304 frag may have been closed off. eg. by frag_var. */
9305 dwarf2_emit_insn (0);
9306
2bbd9c25
JJ
9307 insn_start_frag = frag_now;
9308 insn_start_off = frag_now_fix ();
9309
79d72f45 9310 if (add_branch_padding_frag_p (&branch, &mf_jcc))
e379e5f3
L
9311 {
9312 char *p;
9313 /* Branch can be 8 bytes. Leave some room for prefixes. */
9314 unsigned int max_branch_padding_size = 14;
9315
9316 /* Align section to boundary. */
9317 record_alignment (now_seg, align_branch_power);
9318
9319 /* Make room for padding. */
9320 frag_grow (max_branch_padding_size);
9321
9322 /* Start of the padding. */
9323 p = frag_more (0);
9324
9325 fragP = frag_now;
9326
9327 frag_var (rs_machine_dependent, max_branch_padding_size, 0,
9328 ENCODE_RELAX_STATE (BRANCH_PADDING, 0),
9329 NULL, 0, p);
9330
79d72f45 9331 fragP->tc_frag_data.mf_type = mf_jcc;
e379e5f3
L
9332 fragP->tc_frag_data.branch_type = branch;
9333 fragP->tc_frag_data.max_bytes = max_branch_padding_size;
9334 }
9335
29b0f896 9336 /* Output jumps. */
0cfa3eb3 9337 if (i.tm.opcode_modifier.jump == JUMP)
29b0f896 9338 output_branch ();
0cfa3eb3
JB
9339 else if (i.tm.opcode_modifier.jump == JUMP_BYTE
9340 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896 9341 output_jump ();
0cfa3eb3 9342 else if (i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT)
29b0f896
AM
9343 output_interseg_jump ();
9344 else
9345 {
9346 /* Output normal instructions here. */
9347 char *p;
9348 unsigned char *q;
47465058 9349 unsigned int j;
79d72f45 9350 enum mf_cmp_kind mf_cmp;
4dffcebc 9351
e4e00185 9352 if (avoid_fence
389d00a5
JB
9353 && (i.tm.base_opcode == 0xaee8
9354 || i.tm.base_opcode == 0xaef0
9355 || i.tm.base_opcode == 0xaef8))
48ef937e
JB
9356 {
9357 /* Encode lfence, mfence, and sfence as
9358 f0 83 04 24 00 lock addl $0x0, (%{re}sp). */
9359 if (now_seg != absolute_section)
9360 {
9361 offsetT val = 0x240483f0ULL;
9362
9363 p = frag_more (5);
9364 md_number_to_chars (p, val, 5);
9365 }
9366 else
9367 abs_section_offset += 5;
9368 return;
9369 }
e4e00185 9370
d022bddd
IT
9371 /* Some processors fail on LOCK prefix. This options makes
9372 assembler ignore LOCK prefix and serves as a workaround. */
9373 if (omit_lock_prefix)
9374 {
35648716
JB
9375 if (i.tm.base_opcode == LOCK_PREFIX_OPCODE
9376 && i.tm.opcode_modifier.isprefix)
d022bddd
IT
9377 return;
9378 i.prefix[LOCK_PREFIX] = 0;
9379 }
9380
e379e5f3
L
9381 if (branch)
9382 /* Skip if this is a branch. */
9383 ;
79d72f45 9384 else if (add_fused_jcc_padding_frag_p (&mf_cmp))
e379e5f3
L
9385 {
9386 /* Make room for padding. */
9387 frag_grow (MAX_FUSED_JCC_PADDING_SIZE);
9388 p = frag_more (0);
9389
9390 fragP = frag_now;
9391
9392 frag_var (rs_machine_dependent, MAX_FUSED_JCC_PADDING_SIZE, 0,
9393 ENCODE_RELAX_STATE (FUSED_JCC_PADDING, 0),
9394 NULL, 0, p);
9395
79d72f45 9396 fragP->tc_frag_data.mf_type = mf_cmp;
e379e5f3
L
9397 fragP->tc_frag_data.branch_type = align_branch_fused;
9398 fragP->tc_frag_data.max_bytes = MAX_FUSED_JCC_PADDING_SIZE;
9399 }
9400 else if (add_branch_prefix_frag_p ())
9401 {
9402 unsigned int max_prefix_size = align_branch_prefix_size;
9403
9404 /* Make room for padding. */
9405 frag_grow (max_prefix_size);
9406 p = frag_more (0);
9407
9408 fragP = frag_now;
9409
9410 frag_var (rs_machine_dependent, max_prefix_size, 0,
9411 ENCODE_RELAX_STATE (BRANCH_PREFIX, 0),
9412 NULL, 0, p);
9413
9414 fragP->tc_frag_data.max_bytes = max_prefix_size;
9415 }
9416
43234a1e
L
9417 /* Since the VEX/EVEX prefix contains the implicit prefix, we
9418 don't need the explicit prefix. */
9419 if (!i.tm.opcode_modifier.vex && !i.tm.opcode_modifier.evex)
bc4bd9ab 9420 {
7b47a312 9421 switch (i.tm.opcode_modifier.opcodeprefix)
bc4bd9ab 9422 {
7b47a312
L
9423 case PREFIX_0X66:
9424 add_prefix (0x66);
9425 break;
9426 case PREFIX_0XF2:
9427 add_prefix (0xf2);
9428 break;
9429 case PREFIX_0XF3:
8b65b895
L
9430 if (!i.tm.cpu_flags.bitfield.cpupadlock
9431 || (i.prefix[REP_PREFIX] != 0xf3))
9432 add_prefix (0xf3);
c0f3af97 9433 break;
7b47a312 9434 case PREFIX_NONE:
9a182d04 9435 switch (i.opcode_length)
c0f3af97 9436 {
7b47a312 9437 case 2:
7b47a312 9438 break;
9a182d04 9439 case 1:
7b47a312 9440 /* Check for pseudo prefixes. */
9a182d04
JB
9441 if (!i.tm.opcode_modifier.isprefix || i.tm.base_opcode)
9442 break;
7b47a312
L
9443 as_bad_where (insn_start_frag->fr_file,
9444 insn_start_frag->fr_line,
9445 _("pseudo prefix without instruction"));
9446 return;
9447 default:
9448 abort ();
4dffcebc 9449 }
c0f3af97 9450 break;
c0f3af97
L
9451 default:
9452 abort ();
bc4bd9ab 9453 }
c0f3af97 9454
6d19a37a 9455#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
cf61b747
L
9456 /* For x32, add a dummy REX_OPCODE prefix for mov/add with
9457 R_X86_64_GOTTPOFF relocation so that linker can safely
14470f07
L
9458 perform IE->LE optimization. A dummy REX_OPCODE prefix
9459 is also needed for lea with R_X86_64_GOTPC32_TLSDESC
9460 relocation for GDesc -> IE/LE optimization. */
cf61b747
L
9461 if (x86_elf_abi == X86_64_X32_ABI
9462 && i.operands == 2
14470f07
L
9463 && (i.reloc[0] == BFD_RELOC_X86_64_GOTTPOFF
9464 || i.reloc[0] == BFD_RELOC_X86_64_GOTPC32_TLSDESC)
cf61b747
L
9465 && i.prefix[REX_PREFIX] == 0)
9466 add_prefix (REX_OPCODE);
6d19a37a 9467#endif
cf61b747 9468
c0f3af97
L
9469 /* The prefix bytes. */
9470 for (j = ARRAY_SIZE (i.prefix), q = i.prefix; j > 0; j--, q++)
9471 if (*q)
48ef937e 9472 frag_opcode_byte (*q);
0f10071e 9473 }
ae5c1c7b 9474 else
c0f3af97
L
9475 {
9476 for (j = 0, q = i.prefix; j < ARRAY_SIZE (i.prefix); j++, q++)
9477 if (*q)
9478 switch (j)
9479 {
c0f3af97
L
9480 case SEG_PREFIX:
9481 case ADDR_PREFIX:
48ef937e 9482 frag_opcode_byte (*q);
c0f3af97
L
9483 break;
9484 default:
9485 /* There should be no other prefixes for instructions
9486 with VEX prefix. */
9487 abort ();
9488 }
9489
43234a1e
L
9490 /* For EVEX instructions i.vrex should become 0 after
9491 build_evex_prefix. For VEX instructions upper 16 registers
9492 aren't available, so VREX should be 0. */
9493 if (i.vrex)
9494 abort ();
c0f3af97 9495 /* Now the VEX prefix. */
48ef937e
JB
9496 if (now_seg != absolute_section)
9497 {
9498 p = frag_more (i.vex.length);
9499 for (j = 0; j < i.vex.length; j++)
9500 p[j] = i.vex.bytes[j];
9501 }
9502 else
9503 abs_section_offset += i.vex.length;
c0f3af97 9504 }
252b5132 9505
29b0f896 9506 /* Now the opcode; be careful about word order here! */
389d00a5
JB
9507 j = i.opcode_length;
9508 if (!i.vex.length)
9509 switch (i.tm.opcode_modifier.opcodespace)
9510 {
9511 case SPACE_BASE:
9512 break;
9513 case SPACE_0F:
9514 ++j;
9515 break;
9516 case SPACE_0F38:
9517 case SPACE_0F3A:
9518 j += 2;
9519 break;
9520 default:
9521 abort ();
9522 }
9523
48ef937e 9524 if (now_seg == absolute_section)
389d00a5
JB
9525 abs_section_offset += j;
9526 else if (j == 1)
29b0f896
AM
9527 {
9528 FRAG_APPEND_1_CHAR (i.tm.base_opcode);
9529 }
9530 else
9531 {
389d00a5
JB
9532 p = frag_more (j);
9533 if (!i.vex.length
9534 && i.tm.opcode_modifier.opcodespace != SPACE_BASE)
9535 {
9536 *p++ = 0x0f;
9537 if (i.tm.opcode_modifier.opcodespace != SPACE_0F)
9538 *p++ = i.tm.opcode_modifier.opcodespace == SPACE_0F38
9539 ? 0x38 : 0x3a;
9540 }
9541
9a182d04 9542 switch (i.opcode_length)
331d2d0d 9543 {
4dffcebc 9544 case 2:
389d00a5
JB
9545 /* Put out high byte first: can't use md_number_to_chars! */
9546 *p++ = (i.tm.base_opcode >> 8) & 0xff;
9547 /* Fall through. */
9548 case 1:
9549 *p = i.tm.base_opcode & 0xff;
4dffcebc
L
9550 break;
9551 default:
9552 abort ();
9553 break;
331d2d0d 9554 }
0f10071e 9555
29b0f896 9556 }
3e73aa7c 9557
29b0f896 9558 /* Now the modrm byte and sib byte (if present). */
40fb9820 9559 if (i.tm.opcode_modifier.modrm)
29b0f896 9560 {
48ef937e
JB
9561 frag_opcode_byte ((i.rm.regmem << 0)
9562 | (i.rm.reg << 3)
9563 | (i.rm.mode << 6));
29b0f896
AM
9564 /* If i.rm.regmem == ESP (4)
9565 && i.rm.mode != (Register mode)
9566 && not 16 bit
9567 ==> need second modrm byte. */
9568 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
9569 && i.rm.mode != 3
dc821c5f 9570 && !(i.base_reg && i.base_reg->reg_type.bitfield.word))
48ef937e
JB
9571 frag_opcode_byte ((i.sib.base << 0)
9572 | (i.sib.index << 3)
9573 | (i.sib.scale << 6));
29b0f896 9574 }
3e73aa7c 9575
29b0f896 9576 if (i.disp_operands)
2bbd9c25 9577 output_disp (insn_start_frag, insn_start_off);
3e73aa7c 9578
29b0f896 9579 if (i.imm_operands)
2bbd9c25 9580 output_imm (insn_start_frag, insn_start_off);
9c33702b
JB
9581
9582 /*
9583 * frag_now_fix () returning plain abs_section_offset when we're in the
9584 * absolute section, and abs_section_offset not getting updated as data
9585 * gets added to the frag breaks the logic below.
9586 */
9587 if (now_seg != absolute_section)
9588 {
9589 j = encoding_length (insn_start_frag, insn_start_off, frag_more (0));
9590 if (j > 15)
9591 as_warn (_("instruction length of %u bytes exceeds the limit of 15"),
9592 j);
e379e5f3
L
9593 else if (fragP)
9594 {
9595 /* NB: Don't add prefix with GOTPC relocation since
9596 output_disp() above depends on the fixed encoding
9597 length. Can't add prefix with TLS relocation since
9598 it breaks TLS linker optimization. */
9599 unsigned int max = i.has_gotpc_tls_reloc ? 0 : 15 - j;
9600 /* Prefix count on the current instruction. */
9601 unsigned int count = i.vex.length;
9602 unsigned int k;
9603 for (k = 0; k < ARRAY_SIZE (i.prefix); k++)
9604 /* REX byte is encoded in VEX/EVEX prefix. */
9605 if (i.prefix[k] && (k != REX_PREFIX || !i.vex.length))
9606 count++;
9607
9608 /* Count prefixes for extended opcode maps. */
9609 if (!i.vex.length)
389d00a5 9610 switch (i.tm.opcode_modifier.opcodespace)
e379e5f3 9611 {
389d00a5 9612 case SPACE_BASE:
e379e5f3 9613 break;
389d00a5
JB
9614 case SPACE_0F:
9615 count++;
e379e5f3 9616 break;
389d00a5
JB
9617 case SPACE_0F38:
9618 case SPACE_0F3A:
9619 count += 2;
e379e5f3
L
9620 break;
9621 default:
9622 abort ();
9623 }
9624
9625 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
9626 == BRANCH_PREFIX)
9627 {
9628 /* Set the maximum prefix size in BRANCH_PREFIX
9629 frag. */
9630 if (fragP->tc_frag_data.max_bytes > max)
9631 fragP->tc_frag_data.max_bytes = max;
9632 if (fragP->tc_frag_data.max_bytes > count)
9633 fragP->tc_frag_data.max_bytes -= count;
9634 else
9635 fragP->tc_frag_data.max_bytes = 0;
9636 }
9637 else
9638 {
9639 /* Remember the maximum prefix size in FUSED_JCC_PADDING
9640 frag. */
9641 unsigned int max_prefix_size;
9642 if (align_branch_prefix_size > max)
9643 max_prefix_size = max;
9644 else
9645 max_prefix_size = align_branch_prefix_size;
9646 if (max_prefix_size > count)
9647 fragP->tc_frag_data.max_prefix_length
9648 = max_prefix_size - count;
9649 }
9650
9651 /* Use existing segment prefix if possible. Use CS
9652 segment prefix in 64-bit mode. In 32-bit mode, use SS
9653 segment prefix with ESP/EBP base register and use DS
9654 segment prefix without ESP/EBP base register. */
9655 if (i.prefix[SEG_PREFIX])
9656 fragP->tc_frag_data.default_prefix = i.prefix[SEG_PREFIX];
9657 else if (flag_code == CODE_64BIT)
9658 fragP->tc_frag_data.default_prefix = CS_PREFIX_OPCODE;
9659 else if (i.base_reg
9660 && (i.base_reg->reg_num == 4
9661 || i.base_reg->reg_num == 5))
9662 fragP->tc_frag_data.default_prefix = SS_PREFIX_OPCODE;
9663 else
9664 fragP->tc_frag_data.default_prefix = DS_PREFIX_OPCODE;
9665 }
9c33702b 9666 }
29b0f896 9667 }
252b5132 9668
e379e5f3
L
9669 /* NB: Don't work with COND_JUMP86 without i386. */
9670 if (align_branch_power
9671 && now_seg != absolute_section
9672 && cpu_arch_flags.bitfield.cpui386)
9673 {
9674 /* Terminate each frag so that we can add prefix and check for
9675 fused jcc. */
9676 frag_wane (frag_now);
9677 frag_new (0);
9678 }
9679
29b0f896
AM
9680#ifdef DEBUG386
9681 if (flag_debug)
9682 {
7b81dfbb 9683 pi ("" /*line*/, &i);
29b0f896
AM
9684 }
9685#endif /* DEBUG386 */
9686}
252b5132 9687
e205caa7
L
9688/* Return the size of the displacement operand N. */
9689
9690static int
9691disp_size (unsigned int n)
9692{
9693 int size = 4;
43234a1e 9694
b5014f7a 9695 if (i.types[n].bitfield.disp64)
40fb9820
L
9696 size = 8;
9697 else if (i.types[n].bitfield.disp8)
9698 size = 1;
9699 else if (i.types[n].bitfield.disp16)
9700 size = 2;
e205caa7
L
9701 return size;
9702}
9703
9704/* Return the size of the immediate operand N. */
9705
9706static int
9707imm_size (unsigned int n)
9708{
9709 int size = 4;
40fb9820
L
9710 if (i.types[n].bitfield.imm64)
9711 size = 8;
9712 else if (i.types[n].bitfield.imm8 || i.types[n].bitfield.imm8s)
9713 size = 1;
9714 else if (i.types[n].bitfield.imm16)
9715 size = 2;
e205caa7
L
9716 return size;
9717}
9718
29b0f896 9719static void
64e74474 9720output_disp (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
9721{
9722 char *p;
9723 unsigned int n;
252b5132 9724
29b0f896
AM
9725 for (n = 0; n < i.operands; n++)
9726 {
b5014f7a 9727 if (operand_type_check (i.types[n], disp))
29b0f896 9728 {
48ef937e
JB
9729 int size = disp_size (n);
9730
9731 if (now_seg == absolute_section)
9732 abs_section_offset += size;
9733 else if (i.op[n].disps->X_op == O_constant)
29b0f896 9734 {
43234a1e 9735 offsetT val = i.op[n].disps->X_add_number;
252b5132 9736
629cfaf1
JB
9737 val = offset_in_range (val >> (size == 1 ? i.memshift : 0),
9738 size);
29b0f896
AM
9739 p = frag_more (size);
9740 md_number_to_chars (p, val, size);
9741 }
9742 else
9743 {
f86103b7 9744 enum bfd_reloc_code_real reloc_type;
40fb9820 9745 int sign = i.types[n].bitfield.disp32s;
29b0f896 9746 int pcrel = (i.flags[n] & Operand_PCrel) != 0;
02a86693 9747 fixS *fixP;
29b0f896 9748
e205caa7 9749 /* We can't have 8 bit displacement here. */
9c2799c2 9750 gas_assert (!i.types[n].bitfield.disp8);
e205caa7 9751
29b0f896
AM
9752 /* The PC relative address is computed relative
9753 to the instruction boundary, so in case immediate
9754 fields follows, we need to adjust the value. */
9755 if (pcrel && i.imm_operands)
9756 {
29b0f896 9757 unsigned int n1;
e205caa7 9758 int sz = 0;
252b5132 9759
29b0f896 9760 for (n1 = 0; n1 < i.operands; n1++)
40fb9820 9761 if (operand_type_check (i.types[n1], imm))
252b5132 9762 {
e205caa7
L
9763 /* Only one immediate is allowed for PC
9764 relative address. */
9c2799c2 9765 gas_assert (sz == 0);
e205caa7
L
9766 sz = imm_size (n1);
9767 i.op[n].disps->X_add_number -= sz;
252b5132 9768 }
29b0f896 9769 /* We should find the immediate. */
9c2799c2 9770 gas_assert (sz != 0);
29b0f896 9771 }
520dc8e8 9772
29b0f896 9773 p = frag_more (size);
d258b828 9774 reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
d6ab8113 9775 if (GOT_symbol
2bbd9c25 9776 && GOT_symbol == i.op[n].disps->X_add_symbol
d6ab8113 9777 && (((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
9778 || reloc_type == BFD_RELOC_X86_64_32S
9779 || (reloc_type == BFD_RELOC_64
9780 && object_64bit))
d6ab8113
JB
9781 && (i.op[n].disps->X_op == O_symbol
9782 || (i.op[n].disps->X_op == O_add
9783 && ((symbol_get_value_expression
9784 (i.op[n].disps->X_op_symbol)->X_op)
9785 == O_subtract))))
9786 || reloc_type == BFD_RELOC_32_PCREL))
2bbd9c25 9787 {
4fa24527 9788 if (!object_64bit)
7b81dfbb
AJ
9789 {
9790 reloc_type = BFD_RELOC_386_GOTPC;
e379e5f3 9791 i.has_gotpc_tls_reloc = TRUE;
d583596c
JB
9792 i.op[n].imms->X_add_number +=
9793 encoding_length (insn_start_frag, insn_start_off, p);
7b81dfbb
AJ
9794 }
9795 else if (reloc_type == BFD_RELOC_64)
9796 reloc_type = BFD_RELOC_X86_64_GOTPC64;
d6ab8113 9797 else
7b81dfbb
AJ
9798 /* Don't do the adjustment for x86-64, as there
9799 the pcrel addressing is relative to the _next_
9800 insn, and that is taken care of in other code. */
d6ab8113 9801 reloc_type = BFD_RELOC_X86_64_GOTPC32;
2bbd9c25 9802 }
e379e5f3
L
9803 else if (align_branch_power)
9804 {
9805 switch (reloc_type)
9806 {
9807 case BFD_RELOC_386_TLS_GD:
9808 case BFD_RELOC_386_TLS_LDM:
9809 case BFD_RELOC_386_TLS_IE:
9810 case BFD_RELOC_386_TLS_IE_32:
9811 case BFD_RELOC_386_TLS_GOTIE:
9812 case BFD_RELOC_386_TLS_GOTDESC:
9813 case BFD_RELOC_386_TLS_DESC_CALL:
9814 case BFD_RELOC_X86_64_TLSGD:
9815 case BFD_RELOC_X86_64_TLSLD:
9816 case BFD_RELOC_X86_64_GOTTPOFF:
9817 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
9818 case BFD_RELOC_X86_64_TLSDESC_CALL:
9819 i.has_gotpc_tls_reloc = TRUE;
9820 default:
9821 break;
9822 }
9823 }
02a86693
L
9824 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal,
9825 size, i.op[n].disps, pcrel,
9826 reloc_type);
9827 /* Check for "call/jmp *mem", "mov mem, %reg",
9828 "test %reg, mem" and "binop mem, %reg" where binop
9829 is one of adc, add, and, cmp, or, sbb, sub, xor
e60f4d3b
L
9830 instructions without data prefix. Always generate
9831 R_386_GOT32X for "sym*GOT" operand in 32-bit mode. */
9832 if (i.prefix[DATA_PREFIX] == 0
9833 && (generate_relax_relocations
9834 || (!object_64bit
9835 && i.rm.mode == 0
9836 && i.rm.regmem == 5))
0cb4071e
L
9837 && (i.rm.mode == 2
9838 || (i.rm.mode == 0 && i.rm.regmem == 5))
389d00a5 9839 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
02a86693
L
9840 && ((i.operands == 1
9841 && i.tm.base_opcode == 0xff
9842 && (i.rm.reg == 2 || i.rm.reg == 4))
9843 || (i.operands == 2
9844 && (i.tm.base_opcode == 0x8b
9845 || i.tm.base_opcode == 0x85
2ae4c703 9846 || (i.tm.base_opcode & ~0x38) == 0x03))))
02a86693
L
9847 {
9848 if (object_64bit)
9849 {
9850 fixP->fx_tcbit = i.rex != 0;
9851 if (i.base_reg
e968fc9b 9852 && (i.base_reg->reg_num == RegIP))
02a86693
L
9853 fixP->fx_tcbit2 = 1;
9854 }
9855 else
9856 fixP->fx_tcbit2 = 1;
9857 }
29b0f896
AM
9858 }
9859 }
9860 }
9861}
252b5132 9862
29b0f896 9863static void
64e74474 9864output_imm (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
9865{
9866 char *p;
9867 unsigned int n;
252b5132 9868
29b0f896
AM
9869 for (n = 0; n < i.operands; n++)
9870 {
43234a1e 9871 /* Skip SAE/RC Imm operand in EVEX. They are already handled. */
783c187b 9872 if (i.rounding && n == i.rounding->operand)
43234a1e
L
9873 continue;
9874
40fb9820 9875 if (operand_type_check (i.types[n], imm))
29b0f896 9876 {
48ef937e
JB
9877 int size = imm_size (n);
9878
9879 if (now_seg == absolute_section)
9880 abs_section_offset += size;
9881 else if (i.op[n].imms->X_op == O_constant)
29b0f896 9882 {
29b0f896 9883 offsetT val;
b4cac588 9884
29b0f896
AM
9885 val = offset_in_range (i.op[n].imms->X_add_number,
9886 size);
9887 p = frag_more (size);
9888 md_number_to_chars (p, val, size);
9889 }
9890 else
9891 {
9892 /* Not absolute_section.
9893 Need a 32-bit fixup (don't support 8bit
9894 non-absolute imms). Try to support other
9895 sizes ... */
f86103b7 9896 enum bfd_reloc_code_real reloc_type;
e205caa7 9897 int sign;
29b0f896 9898
40fb9820 9899 if (i.types[n].bitfield.imm32s
a7d61044 9900 && (i.suffix == QWORD_MNEM_SUFFIX
40fb9820 9901 || (!i.suffix && i.tm.opcode_modifier.no_lsuf)))
29b0f896 9902 sign = 1;
e205caa7
L
9903 else
9904 sign = 0;
520dc8e8 9905
29b0f896 9906 p = frag_more (size);
d258b828 9907 reloc_type = reloc (size, 0, sign, i.reloc[n]);
f86103b7 9908
2bbd9c25
JJ
9909 /* This is tough to explain. We end up with this one if we
9910 * have operands that look like
9911 * "_GLOBAL_OFFSET_TABLE_+[.-.L284]". The goal here is to
9912 * obtain the absolute address of the GOT, and it is strongly
9913 * preferable from a performance point of view to avoid using
9914 * a runtime relocation for this. The actual sequence of
9915 * instructions often look something like:
9916 *
9917 * call .L66
9918 * .L66:
9919 * popl %ebx
9920 * addl $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
9921 *
9922 * The call and pop essentially return the absolute address
9923 * of the label .L66 and store it in %ebx. The linker itself
9924 * will ultimately change the first operand of the addl so
9925 * that %ebx points to the GOT, but to keep things simple, the
9926 * .o file must have this operand set so that it generates not
9927 * the absolute address of .L66, but the absolute address of
9928 * itself. This allows the linker itself simply treat a GOTPC
9929 * relocation as asking for a pcrel offset to the GOT to be
9930 * added in, and the addend of the relocation is stored in the
9931 * operand field for the instruction itself.
9932 *
9933 * Our job here is to fix the operand so that it would add
9934 * the correct offset so that %ebx would point to itself. The
9935 * thing that is tricky is that .-.L66 will point to the
9936 * beginning of the instruction, so we need to further modify
9937 * the operand so that it will point to itself. There are
9938 * other cases where you have something like:
9939 *
9940 * .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
9941 *
9942 * and here no correction would be required. Internally in
9943 * the assembler we treat operands of this form as not being
9944 * pcrel since the '.' is explicitly mentioned, and I wonder
9945 * whether it would simplify matters to do it this way. Who
9946 * knows. In earlier versions of the PIC patches, the
9947 * pcrel_adjust field was used to store the correction, but
9948 * since the expression is not pcrel, I felt it would be
9949 * confusing to do it this way. */
9950
d6ab8113 9951 if ((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
9952 || reloc_type == BFD_RELOC_X86_64_32S
9953 || reloc_type == BFD_RELOC_64)
29b0f896
AM
9954 && GOT_symbol
9955 && GOT_symbol == i.op[n].imms->X_add_symbol
9956 && (i.op[n].imms->X_op == O_symbol
9957 || (i.op[n].imms->X_op == O_add
9958 && ((symbol_get_value_expression
9959 (i.op[n].imms->X_op_symbol)->X_op)
9960 == O_subtract))))
9961 {
4fa24527 9962 if (!object_64bit)
d6ab8113 9963 reloc_type = BFD_RELOC_386_GOTPC;
7b81dfbb 9964 else if (size == 4)
d6ab8113 9965 reloc_type = BFD_RELOC_X86_64_GOTPC32;
7b81dfbb
AJ
9966 else if (size == 8)
9967 reloc_type = BFD_RELOC_X86_64_GOTPC64;
e379e5f3 9968 i.has_gotpc_tls_reloc = TRUE;
d583596c
JB
9969 i.op[n].imms->X_add_number +=
9970 encoding_length (insn_start_frag, insn_start_off, p);
29b0f896 9971 }
29b0f896
AM
9972 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
9973 i.op[n].imms, 0, reloc_type);
9974 }
9975 }
9976 }
252b5132
RH
9977}
9978\f
d182319b
JB
9979/* x86_cons_fix_new is called via the expression parsing code when a
9980 reloc is needed. We use this hook to get the correct .got reloc. */
d182319b
JB
9981static int cons_sign = -1;
9982
9983void
e3bb37b5 9984x86_cons_fix_new (fragS *frag, unsigned int off, unsigned int len,
62ebcb5c 9985 expressionS *exp, bfd_reloc_code_real_type r)
d182319b 9986{
d258b828 9987 r = reloc (len, 0, cons_sign, r);
d182319b
JB
9988
9989#ifdef TE_PE
9990 if (exp->X_op == O_secrel)
9991 {
9992 exp->X_op = O_symbol;
9993 r = BFD_RELOC_32_SECREL;
9994 }
9995#endif
9996
9997 fix_new_exp (frag, off, len, exp, 0, r);
9998}
9999
357d1bd8
L
10000/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
10001 purpose of the `.dc.a' internal pseudo-op. */
10002
10003int
10004x86_address_bytes (void)
10005{
10006 if ((stdoutput->arch_info->mach & bfd_mach_x64_32))
10007 return 4;
10008 return stdoutput->arch_info->bits_per_address / 8;
10009}
10010
d382c579
TG
10011#if !(defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined (OBJ_MACH_O)) \
10012 || defined (LEX_AT)
d258b828 10013# define lex_got(reloc, adjust, types) NULL
718ddfc0 10014#else
f3c180ae
AM
10015/* Parse operands of the form
10016 <symbol>@GOTOFF+<nnn>
10017 and similar .plt or .got references.
10018
10019 If we find one, set up the correct relocation in RELOC and copy the
10020 input string, minus the `@GOTOFF' into a malloc'd buffer for
10021 parsing by the calling routine. Return this buffer, and if ADJUST
10022 is non-null set it to the length of the string we removed from the
10023 input line. Otherwise return NULL. */
10024static char *
91d6fa6a 10025lex_got (enum bfd_reloc_code_real *rel,
64e74474 10026 int *adjust,
d258b828 10027 i386_operand_type *types)
f3c180ae 10028{
7b81dfbb
AJ
10029 /* Some of the relocations depend on the size of what field is to
10030 be relocated. But in our callers i386_immediate and i386_displacement
10031 we don't yet know the operand size (this will be set by insn
10032 matching). Hence we record the word32 relocation here,
10033 and adjust the reloc according to the real size in reloc(). */
f3c180ae
AM
10034 static const struct {
10035 const char *str;
cff8d58a 10036 int len;
4fa24527 10037 const enum bfd_reloc_code_real rel[2];
40fb9820 10038 const i386_operand_type types64;
844bf810 10039 bfd_boolean need_GOT_symbol;
f3c180ae 10040 } gotrel[] = {
8ce3d284 10041#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
10042 { STRING_COMMA_LEN ("SIZE"), { BFD_RELOC_SIZE32,
10043 BFD_RELOC_SIZE32 },
844bf810 10044 OPERAND_TYPE_IMM32_64, FALSE },
8ce3d284 10045#endif
cff8d58a
L
10046 { STRING_COMMA_LEN ("PLTOFF"), { _dummy_first_bfd_reloc_code_real,
10047 BFD_RELOC_X86_64_PLTOFF64 },
844bf810 10048 OPERAND_TYPE_IMM64, TRUE },
cff8d58a
L
10049 { STRING_COMMA_LEN ("PLT"), { BFD_RELOC_386_PLT32,
10050 BFD_RELOC_X86_64_PLT32 },
844bf810 10051 OPERAND_TYPE_IMM32_32S_DISP32, FALSE },
cff8d58a
L
10052 { STRING_COMMA_LEN ("GOTPLT"), { _dummy_first_bfd_reloc_code_real,
10053 BFD_RELOC_X86_64_GOTPLT64 },
844bf810 10054 OPERAND_TYPE_IMM64_DISP64, TRUE },
cff8d58a
L
10055 { STRING_COMMA_LEN ("GOTOFF"), { BFD_RELOC_386_GOTOFF,
10056 BFD_RELOC_X86_64_GOTOFF64 },
844bf810 10057 OPERAND_TYPE_IMM64_DISP64, TRUE },
cff8d58a
L
10058 { STRING_COMMA_LEN ("GOTPCREL"), { _dummy_first_bfd_reloc_code_real,
10059 BFD_RELOC_X86_64_GOTPCREL },
844bf810 10060 OPERAND_TYPE_IMM32_32S_DISP32, TRUE },
cff8d58a
L
10061 { STRING_COMMA_LEN ("TLSGD"), { BFD_RELOC_386_TLS_GD,
10062 BFD_RELOC_X86_64_TLSGD },
844bf810 10063 OPERAND_TYPE_IMM32_32S_DISP32, TRUE },
cff8d58a
L
10064 { STRING_COMMA_LEN ("TLSLDM"), { BFD_RELOC_386_TLS_LDM,
10065 _dummy_first_bfd_reloc_code_real },
844bf810 10066 OPERAND_TYPE_NONE, TRUE },
cff8d58a
L
10067 { STRING_COMMA_LEN ("TLSLD"), { _dummy_first_bfd_reloc_code_real,
10068 BFD_RELOC_X86_64_TLSLD },
844bf810 10069 OPERAND_TYPE_IMM32_32S_DISP32, TRUE },
cff8d58a
L
10070 { STRING_COMMA_LEN ("GOTTPOFF"), { BFD_RELOC_386_TLS_IE_32,
10071 BFD_RELOC_X86_64_GOTTPOFF },
844bf810 10072 OPERAND_TYPE_IMM32_32S_DISP32, TRUE },
cff8d58a
L
10073 { STRING_COMMA_LEN ("TPOFF"), { BFD_RELOC_386_TLS_LE_32,
10074 BFD_RELOC_X86_64_TPOFF32 },
844bf810 10075 OPERAND_TYPE_IMM32_32S_64_DISP32_64, TRUE },
cff8d58a
L
10076 { STRING_COMMA_LEN ("NTPOFF"), { BFD_RELOC_386_TLS_LE,
10077 _dummy_first_bfd_reloc_code_real },
844bf810 10078 OPERAND_TYPE_NONE, TRUE },
cff8d58a
L
10079 { STRING_COMMA_LEN ("DTPOFF"), { BFD_RELOC_386_TLS_LDO_32,
10080 BFD_RELOC_X86_64_DTPOFF32 },
844bf810 10081 OPERAND_TYPE_IMM32_32S_64_DISP32_64, TRUE },
cff8d58a
L
10082 { STRING_COMMA_LEN ("GOTNTPOFF"),{ BFD_RELOC_386_TLS_GOTIE,
10083 _dummy_first_bfd_reloc_code_real },
844bf810 10084 OPERAND_TYPE_NONE, TRUE },
cff8d58a
L
10085 { STRING_COMMA_LEN ("INDNTPOFF"),{ BFD_RELOC_386_TLS_IE,
10086 _dummy_first_bfd_reloc_code_real },
844bf810 10087 OPERAND_TYPE_NONE, TRUE },
cff8d58a
L
10088 { STRING_COMMA_LEN ("GOT"), { BFD_RELOC_386_GOT32,
10089 BFD_RELOC_X86_64_GOT32 },
844bf810 10090 OPERAND_TYPE_IMM32_32S_64_DISP32, TRUE },
cff8d58a
L
10091 { STRING_COMMA_LEN ("TLSDESC"), { BFD_RELOC_386_TLS_GOTDESC,
10092 BFD_RELOC_X86_64_GOTPC32_TLSDESC },
844bf810 10093 OPERAND_TYPE_IMM32_32S_DISP32, TRUE },
cff8d58a
L
10094 { STRING_COMMA_LEN ("TLSCALL"), { BFD_RELOC_386_TLS_DESC_CALL,
10095 BFD_RELOC_X86_64_TLSDESC_CALL },
844bf810 10096 OPERAND_TYPE_IMM32_32S_DISP32, TRUE },
f3c180ae
AM
10097 };
10098 char *cp;
10099 unsigned int j;
10100
d382c579 10101#if defined (OBJ_MAYBE_ELF)
718ddfc0
JB
10102 if (!IS_ELF)
10103 return NULL;
d382c579 10104#endif
718ddfc0 10105
f3c180ae 10106 for (cp = input_line_pointer; *cp != '@'; cp++)
67c11a9b 10107 if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
f3c180ae
AM
10108 return NULL;
10109
47465058 10110 for (j = 0; j < ARRAY_SIZE (gotrel); j++)
f3c180ae 10111 {
cff8d58a 10112 int len = gotrel[j].len;
28f81592 10113 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
f3c180ae 10114 {
4fa24527 10115 if (gotrel[j].rel[object_64bit] != 0)
f3c180ae 10116 {
28f81592
AM
10117 int first, second;
10118 char *tmpbuf, *past_reloc;
f3c180ae 10119
91d6fa6a 10120 *rel = gotrel[j].rel[object_64bit];
f3c180ae 10121
3956db08
JB
10122 if (types)
10123 {
10124 if (flag_code != CODE_64BIT)
40fb9820
L
10125 {
10126 types->bitfield.imm32 = 1;
10127 types->bitfield.disp32 = 1;
10128 }
3956db08
JB
10129 else
10130 *types = gotrel[j].types64;
10131 }
10132
844bf810 10133 if (gotrel[j].need_GOT_symbol && GOT_symbol == NULL)
f3c180ae
AM
10134 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
10135
28f81592 10136 /* The length of the first part of our input line. */
f3c180ae 10137 first = cp - input_line_pointer;
28f81592
AM
10138
10139 /* The second part goes from after the reloc token until
67c11a9b 10140 (and including) an end_of_line char or comma. */
28f81592 10141 past_reloc = cp + 1 + len;
67c11a9b
AM
10142 cp = past_reloc;
10143 while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
10144 ++cp;
10145 second = cp + 1 - past_reloc;
28f81592
AM
10146
10147 /* Allocate and copy string. The trailing NUL shouldn't
10148 be necessary, but be safe. */
add39d23 10149 tmpbuf = XNEWVEC (char, first + second + 2);
f3c180ae 10150 memcpy (tmpbuf, input_line_pointer, first);
0787a12d
AM
10151 if (second != 0 && *past_reloc != ' ')
10152 /* Replace the relocation token with ' ', so that
10153 errors like foo@GOTOFF1 will be detected. */
10154 tmpbuf[first++] = ' ';
af89796a
L
10155 else
10156 /* Increment length by 1 if the relocation token is
10157 removed. */
10158 len++;
10159 if (adjust)
10160 *adjust = len;
0787a12d
AM
10161 memcpy (tmpbuf + first, past_reloc, second);
10162 tmpbuf[first + second] = '\0';
f3c180ae
AM
10163 return tmpbuf;
10164 }
10165
4fa24527
JB
10166 as_bad (_("@%s reloc is not supported with %d-bit output format"),
10167 gotrel[j].str, 1 << (5 + object_64bit));
f3c180ae
AM
10168 return NULL;
10169 }
10170 }
10171
10172 /* Might be a symbol version string. Don't as_bad here. */
10173 return NULL;
10174}
4e4f7c87 10175#endif
f3c180ae 10176
a988325c
NC
10177#ifdef TE_PE
10178#ifdef lex_got
10179#undef lex_got
10180#endif
10181/* Parse operands of the form
10182 <symbol>@SECREL32+<nnn>
10183
10184 If we find one, set up the correct relocation in RELOC and copy the
10185 input string, minus the `@SECREL32' into a malloc'd buffer for
10186 parsing by the calling routine. Return this buffer, and if ADJUST
10187 is non-null set it to the length of the string we removed from the
34bca508
L
10188 input line. Otherwise return NULL.
10189
a988325c
NC
10190 This function is copied from the ELF version above adjusted for PE targets. */
10191
10192static char *
10193lex_got (enum bfd_reloc_code_real *rel ATTRIBUTE_UNUSED,
10194 int *adjust ATTRIBUTE_UNUSED,
d258b828 10195 i386_operand_type *types)
a988325c
NC
10196{
10197 static const struct
10198 {
10199 const char *str;
10200 int len;
10201 const enum bfd_reloc_code_real rel[2];
10202 const i386_operand_type types64;
10203 }
10204 gotrel[] =
10205 {
10206 { STRING_COMMA_LEN ("SECREL32"), { BFD_RELOC_32_SECREL,
10207 BFD_RELOC_32_SECREL },
10208 OPERAND_TYPE_IMM32_32S_64_DISP32_64 },
10209 };
10210
10211 char *cp;
10212 unsigned j;
10213
10214 for (cp = input_line_pointer; *cp != '@'; cp++)
10215 if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
10216 return NULL;
10217
10218 for (j = 0; j < ARRAY_SIZE (gotrel); j++)
10219 {
10220 int len = gotrel[j].len;
10221
10222 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
10223 {
10224 if (gotrel[j].rel[object_64bit] != 0)
10225 {
10226 int first, second;
10227 char *tmpbuf, *past_reloc;
10228
10229 *rel = gotrel[j].rel[object_64bit];
10230 if (adjust)
10231 *adjust = len;
10232
10233 if (types)
10234 {
10235 if (flag_code != CODE_64BIT)
10236 {
10237 types->bitfield.imm32 = 1;
10238 types->bitfield.disp32 = 1;
10239 }
10240 else
10241 *types = gotrel[j].types64;
10242 }
10243
10244 /* The length of the first part of our input line. */
10245 first = cp - input_line_pointer;
10246
10247 /* The second part goes from after the reloc token until
10248 (and including) an end_of_line char or comma. */
10249 past_reloc = cp + 1 + len;
10250 cp = past_reloc;
10251 while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
10252 ++cp;
10253 second = cp + 1 - past_reloc;
10254
10255 /* Allocate and copy string. The trailing NUL shouldn't
10256 be necessary, but be safe. */
add39d23 10257 tmpbuf = XNEWVEC (char, first + second + 2);
a988325c
NC
10258 memcpy (tmpbuf, input_line_pointer, first);
10259 if (second != 0 && *past_reloc != ' ')
10260 /* Replace the relocation token with ' ', so that
10261 errors like foo@SECLREL321 will be detected. */
10262 tmpbuf[first++] = ' ';
10263 memcpy (tmpbuf + first, past_reloc, second);
10264 tmpbuf[first + second] = '\0';
10265 return tmpbuf;
10266 }
10267
10268 as_bad (_("@%s reloc is not supported with %d-bit output format"),
10269 gotrel[j].str, 1 << (5 + object_64bit));
10270 return NULL;
10271 }
10272 }
10273
10274 /* Might be a symbol version string. Don't as_bad here. */
10275 return NULL;
10276}
10277
10278#endif /* TE_PE */
10279
62ebcb5c 10280bfd_reloc_code_real_type
e3bb37b5 10281x86_cons (expressionS *exp, int size)
f3c180ae 10282{
62ebcb5c
AM
10283 bfd_reloc_code_real_type got_reloc = NO_RELOC;
10284
ee86248c
JB
10285 intel_syntax = -intel_syntax;
10286
3c7b9c2c 10287 exp->X_md = 0;
4fa24527 10288 if (size == 4 || (object_64bit && size == 8))
f3c180ae
AM
10289 {
10290 /* Handle @GOTOFF and the like in an expression. */
10291 char *save;
10292 char *gotfree_input_line;
4a57f2cf 10293 int adjust = 0;
f3c180ae
AM
10294
10295 save = input_line_pointer;
d258b828 10296 gotfree_input_line = lex_got (&got_reloc, &adjust, NULL);
f3c180ae
AM
10297 if (gotfree_input_line)
10298 input_line_pointer = gotfree_input_line;
10299
10300 expression (exp);
10301
10302 if (gotfree_input_line)
10303 {
10304 /* expression () has merrily parsed up to the end of line,
10305 or a comma - in the wrong buffer. Transfer how far
10306 input_line_pointer has moved to the right buffer. */
10307 input_line_pointer = (save
10308 + (input_line_pointer - gotfree_input_line)
10309 + adjust);
10310 free (gotfree_input_line);
3992d3b7
AM
10311 if (exp->X_op == O_constant
10312 || exp->X_op == O_absent
10313 || exp->X_op == O_illegal
0398aac5 10314 || exp->X_op == O_register
3992d3b7
AM
10315 || exp->X_op == O_big)
10316 {
10317 char c = *input_line_pointer;
10318 *input_line_pointer = 0;
10319 as_bad (_("missing or invalid expression `%s'"), save);
10320 *input_line_pointer = c;
10321 }
b9519cfe
L
10322 else if ((got_reloc == BFD_RELOC_386_PLT32
10323 || got_reloc == BFD_RELOC_X86_64_PLT32)
10324 && exp->X_op != O_symbol)
10325 {
10326 char c = *input_line_pointer;
10327 *input_line_pointer = 0;
10328 as_bad (_("invalid PLT expression `%s'"), save);
10329 *input_line_pointer = c;
10330 }
f3c180ae
AM
10331 }
10332 }
10333 else
10334 expression (exp);
ee86248c
JB
10335
10336 intel_syntax = -intel_syntax;
10337
10338 if (intel_syntax)
10339 i386_intel_simplify (exp);
62ebcb5c
AM
10340
10341 return got_reloc;
f3c180ae 10342}
f3c180ae 10343
9f32dd5b
L
10344static void
10345signed_cons (int size)
6482c264 10346{
d182319b
JB
10347 if (flag_code == CODE_64BIT)
10348 cons_sign = 1;
10349 cons (size);
10350 cons_sign = -1;
6482c264
NC
10351}
10352
d182319b 10353#ifdef TE_PE
6482c264 10354static void
7016a5d5 10355pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
6482c264
NC
10356{
10357 expressionS exp;
10358
10359 do
10360 {
10361 expression (&exp);
10362 if (exp.X_op == O_symbol)
10363 exp.X_op = O_secrel;
10364
10365 emit_expr (&exp, 4);
10366 }
10367 while (*input_line_pointer++ == ',');
10368
10369 input_line_pointer--;
10370 demand_empty_rest_of_line ();
10371}
6482c264
NC
10372#endif
10373
43234a1e
L
10374/* Handle Vector operations. */
10375
10376static char *
10377check_VecOperations (char *op_string, char *op_end)
10378{
10379 const reg_entry *mask;
10380 const char *saved;
10381 char *end_op;
10382
10383 while (*op_string
10384 && (op_end == NULL || op_string < op_end))
10385 {
10386 saved = op_string;
10387 if (*op_string == '{')
10388 {
10389 op_string++;
10390
10391 /* Check broadcasts. */
10392 if (strncmp (op_string, "1to", 3) == 0)
10393 {
10394 int bcst_type;
10395
10396 if (i.broadcast)
10397 goto duplicated_vec_op;
10398
10399 op_string += 3;
10400 if (*op_string == '8')
8e6e0792 10401 bcst_type = 8;
b28d1bda 10402 else if (*op_string == '4')
8e6e0792 10403 bcst_type = 4;
b28d1bda 10404 else if (*op_string == '2')
8e6e0792 10405 bcst_type = 2;
43234a1e
L
10406 else if (*op_string == '1'
10407 && *(op_string+1) == '6')
10408 {
8e6e0792 10409 bcst_type = 16;
43234a1e
L
10410 op_string++;
10411 }
10412 else
10413 {
10414 as_bad (_("Unsupported broadcast: `%s'"), saved);
10415 return NULL;
10416 }
10417 op_string++;
10418
10419 broadcast_op.type = bcst_type;
10420 broadcast_op.operand = this_operand;
1f75763a 10421 broadcast_op.bytes = 0;
43234a1e
L
10422 i.broadcast = &broadcast_op;
10423 }
10424 /* Check masking operation. */
10425 else if ((mask = parse_register (op_string, &end_op)) != NULL)
10426 {
8a6fb3f9
JB
10427 if (mask == &bad_reg)
10428 return NULL;
10429
43234a1e 10430 /* k0 can't be used for write mask. */
f74a6307 10431 if (mask->reg_type.bitfield.class != RegMask || !mask->reg_num)
43234a1e 10432 {
6d2cd6b2
JB
10433 as_bad (_("`%s%s' can't be used for write mask"),
10434 register_prefix, mask->reg_name);
43234a1e
L
10435 return NULL;
10436 }
10437
6225c532 10438 if (!i.mask.reg)
43234a1e 10439 {
6225c532
JB
10440 i.mask.reg = mask;
10441 i.mask.operand = this_operand;
43234a1e 10442 }
6225c532
JB
10443 else if (i.mask.reg->reg_num)
10444 goto duplicated_vec_op;
43234a1e
L
10445 else
10446 {
6225c532 10447 i.mask.reg = mask;
43234a1e
L
10448
10449 /* Only "{z}" is allowed here. No need to check
10450 zeroing mask explicitly. */
6225c532 10451 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
10452 {
10453 as_bad (_("invalid write mask `%s'"), saved);
10454 return NULL;
10455 }
10456 }
10457
10458 op_string = end_op;
10459 }
10460 /* Check zeroing-flag for masking operation. */
10461 else if (*op_string == 'z')
10462 {
6225c532 10463 if (!i.mask.reg)
43234a1e 10464 {
6225c532
JB
10465 i.mask.reg = reg_k0;
10466 i.mask.zeroing = 1;
10467 i.mask.operand = this_operand;
43234a1e
L
10468 }
10469 else
10470 {
6225c532 10471 if (i.mask.zeroing)
43234a1e
L
10472 {
10473 duplicated_vec_op:
10474 as_bad (_("duplicated `%s'"), saved);
10475 return NULL;
10476 }
10477
6225c532 10478 i.mask.zeroing = 1;
43234a1e
L
10479
10480 /* Only "{%k}" is allowed here. No need to check mask
10481 register explicitly. */
6225c532 10482 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
10483 {
10484 as_bad (_("invalid zeroing-masking `%s'"),
10485 saved);
10486 return NULL;
10487 }
10488 }
10489
10490 op_string++;
10491 }
10492 else
10493 goto unknown_vec_op;
10494
10495 if (*op_string != '}')
10496 {
10497 as_bad (_("missing `}' in `%s'"), saved);
10498 return NULL;
10499 }
10500 op_string++;
0ba3a731
L
10501
10502 /* Strip whitespace since the addition of pseudo prefixes
10503 changed how the scrubber treats '{'. */
10504 if (is_space_char (*op_string))
10505 ++op_string;
10506
43234a1e
L
10507 continue;
10508 }
10509 unknown_vec_op:
10510 /* We don't know this one. */
10511 as_bad (_("unknown vector operation: `%s'"), saved);
10512 return NULL;
10513 }
10514
6225c532 10515 if (i.mask.reg && i.mask.zeroing && !i.mask.reg->reg_num)
6d2cd6b2
JB
10516 {
10517 as_bad (_("zeroing-masking only allowed with write mask"));
10518 return NULL;
10519 }
10520
43234a1e
L
10521 return op_string;
10522}
10523
252b5132 10524static int
70e41ade 10525i386_immediate (char *imm_start)
252b5132
RH
10526{
10527 char *save_input_line_pointer;
f3c180ae 10528 char *gotfree_input_line;
252b5132 10529 segT exp_seg = 0;
47926f60 10530 expressionS *exp;
40fb9820
L
10531 i386_operand_type types;
10532
0dfbf9d7 10533 operand_type_set (&types, ~0);
252b5132
RH
10534
10535 if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
10536 {
31b2323c
L
10537 as_bad (_("at most %d immediate operands are allowed"),
10538 MAX_IMMEDIATE_OPERANDS);
252b5132
RH
10539 return 0;
10540 }
10541
10542 exp = &im_expressions[i.imm_operands++];
520dc8e8 10543 i.op[this_operand].imms = exp;
252b5132
RH
10544
10545 if (is_space_char (*imm_start))
10546 ++imm_start;
10547
10548 save_input_line_pointer = input_line_pointer;
10549 input_line_pointer = imm_start;
10550
d258b828 10551 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
10552 if (gotfree_input_line)
10553 input_line_pointer = gotfree_input_line;
252b5132
RH
10554
10555 exp_seg = expression (exp);
10556
83183c0c 10557 SKIP_WHITESPACE ();
43234a1e
L
10558
10559 /* Handle vector operations. */
10560 if (*input_line_pointer == '{')
10561 {
10562 input_line_pointer = check_VecOperations (input_line_pointer,
10563 NULL);
10564 if (input_line_pointer == NULL)
10565 return 0;
10566 }
10567
252b5132 10568 if (*input_line_pointer)
f3c180ae 10569 as_bad (_("junk `%s' after expression"), input_line_pointer);
252b5132
RH
10570
10571 input_line_pointer = save_input_line_pointer;
f3c180ae 10572 if (gotfree_input_line)
ee86248c
JB
10573 {
10574 free (gotfree_input_line);
10575
10576 if (exp->X_op == O_constant || exp->X_op == O_register)
10577 exp->X_op = O_illegal;
10578 }
10579
10580 return i386_finalize_immediate (exp_seg, exp, types, imm_start);
10581}
252b5132 10582
ee86248c
JB
10583static int
10584i386_finalize_immediate (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
10585 i386_operand_type types, const char *imm_start)
10586{
10587 if (exp->X_op == O_absent || exp->X_op == O_illegal || exp->X_op == O_big)
252b5132 10588 {
313c53d1
L
10589 if (imm_start)
10590 as_bad (_("missing or invalid immediate expression `%s'"),
10591 imm_start);
3992d3b7 10592 return 0;
252b5132 10593 }
3e73aa7c 10594 else if (exp->X_op == O_constant)
252b5132 10595 {
47926f60 10596 /* Size it properly later. */
40fb9820 10597 i.types[this_operand].bitfield.imm64 = 1;
13f864ae
L
10598 /* If not 64bit, sign extend val. */
10599 if (flag_code != CODE_64BIT
4eed87de
AM
10600 && (exp->X_add_number & ~(((addressT) 2 << 31) - 1)) == 0)
10601 exp->X_add_number
10602 = (exp->X_add_number ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
252b5132 10603 }
4c63da97 10604#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
f86103b7 10605 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
31312f95 10606 && exp_seg != absolute_section
47926f60 10607 && exp_seg != text_section
24eab124
AM
10608 && exp_seg != data_section
10609 && exp_seg != bss_section
10610 && exp_seg != undefined_section
f86103b7 10611 && !bfd_is_com_section (exp_seg))
252b5132 10612 {
d0b47220 10613 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
252b5132
RH
10614 return 0;
10615 }
10616#endif
a841bdf5 10617 else if (!intel_syntax && exp_seg == reg_section)
bb8f5920 10618 {
313c53d1
L
10619 if (imm_start)
10620 as_bad (_("illegal immediate register operand %s"), imm_start);
bb8f5920
L
10621 return 0;
10622 }
252b5132
RH
10623 else
10624 {
10625 /* This is an address. The size of the address will be
24eab124 10626 determined later, depending on destination register,
3e73aa7c 10627 suffix, or the default for the section. */
40fb9820
L
10628 i.types[this_operand].bitfield.imm8 = 1;
10629 i.types[this_operand].bitfield.imm16 = 1;
10630 i.types[this_operand].bitfield.imm32 = 1;
10631 i.types[this_operand].bitfield.imm32s = 1;
10632 i.types[this_operand].bitfield.imm64 = 1;
c6fb90c8
L
10633 i.types[this_operand] = operand_type_and (i.types[this_operand],
10634 types);
252b5132
RH
10635 }
10636
10637 return 1;
10638}
10639
551c1ca1 10640static char *
e3bb37b5 10641i386_scale (char *scale)
252b5132 10642{
551c1ca1
AM
10643 offsetT val;
10644 char *save = input_line_pointer;
252b5132 10645
551c1ca1
AM
10646 input_line_pointer = scale;
10647 val = get_absolute_expression ();
10648
10649 switch (val)
252b5132 10650 {
551c1ca1 10651 case 1:
252b5132
RH
10652 i.log2_scale_factor = 0;
10653 break;
551c1ca1 10654 case 2:
252b5132
RH
10655 i.log2_scale_factor = 1;
10656 break;
551c1ca1 10657 case 4:
252b5132
RH
10658 i.log2_scale_factor = 2;
10659 break;
551c1ca1 10660 case 8:
252b5132
RH
10661 i.log2_scale_factor = 3;
10662 break;
10663 default:
a724f0f4
JB
10664 {
10665 char sep = *input_line_pointer;
10666
10667 *input_line_pointer = '\0';
10668 as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
10669 scale);
10670 *input_line_pointer = sep;
10671 input_line_pointer = save;
10672 return NULL;
10673 }
252b5132 10674 }
29b0f896 10675 if (i.log2_scale_factor != 0 && i.index_reg == 0)
252b5132
RH
10676 {
10677 as_warn (_("scale factor of %d without an index register"),
24eab124 10678 1 << i.log2_scale_factor);
252b5132 10679 i.log2_scale_factor = 0;
252b5132 10680 }
551c1ca1
AM
10681 scale = input_line_pointer;
10682 input_line_pointer = save;
10683 return scale;
252b5132
RH
10684}
10685
252b5132 10686static int
e3bb37b5 10687i386_displacement (char *disp_start, char *disp_end)
252b5132 10688{
29b0f896 10689 expressionS *exp;
252b5132
RH
10690 segT exp_seg = 0;
10691 char *save_input_line_pointer;
f3c180ae 10692 char *gotfree_input_line;
40fb9820
L
10693 int override;
10694 i386_operand_type bigdisp, types = anydisp;
3992d3b7 10695 int ret;
252b5132 10696
31b2323c
L
10697 if (i.disp_operands == MAX_MEMORY_OPERANDS)
10698 {
10699 as_bad (_("at most %d displacement operands are allowed"),
10700 MAX_MEMORY_OPERANDS);
10701 return 0;
10702 }
10703
0dfbf9d7 10704 operand_type_set (&bigdisp, 0);
6f2f06be 10705 if (i.jumpabsolute
48bcea9f 10706 || i.types[this_operand].bitfield.baseindex
0cfa3eb3
JB
10707 || (current_templates->start->opcode_modifier.jump != JUMP
10708 && current_templates->start->opcode_modifier.jump != JUMP_DWORD))
e05278af 10709 {
48bcea9f 10710 i386_addressing_mode ();
e05278af 10711 override = (i.prefix[ADDR_PREFIX] != 0);
40fb9820
L
10712 if (flag_code == CODE_64BIT)
10713 {
10714 if (!override)
10715 {
10716 bigdisp.bitfield.disp32s = 1;
10717 bigdisp.bitfield.disp64 = 1;
10718 }
48bcea9f
JB
10719 else
10720 bigdisp.bitfield.disp32 = 1;
40fb9820
L
10721 }
10722 else if ((flag_code == CODE_16BIT) ^ override)
40fb9820 10723 bigdisp.bitfield.disp16 = 1;
48bcea9f
JB
10724 else
10725 bigdisp.bitfield.disp32 = 1;
e05278af
JB
10726 }
10727 else
10728 {
376cd056
JB
10729 /* For PC-relative branches, the width of the displacement may be
10730 dependent upon data size, but is never dependent upon address size.
10731 Also make sure to not unintentionally match against a non-PC-relative
10732 branch template. */
10733 static templates aux_templates;
10734 const insn_template *t = current_templates->start;
10735 bfd_boolean has_intel64 = FALSE;
10736
10737 aux_templates.start = t;
10738 while (++t < current_templates->end)
10739 {
10740 if (t->opcode_modifier.jump
10741 != current_templates->start->opcode_modifier.jump)
10742 break;
4b5aaf5f 10743 if ((t->opcode_modifier.isa64 >= INTEL64))
376cd056
JB
10744 has_intel64 = TRUE;
10745 }
10746 if (t < current_templates->end)
10747 {
10748 aux_templates.end = t;
10749 current_templates = &aux_templates;
10750 }
10751
e05278af 10752 override = (i.prefix[DATA_PREFIX] != 0);
40fb9820
L
10753 if (flag_code == CODE_64BIT)
10754 {
376cd056
JB
10755 if ((override || i.suffix == WORD_MNEM_SUFFIX)
10756 && (!intel64 || !has_intel64))
40fb9820
L
10757 bigdisp.bitfield.disp16 = 1;
10758 else
48bcea9f 10759 bigdisp.bitfield.disp32s = 1;
40fb9820
L
10760 }
10761 else
e05278af
JB
10762 {
10763 if (!override)
10764 override = (i.suffix == (flag_code != CODE_16BIT
10765 ? WORD_MNEM_SUFFIX
10766 : LONG_MNEM_SUFFIX));
40fb9820
L
10767 bigdisp.bitfield.disp32 = 1;
10768 if ((flag_code == CODE_16BIT) ^ override)
10769 {
10770 bigdisp.bitfield.disp32 = 0;
10771 bigdisp.bitfield.disp16 = 1;
10772 }
e05278af 10773 }
e05278af 10774 }
c6fb90c8
L
10775 i.types[this_operand] = operand_type_or (i.types[this_operand],
10776 bigdisp);
252b5132
RH
10777
10778 exp = &disp_expressions[i.disp_operands];
520dc8e8 10779 i.op[this_operand].disps = exp;
252b5132
RH
10780 i.disp_operands++;
10781 save_input_line_pointer = input_line_pointer;
10782 input_line_pointer = disp_start;
10783 END_STRING_AND_SAVE (disp_end);
10784
10785#ifndef GCC_ASM_O_HACK
10786#define GCC_ASM_O_HACK 0
10787#endif
10788#if GCC_ASM_O_HACK
10789 END_STRING_AND_SAVE (disp_end + 1);
40fb9820 10790 if (i.types[this_operand].bitfield.baseIndex
24eab124 10791 && displacement_string_end[-1] == '+')
252b5132
RH
10792 {
10793 /* This hack is to avoid a warning when using the "o"
24eab124
AM
10794 constraint within gcc asm statements.
10795 For instance:
10796
10797 #define _set_tssldt_desc(n,addr,limit,type) \
10798 __asm__ __volatile__ ( \
10799 "movw %w2,%0\n\t" \
10800 "movw %w1,2+%0\n\t" \
10801 "rorl $16,%1\n\t" \
10802 "movb %b1,4+%0\n\t" \
10803 "movb %4,5+%0\n\t" \
10804 "movb $0,6+%0\n\t" \
10805 "movb %h1,7+%0\n\t" \
10806 "rorl $16,%1" \
10807 : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
10808
10809 This works great except that the output assembler ends
10810 up looking a bit weird if it turns out that there is
10811 no offset. You end up producing code that looks like:
10812
10813 #APP
10814 movw $235,(%eax)
10815 movw %dx,2+(%eax)
10816 rorl $16,%edx
10817 movb %dl,4+(%eax)
10818 movb $137,5+(%eax)
10819 movb $0,6+(%eax)
10820 movb %dh,7+(%eax)
10821 rorl $16,%edx
10822 #NO_APP
10823
47926f60 10824 So here we provide the missing zero. */
24eab124
AM
10825
10826 *displacement_string_end = '0';
252b5132
RH
10827 }
10828#endif
d258b828 10829 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
10830 if (gotfree_input_line)
10831 input_line_pointer = gotfree_input_line;
252b5132 10832
24eab124 10833 exp_seg = expression (exp);
252b5132 10834
636c26b0
AM
10835 SKIP_WHITESPACE ();
10836 if (*input_line_pointer)
10837 as_bad (_("junk `%s' after expression"), input_line_pointer);
10838#if GCC_ASM_O_HACK
10839 RESTORE_END_STRING (disp_end + 1);
10840#endif
636c26b0 10841 input_line_pointer = save_input_line_pointer;
636c26b0 10842 if (gotfree_input_line)
ee86248c
JB
10843 {
10844 free (gotfree_input_line);
10845
10846 if (exp->X_op == O_constant || exp->X_op == O_register)
10847 exp->X_op = O_illegal;
10848 }
10849
10850 ret = i386_finalize_displacement (exp_seg, exp, types, disp_start);
10851
10852 RESTORE_END_STRING (disp_end);
10853
10854 return ret;
10855}
10856
10857static int
10858i386_finalize_displacement (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
10859 i386_operand_type types, const char *disp_start)
10860{
10861 i386_operand_type bigdisp;
10862 int ret = 1;
636c26b0 10863
24eab124
AM
10864 /* We do this to make sure that the section symbol is in
10865 the symbol table. We will ultimately change the relocation
47926f60 10866 to be relative to the beginning of the section. */
1ae12ab7 10867 if (i.reloc[this_operand] == BFD_RELOC_386_GOTOFF
d6ab8113
JB
10868 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
10869 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
24eab124 10870 {
636c26b0 10871 if (exp->X_op != O_symbol)
3992d3b7 10872 goto inv_disp;
636c26b0 10873
e5cb08ac 10874 if (S_IS_LOCAL (exp->X_add_symbol)
c64efb4b
L
10875 && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section
10876 && S_GET_SEGMENT (exp->X_add_symbol) != expr_section)
24eab124 10877 section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
24eab124
AM
10878 exp->X_op = O_subtract;
10879 exp->X_op_symbol = GOT_symbol;
1ae12ab7 10880 if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
29b0f896 10881 i.reloc[this_operand] = BFD_RELOC_32_PCREL;
d6ab8113
JB
10882 else if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
10883 i.reloc[this_operand] = BFD_RELOC_64;
23df1078 10884 else
29b0f896 10885 i.reloc[this_operand] = BFD_RELOC_32;
24eab124 10886 }
252b5132 10887
3992d3b7
AM
10888 else if (exp->X_op == O_absent
10889 || exp->X_op == O_illegal
ee86248c 10890 || exp->X_op == O_big)
2daf4fd8 10891 {
3992d3b7
AM
10892 inv_disp:
10893 as_bad (_("missing or invalid displacement expression `%s'"),
2daf4fd8 10894 disp_start);
3992d3b7 10895 ret = 0;
2daf4fd8
AM
10896 }
10897
0e1147d9
L
10898 else if (flag_code == CODE_64BIT
10899 && !i.prefix[ADDR_PREFIX]
10900 && exp->X_op == O_constant)
10901 {
10902 /* Since displacement is signed extended to 64bit, don't allow
10903 disp32 and turn off disp32s if they are out of range. */
10904 i.types[this_operand].bitfield.disp32 = 0;
10905 if (!fits_in_signed_long (exp->X_add_number))
10906 {
10907 i.types[this_operand].bitfield.disp32s = 0;
10908 if (i.types[this_operand].bitfield.baseindex)
10909 {
10910 as_bad (_("0x%lx out range of signed 32bit displacement"),
10911 (long) exp->X_add_number);
10912 ret = 0;
10913 }
10914 }
10915 }
10916
4c63da97 10917#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
3992d3b7
AM
10918 else if (exp->X_op != O_constant
10919 && OUTPUT_FLAVOR == bfd_target_aout_flavour
10920 && exp_seg != absolute_section
10921 && exp_seg != text_section
10922 && exp_seg != data_section
10923 && exp_seg != bss_section
10924 && exp_seg != undefined_section
10925 && !bfd_is_com_section (exp_seg))
24eab124 10926 {
d0b47220 10927 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
3992d3b7 10928 ret = 0;
24eab124 10929 }
252b5132 10930#endif
3956db08 10931
48bcea9f
JB
10932 if (current_templates->start->opcode_modifier.jump == JUMP_BYTE
10933 /* Constants get taken care of by optimize_disp(). */
10934 && exp->X_op != O_constant)
10935 i.types[this_operand].bitfield.disp8 = 1;
10936
40fb9820
L
10937 /* Check if this is a displacement only operand. */
10938 bigdisp = i.types[this_operand];
10939 bigdisp.bitfield.disp8 = 0;
10940 bigdisp.bitfield.disp16 = 0;
10941 bigdisp.bitfield.disp32 = 0;
10942 bigdisp.bitfield.disp32s = 0;
10943 bigdisp.bitfield.disp64 = 0;
0dfbf9d7 10944 if (operand_type_all_zero (&bigdisp))
c6fb90c8
L
10945 i.types[this_operand] = operand_type_and (i.types[this_operand],
10946 types);
3956db08 10947
3992d3b7 10948 return ret;
252b5132
RH
10949}
10950
2abc2bec
JB
10951/* Return the active addressing mode, taking address override and
10952 registers forming the address into consideration. Update the
10953 address override prefix if necessary. */
47926f60 10954
2abc2bec
JB
10955static enum flag_code
10956i386_addressing_mode (void)
252b5132 10957{
be05d201
L
10958 enum flag_code addr_mode;
10959
10960 if (i.prefix[ADDR_PREFIX])
10961 addr_mode = flag_code == CODE_32BIT ? CODE_16BIT : CODE_32BIT;
a23b33b3
JB
10962 else if (flag_code == CODE_16BIT
10963 && current_templates->start->cpu_flags.bitfield.cpumpx
10964 /* Avoid replacing the "16-bit addressing not allowed" diagnostic
10965 from md_assemble() by "is not a valid base/index expression"
10966 when there is a base and/or index. */
10967 && !i.types[this_operand].bitfield.baseindex)
10968 {
10969 /* MPX insn memory operands with neither base nor index must be forced
10970 to use 32-bit addressing in 16-bit mode. */
10971 addr_mode = CODE_32BIT;
10972 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
10973 ++i.prefixes;
10974 gas_assert (!i.types[this_operand].bitfield.disp16);
10975 gas_assert (!i.types[this_operand].bitfield.disp32);
10976 }
be05d201
L
10977 else
10978 {
10979 addr_mode = flag_code;
10980
24eab124 10981#if INFER_ADDR_PREFIX
be05d201
L
10982 if (i.mem_operands == 0)
10983 {
10984 /* Infer address prefix from the first memory operand. */
10985 const reg_entry *addr_reg = i.base_reg;
10986
10987 if (addr_reg == NULL)
10988 addr_reg = i.index_reg;
eecb386c 10989
be05d201
L
10990 if (addr_reg)
10991 {
e968fc9b 10992 if (addr_reg->reg_type.bitfield.dword)
be05d201
L
10993 addr_mode = CODE_32BIT;
10994 else if (flag_code != CODE_64BIT
dc821c5f 10995 && addr_reg->reg_type.bitfield.word)
be05d201
L
10996 addr_mode = CODE_16BIT;
10997
10998 if (addr_mode != flag_code)
10999 {
11000 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
11001 i.prefixes += 1;
11002 /* Change the size of any displacement too. At most one
11003 of Disp16 or Disp32 is set.
11004 FIXME. There doesn't seem to be any real need for
11005 separate Disp16 and Disp32 flags. The same goes for
11006 Imm16 and Imm32. Removing them would probably clean
11007 up the code quite a lot. */
11008 if (flag_code != CODE_64BIT
11009 && (i.types[this_operand].bitfield.disp16
11010 || i.types[this_operand].bitfield.disp32))
11011 i.types[this_operand]
11012 = operand_type_xor (i.types[this_operand], disp16_32);
11013 }
11014 }
11015 }
24eab124 11016#endif
be05d201
L
11017 }
11018
2abc2bec
JB
11019 return addr_mode;
11020}
11021
11022/* Make sure the memory operand we've been dealt is valid.
11023 Return 1 on success, 0 on a failure. */
11024
11025static int
11026i386_index_check (const char *operand_string)
11027{
11028 const char *kind = "base/index";
11029 enum flag_code addr_mode = i386_addressing_mode ();
a152332d 11030 const insn_template *t = current_templates->start;
2abc2bec 11031
a152332d
JB
11032 if (t->opcode_modifier.isstring
11033 && !t->cpu_flags.bitfield.cpupadlock
fc0763e6
JB
11034 && (current_templates->end[-1].opcode_modifier.isstring
11035 || i.mem_operands))
11036 {
11037 /* Memory operands of string insns are special in that they only allow
11038 a single register (rDI, rSI, or rBX) as their memory address. */
be05d201
L
11039 const reg_entry *expected_reg;
11040 static const char *di_si[][2] =
11041 {
11042 { "esi", "edi" },
11043 { "si", "di" },
11044 { "rsi", "rdi" }
11045 };
11046 static const char *bx[] = { "ebx", "bx", "rbx" };
fc0763e6
JB
11047
11048 kind = "string address";
11049
a152332d 11050 if (t->opcode_modifier.prefixok == PrefixRep)
fc0763e6 11051 {
51c8edf6
JB
11052 int es_op = current_templates->end[-1].opcode_modifier.isstring
11053 - IS_STRING_ES_OP0;
11054 int op = 0;
fc0763e6 11055
51c8edf6 11056 if (!current_templates->end[-1].operand_types[0].bitfield.baseindex
fc0763e6
JB
11057 || ((!i.mem_operands != !intel_syntax)
11058 && current_templates->end[-1].operand_types[1]
11059 .bitfield.baseindex))
51c8edf6 11060 op = 1;
fe0e921f
AM
11061 expected_reg
11062 = (const reg_entry *) str_hash_find (reg_hash,
11063 di_si[addr_mode][op == es_op]);
fc0763e6
JB
11064 }
11065 else
fe0e921f
AM
11066 expected_reg
11067 = (const reg_entry *)str_hash_find (reg_hash, bx[addr_mode]);
fc0763e6 11068
be05d201
L
11069 if (i.base_reg != expected_reg
11070 || i.index_reg
fc0763e6 11071 || operand_type_check (i.types[this_operand], disp))
fc0763e6 11072 {
be05d201
L
11073 /* The second memory operand must have the same size as
11074 the first one. */
11075 if (i.mem_operands
11076 && i.base_reg
11077 && !((addr_mode == CODE_64BIT
dc821c5f 11078 && i.base_reg->reg_type.bitfield.qword)
be05d201 11079 || (addr_mode == CODE_32BIT
dc821c5f
JB
11080 ? i.base_reg->reg_type.bitfield.dword
11081 : i.base_reg->reg_type.bitfield.word)))
be05d201
L
11082 goto bad_address;
11083
fc0763e6
JB
11084 as_warn (_("`%s' is not valid here (expected `%c%s%s%c')"),
11085 operand_string,
11086 intel_syntax ? '[' : '(',
11087 register_prefix,
be05d201 11088 expected_reg->reg_name,
fc0763e6 11089 intel_syntax ? ']' : ')');
be05d201 11090 return 1;
fc0763e6 11091 }
be05d201
L
11092 else
11093 return 1;
11094
dc1e8a47 11095 bad_address:
be05d201
L
11096 as_bad (_("`%s' is not a valid %s expression"),
11097 operand_string, kind);
11098 return 0;
3e73aa7c
JH
11099 }
11100 else
11101 {
be05d201
L
11102 if (addr_mode != CODE_16BIT)
11103 {
11104 /* 32-bit/64-bit checks. */
41eb8e88
L
11105 if (i.disp_encoding == disp_encoding_16bit)
11106 {
11107 bad_disp:
11108 as_bad (_("invalid `%s' prefix"),
11109 addr_mode == CODE_16BIT ? "{disp32}" : "{disp16}");
11110 return 0;
11111 }
11112
be05d201 11113 if ((i.base_reg
e968fc9b
JB
11114 && ((addr_mode == CODE_64BIT
11115 ? !i.base_reg->reg_type.bitfield.qword
11116 : !i.base_reg->reg_type.bitfield.dword)
11117 || (i.index_reg && i.base_reg->reg_num == RegIP)
11118 || i.base_reg->reg_num == RegIZ))
be05d201 11119 || (i.index_reg
1b54b8d7
JB
11120 && !i.index_reg->reg_type.bitfield.xmmword
11121 && !i.index_reg->reg_type.bitfield.ymmword
11122 && !i.index_reg->reg_type.bitfield.zmmword
be05d201 11123 && ((addr_mode == CODE_64BIT
e968fc9b
JB
11124 ? !i.index_reg->reg_type.bitfield.qword
11125 : !i.index_reg->reg_type.bitfield.dword)
be05d201
L
11126 || !i.index_reg->reg_type.bitfield.baseindex)))
11127 goto bad_address;
8178be5b 11128
260cd341 11129 /* bndmk, bndldx, bndstx and mandatory non-vector SIB have special restrictions. */
a152332d 11130 if ((t->opcode_modifier.opcodeprefix == PREFIX_0XF3
389d00a5
JB
11131 && t->opcode_modifier.opcodespace == SPACE_0F
11132 && t->base_opcode == 0x1b)
a152332d 11133 || (t->opcode_modifier.opcodeprefix == PREFIX_NONE
389d00a5
JB
11134 && t->opcode_modifier.opcodespace == SPACE_0F
11135 && (t->base_opcode & ~1) == 0x1a)
a152332d 11136 || t->opcode_modifier.sib == SIBMEM)
8178be5b
JB
11137 {
11138 /* They cannot use RIP-relative addressing. */
e968fc9b 11139 if (i.base_reg && i.base_reg->reg_num == RegIP)
8178be5b
JB
11140 {
11141 as_bad (_("`%s' cannot be used here"), operand_string);
11142 return 0;
11143 }
11144
11145 /* bndldx and bndstx ignore their scale factor. */
a152332d 11146 if (t->opcode_modifier.opcodeprefix == PREFIX_NONE
389d00a5
JB
11147 && t->opcode_modifier.opcodespace == SPACE_0F
11148 && (t->base_opcode & ~1) == 0x1a
8178be5b
JB
11149 && i.log2_scale_factor)
11150 as_warn (_("register scaling is being ignored here"));
11151 }
be05d201
L
11152 }
11153 else
3e73aa7c 11154 {
be05d201 11155 /* 16-bit checks. */
41eb8e88
L
11156 if (i.disp_encoding == disp_encoding_32bit)
11157 goto bad_disp;
11158
3e73aa7c 11159 if ((i.base_reg
dc821c5f 11160 && (!i.base_reg->reg_type.bitfield.word
40fb9820 11161 || !i.base_reg->reg_type.bitfield.baseindex))
3e73aa7c 11162 || (i.index_reg
dc821c5f 11163 && (!i.index_reg->reg_type.bitfield.word
40fb9820 11164 || !i.index_reg->reg_type.bitfield.baseindex
29b0f896
AM
11165 || !(i.base_reg
11166 && i.base_reg->reg_num < 6
11167 && i.index_reg->reg_num >= 6
11168 && i.log2_scale_factor == 0))))
be05d201 11169 goto bad_address;
3e73aa7c
JH
11170 }
11171 }
be05d201 11172 return 1;
24eab124 11173}
252b5132 11174
43234a1e
L
11175/* Handle vector immediates. */
11176
11177static int
11178RC_SAE_immediate (const char *imm_start)
11179{
11180 unsigned int match_found, j;
11181 const char *pstr = imm_start;
11182 expressionS *exp;
11183
11184 if (*pstr != '{')
11185 return 0;
11186
11187 pstr++;
11188 match_found = 0;
11189 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); j++)
11190 {
11191 if (!strncmp (pstr, RC_NamesTable[j].name, RC_NamesTable[j].len))
11192 {
11193 if (!i.rounding)
11194 {
11195 rc_op.type = RC_NamesTable[j].type;
11196 rc_op.operand = this_operand;
11197 i.rounding = &rc_op;
11198 }
11199 else
11200 {
11201 as_bad (_("duplicated `%s'"), imm_start);
11202 return 0;
11203 }
11204 pstr += RC_NamesTable[j].len;
11205 match_found = 1;
11206 break;
11207 }
11208 }
11209 if (!match_found)
11210 return 0;
11211
11212 if (*pstr++ != '}')
11213 {
11214 as_bad (_("Missing '}': '%s'"), imm_start);
11215 return 0;
11216 }
11217 /* RC/SAE immediate string should contain nothing more. */;
11218 if (*pstr != 0)
11219 {
11220 as_bad (_("Junk after '}': '%s'"), imm_start);
11221 return 0;
11222 }
11223
11224 exp = &im_expressions[i.imm_operands++];
11225 i.op[this_operand].imms = exp;
11226
11227 exp->X_op = O_constant;
11228 exp->X_add_number = 0;
11229 exp->X_add_symbol = (symbolS *) 0;
11230 exp->X_op_symbol = (symbolS *) 0;
11231
11232 i.types[this_operand].bitfield.imm8 = 1;
11233 return 1;
11234}
11235
8325cc63
JB
11236/* Only string instructions can have a second memory operand, so
11237 reduce current_templates to just those if it contains any. */
11238static int
11239maybe_adjust_templates (void)
11240{
11241 const insn_template *t;
11242
11243 gas_assert (i.mem_operands == 1);
11244
11245 for (t = current_templates->start; t < current_templates->end; ++t)
11246 if (t->opcode_modifier.isstring)
11247 break;
11248
11249 if (t < current_templates->end)
11250 {
11251 static templates aux_templates;
11252 bfd_boolean recheck;
11253
11254 aux_templates.start = t;
11255 for (; t < current_templates->end; ++t)
11256 if (!t->opcode_modifier.isstring)
11257 break;
11258 aux_templates.end = t;
11259
11260 /* Determine whether to re-check the first memory operand. */
11261 recheck = (aux_templates.start != current_templates->start
11262 || t != current_templates->end);
11263
11264 current_templates = &aux_templates;
11265
11266 if (recheck)
11267 {
11268 i.mem_operands = 0;
11269 if (i.memop1_string != NULL
11270 && i386_index_check (i.memop1_string) == 0)
11271 return 0;
11272 i.mem_operands = 1;
11273 }
11274 }
11275
11276 return 1;
11277}
11278
fc0763e6 11279/* Parse OPERAND_STRING into the i386_insn structure I. Returns zero
47926f60 11280 on error. */
252b5132 11281
252b5132 11282static int
a7619375 11283i386_att_operand (char *operand_string)
252b5132 11284{
af6bdddf
AM
11285 const reg_entry *r;
11286 char *end_op;
24eab124 11287 char *op_string = operand_string;
252b5132 11288
24eab124 11289 if (is_space_char (*op_string))
252b5132
RH
11290 ++op_string;
11291
24eab124 11292 /* We check for an absolute prefix (differentiating,
47926f60 11293 for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
24eab124
AM
11294 if (*op_string == ABSOLUTE_PREFIX)
11295 {
11296 ++op_string;
11297 if (is_space_char (*op_string))
11298 ++op_string;
6f2f06be 11299 i.jumpabsolute = TRUE;
24eab124 11300 }
252b5132 11301
47926f60 11302 /* Check if operand is a register. */
4d1bb795 11303 if ((r = parse_register (op_string, &end_op)) != NULL)
24eab124 11304 {
40fb9820
L
11305 i386_operand_type temp;
11306
8a6fb3f9
JB
11307 if (r == &bad_reg)
11308 return 0;
11309
24eab124
AM
11310 /* Check for a segment override by searching for ':' after a
11311 segment register. */
11312 op_string = end_op;
11313 if (is_space_char (*op_string))
11314 ++op_string;
00cee14f 11315 if (*op_string == ':' && r->reg_type.bitfield.class == SReg)
24eab124
AM
11316 {
11317 switch (r->reg_num)
11318 {
11319 case 0:
11320 i.seg[i.mem_operands] = &es;
11321 break;
11322 case 1:
11323 i.seg[i.mem_operands] = &cs;
11324 break;
11325 case 2:
11326 i.seg[i.mem_operands] = &ss;
11327 break;
11328 case 3:
11329 i.seg[i.mem_operands] = &ds;
11330 break;
11331 case 4:
11332 i.seg[i.mem_operands] = &fs;
11333 break;
11334 case 5:
11335 i.seg[i.mem_operands] = &gs;
11336 break;
11337 }
252b5132 11338
24eab124 11339 /* Skip the ':' and whitespace. */
252b5132
RH
11340 ++op_string;
11341 if (is_space_char (*op_string))
24eab124 11342 ++op_string;
252b5132 11343
24eab124
AM
11344 if (!is_digit_char (*op_string)
11345 && !is_identifier_char (*op_string)
11346 && *op_string != '('
11347 && *op_string != ABSOLUTE_PREFIX)
11348 {
11349 as_bad (_("bad memory operand `%s'"), op_string);
11350 return 0;
11351 }
47926f60 11352 /* Handle case of %es:*foo. */
24eab124
AM
11353 if (*op_string == ABSOLUTE_PREFIX)
11354 {
11355 ++op_string;
11356 if (is_space_char (*op_string))
11357 ++op_string;
6f2f06be 11358 i.jumpabsolute = TRUE;
24eab124
AM
11359 }
11360 goto do_memory_reference;
11361 }
43234a1e
L
11362
11363 /* Handle vector operations. */
11364 if (*op_string == '{')
11365 {
11366 op_string = check_VecOperations (op_string, NULL);
11367 if (op_string == NULL)
11368 return 0;
11369 }
11370
24eab124
AM
11371 if (*op_string)
11372 {
d0b47220 11373 as_bad (_("junk `%s' after register"), op_string);
24eab124
AM
11374 return 0;
11375 }
40fb9820
L
11376 temp = r->reg_type;
11377 temp.bitfield.baseindex = 0;
c6fb90c8
L
11378 i.types[this_operand] = operand_type_or (i.types[this_operand],
11379 temp);
7d5e4556 11380 i.types[this_operand].bitfield.unspecified = 0;
520dc8e8 11381 i.op[this_operand].regs = r;
24eab124
AM
11382 i.reg_operands++;
11383 }
af6bdddf
AM
11384 else if (*op_string == REGISTER_PREFIX)
11385 {
11386 as_bad (_("bad register name `%s'"), op_string);
11387 return 0;
11388 }
24eab124 11389 else if (*op_string == IMMEDIATE_PREFIX)
ce8a8b2f 11390 {
24eab124 11391 ++op_string;
6f2f06be 11392 if (i.jumpabsolute)
24eab124 11393 {
d0b47220 11394 as_bad (_("immediate operand illegal with absolute jump"));
24eab124
AM
11395 return 0;
11396 }
11397 if (!i386_immediate (op_string))
11398 return 0;
11399 }
43234a1e
L
11400 else if (RC_SAE_immediate (operand_string))
11401 {
11402 /* If it is a RC or SAE immediate, do nothing. */
11403 ;
11404 }
24eab124
AM
11405 else if (is_digit_char (*op_string)
11406 || is_identifier_char (*op_string)
d02603dc 11407 || *op_string == '"'
e5cb08ac 11408 || *op_string == '(')
24eab124 11409 {
47926f60 11410 /* This is a memory reference of some sort. */
af6bdddf 11411 char *base_string;
252b5132 11412
47926f60 11413 /* Start and end of displacement string expression (if found). */
eecb386c
AM
11414 char *displacement_string_start;
11415 char *displacement_string_end;
43234a1e 11416 char *vop_start;
252b5132 11417
24eab124 11418 do_memory_reference:
8325cc63
JB
11419 if (i.mem_operands == 1 && !maybe_adjust_templates ())
11420 return 0;
24eab124 11421 if ((i.mem_operands == 1
40fb9820 11422 && !current_templates->start->opcode_modifier.isstring)
24eab124
AM
11423 || i.mem_operands == 2)
11424 {
11425 as_bad (_("too many memory references for `%s'"),
11426 current_templates->start->name);
11427 return 0;
11428 }
252b5132 11429
24eab124
AM
11430 /* Check for base index form. We detect the base index form by
11431 looking for an ')' at the end of the operand, searching
11432 for the '(' matching it, and finding a REGISTER_PREFIX or ','
11433 after the '('. */
af6bdddf 11434 base_string = op_string + strlen (op_string);
c3332e24 11435
43234a1e
L
11436 /* Handle vector operations. */
11437 vop_start = strchr (op_string, '{');
11438 if (vop_start && vop_start < base_string)
11439 {
11440 if (check_VecOperations (vop_start, base_string) == NULL)
11441 return 0;
11442 base_string = vop_start;
11443 }
11444
af6bdddf
AM
11445 --base_string;
11446 if (is_space_char (*base_string))
11447 --base_string;
252b5132 11448
47926f60 11449 /* If we only have a displacement, set-up for it to be parsed later. */
af6bdddf
AM
11450 displacement_string_start = op_string;
11451 displacement_string_end = base_string + 1;
252b5132 11452
24eab124
AM
11453 if (*base_string == ')')
11454 {
af6bdddf 11455 char *temp_string;
24eab124
AM
11456 unsigned int parens_balanced = 1;
11457 /* We've already checked that the number of left & right ()'s are
47926f60 11458 equal, so this loop will not be infinite. */
24eab124
AM
11459 do
11460 {
11461 base_string--;
11462 if (*base_string == ')')
11463 parens_balanced++;
11464 if (*base_string == '(')
11465 parens_balanced--;
11466 }
11467 while (parens_balanced);
c3332e24 11468
af6bdddf 11469 temp_string = base_string;
c3332e24 11470
24eab124 11471 /* Skip past '(' and whitespace. */
252b5132
RH
11472 ++base_string;
11473 if (is_space_char (*base_string))
24eab124 11474 ++base_string;
252b5132 11475
af6bdddf 11476 if (*base_string == ','
4eed87de
AM
11477 || ((i.base_reg = parse_register (base_string, &end_op))
11478 != NULL))
252b5132 11479 {
af6bdddf 11480 displacement_string_end = temp_string;
252b5132 11481
40fb9820 11482 i.types[this_operand].bitfield.baseindex = 1;
252b5132 11483
af6bdddf 11484 if (i.base_reg)
24eab124 11485 {
8a6fb3f9
JB
11486 if (i.base_reg == &bad_reg)
11487 return 0;
24eab124
AM
11488 base_string = end_op;
11489 if (is_space_char (*base_string))
11490 ++base_string;
af6bdddf
AM
11491 }
11492
11493 /* There may be an index reg or scale factor here. */
11494 if (*base_string == ',')
11495 {
11496 ++base_string;
11497 if (is_space_char (*base_string))
11498 ++base_string;
11499
4eed87de
AM
11500 if ((i.index_reg = parse_register (base_string, &end_op))
11501 != NULL)
24eab124 11502 {
8a6fb3f9
JB
11503 if (i.index_reg == &bad_reg)
11504 return 0;
af6bdddf 11505 base_string = end_op;
24eab124
AM
11506 if (is_space_char (*base_string))
11507 ++base_string;
af6bdddf
AM
11508 if (*base_string == ',')
11509 {
11510 ++base_string;
11511 if (is_space_char (*base_string))
11512 ++base_string;
11513 }
e5cb08ac 11514 else if (*base_string != ')')
af6bdddf 11515 {
4eed87de
AM
11516 as_bad (_("expecting `,' or `)' "
11517 "after index register in `%s'"),
af6bdddf
AM
11518 operand_string);
11519 return 0;
11520 }
24eab124 11521 }
af6bdddf 11522 else if (*base_string == REGISTER_PREFIX)
24eab124 11523 {
f76bf5e0
L
11524 end_op = strchr (base_string, ',');
11525 if (end_op)
11526 *end_op = '\0';
af6bdddf 11527 as_bad (_("bad register name `%s'"), base_string);
24eab124
AM
11528 return 0;
11529 }
252b5132 11530
47926f60 11531 /* Check for scale factor. */
551c1ca1 11532 if (*base_string != ')')
af6bdddf 11533 {
551c1ca1
AM
11534 char *end_scale = i386_scale (base_string);
11535
11536 if (!end_scale)
af6bdddf 11537 return 0;
24eab124 11538
551c1ca1 11539 base_string = end_scale;
af6bdddf
AM
11540 if (is_space_char (*base_string))
11541 ++base_string;
11542 if (*base_string != ')')
11543 {
4eed87de
AM
11544 as_bad (_("expecting `)' "
11545 "after scale factor in `%s'"),
af6bdddf
AM
11546 operand_string);
11547 return 0;
11548 }
11549 }
11550 else if (!i.index_reg)
24eab124 11551 {
4eed87de
AM
11552 as_bad (_("expecting index register or scale factor "
11553 "after `,'; got '%c'"),
af6bdddf 11554 *base_string);
24eab124
AM
11555 return 0;
11556 }
11557 }
af6bdddf 11558 else if (*base_string != ')')
24eab124 11559 {
4eed87de
AM
11560 as_bad (_("expecting `,' or `)' "
11561 "after base register in `%s'"),
af6bdddf 11562 operand_string);
24eab124
AM
11563 return 0;
11564 }
c3332e24 11565 }
af6bdddf 11566 else if (*base_string == REGISTER_PREFIX)
c3332e24 11567 {
f76bf5e0
L
11568 end_op = strchr (base_string, ',');
11569 if (end_op)
11570 *end_op = '\0';
af6bdddf 11571 as_bad (_("bad register name `%s'"), base_string);
24eab124 11572 return 0;
c3332e24 11573 }
24eab124
AM
11574 }
11575
11576 /* If there's an expression beginning the operand, parse it,
11577 assuming displacement_string_start and
11578 displacement_string_end are meaningful. */
11579 if (displacement_string_start != displacement_string_end)
11580 {
11581 if (!i386_displacement (displacement_string_start,
11582 displacement_string_end))
11583 return 0;
11584 }
11585
11586 /* Special case for (%dx) while doing input/output op. */
11587 if (i.base_reg
75e5731b
JB
11588 && i.base_reg->reg_type.bitfield.instance == RegD
11589 && i.base_reg->reg_type.bitfield.word
24eab124
AM
11590 && i.index_reg == 0
11591 && i.log2_scale_factor == 0
11592 && i.seg[i.mem_operands] == 0
40fb9820 11593 && !operand_type_check (i.types[this_operand], disp))
24eab124 11594 {
2fb5be8d 11595 i.types[this_operand] = i.base_reg->reg_type;
24eab124
AM
11596 return 1;
11597 }
11598
eecb386c
AM
11599 if (i386_index_check (operand_string) == 0)
11600 return 0;
c48dadc9 11601 i.flags[this_operand] |= Operand_Mem;
8325cc63
JB
11602 if (i.mem_operands == 0)
11603 i.memop1_string = xstrdup (operand_string);
24eab124
AM
11604 i.mem_operands++;
11605 }
11606 else
ce8a8b2f
AM
11607 {
11608 /* It's not a memory operand; argh! */
24eab124
AM
11609 as_bad (_("invalid char %s beginning operand %d `%s'"),
11610 output_invalid (*op_string),
11611 this_operand + 1,
11612 op_string);
11613 return 0;
11614 }
47926f60 11615 return 1; /* Normal return. */
252b5132
RH
11616}
11617\f
fa94de6b
RM
11618/* Calculate the maximum variable size (i.e., excluding fr_fix)
11619 that an rs_machine_dependent frag may reach. */
11620
11621unsigned int
11622i386_frag_max_var (fragS *frag)
11623{
11624 /* The only relaxable frags are for jumps.
11625 Unconditional jumps can grow by 4 bytes and others by 5 bytes. */
11626 gas_assert (frag->fr_type == rs_machine_dependent);
11627 return TYPE_FROM_RELAX_STATE (frag->fr_subtype) == UNCOND_JUMP ? 4 : 5;
11628}
11629
b084df0b
L
11630#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
11631static int
8dcea932 11632elf_symbol_resolved_in_segment_p (symbolS *fr_symbol, offsetT fr_var)
b084df0b
L
11633{
11634 /* STT_GNU_IFUNC symbol must go through PLT. */
11635 if ((symbol_get_bfdsym (fr_symbol)->flags
11636 & BSF_GNU_INDIRECT_FUNCTION) != 0)
11637 return 0;
11638
11639 if (!S_IS_EXTERNAL (fr_symbol))
11640 /* Symbol may be weak or local. */
11641 return !S_IS_WEAK (fr_symbol);
11642
8dcea932
L
11643 /* Global symbols with non-default visibility can't be preempted. */
11644 if (ELF_ST_VISIBILITY (S_GET_OTHER (fr_symbol)) != STV_DEFAULT)
11645 return 1;
11646
11647 if (fr_var != NO_RELOC)
11648 switch ((enum bfd_reloc_code_real) fr_var)
11649 {
11650 case BFD_RELOC_386_PLT32:
11651 case BFD_RELOC_X86_64_PLT32:
33eaf5de 11652 /* Symbol with PLT relocation may be preempted. */
8dcea932
L
11653 return 0;
11654 default:
11655 abort ();
11656 }
11657
b084df0b
L
11658 /* Global symbols with default visibility in a shared library may be
11659 preempted by another definition. */
8dcea932 11660 return !shared;
b084df0b
L
11661}
11662#endif
11663
79d72f45
HL
11664/* Table 3-2. Macro-Fusible Instructions in Haswell Microarchitecture
11665 Note also work for Skylake and Cascadelake.
11666---------------------------------------------------------------------
11667| JCC | ADD/SUB/CMP | INC/DEC | TEST/AND |
11668| ------ | ----------- | ------- | -------- |
11669| Jo | N | N | Y |
11670| Jno | N | N | Y |
11671| Jc/Jb | Y | N | Y |
11672| Jae/Jnb | Y | N | Y |
11673| Je/Jz | Y | Y | Y |
11674| Jne/Jnz | Y | Y | Y |
11675| Jna/Jbe | Y | N | Y |
11676| Ja/Jnbe | Y | N | Y |
11677| Js | N | N | Y |
11678| Jns | N | N | Y |
11679| Jp/Jpe | N | N | Y |
11680| Jnp/Jpo | N | N | Y |
11681| Jl/Jnge | Y | Y | Y |
11682| Jge/Jnl | Y | Y | Y |
11683| Jle/Jng | Y | Y | Y |
11684| Jg/Jnle | Y | Y | Y |
11685--------------------------------------------------------------------- */
11686static int
11687i386_macro_fusible_p (enum mf_cmp_kind mf_cmp, enum mf_jcc_kind mf_jcc)
11688{
11689 if (mf_cmp == mf_cmp_alu_cmp)
11690 return ((mf_jcc >= mf_jcc_jc && mf_jcc <= mf_jcc_jna)
11691 || mf_jcc == mf_jcc_jl || mf_jcc == mf_jcc_jle);
11692 if (mf_cmp == mf_cmp_incdec)
11693 return (mf_jcc == mf_jcc_je || mf_jcc == mf_jcc_jl
11694 || mf_jcc == mf_jcc_jle);
11695 if (mf_cmp == mf_cmp_test_and)
11696 return 1;
11697 return 0;
11698}
11699
e379e5f3
L
11700/* Return the next non-empty frag. */
11701
11702static fragS *
11703i386_next_non_empty_frag (fragS *fragP)
11704{
11705 /* There may be a frag with a ".fill 0" when there is no room in
11706 the current frag for frag_grow in output_insn. */
11707 for (fragP = fragP->fr_next;
11708 (fragP != NULL
11709 && fragP->fr_type == rs_fill
11710 && fragP->fr_fix == 0);
11711 fragP = fragP->fr_next)
11712 ;
11713 return fragP;
11714}
11715
11716/* Return the next jcc frag after BRANCH_PADDING. */
11717
11718static fragS *
79d72f45 11719i386_next_fusible_jcc_frag (fragS *maybe_cmp_fragP, fragS *pad_fragP)
e379e5f3 11720{
79d72f45
HL
11721 fragS *branch_fragP;
11722 if (!pad_fragP)
e379e5f3
L
11723 return NULL;
11724
79d72f45
HL
11725 if (pad_fragP->fr_type == rs_machine_dependent
11726 && (TYPE_FROM_RELAX_STATE (pad_fragP->fr_subtype)
e379e5f3
L
11727 == BRANCH_PADDING))
11728 {
79d72f45
HL
11729 branch_fragP = i386_next_non_empty_frag (pad_fragP);
11730 if (branch_fragP->fr_type != rs_machine_dependent)
e379e5f3 11731 return NULL;
79d72f45
HL
11732 if (TYPE_FROM_RELAX_STATE (branch_fragP->fr_subtype) == COND_JUMP
11733 && i386_macro_fusible_p (maybe_cmp_fragP->tc_frag_data.mf_type,
11734 pad_fragP->tc_frag_data.mf_type))
11735 return branch_fragP;
e379e5f3
L
11736 }
11737
11738 return NULL;
11739}
11740
11741/* Classify BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags. */
11742
11743static void
11744i386_classify_machine_dependent_frag (fragS *fragP)
11745{
11746 fragS *cmp_fragP;
11747 fragS *pad_fragP;
11748 fragS *branch_fragP;
11749 fragS *next_fragP;
11750 unsigned int max_prefix_length;
11751
11752 if (fragP->tc_frag_data.classified)
11753 return;
11754
11755 /* First scan for BRANCH_PADDING and FUSED_JCC_PADDING. Convert
11756 FUSED_JCC_PADDING and merge BRANCH_PADDING. */
11757 for (next_fragP = fragP;
11758 next_fragP != NULL;
11759 next_fragP = next_fragP->fr_next)
11760 {
11761 next_fragP->tc_frag_data.classified = 1;
11762 if (next_fragP->fr_type == rs_machine_dependent)
11763 switch (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype))
11764 {
11765 case BRANCH_PADDING:
11766 /* The BRANCH_PADDING frag must be followed by a branch
11767 frag. */
11768 branch_fragP = i386_next_non_empty_frag (next_fragP);
11769 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
11770 break;
11771 case FUSED_JCC_PADDING:
11772 /* Check if this is a fused jcc:
11773 FUSED_JCC_PADDING
11774 CMP like instruction
11775 BRANCH_PADDING
11776 COND_JUMP
11777 */
11778 cmp_fragP = i386_next_non_empty_frag (next_fragP);
11779 pad_fragP = i386_next_non_empty_frag (cmp_fragP);
79d72f45 11780 branch_fragP = i386_next_fusible_jcc_frag (next_fragP, pad_fragP);
e379e5f3
L
11781 if (branch_fragP)
11782 {
11783 /* The BRANCH_PADDING frag is merged with the
11784 FUSED_JCC_PADDING frag. */
11785 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
11786 /* CMP like instruction size. */
11787 next_fragP->tc_frag_data.cmp_size = cmp_fragP->fr_fix;
11788 frag_wane (pad_fragP);
11789 /* Skip to branch_fragP. */
11790 next_fragP = branch_fragP;
11791 }
11792 else if (next_fragP->tc_frag_data.max_prefix_length)
11793 {
11794 /* Turn FUSED_JCC_PADDING into BRANCH_PREFIX if it isn't
11795 a fused jcc. */
11796 next_fragP->fr_subtype
11797 = ENCODE_RELAX_STATE (BRANCH_PREFIX, 0);
11798 next_fragP->tc_frag_data.max_bytes
11799 = next_fragP->tc_frag_data.max_prefix_length;
11800 /* This will be updated in the BRANCH_PREFIX scan. */
11801 next_fragP->tc_frag_data.max_prefix_length = 0;
11802 }
11803 else
11804 frag_wane (next_fragP);
11805 break;
11806 }
11807 }
11808
11809 /* Stop if there is no BRANCH_PREFIX. */
11810 if (!align_branch_prefix_size)
11811 return;
11812
11813 /* Scan for BRANCH_PREFIX. */
11814 for (; fragP != NULL; fragP = fragP->fr_next)
11815 {
11816 if (fragP->fr_type != rs_machine_dependent
11817 || (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
11818 != BRANCH_PREFIX))
11819 continue;
11820
11821 /* Count all BRANCH_PREFIX frags before BRANCH_PADDING and
11822 COND_JUMP_PREFIX. */
11823 max_prefix_length = 0;
11824 for (next_fragP = fragP;
11825 next_fragP != NULL;
11826 next_fragP = next_fragP->fr_next)
11827 {
11828 if (next_fragP->fr_type == rs_fill)
11829 /* Skip rs_fill frags. */
11830 continue;
11831 else if (next_fragP->fr_type != rs_machine_dependent)
11832 /* Stop for all other frags. */
11833 break;
11834
11835 /* rs_machine_dependent frags. */
11836 if (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
11837 == BRANCH_PREFIX)
11838 {
11839 /* Count BRANCH_PREFIX frags. */
11840 if (max_prefix_length >= MAX_FUSED_JCC_PADDING_SIZE)
11841 {
11842 max_prefix_length = MAX_FUSED_JCC_PADDING_SIZE;
11843 frag_wane (next_fragP);
11844 }
11845 else
11846 max_prefix_length
11847 += next_fragP->tc_frag_data.max_bytes;
11848 }
11849 else if ((TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
11850 == BRANCH_PADDING)
11851 || (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
11852 == FUSED_JCC_PADDING))
11853 {
11854 /* Stop at BRANCH_PADDING and FUSED_JCC_PADDING. */
11855 fragP->tc_frag_data.u.padding_fragP = next_fragP;
11856 break;
11857 }
11858 else
11859 /* Stop for other rs_machine_dependent frags. */
11860 break;
11861 }
11862
11863 fragP->tc_frag_data.max_prefix_length = max_prefix_length;
11864
11865 /* Skip to the next frag. */
11866 fragP = next_fragP;
11867 }
11868}
11869
11870/* Compute padding size for
11871
11872 FUSED_JCC_PADDING
11873 CMP like instruction
11874 BRANCH_PADDING
11875 COND_JUMP/UNCOND_JUMP
11876
11877 or
11878
11879 BRANCH_PADDING
11880 COND_JUMP/UNCOND_JUMP
11881 */
11882
11883static int
11884i386_branch_padding_size (fragS *fragP, offsetT address)
11885{
11886 unsigned int offset, size, padding_size;
11887 fragS *branch_fragP = fragP->tc_frag_data.u.branch_fragP;
11888
11889 /* The start address of the BRANCH_PADDING or FUSED_JCC_PADDING frag. */
11890 if (!address)
11891 address = fragP->fr_address;
11892 address += fragP->fr_fix;
11893
11894 /* CMP like instrunction size. */
11895 size = fragP->tc_frag_data.cmp_size;
11896
11897 /* The base size of the branch frag. */
11898 size += branch_fragP->fr_fix;
11899
11900 /* Add opcode and displacement bytes for the rs_machine_dependent
11901 branch frag. */
11902 if (branch_fragP->fr_type == rs_machine_dependent)
11903 size += md_relax_table[branch_fragP->fr_subtype].rlx_length;
11904
11905 /* Check if branch is within boundary and doesn't end at the last
11906 byte. */
11907 offset = address & ((1U << align_branch_power) - 1);
11908 if ((offset + size) >= (1U << align_branch_power))
11909 /* Padding needed to avoid crossing boundary. */
11910 padding_size = (1U << align_branch_power) - offset;
11911 else
11912 /* No padding needed. */
11913 padding_size = 0;
11914
11915 /* The return value may be saved in tc_frag_data.length which is
11916 unsigned byte. */
11917 if (!fits_in_unsigned_byte (padding_size))
11918 abort ();
11919
11920 return padding_size;
11921}
11922
11923/* i386_generic_table_relax_frag()
11924
11925 Handle BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags to
11926 grow/shrink padding to align branch frags. Hand others to
11927 relax_frag(). */
11928
11929long
11930i386_generic_table_relax_frag (segT segment, fragS *fragP, long stretch)
11931{
11932 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
11933 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
11934 {
11935 long padding_size = i386_branch_padding_size (fragP, 0);
11936 long grow = padding_size - fragP->tc_frag_data.length;
11937
11938 /* When the BRANCH_PREFIX frag is used, the computed address
11939 must match the actual address and there should be no padding. */
11940 if (fragP->tc_frag_data.padding_address
11941 && (fragP->tc_frag_data.padding_address != fragP->fr_address
11942 || padding_size))
11943 abort ();
11944
11945 /* Update the padding size. */
11946 if (grow)
11947 fragP->tc_frag_data.length = padding_size;
11948
11949 return grow;
11950 }
11951 else if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
11952 {
11953 fragS *padding_fragP, *next_fragP;
11954 long padding_size, left_size, last_size;
11955
11956 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
11957 if (!padding_fragP)
11958 /* Use the padding set by the leading BRANCH_PREFIX frag. */
11959 return (fragP->tc_frag_data.length
11960 - fragP->tc_frag_data.last_length);
11961
11962 /* Compute the relative address of the padding frag in the very
11963 first time where the BRANCH_PREFIX frag sizes are zero. */
11964 if (!fragP->tc_frag_data.padding_address)
11965 fragP->tc_frag_data.padding_address
11966 = padding_fragP->fr_address - (fragP->fr_address - stretch);
11967
11968 /* First update the last length from the previous interation. */
11969 left_size = fragP->tc_frag_data.prefix_length;
11970 for (next_fragP = fragP;
11971 next_fragP != padding_fragP;
11972 next_fragP = next_fragP->fr_next)
11973 if (next_fragP->fr_type == rs_machine_dependent
11974 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
11975 == BRANCH_PREFIX))
11976 {
11977 if (left_size)
11978 {
11979 int max = next_fragP->tc_frag_data.max_bytes;
11980 if (max)
11981 {
11982 int size;
11983 if (max > left_size)
11984 size = left_size;
11985 else
11986 size = max;
11987 left_size -= size;
11988 next_fragP->tc_frag_data.last_length = size;
11989 }
11990 }
11991 else
11992 next_fragP->tc_frag_data.last_length = 0;
11993 }
11994
11995 /* Check the padding size for the padding frag. */
11996 padding_size = i386_branch_padding_size
11997 (padding_fragP, (fragP->fr_address
11998 + fragP->tc_frag_data.padding_address));
11999
12000 last_size = fragP->tc_frag_data.prefix_length;
12001 /* Check if there is change from the last interation. */
12002 if (padding_size == last_size)
12003 {
12004 /* Update the expected address of the padding frag. */
12005 padding_fragP->tc_frag_data.padding_address
12006 = (fragP->fr_address + padding_size
12007 + fragP->tc_frag_data.padding_address);
12008 return 0;
12009 }
12010
12011 if (padding_size > fragP->tc_frag_data.max_prefix_length)
12012 {
12013 /* No padding if there is no sufficient room. Clear the
12014 expected address of the padding frag. */
12015 padding_fragP->tc_frag_data.padding_address = 0;
12016 padding_size = 0;
12017 }
12018 else
12019 /* Store the expected address of the padding frag. */
12020 padding_fragP->tc_frag_data.padding_address
12021 = (fragP->fr_address + padding_size
12022 + fragP->tc_frag_data.padding_address);
12023
12024 fragP->tc_frag_data.prefix_length = padding_size;
12025
12026 /* Update the length for the current interation. */
12027 left_size = padding_size;
12028 for (next_fragP = fragP;
12029 next_fragP != padding_fragP;
12030 next_fragP = next_fragP->fr_next)
12031 if (next_fragP->fr_type == rs_machine_dependent
12032 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12033 == BRANCH_PREFIX))
12034 {
12035 if (left_size)
12036 {
12037 int max = next_fragP->tc_frag_data.max_bytes;
12038 if (max)
12039 {
12040 int size;
12041 if (max > left_size)
12042 size = left_size;
12043 else
12044 size = max;
12045 left_size -= size;
12046 next_fragP->tc_frag_data.length = size;
12047 }
12048 }
12049 else
12050 next_fragP->tc_frag_data.length = 0;
12051 }
12052
12053 return (fragP->tc_frag_data.length
12054 - fragP->tc_frag_data.last_length);
12055 }
12056 return relax_frag (segment, fragP, stretch);
12057}
12058
ee7fcc42
AM
12059/* md_estimate_size_before_relax()
12060
12061 Called just before relax() for rs_machine_dependent frags. The x86
12062 assembler uses these frags to handle variable size jump
12063 instructions.
12064
12065 Any symbol that is now undefined will not become defined.
12066 Return the correct fr_subtype in the frag.
12067 Return the initial "guess for variable size of frag" to caller.
12068 The guess is actually the growth beyond the fixed part. Whatever
12069 we do to grow the fixed or variable part contributes to our
12070 returned value. */
12071
252b5132 12072int
7016a5d5 12073md_estimate_size_before_relax (fragS *fragP, segT segment)
252b5132 12074{
e379e5f3
L
12075 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12076 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX
12077 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
12078 {
12079 i386_classify_machine_dependent_frag (fragP);
12080 return fragP->tc_frag_data.length;
12081 }
12082
252b5132 12083 /* We've already got fragP->fr_subtype right; all we have to do is
b98ef147
AM
12084 check for un-relaxable symbols. On an ELF system, we can't relax
12085 an externally visible symbol, because it may be overridden by a
12086 shared library. */
12087 if (S_GET_SEGMENT (fragP->fr_symbol) != segment
6d249963 12088#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12089 || (IS_ELF
8dcea932
L
12090 && !elf_symbol_resolved_in_segment_p (fragP->fr_symbol,
12091 fragP->fr_var))
fbeb56a4
DK
12092#endif
12093#if defined (OBJ_COFF) && defined (TE_PE)
7ab9ffdd 12094 || (OUTPUT_FLAVOR == bfd_target_coff_flavour
fbeb56a4 12095 && S_IS_WEAK (fragP->fr_symbol))
b98ef147
AM
12096#endif
12097 )
252b5132 12098 {
b98ef147
AM
12099 /* Symbol is undefined in this segment, or we need to keep a
12100 reloc so that weak symbols can be overridden. */
12101 int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
f86103b7 12102 enum bfd_reloc_code_real reloc_type;
ee7fcc42
AM
12103 unsigned char *opcode;
12104 int old_fr_fix;
f6af82bd 12105
ee7fcc42 12106 if (fragP->fr_var != NO_RELOC)
1e9cc1c2 12107 reloc_type = (enum bfd_reloc_code_real) fragP->fr_var;
b98ef147 12108 else if (size == 2)
f6af82bd 12109 reloc_type = BFD_RELOC_16_PCREL;
bd7ab16b
L
12110#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
12111 else if (need_plt32_p (fragP->fr_symbol))
12112 reloc_type = BFD_RELOC_X86_64_PLT32;
12113#endif
f6af82bd
AM
12114 else
12115 reloc_type = BFD_RELOC_32_PCREL;
252b5132 12116
ee7fcc42
AM
12117 old_fr_fix = fragP->fr_fix;
12118 opcode = (unsigned char *) fragP->fr_opcode;
12119
fddf5b5b 12120 switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
252b5132 12121 {
fddf5b5b
AM
12122 case UNCOND_JUMP:
12123 /* Make jmp (0xeb) a (d)word displacement jump. */
47926f60 12124 opcode[0] = 0xe9;
252b5132 12125 fragP->fr_fix += size;
062cd5e7
AS
12126 fix_new (fragP, old_fr_fix, size,
12127 fragP->fr_symbol,
12128 fragP->fr_offset, 1,
12129 reloc_type);
252b5132
RH
12130 break;
12131
fddf5b5b 12132 case COND_JUMP86:
412167cb
AM
12133 if (size == 2
12134 && (!no_cond_jump_promotion || fragP->fr_var != NO_RELOC))
fddf5b5b
AM
12135 {
12136 /* Negate the condition, and branch past an
12137 unconditional jump. */
12138 opcode[0] ^= 1;
12139 opcode[1] = 3;
12140 /* Insert an unconditional jump. */
12141 opcode[2] = 0xe9;
12142 /* We added two extra opcode bytes, and have a two byte
12143 offset. */
12144 fragP->fr_fix += 2 + 2;
062cd5e7
AS
12145 fix_new (fragP, old_fr_fix + 2, 2,
12146 fragP->fr_symbol,
12147 fragP->fr_offset, 1,
12148 reloc_type);
fddf5b5b
AM
12149 break;
12150 }
12151 /* Fall through. */
12152
12153 case COND_JUMP:
412167cb
AM
12154 if (no_cond_jump_promotion && fragP->fr_var == NO_RELOC)
12155 {
3e02c1cc
AM
12156 fixS *fixP;
12157
412167cb 12158 fragP->fr_fix += 1;
3e02c1cc
AM
12159 fixP = fix_new (fragP, old_fr_fix, 1,
12160 fragP->fr_symbol,
12161 fragP->fr_offset, 1,
12162 BFD_RELOC_8_PCREL);
12163 fixP->fx_signed = 1;
412167cb
AM
12164 break;
12165 }
93c2a809 12166
24eab124 12167 /* This changes the byte-displacement jump 0x7N
fddf5b5b 12168 to the (d)word-displacement jump 0x0f,0x8N. */
252b5132 12169 opcode[1] = opcode[0] + 0x10;
f6af82bd 12170 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
47926f60
KH
12171 /* We've added an opcode byte. */
12172 fragP->fr_fix += 1 + size;
062cd5e7
AS
12173 fix_new (fragP, old_fr_fix + 1, size,
12174 fragP->fr_symbol,
12175 fragP->fr_offset, 1,
12176 reloc_type);
252b5132 12177 break;
fddf5b5b
AM
12178
12179 default:
12180 BAD_CASE (fragP->fr_subtype);
12181 break;
252b5132
RH
12182 }
12183 frag_wane (fragP);
ee7fcc42 12184 return fragP->fr_fix - old_fr_fix;
252b5132 12185 }
93c2a809 12186
93c2a809
AM
12187 /* Guess size depending on current relax state. Initially the relax
12188 state will correspond to a short jump and we return 1, because
12189 the variable part of the frag (the branch offset) is one byte
12190 long. However, we can relax a section more than once and in that
12191 case we must either set fr_subtype back to the unrelaxed state,
12192 or return the value for the appropriate branch. */
12193 return md_relax_table[fragP->fr_subtype].rlx_length;
ee7fcc42
AM
12194}
12195
47926f60
KH
12196/* Called after relax() is finished.
12197
12198 In: Address of frag.
12199 fr_type == rs_machine_dependent.
12200 fr_subtype is what the address relaxed to.
12201
12202 Out: Any fixSs and constants are set up.
12203 Caller will turn frag into a ".space 0". */
12204
252b5132 12205void
7016a5d5
TG
12206md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec ATTRIBUTE_UNUSED,
12207 fragS *fragP)
252b5132 12208{
29b0f896 12209 unsigned char *opcode;
252b5132 12210 unsigned char *where_to_put_displacement = NULL;
847f7ad4
AM
12211 offsetT target_address;
12212 offsetT opcode_address;
252b5132 12213 unsigned int extension = 0;
847f7ad4 12214 offsetT displacement_from_opcode_start;
252b5132 12215
e379e5f3
L
12216 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12217 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING
12218 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12219 {
12220 /* Generate nop padding. */
12221 unsigned int size = fragP->tc_frag_data.length;
12222 if (size)
12223 {
12224 if (size > fragP->tc_frag_data.max_bytes)
12225 abort ();
12226
12227 if (flag_debug)
12228 {
12229 const char *msg;
12230 const char *branch = "branch";
12231 const char *prefix = "";
12232 fragS *padding_fragP;
12233 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
12234 == BRANCH_PREFIX)
12235 {
12236 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
12237 switch (fragP->tc_frag_data.default_prefix)
12238 {
12239 default:
12240 abort ();
12241 break;
12242 case CS_PREFIX_OPCODE:
12243 prefix = " cs";
12244 break;
12245 case DS_PREFIX_OPCODE:
12246 prefix = " ds";
12247 break;
12248 case ES_PREFIX_OPCODE:
12249 prefix = " es";
12250 break;
12251 case FS_PREFIX_OPCODE:
12252 prefix = " fs";
12253 break;
12254 case GS_PREFIX_OPCODE:
12255 prefix = " gs";
12256 break;
12257 case SS_PREFIX_OPCODE:
12258 prefix = " ss";
12259 break;
12260 }
12261 if (padding_fragP)
12262 msg = _("%s:%u: add %d%s at 0x%llx to align "
12263 "%s within %d-byte boundary\n");
12264 else
12265 msg = _("%s:%u: add additional %d%s at 0x%llx to "
12266 "align %s within %d-byte boundary\n");
12267 }
12268 else
12269 {
12270 padding_fragP = fragP;
12271 msg = _("%s:%u: add %d%s-byte nop at 0x%llx to align "
12272 "%s within %d-byte boundary\n");
12273 }
12274
12275 if (padding_fragP)
12276 switch (padding_fragP->tc_frag_data.branch_type)
12277 {
12278 case align_branch_jcc:
12279 branch = "jcc";
12280 break;
12281 case align_branch_fused:
12282 branch = "fused jcc";
12283 break;
12284 case align_branch_jmp:
12285 branch = "jmp";
12286 break;
12287 case align_branch_call:
12288 branch = "call";
12289 break;
12290 case align_branch_indirect:
12291 branch = "indiret branch";
12292 break;
12293 case align_branch_ret:
12294 branch = "ret";
12295 break;
12296 default:
12297 break;
12298 }
12299
12300 fprintf (stdout, msg,
12301 fragP->fr_file, fragP->fr_line, size, prefix,
12302 (long long) fragP->fr_address, branch,
12303 1 << align_branch_power);
12304 }
12305 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12306 memset (fragP->fr_opcode,
12307 fragP->tc_frag_data.default_prefix, size);
12308 else
12309 i386_generate_nops (fragP, (char *) fragP->fr_opcode,
12310 size, 0);
12311 fragP->fr_fix += size;
12312 }
12313 return;
12314 }
12315
252b5132
RH
12316 opcode = (unsigned char *) fragP->fr_opcode;
12317
47926f60 12318 /* Address we want to reach in file space. */
252b5132 12319 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
252b5132 12320
47926f60 12321 /* Address opcode resides at in file space. */
252b5132
RH
12322 opcode_address = fragP->fr_address + fragP->fr_fix;
12323
47926f60 12324 /* Displacement from opcode start to fill into instruction. */
252b5132
RH
12325 displacement_from_opcode_start = target_address - opcode_address;
12326
fddf5b5b 12327 if ((fragP->fr_subtype & BIG) == 0)
252b5132 12328 {
47926f60
KH
12329 /* Don't have to change opcode. */
12330 extension = 1; /* 1 opcode + 1 displacement */
252b5132 12331 where_to_put_displacement = &opcode[1];
fddf5b5b
AM
12332 }
12333 else
12334 {
12335 if (no_cond_jump_promotion
12336 && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
4eed87de
AM
12337 as_warn_where (fragP->fr_file, fragP->fr_line,
12338 _("long jump required"));
252b5132 12339
fddf5b5b
AM
12340 switch (fragP->fr_subtype)
12341 {
12342 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
12343 extension = 4; /* 1 opcode + 4 displacement */
12344 opcode[0] = 0xe9;
12345 where_to_put_displacement = &opcode[1];
12346 break;
252b5132 12347
fddf5b5b
AM
12348 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
12349 extension = 2; /* 1 opcode + 2 displacement */
12350 opcode[0] = 0xe9;
12351 where_to_put_displacement = &opcode[1];
12352 break;
252b5132 12353
fddf5b5b
AM
12354 case ENCODE_RELAX_STATE (COND_JUMP, BIG):
12355 case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
12356 extension = 5; /* 2 opcode + 4 displacement */
12357 opcode[1] = opcode[0] + 0x10;
12358 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12359 where_to_put_displacement = &opcode[2];
12360 break;
252b5132 12361
fddf5b5b
AM
12362 case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
12363 extension = 3; /* 2 opcode + 2 displacement */
12364 opcode[1] = opcode[0] + 0x10;
12365 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12366 where_to_put_displacement = &opcode[2];
12367 break;
252b5132 12368
fddf5b5b
AM
12369 case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
12370 extension = 4;
12371 opcode[0] ^= 1;
12372 opcode[1] = 3;
12373 opcode[2] = 0xe9;
12374 where_to_put_displacement = &opcode[3];
12375 break;
12376
12377 default:
12378 BAD_CASE (fragP->fr_subtype);
12379 break;
12380 }
252b5132 12381 }
fddf5b5b 12382
7b81dfbb
AJ
12383 /* If size if less then four we are sure that the operand fits,
12384 but if it's 4, then it could be that the displacement is larger
12385 then -/+ 2GB. */
12386 if (DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype) == 4
12387 && object_64bit
12388 && ((addressT) (displacement_from_opcode_start - extension
4eed87de
AM
12389 + ((addressT) 1 << 31))
12390 > (((addressT) 2 << 31) - 1)))
7b81dfbb
AJ
12391 {
12392 as_bad_where (fragP->fr_file, fragP->fr_line,
12393 _("jump target out of range"));
12394 /* Make us emit 0. */
12395 displacement_from_opcode_start = extension;
12396 }
47926f60 12397 /* Now put displacement after opcode. */
252b5132
RH
12398 md_number_to_chars ((char *) where_to_put_displacement,
12399 (valueT) (displacement_from_opcode_start - extension),
fddf5b5b 12400 DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
252b5132
RH
12401 fragP->fr_fix += extension;
12402}
12403\f
7016a5d5 12404/* Apply a fixup (fixP) to segment data, once it has been determined
252b5132
RH
12405 by our caller that we have all the info we need to fix it up.
12406
7016a5d5
TG
12407 Parameter valP is the pointer to the value of the bits.
12408
252b5132
RH
12409 On the 386, immediates, displacements, and data pointers are all in
12410 the same (little-endian) format, so we don't need to care about which
12411 we are handling. */
12412
94f592af 12413void
7016a5d5 12414md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 12415{
94f592af 12416 char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
c6682705 12417 valueT value = *valP;
252b5132 12418
f86103b7 12419#if !defined (TE_Mach)
93382f6d
AM
12420 if (fixP->fx_pcrel)
12421 {
12422 switch (fixP->fx_r_type)
12423 {
5865bb77
ILT
12424 default:
12425 break;
12426
d6ab8113
JB
12427 case BFD_RELOC_64:
12428 fixP->fx_r_type = BFD_RELOC_64_PCREL;
12429 break;
93382f6d 12430 case BFD_RELOC_32:
ae8887b5 12431 case BFD_RELOC_X86_64_32S:
93382f6d
AM
12432 fixP->fx_r_type = BFD_RELOC_32_PCREL;
12433 break;
12434 case BFD_RELOC_16:
12435 fixP->fx_r_type = BFD_RELOC_16_PCREL;
12436 break;
12437 case BFD_RELOC_8:
12438 fixP->fx_r_type = BFD_RELOC_8_PCREL;
12439 break;
12440 }
12441 }
252b5132 12442
a161fe53 12443 if (fixP->fx_addsy != NULL
31312f95 12444 && (fixP->fx_r_type == BFD_RELOC_32_PCREL
d6ab8113 12445 || fixP->fx_r_type == BFD_RELOC_64_PCREL
31312f95 12446 || fixP->fx_r_type == BFD_RELOC_16_PCREL
d258b828 12447 || fixP->fx_r_type == BFD_RELOC_8_PCREL)
31312f95 12448 && !use_rela_relocations)
252b5132 12449 {
31312f95
AM
12450 /* This is a hack. There should be a better way to handle this.
12451 This covers for the fact that bfd_install_relocation will
12452 subtract the current location (for partial_inplace, PC relative
12453 relocations); see more below. */
252b5132 12454#ifndef OBJ_AOUT
718ddfc0 12455 if (IS_ELF
252b5132
RH
12456#ifdef TE_PE
12457 || OUTPUT_FLAVOR == bfd_target_coff_flavour
12458#endif
12459 )
12460 value += fixP->fx_where + fixP->fx_frag->fr_address;
12461#endif
12462#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12463 if (IS_ELF)
252b5132 12464 {
6539b54b 12465 segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
2f66722d 12466
6539b54b 12467 if ((sym_seg == seg
2f66722d 12468 || (symbol_section_p (fixP->fx_addsy)
6539b54b 12469 && sym_seg != absolute_section))
af65af87 12470 && !generic_force_reloc (fixP))
2f66722d
AM
12471 {
12472 /* Yes, we add the values in twice. This is because
6539b54b
AM
12473 bfd_install_relocation subtracts them out again. I think
12474 bfd_install_relocation is broken, but I don't dare change
2f66722d
AM
12475 it. FIXME. */
12476 value += fixP->fx_where + fixP->fx_frag->fr_address;
12477 }
252b5132
RH
12478 }
12479#endif
12480#if defined (OBJ_COFF) && defined (TE_PE)
977cdf5a
NC
12481 /* For some reason, the PE format does not store a
12482 section address offset for a PC relative symbol. */
12483 if (S_GET_SEGMENT (fixP->fx_addsy) != seg
7be1c489 12484 || S_IS_WEAK (fixP->fx_addsy))
252b5132
RH
12485 value += md_pcrel_from (fixP);
12486#endif
12487 }
fbeb56a4 12488#if defined (OBJ_COFF) && defined (TE_PE)
f01c1a09
NC
12489 if (fixP->fx_addsy != NULL
12490 && S_IS_WEAK (fixP->fx_addsy)
12491 /* PR 16858: Do not modify weak function references. */
12492 && ! fixP->fx_pcrel)
fbeb56a4 12493 {
296a8689
NC
12494#if !defined (TE_PEP)
12495 /* For x86 PE weak function symbols are neither PC-relative
12496 nor do they set S_IS_FUNCTION. So the only reliable way
12497 to detect them is to check the flags of their containing
12498 section. */
12499 if (S_GET_SEGMENT (fixP->fx_addsy) != NULL
12500 && S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_CODE)
12501 ;
12502 else
12503#endif
fbeb56a4
DK
12504 value -= S_GET_VALUE (fixP->fx_addsy);
12505 }
12506#endif
252b5132
RH
12507
12508 /* Fix a few things - the dynamic linker expects certain values here,
0234cb7c 12509 and we must not disappoint it. */
252b5132 12510#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12511 if (IS_ELF && fixP->fx_addsy)
47926f60
KH
12512 switch (fixP->fx_r_type)
12513 {
12514 case BFD_RELOC_386_PLT32:
3e73aa7c 12515 case BFD_RELOC_X86_64_PLT32:
b9519cfe
L
12516 /* Make the jump instruction point to the address of the operand.
12517 At runtime we merely add the offset to the actual PLT entry.
12518 NB: Subtract the offset size only for jump instructions. */
12519 if (fixP->fx_pcrel)
12520 value = -4;
47926f60 12521 break;
31312f95 12522
13ae64f3
JJ
12523 case BFD_RELOC_386_TLS_GD:
12524 case BFD_RELOC_386_TLS_LDM:
13ae64f3 12525 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
12526 case BFD_RELOC_386_TLS_IE:
12527 case BFD_RELOC_386_TLS_GOTIE:
67a4f2b7 12528 case BFD_RELOC_386_TLS_GOTDESC:
bffbf940
JJ
12529 case BFD_RELOC_X86_64_TLSGD:
12530 case BFD_RELOC_X86_64_TLSLD:
12531 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7 12532 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
00f7efb6
JJ
12533 value = 0; /* Fully resolved at runtime. No addend. */
12534 /* Fallthrough */
12535 case BFD_RELOC_386_TLS_LE:
12536 case BFD_RELOC_386_TLS_LDO_32:
12537 case BFD_RELOC_386_TLS_LE_32:
12538 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 12539 case BFD_RELOC_X86_64_DTPOFF64:
00f7efb6 12540 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113 12541 case BFD_RELOC_X86_64_TPOFF64:
00f7efb6
JJ
12542 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12543 break;
12544
67a4f2b7
AO
12545 case BFD_RELOC_386_TLS_DESC_CALL:
12546 case BFD_RELOC_X86_64_TLSDESC_CALL:
12547 value = 0; /* Fully resolved at runtime. No addend. */
12548 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12549 fixP->fx_done = 0;
12550 return;
12551
47926f60
KH
12552 case BFD_RELOC_VTABLE_INHERIT:
12553 case BFD_RELOC_VTABLE_ENTRY:
12554 fixP->fx_done = 0;
94f592af 12555 return;
47926f60
KH
12556
12557 default:
12558 break;
12559 }
12560#endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) */
c6682705 12561 *valP = value;
f86103b7 12562#endif /* !defined (TE_Mach) */
3e73aa7c 12563
3e73aa7c 12564 /* Are we finished with this relocation now? */
c6682705 12565 if (fixP->fx_addsy == NULL)
3e73aa7c 12566 fixP->fx_done = 1;
fbeb56a4
DK
12567#if defined (OBJ_COFF) && defined (TE_PE)
12568 else if (fixP->fx_addsy != NULL && S_IS_WEAK (fixP->fx_addsy))
12569 {
12570 fixP->fx_done = 0;
12571 /* Remember value for tc_gen_reloc. */
12572 fixP->fx_addnumber = value;
12573 /* Clear out the frag for now. */
12574 value = 0;
12575 }
12576#endif
3e73aa7c
JH
12577 else if (use_rela_relocations)
12578 {
12579 fixP->fx_no_overflow = 1;
062cd5e7
AS
12580 /* Remember value for tc_gen_reloc. */
12581 fixP->fx_addnumber = value;
3e73aa7c
JH
12582 value = 0;
12583 }
f86103b7 12584
94f592af 12585 md_number_to_chars (p, value, fixP->fx_size);
252b5132 12586}
252b5132 12587\f
6d4af3c2 12588const char *
499ac353 12589md_atof (int type, char *litP, int *sizeP)
252b5132 12590{
499ac353
NC
12591 /* This outputs the LITTLENUMs in REVERSE order;
12592 in accord with the bigendian 386. */
12593 return ieee_md_atof (type, litP, sizeP, FALSE);
252b5132
RH
12594}
12595\f
2d545b82 12596static char output_invalid_buf[sizeof (unsigned char) * 2 + 6];
252b5132 12597
252b5132 12598static char *
e3bb37b5 12599output_invalid (int c)
252b5132 12600{
3882b010 12601 if (ISPRINT (c))
f9f21a03
L
12602 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
12603 "'%c'", c);
252b5132 12604 else
f9f21a03 12605 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
2d545b82 12606 "(0x%x)", (unsigned char) c);
252b5132
RH
12607 return output_invalid_buf;
12608}
12609
8a6fb3f9
JB
12610/* Verify that @r can be used in the current context. */
12611
12612static bfd_boolean check_register (const reg_entry *r)
12613{
12614 if (allow_pseudo_reg)
12615 return TRUE;
12616
12617 if (operand_type_all_zero (&r->reg_type))
12618 return FALSE;
12619
12620 if ((r->reg_type.bitfield.dword
12621 || (r->reg_type.bitfield.class == SReg && r->reg_num > 3)
12622 || r->reg_type.bitfield.class == RegCR
22e00a3f 12623 || r->reg_type.bitfield.class == RegDR)
8a6fb3f9
JB
12624 && !cpu_arch_flags.bitfield.cpui386)
12625 return FALSE;
12626
22e00a3f
JB
12627 if (r->reg_type.bitfield.class == RegTR
12628 && (flag_code == CODE_64BIT
12629 || !cpu_arch_flags.bitfield.cpui386
12630 || cpu_arch_isa_flags.bitfield.cpui586
12631 || cpu_arch_isa_flags.bitfield.cpui686))
12632 return FALSE;
12633
8a6fb3f9
JB
12634 if (r->reg_type.bitfield.class == RegMMX && !cpu_arch_flags.bitfield.cpummx)
12635 return FALSE;
12636
12637 if (!cpu_arch_flags.bitfield.cpuavx512f)
12638 {
12639 if (r->reg_type.bitfield.zmmword
12640 || r->reg_type.bitfield.class == RegMask)
12641 return FALSE;
12642
12643 if (!cpu_arch_flags.bitfield.cpuavx)
12644 {
12645 if (r->reg_type.bitfield.ymmword)
12646 return FALSE;
12647
12648 if (!cpu_arch_flags.bitfield.cpusse && r->reg_type.bitfield.xmmword)
12649 return FALSE;
12650 }
12651 }
12652
260cd341
LC
12653 if (r->reg_type.bitfield.tmmword
12654 && (!cpu_arch_flags.bitfield.cpuamx_tile
12655 || flag_code != CODE_64BIT))
12656 return FALSE;
12657
8a6fb3f9
JB
12658 if (r->reg_type.bitfield.class == RegBND && !cpu_arch_flags.bitfield.cpumpx)
12659 return FALSE;
12660
12661 /* Don't allow fake index register unless allow_index_reg isn't 0. */
12662 if (!allow_index_reg && r->reg_num == RegIZ)
12663 return FALSE;
12664
12665 /* Upper 16 vector registers are only available with VREX in 64bit
12666 mode, and require EVEX encoding. */
12667 if (r->reg_flags & RegVRex)
12668 {
12669 if (!cpu_arch_flags.bitfield.cpuavx512f
12670 || flag_code != CODE_64BIT)
12671 return FALSE;
12672
da4977e0
JB
12673 if (i.vec_encoding == vex_encoding_default)
12674 i.vec_encoding = vex_encoding_evex;
12675 else if (i.vec_encoding != vex_encoding_evex)
12676 i.vec_encoding = vex_encoding_error;
8a6fb3f9
JB
12677 }
12678
12679 if (((r->reg_flags & (RegRex64 | RegRex)) || r->reg_type.bitfield.qword)
12680 && (!cpu_arch_flags.bitfield.cpulm || r->reg_type.bitfield.class != RegCR)
12681 && flag_code != CODE_64BIT)
12682 return FALSE;
12683
12684 if (r->reg_type.bitfield.class == SReg && r->reg_num == RegFlat
12685 && !intel_syntax)
12686 return FALSE;
12687
12688 return TRUE;
12689}
12690
af6bdddf 12691/* REG_STRING starts *before* REGISTER_PREFIX. */
252b5132
RH
12692
12693static const reg_entry *
4d1bb795 12694parse_real_register (char *reg_string, char **end_op)
252b5132 12695{
af6bdddf
AM
12696 char *s = reg_string;
12697 char *p;
252b5132
RH
12698 char reg_name_given[MAX_REG_NAME_SIZE + 1];
12699 const reg_entry *r;
12700
12701 /* Skip possible REGISTER_PREFIX and possible whitespace. */
12702 if (*s == REGISTER_PREFIX)
12703 ++s;
12704
12705 if (is_space_char (*s))
12706 ++s;
12707
12708 p = reg_name_given;
af6bdddf 12709 while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
252b5132
RH
12710 {
12711 if (p >= reg_name_given + MAX_REG_NAME_SIZE)
af6bdddf
AM
12712 return (const reg_entry *) NULL;
12713 s++;
252b5132
RH
12714 }
12715
6588847e
DN
12716 /* For naked regs, make sure that we are not dealing with an identifier.
12717 This prevents confusing an identifier like `eax_var' with register
12718 `eax'. */
12719 if (allow_naked_reg && identifier_chars[(unsigned char) *s])
12720 return (const reg_entry *) NULL;
12721
af6bdddf 12722 *end_op = s;
252b5132 12723
629310ab 12724 r = (const reg_entry *) str_hash_find (reg_hash, reg_name_given);
252b5132 12725
5f47d35b 12726 /* Handle floating point regs, allowing spaces in the (i) part. */
47926f60 12727 if (r == i386_regtab /* %st is first entry of table */)
5f47d35b 12728 {
0e0eea78
JB
12729 if (!cpu_arch_flags.bitfield.cpu8087
12730 && !cpu_arch_flags.bitfield.cpu287
af32b722
JB
12731 && !cpu_arch_flags.bitfield.cpu387
12732 && !allow_pseudo_reg)
0e0eea78
JB
12733 return (const reg_entry *) NULL;
12734
5f47d35b
AM
12735 if (is_space_char (*s))
12736 ++s;
12737 if (*s == '(')
12738 {
af6bdddf 12739 ++s;
5f47d35b
AM
12740 if (is_space_char (*s))
12741 ++s;
12742 if (*s >= '0' && *s <= '7')
12743 {
db557034 12744 int fpr = *s - '0';
af6bdddf 12745 ++s;
5f47d35b
AM
12746 if (is_space_char (*s))
12747 ++s;
12748 if (*s == ')')
12749 {
12750 *end_op = s + 1;
629310ab 12751 r = (const reg_entry *) str_hash_find (reg_hash, "st(0)");
db557034
AM
12752 know (r);
12753 return r + fpr;
5f47d35b 12754 }
5f47d35b 12755 }
47926f60 12756 /* We have "%st(" then garbage. */
5f47d35b
AM
12757 return (const reg_entry *) NULL;
12758 }
12759 }
12760
8a6fb3f9 12761 return r && check_register (r) ? r : NULL;
252b5132 12762}
4d1bb795
JB
12763
12764/* REG_STRING starts *before* REGISTER_PREFIX. */
12765
12766static const reg_entry *
12767parse_register (char *reg_string, char **end_op)
12768{
12769 const reg_entry *r;
12770
12771 if (*reg_string == REGISTER_PREFIX || allow_naked_reg)
12772 r = parse_real_register (reg_string, end_op);
12773 else
12774 r = NULL;
12775 if (!r)
12776 {
12777 char *save = input_line_pointer;
12778 char c;
12779 symbolS *symbolP;
12780
12781 input_line_pointer = reg_string;
d02603dc 12782 c = get_symbol_name (&reg_string);
4d1bb795
JB
12783 symbolP = symbol_find (reg_string);
12784 if (symbolP && S_GET_SEGMENT (symbolP) == reg_section)
12785 {
12786 const expressionS *e = symbol_get_value_expression (symbolP);
12787
0398aac5 12788 know (e->X_op == O_register);
4eed87de 12789 know (e->X_add_number >= 0
c3fe08fa 12790 && (valueT) e->X_add_number < i386_regtab_size);
4d1bb795 12791 r = i386_regtab + e->X_add_number;
8a6fb3f9
JB
12792 if (!check_register (r))
12793 {
12794 as_bad (_("register '%s%s' cannot be used here"),
12795 register_prefix, r->reg_name);
12796 r = &bad_reg;
12797 }
4d1bb795
JB
12798 *end_op = input_line_pointer;
12799 }
12800 *input_line_pointer = c;
12801 input_line_pointer = save;
12802 }
12803 return r;
12804}
12805
12806int
12807i386_parse_name (char *name, expressionS *e, char *nextcharP)
12808{
12809 const reg_entry *r;
12810 char *end = input_line_pointer;
12811
12812 *end = *nextcharP;
12813 r = parse_register (name, &input_line_pointer);
12814 if (r && end <= input_line_pointer)
12815 {
12816 *nextcharP = *input_line_pointer;
12817 *input_line_pointer = 0;
8a6fb3f9
JB
12818 if (r != &bad_reg)
12819 {
12820 e->X_op = O_register;
12821 e->X_add_number = r - i386_regtab;
12822 }
12823 else
12824 e->X_op = O_illegal;
4d1bb795
JB
12825 return 1;
12826 }
12827 input_line_pointer = end;
12828 *end = 0;
ee86248c 12829 return intel_syntax ? i386_intel_parse_name (name, e) : 0;
4d1bb795
JB
12830}
12831
12832void
12833md_operand (expressionS *e)
12834{
ee86248c
JB
12835 char *end;
12836 const reg_entry *r;
4d1bb795 12837
ee86248c
JB
12838 switch (*input_line_pointer)
12839 {
12840 case REGISTER_PREFIX:
12841 r = parse_real_register (input_line_pointer, &end);
4d1bb795
JB
12842 if (r)
12843 {
12844 e->X_op = O_register;
12845 e->X_add_number = r - i386_regtab;
12846 input_line_pointer = end;
12847 }
ee86248c
JB
12848 break;
12849
12850 case '[':
9c2799c2 12851 gas_assert (intel_syntax);
ee86248c
JB
12852 end = input_line_pointer++;
12853 expression (e);
12854 if (*input_line_pointer == ']')
12855 {
12856 ++input_line_pointer;
12857 e->X_op_symbol = make_expr_symbol (e);
12858 e->X_add_symbol = NULL;
12859 e->X_add_number = 0;
12860 e->X_op = O_index;
12861 }
12862 else
12863 {
12864 e->X_op = O_absent;
12865 input_line_pointer = end;
12866 }
12867 break;
4d1bb795
JB
12868 }
12869}
12870
252b5132 12871\f
4cc782b5 12872#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
b6f8c7c4 12873const char *md_shortopts = "kVQ:sqnO::";
252b5132 12874#else
b6f8c7c4 12875const char *md_shortopts = "qnO::";
252b5132 12876#endif
6e0b89ee 12877
3e73aa7c 12878#define OPTION_32 (OPTION_MD_BASE + 0)
b3b91714
AM
12879#define OPTION_64 (OPTION_MD_BASE + 1)
12880#define OPTION_DIVIDE (OPTION_MD_BASE + 2)
9103f4f4
L
12881#define OPTION_MARCH (OPTION_MD_BASE + 3)
12882#define OPTION_MTUNE (OPTION_MD_BASE + 4)
1efbbeb4
L
12883#define OPTION_MMNEMONIC (OPTION_MD_BASE + 5)
12884#define OPTION_MSYNTAX (OPTION_MD_BASE + 6)
12885#define OPTION_MINDEX_REG (OPTION_MD_BASE + 7)
12886#define OPTION_MNAKED_REG (OPTION_MD_BASE + 8)
bd5dea88 12887#define OPTION_MRELAX_RELOCATIONS (OPTION_MD_BASE + 9)
c0f3af97 12888#define OPTION_MSSE2AVX (OPTION_MD_BASE + 10)
daf50ae7 12889#define OPTION_MSSE_CHECK (OPTION_MD_BASE + 11)
7bab8ab5
JB
12890#define OPTION_MOPERAND_CHECK (OPTION_MD_BASE + 12)
12891#define OPTION_MAVXSCALAR (OPTION_MD_BASE + 13)
12892#define OPTION_X32 (OPTION_MD_BASE + 14)
7e8b059b 12893#define OPTION_MADD_BND_PREFIX (OPTION_MD_BASE + 15)
43234a1e
L
12894#define OPTION_MEVEXLIG (OPTION_MD_BASE + 16)
12895#define OPTION_MEVEXWIG (OPTION_MD_BASE + 17)
167ad85b 12896#define OPTION_MBIG_OBJ (OPTION_MD_BASE + 18)
d1982f93 12897#define OPTION_MOMIT_LOCK_PREFIX (OPTION_MD_BASE + 19)
d3d3c6db 12898#define OPTION_MEVEXRCIG (OPTION_MD_BASE + 20)
8dcea932 12899#define OPTION_MSHARED (OPTION_MD_BASE + 21)
5db04b09
L
12900#define OPTION_MAMD64 (OPTION_MD_BASE + 22)
12901#define OPTION_MINTEL64 (OPTION_MD_BASE + 23)
e4e00185 12902#define OPTION_MFENCE_AS_LOCK_ADD (OPTION_MD_BASE + 24)
b4a3a7b4 12903#define OPTION_X86_USED_NOTE (OPTION_MD_BASE + 25)
03751133 12904#define OPTION_MVEXWIG (OPTION_MD_BASE + 26)
e379e5f3
L
12905#define OPTION_MALIGN_BRANCH_BOUNDARY (OPTION_MD_BASE + 27)
12906#define OPTION_MALIGN_BRANCH_PREFIX_SIZE (OPTION_MD_BASE + 28)
12907#define OPTION_MALIGN_BRANCH (OPTION_MD_BASE + 29)
76cf450b 12908#define OPTION_MBRANCHES_WITH_32B_BOUNDARIES (OPTION_MD_BASE + 30)
ae531041
L
12909#define OPTION_MLFENCE_AFTER_LOAD (OPTION_MD_BASE + 31)
12910#define OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH (OPTION_MD_BASE + 32)
12911#define OPTION_MLFENCE_BEFORE_RET (OPTION_MD_BASE + 33)
b3b91714 12912
99ad8390
NC
12913struct option md_longopts[] =
12914{
3e73aa7c 12915 {"32", no_argument, NULL, OPTION_32},
321098a5 12916#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 12917 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c 12918 {"64", no_argument, NULL, OPTION_64},
351f65ca
L
12919#endif
12920#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 12921 {"x32", no_argument, NULL, OPTION_X32},
8dcea932 12922 {"mshared", no_argument, NULL, OPTION_MSHARED},
b4a3a7b4 12923 {"mx86-used-note", required_argument, NULL, OPTION_X86_USED_NOTE},
6e0b89ee 12924#endif
b3b91714 12925 {"divide", no_argument, NULL, OPTION_DIVIDE},
9103f4f4
L
12926 {"march", required_argument, NULL, OPTION_MARCH},
12927 {"mtune", required_argument, NULL, OPTION_MTUNE},
1efbbeb4
L
12928 {"mmnemonic", required_argument, NULL, OPTION_MMNEMONIC},
12929 {"msyntax", required_argument, NULL, OPTION_MSYNTAX},
12930 {"mindex-reg", no_argument, NULL, OPTION_MINDEX_REG},
12931 {"mnaked-reg", no_argument, NULL, OPTION_MNAKED_REG},
c0f3af97 12932 {"msse2avx", no_argument, NULL, OPTION_MSSE2AVX},
daf50ae7 12933 {"msse-check", required_argument, NULL, OPTION_MSSE_CHECK},
7bab8ab5 12934 {"moperand-check", required_argument, NULL, OPTION_MOPERAND_CHECK},
539f890d 12935 {"mavxscalar", required_argument, NULL, OPTION_MAVXSCALAR},
03751133 12936 {"mvexwig", required_argument, NULL, OPTION_MVEXWIG},
7e8b059b 12937 {"madd-bnd-prefix", no_argument, NULL, OPTION_MADD_BND_PREFIX},
43234a1e
L
12938 {"mevexlig", required_argument, NULL, OPTION_MEVEXLIG},
12939 {"mevexwig", required_argument, NULL, OPTION_MEVEXWIG},
167ad85b
TG
12940# if defined (TE_PE) || defined (TE_PEP)
12941 {"mbig-obj", no_argument, NULL, OPTION_MBIG_OBJ},
12942#endif
d1982f93 12943 {"momit-lock-prefix", required_argument, NULL, OPTION_MOMIT_LOCK_PREFIX},
e4e00185 12944 {"mfence-as-lock-add", required_argument, NULL, OPTION_MFENCE_AS_LOCK_ADD},
0cb4071e 12945 {"mrelax-relocations", required_argument, NULL, OPTION_MRELAX_RELOCATIONS},
d3d3c6db 12946 {"mevexrcig", required_argument, NULL, OPTION_MEVEXRCIG},
e379e5f3
L
12947 {"malign-branch-boundary", required_argument, NULL, OPTION_MALIGN_BRANCH_BOUNDARY},
12948 {"malign-branch-prefix-size", required_argument, NULL, OPTION_MALIGN_BRANCH_PREFIX_SIZE},
12949 {"malign-branch", required_argument, NULL, OPTION_MALIGN_BRANCH},
76cf450b 12950 {"mbranches-within-32B-boundaries", no_argument, NULL, OPTION_MBRANCHES_WITH_32B_BOUNDARIES},
ae531041
L
12951 {"mlfence-after-load", required_argument, NULL, OPTION_MLFENCE_AFTER_LOAD},
12952 {"mlfence-before-indirect-branch", required_argument, NULL,
12953 OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH},
12954 {"mlfence-before-ret", required_argument, NULL, OPTION_MLFENCE_BEFORE_RET},
5db04b09
L
12955 {"mamd64", no_argument, NULL, OPTION_MAMD64},
12956 {"mintel64", no_argument, NULL, OPTION_MINTEL64},
252b5132
RH
12957 {NULL, no_argument, NULL, 0}
12958};
12959size_t md_longopts_size = sizeof (md_longopts);
12960
12961int
17b9d67d 12962md_parse_option (int c, const char *arg)
252b5132 12963{
91d6fa6a 12964 unsigned int j;
e379e5f3 12965 char *arch, *next, *saved, *type;
9103f4f4 12966
252b5132
RH
12967 switch (c)
12968 {
12b55ccc
L
12969 case 'n':
12970 optimize_align_code = 0;
12971 break;
12972
a38cf1db
AM
12973 case 'q':
12974 quiet_warnings = 1;
252b5132
RH
12975 break;
12976
12977#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
a38cf1db
AM
12978 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
12979 should be emitted or not. FIXME: Not implemented. */
12980 case 'Q':
d4693039
JB
12981 if ((arg[0] != 'y' && arg[0] != 'n') || arg[1])
12982 return 0;
252b5132
RH
12983 break;
12984
12985 /* -V: SVR4 argument to print version ID. */
12986 case 'V':
12987 print_version_id ();
12988 break;
12989
a38cf1db
AM
12990 /* -k: Ignore for FreeBSD compatibility. */
12991 case 'k':
252b5132 12992 break;
4cc782b5
ILT
12993
12994 case 's':
12995 /* -s: On i386 Solaris, this tells the native assembler to use
29b0f896 12996 .stab instead of .stab.excl. We always use .stab anyhow. */
4cc782b5 12997 break;
8dcea932
L
12998
12999 case OPTION_MSHARED:
13000 shared = 1;
13001 break;
b4a3a7b4
L
13002
13003 case OPTION_X86_USED_NOTE:
13004 if (strcasecmp (arg, "yes") == 0)
13005 x86_used_note = 1;
13006 else if (strcasecmp (arg, "no") == 0)
13007 x86_used_note = 0;
13008 else
13009 as_fatal (_("invalid -mx86-used-note= option: `%s'"), arg);
13010 break;
13011
13012
99ad8390 13013#endif
321098a5 13014#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 13015 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c
JH
13016 case OPTION_64:
13017 {
13018 const char **list, **l;
13019
3e73aa7c
JH
13020 list = bfd_target_list ();
13021 for (l = list; *l != NULL; l++)
08dedd66 13022 if (startswith (*l, "elf64-x86-64")
99ad8390
NC
13023 || strcmp (*l, "coff-x86-64") == 0
13024 || strcmp (*l, "pe-x86-64") == 0
d382c579
TG
13025 || strcmp (*l, "pei-x86-64") == 0
13026 || strcmp (*l, "mach-o-x86-64") == 0)
6e0b89ee
AM
13027 {
13028 default_arch = "x86_64";
13029 break;
13030 }
3e73aa7c 13031 if (*l == NULL)
2b5d6a91 13032 as_fatal (_("no compiled in support for x86_64"));
3e73aa7c
JH
13033 free (list);
13034 }
13035 break;
13036#endif
252b5132 13037
351f65ca 13038#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 13039 case OPTION_X32:
351f65ca
L
13040 if (IS_ELF)
13041 {
13042 const char **list, **l;
13043
13044 list = bfd_target_list ();
13045 for (l = list; *l != NULL; l++)
08dedd66 13046 if (startswith (*l, "elf32-x86-64"))
351f65ca
L
13047 {
13048 default_arch = "x86_64:32";
13049 break;
13050 }
13051 if (*l == NULL)
2b5d6a91 13052 as_fatal (_("no compiled in support for 32bit x86_64"));
351f65ca
L
13053 free (list);
13054 }
13055 else
13056 as_fatal (_("32bit x86_64 is only supported for ELF"));
13057 break;
13058#endif
13059
6e0b89ee
AM
13060 case OPTION_32:
13061 default_arch = "i386";
13062 break;
13063
b3b91714
AM
13064 case OPTION_DIVIDE:
13065#ifdef SVR4_COMMENT_CHARS
13066 {
13067 char *n, *t;
13068 const char *s;
13069
add39d23 13070 n = XNEWVEC (char, strlen (i386_comment_chars) + 1);
b3b91714
AM
13071 t = n;
13072 for (s = i386_comment_chars; *s != '\0'; s++)
13073 if (*s != '/')
13074 *t++ = *s;
13075 *t = '\0';
13076 i386_comment_chars = n;
13077 }
13078#endif
13079 break;
13080
9103f4f4 13081 case OPTION_MARCH:
293f5f65
L
13082 saved = xstrdup (arg);
13083 arch = saved;
13084 /* Allow -march=+nosse. */
13085 if (*arch == '+')
13086 arch++;
6305a203 13087 do
9103f4f4 13088 {
6305a203 13089 if (*arch == '.')
2b5d6a91 13090 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
13091 next = strchr (arch, '+');
13092 if (next)
13093 *next++ = '\0';
91d6fa6a 13094 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 13095 {
91d6fa6a 13096 if (strcmp (arch, cpu_arch [j].name) == 0)
ccc9c027 13097 {
6305a203 13098 /* Processor. */
1ded5609
JB
13099 if (! cpu_arch[j].flags.bitfield.cpui386)
13100 continue;
13101
91d6fa6a 13102 cpu_arch_name = cpu_arch[j].name;
6305a203 13103 cpu_sub_arch_name = NULL;
91d6fa6a
NC
13104 cpu_arch_flags = cpu_arch[j].flags;
13105 cpu_arch_isa = cpu_arch[j].type;
13106 cpu_arch_isa_flags = cpu_arch[j].flags;
6305a203
L
13107 if (!cpu_arch_tune_set)
13108 {
13109 cpu_arch_tune = cpu_arch_isa;
13110 cpu_arch_tune_flags = cpu_arch_isa_flags;
13111 }
13112 break;
13113 }
91d6fa6a
NC
13114 else if (*cpu_arch [j].name == '.'
13115 && strcmp (arch, cpu_arch [j].name + 1) == 0)
6305a203 13116 {
33eaf5de 13117 /* ISA extension. */
6305a203 13118 i386_cpu_flags flags;
309d3373 13119
293f5f65
L
13120 flags = cpu_flags_or (cpu_arch_flags,
13121 cpu_arch[j].flags);
81486035 13122
5b64d091 13123 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
6305a203
L
13124 {
13125 if (cpu_sub_arch_name)
13126 {
13127 char *name = cpu_sub_arch_name;
13128 cpu_sub_arch_name = concat (name,
91d6fa6a 13129 cpu_arch[j].name,
1bf57e9f 13130 (const char *) NULL);
6305a203
L
13131 free (name);
13132 }
13133 else
91d6fa6a 13134 cpu_sub_arch_name = xstrdup (cpu_arch[j].name);
6305a203 13135 cpu_arch_flags = flags;
a586129e 13136 cpu_arch_isa_flags = flags;
6305a203 13137 }
0089dace
L
13138 else
13139 cpu_arch_isa_flags
13140 = cpu_flags_or (cpu_arch_isa_flags,
13141 cpu_arch[j].flags);
6305a203 13142 break;
ccc9c027 13143 }
9103f4f4 13144 }
6305a203 13145
293f5f65
L
13146 if (j >= ARRAY_SIZE (cpu_arch))
13147 {
33eaf5de 13148 /* Disable an ISA extension. */
293f5f65
L
13149 for (j = 0; j < ARRAY_SIZE (cpu_noarch); j++)
13150 if (strcmp (arch, cpu_noarch [j].name) == 0)
13151 {
13152 i386_cpu_flags flags;
13153
13154 flags = cpu_flags_and_not (cpu_arch_flags,
13155 cpu_noarch[j].flags);
13156 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
13157 {
13158 if (cpu_sub_arch_name)
13159 {
13160 char *name = cpu_sub_arch_name;
13161 cpu_sub_arch_name = concat (arch,
13162 (const char *) NULL);
13163 free (name);
13164 }
13165 else
13166 cpu_sub_arch_name = xstrdup (arch);
13167 cpu_arch_flags = flags;
13168 cpu_arch_isa_flags = flags;
13169 }
13170 break;
13171 }
13172
13173 if (j >= ARRAY_SIZE (cpu_noarch))
13174 j = ARRAY_SIZE (cpu_arch);
13175 }
13176
91d6fa6a 13177 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 13178 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
13179
13180 arch = next;
9103f4f4 13181 }
293f5f65
L
13182 while (next != NULL);
13183 free (saved);
9103f4f4
L
13184 break;
13185
13186 case OPTION_MTUNE:
13187 if (*arg == '.')
2b5d6a91 13188 as_fatal (_("invalid -mtune= option: `%s'"), arg);
91d6fa6a 13189 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 13190 {
91d6fa6a 13191 if (strcmp (arg, cpu_arch [j].name) == 0)
9103f4f4 13192 {
ccc9c027 13193 cpu_arch_tune_set = 1;
91d6fa6a
NC
13194 cpu_arch_tune = cpu_arch [j].type;
13195 cpu_arch_tune_flags = cpu_arch[j].flags;
9103f4f4
L
13196 break;
13197 }
13198 }
91d6fa6a 13199 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 13200 as_fatal (_("invalid -mtune= option: `%s'"), arg);
9103f4f4
L
13201 break;
13202
1efbbeb4
L
13203 case OPTION_MMNEMONIC:
13204 if (strcasecmp (arg, "att") == 0)
13205 intel_mnemonic = 0;
13206 else if (strcasecmp (arg, "intel") == 0)
13207 intel_mnemonic = 1;
13208 else
2b5d6a91 13209 as_fatal (_("invalid -mmnemonic= option: `%s'"), arg);
1efbbeb4
L
13210 break;
13211
13212 case OPTION_MSYNTAX:
13213 if (strcasecmp (arg, "att") == 0)
13214 intel_syntax = 0;
13215 else if (strcasecmp (arg, "intel") == 0)
13216 intel_syntax = 1;
13217 else
2b5d6a91 13218 as_fatal (_("invalid -msyntax= option: `%s'"), arg);
1efbbeb4
L
13219 break;
13220
13221 case OPTION_MINDEX_REG:
13222 allow_index_reg = 1;
13223 break;
13224
13225 case OPTION_MNAKED_REG:
13226 allow_naked_reg = 1;
13227 break;
13228
c0f3af97
L
13229 case OPTION_MSSE2AVX:
13230 sse2avx = 1;
13231 break;
13232
daf50ae7
L
13233 case OPTION_MSSE_CHECK:
13234 if (strcasecmp (arg, "error") == 0)
7bab8ab5 13235 sse_check = check_error;
daf50ae7 13236 else if (strcasecmp (arg, "warning") == 0)
7bab8ab5 13237 sse_check = check_warning;
daf50ae7 13238 else if (strcasecmp (arg, "none") == 0)
7bab8ab5 13239 sse_check = check_none;
daf50ae7 13240 else
2b5d6a91 13241 as_fatal (_("invalid -msse-check= option: `%s'"), arg);
daf50ae7
L
13242 break;
13243
7bab8ab5
JB
13244 case OPTION_MOPERAND_CHECK:
13245 if (strcasecmp (arg, "error") == 0)
13246 operand_check = check_error;
13247 else if (strcasecmp (arg, "warning") == 0)
13248 operand_check = check_warning;
13249 else if (strcasecmp (arg, "none") == 0)
13250 operand_check = check_none;
13251 else
13252 as_fatal (_("invalid -moperand-check= option: `%s'"), arg);
13253 break;
13254
539f890d
L
13255 case OPTION_MAVXSCALAR:
13256 if (strcasecmp (arg, "128") == 0)
13257 avxscalar = vex128;
13258 else if (strcasecmp (arg, "256") == 0)
13259 avxscalar = vex256;
13260 else
2b5d6a91 13261 as_fatal (_("invalid -mavxscalar= option: `%s'"), arg);
539f890d
L
13262 break;
13263
03751133
L
13264 case OPTION_MVEXWIG:
13265 if (strcmp (arg, "0") == 0)
40c9c8de 13266 vexwig = vexw0;
03751133 13267 else if (strcmp (arg, "1") == 0)
40c9c8de 13268 vexwig = vexw1;
03751133
L
13269 else
13270 as_fatal (_("invalid -mvexwig= option: `%s'"), arg);
13271 break;
13272
7e8b059b
L
13273 case OPTION_MADD_BND_PREFIX:
13274 add_bnd_prefix = 1;
13275 break;
13276
43234a1e
L
13277 case OPTION_MEVEXLIG:
13278 if (strcmp (arg, "128") == 0)
13279 evexlig = evexl128;
13280 else if (strcmp (arg, "256") == 0)
13281 evexlig = evexl256;
13282 else if (strcmp (arg, "512") == 0)
13283 evexlig = evexl512;
13284 else
13285 as_fatal (_("invalid -mevexlig= option: `%s'"), arg);
13286 break;
13287
d3d3c6db
IT
13288 case OPTION_MEVEXRCIG:
13289 if (strcmp (arg, "rne") == 0)
13290 evexrcig = rne;
13291 else if (strcmp (arg, "rd") == 0)
13292 evexrcig = rd;
13293 else if (strcmp (arg, "ru") == 0)
13294 evexrcig = ru;
13295 else if (strcmp (arg, "rz") == 0)
13296 evexrcig = rz;
13297 else
13298 as_fatal (_("invalid -mevexrcig= option: `%s'"), arg);
13299 break;
13300
43234a1e
L
13301 case OPTION_MEVEXWIG:
13302 if (strcmp (arg, "0") == 0)
13303 evexwig = evexw0;
13304 else if (strcmp (arg, "1") == 0)
13305 evexwig = evexw1;
13306 else
13307 as_fatal (_("invalid -mevexwig= option: `%s'"), arg);
13308 break;
13309
167ad85b
TG
13310# if defined (TE_PE) || defined (TE_PEP)
13311 case OPTION_MBIG_OBJ:
13312 use_big_obj = 1;
13313 break;
13314#endif
13315
d1982f93 13316 case OPTION_MOMIT_LOCK_PREFIX:
d022bddd
IT
13317 if (strcasecmp (arg, "yes") == 0)
13318 omit_lock_prefix = 1;
13319 else if (strcasecmp (arg, "no") == 0)
13320 omit_lock_prefix = 0;
13321 else
13322 as_fatal (_("invalid -momit-lock-prefix= option: `%s'"), arg);
13323 break;
13324
e4e00185
AS
13325 case OPTION_MFENCE_AS_LOCK_ADD:
13326 if (strcasecmp (arg, "yes") == 0)
13327 avoid_fence = 1;
13328 else if (strcasecmp (arg, "no") == 0)
13329 avoid_fence = 0;
13330 else
13331 as_fatal (_("invalid -mfence-as-lock-add= option: `%s'"), arg);
13332 break;
13333
ae531041
L
13334 case OPTION_MLFENCE_AFTER_LOAD:
13335 if (strcasecmp (arg, "yes") == 0)
13336 lfence_after_load = 1;
13337 else if (strcasecmp (arg, "no") == 0)
13338 lfence_after_load = 0;
13339 else
13340 as_fatal (_("invalid -mlfence-after-load= option: `%s'"), arg);
13341 break;
13342
13343 case OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH:
13344 if (strcasecmp (arg, "all") == 0)
a09f656b 13345 {
13346 lfence_before_indirect_branch = lfence_branch_all;
13347 if (lfence_before_ret == lfence_before_ret_none)
13348 lfence_before_ret = lfence_before_ret_shl;
13349 }
ae531041
L
13350 else if (strcasecmp (arg, "memory") == 0)
13351 lfence_before_indirect_branch = lfence_branch_memory;
13352 else if (strcasecmp (arg, "register") == 0)
13353 lfence_before_indirect_branch = lfence_branch_register;
13354 else if (strcasecmp (arg, "none") == 0)
13355 lfence_before_indirect_branch = lfence_branch_none;
13356 else
13357 as_fatal (_("invalid -mlfence-before-indirect-branch= option: `%s'"),
13358 arg);
13359 break;
13360
13361 case OPTION_MLFENCE_BEFORE_RET:
13362 if (strcasecmp (arg, "or") == 0)
13363 lfence_before_ret = lfence_before_ret_or;
13364 else if (strcasecmp (arg, "not") == 0)
13365 lfence_before_ret = lfence_before_ret_not;
a09f656b 13366 else if (strcasecmp (arg, "shl") == 0 || strcasecmp (arg, "yes") == 0)
13367 lfence_before_ret = lfence_before_ret_shl;
ae531041
L
13368 else if (strcasecmp (arg, "none") == 0)
13369 lfence_before_ret = lfence_before_ret_none;
13370 else
13371 as_fatal (_("invalid -mlfence-before-ret= option: `%s'"),
13372 arg);
13373 break;
13374
0cb4071e
L
13375 case OPTION_MRELAX_RELOCATIONS:
13376 if (strcasecmp (arg, "yes") == 0)
13377 generate_relax_relocations = 1;
13378 else if (strcasecmp (arg, "no") == 0)
13379 generate_relax_relocations = 0;
13380 else
13381 as_fatal (_("invalid -mrelax-relocations= option: `%s'"), arg);
13382 break;
13383
e379e5f3
L
13384 case OPTION_MALIGN_BRANCH_BOUNDARY:
13385 {
13386 char *end;
13387 long int align = strtoul (arg, &end, 0);
13388 if (*end == '\0')
13389 {
13390 if (align == 0)
13391 {
13392 align_branch_power = 0;
13393 break;
13394 }
13395 else if (align >= 16)
13396 {
13397 int align_power;
13398 for (align_power = 0;
13399 (align & 1) == 0;
13400 align >>= 1, align_power++)
13401 continue;
13402 /* Limit alignment power to 31. */
13403 if (align == 1 && align_power < 32)
13404 {
13405 align_branch_power = align_power;
13406 break;
13407 }
13408 }
13409 }
13410 as_fatal (_("invalid -malign-branch-boundary= value: %s"), arg);
13411 }
13412 break;
13413
13414 case OPTION_MALIGN_BRANCH_PREFIX_SIZE:
13415 {
13416 char *end;
13417 int align = strtoul (arg, &end, 0);
13418 /* Some processors only support 5 prefixes. */
13419 if (*end == '\0' && align >= 0 && align < 6)
13420 {
13421 align_branch_prefix_size = align;
13422 break;
13423 }
13424 as_fatal (_("invalid -malign-branch-prefix-size= value: %s"),
13425 arg);
13426 }
13427 break;
13428
13429 case OPTION_MALIGN_BRANCH:
13430 align_branch = 0;
13431 saved = xstrdup (arg);
13432 type = saved;
13433 do
13434 {
13435 next = strchr (type, '+');
13436 if (next)
13437 *next++ = '\0';
13438 if (strcasecmp (type, "jcc") == 0)
13439 align_branch |= align_branch_jcc_bit;
13440 else if (strcasecmp (type, "fused") == 0)
13441 align_branch |= align_branch_fused_bit;
13442 else if (strcasecmp (type, "jmp") == 0)
13443 align_branch |= align_branch_jmp_bit;
13444 else if (strcasecmp (type, "call") == 0)
13445 align_branch |= align_branch_call_bit;
13446 else if (strcasecmp (type, "ret") == 0)
13447 align_branch |= align_branch_ret_bit;
13448 else if (strcasecmp (type, "indirect") == 0)
13449 align_branch |= align_branch_indirect_bit;
13450 else
13451 as_fatal (_("invalid -malign-branch= option: `%s'"), arg);
13452 type = next;
13453 }
13454 while (next != NULL);
13455 free (saved);
13456 break;
13457
76cf450b
L
13458 case OPTION_MBRANCHES_WITH_32B_BOUNDARIES:
13459 align_branch_power = 5;
13460 align_branch_prefix_size = 5;
13461 align_branch = (align_branch_jcc_bit
13462 | align_branch_fused_bit
13463 | align_branch_jmp_bit);
13464 break;
13465
5db04b09 13466 case OPTION_MAMD64:
4b5aaf5f 13467 isa64 = amd64;
5db04b09
L
13468 break;
13469
13470 case OPTION_MINTEL64:
4b5aaf5f 13471 isa64 = intel64;
5db04b09
L
13472 break;
13473
b6f8c7c4
L
13474 case 'O':
13475 if (arg == NULL)
13476 {
13477 optimize = 1;
13478 /* Turn off -Os. */
13479 optimize_for_space = 0;
13480 }
13481 else if (*arg == 's')
13482 {
13483 optimize_for_space = 1;
13484 /* Turn on all encoding optimizations. */
41fd2579 13485 optimize = INT_MAX;
b6f8c7c4
L
13486 }
13487 else
13488 {
13489 optimize = atoi (arg);
13490 /* Turn off -Os. */
13491 optimize_for_space = 0;
13492 }
13493 break;
13494
252b5132
RH
13495 default:
13496 return 0;
13497 }
13498 return 1;
13499}
13500
8a2c8fef
L
13501#define MESSAGE_TEMPLATE \
13502" "
13503
293f5f65
L
13504static char *
13505output_message (FILE *stream, char *p, char *message, char *start,
13506 int *left_p, const char *name, int len)
13507{
13508 int size = sizeof (MESSAGE_TEMPLATE);
13509 int left = *left_p;
13510
13511 /* Reserve 2 spaces for ", " or ",\0" */
13512 left -= len + 2;
13513
13514 /* Check if there is any room. */
13515 if (left >= 0)
13516 {
13517 if (p != start)
13518 {
13519 *p++ = ',';
13520 *p++ = ' ';
13521 }
13522 p = mempcpy (p, name, len);
13523 }
13524 else
13525 {
13526 /* Output the current message now and start a new one. */
13527 *p++ = ',';
13528 *p = '\0';
13529 fprintf (stream, "%s\n", message);
13530 p = start;
13531 left = size - (start - message) - len - 2;
13532
13533 gas_assert (left >= 0);
13534
13535 p = mempcpy (p, name, len);
13536 }
13537
13538 *left_p = left;
13539 return p;
13540}
13541
8a2c8fef 13542static void
1ded5609 13543show_arch (FILE *stream, int ext, int check)
8a2c8fef
L
13544{
13545 static char message[] = MESSAGE_TEMPLATE;
13546 char *start = message + 27;
13547 char *p;
13548 int size = sizeof (MESSAGE_TEMPLATE);
13549 int left;
13550 const char *name;
13551 int len;
13552 unsigned int j;
13553
13554 p = start;
13555 left = size - (start - message);
13556 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13557 {
13558 /* Should it be skipped? */
13559 if (cpu_arch [j].skip)
13560 continue;
13561
13562 name = cpu_arch [j].name;
13563 len = cpu_arch [j].len;
13564 if (*name == '.')
13565 {
13566 /* It is an extension. Skip if we aren't asked to show it. */
13567 if (ext)
13568 {
13569 name++;
13570 len--;
13571 }
13572 else
13573 continue;
13574 }
13575 else if (ext)
13576 {
13577 /* It is an processor. Skip if we show only extension. */
13578 continue;
13579 }
1ded5609
JB
13580 else if (check && ! cpu_arch[j].flags.bitfield.cpui386)
13581 {
13582 /* It is an impossible processor - skip. */
13583 continue;
13584 }
8a2c8fef 13585
293f5f65 13586 p = output_message (stream, p, message, start, &left, name, len);
8a2c8fef
L
13587 }
13588
293f5f65
L
13589 /* Display disabled extensions. */
13590 if (ext)
13591 for (j = 0; j < ARRAY_SIZE (cpu_noarch); j++)
13592 {
13593 name = cpu_noarch [j].name;
13594 len = cpu_noarch [j].len;
13595 p = output_message (stream, p, message, start, &left, name,
13596 len);
13597 }
13598
8a2c8fef
L
13599 *p = '\0';
13600 fprintf (stream, "%s\n", message);
13601}
13602
252b5132 13603void
8a2c8fef 13604md_show_usage (FILE *stream)
252b5132 13605{
4cc782b5
ILT
13606#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13607 fprintf (stream, _("\
d4693039 13608 -Qy, -Qn ignored\n\
a38cf1db 13609 -V print assembler version number\n\
b3b91714
AM
13610 -k ignored\n"));
13611#endif
13612 fprintf (stream, _("\
12b55ccc 13613 -n Do not optimize code alignment\n\
b3b91714
AM
13614 -q quieten some warnings\n"));
13615#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13616 fprintf (stream, _("\
a38cf1db 13617 -s ignored\n"));
b3b91714 13618#endif
d7f449c0
L
13619#if defined BFD64 && (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
13620 || defined (TE_PE) || defined (TE_PEP))
751d281c 13621 fprintf (stream, _("\
570561f7 13622 --32/--64/--x32 generate 32bit/64bit/x32 code\n"));
751d281c 13623#endif
b3b91714
AM
13624#ifdef SVR4_COMMENT_CHARS
13625 fprintf (stream, _("\
13626 --divide do not treat `/' as a comment character\n"));
a38cf1db
AM
13627#else
13628 fprintf (stream, _("\
b3b91714 13629 --divide ignored\n"));
4cc782b5 13630#endif
9103f4f4 13631 fprintf (stream, _("\
6305a203 13632 -march=CPU[,+EXTENSION...]\n\
8a2c8fef 13633 generate code for CPU and EXTENSION, CPU is one of:\n"));
1ded5609 13634 show_arch (stream, 0, 1);
8a2c8fef
L
13635 fprintf (stream, _("\
13636 EXTENSION is combination of:\n"));
1ded5609 13637 show_arch (stream, 1, 0);
6305a203 13638 fprintf (stream, _("\
8a2c8fef 13639 -mtune=CPU optimize for CPU, CPU is one of:\n"));
1ded5609 13640 show_arch (stream, 0, 0);
ba104c83 13641 fprintf (stream, _("\
c0f3af97
L
13642 -msse2avx encode SSE instructions with VEX prefix\n"));
13643 fprintf (stream, _("\
7c5c05ef 13644 -msse-check=[none|error|warning] (default: warning)\n\
daf50ae7
L
13645 check SSE instructions\n"));
13646 fprintf (stream, _("\
7c5c05ef 13647 -moperand-check=[none|error|warning] (default: warning)\n\
7bab8ab5
JB
13648 check operand combinations for validity\n"));
13649 fprintf (stream, _("\
7c5c05ef
L
13650 -mavxscalar=[128|256] (default: 128)\n\
13651 encode scalar AVX instructions with specific vector\n\
539f890d
L
13652 length\n"));
13653 fprintf (stream, _("\
03751133
L
13654 -mvexwig=[0|1] (default: 0)\n\
13655 encode VEX instructions with specific VEX.W value\n\
13656 for VEX.W bit ignored instructions\n"));
13657 fprintf (stream, _("\
7c5c05ef
L
13658 -mevexlig=[128|256|512] (default: 128)\n\
13659 encode scalar EVEX instructions with specific vector\n\
43234a1e
L
13660 length\n"));
13661 fprintf (stream, _("\
7c5c05ef
L
13662 -mevexwig=[0|1] (default: 0)\n\
13663 encode EVEX instructions with specific EVEX.W value\n\
43234a1e
L
13664 for EVEX.W bit ignored instructions\n"));
13665 fprintf (stream, _("\
7c5c05ef 13666 -mevexrcig=[rne|rd|ru|rz] (default: rne)\n\
d3d3c6db
IT
13667 encode EVEX instructions with specific EVEX.RC value\n\
13668 for SAE-only ignored instructions\n"));
13669 fprintf (stream, _("\
7c5c05ef
L
13670 -mmnemonic=[att|intel] "));
13671 if (SYSV386_COMPAT)
13672 fprintf (stream, _("(default: att)\n"));
13673 else
13674 fprintf (stream, _("(default: intel)\n"));
13675 fprintf (stream, _("\
13676 use AT&T/Intel mnemonic\n"));
ba104c83 13677 fprintf (stream, _("\
7c5c05ef
L
13678 -msyntax=[att|intel] (default: att)\n\
13679 use AT&T/Intel syntax\n"));
ba104c83
L
13680 fprintf (stream, _("\
13681 -mindex-reg support pseudo index registers\n"));
13682 fprintf (stream, _("\
13683 -mnaked-reg don't require `%%' prefix for registers\n"));
13684 fprintf (stream, _("\
7e8b059b 13685 -madd-bnd-prefix add BND prefix for all valid branches\n"));
b4a3a7b4 13686#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8dcea932
L
13687 fprintf (stream, _("\
13688 -mshared disable branch optimization for shared code\n"));
b4a3a7b4
L
13689 fprintf (stream, _("\
13690 -mx86-used-note=[no|yes] "));
13691 if (DEFAULT_X86_USED_NOTE)
13692 fprintf (stream, _("(default: yes)\n"));
13693 else
13694 fprintf (stream, _("(default: no)\n"));
13695 fprintf (stream, _("\
13696 generate x86 used ISA and feature properties\n"));
13697#endif
13698#if defined (TE_PE) || defined (TE_PEP)
167ad85b
TG
13699 fprintf (stream, _("\
13700 -mbig-obj generate big object files\n"));
13701#endif
d022bddd 13702 fprintf (stream, _("\
7c5c05ef 13703 -momit-lock-prefix=[no|yes] (default: no)\n\
d022bddd 13704 strip all lock prefixes\n"));
5db04b09 13705 fprintf (stream, _("\
7c5c05ef 13706 -mfence-as-lock-add=[no|yes] (default: no)\n\
e4e00185
AS
13707 encode lfence, mfence and sfence as\n\
13708 lock addl $0x0, (%%{re}sp)\n"));
13709 fprintf (stream, _("\
7c5c05ef
L
13710 -mrelax-relocations=[no|yes] "));
13711 if (DEFAULT_GENERATE_X86_RELAX_RELOCATIONS)
13712 fprintf (stream, _("(default: yes)\n"));
13713 else
13714 fprintf (stream, _("(default: no)\n"));
13715 fprintf (stream, _("\
0cb4071e
L
13716 generate relax relocations\n"));
13717 fprintf (stream, _("\
e379e5f3
L
13718 -malign-branch-boundary=NUM (default: 0)\n\
13719 align branches within NUM byte boundary\n"));
13720 fprintf (stream, _("\
13721 -malign-branch=TYPE[+TYPE...] (default: jcc+fused+jmp)\n\
13722 TYPE is combination of jcc, fused, jmp, call, ret,\n\
13723 indirect\n\
13724 specify types of branches to align\n"));
13725 fprintf (stream, _("\
13726 -malign-branch-prefix-size=NUM (default: 5)\n\
13727 align branches with NUM prefixes per instruction\n"));
13728 fprintf (stream, _("\
76cf450b
L
13729 -mbranches-within-32B-boundaries\n\
13730 align branches within 32 byte boundary\n"));
13731 fprintf (stream, _("\
ae531041
L
13732 -mlfence-after-load=[no|yes] (default: no)\n\
13733 generate lfence after load\n"));
13734 fprintf (stream, _("\
13735 -mlfence-before-indirect-branch=[none|all|register|memory] (default: none)\n\
13736 generate lfence before indirect near branch\n"));
13737 fprintf (stream, _("\
a09f656b 13738 -mlfence-before-ret=[none|or|not|shl|yes] (default: none)\n\
ae531041
L
13739 generate lfence before ret\n"));
13740 fprintf (stream, _("\
7c5c05ef 13741 -mamd64 accept only AMD64 ISA [default]\n"));
5db04b09
L
13742 fprintf (stream, _("\
13743 -mintel64 accept only Intel64 ISA\n"));
252b5132
RH
13744}
13745
3e73aa7c 13746#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
321098a5 13747 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
e57f8c65 13748 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
252b5132
RH
13749
13750/* Pick the target format to use. */
13751
47926f60 13752const char *
e3bb37b5 13753i386_target_format (void)
252b5132 13754{
351f65ca
L
13755 if (!strncmp (default_arch, "x86_64", 6))
13756 {
13757 update_code_flag (CODE_64BIT, 1);
13758 if (default_arch[6] == '\0')
7f56bc95 13759 x86_elf_abi = X86_64_ABI;
351f65ca 13760 else
7f56bc95 13761 x86_elf_abi = X86_64_X32_ABI;
351f65ca 13762 }
3e73aa7c 13763 else if (!strcmp (default_arch, "i386"))
78f12dd3 13764 update_code_flag (CODE_32BIT, 1);
5197d474
L
13765 else if (!strcmp (default_arch, "iamcu"))
13766 {
13767 update_code_flag (CODE_32BIT, 1);
13768 if (cpu_arch_isa == PROCESSOR_UNKNOWN)
13769 {
13770 static const i386_cpu_flags iamcu_flags = CPU_IAMCU_FLAGS;
13771 cpu_arch_name = "iamcu";
13772 cpu_sub_arch_name = NULL;
13773 cpu_arch_flags = iamcu_flags;
13774 cpu_arch_isa = PROCESSOR_IAMCU;
13775 cpu_arch_isa_flags = iamcu_flags;
13776 if (!cpu_arch_tune_set)
13777 {
13778 cpu_arch_tune = cpu_arch_isa;
13779 cpu_arch_tune_flags = cpu_arch_isa_flags;
13780 }
13781 }
8d471ec1 13782 else if (cpu_arch_isa != PROCESSOR_IAMCU)
5197d474
L
13783 as_fatal (_("Intel MCU doesn't support `%s' architecture"),
13784 cpu_arch_name);
13785 }
3e73aa7c 13786 else
2b5d6a91 13787 as_fatal (_("unknown architecture"));
89507696
JB
13788
13789 if (cpu_flags_all_zero (&cpu_arch_isa_flags))
13790 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].flags;
13791 if (cpu_flags_all_zero (&cpu_arch_tune_flags))
13792 cpu_arch_tune_flags = cpu_arch[flag_code == CODE_64BIT].flags;
13793
252b5132
RH
13794 switch (OUTPUT_FLAVOR)
13795 {
9384f2ff 13796#if defined (OBJ_MAYBE_AOUT) || defined (OBJ_AOUT)
4c63da97 13797 case bfd_target_aout_flavour:
47926f60 13798 return AOUT_TARGET_FORMAT;
4c63da97 13799#endif
9384f2ff
AM
13800#if defined (OBJ_MAYBE_COFF) || defined (OBJ_COFF)
13801# if defined (TE_PE) || defined (TE_PEP)
13802 case bfd_target_coff_flavour:
167ad85b
TG
13803 if (flag_code == CODE_64BIT)
13804 return use_big_obj ? "pe-bigobj-x86-64" : "pe-x86-64";
13805 else
251dae91 13806 return use_big_obj ? "pe-bigobj-i386" : "pe-i386";
9384f2ff 13807# elif defined (TE_GO32)
0561d57c
JK
13808 case bfd_target_coff_flavour:
13809 return "coff-go32";
9384f2ff 13810# else
252b5132
RH
13811 case bfd_target_coff_flavour:
13812 return "coff-i386";
9384f2ff 13813# endif
4c63da97 13814#endif
3e73aa7c 13815#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
252b5132 13816 case bfd_target_elf_flavour:
3e73aa7c 13817 {
351f65ca
L
13818 const char *format;
13819
13820 switch (x86_elf_abi)
4fa24527 13821 {
351f65ca
L
13822 default:
13823 format = ELF_TARGET_FORMAT;
e379e5f3
L
13824#ifndef TE_SOLARIS
13825 tls_get_addr = "___tls_get_addr";
13826#endif
351f65ca 13827 break;
7f56bc95 13828 case X86_64_ABI:
351f65ca 13829 use_rela_relocations = 1;
4fa24527 13830 object_64bit = 1;
e379e5f3
L
13831#ifndef TE_SOLARIS
13832 tls_get_addr = "__tls_get_addr";
13833#endif
351f65ca
L
13834 format = ELF_TARGET_FORMAT64;
13835 break;
7f56bc95 13836 case X86_64_X32_ABI:
4fa24527 13837 use_rela_relocations = 1;
351f65ca 13838 object_64bit = 1;
e379e5f3
L
13839#ifndef TE_SOLARIS
13840 tls_get_addr = "__tls_get_addr";
13841#endif
862be3fb 13842 disallow_64bit_reloc = 1;
351f65ca
L
13843 format = ELF_TARGET_FORMAT32;
13844 break;
4fa24527 13845 }
3632d14b 13846 if (cpu_arch_isa == PROCESSOR_L1OM)
8a9036a4 13847 {
7f56bc95 13848 if (x86_elf_abi != X86_64_ABI)
8a9036a4
L
13849 as_fatal (_("Intel L1OM is 64bit only"));
13850 return ELF_TARGET_L1OM_FORMAT;
13851 }
b49f93f6 13852 else if (cpu_arch_isa == PROCESSOR_K1OM)
7a9068fe
L
13853 {
13854 if (x86_elf_abi != X86_64_ABI)
13855 as_fatal (_("Intel K1OM is 64bit only"));
13856 return ELF_TARGET_K1OM_FORMAT;
13857 }
81486035
L
13858 else if (cpu_arch_isa == PROCESSOR_IAMCU)
13859 {
13860 if (x86_elf_abi != I386_ABI)
13861 as_fatal (_("Intel MCU is 32bit only"));
13862 return ELF_TARGET_IAMCU_FORMAT;
13863 }
8a9036a4 13864 else
351f65ca 13865 return format;
3e73aa7c 13866 }
e57f8c65
TG
13867#endif
13868#if defined (OBJ_MACH_O)
13869 case bfd_target_mach_o_flavour:
d382c579
TG
13870 if (flag_code == CODE_64BIT)
13871 {
13872 use_rela_relocations = 1;
13873 object_64bit = 1;
13874 return "mach-o-x86-64";
13875 }
13876 else
13877 return "mach-o-i386";
4c63da97 13878#endif
252b5132
RH
13879 default:
13880 abort ();
13881 return NULL;
13882 }
13883}
13884
47926f60 13885#endif /* OBJ_MAYBE_ more than one */
252b5132 13886\f
252b5132 13887symbolS *
7016a5d5 13888md_undefined_symbol (char *name)
252b5132 13889{
18dc2407
ILT
13890 if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
13891 && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
13892 && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
13893 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
24eab124
AM
13894 {
13895 if (!GOT_symbol)
13896 {
13897 if (symbol_find (name))
13898 as_bad (_("GOT already in symbol table"));
13899 GOT_symbol = symbol_new (name, undefined_section,
e01e1cee 13900 &zero_address_frag, 0);
24eab124
AM
13901 };
13902 return GOT_symbol;
13903 }
252b5132
RH
13904 return 0;
13905}
13906
13907/* Round up a section size to the appropriate boundary. */
47926f60 13908
252b5132 13909valueT
7016a5d5 13910md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
252b5132 13911{
4c63da97
AM
13912#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
13913 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
13914 {
13915 /* For a.out, force the section size to be aligned. If we don't do
13916 this, BFD will align it for us, but it will not write out the
13917 final bytes of the section. This may be a bug in BFD, but it is
13918 easier to fix it here since that is how the other a.out targets
13919 work. */
13920 int align;
13921
fd361982 13922 align = bfd_section_alignment (segment);
8d3842cd 13923 size = ((size + (1 << align) - 1) & (-((valueT) 1 << align)));
4c63da97 13924 }
252b5132
RH
13925#endif
13926
13927 return size;
13928}
13929
13930/* On the i386, PC-relative offsets are relative to the start of the
13931 next instruction. That is, the address of the offset, plus its
13932 size, since the offset is always the last part of the insn. */
13933
13934long
e3bb37b5 13935md_pcrel_from (fixS *fixP)
252b5132
RH
13936{
13937 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
13938}
13939
13940#ifndef I386COFF
13941
13942static void
e3bb37b5 13943s_bss (int ignore ATTRIBUTE_UNUSED)
252b5132 13944{
29b0f896 13945 int temp;
252b5132 13946
8a75718c
JB
13947#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13948 if (IS_ELF)
13949 obj_elf_section_change_hook ();
13950#endif
252b5132
RH
13951 temp = get_absolute_expression ();
13952 subseg_set (bss_section, (subsegT) temp);
13953 demand_empty_rest_of_line ();
13954}
13955
13956#endif
13957
e379e5f3
L
13958/* Remember constant directive. */
13959
13960void
13961i386_cons_align (int ignore ATTRIBUTE_UNUSED)
13962{
13963 if (last_insn.kind != last_insn_directive
13964 && (bfd_section_flags (now_seg) & SEC_CODE))
13965 {
13966 last_insn.seg = now_seg;
13967 last_insn.kind = last_insn_directive;
13968 last_insn.name = "constant directive";
13969 last_insn.file = as_where (&last_insn.line);
ae531041
L
13970 if (lfence_before_ret != lfence_before_ret_none)
13971 {
13972 if (lfence_before_indirect_branch != lfence_branch_none)
13973 as_warn (_("constant directive skips -mlfence-before-ret "
13974 "and -mlfence-before-indirect-branch"));
13975 else
13976 as_warn (_("constant directive skips -mlfence-before-ret"));
13977 }
13978 else if (lfence_before_indirect_branch != lfence_branch_none)
13979 as_warn (_("constant directive skips -mlfence-before-indirect-branch"));
e379e5f3
L
13980 }
13981}
13982
252b5132 13983void
e3bb37b5 13984i386_validate_fix (fixS *fixp)
252b5132 13985{
02a86693 13986 if (fixp->fx_subsy)
252b5132 13987 {
02a86693 13988 if (fixp->fx_subsy == GOT_symbol)
23df1078 13989 {
02a86693
L
13990 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
13991 {
13992 if (!object_64bit)
13993 abort ();
13994#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13995 if (fixp->fx_tcbit2)
56ceb5b5
L
13996 fixp->fx_r_type = (fixp->fx_tcbit
13997 ? BFD_RELOC_X86_64_REX_GOTPCRELX
13998 : BFD_RELOC_X86_64_GOTPCRELX);
02a86693
L
13999 else
14000#endif
14001 fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
14002 }
d6ab8113 14003 else
02a86693
L
14004 {
14005 if (!object_64bit)
14006 fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
14007 else
14008 fixp->fx_r_type = BFD_RELOC_X86_64_GOTOFF64;
14009 }
14010 fixp->fx_subsy = 0;
23df1078 14011 }
252b5132 14012 }
02a86693 14013#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2585b7a5 14014 else
02a86693 14015 {
2585b7a5
L
14016 /* NB: Commit 292676c1 resolved PLT32 reloc aganst local symbol
14017 to section. Since PLT32 relocation must be against symbols,
14018 turn such PLT32 relocation into PC32 relocation. */
14019 if (fixp->fx_addsy
14020 && (fixp->fx_r_type == BFD_RELOC_386_PLT32
14021 || fixp->fx_r_type == BFD_RELOC_X86_64_PLT32)
14022 && symbol_section_p (fixp->fx_addsy))
14023 fixp->fx_r_type = BFD_RELOC_32_PCREL;
14024 if (!object_64bit)
14025 {
14026 if (fixp->fx_r_type == BFD_RELOC_386_GOT32
14027 && fixp->fx_tcbit2)
14028 fixp->fx_r_type = BFD_RELOC_386_GOT32X;
14029 }
02a86693
L
14030 }
14031#endif
252b5132
RH
14032}
14033
252b5132 14034arelent *
7016a5d5 14035tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
14036{
14037 arelent *rel;
14038 bfd_reloc_code_real_type code;
14039
14040 switch (fixp->fx_r_type)
14041 {
8ce3d284 14042#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
14043 case BFD_RELOC_SIZE32:
14044 case BFD_RELOC_SIZE64:
14045 if (S_IS_DEFINED (fixp->fx_addsy)
14046 && !S_IS_EXTERNAL (fixp->fx_addsy))
14047 {
14048 /* Resolve size relocation against local symbol to size of
14049 the symbol plus addend. */
14050 valueT value = S_GET_SIZE (fixp->fx_addsy) + fixp->fx_offset;
14051 if (fixp->fx_r_type == BFD_RELOC_SIZE32
14052 && !fits_in_unsigned_long (value))
14053 as_bad_where (fixp->fx_file, fixp->fx_line,
14054 _("symbol size computation overflow"));
14055 fixp->fx_addsy = NULL;
14056 fixp->fx_subsy = NULL;
14057 md_apply_fix (fixp, (valueT *) &value, NULL);
14058 return NULL;
14059 }
8ce3d284 14060#endif
1a0670f3 14061 /* Fall through. */
8fd4256d 14062
3e73aa7c
JH
14063 case BFD_RELOC_X86_64_PLT32:
14064 case BFD_RELOC_X86_64_GOT32:
14065 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
14066 case BFD_RELOC_X86_64_GOTPCRELX:
14067 case BFD_RELOC_X86_64_REX_GOTPCRELX:
252b5132
RH
14068 case BFD_RELOC_386_PLT32:
14069 case BFD_RELOC_386_GOT32:
02a86693 14070 case BFD_RELOC_386_GOT32X:
252b5132
RH
14071 case BFD_RELOC_386_GOTOFF:
14072 case BFD_RELOC_386_GOTPC:
13ae64f3
JJ
14073 case BFD_RELOC_386_TLS_GD:
14074 case BFD_RELOC_386_TLS_LDM:
14075 case BFD_RELOC_386_TLS_LDO_32:
14076 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
14077 case BFD_RELOC_386_TLS_IE:
14078 case BFD_RELOC_386_TLS_GOTIE:
13ae64f3
JJ
14079 case BFD_RELOC_386_TLS_LE_32:
14080 case BFD_RELOC_386_TLS_LE:
67a4f2b7
AO
14081 case BFD_RELOC_386_TLS_GOTDESC:
14082 case BFD_RELOC_386_TLS_DESC_CALL:
bffbf940
JJ
14083 case BFD_RELOC_X86_64_TLSGD:
14084 case BFD_RELOC_X86_64_TLSLD:
14085 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 14086 case BFD_RELOC_X86_64_DTPOFF64:
bffbf940
JJ
14087 case BFD_RELOC_X86_64_GOTTPOFF:
14088 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113
JB
14089 case BFD_RELOC_X86_64_TPOFF64:
14090 case BFD_RELOC_X86_64_GOTOFF64:
14091 case BFD_RELOC_X86_64_GOTPC32:
7b81dfbb
AJ
14092 case BFD_RELOC_X86_64_GOT64:
14093 case BFD_RELOC_X86_64_GOTPCREL64:
14094 case BFD_RELOC_X86_64_GOTPC64:
14095 case BFD_RELOC_X86_64_GOTPLT64:
14096 case BFD_RELOC_X86_64_PLTOFF64:
67a4f2b7
AO
14097 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14098 case BFD_RELOC_X86_64_TLSDESC_CALL:
252b5132
RH
14099 case BFD_RELOC_RVA:
14100 case BFD_RELOC_VTABLE_ENTRY:
14101 case BFD_RELOC_VTABLE_INHERIT:
6482c264
NC
14102#ifdef TE_PE
14103 case BFD_RELOC_32_SECREL:
14104#endif
252b5132
RH
14105 code = fixp->fx_r_type;
14106 break;
dbbaec26
L
14107 case BFD_RELOC_X86_64_32S:
14108 if (!fixp->fx_pcrel)
14109 {
14110 /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32. */
14111 code = fixp->fx_r_type;
14112 break;
14113 }
1a0670f3 14114 /* Fall through. */
252b5132 14115 default:
93382f6d 14116 if (fixp->fx_pcrel)
252b5132 14117 {
93382f6d
AM
14118 switch (fixp->fx_size)
14119 {
14120 default:
b091f402
AM
14121 as_bad_where (fixp->fx_file, fixp->fx_line,
14122 _("can not do %d byte pc-relative relocation"),
14123 fixp->fx_size);
93382f6d
AM
14124 code = BFD_RELOC_32_PCREL;
14125 break;
14126 case 1: code = BFD_RELOC_8_PCREL; break;
14127 case 2: code = BFD_RELOC_16_PCREL; break;
d258b828 14128 case 4: code = BFD_RELOC_32_PCREL; break;
d6ab8113
JB
14129#ifdef BFD64
14130 case 8: code = BFD_RELOC_64_PCREL; break;
14131#endif
93382f6d
AM
14132 }
14133 }
14134 else
14135 {
14136 switch (fixp->fx_size)
14137 {
14138 default:
b091f402
AM
14139 as_bad_where (fixp->fx_file, fixp->fx_line,
14140 _("can not do %d byte relocation"),
14141 fixp->fx_size);
93382f6d
AM
14142 code = BFD_RELOC_32;
14143 break;
14144 case 1: code = BFD_RELOC_8; break;
14145 case 2: code = BFD_RELOC_16; break;
14146 case 4: code = BFD_RELOC_32; break;
937149dd 14147#ifdef BFD64
3e73aa7c 14148 case 8: code = BFD_RELOC_64; break;
937149dd 14149#endif
93382f6d 14150 }
252b5132
RH
14151 }
14152 break;
14153 }
252b5132 14154
d182319b
JB
14155 if ((code == BFD_RELOC_32
14156 || code == BFD_RELOC_32_PCREL
14157 || code == BFD_RELOC_X86_64_32S)
252b5132
RH
14158 && GOT_symbol
14159 && fixp->fx_addsy == GOT_symbol)
3e73aa7c 14160 {
4fa24527 14161 if (!object_64bit)
d6ab8113
JB
14162 code = BFD_RELOC_386_GOTPC;
14163 else
14164 code = BFD_RELOC_X86_64_GOTPC32;
3e73aa7c 14165 }
7b81dfbb
AJ
14166 if ((code == BFD_RELOC_64 || code == BFD_RELOC_64_PCREL)
14167 && GOT_symbol
14168 && fixp->fx_addsy == GOT_symbol)
14169 {
14170 code = BFD_RELOC_X86_64_GOTPC64;
14171 }
252b5132 14172
add39d23
TS
14173 rel = XNEW (arelent);
14174 rel->sym_ptr_ptr = XNEW (asymbol *);
49309057 14175 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
14176
14177 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
c87db184 14178
3e73aa7c
JH
14179 if (!use_rela_relocations)
14180 {
14181 /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
14182 vtable entry to be used in the relocation's section offset. */
14183 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
14184 rel->address = fixp->fx_offset;
fbeb56a4
DK
14185#if defined (OBJ_COFF) && defined (TE_PE)
14186 else if (fixp->fx_addsy && S_IS_WEAK (fixp->fx_addsy))
14187 rel->addend = fixp->fx_addnumber - (S_GET_VALUE (fixp->fx_addsy) * 2);
14188 else
14189#endif
c6682705 14190 rel->addend = 0;
3e73aa7c
JH
14191 }
14192 /* Use the rela in 64bit mode. */
252b5132 14193 else
3e73aa7c 14194 {
862be3fb
L
14195 if (disallow_64bit_reloc)
14196 switch (code)
14197 {
862be3fb
L
14198 case BFD_RELOC_X86_64_DTPOFF64:
14199 case BFD_RELOC_X86_64_TPOFF64:
14200 case BFD_RELOC_64_PCREL:
14201 case BFD_RELOC_X86_64_GOTOFF64:
14202 case BFD_RELOC_X86_64_GOT64:
14203 case BFD_RELOC_X86_64_GOTPCREL64:
14204 case BFD_RELOC_X86_64_GOTPC64:
14205 case BFD_RELOC_X86_64_GOTPLT64:
14206 case BFD_RELOC_X86_64_PLTOFF64:
14207 as_bad_where (fixp->fx_file, fixp->fx_line,
14208 _("cannot represent relocation type %s in x32 mode"),
14209 bfd_get_reloc_code_name (code));
14210 break;
14211 default:
14212 break;
14213 }
14214
062cd5e7
AS
14215 if (!fixp->fx_pcrel)
14216 rel->addend = fixp->fx_offset;
14217 else
14218 switch (code)
14219 {
14220 case BFD_RELOC_X86_64_PLT32:
14221 case BFD_RELOC_X86_64_GOT32:
14222 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
14223 case BFD_RELOC_X86_64_GOTPCRELX:
14224 case BFD_RELOC_X86_64_REX_GOTPCRELX:
bffbf940
JJ
14225 case BFD_RELOC_X86_64_TLSGD:
14226 case BFD_RELOC_X86_64_TLSLD:
14227 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7
AO
14228 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14229 case BFD_RELOC_X86_64_TLSDESC_CALL:
062cd5e7
AS
14230 rel->addend = fixp->fx_offset - fixp->fx_size;
14231 break;
14232 default:
14233 rel->addend = (section->vma
14234 - fixp->fx_size
14235 + fixp->fx_addnumber
14236 + md_pcrel_from (fixp));
14237 break;
14238 }
3e73aa7c
JH
14239 }
14240
252b5132
RH
14241 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
14242 if (rel->howto == NULL)
14243 {
14244 as_bad_where (fixp->fx_file, fixp->fx_line,
d0b47220 14245 _("cannot represent relocation type %s"),
252b5132
RH
14246 bfd_get_reloc_code_name (code));
14247 /* Set howto to a garbage value so that we can keep going. */
14248 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
9c2799c2 14249 gas_assert (rel->howto != NULL);
252b5132
RH
14250 }
14251
14252 return rel;
14253}
14254
ee86248c 14255#include "tc-i386-intel.c"
54cfded0 14256
a60de03c
JB
14257void
14258tc_x86_parse_to_dw2regnum (expressionS *exp)
54cfded0 14259{
a60de03c
JB
14260 int saved_naked_reg;
14261 char saved_register_dot;
54cfded0 14262
a60de03c
JB
14263 saved_naked_reg = allow_naked_reg;
14264 allow_naked_reg = 1;
14265 saved_register_dot = register_chars['.'];
14266 register_chars['.'] = '.';
14267 allow_pseudo_reg = 1;
14268 expression_and_evaluate (exp);
14269 allow_pseudo_reg = 0;
14270 register_chars['.'] = saved_register_dot;
14271 allow_naked_reg = saved_naked_reg;
14272
e96d56a1 14273 if (exp->X_op == O_register && exp->X_add_number >= 0)
54cfded0 14274 {
a60de03c
JB
14275 if ((addressT) exp->X_add_number < i386_regtab_size)
14276 {
14277 exp->X_op = O_constant;
14278 exp->X_add_number = i386_regtab[exp->X_add_number]
14279 .dw2_regnum[flag_code >> 1];
14280 }
14281 else
14282 exp->X_op = O_illegal;
54cfded0 14283 }
54cfded0
AM
14284}
14285
14286void
14287tc_x86_frame_initial_instructions (void)
14288{
a60de03c
JB
14289 static unsigned int sp_regno[2];
14290
14291 if (!sp_regno[flag_code >> 1])
14292 {
14293 char *saved_input = input_line_pointer;
14294 char sp[][4] = {"esp", "rsp"};
14295 expressionS exp;
a4447b93 14296
a60de03c
JB
14297 input_line_pointer = sp[flag_code >> 1];
14298 tc_x86_parse_to_dw2regnum (&exp);
9c2799c2 14299 gas_assert (exp.X_op == O_constant);
a60de03c
JB
14300 sp_regno[flag_code >> 1] = exp.X_add_number;
14301 input_line_pointer = saved_input;
14302 }
a4447b93 14303
61ff971f
L
14304 cfi_add_CFA_def_cfa (sp_regno[flag_code >> 1], -x86_cie_data_alignment);
14305 cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
54cfded0 14306}
d2b2c203 14307
d7921315
L
14308int
14309x86_dwarf2_addr_size (void)
14310{
14311#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
14312 if (x86_elf_abi == X86_64_X32_ABI)
14313 return 4;
14314#endif
14315 return bfd_arch_bits_per_address (stdoutput) / 8;
14316}
14317
d2b2c203
DJ
14318int
14319i386_elf_section_type (const char *str, size_t len)
14320{
14321 if (flag_code == CODE_64BIT
14322 && len == sizeof ("unwind") - 1
14323 && strncmp (str, "unwind", 6) == 0)
14324 return SHT_X86_64_UNWIND;
14325
14326 return -1;
14327}
bb41ade5 14328
ad5fec3b
EB
14329#ifdef TE_SOLARIS
14330void
14331i386_solaris_fix_up_eh_frame (segT sec)
14332{
14333 if (flag_code == CODE_64BIT)
14334 elf_section_type (sec) = SHT_X86_64_UNWIND;
14335}
14336#endif
14337
bb41ade5
AM
14338#ifdef TE_PE
14339void
14340tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
14341{
91d6fa6a 14342 expressionS exp;
bb41ade5 14343
91d6fa6a
NC
14344 exp.X_op = O_secrel;
14345 exp.X_add_symbol = symbol;
14346 exp.X_add_number = 0;
14347 emit_expr (&exp, size);
bb41ade5
AM
14348}
14349#endif
3b22753a
L
14350
14351#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14352/* For ELF on x86-64, add support for SHF_X86_64_LARGE. */
14353
01e1a5bc 14354bfd_vma
6d4af3c2 14355x86_64_section_letter (int letter, const char **ptr_msg)
3b22753a
L
14356{
14357 if (flag_code == CODE_64BIT)
14358 {
14359 if (letter == 'l')
14360 return SHF_X86_64_LARGE;
14361
8f3bae45 14362 *ptr_msg = _("bad .section directive: want a,l,w,x,M,S,G,T in string");
64e74474 14363 }
3b22753a 14364 else
8f3bae45 14365 *ptr_msg = _("bad .section directive: want a,w,x,M,S,G,T in string");
3b22753a
L
14366 return -1;
14367}
14368
01e1a5bc 14369bfd_vma
3b22753a
L
14370x86_64_section_word (char *str, size_t len)
14371{
08dedd66 14372 if (len == 5 && flag_code == CODE_64BIT && startswith (str, "large"))
3b22753a
L
14373 return SHF_X86_64_LARGE;
14374
14375 return -1;
14376}
14377
14378static void
14379handle_large_common (int small ATTRIBUTE_UNUSED)
14380{
14381 if (flag_code != CODE_64BIT)
14382 {
14383 s_comm_internal (0, elf_common_parse);
14384 as_warn (_(".largecomm supported only in 64bit mode, producing .comm"));
14385 }
14386 else
14387 {
14388 static segT lbss_section;
14389 asection *saved_com_section_ptr = elf_com_section_ptr;
14390 asection *saved_bss_section = bss_section;
14391
14392 if (lbss_section == NULL)
14393 {
14394 flagword applicable;
14395 segT seg = now_seg;
14396 subsegT subseg = now_subseg;
14397
14398 /* The .lbss section is for local .largecomm symbols. */
14399 lbss_section = subseg_new (".lbss", 0);
14400 applicable = bfd_applicable_section_flags (stdoutput);
fd361982 14401 bfd_set_section_flags (lbss_section, applicable & SEC_ALLOC);
3b22753a
L
14402 seg_info (lbss_section)->bss = 1;
14403
14404 subseg_set (seg, subseg);
14405 }
14406
14407 elf_com_section_ptr = &_bfd_elf_large_com_section;
14408 bss_section = lbss_section;
14409
14410 s_comm_internal (0, elf_common_parse);
14411
14412 elf_com_section_ptr = saved_com_section_ptr;
14413 bss_section = saved_bss_section;
14414 }
14415}
14416#endif /* OBJ_ELF || OBJ_MAYBE_ELF */