]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/config/tc-i386.c
x86-64: conditionalize tests using --32
[thirdparty/binutils-gdb.git] / gas / config / tc-i386.c
CommitLineData
b534c6d3 1/* tc-i386.c -- Assemble code for the Intel 80386
d87bef3a 2 Copyright (C) 1989-2023 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"
b52c4ee4
IB
33#include "gen-sframe.h"
34#include "sframe.h"
d2b2c203 35#include "elf/x86-64.h"
40fb9820 36#include "opcodes/i386-init.h"
5c139202 37#include "opcodes/i386-mnem.h"
41fd2579 38#include <limits.h>
41fd2579 39
c3332e24 40#ifndef INFER_ADDR_PREFIX
eecb386c 41#define INFER_ADDR_PREFIX 1
c3332e24
AM
42#endif
43
29b0f896
AM
44#ifndef DEFAULT_ARCH
45#define DEFAULT_ARCH "i386"
246fcdee 46#endif
252b5132 47
edde18a5
AM
48#ifndef INLINE
49#if __GNUC__ >= 2
50#define INLINE __inline__
51#else
52#define INLINE
53#endif
54#endif
55
6305a203
L
56/* Prefixes will be emitted in the order defined below.
57 WAIT_PREFIX must be the first prefix since FWAIT is really is an
58 instruction, and so must come before any prefixes.
59 The preferred prefix order is SEG_PREFIX, ADDR_PREFIX, DATA_PREFIX,
42164a71 60 REP_PREFIX/HLE_PREFIX, LOCK_PREFIX. */
6305a203
L
61#define WAIT_PREFIX 0
62#define SEG_PREFIX 1
63#define ADDR_PREFIX 2
64#define DATA_PREFIX 3
c32fa91d 65#define REP_PREFIX 4
42164a71 66#define HLE_PREFIX REP_PREFIX
7e8b059b 67#define BND_PREFIX REP_PREFIX
c32fa91d 68#define LOCK_PREFIX 5
4e9ac44a
L
69#define REX_PREFIX 6 /* must come last. */
70#define MAX_PREFIXES 7 /* max prefixes per opcode */
6305a203
L
71
72/* we define the syntax here (modulo base,index,scale syntax) */
73#define REGISTER_PREFIX '%'
74#define IMMEDIATE_PREFIX '$'
75#define ABSOLUTE_PREFIX '*'
76
77/* these are the instruction mnemonic suffixes in AT&T syntax or
78 memory operand size in Intel syntax. */
79#define WORD_MNEM_SUFFIX 'w'
80#define BYTE_MNEM_SUFFIX 'b'
81#define SHORT_MNEM_SUFFIX 's'
82#define LONG_MNEM_SUFFIX 'l'
83#define QWORD_MNEM_SUFFIX 'q'
6305a203
L
84
85#define END_OF_INSN '\0'
86
05909f23
JB
87#define OPERAND_TYPE_NONE { .bitfield = { .class = ClassNone } }
88
79dec6b7
JB
89/* This matches the C -> StaticRounding alias in the opcode table. */
90#define commutative staticrounding
91
6305a203
L
92/*
93 'templates' is for grouping together 'template' structures for opcodes
94 of the same name. This is only used for storing the insns in the grand
95 ole hash table of insns.
96 The templates themselves start at START and range up to (but not including)
97 END.
98 */
99typedef struct
100{
d3ce72d0
NC
101 const insn_template *start;
102 const insn_template *end;
6305a203
L
103}
104templates;
105
106/* 386 operand encoding bytes: see 386 book for details of this. */
107typedef struct
108{
109 unsigned int regmem; /* codes register or memory operand */
110 unsigned int reg; /* codes register operand (or extended opcode) */
111 unsigned int mode; /* how to interpret regmem & reg */
112}
113modrm_byte;
114
115/* x86-64 extension prefix. */
116typedef int rex_byte;
117
6305a203
L
118/* 386 opcode byte to code indirect addressing. */
119typedef struct
120{
121 unsigned base;
122 unsigned index;
123 unsigned scale;
124}
125sib_byte;
126
6305a203
L
127/* x86 arch names, types and features */
128typedef struct
129{
130 const char *name; /* arch name */
6ceeed25
JB
131 unsigned int len:8; /* arch string length */
132 bool skip:1; /* show_arch should skip this. */
6305a203 133 enum processor_type type; /* arch type */
ae89daec
JB
134 i386_cpu_flags enable; /* cpu feature enable flags */
135 i386_cpu_flags disable; /* cpu feature disable flags */
6305a203
L
136}
137arch_entry;
138
78f12dd3 139static void update_code_flag (int, int);
edd67638 140static void s_insn (int);
e3bb37b5
L
141static void set_code_flag (int);
142static void set_16bit_gcc_code_flag (int);
143static void set_intel_syntax (int);
1efbbeb4 144static void set_intel_mnemonic (int);
db51cc60 145static void set_allow_index_reg (int);
7bab8ab5 146static void set_check (int);
e3bb37b5 147static void set_cpu_arch (int);
6482c264 148#ifdef TE_PE
e3bb37b5 149static void pe_directive_secrel (int);
145667f8 150static void pe_directive_secidx (int);
6482c264 151#endif
e3bb37b5
L
152static void signed_cons (int);
153static char *output_invalid (int c);
ee86248c
JB
154static int i386_finalize_immediate (segT, expressionS *, i386_operand_type,
155 const char *);
156static int i386_finalize_displacement (segT, expressionS *, i386_operand_type,
157 const char *);
a7619375 158static int i386_att_operand (char *);
e3bb37b5 159static int i386_intel_operand (char *, int);
ee86248c
JB
160static int i386_intel_simplify (expressionS *);
161static int i386_intel_parse_name (const char *, expressionS *);
4f081312 162static const reg_entry *parse_register (const char *, char **);
edd67638 163static const char *parse_insn (const char *, char *, bool);
e3bb37b5
L
164static char *parse_operands (char *, const char *);
165static void swap_operands (void);
783c187b 166static void swap_2_operands (unsigned int, unsigned int);
48bcea9f 167static enum flag_code i386_addressing_mode (void);
e3bb37b5 168static void optimize_imm (void);
0de704b9 169static bool optimize_disp (const insn_template *t);
83b16ac6 170static const insn_template *match_template (char);
e3bb37b5
L
171static int check_string (void);
172static int process_suffix (void);
173static int check_byte_reg (void);
174static int check_long_reg (void);
175static int check_qword_reg (void);
176static int check_word_reg (void);
177static int finalize_imm (void);
178static int process_operands (void);
5e042380 179static const reg_entry *build_modrm_byte (void);
e3bb37b5
L
180static void output_insn (void);
181static void output_imm (fragS *, offsetT);
182static void output_disp (fragS *, offsetT);
29b0f896 183#ifndef I386COFF
e3bb37b5 184static void s_bss (int);
252b5132 185#endif
17d4e2a2
L
186#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
187static void handle_large_common (int small ATTRIBUTE_UNUSED);
b4a3a7b4
L
188
189/* GNU_PROPERTY_X86_ISA_1_USED. */
190static unsigned int x86_isa_1_used;
191/* GNU_PROPERTY_X86_FEATURE_2_USED. */
192static unsigned int x86_feature_2_used;
193/* Generate x86 used ISA and feature properties. */
194static unsigned int x86_used_note = DEFAULT_X86_USED_NOTE;
17d4e2a2 195#endif
252b5132 196
a847613f 197static const char *default_arch = DEFAULT_ARCH;
3e73aa7c 198
8a6fb3f9
JB
199/* parse_register() returns this when a register alias cannot be used. */
200static const reg_entry bad_reg = { "<bad>", OPERAND_TYPE_NONE, 0, 0,
201 { Dw2Inval, Dw2Inval } };
202
34684862 203static const reg_entry *reg_eax;
5e042380
JB
204static const reg_entry *reg_ds;
205static const reg_entry *reg_es;
206static const reg_entry *reg_ss;
6288d05f 207static const reg_entry *reg_st0;
6225c532
JB
208static const reg_entry *reg_k0;
209
c0f3af97
L
210/* VEX prefix. */
211typedef struct
212{
43234a1e
L
213 /* VEX prefix is either 2 byte or 3 byte. EVEX is 4 byte. */
214 unsigned char bytes[4];
c0f3af97
L
215 unsigned int length;
216 /* Destination or source register specifier. */
217 const reg_entry *register_specifier;
218} vex_prefix;
219
252b5132 220/* 'md_assemble ()' gathers together information and puts it into a
47926f60 221 i386_insn. */
252b5132 222
520dc8e8
AM
223union i386_op
224 {
225 expressionS *disps;
226 expressionS *imms;
227 const reg_entry *regs;
228 };
229
a65babc9
L
230enum i386_error
231 {
b4d65f2d 232 no_error, /* Must be first. */
86e026a4 233 operand_size_mismatch,
a65babc9
L
234 operand_type_mismatch,
235 register_type_mismatch,
236 number_of_operands_mismatch,
237 invalid_instruction_suffix,
238 bad_imm4,
a65babc9
L
239 unsupported_with_intel_mnemonic,
240 unsupported_syntax,
6c30d220 241 unsupported,
9db83a32
JB
242 unsupported_on_arch,
243 unsupported_64bit,
260cd341 244 invalid_sib_address,
6c30d220 245 invalid_vsib_address,
7bab8ab5 246 invalid_vector_register_set,
260cd341 247 invalid_tmm_register_set,
0cc78721 248 invalid_dest_and_src_register_set,
43234a1e
L
249 unsupported_vector_index_register,
250 unsupported_broadcast,
43234a1e
L
251 broadcast_needed,
252 unsupported_masking,
253 mask_not_on_destination,
254 no_default_mask,
255 unsupported_rc_sae,
43234a1e 256 invalid_register_operand,
a65babc9
L
257 };
258
252b5132
RH
259struct _i386_insn
260 {
47926f60 261 /* TM holds the template for the insn were currently assembling. */
d3ce72d0 262 insn_template tm;
252b5132 263
7d5e4556
L
264 /* SUFFIX holds the instruction size suffix for byte, word, dword
265 or qword, if given. */
252b5132
RH
266 char suffix;
267
9a182d04
JB
268 /* OPCODE_LENGTH holds the number of base opcode bytes. */
269 unsigned char opcode_length;
270
47926f60 271 /* OPERANDS gives the number of given operands. */
252b5132
RH
272 unsigned int operands;
273
274 /* REG_OPERANDS, DISP_OPERANDS, MEM_OPERANDS, IMM_OPERANDS give the number
275 of given register, displacement, memory operands and immediate
47926f60 276 operands. */
252b5132
RH
277 unsigned int reg_operands, disp_operands, mem_operands, imm_operands;
278
279 /* TYPES [i] is the type (see above #defines) which tells us how to
520dc8e8 280 use OP[i] for the corresponding operand. */
40fb9820 281 i386_operand_type types[MAX_OPERANDS];
252b5132 282
520dc8e8
AM
283 /* Displacement expression, immediate expression, or register for each
284 operand. */
285 union i386_op op[MAX_OPERANDS];
252b5132 286
3e73aa7c
JH
287 /* Flags for operands. */
288 unsigned int flags[MAX_OPERANDS];
289#define Operand_PCrel 1
c48dadc9 290#define Operand_Mem 2
c032bc4f 291#define Operand_Signed 4 /* .insn only */
3e73aa7c 292
252b5132 293 /* Relocation type for operand */
f86103b7 294 enum bfd_reloc_code_real reloc[MAX_OPERANDS];
252b5132 295
252b5132
RH
296 /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
297 the base index byte below. */
298 const reg_entry *base_reg;
299 const reg_entry *index_reg;
300 unsigned int log2_scale_factor;
301
302 /* SEG gives the seg_entries of this insn. They are zero unless
47926f60 303 explicit segment overrides are given. */
5e042380 304 const reg_entry *seg[2];
252b5132
RH
305
306 /* PREFIX holds all the given prefix opcodes (usually null).
307 PREFIXES is the number of prefix opcodes. */
308 unsigned int prefixes;
309 unsigned char prefix[MAX_PREFIXES];
310
d0c2e3ec
JB
311 /* .insn allows for reserved opcode spaces. */
312 unsigned char insn_opcode_space;
313
c032bc4f
JB
314 /* .insn also allows (requires) specifying immediate size. */
315 unsigned char imm_bits[MAX_OPERANDS];
316
50128d0c 317 /* Register is in low 3 bits of opcode. */
5b7c81bd 318 bool short_form;
50128d0c 319
6f2f06be 320 /* The operand to a branch insn indicates an absolute branch. */
5b7c81bd 321 bool jumpabsolute;
6f2f06be 322
a4d3acd2
JB
323 /* The operand to a branch insn indicates a far branch. */
324 bool far_branch;
325
9373f275
L
326 /* There is a memory operand of (%dx) which should be only used
327 with input/output instructions. */
328 bool input_output_operand;
329
921eafea
L
330 /* Extended states. */
331 enum
332 {
333 /* Use MMX state. */
334 xstate_mmx = 1 << 0,
335 /* Use XMM state. */
336 xstate_xmm = 1 << 1,
337 /* Use YMM state. */
338 xstate_ymm = 1 << 2 | xstate_xmm,
339 /* Use ZMM state. */
340 xstate_zmm = 1 << 3 | xstate_ymm,
341 /* Use TMM state. */
32930e4e
L
342 xstate_tmm = 1 << 4,
343 /* Use MASK state. */
344 xstate_mask = 1 << 5
921eafea 345 } xstate;
260cd341 346
e379e5f3 347 /* Has GOTPC or TLS relocation. */
5b7c81bd 348 bool has_gotpc_tls_reloc;
e379e5f3 349
252b5132 350 /* RM and SIB are the modrm byte and the sib byte where the
c1e679ec 351 addressing modes of this insn are encoded. */
252b5132 352 modrm_byte rm;
3e73aa7c 353 rex_byte rex;
43234a1e 354 rex_byte vrex;
252b5132 355 sib_byte sib;
c0f3af97 356 vex_prefix vex;
b6169b20 357
6225c532
JB
358 /* Masking attributes.
359
360 The struct describes masking, applied to OPERAND in the instruction.
361 REG is a pointer to the corresponding mask register. ZEROING tells
362 whether merging or zeroing mask is used. */
363 struct Mask_Operation
364 {
365 const reg_entry *reg;
366 unsigned int zeroing;
367 /* The operand where this operation is associated. */
368 unsigned int operand;
369 } mask;
43234a1e
L
370
371 /* Rounding control and SAE attributes. */
ca5312a2
JB
372 struct RC_Operation
373 {
374 enum rc_type
375 {
376 rc_none = -1,
377 rne,
378 rd,
379 ru,
380 rz,
381 saeonly
382 } type;
7063667e
JB
383 /* In Intel syntax the operand modifier form is supposed to be used, but
384 we continue to accept the immediate forms as well. */
385 bool modifier;
ca5312a2 386 } rounding;
43234a1e 387
5273a3cd
JB
388 /* Broadcasting attributes.
389
390 The struct describes broadcasting, applied to OPERAND. TYPE is
391 expresses the broadcast factor. */
392 struct Broadcast_Operation
393 {
0cc78721 394 /* Type of broadcast: {1to2}, {1to4}, {1to8}, {1to16} or {1to32}. */
5273a3cd
JB
395 unsigned int type;
396
397 /* Index of broadcasted operand. */
398 unsigned int operand;
399
400 /* Number of bytes to broadcast. */
401 unsigned int bytes;
402 } broadcast;
43234a1e
L
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 425 /* Prefer the REX byte in encoding. */
5b7c81bd 426 bool rex_encoding;
6b6b6807 427
b6f8c7c4 428 /* Disable instruction size optimization. */
5b7c81bd 429 bool no_optimize;
b6f8c7c4 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
3bfea8ba
L
477/* To be indexed by segment register number. */
478static const unsigned char i386_seg_prefixes[] = {
479 ES_PREFIX_OPCODE,
480 CS_PREFIX_OPCODE,
481 SS_PREFIX_OPCODE,
482 DS_PREFIX_OPCODE,
483 FS_PREFIX_OPCODE,
484 GS_PREFIX_OPCODE
485};
486
252b5132
RH
487/* List of chars besides those in app.c:symbol_chars that can start an
488 operand. Used to prevent the scrubber eating vital white-space. */
86fa6981 489const char extra_symbol_chars[] = "*%-([{}"
252b5132 490#ifdef LEX_AT
32137342
NC
491 "@"
492#endif
493#ifdef LEX_QM
494 "?"
252b5132 495#endif
32137342 496 ;
252b5132 497
b3983e5f
JB
498#if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
499 && !defined (TE_GNU) \
500 && !defined (TE_LINUX) \
d85e70a3 501 && !defined (TE_Haiku) \
b3983e5f
JB
502 && !defined (TE_FreeBSD) \
503 && !defined (TE_DragonFly) \
504 && !defined (TE_NetBSD))
252b5132 505/* This array holds the chars that always start a comment. If the
b3b91714
AM
506 pre-processor is disabled, these aren't very useful. The option
507 --divide will remove '/' from this list. */
508const char *i386_comment_chars = "#/";
509#define SVR4_COMMENT_CHARS 1
252b5132 510#define PREFIX_SEPARATOR '\\'
252b5132 511
b3b91714
AM
512#else
513const char *i386_comment_chars = "#";
514#define PREFIX_SEPARATOR '/'
515#endif
516
252b5132
RH
517/* This array holds the chars that only start a comment at the beginning of
518 a line. If the line seems to have the form '# 123 filename'
ce8a8b2f
AM
519 .line and .file directives will appear in the pre-processed output.
520 Note that input_file.c hand checks for '#' at the beginning of the
252b5132 521 first line of the input file. This is because the compiler outputs
ce8a8b2f
AM
522 #NO_APP at the beginning of its output.
523 Also note that comments started like this one will always work if
252b5132 524 '/' isn't otherwise defined. */
b3b91714 525const char line_comment_chars[] = "#/";
252b5132 526
63a0b638 527const char line_separator_chars[] = ";";
252b5132 528
ce8a8b2f
AM
529/* Chars that can be used to separate mant from exp in floating point
530 nums. */
252b5132
RH
531const char EXP_CHARS[] = "eE";
532
ce8a8b2f
AM
533/* Chars that mean this number is a floating point constant
534 As in 0f12.456
535 or 0d1.2345e12. */
de133cf9 536const char FLT_CHARS[] = "fFdDxXhHbB";
252b5132 537
ce8a8b2f 538/* Tables for lexical analysis. */
252b5132
RH
539static char mnemonic_chars[256];
540static char register_chars[256];
541static char operand_chars[256];
252b5132 542
ce8a8b2f 543/* Lexical macros. */
252b5132
RH
544#define is_operand_char(x) (operand_chars[(unsigned char) x])
545#define is_register_char(x) (register_chars[(unsigned char) x])
546#define is_space_char(x) ((x) == ' ')
252b5132 547
d2b1a14d
JB
548/* All non-digit non-letter characters that may occur in an operand and
549 which aren't already in extra_symbol_chars[]. */
550static const char operand_special_chars[] = "$+,)._~/<>|&^!=:@]";
252b5132
RH
551
552/* md_assemble() always leaves the strings it's passed unaltered. To
553 effect this we maintain a stack of saved characters that we've smashed
554 with '\0's (indicating end of strings for various sub-fields of the
47926f60 555 assembler instruction). */
252b5132 556static char save_stack[32];
ce8a8b2f 557static char *save_stack_p;
252b5132
RH
558#define END_STRING_AND_SAVE(s) \
559 do { *save_stack_p++ = *(s); *(s) = '\0'; } while (0)
560#define RESTORE_END_STRING(s) \
561 do { *(s) = *--save_stack_p; } while (0)
562
47926f60 563/* The instruction we're assembling. */
252b5132
RH
564static i386_insn i;
565
566/* Possible templates for current insn. */
567static const templates *current_templates;
568
31b2323c
L
569/* Per instruction expressionS buffers: max displacements & immediates. */
570static expressionS disp_expressions[MAX_MEMORY_OPERANDS];
571static expressionS im_expressions[MAX_IMMEDIATE_OPERANDS];
252b5132 572
47926f60 573/* Current operand we are working on. */
ee86248c 574static int this_operand = -1;
252b5132 575
d0c2e3ec
JB
576/* Are we processing a .insn directive? */
577#define dot_insn() (i.tm.mnem_off == MN__insn)
578
3e73aa7c
JH
579/* We support four different modes. FLAG_CODE variable is used to distinguish
580 these. */
581
582enum flag_code {
583 CODE_32BIT,
584 CODE_16BIT,
585 CODE_64BIT };
586
587static enum flag_code flag_code;
4fa24527 588static unsigned int object_64bit;
862be3fb 589static unsigned int disallow_64bit_reloc;
3e73aa7c 590static int use_rela_relocations = 0;
e379e5f3
L
591/* __tls_get_addr/___tls_get_addr symbol for TLS. */
592static const char *tls_get_addr;
3e73aa7c 593
7af8ed2d
NC
594#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
595 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
596 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
597
351f65ca
L
598/* The ELF ABI to use. */
599enum x86_elf_abi
600{
601 I386_ABI,
7f56bc95
L
602 X86_64_ABI,
603 X86_64_X32_ABI
351f65ca
L
604};
605
606static enum x86_elf_abi x86_elf_abi = I386_ABI;
7af8ed2d 607#endif
351f65ca 608
167ad85b
TG
609#if defined (TE_PE) || defined (TE_PEP)
610/* Use big object file format. */
611static int use_big_obj = 0;
612#endif
613
8dcea932
L
614#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
615/* 1 if generating code for a shared library. */
616static int shared = 0;
b52c4ee4
IB
617
618unsigned int x86_sframe_cfa_sp_reg;
3e3e792a 619/* The other CFA base register for SFrame stack trace info. */
b52c4ee4
IB
620unsigned int x86_sframe_cfa_fp_reg;
621unsigned int x86_sframe_cfa_ra_reg;
622
8dcea932
L
623#endif
624
47926f60
KH
625/* 1 for intel syntax,
626 0 if att syntax. */
627static int intel_syntax = 0;
252b5132 628
4b5aaf5f
L
629static enum x86_64_isa
630{
631 amd64 = 1, /* AMD64 ISA. */
632 intel64 /* Intel64 ISA. */
633} isa64;
e89c5eaa 634
1efbbeb4
L
635/* 1 for intel mnemonic,
636 0 if att mnemonic. */
637static int intel_mnemonic = !SYSV386_COMPAT;
638
a60de03c
JB
639/* 1 if pseudo registers are permitted. */
640static int allow_pseudo_reg = 0;
641
47926f60
KH
642/* 1 if register prefix % not required. */
643static int allow_naked_reg = 0;
252b5132 644
33eaf5de 645/* 1 if the assembler should add BND prefix for all control-transferring
7e8b059b
L
646 instructions supporting it, even if this prefix wasn't specified
647 explicitly. */
648static int add_bnd_prefix = 0;
649
ba104c83 650/* 1 if pseudo index register, eiz/riz, is allowed . */
db51cc60
L
651static int allow_index_reg = 0;
652
d022bddd
IT
653/* 1 if the assembler should ignore LOCK prefix, even if it was
654 specified explicitly. */
655static int omit_lock_prefix = 0;
656
e4e00185
AS
657/* 1 if the assembler should encode lfence, mfence, and sfence as
658 "lock addl $0, (%{re}sp)". */
659static int avoid_fence = 0;
660
ae531041
L
661/* 1 if lfence should be inserted after every load. */
662static int lfence_after_load = 0;
663
664/* Non-zero if lfence should be inserted before indirect branch. */
665static enum lfence_before_indirect_branch_kind
666 {
667 lfence_branch_none = 0,
668 lfence_branch_register,
669 lfence_branch_memory,
670 lfence_branch_all
671 }
672lfence_before_indirect_branch;
673
674/* Non-zero if lfence should be inserted before ret. */
675static enum lfence_before_ret_kind
676 {
677 lfence_before_ret_none = 0,
678 lfence_before_ret_not,
a09f656b 679 lfence_before_ret_or,
680 lfence_before_ret_shl
ae531041
L
681 }
682lfence_before_ret;
683
684/* Types of previous instruction is .byte or prefix. */
e379e5f3
L
685static struct
686 {
687 segT seg;
688 const char *file;
689 const char *name;
690 unsigned int line;
691 enum last_insn_kind
692 {
693 last_insn_other = 0,
694 last_insn_directive,
695 last_insn_prefix
696 } kind;
697 } last_insn;
698
0cb4071e
L
699/* 1 if the assembler should generate relax relocations. */
700
701static int generate_relax_relocations
702 = DEFAULT_GENERATE_X86_RELAX_RELOCATIONS;
703
7bab8ab5 704static enum check_kind
daf50ae7 705 {
7bab8ab5
JB
706 check_none = 0,
707 check_warning,
708 check_error
daf50ae7 709 }
7bab8ab5 710sse_check, operand_check = check_warning;
daf50ae7 711
e379e5f3
L
712/* Non-zero if branches should be aligned within power of 2 boundary. */
713static int align_branch_power = 0;
714
715/* Types of branches to align. */
716enum align_branch_kind
717 {
718 align_branch_none = 0,
719 align_branch_jcc = 1,
720 align_branch_fused = 2,
721 align_branch_jmp = 3,
722 align_branch_call = 4,
723 align_branch_indirect = 5,
724 align_branch_ret = 6
725 };
726
727/* Type bits of branches to align. */
728enum align_branch_bit
729 {
730 align_branch_jcc_bit = 1 << align_branch_jcc,
731 align_branch_fused_bit = 1 << align_branch_fused,
732 align_branch_jmp_bit = 1 << align_branch_jmp,
733 align_branch_call_bit = 1 << align_branch_call,
734 align_branch_indirect_bit = 1 << align_branch_indirect,
735 align_branch_ret_bit = 1 << align_branch_ret
736 };
737
738static unsigned int align_branch = (align_branch_jcc_bit
739 | align_branch_fused_bit
740 | align_branch_jmp_bit);
741
79d72f45
HL
742/* Types of condition jump used by macro-fusion. */
743enum mf_jcc_kind
744 {
745 mf_jcc_jo = 0, /* base opcode 0x70 */
746 mf_jcc_jc, /* base opcode 0x72 */
747 mf_jcc_je, /* base opcode 0x74 */
748 mf_jcc_jna, /* base opcode 0x76 */
749 mf_jcc_js, /* base opcode 0x78 */
750 mf_jcc_jp, /* base opcode 0x7a */
751 mf_jcc_jl, /* base opcode 0x7c */
752 mf_jcc_jle, /* base opcode 0x7e */
753 };
754
755/* Types of compare flag-modifying insntructions used by macro-fusion. */
756enum mf_cmp_kind
757 {
758 mf_cmp_test_and, /* test/cmp */
759 mf_cmp_alu_cmp, /* add/sub/cmp */
760 mf_cmp_incdec /* inc/dec */
761 };
762
e379e5f3
L
763/* The maximum padding size for fused jcc. CMP like instruction can
764 be 9 bytes and jcc can be 6 bytes. Leave room just in case for
765 prefixes. */
766#define MAX_FUSED_JCC_PADDING_SIZE 20
767
768/* The maximum number of prefixes added for an instruction. */
769static unsigned int align_branch_prefix_size = 5;
770
b6f8c7c4
L
771/* Optimization:
772 1. Clear the REX_W bit with register operand if possible.
773 2. Above plus use 128bit vector instruction to clear the full vector
774 register.
775 */
776static int optimize = 0;
777
778/* Optimization:
779 1. Clear the REX_W bit with register operand if possible.
780 2. Above plus use 128bit vector instruction to clear the full vector
781 register.
782 3. Above plus optimize "test{q,l,w} $imm8,%r{64,32,16}" to
783 "testb $imm7,%r8".
784 */
785static int optimize_for_space = 0;
786
2ca3ace5
L
787/* Register prefix used for error message. */
788static const char *register_prefix = "%";
789
47926f60
KH
790/* Used in 16 bit gcc mode to add an l suffix to call, ret, enter,
791 leave, push, and pop instructions so that gcc has the same stack
792 frame as in 32 bit mode. */
793static char stackop_size = '\0';
eecb386c 794
12b55ccc
L
795/* Non-zero to optimize code alignment. */
796int optimize_align_code = 1;
797
47926f60
KH
798/* Non-zero to quieten some warnings. */
799static int quiet_warnings = 0;
a38cf1db 800
d59a54c2
JB
801/* Guard to avoid repeated warnings about non-16-bit code on 16-bit CPUs. */
802static bool pre_386_16bit_warned;
803
47926f60
KH
804/* CPU name. */
805static const char *cpu_arch_name = NULL;
6305a203 806static char *cpu_sub_arch_name = NULL;
a38cf1db 807
47926f60 808/* CPU feature flags. */
40fb9820
L
809static i386_cpu_flags cpu_arch_flags = CPU_UNKNOWN_FLAGS;
810
ccc9c027
L
811/* If we have selected a cpu we are generating instructions for. */
812static int cpu_arch_tune_set = 0;
813
9103f4f4 814/* Cpu we are generating instructions for. */
fbf3f584 815enum processor_type cpu_arch_tune = PROCESSOR_UNKNOWN;
9103f4f4
L
816
817/* CPU feature flags of cpu we are generating instructions for. */
40fb9820 818static i386_cpu_flags cpu_arch_tune_flags;
9103f4f4 819
ccc9c027 820/* CPU instruction set architecture used. */
fbf3f584 821enum processor_type cpu_arch_isa = PROCESSOR_UNKNOWN;
ccc9c027 822
9103f4f4 823/* CPU feature flags of instruction set architecture used. */
fbf3f584 824i386_cpu_flags cpu_arch_isa_flags;
9103f4f4 825
fddf5b5b
AM
826/* If set, conditional jumps are not automatically promoted to handle
827 larger than a byte offset. */
f68697e8 828static bool no_cond_jump_promotion = false;
fddf5b5b 829
5cc00775
JB
830/* This will be set from an expression parser hook if there's any
831 applicable operator involved in an expression. */
832static enum {
833 expr_operator_none,
834 expr_operator_present,
835 expr_large_value,
836} expr_mode;
837
c0f3af97
L
838/* Encode SSE instructions with VEX prefix. */
839static unsigned int sse2avx;
840
c8480b58
L
841/* Encode aligned vector move as unaligned vector move. */
842static unsigned int use_unaligned_vector_move;
843
539f890d
L
844/* Encode scalar AVX instructions with specific vector length. */
845static enum
846 {
847 vex128 = 0,
848 vex256
849 } avxscalar;
850
03751133
L
851/* Encode VEX WIG instructions with specific vex.w. */
852static enum
853 {
854 vexw0 = 0,
855 vexw1
856 } vexwig;
857
43234a1e
L
858/* Encode scalar EVEX LIG instructions with specific vector length. */
859static enum
860 {
861 evexl128 = 0,
862 evexl256,
863 evexl512
864 } evexlig;
865
866/* Encode EVEX WIG instructions with specific evex.w. */
867static enum
868 {
869 evexw0 = 0,
870 evexw1
871 } evexwig;
872
d3d3c6db
IT
873/* Value to encode in EVEX RC bits, for SAE-only instructions. */
874static enum rc_type evexrcig = rne;
875
29b0f896 876/* Pre-defined "_GLOBAL_OFFSET_TABLE_". */
87c245cc 877static symbolS *GOT_symbol;
29b0f896 878
a4447b93
RH
879/* The dwarf2 return column, adjusted for 32 or 64 bit. */
880unsigned int x86_dwarf2_return_column;
881
882/* The dwarf2 data alignment, adjusted for 32 or 64 bit. */
883int x86_cie_data_alignment;
884
252b5132 885/* Interface to relax_segment.
fddf5b5b
AM
886 There are 3 major relax states for 386 jump insns because the
887 different types of jumps add different sizes to frags when we're
e379e5f3
L
888 figuring out what sort of jump to choose to reach a given label.
889
890 BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING are used to align
891 branches which are handled by md_estimate_size_before_relax() and
892 i386_generic_table_relax_frag(). */
252b5132 893
47926f60 894/* Types. */
93c2a809
AM
895#define UNCOND_JUMP 0
896#define COND_JUMP 1
897#define COND_JUMP86 2
e379e5f3
L
898#define BRANCH_PADDING 3
899#define BRANCH_PREFIX 4
900#define FUSED_JCC_PADDING 5
fddf5b5b 901
47926f60 902/* Sizes. */
252b5132
RH
903#define CODE16 1
904#define SMALL 0
29b0f896 905#define SMALL16 (SMALL | CODE16)
252b5132 906#define BIG 2
29b0f896 907#define BIG16 (BIG | CODE16)
252b5132
RH
908
909#ifndef INLINE
910#ifdef __GNUC__
911#define INLINE __inline__
912#else
913#define INLINE
914#endif
915#endif
916
fddf5b5b
AM
917#define ENCODE_RELAX_STATE(type, size) \
918 ((relax_substateT) (((type) << 2) | (size)))
919#define TYPE_FROM_RELAX_STATE(s) \
920 ((s) >> 2)
921#define DISP_SIZE_FROM_RELAX_STATE(s) \
922 ((((s) & 3) == BIG ? 4 : (((s) & 3) == BIG16 ? 2 : 1)))
252b5132
RH
923
924/* This table is used by relax_frag to promote short jumps to long
925 ones where necessary. SMALL (short) jumps may be promoted to BIG
926 (32 bit long) ones, and SMALL16 jumps to BIG16 (16 bit long). We
927 don't allow a short jump in a 32 bit code segment to be promoted to
928 a 16 bit offset jump because it's slower (requires data size
929 prefix), and doesn't work, unless the destination is in the bottom
930 64k of the code segment (The top 16 bits of eip are zeroed). */
931
932const relax_typeS md_relax_table[] =
933{
24eab124
AM
934 /* The fields are:
935 1) most positive reach of this state,
936 2) most negative reach of this state,
93c2a809 937 3) how many bytes this mode will have in the variable part of the frag
ce8a8b2f 938 4) which index into the table to try if we can't fit into this one. */
252b5132 939
fddf5b5b 940 /* UNCOND_JUMP states. */
93c2a809
AM
941 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG)},
942 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16)},
943 /* dword jmp adds 4 bytes to frag:
944 0 extra opcode bytes, 4 displacement bytes. */
252b5132 945 {0, 0, 4, 0},
93c2a809
AM
946 /* word jmp adds 2 byte2 to frag:
947 0 extra opcode bytes, 2 displacement bytes. */
252b5132
RH
948 {0, 0, 2, 0},
949
93c2a809
AM
950 /* COND_JUMP states. */
951 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG)},
952 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG16)},
953 /* dword conditionals adds 5 bytes to frag:
954 1 extra opcode byte, 4 displacement bytes. */
955 {0, 0, 5, 0},
fddf5b5b 956 /* word conditionals add 3 bytes to frag:
93c2a809
AM
957 1 extra opcode byte, 2 displacement bytes. */
958 {0, 0, 3, 0},
959
960 /* COND_JUMP86 states. */
961 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG)},
962 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG16)},
963 /* dword conditionals adds 5 bytes to frag:
964 1 extra opcode byte, 4 displacement bytes. */
965 {0, 0, 5, 0},
966 /* word conditionals add 4 bytes to frag:
967 1 displacement byte and a 3 byte long branch insn. */
968 {0, 0, 4, 0}
252b5132
RH
969};
970
6ceeed25 971#define ARCH(n, t, f, s) \
ae89daec
JB
972 { STRING_COMMA_LEN (#n), s, PROCESSOR_ ## t, CPU_ ## f ## _FLAGS, \
973 CPU_NONE_FLAGS }
974#define SUBARCH(n, e, d, s) \
975 { STRING_COMMA_LEN (#n), s, PROCESSOR_NONE, CPU_ ## e ## _FLAGS, \
976 CPU_ ## d ## _FLAGS }
6ceeed25 977
9103f4f4
L
978static const arch_entry cpu_arch[] =
979{
3ce2ebcf
JB
980 /* Do not replace the first two entries - i386_target_format() and
981 set_cpu_arch() rely on them being there in this order. */
6ceeed25
JB
982 ARCH (generic32, GENERIC32, GENERIC32, false),
983 ARCH (generic64, GENERIC64, GENERIC64, false),
984 ARCH (i8086, UNKNOWN, NONE, false),
4d97c5c8
JB
985 ARCH (i186, UNKNOWN, 186, false),
986 ARCH (i286, UNKNOWN, 286, false),
987 ARCH (i386, I386, 386, false),
988 ARCH (i486, I486, 486, false),
989 ARCH (i586, PENTIUM, 586, false),
990 ARCH (i686, PENTIUMPRO, 686, false),
991 ARCH (pentium, PENTIUM, 586, false),
6ceeed25
JB
992 ARCH (pentiumpro, PENTIUMPRO, PENTIUMPRO, false),
993 ARCH (pentiumii, PENTIUMPRO, P2, false),
994 ARCH (pentiumiii, PENTIUMPRO, P3, false),
995 ARCH (pentium4, PENTIUM4, P4, false),
996 ARCH (prescott, NOCONA, CORE, false),
997 ARCH (nocona, NOCONA, NOCONA, false),
998 ARCH (yonah, CORE, CORE, true),
999 ARCH (core, CORE, CORE, false),
1000 ARCH (merom, CORE2, CORE2, true),
1001 ARCH (core2, CORE2, CORE2, false),
1002 ARCH (corei7, COREI7, COREI7, false),
1003 ARCH (iamcu, IAMCU, IAMCU, false),
1004 ARCH (k6, K6, K6, false),
1005 ARCH (k6_2, K6, K6_2, false),
1006 ARCH (athlon, ATHLON, ATHLON, false),
1007 ARCH (sledgehammer, K8, K8, true),
1008 ARCH (opteron, K8, K8, false),
1009 ARCH (k8, K8, K8, false),
1010 ARCH (amdfam10, AMDFAM10, AMDFAM10, false),
1011 ARCH (bdver1, BD, BDVER1, false),
1012 ARCH (bdver2, BD, BDVER2, false),
1013 ARCH (bdver3, BD, BDVER3, false),
1014 ARCH (bdver4, BD, BDVER4, false),
1015 ARCH (znver1, ZNVER, ZNVER1, false),
1016 ARCH (znver2, ZNVER, ZNVER2, false),
1017 ARCH (znver3, ZNVER, ZNVER3, false),
b0e8fa7f 1018 ARCH (znver4, ZNVER, ZNVER4, false),
6ceeed25
JB
1019 ARCH (btver1, BT, BTVER1, false),
1020 ARCH (btver2, BT, BTVER2, false),
1021
4d97c5c8
JB
1022 SUBARCH (8087, 8087, ANY_8087, false),
1023 SUBARCH (87, NONE, ANY_8087, false), /* Disable only! */
ae89daec
JB
1024 SUBARCH (287, 287, ANY_287, false),
1025 SUBARCH (387, 387, ANY_387, false),
1026 SUBARCH (687, 687, ANY_687, false),
4d97c5c8 1027 SUBARCH (cmov, CMOV, CMOV, false),
88bd2203 1028 SUBARCH (fxsr, FXSR, ANY_FXSR, false),
ae89daec
JB
1029 SUBARCH (mmx, MMX, ANY_MMX, false),
1030 SUBARCH (sse, SSE, ANY_SSE, false),
1031 SUBARCH (sse2, SSE2, ANY_SSE2, false),
1032 SUBARCH (sse3, SSE3, ANY_SSE3, false),
1033 SUBARCH (sse4a, SSE4A, ANY_SSE4A, false),
1034 SUBARCH (ssse3, SSSE3, ANY_SSSE3, false),
1035 SUBARCH (sse4.1, SSE4_1, ANY_SSE4_1, false),
1036 SUBARCH (sse4.2, SSE4_2, ANY_SSE4_2, false),
1037 SUBARCH (sse4, SSE4_2, ANY_SSE4_1, false),
1038 SUBARCH (avx, AVX, ANY_AVX, false),
1039 SUBARCH (avx2, AVX2, ANY_AVX2, false),
1040 SUBARCH (avx512f, AVX512F, ANY_AVX512F, false),
1041 SUBARCH (avx512cd, AVX512CD, ANY_AVX512CD, false),
1042 SUBARCH (avx512er, AVX512ER, ANY_AVX512ER, false),
1043 SUBARCH (avx512pf, AVX512PF, ANY_AVX512PF, false),
1044 SUBARCH (avx512dq, AVX512DQ, ANY_AVX512DQ, false),
1045 SUBARCH (avx512bw, AVX512BW, ANY_AVX512BW, false),
1046 SUBARCH (avx512vl, AVX512VL, ANY_AVX512VL, false),
cafa5ef7 1047 SUBARCH (monitor, MONITOR, MONITOR, false),
25626f79
JB
1048 SUBARCH (vmx, VMX, ANY_VMX, false),
1049 SUBARCH (vmfunc, VMFUNC, ANY_VMFUNC, false),
ae89daec 1050 SUBARCH (smx, SMX, SMX, false),
5091b9ee
JB
1051 SUBARCH (xsave, XSAVE, ANY_XSAVE, false),
1052 SUBARCH (xsaveopt, XSAVEOPT, ANY_XSAVEOPT, false),
1053 SUBARCH (xsavec, XSAVEC, ANY_XSAVEC, false),
1054 SUBARCH (xsaves, XSAVES, ANY_XSAVES, false),
1055 SUBARCH (aes, AES, ANY_AES, false),
1056 SUBARCH (pclmul, PCLMUL, ANY_PCLMUL, false),
1057 SUBARCH (clmul, PCLMUL, ANY_PCLMUL, true),
ae89daec
JB
1058 SUBARCH (fsgsbase, FSGSBASE, FSGSBASE, false),
1059 SUBARCH (rdrnd, RDRND, RDRND, false),
5091b9ee 1060 SUBARCH (f16c, F16C, ANY_F16C, false),
ae89daec 1061 SUBARCH (bmi2, BMI2, BMI2, false),
5091b9ee
JB
1062 SUBARCH (fma, FMA, ANY_FMA, false),
1063 SUBARCH (fma4, FMA4, ANY_FMA4, false),
1064 SUBARCH (xop, XOP, ANY_XOP, false),
1065 SUBARCH (lwp, LWP, ANY_LWP, false),
ae89daec
JB
1066 SUBARCH (movbe, MOVBE, MOVBE, false),
1067 SUBARCH (cx16, CX16, CX16, false),
c3bb24f5 1068 SUBARCH (lahf_sahf, LAHF_SAHF, LAHF_SAHF, false),
25626f79 1069 SUBARCH (ept, EPT, ANY_EPT, false),
ae89daec
JB
1070 SUBARCH (lzcnt, LZCNT, LZCNT, false),
1071 SUBARCH (popcnt, POPCNT, POPCNT, false),
1072 SUBARCH (hle, HLE, HLE, false),
760ab3d0
JB
1073 SUBARCH (rtm, RTM, ANY_RTM, false),
1074 SUBARCH (tsx, TSX, TSX, false),
ae89daec
JB
1075 SUBARCH (invpcid, INVPCID, INVPCID, false),
1076 SUBARCH (clflush, CLFLUSH, CLFLUSH, false),
1077 SUBARCH (nop, NOP, NOP, false),
1078 SUBARCH (syscall, SYSCALL, SYSCALL, false),
1079 SUBARCH (rdtscp, RDTSCP, RDTSCP, false),
5091b9ee
JB
1080 SUBARCH (3dnow, 3DNOW, ANY_3DNOW, false),
1081 SUBARCH (3dnowa, 3DNOWA, ANY_3DNOWA, false),
ae89daec 1082 SUBARCH (padlock, PADLOCK, PADLOCK, false),
0919e770
JB
1083 SUBARCH (pacifica, SVME, ANY_SVME, true),
1084 SUBARCH (svme, SVME, ANY_SVME, false),
ae89daec
JB
1085 SUBARCH (abm, ABM, ABM, false),
1086 SUBARCH (bmi, BMI, BMI, false),
1087 SUBARCH (tbm, TBM, TBM, false),
1088 SUBARCH (adx, ADX, ADX, false),
1089 SUBARCH (rdseed, RDSEED, RDSEED, false),
1090 SUBARCH (prfchw, PRFCHW, PRFCHW, false),
1091 SUBARCH (smap, SMAP, SMAP, false),
5091b9ee
JB
1092 SUBARCH (mpx, MPX, ANY_MPX, false),
1093 SUBARCH (sha, SHA, ANY_SHA, false),
ae89daec
JB
1094 SUBARCH (clflushopt, CLFLUSHOPT, CLFLUSHOPT, false),
1095 SUBARCH (prefetchwt1, PREFETCHWT1, PREFETCHWT1, false),
1096 SUBARCH (se1, SE1, SE1, false),
1097 SUBARCH (clwb, CLWB, CLWB, false),
1098 SUBARCH (avx512ifma, AVX512IFMA, ANY_AVX512IFMA, false),
1099 SUBARCH (avx512vbmi, AVX512VBMI, ANY_AVX512VBMI, false),
1100 SUBARCH (avx512_4fmaps, AVX512_4FMAPS, ANY_AVX512_4FMAPS, false),
1101 SUBARCH (avx512_4vnniw, AVX512_4VNNIW, ANY_AVX512_4VNNIW, false),
1102 SUBARCH (avx512_vpopcntdq, AVX512_VPOPCNTDQ, ANY_AVX512_VPOPCNTDQ, false),
1103 SUBARCH (avx512_vbmi2, AVX512_VBMI2, ANY_AVX512_VBMI2, false),
1104 SUBARCH (avx512_vnni, AVX512_VNNI, ANY_AVX512_VNNI, false),
1105 SUBARCH (avx512_bitalg, AVX512_BITALG, ANY_AVX512_BITALG, false),
1106 SUBARCH (avx_vnni, AVX_VNNI, ANY_AVX_VNNI, false),
1107 SUBARCH (clzero, CLZERO, CLZERO, false),
1108 SUBARCH (mwaitx, MWAITX, MWAITX, false),
5091b9ee 1109 SUBARCH (ospke, OSPKE, ANY_OSPKE, false),
ae89daec
JB
1110 SUBARCH (rdpid, RDPID, RDPID, false),
1111 SUBARCH (ptwrite, PTWRITE, PTWRITE, false),
4d97c5c8
JB
1112 SUBARCH (ibt, IBT, IBT, false),
1113 SUBARCH (shstk, SHSTK, SHSTK, false),
88bd2203 1114 SUBARCH (gfni, GFNI, ANY_GFNI, false),
b20f4261
JB
1115 SUBARCH (vaes, VAES, ANY_VAES, false),
1116 SUBARCH (vpclmulqdq, VPCLMULQDQ, ANY_VPCLMULQDQ, false),
ae89daec
JB
1117 SUBARCH (wbnoinvd, WBNOINVD, WBNOINVD, false),
1118 SUBARCH (pconfig, PCONFIG, PCONFIG, false),
1119 SUBARCH (waitpkg, WAITPKG, WAITPKG, false),
1120 SUBARCH (cldemote, CLDEMOTE, CLDEMOTE, false),
1121 SUBARCH (amx_int8, AMX_INT8, ANY_AMX_INT8, false),
1122 SUBARCH (amx_bf16, AMX_BF16, ANY_AMX_BF16, false),
5091b9ee 1123 SUBARCH (amx_fp16, AMX_FP16, ANY_AMX_FP16, false),
d100d8c1 1124 SUBARCH (amx_complex, AMX_COMPLEX, ANY_AMX_COMPLEX, false),
ae89daec 1125 SUBARCH (amx_tile, AMX_TILE, ANY_AMX_TILE, false),
4d97c5c8
JB
1126 SUBARCH (movdiri, MOVDIRI, MOVDIRI, false),
1127 SUBARCH (movdir64b, MOVDIR64B, MOVDIR64B, false),
ae89daec
JB
1128 SUBARCH (avx512_bf16, AVX512_BF16, ANY_AVX512_BF16, false),
1129 SUBARCH (avx512_vp2intersect, AVX512_VP2INTERSECT,
1130 ANY_AVX512_VP2INTERSECT, false),
4d97c5c8
JB
1131 SUBARCH (tdx, TDX, TDX, false),
1132 SUBARCH (enqcmd, ENQCMD, ENQCMD, false),
1133 SUBARCH (serialize, SERIALIZE, SERIALIZE, false),
ae89daec
JB
1134 SUBARCH (rdpru, RDPRU, RDPRU, false),
1135 SUBARCH (mcommit, MCOMMIT, MCOMMIT, false),
0919e770 1136 SUBARCH (sev_es, SEV_ES, ANY_SEV_ES, false),
760ab3d0 1137 SUBARCH (tsxldtrk, TSXLDTRK, ANY_TSXLDTRK, false),
88bd2203
JB
1138 SUBARCH (kl, KL, ANY_KL, false),
1139 SUBARCH (widekl, WIDEKL, ANY_WIDEKL, false),
4d97c5c8
JB
1140 SUBARCH (uintr, UINTR, UINTR, false),
1141 SUBARCH (hreset, HRESET, HRESET, false),
ae89daec 1142 SUBARCH (avx512_fp16, AVX512_FP16, ANY_AVX512_FP16, false),
ef07be45 1143 SUBARCH (prefetchi, PREFETCHI, PREFETCHI, false),
4321af3e 1144 SUBARCH (avx_ifma, AVX_IFMA, ANY_AVX_IFMA, false),
23ae61ad 1145 SUBARCH (avx_vnni_int8, AVX_VNNI_INT8, ANY_AVX_VNNI_INT8, false),
4d97c5c8
JB
1146 SUBARCH (cmpccxadd, CMPCCXADD, CMPCCXADD, false),
1147 SUBARCH (wrmsrns, WRMSRNS, WRMSRNS, false),
1148 SUBARCH (msrlist, MSRLIST, MSRLIST, false),
01d8ce74 1149 SUBARCH (avx_ne_convert, AVX_NE_CONVERT, ANY_AVX_NE_CONVERT, false),
4d97c5c8 1150 SUBARCH (rao_int, RAO_INT, RAO_INT, false),
0919e770 1151 SUBARCH (rmpquery, RMPQUERY, ANY_RMPQUERY, false),
c88ed92f
ZJ
1152 SUBARCH (fred, FRED, ANY_FRED, false),
1153 SUBARCH (lkgs, LKGS, ANY_LKGS, false),
293f5f65
L
1154};
1155
6ceeed25
JB
1156#undef SUBARCH
1157#undef ARCH
1158
704209c0 1159#ifdef I386COFF
a6c24e68
NC
1160/* Like s_lcomm_internal in gas/read.c but the alignment string
1161 is allowed to be optional. */
1162
1163static symbolS *
1164pe_lcomm_internal (int needs_align, symbolS *symbolP, addressT size)
1165{
1166 addressT align = 0;
1167
1168 SKIP_WHITESPACE ();
1169
7ab9ffdd 1170 if (needs_align
a6c24e68
NC
1171 && *input_line_pointer == ',')
1172 {
1173 align = parse_align (needs_align - 1);
7ab9ffdd 1174
a6c24e68
NC
1175 if (align == (addressT) -1)
1176 return NULL;
1177 }
1178 else
1179 {
1180 if (size >= 8)
1181 align = 3;
1182 else if (size >= 4)
1183 align = 2;
1184 else if (size >= 2)
1185 align = 1;
1186 else
1187 align = 0;
1188 }
1189
1190 bss_alloc (symbolP, size, align);
1191 return symbolP;
1192}
1193
704209c0 1194static void
a6c24e68
NC
1195pe_lcomm (int needs_align)
1196{
1197 s_comm_internal (needs_align * 2, pe_lcomm_internal);
1198}
704209c0 1199#endif
a6c24e68 1200
29b0f896
AM
1201const pseudo_typeS md_pseudo_table[] =
1202{
1203#if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
1204 {"align", s_align_bytes, 0},
1205#else
1206 {"align", s_align_ptwo, 0},
1207#endif
1208 {"arch", set_cpu_arch, 0},
1209#ifndef I386COFF
1210 {"bss", s_bss, 0},
a6c24e68
NC
1211#else
1212 {"lcomm", pe_lcomm, 1},
29b0f896
AM
1213#endif
1214 {"ffloat", float_cons, 'f'},
1215 {"dfloat", float_cons, 'd'},
1216 {"tfloat", float_cons, 'x'},
7d19d096 1217 {"hfloat", float_cons, 'h'},
de133cf9 1218 {"bfloat16", float_cons, 'b'},
29b0f896 1219 {"value", cons, 2},
d182319b 1220 {"slong", signed_cons, 4},
edd67638 1221 {"insn", s_insn, 0},
29b0f896
AM
1222 {"noopt", s_ignore, 0},
1223 {"optim", s_ignore, 0},
1224 {"code16gcc", set_16bit_gcc_code_flag, CODE_16BIT},
1225 {"code16", set_code_flag, CODE_16BIT},
1226 {"code32", set_code_flag, CODE_32BIT},
da5f19a2 1227#ifdef BFD64
29b0f896 1228 {"code64", set_code_flag, CODE_64BIT},
da5f19a2 1229#endif
29b0f896
AM
1230 {"intel_syntax", set_intel_syntax, 1},
1231 {"att_syntax", set_intel_syntax, 0},
1efbbeb4
L
1232 {"intel_mnemonic", set_intel_mnemonic, 1},
1233 {"att_mnemonic", set_intel_mnemonic, 0},
db51cc60
L
1234 {"allow_index_reg", set_allow_index_reg, 1},
1235 {"disallow_index_reg", set_allow_index_reg, 0},
7bab8ab5
JB
1236 {"sse_check", set_check, 0},
1237 {"operand_check", set_check, 1},
3b22753a
L
1238#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1239 {"largecomm", handle_large_common, 0},
07a53e5c 1240#else
68d20676 1241 {"file", dwarf2_directive_file, 0},
07a53e5c
RH
1242 {"loc", dwarf2_directive_loc, 0},
1243 {"loc_mark_labels", dwarf2_directive_loc_mark_labels, 0},
3b22753a 1244#endif
6482c264
NC
1245#ifdef TE_PE
1246 {"secrel32", pe_directive_secrel, 0},
145667f8 1247 {"secidx", pe_directive_secidx, 0},
6482c264 1248#endif
29b0f896
AM
1249 {0, 0, 0}
1250};
1251
1252/* For interface with expression (). */
1253extern char *input_line_pointer;
1254
1255/* Hash table for instruction mnemonic lookup. */
629310ab 1256static htab_t op_hash;
29b0f896
AM
1257
1258/* Hash table for register lookup. */
629310ab 1259static htab_t reg_hash;
29b0f896 1260\f
ce8a8b2f
AM
1261 /* Various efficient no-op patterns for aligning code labels.
1262 Note: Don't try to assemble the instructions in the comments.
1263 0L and 0w are not legal. */
62a02d25
L
1264static const unsigned char f32_1[] =
1265 {0x90}; /* nop */
1266static const unsigned char f32_2[] =
1267 {0x66,0x90}; /* xchg %ax,%ax */
1268static const unsigned char f32_3[] =
1269 {0x8d,0x76,0x00}; /* leal 0(%esi),%esi */
1270static const unsigned char f32_4[] =
1271 {0x8d,0x74,0x26,0x00}; /* leal 0(%esi,1),%esi */
62a02d25
L
1272static const unsigned char f32_6[] =
1273 {0x8d,0xb6,0x00,0x00,0x00,0x00}; /* leal 0L(%esi),%esi */
1274static const unsigned char f32_7[] =
1275 {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal 0L(%esi,1),%esi */
62a02d25 1276static const unsigned char f16_3[] =
3ae729d5 1277 {0x8d,0x74,0x00}; /* lea 0(%si),%si */
62a02d25 1278static const unsigned char f16_4[] =
3ae729d5
L
1279 {0x8d,0xb4,0x00,0x00}; /* lea 0W(%si),%si */
1280static const unsigned char jump_disp8[] =
1281 {0xeb}; /* jmp disp8 */
1282static const unsigned char jump32_disp32[] =
1283 {0xe9}; /* jmp disp32 */
1284static const unsigned char jump16_disp32[] =
1285 {0x66,0xe9}; /* jmp disp32 */
62a02d25
L
1286/* 32-bit NOPs patterns. */
1287static const unsigned char *const f32_patt[] = {
3ae729d5 1288 f32_1, f32_2, f32_3, f32_4, NULL, f32_6, f32_7
62a02d25
L
1289};
1290/* 16-bit NOPs patterns. */
1291static const unsigned char *const f16_patt[] = {
3ae729d5 1292 f32_1, f32_2, f16_3, f16_4
62a02d25
L
1293};
1294/* nopl (%[re]ax) */
1295static const unsigned char alt_3[] =
1296 {0x0f,0x1f,0x00};
1297/* nopl 0(%[re]ax) */
1298static const unsigned char alt_4[] =
1299 {0x0f,0x1f,0x40,0x00};
1300/* nopl 0(%[re]ax,%[re]ax,1) */
1301static const unsigned char alt_5[] =
1302 {0x0f,0x1f,0x44,0x00,0x00};
1303/* nopw 0(%[re]ax,%[re]ax,1) */
1304static const unsigned char alt_6[] =
1305 {0x66,0x0f,0x1f,0x44,0x00,0x00};
1306/* nopl 0L(%[re]ax) */
1307static const unsigned char alt_7[] =
1308 {0x0f,0x1f,0x80,0x00,0x00,0x00,0x00};
1309/* nopl 0L(%[re]ax,%[re]ax,1) */
1310static const unsigned char alt_8[] =
1311 {0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1312/* nopw 0L(%[re]ax,%[re]ax,1) */
1313static const unsigned char alt_9[] =
1314 {0x66,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1315/* nopw %cs:0L(%[re]ax,%[re]ax,1) */
1316static const unsigned char alt_10[] =
1317 {0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
3ae729d5
L
1318/* data16 nopw %cs:0L(%eax,%eax,1) */
1319static const unsigned char alt_11[] =
1320 {0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
62a02d25
L
1321/* 32-bit and 64-bit NOPs patterns. */
1322static const unsigned char *const alt_patt[] = {
1323 f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
3ae729d5 1324 alt_9, alt_10, alt_11
62a02d25
L
1325};
1326
1327/* Genenerate COUNT bytes of NOPs to WHERE from PATT with the maximum
1328 size of a single NOP instruction MAX_SINGLE_NOP_SIZE. */
1329
1330static void
1331i386_output_nops (char *where, const unsigned char *const *patt,
1332 int count, int max_single_nop_size)
1333
1334{
3ae729d5
L
1335 /* Place the longer NOP first. */
1336 int last;
1337 int offset;
3076e594
NC
1338 const unsigned char *nops;
1339
1340 if (max_single_nop_size < 1)
1341 {
1342 as_fatal (_("i386_output_nops called to generate nops of at most %d bytes!"),
1343 max_single_nop_size);
1344 return;
1345 }
1346
1347 nops = patt[max_single_nop_size - 1];
3ae729d5
L
1348
1349 /* Use the smaller one if the requsted one isn't available. */
1350 if (nops == NULL)
62a02d25 1351 {
3ae729d5
L
1352 max_single_nop_size--;
1353 nops = patt[max_single_nop_size - 1];
62a02d25
L
1354 }
1355
3ae729d5
L
1356 last = count % max_single_nop_size;
1357
1358 count -= last;
1359 for (offset = 0; offset < count; offset += max_single_nop_size)
1360 memcpy (where + offset, nops, max_single_nop_size);
1361
1362 if (last)
1363 {
1364 nops = patt[last - 1];
1365 if (nops == NULL)
1366 {
1367 /* Use the smaller one plus one-byte NOP if the needed one
1368 isn't available. */
1369 last--;
1370 nops = patt[last - 1];
1371 memcpy (where + offset, nops, last);
1372 where[offset + last] = *patt[0];
1373 }
1374 else
1375 memcpy (where + offset, nops, last);
1376 }
62a02d25
L
1377}
1378
3ae729d5
L
1379static INLINE int
1380fits_in_imm7 (offsetT num)
1381{
1382 return (num & 0x7f) == num;
1383}
1384
1385static INLINE int
1386fits_in_imm31 (offsetT num)
1387{
1388 return (num & 0x7fffffff) == num;
1389}
62a02d25
L
1390
1391/* Genenerate COUNT bytes of NOPs to WHERE with the maximum size of a
1392 single NOP instruction LIMIT. */
1393
1394void
3ae729d5 1395i386_generate_nops (fragS *fragP, char *where, offsetT count, int limit)
62a02d25 1396{
3ae729d5 1397 const unsigned char *const *patt = NULL;
62a02d25 1398 int max_single_nop_size;
3ae729d5
L
1399 /* Maximum number of NOPs before switching to jump over NOPs. */
1400 int max_number_of_nops;
62a02d25 1401
3ae729d5 1402 switch (fragP->fr_type)
62a02d25 1403 {
3ae729d5
L
1404 case rs_fill_nop:
1405 case rs_align_code:
1406 break;
e379e5f3
L
1407 case rs_machine_dependent:
1408 /* Allow NOP padding for jumps and calls. */
1409 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
1410 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
1411 break;
1412 /* Fall through. */
3ae729d5 1413 default:
62a02d25
L
1414 return;
1415 }
1416
ccc9c027
L
1417 /* We need to decide which NOP sequence to use for 32bit and
1418 64bit. When -mtune= is used:
4eed87de 1419
76bc74dc
L
1420 1. For PROCESSOR_I386, PROCESSOR_I486, PROCESSOR_PENTIUM and
1421 PROCESSOR_GENERIC32, f32_patt will be used.
80b8656c
L
1422 2. For the rest, alt_patt will be used.
1423
1424 When -mtune= isn't used, alt_patt will be used if
22109423 1425 cpu_arch_isa_flags has CpuNop. Otherwise, f32_patt will
76bc74dc 1426 be used.
ccc9c027
L
1427
1428 When -march= or .arch is used, we can't use anything beyond
1429 cpu_arch_isa_flags. */
1430
1431 if (flag_code == CODE_16BIT)
1432 {
3ae729d5
L
1433 patt = f16_patt;
1434 max_single_nop_size = sizeof (f16_patt) / sizeof (f16_patt[0]);
1435 /* Limit number of NOPs to 2 in 16-bit mode. */
1436 max_number_of_nops = 2;
252b5132 1437 }
33fef721 1438 else
ccc9c027 1439 {
fbf3f584 1440 if (fragP->tc_frag_data.isa == PROCESSOR_UNKNOWN)
ccc9c027
L
1441 {
1442 /* PROCESSOR_UNKNOWN means that all ISAs may be used. */
1443 switch (cpu_arch_tune)
1444 {
1445 case PROCESSOR_UNKNOWN:
1446 /* We use cpu_arch_isa_flags to check if we SHOULD
22109423
L
1447 optimize with nops. */
1448 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1449 patt = alt_patt;
ccc9c027
L
1450 else
1451 patt = f32_patt;
1452 break;
ccc9c027
L
1453 case PROCESSOR_PENTIUM4:
1454 case PROCESSOR_NOCONA:
ef05d495 1455 case PROCESSOR_CORE:
76bc74dc 1456 case PROCESSOR_CORE2:
bd5295b2 1457 case PROCESSOR_COREI7:
76bc74dc 1458 case PROCESSOR_GENERIC64:
ccc9c027
L
1459 case PROCESSOR_K6:
1460 case PROCESSOR_ATHLON:
1461 case PROCESSOR_K8:
4eed87de 1462 case PROCESSOR_AMDFAM10:
8aedb9fe 1463 case PROCESSOR_BD:
029f3522 1464 case PROCESSOR_ZNVER:
7b458c12 1465 case PROCESSOR_BT:
80b8656c 1466 patt = alt_patt;
ccc9c027 1467 break;
76bc74dc 1468 case PROCESSOR_I386:
ccc9c027
L
1469 case PROCESSOR_I486:
1470 case PROCESSOR_PENTIUM:
2dde1948 1471 case PROCESSOR_PENTIUMPRO:
81486035 1472 case PROCESSOR_IAMCU:
ccc9c027
L
1473 case PROCESSOR_GENERIC32:
1474 patt = f32_patt;
1475 break;
c368d2a8
JB
1476 case PROCESSOR_NONE:
1477 abort ();
4eed87de 1478 }
ccc9c027
L
1479 }
1480 else
1481 {
fbf3f584 1482 switch (fragP->tc_frag_data.tune)
ccc9c027
L
1483 {
1484 case PROCESSOR_UNKNOWN:
e6a14101 1485 /* When cpu_arch_isa is set, cpu_arch_tune shouldn't be
ccc9c027
L
1486 PROCESSOR_UNKNOWN. */
1487 abort ();
1488 break;
1489
76bc74dc 1490 case PROCESSOR_I386:
ccc9c027
L
1491 case PROCESSOR_I486:
1492 case PROCESSOR_PENTIUM:
81486035 1493 case PROCESSOR_IAMCU:
ccc9c027
L
1494 case PROCESSOR_K6:
1495 case PROCESSOR_ATHLON:
1496 case PROCESSOR_K8:
4eed87de 1497 case PROCESSOR_AMDFAM10:
8aedb9fe 1498 case PROCESSOR_BD:
029f3522 1499 case PROCESSOR_ZNVER:
7b458c12 1500 case PROCESSOR_BT:
ccc9c027
L
1501 case PROCESSOR_GENERIC32:
1502 /* We use cpu_arch_isa_flags to check if we CAN optimize
22109423
L
1503 with nops. */
1504 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1505 patt = alt_patt;
ccc9c027
L
1506 else
1507 patt = f32_patt;
1508 break;
76bc74dc
L
1509 case PROCESSOR_PENTIUMPRO:
1510 case PROCESSOR_PENTIUM4:
1511 case PROCESSOR_NOCONA:
1512 case PROCESSOR_CORE:
ef05d495 1513 case PROCESSOR_CORE2:
bd5295b2 1514 case PROCESSOR_COREI7:
22109423 1515 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1516 patt = alt_patt;
ccc9c027
L
1517 else
1518 patt = f32_patt;
1519 break;
1520 case PROCESSOR_GENERIC64:
80b8656c 1521 patt = alt_patt;
ccc9c027 1522 break;
c368d2a8
JB
1523 case PROCESSOR_NONE:
1524 abort ();
4eed87de 1525 }
ccc9c027
L
1526 }
1527
76bc74dc
L
1528 if (patt == f32_patt)
1529 {
3ae729d5
L
1530 max_single_nop_size = sizeof (f32_patt) / sizeof (f32_patt[0]);
1531 /* Limit number of NOPs to 2 for older processors. */
1532 max_number_of_nops = 2;
76bc74dc
L
1533 }
1534 else
1535 {
3ae729d5
L
1536 max_single_nop_size = sizeof (alt_patt) / sizeof (alt_patt[0]);
1537 /* Limit number of NOPs to 7 for newer processors. */
1538 max_number_of_nops = 7;
1539 }
1540 }
1541
1542 if (limit == 0)
1543 limit = max_single_nop_size;
1544
1545 if (fragP->fr_type == rs_fill_nop)
1546 {
1547 /* Output NOPs for .nop directive. */
1548 if (limit > max_single_nop_size)
1549 {
1550 as_bad_where (fragP->fr_file, fragP->fr_line,
1551 _("invalid single nop size: %d "
1552 "(expect within [0, %d])"),
1553 limit, max_single_nop_size);
1554 return;
1555 }
1556 }
e379e5f3 1557 else if (fragP->fr_type != rs_machine_dependent)
3ae729d5
L
1558 fragP->fr_var = count;
1559
1560 if ((count / max_single_nop_size) > max_number_of_nops)
1561 {
1562 /* Generate jump over NOPs. */
1563 offsetT disp = count - 2;
1564 if (fits_in_imm7 (disp))
1565 {
1566 /* Use "jmp disp8" if possible. */
1567 count = disp;
1568 where[0] = jump_disp8[0];
1569 where[1] = count;
1570 where += 2;
1571 }
1572 else
1573 {
1574 unsigned int size_of_jump;
1575
1576 if (flag_code == CODE_16BIT)
1577 {
1578 where[0] = jump16_disp32[0];
1579 where[1] = jump16_disp32[1];
1580 size_of_jump = 2;
1581 }
1582 else
1583 {
1584 where[0] = jump32_disp32[0];
1585 size_of_jump = 1;
1586 }
1587
1588 count -= size_of_jump + 4;
1589 if (!fits_in_imm31 (count))
1590 {
1591 as_bad_where (fragP->fr_file, fragP->fr_line,
1592 _("jump over nop padding out of range"));
1593 return;
1594 }
1595
1596 md_number_to_chars (where + size_of_jump, count, 4);
1597 where += size_of_jump + 4;
76bc74dc 1598 }
ccc9c027 1599 }
3ae729d5
L
1600
1601 /* Generate multiple NOPs. */
1602 i386_output_nops (where, patt, count, limit);
252b5132
RH
1603}
1604
c6fb90c8 1605static INLINE int
0dfbf9d7 1606operand_type_all_zero (const union i386_operand_type *x)
40fb9820 1607{
0dfbf9d7 1608 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1609 {
1610 case 3:
0dfbf9d7 1611 if (x->array[2])
c6fb90c8 1612 return 0;
1a0670f3 1613 /* Fall through. */
c6fb90c8 1614 case 2:
0dfbf9d7 1615 if (x->array[1])
c6fb90c8 1616 return 0;
1a0670f3 1617 /* Fall through. */
c6fb90c8 1618 case 1:
0dfbf9d7 1619 return !x->array[0];
c6fb90c8
L
1620 default:
1621 abort ();
1622 }
40fb9820
L
1623}
1624
c6fb90c8 1625static INLINE void
0dfbf9d7 1626operand_type_set (union i386_operand_type *x, unsigned int v)
40fb9820 1627{
0dfbf9d7 1628 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1629 {
1630 case 3:
0dfbf9d7 1631 x->array[2] = v;
1a0670f3 1632 /* Fall through. */
c6fb90c8 1633 case 2:
0dfbf9d7 1634 x->array[1] = v;
1a0670f3 1635 /* Fall through. */
c6fb90c8 1636 case 1:
0dfbf9d7 1637 x->array[0] = v;
1a0670f3 1638 /* Fall through. */
c6fb90c8
L
1639 break;
1640 default:
1641 abort ();
1642 }
bab6aec1
JB
1643
1644 x->bitfield.class = ClassNone;
75e5731b 1645 x->bitfield.instance = InstanceNone;
c6fb90c8 1646}
40fb9820 1647
c6fb90c8 1648static INLINE int
0dfbf9d7
L
1649operand_type_equal (const union i386_operand_type *x,
1650 const union i386_operand_type *y)
c6fb90c8 1651{
0dfbf9d7 1652 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1653 {
1654 case 3:
0dfbf9d7 1655 if (x->array[2] != y->array[2])
c6fb90c8 1656 return 0;
1a0670f3 1657 /* Fall through. */
c6fb90c8 1658 case 2:
0dfbf9d7 1659 if (x->array[1] != y->array[1])
c6fb90c8 1660 return 0;
1a0670f3 1661 /* Fall through. */
c6fb90c8 1662 case 1:
0dfbf9d7 1663 return x->array[0] == y->array[0];
c6fb90c8
L
1664 break;
1665 default:
1666 abort ();
1667 }
1668}
40fb9820 1669
0dfbf9d7
L
1670static INLINE int
1671cpu_flags_all_zero (const union i386_cpu_flags *x)
1672{
1673 switch (ARRAY_SIZE(x->array))
1674 {
75f8266a
KL
1675 case 5:
1676 if (x->array[4])
1677 return 0;
1678 /* Fall through. */
53467f57
IT
1679 case 4:
1680 if (x->array[3])
1681 return 0;
1682 /* Fall through. */
0dfbf9d7
L
1683 case 3:
1684 if (x->array[2])
1685 return 0;
1a0670f3 1686 /* Fall through. */
0dfbf9d7
L
1687 case 2:
1688 if (x->array[1])
1689 return 0;
1a0670f3 1690 /* Fall through. */
0dfbf9d7
L
1691 case 1:
1692 return !x->array[0];
1693 default:
1694 abort ();
1695 }
1696}
1697
0dfbf9d7
L
1698static INLINE int
1699cpu_flags_equal (const union i386_cpu_flags *x,
1700 const union i386_cpu_flags *y)
1701{
1702 switch (ARRAY_SIZE(x->array))
1703 {
75f8266a
KL
1704 case 5:
1705 if (x->array[4] != y->array[4])
1706 return 0;
1707 /* Fall through. */
53467f57
IT
1708 case 4:
1709 if (x->array[3] != y->array[3])
1710 return 0;
1711 /* Fall through. */
0dfbf9d7
L
1712 case 3:
1713 if (x->array[2] != y->array[2])
1714 return 0;
1a0670f3 1715 /* Fall through. */
0dfbf9d7
L
1716 case 2:
1717 if (x->array[1] != y->array[1])
1718 return 0;
1a0670f3 1719 /* Fall through. */
0dfbf9d7
L
1720 case 1:
1721 return x->array[0] == y->array[0];
1722 break;
1723 default:
1724 abort ();
1725 }
1726}
c6fb90c8
L
1727
1728static INLINE int
1729cpu_flags_check_cpu64 (i386_cpu_flags f)
1730{
1731 return !((flag_code == CODE_64BIT && f.bitfield.cpuno64)
1732 || (flag_code != CODE_64BIT && f.bitfield.cpu64));
40fb9820
L
1733}
1734
c6fb90c8
L
1735static INLINE i386_cpu_flags
1736cpu_flags_and (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1737{
c6fb90c8
L
1738 switch (ARRAY_SIZE (x.array))
1739 {
75f8266a
KL
1740 case 5:
1741 x.array [4] &= y.array [4];
1742 /* Fall through. */
53467f57
IT
1743 case 4:
1744 x.array [3] &= y.array [3];
1745 /* Fall through. */
c6fb90c8
L
1746 case 3:
1747 x.array [2] &= y.array [2];
1a0670f3 1748 /* Fall through. */
c6fb90c8
L
1749 case 2:
1750 x.array [1] &= y.array [1];
1a0670f3 1751 /* Fall through. */
c6fb90c8
L
1752 case 1:
1753 x.array [0] &= y.array [0];
1754 break;
1755 default:
1756 abort ();
1757 }
1758 return x;
1759}
40fb9820 1760
c6fb90c8
L
1761static INLINE i386_cpu_flags
1762cpu_flags_or (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1763{
c6fb90c8 1764 switch (ARRAY_SIZE (x.array))
40fb9820 1765 {
75f8266a
KL
1766 case 5:
1767 x.array [4] |= y.array [4];
1768 /* Fall through. */
53467f57
IT
1769 case 4:
1770 x.array [3] |= y.array [3];
1771 /* Fall through. */
c6fb90c8
L
1772 case 3:
1773 x.array [2] |= y.array [2];
1a0670f3 1774 /* Fall through. */
c6fb90c8
L
1775 case 2:
1776 x.array [1] |= y.array [1];
1a0670f3 1777 /* Fall through. */
c6fb90c8
L
1778 case 1:
1779 x.array [0] |= y.array [0];
40fb9820
L
1780 break;
1781 default:
1782 abort ();
1783 }
40fb9820
L
1784 return x;
1785}
1786
309d3373
JB
1787static INLINE i386_cpu_flags
1788cpu_flags_and_not (i386_cpu_flags x, i386_cpu_flags y)
1789{
1790 switch (ARRAY_SIZE (x.array))
1791 {
75f8266a
KL
1792 case 5:
1793 x.array [4] &= ~y.array [4];
1794 /* Fall through. */
53467f57
IT
1795 case 4:
1796 x.array [3] &= ~y.array [3];
1797 /* Fall through. */
309d3373
JB
1798 case 3:
1799 x.array [2] &= ~y.array [2];
1a0670f3 1800 /* Fall through. */
309d3373
JB
1801 case 2:
1802 x.array [1] &= ~y.array [1];
1a0670f3 1803 /* Fall through. */
309d3373
JB
1804 case 1:
1805 x.array [0] &= ~y.array [0];
1806 break;
1807 default:
1808 abort ();
1809 }
1810 return x;
1811}
1812
6c0946d0
JB
1813static const i386_cpu_flags avx512 = CPU_ANY_AVX512F_FLAGS;
1814
c0f3af97
L
1815#define CPU_FLAGS_ARCH_MATCH 0x1
1816#define CPU_FLAGS_64BIT_MATCH 0x2
1817
c0f3af97 1818#define CPU_FLAGS_PERFECT_MATCH \
db12e14e 1819 (CPU_FLAGS_ARCH_MATCH | CPU_FLAGS_64BIT_MATCH)
c0f3af97
L
1820
1821/* Return CPU flags match bits. */
3629bb00 1822
40fb9820 1823static int
d3ce72d0 1824cpu_flags_match (const insn_template *t)
40fb9820 1825{
c0f3af97
L
1826 i386_cpu_flags x = t->cpu_flags;
1827 int match = cpu_flags_check_cpu64 (x) ? CPU_FLAGS_64BIT_MATCH : 0;
40fb9820
L
1828
1829 x.bitfield.cpu64 = 0;
1830 x.bitfield.cpuno64 = 0;
1831
0dfbf9d7 1832 if (cpu_flags_all_zero (&x))
c0f3af97
L
1833 {
1834 /* This instruction is available on all archs. */
db12e14e 1835 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 1836 }
3629bb00
L
1837 else
1838 {
c0f3af97 1839 /* This instruction is available only on some archs. */
3629bb00
L
1840 i386_cpu_flags cpu = cpu_arch_flags;
1841
ab592e75
JB
1842 /* AVX512VL is no standalone feature - match it and then strip it. */
1843 if (x.bitfield.cpuavx512vl && !cpu.bitfield.cpuavx512vl)
1844 return match;
1845 x.bitfield.cpuavx512vl = 0;
1846
22c36940
JB
1847 /* AVX and AVX2 present at the same time express an operand size
1848 dependency - strip AVX2 for the purposes here. The operand size
1849 dependent check occurs in check_vecOperands(). */
1850 if (x.bitfield.cpuavx && x.bitfield.cpuavx2)
1851 x.bitfield.cpuavx2 = 0;
1852
3629bb00 1853 cpu = cpu_flags_and (x, cpu);
c0f3af97
L
1854 if (!cpu_flags_all_zero (&cpu))
1855 {
57392598 1856 if (x.bitfield.cpuavx)
a5ff0eb2 1857 {
929f69fa 1858 /* We need to check a few extra flags with AVX. */
b9d49817 1859 if (cpu.bitfield.cpuavx
40d231b4
JB
1860 && (!t->opcode_modifier.sse2avx
1861 || (sse2avx && !i.prefix[DATA_PREFIX]))
b9d49817 1862 && (!x.bitfield.cpuaes || cpu.bitfield.cpuaes)
929f69fa 1863 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
b9d49817
JB
1864 && (!x.bitfield.cpupclmul || cpu.bitfield.cpupclmul))
1865 match |= CPU_FLAGS_ARCH_MATCH;
a5ff0eb2 1866 }
929f69fa
JB
1867 else if (x.bitfield.cpuavx512f)
1868 {
1869 /* We need to check a few extra flags with AVX512F. */
1870 if (cpu.bitfield.cpuavx512f
1871 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
1872 && (!x.bitfield.cpuvaes || cpu.bitfield.cpuvaes)
1873 && (!x.bitfield.cpuvpclmulqdq || cpu.bitfield.cpuvpclmulqdq))
1874 match |= CPU_FLAGS_ARCH_MATCH;
1875 }
a5ff0eb2 1876 else
db12e14e 1877 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 1878 }
3629bb00 1879 }
c0f3af97 1880 return match;
40fb9820
L
1881}
1882
c6fb90c8
L
1883static INLINE i386_operand_type
1884operand_type_and (i386_operand_type x, i386_operand_type y)
40fb9820 1885{
bab6aec1
JB
1886 if (x.bitfield.class != y.bitfield.class)
1887 x.bitfield.class = ClassNone;
75e5731b
JB
1888 if (x.bitfield.instance != y.bitfield.instance)
1889 x.bitfield.instance = InstanceNone;
bab6aec1 1890
c6fb90c8
L
1891 switch (ARRAY_SIZE (x.array))
1892 {
1893 case 3:
1894 x.array [2] &= y.array [2];
1a0670f3 1895 /* Fall through. */
c6fb90c8
L
1896 case 2:
1897 x.array [1] &= y.array [1];
1a0670f3 1898 /* Fall through. */
c6fb90c8
L
1899 case 1:
1900 x.array [0] &= y.array [0];
1901 break;
1902 default:
1903 abort ();
1904 }
1905 return x;
40fb9820
L
1906}
1907
73053c1f
JB
1908static INLINE i386_operand_type
1909operand_type_and_not (i386_operand_type x, i386_operand_type y)
1910{
bab6aec1 1911 gas_assert (y.bitfield.class == ClassNone);
75e5731b 1912 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 1913
73053c1f
JB
1914 switch (ARRAY_SIZE (x.array))
1915 {
1916 case 3:
1917 x.array [2] &= ~y.array [2];
1918 /* Fall through. */
1919 case 2:
1920 x.array [1] &= ~y.array [1];
1921 /* Fall through. */
1922 case 1:
1923 x.array [0] &= ~y.array [0];
1924 break;
1925 default:
1926 abort ();
1927 }
1928 return x;
1929}
1930
c6fb90c8
L
1931static INLINE i386_operand_type
1932operand_type_or (i386_operand_type x, i386_operand_type y)
40fb9820 1933{
bab6aec1
JB
1934 gas_assert (x.bitfield.class == ClassNone ||
1935 y.bitfield.class == ClassNone ||
1936 x.bitfield.class == y.bitfield.class);
75e5731b
JB
1937 gas_assert (x.bitfield.instance == InstanceNone ||
1938 y.bitfield.instance == InstanceNone ||
1939 x.bitfield.instance == y.bitfield.instance);
bab6aec1 1940
c6fb90c8 1941 switch (ARRAY_SIZE (x.array))
40fb9820 1942 {
c6fb90c8
L
1943 case 3:
1944 x.array [2] |= y.array [2];
1a0670f3 1945 /* Fall through. */
c6fb90c8
L
1946 case 2:
1947 x.array [1] |= y.array [1];
1a0670f3 1948 /* Fall through. */
c6fb90c8
L
1949 case 1:
1950 x.array [0] |= y.array [0];
40fb9820
L
1951 break;
1952 default:
1953 abort ();
1954 }
c6fb90c8
L
1955 return x;
1956}
40fb9820 1957
c6fb90c8
L
1958static INLINE i386_operand_type
1959operand_type_xor (i386_operand_type x, i386_operand_type y)
1960{
bab6aec1 1961 gas_assert (y.bitfield.class == ClassNone);
75e5731b 1962 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 1963
c6fb90c8
L
1964 switch (ARRAY_SIZE (x.array))
1965 {
1966 case 3:
1967 x.array [2] ^= y.array [2];
1a0670f3 1968 /* Fall through. */
c6fb90c8
L
1969 case 2:
1970 x.array [1] ^= y.array [1];
1a0670f3 1971 /* Fall through. */
c6fb90c8
L
1972 case 1:
1973 x.array [0] ^= y.array [0];
1974 break;
1975 default:
1976 abort ();
1977 }
40fb9820
L
1978 return x;
1979}
1980
05909f23
JB
1981static const i386_operand_type anydisp = {
1982 .bitfield = { .disp8 = 1, .disp16 = 1, .disp32 = 1, .disp64 = 1 }
1983};
40fb9820
L
1984
1985enum operand_type
1986{
1987 reg,
40fb9820
L
1988 imm,
1989 disp,
1990 anymem
1991};
1992
c6fb90c8 1993static INLINE int
40fb9820
L
1994operand_type_check (i386_operand_type t, enum operand_type c)
1995{
1996 switch (c)
1997 {
1998 case reg:
bab6aec1 1999 return t.bitfield.class == Reg;
40fb9820 2000
40fb9820
L
2001 case imm:
2002 return (t.bitfield.imm8
2003 || t.bitfield.imm8s
2004 || t.bitfield.imm16
2005 || t.bitfield.imm32
2006 || t.bitfield.imm32s
2007 || t.bitfield.imm64);
2008
2009 case disp:
2010 return (t.bitfield.disp8
2011 || t.bitfield.disp16
2012 || t.bitfield.disp32
40fb9820
L
2013 || t.bitfield.disp64);
2014
2015 case anymem:
2016 return (t.bitfield.disp8
2017 || t.bitfield.disp16
2018 || t.bitfield.disp32
40fb9820
L
2019 || t.bitfield.disp64
2020 || t.bitfield.baseindex);
2021
2022 default:
2023 abort ();
2024 }
2cfe26b6
AM
2025
2026 return 0;
40fb9820
L
2027}
2028
7a54636a
L
2029/* Return 1 if there is no conflict in 8bit/16bit/32bit/64bit/80bit size
2030 between operand GIVEN and opeand WANTED for instruction template T. */
5c07affc
L
2031
2032static INLINE int
7a54636a
L
2033match_operand_size (const insn_template *t, unsigned int wanted,
2034 unsigned int given)
5c07affc 2035{
3ac21baa
JB
2036 return !((i.types[given].bitfield.byte
2037 && !t->operand_types[wanted].bitfield.byte)
2038 || (i.types[given].bitfield.word
2039 && !t->operand_types[wanted].bitfield.word)
2040 || (i.types[given].bitfield.dword
2041 && !t->operand_types[wanted].bitfield.dword)
2042 || (i.types[given].bitfield.qword
9db83a32
JB
2043 && (!t->operand_types[wanted].bitfield.qword
2044 /* Don't allow 64-bit (memory) operands outside of 64-bit
2045 mode, when they're used where a 64-bit GPR could also
2046 be used. Checking is needed for Intel Syntax only. */
2047 || (intel_syntax
2048 && flag_code != CODE_64BIT
2049 && (t->operand_types[wanted].bitfield.class == Reg
2050 || t->operand_types[wanted].bitfield.class == Accum
2051 || t->opcode_modifier.isstring))))
3ac21baa
JB
2052 || (i.types[given].bitfield.tbyte
2053 && !t->operand_types[wanted].bitfield.tbyte));
5c07affc
L
2054}
2055
dd40ce22
L
2056/* Return 1 if there is no conflict in SIMD register between operand
2057 GIVEN and opeand WANTED for instruction template T. */
1b54b8d7
JB
2058
2059static INLINE int
dd40ce22
L
2060match_simd_size (const insn_template *t, unsigned int wanted,
2061 unsigned int given)
1b54b8d7 2062{
3ac21baa
JB
2063 return !((i.types[given].bitfield.xmmword
2064 && !t->operand_types[wanted].bitfield.xmmword)
2065 || (i.types[given].bitfield.ymmword
2066 && !t->operand_types[wanted].bitfield.ymmword)
2067 || (i.types[given].bitfield.zmmword
260cd341
LC
2068 && !t->operand_types[wanted].bitfield.zmmword)
2069 || (i.types[given].bitfield.tmmword
2070 && !t->operand_types[wanted].bitfield.tmmword));
1b54b8d7
JB
2071}
2072
7a54636a
L
2073/* Return 1 if there is no conflict in any size between operand GIVEN
2074 and opeand WANTED for instruction template T. */
5c07affc
L
2075
2076static INLINE int
dd40ce22
L
2077match_mem_size (const insn_template *t, unsigned int wanted,
2078 unsigned int given)
5c07affc 2079{
7a54636a 2080 return (match_operand_size (t, wanted, given)
3ac21baa 2081 && !((i.types[given].bitfield.unspecified
5273a3cd 2082 && !i.broadcast.type
a5748e0d 2083 && !i.broadcast.bytes
3ac21baa
JB
2084 && !t->operand_types[wanted].bitfield.unspecified)
2085 || (i.types[given].bitfield.fword
2086 && !t->operand_types[wanted].bitfield.fword)
1b54b8d7
JB
2087 /* For scalar opcode templates to allow register and memory
2088 operands at the same time, some special casing is needed
d6793fa1
JB
2089 here. Also for v{,p}broadcast*, {,v}pmov{s,z}*, and
2090 down-conversion vpmov*. */
3528c362 2091 || ((t->operand_types[wanted].bitfield.class == RegSIMD
bc49bfd8
JB
2092 && t->operand_types[wanted].bitfield.byte
2093 + t->operand_types[wanted].bitfield.word
2094 + t->operand_types[wanted].bitfield.dword
2095 + t->operand_types[wanted].bitfield.qword
2096 > !!t->opcode_modifier.broadcast)
3ac21baa
JB
2097 ? (i.types[given].bitfield.xmmword
2098 || i.types[given].bitfield.ymmword
2099 || i.types[given].bitfield.zmmword)
2100 : !match_simd_size(t, wanted, given))));
5c07affc
L
2101}
2102
3ac21baa
JB
2103/* Return value has MATCH_STRAIGHT set if there is no size conflict on any
2104 operands for instruction template T, and it has MATCH_REVERSE set if there
2105 is no size conflict on any operands for the template with operands reversed
2106 (and the template allows for reversing in the first place). */
5c07affc 2107
3ac21baa
JB
2108#define MATCH_STRAIGHT 1
2109#define MATCH_REVERSE 2
2110
2111static INLINE unsigned int
d3ce72d0 2112operand_size_match (const insn_template *t)
5c07affc 2113{
3ac21baa 2114 unsigned int j, match = MATCH_STRAIGHT;
5c07affc 2115
0cfa3eb3 2116 /* Don't check non-absolute jump instructions. */
5c07affc 2117 if (t->opcode_modifier.jump
0cfa3eb3 2118 && t->opcode_modifier.jump != JUMP_ABSOLUTE)
5c07affc
L
2119 return match;
2120
2121 /* Check memory and accumulator operand size. */
2122 for (j = 0; j < i.operands; j++)
2123 {
3528c362
JB
2124 if (i.types[j].bitfield.class != Reg
2125 && i.types[j].bitfield.class != RegSIMD
255571cd 2126 && t->opcode_modifier.operandconstraint == ANY_SIZE)
5c07affc
L
2127 continue;
2128
bab6aec1 2129 if (t->operand_types[j].bitfield.class == Reg
7a54636a 2130 && !match_operand_size (t, j, j))
5c07affc
L
2131 {
2132 match = 0;
2133 break;
2134 }
2135
3528c362 2136 if (t->operand_types[j].bitfield.class == RegSIMD
3ac21baa 2137 && !match_simd_size (t, j, j))
1b54b8d7
JB
2138 {
2139 match = 0;
2140 break;
2141 }
2142
75e5731b 2143 if (t->operand_types[j].bitfield.instance == Accum
7a54636a 2144 && (!match_operand_size (t, j, j) || !match_simd_size (t, j, j)))
1b54b8d7
JB
2145 {
2146 match = 0;
2147 break;
2148 }
2149
c48dadc9 2150 if ((i.flags[j] & Operand_Mem) && !match_mem_size (t, j, j))
5c07affc
L
2151 {
2152 match = 0;
2153 break;
2154 }
2155 }
2156
3ac21baa 2157 if (!t->opcode_modifier.d)
7b94647a 2158 return match;
5c07affc
L
2159
2160 /* Check reverse. */
aa180741 2161 gas_assert (i.operands >= 2);
5c07affc 2162
f5eb1d70 2163 for (j = 0; j < i.operands; j++)
5c07affc 2164 {
f5eb1d70
JB
2165 unsigned int given = i.operands - j - 1;
2166
aa180741 2167 /* For FMA4 and XOP insns VEX.W controls just the first two
8bd915b7 2168 register operands. */
aa180741 2169 if (t->cpu_flags.bitfield.cpufma4 || t->cpu_flags.bitfield.cpuxop)
8bd915b7
JB
2170 given = j < 2 ? 1 - j : j;
2171
bab6aec1 2172 if (t->operand_types[j].bitfield.class == Reg
f5eb1d70 2173 && !match_operand_size (t, j, given))
7b94647a 2174 return match;
5c07affc 2175
3528c362 2176 if (t->operand_types[j].bitfield.class == RegSIMD
f5eb1d70 2177 && !match_simd_size (t, j, given))
7b94647a 2178 return match;
dbbc8b7e 2179
75e5731b 2180 if (t->operand_types[j].bitfield.instance == Accum
f5eb1d70
JB
2181 && (!match_operand_size (t, j, given)
2182 || !match_simd_size (t, j, given)))
7b94647a 2183 return match;
dbbc8b7e 2184
f5eb1d70 2185 if ((i.flags[given] & Operand_Mem) && !match_mem_size (t, j, given))
7b94647a 2186 return match;
5c07affc
L
2187 }
2188
3ac21baa 2189 return match | MATCH_REVERSE;
5c07affc
L
2190}
2191
c6fb90c8 2192static INLINE int
40fb9820
L
2193operand_type_match (i386_operand_type overlap,
2194 i386_operand_type given)
2195{
2196 i386_operand_type temp = overlap;
2197
7d5e4556 2198 temp.bitfield.unspecified = 0;
5c07affc
L
2199 temp.bitfield.byte = 0;
2200 temp.bitfield.word = 0;
2201 temp.bitfield.dword = 0;
2202 temp.bitfield.fword = 0;
2203 temp.bitfield.qword = 0;
2204 temp.bitfield.tbyte = 0;
2205 temp.bitfield.xmmword = 0;
c0f3af97 2206 temp.bitfield.ymmword = 0;
43234a1e 2207 temp.bitfield.zmmword = 0;
260cd341 2208 temp.bitfield.tmmword = 0;
0dfbf9d7 2209 if (operand_type_all_zero (&temp))
891edac4 2210 goto mismatch;
40fb9820 2211
6f2f06be 2212 if (given.bitfield.baseindex == overlap.bitfield.baseindex)
891edac4
L
2213 return 1;
2214
dc1e8a47 2215 mismatch:
a65babc9 2216 i.error = operand_type_mismatch;
891edac4 2217 return 0;
40fb9820
L
2218}
2219
7d5e4556 2220/* If given types g0 and g1 are registers they must be of the same type
10c17abd 2221 unless the expected operand type register overlap is null.
8ee52bcf 2222 Intel syntax sized memory operands are also checked here. */
40fb9820 2223
c6fb90c8 2224static INLINE int
dc821c5f 2225operand_type_register_match (i386_operand_type g0,
40fb9820 2226 i386_operand_type t0,
40fb9820
L
2227 i386_operand_type g1,
2228 i386_operand_type t1)
2229{
bab6aec1 2230 if (g0.bitfield.class != Reg
3528c362 2231 && g0.bitfield.class != RegSIMD
8ee52bcf
JB
2232 && (g0.bitfield.unspecified
2233 || !operand_type_check (g0, anymem)))
40fb9820
L
2234 return 1;
2235
bab6aec1 2236 if (g1.bitfield.class != Reg
3528c362 2237 && g1.bitfield.class != RegSIMD
8ee52bcf
JB
2238 && (g1.bitfield.unspecified
2239 || !operand_type_check (g1, anymem)))
40fb9820
L
2240 return 1;
2241
dc821c5f
JB
2242 if (g0.bitfield.byte == g1.bitfield.byte
2243 && g0.bitfield.word == g1.bitfield.word
2244 && g0.bitfield.dword == g1.bitfield.dword
10c17abd
JB
2245 && g0.bitfield.qword == g1.bitfield.qword
2246 && g0.bitfield.xmmword == g1.bitfield.xmmword
2247 && g0.bitfield.ymmword == g1.bitfield.ymmword
2248 && g0.bitfield.zmmword == g1.bitfield.zmmword)
40fb9820
L
2249 return 1;
2250
c4d09633
JB
2251 /* If expectations overlap in no more than a single size, all is fine. */
2252 g0 = operand_type_and (t0, t1);
2253 if (g0.bitfield.byte
2254 + g0.bitfield.word
2255 + g0.bitfield.dword
2256 + g0.bitfield.qword
2257 + g0.bitfield.xmmword
2258 + g0.bitfield.ymmword
2259 + g0.bitfield.zmmword <= 1)
891edac4
L
2260 return 1;
2261
a65babc9 2262 i.error = register_type_mismatch;
891edac4
L
2263
2264 return 0;
40fb9820
L
2265}
2266
4c692bc7
JB
2267static INLINE unsigned int
2268register_number (const reg_entry *r)
2269{
2270 unsigned int nr = r->reg_num;
2271
2272 if (r->reg_flags & RegRex)
2273 nr += 8;
2274
200cbe0f
L
2275 if (r->reg_flags & RegVRex)
2276 nr += 16;
2277
4c692bc7
JB
2278 return nr;
2279}
2280
252b5132 2281static INLINE unsigned int
40fb9820 2282mode_from_disp_size (i386_operand_type t)
252b5132 2283{
b5014f7a 2284 if (t.bitfield.disp8)
40fb9820
L
2285 return 1;
2286 else if (t.bitfield.disp16
a775efc8 2287 || t.bitfield.disp32)
40fb9820
L
2288 return 2;
2289 else
2290 return 0;
252b5132
RH
2291}
2292
2293static INLINE int
65879393 2294fits_in_signed_byte (addressT num)
252b5132 2295{
65879393 2296 return num + 0x80 <= 0xff;
47926f60 2297}
252b5132
RH
2298
2299static INLINE int
65879393 2300fits_in_unsigned_byte (addressT num)
252b5132 2301{
65879393 2302 return num <= 0xff;
47926f60 2303}
252b5132
RH
2304
2305static INLINE int
65879393 2306fits_in_unsigned_word (addressT num)
252b5132 2307{
65879393 2308 return num <= 0xffff;
47926f60 2309}
252b5132
RH
2310
2311static INLINE int
65879393 2312fits_in_signed_word (addressT num)
252b5132 2313{
65879393 2314 return num + 0x8000 <= 0xffff;
47926f60 2315}
2a962e6d 2316
3e73aa7c 2317static INLINE int
65879393 2318fits_in_signed_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2319{
2320#ifndef BFD64
2321 return 1;
2322#else
65879393 2323 return num + 0x80000000 <= 0xffffffff;
3e73aa7c
JH
2324#endif
2325} /* fits_in_signed_long() */
2a962e6d 2326
3e73aa7c 2327static INLINE int
65879393 2328fits_in_unsigned_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2329{
2330#ifndef BFD64
2331 return 1;
2332#else
65879393 2333 return num <= 0xffffffff;
3e73aa7c
JH
2334#endif
2335} /* fits_in_unsigned_long() */
252b5132 2336
a442cac5
JB
2337static INLINE valueT extend_to_32bit_address (addressT num)
2338{
2339#ifdef BFD64
2340 if (fits_in_unsigned_long(num))
2341 return (num ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
2342
2343 if (!fits_in_signed_long (num))
2344 return num & 0xffffffff;
2345#endif
2346
2347 return num;
2348}
2349
43234a1e 2350static INLINE int
b5014f7a 2351fits_in_disp8 (offsetT num)
43234a1e
L
2352{
2353 int shift = i.memshift;
2354 unsigned int mask;
2355
2356 if (shift == -1)
2357 abort ();
2358
2359 mask = (1 << shift) - 1;
2360
2361 /* Return 0 if NUM isn't properly aligned. */
2362 if ((num & mask))
2363 return 0;
2364
2365 /* Check if NUM will fit in 8bit after shift. */
2366 return fits_in_signed_byte (num >> shift);
2367}
2368
a683cc34
SP
2369static INLINE int
2370fits_in_imm4 (offsetT num)
2371{
0ff3b7d0
JB
2372 /* Despite the name, check for imm3 if we're dealing with EVEX. */
2373 return (num & (i.vec_encoding != vex_encoding_evex ? 0xf : 7)) == num;
a683cc34
SP
2374}
2375
40fb9820 2376static i386_operand_type
e3bb37b5 2377smallest_imm_type (offsetT num)
252b5132 2378{
40fb9820 2379 i386_operand_type t;
7ab9ffdd 2380
0dfbf9d7 2381 operand_type_set (&t, 0);
40fb9820
L
2382 t.bitfield.imm64 = 1;
2383
2384 if (cpu_arch_tune != PROCESSOR_I486 && num == 1)
e413e4e9
AM
2385 {
2386 /* This code is disabled on the 486 because all the Imm1 forms
2387 in the opcode table are slower on the i486. They're the
2388 versions with the implicitly specified single-position
2389 displacement, which has another syntax if you really want to
2390 use that form. */
40fb9820
L
2391 t.bitfield.imm1 = 1;
2392 t.bitfield.imm8 = 1;
2393 t.bitfield.imm8s = 1;
2394 t.bitfield.imm16 = 1;
2395 t.bitfield.imm32 = 1;
2396 t.bitfield.imm32s = 1;
2397 }
2398 else if (fits_in_signed_byte (num))
2399 {
c34d1cc9
JB
2400 if (fits_in_unsigned_byte (num))
2401 t.bitfield.imm8 = 1;
40fb9820
L
2402 t.bitfield.imm8s = 1;
2403 t.bitfield.imm16 = 1;
2404 t.bitfield.imm32 = 1;
2405 t.bitfield.imm32s = 1;
2406 }
2407 else if (fits_in_unsigned_byte (num))
2408 {
2409 t.bitfield.imm8 = 1;
2410 t.bitfield.imm16 = 1;
2411 t.bitfield.imm32 = 1;
2412 t.bitfield.imm32s = 1;
2413 }
2414 else if (fits_in_signed_word (num) || fits_in_unsigned_word (num))
2415 {
2416 t.bitfield.imm16 = 1;
2417 t.bitfield.imm32 = 1;
2418 t.bitfield.imm32s = 1;
2419 }
2420 else if (fits_in_signed_long (num))
2421 {
2422 t.bitfield.imm32 = 1;
2423 t.bitfield.imm32s = 1;
2424 }
2425 else if (fits_in_unsigned_long (num))
2426 t.bitfield.imm32 = 1;
2427
2428 return t;
47926f60 2429}
252b5132 2430
847f7ad4 2431static offsetT
e3bb37b5 2432offset_in_range (offsetT val, int size)
847f7ad4 2433{
508866be 2434 addressT mask;
ba2adb93 2435
847f7ad4
AM
2436 switch (size)
2437 {
508866be
L
2438 case 1: mask = ((addressT) 1 << 8) - 1; break;
2439 case 2: mask = ((addressT) 1 << 16) - 1; break;
3e73aa7c 2440#ifdef BFD64
64965897 2441 case 4: mask = ((addressT) 1 << 32) - 1; break;
3e73aa7c 2442#endif
64965897 2443 case sizeof (val): return val;
47926f60 2444 default: abort ();
847f7ad4
AM
2445 }
2446
4fe51f7d 2447 if ((val & ~mask) != 0 && (-val & ~mask) != 0)
f493c217
AM
2448 as_warn (_("0x%" PRIx64 " shortened to 0x%" PRIx64),
2449 (uint64_t) val, (uint64_t) (val & mask));
847f7ad4 2450
847f7ad4
AM
2451 return val & mask;
2452}
2453
76d3f746
JB
2454static INLINE const char *insn_name (const insn_template *t)
2455{
5c139202 2456 return &i386_mnemonics[t->mnem_off];
76d3f746
JB
2457}
2458
c32fa91d
L
2459enum PREFIX_GROUP
2460{
2461 PREFIX_EXIST = 0,
2462 PREFIX_LOCK,
2463 PREFIX_REP,
04ef582a 2464 PREFIX_DS,
c32fa91d
L
2465 PREFIX_OTHER
2466};
2467
2468/* Returns
2469 a. PREFIX_EXIST if attempting to add a prefix where one from the
2470 same class already exists.
2471 b. PREFIX_LOCK if lock prefix is added.
2472 c. PREFIX_REP if rep/repne prefix is added.
04ef582a
L
2473 d. PREFIX_DS if ds prefix is added.
2474 e. PREFIX_OTHER if other prefix is added.
c32fa91d
L
2475 */
2476
2477static enum PREFIX_GROUP
e3bb37b5 2478add_prefix (unsigned int prefix)
252b5132 2479{
c32fa91d 2480 enum PREFIX_GROUP ret = PREFIX_OTHER;
b1905489 2481 unsigned int q;
252b5132 2482
29b0f896
AM
2483 if (prefix >= REX_OPCODE && prefix < REX_OPCODE + 16
2484 && flag_code == CODE_64BIT)
b1905489 2485 {
161a04f6 2486 if ((i.prefix[REX_PREFIX] & prefix & REX_W)
44846f29
JB
2487 || (i.prefix[REX_PREFIX] & prefix & REX_R)
2488 || (i.prefix[REX_PREFIX] & prefix & REX_X)
2489 || (i.prefix[REX_PREFIX] & prefix & REX_B))
c32fa91d 2490 ret = PREFIX_EXIST;
b1905489
JB
2491 q = REX_PREFIX;
2492 }
3e73aa7c 2493 else
b1905489
JB
2494 {
2495 switch (prefix)
2496 {
2497 default:
2498 abort ();
2499
b1905489 2500 case DS_PREFIX_OPCODE:
04ef582a
L
2501 ret = PREFIX_DS;
2502 /* Fall through. */
2503 case CS_PREFIX_OPCODE:
b1905489
JB
2504 case ES_PREFIX_OPCODE:
2505 case FS_PREFIX_OPCODE:
2506 case GS_PREFIX_OPCODE:
2507 case SS_PREFIX_OPCODE:
2508 q = SEG_PREFIX;
2509 break;
2510
2511 case REPNE_PREFIX_OPCODE:
2512 case REPE_PREFIX_OPCODE:
c32fa91d
L
2513 q = REP_PREFIX;
2514 ret = PREFIX_REP;
2515 break;
2516
b1905489 2517 case LOCK_PREFIX_OPCODE:
c32fa91d
L
2518 q = LOCK_PREFIX;
2519 ret = PREFIX_LOCK;
b1905489
JB
2520 break;
2521
2522 case FWAIT_OPCODE:
2523 q = WAIT_PREFIX;
2524 break;
2525
2526 case ADDR_PREFIX_OPCODE:
2527 q = ADDR_PREFIX;
2528 break;
2529
2530 case DATA_PREFIX_OPCODE:
2531 q = DATA_PREFIX;
2532 break;
2533 }
2534 if (i.prefix[q] != 0)
c32fa91d 2535 ret = PREFIX_EXIST;
b1905489 2536 }
252b5132 2537
b1905489 2538 if (ret)
252b5132 2539 {
b1905489
JB
2540 if (!i.prefix[q])
2541 ++i.prefixes;
2542 i.prefix[q] |= prefix;
252b5132 2543 }
b1905489
JB
2544 else
2545 as_bad (_("same type of prefix used twice"));
252b5132 2546
252b5132
RH
2547 return ret;
2548}
2549
2550static void
78f12dd3 2551update_code_flag (int value, int check)
eecb386c 2552{
78f12dd3
L
2553 PRINTF_LIKE ((*as_error));
2554
1e9cc1c2 2555 flag_code = (enum flag_code) value;
40fb9820
L
2556 if (flag_code == CODE_64BIT)
2557 {
2558 cpu_arch_flags.bitfield.cpu64 = 1;
2559 cpu_arch_flags.bitfield.cpuno64 = 0;
40fb9820
L
2560 }
2561 else
2562 {
2563 cpu_arch_flags.bitfield.cpu64 = 0;
2564 cpu_arch_flags.bitfield.cpuno64 = 1;
40fb9820
L
2565 }
2566 if (value == CODE_64BIT && !cpu_arch_flags.bitfield.cpulm )
3e73aa7c 2567 {
78f12dd3
L
2568 if (check)
2569 as_error = as_fatal;
2570 else
2571 as_error = as_bad;
2572 (*as_error) (_("64bit mode not supported on `%s'."),
2573 cpu_arch_name ? cpu_arch_name : default_arch);
3e73aa7c 2574 }
40fb9820 2575 if (value == CODE_32BIT && !cpu_arch_flags.bitfield.cpui386)
3e73aa7c 2576 {
78f12dd3
L
2577 if (check)
2578 as_error = as_fatal;
2579 else
2580 as_error = as_bad;
2581 (*as_error) (_("32bit mode not supported on `%s'."),
2582 cpu_arch_name ? cpu_arch_name : default_arch);
3e73aa7c 2583 }
eecb386c
AM
2584 stackop_size = '\0';
2585}
2586
78f12dd3
L
2587static void
2588set_code_flag (int value)
2589{
2590 update_code_flag (value, 0);
2591}
2592
eecb386c 2593static void
e3bb37b5 2594set_16bit_gcc_code_flag (int new_code_flag)
252b5132 2595{
1e9cc1c2 2596 flag_code = (enum flag_code) new_code_flag;
40fb9820
L
2597 if (flag_code != CODE_16BIT)
2598 abort ();
2599 cpu_arch_flags.bitfield.cpu64 = 0;
2600 cpu_arch_flags.bitfield.cpuno64 = 1;
9306ca4a 2601 stackop_size = LONG_MNEM_SUFFIX;
252b5132
RH
2602}
2603
2604static void
e3bb37b5 2605set_intel_syntax (int syntax_flag)
252b5132
RH
2606{
2607 /* Find out if register prefixing is specified. */
2608 int ask_naked_reg = 0;
2609
2610 SKIP_WHITESPACE ();
29b0f896 2611 if (!is_end_of_line[(unsigned char) *input_line_pointer])
252b5132 2612 {
d02603dc
NC
2613 char *string;
2614 int e = get_symbol_name (&string);
252b5132 2615
47926f60 2616 if (strcmp (string, "prefix") == 0)
252b5132 2617 ask_naked_reg = 1;
47926f60 2618 else if (strcmp (string, "noprefix") == 0)
252b5132
RH
2619 ask_naked_reg = -1;
2620 else
d0b47220 2621 as_bad (_("bad argument to syntax directive."));
d02603dc 2622 (void) restore_line_pointer (e);
252b5132
RH
2623 }
2624 demand_empty_rest_of_line ();
c3332e24 2625
252b5132
RH
2626 intel_syntax = syntax_flag;
2627
2628 if (ask_naked_reg == 0)
f86103b7
AM
2629 allow_naked_reg = (intel_syntax
2630 && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
252b5132
RH
2631 else
2632 allow_naked_reg = (ask_naked_reg < 0);
9306ca4a 2633
ee86248c 2634 expr_set_rank (O_full_ptr, syntax_flag ? 10 : 0);
7ab9ffdd 2635
e4a3b5a4 2636 register_prefix = allow_naked_reg ? "" : "%";
252b5132
RH
2637}
2638
1efbbeb4
L
2639static void
2640set_intel_mnemonic (int mnemonic_flag)
2641{
e1d4d893 2642 intel_mnemonic = mnemonic_flag;
1efbbeb4
L
2643}
2644
db51cc60
L
2645static void
2646set_allow_index_reg (int flag)
2647{
2648 allow_index_reg = flag;
2649}
2650
cb19c032 2651static void
7bab8ab5 2652set_check (int what)
cb19c032 2653{
7bab8ab5
JB
2654 enum check_kind *kind;
2655 const char *str;
2656
2657 if (what)
2658 {
2659 kind = &operand_check;
2660 str = "operand";
2661 }
2662 else
2663 {
2664 kind = &sse_check;
2665 str = "sse";
2666 }
2667
cb19c032
L
2668 SKIP_WHITESPACE ();
2669
2670 if (!is_end_of_line[(unsigned char) *input_line_pointer])
2671 {
d02603dc
NC
2672 char *string;
2673 int e = get_symbol_name (&string);
cb19c032
L
2674
2675 if (strcmp (string, "none") == 0)
7bab8ab5 2676 *kind = check_none;
cb19c032 2677 else if (strcmp (string, "warning") == 0)
7bab8ab5 2678 *kind = check_warning;
cb19c032 2679 else if (strcmp (string, "error") == 0)
7bab8ab5 2680 *kind = check_error;
cb19c032 2681 else
7bab8ab5 2682 as_bad (_("bad argument to %s_check directive."), str);
d02603dc 2683 (void) restore_line_pointer (e);
cb19c032
L
2684 }
2685 else
7bab8ab5 2686 as_bad (_("missing argument for %s_check directive"), str);
cb19c032
L
2687
2688 demand_empty_rest_of_line ();
2689}
2690
8a9036a4
L
2691static void
2692check_cpu_arch_compatible (const char *name ATTRIBUTE_UNUSED,
1e9cc1c2 2693 i386_cpu_flags new_flag ATTRIBUTE_UNUSED)
8a9036a4
L
2694{
2695#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2696 static const char *arch;
2697
c085ab00 2698 /* Intel MCU is only supported on ELF. */
8a9036a4
L
2699 if (!IS_ELF)
2700 return;
2701
2702 if (!arch)
2703 {
2704 /* Use cpu_arch_name if it is set in md_parse_option. Otherwise
2705 use default_arch. */
2706 arch = cpu_arch_name;
2707 if (!arch)
2708 arch = default_arch;
2709 }
2710
81486035 2711 /* If we are targeting Intel MCU, we must enable it. */
648d04db
JB
2712 if ((get_elf_backend_data (stdoutput)->elf_machine_code == EM_IAMCU)
2713 == new_flag.bitfield.cpuiamcu)
81486035
L
2714 return;
2715
8a9036a4
L
2716 as_bad (_("`%s' is not supported on `%s'"), name, arch);
2717#endif
2718}
2719
8180707f
JB
2720static void
2721extend_cpu_sub_arch_name (const char *name)
2722{
2723 if (cpu_sub_arch_name)
2724 cpu_sub_arch_name = reconcat (cpu_sub_arch_name, cpu_sub_arch_name,
ae89daec 2725 ".", name, (const char *) NULL);
8180707f 2726 else
ae89daec 2727 cpu_sub_arch_name = concat (".", name, (const char *) NULL);
8180707f
JB
2728}
2729
e413e4e9 2730static void
e3bb37b5 2731set_cpu_arch (int dummy ATTRIBUTE_UNUSED)
e413e4e9 2732{
f68697e8
JB
2733 typedef struct arch_stack_entry
2734 {
2735 const struct arch_stack_entry *prev;
2736 const char *name;
2737 char *sub_name;
2738 i386_cpu_flags flags;
2739 i386_cpu_flags isa_flags;
2740 enum processor_type isa;
2741 enum flag_code flag_code;
2742 char stackop_size;
2743 bool no_cond_jump_promotion;
2744 } arch_stack_entry;
2745 static const arch_stack_entry *arch_stack_top;
2746
47926f60 2747 SKIP_WHITESPACE ();
e413e4e9 2748
29b0f896 2749 if (!is_end_of_line[(unsigned char) *input_line_pointer])
e413e4e9 2750 {
3ce2ebcf
JB
2751 char *s;
2752 int e = get_symbol_name (&s);
2753 const char *string = s;
2754 unsigned int j = 0;
40fb9820 2755 i386_cpu_flags flags;
e413e4e9 2756
3ce2ebcf
JB
2757 if (strcmp (string, "default") == 0)
2758 {
2759 if (strcmp (default_arch, "iamcu") == 0)
2760 string = default_arch;
2761 else
2762 {
2763 static const i386_cpu_flags cpu_unknown_flags = CPU_UNKNOWN_FLAGS;
2764
2765 cpu_arch_name = NULL;
2766 free (cpu_sub_arch_name);
2767 cpu_sub_arch_name = NULL;
2768 cpu_arch_flags = cpu_unknown_flags;
2769 if (flag_code == CODE_64BIT)
2770 {
2771 cpu_arch_flags.bitfield.cpu64 = 1;
2772 cpu_arch_flags.bitfield.cpuno64 = 0;
2773 }
2774 else
2775 {
2776 cpu_arch_flags.bitfield.cpu64 = 0;
2777 cpu_arch_flags.bitfield.cpuno64 = 1;
2778 }
2779 cpu_arch_isa = PROCESSOR_UNKNOWN;
ae89daec 2780 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
3ce2ebcf
JB
2781 if (!cpu_arch_tune_set)
2782 {
2783 cpu_arch_tune = cpu_arch_isa;
2784 cpu_arch_tune_flags = cpu_arch_isa_flags;
2785 }
2786
2787 j = ARRAY_SIZE (cpu_arch) + 1;
2788 }
2789 }
f68697e8
JB
2790 else if (strcmp (string, "push") == 0)
2791 {
2792 arch_stack_entry *top = XNEW (arch_stack_entry);
2793
2794 top->name = cpu_arch_name;
2795 if (cpu_sub_arch_name)
2796 top->sub_name = xstrdup (cpu_sub_arch_name);
2797 else
2798 top->sub_name = NULL;
2799 top->flags = cpu_arch_flags;
2800 top->isa = cpu_arch_isa;
2801 top->isa_flags = cpu_arch_isa_flags;
2802 top->flag_code = flag_code;
2803 top->stackop_size = stackop_size;
2804 top->no_cond_jump_promotion = no_cond_jump_promotion;
2805
2806 top->prev = arch_stack_top;
2807 arch_stack_top = top;
2808
2809 (void) restore_line_pointer (e);
2810 demand_empty_rest_of_line ();
2811 return;
2812 }
2813 else if (strcmp (string, "pop") == 0)
2814 {
2815 const arch_stack_entry *top = arch_stack_top;
2816
2817 if (!top)
2818 as_bad (_(".arch stack is empty"));
2819 else if (top->flag_code != flag_code
2820 || top->stackop_size != stackop_size)
2821 {
2822 static const unsigned int bits[] = {
2823 [CODE_16BIT] = 16,
2824 [CODE_32BIT] = 32,
2825 [CODE_64BIT] = 64,
2826 };
2827
2828 as_bad (_("this `.arch pop' requires `.code%u%s' to be in effect"),
2829 bits[top->flag_code],
2830 top->stackop_size == LONG_MNEM_SUFFIX ? "gcc" : "");
2831 }
2832 else
2833 {
2834 arch_stack_top = top->prev;
2835
2836 cpu_arch_name = top->name;
2837 free (cpu_sub_arch_name);
2838 cpu_sub_arch_name = top->sub_name;
2839 cpu_arch_flags = top->flags;
2840 cpu_arch_isa = top->isa;
2841 cpu_arch_isa_flags = top->isa_flags;
2842 no_cond_jump_promotion = top->no_cond_jump_promotion;
2843
2844 XDELETE (top);
2845 }
2846
2847 (void) restore_line_pointer (e);
2848 demand_empty_rest_of_line ();
2849 return;
2850 }
3ce2ebcf
JB
2851
2852 for (; j < ARRAY_SIZE (cpu_arch); j++)
e413e4e9 2853 {
ae89daec
JB
2854 if (strcmp (string + (*string == '.'), cpu_arch[j].name) == 0
2855 && (*string == '.') == (cpu_arch[j].type == PROCESSOR_NONE))
e413e4e9 2856 {
5c6af06e
JB
2857 if (*string != '.')
2858 {
ae89daec 2859 check_cpu_arch_compatible (string, cpu_arch[j].enable);
648d04db 2860
91d6fa6a 2861 cpu_arch_name = cpu_arch[j].name;
d92c7521 2862 free (cpu_sub_arch_name);
5c6af06e 2863 cpu_sub_arch_name = NULL;
ae89daec 2864 cpu_arch_flags = cpu_arch[j].enable;
40fb9820
L
2865 if (flag_code == CODE_64BIT)
2866 {
2867 cpu_arch_flags.bitfield.cpu64 = 1;
2868 cpu_arch_flags.bitfield.cpuno64 = 0;
2869 }
2870 else
2871 {
2872 cpu_arch_flags.bitfield.cpu64 = 0;
2873 cpu_arch_flags.bitfield.cpuno64 = 1;
2874 }
91d6fa6a 2875 cpu_arch_isa = cpu_arch[j].type;
ae89daec 2876 cpu_arch_isa_flags = cpu_arch[j].enable;
ccc9c027
L
2877 if (!cpu_arch_tune_set)
2878 {
2879 cpu_arch_tune = cpu_arch_isa;
2880 cpu_arch_tune_flags = cpu_arch_isa_flags;
2881 }
d59a54c2 2882 pre_386_16bit_warned = false;
5c6af06e
JB
2883 break;
2884 }
40fb9820 2885
ae89daec
JB
2886 if (cpu_flags_all_zero (&cpu_arch[j].enable))
2887 continue;
2888
293f5f65 2889 flags = cpu_flags_or (cpu_arch_flags,
ae89daec 2890 cpu_arch[j].enable);
81486035 2891
5b64d091 2892 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
5c6af06e 2893 {
ae89daec 2894 extend_cpu_sub_arch_name (string + 1);
40fb9820 2895 cpu_arch_flags = flags;
a586129e 2896 cpu_arch_isa_flags = flags;
5c6af06e 2897 }
0089dace
L
2898 else
2899 cpu_arch_isa_flags
2900 = cpu_flags_or (cpu_arch_isa_flags,
ae89daec 2901 cpu_arch[j].enable);
d02603dc 2902 (void) restore_line_pointer (e);
5c6af06e
JB
2903 demand_empty_rest_of_line ();
2904 return;
e413e4e9
AM
2905 }
2906 }
293f5f65 2907
ae89daec 2908 if (startswith (string, ".no") && j >= ARRAY_SIZE (cpu_arch))
293f5f65 2909 {
33eaf5de 2910 /* Disable an ISA extension. */
ae89daec
JB
2911 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
2912 if (cpu_arch[j].type == PROCESSOR_NONE
2913 && strcmp (string + 3, cpu_arch[j].name) == 0)
293f5f65
L
2914 {
2915 flags = cpu_flags_and_not (cpu_arch_flags,
ae89daec 2916 cpu_arch[j].disable);
293f5f65
L
2917 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
2918 {
ae89daec 2919 extend_cpu_sub_arch_name (string + 1);
293f5f65
L
2920 cpu_arch_flags = flags;
2921 cpu_arch_isa_flags = flags;
2922 }
2923 (void) restore_line_pointer (e);
2924 demand_empty_rest_of_line ();
2925 return;
2926 }
293f5f65
L
2927 }
2928
3ce2ebcf 2929 if (j == ARRAY_SIZE (cpu_arch))
e413e4e9
AM
2930 as_bad (_("no such architecture: `%s'"), string);
2931
2932 *input_line_pointer = e;
2933 }
2934 else
2935 as_bad (_("missing cpu architecture"));
2936
fddf5b5b
AM
2937 no_cond_jump_promotion = 0;
2938 if (*input_line_pointer == ','
29b0f896 2939 && !is_end_of_line[(unsigned char) input_line_pointer[1]])
fddf5b5b 2940 {
d02603dc
NC
2941 char *string;
2942 char e;
2943
2944 ++input_line_pointer;
2945 e = get_symbol_name (&string);
fddf5b5b
AM
2946
2947 if (strcmp (string, "nojumps") == 0)
2948 no_cond_jump_promotion = 1;
2949 else if (strcmp (string, "jumps") == 0)
2950 ;
2951 else
2952 as_bad (_("no such architecture modifier: `%s'"), string);
2953
d02603dc 2954 (void) restore_line_pointer (e);
fddf5b5b
AM
2955 }
2956
e413e4e9
AM
2957 demand_empty_rest_of_line ();
2958}
2959
8a9036a4
L
2960enum bfd_architecture
2961i386_arch (void)
2962{
c085ab00 2963 if (cpu_arch_isa == PROCESSOR_IAMCU)
81486035
L
2964 {
2965 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2966 || flag_code == CODE_64BIT)
2967 as_fatal (_("Intel MCU is 32bit ELF only"));
2968 return bfd_arch_iamcu;
2969 }
8a9036a4
L
2970 else
2971 return bfd_arch_i386;
2972}
2973
b9d79e03 2974unsigned long
7016a5d5 2975i386_mach (void)
b9d79e03 2976{
d34049e8 2977 if (startswith (default_arch, "x86_64"))
8a9036a4 2978 {
c085ab00 2979 if (default_arch[6] == '\0')
8a9036a4 2980 return bfd_mach_x86_64;
351f65ca
L
2981 else
2982 return bfd_mach_x64_32;
8a9036a4 2983 }
5197d474
L
2984 else if (!strcmp (default_arch, "i386")
2985 || !strcmp (default_arch, "iamcu"))
81486035
L
2986 {
2987 if (cpu_arch_isa == PROCESSOR_IAMCU)
2988 {
2989 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
2990 as_fatal (_("Intel MCU is 32bit ELF only"));
2991 return bfd_mach_i386_iamcu;
2992 }
2993 else
2994 return bfd_mach_i386_i386;
2995 }
b9d79e03 2996 else
2b5d6a91 2997 as_fatal (_("unknown architecture"));
b9d79e03 2998}
b9d79e03 2999\f
99f0fb12
JB
3000#include "opcodes/i386-tbl.h"
3001
252b5132 3002void
7016a5d5 3003md_begin (void)
252b5132 3004{
86fa6981
L
3005 /* Support pseudo prefixes like {disp32}. */
3006 lex_type ['{'] = LEX_BEGIN_NAME;
3007
47926f60 3008 /* Initialize op_hash hash table. */
629310ab 3009 op_hash = str_htab_create ();
252b5132
RH
3010
3011 {
65f440c8
JB
3012 const insn_template *const *sets = i386_op_sets;
3013 const insn_template *const *end = sets + ARRAY_SIZE (i386_op_sets) - 1;
3014
3015 /* Type checks to compensate for the conversion through void * which
3016 occurs during hash table insertion / lookup. */
eb993861
JB
3017 (void) sizeof (sets == &current_templates->start);
3018 (void) sizeof (end == &current_templates->end);
65f440c8 3019 for (; sets < end; ++sets)
76d3f746
JB
3020 if (str_hash_insert (op_hash, insn_name (*sets), sets, 0))
3021 as_fatal (_("duplicate %s"), insn_name (*sets));
252b5132
RH
3022 }
3023
47926f60 3024 /* Initialize reg_hash hash table. */
629310ab 3025 reg_hash = str_htab_create ();
252b5132 3026 {
29b0f896 3027 const reg_entry *regtab;
c3fe08fa 3028 unsigned int regtab_size = i386_regtab_size;
252b5132 3029
c3fe08fa 3030 for (regtab = i386_regtab; regtab_size--; regtab++)
6225c532 3031 {
6288d05f
JB
3032 switch (regtab->reg_type.bitfield.class)
3033 {
3034 case Reg:
34684862
JB
3035 if (regtab->reg_type.bitfield.dword)
3036 {
3037 if (regtab->reg_type.bitfield.instance == Accum)
3038 reg_eax = regtab;
3039 }
3040 else if (regtab->reg_type.bitfield.tbyte)
6288d05f
JB
3041 {
3042 /* There's no point inserting st(<N>) in the hash table, as
3043 parentheses aren't included in register_chars[] anyway. */
3044 if (regtab->reg_type.bitfield.instance != Accum)
3045 continue;
3046 reg_st0 = regtab;
3047 }
3048 break;
3049
5e042380
JB
3050 case SReg:
3051 switch (regtab->reg_num)
3052 {
3053 case 0: reg_es = regtab; break;
3054 case 2: reg_ss = regtab; break;
3055 case 3: reg_ds = regtab; break;
3056 }
3057 break;
3058
6288d05f
JB
3059 case RegMask:
3060 if (!regtab->reg_num)
3061 reg_k0 = regtab;
3062 break;
3063 }
3064
6225c532
JB
3065 if (str_hash_insert (reg_hash, regtab->reg_name, regtab, 0) != NULL)
3066 as_fatal (_("duplicate %s"), regtab->reg_name);
6225c532 3067 }
252b5132
RH
3068 }
3069
47926f60 3070 /* Fill in lexical tables: mnemonic_chars, operand_chars. */
252b5132 3071 {
29b0f896 3072 int c;
d2b1a14d 3073 const char *p;
252b5132
RH
3074
3075 for (c = 0; c < 256; c++)
3076 {
014fbcda 3077 if (ISDIGIT (c) || ISLOWER (c))
252b5132
RH
3078 {
3079 mnemonic_chars[c] = c;
3080 register_chars[c] = c;
3081 operand_chars[c] = c;
3082 }
3882b010 3083 else if (ISUPPER (c))
252b5132 3084 {
3882b010 3085 mnemonic_chars[c] = TOLOWER (c);
252b5132
RH
3086 register_chars[c] = mnemonic_chars[c];
3087 operand_chars[c] = c;
3088 }
b3983e5f
JB
3089#ifdef SVR4_COMMENT_CHARS
3090 else if (c == '\\' && strchr (i386_comment_chars, '/'))
3091 operand_chars[c] = c;
3092#endif
252b5132 3093
4795cd4a
JB
3094 if (c >= 128)
3095 operand_chars[c] = c;
252b5132
RH
3096 }
3097
c0f3af97 3098 mnemonic_chars['_'] = '_';
791fe849 3099 mnemonic_chars['-'] = '-';
0003779b 3100 mnemonic_chars['.'] = '.';
252b5132 3101
d2b1a14d
JB
3102 for (p = extra_symbol_chars; *p != '\0'; p++)
3103 operand_chars[(unsigned char) *p] = *p;
252b5132
RH
3104 for (p = operand_special_chars; *p != '\0'; p++)
3105 operand_chars[(unsigned char) *p] = *p;
3106 }
3107
a4447b93
RH
3108 if (flag_code == CODE_64BIT)
3109 {
ca19b261
KT
3110#if defined (OBJ_COFF) && defined (TE_PE)
3111 x86_dwarf2_return_column = (OUTPUT_FLAVOR == bfd_target_coff_flavour
3112 ? 32 : 16);
3113#else
a4447b93 3114 x86_dwarf2_return_column = 16;
ca19b261 3115#endif
61ff971f 3116 x86_cie_data_alignment = -8;
b52c4ee4
IB
3117#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3118 x86_sframe_cfa_sp_reg = 7;
3119 x86_sframe_cfa_fp_reg = 6;
3120#endif
a4447b93
RH
3121 }
3122 else
3123 {
3124 x86_dwarf2_return_column = 8;
3125 x86_cie_data_alignment = -4;
3126 }
e379e5f3
L
3127
3128 /* NB: FUSED_JCC_PADDING frag must have sufficient room so that it
3129 can be turned into BRANCH_PREFIX frag. */
3130 if (align_branch_prefix_size > MAX_FUSED_JCC_PADDING_SIZE)
3131 abort ();
252b5132
RH
3132}
3133
3134void
e3bb37b5 3135i386_print_statistics (FILE *file)
252b5132 3136{
629310ab
ML
3137 htab_print_statistics (file, "i386 opcode", op_hash);
3138 htab_print_statistics (file, "i386 register", reg_hash);
252b5132 3139}
654d6f31
AM
3140
3141void
3142i386_md_end (void)
3143{
3144 htab_delete (op_hash);
3145 htab_delete (reg_hash);
3146}
252b5132 3147\f
252b5132
RH
3148#ifdef DEBUG386
3149
ce8a8b2f 3150/* Debugging routines for md_assemble. */
d3ce72d0 3151static void pte (insn_template *);
40fb9820 3152static void pt (i386_operand_type);
e3bb37b5
L
3153static void pe (expressionS *);
3154static void ps (symbolS *);
252b5132
RH
3155
3156static void
2c703856 3157pi (const char *line, i386_insn *x)
252b5132 3158{
09137c09 3159 unsigned int j;
252b5132
RH
3160
3161 fprintf (stdout, "%s: template ", line);
3162 pte (&x->tm);
09f131f2
JH
3163 fprintf (stdout, " address: base %s index %s scale %x\n",
3164 x->base_reg ? x->base_reg->reg_name : "none",
3165 x->index_reg ? x->index_reg->reg_name : "none",
3166 x->log2_scale_factor);
3167 fprintf (stdout, " modrm: mode %x reg %x reg/mem %x\n",
252b5132 3168 x->rm.mode, x->rm.reg, x->rm.regmem);
09f131f2
JH
3169 fprintf (stdout, " sib: base %x index %x scale %x\n",
3170 x->sib.base, x->sib.index, x->sib.scale);
3171 fprintf (stdout, " rex: 64bit %x extX %x extY %x extZ %x\n",
161a04f6
L
3172 (x->rex & REX_W) != 0,
3173 (x->rex & REX_R) != 0,
3174 (x->rex & REX_X) != 0,
3175 (x->rex & REX_B) != 0);
09137c09 3176 for (j = 0; j < x->operands; j++)
252b5132 3177 {
09137c09
SP
3178 fprintf (stdout, " #%d: ", j + 1);
3179 pt (x->types[j]);
252b5132 3180 fprintf (stdout, "\n");
bab6aec1 3181 if (x->types[j].bitfield.class == Reg
3528c362
JB
3182 || x->types[j].bitfield.class == RegMMX
3183 || x->types[j].bitfield.class == RegSIMD
dd6b8a0b 3184 || x->types[j].bitfield.class == RegMask
00cee14f 3185 || x->types[j].bitfield.class == SReg
4a5c67ed
JB
3186 || x->types[j].bitfield.class == RegCR
3187 || x->types[j].bitfield.class == RegDR
dd6b8a0b
JB
3188 || x->types[j].bitfield.class == RegTR
3189 || x->types[j].bitfield.class == RegBND)
09137c09
SP
3190 fprintf (stdout, "%s\n", x->op[j].regs->reg_name);
3191 if (operand_type_check (x->types[j], imm))
3192 pe (x->op[j].imms);
3193 if (operand_type_check (x->types[j], disp))
3194 pe (x->op[j].disps);
252b5132
RH
3195 }
3196}
3197
3198static void
d3ce72d0 3199pte (insn_template *t)
252b5132 3200{
b933fa4b 3201 static const unsigned char opc_pfx[] = { 0, 0x66, 0xf3, 0xf2 };
441f6aca 3202 static const char *const opc_spc[] = {
0cc78721 3203 NULL, "0f", "0f38", "0f3a", NULL, "evexmap5", "evexmap6", NULL,
441f6aca
JB
3204 "XOP08", "XOP09", "XOP0A",
3205 };
09137c09 3206 unsigned int j;
441f6aca 3207
252b5132 3208 fprintf (stdout, " %d operands ", t->operands);
441f6aca
JB
3209 if (opc_pfx[t->opcode_modifier.opcodeprefix])
3210 fprintf (stdout, "pfx %x ", opc_pfx[t->opcode_modifier.opcodeprefix]);
ddb62495
JB
3211 if (opc_spc[t->opcode_space])
3212 fprintf (stdout, "space %s ", opc_spc[t->opcode_space]);
47926f60 3213 fprintf (stdout, "opcode %x ", t->base_opcode);
252b5132
RH
3214 if (t->extension_opcode != None)
3215 fprintf (stdout, "ext %x ", t->extension_opcode);
40fb9820 3216 if (t->opcode_modifier.d)
252b5132 3217 fprintf (stdout, "D");
40fb9820 3218 if (t->opcode_modifier.w)
252b5132
RH
3219 fprintf (stdout, "W");
3220 fprintf (stdout, "\n");
09137c09 3221 for (j = 0; j < t->operands; j++)
252b5132 3222 {
09137c09
SP
3223 fprintf (stdout, " #%d type ", j + 1);
3224 pt (t->operand_types[j]);
252b5132
RH
3225 fprintf (stdout, "\n");
3226 }
3227}
3228
3229static void
e3bb37b5 3230pe (expressionS *e)
252b5132 3231{
24eab124 3232 fprintf (stdout, " operation %d\n", e->X_op);
b8281767
AM
3233 fprintf (stdout, " add_number %" PRId64 " (%" PRIx64 ")\n",
3234 (int64_t) e->X_add_number, (uint64_t) (valueT) e->X_add_number);
252b5132
RH
3235 if (e->X_add_symbol)
3236 {
3237 fprintf (stdout, " add_symbol ");
3238 ps (e->X_add_symbol);
3239 fprintf (stdout, "\n");
3240 }
3241 if (e->X_op_symbol)
3242 {
3243 fprintf (stdout, " op_symbol ");
3244 ps (e->X_op_symbol);
3245 fprintf (stdout, "\n");
3246 }
3247}
3248
3249static void
e3bb37b5 3250ps (symbolS *s)
252b5132
RH
3251{
3252 fprintf (stdout, "%s type %s%s",
3253 S_GET_NAME (s),
3254 S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
3255 segment_name (S_GET_SEGMENT (s)));
3256}
3257
7b81dfbb 3258static struct type_name
252b5132 3259 {
40fb9820
L
3260 i386_operand_type mask;
3261 const char *name;
252b5132 3262 }
7b81dfbb 3263const type_names[] =
252b5132 3264{
05909f23
JB
3265 { { .bitfield = { .class = Reg, .byte = 1 } }, "r8" },
3266 { { .bitfield = { .class = Reg, .word = 1 } }, "r16" },
3267 { { .bitfield = { .class = Reg, .dword = 1 } }, "r32" },
3268 { { .bitfield = { .class = Reg, .qword = 1 } }, "r64" },
3269 { { .bitfield = { .instance = Accum, .byte = 1 } }, "acc8" },
3270 { { .bitfield = { .instance = Accum, .word = 1 } }, "acc16" },
3271 { { .bitfield = { .instance = Accum, .dword = 1 } }, "acc32" },
3272 { { .bitfield = { .instance = Accum, .qword = 1 } }, "acc64" },
3273 { { .bitfield = { .imm8 = 1 } }, "i8" },
3274 { { .bitfield = { .imm8s = 1 } }, "i8s" },
3275 { { .bitfield = { .imm16 = 1 } }, "i16" },
3276 { { .bitfield = { .imm32 = 1 } }, "i32" },
3277 { { .bitfield = { .imm32s = 1 } }, "i32s" },
3278 { { .bitfield = { .imm64 = 1 } }, "i64" },
3279 { { .bitfield = { .imm1 = 1 } }, "i1" },
3280 { { .bitfield = { .baseindex = 1 } }, "BaseIndex" },
3281 { { .bitfield = { .disp8 = 1 } }, "d8" },
3282 { { .bitfield = { .disp16 = 1 } }, "d16" },
3283 { { .bitfield = { .disp32 = 1 } }, "d32" },
3284 { { .bitfield = { .disp64 = 1 } }, "d64" },
3285 { { .bitfield = { .instance = RegD, .word = 1 } }, "InOutPortReg" },
3286 { { .bitfield = { .instance = RegC, .byte = 1 } }, "ShiftCount" },
3287 { { .bitfield = { .class = RegCR } }, "control reg" },
3288 { { .bitfield = { .class = RegTR } }, "test reg" },
3289 { { .bitfield = { .class = RegDR } }, "debug reg" },
3290 { { .bitfield = { .class = Reg, .tbyte = 1 } }, "FReg" },
3291 { { .bitfield = { .instance = Accum, .tbyte = 1 } }, "FAcc" },
3292 { { .bitfield = { .class = SReg } }, "SReg" },
3293 { { .bitfield = { .class = RegMMX } }, "rMMX" },
3294 { { .bitfield = { .class = RegSIMD, .xmmword = 1 } }, "rXMM" },
3295 { { .bitfield = { .class = RegSIMD, .ymmword = 1 } }, "rYMM" },
3296 { { .bitfield = { .class = RegSIMD, .zmmword = 1 } }, "rZMM" },
3297 { { .bitfield = { .class = RegSIMD, .tmmword = 1 } }, "rTMM" },
3298 { { .bitfield = { .class = RegMask } }, "Mask reg" },
252b5132
RH
3299};
3300
3301static void
40fb9820 3302pt (i386_operand_type t)
252b5132 3303{
40fb9820 3304 unsigned int j;
c6fb90c8 3305 i386_operand_type a;
252b5132 3306
40fb9820 3307 for (j = 0; j < ARRAY_SIZE (type_names); j++)
c6fb90c8
L
3308 {
3309 a = operand_type_and (t, type_names[j].mask);
2c703856 3310 if (operand_type_equal (&a, &type_names[j].mask))
c6fb90c8
L
3311 fprintf (stdout, "%s, ", type_names[j].name);
3312 }
252b5132
RH
3313 fflush (stdout);
3314}
3315
3316#endif /* DEBUG386 */
3317\f
252b5132 3318static bfd_reloc_code_real_type
3956db08 3319reloc (unsigned int size,
64e74474
AM
3320 int pcrel,
3321 int sign,
3322 bfd_reloc_code_real_type other)
252b5132 3323{
47926f60 3324 if (other != NO_RELOC)
3956db08 3325 {
91d6fa6a 3326 reloc_howto_type *rel;
3956db08
JB
3327
3328 if (size == 8)
3329 switch (other)
3330 {
64e74474
AM
3331 case BFD_RELOC_X86_64_GOT32:
3332 return BFD_RELOC_X86_64_GOT64;
3333 break;
553d1284
L
3334 case BFD_RELOC_X86_64_GOTPLT64:
3335 return BFD_RELOC_X86_64_GOTPLT64;
3336 break;
64e74474
AM
3337 case BFD_RELOC_X86_64_PLTOFF64:
3338 return BFD_RELOC_X86_64_PLTOFF64;
3339 break;
3340 case BFD_RELOC_X86_64_GOTPC32:
3341 other = BFD_RELOC_X86_64_GOTPC64;
3342 break;
3343 case BFD_RELOC_X86_64_GOTPCREL:
3344 other = BFD_RELOC_X86_64_GOTPCREL64;
3345 break;
3346 case BFD_RELOC_X86_64_TPOFF32:
3347 other = BFD_RELOC_X86_64_TPOFF64;
3348 break;
3349 case BFD_RELOC_X86_64_DTPOFF32:
3350 other = BFD_RELOC_X86_64_DTPOFF64;
3351 break;
3352 default:
3353 break;
3956db08 3354 }
e05278af 3355
8ce3d284 3356#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
3357 if (other == BFD_RELOC_SIZE32)
3358 {
3359 if (size == 8)
1ab668bf 3360 other = BFD_RELOC_SIZE64;
8fd4256d 3361 if (pcrel)
1ab668bf
AM
3362 {
3363 as_bad (_("there are no pc-relative size relocations"));
3364 return NO_RELOC;
3365 }
8fd4256d 3366 }
8ce3d284 3367#endif
8fd4256d 3368
e05278af 3369 /* Sign-checking 4-byte relocations in 16-/32-bit code is pointless. */
f2d8a97c 3370 if (size == 4 && (flag_code != CODE_64BIT || disallow_64bit_reloc))
e05278af
JB
3371 sign = -1;
3372
91d6fa6a
NC
3373 rel = bfd_reloc_type_lookup (stdoutput, other);
3374 if (!rel)
3956db08 3375 as_bad (_("unknown relocation (%u)"), other);
91d6fa6a 3376 else if (size != bfd_get_reloc_size (rel))
3956db08 3377 as_bad (_("%u-byte relocation cannot be applied to %u-byte field"),
91d6fa6a 3378 bfd_get_reloc_size (rel),
3956db08 3379 size);
91d6fa6a 3380 else if (pcrel && !rel->pc_relative)
3956db08 3381 as_bad (_("non-pc-relative relocation for pc-relative field"));
91d6fa6a 3382 else if ((rel->complain_on_overflow == complain_overflow_signed
3956db08 3383 && !sign)
91d6fa6a 3384 || (rel->complain_on_overflow == complain_overflow_unsigned
64e74474 3385 && sign > 0))
3956db08
JB
3386 as_bad (_("relocated field and relocation type differ in signedness"));
3387 else
3388 return other;
3389 return NO_RELOC;
3390 }
252b5132
RH
3391
3392 if (pcrel)
3393 {
3e73aa7c 3394 if (!sign)
3956db08 3395 as_bad (_("there are no unsigned pc-relative relocations"));
252b5132
RH
3396 switch (size)
3397 {
3398 case 1: return BFD_RELOC_8_PCREL;
3399 case 2: return BFD_RELOC_16_PCREL;
d258b828 3400 case 4: return BFD_RELOC_32_PCREL;
d6ab8113 3401 case 8: return BFD_RELOC_64_PCREL;
252b5132 3402 }
3956db08 3403 as_bad (_("cannot do %u byte pc-relative relocation"), size);
252b5132
RH
3404 }
3405 else
3406 {
3956db08 3407 if (sign > 0)
e5cb08ac 3408 switch (size)
3e73aa7c
JH
3409 {
3410 case 4: return BFD_RELOC_X86_64_32S;
3411 }
3412 else
3413 switch (size)
3414 {
3415 case 1: return BFD_RELOC_8;
3416 case 2: return BFD_RELOC_16;
3417 case 4: return BFD_RELOC_32;
3418 case 8: return BFD_RELOC_64;
3419 }
3956db08
JB
3420 as_bad (_("cannot do %s %u byte relocation"),
3421 sign > 0 ? "signed" : "unsigned", size);
252b5132
RH
3422 }
3423
0cc9e1d3 3424 return NO_RELOC;
252b5132
RH
3425}
3426
47926f60
KH
3427/* Here we decide which fixups can be adjusted to make them relative to
3428 the beginning of the section instead of the symbol. Basically we need
3429 to make sure that the dynamic relocations are done correctly, so in
3430 some cases we force the original symbol to be used. */
3431
252b5132 3432int
e3bb37b5 3433tc_i386_fix_adjustable (fixS *fixP ATTRIBUTE_UNUSED)
252b5132 3434{
6d249963 3435#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 3436 if (!IS_ELF)
31312f95
AM
3437 return 1;
3438
a161fe53
AM
3439 /* Don't adjust pc-relative references to merge sections in 64-bit
3440 mode. */
3441 if (use_rela_relocations
3442 && (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0
3443 && fixP->fx_pcrel)
252b5132 3444 return 0;
31312f95 3445
8d01d9a9
AJ
3446 /* The x86_64 GOTPCREL are represented as 32bit PCrel relocations
3447 and changed later by validate_fix. */
3448 if (GOT_symbol && fixP->fx_subsy == GOT_symbol
3449 && fixP->fx_r_type == BFD_RELOC_32_PCREL)
3450 return 0;
3451
8fd4256d
L
3452 /* Adjust_reloc_syms doesn't know about the GOT. Need to keep symbol
3453 for size relocations. */
3454 if (fixP->fx_r_type == BFD_RELOC_SIZE32
3455 || fixP->fx_r_type == BFD_RELOC_SIZE64
3456 || fixP->fx_r_type == BFD_RELOC_386_GOTOFF
252b5132 3457 || fixP->fx_r_type == BFD_RELOC_386_GOT32
02a86693 3458 || fixP->fx_r_type == BFD_RELOC_386_GOT32X
13ae64f3
JJ
3459 || fixP->fx_r_type == BFD_RELOC_386_TLS_GD
3460 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDM
3461 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDO_32
3462 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE_32
37e55690
JJ
3463 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE
3464 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTIE
13ae64f3
JJ
3465 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE_32
3466 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE
67a4f2b7
AO
3467 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTDESC
3468 || fixP->fx_r_type == BFD_RELOC_386_TLS_DESC_CALL
3e73aa7c 3469 || fixP->fx_r_type == BFD_RELOC_X86_64_GOT32
80b3ee89 3470 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCREL
56ceb5b5
L
3471 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCRELX
3472 || fixP->fx_r_type == BFD_RELOC_X86_64_REX_GOTPCRELX
bffbf940
JJ
3473 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSGD
3474 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSLD
3475 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF32
d6ab8113 3476 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF64
bffbf940
JJ
3477 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF
3478 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF32
d6ab8113
JB
3479 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF64
3480 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTOFF64
67a4f2b7
AO
3481 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
3482 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSDESC_CALL
252b5132
RH
3483 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3484 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3485 return 0;
31312f95 3486#endif
252b5132
RH
3487 return 1;
3488}
252b5132 3489
a9aabc23
JB
3490static INLINE bool
3491want_disp32 (const insn_template *t)
3492{
3493 return flag_code != CODE_64BIT
3494 || i.prefix[ADDR_PREFIX]
7fc69528 3495 || (t->mnem_off == MN_lea
fe134c65
JB
3496 && (!i.types[1].bitfield.qword
3497 || t->opcode_modifier.size == SIZE32));
a9aabc23
JB
3498}
3499
b4cac588 3500static int
e3bb37b5 3501intel_float_operand (const char *mnemonic)
252b5132 3502{
9306ca4a
JB
3503 /* Note that the value returned is meaningful only for opcodes with (memory)
3504 operands, hence the code here is free to improperly handle opcodes that
3505 have no operands (for better performance and smaller code). */
3506
3507 if (mnemonic[0] != 'f')
3508 return 0; /* non-math */
3509
3510 switch (mnemonic[1])
3511 {
3512 /* fclex, fdecstp, fdisi, femms, feni, fincstp, finit, fsetpm, and
3513 the fs segment override prefix not currently handled because no
3514 call path can make opcodes without operands get here */
3515 case 'i':
3516 return 2 /* integer op */;
3517 case 'l':
3518 if (mnemonic[2] == 'd' && (mnemonic[3] == 'c' || mnemonic[3] == 'e'))
3519 return 3; /* fldcw/fldenv */
3520 break;
3521 case 'n':
3522 if (mnemonic[2] != 'o' /* fnop */)
3523 return 3; /* non-waiting control op */
3524 break;
3525 case 'r':
3526 if (mnemonic[2] == 's')
3527 return 3; /* frstor/frstpm */
3528 break;
3529 case 's':
3530 if (mnemonic[2] == 'a')
3531 return 3; /* fsave */
3532 if (mnemonic[2] == 't')
3533 {
3534 switch (mnemonic[3])
3535 {
3536 case 'c': /* fstcw */
3537 case 'd': /* fstdw */
3538 case 'e': /* fstenv */
3539 case 's': /* fsts[gw] */
3540 return 3;
3541 }
3542 }
3543 break;
3544 case 'x':
3545 if (mnemonic[2] == 'r' || mnemonic[2] == 's')
3546 return 0; /* fxsave/fxrstor are not really math ops */
3547 break;
3548 }
252b5132 3549
9306ca4a 3550 return 1;
252b5132
RH
3551}
3552
9a182d04
JB
3553static INLINE void
3554install_template (const insn_template *t)
3555{
3556 unsigned int l;
3557
3558 i.tm = *t;
3559
3560 /* Note that for pseudo prefixes this produces a length of 1. But for them
3561 the length isn't interesting at all. */
3562 for (l = 1; l < 4; ++l)
3563 if (!(t->base_opcode >> (8 * l)))
3564 break;
3565
3566 i.opcode_length = l;
3567}
3568
c0f3af97
L
3569/* Build the VEX prefix. */
3570
3571static void
d3ce72d0 3572build_vex_prefix (const insn_template *t)
c0f3af97
L
3573{
3574 unsigned int register_specifier;
c0f3af97 3575 unsigned int vector_length;
03751133 3576 unsigned int w;
c0f3af97
L
3577
3578 /* Check register specifier. */
3579 if (i.vex.register_specifier)
43234a1e
L
3580 {
3581 register_specifier =
3582 ~register_number (i.vex.register_specifier) & 0xf;
3583 gas_assert ((i.vex.register_specifier->reg_flags & RegVRex) == 0);
3584 }
c0f3af97
L
3585 else
3586 register_specifier = 0xf;
3587
79f0fa25
L
3588 /* Use 2-byte VEX prefix by swapping destination and source operand
3589 if there are more than 1 register operand. */
3590 if (i.reg_operands > 1
3591 && i.vec_encoding != vex_encoding_vex3
86fa6981 3592 && i.dir_encoding == dir_encoding_default
fa99fab2 3593 && i.operands == i.reg_operands
dbbc8b7e 3594 && operand_type_equal (&i.types[0], &i.types[i.operands - 1])
ddb62495 3595 && i.tm.opcode_space == SPACE_0F
dbbc8b7e 3596 && (i.tm.opcode_modifier.load || i.tm.opcode_modifier.d)
fa99fab2
L
3597 && i.rex == REX_B)
3598 {
67748abf 3599 unsigned int xchg;
fa99fab2 3600
67748abf 3601 swap_2_operands (0, i.operands - 1);
fa99fab2 3602
9c2799c2 3603 gas_assert (i.rm.mode == 3);
fa99fab2
L
3604
3605 i.rex = REX_R;
3606 xchg = i.rm.regmem;
3607 i.rm.regmem = i.rm.reg;
3608 i.rm.reg = xchg;
3609
dbbc8b7e
JB
3610 if (i.tm.opcode_modifier.d)
3611 i.tm.base_opcode ^= (i.tm.base_opcode & 0xee) != 0x6e
2c735193 3612 ? Opcode_ExtD : Opcode_SIMD_IntD;
dbbc8b7e 3613 else /* Use the next insn. */
9a182d04 3614 install_template (&t[1]);
fa99fab2
L
3615 }
3616
79dec6b7
JB
3617 /* Use 2-byte VEX prefix by swapping commutative source operands if there
3618 are no memory operands and at least 3 register ones. */
3619 if (i.reg_operands >= 3
3620 && i.vec_encoding != vex_encoding_vex3
3621 && i.reg_operands == i.operands - i.imm_operands
3622 && i.tm.opcode_modifier.vex
3623 && i.tm.opcode_modifier.commutative
33740f21
JB
3624 && (i.tm.opcode_modifier.sse2avx
3625 || (optimize > 1 && !i.no_optimize))
79dec6b7
JB
3626 && i.rex == REX_B
3627 && i.vex.register_specifier
3628 && !(i.vex.register_specifier->reg_flags & RegRex))
3629 {
3630 unsigned int xchg = i.operands - i.reg_operands;
79dec6b7 3631
ddb62495 3632 gas_assert (i.tm.opcode_space == SPACE_0F);
79dec6b7
JB
3633 gas_assert (!i.tm.opcode_modifier.sae);
3634 gas_assert (operand_type_equal (&i.types[i.operands - 2],
3635 &i.types[i.operands - 3]));
3636 gas_assert (i.rm.mode == 3);
3637
67748abf 3638 swap_2_operands (xchg, xchg + 1);
79dec6b7
JB
3639
3640 i.rex = 0;
3641 xchg = i.rm.regmem | 8;
3642 i.rm.regmem = ~register_specifier & 0xf;
3643 gas_assert (!(i.rm.regmem & 8));
3644 i.vex.register_specifier += xchg - i.rm.regmem;
3645 register_specifier = ~xchg & 0xf;
3646 }
3647
539f890d
L
3648 if (i.tm.opcode_modifier.vex == VEXScalar)
3649 vector_length = avxscalar;
10c17abd
JB
3650 else if (i.tm.opcode_modifier.vex == VEX256)
3651 vector_length = 1;
d0c2e3ec
JB
3652 else if (dot_insn () && i.tm.opcode_modifier.vex == VEX128)
3653 vector_length = 0;
539f890d 3654 else
10c17abd 3655 {
56522fc5 3656 unsigned int op;
10c17abd 3657
c7213af9
L
3658 /* Determine vector length from the last multi-length vector
3659 operand. */
10c17abd 3660 vector_length = 0;
56522fc5 3661 for (op = t->operands; op--;)
10c17abd
JB
3662 if (t->operand_types[op].bitfield.xmmword
3663 && t->operand_types[op].bitfield.ymmword
3664 && i.types[op].bitfield.ymmword)
3665 {
3666 vector_length = 1;
3667 break;
3668 }
3669 }
c0f3af97 3670
03751133
L
3671 /* Check the REX.W bit and VEXW. */
3672 if (i.tm.opcode_modifier.vexw == VEXWIG)
3673 w = (vexwig == vexw1 || (i.rex & REX_W)) ? 1 : 0;
3674 else if (i.tm.opcode_modifier.vexw)
3675 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3676 else
931d03b7 3677 w = (flag_code == CODE_64BIT ? i.rex & REX_W : vexwig == vexw1) ? 1 : 0;
03751133 3678
c0f3af97 3679 /* Use 2-byte VEX prefix if possible. */
03751133
L
3680 if (w == 0
3681 && i.vec_encoding != vex_encoding_vex3
ddb62495 3682 && i.tm.opcode_space == SPACE_0F
c0f3af97
L
3683 && (i.rex & (REX_W | REX_X | REX_B)) == 0)
3684 {
3685 /* 2-byte VEX prefix. */
3686 unsigned int r;
3687
3688 i.vex.length = 2;
3689 i.vex.bytes[0] = 0xc5;
3690
3691 /* Check the REX.R bit. */
3692 r = (i.rex & REX_R) ? 0 : 1;
3693 i.vex.bytes[1] = (r << 7
3694 | register_specifier << 3
3695 | vector_length << 2
35648716 3696 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3697 }
3698 else
3699 {
3700 /* 3-byte VEX prefix. */
f88c9eb0 3701 i.vex.length = 3;
f88c9eb0 3702
ddb62495 3703 switch (i.tm.opcode_space)
5dd85c99 3704 {
441f6aca
JB
3705 case SPACE_0F:
3706 case SPACE_0F38:
3707 case SPACE_0F3A:
80de6e00 3708 i.vex.bytes[0] = 0xc4;
7f399153 3709 break;
441f6aca
JB
3710 case SPACE_XOP08:
3711 case SPACE_XOP09:
3712 case SPACE_XOP0A:
f88c9eb0 3713 i.vex.bytes[0] = 0x8f;
7f399153
L
3714 break;
3715 default:
3716 abort ();
f88c9eb0 3717 }
c0f3af97 3718
c0f3af97
L
3719 /* The high 3 bits of the second VEX byte are 1's compliment
3720 of RXB bits from REX. */
d0c2e3ec
JB
3721 i.vex.bytes[1] = ((~i.rex & 7) << 5)
3722 | (!dot_insn () ? i.tm.opcode_space
3723 : i.insn_opcode_space);
c0f3af97 3724
c0f3af97
L
3725 i.vex.bytes[2] = (w << 7
3726 | register_specifier << 3
3727 | vector_length << 2
35648716 3728 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3729 }
3730}
3731
5b7c81bd 3732static INLINE bool
e771e7c9
JB
3733is_evex_encoding (const insn_template *t)
3734{
7091c612 3735 return t->opcode_modifier.evex || t->opcode_modifier.disp8memshift
e771e7c9 3736 || t->opcode_modifier.broadcast || t->opcode_modifier.masking
a80195f1 3737 || t->opcode_modifier.sae;
e771e7c9
JB
3738}
3739
5b7c81bd 3740static INLINE bool
7a8655d2
JB
3741is_any_vex_encoding (const insn_template *t)
3742{
7b47a312 3743 return t->opcode_modifier.vex || is_evex_encoding (t);
7a8655d2
JB
3744}
3745
a5748e0d
JB
3746static unsigned int
3747get_broadcast_bytes (const insn_template *t, bool diag)
3748{
3749 unsigned int op, bytes;
3750 const i386_operand_type *types;
3751
3752 if (i.broadcast.type)
9b345ce8 3753 return (1 << (t->opcode_modifier.broadcast - 1)) * i.broadcast.type;
a5748e0d
JB
3754
3755 gas_assert (intel_syntax);
3756
3757 for (op = 0; op < t->operands; ++op)
3758 if (t->operand_types[op].bitfield.baseindex)
3759 break;
3760
3761 gas_assert (op < t->operands);
3762
3763 if (t->opcode_modifier.evex
3764 && t->opcode_modifier.evex != EVEXDYN)
3765 switch (i.broadcast.bytes)
3766 {
3767 case 1:
3768 if (t->operand_types[op].bitfield.word)
3769 return 2;
3770 /* Fall through. */
3771 case 2:
3772 if (t->operand_types[op].bitfield.dword)
3773 return 4;
3774 /* Fall through. */
3775 case 4:
3776 if (t->operand_types[op].bitfield.qword)
3777 return 8;
3778 /* Fall through. */
3779 case 8:
3780 if (t->operand_types[op].bitfield.xmmword)
3781 return 16;
3782 if (t->operand_types[op].bitfield.ymmword)
3783 return 32;
3784 if (t->operand_types[op].bitfield.zmmword)
3785 return 64;
3786 /* Fall through. */
3787 default:
3788 abort ();
3789 }
3790
3791 gas_assert (op + 1 < t->operands);
3792
3793 if (t->operand_types[op + 1].bitfield.xmmword
3794 + t->operand_types[op + 1].bitfield.ymmword
3795 + t->operand_types[op + 1].bitfield.zmmword > 1)
3796 {
3797 types = &i.types[op + 1];
3798 diag = false;
3799 }
3800 else /* Ambiguous - guess with a preference to non-AVX512VL forms. */
3801 types = &t->operand_types[op];
3802
3803 if (types->bitfield.zmmword)
3804 bytes = 64;
3805 else if (types->bitfield.ymmword)
3806 bytes = 32;
3807 else
3808 bytes = 16;
3809
3810 if (diag)
3811 as_warn (_("ambiguous broadcast for `%s', using %u-bit form"),
76d3f746 3812 insn_name (t), bytes * 8);
a5748e0d
JB
3813
3814 return bytes;
3815}
3816
43234a1e
L
3817/* Build the EVEX prefix. */
3818
3819static void
3820build_evex_prefix (void)
3821{
35648716 3822 unsigned int register_specifier, w;
43234a1e
L
3823 rex_byte vrex_used = 0;
3824
3825 /* Check register specifier. */
3826 if (i.vex.register_specifier)
3827 {
3828 gas_assert ((i.vrex & REX_X) == 0);
3829
3830 register_specifier = i.vex.register_specifier->reg_num;
3831 if ((i.vex.register_specifier->reg_flags & RegRex))
3832 register_specifier += 8;
3833 /* The upper 16 registers are encoded in the fourth byte of the
3834 EVEX prefix. */
3835 if (!(i.vex.register_specifier->reg_flags & RegVRex))
3836 i.vex.bytes[3] = 0x8;
3837 register_specifier = ~register_specifier & 0xf;
3838 }
3839 else
3840 {
3841 register_specifier = 0xf;
3842
3843 /* Encode upper 16 vector index register in the fourth byte of
3844 the EVEX prefix. */
3845 if (!(i.vrex & REX_X))
3846 i.vex.bytes[3] = 0x8;
3847 else
3848 vrex_used |= REX_X;
3849 }
3850
43234a1e
L
3851 /* 4 byte EVEX prefix. */
3852 i.vex.length = 4;
3853 i.vex.bytes[0] = 0x62;
3854
43234a1e
L
3855 /* The high 3 bits of the second EVEX byte are 1's compliment of RXB
3856 bits from REX. */
ddb62495
JB
3857 gas_assert (i.tm.opcode_space >= SPACE_0F);
3858 gas_assert (i.tm.opcode_space <= SPACE_EVEXMAP6);
d0c2e3ec
JB
3859 i.vex.bytes[1] = ((~i.rex & 7) << 5)
3860 | (!dot_insn () ? i.tm.opcode_space
3861 : i.insn_opcode_space);
43234a1e
L
3862
3863 /* The fifth bit of the second EVEX byte is 1's compliment of the
3864 REX_R bit in VREX. */
3865 if (!(i.vrex & REX_R))
3866 i.vex.bytes[1] |= 0x10;
3867 else
3868 vrex_used |= REX_R;
3869
3870 if ((i.reg_operands + i.imm_operands) == i.operands)
3871 {
3872 /* When all operands are registers, the REX_X bit in REX is not
3873 used. We reuse it to encode the upper 16 registers, which is
3874 indicated by the REX_B bit in VREX. The REX_X bit is encoded
3875 as 1's compliment. */
3876 if ((i.vrex & REX_B))
3877 {
3878 vrex_used |= REX_B;
3879 i.vex.bytes[1] &= ~0x40;
3880 }
3881 }
3882
3883 /* EVEX instructions shouldn't need the REX prefix. */
3884 i.vrex &= ~vrex_used;
3885 gas_assert (i.vrex == 0);
3886
6865c043
L
3887 /* Check the REX.W bit and VEXW. */
3888 if (i.tm.opcode_modifier.vexw == VEXWIG)
3889 w = (evexwig == evexw1 || (i.rex & REX_W)) ? 1 : 0;
3890 else if (i.tm.opcode_modifier.vexw)
3891 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3892 else
931d03b7 3893 w = (flag_code == CODE_64BIT ? i.rex & REX_W : evexwig == evexw1) ? 1 : 0;
43234a1e 3894
43234a1e 3895 /* The third byte of the EVEX prefix. */
35648716
JB
3896 i.vex.bytes[2] = ((w << 7)
3897 | (register_specifier << 3)
3898 | 4 /* Encode the U bit. */
3899 | i.tm.opcode_modifier.opcodeprefix);
43234a1e
L
3900
3901 /* The fourth byte of the EVEX prefix. */
3902 /* The zeroing-masking bit. */
6225c532 3903 if (i.mask.reg && i.mask.zeroing)
43234a1e
L
3904 i.vex.bytes[3] |= 0x80;
3905
3906 /* Don't always set the broadcast bit if there is no RC. */
ca5312a2 3907 if (i.rounding.type == rc_none)
43234a1e
L
3908 {
3909 /* Encode the vector length. */
3910 unsigned int vec_length;
3911
e771e7c9
JB
3912 if (!i.tm.opcode_modifier.evex
3913 || i.tm.opcode_modifier.evex == EVEXDYN)
3914 {
56522fc5 3915 unsigned int op;
e771e7c9 3916
c7213af9
L
3917 /* Determine vector length from the last multi-length vector
3918 operand. */
56522fc5 3919 for (op = i.operands; op--;)
e771e7c9
JB
3920 if (i.tm.operand_types[op].bitfield.xmmword
3921 + i.tm.operand_types[op].bitfield.ymmword
3922 + i.tm.operand_types[op].bitfield.zmmword > 1)
3923 {
3924 if (i.types[op].bitfield.zmmword)
c7213af9
L
3925 {
3926 i.tm.opcode_modifier.evex = EVEX512;
3927 break;
3928 }
e771e7c9 3929 else if (i.types[op].bitfield.ymmword)
c7213af9
L
3930 {
3931 i.tm.opcode_modifier.evex = EVEX256;
3932 break;
3933 }
e771e7c9 3934 else if (i.types[op].bitfield.xmmword)
c7213af9
L
3935 {
3936 i.tm.opcode_modifier.evex = EVEX128;
3937 break;
3938 }
9b345ce8
JB
3939 else if ((i.broadcast.type || i.broadcast.bytes)
3940 && op == i.broadcast.operand)
625cbd7a 3941 {
a5748e0d 3942 switch (get_broadcast_bytes (&i.tm, true))
625cbd7a
JB
3943 {
3944 case 64:
3945 i.tm.opcode_modifier.evex = EVEX512;
3946 break;
3947 case 32:
3948 i.tm.opcode_modifier.evex = EVEX256;
3949 break;
3950 case 16:
3951 i.tm.opcode_modifier.evex = EVEX128;
3952 break;
3953 default:
c7213af9 3954 abort ();
625cbd7a 3955 }
c7213af9 3956 break;
625cbd7a 3957 }
e771e7c9 3958 }
c7213af9 3959
56522fc5 3960 if (op >= MAX_OPERANDS)
c7213af9 3961 abort ();
e771e7c9
JB
3962 }
3963
43234a1e
L
3964 switch (i.tm.opcode_modifier.evex)
3965 {
3966 case EVEXLIG: /* LL' is ignored */
3967 vec_length = evexlig << 5;
3968 break;
3969 case EVEX128:
3970 vec_length = 0 << 5;
3971 break;
3972 case EVEX256:
3973 vec_length = 1 << 5;
3974 break;
3975 case EVEX512:
3976 vec_length = 2 << 5;
3977 break;
d0c2e3ec
JB
3978 case EVEX_L3:
3979 if (dot_insn ())
3980 {
3981 vec_length = 3 << 5;
3982 break;
3983 }
3984 /* Fall through. */
43234a1e
L
3985 default:
3986 abort ();
3987 break;
3988 }
3989 i.vex.bytes[3] |= vec_length;
3990 /* Encode the broadcast bit. */
9b345ce8 3991 if (i.broadcast.type || i.broadcast.bytes)
43234a1e
L
3992 i.vex.bytes[3] |= 0x10;
3993 }
ca5312a2
JB
3994 else if (i.rounding.type != saeonly)
3995 i.vex.bytes[3] |= 0x10 | (i.rounding.type << 5);
43234a1e 3996 else
ca5312a2 3997 i.vex.bytes[3] |= 0x10 | (evexrcig << 5);
43234a1e 3998
6225c532
JB
3999 if (i.mask.reg)
4000 i.vex.bytes[3] |= i.mask.reg->reg_num;
43234a1e
L
4001}
4002
65da13b5
L
4003static void
4004process_immext (void)
4005{
4006 expressionS *exp;
4007
c0f3af97 4008 /* These AMD 3DNow! and SSE2 instructions have an opcode suffix
65da13b5
L
4009 which is coded in the same place as an 8-bit immediate field
4010 would be. Here we fake an 8-bit immediate operand from the
4011 opcode suffix stored in tm.extension_opcode.
4012
c1e679ec 4013 AVX instructions also use this encoding, for some of
c0f3af97 4014 3 argument instructions. */
65da13b5 4015
43234a1e 4016 gas_assert (i.imm_operands <= 1
7ab9ffdd 4017 && (i.operands <= 2
7a8655d2 4018 || (is_any_vex_encoding (&i.tm)
7ab9ffdd 4019 && i.operands <= 4)));
65da13b5
L
4020
4021 exp = &im_expressions[i.imm_operands++];
4022 i.op[i.operands].imms = exp;
be1643ff 4023 i.types[i.operands].bitfield.imm8 = 1;
65da13b5
L
4024 i.operands++;
4025 exp->X_op = O_constant;
4026 exp->X_add_number = i.tm.extension_opcode;
4027 i.tm.extension_opcode = None;
4028}
4029
42164a71
L
4030
4031static int
4032check_hle (void)
4033{
742732c7 4034 switch (i.tm.opcode_modifier.prefixok)
42164a71
L
4035 {
4036 default:
4037 abort ();
742732c7
JB
4038 case PrefixLock:
4039 case PrefixNone:
4040 case PrefixNoTrack:
4041 case PrefixRep:
165de32a 4042 as_bad (_("invalid instruction `%s' after `%s'"),
76d3f746 4043 insn_name (&i.tm), i.hle_prefix);
42164a71 4044 return 0;
742732c7 4045 case PrefixHLELock:
42164a71
L
4046 if (i.prefix[LOCK_PREFIX])
4047 return 1;
165de32a 4048 as_bad (_("missing `lock' with `%s'"), i.hle_prefix);
42164a71 4049 return 0;
742732c7 4050 case PrefixHLEAny:
42164a71 4051 return 1;
742732c7 4052 case PrefixHLERelease:
42164a71
L
4053 if (i.prefix[HLE_PREFIX] != XRELEASE_PREFIX_OPCODE)
4054 {
4055 as_bad (_("instruction `%s' after `xacquire' not allowed"),
76d3f746 4056 insn_name (&i.tm));
42164a71
L
4057 return 0;
4058 }
8dc0818e 4059 if (i.mem_operands == 0 || !(i.flags[i.operands - 1] & Operand_Mem))
42164a71
L
4060 {
4061 as_bad (_("memory destination needed for instruction `%s'"
76d3f746 4062 " after `xrelease'"), insn_name (&i.tm));
42164a71
L
4063 return 0;
4064 }
4065 return 1;
4066 }
4067}
4068
c8480b58
L
4069/* Encode aligned vector move as unaligned vector move. */
4070
4071static void
4072encode_with_unaligned_vector_move (void)
4073{
4074 switch (i.tm.base_opcode)
4075 {
b3a9fe6f
L
4076 case 0x28: /* Load instructions. */
4077 case 0x29: /* Store instructions. */
c8480b58 4078 /* movaps/movapd/vmovaps/vmovapd. */
ddb62495 4079 if (i.tm.opcode_space == SPACE_0F
c8480b58 4080 && i.tm.opcode_modifier.opcodeprefix <= PREFIX_0X66)
b3a9fe6f 4081 i.tm.base_opcode = 0x10 | (i.tm.base_opcode & 1);
c8480b58 4082 break;
b3a9fe6f
L
4083 case 0x6f: /* Load instructions. */
4084 case 0x7f: /* Store instructions. */
c8480b58 4085 /* movdqa/vmovdqa/vmovdqa64/vmovdqa32. */
ddb62495 4086 if (i.tm.opcode_space == SPACE_0F
c8480b58
L
4087 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0X66)
4088 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
4089 break;
4090 default:
4091 break;
4092 }
4093}
4094
b6f8c7c4
L
4095/* Try the shortest encoding by shortening operand size. */
4096
4097static void
4098optimize_encoding (void)
4099{
a0a1771e 4100 unsigned int j;
b6f8c7c4 4101
7fc69528 4102 if (i.tm.mnem_off == MN_lea)
fe134c65
JB
4103 {
4104 /* Optimize: -O:
4105 lea symbol, %rN -> mov $symbol, %rN
4106 lea (%rM), %rN -> mov %rM, %rN
4107 lea (,%rM,1), %rN -> mov %rM, %rN
4108
4109 and in 32-bit mode for 16-bit addressing
4110
4111 lea (%rM), %rN -> movzx %rM, %rN
4112
4113 and in 64-bit mode zap 32-bit addressing in favor of using a
4114 32-bit (or less) destination.
4115 */
4116 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
4117 {
4118 if (!i.op[1].regs->reg_type.bitfield.word)
4119 i.tm.opcode_modifier.size = SIZE32;
4120 i.prefix[ADDR_PREFIX] = 0;
4121 }
4122
4123 if (!i.index_reg && !i.base_reg)
4124 {
4125 /* Handle:
4126 lea symbol, %rN -> mov $symbol, %rN
4127 */
4128 if (flag_code == CODE_64BIT)
4129 {
4130 /* Don't transform a relocation to a 16-bit one. */
4131 if (i.op[0].disps
4132 && i.op[0].disps->X_op != O_constant
4133 && i.op[1].regs->reg_type.bitfield.word)
4134 return;
4135
4136 if (!i.op[1].regs->reg_type.bitfield.qword
4137 || i.tm.opcode_modifier.size == SIZE32)
4138 {
4139 i.tm.base_opcode = 0xb8;
4140 i.tm.opcode_modifier.modrm = 0;
4141 if (!i.op[1].regs->reg_type.bitfield.word)
4142 i.types[0].bitfield.imm32 = 1;
4143 else
4144 {
4145 i.tm.opcode_modifier.size = SIZE16;
4146 i.types[0].bitfield.imm16 = 1;
4147 }
4148 }
4149 else
4150 {
4151 /* Subject to further optimization below. */
4152 i.tm.base_opcode = 0xc7;
4153 i.tm.extension_opcode = 0;
4154 i.types[0].bitfield.imm32s = 1;
4155 i.types[0].bitfield.baseindex = 0;
4156 }
4157 }
4158 /* Outside of 64-bit mode address and operand sizes have to match if
4159 a relocation is involved, as otherwise we wouldn't (currently) or
4160 even couldn't express the relocation correctly. */
4161 else if (i.op[0].disps
4162 && i.op[0].disps->X_op != O_constant
4163 && ((!i.prefix[ADDR_PREFIX])
4164 != (flag_code == CODE_32BIT
4165 ? i.op[1].regs->reg_type.bitfield.dword
4166 : i.op[1].regs->reg_type.bitfield.word)))
4167 return;
7772f168
JB
4168 /* In 16-bit mode converting LEA with 16-bit addressing and a 32-bit
4169 destination is going to grow encoding size. */
4170 else if (flag_code == CODE_16BIT
4171 && (optimize <= 1 || optimize_for_space)
4172 && !i.prefix[ADDR_PREFIX]
4173 && i.op[1].regs->reg_type.bitfield.dword)
4174 return;
fe134c65
JB
4175 else
4176 {
4177 i.tm.base_opcode = 0xb8;
4178 i.tm.opcode_modifier.modrm = 0;
4179 if (i.op[1].regs->reg_type.bitfield.dword)
4180 i.types[0].bitfield.imm32 = 1;
4181 else
4182 i.types[0].bitfield.imm16 = 1;
4183
4184 if (i.op[0].disps
4185 && i.op[0].disps->X_op == O_constant
4186 && i.op[1].regs->reg_type.bitfield.dword
60cfa10c
L
4187 /* NB: Add () to !i.prefix[ADDR_PREFIX] to silence
4188 GCC 5. */
4189 && (!i.prefix[ADDR_PREFIX]) != (flag_code == CODE_32BIT))
fe134c65
JB
4190 i.op[0].disps->X_add_number &= 0xffff;
4191 }
4192
4193 i.tm.operand_types[0] = i.types[0];
4194 i.imm_operands = 1;
4195 if (!i.op[0].imms)
4196 {
4197 i.op[0].imms = &im_expressions[0];
4198 i.op[0].imms->X_op = O_absent;
4199 }
4200 }
4201 else if (i.op[0].disps
4202 && (i.op[0].disps->X_op != O_constant
4203 || i.op[0].disps->X_add_number))
4204 return;
4205 else
4206 {
4207 /* Handle:
4208 lea (%rM), %rN -> mov %rM, %rN
4209 lea (,%rM,1), %rN -> mov %rM, %rN
4210 lea (%rM), %rN -> movzx %rM, %rN
4211 */
4212 const reg_entry *addr_reg;
4213
4214 if (!i.index_reg && i.base_reg->reg_num != RegIP)
4215 addr_reg = i.base_reg;
4216 else if (!i.base_reg
4217 && i.index_reg->reg_num != RegIZ
4218 && !i.log2_scale_factor)
4219 addr_reg = i.index_reg;
4220 else
4221 return;
4222
4223 if (addr_reg->reg_type.bitfield.word
4224 && i.op[1].regs->reg_type.bitfield.dword)
4225 {
4226 if (flag_code != CODE_32BIT)
4227 return;
ddb62495 4228 i.tm.opcode_space = SPACE_0F;
fe134c65
JB
4229 i.tm.base_opcode = 0xb7;
4230 }
4231 else
4232 i.tm.base_opcode = 0x8b;
4233
4234 if (addr_reg->reg_type.bitfield.dword
4235 && i.op[1].regs->reg_type.bitfield.qword)
4236 i.tm.opcode_modifier.size = SIZE32;
4237
4238 i.op[0].regs = addr_reg;
4239 i.reg_operands = 2;
4240 }
4241
4242 i.mem_operands = 0;
4243 i.disp_operands = 0;
4244 i.prefix[ADDR_PREFIX] = 0;
4245 i.prefix[SEG_PREFIX] = 0;
4246 i.seg[0] = NULL;
4247 }
4248
b6f8c7c4 4249 if (optimize_for_space
7fc69528 4250 && i.tm.mnem_off == MN_test
b6f8c7c4
L
4251 && i.reg_operands == 1
4252 && i.imm_operands == 1
4253 && !i.types[1].bitfield.byte
4254 && i.op[0].imms->X_op == O_constant
7fc69528 4255 && fits_in_imm7 (i.op[0].imms->X_add_number))
b6f8c7c4
L
4256 {
4257 /* Optimize: -Os:
4258 test $imm7, %r64/%r32/%r16 -> test $imm7, %r8
4259 */
4260 unsigned int base_regnum = i.op[1].regs->reg_num;
4261 if (flag_code == CODE_64BIT || base_regnum < 4)
4262 {
4263 i.types[1].bitfield.byte = 1;
4264 /* Ignore the suffix. */
4265 i.suffix = 0;
7697afb6
JB
4266 /* Convert to byte registers. */
4267 if (i.types[1].bitfield.word)
4268 j = 16;
4269 else if (i.types[1].bitfield.dword)
4270 j = 32;
4271 else
4272 j = 48;
4273 if (!(i.op[1].regs->reg_flags & RegRex) && base_regnum < 4)
4274 j += 8;
4275 i.op[1].regs -= j;
b6f8c7c4
L
4276 }
4277 }
4278 else if (flag_code == CODE_64BIT
ddb62495 4279 && i.tm.opcode_space == SPACE_BASE
d3d50934
L
4280 && ((i.types[1].bitfield.qword
4281 && i.reg_operands == 1
b6f8c7c4
L
4282 && i.imm_operands == 1
4283 && i.op[0].imms->X_op == O_constant
507916b8 4284 && ((i.tm.base_opcode == 0xb8
b6f8c7c4
L
4285 && i.tm.extension_opcode == None
4286 && fits_in_unsigned_long (i.op[0].imms->X_add_number))
4287 || (fits_in_imm31 (i.op[0].imms->X_add_number)
7fc69528 4288 && (i.tm.base_opcode == 0x24
b6f8c7c4
L
4289 || (i.tm.base_opcode == 0x80
4290 && i.tm.extension_opcode == 0x4)
7fc69528
JB
4291 || i.tm.mnem_off == MN_test
4292 || ((i.tm.base_opcode | 1) == 0xc7
b8364fa7
JB
4293 && i.tm.extension_opcode == 0x0)))
4294 || (fits_in_imm7 (i.op[0].imms->X_add_number)
4295 && i.tm.base_opcode == 0x83
4296 && i.tm.extension_opcode == 0x4)))
d3d50934
L
4297 || (i.types[0].bitfield.qword
4298 && ((i.reg_operands == 2
4299 && i.op[0].regs == i.op[1].regs
7fc69528
JB
4300 && (i.tm.mnem_off == MN_xor
4301 || i.tm.mnem_off == MN_sub))
4302 || i.tm.mnem_off == MN_clr))))
b6f8c7c4
L
4303 {
4304 /* Optimize: -O:
4305 andq $imm31, %r64 -> andl $imm31, %r32
b8364fa7 4306 andq $imm7, %r64 -> andl $imm7, %r32
b6f8c7c4
L
4307 testq $imm31, %r64 -> testl $imm31, %r32
4308 xorq %r64, %r64 -> xorl %r32, %r32
4309 subq %r64, %r64 -> subl %r32, %r32
4310 movq $imm31, %r64 -> movl $imm31, %r32
4311 movq $imm32, %r64 -> movl $imm32, %r32
4312 */
04784e33
JB
4313 i.tm.opcode_modifier.size = SIZE32;
4314 if (i.imm_operands)
4315 {
4316 i.types[0].bitfield.imm32 = 1;
4317 i.types[0].bitfield.imm32s = 0;
4318 i.types[0].bitfield.imm64 = 0;
4319 }
4320 else
4321 {
4322 i.types[0].bitfield.dword = 1;
4323 i.types[0].bitfield.qword = 0;
4324 }
4325 i.types[1].bitfield.dword = 1;
4326 i.types[1].bitfield.qword = 0;
7fc69528 4327 if (i.tm.mnem_off == MN_mov || i.tm.mnem_off == MN_lea)
b6f8c7c4
L
4328 {
4329 /* Handle
4330 movq $imm31, %r64 -> movl $imm31, %r32
4331 movq $imm32, %r64 -> movl $imm32, %r32
4332 */
4333 i.tm.operand_types[0].bitfield.imm32 = 1;
4334 i.tm.operand_types[0].bitfield.imm32s = 0;
4335 i.tm.operand_types[0].bitfield.imm64 = 0;
507916b8 4336 if ((i.tm.base_opcode | 1) == 0xc7)
b6f8c7c4
L
4337 {
4338 /* Handle
4339 movq $imm31, %r64 -> movl $imm31, %r32
4340 */
507916b8 4341 i.tm.base_opcode = 0xb8;
b6f8c7c4 4342 i.tm.extension_opcode = None;
507916b8 4343 i.tm.opcode_modifier.w = 0;
b6f8c7c4
L
4344 i.tm.opcode_modifier.modrm = 0;
4345 }
4346 }
4347 }
5641ec01
JB
4348 else if (optimize > 1
4349 && !optimize_for_space
4350 && i.reg_operands == 2
4351 && i.op[0].regs == i.op[1].regs
7fc69528 4352 && (i.tm.mnem_off == MN_and || i.tm.mnem_off == MN_or)
5641ec01
JB
4353 && (flag_code != CODE_64BIT || !i.types[0].bitfield.dword))
4354 {
4355 /* Optimize: -O2:
4356 andb %rN, %rN -> testb %rN, %rN
4357 andw %rN, %rN -> testw %rN, %rN
4358 andq %rN, %rN -> testq %rN, %rN
4359 orb %rN, %rN -> testb %rN, %rN
4360 orw %rN, %rN -> testw %rN, %rN
4361 orq %rN, %rN -> testq %rN, %rN
4362
4363 and outside of 64-bit mode
4364
4365 andl %rN, %rN -> testl %rN, %rN
4366 orl %rN, %rN -> testl %rN, %rN
4367 */
4368 i.tm.base_opcode = 0x84 | (i.tm.base_opcode & 1);
4369 }
ad2f4436
JB
4370 else if (i.tm.base_opcode == 0xba
4371 && i.tm.opcode_space == SPACE_0F
4372 && i.reg_operands == 1
4373 && i.op[0].imms->X_op == O_constant
4374 && i.op[0].imms->X_add_number >= 0)
4375 {
4376 /* Optimize: -O:
4377 btw $n, %rN -> btl $n, %rN (outside of 16-bit mode, n < 16)
4378 btq $n, %rN -> btl $n, %rN (in 64-bit mode, n < 32, N < 8)
4379 btl $n, %rN -> btw $n, %rN (in 16-bit mode, n < 16)
4380
4381 With <BT> one of bts, btr, and bts also:
4382 <BT>w $n, %rN -> btl $n, %rN (in 32-bit mode, n < 16)
4383 <BT>l $n, %rN -> btw $n, %rN (in 16-bit mode, n < 16)
4384 */
4385 switch (flag_code)
4386 {
4387 case CODE_64BIT:
4388 if (i.tm.extension_opcode != 4)
4389 break;
4390 if (i.types[1].bitfield.qword
4391 && i.op[0].imms->X_add_number < 32
4392 && !(i.op[1].regs->reg_flags & RegRex))
4393 i.tm.opcode_modifier.size = SIZE32;
4394 /* Fall through. */
4395 case CODE_32BIT:
4396 if (i.types[1].bitfield.word
4397 && i.op[0].imms->X_add_number < 16)
4398 i.tm.opcode_modifier.size = SIZE32;
4399 break;
4400 case CODE_16BIT:
4401 if (i.op[0].imms->X_add_number < 16)
4402 i.tm.opcode_modifier.size = SIZE16;
4403 break;
4404 }
4405 }
99112332 4406 else if (i.reg_operands == 3
b6f8c7c4
L
4407 && i.op[0].regs == i.op[1].regs
4408 && !i.types[2].bitfield.xmmword
4409 && (i.tm.opcode_modifier.vex
6225c532 4410 || ((!i.mask.reg || i.mask.zeroing)
e771e7c9 4411 && is_evex_encoding (&i.tm)
80c34c38 4412 && (i.vec_encoding != vex_encoding_evex
dd22218c 4413 || cpu_arch_isa_flags.bitfield.cpuavx512vl
80c34c38 4414 || i.tm.cpu_flags.bitfield.cpuavx512vl
7091c612 4415 || (i.tm.operand_types[2].bitfield.zmmword
dd22218c 4416 && i.types[2].bitfield.ymmword))))
ddb62495 4417 && i.tm.opcode_space == SPACE_0F
5844ccaa
JB
4418 && ((i.tm.base_opcode | 2) == 0x57
4419 || i.tm.base_opcode == 0xdf
4420 || i.tm.base_opcode == 0xef
4421 || (i.tm.base_opcode | 3) == 0xfb
4422 || i.tm.base_opcode == 0x42
4423 || i.tm.base_opcode == 0x47))
b6f8c7c4 4424 {
99112332 4425 /* Optimize: -O1:
8305403a
L
4426 VOP, one of vandnps, vandnpd, vxorps, vxorpd, vpsubb, vpsubd,
4427 vpsubq and vpsubw:
b6f8c7c4
L
4428 EVEX VOP %zmmM, %zmmM, %zmmN
4429 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4430 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4431 EVEX VOP %ymmM, %ymmM, %ymmN
4432 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4433 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4434 VEX VOP %ymmM, %ymmM, %ymmN
4435 -> VEX VOP %xmmM, %xmmM, %xmmN
4436 VOP, one of vpandn and vpxor:
4437 VEX VOP %ymmM, %ymmM, %ymmN
4438 -> VEX VOP %xmmM, %xmmM, %xmmN
4439 VOP, one of vpandnd and vpandnq:
4440 EVEX VOP %zmmM, %zmmM, %zmmN
4441 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4442 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4443 EVEX VOP %ymmM, %ymmM, %ymmN
4444 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4445 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4446 VOP, one of vpxord and vpxorq:
4447 EVEX VOP %zmmM, %zmmM, %zmmN
4448 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4449 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4450 EVEX VOP %ymmM, %ymmM, %ymmN
4451 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4452 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
1424ad86
JB
4453 VOP, one of kxord and kxorq:
4454 VEX VOP %kM, %kM, %kN
4455 -> VEX kxorw %kM, %kM, %kN
4456 VOP, one of kandnd and kandnq:
4457 VEX VOP %kM, %kM, %kN
4458 -> VEX kandnw %kM, %kM, %kN
b6f8c7c4 4459 */
e771e7c9 4460 if (is_evex_encoding (&i.tm))
b6f8c7c4 4461 {
7b1d7ca1 4462 if (i.vec_encoding != vex_encoding_evex)
b6f8c7c4
L
4463 {
4464 i.tm.opcode_modifier.vex = VEX128;
4465 i.tm.opcode_modifier.vexw = VEXW0;
4466 i.tm.opcode_modifier.evex = 0;
4467 }
7b1d7ca1 4468 else if (optimize > 1)
dd22218c
L
4469 i.tm.opcode_modifier.evex = EVEX128;
4470 else
4471 return;
b6f8c7c4 4472 }
f74a6307 4473 else if (i.tm.operand_types[0].bitfield.class == RegMask)
1424ad86 4474 {
35648716 4475 i.tm.opcode_modifier.opcodeprefix = PREFIX_NONE;
1424ad86
JB
4476 i.tm.opcode_modifier.vexw = VEXW0;
4477 }
b6f8c7c4
L
4478 else
4479 i.tm.opcode_modifier.vex = VEX128;
4480
4481 if (i.tm.opcode_modifier.vex)
4482 for (j = 0; j < 3; j++)
4483 {
4484 i.types[j].bitfield.xmmword = 1;
4485 i.types[j].bitfield.ymmword = 0;
4486 }
4487 }
392a5972 4488 else if (i.vec_encoding != vex_encoding_evex
97ed31ae 4489 && !i.types[0].bitfield.zmmword
392a5972 4490 && !i.types[1].bitfield.zmmword
6225c532 4491 && !i.mask.reg
9b345ce8 4492 && !i.broadcast.type
a5748e0d 4493 && !i.broadcast.bytes
97ed31ae 4494 && is_evex_encoding (&i.tm)
35648716
JB
4495 && ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4496 || (i.tm.base_opcode & ~4) == 0xdb
4497 || (i.tm.base_opcode & ~4) == 0xeb)
97ed31ae
L
4498 && i.tm.extension_opcode == None)
4499 {
4500 /* Optimize: -O1:
4501 VOP, one of vmovdqa32, vmovdqa64, vmovdqu8, vmovdqu16,
4502 vmovdqu32 and vmovdqu64:
4503 EVEX VOP %xmmM, %xmmN
4504 -> VEX vmovdqa|vmovdqu %xmmM, %xmmN (M and N < 16)
4505 EVEX VOP %ymmM, %ymmN
4506 -> VEX vmovdqa|vmovdqu %ymmM, %ymmN (M and N < 16)
4507 EVEX VOP %xmmM, mem
4508 -> VEX vmovdqa|vmovdqu %xmmM, mem (M < 16)
4509 EVEX VOP %ymmM, mem
4510 -> VEX vmovdqa|vmovdqu %ymmM, mem (M < 16)
4511 EVEX VOP mem, %xmmN
4512 -> VEX mvmovdqa|vmovdquem, %xmmN (N < 16)
4513 EVEX VOP mem, %ymmN
4514 -> VEX vmovdqa|vmovdqu mem, %ymmN (N < 16)
a0a1771e
JB
4515 VOP, one of vpand, vpandn, vpor, vpxor:
4516 EVEX VOP{d,q} %xmmL, %xmmM, %xmmN
4517 -> VEX VOP %xmmL, %xmmM, %xmmN (L, M, and N < 16)
4518 EVEX VOP{d,q} %ymmL, %ymmM, %ymmN
4519 -> VEX VOP %ymmL, %ymmM, %ymmN (L, M, and N < 16)
4520 EVEX VOP{d,q} mem, %xmmM, %xmmN
4521 -> VEX VOP mem, %xmmM, %xmmN (M and N < 16)
4522 EVEX VOP{d,q} mem, %ymmM, %ymmN
4523 -> VEX VOP mem, %ymmM, %ymmN (M and N < 16)
97ed31ae 4524 */
a0a1771e 4525 for (j = 0; j < i.operands; j++)
392a5972
L
4526 if (operand_type_check (i.types[j], disp)
4527 && i.op[j].disps->X_op == O_constant)
4528 {
4529 /* Since the VEX prefix has 2 or 3 bytes, the EVEX prefix
4530 has 4 bytes, EVEX Disp8 has 1 byte and VEX Disp32 has 4
4531 bytes, we choose EVEX Disp8 over VEX Disp32. */
4532 int evex_disp8, vex_disp8;
4533 unsigned int memshift = i.memshift;
4534 offsetT n = i.op[j].disps->X_add_number;
4535
4536 evex_disp8 = fits_in_disp8 (n);
4537 i.memshift = 0;
4538 vex_disp8 = fits_in_disp8 (n);
4539 if (evex_disp8 != vex_disp8)
4540 {
4541 i.memshift = memshift;
4542 return;
4543 }
4544
4545 i.types[j].bitfield.disp8 = vex_disp8;
4546 break;
4547 }
35648716
JB
4548 if ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4549 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2)
4550 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
97ed31ae
L
4551 i.tm.opcode_modifier.vex
4552 = i.types[0].bitfield.ymmword ? VEX256 : VEX128;
4553 i.tm.opcode_modifier.vexw = VEXW0;
79dec6b7 4554 /* VPAND, VPOR, and VPXOR are commutative. */
35648716 4555 if (i.reg_operands == 3 && i.tm.base_opcode != 0xdf)
79dec6b7 4556 i.tm.opcode_modifier.commutative = 1;
97ed31ae
L
4557 i.tm.opcode_modifier.evex = 0;
4558 i.tm.opcode_modifier.masking = 0;
a0a1771e 4559 i.tm.opcode_modifier.broadcast = 0;
97ed31ae
L
4560 i.tm.opcode_modifier.disp8memshift = 0;
4561 i.memshift = 0;
a0a1771e
JB
4562 if (j < i.operands)
4563 i.types[j].bitfield.disp8
4564 = fits_in_disp8 (i.op[j].disps->X_add_number);
97ed31ae 4565 }
b6f8c7c4
L
4566}
4567
ae531041
L
4568/* Return non-zero for load instruction. */
4569
4570static int
4571load_insn_p (void)
4572{
4573 unsigned int dest;
4574 int any_vex_p = is_any_vex_encoding (&i.tm);
4575 unsigned int base_opcode = i.tm.base_opcode | 1;
4576
4577 if (!any_vex_p)
4578 {
ef07be45
CL
4579 /* Anysize insns: lea, invlpg, clflush, prefetch*, bndmk, bndcl, bndcu,
4580 bndcn, bndstx, bndldx, clflushopt, clwb, cldemote. */
255571cd 4581 if (i.tm.opcode_modifier.operandconstraint == ANY_SIZE)
ae531041
L
4582 return 0;
4583
389d00a5 4584 /* pop. */
6d86a545 4585 if (i.tm.mnem_off == MN_pop)
389d00a5
JB
4586 return 1;
4587 }
4588
ddb62495 4589 if (i.tm.opcode_space == SPACE_BASE)
389d00a5
JB
4590 {
4591 /* popf, popa. */
4592 if (i.tm.base_opcode == 0x9d
a09f656b 4593 || i.tm.base_opcode == 0x61)
ae531041
L
4594 return 1;
4595
4596 /* movs, cmps, lods, scas. */
4597 if ((i.tm.base_opcode | 0xb) == 0xaf)
4598 return 1;
4599
a09f656b 4600 /* outs, xlatb. */
4601 if (base_opcode == 0x6f
4602 || i.tm.base_opcode == 0xd7)
ae531041 4603 return 1;
a09f656b 4604 /* NB: For AMD-specific insns with implicit memory operands,
4605 they're intentionally not covered. */
ae531041
L
4606 }
4607
4608 /* No memory operand. */
4609 if (!i.mem_operands)
4610 return 0;
4611
4612 if (any_vex_p)
4613 {
7fc69528 4614 if (i.tm.mnem_off == MN_vldmxcsr)
ae531041
L
4615 return 1;
4616 }
ddb62495 4617 else if (i.tm.opcode_space == SPACE_BASE)
ae531041
L
4618 {
4619 /* test, not, neg, mul, imul, div, idiv. */
aa4c197d 4620 if (base_opcode == 0xf7 && i.tm.extension_opcode != 1)
ae531041
L
4621 return 1;
4622
4623 /* inc, dec. */
4624 if (base_opcode == 0xff && i.tm.extension_opcode <= 1)
4625 return 1;
4626
4627 /* add, or, adc, sbb, and, sub, xor, cmp. */
4628 if (i.tm.base_opcode >= 0x80 && i.tm.base_opcode <= 0x83)
4629 return 1;
4630
ae531041 4631 /* rol, ror, rcl, rcr, shl/sal, shr, sar. */
aa4c197d 4632 if ((base_opcode == 0xc1 || (base_opcode | 2) == 0xd3)
ae531041
L
4633 && i.tm.extension_opcode != 6)
4634 return 1;
4635
ae531041 4636 /* Check for x87 instructions. */
aa4c197d 4637 if ((base_opcode | 6) == 0xdf)
ae531041
L
4638 {
4639 /* Skip fst, fstp, fstenv, fstcw. */
4640 if (i.tm.base_opcode == 0xd9
4641 && (i.tm.extension_opcode == 2
4642 || i.tm.extension_opcode == 3
4643 || i.tm.extension_opcode == 6
4644 || i.tm.extension_opcode == 7))
4645 return 0;
4646
4647 /* Skip fisttp, fist, fistp, fstp. */
4648 if (i.tm.base_opcode == 0xdb
4649 && (i.tm.extension_opcode == 1
4650 || i.tm.extension_opcode == 2
4651 || i.tm.extension_opcode == 3
4652 || i.tm.extension_opcode == 7))
4653 return 0;
4654
4655 /* Skip fisttp, fst, fstp, fsave, fstsw. */
4656 if (i.tm.base_opcode == 0xdd
4657 && (i.tm.extension_opcode == 1
4658 || i.tm.extension_opcode == 2
4659 || i.tm.extension_opcode == 3
4660 || i.tm.extension_opcode == 6
4661 || i.tm.extension_opcode == 7))
4662 return 0;
4663
4664 /* Skip fisttp, fist, fistp, fbstp, fistp. */
4665 if (i.tm.base_opcode == 0xdf
4666 && (i.tm.extension_opcode == 1
4667 || i.tm.extension_opcode == 2
4668 || i.tm.extension_opcode == 3
4669 || i.tm.extension_opcode == 6
4670 || i.tm.extension_opcode == 7))
4671 return 0;
4672
4673 return 1;
4674 }
4675 }
ddb62495 4676 else if (i.tm.opcode_space == SPACE_0F)
389d00a5
JB
4677 {
4678 /* bt, bts, btr, btc. */
4679 if (i.tm.base_opcode == 0xba
aa4c197d 4680 && (i.tm.extension_opcode | 3) == 7)
389d00a5
JB
4681 return 1;
4682
4683 /* cmpxchg8b, cmpxchg16b, xrstors, vmptrld. */
4684 if (i.tm.base_opcode == 0xc7
4685 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
4686 && (i.tm.extension_opcode == 1 || i.tm.extension_opcode == 3
4687 || i.tm.extension_opcode == 6))
4688 return 1;
4689
4690 /* fxrstor, ldmxcsr, xrstor. */
4691 if (i.tm.base_opcode == 0xae
4692 && (i.tm.extension_opcode == 1
4693 || i.tm.extension_opcode == 2
4694 || i.tm.extension_opcode == 5))
4695 return 1;
4696
4697 /* lgdt, lidt, lmsw. */
4698 if (i.tm.base_opcode == 0x01
4699 && (i.tm.extension_opcode == 2
4700 || i.tm.extension_opcode == 3
4701 || i.tm.extension_opcode == 6))
4702 return 1;
4703 }
ae531041
L
4704
4705 dest = i.operands - 1;
4706
4707 /* Check fake imm8 operand and 3 source operands. */
4708 if ((i.tm.opcode_modifier.immext
aa180741 4709 || i.reg_operands + i.mem_operands == 4)
ae531041
L
4710 && i.types[dest].bitfield.imm8)
4711 dest--;
4712
389d00a5 4713 /* add, or, adc, sbb, and, sub, xor, cmp, test, xchg. */
ddb62495 4714 if (i.tm.opcode_space == SPACE_BASE
aa4c197d 4715 && ((base_opcode | 0x38) == 0x39
389d00a5
JB
4716 || (base_opcode | 2) == 0x87))
4717 return 1;
4718
7fc69528 4719 if (i.tm.mnem_off == MN_xadd)
ae531041
L
4720 return 1;
4721
4722 /* Check for load instruction. */
4723 return (i.types[dest].bitfield.class != ClassNone
4724 || i.types[dest].bitfield.instance == Accum);
4725}
4726
4727/* Output lfence, 0xfaee8, after instruction. */
4728
4729static void
4730insert_lfence_after (void)
4731{
4732 if (lfence_after_load && load_insn_p ())
4733 {
a09f656b 4734 /* There are also two REP string instructions that require
4735 special treatment. Specifically, the compare string (CMPS)
4736 and scan string (SCAS) instructions set EFLAGS in a manner
4737 that depends on the data being compared/scanned. When used
4738 with a REP prefix, the number of iterations may therefore
4739 vary depending on this data. If the data is a program secret
4740 chosen by the adversary using an LVI method,
4741 then this data-dependent behavior may leak some aspect
4742 of the secret. */
aa4c197d 4743 if (((i.tm.base_opcode | 0x9) == 0xaf)
a09f656b 4744 && i.prefix[REP_PREFIX])
4745 {
4746 as_warn (_("`%s` changes flags which would affect control flow behavior"),
76d3f746 4747 insn_name (&i.tm));
a09f656b 4748 }
ae531041
L
4749 char *p = frag_more (3);
4750 *p++ = 0xf;
4751 *p++ = 0xae;
4752 *p = 0xe8;
4753 }
4754}
4755
4756/* Output lfence, 0xfaee8, before instruction. */
4757
4758static void
4759insert_lfence_before (void)
4760{
4761 char *p;
4762
ddb62495 4763 if (i.tm.opcode_space != SPACE_BASE)
ae531041
L
4764 return;
4765
4766 if (i.tm.base_opcode == 0xff
4767 && (i.tm.extension_opcode == 2 || i.tm.extension_opcode == 4))
4768 {
4769 /* Insert lfence before indirect branch if needed. */
4770
4771 if (lfence_before_indirect_branch == lfence_branch_none)
4772 return;
4773
4774 if (i.operands != 1)
4775 abort ();
4776
4777 if (i.reg_operands == 1)
4778 {
4779 /* Indirect branch via register. Don't insert lfence with
4780 -mlfence-after-load=yes. */
4781 if (lfence_after_load
4782 || lfence_before_indirect_branch == lfence_branch_memory)
4783 return;
4784 }
4785 else if (i.mem_operands == 1
4786 && lfence_before_indirect_branch != lfence_branch_register)
4787 {
4788 as_warn (_("indirect `%s` with memory operand should be avoided"),
76d3f746 4789 insn_name (&i.tm));
ae531041
L
4790 return;
4791 }
4792 else
4793 return;
4794
4795 if (last_insn.kind != last_insn_other
4796 && last_insn.seg == now_seg)
4797 {
4798 as_warn_where (last_insn.file, last_insn.line,
4799 _("`%s` skips -mlfence-before-indirect-branch on `%s`"),
76d3f746 4800 last_insn.name, insn_name (&i.tm));
ae531041
L
4801 return;
4802 }
4803
4804 p = frag_more (3);
4805 *p++ = 0xf;
4806 *p++ = 0xae;
4807 *p = 0xe8;
4808 return;
4809 }
4810
503648e4 4811 /* Output or/not/shl and lfence before near ret. */
ae531041 4812 if (lfence_before_ret != lfence_before_ret_none
aa4c197d 4813 && (i.tm.base_opcode | 1) == 0xc3)
ae531041
L
4814 {
4815 if (last_insn.kind != last_insn_other
4816 && last_insn.seg == now_seg)
4817 {
4818 as_warn_where (last_insn.file, last_insn.line,
4819 _("`%s` skips -mlfence-before-ret on `%s`"),
76d3f746 4820 last_insn.name, insn_name (&i.tm));
ae531041
L
4821 return;
4822 }
a09f656b 4823
a09f656b 4824 /* Near ret ingore operand size override under CPU64. */
503648e4 4825 char prefix = flag_code == CODE_64BIT
4826 ? 0x48
4827 : i.prefix[DATA_PREFIX] ? 0x66 : 0x0;
a09f656b 4828
4829 if (lfence_before_ret == lfence_before_ret_not)
4830 {
4831 /* not: 0xf71424, may add prefix
4832 for operand size override or 64-bit code. */
4833 p = frag_more ((prefix ? 2 : 0) + 6 + 3);
4834 if (prefix)
4835 *p++ = prefix;
ae531041
L
4836 *p++ = 0xf7;
4837 *p++ = 0x14;
4838 *p++ = 0x24;
a09f656b 4839 if (prefix)
4840 *p++ = prefix;
ae531041
L
4841 *p++ = 0xf7;
4842 *p++ = 0x14;
4843 *p++ = 0x24;
4844 }
a09f656b 4845 else
4846 {
4847 p = frag_more ((prefix ? 1 : 0) + 4 + 3);
4848 if (prefix)
4849 *p++ = prefix;
4850 if (lfence_before_ret == lfence_before_ret_or)
4851 {
4852 /* or: 0x830c2400, may add prefix
4853 for operand size override or 64-bit code. */
4854 *p++ = 0x83;
4855 *p++ = 0x0c;
4856 }
4857 else
4858 {
4859 /* shl: 0xc1242400, may add prefix
4860 for operand size override or 64-bit code. */
4861 *p++ = 0xc1;
4862 *p++ = 0x24;
4863 }
4864
4865 *p++ = 0x24;
4866 *p++ = 0x0;
4867 }
4868
ae531041
L
4869 *p++ = 0xf;
4870 *p++ = 0xae;
4871 *p = 0xe8;
4872 }
4873}
4874
edd67638
JB
4875/* Shared helper for md_assemble() and s_insn(). */
4876static void init_globals (void)
4877{
4878 unsigned int j;
4879
4880 memset (&i, '\0', sizeof (i));
4881 i.rounding.type = rc_none;
4882 for (j = 0; j < MAX_OPERANDS; j++)
4883 i.reloc[j] = NO_RELOC;
4884 memset (disp_expressions, '\0', sizeof (disp_expressions));
4885 memset (im_expressions, '\0', sizeof (im_expressions));
4886 save_stack_p = save_stack;
4887}
4888
04784e33
JB
4889/* Helper for md_assemble() to decide whether to prepare for a possible 2nd
4890 parsing pass. Instead of introducing a rarely use new insn attribute this
4891 utilizes a common pattern between affected templates. It is deemed
4892 acceptable that this will lead to unnecessary pass 2 preparations in a
4893 limited set of cases. */
4894static INLINE bool may_need_pass2 (const insn_template *t)
4895{
4896 return t->opcode_modifier.sse2avx
4897 /* Note that all SSE2AVX templates have at least one operand. */
a28fedbc 4898 ? t->operand_types[t->operands - 1].bitfield.class == RegSIMD
ddb62495 4899 : (t->opcode_space == SPACE_0F
a28fedbc 4900 && (t->base_opcode | 1) == 0xbf)
ddb62495 4901 || (t->opcode_space == SPACE_BASE
a28fedbc 4902 && t->base_opcode == 0x63);
04784e33
JB
4903}
4904
252b5132
RH
4905/* This is the guts of the machine-dependent assembler. LINE points to a
4906 machine dependent instruction. This function is supposed to emit
4907 the frags/bytes it assembles to. */
4908
4909void
65da13b5 4910md_assemble (char *line)
252b5132 4911{
40fb9820 4912 unsigned int j;
9db83a32 4913 char mnemonic[MAX_MNEM_SIZE], mnem_suffix = 0, *copy = NULL;
04784e33
JB
4914 const char *end, *pass1_mnem = NULL;
4915 enum i386_error pass1_err = 0;
d3ce72d0 4916 const insn_template *t;
252b5132 4917
47926f60 4918 /* Initialize globals. */
04784e33
JB
4919 current_templates = NULL;
4920 retry:
edd67638 4921 init_globals ();
252b5132
RH
4922
4923 /* First parse an instruction mnemonic & call i386_operand for the operands.
4924 We assume that the scrubber has arranged it so that line[0] is the valid
47926f60 4925 start of a (possibly prefixed) mnemonic. */
252b5132 4926
edd67638 4927 end = parse_insn (line, mnemonic, false);
5317ad2c 4928 if (end == NULL)
04784e33
JB
4929 {
4930 if (pass1_mnem != NULL)
4931 goto match_error;
9db83a32
JB
4932 if (i.error != no_error)
4933 {
4934 gas_assert (current_templates != NULL);
4935 if (may_need_pass2 (current_templates->start) && !i.suffix)
4936 goto no_match;
4937 /* No point in trying a 2nd pass - it'll only find the same suffix
4938 again. */
4939 mnem_suffix = i.suffix;
4940 goto match_error;
4941 }
04784e33
JB
4942 return;
4943 }
6d86a545
JB
4944 t = current_templates->start;
4945 if (may_need_pass2 (t))
04784e33
JB
4946 {
4947 /* Make a copy of the full line in case we need to retry. */
4948 copy = xstrdup (line);
4949 }
5317ad2c 4950 line += end - line;
83b16ac6 4951 mnem_suffix = i.suffix;
252b5132 4952
29b0f896 4953 line = parse_operands (line, mnemonic);
ee86248c 4954 this_operand = -1;
29b0f896 4955 if (line == NULL)
04784e33
JB
4956 {
4957 free (copy);
4958 return;
4959 }
252b5132 4960
29b0f896
AM
4961 /* Now we've parsed the mnemonic into a set of templates, and have the
4962 operands at hand. */
4963
b630c145 4964 /* All Intel opcodes have reversed operands except for "bound", "enter",
c0e54661 4965 "invlpg*", "monitor*", "mwait*", "tpause", "umwait", "pvalidate",
b0e8fa7f
TJ
4966 "rmpadjust", "rmpupdate", and "rmpquery". We also don't reverse
4967 intersegment "jmp" and "call" instructions with 2 immediate operands so
4968 that the immediate segment precedes the offset consistently in Intel and
4969 AT&T modes. */
4d456e3d
L
4970 if (intel_syntax
4971 && i.operands > 1
6d86a545
JB
4972 && (t->mnem_off != MN_bound)
4973 && !startswith (mnemonic, "invlpg")
d34049e8
ML
4974 && !startswith (mnemonic, "monitor")
4975 && !startswith (mnemonic, "mwait")
6d86a545 4976 && (t->mnem_off != MN_pvalidate)
d34049e8 4977 && !startswith (mnemonic, "rmp")
6d86a545
JB
4978 && (t->mnem_off != MN_tpause)
4979 && (t->mnem_off != MN_umwait)
47c0279b
JB
4980 && !(i.operands == 2
4981 && operand_type_check (i.types[0], imm)
40fb9820 4982 && operand_type_check (i.types[1], imm)))
29b0f896
AM
4983 swap_operands ();
4984
ec56d5c0
JB
4985 /* The order of the immediates should be reversed
4986 for 2 immediates extrq and insertq instructions */
4987 if (i.imm_operands == 2
6d86a545 4988 && (t->mnem_off == MN_extrq || t->mnem_off == MN_insertq))
ec56d5c0
JB
4989 swap_2_operands (0, 1);
4990
29b0f896
AM
4991 if (i.imm_operands)
4992 optimize_imm ();
4993
0de704b9
JB
4994 if (i.disp_operands && !optimize_disp (t))
4995 return;
29b0f896
AM
4996
4997 /* Next, we find a template that matches the given insn,
4998 making sure the overlap of the given operands types is consistent
4999 with the template operand types. */
252b5132 5000
83b16ac6 5001 if (!(t = match_template (mnem_suffix)))
04784e33
JB
5002 {
5003 const char *err_msg;
5004
5005 if (copy && !mnem_suffix)
5006 {
5007 line = copy;
5008 copy = NULL;
9db83a32 5009 no_match:
04784e33 5010 pass1_err = i.error;
76d3f746 5011 pass1_mnem = insn_name (current_templates->start);
04784e33
JB
5012 goto retry;
5013 }
9db83a32
JB
5014
5015 /* If a non-/only-64bit template (group) was found in pass 1, and if
5016 _some_ template (group) was found in pass 2, squash pass 1's
5017 error. */
5018 if (pass1_err == unsupported_64bit)
5019 pass1_mnem = NULL;
5020
04784e33 5021 match_error:
9db83a32
JB
5022 free (copy);
5023
04784e33
JB
5024 switch (pass1_mnem ? pass1_err : i.error)
5025 {
5026 default:
5027 abort ();
5028 case operand_size_mismatch:
5029 err_msg = _("operand size mismatch");
5030 break;
5031 case operand_type_mismatch:
5032 err_msg = _("operand type mismatch");
5033 break;
5034 case register_type_mismatch:
5035 err_msg = _("register type mismatch");
5036 break;
5037 case number_of_operands_mismatch:
5038 err_msg = _("number of operands mismatch");
5039 break;
5040 case invalid_instruction_suffix:
5041 err_msg = _("invalid instruction suffix");
5042 break;
5043 case bad_imm4:
5044 err_msg = _("constant doesn't fit in 4 bits");
5045 break;
5046 case unsupported_with_intel_mnemonic:
5047 err_msg = _("unsupported with Intel mnemonic");
5048 break;
5049 case unsupported_syntax:
5050 err_msg = _("unsupported syntax");
5051 break;
5052 case unsupported:
5053 as_bad (_("unsupported instruction `%s'"),
76d3f746 5054 pass1_mnem ? pass1_mnem : insn_name (current_templates->start));
04784e33 5055 return;
9db83a32
JB
5056 case unsupported_on_arch:
5057 as_bad (_("`%s' is not supported on `%s%s'"),
76d3f746 5058 pass1_mnem ? pass1_mnem : insn_name (current_templates->start),
9db83a32
JB
5059 cpu_arch_name ? cpu_arch_name : default_arch,
5060 cpu_sub_arch_name ? cpu_sub_arch_name : "");
5061 return;
5062 case unsupported_64bit:
5063 if (ISLOWER (mnem_suffix))
e8b4b7b2
NC
5064 {
5065 if (flag_code == CODE_64BIT)
5066 as_bad (_("`%s%c' is not supported in 64-bit mode"),
76d3f746 5067 pass1_mnem ? pass1_mnem : insn_name (current_templates->start),
e8b4b7b2
NC
5068 mnem_suffix);
5069 else
5070 as_bad (_("`%s%c' is only supported in 64-bit mode"),
76d3f746 5071 pass1_mnem ? pass1_mnem : insn_name (current_templates->start),
e8b4b7b2
NC
5072 mnem_suffix);
5073 }
9db83a32 5074 else
e8b4b7b2
NC
5075 {
5076 if (flag_code == CODE_64BIT)
5077 as_bad (_("`%s' is not supported in 64-bit mode"),
76d3f746 5078 pass1_mnem ? pass1_mnem : insn_name (current_templates->start));
e8b4b7b2
NC
5079 else
5080 as_bad (_("`%s' is only supported in 64-bit mode"),
76d3f746 5081 pass1_mnem ? pass1_mnem : insn_name (current_templates->start));
e8b4b7b2 5082 }
9db83a32 5083 return;
04784e33
JB
5084 case invalid_sib_address:
5085 err_msg = _("invalid SIB address");
5086 break;
5087 case invalid_vsib_address:
5088 err_msg = _("invalid VSIB address");
5089 break;
5090 case invalid_vector_register_set:
5091 err_msg = _("mask, index, and destination registers must be distinct");
5092 break;
5093 case invalid_tmm_register_set:
5094 err_msg = _("all tmm registers must be distinct");
5095 break;
5096 case invalid_dest_and_src_register_set:
5097 err_msg = _("destination and source registers must be distinct");
5098 break;
5099 case unsupported_vector_index_register:
5100 err_msg = _("unsupported vector index register");
5101 break;
5102 case unsupported_broadcast:
5103 err_msg = _("unsupported broadcast");
5104 break;
5105 case broadcast_needed:
5106 err_msg = _("broadcast is needed for operand of such type");
5107 break;
5108 case unsupported_masking:
5109 err_msg = _("unsupported masking");
5110 break;
5111 case mask_not_on_destination:
5112 err_msg = _("mask not on destination operand");
5113 break;
5114 case no_default_mask:
5115 err_msg = _("default mask isn't allowed");
5116 break;
5117 case unsupported_rc_sae:
5118 err_msg = _("unsupported static rounding/sae");
5119 break;
5120 case invalid_register_operand:
5121 err_msg = _("invalid register operand");
5122 break;
5123 }
5124 as_bad (_("%s for `%s'"), err_msg,
76d3f746 5125 pass1_mnem ? pass1_mnem : insn_name (current_templates->start));
04784e33
JB
5126 return;
5127 }
5128
5129 free (copy);
252b5132 5130
7bab8ab5 5131 if (sse_check != check_none
ffb86450
JB
5132 /* The opcode space check isn't strictly needed; it's there only to
5133 bypass the logic below when easily possible. */
ddb62495
JB
5134 && t->opcode_space >= SPACE_0F
5135 && t->opcode_space <= SPACE_0F3A
ffb86450
JB
5136 && !i.tm.cpu_flags.bitfield.cpusse4a
5137 && !is_any_vex_encoding (t))
daf50ae7 5138 {
ffb86450
JB
5139 bool simd = false;
5140
5141 for (j = 0; j < t->operands; ++j)
5142 {
5143 if (t->operand_types[j].bitfield.class == RegMMX)
5144 break;
5145 if (t->operand_types[j].bitfield.class == RegSIMD)
5146 simd = true;
5147 }
5148
5149 if (j >= t->operands && simd)
5150 (sse_check == check_warning
5151 ? as_warn
76d3f746 5152 : as_bad) (_("SSE instruction `%s' is used"), insn_name (&i.tm));
daf50ae7
L
5153 }
5154
40fb9820 5155 if (i.tm.opcode_modifier.fwait)
29b0f896
AM
5156 if (!add_prefix (FWAIT_OPCODE))
5157 return;
252b5132 5158
d5de92cf 5159 /* Check if REP prefix is OK. */
742732c7 5160 if (i.rep_prefix && i.tm.opcode_modifier.prefixok != PrefixRep)
d5de92cf
L
5161 {
5162 as_bad (_("invalid instruction `%s' after `%s'"),
76d3f746 5163 insn_name (&i.tm), i.rep_prefix);
d5de92cf
L
5164 return;
5165 }
5166
c1ba0266
L
5167 /* Check for lock without a lockable instruction. Destination operand
5168 must be memory unless it is xchg (0x86). */
9a4a4499
JB
5169 if (i.prefix[LOCK_PREFIX])
5170 {
5171 if (i.tm.opcode_modifier.prefixok < PrefixLock
c1ba0266
L
5172 || i.mem_operands == 0
5173 || (i.tm.base_opcode != 0x86
9a4a4499
JB
5174 && !(i.flags[i.operands - 1] & Operand_Mem)))
5175 {
5176 as_bad (_("expecting lockable instruction after `lock'"));
5177 return;
5178 }
5179
5180 /* Zap the redundant prefix from XCHG when optimizing. */
5181 if (i.tm.base_opcode == 0x86 && optimize && !i.no_optimize)
5182 i.prefix[LOCK_PREFIX] = 0;
c32fa91d
L
5183 }
5184
e3669c7f
JB
5185 if (is_any_vex_encoding (&i.tm)
5186 || i.tm.operand_types[i.imm_operands].bitfield.class >= RegMMX
5187 || i.tm.operand_types[i.imm_operands + 1].bitfield.class >= RegMMX)
7a8655d2 5188 {
e3669c7f
JB
5189 /* Check for data size prefix on VEX/XOP/EVEX encoded and SIMD insns. */
5190 if (i.prefix[DATA_PREFIX])
5191 {
76d3f746 5192 as_bad (_("data size prefix invalid with `%s'"), insn_name (&i.tm));
e3669c7f
JB
5193 return;
5194 }
5195
5196 /* Don't allow e.g. KMOV in TLS code sequences. */
5197 for (j = i.imm_operands; j < i.operands; ++j)
5198 switch (i.reloc[j])
5199 {
5200 case BFD_RELOC_386_TLS_GOTIE:
5201 case BFD_RELOC_386_TLS_LE_32:
5202 case BFD_RELOC_X86_64_GOTTPOFF:
5203 case BFD_RELOC_X86_64_TLSLD:
76d3f746 5204 as_bad (_("TLS relocation cannot be used with `%s'"), insn_name (&i.tm));
e3669c7f
JB
5205 return;
5206 default:
5207 break;
5208 }
7a8655d2
JB
5209 }
5210
42164a71 5211 /* Check if HLE prefix is OK. */
165de32a 5212 if (i.hle_prefix && !check_hle ())
42164a71
L
5213 return;
5214
7e8b059b
L
5215 /* Check BND prefix. */
5216 if (i.bnd_prefix && !i.tm.opcode_modifier.bndprefixok)
5217 as_bad (_("expecting valid branch instruction after `bnd'"));
5218
04ef582a 5219 /* Check NOTRACK prefix. */
742732c7 5220 if (i.notrack_prefix && i.tm.opcode_modifier.prefixok != PrefixNoTrack)
9fef80d6 5221 as_bad (_("expecting indirect branch instruction after `notrack'"));
04ef582a 5222
327e8c42
JB
5223 if (i.tm.cpu_flags.bitfield.cpumpx)
5224 {
5225 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
5226 as_bad (_("32-bit address isn't allowed in 64-bit MPX instructions."));
5227 else if (flag_code != CODE_16BIT
5228 ? i.prefix[ADDR_PREFIX]
5229 : i.mem_operands && !i.prefix[ADDR_PREFIX])
5230 as_bad (_("16-bit address isn't allowed in MPX instructions"));
5231 }
7e8b059b
L
5232
5233 /* Insert BND prefix. */
76d3a78a
JB
5234 if (add_bnd_prefix && i.tm.opcode_modifier.bndprefixok)
5235 {
5236 if (!i.prefix[BND_PREFIX])
5237 add_prefix (BND_PREFIX_OPCODE);
5238 else if (i.prefix[BND_PREFIX] != BND_PREFIX_OPCODE)
5239 {
5240 as_warn (_("replacing `rep'/`repe' prefix by `bnd'"));
5241 i.prefix[BND_PREFIX] = BND_PREFIX_OPCODE;
5242 }
5243 }
7e8b059b 5244
29b0f896 5245 /* Check string instruction segment overrides. */
51c8edf6 5246 if (i.tm.opcode_modifier.isstring >= IS_STRING_ES_OP0)
29b0f896 5247 {
51c8edf6 5248 gas_assert (i.mem_operands);
29b0f896 5249 if (!check_string ())
5dd0794d 5250 return;
fc0763e6 5251 i.disp_operands = 0;
29b0f896 5252 }
5dd0794d 5253
9373f275
L
5254 /* The memory operand of (%dx) should be only used with input/output
5255 instructions (base opcodes: 0x6c, 0x6e, 0xec, 0xee). */
5256 if (i.input_output_operand
5257 && ((i.tm.base_opcode | 0x82) != 0xee
ddb62495 5258 || i.tm.opcode_space != SPACE_BASE))
9373f275
L
5259 {
5260 as_bad (_("input/output port address isn't allowed with `%s'"),
76d3f746 5261 insn_name (&i.tm));
9373f275
L
5262 return;
5263 }
5264
b6f8c7c4
L
5265 if (optimize && !i.no_optimize && i.tm.opcode_modifier.optimize)
5266 optimize_encoding ();
5267
c8480b58
L
5268 if (use_unaligned_vector_move)
5269 encode_with_unaligned_vector_move ();
5270
29b0f896
AM
5271 if (!process_suffix ())
5272 return;
e413e4e9 5273
ef07be45
CL
5274 /* Check if IP-relative addressing requirements can be satisfied. */
5275 if (i.tm.cpu_flags.bitfield.cpuprefetchi
5276 && !(i.base_reg && i.base_reg->reg_num == RegIP))
76d3f746 5277 as_warn (_("'%s' only supports RIP-relative address"), insn_name (&i.tm));
ef07be45 5278
921eafea 5279 /* Update operand types and check extended states. */
bc0844ae 5280 for (j = 0; j < i.operands; j++)
921eafea
L
5281 {
5282 i.types[j] = operand_type_and (i.types[j], i.tm.operand_types[j]);
3d70986f 5283 switch (i.tm.operand_types[j].bitfield.class)
921eafea
L
5284 {
5285 default:
5286 break;
5287 case RegMMX:
5288 i.xstate |= xstate_mmx;
5289 break;
5290 case RegMask:
32930e4e 5291 i.xstate |= xstate_mask;
921eafea
L
5292 break;
5293 case RegSIMD:
3d70986f 5294 if (i.tm.operand_types[j].bitfield.tmmword)
921eafea 5295 i.xstate |= xstate_tmm;
3d70986f 5296 else if (i.tm.operand_types[j].bitfield.zmmword)
921eafea 5297 i.xstate |= xstate_zmm;
3d70986f 5298 else if (i.tm.operand_types[j].bitfield.ymmword)
921eafea 5299 i.xstate |= xstate_ymm;
3d70986f 5300 else if (i.tm.operand_types[j].bitfield.xmmword)
921eafea
L
5301 i.xstate |= xstate_xmm;
5302 break;
5303 }
5304 }
bc0844ae 5305
29b0f896
AM
5306 /* Make still unresolved immediate matches conform to size of immediate
5307 given in i.suffix. */
5308 if (!finalize_imm ())
5309 return;
252b5132 5310
40fb9820 5311 if (i.types[0].bitfield.imm1)
29b0f896 5312 i.imm_operands = 0; /* kludge for shift insns. */
252b5132 5313
29b0f896
AM
5314 /* For insns with operands there are more diddles to do to the opcode. */
5315 if (i.operands)
5316 {
5317 if (!process_operands ())
5318 return;
5319 }
255571cd 5320 else if (!quiet_warnings && i.tm.opcode_modifier.operandconstraint == UGH)
29b0f896
AM
5321 {
5322 /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc. */
76d3f746 5323 as_warn (_("translating to `%sp'"), insn_name (&i.tm));
29b0f896 5324 }
252b5132 5325
7a8655d2 5326 if (is_any_vex_encoding (&i.tm))
9e5e5283 5327 {
c1dc7af5 5328 if (!cpu_arch_flags.bitfield.cpui286)
9e5e5283 5329 {
c1dc7af5 5330 as_bad (_("instruction `%s' isn't supported outside of protected mode."),
76d3f746 5331 insn_name (&i.tm));
9e5e5283
L
5332 return;
5333 }
c0f3af97 5334
0b9404fd
JB
5335 /* Check for explicit REX prefix. */
5336 if (i.prefix[REX_PREFIX] || i.rex_encoding)
5337 {
76d3f746 5338 as_bad (_("REX prefix invalid with `%s'"), insn_name (&i.tm));
0b9404fd
JB
5339 return;
5340 }
5341
9e5e5283
L
5342 if (i.tm.opcode_modifier.vex)
5343 build_vex_prefix (t);
5344 else
5345 build_evex_prefix ();
0b9404fd
JB
5346
5347 /* The individual REX.RXBW bits got consumed. */
5348 i.rex &= REX_OPCODE;
9e5e5283 5349 }
43234a1e 5350
7fc69528
JB
5351 /* Handle conversion of 'int $3' --> special int3 insn. */
5352 if (i.tm.mnem_off == MN_int
a6461c02 5353 && i.op[0].imms->X_add_number == 3)
29b0f896
AM
5354 {
5355 i.tm.base_opcode = INT3_OPCODE;
5356 i.imm_operands = 0;
5357 }
252b5132 5358
0cfa3eb3
JB
5359 if ((i.tm.opcode_modifier.jump == JUMP
5360 || i.tm.opcode_modifier.jump == JUMP_BYTE
5361 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896
AM
5362 && i.op[0].disps->X_op == O_constant)
5363 {
5364 /* Convert "jmp constant" (and "call constant") to a jump (call) to
5365 the absolute address given by the constant. Since ix86 jumps and
5366 calls are pc relative, we need to generate a reloc. */
5367 i.op[0].disps->X_add_symbol = &abs_symbol;
5368 i.op[0].disps->X_op = O_symbol;
5369 }
252b5132 5370
29b0f896
AM
5371 /* For 8 bit registers we need an empty rex prefix. Also if the
5372 instruction already has a prefix, we need to convert old
5373 registers to new ones. */
773f551c 5374
bab6aec1 5375 if ((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte
29b0f896 5376 && (i.op[0].regs->reg_flags & RegRex64) != 0)
bab6aec1 5377 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte
29b0f896 5378 && (i.op[1].regs->reg_flags & RegRex64) != 0)
bab6aec1
JB
5379 || (((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte)
5380 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte))
29b0f896
AM
5381 && i.rex != 0))
5382 {
5383 int x;
726c5dcd 5384
29b0f896
AM
5385 i.rex |= REX_OPCODE;
5386 for (x = 0; x < 2; x++)
5387 {
5388 /* Look for 8 bit operand that uses old registers. */
bab6aec1 5389 if (i.types[x].bitfield.class == Reg && i.types[x].bitfield.byte
29b0f896 5390 && (i.op[x].regs->reg_flags & RegRex64) == 0)
773f551c 5391 {
3f93af61 5392 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
29b0f896
AM
5393 /* In case it is "hi" register, give up. */
5394 if (i.op[x].regs->reg_num > 3)
a540244d 5395 as_bad (_("can't encode register '%s%s' in an "
4eed87de 5396 "instruction requiring REX prefix."),
a540244d 5397 register_prefix, i.op[x].regs->reg_name);
773f551c 5398
29b0f896
AM
5399 /* Otherwise it is equivalent to the extended register.
5400 Since the encoding doesn't change this is merely
5401 cosmetic cleanup for debug output. */
5402
5403 i.op[x].regs = i.op[x].regs + 8;
773f551c 5404 }
29b0f896
AM
5405 }
5406 }
773f551c 5407
6b6b6807
L
5408 if (i.rex == 0 && i.rex_encoding)
5409 {
5410 /* Check if we can add a REX_OPCODE byte. Look for 8 bit operand
3f93af61 5411 that uses legacy register. If it is "hi" register, don't add
6b6b6807
L
5412 the REX_OPCODE byte. */
5413 int x;
5414 for (x = 0; x < 2; x++)
bab6aec1 5415 if (i.types[x].bitfield.class == Reg
6b6b6807
L
5416 && i.types[x].bitfield.byte
5417 && (i.op[x].regs->reg_flags & RegRex64) == 0
5418 && i.op[x].regs->reg_num > 3)
5419 {
3f93af61 5420 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
5b7c81bd 5421 i.rex_encoding = false;
6b6b6807
L
5422 break;
5423 }
5424
5425 if (i.rex_encoding)
5426 i.rex = REX_OPCODE;
5427 }
5428
7ab9ffdd 5429 if (i.rex != 0)
29b0f896
AM
5430 add_prefix (REX_OPCODE | i.rex);
5431
ae531041
L
5432 insert_lfence_before ();
5433
29b0f896
AM
5434 /* We are ready to output the insn. */
5435 output_insn ();
e379e5f3 5436
ae531041
L
5437 insert_lfence_after ();
5438
e379e5f3
L
5439 last_insn.seg = now_seg;
5440
5441 if (i.tm.opcode_modifier.isprefix)
5442 {
5443 last_insn.kind = last_insn_prefix;
76d3f746 5444 last_insn.name = insn_name (&i.tm);
e379e5f3
L
5445 last_insn.file = as_where (&last_insn.line);
5446 }
5447 else
5448 last_insn.kind = last_insn_other;
29b0f896
AM
5449}
5450
9db83a32
JB
5451/* The Q suffix is generally valid only in 64-bit mode, with very few
5452 exceptions: fild, fistp, fisttp, and cmpxchg8b. Note that for fild
5453 and fisttp only one of their two templates is matched below: That's
5454 sufficient since other relevant attributes are the same between both
5455 respective templates. */
5456static INLINE bool q_suffix_allowed(const insn_template *t)
5457{
5458 return flag_code == CODE_64BIT
ddb62495 5459 || (t->opcode_space == SPACE_BASE
9db83a32
JB
5460 && t->base_opcode == 0xdf
5461 && (t->extension_opcode & 1)) /* fild / fistp / fisttp */
7fc69528 5462 || t->mnem_off == MN_cmpxchg8b;
9db83a32
JB
5463}
5464
5317ad2c 5465static const char *
edd67638 5466parse_insn (const char *line, char *mnemonic, bool prefix_only)
29b0f896 5467{
5317ad2c 5468 const char *l = line, *token_start = l;
29b0f896 5469 char *mnem_p;
04784e33 5470 bool pass1 = !current_templates;
5c6af06e 5471 int supported;
d3ce72d0 5472 const insn_template *t;
b6169b20 5473 char *dot_p = NULL;
29b0f896 5474
29b0f896
AM
5475 while (1)
5476 {
5477 mnem_p = mnemonic;
778415f5
JB
5478 /* Pseudo-prefixes start with an opening figure brace. */
5479 if ((*mnem_p = *l) == '{')
5480 {
5481 ++mnem_p;
5482 ++l;
5483 }
29b0f896
AM
5484 while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
5485 {
b6169b20
L
5486 if (*mnem_p == '.')
5487 dot_p = mnem_p;
29b0f896
AM
5488 mnem_p++;
5489 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
45288df1 5490 {
778415f5 5491 too_long:
29b0f896
AM
5492 as_bad (_("no such instruction: `%s'"), token_start);
5493 return NULL;
5494 }
5495 l++;
5496 }
778415f5
JB
5497 /* Pseudo-prefixes end with a closing figure brace. */
5498 if (*mnemonic == '{' && *l == '}')
5499 {
5500 *mnem_p++ = *l++;
5501 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
5502 goto too_long;
5503 *mnem_p = '\0';
5504
5505 /* Point l at the closing brace if there's no other separator. */
5506 if (*l != END_OF_INSN && !is_space_char (*l)
5507 && *l != PREFIX_SEPARATOR)
5508 --l;
5509 }
5510 else if (!is_space_char (*l)
5511 && *l != END_OF_INSN
5512 && (intel_syntax
5513 || (*l != PREFIX_SEPARATOR && *l != ',')))
29b0f896 5514 {
edd67638
JB
5515 if (prefix_only)
5516 break;
29b0f896
AM
5517 as_bad (_("invalid character %s in mnemonic"),
5518 output_invalid (*l));
5519 return NULL;
5520 }
5521 if (token_start == l)
5522 {
e44823cf 5523 if (!intel_syntax && *l == PREFIX_SEPARATOR)
29b0f896
AM
5524 as_bad (_("expecting prefix; got nothing"));
5525 else
5526 as_bad (_("expecting mnemonic; got nothing"));
5527 return NULL;
5528 }
45288df1 5529
29b0f896 5530 /* Look up instruction (or prefix) via hash table. */
629310ab 5531 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
47926f60 5532
29b0f896
AM
5533 if (*l != END_OF_INSN
5534 && (!is_space_char (*l) || l[1] != END_OF_INSN)
5535 && current_templates
40fb9820 5536 && current_templates->start->opcode_modifier.isprefix)
29b0f896 5537 {
c6fb90c8 5538 if (!cpu_flags_check_cpu64 (current_templates->start->cpu_flags))
2dd88dca
JB
5539 {
5540 as_bad ((flag_code != CODE_64BIT
5541 ? _("`%s' is only supported in 64-bit mode")
5542 : _("`%s' is not supported in 64-bit mode")),
76d3f746 5543 insn_name (current_templates->start));
2dd88dca
JB
5544 return NULL;
5545 }
29b0f896
AM
5546 /* If we are in 16-bit mode, do not allow addr16 or data16.
5547 Similarly, in 32-bit mode, do not allow addr32 or data32. */
673fe0f0
JB
5548 if ((current_templates->start->opcode_modifier.size == SIZE16
5549 || current_templates->start->opcode_modifier.size == SIZE32)
29b0f896 5550 && flag_code != CODE_64BIT
673fe0f0 5551 && ((current_templates->start->opcode_modifier.size == SIZE32)
29b0f896
AM
5552 ^ (flag_code == CODE_16BIT)))
5553 {
5554 as_bad (_("redundant %s prefix"),
76d3f746 5555 insn_name (current_templates->start));
29b0f896 5556 return NULL;
45288df1 5557 }
31184569
JB
5558
5559 if (current_templates->start->base_opcode == PSEUDO_PREFIX)
29b0f896 5560 {
86fa6981 5561 /* Handle pseudo prefixes. */
31184569 5562 switch (current_templates->start->extension_opcode)
86fa6981 5563 {
41eb8e88 5564 case Prefix_Disp8:
86fa6981
L
5565 /* {disp8} */
5566 i.disp_encoding = disp_encoding_8bit;
5567 break;
41eb8e88
L
5568 case Prefix_Disp16:
5569 /* {disp16} */
5570 i.disp_encoding = disp_encoding_16bit;
5571 break;
5572 case Prefix_Disp32:
86fa6981
L
5573 /* {disp32} */
5574 i.disp_encoding = disp_encoding_32bit;
5575 break;
41eb8e88 5576 case Prefix_Load:
86fa6981
L
5577 /* {load} */
5578 i.dir_encoding = dir_encoding_load;
5579 break;
41eb8e88 5580 case Prefix_Store:
86fa6981
L
5581 /* {store} */
5582 i.dir_encoding = dir_encoding_store;
5583 break;
41eb8e88 5584 case Prefix_VEX:
42e04b36
L
5585 /* {vex} */
5586 i.vec_encoding = vex_encoding_vex;
86fa6981 5587 break;
41eb8e88 5588 case Prefix_VEX3:
86fa6981
L
5589 /* {vex3} */
5590 i.vec_encoding = vex_encoding_vex3;
5591 break;
41eb8e88 5592 case Prefix_EVEX:
86fa6981
L
5593 /* {evex} */
5594 i.vec_encoding = vex_encoding_evex;
5595 break;
41eb8e88 5596 case Prefix_REX:
6b6b6807 5597 /* {rex} */
5b7c81bd 5598 i.rex_encoding = true;
6b6b6807 5599 break;
41eb8e88 5600 case Prefix_NoOptimize:
b6f8c7c4 5601 /* {nooptimize} */
5b7c81bd 5602 i.no_optimize = true;
b6f8c7c4 5603 break;
86fa6981
L
5604 default:
5605 abort ();
5606 }
5607 }
5608 else
5609 {
5610 /* Add prefix, checking for repeated prefixes. */
4e9ac44a 5611 switch (add_prefix (current_templates->start->base_opcode))
86fa6981 5612 {
4e9ac44a
L
5613 case PREFIX_EXIST:
5614 return NULL;
5615 case PREFIX_DS:
d777820b 5616 if (current_templates->start->cpu_flags.bitfield.cpuibt)
76d3f746 5617 i.notrack_prefix = insn_name (current_templates->start);
4e9ac44a
L
5618 break;
5619 case PREFIX_REP:
5620 if (current_templates->start->cpu_flags.bitfield.cpuhle)
76d3f746 5621 i.hle_prefix = insn_name (current_templates->start);
4e9ac44a 5622 else if (current_templates->start->cpu_flags.bitfield.cpumpx)
76d3f746 5623 i.bnd_prefix = insn_name (current_templates->start);
4e9ac44a 5624 else
76d3f746 5625 i.rep_prefix = insn_name (current_templates->start);
4e9ac44a
L
5626 break;
5627 default:
5628 break;
86fa6981 5629 }
29b0f896
AM
5630 }
5631 /* Skip past PREFIX_SEPARATOR and reset token_start. */
5632 token_start = ++l;
5633 }
5634 else
5635 break;
5636 }
45288df1 5637
edd67638
JB
5638 if (prefix_only)
5639 return token_start;
5640
30a55f88 5641 if (!current_templates)
b6169b20 5642 {
07d5e953
JB
5643 /* Deprecated functionality (new code should use pseudo-prefixes instead):
5644 Check if we should swap operand or force 32bit displacement in
f8a5c266 5645 encoding. */
30a55f88 5646 if (mnem_p - 2 == dot_p && dot_p[1] == 's')
64c49ab3 5647 i.dir_encoding = dir_encoding_swap;
8d63c93e 5648 else if (mnem_p - 3 == dot_p
a501d77e
L
5649 && dot_p[1] == 'd'
5650 && dot_p[2] == '8')
5651 i.disp_encoding = disp_encoding_8bit;
8d63c93e 5652 else if (mnem_p - 4 == dot_p
f8a5c266
L
5653 && dot_p[1] == 'd'
5654 && dot_p[2] == '3'
5655 && dot_p[3] == '2')
a501d77e 5656 i.disp_encoding = disp_encoding_32bit;
30a55f88
L
5657 else
5658 goto check_suffix;
5659 mnem_p = dot_p;
5660 *dot_p = '\0';
629310ab 5661 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
b6169b20
L
5662 }
5663
04784e33 5664 if (!current_templates || !pass1)
29b0f896 5665 {
04784e33
JB
5666 current_templates = NULL;
5667
dc1e8a47 5668 check_suffix:
1c529385 5669 if (mnem_p > mnemonic)
29b0f896 5670 {
1c529385
LH
5671 /* See if we can get a match by trimming off a suffix. */
5672 switch (mnem_p[-1])
29b0f896 5673 {
1c529385
LH
5674 case WORD_MNEM_SUFFIX:
5675 if (intel_syntax && (intel_float_operand (mnemonic) & 2))
29b0f896
AM
5676 i.suffix = SHORT_MNEM_SUFFIX;
5677 else
1c529385
LH
5678 /* Fall through. */
5679 case BYTE_MNEM_SUFFIX:
5680 case QWORD_MNEM_SUFFIX:
5681 i.suffix = mnem_p[-1];
29b0f896 5682 mnem_p[-1] = '\0';
fe0e921f
AM
5683 current_templates
5684 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5685 break;
5686 case SHORT_MNEM_SUFFIX:
5687 case LONG_MNEM_SUFFIX:
5688 if (!intel_syntax)
5689 {
5690 i.suffix = mnem_p[-1];
5691 mnem_p[-1] = '\0';
fe0e921f
AM
5692 current_templates
5693 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5694 }
5695 break;
5696
5697 /* Intel Syntax. */
5698 case 'd':
5699 if (intel_syntax)
5700 {
5701 if (intel_float_operand (mnemonic) == 1)
5702 i.suffix = SHORT_MNEM_SUFFIX;
5703 else
5704 i.suffix = LONG_MNEM_SUFFIX;
5705 mnem_p[-1] = '\0';
fe0e921f
AM
5706 current_templates
5707 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385 5708 }
04784e33
JB
5709 /* For compatibility reasons accept MOVSD and CMPSD without
5710 operands even in AT&T mode. */
5711 else if (*l == END_OF_INSN
5712 || (is_space_char (*l) && l[1] == END_OF_INSN))
5713 {
5714 mnem_p[-1] = '\0';
5715 current_templates
5716 = (const templates *) str_hash_find (op_hash, mnemonic);
5717 if (current_templates != NULL
5718 /* MOVS or CMPS */
5719 && (current_templates->start->base_opcode | 2) == 0xa6
ddb62495 5720 && current_templates->start->opcode_space
04784e33
JB
5721 == SPACE_BASE
5722 && mnem_p[-2] == 's')
5723 {
5724 as_warn (_("found `%sd'; assuming `%sl' was meant"),
5725 mnemonic, mnemonic);
5726 i.suffix = LONG_MNEM_SUFFIX;
5727 }
5728 else
5729 {
5730 current_templates = NULL;
5731 mnem_p[-1] = 'd';
5732 }
5733 }
1c529385 5734 break;
29b0f896 5735 }
29b0f896 5736 }
1c529385 5737
29b0f896
AM
5738 if (!current_templates)
5739 {
04784e33
JB
5740 if (pass1)
5741 as_bad (_("no such instruction: `%s'"), token_start);
29b0f896
AM
5742 return NULL;
5743 }
5744 }
252b5132 5745
0cfa3eb3
JB
5746 if (current_templates->start->opcode_modifier.jump == JUMP
5747 || current_templates->start->opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
5748 {
5749 /* Check for a branch hint. We allow ",pt" and ",pn" for
5750 predict taken and predict not taken respectively.
5751 I'm not sure that branch hints actually do anything on loop
5752 and jcxz insns (JumpByte) for current Pentium4 chips. They
5753 may work in the future and it doesn't hurt to accept them
5754 now. */
5755 if (l[0] == ',' && l[1] == 'p')
5756 {
5757 if (l[2] == 't')
5758 {
5759 if (!add_prefix (DS_PREFIX_OPCODE))
5760 return NULL;
5761 l += 3;
5762 }
5763 else if (l[2] == 'n')
5764 {
5765 if (!add_prefix (CS_PREFIX_OPCODE))
5766 return NULL;
5767 l += 3;
5768 }
5769 }
5770 }
5771 /* Any other comma loses. */
5772 if (*l == ',')
5773 {
5774 as_bad (_("invalid character %s in mnemonic"),
5775 output_invalid (*l));
5776 return NULL;
5777 }
252b5132 5778
29b0f896 5779 /* Check if instruction is supported on specified architecture. */
5c6af06e
JB
5780 supported = 0;
5781 for (t = current_templates->start; t < current_templates->end; ++t)
5782 {
c0f3af97 5783 supported |= cpu_flags_match (t);
9db83a32
JB
5784
5785 if (i.suffix == QWORD_MNEM_SUFFIX && !q_suffix_allowed (t))
5786 supported &= ~CPU_FLAGS_64BIT_MATCH;
5787
c0f3af97 5788 if (supported == CPU_FLAGS_PERFECT_MATCH)
d59a54c2 5789 return l;
29b0f896 5790 }
3629bb00 5791
9db83a32
JB
5792 if (pass1)
5793 {
5794 if (supported & CPU_FLAGS_64BIT_MATCH)
5795 i.error = unsupported_on_arch;
5796 else
5797 i.error = unsupported_64bit;
5798 }
252b5132 5799
548d0ee6 5800 return NULL;
29b0f896 5801}
252b5132 5802
29b0f896 5803static char *
e3bb37b5 5804parse_operands (char *l, const char *mnemonic)
29b0f896
AM
5805{
5806 char *token_start;
3138f287 5807
29b0f896
AM
5808 /* 1 if operand is pending after ','. */
5809 unsigned int expecting_operand = 0;
252b5132 5810
29b0f896
AM
5811 while (*l != END_OF_INSN)
5812 {
e68c3d59
JB
5813 /* Non-zero if operand parens not balanced. */
5814 unsigned int paren_not_balanced = 0;
5815 /* True if inside double quotes. */
5816 bool in_quotes = false;
5817
29b0f896
AM
5818 /* Skip optional white space before operand. */
5819 if (is_space_char (*l))
5820 ++l;
d02603dc 5821 if (!is_operand_char (*l) && *l != END_OF_INSN && *l != '"')
29b0f896
AM
5822 {
5823 as_bad (_("invalid character %s before operand %d"),
5824 output_invalid (*l),
5825 i.operands + 1);
5826 return NULL;
5827 }
d02603dc 5828 token_start = l; /* After white space. */
e68c3d59 5829 while (in_quotes || paren_not_balanced || *l != ',')
29b0f896
AM
5830 {
5831 if (*l == END_OF_INSN)
5832 {
e68c3d59
JB
5833 if (in_quotes)
5834 {
5835 as_bad (_("unbalanced double quotes in operand %d."),
5836 i.operands + 1);
5837 return NULL;
5838 }
29b0f896
AM
5839 if (paren_not_balanced)
5840 {
98ff9f1c
JB
5841 know (!intel_syntax);
5842 as_bad (_("unbalanced parenthesis in operand %d."),
5843 i.operands + 1);
29b0f896
AM
5844 return NULL;
5845 }
5846 else
5847 break; /* we are done */
5848 }
e68c3d59
JB
5849 else if (*l == '\\' && l[1] == '"')
5850 ++l;
5851 else if (*l == '"')
5852 in_quotes = !in_quotes;
5853 else if (!in_quotes && !is_operand_char (*l) && !is_space_char (*l))
29b0f896
AM
5854 {
5855 as_bad (_("invalid character %s in operand %d"),
5856 output_invalid (*l),
5857 i.operands + 1);
5858 return NULL;
5859 }
e68c3d59 5860 if (!intel_syntax && !in_quotes)
29b0f896
AM
5861 {
5862 if (*l == '(')
5863 ++paren_not_balanced;
5864 if (*l == ')')
5865 --paren_not_balanced;
5866 }
29b0f896
AM
5867 l++;
5868 }
5869 if (l != token_start)
5870 { /* Yes, we've read in another operand. */
5871 unsigned int operand_ok;
5872 this_operand = i.operands++;
5873 if (i.operands > MAX_OPERANDS)
5874 {
5875 as_bad (_("spurious operands; (%d operands/instruction max)"),
5876 MAX_OPERANDS);
5877 return NULL;
5878 }
9d46ce34 5879 i.types[this_operand].bitfield.unspecified = 1;
29b0f896
AM
5880 /* Now parse operand adding info to 'i' as we go along. */
5881 END_STRING_AND_SAVE (l);
5882
1286ab78
L
5883 if (i.mem_operands > 1)
5884 {
5885 as_bad (_("too many memory references for `%s'"),
5886 mnemonic);
5887 return 0;
5888 }
5889
29b0f896
AM
5890 if (intel_syntax)
5891 operand_ok =
5892 i386_intel_operand (token_start,
5893 intel_float_operand (mnemonic));
5894 else
a7619375 5895 operand_ok = i386_att_operand (token_start);
29b0f896
AM
5896
5897 RESTORE_END_STRING (l);
5898 if (!operand_ok)
5899 return NULL;
5900 }
5901 else
5902 {
5903 if (expecting_operand)
5904 {
5905 expecting_operand_after_comma:
5906 as_bad (_("expecting operand after ','; got nothing"));
5907 return NULL;
5908 }
5909 if (*l == ',')
5910 {
5911 as_bad (_("expecting operand before ','; got nothing"));
5912 return NULL;
5913 }
5914 }
7f3f1ea2 5915
29b0f896
AM
5916 /* Now *l must be either ',' or END_OF_INSN. */
5917 if (*l == ',')
5918 {
5919 if (*++l == END_OF_INSN)
5920 {
5921 /* Just skip it, if it's \n complain. */
5922 goto expecting_operand_after_comma;
5923 }
5924 expecting_operand = 1;
5925 }
5926 }
5927 return l;
5928}
7f3f1ea2 5929
050dfa73 5930static void
783c187b 5931swap_2_operands (unsigned int xchg1, unsigned int xchg2)
050dfa73
MM
5932{
5933 union i386_op temp_op;
40fb9820 5934 i386_operand_type temp_type;
c48dadc9 5935 unsigned int temp_flags;
050dfa73 5936 enum bfd_reloc_code_real temp_reloc;
4eed87de 5937
050dfa73
MM
5938 temp_type = i.types[xchg2];
5939 i.types[xchg2] = i.types[xchg1];
5940 i.types[xchg1] = temp_type;
c48dadc9
JB
5941
5942 temp_flags = i.flags[xchg2];
5943 i.flags[xchg2] = i.flags[xchg1];
5944 i.flags[xchg1] = temp_flags;
5945
050dfa73
MM
5946 temp_op = i.op[xchg2];
5947 i.op[xchg2] = i.op[xchg1];
5948 i.op[xchg1] = temp_op;
c48dadc9 5949
050dfa73
MM
5950 temp_reloc = i.reloc[xchg2];
5951 i.reloc[xchg2] = i.reloc[xchg1];
5952 i.reloc[xchg1] = temp_reloc;
43234a1e 5953
c032bc4f
JB
5954 temp_flags = i.imm_bits[xchg2];
5955 i.imm_bits[xchg2] = i.imm_bits[xchg1];
5956 i.imm_bits[xchg1] = temp_flags;
5957
6225c532 5958 if (i.mask.reg)
43234a1e 5959 {
6225c532
JB
5960 if (i.mask.operand == xchg1)
5961 i.mask.operand = xchg2;
5962 else if (i.mask.operand == xchg2)
5963 i.mask.operand = xchg1;
43234a1e 5964 }
a5748e0d 5965 if (i.broadcast.type || i.broadcast.bytes)
43234a1e 5966 {
5273a3cd
JB
5967 if (i.broadcast.operand == xchg1)
5968 i.broadcast.operand = xchg2;
5969 else if (i.broadcast.operand == xchg2)
5970 i.broadcast.operand = xchg1;
43234a1e 5971 }
050dfa73
MM
5972}
5973
29b0f896 5974static void
e3bb37b5 5975swap_operands (void)
29b0f896 5976{
b7c61d9a 5977 switch (i.operands)
050dfa73 5978 {
c0f3af97 5979 case 5:
b7c61d9a 5980 case 4:
4d456e3d 5981 swap_2_operands (1, i.operands - 2);
1a0670f3 5982 /* Fall through. */
b7c61d9a
L
5983 case 3:
5984 case 2:
4d456e3d 5985 swap_2_operands (0, i.operands - 1);
b7c61d9a
L
5986 break;
5987 default:
5988 abort ();
29b0f896 5989 }
29b0f896
AM
5990
5991 if (i.mem_operands == 2)
5992 {
5e042380 5993 const reg_entry *temp_seg;
29b0f896
AM
5994 temp_seg = i.seg[0];
5995 i.seg[0] = i.seg[1];
5996 i.seg[1] = temp_seg;
5997 }
5998}
252b5132 5999
29b0f896
AM
6000/* Try to ensure constant immediates are represented in the smallest
6001 opcode possible. */
6002static void
e3bb37b5 6003optimize_imm (void)
29b0f896
AM
6004{
6005 char guess_suffix = 0;
6006 int op;
252b5132 6007
29b0f896
AM
6008 if (i.suffix)
6009 guess_suffix = i.suffix;
6010 else if (i.reg_operands)
6011 {
6012 /* Figure out a suffix from the last register operand specified.
75e5731b
JB
6013 We can't do this properly yet, i.e. excluding special register
6014 instances, but the following works for instructions with
6015 immediates. In any case, we can't set i.suffix yet. */
29b0f896 6016 for (op = i.operands; --op >= 0;)
bab6aec1
JB
6017 if (i.types[op].bitfield.class != Reg)
6018 continue;
6019 else if (i.types[op].bitfield.byte)
7ab9ffdd 6020 {
40fb9820
L
6021 guess_suffix = BYTE_MNEM_SUFFIX;
6022 break;
6023 }
bab6aec1 6024 else if (i.types[op].bitfield.word)
252b5132 6025 {
40fb9820
L
6026 guess_suffix = WORD_MNEM_SUFFIX;
6027 break;
6028 }
bab6aec1 6029 else if (i.types[op].bitfield.dword)
40fb9820
L
6030 {
6031 guess_suffix = LONG_MNEM_SUFFIX;
6032 break;
6033 }
bab6aec1 6034 else if (i.types[op].bitfield.qword)
40fb9820
L
6035 {
6036 guess_suffix = QWORD_MNEM_SUFFIX;
29b0f896 6037 break;
252b5132 6038 }
29b0f896
AM
6039 }
6040 else if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
6041 guess_suffix = WORD_MNEM_SUFFIX;
5cc00775
JB
6042 else if (flag_code != CODE_64BIT || !(i.prefix[REX_PREFIX] & REX_W))
6043 guess_suffix = LONG_MNEM_SUFFIX;
29b0f896
AM
6044
6045 for (op = i.operands; --op >= 0;)
40fb9820 6046 if (operand_type_check (i.types[op], imm))
29b0f896
AM
6047 {
6048 switch (i.op[op].imms->X_op)
252b5132 6049 {
29b0f896
AM
6050 case O_constant:
6051 /* If a suffix is given, this operand may be shortened. */
6052 switch (guess_suffix)
252b5132 6053 {
29b0f896 6054 case LONG_MNEM_SUFFIX:
40fb9820
L
6055 i.types[op].bitfield.imm32 = 1;
6056 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
6057 break;
6058 case WORD_MNEM_SUFFIX:
40fb9820
L
6059 i.types[op].bitfield.imm16 = 1;
6060 i.types[op].bitfield.imm32 = 1;
6061 i.types[op].bitfield.imm32s = 1;
6062 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
6063 break;
6064 case BYTE_MNEM_SUFFIX:
40fb9820
L
6065 i.types[op].bitfield.imm8 = 1;
6066 i.types[op].bitfield.imm8s = 1;
6067 i.types[op].bitfield.imm16 = 1;
6068 i.types[op].bitfield.imm32 = 1;
6069 i.types[op].bitfield.imm32s = 1;
6070 i.types[op].bitfield.imm64 = 1;
29b0f896 6071 break;
252b5132 6072 }
252b5132 6073
29b0f896
AM
6074 /* If this operand is at most 16 bits, convert it
6075 to a signed 16 bit number before trying to see
6076 whether it will fit in an even smaller size.
6077 This allows a 16-bit operand such as $0xffe0 to
6078 be recognised as within Imm8S range. */
40fb9820 6079 if ((i.types[op].bitfield.imm16)
7e96fb68 6080 && fits_in_unsigned_word (i.op[op].imms->X_add_number))
252b5132 6081 {
87ed972d
JB
6082 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
6083 ^ 0x8000) - 0x8000);
29b0f896 6084 }
a28def75
L
6085#ifdef BFD64
6086 /* Store 32-bit immediate in 64-bit for 64-bit BFD. */
40fb9820 6087 if ((i.types[op].bitfield.imm32)
7e96fb68 6088 && fits_in_unsigned_long (i.op[op].imms->X_add_number))
29b0f896
AM
6089 {
6090 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
6091 ^ ((offsetT) 1 << 31))
6092 - ((offsetT) 1 << 31));
6093 }
a28def75 6094#endif
40fb9820 6095 i.types[op]
c6fb90c8
L
6096 = operand_type_or (i.types[op],
6097 smallest_imm_type (i.op[op].imms->X_add_number));
252b5132 6098
29b0f896
AM
6099 /* We must avoid matching of Imm32 templates when 64bit
6100 only immediate is available. */
6101 if (guess_suffix == QWORD_MNEM_SUFFIX)
40fb9820 6102 i.types[op].bitfield.imm32 = 0;
29b0f896 6103 break;
252b5132 6104
29b0f896
AM
6105 case O_absent:
6106 case O_register:
6107 abort ();
6108
6109 /* Symbols and expressions. */
6110 default:
9cd96992
JB
6111 /* Convert symbolic operand to proper sizes for matching, but don't
6112 prevent matching a set of insns that only supports sizes other
6113 than those matching the insn suffix. */
6114 {
40fb9820 6115 i386_operand_type mask, allowed;
87ed972d 6116 const insn_template *t = current_templates->start;
9cd96992 6117
0dfbf9d7 6118 operand_type_set (&mask, 0);
9cd96992
JB
6119 switch (guess_suffix)
6120 {
6121 case QWORD_MNEM_SUFFIX:
40fb9820
L
6122 mask.bitfield.imm64 = 1;
6123 mask.bitfield.imm32s = 1;
9cd96992
JB
6124 break;
6125 case LONG_MNEM_SUFFIX:
40fb9820 6126 mask.bitfield.imm32 = 1;
9cd96992
JB
6127 break;
6128 case WORD_MNEM_SUFFIX:
40fb9820 6129 mask.bitfield.imm16 = 1;
9cd96992
JB
6130 break;
6131 case BYTE_MNEM_SUFFIX:
40fb9820 6132 mask.bitfield.imm8 = 1;
9cd96992
JB
6133 break;
6134 default:
9cd96992
JB
6135 break;
6136 }
8f0212ac
JB
6137
6138 allowed = operand_type_and (t->operand_types[op], mask);
6139 while (++t < current_templates->end)
6140 {
6141 allowed = operand_type_or (allowed, t->operand_types[op]);
6142 allowed = operand_type_and (allowed, mask);
6143 }
6144
0dfbf9d7 6145 if (!operand_type_all_zero (&allowed))
c6fb90c8 6146 i.types[op] = operand_type_and (i.types[op], mask);
9cd96992 6147 }
29b0f896 6148 break;
252b5132 6149 }
29b0f896
AM
6150 }
6151}
47926f60 6152
29b0f896 6153/* Try to use the smallest displacement type too. */
0de704b9
JB
6154static bool
6155optimize_disp (const insn_template *t)
29b0f896 6156{
0de704b9 6157 unsigned int op;
3e73aa7c 6158
0de704b9
JB
6159 if (!want_disp32 (t)
6160 && (!t->opcode_modifier.jump
6161 || i.jumpabsolute || i.types[0].bitfield.baseindex))
6162 {
6163 for (op = 0; op < i.operands; ++op)
6164 {
6165 const expressionS *exp = i.op[op].disps;
6166
6167 if (!operand_type_check (i.types[op], disp))
6168 continue;
6169
6170 if (exp->X_op != O_constant)
6171 continue;
6172
6173 /* Since displacement is signed extended to 64bit, don't allow
6174 disp32 if it is out of range. */
6175 if (fits_in_signed_long (exp->X_add_number))
6176 continue;
6177
6178 i.types[op].bitfield.disp32 = 0;
6179 if (i.types[op].bitfield.baseindex)
6180 {
6181 as_bad (_("0x%" PRIx64 " out of range of signed 32bit displacement"),
6182 (uint64_t) exp->X_add_number);
6183 return false;
6184 }
6185 }
6186 }
6187
6188 /* Don't optimize displacement for movabs since it only takes 64bit
6189 displacement. */
6190 if (i.disp_encoding > disp_encoding_8bit
6191 || (flag_code == CODE_64BIT && t->mnem_off == MN_movabs))
6192 return true;
6193
6194 for (op = i.operands; op-- > 0;)
40fb9820 6195 if (operand_type_check (i.types[op], disp))
252b5132 6196 {
b300c311 6197 if (i.op[op].disps->X_op == O_constant)
252b5132 6198 {
91d6fa6a 6199 offsetT op_disp = i.op[op].disps->X_add_number;
29b0f896 6200
91d6fa6a 6201 if (!op_disp && i.types[op].bitfield.baseindex)
b300c311 6202 {
2f2be86b
JB
6203 i.types[op] = operand_type_and_not (i.types[op], anydisp);
6204 i.op[op].disps = NULL;
b300c311 6205 i.disp_operands--;
f185acdd
JB
6206 continue;
6207 }
6208
6209 if (i.types[op].bitfield.disp16
cd613c1f 6210 && fits_in_unsigned_word (op_disp))
f185acdd
JB
6211 {
6212 /* If this operand is at most 16 bits, convert
6213 to a signed 16 bit number and don't use 64bit
6214 displacement. */
6215 op_disp = ((op_disp ^ 0x8000) - 0x8000);
6216 i.types[op].bitfield.disp64 = 0;
b300c311 6217 }
f185acdd 6218
28a167a4 6219#ifdef BFD64
a50187b2 6220 /* Optimize 64-bit displacement to 32-bit for 64-bit BFD. */
a775efc8
JB
6221 if ((flag_code != CODE_64BIT
6222 ? i.types[op].bitfield.disp32
0de704b9
JB
6223 : want_disp32 (t)
6224 && (!t->opcode_modifier.jump
a775efc8 6225 || i.jumpabsolute || i.types[op].bitfield.baseindex))
a50187b2 6226 && fits_in_unsigned_long (op_disp))
b300c311 6227 {
a50187b2
JB
6228 /* If this operand is at most 32 bits, convert
6229 to a signed 32 bit number and don't use 64bit
6230 displacement. */
6231 op_disp = (op_disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
6232 i.types[op].bitfield.disp64 = 0;
6233 i.types[op].bitfield.disp32 = 1;
6234 }
28a167a4 6235
a50187b2
JB
6236 if (flag_code == CODE_64BIT && fits_in_signed_long (op_disp))
6237 {
6238 i.types[op].bitfield.disp64 = 0;
a775efc8 6239 i.types[op].bitfield.disp32 = 1;
b300c311 6240 }
28a167a4 6241#endif
40fb9820 6242 if ((i.types[op].bitfield.disp32
40fb9820 6243 || i.types[op].bitfield.disp16)
b5014f7a 6244 && fits_in_disp8 (op_disp))
40fb9820 6245 i.types[op].bitfield.disp8 = 1;
77c59789
JB
6246
6247 i.op[op].disps->X_add_number = op_disp;
252b5132 6248 }
67a4f2b7
AO
6249 else if (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
6250 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL)
6251 {
6252 fix_new_exp (frag_now, frag_more (0) - frag_now->fr_literal, 0,
6253 i.op[op].disps, 0, i.reloc[op]);
2f2be86b 6254 i.types[op] = operand_type_and_not (i.types[op], anydisp);
67a4f2b7
AO
6255 }
6256 else
b300c311 6257 /* We only support 64bit displacement on constants. */
40fb9820 6258 i.types[op].bitfield.disp64 = 0;
252b5132 6259 }
0de704b9
JB
6260
6261 return true;
29b0f896
AM
6262}
6263
4a1b91ea
L
6264/* Return 1 if there is a match in broadcast bytes between operand
6265 GIVEN and instruction template T. */
6266
6267static INLINE int
6268match_broadcast_size (const insn_template *t, unsigned int given)
6269{
6270 return ((t->opcode_modifier.broadcast == BYTE_BROADCAST
6271 && i.types[given].bitfield.byte)
6272 || (t->opcode_modifier.broadcast == WORD_BROADCAST
6273 && i.types[given].bitfield.word)
6274 || (t->opcode_modifier.broadcast == DWORD_BROADCAST
6275 && i.types[given].bitfield.dword)
6276 || (t->opcode_modifier.broadcast == QWORD_BROADCAST
6277 && i.types[given].bitfield.qword));
6278}
6279
6c30d220
L
6280/* Check if operands are valid for the instruction. */
6281
6282static int
6283check_VecOperands (const insn_template *t)
6284{
43234a1e 6285 unsigned int op;
e2195274 6286 i386_cpu_flags cpu;
e2195274
JB
6287
6288 /* Templates allowing for ZMMword as well as YMMword and/or XMMword for
6289 any one operand are implicity requiring AVX512VL support if the actual
6290 operand size is YMMword or XMMword. Since this function runs after
6291 template matching, there's no need to check for YMMword/XMMword in
6292 the template. */
6293 cpu = cpu_flags_and (t->cpu_flags, avx512);
6294 if (!cpu_flags_all_zero (&cpu)
6295 && !t->cpu_flags.bitfield.cpuavx512vl
6296 && !cpu_arch_flags.bitfield.cpuavx512vl)
6297 {
6298 for (op = 0; op < t->operands; ++op)
6299 {
6300 if (t->operand_types[op].bitfield.zmmword
6301 && (i.types[op].bitfield.ymmword
6302 || i.types[op].bitfield.xmmword))
6303 {
6304 i.error = unsupported;
6305 return 1;
6306 }
6307 }
6308 }
43234a1e 6309
22c36940
JB
6310 /* Somewhat similarly, templates specifying both AVX and AVX2 are
6311 requiring AVX2 support if the actual operand size is YMMword. */
6312 if (t->cpu_flags.bitfield.cpuavx
6313 && t->cpu_flags.bitfield.cpuavx2
6314 && !cpu_arch_flags.bitfield.cpuavx2)
6315 {
6316 for (op = 0; op < t->operands; ++op)
6317 {
6318 if (t->operand_types[op].bitfield.xmmword
6319 && i.types[op].bitfield.ymmword)
6320 {
6321 i.error = unsupported;
6322 return 1;
6323 }
6324 }
6325 }
6326
6c30d220 6327 /* Without VSIB byte, we can't have a vector register for index. */
63112cd6 6328 if (!t->opcode_modifier.sib
6c30d220 6329 && i.index_reg
1b54b8d7
JB
6330 && (i.index_reg->reg_type.bitfield.xmmword
6331 || i.index_reg->reg_type.bitfield.ymmword
6332 || i.index_reg->reg_type.bitfield.zmmword))
6c30d220
L
6333 {
6334 i.error = unsupported_vector_index_register;
6335 return 1;
6336 }
6337
ad8ecc81 6338 /* Check if default mask is allowed. */
255571cd 6339 if (t->opcode_modifier.operandconstraint == NO_DEFAULT_MASK
6225c532 6340 && (!i.mask.reg || i.mask.reg->reg_num == 0))
ad8ecc81
MZ
6341 {
6342 i.error = no_default_mask;
6343 return 1;
6344 }
6345
7bab8ab5
JB
6346 /* For VSIB byte, we need a vector register for index, and all vector
6347 registers must be distinct. */
260cd341 6348 if (t->opcode_modifier.sib && t->opcode_modifier.sib != SIBMEM)
7bab8ab5
JB
6349 {
6350 if (!i.index_reg
63112cd6 6351 || !((t->opcode_modifier.sib == VECSIB128
1b54b8d7 6352 && i.index_reg->reg_type.bitfield.xmmword)
63112cd6 6353 || (t->opcode_modifier.sib == VECSIB256
1b54b8d7 6354 && i.index_reg->reg_type.bitfield.ymmword)
63112cd6 6355 || (t->opcode_modifier.sib == VECSIB512
1b54b8d7 6356 && i.index_reg->reg_type.bitfield.zmmword)))
7bab8ab5
JB
6357 {
6358 i.error = invalid_vsib_address;
6359 return 1;
6360 }
6361
6225c532
JB
6362 gas_assert (i.reg_operands == 2 || i.mask.reg);
6363 if (i.reg_operands == 2 && !i.mask.reg)
43234a1e 6364 {
3528c362 6365 gas_assert (i.types[0].bitfield.class == RegSIMD);
1b54b8d7
JB
6366 gas_assert (i.types[0].bitfield.xmmword
6367 || i.types[0].bitfield.ymmword);
3528c362 6368 gas_assert (i.types[2].bitfield.class == RegSIMD);
1b54b8d7
JB
6369 gas_assert (i.types[2].bitfield.xmmword
6370 || i.types[2].bitfield.ymmword);
43234a1e
L
6371 if (operand_check == check_none)
6372 return 0;
6373 if (register_number (i.op[0].regs)
6374 != register_number (i.index_reg)
6375 && register_number (i.op[2].regs)
6376 != register_number (i.index_reg)
6377 && register_number (i.op[0].regs)
6378 != register_number (i.op[2].regs))
6379 return 0;
6380 if (operand_check == check_error)
6381 {
6382 i.error = invalid_vector_register_set;
6383 return 1;
6384 }
6385 as_warn (_("mask, index, and destination registers should be distinct"));
6386 }
6225c532 6387 else if (i.reg_operands == 1 && i.mask.reg)
8444f82a 6388 {
3528c362 6389 if (i.types[1].bitfield.class == RegSIMD
1b54b8d7
JB
6390 && (i.types[1].bitfield.xmmword
6391 || i.types[1].bitfield.ymmword
6392 || i.types[1].bitfield.zmmword)
8444f82a
MZ
6393 && (register_number (i.op[1].regs)
6394 == register_number (i.index_reg)))
6395 {
6396 if (operand_check == check_error)
6397 {
6398 i.error = invalid_vector_register_set;
6399 return 1;
6400 }
6401 if (operand_check != check_none)
6402 as_warn (_("index and destination registers should be distinct"));
6403 }
6404 }
43234a1e 6405 }
7bab8ab5 6406
fc141319
L
6407 /* For AMX instructions with 3 TMM register operands, all operands
6408 must be distinct. */
6409 if (i.reg_operands == 3
6410 && t->operand_types[0].bitfield.tmmword
6411 && (i.op[0].regs == i.op[1].regs
6412 || i.op[0].regs == i.op[2].regs
6413 || i.op[1].regs == i.op[2].regs))
6414 {
6415 i.error = invalid_tmm_register_set;
6416 return 1;
260cd341
LC
6417 }
6418
0cc78721
CL
6419 /* For some special instructions require that destination must be distinct
6420 from source registers. */
255571cd 6421 if (t->opcode_modifier.operandconstraint == DISTINCT_DEST)
0cc78721
CL
6422 {
6423 unsigned int dest_reg = i.operands - 1;
6424
6425 know (i.operands >= 3);
6426
6427 /* #UD if dest_reg == src1_reg or dest_reg == src2_reg. */
6428 if (i.op[dest_reg - 1].regs == i.op[dest_reg].regs
6429 || (i.reg_operands > 2
6430 && i.op[dest_reg - 2].regs == i.op[dest_reg].regs))
6431 {
6432 i.error = invalid_dest_and_src_register_set;
6433 return 1;
6434 }
6435 }
6436
43234a1e
L
6437 /* Check if broadcast is supported by the instruction and is applied
6438 to the memory operand. */
a5748e0d 6439 if (i.broadcast.type || i.broadcast.bytes)
43234a1e 6440 {
8e6e0792 6441 i386_operand_type type, overlap;
43234a1e
L
6442
6443 /* Check if specified broadcast is supported in this instruction,
4a1b91ea 6444 and its broadcast bytes match the memory operand. */
5273a3cd 6445 op = i.broadcast.operand;
8e6e0792 6446 if (!t->opcode_modifier.broadcast
c48dadc9 6447 || !(i.flags[op] & Operand_Mem)
c39e5b26 6448 || (!i.types[op].bitfield.unspecified
4a1b91ea 6449 && !match_broadcast_size (t, op)))
43234a1e
L
6450 {
6451 bad_broadcast:
6452 i.error = unsupported_broadcast;
6453 return 1;
6454 }
8e6e0792
JB
6455
6456 operand_type_set (&type, 0);
a5748e0d 6457 switch (get_broadcast_bytes (t, false))
8e6e0792 6458 {
4a1b91ea
L
6459 case 2:
6460 type.bitfield.word = 1;
6461 break;
6462 case 4:
6463 type.bitfield.dword = 1;
6464 break;
8e6e0792
JB
6465 case 8:
6466 type.bitfield.qword = 1;
6467 break;
6468 case 16:
6469 type.bitfield.xmmword = 1;
6470 break;
6471 case 32:
6472 type.bitfield.ymmword = 1;
6473 break;
6474 case 64:
6475 type.bitfield.zmmword = 1;
6476 break;
6477 default:
6478 goto bad_broadcast;
6479 }
6480
6481 overlap = operand_type_and (type, t->operand_types[op]);
bc49bfd8
JB
6482 if (t->operand_types[op].bitfield.class == RegSIMD
6483 && t->operand_types[op].bitfield.byte
6484 + t->operand_types[op].bitfield.word
6485 + t->operand_types[op].bitfield.dword
6486 + t->operand_types[op].bitfield.qword > 1)
6487 {
6488 overlap.bitfield.xmmword = 0;
6489 overlap.bitfield.ymmword = 0;
6490 overlap.bitfield.zmmword = 0;
6491 }
8e6e0792
JB
6492 if (operand_type_all_zero (&overlap))
6493 goto bad_broadcast;
6494
9c19e9ec 6495 if (t->opcode_modifier.checkoperandsize)
8e6e0792
JB
6496 {
6497 unsigned int j;
6498
e2195274 6499 type.bitfield.baseindex = 1;
8e6e0792
JB
6500 for (j = 0; j < i.operands; ++j)
6501 {
6502 if (j != op
6503 && !operand_type_register_match(i.types[j],
6504 t->operand_types[j],
6505 type,
6506 t->operand_types[op]))
6507 goto bad_broadcast;
6508 }
6509 }
43234a1e
L
6510 }
6511 /* If broadcast is supported in this instruction, we need to check if
6512 operand of one-element size isn't specified without broadcast. */
6513 else if (t->opcode_modifier.broadcast && i.mem_operands)
6514 {
6515 /* Find memory operand. */
6516 for (op = 0; op < i.operands; op++)
8dc0818e 6517 if (i.flags[op] & Operand_Mem)
43234a1e
L
6518 break;
6519 gas_assert (op < i.operands);
6520 /* Check size of the memory operand. */
4a1b91ea 6521 if (match_broadcast_size (t, op))
43234a1e
L
6522 {
6523 i.error = broadcast_needed;
6524 return 1;
6525 }
6526 }
c39e5b26
JB
6527 else
6528 op = MAX_OPERANDS - 1; /* Avoid uninitialized variable warning. */
43234a1e
L
6529
6530 /* Check if requested masking is supported. */
6225c532 6531 if (i.mask.reg)
43234a1e 6532 {
ae2387fe
JB
6533 switch (t->opcode_modifier.masking)
6534 {
6535 case BOTH_MASKING:
6536 break;
6537 case MERGING_MASKING:
6225c532 6538 if (i.mask.zeroing)
ae2387fe
JB
6539 {
6540 case 0:
6541 i.error = unsupported_masking;
6542 return 1;
6543 }
6544 break;
6545 case DYNAMIC_MASKING:
6546 /* Memory destinations allow only merging masking. */
6225c532 6547 if (i.mask.zeroing && i.mem_operands)
ae2387fe
JB
6548 {
6549 /* Find memory operand. */
6550 for (op = 0; op < i.operands; op++)
c48dadc9 6551 if (i.flags[op] & Operand_Mem)
ae2387fe
JB
6552 break;
6553 gas_assert (op < i.operands);
6554 if (op == i.operands - 1)
6555 {
6556 i.error = unsupported_masking;
6557 return 1;
6558 }
6559 }
6560 break;
6561 default:
6562 abort ();
6563 }
43234a1e
L
6564 }
6565
6566 /* Check if masking is applied to dest operand. */
6225c532 6567 if (i.mask.reg && (i.mask.operand != i.operands - 1))
43234a1e
L
6568 {
6569 i.error = mask_not_on_destination;
6570 return 1;
6571 }
6572
43234a1e 6573 /* Check RC/SAE. */
ca5312a2 6574 if (i.rounding.type != rc_none)
43234a1e 6575 {
a80195f1 6576 if (!t->opcode_modifier.sae
cf665fee
JB
6577 || ((i.rounding.type != saeonly) != t->opcode_modifier.staticrounding)
6578 || i.mem_operands)
43234a1e
L
6579 {
6580 i.error = unsupported_rc_sae;
6581 return 1;
6582 }
cf665fee
JB
6583
6584 /* Non-EVEX.LIG forms need to have a ZMM register as at least one
6585 operand. */
6586 if (t->opcode_modifier.evex != EVEXLIG)
7bab8ab5 6587 {
cf665fee
JB
6588 for (op = 0; op < t->operands; ++op)
6589 if (i.types[op].bitfield.zmmword)
6590 break;
6591 if (op >= t->operands)
6592 {
6593 i.error = operand_size_mismatch;
6594 return 1;
6595 }
7bab8ab5 6596 }
6c30d220
L
6597 }
6598
da4977e0
JB
6599 /* Check the special Imm4 cases; must be the first operand. */
6600 if (t->cpu_flags.bitfield.cpuxop && t->operands == 5)
6601 {
6602 if (i.op[0].imms->X_op != O_constant
6603 || !fits_in_imm4 (i.op[0].imms->X_add_number))
6604 {
6605 i.error = bad_imm4;
6606 return 1;
6607 }
6608
6609 /* Turn off Imm<N> so that update_imm won't complain. */
6610 operand_type_set (&i.types[0], 0);
6611 }
6612
43234a1e 6613 /* Check vector Disp8 operand. */
b5014f7a 6614 if (t->opcode_modifier.disp8memshift
1a42a9fe 6615 && i.disp_encoding <= disp_encoding_8bit)
43234a1e 6616 {
9b345ce8 6617 if (i.broadcast.type || i.broadcast.bytes)
4a1b91ea 6618 i.memshift = t->opcode_modifier.broadcast - 1;
7091c612 6619 else if (t->opcode_modifier.disp8memshift != DISP8_SHIFT_VL)
43234a1e 6620 i.memshift = t->opcode_modifier.disp8memshift;
7091c612
JB
6621 else
6622 {
125ff819 6623 const i386_operand_type *type = NULL, *fallback = NULL;
7091c612
JB
6624
6625 i.memshift = 0;
6626 for (op = 0; op < i.operands; op++)
8dc0818e 6627 if (i.flags[op] & Operand_Mem)
7091c612 6628 {
4174bfff
JB
6629 if (t->opcode_modifier.evex == EVEXLIG)
6630 i.memshift = 2 + (i.suffix == QWORD_MNEM_SUFFIX);
6631 else if (t->operand_types[op].bitfield.xmmword
6632 + t->operand_types[op].bitfield.ymmword
6633 + t->operand_types[op].bitfield.zmmword <= 1)
7091c612
JB
6634 type = &t->operand_types[op];
6635 else if (!i.types[op].bitfield.unspecified)
6636 type = &i.types[op];
125ff819
JB
6637 else /* Ambiguities get resolved elsewhere. */
6638 fallback = &t->operand_types[op];
7091c612 6639 }
3528c362 6640 else if (i.types[op].bitfield.class == RegSIMD
4174bfff 6641 && t->opcode_modifier.evex != EVEXLIG)
7091c612
JB
6642 {
6643 if (i.types[op].bitfield.zmmword)
6644 i.memshift = 6;
6645 else if (i.types[op].bitfield.ymmword && i.memshift < 5)
6646 i.memshift = 5;
6647 else if (i.types[op].bitfield.xmmword && i.memshift < 4)
6648 i.memshift = 4;
6649 }
6650
125ff819
JB
6651 if (!type && !i.memshift)
6652 type = fallback;
7091c612
JB
6653 if (type)
6654 {
6655 if (type->bitfield.zmmword)
6656 i.memshift = 6;
6657 else if (type->bitfield.ymmword)
6658 i.memshift = 5;
6659 else if (type->bitfield.xmmword)
6660 i.memshift = 4;
6661 }
6662
6663 /* For the check in fits_in_disp8(). */
6664 if (i.memshift == 0)
6665 i.memshift = -1;
6666 }
43234a1e
L
6667
6668 for (op = 0; op < i.operands; op++)
6669 if (operand_type_check (i.types[op], disp)
6670 && i.op[op].disps->X_op == O_constant)
6671 {
b5014f7a 6672 if (fits_in_disp8 (i.op[op].disps->X_add_number))
43234a1e 6673 {
b5014f7a
JB
6674 i.types[op].bitfield.disp8 = 1;
6675 return 0;
43234a1e 6676 }
b5014f7a 6677 i.types[op].bitfield.disp8 = 0;
43234a1e
L
6678 }
6679 }
b5014f7a
JB
6680
6681 i.memshift = 0;
43234a1e 6682
6c30d220
L
6683 return 0;
6684}
6685
da4977e0 6686/* Check if encoding requirements are met by the instruction. */
a683cc34
SP
6687
6688static int
da4977e0 6689VEX_check_encoding (const insn_template *t)
a683cc34 6690{
da4977e0
JB
6691 if (i.vec_encoding == vex_encoding_error)
6692 {
6693 i.error = unsupported;
6694 return 1;
6695 }
6696
86fa6981 6697 if (i.vec_encoding == vex_encoding_evex)
43234a1e 6698 {
86fa6981 6699 /* This instruction must be encoded with EVEX prefix. */
e771e7c9 6700 if (!is_evex_encoding (t))
86fa6981
L
6701 {
6702 i.error = unsupported;
6703 return 1;
6704 }
6705 return 0;
43234a1e
L
6706 }
6707
a683cc34 6708 if (!t->opcode_modifier.vex)
86fa6981
L
6709 {
6710 /* This instruction template doesn't have VEX prefix. */
6711 if (i.vec_encoding != vex_encoding_default)
6712 {
6713 i.error = unsupported;
6714 return 1;
6715 }
6716 return 0;
6717 }
a683cc34 6718
a683cc34
SP
6719 return 0;
6720}
6721
7b94647a
JB
6722/* Helper function for the progress() macro in match_template(). */
6723static INLINE enum i386_error progress (enum i386_error new,
6724 enum i386_error last,
6725 unsigned int line, unsigned int *line_p)
6726{
6727 if (line <= *line_p)
6728 return last;
6729 *line_p = line;
6730 return new;
6731}
6732
d3ce72d0 6733static const insn_template *
83b16ac6 6734match_template (char mnem_suffix)
29b0f896
AM
6735{
6736 /* Points to template once we've found it. */
d3ce72d0 6737 const insn_template *t;
40fb9820 6738 i386_operand_type overlap0, overlap1, overlap2, overlap3;
c0f3af97 6739 i386_operand_type overlap4;
29b0f896 6740 unsigned int found_reverse_match;
40fb9820 6741 i386_operand_type operand_types [MAX_OPERANDS];
539e75ad 6742 int addr_prefix_disp;
7b94647a
JB
6743 unsigned int j, size_match, check_register, errline = __LINE__;
6744 enum i386_error specific_error = number_of_operands_mismatch;
6745#define progress(err) progress (err, specific_error, __LINE__, &errline)
29b0f896 6746
c0f3af97
L
6747#if MAX_OPERANDS != 5
6748# error "MAX_OPERANDS must be 5."
f48ff2ae
L
6749#endif
6750
29b0f896 6751 found_reverse_match = 0;
539e75ad 6752 addr_prefix_disp = -1;
40fb9820 6753
45aa61fe 6754 for (t = current_templates->start; t < current_templates->end; t++)
29b0f896 6755 {
539e75ad 6756 addr_prefix_disp = -1;
dbbc8b7e 6757 found_reverse_match = 0;
539e75ad 6758
7b94647a 6759 /* Must have right number of operands. */
29b0f896
AM
6760 if (i.operands != t->operands)
6761 continue;
6762
50aecf8c 6763 /* Check processor support. */
7b94647a 6764 specific_error = progress (unsupported);
45a4bb20 6765 if (cpu_flags_match (t) != CPU_FLAGS_PERFECT_MATCH)
50aecf8c
L
6766 continue;
6767
e1d4d893 6768 /* Check AT&T mnemonic. */
7b94647a 6769 specific_error = progress (unsupported_with_intel_mnemonic);
e1d4d893 6770 if (intel_mnemonic && t->opcode_modifier.attmnemonic)
1efbbeb4
L
6771 continue;
6772
4b5aaf5f 6773 /* Check AT&T/Intel syntax. */
7b94647a 6774 specific_error = progress (unsupported_syntax);
5c07affc 6775 if ((intel_syntax && t->opcode_modifier.attsyntax)
4b5aaf5f 6776 || (!intel_syntax && t->opcode_modifier.intelsyntax))
1efbbeb4
L
6777 continue;
6778
4b5aaf5f
L
6779 /* Check Intel64/AMD64 ISA. */
6780 switch (isa64)
6781 {
6782 default:
6783 /* Default: Don't accept Intel64. */
6784 if (t->opcode_modifier.isa64 == INTEL64)
6785 continue;
6786 break;
6787 case amd64:
6788 /* -mamd64: Don't accept Intel64 and Intel64 only. */
6789 if (t->opcode_modifier.isa64 >= INTEL64)
6790 continue;
6791 break;
6792 case intel64:
6793 /* -mintel64: Don't accept AMD64. */
5990e377 6794 if (t->opcode_modifier.isa64 == AMD64 && flag_code == CODE_64BIT)
4b5aaf5f
L
6795 continue;
6796 break;
6797 }
6798
dc2be329 6799 /* Check the suffix. */
7b94647a 6800 specific_error = progress (invalid_instruction_suffix);
7505bb03
JB
6801 if ((t->opcode_modifier.no_bsuf && mnem_suffix == BYTE_MNEM_SUFFIX)
6802 || (t->opcode_modifier.no_wsuf && mnem_suffix == WORD_MNEM_SUFFIX)
6803 || (t->opcode_modifier.no_lsuf && mnem_suffix == LONG_MNEM_SUFFIX)
6804 || (t->opcode_modifier.no_ssuf && mnem_suffix == SHORT_MNEM_SUFFIX)
6805 || (t->opcode_modifier.no_qsuf && mnem_suffix == QWORD_MNEM_SUFFIX))
83b16ac6 6806 continue;
29b0f896 6807
7b94647a 6808 specific_error = progress (operand_size_mismatch);
3ac21baa
JB
6809 size_match = operand_size_match (t);
6810 if (!size_match)
7d5e4556 6811 continue;
539e75ad 6812
6f2f06be
JB
6813 /* This is intentionally not
6814
0cfa3eb3 6815 if (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE))
6f2f06be
JB
6816
6817 as the case of a missing * on the operand is accepted (perhaps with
6818 a warning, issued further down). */
7b94647a 6819 specific_error = progress (operand_type_mismatch);
0cfa3eb3 6820 if (i.jumpabsolute && t->opcode_modifier.jump != JUMP_ABSOLUTE)
7b94647a 6821 continue;
6f2f06be 6822
a4d3acd2
JB
6823 /* In Intel syntax, normally we can check for memory operand size when
6824 there is no mnemonic suffix. But jmp and call have 2 different
6825 encodings with Dword memory operand size. Skip the "near" one
6826 (permitting a register operand) when "far" was requested. */
6827 if (i.far_branch
6828 && t->opcode_modifier.jump == JUMP_ABSOLUTE
6829 && t->operand_types[0].bitfield.class == Reg)
6830 continue;
6831
5c07affc
L
6832 for (j = 0; j < MAX_OPERANDS; j++)
6833 operand_types[j] = t->operand_types[j];
6834
9db83a32 6835 /* In general, don't allow 32-bit operands on pre-386. */
7b94647a
JB
6836 specific_error = progress (mnem_suffix ? invalid_instruction_suffix
6837 : operand_size_mismatch);
4873e243 6838 j = i.imm_operands + (t->operands > i.imm_operands + 1);
9db83a32
JB
6839 if (i.suffix == LONG_MNEM_SUFFIX
6840 && !cpu_arch_flags.bitfield.cpui386
45aa61fe 6841 && (intel_syntax
3cd7f3e3 6842 ? (t->opcode_modifier.mnemonicsize != IGNORESIZE
76d3f746
JB
6843 && !intel_float_operand (insn_name (t)))
6844 : intel_float_operand (insn_name (t)) != 2)
4873e243
JB
6845 && (t->operands == i.imm_operands
6846 || (operand_types[i.imm_operands].bitfield.class != RegMMX
6847 && operand_types[i.imm_operands].bitfield.class != RegSIMD
6848 && operand_types[i.imm_operands].bitfield.class != RegMask)
6849 || (operand_types[j].bitfield.class != RegMMX
6850 && operand_types[j].bitfield.class != RegSIMD
6851 && operand_types[j].bitfield.class != RegMask))
63112cd6 6852 && !t->opcode_modifier.sib)
192dc9c6
JB
6853 continue;
6854
29b0f896 6855 /* Do not verify operands when there are none. */
e365e234 6856 if (!t->operands)
da4977e0
JB
6857 {
6858 if (VEX_check_encoding (t))
6859 {
7b94647a 6860 specific_error = progress (i.error);
da4977e0
JB
6861 continue;
6862 }
6863
6864 /* We've found a match; break out of loop. */
6865 break;
6866 }
252b5132 6867
48bcea9f
JB
6868 if (!t->opcode_modifier.jump
6869 || t->opcode_modifier.jump == JUMP_ABSOLUTE)
6870 {
6871 /* There should be only one Disp operand. */
6872 for (j = 0; j < MAX_OPERANDS; j++)
6873 if (operand_type_check (operand_types[j], disp))
539e75ad 6874 break;
48bcea9f
JB
6875 if (j < MAX_OPERANDS)
6876 {
5b7c81bd 6877 bool override = (i.prefix[ADDR_PREFIX] != 0);
48bcea9f
JB
6878
6879 addr_prefix_disp = j;
6880
a775efc8
JB
6881 /* Address size prefix will turn Disp64 operand into Disp32 and
6882 Disp32/Disp16 one into Disp16/Disp32 respectively. */
48bcea9f 6883 switch (flag_code)
40fb9820 6884 {
48bcea9f
JB
6885 case CODE_16BIT:
6886 override = !override;
6887 /* Fall through. */
6888 case CODE_32BIT:
6889 if (operand_types[j].bitfield.disp32
6890 && operand_types[j].bitfield.disp16)
40fb9820 6891 {
48bcea9f
JB
6892 operand_types[j].bitfield.disp16 = override;
6893 operand_types[j].bitfield.disp32 = !override;
40fb9820 6894 }
a775efc8 6895 gas_assert (!operand_types[j].bitfield.disp64);
48bcea9f
JB
6896 break;
6897
6898 case CODE_64BIT:
a775efc8 6899 if (operand_types[j].bitfield.disp64)
40fb9820 6900 {
a775efc8 6901 gas_assert (!operand_types[j].bitfield.disp32);
48bcea9f 6902 operand_types[j].bitfield.disp32 = override;
a775efc8 6903 operand_types[j].bitfield.disp64 = !override;
40fb9820 6904 }
48bcea9f
JB
6905 operand_types[j].bitfield.disp16 = 0;
6906 break;
40fb9820 6907 }
539e75ad 6908 }
48bcea9f 6909 }
539e75ad 6910
56ffb741 6911 /* We check register size if needed. */
9c19e9ec 6912 if (t->opcode_modifier.checkoperandsize)
e2195274
JB
6913 {
6914 check_register = (1 << t->operands) - 1;
a5748e0d 6915 if (i.broadcast.type || i.broadcast.bytes)
5273a3cd 6916 check_register &= ~(1 << i.broadcast.operand);
e2195274
JB
6917 }
6918 else
6919 check_register = 0;
6920
c6fb90c8 6921 overlap0 = operand_type_and (i.types[0], operand_types[0]);
29b0f896
AM
6922 switch (t->operands)
6923 {
6924 case 1:
40fb9820 6925 if (!operand_type_match (overlap0, i.types[0]))
29b0f896 6926 continue;
ae9a0a51
JB
6927
6928 /* Allow the ModR/M encoding to be requested by using the {load} or
6929 {store} pseudo prefix on an applicable insn. */
6930 if (!t->opcode_modifier.modrm
6931 && i.reg_operands == 1
6932 && ((i.dir_encoding == dir_encoding_load
6933 && t->mnem_off != MN_pop)
6934 || (i.dir_encoding == dir_encoding_store
6935 && t->mnem_off != MN_push))
6936 /* Avoid BSWAP. */
6937 && t->mnem_off != MN_bswap)
6938 continue;
29b0f896 6939 break;
ae9a0a51 6940
29b0f896 6941 case 2:
33eaf5de 6942 /* xchg %eax, %eax is a special case. It is an alias for nop
8b38ad71
L
6943 only in 32bit mode and we can use opcode 0x90. In 64bit
6944 mode, we can't use 0x90 for xchg %eax, %eax since it should
6945 zero-extend %eax to %rax. */
ae9a0a51
JB
6946 if (t->base_opcode == 0x90
6947 && t->opcode_space == SPACE_BASE)
6948 {
6949 if (flag_code == CODE_64BIT
6950 && i.types[0].bitfield.instance == Accum
6951 && i.types[0].bitfield.dword
6952 && i.types[1].bitfield.instance == Accum)
6953 continue;
6954
6955 /* Allow the ModR/M encoding to be requested by using the
6956 {load} or {store} pseudo prefix. */
6957 if (i.dir_encoding == dir_encoding_load
6958 || i.dir_encoding == dir_encoding_store)
6959 continue;
6960 }
e3669c7f
JB
6961
6962 if (t->base_opcode == MOV_AX_DISP32
ddb62495 6963 && t->opcode_space == SPACE_BASE
69196391 6964 && t->mnem_off != MN_movabs)
e3669c7f
JB
6965 {
6966 /* Force 0x8b encoding for "mov foo@GOT, %eax". */
6967 if (i.reloc[0] == BFD_RELOC_386_GOT32)
6968 continue;
6969
6970 /* xrelease mov %eax, <disp> is another special case. It must not
6971 match the accumulator-only encoding of mov. */
6972 if (i.hle_prefix)
6973 continue;
ae9a0a51
JB
6974
6975 /* Allow the ModR/M encoding to be requested by using a suitable
6976 {load} or {store} pseudo prefix. */
6977 if (i.dir_encoding == (i.types[0].bitfield.instance == Accum
6978 ? dir_encoding_store
6979 : dir_encoding_load)
6980 && !i.types[0].bitfield.disp64
6981 && !i.types[1].bitfield.disp64)
6982 continue;
6983 }
6984
6985 /* Allow the ModR/M encoding to be requested by using the {load} or
6986 {store} pseudo prefix on an applicable insn. */
6987 if (!t->opcode_modifier.modrm
6988 && i.reg_operands == 1
6989 && i.imm_operands == 1
6990 && (i.dir_encoding == dir_encoding_load
6991 || i.dir_encoding == dir_encoding_store)
6992 && t->opcode_space == SPACE_BASE)
6993 {
6994 if (t->base_opcode == 0xb0 /* mov $imm, %reg */
6995 && i.dir_encoding == dir_encoding_store)
6996 continue;
6997
6998 if ((t->base_opcode | 0x38) == 0x3c /* <alu> $imm, %acc */
6999 && (t->base_opcode != 0x3c /* cmp $imm, %acc */
7000 || i.dir_encoding == dir_encoding_load))
7001 continue;
7002
7003 if (t->base_opcode == 0xa8 /* test $imm, %acc */
7004 && i.dir_encoding == dir_encoding_load)
7005 continue;
e3669c7f 7006 }
f5eb1d70
JB
7007 /* Fall through. */
7008
7009 case 3:
3ac21baa
JB
7010 if (!(size_match & MATCH_STRAIGHT))
7011 goto check_reverse;
64c49ab3
JB
7012 /* Reverse direction of operands if swapping is possible in the first
7013 place (operands need to be symmetric) and
7014 - the load form is requested, and the template is a store form,
7015 - the store form is requested, and the template is a load form,
7016 - the non-default (swapped) form is requested. */
7017 overlap1 = operand_type_and (operand_types[0], operand_types[1]);
f5eb1d70 7018 if (t->opcode_modifier.d && i.reg_operands == i.operands
64c49ab3
JB
7019 && !operand_type_all_zero (&overlap1))
7020 switch (i.dir_encoding)
7021 {
7022 case dir_encoding_load:
7023 if (operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 7024 || t->opcode_modifier.regmem)
64c49ab3
JB
7025 goto check_reverse;
7026 break;
7027
7028 case dir_encoding_store:
7029 if (!operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 7030 && !t->opcode_modifier.regmem)
64c49ab3
JB
7031 goto check_reverse;
7032 break;
7033
7034 case dir_encoding_swap:
7035 goto check_reverse;
7036
7037 case dir_encoding_default:
7038 break;
7039 }
86fa6981 7040 /* If we want store form, we skip the current load. */
64c49ab3
JB
7041 if ((i.dir_encoding == dir_encoding_store
7042 || i.dir_encoding == dir_encoding_swap)
86fa6981
L
7043 && i.mem_operands == 0
7044 && t->opcode_modifier.load)
fa99fab2 7045 continue;
1a0670f3 7046 /* Fall through. */
f48ff2ae 7047 case 4:
c0f3af97 7048 case 5:
c6fb90c8 7049 overlap1 = operand_type_and (i.types[1], operand_types[1]);
40fb9820
L
7050 if (!operand_type_match (overlap0, i.types[0])
7051 || !operand_type_match (overlap1, i.types[1])
e2195274 7052 || ((check_register & 3) == 3
dc821c5f 7053 && !operand_type_register_match (i.types[0],
40fb9820 7054 operand_types[0],
dc821c5f 7055 i.types[1],
40fb9820 7056 operand_types[1])))
29b0f896 7057 {
7b94647a
JB
7058 specific_error = progress (i.error);
7059
29b0f896 7060 /* Check if other direction is valid ... */
38e314eb 7061 if (!t->opcode_modifier.d)
29b0f896
AM
7062 continue;
7063
dc1e8a47 7064 check_reverse:
3ac21baa
JB
7065 if (!(size_match & MATCH_REVERSE))
7066 continue;
29b0f896 7067 /* Try reversing direction of operands. */
aa180741 7068 j = t->cpu_flags.bitfield.cpufma4
5dab1799 7069 || t->cpu_flags.bitfield.cpuxop ? 1 : i.operands - 1;
8bd915b7
JB
7070 overlap0 = operand_type_and (i.types[0], operand_types[j]);
7071 overlap1 = operand_type_and (i.types[j], operand_types[0]);
c975cec5
JB
7072 overlap2 = operand_type_and (i.types[1], operand_types[1]);
7073 gas_assert (t->operands != 3 || !check_register);
40fb9820 7074 if (!operand_type_match (overlap0, i.types[0])
8bd915b7 7075 || !operand_type_match (overlap1, i.types[j])
c975cec5
JB
7076 || (t->operands == 3
7077 && !operand_type_match (overlap2, i.types[1]))
45664ddb 7078 || (check_register
dc821c5f 7079 && !operand_type_register_match (i.types[0],
8bd915b7
JB
7080 operand_types[j],
7081 i.types[j],
45664ddb 7082 operand_types[0])))
29b0f896
AM
7083 {
7084 /* Does not match either direction. */
7b94647a 7085 specific_error = progress (i.error);
29b0f896
AM
7086 continue;
7087 }
ac9226cf 7088 /* found_reverse_match holds which variant of D
29b0f896 7089 we've found. */
38e314eb
JB
7090 if (!t->opcode_modifier.d)
7091 found_reverse_match = 0;
7092 else if (operand_types[0].bitfield.tbyte)
ac9226cf 7093 {
4943d587
JB
7094 if (t->opcode_modifier.operandconstraint != UGH)
7095 found_reverse_match = Opcode_FloatD;
bd782808
JB
7096 else
7097 found_reverse_match = ~0;
ac9226cf 7098 /* FSUB{,R} and FDIV{,R} may need a 2nd bit flipped. */
bd782808 7099 if ((t->extension_opcode & 4)
ac9226cf
JB
7100 && (intel_syntax || intel_mnemonic))
7101 found_reverse_match |= Opcode_FloatR;
7102 }
aa180741 7103 else if (t->cpu_flags.bitfield.cpufma4
5dab1799 7104 || t->cpu_flags.bitfield.cpuxop)
8bd915b7
JB
7105 {
7106 found_reverse_match = Opcode_VexW;
7107 goto check_operands_345;
7108 }
ddb62495
JB
7109 else if (t->opcode_space != SPACE_BASE
7110 && (t->opcode_space != SPACE_0F
2c735193
JB
7111 /* MOV to/from CR/DR/TR, as an exception, follow
7112 the base opcode space encoding model. */
7113 || (t->base_opcode | 7) != 0x27))
dbbc8b7e 7114 found_reverse_match = (t->base_opcode & 0xee) != 0x6e
2c735193 7115 ? Opcode_ExtD : Opcode_SIMD_IntD;
a33ef3c2 7116 else if (!t->opcode_modifier.commutative)
38e314eb 7117 found_reverse_match = Opcode_D;
a33ef3c2
JB
7118 else
7119 found_reverse_match = ~0;
29b0f896 7120 }
f48ff2ae 7121 else
29b0f896 7122 {
f48ff2ae 7123 /* Found a forward 2 operand match here. */
8bd915b7 7124 check_operands_345:
d1cbb4db
L
7125 switch (t->operands)
7126 {
c0f3af97 7127 case 5:
3d0738af 7128 overlap4 = operand_type_and (i.types[4], operand_types[4]);
c0f3af97 7129 if (!operand_type_match (overlap4, i.types[4])
dc821c5f 7130 || !operand_type_register_match (i.types[3],
c0f3af97 7131 operand_types[3],
c0f3af97
L
7132 i.types[4],
7133 operand_types[4]))
7b94647a
JB
7134 {
7135 specific_error = progress (i.error);
7136 continue;
7137 }
1a0670f3 7138 /* Fall through. */
f48ff2ae 7139 case 4:
3d0738af 7140 overlap3 = operand_type_and (i.types[3], operand_types[3]);
40fb9820 7141 if (!operand_type_match (overlap3, i.types[3])
e2195274
JB
7142 || ((check_register & 0xa) == 0xa
7143 && !operand_type_register_match (i.types[1],
f7768225
JB
7144 operand_types[1],
7145 i.types[3],
e2195274
JB
7146 operand_types[3]))
7147 || ((check_register & 0xc) == 0xc
7148 && !operand_type_register_match (i.types[2],
7149 operand_types[2],
7150 i.types[3],
7151 operand_types[3])))
7b94647a
JB
7152 {
7153 specific_error = progress (i.error);
7154 continue;
7155 }
1a0670f3 7156 /* Fall through. */
f48ff2ae 7157 case 3:
3d0738af 7158 overlap2 = operand_type_and (i.types[2], operand_types[2]);
40fb9820 7159 if (!operand_type_match (overlap2, i.types[2])
e2195274
JB
7160 || ((check_register & 5) == 5
7161 && !operand_type_register_match (i.types[0],
23e42951
JB
7162 operand_types[0],
7163 i.types[2],
e2195274
JB
7164 operand_types[2]))
7165 || ((check_register & 6) == 6
7166 && !operand_type_register_match (i.types[1],
7167 operand_types[1],
7168 i.types[2],
7169 operand_types[2])))
7b94647a
JB
7170 {
7171 specific_error = progress (i.error);
7172 continue;
7173 }
f48ff2ae
L
7174 break;
7175 }
29b0f896 7176 }
f48ff2ae 7177 /* Found either forward/reverse 2, 3 or 4 operand match here:
29b0f896
AM
7178 slip through to break. */
7179 }
c0f3af97 7180
9bb4d860
L
7181 /* Check if VEX/EVEX encoding requirements can be satisfied. */
7182 if (VEX_check_encoding (t))
da4977e0 7183 {
7b94647a 7184 specific_error = progress (i.error);
da4977e0
JB
7185 continue;
7186 }
7187
9bb4d860
L
7188 /* Check if vector operands are valid. */
7189 if (check_VecOperands (t))
5614d22c 7190 {
7b94647a 7191 specific_error = progress (i.error);
5614d22c
JB
7192 continue;
7193 }
a683cc34 7194
29b0f896
AM
7195 /* We've found a match; break out of loop. */
7196 break;
7197 }
7198
7b94647a
JB
7199#undef progress
7200
29b0f896
AM
7201 if (t == current_templates->end)
7202 {
7203 /* We found no match. */
04784e33 7204 i.error = specific_error;
fa99fab2 7205 return NULL;
29b0f896 7206 }
252b5132 7207
29b0f896
AM
7208 if (!quiet_warnings)
7209 {
7210 if (!intel_syntax
0cfa3eb3 7211 && (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE)))
76d3f746 7212 as_warn (_("indirect %s without `*'"), insn_name (t));
29b0f896 7213
40fb9820 7214 if (t->opcode_modifier.isprefix
3cd7f3e3 7215 && t->opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
7216 {
7217 /* Warn them that a data or address size prefix doesn't
7218 affect assembly of the next line of code. */
76d3f746 7219 as_warn (_("stand-alone `%s' prefix"), insn_name (t));
29b0f896
AM
7220 }
7221 }
7222
7223 /* Copy the template we found. */
9a182d04 7224 install_template (t);
539e75ad
L
7225
7226 if (addr_prefix_disp != -1)
7227 i.tm.operand_types[addr_prefix_disp]
7228 = operand_types[addr_prefix_disp];
7229
8bd915b7 7230 switch (found_reverse_match)
29b0f896 7231 {
8bd915b7
JB
7232 case 0:
7233 break;
7234
bd782808
JB
7235 case Opcode_FloatR:
7236 case Opcode_FloatR | Opcode_FloatD:
7237 i.tm.extension_opcode ^= Opcode_FloatR >> 3;
7238 found_reverse_match &= Opcode_FloatD;
7239
7240 /* Fall through. */
8bd915b7 7241 default:
dfd69174
JB
7242 /* If we found a reverse match we must alter the opcode direction
7243 bit and clear/flip the regmem modifier one. found_reverse_match
7244 holds bits to change (different for int & float insns). */
29b0f896
AM
7245
7246 i.tm.base_opcode ^= found_reverse_match;
7247
dfd69174
JB
7248 /* Certain SIMD insns have their load forms specified in the opcode
7249 table, and hence we need to _set_ RegMem instead of clearing it.
7250 We need to avoid setting the bit though on insns like KMOVW. */
7251 i.tm.opcode_modifier.regmem
7252 = i.tm.opcode_modifier.modrm && i.tm.opcode_modifier.d
7253 && i.tm.operands > 2U - i.tm.opcode_modifier.sse2avx
7254 && !i.tm.opcode_modifier.regmem;
a33ef3c2
JB
7255
7256 /* Fall through. */
7257 case ~0:
7258 i.tm.operand_types[0] = operand_types[i.operands - 1];
7259 i.tm.operand_types[i.operands - 1] = operand_types[0];
8bd915b7
JB
7260 break;
7261
7262 case Opcode_VexW:
7263 /* Only the first two register operands need reversing, alongside
7264 flipping VEX.W. */
7265 i.tm.opcode_modifier.vexw ^= VEXW0 ^ VEXW1;
7266
7267 j = i.tm.operand_types[0].bitfield.imm8;
7268 i.tm.operand_types[j] = operand_types[j + 1];
7269 i.tm.operand_types[j + 1] = operand_types[j];
7270 break;
29b0f896
AM
7271 }
7272
fa99fab2 7273 return t;
29b0f896
AM
7274}
7275
7276static int
e3bb37b5 7277check_string (void)
29b0f896 7278{
51c8edf6
JB
7279 unsigned int es_op = i.tm.opcode_modifier.isstring - IS_STRING_ES_OP0;
7280 unsigned int op = i.tm.operand_types[0].bitfield.baseindex ? es_op : 0;
8dc0818e 7281
5e042380 7282 if (i.seg[op] != NULL && i.seg[op] != reg_es)
29b0f896 7283 {
51c8edf6 7284 as_bad (_("`%s' operand %u must use `%ses' segment"),
76d3f746 7285 insn_name (&i.tm),
51c8edf6
JB
7286 intel_syntax ? i.tm.operands - es_op : es_op + 1,
7287 register_prefix);
7288 return 0;
29b0f896 7289 }
51c8edf6
JB
7290
7291 /* There's only ever one segment override allowed per instruction.
7292 This instruction possibly has a legal segment override on the
7293 second operand, so copy the segment to where non-string
7294 instructions store it, allowing common code. */
7295 i.seg[op] = i.seg[1];
7296
29b0f896
AM
7297 return 1;
7298}
7299
7300static int
543613e9 7301process_suffix (void)
29b0f896 7302{
7fc69528 7303 bool is_movx = false;
8b65b895 7304
29b0f896
AM
7305 /* If matched instruction specifies an explicit instruction mnemonic
7306 suffix, use it. */
673fe0f0 7307 if (i.tm.opcode_modifier.size == SIZE16)
40fb9820 7308 i.suffix = WORD_MNEM_SUFFIX;
673fe0f0 7309 else if (i.tm.opcode_modifier.size == SIZE32)
40fb9820 7310 i.suffix = LONG_MNEM_SUFFIX;
673fe0f0 7311 else if (i.tm.opcode_modifier.size == SIZE64)
40fb9820 7312 i.suffix = QWORD_MNEM_SUFFIX;
13e600d0 7313 else if (i.reg_operands
c8f8eebc 7314 && (i.operands > 1 || i.types[0].bitfield.class == Reg)
255571cd 7315 && i.tm.opcode_modifier.operandconstraint != ADDR_PREFIX_OP_REG)
29b0f896 7316 {
65fca059 7317 unsigned int numop = i.operands;
389d00a5
JB
7318
7319 /* MOVSX/MOVZX */
ddb62495 7320 is_movx = (i.tm.opcode_space == SPACE_0F
389d00a5 7321 && (i.tm.base_opcode | 8) == 0xbe)
ddb62495 7322 || (i.tm.opcode_space == SPACE_BASE
389d00a5
JB
7323 && i.tm.base_opcode == 0x63
7324 && i.tm.cpu_flags.bitfield.cpu64);
7325
65fca059
JB
7326 /* movsx/movzx want only their source operand considered here, for the
7327 ambiguity checking below. The suffix will be replaced afterwards
7328 to represent the destination (register). */
389d00a5 7329 if (is_movx && (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63))
65fca059
JB
7330 --i.operands;
7331
643bb870 7332 /* crc32 needs REX.W set regardless of suffix / source operand size. */
7fc69528 7333 if (i.tm.mnem_off == MN_crc32 && i.tm.operand_types[1].bitfield.qword)
643bb870
JB
7334 i.rex |= REX_W;
7335
29b0f896 7336 /* If there's no instruction mnemonic suffix we try to invent one
13e600d0 7337 based on GPR operands. */
29b0f896
AM
7338 if (!i.suffix)
7339 {
7340 /* We take i.suffix from the last register operand specified,
7341 Destination register type is more significant than source
381d071f
L
7342 register type. crc32 in SSE4.2 prefers source register
7343 type. */
7fc69528 7344 unsigned int op = i.tm.mnem_off == MN_crc32 ? 1 : i.operands;
20592a94 7345
1a035124
JB
7346 while (op--)
7347 if (i.tm.operand_types[op].bitfield.instance == InstanceNone
7348 || i.tm.operand_types[op].bitfield.instance == Accum)
7349 {
7350 if (i.types[op].bitfield.class != Reg)
7351 continue;
7352 if (i.types[op].bitfield.byte)
7353 i.suffix = BYTE_MNEM_SUFFIX;
7354 else if (i.types[op].bitfield.word)
7355 i.suffix = WORD_MNEM_SUFFIX;
7356 else if (i.types[op].bitfield.dword)
7357 i.suffix = LONG_MNEM_SUFFIX;
7358 else if (i.types[op].bitfield.qword)
7359 i.suffix = QWORD_MNEM_SUFFIX;
7360 else
7361 continue;
7362 break;
7363 }
65fca059
JB
7364
7365 /* As an exception, movsx/movzx silently default to a byte source
7366 in AT&T mode. */
389d00a5 7367 if (is_movx && i.tm.opcode_modifier.w && !i.suffix && !intel_syntax)
65fca059 7368 i.suffix = BYTE_MNEM_SUFFIX;
29b0f896
AM
7369 }
7370 else if (i.suffix == BYTE_MNEM_SUFFIX)
7371 {
1cb0ab18 7372 if (!check_byte_reg ())
29b0f896
AM
7373 return 0;
7374 }
7375 else if (i.suffix == LONG_MNEM_SUFFIX)
7376 {
1cb0ab18 7377 if (!check_long_reg ())
29b0f896
AM
7378 return 0;
7379 }
7380 else if (i.suffix == QWORD_MNEM_SUFFIX)
7381 {
1cb0ab18 7382 if (!check_qword_reg ())
29b0f896
AM
7383 return 0;
7384 }
7385 else if (i.suffix == WORD_MNEM_SUFFIX)
7386 {
1cb0ab18 7387 if (!check_word_reg ())
29b0f896
AM
7388 return 0;
7389 }
3cd7f3e3
L
7390 else if (intel_syntax
7391 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
7392 /* Do nothing if the instruction is going to ignore the prefix. */
7393 ;
7394 else
7395 abort ();
65fca059
JB
7396
7397 /* Undo the movsx/movzx change done above. */
7398 i.operands = numop;
29b0f896 7399 }
3cd7f3e3
L
7400 else if (i.tm.opcode_modifier.mnemonicsize == DEFAULTSIZE
7401 && !i.suffix)
29b0f896 7402 {
13e600d0
JB
7403 i.suffix = stackop_size;
7404 if (stackop_size == LONG_MNEM_SUFFIX)
06f74c5c
L
7405 {
7406 /* stackop_size is set to LONG_MNEM_SUFFIX for the
7407 .code16gcc directive to support 16-bit mode with
7408 32-bit address. For IRET without a suffix, generate
7409 16-bit IRET (opcode 0xcf) to return from an interrupt
7410 handler. */
13e600d0
JB
7411 if (i.tm.base_opcode == 0xcf)
7412 {
7413 i.suffix = WORD_MNEM_SUFFIX;
7414 as_warn (_("generating 16-bit `iret' for .code16gcc directive"));
7415 }
7416 /* Warn about changed behavior for segment register push/pop. */
7417 else if ((i.tm.base_opcode | 1) == 0x07)
7418 as_warn (_("generating 32-bit `%s', unlike earlier gas versions"),
76d3f746 7419 insn_name (&i.tm));
06f74c5c 7420 }
29b0f896 7421 }
c006a730 7422 else if (!i.suffix
0cfa3eb3
JB
7423 && (i.tm.opcode_modifier.jump == JUMP_ABSOLUTE
7424 || i.tm.opcode_modifier.jump == JUMP_BYTE
7425 || i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT
ddb62495 7426 || (i.tm.opcode_space == SPACE_0F
389d00a5 7427 && i.tm.base_opcode == 0x01 /* [ls][gi]dt */
64e74474 7428 && i.tm.extension_opcode <= 3)))
9306ca4a
JB
7429 {
7430 switch (flag_code)
7431 {
7432 case CODE_64BIT:
40fb9820 7433 if (!i.tm.opcode_modifier.no_qsuf)
9306ca4a 7434 {
828c2a25
JB
7435 if (i.tm.opcode_modifier.jump == JUMP_BYTE
7436 || i.tm.opcode_modifier.no_lsuf)
7437 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a
JB
7438 break;
7439 }
1a0670f3 7440 /* Fall through. */
9306ca4a 7441 case CODE_32BIT:
40fb9820 7442 if (!i.tm.opcode_modifier.no_lsuf)
9306ca4a
JB
7443 i.suffix = LONG_MNEM_SUFFIX;
7444 break;
7445 case CODE_16BIT:
40fb9820 7446 if (!i.tm.opcode_modifier.no_wsuf)
9306ca4a
JB
7447 i.suffix = WORD_MNEM_SUFFIX;
7448 break;
7449 }
7450 }
252b5132 7451
c006a730 7452 if (!i.suffix
3cd7f3e3 7453 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8
JB
7454 /* Also cover lret/retf/iret in 64-bit mode. */
7455 || (flag_code == CODE_64BIT
7456 && !i.tm.opcode_modifier.no_lsuf
7457 && !i.tm.opcode_modifier.no_qsuf))
3cd7f3e3 7458 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
8bbb3ad8
JB
7459 /* Explicit sizing prefixes are assumed to disambiguate insns. */
7460 && !i.prefix[DATA_PREFIX] && !(i.prefix[REX_PREFIX] & REX_W)
62b3f548
JB
7461 /* Accept FLDENV et al without suffix. */
7462 && (i.tm.opcode_modifier.no_ssuf || i.tm.opcode_modifier.floatmf))
29b0f896 7463 {
6c0946d0 7464 unsigned int suffixes, evex = 0;
c006a730
JB
7465
7466 suffixes = !i.tm.opcode_modifier.no_bsuf;
7467 if (!i.tm.opcode_modifier.no_wsuf)
7468 suffixes |= 1 << 1;
7469 if (!i.tm.opcode_modifier.no_lsuf)
7470 suffixes |= 1 << 2;
c006a730
JB
7471 if (!i.tm.opcode_modifier.no_ssuf)
7472 suffixes |= 1 << 4;
7473 if (flag_code == CODE_64BIT && !i.tm.opcode_modifier.no_qsuf)
7474 suffixes |= 1 << 5;
7475
6c0946d0
JB
7476 /* For [XYZ]MMWORD operands inspect operand sizes. While generally
7477 also suitable for AT&T syntax mode, it was requested that this be
7478 restricted to just Intel syntax. */
a5748e0d
JB
7479 if (intel_syntax && is_any_vex_encoding (&i.tm)
7480 && !i.broadcast.type && !i.broadcast.bytes)
6c0946d0 7481 {
b9915cbc 7482 unsigned int op;
6c0946d0 7483
b9915cbc 7484 for (op = 0; op < i.tm.operands; ++op)
6c0946d0 7485 {
b9915cbc
JB
7486 if (is_evex_encoding (&i.tm)
7487 && !cpu_arch_flags.bitfield.cpuavx512vl)
6c0946d0 7488 {
b9915cbc
JB
7489 if (i.tm.operand_types[op].bitfield.ymmword)
7490 i.tm.operand_types[op].bitfield.xmmword = 0;
7491 if (i.tm.operand_types[op].bitfield.zmmword)
7492 i.tm.operand_types[op].bitfield.ymmword = 0;
7493 if (!i.tm.opcode_modifier.evex
7494 || i.tm.opcode_modifier.evex == EVEXDYN)
7495 i.tm.opcode_modifier.evex = EVEX512;
7496 }
6c0946d0 7497
b9915cbc
JB
7498 if (i.tm.operand_types[op].bitfield.xmmword
7499 + i.tm.operand_types[op].bitfield.ymmword
7500 + i.tm.operand_types[op].bitfield.zmmword < 2)
7501 continue;
6c0946d0 7502
b9915cbc
JB
7503 /* Any properly sized operand disambiguates the insn. */
7504 if (i.types[op].bitfield.xmmword
7505 || i.types[op].bitfield.ymmword
7506 || i.types[op].bitfield.zmmword)
7507 {
7508 suffixes &= ~(7 << 6);
7509 evex = 0;
7510 break;
7511 }
6c0946d0 7512
b9915cbc
JB
7513 if ((i.flags[op] & Operand_Mem)
7514 && i.tm.operand_types[op].bitfield.unspecified)
7515 {
7516 if (i.tm.operand_types[op].bitfield.xmmword)
7517 suffixes |= 1 << 6;
7518 if (i.tm.operand_types[op].bitfield.ymmword)
7519 suffixes |= 1 << 7;
7520 if (i.tm.operand_types[op].bitfield.zmmword)
7521 suffixes |= 1 << 8;
7522 if (is_evex_encoding (&i.tm))
7523 evex = EVEX512;
6c0946d0
JB
7524 }
7525 }
7526 }
7527
7528 /* Are multiple suffixes / operand sizes allowed? */
c006a730 7529 if (suffixes & (suffixes - 1))
9306ca4a 7530 {
873494c8 7531 if (intel_syntax
3cd7f3e3 7532 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8 7533 || operand_check == check_error))
9306ca4a 7534 {
76d3f746 7535 as_bad (_("ambiguous operand size for `%s'"), insn_name (&i.tm));
9306ca4a
JB
7536 return 0;
7537 }
c006a730 7538 if (operand_check == check_error)
9306ca4a 7539 {
c006a730 7540 as_bad (_("no instruction mnemonic suffix given and "
76d3f746 7541 "no register operands; can't size `%s'"), insn_name (&i.tm));
9306ca4a
JB
7542 return 0;
7543 }
c006a730 7544 if (operand_check == check_warning)
873494c8
JB
7545 as_warn (_("%s; using default for `%s'"),
7546 intel_syntax
7547 ? _("ambiguous operand size")
7548 : _("no instruction mnemonic suffix given and "
7549 "no register operands"),
76d3f746 7550 insn_name (&i.tm));
c006a730
JB
7551
7552 if (i.tm.opcode_modifier.floatmf)
7553 i.suffix = SHORT_MNEM_SUFFIX;
389d00a5 7554 else if (is_movx)
65fca059 7555 /* handled below */;
6c0946d0
JB
7556 else if (evex)
7557 i.tm.opcode_modifier.evex = evex;
c006a730
JB
7558 else if (flag_code == CODE_16BIT)
7559 i.suffix = WORD_MNEM_SUFFIX;
1a035124 7560 else if (!i.tm.opcode_modifier.no_lsuf)
c006a730 7561 i.suffix = LONG_MNEM_SUFFIX;
1a035124
JB
7562 else
7563 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a 7564 }
29b0f896 7565 }
252b5132 7566
389d00a5 7567 if (is_movx)
65fca059
JB
7568 {
7569 /* In Intel syntax, movsx/movzx must have a "suffix" (checked above).
7570 In AT&T syntax, if there is no suffix (warned about above), the default
7571 will be byte extension. */
7572 if (i.tm.opcode_modifier.w && i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
7573 i.tm.base_opcode |= 1;
7574
7575 /* For further processing, the suffix should represent the destination
7576 (register). This is already the case when one was used with
7577 mov[sz][bw]*, but we need to replace it for mov[sz]x, or if there was
7578 no suffix to begin with. */
7579 if (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63 || !i.suffix)
7580 {
7581 if (i.types[1].bitfield.word)
7582 i.suffix = WORD_MNEM_SUFFIX;
7583 else if (i.types[1].bitfield.qword)
7584 i.suffix = QWORD_MNEM_SUFFIX;
7585 else
7586 i.suffix = LONG_MNEM_SUFFIX;
7587
7588 i.tm.opcode_modifier.w = 0;
7589 }
7590 }
7591
50128d0c
JB
7592 if (!i.tm.opcode_modifier.modrm && i.reg_operands && i.tm.operands < 3)
7593 i.short_form = (i.tm.operand_types[0].bitfield.class == Reg)
7594 != (i.tm.operand_types[1].bitfield.class == Reg);
7595
d2224064
JB
7596 /* Change the opcode based on the operand size given by i.suffix. */
7597 switch (i.suffix)
29b0f896 7598 {
d2224064
JB
7599 /* Size floating point instruction. */
7600 case LONG_MNEM_SUFFIX:
7601 if (i.tm.opcode_modifier.floatmf)
7602 {
7603 i.tm.base_opcode ^= 4;
7604 break;
7605 }
7606 /* fall through */
7607 case WORD_MNEM_SUFFIX:
7608 case QWORD_MNEM_SUFFIX:
29b0f896 7609 /* It's not a byte, select word/dword operation. */
40fb9820 7610 if (i.tm.opcode_modifier.w)
29b0f896 7611 {
50128d0c 7612 if (i.short_form)
29b0f896
AM
7613 i.tm.base_opcode |= 8;
7614 else
7615 i.tm.base_opcode |= 1;
7616 }
d2224064
JB
7617 /* fall through */
7618 case SHORT_MNEM_SUFFIX:
29b0f896
AM
7619 /* Now select between word & dword operations via the operand
7620 size prefix, except for instructions that will ignore this
7621 prefix anyway. */
c8f8eebc 7622 if (i.suffix != QWORD_MNEM_SUFFIX
3cd7f3e3 7623 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
c8f8eebc
JB
7624 && !i.tm.opcode_modifier.floatmf
7625 && !is_any_vex_encoding (&i.tm)
7626 && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
7627 || (flag_code == CODE_64BIT
7628 && i.tm.opcode_modifier.jump == JUMP_BYTE)))
24eab124
AM
7629 {
7630 unsigned int prefix = DATA_PREFIX_OPCODE;
543613e9 7631
0cfa3eb3 7632 if (i.tm.opcode_modifier.jump == JUMP_BYTE) /* jcxz, loop */
29b0f896 7633 prefix = ADDR_PREFIX_OPCODE;
252b5132 7634
29b0f896
AM
7635 if (!add_prefix (prefix))
7636 return 0;
24eab124 7637 }
252b5132 7638
29b0f896
AM
7639 /* Set mode64 for an operand. */
7640 if (i.suffix == QWORD_MNEM_SUFFIX
9146926a 7641 && flag_code == CODE_64BIT
d2224064 7642 && !i.tm.opcode_modifier.norex64
4ed21b58 7643 && !i.tm.opcode_modifier.vexw
46e883c5 7644 /* Special case for xchg %rax,%rax. It is NOP and doesn't
d2224064
JB
7645 need rex64. */
7646 && ! (i.operands == 2
7647 && i.tm.base_opcode == 0x90
ddb62495 7648 && i.tm.opcode_space == SPACE_BASE
75e5731b
JB
7649 && i.types[0].bitfield.instance == Accum
7650 && i.types[0].bitfield.qword
44732014 7651 && i.types[1].bitfield.instance == Accum))
d2224064 7652 i.rex |= REX_W;
3e73aa7c 7653
d2224064 7654 break;
8bbb3ad8
JB
7655
7656 case 0:
f9a6a8f0 7657 /* Select word/dword/qword operation with explicit data sizing prefix
8bbb3ad8
JB
7658 when there are no suitable register operands. */
7659 if (i.tm.opcode_modifier.w
7660 && (i.prefix[DATA_PREFIX] || (i.prefix[REX_PREFIX] & REX_W))
7661 && (!i.reg_operands
7662 || (i.reg_operands == 1
7663 /* ShiftCount */
7664 && (i.tm.operand_types[0].bitfield.instance == RegC
7665 /* InOutPortReg */
7666 || i.tm.operand_types[0].bitfield.instance == RegD
7667 || i.tm.operand_types[1].bitfield.instance == RegD
7fc69528 7668 || i.tm.mnem_off == MN_crc32))))
8bbb3ad8
JB
7669 i.tm.base_opcode |= 1;
7670 break;
29b0f896 7671 }
7ecd2f8b 7672
255571cd 7673 if (i.tm.opcode_modifier.operandconstraint == ADDR_PREFIX_OP_REG)
c0a30a9f 7674 {
c8f8eebc
JB
7675 gas_assert (!i.suffix);
7676 gas_assert (i.reg_operands);
c0a30a9f 7677
c8f8eebc
JB
7678 if (i.tm.operand_types[0].bitfield.instance == Accum
7679 || i.operands == 1)
7680 {
7681 /* The address size override prefix changes the size of the
7682 first operand. */
7683 if (flag_code == CODE_64BIT
7684 && i.op[0].regs->reg_type.bitfield.word)
7685 {
7686 as_bad (_("16-bit addressing unavailable for `%s'"),
76d3f746 7687 insn_name (&i.tm));
c8f8eebc
JB
7688 return 0;
7689 }
7690
7691 if ((flag_code == CODE_32BIT
7692 ? i.op[0].regs->reg_type.bitfield.word
7693 : i.op[0].regs->reg_type.bitfield.dword)
7694 && !add_prefix (ADDR_PREFIX_OPCODE))
7695 return 0;
7696 }
c0a30a9f
L
7697 else
7698 {
c8f8eebc
JB
7699 /* Check invalid register operand when the address size override
7700 prefix changes the size of register operands. */
7701 unsigned int op;
7702 enum { need_word, need_dword, need_qword } need;
7703
27f13469 7704 /* Check the register operand for the address size prefix if
b3a3496f 7705 the memory operand has no real registers, like symbol, DISP
829f3fe1 7706 or bogus (x32-only) symbol(%rip) when symbol(%eip) is meant. */
27f13469
L
7707 if (i.mem_operands == 1
7708 && i.reg_operands == 1
7709 && i.operands == 2
27f13469 7710 && i.types[1].bitfield.class == Reg
b3a3496f
L
7711 && (flag_code == CODE_32BIT
7712 ? i.op[1].regs->reg_type.bitfield.word
7713 : i.op[1].regs->reg_type.bitfield.dword)
7714 && ((i.base_reg == NULL && i.index_reg == NULL)
829f3fe1
JB
7715#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
7716 || (x86_elf_abi == X86_64_X32_ABI
7717 && i.base_reg
b3a3496f
L
7718 && i.base_reg->reg_num == RegIP
7719 && i.base_reg->reg_type.bitfield.qword))
829f3fe1
JB
7720#else
7721 || 0)
7722#endif
27f13469
L
7723 && !add_prefix (ADDR_PREFIX_OPCODE))
7724 return 0;
7725
c8f8eebc
JB
7726 if (flag_code == CODE_32BIT)
7727 need = i.prefix[ADDR_PREFIX] ? need_word : need_dword;
7728 else if (i.prefix[ADDR_PREFIX])
c0a30a9f
L
7729 need = need_dword;
7730 else
7731 need = flag_code == CODE_64BIT ? need_qword : need_word;
c0a30a9f 7732
c8f8eebc
JB
7733 for (op = 0; op < i.operands; op++)
7734 {
7735 if (i.types[op].bitfield.class != Reg)
7736 continue;
7737
7738 switch (need)
7739 {
7740 case need_word:
7741 if (i.op[op].regs->reg_type.bitfield.word)
7742 continue;
7743 break;
7744 case need_dword:
7745 if (i.op[op].regs->reg_type.bitfield.dword)
7746 continue;
7747 break;
7748 case need_qword:
7749 if (i.op[op].regs->reg_type.bitfield.qword)
7750 continue;
7751 break;
7752 }
7753
7754 as_bad (_("invalid register operand size for `%s'"),
76d3f746 7755 insn_name (&i.tm));
c8f8eebc
JB
7756 return 0;
7757 }
7758 }
c0a30a9f
L
7759 }
7760
29b0f896
AM
7761 return 1;
7762}
3e73aa7c 7763
29b0f896 7764static int
543613e9 7765check_byte_reg (void)
29b0f896
AM
7766{
7767 int op;
543613e9 7768
29b0f896
AM
7769 for (op = i.operands; --op >= 0;)
7770 {
dc821c5f 7771 /* Skip non-register operands. */
bab6aec1 7772 if (i.types[op].bitfield.class != Reg)
dc821c5f
JB
7773 continue;
7774
29b0f896
AM
7775 /* If this is an eight bit register, it's OK. If it's the 16 or
7776 32 bit version of an eight bit register, we will just use the
7777 low portion, and that's OK too. */
dc821c5f 7778 if (i.types[op].bitfield.byte)
29b0f896
AM
7779 continue;
7780
5a819eb9 7781 /* I/O port address operands are OK too. */
75e5731b
JB
7782 if (i.tm.operand_types[op].bitfield.instance == RegD
7783 && i.tm.operand_types[op].bitfield.word)
5a819eb9
JB
7784 continue;
7785
9706160a 7786 /* crc32 only wants its source operand checked here. */
7fc69528 7787 if (i.tm.mnem_off == MN_crc32 && op != 0)
9344ff29
L
7788 continue;
7789
29b0f896 7790 /* Any other register is bad. */
73c76375
JB
7791 as_bad (_("`%s%s' not allowed with `%s%c'"),
7792 register_prefix, i.op[op].regs->reg_name,
76d3f746 7793 insn_name (&i.tm), i.suffix);
73c76375 7794 return 0;
29b0f896
AM
7795 }
7796 return 1;
7797}
7798
7799static int
e3bb37b5 7800check_long_reg (void)
29b0f896
AM
7801{
7802 int op;
7803
7804 for (op = i.operands; --op >= 0;)
dc821c5f 7805 /* Skip non-register operands. */
bab6aec1 7806 if (i.types[op].bitfield.class != Reg)
dc821c5f 7807 continue;
29b0f896
AM
7808 /* Reject eight bit registers, except where the template requires
7809 them. (eg. movzb) */
dc821c5f 7810 else if (i.types[op].bitfield.byte
bab6aec1 7811 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7812 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7813 && (i.tm.operand_types[op].bitfield.word
7814 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7815 {
a540244d
L
7816 as_bad (_("`%s%s' not allowed with `%s%c'"),
7817 register_prefix,
29b0f896 7818 i.op[op].regs->reg_name,
76d3f746 7819 insn_name (&i.tm),
29b0f896
AM
7820 i.suffix);
7821 return 0;
7822 }
be4c5e58
L
7823 /* Error if the e prefix on a general reg is missing. */
7824 else if (i.types[op].bitfield.word
bab6aec1 7825 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7826 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7827 && i.tm.operand_types[op].bitfield.dword)
29b0f896 7828 {
be4c5e58
L
7829 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7830 register_prefix, i.op[op].regs->reg_name,
7831 i.suffix);
7832 return 0;
252b5132 7833 }
e4630f71 7834 /* Warn if the r prefix on a general reg is present. */
dc821c5f 7835 else if (i.types[op].bitfield.qword
bab6aec1 7836 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7837 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7838 && i.tm.operand_types[op].bitfield.dword)
252b5132 7839 {
1cb0ab18
JB
7840 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7841 register_prefix, i.op[op].regs->reg_name, i.suffix);
7842 return 0;
29b0f896
AM
7843 }
7844 return 1;
7845}
252b5132 7846
29b0f896 7847static int
e3bb37b5 7848check_qword_reg (void)
29b0f896
AM
7849{
7850 int op;
252b5132 7851
29b0f896 7852 for (op = i.operands; --op >= 0; )
dc821c5f 7853 /* Skip non-register operands. */
bab6aec1 7854 if (i.types[op].bitfield.class != Reg)
dc821c5f 7855 continue;
29b0f896
AM
7856 /* Reject eight bit registers, except where the template requires
7857 them. (eg. movzb) */
dc821c5f 7858 else if (i.types[op].bitfield.byte
bab6aec1 7859 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7860 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7861 && (i.tm.operand_types[op].bitfield.word
7862 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7863 {
a540244d
L
7864 as_bad (_("`%s%s' not allowed with `%s%c'"),
7865 register_prefix,
29b0f896 7866 i.op[op].regs->reg_name,
76d3f746 7867 insn_name (&i.tm),
29b0f896
AM
7868 i.suffix);
7869 return 0;
7870 }
e4630f71 7871 /* Warn if the r prefix on a general reg is missing. */
dc821c5f
JB
7872 else if ((i.types[op].bitfield.word
7873 || i.types[op].bitfield.dword)
bab6aec1 7874 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7875 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7876 && i.tm.operand_types[op].bitfield.qword)
29b0f896
AM
7877 {
7878 /* Prohibit these changes in the 64bit mode, since the
7879 lowering is more complicated. */
1cb0ab18
JB
7880 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7881 register_prefix, i.op[op].regs->reg_name, i.suffix);
7882 return 0;
252b5132 7883 }
29b0f896
AM
7884 return 1;
7885}
252b5132 7886
29b0f896 7887static int
e3bb37b5 7888check_word_reg (void)
29b0f896
AM
7889{
7890 int op;
7891 for (op = i.operands; --op >= 0;)
dc821c5f 7892 /* Skip non-register operands. */
bab6aec1 7893 if (i.types[op].bitfield.class != Reg)
dc821c5f 7894 continue;
29b0f896
AM
7895 /* Reject eight bit registers, except where the template requires
7896 them. (eg. movzb) */
dc821c5f 7897 else if (i.types[op].bitfield.byte
bab6aec1 7898 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7899 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7900 && (i.tm.operand_types[op].bitfield.word
7901 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7902 {
a540244d
L
7903 as_bad (_("`%s%s' not allowed with `%s%c'"),
7904 register_prefix,
29b0f896 7905 i.op[op].regs->reg_name,
76d3f746 7906 insn_name (&i.tm),
29b0f896
AM
7907 i.suffix);
7908 return 0;
7909 }
9706160a
JB
7910 /* Error if the e or r prefix on a general reg is present. */
7911 else if ((i.types[op].bitfield.dword
dc821c5f 7912 || i.types[op].bitfield.qword)
bab6aec1 7913 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7914 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7915 && i.tm.operand_types[op].bitfield.word)
252b5132 7916 {
9706160a
JB
7917 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7918 register_prefix, i.op[op].regs->reg_name,
7919 i.suffix);
7920 return 0;
29b0f896
AM
7921 }
7922 return 1;
7923}
252b5132 7924
29b0f896 7925static int
40fb9820 7926update_imm (unsigned int j)
29b0f896 7927{
bc0844ae 7928 i386_operand_type overlap = i.types[j];
c34d1cc9
JB
7929
7930 if (i.tm.operand_types[j].bitfield.imm8
7931 && i.tm.operand_types[j].bitfield.imm8s
7932 && overlap.bitfield.imm8 && overlap.bitfield.imm8s)
7933 {
7934 /* This combination is used on 8-bit immediates where e.g. $~0 is
7935 desirable to permit. We're past operand type matching, so simply
7936 put things back in the shape they were before introducing the
7937 distinction between Imm8, Imm8S, and Imm8|Imm8S. */
7938 overlap.bitfield.imm8s = 0;
7939 }
7940
be1643ff
JB
7941 if (overlap.bitfield.imm8
7942 + overlap.bitfield.imm8s
7943 + overlap.bitfield.imm16
7944 + overlap.bitfield.imm32
7945 + overlap.bitfield.imm32s
7946 + overlap.bitfield.imm64 > 1)
29b0f896 7947 {
05909f23
JB
7948 static const i386_operand_type imm16 = { .bitfield = { .imm16 = 1 } };
7949 static const i386_operand_type imm32 = { .bitfield = { .imm32 = 1 } };
7950 static const i386_operand_type imm32s = { .bitfield = { .imm32s = 1 } };
7951 static const i386_operand_type imm16_32 = { .bitfield =
7952 { .imm16 = 1, .imm32 = 1 }
7953 };
7954 static const i386_operand_type imm16_32s = { .bitfield =
7955 { .imm16 = 1, .imm32s = 1 }
7956 };
7957 static const i386_operand_type imm16_32_32s = { .bitfield =
7958 { .imm16 = 1, .imm32 = 1, .imm32s = 1 }
7959 };
7960
29b0f896
AM
7961 if (i.suffix)
7962 {
40fb9820
L
7963 i386_operand_type temp;
7964
0dfbf9d7 7965 operand_type_set (&temp, 0);
7ab9ffdd 7966 if (i.suffix == BYTE_MNEM_SUFFIX)
40fb9820
L
7967 {
7968 temp.bitfield.imm8 = overlap.bitfield.imm8;
7969 temp.bitfield.imm8s = overlap.bitfield.imm8s;
7970 }
7971 else if (i.suffix == WORD_MNEM_SUFFIX)
7972 temp.bitfield.imm16 = overlap.bitfield.imm16;
7973 else if (i.suffix == QWORD_MNEM_SUFFIX)
7974 {
7975 temp.bitfield.imm64 = overlap.bitfield.imm64;
7976 temp.bitfield.imm32s = overlap.bitfield.imm32s;
7977 }
7978 else
7979 temp.bitfield.imm32 = overlap.bitfield.imm32;
7980 overlap = temp;
29b0f896 7981 }
0dfbf9d7
L
7982 else if (operand_type_equal (&overlap, &imm16_32_32s)
7983 || operand_type_equal (&overlap, &imm16_32)
7984 || operand_type_equal (&overlap, &imm16_32s))
29b0f896 7985 {
40fb9820 7986 if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
65da13b5 7987 overlap = imm16;
40fb9820 7988 else
65da13b5 7989 overlap = imm32s;
29b0f896 7990 }
8bbb3ad8
JB
7991 else if (i.prefix[REX_PREFIX] & REX_W)
7992 overlap = operand_type_and (overlap, imm32s);
7993 else if (i.prefix[DATA_PREFIX])
7994 overlap = operand_type_and (overlap,
7995 flag_code != CODE_16BIT ? imm16 : imm32);
be1643ff
JB
7996 if (overlap.bitfield.imm8
7997 + overlap.bitfield.imm8s
7998 + overlap.bitfield.imm16
7999 + overlap.bitfield.imm32
8000 + overlap.bitfield.imm32s
8001 + overlap.bitfield.imm64 != 1)
29b0f896 8002 {
4eed87de
AM
8003 as_bad (_("no instruction mnemonic suffix given; "
8004 "can't determine immediate size"));
29b0f896
AM
8005 return 0;
8006 }
8007 }
40fb9820 8008 i.types[j] = overlap;
29b0f896 8009
40fb9820
L
8010 return 1;
8011}
8012
8013static int
8014finalize_imm (void)
8015{
bc0844ae 8016 unsigned int j, n;
29b0f896 8017
bc0844ae
L
8018 /* Update the first 2 immediate operands. */
8019 n = i.operands > 2 ? 2 : i.operands;
8020 if (n)
8021 {
8022 for (j = 0; j < n; j++)
8023 if (update_imm (j) == 0)
8024 return 0;
40fb9820 8025
bc0844ae
L
8026 /* The 3rd operand can't be immediate operand. */
8027 gas_assert (operand_type_check (i.types[2], imm) == 0);
8028 }
29b0f896
AM
8029
8030 return 1;
8031}
8032
0a3eba42
JB
8033static INLINE void set_rex_vrex (const reg_entry *r, unsigned int rex_bit,
8034 bool do_sse2avx)
8035{
8036 if (r->reg_flags & RegRex)
8037 {
8038 if (i.rex & rex_bit)
8039 as_bad (_("same type of prefix used twice"));
8040 i.rex |= rex_bit;
8041 }
8042 else if (do_sse2avx && (i.rex & rex_bit) && i.vex.register_specifier)
8043 {
8044 gas_assert (i.vex.register_specifier == r);
8045 i.vex.register_specifier += 8;
8046 }
8047
8048 if (r->reg_flags & RegVRex)
8049 i.vrex |= rex_bit;
8050}
8051
29b0f896 8052static int
e3bb37b5 8053process_operands (void)
29b0f896
AM
8054{
8055 /* Default segment register this instruction will use for memory
8056 accesses. 0 means unknown. This is only for optimizing out
8057 unnecessary segment overrides. */
5e042380 8058 const reg_entry *default_seg = NULL;
29b0f896 8059
31b4cda3
JB
8060 /* We only need to check those implicit registers for instructions
8061 with 3 operands or less. */
8062 if (i.operands <= 3)
8063 for (unsigned int j = 0; j < i.operands; j++)
8064 if (i.types[j].bitfield.instance != InstanceNone)
8065 i.reg_operands--;
8066
a5aeccd9
JB
8067 if (i.tm.opcode_modifier.sse2avx)
8068 {
8069 /* Legacy encoded insns allow explicit REX prefixes, so these prefixes
8070 need converting. */
8071 i.rex |= i.prefix[REX_PREFIX] & (REX_W | REX_R | REX_X | REX_B);
8072 i.prefix[REX_PREFIX] = 0;
8073 i.rex_encoding = 0;
8074 }
c423d21a
JB
8075 /* ImmExt should be processed after SSE2AVX. */
8076 else if (i.tm.opcode_modifier.immext)
8077 process_immext ();
a5aeccd9 8078
ecb96e55
JB
8079 /* TILEZERO is unusual in that it has a single operand encoded in ModR/M.reg,
8080 not ModR/M.rm. To avoid special casing this in build_modrm_byte(), fake a
8081 new destination operand here, while converting the source one to register
8082 number 0. */
8083 if (i.tm.mnem_off == MN_tilezero)
8084 {
8085 i.op[1].regs = i.op[0].regs;
8086 i.op[0].regs -= i.op[0].regs->reg_num;
8087 i.types[1] = i.types[0];
8088 i.tm.operand_types[1] = i.tm.operand_types[0];
8089 i.flags[1] = i.flags[0];
8090 i.operands++;
8091 i.reg_operands++;
8092 i.tm.operands++;
8093 }
8094
2426c15f 8095 if (i.tm.opcode_modifier.sse2avx && i.tm.opcode_modifier.vexvvvv)
29b0f896 8096 {
05909f23
JB
8097 static const i386_operand_type regxmm = {
8098 .bitfield = { .class = RegSIMD, .xmmword = 1 }
8099 };
91d6fa6a
NC
8100 unsigned int dupl = i.operands;
8101 unsigned int dest = dupl - 1;
9fcfb3d7
L
8102 unsigned int j;
8103
c0f3af97 8104 /* The destination must be an xmm register. */
9c2799c2 8105 gas_assert (i.reg_operands
91d6fa6a 8106 && MAX_OPERANDS > dupl
7ab9ffdd 8107 && operand_type_equal (&i.types[dest], &regxmm));
c0f3af97 8108
75e5731b 8109 if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 8110 && i.tm.operand_types[0].bitfield.xmmword)
e2ec9d29 8111 {
95dfdd85
JB
8112 /* Keep xmm0 for instructions with VEX prefix and 3
8113 sources. */
8114 i.tm.operand_types[0].bitfield.instance = InstanceNone;
8115 i.tm.operand_types[0].bitfield.class = RegSIMD;
31b4cda3 8116 i.reg_operands++;
95dfdd85 8117 goto duplicate;
c0f3af97 8118 }
95dfdd85
JB
8119
8120 if (i.tm.opcode_modifier.operandconstraint == IMPLICIT_1ST_XMM0)
7ab9ffdd 8121 {
aa180741 8122 gas_assert ((MAX_OPERANDS - 1) > dupl);
c0f3af97
L
8123
8124 /* Add the implicit xmm0 for instructions with VEX prefix
8125 and 3 sources. */
8126 for (j = i.operands; j > 0; j--)
8127 {
8128 i.op[j] = i.op[j - 1];
8129 i.types[j] = i.types[j - 1];
8130 i.tm.operand_types[j] = i.tm.operand_types[j - 1];
8dc0818e 8131 i.flags[j] = i.flags[j - 1];
c0f3af97
L
8132 }
8133 i.op[0].regs
629310ab 8134 = (const reg_entry *) str_hash_find (reg_hash, "xmm0");
7ab9ffdd 8135 i.types[0] = regxmm;
c0f3af97
L
8136 i.tm.operand_types[0] = regxmm;
8137
8138 i.operands += 2;
8139 i.reg_operands += 2;
8140 i.tm.operands += 2;
8141
91d6fa6a 8142 dupl++;
c0f3af97 8143 dest++;
91d6fa6a
NC
8144 i.op[dupl] = i.op[dest];
8145 i.types[dupl] = i.types[dest];
8146 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 8147 i.flags[dupl] = i.flags[dest];
e2ec9d29 8148 }
c0f3af97
L
8149 else
8150 {
dc1e8a47 8151 duplicate:
c0f3af97
L
8152 i.operands++;
8153 i.reg_operands++;
8154 i.tm.operands++;
8155
91d6fa6a
NC
8156 i.op[dupl] = i.op[dest];
8157 i.types[dupl] = i.types[dest];
8158 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 8159 i.flags[dupl] = i.flags[dest];
c0f3af97
L
8160 }
8161
8162 if (i.tm.opcode_modifier.immext)
8163 process_immext ();
8164 }
75e5731b 8165 else if (i.tm.operand_types[0].bitfield.instance == Accum
bd782808 8166 && i.tm.opcode_modifier.modrm)
c0f3af97
L
8167 {
8168 unsigned int j;
8169
9fcfb3d7
L
8170 for (j = 1; j < i.operands; j++)
8171 {
8172 i.op[j - 1] = i.op[j];
8173 i.types[j - 1] = i.types[j];
8174
8175 /* We need to adjust fields in i.tm since they are used by
8176 build_modrm_byte. */
8177 i.tm.operand_types [j - 1] = i.tm.operand_types [j];
8dc0818e
JB
8178
8179 i.flags[j - 1] = i.flags[j];
9fcfb3d7
L
8180 }
8181
31b4cda3
JB
8182 /* No adjustment to i.reg_operands: This was already done at the top
8183 of the function. */
e2ec9d29 8184 i.operands--;
e2ec9d29
L
8185 i.tm.operands--;
8186 }
255571cd 8187 else if (i.tm.opcode_modifier.operandconstraint == IMPLICIT_QUAD_GROUP)
920d2ddc 8188 {
a477a8c4
JB
8189 unsigned int regnum, first_reg_in_group, last_reg_in_group;
8190
920d2ddc 8191 /* The second operand must be {x,y,z}mmN, where N is a multiple of 4. */
3528c362 8192 gas_assert (i.operands >= 2 && i.types[1].bitfield.class == RegSIMD);
a477a8c4
JB
8193 regnum = register_number (i.op[1].regs);
8194 first_reg_in_group = regnum & ~3;
8195 last_reg_in_group = first_reg_in_group + 3;
8196 if (regnum != first_reg_in_group)
8197 as_warn (_("source register `%s%s' implicitly denotes"
8198 " `%s%.3s%u' to `%s%.3s%u' source group in `%s'"),
8199 register_prefix, i.op[1].regs->reg_name,
8200 register_prefix, i.op[1].regs->reg_name, first_reg_in_group,
8201 register_prefix, i.op[1].regs->reg_name, last_reg_in_group,
76d3f746 8202 insn_name (&i.tm));
a477a8c4 8203 }
255571cd 8204 else if (i.tm.opcode_modifier.operandconstraint == REG_KLUDGE)
e2ec9d29
L
8205 {
8206 /* The imul $imm, %reg instruction is converted into
8207 imul $imm, %reg, %reg, and the clr %reg instruction
8208 is converted into xor %reg, %reg. */
8209
8210 unsigned int first_reg_op;
8211
8212 if (operand_type_check (i.types[0], reg))
8213 first_reg_op = 0;
8214 else
8215 first_reg_op = 1;
8216 /* Pretend we saw the extra register operand. */
9c2799c2 8217 gas_assert (i.reg_operands == 1
7ab9ffdd 8218 && i.op[first_reg_op + 1].regs == 0);
e2ec9d29
L
8219 i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
8220 i.types[first_reg_op + 1] = i.types[first_reg_op];
8221 i.operands++;
8222 i.reg_operands++;
29b0f896
AM
8223 }
8224
85b80b0f 8225 if (i.tm.opcode_modifier.modrm)
29b0f896
AM
8226 {
8227 /* The opcode is completed (modulo i.tm.extension_opcode which
52271982
AM
8228 must be put into the modrm byte). Now, we make the modrm and
8229 index base bytes based on all the info we've collected. */
29b0f896
AM
8230
8231 default_seg = build_modrm_byte ();
bd782808
JB
8232
8233 if (!quiet_warnings && i.tm.opcode_modifier.operandconstraint == UGH)
8234 {
8235 /* Warn about some common errors, but press on regardless. */
8236 if (i.operands == 2)
8237 {
8238 /* Reversed arguments on faddp or fmulp. */
8239 as_warn (_("translating to `%s %s%s,%s%s'"), insn_name (&i.tm),
8240 register_prefix, i.op[!intel_syntax].regs->reg_name,
8241 register_prefix, i.op[intel_syntax].regs->reg_name);
8242 }
8243 else if (i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
8244 {
8245 /* Extraneous `l' suffix on fp insn. */
8246 as_warn (_("translating to `%s %s%s'"), insn_name (&i.tm),
8247 register_prefix, i.op[0].regs->reg_name);
8248 }
8249 }
29b0f896 8250 }
0ff3b7d0 8251 else if (i.types[0].bitfield.class == SReg && !dot_insn ())
85b80b0f
JB
8252 {
8253 if (flag_code != CODE_64BIT
8254 ? i.tm.base_opcode == POP_SEG_SHORT
8255 && i.op[0].regs->reg_num == 1
389d00a5 8256 : (i.tm.base_opcode | 1) == (POP_SEG386_SHORT & 0xff)
85b80b0f
JB
8257 && i.op[0].regs->reg_num < 4)
8258 {
8259 as_bad (_("you can't `%s %s%s'"),
76d3f746 8260 insn_name (&i.tm), register_prefix, i.op[0].regs->reg_name);
85b80b0f
JB
8261 return 0;
8262 }
389d00a5 8263 if (i.op[0].regs->reg_num > 3
ddb62495 8264 && i.tm.opcode_space == SPACE_BASE )
85b80b0f 8265 {
389d00a5 8266 i.tm.base_opcode ^= (POP_SEG_SHORT ^ POP_SEG386_SHORT) & 0xff;
ddb62495 8267 i.tm.opcode_space = SPACE_0F;
85b80b0f
JB
8268 }
8269 i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
8270 }
ddb62495 8271 else if (i.tm.opcode_space == SPACE_BASE
389d00a5 8272 && (i.tm.base_opcode & ~3) == MOV_AX_DISP32)
29b0f896 8273 {
5e042380 8274 default_seg = reg_ds;
29b0f896 8275 }
40fb9820 8276 else if (i.tm.opcode_modifier.isstring)
29b0f896
AM
8277 {
8278 /* For the string instructions that allow a segment override
8279 on one of their operands, the default segment is ds. */
5e042380 8280 default_seg = reg_ds;
29b0f896 8281 }
50128d0c 8282 else if (i.short_form)
85b80b0f 8283 {
0ff3b7d0
JB
8284 /* The register operand is in the 1st or 2nd non-immediate operand. */
8285 const reg_entry *r = i.op[i.imm_operands].regs;
85b80b0f 8286
0ff3b7d0
JB
8287 if (!dot_insn ()
8288 && r->reg_type.bitfield.instance == Accum
8289 && i.op[i.imm_operands + 1].regs)
8290 r = i.op[i.imm_operands + 1].regs;
85b80b0f 8291 /* Register goes in low 3 bits of opcode. */
4943d587 8292 i.tm.base_opcode |= r->reg_num;
0a3eba42 8293 set_rex_vrex (r, REX_B, false);
0ff3b7d0
JB
8294
8295 if (dot_insn () && i.reg_operands == 2)
8296 {
8297 gas_assert (is_any_vex_encoding (&i.tm)
8298 || i.vec_encoding != vex_encoding_default);
8299 i.vex.register_specifier = i.op[i.operands - 1].regs;
8300 }
8301 }
8302 else if (i.reg_operands == 1
8303 && !i.flags[i.operands - 1]
8304 && i.tm.operand_types[i.operands - 1].bitfield.instance
8305 == InstanceNone)
8306 {
8307 gas_assert (is_any_vex_encoding (&i.tm)
8308 || i.vec_encoding != vex_encoding_default);
8309 i.vex.register_specifier = i.op[i.operands - 1].regs;
85b80b0f 8310 }
29b0f896 8311
514a8bb0 8312 if ((i.seg[0] || i.prefix[SEG_PREFIX])
7fc69528 8313 && i.tm.mnem_off == MN_lea)
92334ad2
JB
8314 {
8315 if (!quiet_warnings)
76d3f746 8316 as_warn (_("segment override on `%s' is ineffectual"), insn_name (&i.tm));
739d7649 8317 if (optimize && !i.no_optimize)
92334ad2
JB
8318 {
8319 i.seg[0] = NULL;
8320 i.prefix[SEG_PREFIX] = 0;
8321 }
8322 }
52271982
AM
8323
8324 /* If a segment was explicitly specified, and the specified segment
b6773884
JB
8325 is neither the default nor the one already recorded from a prefix,
8326 use an opcode prefix to select it. If we never figured out what
8327 the default segment is, then default_seg will be zero at this
8328 point, and the specified segment prefix will always be used. */
8329 if (i.seg[0]
8330 && i.seg[0] != default_seg
5e042380 8331 && i386_seg_prefixes[i.seg[0]->reg_num] != i.prefix[SEG_PREFIX])
29b0f896 8332 {
5e042380 8333 if (!add_prefix (i386_seg_prefixes[i.seg[0]->reg_num]))
29b0f896
AM
8334 return 0;
8335 }
8336 return 1;
8337}
8338
5e042380 8339static const reg_entry *
e3bb37b5 8340build_modrm_byte (void)
29b0f896 8341{
5e042380 8342 const reg_entry *default_seg = NULL;
ecb96e55
JB
8343 unsigned int source = i.imm_operands - i.tm.opcode_modifier.immext
8344 /* Compensate for kludge in md_assemble(). */
8345 + i.tm.operand_types[0].bitfield.imm1;
8346 unsigned int dest = i.operands - 1 - i.tm.opcode_modifier.immext;
8347 unsigned int v, op, reg_slot = ~0;
8348
8349 /* Accumulator (in particular %st), shift count (%cl), and alike need
8350 to be skipped just like immediate operands do. */
8351 if (i.tm.operand_types[source].bitfield.instance)
8352 ++source;
8353 while (i.tm.operand_types[dest].bitfield.instance)
8354 --dest;
8355
8356 for (op = source; op < i.operands; ++op)
8357 if (i.tm.operand_types[op].bitfield.baseindex)
8358 break;
c0f3af97 8359
ecb96e55 8360 if (i.reg_operands + i.mem_operands + (i.tm.extension_opcode != None) == 4)
c0f3af97 8361 {
4c2c6516 8362 expressionS *exp;
c0f3af97 8363
a683cc34 8364 /* There are 2 kinds of instructions:
bed3d976 8365 1. 5 operands: 4 register operands or 3 register operands
9d3bf266 8366 plus 1 memory operand plus one Imm4 operand, VexXDS, and
bed3d976 8367 VexW0 or VexW1. The destination must be either XMM, YMM or
43234a1e 8368 ZMM register.
bed3d976 8369 2. 4 operands: 4 register operands or 3 register operands
0ff3b7d0
JB
8370 plus 1 memory operand, with VexXDS.
8371 3. Other equivalent combinations when coming from s_insn(). */
eea96d3f 8372 gas_assert (i.tm.opcode_modifier.vexvvvv
0ff3b7d0
JB
8373 && i.tm.opcode_modifier.vexw);
8374 gas_assert (dot_insn ()
8375 || i.tm.operand_types[dest].bitfield.class == RegSIMD);
a683cc34 8376
ecb96e55
JB
8377 /* Of the first two non-immediate operands the one with the template
8378 not allowing for a memory one is encoded in the immediate operand. */
8379 if (source == op)
8380 reg_slot = source + 1;
48db9223 8381 else
ecb96e55 8382 reg_slot = source++;
48db9223 8383
0ff3b7d0
JB
8384 if (!dot_insn ())
8385 {
8386 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
8387 gas_assert (!(i.op[reg_slot].regs->reg_flags & RegVRex));
8388 }
8389 else
8390 gas_assert (i.tm.operand_types[reg_slot].bitfield.class != ClassNone);
8391
a683cc34 8392 if (i.imm_operands == 0)
bed3d976
JB
8393 {
8394 /* When there is no immediate operand, generate an 8bit
8395 immediate operand to encode the first operand. */
8396 exp = &im_expressions[i.imm_operands++];
8397 i.op[i.operands].imms = exp;
be1643ff 8398 i.types[i.operands].bitfield.imm8 = 1;
bed3d976
JB
8399 i.operands++;
8400
bed3d976 8401 exp->X_op = O_constant;
43234a1e 8402 }
922d8de8 8403 else
bed3d976 8404 {
9d3bf266
JB
8405 gas_assert (i.imm_operands == 1);
8406 gas_assert (fits_in_imm4 (i.op[0].imms->X_add_number));
8407 gas_assert (!i.tm.opcode_modifier.immext);
a683cc34 8408
9d3bf266
JB
8409 /* Turn on Imm8 again so that output_imm will generate it. */
8410 i.types[0].bitfield.imm8 = 1;
bed3d976 8411
0ff3b7d0 8412 exp = i.op[0].imms;
bed3d976 8413 }
0ff3b7d0
JB
8414 exp->X_add_number |= register_number (i.op[reg_slot].regs)
8415 << (3 + !(is_evex_encoding (&i.tm)
8416 || i.vec_encoding == vex_encoding_evex));
c0f3af97 8417 }
f12dc422 8418
ecb96e55
JB
8419 for (v = source + 1; v < dest; ++v)
8420 if (v != reg_slot)
8421 break;
8422 if (v >= dest)
8423 v = ~0;
8424 if (i.tm.extension_opcode != None)
8425 {
8426 if (dest != source)
8427 v = dest;
8428 dest = ~0;
8429 }
8430 gas_assert (source < dest);
8431 if (i.tm.opcode_modifier.operandconstraint == SWAP_SOURCES
8432 && source != op)
8433 {
8434 unsigned int tmp = source;
f12dc422 8435
ecb96e55
JB
8436 source = v;
8437 v = tmp;
8438 }
29b0f896 8439
ecb96e55
JB
8440 if (v < MAX_OPERANDS)
8441 {
8442 gas_assert (i.tm.opcode_modifier.vexvvvv);
8443 i.vex.register_specifier = i.op[v].regs;
29b0f896 8444 }
c0f3af97 8445
ecb96e55
JB
8446 if (op < i.operands)
8447 {
29b0f896
AM
8448 if (i.mem_operands)
8449 {
8450 unsigned int fake_zero_displacement = 0;
4eed87de 8451
ecb96e55 8452 gas_assert (i.flags[op] & Operand_Mem);
29b0f896 8453
63112cd6 8454 if (i.tm.opcode_modifier.sib)
6c30d220 8455 {
260cd341
LC
8456 /* The index register of VSIB shouldn't be RegIZ. */
8457 if (i.tm.opcode_modifier.sib != SIBMEM
8458 && i.index_reg->reg_num == RegIZ)
6c30d220
L
8459 abort ();
8460
8461 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8462 if (!i.base_reg)
8463 {
8464 i.sib.base = NO_BASE_REGISTER;
8465 i.sib.scale = i.log2_scale_factor;
2f2be86b 8466 i.types[op] = operand_type_and_not (i.types[op], anydisp);
a775efc8 8467 i.types[op].bitfield.disp32 = 1;
6c30d220 8468 }
260cd341
LC
8469
8470 /* Since the mandatory SIB always has index register, so
8471 the code logic remains unchanged. The non-mandatory SIB
8472 without index register is allowed and will be handled
8473 later. */
8474 if (i.index_reg)
8475 {
8476 if (i.index_reg->reg_num == RegIZ)
8477 i.sib.index = NO_INDEX_REGISTER;
8478 else
8479 i.sib.index = i.index_reg->reg_num;
5b7c81bd 8480 set_rex_vrex (i.index_reg, REX_X, false);
260cd341 8481 }
6c30d220
L
8482 }
8483
5e042380 8484 default_seg = reg_ds;
29b0f896
AM
8485
8486 if (i.base_reg == 0)
8487 {
8488 i.rm.mode = 0;
8489 if (!i.disp_operands)
9bb129e8 8490 fake_zero_displacement = 1;
29b0f896
AM
8491 if (i.index_reg == 0)
8492 {
260cd341
LC
8493 /* Both check for VSIB and mandatory non-vector SIB. */
8494 gas_assert (!i.tm.opcode_modifier.sib
8495 || i.tm.opcode_modifier.sib == SIBMEM);
29b0f896 8496 /* Operand is just <disp> */
2f2be86b 8497 i.types[op] = operand_type_and_not (i.types[op], anydisp);
20f0a1fc 8498 if (flag_code == CODE_64BIT)
29b0f896
AM
8499 {
8500 /* 64bit mode overwrites the 32bit absolute
8501 addressing by RIP relative addressing and
8502 absolute addressing is encoded by one of the
8503 redundant SIB forms. */
8504 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8505 i.sib.base = NO_BASE_REGISTER;
8506 i.sib.index = NO_INDEX_REGISTER;
a775efc8 8507 i.types[op].bitfield.disp32 = 1;
20f0a1fc 8508 }
fc225355
L
8509 else if ((flag_code == CODE_16BIT)
8510 ^ (i.prefix[ADDR_PREFIX] != 0))
20f0a1fc
NC
8511 {
8512 i.rm.regmem = NO_BASE_REGISTER_16;
2f2be86b 8513 i.types[op].bitfield.disp16 = 1;
20f0a1fc
NC
8514 }
8515 else
8516 {
8517 i.rm.regmem = NO_BASE_REGISTER;
2f2be86b 8518 i.types[op].bitfield.disp32 = 1;
29b0f896
AM
8519 }
8520 }
63112cd6 8521 else if (!i.tm.opcode_modifier.sib)
29b0f896 8522 {
6c30d220 8523 /* !i.base_reg && i.index_reg */
e968fc9b 8524 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8525 i.sib.index = NO_INDEX_REGISTER;
8526 else
8527 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8528 i.sib.base = NO_BASE_REGISTER;
8529 i.sib.scale = i.log2_scale_factor;
8530 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
2f2be86b 8531 i.types[op] = operand_type_and_not (i.types[op], anydisp);
a775efc8 8532 i.types[op].bitfield.disp32 = 1;
29b0f896 8533 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8534 i.rex |= REX_X;
29b0f896
AM
8535 }
8536 }
8537 /* RIP addressing for 64bit mode. */
e968fc9b 8538 else if (i.base_reg->reg_num == RegIP)
29b0f896 8539 {
63112cd6 8540 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896 8541 i.rm.regmem = NO_BASE_REGISTER;
40fb9820
L
8542 i.types[op].bitfield.disp8 = 0;
8543 i.types[op].bitfield.disp16 = 0;
a775efc8 8544 i.types[op].bitfield.disp32 = 1;
40fb9820 8545 i.types[op].bitfield.disp64 = 0;
71903a11 8546 i.flags[op] |= Operand_PCrel;
20f0a1fc
NC
8547 if (! i.disp_operands)
8548 fake_zero_displacement = 1;
29b0f896 8549 }
dc821c5f 8550 else if (i.base_reg->reg_type.bitfield.word)
29b0f896 8551 {
63112cd6 8552 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896
AM
8553 switch (i.base_reg->reg_num)
8554 {
8555 case 3: /* (%bx) */
8556 if (i.index_reg == 0)
8557 i.rm.regmem = 7;
8558 else /* (%bx,%si) -> 0, or (%bx,%di) -> 1 */
8559 i.rm.regmem = i.index_reg->reg_num - 6;
8560 break;
8561 case 5: /* (%bp) */
5e042380 8562 default_seg = reg_ss;
29b0f896
AM
8563 if (i.index_reg == 0)
8564 {
8565 i.rm.regmem = 6;
40fb9820 8566 if (operand_type_check (i.types[op], disp) == 0)
29b0f896
AM
8567 {
8568 /* fake (%bp) into 0(%bp) */
41eb8e88 8569 if (i.disp_encoding == disp_encoding_16bit)
1a02d6b0
L
8570 i.types[op].bitfield.disp16 = 1;
8571 else
8572 i.types[op].bitfield.disp8 = 1;
252b5132 8573 fake_zero_displacement = 1;
29b0f896
AM
8574 }
8575 }
8576 else /* (%bp,%si) -> 2, or (%bp,%di) -> 3 */
8577 i.rm.regmem = i.index_reg->reg_num - 6 + 2;
8578 break;
8579 default: /* (%si) -> 4 or (%di) -> 5 */
8580 i.rm.regmem = i.base_reg->reg_num - 6 + 4;
8581 }
41eb8e88
L
8582 if (!fake_zero_displacement
8583 && !i.disp_operands
8584 && i.disp_encoding)
8585 {
8586 fake_zero_displacement = 1;
8587 if (i.disp_encoding == disp_encoding_8bit)
8588 i.types[op].bitfield.disp8 = 1;
8589 else
8590 i.types[op].bitfield.disp16 = 1;
8591 }
29b0f896
AM
8592 i.rm.mode = mode_from_disp_size (i.types[op]);
8593 }
8594 else /* i.base_reg and 32/64 bit mode */
8595 {
a9aabc23 8596 if (operand_type_check (i.types[op], disp))
40fb9820 8597 {
73053c1f
JB
8598 i.types[op].bitfield.disp16 = 0;
8599 i.types[op].bitfield.disp64 = 0;
a775efc8 8600 i.types[op].bitfield.disp32 = 1;
40fb9820 8601 }
20f0a1fc 8602
63112cd6 8603 if (!i.tm.opcode_modifier.sib)
6c30d220 8604 i.rm.regmem = i.base_reg->reg_num;
29b0f896 8605 if ((i.base_reg->reg_flags & RegRex) != 0)
161a04f6 8606 i.rex |= REX_B;
29b0f896
AM
8607 i.sib.base = i.base_reg->reg_num;
8608 /* x86-64 ignores REX prefix bit here to avoid decoder
8609 complications. */
848930b2
JB
8610 if (!(i.base_reg->reg_flags & RegRex)
8611 && (i.base_reg->reg_num == EBP_REG_NUM
8612 || i.base_reg->reg_num == ESP_REG_NUM))
5e042380 8613 default_seg = reg_ss;
848930b2 8614 if (i.base_reg->reg_num == 5 && i.disp_operands == 0)
29b0f896 8615 {
848930b2 8616 fake_zero_displacement = 1;
1a02d6b0
L
8617 if (i.disp_encoding == disp_encoding_32bit)
8618 i.types[op].bitfield.disp32 = 1;
8619 else
8620 i.types[op].bitfield.disp8 = 1;
29b0f896
AM
8621 }
8622 i.sib.scale = i.log2_scale_factor;
8623 if (i.index_reg == 0)
8624 {
260cd341
LC
8625 /* Only check for VSIB. */
8626 gas_assert (i.tm.opcode_modifier.sib != VECSIB128
8627 && i.tm.opcode_modifier.sib != VECSIB256
8628 && i.tm.opcode_modifier.sib != VECSIB512);
8629
29b0f896
AM
8630 /* <disp>(%esp) becomes two byte modrm with no index
8631 register. We've already stored the code for esp
8632 in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
8633 Any base register besides %esp will not use the
8634 extra modrm byte. */
8635 i.sib.index = NO_INDEX_REGISTER;
29b0f896 8636 }
63112cd6 8637 else if (!i.tm.opcode_modifier.sib)
29b0f896 8638 {
e968fc9b 8639 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8640 i.sib.index = NO_INDEX_REGISTER;
8641 else
8642 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8643 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8644 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8645 i.rex |= REX_X;
29b0f896 8646 }
67a4f2b7
AO
8647
8648 if (i.disp_operands
8649 && (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
8650 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL))
8651 i.rm.mode = 0;
8652 else
a501d77e
L
8653 {
8654 if (!fake_zero_displacement
8655 && !i.disp_operands
8656 && i.disp_encoding)
8657 {
8658 fake_zero_displacement = 1;
8659 if (i.disp_encoding == disp_encoding_8bit)
8660 i.types[op].bitfield.disp8 = 1;
8661 else
8662 i.types[op].bitfield.disp32 = 1;
8663 }
8664 i.rm.mode = mode_from_disp_size (i.types[op]);
8665 }
29b0f896 8666 }
252b5132 8667
29b0f896
AM
8668 if (fake_zero_displacement)
8669 {
8670 /* Fakes a zero displacement assuming that i.types[op]
8671 holds the correct displacement size. */
8672 expressionS *exp;
8673
9c2799c2 8674 gas_assert (i.op[op].disps == 0);
29b0f896
AM
8675 exp = &disp_expressions[i.disp_operands++];
8676 i.op[op].disps = exp;
8677 exp->X_op = O_constant;
8678 exp->X_add_number = 0;
8679 exp->X_add_symbol = (symbolS *) 0;
8680 exp->X_op_symbol = (symbolS *) 0;
8681 }
8682 }
ecb96e55
JB
8683 else
8684 {
8685 i.rm.mode = 3;
8686 i.rm.regmem = i.op[op].regs->reg_num;
8687 set_rex_vrex (i.op[op].regs, REX_B, false);
8688 }
252b5132 8689
ecb96e55
JB
8690 if (op == dest)
8691 dest = ~0;
8692 if (op == source)
8693 source = ~0;
8694 }
8695 else
8696 {
8697 i.rm.mode = 3;
8698 if (!i.tm.opcode_modifier.regmem)
f88c9eb0 8699 {
ecb96e55
JB
8700 gas_assert (source < MAX_OPERANDS);
8701 i.rm.regmem = i.op[source].regs->reg_num;
8702 set_rex_vrex (i.op[source].regs, REX_B,
8703 dest >= MAX_OPERANDS && i.tm.opcode_modifier.sse2avx);
8704 source = ~0;
f88c9eb0 8705 }
ecb96e55 8706 else
29b0f896 8707 {
ecb96e55
JB
8708 gas_assert (dest < MAX_OPERANDS);
8709 i.rm.regmem = i.op[dest].regs->reg_num;
8710 set_rex_vrex (i.op[dest].regs, REX_B, i.tm.opcode_modifier.sse2avx);
8711 dest = ~0;
29b0f896 8712 }
ecb96e55 8713 }
252b5132 8714
ecb96e55
JB
8715 /* Fill in i.rm.reg field with extension opcode (if any) or the
8716 appropriate register. */
8717 if (i.tm.extension_opcode != None)
8718 i.rm.reg = i.tm.extension_opcode;
8719 else if (!i.tm.opcode_modifier.regmem && dest < MAX_OPERANDS)
8720 {
8721 i.rm.reg = i.op[dest].regs->reg_num;
8722 set_rex_vrex (i.op[dest].regs, REX_R, i.tm.opcode_modifier.sse2avx);
8723 }
8724 else
8725 {
8726 gas_assert (source < MAX_OPERANDS);
8727 i.rm.reg = i.op[source].regs->reg_num;
8728 set_rex_vrex (i.op[source].regs, REX_R, false);
29b0f896 8729 }
ecb96e55
JB
8730
8731 if (flag_code != CODE_64BIT && (i.rex & REX_R))
8732 {
8733 gas_assert (i.types[!i.tm.opcode_modifier.regmem].bitfield.class == RegCR);
8734 i.rex &= ~REX_R;
8735 add_prefix (LOCK_PREFIX_OPCODE);
8736 }
8737
29b0f896
AM
8738 return default_seg;
8739}
252b5132 8740
48ef937e
JB
8741static INLINE void
8742frag_opcode_byte (unsigned char byte)
8743{
8744 if (now_seg != absolute_section)
8745 FRAG_APPEND_1_CHAR (byte);
8746 else
8747 ++abs_section_offset;
8748}
8749
376cd056
JB
8750static unsigned int
8751flip_code16 (unsigned int code16)
8752{
8753 gas_assert (i.tm.operands == 1);
8754
8755 return !(i.prefix[REX_PREFIX] & REX_W)
8756 && (code16 ? i.tm.operand_types[0].bitfield.disp32
376cd056
JB
8757 : i.tm.operand_types[0].bitfield.disp16)
8758 ? CODE16 : 0;
8759}
8760
29b0f896 8761static void
e3bb37b5 8762output_branch (void)
29b0f896
AM
8763{
8764 char *p;
f8a5c266 8765 int size;
29b0f896
AM
8766 int code16;
8767 int prefix;
8768 relax_substateT subtype;
8769 symbolS *sym;
8770 offsetT off;
8771
48ef937e
JB
8772 if (now_seg == absolute_section)
8773 {
8774 as_bad (_("relaxable branches not supported in absolute section"));
8775 return;
8776 }
8777
f8a5c266 8778 code16 = flag_code == CODE_16BIT ? CODE16 : 0;
1a42a9fe 8779 size = i.disp_encoding > disp_encoding_8bit ? BIG : SMALL;
29b0f896
AM
8780
8781 prefix = 0;
8782 if (i.prefix[DATA_PREFIX] != 0)
252b5132 8783 {
29b0f896
AM
8784 prefix = 1;
8785 i.prefixes -= 1;
376cd056 8786 code16 ^= flip_code16(code16);
252b5132 8787 }
29b0f896
AM
8788 /* Pentium4 branch hints. */
8789 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8790 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
2f66722d 8791 {
29b0f896
AM
8792 prefix++;
8793 i.prefixes--;
8794 }
8795 if (i.prefix[REX_PREFIX] != 0)
8796 {
8797 prefix++;
8798 i.prefixes--;
2f66722d
AM
8799 }
8800
7e8b059b
L
8801 /* BND prefixed jump. */
8802 if (i.prefix[BND_PREFIX] != 0)
8803 {
6cb0a70e
JB
8804 prefix++;
8805 i.prefixes--;
7e8b059b
L
8806 }
8807
f2810fe0 8808 if (i.prefixes != 0)
76d3f746 8809 as_warn (_("skipping prefixes on `%s'"), insn_name (&i.tm));
29b0f896
AM
8810
8811 /* It's always a symbol; End frag & setup for relax.
8812 Make sure there is enough room in this frag for the largest
8813 instruction we may generate in md_convert_frag. This is 2
8814 bytes for the opcode and room for the prefix and largest
8815 displacement. */
8816 frag_grow (prefix + 2 + 4);
8817 /* Prefix and 1 opcode byte go in fr_fix. */
8818 p = frag_more (prefix + 1);
8819 if (i.prefix[DATA_PREFIX] != 0)
8820 *p++ = DATA_PREFIX_OPCODE;
8821 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
8822 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
8823 *p++ = i.prefix[SEG_PREFIX];
6cb0a70e
JB
8824 if (i.prefix[BND_PREFIX] != 0)
8825 *p++ = BND_PREFIX_OPCODE;
29b0f896
AM
8826 if (i.prefix[REX_PREFIX] != 0)
8827 *p++ = i.prefix[REX_PREFIX];
8828 *p = i.tm.base_opcode;
8829
8830 if ((unsigned char) *p == JUMP_PC_RELATIVE)
f8a5c266 8831 subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, size);
40fb9820 8832 else if (cpu_arch_flags.bitfield.cpui386)
f8a5c266 8833 subtype = ENCODE_RELAX_STATE (COND_JUMP, size);
29b0f896 8834 else
f8a5c266 8835 subtype = ENCODE_RELAX_STATE (COND_JUMP86, size);
29b0f896 8836 subtype |= code16;
3e73aa7c 8837
29b0f896
AM
8838 sym = i.op[0].disps->X_add_symbol;
8839 off = i.op[0].disps->X_add_number;
3e73aa7c 8840
29b0f896
AM
8841 if (i.op[0].disps->X_op != O_constant
8842 && i.op[0].disps->X_op != O_symbol)
3e73aa7c 8843 {
29b0f896
AM
8844 /* Handle complex expressions. */
8845 sym = make_expr_symbol (i.op[0].disps);
8846 off = 0;
8847 }
3e73aa7c 8848
1ef3994a
JB
8849 frag_now->tc_frag_data.code64 = flag_code == CODE_64BIT;
8850
29b0f896
AM
8851 /* 1 possible extra opcode + 4 byte displacement go in var part.
8852 Pass reloc in fr_var. */
d258b828 8853 frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
29b0f896 8854}
3e73aa7c 8855
bd7ab16b
L
8856#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8857/* Return TRUE iff PLT32 relocation should be used for branching to
8858 symbol S. */
8859
5b7c81bd 8860static bool
bd7ab16b
L
8861need_plt32_p (symbolS *s)
8862{
8863 /* PLT32 relocation is ELF only. */
8864 if (!IS_ELF)
5b7c81bd 8865 return false;
bd7ab16b 8866
a5def729
RO
8867#ifdef TE_SOLARIS
8868 /* Don't emit PLT32 relocation on Solaris: neither native linker nor
8869 krtld support it. */
5b7c81bd 8870 return false;
a5def729
RO
8871#endif
8872
bd7ab16b
L
8873 /* Since there is no need to prepare for PLT branch on x86-64, we
8874 can generate R_X86_64_PLT32, instead of R_X86_64_PC32, which can
8875 be used as a marker for 32-bit PC-relative branches. */
8876 if (!object_64bit)
5b7c81bd 8877 return false;
bd7ab16b 8878
44365e88 8879 if (s == NULL)
5b7c81bd 8880 return false;
44365e88 8881
bd7ab16b
L
8882 /* Weak or undefined symbol need PLT32 relocation. */
8883 if (S_IS_WEAK (s) || !S_IS_DEFINED (s))
5b7c81bd 8884 return true;
bd7ab16b
L
8885
8886 /* Non-global symbol doesn't need PLT32 relocation. */
8887 if (! S_IS_EXTERNAL (s))
5b7c81bd 8888 return false;
bd7ab16b
L
8889
8890 /* Other global symbols need PLT32 relocation. NB: Symbol with
8891 non-default visibilities are treated as normal global symbol
8892 so that PLT32 relocation can be used as a marker for 32-bit
8893 PC-relative branches. It is useful for linker relaxation. */
5b7c81bd 8894 return true;
bd7ab16b
L
8895}
8896#endif
8897
29b0f896 8898static void
e3bb37b5 8899output_jump (void)
29b0f896
AM
8900{
8901 char *p;
8902 int size;
3e02c1cc 8903 fixS *fixP;
bd7ab16b 8904 bfd_reloc_code_real_type jump_reloc = i.reloc[0];
29b0f896 8905
0cfa3eb3 8906 if (i.tm.opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
8907 {
8908 /* This is a loop or jecxz type instruction. */
8909 size = 1;
8910 if (i.prefix[ADDR_PREFIX] != 0)
8911 {
48ef937e 8912 frag_opcode_byte (ADDR_PREFIX_OPCODE);
29b0f896
AM
8913 i.prefixes -= 1;
8914 }
8915 /* Pentium4 branch hints. */
8916 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8917 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
8918 {
48ef937e 8919 frag_opcode_byte (i.prefix[SEG_PREFIX]);
29b0f896 8920 i.prefixes--;
3e73aa7c
JH
8921 }
8922 }
29b0f896
AM
8923 else
8924 {
8925 int code16;
3e73aa7c 8926
29b0f896
AM
8927 code16 = 0;
8928 if (flag_code == CODE_16BIT)
8929 code16 = CODE16;
3e73aa7c 8930
29b0f896
AM
8931 if (i.prefix[DATA_PREFIX] != 0)
8932 {
48ef937e 8933 frag_opcode_byte (DATA_PREFIX_OPCODE);
29b0f896 8934 i.prefixes -= 1;
376cd056 8935 code16 ^= flip_code16(code16);
29b0f896 8936 }
252b5132 8937
29b0f896
AM
8938 size = 4;
8939 if (code16)
8940 size = 2;
8941 }
9fcc94b6 8942
6cb0a70e
JB
8943 /* BND prefixed jump. */
8944 if (i.prefix[BND_PREFIX] != 0)
29b0f896 8945 {
48ef937e 8946 frag_opcode_byte (i.prefix[BND_PREFIX]);
29b0f896
AM
8947 i.prefixes -= 1;
8948 }
252b5132 8949
6cb0a70e 8950 if (i.prefix[REX_PREFIX] != 0)
7e8b059b 8951 {
48ef937e 8952 frag_opcode_byte (i.prefix[REX_PREFIX]);
7e8b059b
L
8953 i.prefixes -= 1;
8954 }
8955
f2810fe0 8956 if (i.prefixes != 0)
76d3f746 8957 as_warn (_("skipping prefixes on `%s'"), insn_name (&i.tm));
e0890092 8958
48ef937e
JB
8959 if (now_seg == absolute_section)
8960 {
9a182d04 8961 abs_section_offset += i.opcode_length + size;
48ef937e
JB
8962 return;
8963 }
8964
9a182d04
JB
8965 p = frag_more (i.opcode_length + size);
8966 switch (i.opcode_length)
42164a71
L
8967 {
8968 case 2:
8969 *p++ = i.tm.base_opcode >> 8;
1a0670f3 8970 /* Fall through. */
42164a71
L
8971 case 1:
8972 *p++ = i.tm.base_opcode;
8973 break;
8974 default:
8975 abort ();
8976 }
e0890092 8977
bd7ab16b 8978#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1ef3994a
JB
8979 if (flag_code == CODE_64BIT && size == 4
8980 && jump_reloc == NO_RELOC && i.op[0].disps->X_add_number == 0
bd7ab16b
L
8981 && need_plt32_p (i.op[0].disps->X_add_symbol))
8982 jump_reloc = BFD_RELOC_X86_64_PLT32;
8983#endif
8984
8985 jump_reloc = reloc (size, 1, 1, jump_reloc);
8986
3e02c1cc 8987 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
bd7ab16b 8988 i.op[0].disps, 1, jump_reloc);
3e02c1cc 8989
eb19308f
JB
8990 /* All jumps handled here are signed, but don't unconditionally use a
8991 signed limit check for 32 and 16 bit jumps as we want to allow wrap
8992 around at 4G (outside of 64-bit mode) and 64k (except for XBEGIN)
8993 respectively. */
8994 switch (size)
8995 {
8996 case 1:
8997 fixP->fx_signed = 1;
8998 break;
8999
9000 case 2:
7fc69528 9001 if (i.tm.mnem_off == MN_xbegin)
eb19308f
JB
9002 fixP->fx_signed = 1;
9003 break;
9004
9005 case 4:
9006 if (flag_code == CODE_64BIT)
9007 fixP->fx_signed = 1;
9008 break;
9009 }
29b0f896 9010}
e0890092 9011
29b0f896 9012static void
e3bb37b5 9013output_interseg_jump (void)
29b0f896
AM
9014{
9015 char *p;
9016 int size;
9017 int prefix;
9018 int code16;
252b5132 9019
29b0f896
AM
9020 code16 = 0;
9021 if (flag_code == CODE_16BIT)
9022 code16 = CODE16;
a217f122 9023
29b0f896
AM
9024 prefix = 0;
9025 if (i.prefix[DATA_PREFIX] != 0)
9026 {
9027 prefix = 1;
9028 i.prefixes -= 1;
9029 code16 ^= CODE16;
9030 }
6cb0a70e
JB
9031
9032 gas_assert (!i.prefix[REX_PREFIX]);
252b5132 9033
29b0f896
AM
9034 size = 4;
9035 if (code16)
9036 size = 2;
252b5132 9037
f2810fe0 9038 if (i.prefixes != 0)
76d3f746 9039 as_warn (_("skipping prefixes on `%s'"), insn_name (&i.tm));
252b5132 9040
48ef937e
JB
9041 if (now_seg == absolute_section)
9042 {
9043 abs_section_offset += prefix + 1 + 2 + size;
9044 return;
9045 }
9046
29b0f896
AM
9047 /* 1 opcode; 2 segment; offset */
9048 p = frag_more (prefix + 1 + 2 + size);
3e73aa7c 9049
29b0f896
AM
9050 if (i.prefix[DATA_PREFIX] != 0)
9051 *p++ = DATA_PREFIX_OPCODE;
252b5132 9052
29b0f896
AM
9053 if (i.prefix[REX_PREFIX] != 0)
9054 *p++ = i.prefix[REX_PREFIX];
252b5132 9055
29b0f896
AM
9056 *p++ = i.tm.base_opcode;
9057 if (i.op[1].imms->X_op == O_constant)
9058 {
9059 offsetT n = i.op[1].imms->X_add_number;
252b5132 9060
29b0f896
AM
9061 if (size == 2
9062 && !fits_in_unsigned_word (n)
9063 && !fits_in_signed_word (n))
9064 {
9065 as_bad (_("16-bit jump out of range"));
9066 return;
9067 }
9068 md_number_to_chars (p, n, size);
9069 }
9070 else
9071 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
d258b828 9072 i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
6d96a594
C
9073
9074 p += size;
9075 if (i.op[0].imms->X_op == O_constant)
9076 md_number_to_chars (p, (valueT) i.op[0].imms->X_add_number, 2);
9077 else
9078 fix_new_exp (frag_now, p - frag_now->fr_literal, 2,
9079 i.op[0].imms, 0, reloc (2, 0, 0, i.reloc[0]));
29b0f896 9080}
a217f122 9081
b4a3a7b4
L
9082#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9083void
9084x86_cleanup (void)
9085{
9086 char *p;
9087 asection *seg = now_seg;
9088 subsegT subseg = now_subseg;
9089 asection *sec;
9090 unsigned int alignment, align_size_1;
9091 unsigned int isa_1_descsz, feature_2_descsz, descsz;
9092 unsigned int isa_1_descsz_raw, feature_2_descsz_raw;
9093 unsigned int padding;
9094
1273b2f8 9095 if (!IS_ELF || !x86_used_note)
b4a3a7b4
L
9096 return;
9097
b4a3a7b4
L
9098 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X86;
9099
9100 /* The .note.gnu.property section layout:
9101
9102 Field Length Contents
9103 ---- ---- ----
9104 n_namsz 4 4
9105 n_descsz 4 The note descriptor size
9106 n_type 4 NT_GNU_PROPERTY_TYPE_0
9107 n_name 4 "GNU"
9108 n_desc n_descsz The program property array
9109 .... .... ....
9110 */
9111
9112 /* Create the .note.gnu.property section. */
9113 sec = subseg_new (NOTE_GNU_PROPERTY_SECTION_NAME, 0);
fd361982 9114 bfd_set_section_flags (sec,
b4a3a7b4
L
9115 (SEC_ALLOC
9116 | SEC_LOAD
9117 | SEC_DATA
9118 | SEC_HAS_CONTENTS
9119 | SEC_READONLY));
9120
9121 if (get_elf_backend_data (stdoutput)->s->elfclass == ELFCLASS64)
9122 {
9123 align_size_1 = 7;
9124 alignment = 3;
9125 }
9126 else
9127 {
9128 align_size_1 = 3;
9129 alignment = 2;
9130 }
9131
fd361982 9132 bfd_set_section_alignment (sec, alignment);
b4a3a7b4
L
9133 elf_section_type (sec) = SHT_NOTE;
9134
1273b2f8
L
9135 /* GNU_PROPERTY_X86_ISA_1_USED: 4-byte type + 4-byte data size
9136 + 4-byte data */
9137 isa_1_descsz_raw = 4 + 4 + 4;
9138 /* Align GNU_PROPERTY_X86_ISA_1_USED. */
9139 isa_1_descsz = (isa_1_descsz_raw + align_size_1) & ~align_size_1;
b4a3a7b4
L
9140
9141 feature_2_descsz_raw = isa_1_descsz;
9142 /* GNU_PROPERTY_X86_FEATURE_2_USED: 4-byte type + 4-byte data size
1273b2f8 9143 + 4-byte data */
b4a3a7b4
L
9144 feature_2_descsz_raw += 4 + 4 + 4;
9145 /* Align GNU_PROPERTY_X86_FEATURE_2_USED. */
9146 feature_2_descsz = ((feature_2_descsz_raw + align_size_1)
9147 & ~align_size_1);
9148
9149 descsz = feature_2_descsz;
9150 /* Section size: n_namsz + n_descsz + n_type + n_name + n_descsz. */
9151 p = frag_more (4 + 4 + 4 + 4 + descsz);
9152
9153 /* Write n_namsz. */
9154 md_number_to_chars (p, (valueT) 4, 4);
9155
9156 /* Write n_descsz. */
9157 md_number_to_chars (p + 4, (valueT) descsz, 4);
9158
9159 /* Write n_type. */
9160 md_number_to_chars (p + 4 * 2, (valueT) NT_GNU_PROPERTY_TYPE_0, 4);
9161
9162 /* Write n_name. */
9163 memcpy (p + 4 * 3, "GNU", 4);
9164
1273b2f8
L
9165 /* Write 4-byte type. */
9166 md_number_to_chars (p + 4 * 4,
9167 (valueT) GNU_PROPERTY_X86_ISA_1_USED, 4);
b4a3a7b4 9168
1273b2f8
L
9169 /* Write 4-byte data size. */
9170 md_number_to_chars (p + 4 * 5, (valueT) 4, 4);
b4a3a7b4 9171
1273b2f8
L
9172 /* Write 4-byte data. */
9173 md_number_to_chars (p + 4 * 6, (valueT) x86_isa_1_used, 4);
b4a3a7b4 9174
1273b2f8
L
9175 /* Zero out paddings. */
9176 padding = isa_1_descsz - isa_1_descsz_raw;
9177 if (padding)
9178 memset (p + 4 * 7, 0, padding);
b4a3a7b4
L
9179
9180 /* Write 4-byte type. */
9181 md_number_to_chars (p + isa_1_descsz + 4 * 4,
9182 (valueT) GNU_PROPERTY_X86_FEATURE_2_USED, 4);
9183
9184 /* Write 4-byte data size. */
9185 md_number_to_chars (p + isa_1_descsz + 4 * 5, (valueT) 4, 4);
9186
9187 /* Write 4-byte data. */
9188 md_number_to_chars (p + isa_1_descsz + 4 * 6,
9189 (valueT) x86_feature_2_used, 4);
9190
9191 /* Zero out paddings. */
9192 padding = feature_2_descsz - feature_2_descsz_raw;
9193 if (padding)
9194 memset (p + isa_1_descsz + 4 * 7, 0, padding);
9195
9196 /* We probably can't restore the current segment, for there likely
9197 isn't one yet... */
9198 if (seg && subseg)
9199 subseg_set (seg, subseg);
9200}
b52c4ee4
IB
9201
9202bool
9203x86_support_sframe_p (void)
9204{
3e3e792a 9205 /* At this time, SFrame stack trace is supported for AMD64 ABI only. */
b52c4ee4
IB
9206 return (x86_elf_abi == X86_64_ABI);
9207}
9208
9209bool
9210x86_sframe_ra_tracking_p (void)
9211{
9212 /* In AMD64, return address is always stored on the stack at a fixed offset
9213 from the CFA (provided via x86_sframe_cfa_ra_offset ()).
9214 Do not track explicitly via an SFrame Frame Row Entry. */
9215 return false;
9216}
9217
9218offsetT
9219x86_sframe_cfa_ra_offset (void)
9220{
9221 gas_assert (x86_elf_abi == X86_64_ABI);
9222 return (offsetT) -8;
9223}
9224
9225unsigned char
9226x86_sframe_get_abi_arch (void)
9227{
9228 unsigned char sframe_abi_arch = 0;
9229
9230 if (x86_support_sframe_p ())
9231 {
9232 gas_assert (!target_big_endian);
9233 sframe_abi_arch = SFRAME_ABI_AMD64_ENDIAN_LITTLE;
9234 }
9235
9236 return sframe_abi_arch;
9237}
9238
b4a3a7b4
L
9239#endif
9240
9c33702b
JB
9241static unsigned int
9242encoding_length (const fragS *start_frag, offsetT start_off,
9243 const char *frag_now_ptr)
9244{
9245 unsigned int len = 0;
9246
9247 if (start_frag != frag_now)
9248 {
9249 const fragS *fr = start_frag;
9250
9251 do {
9252 len += fr->fr_fix;
9253 fr = fr->fr_next;
9254 } while (fr && fr != frag_now);
9255 }
9256
9257 return len - start_off + (frag_now_ptr - frag_now->fr_literal);
9258}
9259
e379e5f3 9260/* Return 1 for test, and, cmp, add, sub, inc and dec which may
79d72f45
HL
9261 be macro-fused with conditional jumps.
9262 NB: If TEST/AND/CMP/ADD/SUB/INC/DEC is of RIP relative address,
9263 or is one of the following format:
9264
9265 cmp m, imm
9266 add m, imm
9267 sub m, imm
9268 test m, imm
9269 and m, imm
9270 inc m
9271 dec m
9272
9273 it is unfusible. */
e379e5f3
L
9274
9275static int
79d72f45 9276maybe_fused_with_jcc_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
9277{
9278 /* No RIP address. */
9279 if (i.base_reg && i.base_reg->reg_num == RegIP)
9280 return 0;
9281
389d00a5 9282 /* No opcodes outside of base encoding space. */
ddb62495 9283 if (i.tm.opcode_space != SPACE_BASE)
e379e5f3
L
9284 return 0;
9285
79d72f45
HL
9286 /* add, sub without add/sub m, imm. */
9287 if (i.tm.base_opcode <= 5
e379e5f3
L
9288 || (i.tm.base_opcode >= 0x28 && i.tm.base_opcode <= 0x2d)
9289 || ((i.tm.base_opcode | 3) == 0x83
79d72f45 9290 && (i.tm.extension_opcode == 0x5
e379e5f3 9291 || i.tm.extension_opcode == 0x0)))
79d72f45
HL
9292 {
9293 *mf_cmp_p = mf_cmp_alu_cmp;
9294 return !(i.mem_operands && i.imm_operands);
9295 }
e379e5f3 9296
79d72f45
HL
9297 /* and without and m, imm. */
9298 if ((i.tm.base_opcode >= 0x20 && i.tm.base_opcode <= 0x25)
9299 || ((i.tm.base_opcode | 3) == 0x83
9300 && i.tm.extension_opcode == 0x4))
9301 {
9302 *mf_cmp_p = mf_cmp_test_and;
9303 return !(i.mem_operands && i.imm_operands);
9304 }
9305
9306 /* test without test m imm. */
e379e5f3
L
9307 if ((i.tm.base_opcode | 1) == 0x85
9308 || (i.tm.base_opcode | 1) == 0xa9
9309 || ((i.tm.base_opcode | 1) == 0xf7
79d72f45
HL
9310 && i.tm.extension_opcode == 0))
9311 {
9312 *mf_cmp_p = mf_cmp_test_and;
9313 return !(i.mem_operands && i.imm_operands);
9314 }
9315
9316 /* cmp without cmp m, imm. */
9317 if ((i.tm.base_opcode >= 0x38 && i.tm.base_opcode <= 0x3d)
e379e5f3
L
9318 || ((i.tm.base_opcode | 3) == 0x83
9319 && (i.tm.extension_opcode == 0x7)))
79d72f45
HL
9320 {
9321 *mf_cmp_p = mf_cmp_alu_cmp;
9322 return !(i.mem_operands && i.imm_operands);
9323 }
e379e5f3 9324
79d72f45 9325 /* inc, dec without inc/dec m. */
e379e5f3
L
9326 if ((i.tm.cpu_flags.bitfield.cpuno64
9327 && (i.tm.base_opcode | 0xf) == 0x4f)
9328 || ((i.tm.base_opcode | 1) == 0xff
9329 && i.tm.extension_opcode <= 0x1))
79d72f45
HL
9330 {
9331 *mf_cmp_p = mf_cmp_incdec;
9332 return !i.mem_operands;
9333 }
e379e5f3
L
9334
9335 return 0;
9336}
9337
9338/* Return 1 if a FUSED_JCC_PADDING frag should be generated. */
9339
9340static int
79d72f45 9341add_fused_jcc_padding_frag_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
9342{
9343 /* NB: Don't work with COND_JUMP86 without i386. */
9344 if (!align_branch_power
9345 || now_seg == absolute_section
9346 || !cpu_arch_flags.bitfield.cpui386
9347 || !(align_branch & align_branch_fused_bit))
9348 return 0;
9349
79d72f45 9350 if (maybe_fused_with_jcc_p (mf_cmp_p))
e379e5f3
L
9351 {
9352 if (last_insn.kind == last_insn_other
9353 || last_insn.seg != now_seg)
9354 return 1;
9355 if (flag_debug)
9356 as_warn_where (last_insn.file, last_insn.line,
9357 _("`%s` skips -malign-branch-boundary on `%s`"),
76d3f746 9358 last_insn.name, insn_name (&i.tm));
e379e5f3
L
9359 }
9360
9361 return 0;
9362}
9363
9364/* Return 1 if a BRANCH_PREFIX frag should be generated. */
9365
9366static int
9367add_branch_prefix_frag_p (void)
9368{
9369 /* NB: Don't work with COND_JUMP86 without i386. Don't add prefix
9370 to PadLock instructions since they include prefixes in opcode. */
9371 if (!align_branch_power
9372 || !align_branch_prefix_size
9373 || now_seg == absolute_section
9374 || i.tm.cpu_flags.bitfield.cpupadlock
9375 || !cpu_arch_flags.bitfield.cpui386)
9376 return 0;
9377
9378 /* Don't add prefix if it is a prefix or there is no operand in case
9379 that segment prefix is special. */
9380 if (!i.operands || i.tm.opcode_modifier.isprefix)
9381 return 0;
9382
9383 if (last_insn.kind == last_insn_other
9384 || last_insn.seg != now_seg)
9385 return 1;
9386
9387 if (flag_debug)
9388 as_warn_where (last_insn.file, last_insn.line,
9389 _("`%s` skips -malign-branch-boundary on `%s`"),
76d3f746 9390 last_insn.name, insn_name (&i.tm));
e379e5f3
L
9391
9392 return 0;
9393}
9394
9395/* Return 1 if a BRANCH_PADDING frag should be generated. */
9396
9397static int
79d72f45
HL
9398add_branch_padding_frag_p (enum align_branch_kind *branch_p,
9399 enum mf_jcc_kind *mf_jcc_p)
e379e5f3
L
9400{
9401 int add_padding;
9402
9403 /* NB: Don't work with COND_JUMP86 without i386. */
9404 if (!align_branch_power
9405 || now_seg == absolute_section
389d00a5 9406 || !cpu_arch_flags.bitfield.cpui386
ddb62495 9407 || i.tm.opcode_space != SPACE_BASE)
e379e5f3
L
9408 return 0;
9409
9410 add_padding = 0;
9411
9412 /* Check for jcc and direct jmp. */
9413 if (i.tm.opcode_modifier.jump == JUMP)
9414 {
9415 if (i.tm.base_opcode == JUMP_PC_RELATIVE)
9416 {
9417 *branch_p = align_branch_jmp;
9418 add_padding = align_branch & align_branch_jmp_bit;
9419 }
9420 else
9421 {
79d72f45
HL
9422 /* Because J<cc> and JN<cc> share same group in macro-fusible table,
9423 igore the lowest bit. */
9424 *mf_jcc_p = (i.tm.base_opcode & 0x0e) >> 1;
e379e5f3
L
9425 *branch_p = align_branch_jcc;
9426 if ((align_branch & align_branch_jcc_bit))
9427 add_padding = 1;
9428 }
9429 }
e379e5f3
L
9430 else if ((i.tm.base_opcode | 1) == 0xc3)
9431 {
9432 /* Near ret. */
9433 *branch_p = align_branch_ret;
9434 if ((align_branch & align_branch_ret_bit))
9435 add_padding = 1;
9436 }
9437 else
9438 {
9439 /* Check for indirect jmp, direct and indirect calls. */
9440 if (i.tm.base_opcode == 0xe8)
9441 {
9442 /* Direct call. */
9443 *branch_p = align_branch_call;
9444 if ((align_branch & align_branch_call_bit))
9445 add_padding = 1;
9446 }
9447 else if (i.tm.base_opcode == 0xff
9448 && (i.tm.extension_opcode == 2
9449 || i.tm.extension_opcode == 4))
9450 {
9451 /* Indirect call and jmp. */
9452 *branch_p = align_branch_indirect;
9453 if ((align_branch & align_branch_indirect_bit))
9454 add_padding = 1;
9455 }
9456
9457 if (add_padding
9458 && i.disp_operands
9459 && tls_get_addr
9460 && (i.op[0].disps->X_op == O_symbol
9461 || (i.op[0].disps->X_op == O_subtract
9462 && i.op[0].disps->X_op_symbol == GOT_symbol)))
9463 {
9464 symbolS *s = i.op[0].disps->X_add_symbol;
9465 /* No padding to call to global or undefined tls_get_addr. */
9466 if ((S_IS_EXTERNAL (s) || !S_IS_DEFINED (s))
9467 && strcmp (S_GET_NAME (s), tls_get_addr) == 0)
9468 return 0;
9469 }
9470 }
9471
9472 if (add_padding
9473 && last_insn.kind != last_insn_other
9474 && last_insn.seg == now_seg)
9475 {
9476 if (flag_debug)
9477 as_warn_where (last_insn.file, last_insn.line,
9478 _("`%s` skips -malign-branch-boundary on `%s`"),
76d3f746 9479 last_insn.name, insn_name (&i.tm));
e379e5f3
L
9480 return 0;
9481 }
9482
9483 return add_padding;
9484}
9485
29b0f896 9486static void
e3bb37b5 9487output_insn (void)
29b0f896 9488{
2bbd9c25
JJ
9489 fragS *insn_start_frag;
9490 offsetT insn_start_off;
e379e5f3
L
9491 fragS *fragP = NULL;
9492 enum align_branch_kind branch = align_branch_none;
79d72f45
HL
9493 /* The initializer is arbitrary just to avoid uninitialized error.
9494 it's actually either assigned in add_branch_padding_frag_p
9495 or never be used. */
9496 enum mf_jcc_kind mf_jcc = mf_jcc_jo;
2bbd9c25 9497
b4a3a7b4 9498#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
48ef937e 9499 if (IS_ELF && x86_used_note && now_seg != absolute_section)
b4a3a7b4 9500 {
32930e4e
L
9501 if ((i.xstate & xstate_tmm) == xstate_tmm
9502 || i.tm.cpu_flags.bitfield.cpuamx_tile)
9503 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_TMM;
9504
b4a3a7b4
L
9505 if (i.tm.cpu_flags.bitfield.cpu8087
9506 || i.tm.cpu_flags.bitfield.cpu287
9507 || i.tm.cpu_flags.bitfield.cpu387
9508 || i.tm.cpu_flags.bitfield.cpu687
9509 || i.tm.cpu_flags.bitfield.cpufisttp)
9510 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X87;
014d61ea 9511
921eafea 9512 if ((i.xstate & xstate_mmx)
7fc69528
JB
9513 || i.tm.mnem_off == MN_emms
9514 || i.tm.mnem_off == MN_femms)
b4a3a7b4 9515 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MMX;
014d61ea 9516
32930e4e
L
9517 if (i.index_reg)
9518 {
9519 if (i.index_reg->reg_type.bitfield.zmmword)
9520 i.xstate |= xstate_zmm;
9521 else if (i.index_reg->reg_type.bitfield.ymmword)
9522 i.xstate |= xstate_ymm;
9523 else if (i.index_reg->reg_type.bitfield.xmmword)
9524 i.xstate |= xstate_xmm;
9525 }
014d61ea
JB
9526
9527 /* vzeroall / vzeroupper */
9528 if (i.tm.base_opcode == 0x77 && i.tm.cpu_flags.bitfield.cpuavx)
9529 i.xstate |= xstate_ymm;
9530
c4694f17 9531 if ((i.xstate & xstate_xmm)
389d00a5
JB
9532 /* ldmxcsr / stmxcsr / vldmxcsr / vstmxcsr */
9533 || (i.tm.base_opcode == 0xae
9534 && (i.tm.cpu_flags.bitfield.cpusse
9535 || i.tm.cpu_flags.bitfield.cpuavx))
c4694f17
TG
9536 || i.tm.cpu_flags.bitfield.cpuwidekl
9537 || i.tm.cpu_flags.bitfield.cpukl)
b4a3a7b4 9538 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XMM;
014d61ea 9539
921eafea 9540 if ((i.xstate & xstate_ymm) == xstate_ymm)
b4a3a7b4 9541 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_YMM;
921eafea 9542 if ((i.xstate & xstate_zmm) == xstate_zmm)
b4a3a7b4 9543 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_ZMM;
6225c532 9544 if (i.mask.reg || (i.xstate & xstate_mask) == xstate_mask)
32930e4e 9545 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MASK;
b4a3a7b4
L
9546 if (i.tm.cpu_flags.bitfield.cpufxsr)
9547 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_FXSR;
9548 if (i.tm.cpu_flags.bitfield.cpuxsave)
9549 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVE;
9550 if (i.tm.cpu_flags.bitfield.cpuxsaveopt)
9551 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT;
9552 if (i.tm.cpu_flags.bitfield.cpuxsavec)
9553 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEC;
b0ab0693
L
9554
9555 if (x86_feature_2_used
9556 || i.tm.cpu_flags.bitfield.cpucmov
9557 || i.tm.cpu_flags.bitfield.cpusyscall
7fc69528 9558 || i.tm.mnem_off == MN_cmpxchg8b)
b0ab0693
L
9559 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_BASELINE;
9560 if (i.tm.cpu_flags.bitfield.cpusse3
9561 || i.tm.cpu_flags.bitfield.cpussse3
9562 || i.tm.cpu_flags.bitfield.cpusse4_1
9563 || i.tm.cpu_flags.bitfield.cpusse4_2
9564 || i.tm.cpu_flags.bitfield.cpucx16
9565 || i.tm.cpu_flags.bitfield.cpupopcnt
9566 /* LAHF-SAHF insns in 64-bit mode. */
9567 || (flag_code == CODE_64BIT
35648716 9568 && (i.tm.base_opcode | 1) == 0x9f
ddb62495 9569 && i.tm.opcode_space == SPACE_BASE))
b0ab0693
L
9570 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V2;
9571 if (i.tm.cpu_flags.bitfield.cpuavx
9572 || i.tm.cpu_flags.bitfield.cpuavx2
a9860005
JB
9573 /* Any VEX encoded insns execpt for AVX512F, AVX512BW, AVX512DQ,
9574 XOP, FMA4, LPW, TBM, and AMX. */
b0ab0693
L
9575 || (i.tm.opcode_modifier.vex
9576 && !i.tm.cpu_flags.bitfield.cpuavx512f
9577 && !i.tm.cpu_flags.bitfield.cpuavx512bw
9578 && !i.tm.cpu_flags.bitfield.cpuavx512dq
a9860005
JB
9579 && !i.tm.cpu_flags.bitfield.cpuxop
9580 && !i.tm.cpu_flags.bitfield.cpufma4
b0ab0693
L
9581 && !i.tm.cpu_flags.bitfield.cpulwp
9582 && !i.tm.cpu_flags.bitfield.cputbm
9583 && !(x86_feature_2_used & GNU_PROPERTY_X86_FEATURE_2_TMM))
9584 || i.tm.cpu_flags.bitfield.cpuf16c
9585 || i.tm.cpu_flags.bitfield.cpufma
9586 || i.tm.cpu_flags.bitfield.cpulzcnt
9587 || i.tm.cpu_flags.bitfield.cpumovbe
9588 || i.tm.cpu_flags.bitfield.cpuxsaves
9589 || (x86_feature_2_used
9590 & (GNU_PROPERTY_X86_FEATURE_2_XSAVE
9591 | GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT
9592 | GNU_PROPERTY_X86_FEATURE_2_XSAVEC)) != 0)
9593 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V3;
9594 if (i.tm.cpu_flags.bitfield.cpuavx512f
9595 || i.tm.cpu_flags.bitfield.cpuavx512bw
9596 || i.tm.cpu_flags.bitfield.cpuavx512dq
9597 || i.tm.cpu_flags.bitfield.cpuavx512vl
a9860005
JB
9598 /* Any EVEX encoded insns except for AVX512ER, AVX512PF,
9599 AVX512-4FMAPS, and AVX512-4VNNIW. */
b0ab0693
L
9600 || (i.tm.opcode_modifier.evex
9601 && !i.tm.cpu_flags.bitfield.cpuavx512er
9602 && !i.tm.cpu_flags.bitfield.cpuavx512pf
a9860005 9603 && !i.tm.cpu_flags.bitfield.cpuavx512_4fmaps
b0ab0693
L
9604 && !i.tm.cpu_flags.bitfield.cpuavx512_4vnniw))
9605 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V4;
b4a3a7b4
L
9606 }
9607#endif
9608
29b0f896
AM
9609 /* Tie dwarf2 debug info to the address at the start of the insn.
9610 We can't do this after the insn has been output as the current
9611 frag may have been closed off. eg. by frag_var. */
9612 dwarf2_emit_insn (0);
9613
2bbd9c25
JJ
9614 insn_start_frag = frag_now;
9615 insn_start_off = frag_now_fix ();
9616
79d72f45 9617 if (add_branch_padding_frag_p (&branch, &mf_jcc))
e379e5f3
L
9618 {
9619 char *p;
9620 /* Branch can be 8 bytes. Leave some room for prefixes. */
9621 unsigned int max_branch_padding_size = 14;
9622
9623 /* Align section to boundary. */
9624 record_alignment (now_seg, align_branch_power);
9625
9626 /* Make room for padding. */
9627 frag_grow (max_branch_padding_size);
9628
9629 /* Start of the padding. */
9630 p = frag_more (0);
9631
9632 fragP = frag_now;
9633
9634 frag_var (rs_machine_dependent, max_branch_padding_size, 0,
9635 ENCODE_RELAX_STATE (BRANCH_PADDING, 0),
9636 NULL, 0, p);
9637
79d72f45 9638 fragP->tc_frag_data.mf_type = mf_jcc;
e379e5f3
L
9639 fragP->tc_frag_data.branch_type = branch;
9640 fragP->tc_frag_data.max_bytes = max_branch_padding_size;
9641 }
9642
d59a54c2
JB
9643 if (!cpu_arch_flags.bitfield.cpui386 && (flag_code != CODE_16BIT)
9644 && !pre_386_16bit_warned)
9645 {
9646 as_warn (_("use .code16 to ensure correct addressing mode"));
9647 pre_386_16bit_warned = true;
9648 }
9649
29b0f896 9650 /* Output jumps. */
0cfa3eb3 9651 if (i.tm.opcode_modifier.jump == JUMP)
29b0f896 9652 output_branch ();
0cfa3eb3
JB
9653 else if (i.tm.opcode_modifier.jump == JUMP_BYTE
9654 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896 9655 output_jump ();
0cfa3eb3 9656 else if (i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT)
29b0f896
AM
9657 output_interseg_jump ();
9658 else
9659 {
9660 /* Output normal instructions here. */
9661 char *p;
9662 unsigned char *q;
47465058 9663 unsigned int j;
79d72f45 9664 enum mf_cmp_kind mf_cmp;
4dffcebc 9665
e4e00185 9666 if (avoid_fence
389d00a5
JB
9667 && (i.tm.base_opcode == 0xaee8
9668 || i.tm.base_opcode == 0xaef0
9669 || i.tm.base_opcode == 0xaef8))
48ef937e
JB
9670 {
9671 /* Encode lfence, mfence, and sfence as
9672 f0 83 04 24 00 lock addl $0x0, (%{re}sp). */
47f4115a 9673 if (flag_code == CODE_16BIT)
76d3f746 9674 as_bad (_("Cannot convert `%s' in 16-bit mode"), insn_name (&i.tm));
47f4115a
JB
9675 else if (omit_lock_prefix)
9676 as_bad (_("Cannot convert `%s' with `-momit-lock-prefix=yes' in effect"),
76d3f746 9677 insn_name (&i.tm));
47f4115a 9678 else if (now_seg != absolute_section)
48ef937e
JB
9679 {
9680 offsetT val = 0x240483f0ULL;
9681
9682 p = frag_more (5);
9683 md_number_to_chars (p, val, 5);
9684 }
9685 else
9686 abs_section_offset += 5;
9687 return;
9688 }
e4e00185 9689
d022bddd
IT
9690 /* Some processors fail on LOCK prefix. This options makes
9691 assembler ignore LOCK prefix and serves as a workaround. */
9692 if (omit_lock_prefix)
9693 {
35648716
JB
9694 if (i.tm.base_opcode == LOCK_PREFIX_OPCODE
9695 && i.tm.opcode_modifier.isprefix)
d022bddd
IT
9696 return;
9697 i.prefix[LOCK_PREFIX] = 0;
9698 }
9699
e379e5f3
L
9700 if (branch)
9701 /* Skip if this is a branch. */
9702 ;
79d72f45 9703 else if (add_fused_jcc_padding_frag_p (&mf_cmp))
e379e5f3
L
9704 {
9705 /* Make room for padding. */
9706 frag_grow (MAX_FUSED_JCC_PADDING_SIZE);
9707 p = frag_more (0);
9708
9709 fragP = frag_now;
9710
9711 frag_var (rs_machine_dependent, MAX_FUSED_JCC_PADDING_SIZE, 0,
9712 ENCODE_RELAX_STATE (FUSED_JCC_PADDING, 0),
9713 NULL, 0, p);
9714
79d72f45 9715 fragP->tc_frag_data.mf_type = mf_cmp;
e379e5f3
L
9716 fragP->tc_frag_data.branch_type = align_branch_fused;
9717 fragP->tc_frag_data.max_bytes = MAX_FUSED_JCC_PADDING_SIZE;
9718 }
9719 else if (add_branch_prefix_frag_p ())
9720 {
9721 unsigned int max_prefix_size = align_branch_prefix_size;
9722
9723 /* Make room for padding. */
9724 frag_grow (max_prefix_size);
9725 p = frag_more (0);
9726
9727 fragP = frag_now;
9728
9729 frag_var (rs_machine_dependent, max_prefix_size, 0,
9730 ENCODE_RELAX_STATE (BRANCH_PREFIX, 0),
9731 NULL, 0, p);
9732
9733 fragP->tc_frag_data.max_bytes = max_prefix_size;
9734 }
9735
43234a1e
L
9736 /* Since the VEX/EVEX prefix contains the implicit prefix, we
9737 don't need the explicit prefix. */
cf665fee 9738 if (!is_any_vex_encoding (&i.tm))
bc4bd9ab 9739 {
7b47a312 9740 switch (i.tm.opcode_modifier.opcodeprefix)
bc4bd9ab 9741 {
7b47a312
L
9742 case PREFIX_0X66:
9743 add_prefix (0x66);
9744 break;
9745 case PREFIX_0XF2:
9746 add_prefix (0xf2);
9747 break;
9748 case PREFIX_0XF3:
8b65b895
L
9749 if (!i.tm.cpu_flags.bitfield.cpupadlock
9750 || (i.prefix[REP_PREFIX] != 0xf3))
9751 add_prefix (0xf3);
c0f3af97 9752 break;
7b47a312 9753 case PREFIX_NONE:
9a182d04 9754 switch (i.opcode_length)
c0f3af97 9755 {
7b47a312 9756 case 2:
7b47a312 9757 break;
9a182d04 9758 case 1:
7b47a312 9759 /* Check for pseudo prefixes. */
9a182d04
JB
9760 if (!i.tm.opcode_modifier.isprefix || i.tm.base_opcode)
9761 break;
7b47a312
L
9762 as_bad_where (insn_start_frag->fr_file,
9763 insn_start_frag->fr_line,
9764 _("pseudo prefix without instruction"));
9765 return;
9766 default:
9767 abort ();
4dffcebc 9768 }
c0f3af97 9769 break;
c0f3af97
L
9770 default:
9771 abort ();
bc4bd9ab 9772 }
c0f3af97 9773
6d19a37a 9774#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
cf61b747
L
9775 /* For x32, add a dummy REX_OPCODE prefix for mov/add with
9776 R_X86_64_GOTTPOFF relocation so that linker can safely
14470f07
L
9777 perform IE->LE optimization. A dummy REX_OPCODE prefix
9778 is also needed for lea with R_X86_64_GOTPC32_TLSDESC
9779 relocation for GDesc -> IE/LE optimization. */
cf61b747
L
9780 if (x86_elf_abi == X86_64_X32_ABI
9781 && i.operands == 2
14470f07
L
9782 && (i.reloc[0] == BFD_RELOC_X86_64_GOTTPOFF
9783 || i.reloc[0] == BFD_RELOC_X86_64_GOTPC32_TLSDESC)
cf61b747
L
9784 && i.prefix[REX_PREFIX] == 0)
9785 add_prefix (REX_OPCODE);
6d19a37a 9786#endif
cf61b747 9787
c0f3af97
L
9788 /* The prefix bytes. */
9789 for (j = ARRAY_SIZE (i.prefix), q = i.prefix; j > 0; j--, q++)
9790 if (*q)
48ef937e 9791 frag_opcode_byte (*q);
0f10071e 9792 }
ae5c1c7b 9793 else
c0f3af97
L
9794 {
9795 for (j = 0, q = i.prefix; j < ARRAY_SIZE (i.prefix); j++, q++)
9796 if (*q)
9797 switch (j)
9798 {
c0f3af97
L
9799 case SEG_PREFIX:
9800 case ADDR_PREFIX:
48ef937e 9801 frag_opcode_byte (*q);
c0f3af97
L
9802 break;
9803 default:
9804 /* There should be no other prefixes for instructions
9805 with VEX prefix. */
9806 abort ();
9807 }
9808
43234a1e
L
9809 /* For EVEX instructions i.vrex should become 0 after
9810 build_evex_prefix. For VEX instructions upper 16 registers
9811 aren't available, so VREX should be 0. */
9812 if (i.vrex)
9813 abort ();
c0f3af97 9814 /* Now the VEX prefix. */
48ef937e
JB
9815 if (now_seg != absolute_section)
9816 {
9817 p = frag_more (i.vex.length);
9818 for (j = 0; j < i.vex.length; j++)
9819 p[j] = i.vex.bytes[j];
9820 }
9821 else
9822 abs_section_offset += i.vex.length;
c0f3af97 9823 }
252b5132 9824
29b0f896 9825 /* Now the opcode; be careful about word order here! */
389d00a5
JB
9826 j = i.opcode_length;
9827 if (!i.vex.length)
ddb62495 9828 switch (i.tm.opcode_space)
389d00a5
JB
9829 {
9830 case SPACE_BASE:
9831 break;
9832 case SPACE_0F:
9833 ++j;
9834 break;
9835 case SPACE_0F38:
9836 case SPACE_0F3A:
9837 j += 2;
9838 break;
9839 default:
9840 abort ();
9841 }
9842
48ef937e 9843 if (now_seg == absolute_section)
389d00a5
JB
9844 abs_section_offset += j;
9845 else if (j == 1)
29b0f896
AM
9846 {
9847 FRAG_APPEND_1_CHAR (i.tm.base_opcode);
9848 }
9849 else
9850 {
389d00a5
JB
9851 p = frag_more (j);
9852 if (!i.vex.length
ddb62495 9853 && i.tm.opcode_space != SPACE_BASE)
389d00a5
JB
9854 {
9855 *p++ = 0x0f;
ddb62495
JB
9856 if (i.tm.opcode_space != SPACE_0F)
9857 *p++ = i.tm.opcode_space == SPACE_0F38
389d00a5
JB
9858 ? 0x38 : 0x3a;
9859 }
9860
9a182d04 9861 switch (i.opcode_length)
331d2d0d 9862 {
4dffcebc 9863 case 2:
389d00a5
JB
9864 /* Put out high byte first: can't use md_number_to_chars! */
9865 *p++ = (i.tm.base_opcode >> 8) & 0xff;
9866 /* Fall through. */
9867 case 1:
9868 *p = i.tm.base_opcode & 0xff;
4dffcebc
L
9869 break;
9870 default:
9871 abort ();
9872 break;
331d2d0d 9873 }
0f10071e 9874
29b0f896 9875 }
3e73aa7c 9876
29b0f896 9877 /* Now the modrm byte and sib byte (if present). */
40fb9820 9878 if (i.tm.opcode_modifier.modrm)
29b0f896 9879 {
48ef937e
JB
9880 frag_opcode_byte ((i.rm.regmem << 0)
9881 | (i.rm.reg << 3)
9882 | (i.rm.mode << 6));
29b0f896
AM
9883 /* If i.rm.regmem == ESP (4)
9884 && i.rm.mode != (Register mode)
9885 && not 16 bit
9886 ==> need second modrm byte. */
9887 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
9888 && i.rm.mode != 3
dc821c5f 9889 && !(i.base_reg && i.base_reg->reg_type.bitfield.word))
48ef937e
JB
9890 frag_opcode_byte ((i.sib.base << 0)
9891 | (i.sib.index << 3)
9892 | (i.sib.scale << 6));
29b0f896 9893 }
3e73aa7c 9894
29b0f896 9895 if (i.disp_operands)
2bbd9c25 9896 output_disp (insn_start_frag, insn_start_off);
3e73aa7c 9897
29b0f896 9898 if (i.imm_operands)
2bbd9c25 9899 output_imm (insn_start_frag, insn_start_off);
9c33702b
JB
9900
9901 /*
9902 * frag_now_fix () returning plain abs_section_offset when we're in the
9903 * absolute section, and abs_section_offset not getting updated as data
9904 * gets added to the frag breaks the logic below.
9905 */
9906 if (now_seg != absolute_section)
9907 {
9908 j = encoding_length (insn_start_frag, insn_start_off, frag_more (0));
9909 if (j > 15)
9910 as_warn (_("instruction length of %u bytes exceeds the limit of 15"),
9911 j);
e379e5f3
L
9912 else if (fragP)
9913 {
9914 /* NB: Don't add prefix with GOTPC relocation since
9915 output_disp() above depends on the fixed encoding
9916 length. Can't add prefix with TLS relocation since
9917 it breaks TLS linker optimization. */
9918 unsigned int max = i.has_gotpc_tls_reloc ? 0 : 15 - j;
9919 /* Prefix count on the current instruction. */
9920 unsigned int count = i.vex.length;
9921 unsigned int k;
9922 for (k = 0; k < ARRAY_SIZE (i.prefix); k++)
9923 /* REX byte is encoded in VEX/EVEX prefix. */
9924 if (i.prefix[k] && (k != REX_PREFIX || !i.vex.length))
9925 count++;
9926
9927 /* Count prefixes for extended opcode maps. */
9928 if (!i.vex.length)
ddb62495 9929 switch (i.tm.opcode_space)
e379e5f3 9930 {
389d00a5 9931 case SPACE_BASE:
e379e5f3 9932 break;
389d00a5
JB
9933 case SPACE_0F:
9934 count++;
e379e5f3 9935 break;
389d00a5
JB
9936 case SPACE_0F38:
9937 case SPACE_0F3A:
9938 count += 2;
e379e5f3
L
9939 break;
9940 default:
9941 abort ();
9942 }
9943
9944 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
9945 == BRANCH_PREFIX)
9946 {
9947 /* Set the maximum prefix size in BRANCH_PREFIX
9948 frag. */
9949 if (fragP->tc_frag_data.max_bytes > max)
9950 fragP->tc_frag_data.max_bytes = max;
9951 if (fragP->tc_frag_data.max_bytes > count)
9952 fragP->tc_frag_data.max_bytes -= count;
9953 else
9954 fragP->tc_frag_data.max_bytes = 0;
9955 }
9956 else
9957 {
9958 /* Remember the maximum prefix size in FUSED_JCC_PADDING
9959 frag. */
9960 unsigned int max_prefix_size;
9961 if (align_branch_prefix_size > max)
9962 max_prefix_size = max;
9963 else
9964 max_prefix_size = align_branch_prefix_size;
9965 if (max_prefix_size > count)
9966 fragP->tc_frag_data.max_prefix_length
9967 = max_prefix_size - count;
9968 }
9969
9970 /* Use existing segment prefix if possible. Use CS
9971 segment prefix in 64-bit mode. In 32-bit mode, use SS
9972 segment prefix with ESP/EBP base register and use DS
9973 segment prefix without ESP/EBP base register. */
9974 if (i.prefix[SEG_PREFIX])
9975 fragP->tc_frag_data.default_prefix = i.prefix[SEG_PREFIX];
9976 else if (flag_code == CODE_64BIT)
9977 fragP->tc_frag_data.default_prefix = CS_PREFIX_OPCODE;
9978 else if (i.base_reg
9979 && (i.base_reg->reg_num == 4
9980 || i.base_reg->reg_num == 5))
9981 fragP->tc_frag_data.default_prefix = SS_PREFIX_OPCODE;
9982 else
9983 fragP->tc_frag_data.default_prefix = DS_PREFIX_OPCODE;
9984 }
9c33702b 9985 }
29b0f896 9986 }
252b5132 9987
e379e5f3
L
9988 /* NB: Don't work with COND_JUMP86 without i386. */
9989 if (align_branch_power
9990 && now_seg != absolute_section
9991 && cpu_arch_flags.bitfield.cpui386)
9992 {
9993 /* Terminate each frag so that we can add prefix and check for
9994 fused jcc. */
9995 frag_wane (frag_now);
9996 frag_new (0);
9997 }
9998
29b0f896
AM
9999#ifdef DEBUG386
10000 if (flag_debug)
10001 {
7b81dfbb 10002 pi ("" /*line*/, &i);
29b0f896
AM
10003 }
10004#endif /* DEBUG386 */
10005}
252b5132 10006
e205caa7
L
10007/* Return the size of the displacement operand N. */
10008
10009static int
10010disp_size (unsigned int n)
10011{
10012 int size = 4;
43234a1e 10013
b5014f7a 10014 if (i.types[n].bitfield.disp64)
40fb9820
L
10015 size = 8;
10016 else if (i.types[n].bitfield.disp8)
10017 size = 1;
10018 else if (i.types[n].bitfield.disp16)
10019 size = 2;
e205caa7
L
10020 return size;
10021}
10022
10023/* Return the size of the immediate operand N. */
10024
10025static int
10026imm_size (unsigned int n)
10027{
10028 int size = 4;
40fb9820
L
10029 if (i.types[n].bitfield.imm64)
10030 size = 8;
10031 else if (i.types[n].bitfield.imm8 || i.types[n].bitfield.imm8s)
10032 size = 1;
10033 else if (i.types[n].bitfield.imm16)
10034 size = 2;
e205caa7
L
10035 return size;
10036}
10037
29b0f896 10038static void
64e74474 10039output_disp (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
10040{
10041 char *p;
10042 unsigned int n;
252b5132 10043
29b0f896
AM
10044 for (n = 0; n < i.operands; n++)
10045 {
b5014f7a 10046 if (operand_type_check (i.types[n], disp))
29b0f896 10047 {
48ef937e
JB
10048 int size = disp_size (n);
10049
10050 if (now_seg == absolute_section)
10051 abs_section_offset += size;
10052 else if (i.op[n].disps->X_op == O_constant)
29b0f896 10053 {
43234a1e 10054 offsetT val = i.op[n].disps->X_add_number;
252b5132 10055
629cfaf1
JB
10056 val = offset_in_range (val >> (size == 1 ? i.memshift : 0),
10057 size);
29b0f896
AM
10058 p = frag_more (size);
10059 md_number_to_chars (p, val, size);
10060 }
10061 else
10062 {
f86103b7 10063 enum bfd_reloc_code_real reloc_type;
a775efc8
JB
10064 bool pcrel = (i.flags[n] & Operand_PCrel) != 0;
10065 bool sign = (flag_code == CODE_64BIT && size == 4
10066 && (!want_disp32 (&i.tm)
10067 || (i.tm.opcode_modifier.jump && !i.jumpabsolute
10068 && !i.types[n].bitfield.baseindex)))
10069 || pcrel;
02a86693 10070 fixS *fixP;
29b0f896 10071
e205caa7 10072 /* We can't have 8 bit displacement here. */
9c2799c2 10073 gas_assert (!i.types[n].bitfield.disp8);
e205caa7 10074
29b0f896
AM
10075 /* The PC relative address is computed relative
10076 to the instruction boundary, so in case immediate
10077 fields follows, we need to adjust the value. */
10078 if (pcrel && i.imm_operands)
10079 {
29b0f896 10080 unsigned int n1;
e205caa7 10081 int sz = 0;
252b5132 10082
29b0f896 10083 for (n1 = 0; n1 < i.operands; n1++)
40fb9820 10084 if (operand_type_check (i.types[n1], imm))
252b5132 10085 {
e205caa7 10086 /* Only one immediate is allowed for PC
e3bf0aad
JB
10087 relative address, except with .insn. */
10088 gas_assert (sz == 0 || dot_insn ());
10089 sz += imm_size (n1);
252b5132 10090 }
e3bf0aad 10091 /* We should find at least one immediate. */
9c2799c2 10092 gas_assert (sz != 0);
e3bf0aad 10093 i.op[n].disps->X_add_number -= sz;
29b0f896 10094 }
520dc8e8 10095
29b0f896 10096 p = frag_more (size);
d258b828 10097 reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
d6ab8113 10098 if (GOT_symbol
2bbd9c25 10099 && GOT_symbol == i.op[n].disps->X_add_symbol
d6ab8113 10100 && (((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
10101 || reloc_type == BFD_RELOC_X86_64_32S
10102 || (reloc_type == BFD_RELOC_64
10103 && object_64bit))
d6ab8113
JB
10104 && (i.op[n].disps->X_op == O_symbol
10105 || (i.op[n].disps->X_op == O_add
10106 && ((symbol_get_value_expression
10107 (i.op[n].disps->X_op_symbol)->X_op)
10108 == O_subtract))))
10109 || reloc_type == BFD_RELOC_32_PCREL))
2bbd9c25 10110 {
4fa24527 10111 if (!object_64bit)
7b81dfbb
AJ
10112 {
10113 reloc_type = BFD_RELOC_386_GOTPC;
5b7c81bd 10114 i.has_gotpc_tls_reloc = true;
98da05bf 10115 i.op[n].disps->X_add_number +=
d583596c 10116 encoding_length (insn_start_frag, insn_start_off, p);
7b81dfbb
AJ
10117 }
10118 else if (reloc_type == BFD_RELOC_64)
10119 reloc_type = BFD_RELOC_X86_64_GOTPC64;
d6ab8113 10120 else
7b81dfbb
AJ
10121 /* Don't do the adjustment for x86-64, as there
10122 the pcrel addressing is relative to the _next_
10123 insn, and that is taken care of in other code. */
d6ab8113 10124 reloc_type = BFD_RELOC_X86_64_GOTPC32;
2bbd9c25 10125 }
e379e5f3
L
10126 else if (align_branch_power)
10127 {
10128 switch (reloc_type)
10129 {
10130 case BFD_RELOC_386_TLS_GD:
10131 case BFD_RELOC_386_TLS_LDM:
10132 case BFD_RELOC_386_TLS_IE:
10133 case BFD_RELOC_386_TLS_IE_32:
10134 case BFD_RELOC_386_TLS_GOTIE:
10135 case BFD_RELOC_386_TLS_GOTDESC:
10136 case BFD_RELOC_386_TLS_DESC_CALL:
10137 case BFD_RELOC_X86_64_TLSGD:
10138 case BFD_RELOC_X86_64_TLSLD:
10139 case BFD_RELOC_X86_64_GOTTPOFF:
10140 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
10141 case BFD_RELOC_X86_64_TLSDESC_CALL:
5b7c81bd 10142 i.has_gotpc_tls_reloc = true;
e379e5f3
L
10143 default:
10144 break;
10145 }
10146 }
02a86693
L
10147 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal,
10148 size, i.op[n].disps, pcrel,
10149 reloc_type);
eb19308f
JB
10150
10151 if (flag_code == CODE_64BIT && size == 4 && pcrel
10152 && !i.prefix[ADDR_PREFIX])
10153 fixP->fx_signed = 1;
10154
02a86693
L
10155 /* Check for "call/jmp *mem", "mov mem, %reg",
10156 "test %reg, mem" and "binop mem, %reg" where binop
10157 is one of adc, add, and, cmp, or, sbb, sub, xor
e60f4d3b
L
10158 instructions without data prefix. Always generate
10159 R_386_GOT32X for "sym*GOT" operand in 32-bit mode. */
10160 if (i.prefix[DATA_PREFIX] == 0
10161 && (generate_relax_relocations
10162 || (!object_64bit
10163 && i.rm.mode == 0
10164 && i.rm.regmem == 5))
0cb4071e
L
10165 && (i.rm.mode == 2
10166 || (i.rm.mode == 0 && i.rm.regmem == 5))
ddb62495 10167 && i.tm.opcode_space == SPACE_BASE
02a86693
L
10168 && ((i.operands == 1
10169 && i.tm.base_opcode == 0xff
10170 && (i.rm.reg == 2 || i.rm.reg == 4))
10171 || (i.operands == 2
10172 && (i.tm.base_opcode == 0x8b
10173 || i.tm.base_opcode == 0x85
2ae4c703 10174 || (i.tm.base_opcode & ~0x38) == 0x03))))
02a86693
L
10175 {
10176 if (object_64bit)
10177 {
10178 fixP->fx_tcbit = i.rex != 0;
10179 if (i.base_reg
e968fc9b 10180 && (i.base_reg->reg_num == RegIP))
02a86693
L
10181 fixP->fx_tcbit2 = 1;
10182 }
10183 else
10184 fixP->fx_tcbit2 = 1;
10185 }
29b0f896
AM
10186 }
10187 }
10188 }
10189}
252b5132 10190
29b0f896 10191static void
64e74474 10192output_imm (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
10193{
10194 char *p;
10195 unsigned int n;
252b5132 10196
29b0f896
AM
10197 for (n = 0; n < i.operands; n++)
10198 {
40fb9820 10199 if (operand_type_check (i.types[n], imm))
29b0f896 10200 {
48ef937e
JB
10201 int size = imm_size (n);
10202
10203 if (now_seg == absolute_section)
10204 abs_section_offset += size;
10205 else if (i.op[n].imms->X_op == O_constant)
29b0f896 10206 {
29b0f896 10207 offsetT val;
b4cac588 10208
29b0f896
AM
10209 val = offset_in_range (i.op[n].imms->X_add_number,
10210 size);
10211 p = frag_more (size);
10212 md_number_to_chars (p, val, size);
10213 }
10214 else
10215 {
10216 /* Not absolute_section.
10217 Need a 32-bit fixup (don't support 8bit
10218 non-absolute imms). Try to support other
10219 sizes ... */
f86103b7 10220 enum bfd_reloc_code_real reloc_type;
e205caa7 10221 int sign;
29b0f896 10222
40fb9820 10223 if (i.types[n].bitfield.imm32s
a7d61044 10224 && (i.suffix == QWORD_MNEM_SUFFIX
c032bc4f
JB
10225 || (!i.suffix && i.tm.opcode_modifier.no_lsuf)
10226 || dot_insn ()))
29b0f896 10227 sign = 1;
e205caa7
L
10228 else
10229 sign = 0;
520dc8e8 10230
29b0f896 10231 p = frag_more (size);
d258b828 10232 reloc_type = reloc (size, 0, sign, i.reloc[n]);
f86103b7 10233
2bbd9c25
JJ
10234 /* This is tough to explain. We end up with this one if we
10235 * have operands that look like
10236 * "_GLOBAL_OFFSET_TABLE_+[.-.L284]". The goal here is to
10237 * obtain the absolute address of the GOT, and it is strongly
10238 * preferable from a performance point of view to avoid using
10239 * a runtime relocation for this. The actual sequence of
10240 * instructions often look something like:
10241 *
10242 * call .L66
10243 * .L66:
10244 * popl %ebx
10245 * addl $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
10246 *
10247 * The call and pop essentially return the absolute address
10248 * of the label .L66 and store it in %ebx. The linker itself
10249 * will ultimately change the first operand of the addl so
10250 * that %ebx points to the GOT, but to keep things simple, the
10251 * .o file must have this operand set so that it generates not
10252 * the absolute address of .L66, but the absolute address of
10253 * itself. This allows the linker itself simply treat a GOTPC
10254 * relocation as asking for a pcrel offset to the GOT to be
10255 * added in, and the addend of the relocation is stored in the
10256 * operand field for the instruction itself.
10257 *
10258 * Our job here is to fix the operand so that it would add
10259 * the correct offset so that %ebx would point to itself. The
10260 * thing that is tricky is that .-.L66 will point to the
10261 * beginning of the instruction, so we need to further modify
10262 * the operand so that it will point to itself. There are
10263 * other cases where you have something like:
10264 *
10265 * .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
10266 *
10267 * and here no correction would be required. Internally in
10268 * the assembler we treat operands of this form as not being
10269 * pcrel since the '.' is explicitly mentioned, and I wonder
10270 * whether it would simplify matters to do it this way. Who
10271 * knows. In earlier versions of the PIC patches, the
10272 * pcrel_adjust field was used to store the correction, but
10273 * since the expression is not pcrel, I felt it would be
10274 * confusing to do it this way. */
10275
d6ab8113 10276 if ((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
10277 || reloc_type == BFD_RELOC_X86_64_32S
10278 || reloc_type == BFD_RELOC_64)
29b0f896
AM
10279 && GOT_symbol
10280 && GOT_symbol == i.op[n].imms->X_add_symbol
10281 && (i.op[n].imms->X_op == O_symbol
10282 || (i.op[n].imms->X_op == O_add
10283 && ((symbol_get_value_expression
10284 (i.op[n].imms->X_op_symbol)->X_op)
10285 == O_subtract))))
10286 {
4fa24527 10287 if (!object_64bit)
d6ab8113 10288 reloc_type = BFD_RELOC_386_GOTPC;
7b81dfbb 10289 else if (size == 4)
d6ab8113 10290 reloc_type = BFD_RELOC_X86_64_GOTPC32;
7b81dfbb
AJ
10291 else if (size == 8)
10292 reloc_type = BFD_RELOC_X86_64_GOTPC64;
5b7c81bd 10293 i.has_gotpc_tls_reloc = true;
d583596c
JB
10294 i.op[n].imms->X_add_number +=
10295 encoding_length (insn_start_frag, insn_start_off, p);
29b0f896 10296 }
29b0f896
AM
10297 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
10298 i.op[n].imms, 0, reloc_type);
10299 }
10300 }
10301 }
252b5132
RH
10302}
10303\f
d182319b
JB
10304/* x86_cons_fix_new is called via the expression parsing code when a
10305 reloc is needed. We use this hook to get the correct .got reloc. */
d182319b
JB
10306static int cons_sign = -1;
10307
10308void
e3bb37b5 10309x86_cons_fix_new (fragS *frag, unsigned int off, unsigned int len,
62ebcb5c 10310 expressionS *exp, bfd_reloc_code_real_type r)
d182319b 10311{
d258b828 10312 r = reloc (len, 0, cons_sign, r);
d182319b
JB
10313
10314#ifdef TE_PE
10315 if (exp->X_op == O_secrel)
10316 {
10317 exp->X_op = O_symbol;
10318 r = BFD_RELOC_32_SECREL;
10319 }
145667f8
MH
10320 else if (exp->X_op == O_secidx)
10321 r = BFD_RELOC_16_SECIDX;
d182319b
JB
10322#endif
10323
10324 fix_new_exp (frag, off, len, exp, 0, r);
10325}
10326
357d1bd8
L
10327/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
10328 purpose of the `.dc.a' internal pseudo-op. */
10329
10330int
10331x86_address_bytes (void)
10332{
10333 if ((stdoutput->arch_info->mach & bfd_mach_x64_32))
10334 return 4;
10335 return stdoutput->arch_info->bits_per_address / 8;
10336}
10337
deea4973
JB
10338#if (!(defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined (OBJ_MACH_O)) \
10339 || defined (LEX_AT)) && !defined (TE_PE)
d258b828 10340# define lex_got(reloc, adjust, types) NULL
718ddfc0 10341#else
f3c180ae
AM
10342/* Parse operands of the form
10343 <symbol>@GOTOFF+<nnn>
10344 and similar .plt or .got references.
10345
10346 If we find one, set up the correct relocation in RELOC and copy the
10347 input string, minus the `@GOTOFF' into a malloc'd buffer for
10348 parsing by the calling routine. Return this buffer, and if ADJUST
10349 is non-null set it to the length of the string we removed from the
10350 input line. Otherwise return NULL. */
10351static char *
91d6fa6a 10352lex_got (enum bfd_reloc_code_real *rel,
64e74474 10353 int *adjust,
d258b828 10354 i386_operand_type *types)
f3c180ae 10355{
7b81dfbb
AJ
10356 /* Some of the relocations depend on the size of what field is to
10357 be relocated. But in our callers i386_immediate and i386_displacement
10358 we don't yet know the operand size (this will be set by insn
10359 matching). Hence we record the word32 relocation here,
10360 and adjust the reloc according to the real size in reloc(). */
145667f8
MH
10361 static const struct
10362 {
f3c180ae 10363 const char *str;
cff8d58a 10364 int len;
4fa24527 10365 const enum bfd_reloc_code_real rel[2];
40fb9820 10366 const i386_operand_type types64;
5b7c81bd 10367 bool need_GOT_symbol;
145667f8
MH
10368 }
10369 gotrel[] =
10370 {
05909f23
JB
10371
10372#define OPERAND_TYPE_IMM32_32S_DISP32 { .bitfield = \
10373 { .imm32 = 1, .imm32s = 1, .disp32 = 1 } }
10374#define OPERAND_TYPE_IMM32_32S_64_DISP32 { .bitfield = \
10375 { .imm32 = 1, .imm32s = 1, .imm64 = 1, .disp32 = 1 } }
10376#define OPERAND_TYPE_IMM32_32S_64_DISP32_64 { .bitfield = \
10377 { .imm32 = 1, .imm32s = 1, .imm64 = 1, .disp32 = 1, .disp64 = 1 } }
10378#define OPERAND_TYPE_IMM64_DISP64 { .bitfield = \
10379 { .imm64 = 1, .disp64 = 1 } }
10380
deea4973 10381#ifndef TE_PE
8ce3d284 10382#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
10383 { STRING_COMMA_LEN ("SIZE"), { BFD_RELOC_SIZE32,
10384 BFD_RELOC_SIZE32 },
05909f23 10385 { .bitfield = { .imm32 = 1, .imm64 = 1 } }, false },
8ce3d284 10386#endif
cff8d58a
L
10387 { STRING_COMMA_LEN ("PLTOFF"), { _dummy_first_bfd_reloc_code_real,
10388 BFD_RELOC_X86_64_PLTOFF64 },
05909f23 10389 { .bitfield = { .imm64 = 1 } }, true },
cff8d58a
L
10390 { STRING_COMMA_LEN ("PLT"), { BFD_RELOC_386_PLT32,
10391 BFD_RELOC_X86_64_PLT32 },
a775efc8 10392 OPERAND_TYPE_IMM32_32S_DISP32, false },
cff8d58a
L
10393 { STRING_COMMA_LEN ("GOTPLT"), { _dummy_first_bfd_reloc_code_real,
10394 BFD_RELOC_X86_64_GOTPLT64 },
5b7c81bd 10395 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
10396 { STRING_COMMA_LEN ("GOTOFF"), { BFD_RELOC_386_GOTOFF,
10397 BFD_RELOC_X86_64_GOTOFF64 },
5b7c81bd 10398 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
10399 { STRING_COMMA_LEN ("GOTPCREL"), { _dummy_first_bfd_reloc_code_real,
10400 BFD_RELOC_X86_64_GOTPCREL },
a775efc8 10401 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10402 { STRING_COMMA_LEN ("TLSGD"), { BFD_RELOC_386_TLS_GD,
10403 BFD_RELOC_X86_64_TLSGD },
a775efc8 10404 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10405 { STRING_COMMA_LEN ("TLSLDM"), { BFD_RELOC_386_TLS_LDM,
10406 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10407 OPERAND_TYPE_NONE, true },
cff8d58a
L
10408 { STRING_COMMA_LEN ("TLSLD"), { _dummy_first_bfd_reloc_code_real,
10409 BFD_RELOC_X86_64_TLSLD },
a775efc8 10410 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10411 { STRING_COMMA_LEN ("GOTTPOFF"), { BFD_RELOC_386_TLS_IE_32,
10412 BFD_RELOC_X86_64_GOTTPOFF },
a775efc8 10413 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10414 { STRING_COMMA_LEN ("TPOFF"), { BFD_RELOC_386_TLS_LE_32,
10415 BFD_RELOC_X86_64_TPOFF32 },
a775efc8 10416 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
10417 { STRING_COMMA_LEN ("NTPOFF"), { BFD_RELOC_386_TLS_LE,
10418 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10419 OPERAND_TYPE_NONE, true },
cff8d58a
L
10420 { STRING_COMMA_LEN ("DTPOFF"), { BFD_RELOC_386_TLS_LDO_32,
10421 BFD_RELOC_X86_64_DTPOFF32 },
a775efc8 10422 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
10423 { STRING_COMMA_LEN ("GOTNTPOFF"),{ BFD_RELOC_386_TLS_GOTIE,
10424 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10425 OPERAND_TYPE_NONE, true },
cff8d58a
L
10426 { STRING_COMMA_LEN ("INDNTPOFF"),{ BFD_RELOC_386_TLS_IE,
10427 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10428 OPERAND_TYPE_NONE, true },
cff8d58a
L
10429 { STRING_COMMA_LEN ("GOT"), { BFD_RELOC_386_GOT32,
10430 BFD_RELOC_X86_64_GOT32 },
a775efc8 10431 OPERAND_TYPE_IMM32_32S_64_DISP32, true },
cff8d58a
L
10432 { STRING_COMMA_LEN ("TLSDESC"), { BFD_RELOC_386_TLS_GOTDESC,
10433 BFD_RELOC_X86_64_GOTPC32_TLSDESC },
a775efc8 10434 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10435 { STRING_COMMA_LEN ("TLSCALL"), { BFD_RELOC_386_TLS_DESC_CALL,
10436 BFD_RELOC_X86_64_TLSDESC_CALL },
a775efc8 10437 OPERAND_TYPE_IMM32_32S_DISP32, true },
deea4973
JB
10438#else /* TE_PE */
10439 { STRING_COMMA_LEN ("SECREL32"), { BFD_RELOC_32_SECREL,
10440 BFD_RELOC_32_SECREL },
a775efc8 10441 OPERAND_TYPE_IMM32_32S_64_DISP32_64, false },
deea4973 10442#endif
05909f23
JB
10443
10444#undef OPERAND_TYPE_IMM32_32S_DISP32
10445#undef OPERAND_TYPE_IMM32_32S_64_DISP32
10446#undef OPERAND_TYPE_IMM32_32S_64_DISP32_64
10447#undef OPERAND_TYPE_IMM64_DISP64
10448
f3c180ae
AM
10449 };
10450 char *cp;
10451 unsigned int j;
10452
deea4973 10453#if defined (OBJ_MAYBE_ELF) && !defined (TE_PE)
718ddfc0
JB
10454 if (!IS_ELF)
10455 return NULL;
d382c579 10456#endif
718ddfc0 10457
f3c180ae 10458 for (cp = input_line_pointer; *cp != '@'; cp++)
67c11a9b 10459 if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
f3c180ae
AM
10460 return NULL;
10461
47465058 10462 for (j = 0; j < ARRAY_SIZE (gotrel); j++)
f3c180ae 10463 {
cff8d58a 10464 int len = gotrel[j].len;
28f81592 10465 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
f3c180ae 10466 {
4fa24527 10467 if (gotrel[j].rel[object_64bit] != 0)
f3c180ae 10468 {
28f81592
AM
10469 int first, second;
10470 char *tmpbuf, *past_reloc;
f3c180ae 10471
91d6fa6a 10472 *rel = gotrel[j].rel[object_64bit];
f3c180ae 10473
3956db08
JB
10474 if (types)
10475 {
10476 if (flag_code != CODE_64BIT)
40fb9820
L
10477 {
10478 types->bitfield.imm32 = 1;
10479 types->bitfield.disp32 = 1;
10480 }
3956db08
JB
10481 else
10482 *types = gotrel[j].types64;
10483 }
10484
844bf810 10485 if (gotrel[j].need_GOT_symbol && GOT_symbol == NULL)
f3c180ae
AM
10486 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
10487
28f81592 10488 /* The length of the first part of our input line. */
f3c180ae 10489 first = cp - input_line_pointer;
28f81592
AM
10490
10491 /* The second part goes from after the reloc token until
67c11a9b 10492 (and including) an end_of_line char or comma. */
28f81592 10493 past_reloc = cp + 1 + len;
67c11a9b
AM
10494 cp = past_reloc;
10495 while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
10496 ++cp;
10497 second = cp + 1 - past_reloc;
28f81592
AM
10498
10499 /* Allocate and copy string. The trailing NUL shouldn't
10500 be necessary, but be safe. */
add39d23 10501 tmpbuf = XNEWVEC (char, first + second + 2);
f3c180ae 10502 memcpy (tmpbuf, input_line_pointer, first);
0787a12d
AM
10503 if (second != 0 && *past_reloc != ' ')
10504 /* Replace the relocation token with ' ', so that
10505 errors like foo@GOTOFF1 will be detected. */
10506 tmpbuf[first++] = ' ';
af89796a
L
10507 else
10508 /* Increment length by 1 if the relocation token is
10509 removed. */
10510 len++;
10511 if (adjust)
10512 *adjust = len;
0787a12d
AM
10513 memcpy (tmpbuf + first, past_reloc, second);
10514 tmpbuf[first + second] = '\0';
f3c180ae
AM
10515 return tmpbuf;
10516 }
10517
4fa24527
JB
10518 as_bad (_("@%s reloc is not supported with %d-bit output format"),
10519 gotrel[j].str, 1 << (5 + object_64bit));
f3c180ae
AM
10520 return NULL;
10521 }
10522 }
10523
10524 /* Might be a symbol version string. Don't as_bad here. */
10525 return NULL;
10526}
4e4f7c87 10527#endif
f3c180ae 10528
62ebcb5c 10529bfd_reloc_code_real_type
e3bb37b5 10530x86_cons (expressionS *exp, int size)
f3c180ae 10531{
62ebcb5c
AM
10532 bfd_reloc_code_real_type got_reloc = NO_RELOC;
10533
6b50f5f4
JB
10534 intel_syntax = -intel_syntax;
10535 exp->X_md = 0;
5cc00775 10536 expr_mode = expr_operator_none;
6b50f5f4 10537
2748c1b1
L
10538#if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
10539 && !defined (LEX_AT)) \
10540 || defined (TE_PE)
4fa24527 10541 if (size == 4 || (object_64bit && size == 8))
f3c180ae
AM
10542 {
10543 /* Handle @GOTOFF and the like in an expression. */
10544 char *save;
10545 char *gotfree_input_line;
4a57f2cf 10546 int adjust = 0;
f3c180ae
AM
10547
10548 save = input_line_pointer;
d258b828 10549 gotfree_input_line = lex_got (&got_reloc, &adjust, NULL);
f3c180ae
AM
10550 if (gotfree_input_line)
10551 input_line_pointer = gotfree_input_line;
10552
10553 expression (exp);
10554
10555 if (gotfree_input_line)
10556 {
10557 /* expression () has merrily parsed up to the end of line,
10558 or a comma - in the wrong buffer. Transfer how far
10559 input_line_pointer has moved to the right buffer. */
10560 input_line_pointer = (save
10561 + (input_line_pointer - gotfree_input_line)
10562 + adjust);
10563 free (gotfree_input_line);
3992d3b7
AM
10564 if (exp->X_op == O_constant
10565 || exp->X_op == O_absent
10566 || exp->X_op == O_illegal
0398aac5 10567 || exp->X_op == O_register
3992d3b7
AM
10568 || exp->X_op == O_big)
10569 {
10570 char c = *input_line_pointer;
10571 *input_line_pointer = 0;
10572 as_bad (_("missing or invalid expression `%s'"), save);
10573 *input_line_pointer = c;
10574 }
b9519cfe
L
10575 else if ((got_reloc == BFD_RELOC_386_PLT32
10576 || got_reloc == BFD_RELOC_X86_64_PLT32)
10577 && exp->X_op != O_symbol)
10578 {
10579 char c = *input_line_pointer;
10580 *input_line_pointer = 0;
10581 as_bad (_("invalid PLT expression `%s'"), save);
10582 *input_line_pointer = c;
10583 }
f3c180ae
AM
10584 }
10585 }
10586 else
6b50f5f4 10587#endif
f3c180ae 10588 expression (exp);
ee86248c
JB
10589
10590 intel_syntax = -intel_syntax;
10591
10592 if (intel_syntax)
10593 i386_intel_simplify (exp);
62ebcb5c 10594
a442cac5 10595 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
5cc00775
JB
10596 if (size <= 4 && expr_mode == expr_operator_present
10597 && exp->X_op == O_constant && !object_64bit)
a442cac5
JB
10598 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
10599
62ebcb5c 10600 return got_reloc;
f3c180ae 10601}
f3c180ae 10602
9f32dd5b
L
10603static void
10604signed_cons (int size)
6482c264 10605{
a442cac5 10606 if (object_64bit)
d182319b
JB
10607 cons_sign = 1;
10608 cons (size);
10609 cons_sign = -1;
6482c264
NC
10610}
10611
edd67638
JB
10612static void
10613s_insn (int dummy ATTRIBUTE_UNUSED)
10614{
393fbe8d 10615 char mnemonic[MAX_MNEM_SIZE], *line = input_line_pointer, *ptr;
edd67638
JB
10616 char *saved_ilp = find_end_of_line (line, false), saved_char;
10617 const char *end;
10618 unsigned int j;
10619 valueT val;
10620 bool vex = false, xop = false, evex = false;
10621 static const templates tt = { &i.tm, &i.tm + 1 };
10622
10623 init_globals ();
10624
10625 saved_char = *saved_ilp;
10626 *saved_ilp = 0;
10627
10628 end = parse_insn (line, mnemonic, true);
10629 if (end == NULL)
10630 {
10631 bad:
10632 *saved_ilp = saved_char;
10633 ignore_rest_of_line ();
d0c2e3ec 10634 i.tm.mnem_off = 0;
edd67638
JB
10635 return;
10636 }
10637 line += end - line;
10638
10639 current_templates = &tt;
10640 i.tm.mnem_off = MN__insn;
393fbe8d 10641 i.tm.extension_opcode = None;
edd67638
JB
10642
10643 if (startswith (line, "VEX")
10644 && (line[3] == '.' || is_space_char (line[3])))
10645 {
10646 vex = true;
10647 line += 3;
10648 }
10649 else if (startswith (line, "XOP") && ISDIGIT (line[3]))
10650 {
10651 char *e;
10652 unsigned long n = strtoul (line + 3, &e, 16);
10653
10654 if (e == line + 5 && n >= 0x08 && n <= 0x1f
10655 && (*e == '.' || is_space_char (*e)))
10656 {
10657 xop = true;
d0c2e3ec
JB
10658 /* Arrange for build_vex_prefix() to emit 0x8f. */
10659 i.tm.opcode_space = SPACE_XOP08;
10660 i.insn_opcode_space = n;
edd67638
JB
10661 line = e;
10662 }
10663 }
10664 else if (startswith (line, "EVEX")
10665 && (line[4] == '.' || is_space_char (line[4])))
10666 {
10667 evex = true;
10668 line += 4;
10669 }
10670
10671 if (vex || xop
10672 ? i.vec_encoding == vex_encoding_evex
10673 : evex
10674 ? i.vec_encoding == vex_encoding_vex
10675 || i.vec_encoding == vex_encoding_vex3
10676 : i.vec_encoding != vex_encoding_default)
10677 {
10678 as_bad (_("pseudo-prefix conflicts with encoding specifier"));
10679 goto bad;
10680 }
10681
0ff3b7d0
JB
10682 if (line > end && i.vec_encoding == vex_encoding_default)
10683 i.vec_encoding = evex ? vex_encoding_evex : vex_encoding_vex;
10684
edd67638
JB
10685 if (line > end && *line == '.')
10686 {
d0c2e3ec
JB
10687 /* Length specifier (VEX.L, XOP.L, EVEX.L'L). */
10688 switch (line[1])
10689 {
10690 case 'L':
10691 switch (line[2])
10692 {
10693 case '0':
10694 if (evex)
10695 i.tm.opcode_modifier.evex = EVEX128;
10696 else
10697 i.tm.opcode_modifier.vex = VEX128;
10698 break;
10699
10700 case '1':
10701 if (evex)
10702 i.tm.opcode_modifier.evex = EVEX256;
10703 else
10704 i.tm.opcode_modifier.vex = VEX256;
10705 break;
10706
10707 case '2':
10708 if (evex)
10709 i.tm.opcode_modifier.evex = EVEX512;
10710 break;
10711
10712 case '3':
10713 if (evex)
10714 i.tm.opcode_modifier.evex = EVEX_L3;
10715 break;
10716
10717 case 'I':
10718 if (line[3] == 'G')
10719 {
10720 if (evex)
10721 i.tm.opcode_modifier.evex = EVEXLIG;
10722 else
10723 i.tm.opcode_modifier.vex = VEXScalar; /* LIG */
10724 ++line;
10725 }
10726 break;
10727 }
10728
10729 if (i.tm.opcode_modifier.vex || i.tm.opcode_modifier.evex)
10730 line += 3;
10731 break;
10732
10733 case '1':
10734 if (line[2] == '2' && line[3] == '8')
10735 {
10736 if (evex)
10737 i.tm.opcode_modifier.evex = EVEX128;
10738 else
10739 i.tm.opcode_modifier.vex = VEX128;
10740 line += 4;
10741 }
10742 break;
10743
10744 case '2':
10745 if (line[2] == '5' && line[3] == '6')
10746 {
10747 if (evex)
10748 i.tm.opcode_modifier.evex = EVEX256;
10749 else
10750 i.tm.opcode_modifier.vex = VEX256;
10751 line += 4;
10752 }
10753 break;
10754
10755 case '5':
10756 if (evex && line[2] == '1' && line[3] == '2')
10757 {
10758 i.tm.opcode_modifier.evex = EVEX512;
10759 line += 4;
10760 }
10761 break;
10762 }
10763 }
10764
10765 if (line > end && *line == '.')
10766 {
10767 /* embedded prefix (VEX.pp, XOP.pp, EVEX.pp). */
10768 switch (line[1])
10769 {
10770 case 'N':
10771 if (line[2] == 'P')
10772 line += 3;
10773 break;
10774
10775 case '6':
10776 if (line[2] == '6')
10777 {
10778 i.tm.opcode_modifier.opcodeprefix = PREFIX_0X66;
10779 line += 3;
10780 }
10781 break;
10782
10783 case 'F': case 'f':
10784 if (line[2] == '3')
10785 {
10786 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
10787 line += 3;
10788 }
10789 else if (line[2] == '2')
10790 {
10791 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF2;
10792 line += 3;
10793 }
10794 break;
10795 }
10796 }
10797
10798 if (line > end && !xop && *line == '.')
10799 {
10800 /* Encoding space (VEX.mmmmm, EVEX.mmmm). */
10801 switch (line[1])
10802 {
10803 case '0':
10804 if (TOUPPER (line[2]) != 'F')
10805 break;
10806 if (line[3] == '.' || is_space_char (line[3]))
10807 {
10808 i.insn_opcode_space = SPACE_0F;
10809 line += 3;
10810 }
10811 else if (line[3] == '3'
10812 && (line[4] == '8' || TOUPPER (line[4]) == 'A')
10813 && (line[5] == '.' || is_space_char (line[5])))
10814 {
10815 i.insn_opcode_space = line[4] == '8' ? SPACE_0F38 : SPACE_0F3A;
10816 line += 5;
10817 }
10818 break;
10819
10820 case 'M':
10821 if (ISDIGIT (line[2]) && line[2] != '0')
10822 {
10823 char *e;
10824 unsigned long n = strtoul (line + 2, &e, 10);
10825
10826 if (n <= (evex ? 15 : 31)
10827 && (*e == '.' || is_space_char (*e)))
10828 {
10829 i.insn_opcode_space = n;
10830 line = e;
10831 }
10832 }
10833 break;
10834 }
10835 }
10836
10837 if (line > end && *line == '.' && line[1] == 'W')
10838 {
10839 /* VEX.W, XOP.W, EVEX.W */
10840 switch (line[2])
10841 {
10842 case '0':
10843 i.tm.opcode_modifier.vexw = VEXW0;
10844 break;
10845
10846 case '1':
10847 i.tm.opcode_modifier.vexw = VEXW1;
10848 break;
10849
10850 case 'I':
10851 if (line[3] == 'G')
10852 {
10853 i.tm.opcode_modifier.vexw = VEXWIG;
10854 ++line;
10855 }
10856 break;
10857 }
10858
10859 if (i.tm.opcode_modifier.vexw)
10860 line += 3;
10861 }
10862
10863 if (line > end && *line && !is_space_char (*line))
10864 {
10865 /* Improve diagnostic a little. */
10866 if (*line == '.' && line[1] && !is_space_char (line[1]))
10867 ++line;
10868 goto done;
edd67638
JB
10869 }
10870
393fbe8d
JB
10871 /* Before processing the opcode expression, find trailing "+r" or
10872 "/<digit>" specifiers. */
10873 for (ptr = line; ; ++ptr)
10874 {
10875 unsigned long n;
10876 char *e;
10877
10878 ptr = strpbrk (ptr, "+/,");
10879 if (ptr == NULL || *ptr == ',')
10880 break;
10881
10882 if (*ptr == '+' && ptr[1] == 'r'
10883 && (ptr[2] == ',' || (is_space_char (ptr[2]) && ptr[3] == ',')))
10884 {
10885 *ptr = ' ';
10886 ptr[1] = ' ';
10887 i.short_form = true;
10888 break;
10889 }
10890
10891 if (*ptr == '/' && ISDIGIT (ptr[1])
10892 && (n = strtoul (ptr + 1, &e, 8)) < 8
10893 && e == ptr + 2
10894 && (ptr[2] == ',' || (is_space_char (ptr[2]) && ptr[3] == ',')))
10895 {
10896 *ptr = ' ';
10897 ptr[1] = ' ';
10898 i.tm.extension_opcode = n;
10899 i.tm.opcode_modifier.modrm = 1;
10900 break;
10901 }
10902 }
10903
edd67638
JB
10904 input_line_pointer = line;
10905 val = get_absolute_expression ();
10906 line = input_line_pointer;
10907
393fbe8d
JB
10908 if (i.short_form && (val & 7))
10909 as_warn ("`+r' assumes low three opcode bits to be clear");
10910
edd67638
JB
10911 for (j = 1; j < sizeof(val); ++j)
10912 if (!(val >> (j * 8)))
10913 break;
10914
10915 /* Trim off a prefix if present. */
10916 if (j > 1 && !vex && !xop && !evex)
10917 {
10918 uint8_t byte = val >> ((j - 1) * 8);
10919
10920 switch (byte)
10921 {
10922 case DATA_PREFIX_OPCODE:
10923 case REPE_PREFIX_OPCODE:
10924 case REPNE_PREFIX_OPCODE:
10925 if (!add_prefix (byte))
10926 goto bad;
10927 val &= ((uint64_t)1 << (--j * 8)) - 1;
10928 break;
10929 }
10930 }
10931
10932 /* Trim off encoding space. */
d0c2e3ec 10933 if (j > 1 && !i.insn_opcode_space && (val >> ((j - 1) * 8)) == 0x0f)
edd67638
JB
10934 {
10935 uint8_t byte = val >> ((--j - 1) * 8);
10936
d0c2e3ec 10937 i.insn_opcode_space = SPACE_0F;
edd67638
JB
10938 switch (byte & -(j > 1))
10939 {
10940 case 0x38:
d0c2e3ec 10941 i.insn_opcode_space = SPACE_0F38;
edd67638
JB
10942 --j;
10943 break;
10944 case 0x3a:
d0c2e3ec 10945 i.insn_opcode_space = SPACE_0F3A;
edd67638
JB
10946 --j;
10947 break;
10948 }
d0c2e3ec 10949 i.tm.opcode_space = i.insn_opcode_space;
edd67638
JB
10950 val &= ((uint64_t)1 << (j * 8)) - 1;
10951 }
d0c2e3ec
JB
10952 if (!i.tm.opcode_space && (vex || evex))
10953 /* Arrange for build_vex_prefix() to properly emit 0xC4/0xC5.
10954 Also avoid hitting abort() there or in build_evex_prefix(). */
10955 i.tm.opcode_space = i.insn_opcode_space == SPACE_0F ? SPACE_0F
10956 : SPACE_0F38;
edd67638
JB
10957
10958 if (j > 2)
10959 {
10960 as_bad (_("opcode residual (%#"PRIx64") too wide"), (uint64_t) val);
10961 goto bad;
10962 }
10963 i.opcode_length = j;
0ff3b7d0
JB
10964
10965 /* Handle operands, if any. */
10966 if (*line == ',')
10967 {
10968 i386_operand_type combined;
f7377a91 10969 expressionS *disp_exp = NULL;
0ff3b7d0
JB
10970 bool changed;
10971
f7377a91
JB
10972 i.memshift = -1;
10973
0ff3b7d0
JB
10974 ptr = parse_operands (line + 1, &i386_mnemonics[MN__insn]);
10975 this_operand = -1;
10976 if (!ptr)
10977 goto bad;
10978 line = ptr;
10979
10980 if (!i.operands)
10981 {
10982 as_bad (_("expecting operand after ','; got nothing"));
10983 goto done;
10984 }
10985
10986 if (i.mem_operands > 1)
10987 {
10988 as_bad (_("too many memory references for `%s'"),
10989 &i386_mnemonics[MN__insn]);
10990 goto done;
10991 }
10992
10993 /* Are we to emit ModR/M encoding? */
10994 if (!i.short_form
10995 && (i.mem_operands
10996 || i.reg_operands > (i.vec_encoding != vex_encoding_default)
10997 || i.tm.extension_opcode != None))
10998 i.tm.opcode_modifier.modrm = 1;
10999
11000 if (!i.tm.opcode_modifier.modrm
11001 && (i.reg_operands
11002 > i.short_form + 0U + (i.vec_encoding != vex_encoding_default)
11003 || i.mem_operands))
11004 {
11005 as_bad (_("too many register/memory operands"));
11006 goto done;
11007 }
11008
11009 /* Enforce certain constraints on operands. */
11010 switch (i.reg_operands + i.mem_operands
11011 + (i.tm.extension_opcode != None))
11012 {
11013 case 0:
11014 if (i.short_form)
11015 {
11016 as_bad (_("too few register/memory operands"));
11017 goto done;
11018 }
11019 /* Fall through. */
11020 case 1:
11021 if (i.tm.opcode_modifier.modrm)
11022 {
11023 as_bad (_("too few register/memory operands"));
11024 goto done;
11025 }
11026 break;
11027
11028 case 2:
11029 break;
11030
11031 case 4:
11032 if (i.imm_operands
11033 && (i.op[0].imms->X_op != O_constant
11034 || !fits_in_imm4 (i.op[0].imms->X_add_number)))
11035 {
11036 as_bad (_("constant doesn't fit in %d bits"), evex ? 3 : 4);
11037 goto done;
11038 }
11039 /* Fall through. */
11040 case 3:
11041 if (i.vec_encoding != vex_encoding_default)
11042 {
11043 i.tm.opcode_modifier.vexvvvv = 1;
11044 break;
11045 }
11046 /* Fall through. */
11047 default:
11048 as_bad (_("too many register/memory operands"));
11049 goto done;
11050 }
11051
11052 /* Bring operands into canonical order (imm, mem, reg). */
11053 do
11054 {
11055 changed = false;
11056
11057 for (j = 1; j < i.operands; ++j)
11058 {
11059 if ((!operand_type_check (i.types[j - 1], imm)
11060 && operand_type_check (i.types[j], imm))
11061 || (i.types[j - 1].bitfield.class != ClassNone
11062 && i.types[j].bitfield.class == ClassNone))
11063 {
11064 swap_2_operands (j - 1, j);
11065 changed = true;
11066 }
11067 }
11068 }
11069 while (changed);
11070
11071 /* For Intel syntax swap the order of register operands. */
11072 if (intel_syntax)
11073 switch (i.reg_operands)
11074 {
11075 case 0:
11076 case 1:
11077 break;
11078
11079 case 4:
11080 swap_2_operands (i.imm_operands + i.mem_operands + 1, i.operands - 2);
11081 /* Fall through. */
11082 case 3:
11083 case 2:
11084 swap_2_operands (i.imm_operands + i.mem_operands, i.operands - 1);
11085 break;
11086
11087 default:
11088 abort ();
11089 }
11090
11091 /* Enforce constraints when using VSIB. */
11092 if (i.index_reg
11093 && (i.index_reg->reg_type.bitfield.xmmword
11094 || i.index_reg->reg_type.bitfield.ymmword
11095 || i.index_reg->reg_type.bitfield.zmmword))
11096 {
11097 if (i.vec_encoding == vex_encoding_default)
11098 {
11099 as_bad (_("VSIB unavailable with legacy encoding"));
11100 goto done;
11101 }
11102
11103 if (i.vec_encoding == vex_encoding_evex
11104 && i.reg_operands > 1)
11105 {
11106 /* We could allow two register operands, encoding the 2nd one in
11107 an 8-bit immediate like for 4-register-operand insns, but that
11108 would require ugly fiddling with process_operands() and/or
11109 build_modrm_byte(). */
11110 as_bad (_("too many register operands with VSIB"));
11111 goto done;
11112 }
11113
11114 i.tm.opcode_modifier.sib = 1;
11115 }
11116
11117 /* Establish operand size encoding. */
11118 operand_type_set (&combined, 0);
f7377a91 11119
0ff3b7d0
JB
11120 for (j = i.imm_operands; j < i.operands; ++j)
11121 {
11122 i.types[j].bitfield.instance = InstanceNone;
11123
11124 if (operand_type_check (i.types[j], disp))
f7377a91
JB
11125 {
11126 i.types[j].bitfield.baseindex = 1;
11127 disp_exp = i.op[j].disps;
11128 }
11129
11130 if (evex && i.types[j].bitfield.baseindex)
11131 {
11132 unsigned int n = i.memshift;
11133
11134 if (i.types[j].bitfield.byte)
11135 n = 0;
11136 else if (i.types[j].bitfield.word)
11137 n = 1;
11138 else if (i.types[j].bitfield.dword)
11139 n = 2;
11140 else if (i.types[j].bitfield.qword)
11141 n = 3;
11142 else if (i.types[j].bitfield.xmmword)
11143 n = 4;
11144 else if (i.types[j].bitfield.ymmword)
11145 n = 5;
11146 else if (i.types[j].bitfield.zmmword)
11147 n = 6;
11148
11149 if (i.memshift < 32 && n != i.memshift)
11150 as_warn ("conflicting memory operand size specifiers");
11151 i.memshift = n;
11152 }
0ff3b7d0
JB
11153
11154 if ((i.broadcast.type || i.broadcast.bytes)
11155 && j == i.broadcast.operand)
11156 continue;
11157
11158 combined = operand_type_or (combined, i.types[j]);
11159 combined.bitfield.class = ClassNone;
11160 }
11161
f7377a91
JB
11162 switch ((i.broadcast.type ? i.broadcast.type : 1)
11163 << (i.memshift < 32 ? i.memshift : 0))
11164 {
11165 case 64: combined.bitfield.zmmword = 1; break;
11166 case 32: combined.bitfield.ymmword = 1; break;
11167 case 16: combined.bitfield.xmmword = 1; break;
11168 case 8: combined.bitfield.qword = 1; break;
11169 case 4: combined.bitfield.dword = 1; break;
11170 }
11171
0ff3b7d0
JB
11172 if (i.vec_encoding == vex_encoding_default)
11173 {
11174 if (flag_code == CODE_64BIT && combined.bitfield.qword)
11175 i.rex |= REX_W;
11176 else if ((flag_code == CODE_16BIT ? combined.bitfield.dword
11177 : combined.bitfield.word)
11178 && !add_prefix (DATA_PREFIX_OPCODE))
11179 goto done;
11180 }
11181 else if (!i.tm.opcode_modifier.vexw)
11182 {
11183 if (flag_code == CODE_64BIT)
11184 {
11185 if (combined.bitfield.qword)
11186 i.tm.opcode_modifier.vexw = VEXW1;
11187 else if (combined.bitfield.dword)
11188 i.tm.opcode_modifier.vexw = VEXW0;
11189 }
11190
11191 if (!i.tm.opcode_modifier.vexw)
11192 i.tm.opcode_modifier.vexw = VEXWIG;
11193 }
11194
11195 if (vex || xop)
11196 {
11197 if (!i.tm.opcode_modifier.vex)
11198 {
11199 if (combined.bitfield.ymmword)
11200 i.tm.opcode_modifier.vex = VEX256;
11201 else if (combined.bitfield.xmmword)
11202 i.tm.opcode_modifier.vex = VEX128;
11203 }
11204 }
11205 else if (evex)
11206 {
11207 if (!i.tm.opcode_modifier.evex)
11208 {
11209 /* Do _not_ consider AVX512VL here. */
11210 if (i.rounding.type != rc_none || combined.bitfield.zmmword)
11211 i.tm.opcode_modifier.evex = EVEX512;
11212 else if (combined.bitfield.ymmword)
11213 i.tm.opcode_modifier.evex = EVEX256;
11214 else if (combined.bitfield.xmmword)
11215 i.tm.opcode_modifier.evex = EVEX128;
11216 }
f7377a91
JB
11217
11218 if (i.memshift >= 32)
11219 {
11220 unsigned int n = 0;
11221
11222 switch (i.tm.opcode_modifier.evex)
11223 {
11224 case EVEX512: n = 64; break;
11225 case EVEX256: n = 32; break;
11226 case EVEX128: n = 16; break;
11227 }
11228
11229 if (i.broadcast.type)
11230 n /= i.broadcast.type;
11231
11232 if (n > 0)
11233 for (i.memshift = 0; !(n & 1); n >>= 1)
11234 ++i.memshift;
11235 else if (disp_exp != NULL && disp_exp->X_op == O_constant
11236 && disp_exp->X_add_number != 0
11237 && i.disp_encoding != disp_encoding_32bit)
11238 {
11239 if (!quiet_warnings)
11240 as_warn ("cannot determine memory operand size");
11241 i.disp_encoding = disp_encoding_32bit;
11242 }
11243 }
0ff3b7d0
JB
11244 }
11245
f7377a91
JB
11246 if (i.memshift >= 32)
11247 i.memshift = 0;
11248 else if (!evex)
11249 i.vec_encoding = vex_encoding_error;
11250
0ff3b7d0
JB
11251 if (i.disp_operands && !optimize_disp (&i.tm))
11252 goto done;
11253
c032bc4f
JB
11254 /* Establish size for immediate operands. */
11255 for (j = 0; j < i.imm_operands; ++j)
11256 {
11257 expressionS *expP = i.op[j].imms;
11258
11259 gas_assert (operand_type_check (i.types[j], imm));
11260 operand_type_set (&i.types[j], 0);
11261
11262 if (i.imm_bits[j] > 32)
11263 i.types[j].bitfield.imm64 = 1;
11264 else if (i.imm_bits[j] > 16)
11265 {
11266 if (flag_code == CODE_64BIT && (i.flags[j] & Operand_Signed))
11267 i.types[j].bitfield.imm32s = 1;
11268 else
11269 i.types[j].bitfield.imm32 = 1;
11270 }
11271 else if (i.imm_bits[j] > 8)
11272 i.types[j].bitfield.imm16 = 1;
11273 else if (i.imm_bits[j] > 0)
11274 {
11275 if (i.flags[j] & Operand_Signed)
11276 i.types[j].bitfield.imm8s = 1;
11277 else
11278 i.types[j].bitfield.imm8 = 1;
11279 }
11280 else if (expP->X_op == O_constant)
11281 {
11282 i.types[j] = smallest_imm_type (expP->X_add_number);
11283 i.types[j].bitfield.imm1 = 0;
11284 /* Oddly enough imm_size() checks imm64 first, so the bit needs
11285 zapping since smallest_imm_type() sets it unconditionally. */
11286 if (flag_code != CODE_64BIT)
11287 {
11288 i.types[j].bitfield.imm64 = 0;
11289 i.types[j].bitfield.imm32s = 0;
11290 i.types[j].bitfield.imm32 = 1;
11291 }
11292 else if (i.types[j].bitfield.imm32 || i.types[j].bitfield.imm32s)
11293 i.types[j].bitfield.imm64 = 0;
11294 }
11295 else
11296 /* Non-constant expressions are sized heuristically. */
11297 switch (flag_code)
11298 {
11299 case CODE_64BIT: i.types[j].bitfield.imm32s = 1; break;
11300 case CODE_32BIT: i.types[j].bitfield.imm32 = 1; break;
11301 case CODE_16BIT: i.types[j].bitfield.imm16 = 1; break;
11302 }
11303 }
11304
0ff3b7d0
JB
11305 for (j = 0; j < i.operands; ++j)
11306 i.tm.operand_types[j] = i.types[j];
11307
11308 process_operands ();
11309 }
11310
11311 /* Don't set opcode until after processing operands, to avoid any
11312 potential special casing there. */
11313 i.tm.base_opcode |= val;
11314
11315 if (i.vec_encoding == vex_encoding_error
11316 || (i.vec_encoding != vex_encoding_evex
11317 ? i.broadcast.type || i.broadcast.bytes
11318 || i.rounding.type != rc_none
11319 || i.mask.reg
11320 : (i.broadcast.type || i.broadcast.bytes)
11321 && i.rounding.type != rc_none))
11322 {
11323 as_bad (_("conflicting .insn operands"));
11324 goto done;
11325 }
edd67638 11326
d0c2e3ec
JB
11327 if (vex || xop)
11328 {
11329 if (!i.tm.opcode_modifier.vex)
11330 i.tm.opcode_modifier.vex = VEXScalar; /* LIG */
11331
11332 build_vex_prefix (NULL);
11333 i.rex &= REX_OPCODE;
11334 }
11335 else if (evex)
11336 {
11337 if (!i.tm.opcode_modifier.evex)
11338 i.tm.opcode_modifier.evex = EVEXLIG;
11339
11340 build_evex_prefix ();
11341 i.rex &= REX_OPCODE;
11342 }
0ff3b7d0
JB
11343 else if (i.rex != 0)
11344 add_prefix (REX_OPCODE | i.rex);
d0c2e3ec 11345
edd67638
JB
11346 output_insn ();
11347
d0c2e3ec 11348 done:
edd67638
JB
11349 *saved_ilp = saved_char;
11350 input_line_pointer = line;
11351
11352 demand_empty_rest_of_line ();
d0c2e3ec
JB
11353
11354 /* Make sure dot_insn() won't yield "true" anymore. */
11355 i.tm.mnem_off = 0;
edd67638
JB
11356}
11357
d182319b 11358#ifdef TE_PE
6482c264 11359static void
7016a5d5 11360pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
6482c264
NC
11361{
11362 expressionS exp;
11363
11364 do
11365 {
11366 expression (&exp);
11367 if (exp.X_op == O_symbol)
11368 exp.X_op = O_secrel;
11369
11370 emit_expr (&exp, 4);
11371 }
11372 while (*input_line_pointer++ == ',');
11373
11374 input_line_pointer--;
11375 demand_empty_rest_of_line ();
11376}
145667f8
MH
11377
11378static void
11379pe_directive_secidx (int dummy ATTRIBUTE_UNUSED)
11380{
11381 expressionS exp;
11382
11383 do
11384 {
11385 expression (&exp);
11386 if (exp.X_op == O_symbol)
11387 exp.X_op = O_secidx;
11388
11389 emit_expr (&exp, 2);
11390 }
11391 while (*input_line_pointer++ == ',');
11392
11393 input_line_pointer--;
11394 demand_empty_rest_of_line ();
11395}
6482c264
NC
11396#endif
11397
7063667e
JB
11398/* Handle Rounding Control / SAE specifiers. */
11399
11400static char *
11401RC_SAE_specifier (const char *pstr)
11402{
11403 unsigned int j;
11404
11405 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); j++)
11406 {
11407 if (!strncmp (pstr, RC_NamesTable[j].name, RC_NamesTable[j].len))
11408 {
11409 if (i.rounding.type != rc_none)
11410 {
11411 as_bad (_("duplicated `{%s}'"), RC_NamesTable[j].name);
11412 return NULL;
11413 }
11414
11415 i.rounding.type = RC_NamesTable[j].type;
11416
11417 return (char *)(pstr + RC_NamesTable[j].len);
11418 }
11419 }
11420
11421 return NULL;
11422}
11423
43234a1e
L
11424/* Handle Vector operations. */
11425
11426static char *
f70c6814 11427check_VecOperations (char *op_string)
43234a1e
L
11428{
11429 const reg_entry *mask;
11430 const char *saved;
11431 char *end_op;
11432
f70c6814 11433 while (*op_string)
43234a1e
L
11434 {
11435 saved = op_string;
11436 if (*op_string == '{')
11437 {
11438 op_string++;
11439
11440 /* Check broadcasts. */
d34049e8 11441 if (startswith (op_string, "1to"))
43234a1e 11442 {
5273a3cd 11443 unsigned int bcst_type;
43234a1e 11444
5273a3cd 11445 if (i.broadcast.type)
43234a1e
L
11446 goto duplicated_vec_op;
11447
11448 op_string += 3;
11449 if (*op_string == '8')
8e6e0792 11450 bcst_type = 8;
b28d1bda 11451 else if (*op_string == '4')
8e6e0792 11452 bcst_type = 4;
b28d1bda 11453 else if (*op_string == '2')
8e6e0792 11454 bcst_type = 2;
43234a1e
L
11455 else if (*op_string == '1'
11456 && *(op_string+1) == '6')
11457 {
8e6e0792 11458 bcst_type = 16;
43234a1e
L
11459 op_string++;
11460 }
0cc78721
CL
11461 else if (*op_string == '3'
11462 && *(op_string+1) == '2')
11463 {
11464 bcst_type = 32;
11465 op_string++;
11466 }
43234a1e
L
11467 else
11468 {
11469 as_bad (_("Unsupported broadcast: `%s'"), saved);
11470 return NULL;
11471 }
11472 op_string++;
11473
5273a3cd
JB
11474 i.broadcast.type = bcst_type;
11475 i.broadcast.operand = this_operand;
f7377a91
JB
11476
11477 /* For .insn a data size specifier may be appended. */
11478 if (dot_insn () && *op_string == ':')
11479 goto dot_insn_modifier;
11480 }
11481 /* Check .insn special cases. */
11482 else if (dot_insn () && *op_string == ':')
11483 {
11484 dot_insn_modifier:
c032bc4f 11485 switch (op_string[1])
f7377a91
JB
11486 {
11487 unsigned long n;
11488
c032bc4f 11489 case 'd':
f7377a91
JB
11490 if (i.memshift < 32)
11491 goto duplicated_vec_op;
11492
11493 n = strtoul (op_string + 2, &end_op, 0);
11494 if (n)
11495 for (i.memshift = 0; !(n & 1); n >>= 1)
11496 ++i.memshift;
11497 if (i.memshift < 32 && n == 1)
11498 op_string = end_op;
c032bc4f
JB
11499 break;
11500
11501 case 's': case 'u':
11502 /* This isn't really a "vector" operation, but a sign/size
11503 specifier for immediate operands of .insn. Note that AT&T
11504 syntax handles the same in i386_immediate(). */
11505 if (!intel_syntax)
11506 break;
11507
11508 if (i.imm_bits[this_operand])
11509 goto duplicated_vec_op;
11510
11511 n = strtoul (op_string + 2, &end_op, 0);
11512 if (n && n <= (flag_code == CODE_64BIT ? 64 : 32))
11513 {
11514 i.imm_bits[this_operand] = n;
11515 if (op_string[1] == 's')
11516 i.flags[this_operand] |= Operand_Signed;
11517 op_string = end_op;
11518 }
11519 break;
f7377a91 11520 }
43234a1e
L
11521 }
11522 /* Check masking operation. */
11523 else if ((mask = parse_register (op_string, &end_op)) != NULL)
11524 {
8a6fb3f9
JB
11525 if (mask == &bad_reg)
11526 return NULL;
11527
43234a1e 11528 /* k0 can't be used for write mask. */
f74a6307 11529 if (mask->reg_type.bitfield.class != RegMask || !mask->reg_num)
43234a1e 11530 {
6d2cd6b2
JB
11531 as_bad (_("`%s%s' can't be used for write mask"),
11532 register_prefix, mask->reg_name);
43234a1e
L
11533 return NULL;
11534 }
11535
6225c532 11536 if (!i.mask.reg)
43234a1e 11537 {
6225c532
JB
11538 i.mask.reg = mask;
11539 i.mask.operand = this_operand;
43234a1e 11540 }
6225c532
JB
11541 else if (i.mask.reg->reg_num)
11542 goto duplicated_vec_op;
43234a1e
L
11543 else
11544 {
6225c532 11545 i.mask.reg = mask;
43234a1e
L
11546
11547 /* Only "{z}" is allowed here. No need to check
11548 zeroing mask explicitly. */
6225c532 11549 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
11550 {
11551 as_bad (_("invalid write mask `%s'"), saved);
11552 return NULL;
11553 }
11554 }
11555
11556 op_string = end_op;
11557 }
11558 /* Check zeroing-flag for masking operation. */
11559 else if (*op_string == 'z')
11560 {
6225c532 11561 if (!i.mask.reg)
43234a1e 11562 {
6225c532
JB
11563 i.mask.reg = reg_k0;
11564 i.mask.zeroing = 1;
11565 i.mask.operand = this_operand;
43234a1e
L
11566 }
11567 else
11568 {
6225c532 11569 if (i.mask.zeroing)
43234a1e
L
11570 {
11571 duplicated_vec_op:
11572 as_bad (_("duplicated `%s'"), saved);
11573 return NULL;
11574 }
11575
6225c532 11576 i.mask.zeroing = 1;
43234a1e
L
11577
11578 /* Only "{%k}" is allowed here. No need to check mask
11579 register explicitly. */
6225c532 11580 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
11581 {
11582 as_bad (_("invalid zeroing-masking `%s'"),
11583 saved);
11584 return NULL;
11585 }
11586 }
11587
11588 op_string++;
11589 }
7063667e
JB
11590 else if (intel_syntax
11591 && (op_string = RC_SAE_specifier (op_string)) != NULL)
11592 i.rounding.modifier = true;
43234a1e
L
11593 else
11594 goto unknown_vec_op;
11595
11596 if (*op_string != '}')
11597 {
11598 as_bad (_("missing `}' in `%s'"), saved);
11599 return NULL;
11600 }
11601 op_string++;
0ba3a731
L
11602
11603 /* Strip whitespace since the addition of pseudo prefixes
11604 changed how the scrubber treats '{'. */
11605 if (is_space_char (*op_string))
11606 ++op_string;
11607
43234a1e
L
11608 continue;
11609 }
11610 unknown_vec_op:
11611 /* We don't know this one. */
11612 as_bad (_("unknown vector operation: `%s'"), saved);
11613 return NULL;
11614 }
11615
6225c532 11616 if (i.mask.reg && i.mask.zeroing && !i.mask.reg->reg_num)
6d2cd6b2
JB
11617 {
11618 as_bad (_("zeroing-masking only allowed with write mask"));
11619 return NULL;
11620 }
11621
43234a1e
L
11622 return op_string;
11623}
11624
252b5132 11625static int
70e41ade 11626i386_immediate (char *imm_start)
252b5132
RH
11627{
11628 char *save_input_line_pointer;
f3c180ae 11629 char *gotfree_input_line;
252b5132 11630 segT exp_seg = 0;
47926f60 11631 expressionS *exp;
40fb9820
L
11632 i386_operand_type types;
11633
0dfbf9d7 11634 operand_type_set (&types, ~0);
252b5132
RH
11635
11636 if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
11637 {
31b2323c
L
11638 as_bad (_("at most %d immediate operands are allowed"),
11639 MAX_IMMEDIATE_OPERANDS);
252b5132
RH
11640 return 0;
11641 }
11642
11643 exp = &im_expressions[i.imm_operands++];
520dc8e8 11644 i.op[this_operand].imms = exp;
252b5132
RH
11645
11646 if (is_space_char (*imm_start))
11647 ++imm_start;
11648
11649 save_input_line_pointer = input_line_pointer;
11650 input_line_pointer = imm_start;
11651
d258b828 11652 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
11653 if (gotfree_input_line)
11654 input_line_pointer = gotfree_input_line;
252b5132 11655
5cc00775 11656 expr_mode = expr_operator_none;
252b5132
RH
11657 exp_seg = expression (exp);
11658
c032bc4f
JB
11659 /* For .insn immediates there may be a size specifier. */
11660 if (dot_insn () && *input_line_pointer == '{' && input_line_pointer[1] == ':'
11661 && (input_line_pointer[2] == 's' || input_line_pointer[2] == 'u'))
11662 {
11663 char *e;
11664 unsigned long n = strtoul (input_line_pointer + 3, &e, 0);
11665
11666 if (*e == '}' && n && n <= (flag_code == CODE_64BIT ? 64 : 32))
11667 {
11668 i.imm_bits[this_operand] = n;
11669 if (input_line_pointer[2] == 's')
11670 i.flags[this_operand] |= Operand_Signed;
11671 input_line_pointer = e + 1;
11672 }
11673 }
11674
83183c0c 11675 SKIP_WHITESPACE ();
252b5132 11676 if (*input_line_pointer)
f3c180ae 11677 as_bad (_("junk `%s' after expression"), input_line_pointer);
252b5132
RH
11678
11679 input_line_pointer = save_input_line_pointer;
f3c180ae 11680 if (gotfree_input_line)
ee86248c
JB
11681 {
11682 free (gotfree_input_line);
11683
9aac24b1 11684 if (exp->X_op == O_constant)
ee86248c
JB
11685 exp->X_op = O_illegal;
11686 }
11687
9aac24b1
JB
11688 if (exp_seg == reg_section)
11689 {
11690 as_bad (_("illegal immediate register operand %s"), imm_start);
11691 return 0;
11692 }
11693
ee86248c
JB
11694 return i386_finalize_immediate (exp_seg, exp, types, imm_start);
11695}
252b5132 11696
ee86248c
JB
11697static int
11698i386_finalize_immediate (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
11699 i386_operand_type types, const char *imm_start)
11700{
11701 if (exp->X_op == O_absent || exp->X_op == O_illegal || exp->X_op == O_big)
252b5132 11702 {
313c53d1
L
11703 if (imm_start)
11704 as_bad (_("missing or invalid immediate expression `%s'"),
11705 imm_start);
3992d3b7 11706 return 0;
252b5132 11707 }
3e73aa7c 11708 else if (exp->X_op == O_constant)
252b5132 11709 {
47926f60 11710 /* Size it properly later. */
40fb9820 11711 i.types[this_operand].bitfield.imm64 = 1;
a442cac5
JB
11712
11713 /* If not 64bit, sign/zero extend val, to account for wraparound
11714 when !BFD64. */
5cc00775
JB
11715 if (expr_mode == expr_operator_present
11716 && flag_code != CODE_64BIT && !object_64bit)
a442cac5 11717 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
252b5132 11718 }
4c63da97 11719#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
f86103b7 11720 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
31312f95 11721 && exp_seg != absolute_section
47926f60 11722 && exp_seg != text_section
24eab124
AM
11723 && exp_seg != data_section
11724 && exp_seg != bss_section
11725 && exp_seg != undefined_section
f86103b7 11726 && !bfd_is_com_section (exp_seg))
252b5132 11727 {
d0b47220 11728 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
252b5132
RH
11729 return 0;
11730 }
11731#endif
11732 else
11733 {
11734 /* This is an address. The size of the address will be
24eab124 11735 determined later, depending on destination register,
3e73aa7c 11736 suffix, or the default for the section. */
40fb9820
L
11737 i.types[this_operand].bitfield.imm8 = 1;
11738 i.types[this_operand].bitfield.imm16 = 1;
11739 i.types[this_operand].bitfield.imm32 = 1;
11740 i.types[this_operand].bitfield.imm32s = 1;
11741 i.types[this_operand].bitfield.imm64 = 1;
c6fb90c8
L
11742 i.types[this_operand] = operand_type_and (i.types[this_operand],
11743 types);
252b5132
RH
11744 }
11745
11746 return 1;
11747}
11748
551c1ca1 11749static char *
e3bb37b5 11750i386_scale (char *scale)
252b5132 11751{
551c1ca1
AM
11752 offsetT val;
11753 char *save = input_line_pointer;
252b5132 11754
551c1ca1
AM
11755 input_line_pointer = scale;
11756 val = get_absolute_expression ();
11757
11758 switch (val)
252b5132 11759 {
551c1ca1 11760 case 1:
252b5132
RH
11761 i.log2_scale_factor = 0;
11762 break;
551c1ca1 11763 case 2:
252b5132
RH
11764 i.log2_scale_factor = 1;
11765 break;
551c1ca1 11766 case 4:
252b5132
RH
11767 i.log2_scale_factor = 2;
11768 break;
551c1ca1 11769 case 8:
252b5132
RH
11770 i.log2_scale_factor = 3;
11771 break;
11772 default:
a724f0f4
JB
11773 {
11774 char sep = *input_line_pointer;
11775
11776 *input_line_pointer = '\0';
11777 as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
11778 scale);
11779 *input_line_pointer = sep;
11780 input_line_pointer = save;
11781 return NULL;
11782 }
252b5132 11783 }
29b0f896 11784 if (i.log2_scale_factor != 0 && i.index_reg == 0)
252b5132
RH
11785 {
11786 as_warn (_("scale factor of %d without an index register"),
24eab124 11787 1 << i.log2_scale_factor);
252b5132 11788 i.log2_scale_factor = 0;
252b5132 11789 }
551c1ca1
AM
11790 scale = input_line_pointer;
11791 input_line_pointer = save;
11792 return scale;
252b5132
RH
11793}
11794
252b5132 11795static int
e3bb37b5 11796i386_displacement (char *disp_start, char *disp_end)
252b5132 11797{
29b0f896 11798 expressionS *exp;
252b5132
RH
11799 segT exp_seg = 0;
11800 char *save_input_line_pointer;
f3c180ae 11801 char *gotfree_input_line;
40fb9820
L
11802 int override;
11803 i386_operand_type bigdisp, types = anydisp;
3992d3b7 11804 int ret;
252b5132 11805
31b2323c
L
11806 if (i.disp_operands == MAX_MEMORY_OPERANDS)
11807 {
11808 as_bad (_("at most %d displacement operands are allowed"),
11809 MAX_MEMORY_OPERANDS);
11810 return 0;
11811 }
11812
0dfbf9d7 11813 operand_type_set (&bigdisp, 0);
6f2f06be 11814 if (i.jumpabsolute
48bcea9f 11815 || i.types[this_operand].bitfield.baseindex
0cfa3eb3
JB
11816 || (current_templates->start->opcode_modifier.jump != JUMP
11817 && current_templates->start->opcode_modifier.jump != JUMP_DWORD))
e05278af 11818 {
48bcea9f 11819 i386_addressing_mode ();
e05278af 11820 override = (i.prefix[ADDR_PREFIX] != 0);
40fb9820
L
11821 if (flag_code == CODE_64BIT)
11822 {
a775efc8 11823 bigdisp.bitfield.disp32 = 1;
40fb9820 11824 if (!override)
a775efc8 11825 bigdisp.bitfield.disp64 = 1;
40fb9820
L
11826 }
11827 else if ((flag_code == CODE_16BIT) ^ override)
40fb9820 11828 bigdisp.bitfield.disp16 = 1;
48bcea9f
JB
11829 else
11830 bigdisp.bitfield.disp32 = 1;
e05278af
JB
11831 }
11832 else
11833 {
376cd056
JB
11834 /* For PC-relative branches, the width of the displacement may be
11835 dependent upon data size, but is never dependent upon address size.
11836 Also make sure to not unintentionally match against a non-PC-relative
11837 branch template. */
11838 static templates aux_templates;
11839 const insn_template *t = current_templates->start;
5b7c81bd 11840 bool has_intel64 = false;
376cd056
JB
11841
11842 aux_templates.start = t;
11843 while (++t < current_templates->end)
11844 {
11845 if (t->opcode_modifier.jump
11846 != current_templates->start->opcode_modifier.jump)
11847 break;
4b5aaf5f 11848 if ((t->opcode_modifier.isa64 >= INTEL64))
5b7c81bd 11849 has_intel64 = true;
376cd056
JB
11850 }
11851 if (t < current_templates->end)
11852 {
11853 aux_templates.end = t;
11854 current_templates = &aux_templates;
11855 }
11856
e05278af 11857 override = (i.prefix[DATA_PREFIX] != 0);
40fb9820
L
11858 if (flag_code == CODE_64BIT)
11859 {
376cd056
JB
11860 if ((override || i.suffix == WORD_MNEM_SUFFIX)
11861 && (!intel64 || !has_intel64))
40fb9820
L
11862 bigdisp.bitfield.disp16 = 1;
11863 else
a775efc8 11864 bigdisp.bitfield.disp32 = 1;
40fb9820
L
11865 }
11866 else
e05278af
JB
11867 {
11868 if (!override)
11869 override = (i.suffix == (flag_code != CODE_16BIT
11870 ? WORD_MNEM_SUFFIX
11871 : LONG_MNEM_SUFFIX));
40fb9820
L
11872 bigdisp.bitfield.disp32 = 1;
11873 if ((flag_code == CODE_16BIT) ^ override)
11874 {
11875 bigdisp.bitfield.disp32 = 0;
11876 bigdisp.bitfield.disp16 = 1;
11877 }
e05278af 11878 }
e05278af 11879 }
c6fb90c8
L
11880 i.types[this_operand] = operand_type_or (i.types[this_operand],
11881 bigdisp);
252b5132
RH
11882
11883 exp = &disp_expressions[i.disp_operands];
520dc8e8 11884 i.op[this_operand].disps = exp;
252b5132
RH
11885 i.disp_operands++;
11886 save_input_line_pointer = input_line_pointer;
11887 input_line_pointer = disp_start;
11888 END_STRING_AND_SAVE (disp_end);
11889
11890#ifndef GCC_ASM_O_HACK
11891#define GCC_ASM_O_HACK 0
11892#endif
11893#if GCC_ASM_O_HACK
11894 END_STRING_AND_SAVE (disp_end + 1);
40fb9820 11895 if (i.types[this_operand].bitfield.baseIndex
24eab124 11896 && displacement_string_end[-1] == '+')
252b5132
RH
11897 {
11898 /* This hack is to avoid a warning when using the "o"
24eab124
AM
11899 constraint within gcc asm statements.
11900 For instance:
11901
11902 #define _set_tssldt_desc(n,addr,limit,type) \
11903 __asm__ __volatile__ ( \
11904 "movw %w2,%0\n\t" \
11905 "movw %w1,2+%0\n\t" \
11906 "rorl $16,%1\n\t" \
11907 "movb %b1,4+%0\n\t" \
11908 "movb %4,5+%0\n\t" \
11909 "movb $0,6+%0\n\t" \
11910 "movb %h1,7+%0\n\t" \
11911 "rorl $16,%1" \
11912 : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
11913
11914 This works great except that the output assembler ends
11915 up looking a bit weird if it turns out that there is
11916 no offset. You end up producing code that looks like:
11917
11918 #APP
11919 movw $235,(%eax)
11920 movw %dx,2+(%eax)
11921 rorl $16,%edx
11922 movb %dl,4+(%eax)
11923 movb $137,5+(%eax)
11924 movb $0,6+(%eax)
11925 movb %dh,7+(%eax)
11926 rorl $16,%edx
11927 #NO_APP
11928
47926f60 11929 So here we provide the missing zero. */
24eab124
AM
11930
11931 *displacement_string_end = '0';
252b5132
RH
11932 }
11933#endif
d258b828 11934 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
11935 if (gotfree_input_line)
11936 input_line_pointer = gotfree_input_line;
252b5132 11937
5cc00775 11938 expr_mode = expr_operator_none;
24eab124 11939 exp_seg = expression (exp);
252b5132 11940
636c26b0
AM
11941 SKIP_WHITESPACE ();
11942 if (*input_line_pointer)
11943 as_bad (_("junk `%s' after expression"), input_line_pointer);
11944#if GCC_ASM_O_HACK
11945 RESTORE_END_STRING (disp_end + 1);
11946#endif
636c26b0 11947 input_line_pointer = save_input_line_pointer;
636c26b0 11948 if (gotfree_input_line)
ee86248c
JB
11949 {
11950 free (gotfree_input_line);
11951
11952 if (exp->X_op == O_constant || exp->X_op == O_register)
11953 exp->X_op = O_illegal;
11954 }
11955
11956 ret = i386_finalize_displacement (exp_seg, exp, types, disp_start);
11957
11958 RESTORE_END_STRING (disp_end);
11959
11960 return ret;
11961}
11962
11963static int
11964i386_finalize_displacement (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
11965 i386_operand_type types, const char *disp_start)
11966{
ee86248c 11967 int ret = 1;
636c26b0 11968
24eab124
AM
11969 /* We do this to make sure that the section symbol is in
11970 the symbol table. We will ultimately change the relocation
47926f60 11971 to be relative to the beginning of the section. */
1ae12ab7 11972 if (i.reloc[this_operand] == BFD_RELOC_386_GOTOFF
d6ab8113
JB
11973 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
11974 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
24eab124 11975 {
636c26b0 11976 if (exp->X_op != O_symbol)
3992d3b7 11977 goto inv_disp;
636c26b0 11978
e5cb08ac 11979 if (S_IS_LOCAL (exp->X_add_symbol)
c64efb4b
L
11980 && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section
11981 && S_GET_SEGMENT (exp->X_add_symbol) != expr_section)
24eab124 11982 section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
24eab124
AM
11983 exp->X_op = O_subtract;
11984 exp->X_op_symbol = GOT_symbol;
1ae12ab7 11985 if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
29b0f896 11986 i.reloc[this_operand] = BFD_RELOC_32_PCREL;
d6ab8113
JB
11987 else if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
11988 i.reloc[this_operand] = BFD_RELOC_64;
23df1078 11989 else
29b0f896 11990 i.reloc[this_operand] = BFD_RELOC_32;
24eab124 11991 }
252b5132 11992
3992d3b7
AM
11993 else if (exp->X_op == O_absent
11994 || exp->X_op == O_illegal
ee86248c 11995 || exp->X_op == O_big)
2daf4fd8 11996 {
3992d3b7
AM
11997 inv_disp:
11998 as_bad (_("missing or invalid displacement expression `%s'"),
2daf4fd8 11999 disp_start);
3992d3b7 12000 ret = 0;
2daf4fd8
AM
12001 }
12002
a50187b2
JB
12003 else if (exp->X_op == O_constant)
12004 {
12005 /* Sizing gets taken care of by optimize_disp().
12006
12007 If not 64bit, sign/zero extend val, to account for wraparound
12008 when !BFD64. */
5cc00775
JB
12009 if (expr_mode == expr_operator_present
12010 && flag_code != CODE_64BIT && !object_64bit)
a50187b2
JB
12011 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
12012 }
12013
4c63da97 12014#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
a50187b2 12015 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
3992d3b7
AM
12016 && exp_seg != absolute_section
12017 && exp_seg != text_section
12018 && exp_seg != data_section
12019 && exp_seg != bss_section
12020 && exp_seg != undefined_section
12021 && !bfd_is_com_section (exp_seg))
24eab124 12022 {
d0b47220 12023 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
3992d3b7 12024 ret = 0;
24eab124 12025 }
252b5132 12026#endif
3956db08 12027
a50187b2 12028 else if (current_templates->start->opcode_modifier.jump == JUMP_BYTE)
48bcea9f
JB
12029 i.types[this_operand].bitfield.disp8 = 1;
12030
40fb9820 12031 /* Check if this is a displacement only operand. */
02b83698
JB
12032 if (!i.types[this_operand].bitfield.baseindex)
12033 i.types[this_operand] =
12034 operand_type_or (operand_type_and_not (i.types[this_operand], anydisp),
12035 operand_type_and (i.types[this_operand], types));
3956db08 12036
3992d3b7 12037 return ret;
252b5132
RH
12038}
12039
2abc2bec
JB
12040/* Return the active addressing mode, taking address override and
12041 registers forming the address into consideration. Update the
12042 address override prefix if necessary. */
47926f60 12043
2abc2bec
JB
12044static enum flag_code
12045i386_addressing_mode (void)
252b5132 12046{
be05d201
L
12047 enum flag_code addr_mode;
12048
12049 if (i.prefix[ADDR_PREFIX])
12050 addr_mode = flag_code == CODE_32BIT ? CODE_16BIT : CODE_32BIT;
a23b33b3
JB
12051 else if (flag_code == CODE_16BIT
12052 && current_templates->start->cpu_flags.bitfield.cpumpx
12053 /* Avoid replacing the "16-bit addressing not allowed" diagnostic
12054 from md_assemble() by "is not a valid base/index expression"
12055 when there is a base and/or index. */
12056 && !i.types[this_operand].bitfield.baseindex)
12057 {
12058 /* MPX insn memory operands with neither base nor index must be forced
12059 to use 32-bit addressing in 16-bit mode. */
12060 addr_mode = CODE_32BIT;
12061 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
12062 ++i.prefixes;
12063 gas_assert (!i.types[this_operand].bitfield.disp16);
12064 gas_assert (!i.types[this_operand].bitfield.disp32);
12065 }
be05d201
L
12066 else
12067 {
12068 addr_mode = flag_code;
12069
24eab124 12070#if INFER_ADDR_PREFIX
be05d201
L
12071 if (i.mem_operands == 0)
12072 {
12073 /* Infer address prefix from the first memory operand. */
12074 const reg_entry *addr_reg = i.base_reg;
12075
12076 if (addr_reg == NULL)
12077 addr_reg = i.index_reg;
eecb386c 12078
be05d201
L
12079 if (addr_reg)
12080 {
e968fc9b 12081 if (addr_reg->reg_type.bitfield.dword)
be05d201
L
12082 addr_mode = CODE_32BIT;
12083 else if (flag_code != CODE_64BIT
dc821c5f 12084 && addr_reg->reg_type.bitfield.word)
be05d201
L
12085 addr_mode = CODE_16BIT;
12086
12087 if (addr_mode != flag_code)
12088 {
12089 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
12090 i.prefixes += 1;
12091 /* Change the size of any displacement too. At most one
12092 of Disp16 or Disp32 is set.
12093 FIXME. There doesn't seem to be any real need for
12094 separate Disp16 and Disp32 flags. The same goes for
12095 Imm16 and Imm32. Removing them would probably clean
12096 up the code quite a lot. */
12097 if (flag_code != CODE_64BIT
12098 && (i.types[this_operand].bitfield.disp16
12099 || i.types[this_operand].bitfield.disp32))
05909f23
JB
12100 {
12101 static const i386_operand_type disp16_32 = {
12102 .bitfield = { .disp16 = 1, .disp32 = 1 }
12103 };
12104
12105 i.types[this_operand]
12106 = operand_type_xor (i.types[this_operand], disp16_32);
12107 }
be05d201
L
12108 }
12109 }
12110 }
24eab124 12111#endif
be05d201
L
12112 }
12113
2abc2bec
JB
12114 return addr_mode;
12115}
12116
12117/* Make sure the memory operand we've been dealt is valid.
12118 Return 1 on success, 0 on a failure. */
12119
12120static int
12121i386_index_check (const char *operand_string)
12122{
12123 const char *kind = "base/index";
12124 enum flag_code addr_mode = i386_addressing_mode ();
77a19f0e 12125 const insn_template *t = current_templates->end - 1;
2abc2bec 12126
77a19f0e 12127 if (t->opcode_modifier.isstring)
fc0763e6
JB
12128 {
12129 /* Memory operands of string insns are special in that they only allow
12130 a single register (rDI, rSI, or rBX) as their memory address. */
be05d201
L
12131 const reg_entry *expected_reg;
12132 static const char *di_si[][2] =
12133 {
12134 { "esi", "edi" },
12135 { "si", "di" },
12136 { "rsi", "rdi" }
12137 };
12138 static const char *bx[] = { "ebx", "bx", "rbx" };
fc0763e6
JB
12139
12140 kind = "string address";
12141
a152332d 12142 if (t->opcode_modifier.prefixok == PrefixRep)
fc0763e6 12143 {
77a19f0e 12144 int es_op = t->opcode_modifier.isstring - IS_STRING_ES_OP0;
51c8edf6 12145 int op = 0;
fc0763e6 12146
77a19f0e 12147 if (!t->operand_types[0].bitfield.baseindex
fc0763e6 12148 || ((!i.mem_operands != !intel_syntax)
77a19f0e 12149 && t->operand_types[1].bitfield.baseindex))
51c8edf6 12150 op = 1;
fe0e921f
AM
12151 expected_reg
12152 = (const reg_entry *) str_hash_find (reg_hash,
12153 di_si[addr_mode][op == es_op]);
fc0763e6
JB
12154 }
12155 else
fe0e921f
AM
12156 expected_reg
12157 = (const reg_entry *)str_hash_find (reg_hash, bx[addr_mode]);
fc0763e6 12158
be05d201
L
12159 if (i.base_reg != expected_reg
12160 || i.index_reg
fc0763e6 12161 || operand_type_check (i.types[this_operand], disp))
fc0763e6 12162 {
be05d201
L
12163 /* The second memory operand must have the same size as
12164 the first one. */
12165 if (i.mem_operands
12166 && i.base_reg
12167 && !((addr_mode == CODE_64BIT
dc821c5f 12168 && i.base_reg->reg_type.bitfield.qword)
be05d201 12169 || (addr_mode == CODE_32BIT
dc821c5f
JB
12170 ? i.base_reg->reg_type.bitfield.dword
12171 : i.base_reg->reg_type.bitfield.word)))
be05d201
L
12172 goto bad_address;
12173
fc0763e6
JB
12174 as_warn (_("`%s' is not valid here (expected `%c%s%s%c')"),
12175 operand_string,
12176 intel_syntax ? '[' : '(',
12177 register_prefix,
be05d201 12178 expected_reg->reg_name,
fc0763e6 12179 intel_syntax ? ']' : ')');
be05d201 12180 return 1;
fc0763e6 12181 }
be05d201
L
12182 else
12183 return 1;
12184
dc1e8a47 12185 bad_address:
be05d201
L
12186 as_bad (_("`%s' is not a valid %s expression"),
12187 operand_string, kind);
12188 return 0;
3e73aa7c
JH
12189 }
12190 else
12191 {
77a19f0e
JB
12192 t = current_templates->start;
12193
be05d201
L
12194 if (addr_mode != CODE_16BIT)
12195 {
12196 /* 32-bit/64-bit checks. */
41eb8e88
L
12197 if (i.disp_encoding == disp_encoding_16bit)
12198 {
12199 bad_disp:
12200 as_bad (_("invalid `%s' prefix"),
12201 addr_mode == CODE_16BIT ? "{disp32}" : "{disp16}");
12202 return 0;
12203 }
12204
be05d201 12205 if ((i.base_reg
e968fc9b
JB
12206 && ((addr_mode == CODE_64BIT
12207 ? !i.base_reg->reg_type.bitfield.qword
12208 : !i.base_reg->reg_type.bitfield.dword)
12209 || (i.index_reg && i.base_reg->reg_num == RegIP)
12210 || i.base_reg->reg_num == RegIZ))
be05d201 12211 || (i.index_reg
1b54b8d7
JB
12212 && !i.index_reg->reg_type.bitfield.xmmword
12213 && !i.index_reg->reg_type.bitfield.ymmword
12214 && !i.index_reg->reg_type.bitfield.zmmword
be05d201 12215 && ((addr_mode == CODE_64BIT
e968fc9b
JB
12216 ? !i.index_reg->reg_type.bitfield.qword
12217 : !i.index_reg->reg_type.bitfield.dword)
be05d201
L
12218 || !i.index_reg->reg_type.bitfield.baseindex)))
12219 goto bad_address;
8178be5b 12220
260cd341 12221 /* bndmk, bndldx, bndstx and mandatory non-vector SIB have special restrictions. */
7fc69528
JB
12222 if (t->mnem_off == MN_bndmk
12223 || t->mnem_off == MN_bndldx
12224 || t->mnem_off == MN_bndstx
a152332d 12225 || t->opcode_modifier.sib == SIBMEM)
8178be5b
JB
12226 {
12227 /* They cannot use RIP-relative addressing. */
e968fc9b 12228 if (i.base_reg && i.base_reg->reg_num == RegIP)
8178be5b
JB
12229 {
12230 as_bad (_("`%s' cannot be used here"), operand_string);
12231 return 0;
12232 }
12233
12234 /* bndldx and bndstx ignore their scale factor. */
7fc69528 12235 if ((t->mnem_off == MN_bndldx || t->mnem_off == MN_bndstx)
8178be5b
JB
12236 && i.log2_scale_factor)
12237 as_warn (_("register scaling is being ignored here"));
12238 }
be05d201
L
12239 }
12240 else
3e73aa7c 12241 {
be05d201 12242 /* 16-bit checks. */
41eb8e88
L
12243 if (i.disp_encoding == disp_encoding_32bit)
12244 goto bad_disp;
12245
3e73aa7c 12246 if ((i.base_reg
dc821c5f 12247 && (!i.base_reg->reg_type.bitfield.word
40fb9820 12248 || !i.base_reg->reg_type.bitfield.baseindex))
3e73aa7c 12249 || (i.index_reg
dc821c5f 12250 && (!i.index_reg->reg_type.bitfield.word
40fb9820 12251 || !i.index_reg->reg_type.bitfield.baseindex
29b0f896
AM
12252 || !(i.base_reg
12253 && i.base_reg->reg_num < 6
12254 && i.index_reg->reg_num >= 6
12255 && i.log2_scale_factor == 0))))
be05d201 12256 goto bad_address;
3e73aa7c
JH
12257 }
12258 }
be05d201 12259 return 1;
24eab124 12260}
252b5132 12261
43234a1e
L
12262/* Handle vector immediates. */
12263
12264static int
12265RC_SAE_immediate (const char *imm_start)
12266{
43234a1e 12267 const char *pstr = imm_start;
43234a1e
L
12268
12269 if (*pstr != '{')
12270 return 0;
12271
7063667e
JB
12272 pstr = RC_SAE_specifier (pstr + 1);
12273 if (pstr == NULL)
43234a1e
L
12274 return 0;
12275
12276 if (*pstr++ != '}')
12277 {
12278 as_bad (_("Missing '}': '%s'"), imm_start);
12279 return 0;
12280 }
12281 /* RC/SAE immediate string should contain nothing more. */;
12282 if (*pstr != 0)
12283 {
12284 as_bad (_("Junk after '}': '%s'"), imm_start);
12285 return 0;
12286 }
12287
cf665fee
JB
12288 /* Internally this doesn't count as an operand. */
12289 --i.operands;
43234a1e 12290
43234a1e
L
12291 return 1;
12292}
12293
9d299bea
JB
12294static INLINE bool starts_memory_operand (char c)
12295{
014fbcda 12296 return ISDIGIT (c)
4795cd4a 12297 || is_name_beginner (c)
014fbcda 12298 || strchr ("([\"+-!~", c);
9d299bea
JB
12299}
12300
fc0763e6 12301/* Parse OPERAND_STRING into the i386_insn structure I. Returns zero
47926f60 12302 on error. */
252b5132 12303
252b5132 12304static int
a7619375 12305i386_att_operand (char *operand_string)
252b5132 12306{
af6bdddf
AM
12307 const reg_entry *r;
12308 char *end_op;
24eab124 12309 char *op_string = operand_string;
252b5132 12310
24eab124 12311 if (is_space_char (*op_string))
252b5132
RH
12312 ++op_string;
12313
24eab124 12314 /* We check for an absolute prefix (differentiating,
47926f60 12315 for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
ccb05c9c
JB
12316 if (*op_string == ABSOLUTE_PREFIX
12317 && current_templates->start->opcode_modifier.jump)
24eab124
AM
12318 {
12319 ++op_string;
12320 if (is_space_char (*op_string))
12321 ++op_string;
5b7c81bd 12322 i.jumpabsolute = true;
24eab124 12323 }
252b5132 12324
47926f60 12325 /* Check if operand is a register. */
4d1bb795 12326 if ((r = parse_register (op_string, &end_op)) != NULL)
24eab124 12327 {
40fb9820
L
12328 i386_operand_type temp;
12329
8a6fb3f9
JB
12330 if (r == &bad_reg)
12331 return 0;
12332
24eab124
AM
12333 /* Check for a segment override by searching for ':' after a
12334 segment register. */
12335 op_string = end_op;
12336 if (is_space_char (*op_string))
12337 ++op_string;
00cee14f 12338 if (*op_string == ':' && r->reg_type.bitfield.class == SReg)
24eab124 12339 {
5e042380 12340 i.seg[i.mem_operands] = r;
252b5132 12341
24eab124 12342 /* Skip the ':' and whitespace. */
252b5132
RH
12343 ++op_string;
12344 if (is_space_char (*op_string))
24eab124 12345 ++op_string;
252b5132 12346
47926f60 12347 /* Handle case of %es:*foo. */
ccb05c9c
JB
12348 if (!i.jumpabsolute && *op_string == ABSOLUTE_PREFIX
12349 && current_templates->start->opcode_modifier.jump)
24eab124
AM
12350 {
12351 ++op_string;
12352 if (is_space_char (*op_string))
12353 ++op_string;
5b7c81bd 12354 i.jumpabsolute = true;
24eab124 12355 }
c8d541e2 12356
9d299bea 12357 if (!starts_memory_operand (*op_string))
c8d541e2
JB
12358 {
12359 as_bad (_("bad memory operand `%s'"), op_string);
12360 return 0;
12361 }
24eab124
AM
12362 goto do_memory_reference;
12363 }
43234a1e
L
12364
12365 /* Handle vector operations. */
12366 if (*op_string == '{')
12367 {
f70c6814 12368 op_string = check_VecOperations (op_string);
43234a1e
L
12369 if (op_string == NULL)
12370 return 0;
12371 }
12372
24eab124
AM
12373 if (*op_string)
12374 {
d0b47220 12375 as_bad (_("junk `%s' after register"), op_string);
24eab124
AM
12376 return 0;
12377 }
0ff3b7d0
JB
12378
12379 /* Reject pseudo registers for .insn. */
12380 if (dot_insn () && r->reg_type.bitfield.class == ClassNone)
12381 {
12382 as_bad (_("`%s%s' cannot be used here"),
12383 register_prefix, r->reg_name);
12384 return 0;
12385 }
12386
40fb9820
L
12387 temp = r->reg_type;
12388 temp.bitfield.baseindex = 0;
c6fb90c8
L
12389 i.types[this_operand] = operand_type_or (i.types[this_operand],
12390 temp);
7d5e4556 12391 i.types[this_operand].bitfield.unspecified = 0;
520dc8e8 12392 i.op[this_operand].regs = r;
24eab124 12393 i.reg_operands++;
cf665fee
JB
12394
12395 /* A GPR may follow an RC or SAE immediate only if a (vector) register
12396 operand was also present earlier on. */
12397 if (i.rounding.type != rc_none && temp.bitfield.class == Reg
12398 && i.reg_operands == 1)
12399 {
12400 unsigned int j;
12401
12402 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); ++j)
12403 if (i.rounding.type == RC_NamesTable[j].type)
12404 break;
12405 as_bad (_("`%s': misplaced `{%s}'"),
76d3f746 12406 insn_name (current_templates->start), RC_NamesTable[j].name);
cf665fee
JB
12407 return 0;
12408 }
24eab124 12409 }
af6bdddf
AM
12410 else if (*op_string == REGISTER_PREFIX)
12411 {
12412 as_bad (_("bad register name `%s'"), op_string);
12413 return 0;
12414 }
24eab124 12415 else if (*op_string == IMMEDIATE_PREFIX)
ce8a8b2f 12416 {
24eab124 12417 ++op_string;
6f2f06be 12418 if (i.jumpabsolute)
24eab124 12419 {
d0b47220 12420 as_bad (_("immediate operand illegal with absolute jump"));
24eab124
AM
12421 return 0;
12422 }
12423 if (!i386_immediate (op_string))
12424 return 0;
cf665fee
JB
12425 if (i.rounding.type != rc_none)
12426 {
12427 as_bad (_("`%s': RC/SAE operand must follow immediate operands"),
76d3f746 12428 insn_name (current_templates->start));
cf665fee
JB
12429 return 0;
12430 }
24eab124 12431 }
43234a1e
L
12432 else if (RC_SAE_immediate (operand_string))
12433 {
cf665fee
JB
12434 /* If it is a RC or SAE immediate, do the necessary placement check:
12435 Only another immediate or a GPR may precede it. */
12436 if (i.mem_operands || i.reg_operands + i.imm_operands > 1
12437 || (i.reg_operands == 1
12438 && i.op[0].regs->reg_type.bitfield.class != Reg))
12439 {
12440 as_bad (_("`%s': misplaced `%s'"),
76d3f746 12441 insn_name (current_templates->start), operand_string);
cf665fee
JB
12442 return 0;
12443 }
43234a1e 12444 }
9d299bea 12445 else if (starts_memory_operand (*op_string))
24eab124 12446 {
47926f60 12447 /* This is a memory reference of some sort. */
af6bdddf 12448 char *base_string;
252b5132 12449
47926f60 12450 /* Start and end of displacement string expression (if found). */
eecb386c
AM
12451 char *displacement_string_start;
12452 char *displacement_string_end;
252b5132 12453
24eab124 12454 do_memory_reference:
24eab124
AM
12455 /* Check for base index form. We detect the base index form by
12456 looking for an ')' at the end of the operand, searching
12457 for the '(' matching it, and finding a REGISTER_PREFIX or ','
12458 after the '('. */
af6bdddf 12459 base_string = op_string + strlen (op_string);
c3332e24 12460
43234a1e 12461 /* Handle vector operations. */
6b5ba0d4
JB
12462 --base_string;
12463 if (is_space_char (*base_string))
12464 --base_string;
12465
12466 if (*base_string == '}')
43234a1e 12467 {
6b5ba0d4
JB
12468 char *vop_start = NULL;
12469
12470 while (base_string-- > op_string)
12471 {
12472 if (*base_string == '"')
12473 break;
12474 if (*base_string != '{')
12475 continue;
12476
12477 vop_start = base_string;
12478
12479 --base_string;
12480 if (is_space_char (*base_string))
12481 --base_string;
12482
12483 if (*base_string != '}')
12484 break;
12485
12486 vop_start = NULL;
12487 }
12488
12489 if (!vop_start)
12490 {
12491 as_bad (_("unbalanced figure braces"));
12492 return 0;
12493 }
12494
f70c6814 12495 if (check_VecOperations (vop_start) == NULL)
43234a1e 12496 return 0;
43234a1e
L
12497 }
12498
47926f60 12499 /* If we only have a displacement, set-up for it to be parsed later. */
af6bdddf
AM
12500 displacement_string_start = op_string;
12501 displacement_string_end = base_string + 1;
252b5132 12502
24eab124
AM
12503 if (*base_string == ')')
12504 {
af6bdddf 12505 char *temp_string;
e87fb6a6
JB
12506 unsigned int parens_not_balanced = 0;
12507 bool in_quotes = false;
e68c3d59 12508
24eab124 12509 /* We've already checked that the number of left & right ()'s are
e87fb6a6
JB
12510 equal, and that there's a matching set of double quotes. */
12511 end_op = base_string;
12512 for (temp_string = op_string; temp_string < end_op; temp_string++)
24eab124 12513 {
e87fb6a6
JB
12514 if (*temp_string == '\\' && temp_string[1] == '"')
12515 ++temp_string;
12516 else if (*temp_string == '"')
12517 in_quotes = !in_quotes;
12518 else if (!in_quotes)
12519 {
12520 if (*temp_string == '(' && !parens_not_balanced++)
12521 base_string = temp_string;
12522 if (*temp_string == ')')
12523 --parens_not_balanced;
12524 }
24eab124 12525 }
c3332e24 12526
af6bdddf 12527 temp_string = base_string;
c3332e24 12528
24eab124 12529 /* Skip past '(' and whitespace. */
4f081312 12530 gas_assert (*base_string == '(');
e87fb6a6 12531 ++base_string;
252b5132 12532 if (is_space_char (*base_string))
24eab124 12533 ++base_string;
252b5132 12534
af6bdddf 12535 if (*base_string == ','
4eed87de
AM
12536 || ((i.base_reg = parse_register (base_string, &end_op))
12537 != NULL))
252b5132 12538 {
af6bdddf 12539 displacement_string_end = temp_string;
252b5132 12540
40fb9820 12541 i.types[this_operand].bitfield.baseindex = 1;
252b5132 12542
af6bdddf 12543 if (i.base_reg)
24eab124 12544 {
8a6fb3f9
JB
12545 if (i.base_reg == &bad_reg)
12546 return 0;
24eab124
AM
12547 base_string = end_op;
12548 if (is_space_char (*base_string))
12549 ++base_string;
af6bdddf
AM
12550 }
12551
12552 /* There may be an index reg or scale factor here. */
12553 if (*base_string == ',')
12554 {
12555 ++base_string;
12556 if (is_space_char (*base_string))
12557 ++base_string;
12558
4eed87de
AM
12559 if ((i.index_reg = parse_register (base_string, &end_op))
12560 != NULL)
24eab124 12561 {
8a6fb3f9
JB
12562 if (i.index_reg == &bad_reg)
12563 return 0;
af6bdddf 12564 base_string = end_op;
24eab124
AM
12565 if (is_space_char (*base_string))
12566 ++base_string;
af6bdddf
AM
12567 if (*base_string == ',')
12568 {
12569 ++base_string;
12570 if (is_space_char (*base_string))
12571 ++base_string;
12572 }
e5cb08ac 12573 else if (*base_string != ')')
af6bdddf 12574 {
4eed87de
AM
12575 as_bad (_("expecting `,' or `)' "
12576 "after index register in `%s'"),
af6bdddf
AM
12577 operand_string);
12578 return 0;
12579 }
24eab124 12580 }
af6bdddf 12581 else if (*base_string == REGISTER_PREFIX)
24eab124 12582 {
f76bf5e0
L
12583 end_op = strchr (base_string, ',');
12584 if (end_op)
12585 *end_op = '\0';
af6bdddf 12586 as_bad (_("bad register name `%s'"), base_string);
24eab124
AM
12587 return 0;
12588 }
252b5132 12589
47926f60 12590 /* Check for scale factor. */
551c1ca1 12591 if (*base_string != ')')
af6bdddf 12592 {
551c1ca1
AM
12593 char *end_scale = i386_scale (base_string);
12594
12595 if (!end_scale)
af6bdddf 12596 return 0;
24eab124 12597
551c1ca1 12598 base_string = end_scale;
af6bdddf
AM
12599 if (is_space_char (*base_string))
12600 ++base_string;
12601 if (*base_string != ')')
12602 {
4eed87de
AM
12603 as_bad (_("expecting `)' "
12604 "after scale factor in `%s'"),
af6bdddf
AM
12605 operand_string);
12606 return 0;
12607 }
12608 }
12609 else if (!i.index_reg)
24eab124 12610 {
4eed87de
AM
12611 as_bad (_("expecting index register or scale factor "
12612 "after `,'; got '%c'"),
af6bdddf 12613 *base_string);
24eab124
AM
12614 return 0;
12615 }
12616 }
af6bdddf 12617 else if (*base_string != ')')
24eab124 12618 {
4eed87de
AM
12619 as_bad (_("expecting `,' or `)' "
12620 "after base register in `%s'"),
af6bdddf 12621 operand_string);
24eab124
AM
12622 return 0;
12623 }
c3332e24 12624 }
af6bdddf 12625 else if (*base_string == REGISTER_PREFIX)
c3332e24 12626 {
f76bf5e0
L
12627 end_op = strchr (base_string, ',');
12628 if (end_op)
12629 *end_op = '\0';
af6bdddf 12630 as_bad (_("bad register name `%s'"), base_string);
24eab124 12631 return 0;
c3332e24 12632 }
24eab124
AM
12633 }
12634
12635 /* If there's an expression beginning the operand, parse it,
12636 assuming displacement_string_start and
12637 displacement_string_end are meaningful. */
12638 if (displacement_string_start != displacement_string_end)
12639 {
12640 if (!i386_displacement (displacement_string_start,
12641 displacement_string_end))
12642 return 0;
12643 }
12644
12645 /* Special case for (%dx) while doing input/output op. */
12646 if (i.base_reg
75e5731b
JB
12647 && i.base_reg->reg_type.bitfield.instance == RegD
12648 && i.base_reg->reg_type.bitfield.word
24eab124
AM
12649 && i.index_reg == 0
12650 && i.log2_scale_factor == 0
12651 && i.seg[i.mem_operands] == 0
40fb9820 12652 && !operand_type_check (i.types[this_operand], disp))
24eab124 12653 {
2fb5be8d 12654 i.types[this_operand] = i.base_reg->reg_type;
9373f275 12655 i.input_output_operand = true;
24eab124
AM
12656 return 1;
12657 }
12658
eecb386c
AM
12659 if (i386_index_check (operand_string) == 0)
12660 return 0;
c48dadc9 12661 i.flags[this_operand] |= Operand_Mem;
24eab124
AM
12662 i.mem_operands++;
12663 }
12664 else
ce8a8b2f
AM
12665 {
12666 /* It's not a memory operand; argh! */
24eab124
AM
12667 as_bad (_("invalid char %s beginning operand %d `%s'"),
12668 output_invalid (*op_string),
12669 this_operand + 1,
12670 op_string);
12671 return 0;
12672 }
47926f60 12673 return 1; /* Normal return. */
252b5132
RH
12674}
12675\f
fa94de6b
RM
12676/* Calculate the maximum variable size (i.e., excluding fr_fix)
12677 that an rs_machine_dependent frag may reach. */
12678
12679unsigned int
12680i386_frag_max_var (fragS *frag)
12681{
12682 /* The only relaxable frags are for jumps.
12683 Unconditional jumps can grow by 4 bytes and others by 5 bytes. */
12684 gas_assert (frag->fr_type == rs_machine_dependent);
12685 return TYPE_FROM_RELAX_STATE (frag->fr_subtype) == UNCOND_JUMP ? 4 : 5;
12686}
12687
b084df0b
L
12688#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
12689static int
8dcea932 12690elf_symbol_resolved_in_segment_p (symbolS *fr_symbol, offsetT fr_var)
b084df0b
L
12691{
12692 /* STT_GNU_IFUNC symbol must go through PLT. */
12693 if ((symbol_get_bfdsym (fr_symbol)->flags
12694 & BSF_GNU_INDIRECT_FUNCTION) != 0)
12695 return 0;
12696
12697 if (!S_IS_EXTERNAL (fr_symbol))
12698 /* Symbol may be weak or local. */
12699 return !S_IS_WEAK (fr_symbol);
12700
8dcea932
L
12701 /* Global symbols with non-default visibility can't be preempted. */
12702 if (ELF_ST_VISIBILITY (S_GET_OTHER (fr_symbol)) != STV_DEFAULT)
12703 return 1;
12704
12705 if (fr_var != NO_RELOC)
12706 switch ((enum bfd_reloc_code_real) fr_var)
12707 {
12708 case BFD_RELOC_386_PLT32:
12709 case BFD_RELOC_X86_64_PLT32:
33eaf5de 12710 /* Symbol with PLT relocation may be preempted. */
8dcea932
L
12711 return 0;
12712 default:
12713 abort ();
12714 }
12715
b084df0b
L
12716 /* Global symbols with default visibility in a shared library may be
12717 preempted by another definition. */
8dcea932 12718 return !shared;
b084df0b
L
12719}
12720#endif
12721
79d72f45
HL
12722/* Table 3-2. Macro-Fusible Instructions in Haswell Microarchitecture
12723 Note also work for Skylake and Cascadelake.
12724---------------------------------------------------------------------
12725| JCC | ADD/SUB/CMP | INC/DEC | TEST/AND |
12726| ------ | ----------- | ------- | -------- |
12727| Jo | N | N | Y |
12728| Jno | N | N | Y |
12729| Jc/Jb | Y | N | Y |
12730| Jae/Jnb | Y | N | Y |
12731| Je/Jz | Y | Y | Y |
12732| Jne/Jnz | Y | Y | Y |
12733| Jna/Jbe | Y | N | Y |
12734| Ja/Jnbe | Y | N | Y |
12735| Js | N | N | Y |
12736| Jns | N | N | Y |
12737| Jp/Jpe | N | N | Y |
12738| Jnp/Jpo | N | N | Y |
12739| Jl/Jnge | Y | Y | Y |
12740| Jge/Jnl | Y | Y | Y |
12741| Jle/Jng | Y | Y | Y |
12742| Jg/Jnle | Y | Y | Y |
12743--------------------------------------------------------------------- */
12744static int
12745i386_macro_fusible_p (enum mf_cmp_kind mf_cmp, enum mf_jcc_kind mf_jcc)
12746{
12747 if (mf_cmp == mf_cmp_alu_cmp)
12748 return ((mf_jcc >= mf_jcc_jc && mf_jcc <= mf_jcc_jna)
12749 || mf_jcc == mf_jcc_jl || mf_jcc == mf_jcc_jle);
12750 if (mf_cmp == mf_cmp_incdec)
12751 return (mf_jcc == mf_jcc_je || mf_jcc == mf_jcc_jl
12752 || mf_jcc == mf_jcc_jle);
12753 if (mf_cmp == mf_cmp_test_and)
12754 return 1;
12755 return 0;
12756}
12757
e379e5f3
L
12758/* Return the next non-empty frag. */
12759
12760static fragS *
12761i386_next_non_empty_frag (fragS *fragP)
12762{
12763 /* There may be a frag with a ".fill 0" when there is no room in
12764 the current frag for frag_grow in output_insn. */
12765 for (fragP = fragP->fr_next;
12766 (fragP != NULL
12767 && fragP->fr_type == rs_fill
12768 && fragP->fr_fix == 0);
12769 fragP = fragP->fr_next)
12770 ;
12771 return fragP;
12772}
12773
12774/* Return the next jcc frag after BRANCH_PADDING. */
12775
12776static fragS *
79d72f45 12777i386_next_fusible_jcc_frag (fragS *maybe_cmp_fragP, fragS *pad_fragP)
e379e5f3 12778{
79d72f45
HL
12779 fragS *branch_fragP;
12780 if (!pad_fragP)
e379e5f3
L
12781 return NULL;
12782
79d72f45
HL
12783 if (pad_fragP->fr_type == rs_machine_dependent
12784 && (TYPE_FROM_RELAX_STATE (pad_fragP->fr_subtype)
e379e5f3
L
12785 == BRANCH_PADDING))
12786 {
79d72f45
HL
12787 branch_fragP = i386_next_non_empty_frag (pad_fragP);
12788 if (branch_fragP->fr_type != rs_machine_dependent)
e379e5f3 12789 return NULL;
79d72f45
HL
12790 if (TYPE_FROM_RELAX_STATE (branch_fragP->fr_subtype) == COND_JUMP
12791 && i386_macro_fusible_p (maybe_cmp_fragP->tc_frag_data.mf_type,
12792 pad_fragP->tc_frag_data.mf_type))
12793 return branch_fragP;
e379e5f3
L
12794 }
12795
12796 return NULL;
12797}
12798
12799/* Classify BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags. */
12800
12801static void
12802i386_classify_machine_dependent_frag (fragS *fragP)
12803{
12804 fragS *cmp_fragP;
12805 fragS *pad_fragP;
12806 fragS *branch_fragP;
12807 fragS *next_fragP;
12808 unsigned int max_prefix_length;
12809
12810 if (fragP->tc_frag_data.classified)
12811 return;
12812
12813 /* First scan for BRANCH_PADDING and FUSED_JCC_PADDING. Convert
12814 FUSED_JCC_PADDING and merge BRANCH_PADDING. */
12815 for (next_fragP = fragP;
12816 next_fragP != NULL;
12817 next_fragP = next_fragP->fr_next)
12818 {
12819 next_fragP->tc_frag_data.classified = 1;
12820 if (next_fragP->fr_type == rs_machine_dependent)
12821 switch (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype))
12822 {
12823 case BRANCH_PADDING:
12824 /* The BRANCH_PADDING frag must be followed by a branch
12825 frag. */
12826 branch_fragP = i386_next_non_empty_frag (next_fragP);
12827 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
12828 break;
12829 case FUSED_JCC_PADDING:
12830 /* Check if this is a fused jcc:
12831 FUSED_JCC_PADDING
12832 CMP like instruction
12833 BRANCH_PADDING
12834 COND_JUMP
12835 */
12836 cmp_fragP = i386_next_non_empty_frag (next_fragP);
12837 pad_fragP = i386_next_non_empty_frag (cmp_fragP);
79d72f45 12838 branch_fragP = i386_next_fusible_jcc_frag (next_fragP, pad_fragP);
e379e5f3
L
12839 if (branch_fragP)
12840 {
12841 /* The BRANCH_PADDING frag is merged with the
12842 FUSED_JCC_PADDING frag. */
12843 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
12844 /* CMP like instruction size. */
12845 next_fragP->tc_frag_data.cmp_size = cmp_fragP->fr_fix;
12846 frag_wane (pad_fragP);
12847 /* Skip to branch_fragP. */
12848 next_fragP = branch_fragP;
12849 }
12850 else if (next_fragP->tc_frag_data.max_prefix_length)
12851 {
12852 /* Turn FUSED_JCC_PADDING into BRANCH_PREFIX if it isn't
12853 a fused jcc. */
12854 next_fragP->fr_subtype
12855 = ENCODE_RELAX_STATE (BRANCH_PREFIX, 0);
12856 next_fragP->tc_frag_data.max_bytes
12857 = next_fragP->tc_frag_data.max_prefix_length;
12858 /* This will be updated in the BRANCH_PREFIX scan. */
12859 next_fragP->tc_frag_data.max_prefix_length = 0;
12860 }
12861 else
12862 frag_wane (next_fragP);
12863 break;
12864 }
12865 }
12866
12867 /* Stop if there is no BRANCH_PREFIX. */
12868 if (!align_branch_prefix_size)
12869 return;
12870
12871 /* Scan for BRANCH_PREFIX. */
12872 for (; fragP != NULL; fragP = fragP->fr_next)
12873 {
12874 if (fragP->fr_type != rs_machine_dependent
12875 || (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
12876 != BRANCH_PREFIX))
12877 continue;
12878
12879 /* Count all BRANCH_PREFIX frags before BRANCH_PADDING and
12880 COND_JUMP_PREFIX. */
12881 max_prefix_length = 0;
12882 for (next_fragP = fragP;
12883 next_fragP != NULL;
12884 next_fragP = next_fragP->fr_next)
12885 {
12886 if (next_fragP->fr_type == rs_fill)
12887 /* Skip rs_fill frags. */
12888 continue;
12889 else if (next_fragP->fr_type != rs_machine_dependent)
12890 /* Stop for all other frags. */
12891 break;
12892
12893 /* rs_machine_dependent frags. */
12894 if (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12895 == BRANCH_PREFIX)
12896 {
12897 /* Count BRANCH_PREFIX frags. */
12898 if (max_prefix_length >= MAX_FUSED_JCC_PADDING_SIZE)
12899 {
12900 max_prefix_length = MAX_FUSED_JCC_PADDING_SIZE;
12901 frag_wane (next_fragP);
12902 }
12903 else
12904 max_prefix_length
12905 += next_fragP->tc_frag_data.max_bytes;
12906 }
12907 else if ((TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12908 == BRANCH_PADDING)
12909 || (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12910 == FUSED_JCC_PADDING))
12911 {
12912 /* Stop at BRANCH_PADDING and FUSED_JCC_PADDING. */
12913 fragP->tc_frag_data.u.padding_fragP = next_fragP;
12914 break;
12915 }
12916 else
12917 /* Stop for other rs_machine_dependent frags. */
12918 break;
12919 }
12920
12921 fragP->tc_frag_data.max_prefix_length = max_prefix_length;
12922
12923 /* Skip to the next frag. */
12924 fragP = next_fragP;
12925 }
12926}
12927
12928/* Compute padding size for
12929
12930 FUSED_JCC_PADDING
12931 CMP like instruction
12932 BRANCH_PADDING
12933 COND_JUMP/UNCOND_JUMP
12934
12935 or
12936
12937 BRANCH_PADDING
12938 COND_JUMP/UNCOND_JUMP
12939 */
12940
12941static int
12942i386_branch_padding_size (fragS *fragP, offsetT address)
12943{
12944 unsigned int offset, size, padding_size;
12945 fragS *branch_fragP = fragP->tc_frag_data.u.branch_fragP;
12946
12947 /* The start address of the BRANCH_PADDING or FUSED_JCC_PADDING frag. */
12948 if (!address)
12949 address = fragP->fr_address;
12950 address += fragP->fr_fix;
12951
12952 /* CMP like instrunction size. */
12953 size = fragP->tc_frag_data.cmp_size;
12954
12955 /* The base size of the branch frag. */
12956 size += branch_fragP->fr_fix;
12957
12958 /* Add opcode and displacement bytes for the rs_machine_dependent
12959 branch frag. */
12960 if (branch_fragP->fr_type == rs_machine_dependent)
12961 size += md_relax_table[branch_fragP->fr_subtype].rlx_length;
12962
12963 /* Check if branch is within boundary and doesn't end at the last
12964 byte. */
12965 offset = address & ((1U << align_branch_power) - 1);
12966 if ((offset + size) >= (1U << align_branch_power))
12967 /* Padding needed to avoid crossing boundary. */
12968 padding_size = (1U << align_branch_power) - offset;
12969 else
12970 /* No padding needed. */
12971 padding_size = 0;
12972
12973 /* The return value may be saved in tc_frag_data.length which is
12974 unsigned byte. */
12975 if (!fits_in_unsigned_byte (padding_size))
12976 abort ();
12977
12978 return padding_size;
12979}
12980
12981/* i386_generic_table_relax_frag()
12982
12983 Handle BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags to
12984 grow/shrink padding to align branch frags. Hand others to
12985 relax_frag(). */
12986
12987long
12988i386_generic_table_relax_frag (segT segment, fragS *fragP, long stretch)
12989{
12990 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12991 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
12992 {
12993 long padding_size = i386_branch_padding_size (fragP, 0);
12994 long grow = padding_size - fragP->tc_frag_data.length;
12995
12996 /* When the BRANCH_PREFIX frag is used, the computed address
12997 must match the actual address and there should be no padding. */
12998 if (fragP->tc_frag_data.padding_address
12999 && (fragP->tc_frag_data.padding_address != fragP->fr_address
13000 || padding_size))
13001 abort ();
13002
13003 /* Update the padding size. */
13004 if (grow)
13005 fragP->tc_frag_data.length = padding_size;
13006
13007 return grow;
13008 }
13009 else if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
13010 {
13011 fragS *padding_fragP, *next_fragP;
13012 long padding_size, left_size, last_size;
13013
13014 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
13015 if (!padding_fragP)
13016 /* Use the padding set by the leading BRANCH_PREFIX frag. */
13017 return (fragP->tc_frag_data.length
13018 - fragP->tc_frag_data.last_length);
13019
13020 /* Compute the relative address of the padding frag in the very
13021 first time where the BRANCH_PREFIX frag sizes are zero. */
13022 if (!fragP->tc_frag_data.padding_address)
13023 fragP->tc_frag_data.padding_address
13024 = padding_fragP->fr_address - (fragP->fr_address - stretch);
13025
13026 /* First update the last length from the previous interation. */
13027 left_size = fragP->tc_frag_data.prefix_length;
13028 for (next_fragP = fragP;
13029 next_fragP != padding_fragP;
13030 next_fragP = next_fragP->fr_next)
13031 if (next_fragP->fr_type == rs_machine_dependent
13032 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
13033 == BRANCH_PREFIX))
13034 {
13035 if (left_size)
13036 {
13037 int max = next_fragP->tc_frag_data.max_bytes;
13038 if (max)
13039 {
13040 int size;
13041 if (max > left_size)
13042 size = left_size;
13043 else
13044 size = max;
13045 left_size -= size;
13046 next_fragP->tc_frag_data.last_length = size;
13047 }
13048 }
13049 else
13050 next_fragP->tc_frag_data.last_length = 0;
13051 }
13052
13053 /* Check the padding size for the padding frag. */
13054 padding_size = i386_branch_padding_size
13055 (padding_fragP, (fragP->fr_address
13056 + fragP->tc_frag_data.padding_address));
13057
13058 last_size = fragP->tc_frag_data.prefix_length;
13059 /* Check if there is change from the last interation. */
13060 if (padding_size == last_size)
13061 {
13062 /* Update the expected address of the padding frag. */
13063 padding_fragP->tc_frag_data.padding_address
13064 = (fragP->fr_address + padding_size
13065 + fragP->tc_frag_data.padding_address);
13066 return 0;
13067 }
13068
13069 if (padding_size > fragP->tc_frag_data.max_prefix_length)
13070 {
13071 /* No padding if there is no sufficient room. Clear the
13072 expected address of the padding frag. */
13073 padding_fragP->tc_frag_data.padding_address = 0;
13074 padding_size = 0;
13075 }
13076 else
13077 /* Store the expected address of the padding frag. */
13078 padding_fragP->tc_frag_data.padding_address
13079 = (fragP->fr_address + padding_size
13080 + fragP->tc_frag_data.padding_address);
13081
13082 fragP->tc_frag_data.prefix_length = padding_size;
13083
13084 /* Update the length for the current interation. */
13085 left_size = padding_size;
13086 for (next_fragP = fragP;
13087 next_fragP != padding_fragP;
13088 next_fragP = next_fragP->fr_next)
13089 if (next_fragP->fr_type == rs_machine_dependent
13090 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
13091 == BRANCH_PREFIX))
13092 {
13093 if (left_size)
13094 {
13095 int max = next_fragP->tc_frag_data.max_bytes;
13096 if (max)
13097 {
13098 int size;
13099 if (max > left_size)
13100 size = left_size;
13101 else
13102 size = max;
13103 left_size -= size;
13104 next_fragP->tc_frag_data.length = size;
13105 }
13106 }
13107 else
13108 next_fragP->tc_frag_data.length = 0;
13109 }
13110
13111 return (fragP->tc_frag_data.length
13112 - fragP->tc_frag_data.last_length);
13113 }
13114 return relax_frag (segment, fragP, stretch);
13115}
13116
ee7fcc42
AM
13117/* md_estimate_size_before_relax()
13118
13119 Called just before relax() for rs_machine_dependent frags. The x86
13120 assembler uses these frags to handle variable size jump
13121 instructions.
13122
13123 Any symbol that is now undefined will not become defined.
13124 Return the correct fr_subtype in the frag.
13125 Return the initial "guess for variable size of frag" to caller.
13126 The guess is actually the growth beyond the fixed part. Whatever
13127 we do to grow the fixed or variable part contributes to our
13128 returned value. */
13129
252b5132 13130int
7016a5d5 13131md_estimate_size_before_relax (fragS *fragP, segT segment)
252b5132 13132{
e379e5f3
L
13133 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
13134 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX
13135 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
13136 {
13137 i386_classify_machine_dependent_frag (fragP);
13138 return fragP->tc_frag_data.length;
13139 }
13140
252b5132 13141 /* We've already got fragP->fr_subtype right; all we have to do is
b98ef147
AM
13142 check for un-relaxable symbols. On an ELF system, we can't relax
13143 an externally visible symbol, because it may be overridden by a
13144 shared library. */
13145 if (S_GET_SEGMENT (fragP->fr_symbol) != segment
6d249963 13146#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 13147 || (IS_ELF
8dcea932
L
13148 && !elf_symbol_resolved_in_segment_p (fragP->fr_symbol,
13149 fragP->fr_var))
fbeb56a4
DK
13150#endif
13151#if defined (OBJ_COFF) && defined (TE_PE)
7ab9ffdd 13152 || (OUTPUT_FLAVOR == bfd_target_coff_flavour
fbeb56a4 13153 && S_IS_WEAK (fragP->fr_symbol))
b98ef147
AM
13154#endif
13155 )
252b5132 13156 {
b98ef147
AM
13157 /* Symbol is undefined in this segment, or we need to keep a
13158 reloc so that weak symbols can be overridden. */
13159 int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
f86103b7 13160 enum bfd_reloc_code_real reloc_type;
ee7fcc42
AM
13161 unsigned char *opcode;
13162 int old_fr_fix;
eb19308f 13163 fixS *fixP = NULL;
f6af82bd 13164
ee7fcc42 13165 if (fragP->fr_var != NO_RELOC)
1e9cc1c2 13166 reloc_type = (enum bfd_reloc_code_real) fragP->fr_var;
b98ef147 13167 else if (size == 2)
f6af82bd 13168 reloc_type = BFD_RELOC_16_PCREL;
bd7ab16b 13169#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1ef3994a
JB
13170 else if (fragP->tc_frag_data.code64 && fragP->fr_offset == 0
13171 && need_plt32_p (fragP->fr_symbol))
bd7ab16b
L
13172 reloc_type = BFD_RELOC_X86_64_PLT32;
13173#endif
f6af82bd
AM
13174 else
13175 reloc_type = BFD_RELOC_32_PCREL;
252b5132 13176
ee7fcc42
AM
13177 old_fr_fix = fragP->fr_fix;
13178 opcode = (unsigned char *) fragP->fr_opcode;
13179
fddf5b5b 13180 switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
252b5132 13181 {
fddf5b5b
AM
13182 case UNCOND_JUMP:
13183 /* Make jmp (0xeb) a (d)word displacement jump. */
47926f60 13184 opcode[0] = 0xe9;
252b5132 13185 fragP->fr_fix += size;
eb19308f
JB
13186 fixP = fix_new (fragP, old_fr_fix, size,
13187 fragP->fr_symbol,
13188 fragP->fr_offset, 1,
13189 reloc_type);
252b5132
RH
13190 break;
13191
fddf5b5b 13192 case COND_JUMP86:
412167cb
AM
13193 if (size == 2
13194 && (!no_cond_jump_promotion || fragP->fr_var != NO_RELOC))
fddf5b5b
AM
13195 {
13196 /* Negate the condition, and branch past an
13197 unconditional jump. */
13198 opcode[0] ^= 1;
13199 opcode[1] = 3;
13200 /* Insert an unconditional jump. */
13201 opcode[2] = 0xe9;
13202 /* We added two extra opcode bytes, and have a two byte
13203 offset. */
13204 fragP->fr_fix += 2 + 2;
062cd5e7
AS
13205 fix_new (fragP, old_fr_fix + 2, 2,
13206 fragP->fr_symbol,
13207 fragP->fr_offset, 1,
13208 reloc_type);
fddf5b5b
AM
13209 break;
13210 }
13211 /* Fall through. */
13212
13213 case COND_JUMP:
412167cb
AM
13214 if (no_cond_jump_promotion && fragP->fr_var == NO_RELOC)
13215 {
13216 fragP->fr_fix += 1;
3e02c1cc
AM
13217 fixP = fix_new (fragP, old_fr_fix, 1,
13218 fragP->fr_symbol,
13219 fragP->fr_offset, 1,
13220 BFD_RELOC_8_PCREL);
13221 fixP->fx_signed = 1;
412167cb
AM
13222 break;
13223 }
93c2a809 13224
24eab124 13225 /* This changes the byte-displacement jump 0x7N
fddf5b5b 13226 to the (d)word-displacement jump 0x0f,0x8N. */
252b5132 13227 opcode[1] = opcode[0] + 0x10;
f6af82bd 13228 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
47926f60
KH
13229 /* We've added an opcode byte. */
13230 fragP->fr_fix += 1 + size;
eb19308f
JB
13231 fixP = fix_new (fragP, old_fr_fix + 1, size,
13232 fragP->fr_symbol,
13233 fragP->fr_offset, 1,
13234 reloc_type);
252b5132 13235 break;
fddf5b5b
AM
13236
13237 default:
13238 BAD_CASE (fragP->fr_subtype);
13239 break;
252b5132 13240 }
eb19308f
JB
13241
13242 /* All jumps handled here are signed, but don't unconditionally use a
13243 signed limit check for 32 and 16 bit jumps as we want to allow wrap
13244 around at 4G (outside of 64-bit mode) and 64k. */
13245 if (size == 4 && flag_code == CODE_64BIT)
13246 fixP->fx_signed = 1;
13247
252b5132 13248 frag_wane (fragP);
ee7fcc42 13249 return fragP->fr_fix - old_fr_fix;
252b5132 13250 }
93c2a809 13251
93c2a809
AM
13252 /* Guess size depending on current relax state. Initially the relax
13253 state will correspond to a short jump and we return 1, because
13254 the variable part of the frag (the branch offset) is one byte
13255 long. However, we can relax a section more than once and in that
13256 case we must either set fr_subtype back to the unrelaxed state,
13257 or return the value for the appropriate branch. */
13258 return md_relax_table[fragP->fr_subtype].rlx_length;
ee7fcc42
AM
13259}
13260
47926f60
KH
13261/* Called after relax() is finished.
13262
13263 In: Address of frag.
13264 fr_type == rs_machine_dependent.
13265 fr_subtype is what the address relaxed to.
13266
13267 Out: Any fixSs and constants are set up.
13268 Caller will turn frag into a ".space 0". */
13269
252b5132 13270void
7016a5d5
TG
13271md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec ATTRIBUTE_UNUSED,
13272 fragS *fragP)
252b5132 13273{
29b0f896 13274 unsigned char *opcode;
252b5132 13275 unsigned char *where_to_put_displacement = NULL;
847f7ad4
AM
13276 offsetT target_address;
13277 offsetT opcode_address;
252b5132 13278 unsigned int extension = 0;
847f7ad4 13279 offsetT displacement_from_opcode_start;
252b5132 13280
e379e5f3
L
13281 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
13282 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING
13283 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
13284 {
13285 /* Generate nop padding. */
13286 unsigned int size = fragP->tc_frag_data.length;
13287 if (size)
13288 {
13289 if (size > fragP->tc_frag_data.max_bytes)
13290 abort ();
13291
13292 if (flag_debug)
13293 {
13294 const char *msg;
13295 const char *branch = "branch";
13296 const char *prefix = "";
13297 fragS *padding_fragP;
13298 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
13299 == BRANCH_PREFIX)
13300 {
13301 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
13302 switch (fragP->tc_frag_data.default_prefix)
13303 {
13304 default:
13305 abort ();
13306 break;
13307 case CS_PREFIX_OPCODE:
13308 prefix = " cs";
13309 break;
13310 case DS_PREFIX_OPCODE:
13311 prefix = " ds";
13312 break;
13313 case ES_PREFIX_OPCODE:
13314 prefix = " es";
13315 break;
13316 case FS_PREFIX_OPCODE:
13317 prefix = " fs";
13318 break;
13319 case GS_PREFIX_OPCODE:
13320 prefix = " gs";
13321 break;
13322 case SS_PREFIX_OPCODE:
13323 prefix = " ss";
13324 break;
13325 }
13326 if (padding_fragP)
13327 msg = _("%s:%u: add %d%s at 0x%llx to align "
13328 "%s within %d-byte boundary\n");
13329 else
13330 msg = _("%s:%u: add additional %d%s at 0x%llx to "
13331 "align %s within %d-byte boundary\n");
13332 }
13333 else
13334 {
13335 padding_fragP = fragP;
13336 msg = _("%s:%u: add %d%s-byte nop at 0x%llx to align "
13337 "%s within %d-byte boundary\n");
13338 }
13339
13340 if (padding_fragP)
13341 switch (padding_fragP->tc_frag_data.branch_type)
13342 {
13343 case align_branch_jcc:
13344 branch = "jcc";
13345 break;
13346 case align_branch_fused:
13347 branch = "fused jcc";
13348 break;
13349 case align_branch_jmp:
13350 branch = "jmp";
13351 break;
13352 case align_branch_call:
13353 branch = "call";
13354 break;
13355 case align_branch_indirect:
13356 branch = "indiret branch";
13357 break;
13358 case align_branch_ret:
13359 branch = "ret";
13360 break;
13361 default:
13362 break;
13363 }
13364
13365 fprintf (stdout, msg,
13366 fragP->fr_file, fragP->fr_line, size, prefix,
13367 (long long) fragP->fr_address, branch,
13368 1 << align_branch_power);
13369 }
13370 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
13371 memset (fragP->fr_opcode,
13372 fragP->tc_frag_data.default_prefix, size);
13373 else
13374 i386_generate_nops (fragP, (char *) fragP->fr_opcode,
13375 size, 0);
13376 fragP->fr_fix += size;
13377 }
13378 return;
13379 }
13380
252b5132
RH
13381 opcode = (unsigned char *) fragP->fr_opcode;
13382
47926f60 13383 /* Address we want to reach in file space. */
252b5132 13384 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
252b5132 13385
47926f60 13386 /* Address opcode resides at in file space. */
252b5132
RH
13387 opcode_address = fragP->fr_address + fragP->fr_fix;
13388
47926f60 13389 /* Displacement from opcode start to fill into instruction. */
252b5132
RH
13390 displacement_from_opcode_start = target_address - opcode_address;
13391
fddf5b5b 13392 if ((fragP->fr_subtype & BIG) == 0)
252b5132 13393 {
47926f60
KH
13394 /* Don't have to change opcode. */
13395 extension = 1; /* 1 opcode + 1 displacement */
252b5132 13396 where_to_put_displacement = &opcode[1];
fddf5b5b
AM
13397 }
13398 else
13399 {
13400 if (no_cond_jump_promotion
13401 && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
4eed87de
AM
13402 as_warn_where (fragP->fr_file, fragP->fr_line,
13403 _("long jump required"));
252b5132 13404
fddf5b5b
AM
13405 switch (fragP->fr_subtype)
13406 {
13407 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
13408 extension = 4; /* 1 opcode + 4 displacement */
13409 opcode[0] = 0xe9;
13410 where_to_put_displacement = &opcode[1];
13411 break;
252b5132 13412
fddf5b5b
AM
13413 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
13414 extension = 2; /* 1 opcode + 2 displacement */
13415 opcode[0] = 0xe9;
13416 where_to_put_displacement = &opcode[1];
13417 break;
252b5132 13418
fddf5b5b
AM
13419 case ENCODE_RELAX_STATE (COND_JUMP, BIG):
13420 case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
13421 extension = 5; /* 2 opcode + 4 displacement */
13422 opcode[1] = opcode[0] + 0x10;
13423 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
13424 where_to_put_displacement = &opcode[2];
13425 break;
252b5132 13426
fddf5b5b
AM
13427 case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
13428 extension = 3; /* 2 opcode + 2 displacement */
13429 opcode[1] = opcode[0] + 0x10;
13430 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
13431 where_to_put_displacement = &opcode[2];
13432 break;
252b5132 13433
fddf5b5b
AM
13434 case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
13435 extension = 4;
13436 opcode[0] ^= 1;
13437 opcode[1] = 3;
13438 opcode[2] = 0xe9;
13439 where_to_put_displacement = &opcode[3];
13440 break;
13441
13442 default:
13443 BAD_CASE (fragP->fr_subtype);
13444 break;
13445 }
252b5132 13446 }
fddf5b5b 13447
7b81dfbb
AJ
13448 /* If size if less then four we are sure that the operand fits,
13449 but if it's 4, then it could be that the displacement is larger
13450 then -/+ 2GB. */
13451 if (DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype) == 4
13452 && object_64bit
13453 && ((addressT) (displacement_from_opcode_start - extension
4eed87de
AM
13454 + ((addressT) 1 << 31))
13455 > (((addressT) 2 << 31) - 1)))
7b81dfbb
AJ
13456 {
13457 as_bad_where (fragP->fr_file, fragP->fr_line,
13458 _("jump target out of range"));
13459 /* Make us emit 0. */
13460 displacement_from_opcode_start = extension;
13461 }
47926f60 13462 /* Now put displacement after opcode. */
252b5132
RH
13463 md_number_to_chars ((char *) where_to_put_displacement,
13464 (valueT) (displacement_from_opcode_start - extension),
fddf5b5b 13465 DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
252b5132
RH
13466 fragP->fr_fix += extension;
13467}
13468\f
7016a5d5 13469/* Apply a fixup (fixP) to segment data, once it has been determined
252b5132
RH
13470 by our caller that we have all the info we need to fix it up.
13471
7016a5d5
TG
13472 Parameter valP is the pointer to the value of the bits.
13473
252b5132
RH
13474 On the 386, immediates, displacements, and data pointers are all in
13475 the same (little-endian) format, so we don't need to care about which
13476 we are handling. */
13477
94f592af 13478void
7016a5d5 13479md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 13480{
94f592af 13481 char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
c6682705 13482 valueT value = *valP;
252b5132 13483
f86103b7 13484#if !defined (TE_Mach)
93382f6d
AM
13485 if (fixP->fx_pcrel)
13486 {
13487 switch (fixP->fx_r_type)
13488 {
5865bb77
ILT
13489 default:
13490 break;
13491
d6ab8113
JB
13492 case BFD_RELOC_64:
13493 fixP->fx_r_type = BFD_RELOC_64_PCREL;
13494 break;
93382f6d 13495 case BFD_RELOC_32:
ae8887b5 13496 case BFD_RELOC_X86_64_32S:
93382f6d
AM
13497 fixP->fx_r_type = BFD_RELOC_32_PCREL;
13498 break;
13499 case BFD_RELOC_16:
13500 fixP->fx_r_type = BFD_RELOC_16_PCREL;
13501 break;
13502 case BFD_RELOC_8:
13503 fixP->fx_r_type = BFD_RELOC_8_PCREL;
13504 break;
13505 }
13506 }
252b5132 13507
a161fe53 13508 if (fixP->fx_addsy != NULL
31312f95 13509 && (fixP->fx_r_type == BFD_RELOC_32_PCREL
d6ab8113 13510 || fixP->fx_r_type == BFD_RELOC_64_PCREL
31312f95 13511 || fixP->fx_r_type == BFD_RELOC_16_PCREL
d258b828 13512 || fixP->fx_r_type == BFD_RELOC_8_PCREL)
31312f95 13513 && !use_rela_relocations)
252b5132 13514 {
31312f95
AM
13515 /* This is a hack. There should be a better way to handle this.
13516 This covers for the fact that bfd_install_relocation will
13517 subtract the current location (for partial_inplace, PC relative
13518 relocations); see more below. */
252b5132 13519#ifndef OBJ_AOUT
718ddfc0 13520 if (IS_ELF
252b5132
RH
13521#ifdef TE_PE
13522 || OUTPUT_FLAVOR == bfd_target_coff_flavour
13523#endif
13524 )
13525 value += fixP->fx_where + fixP->fx_frag->fr_address;
13526#endif
13527#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 13528 if (IS_ELF)
252b5132 13529 {
6539b54b 13530 segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
2f66722d 13531
6539b54b 13532 if ((sym_seg == seg
2f66722d 13533 || (symbol_section_p (fixP->fx_addsy)
6539b54b 13534 && sym_seg != absolute_section))
af65af87 13535 && !generic_force_reloc (fixP))
2f66722d
AM
13536 {
13537 /* Yes, we add the values in twice. This is because
6539b54b
AM
13538 bfd_install_relocation subtracts them out again. I think
13539 bfd_install_relocation is broken, but I don't dare change
2f66722d
AM
13540 it. FIXME. */
13541 value += fixP->fx_where + fixP->fx_frag->fr_address;
13542 }
252b5132
RH
13543 }
13544#endif
13545#if defined (OBJ_COFF) && defined (TE_PE)
977cdf5a
NC
13546 /* For some reason, the PE format does not store a
13547 section address offset for a PC relative symbol. */
13548 if (S_GET_SEGMENT (fixP->fx_addsy) != seg
7be1c489 13549 || S_IS_WEAK (fixP->fx_addsy))
252b5132
RH
13550 value += md_pcrel_from (fixP);
13551#endif
13552 }
fbeb56a4 13553#if defined (OBJ_COFF) && defined (TE_PE)
f01c1a09
NC
13554 if (fixP->fx_addsy != NULL
13555 && S_IS_WEAK (fixP->fx_addsy)
13556 /* PR 16858: Do not modify weak function references. */
13557 && ! fixP->fx_pcrel)
fbeb56a4 13558 {
296a8689
NC
13559#if !defined (TE_PEP)
13560 /* For x86 PE weak function symbols are neither PC-relative
13561 nor do they set S_IS_FUNCTION. So the only reliable way
13562 to detect them is to check the flags of their containing
13563 section. */
13564 if (S_GET_SEGMENT (fixP->fx_addsy) != NULL
13565 && S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_CODE)
13566 ;
13567 else
13568#endif
fbeb56a4
DK
13569 value -= S_GET_VALUE (fixP->fx_addsy);
13570 }
13571#endif
252b5132
RH
13572
13573 /* Fix a few things - the dynamic linker expects certain values here,
0234cb7c 13574 and we must not disappoint it. */
252b5132 13575#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 13576 if (IS_ELF && fixP->fx_addsy)
47926f60
KH
13577 switch (fixP->fx_r_type)
13578 {
13579 case BFD_RELOC_386_PLT32:
3e73aa7c 13580 case BFD_RELOC_X86_64_PLT32:
b9519cfe
L
13581 /* Make the jump instruction point to the address of the operand.
13582 At runtime we merely add the offset to the actual PLT entry.
13583 NB: Subtract the offset size only for jump instructions. */
13584 if (fixP->fx_pcrel)
13585 value = -4;
47926f60 13586 break;
31312f95 13587
13ae64f3
JJ
13588 case BFD_RELOC_386_TLS_GD:
13589 case BFD_RELOC_386_TLS_LDM:
13ae64f3 13590 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
13591 case BFD_RELOC_386_TLS_IE:
13592 case BFD_RELOC_386_TLS_GOTIE:
67a4f2b7 13593 case BFD_RELOC_386_TLS_GOTDESC:
bffbf940
JJ
13594 case BFD_RELOC_X86_64_TLSGD:
13595 case BFD_RELOC_X86_64_TLSLD:
13596 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7 13597 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
00f7efb6
JJ
13598 value = 0; /* Fully resolved at runtime. No addend. */
13599 /* Fallthrough */
13600 case BFD_RELOC_386_TLS_LE:
13601 case BFD_RELOC_386_TLS_LDO_32:
13602 case BFD_RELOC_386_TLS_LE_32:
13603 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 13604 case BFD_RELOC_X86_64_DTPOFF64:
00f7efb6 13605 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113 13606 case BFD_RELOC_X86_64_TPOFF64:
00f7efb6
JJ
13607 S_SET_THREAD_LOCAL (fixP->fx_addsy);
13608 break;
13609
67a4f2b7
AO
13610 case BFD_RELOC_386_TLS_DESC_CALL:
13611 case BFD_RELOC_X86_64_TLSDESC_CALL:
13612 value = 0; /* Fully resolved at runtime. No addend. */
13613 S_SET_THREAD_LOCAL (fixP->fx_addsy);
13614 fixP->fx_done = 0;
13615 return;
13616
47926f60
KH
13617 case BFD_RELOC_VTABLE_INHERIT:
13618 case BFD_RELOC_VTABLE_ENTRY:
13619 fixP->fx_done = 0;
94f592af 13620 return;
47926f60
KH
13621
13622 default:
13623 break;
13624 }
13625#endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) */
a442cac5
JB
13626
13627 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
13628 if (!object_64bit)
13629 value = extend_to_32bit_address (value);
13630
c6682705 13631 *valP = value;
f86103b7 13632#endif /* !defined (TE_Mach) */
3e73aa7c 13633
3e73aa7c 13634 /* Are we finished with this relocation now? */
c6682705 13635 if (fixP->fx_addsy == NULL)
b8188555
JB
13636 {
13637 fixP->fx_done = 1;
13638 switch (fixP->fx_r_type)
13639 {
13640 case BFD_RELOC_X86_64_32S:
13641 fixP->fx_signed = 1;
13642 break;
13643
13644 default:
13645 break;
13646 }
13647 }
fbeb56a4
DK
13648#if defined (OBJ_COFF) && defined (TE_PE)
13649 else if (fixP->fx_addsy != NULL && S_IS_WEAK (fixP->fx_addsy))
13650 {
13651 fixP->fx_done = 0;
13652 /* Remember value for tc_gen_reloc. */
13653 fixP->fx_addnumber = value;
13654 /* Clear out the frag for now. */
13655 value = 0;
13656 }
13657#endif
3e73aa7c
JH
13658 else if (use_rela_relocations)
13659 {
46fb6d5a
JB
13660 if (!disallow_64bit_reloc || fixP->fx_r_type == NO_RELOC)
13661 fixP->fx_no_overflow = 1;
062cd5e7
AS
13662 /* Remember value for tc_gen_reloc. */
13663 fixP->fx_addnumber = value;
3e73aa7c
JH
13664 value = 0;
13665 }
f86103b7 13666
94f592af 13667 md_number_to_chars (p, value, fixP->fx_size);
252b5132 13668}
252b5132 13669\f
6d4af3c2 13670const char *
499ac353 13671md_atof (int type, char *litP, int *sizeP)
252b5132 13672{
499ac353
NC
13673 /* This outputs the LITTLENUMs in REVERSE order;
13674 in accord with the bigendian 386. */
5b7c81bd 13675 return ieee_md_atof (type, litP, sizeP, false);
252b5132
RH
13676}
13677\f
2d545b82 13678static char output_invalid_buf[sizeof (unsigned char) * 2 + 6];
252b5132 13679
252b5132 13680static char *
e3bb37b5 13681output_invalid (int c)
252b5132 13682{
3882b010 13683 if (ISPRINT (c))
f9f21a03
L
13684 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
13685 "'%c'", c);
252b5132 13686 else
f9f21a03 13687 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
2d545b82 13688 "(0x%x)", (unsigned char) c);
252b5132
RH
13689 return output_invalid_buf;
13690}
13691
8a6fb3f9
JB
13692/* Verify that @r can be used in the current context. */
13693
5b7c81bd 13694static bool check_register (const reg_entry *r)
8a6fb3f9
JB
13695{
13696 if (allow_pseudo_reg)
5b7c81bd 13697 return true;
8a6fb3f9
JB
13698
13699 if (operand_type_all_zero (&r->reg_type))
5b7c81bd 13700 return false;
8a6fb3f9
JB
13701
13702 if ((r->reg_type.bitfield.dword
13703 || (r->reg_type.bitfield.class == SReg && r->reg_num > 3)
13704 || r->reg_type.bitfield.class == RegCR
22e00a3f 13705 || r->reg_type.bitfield.class == RegDR)
8a6fb3f9 13706 && !cpu_arch_flags.bitfield.cpui386)
5b7c81bd 13707 return false;
8a6fb3f9 13708
22e00a3f
JB
13709 if (r->reg_type.bitfield.class == RegTR
13710 && (flag_code == CODE_64BIT
13711 || !cpu_arch_flags.bitfield.cpui386
13712 || cpu_arch_isa_flags.bitfield.cpui586
13713 || cpu_arch_isa_flags.bitfield.cpui686))
5b7c81bd 13714 return false;
22e00a3f 13715
8a6fb3f9 13716 if (r->reg_type.bitfield.class == RegMMX && !cpu_arch_flags.bitfield.cpummx)
5b7c81bd 13717 return false;
8a6fb3f9
JB
13718
13719 if (!cpu_arch_flags.bitfield.cpuavx512f)
13720 {
13721 if (r->reg_type.bitfield.zmmword
13722 || r->reg_type.bitfield.class == RegMask)
5b7c81bd 13723 return false;
8a6fb3f9
JB
13724
13725 if (!cpu_arch_flags.bitfield.cpuavx)
13726 {
13727 if (r->reg_type.bitfield.ymmword)
5b7c81bd 13728 return false;
8a6fb3f9
JB
13729
13730 if (!cpu_arch_flags.bitfield.cpusse && r->reg_type.bitfield.xmmword)
5b7c81bd 13731 return false;
8a6fb3f9
JB
13732 }
13733 }
13734
260cd341
LC
13735 if (r->reg_type.bitfield.tmmword
13736 && (!cpu_arch_flags.bitfield.cpuamx_tile
13737 || flag_code != CODE_64BIT))
5b7c81bd 13738 return false;
260cd341 13739
8a6fb3f9 13740 if (r->reg_type.bitfield.class == RegBND && !cpu_arch_flags.bitfield.cpumpx)
5b7c81bd 13741 return false;
8a6fb3f9
JB
13742
13743 /* Don't allow fake index register unless allow_index_reg isn't 0. */
13744 if (!allow_index_reg && r->reg_num == RegIZ)
5b7c81bd 13745 return false;
8a6fb3f9
JB
13746
13747 /* Upper 16 vector registers are only available with VREX in 64bit
13748 mode, and require EVEX encoding. */
13749 if (r->reg_flags & RegVRex)
13750 {
13751 if (!cpu_arch_flags.bitfield.cpuavx512f
13752 || flag_code != CODE_64BIT)
5b7c81bd 13753 return false;
8a6fb3f9 13754
da4977e0
JB
13755 if (i.vec_encoding == vex_encoding_default)
13756 i.vec_encoding = vex_encoding_evex;
13757 else if (i.vec_encoding != vex_encoding_evex)
13758 i.vec_encoding = vex_encoding_error;
8a6fb3f9
JB
13759 }
13760
13761 if (((r->reg_flags & (RegRex64 | RegRex)) || r->reg_type.bitfield.qword)
0ff3b7d0
JB
13762 && (!cpu_arch_flags.bitfield.cpulm
13763 || r->reg_type.bitfield.class != RegCR
13764 || dot_insn ())
8a6fb3f9 13765 && flag_code != CODE_64BIT)
5b7c81bd 13766 return false;
8a6fb3f9
JB
13767
13768 if (r->reg_type.bitfield.class == SReg && r->reg_num == RegFlat
13769 && !intel_syntax)
5b7c81bd 13770 return false;
8a6fb3f9 13771
5b7c81bd 13772 return true;
8a6fb3f9
JB
13773}
13774
af6bdddf 13775/* REG_STRING starts *before* REGISTER_PREFIX. */
252b5132
RH
13776
13777static const reg_entry *
74e05e01 13778parse_real_register (const char *reg_string, char **end_op)
252b5132 13779{
74e05e01 13780 const char *s = reg_string;
af6bdddf 13781 char *p;
252b5132
RH
13782 char reg_name_given[MAX_REG_NAME_SIZE + 1];
13783 const reg_entry *r;
13784
13785 /* Skip possible REGISTER_PREFIX and possible whitespace. */
13786 if (*s == REGISTER_PREFIX)
13787 ++s;
13788
13789 if (is_space_char (*s))
13790 ++s;
13791
13792 p = reg_name_given;
af6bdddf 13793 while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
252b5132
RH
13794 {
13795 if (p >= reg_name_given + MAX_REG_NAME_SIZE)
af6bdddf
AM
13796 return (const reg_entry *) NULL;
13797 s++;
252b5132
RH
13798 }
13799
4795cd4a 13800 if (is_part_of_name (*s))
6588847e
DN
13801 return (const reg_entry *) NULL;
13802
74e05e01 13803 *end_op = (char *) s;
252b5132 13804
629310ab 13805 r = (const reg_entry *) str_hash_find (reg_hash, reg_name_given);
252b5132 13806
5f47d35b 13807 /* Handle floating point regs, allowing spaces in the (i) part. */
6288d05f 13808 if (r == reg_st0)
5f47d35b 13809 {
0e0eea78
JB
13810 if (!cpu_arch_flags.bitfield.cpu8087
13811 && !cpu_arch_flags.bitfield.cpu287
af32b722
JB
13812 && !cpu_arch_flags.bitfield.cpu387
13813 && !allow_pseudo_reg)
0e0eea78
JB
13814 return (const reg_entry *) NULL;
13815
5f47d35b
AM
13816 if (is_space_char (*s))
13817 ++s;
13818 if (*s == '(')
13819 {
af6bdddf 13820 ++s;
5f47d35b
AM
13821 if (is_space_char (*s))
13822 ++s;
13823 if (*s >= '0' && *s <= '7')
13824 {
db557034 13825 int fpr = *s - '0';
af6bdddf 13826 ++s;
5f47d35b
AM
13827 if (is_space_char (*s))
13828 ++s;
13829 if (*s == ')')
13830 {
74e05e01 13831 *end_op = (char *) s + 1;
6288d05f 13832 know (r[fpr].reg_num == fpr);
db557034 13833 return r + fpr;
5f47d35b 13834 }
5f47d35b 13835 }
47926f60 13836 /* We have "%st(" then garbage. */
5f47d35b
AM
13837 return (const reg_entry *) NULL;
13838 }
13839 }
13840
8a6fb3f9 13841 return r && check_register (r) ? r : NULL;
252b5132 13842}
4d1bb795
JB
13843
13844/* REG_STRING starts *before* REGISTER_PREFIX. */
13845
13846static const reg_entry *
4f081312 13847parse_register (const char *reg_string, char **end_op)
4d1bb795
JB
13848{
13849 const reg_entry *r;
13850
13851 if (*reg_string == REGISTER_PREFIX || allow_naked_reg)
13852 r = parse_real_register (reg_string, end_op);
13853 else
13854 r = NULL;
13855 if (!r)
13856 {
13857 char *save = input_line_pointer;
4f081312 13858 char *buf = xstrdup (reg_string), *name;
4d1bb795
JB
13859 symbolS *symbolP;
13860
4f081312
JB
13861 input_line_pointer = buf;
13862 get_symbol_name (&name);
13863 symbolP = symbol_find (name);
d50c498a 13864 while (symbolP && symbol_equated_p (symbolP))
64d23078
JB
13865 {
13866 const expressionS *e = symbol_get_value_expression(symbolP);
13867
d50c498a 13868 if (e->X_add_number)
64d23078
JB
13869 break;
13870 symbolP = e->X_add_symbol;
13871 }
4d1bb795
JB
13872 if (symbolP && S_GET_SEGMENT (symbolP) == reg_section)
13873 {
13874 const expressionS *e = symbol_get_value_expression (symbolP);
13875
0ccade1a
JB
13876 if (e->X_op == O_register)
13877 {
13878 know (e->X_add_number >= 0
13879 && (valueT) e->X_add_number < i386_regtab_size);
13880 r = i386_regtab + e->X_add_number;
4f081312 13881 *end_op = (char *) reg_string + (input_line_pointer - buf);
0ccade1a
JB
13882 }
13883 if (r && !check_register (r))
8a6fb3f9 13884 {
3b55a1d0
JB
13885 as_bad (_("register '%s%s' cannot be used here"),
13886 register_prefix, r->reg_name);
13887 r = &bad_reg;
8a6fb3f9 13888 }
4d1bb795 13889 }
4d1bb795 13890 input_line_pointer = save;
4f081312 13891 free (buf);
4d1bb795
JB
13892 }
13893 return r;
13894}
13895
13896int
13897i386_parse_name (char *name, expressionS *e, char *nextcharP)
13898{
4faaa10f 13899 const reg_entry *r = NULL;
4d1bb795
JB
13900 char *end = input_line_pointer;
13901
6acf9130
JB
13902 /* We only know the terminating character here. It being double quote could
13903 be the closing one of a quoted symbol name, or an opening one from a
13904 following string (or another quoted symbol name). Since the latter can't
13905 be valid syntax for anything, bailing in either case is good enough. */
13906 if (*nextcharP == '"')
13907 return 0;
13908
4d1bb795 13909 *end = *nextcharP;
4faaa10f
JB
13910 if (*name == REGISTER_PREFIX || allow_naked_reg)
13911 r = parse_real_register (name, &input_line_pointer);
4d1bb795
JB
13912 if (r && end <= input_line_pointer)
13913 {
13914 *nextcharP = *input_line_pointer;
13915 *input_line_pointer = 0;
f847749a
JB
13916 e->X_op = O_register;
13917 e->X_add_number = r - i386_regtab;
4d1bb795
JB
13918 return 1;
13919 }
13920 input_line_pointer = end;
13921 *end = 0;
ee86248c 13922 return intel_syntax ? i386_intel_parse_name (name, e) : 0;
4d1bb795
JB
13923}
13924
13925void
13926md_operand (expressionS *e)
13927{
ee86248c
JB
13928 char *end;
13929 const reg_entry *r;
4d1bb795 13930
ee86248c
JB
13931 switch (*input_line_pointer)
13932 {
13933 case REGISTER_PREFIX:
13934 r = parse_real_register (input_line_pointer, &end);
4d1bb795
JB
13935 if (r)
13936 {
13937 e->X_op = O_register;
13938 e->X_add_number = r - i386_regtab;
13939 input_line_pointer = end;
13940 }
ee86248c
JB
13941 break;
13942
13943 case '[':
9c2799c2 13944 gas_assert (intel_syntax);
ee86248c
JB
13945 end = input_line_pointer++;
13946 expression (e);
13947 if (*input_line_pointer == ']')
13948 {
13949 ++input_line_pointer;
13950 e->X_op_symbol = make_expr_symbol (e);
13951 e->X_add_symbol = NULL;
13952 e->X_add_number = 0;
13953 e->X_op = O_index;
13954 }
13955 else
13956 {
13957 e->X_op = O_absent;
13958 input_line_pointer = end;
13959 }
13960 break;
4d1bb795
JB
13961 }
13962}
13963
5cc00775
JB
13964#ifdef BFD64
13965/* To maintain consistency with !BFD64 builds of gas record, whether any
13966 (binary) operator was involved in an expression. As expressions are
13967 evaluated in only 32 bits when !BFD64, we use this to decide whether to
13968 truncate results. */
13969bool i386_record_operator (operatorT op,
13970 const expressionS *left,
13971 const expressionS *right)
13972{
13973 if (op == O_absent)
13974 return false;
13975
13976 if (!left)
13977 {
13978 /* Since the expression parser applies unary operators fine to bignum
13979 operands, we don't need to be concerned of respective operands not
13980 fitting in 32 bits. */
13981 if (right->X_op == O_constant && right->X_unsigned
13982 && !fits_in_unsigned_long (right->X_add_number))
13983 return false;
13984 }
13985 /* This isn't entirely right: The pattern can also result when constant
13986 expressions are folded (e.g. 0xffffffff + 1). */
13987 else if ((left->X_op == O_constant && left->X_unsigned
13988 && !fits_in_unsigned_long (left->X_add_number))
13989 || (right->X_op == O_constant && right->X_unsigned
13990 && !fits_in_unsigned_long (right->X_add_number)))
13991 expr_mode = expr_large_value;
13992
13993 if (expr_mode != expr_large_value)
13994 expr_mode = expr_operator_present;
13995
13996 return false;
13997}
13998#endif
252b5132 13999\f
4cc782b5 14000#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
b6f8c7c4 14001const char *md_shortopts = "kVQ:sqnO::";
252b5132 14002#else
b6f8c7c4 14003const char *md_shortopts = "qnO::";
252b5132 14004#endif
6e0b89ee 14005
3e73aa7c 14006#define OPTION_32 (OPTION_MD_BASE + 0)
b3b91714
AM
14007#define OPTION_64 (OPTION_MD_BASE + 1)
14008#define OPTION_DIVIDE (OPTION_MD_BASE + 2)
9103f4f4
L
14009#define OPTION_MARCH (OPTION_MD_BASE + 3)
14010#define OPTION_MTUNE (OPTION_MD_BASE + 4)
1efbbeb4
L
14011#define OPTION_MMNEMONIC (OPTION_MD_BASE + 5)
14012#define OPTION_MSYNTAX (OPTION_MD_BASE + 6)
14013#define OPTION_MINDEX_REG (OPTION_MD_BASE + 7)
14014#define OPTION_MNAKED_REG (OPTION_MD_BASE + 8)
bd5dea88 14015#define OPTION_MRELAX_RELOCATIONS (OPTION_MD_BASE + 9)
c0f3af97 14016#define OPTION_MSSE2AVX (OPTION_MD_BASE + 10)
daf50ae7 14017#define OPTION_MSSE_CHECK (OPTION_MD_BASE + 11)
7bab8ab5
JB
14018#define OPTION_MOPERAND_CHECK (OPTION_MD_BASE + 12)
14019#define OPTION_MAVXSCALAR (OPTION_MD_BASE + 13)
14020#define OPTION_X32 (OPTION_MD_BASE + 14)
7e8b059b 14021#define OPTION_MADD_BND_PREFIX (OPTION_MD_BASE + 15)
43234a1e
L
14022#define OPTION_MEVEXLIG (OPTION_MD_BASE + 16)
14023#define OPTION_MEVEXWIG (OPTION_MD_BASE + 17)
167ad85b 14024#define OPTION_MBIG_OBJ (OPTION_MD_BASE + 18)
d1982f93 14025#define OPTION_MOMIT_LOCK_PREFIX (OPTION_MD_BASE + 19)
d3d3c6db 14026#define OPTION_MEVEXRCIG (OPTION_MD_BASE + 20)
8dcea932 14027#define OPTION_MSHARED (OPTION_MD_BASE + 21)
5db04b09
L
14028#define OPTION_MAMD64 (OPTION_MD_BASE + 22)
14029#define OPTION_MINTEL64 (OPTION_MD_BASE + 23)
e4e00185 14030#define OPTION_MFENCE_AS_LOCK_ADD (OPTION_MD_BASE + 24)
b4a3a7b4 14031#define OPTION_X86_USED_NOTE (OPTION_MD_BASE + 25)
03751133 14032#define OPTION_MVEXWIG (OPTION_MD_BASE + 26)
e379e5f3
L
14033#define OPTION_MALIGN_BRANCH_BOUNDARY (OPTION_MD_BASE + 27)
14034#define OPTION_MALIGN_BRANCH_PREFIX_SIZE (OPTION_MD_BASE + 28)
14035#define OPTION_MALIGN_BRANCH (OPTION_MD_BASE + 29)
76cf450b 14036#define OPTION_MBRANCHES_WITH_32B_BOUNDARIES (OPTION_MD_BASE + 30)
ae531041
L
14037#define OPTION_MLFENCE_AFTER_LOAD (OPTION_MD_BASE + 31)
14038#define OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH (OPTION_MD_BASE + 32)
14039#define OPTION_MLFENCE_BEFORE_RET (OPTION_MD_BASE + 33)
c8480b58 14040#define OPTION_MUSE_UNALIGNED_VECTOR_MOVE (OPTION_MD_BASE + 34)
b3b91714 14041
99ad8390
NC
14042struct option md_longopts[] =
14043{
3e73aa7c 14044 {"32", no_argument, NULL, OPTION_32},
321098a5 14045#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 14046 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c 14047 {"64", no_argument, NULL, OPTION_64},
351f65ca
L
14048#endif
14049#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 14050 {"x32", no_argument, NULL, OPTION_X32},
8dcea932 14051 {"mshared", no_argument, NULL, OPTION_MSHARED},
b4a3a7b4 14052 {"mx86-used-note", required_argument, NULL, OPTION_X86_USED_NOTE},
6e0b89ee 14053#endif
b3b91714 14054 {"divide", no_argument, NULL, OPTION_DIVIDE},
9103f4f4
L
14055 {"march", required_argument, NULL, OPTION_MARCH},
14056 {"mtune", required_argument, NULL, OPTION_MTUNE},
1efbbeb4
L
14057 {"mmnemonic", required_argument, NULL, OPTION_MMNEMONIC},
14058 {"msyntax", required_argument, NULL, OPTION_MSYNTAX},
14059 {"mindex-reg", no_argument, NULL, OPTION_MINDEX_REG},
14060 {"mnaked-reg", no_argument, NULL, OPTION_MNAKED_REG},
c0f3af97 14061 {"msse2avx", no_argument, NULL, OPTION_MSSE2AVX},
c8480b58 14062 {"muse-unaligned-vector-move", no_argument, NULL, OPTION_MUSE_UNALIGNED_VECTOR_MOVE},
daf50ae7 14063 {"msse-check", required_argument, NULL, OPTION_MSSE_CHECK},
7bab8ab5 14064 {"moperand-check", required_argument, NULL, OPTION_MOPERAND_CHECK},
539f890d 14065 {"mavxscalar", required_argument, NULL, OPTION_MAVXSCALAR},
03751133 14066 {"mvexwig", required_argument, NULL, OPTION_MVEXWIG},
7e8b059b 14067 {"madd-bnd-prefix", no_argument, NULL, OPTION_MADD_BND_PREFIX},
43234a1e
L
14068 {"mevexlig", required_argument, NULL, OPTION_MEVEXLIG},
14069 {"mevexwig", required_argument, NULL, OPTION_MEVEXWIG},
167ad85b
TG
14070# if defined (TE_PE) || defined (TE_PEP)
14071 {"mbig-obj", no_argument, NULL, OPTION_MBIG_OBJ},
14072#endif
d1982f93 14073 {"momit-lock-prefix", required_argument, NULL, OPTION_MOMIT_LOCK_PREFIX},
e4e00185 14074 {"mfence-as-lock-add", required_argument, NULL, OPTION_MFENCE_AS_LOCK_ADD},
0cb4071e 14075 {"mrelax-relocations", required_argument, NULL, OPTION_MRELAX_RELOCATIONS},
d3d3c6db 14076 {"mevexrcig", required_argument, NULL, OPTION_MEVEXRCIG},
e379e5f3
L
14077 {"malign-branch-boundary", required_argument, NULL, OPTION_MALIGN_BRANCH_BOUNDARY},
14078 {"malign-branch-prefix-size", required_argument, NULL, OPTION_MALIGN_BRANCH_PREFIX_SIZE},
14079 {"malign-branch", required_argument, NULL, OPTION_MALIGN_BRANCH},
76cf450b 14080 {"mbranches-within-32B-boundaries", no_argument, NULL, OPTION_MBRANCHES_WITH_32B_BOUNDARIES},
ae531041
L
14081 {"mlfence-after-load", required_argument, NULL, OPTION_MLFENCE_AFTER_LOAD},
14082 {"mlfence-before-indirect-branch", required_argument, NULL,
14083 OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH},
14084 {"mlfence-before-ret", required_argument, NULL, OPTION_MLFENCE_BEFORE_RET},
5db04b09
L
14085 {"mamd64", no_argument, NULL, OPTION_MAMD64},
14086 {"mintel64", no_argument, NULL, OPTION_MINTEL64},
252b5132
RH
14087 {NULL, no_argument, NULL, 0}
14088};
14089size_t md_longopts_size = sizeof (md_longopts);
14090
14091int
17b9d67d 14092md_parse_option (int c, const char *arg)
252b5132 14093{
91d6fa6a 14094 unsigned int j;
e379e5f3 14095 char *arch, *next, *saved, *type;
9103f4f4 14096
252b5132
RH
14097 switch (c)
14098 {
12b55ccc
L
14099 case 'n':
14100 optimize_align_code = 0;
14101 break;
14102
a38cf1db
AM
14103 case 'q':
14104 quiet_warnings = 1;
252b5132
RH
14105 break;
14106
14107#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
a38cf1db
AM
14108 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
14109 should be emitted or not. FIXME: Not implemented. */
14110 case 'Q':
d4693039
JB
14111 if ((arg[0] != 'y' && arg[0] != 'n') || arg[1])
14112 return 0;
252b5132
RH
14113 break;
14114
14115 /* -V: SVR4 argument to print version ID. */
14116 case 'V':
14117 print_version_id ();
14118 break;
14119
a38cf1db
AM
14120 /* -k: Ignore for FreeBSD compatibility. */
14121 case 'k':
252b5132 14122 break;
4cc782b5
ILT
14123
14124 case 's':
14125 /* -s: On i386 Solaris, this tells the native assembler to use
29b0f896 14126 .stab instead of .stab.excl. We always use .stab anyhow. */
4cc782b5 14127 break;
8dcea932
L
14128
14129 case OPTION_MSHARED:
14130 shared = 1;
14131 break;
b4a3a7b4
L
14132
14133 case OPTION_X86_USED_NOTE:
14134 if (strcasecmp (arg, "yes") == 0)
14135 x86_used_note = 1;
14136 else if (strcasecmp (arg, "no") == 0)
14137 x86_used_note = 0;
14138 else
14139 as_fatal (_("invalid -mx86-used-note= option: `%s'"), arg);
14140 break;
14141
14142
99ad8390 14143#endif
321098a5 14144#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 14145 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c
JH
14146 case OPTION_64:
14147 {
14148 const char **list, **l;
14149
3e73aa7c
JH
14150 list = bfd_target_list ();
14151 for (l = list; *l != NULL; l++)
08dedd66 14152 if (startswith (*l, "elf64-x86-64")
99ad8390
NC
14153 || strcmp (*l, "coff-x86-64") == 0
14154 || strcmp (*l, "pe-x86-64") == 0
d382c579
TG
14155 || strcmp (*l, "pei-x86-64") == 0
14156 || strcmp (*l, "mach-o-x86-64") == 0)
6e0b89ee
AM
14157 {
14158 default_arch = "x86_64";
14159 break;
14160 }
3e73aa7c 14161 if (*l == NULL)
2b5d6a91 14162 as_fatal (_("no compiled in support for x86_64"));
3e73aa7c
JH
14163 free (list);
14164 }
14165 break;
14166#endif
252b5132 14167
351f65ca 14168#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 14169 case OPTION_X32:
351f65ca
L
14170 if (IS_ELF)
14171 {
14172 const char **list, **l;
14173
14174 list = bfd_target_list ();
14175 for (l = list; *l != NULL; l++)
08dedd66 14176 if (startswith (*l, "elf32-x86-64"))
351f65ca
L
14177 {
14178 default_arch = "x86_64:32";
14179 break;
14180 }
14181 if (*l == NULL)
2b5d6a91 14182 as_fatal (_("no compiled in support for 32bit x86_64"));
351f65ca
L
14183 free (list);
14184 }
14185 else
14186 as_fatal (_("32bit x86_64 is only supported for ELF"));
14187 break;
14188#endif
14189
6e0b89ee
AM
14190 case OPTION_32:
14191 default_arch = "i386";
14192 break;
14193
b3b91714
AM
14194 case OPTION_DIVIDE:
14195#ifdef SVR4_COMMENT_CHARS
14196 {
14197 char *n, *t;
14198 const char *s;
14199
add39d23 14200 n = XNEWVEC (char, strlen (i386_comment_chars) + 1);
b3b91714
AM
14201 t = n;
14202 for (s = i386_comment_chars; *s != '\0'; s++)
14203 if (*s != '/')
14204 *t++ = *s;
14205 *t = '\0';
14206 i386_comment_chars = n;
14207 }
14208#endif
14209 break;
14210
9103f4f4 14211 case OPTION_MARCH:
293f5f65
L
14212 saved = xstrdup (arg);
14213 arch = saved;
14214 /* Allow -march=+nosse. */
14215 if (*arch == '+')
14216 arch++;
6305a203 14217 do
9103f4f4 14218 {
6305a203 14219 if (*arch == '.')
2b5d6a91 14220 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
14221 next = strchr (arch, '+');
14222 if (next)
14223 *next++ = '\0';
91d6fa6a 14224 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 14225 {
ae89daec
JB
14226 if (arch == saved && cpu_arch[j].type != PROCESSOR_NONE
14227 && strcmp (arch, cpu_arch[j].name) == 0)
ccc9c027 14228 {
6305a203 14229 /* Processor. */
ae89daec 14230 if (! cpu_arch[j].enable.bitfield.cpui386)
1ded5609
JB
14231 continue;
14232
91d6fa6a 14233 cpu_arch_name = cpu_arch[j].name;
d92c7521 14234 free (cpu_sub_arch_name);
6305a203 14235 cpu_sub_arch_name = NULL;
ae89daec 14236 cpu_arch_flags = cpu_arch[j].enable;
91d6fa6a 14237 cpu_arch_isa = cpu_arch[j].type;
ae89daec 14238 cpu_arch_isa_flags = cpu_arch[j].enable;
6305a203
L
14239 if (!cpu_arch_tune_set)
14240 {
14241 cpu_arch_tune = cpu_arch_isa;
14242 cpu_arch_tune_flags = cpu_arch_isa_flags;
14243 }
14244 break;
14245 }
ae89daec
JB
14246 else if (cpu_arch[j].type == PROCESSOR_NONE
14247 && strcmp (arch, cpu_arch[j].name) == 0
14248 && !cpu_flags_all_zero (&cpu_arch[j].enable))
6305a203 14249 {
33eaf5de 14250 /* ISA extension. */
6305a203 14251 i386_cpu_flags flags;
309d3373 14252
293f5f65 14253 flags = cpu_flags_or (cpu_arch_flags,
ae89daec 14254 cpu_arch[j].enable);
81486035 14255
5b64d091 14256 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
6305a203 14257 {
ae89daec 14258 extend_cpu_sub_arch_name (arch);
6305a203 14259 cpu_arch_flags = flags;
a586129e 14260 cpu_arch_isa_flags = flags;
6305a203 14261 }
0089dace
L
14262 else
14263 cpu_arch_isa_flags
14264 = cpu_flags_or (cpu_arch_isa_flags,
ae89daec 14265 cpu_arch[j].enable);
6305a203 14266 break;
ccc9c027 14267 }
9103f4f4 14268 }
6305a203 14269
ae89daec 14270 if (j >= ARRAY_SIZE (cpu_arch) && startswith (arch, "no"))
293f5f65 14271 {
33eaf5de 14272 /* Disable an ISA extension. */
ae89daec
JB
14273 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
14274 if (cpu_arch[j].type == PROCESSOR_NONE
14275 && strcmp (arch + 2, cpu_arch[j].name) == 0)
293f5f65
L
14276 {
14277 i386_cpu_flags flags;
14278
14279 flags = cpu_flags_and_not (cpu_arch_flags,
ae89daec 14280 cpu_arch[j].disable);
293f5f65
L
14281 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
14282 {
8180707f 14283 extend_cpu_sub_arch_name (arch);
293f5f65
L
14284 cpu_arch_flags = flags;
14285 cpu_arch_isa_flags = flags;
14286 }
14287 break;
14288 }
293f5f65
L
14289 }
14290
91d6fa6a 14291 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 14292 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
14293
14294 arch = next;
9103f4f4 14295 }
293f5f65
L
14296 while (next != NULL);
14297 free (saved);
9103f4f4
L
14298 break;
14299
14300 case OPTION_MTUNE:
14301 if (*arg == '.')
2b5d6a91 14302 as_fatal (_("invalid -mtune= option: `%s'"), arg);
91d6fa6a 14303 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 14304 {
ae89daec
JB
14305 if (cpu_arch[j].type != PROCESSOR_NONE
14306 && strcmp (arg, cpu_arch[j].name) == 0)
9103f4f4 14307 {
ccc9c027 14308 cpu_arch_tune_set = 1;
91d6fa6a 14309 cpu_arch_tune = cpu_arch [j].type;
ae89daec 14310 cpu_arch_tune_flags = cpu_arch[j].enable;
9103f4f4
L
14311 break;
14312 }
14313 }
91d6fa6a 14314 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 14315 as_fatal (_("invalid -mtune= option: `%s'"), arg);
9103f4f4
L
14316 break;
14317
1efbbeb4
L
14318 case OPTION_MMNEMONIC:
14319 if (strcasecmp (arg, "att") == 0)
14320 intel_mnemonic = 0;
14321 else if (strcasecmp (arg, "intel") == 0)
14322 intel_mnemonic = 1;
14323 else
2b5d6a91 14324 as_fatal (_("invalid -mmnemonic= option: `%s'"), arg);
1efbbeb4
L
14325 break;
14326
14327 case OPTION_MSYNTAX:
14328 if (strcasecmp (arg, "att") == 0)
14329 intel_syntax = 0;
14330 else if (strcasecmp (arg, "intel") == 0)
14331 intel_syntax = 1;
14332 else
2b5d6a91 14333 as_fatal (_("invalid -msyntax= option: `%s'"), arg);
1efbbeb4
L
14334 break;
14335
14336 case OPTION_MINDEX_REG:
14337 allow_index_reg = 1;
14338 break;
14339
14340 case OPTION_MNAKED_REG:
14341 allow_naked_reg = 1;
14342 break;
14343
c0f3af97
L
14344 case OPTION_MSSE2AVX:
14345 sse2avx = 1;
14346 break;
14347
c8480b58
L
14348 case OPTION_MUSE_UNALIGNED_VECTOR_MOVE:
14349 use_unaligned_vector_move = 1;
14350 break;
14351
daf50ae7
L
14352 case OPTION_MSSE_CHECK:
14353 if (strcasecmp (arg, "error") == 0)
7bab8ab5 14354 sse_check = check_error;
daf50ae7 14355 else if (strcasecmp (arg, "warning") == 0)
7bab8ab5 14356 sse_check = check_warning;
daf50ae7 14357 else if (strcasecmp (arg, "none") == 0)
7bab8ab5 14358 sse_check = check_none;
daf50ae7 14359 else
2b5d6a91 14360 as_fatal (_("invalid -msse-check= option: `%s'"), arg);
daf50ae7
L
14361 break;
14362
7bab8ab5
JB
14363 case OPTION_MOPERAND_CHECK:
14364 if (strcasecmp (arg, "error") == 0)
14365 operand_check = check_error;
14366 else if (strcasecmp (arg, "warning") == 0)
14367 operand_check = check_warning;
14368 else if (strcasecmp (arg, "none") == 0)
14369 operand_check = check_none;
14370 else
14371 as_fatal (_("invalid -moperand-check= option: `%s'"), arg);
14372 break;
14373
539f890d
L
14374 case OPTION_MAVXSCALAR:
14375 if (strcasecmp (arg, "128") == 0)
14376 avxscalar = vex128;
14377 else if (strcasecmp (arg, "256") == 0)
14378 avxscalar = vex256;
14379 else
2b5d6a91 14380 as_fatal (_("invalid -mavxscalar= option: `%s'"), arg);
539f890d
L
14381 break;
14382
03751133
L
14383 case OPTION_MVEXWIG:
14384 if (strcmp (arg, "0") == 0)
40c9c8de 14385 vexwig = vexw0;
03751133 14386 else if (strcmp (arg, "1") == 0)
40c9c8de 14387 vexwig = vexw1;
03751133
L
14388 else
14389 as_fatal (_("invalid -mvexwig= option: `%s'"), arg);
14390 break;
14391
7e8b059b
L
14392 case OPTION_MADD_BND_PREFIX:
14393 add_bnd_prefix = 1;
14394 break;
14395
43234a1e
L
14396 case OPTION_MEVEXLIG:
14397 if (strcmp (arg, "128") == 0)
14398 evexlig = evexl128;
14399 else if (strcmp (arg, "256") == 0)
14400 evexlig = evexl256;
14401 else if (strcmp (arg, "512") == 0)
14402 evexlig = evexl512;
14403 else
14404 as_fatal (_("invalid -mevexlig= option: `%s'"), arg);
14405 break;
14406
d3d3c6db
IT
14407 case OPTION_MEVEXRCIG:
14408 if (strcmp (arg, "rne") == 0)
14409 evexrcig = rne;
14410 else if (strcmp (arg, "rd") == 0)
14411 evexrcig = rd;
14412 else if (strcmp (arg, "ru") == 0)
14413 evexrcig = ru;
14414 else if (strcmp (arg, "rz") == 0)
14415 evexrcig = rz;
14416 else
14417 as_fatal (_("invalid -mevexrcig= option: `%s'"), arg);
14418 break;
14419
43234a1e
L
14420 case OPTION_MEVEXWIG:
14421 if (strcmp (arg, "0") == 0)
14422 evexwig = evexw0;
14423 else if (strcmp (arg, "1") == 0)
14424 evexwig = evexw1;
14425 else
14426 as_fatal (_("invalid -mevexwig= option: `%s'"), arg);
14427 break;
14428
167ad85b
TG
14429# if defined (TE_PE) || defined (TE_PEP)
14430 case OPTION_MBIG_OBJ:
14431 use_big_obj = 1;
14432 break;
14433#endif
14434
d1982f93 14435 case OPTION_MOMIT_LOCK_PREFIX:
d022bddd
IT
14436 if (strcasecmp (arg, "yes") == 0)
14437 omit_lock_prefix = 1;
14438 else if (strcasecmp (arg, "no") == 0)
14439 omit_lock_prefix = 0;
14440 else
14441 as_fatal (_("invalid -momit-lock-prefix= option: `%s'"), arg);
14442 break;
14443
e4e00185
AS
14444 case OPTION_MFENCE_AS_LOCK_ADD:
14445 if (strcasecmp (arg, "yes") == 0)
14446 avoid_fence = 1;
14447 else if (strcasecmp (arg, "no") == 0)
14448 avoid_fence = 0;
14449 else
14450 as_fatal (_("invalid -mfence-as-lock-add= option: `%s'"), arg);
14451 break;
14452
ae531041
L
14453 case OPTION_MLFENCE_AFTER_LOAD:
14454 if (strcasecmp (arg, "yes") == 0)
14455 lfence_after_load = 1;
14456 else if (strcasecmp (arg, "no") == 0)
14457 lfence_after_load = 0;
14458 else
14459 as_fatal (_("invalid -mlfence-after-load= option: `%s'"), arg);
14460 break;
14461
14462 case OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH:
14463 if (strcasecmp (arg, "all") == 0)
a09f656b 14464 {
14465 lfence_before_indirect_branch = lfence_branch_all;
14466 if (lfence_before_ret == lfence_before_ret_none)
14467 lfence_before_ret = lfence_before_ret_shl;
14468 }
ae531041
L
14469 else if (strcasecmp (arg, "memory") == 0)
14470 lfence_before_indirect_branch = lfence_branch_memory;
14471 else if (strcasecmp (arg, "register") == 0)
14472 lfence_before_indirect_branch = lfence_branch_register;
14473 else if (strcasecmp (arg, "none") == 0)
14474 lfence_before_indirect_branch = lfence_branch_none;
14475 else
14476 as_fatal (_("invalid -mlfence-before-indirect-branch= option: `%s'"),
14477 arg);
14478 break;
14479
14480 case OPTION_MLFENCE_BEFORE_RET:
14481 if (strcasecmp (arg, "or") == 0)
14482 lfence_before_ret = lfence_before_ret_or;
14483 else if (strcasecmp (arg, "not") == 0)
14484 lfence_before_ret = lfence_before_ret_not;
a09f656b 14485 else if (strcasecmp (arg, "shl") == 0 || strcasecmp (arg, "yes") == 0)
14486 lfence_before_ret = lfence_before_ret_shl;
ae531041
L
14487 else if (strcasecmp (arg, "none") == 0)
14488 lfence_before_ret = lfence_before_ret_none;
14489 else
14490 as_fatal (_("invalid -mlfence-before-ret= option: `%s'"),
14491 arg);
14492 break;
14493
0cb4071e
L
14494 case OPTION_MRELAX_RELOCATIONS:
14495 if (strcasecmp (arg, "yes") == 0)
14496 generate_relax_relocations = 1;
14497 else if (strcasecmp (arg, "no") == 0)
14498 generate_relax_relocations = 0;
14499 else
14500 as_fatal (_("invalid -mrelax-relocations= option: `%s'"), arg);
14501 break;
14502
e379e5f3
L
14503 case OPTION_MALIGN_BRANCH_BOUNDARY:
14504 {
14505 char *end;
14506 long int align = strtoul (arg, &end, 0);
14507 if (*end == '\0')
14508 {
14509 if (align == 0)
14510 {
14511 align_branch_power = 0;
14512 break;
14513 }
14514 else if (align >= 16)
14515 {
14516 int align_power;
14517 for (align_power = 0;
14518 (align & 1) == 0;
14519 align >>= 1, align_power++)
14520 continue;
14521 /* Limit alignment power to 31. */
14522 if (align == 1 && align_power < 32)
14523 {
14524 align_branch_power = align_power;
14525 break;
14526 }
14527 }
14528 }
14529 as_fatal (_("invalid -malign-branch-boundary= value: %s"), arg);
14530 }
14531 break;
14532
14533 case OPTION_MALIGN_BRANCH_PREFIX_SIZE:
14534 {
14535 char *end;
14536 int align = strtoul (arg, &end, 0);
14537 /* Some processors only support 5 prefixes. */
14538 if (*end == '\0' && align >= 0 && align < 6)
14539 {
14540 align_branch_prefix_size = align;
14541 break;
14542 }
14543 as_fatal (_("invalid -malign-branch-prefix-size= value: %s"),
14544 arg);
14545 }
14546 break;
14547
14548 case OPTION_MALIGN_BRANCH:
14549 align_branch = 0;
14550 saved = xstrdup (arg);
14551 type = saved;
14552 do
14553 {
14554 next = strchr (type, '+');
14555 if (next)
14556 *next++ = '\0';
14557 if (strcasecmp (type, "jcc") == 0)
14558 align_branch |= align_branch_jcc_bit;
14559 else if (strcasecmp (type, "fused") == 0)
14560 align_branch |= align_branch_fused_bit;
14561 else if (strcasecmp (type, "jmp") == 0)
14562 align_branch |= align_branch_jmp_bit;
14563 else if (strcasecmp (type, "call") == 0)
14564 align_branch |= align_branch_call_bit;
14565 else if (strcasecmp (type, "ret") == 0)
14566 align_branch |= align_branch_ret_bit;
14567 else if (strcasecmp (type, "indirect") == 0)
14568 align_branch |= align_branch_indirect_bit;
14569 else
14570 as_fatal (_("invalid -malign-branch= option: `%s'"), arg);
14571 type = next;
14572 }
14573 while (next != NULL);
14574 free (saved);
14575 break;
14576
76cf450b
L
14577 case OPTION_MBRANCHES_WITH_32B_BOUNDARIES:
14578 align_branch_power = 5;
14579 align_branch_prefix_size = 5;
14580 align_branch = (align_branch_jcc_bit
14581 | align_branch_fused_bit
14582 | align_branch_jmp_bit);
14583 break;
14584
5db04b09 14585 case OPTION_MAMD64:
4b5aaf5f 14586 isa64 = amd64;
5db04b09
L
14587 break;
14588
14589 case OPTION_MINTEL64:
4b5aaf5f 14590 isa64 = intel64;
5db04b09
L
14591 break;
14592
b6f8c7c4
L
14593 case 'O':
14594 if (arg == NULL)
14595 {
14596 optimize = 1;
14597 /* Turn off -Os. */
14598 optimize_for_space = 0;
14599 }
14600 else if (*arg == 's')
14601 {
14602 optimize_for_space = 1;
14603 /* Turn on all encoding optimizations. */
41fd2579 14604 optimize = INT_MAX;
b6f8c7c4
L
14605 }
14606 else
14607 {
14608 optimize = atoi (arg);
14609 /* Turn off -Os. */
14610 optimize_for_space = 0;
14611 }
14612 break;
14613
252b5132
RH
14614 default:
14615 return 0;
14616 }
14617 return 1;
14618}
14619
8a2c8fef
L
14620#define MESSAGE_TEMPLATE \
14621" "
14622
293f5f65
L
14623static char *
14624output_message (FILE *stream, char *p, char *message, char *start,
14625 int *left_p, const char *name, int len)
14626{
14627 int size = sizeof (MESSAGE_TEMPLATE);
14628 int left = *left_p;
14629
14630 /* Reserve 2 spaces for ", " or ",\0" */
14631 left -= len + 2;
14632
14633 /* Check if there is any room. */
14634 if (left >= 0)
14635 {
14636 if (p != start)
14637 {
14638 *p++ = ',';
14639 *p++ = ' ';
14640 }
14641 p = mempcpy (p, name, len);
14642 }
14643 else
14644 {
14645 /* Output the current message now and start a new one. */
14646 *p++ = ',';
14647 *p = '\0';
14648 fprintf (stream, "%s\n", message);
14649 p = start;
14650 left = size - (start - message) - len - 2;
14651
14652 gas_assert (left >= 0);
14653
14654 p = mempcpy (p, name, len);
14655 }
14656
14657 *left_p = left;
14658 return p;
14659}
14660
8a2c8fef 14661static void
1ded5609 14662show_arch (FILE *stream, int ext, int check)
8a2c8fef
L
14663{
14664 static char message[] = MESSAGE_TEMPLATE;
14665 char *start = message + 27;
14666 char *p;
14667 int size = sizeof (MESSAGE_TEMPLATE);
14668 int left;
14669 const char *name;
14670 int len;
14671 unsigned int j;
14672
14673 p = start;
14674 left = size - (start - message);
3ce2ebcf
JB
14675
14676 if (!ext && check)
14677 {
14678 p = output_message (stream, p, message, start, &left,
14679 STRING_COMMA_LEN ("default"));
f68697e8
JB
14680 p = output_message (stream, p, message, start, &left,
14681 STRING_COMMA_LEN ("push"));
14682 p = output_message (stream, p, message, start, &left,
14683 STRING_COMMA_LEN ("pop"));
3ce2ebcf
JB
14684 }
14685
8a2c8fef
L
14686 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
14687 {
14688 /* Should it be skipped? */
14689 if (cpu_arch [j].skip)
14690 continue;
14691
14692 name = cpu_arch [j].name;
14693 len = cpu_arch [j].len;
ae89daec 14694 if (cpu_arch[j].type == PROCESSOR_NONE)
8a2c8fef
L
14695 {
14696 /* It is an extension. Skip if we aren't asked to show it. */
ae89daec 14697 if (!ext || cpu_flags_all_zero (&cpu_arch[j].enable))
8a2c8fef
L
14698 continue;
14699 }
14700 else if (ext)
14701 {
14702 /* It is an processor. Skip if we show only extension. */
14703 continue;
14704 }
ae89daec 14705 else if (check && ! cpu_arch[j].enable.bitfield.cpui386)
1ded5609
JB
14706 {
14707 /* It is an impossible processor - skip. */
14708 continue;
14709 }
8a2c8fef 14710
293f5f65 14711 p = output_message (stream, p, message, start, &left, name, len);
8a2c8fef
L
14712 }
14713
293f5f65
L
14714 /* Display disabled extensions. */
14715 if (ext)
ae89daec 14716 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
293f5f65 14717 {
ae89daec
JB
14718 char *str;
14719
14720 if (cpu_arch[j].type != PROCESSOR_NONE
14721 || !cpu_flags_all_zero (&cpu_arch[j].enable))
14722 continue;
14723 str = xasprintf ("no%s", cpu_arch[j].name);
14724 p = output_message (stream, p, message, start, &left, str,
14725 strlen (str));
14726 free (str);
293f5f65
L
14727 }
14728
8a2c8fef
L
14729 *p = '\0';
14730 fprintf (stream, "%s\n", message);
14731}
14732
252b5132 14733void
8a2c8fef 14734md_show_usage (FILE *stream)
252b5132 14735{
4cc782b5
ILT
14736#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14737 fprintf (stream, _("\
d4693039 14738 -Qy, -Qn ignored\n\
a38cf1db 14739 -V print assembler version number\n\
b3b91714
AM
14740 -k ignored\n"));
14741#endif
14742 fprintf (stream, _("\
7ebd68d1
NC
14743 -n do not optimize code alignment\n\
14744 -O{012s} attempt some code optimizations\n\
b3b91714
AM
14745 -q quieten some warnings\n"));
14746#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14747 fprintf (stream, _("\
a38cf1db 14748 -s ignored\n"));
b3b91714 14749#endif
b00af7c8
JB
14750#ifdef BFD64
14751# if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14752 fprintf (stream, _("\
14753 --32/--64/--x32 generate 32bit/64bit/x32 object\n"));
14754# elif defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O)
751d281c 14755 fprintf (stream, _("\
b00af7c8
JB
14756 --32/--64 generate 32bit/64bit object\n"));
14757# endif
751d281c 14758#endif
b3b91714
AM
14759#ifdef SVR4_COMMENT_CHARS
14760 fprintf (stream, _("\
14761 --divide do not treat `/' as a comment character\n"));
a38cf1db
AM
14762#else
14763 fprintf (stream, _("\
b3b91714 14764 --divide ignored\n"));
4cc782b5 14765#endif
9103f4f4 14766 fprintf (stream, _("\
6305a203 14767 -march=CPU[,+EXTENSION...]\n\
8a2c8fef 14768 generate code for CPU and EXTENSION, CPU is one of:\n"));
1ded5609 14769 show_arch (stream, 0, 1);
8a2c8fef 14770 fprintf (stream, _("\
ae89daec 14771 EXTENSION is combination of (possibly \"no\"-prefixed):\n"));
1ded5609 14772 show_arch (stream, 1, 0);
6305a203 14773 fprintf (stream, _("\
8a2c8fef 14774 -mtune=CPU optimize for CPU, CPU is one of:\n"));
1ded5609 14775 show_arch (stream, 0, 0);
ba104c83 14776 fprintf (stream, _("\
c0f3af97
L
14777 -msse2avx encode SSE instructions with VEX prefix\n"));
14778 fprintf (stream, _("\
c8480b58
L
14779 -muse-unaligned-vector-move\n\
14780 encode aligned vector move as unaligned vector move\n"));
14781 fprintf (stream, _("\
7c5c05ef 14782 -msse-check=[none|error|warning] (default: warning)\n\
daf50ae7
L
14783 check SSE instructions\n"));
14784 fprintf (stream, _("\
7c5c05ef 14785 -moperand-check=[none|error|warning] (default: warning)\n\
7bab8ab5
JB
14786 check operand combinations for validity\n"));
14787 fprintf (stream, _("\
7c5c05ef
L
14788 -mavxscalar=[128|256] (default: 128)\n\
14789 encode scalar AVX instructions with specific vector\n\
539f890d
L
14790 length\n"));
14791 fprintf (stream, _("\
03751133
L
14792 -mvexwig=[0|1] (default: 0)\n\
14793 encode VEX instructions with specific VEX.W value\n\
14794 for VEX.W bit ignored instructions\n"));
14795 fprintf (stream, _("\
7c5c05ef
L
14796 -mevexlig=[128|256|512] (default: 128)\n\
14797 encode scalar EVEX instructions with specific vector\n\
43234a1e
L
14798 length\n"));
14799 fprintf (stream, _("\
7c5c05ef
L
14800 -mevexwig=[0|1] (default: 0)\n\
14801 encode EVEX instructions with specific EVEX.W value\n\
43234a1e
L
14802 for EVEX.W bit ignored instructions\n"));
14803 fprintf (stream, _("\
7c5c05ef 14804 -mevexrcig=[rne|rd|ru|rz] (default: rne)\n\
d3d3c6db
IT
14805 encode EVEX instructions with specific EVEX.RC value\n\
14806 for SAE-only ignored instructions\n"));
14807 fprintf (stream, _("\
7c5c05ef
L
14808 -mmnemonic=[att|intel] "));
14809 if (SYSV386_COMPAT)
14810 fprintf (stream, _("(default: att)\n"));
14811 else
14812 fprintf (stream, _("(default: intel)\n"));
14813 fprintf (stream, _("\
14814 use AT&T/Intel mnemonic\n"));
ba104c83 14815 fprintf (stream, _("\
7c5c05ef
L
14816 -msyntax=[att|intel] (default: att)\n\
14817 use AT&T/Intel syntax\n"));
ba104c83
L
14818 fprintf (stream, _("\
14819 -mindex-reg support pseudo index registers\n"));
14820 fprintf (stream, _("\
14821 -mnaked-reg don't require `%%' prefix for registers\n"));
14822 fprintf (stream, _("\
7e8b059b 14823 -madd-bnd-prefix add BND prefix for all valid branches\n"));
b4a3a7b4 14824#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8dcea932
L
14825 fprintf (stream, _("\
14826 -mshared disable branch optimization for shared code\n"));
b4a3a7b4
L
14827 fprintf (stream, _("\
14828 -mx86-used-note=[no|yes] "));
14829 if (DEFAULT_X86_USED_NOTE)
14830 fprintf (stream, _("(default: yes)\n"));
14831 else
14832 fprintf (stream, _("(default: no)\n"));
14833 fprintf (stream, _("\
14834 generate x86 used ISA and feature properties\n"));
14835#endif
14836#if defined (TE_PE) || defined (TE_PEP)
167ad85b
TG
14837 fprintf (stream, _("\
14838 -mbig-obj generate big object files\n"));
14839#endif
d022bddd 14840 fprintf (stream, _("\
7c5c05ef 14841 -momit-lock-prefix=[no|yes] (default: no)\n\
d022bddd 14842 strip all lock prefixes\n"));
5db04b09 14843 fprintf (stream, _("\
7c5c05ef 14844 -mfence-as-lock-add=[no|yes] (default: no)\n\
e4e00185
AS
14845 encode lfence, mfence and sfence as\n\
14846 lock addl $0x0, (%%{re}sp)\n"));
14847 fprintf (stream, _("\
7c5c05ef
L
14848 -mrelax-relocations=[no|yes] "));
14849 if (DEFAULT_GENERATE_X86_RELAX_RELOCATIONS)
14850 fprintf (stream, _("(default: yes)\n"));
14851 else
14852 fprintf (stream, _("(default: no)\n"));
14853 fprintf (stream, _("\
0cb4071e
L
14854 generate relax relocations\n"));
14855 fprintf (stream, _("\
e379e5f3
L
14856 -malign-branch-boundary=NUM (default: 0)\n\
14857 align branches within NUM byte boundary\n"));
14858 fprintf (stream, _("\
14859 -malign-branch=TYPE[+TYPE...] (default: jcc+fused+jmp)\n\
14860 TYPE is combination of jcc, fused, jmp, call, ret,\n\
14861 indirect\n\
14862 specify types of branches to align\n"));
14863 fprintf (stream, _("\
14864 -malign-branch-prefix-size=NUM (default: 5)\n\
14865 align branches with NUM prefixes per instruction\n"));
14866 fprintf (stream, _("\
76cf450b
L
14867 -mbranches-within-32B-boundaries\n\
14868 align branches within 32 byte boundary\n"));
14869 fprintf (stream, _("\
ae531041
L
14870 -mlfence-after-load=[no|yes] (default: no)\n\
14871 generate lfence after load\n"));
14872 fprintf (stream, _("\
14873 -mlfence-before-indirect-branch=[none|all|register|memory] (default: none)\n\
14874 generate lfence before indirect near branch\n"));
14875 fprintf (stream, _("\
a09f656b 14876 -mlfence-before-ret=[none|or|not|shl|yes] (default: none)\n\
ae531041
L
14877 generate lfence before ret\n"));
14878 fprintf (stream, _("\
7c5c05ef 14879 -mamd64 accept only AMD64 ISA [default]\n"));
5db04b09
L
14880 fprintf (stream, _("\
14881 -mintel64 accept only Intel64 ISA\n"));
252b5132
RH
14882}
14883
3e73aa7c 14884#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
321098a5 14885 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
e57f8c65 14886 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
252b5132
RH
14887
14888/* Pick the target format to use. */
14889
47926f60 14890const char *
e3bb37b5 14891i386_target_format (void)
252b5132 14892{
d34049e8 14893 if (startswith (default_arch, "x86_64"))
351f65ca
L
14894 {
14895 update_code_flag (CODE_64BIT, 1);
14896 if (default_arch[6] == '\0')
7f56bc95 14897 x86_elf_abi = X86_64_ABI;
351f65ca 14898 else
7f56bc95 14899 x86_elf_abi = X86_64_X32_ABI;
351f65ca 14900 }
3e73aa7c 14901 else if (!strcmp (default_arch, "i386"))
78f12dd3 14902 update_code_flag (CODE_32BIT, 1);
5197d474
L
14903 else if (!strcmp (default_arch, "iamcu"))
14904 {
14905 update_code_flag (CODE_32BIT, 1);
14906 if (cpu_arch_isa == PROCESSOR_UNKNOWN)
14907 {
14908 static const i386_cpu_flags iamcu_flags = CPU_IAMCU_FLAGS;
14909 cpu_arch_name = "iamcu";
d92c7521 14910 free (cpu_sub_arch_name);
5197d474
L
14911 cpu_sub_arch_name = NULL;
14912 cpu_arch_flags = iamcu_flags;
14913 cpu_arch_isa = PROCESSOR_IAMCU;
14914 cpu_arch_isa_flags = iamcu_flags;
14915 if (!cpu_arch_tune_set)
14916 {
14917 cpu_arch_tune = cpu_arch_isa;
14918 cpu_arch_tune_flags = cpu_arch_isa_flags;
14919 }
14920 }
8d471ec1 14921 else if (cpu_arch_isa != PROCESSOR_IAMCU)
5197d474
L
14922 as_fatal (_("Intel MCU doesn't support `%s' architecture"),
14923 cpu_arch_name);
14924 }
3e73aa7c 14925 else
2b5d6a91 14926 as_fatal (_("unknown architecture"));
89507696
JB
14927
14928 if (cpu_flags_all_zero (&cpu_arch_isa_flags))
ae89daec 14929 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
89507696 14930 if (cpu_flags_all_zero (&cpu_arch_tune_flags))
ae89daec 14931 cpu_arch_tune_flags = cpu_arch[flag_code == CODE_64BIT].enable;
89507696 14932
252b5132
RH
14933 switch (OUTPUT_FLAVOR)
14934 {
9384f2ff 14935#if defined (OBJ_MAYBE_AOUT) || defined (OBJ_AOUT)
4c63da97 14936 case bfd_target_aout_flavour:
47926f60 14937 return AOUT_TARGET_FORMAT;
4c63da97 14938#endif
9384f2ff
AM
14939#if defined (OBJ_MAYBE_COFF) || defined (OBJ_COFF)
14940# if defined (TE_PE) || defined (TE_PEP)
14941 case bfd_target_coff_flavour:
167ad85b 14942 if (flag_code == CODE_64BIT)
eb19308f
JB
14943 {
14944 object_64bit = 1;
14945 return use_big_obj ? "pe-bigobj-x86-64" : "pe-x86-64";
14946 }
14947 return use_big_obj ? "pe-bigobj-i386" : "pe-i386";
9384f2ff 14948# elif defined (TE_GO32)
0561d57c
JK
14949 case bfd_target_coff_flavour:
14950 return "coff-go32";
9384f2ff 14951# else
252b5132
RH
14952 case bfd_target_coff_flavour:
14953 return "coff-i386";
9384f2ff 14954# endif
4c63da97 14955#endif
3e73aa7c 14956#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
252b5132 14957 case bfd_target_elf_flavour:
3e73aa7c 14958 {
351f65ca
L
14959 const char *format;
14960
14961 switch (x86_elf_abi)
4fa24527 14962 {
351f65ca
L
14963 default:
14964 format = ELF_TARGET_FORMAT;
e379e5f3
L
14965#ifndef TE_SOLARIS
14966 tls_get_addr = "___tls_get_addr";
14967#endif
351f65ca 14968 break;
7f56bc95 14969 case X86_64_ABI:
351f65ca 14970 use_rela_relocations = 1;
4fa24527 14971 object_64bit = 1;
e379e5f3
L
14972#ifndef TE_SOLARIS
14973 tls_get_addr = "__tls_get_addr";
14974#endif
351f65ca
L
14975 format = ELF_TARGET_FORMAT64;
14976 break;
7f56bc95 14977 case X86_64_X32_ABI:
4fa24527 14978 use_rela_relocations = 1;
351f65ca 14979 object_64bit = 1;
e379e5f3
L
14980#ifndef TE_SOLARIS
14981 tls_get_addr = "__tls_get_addr";
14982#endif
862be3fb 14983 disallow_64bit_reloc = 1;
351f65ca
L
14984 format = ELF_TARGET_FORMAT32;
14985 break;
4fa24527 14986 }
c085ab00 14987 if (cpu_arch_isa == PROCESSOR_IAMCU)
81486035
L
14988 {
14989 if (x86_elf_abi != I386_ABI)
14990 as_fatal (_("Intel MCU is 32bit only"));
14991 return ELF_TARGET_IAMCU_FORMAT;
14992 }
8a9036a4 14993 else
351f65ca 14994 return format;
3e73aa7c 14995 }
e57f8c65
TG
14996#endif
14997#if defined (OBJ_MACH_O)
14998 case bfd_target_mach_o_flavour:
d382c579
TG
14999 if (flag_code == CODE_64BIT)
15000 {
15001 use_rela_relocations = 1;
15002 object_64bit = 1;
15003 return "mach-o-x86-64";
15004 }
15005 else
15006 return "mach-o-i386";
4c63da97 15007#endif
252b5132
RH
15008 default:
15009 abort ();
15010 return NULL;
15011 }
15012}
15013
47926f60 15014#endif /* OBJ_MAYBE_ more than one */
252b5132 15015\f
252b5132 15016symbolS *
7016a5d5 15017md_undefined_symbol (char *name)
252b5132 15018{
18dc2407
ILT
15019 if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
15020 && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
15021 && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
15022 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
24eab124
AM
15023 {
15024 if (!GOT_symbol)
15025 {
15026 if (symbol_find (name))
15027 as_bad (_("GOT already in symbol table"));
15028 GOT_symbol = symbol_new (name, undefined_section,
e01e1cee 15029 &zero_address_frag, 0);
24eab124
AM
15030 };
15031 return GOT_symbol;
15032 }
252b5132
RH
15033 return 0;
15034}
15035
15036/* Round up a section size to the appropriate boundary. */
47926f60 15037
252b5132 15038valueT
7016a5d5 15039md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
252b5132 15040{
4c63da97
AM
15041#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
15042 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
15043 {
15044 /* For a.out, force the section size to be aligned. If we don't do
15045 this, BFD will align it for us, but it will not write out the
15046 final bytes of the section. This may be a bug in BFD, but it is
15047 easier to fix it here since that is how the other a.out targets
15048 work. */
15049 int align;
15050
fd361982 15051 align = bfd_section_alignment (segment);
8d3842cd 15052 size = ((size + (1 << align) - 1) & (-((valueT) 1 << align)));
4c63da97 15053 }
252b5132
RH
15054#endif
15055
15056 return size;
15057}
15058
15059/* On the i386, PC-relative offsets are relative to the start of the
15060 next instruction. That is, the address of the offset, plus its
15061 size, since the offset is always the last part of the insn. */
15062
15063long
e3bb37b5 15064md_pcrel_from (fixS *fixP)
252b5132
RH
15065{
15066 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
15067}
15068
15069#ifndef I386COFF
15070
15071static void
e3bb37b5 15072s_bss (int ignore ATTRIBUTE_UNUSED)
252b5132 15073{
29b0f896 15074 int temp;
252b5132 15075
8a75718c
JB
15076#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15077 if (IS_ELF)
15078 obj_elf_section_change_hook ();
15079#endif
252b5132
RH
15080 temp = get_absolute_expression ();
15081 subseg_set (bss_section, (subsegT) temp);
15082 demand_empty_rest_of_line ();
15083}
15084
15085#endif
15086
e379e5f3
L
15087/* Remember constant directive. */
15088
15089void
15090i386_cons_align (int ignore ATTRIBUTE_UNUSED)
15091{
15092 if (last_insn.kind != last_insn_directive
15093 && (bfd_section_flags (now_seg) & SEC_CODE))
15094 {
15095 last_insn.seg = now_seg;
15096 last_insn.kind = last_insn_directive;
15097 last_insn.name = "constant directive";
15098 last_insn.file = as_where (&last_insn.line);
ae531041
L
15099 if (lfence_before_ret != lfence_before_ret_none)
15100 {
15101 if (lfence_before_indirect_branch != lfence_branch_none)
15102 as_warn (_("constant directive skips -mlfence-before-ret "
15103 "and -mlfence-before-indirect-branch"));
15104 else
15105 as_warn (_("constant directive skips -mlfence-before-ret"));
15106 }
15107 else if (lfence_before_indirect_branch != lfence_branch_none)
15108 as_warn (_("constant directive skips -mlfence-before-indirect-branch"));
e379e5f3
L
15109 }
15110}
15111
3abbafc2 15112int
e3bb37b5 15113i386_validate_fix (fixS *fixp)
252b5132 15114{
e52a16f2
JB
15115 if (fixp->fx_addsy && S_GET_SEGMENT(fixp->fx_addsy) == reg_section)
15116 {
15117 reloc_howto_type *howto;
15118
15119 howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
15120 as_bad_where (fixp->fx_file, fixp->fx_line,
15121 _("invalid %s relocation against register"),
15122 howto ? howto->name : "<unknown>");
15123 return 0;
15124 }
15125
3abbafc2
JB
15126#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15127 if (fixp->fx_r_type == BFD_RELOC_SIZE32
15128 || fixp->fx_r_type == BFD_RELOC_SIZE64)
15129 return IS_ELF && fixp->fx_addsy
15130 && (!S_IS_DEFINED (fixp->fx_addsy)
15131 || S_IS_EXTERNAL (fixp->fx_addsy));
15132#endif
15133
02a86693 15134 if (fixp->fx_subsy)
252b5132 15135 {
02a86693 15136 if (fixp->fx_subsy == GOT_symbol)
23df1078 15137 {
02a86693
L
15138 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
15139 {
15140 if (!object_64bit)
15141 abort ();
15142#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15143 if (fixp->fx_tcbit2)
56ceb5b5
L
15144 fixp->fx_r_type = (fixp->fx_tcbit
15145 ? BFD_RELOC_X86_64_REX_GOTPCRELX
15146 : BFD_RELOC_X86_64_GOTPCRELX);
02a86693
L
15147 else
15148#endif
15149 fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
15150 }
d6ab8113 15151 else
02a86693
L
15152 {
15153 if (!object_64bit)
15154 fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
15155 else
15156 fixp->fx_r_type = BFD_RELOC_X86_64_GOTOFF64;
15157 }
15158 fixp->fx_subsy = 0;
23df1078 15159 }
252b5132 15160 }
02a86693 15161#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2585b7a5 15162 else
02a86693 15163 {
2585b7a5
L
15164 /* NB: Commit 292676c1 resolved PLT32 reloc aganst local symbol
15165 to section. Since PLT32 relocation must be against symbols,
15166 turn such PLT32 relocation into PC32 relocation. */
15167 if (fixp->fx_addsy
15168 && (fixp->fx_r_type == BFD_RELOC_386_PLT32
15169 || fixp->fx_r_type == BFD_RELOC_X86_64_PLT32)
15170 && symbol_section_p (fixp->fx_addsy))
15171 fixp->fx_r_type = BFD_RELOC_32_PCREL;
15172 if (!object_64bit)
15173 {
15174 if (fixp->fx_r_type == BFD_RELOC_386_GOT32
15175 && fixp->fx_tcbit2)
15176 fixp->fx_r_type = BFD_RELOC_386_GOT32X;
15177 }
02a86693
L
15178 }
15179#endif
3abbafc2
JB
15180
15181 return 1;
252b5132
RH
15182}
15183
252b5132 15184arelent *
7016a5d5 15185tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
15186{
15187 arelent *rel;
15188 bfd_reloc_code_real_type code;
15189
15190 switch (fixp->fx_r_type)
15191 {
8ce3d284 15192#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3abbafc2
JB
15193 symbolS *sym;
15194
8fd4256d
L
15195 case BFD_RELOC_SIZE32:
15196 case BFD_RELOC_SIZE64:
3abbafc2
JB
15197 if (fixp->fx_addsy
15198 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))
15199 && (!fixp->fx_subsy
15200 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))))
15201 sym = fixp->fx_addsy;
15202 else if (fixp->fx_subsy
15203 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))
15204 && (!fixp->fx_addsy
15205 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))))
15206 sym = fixp->fx_subsy;
15207 else
15208 sym = NULL;
15209 if (IS_ELF && sym && S_IS_DEFINED (sym) && !S_IS_EXTERNAL (sym))
8fd4256d
L
15210 {
15211 /* Resolve size relocation against local symbol to size of
15212 the symbol plus addend. */
3abbafc2 15213 valueT value = S_GET_SIZE (sym);
44f87162 15214
3abbafc2
JB
15215 if (symbol_get_bfdsym (sym)->flags & BSF_SECTION_SYM)
15216 value = bfd_section_size (S_GET_SEGMENT (sym));
15217 if (sym == fixp->fx_subsy)
15218 {
15219 value = -value;
15220 if (fixp->fx_addsy)
15221 value += S_GET_VALUE (fixp->fx_addsy);
15222 }
15223 else if (fixp->fx_subsy)
15224 value -= S_GET_VALUE (fixp->fx_subsy);
44f87162 15225 value += fixp->fx_offset;
8fd4256d 15226 if (fixp->fx_r_type == BFD_RELOC_SIZE32
d965814f 15227 && object_64bit
8fd4256d
L
15228 && !fits_in_unsigned_long (value))
15229 as_bad_where (fixp->fx_file, fixp->fx_line,
15230 _("symbol size computation overflow"));
15231 fixp->fx_addsy = NULL;
15232 fixp->fx_subsy = NULL;
15233 md_apply_fix (fixp, (valueT *) &value, NULL);
15234 return NULL;
15235 }
3abbafc2
JB
15236 if (!fixp->fx_addsy || fixp->fx_subsy)
15237 {
15238 as_bad_where (fixp->fx_file, fixp->fx_line,
15239 "unsupported expression involving @size");
15240 return NULL;
15241 }
8ce3d284 15242#endif
1a0670f3 15243 /* Fall through. */
8fd4256d 15244
3e73aa7c
JH
15245 case BFD_RELOC_X86_64_PLT32:
15246 case BFD_RELOC_X86_64_GOT32:
15247 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
15248 case BFD_RELOC_X86_64_GOTPCRELX:
15249 case BFD_RELOC_X86_64_REX_GOTPCRELX:
252b5132
RH
15250 case BFD_RELOC_386_PLT32:
15251 case BFD_RELOC_386_GOT32:
02a86693 15252 case BFD_RELOC_386_GOT32X:
252b5132
RH
15253 case BFD_RELOC_386_GOTOFF:
15254 case BFD_RELOC_386_GOTPC:
13ae64f3
JJ
15255 case BFD_RELOC_386_TLS_GD:
15256 case BFD_RELOC_386_TLS_LDM:
15257 case BFD_RELOC_386_TLS_LDO_32:
15258 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
15259 case BFD_RELOC_386_TLS_IE:
15260 case BFD_RELOC_386_TLS_GOTIE:
13ae64f3
JJ
15261 case BFD_RELOC_386_TLS_LE_32:
15262 case BFD_RELOC_386_TLS_LE:
67a4f2b7
AO
15263 case BFD_RELOC_386_TLS_GOTDESC:
15264 case BFD_RELOC_386_TLS_DESC_CALL:
bffbf940
JJ
15265 case BFD_RELOC_X86_64_TLSGD:
15266 case BFD_RELOC_X86_64_TLSLD:
15267 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 15268 case BFD_RELOC_X86_64_DTPOFF64:
bffbf940
JJ
15269 case BFD_RELOC_X86_64_GOTTPOFF:
15270 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113
JB
15271 case BFD_RELOC_X86_64_TPOFF64:
15272 case BFD_RELOC_X86_64_GOTOFF64:
15273 case BFD_RELOC_X86_64_GOTPC32:
7b81dfbb
AJ
15274 case BFD_RELOC_X86_64_GOT64:
15275 case BFD_RELOC_X86_64_GOTPCREL64:
15276 case BFD_RELOC_X86_64_GOTPC64:
15277 case BFD_RELOC_X86_64_GOTPLT64:
15278 case BFD_RELOC_X86_64_PLTOFF64:
67a4f2b7
AO
15279 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
15280 case BFD_RELOC_X86_64_TLSDESC_CALL:
252b5132
RH
15281 case BFD_RELOC_RVA:
15282 case BFD_RELOC_VTABLE_ENTRY:
15283 case BFD_RELOC_VTABLE_INHERIT:
6482c264
NC
15284#ifdef TE_PE
15285 case BFD_RELOC_32_SECREL:
145667f8 15286 case BFD_RELOC_16_SECIDX:
6482c264 15287#endif
252b5132
RH
15288 code = fixp->fx_r_type;
15289 break;
dbbaec26
L
15290 case BFD_RELOC_X86_64_32S:
15291 if (!fixp->fx_pcrel)
15292 {
15293 /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32. */
15294 code = fixp->fx_r_type;
15295 break;
15296 }
1a0670f3 15297 /* Fall through. */
252b5132 15298 default:
93382f6d 15299 if (fixp->fx_pcrel)
252b5132 15300 {
93382f6d
AM
15301 switch (fixp->fx_size)
15302 {
15303 default:
b091f402
AM
15304 as_bad_where (fixp->fx_file, fixp->fx_line,
15305 _("can not do %d byte pc-relative relocation"),
15306 fixp->fx_size);
93382f6d
AM
15307 code = BFD_RELOC_32_PCREL;
15308 break;
15309 case 1: code = BFD_RELOC_8_PCREL; break;
15310 case 2: code = BFD_RELOC_16_PCREL; break;
d258b828 15311 case 4: code = BFD_RELOC_32_PCREL; break;
d6ab8113
JB
15312#ifdef BFD64
15313 case 8: code = BFD_RELOC_64_PCREL; break;
15314#endif
93382f6d
AM
15315 }
15316 }
15317 else
15318 {
15319 switch (fixp->fx_size)
15320 {
15321 default:
b091f402
AM
15322 as_bad_where (fixp->fx_file, fixp->fx_line,
15323 _("can not do %d byte relocation"),
15324 fixp->fx_size);
93382f6d
AM
15325 code = BFD_RELOC_32;
15326 break;
15327 case 1: code = BFD_RELOC_8; break;
15328 case 2: code = BFD_RELOC_16; break;
15329 case 4: code = BFD_RELOC_32; break;
937149dd 15330#ifdef BFD64
3e73aa7c 15331 case 8: code = BFD_RELOC_64; break;
937149dd 15332#endif
93382f6d 15333 }
252b5132
RH
15334 }
15335 break;
15336 }
252b5132 15337
d182319b
JB
15338 if ((code == BFD_RELOC_32
15339 || code == BFD_RELOC_32_PCREL
15340 || code == BFD_RELOC_X86_64_32S)
252b5132
RH
15341 && GOT_symbol
15342 && fixp->fx_addsy == GOT_symbol)
3e73aa7c 15343 {
4fa24527 15344 if (!object_64bit)
d6ab8113
JB
15345 code = BFD_RELOC_386_GOTPC;
15346 else
15347 code = BFD_RELOC_X86_64_GOTPC32;
3e73aa7c 15348 }
7b81dfbb
AJ
15349 if ((code == BFD_RELOC_64 || code == BFD_RELOC_64_PCREL)
15350 && GOT_symbol
15351 && fixp->fx_addsy == GOT_symbol)
15352 {
15353 code = BFD_RELOC_X86_64_GOTPC64;
15354 }
252b5132 15355
add39d23
TS
15356 rel = XNEW (arelent);
15357 rel->sym_ptr_ptr = XNEW (asymbol *);
49309057 15358 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
15359
15360 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
c87db184 15361
3e73aa7c
JH
15362 if (!use_rela_relocations)
15363 {
15364 /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
15365 vtable entry to be used in the relocation's section offset. */
15366 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
15367 rel->address = fixp->fx_offset;
fbeb56a4
DK
15368#if defined (OBJ_COFF) && defined (TE_PE)
15369 else if (fixp->fx_addsy && S_IS_WEAK (fixp->fx_addsy))
15370 rel->addend = fixp->fx_addnumber - (S_GET_VALUE (fixp->fx_addsy) * 2);
15371 else
15372#endif
c6682705 15373 rel->addend = 0;
3e73aa7c
JH
15374 }
15375 /* Use the rela in 64bit mode. */
252b5132 15376 else
3e73aa7c 15377 {
862be3fb
L
15378 if (disallow_64bit_reloc)
15379 switch (code)
15380 {
862be3fb
L
15381 case BFD_RELOC_X86_64_DTPOFF64:
15382 case BFD_RELOC_X86_64_TPOFF64:
15383 case BFD_RELOC_64_PCREL:
15384 case BFD_RELOC_X86_64_GOTOFF64:
15385 case BFD_RELOC_X86_64_GOT64:
15386 case BFD_RELOC_X86_64_GOTPCREL64:
15387 case BFD_RELOC_X86_64_GOTPC64:
15388 case BFD_RELOC_X86_64_GOTPLT64:
15389 case BFD_RELOC_X86_64_PLTOFF64:
15390 as_bad_where (fixp->fx_file, fixp->fx_line,
15391 _("cannot represent relocation type %s in x32 mode"),
15392 bfd_get_reloc_code_name (code));
15393 break;
15394 default:
15395 break;
15396 }
15397
062cd5e7
AS
15398 if (!fixp->fx_pcrel)
15399 rel->addend = fixp->fx_offset;
15400 else
15401 switch (code)
15402 {
15403 case BFD_RELOC_X86_64_PLT32:
15404 case BFD_RELOC_X86_64_GOT32:
15405 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
15406 case BFD_RELOC_X86_64_GOTPCRELX:
15407 case BFD_RELOC_X86_64_REX_GOTPCRELX:
bffbf940
JJ
15408 case BFD_RELOC_X86_64_TLSGD:
15409 case BFD_RELOC_X86_64_TLSLD:
15410 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7
AO
15411 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
15412 case BFD_RELOC_X86_64_TLSDESC_CALL:
062cd5e7
AS
15413 rel->addend = fixp->fx_offset - fixp->fx_size;
15414 break;
15415 default:
15416 rel->addend = (section->vma
15417 - fixp->fx_size
15418 + fixp->fx_addnumber
15419 + md_pcrel_from (fixp));
15420 break;
15421 }
3e73aa7c
JH
15422 }
15423
252b5132
RH
15424 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
15425 if (rel->howto == NULL)
15426 {
15427 as_bad_where (fixp->fx_file, fixp->fx_line,
d0b47220 15428 _("cannot represent relocation type %s"),
252b5132
RH
15429 bfd_get_reloc_code_name (code));
15430 /* Set howto to a garbage value so that we can keep going. */
15431 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
9c2799c2 15432 gas_assert (rel->howto != NULL);
252b5132
RH
15433 }
15434
15435 return rel;
15436}
15437
ee86248c 15438#include "tc-i386-intel.c"
54cfded0 15439
a60de03c
JB
15440void
15441tc_x86_parse_to_dw2regnum (expressionS *exp)
54cfded0 15442{
a60de03c
JB
15443 int saved_naked_reg;
15444 char saved_register_dot;
54cfded0 15445
a60de03c
JB
15446 saved_naked_reg = allow_naked_reg;
15447 allow_naked_reg = 1;
15448 saved_register_dot = register_chars['.'];
15449 register_chars['.'] = '.';
15450 allow_pseudo_reg = 1;
15451 expression_and_evaluate (exp);
15452 allow_pseudo_reg = 0;
15453 register_chars['.'] = saved_register_dot;
15454 allow_naked_reg = saved_naked_reg;
15455
e96d56a1 15456 if (exp->X_op == O_register && exp->X_add_number >= 0)
54cfded0 15457 {
a60de03c
JB
15458 if ((addressT) exp->X_add_number < i386_regtab_size)
15459 {
15460 exp->X_op = O_constant;
15461 exp->X_add_number = i386_regtab[exp->X_add_number]
15462 .dw2_regnum[flag_code >> 1];
15463 }
15464 else
15465 exp->X_op = O_illegal;
54cfded0 15466 }
54cfded0
AM
15467}
15468
15469void
15470tc_x86_frame_initial_instructions (void)
15471{
a60de03c
JB
15472 static unsigned int sp_regno[2];
15473
15474 if (!sp_regno[flag_code >> 1])
15475 {
15476 char *saved_input = input_line_pointer;
15477 char sp[][4] = {"esp", "rsp"};
15478 expressionS exp;
a4447b93 15479
a60de03c
JB
15480 input_line_pointer = sp[flag_code >> 1];
15481 tc_x86_parse_to_dw2regnum (&exp);
9c2799c2 15482 gas_assert (exp.X_op == O_constant);
a60de03c
JB
15483 sp_regno[flag_code >> 1] = exp.X_add_number;
15484 input_line_pointer = saved_input;
15485 }
a4447b93 15486
61ff971f
L
15487 cfi_add_CFA_def_cfa (sp_regno[flag_code >> 1], -x86_cie_data_alignment);
15488 cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
54cfded0 15489}
d2b2c203 15490
d7921315
L
15491int
15492x86_dwarf2_addr_size (void)
15493{
15494#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
15495 if (x86_elf_abi == X86_64_X32_ABI)
15496 return 4;
15497#endif
15498 return bfd_arch_bits_per_address (stdoutput) / 8;
15499}
15500
d2b2c203
DJ
15501int
15502i386_elf_section_type (const char *str, size_t len)
15503{
15504 if (flag_code == CODE_64BIT
15505 && len == sizeof ("unwind") - 1
d34049e8 15506 && startswith (str, "unwind"))
d2b2c203
DJ
15507 return SHT_X86_64_UNWIND;
15508
15509 return -1;
15510}
bb41ade5 15511
ad5fec3b
EB
15512#ifdef TE_SOLARIS
15513void
15514i386_solaris_fix_up_eh_frame (segT sec)
15515{
15516 if (flag_code == CODE_64BIT)
15517 elf_section_type (sec) = SHT_X86_64_UNWIND;
15518}
15519#endif
15520
bb41ade5
AM
15521#ifdef TE_PE
15522void
15523tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
15524{
91d6fa6a 15525 expressionS exp;
bb41ade5 15526
91d6fa6a
NC
15527 exp.X_op = O_secrel;
15528 exp.X_add_symbol = symbol;
15529 exp.X_add_number = 0;
15530 emit_expr (&exp, size);
bb41ade5
AM
15531}
15532#endif
3b22753a
L
15533
15534#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15535/* For ELF on x86-64, add support for SHF_X86_64_LARGE. */
15536
01e1a5bc 15537bfd_vma
6d4af3c2 15538x86_64_section_letter (int letter, const char **ptr_msg)
3b22753a
L
15539{
15540 if (flag_code == CODE_64BIT)
15541 {
15542 if (letter == 'l')
15543 return SHF_X86_64_LARGE;
15544
8f3bae45 15545 *ptr_msg = _("bad .section directive: want a,l,w,x,M,S,G,T in string");
64e74474 15546 }
3b22753a 15547 else
8f3bae45 15548 *ptr_msg = _("bad .section directive: want a,w,x,M,S,G,T in string");
3b22753a
L
15549 return -1;
15550}
15551
01e1a5bc 15552bfd_vma
3b22753a
L
15553x86_64_section_word (char *str, size_t len)
15554{
08dedd66 15555 if (len == 5 && flag_code == CODE_64BIT && startswith (str, "large"))
3b22753a
L
15556 return SHF_X86_64_LARGE;
15557
15558 return -1;
15559}
15560
15561static void
15562handle_large_common (int small ATTRIBUTE_UNUSED)
15563{
15564 if (flag_code != CODE_64BIT)
15565 {
15566 s_comm_internal (0, elf_common_parse);
15567 as_warn (_(".largecomm supported only in 64bit mode, producing .comm"));
15568 }
15569 else
15570 {
15571 static segT lbss_section;
15572 asection *saved_com_section_ptr = elf_com_section_ptr;
15573 asection *saved_bss_section = bss_section;
15574
15575 if (lbss_section == NULL)
15576 {
15577 flagword applicable;
15578 segT seg = now_seg;
15579 subsegT subseg = now_subseg;
15580
15581 /* The .lbss section is for local .largecomm symbols. */
15582 lbss_section = subseg_new (".lbss", 0);
15583 applicable = bfd_applicable_section_flags (stdoutput);
fd361982 15584 bfd_set_section_flags (lbss_section, applicable & SEC_ALLOC);
3b22753a
L
15585 seg_info (lbss_section)->bss = 1;
15586
15587 subseg_set (seg, subseg);
15588 }
15589
15590 elf_com_section_ptr = &_bfd_elf_large_com_section;
15591 bss_section = lbss_section;
15592
15593 s_comm_internal (0, elf_common_parse);
15594
15595 elf_com_section_ptr = saved_com_section_ptr;
15596 bss_section = saved_bss_section;
15597 }
15598}
15599#endif /* OBJ_ELF || OBJ_MAYBE_ELF */