x86/Intel: drop unnecessary bracket matching from parse_operands()
[deliverable/binutils-gdb.git] / gas / config / tc-i386.c
CommitLineData
b534c6d3 1/* tc-i386.c -- Assemble code for the Intel 80386
250d07de 2 Copyright (C) 1989-2021 Free Software Foundation, Inc.
252b5132
RH
3
4 This file is part of GAS, the GNU Assembler.
5
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
ec2655a6 8 the Free Software Foundation; either version 3, or (at your option)
252b5132
RH
9 any later version.
10
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to the Free
4b4da160
NC
18 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
19 02110-1301, USA. */
252b5132 20
47926f60
KH
21/* Intel 80386 machine specific gas.
22 Written by Eliot Dresselhaus (eliot@mgm.mit.edu).
3e73aa7c 23 x86_64 support by Jan Hubicka (jh@suse.cz)
0f10071e 24 VIA PadLock support by Michal Ludvig (mludvig@suse.cz)
47926f60
KH
25 Bugs & suggestions are completely welcome. This is free software.
26 Please help us make it better. */
252b5132 27
252b5132 28#include "as.h"
3882b010 29#include "safe-ctype.h"
252b5132 30#include "subsegs.h"
316e2c05 31#include "dwarf2dbg.h"
54cfded0 32#include "dw2gencfi.h"
d2b2c203 33#include "elf/x86-64.h"
40fb9820 34#include "opcodes/i386-init.h"
41fd2579 35#include <limits.h>
41fd2579 36
c3332e24 37#ifndef INFER_ADDR_PREFIX
eecb386c 38#define INFER_ADDR_PREFIX 1
c3332e24
AM
39#endif
40
29b0f896
AM
41#ifndef DEFAULT_ARCH
42#define DEFAULT_ARCH "i386"
246fcdee 43#endif
252b5132 44
edde18a5
AM
45#ifndef INLINE
46#if __GNUC__ >= 2
47#define INLINE __inline__
48#else
49#define INLINE
50#endif
51#endif
52
6305a203
L
53/* Prefixes will be emitted in the order defined below.
54 WAIT_PREFIX must be the first prefix since FWAIT is really is an
55 instruction, and so must come before any prefixes.
56 The preferred prefix order is SEG_PREFIX, ADDR_PREFIX, DATA_PREFIX,
42164a71 57 REP_PREFIX/HLE_PREFIX, LOCK_PREFIX. */
6305a203
L
58#define WAIT_PREFIX 0
59#define SEG_PREFIX 1
60#define ADDR_PREFIX 2
61#define DATA_PREFIX 3
c32fa91d 62#define REP_PREFIX 4
42164a71 63#define HLE_PREFIX REP_PREFIX
7e8b059b 64#define BND_PREFIX REP_PREFIX
c32fa91d 65#define LOCK_PREFIX 5
4e9ac44a
L
66#define REX_PREFIX 6 /* must come last. */
67#define MAX_PREFIXES 7 /* max prefixes per opcode */
6305a203
L
68
69/* we define the syntax here (modulo base,index,scale syntax) */
70#define REGISTER_PREFIX '%'
71#define IMMEDIATE_PREFIX '$'
72#define ABSOLUTE_PREFIX '*'
73
74/* these are the instruction mnemonic suffixes in AT&T syntax or
75 memory operand size in Intel syntax. */
76#define WORD_MNEM_SUFFIX 'w'
77#define BYTE_MNEM_SUFFIX 'b'
78#define SHORT_MNEM_SUFFIX 's'
79#define LONG_MNEM_SUFFIX 'l'
80#define QWORD_MNEM_SUFFIX 'q'
6305a203
L
81/* Intel Syntax. Use a non-ascii letter since since it never appears
82 in instructions. */
83#define LONG_DOUBLE_MNEM_SUFFIX '\1'
84
85#define END_OF_INSN '\0'
86
79dec6b7
JB
87/* This matches the C -> StaticRounding alias in the opcode table. */
88#define commutative staticrounding
89
6305a203
L
90/*
91 'templates' is for grouping together 'template' structures for opcodes
92 of the same name. This is only used for storing the insns in the grand
93 ole hash table of insns.
94 The templates themselves start at START and range up to (but not including)
95 END.
96 */
97typedef struct
98{
d3ce72d0
NC
99 const insn_template *start;
100 const insn_template *end;
6305a203
L
101}
102templates;
103
104/* 386 operand encoding bytes: see 386 book for details of this. */
105typedef struct
106{
107 unsigned int regmem; /* codes register or memory operand */
108 unsigned int reg; /* codes register operand (or extended opcode) */
109 unsigned int mode; /* how to interpret regmem & reg */
110}
111modrm_byte;
112
113/* x86-64 extension prefix. */
114typedef int rex_byte;
115
6305a203
L
116/* 386 opcode byte to code indirect addressing. */
117typedef struct
118{
119 unsigned base;
120 unsigned index;
121 unsigned scale;
122}
123sib_byte;
124
6305a203
L
125/* x86 arch names, types and features */
126typedef struct
127{
128 const char *name; /* arch name */
8a2c8fef 129 unsigned int len; /* arch string length */
6305a203
L
130 enum processor_type type; /* arch type */
131 i386_cpu_flags flags; /* cpu feature flags */
8a2c8fef 132 unsigned int skip; /* show_arch should skip this. */
6305a203
L
133}
134arch_entry;
135
293f5f65
L
136/* Used to turn off indicated flags. */
137typedef struct
138{
139 const char *name; /* arch name */
140 unsigned int len; /* arch string length */
141 i386_cpu_flags flags; /* cpu feature flags */
142}
143noarch_entry;
144
78f12dd3 145static void update_code_flag (int, int);
e3bb37b5
L
146static void set_code_flag (int);
147static void set_16bit_gcc_code_flag (int);
148static void set_intel_syntax (int);
1efbbeb4 149static void set_intel_mnemonic (int);
db51cc60 150static void set_allow_index_reg (int);
7bab8ab5 151static void set_check (int);
e3bb37b5 152static void set_cpu_arch (int);
6482c264 153#ifdef TE_PE
e3bb37b5 154static void pe_directive_secrel (int);
6482c264 155#endif
e3bb37b5
L
156static void signed_cons (int);
157static char *output_invalid (int c);
ee86248c
JB
158static int i386_finalize_immediate (segT, expressionS *, i386_operand_type,
159 const char *);
160static int i386_finalize_displacement (segT, expressionS *, i386_operand_type,
161 const char *);
a7619375 162static int i386_att_operand (char *);
e3bb37b5 163static int i386_intel_operand (char *, int);
ee86248c
JB
164static int i386_intel_simplify (expressionS *);
165static int i386_intel_parse_name (const char *, expressionS *);
e3bb37b5
L
166static const reg_entry *parse_register (char *, char **);
167static char *parse_insn (char *, char *);
168static char *parse_operands (char *, const char *);
169static void swap_operands (void);
783c187b 170static void swap_2_operands (unsigned int, unsigned int);
48bcea9f 171static enum flag_code i386_addressing_mode (void);
e3bb37b5
L
172static void optimize_imm (void);
173static void optimize_disp (void);
83b16ac6 174static const insn_template *match_template (char);
e3bb37b5
L
175static int check_string (void);
176static int process_suffix (void);
177static int check_byte_reg (void);
178static int check_long_reg (void);
179static int check_qword_reg (void);
180static int check_word_reg (void);
181static int finalize_imm (void);
182static int process_operands (void);
5e042380 183static const reg_entry *build_modrm_byte (void);
e3bb37b5
L
184static void output_insn (void);
185static void output_imm (fragS *, offsetT);
186static void output_disp (fragS *, offsetT);
29b0f896 187#ifndef I386COFF
e3bb37b5 188static void s_bss (int);
252b5132 189#endif
17d4e2a2
L
190#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
191static void handle_large_common (int small ATTRIBUTE_UNUSED);
b4a3a7b4
L
192
193/* GNU_PROPERTY_X86_ISA_1_USED. */
194static unsigned int x86_isa_1_used;
195/* GNU_PROPERTY_X86_FEATURE_2_USED. */
196static unsigned int x86_feature_2_used;
197/* Generate x86 used ISA and feature properties. */
198static unsigned int x86_used_note = DEFAULT_X86_USED_NOTE;
17d4e2a2 199#endif
252b5132 200
a847613f 201static const char *default_arch = DEFAULT_ARCH;
3e73aa7c 202
8a6fb3f9
JB
203/* parse_register() returns this when a register alias cannot be used. */
204static const reg_entry bad_reg = { "<bad>", OPERAND_TYPE_NONE, 0, 0,
205 { Dw2Inval, Dw2Inval } };
206
34684862 207static const reg_entry *reg_eax;
5e042380
JB
208static const reg_entry *reg_ds;
209static const reg_entry *reg_es;
210static const reg_entry *reg_ss;
6288d05f 211static const reg_entry *reg_st0;
6225c532
JB
212static const reg_entry *reg_k0;
213
c0f3af97
L
214/* VEX prefix. */
215typedef struct
216{
43234a1e
L
217 /* VEX prefix is either 2 byte or 3 byte. EVEX is 4 byte. */
218 unsigned char bytes[4];
c0f3af97
L
219 unsigned int length;
220 /* Destination or source register specifier. */
221 const reg_entry *register_specifier;
222} vex_prefix;
223
252b5132 224/* 'md_assemble ()' gathers together information and puts it into a
47926f60 225 i386_insn. */
252b5132 226
520dc8e8
AM
227union i386_op
228 {
229 expressionS *disps;
230 expressionS *imms;
231 const reg_entry *regs;
232 };
233
a65babc9
L
234enum i386_error
235 {
86e026a4 236 operand_size_mismatch,
a65babc9
L
237 operand_type_mismatch,
238 register_type_mismatch,
239 number_of_operands_mismatch,
240 invalid_instruction_suffix,
241 bad_imm4,
a65babc9
L
242 unsupported_with_intel_mnemonic,
243 unsupported_syntax,
6c30d220 244 unsupported,
260cd341 245 invalid_sib_address,
6c30d220 246 invalid_vsib_address,
7bab8ab5 247 invalid_vector_register_set,
260cd341 248 invalid_tmm_register_set,
43234a1e
L
249 unsupported_vector_index_register,
250 unsupported_broadcast,
43234a1e
L
251 broadcast_needed,
252 unsupported_masking,
253 mask_not_on_destination,
254 no_default_mask,
255 unsupported_rc_sae,
256 rc_sae_operand_not_last_imm,
257 invalid_register_operand,
a65babc9
L
258 };
259
252b5132
RH
260struct _i386_insn
261 {
47926f60 262 /* TM holds the template for the insn were currently assembling. */
d3ce72d0 263 insn_template tm;
252b5132 264
7d5e4556
L
265 /* SUFFIX holds the instruction size suffix for byte, word, dword
266 or qword, if given. */
252b5132
RH
267 char suffix;
268
9a182d04
JB
269 /* OPCODE_LENGTH holds the number of base opcode bytes. */
270 unsigned char opcode_length;
271
47926f60 272 /* OPERANDS gives the number of given operands. */
252b5132
RH
273 unsigned int operands;
274
275 /* REG_OPERANDS, DISP_OPERANDS, MEM_OPERANDS, IMM_OPERANDS give the number
276 of given register, displacement, memory operands and immediate
47926f60 277 operands. */
252b5132
RH
278 unsigned int reg_operands, disp_operands, mem_operands, imm_operands;
279
280 /* TYPES [i] is the type (see above #defines) which tells us how to
520dc8e8 281 use OP[i] for the corresponding operand. */
40fb9820 282 i386_operand_type types[MAX_OPERANDS];
252b5132 283
520dc8e8
AM
284 /* Displacement expression, immediate expression, or register for each
285 operand. */
286 union i386_op op[MAX_OPERANDS];
252b5132 287
3e73aa7c
JH
288 /* Flags for operands. */
289 unsigned int flags[MAX_OPERANDS];
290#define Operand_PCrel 1
c48dadc9 291#define Operand_Mem 2
3e73aa7c 292
252b5132 293 /* Relocation type for operand */
f86103b7 294 enum bfd_reloc_code_real reloc[MAX_OPERANDS];
252b5132 295
252b5132
RH
296 /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
297 the base index byte below. */
298 const reg_entry *base_reg;
299 const reg_entry *index_reg;
300 unsigned int log2_scale_factor;
301
302 /* SEG gives the seg_entries of this insn. They are zero unless
47926f60 303 explicit segment overrides are given. */
5e042380 304 const reg_entry *seg[2];
252b5132 305
8325cc63
JB
306 /* Copied first memory operand string, for re-checking. */
307 char *memop1_string;
308
252b5132
RH
309 /* PREFIX holds all the given prefix opcodes (usually null).
310 PREFIXES is the number of prefix opcodes. */
311 unsigned int prefixes;
312 unsigned char prefix[MAX_PREFIXES];
313
50128d0c 314 /* Register is in low 3 bits of opcode. */
5b7c81bd 315 bool short_form;
50128d0c 316
6f2f06be 317 /* The operand to a branch insn indicates an absolute branch. */
5b7c81bd 318 bool jumpabsolute;
6f2f06be 319
921eafea
L
320 /* Extended states. */
321 enum
322 {
323 /* Use MMX state. */
324 xstate_mmx = 1 << 0,
325 /* Use XMM state. */
326 xstate_xmm = 1 << 1,
327 /* Use YMM state. */
328 xstate_ymm = 1 << 2 | xstate_xmm,
329 /* Use ZMM state. */
330 xstate_zmm = 1 << 3 | xstate_ymm,
331 /* Use TMM state. */
32930e4e
L
332 xstate_tmm = 1 << 4,
333 /* Use MASK state. */
334 xstate_mask = 1 << 5
921eafea 335 } xstate;
260cd341 336
e379e5f3 337 /* Has GOTPC or TLS relocation. */
5b7c81bd 338 bool has_gotpc_tls_reloc;
e379e5f3 339
252b5132 340 /* RM and SIB are the modrm byte and the sib byte where the
c1e679ec 341 addressing modes of this insn are encoded. */
252b5132 342 modrm_byte rm;
3e73aa7c 343 rex_byte rex;
43234a1e 344 rex_byte vrex;
252b5132 345 sib_byte sib;
c0f3af97 346 vex_prefix vex;
b6169b20 347
6225c532
JB
348 /* Masking attributes.
349
350 The struct describes masking, applied to OPERAND in the instruction.
351 REG is a pointer to the corresponding mask register. ZEROING tells
352 whether merging or zeroing mask is used. */
353 struct Mask_Operation
354 {
355 const reg_entry *reg;
356 unsigned int zeroing;
357 /* The operand where this operation is associated. */
358 unsigned int operand;
359 } mask;
43234a1e
L
360
361 /* Rounding control and SAE attributes. */
ca5312a2
JB
362 struct RC_Operation
363 {
364 enum rc_type
365 {
366 rc_none = -1,
367 rne,
368 rd,
369 ru,
370 rz,
371 saeonly
372 } type;
373
374 unsigned int operand;
375 } rounding;
43234a1e 376
5273a3cd
JB
377 /* Broadcasting attributes.
378
379 The struct describes broadcasting, applied to OPERAND. TYPE is
380 expresses the broadcast factor. */
381 struct Broadcast_Operation
382 {
383 /* Type of broadcast: {1to2}, {1to4}, {1to8}, or {1to16}. */
384 unsigned int type;
385
386 /* Index of broadcasted operand. */
387 unsigned int operand;
388
389 /* Number of bytes to broadcast. */
390 unsigned int bytes;
391 } broadcast;
43234a1e
L
392
393 /* Compressed disp8*N attribute. */
394 unsigned int memshift;
395
86fa6981
L
396 /* Prefer load or store in encoding. */
397 enum
398 {
399 dir_encoding_default = 0,
400 dir_encoding_load,
64c49ab3
JB
401 dir_encoding_store,
402 dir_encoding_swap
86fa6981 403 } dir_encoding;
891edac4 404
41eb8e88 405 /* Prefer 8bit, 16bit, 32bit displacement in encoding. */
a501d77e
L
406 enum
407 {
408 disp_encoding_default = 0,
409 disp_encoding_8bit,
41eb8e88 410 disp_encoding_16bit,
a501d77e
L
411 disp_encoding_32bit
412 } disp_encoding;
f8a5c266 413
6b6b6807 414 /* Prefer the REX byte in encoding. */
5b7c81bd 415 bool rex_encoding;
6b6b6807 416
b6f8c7c4 417 /* Disable instruction size optimization. */
5b7c81bd 418 bool no_optimize;
b6f8c7c4 419
86fa6981
L
420 /* How to encode vector instructions. */
421 enum
422 {
423 vex_encoding_default = 0,
42e04b36 424 vex_encoding_vex,
86fa6981 425 vex_encoding_vex3,
da4977e0
JB
426 vex_encoding_evex,
427 vex_encoding_error
86fa6981
L
428 } vec_encoding;
429
d5de92cf
L
430 /* REP prefix. */
431 const char *rep_prefix;
432
165de32a
L
433 /* HLE prefix. */
434 const char *hle_prefix;
42164a71 435
7e8b059b
L
436 /* Have BND prefix. */
437 const char *bnd_prefix;
438
04ef582a
L
439 /* Have NOTRACK prefix. */
440 const char *notrack_prefix;
441
891edac4 442 /* Error message. */
a65babc9 443 enum i386_error error;
252b5132
RH
444 };
445
446typedef struct _i386_insn i386_insn;
447
43234a1e
L
448/* Link RC type with corresponding string, that'll be looked for in
449 asm. */
450struct RC_name
451{
452 enum rc_type type;
453 const char *name;
454 unsigned int len;
455};
456
457static const struct RC_name RC_NamesTable[] =
458{
459 { rne, STRING_COMMA_LEN ("rn-sae") },
460 { rd, STRING_COMMA_LEN ("rd-sae") },
461 { ru, STRING_COMMA_LEN ("ru-sae") },
462 { rz, STRING_COMMA_LEN ("rz-sae") },
463 { saeonly, STRING_COMMA_LEN ("sae") },
464};
465
252b5132
RH
466/* List of chars besides those in app.c:symbol_chars that can start an
467 operand. Used to prevent the scrubber eating vital white-space. */
86fa6981 468const char extra_symbol_chars[] = "*%-([{}"
252b5132 469#ifdef LEX_AT
32137342
NC
470 "@"
471#endif
472#ifdef LEX_QM
473 "?"
252b5132 474#endif
32137342 475 ;
252b5132 476
b3983e5f
JB
477#if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
478 && !defined (TE_GNU) \
479 && !defined (TE_LINUX) \
480 && !defined (TE_FreeBSD) \
481 && !defined (TE_DragonFly) \
482 && !defined (TE_NetBSD))
252b5132 483/* This array holds the chars that always start a comment. If the
b3b91714
AM
484 pre-processor is disabled, these aren't very useful. The option
485 --divide will remove '/' from this list. */
486const char *i386_comment_chars = "#/";
487#define SVR4_COMMENT_CHARS 1
252b5132 488#define PREFIX_SEPARATOR '\\'
252b5132 489
b3b91714
AM
490#else
491const char *i386_comment_chars = "#";
492#define PREFIX_SEPARATOR '/'
493#endif
494
252b5132
RH
495/* This array holds the chars that only start a comment at the beginning of
496 a line. If the line seems to have the form '# 123 filename'
ce8a8b2f
AM
497 .line and .file directives will appear in the pre-processed output.
498 Note that input_file.c hand checks for '#' at the beginning of the
252b5132 499 first line of the input file. This is because the compiler outputs
ce8a8b2f
AM
500 #NO_APP at the beginning of its output.
501 Also note that comments started like this one will always work if
252b5132 502 '/' isn't otherwise defined. */
b3b91714 503const char line_comment_chars[] = "#/";
252b5132 504
63a0b638 505const char line_separator_chars[] = ";";
252b5132 506
ce8a8b2f
AM
507/* Chars that can be used to separate mant from exp in floating point
508 nums. */
252b5132
RH
509const char EXP_CHARS[] = "eE";
510
ce8a8b2f
AM
511/* Chars that mean this number is a floating point constant
512 As in 0f12.456
513 or 0d1.2345e12. */
252b5132
RH
514const char FLT_CHARS[] = "fFdDxX";
515
ce8a8b2f 516/* Tables for lexical analysis. */
252b5132
RH
517static char mnemonic_chars[256];
518static char register_chars[256];
519static char operand_chars[256];
520static char identifier_chars[256];
521static char digit_chars[256];
522
ce8a8b2f 523/* Lexical macros. */
252b5132
RH
524#define is_mnemonic_char(x) (mnemonic_chars[(unsigned char) x])
525#define is_operand_char(x) (operand_chars[(unsigned char) x])
526#define is_register_char(x) (register_chars[(unsigned char) x])
527#define is_space_char(x) ((x) == ' ')
528#define is_identifier_char(x) (identifier_chars[(unsigned char) x])
529#define is_digit_char(x) (digit_chars[(unsigned char) x])
530
0234cb7c 531/* All non-digit non-letter characters that may occur in an operand. */
252b5132
RH
532static char operand_special_chars[] = "%$-+(,)*._~/<>|&^!:[@]";
533
534/* md_assemble() always leaves the strings it's passed unaltered. To
535 effect this we maintain a stack of saved characters that we've smashed
536 with '\0's (indicating end of strings for various sub-fields of the
47926f60 537 assembler instruction). */
252b5132 538static char save_stack[32];
ce8a8b2f 539static char *save_stack_p;
252b5132
RH
540#define END_STRING_AND_SAVE(s) \
541 do { *save_stack_p++ = *(s); *(s) = '\0'; } while (0)
542#define RESTORE_END_STRING(s) \
543 do { *(s) = *--save_stack_p; } while (0)
544
47926f60 545/* The instruction we're assembling. */
252b5132
RH
546static i386_insn i;
547
548/* Possible templates for current insn. */
549static const templates *current_templates;
550
31b2323c
L
551/* Per instruction expressionS buffers: max displacements & immediates. */
552static expressionS disp_expressions[MAX_MEMORY_OPERANDS];
553static expressionS im_expressions[MAX_IMMEDIATE_OPERANDS];
252b5132 554
47926f60 555/* Current operand we are working on. */
ee86248c 556static int this_operand = -1;
252b5132 557
3e73aa7c
JH
558/* We support four different modes. FLAG_CODE variable is used to distinguish
559 these. */
560
561enum flag_code {
562 CODE_32BIT,
563 CODE_16BIT,
564 CODE_64BIT };
565
566static enum flag_code flag_code;
4fa24527 567static unsigned int object_64bit;
862be3fb 568static unsigned int disallow_64bit_reloc;
3e73aa7c 569static int use_rela_relocations = 0;
e379e5f3
L
570/* __tls_get_addr/___tls_get_addr symbol for TLS. */
571static const char *tls_get_addr;
3e73aa7c 572
7af8ed2d
NC
573#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
574 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
575 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
576
351f65ca
L
577/* The ELF ABI to use. */
578enum x86_elf_abi
579{
580 I386_ABI,
7f56bc95
L
581 X86_64_ABI,
582 X86_64_X32_ABI
351f65ca
L
583};
584
585static enum x86_elf_abi x86_elf_abi = I386_ABI;
7af8ed2d 586#endif
351f65ca 587
167ad85b
TG
588#if defined (TE_PE) || defined (TE_PEP)
589/* Use big object file format. */
590static int use_big_obj = 0;
591#endif
592
8dcea932
L
593#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
594/* 1 if generating code for a shared library. */
595static int shared = 0;
596#endif
597
47926f60
KH
598/* 1 for intel syntax,
599 0 if att syntax. */
600static int intel_syntax = 0;
252b5132 601
4b5aaf5f
L
602static enum x86_64_isa
603{
604 amd64 = 1, /* AMD64 ISA. */
605 intel64 /* Intel64 ISA. */
606} isa64;
e89c5eaa 607
1efbbeb4
L
608/* 1 for intel mnemonic,
609 0 if att mnemonic. */
610static int intel_mnemonic = !SYSV386_COMPAT;
611
a60de03c
JB
612/* 1 if pseudo registers are permitted. */
613static int allow_pseudo_reg = 0;
614
47926f60
KH
615/* 1 if register prefix % not required. */
616static int allow_naked_reg = 0;
252b5132 617
33eaf5de 618/* 1 if the assembler should add BND prefix for all control-transferring
7e8b059b
L
619 instructions supporting it, even if this prefix wasn't specified
620 explicitly. */
621static int add_bnd_prefix = 0;
622
ba104c83 623/* 1 if pseudo index register, eiz/riz, is allowed . */
db51cc60
L
624static int allow_index_reg = 0;
625
d022bddd
IT
626/* 1 if the assembler should ignore LOCK prefix, even if it was
627 specified explicitly. */
628static int omit_lock_prefix = 0;
629
e4e00185
AS
630/* 1 if the assembler should encode lfence, mfence, and sfence as
631 "lock addl $0, (%{re}sp)". */
632static int avoid_fence = 0;
633
ae531041
L
634/* 1 if lfence should be inserted after every load. */
635static int lfence_after_load = 0;
636
637/* Non-zero if lfence should be inserted before indirect branch. */
638static enum lfence_before_indirect_branch_kind
639 {
640 lfence_branch_none = 0,
641 lfence_branch_register,
642 lfence_branch_memory,
643 lfence_branch_all
644 }
645lfence_before_indirect_branch;
646
647/* Non-zero if lfence should be inserted before ret. */
648static enum lfence_before_ret_kind
649 {
650 lfence_before_ret_none = 0,
651 lfence_before_ret_not,
a09f656b 652 lfence_before_ret_or,
653 lfence_before_ret_shl
ae531041
L
654 }
655lfence_before_ret;
656
657/* Types of previous instruction is .byte or prefix. */
e379e5f3
L
658static struct
659 {
660 segT seg;
661 const char *file;
662 const char *name;
663 unsigned int line;
664 enum last_insn_kind
665 {
666 last_insn_other = 0,
667 last_insn_directive,
668 last_insn_prefix
669 } kind;
670 } last_insn;
671
0cb4071e
L
672/* 1 if the assembler should generate relax relocations. */
673
674static int generate_relax_relocations
675 = DEFAULT_GENERATE_X86_RELAX_RELOCATIONS;
676
7bab8ab5 677static enum check_kind
daf50ae7 678 {
7bab8ab5
JB
679 check_none = 0,
680 check_warning,
681 check_error
daf50ae7 682 }
7bab8ab5 683sse_check, operand_check = check_warning;
daf50ae7 684
e379e5f3
L
685/* Non-zero if branches should be aligned within power of 2 boundary. */
686static int align_branch_power = 0;
687
688/* Types of branches to align. */
689enum align_branch_kind
690 {
691 align_branch_none = 0,
692 align_branch_jcc = 1,
693 align_branch_fused = 2,
694 align_branch_jmp = 3,
695 align_branch_call = 4,
696 align_branch_indirect = 5,
697 align_branch_ret = 6
698 };
699
700/* Type bits of branches to align. */
701enum align_branch_bit
702 {
703 align_branch_jcc_bit = 1 << align_branch_jcc,
704 align_branch_fused_bit = 1 << align_branch_fused,
705 align_branch_jmp_bit = 1 << align_branch_jmp,
706 align_branch_call_bit = 1 << align_branch_call,
707 align_branch_indirect_bit = 1 << align_branch_indirect,
708 align_branch_ret_bit = 1 << align_branch_ret
709 };
710
711static unsigned int align_branch = (align_branch_jcc_bit
712 | align_branch_fused_bit
713 | align_branch_jmp_bit);
714
79d72f45
HL
715/* Types of condition jump used by macro-fusion. */
716enum mf_jcc_kind
717 {
718 mf_jcc_jo = 0, /* base opcode 0x70 */
719 mf_jcc_jc, /* base opcode 0x72 */
720 mf_jcc_je, /* base opcode 0x74 */
721 mf_jcc_jna, /* base opcode 0x76 */
722 mf_jcc_js, /* base opcode 0x78 */
723 mf_jcc_jp, /* base opcode 0x7a */
724 mf_jcc_jl, /* base opcode 0x7c */
725 mf_jcc_jle, /* base opcode 0x7e */
726 };
727
728/* Types of compare flag-modifying insntructions used by macro-fusion. */
729enum mf_cmp_kind
730 {
731 mf_cmp_test_and, /* test/cmp */
732 mf_cmp_alu_cmp, /* add/sub/cmp */
733 mf_cmp_incdec /* inc/dec */
734 };
735
e379e5f3
L
736/* The maximum padding size for fused jcc. CMP like instruction can
737 be 9 bytes and jcc can be 6 bytes. Leave room just in case for
738 prefixes. */
739#define MAX_FUSED_JCC_PADDING_SIZE 20
740
741/* The maximum number of prefixes added for an instruction. */
742static unsigned int align_branch_prefix_size = 5;
743
b6f8c7c4
L
744/* Optimization:
745 1. Clear the REX_W bit with register operand if possible.
746 2. Above plus use 128bit vector instruction to clear the full vector
747 register.
748 */
749static int optimize = 0;
750
751/* Optimization:
752 1. Clear the REX_W bit with register operand if possible.
753 2. Above plus use 128bit vector instruction to clear the full vector
754 register.
755 3. Above plus optimize "test{q,l,w} $imm8,%r{64,32,16}" to
756 "testb $imm7,%r8".
757 */
758static int optimize_for_space = 0;
759
2ca3ace5
L
760/* Register prefix used for error message. */
761static const char *register_prefix = "%";
762
47926f60
KH
763/* Used in 16 bit gcc mode to add an l suffix to call, ret, enter,
764 leave, push, and pop instructions so that gcc has the same stack
765 frame as in 32 bit mode. */
766static char stackop_size = '\0';
eecb386c 767
12b55ccc
L
768/* Non-zero to optimize code alignment. */
769int optimize_align_code = 1;
770
47926f60
KH
771/* Non-zero to quieten some warnings. */
772static int quiet_warnings = 0;
a38cf1db 773
47926f60
KH
774/* CPU name. */
775static const char *cpu_arch_name = NULL;
6305a203 776static char *cpu_sub_arch_name = NULL;
a38cf1db 777
47926f60 778/* CPU feature flags. */
40fb9820
L
779static i386_cpu_flags cpu_arch_flags = CPU_UNKNOWN_FLAGS;
780
ccc9c027
L
781/* If we have selected a cpu we are generating instructions for. */
782static int cpu_arch_tune_set = 0;
783
9103f4f4 784/* Cpu we are generating instructions for. */
fbf3f584 785enum processor_type cpu_arch_tune = PROCESSOR_UNKNOWN;
9103f4f4
L
786
787/* CPU feature flags of cpu we are generating instructions for. */
40fb9820 788static i386_cpu_flags cpu_arch_tune_flags;
9103f4f4 789
ccc9c027 790/* CPU instruction set architecture used. */
fbf3f584 791enum processor_type cpu_arch_isa = PROCESSOR_UNKNOWN;
ccc9c027 792
9103f4f4 793/* CPU feature flags of instruction set architecture used. */
fbf3f584 794i386_cpu_flags cpu_arch_isa_flags;
9103f4f4 795
fddf5b5b
AM
796/* If set, conditional jumps are not automatically promoted to handle
797 larger than a byte offset. */
798static unsigned int no_cond_jump_promotion = 0;
799
c0f3af97
L
800/* Encode SSE instructions with VEX prefix. */
801static unsigned int sse2avx;
802
539f890d
L
803/* Encode scalar AVX instructions with specific vector length. */
804static enum
805 {
806 vex128 = 0,
807 vex256
808 } avxscalar;
809
03751133
L
810/* Encode VEX WIG instructions with specific vex.w. */
811static enum
812 {
813 vexw0 = 0,
814 vexw1
815 } vexwig;
816
43234a1e
L
817/* Encode scalar EVEX LIG instructions with specific vector length. */
818static enum
819 {
820 evexl128 = 0,
821 evexl256,
822 evexl512
823 } evexlig;
824
825/* Encode EVEX WIG instructions with specific evex.w. */
826static enum
827 {
828 evexw0 = 0,
829 evexw1
830 } evexwig;
831
d3d3c6db
IT
832/* Value to encode in EVEX RC bits, for SAE-only instructions. */
833static enum rc_type evexrcig = rne;
834
29b0f896 835/* Pre-defined "_GLOBAL_OFFSET_TABLE_". */
87c245cc 836static symbolS *GOT_symbol;
29b0f896 837
a4447b93
RH
838/* The dwarf2 return column, adjusted for 32 or 64 bit. */
839unsigned int x86_dwarf2_return_column;
840
841/* The dwarf2 data alignment, adjusted for 32 or 64 bit. */
842int x86_cie_data_alignment;
843
252b5132 844/* Interface to relax_segment.
fddf5b5b
AM
845 There are 3 major relax states for 386 jump insns because the
846 different types of jumps add different sizes to frags when we're
e379e5f3
L
847 figuring out what sort of jump to choose to reach a given label.
848
849 BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING are used to align
850 branches which are handled by md_estimate_size_before_relax() and
851 i386_generic_table_relax_frag(). */
252b5132 852
47926f60 853/* Types. */
93c2a809
AM
854#define UNCOND_JUMP 0
855#define COND_JUMP 1
856#define COND_JUMP86 2
e379e5f3
L
857#define BRANCH_PADDING 3
858#define BRANCH_PREFIX 4
859#define FUSED_JCC_PADDING 5
fddf5b5b 860
47926f60 861/* Sizes. */
252b5132
RH
862#define CODE16 1
863#define SMALL 0
29b0f896 864#define SMALL16 (SMALL | CODE16)
252b5132 865#define BIG 2
29b0f896 866#define BIG16 (BIG | CODE16)
252b5132
RH
867
868#ifndef INLINE
869#ifdef __GNUC__
870#define INLINE __inline__
871#else
872#define INLINE
873#endif
874#endif
875
fddf5b5b
AM
876#define ENCODE_RELAX_STATE(type, size) \
877 ((relax_substateT) (((type) << 2) | (size)))
878#define TYPE_FROM_RELAX_STATE(s) \
879 ((s) >> 2)
880#define DISP_SIZE_FROM_RELAX_STATE(s) \
881 ((((s) & 3) == BIG ? 4 : (((s) & 3) == BIG16 ? 2 : 1)))
252b5132
RH
882
883/* This table is used by relax_frag to promote short jumps to long
884 ones where necessary. SMALL (short) jumps may be promoted to BIG
885 (32 bit long) ones, and SMALL16 jumps to BIG16 (16 bit long). We
886 don't allow a short jump in a 32 bit code segment to be promoted to
887 a 16 bit offset jump because it's slower (requires data size
888 prefix), and doesn't work, unless the destination is in the bottom
889 64k of the code segment (The top 16 bits of eip are zeroed). */
890
891const relax_typeS md_relax_table[] =
892{
24eab124
AM
893 /* The fields are:
894 1) most positive reach of this state,
895 2) most negative reach of this state,
93c2a809 896 3) how many bytes this mode will have in the variable part of the frag
ce8a8b2f 897 4) which index into the table to try if we can't fit into this one. */
252b5132 898
fddf5b5b 899 /* UNCOND_JUMP states. */
93c2a809
AM
900 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG)},
901 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16)},
902 /* dword jmp adds 4 bytes to frag:
903 0 extra opcode bytes, 4 displacement bytes. */
252b5132 904 {0, 0, 4, 0},
93c2a809
AM
905 /* word jmp adds 2 byte2 to frag:
906 0 extra opcode bytes, 2 displacement bytes. */
252b5132
RH
907 {0, 0, 2, 0},
908
93c2a809
AM
909 /* COND_JUMP states. */
910 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG)},
911 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG16)},
912 /* dword conditionals adds 5 bytes to frag:
913 1 extra opcode byte, 4 displacement bytes. */
914 {0, 0, 5, 0},
fddf5b5b 915 /* word conditionals add 3 bytes to frag:
93c2a809
AM
916 1 extra opcode byte, 2 displacement bytes. */
917 {0, 0, 3, 0},
918
919 /* COND_JUMP86 states. */
920 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG)},
921 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG16)},
922 /* dword conditionals adds 5 bytes to frag:
923 1 extra opcode byte, 4 displacement bytes. */
924 {0, 0, 5, 0},
925 /* word conditionals add 4 bytes to frag:
926 1 displacement byte and a 3 byte long branch insn. */
927 {0, 0, 4, 0}
252b5132
RH
928};
929
9103f4f4
L
930static const arch_entry cpu_arch[] =
931{
89507696
JB
932 /* Do not replace the first two entries - i386_target_format()
933 relies on them being there in this order. */
8a2c8fef 934 { STRING_COMMA_LEN ("generic32"), PROCESSOR_GENERIC32,
293f5f65 935 CPU_GENERIC32_FLAGS, 0 },
8a2c8fef 936 { STRING_COMMA_LEN ("generic64"), PROCESSOR_GENERIC64,
293f5f65 937 CPU_GENERIC64_FLAGS, 0 },
8a2c8fef 938 { STRING_COMMA_LEN ("i8086"), PROCESSOR_UNKNOWN,
293f5f65 939 CPU_NONE_FLAGS, 0 },
8a2c8fef 940 { STRING_COMMA_LEN ("i186"), PROCESSOR_UNKNOWN,
293f5f65 941 CPU_I186_FLAGS, 0 },
8a2c8fef 942 { STRING_COMMA_LEN ("i286"), PROCESSOR_UNKNOWN,
293f5f65 943 CPU_I286_FLAGS, 0 },
8a2c8fef 944 { STRING_COMMA_LEN ("i386"), PROCESSOR_I386,
293f5f65 945 CPU_I386_FLAGS, 0 },
8a2c8fef 946 { STRING_COMMA_LEN ("i486"), PROCESSOR_I486,
293f5f65 947 CPU_I486_FLAGS, 0 },
8a2c8fef 948 { STRING_COMMA_LEN ("i586"), PROCESSOR_PENTIUM,
293f5f65 949 CPU_I586_FLAGS, 0 },
8a2c8fef 950 { STRING_COMMA_LEN ("i686"), PROCESSOR_PENTIUMPRO,
293f5f65 951 CPU_I686_FLAGS, 0 },
8a2c8fef 952 { STRING_COMMA_LEN ("pentium"), PROCESSOR_PENTIUM,
293f5f65 953 CPU_I586_FLAGS, 0 },
8a2c8fef 954 { STRING_COMMA_LEN ("pentiumpro"), PROCESSOR_PENTIUMPRO,
293f5f65 955 CPU_PENTIUMPRO_FLAGS, 0 },
8a2c8fef 956 { STRING_COMMA_LEN ("pentiumii"), PROCESSOR_PENTIUMPRO,
293f5f65 957 CPU_P2_FLAGS, 0 },
8a2c8fef 958 { STRING_COMMA_LEN ("pentiumiii"),PROCESSOR_PENTIUMPRO,
293f5f65 959 CPU_P3_FLAGS, 0 },
8a2c8fef 960 { STRING_COMMA_LEN ("pentium4"), PROCESSOR_PENTIUM4,
293f5f65 961 CPU_P4_FLAGS, 0 },
8a2c8fef 962 { STRING_COMMA_LEN ("prescott"), PROCESSOR_NOCONA,
293f5f65 963 CPU_CORE_FLAGS, 0 },
8a2c8fef 964 { STRING_COMMA_LEN ("nocona"), PROCESSOR_NOCONA,
293f5f65 965 CPU_NOCONA_FLAGS, 0 },
8a2c8fef 966 { STRING_COMMA_LEN ("yonah"), PROCESSOR_CORE,
293f5f65 967 CPU_CORE_FLAGS, 1 },
8a2c8fef 968 { STRING_COMMA_LEN ("core"), PROCESSOR_CORE,
293f5f65 969 CPU_CORE_FLAGS, 0 },
8a2c8fef 970 { STRING_COMMA_LEN ("merom"), PROCESSOR_CORE2,
293f5f65 971 CPU_CORE2_FLAGS, 1 },
8a2c8fef 972 { STRING_COMMA_LEN ("core2"), PROCESSOR_CORE2,
293f5f65 973 CPU_CORE2_FLAGS, 0 },
8a2c8fef 974 { STRING_COMMA_LEN ("corei7"), PROCESSOR_COREI7,
293f5f65 975 CPU_COREI7_FLAGS, 0 },
8a2c8fef 976 { STRING_COMMA_LEN ("l1om"), PROCESSOR_L1OM,
293f5f65 977 CPU_L1OM_FLAGS, 0 },
7a9068fe 978 { STRING_COMMA_LEN ("k1om"), PROCESSOR_K1OM,
293f5f65 979 CPU_K1OM_FLAGS, 0 },
81486035 980 { STRING_COMMA_LEN ("iamcu"), PROCESSOR_IAMCU,
293f5f65 981 CPU_IAMCU_FLAGS, 0 },
8a2c8fef 982 { STRING_COMMA_LEN ("k6"), PROCESSOR_K6,
293f5f65 983 CPU_K6_FLAGS, 0 },
8a2c8fef 984 { STRING_COMMA_LEN ("k6_2"), PROCESSOR_K6,
293f5f65 985 CPU_K6_2_FLAGS, 0 },
8a2c8fef 986 { STRING_COMMA_LEN ("athlon"), PROCESSOR_ATHLON,
293f5f65 987 CPU_ATHLON_FLAGS, 0 },
8a2c8fef 988 { STRING_COMMA_LEN ("sledgehammer"), PROCESSOR_K8,
293f5f65 989 CPU_K8_FLAGS, 1 },
8a2c8fef 990 { STRING_COMMA_LEN ("opteron"), PROCESSOR_K8,
293f5f65 991 CPU_K8_FLAGS, 0 },
8a2c8fef 992 { STRING_COMMA_LEN ("k8"), PROCESSOR_K8,
293f5f65 993 CPU_K8_FLAGS, 0 },
8a2c8fef 994 { STRING_COMMA_LEN ("amdfam10"), PROCESSOR_AMDFAM10,
293f5f65 995 CPU_AMDFAM10_FLAGS, 0 },
8aedb9fe 996 { STRING_COMMA_LEN ("bdver1"), PROCESSOR_BD,
293f5f65 997 CPU_BDVER1_FLAGS, 0 },
8aedb9fe 998 { STRING_COMMA_LEN ("bdver2"), PROCESSOR_BD,
293f5f65 999 CPU_BDVER2_FLAGS, 0 },
5e5c50d3 1000 { STRING_COMMA_LEN ("bdver3"), PROCESSOR_BD,
293f5f65 1001 CPU_BDVER3_FLAGS, 0 },
c7b0bd56 1002 { STRING_COMMA_LEN ("bdver4"), PROCESSOR_BD,
293f5f65 1003 CPU_BDVER4_FLAGS, 0 },
029f3522 1004 { STRING_COMMA_LEN ("znver1"), PROCESSOR_ZNVER,
293f5f65 1005 CPU_ZNVER1_FLAGS, 0 },
a9660a6f
AP
1006 { STRING_COMMA_LEN ("znver2"), PROCESSOR_ZNVER,
1007 CPU_ZNVER2_FLAGS, 0 },
646cc3e0
GG
1008 { STRING_COMMA_LEN ("znver3"), PROCESSOR_ZNVER,
1009 CPU_ZNVER3_FLAGS, 0 },
7b458c12 1010 { STRING_COMMA_LEN ("btver1"), PROCESSOR_BT,
293f5f65 1011 CPU_BTVER1_FLAGS, 0 },
7b458c12 1012 { STRING_COMMA_LEN ("btver2"), PROCESSOR_BT,
293f5f65 1013 CPU_BTVER2_FLAGS, 0 },
8a2c8fef 1014 { STRING_COMMA_LEN (".8087"), PROCESSOR_UNKNOWN,
293f5f65 1015 CPU_8087_FLAGS, 0 },
8a2c8fef 1016 { STRING_COMMA_LEN (".287"), PROCESSOR_UNKNOWN,
293f5f65 1017 CPU_287_FLAGS, 0 },
8a2c8fef 1018 { STRING_COMMA_LEN (".387"), PROCESSOR_UNKNOWN,
293f5f65 1019 CPU_387_FLAGS, 0 },
1848e567
L
1020 { STRING_COMMA_LEN (".687"), PROCESSOR_UNKNOWN,
1021 CPU_687_FLAGS, 0 },
d871f3f4
L
1022 { STRING_COMMA_LEN (".cmov"), PROCESSOR_UNKNOWN,
1023 CPU_CMOV_FLAGS, 0 },
1024 { STRING_COMMA_LEN (".fxsr"), PROCESSOR_UNKNOWN,
1025 CPU_FXSR_FLAGS, 0 },
8a2c8fef 1026 { STRING_COMMA_LEN (".mmx"), PROCESSOR_UNKNOWN,
293f5f65 1027 CPU_MMX_FLAGS, 0 },
8a2c8fef 1028 { STRING_COMMA_LEN (".sse"), PROCESSOR_UNKNOWN,
293f5f65 1029 CPU_SSE_FLAGS, 0 },
8a2c8fef 1030 { STRING_COMMA_LEN (".sse2"), PROCESSOR_UNKNOWN,
293f5f65 1031 CPU_SSE2_FLAGS, 0 },
8a2c8fef 1032 { STRING_COMMA_LEN (".sse3"), PROCESSOR_UNKNOWN,
293f5f65 1033 CPU_SSE3_FLAGS, 0 },
af5c13b0
L
1034 { STRING_COMMA_LEN (".sse4a"), PROCESSOR_UNKNOWN,
1035 CPU_SSE4A_FLAGS, 0 },
8a2c8fef 1036 { STRING_COMMA_LEN (".ssse3"), PROCESSOR_UNKNOWN,
293f5f65 1037 CPU_SSSE3_FLAGS, 0 },
8a2c8fef 1038 { STRING_COMMA_LEN (".sse4.1"), PROCESSOR_UNKNOWN,
293f5f65 1039 CPU_SSE4_1_FLAGS, 0 },
8a2c8fef 1040 { STRING_COMMA_LEN (".sse4.2"), PROCESSOR_UNKNOWN,
293f5f65 1041 CPU_SSE4_2_FLAGS, 0 },
8a2c8fef 1042 { STRING_COMMA_LEN (".sse4"), PROCESSOR_UNKNOWN,
293f5f65 1043 CPU_SSE4_2_FLAGS, 0 },
8a2c8fef 1044 { STRING_COMMA_LEN (".avx"), PROCESSOR_UNKNOWN,
293f5f65 1045 CPU_AVX_FLAGS, 0 },
6c30d220 1046 { STRING_COMMA_LEN (".avx2"), PROCESSOR_UNKNOWN,
293f5f65 1047 CPU_AVX2_FLAGS, 0 },
43234a1e 1048 { STRING_COMMA_LEN (".avx512f"), PROCESSOR_UNKNOWN,
293f5f65 1049 CPU_AVX512F_FLAGS, 0 },
43234a1e 1050 { STRING_COMMA_LEN (".avx512cd"), PROCESSOR_UNKNOWN,
293f5f65 1051 CPU_AVX512CD_FLAGS, 0 },
43234a1e 1052 { STRING_COMMA_LEN (".avx512er"), PROCESSOR_UNKNOWN,
293f5f65 1053 CPU_AVX512ER_FLAGS, 0 },
43234a1e 1054 { STRING_COMMA_LEN (".avx512pf"), PROCESSOR_UNKNOWN,
293f5f65 1055 CPU_AVX512PF_FLAGS, 0 },
1dfc6506 1056 { STRING_COMMA_LEN (".avx512dq"), PROCESSOR_UNKNOWN,
293f5f65 1057 CPU_AVX512DQ_FLAGS, 0 },
1dfc6506 1058 { STRING_COMMA_LEN (".avx512bw"), PROCESSOR_UNKNOWN,
293f5f65 1059 CPU_AVX512BW_FLAGS, 0 },
1dfc6506 1060 { STRING_COMMA_LEN (".avx512vl"), PROCESSOR_UNKNOWN,
293f5f65 1061 CPU_AVX512VL_FLAGS, 0 },
8a2c8fef 1062 { STRING_COMMA_LEN (".vmx"), PROCESSOR_UNKNOWN,
293f5f65 1063 CPU_VMX_FLAGS, 0 },
8729a6f6 1064 { STRING_COMMA_LEN (".vmfunc"), PROCESSOR_UNKNOWN,
293f5f65 1065 CPU_VMFUNC_FLAGS, 0 },
8a2c8fef 1066 { STRING_COMMA_LEN (".smx"), PROCESSOR_UNKNOWN,
293f5f65 1067 CPU_SMX_FLAGS, 0 },
8a2c8fef 1068 { STRING_COMMA_LEN (".xsave"), PROCESSOR_UNKNOWN,
293f5f65 1069 CPU_XSAVE_FLAGS, 0 },
c7b8aa3a 1070 { STRING_COMMA_LEN (".xsaveopt"), PROCESSOR_UNKNOWN,
293f5f65 1071 CPU_XSAVEOPT_FLAGS, 0 },
1dfc6506 1072 { STRING_COMMA_LEN (".xsavec"), PROCESSOR_UNKNOWN,
293f5f65 1073 CPU_XSAVEC_FLAGS, 0 },
1dfc6506 1074 { STRING_COMMA_LEN (".xsaves"), PROCESSOR_UNKNOWN,
293f5f65 1075 CPU_XSAVES_FLAGS, 0 },
8a2c8fef 1076 { STRING_COMMA_LEN (".aes"), PROCESSOR_UNKNOWN,
293f5f65 1077 CPU_AES_FLAGS, 0 },
8a2c8fef 1078 { STRING_COMMA_LEN (".pclmul"), PROCESSOR_UNKNOWN,
293f5f65 1079 CPU_PCLMUL_FLAGS, 0 },
8a2c8fef 1080 { STRING_COMMA_LEN (".clmul"), PROCESSOR_UNKNOWN,
293f5f65 1081 CPU_PCLMUL_FLAGS, 1 },
c7b8aa3a 1082 { STRING_COMMA_LEN (".fsgsbase"), PROCESSOR_UNKNOWN,
293f5f65 1083 CPU_FSGSBASE_FLAGS, 0 },
c7b8aa3a 1084 { STRING_COMMA_LEN (".rdrnd"), PROCESSOR_UNKNOWN,
293f5f65 1085 CPU_RDRND_FLAGS, 0 },
c7b8aa3a 1086 { STRING_COMMA_LEN (".f16c"), PROCESSOR_UNKNOWN,
293f5f65 1087 CPU_F16C_FLAGS, 0 },
6c30d220 1088 { STRING_COMMA_LEN (".bmi2"), PROCESSOR_UNKNOWN,
293f5f65 1089 CPU_BMI2_FLAGS, 0 },
8a2c8fef 1090 { STRING_COMMA_LEN (".fma"), PROCESSOR_UNKNOWN,
293f5f65 1091 CPU_FMA_FLAGS, 0 },
8a2c8fef 1092 { STRING_COMMA_LEN (".fma4"), PROCESSOR_UNKNOWN,
293f5f65 1093 CPU_FMA4_FLAGS, 0 },
8a2c8fef 1094 { STRING_COMMA_LEN (".xop"), PROCESSOR_UNKNOWN,
293f5f65 1095 CPU_XOP_FLAGS, 0 },
8a2c8fef 1096 { STRING_COMMA_LEN (".lwp"), PROCESSOR_UNKNOWN,
293f5f65 1097 CPU_LWP_FLAGS, 0 },
8a2c8fef 1098 { STRING_COMMA_LEN (".movbe"), PROCESSOR_UNKNOWN,
293f5f65 1099 CPU_MOVBE_FLAGS, 0 },
60aa667e 1100 { STRING_COMMA_LEN (".cx16"), PROCESSOR_UNKNOWN,
293f5f65 1101 CPU_CX16_FLAGS, 0 },
8a2c8fef 1102 { STRING_COMMA_LEN (".ept"), PROCESSOR_UNKNOWN,
293f5f65 1103 CPU_EPT_FLAGS, 0 },
6c30d220 1104 { STRING_COMMA_LEN (".lzcnt"), PROCESSOR_UNKNOWN,
293f5f65 1105 CPU_LZCNT_FLAGS, 0 },
272a84b1
L
1106 { STRING_COMMA_LEN (".popcnt"), PROCESSOR_UNKNOWN,
1107 CPU_POPCNT_FLAGS, 0 },
42164a71 1108 { STRING_COMMA_LEN (".hle"), PROCESSOR_UNKNOWN,
293f5f65 1109 CPU_HLE_FLAGS, 0 },
42164a71 1110 { STRING_COMMA_LEN (".rtm"), PROCESSOR_UNKNOWN,
293f5f65 1111 CPU_RTM_FLAGS, 0 },
6c30d220 1112 { STRING_COMMA_LEN (".invpcid"), PROCESSOR_UNKNOWN,
293f5f65 1113 CPU_INVPCID_FLAGS, 0 },
8a2c8fef 1114 { STRING_COMMA_LEN (".clflush"), PROCESSOR_UNKNOWN,
293f5f65 1115 CPU_CLFLUSH_FLAGS, 0 },
22109423 1116 { STRING_COMMA_LEN (".nop"), PROCESSOR_UNKNOWN,
293f5f65 1117 CPU_NOP_FLAGS, 0 },
8a2c8fef 1118 { STRING_COMMA_LEN (".syscall"), PROCESSOR_UNKNOWN,
293f5f65 1119 CPU_SYSCALL_FLAGS, 0 },
8a2c8fef 1120 { STRING_COMMA_LEN (".rdtscp"), PROCESSOR_UNKNOWN,
293f5f65 1121 CPU_RDTSCP_FLAGS, 0 },
8a2c8fef 1122 { STRING_COMMA_LEN (".3dnow"), PROCESSOR_UNKNOWN,
293f5f65 1123 CPU_3DNOW_FLAGS, 0 },
8a2c8fef 1124 { STRING_COMMA_LEN (".3dnowa"), PROCESSOR_UNKNOWN,
293f5f65 1125 CPU_3DNOWA_FLAGS, 0 },
8a2c8fef 1126 { STRING_COMMA_LEN (".padlock"), PROCESSOR_UNKNOWN,
293f5f65 1127 CPU_PADLOCK_FLAGS, 0 },
8a2c8fef 1128 { STRING_COMMA_LEN (".pacifica"), PROCESSOR_UNKNOWN,
293f5f65 1129 CPU_SVME_FLAGS, 1 },
8a2c8fef 1130 { STRING_COMMA_LEN (".svme"), PROCESSOR_UNKNOWN,
293f5f65 1131 CPU_SVME_FLAGS, 0 },
8a2c8fef 1132 { STRING_COMMA_LEN (".sse4a"), PROCESSOR_UNKNOWN,
293f5f65 1133 CPU_SSE4A_FLAGS, 0 },
8a2c8fef 1134 { STRING_COMMA_LEN (".abm"), PROCESSOR_UNKNOWN,
293f5f65 1135 CPU_ABM_FLAGS, 0 },
87973e9f 1136 { STRING_COMMA_LEN (".bmi"), PROCESSOR_UNKNOWN,
293f5f65 1137 CPU_BMI_FLAGS, 0 },
2a2a0f38 1138 { STRING_COMMA_LEN (".tbm"), PROCESSOR_UNKNOWN,
293f5f65 1139 CPU_TBM_FLAGS, 0 },
e2e1fcde 1140 { STRING_COMMA_LEN (".adx"), PROCESSOR_UNKNOWN,
293f5f65 1141 CPU_ADX_FLAGS, 0 },
e2e1fcde 1142 { STRING_COMMA_LEN (".rdseed"), PROCESSOR_UNKNOWN,
293f5f65 1143 CPU_RDSEED_FLAGS, 0 },
e2e1fcde 1144 { STRING_COMMA_LEN (".prfchw"), PROCESSOR_UNKNOWN,
293f5f65 1145 CPU_PRFCHW_FLAGS, 0 },
5c111e37 1146 { STRING_COMMA_LEN (".smap"), PROCESSOR_UNKNOWN,
293f5f65 1147 CPU_SMAP_FLAGS, 0 },
7e8b059b 1148 { STRING_COMMA_LEN (".mpx"), PROCESSOR_UNKNOWN,
293f5f65 1149 CPU_MPX_FLAGS, 0 },
a0046408 1150 { STRING_COMMA_LEN (".sha"), PROCESSOR_UNKNOWN,
293f5f65 1151 CPU_SHA_FLAGS, 0 },
963f3586 1152 { STRING_COMMA_LEN (".clflushopt"), PROCESSOR_UNKNOWN,
293f5f65 1153 CPU_CLFLUSHOPT_FLAGS, 0 },
dcf893b5 1154 { STRING_COMMA_LEN (".prefetchwt1"), PROCESSOR_UNKNOWN,
293f5f65 1155 CPU_PREFETCHWT1_FLAGS, 0 },
2cf200a4 1156 { STRING_COMMA_LEN (".se1"), PROCESSOR_UNKNOWN,
293f5f65 1157 CPU_SE1_FLAGS, 0 },
c5e7287a 1158 { STRING_COMMA_LEN (".clwb"), PROCESSOR_UNKNOWN,
293f5f65 1159 CPU_CLWB_FLAGS, 0 },
2cc1b5aa 1160 { STRING_COMMA_LEN (".avx512ifma"), PROCESSOR_UNKNOWN,
293f5f65 1161 CPU_AVX512IFMA_FLAGS, 0 },
14f195c9 1162 { STRING_COMMA_LEN (".avx512vbmi"), PROCESSOR_UNKNOWN,
293f5f65 1163 CPU_AVX512VBMI_FLAGS, 0 },
920d2ddc
IT
1164 { STRING_COMMA_LEN (".avx512_4fmaps"), PROCESSOR_UNKNOWN,
1165 CPU_AVX512_4FMAPS_FLAGS, 0 },
47acf0bd
IT
1166 { STRING_COMMA_LEN (".avx512_4vnniw"), PROCESSOR_UNKNOWN,
1167 CPU_AVX512_4VNNIW_FLAGS, 0 },
620214f7
IT
1168 { STRING_COMMA_LEN (".avx512_vpopcntdq"), PROCESSOR_UNKNOWN,
1169 CPU_AVX512_VPOPCNTDQ_FLAGS, 0 },
53467f57
IT
1170 { STRING_COMMA_LEN (".avx512_vbmi2"), PROCESSOR_UNKNOWN,
1171 CPU_AVX512_VBMI2_FLAGS, 0 },
8cfcb765
IT
1172 { STRING_COMMA_LEN (".avx512_vnni"), PROCESSOR_UNKNOWN,
1173 CPU_AVX512_VNNI_FLAGS, 0 },
ee6872be
IT
1174 { STRING_COMMA_LEN (".avx512_bitalg"), PROCESSOR_UNKNOWN,
1175 CPU_AVX512_BITALG_FLAGS, 0 },
58bf9b6a
L
1176 { STRING_COMMA_LEN (".avx_vnni"), PROCESSOR_UNKNOWN,
1177 CPU_AVX_VNNI_FLAGS, 0 },
029f3522 1178 { STRING_COMMA_LEN (".clzero"), PROCESSOR_UNKNOWN,
293f5f65 1179 CPU_CLZERO_FLAGS, 0 },
9916071f 1180 { STRING_COMMA_LEN (".mwaitx"), PROCESSOR_UNKNOWN,
293f5f65 1181 CPU_MWAITX_FLAGS, 0 },
8eab4136 1182 { STRING_COMMA_LEN (".ospke"), PROCESSOR_UNKNOWN,
293f5f65 1183 CPU_OSPKE_FLAGS, 0 },
8bc52696 1184 { STRING_COMMA_LEN (".rdpid"), PROCESSOR_UNKNOWN,
293f5f65 1185 CPU_RDPID_FLAGS, 0 },
6b40c462
L
1186 { STRING_COMMA_LEN (".ptwrite"), PROCESSOR_UNKNOWN,
1187 CPU_PTWRITE_FLAGS, 0 },
d777820b
IT
1188 { STRING_COMMA_LEN (".ibt"), PROCESSOR_UNKNOWN,
1189 CPU_IBT_FLAGS, 0 },
1190 { STRING_COMMA_LEN (".shstk"), PROCESSOR_UNKNOWN,
1191 CPU_SHSTK_FLAGS, 0 },
48521003
IT
1192 { STRING_COMMA_LEN (".gfni"), PROCESSOR_UNKNOWN,
1193 CPU_GFNI_FLAGS, 0 },
8dcf1fad
IT
1194 { STRING_COMMA_LEN (".vaes"), PROCESSOR_UNKNOWN,
1195 CPU_VAES_FLAGS, 0 },
ff1982d5
IT
1196 { STRING_COMMA_LEN (".vpclmulqdq"), PROCESSOR_UNKNOWN,
1197 CPU_VPCLMULQDQ_FLAGS, 0 },
3233d7d0
IT
1198 { STRING_COMMA_LEN (".wbnoinvd"), PROCESSOR_UNKNOWN,
1199 CPU_WBNOINVD_FLAGS, 0 },
be3a8dca
IT
1200 { STRING_COMMA_LEN (".pconfig"), PROCESSOR_UNKNOWN,
1201 CPU_PCONFIG_FLAGS, 0 },
de89d0a3
IT
1202 { STRING_COMMA_LEN (".waitpkg"), PROCESSOR_UNKNOWN,
1203 CPU_WAITPKG_FLAGS, 0 },
c48935d7
IT
1204 { STRING_COMMA_LEN (".cldemote"), PROCESSOR_UNKNOWN,
1205 CPU_CLDEMOTE_FLAGS, 0 },
260cd341
LC
1206 { STRING_COMMA_LEN (".amx_int8"), PROCESSOR_UNKNOWN,
1207 CPU_AMX_INT8_FLAGS, 0 },
1208 { STRING_COMMA_LEN (".amx_bf16"), PROCESSOR_UNKNOWN,
1209 CPU_AMX_BF16_FLAGS, 0 },
1210 { STRING_COMMA_LEN (".amx_tile"), PROCESSOR_UNKNOWN,
1211 CPU_AMX_TILE_FLAGS, 0 },
c0a30a9f
L
1212 { STRING_COMMA_LEN (".movdiri"), PROCESSOR_UNKNOWN,
1213 CPU_MOVDIRI_FLAGS, 0 },
1214 { STRING_COMMA_LEN (".movdir64b"), PROCESSOR_UNKNOWN,
1215 CPU_MOVDIR64B_FLAGS, 0 },
d6aab7a1
XG
1216 { STRING_COMMA_LEN (".avx512_bf16"), PROCESSOR_UNKNOWN,
1217 CPU_AVX512_BF16_FLAGS, 0 },
9186c494
L
1218 { STRING_COMMA_LEN (".avx512_vp2intersect"), PROCESSOR_UNKNOWN,
1219 CPU_AVX512_VP2INTERSECT_FLAGS, 0 },
81d54bb7
CL
1220 { STRING_COMMA_LEN (".tdx"), PROCESSOR_UNKNOWN,
1221 CPU_TDX_FLAGS, 0 },
dd455cf5
L
1222 { STRING_COMMA_LEN (".enqcmd"), PROCESSOR_UNKNOWN,
1223 CPU_ENQCMD_FLAGS, 0 },
4b27d27c
L
1224 { STRING_COMMA_LEN (".serialize"), PROCESSOR_UNKNOWN,
1225 CPU_SERIALIZE_FLAGS, 0 },
142861df
JB
1226 { STRING_COMMA_LEN (".rdpru"), PROCESSOR_UNKNOWN,
1227 CPU_RDPRU_FLAGS, 0 },
1228 { STRING_COMMA_LEN (".mcommit"), PROCESSOR_UNKNOWN,
1229 CPU_MCOMMIT_FLAGS, 0 },
a847e322
JB
1230 { STRING_COMMA_LEN (".sev_es"), PROCESSOR_UNKNOWN,
1231 CPU_SEV_ES_FLAGS, 0 },
bb651e8b
CL
1232 { STRING_COMMA_LEN (".tsxldtrk"), PROCESSOR_UNKNOWN,
1233 CPU_TSXLDTRK_FLAGS, 0 },
c4694f17
TG
1234 { STRING_COMMA_LEN (".kl"), PROCESSOR_UNKNOWN,
1235 CPU_KL_FLAGS, 0 },
1236 { STRING_COMMA_LEN (".widekl"), PROCESSOR_UNKNOWN,
1237 CPU_WIDEKL_FLAGS, 0 },
f64c42a9
LC
1238 { STRING_COMMA_LEN (".uintr"), PROCESSOR_UNKNOWN,
1239 CPU_UINTR_FLAGS, 0 },
c1fa250a
LC
1240 { STRING_COMMA_LEN (".hreset"), PROCESSOR_UNKNOWN,
1241 CPU_HRESET_FLAGS, 0 },
293f5f65
L
1242};
1243
1244static const noarch_entry cpu_noarch[] =
1245{
1246 { STRING_COMMA_LEN ("no87"), CPU_ANY_X87_FLAGS },
1848e567
L
1247 { STRING_COMMA_LEN ("no287"), CPU_ANY_287_FLAGS },
1248 { STRING_COMMA_LEN ("no387"), CPU_ANY_387_FLAGS },
1249 { STRING_COMMA_LEN ("no687"), CPU_ANY_687_FLAGS },
d871f3f4
L
1250 { STRING_COMMA_LEN ("nocmov"), CPU_ANY_CMOV_FLAGS },
1251 { STRING_COMMA_LEN ("nofxsr"), CPU_ANY_FXSR_FLAGS },
293f5f65
L
1252 { STRING_COMMA_LEN ("nommx"), CPU_ANY_MMX_FLAGS },
1253 { STRING_COMMA_LEN ("nosse"), CPU_ANY_SSE_FLAGS },
1848e567
L
1254 { STRING_COMMA_LEN ("nosse2"), CPU_ANY_SSE2_FLAGS },
1255 { STRING_COMMA_LEN ("nosse3"), CPU_ANY_SSE3_FLAGS },
af5c13b0 1256 { STRING_COMMA_LEN ("nosse4a"), CPU_ANY_SSE4A_FLAGS },
1848e567
L
1257 { STRING_COMMA_LEN ("nossse3"), CPU_ANY_SSSE3_FLAGS },
1258 { STRING_COMMA_LEN ("nosse4.1"), CPU_ANY_SSE4_1_FLAGS },
1259 { STRING_COMMA_LEN ("nosse4.2"), CPU_ANY_SSE4_2_FLAGS },
af5c13b0 1260 { STRING_COMMA_LEN ("nosse4"), CPU_ANY_SSE4_1_FLAGS },
293f5f65 1261 { STRING_COMMA_LEN ("noavx"), CPU_ANY_AVX_FLAGS },
1848e567 1262 { STRING_COMMA_LEN ("noavx2"), CPU_ANY_AVX2_FLAGS },
144b71e2
L
1263 { STRING_COMMA_LEN ("noavx512f"), CPU_ANY_AVX512F_FLAGS },
1264 { STRING_COMMA_LEN ("noavx512cd"), CPU_ANY_AVX512CD_FLAGS },
1265 { STRING_COMMA_LEN ("noavx512er"), CPU_ANY_AVX512ER_FLAGS },
1266 { STRING_COMMA_LEN ("noavx512pf"), CPU_ANY_AVX512PF_FLAGS },
1267 { STRING_COMMA_LEN ("noavx512dq"), CPU_ANY_AVX512DQ_FLAGS },
1268 { STRING_COMMA_LEN ("noavx512bw"), CPU_ANY_AVX512BW_FLAGS },
1269 { STRING_COMMA_LEN ("noavx512vl"), CPU_ANY_AVX512VL_FLAGS },
1270 { STRING_COMMA_LEN ("noavx512ifma"), CPU_ANY_AVX512IFMA_FLAGS },
1271 { STRING_COMMA_LEN ("noavx512vbmi"), CPU_ANY_AVX512VBMI_FLAGS },
920d2ddc 1272 { STRING_COMMA_LEN ("noavx512_4fmaps"), CPU_ANY_AVX512_4FMAPS_FLAGS },
47acf0bd 1273 { STRING_COMMA_LEN ("noavx512_4vnniw"), CPU_ANY_AVX512_4VNNIW_FLAGS },
620214f7 1274 { STRING_COMMA_LEN ("noavx512_vpopcntdq"), CPU_ANY_AVX512_VPOPCNTDQ_FLAGS },
53467f57 1275 { STRING_COMMA_LEN ("noavx512_vbmi2"), CPU_ANY_AVX512_VBMI2_FLAGS },
8cfcb765 1276 { STRING_COMMA_LEN ("noavx512_vnni"), CPU_ANY_AVX512_VNNI_FLAGS },
ee6872be 1277 { STRING_COMMA_LEN ("noavx512_bitalg"), CPU_ANY_AVX512_BITALG_FLAGS },
58bf9b6a 1278 { STRING_COMMA_LEN ("noavx_vnni"), CPU_ANY_AVX_VNNI_FLAGS },
d777820b
IT
1279 { STRING_COMMA_LEN ("noibt"), CPU_ANY_IBT_FLAGS },
1280 { STRING_COMMA_LEN ("noshstk"), CPU_ANY_SHSTK_FLAGS },
260cd341
LC
1281 { STRING_COMMA_LEN ("noamx_int8"), CPU_ANY_AMX_INT8_FLAGS },
1282 { STRING_COMMA_LEN ("noamx_bf16"), CPU_ANY_AMX_BF16_FLAGS },
1283 { STRING_COMMA_LEN ("noamx_tile"), CPU_ANY_AMX_TILE_FLAGS },
c0a30a9f
L
1284 { STRING_COMMA_LEN ("nomovdiri"), CPU_ANY_MOVDIRI_FLAGS },
1285 { STRING_COMMA_LEN ("nomovdir64b"), CPU_ANY_MOVDIR64B_FLAGS },
d6aab7a1 1286 { STRING_COMMA_LEN ("noavx512_bf16"), CPU_ANY_AVX512_BF16_FLAGS },
708a2fff
CL
1287 { STRING_COMMA_LEN ("noavx512_vp2intersect"),
1288 CPU_ANY_AVX512_VP2INTERSECT_FLAGS },
81d54bb7 1289 { STRING_COMMA_LEN ("notdx"), CPU_ANY_TDX_FLAGS },
dd455cf5 1290 { STRING_COMMA_LEN ("noenqcmd"), CPU_ANY_ENQCMD_FLAGS },
4b27d27c 1291 { STRING_COMMA_LEN ("noserialize"), CPU_ANY_SERIALIZE_FLAGS },
bb651e8b 1292 { STRING_COMMA_LEN ("notsxldtrk"), CPU_ANY_TSXLDTRK_FLAGS },
c4694f17
TG
1293 { STRING_COMMA_LEN ("nokl"), CPU_ANY_KL_FLAGS },
1294 { STRING_COMMA_LEN ("nowidekl"), CPU_ANY_WIDEKL_FLAGS },
f64c42a9 1295 { STRING_COMMA_LEN ("nouintr"), CPU_ANY_UINTR_FLAGS },
c1fa250a 1296 { STRING_COMMA_LEN ("nohreset"), CPU_ANY_HRESET_FLAGS },
e413e4e9
AM
1297};
1298
704209c0 1299#ifdef I386COFF
a6c24e68
NC
1300/* Like s_lcomm_internal in gas/read.c but the alignment string
1301 is allowed to be optional. */
1302
1303static symbolS *
1304pe_lcomm_internal (int needs_align, symbolS *symbolP, addressT size)
1305{
1306 addressT align = 0;
1307
1308 SKIP_WHITESPACE ();
1309
7ab9ffdd 1310 if (needs_align
a6c24e68
NC
1311 && *input_line_pointer == ',')
1312 {
1313 align = parse_align (needs_align - 1);
7ab9ffdd 1314
a6c24e68
NC
1315 if (align == (addressT) -1)
1316 return NULL;
1317 }
1318 else
1319 {
1320 if (size >= 8)
1321 align = 3;
1322 else if (size >= 4)
1323 align = 2;
1324 else if (size >= 2)
1325 align = 1;
1326 else
1327 align = 0;
1328 }
1329
1330 bss_alloc (symbolP, size, align);
1331 return symbolP;
1332}
1333
704209c0 1334static void
a6c24e68
NC
1335pe_lcomm (int needs_align)
1336{
1337 s_comm_internal (needs_align * 2, pe_lcomm_internal);
1338}
704209c0 1339#endif
a6c24e68 1340
29b0f896
AM
1341const pseudo_typeS md_pseudo_table[] =
1342{
1343#if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
1344 {"align", s_align_bytes, 0},
1345#else
1346 {"align", s_align_ptwo, 0},
1347#endif
1348 {"arch", set_cpu_arch, 0},
1349#ifndef I386COFF
1350 {"bss", s_bss, 0},
a6c24e68
NC
1351#else
1352 {"lcomm", pe_lcomm, 1},
29b0f896
AM
1353#endif
1354 {"ffloat", float_cons, 'f'},
1355 {"dfloat", float_cons, 'd'},
1356 {"tfloat", float_cons, 'x'},
1357 {"value", cons, 2},
d182319b 1358 {"slong", signed_cons, 4},
29b0f896
AM
1359 {"noopt", s_ignore, 0},
1360 {"optim", s_ignore, 0},
1361 {"code16gcc", set_16bit_gcc_code_flag, CODE_16BIT},
1362 {"code16", set_code_flag, CODE_16BIT},
1363 {"code32", set_code_flag, CODE_32BIT},
da5f19a2 1364#ifdef BFD64
29b0f896 1365 {"code64", set_code_flag, CODE_64BIT},
da5f19a2 1366#endif
29b0f896
AM
1367 {"intel_syntax", set_intel_syntax, 1},
1368 {"att_syntax", set_intel_syntax, 0},
1efbbeb4
L
1369 {"intel_mnemonic", set_intel_mnemonic, 1},
1370 {"att_mnemonic", set_intel_mnemonic, 0},
db51cc60
L
1371 {"allow_index_reg", set_allow_index_reg, 1},
1372 {"disallow_index_reg", set_allow_index_reg, 0},
7bab8ab5
JB
1373 {"sse_check", set_check, 0},
1374 {"operand_check", set_check, 1},
3b22753a
L
1375#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1376 {"largecomm", handle_large_common, 0},
07a53e5c 1377#else
68d20676 1378 {"file", dwarf2_directive_file, 0},
07a53e5c
RH
1379 {"loc", dwarf2_directive_loc, 0},
1380 {"loc_mark_labels", dwarf2_directive_loc_mark_labels, 0},
3b22753a 1381#endif
6482c264
NC
1382#ifdef TE_PE
1383 {"secrel32", pe_directive_secrel, 0},
1384#endif
29b0f896
AM
1385 {0, 0, 0}
1386};
1387
1388/* For interface with expression (). */
1389extern char *input_line_pointer;
1390
1391/* Hash table for instruction mnemonic lookup. */
629310ab 1392static htab_t op_hash;
29b0f896
AM
1393
1394/* Hash table for register lookup. */
629310ab 1395static htab_t reg_hash;
29b0f896 1396\f
ce8a8b2f
AM
1397 /* Various efficient no-op patterns for aligning code labels.
1398 Note: Don't try to assemble the instructions in the comments.
1399 0L and 0w are not legal. */
62a02d25
L
1400static const unsigned char f32_1[] =
1401 {0x90}; /* nop */
1402static const unsigned char f32_2[] =
1403 {0x66,0x90}; /* xchg %ax,%ax */
1404static const unsigned char f32_3[] =
1405 {0x8d,0x76,0x00}; /* leal 0(%esi),%esi */
1406static const unsigned char f32_4[] =
1407 {0x8d,0x74,0x26,0x00}; /* leal 0(%esi,1),%esi */
62a02d25
L
1408static const unsigned char f32_6[] =
1409 {0x8d,0xb6,0x00,0x00,0x00,0x00}; /* leal 0L(%esi),%esi */
1410static const unsigned char f32_7[] =
1411 {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal 0L(%esi,1),%esi */
62a02d25 1412static const unsigned char f16_3[] =
3ae729d5 1413 {0x8d,0x74,0x00}; /* lea 0(%si),%si */
62a02d25 1414static const unsigned char f16_4[] =
3ae729d5
L
1415 {0x8d,0xb4,0x00,0x00}; /* lea 0W(%si),%si */
1416static const unsigned char jump_disp8[] =
1417 {0xeb}; /* jmp disp8 */
1418static const unsigned char jump32_disp32[] =
1419 {0xe9}; /* jmp disp32 */
1420static const unsigned char jump16_disp32[] =
1421 {0x66,0xe9}; /* jmp disp32 */
62a02d25
L
1422/* 32-bit NOPs patterns. */
1423static const unsigned char *const f32_patt[] = {
3ae729d5 1424 f32_1, f32_2, f32_3, f32_4, NULL, f32_6, f32_7
62a02d25
L
1425};
1426/* 16-bit NOPs patterns. */
1427static const unsigned char *const f16_patt[] = {
3ae729d5 1428 f32_1, f32_2, f16_3, f16_4
62a02d25
L
1429};
1430/* nopl (%[re]ax) */
1431static const unsigned char alt_3[] =
1432 {0x0f,0x1f,0x00};
1433/* nopl 0(%[re]ax) */
1434static const unsigned char alt_4[] =
1435 {0x0f,0x1f,0x40,0x00};
1436/* nopl 0(%[re]ax,%[re]ax,1) */
1437static const unsigned char alt_5[] =
1438 {0x0f,0x1f,0x44,0x00,0x00};
1439/* nopw 0(%[re]ax,%[re]ax,1) */
1440static const unsigned char alt_6[] =
1441 {0x66,0x0f,0x1f,0x44,0x00,0x00};
1442/* nopl 0L(%[re]ax) */
1443static const unsigned char alt_7[] =
1444 {0x0f,0x1f,0x80,0x00,0x00,0x00,0x00};
1445/* nopl 0L(%[re]ax,%[re]ax,1) */
1446static const unsigned char alt_8[] =
1447 {0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1448/* nopw 0L(%[re]ax,%[re]ax,1) */
1449static const unsigned char alt_9[] =
1450 {0x66,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1451/* nopw %cs:0L(%[re]ax,%[re]ax,1) */
1452static const unsigned char alt_10[] =
1453 {0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
3ae729d5
L
1454/* data16 nopw %cs:0L(%eax,%eax,1) */
1455static const unsigned char alt_11[] =
1456 {0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
62a02d25
L
1457/* 32-bit and 64-bit NOPs patterns. */
1458static const unsigned char *const alt_patt[] = {
1459 f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
3ae729d5 1460 alt_9, alt_10, alt_11
62a02d25
L
1461};
1462
1463/* Genenerate COUNT bytes of NOPs to WHERE from PATT with the maximum
1464 size of a single NOP instruction MAX_SINGLE_NOP_SIZE. */
1465
1466static void
1467i386_output_nops (char *where, const unsigned char *const *patt,
1468 int count, int max_single_nop_size)
1469
1470{
3ae729d5
L
1471 /* Place the longer NOP first. */
1472 int last;
1473 int offset;
3076e594
NC
1474 const unsigned char *nops;
1475
1476 if (max_single_nop_size < 1)
1477 {
1478 as_fatal (_("i386_output_nops called to generate nops of at most %d bytes!"),
1479 max_single_nop_size);
1480 return;
1481 }
1482
1483 nops = patt[max_single_nop_size - 1];
3ae729d5
L
1484
1485 /* Use the smaller one if the requsted one isn't available. */
1486 if (nops == NULL)
62a02d25 1487 {
3ae729d5
L
1488 max_single_nop_size--;
1489 nops = patt[max_single_nop_size - 1];
62a02d25
L
1490 }
1491
3ae729d5
L
1492 last = count % max_single_nop_size;
1493
1494 count -= last;
1495 for (offset = 0; offset < count; offset += max_single_nop_size)
1496 memcpy (where + offset, nops, max_single_nop_size);
1497
1498 if (last)
1499 {
1500 nops = patt[last - 1];
1501 if (nops == NULL)
1502 {
1503 /* Use the smaller one plus one-byte NOP if the needed one
1504 isn't available. */
1505 last--;
1506 nops = patt[last - 1];
1507 memcpy (where + offset, nops, last);
1508 where[offset + last] = *patt[0];
1509 }
1510 else
1511 memcpy (where + offset, nops, last);
1512 }
62a02d25
L
1513}
1514
3ae729d5
L
1515static INLINE int
1516fits_in_imm7 (offsetT num)
1517{
1518 return (num & 0x7f) == num;
1519}
1520
1521static INLINE int
1522fits_in_imm31 (offsetT num)
1523{
1524 return (num & 0x7fffffff) == num;
1525}
62a02d25
L
1526
1527/* Genenerate COUNT bytes of NOPs to WHERE with the maximum size of a
1528 single NOP instruction LIMIT. */
1529
1530void
3ae729d5 1531i386_generate_nops (fragS *fragP, char *where, offsetT count, int limit)
62a02d25 1532{
3ae729d5 1533 const unsigned char *const *patt = NULL;
62a02d25 1534 int max_single_nop_size;
3ae729d5
L
1535 /* Maximum number of NOPs before switching to jump over NOPs. */
1536 int max_number_of_nops;
62a02d25 1537
3ae729d5 1538 switch (fragP->fr_type)
62a02d25 1539 {
3ae729d5
L
1540 case rs_fill_nop:
1541 case rs_align_code:
1542 break;
e379e5f3
L
1543 case rs_machine_dependent:
1544 /* Allow NOP padding for jumps and calls. */
1545 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
1546 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
1547 break;
1548 /* Fall through. */
3ae729d5 1549 default:
62a02d25
L
1550 return;
1551 }
1552
ccc9c027
L
1553 /* We need to decide which NOP sequence to use for 32bit and
1554 64bit. When -mtune= is used:
4eed87de 1555
76bc74dc
L
1556 1. For PROCESSOR_I386, PROCESSOR_I486, PROCESSOR_PENTIUM and
1557 PROCESSOR_GENERIC32, f32_patt will be used.
80b8656c
L
1558 2. For the rest, alt_patt will be used.
1559
1560 When -mtune= isn't used, alt_patt will be used if
22109423 1561 cpu_arch_isa_flags has CpuNop. Otherwise, f32_patt will
76bc74dc 1562 be used.
ccc9c027
L
1563
1564 When -march= or .arch is used, we can't use anything beyond
1565 cpu_arch_isa_flags. */
1566
1567 if (flag_code == CODE_16BIT)
1568 {
3ae729d5
L
1569 patt = f16_patt;
1570 max_single_nop_size = sizeof (f16_patt) / sizeof (f16_patt[0]);
1571 /* Limit number of NOPs to 2 in 16-bit mode. */
1572 max_number_of_nops = 2;
252b5132 1573 }
33fef721 1574 else
ccc9c027 1575 {
fbf3f584 1576 if (fragP->tc_frag_data.isa == PROCESSOR_UNKNOWN)
ccc9c027
L
1577 {
1578 /* PROCESSOR_UNKNOWN means that all ISAs may be used. */
1579 switch (cpu_arch_tune)
1580 {
1581 case PROCESSOR_UNKNOWN:
1582 /* We use cpu_arch_isa_flags to check if we SHOULD
22109423
L
1583 optimize with nops. */
1584 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1585 patt = alt_patt;
ccc9c027
L
1586 else
1587 patt = f32_patt;
1588 break;
ccc9c027
L
1589 case PROCESSOR_PENTIUM4:
1590 case PROCESSOR_NOCONA:
ef05d495 1591 case PROCESSOR_CORE:
76bc74dc 1592 case PROCESSOR_CORE2:
bd5295b2 1593 case PROCESSOR_COREI7:
3632d14b 1594 case PROCESSOR_L1OM:
7a9068fe 1595 case PROCESSOR_K1OM:
76bc74dc 1596 case PROCESSOR_GENERIC64:
ccc9c027
L
1597 case PROCESSOR_K6:
1598 case PROCESSOR_ATHLON:
1599 case PROCESSOR_K8:
4eed87de 1600 case PROCESSOR_AMDFAM10:
8aedb9fe 1601 case PROCESSOR_BD:
029f3522 1602 case PROCESSOR_ZNVER:
7b458c12 1603 case PROCESSOR_BT:
80b8656c 1604 patt = alt_patt;
ccc9c027 1605 break;
76bc74dc 1606 case PROCESSOR_I386:
ccc9c027
L
1607 case PROCESSOR_I486:
1608 case PROCESSOR_PENTIUM:
2dde1948 1609 case PROCESSOR_PENTIUMPRO:
81486035 1610 case PROCESSOR_IAMCU:
ccc9c027
L
1611 case PROCESSOR_GENERIC32:
1612 patt = f32_patt;
1613 break;
4eed87de 1614 }
ccc9c027
L
1615 }
1616 else
1617 {
fbf3f584 1618 switch (fragP->tc_frag_data.tune)
ccc9c027
L
1619 {
1620 case PROCESSOR_UNKNOWN:
e6a14101 1621 /* When cpu_arch_isa is set, cpu_arch_tune shouldn't be
ccc9c027
L
1622 PROCESSOR_UNKNOWN. */
1623 abort ();
1624 break;
1625
76bc74dc 1626 case PROCESSOR_I386:
ccc9c027
L
1627 case PROCESSOR_I486:
1628 case PROCESSOR_PENTIUM:
81486035 1629 case PROCESSOR_IAMCU:
ccc9c027
L
1630 case PROCESSOR_K6:
1631 case PROCESSOR_ATHLON:
1632 case PROCESSOR_K8:
4eed87de 1633 case PROCESSOR_AMDFAM10:
8aedb9fe 1634 case PROCESSOR_BD:
029f3522 1635 case PROCESSOR_ZNVER:
7b458c12 1636 case PROCESSOR_BT:
ccc9c027
L
1637 case PROCESSOR_GENERIC32:
1638 /* We use cpu_arch_isa_flags to check if we CAN optimize
22109423
L
1639 with nops. */
1640 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1641 patt = alt_patt;
ccc9c027
L
1642 else
1643 patt = f32_patt;
1644 break;
76bc74dc
L
1645 case PROCESSOR_PENTIUMPRO:
1646 case PROCESSOR_PENTIUM4:
1647 case PROCESSOR_NOCONA:
1648 case PROCESSOR_CORE:
ef05d495 1649 case PROCESSOR_CORE2:
bd5295b2 1650 case PROCESSOR_COREI7:
3632d14b 1651 case PROCESSOR_L1OM:
7a9068fe 1652 case PROCESSOR_K1OM:
22109423 1653 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1654 patt = alt_patt;
ccc9c027
L
1655 else
1656 patt = f32_patt;
1657 break;
1658 case PROCESSOR_GENERIC64:
80b8656c 1659 patt = alt_patt;
ccc9c027 1660 break;
4eed87de 1661 }
ccc9c027
L
1662 }
1663
76bc74dc
L
1664 if (patt == f32_patt)
1665 {
3ae729d5
L
1666 max_single_nop_size = sizeof (f32_patt) / sizeof (f32_patt[0]);
1667 /* Limit number of NOPs to 2 for older processors. */
1668 max_number_of_nops = 2;
76bc74dc
L
1669 }
1670 else
1671 {
3ae729d5
L
1672 max_single_nop_size = sizeof (alt_patt) / sizeof (alt_patt[0]);
1673 /* Limit number of NOPs to 7 for newer processors. */
1674 max_number_of_nops = 7;
1675 }
1676 }
1677
1678 if (limit == 0)
1679 limit = max_single_nop_size;
1680
1681 if (fragP->fr_type == rs_fill_nop)
1682 {
1683 /* Output NOPs for .nop directive. */
1684 if (limit > max_single_nop_size)
1685 {
1686 as_bad_where (fragP->fr_file, fragP->fr_line,
1687 _("invalid single nop size: %d "
1688 "(expect within [0, %d])"),
1689 limit, max_single_nop_size);
1690 return;
1691 }
1692 }
e379e5f3 1693 else if (fragP->fr_type != rs_machine_dependent)
3ae729d5
L
1694 fragP->fr_var = count;
1695
1696 if ((count / max_single_nop_size) > max_number_of_nops)
1697 {
1698 /* Generate jump over NOPs. */
1699 offsetT disp = count - 2;
1700 if (fits_in_imm7 (disp))
1701 {
1702 /* Use "jmp disp8" if possible. */
1703 count = disp;
1704 where[0] = jump_disp8[0];
1705 where[1] = count;
1706 where += 2;
1707 }
1708 else
1709 {
1710 unsigned int size_of_jump;
1711
1712 if (flag_code == CODE_16BIT)
1713 {
1714 where[0] = jump16_disp32[0];
1715 where[1] = jump16_disp32[1];
1716 size_of_jump = 2;
1717 }
1718 else
1719 {
1720 where[0] = jump32_disp32[0];
1721 size_of_jump = 1;
1722 }
1723
1724 count -= size_of_jump + 4;
1725 if (!fits_in_imm31 (count))
1726 {
1727 as_bad_where (fragP->fr_file, fragP->fr_line,
1728 _("jump over nop padding out of range"));
1729 return;
1730 }
1731
1732 md_number_to_chars (where + size_of_jump, count, 4);
1733 where += size_of_jump + 4;
76bc74dc 1734 }
ccc9c027 1735 }
3ae729d5
L
1736
1737 /* Generate multiple NOPs. */
1738 i386_output_nops (where, patt, count, limit);
252b5132
RH
1739}
1740
c6fb90c8 1741static INLINE int
0dfbf9d7 1742operand_type_all_zero (const union i386_operand_type *x)
40fb9820 1743{
0dfbf9d7 1744 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1745 {
1746 case 3:
0dfbf9d7 1747 if (x->array[2])
c6fb90c8 1748 return 0;
1a0670f3 1749 /* Fall through. */
c6fb90c8 1750 case 2:
0dfbf9d7 1751 if (x->array[1])
c6fb90c8 1752 return 0;
1a0670f3 1753 /* Fall through. */
c6fb90c8 1754 case 1:
0dfbf9d7 1755 return !x->array[0];
c6fb90c8
L
1756 default:
1757 abort ();
1758 }
40fb9820
L
1759}
1760
c6fb90c8 1761static INLINE void
0dfbf9d7 1762operand_type_set (union i386_operand_type *x, unsigned int v)
40fb9820 1763{
0dfbf9d7 1764 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1765 {
1766 case 3:
0dfbf9d7 1767 x->array[2] = v;
1a0670f3 1768 /* Fall through. */
c6fb90c8 1769 case 2:
0dfbf9d7 1770 x->array[1] = v;
1a0670f3 1771 /* Fall through. */
c6fb90c8 1772 case 1:
0dfbf9d7 1773 x->array[0] = v;
1a0670f3 1774 /* Fall through. */
c6fb90c8
L
1775 break;
1776 default:
1777 abort ();
1778 }
bab6aec1
JB
1779
1780 x->bitfield.class = ClassNone;
75e5731b 1781 x->bitfield.instance = InstanceNone;
c6fb90c8 1782}
40fb9820 1783
c6fb90c8 1784static INLINE int
0dfbf9d7
L
1785operand_type_equal (const union i386_operand_type *x,
1786 const union i386_operand_type *y)
c6fb90c8 1787{
0dfbf9d7 1788 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1789 {
1790 case 3:
0dfbf9d7 1791 if (x->array[2] != y->array[2])
c6fb90c8 1792 return 0;
1a0670f3 1793 /* Fall through. */
c6fb90c8 1794 case 2:
0dfbf9d7 1795 if (x->array[1] != y->array[1])
c6fb90c8 1796 return 0;
1a0670f3 1797 /* Fall through. */
c6fb90c8 1798 case 1:
0dfbf9d7 1799 return x->array[0] == y->array[0];
c6fb90c8
L
1800 break;
1801 default:
1802 abort ();
1803 }
1804}
40fb9820 1805
0dfbf9d7
L
1806static INLINE int
1807cpu_flags_all_zero (const union i386_cpu_flags *x)
1808{
1809 switch (ARRAY_SIZE(x->array))
1810 {
53467f57
IT
1811 case 4:
1812 if (x->array[3])
1813 return 0;
1814 /* Fall through. */
0dfbf9d7
L
1815 case 3:
1816 if (x->array[2])
1817 return 0;
1a0670f3 1818 /* Fall through. */
0dfbf9d7
L
1819 case 2:
1820 if (x->array[1])
1821 return 0;
1a0670f3 1822 /* Fall through. */
0dfbf9d7
L
1823 case 1:
1824 return !x->array[0];
1825 default:
1826 abort ();
1827 }
1828}
1829
0dfbf9d7
L
1830static INLINE int
1831cpu_flags_equal (const union i386_cpu_flags *x,
1832 const union i386_cpu_flags *y)
1833{
1834 switch (ARRAY_SIZE(x->array))
1835 {
53467f57
IT
1836 case 4:
1837 if (x->array[3] != y->array[3])
1838 return 0;
1839 /* Fall through. */
0dfbf9d7
L
1840 case 3:
1841 if (x->array[2] != y->array[2])
1842 return 0;
1a0670f3 1843 /* Fall through. */
0dfbf9d7
L
1844 case 2:
1845 if (x->array[1] != y->array[1])
1846 return 0;
1a0670f3 1847 /* Fall through. */
0dfbf9d7
L
1848 case 1:
1849 return x->array[0] == y->array[0];
1850 break;
1851 default:
1852 abort ();
1853 }
1854}
c6fb90c8
L
1855
1856static INLINE int
1857cpu_flags_check_cpu64 (i386_cpu_flags f)
1858{
1859 return !((flag_code == CODE_64BIT && f.bitfield.cpuno64)
1860 || (flag_code != CODE_64BIT && f.bitfield.cpu64));
40fb9820
L
1861}
1862
c6fb90c8
L
1863static INLINE i386_cpu_flags
1864cpu_flags_and (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1865{
c6fb90c8
L
1866 switch (ARRAY_SIZE (x.array))
1867 {
53467f57
IT
1868 case 4:
1869 x.array [3] &= y.array [3];
1870 /* Fall through. */
c6fb90c8
L
1871 case 3:
1872 x.array [2] &= y.array [2];
1a0670f3 1873 /* Fall through. */
c6fb90c8
L
1874 case 2:
1875 x.array [1] &= y.array [1];
1a0670f3 1876 /* Fall through. */
c6fb90c8
L
1877 case 1:
1878 x.array [0] &= y.array [0];
1879 break;
1880 default:
1881 abort ();
1882 }
1883 return x;
1884}
40fb9820 1885
c6fb90c8
L
1886static INLINE i386_cpu_flags
1887cpu_flags_or (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1888{
c6fb90c8 1889 switch (ARRAY_SIZE (x.array))
40fb9820 1890 {
53467f57
IT
1891 case 4:
1892 x.array [3] |= y.array [3];
1893 /* Fall through. */
c6fb90c8
L
1894 case 3:
1895 x.array [2] |= y.array [2];
1a0670f3 1896 /* Fall through. */
c6fb90c8
L
1897 case 2:
1898 x.array [1] |= y.array [1];
1a0670f3 1899 /* Fall through. */
c6fb90c8
L
1900 case 1:
1901 x.array [0] |= y.array [0];
40fb9820
L
1902 break;
1903 default:
1904 abort ();
1905 }
40fb9820
L
1906 return x;
1907}
1908
309d3373
JB
1909static INLINE i386_cpu_flags
1910cpu_flags_and_not (i386_cpu_flags x, i386_cpu_flags y)
1911{
1912 switch (ARRAY_SIZE (x.array))
1913 {
53467f57
IT
1914 case 4:
1915 x.array [3] &= ~y.array [3];
1916 /* Fall through. */
309d3373
JB
1917 case 3:
1918 x.array [2] &= ~y.array [2];
1a0670f3 1919 /* Fall through. */
309d3373
JB
1920 case 2:
1921 x.array [1] &= ~y.array [1];
1a0670f3 1922 /* Fall through. */
309d3373
JB
1923 case 1:
1924 x.array [0] &= ~y.array [0];
1925 break;
1926 default:
1927 abort ();
1928 }
1929 return x;
1930}
1931
6c0946d0
JB
1932static const i386_cpu_flags avx512 = CPU_ANY_AVX512F_FLAGS;
1933
c0f3af97
L
1934#define CPU_FLAGS_ARCH_MATCH 0x1
1935#define CPU_FLAGS_64BIT_MATCH 0x2
1936
c0f3af97 1937#define CPU_FLAGS_PERFECT_MATCH \
db12e14e 1938 (CPU_FLAGS_ARCH_MATCH | CPU_FLAGS_64BIT_MATCH)
c0f3af97
L
1939
1940/* Return CPU flags match bits. */
3629bb00 1941
40fb9820 1942static int
d3ce72d0 1943cpu_flags_match (const insn_template *t)
40fb9820 1944{
c0f3af97
L
1945 i386_cpu_flags x = t->cpu_flags;
1946 int match = cpu_flags_check_cpu64 (x) ? CPU_FLAGS_64BIT_MATCH : 0;
40fb9820
L
1947
1948 x.bitfield.cpu64 = 0;
1949 x.bitfield.cpuno64 = 0;
1950
0dfbf9d7 1951 if (cpu_flags_all_zero (&x))
c0f3af97
L
1952 {
1953 /* This instruction is available on all archs. */
db12e14e 1954 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 1955 }
3629bb00
L
1956 else
1957 {
c0f3af97 1958 /* This instruction is available only on some archs. */
3629bb00
L
1959 i386_cpu_flags cpu = cpu_arch_flags;
1960
ab592e75
JB
1961 /* AVX512VL is no standalone feature - match it and then strip it. */
1962 if (x.bitfield.cpuavx512vl && !cpu.bitfield.cpuavx512vl)
1963 return match;
1964 x.bitfield.cpuavx512vl = 0;
1965
3629bb00 1966 cpu = cpu_flags_and (x, cpu);
c0f3af97
L
1967 if (!cpu_flags_all_zero (&cpu))
1968 {
57392598 1969 if (x.bitfield.cpuavx)
a5ff0eb2 1970 {
929f69fa 1971 /* We need to check a few extra flags with AVX. */
b9d49817 1972 if (cpu.bitfield.cpuavx
40d231b4
JB
1973 && (!t->opcode_modifier.sse2avx
1974 || (sse2avx && !i.prefix[DATA_PREFIX]))
b9d49817 1975 && (!x.bitfield.cpuaes || cpu.bitfield.cpuaes)
929f69fa 1976 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
b9d49817
JB
1977 && (!x.bitfield.cpupclmul || cpu.bitfield.cpupclmul))
1978 match |= CPU_FLAGS_ARCH_MATCH;
a5ff0eb2 1979 }
929f69fa
JB
1980 else if (x.bitfield.cpuavx512f)
1981 {
1982 /* We need to check a few extra flags with AVX512F. */
1983 if (cpu.bitfield.cpuavx512f
1984 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
1985 && (!x.bitfield.cpuvaes || cpu.bitfield.cpuvaes)
1986 && (!x.bitfield.cpuvpclmulqdq || cpu.bitfield.cpuvpclmulqdq))
1987 match |= CPU_FLAGS_ARCH_MATCH;
1988 }
a5ff0eb2 1989 else
db12e14e 1990 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 1991 }
3629bb00 1992 }
c0f3af97 1993 return match;
40fb9820
L
1994}
1995
c6fb90c8
L
1996static INLINE i386_operand_type
1997operand_type_and (i386_operand_type x, i386_operand_type y)
40fb9820 1998{
bab6aec1
JB
1999 if (x.bitfield.class != y.bitfield.class)
2000 x.bitfield.class = ClassNone;
75e5731b
JB
2001 if (x.bitfield.instance != y.bitfield.instance)
2002 x.bitfield.instance = InstanceNone;
bab6aec1 2003
c6fb90c8
L
2004 switch (ARRAY_SIZE (x.array))
2005 {
2006 case 3:
2007 x.array [2] &= y.array [2];
1a0670f3 2008 /* Fall through. */
c6fb90c8
L
2009 case 2:
2010 x.array [1] &= y.array [1];
1a0670f3 2011 /* Fall through. */
c6fb90c8
L
2012 case 1:
2013 x.array [0] &= y.array [0];
2014 break;
2015 default:
2016 abort ();
2017 }
2018 return x;
40fb9820
L
2019}
2020
73053c1f
JB
2021static INLINE i386_operand_type
2022operand_type_and_not (i386_operand_type x, i386_operand_type y)
2023{
bab6aec1 2024 gas_assert (y.bitfield.class == ClassNone);
75e5731b 2025 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 2026
73053c1f
JB
2027 switch (ARRAY_SIZE (x.array))
2028 {
2029 case 3:
2030 x.array [2] &= ~y.array [2];
2031 /* Fall through. */
2032 case 2:
2033 x.array [1] &= ~y.array [1];
2034 /* Fall through. */
2035 case 1:
2036 x.array [0] &= ~y.array [0];
2037 break;
2038 default:
2039 abort ();
2040 }
2041 return x;
2042}
2043
c6fb90c8
L
2044static INLINE i386_operand_type
2045operand_type_or (i386_operand_type x, i386_operand_type y)
40fb9820 2046{
bab6aec1
JB
2047 gas_assert (x.bitfield.class == ClassNone ||
2048 y.bitfield.class == ClassNone ||
2049 x.bitfield.class == y.bitfield.class);
75e5731b
JB
2050 gas_assert (x.bitfield.instance == InstanceNone ||
2051 y.bitfield.instance == InstanceNone ||
2052 x.bitfield.instance == y.bitfield.instance);
bab6aec1 2053
c6fb90c8 2054 switch (ARRAY_SIZE (x.array))
40fb9820 2055 {
c6fb90c8
L
2056 case 3:
2057 x.array [2] |= y.array [2];
1a0670f3 2058 /* Fall through. */
c6fb90c8
L
2059 case 2:
2060 x.array [1] |= y.array [1];
1a0670f3 2061 /* Fall through. */
c6fb90c8
L
2062 case 1:
2063 x.array [0] |= y.array [0];
40fb9820
L
2064 break;
2065 default:
2066 abort ();
2067 }
c6fb90c8
L
2068 return x;
2069}
40fb9820 2070
c6fb90c8
L
2071static INLINE i386_operand_type
2072operand_type_xor (i386_operand_type x, i386_operand_type y)
2073{
bab6aec1 2074 gas_assert (y.bitfield.class == ClassNone);
75e5731b 2075 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 2076
c6fb90c8
L
2077 switch (ARRAY_SIZE (x.array))
2078 {
2079 case 3:
2080 x.array [2] ^= y.array [2];
1a0670f3 2081 /* Fall through. */
c6fb90c8
L
2082 case 2:
2083 x.array [1] ^= y.array [1];
1a0670f3 2084 /* Fall through. */
c6fb90c8
L
2085 case 1:
2086 x.array [0] ^= y.array [0];
2087 break;
2088 default:
2089 abort ();
2090 }
40fb9820
L
2091 return x;
2092}
2093
40fb9820
L
2094static const i386_operand_type disp16 = OPERAND_TYPE_DISP16;
2095static const i386_operand_type disp32 = OPERAND_TYPE_DISP32;
2096static const i386_operand_type disp32s = OPERAND_TYPE_DISP32S;
2097static const i386_operand_type disp16_32 = OPERAND_TYPE_DISP16_32;
bab6aec1
JB
2098static const i386_operand_type anydisp = OPERAND_TYPE_ANYDISP;
2099static const i386_operand_type anyimm = OPERAND_TYPE_ANYIMM;
40fb9820 2100static const i386_operand_type regxmm = OPERAND_TYPE_REGXMM;
43234a1e 2101static const i386_operand_type regmask = OPERAND_TYPE_REGMASK;
40fb9820
L
2102static const i386_operand_type imm8 = OPERAND_TYPE_IMM8;
2103static const i386_operand_type imm8s = OPERAND_TYPE_IMM8S;
2104static const i386_operand_type imm16 = OPERAND_TYPE_IMM16;
2105static const i386_operand_type imm32 = OPERAND_TYPE_IMM32;
2106static const i386_operand_type imm32s = OPERAND_TYPE_IMM32S;
2107static const i386_operand_type imm64 = OPERAND_TYPE_IMM64;
2108static const i386_operand_type imm16_32 = OPERAND_TYPE_IMM16_32;
2109static const i386_operand_type imm16_32s = OPERAND_TYPE_IMM16_32S;
2110static const i386_operand_type imm16_32_32s = OPERAND_TYPE_IMM16_32_32S;
2111
2112enum operand_type
2113{
2114 reg,
40fb9820
L
2115 imm,
2116 disp,
2117 anymem
2118};
2119
c6fb90c8 2120static INLINE int
40fb9820
L
2121operand_type_check (i386_operand_type t, enum operand_type c)
2122{
2123 switch (c)
2124 {
2125 case reg:
bab6aec1 2126 return t.bitfield.class == Reg;
40fb9820 2127
40fb9820
L
2128 case imm:
2129 return (t.bitfield.imm8
2130 || t.bitfield.imm8s
2131 || t.bitfield.imm16
2132 || t.bitfield.imm32
2133 || t.bitfield.imm32s
2134 || t.bitfield.imm64);
2135
2136 case disp:
2137 return (t.bitfield.disp8
2138 || t.bitfield.disp16
2139 || t.bitfield.disp32
2140 || t.bitfield.disp32s
2141 || t.bitfield.disp64);
2142
2143 case anymem:
2144 return (t.bitfield.disp8
2145 || t.bitfield.disp16
2146 || t.bitfield.disp32
2147 || t.bitfield.disp32s
2148 || t.bitfield.disp64
2149 || t.bitfield.baseindex);
2150
2151 default:
2152 abort ();
2153 }
2cfe26b6
AM
2154
2155 return 0;
40fb9820
L
2156}
2157
7a54636a
L
2158/* Return 1 if there is no conflict in 8bit/16bit/32bit/64bit/80bit size
2159 between operand GIVEN and opeand WANTED for instruction template T. */
5c07affc
L
2160
2161static INLINE int
7a54636a
L
2162match_operand_size (const insn_template *t, unsigned int wanted,
2163 unsigned int given)
5c07affc 2164{
3ac21baa
JB
2165 return !((i.types[given].bitfield.byte
2166 && !t->operand_types[wanted].bitfield.byte)
2167 || (i.types[given].bitfield.word
2168 && !t->operand_types[wanted].bitfield.word)
2169 || (i.types[given].bitfield.dword
2170 && !t->operand_types[wanted].bitfield.dword)
2171 || (i.types[given].bitfield.qword
2172 && !t->operand_types[wanted].bitfield.qword)
2173 || (i.types[given].bitfield.tbyte
2174 && !t->operand_types[wanted].bitfield.tbyte));
5c07affc
L
2175}
2176
dd40ce22
L
2177/* Return 1 if there is no conflict in SIMD register between operand
2178 GIVEN and opeand WANTED for instruction template T. */
1b54b8d7
JB
2179
2180static INLINE int
dd40ce22
L
2181match_simd_size (const insn_template *t, unsigned int wanted,
2182 unsigned int given)
1b54b8d7 2183{
3ac21baa
JB
2184 return !((i.types[given].bitfield.xmmword
2185 && !t->operand_types[wanted].bitfield.xmmword)
2186 || (i.types[given].bitfield.ymmword
2187 && !t->operand_types[wanted].bitfield.ymmword)
2188 || (i.types[given].bitfield.zmmword
260cd341
LC
2189 && !t->operand_types[wanted].bitfield.zmmword)
2190 || (i.types[given].bitfield.tmmword
2191 && !t->operand_types[wanted].bitfield.tmmword));
1b54b8d7
JB
2192}
2193
7a54636a
L
2194/* Return 1 if there is no conflict in any size between operand GIVEN
2195 and opeand WANTED for instruction template T. */
5c07affc
L
2196
2197static INLINE int
dd40ce22
L
2198match_mem_size (const insn_template *t, unsigned int wanted,
2199 unsigned int given)
5c07affc 2200{
7a54636a 2201 return (match_operand_size (t, wanted, given)
3ac21baa 2202 && !((i.types[given].bitfield.unspecified
5273a3cd 2203 && !i.broadcast.type
3ac21baa
JB
2204 && !t->operand_types[wanted].bitfield.unspecified)
2205 || (i.types[given].bitfield.fword
2206 && !t->operand_types[wanted].bitfield.fword)
1b54b8d7
JB
2207 /* For scalar opcode templates to allow register and memory
2208 operands at the same time, some special casing is needed
d6793fa1
JB
2209 here. Also for v{,p}broadcast*, {,v}pmov{s,z}*, and
2210 down-conversion vpmov*. */
3528c362 2211 || ((t->operand_types[wanted].bitfield.class == RegSIMD
bc49bfd8
JB
2212 && t->operand_types[wanted].bitfield.byte
2213 + t->operand_types[wanted].bitfield.word
2214 + t->operand_types[wanted].bitfield.dword
2215 + t->operand_types[wanted].bitfield.qword
2216 > !!t->opcode_modifier.broadcast)
3ac21baa
JB
2217 ? (i.types[given].bitfield.xmmword
2218 || i.types[given].bitfield.ymmword
2219 || i.types[given].bitfield.zmmword)
2220 : !match_simd_size(t, wanted, given))));
5c07affc
L
2221}
2222
3ac21baa
JB
2223/* Return value has MATCH_STRAIGHT set if there is no size conflict on any
2224 operands for instruction template T, and it has MATCH_REVERSE set if there
2225 is no size conflict on any operands for the template with operands reversed
2226 (and the template allows for reversing in the first place). */
5c07affc 2227
3ac21baa
JB
2228#define MATCH_STRAIGHT 1
2229#define MATCH_REVERSE 2
2230
2231static INLINE unsigned int
d3ce72d0 2232operand_size_match (const insn_template *t)
5c07affc 2233{
3ac21baa 2234 unsigned int j, match = MATCH_STRAIGHT;
5c07affc 2235
0cfa3eb3 2236 /* Don't check non-absolute jump instructions. */
5c07affc 2237 if (t->opcode_modifier.jump
0cfa3eb3 2238 && t->opcode_modifier.jump != JUMP_ABSOLUTE)
5c07affc
L
2239 return match;
2240
2241 /* Check memory and accumulator operand size. */
2242 for (j = 0; j < i.operands; j++)
2243 {
3528c362
JB
2244 if (i.types[j].bitfield.class != Reg
2245 && i.types[j].bitfield.class != RegSIMD
601e8564 2246 && t->opcode_modifier.anysize)
5c07affc
L
2247 continue;
2248
bab6aec1 2249 if (t->operand_types[j].bitfield.class == Reg
7a54636a 2250 && !match_operand_size (t, j, j))
5c07affc
L
2251 {
2252 match = 0;
2253 break;
2254 }
2255
3528c362 2256 if (t->operand_types[j].bitfield.class == RegSIMD
3ac21baa 2257 && !match_simd_size (t, j, j))
1b54b8d7
JB
2258 {
2259 match = 0;
2260 break;
2261 }
2262
75e5731b 2263 if (t->operand_types[j].bitfield.instance == Accum
7a54636a 2264 && (!match_operand_size (t, j, j) || !match_simd_size (t, j, j)))
1b54b8d7
JB
2265 {
2266 match = 0;
2267 break;
2268 }
2269
c48dadc9 2270 if ((i.flags[j] & Operand_Mem) && !match_mem_size (t, j, j))
5c07affc
L
2271 {
2272 match = 0;
2273 break;
2274 }
2275 }
2276
3ac21baa 2277 if (!t->opcode_modifier.d)
891edac4 2278 {
dc1e8a47 2279 mismatch:
3ac21baa
JB
2280 if (!match)
2281 i.error = operand_size_mismatch;
2282 return match;
891edac4 2283 }
5c07affc
L
2284
2285 /* Check reverse. */
f5eb1d70 2286 gas_assert (i.operands >= 2 && i.operands <= 3);
5c07affc 2287
f5eb1d70 2288 for (j = 0; j < i.operands; j++)
5c07affc 2289 {
f5eb1d70
JB
2290 unsigned int given = i.operands - j - 1;
2291
bab6aec1 2292 if (t->operand_types[j].bitfield.class == Reg
f5eb1d70 2293 && !match_operand_size (t, j, given))
891edac4 2294 goto mismatch;
5c07affc 2295
3528c362 2296 if (t->operand_types[j].bitfield.class == RegSIMD
f5eb1d70 2297 && !match_simd_size (t, j, given))
dbbc8b7e
JB
2298 goto mismatch;
2299
75e5731b 2300 if (t->operand_types[j].bitfield.instance == Accum
f5eb1d70
JB
2301 && (!match_operand_size (t, j, given)
2302 || !match_simd_size (t, j, given)))
dbbc8b7e
JB
2303 goto mismatch;
2304
f5eb1d70 2305 if ((i.flags[given] & Operand_Mem) && !match_mem_size (t, j, given))
891edac4 2306 goto mismatch;
5c07affc
L
2307 }
2308
3ac21baa 2309 return match | MATCH_REVERSE;
5c07affc
L
2310}
2311
c6fb90c8 2312static INLINE int
40fb9820
L
2313operand_type_match (i386_operand_type overlap,
2314 i386_operand_type given)
2315{
2316 i386_operand_type temp = overlap;
2317
7d5e4556 2318 temp.bitfield.unspecified = 0;
5c07affc
L
2319 temp.bitfield.byte = 0;
2320 temp.bitfield.word = 0;
2321 temp.bitfield.dword = 0;
2322 temp.bitfield.fword = 0;
2323 temp.bitfield.qword = 0;
2324 temp.bitfield.tbyte = 0;
2325 temp.bitfield.xmmword = 0;
c0f3af97 2326 temp.bitfield.ymmword = 0;
43234a1e 2327 temp.bitfield.zmmword = 0;
260cd341 2328 temp.bitfield.tmmword = 0;
0dfbf9d7 2329 if (operand_type_all_zero (&temp))
891edac4 2330 goto mismatch;
40fb9820 2331
6f2f06be 2332 if (given.bitfield.baseindex == overlap.bitfield.baseindex)
891edac4
L
2333 return 1;
2334
dc1e8a47 2335 mismatch:
a65babc9 2336 i.error = operand_type_mismatch;
891edac4 2337 return 0;
40fb9820
L
2338}
2339
7d5e4556 2340/* If given types g0 and g1 are registers they must be of the same type
10c17abd 2341 unless the expected operand type register overlap is null.
5de4d9ef 2342 Some Intel syntax memory operand size checking also happens here. */
40fb9820 2343
c6fb90c8 2344static INLINE int
dc821c5f 2345operand_type_register_match (i386_operand_type g0,
40fb9820 2346 i386_operand_type t0,
40fb9820
L
2347 i386_operand_type g1,
2348 i386_operand_type t1)
2349{
bab6aec1 2350 if (g0.bitfield.class != Reg
3528c362 2351 && g0.bitfield.class != RegSIMD
10c17abd
JB
2352 && (!operand_type_check (g0, anymem)
2353 || g0.bitfield.unspecified
5de4d9ef
JB
2354 || (t0.bitfield.class != Reg
2355 && t0.bitfield.class != RegSIMD)))
40fb9820
L
2356 return 1;
2357
bab6aec1 2358 if (g1.bitfield.class != Reg
3528c362 2359 && g1.bitfield.class != RegSIMD
10c17abd
JB
2360 && (!operand_type_check (g1, anymem)
2361 || g1.bitfield.unspecified
5de4d9ef
JB
2362 || (t1.bitfield.class != Reg
2363 && t1.bitfield.class != RegSIMD)))
40fb9820
L
2364 return 1;
2365
dc821c5f
JB
2366 if (g0.bitfield.byte == g1.bitfield.byte
2367 && g0.bitfield.word == g1.bitfield.word
2368 && g0.bitfield.dword == g1.bitfield.dword
10c17abd
JB
2369 && g0.bitfield.qword == g1.bitfield.qword
2370 && g0.bitfield.xmmword == g1.bitfield.xmmword
2371 && g0.bitfield.ymmword == g1.bitfield.ymmword
2372 && g0.bitfield.zmmword == g1.bitfield.zmmword)
40fb9820
L
2373 return 1;
2374
dc821c5f
JB
2375 if (!(t0.bitfield.byte & t1.bitfield.byte)
2376 && !(t0.bitfield.word & t1.bitfield.word)
2377 && !(t0.bitfield.dword & t1.bitfield.dword)
10c17abd
JB
2378 && !(t0.bitfield.qword & t1.bitfield.qword)
2379 && !(t0.bitfield.xmmword & t1.bitfield.xmmword)
2380 && !(t0.bitfield.ymmword & t1.bitfield.ymmword)
2381 && !(t0.bitfield.zmmword & t1.bitfield.zmmword))
891edac4
L
2382 return 1;
2383
a65babc9 2384 i.error = register_type_mismatch;
891edac4
L
2385
2386 return 0;
40fb9820
L
2387}
2388
4c692bc7
JB
2389static INLINE unsigned int
2390register_number (const reg_entry *r)
2391{
2392 unsigned int nr = r->reg_num;
2393
2394 if (r->reg_flags & RegRex)
2395 nr += 8;
2396
200cbe0f
L
2397 if (r->reg_flags & RegVRex)
2398 nr += 16;
2399
4c692bc7
JB
2400 return nr;
2401}
2402
252b5132 2403static INLINE unsigned int
40fb9820 2404mode_from_disp_size (i386_operand_type t)
252b5132 2405{
b5014f7a 2406 if (t.bitfield.disp8)
40fb9820
L
2407 return 1;
2408 else if (t.bitfield.disp16
2409 || t.bitfield.disp32
2410 || t.bitfield.disp32s)
2411 return 2;
2412 else
2413 return 0;
252b5132
RH
2414}
2415
2416static INLINE int
65879393 2417fits_in_signed_byte (addressT num)
252b5132 2418{
65879393 2419 return num + 0x80 <= 0xff;
47926f60 2420}
252b5132
RH
2421
2422static INLINE int
65879393 2423fits_in_unsigned_byte (addressT num)
252b5132 2424{
65879393 2425 return num <= 0xff;
47926f60 2426}
252b5132
RH
2427
2428static INLINE int
65879393 2429fits_in_unsigned_word (addressT num)
252b5132 2430{
65879393 2431 return num <= 0xffff;
47926f60 2432}
252b5132
RH
2433
2434static INLINE int
65879393 2435fits_in_signed_word (addressT num)
252b5132 2436{
65879393 2437 return num + 0x8000 <= 0xffff;
47926f60 2438}
2a962e6d 2439
3e73aa7c 2440static INLINE int
65879393 2441fits_in_signed_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2442{
2443#ifndef BFD64
2444 return 1;
2445#else
65879393 2446 return num + 0x80000000 <= 0xffffffff;
3e73aa7c
JH
2447#endif
2448} /* fits_in_signed_long() */
2a962e6d 2449
3e73aa7c 2450static INLINE int
65879393 2451fits_in_unsigned_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2452{
2453#ifndef BFD64
2454 return 1;
2455#else
65879393 2456 return num <= 0xffffffff;
3e73aa7c
JH
2457#endif
2458} /* fits_in_unsigned_long() */
252b5132 2459
a442cac5
JB
2460static INLINE valueT extend_to_32bit_address (addressT num)
2461{
2462#ifdef BFD64
2463 if (fits_in_unsigned_long(num))
2464 return (num ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
2465
2466 if (!fits_in_signed_long (num))
2467 return num & 0xffffffff;
2468#endif
2469
2470 return num;
2471}
2472
43234a1e 2473static INLINE int
b5014f7a 2474fits_in_disp8 (offsetT num)
43234a1e
L
2475{
2476 int shift = i.memshift;
2477 unsigned int mask;
2478
2479 if (shift == -1)
2480 abort ();
2481
2482 mask = (1 << shift) - 1;
2483
2484 /* Return 0 if NUM isn't properly aligned. */
2485 if ((num & mask))
2486 return 0;
2487
2488 /* Check if NUM will fit in 8bit after shift. */
2489 return fits_in_signed_byte (num >> shift);
2490}
2491
a683cc34
SP
2492static INLINE int
2493fits_in_imm4 (offsetT num)
2494{
2495 return (num & 0xf) == num;
2496}
2497
40fb9820 2498static i386_operand_type
e3bb37b5 2499smallest_imm_type (offsetT num)
252b5132 2500{
40fb9820 2501 i386_operand_type t;
7ab9ffdd 2502
0dfbf9d7 2503 operand_type_set (&t, 0);
40fb9820
L
2504 t.bitfield.imm64 = 1;
2505
2506 if (cpu_arch_tune != PROCESSOR_I486 && num == 1)
e413e4e9
AM
2507 {
2508 /* This code is disabled on the 486 because all the Imm1 forms
2509 in the opcode table are slower on the i486. They're the
2510 versions with the implicitly specified single-position
2511 displacement, which has another syntax if you really want to
2512 use that form. */
40fb9820
L
2513 t.bitfield.imm1 = 1;
2514 t.bitfield.imm8 = 1;
2515 t.bitfield.imm8s = 1;
2516 t.bitfield.imm16 = 1;
2517 t.bitfield.imm32 = 1;
2518 t.bitfield.imm32s = 1;
2519 }
2520 else if (fits_in_signed_byte (num))
2521 {
2522 t.bitfield.imm8 = 1;
2523 t.bitfield.imm8s = 1;
2524 t.bitfield.imm16 = 1;
2525 t.bitfield.imm32 = 1;
2526 t.bitfield.imm32s = 1;
2527 }
2528 else if (fits_in_unsigned_byte (num))
2529 {
2530 t.bitfield.imm8 = 1;
2531 t.bitfield.imm16 = 1;
2532 t.bitfield.imm32 = 1;
2533 t.bitfield.imm32s = 1;
2534 }
2535 else if (fits_in_signed_word (num) || fits_in_unsigned_word (num))
2536 {
2537 t.bitfield.imm16 = 1;
2538 t.bitfield.imm32 = 1;
2539 t.bitfield.imm32s = 1;
2540 }
2541 else if (fits_in_signed_long (num))
2542 {
2543 t.bitfield.imm32 = 1;
2544 t.bitfield.imm32s = 1;
2545 }
2546 else if (fits_in_unsigned_long (num))
2547 t.bitfield.imm32 = 1;
2548
2549 return t;
47926f60 2550}
252b5132 2551
847f7ad4 2552static offsetT
e3bb37b5 2553offset_in_range (offsetT val, int size)
847f7ad4 2554{
508866be 2555 addressT mask;
ba2adb93 2556
847f7ad4
AM
2557 switch (size)
2558 {
508866be
L
2559 case 1: mask = ((addressT) 1 << 8) - 1; break;
2560 case 2: mask = ((addressT) 1 << 16) - 1; break;
3b0ec529 2561 case 4: mask = ((addressT) 2 << 31) - 1; break;
3e73aa7c
JH
2562#ifdef BFD64
2563 case 8: mask = ((addressT) 2 << 63) - 1; break;
2564#endif
47926f60 2565 default: abort ();
847f7ad4
AM
2566 }
2567
47926f60 2568 if ((val & ~mask) != 0 && (val & ~mask) != ~mask)
847f7ad4
AM
2569 {
2570 char buf1[40], buf2[40];
2571
9a8041fd
JB
2572 bfd_sprintf_vma (stdoutput, buf1, val);
2573 bfd_sprintf_vma (stdoutput, buf2, val & mask);
847f7ad4
AM
2574 as_warn (_("%s shortened to %s"), buf1, buf2);
2575 }
2576 return val & mask;
2577}
2578
c32fa91d
L
2579enum PREFIX_GROUP
2580{
2581 PREFIX_EXIST = 0,
2582 PREFIX_LOCK,
2583 PREFIX_REP,
04ef582a 2584 PREFIX_DS,
c32fa91d
L
2585 PREFIX_OTHER
2586};
2587
2588/* Returns
2589 a. PREFIX_EXIST if attempting to add a prefix where one from the
2590 same class already exists.
2591 b. PREFIX_LOCK if lock prefix is added.
2592 c. PREFIX_REP if rep/repne prefix is added.
04ef582a
L
2593 d. PREFIX_DS if ds prefix is added.
2594 e. PREFIX_OTHER if other prefix is added.
c32fa91d
L
2595 */
2596
2597static enum PREFIX_GROUP
e3bb37b5 2598add_prefix (unsigned int prefix)
252b5132 2599{
c32fa91d 2600 enum PREFIX_GROUP ret = PREFIX_OTHER;
b1905489 2601 unsigned int q;
252b5132 2602
29b0f896
AM
2603 if (prefix >= REX_OPCODE && prefix < REX_OPCODE + 16
2604 && flag_code == CODE_64BIT)
b1905489 2605 {
161a04f6 2606 if ((i.prefix[REX_PREFIX] & prefix & REX_W)
44846f29
JB
2607 || (i.prefix[REX_PREFIX] & prefix & REX_R)
2608 || (i.prefix[REX_PREFIX] & prefix & REX_X)
2609 || (i.prefix[REX_PREFIX] & prefix & REX_B))
c32fa91d 2610 ret = PREFIX_EXIST;
b1905489
JB
2611 q = REX_PREFIX;
2612 }
3e73aa7c 2613 else
b1905489
JB
2614 {
2615 switch (prefix)
2616 {
2617 default:
2618 abort ();
2619
b1905489 2620 case DS_PREFIX_OPCODE:
04ef582a
L
2621 ret = PREFIX_DS;
2622 /* Fall through. */
2623 case CS_PREFIX_OPCODE:
b1905489
JB
2624 case ES_PREFIX_OPCODE:
2625 case FS_PREFIX_OPCODE:
2626 case GS_PREFIX_OPCODE:
2627 case SS_PREFIX_OPCODE:
2628 q = SEG_PREFIX;
2629 break;
2630
2631 case REPNE_PREFIX_OPCODE:
2632 case REPE_PREFIX_OPCODE:
c32fa91d
L
2633 q = REP_PREFIX;
2634 ret = PREFIX_REP;
2635 break;
2636
b1905489 2637 case LOCK_PREFIX_OPCODE:
c32fa91d
L
2638 q = LOCK_PREFIX;
2639 ret = PREFIX_LOCK;
b1905489
JB
2640 break;
2641
2642 case FWAIT_OPCODE:
2643 q = WAIT_PREFIX;
2644 break;
2645
2646 case ADDR_PREFIX_OPCODE:
2647 q = ADDR_PREFIX;
2648 break;
2649
2650 case DATA_PREFIX_OPCODE:
2651 q = DATA_PREFIX;
2652 break;
2653 }
2654 if (i.prefix[q] != 0)
c32fa91d 2655 ret = PREFIX_EXIST;
b1905489 2656 }
252b5132 2657
b1905489 2658 if (ret)
252b5132 2659 {
b1905489
JB
2660 if (!i.prefix[q])
2661 ++i.prefixes;
2662 i.prefix[q] |= prefix;
252b5132 2663 }
b1905489
JB
2664 else
2665 as_bad (_("same type of prefix used twice"));
252b5132 2666
252b5132
RH
2667 return ret;
2668}
2669
2670static void
78f12dd3 2671update_code_flag (int value, int check)
eecb386c 2672{
78f12dd3
L
2673 PRINTF_LIKE ((*as_error));
2674
1e9cc1c2 2675 flag_code = (enum flag_code) value;
40fb9820
L
2676 if (flag_code == CODE_64BIT)
2677 {
2678 cpu_arch_flags.bitfield.cpu64 = 1;
2679 cpu_arch_flags.bitfield.cpuno64 = 0;
40fb9820
L
2680 }
2681 else
2682 {
2683 cpu_arch_flags.bitfield.cpu64 = 0;
2684 cpu_arch_flags.bitfield.cpuno64 = 1;
40fb9820
L
2685 }
2686 if (value == CODE_64BIT && !cpu_arch_flags.bitfield.cpulm )
3e73aa7c 2687 {
78f12dd3
L
2688 if (check)
2689 as_error = as_fatal;
2690 else
2691 as_error = as_bad;
2692 (*as_error) (_("64bit mode not supported on `%s'."),
2693 cpu_arch_name ? cpu_arch_name : default_arch);
3e73aa7c 2694 }
40fb9820 2695 if (value == CODE_32BIT && !cpu_arch_flags.bitfield.cpui386)
3e73aa7c 2696 {
78f12dd3
L
2697 if (check)
2698 as_error = as_fatal;
2699 else
2700 as_error = as_bad;
2701 (*as_error) (_("32bit mode not supported on `%s'."),
2702 cpu_arch_name ? cpu_arch_name : default_arch);
3e73aa7c 2703 }
eecb386c
AM
2704 stackop_size = '\0';
2705}
2706
78f12dd3
L
2707static void
2708set_code_flag (int value)
2709{
2710 update_code_flag (value, 0);
2711}
2712
eecb386c 2713static void
e3bb37b5 2714set_16bit_gcc_code_flag (int new_code_flag)
252b5132 2715{
1e9cc1c2 2716 flag_code = (enum flag_code) new_code_flag;
40fb9820
L
2717 if (flag_code != CODE_16BIT)
2718 abort ();
2719 cpu_arch_flags.bitfield.cpu64 = 0;
2720 cpu_arch_flags.bitfield.cpuno64 = 1;
9306ca4a 2721 stackop_size = LONG_MNEM_SUFFIX;
252b5132
RH
2722}
2723
2724static void
e3bb37b5 2725set_intel_syntax (int syntax_flag)
252b5132
RH
2726{
2727 /* Find out if register prefixing is specified. */
2728 int ask_naked_reg = 0;
2729
2730 SKIP_WHITESPACE ();
29b0f896 2731 if (!is_end_of_line[(unsigned char) *input_line_pointer])
252b5132 2732 {
d02603dc
NC
2733 char *string;
2734 int e = get_symbol_name (&string);
252b5132 2735
47926f60 2736 if (strcmp (string, "prefix") == 0)
252b5132 2737 ask_naked_reg = 1;
47926f60 2738 else if (strcmp (string, "noprefix") == 0)
252b5132
RH
2739 ask_naked_reg = -1;
2740 else
d0b47220 2741 as_bad (_("bad argument to syntax directive."));
d02603dc 2742 (void) restore_line_pointer (e);
252b5132
RH
2743 }
2744 demand_empty_rest_of_line ();
c3332e24 2745
252b5132
RH
2746 intel_syntax = syntax_flag;
2747
2748 if (ask_naked_reg == 0)
f86103b7
AM
2749 allow_naked_reg = (intel_syntax
2750 && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
252b5132
RH
2751 else
2752 allow_naked_reg = (ask_naked_reg < 0);
9306ca4a 2753
ee86248c 2754 expr_set_rank (O_full_ptr, syntax_flag ? 10 : 0);
7ab9ffdd 2755
e4a3b5a4 2756 identifier_chars['%'] = intel_syntax && allow_naked_reg ? '%' : 0;
9306ca4a 2757 identifier_chars['$'] = intel_syntax ? '$' : 0;
e4a3b5a4 2758 register_prefix = allow_naked_reg ? "" : "%";
252b5132
RH
2759}
2760
1efbbeb4
L
2761static void
2762set_intel_mnemonic (int mnemonic_flag)
2763{
e1d4d893 2764 intel_mnemonic = mnemonic_flag;
1efbbeb4
L
2765}
2766
db51cc60
L
2767static void
2768set_allow_index_reg (int flag)
2769{
2770 allow_index_reg = flag;
2771}
2772
cb19c032 2773static void
7bab8ab5 2774set_check (int what)
cb19c032 2775{
7bab8ab5
JB
2776 enum check_kind *kind;
2777 const char *str;
2778
2779 if (what)
2780 {
2781 kind = &operand_check;
2782 str = "operand";
2783 }
2784 else
2785 {
2786 kind = &sse_check;
2787 str = "sse";
2788 }
2789
cb19c032
L
2790 SKIP_WHITESPACE ();
2791
2792 if (!is_end_of_line[(unsigned char) *input_line_pointer])
2793 {
d02603dc
NC
2794 char *string;
2795 int e = get_symbol_name (&string);
cb19c032
L
2796
2797 if (strcmp (string, "none") == 0)
7bab8ab5 2798 *kind = check_none;
cb19c032 2799 else if (strcmp (string, "warning") == 0)
7bab8ab5 2800 *kind = check_warning;
cb19c032 2801 else if (strcmp (string, "error") == 0)
7bab8ab5 2802 *kind = check_error;
cb19c032 2803 else
7bab8ab5 2804 as_bad (_("bad argument to %s_check directive."), str);
d02603dc 2805 (void) restore_line_pointer (e);
cb19c032
L
2806 }
2807 else
7bab8ab5 2808 as_bad (_("missing argument for %s_check directive"), str);
cb19c032
L
2809
2810 demand_empty_rest_of_line ();
2811}
2812
8a9036a4
L
2813static void
2814check_cpu_arch_compatible (const char *name ATTRIBUTE_UNUSED,
1e9cc1c2 2815 i386_cpu_flags new_flag ATTRIBUTE_UNUSED)
8a9036a4
L
2816{
2817#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2818 static const char *arch;
2819
2820 /* Intel LIOM is only supported on ELF. */
2821 if (!IS_ELF)
2822 return;
2823
2824 if (!arch)
2825 {
2826 /* Use cpu_arch_name if it is set in md_parse_option. Otherwise
2827 use default_arch. */
2828 arch = cpu_arch_name;
2829 if (!arch)
2830 arch = default_arch;
2831 }
2832
81486035
L
2833 /* If we are targeting Intel MCU, we must enable it. */
2834 if (get_elf_backend_data (stdoutput)->elf_machine_code != EM_IAMCU
2835 || new_flag.bitfield.cpuiamcu)
2836 return;
2837
3632d14b 2838 /* If we are targeting Intel L1OM, we must enable it. */
8a9036a4 2839 if (get_elf_backend_data (stdoutput)->elf_machine_code != EM_L1OM
1e9cc1c2 2840 || new_flag.bitfield.cpul1om)
8a9036a4 2841 return;
76ba9986 2842
7a9068fe
L
2843 /* If we are targeting Intel K1OM, we must enable it. */
2844 if (get_elf_backend_data (stdoutput)->elf_machine_code != EM_K1OM
2845 || new_flag.bitfield.cpuk1om)
2846 return;
2847
8a9036a4
L
2848 as_bad (_("`%s' is not supported on `%s'"), name, arch);
2849#endif
2850}
2851
e413e4e9 2852static void
e3bb37b5 2853set_cpu_arch (int dummy ATTRIBUTE_UNUSED)
e413e4e9 2854{
47926f60 2855 SKIP_WHITESPACE ();
e413e4e9 2856
29b0f896 2857 if (!is_end_of_line[(unsigned char) *input_line_pointer])
e413e4e9 2858 {
d02603dc
NC
2859 char *string;
2860 int e = get_symbol_name (&string);
91d6fa6a 2861 unsigned int j;
40fb9820 2862 i386_cpu_flags flags;
e413e4e9 2863
91d6fa6a 2864 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
e413e4e9 2865 {
91d6fa6a 2866 if (strcmp (string, cpu_arch[j].name) == 0)
e413e4e9 2867 {
91d6fa6a 2868 check_cpu_arch_compatible (string, cpu_arch[j].flags);
8a9036a4 2869
5c6af06e
JB
2870 if (*string != '.')
2871 {
91d6fa6a 2872 cpu_arch_name = cpu_arch[j].name;
5c6af06e 2873 cpu_sub_arch_name = NULL;
91d6fa6a 2874 cpu_arch_flags = cpu_arch[j].flags;
40fb9820
L
2875 if (flag_code == CODE_64BIT)
2876 {
2877 cpu_arch_flags.bitfield.cpu64 = 1;
2878 cpu_arch_flags.bitfield.cpuno64 = 0;
2879 }
2880 else
2881 {
2882 cpu_arch_flags.bitfield.cpu64 = 0;
2883 cpu_arch_flags.bitfield.cpuno64 = 1;
2884 }
91d6fa6a
NC
2885 cpu_arch_isa = cpu_arch[j].type;
2886 cpu_arch_isa_flags = cpu_arch[j].flags;
ccc9c027
L
2887 if (!cpu_arch_tune_set)
2888 {
2889 cpu_arch_tune = cpu_arch_isa;
2890 cpu_arch_tune_flags = cpu_arch_isa_flags;
2891 }
5c6af06e
JB
2892 break;
2893 }
40fb9820 2894
293f5f65
L
2895 flags = cpu_flags_or (cpu_arch_flags,
2896 cpu_arch[j].flags);
81486035 2897
5b64d091 2898 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
5c6af06e 2899 {
6305a203
L
2900 if (cpu_sub_arch_name)
2901 {
2902 char *name = cpu_sub_arch_name;
2903 cpu_sub_arch_name = concat (name,
91d6fa6a 2904 cpu_arch[j].name,
1bf57e9f 2905 (const char *) NULL);
6305a203
L
2906 free (name);
2907 }
2908 else
91d6fa6a 2909 cpu_sub_arch_name = xstrdup (cpu_arch[j].name);
40fb9820 2910 cpu_arch_flags = flags;
a586129e 2911 cpu_arch_isa_flags = flags;
5c6af06e 2912 }
0089dace
L
2913 else
2914 cpu_arch_isa_flags
2915 = cpu_flags_or (cpu_arch_isa_flags,
2916 cpu_arch[j].flags);
d02603dc 2917 (void) restore_line_pointer (e);
5c6af06e
JB
2918 demand_empty_rest_of_line ();
2919 return;
e413e4e9
AM
2920 }
2921 }
293f5f65
L
2922
2923 if (*string == '.' && j >= ARRAY_SIZE (cpu_arch))
2924 {
33eaf5de 2925 /* Disable an ISA extension. */
293f5f65
L
2926 for (j = 0; j < ARRAY_SIZE (cpu_noarch); j++)
2927 if (strcmp (string + 1, cpu_noarch [j].name) == 0)
2928 {
2929 flags = cpu_flags_and_not (cpu_arch_flags,
2930 cpu_noarch[j].flags);
2931 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
2932 {
2933 if (cpu_sub_arch_name)
2934 {
2935 char *name = cpu_sub_arch_name;
2936 cpu_sub_arch_name = concat (name, string,
2937 (const char *) NULL);
2938 free (name);
2939 }
2940 else
2941 cpu_sub_arch_name = xstrdup (string);
2942 cpu_arch_flags = flags;
2943 cpu_arch_isa_flags = flags;
2944 }
2945 (void) restore_line_pointer (e);
2946 demand_empty_rest_of_line ();
2947 return;
2948 }
2949
2950 j = ARRAY_SIZE (cpu_arch);
2951 }
2952
91d6fa6a 2953 if (j >= ARRAY_SIZE (cpu_arch))
e413e4e9
AM
2954 as_bad (_("no such architecture: `%s'"), string);
2955
2956 *input_line_pointer = e;
2957 }
2958 else
2959 as_bad (_("missing cpu architecture"));
2960
fddf5b5b
AM
2961 no_cond_jump_promotion = 0;
2962 if (*input_line_pointer == ','
29b0f896 2963 && !is_end_of_line[(unsigned char) input_line_pointer[1]])
fddf5b5b 2964 {
d02603dc
NC
2965 char *string;
2966 char e;
2967
2968 ++input_line_pointer;
2969 e = get_symbol_name (&string);
fddf5b5b
AM
2970
2971 if (strcmp (string, "nojumps") == 0)
2972 no_cond_jump_promotion = 1;
2973 else if (strcmp (string, "jumps") == 0)
2974 ;
2975 else
2976 as_bad (_("no such architecture modifier: `%s'"), string);
2977
d02603dc 2978 (void) restore_line_pointer (e);
fddf5b5b
AM
2979 }
2980
e413e4e9
AM
2981 demand_empty_rest_of_line ();
2982}
2983
8a9036a4
L
2984enum bfd_architecture
2985i386_arch (void)
2986{
3632d14b 2987 if (cpu_arch_isa == PROCESSOR_L1OM)
8a9036a4
L
2988 {
2989 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2990 || flag_code != CODE_64BIT)
2991 as_fatal (_("Intel L1OM is 64bit ELF only"));
2992 return bfd_arch_l1om;
2993 }
7a9068fe
L
2994 else if (cpu_arch_isa == PROCESSOR_K1OM)
2995 {
2996 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2997 || flag_code != CODE_64BIT)
2998 as_fatal (_("Intel K1OM is 64bit ELF only"));
2999 return bfd_arch_k1om;
3000 }
81486035
L
3001 else if (cpu_arch_isa == PROCESSOR_IAMCU)
3002 {
3003 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
3004 || flag_code == CODE_64BIT)
3005 as_fatal (_("Intel MCU is 32bit ELF only"));
3006 return bfd_arch_iamcu;
3007 }
8a9036a4
L
3008 else
3009 return bfd_arch_i386;
3010}
3011
b9d79e03 3012unsigned long
7016a5d5 3013i386_mach (void)
b9d79e03 3014{
d34049e8 3015 if (startswith (default_arch, "x86_64"))
8a9036a4 3016 {
3632d14b 3017 if (cpu_arch_isa == PROCESSOR_L1OM)
8a9036a4 3018 {
351f65ca
L
3019 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
3020 || default_arch[6] != '\0')
8a9036a4
L
3021 as_fatal (_("Intel L1OM is 64bit ELF only"));
3022 return bfd_mach_l1om;
3023 }
7a9068fe
L
3024 else if (cpu_arch_isa == PROCESSOR_K1OM)
3025 {
3026 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
3027 || default_arch[6] != '\0')
3028 as_fatal (_("Intel K1OM is 64bit ELF only"));
3029 return bfd_mach_k1om;
3030 }
351f65ca 3031 else if (default_arch[6] == '\0')
8a9036a4 3032 return bfd_mach_x86_64;
351f65ca
L
3033 else
3034 return bfd_mach_x64_32;
8a9036a4 3035 }
5197d474
L
3036 else if (!strcmp (default_arch, "i386")
3037 || !strcmp (default_arch, "iamcu"))
81486035
L
3038 {
3039 if (cpu_arch_isa == PROCESSOR_IAMCU)
3040 {
3041 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
3042 as_fatal (_("Intel MCU is 32bit ELF only"));
3043 return bfd_mach_i386_iamcu;
3044 }
3045 else
3046 return bfd_mach_i386_i386;
3047 }
b9d79e03 3048 else
2b5d6a91 3049 as_fatal (_("unknown architecture"));
b9d79e03 3050}
b9d79e03 3051\f
252b5132 3052void
7016a5d5 3053md_begin (void)
252b5132 3054{
86fa6981
L
3055 /* Support pseudo prefixes like {disp32}. */
3056 lex_type ['{'] = LEX_BEGIN_NAME;
3057
47926f60 3058 /* Initialize op_hash hash table. */
629310ab 3059 op_hash = str_htab_create ();
252b5132
RH
3060
3061 {
d3ce72d0 3062 const insn_template *optab;
29b0f896 3063 templates *core_optab;
252b5132 3064
47926f60
KH
3065 /* Setup for loop. */
3066 optab = i386_optab;
add39d23 3067 core_optab = XNEW (templates);
252b5132
RH
3068 core_optab->start = optab;
3069
3070 while (1)
3071 {
3072 ++optab;
3073 if (optab->name == NULL
3074 || strcmp (optab->name, (optab - 1)->name) != 0)
3075 {
3076 /* different name --> ship out current template list;
47926f60 3077 add to hash table; & begin anew. */
252b5132 3078 core_optab->end = optab;
fe0e921f
AM
3079 if (str_hash_insert (op_hash, (optab - 1)->name, core_optab, 0))
3080 as_fatal (_("duplicate %s"), (optab - 1)->name);
3081
252b5132
RH
3082 if (optab->name == NULL)
3083 break;
add39d23 3084 core_optab = XNEW (templates);
252b5132
RH
3085 core_optab->start = optab;
3086 }
3087 }
3088 }
3089
47926f60 3090 /* Initialize reg_hash hash table. */
629310ab 3091 reg_hash = str_htab_create ();
252b5132 3092 {
29b0f896 3093 const reg_entry *regtab;
c3fe08fa 3094 unsigned int regtab_size = i386_regtab_size;
252b5132 3095
c3fe08fa 3096 for (regtab = i386_regtab; regtab_size--; regtab++)
6225c532 3097 {
6288d05f
JB
3098 switch (regtab->reg_type.bitfield.class)
3099 {
3100 case Reg:
34684862
JB
3101 if (regtab->reg_type.bitfield.dword)
3102 {
3103 if (regtab->reg_type.bitfield.instance == Accum)
3104 reg_eax = regtab;
3105 }
3106 else if (regtab->reg_type.bitfield.tbyte)
6288d05f
JB
3107 {
3108 /* There's no point inserting st(<N>) in the hash table, as
3109 parentheses aren't included in register_chars[] anyway. */
3110 if (regtab->reg_type.bitfield.instance != Accum)
3111 continue;
3112 reg_st0 = regtab;
3113 }
3114 break;
3115
5e042380
JB
3116 case SReg:
3117 switch (regtab->reg_num)
3118 {
3119 case 0: reg_es = regtab; break;
3120 case 2: reg_ss = regtab; break;
3121 case 3: reg_ds = regtab; break;
3122 }
3123 break;
3124
6288d05f
JB
3125 case RegMask:
3126 if (!regtab->reg_num)
3127 reg_k0 = regtab;
3128 break;
3129 }
3130
6225c532
JB
3131 if (str_hash_insert (reg_hash, regtab->reg_name, regtab, 0) != NULL)
3132 as_fatal (_("duplicate %s"), regtab->reg_name);
6225c532 3133 }
252b5132
RH
3134 }
3135
47926f60 3136 /* Fill in lexical tables: mnemonic_chars, operand_chars. */
252b5132 3137 {
29b0f896
AM
3138 int c;
3139 char *p;
252b5132
RH
3140
3141 for (c = 0; c < 256; c++)
3142 {
3882b010 3143 if (ISDIGIT (c))
252b5132
RH
3144 {
3145 digit_chars[c] = c;
3146 mnemonic_chars[c] = c;
3147 register_chars[c] = c;
3148 operand_chars[c] = c;
3149 }
3882b010 3150 else if (ISLOWER (c))
252b5132
RH
3151 {
3152 mnemonic_chars[c] = c;
3153 register_chars[c] = c;
3154 operand_chars[c] = c;
3155 }
3882b010 3156 else if (ISUPPER (c))
252b5132 3157 {
3882b010 3158 mnemonic_chars[c] = TOLOWER (c);
252b5132
RH
3159 register_chars[c] = mnemonic_chars[c];
3160 operand_chars[c] = c;
3161 }
43234a1e 3162 else if (c == '{' || c == '}')
86fa6981
L
3163 {
3164 mnemonic_chars[c] = c;
3165 operand_chars[c] = c;
3166 }
b3983e5f
JB
3167#ifdef SVR4_COMMENT_CHARS
3168 else if (c == '\\' && strchr (i386_comment_chars, '/'))
3169 operand_chars[c] = c;
3170#endif
252b5132 3171
3882b010 3172 if (ISALPHA (c) || ISDIGIT (c))
252b5132
RH
3173 identifier_chars[c] = c;
3174 else if (c >= 128)
3175 {
3176 identifier_chars[c] = c;
3177 operand_chars[c] = c;
3178 }
3179 }
3180
3181#ifdef LEX_AT
3182 identifier_chars['@'] = '@';
32137342
NC
3183#endif
3184#ifdef LEX_QM
3185 identifier_chars['?'] = '?';
3186 operand_chars['?'] = '?';
252b5132 3187#endif
252b5132 3188 digit_chars['-'] = '-';
c0f3af97 3189 mnemonic_chars['_'] = '_';
791fe849 3190 mnemonic_chars['-'] = '-';
0003779b 3191 mnemonic_chars['.'] = '.';
252b5132
RH
3192 identifier_chars['_'] = '_';
3193 identifier_chars['.'] = '.';
3194
3195 for (p = operand_special_chars; *p != '\0'; p++)
3196 operand_chars[(unsigned char) *p] = *p;
3197 }
3198
a4447b93
RH
3199 if (flag_code == CODE_64BIT)
3200 {
ca19b261
KT
3201#if defined (OBJ_COFF) && defined (TE_PE)
3202 x86_dwarf2_return_column = (OUTPUT_FLAVOR == bfd_target_coff_flavour
3203 ? 32 : 16);
3204#else
a4447b93 3205 x86_dwarf2_return_column = 16;
ca19b261 3206#endif
61ff971f 3207 x86_cie_data_alignment = -8;
a4447b93
RH
3208 }
3209 else
3210 {
3211 x86_dwarf2_return_column = 8;
3212 x86_cie_data_alignment = -4;
3213 }
e379e5f3
L
3214
3215 /* NB: FUSED_JCC_PADDING frag must have sufficient room so that it
3216 can be turned into BRANCH_PREFIX frag. */
3217 if (align_branch_prefix_size > MAX_FUSED_JCC_PADDING_SIZE)
3218 abort ();
252b5132
RH
3219}
3220
3221void
e3bb37b5 3222i386_print_statistics (FILE *file)
252b5132 3223{
629310ab
ML
3224 htab_print_statistics (file, "i386 opcode", op_hash);
3225 htab_print_statistics (file, "i386 register", reg_hash);
252b5132
RH
3226}
3227\f
252b5132
RH
3228#ifdef DEBUG386
3229
ce8a8b2f 3230/* Debugging routines for md_assemble. */
d3ce72d0 3231static void pte (insn_template *);
40fb9820 3232static void pt (i386_operand_type);
e3bb37b5
L
3233static void pe (expressionS *);
3234static void ps (symbolS *);
252b5132
RH
3235
3236static void
2c703856 3237pi (const char *line, i386_insn *x)
252b5132 3238{
09137c09 3239 unsigned int j;
252b5132
RH
3240
3241 fprintf (stdout, "%s: template ", line);
3242 pte (&x->tm);
09f131f2
JH
3243 fprintf (stdout, " address: base %s index %s scale %x\n",
3244 x->base_reg ? x->base_reg->reg_name : "none",
3245 x->index_reg ? x->index_reg->reg_name : "none",
3246 x->log2_scale_factor);
3247 fprintf (stdout, " modrm: mode %x reg %x reg/mem %x\n",
252b5132 3248 x->rm.mode, x->rm.reg, x->rm.regmem);
09f131f2
JH
3249 fprintf (stdout, " sib: base %x index %x scale %x\n",
3250 x->sib.base, x->sib.index, x->sib.scale);
3251 fprintf (stdout, " rex: 64bit %x extX %x extY %x extZ %x\n",
161a04f6
L
3252 (x->rex & REX_W) != 0,
3253 (x->rex & REX_R) != 0,
3254 (x->rex & REX_X) != 0,
3255 (x->rex & REX_B) != 0);
09137c09 3256 for (j = 0; j < x->operands; j++)
252b5132 3257 {
09137c09
SP
3258 fprintf (stdout, " #%d: ", j + 1);
3259 pt (x->types[j]);
252b5132 3260 fprintf (stdout, "\n");
bab6aec1 3261 if (x->types[j].bitfield.class == Reg
3528c362
JB
3262 || x->types[j].bitfield.class == RegMMX
3263 || x->types[j].bitfield.class == RegSIMD
dd6b8a0b 3264 || x->types[j].bitfield.class == RegMask
00cee14f 3265 || x->types[j].bitfield.class == SReg
4a5c67ed
JB
3266 || x->types[j].bitfield.class == RegCR
3267 || x->types[j].bitfield.class == RegDR
dd6b8a0b
JB
3268 || x->types[j].bitfield.class == RegTR
3269 || x->types[j].bitfield.class == RegBND)
09137c09
SP
3270 fprintf (stdout, "%s\n", x->op[j].regs->reg_name);
3271 if (operand_type_check (x->types[j], imm))
3272 pe (x->op[j].imms);
3273 if (operand_type_check (x->types[j], disp))
3274 pe (x->op[j].disps);
252b5132
RH
3275 }
3276}
3277
3278static void
d3ce72d0 3279pte (insn_template *t)
252b5132 3280{
b933fa4b 3281 static const unsigned char opc_pfx[] = { 0, 0x66, 0xf3, 0xf2 };
441f6aca
JB
3282 static const char *const opc_spc[] = {
3283 NULL, "0f", "0f38", "0f3a", NULL, NULL, NULL, NULL,
3284 "XOP08", "XOP09", "XOP0A",
3285 };
09137c09 3286 unsigned int j;
441f6aca 3287
252b5132 3288 fprintf (stdout, " %d operands ", t->operands);
441f6aca
JB
3289 if (opc_pfx[t->opcode_modifier.opcodeprefix])
3290 fprintf (stdout, "pfx %x ", opc_pfx[t->opcode_modifier.opcodeprefix]);
3291 if (opc_spc[t->opcode_modifier.opcodespace])
3292 fprintf (stdout, "space %s ", opc_spc[t->opcode_modifier.opcodespace]);
47926f60 3293 fprintf (stdout, "opcode %x ", t->base_opcode);
252b5132
RH
3294 if (t->extension_opcode != None)
3295 fprintf (stdout, "ext %x ", t->extension_opcode);
40fb9820 3296 if (t->opcode_modifier.d)
252b5132 3297 fprintf (stdout, "D");
40fb9820 3298 if (t->opcode_modifier.w)
252b5132
RH
3299 fprintf (stdout, "W");
3300 fprintf (stdout, "\n");
09137c09 3301 for (j = 0; j < t->operands; j++)
252b5132 3302 {
09137c09
SP
3303 fprintf (stdout, " #%d type ", j + 1);
3304 pt (t->operand_types[j]);
252b5132
RH
3305 fprintf (stdout, "\n");
3306 }
3307}
3308
3309static void
e3bb37b5 3310pe (expressionS *e)
252b5132 3311{
24eab124 3312 fprintf (stdout, " operation %d\n", e->X_op);
7b025ee8
JB
3313 fprintf (stdout, " add_number %" BFD_VMA_FMT "d (%" BFD_VMA_FMT "x)\n",
3314 e->X_add_number, e->X_add_number);
252b5132
RH
3315 if (e->X_add_symbol)
3316 {
3317 fprintf (stdout, " add_symbol ");
3318 ps (e->X_add_symbol);
3319 fprintf (stdout, "\n");
3320 }
3321 if (e->X_op_symbol)
3322 {
3323 fprintf (stdout, " op_symbol ");
3324 ps (e->X_op_symbol);
3325 fprintf (stdout, "\n");
3326 }
3327}
3328
3329static void
e3bb37b5 3330ps (symbolS *s)
252b5132
RH
3331{
3332 fprintf (stdout, "%s type %s%s",
3333 S_GET_NAME (s),
3334 S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
3335 segment_name (S_GET_SEGMENT (s)));
3336}
3337
7b81dfbb 3338static struct type_name
252b5132 3339 {
40fb9820
L
3340 i386_operand_type mask;
3341 const char *name;
252b5132 3342 }
7b81dfbb 3343const type_names[] =
252b5132 3344{
40fb9820
L
3345 { OPERAND_TYPE_REG8, "r8" },
3346 { OPERAND_TYPE_REG16, "r16" },
3347 { OPERAND_TYPE_REG32, "r32" },
3348 { OPERAND_TYPE_REG64, "r64" },
2c703856
JB
3349 { OPERAND_TYPE_ACC8, "acc8" },
3350 { OPERAND_TYPE_ACC16, "acc16" },
3351 { OPERAND_TYPE_ACC32, "acc32" },
3352 { OPERAND_TYPE_ACC64, "acc64" },
40fb9820
L
3353 { OPERAND_TYPE_IMM8, "i8" },
3354 { OPERAND_TYPE_IMM8, "i8s" },
3355 { OPERAND_TYPE_IMM16, "i16" },
3356 { OPERAND_TYPE_IMM32, "i32" },
3357 { OPERAND_TYPE_IMM32S, "i32s" },
3358 { OPERAND_TYPE_IMM64, "i64" },
3359 { OPERAND_TYPE_IMM1, "i1" },
3360 { OPERAND_TYPE_BASEINDEX, "BaseIndex" },
3361 { OPERAND_TYPE_DISP8, "d8" },
3362 { OPERAND_TYPE_DISP16, "d16" },
3363 { OPERAND_TYPE_DISP32, "d32" },
3364 { OPERAND_TYPE_DISP32S, "d32s" },
3365 { OPERAND_TYPE_DISP64, "d64" },
3366 { OPERAND_TYPE_INOUTPORTREG, "InOutPortReg" },
3367 { OPERAND_TYPE_SHIFTCOUNT, "ShiftCount" },
3368 { OPERAND_TYPE_CONTROL, "control reg" },
3369 { OPERAND_TYPE_TEST, "test reg" },
3370 { OPERAND_TYPE_DEBUG, "debug reg" },
3371 { OPERAND_TYPE_FLOATREG, "FReg" },
3372 { OPERAND_TYPE_FLOATACC, "FAcc" },
21df382b 3373 { OPERAND_TYPE_SREG, "SReg" },
40fb9820
L
3374 { OPERAND_TYPE_REGMMX, "rMMX" },
3375 { OPERAND_TYPE_REGXMM, "rXMM" },
0349dc08 3376 { OPERAND_TYPE_REGYMM, "rYMM" },
43234a1e 3377 { OPERAND_TYPE_REGZMM, "rZMM" },
260cd341 3378 { OPERAND_TYPE_REGTMM, "rTMM" },
43234a1e 3379 { OPERAND_TYPE_REGMASK, "Mask reg" },
252b5132
RH
3380};
3381
3382static void
40fb9820 3383pt (i386_operand_type t)
252b5132 3384{
40fb9820 3385 unsigned int j;
c6fb90c8 3386 i386_operand_type a;
252b5132 3387
40fb9820 3388 for (j = 0; j < ARRAY_SIZE (type_names); j++)
c6fb90c8
L
3389 {
3390 a = operand_type_and (t, type_names[j].mask);
2c703856 3391 if (operand_type_equal (&a, &type_names[j].mask))
c6fb90c8
L
3392 fprintf (stdout, "%s, ", type_names[j].name);
3393 }
252b5132
RH
3394 fflush (stdout);
3395}
3396
3397#endif /* DEBUG386 */
3398\f
252b5132 3399static bfd_reloc_code_real_type
3956db08 3400reloc (unsigned int size,
64e74474
AM
3401 int pcrel,
3402 int sign,
3403 bfd_reloc_code_real_type other)
252b5132 3404{
47926f60 3405 if (other != NO_RELOC)
3956db08 3406 {
91d6fa6a 3407 reloc_howto_type *rel;
3956db08
JB
3408
3409 if (size == 8)
3410 switch (other)
3411 {
64e74474
AM
3412 case BFD_RELOC_X86_64_GOT32:
3413 return BFD_RELOC_X86_64_GOT64;
3414 break;
553d1284
L
3415 case BFD_RELOC_X86_64_GOTPLT64:
3416 return BFD_RELOC_X86_64_GOTPLT64;
3417 break;
64e74474
AM
3418 case BFD_RELOC_X86_64_PLTOFF64:
3419 return BFD_RELOC_X86_64_PLTOFF64;
3420 break;
3421 case BFD_RELOC_X86_64_GOTPC32:
3422 other = BFD_RELOC_X86_64_GOTPC64;
3423 break;
3424 case BFD_RELOC_X86_64_GOTPCREL:
3425 other = BFD_RELOC_X86_64_GOTPCREL64;
3426 break;
3427 case BFD_RELOC_X86_64_TPOFF32:
3428 other = BFD_RELOC_X86_64_TPOFF64;
3429 break;
3430 case BFD_RELOC_X86_64_DTPOFF32:
3431 other = BFD_RELOC_X86_64_DTPOFF64;
3432 break;
3433 default:
3434 break;
3956db08 3435 }
e05278af 3436
8ce3d284 3437#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
3438 if (other == BFD_RELOC_SIZE32)
3439 {
3440 if (size == 8)
1ab668bf 3441 other = BFD_RELOC_SIZE64;
8fd4256d 3442 if (pcrel)
1ab668bf
AM
3443 {
3444 as_bad (_("there are no pc-relative size relocations"));
3445 return NO_RELOC;
3446 }
8fd4256d 3447 }
8ce3d284 3448#endif
8fd4256d 3449
e05278af 3450 /* Sign-checking 4-byte relocations in 16-/32-bit code is pointless. */
f2d8a97c 3451 if (size == 4 && (flag_code != CODE_64BIT || disallow_64bit_reloc))
e05278af
JB
3452 sign = -1;
3453
91d6fa6a
NC
3454 rel = bfd_reloc_type_lookup (stdoutput, other);
3455 if (!rel)
3956db08 3456 as_bad (_("unknown relocation (%u)"), other);
91d6fa6a 3457 else if (size != bfd_get_reloc_size (rel))
3956db08 3458 as_bad (_("%u-byte relocation cannot be applied to %u-byte field"),
91d6fa6a 3459 bfd_get_reloc_size (rel),
3956db08 3460 size);
91d6fa6a 3461 else if (pcrel && !rel->pc_relative)
3956db08 3462 as_bad (_("non-pc-relative relocation for pc-relative field"));
91d6fa6a 3463 else if ((rel->complain_on_overflow == complain_overflow_signed
3956db08 3464 && !sign)
91d6fa6a 3465 || (rel->complain_on_overflow == complain_overflow_unsigned
64e74474 3466 && sign > 0))
3956db08
JB
3467 as_bad (_("relocated field and relocation type differ in signedness"));
3468 else
3469 return other;
3470 return NO_RELOC;
3471 }
252b5132
RH
3472
3473 if (pcrel)
3474 {
3e73aa7c 3475 if (!sign)
3956db08 3476 as_bad (_("there are no unsigned pc-relative relocations"));
252b5132
RH
3477 switch (size)
3478 {
3479 case 1: return BFD_RELOC_8_PCREL;
3480 case 2: return BFD_RELOC_16_PCREL;
d258b828 3481 case 4: return BFD_RELOC_32_PCREL;
d6ab8113 3482 case 8: return BFD_RELOC_64_PCREL;
252b5132 3483 }
3956db08 3484 as_bad (_("cannot do %u byte pc-relative relocation"), size);
252b5132
RH
3485 }
3486 else
3487 {
3956db08 3488 if (sign > 0)
e5cb08ac 3489 switch (size)
3e73aa7c
JH
3490 {
3491 case 4: return BFD_RELOC_X86_64_32S;
3492 }
3493 else
3494 switch (size)
3495 {
3496 case 1: return BFD_RELOC_8;
3497 case 2: return BFD_RELOC_16;
3498 case 4: return BFD_RELOC_32;
3499 case 8: return BFD_RELOC_64;
3500 }
3956db08
JB
3501 as_bad (_("cannot do %s %u byte relocation"),
3502 sign > 0 ? "signed" : "unsigned", size);
252b5132
RH
3503 }
3504
0cc9e1d3 3505 return NO_RELOC;
252b5132
RH
3506}
3507
47926f60
KH
3508/* Here we decide which fixups can be adjusted to make them relative to
3509 the beginning of the section instead of the symbol. Basically we need
3510 to make sure that the dynamic relocations are done correctly, so in
3511 some cases we force the original symbol to be used. */
3512
252b5132 3513int
e3bb37b5 3514tc_i386_fix_adjustable (fixS *fixP ATTRIBUTE_UNUSED)
252b5132 3515{
6d249963 3516#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 3517 if (!IS_ELF)
31312f95
AM
3518 return 1;
3519
a161fe53
AM
3520 /* Don't adjust pc-relative references to merge sections in 64-bit
3521 mode. */
3522 if (use_rela_relocations
3523 && (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0
3524 && fixP->fx_pcrel)
252b5132 3525 return 0;
31312f95 3526
8d01d9a9
AJ
3527 /* The x86_64 GOTPCREL are represented as 32bit PCrel relocations
3528 and changed later by validate_fix. */
3529 if (GOT_symbol && fixP->fx_subsy == GOT_symbol
3530 && fixP->fx_r_type == BFD_RELOC_32_PCREL)
3531 return 0;
3532
8fd4256d
L
3533 /* Adjust_reloc_syms doesn't know about the GOT. Need to keep symbol
3534 for size relocations. */
3535 if (fixP->fx_r_type == BFD_RELOC_SIZE32
3536 || fixP->fx_r_type == BFD_RELOC_SIZE64
3537 || fixP->fx_r_type == BFD_RELOC_386_GOTOFF
252b5132 3538 || fixP->fx_r_type == BFD_RELOC_386_GOT32
02a86693 3539 || fixP->fx_r_type == BFD_RELOC_386_GOT32X
13ae64f3
JJ
3540 || fixP->fx_r_type == BFD_RELOC_386_TLS_GD
3541 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDM
3542 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDO_32
3543 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE_32
37e55690
JJ
3544 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE
3545 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTIE
13ae64f3
JJ
3546 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE_32
3547 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE
67a4f2b7
AO
3548 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTDESC
3549 || fixP->fx_r_type == BFD_RELOC_386_TLS_DESC_CALL
3e73aa7c 3550 || fixP->fx_r_type == BFD_RELOC_X86_64_GOT32
80b3ee89 3551 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCREL
56ceb5b5
L
3552 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCRELX
3553 || fixP->fx_r_type == BFD_RELOC_X86_64_REX_GOTPCRELX
bffbf940
JJ
3554 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSGD
3555 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSLD
3556 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF32
d6ab8113 3557 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF64
bffbf940
JJ
3558 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF
3559 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF32
d6ab8113
JB
3560 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF64
3561 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTOFF64
67a4f2b7
AO
3562 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
3563 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSDESC_CALL
252b5132
RH
3564 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3565 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3566 return 0;
31312f95 3567#endif
252b5132
RH
3568 return 1;
3569}
252b5132 3570
a9aabc23
JB
3571static INLINE bool
3572want_disp32 (const insn_template *t)
3573{
3574 return flag_code != CODE_64BIT
3575 || i.prefix[ADDR_PREFIX]
3576 || (t->base_opcode == 0x8d
3577 && t->opcode_modifier.opcodespace == SPACE_BASE
fe134c65
JB
3578 && (!i.types[1].bitfield.qword
3579 || t->opcode_modifier.size == SIZE32));
a9aabc23
JB
3580}
3581
b4cac588 3582static int
e3bb37b5 3583intel_float_operand (const char *mnemonic)
252b5132 3584{
9306ca4a
JB
3585 /* Note that the value returned is meaningful only for opcodes with (memory)
3586 operands, hence the code here is free to improperly handle opcodes that
3587 have no operands (for better performance and smaller code). */
3588
3589 if (mnemonic[0] != 'f')
3590 return 0; /* non-math */
3591
3592 switch (mnemonic[1])
3593 {
3594 /* fclex, fdecstp, fdisi, femms, feni, fincstp, finit, fsetpm, and
3595 the fs segment override prefix not currently handled because no
3596 call path can make opcodes without operands get here */
3597 case 'i':
3598 return 2 /* integer op */;
3599 case 'l':
3600 if (mnemonic[2] == 'd' && (mnemonic[3] == 'c' || mnemonic[3] == 'e'))
3601 return 3; /* fldcw/fldenv */
3602 break;
3603 case 'n':
3604 if (mnemonic[2] != 'o' /* fnop */)
3605 return 3; /* non-waiting control op */
3606 break;
3607 case 'r':
3608 if (mnemonic[2] == 's')
3609 return 3; /* frstor/frstpm */
3610 break;
3611 case 's':
3612 if (mnemonic[2] == 'a')
3613 return 3; /* fsave */
3614 if (mnemonic[2] == 't')
3615 {
3616 switch (mnemonic[3])
3617 {
3618 case 'c': /* fstcw */
3619 case 'd': /* fstdw */
3620 case 'e': /* fstenv */
3621 case 's': /* fsts[gw] */
3622 return 3;
3623 }
3624 }
3625 break;
3626 case 'x':
3627 if (mnemonic[2] == 'r' || mnemonic[2] == 's')
3628 return 0; /* fxsave/fxrstor are not really math ops */
3629 break;
3630 }
252b5132 3631
9306ca4a 3632 return 1;
252b5132
RH
3633}
3634
9a182d04
JB
3635static INLINE void
3636install_template (const insn_template *t)
3637{
3638 unsigned int l;
3639
3640 i.tm = *t;
3641
3642 /* Note that for pseudo prefixes this produces a length of 1. But for them
3643 the length isn't interesting at all. */
3644 for (l = 1; l < 4; ++l)
3645 if (!(t->base_opcode >> (8 * l)))
3646 break;
3647
3648 i.opcode_length = l;
3649}
3650
c0f3af97
L
3651/* Build the VEX prefix. */
3652
3653static void
d3ce72d0 3654build_vex_prefix (const insn_template *t)
c0f3af97
L
3655{
3656 unsigned int register_specifier;
c0f3af97 3657 unsigned int vector_length;
03751133 3658 unsigned int w;
c0f3af97
L
3659
3660 /* Check register specifier. */
3661 if (i.vex.register_specifier)
43234a1e
L
3662 {
3663 register_specifier =
3664 ~register_number (i.vex.register_specifier) & 0xf;
3665 gas_assert ((i.vex.register_specifier->reg_flags & RegVRex) == 0);
3666 }
c0f3af97
L
3667 else
3668 register_specifier = 0xf;
3669
79f0fa25
L
3670 /* Use 2-byte VEX prefix by swapping destination and source operand
3671 if there are more than 1 register operand. */
3672 if (i.reg_operands > 1
3673 && i.vec_encoding != vex_encoding_vex3
86fa6981 3674 && i.dir_encoding == dir_encoding_default
fa99fab2 3675 && i.operands == i.reg_operands
dbbc8b7e 3676 && operand_type_equal (&i.types[0], &i.types[i.operands - 1])
441f6aca 3677 && i.tm.opcode_modifier.opcodespace == SPACE_0F
dbbc8b7e 3678 && (i.tm.opcode_modifier.load || i.tm.opcode_modifier.d)
fa99fab2
L
3679 && i.rex == REX_B)
3680 {
3681 unsigned int xchg = i.operands - 1;
3682 union i386_op temp_op;
3683 i386_operand_type temp_type;
3684
3685 temp_type = i.types[xchg];
3686 i.types[xchg] = i.types[0];
3687 i.types[0] = temp_type;
3688 temp_op = i.op[xchg];
3689 i.op[xchg] = i.op[0];
3690 i.op[0] = temp_op;
3691
9c2799c2 3692 gas_assert (i.rm.mode == 3);
fa99fab2
L
3693
3694 i.rex = REX_R;
3695 xchg = i.rm.regmem;
3696 i.rm.regmem = i.rm.reg;
3697 i.rm.reg = xchg;
3698
dbbc8b7e
JB
3699 if (i.tm.opcode_modifier.d)
3700 i.tm.base_opcode ^= (i.tm.base_opcode & 0xee) != 0x6e
3701 ? Opcode_SIMD_FloatD : Opcode_SIMD_IntD;
3702 else /* Use the next insn. */
9a182d04 3703 install_template (&t[1]);
fa99fab2
L
3704 }
3705
79dec6b7
JB
3706 /* Use 2-byte VEX prefix by swapping commutative source operands if there
3707 are no memory operands and at least 3 register ones. */
3708 if (i.reg_operands >= 3
3709 && i.vec_encoding != vex_encoding_vex3
3710 && i.reg_operands == i.operands - i.imm_operands
3711 && i.tm.opcode_modifier.vex
3712 && i.tm.opcode_modifier.commutative
3713 && (i.tm.opcode_modifier.sse2avx || optimize > 1)
3714 && i.rex == REX_B
3715 && i.vex.register_specifier
3716 && !(i.vex.register_specifier->reg_flags & RegRex))
3717 {
3718 unsigned int xchg = i.operands - i.reg_operands;
3719 union i386_op temp_op;
3720 i386_operand_type temp_type;
3721
441f6aca 3722 gas_assert (i.tm.opcode_modifier.opcodespace == SPACE_0F);
79dec6b7
JB
3723 gas_assert (!i.tm.opcode_modifier.sae);
3724 gas_assert (operand_type_equal (&i.types[i.operands - 2],
3725 &i.types[i.operands - 3]));
3726 gas_assert (i.rm.mode == 3);
3727
3728 temp_type = i.types[xchg];
3729 i.types[xchg] = i.types[xchg + 1];
3730 i.types[xchg + 1] = temp_type;
3731 temp_op = i.op[xchg];
3732 i.op[xchg] = i.op[xchg + 1];
3733 i.op[xchg + 1] = temp_op;
3734
3735 i.rex = 0;
3736 xchg = i.rm.regmem | 8;
3737 i.rm.regmem = ~register_specifier & 0xf;
3738 gas_assert (!(i.rm.regmem & 8));
3739 i.vex.register_specifier += xchg - i.rm.regmem;
3740 register_specifier = ~xchg & 0xf;
3741 }
3742
539f890d
L
3743 if (i.tm.opcode_modifier.vex == VEXScalar)
3744 vector_length = avxscalar;
10c17abd
JB
3745 else if (i.tm.opcode_modifier.vex == VEX256)
3746 vector_length = 1;
539f890d 3747 else
10c17abd 3748 {
56522fc5 3749 unsigned int op;
10c17abd 3750
c7213af9
L
3751 /* Determine vector length from the last multi-length vector
3752 operand. */
10c17abd 3753 vector_length = 0;
56522fc5 3754 for (op = t->operands; op--;)
10c17abd
JB
3755 if (t->operand_types[op].bitfield.xmmword
3756 && t->operand_types[op].bitfield.ymmword
3757 && i.types[op].bitfield.ymmword)
3758 {
3759 vector_length = 1;
3760 break;
3761 }
3762 }
c0f3af97 3763
03751133
L
3764 /* Check the REX.W bit and VEXW. */
3765 if (i.tm.opcode_modifier.vexw == VEXWIG)
3766 w = (vexwig == vexw1 || (i.rex & REX_W)) ? 1 : 0;
3767 else if (i.tm.opcode_modifier.vexw)
3768 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3769 else
931d03b7 3770 w = (flag_code == CODE_64BIT ? i.rex & REX_W : vexwig == vexw1) ? 1 : 0;
03751133 3771
c0f3af97 3772 /* Use 2-byte VEX prefix if possible. */
03751133
L
3773 if (w == 0
3774 && i.vec_encoding != vex_encoding_vex3
441f6aca 3775 && i.tm.opcode_modifier.opcodespace == SPACE_0F
c0f3af97
L
3776 && (i.rex & (REX_W | REX_X | REX_B)) == 0)
3777 {
3778 /* 2-byte VEX prefix. */
3779 unsigned int r;
3780
3781 i.vex.length = 2;
3782 i.vex.bytes[0] = 0xc5;
3783
3784 /* Check the REX.R bit. */
3785 r = (i.rex & REX_R) ? 0 : 1;
3786 i.vex.bytes[1] = (r << 7
3787 | register_specifier << 3
3788 | vector_length << 2
35648716 3789 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3790 }
3791 else
3792 {
3793 /* 3-byte VEX prefix. */
f88c9eb0 3794 i.vex.length = 3;
f88c9eb0 3795
441f6aca 3796 switch (i.tm.opcode_modifier.opcodespace)
5dd85c99 3797 {
441f6aca
JB
3798 case SPACE_0F:
3799 case SPACE_0F38:
3800 case SPACE_0F3A:
80de6e00 3801 i.vex.bytes[0] = 0xc4;
7f399153 3802 break;
441f6aca
JB
3803 case SPACE_XOP08:
3804 case SPACE_XOP09:
3805 case SPACE_XOP0A:
f88c9eb0 3806 i.vex.bytes[0] = 0x8f;
7f399153
L
3807 break;
3808 default:
3809 abort ();
f88c9eb0 3810 }
c0f3af97 3811
c0f3af97
L
3812 /* The high 3 bits of the second VEX byte are 1's compliment
3813 of RXB bits from REX. */
441f6aca 3814 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | i.tm.opcode_modifier.opcodespace;
c0f3af97 3815
c0f3af97
L
3816 i.vex.bytes[2] = (w << 7
3817 | register_specifier << 3
3818 | vector_length << 2
35648716 3819 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3820 }
3821}
3822
5b7c81bd 3823static INLINE bool
e771e7c9
JB
3824is_evex_encoding (const insn_template *t)
3825{
7091c612 3826 return t->opcode_modifier.evex || t->opcode_modifier.disp8memshift
e771e7c9 3827 || t->opcode_modifier.broadcast || t->opcode_modifier.masking
a80195f1 3828 || t->opcode_modifier.sae;
e771e7c9
JB
3829}
3830
5b7c81bd 3831static INLINE bool
7a8655d2
JB
3832is_any_vex_encoding (const insn_template *t)
3833{
7b47a312 3834 return t->opcode_modifier.vex || is_evex_encoding (t);
7a8655d2
JB
3835}
3836
43234a1e
L
3837/* Build the EVEX prefix. */
3838
3839static void
3840build_evex_prefix (void)
3841{
35648716 3842 unsigned int register_specifier, w;
43234a1e
L
3843 rex_byte vrex_used = 0;
3844
3845 /* Check register specifier. */
3846 if (i.vex.register_specifier)
3847 {
3848 gas_assert ((i.vrex & REX_X) == 0);
3849
3850 register_specifier = i.vex.register_specifier->reg_num;
3851 if ((i.vex.register_specifier->reg_flags & RegRex))
3852 register_specifier += 8;
3853 /* The upper 16 registers are encoded in the fourth byte of the
3854 EVEX prefix. */
3855 if (!(i.vex.register_specifier->reg_flags & RegVRex))
3856 i.vex.bytes[3] = 0x8;
3857 register_specifier = ~register_specifier & 0xf;
3858 }
3859 else
3860 {
3861 register_specifier = 0xf;
3862
3863 /* Encode upper 16 vector index register in the fourth byte of
3864 the EVEX prefix. */
3865 if (!(i.vrex & REX_X))
3866 i.vex.bytes[3] = 0x8;
3867 else
3868 vrex_used |= REX_X;
3869 }
3870
43234a1e
L
3871 /* 4 byte EVEX prefix. */
3872 i.vex.length = 4;
3873 i.vex.bytes[0] = 0x62;
3874
43234a1e
L
3875 /* The high 3 bits of the second EVEX byte are 1's compliment of RXB
3876 bits from REX. */
441f6aca
JB
3877 gas_assert (i.tm.opcode_modifier.opcodespace >= SPACE_0F);
3878 gas_assert (i.tm.opcode_modifier.opcodespace <= SPACE_0F3A);
3879 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | i.tm.opcode_modifier.opcodespace;
43234a1e
L
3880
3881 /* The fifth bit of the second EVEX byte is 1's compliment of the
3882 REX_R bit in VREX. */
3883 if (!(i.vrex & REX_R))
3884 i.vex.bytes[1] |= 0x10;
3885 else
3886 vrex_used |= REX_R;
3887
3888 if ((i.reg_operands + i.imm_operands) == i.operands)
3889 {
3890 /* When all operands are registers, the REX_X bit in REX is not
3891 used. We reuse it to encode the upper 16 registers, which is
3892 indicated by the REX_B bit in VREX. The REX_X bit is encoded
3893 as 1's compliment. */
3894 if ((i.vrex & REX_B))
3895 {
3896 vrex_used |= REX_B;
3897 i.vex.bytes[1] &= ~0x40;
3898 }
3899 }
3900
3901 /* EVEX instructions shouldn't need the REX prefix. */
3902 i.vrex &= ~vrex_used;
3903 gas_assert (i.vrex == 0);
3904
6865c043
L
3905 /* Check the REX.W bit and VEXW. */
3906 if (i.tm.opcode_modifier.vexw == VEXWIG)
3907 w = (evexwig == evexw1 || (i.rex & REX_W)) ? 1 : 0;
3908 else if (i.tm.opcode_modifier.vexw)
3909 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3910 else
931d03b7 3911 w = (flag_code == CODE_64BIT ? i.rex & REX_W : evexwig == evexw1) ? 1 : 0;
43234a1e 3912
43234a1e 3913 /* The third byte of the EVEX prefix. */
35648716
JB
3914 i.vex.bytes[2] = ((w << 7)
3915 | (register_specifier << 3)
3916 | 4 /* Encode the U bit. */
3917 | i.tm.opcode_modifier.opcodeprefix);
43234a1e
L
3918
3919 /* The fourth byte of the EVEX prefix. */
3920 /* The zeroing-masking bit. */
6225c532 3921 if (i.mask.reg && i.mask.zeroing)
43234a1e
L
3922 i.vex.bytes[3] |= 0x80;
3923
3924 /* Don't always set the broadcast bit if there is no RC. */
ca5312a2 3925 if (i.rounding.type == rc_none)
43234a1e
L
3926 {
3927 /* Encode the vector length. */
3928 unsigned int vec_length;
3929
e771e7c9
JB
3930 if (!i.tm.opcode_modifier.evex
3931 || i.tm.opcode_modifier.evex == EVEXDYN)
3932 {
56522fc5 3933 unsigned int op;
e771e7c9 3934
c7213af9
L
3935 /* Determine vector length from the last multi-length vector
3936 operand. */
56522fc5 3937 for (op = i.operands; op--;)
e771e7c9
JB
3938 if (i.tm.operand_types[op].bitfield.xmmword
3939 + i.tm.operand_types[op].bitfield.ymmword
3940 + i.tm.operand_types[op].bitfield.zmmword > 1)
3941 {
3942 if (i.types[op].bitfield.zmmword)
c7213af9
L
3943 {
3944 i.tm.opcode_modifier.evex = EVEX512;
3945 break;
3946 }
e771e7c9 3947 else if (i.types[op].bitfield.ymmword)
c7213af9
L
3948 {
3949 i.tm.opcode_modifier.evex = EVEX256;
3950 break;
3951 }
e771e7c9 3952 else if (i.types[op].bitfield.xmmword)
c7213af9
L
3953 {
3954 i.tm.opcode_modifier.evex = EVEX128;
3955 break;
3956 }
5273a3cd 3957 else if (i.broadcast.type && op == i.broadcast.operand)
625cbd7a 3958 {
5273a3cd 3959 switch (i.broadcast.bytes)
625cbd7a
JB
3960 {
3961 case 64:
3962 i.tm.opcode_modifier.evex = EVEX512;
3963 break;
3964 case 32:
3965 i.tm.opcode_modifier.evex = EVEX256;
3966 break;
3967 case 16:
3968 i.tm.opcode_modifier.evex = EVEX128;
3969 break;
3970 default:
c7213af9 3971 abort ();
625cbd7a 3972 }
c7213af9 3973 break;
625cbd7a 3974 }
e771e7c9 3975 }
c7213af9 3976
56522fc5 3977 if (op >= MAX_OPERANDS)
c7213af9 3978 abort ();
e771e7c9
JB
3979 }
3980
43234a1e
L
3981 switch (i.tm.opcode_modifier.evex)
3982 {
3983 case EVEXLIG: /* LL' is ignored */
3984 vec_length = evexlig << 5;
3985 break;
3986 case EVEX128:
3987 vec_length = 0 << 5;
3988 break;
3989 case EVEX256:
3990 vec_length = 1 << 5;
3991 break;
3992 case EVEX512:
3993 vec_length = 2 << 5;
3994 break;
3995 default:
3996 abort ();
3997 break;
3998 }
3999 i.vex.bytes[3] |= vec_length;
4000 /* Encode the broadcast bit. */
5273a3cd 4001 if (i.broadcast.type)
43234a1e
L
4002 i.vex.bytes[3] |= 0x10;
4003 }
ca5312a2
JB
4004 else if (i.rounding.type != saeonly)
4005 i.vex.bytes[3] |= 0x10 | (i.rounding.type << 5);
43234a1e 4006 else
ca5312a2 4007 i.vex.bytes[3] |= 0x10 | (evexrcig << 5);
43234a1e 4008
6225c532
JB
4009 if (i.mask.reg)
4010 i.vex.bytes[3] |= i.mask.reg->reg_num;
43234a1e
L
4011}
4012
65da13b5
L
4013static void
4014process_immext (void)
4015{
4016 expressionS *exp;
4017
c0f3af97 4018 /* These AMD 3DNow! and SSE2 instructions have an opcode suffix
65da13b5
L
4019 which is coded in the same place as an 8-bit immediate field
4020 would be. Here we fake an 8-bit immediate operand from the
4021 opcode suffix stored in tm.extension_opcode.
4022
c1e679ec 4023 AVX instructions also use this encoding, for some of
c0f3af97 4024 3 argument instructions. */
65da13b5 4025
43234a1e 4026 gas_assert (i.imm_operands <= 1
7ab9ffdd 4027 && (i.operands <= 2
7a8655d2 4028 || (is_any_vex_encoding (&i.tm)
7ab9ffdd 4029 && i.operands <= 4)));
65da13b5
L
4030
4031 exp = &im_expressions[i.imm_operands++];
4032 i.op[i.operands].imms = exp;
4033 i.types[i.operands] = imm8;
4034 i.operands++;
4035 exp->X_op = O_constant;
4036 exp->X_add_number = i.tm.extension_opcode;
4037 i.tm.extension_opcode = None;
4038}
4039
42164a71
L
4040
4041static int
4042check_hle (void)
4043{
742732c7 4044 switch (i.tm.opcode_modifier.prefixok)
42164a71
L
4045 {
4046 default:
4047 abort ();
742732c7
JB
4048 case PrefixLock:
4049 case PrefixNone:
4050 case PrefixNoTrack:
4051 case PrefixRep:
165de32a
L
4052 as_bad (_("invalid instruction `%s' after `%s'"),
4053 i.tm.name, i.hle_prefix);
42164a71 4054 return 0;
742732c7 4055 case PrefixHLELock:
42164a71
L
4056 if (i.prefix[LOCK_PREFIX])
4057 return 1;
165de32a 4058 as_bad (_("missing `lock' with `%s'"), i.hle_prefix);
42164a71 4059 return 0;
742732c7 4060 case PrefixHLEAny:
42164a71 4061 return 1;
742732c7 4062 case PrefixHLERelease:
42164a71
L
4063 if (i.prefix[HLE_PREFIX] != XRELEASE_PREFIX_OPCODE)
4064 {
4065 as_bad (_("instruction `%s' after `xacquire' not allowed"),
4066 i.tm.name);
4067 return 0;
4068 }
8dc0818e 4069 if (i.mem_operands == 0 || !(i.flags[i.operands - 1] & Operand_Mem))
42164a71
L
4070 {
4071 as_bad (_("memory destination needed for instruction `%s'"
4072 " after `xrelease'"), i.tm.name);
4073 return 0;
4074 }
4075 return 1;
4076 }
4077}
4078
b6f8c7c4
L
4079/* Try the shortest encoding by shortening operand size. */
4080
4081static void
4082optimize_encoding (void)
4083{
a0a1771e 4084 unsigned int j;
b6f8c7c4 4085
fe134c65
JB
4086 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
4087 && i.tm.base_opcode == 0x8d)
4088 {
4089 /* Optimize: -O:
4090 lea symbol, %rN -> mov $symbol, %rN
4091 lea (%rM), %rN -> mov %rM, %rN
4092 lea (,%rM,1), %rN -> mov %rM, %rN
4093
4094 and in 32-bit mode for 16-bit addressing
4095
4096 lea (%rM), %rN -> movzx %rM, %rN
4097
4098 and in 64-bit mode zap 32-bit addressing in favor of using a
4099 32-bit (or less) destination.
4100 */
4101 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
4102 {
4103 if (!i.op[1].regs->reg_type.bitfield.word)
4104 i.tm.opcode_modifier.size = SIZE32;
4105 i.prefix[ADDR_PREFIX] = 0;
4106 }
4107
4108 if (!i.index_reg && !i.base_reg)
4109 {
4110 /* Handle:
4111 lea symbol, %rN -> mov $symbol, %rN
4112 */
4113 if (flag_code == CODE_64BIT)
4114 {
4115 /* Don't transform a relocation to a 16-bit one. */
4116 if (i.op[0].disps
4117 && i.op[0].disps->X_op != O_constant
4118 && i.op[1].regs->reg_type.bitfield.word)
4119 return;
4120
4121 if (!i.op[1].regs->reg_type.bitfield.qword
4122 || i.tm.opcode_modifier.size == SIZE32)
4123 {
4124 i.tm.base_opcode = 0xb8;
4125 i.tm.opcode_modifier.modrm = 0;
4126 if (!i.op[1].regs->reg_type.bitfield.word)
4127 i.types[0].bitfield.imm32 = 1;
4128 else
4129 {
4130 i.tm.opcode_modifier.size = SIZE16;
4131 i.types[0].bitfield.imm16 = 1;
4132 }
4133 }
4134 else
4135 {
4136 /* Subject to further optimization below. */
4137 i.tm.base_opcode = 0xc7;
4138 i.tm.extension_opcode = 0;
4139 i.types[0].bitfield.imm32s = 1;
4140 i.types[0].bitfield.baseindex = 0;
4141 }
4142 }
4143 /* Outside of 64-bit mode address and operand sizes have to match if
4144 a relocation is involved, as otherwise we wouldn't (currently) or
4145 even couldn't express the relocation correctly. */
4146 else if (i.op[0].disps
4147 && i.op[0].disps->X_op != O_constant
4148 && ((!i.prefix[ADDR_PREFIX])
4149 != (flag_code == CODE_32BIT
4150 ? i.op[1].regs->reg_type.bitfield.dword
4151 : i.op[1].regs->reg_type.bitfield.word)))
4152 return;
4153 else
4154 {
4155 i.tm.base_opcode = 0xb8;
4156 i.tm.opcode_modifier.modrm = 0;
4157 if (i.op[1].regs->reg_type.bitfield.dword)
4158 i.types[0].bitfield.imm32 = 1;
4159 else
4160 i.types[0].bitfield.imm16 = 1;
4161
4162 if (i.op[0].disps
4163 && i.op[0].disps->X_op == O_constant
4164 && i.op[1].regs->reg_type.bitfield.dword
60cfa10c
L
4165 /* NB: Add () to !i.prefix[ADDR_PREFIX] to silence
4166 GCC 5. */
4167 && (!i.prefix[ADDR_PREFIX]) != (flag_code == CODE_32BIT))
fe134c65
JB
4168 i.op[0].disps->X_add_number &= 0xffff;
4169 }
4170
4171 i.tm.operand_types[0] = i.types[0];
4172 i.imm_operands = 1;
4173 if (!i.op[0].imms)
4174 {
4175 i.op[0].imms = &im_expressions[0];
4176 i.op[0].imms->X_op = O_absent;
4177 }
4178 }
4179 else if (i.op[0].disps
4180 && (i.op[0].disps->X_op != O_constant
4181 || i.op[0].disps->X_add_number))
4182 return;
4183 else
4184 {
4185 /* Handle:
4186 lea (%rM), %rN -> mov %rM, %rN
4187 lea (,%rM,1), %rN -> mov %rM, %rN
4188 lea (%rM), %rN -> movzx %rM, %rN
4189 */
4190 const reg_entry *addr_reg;
4191
4192 if (!i.index_reg && i.base_reg->reg_num != RegIP)
4193 addr_reg = i.base_reg;
4194 else if (!i.base_reg
4195 && i.index_reg->reg_num != RegIZ
4196 && !i.log2_scale_factor)
4197 addr_reg = i.index_reg;
4198 else
4199 return;
4200
4201 if (addr_reg->reg_type.bitfield.word
4202 && i.op[1].regs->reg_type.bitfield.dword)
4203 {
4204 if (flag_code != CODE_32BIT)
4205 return;
4206 i.tm.opcode_modifier.opcodespace = SPACE_0F;
4207 i.tm.base_opcode = 0xb7;
4208 }
4209 else
4210 i.tm.base_opcode = 0x8b;
4211
4212 if (addr_reg->reg_type.bitfield.dword
4213 && i.op[1].regs->reg_type.bitfield.qword)
4214 i.tm.opcode_modifier.size = SIZE32;
4215
4216 i.op[0].regs = addr_reg;
4217 i.reg_operands = 2;
4218 }
4219
4220 i.mem_operands = 0;
4221 i.disp_operands = 0;
4222 i.prefix[ADDR_PREFIX] = 0;
4223 i.prefix[SEG_PREFIX] = 0;
4224 i.seg[0] = NULL;
4225 }
4226
b6f8c7c4 4227 if (optimize_for_space
389d00a5 4228 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
b6f8c7c4
L
4229 && i.reg_operands == 1
4230 && i.imm_operands == 1
4231 && !i.types[1].bitfield.byte
4232 && i.op[0].imms->X_op == O_constant
4233 && fits_in_imm7 (i.op[0].imms->X_add_number)
72aea328 4234 && (i.tm.base_opcode == 0xa8
b6f8c7c4
L
4235 || (i.tm.base_opcode == 0xf6
4236 && i.tm.extension_opcode == 0x0)))
4237 {
4238 /* Optimize: -Os:
4239 test $imm7, %r64/%r32/%r16 -> test $imm7, %r8
4240 */
4241 unsigned int base_regnum = i.op[1].regs->reg_num;
4242 if (flag_code == CODE_64BIT || base_regnum < 4)
4243 {
4244 i.types[1].bitfield.byte = 1;
4245 /* Ignore the suffix. */
4246 i.suffix = 0;
7697afb6
JB
4247 /* Convert to byte registers. */
4248 if (i.types[1].bitfield.word)
4249 j = 16;
4250 else if (i.types[1].bitfield.dword)
4251 j = 32;
4252 else
4253 j = 48;
4254 if (!(i.op[1].regs->reg_flags & RegRex) && base_regnum < 4)
4255 j += 8;
4256 i.op[1].regs -= j;
b6f8c7c4
L
4257 }
4258 }
4259 else if (flag_code == CODE_64BIT
389d00a5 4260 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
d3d50934
L
4261 && ((i.types[1].bitfield.qword
4262 && i.reg_operands == 1
b6f8c7c4
L
4263 && i.imm_operands == 1
4264 && i.op[0].imms->X_op == O_constant
507916b8 4265 && ((i.tm.base_opcode == 0xb8
b6f8c7c4
L
4266 && i.tm.extension_opcode == None
4267 && fits_in_unsigned_long (i.op[0].imms->X_add_number))
4268 || (fits_in_imm31 (i.op[0].imms->X_add_number)
72aea328
JB
4269 && ((i.tm.base_opcode == 0x24
4270 || i.tm.base_opcode == 0xa8)
b6f8c7c4
L
4271 || (i.tm.base_opcode == 0x80
4272 && i.tm.extension_opcode == 0x4)
4273 || ((i.tm.base_opcode == 0xf6
507916b8 4274 || (i.tm.base_opcode | 1) == 0xc7)
b8364fa7
JB
4275 && i.tm.extension_opcode == 0x0)))
4276 || (fits_in_imm7 (i.op[0].imms->X_add_number)
4277 && i.tm.base_opcode == 0x83
4278 && i.tm.extension_opcode == 0x4)))
d3d50934
L
4279 || (i.types[0].bitfield.qword
4280 && ((i.reg_operands == 2
4281 && i.op[0].regs == i.op[1].regs
72aea328
JB
4282 && (i.tm.base_opcode == 0x30
4283 || i.tm.base_opcode == 0x28))
d3d50934
L
4284 || (i.reg_operands == 1
4285 && i.operands == 1
72aea328 4286 && i.tm.base_opcode == 0x30)))))
b6f8c7c4
L
4287 {
4288 /* Optimize: -O:
4289 andq $imm31, %r64 -> andl $imm31, %r32
b8364fa7 4290 andq $imm7, %r64 -> andl $imm7, %r32
b6f8c7c4
L
4291 testq $imm31, %r64 -> testl $imm31, %r32
4292 xorq %r64, %r64 -> xorl %r32, %r32
4293 subq %r64, %r64 -> subl %r32, %r32
4294 movq $imm31, %r64 -> movl $imm31, %r32
4295 movq $imm32, %r64 -> movl $imm32, %r32
4296 */
4297 i.tm.opcode_modifier.norex64 = 1;
507916b8 4298 if (i.tm.base_opcode == 0xb8 || (i.tm.base_opcode | 1) == 0xc7)
b6f8c7c4
L
4299 {
4300 /* Handle
4301 movq $imm31, %r64 -> movl $imm31, %r32
4302 movq $imm32, %r64 -> movl $imm32, %r32
4303 */
4304 i.tm.operand_types[0].bitfield.imm32 = 1;
4305 i.tm.operand_types[0].bitfield.imm32s = 0;
4306 i.tm.operand_types[0].bitfield.imm64 = 0;
4307 i.types[0].bitfield.imm32 = 1;
4308 i.types[0].bitfield.imm32s = 0;
4309 i.types[0].bitfield.imm64 = 0;
4310 i.types[1].bitfield.dword = 1;
4311 i.types[1].bitfield.qword = 0;
507916b8 4312 if ((i.tm.base_opcode | 1) == 0xc7)
b6f8c7c4
L
4313 {
4314 /* Handle
4315 movq $imm31, %r64 -> movl $imm31, %r32
4316 */
507916b8 4317 i.tm.base_opcode = 0xb8;
b6f8c7c4 4318 i.tm.extension_opcode = None;
507916b8 4319 i.tm.opcode_modifier.w = 0;
b6f8c7c4
L
4320 i.tm.opcode_modifier.modrm = 0;
4321 }
4322 }
4323 }
5641ec01
JB
4324 else if (optimize > 1
4325 && !optimize_for_space
389d00a5 4326 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
5641ec01
JB
4327 && i.reg_operands == 2
4328 && i.op[0].regs == i.op[1].regs
4329 && ((i.tm.base_opcode & ~(Opcode_D | 1)) == 0x8
4330 || (i.tm.base_opcode & ~(Opcode_D | 1)) == 0x20)
4331 && (flag_code != CODE_64BIT || !i.types[0].bitfield.dword))
4332 {
4333 /* Optimize: -O2:
4334 andb %rN, %rN -> testb %rN, %rN
4335 andw %rN, %rN -> testw %rN, %rN
4336 andq %rN, %rN -> testq %rN, %rN
4337 orb %rN, %rN -> testb %rN, %rN
4338 orw %rN, %rN -> testw %rN, %rN
4339 orq %rN, %rN -> testq %rN, %rN
4340
4341 and outside of 64-bit mode
4342
4343 andl %rN, %rN -> testl %rN, %rN
4344 orl %rN, %rN -> testl %rN, %rN
4345 */
4346 i.tm.base_opcode = 0x84 | (i.tm.base_opcode & 1);
4347 }
99112332 4348 else if (i.reg_operands == 3
b6f8c7c4
L
4349 && i.op[0].regs == i.op[1].regs
4350 && !i.types[2].bitfield.xmmword
4351 && (i.tm.opcode_modifier.vex
6225c532 4352 || ((!i.mask.reg || i.mask.zeroing)
ca5312a2 4353 && i.rounding.type == rc_none
e771e7c9 4354 && is_evex_encoding (&i.tm)
80c34c38 4355 && (i.vec_encoding != vex_encoding_evex
dd22218c 4356 || cpu_arch_isa_flags.bitfield.cpuavx512vl
80c34c38 4357 || i.tm.cpu_flags.bitfield.cpuavx512vl
7091c612 4358 || (i.tm.operand_types[2].bitfield.zmmword
dd22218c 4359 && i.types[2].bitfield.ymmword))))
b6f8c7c4 4360 && ((i.tm.base_opcode == 0x55
b6f8c7c4 4361 || i.tm.base_opcode == 0x57
35648716
JB
4362 || i.tm.base_opcode == 0xdf
4363 || i.tm.base_opcode == 0xef
4364 || i.tm.base_opcode == 0xf8
4365 || i.tm.base_opcode == 0xf9
4366 || i.tm.base_opcode == 0xfa
4367 || i.tm.base_opcode == 0xfb
1424ad86 4368 || i.tm.base_opcode == 0x42
35648716 4369 || i.tm.base_opcode == 0x47)
b6f8c7c4
L
4370 && i.tm.extension_opcode == None))
4371 {
99112332 4372 /* Optimize: -O1:
8305403a
L
4373 VOP, one of vandnps, vandnpd, vxorps, vxorpd, vpsubb, vpsubd,
4374 vpsubq and vpsubw:
b6f8c7c4
L
4375 EVEX VOP %zmmM, %zmmM, %zmmN
4376 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4377 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4378 EVEX VOP %ymmM, %ymmM, %ymmN
4379 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4380 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4381 VEX VOP %ymmM, %ymmM, %ymmN
4382 -> VEX VOP %xmmM, %xmmM, %xmmN
4383 VOP, one of vpandn and vpxor:
4384 VEX VOP %ymmM, %ymmM, %ymmN
4385 -> VEX VOP %xmmM, %xmmM, %xmmN
4386 VOP, one of vpandnd and vpandnq:
4387 EVEX VOP %zmmM, %zmmM, %zmmN
4388 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4389 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4390 EVEX VOP %ymmM, %ymmM, %ymmN
4391 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4392 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4393 VOP, one of vpxord and vpxorq:
4394 EVEX VOP %zmmM, %zmmM, %zmmN
4395 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4396 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4397 EVEX VOP %ymmM, %ymmM, %ymmN
4398 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4399 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
1424ad86
JB
4400 VOP, one of kxord and kxorq:
4401 VEX VOP %kM, %kM, %kN
4402 -> VEX kxorw %kM, %kM, %kN
4403 VOP, one of kandnd and kandnq:
4404 VEX VOP %kM, %kM, %kN
4405 -> VEX kandnw %kM, %kM, %kN
b6f8c7c4 4406 */
e771e7c9 4407 if (is_evex_encoding (&i.tm))
b6f8c7c4 4408 {
7b1d7ca1 4409 if (i.vec_encoding != vex_encoding_evex)
b6f8c7c4
L
4410 {
4411 i.tm.opcode_modifier.vex = VEX128;
4412 i.tm.opcode_modifier.vexw = VEXW0;
4413 i.tm.opcode_modifier.evex = 0;
4414 }
7b1d7ca1 4415 else if (optimize > 1)
dd22218c
L
4416 i.tm.opcode_modifier.evex = EVEX128;
4417 else
4418 return;
b6f8c7c4 4419 }
f74a6307 4420 else if (i.tm.operand_types[0].bitfield.class == RegMask)
1424ad86 4421 {
35648716 4422 i.tm.opcode_modifier.opcodeprefix = PREFIX_NONE;
1424ad86
JB
4423 i.tm.opcode_modifier.vexw = VEXW0;
4424 }
b6f8c7c4
L
4425 else
4426 i.tm.opcode_modifier.vex = VEX128;
4427
4428 if (i.tm.opcode_modifier.vex)
4429 for (j = 0; j < 3; j++)
4430 {
4431 i.types[j].bitfield.xmmword = 1;
4432 i.types[j].bitfield.ymmword = 0;
4433 }
4434 }
392a5972 4435 else if (i.vec_encoding != vex_encoding_evex
97ed31ae 4436 && !i.types[0].bitfield.zmmword
392a5972 4437 && !i.types[1].bitfield.zmmword
6225c532 4438 && !i.mask.reg
5273a3cd 4439 && !i.broadcast.type
97ed31ae 4440 && is_evex_encoding (&i.tm)
35648716
JB
4441 && ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4442 || (i.tm.base_opcode & ~4) == 0xdb
4443 || (i.tm.base_opcode & ~4) == 0xeb)
97ed31ae
L
4444 && i.tm.extension_opcode == None)
4445 {
4446 /* Optimize: -O1:
4447 VOP, one of vmovdqa32, vmovdqa64, vmovdqu8, vmovdqu16,
4448 vmovdqu32 and vmovdqu64:
4449 EVEX VOP %xmmM, %xmmN
4450 -> VEX vmovdqa|vmovdqu %xmmM, %xmmN (M and N < 16)
4451 EVEX VOP %ymmM, %ymmN
4452 -> VEX vmovdqa|vmovdqu %ymmM, %ymmN (M and N < 16)
4453 EVEX VOP %xmmM, mem
4454 -> VEX vmovdqa|vmovdqu %xmmM, mem (M < 16)
4455 EVEX VOP %ymmM, mem
4456 -> VEX vmovdqa|vmovdqu %ymmM, mem (M < 16)
4457 EVEX VOP mem, %xmmN
4458 -> VEX mvmovdqa|vmovdquem, %xmmN (N < 16)
4459 EVEX VOP mem, %ymmN
4460 -> VEX vmovdqa|vmovdqu mem, %ymmN (N < 16)
a0a1771e
JB
4461 VOP, one of vpand, vpandn, vpor, vpxor:
4462 EVEX VOP{d,q} %xmmL, %xmmM, %xmmN
4463 -> VEX VOP %xmmL, %xmmM, %xmmN (L, M, and N < 16)
4464 EVEX VOP{d,q} %ymmL, %ymmM, %ymmN
4465 -> VEX VOP %ymmL, %ymmM, %ymmN (L, M, and N < 16)
4466 EVEX VOP{d,q} mem, %xmmM, %xmmN
4467 -> VEX VOP mem, %xmmM, %xmmN (M and N < 16)
4468 EVEX VOP{d,q} mem, %ymmM, %ymmN
4469 -> VEX VOP mem, %ymmM, %ymmN (M and N < 16)
97ed31ae 4470 */
a0a1771e 4471 for (j = 0; j < i.operands; j++)
392a5972
L
4472 if (operand_type_check (i.types[j], disp)
4473 && i.op[j].disps->X_op == O_constant)
4474 {
4475 /* Since the VEX prefix has 2 or 3 bytes, the EVEX prefix
4476 has 4 bytes, EVEX Disp8 has 1 byte and VEX Disp32 has 4
4477 bytes, we choose EVEX Disp8 over VEX Disp32. */
4478 int evex_disp8, vex_disp8;
4479 unsigned int memshift = i.memshift;
4480 offsetT n = i.op[j].disps->X_add_number;
4481
4482 evex_disp8 = fits_in_disp8 (n);
4483 i.memshift = 0;
4484 vex_disp8 = fits_in_disp8 (n);
4485 if (evex_disp8 != vex_disp8)
4486 {
4487 i.memshift = memshift;
4488 return;
4489 }
4490
4491 i.types[j].bitfield.disp8 = vex_disp8;
4492 break;
4493 }
35648716
JB
4494 if ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4495 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2)
4496 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
97ed31ae
L
4497 i.tm.opcode_modifier.vex
4498 = i.types[0].bitfield.ymmword ? VEX256 : VEX128;
4499 i.tm.opcode_modifier.vexw = VEXW0;
79dec6b7 4500 /* VPAND, VPOR, and VPXOR are commutative. */
35648716 4501 if (i.reg_operands == 3 && i.tm.base_opcode != 0xdf)
79dec6b7 4502 i.tm.opcode_modifier.commutative = 1;
97ed31ae
L
4503 i.tm.opcode_modifier.evex = 0;
4504 i.tm.opcode_modifier.masking = 0;
a0a1771e 4505 i.tm.opcode_modifier.broadcast = 0;
97ed31ae
L
4506 i.tm.opcode_modifier.disp8memshift = 0;
4507 i.memshift = 0;
a0a1771e
JB
4508 if (j < i.operands)
4509 i.types[j].bitfield.disp8
4510 = fits_in_disp8 (i.op[j].disps->X_add_number);
97ed31ae 4511 }
b6f8c7c4
L
4512}
4513
ae531041
L
4514/* Return non-zero for load instruction. */
4515
4516static int
4517load_insn_p (void)
4518{
4519 unsigned int dest;
4520 int any_vex_p = is_any_vex_encoding (&i.tm);
4521 unsigned int base_opcode = i.tm.base_opcode | 1;
4522
4523 if (!any_vex_p)
4524 {
a09f656b 4525 /* Anysize insns: lea, invlpg, clflush, prefetchnta, prefetcht0,
4526 prefetcht1, prefetcht2, prefetchtw, bndmk, bndcl, bndcu, bndcn,
4527 bndstx, bndldx, prefetchwt1, clflushopt, clwb, cldemote. */
4528 if (i.tm.opcode_modifier.anysize)
ae531041
L
4529 return 0;
4530
389d00a5
JB
4531 /* pop. */
4532 if (strcmp (i.tm.name, "pop") == 0)
4533 return 1;
4534 }
4535
4536 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE)
4537 {
4538 /* popf, popa. */
4539 if (i.tm.base_opcode == 0x9d
a09f656b 4540 || i.tm.base_opcode == 0x61)
ae531041
L
4541 return 1;
4542
4543 /* movs, cmps, lods, scas. */
4544 if ((i.tm.base_opcode | 0xb) == 0xaf)
4545 return 1;
4546
a09f656b 4547 /* outs, xlatb. */
4548 if (base_opcode == 0x6f
4549 || i.tm.base_opcode == 0xd7)
ae531041 4550 return 1;
a09f656b 4551 /* NB: For AMD-specific insns with implicit memory operands,
4552 they're intentionally not covered. */
ae531041
L
4553 }
4554
4555 /* No memory operand. */
4556 if (!i.mem_operands)
4557 return 0;
4558
4559 if (any_vex_p)
4560 {
4561 /* vldmxcsr. */
4562 if (i.tm.base_opcode == 0xae
4563 && i.tm.opcode_modifier.vex
441f6aca 4564 && i.tm.opcode_modifier.opcodespace == SPACE_0F
35648716 4565 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
ae531041
L
4566 && i.tm.extension_opcode == 2)
4567 return 1;
4568 }
389d00a5 4569 else if (i.tm.opcode_modifier.opcodespace == SPACE_BASE)
ae531041
L
4570 {
4571 /* test, not, neg, mul, imul, div, idiv. */
4572 if ((i.tm.base_opcode == 0xf6 || i.tm.base_opcode == 0xf7)
4573 && i.tm.extension_opcode != 1)
4574 return 1;
4575
4576 /* inc, dec. */
4577 if (base_opcode == 0xff && i.tm.extension_opcode <= 1)
4578 return 1;
4579
4580 /* add, or, adc, sbb, and, sub, xor, cmp. */
4581 if (i.tm.base_opcode >= 0x80 && i.tm.base_opcode <= 0x83)
4582 return 1;
4583
ae531041
L
4584 /* rol, ror, rcl, rcr, shl/sal, shr, sar. */
4585 if ((base_opcode == 0xc1
4586 || (i.tm.base_opcode >= 0xd0 && i.tm.base_opcode <= 0xd3))
4587 && i.tm.extension_opcode != 6)
4588 return 1;
4589
ae531041 4590 /* Check for x87 instructions. */
389d00a5 4591 if (base_opcode >= 0xd8 && base_opcode <= 0xdf)
ae531041
L
4592 {
4593 /* Skip fst, fstp, fstenv, fstcw. */
4594 if (i.tm.base_opcode == 0xd9
4595 && (i.tm.extension_opcode == 2
4596 || i.tm.extension_opcode == 3
4597 || i.tm.extension_opcode == 6
4598 || i.tm.extension_opcode == 7))
4599 return 0;
4600
4601 /* Skip fisttp, fist, fistp, fstp. */
4602 if (i.tm.base_opcode == 0xdb
4603 && (i.tm.extension_opcode == 1
4604 || i.tm.extension_opcode == 2
4605 || i.tm.extension_opcode == 3
4606 || i.tm.extension_opcode == 7))
4607 return 0;
4608
4609 /* Skip fisttp, fst, fstp, fsave, fstsw. */
4610 if (i.tm.base_opcode == 0xdd
4611 && (i.tm.extension_opcode == 1
4612 || i.tm.extension_opcode == 2
4613 || i.tm.extension_opcode == 3
4614 || i.tm.extension_opcode == 6
4615 || i.tm.extension_opcode == 7))
4616 return 0;
4617
4618 /* Skip fisttp, fist, fistp, fbstp, fistp. */
4619 if (i.tm.base_opcode == 0xdf
4620 && (i.tm.extension_opcode == 1
4621 || i.tm.extension_opcode == 2
4622 || i.tm.extension_opcode == 3
4623 || i.tm.extension_opcode == 6
4624 || i.tm.extension_opcode == 7))
4625 return 0;
4626
4627 return 1;
4628 }
4629 }
389d00a5
JB
4630 else if (i.tm.opcode_modifier.opcodespace == SPACE_0F)
4631 {
4632 /* bt, bts, btr, btc. */
4633 if (i.tm.base_opcode == 0xba
4634 && (i.tm.extension_opcode >= 4 && i.tm.extension_opcode <= 7))
4635 return 1;
4636
4637 /* cmpxchg8b, cmpxchg16b, xrstors, vmptrld. */
4638 if (i.tm.base_opcode == 0xc7
4639 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
4640 && (i.tm.extension_opcode == 1 || i.tm.extension_opcode == 3
4641 || i.tm.extension_opcode == 6))
4642 return 1;
4643
4644 /* fxrstor, ldmxcsr, xrstor. */
4645 if (i.tm.base_opcode == 0xae
4646 && (i.tm.extension_opcode == 1
4647 || i.tm.extension_opcode == 2
4648 || i.tm.extension_opcode == 5))
4649 return 1;
4650
4651 /* lgdt, lidt, lmsw. */
4652 if (i.tm.base_opcode == 0x01
4653 && (i.tm.extension_opcode == 2
4654 || i.tm.extension_opcode == 3
4655 || i.tm.extension_opcode == 6))
4656 return 1;
4657 }
ae531041
L
4658
4659 dest = i.operands - 1;
4660
4661 /* Check fake imm8 operand and 3 source operands. */
4662 if ((i.tm.opcode_modifier.immext
4663 || i.tm.opcode_modifier.vexsources == VEX3SOURCES)
4664 && i.types[dest].bitfield.imm8)
4665 dest--;
4666
389d00a5
JB
4667 /* add, or, adc, sbb, and, sub, xor, cmp, test, xchg. */
4668 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
ae531041
L
4669 && (base_opcode == 0x1
4670 || base_opcode == 0x9
4671 || base_opcode == 0x11
4672 || base_opcode == 0x19
4673 || base_opcode == 0x21
4674 || base_opcode == 0x29
4675 || base_opcode == 0x31
4676 || base_opcode == 0x39
389d00a5
JB
4677 || (base_opcode | 2) == 0x87))
4678 return 1;
4679
4680 /* xadd. */
4681 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4682 && base_opcode == 0xc1)
ae531041
L
4683 return 1;
4684
4685 /* Check for load instruction. */
4686 return (i.types[dest].bitfield.class != ClassNone
4687 || i.types[dest].bitfield.instance == Accum);
4688}
4689
4690/* Output lfence, 0xfaee8, after instruction. */
4691
4692static void
4693insert_lfence_after (void)
4694{
4695 if (lfence_after_load && load_insn_p ())
4696 {
a09f656b 4697 /* There are also two REP string instructions that require
4698 special treatment. Specifically, the compare string (CMPS)
4699 and scan string (SCAS) instructions set EFLAGS in a manner
4700 that depends on the data being compared/scanned. When used
4701 with a REP prefix, the number of iterations may therefore
4702 vary depending on this data. If the data is a program secret
4703 chosen by the adversary using an LVI method,
4704 then this data-dependent behavior may leak some aspect
4705 of the secret. */
4706 if (((i.tm.base_opcode | 0x1) == 0xa7
4707 || (i.tm.base_opcode | 0x1) == 0xaf)
4708 && i.prefix[REP_PREFIX])
4709 {
4710 as_warn (_("`%s` changes flags which would affect control flow behavior"),
4711 i.tm.name);
4712 }
ae531041
L
4713 char *p = frag_more (3);
4714 *p++ = 0xf;
4715 *p++ = 0xae;
4716 *p = 0xe8;
4717 }
4718}
4719
4720/* Output lfence, 0xfaee8, before instruction. */
4721
4722static void
4723insert_lfence_before (void)
4724{
4725 char *p;
4726
389d00a5 4727 if (i.tm.opcode_modifier.opcodespace != SPACE_BASE)
ae531041
L
4728 return;
4729
4730 if (i.tm.base_opcode == 0xff
4731 && (i.tm.extension_opcode == 2 || i.tm.extension_opcode == 4))
4732 {
4733 /* Insert lfence before indirect branch if needed. */
4734
4735 if (lfence_before_indirect_branch == lfence_branch_none)
4736 return;
4737
4738 if (i.operands != 1)
4739 abort ();
4740
4741 if (i.reg_operands == 1)
4742 {
4743 /* Indirect branch via register. Don't insert lfence with
4744 -mlfence-after-load=yes. */
4745 if (lfence_after_load
4746 || lfence_before_indirect_branch == lfence_branch_memory)
4747 return;
4748 }
4749 else if (i.mem_operands == 1
4750 && lfence_before_indirect_branch != lfence_branch_register)
4751 {
4752 as_warn (_("indirect `%s` with memory operand should be avoided"),
4753 i.tm.name);
4754 return;
4755 }
4756 else
4757 return;
4758
4759 if (last_insn.kind != last_insn_other
4760 && last_insn.seg == now_seg)
4761 {
4762 as_warn_where (last_insn.file, last_insn.line,
4763 _("`%s` skips -mlfence-before-indirect-branch on `%s`"),
4764 last_insn.name, i.tm.name);
4765 return;
4766 }
4767
4768 p = frag_more (3);
4769 *p++ = 0xf;
4770 *p++ = 0xae;
4771 *p = 0xe8;
4772 return;
4773 }
4774
503648e4 4775 /* Output or/not/shl and lfence before near ret. */
ae531041
L
4776 if (lfence_before_ret != lfence_before_ret_none
4777 && (i.tm.base_opcode == 0xc2
503648e4 4778 || i.tm.base_opcode == 0xc3))
ae531041
L
4779 {
4780 if (last_insn.kind != last_insn_other
4781 && last_insn.seg == now_seg)
4782 {
4783 as_warn_where (last_insn.file, last_insn.line,
4784 _("`%s` skips -mlfence-before-ret on `%s`"),
4785 last_insn.name, i.tm.name);
4786 return;
4787 }
a09f656b 4788
a09f656b 4789 /* Near ret ingore operand size override under CPU64. */
503648e4 4790 char prefix = flag_code == CODE_64BIT
4791 ? 0x48
4792 : i.prefix[DATA_PREFIX] ? 0x66 : 0x0;
a09f656b 4793
4794 if (lfence_before_ret == lfence_before_ret_not)
4795 {
4796 /* not: 0xf71424, may add prefix
4797 for operand size override or 64-bit code. */
4798 p = frag_more ((prefix ? 2 : 0) + 6 + 3);
4799 if (prefix)
4800 *p++ = prefix;
ae531041
L
4801 *p++ = 0xf7;
4802 *p++ = 0x14;
4803 *p++ = 0x24;
a09f656b 4804 if (prefix)
4805 *p++ = prefix;
ae531041
L
4806 *p++ = 0xf7;
4807 *p++ = 0x14;
4808 *p++ = 0x24;
4809 }
a09f656b 4810 else
4811 {
4812 p = frag_more ((prefix ? 1 : 0) + 4 + 3);
4813 if (prefix)
4814 *p++ = prefix;
4815 if (lfence_before_ret == lfence_before_ret_or)
4816 {
4817 /* or: 0x830c2400, may add prefix
4818 for operand size override or 64-bit code. */
4819 *p++ = 0x83;
4820 *p++ = 0x0c;
4821 }
4822 else
4823 {
4824 /* shl: 0xc1242400, may add prefix
4825 for operand size override or 64-bit code. */
4826 *p++ = 0xc1;
4827 *p++ = 0x24;
4828 }
4829
4830 *p++ = 0x24;
4831 *p++ = 0x0;
4832 }
4833
ae531041
L
4834 *p++ = 0xf;
4835 *p++ = 0xae;
4836 *p = 0xe8;
4837 }
4838}
4839
252b5132
RH
4840/* This is the guts of the machine-dependent assembler. LINE points to a
4841 machine dependent instruction. This function is supposed to emit
4842 the frags/bytes it assembles to. */
4843
4844void
65da13b5 4845md_assemble (char *line)
252b5132 4846{
40fb9820 4847 unsigned int j;
83b16ac6 4848 char mnemonic[MAX_MNEM_SIZE], mnem_suffix;
d3ce72d0 4849 const insn_template *t;
252b5132 4850
47926f60 4851 /* Initialize globals. */
252b5132 4852 memset (&i, '\0', sizeof (i));
ca5312a2 4853 i.rounding.type = rc_none;
252b5132 4854 for (j = 0; j < MAX_OPERANDS; j++)
1ae12ab7 4855 i.reloc[j] = NO_RELOC;
252b5132
RH
4856 memset (disp_expressions, '\0', sizeof (disp_expressions));
4857 memset (im_expressions, '\0', sizeof (im_expressions));
ce8a8b2f 4858 save_stack_p = save_stack;
252b5132
RH
4859
4860 /* First parse an instruction mnemonic & call i386_operand for the operands.
4861 We assume that the scrubber has arranged it so that line[0] is the valid
47926f60 4862 start of a (possibly prefixed) mnemonic. */
252b5132 4863
29b0f896
AM
4864 line = parse_insn (line, mnemonic);
4865 if (line == NULL)
4866 return;
83b16ac6 4867 mnem_suffix = i.suffix;
252b5132 4868
29b0f896 4869 line = parse_operands (line, mnemonic);
ee86248c 4870 this_operand = -1;
8325cc63
JB
4871 xfree (i.memop1_string);
4872 i.memop1_string = NULL;
29b0f896
AM
4873 if (line == NULL)
4874 return;
252b5132 4875
29b0f896
AM
4876 /* Now we've parsed the mnemonic into a set of templates, and have the
4877 operands at hand. */
4878
b630c145 4879 /* All Intel opcodes have reversed operands except for "bound", "enter",
c0e54661
JB
4880 "invlpg*", "monitor*", "mwait*", "tpause", "umwait", "pvalidate",
4881 "rmpadjust", and "rmpupdate". We also don't reverse intersegment "jmp"
4882 and "call" instructions with 2 immediate operands so that the immediate
4883 segment precedes the offset consistently in Intel and AT&T modes. */
4d456e3d
L
4884 if (intel_syntax
4885 && i.operands > 1
29b0f896 4886 && (strcmp (mnemonic, "bound") != 0)
c0e54661 4887 && (strncmp (mnemonic, "invlpg", 6) != 0)
d34049e8
ML
4888 && !startswith (mnemonic, "monitor")
4889 && !startswith (mnemonic, "mwait")
c0e54661 4890 && (strcmp (mnemonic, "pvalidate") != 0)
d34049e8 4891 && !startswith (mnemonic, "rmp")
b630c145
JB
4892 && (strcmp (mnemonic, "tpause") != 0)
4893 && (strcmp (mnemonic, "umwait") != 0)
40fb9820
L
4894 && !(operand_type_check (i.types[0], imm)
4895 && operand_type_check (i.types[1], imm)))
29b0f896
AM
4896 swap_operands ();
4897
ec56d5c0
JB
4898 /* The order of the immediates should be reversed
4899 for 2 immediates extrq and insertq instructions */
4900 if (i.imm_operands == 2
4901 && (strcmp (mnemonic, "extrq") == 0
4902 || strcmp (mnemonic, "insertq") == 0))
4903 swap_2_operands (0, 1);
4904
29b0f896
AM
4905 if (i.imm_operands)
4906 optimize_imm ();
4907
a9aabc23 4908 if (i.disp_operands && !want_disp32 (current_templates->start))
cce08655
JB
4909 {
4910 for (j = 0; j < i.operands; ++j)
4911 {
4912 const expressionS *exp = i.op[j].disps;
4913
4914 if (!operand_type_check (i.types[j], disp))
4915 continue;
4916
4917 if (exp->X_op != O_constant)
4918 continue;
4919
4920 /* Since displacement is signed extended to 64bit, don't allow
4921 disp32 and turn off disp32s if they are out of range. */
4922 i.types[j].bitfield.disp32 = 0;
4923 if (fits_in_signed_long (exp->X_add_number))
4924 continue;
4925
4926 i.types[j].bitfield.disp32s = 0;
4927 if (i.types[j].bitfield.baseindex)
4928 {
4929 as_bad (_("0x%" BFD_VMA_FMT "x out of range of signed 32bit displacement"),
4930 exp->X_add_number);
4931 return;
4932 }
4933 }
4934 }
4935
b300c311
L
4936 /* Don't optimize displacement for movabs since it only takes 64bit
4937 displacement. */
4938 if (i.disp_operands
a501d77e 4939 && i.disp_encoding != disp_encoding_32bit
862be3fb
L
4940 && (flag_code != CODE_64BIT
4941 || strcmp (mnemonic, "movabs") != 0))
4942 optimize_disp ();
29b0f896
AM
4943
4944 /* Next, we find a template that matches the given insn,
4945 making sure the overlap of the given operands types is consistent
4946 with the template operand types. */
252b5132 4947
83b16ac6 4948 if (!(t = match_template (mnem_suffix)))
29b0f896 4949 return;
252b5132 4950
7bab8ab5 4951 if (sse_check != check_none
81f8a913 4952 && !i.tm.opcode_modifier.noavx
6e3e5c9e 4953 && !i.tm.cpu_flags.bitfield.cpuavx
569d50f1 4954 && !i.tm.cpu_flags.bitfield.cpuavx512f
daf50ae7
L
4955 && (i.tm.cpu_flags.bitfield.cpusse
4956 || i.tm.cpu_flags.bitfield.cpusse2
4957 || i.tm.cpu_flags.bitfield.cpusse3
4958 || i.tm.cpu_flags.bitfield.cpussse3
4959 || i.tm.cpu_flags.bitfield.cpusse4_1
6e3e5c9e
JB
4960 || i.tm.cpu_flags.bitfield.cpusse4_2
4961 || i.tm.cpu_flags.bitfield.cpupclmul
4962 || i.tm.cpu_flags.bitfield.cpuaes
569d50f1 4963 || i.tm.cpu_flags.bitfield.cpusha
6e3e5c9e 4964 || i.tm.cpu_flags.bitfield.cpugfni))
daf50ae7 4965 {
7bab8ab5 4966 (sse_check == check_warning
daf50ae7
L
4967 ? as_warn
4968 : as_bad) (_("SSE instruction `%s' is used"), i.tm.name);
4969 }
4970
40fb9820 4971 if (i.tm.opcode_modifier.fwait)
29b0f896
AM
4972 if (!add_prefix (FWAIT_OPCODE))
4973 return;
252b5132 4974
d5de92cf 4975 /* Check if REP prefix is OK. */
742732c7 4976 if (i.rep_prefix && i.tm.opcode_modifier.prefixok != PrefixRep)
d5de92cf
L
4977 {
4978 as_bad (_("invalid instruction `%s' after `%s'"),
4979 i.tm.name, i.rep_prefix);
4980 return;
4981 }
4982
c1ba0266
L
4983 /* Check for lock without a lockable instruction. Destination operand
4984 must be memory unless it is xchg (0x86). */
c32fa91d 4985 if (i.prefix[LOCK_PREFIX]
742732c7 4986 && (i.tm.opcode_modifier.prefixok < PrefixLock
c1ba0266
L
4987 || i.mem_operands == 0
4988 || (i.tm.base_opcode != 0x86
8dc0818e 4989 && !(i.flags[i.operands - 1] & Operand_Mem))))
c32fa91d
L
4990 {
4991 as_bad (_("expecting lockable instruction after `lock'"));
4992 return;
4993 }
4994
40d231b4
JB
4995 /* Check for data size prefix on VEX/XOP/EVEX encoded and SIMD insns. */
4996 if (i.prefix[DATA_PREFIX]
4997 && (is_any_vex_encoding (&i.tm)
4998 || i.tm.operand_types[i.imm_operands].bitfield.class >= RegMMX
4999 || i.tm.operand_types[i.imm_operands + 1].bitfield.class >= RegMMX))
7a8655d2
JB
5000 {
5001 as_bad (_("data size prefix invalid with `%s'"), i.tm.name);
5002 return;
5003 }
5004
42164a71 5005 /* Check if HLE prefix is OK. */
165de32a 5006 if (i.hle_prefix && !check_hle ())
42164a71
L
5007 return;
5008
7e8b059b
L
5009 /* Check BND prefix. */
5010 if (i.bnd_prefix && !i.tm.opcode_modifier.bndprefixok)
5011 as_bad (_("expecting valid branch instruction after `bnd'"));
5012
04ef582a 5013 /* Check NOTRACK prefix. */
742732c7 5014 if (i.notrack_prefix && i.tm.opcode_modifier.prefixok != PrefixNoTrack)
9fef80d6 5015 as_bad (_("expecting indirect branch instruction after `notrack'"));
04ef582a 5016
327e8c42
JB
5017 if (i.tm.cpu_flags.bitfield.cpumpx)
5018 {
5019 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
5020 as_bad (_("32-bit address isn't allowed in 64-bit MPX instructions."));
5021 else if (flag_code != CODE_16BIT
5022 ? i.prefix[ADDR_PREFIX]
5023 : i.mem_operands && !i.prefix[ADDR_PREFIX])
5024 as_bad (_("16-bit address isn't allowed in MPX instructions"));
5025 }
7e8b059b
L
5026
5027 /* Insert BND prefix. */
76d3a78a
JB
5028 if (add_bnd_prefix && i.tm.opcode_modifier.bndprefixok)
5029 {
5030 if (!i.prefix[BND_PREFIX])
5031 add_prefix (BND_PREFIX_OPCODE);
5032 else if (i.prefix[BND_PREFIX] != BND_PREFIX_OPCODE)
5033 {
5034 as_warn (_("replacing `rep'/`repe' prefix by `bnd'"));
5035 i.prefix[BND_PREFIX] = BND_PREFIX_OPCODE;
5036 }
5037 }
7e8b059b 5038
29b0f896 5039 /* Check string instruction segment overrides. */
51c8edf6 5040 if (i.tm.opcode_modifier.isstring >= IS_STRING_ES_OP0)
29b0f896 5041 {
51c8edf6 5042 gas_assert (i.mem_operands);
29b0f896 5043 if (!check_string ())
5dd0794d 5044 return;
fc0763e6 5045 i.disp_operands = 0;
29b0f896 5046 }
5dd0794d 5047
b6f8c7c4
L
5048 if (optimize && !i.no_optimize && i.tm.opcode_modifier.optimize)
5049 optimize_encoding ();
5050
29b0f896
AM
5051 if (!process_suffix ())
5052 return;
e413e4e9 5053
921eafea 5054 /* Update operand types and check extended states. */
bc0844ae 5055 for (j = 0; j < i.operands; j++)
921eafea
L
5056 {
5057 i.types[j] = operand_type_and (i.types[j], i.tm.operand_types[j]);
3d70986f 5058 switch (i.tm.operand_types[j].bitfield.class)
921eafea
L
5059 {
5060 default:
5061 break;
5062 case RegMMX:
5063 i.xstate |= xstate_mmx;
5064 break;
5065 case RegMask:
32930e4e 5066 i.xstate |= xstate_mask;
921eafea
L
5067 break;
5068 case RegSIMD:
3d70986f 5069 if (i.tm.operand_types[j].bitfield.tmmword)
921eafea 5070 i.xstate |= xstate_tmm;
3d70986f 5071 else if (i.tm.operand_types[j].bitfield.zmmword)
921eafea 5072 i.xstate |= xstate_zmm;
3d70986f 5073 else if (i.tm.operand_types[j].bitfield.ymmword)
921eafea 5074 i.xstate |= xstate_ymm;
3d70986f 5075 else if (i.tm.operand_types[j].bitfield.xmmword)
921eafea
L
5076 i.xstate |= xstate_xmm;
5077 break;
5078 }
5079 }
bc0844ae 5080
29b0f896
AM
5081 /* Make still unresolved immediate matches conform to size of immediate
5082 given in i.suffix. */
5083 if (!finalize_imm ())
5084 return;
252b5132 5085
40fb9820 5086 if (i.types[0].bitfield.imm1)
29b0f896 5087 i.imm_operands = 0; /* kludge for shift insns. */
252b5132 5088
9afe6eb8
L
5089 /* We only need to check those implicit registers for instructions
5090 with 3 operands or less. */
5091 if (i.operands <= 3)
5092 for (j = 0; j < i.operands; j++)
75e5731b
JB
5093 if (i.types[j].bitfield.instance != InstanceNone
5094 && !i.types[j].bitfield.xmmword)
9afe6eb8 5095 i.reg_operands--;
40fb9820 5096
29b0f896
AM
5097 /* For insns with operands there are more diddles to do to the opcode. */
5098 if (i.operands)
5099 {
5100 if (!process_operands ())
5101 return;
5102 }
8c190ce0 5103 else if (!quiet_warnings && i.tm.opcode_modifier.ugh)
29b0f896
AM
5104 {
5105 /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc. */
5106 as_warn (_("translating to `%sp'"), i.tm.name);
5107 }
252b5132 5108
7a8655d2 5109 if (is_any_vex_encoding (&i.tm))
9e5e5283 5110 {
c1dc7af5 5111 if (!cpu_arch_flags.bitfield.cpui286)
9e5e5283 5112 {
c1dc7af5 5113 as_bad (_("instruction `%s' isn't supported outside of protected mode."),
9e5e5283
L
5114 i.tm.name);
5115 return;
5116 }
c0f3af97 5117
0b9404fd
JB
5118 /* Check for explicit REX prefix. */
5119 if (i.prefix[REX_PREFIX] || i.rex_encoding)
5120 {
5121 as_bad (_("REX prefix invalid with `%s'"), i.tm.name);
5122 return;
5123 }
5124
9e5e5283
L
5125 if (i.tm.opcode_modifier.vex)
5126 build_vex_prefix (t);
5127 else
5128 build_evex_prefix ();
0b9404fd
JB
5129
5130 /* The individual REX.RXBW bits got consumed. */
5131 i.rex &= REX_OPCODE;
9e5e5283 5132 }
43234a1e 5133
5dd85c99
SP
5134 /* Handle conversion of 'int $3' --> special int3 insn. XOP or FMA4
5135 instructions may define INT_OPCODE as well, so avoid this corner
5136 case for those instructions that use MODRM. */
389d00a5
JB
5137 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
5138 && i.tm.base_opcode == INT_OPCODE
a6461c02
SP
5139 && !i.tm.opcode_modifier.modrm
5140 && i.op[0].imms->X_add_number == 3)
29b0f896
AM
5141 {
5142 i.tm.base_opcode = INT3_OPCODE;
5143 i.imm_operands = 0;
5144 }
252b5132 5145
0cfa3eb3
JB
5146 if ((i.tm.opcode_modifier.jump == JUMP
5147 || i.tm.opcode_modifier.jump == JUMP_BYTE
5148 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896
AM
5149 && i.op[0].disps->X_op == O_constant)
5150 {
5151 /* Convert "jmp constant" (and "call constant") to a jump (call) to
5152 the absolute address given by the constant. Since ix86 jumps and
5153 calls are pc relative, we need to generate a reloc. */
5154 i.op[0].disps->X_add_symbol = &abs_symbol;
5155 i.op[0].disps->X_op = O_symbol;
5156 }
252b5132 5157
29b0f896
AM
5158 /* For 8 bit registers we need an empty rex prefix. Also if the
5159 instruction already has a prefix, we need to convert old
5160 registers to new ones. */
773f551c 5161
bab6aec1 5162 if ((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte
29b0f896 5163 && (i.op[0].regs->reg_flags & RegRex64) != 0)
bab6aec1 5164 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte
29b0f896 5165 && (i.op[1].regs->reg_flags & RegRex64) != 0)
bab6aec1
JB
5166 || (((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte)
5167 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte))
29b0f896
AM
5168 && i.rex != 0))
5169 {
5170 int x;
726c5dcd 5171
29b0f896
AM
5172 i.rex |= REX_OPCODE;
5173 for (x = 0; x < 2; x++)
5174 {
5175 /* Look for 8 bit operand that uses old registers. */
bab6aec1 5176 if (i.types[x].bitfield.class == Reg && i.types[x].bitfield.byte
29b0f896 5177 && (i.op[x].regs->reg_flags & RegRex64) == 0)
773f551c 5178 {
3f93af61 5179 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
29b0f896
AM
5180 /* In case it is "hi" register, give up. */
5181 if (i.op[x].regs->reg_num > 3)
a540244d 5182 as_bad (_("can't encode register '%s%s' in an "
4eed87de 5183 "instruction requiring REX prefix."),
a540244d 5184 register_prefix, i.op[x].regs->reg_name);
773f551c 5185
29b0f896
AM
5186 /* Otherwise it is equivalent to the extended register.
5187 Since the encoding doesn't change this is merely
5188 cosmetic cleanup for debug output. */
5189
5190 i.op[x].regs = i.op[x].regs + 8;
773f551c 5191 }
29b0f896
AM
5192 }
5193 }
773f551c 5194
6b6b6807
L
5195 if (i.rex == 0 && i.rex_encoding)
5196 {
5197 /* Check if we can add a REX_OPCODE byte. Look for 8 bit operand
3f93af61 5198 that uses legacy register. If it is "hi" register, don't add
6b6b6807
L
5199 the REX_OPCODE byte. */
5200 int x;
5201 for (x = 0; x < 2; x++)
bab6aec1 5202 if (i.types[x].bitfield.class == Reg
6b6b6807
L
5203 && i.types[x].bitfield.byte
5204 && (i.op[x].regs->reg_flags & RegRex64) == 0
5205 && i.op[x].regs->reg_num > 3)
5206 {
3f93af61 5207 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
5b7c81bd 5208 i.rex_encoding = false;
6b6b6807
L
5209 break;
5210 }
5211
5212 if (i.rex_encoding)
5213 i.rex = REX_OPCODE;
5214 }
5215
7ab9ffdd 5216 if (i.rex != 0)
29b0f896
AM
5217 add_prefix (REX_OPCODE | i.rex);
5218
ae531041
L
5219 insert_lfence_before ();
5220
29b0f896
AM
5221 /* We are ready to output the insn. */
5222 output_insn ();
e379e5f3 5223
ae531041
L
5224 insert_lfence_after ();
5225
e379e5f3
L
5226 last_insn.seg = now_seg;
5227
5228 if (i.tm.opcode_modifier.isprefix)
5229 {
5230 last_insn.kind = last_insn_prefix;
5231 last_insn.name = i.tm.name;
5232 last_insn.file = as_where (&last_insn.line);
5233 }
5234 else
5235 last_insn.kind = last_insn_other;
29b0f896
AM
5236}
5237
5238static char *
e3bb37b5 5239parse_insn (char *line, char *mnemonic)
29b0f896
AM
5240{
5241 char *l = line;
5242 char *token_start = l;
5243 char *mnem_p;
5c6af06e 5244 int supported;
d3ce72d0 5245 const insn_template *t;
b6169b20 5246 char *dot_p = NULL;
29b0f896 5247
29b0f896
AM
5248 while (1)
5249 {
5250 mnem_p = mnemonic;
5251 while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
5252 {
b6169b20
L
5253 if (*mnem_p == '.')
5254 dot_p = mnem_p;
29b0f896
AM
5255 mnem_p++;
5256 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
45288df1 5257 {
29b0f896
AM
5258 as_bad (_("no such instruction: `%s'"), token_start);
5259 return NULL;
5260 }
5261 l++;
5262 }
5263 if (!is_space_char (*l)
5264 && *l != END_OF_INSN
e44823cf
JB
5265 && (intel_syntax
5266 || (*l != PREFIX_SEPARATOR
5267 && *l != ',')))
29b0f896
AM
5268 {
5269 as_bad (_("invalid character %s in mnemonic"),
5270 output_invalid (*l));
5271 return NULL;
5272 }
5273 if (token_start == l)
5274 {
e44823cf 5275 if (!intel_syntax && *l == PREFIX_SEPARATOR)
29b0f896
AM
5276 as_bad (_("expecting prefix; got nothing"));
5277 else
5278 as_bad (_("expecting mnemonic; got nothing"));
5279 return NULL;
5280 }
45288df1 5281
29b0f896 5282 /* Look up instruction (or prefix) via hash table. */
629310ab 5283 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
47926f60 5284
29b0f896
AM
5285 if (*l != END_OF_INSN
5286 && (!is_space_char (*l) || l[1] != END_OF_INSN)
5287 && current_templates
40fb9820 5288 && current_templates->start->opcode_modifier.isprefix)
29b0f896 5289 {
c6fb90c8 5290 if (!cpu_flags_check_cpu64 (current_templates->start->cpu_flags))
2dd88dca
JB
5291 {
5292 as_bad ((flag_code != CODE_64BIT
5293 ? _("`%s' is only supported in 64-bit mode")
5294 : _("`%s' is not supported in 64-bit mode")),
5295 current_templates->start->name);
5296 return NULL;
5297 }
29b0f896
AM
5298 /* If we are in 16-bit mode, do not allow addr16 or data16.
5299 Similarly, in 32-bit mode, do not allow addr32 or data32. */
673fe0f0
JB
5300 if ((current_templates->start->opcode_modifier.size == SIZE16
5301 || current_templates->start->opcode_modifier.size == SIZE32)
29b0f896 5302 && flag_code != CODE_64BIT
673fe0f0 5303 && ((current_templates->start->opcode_modifier.size == SIZE32)
29b0f896
AM
5304 ^ (flag_code == CODE_16BIT)))
5305 {
5306 as_bad (_("redundant %s prefix"),
5307 current_templates->start->name);
5308 return NULL;
45288df1 5309 }
31184569
JB
5310
5311 if (current_templates->start->base_opcode == PSEUDO_PREFIX)
29b0f896 5312 {
86fa6981 5313 /* Handle pseudo prefixes. */
31184569 5314 switch (current_templates->start->extension_opcode)
86fa6981 5315 {
41eb8e88 5316 case Prefix_Disp8:
86fa6981
L
5317 /* {disp8} */
5318 i.disp_encoding = disp_encoding_8bit;
5319 break;
41eb8e88
L
5320 case Prefix_Disp16:
5321 /* {disp16} */
5322 i.disp_encoding = disp_encoding_16bit;
5323 break;
5324 case Prefix_Disp32:
86fa6981
L
5325 /* {disp32} */
5326 i.disp_encoding = disp_encoding_32bit;
5327 break;
41eb8e88 5328 case Prefix_Load:
86fa6981
L
5329 /* {load} */
5330 i.dir_encoding = dir_encoding_load;
5331 break;
41eb8e88 5332 case Prefix_Store:
86fa6981
L
5333 /* {store} */
5334 i.dir_encoding = dir_encoding_store;
5335 break;
41eb8e88 5336 case Prefix_VEX:
42e04b36
L
5337 /* {vex} */
5338 i.vec_encoding = vex_encoding_vex;
86fa6981 5339 break;
41eb8e88 5340 case Prefix_VEX3:
86fa6981
L
5341 /* {vex3} */
5342 i.vec_encoding = vex_encoding_vex3;
5343 break;
41eb8e88 5344 case Prefix_EVEX:
86fa6981
L
5345 /* {evex} */
5346 i.vec_encoding = vex_encoding_evex;
5347 break;
41eb8e88 5348 case Prefix_REX:
6b6b6807 5349 /* {rex} */
5b7c81bd 5350 i.rex_encoding = true;
6b6b6807 5351 break;
41eb8e88 5352 case Prefix_NoOptimize:
b6f8c7c4 5353 /* {nooptimize} */
5b7c81bd 5354 i.no_optimize = true;
b6f8c7c4 5355 break;
86fa6981
L
5356 default:
5357 abort ();
5358 }
5359 }
5360 else
5361 {
5362 /* Add prefix, checking for repeated prefixes. */
4e9ac44a 5363 switch (add_prefix (current_templates->start->base_opcode))
86fa6981 5364 {
4e9ac44a
L
5365 case PREFIX_EXIST:
5366 return NULL;
5367 case PREFIX_DS:
d777820b 5368 if (current_templates->start->cpu_flags.bitfield.cpuibt)
4e9ac44a
L
5369 i.notrack_prefix = current_templates->start->name;
5370 break;
5371 case PREFIX_REP:
5372 if (current_templates->start->cpu_flags.bitfield.cpuhle)
5373 i.hle_prefix = current_templates->start->name;
5374 else if (current_templates->start->cpu_flags.bitfield.cpumpx)
5375 i.bnd_prefix = current_templates->start->name;
5376 else
5377 i.rep_prefix = current_templates->start->name;
5378 break;
5379 default:
5380 break;
86fa6981 5381 }
29b0f896
AM
5382 }
5383 /* Skip past PREFIX_SEPARATOR and reset token_start. */
5384 token_start = ++l;
5385 }
5386 else
5387 break;
5388 }
45288df1 5389
30a55f88 5390 if (!current_templates)
b6169b20 5391 {
07d5e953
JB
5392 /* Deprecated functionality (new code should use pseudo-prefixes instead):
5393 Check if we should swap operand or force 32bit displacement in
f8a5c266 5394 encoding. */
30a55f88 5395 if (mnem_p - 2 == dot_p && dot_p[1] == 's')
64c49ab3 5396 i.dir_encoding = dir_encoding_swap;
8d63c93e 5397 else if (mnem_p - 3 == dot_p
a501d77e
L
5398 && dot_p[1] == 'd'
5399 && dot_p[2] == '8')
5400 i.disp_encoding = disp_encoding_8bit;
8d63c93e 5401 else if (mnem_p - 4 == dot_p
f8a5c266
L
5402 && dot_p[1] == 'd'
5403 && dot_p[2] == '3'
5404 && dot_p[3] == '2')
a501d77e 5405 i.disp_encoding = disp_encoding_32bit;
30a55f88
L
5406 else
5407 goto check_suffix;
5408 mnem_p = dot_p;
5409 *dot_p = '\0';
629310ab 5410 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
b6169b20
L
5411 }
5412
29b0f896
AM
5413 if (!current_templates)
5414 {
dc1e8a47 5415 check_suffix:
1c529385 5416 if (mnem_p > mnemonic)
29b0f896 5417 {
1c529385
LH
5418 /* See if we can get a match by trimming off a suffix. */
5419 switch (mnem_p[-1])
29b0f896 5420 {
1c529385
LH
5421 case WORD_MNEM_SUFFIX:
5422 if (intel_syntax && (intel_float_operand (mnemonic) & 2))
29b0f896
AM
5423 i.suffix = SHORT_MNEM_SUFFIX;
5424 else
1c529385
LH
5425 /* Fall through. */
5426 case BYTE_MNEM_SUFFIX:
5427 case QWORD_MNEM_SUFFIX:
5428 i.suffix = mnem_p[-1];
29b0f896 5429 mnem_p[-1] = '\0';
fe0e921f
AM
5430 current_templates
5431 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5432 break;
5433 case SHORT_MNEM_SUFFIX:
5434 case LONG_MNEM_SUFFIX:
5435 if (!intel_syntax)
5436 {
5437 i.suffix = mnem_p[-1];
5438 mnem_p[-1] = '\0';
fe0e921f
AM
5439 current_templates
5440 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5441 }
5442 break;
5443
5444 /* Intel Syntax. */
5445 case 'd':
5446 if (intel_syntax)
5447 {
5448 if (intel_float_operand (mnemonic) == 1)
5449 i.suffix = SHORT_MNEM_SUFFIX;
5450 else
5451 i.suffix = LONG_MNEM_SUFFIX;
5452 mnem_p[-1] = '\0';
fe0e921f
AM
5453 current_templates
5454 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5455 }
5456 break;
29b0f896 5457 }
29b0f896 5458 }
1c529385 5459
29b0f896
AM
5460 if (!current_templates)
5461 {
5462 as_bad (_("no such instruction: `%s'"), token_start);
5463 return NULL;
5464 }
5465 }
252b5132 5466
0cfa3eb3
JB
5467 if (current_templates->start->opcode_modifier.jump == JUMP
5468 || current_templates->start->opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
5469 {
5470 /* Check for a branch hint. We allow ",pt" and ",pn" for
5471 predict taken and predict not taken respectively.
5472 I'm not sure that branch hints actually do anything on loop
5473 and jcxz insns (JumpByte) for current Pentium4 chips. They
5474 may work in the future and it doesn't hurt to accept them
5475 now. */
5476 if (l[0] == ',' && l[1] == 'p')
5477 {
5478 if (l[2] == 't')
5479 {
5480 if (!add_prefix (DS_PREFIX_OPCODE))
5481 return NULL;
5482 l += 3;
5483 }
5484 else if (l[2] == 'n')
5485 {
5486 if (!add_prefix (CS_PREFIX_OPCODE))
5487 return NULL;
5488 l += 3;
5489 }
5490 }
5491 }
5492 /* Any other comma loses. */
5493 if (*l == ',')
5494 {
5495 as_bad (_("invalid character %s in mnemonic"),
5496 output_invalid (*l));
5497 return NULL;
5498 }
252b5132 5499
29b0f896 5500 /* Check if instruction is supported on specified architecture. */
5c6af06e
JB
5501 supported = 0;
5502 for (t = current_templates->start; t < current_templates->end; ++t)
5503 {
c0f3af97
L
5504 supported |= cpu_flags_match (t);
5505 if (supported == CPU_FLAGS_PERFECT_MATCH)
548d0ee6
JB
5506 {
5507 if (!cpu_arch_flags.bitfield.cpui386 && (flag_code != CODE_16BIT))
5508 as_warn (_("use .code16 to ensure correct addressing mode"));
3629bb00 5509
548d0ee6
JB
5510 return l;
5511 }
29b0f896 5512 }
3629bb00 5513
548d0ee6
JB
5514 if (!(supported & CPU_FLAGS_64BIT_MATCH))
5515 as_bad (flag_code == CODE_64BIT
5516 ? _("`%s' is not supported in 64-bit mode")
5517 : _("`%s' is only supported in 64-bit mode"),
5518 current_templates->start->name);
5519 else
5520 as_bad (_("`%s' is not supported on `%s%s'"),
5521 current_templates->start->name,
5522 cpu_arch_name ? cpu_arch_name : default_arch,
5523 cpu_sub_arch_name ? cpu_sub_arch_name : "");
252b5132 5524
548d0ee6 5525 return NULL;
29b0f896 5526}
252b5132 5527
29b0f896 5528static char *
e3bb37b5 5529parse_operands (char *l, const char *mnemonic)
29b0f896
AM
5530{
5531 char *token_start;
3138f287 5532
29b0f896
AM
5533 /* 1 if operand is pending after ','. */
5534 unsigned int expecting_operand = 0;
252b5132 5535
29b0f896
AM
5536 /* Non-zero if operand parens not balanced. */
5537 unsigned int paren_not_balanced;
5538
5539 while (*l != END_OF_INSN)
5540 {
5541 /* Skip optional white space before operand. */
5542 if (is_space_char (*l))
5543 ++l;
d02603dc 5544 if (!is_operand_char (*l) && *l != END_OF_INSN && *l != '"')
29b0f896
AM
5545 {
5546 as_bad (_("invalid character %s before operand %d"),
5547 output_invalid (*l),
5548 i.operands + 1);
5549 return NULL;
5550 }
d02603dc 5551 token_start = l; /* After white space. */
29b0f896
AM
5552 paren_not_balanced = 0;
5553 while (paren_not_balanced || *l != ',')
5554 {
5555 if (*l == END_OF_INSN)
5556 {
5557 if (paren_not_balanced)
5558 {
98ff9f1c
JB
5559 know (!intel_syntax);
5560 as_bad (_("unbalanced parenthesis in operand %d."),
5561 i.operands + 1);
29b0f896
AM
5562 return NULL;
5563 }
5564 else
5565 break; /* we are done */
5566 }
d02603dc 5567 else if (!is_operand_char (*l) && !is_space_char (*l) && *l != '"')
29b0f896
AM
5568 {
5569 as_bad (_("invalid character %s in operand %d"),
5570 output_invalid (*l),
5571 i.operands + 1);
5572 return NULL;
5573 }
5574 if (!intel_syntax)
5575 {
5576 if (*l == '(')
5577 ++paren_not_balanced;
5578 if (*l == ')')
5579 --paren_not_balanced;
5580 }
29b0f896
AM
5581 l++;
5582 }
5583 if (l != token_start)
5584 { /* Yes, we've read in another operand. */
5585 unsigned int operand_ok;
5586 this_operand = i.operands++;
5587 if (i.operands > MAX_OPERANDS)
5588 {
5589 as_bad (_("spurious operands; (%d operands/instruction max)"),
5590 MAX_OPERANDS);
5591 return NULL;
5592 }
9d46ce34 5593 i.types[this_operand].bitfield.unspecified = 1;
29b0f896
AM
5594 /* Now parse operand adding info to 'i' as we go along. */
5595 END_STRING_AND_SAVE (l);
5596
1286ab78
L
5597 if (i.mem_operands > 1)
5598 {
5599 as_bad (_("too many memory references for `%s'"),
5600 mnemonic);
5601 return 0;
5602 }
5603
29b0f896
AM
5604 if (intel_syntax)
5605 operand_ok =
5606 i386_intel_operand (token_start,
5607 intel_float_operand (mnemonic));
5608 else
a7619375 5609 operand_ok = i386_att_operand (token_start);
29b0f896
AM
5610
5611 RESTORE_END_STRING (l);
5612 if (!operand_ok)
5613 return NULL;
5614 }
5615 else
5616 {
5617 if (expecting_operand)
5618 {
5619 expecting_operand_after_comma:
5620 as_bad (_("expecting operand after ','; got nothing"));
5621 return NULL;
5622 }
5623 if (*l == ',')
5624 {
5625 as_bad (_("expecting operand before ','; got nothing"));
5626 return NULL;
5627 }
5628 }
7f3f1ea2 5629
29b0f896
AM
5630 /* Now *l must be either ',' or END_OF_INSN. */
5631 if (*l == ',')
5632 {
5633 if (*++l == END_OF_INSN)
5634 {
5635 /* Just skip it, if it's \n complain. */
5636 goto expecting_operand_after_comma;
5637 }
5638 expecting_operand = 1;
5639 }
5640 }
5641 return l;
5642}
7f3f1ea2 5643
050dfa73 5644static void
783c187b 5645swap_2_operands (unsigned int xchg1, unsigned int xchg2)
050dfa73
MM
5646{
5647 union i386_op temp_op;
40fb9820 5648 i386_operand_type temp_type;
c48dadc9 5649 unsigned int temp_flags;
050dfa73 5650 enum bfd_reloc_code_real temp_reloc;
4eed87de 5651
050dfa73
MM
5652 temp_type = i.types[xchg2];
5653 i.types[xchg2] = i.types[xchg1];
5654 i.types[xchg1] = temp_type;
c48dadc9
JB
5655
5656 temp_flags = i.flags[xchg2];
5657 i.flags[xchg2] = i.flags[xchg1];
5658 i.flags[xchg1] = temp_flags;
5659
050dfa73
MM
5660 temp_op = i.op[xchg2];
5661 i.op[xchg2] = i.op[xchg1];
5662 i.op[xchg1] = temp_op;
c48dadc9 5663
050dfa73
MM
5664 temp_reloc = i.reloc[xchg2];
5665 i.reloc[xchg2] = i.reloc[xchg1];
5666 i.reloc[xchg1] = temp_reloc;
43234a1e 5667
6225c532 5668 if (i.mask.reg)
43234a1e 5669 {
6225c532
JB
5670 if (i.mask.operand == xchg1)
5671 i.mask.operand = xchg2;
5672 else if (i.mask.operand == xchg2)
5673 i.mask.operand = xchg1;
43234a1e 5674 }
5273a3cd 5675 if (i.broadcast.type)
43234a1e 5676 {
5273a3cd
JB
5677 if (i.broadcast.operand == xchg1)
5678 i.broadcast.operand = xchg2;
5679 else if (i.broadcast.operand == xchg2)
5680 i.broadcast.operand = xchg1;
43234a1e 5681 }
ca5312a2 5682 if (i.rounding.type != rc_none)
43234a1e 5683 {
ca5312a2
JB
5684 if (i.rounding.operand == xchg1)
5685 i.rounding.operand = xchg2;
5686 else if (i.rounding.operand == xchg2)
5687 i.rounding.operand = xchg1;
43234a1e 5688 }
050dfa73
MM
5689}
5690
29b0f896 5691static void
e3bb37b5 5692swap_operands (void)
29b0f896 5693{
b7c61d9a 5694 switch (i.operands)
050dfa73 5695 {
c0f3af97 5696 case 5:
b7c61d9a 5697 case 4:
4d456e3d 5698 swap_2_operands (1, i.operands - 2);
1a0670f3 5699 /* Fall through. */
b7c61d9a
L
5700 case 3:
5701 case 2:
4d456e3d 5702 swap_2_operands (0, i.operands - 1);
b7c61d9a
L
5703 break;
5704 default:
5705 abort ();
29b0f896 5706 }
29b0f896
AM
5707
5708 if (i.mem_operands == 2)
5709 {
5e042380 5710 const reg_entry *temp_seg;
29b0f896
AM
5711 temp_seg = i.seg[0];
5712 i.seg[0] = i.seg[1];
5713 i.seg[1] = temp_seg;
5714 }
5715}
252b5132 5716
29b0f896
AM
5717/* Try to ensure constant immediates are represented in the smallest
5718 opcode possible. */
5719static void
e3bb37b5 5720optimize_imm (void)
29b0f896
AM
5721{
5722 char guess_suffix = 0;
5723 int op;
252b5132 5724
29b0f896
AM
5725 if (i.suffix)
5726 guess_suffix = i.suffix;
5727 else if (i.reg_operands)
5728 {
5729 /* Figure out a suffix from the last register operand specified.
75e5731b
JB
5730 We can't do this properly yet, i.e. excluding special register
5731 instances, but the following works for instructions with
5732 immediates. In any case, we can't set i.suffix yet. */
29b0f896 5733 for (op = i.operands; --op >= 0;)
bab6aec1
JB
5734 if (i.types[op].bitfield.class != Reg)
5735 continue;
5736 else if (i.types[op].bitfield.byte)
7ab9ffdd 5737 {
40fb9820
L
5738 guess_suffix = BYTE_MNEM_SUFFIX;
5739 break;
5740 }
bab6aec1 5741 else if (i.types[op].bitfield.word)
252b5132 5742 {
40fb9820
L
5743 guess_suffix = WORD_MNEM_SUFFIX;
5744 break;
5745 }
bab6aec1 5746 else if (i.types[op].bitfield.dword)
40fb9820
L
5747 {
5748 guess_suffix = LONG_MNEM_SUFFIX;
5749 break;
5750 }
bab6aec1 5751 else if (i.types[op].bitfield.qword)
40fb9820
L
5752 {
5753 guess_suffix = QWORD_MNEM_SUFFIX;
29b0f896 5754 break;
252b5132 5755 }
29b0f896
AM
5756 }
5757 else if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
5758 guess_suffix = WORD_MNEM_SUFFIX;
5759
5760 for (op = i.operands; --op >= 0;)
40fb9820 5761 if (operand_type_check (i.types[op], imm))
29b0f896
AM
5762 {
5763 switch (i.op[op].imms->X_op)
252b5132 5764 {
29b0f896
AM
5765 case O_constant:
5766 /* If a suffix is given, this operand may be shortened. */
5767 switch (guess_suffix)
252b5132 5768 {
29b0f896 5769 case LONG_MNEM_SUFFIX:
40fb9820
L
5770 i.types[op].bitfield.imm32 = 1;
5771 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
5772 break;
5773 case WORD_MNEM_SUFFIX:
40fb9820
L
5774 i.types[op].bitfield.imm16 = 1;
5775 i.types[op].bitfield.imm32 = 1;
5776 i.types[op].bitfield.imm32s = 1;
5777 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
5778 break;
5779 case BYTE_MNEM_SUFFIX:
40fb9820
L
5780 i.types[op].bitfield.imm8 = 1;
5781 i.types[op].bitfield.imm8s = 1;
5782 i.types[op].bitfield.imm16 = 1;
5783 i.types[op].bitfield.imm32 = 1;
5784 i.types[op].bitfield.imm32s = 1;
5785 i.types[op].bitfield.imm64 = 1;
29b0f896 5786 break;
252b5132 5787 }
252b5132 5788
29b0f896
AM
5789 /* If this operand is at most 16 bits, convert it
5790 to a signed 16 bit number before trying to see
5791 whether it will fit in an even smaller size.
5792 This allows a 16-bit operand such as $0xffe0 to
5793 be recognised as within Imm8S range. */
40fb9820 5794 if ((i.types[op].bitfield.imm16)
29b0f896 5795 && (i.op[op].imms->X_add_number & ~(offsetT) 0xffff) == 0)
252b5132 5796 {
87ed972d
JB
5797 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
5798 ^ 0x8000) - 0x8000);
29b0f896 5799 }
a28def75
L
5800#ifdef BFD64
5801 /* Store 32-bit immediate in 64-bit for 64-bit BFD. */
40fb9820 5802 if ((i.types[op].bitfield.imm32)
29b0f896
AM
5803 && ((i.op[op].imms->X_add_number & ~(((offsetT) 2 << 31) - 1))
5804 == 0))
5805 {
5806 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
5807 ^ ((offsetT) 1 << 31))
5808 - ((offsetT) 1 << 31));
5809 }
a28def75 5810#endif
40fb9820 5811 i.types[op]
c6fb90c8
L
5812 = operand_type_or (i.types[op],
5813 smallest_imm_type (i.op[op].imms->X_add_number));
252b5132 5814
29b0f896
AM
5815 /* We must avoid matching of Imm32 templates when 64bit
5816 only immediate is available. */
5817 if (guess_suffix == QWORD_MNEM_SUFFIX)
40fb9820 5818 i.types[op].bitfield.imm32 = 0;
29b0f896 5819 break;
252b5132 5820
29b0f896
AM
5821 case O_absent:
5822 case O_register:
5823 abort ();
5824
5825 /* Symbols and expressions. */
5826 default:
9cd96992
JB
5827 /* Convert symbolic operand to proper sizes for matching, but don't
5828 prevent matching a set of insns that only supports sizes other
5829 than those matching the insn suffix. */
5830 {
40fb9820 5831 i386_operand_type mask, allowed;
87ed972d 5832 const insn_template *t = current_templates->start;
9cd96992 5833
0dfbf9d7 5834 operand_type_set (&mask, 0);
87ed972d 5835 allowed = t->operand_types[op];
40fb9820 5836
87ed972d 5837 while (++t < current_templates->end)
bab6aec1 5838 {
bab6aec1 5839 allowed = operand_type_and (allowed, anyimm);
87ed972d 5840 allowed = operand_type_or (allowed, t->operand_types[op]);
bab6aec1 5841 }
9cd96992
JB
5842 switch (guess_suffix)
5843 {
5844 case QWORD_MNEM_SUFFIX:
40fb9820
L
5845 mask.bitfield.imm64 = 1;
5846 mask.bitfield.imm32s = 1;
9cd96992
JB
5847 break;
5848 case LONG_MNEM_SUFFIX:
40fb9820 5849 mask.bitfield.imm32 = 1;
9cd96992
JB
5850 break;
5851 case WORD_MNEM_SUFFIX:
40fb9820 5852 mask.bitfield.imm16 = 1;
9cd96992
JB
5853 break;
5854 case BYTE_MNEM_SUFFIX:
40fb9820 5855 mask.bitfield.imm8 = 1;
9cd96992
JB
5856 break;
5857 default:
9cd96992
JB
5858 break;
5859 }
c6fb90c8 5860 allowed = operand_type_and (mask, allowed);
0dfbf9d7 5861 if (!operand_type_all_zero (&allowed))
c6fb90c8 5862 i.types[op] = operand_type_and (i.types[op], mask);
9cd96992 5863 }
29b0f896 5864 break;
252b5132 5865 }
29b0f896
AM
5866 }
5867}
47926f60 5868
29b0f896
AM
5869/* Try to use the smallest displacement type too. */
5870static void
e3bb37b5 5871optimize_disp (void)
29b0f896
AM
5872{
5873 int op;
3e73aa7c 5874
29b0f896 5875 for (op = i.operands; --op >= 0;)
40fb9820 5876 if (operand_type_check (i.types[op], disp))
252b5132 5877 {
b300c311 5878 if (i.op[op].disps->X_op == O_constant)
252b5132 5879 {
91d6fa6a 5880 offsetT op_disp = i.op[op].disps->X_add_number;
29b0f896 5881
40fb9820 5882 if (i.types[op].bitfield.disp16
91d6fa6a 5883 && (op_disp & ~(offsetT) 0xffff) == 0)
b300c311
L
5884 {
5885 /* If this operand is at most 16 bits, convert
5886 to a signed 16 bit number and don't use 64bit
5887 displacement. */
91d6fa6a 5888 op_disp = (((op_disp & 0xffff) ^ 0x8000) - 0x8000);
40fb9820 5889 i.types[op].bitfield.disp64 = 0;
b300c311 5890 }
91d6fa6a 5891 if (!op_disp && i.types[op].bitfield.baseindex)
b300c311 5892 {
40fb9820
L
5893 i.types[op].bitfield.disp8 = 0;
5894 i.types[op].bitfield.disp16 = 0;
5895 i.types[op].bitfield.disp32 = 0;
5896 i.types[op].bitfield.disp32s = 0;
5897 i.types[op].bitfield.disp64 = 0;
b300c311
L
5898 i.op[op].disps = 0;
5899 i.disp_operands--;
5900 }
28a167a4 5901#ifdef BFD64
b300c311
L
5902 else if (flag_code == CODE_64BIT)
5903 {
a9aabc23 5904 if (want_disp32 (current_templates->start)
28a167a4
JB
5905 && fits_in_unsigned_long (op_disp))
5906 i.types[op].bitfield.disp32 = 1;
5907
5908 /* Optimize 64-bit displacement to 32-bit for 64-bit BFD. */
5909 if (i.types[op].bitfield.disp32
5910 && (op_disp & ~(((offsetT) 2 << 31) - 1)) == 0)
5911 {
5912 /* If this operand is at most 32 bits, convert
5913 to a signed 32 bit number and don't use 64bit
5914 displacement. */
5915 op_disp &= (((offsetT) 2 << 31) - 1);
5916 op_disp = (op_disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
5917 i.types[op].bitfield.disp64 = 0;
5918 }
5919
91d6fa6a 5920 if (fits_in_signed_long (op_disp))
28a9d8f5 5921 {
40fb9820
L
5922 i.types[op].bitfield.disp64 = 0;
5923 i.types[op].bitfield.disp32s = 1;
28a9d8f5 5924 }
b300c311 5925 }
28a167a4 5926#endif
40fb9820
L
5927 if ((i.types[op].bitfield.disp32
5928 || i.types[op].bitfield.disp32s
5929 || i.types[op].bitfield.disp16)
b5014f7a 5930 && fits_in_disp8 (op_disp))
40fb9820 5931 i.types[op].bitfield.disp8 = 1;
252b5132 5932 }
67a4f2b7
AO
5933 else if (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
5934 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL)
5935 {
5936 fix_new_exp (frag_now, frag_more (0) - frag_now->fr_literal, 0,
5937 i.op[op].disps, 0, i.reloc[op]);
40fb9820
L
5938 i.types[op].bitfield.disp8 = 0;
5939 i.types[op].bitfield.disp16 = 0;
5940 i.types[op].bitfield.disp32 = 0;
5941 i.types[op].bitfield.disp32s = 0;
5942 i.types[op].bitfield.disp64 = 0;
67a4f2b7
AO
5943 }
5944 else
b300c311 5945 /* We only support 64bit displacement on constants. */
40fb9820 5946 i.types[op].bitfield.disp64 = 0;
252b5132 5947 }
29b0f896
AM
5948}
5949
4a1b91ea
L
5950/* Return 1 if there is a match in broadcast bytes between operand
5951 GIVEN and instruction template T. */
5952
5953static INLINE int
5954match_broadcast_size (const insn_template *t, unsigned int given)
5955{
5956 return ((t->opcode_modifier.broadcast == BYTE_BROADCAST
5957 && i.types[given].bitfield.byte)
5958 || (t->opcode_modifier.broadcast == WORD_BROADCAST
5959 && i.types[given].bitfield.word)
5960 || (t->opcode_modifier.broadcast == DWORD_BROADCAST
5961 && i.types[given].bitfield.dword)
5962 || (t->opcode_modifier.broadcast == QWORD_BROADCAST
5963 && i.types[given].bitfield.qword));
5964}
5965
6c30d220
L
5966/* Check if operands are valid for the instruction. */
5967
5968static int
5969check_VecOperands (const insn_template *t)
5970{
43234a1e 5971 unsigned int op;
e2195274 5972 i386_cpu_flags cpu;
e2195274
JB
5973
5974 /* Templates allowing for ZMMword as well as YMMword and/or XMMword for
5975 any one operand are implicity requiring AVX512VL support if the actual
5976 operand size is YMMword or XMMword. Since this function runs after
5977 template matching, there's no need to check for YMMword/XMMword in
5978 the template. */
5979 cpu = cpu_flags_and (t->cpu_flags, avx512);
5980 if (!cpu_flags_all_zero (&cpu)
5981 && !t->cpu_flags.bitfield.cpuavx512vl
5982 && !cpu_arch_flags.bitfield.cpuavx512vl)
5983 {
5984 for (op = 0; op < t->operands; ++op)
5985 {
5986 if (t->operand_types[op].bitfield.zmmword
5987 && (i.types[op].bitfield.ymmword
5988 || i.types[op].bitfield.xmmword))
5989 {
5990 i.error = unsupported;
5991 return 1;
5992 }
5993 }
5994 }
43234a1e 5995
6c30d220 5996 /* Without VSIB byte, we can't have a vector register for index. */
63112cd6 5997 if (!t->opcode_modifier.sib
6c30d220 5998 && i.index_reg
1b54b8d7
JB
5999 && (i.index_reg->reg_type.bitfield.xmmword
6000 || i.index_reg->reg_type.bitfield.ymmword
6001 || i.index_reg->reg_type.bitfield.zmmword))
6c30d220
L
6002 {
6003 i.error = unsupported_vector_index_register;
6004 return 1;
6005 }
6006
ad8ecc81
MZ
6007 /* Check if default mask is allowed. */
6008 if (t->opcode_modifier.nodefmask
6225c532 6009 && (!i.mask.reg || i.mask.reg->reg_num == 0))
ad8ecc81
MZ
6010 {
6011 i.error = no_default_mask;
6012 return 1;
6013 }
6014
7bab8ab5
JB
6015 /* For VSIB byte, we need a vector register for index, and all vector
6016 registers must be distinct. */
260cd341 6017 if (t->opcode_modifier.sib && t->opcode_modifier.sib != SIBMEM)
7bab8ab5
JB
6018 {
6019 if (!i.index_reg
63112cd6 6020 || !((t->opcode_modifier.sib == VECSIB128
1b54b8d7 6021 && i.index_reg->reg_type.bitfield.xmmword)
63112cd6 6022 || (t->opcode_modifier.sib == VECSIB256
1b54b8d7 6023 && i.index_reg->reg_type.bitfield.ymmword)
63112cd6 6024 || (t->opcode_modifier.sib == VECSIB512
1b54b8d7 6025 && i.index_reg->reg_type.bitfield.zmmword)))
7bab8ab5
JB
6026 {
6027 i.error = invalid_vsib_address;
6028 return 1;
6029 }
6030
6225c532
JB
6031 gas_assert (i.reg_operands == 2 || i.mask.reg);
6032 if (i.reg_operands == 2 && !i.mask.reg)
43234a1e 6033 {
3528c362 6034 gas_assert (i.types[0].bitfield.class == RegSIMD);
1b54b8d7
JB
6035 gas_assert (i.types[0].bitfield.xmmword
6036 || i.types[0].bitfield.ymmword);
3528c362 6037 gas_assert (i.types[2].bitfield.class == RegSIMD);
1b54b8d7
JB
6038 gas_assert (i.types[2].bitfield.xmmword
6039 || i.types[2].bitfield.ymmword);
43234a1e
L
6040 if (operand_check == check_none)
6041 return 0;
6042 if (register_number (i.op[0].regs)
6043 != register_number (i.index_reg)
6044 && register_number (i.op[2].regs)
6045 != register_number (i.index_reg)
6046 && register_number (i.op[0].regs)
6047 != register_number (i.op[2].regs))
6048 return 0;
6049 if (operand_check == check_error)
6050 {
6051 i.error = invalid_vector_register_set;
6052 return 1;
6053 }
6054 as_warn (_("mask, index, and destination registers should be distinct"));
6055 }
6225c532 6056 else if (i.reg_operands == 1 && i.mask.reg)
8444f82a 6057 {
3528c362 6058 if (i.types[1].bitfield.class == RegSIMD
1b54b8d7
JB
6059 && (i.types[1].bitfield.xmmword
6060 || i.types[1].bitfield.ymmword
6061 || i.types[1].bitfield.zmmword)
8444f82a
MZ
6062 && (register_number (i.op[1].regs)
6063 == register_number (i.index_reg)))
6064 {
6065 if (operand_check == check_error)
6066 {
6067 i.error = invalid_vector_register_set;
6068 return 1;
6069 }
6070 if (operand_check != check_none)
6071 as_warn (_("index and destination registers should be distinct"));
6072 }
6073 }
43234a1e 6074 }
7bab8ab5 6075
260cd341
LC
6076 /* For AMX instructions with three tmmword operands, all tmmword operand must be
6077 distinct */
6078 if (t->operand_types[0].bitfield.tmmword
6079 && i.reg_operands == 3)
6080 {
6081 if (register_number (i.op[0].regs)
6082 == register_number (i.op[1].regs)
6083 || register_number (i.op[0].regs)
6084 == register_number (i.op[2].regs)
6085 || register_number (i.op[1].regs)
6086 == register_number (i.op[2].regs))
6087 {
6088 i.error = invalid_tmm_register_set;
6089 return 1;
6090 }
6091 }
6092
43234a1e
L
6093 /* Check if broadcast is supported by the instruction and is applied
6094 to the memory operand. */
5273a3cd 6095 if (i.broadcast.type)
43234a1e 6096 {
8e6e0792 6097 i386_operand_type type, overlap;
43234a1e
L
6098
6099 /* Check if specified broadcast is supported in this instruction,
4a1b91ea 6100 and its broadcast bytes match the memory operand. */
5273a3cd 6101 op = i.broadcast.operand;
8e6e0792 6102 if (!t->opcode_modifier.broadcast
c48dadc9 6103 || !(i.flags[op] & Operand_Mem)
c39e5b26 6104 || (!i.types[op].bitfield.unspecified
4a1b91ea 6105 && !match_broadcast_size (t, op)))
43234a1e
L
6106 {
6107 bad_broadcast:
6108 i.error = unsupported_broadcast;
6109 return 1;
6110 }
8e6e0792 6111
5273a3cd
JB
6112 i.broadcast.bytes = ((1 << (t->opcode_modifier.broadcast - 1))
6113 * i.broadcast.type);
8e6e0792 6114 operand_type_set (&type, 0);
5273a3cd 6115 switch (i.broadcast.bytes)
8e6e0792 6116 {
4a1b91ea
L
6117 case 2:
6118 type.bitfield.word = 1;
6119 break;
6120 case 4:
6121 type.bitfield.dword = 1;
6122 break;
8e6e0792
JB
6123 case 8:
6124 type.bitfield.qword = 1;
6125 break;
6126 case 16:
6127 type.bitfield.xmmword = 1;
6128 break;
6129 case 32:
6130 type.bitfield.ymmword = 1;
6131 break;
6132 case 64:
6133 type.bitfield.zmmword = 1;
6134 break;
6135 default:
6136 goto bad_broadcast;
6137 }
6138
6139 overlap = operand_type_and (type, t->operand_types[op]);
bc49bfd8
JB
6140 if (t->operand_types[op].bitfield.class == RegSIMD
6141 && t->operand_types[op].bitfield.byte
6142 + t->operand_types[op].bitfield.word
6143 + t->operand_types[op].bitfield.dword
6144 + t->operand_types[op].bitfield.qword > 1)
6145 {
6146 overlap.bitfield.xmmword = 0;
6147 overlap.bitfield.ymmword = 0;
6148 overlap.bitfield.zmmword = 0;
6149 }
8e6e0792
JB
6150 if (operand_type_all_zero (&overlap))
6151 goto bad_broadcast;
6152
6153 if (t->opcode_modifier.checkregsize)
6154 {
6155 unsigned int j;
6156
e2195274 6157 type.bitfield.baseindex = 1;
8e6e0792
JB
6158 for (j = 0; j < i.operands; ++j)
6159 {
6160 if (j != op
6161 && !operand_type_register_match(i.types[j],
6162 t->operand_types[j],
6163 type,
6164 t->operand_types[op]))
6165 goto bad_broadcast;
6166 }
6167 }
43234a1e
L
6168 }
6169 /* If broadcast is supported in this instruction, we need to check if
6170 operand of one-element size isn't specified without broadcast. */
6171 else if (t->opcode_modifier.broadcast && i.mem_operands)
6172 {
6173 /* Find memory operand. */
6174 for (op = 0; op < i.operands; op++)
8dc0818e 6175 if (i.flags[op] & Operand_Mem)
43234a1e
L
6176 break;
6177 gas_assert (op < i.operands);
6178 /* Check size of the memory operand. */
4a1b91ea 6179 if (match_broadcast_size (t, op))
43234a1e
L
6180 {
6181 i.error = broadcast_needed;
6182 return 1;
6183 }
6184 }
c39e5b26
JB
6185 else
6186 op = MAX_OPERANDS - 1; /* Avoid uninitialized variable warning. */
43234a1e
L
6187
6188 /* Check if requested masking is supported. */
6225c532 6189 if (i.mask.reg)
43234a1e 6190 {
ae2387fe
JB
6191 switch (t->opcode_modifier.masking)
6192 {
6193 case BOTH_MASKING:
6194 break;
6195 case MERGING_MASKING:
6225c532 6196 if (i.mask.zeroing)
ae2387fe
JB
6197 {
6198 case 0:
6199 i.error = unsupported_masking;
6200 return 1;
6201 }
6202 break;
6203 case DYNAMIC_MASKING:
6204 /* Memory destinations allow only merging masking. */
6225c532 6205 if (i.mask.zeroing && i.mem_operands)
ae2387fe
JB
6206 {
6207 /* Find memory operand. */
6208 for (op = 0; op < i.operands; op++)
c48dadc9 6209 if (i.flags[op] & Operand_Mem)
ae2387fe
JB
6210 break;
6211 gas_assert (op < i.operands);
6212 if (op == i.operands - 1)
6213 {
6214 i.error = unsupported_masking;
6215 return 1;
6216 }
6217 }
6218 break;
6219 default:
6220 abort ();
6221 }
43234a1e
L
6222 }
6223
6224 /* Check if masking is applied to dest operand. */
6225c532 6225 if (i.mask.reg && (i.mask.operand != i.operands - 1))
43234a1e
L
6226 {
6227 i.error = mask_not_on_destination;
6228 return 1;
6229 }
6230
43234a1e 6231 /* Check RC/SAE. */
ca5312a2 6232 if (i.rounding.type != rc_none)
43234a1e 6233 {
a80195f1 6234 if (!t->opcode_modifier.sae
ca5312a2 6235 || (i.rounding.type != saeonly && !t->opcode_modifier.staticrounding))
43234a1e
L
6236 {
6237 i.error = unsupported_rc_sae;
6238 return 1;
6239 }
6240 /* If the instruction has several immediate operands and one of
6241 them is rounding, the rounding operand should be the last
6242 immediate operand. */
6243 if (i.imm_operands > 1
ca5312a2 6244 && i.rounding.operand != i.imm_operands - 1)
7bab8ab5 6245 {
43234a1e 6246 i.error = rc_sae_operand_not_last_imm;
7bab8ab5
JB
6247 return 1;
6248 }
6c30d220
L
6249 }
6250
da4977e0
JB
6251 /* Check the special Imm4 cases; must be the first operand. */
6252 if (t->cpu_flags.bitfield.cpuxop && t->operands == 5)
6253 {
6254 if (i.op[0].imms->X_op != O_constant
6255 || !fits_in_imm4 (i.op[0].imms->X_add_number))
6256 {
6257 i.error = bad_imm4;
6258 return 1;
6259 }
6260
6261 /* Turn off Imm<N> so that update_imm won't complain. */
6262 operand_type_set (&i.types[0], 0);
6263 }
6264
43234a1e 6265 /* Check vector Disp8 operand. */
b5014f7a
JB
6266 if (t->opcode_modifier.disp8memshift
6267 && i.disp_encoding != disp_encoding_32bit)
43234a1e 6268 {
5273a3cd 6269 if (i.broadcast.type)
4a1b91ea 6270 i.memshift = t->opcode_modifier.broadcast - 1;
7091c612 6271 else if (t->opcode_modifier.disp8memshift != DISP8_SHIFT_VL)
43234a1e 6272 i.memshift = t->opcode_modifier.disp8memshift;
7091c612
JB
6273 else
6274 {
6275 const i386_operand_type *type = NULL;
6276
6277 i.memshift = 0;
6278 for (op = 0; op < i.operands; op++)
8dc0818e 6279 if (i.flags[op] & Operand_Mem)
7091c612 6280 {
4174bfff
JB
6281 if (t->opcode_modifier.evex == EVEXLIG)
6282 i.memshift = 2 + (i.suffix == QWORD_MNEM_SUFFIX);
6283 else if (t->operand_types[op].bitfield.xmmword
6284 + t->operand_types[op].bitfield.ymmword
6285 + t->operand_types[op].bitfield.zmmword <= 1)
7091c612
JB
6286 type = &t->operand_types[op];
6287 else if (!i.types[op].bitfield.unspecified)
6288 type = &i.types[op];
6289 }
3528c362 6290 else if (i.types[op].bitfield.class == RegSIMD
4174bfff 6291 && t->opcode_modifier.evex != EVEXLIG)
7091c612
JB
6292 {
6293 if (i.types[op].bitfield.zmmword)
6294 i.memshift = 6;
6295 else if (i.types[op].bitfield.ymmword && i.memshift < 5)
6296 i.memshift = 5;
6297 else if (i.types[op].bitfield.xmmword && i.memshift < 4)
6298 i.memshift = 4;
6299 }
6300
6301 if (type)
6302 {
6303 if (type->bitfield.zmmword)
6304 i.memshift = 6;
6305 else if (type->bitfield.ymmword)
6306 i.memshift = 5;
6307 else if (type->bitfield.xmmword)
6308 i.memshift = 4;
6309 }
6310
6311 /* For the check in fits_in_disp8(). */
6312 if (i.memshift == 0)
6313 i.memshift = -1;
6314 }
43234a1e
L
6315
6316 for (op = 0; op < i.operands; op++)
6317 if (operand_type_check (i.types[op], disp)
6318 && i.op[op].disps->X_op == O_constant)
6319 {
b5014f7a 6320 if (fits_in_disp8 (i.op[op].disps->X_add_number))
43234a1e 6321 {
b5014f7a
JB
6322 i.types[op].bitfield.disp8 = 1;
6323 return 0;
43234a1e 6324 }
b5014f7a 6325 i.types[op].bitfield.disp8 = 0;
43234a1e
L
6326 }
6327 }
b5014f7a
JB
6328
6329 i.memshift = 0;
43234a1e 6330
6c30d220
L
6331 return 0;
6332}
6333
da4977e0 6334/* Check if encoding requirements are met by the instruction. */
a683cc34
SP
6335
6336static int
da4977e0 6337VEX_check_encoding (const insn_template *t)
a683cc34 6338{
da4977e0
JB
6339 if (i.vec_encoding == vex_encoding_error)
6340 {
6341 i.error = unsupported;
6342 return 1;
6343 }
6344
86fa6981 6345 if (i.vec_encoding == vex_encoding_evex)
43234a1e 6346 {
86fa6981 6347 /* This instruction must be encoded with EVEX prefix. */
e771e7c9 6348 if (!is_evex_encoding (t))
86fa6981
L
6349 {
6350 i.error = unsupported;
6351 return 1;
6352 }
6353 return 0;
43234a1e
L
6354 }
6355
a683cc34 6356 if (!t->opcode_modifier.vex)
86fa6981
L
6357 {
6358 /* This instruction template doesn't have VEX prefix. */
6359 if (i.vec_encoding != vex_encoding_default)
6360 {
6361 i.error = unsupported;
6362 return 1;
6363 }
6364 return 0;
6365 }
a683cc34 6366
a683cc34
SP
6367 return 0;
6368}
6369
d3ce72d0 6370static const insn_template *
83b16ac6 6371match_template (char mnem_suffix)
29b0f896
AM
6372{
6373 /* Points to template once we've found it. */
d3ce72d0 6374 const insn_template *t;
40fb9820 6375 i386_operand_type overlap0, overlap1, overlap2, overlap3;
c0f3af97 6376 i386_operand_type overlap4;
29b0f896 6377 unsigned int found_reverse_match;
dc2be329 6378 i386_opcode_modifier suffix_check;
40fb9820 6379 i386_operand_type operand_types [MAX_OPERANDS];
539e75ad 6380 int addr_prefix_disp;
45a4bb20 6381 unsigned int j, size_match, check_register;
5614d22c 6382 enum i386_error specific_error = 0;
29b0f896 6383
c0f3af97
L
6384#if MAX_OPERANDS != 5
6385# error "MAX_OPERANDS must be 5."
f48ff2ae
L
6386#endif
6387
29b0f896 6388 found_reverse_match = 0;
539e75ad 6389 addr_prefix_disp = -1;
40fb9820 6390
dc2be329 6391 /* Prepare for mnemonic suffix check. */
40fb9820 6392 memset (&suffix_check, 0, sizeof (suffix_check));
dc2be329
L
6393 switch (mnem_suffix)
6394 {
6395 case BYTE_MNEM_SUFFIX:
6396 suffix_check.no_bsuf = 1;
6397 break;
6398 case WORD_MNEM_SUFFIX:
6399 suffix_check.no_wsuf = 1;
6400 break;
6401 case SHORT_MNEM_SUFFIX:
6402 suffix_check.no_ssuf = 1;
6403 break;
6404 case LONG_MNEM_SUFFIX:
6405 suffix_check.no_lsuf = 1;
6406 break;
6407 case QWORD_MNEM_SUFFIX:
6408 suffix_check.no_qsuf = 1;
6409 break;
6410 default:
6411 /* NB: In Intel syntax, normally we can check for memory operand
6412 size when there is no mnemonic suffix. But jmp and call have
6413 2 different encodings with Dword memory operand size, one with
6414 No_ldSuf and the other without. i.suffix is set to
6415 LONG_DOUBLE_MNEM_SUFFIX to skip the one with No_ldSuf. */
6416 if (i.suffix == LONG_DOUBLE_MNEM_SUFFIX)
6417 suffix_check.no_ldsuf = 1;
83b16ac6
JB
6418 }
6419
01559ecc
L
6420 /* Must have right number of operands. */
6421 i.error = number_of_operands_mismatch;
6422
45aa61fe 6423 for (t = current_templates->start; t < current_templates->end; t++)
29b0f896 6424 {
539e75ad 6425 addr_prefix_disp = -1;
dbbc8b7e 6426 found_reverse_match = 0;
539e75ad 6427
29b0f896
AM
6428 if (i.operands != t->operands)
6429 continue;
6430
50aecf8c 6431 /* Check processor support. */
a65babc9 6432 i.error = unsupported;
45a4bb20 6433 if (cpu_flags_match (t) != CPU_FLAGS_PERFECT_MATCH)
50aecf8c
L
6434 continue;
6435
57392598
CL
6436 /* Check Pseudo Prefix. */
6437 i.error = unsupported;
6438 if (t->opcode_modifier.pseudovexprefix
6439 && !(i.vec_encoding == vex_encoding_vex
6440 || i.vec_encoding == vex_encoding_vex3))
6441 continue;
6442
e1d4d893 6443 /* Check AT&T mnemonic. */
a65babc9 6444 i.error = unsupported_with_intel_mnemonic;
e1d4d893 6445 if (intel_mnemonic && t->opcode_modifier.attmnemonic)
1efbbeb4
L
6446 continue;
6447
4b5aaf5f 6448 /* Check AT&T/Intel syntax. */
a65babc9 6449 i.error = unsupported_syntax;
5c07affc 6450 if ((intel_syntax && t->opcode_modifier.attsyntax)
4b5aaf5f 6451 || (!intel_syntax && t->opcode_modifier.intelsyntax))
1efbbeb4
L
6452 continue;
6453
4b5aaf5f
L
6454 /* Check Intel64/AMD64 ISA. */
6455 switch (isa64)
6456 {
6457 default:
6458 /* Default: Don't accept Intel64. */
6459 if (t->opcode_modifier.isa64 == INTEL64)
6460 continue;
6461 break;
6462 case amd64:
6463 /* -mamd64: Don't accept Intel64 and Intel64 only. */
6464 if (t->opcode_modifier.isa64 >= INTEL64)
6465 continue;
6466 break;
6467 case intel64:
6468 /* -mintel64: Don't accept AMD64. */
5990e377 6469 if (t->opcode_modifier.isa64 == AMD64 && flag_code == CODE_64BIT)
4b5aaf5f
L
6470 continue;
6471 break;
6472 }
6473
dc2be329 6474 /* Check the suffix. */
a65babc9 6475 i.error = invalid_instruction_suffix;
dc2be329
L
6476 if ((t->opcode_modifier.no_bsuf && suffix_check.no_bsuf)
6477 || (t->opcode_modifier.no_wsuf && suffix_check.no_wsuf)
6478 || (t->opcode_modifier.no_lsuf && suffix_check.no_lsuf)
6479 || (t->opcode_modifier.no_ssuf && suffix_check.no_ssuf)
6480 || (t->opcode_modifier.no_qsuf && suffix_check.no_qsuf)
6481 || (t->opcode_modifier.no_ldsuf && suffix_check.no_ldsuf))
83b16ac6 6482 continue;
29b0f896 6483
3ac21baa
JB
6484 size_match = operand_size_match (t);
6485 if (!size_match)
7d5e4556 6486 continue;
539e75ad 6487
6f2f06be
JB
6488 /* This is intentionally not
6489
0cfa3eb3 6490 if (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE))
6f2f06be
JB
6491
6492 as the case of a missing * on the operand is accepted (perhaps with
6493 a warning, issued further down). */
0cfa3eb3 6494 if (i.jumpabsolute && t->opcode_modifier.jump != JUMP_ABSOLUTE)
6f2f06be
JB
6495 {
6496 i.error = operand_type_mismatch;
6497 continue;
6498 }
6499
5c07affc
L
6500 for (j = 0; j < MAX_OPERANDS; j++)
6501 operand_types[j] = t->operand_types[j];
6502
e365e234
JB
6503 /* In general, don't allow
6504 - 64-bit operands outside of 64-bit mode,
6505 - 32-bit operands on pre-386. */
4873e243 6506 j = i.imm_operands + (t->operands > i.imm_operands + 1);
e365e234
JB
6507 if (((i.suffix == QWORD_MNEM_SUFFIX
6508 && flag_code != CODE_64BIT
389d00a5
JB
6509 && !(t->opcode_modifier.opcodespace == SPACE_0F
6510 && t->base_opcode == 0xc7
5e74b495 6511 && t->opcode_modifier.opcodeprefix == PREFIX_NONE
8b65b895 6512 && t->extension_opcode == 1) /* cmpxchg8b */)
e365e234
JB
6513 || (i.suffix == LONG_MNEM_SUFFIX
6514 && !cpu_arch_flags.bitfield.cpui386))
45aa61fe 6515 && (intel_syntax
3cd7f3e3 6516 ? (t->opcode_modifier.mnemonicsize != IGNORESIZE
45aa61fe
AM
6517 && !intel_float_operand (t->name))
6518 : intel_float_operand (t->name) != 2)
4873e243
JB
6519 && (t->operands == i.imm_operands
6520 || (operand_types[i.imm_operands].bitfield.class != RegMMX
6521 && operand_types[i.imm_operands].bitfield.class != RegSIMD
6522 && operand_types[i.imm_operands].bitfield.class != RegMask)
6523 || (operand_types[j].bitfield.class != RegMMX
6524 && operand_types[j].bitfield.class != RegSIMD
6525 && operand_types[j].bitfield.class != RegMask))
63112cd6 6526 && !t->opcode_modifier.sib)
192dc9c6
JB
6527 continue;
6528
29b0f896 6529 /* Do not verify operands when there are none. */
e365e234 6530 if (!t->operands)
da4977e0
JB
6531 {
6532 if (VEX_check_encoding (t))
6533 {
6534 specific_error = i.error;
6535 continue;
6536 }
6537
6538 /* We've found a match; break out of loop. */
6539 break;
6540 }
252b5132 6541
48bcea9f
JB
6542 if (!t->opcode_modifier.jump
6543 || t->opcode_modifier.jump == JUMP_ABSOLUTE)
6544 {
6545 /* There should be only one Disp operand. */
6546 for (j = 0; j < MAX_OPERANDS; j++)
6547 if (operand_type_check (operand_types[j], disp))
539e75ad 6548 break;
48bcea9f
JB
6549 if (j < MAX_OPERANDS)
6550 {
5b7c81bd 6551 bool override = (i.prefix[ADDR_PREFIX] != 0);
48bcea9f
JB
6552
6553 addr_prefix_disp = j;
6554
6555 /* Address size prefix will turn Disp64/Disp32S/Disp32/Disp16
6556 operand into Disp32/Disp32/Disp16/Disp32 operand. */
6557 switch (flag_code)
40fb9820 6558 {
48bcea9f
JB
6559 case CODE_16BIT:
6560 override = !override;
6561 /* Fall through. */
6562 case CODE_32BIT:
6563 if (operand_types[j].bitfield.disp32
6564 && operand_types[j].bitfield.disp16)
40fb9820 6565 {
48bcea9f
JB
6566 operand_types[j].bitfield.disp16 = override;
6567 operand_types[j].bitfield.disp32 = !override;
40fb9820 6568 }
48bcea9f
JB
6569 operand_types[j].bitfield.disp32s = 0;
6570 operand_types[j].bitfield.disp64 = 0;
6571 break;
6572
6573 case CODE_64BIT:
6574 if (operand_types[j].bitfield.disp32s
6575 || operand_types[j].bitfield.disp64)
40fb9820 6576 {
48bcea9f
JB
6577 operand_types[j].bitfield.disp64 &= !override;
6578 operand_types[j].bitfield.disp32s &= !override;
6579 operand_types[j].bitfield.disp32 = override;
40fb9820 6580 }
48bcea9f
JB
6581 operand_types[j].bitfield.disp16 = 0;
6582 break;
40fb9820 6583 }
539e75ad 6584 }
48bcea9f 6585 }
539e75ad 6586
02a86693 6587 /* Force 0x8b encoding for "mov foo@GOT, %eax". */
35648716
JB
6588 if (i.reloc[0] == BFD_RELOC_386_GOT32
6589 && t->base_opcode == 0xa0
6590 && t->opcode_modifier.opcodespace == SPACE_BASE)
02a86693
L
6591 continue;
6592
56ffb741 6593 /* We check register size if needed. */
e2195274
JB
6594 if (t->opcode_modifier.checkregsize)
6595 {
6596 check_register = (1 << t->operands) - 1;
5273a3cd
JB
6597 if (i.broadcast.type)
6598 check_register &= ~(1 << i.broadcast.operand);
e2195274
JB
6599 }
6600 else
6601 check_register = 0;
6602
c6fb90c8 6603 overlap0 = operand_type_and (i.types[0], operand_types[0]);
29b0f896
AM
6604 switch (t->operands)
6605 {
6606 case 1:
40fb9820 6607 if (!operand_type_match (overlap0, i.types[0]))
29b0f896
AM
6608 continue;
6609 break;
6610 case 2:
33eaf5de 6611 /* xchg %eax, %eax is a special case. It is an alias for nop
8b38ad71
L
6612 only in 32bit mode and we can use opcode 0x90. In 64bit
6613 mode, we can't use 0x90 for xchg %eax, %eax since it should
6614 zero-extend %eax to %rax. */
6615 if (flag_code == CODE_64BIT
6616 && t->base_opcode == 0x90
35648716 6617 && t->opcode_modifier.opcodespace == SPACE_BASE
75e5731b
JB
6618 && i.types[0].bitfield.instance == Accum
6619 && i.types[0].bitfield.dword
6620 && i.types[1].bitfield.instance == Accum
6621 && i.types[1].bitfield.dword)
8b38ad71 6622 continue;
1212781b
JB
6623 /* xrelease mov %eax, <disp> is another special case. It must not
6624 match the accumulator-only encoding of mov. */
6625 if (flag_code != CODE_64BIT
6626 && i.hle_prefix
6627 && t->base_opcode == 0xa0
35648716 6628 && t->opcode_modifier.opcodespace == SPACE_BASE
75e5731b 6629 && i.types[0].bitfield.instance == Accum
8dc0818e 6630 && (i.flags[1] & Operand_Mem))
1212781b 6631 continue;
f5eb1d70
JB
6632 /* Fall through. */
6633
6634 case 3:
3ac21baa
JB
6635 if (!(size_match & MATCH_STRAIGHT))
6636 goto check_reverse;
64c49ab3
JB
6637 /* Reverse direction of operands if swapping is possible in the first
6638 place (operands need to be symmetric) and
6639 - the load form is requested, and the template is a store form,
6640 - the store form is requested, and the template is a load form,
6641 - the non-default (swapped) form is requested. */
6642 overlap1 = operand_type_and (operand_types[0], operand_types[1]);
f5eb1d70 6643 if (t->opcode_modifier.d && i.reg_operands == i.operands
64c49ab3
JB
6644 && !operand_type_all_zero (&overlap1))
6645 switch (i.dir_encoding)
6646 {
6647 case dir_encoding_load:
6648 if (operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 6649 || t->opcode_modifier.regmem)
64c49ab3
JB
6650 goto check_reverse;
6651 break;
6652
6653 case dir_encoding_store:
6654 if (!operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 6655 && !t->opcode_modifier.regmem)
64c49ab3
JB
6656 goto check_reverse;
6657 break;
6658
6659 case dir_encoding_swap:
6660 goto check_reverse;
6661
6662 case dir_encoding_default:
6663 break;
6664 }
86fa6981 6665 /* If we want store form, we skip the current load. */
64c49ab3
JB
6666 if ((i.dir_encoding == dir_encoding_store
6667 || i.dir_encoding == dir_encoding_swap)
86fa6981
L
6668 && i.mem_operands == 0
6669 && t->opcode_modifier.load)
fa99fab2 6670 continue;
1a0670f3 6671 /* Fall through. */
f48ff2ae 6672 case 4:
c0f3af97 6673 case 5:
c6fb90c8 6674 overlap1 = operand_type_and (i.types[1], operand_types[1]);
40fb9820
L
6675 if (!operand_type_match (overlap0, i.types[0])
6676 || !operand_type_match (overlap1, i.types[1])
e2195274 6677 || ((check_register & 3) == 3
dc821c5f 6678 && !operand_type_register_match (i.types[0],
40fb9820 6679 operand_types[0],
dc821c5f 6680 i.types[1],
40fb9820 6681 operand_types[1])))
29b0f896
AM
6682 {
6683 /* Check if other direction is valid ... */
38e314eb 6684 if (!t->opcode_modifier.d)
29b0f896
AM
6685 continue;
6686
dc1e8a47 6687 check_reverse:
3ac21baa
JB
6688 if (!(size_match & MATCH_REVERSE))
6689 continue;
29b0f896 6690 /* Try reversing direction of operands. */
f5eb1d70
JB
6691 overlap0 = operand_type_and (i.types[0], operand_types[i.operands - 1]);
6692 overlap1 = operand_type_and (i.types[i.operands - 1], operand_types[0]);
40fb9820 6693 if (!operand_type_match (overlap0, i.types[0])
f5eb1d70 6694 || !operand_type_match (overlap1, i.types[i.operands - 1])
45664ddb 6695 || (check_register
dc821c5f 6696 && !operand_type_register_match (i.types[0],
f5eb1d70
JB
6697 operand_types[i.operands - 1],
6698 i.types[i.operands - 1],
45664ddb 6699 operand_types[0])))
29b0f896
AM
6700 {
6701 /* Does not match either direction. */
6702 continue;
6703 }
38e314eb 6704 /* found_reverse_match holds which of D or FloatR
29b0f896 6705 we've found. */
38e314eb
JB
6706 if (!t->opcode_modifier.d)
6707 found_reverse_match = 0;
6708 else if (operand_types[0].bitfield.tbyte)
8a2ed489 6709 found_reverse_match = Opcode_FloatD;
dbbc8b7e 6710 else if (operand_types[0].bitfield.xmmword
f5eb1d70 6711 || operand_types[i.operands - 1].bitfield.xmmword
3528c362
JB
6712 || operand_types[0].bitfield.class == RegMMX
6713 || operand_types[i.operands - 1].bitfield.class == RegMMX
dbbc8b7e
JB
6714 || is_any_vex_encoding(t))
6715 found_reverse_match = (t->base_opcode & 0xee) != 0x6e
6716 ? Opcode_SIMD_FloatD : Opcode_SIMD_IntD;
8a2ed489 6717 else
38e314eb 6718 found_reverse_match = Opcode_D;
40fb9820 6719 if (t->opcode_modifier.floatr)
8a2ed489 6720 found_reverse_match |= Opcode_FloatR;
29b0f896 6721 }
f48ff2ae 6722 else
29b0f896 6723 {
f48ff2ae 6724 /* Found a forward 2 operand match here. */
d1cbb4db
L
6725 switch (t->operands)
6726 {
c0f3af97
L
6727 case 5:
6728 overlap4 = operand_type_and (i.types[4],
6729 operand_types[4]);
1a0670f3 6730 /* Fall through. */
d1cbb4db 6731 case 4:
c6fb90c8
L
6732 overlap3 = operand_type_and (i.types[3],
6733 operand_types[3]);
1a0670f3 6734 /* Fall through. */
d1cbb4db 6735 case 3:
c6fb90c8
L
6736 overlap2 = operand_type_and (i.types[2],
6737 operand_types[2]);
d1cbb4db
L
6738 break;
6739 }
29b0f896 6740
f48ff2ae
L
6741 switch (t->operands)
6742 {
c0f3af97
L
6743 case 5:
6744 if (!operand_type_match (overlap4, i.types[4])
dc821c5f 6745 || !operand_type_register_match (i.types[3],
c0f3af97 6746 operand_types[3],
c0f3af97
L
6747 i.types[4],
6748 operand_types[4]))
6749 continue;
1a0670f3 6750 /* Fall through. */
f48ff2ae 6751 case 4:
40fb9820 6752 if (!operand_type_match (overlap3, i.types[3])
e2195274
JB
6753 || ((check_register & 0xa) == 0xa
6754 && !operand_type_register_match (i.types[1],
f7768225
JB
6755 operand_types[1],
6756 i.types[3],
e2195274
JB
6757 operand_types[3]))
6758 || ((check_register & 0xc) == 0xc
6759 && !operand_type_register_match (i.types[2],
6760 operand_types[2],
6761 i.types[3],
6762 operand_types[3])))
f48ff2ae 6763 continue;
1a0670f3 6764 /* Fall through. */
f48ff2ae
L
6765 case 3:
6766 /* Here we make use of the fact that there are no
23e42951 6767 reverse match 3 operand instructions. */
40fb9820 6768 if (!operand_type_match (overlap2, i.types[2])
e2195274
JB
6769 || ((check_register & 5) == 5
6770 && !operand_type_register_match (i.types[0],
23e42951
JB
6771 operand_types[0],
6772 i.types[2],
e2195274
JB
6773 operand_types[2]))
6774 || ((check_register & 6) == 6
6775 && !operand_type_register_match (i.types[1],
6776 operand_types[1],
6777 i.types[2],
6778 operand_types[2])))
f48ff2ae
L
6779 continue;
6780 break;
6781 }
29b0f896 6782 }
f48ff2ae 6783 /* Found either forward/reverse 2, 3 or 4 operand match here:
29b0f896
AM
6784 slip through to break. */
6785 }
c0f3af97 6786
da4977e0
JB
6787 /* Check if vector operands are valid. */
6788 if (check_VecOperands (t))
6789 {
6790 specific_error = i.error;
6791 continue;
6792 }
6793
6794 /* Check if VEX/EVEX encoding requirements can be satisfied. */
6795 if (VEX_check_encoding (t))
5614d22c
JB
6796 {
6797 specific_error = i.error;
6798 continue;
6799 }
a683cc34 6800
29b0f896
AM
6801 /* We've found a match; break out of loop. */
6802 break;
6803 }
6804
6805 if (t == current_templates->end)
6806 {
6807 /* We found no match. */
a65babc9 6808 const char *err_msg;
5614d22c 6809 switch (specific_error ? specific_error : i.error)
a65babc9
L
6810 {
6811 default:
6812 abort ();
86e026a4 6813 case operand_size_mismatch:
a65babc9
L
6814 err_msg = _("operand size mismatch");
6815 break;
6816 case operand_type_mismatch:
6817 err_msg = _("operand type mismatch");
6818 break;
6819 case register_type_mismatch:
6820 err_msg = _("register type mismatch");
6821 break;
6822 case number_of_operands_mismatch:
6823 err_msg = _("number of operands mismatch");
6824 break;
6825 case invalid_instruction_suffix:
6826 err_msg = _("invalid instruction suffix");
6827 break;
6828 case bad_imm4:
4a2608e3 6829 err_msg = _("constant doesn't fit in 4 bits");
a65babc9 6830 break;
a65babc9
L
6831 case unsupported_with_intel_mnemonic:
6832 err_msg = _("unsupported with Intel mnemonic");
6833 break;
6834 case unsupported_syntax:
6835 err_msg = _("unsupported syntax");
6836 break;
6837 case unsupported:
35262a23 6838 as_bad (_("unsupported instruction `%s'"),
10efe3f6
L
6839 current_templates->start->name);
6840 return NULL;
260cd341
LC
6841 case invalid_sib_address:
6842 err_msg = _("invalid SIB address");
6843 break;
6c30d220
L
6844 case invalid_vsib_address:
6845 err_msg = _("invalid VSIB address");
6846 break;
7bab8ab5
JB
6847 case invalid_vector_register_set:
6848 err_msg = _("mask, index, and destination registers must be distinct");
6849 break;
260cd341
LC
6850 case invalid_tmm_register_set:
6851 err_msg = _("all tmm registers must be distinct");
6852 break;
6c30d220
L
6853 case unsupported_vector_index_register:
6854 err_msg = _("unsupported vector index register");
6855 break;
43234a1e
L
6856 case unsupported_broadcast:
6857 err_msg = _("unsupported broadcast");
6858 break;
43234a1e
L
6859 case broadcast_needed:
6860 err_msg = _("broadcast is needed for operand of such type");
6861 break;
6862 case unsupported_masking:
6863 err_msg = _("unsupported masking");
6864 break;
6865 case mask_not_on_destination:
6866 err_msg = _("mask not on destination operand");
6867 break;
6868 case no_default_mask:
6869 err_msg = _("default mask isn't allowed");
6870 break;
6871 case unsupported_rc_sae:
6872 err_msg = _("unsupported static rounding/sae");
6873 break;
6874 case rc_sae_operand_not_last_imm:
6875 if (intel_syntax)
6876 err_msg = _("RC/SAE operand must precede immediate operands");
6877 else
6878 err_msg = _("RC/SAE operand must follow immediate operands");
6879 break;
6880 case invalid_register_operand:
6881 err_msg = _("invalid register operand");
6882 break;
a65babc9
L
6883 }
6884 as_bad (_("%s for `%s'"), err_msg,
891edac4 6885 current_templates->start->name);
fa99fab2 6886 return NULL;
29b0f896 6887 }
252b5132 6888
29b0f896
AM
6889 if (!quiet_warnings)
6890 {
6891 if (!intel_syntax
0cfa3eb3 6892 && (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE)))
6f2f06be 6893 as_warn (_("indirect %s without `*'"), t->name);
29b0f896 6894
40fb9820 6895 if (t->opcode_modifier.isprefix
3cd7f3e3 6896 && t->opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
6897 {
6898 /* Warn them that a data or address size prefix doesn't
6899 affect assembly of the next line of code. */
6900 as_warn (_("stand-alone `%s' prefix"), t->name);
6901 }
6902 }
6903
6904 /* Copy the template we found. */
9a182d04 6905 install_template (t);
539e75ad
L
6906
6907 if (addr_prefix_disp != -1)
6908 i.tm.operand_types[addr_prefix_disp]
6909 = operand_types[addr_prefix_disp];
6910
29b0f896
AM
6911 if (found_reverse_match)
6912 {
dfd69174
JB
6913 /* If we found a reverse match we must alter the opcode direction
6914 bit and clear/flip the regmem modifier one. found_reverse_match
6915 holds bits to change (different for int & float insns). */
29b0f896
AM
6916
6917 i.tm.base_opcode ^= found_reverse_match;
6918
f5eb1d70
JB
6919 i.tm.operand_types[0] = operand_types[i.operands - 1];
6920 i.tm.operand_types[i.operands - 1] = operand_types[0];
dfd69174
JB
6921
6922 /* Certain SIMD insns have their load forms specified in the opcode
6923 table, and hence we need to _set_ RegMem instead of clearing it.
6924 We need to avoid setting the bit though on insns like KMOVW. */
6925 i.tm.opcode_modifier.regmem
6926 = i.tm.opcode_modifier.modrm && i.tm.opcode_modifier.d
6927 && i.tm.operands > 2U - i.tm.opcode_modifier.sse2avx
6928 && !i.tm.opcode_modifier.regmem;
29b0f896
AM
6929 }
6930
fa99fab2 6931 return t;
29b0f896
AM
6932}
6933
6934static int
e3bb37b5 6935check_string (void)
29b0f896 6936{
51c8edf6
JB
6937 unsigned int es_op = i.tm.opcode_modifier.isstring - IS_STRING_ES_OP0;
6938 unsigned int op = i.tm.operand_types[0].bitfield.baseindex ? es_op : 0;
8dc0818e 6939
5e042380 6940 if (i.seg[op] != NULL && i.seg[op] != reg_es)
29b0f896 6941 {
51c8edf6
JB
6942 as_bad (_("`%s' operand %u must use `%ses' segment"),
6943 i.tm.name,
6944 intel_syntax ? i.tm.operands - es_op : es_op + 1,
6945 register_prefix);
6946 return 0;
29b0f896 6947 }
51c8edf6
JB
6948
6949 /* There's only ever one segment override allowed per instruction.
6950 This instruction possibly has a legal segment override on the
6951 second operand, so copy the segment to where non-string
6952 instructions store it, allowing common code. */
6953 i.seg[op] = i.seg[1];
6954
29b0f896
AM
6955 return 1;
6956}
6957
6958static int
543613e9 6959process_suffix (void)
29b0f896 6960{
5b7c81bd 6961 bool is_crc32 = false, is_movx = false;
8b65b895 6962
29b0f896
AM
6963 /* If matched instruction specifies an explicit instruction mnemonic
6964 suffix, use it. */
673fe0f0 6965 if (i.tm.opcode_modifier.size == SIZE16)
40fb9820 6966 i.suffix = WORD_MNEM_SUFFIX;
673fe0f0 6967 else if (i.tm.opcode_modifier.size == SIZE32)
40fb9820 6968 i.suffix = LONG_MNEM_SUFFIX;
673fe0f0 6969 else if (i.tm.opcode_modifier.size == SIZE64)
40fb9820 6970 i.suffix = QWORD_MNEM_SUFFIX;
13e600d0 6971 else if (i.reg_operands
c8f8eebc
JB
6972 && (i.operands > 1 || i.types[0].bitfield.class == Reg)
6973 && !i.tm.opcode_modifier.addrprefixopreg)
29b0f896 6974 {
65fca059 6975 unsigned int numop = i.operands;
389d00a5
JB
6976
6977 /* MOVSX/MOVZX */
6978 is_movx = (i.tm.opcode_modifier.opcodespace == SPACE_0F
6979 && (i.tm.base_opcode | 8) == 0xbe)
6980 || (i.tm.opcode_modifier.opcodespace == SPACE_BASE
6981 && i.tm.base_opcode == 0x63
6982 && i.tm.cpu_flags.bitfield.cpu64);
6983
8b65b895 6984 /* CRC32 */
389d00a5
JB
6985 is_crc32 = (i.tm.base_opcode == 0xf0
6986 && i.tm.opcode_modifier.opcodespace == SPACE_0F38
8b65b895 6987 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2);
65fca059
JB
6988
6989 /* movsx/movzx want only their source operand considered here, for the
6990 ambiguity checking below. The suffix will be replaced afterwards
6991 to represent the destination (register). */
389d00a5 6992 if (is_movx && (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63))
65fca059
JB
6993 --i.operands;
6994
643bb870 6995 /* crc32 needs REX.W set regardless of suffix / source operand size. */
8b65b895 6996 if (is_crc32 && i.tm.operand_types[1].bitfield.qword)
643bb870
JB
6997 i.rex |= REX_W;
6998
29b0f896 6999 /* If there's no instruction mnemonic suffix we try to invent one
13e600d0 7000 based on GPR operands. */
29b0f896
AM
7001 if (!i.suffix)
7002 {
7003 /* We take i.suffix from the last register operand specified,
7004 Destination register type is more significant than source
381d071f
L
7005 register type. crc32 in SSE4.2 prefers source register
7006 type. */
8b65b895 7007 unsigned int op = is_crc32 ? 1 : i.operands;
20592a94 7008
1a035124
JB
7009 while (op--)
7010 if (i.tm.operand_types[op].bitfield.instance == InstanceNone
7011 || i.tm.operand_types[op].bitfield.instance == Accum)
7012 {
7013 if (i.types[op].bitfield.class != Reg)
7014 continue;
7015 if (i.types[op].bitfield.byte)
7016 i.suffix = BYTE_MNEM_SUFFIX;
7017 else if (i.types[op].bitfield.word)
7018 i.suffix = WORD_MNEM_SUFFIX;
7019 else if (i.types[op].bitfield.dword)
7020 i.suffix = LONG_MNEM_SUFFIX;
7021 else if (i.types[op].bitfield.qword)
7022 i.suffix = QWORD_MNEM_SUFFIX;
7023 else
7024 continue;
7025 break;
7026 }
65fca059
JB
7027
7028 /* As an exception, movsx/movzx silently default to a byte source
7029 in AT&T mode. */
389d00a5 7030 if (is_movx && i.tm.opcode_modifier.w && !i.suffix && !intel_syntax)
65fca059 7031 i.suffix = BYTE_MNEM_SUFFIX;
29b0f896
AM
7032 }
7033 else if (i.suffix == BYTE_MNEM_SUFFIX)
7034 {
2eb952a4 7035 if (intel_syntax
3cd7f3e3 7036 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
2eb952a4
L
7037 && i.tm.opcode_modifier.no_bsuf)
7038 i.suffix = 0;
7039 else if (!check_byte_reg ())
29b0f896
AM
7040 return 0;
7041 }
7042 else if (i.suffix == LONG_MNEM_SUFFIX)
7043 {
2eb952a4 7044 if (intel_syntax
3cd7f3e3 7045 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
9f123b91
JB
7046 && i.tm.opcode_modifier.no_lsuf
7047 && !i.tm.opcode_modifier.todword
7048 && !i.tm.opcode_modifier.toqword)
2eb952a4
L
7049 i.suffix = 0;
7050 else if (!check_long_reg ())
29b0f896
AM
7051 return 0;
7052 }
7053 else if (i.suffix == QWORD_MNEM_SUFFIX)
7054 {
955e1e6a 7055 if (intel_syntax
3cd7f3e3 7056 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
9f123b91
JB
7057 && i.tm.opcode_modifier.no_qsuf
7058 && !i.tm.opcode_modifier.todword
7059 && !i.tm.opcode_modifier.toqword)
955e1e6a
L
7060 i.suffix = 0;
7061 else if (!check_qword_reg ())
29b0f896
AM
7062 return 0;
7063 }
7064 else if (i.suffix == WORD_MNEM_SUFFIX)
7065 {
2eb952a4 7066 if (intel_syntax
3cd7f3e3 7067 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
2eb952a4
L
7068 && i.tm.opcode_modifier.no_wsuf)
7069 i.suffix = 0;
7070 else if (!check_word_reg ())
29b0f896
AM
7071 return 0;
7072 }
3cd7f3e3
L
7073 else if (intel_syntax
7074 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
7075 /* Do nothing if the instruction is going to ignore the prefix. */
7076 ;
7077 else
7078 abort ();
65fca059
JB
7079
7080 /* Undo the movsx/movzx change done above. */
7081 i.operands = numop;
29b0f896 7082 }
3cd7f3e3
L
7083 else if (i.tm.opcode_modifier.mnemonicsize == DEFAULTSIZE
7084 && !i.suffix)
29b0f896 7085 {
13e600d0
JB
7086 i.suffix = stackop_size;
7087 if (stackop_size == LONG_MNEM_SUFFIX)
06f74c5c
L
7088 {
7089 /* stackop_size is set to LONG_MNEM_SUFFIX for the
7090 .code16gcc directive to support 16-bit mode with
7091 32-bit address. For IRET without a suffix, generate
7092 16-bit IRET (opcode 0xcf) to return from an interrupt
7093 handler. */
13e600d0
JB
7094 if (i.tm.base_opcode == 0xcf)
7095 {
7096 i.suffix = WORD_MNEM_SUFFIX;
7097 as_warn (_("generating 16-bit `iret' for .code16gcc directive"));
7098 }
7099 /* Warn about changed behavior for segment register push/pop. */
7100 else if ((i.tm.base_opcode | 1) == 0x07)
7101 as_warn (_("generating 32-bit `%s', unlike earlier gas versions"),
7102 i.tm.name);
06f74c5c 7103 }
29b0f896 7104 }
c006a730 7105 else if (!i.suffix
0cfa3eb3
JB
7106 && (i.tm.opcode_modifier.jump == JUMP_ABSOLUTE
7107 || i.tm.opcode_modifier.jump == JUMP_BYTE
7108 || i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT
389d00a5
JB
7109 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
7110 && i.tm.base_opcode == 0x01 /* [ls][gi]dt */
64e74474 7111 && i.tm.extension_opcode <= 3)))
9306ca4a
JB
7112 {
7113 switch (flag_code)
7114 {
7115 case CODE_64BIT:
40fb9820 7116 if (!i.tm.opcode_modifier.no_qsuf)
9306ca4a 7117 {
828c2a25
JB
7118 if (i.tm.opcode_modifier.jump == JUMP_BYTE
7119 || i.tm.opcode_modifier.no_lsuf)
7120 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a
JB
7121 break;
7122 }
1a0670f3 7123 /* Fall through. */
9306ca4a 7124 case CODE_32BIT:
40fb9820 7125 if (!i.tm.opcode_modifier.no_lsuf)
9306ca4a
JB
7126 i.suffix = LONG_MNEM_SUFFIX;
7127 break;
7128 case CODE_16BIT:
40fb9820 7129 if (!i.tm.opcode_modifier.no_wsuf)
9306ca4a
JB
7130 i.suffix = WORD_MNEM_SUFFIX;
7131 break;
7132 }
7133 }
252b5132 7134
c006a730 7135 if (!i.suffix
3cd7f3e3 7136 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8
JB
7137 /* Also cover lret/retf/iret in 64-bit mode. */
7138 || (flag_code == CODE_64BIT
7139 && !i.tm.opcode_modifier.no_lsuf
7140 && !i.tm.opcode_modifier.no_qsuf))
3cd7f3e3 7141 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
8bbb3ad8
JB
7142 /* Explicit sizing prefixes are assumed to disambiguate insns. */
7143 && !i.prefix[DATA_PREFIX] && !(i.prefix[REX_PREFIX] & REX_W)
62b3f548
JB
7144 /* Accept FLDENV et al without suffix. */
7145 && (i.tm.opcode_modifier.no_ssuf || i.tm.opcode_modifier.floatmf))
29b0f896 7146 {
6c0946d0 7147 unsigned int suffixes, evex = 0;
c006a730
JB
7148
7149 suffixes = !i.tm.opcode_modifier.no_bsuf;
7150 if (!i.tm.opcode_modifier.no_wsuf)
7151 suffixes |= 1 << 1;
7152 if (!i.tm.opcode_modifier.no_lsuf)
7153 suffixes |= 1 << 2;
7154 if (!i.tm.opcode_modifier.no_ldsuf)
7155 suffixes |= 1 << 3;
7156 if (!i.tm.opcode_modifier.no_ssuf)
7157 suffixes |= 1 << 4;
7158 if (flag_code == CODE_64BIT && !i.tm.opcode_modifier.no_qsuf)
7159 suffixes |= 1 << 5;
7160
6c0946d0
JB
7161 /* For [XYZ]MMWORD operands inspect operand sizes. While generally
7162 also suitable for AT&T syntax mode, it was requested that this be
7163 restricted to just Intel syntax. */
5273a3cd 7164 if (intel_syntax && is_any_vex_encoding (&i.tm) && !i.broadcast.type)
6c0946d0 7165 {
b9915cbc 7166 unsigned int op;
6c0946d0 7167
b9915cbc 7168 for (op = 0; op < i.tm.operands; ++op)
6c0946d0 7169 {
b9915cbc
JB
7170 if (is_evex_encoding (&i.tm)
7171 && !cpu_arch_flags.bitfield.cpuavx512vl)
6c0946d0 7172 {
b9915cbc
JB
7173 if (i.tm.operand_types[op].bitfield.ymmword)
7174 i.tm.operand_types[op].bitfield.xmmword = 0;
7175 if (i.tm.operand_types[op].bitfield.zmmword)
7176 i.tm.operand_types[op].bitfield.ymmword = 0;
7177 if (!i.tm.opcode_modifier.evex
7178 || i.tm.opcode_modifier.evex == EVEXDYN)
7179 i.tm.opcode_modifier.evex = EVEX512;
7180 }
6c0946d0 7181
b9915cbc
JB
7182 if (i.tm.operand_types[op].bitfield.xmmword
7183 + i.tm.operand_types[op].bitfield.ymmword
7184 + i.tm.operand_types[op].bitfield.zmmword < 2)
7185 continue;
6c0946d0 7186
b9915cbc
JB
7187 /* Any properly sized operand disambiguates the insn. */
7188 if (i.types[op].bitfield.xmmword
7189 || i.types[op].bitfield.ymmword
7190 || i.types[op].bitfield.zmmword)
7191 {
7192 suffixes &= ~(7 << 6);
7193 evex = 0;
7194 break;
7195 }
6c0946d0 7196
b9915cbc
JB
7197 if ((i.flags[op] & Operand_Mem)
7198 && i.tm.operand_types[op].bitfield.unspecified)
7199 {
7200 if (i.tm.operand_types[op].bitfield.xmmword)
7201 suffixes |= 1 << 6;
7202 if (i.tm.operand_types[op].bitfield.ymmword)
7203 suffixes |= 1 << 7;
7204 if (i.tm.operand_types[op].bitfield.zmmword)
7205 suffixes |= 1 << 8;
7206 if (is_evex_encoding (&i.tm))
7207 evex = EVEX512;
6c0946d0
JB
7208 }
7209 }
7210 }
7211
7212 /* Are multiple suffixes / operand sizes allowed? */
c006a730 7213 if (suffixes & (suffixes - 1))
9306ca4a 7214 {
873494c8 7215 if (intel_syntax
3cd7f3e3 7216 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8 7217 || operand_check == check_error))
9306ca4a 7218 {
c006a730 7219 as_bad (_("ambiguous operand size for `%s'"), i.tm.name);
9306ca4a
JB
7220 return 0;
7221 }
c006a730 7222 if (operand_check == check_error)
9306ca4a 7223 {
c006a730
JB
7224 as_bad (_("no instruction mnemonic suffix given and "
7225 "no register operands; can't size `%s'"), i.tm.name);
9306ca4a
JB
7226 return 0;
7227 }
c006a730 7228 if (operand_check == check_warning)
873494c8
JB
7229 as_warn (_("%s; using default for `%s'"),
7230 intel_syntax
7231 ? _("ambiguous operand size")
7232 : _("no instruction mnemonic suffix given and "
7233 "no register operands"),
7234 i.tm.name);
c006a730
JB
7235
7236 if (i.tm.opcode_modifier.floatmf)
7237 i.suffix = SHORT_MNEM_SUFFIX;
389d00a5 7238 else if (is_movx)
65fca059 7239 /* handled below */;
6c0946d0
JB
7240 else if (evex)
7241 i.tm.opcode_modifier.evex = evex;
c006a730
JB
7242 else if (flag_code == CODE_16BIT)
7243 i.suffix = WORD_MNEM_SUFFIX;
1a035124 7244 else if (!i.tm.opcode_modifier.no_lsuf)
c006a730 7245 i.suffix = LONG_MNEM_SUFFIX;
1a035124
JB
7246 else
7247 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a 7248 }
29b0f896 7249 }
252b5132 7250
389d00a5 7251 if (is_movx)
65fca059
JB
7252 {
7253 /* In Intel syntax, movsx/movzx must have a "suffix" (checked above).
7254 In AT&T syntax, if there is no suffix (warned about above), the default
7255 will be byte extension. */
7256 if (i.tm.opcode_modifier.w && i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
7257 i.tm.base_opcode |= 1;
7258
7259 /* For further processing, the suffix should represent the destination
7260 (register). This is already the case when one was used with
7261 mov[sz][bw]*, but we need to replace it for mov[sz]x, or if there was
7262 no suffix to begin with. */
7263 if (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63 || !i.suffix)
7264 {
7265 if (i.types[1].bitfield.word)
7266 i.suffix = WORD_MNEM_SUFFIX;
7267 else if (i.types[1].bitfield.qword)
7268 i.suffix = QWORD_MNEM_SUFFIX;
7269 else
7270 i.suffix = LONG_MNEM_SUFFIX;
7271
7272 i.tm.opcode_modifier.w = 0;
7273 }
7274 }
7275
50128d0c
JB
7276 if (!i.tm.opcode_modifier.modrm && i.reg_operands && i.tm.operands < 3)
7277 i.short_form = (i.tm.operand_types[0].bitfield.class == Reg)
7278 != (i.tm.operand_types[1].bitfield.class == Reg);
7279
d2224064
JB
7280 /* Change the opcode based on the operand size given by i.suffix. */
7281 switch (i.suffix)
29b0f896 7282 {
d2224064
JB
7283 /* Size floating point instruction. */
7284 case LONG_MNEM_SUFFIX:
7285 if (i.tm.opcode_modifier.floatmf)
7286 {
7287 i.tm.base_opcode ^= 4;
7288 break;
7289 }
7290 /* fall through */
7291 case WORD_MNEM_SUFFIX:
7292 case QWORD_MNEM_SUFFIX:
29b0f896 7293 /* It's not a byte, select word/dword operation. */
40fb9820 7294 if (i.tm.opcode_modifier.w)
29b0f896 7295 {
50128d0c 7296 if (i.short_form)
29b0f896
AM
7297 i.tm.base_opcode |= 8;
7298 else
7299 i.tm.base_opcode |= 1;
7300 }
d2224064
JB
7301 /* fall through */
7302 case SHORT_MNEM_SUFFIX:
29b0f896
AM
7303 /* Now select between word & dword operations via the operand
7304 size prefix, except for instructions that will ignore this
7305 prefix anyway. */
c8f8eebc 7306 if (i.suffix != QWORD_MNEM_SUFFIX
3cd7f3e3 7307 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
c8f8eebc
JB
7308 && !i.tm.opcode_modifier.floatmf
7309 && !is_any_vex_encoding (&i.tm)
7310 && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
7311 || (flag_code == CODE_64BIT
7312 && i.tm.opcode_modifier.jump == JUMP_BYTE)))
24eab124
AM
7313 {
7314 unsigned int prefix = DATA_PREFIX_OPCODE;
543613e9 7315
0cfa3eb3 7316 if (i.tm.opcode_modifier.jump == JUMP_BYTE) /* jcxz, loop */
29b0f896 7317 prefix = ADDR_PREFIX_OPCODE;
252b5132 7318
29b0f896
AM
7319 if (!add_prefix (prefix))
7320 return 0;
24eab124 7321 }
252b5132 7322
29b0f896
AM
7323 /* Set mode64 for an operand. */
7324 if (i.suffix == QWORD_MNEM_SUFFIX
9146926a 7325 && flag_code == CODE_64BIT
d2224064 7326 && !i.tm.opcode_modifier.norex64
4ed21b58 7327 && !i.tm.opcode_modifier.vexw
46e883c5 7328 /* Special case for xchg %rax,%rax. It is NOP and doesn't
d2224064
JB
7329 need rex64. */
7330 && ! (i.operands == 2
7331 && i.tm.base_opcode == 0x90
7332 && i.tm.extension_opcode == None
75e5731b
JB
7333 && i.types[0].bitfield.instance == Accum
7334 && i.types[0].bitfield.qword
7335 && i.types[1].bitfield.instance == Accum
7336 && i.types[1].bitfield.qword))
d2224064 7337 i.rex |= REX_W;
3e73aa7c 7338
d2224064 7339 break;
8bbb3ad8
JB
7340
7341 case 0:
f9a6a8f0 7342 /* Select word/dword/qword operation with explicit data sizing prefix
8bbb3ad8
JB
7343 when there are no suitable register operands. */
7344 if (i.tm.opcode_modifier.w
7345 && (i.prefix[DATA_PREFIX] || (i.prefix[REX_PREFIX] & REX_W))
7346 && (!i.reg_operands
7347 || (i.reg_operands == 1
7348 /* ShiftCount */
7349 && (i.tm.operand_types[0].bitfield.instance == RegC
7350 /* InOutPortReg */
7351 || i.tm.operand_types[0].bitfield.instance == RegD
7352 || i.tm.operand_types[1].bitfield.instance == RegD
7353 /* CRC32 */
8b65b895 7354 || is_crc32))))
8bbb3ad8
JB
7355 i.tm.base_opcode |= 1;
7356 break;
29b0f896 7357 }
7ecd2f8b 7358
c8f8eebc 7359 if (i.tm.opcode_modifier.addrprefixopreg)
c0a30a9f 7360 {
c8f8eebc
JB
7361 gas_assert (!i.suffix);
7362 gas_assert (i.reg_operands);
c0a30a9f 7363
c8f8eebc
JB
7364 if (i.tm.operand_types[0].bitfield.instance == Accum
7365 || i.operands == 1)
7366 {
7367 /* The address size override prefix changes the size of the
7368 first operand. */
7369 if (flag_code == CODE_64BIT
7370 && i.op[0].regs->reg_type.bitfield.word)
7371 {
7372 as_bad (_("16-bit addressing unavailable for `%s'"),
7373 i.tm.name);
7374 return 0;
7375 }
7376
7377 if ((flag_code == CODE_32BIT
7378 ? i.op[0].regs->reg_type.bitfield.word
7379 : i.op[0].regs->reg_type.bitfield.dword)
7380 && !add_prefix (ADDR_PREFIX_OPCODE))
7381 return 0;
7382 }
c0a30a9f
L
7383 else
7384 {
c8f8eebc
JB
7385 /* Check invalid register operand when the address size override
7386 prefix changes the size of register operands. */
7387 unsigned int op;
7388 enum { need_word, need_dword, need_qword } need;
7389
27f13469 7390 /* Check the register operand for the address size prefix if
b3a3496f 7391 the memory operand has no real registers, like symbol, DISP
829f3fe1 7392 or bogus (x32-only) symbol(%rip) when symbol(%eip) is meant. */
27f13469
L
7393 if (i.mem_operands == 1
7394 && i.reg_operands == 1
7395 && i.operands == 2
27f13469 7396 && i.types[1].bitfield.class == Reg
b3a3496f
L
7397 && (flag_code == CODE_32BIT
7398 ? i.op[1].regs->reg_type.bitfield.word
7399 : i.op[1].regs->reg_type.bitfield.dword)
7400 && ((i.base_reg == NULL && i.index_reg == NULL)
829f3fe1
JB
7401#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
7402 || (x86_elf_abi == X86_64_X32_ABI
7403 && i.base_reg
b3a3496f
L
7404 && i.base_reg->reg_num == RegIP
7405 && i.base_reg->reg_type.bitfield.qword))
829f3fe1
JB
7406#else
7407 || 0)
7408#endif
27f13469
L
7409 && !add_prefix (ADDR_PREFIX_OPCODE))
7410 return 0;
7411
c8f8eebc
JB
7412 if (flag_code == CODE_32BIT)
7413 need = i.prefix[ADDR_PREFIX] ? need_word : need_dword;
7414 else if (i.prefix[ADDR_PREFIX])
c0a30a9f
L
7415 need = need_dword;
7416 else
7417 need = flag_code == CODE_64BIT ? need_qword : need_word;
c0a30a9f 7418
c8f8eebc
JB
7419 for (op = 0; op < i.operands; op++)
7420 {
7421 if (i.types[op].bitfield.class != Reg)
7422 continue;
7423
7424 switch (need)
7425 {
7426 case need_word:
7427 if (i.op[op].regs->reg_type.bitfield.word)
7428 continue;
7429 break;
7430 case need_dword:
7431 if (i.op[op].regs->reg_type.bitfield.dword)
7432 continue;
7433 break;
7434 case need_qword:
7435 if (i.op[op].regs->reg_type.bitfield.qword)
7436 continue;
7437 break;
7438 }
7439
7440 as_bad (_("invalid register operand size for `%s'"),
7441 i.tm.name);
7442 return 0;
7443 }
7444 }
c0a30a9f
L
7445 }
7446
29b0f896
AM
7447 return 1;
7448}
3e73aa7c 7449
29b0f896 7450static int
543613e9 7451check_byte_reg (void)
29b0f896
AM
7452{
7453 int op;
543613e9 7454
29b0f896
AM
7455 for (op = i.operands; --op >= 0;)
7456 {
dc821c5f 7457 /* Skip non-register operands. */
bab6aec1 7458 if (i.types[op].bitfield.class != Reg)
dc821c5f
JB
7459 continue;
7460
29b0f896
AM
7461 /* If this is an eight bit register, it's OK. If it's the 16 or
7462 32 bit version of an eight bit register, we will just use the
7463 low portion, and that's OK too. */
dc821c5f 7464 if (i.types[op].bitfield.byte)
29b0f896
AM
7465 continue;
7466
5a819eb9 7467 /* I/O port address operands are OK too. */
75e5731b
JB
7468 if (i.tm.operand_types[op].bitfield.instance == RegD
7469 && i.tm.operand_types[op].bitfield.word)
5a819eb9
JB
7470 continue;
7471
9706160a 7472 /* crc32 only wants its source operand checked here. */
389d00a5
JB
7473 if (i.tm.base_opcode == 0xf0
7474 && i.tm.opcode_modifier.opcodespace == SPACE_0F38
8b65b895
L
7475 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2
7476 && op != 0)
9344ff29
L
7477 continue;
7478
29b0f896 7479 /* Any other register is bad. */
73c76375
JB
7480 as_bad (_("`%s%s' not allowed with `%s%c'"),
7481 register_prefix, i.op[op].regs->reg_name,
7482 i.tm.name, i.suffix);
7483 return 0;
29b0f896
AM
7484 }
7485 return 1;
7486}
7487
7488static int
e3bb37b5 7489check_long_reg (void)
29b0f896
AM
7490{
7491 int op;
7492
7493 for (op = i.operands; --op >= 0;)
dc821c5f 7494 /* Skip non-register operands. */
bab6aec1 7495 if (i.types[op].bitfield.class != Reg)
dc821c5f 7496 continue;
29b0f896
AM
7497 /* Reject eight bit registers, except where the template requires
7498 them. (eg. movzb) */
dc821c5f 7499 else if (i.types[op].bitfield.byte
bab6aec1 7500 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7501 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7502 && (i.tm.operand_types[op].bitfield.word
7503 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7504 {
a540244d
L
7505 as_bad (_("`%s%s' not allowed with `%s%c'"),
7506 register_prefix,
29b0f896
AM
7507 i.op[op].regs->reg_name,
7508 i.tm.name,
7509 i.suffix);
7510 return 0;
7511 }
be4c5e58
L
7512 /* Error if the e prefix on a general reg is missing. */
7513 else if (i.types[op].bitfield.word
bab6aec1 7514 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7515 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7516 && i.tm.operand_types[op].bitfield.dword)
29b0f896 7517 {
be4c5e58
L
7518 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7519 register_prefix, i.op[op].regs->reg_name,
7520 i.suffix);
7521 return 0;
252b5132 7522 }
e4630f71 7523 /* Warn if the r prefix on a general reg is present. */
dc821c5f 7524 else if (i.types[op].bitfield.qword
bab6aec1 7525 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7526 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7527 && i.tm.operand_types[op].bitfield.dword)
252b5132 7528 {
34828aad 7529 if (intel_syntax
65fca059 7530 && i.tm.opcode_modifier.toqword
3528c362 7531 && i.types[0].bitfield.class != RegSIMD)
34828aad 7532 {
ca61edf2 7533 /* Convert to QWORD. We want REX byte. */
34828aad
L
7534 i.suffix = QWORD_MNEM_SUFFIX;
7535 }
7536 else
7537 {
2b5d6a91 7538 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
34828aad
L
7539 register_prefix, i.op[op].regs->reg_name,
7540 i.suffix);
7541 return 0;
7542 }
29b0f896
AM
7543 }
7544 return 1;
7545}
252b5132 7546
29b0f896 7547static int
e3bb37b5 7548check_qword_reg (void)
29b0f896
AM
7549{
7550 int op;
252b5132 7551
29b0f896 7552 for (op = i.operands; --op >= 0; )
dc821c5f 7553 /* Skip non-register operands. */
bab6aec1 7554 if (i.types[op].bitfield.class != Reg)
dc821c5f 7555 continue;
29b0f896
AM
7556 /* Reject eight bit registers, except where the template requires
7557 them. (eg. movzb) */
dc821c5f 7558 else if (i.types[op].bitfield.byte
bab6aec1 7559 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7560 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7561 && (i.tm.operand_types[op].bitfield.word
7562 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7563 {
a540244d
L
7564 as_bad (_("`%s%s' not allowed with `%s%c'"),
7565 register_prefix,
29b0f896
AM
7566 i.op[op].regs->reg_name,
7567 i.tm.name,
7568 i.suffix);
7569 return 0;
7570 }
e4630f71 7571 /* Warn if the r prefix on a general reg is missing. */
dc821c5f
JB
7572 else if ((i.types[op].bitfield.word
7573 || i.types[op].bitfield.dword)
bab6aec1 7574 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7575 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7576 && i.tm.operand_types[op].bitfield.qword)
29b0f896
AM
7577 {
7578 /* Prohibit these changes in the 64bit mode, since the
7579 lowering is more complicated. */
34828aad 7580 if (intel_syntax
ca61edf2 7581 && i.tm.opcode_modifier.todword
3528c362 7582 && i.types[0].bitfield.class != RegSIMD)
34828aad 7583 {
ca61edf2 7584 /* Convert to DWORD. We don't want REX byte. */
34828aad
L
7585 i.suffix = LONG_MNEM_SUFFIX;
7586 }
7587 else
7588 {
2b5d6a91 7589 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
34828aad
L
7590 register_prefix, i.op[op].regs->reg_name,
7591 i.suffix);
7592 return 0;
7593 }
252b5132 7594 }
29b0f896
AM
7595 return 1;
7596}
252b5132 7597
29b0f896 7598static int
e3bb37b5 7599check_word_reg (void)
29b0f896
AM
7600{
7601 int op;
7602 for (op = i.operands; --op >= 0;)
dc821c5f 7603 /* Skip non-register operands. */
bab6aec1 7604 if (i.types[op].bitfield.class != Reg)
dc821c5f 7605 continue;
29b0f896
AM
7606 /* Reject eight bit registers, except where the template requires
7607 them. (eg. movzb) */
dc821c5f 7608 else if (i.types[op].bitfield.byte
bab6aec1 7609 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7610 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7611 && (i.tm.operand_types[op].bitfield.word
7612 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7613 {
a540244d
L
7614 as_bad (_("`%s%s' not allowed with `%s%c'"),
7615 register_prefix,
29b0f896
AM
7616 i.op[op].regs->reg_name,
7617 i.tm.name,
7618 i.suffix);
7619 return 0;
7620 }
9706160a
JB
7621 /* Error if the e or r prefix on a general reg is present. */
7622 else if ((i.types[op].bitfield.dword
dc821c5f 7623 || i.types[op].bitfield.qword)
bab6aec1 7624 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7625 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7626 && i.tm.operand_types[op].bitfield.word)
252b5132 7627 {
9706160a
JB
7628 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7629 register_prefix, i.op[op].regs->reg_name,
7630 i.suffix);
7631 return 0;
29b0f896
AM
7632 }
7633 return 1;
7634}
252b5132 7635
29b0f896 7636static int
40fb9820 7637update_imm (unsigned int j)
29b0f896 7638{
bc0844ae 7639 i386_operand_type overlap = i.types[j];
40fb9820
L
7640 if ((overlap.bitfield.imm8
7641 || overlap.bitfield.imm8s
7642 || overlap.bitfield.imm16
7643 || overlap.bitfield.imm32
7644 || overlap.bitfield.imm32s
7645 || overlap.bitfield.imm64)
0dfbf9d7
L
7646 && !operand_type_equal (&overlap, &imm8)
7647 && !operand_type_equal (&overlap, &imm8s)
7648 && !operand_type_equal (&overlap, &imm16)
7649 && !operand_type_equal (&overlap, &imm32)
7650 && !operand_type_equal (&overlap, &imm32s)
7651 && !operand_type_equal (&overlap, &imm64))
29b0f896
AM
7652 {
7653 if (i.suffix)
7654 {
40fb9820
L
7655 i386_operand_type temp;
7656
0dfbf9d7 7657 operand_type_set (&temp, 0);
7ab9ffdd 7658 if (i.suffix == BYTE_MNEM_SUFFIX)
40fb9820
L
7659 {
7660 temp.bitfield.imm8 = overlap.bitfield.imm8;
7661 temp.bitfield.imm8s = overlap.bitfield.imm8s;
7662 }
7663 else if (i.suffix == WORD_MNEM_SUFFIX)
7664 temp.bitfield.imm16 = overlap.bitfield.imm16;
7665 else if (i.suffix == QWORD_MNEM_SUFFIX)
7666 {
7667 temp.bitfield.imm64 = overlap.bitfield.imm64;
7668 temp.bitfield.imm32s = overlap.bitfield.imm32s;
7669 }
7670 else
7671 temp.bitfield.imm32 = overlap.bitfield.imm32;
7672 overlap = temp;
29b0f896 7673 }
0dfbf9d7
L
7674 else if (operand_type_equal (&overlap, &imm16_32_32s)
7675 || operand_type_equal (&overlap, &imm16_32)
7676 || operand_type_equal (&overlap, &imm16_32s))
29b0f896 7677 {
40fb9820 7678 if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
65da13b5 7679 overlap = imm16;
40fb9820 7680 else
65da13b5 7681 overlap = imm32s;
29b0f896 7682 }
8bbb3ad8
JB
7683 else if (i.prefix[REX_PREFIX] & REX_W)
7684 overlap = operand_type_and (overlap, imm32s);
7685 else if (i.prefix[DATA_PREFIX])
7686 overlap = operand_type_and (overlap,
7687 flag_code != CODE_16BIT ? imm16 : imm32);
0dfbf9d7
L
7688 if (!operand_type_equal (&overlap, &imm8)
7689 && !operand_type_equal (&overlap, &imm8s)
7690 && !operand_type_equal (&overlap, &imm16)
7691 && !operand_type_equal (&overlap, &imm32)
7692 && !operand_type_equal (&overlap, &imm32s)
7693 && !operand_type_equal (&overlap, &imm64))
29b0f896 7694 {
4eed87de
AM
7695 as_bad (_("no instruction mnemonic suffix given; "
7696 "can't determine immediate size"));
29b0f896
AM
7697 return 0;
7698 }
7699 }
40fb9820 7700 i.types[j] = overlap;
29b0f896 7701
40fb9820
L
7702 return 1;
7703}
7704
7705static int
7706finalize_imm (void)
7707{
bc0844ae 7708 unsigned int j, n;
29b0f896 7709
bc0844ae
L
7710 /* Update the first 2 immediate operands. */
7711 n = i.operands > 2 ? 2 : i.operands;
7712 if (n)
7713 {
7714 for (j = 0; j < n; j++)
7715 if (update_imm (j) == 0)
7716 return 0;
40fb9820 7717
bc0844ae
L
7718 /* The 3rd operand can't be immediate operand. */
7719 gas_assert (operand_type_check (i.types[2], imm) == 0);
7720 }
29b0f896
AM
7721
7722 return 1;
7723}
7724
7725static int
e3bb37b5 7726process_operands (void)
29b0f896
AM
7727{
7728 /* Default segment register this instruction will use for memory
7729 accesses. 0 means unknown. This is only for optimizing out
7730 unnecessary segment overrides. */
5e042380 7731 const reg_entry *default_seg = NULL;
29b0f896 7732
a5aeccd9
JB
7733 if (i.tm.opcode_modifier.sse2avx)
7734 {
7735 /* Legacy encoded insns allow explicit REX prefixes, so these prefixes
7736 need converting. */
7737 i.rex |= i.prefix[REX_PREFIX] & (REX_W | REX_R | REX_X | REX_B);
7738 i.prefix[REX_PREFIX] = 0;
7739 i.rex_encoding = 0;
7740 }
c423d21a
JB
7741 /* ImmExt should be processed after SSE2AVX. */
7742 else if (i.tm.opcode_modifier.immext)
7743 process_immext ();
a5aeccd9 7744
2426c15f 7745 if (i.tm.opcode_modifier.sse2avx && i.tm.opcode_modifier.vexvvvv)
29b0f896 7746 {
91d6fa6a
NC
7747 unsigned int dupl = i.operands;
7748 unsigned int dest = dupl - 1;
9fcfb3d7
L
7749 unsigned int j;
7750
c0f3af97 7751 /* The destination must be an xmm register. */
9c2799c2 7752 gas_assert (i.reg_operands
91d6fa6a 7753 && MAX_OPERANDS > dupl
7ab9ffdd 7754 && operand_type_equal (&i.types[dest], &regxmm));
c0f3af97 7755
75e5731b 7756 if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 7757 && i.tm.operand_types[0].bitfield.xmmword)
e2ec9d29 7758 {
8cd7925b 7759 if (i.tm.opcode_modifier.vexsources == VEX3SOURCES)
c0f3af97
L
7760 {
7761 /* Keep xmm0 for instructions with VEX prefix and 3
7762 sources. */
75e5731b 7763 i.tm.operand_types[0].bitfield.instance = InstanceNone;
3528c362 7764 i.tm.operand_types[0].bitfield.class = RegSIMD;
c0f3af97
L
7765 goto duplicate;
7766 }
e2ec9d29 7767 else
c0f3af97
L
7768 {
7769 /* We remove the first xmm0 and keep the number of
7770 operands unchanged, which in fact duplicates the
7771 destination. */
7772 for (j = 1; j < i.operands; j++)
7773 {
7774 i.op[j - 1] = i.op[j];
7775 i.types[j - 1] = i.types[j];
7776 i.tm.operand_types[j - 1] = i.tm.operand_types[j];
8dc0818e 7777 i.flags[j - 1] = i.flags[j];
c0f3af97
L
7778 }
7779 }
7780 }
7781 else if (i.tm.opcode_modifier.implicit1stxmm0)
7ab9ffdd 7782 {
91d6fa6a 7783 gas_assert ((MAX_OPERANDS - 1) > dupl
8cd7925b
L
7784 && (i.tm.opcode_modifier.vexsources
7785 == VEX3SOURCES));
c0f3af97
L
7786
7787 /* Add the implicit xmm0 for instructions with VEX prefix
7788 and 3 sources. */
7789 for (j = i.operands; j > 0; j--)
7790 {
7791 i.op[j] = i.op[j - 1];
7792 i.types[j] = i.types[j - 1];
7793 i.tm.operand_types[j] = i.tm.operand_types[j - 1];
8dc0818e 7794 i.flags[j] = i.flags[j - 1];
c0f3af97
L
7795 }
7796 i.op[0].regs
629310ab 7797 = (const reg_entry *) str_hash_find (reg_hash, "xmm0");
7ab9ffdd 7798 i.types[0] = regxmm;
c0f3af97
L
7799 i.tm.operand_types[0] = regxmm;
7800
7801 i.operands += 2;
7802 i.reg_operands += 2;
7803 i.tm.operands += 2;
7804
91d6fa6a 7805 dupl++;
c0f3af97 7806 dest++;
91d6fa6a
NC
7807 i.op[dupl] = i.op[dest];
7808 i.types[dupl] = i.types[dest];
7809 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 7810 i.flags[dupl] = i.flags[dest];
e2ec9d29 7811 }
c0f3af97
L
7812 else
7813 {
dc1e8a47 7814 duplicate:
c0f3af97
L
7815 i.operands++;
7816 i.reg_operands++;
7817 i.tm.operands++;
7818
91d6fa6a
NC
7819 i.op[dupl] = i.op[dest];
7820 i.types[dupl] = i.types[dest];
7821 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 7822 i.flags[dupl] = i.flags[dest];
c0f3af97
L
7823 }
7824
7825 if (i.tm.opcode_modifier.immext)
7826 process_immext ();
7827 }
75e5731b 7828 else if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 7829 && i.tm.operand_types[0].bitfield.xmmword)
c0f3af97
L
7830 {
7831 unsigned int j;
7832
9fcfb3d7
L
7833 for (j = 1; j < i.operands; j++)
7834 {
7835 i.op[j - 1] = i.op[j];
7836 i.types[j - 1] = i.types[j];
7837
7838 /* We need to adjust fields in i.tm since they are used by
7839 build_modrm_byte. */
7840 i.tm.operand_types [j - 1] = i.tm.operand_types [j];
8dc0818e
JB
7841
7842 i.flags[j - 1] = i.flags[j];
9fcfb3d7
L
7843 }
7844
e2ec9d29
L
7845 i.operands--;
7846 i.reg_operands--;
e2ec9d29
L
7847 i.tm.operands--;
7848 }
920d2ddc
IT
7849 else if (i.tm.opcode_modifier.implicitquadgroup)
7850 {
a477a8c4
JB
7851 unsigned int regnum, first_reg_in_group, last_reg_in_group;
7852
920d2ddc 7853 /* The second operand must be {x,y,z}mmN, where N is a multiple of 4. */
3528c362 7854 gas_assert (i.operands >= 2 && i.types[1].bitfield.class == RegSIMD);
a477a8c4
JB
7855 regnum = register_number (i.op[1].regs);
7856 first_reg_in_group = regnum & ~3;
7857 last_reg_in_group = first_reg_in_group + 3;
7858 if (regnum != first_reg_in_group)
7859 as_warn (_("source register `%s%s' implicitly denotes"
7860 " `%s%.3s%u' to `%s%.3s%u' source group in `%s'"),
7861 register_prefix, i.op[1].regs->reg_name,
7862 register_prefix, i.op[1].regs->reg_name, first_reg_in_group,
7863 register_prefix, i.op[1].regs->reg_name, last_reg_in_group,
7864 i.tm.name);
7865 }
e2ec9d29
L
7866 else if (i.tm.opcode_modifier.regkludge)
7867 {
7868 /* The imul $imm, %reg instruction is converted into
7869 imul $imm, %reg, %reg, and the clr %reg instruction
7870 is converted into xor %reg, %reg. */
7871
7872 unsigned int first_reg_op;
7873
7874 if (operand_type_check (i.types[0], reg))
7875 first_reg_op = 0;
7876 else
7877 first_reg_op = 1;
7878 /* Pretend we saw the extra register operand. */
9c2799c2 7879 gas_assert (i.reg_operands == 1
7ab9ffdd 7880 && i.op[first_reg_op + 1].regs == 0);
e2ec9d29
L
7881 i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
7882 i.types[first_reg_op + 1] = i.types[first_reg_op];
7883 i.operands++;
7884 i.reg_operands++;
29b0f896
AM
7885 }
7886
85b80b0f 7887 if (i.tm.opcode_modifier.modrm)
29b0f896
AM
7888 {
7889 /* The opcode is completed (modulo i.tm.extension_opcode which
52271982
AM
7890 must be put into the modrm byte). Now, we make the modrm and
7891 index base bytes based on all the info we've collected. */
29b0f896
AM
7892
7893 default_seg = build_modrm_byte ();
7894 }
00cee14f 7895 else if (i.types[0].bitfield.class == SReg)
85b80b0f
JB
7896 {
7897 if (flag_code != CODE_64BIT
7898 ? i.tm.base_opcode == POP_SEG_SHORT
7899 && i.op[0].regs->reg_num == 1
389d00a5 7900 : (i.tm.base_opcode | 1) == (POP_SEG386_SHORT & 0xff)
85b80b0f
JB
7901 && i.op[0].regs->reg_num < 4)
7902 {
7903 as_bad (_("you can't `%s %s%s'"),
7904 i.tm.name, register_prefix, i.op[0].regs->reg_name);
7905 return 0;
7906 }
389d00a5
JB
7907 if (i.op[0].regs->reg_num > 3
7908 && i.tm.opcode_modifier.opcodespace == SPACE_BASE )
85b80b0f 7909 {
389d00a5
JB
7910 i.tm.base_opcode ^= (POP_SEG_SHORT ^ POP_SEG386_SHORT) & 0xff;
7911 i.tm.opcode_modifier.opcodespace = SPACE_0F;
85b80b0f
JB
7912 }
7913 i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
7914 }
389d00a5
JB
7915 else if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
7916 && (i.tm.base_opcode & ~3) == MOV_AX_DISP32)
29b0f896 7917 {
5e042380 7918 default_seg = reg_ds;
29b0f896 7919 }
40fb9820 7920 else if (i.tm.opcode_modifier.isstring)
29b0f896
AM
7921 {
7922 /* For the string instructions that allow a segment override
7923 on one of their operands, the default segment is ds. */
5e042380 7924 default_seg = reg_ds;
29b0f896 7925 }
50128d0c 7926 else if (i.short_form)
85b80b0f
JB
7927 {
7928 /* The register or float register operand is in operand
7929 0 or 1. */
bab6aec1 7930 unsigned int op = i.tm.operand_types[0].bitfield.class != Reg;
85b80b0f
JB
7931
7932 /* Register goes in low 3 bits of opcode. */
7933 i.tm.base_opcode |= i.op[op].regs->reg_num;
7934 if ((i.op[op].regs->reg_flags & RegRex) != 0)
7935 i.rex |= REX_B;
7936 if (!quiet_warnings && i.tm.opcode_modifier.ugh)
7937 {
7938 /* Warn about some common errors, but press on regardless.
7939 The first case can be generated by gcc (<= 2.8.1). */
7940 if (i.operands == 2)
7941 {
7942 /* Reversed arguments on faddp, fsubp, etc. */
7943 as_warn (_("translating to `%s %s%s,%s%s'"), i.tm.name,
7944 register_prefix, i.op[!intel_syntax].regs->reg_name,
7945 register_prefix, i.op[intel_syntax].regs->reg_name);
7946 }
7947 else
7948 {
7949 /* Extraneous `l' suffix on fp insn. */
7950 as_warn (_("translating to `%s %s%s'"), i.tm.name,
7951 register_prefix, i.op[0].regs->reg_name);
7952 }
7953 }
7954 }
29b0f896 7955
514a8bb0 7956 if ((i.seg[0] || i.prefix[SEG_PREFIX])
514a8bb0 7957 && i.tm.base_opcode == 0x8d /* lea */
35648716 7958 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
514a8bb0 7959 && !is_any_vex_encoding(&i.tm))
92334ad2
JB
7960 {
7961 if (!quiet_warnings)
7962 as_warn (_("segment override on `%s' is ineffectual"), i.tm.name);
7963 if (optimize)
7964 {
7965 i.seg[0] = NULL;
7966 i.prefix[SEG_PREFIX] = 0;
7967 }
7968 }
52271982
AM
7969
7970 /* If a segment was explicitly specified, and the specified segment
b6773884
JB
7971 is neither the default nor the one already recorded from a prefix,
7972 use an opcode prefix to select it. If we never figured out what
7973 the default segment is, then default_seg will be zero at this
7974 point, and the specified segment prefix will always be used. */
7975 if (i.seg[0]
7976 && i.seg[0] != default_seg
5e042380 7977 && i386_seg_prefixes[i.seg[0]->reg_num] != i.prefix[SEG_PREFIX])
29b0f896 7978 {
5e042380 7979 if (!add_prefix (i386_seg_prefixes[i.seg[0]->reg_num]))
29b0f896
AM
7980 return 0;
7981 }
7982 return 1;
7983}
7984
a5aeccd9 7985static INLINE void set_rex_vrex (const reg_entry *r, unsigned int rex_bit,
5b7c81bd 7986 bool do_sse2avx)
a5aeccd9
JB
7987{
7988 if (r->reg_flags & RegRex)
7989 {
7990 if (i.rex & rex_bit)
7991 as_bad (_("same type of prefix used twice"));
7992 i.rex |= rex_bit;
7993 }
7994 else if (do_sse2avx && (i.rex & rex_bit) && i.vex.register_specifier)
7995 {
7996 gas_assert (i.vex.register_specifier == r);
7997 i.vex.register_specifier += 8;
7998 }
7999
8000 if (r->reg_flags & RegVRex)
8001 i.vrex |= rex_bit;
8002}
8003
5e042380 8004static const reg_entry *
e3bb37b5 8005build_modrm_byte (void)
29b0f896 8006{
5e042380 8007 const reg_entry *default_seg = NULL;
c0f3af97 8008 unsigned int source, dest;
8cd7925b 8009 int vex_3_sources;
c0f3af97 8010
8cd7925b 8011 vex_3_sources = i.tm.opcode_modifier.vexsources == VEX3SOURCES;
c0f3af97
L
8012 if (vex_3_sources)
8013 {
91d6fa6a 8014 unsigned int nds, reg_slot;
4c2c6516 8015 expressionS *exp;
c0f3af97 8016
6b8d3588 8017 dest = i.operands - 1;
c0f3af97 8018 nds = dest - 1;
922d8de8 8019
a683cc34 8020 /* There are 2 kinds of instructions:
bed3d976 8021 1. 5 operands: 4 register operands or 3 register operands
9d3bf266 8022 plus 1 memory operand plus one Imm4 operand, VexXDS, and
bed3d976 8023 VexW0 or VexW1. The destination must be either XMM, YMM or
43234a1e 8024 ZMM register.
bed3d976 8025 2. 4 operands: 4 register operands or 3 register operands
2f1bada2 8026 plus 1 memory operand, with VexXDS. */
922d8de8 8027 gas_assert ((i.reg_operands == 4
bed3d976
JB
8028 || (i.reg_operands == 3 && i.mem_operands == 1))
8029 && i.tm.opcode_modifier.vexvvvv == VEXXDS
dcd7e323 8030 && i.tm.opcode_modifier.vexw
3528c362 8031 && i.tm.operand_types[dest].bitfield.class == RegSIMD);
a683cc34 8032
48db9223
JB
8033 /* If VexW1 is set, the first non-immediate operand is the source and
8034 the second non-immediate one is encoded in the immediate operand. */
8035 if (i.tm.opcode_modifier.vexw == VEXW1)
8036 {
8037 source = i.imm_operands;
8038 reg_slot = i.imm_operands + 1;
8039 }
8040 else
8041 {
8042 source = i.imm_operands + 1;
8043 reg_slot = i.imm_operands;
8044 }
8045
a683cc34 8046 if (i.imm_operands == 0)
bed3d976
JB
8047 {
8048 /* When there is no immediate operand, generate an 8bit
8049 immediate operand to encode the first operand. */
8050 exp = &im_expressions[i.imm_operands++];
8051 i.op[i.operands].imms = exp;
8052 i.types[i.operands] = imm8;
8053 i.operands++;
8054
3528c362 8055 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
bed3d976
JB
8056 exp->X_op = O_constant;
8057 exp->X_add_number = register_number (i.op[reg_slot].regs) << 4;
43234a1e
L
8058 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
8059 }
922d8de8 8060 else
bed3d976 8061 {
9d3bf266
JB
8062 gas_assert (i.imm_operands == 1);
8063 gas_assert (fits_in_imm4 (i.op[0].imms->X_add_number));
8064 gas_assert (!i.tm.opcode_modifier.immext);
a683cc34 8065
9d3bf266
JB
8066 /* Turn on Imm8 again so that output_imm will generate it. */
8067 i.types[0].bitfield.imm8 = 1;
bed3d976 8068
3528c362 8069 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
9d3bf266 8070 i.op[0].imms->X_add_number
bed3d976 8071 |= register_number (i.op[reg_slot].regs) << 4;
43234a1e 8072 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
bed3d976 8073 }
a683cc34 8074
3528c362 8075 gas_assert (i.tm.operand_types[nds].bitfield.class == RegSIMD);
dae39acc 8076 i.vex.register_specifier = i.op[nds].regs;
c0f3af97
L
8077 }
8078 else
8079 source = dest = 0;
29b0f896
AM
8080
8081 /* i.reg_operands MUST be the number of real register operands;
c0f3af97
L
8082 implicit registers do not count. If there are 3 register
8083 operands, it must be a instruction with VexNDS. For a
8084 instruction with VexNDD, the destination register is encoded
8085 in VEX prefix. If there are 4 register operands, it must be
8086 a instruction with VEX prefix and 3 sources. */
7ab9ffdd
L
8087 if (i.mem_operands == 0
8088 && ((i.reg_operands == 2
2426c15f 8089 && i.tm.opcode_modifier.vexvvvv <= VEXXDS)
7ab9ffdd 8090 || (i.reg_operands == 3
2426c15f 8091 && i.tm.opcode_modifier.vexvvvv == VEXXDS)
7ab9ffdd 8092 || (i.reg_operands == 4 && vex_3_sources)))
29b0f896 8093 {
cab737b9
L
8094 switch (i.operands)
8095 {
8096 case 2:
8097 source = 0;
8098 break;
8099 case 3:
c81128dc
L
8100 /* When there are 3 operands, one of them may be immediate,
8101 which may be the first or the last operand. Otherwise,
c0f3af97
L
8102 the first operand must be shift count register (cl) or it
8103 is an instruction with VexNDS. */
9c2799c2 8104 gas_assert (i.imm_operands == 1
7ab9ffdd 8105 || (i.imm_operands == 0
2426c15f 8106 && (i.tm.opcode_modifier.vexvvvv == VEXXDS
75e5731b
JB
8107 || (i.types[0].bitfield.instance == RegC
8108 && i.types[0].bitfield.byte))));
40fb9820 8109 if (operand_type_check (i.types[0], imm)
75e5731b
JB
8110 || (i.types[0].bitfield.instance == RegC
8111 && i.types[0].bitfield.byte))
40fb9820
L
8112 source = 1;
8113 else
8114 source = 0;
cab737b9
L
8115 break;
8116 case 4:
368d64cc
L
8117 /* When there are 4 operands, the first two must be 8bit
8118 immediate operands. The source operand will be the 3rd
c0f3af97
L
8119 one.
8120
8121 For instructions with VexNDS, if the first operand
8122 an imm8, the source operand is the 2nd one. If the last
8123 operand is imm8, the source operand is the first one. */
9c2799c2 8124 gas_assert ((i.imm_operands == 2
7ab9ffdd
L
8125 && i.types[0].bitfield.imm8
8126 && i.types[1].bitfield.imm8)
2426c15f 8127 || (i.tm.opcode_modifier.vexvvvv == VEXXDS
7ab9ffdd
L
8128 && i.imm_operands == 1
8129 && (i.types[0].bitfield.imm8
43234a1e 8130 || i.types[i.operands - 1].bitfield.imm8
ca5312a2 8131 || i.rounding.type != rc_none)));
9f2670f2
L
8132 if (i.imm_operands == 2)
8133 source = 2;
8134 else
c0f3af97
L
8135 {
8136 if (i.types[0].bitfield.imm8)
8137 source = 1;
8138 else
8139 source = 0;
8140 }
c0f3af97
L
8141 break;
8142 case 5:
e771e7c9 8143 if (is_evex_encoding (&i.tm))
43234a1e
L
8144 {
8145 /* For EVEX instructions, when there are 5 operands, the
8146 first one must be immediate operand. If the second one
8147 is immediate operand, the source operand is the 3th
8148 one. If the last one is immediate operand, the source
8149 operand is the 2nd one. */
8150 gas_assert (i.imm_operands == 2
8151 && i.tm.opcode_modifier.sae
8152 && operand_type_check (i.types[0], imm));
8153 if (operand_type_check (i.types[1], imm))
8154 source = 2;
8155 else if (operand_type_check (i.types[4], imm))
8156 source = 1;
8157 else
8158 abort ();
8159 }
cab737b9
L
8160 break;
8161 default:
8162 abort ();
8163 }
8164
c0f3af97
L
8165 if (!vex_3_sources)
8166 {
8167 dest = source + 1;
8168
43234a1e
L
8169 /* RC/SAE operand could be between DEST and SRC. That happens
8170 when one operand is GPR and the other one is XMM/YMM/ZMM
8171 register. */
ca5312a2 8172 if (i.rounding.type != rc_none && i.rounding.operand == dest)
43234a1e
L
8173 dest++;
8174
2426c15f 8175 if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
c0f3af97 8176 {
43234a1e 8177 /* For instructions with VexNDS, the register-only source
c5d0745b 8178 operand must be a 32/64bit integer, XMM, YMM, ZMM, or mask
dfd69174 8179 register. It is encoded in VEX prefix. */
f12dc422
L
8180
8181 i386_operand_type op;
8182 unsigned int vvvv;
8183
c2ecccb3
L
8184 /* Swap two source operands if needed. */
8185 if (i.tm.opcode_modifier.swapsources)
f12dc422
L
8186 {
8187 vvvv = source;
8188 source = dest;
8189 }
8190 else
8191 vvvv = dest;
8192
8193 op = i.tm.operand_types[vvvv];
c0f3af97 8194 if ((dest + 1) >= i.operands
bab6aec1 8195 || ((op.bitfield.class != Reg
dc821c5f 8196 || (!op.bitfield.dword && !op.bitfield.qword))
3528c362 8197 && op.bitfield.class != RegSIMD
43234a1e 8198 && !operand_type_equal (&op, &regmask)))
c0f3af97 8199 abort ();
f12dc422 8200 i.vex.register_specifier = i.op[vvvv].regs;
c0f3af97
L
8201 dest++;
8202 }
8203 }
29b0f896
AM
8204
8205 i.rm.mode = 3;
dfd69174
JB
8206 /* One of the register operands will be encoded in the i.rm.reg
8207 field, the other in the combined i.rm.mode and i.rm.regmem
29b0f896
AM
8208 fields. If no form of this instruction supports a memory
8209 destination operand, then we assume the source operand may
8210 sometimes be a memory operand and so we need to store the
8211 destination in the i.rm.reg field. */
dfd69174 8212 if (!i.tm.opcode_modifier.regmem
40fb9820 8213 && operand_type_check (i.tm.operand_types[dest], anymem) == 0)
29b0f896
AM
8214 {
8215 i.rm.reg = i.op[dest].regs->reg_num;
8216 i.rm.regmem = i.op[source].regs->reg_num;
a5aeccd9 8217 set_rex_vrex (i.op[dest].regs, REX_R, i.tm.opcode_modifier.sse2avx);
5b7c81bd 8218 set_rex_vrex (i.op[source].regs, REX_B, false);
29b0f896
AM
8219 }
8220 else
8221 {
8222 i.rm.reg = i.op[source].regs->reg_num;
8223 i.rm.regmem = i.op[dest].regs->reg_num;
a5aeccd9 8224 set_rex_vrex (i.op[dest].regs, REX_B, i.tm.opcode_modifier.sse2avx);
5b7c81bd 8225 set_rex_vrex (i.op[source].regs, REX_R, false);
29b0f896 8226 }
e0c7f900 8227 if (flag_code != CODE_64BIT && (i.rex & REX_R))
c4a530c5 8228 {
4a5c67ed 8229 if (i.types[!i.tm.opcode_modifier.regmem].bitfield.class != RegCR)
c4a530c5 8230 abort ();
e0c7f900 8231 i.rex &= ~REX_R;
c4a530c5
JB
8232 add_prefix (LOCK_PREFIX_OPCODE);
8233 }
29b0f896
AM
8234 }
8235 else
8236 { /* If it's not 2 reg operands... */
c0f3af97
L
8237 unsigned int mem;
8238
29b0f896
AM
8239 if (i.mem_operands)
8240 {
8241 unsigned int fake_zero_displacement = 0;
99018f42 8242 unsigned int op;
4eed87de 8243
7ab9ffdd 8244 for (op = 0; op < i.operands; op++)
8dc0818e 8245 if (i.flags[op] & Operand_Mem)
7ab9ffdd 8246 break;
7ab9ffdd 8247 gas_assert (op < i.operands);
29b0f896 8248
63112cd6 8249 if (i.tm.opcode_modifier.sib)
6c30d220 8250 {
260cd341
LC
8251 /* The index register of VSIB shouldn't be RegIZ. */
8252 if (i.tm.opcode_modifier.sib != SIBMEM
8253 && i.index_reg->reg_num == RegIZ)
6c30d220
L
8254 abort ();
8255
8256 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8257 if (!i.base_reg)
8258 {
8259 i.sib.base = NO_BASE_REGISTER;
8260 i.sib.scale = i.log2_scale_factor;
8261 i.types[op].bitfield.disp8 = 0;
8262 i.types[op].bitfield.disp16 = 0;
8263 i.types[op].bitfield.disp64 = 0;
a9aabc23 8264 if (want_disp32 (&i.tm))
6c30d220
L
8265 {
8266 /* Must be 32 bit */
8267 i.types[op].bitfield.disp32 = 1;
8268 i.types[op].bitfield.disp32s = 0;
8269 }
8270 else
8271 {
8272 i.types[op].bitfield.disp32 = 0;
8273 i.types[op].bitfield.disp32s = 1;
8274 }
8275 }
260cd341
LC
8276
8277 /* Since the mandatory SIB always has index register, so
8278 the code logic remains unchanged. The non-mandatory SIB
8279 without index register is allowed and will be handled
8280 later. */
8281 if (i.index_reg)
8282 {
8283 if (i.index_reg->reg_num == RegIZ)
8284 i.sib.index = NO_INDEX_REGISTER;
8285 else
8286 i.sib.index = i.index_reg->reg_num;
5b7c81bd 8287 set_rex_vrex (i.index_reg, REX_X, false);
260cd341 8288 }
6c30d220
L
8289 }
8290
5e042380 8291 default_seg = reg_ds;
29b0f896
AM
8292
8293 if (i.base_reg == 0)
8294 {
8295 i.rm.mode = 0;
8296 if (!i.disp_operands)
9bb129e8 8297 fake_zero_displacement = 1;
29b0f896
AM
8298 if (i.index_reg == 0)
8299 {
73053c1f
JB
8300 i386_operand_type newdisp;
8301
260cd341
LC
8302 /* Both check for VSIB and mandatory non-vector SIB. */
8303 gas_assert (!i.tm.opcode_modifier.sib
8304 || i.tm.opcode_modifier.sib == SIBMEM);
29b0f896 8305 /* Operand is just <disp> */
20f0a1fc 8306 if (flag_code == CODE_64BIT)
29b0f896
AM
8307 {
8308 /* 64bit mode overwrites the 32bit absolute
8309 addressing by RIP relative addressing and
8310 absolute addressing is encoded by one of the
8311 redundant SIB forms. */
8312 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8313 i.sib.base = NO_BASE_REGISTER;
8314 i.sib.index = NO_INDEX_REGISTER;
a9aabc23 8315 newdisp = (want_disp32(&i.tm) ? disp32 : disp32s);
20f0a1fc 8316 }
fc225355
L
8317 else if ((flag_code == CODE_16BIT)
8318 ^ (i.prefix[ADDR_PREFIX] != 0))
20f0a1fc
NC
8319 {
8320 i.rm.regmem = NO_BASE_REGISTER_16;
73053c1f 8321 newdisp = disp16;
20f0a1fc
NC
8322 }
8323 else
8324 {
8325 i.rm.regmem = NO_BASE_REGISTER;
73053c1f 8326 newdisp = disp32;
29b0f896 8327 }
73053c1f
JB
8328 i.types[op] = operand_type_and_not (i.types[op], anydisp);
8329 i.types[op] = operand_type_or (i.types[op], newdisp);
29b0f896 8330 }
63112cd6 8331 else if (!i.tm.opcode_modifier.sib)
29b0f896 8332 {
6c30d220 8333 /* !i.base_reg && i.index_reg */
e968fc9b 8334 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8335 i.sib.index = NO_INDEX_REGISTER;
8336 else
8337 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8338 i.sib.base = NO_BASE_REGISTER;
8339 i.sib.scale = i.log2_scale_factor;
8340 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
40fb9820
L
8341 i.types[op].bitfield.disp8 = 0;
8342 i.types[op].bitfield.disp16 = 0;
8343 i.types[op].bitfield.disp64 = 0;
a9aabc23 8344 if (want_disp32 (&i.tm))
40fb9820
L
8345 {
8346 /* Must be 32 bit */
8347 i.types[op].bitfield.disp32 = 1;
8348 i.types[op].bitfield.disp32s = 0;
8349 }
29b0f896 8350 else
40fb9820
L
8351 {
8352 i.types[op].bitfield.disp32 = 0;
8353 i.types[op].bitfield.disp32s = 1;
8354 }
29b0f896 8355 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8356 i.rex |= REX_X;
29b0f896
AM
8357 }
8358 }
8359 /* RIP addressing for 64bit mode. */
e968fc9b 8360 else if (i.base_reg->reg_num == RegIP)
29b0f896 8361 {
63112cd6 8362 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896 8363 i.rm.regmem = NO_BASE_REGISTER;
40fb9820
L
8364 i.types[op].bitfield.disp8 = 0;
8365 i.types[op].bitfield.disp16 = 0;
8366 i.types[op].bitfield.disp32 = 0;
8367 i.types[op].bitfield.disp32s = 1;
8368 i.types[op].bitfield.disp64 = 0;
71903a11 8369 i.flags[op] |= Operand_PCrel;
20f0a1fc
NC
8370 if (! i.disp_operands)
8371 fake_zero_displacement = 1;
29b0f896 8372 }
dc821c5f 8373 else if (i.base_reg->reg_type.bitfield.word)
29b0f896 8374 {
63112cd6 8375 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896
AM
8376 switch (i.base_reg->reg_num)
8377 {
8378 case 3: /* (%bx) */
8379 if (i.index_reg == 0)
8380 i.rm.regmem = 7;
8381 else /* (%bx,%si) -> 0, or (%bx,%di) -> 1 */
8382 i.rm.regmem = i.index_reg->reg_num - 6;
8383 break;
8384 case 5: /* (%bp) */
5e042380 8385 default_seg = reg_ss;
29b0f896
AM
8386 if (i.index_reg == 0)
8387 {
8388 i.rm.regmem = 6;
40fb9820 8389 if (operand_type_check (i.types[op], disp) == 0)
29b0f896
AM
8390 {
8391 /* fake (%bp) into 0(%bp) */
41eb8e88 8392 if (i.disp_encoding == disp_encoding_16bit)
1a02d6b0
L
8393 i.types[op].bitfield.disp16 = 1;
8394 else
8395 i.types[op].bitfield.disp8 = 1;
252b5132 8396 fake_zero_displacement = 1;
29b0f896
AM
8397 }
8398 }
8399 else /* (%bp,%si) -> 2, or (%bp,%di) -> 3 */
8400 i.rm.regmem = i.index_reg->reg_num - 6 + 2;
8401 break;
8402 default: /* (%si) -> 4 or (%di) -> 5 */
8403 i.rm.regmem = i.base_reg->reg_num - 6 + 4;
8404 }
41eb8e88
L
8405 if (!fake_zero_displacement
8406 && !i.disp_operands
8407 && i.disp_encoding)
8408 {
8409 fake_zero_displacement = 1;
8410 if (i.disp_encoding == disp_encoding_8bit)
8411 i.types[op].bitfield.disp8 = 1;
8412 else
8413 i.types[op].bitfield.disp16 = 1;
8414 }
29b0f896
AM
8415 i.rm.mode = mode_from_disp_size (i.types[op]);
8416 }
8417 else /* i.base_reg and 32/64 bit mode */
8418 {
a9aabc23 8419 if (operand_type_check (i.types[op], disp))
40fb9820 8420 {
73053c1f
JB
8421 i.types[op].bitfield.disp16 = 0;
8422 i.types[op].bitfield.disp64 = 0;
a9aabc23 8423 if (!want_disp32 (&i.tm))
73053c1f
JB
8424 {
8425 i.types[op].bitfield.disp32 = 0;
8426 i.types[op].bitfield.disp32s = 1;
8427 }
40fb9820 8428 else
73053c1f
JB
8429 {
8430 i.types[op].bitfield.disp32 = 1;
8431 i.types[op].bitfield.disp32s = 0;
8432 }
40fb9820 8433 }
20f0a1fc 8434
63112cd6 8435 if (!i.tm.opcode_modifier.sib)
6c30d220 8436 i.rm.regmem = i.base_reg->reg_num;
29b0f896 8437 if ((i.base_reg->reg_flags & RegRex) != 0)
161a04f6 8438 i.rex |= REX_B;
29b0f896
AM
8439 i.sib.base = i.base_reg->reg_num;
8440 /* x86-64 ignores REX prefix bit here to avoid decoder
8441 complications. */
848930b2
JB
8442 if (!(i.base_reg->reg_flags & RegRex)
8443 && (i.base_reg->reg_num == EBP_REG_NUM
8444 || i.base_reg->reg_num == ESP_REG_NUM))
5e042380 8445 default_seg = reg_ss;
848930b2 8446 if (i.base_reg->reg_num == 5 && i.disp_operands == 0)
29b0f896 8447 {
848930b2 8448 fake_zero_displacement = 1;
1a02d6b0
L
8449 if (i.disp_encoding == disp_encoding_32bit)
8450 i.types[op].bitfield.disp32 = 1;
8451 else
8452 i.types[op].bitfield.disp8 = 1;
29b0f896
AM
8453 }
8454 i.sib.scale = i.log2_scale_factor;
8455 if (i.index_reg == 0)
8456 {
260cd341
LC
8457 /* Only check for VSIB. */
8458 gas_assert (i.tm.opcode_modifier.sib != VECSIB128
8459 && i.tm.opcode_modifier.sib != VECSIB256
8460 && i.tm.opcode_modifier.sib != VECSIB512);
8461
29b0f896
AM
8462 /* <disp>(%esp) becomes two byte modrm with no index
8463 register. We've already stored the code for esp
8464 in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
8465 Any base register besides %esp will not use the
8466 extra modrm byte. */
8467 i.sib.index = NO_INDEX_REGISTER;
29b0f896 8468 }
63112cd6 8469 else if (!i.tm.opcode_modifier.sib)
29b0f896 8470 {
e968fc9b 8471 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8472 i.sib.index = NO_INDEX_REGISTER;
8473 else
8474 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8475 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8476 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8477 i.rex |= REX_X;
29b0f896 8478 }
67a4f2b7
AO
8479
8480 if (i.disp_operands
8481 && (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
8482 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL))
8483 i.rm.mode = 0;
8484 else
a501d77e
L
8485 {
8486 if (!fake_zero_displacement
8487 && !i.disp_operands
8488 && i.disp_encoding)
8489 {
8490 fake_zero_displacement = 1;
8491 if (i.disp_encoding == disp_encoding_8bit)
8492 i.types[op].bitfield.disp8 = 1;
8493 else
8494 i.types[op].bitfield.disp32 = 1;
8495 }
8496 i.rm.mode = mode_from_disp_size (i.types[op]);
8497 }
29b0f896 8498 }
252b5132 8499
29b0f896
AM
8500 if (fake_zero_displacement)
8501 {
8502 /* Fakes a zero displacement assuming that i.types[op]
8503 holds the correct displacement size. */
8504 expressionS *exp;
8505
9c2799c2 8506 gas_assert (i.op[op].disps == 0);
29b0f896
AM
8507 exp = &disp_expressions[i.disp_operands++];
8508 i.op[op].disps = exp;
8509 exp->X_op = O_constant;
8510 exp->X_add_number = 0;
8511 exp->X_add_symbol = (symbolS *) 0;
8512 exp->X_op_symbol = (symbolS *) 0;
8513 }
c0f3af97
L
8514
8515 mem = op;
29b0f896 8516 }
c0f3af97
L
8517 else
8518 mem = ~0;
252b5132 8519
8c43a48b 8520 if (i.tm.opcode_modifier.vexsources == XOP2SOURCES)
5dd85c99
SP
8521 {
8522 if (operand_type_check (i.types[0], imm))
8523 i.vex.register_specifier = NULL;
8524 else
8525 {
8526 /* VEX.vvvv encodes one of the sources when the first
8527 operand is not an immediate. */
1ef99a7b 8528 if (i.tm.opcode_modifier.vexw == VEXW0)
5dd85c99
SP
8529 i.vex.register_specifier = i.op[0].regs;
8530 else
8531 i.vex.register_specifier = i.op[1].regs;
8532 }
8533
8534 /* Destination is a XMM register encoded in the ModRM.reg
8535 and VEX.R bit. */
8536 i.rm.reg = i.op[2].regs->reg_num;
8537 if ((i.op[2].regs->reg_flags & RegRex) != 0)
8538 i.rex |= REX_R;
8539
8540 /* ModRM.rm and VEX.B encodes the other source. */
8541 if (!i.mem_operands)
8542 {
8543 i.rm.mode = 3;
8544
1ef99a7b 8545 if (i.tm.opcode_modifier.vexw == VEXW0)
5dd85c99
SP
8546 i.rm.regmem = i.op[1].regs->reg_num;
8547 else
8548 i.rm.regmem = i.op[0].regs->reg_num;
8549
8550 if ((i.op[1].regs->reg_flags & RegRex) != 0)
8551 i.rex |= REX_B;
8552 }
8553 }
2426c15f 8554 else if (i.tm.opcode_modifier.vexvvvv == VEXLWP)
f88c9eb0
SP
8555 {
8556 i.vex.register_specifier = i.op[2].regs;
8557 if (!i.mem_operands)
8558 {
8559 i.rm.mode = 3;
8560 i.rm.regmem = i.op[1].regs->reg_num;
8561 if ((i.op[1].regs->reg_flags & RegRex) != 0)
8562 i.rex |= REX_B;
8563 }
8564 }
29b0f896
AM
8565 /* Fill in i.rm.reg or i.rm.regmem field with register operand
8566 (if any) based on i.tm.extension_opcode. Again, we must be
8567 careful to make sure that segment/control/debug/test/MMX
8568 registers are coded into the i.rm.reg field. */
f88c9eb0 8569 else if (i.reg_operands)
29b0f896 8570 {
99018f42 8571 unsigned int op;
7ab9ffdd
L
8572 unsigned int vex_reg = ~0;
8573
8574 for (op = 0; op < i.operands; op++)
921eafea
L
8575 if (i.types[op].bitfield.class == Reg
8576 || i.types[op].bitfield.class == RegBND
8577 || i.types[op].bitfield.class == RegMask
8578 || i.types[op].bitfield.class == SReg
8579 || i.types[op].bitfield.class == RegCR
8580 || i.types[op].bitfield.class == RegDR
8581 || i.types[op].bitfield.class == RegTR
8582 || i.types[op].bitfield.class == RegSIMD
8583 || i.types[op].bitfield.class == RegMMX)
8584 break;
c0209578 8585
7ab9ffdd
L
8586 if (vex_3_sources)
8587 op = dest;
2426c15f 8588 else if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
7ab9ffdd
L
8589 {
8590 /* For instructions with VexNDS, the register-only
8591 source operand is encoded in VEX prefix. */
8592 gas_assert (mem != (unsigned int) ~0);
c0f3af97 8593
7ab9ffdd 8594 if (op > mem)
c0f3af97 8595 {
7ab9ffdd
L
8596 vex_reg = op++;
8597 gas_assert (op < i.operands);
c0f3af97
L
8598 }
8599 else
c0f3af97 8600 {
f12dc422
L
8601 /* Check register-only source operand when two source
8602 operands are swapped. */
8603 if (!i.tm.operand_types[op].bitfield.baseindex
8604 && i.tm.operand_types[op + 1].bitfield.baseindex)
8605 {
8606 vex_reg = op;
8607 op += 2;
8608 gas_assert (mem == (vex_reg + 1)
8609 && op < i.operands);
8610 }
8611 else
8612 {
8613 vex_reg = op + 1;
8614 gas_assert (vex_reg < i.operands);
8615 }
c0f3af97 8616 }
7ab9ffdd 8617 }
2426c15f 8618 else if (i.tm.opcode_modifier.vexvvvv == VEXNDD)
7ab9ffdd 8619 {
f12dc422 8620 /* For instructions with VexNDD, the register destination
7ab9ffdd 8621 is encoded in VEX prefix. */
f12dc422
L
8622 if (i.mem_operands == 0)
8623 {
8624 /* There is no memory operand. */
8625 gas_assert ((op + 2) == i.operands);
8626 vex_reg = op + 1;
8627 }
8628 else
8d63c93e 8629 {
ed438a93
JB
8630 /* There are only 2 non-immediate operands. */
8631 gas_assert (op < i.imm_operands + 2
8632 && i.operands == i.imm_operands + 2);
8633 vex_reg = i.imm_operands + 1;
f12dc422 8634 }
7ab9ffdd
L
8635 }
8636 else
8637 gas_assert (op < i.operands);
99018f42 8638
7ab9ffdd
L
8639 if (vex_reg != (unsigned int) ~0)
8640 {
f12dc422 8641 i386_operand_type *type = &i.tm.operand_types[vex_reg];
7ab9ffdd 8642
bab6aec1 8643 if ((type->bitfield.class != Reg
dc821c5f 8644 || (!type->bitfield.dword && !type->bitfield.qword))
3528c362 8645 && type->bitfield.class != RegSIMD
43234a1e 8646 && !operand_type_equal (type, &regmask))
7ab9ffdd 8647 abort ();
f88c9eb0 8648
7ab9ffdd
L
8649 i.vex.register_specifier = i.op[vex_reg].regs;
8650 }
8651
1b9f0c97
L
8652 /* Don't set OP operand twice. */
8653 if (vex_reg != op)
7ab9ffdd 8654 {
1b9f0c97
L
8655 /* If there is an extension opcode to put here, the
8656 register number must be put into the regmem field. */
8657 if (i.tm.extension_opcode != None)
8658 {
8659 i.rm.regmem = i.op[op].regs->reg_num;
a5aeccd9
JB
8660 set_rex_vrex (i.op[op].regs, REX_B,
8661 i.tm.opcode_modifier.sse2avx);
1b9f0c97
L
8662 }
8663 else
8664 {
8665 i.rm.reg = i.op[op].regs->reg_num;
a5aeccd9
JB
8666 set_rex_vrex (i.op[op].regs, REX_R,
8667 i.tm.opcode_modifier.sse2avx);
1b9f0c97 8668 }
7ab9ffdd 8669 }
252b5132 8670
29b0f896
AM
8671 /* Now, if no memory operand has set i.rm.mode = 0, 1, 2 we
8672 must set it to 3 to indicate this is a register operand
8673 in the regmem field. */
8674 if (!i.mem_operands)
8675 i.rm.mode = 3;
8676 }
252b5132 8677
29b0f896 8678 /* Fill in i.rm.reg field with extension opcode (if any). */
c1e679ec 8679 if (i.tm.extension_opcode != None)
29b0f896
AM
8680 i.rm.reg = i.tm.extension_opcode;
8681 }
8682 return default_seg;
8683}
252b5132 8684
48ef937e
JB
8685static INLINE void
8686frag_opcode_byte (unsigned char byte)
8687{
8688 if (now_seg != absolute_section)
8689 FRAG_APPEND_1_CHAR (byte);
8690 else
8691 ++abs_section_offset;
8692}
8693
376cd056
JB
8694static unsigned int
8695flip_code16 (unsigned int code16)
8696{
8697 gas_assert (i.tm.operands == 1);
8698
8699 return !(i.prefix[REX_PREFIX] & REX_W)
8700 && (code16 ? i.tm.operand_types[0].bitfield.disp32
8701 || i.tm.operand_types[0].bitfield.disp32s
8702 : i.tm.operand_types[0].bitfield.disp16)
8703 ? CODE16 : 0;
8704}
8705
29b0f896 8706static void
e3bb37b5 8707output_branch (void)
29b0f896
AM
8708{
8709 char *p;
f8a5c266 8710 int size;
29b0f896
AM
8711 int code16;
8712 int prefix;
8713 relax_substateT subtype;
8714 symbolS *sym;
8715 offsetT off;
8716
48ef937e
JB
8717 if (now_seg == absolute_section)
8718 {
8719 as_bad (_("relaxable branches not supported in absolute section"));
8720 return;
8721 }
8722
f8a5c266 8723 code16 = flag_code == CODE_16BIT ? CODE16 : 0;
a501d77e 8724 size = i.disp_encoding == disp_encoding_32bit ? BIG : SMALL;
29b0f896
AM
8725
8726 prefix = 0;
8727 if (i.prefix[DATA_PREFIX] != 0)
252b5132 8728 {
29b0f896
AM
8729 prefix = 1;
8730 i.prefixes -= 1;
376cd056 8731 code16 ^= flip_code16(code16);
252b5132 8732 }
29b0f896
AM
8733 /* Pentium4 branch hints. */
8734 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8735 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
2f66722d 8736 {
29b0f896
AM
8737 prefix++;
8738 i.prefixes--;
8739 }
8740 if (i.prefix[REX_PREFIX] != 0)
8741 {
8742 prefix++;
8743 i.prefixes--;
2f66722d
AM
8744 }
8745
7e8b059b
L
8746 /* BND prefixed jump. */
8747 if (i.prefix[BND_PREFIX] != 0)
8748 {
6cb0a70e
JB
8749 prefix++;
8750 i.prefixes--;
7e8b059b
L
8751 }
8752
f2810fe0
JB
8753 if (i.prefixes != 0)
8754 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
29b0f896
AM
8755
8756 /* It's always a symbol; End frag & setup for relax.
8757 Make sure there is enough room in this frag for the largest
8758 instruction we may generate in md_convert_frag. This is 2
8759 bytes for the opcode and room for the prefix and largest
8760 displacement. */
8761 frag_grow (prefix + 2 + 4);
8762 /* Prefix and 1 opcode byte go in fr_fix. */
8763 p = frag_more (prefix + 1);
8764 if (i.prefix[DATA_PREFIX] != 0)
8765 *p++ = DATA_PREFIX_OPCODE;
8766 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
8767 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
8768 *p++ = i.prefix[SEG_PREFIX];
6cb0a70e
JB
8769 if (i.prefix[BND_PREFIX] != 0)
8770 *p++ = BND_PREFIX_OPCODE;
29b0f896
AM
8771 if (i.prefix[REX_PREFIX] != 0)
8772 *p++ = i.prefix[REX_PREFIX];
8773 *p = i.tm.base_opcode;
8774
8775 if ((unsigned char) *p == JUMP_PC_RELATIVE)
f8a5c266 8776 subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, size);
40fb9820 8777 else if (cpu_arch_flags.bitfield.cpui386)
f8a5c266 8778 subtype = ENCODE_RELAX_STATE (COND_JUMP, size);
29b0f896 8779 else
f8a5c266 8780 subtype = ENCODE_RELAX_STATE (COND_JUMP86, size);
29b0f896 8781 subtype |= code16;
3e73aa7c 8782
29b0f896
AM
8783 sym = i.op[0].disps->X_add_symbol;
8784 off = i.op[0].disps->X_add_number;
3e73aa7c 8785
29b0f896
AM
8786 if (i.op[0].disps->X_op != O_constant
8787 && i.op[0].disps->X_op != O_symbol)
3e73aa7c 8788 {
29b0f896
AM
8789 /* Handle complex expressions. */
8790 sym = make_expr_symbol (i.op[0].disps);
8791 off = 0;
8792 }
3e73aa7c 8793
29b0f896
AM
8794 /* 1 possible extra opcode + 4 byte displacement go in var part.
8795 Pass reloc in fr_var. */
d258b828 8796 frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
29b0f896 8797}
3e73aa7c 8798
bd7ab16b
L
8799#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8800/* Return TRUE iff PLT32 relocation should be used for branching to
8801 symbol S. */
8802
5b7c81bd 8803static bool
bd7ab16b
L
8804need_plt32_p (symbolS *s)
8805{
8806 /* PLT32 relocation is ELF only. */
8807 if (!IS_ELF)
5b7c81bd 8808 return false;
bd7ab16b 8809
a5def729
RO
8810#ifdef TE_SOLARIS
8811 /* Don't emit PLT32 relocation on Solaris: neither native linker nor
8812 krtld support it. */
5b7c81bd 8813 return false;
a5def729
RO
8814#endif
8815
bd7ab16b
L
8816 /* Since there is no need to prepare for PLT branch on x86-64, we
8817 can generate R_X86_64_PLT32, instead of R_X86_64_PC32, which can
8818 be used as a marker for 32-bit PC-relative branches. */
8819 if (!object_64bit)
5b7c81bd 8820 return false;
bd7ab16b 8821
44365e88 8822 if (s == NULL)
5b7c81bd 8823 return false;
44365e88 8824
bd7ab16b
L
8825 /* Weak or undefined symbol need PLT32 relocation. */
8826 if (S_IS_WEAK (s) || !S_IS_DEFINED (s))
5b7c81bd 8827 return true;
bd7ab16b
L
8828
8829 /* Non-global symbol doesn't need PLT32 relocation. */
8830 if (! S_IS_EXTERNAL (s))
5b7c81bd 8831 return false;
bd7ab16b
L
8832
8833 /* Other global symbols need PLT32 relocation. NB: Symbol with
8834 non-default visibilities are treated as normal global symbol
8835 so that PLT32 relocation can be used as a marker for 32-bit
8836 PC-relative branches. It is useful for linker relaxation. */
5b7c81bd 8837 return true;
bd7ab16b
L
8838}
8839#endif
8840
29b0f896 8841static void
e3bb37b5 8842output_jump (void)
29b0f896
AM
8843{
8844 char *p;
8845 int size;
3e02c1cc 8846 fixS *fixP;
bd7ab16b 8847 bfd_reloc_code_real_type jump_reloc = i.reloc[0];
29b0f896 8848
0cfa3eb3 8849 if (i.tm.opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
8850 {
8851 /* This is a loop or jecxz type instruction. */
8852 size = 1;
8853 if (i.prefix[ADDR_PREFIX] != 0)
8854 {
48ef937e 8855 frag_opcode_byte (ADDR_PREFIX_OPCODE);
29b0f896
AM
8856 i.prefixes -= 1;
8857 }
8858 /* Pentium4 branch hints. */
8859 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8860 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
8861 {
48ef937e 8862 frag_opcode_byte (i.prefix[SEG_PREFIX]);
29b0f896 8863 i.prefixes--;
3e73aa7c
JH
8864 }
8865 }
29b0f896
AM
8866 else
8867 {
8868 int code16;
3e73aa7c 8869
29b0f896
AM
8870 code16 = 0;
8871 if (flag_code == CODE_16BIT)
8872 code16 = CODE16;
3e73aa7c 8873
29b0f896
AM
8874 if (i.prefix[DATA_PREFIX] != 0)
8875 {
48ef937e 8876 frag_opcode_byte (DATA_PREFIX_OPCODE);
29b0f896 8877 i.prefixes -= 1;
376cd056 8878 code16 ^= flip_code16(code16);
29b0f896 8879 }
252b5132 8880
29b0f896
AM
8881 size = 4;
8882 if (code16)
8883 size = 2;
8884 }
9fcc94b6 8885
6cb0a70e
JB
8886 /* BND prefixed jump. */
8887 if (i.prefix[BND_PREFIX] != 0)
29b0f896 8888 {
48ef937e 8889 frag_opcode_byte (i.prefix[BND_PREFIX]);
29b0f896
AM
8890 i.prefixes -= 1;
8891 }
252b5132 8892
6cb0a70e 8893 if (i.prefix[REX_PREFIX] != 0)
7e8b059b 8894 {
48ef937e 8895 frag_opcode_byte (i.prefix[REX_PREFIX]);
7e8b059b
L
8896 i.prefixes -= 1;
8897 }
8898
f2810fe0
JB
8899 if (i.prefixes != 0)
8900 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
e0890092 8901
48ef937e
JB
8902 if (now_seg == absolute_section)
8903 {
9a182d04 8904 abs_section_offset += i.opcode_length + size;
48ef937e
JB
8905 return;
8906 }
8907
9a182d04
JB
8908 p = frag_more (i.opcode_length + size);
8909 switch (i.opcode_length)
42164a71
L
8910 {
8911 case 2:
8912 *p++ = i.tm.base_opcode >> 8;
1a0670f3 8913 /* Fall through. */
42164a71
L
8914 case 1:
8915 *p++ = i.tm.base_opcode;
8916 break;
8917 default:
8918 abort ();
8919 }
e0890092 8920
bd7ab16b
L
8921#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8922 if (size == 4
8923 && jump_reloc == NO_RELOC
8924 && need_plt32_p (i.op[0].disps->X_add_symbol))
8925 jump_reloc = BFD_RELOC_X86_64_PLT32;
8926#endif
8927
8928 jump_reloc = reloc (size, 1, 1, jump_reloc);
8929
3e02c1cc 8930 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
bd7ab16b 8931 i.op[0].disps, 1, jump_reloc);
3e02c1cc 8932
eb19308f
JB
8933 /* All jumps handled here are signed, but don't unconditionally use a
8934 signed limit check for 32 and 16 bit jumps as we want to allow wrap
8935 around at 4G (outside of 64-bit mode) and 64k (except for XBEGIN)
8936 respectively. */
8937 switch (size)
8938 {
8939 case 1:
8940 fixP->fx_signed = 1;
8941 break;
8942
8943 case 2:
8944 if (i.tm.base_opcode == 0xc7f8)
8945 fixP->fx_signed = 1;
8946 break;
8947
8948 case 4:
8949 if (flag_code == CODE_64BIT)
8950 fixP->fx_signed = 1;
8951 break;
8952 }
29b0f896 8953}
e0890092 8954
29b0f896 8955static void
e3bb37b5 8956output_interseg_jump (void)
29b0f896
AM
8957{
8958 char *p;
8959 int size;
8960 int prefix;
8961 int code16;
252b5132 8962
29b0f896
AM
8963 code16 = 0;
8964 if (flag_code == CODE_16BIT)
8965 code16 = CODE16;
a217f122 8966
29b0f896
AM
8967 prefix = 0;
8968 if (i.prefix[DATA_PREFIX] != 0)
8969 {
8970 prefix = 1;
8971 i.prefixes -= 1;
8972 code16 ^= CODE16;
8973 }
6cb0a70e
JB
8974
8975 gas_assert (!i.prefix[REX_PREFIX]);
252b5132 8976
29b0f896
AM
8977 size = 4;
8978 if (code16)
8979 size = 2;
252b5132 8980
f2810fe0
JB
8981 if (i.prefixes != 0)
8982 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
252b5132 8983
48ef937e
JB
8984 if (now_seg == absolute_section)
8985 {
8986 abs_section_offset += prefix + 1 + 2 + size;
8987 return;
8988 }
8989
29b0f896
AM
8990 /* 1 opcode; 2 segment; offset */
8991 p = frag_more (prefix + 1 + 2 + size);
3e73aa7c 8992
29b0f896
AM
8993 if (i.prefix[DATA_PREFIX] != 0)
8994 *p++ = DATA_PREFIX_OPCODE;
252b5132 8995
29b0f896
AM
8996 if (i.prefix[REX_PREFIX] != 0)
8997 *p++ = i.prefix[REX_PREFIX];
252b5132 8998
29b0f896
AM
8999 *p++ = i.tm.base_opcode;
9000 if (i.op[1].imms->X_op == O_constant)
9001 {
9002 offsetT n = i.op[1].imms->X_add_number;
252b5132 9003
29b0f896
AM
9004 if (size == 2
9005 && !fits_in_unsigned_word (n)
9006 && !fits_in_signed_word (n))
9007 {
9008 as_bad (_("16-bit jump out of range"));
9009 return;
9010 }
9011 md_number_to_chars (p, n, size);
9012 }
9013 else
9014 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
d258b828 9015 i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
6d96a594
C
9016
9017 p += size;
9018 if (i.op[0].imms->X_op == O_constant)
9019 md_number_to_chars (p, (valueT) i.op[0].imms->X_add_number, 2);
9020 else
9021 fix_new_exp (frag_now, p - frag_now->fr_literal, 2,
9022 i.op[0].imms, 0, reloc (2, 0, 0, i.reloc[0]));
29b0f896 9023}
a217f122 9024
b4a3a7b4
L
9025#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9026void
9027x86_cleanup (void)
9028{
9029 char *p;
9030 asection *seg = now_seg;
9031 subsegT subseg = now_subseg;
9032 asection *sec;
9033 unsigned int alignment, align_size_1;
9034 unsigned int isa_1_descsz, feature_2_descsz, descsz;
9035 unsigned int isa_1_descsz_raw, feature_2_descsz_raw;
9036 unsigned int padding;
9037
1273b2f8 9038 if (!IS_ELF || !x86_used_note)
b4a3a7b4
L
9039 return;
9040
b4a3a7b4
L
9041 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X86;
9042
9043 /* The .note.gnu.property section layout:
9044
9045 Field Length Contents
9046 ---- ---- ----
9047 n_namsz 4 4
9048 n_descsz 4 The note descriptor size
9049 n_type 4 NT_GNU_PROPERTY_TYPE_0
9050 n_name 4 "GNU"
9051 n_desc n_descsz The program property array
9052 .... .... ....
9053 */
9054
9055 /* Create the .note.gnu.property section. */
9056 sec = subseg_new (NOTE_GNU_PROPERTY_SECTION_NAME, 0);
fd361982 9057 bfd_set_section_flags (sec,
b4a3a7b4
L
9058 (SEC_ALLOC
9059 | SEC_LOAD
9060 | SEC_DATA
9061 | SEC_HAS_CONTENTS
9062 | SEC_READONLY));
9063
9064 if (get_elf_backend_data (stdoutput)->s->elfclass == ELFCLASS64)
9065 {
9066 align_size_1 = 7;
9067 alignment = 3;
9068 }
9069 else
9070 {
9071 align_size_1 = 3;
9072 alignment = 2;
9073 }
9074
fd361982 9075 bfd_set_section_alignment (sec, alignment);
b4a3a7b4
L
9076 elf_section_type (sec) = SHT_NOTE;
9077
1273b2f8
L
9078 /* GNU_PROPERTY_X86_ISA_1_USED: 4-byte type + 4-byte data size
9079 + 4-byte data */
9080 isa_1_descsz_raw = 4 + 4 + 4;
9081 /* Align GNU_PROPERTY_X86_ISA_1_USED. */
9082 isa_1_descsz = (isa_1_descsz_raw + align_size_1) & ~align_size_1;
b4a3a7b4
L
9083
9084 feature_2_descsz_raw = isa_1_descsz;
9085 /* GNU_PROPERTY_X86_FEATURE_2_USED: 4-byte type + 4-byte data size
1273b2f8 9086 + 4-byte data */
b4a3a7b4
L
9087 feature_2_descsz_raw += 4 + 4 + 4;
9088 /* Align GNU_PROPERTY_X86_FEATURE_2_USED. */
9089 feature_2_descsz = ((feature_2_descsz_raw + align_size_1)
9090 & ~align_size_1);
9091
9092 descsz = feature_2_descsz;
9093 /* Section size: n_namsz + n_descsz + n_type + n_name + n_descsz. */
9094 p = frag_more (4 + 4 + 4 + 4 + descsz);
9095
9096 /* Write n_namsz. */
9097 md_number_to_chars (p, (valueT) 4, 4);
9098
9099 /* Write n_descsz. */
9100 md_number_to_chars (p + 4, (valueT) descsz, 4);
9101
9102 /* Write n_type. */
9103 md_number_to_chars (p + 4 * 2, (valueT) NT_GNU_PROPERTY_TYPE_0, 4);
9104
9105 /* Write n_name. */
9106 memcpy (p + 4 * 3, "GNU", 4);
9107
1273b2f8
L
9108 /* Write 4-byte type. */
9109 md_number_to_chars (p + 4 * 4,
9110 (valueT) GNU_PROPERTY_X86_ISA_1_USED, 4);
b4a3a7b4 9111
1273b2f8
L
9112 /* Write 4-byte data size. */
9113 md_number_to_chars (p + 4 * 5, (valueT) 4, 4);
b4a3a7b4 9114
1273b2f8
L
9115 /* Write 4-byte data. */
9116 md_number_to_chars (p + 4 * 6, (valueT) x86_isa_1_used, 4);
b4a3a7b4 9117
1273b2f8
L
9118 /* Zero out paddings. */
9119 padding = isa_1_descsz - isa_1_descsz_raw;
9120 if (padding)
9121 memset (p + 4 * 7, 0, padding);
b4a3a7b4
L
9122
9123 /* Write 4-byte type. */
9124 md_number_to_chars (p + isa_1_descsz + 4 * 4,
9125 (valueT) GNU_PROPERTY_X86_FEATURE_2_USED, 4);
9126
9127 /* Write 4-byte data size. */
9128 md_number_to_chars (p + isa_1_descsz + 4 * 5, (valueT) 4, 4);
9129
9130 /* Write 4-byte data. */
9131 md_number_to_chars (p + isa_1_descsz + 4 * 6,
9132 (valueT) x86_feature_2_used, 4);
9133
9134 /* Zero out paddings. */
9135 padding = feature_2_descsz - feature_2_descsz_raw;
9136 if (padding)
9137 memset (p + isa_1_descsz + 4 * 7, 0, padding);
9138
9139 /* We probably can't restore the current segment, for there likely
9140 isn't one yet... */
9141 if (seg && subseg)
9142 subseg_set (seg, subseg);
9143}
9144#endif
9145
9c33702b
JB
9146static unsigned int
9147encoding_length (const fragS *start_frag, offsetT start_off,
9148 const char *frag_now_ptr)
9149{
9150 unsigned int len = 0;
9151
9152 if (start_frag != frag_now)
9153 {
9154 const fragS *fr = start_frag;
9155
9156 do {
9157 len += fr->fr_fix;
9158 fr = fr->fr_next;
9159 } while (fr && fr != frag_now);
9160 }
9161
9162 return len - start_off + (frag_now_ptr - frag_now->fr_literal);
9163}
9164
e379e5f3 9165/* Return 1 for test, and, cmp, add, sub, inc and dec which may
79d72f45
HL
9166 be macro-fused with conditional jumps.
9167 NB: If TEST/AND/CMP/ADD/SUB/INC/DEC is of RIP relative address,
9168 or is one of the following format:
9169
9170 cmp m, imm
9171 add m, imm
9172 sub m, imm
9173 test m, imm
9174 and m, imm
9175 inc m
9176 dec m
9177
9178 it is unfusible. */
e379e5f3
L
9179
9180static int
79d72f45 9181maybe_fused_with_jcc_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
9182{
9183 /* No RIP address. */
9184 if (i.base_reg && i.base_reg->reg_num == RegIP)
9185 return 0;
9186
389d00a5
JB
9187 /* No opcodes outside of base encoding space. */
9188 if (i.tm.opcode_modifier.opcodespace != SPACE_BASE)
e379e5f3
L
9189 return 0;
9190
79d72f45
HL
9191 /* add, sub without add/sub m, imm. */
9192 if (i.tm.base_opcode <= 5
e379e5f3
L
9193 || (i.tm.base_opcode >= 0x28 && i.tm.base_opcode <= 0x2d)
9194 || ((i.tm.base_opcode | 3) == 0x83
79d72f45 9195 && (i.tm.extension_opcode == 0x5
e379e5f3 9196 || i.tm.extension_opcode == 0x0)))
79d72f45
HL
9197 {
9198 *mf_cmp_p = mf_cmp_alu_cmp;
9199 return !(i.mem_operands && i.imm_operands);
9200 }
e379e5f3 9201
79d72f45
HL
9202 /* and without and m, imm. */
9203 if ((i.tm.base_opcode >= 0x20 && i.tm.base_opcode <= 0x25)
9204 || ((i.tm.base_opcode | 3) == 0x83
9205 && i.tm.extension_opcode == 0x4))
9206 {
9207 *mf_cmp_p = mf_cmp_test_and;
9208 return !(i.mem_operands && i.imm_operands);
9209 }
9210
9211 /* test without test m imm. */
e379e5f3
L
9212 if ((i.tm.base_opcode | 1) == 0x85
9213 || (i.tm.base_opcode | 1) == 0xa9
9214 || ((i.tm.base_opcode | 1) == 0xf7
79d72f45
HL
9215 && i.tm.extension_opcode == 0))
9216 {
9217 *mf_cmp_p = mf_cmp_test_and;
9218 return !(i.mem_operands && i.imm_operands);
9219 }
9220
9221 /* cmp without cmp m, imm. */
9222 if ((i.tm.base_opcode >= 0x38 && i.tm.base_opcode <= 0x3d)
e379e5f3
L
9223 || ((i.tm.base_opcode | 3) == 0x83
9224 && (i.tm.extension_opcode == 0x7)))
79d72f45
HL
9225 {
9226 *mf_cmp_p = mf_cmp_alu_cmp;
9227 return !(i.mem_operands && i.imm_operands);
9228 }
e379e5f3 9229
79d72f45 9230 /* inc, dec without inc/dec m. */
e379e5f3
L
9231 if ((i.tm.cpu_flags.bitfield.cpuno64
9232 && (i.tm.base_opcode | 0xf) == 0x4f)
9233 || ((i.tm.base_opcode | 1) == 0xff
9234 && i.tm.extension_opcode <= 0x1))
79d72f45
HL
9235 {
9236 *mf_cmp_p = mf_cmp_incdec;
9237 return !i.mem_operands;
9238 }
e379e5f3
L
9239
9240 return 0;
9241}
9242
9243/* Return 1 if a FUSED_JCC_PADDING frag should be generated. */
9244
9245static int
79d72f45 9246add_fused_jcc_padding_frag_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
9247{
9248 /* NB: Don't work with COND_JUMP86 without i386. */
9249 if (!align_branch_power
9250 || now_seg == absolute_section
9251 || !cpu_arch_flags.bitfield.cpui386
9252 || !(align_branch & align_branch_fused_bit))
9253 return 0;
9254
79d72f45 9255 if (maybe_fused_with_jcc_p (mf_cmp_p))
e379e5f3
L
9256 {
9257 if (last_insn.kind == last_insn_other
9258 || last_insn.seg != now_seg)
9259 return 1;
9260 if (flag_debug)
9261 as_warn_where (last_insn.file, last_insn.line,
9262 _("`%s` skips -malign-branch-boundary on `%s`"),
9263 last_insn.name, i.tm.name);
9264 }
9265
9266 return 0;
9267}
9268
9269/* Return 1 if a BRANCH_PREFIX frag should be generated. */
9270
9271static int
9272add_branch_prefix_frag_p (void)
9273{
9274 /* NB: Don't work with COND_JUMP86 without i386. Don't add prefix
9275 to PadLock instructions since they include prefixes in opcode. */
9276 if (!align_branch_power
9277 || !align_branch_prefix_size
9278 || now_seg == absolute_section
9279 || i.tm.cpu_flags.bitfield.cpupadlock
9280 || !cpu_arch_flags.bitfield.cpui386)
9281 return 0;
9282
9283 /* Don't add prefix if it is a prefix or there is no operand in case
9284 that segment prefix is special. */
9285 if (!i.operands || i.tm.opcode_modifier.isprefix)
9286 return 0;
9287
9288 if (last_insn.kind == last_insn_other
9289 || last_insn.seg != now_seg)
9290 return 1;
9291
9292 if (flag_debug)
9293 as_warn_where (last_insn.file, last_insn.line,
9294 _("`%s` skips -malign-branch-boundary on `%s`"),
9295 last_insn.name, i.tm.name);
9296
9297 return 0;
9298}
9299
9300/* Return 1 if a BRANCH_PADDING frag should be generated. */
9301
9302static int
79d72f45
HL
9303add_branch_padding_frag_p (enum align_branch_kind *branch_p,
9304 enum mf_jcc_kind *mf_jcc_p)
e379e5f3
L
9305{
9306 int add_padding;
9307
9308 /* NB: Don't work with COND_JUMP86 without i386. */
9309 if (!align_branch_power
9310 || now_seg == absolute_section
389d00a5
JB
9311 || !cpu_arch_flags.bitfield.cpui386
9312 || i.tm.opcode_modifier.opcodespace != SPACE_BASE)
e379e5f3
L
9313 return 0;
9314
9315 add_padding = 0;
9316
9317 /* Check for jcc and direct jmp. */
9318 if (i.tm.opcode_modifier.jump == JUMP)
9319 {
9320 if (i.tm.base_opcode == JUMP_PC_RELATIVE)
9321 {
9322 *branch_p = align_branch_jmp;
9323 add_padding = align_branch & align_branch_jmp_bit;
9324 }
9325 else
9326 {
79d72f45
HL
9327 /* Because J<cc> and JN<cc> share same group in macro-fusible table,
9328 igore the lowest bit. */
9329 *mf_jcc_p = (i.tm.base_opcode & 0x0e) >> 1;
e379e5f3
L
9330 *branch_p = align_branch_jcc;
9331 if ((align_branch & align_branch_jcc_bit))
9332 add_padding = 1;
9333 }
9334 }
e379e5f3
L
9335 else if ((i.tm.base_opcode | 1) == 0xc3)
9336 {
9337 /* Near ret. */
9338 *branch_p = align_branch_ret;
9339 if ((align_branch & align_branch_ret_bit))
9340 add_padding = 1;
9341 }
9342 else
9343 {
9344 /* Check for indirect jmp, direct and indirect calls. */
9345 if (i.tm.base_opcode == 0xe8)
9346 {
9347 /* Direct call. */
9348 *branch_p = align_branch_call;
9349 if ((align_branch & align_branch_call_bit))
9350 add_padding = 1;
9351 }
9352 else if (i.tm.base_opcode == 0xff
9353 && (i.tm.extension_opcode == 2
9354 || i.tm.extension_opcode == 4))
9355 {
9356 /* Indirect call and jmp. */
9357 *branch_p = align_branch_indirect;
9358 if ((align_branch & align_branch_indirect_bit))
9359 add_padding = 1;
9360 }
9361
9362 if (add_padding
9363 && i.disp_operands
9364 && tls_get_addr
9365 && (i.op[0].disps->X_op == O_symbol
9366 || (i.op[0].disps->X_op == O_subtract
9367 && i.op[0].disps->X_op_symbol == GOT_symbol)))
9368 {
9369 symbolS *s = i.op[0].disps->X_add_symbol;
9370 /* No padding to call to global or undefined tls_get_addr. */
9371 if ((S_IS_EXTERNAL (s) || !S_IS_DEFINED (s))
9372 && strcmp (S_GET_NAME (s), tls_get_addr) == 0)
9373 return 0;
9374 }
9375 }
9376
9377 if (add_padding
9378 && last_insn.kind != last_insn_other
9379 && last_insn.seg == now_seg)
9380 {
9381 if (flag_debug)
9382 as_warn_where (last_insn.file, last_insn.line,
9383 _("`%s` skips -malign-branch-boundary on `%s`"),
9384 last_insn.name, i.tm.name);
9385 return 0;
9386 }
9387
9388 return add_padding;
9389}
9390
29b0f896 9391static void
e3bb37b5 9392output_insn (void)
29b0f896 9393{
2bbd9c25
JJ
9394 fragS *insn_start_frag;
9395 offsetT insn_start_off;
e379e5f3
L
9396 fragS *fragP = NULL;
9397 enum align_branch_kind branch = align_branch_none;
79d72f45
HL
9398 /* The initializer is arbitrary just to avoid uninitialized error.
9399 it's actually either assigned in add_branch_padding_frag_p
9400 or never be used. */
9401 enum mf_jcc_kind mf_jcc = mf_jcc_jo;
2bbd9c25 9402
b4a3a7b4 9403#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
48ef937e 9404 if (IS_ELF && x86_used_note && now_seg != absolute_section)
b4a3a7b4 9405 {
32930e4e
L
9406 if ((i.xstate & xstate_tmm) == xstate_tmm
9407 || i.tm.cpu_flags.bitfield.cpuamx_tile)
9408 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_TMM;
9409
b4a3a7b4
L
9410 if (i.tm.cpu_flags.bitfield.cpu8087
9411 || i.tm.cpu_flags.bitfield.cpu287
9412 || i.tm.cpu_flags.bitfield.cpu387
9413 || i.tm.cpu_flags.bitfield.cpu687
9414 || i.tm.cpu_flags.bitfield.cpufisttp)
9415 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X87;
014d61ea 9416
921eafea 9417 if ((i.xstate & xstate_mmx)
389d00a5
JB
9418 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
9419 && !is_any_vex_encoding (&i.tm)
9420 && (i.tm.base_opcode == 0x77 /* emms */
9421 || i.tm.base_opcode == 0x0e /* femms */)))
b4a3a7b4 9422 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MMX;
014d61ea 9423
32930e4e
L
9424 if (i.index_reg)
9425 {
9426 if (i.index_reg->reg_type.bitfield.zmmword)
9427 i.xstate |= xstate_zmm;
9428 else if (i.index_reg->reg_type.bitfield.ymmword)
9429 i.xstate |= xstate_ymm;
9430 else if (i.index_reg->reg_type.bitfield.xmmword)
9431 i.xstate |= xstate_xmm;
9432 }
014d61ea
JB
9433
9434 /* vzeroall / vzeroupper */
9435 if (i.tm.base_opcode == 0x77 && i.tm.cpu_flags.bitfield.cpuavx)
9436 i.xstate |= xstate_ymm;
9437
c4694f17 9438 if ((i.xstate & xstate_xmm)
389d00a5
JB
9439 /* ldmxcsr / stmxcsr / vldmxcsr / vstmxcsr */
9440 || (i.tm.base_opcode == 0xae
9441 && (i.tm.cpu_flags.bitfield.cpusse
9442 || i.tm.cpu_flags.bitfield.cpuavx))
c4694f17
TG
9443 || i.tm.cpu_flags.bitfield.cpuwidekl
9444 || i.tm.cpu_flags.bitfield.cpukl)
b4a3a7b4 9445 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XMM;
014d61ea 9446
921eafea 9447 if ((i.xstate & xstate_ymm) == xstate_ymm)
b4a3a7b4 9448 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_YMM;
921eafea 9449 if ((i.xstate & xstate_zmm) == xstate_zmm)
b4a3a7b4 9450 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_ZMM;
6225c532 9451 if (i.mask.reg || (i.xstate & xstate_mask) == xstate_mask)
32930e4e 9452 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MASK;
b4a3a7b4
L
9453 if (i.tm.cpu_flags.bitfield.cpufxsr)
9454 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_FXSR;
9455 if (i.tm.cpu_flags.bitfield.cpuxsave)
9456 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVE;
9457 if (i.tm.cpu_flags.bitfield.cpuxsaveopt)
9458 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT;
9459 if (i.tm.cpu_flags.bitfield.cpuxsavec)
9460 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEC;
b0ab0693
L
9461
9462 if (x86_feature_2_used
9463 || i.tm.cpu_flags.bitfield.cpucmov
9464 || i.tm.cpu_flags.bitfield.cpusyscall
389d00a5
JB
9465 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
9466 && i.tm.base_opcode == 0xc7
70e95837 9467 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
b0ab0693
L
9468 && i.tm.extension_opcode == 1) /* cmpxchg8b */)
9469 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_BASELINE;
9470 if (i.tm.cpu_flags.bitfield.cpusse3
9471 || i.tm.cpu_flags.bitfield.cpussse3
9472 || i.tm.cpu_flags.bitfield.cpusse4_1
9473 || i.tm.cpu_flags.bitfield.cpusse4_2
9474 || i.tm.cpu_flags.bitfield.cpucx16
9475 || i.tm.cpu_flags.bitfield.cpupopcnt
9476 /* LAHF-SAHF insns in 64-bit mode. */
9477 || (flag_code == CODE_64BIT
35648716
JB
9478 && (i.tm.base_opcode | 1) == 0x9f
9479 && i.tm.opcode_modifier.opcodespace == SPACE_BASE))
b0ab0693
L
9480 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V2;
9481 if (i.tm.cpu_flags.bitfield.cpuavx
9482 || i.tm.cpu_flags.bitfield.cpuavx2
9483 /* Any VEX encoded insns execpt for CpuAVX512F, CpuAVX512BW,
9484 CpuAVX512DQ, LPW, TBM and AMX. */
9485 || (i.tm.opcode_modifier.vex
9486 && !i.tm.cpu_flags.bitfield.cpuavx512f
9487 && !i.tm.cpu_flags.bitfield.cpuavx512bw
9488 && !i.tm.cpu_flags.bitfield.cpuavx512dq
9489 && !i.tm.cpu_flags.bitfield.cpulwp
9490 && !i.tm.cpu_flags.bitfield.cputbm
9491 && !(x86_feature_2_used & GNU_PROPERTY_X86_FEATURE_2_TMM))
9492 || i.tm.cpu_flags.bitfield.cpuf16c
9493 || i.tm.cpu_flags.bitfield.cpufma
9494 || i.tm.cpu_flags.bitfield.cpulzcnt
9495 || i.tm.cpu_flags.bitfield.cpumovbe
9496 || i.tm.cpu_flags.bitfield.cpuxsaves
9497 || (x86_feature_2_used
9498 & (GNU_PROPERTY_X86_FEATURE_2_XSAVE
9499 | GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT
9500 | GNU_PROPERTY_X86_FEATURE_2_XSAVEC)) != 0)
9501 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V3;
9502 if (i.tm.cpu_flags.bitfield.cpuavx512f
9503 || i.tm.cpu_flags.bitfield.cpuavx512bw
9504 || i.tm.cpu_flags.bitfield.cpuavx512dq
9505 || i.tm.cpu_flags.bitfield.cpuavx512vl
9506 /* Any EVEX encoded insns except for AVX512ER, AVX512PF and
9507 VNNIW. */
9508 || (i.tm.opcode_modifier.evex
9509 && !i.tm.cpu_flags.bitfield.cpuavx512er
9510 && !i.tm.cpu_flags.bitfield.cpuavx512pf
9511 && !i.tm.cpu_flags.bitfield.cpuavx512_4vnniw))
9512 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V4;
b4a3a7b4
L
9513 }
9514#endif
9515
29b0f896
AM
9516 /* Tie dwarf2 debug info to the address at the start of the insn.
9517 We can't do this after the insn has been output as the current
9518 frag may have been closed off. eg. by frag_var. */
9519 dwarf2_emit_insn (0);
9520
2bbd9c25
JJ
9521 insn_start_frag = frag_now;
9522 insn_start_off = frag_now_fix ();
9523
79d72f45 9524 if (add_branch_padding_frag_p (&branch, &mf_jcc))
e379e5f3
L
9525 {
9526 char *p;
9527 /* Branch can be 8 bytes. Leave some room for prefixes. */
9528 unsigned int max_branch_padding_size = 14;
9529
9530 /* Align section to boundary. */
9531 record_alignment (now_seg, align_branch_power);
9532
9533 /* Make room for padding. */
9534 frag_grow (max_branch_padding_size);
9535
9536 /* Start of the padding. */
9537 p = frag_more (0);
9538
9539 fragP = frag_now;
9540
9541 frag_var (rs_machine_dependent, max_branch_padding_size, 0,
9542 ENCODE_RELAX_STATE (BRANCH_PADDING, 0),
9543 NULL, 0, p);
9544
79d72f45 9545 fragP->tc_frag_data.mf_type = mf_jcc;
e379e5f3
L
9546 fragP->tc_frag_data.branch_type = branch;
9547 fragP->tc_frag_data.max_bytes = max_branch_padding_size;
9548 }
9549
29b0f896 9550 /* Output jumps. */
0cfa3eb3 9551 if (i.tm.opcode_modifier.jump == JUMP)
29b0f896 9552 output_branch ();
0cfa3eb3
JB
9553 else if (i.tm.opcode_modifier.jump == JUMP_BYTE
9554 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896 9555 output_jump ();
0cfa3eb3 9556 else if (i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT)
29b0f896
AM
9557 output_interseg_jump ();
9558 else
9559 {
9560 /* Output normal instructions here. */
9561 char *p;
9562 unsigned char *q;
47465058 9563 unsigned int j;
79d72f45 9564 enum mf_cmp_kind mf_cmp;
4dffcebc 9565
e4e00185 9566 if (avoid_fence
389d00a5
JB
9567 && (i.tm.base_opcode == 0xaee8
9568 || i.tm.base_opcode == 0xaef0
9569 || i.tm.base_opcode == 0xaef8))
48ef937e
JB
9570 {
9571 /* Encode lfence, mfence, and sfence as
9572 f0 83 04 24 00 lock addl $0x0, (%{re}sp). */
9573 if (now_seg != absolute_section)
9574 {
9575 offsetT val = 0x240483f0ULL;
9576
9577 p = frag_more (5);
9578 md_number_to_chars (p, val, 5);
9579 }
9580 else
9581 abs_section_offset += 5;
9582 return;
9583 }
e4e00185 9584
d022bddd
IT
9585 /* Some processors fail on LOCK prefix. This options makes
9586 assembler ignore LOCK prefix and serves as a workaround. */
9587 if (omit_lock_prefix)
9588 {
35648716
JB
9589 if (i.tm.base_opcode == LOCK_PREFIX_OPCODE
9590 && i.tm.opcode_modifier.isprefix)
d022bddd
IT
9591 return;
9592 i.prefix[LOCK_PREFIX] = 0;
9593 }
9594
e379e5f3
L
9595 if (branch)
9596 /* Skip if this is a branch. */
9597 ;
79d72f45 9598 else if (add_fused_jcc_padding_frag_p (&mf_cmp))
e379e5f3
L
9599 {
9600 /* Make room for padding. */
9601 frag_grow (MAX_FUSED_JCC_PADDING_SIZE);
9602 p = frag_more (0);
9603
9604 fragP = frag_now;
9605
9606 frag_var (rs_machine_dependent, MAX_FUSED_JCC_PADDING_SIZE, 0,
9607 ENCODE_RELAX_STATE (FUSED_JCC_PADDING, 0),
9608 NULL, 0, p);
9609
79d72f45 9610 fragP->tc_frag_data.mf_type = mf_cmp;
e379e5f3
L
9611 fragP->tc_frag_data.branch_type = align_branch_fused;
9612 fragP->tc_frag_data.max_bytes = MAX_FUSED_JCC_PADDING_SIZE;
9613 }
9614 else if (add_branch_prefix_frag_p ())
9615 {
9616 unsigned int max_prefix_size = align_branch_prefix_size;
9617
9618 /* Make room for padding. */
9619 frag_grow (max_prefix_size);
9620 p = frag_more (0);
9621
9622 fragP = frag_now;
9623
9624 frag_var (rs_machine_dependent, max_prefix_size, 0,
9625 ENCODE_RELAX_STATE (BRANCH_PREFIX, 0),
9626 NULL, 0, p);
9627
9628 fragP->tc_frag_data.max_bytes = max_prefix_size;
9629 }
9630
43234a1e
L
9631 /* Since the VEX/EVEX prefix contains the implicit prefix, we
9632 don't need the explicit prefix. */
9633 if (!i.tm.opcode_modifier.vex && !i.tm.opcode_modifier.evex)
bc4bd9ab 9634 {
7b47a312 9635 switch (i.tm.opcode_modifier.opcodeprefix)
bc4bd9ab 9636 {
7b47a312
L
9637 case PREFIX_0X66:
9638 add_prefix (0x66);
9639 break;
9640 case PREFIX_0XF2:
9641 add_prefix (0xf2);
9642 break;
9643 case PREFIX_0XF3:
8b65b895
L
9644 if (!i.tm.cpu_flags.bitfield.cpupadlock
9645 || (i.prefix[REP_PREFIX] != 0xf3))
9646 add_prefix (0xf3);
c0f3af97 9647 break;
7b47a312 9648 case PREFIX_NONE:
9a182d04 9649 switch (i.opcode_length)
c0f3af97 9650 {
7b47a312 9651 case 2:
7b47a312 9652 break;
9a182d04 9653 case 1:
7b47a312 9654 /* Check for pseudo prefixes. */
9a182d04
JB
9655 if (!i.tm.opcode_modifier.isprefix || i.tm.base_opcode)
9656 break;
7b47a312
L
9657 as_bad_where (insn_start_frag->fr_file,
9658 insn_start_frag->fr_line,
9659 _("pseudo prefix without instruction"));
9660 return;
9661 default:
9662 abort ();
4dffcebc 9663 }
c0f3af97 9664 break;
c0f3af97
L
9665 default:
9666 abort ();
bc4bd9ab 9667 }
c0f3af97 9668
6d19a37a 9669#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
cf61b747
L
9670 /* For x32, add a dummy REX_OPCODE prefix for mov/add with
9671 R_X86_64_GOTTPOFF relocation so that linker can safely
14470f07
L
9672 perform IE->LE optimization. A dummy REX_OPCODE prefix
9673 is also needed for lea with R_X86_64_GOTPC32_TLSDESC
9674 relocation for GDesc -> IE/LE optimization. */
cf61b747
L
9675 if (x86_elf_abi == X86_64_X32_ABI
9676 && i.operands == 2
14470f07
L
9677 && (i.reloc[0] == BFD_RELOC_X86_64_GOTTPOFF
9678 || i.reloc[0] == BFD_RELOC_X86_64_GOTPC32_TLSDESC)
cf61b747
L
9679 && i.prefix[REX_PREFIX] == 0)
9680 add_prefix (REX_OPCODE);
6d19a37a 9681#endif
cf61b747 9682
c0f3af97
L
9683 /* The prefix bytes. */
9684 for (j = ARRAY_SIZE (i.prefix), q = i.prefix; j > 0; j--, q++)
9685 if (*q)
48ef937e 9686 frag_opcode_byte (*q);
0f10071e 9687 }
ae5c1c7b 9688 else
c0f3af97
L
9689 {
9690 for (j = 0, q = i.prefix; j < ARRAY_SIZE (i.prefix); j++, q++)
9691 if (*q)
9692 switch (j)
9693 {
c0f3af97
L
9694 case SEG_PREFIX:
9695 case ADDR_PREFIX:
48ef937e 9696 frag_opcode_byte (*q);
c0f3af97
L
9697 break;
9698 default:
9699 /* There should be no other prefixes for instructions
9700 with VEX prefix. */
9701 abort ();
9702 }
9703
43234a1e
L
9704 /* For EVEX instructions i.vrex should become 0 after
9705 build_evex_prefix. For VEX instructions upper 16 registers
9706 aren't available, so VREX should be 0. */
9707 if (i.vrex)
9708 abort ();
c0f3af97 9709 /* Now the VEX prefix. */
48ef937e
JB
9710 if (now_seg != absolute_section)
9711 {
9712 p = frag_more (i.vex.length);
9713 for (j = 0; j < i.vex.length; j++)
9714 p[j] = i.vex.bytes[j];
9715 }
9716 else
9717 abs_section_offset += i.vex.length;
c0f3af97 9718 }
252b5132 9719
29b0f896 9720 /* Now the opcode; be careful about word order here! */
389d00a5
JB
9721 j = i.opcode_length;
9722 if (!i.vex.length)
9723 switch (i.tm.opcode_modifier.opcodespace)
9724 {
9725 case SPACE_BASE:
9726 break;
9727 case SPACE_0F:
9728 ++j;
9729 break;
9730 case SPACE_0F38:
9731 case SPACE_0F3A:
9732 j += 2;
9733 break;
9734 default:
9735 abort ();
9736 }
9737
48ef937e 9738 if (now_seg == absolute_section)
389d00a5
JB
9739 abs_section_offset += j;
9740 else if (j == 1)
29b0f896
AM
9741 {
9742 FRAG_APPEND_1_CHAR (i.tm.base_opcode);
9743 }
9744 else
9745 {
389d00a5
JB
9746 p = frag_more (j);
9747 if (!i.vex.length
9748 && i.tm.opcode_modifier.opcodespace != SPACE_BASE)
9749 {
9750 *p++ = 0x0f;
9751 if (i.tm.opcode_modifier.opcodespace != SPACE_0F)
9752 *p++ = i.tm.opcode_modifier.opcodespace == SPACE_0F38
9753 ? 0x38 : 0x3a;
9754 }
9755
9a182d04 9756 switch (i.opcode_length)
331d2d0d 9757 {
4dffcebc 9758 case 2:
389d00a5
JB
9759 /* Put out high byte first: can't use md_number_to_chars! */
9760 *p++ = (i.tm.base_opcode >> 8) & 0xff;
9761 /* Fall through. */
9762 case 1:
9763 *p = i.tm.base_opcode & 0xff;
4dffcebc
L
9764 break;
9765 default:
9766 abort ();
9767 break;
331d2d0d 9768 }
0f10071e 9769
29b0f896 9770 }
3e73aa7c 9771
29b0f896 9772 /* Now the modrm byte and sib byte (if present). */
40fb9820 9773 if (i.tm.opcode_modifier.modrm)
29b0f896 9774 {
48ef937e
JB
9775 frag_opcode_byte ((i.rm.regmem << 0)
9776 | (i.rm.reg << 3)
9777 | (i.rm.mode << 6));
29b0f896
AM
9778 /* If i.rm.regmem == ESP (4)
9779 && i.rm.mode != (Register mode)
9780 && not 16 bit
9781 ==> need second modrm byte. */
9782 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
9783 && i.rm.mode != 3
dc821c5f 9784 && !(i.base_reg && i.base_reg->reg_type.bitfield.word))
48ef937e
JB
9785 frag_opcode_byte ((i.sib.base << 0)
9786 | (i.sib.index << 3)
9787 | (i.sib.scale << 6));
29b0f896 9788 }
3e73aa7c 9789
29b0f896 9790 if (i.disp_operands)
2bbd9c25 9791 output_disp (insn_start_frag, insn_start_off);
3e73aa7c 9792
29b0f896 9793 if (i.imm_operands)
2bbd9c25 9794 output_imm (insn_start_frag, insn_start_off);
9c33702b
JB
9795
9796 /*
9797 * frag_now_fix () returning plain abs_section_offset when we're in the
9798 * absolute section, and abs_section_offset not getting updated as data
9799 * gets added to the frag breaks the logic below.
9800 */
9801 if (now_seg != absolute_section)
9802 {
9803 j = encoding_length (insn_start_frag, insn_start_off, frag_more (0));
9804 if (j > 15)
9805 as_warn (_("instruction length of %u bytes exceeds the limit of 15"),
9806 j);
e379e5f3
L
9807 else if (fragP)
9808 {
9809 /* NB: Don't add prefix with GOTPC relocation since
9810 output_disp() above depends on the fixed encoding
9811 length. Can't add prefix with TLS relocation since
9812 it breaks TLS linker optimization. */
9813 unsigned int max = i.has_gotpc_tls_reloc ? 0 : 15 - j;
9814 /* Prefix count on the current instruction. */
9815 unsigned int count = i.vex.length;
9816 unsigned int k;
9817 for (k = 0; k < ARRAY_SIZE (i.prefix); k++)
9818 /* REX byte is encoded in VEX/EVEX prefix. */
9819 if (i.prefix[k] && (k != REX_PREFIX || !i.vex.length))
9820 count++;
9821
9822 /* Count prefixes for extended opcode maps. */
9823 if (!i.vex.length)
389d00a5 9824 switch (i.tm.opcode_modifier.opcodespace)
e379e5f3 9825 {
389d00a5 9826 case SPACE_BASE:
e379e5f3 9827 break;
389d00a5
JB
9828 case SPACE_0F:
9829 count++;
e379e5f3 9830 break;
389d00a5
JB
9831 case SPACE_0F38:
9832 case SPACE_0F3A:
9833 count += 2;
e379e5f3
L
9834 break;
9835 default:
9836 abort ();
9837 }
9838
9839 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
9840 == BRANCH_PREFIX)
9841 {
9842 /* Set the maximum prefix size in BRANCH_PREFIX
9843 frag. */
9844 if (fragP->tc_frag_data.max_bytes > max)
9845 fragP->tc_frag_data.max_bytes = max;
9846 if (fragP->tc_frag_data.max_bytes > count)
9847 fragP->tc_frag_data.max_bytes -= count;
9848 else
9849 fragP->tc_frag_data.max_bytes = 0;
9850 }
9851 else
9852 {
9853 /* Remember the maximum prefix size in FUSED_JCC_PADDING
9854 frag. */
9855 unsigned int max_prefix_size;
9856 if (align_branch_prefix_size > max)
9857 max_prefix_size = max;
9858 else
9859 max_prefix_size = align_branch_prefix_size;
9860 if (max_prefix_size > count)
9861 fragP->tc_frag_data.max_prefix_length
9862 = max_prefix_size - count;
9863 }
9864
9865 /* Use existing segment prefix if possible. Use CS
9866 segment prefix in 64-bit mode. In 32-bit mode, use SS
9867 segment prefix with ESP/EBP base register and use DS
9868 segment prefix without ESP/EBP base register. */
9869 if (i.prefix[SEG_PREFIX])
9870 fragP->tc_frag_data.default_prefix = i.prefix[SEG_PREFIX];
9871 else if (flag_code == CODE_64BIT)
9872 fragP->tc_frag_data.default_prefix = CS_PREFIX_OPCODE;
9873 else if (i.base_reg
9874 && (i.base_reg->reg_num == 4
9875 || i.base_reg->reg_num == 5))
9876 fragP->tc_frag_data.default_prefix = SS_PREFIX_OPCODE;
9877 else
9878 fragP->tc_frag_data.default_prefix = DS_PREFIX_OPCODE;
9879 }
9c33702b 9880 }
29b0f896 9881 }
252b5132 9882
e379e5f3
L
9883 /* NB: Don't work with COND_JUMP86 without i386. */
9884 if (align_branch_power
9885 && now_seg != absolute_section
9886 && cpu_arch_flags.bitfield.cpui386)
9887 {
9888 /* Terminate each frag so that we can add prefix and check for
9889 fused jcc. */
9890 frag_wane (frag_now);
9891 frag_new (0);
9892 }
9893
29b0f896
AM
9894#ifdef DEBUG386
9895 if (flag_debug)
9896 {
7b81dfbb 9897 pi ("" /*line*/, &i);
29b0f896
AM
9898 }
9899#endif /* DEBUG386 */
9900}
252b5132 9901
e205caa7
L
9902/* Return the size of the displacement operand N. */
9903
9904static int
9905disp_size (unsigned int n)
9906{
9907 int size = 4;
43234a1e 9908
b5014f7a 9909 if (i.types[n].bitfield.disp64)
40fb9820
L
9910 size = 8;
9911 else if (i.types[n].bitfield.disp8)
9912 size = 1;
9913 else if (i.types[n].bitfield.disp16)
9914 size = 2;
e205caa7
L
9915 return size;
9916}
9917
9918/* Return the size of the immediate operand N. */
9919
9920static int
9921imm_size (unsigned int n)
9922{
9923 int size = 4;
40fb9820
L
9924 if (i.types[n].bitfield.imm64)
9925 size = 8;
9926 else if (i.types[n].bitfield.imm8 || i.types[n].bitfield.imm8s)
9927 size = 1;
9928 else if (i.types[n].bitfield.imm16)
9929 size = 2;
e205caa7
L
9930 return size;
9931}
9932
29b0f896 9933static void
64e74474 9934output_disp (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
9935{
9936 char *p;
9937 unsigned int n;
252b5132 9938
29b0f896
AM
9939 for (n = 0; n < i.operands; n++)
9940 {
b5014f7a 9941 if (operand_type_check (i.types[n], disp))
29b0f896 9942 {
48ef937e
JB
9943 int size = disp_size (n);
9944
9945 if (now_seg == absolute_section)
9946 abs_section_offset += size;
9947 else if (i.op[n].disps->X_op == O_constant)
29b0f896 9948 {
43234a1e 9949 offsetT val = i.op[n].disps->X_add_number;
252b5132 9950
629cfaf1
JB
9951 val = offset_in_range (val >> (size == 1 ? i.memshift : 0),
9952 size);
29b0f896
AM
9953 p = frag_more (size);
9954 md_number_to_chars (p, val, size);
9955 }
9956 else
9957 {
f86103b7 9958 enum bfd_reloc_code_real reloc_type;
40fb9820 9959 int sign = i.types[n].bitfield.disp32s;
29b0f896 9960 int pcrel = (i.flags[n] & Operand_PCrel) != 0;
02a86693 9961 fixS *fixP;
29b0f896 9962
e205caa7 9963 /* We can't have 8 bit displacement here. */
9c2799c2 9964 gas_assert (!i.types[n].bitfield.disp8);
e205caa7 9965
29b0f896
AM
9966 /* The PC relative address is computed relative
9967 to the instruction boundary, so in case immediate
9968 fields follows, we need to adjust the value. */
9969 if (pcrel && i.imm_operands)
9970 {
29b0f896 9971 unsigned int n1;
e205caa7 9972 int sz = 0;
252b5132 9973
29b0f896 9974 for (n1 = 0; n1 < i.operands; n1++)
40fb9820 9975 if (operand_type_check (i.types[n1], imm))
252b5132 9976 {
e205caa7
L
9977 /* Only one immediate is allowed for PC
9978 relative address. */
9c2799c2 9979 gas_assert (sz == 0);
e205caa7
L
9980 sz = imm_size (n1);
9981 i.op[n].disps->X_add_number -= sz;
252b5132 9982 }
29b0f896 9983 /* We should find the immediate. */
9c2799c2 9984 gas_assert (sz != 0);
29b0f896 9985 }
520dc8e8 9986
29b0f896 9987 p = frag_more (size);
d258b828 9988 reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
d6ab8113 9989 if (GOT_symbol
2bbd9c25 9990 && GOT_symbol == i.op[n].disps->X_add_symbol
d6ab8113 9991 && (((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
9992 || reloc_type == BFD_RELOC_X86_64_32S
9993 || (reloc_type == BFD_RELOC_64
9994 && object_64bit))
d6ab8113
JB
9995 && (i.op[n].disps->X_op == O_symbol
9996 || (i.op[n].disps->X_op == O_add
9997 && ((symbol_get_value_expression
9998 (i.op[n].disps->X_op_symbol)->X_op)
9999 == O_subtract))))
10000 || reloc_type == BFD_RELOC_32_PCREL))
2bbd9c25 10001 {
4fa24527 10002 if (!object_64bit)
7b81dfbb
AJ
10003 {
10004 reloc_type = BFD_RELOC_386_GOTPC;
5b7c81bd 10005 i.has_gotpc_tls_reloc = true;
98da05bf 10006 i.op[n].disps->X_add_number +=
d583596c 10007 encoding_length (insn_start_frag, insn_start_off, p);
7b81dfbb
AJ
10008 }
10009 else if (reloc_type == BFD_RELOC_64)
10010 reloc_type = BFD_RELOC_X86_64_GOTPC64;
d6ab8113 10011 else
7b81dfbb
AJ
10012 /* Don't do the adjustment for x86-64, as there
10013 the pcrel addressing is relative to the _next_
10014 insn, and that is taken care of in other code. */
d6ab8113 10015 reloc_type = BFD_RELOC_X86_64_GOTPC32;
2bbd9c25 10016 }
e379e5f3
L
10017 else if (align_branch_power)
10018 {
10019 switch (reloc_type)
10020 {
10021 case BFD_RELOC_386_TLS_GD:
10022 case BFD_RELOC_386_TLS_LDM:
10023 case BFD_RELOC_386_TLS_IE:
10024 case BFD_RELOC_386_TLS_IE_32:
10025 case BFD_RELOC_386_TLS_GOTIE:
10026 case BFD_RELOC_386_TLS_GOTDESC:
10027 case BFD_RELOC_386_TLS_DESC_CALL:
10028 case BFD_RELOC_X86_64_TLSGD:
10029 case BFD_RELOC_X86_64_TLSLD:
10030 case BFD_RELOC_X86_64_GOTTPOFF:
10031 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
10032 case BFD_RELOC_X86_64_TLSDESC_CALL:
5b7c81bd 10033 i.has_gotpc_tls_reloc = true;
e379e5f3
L
10034 default:
10035 break;
10036 }
10037 }
02a86693
L
10038 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal,
10039 size, i.op[n].disps, pcrel,
10040 reloc_type);
eb19308f
JB
10041
10042 if (flag_code == CODE_64BIT && size == 4 && pcrel
10043 && !i.prefix[ADDR_PREFIX])
10044 fixP->fx_signed = 1;
10045
02a86693
L
10046 /* Check for "call/jmp *mem", "mov mem, %reg",
10047 "test %reg, mem" and "binop mem, %reg" where binop
10048 is one of adc, add, and, cmp, or, sbb, sub, xor
e60f4d3b
L
10049 instructions without data prefix. Always generate
10050 R_386_GOT32X for "sym*GOT" operand in 32-bit mode. */
10051 if (i.prefix[DATA_PREFIX] == 0
10052 && (generate_relax_relocations
10053 || (!object_64bit
10054 && i.rm.mode == 0
10055 && i.rm.regmem == 5))
0cb4071e
L
10056 && (i.rm.mode == 2
10057 || (i.rm.mode == 0 && i.rm.regmem == 5))
389d00a5 10058 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
02a86693
L
10059 && ((i.operands == 1
10060 && i.tm.base_opcode == 0xff
10061 && (i.rm.reg == 2 || i.rm.reg == 4))
10062 || (i.operands == 2
10063 && (i.tm.base_opcode == 0x8b
10064 || i.tm.base_opcode == 0x85
2ae4c703 10065 || (i.tm.base_opcode & ~0x38) == 0x03))))
02a86693
L
10066 {
10067 if (object_64bit)
10068 {
10069 fixP->fx_tcbit = i.rex != 0;
10070 if (i.base_reg
e968fc9b 10071 && (i.base_reg->reg_num == RegIP))
02a86693
L
10072 fixP->fx_tcbit2 = 1;
10073 }
10074 else
10075 fixP->fx_tcbit2 = 1;
10076 }
29b0f896
AM
10077 }
10078 }
10079 }
10080}
252b5132 10081
29b0f896 10082static void
64e74474 10083output_imm (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
10084{
10085 char *p;
10086 unsigned int n;
252b5132 10087
29b0f896
AM
10088 for (n = 0; n < i.operands; n++)
10089 {
43234a1e 10090 /* Skip SAE/RC Imm operand in EVEX. They are already handled. */
ca5312a2 10091 if (i.rounding.type != rc_none && n == i.rounding.operand)
43234a1e
L
10092 continue;
10093
40fb9820 10094 if (operand_type_check (i.types[n], imm))
29b0f896 10095 {
48ef937e
JB
10096 int size = imm_size (n);
10097
10098 if (now_seg == absolute_section)
10099 abs_section_offset += size;
10100 else if (i.op[n].imms->X_op == O_constant)
29b0f896 10101 {
29b0f896 10102 offsetT val;
b4cac588 10103
29b0f896
AM
10104 val = offset_in_range (i.op[n].imms->X_add_number,
10105 size);
10106 p = frag_more (size);
10107 md_number_to_chars (p, val, size);
10108 }
10109 else
10110 {
10111 /* Not absolute_section.
10112 Need a 32-bit fixup (don't support 8bit
10113 non-absolute imms). Try to support other
10114 sizes ... */
f86103b7 10115 enum bfd_reloc_code_real reloc_type;
e205caa7 10116 int sign;
29b0f896 10117
40fb9820 10118 if (i.types[n].bitfield.imm32s
a7d61044 10119 && (i.suffix == QWORD_MNEM_SUFFIX
40fb9820 10120 || (!i.suffix && i.tm.opcode_modifier.no_lsuf)))
29b0f896 10121 sign = 1;
e205caa7
L
10122 else
10123 sign = 0;
520dc8e8 10124
29b0f896 10125 p = frag_more (size);
d258b828 10126 reloc_type = reloc (size, 0, sign, i.reloc[n]);
f86103b7 10127
2bbd9c25
JJ
10128 /* This is tough to explain. We end up with this one if we
10129 * have operands that look like
10130 * "_GLOBAL_OFFSET_TABLE_+[.-.L284]". The goal here is to
10131 * obtain the absolute address of the GOT, and it is strongly
10132 * preferable from a performance point of view to avoid using
10133 * a runtime relocation for this. The actual sequence of
10134 * instructions often look something like:
10135 *
10136 * call .L66
10137 * .L66:
10138 * popl %ebx
10139 * addl $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
10140 *
10141 * The call and pop essentially return the absolute address
10142 * of the label .L66 and store it in %ebx. The linker itself
10143 * will ultimately change the first operand of the addl so
10144 * that %ebx points to the GOT, but to keep things simple, the
10145 * .o file must have this operand set so that it generates not
10146 * the absolute address of .L66, but the absolute address of
10147 * itself. This allows the linker itself simply treat a GOTPC
10148 * relocation as asking for a pcrel offset to the GOT to be
10149 * added in, and the addend of the relocation is stored in the
10150 * operand field for the instruction itself.
10151 *
10152 * Our job here is to fix the operand so that it would add
10153 * the correct offset so that %ebx would point to itself. The
10154 * thing that is tricky is that .-.L66 will point to the
10155 * beginning of the instruction, so we need to further modify
10156 * the operand so that it will point to itself. There are
10157 * other cases where you have something like:
10158 *
10159 * .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
10160 *
10161 * and here no correction would be required. Internally in
10162 * the assembler we treat operands of this form as not being
10163 * pcrel since the '.' is explicitly mentioned, and I wonder
10164 * whether it would simplify matters to do it this way. Who
10165 * knows. In earlier versions of the PIC patches, the
10166 * pcrel_adjust field was used to store the correction, but
10167 * since the expression is not pcrel, I felt it would be
10168 * confusing to do it this way. */
10169
d6ab8113 10170 if ((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
10171 || reloc_type == BFD_RELOC_X86_64_32S
10172 || reloc_type == BFD_RELOC_64)
29b0f896
AM
10173 && GOT_symbol
10174 && GOT_symbol == i.op[n].imms->X_add_symbol
10175 && (i.op[n].imms->X_op == O_symbol
10176 || (i.op[n].imms->X_op == O_add
10177 && ((symbol_get_value_expression
10178 (i.op[n].imms->X_op_symbol)->X_op)
10179 == O_subtract))))
10180 {
4fa24527 10181 if (!object_64bit)
d6ab8113 10182 reloc_type = BFD_RELOC_386_GOTPC;
7b81dfbb 10183 else if (size == 4)
d6ab8113 10184 reloc_type = BFD_RELOC_X86_64_GOTPC32;
7b81dfbb
AJ
10185 else if (size == 8)
10186 reloc_type = BFD_RELOC_X86_64_GOTPC64;
5b7c81bd 10187 i.has_gotpc_tls_reloc = true;
d583596c
JB
10188 i.op[n].imms->X_add_number +=
10189 encoding_length (insn_start_frag, insn_start_off, p);
29b0f896 10190 }
29b0f896
AM
10191 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
10192 i.op[n].imms, 0, reloc_type);
10193 }
10194 }
10195 }
252b5132
RH
10196}
10197\f
d182319b
JB
10198/* x86_cons_fix_new is called via the expression parsing code when a
10199 reloc is needed. We use this hook to get the correct .got reloc. */
d182319b
JB
10200static int cons_sign = -1;
10201
10202void
e3bb37b5 10203x86_cons_fix_new (fragS *frag, unsigned int off, unsigned int len,
62ebcb5c 10204 expressionS *exp, bfd_reloc_code_real_type r)
d182319b 10205{
d258b828 10206 r = reloc (len, 0, cons_sign, r);
d182319b
JB
10207
10208#ifdef TE_PE
10209 if (exp->X_op == O_secrel)
10210 {
10211 exp->X_op = O_symbol;
10212 r = BFD_RELOC_32_SECREL;
10213 }
10214#endif
10215
10216 fix_new_exp (frag, off, len, exp, 0, r);
10217}
10218
357d1bd8
L
10219/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
10220 purpose of the `.dc.a' internal pseudo-op. */
10221
10222int
10223x86_address_bytes (void)
10224{
10225 if ((stdoutput->arch_info->mach & bfd_mach_x64_32))
10226 return 4;
10227 return stdoutput->arch_info->bits_per_address / 8;
10228}
10229
deea4973
JB
10230#if (!(defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined (OBJ_MACH_O)) \
10231 || defined (LEX_AT)) && !defined (TE_PE)
d258b828 10232# define lex_got(reloc, adjust, types) NULL
718ddfc0 10233#else
f3c180ae
AM
10234/* Parse operands of the form
10235 <symbol>@GOTOFF+<nnn>
10236 and similar .plt or .got references.
10237
10238 If we find one, set up the correct relocation in RELOC and copy the
10239 input string, minus the `@GOTOFF' into a malloc'd buffer for
10240 parsing by the calling routine. Return this buffer, and if ADJUST
10241 is non-null set it to the length of the string we removed from the
10242 input line. Otherwise return NULL. */
10243static char *
91d6fa6a 10244lex_got (enum bfd_reloc_code_real *rel,
64e74474 10245 int *adjust,
d258b828 10246 i386_operand_type *types)
f3c180ae 10247{
7b81dfbb
AJ
10248 /* Some of the relocations depend on the size of what field is to
10249 be relocated. But in our callers i386_immediate and i386_displacement
10250 we don't yet know the operand size (this will be set by insn
10251 matching). Hence we record the word32 relocation here,
10252 and adjust the reloc according to the real size in reloc(). */
f3c180ae
AM
10253 static const struct {
10254 const char *str;
cff8d58a 10255 int len;
4fa24527 10256 const enum bfd_reloc_code_real rel[2];
40fb9820 10257 const i386_operand_type types64;
5b7c81bd 10258 bool need_GOT_symbol;
f3c180ae 10259 } gotrel[] = {
deea4973 10260#ifndef TE_PE
8ce3d284 10261#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
10262 { STRING_COMMA_LEN ("SIZE"), { BFD_RELOC_SIZE32,
10263 BFD_RELOC_SIZE32 },
5b7c81bd 10264 OPERAND_TYPE_IMM32_64, false },
8ce3d284 10265#endif
cff8d58a
L
10266 { STRING_COMMA_LEN ("PLTOFF"), { _dummy_first_bfd_reloc_code_real,
10267 BFD_RELOC_X86_64_PLTOFF64 },
5b7c81bd 10268 OPERAND_TYPE_IMM64, true },
cff8d58a
L
10269 { STRING_COMMA_LEN ("PLT"), { BFD_RELOC_386_PLT32,
10270 BFD_RELOC_X86_64_PLT32 },
5b7c81bd 10271 OPERAND_TYPE_IMM32_32S_DISP32, false },
cff8d58a
L
10272 { STRING_COMMA_LEN ("GOTPLT"), { _dummy_first_bfd_reloc_code_real,
10273 BFD_RELOC_X86_64_GOTPLT64 },
5b7c81bd 10274 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
10275 { STRING_COMMA_LEN ("GOTOFF"), { BFD_RELOC_386_GOTOFF,
10276 BFD_RELOC_X86_64_GOTOFF64 },
5b7c81bd 10277 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
10278 { STRING_COMMA_LEN ("GOTPCREL"), { _dummy_first_bfd_reloc_code_real,
10279 BFD_RELOC_X86_64_GOTPCREL },
5b7c81bd 10280 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10281 { STRING_COMMA_LEN ("TLSGD"), { BFD_RELOC_386_TLS_GD,
10282 BFD_RELOC_X86_64_TLSGD },
5b7c81bd 10283 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10284 { STRING_COMMA_LEN ("TLSLDM"), { BFD_RELOC_386_TLS_LDM,
10285 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10286 OPERAND_TYPE_NONE, true },
cff8d58a
L
10287 { STRING_COMMA_LEN ("TLSLD"), { _dummy_first_bfd_reloc_code_real,
10288 BFD_RELOC_X86_64_TLSLD },
5b7c81bd 10289 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10290 { STRING_COMMA_LEN ("GOTTPOFF"), { BFD_RELOC_386_TLS_IE_32,
10291 BFD_RELOC_X86_64_GOTTPOFF },
5b7c81bd 10292 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10293 { STRING_COMMA_LEN ("TPOFF"), { BFD_RELOC_386_TLS_LE_32,
10294 BFD_RELOC_X86_64_TPOFF32 },
5b7c81bd 10295 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
10296 { STRING_COMMA_LEN ("NTPOFF"), { BFD_RELOC_386_TLS_LE,
10297 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10298 OPERAND_TYPE_NONE, true },
cff8d58a
L
10299 { STRING_COMMA_LEN ("DTPOFF"), { BFD_RELOC_386_TLS_LDO_32,
10300 BFD_RELOC_X86_64_DTPOFF32 },
5b7c81bd 10301 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
10302 { STRING_COMMA_LEN ("GOTNTPOFF"),{ BFD_RELOC_386_TLS_GOTIE,
10303 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10304 OPERAND_TYPE_NONE, true },
cff8d58a
L
10305 { STRING_COMMA_LEN ("INDNTPOFF"),{ BFD_RELOC_386_TLS_IE,
10306 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10307 OPERAND_TYPE_NONE, true },
cff8d58a
L
10308 { STRING_COMMA_LEN ("GOT"), { BFD_RELOC_386_GOT32,
10309 BFD_RELOC_X86_64_GOT32 },
5b7c81bd 10310 OPERAND_TYPE_IMM32_32S_64_DISP32, true },
cff8d58a
L
10311 { STRING_COMMA_LEN ("TLSDESC"), { BFD_RELOC_386_TLS_GOTDESC,
10312 BFD_RELOC_X86_64_GOTPC32_TLSDESC },
5b7c81bd 10313 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10314 { STRING_COMMA_LEN ("TLSCALL"), { BFD_RELOC_386_TLS_DESC_CALL,
10315 BFD_RELOC_X86_64_TLSDESC_CALL },
5b7c81bd 10316 OPERAND_TYPE_IMM32_32S_DISP32, true },
deea4973
JB
10317#else /* TE_PE */
10318 { STRING_COMMA_LEN ("SECREL32"), { BFD_RELOC_32_SECREL,
10319 BFD_RELOC_32_SECREL },
10320 OPERAND_TYPE_IMM32_32S_64_DISP32_64, false },
10321#endif
f3c180ae
AM
10322 };
10323 char *cp;
10324 unsigned int j;
10325
deea4973 10326#if defined (OBJ_MAYBE_ELF) && !defined (TE_PE)
718ddfc0
JB
10327 if (!IS_ELF)
10328 return NULL;
d382c579 10329#endif
718ddfc0 10330
f3c180ae 10331 for (cp = input_line_pointer; *cp != '@'; cp++)
67c11a9b 10332 if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
f3c180ae
AM
10333 return NULL;
10334
47465058 10335 for (j = 0; j < ARRAY_SIZE (gotrel); j++)
f3c180ae 10336 {
cff8d58a 10337 int len = gotrel[j].len;
28f81592 10338 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
f3c180ae 10339 {
4fa24527 10340 if (gotrel[j].rel[object_64bit] != 0)
f3c180ae 10341 {
28f81592
AM
10342 int first, second;
10343 char *tmpbuf, *past_reloc;
f3c180ae 10344
91d6fa6a 10345 *rel = gotrel[j].rel[object_64bit];
f3c180ae 10346
3956db08
JB
10347 if (types)
10348 {
10349 if (flag_code != CODE_64BIT)
40fb9820
L
10350 {
10351 types->bitfield.imm32 = 1;
10352 types->bitfield.disp32 = 1;
10353 }
3956db08
JB
10354 else
10355 *types = gotrel[j].types64;
10356 }
10357
844bf810 10358 if (gotrel[j].need_GOT_symbol && GOT_symbol == NULL)
f3c180ae
AM
10359 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
10360
28f81592 10361 /* The length of the first part of our input line. */
f3c180ae 10362 first = cp - input_line_pointer;
28f81592
AM
10363
10364 /* The second part goes from after the reloc token until
67c11a9b 10365 (and including) an end_of_line char or comma. */
28f81592 10366 past_reloc = cp + 1 + len;
67c11a9b
AM
10367 cp = past_reloc;
10368 while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
10369 ++cp;
10370 second = cp + 1 - past_reloc;
28f81592
AM
10371
10372 /* Allocate and copy string. The trailing NUL shouldn't
10373 be necessary, but be safe. */
add39d23 10374 tmpbuf = XNEWVEC (char, first + second + 2);
f3c180ae 10375 memcpy (tmpbuf, input_line_pointer, first);
0787a12d
AM
10376 if (second != 0 && *past_reloc != ' ')
10377 /* Replace the relocation token with ' ', so that
10378 errors like foo@GOTOFF1 will be detected. */
10379 tmpbuf[first++] = ' ';
af89796a
L
10380 else
10381 /* Increment length by 1 if the relocation token is
10382 removed. */
10383 len++;
10384 if (adjust)
10385 *adjust = len;
0787a12d
AM
10386 memcpy (tmpbuf + first, past_reloc, second);
10387 tmpbuf[first + second] = '\0';
f3c180ae
AM
10388 return tmpbuf;
10389 }
10390
4fa24527
JB
10391 as_bad (_("@%s reloc is not supported with %d-bit output format"),
10392 gotrel[j].str, 1 << (5 + object_64bit));
f3c180ae
AM
10393 return NULL;
10394 }
10395 }
10396
10397 /* Might be a symbol version string. Don't as_bad here. */
10398 return NULL;
10399}
4e4f7c87 10400#endif
f3c180ae 10401
62ebcb5c 10402bfd_reloc_code_real_type
e3bb37b5 10403x86_cons (expressionS *exp, int size)
f3c180ae 10404{
62ebcb5c
AM
10405 bfd_reloc_code_real_type got_reloc = NO_RELOC;
10406
ee86248c
JB
10407 intel_syntax = -intel_syntax;
10408
3c7b9c2c 10409 exp->X_md = 0;
4fa24527 10410 if (size == 4 || (object_64bit && size == 8))
f3c180ae
AM
10411 {
10412 /* Handle @GOTOFF and the like in an expression. */
10413 char *save;
10414 char *gotfree_input_line;
4a57f2cf 10415 int adjust = 0;
f3c180ae
AM
10416
10417 save = input_line_pointer;
d258b828 10418 gotfree_input_line = lex_got (&got_reloc, &adjust, NULL);
f3c180ae
AM
10419 if (gotfree_input_line)
10420 input_line_pointer = gotfree_input_line;
10421
10422 expression (exp);
10423
10424 if (gotfree_input_line)
10425 {
10426 /* expression () has merrily parsed up to the end of line,
10427 or a comma - in the wrong buffer. Transfer how far
10428 input_line_pointer has moved to the right buffer. */
10429 input_line_pointer = (save
10430 + (input_line_pointer - gotfree_input_line)
10431 + adjust);
10432 free (gotfree_input_line);
3992d3b7
AM
10433 if (exp->X_op == O_constant
10434 || exp->X_op == O_absent
10435 || exp->X_op == O_illegal
0398aac5 10436 || exp->X_op == O_register
3992d3b7
AM
10437 || exp->X_op == O_big)
10438 {
10439 char c = *input_line_pointer;
10440 *input_line_pointer = 0;
10441 as_bad (_("missing or invalid expression `%s'"), save);
10442 *input_line_pointer = c;
10443 }
b9519cfe
L
10444 else if ((got_reloc == BFD_RELOC_386_PLT32
10445 || got_reloc == BFD_RELOC_X86_64_PLT32)
10446 && exp->X_op != O_symbol)
10447 {
10448 char c = *input_line_pointer;
10449 *input_line_pointer = 0;
10450 as_bad (_("invalid PLT expression `%s'"), save);
10451 *input_line_pointer = c;
10452 }
f3c180ae
AM
10453 }
10454 }
10455 else
10456 expression (exp);
ee86248c
JB
10457
10458 intel_syntax = -intel_syntax;
10459
10460 if (intel_syntax)
10461 i386_intel_simplify (exp);
62ebcb5c 10462
a442cac5
JB
10463 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
10464 if (size == 4 && exp->X_op == O_constant && !object_64bit)
10465 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
10466
62ebcb5c 10467 return got_reloc;
f3c180ae 10468}
f3c180ae 10469
9f32dd5b
L
10470static void
10471signed_cons (int size)
6482c264 10472{
a442cac5 10473 if (object_64bit)
d182319b
JB
10474 cons_sign = 1;
10475 cons (size);
10476 cons_sign = -1;
6482c264
NC
10477}
10478
d182319b 10479#ifdef TE_PE
6482c264 10480static void
7016a5d5 10481pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
6482c264
NC
10482{
10483 expressionS exp;
10484
10485 do
10486 {
10487 expression (&exp);
10488 if (exp.X_op == O_symbol)
10489 exp.X_op = O_secrel;
10490
10491 emit_expr (&exp, 4);
10492 }
10493 while (*input_line_pointer++ == ',');
10494
10495 input_line_pointer--;
10496 demand_empty_rest_of_line ();
10497}
6482c264
NC
10498#endif
10499
43234a1e
L
10500/* Handle Vector operations. */
10501
10502static char *
f70c6814 10503check_VecOperations (char *op_string)
43234a1e
L
10504{
10505 const reg_entry *mask;
10506 const char *saved;
10507 char *end_op;
10508
f70c6814 10509 while (*op_string)
43234a1e
L
10510 {
10511 saved = op_string;
10512 if (*op_string == '{')
10513 {
10514 op_string++;
10515
10516 /* Check broadcasts. */
d34049e8 10517 if (startswith (op_string, "1to"))
43234a1e 10518 {
5273a3cd 10519 unsigned int bcst_type;
43234a1e 10520
5273a3cd 10521 if (i.broadcast.type)
43234a1e
L
10522 goto duplicated_vec_op;
10523
10524 op_string += 3;
10525 if (*op_string == '8')
8e6e0792 10526 bcst_type = 8;
b28d1bda 10527 else if (*op_string == '4')
8e6e0792 10528 bcst_type = 4;
b28d1bda 10529 else if (*op_string == '2')
8e6e0792 10530 bcst_type = 2;
43234a1e
L
10531 else if (*op_string == '1'
10532 && *(op_string+1) == '6')
10533 {
8e6e0792 10534 bcst_type = 16;
43234a1e
L
10535 op_string++;
10536 }
10537 else
10538 {
10539 as_bad (_("Unsupported broadcast: `%s'"), saved);
10540 return NULL;
10541 }
10542 op_string++;
10543
5273a3cd
JB
10544 i.broadcast.type = bcst_type;
10545 i.broadcast.operand = this_operand;
43234a1e
L
10546 }
10547 /* Check masking operation. */
10548 else if ((mask = parse_register (op_string, &end_op)) != NULL)
10549 {
8a6fb3f9
JB
10550 if (mask == &bad_reg)
10551 return NULL;
10552
43234a1e 10553 /* k0 can't be used for write mask. */
f74a6307 10554 if (mask->reg_type.bitfield.class != RegMask || !mask->reg_num)
43234a1e 10555 {
6d2cd6b2
JB
10556 as_bad (_("`%s%s' can't be used for write mask"),
10557 register_prefix, mask->reg_name);
43234a1e
L
10558 return NULL;
10559 }
10560
6225c532 10561 if (!i.mask.reg)
43234a1e 10562 {
6225c532
JB
10563 i.mask.reg = mask;
10564 i.mask.operand = this_operand;
43234a1e 10565 }
6225c532
JB
10566 else if (i.mask.reg->reg_num)
10567 goto duplicated_vec_op;
43234a1e
L
10568 else
10569 {
6225c532 10570 i.mask.reg = mask;
43234a1e
L
10571
10572 /* Only "{z}" is allowed here. No need to check
10573 zeroing mask explicitly. */
6225c532 10574 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
10575 {
10576 as_bad (_("invalid write mask `%s'"), saved);
10577 return NULL;
10578 }
10579 }
10580
10581 op_string = end_op;
10582 }
10583 /* Check zeroing-flag for masking operation. */
10584 else if (*op_string == 'z')
10585 {
6225c532 10586 if (!i.mask.reg)
43234a1e 10587 {
6225c532
JB
10588 i.mask.reg = reg_k0;
10589 i.mask.zeroing = 1;
10590 i.mask.operand = this_operand;
43234a1e
L
10591 }
10592 else
10593 {
6225c532 10594 if (i.mask.zeroing)
43234a1e
L
10595 {
10596 duplicated_vec_op:
10597 as_bad (_("duplicated `%s'"), saved);
10598 return NULL;
10599 }
10600
6225c532 10601 i.mask.zeroing = 1;
43234a1e
L
10602
10603 /* Only "{%k}" is allowed here. No need to check mask
10604 register explicitly. */
6225c532 10605 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
10606 {
10607 as_bad (_("invalid zeroing-masking `%s'"),
10608 saved);
10609 return NULL;
10610 }
10611 }
10612
10613 op_string++;
10614 }
10615 else
10616 goto unknown_vec_op;
10617
10618 if (*op_string != '}')
10619 {
10620 as_bad (_("missing `}' in `%s'"), saved);
10621 return NULL;
10622 }
10623 op_string++;
0ba3a731
L
10624
10625 /* Strip whitespace since the addition of pseudo prefixes
10626 changed how the scrubber treats '{'. */
10627 if (is_space_char (*op_string))
10628 ++op_string;
10629
43234a1e
L
10630 continue;
10631 }
10632 unknown_vec_op:
10633 /* We don't know this one. */
10634 as_bad (_("unknown vector operation: `%s'"), saved);
10635 return NULL;
10636 }
10637
6225c532 10638 if (i.mask.reg && i.mask.zeroing && !i.mask.reg->reg_num)
6d2cd6b2
JB
10639 {
10640 as_bad (_("zeroing-masking only allowed with write mask"));
10641 return NULL;
10642 }
10643
43234a1e
L
10644 return op_string;
10645}
10646
252b5132 10647static int
70e41ade 10648i386_immediate (char *imm_start)
252b5132
RH
10649{
10650 char *save_input_line_pointer;
f3c180ae 10651 char *gotfree_input_line;
252b5132 10652 segT exp_seg = 0;
47926f60 10653 expressionS *exp;
40fb9820
L
10654 i386_operand_type types;
10655
0dfbf9d7 10656 operand_type_set (&types, ~0);
252b5132
RH
10657
10658 if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
10659 {
31b2323c
L
10660 as_bad (_("at most %d immediate operands are allowed"),
10661 MAX_IMMEDIATE_OPERANDS);
252b5132
RH
10662 return 0;
10663 }
10664
10665 exp = &im_expressions[i.imm_operands++];
520dc8e8 10666 i.op[this_operand].imms = exp;
252b5132
RH
10667
10668 if (is_space_char (*imm_start))
10669 ++imm_start;
10670
10671 save_input_line_pointer = input_line_pointer;
10672 input_line_pointer = imm_start;
10673
d258b828 10674 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
10675 if (gotfree_input_line)
10676 input_line_pointer = gotfree_input_line;
252b5132
RH
10677
10678 exp_seg = expression (exp);
10679
83183c0c 10680 SKIP_WHITESPACE ();
252b5132 10681 if (*input_line_pointer)
f3c180ae 10682 as_bad (_("junk `%s' after expression"), input_line_pointer);
252b5132
RH
10683
10684 input_line_pointer = save_input_line_pointer;
f3c180ae 10685 if (gotfree_input_line)
ee86248c
JB
10686 {
10687 free (gotfree_input_line);
10688
9aac24b1 10689 if (exp->X_op == O_constant)
ee86248c
JB
10690 exp->X_op = O_illegal;
10691 }
10692
9aac24b1
JB
10693 if (exp_seg == reg_section)
10694 {
10695 as_bad (_("illegal immediate register operand %s"), imm_start);
10696 return 0;
10697 }
10698
ee86248c
JB
10699 return i386_finalize_immediate (exp_seg, exp, types, imm_start);
10700}
252b5132 10701
ee86248c
JB
10702static int
10703i386_finalize_immediate (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
10704 i386_operand_type types, const char *imm_start)
10705{
10706 if (exp->X_op == O_absent || exp->X_op == O_illegal || exp->X_op == O_big)
252b5132 10707 {
313c53d1
L
10708 if (imm_start)
10709 as_bad (_("missing or invalid immediate expression `%s'"),
10710 imm_start);
3992d3b7 10711 return 0;
252b5132 10712 }
3e73aa7c 10713 else if (exp->X_op == O_constant)
252b5132 10714 {
47926f60 10715 /* Size it properly later. */
40fb9820 10716 i.types[this_operand].bitfield.imm64 = 1;
a442cac5
JB
10717
10718 /* If not 64bit, sign/zero extend val, to account for wraparound
10719 when !BFD64. */
10720 if (flag_code != CODE_64BIT)
10721 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
252b5132 10722 }
4c63da97 10723#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
f86103b7 10724 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
31312f95 10725 && exp_seg != absolute_section
47926f60 10726 && exp_seg != text_section
24eab124
AM
10727 && exp_seg != data_section
10728 && exp_seg != bss_section
10729 && exp_seg != undefined_section
f86103b7 10730 && !bfd_is_com_section (exp_seg))
252b5132 10731 {
d0b47220 10732 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
252b5132
RH
10733 return 0;
10734 }
10735#endif
10736 else
10737 {
10738 /* This is an address. The size of the address will be
24eab124 10739 determined later, depending on destination register,
3e73aa7c 10740 suffix, or the default for the section. */
40fb9820
L
10741 i.types[this_operand].bitfield.imm8 = 1;
10742 i.types[this_operand].bitfield.imm16 = 1;
10743 i.types[this_operand].bitfield.imm32 = 1;
10744 i.types[this_operand].bitfield.imm32s = 1;
10745 i.types[this_operand].bitfield.imm64 = 1;
c6fb90c8
L
10746 i.types[this_operand] = operand_type_and (i.types[this_operand],
10747 types);
252b5132
RH
10748 }
10749
10750 return 1;
10751}
10752
551c1ca1 10753static char *
e3bb37b5 10754i386_scale (char *scale)
252b5132 10755{
551c1ca1
AM
10756 offsetT val;
10757 char *save = input_line_pointer;
252b5132 10758
551c1ca1
AM
10759 input_line_pointer = scale;
10760 val = get_absolute_expression ();
10761
10762 switch (val)
252b5132 10763 {
551c1ca1 10764 case 1:
252b5132
RH
10765 i.log2_scale_factor = 0;
10766 break;
551c1ca1 10767 case 2:
252b5132
RH
10768 i.log2_scale_factor = 1;
10769 break;
551c1ca1 10770 case 4:
252b5132
RH
10771 i.log2_scale_factor = 2;
10772 break;
551c1ca1 10773 case 8:
252b5132
RH
10774 i.log2_scale_factor = 3;
10775 break;
10776 default:
a724f0f4
JB
10777 {
10778 char sep = *input_line_pointer;
10779
10780 *input_line_pointer = '\0';
10781 as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
10782 scale);
10783 *input_line_pointer = sep;
10784 input_line_pointer = save;
10785 return NULL;
10786 }
252b5132 10787 }
29b0f896 10788 if (i.log2_scale_factor != 0 && i.index_reg == 0)
252b5132
RH
10789 {
10790 as_warn (_("scale factor of %d without an index register"),
24eab124 10791 1 << i.log2_scale_factor);
252b5132 10792 i.log2_scale_factor = 0;
252b5132 10793 }
551c1ca1
AM
10794 scale = input_line_pointer;
10795 input_line_pointer = save;
10796 return scale;
252b5132
RH
10797}
10798
252b5132 10799static int
e3bb37b5 10800i386_displacement (char *disp_start, char *disp_end)
252b5132 10801{
29b0f896 10802 expressionS *exp;
252b5132
RH
10803 segT exp_seg = 0;
10804 char *save_input_line_pointer;
f3c180ae 10805 char *gotfree_input_line;
40fb9820
L
10806 int override;
10807 i386_operand_type bigdisp, types = anydisp;
3992d3b7 10808 int ret;
252b5132 10809
31b2323c
L
10810 if (i.disp_operands == MAX_MEMORY_OPERANDS)
10811 {
10812 as_bad (_("at most %d displacement operands are allowed"),
10813 MAX_MEMORY_OPERANDS);
10814 return 0;
10815 }
10816
0dfbf9d7 10817 operand_type_set (&bigdisp, 0);
6f2f06be 10818 if (i.jumpabsolute
48bcea9f 10819 || i.types[this_operand].bitfield.baseindex
0cfa3eb3
JB
10820 || (current_templates->start->opcode_modifier.jump != JUMP
10821 && current_templates->start->opcode_modifier.jump != JUMP_DWORD))
e05278af 10822 {
48bcea9f 10823 i386_addressing_mode ();
e05278af 10824 override = (i.prefix[ADDR_PREFIX] != 0);
40fb9820
L
10825 if (flag_code == CODE_64BIT)
10826 {
10827 if (!override)
10828 {
10829 bigdisp.bitfield.disp32s = 1;
10830 bigdisp.bitfield.disp64 = 1;
10831 }
48bcea9f
JB
10832 else
10833 bigdisp.bitfield.disp32 = 1;
40fb9820
L
10834 }
10835 else if ((flag_code == CODE_16BIT) ^ override)
40fb9820 10836 bigdisp.bitfield.disp16 = 1;
48bcea9f
JB
10837 else
10838 bigdisp.bitfield.disp32 = 1;
e05278af
JB
10839 }
10840 else
10841 {
376cd056
JB
10842 /* For PC-relative branches, the width of the displacement may be
10843 dependent upon data size, but is never dependent upon address size.
10844 Also make sure to not unintentionally match against a non-PC-relative
10845 branch template. */
10846 static templates aux_templates;
10847 const insn_template *t = current_templates->start;
5b7c81bd 10848 bool has_intel64 = false;
376cd056
JB
10849
10850 aux_templates.start = t;
10851 while (++t < current_templates->end)
10852 {
10853 if (t->opcode_modifier.jump
10854 != current_templates->start->opcode_modifier.jump)
10855 break;
4b5aaf5f 10856 if ((t->opcode_modifier.isa64 >= INTEL64))
5b7c81bd 10857 has_intel64 = true;
376cd056
JB
10858 }
10859 if (t < current_templates->end)
10860 {
10861 aux_templates.end = t;
10862 current_templates = &aux_templates;
10863 }
10864
e05278af 10865 override = (i.prefix[DATA_PREFIX] != 0);
40fb9820
L
10866 if (flag_code == CODE_64BIT)
10867 {
376cd056
JB
10868 if ((override || i.suffix == WORD_MNEM_SUFFIX)
10869 && (!intel64 || !has_intel64))
40fb9820
L
10870 bigdisp.bitfield.disp16 = 1;
10871 else
48bcea9f 10872 bigdisp.bitfield.disp32s = 1;
40fb9820
L
10873 }
10874 else
e05278af
JB
10875 {
10876 if (!override)
10877 override = (i.suffix == (flag_code != CODE_16BIT
10878 ? WORD_MNEM_SUFFIX
10879 : LONG_MNEM_SUFFIX));
40fb9820
L
10880 bigdisp.bitfield.disp32 = 1;
10881 if ((flag_code == CODE_16BIT) ^ override)
10882 {
10883 bigdisp.bitfield.disp32 = 0;
10884 bigdisp.bitfield.disp16 = 1;
10885 }
e05278af 10886 }
e05278af 10887 }
c6fb90c8
L
10888 i.types[this_operand] = operand_type_or (i.types[this_operand],
10889 bigdisp);
252b5132
RH
10890
10891 exp = &disp_expressions[i.disp_operands];
520dc8e8 10892 i.op[this_operand].disps = exp;
252b5132
RH
10893 i.disp_operands++;
10894 save_input_line_pointer = input_line_pointer;
10895 input_line_pointer = disp_start;
10896 END_STRING_AND_SAVE (disp_end);
10897
10898#ifndef GCC_ASM_O_HACK
10899#define GCC_ASM_O_HACK 0
10900#endif
10901#if GCC_ASM_O_HACK
10902 END_STRING_AND_SAVE (disp_end + 1);
40fb9820 10903 if (i.types[this_operand].bitfield.baseIndex
24eab124 10904 && displacement_string_end[-1] == '+')
252b5132
RH
10905 {
10906 /* This hack is to avoid a warning when using the "o"
24eab124
AM
10907 constraint within gcc asm statements.
10908 For instance:
10909
10910 #define _set_tssldt_desc(n,addr,limit,type) \
10911 __asm__ __volatile__ ( \
10912 "movw %w2,%0\n\t" \
10913 "movw %w1,2+%0\n\t" \
10914 "rorl $16,%1\n\t" \
10915 "movb %b1,4+%0\n\t" \
10916 "movb %4,5+%0\n\t" \
10917 "movb $0,6+%0\n\t" \
10918 "movb %h1,7+%0\n\t" \
10919 "rorl $16,%1" \
10920 : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
10921
10922 This works great except that the output assembler ends
10923 up looking a bit weird if it turns out that there is
10924 no offset. You end up producing code that looks like:
10925
10926 #APP
10927 movw $235,(%eax)
10928 movw %dx,2+(%eax)
10929 rorl $16,%edx
10930 movb %dl,4+(%eax)
10931 movb $137,5+(%eax)
10932 movb $0,6+(%eax)
10933 movb %dh,7+(%eax)
10934 rorl $16,%edx
10935 #NO_APP
10936
47926f60 10937 So here we provide the missing zero. */
24eab124
AM
10938
10939 *displacement_string_end = '0';
252b5132
RH
10940 }
10941#endif
d258b828 10942 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
10943 if (gotfree_input_line)
10944 input_line_pointer = gotfree_input_line;
252b5132 10945
24eab124 10946 exp_seg = expression (exp);
252b5132 10947
636c26b0
AM
10948 SKIP_WHITESPACE ();
10949 if (*input_line_pointer)
10950 as_bad (_("junk `%s' after expression"), input_line_pointer);
10951#if GCC_ASM_O_HACK
10952 RESTORE_END_STRING (disp_end + 1);
10953#endif
636c26b0 10954 input_line_pointer = save_input_line_pointer;
636c26b0 10955 if (gotfree_input_line)
ee86248c
JB
10956 {
10957 free (gotfree_input_line);
10958
10959 if (exp->X_op == O_constant || exp->X_op == O_register)
10960 exp->X_op = O_illegal;
10961 }
10962
10963 ret = i386_finalize_displacement (exp_seg, exp, types, disp_start);
10964
10965 RESTORE_END_STRING (disp_end);
10966
10967 return ret;
10968}
10969
10970static int
10971i386_finalize_displacement (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
10972 i386_operand_type types, const char *disp_start)
10973{
10974 i386_operand_type bigdisp;
10975 int ret = 1;
636c26b0 10976
24eab124
AM
10977 /* We do this to make sure that the section symbol is in
10978 the symbol table. We will ultimately change the relocation
47926f60 10979 to be relative to the beginning of the section. */
1ae12ab7 10980 if (i.reloc[this_operand] == BFD_RELOC_386_GOTOFF
d6ab8113
JB
10981 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
10982 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
24eab124 10983 {
636c26b0 10984 if (exp->X_op != O_symbol)
3992d3b7 10985 goto inv_disp;
636c26b0 10986
e5cb08ac 10987 if (S_IS_LOCAL (exp->X_add_symbol)
c64efb4b
L
10988 && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section
10989 && S_GET_SEGMENT (exp->X_add_symbol) != expr_section)
24eab124 10990 section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
24eab124
AM
10991 exp->X_op = O_subtract;
10992 exp->X_op_symbol = GOT_symbol;
1ae12ab7 10993 if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
29b0f896 10994 i.reloc[this_operand] = BFD_RELOC_32_PCREL;
d6ab8113
JB
10995 else if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
10996 i.reloc[this_operand] = BFD_RELOC_64;
23df1078 10997 else
29b0f896 10998 i.reloc[this_operand] = BFD_RELOC_32;
24eab124 10999 }
252b5132 11000
3992d3b7
AM
11001 else if (exp->X_op == O_absent
11002 || exp->X_op == O_illegal
ee86248c 11003 || exp->X_op == O_big)
2daf4fd8 11004 {
3992d3b7
AM
11005 inv_disp:
11006 as_bad (_("missing or invalid displacement expression `%s'"),
2daf4fd8 11007 disp_start);
3992d3b7 11008 ret = 0;
2daf4fd8
AM
11009 }
11010
4c63da97 11011#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
3992d3b7
AM
11012 else if (exp->X_op != O_constant
11013 && OUTPUT_FLAVOR == bfd_target_aout_flavour
11014 && exp_seg != absolute_section
11015 && exp_seg != text_section
11016 && exp_seg != data_section
11017 && exp_seg != bss_section
11018 && exp_seg != undefined_section
11019 && !bfd_is_com_section (exp_seg))
24eab124 11020 {
d0b47220 11021 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
3992d3b7 11022 ret = 0;
24eab124 11023 }
252b5132 11024#endif
3956db08 11025
48bcea9f
JB
11026 if (current_templates->start->opcode_modifier.jump == JUMP_BYTE
11027 /* Constants get taken care of by optimize_disp(). */
11028 && exp->X_op != O_constant)
11029 i.types[this_operand].bitfield.disp8 = 1;
11030
40fb9820
L
11031 /* Check if this is a displacement only operand. */
11032 bigdisp = i.types[this_operand];
11033 bigdisp.bitfield.disp8 = 0;
11034 bigdisp.bitfield.disp16 = 0;
11035 bigdisp.bitfield.disp32 = 0;
11036 bigdisp.bitfield.disp32s = 0;
11037 bigdisp.bitfield.disp64 = 0;
0dfbf9d7 11038 if (operand_type_all_zero (&bigdisp))
c6fb90c8
L
11039 i.types[this_operand] = operand_type_and (i.types[this_operand],
11040 types);
3956db08 11041
3992d3b7 11042 return ret;
252b5132
RH
11043}
11044
2abc2bec
JB
11045/* Return the active addressing mode, taking address override and
11046 registers forming the address into consideration. Update the
11047 address override prefix if necessary. */
47926f60 11048
2abc2bec
JB
11049static enum flag_code
11050i386_addressing_mode (void)
252b5132 11051{
be05d201
L
11052 enum flag_code addr_mode;
11053
11054 if (i.prefix[ADDR_PREFIX])
11055 addr_mode = flag_code == CODE_32BIT ? CODE_16BIT : CODE_32BIT;
a23b33b3
JB
11056 else if (flag_code == CODE_16BIT
11057 && current_templates->start->cpu_flags.bitfield.cpumpx
11058 /* Avoid replacing the "16-bit addressing not allowed" diagnostic
11059 from md_assemble() by "is not a valid base/index expression"
11060 when there is a base and/or index. */
11061 && !i.types[this_operand].bitfield.baseindex)
11062 {
11063 /* MPX insn memory operands with neither base nor index must be forced
11064 to use 32-bit addressing in 16-bit mode. */
11065 addr_mode = CODE_32BIT;
11066 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
11067 ++i.prefixes;
11068 gas_assert (!i.types[this_operand].bitfield.disp16);
11069 gas_assert (!i.types[this_operand].bitfield.disp32);
11070 }
be05d201
L
11071 else
11072 {
11073 addr_mode = flag_code;
11074
24eab124 11075#if INFER_ADDR_PREFIX
be05d201
L
11076 if (i.mem_operands == 0)
11077 {
11078 /* Infer address prefix from the first memory operand. */
11079 const reg_entry *addr_reg = i.base_reg;
11080
11081 if (addr_reg == NULL)
11082 addr_reg = i.index_reg;
eecb386c 11083
be05d201
L
11084 if (addr_reg)
11085 {
e968fc9b 11086 if (addr_reg->reg_type.bitfield.dword)
be05d201
L
11087 addr_mode = CODE_32BIT;
11088 else if (flag_code != CODE_64BIT
dc821c5f 11089 && addr_reg->reg_type.bitfield.word)
be05d201
L
11090 addr_mode = CODE_16BIT;
11091
11092 if (addr_mode != flag_code)
11093 {
11094 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
11095 i.prefixes += 1;
11096 /* Change the size of any displacement too. At most one
11097 of Disp16 or Disp32 is set.
11098 FIXME. There doesn't seem to be any real need for
11099 separate Disp16 and Disp32 flags. The same goes for
11100 Imm16 and Imm32. Removing them would probably clean
11101 up the code quite a lot. */
11102 if (flag_code != CODE_64BIT
11103 && (i.types[this_operand].bitfield.disp16
11104 || i.types[this_operand].bitfield.disp32))
11105 i.types[this_operand]
11106 = operand_type_xor (i.types[this_operand], disp16_32);
11107 }
11108 }
11109 }
24eab124 11110#endif
be05d201
L
11111 }
11112
2abc2bec
JB
11113 return addr_mode;
11114}
11115
11116/* Make sure the memory operand we've been dealt is valid.
11117 Return 1 on success, 0 on a failure. */
11118
11119static int
11120i386_index_check (const char *operand_string)
11121{
11122 const char *kind = "base/index";
11123 enum flag_code addr_mode = i386_addressing_mode ();
a152332d 11124 const insn_template *t = current_templates->start;
2abc2bec 11125
a152332d
JB
11126 if (t->opcode_modifier.isstring
11127 && !t->cpu_flags.bitfield.cpupadlock
fc0763e6
JB
11128 && (current_templates->end[-1].opcode_modifier.isstring
11129 || i.mem_operands))
11130 {
11131 /* Memory operands of string insns are special in that they only allow
11132 a single register (rDI, rSI, or rBX) as their memory address. */
be05d201
L
11133 const reg_entry *expected_reg;
11134 static const char *di_si[][2] =
11135 {
11136 { "esi", "edi" },
11137 { "si", "di" },
11138 { "rsi", "rdi" }
11139 };
11140 static const char *bx[] = { "ebx", "bx", "rbx" };
fc0763e6
JB
11141
11142 kind = "string address";
11143
a152332d 11144 if (t->opcode_modifier.prefixok == PrefixRep)
fc0763e6 11145 {
51c8edf6
JB
11146 int es_op = current_templates->end[-1].opcode_modifier.isstring
11147 - IS_STRING_ES_OP0;
11148 int op = 0;
fc0763e6 11149
51c8edf6 11150 if (!current_templates->end[-1].operand_types[0].bitfield.baseindex
fc0763e6
JB
11151 || ((!i.mem_operands != !intel_syntax)
11152 && current_templates->end[-1].operand_types[1]
11153 .bitfield.baseindex))
51c8edf6 11154 op = 1;
fe0e921f
AM
11155 expected_reg
11156 = (const reg_entry *) str_hash_find (reg_hash,
11157 di_si[addr_mode][op == es_op]);
fc0763e6
JB
11158 }
11159 else
fe0e921f
AM
11160 expected_reg
11161 = (const reg_entry *)str_hash_find (reg_hash, bx[addr_mode]);
fc0763e6 11162
be05d201
L
11163 if (i.base_reg != expected_reg
11164 || i.index_reg
fc0763e6 11165 || operand_type_check (i.types[this_operand], disp))
fc0763e6 11166 {
be05d201
L
11167 /* The second memory operand must have the same size as
11168 the first one. */
11169 if (i.mem_operands
11170 && i.base_reg
11171 && !((addr_mode == CODE_64BIT
dc821c5f 11172 && i.base_reg->reg_type.bitfield.qword)
be05d201 11173 || (addr_mode == CODE_32BIT
dc821c5f
JB
11174 ? i.base_reg->reg_type.bitfield.dword
11175 : i.base_reg->reg_type.bitfield.word)))
be05d201
L
11176 goto bad_address;
11177
fc0763e6
JB
11178 as_warn (_("`%s' is not valid here (expected `%c%s%s%c')"),
11179 operand_string,
11180 intel_syntax ? '[' : '(',
11181 register_prefix,
be05d201 11182 expected_reg->reg_name,
fc0763e6 11183 intel_syntax ? ']' : ')');
be05d201 11184 return 1;
fc0763e6 11185 }
be05d201
L
11186 else
11187 return 1;
11188
dc1e8a47 11189 bad_address:
be05d201
L
11190 as_bad (_("`%s' is not a valid %s expression"),
11191 operand_string, kind);
11192 return 0;
3e73aa7c
JH
11193 }
11194 else
11195 {
be05d201
L
11196 if (addr_mode != CODE_16BIT)
11197 {
11198 /* 32-bit/64-bit checks. */
41eb8e88
L
11199 if (i.disp_encoding == disp_encoding_16bit)
11200 {
11201 bad_disp:
11202 as_bad (_("invalid `%s' prefix"),
11203 addr_mode == CODE_16BIT ? "{disp32}" : "{disp16}");
11204 return 0;
11205 }
11206
be05d201 11207 if ((i.base_reg
e968fc9b
JB
11208 && ((addr_mode == CODE_64BIT
11209 ? !i.base_reg->reg_type.bitfield.qword
11210 : !i.base_reg->reg_type.bitfield.dword)
11211 || (i.index_reg && i.base_reg->reg_num == RegIP)
11212 || i.base_reg->reg_num == RegIZ))
be05d201 11213 || (i.index_reg
1b54b8d7
JB
11214 && !i.index_reg->reg_type.bitfield.xmmword
11215 && !i.index_reg->reg_type.bitfield.ymmword
11216 && !i.index_reg->reg_type.bitfield.zmmword
be05d201 11217 && ((addr_mode == CODE_64BIT
e968fc9b
JB
11218 ? !i.index_reg->reg_type.bitfield.qword
11219 : !i.index_reg->reg_type.bitfield.dword)
be05d201
L
11220 || !i.index_reg->reg_type.bitfield.baseindex)))
11221 goto bad_address;
8178be5b 11222
260cd341 11223 /* bndmk, bndldx, bndstx and mandatory non-vector SIB have special restrictions. */
a152332d 11224 if ((t->opcode_modifier.opcodeprefix == PREFIX_0XF3
389d00a5
JB
11225 && t->opcode_modifier.opcodespace == SPACE_0F
11226 && t->base_opcode == 0x1b)
a152332d 11227 || (t->opcode_modifier.opcodeprefix == PREFIX_NONE
389d00a5
JB
11228 && t->opcode_modifier.opcodespace == SPACE_0F
11229 && (t->base_opcode & ~1) == 0x1a)
a152332d 11230 || t->opcode_modifier.sib == SIBMEM)
8178be5b
JB
11231 {
11232 /* They cannot use RIP-relative addressing. */
e968fc9b 11233 if (i.base_reg && i.base_reg->reg_num == RegIP)
8178be5b
JB
11234 {
11235 as_bad (_("`%s' cannot be used here"), operand_string);
11236 return 0;
11237 }
11238
11239 /* bndldx and bndstx ignore their scale factor. */
a152332d 11240 if (t->opcode_modifier.opcodeprefix == PREFIX_NONE
389d00a5
JB
11241 && t->opcode_modifier.opcodespace == SPACE_0F
11242 && (t->base_opcode & ~1) == 0x1a
8178be5b
JB
11243 && i.log2_scale_factor)
11244 as_warn (_("register scaling is being ignored here"));
11245 }
be05d201
L
11246 }
11247 else
3e73aa7c 11248 {
be05d201 11249 /* 16-bit checks. */
41eb8e88
L
11250 if (i.disp_encoding == disp_encoding_32bit)
11251 goto bad_disp;
11252
3e73aa7c 11253 if ((i.base_reg
dc821c5f 11254 && (!i.base_reg->reg_type.bitfield.word
40fb9820 11255 || !i.base_reg->reg_type.bitfield.baseindex))
3e73aa7c 11256 || (i.index_reg
dc821c5f 11257 && (!i.index_reg->reg_type.bitfield.word
40fb9820 11258 || !i.index_reg->reg_type.bitfield.baseindex
29b0f896
AM
11259 || !(i.base_reg
11260 && i.base_reg->reg_num < 6
11261 && i.index_reg->reg_num >= 6
11262 && i.log2_scale_factor == 0))))
be05d201 11263 goto bad_address;
3e73aa7c
JH
11264 }
11265 }
be05d201 11266 return 1;
24eab124 11267}
252b5132 11268
43234a1e
L
11269/* Handle vector immediates. */
11270
11271static int
11272RC_SAE_immediate (const char *imm_start)
11273{
11274 unsigned int match_found, j;
11275 const char *pstr = imm_start;
11276 expressionS *exp;
11277
11278 if (*pstr != '{')
11279 return 0;
11280
11281 pstr++;
11282 match_found = 0;
11283 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); j++)
11284 {
11285 if (!strncmp (pstr, RC_NamesTable[j].name, RC_NamesTable[j].len))
11286 {
ca5312a2 11287 if (i.rounding.type != rc_none)
43234a1e
L
11288 {
11289 as_bad (_("duplicated `%s'"), imm_start);
11290 return 0;
11291 }
ca5312a2
JB
11292
11293 i.rounding.type = RC_NamesTable[j].type;
11294 i.rounding.operand = this_operand;
11295
43234a1e
L
11296 pstr += RC_NamesTable[j].len;
11297 match_found = 1;
11298 break;
11299 }
11300 }
11301 if (!match_found)
11302 return 0;
11303
11304 if (*pstr++ != '}')
11305 {
11306 as_bad (_("Missing '}': '%s'"), imm_start);
11307 return 0;
11308 }
11309 /* RC/SAE immediate string should contain nothing more. */;
11310 if (*pstr != 0)
11311 {
11312 as_bad (_("Junk after '}': '%s'"), imm_start);
11313 return 0;
11314 }
11315
11316 exp = &im_expressions[i.imm_operands++];
11317 i.op[this_operand].imms = exp;
11318
11319 exp->X_op = O_constant;
11320 exp->X_add_number = 0;
11321 exp->X_add_symbol = (symbolS *) 0;
11322 exp->X_op_symbol = (symbolS *) 0;
11323
11324 i.types[this_operand].bitfield.imm8 = 1;
11325 return 1;
11326}
11327
8325cc63
JB
11328/* Only string instructions can have a second memory operand, so
11329 reduce current_templates to just those if it contains any. */
11330static int
11331maybe_adjust_templates (void)
11332{
11333 const insn_template *t;
11334
11335 gas_assert (i.mem_operands == 1);
11336
11337 for (t = current_templates->start; t < current_templates->end; ++t)
11338 if (t->opcode_modifier.isstring)
11339 break;
11340
11341 if (t < current_templates->end)
11342 {
11343 static templates aux_templates;
5b7c81bd 11344 bool recheck;
8325cc63
JB
11345
11346 aux_templates.start = t;
11347 for (; t < current_templates->end; ++t)
11348 if (!t->opcode_modifier.isstring)
11349 break;
11350 aux_templates.end = t;
11351
11352 /* Determine whether to re-check the first memory operand. */
11353 recheck = (aux_templates.start != current_templates->start
11354 || t != current_templates->end);
11355
11356 current_templates = &aux_templates;
11357
11358 if (recheck)
11359 {
11360 i.mem_operands = 0;
11361 if (i.memop1_string != NULL
11362 && i386_index_check (i.memop1_string) == 0)
11363 return 0;
11364 i.mem_operands = 1;
11365 }
11366 }
11367
11368 return 1;
11369}
11370
fc0763e6 11371/* Parse OPERAND_STRING into the i386_insn structure I. Returns zero
47926f60 11372 on error. */
252b5132 11373
252b5132 11374static int
a7619375 11375i386_att_operand (char *operand_string)
252b5132 11376{
af6bdddf
AM
11377 const reg_entry *r;
11378 char *end_op;
24eab124 11379 char *op_string = operand_string;
252b5132 11380
24eab124 11381 if (is_space_char (*op_string))
252b5132
RH
11382 ++op_string;
11383
24eab124 11384 /* We check for an absolute prefix (differentiating,
47926f60 11385 for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
24eab124
AM
11386 if (*op_string == ABSOLUTE_PREFIX)
11387 {
11388 ++op_string;
11389 if (is_space_char (*op_string))
11390 ++op_string;
5b7c81bd 11391 i.jumpabsolute = true;
24eab124 11392 }
252b5132 11393
47926f60 11394 /* Check if operand is a register. */
4d1bb795 11395 if ((r = parse_register (op_string, &end_op)) != NULL)
24eab124 11396 {
40fb9820
L
11397 i386_operand_type temp;
11398
8a6fb3f9
JB
11399 if (r == &bad_reg)
11400 return 0;
11401
24eab124
AM
11402 /* Check for a segment override by searching for ':' after a
11403 segment register. */
11404 op_string = end_op;
11405 if (is_space_char (*op_string))
11406 ++op_string;
00cee14f 11407 if (*op_string == ':' && r->reg_type.bitfield.class == SReg)
24eab124 11408 {
5e042380 11409 i.seg[i.mem_operands] = r;
252b5132 11410
24eab124 11411 /* Skip the ':' and whitespace. */
252b5132
RH
11412 ++op_string;
11413 if (is_space_char (*op_string))
24eab124 11414 ++op_string;
252b5132 11415
24eab124
AM
11416 if (!is_digit_char (*op_string)
11417 && !is_identifier_char (*op_string)
11418 && *op_string != '('
11419 && *op_string != ABSOLUTE_PREFIX)
11420 {
11421 as_bad (_("bad memory operand `%s'"), op_string);
11422 return 0;
11423 }
47926f60 11424 /* Handle case of %es:*foo. */
24eab124
AM
11425 if (*op_string == ABSOLUTE_PREFIX)
11426 {
11427 ++op_string;
11428 if (is_space_char (*op_string))
11429 ++op_string;
5b7c81bd 11430 i.jumpabsolute = true;
24eab124
AM
11431 }
11432 goto do_memory_reference;
11433 }
43234a1e
L
11434
11435 /* Handle vector operations. */
11436 if (*op_string == '{')
11437 {
f70c6814 11438 op_string = check_VecOperations (op_string);
43234a1e
L
11439 if (op_string == NULL)
11440 return 0;
11441 }
11442
24eab124
AM
11443 if (*op_string)
11444 {
d0b47220 11445 as_bad (_("junk `%s' after register"), op_string);
24eab124
AM
11446 return 0;
11447 }
40fb9820
L
11448 temp = r->reg_type;
11449 temp.bitfield.baseindex = 0;
c6fb90c8
L
11450 i.types[this_operand] = operand_type_or (i.types[this_operand],
11451 temp);
7d5e4556 11452 i.types[this_operand].bitfield.unspecified = 0;
520dc8e8 11453 i.op[this_operand].regs = r;
24eab124
AM
11454 i.reg_operands++;
11455 }
af6bdddf
AM
11456 else if (*op_string == REGISTER_PREFIX)
11457 {
11458 as_bad (_("bad register name `%s'"), op_string);
11459 return 0;
11460 }
24eab124 11461 else if (*op_string == IMMEDIATE_PREFIX)
ce8a8b2f 11462 {
24eab124 11463 ++op_string;
6f2f06be 11464 if (i.jumpabsolute)
24eab124 11465 {
d0b47220 11466 as_bad (_("immediate operand illegal with absolute jump"));
24eab124
AM
11467 return 0;
11468 }
11469 if (!i386_immediate (op_string))
11470 return 0;
11471 }
43234a1e
L
11472 else if (RC_SAE_immediate (operand_string))
11473 {
11474 /* If it is a RC or SAE immediate, do nothing. */
11475 ;
11476 }
24eab124
AM
11477 else if (is_digit_char (*op_string)
11478 || is_identifier_char (*op_string)
d02603dc 11479 || *op_string == '"'
e5cb08ac 11480 || *op_string == '(')
24eab124 11481 {
47926f60 11482 /* This is a memory reference of some sort. */
af6bdddf 11483 char *base_string;
252b5132 11484
47926f60 11485 /* Start and end of displacement string expression (if found). */
eecb386c
AM
11486 char *displacement_string_start;
11487 char *displacement_string_end;
43234a1e 11488 char *vop_start;
252b5132 11489
24eab124 11490 do_memory_reference:
8325cc63
JB
11491 if (i.mem_operands == 1 && !maybe_adjust_templates ())
11492 return 0;
24eab124 11493 if ((i.mem_operands == 1
40fb9820 11494 && !current_templates->start->opcode_modifier.isstring)
24eab124
AM
11495 || i.mem_operands == 2)
11496 {
11497 as_bad (_("too many memory references for `%s'"),
11498 current_templates->start->name);
11499 return 0;
11500 }
252b5132 11501
24eab124
AM
11502 /* Check for base index form. We detect the base index form by
11503 looking for an ')' at the end of the operand, searching
11504 for the '(' matching it, and finding a REGISTER_PREFIX or ','
11505 after the '('. */
af6bdddf 11506 base_string = op_string + strlen (op_string);
c3332e24 11507
43234a1e
L
11508 /* Handle vector operations. */
11509 vop_start = strchr (op_string, '{');
11510 if (vop_start && vop_start < base_string)
11511 {
f70c6814 11512 if (check_VecOperations (vop_start) == NULL)
43234a1e
L
11513 return 0;
11514 base_string = vop_start;
11515 }
11516
af6bdddf
AM
11517 --base_string;
11518 if (is_space_char (*base_string))
11519 --base_string;
252b5132 11520
47926f60 11521 /* If we only have a displacement, set-up for it to be parsed later. */
af6bdddf
AM
11522 displacement_string_start = op_string;
11523 displacement_string_end = base_string + 1;
252b5132 11524
24eab124
AM
11525 if (*base_string == ')')
11526 {
af6bdddf 11527 char *temp_string;
24eab124
AM
11528 unsigned int parens_balanced = 1;
11529 /* We've already checked that the number of left & right ()'s are
47926f60 11530 equal, so this loop will not be infinite. */
24eab124
AM
11531 do
11532 {
11533 base_string--;
11534 if (*base_string == ')')
11535 parens_balanced++;
11536 if (*base_string == '(')
11537 parens_balanced--;
11538 }
11539 while (parens_balanced);
c3332e24 11540
af6bdddf 11541 temp_string = base_string;
c3332e24 11542
24eab124 11543 /* Skip past '(' and whitespace. */
252b5132
RH
11544 ++base_string;
11545 if (is_space_char (*base_string))
24eab124 11546 ++base_string;
252b5132 11547
af6bdddf 11548 if (*base_string == ','
4eed87de
AM
11549 || ((i.base_reg = parse_register (base_string, &end_op))
11550 != NULL))
252b5132 11551 {
af6bdddf 11552 displacement_string_end = temp_string;
252b5132 11553
40fb9820 11554 i.types[this_operand].bitfield.baseindex = 1;
252b5132 11555
af6bdddf 11556 if (i.base_reg)
24eab124 11557 {
8a6fb3f9
JB
11558 if (i.base_reg == &bad_reg)
11559 return 0;
24eab124
AM
11560 base_string = end_op;
11561 if (is_space_char (*base_string))
11562 ++base_string;
af6bdddf
AM
11563 }
11564
11565 /* There may be an index reg or scale factor here. */
11566 if (*base_string == ',')
11567 {
11568 ++base_string;
11569 if (is_space_char (*base_string))
11570 ++base_string;
11571
4eed87de
AM
11572 if ((i.index_reg = parse_register (base_string, &end_op))
11573 != NULL)
24eab124 11574 {
8a6fb3f9
JB
11575 if (i.index_reg == &bad_reg)
11576 return 0;
af6bdddf 11577 base_string = end_op;
24eab124
AM
11578 if (is_space_char (*base_string))
11579 ++base_string;
af6bdddf
AM
11580 if (*base_string == ',')
11581 {
11582 ++base_string;
11583 if (is_space_char (*base_string))
11584 ++base_string;
11585 }
e5cb08ac 11586 else if (*base_string != ')')
af6bdddf 11587 {
4eed87de
AM
11588 as_bad (_("expecting `,' or `)' "
11589 "after index register in `%s'"),
af6bdddf
AM
11590 operand_string);
11591 return 0;
11592 }
24eab124 11593 }
af6bdddf 11594 else if (*base_string == REGISTER_PREFIX)
24eab124 11595 {
f76bf5e0
L
11596 end_op = strchr (base_string, ',');
11597 if (end_op)
11598 *end_op = '\0';
af6bdddf 11599 as_bad (_("bad register name `%s'"), base_string);
24eab124
AM
11600 return 0;
11601 }
252b5132 11602
47926f60 11603 /* Check for scale factor. */
551c1ca1 11604 if (*base_string != ')')
af6bdddf 11605 {
551c1ca1
AM
11606 char *end_scale = i386_scale (base_string);
11607
11608 if (!end_scale)
af6bdddf 11609 return 0;
24eab124 11610
551c1ca1 11611 base_string = end_scale;
af6bdddf
AM
11612 if (is_space_char (*base_string))
11613 ++base_string;
11614 if (*base_string != ')')
11615 {
4eed87de
AM
11616 as_bad (_("expecting `)' "
11617 "after scale factor in `%s'"),
af6bdddf
AM
11618 operand_string);
11619 return 0;
11620 }
11621 }
11622 else if (!i.index_reg)
24eab124 11623 {
4eed87de
AM
11624 as_bad (_("expecting index register or scale factor "
11625 "after `,'; got '%c'"),
af6bdddf 11626 *base_string);
24eab124
AM
11627 return 0;
11628 }
11629 }
af6bdddf 11630 else if (*base_string != ')')
24eab124 11631 {
4eed87de
AM
11632 as_bad (_("expecting `,' or `)' "
11633 "after base register in `%s'"),
af6bdddf 11634 operand_string);
24eab124
AM
11635 return 0;
11636 }
c3332e24 11637 }
af6bdddf 11638 else if (*base_string == REGISTER_PREFIX)
c3332e24 11639 {
f76bf5e0
L
11640 end_op = strchr (base_string, ',');
11641 if (end_op)
11642 *end_op = '\0';
af6bdddf 11643 as_bad (_("bad register name `%s'"), base_string);
24eab124 11644 return 0;
c3332e24 11645 }
24eab124
AM
11646 }
11647
11648 /* If there's an expression beginning the operand, parse it,
11649 assuming displacement_string_start and
11650 displacement_string_end are meaningful. */
11651 if (displacement_string_start != displacement_string_end)
11652 {
11653 if (!i386_displacement (displacement_string_start,
11654 displacement_string_end))
11655 return 0;
11656 }
11657
11658 /* Special case for (%dx) while doing input/output op. */
11659 if (i.base_reg
75e5731b
JB
11660 && i.base_reg->reg_type.bitfield.instance == RegD
11661 && i.base_reg->reg_type.bitfield.word
24eab124
AM
11662 && i.index_reg == 0
11663 && i.log2_scale_factor == 0
11664 && i.seg[i.mem_operands] == 0
40fb9820 11665 && !operand_type_check (i.types[this_operand], disp))
24eab124 11666 {
2fb5be8d 11667 i.types[this_operand] = i.base_reg->reg_type;
24eab124
AM
11668 return 1;
11669 }
11670
eecb386c
AM
11671 if (i386_index_check (operand_string) == 0)
11672 return 0;
c48dadc9 11673 i.flags[this_operand] |= Operand_Mem;
8325cc63
JB
11674 if (i.mem_operands == 0)
11675 i.memop1_string = xstrdup (operand_string);
24eab124
AM
11676 i.mem_operands++;
11677 }
11678 else
ce8a8b2f
AM
11679 {
11680 /* It's not a memory operand; argh! */
24eab124
AM
11681 as_bad (_("invalid char %s beginning operand %d `%s'"),
11682 output_invalid (*op_string),
11683 this_operand + 1,
11684 op_string);
11685 return 0;
11686 }
47926f60 11687 return 1; /* Normal return. */
252b5132
RH
11688}
11689\f
fa94de6b
RM
11690/* Calculate the maximum variable size (i.e., excluding fr_fix)
11691 that an rs_machine_dependent frag may reach. */
11692
11693unsigned int
11694i386_frag_max_var (fragS *frag)
11695{
11696 /* The only relaxable frags are for jumps.
11697 Unconditional jumps can grow by 4 bytes and others by 5 bytes. */
11698 gas_assert (frag->fr_type == rs_machine_dependent);
11699 return TYPE_FROM_RELAX_STATE (frag->fr_subtype) == UNCOND_JUMP ? 4 : 5;
11700}
11701
b084df0b
L
11702#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
11703static int
8dcea932 11704elf_symbol_resolved_in_segment_p (symbolS *fr_symbol, offsetT fr_var)
b084df0b
L
11705{
11706 /* STT_GNU_IFUNC symbol must go through PLT. */
11707 if ((symbol_get_bfdsym (fr_symbol)->flags
11708 & BSF_GNU_INDIRECT_FUNCTION) != 0)
11709 return 0;
11710
11711 if (!S_IS_EXTERNAL (fr_symbol))
11712 /* Symbol may be weak or local. */
11713 return !S_IS_WEAK (fr_symbol);
11714
8dcea932
L
11715 /* Global symbols with non-default visibility can't be preempted. */
11716 if (ELF_ST_VISIBILITY (S_GET_OTHER (fr_symbol)) != STV_DEFAULT)
11717 return 1;
11718
11719 if (fr_var != NO_RELOC)
11720 switch ((enum bfd_reloc_code_real) fr_var)
11721 {
11722 case BFD_RELOC_386_PLT32:
11723 case BFD_RELOC_X86_64_PLT32:
33eaf5de 11724 /* Symbol with PLT relocation may be preempted. */
8dcea932
L
11725 return 0;
11726 default:
11727 abort ();
11728 }
11729
b084df0b
L
11730 /* Global symbols with default visibility in a shared library may be
11731 preempted by another definition. */
8dcea932 11732 return !shared;
b084df0b
L
11733}
11734#endif
11735
79d72f45
HL
11736/* Table 3-2. Macro-Fusible Instructions in Haswell Microarchitecture
11737 Note also work for Skylake and Cascadelake.
11738---------------------------------------------------------------------
11739| JCC | ADD/SUB/CMP | INC/DEC | TEST/AND |
11740| ------ | ----------- | ------- | -------- |
11741| Jo | N | N | Y |
11742| Jno | N | N | Y |
11743| Jc/Jb | Y | N | Y |
11744| Jae/Jnb | Y | N | Y |
11745| Je/Jz | Y | Y | Y |
11746| Jne/Jnz | Y | Y | Y |
11747| Jna/Jbe | Y | N | Y |
11748| Ja/Jnbe | Y | N | Y |
11749| Js | N | N | Y |
11750| Jns | N | N | Y |
11751| Jp/Jpe | N | N | Y |
11752| Jnp/Jpo | N | N | Y |
11753| Jl/Jnge | Y | Y | Y |
11754| Jge/Jnl | Y | Y | Y |
11755| Jle/Jng | Y | Y | Y |
11756| Jg/Jnle | Y | Y | Y |
11757--------------------------------------------------------------------- */
11758static int
11759i386_macro_fusible_p (enum mf_cmp_kind mf_cmp, enum mf_jcc_kind mf_jcc)
11760{
11761 if (mf_cmp == mf_cmp_alu_cmp)
11762 return ((mf_jcc >= mf_jcc_jc && mf_jcc <= mf_jcc_jna)
11763 || mf_jcc == mf_jcc_jl || mf_jcc == mf_jcc_jle);
11764 if (mf_cmp == mf_cmp_incdec)
11765 return (mf_jcc == mf_jcc_je || mf_jcc == mf_jcc_jl
11766 || mf_jcc == mf_jcc_jle);
11767 if (mf_cmp == mf_cmp_test_and)
11768 return 1;
11769 return 0;
11770}
11771
e379e5f3
L
11772/* Return the next non-empty frag. */
11773
11774static fragS *
11775i386_next_non_empty_frag (fragS *fragP)
11776{
11777 /* There may be a frag with a ".fill 0" when there is no room in
11778 the current frag for frag_grow in output_insn. */
11779 for (fragP = fragP->fr_next;
11780 (fragP != NULL
11781 && fragP->fr_type == rs_fill
11782 && fragP->fr_fix == 0);
11783 fragP = fragP->fr_next)
11784 ;
11785 return fragP;
11786}
11787
11788/* Return the next jcc frag after BRANCH_PADDING. */
11789
11790static fragS *
79d72f45 11791i386_next_fusible_jcc_frag (fragS *maybe_cmp_fragP, fragS *pad_fragP)
e379e5f3 11792{
79d72f45
HL
11793 fragS *branch_fragP;
11794 if (!pad_fragP)
e379e5f3
L
11795 return NULL;
11796
79d72f45
HL
11797 if (pad_fragP->fr_type == rs_machine_dependent
11798 && (TYPE_FROM_RELAX_STATE (pad_fragP->fr_subtype)
e379e5f3
L
11799 == BRANCH_PADDING))
11800 {
79d72f45
HL
11801 branch_fragP = i386_next_non_empty_frag (pad_fragP);
11802 if (branch_fragP->fr_type != rs_machine_dependent)
e379e5f3 11803 return NULL;
79d72f45
HL
11804 if (TYPE_FROM_RELAX_STATE (branch_fragP->fr_subtype) == COND_JUMP
11805 && i386_macro_fusible_p (maybe_cmp_fragP->tc_frag_data.mf_type,
11806 pad_fragP->tc_frag_data.mf_type))
11807 return branch_fragP;
e379e5f3
L
11808 }
11809
11810 return NULL;
11811}
11812
11813/* Classify BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags. */
11814
11815static void
11816i386_classify_machine_dependent_frag (fragS *fragP)
11817{
11818 fragS *cmp_fragP;
11819 fragS *pad_fragP;
11820 fragS *branch_fragP;
11821 fragS *next_fragP;
11822 unsigned int max_prefix_length;
11823
11824 if (fragP->tc_frag_data.classified)
11825 return;
11826
11827 /* First scan for BRANCH_PADDING and FUSED_JCC_PADDING. Convert
11828 FUSED_JCC_PADDING and merge BRANCH_PADDING. */
11829 for (next_fragP = fragP;
11830 next_fragP != NULL;
11831 next_fragP = next_fragP->fr_next)
11832 {
11833 next_fragP->tc_frag_data.classified = 1;
11834 if (next_fragP->fr_type == rs_machine_dependent)
11835 switch (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype))
11836 {
11837 case BRANCH_PADDING:
11838 /* The BRANCH_PADDING frag must be followed by a branch
11839 frag. */
11840 branch_fragP = i386_next_non_empty_frag (next_fragP);
11841 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
11842 break;
11843 case FUSED_JCC_PADDING:
11844 /* Check if this is a fused jcc:
11845 FUSED_JCC_PADDING
11846 CMP like instruction
11847 BRANCH_PADDING
11848 COND_JUMP
11849 */
11850 cmp_fragP = i386_next_non_empty_frag (next_fragP);
11851 pad_fragP = i386_next_non_empty_frag (cmp_fragP);
79d72f45 11852 branch_fragP = i386_next_fusible_jcc_frag (next_fragP, pad_fragP);
e379e5f3
L
11853 if (branch_fragP)
11854 {
11855 /* The BRANCH_PADDING frag is merged with the
11856 FUSED_JCC_PADDING frag. */
11857 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
11858 /* CMP like instruction size. */
11859 next_fragP->tc_frag_data.cmp_size = cmp_fragP->fr_fix;
11860 frag_wane (pad_fragP);
11861 /* Skip to branch_fragP. */
11862 next_fragP = branch_fragP;
11863 }
11864 else if (next_fragP->tc_frag_data.max_prefix_length)
11865 {
11866 /* Turn FUSED_JCC_PADDING into BRANCH_PREFIX if it isn't
11867 a fused jcc. */
11868 next_fragP->fr_subtype
11869 = ENCODE_RELAX_STATE (BRANCH_PREFIX, 0);
11870 next_fragP->tc_frag_data.max_bytes
11871 = next_fragP->tc_frag_data.max_prefix_length;
11872 /* This will be updated in the BRANCH_PREFIX scan. */
11873 next_fragP->tc_frag_data.max_prefix_length = 0;
11874 }
11875 else
11876 frag_wane (next_fragP);
11877 break;
11878 }
11879 }
11880
11881 /* Stop if there is no BRANCH_PREFIX. */
11882 if (!align_branch_prefix_size)
11883 return;
11884
11885 /* Scan for BRANCH_PREFIX. */
11886 for (; fragP != NULL; fragP = fragP->fr_next)
11887 {
11888 if (fragP->fr_type != rs_machine_dependent
11889 || (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
11890 != BRANCH_PREFIX))
11891 continue;
11892
11893 /* Count all BRANCH_PREFIX frags before BRANCH_PADDING and
11894 COND_JUMP_PREFIX. */
11895 max_prefix_length = 0;
11896 for (next_fragP = fragP;
11897 next_fragP != NULL;
11898 next_fragP = next_fragP->fr_next)
11899 {
11900 if (next_fragP->fr_type == rs_fill)
11901 /* Skip rs_fill frags. */
11902 continue;
11903 else if (next_fragP->fr_type != rs_machine_dependent)
11904 /* Stop for all other frags. */
11905 break;
11906
11907 /* rs_machine_dependent frags. */
11908 if (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
11909 == BRANCH_PREFIX)
11910 {
11911 /* Count BRANCH_PREFIX frags. */
11912 if (max_prefix_length >= MAX_FUSED_JCC_PADDING_SIZE)
11913 {
11914 max_prefix_length = MAX_FUSED_JCC_PADDING_SIZE;
11915 frag_wane (next_fragP);
11916 }
11917 else
11918 max_prefix_length
11919 += next_fragP->tc_frag_data.max_bytes;
11920 }
11921 else if ((TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
11922 == BRANCH_PADDING)
11923 || (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
11924 == FUSED_JCC_PADDING))
11925 {
11926 /* Stop at BRANCH_PADDING and FUSED_JCC_PADDING. */
11927 fragP->tc_frag_data.u.padding_fragP = next_fragP;
11928 break;
11929 }
11930 else
11931 /* Stop for other rs_machine_dependent frags. */
11932 break;
11933 }
11934
11935 fragP->tc_frag_data.max_prefix_length = max_prefix_length;
11936
11937 /* Skip to the next frag. */
11938 fragP = next_fragP;
11939 }
11940}
11941
11942/* Compute padding size for
11943
11944 FUSED_JCC_PADDING
11945 CMP like instruction
11946 BRANCH_PADDING
11947 COND_JUMP/UNCOND_JUMP
11948
11949 or
11950
11951 BRANCH_PADDING
11952 COND_JUMP/UNCOND_JUMP
11953 */
11954
11955static int
11956i386_branch_padding_size (fragS *fragP, offsetT address)
11957{
11958 unsigned int offset, size, padding_size;
11959 fragS *branch_fragP = fragP->tc_frag_data.u.branch_fragP;
11960
11961 /* The start address of the BRANCH_PADDING or FUSED_JCC_PADDING frag. */
11962 if (!address)
11963 address = fragP->fr_address;
11964 address += fragP->fr_fix;
11965
11966 /* CMP like instrunction size. */
11967 size = fragP->tc_frag_data.cmp_size;
11968
11969 /* The base size of the branch frag. */
11970 size += branch_fragP->fr_fix;
11971
11972 /* Add opcode and displacement bytes for the rs_machine_dependent
11973 branch frag. */
11974 if (branch_fragP->fr_type == rs_machine_dependent)
11975 size += md_relax_table[branch_fragP->fr_subtype].rlx_length;
11976
11977 /* Check if branch is within boundary and doesn't end at the last
11978 byte. */
11979 offset = address & ((1U << align_branch_power) - 1);
11980 if ((offset + size) >= (1U << align_branch_power))
11981 /* Padding needed to avoid crossing boundary. */
11982 padding_size = (1U << align_branch_power) - offset;
11983 else
11984 /* No padding needed. */
11985 padding_size = 0;
11986
11987 /* The return value may be saved in tc_frag_data.length which is
11988 unsigned byte. */
11989 if (!fits_in_unsigned_byte (padding_size))
11990 abort ();
11991
11992 return padding_size;
11993}
11994
11995/* i386_generic_table_relax_frag()
11996
11997 Handle BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags to
11998 grow/shrink padding to align branch frags. Hand others to
11999 relax_frag(). */
12000
12001long
12002i386_generic_table_relax_frag (segT segment, fragS *fragP, long stretch)
12003{
12004 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12005 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
12006 {
12007 long padding_size = i386_branch_padding_size (fragP, 0);
12008 long grow = padding_size - fragP->tc_frag_data.length;
12009
12010 /* When the BRANCH_PREFIX frag is used, the computed address
12011 must match the actual address and there should be no padding. */
12012 if (fragP->tc_frag_data.padding_address
12013 && (fragP->tc_frag_data.padding_address != fragP->fr_address
12014 || padding_size))
12015 abort ();
12016
12017 /* Update the padding size. */
12018 if (grow)
12019 fragP->tc_frag_data.length = padding_size;
12020
12021 return grow;
12022 }
12023 else if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12024 {
12025 fragS *padding_fragP, *next_fragP;
12026 long padding_size, left_size, last_size;
12027
12028 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
12029 if (!padding_fragP)
12030 /* Use the padding set by the leading BRANCH_PREFIX frag. */
12031 return (fragP->tc_frag_data.length
12032 - fragP->tc_frag_data.last_length);
12033
12034 /* Compute the relative address of the padding frag in the very
12035 first time where the BRANCH_PREFIX frag sizes are zero. */
12036 if (!fragP->tc_frag_data.padding_address)
12037 fragP->tc_frag_data.padding_address
12038 = padding_fragP->fr_address - (fragP->fr_address - stretch);
12039
12040 /* First update the last length from the previous interation. */
12041 left_size = fragP->tc_frag_data.prefix_length;
12042 for (next_fragP = fragP;
12043 next_fragP != padding_fragP;
12044 next_fragP = next_fragP->fr_next)
12045 if (next_fragP->fr_type == rs_machine_dependent
12046 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12047 == BRANCH_PREFIX))
12048 {
12049 if (left_size)
12050 {
12051 int max = next_fragP->tc_frag_data.max_bytes;
12052 if (max)
12053 {
12054 int size;
12055 if (max > left_size)
12056 size = left_size;
12057 else
12058 size = max;
12059 left_size -= size;
12060 next_fragP->tc_frag_data.last_length = size;
12061 }
12062 }
12063 else
12064 next_fragP->tc_frag_data.last_length = 0;
12065 }
12066
12067 /* Check the padding size for the padding frag. */
12068 padding_size = i386_branch_padding_size
12069 (padding_fragP, (fragP->fr_address
12070 + fragP->tc_frag_data.padding_address));
12071
12072 last_size = fragP->tc_frag_data.prefix_length;
12073 /* Check if there is change from the last interation. */
12074 if (padding_size == last_size)
12075 {
12076 /* Update the expected address of the padding frag. */
12077 padding_fragP->tc_frag_data.padding_address
12078 = (fragP->fr_address + padding_size
12079 + fragP->tc_frag_data.padding_address);
12080 return 0;
12081 }
12082
12083 if (padding_size > fragP->tc_frag_data.max_prefix_length)
12084 {
12085 /* No padding if there is no sufficient room. Clear the
12086 expected address of the padding frag. */
12087 padding_fragP->tc_frag_data.padding_address = 0;
12088 padding_size = 0;
12089 }
12090 else
12091 /* Store the expected address of the padding frag. */
12092 padding_fragP->tc_frag_data.padding_address
12093 = (fragP->fr_address + padding_size
12094 + fragP->tc_frag_data.padding_address);
12095
12096 fragP->tc_frag_data.prefix_length = padding_size;
12097
12098 /* Update the length for the current interation. */
12099 left_size = padding_size;
12100 for (next_fragP = fragP;
12101 next_fragP != padding_fragP;
12102 next_fragP = next_fragP->fr_next)
12103 if (next_fragP->fr_type == rs_machine_dependent
12104 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12105 == BRANCH_PREFIX))
12106 {
12107 if (left_size)
12108 {
12109 int max = next_fragP->tc_frag_data.max_bytes;
12110 if (max)
12111 {
12112 int size;
12113 if (max > left_size)
12114 size = left_size;
12115 else
12116 size = max;
12117 left_size -= size;
12118 next_fragP->tc_frag_data.length = size;
12119 }
12120 }
12121 else
12122 next_fragP->tc_frag_data.length = 0;
12123 }
12124
12125 return (fragP->tc_frag_data.length
12126 - fragP->tc_frag_data.last_length);
12127 }
12128 return relax_frag (segment, fragP, stretch);
12129}
12130
ee7fcc42
AM
12131/* md_estimate_size_before_relax()
12132
12133 Called just before relax() for rs_machine_dependent frags. The x86
12134 assembler uses these frags to handle variable size jump
12135 instructions.
12136
12137 Any symbol that is now undefined will not become defined.
12138 Return the correct fr_subtype in the frag.
12139 Return the initial "guess for variable size of frag" to caller.
12140 The guess is actually the growth beyond the fixed part. Whatever
12141 we do to grow the fixed or variable part contributes to our
12142 returned value. */
12143
252b5132 12144int
7016a5d5 12145md_estimate_size_before_relax (fragS *fragP, segT segment)
252b5132 12146{
e379e5f3
L
12147 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12148 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX
12149 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
12150 {
12151 i386_classify_machine_dependent_frag (fragP);
12152 return fragP->tc_frag_data.length;
12153 }
12154
252b5132 12155 /* We've already got fragP->fr_subtype right; all we have to do is
b98ef147
AM
12156 check for un-relaxable symbols. On an ELF system, we can't relax
12157 an externally visible symbol, because it may be overridden by a
12158 shared library. */
12159 if (S_GET_SEGMENT (fragP->fr_symbol) != segment
6d249963 12160#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12161 || (IS_ELF
8dcea932
L
12162 && !elf_symbol_resolved_in_segment_p (fragP->fr_symbol,
12163 fragP->fr_var))
fbeb56a4
DK
12164#endif
12165#if defined (OBJ_COFF) && defined (TE_PE)
7ab9ffdd 12166 || (OUTPUT_FLAVOR == bfd_target_coff_flavour
fbeb56a4 12167 && S_IS_WEAK (fragP->fr_symbol))
b98ef147
AM
12168#endif
12169 )
252b5132 12170 {
b98ef147
AM
12171 /* Symbol is undefined in this segment, or we need to keep a
12172 reloc so that weak symbols can be overridden. */
12173 int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
f86103b7 12174 enum bfd_reloc_code_real reloc_type;
ee7fcc42
AM
12175 unsigned char *opcode;
12176 int old_fr_fix;
eb19308f 12177 fixS *fixP = NULL;
f6af82bd 12178
ee7fcc42 12179 if (fragP->fr_var != NO_RELOC)
1e9cc1c2 12180 reloc_type = (enum bfd_reloc_code_real) fragP->fr_var;
b98ef147 12181 else if (size == 2)
f6af82bd 12182 reloc_type = BFD_RELOC_16_PCREL;
bd7ab16b
L
12183#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
12184 else if (need_plt32_p (fragP->fr_symbol))
12185 reloc_type = BFD_RELOC_X86_64_PLT32;
12186#endif
f6af82bd
AM
12187 else
12188 reloc_type = BFD_RELOC_32_PCREL;
252b5132 12189
ee7fcc42
AM
12190 old_fr_fix = fragP->fr_fix;
12191 opcode = (unsigned char *) fragP->fr_opcode;
12192
fddf5b5b 12193 switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
252b5132 12194 {
fddf5b5b
AM
12195 case UNCOND_JUMP:
12196 /* Make jmp (0xeb) a (d)word displacement jump. */
47926f60 12197 opcode[0] = 0xe9;
252b5132 12198 fragP->fr_fix += size;
eb19308f
JB
12199 fixP = fix_new (fragP, old_fr_fix, size,
12200 fragP->fr_symbol,
12201 fragP->fr_offset, 1,
12202 reloc_type);
252b5132
RH
12203 break;
12204
fddf5b5b 12205 case COND_JUMP86:
412167cb
AM
12206 if (size == 2
12207 && (!no_cond_jump_promotion || fragP->fr_var != NO_RELOC))
fddf5b5b
AM
12208 {
12209 /* Negate the condition, and branch past an
12210 unconditional jump. */
12211 opcode[0] ^= 1;
12212 opcode[1] = 3;
12213 /* Insert an unconditional jump. */
12214 opcode[2] = 0xe9;
12215 /* We added two extra opcode bytes, and have a two byte
12216 offset. */
12217 fragP->fr_fix += 2 + 2;
062cd5e7
AS
12218 fix_new (fragP, old_fr_fix + 2, 2,
12219 fragP->fr_symbol,
12220 fragP->fr_offset, 1,
12221 reloc_type);
fddf5b5b
AM
12222 break;
12223 }
12224 /* Fall through. */
12225
12226 case COND_JUMP:
412167cb
AM
12227 if (no_cond_jump_promotion && fragP->fr_var == NO_RELOC)
12228 {
12229 fragP->fr_fix += 1;
3e02c1cc
AM
12230 fixP = fix_new (fragP, old_fr_fix, 1,
12231 fragP->fr_symbol,
12232 fragP->fr_offset, 1,
12233 BFD_RELOC_8_PCREL);
12234 fixP->fx_signed = 1;
412167cb
AM
12235 break;
12236 }
93c2a809 12237
24eab124 12238 /* This changes the byte-displacement jump 0x7N
fddf5b5b 12239 to the (d)word-displacement jump 0x0f,0x8N. */
252b5132 12240 opcode[1] = opcode[0] + 0x10;
f6af82bd 12241 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
47926f60
KH
12242 /* We've added an opcode byte. */
12243 fragP->fr_fix += 1 + size;
eb19308f
JB
12244 fixP = fix_new (fragP, old_fr_fix + 1, size,
12245 fragP->fr_symbol,
12246 fragP->fr_offset, 1,
12247 reloc_type);
252b5132 12248 break;
fddf5b5b
AM
12249
12250 default:
12251 BAD_CASE (fragP->fr_subtype);
12252 break;
252b5132 12253 }
eb19308f
JB
12254
12255 /* All jumps handled here are signed, but don't unconditionally use a
12256 signed limit check for 32 and 16 bit jumps as we want to allow wrap
12257 around at 4G (outside of 64-bit mode) and 64k. */
12258 if (size == 4 && flag_code == CODE_64BIT)
12259 fixP->fx_signed = 1;
12260
252b5132 12261 frag_wane (fragP);
ee7fcc42 12262 return fragP->fr_fix - old_fr_fix;
252b5132 12263 }
93c2a809 12264
93c2a809
AM
12265 /* Guess size depending on current relax state. Initially the relax
12266 state will correspond to a short jump and we return 1, because
12267 the variable part of the frag (the branch offset) is one byte
12268 long. However, we can relax a section more than once and in that
12269 case we must either set fr_subtype back to the unrelaxed state,
12270 or return the value for the appropriate branch. */
12271 return md_relax_table[fragP->fr_subtype].rlx_length;
ee7fcc42
AM
12272}
12273
47926f60
KH
12274/* Called after relax() is finished.
12275
12276 In: Address of frag.
12277 fr_type == rs_machine_dependent.
12278 fr_subtype is what the address relaxed to.
12279
12280 Out: Any fixSs and constants are set up.
12281 Caller will turn frag into a ".space 0". */
12282
252b5132 12283void
7016a5d5
TG
12284md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec ATTRIBUTE_UNUSED,
12285 fragS *fragP)
252b5132 12286{
29b0f896 12287 unsigned char *opcode;
252b5132 12288 unsigned char *where_to_put_displacement = NULL;
847f7ad4
AM
12289 offsetT target_address;
12290 offsetT opcode_address;
252b5132 12291 unsigned int extension = 0;
847f7ad4 12292 offsetT displacement_from_opcode_start;
252b5132 12293
e379e5f3
L
12294 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12295 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING
12296 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12297 {
12298 /* Generate nop padding. */
12299 unsigned int size = fragP->tc_frag_data.length;
12300 if (size)
12301 {
12302 if (size > fragP->tc_frag_data.max_bytes)
12303 abort ();
12304
12305 if (flag_debug)
12306 {
12307 const char *msg;
12308 const char *branch = "branch";
12309 const char *prefix = "";
12310 fragS *padding_fragP;
12311 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
12312 == BRANCH_PREFIX)
12313 {
12314 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
12315 switch (fragP->tc_frag_data.default_prefix)
12316 {
12317 default:
12318 abort ();
12319 break;
12320 case CS_PREFIX_OPCODE:
12321 prefix = " cs";
12322 break;
12323 case DS_PREFIX_OPCODE:
12324 prefix = " ds";
12325 break;
12326 case ES_PREFIX_OPCODE:
12327 prefix = " es";
12328 break;
12329 case FS_PREFIX_OPCODE:
12330 prefix = " fs";
12331 break;
12332 case GS_PREFIX_OPCODE:
12333 prefix = " gs";
12334 break;
12335 case SS_PREFIX_OPCODE:
12336 prefix = " ss";
12337 break;
12338 }
12339 if (padding_fragP)
12340 msg = _("%s:%u: add %d%s at 0x%llx to align "
12341 "%s within %d-byte boundary\n");
12342 else
12343 msg = _("%s:%u: add additional %d%s at 0x%llx to "
12344 "align %s within %d-byte boundary\n");
12345 }
12346 else
12347 {
12348 padding_fragP = fragP;
12349 msg = _("%s:%u: add %d%s-byte nop at 0x%llx to align "
12350 "%s within %d-byte boundary\n");
12351 }
12352
12353 if (padding_fragP)
12354 switch (padding_fragP->tc_frag_data.branch_type)
12355 {
12356 case align_branch_jcc:
12357 branch = "jcc";
12358 break;
12359 case align_branch_fused:
12360 branch = "fused jcc";
12361 break;
12362 case align_branch_jmp:
12363 branch = "jmp";
12364 break;
12365 case align_branch_call:
12366 branch = "call";
12367 break;
12368 case align_branch_indirect:
12369 branch = "indiret branch";
12370 break;
12371 case align_branch_ret:
12372 branch = "ret";
12373 break;
12374 default:
12375 break;
12376 }
12377
12378 fprintf (stdout, msg,
12379 fragP->fr_file, fragP->fr_line, size, prefix,
12380 (long long) fragP->fr_address, branch,
12381 1 << align_branch_power);
12382 }
12383 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12384 memset (fragP->fr_opcode,
12385 fragP->tc_frag_data.default_prefix, size);
12386 else
12387 i386_generate_nops (fragP, (char *) fragP->fr_opcode,
12388 size, 0);
12389 fragP->fr_fix += size;
12390 }
12391 return;
12392 }
12393
252b5132
RH
12394 opcode = (unsigned char *) fragP->fr_opcode;
12395
47926f60 12396 /* Address we want to reach in file space. */
252b5132 12397 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
252b5132 12398
47926f60 12399 /* Address opcode resides at in file space. */
252b5132
RH
12400 opcode_address = fragP->fr_address + fragP->fr_fix;
12401
47926f60 12402 /* Displacement from opcode start to fill into instruction. */
252b5132
RH
12403 displacement_from_opcode_start = target_address - opcode_address;
12404
fddf5b5b 12405 if ((fragP->fr_subtype & BIG) == 0)
252b5132 12406 {
47926f60
KH
12407 /* Don't have to change opcode. */
12408 extension = 1; /* 1 opcode + 1 displacement */
252b5132 12409 where_to_put_displacement = &opcode[1];
fddf5b5b
AM
12410 }
12411 else
12412 {
12413 if (no_cond_jump_promotion
12414 && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
4eed87de
AM
12415 as_warn_where (fragP->fr_file, fragP->fr_line,
12416 _("long jump required"));
252b5132 12417
fddf5b5b
AM
12418 switch (fragP->fr_subtype)
12419 {
12420 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
12421 extension = 4; /* 1 opcode + 4 displacement */
12422 opcode[0] = 0xe9;
12423 where_to_put_displacement = &opcode[1];
12424 break;
252b5132 12425
fddf5b5b
AM
12426 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
12427 extension = 2; /* 1 opcode + 2 displacement */
12428 opcode[0] = 0xe9;
12429 where_to_put_displacement = &opcode[1];
12430 break;
252b5132 12431
fddf5b5b
AM
12432 case ENCODE_RELAX_STATE (COND_JUMP, BIG):
12433 case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
12434 extension = 5; /* 2 opcode + 4 displacement */
12435 opcode[1] = opcode[0] + 0x10;
12436 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12437 where_to_put_displacement = &opcode[2];
12438 break;
252b5132 12439
fddf5b5b
AM
12440 case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
12441 extension = 3; /* 2 opcode + 2 displacement */
12442 opcode[1] = opcode[0] + 0x10;
12443 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12444 where_to_put_displacement = &opcode[2];
12445 break;
252b5132 12446
fddf5b5b
AM
12447 case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
12448 extension = 4;
12449 opcode[0] ^= 1;
12450 opcode[1] = 3;
12451 opcode[2] = 0xe9;
12452 where_to_put_displacement = &opcode[3];
12453 break;
12454
12455 default:
12456 BAD_CASE (fragP->fr_subtype);
12457 break;
12458 }
252b5132 12459 }
fddf5b5b 12460
7b81dfbb
AJ
12461 /* If size if less then four we are sure that the operand fits,
12462 but if it's 4, then it could be that the displacement is larger
12463 then -/+ 2GB. */
12464 if (DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype) == 4
12465 && object_64bit
12466 && ((addressT) (displacement_from_opcode_start - extension
4eed87de
AM
12467 + ((addressT) 1 << 31))
12468 > (((addressT) 2 << 31) - 1)))
7b81dfbb
AJ
12469 {
12470 as_bad_where (fragP->fr_file, fragP->fr_line,
12471 _("jump target out of range"));
12472 /* Make us emit 0. */
12473 displacement_from_opcode_start = extension;
12474 }
47926f60 12475 /* Now put displacement after opcode. */
252b5132
RH
12476 md_number_to_chars ((char *) where_to_put_displacement,
12477 (valueT) (displacement_from_opcode_start - extension),
fddf5b5b 12478 DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
252b5132
RH
12479 fragP->fr_fix += extension;
12480}
12481\f
7016a5d5 12482/* Apply a fixup (fixP) to segment data, once it has been determined
252b5132
RH
12483 by our caller that we have all the info we need to fix it up.
12484
7016a5d5
TG
12485 Parameter valP is the pointer to the value of the bits.
12486
252b5132
RH
12487 On the 386, immediates, displacements, and data pointers are all in
12488 the same (little-endian) format, so we don't need to care about which
12489 we are handling. */
12490
94f592af 12491void
7016a5d5 12492md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 12493{
94f592af 12494 char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
c6682705 12495 valueT value = *valP;
252b5132 12496
f86103b7 12497#if !defined (TE_Mach)
93382f6d
AM
12498 if (fixP->fx_pcrel)
12499 {
12500 switch (fixP->fx_r_type)
12501 {
5865bb77
ILT
12502 default:
12503 break;
12504
d6ab8113
JB
12505 case BFD_RELOC_64:
12506 fixP->fx_r_type = BFD_RELOC_64_PCREL;
12507 break;
93382f6d 12508 case BFD_RELOC_32:
ae8887b5 12509 case BFD_RELOC_X86_64_32S:
93382f6d
AM
12510 fixP->fx_r_type = BFD_RELOC_32_PCREL;
12511 break;
12512 case BFD_RELOC_16:
12513 fixP->fx_r_type = BFD_RELOC_16_PCREL;
12514 break;
12515 case BFD_RELOC_8:
12516 fixP->fx_r_type = BFD_RELOC_8_PCREL;
12517 break;
12518 }
12519 }
252b5132 12520
a161fe53 12521 if (fixP->fx_addsy != NULL
31312f95 12522 && (fixP->fx_r_type == BFD_RELOC_32_PCREL
d6ab8113 12523 || fixP->fx_r_type == BFD_RELOC_64_PCREL
31312f95 12524 || fixP->fx_r_type == BFD_RELOC_16_PCREL
d258b828 12525 || fixP->fx_r_type == BFD_RELOC_8_PCREL)
31312f95 12526 && !use_rela_relocations)
252b5132 12527 {
31312f95
AM
12528 /* This is a hack. There should be a better way to handle this.
12529 This covers for the fact that bfd_install_relocation will
12530 subtract the current location (for partial_inplace, PC relative
12531 relocations); see more below. */
252b5132 12532#ifndef OBJ_AOUT
718ddfc0 12533 if (IS_ELF
252b5132
RH
12534#ifdef TE_PE
12535 || OUTPUT_FLAVOR == bfd_target_coff_flavour
12536#endif
12537 )
12538 value += fixP->fx_where + fixP->fx_frag->fr_address;
12539#endif
12540#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12541 if (IS_ELF)
252b5132 12542 {
6539b54b 12543 segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
2f66722d 12544
6539b54b 12545 if ((sym_seg == seg
2f66722d 12546 || (symbol_section_p (fixP->fx_addsy)
6539b54b 12547 && sym_seg != absolute_section))
af65af87 12548 && !generic_force_reloc (fixP))
2f66722d
AM
12549 {
12550 /* Yes, we add the values in twice. This is because
6539b54b
AM
12551 bfd_install_relocation subtracts them out again. I think
12552 bfd_install_relocation is broken, but I don't dare change
2f66722d
AM
12553 it. FIXME. */
12554 value += fixP->fx_where + fixP->fx_frag->fr_address;
12555 }
252b5132
RH
12556 }
12557#endif
12558#if defined (OBJ_COFF) && defined (TE_PE)
977cdf5a
NC
12559 /* For some reason, the PE format does not store a
12560 section address offset for a PC relative symbol. */
12561 if (S_GET_SEGMENT (fixP->fx_addsy) != seg
7be1c489 12562 || S_IS_WEAK (fixP->fx_addsy))
252b5132
RH
12563 value += md_pcrel_from (fixP);
12564#endif
12565 }
fbeb56a4 12566#if defined (OBJ_COFF) && defined (TE_PE)
f01c1a09
NC
12567 if (fixP->fx_addsy != NULL
12568 && S_IS_WEAK (fixP->fx_addsy)
12569 /* PR 16858: Do not modify weak function references. */
12570 && ! fixP->fx_pcrel)
fbeb56a4 12571 {
296a8689
NC
12572#if !defined (TE_PEP)
12573 /* For x86 PE weak function symbols are neither PC-relative
12574 nor do they set S_IS_FUNCTION. So the only reliable way
12575 to detect them is to check the flags of their containing
12576 section. */
12577 if (S_GET_SEGMENT (fixP->fx_addsy) != NULL
12578 && S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_CODE)
12579 ;
12580 else
12581#endif
fbeb56a4
DK
12582 value -= S_GET_VALUE (fixP->fx_addsy);
12583 }
12584#endif
252b5132
RH
12585
12586 /* Fix a few things - the dynamic linker expects certain values here,
0234cb7c 12587 and we must not disappoint it. */
252b5132 12588#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12589 if (IS_ELF && fixP->fx_addsy)
47926f60
KH
12590 switch (fixP->fx_r_type)
12591 {
12592 case BFD_RELOC_386_PLT32:
3e73aa7c 12593 case BFD_RELOC_X86_64_PLT32:
b9519cfe
L
12594 /* Make the jump instruction point to the address of the operand.
12595 At runtime we merely add the offset to the actual PLT entry.
12596 NB: Subtract the offset size only for jump instructions. */
12597 if (fixP->fx_pcrel)
12598 value = -4;
47926f60 12599 break;
31312f95 12600
13ae64f3
JJ
12601 case BFD_RELOC_386_TLS_GD:
12602 case BFD_RELOC_386_TLS_LDM:
13ae64f3 12603 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
12604 case BFD_RELOC_386_TLS_IE:
12605 case BFD_RELOC_386_TLS_GOTIE:
67a4f2b7 12606 case BFD_RELOC_386_TLS_GOTDESC:
bffbf940
JJ
12607 case BFD_RELOC_X86_64_TLSGD:
12608 case BFD_RELOC_X86_64_TLSLD:
12609 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7 12610 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
00f7efb6
JJ
12611 value = 0; /* Fully resolved at runtime. No addend. */
12612 /* Fallthrough */
12613 case BFD_RELOC_386_TLS_LE:
12614 case BFD_RELOC_386_TLS_LDO_32:
12615 case BFD_RELOC_386_TLS_LE_32:
12616 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 12617 case BFD_RELOC_X86_64_DTPOFF64:
00f7efb6 12618 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113 12619 case BFD_RELOC_X86_64_TPOFF64:
00f7efb6
JJ
12620 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12621 break;
12622
67a4f2b7
AO
12623 case BFD_RELOC_386_TLS_DESC_CALL:
12624 case BFD_RELOC_X86_64_TLSDESC_CALL:
12625 value = 0; /* Fully resolved at runtime. No addend. */
12626 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12627 fixP->fx_done = 0;
12628 return;
12629
47926f60
KH
12630 case BFD_RELOC_VTABLE_INHERIT:
12631 case BFD_RELOC_VTABLE_ENTRY:
12632 fixP->fx_done = 0;
94f592af 12633 return;
47926f60
KH
12634
12635 default:
12636 break;
12637 }
12638#endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) */
a442cac5
JB
12639
12640 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
12641 if (!object_64bit)
12642 value = extend_to_32bit_address (value);
12643
c6682705 12644 *valP = value;
f86103b7 12645#endif /* !defined (TE_Mach) */
3e73aa7c 12646
3e73aa7c 12647 /* Are we finished with this relocation now? */
c6682705 12648 if (fixP->fx_addsy == NULL)
b8188555
JB
12649 {
12650 fixP->fx_done = 1;
12651 switch (fixP->fx_r_type)
12652 {
12653 case BFD_RELOC_X86_64_32S:
12654 fixP->fx_signed = 1;
12655 break;
12656
12657 default:
12658 break;
12659 }
12660 }
fbeb56a4
DK
12661#if defined (OBJ_COFF) && defined (TE_PE)
12662 else if (fixP->fx_addsy != NULL && S_IS_WEAK (fixP->fx_addsy))
12663 {
12664 fixP->fx_done = 0;
12665 /* Remember value for tc_gen_reloc. */
12666 fixP->fx_addnumber = value;
12667 /* Clear out the frag for now. */
12668 value = 0;
12669 }
12670#endif
3e73aa7c
JH
12671 else if (use_rela_relocations)
12672 {
12673 fixP->fx_no_overflow = 1;
062cd5e7
AS
12674 /* Remember value for tc_gen_reloc. */
12675 fixP->fx_addnumber = value;
3e73aa7c
JH
12676 value = 0;
12677 }
f86103b7 12678
94f592af 12679 md_number_to_chars (p, value, fixP->fx_size);
252b5132 12680}
252b5132 12681\f
6d4af3c2 12682const char *
499ac353 12683md_atof (int type, char *litP, int *sizeP)
252b5132 12684{
499ac353
NC
12685 /* This outputs the LITTLENUMs in REVERSE order;
12686 in accord with the bigendian 386. */
5b7c81bd 12687 return ieee_md_atof (type, litP, sizeP, false);
252b5132
RH
12688}
12689\f
2d545b82 12690static char output_invalid_buf[sizeof (unsigned char) * 2 + 6];
252b5132 12691
252b5132 12692static char *
e3bb37b5 12693output_invalid (int c)
252b5132 12694{
3882b010 12695 if (ISPRINT (c))
f9f21a03
L
12696 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
12697 "'%c'", c);
252b5132 12698 else
f9f21a03 12699 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
2d545b82 12700 "(0x%x)", (unsigned char) c);
252b5132
RH
12701 return output_invalid_buf;
12702}
12703
8a6fb3f9
JB
12704/* Verify that @r can be used in the current context. */
12705
5b7c81bd 12706static bool check_register (const reg_entry *r)
8a6fb3f9
JB
12707{
12708 if (allow_pseudo_reg)
5b7c81bd 12709 return true;
8a6fb3f9
JB
12710
12711 if (operand_type_all_zero (&r->reg_type))
5b7c81bd 12712 return false;
8a6fb3f9
JB
12713
12714 if ((r->reg_type.bitfield.dword
12715 || (r->reg_type.bitfield.class == SReg && r->reg_num > 3)
12716 || r->reg_type.bitfield.class == RegCR
22e00a3f 12717 || r->reg_type.bitfield.class == RegDR)
8a6fb3f9 12718 && !cpu_arch_flags.bitfield.cpui386)
5b7c81bd 12719 return false;
8a6fb3f9 12720
22e00a3f
JB
12721 if (r->reg_type.bitfield.class == RegTR
12722 && (flag_code == CODE_64BIT
12723 || !cpu_arch_flags.bitfield.cpui386
12724 || cpu_arch_isa_flags.bitfield.cpui586
12725 || cpu_arch_isa_flags.bitfield.cpui686))
5b7c81bd 12726 return false;
22e00a3f 12727
8a6fb3f9 12728 if (r->reg_type.bitfield.class == RegMMX && !cpu_arch_flags.bitfield.cpummx)
5b7c81bd 12729 return false;
8a6fb3f9
JB
12730
12731 if (!cpu_arch_flags.bitfield.cpuavx512f)
12732 {
12733 if (r->reg_type.bitfield.zmmword
12734 || r->reg_type.bitfield.class == RegMask)
5b7c81bd 12735 return false;
8a6fb3f9
JB
12736
12737 if (!cpu_arch_flags.bitfield.cpuavx)
12738 {
12739 if (r->reg_type.bitfield.ymmword)
5b7c81bd 12740 return false;
8a6fb3f9
JB
12741
12742 if (!cpu_arch_flags.bitfield.cpusse && r->reg_type.bitfield.xmmword)
5b7c81bd 12743 return false;
8a6fb3f9
JB
12744 }
12745 }
12746
260cd341
LC
12747 if (r->reg_type.bitfield.tmmword
12748 && (!cpu_arch_flags.bitfield.cpuamx_tile
12749 || flag_code != CODE_64BIT))
5b7c81bd 12750 return false;
260cd341 12751
8a6fb3f9 12752 if (r->reg_type.bitfield.class == RegBND && !cpu_arch_flags.bitfield.cpumpx)
5b7c81bd 12753 return false;
8a6fb3f9
JB
12754
12755 /* Don't allow fake index register unless allow_index_reg isn't 0. */
12756 if (!allow_index_reg && r->reg_num == RegIZ)
5b7c81bd 12757 return false;
8a6fb3f9
JB
12758
12759 /* Upper 16 vector registers are only available with VREX in 64bit
12760 mode, and require EVEX encoding. */
12761 if (r->reg_flags & RegVRex)
12762 {
12763 if (!cpu_arch_flags.bitfield.cpuavx512f
12764 || flag_code != CODE_64BIT)
5b7c81bd 12765 return false;
8a6fb3f9 12766
da4977e0
JB
12767 if (i.vec_encoding == vex_encoding_default)
12768 i.vec_encoding = vex_encoding_evex;
12769 else if (i.vec_encoding != vex_encoding_evex)
12770 i.vec_encoding = vex_encoding_error;
8a6fb3f9
JB
12771 }
12772
12773 if (((r->reg_flags & (RegRex64 | RegRex)) || r->reg_type.bitfield.qword)
12774 && (!cpu_arch_flags.bitfield.cpulm || r->reg_type.bitfield.class != RegCR)
12775 && flag_code != CODE_64BIT)
5b7c81bd 12776 return false;
8a6fb3f9
JB
12777
12778 if (r->reg_type.bitfield.class == SReg && r->reg_num == RegFlat
12779 && !intel_syntax)
5b7c81bd 12780 return false;
8a6fb3f9 12781
5b7c81bd 12782 return true;
8a6fb3f9
JB
12783}
12784
af6bdddf 12785/* REG_STRING starts *before* REGISTER_PREFIX. */
252b5132
RH
12786
12787static const reg_entry *
4d1bb795 12788parse_real_register (char *reg_string, char **end_op)
252b5132 12789{
af6bdddf
AM
12790 char *s = reg_string;
12791 char *p;
252b5132
RH
12792 char reg_name_given[MAX_REG_NAME_SIZE + 1];
12793 const reg_entry *r;
12794
12795 /* Skip possible REGISTER_PREFIX and possible whitespace. */
12796 if (*s == REGISTER_PREFIX)
12797 ++s;
12798
12799 if (is_space_char (*s))
12800 ++s;
12801
12802 p = reg_name_given;
af6bdddf 12803 while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
252b5132
RH
12804 {
12805 if (p >= reg_name_given + MAX_REG_NAME_SIZE)
af6bdddf
AM
12806 return (const reg_entry *) NULL;
12807 s++;
252b5132
RH
12808 }
12809
6588847e
DN
12810 /* For naked regs, make sure that we are not dealing with an identifier.
12811 This prevents confusing an identifier like `eax_var' with register
12812 `eax'. */
12813 if (allow_naked_reg && identifier_chars[(unsigned char) *s])
12814 return (const reg_entry *) NULL;
12815
af6bdddf 12816 *end_op = s;
252b5132 12817
629310ab 12818 r = (const reg_entry *) str_hash_find (reg_hash, reg_name_given);
252b5132 12819
5f47d35b 12820 /* Handle floating point regs, allowing spaces in the (i) part. */
6288d05f 12821 if (r == reg_st0)
5f47d35b 12822 {
0e0eea78
JB
12823 if (!cpu_arch_flags.bitfield.cpu8087
12824 && !cpu_arch_flags.bitfield.cpu287
af32b722
JB
12825 && !cpu_arch_flags.bitfield.cpu387
12826 && !allow_pseudo_reg)
0e0eea78
JB
12827 return (const reg_entry *) NULL;
12828
5f47d35b
AM
12829 if (is_space_char (*s))
12830 ++s;
12831 if (*s == '(')
12832 {
af6bdddf 12833 ++s;
5f47d35b
AM
12834 if (is_space_char (*s))
12835 ++s;
12836 if (*s >= '0' && *s <= '7')
12837 {
db557034 12838 int fpr = *s - '0';
af6bdddf 12839 ++s;
5f47d35b
AM
12840 if (is_space_char (*s))
12841 ++s;
12842 if (*s == ')')
12843 {
12844 *end_op = s + 1;
6288d05f 12845 know (r[fpr].reg_num == fpr);
db557034 12846 return r + fpr;
5f47d35b 12847 }
5f47d35b 12848 }
47926f60 12849 /* We have "%st(" then garbage. */
5f47d35b
AM
12850 return (const reg_entry *) NULL;
12851 }
12852 }
12853
8a6fb3f9 12854 return r && check_register (r) ? r : NULL;
252b5132 12855}
4d1bb795
JB
12856
12857/* REG_STRING starts *before* REGISTER_PREFIX. */
12858
12859static const reg_entry *
12860parse_register (char *reg_string, char **end_op)
12861{
12862 const reg_entry *r;
12863
12864 if (*reg_string == REGISTER_PREFIX || allow_naked_reg)
12865 r = parse_real_register (reg_string, end_op);
12866 else
12867 r = NULL;
12868 if (!r)
12869 {
12870 char *save = input_line_pointer;
12871 char c;
12872 symbolS *symbolP;
12873
12874 input_line_pointer = reg_string;
d02603dc 12875 c = get_symbol_name (&reg_string);
4d1bb795
JB
12876 symbolP = symbol_find (reg_string);
12877 if (symbolP && S_GET_SEGMENT (symbolP) == reg_section)
12878 {
12879 const expressionS *e = symbol_get_value_expression (symbolP);
12880
0398aac5 12881 know (e->X_op == O_register);
4eed87de 12882 know (e->X_add_number >= 0
c3fe08fa 12883 && (valueT) e->X_add_number < i386_regtab_size);
4d1bb795 12884 r = i386_regtab + e->X_add_number;
8a6fb3f9
JB
12885 if (!check_register (r))
12886 {
12887 as_bad (_("register '%s%s' cannot be used here"),
12888 register_prefix, r->reg_name);
12889 r = &bad_reg;
12890 }
4d1bb795
JB
12891 *end_op = input_line_pointer;
12892 }
12893 *input_line_pointer = c;
12894 input_line_pointer = save;
12895 }
12896 return r;
12897}
12898
12899int
12900i386_parse_name (char *name, expressionS *e, char *nextcharP)
12901{
12902 const reg_entry *r;
12903 char *end = input_line_pointer;
12904
12905 *end = *nextcharP;
12906 r = parse_register (name, &input_line_pointer);
12907 if (r && end <= input_line_pointer)
12908 {
12909 *nextcharP = *input_line_pointer;
12910 *input_line_pointer = 0;
8a6fb3f9
JB
12911 if (r != &bad_reg)
12912 {
12913 e->X_op = O_register;
12914 e->X_add_number = r - i386_regtab;
12915 }
12916 else
12917 e->X_op = O_illegal;
4d1bb795
JB
12918 return 1;
12919 }
12920 input_line_pointer = end;
12921 *end = 0;
ee86248c 12922 return intel_syntax ? i386_intel_parse_name (name, e) : 0;
4d1bb795
JB
12923}
12924
12925void
12926md_operand (expressionS *e)
12927{
ee86248c
JB
12928 char *end;
12929 const reg_entry *r;
4d1bb795 12930
ee86248c
JB
12931 switch (*input_line_pointer)
12932 {
12933 case REGISTER_PREFIX:
12934 r = parse_real_register (input_line_pointer, &end);
4d1bb795
JB
12935 if (r)
12936 {
12937 e->X_op = O_register;
12938 e->X_add_number = r - i386_regtab;
12939 input_line_pointer = end;
12940 }
ee86248c
JB
12941 break;
12942
12943 case '[':
9c2799c2 12944 gas_assert (intel_syntax);
ee86248c
JB
12945 end = input_line_pointer++;
12946 expression (e);
12947 if (*input_line_pointer == ']')
12948 {
12949 ++input_line_pointer;
12950 e->X_op_symbol = make_expr_symbol (e);
12951 e->X_add_symbol = NULL;
12952 e->X_add_number = 0;
12953 e->X_op = O_index;
12954 }
12955 else
12956 {
12957 e->X_op = O_absent;
12958 input_line_pointer = end;
12959 }
12960 break;
4d1bb795
JB
12961 }
12962}
12963
252b5132 12964\f
4cc782b5 12965#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
b6f8c7c4 12966const char *md_shortopts = "kVQ:sqnO::";
252b5132 12967#else
b6f8c7c4 12968const char *md_shortopts = "qnO::";
252b5132 12969#endif
6e0b89ee 12970
3e73aa7c 12971#define OPTION_32 (OPTION_MD_BASE + 0)
b3b91714
AM
12972#define OPTION_64 (OPTION_MD_BASE + 1)
12973#define OPTION_DIVIDE (OPTION_MD_BASE + 2)
9103f4f4
L
12974#define OPTION_MARCH (OPTION_MD_BASE + 3)
12975#define OPTION_MTUNE (OPTION_MD_BASE + 4)
1efbbeb4
L
12976#define OPTION_MMNEMONIC (OPTION_MD_BASE + 5)
12977#define OPTION_MSYNTAX (OPTION_MD_BASE + 6)
12978#define OPTION_MINDEX_REG (OPTION_MD_BASE + 7)
12979#define OPTION_MNAKED_REG (OPTION_MD_BASE + 8)
bd5dea88 12980#define OPTION_MRELAX_RELOCATIONS (OPTION_MD_BASE + 9)
c0f3af97 12981#define OPTION_MSSE2AVX (OPTION_MD_BASE + 10)
daf50ae7 12982#define OPTION_MSSE_CHECK (OPTION_MD_BASE + 11)
7bab8ab5
JB
12983#define OPTION_MOPERAND_CHECK (OPTION_MD_BASE + 12)
12984#define OPTION_MAVXSCALAR (OPTION_MD_BASE + 13)
12985#define OPTION_X32 (OPTION_MD_BASE + 14)
7e8b059b 12986#define OPTION_MADD_BND_PREFIX (OPTION_MD_BASE + 15)
43234a1e
L
12987#define OPTION_MEVEXLIG (OPTION_MD_BASE + 16)
12988#define OPTION_MEVEXWIG (OPTION_MD_BASE + 17)
167ad85b 12989#define OPTION_MBIG_OBJ (OPTION_MD_BASE + 18)
d1982f93 12990#define OPTION_MOMIT_LOCK_PREFIX (OPTION_MD_BASE + 19)
d3d3c6db 12991#define OPTION_MEVEXRCIG (OPTION_MD_BASE + 20)
8dcea932 12992#define OPTION_MSHARED (OPTION_MD_BASE + 21)
5db04b09
L
12993#define OPTION_MAMD64 (OPTION_MD_BASE + 22)
12994#define OPTION_MINTEL64 (OPTION_MD_BASE + 23)
e4e00185 12995#define OPTION_MFENCE_AS_LOCK_ADD (OPTION_MD_BASE + 24)
b4a3a7b4 12996#define OPTION_X86_USED_NOTE (OPTION_MD_BASE + 25)
03751133 12997#define OPTION_MVEXWIG (OPTION_MD_BASE + 26)
e379e5f3
L
12998#define OPTION_MALIGN_BRANCH_BOUNDARY (OPTION_MD_BASE + 27)
12999#define OPTION_MALIGN_BRANCH_PREFIX_SIZE (OPTION_MD_BASE + 28)
13000#define OPTION_MALIGN_BRANCH (OPTION_MD_BASE + 29)
76cf450b 13001#define OPTION_MBRANCHES_WITH_32B_BOUNDARIES (OPTION_MD_BASE + 30)
ae531041
L
13002#define OPTION_MLFENCE_AFTER_LOAD (OPTION_MD_BASE + 31)
13003#define OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH (OPTION_MD_BASE + 32)
13004#define OPTION_MLFENCE_BEFORE_RET (OPTION_MD_BASE + 33)
b3b91714 13005
99ad8390
NC
13006struct option md_longopts[] =
13007{
3e73aa7c 13008 {"32", no_argument, NULL, OPTION_32},
321098a5 13009#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 13010 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c 13011 {"64", no_argument, NULL, OPTION_64},
351f65ca
L
13012#endif
13013#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 13014 {"x32", no_argument, NULL, OPTION_X32},
8dcea932 13015 {"mshared", no_argument, NULL, OPTION_MSHARED},
b4a3a7b4 13016 {"mx86-used-note", required_argument, NULL, OPTION_X86_USED_NOTE},
6e0b89ee 13017#endif
b3b91714 13018 {"divide", no_argument, NULL, OPTION_DIVIDE},
9103f4f4
L
13019 {"march", required_argument, NULL, OPTION_MARCH},
13020 {"mtune", required_argument, NULL, OPTION_MTUNE},
1efbbeb4
L
13021 {"mmnemonic", required_argument, NULL, OPTION_MMNEMONIC},
13022 {"msyntax", required_argument, NULL, OPTION_MSYNTAX},
13023 {"mindex-reg", no_argument, NULL, OPTION_MINDEX_REG},
13024 {"mnaked-reg", no_argument, NULL, OPTION_MNAKED_REG},
c0f3af97 13025 {"msse2avx", no_argument, NULL, OPTION_MSSE2AVX},
daf50ae7 13026 {"msse-check", required_argument, NULL, OPTION_MSSE_CHECK},
7bab8ab5 13027 {"moperand-check", required_argument, NULL, OPTION_MOPERAND_CHECK},
539f890d 13028 {"mavxscalar", required_argument, NULL, OPTION_MAVXSCALAR},
03751133 13029 {"mvexwig", required_argument, NULL, OPTION_MVEXWIG},
7e8b059b 13030 {"madd-bnd-prefix", no_argument, NULL, OPTION_MADD_BND_PREFIX},
43234a1e
L
13031 {"mevexlig", required_argument, NULL, OPTION_MEVEXLIG},
13032 {"mevexwig", required_argument, NULL, OPTION_MEVEXWIG},
167ad85b
TG
13033# if defined (TE_PE) || defined (TE_PEP)
13034 {"mbig-obj", no_argument, NULL, OPTION_MBIG_OBJ},
13035#endif
d1982f93 13036 {"momit-lock-prefix", required_argument, NULL, OPTION_MOMIT_LOCK_PREFIX},
e4e00185 13037 {"mfence-as-lock-add", required_argument, NULL, OPTION_MFENCE_AS_LOCK_ADD},
0cb4071e 13038 {"mrelax-relocations", required_argument, NULL, OPTION_MRELAX_RELOCATIONS},
d3d3c6db 13039 {"mevexrcig", required_argument, NULL, OPTION_MEVEXRCIG},
e379e5f3
L
13040 {"malign-branch-boundary", required_argument, NULL, OPTION_MALIGN_BRANCH_BOUNDARY},
13041 {"malign-branch-prefix-size", required_argument, NULL, OPTION_MALIGN_BRANCH_PREFIX_SIZE},
13042 {"malign-branch", required_argument, NULL, OPTION_MALIGN_BRANCH},
76cf450b 13043 {"mbranches-within-32B-boundaries", no_argument, NULL, OPTION_MBRANCHES_WITH_32B_BOUNDARIES},
ae531041
L
13044 {"mlfence-after-load", required_argument, NULL, OPTION_MLFENCE_AFTER_LOAD},
13045 {"mlfence-before-indirect-branch", required_argument, NULL,
13046 OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH},
13047 {"mlfence-before-ret", required_argument, NULL, OPTION_MLFENCE_BEFORE_RET},
5db04b09
L
13048 {"mamd64", no_argument, NULL, OPTION_MAMD64},
13049 {"mintel64", no_argument, NULL, OPTION_MINTEL64},
252b5132
RH
13050 {NULL, no_argument, NULL, 0}
13051};
13052size_t md_longopts_size = sizeof (md_longopts);
13053
13054int
17b9d67d 13055md_parse_option (int c, const char *arg)
252b5132 13056{
91d6fa6a 13057 unsigned int j;
e379e5f3 13058 char *arch, *next, *saved, *type;
9103f4f4 13059
252b5132
RH
13060 switch (c)
13061 {
12b55ccc
L
13062 case 'n':
13063 optimize_align_code = 0;
13064 break;
13065
a38cf1db
AM
13066 case 'q':
13067 quiet_warnings = 1;
252b5132
RH
13068 break;
13069
13070#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
a38cf1db
AM
13071 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
13072 should be emitted or not. FIXME: Not implemented. */
13073 case 'Q':
d4693039
JB
13074 if ((arg[0] != 'y' && arg[0] != 'n') || arg[1])
13075 return 0;
252b5132
RH
13076 break;
13077
13078 /* -V: SVR4 argument to print version ID. */
13079 case 'V':
13080 print_version_id ();
13081 break;
13082
a38cf1db
AM
13083 /* -k: Ignore for FreeBSD compatibility. */
13084 case 'k':
252b5132 13085 break;
4cc782b5
ILT
13086
13087 case 's':
13088 /* -s: On i386 Solaris, this tells the native assembler to use
29b0f896 13089 .stab instead of .stab.excl. We always use .stab anyhow. */
4cc782b5 13090 break;
8dcea932
L
13091
13092 case OPTION_MSHARED:
13093 shared = 1;
13094 break;
b4a3a7b4
L
13095
13096 case OPTION_X86_USED_NOTE:
13097 if (strcasecmp (arg, "yes") == 0)
13098 x86_used_note = 1;
13099 else if (strcasecmp (arg, "no") == 0)
13100 x86_used_note = 0;
13101 else
13102 as_fatal (_("invalid -mx86-used-note= option: `%s'"), arg);
13103 break;
13104
13105
99ad8390 13106#endif
321098a5 13107#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 13108 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c
JH
13109 case OPTION_64:
13110 {
13111 const char **list, **l;
13112
3e73aa7c
JH
13113 list = bfd_target_list ();
13114 for (l = list; *l != NULL; l++)
08dedd66 13115 if (startswith (*l, "elf64-x86-64")
99ad8390
NC
13116 || strcmp (*l, "coff-x86-64") == 0
13117 || strcmp (*l, "pe-x86-64") == 0
d382c579
TG
13118 || strcmp (*l, "pei-x86-64") == 0
13119 || strcmp (*l, "mach-o-x86-64") == 0)
6e0b89ee
AM
13120 {
13121 default_arch = "x86_64";
13122 break;
13123 }
3e73aa7c 13124 if (*l == NULL)
2b5d6a91 13125 as_fatal (_("no compiled in support for x86_64"));
3e73aa7c
JH
13126 free (list);
13127 }
13128 break;
13129#endif
252b5132 13130
351f65ca 13131#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 13132 case OPTION_X32:
351f65ca
L
13133 if (IS_ELF)
13134 {
13135 const char **list, **l;
13136
13137 list = bfd_target_list ();
13138 for (l = list; *l != NULL; l++)
08dedd66 13139 if (startswith (*l, "elf32-x86-64"))
351f65ca
L
13140 {
13141 default_arch = "x86_64:32";
13142 break;
13143 }
13144 if (*l == NULL)
2b5d6a91 13145 as_fatal (_("no compiled in support for 32bit x86_64"));
351f65ca
L
13146 free (list);
13147 }
13148 else
13149 as_fatal (_("32bit x86_64 is only supported for ELF"));
13150 break;
13151#endif
13152
6e0b89ee
AM
13153 case OPTION_32:
13154 default_arch = "i386";
13155 break;
13156
b3b91714
AM
13157 case OPTION_DIVIDE:
13158#ifdef SVR4_COMMENT_CHARS
13159 {
13160 char *n, *t;
13161 const char *s;
13162
add39d23 13163 n = XNEWVEC (char, strlen (i386_comment_chars) + 1);
b3b91714
AM
13164 t = n;
13165 for (s = i386_comment_chars; *s != '\0'; s++)
13166 if (*s != '/')
13167 *t++ = *s;
13168 *t = '\0';
13169 i386_comment_chars = n;
13170 }
13171#endif
13172 break;
13173
9103f4f4 13174 case OPTION_MARCH:
293f5f65
L
13175 saved = xstrdup (arg);
13176 arch = saved;
13177 /* Allow -march=+nosse. */
13178 if (*arch == '+')
13179 arch++;
6305a203 13180 do
9103f4f4 13181 {
6305a203 13182 if (*arch == '.')
2b5d6a91 13183 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
13184 next = strchr (arch, '+');
13185 if (next)
13186 *next++ = '\0';
91d6fa6a 13187 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 13188 {
91d6fa6a 13189 if (strcmp (arch, cpu_arch [j].name) == 0)
ccc9c027 13190 {
6305a203 13191 /* Processor. */
1ded5609
JB
13192 if (! cpu_arch[j].flags.bitfield.cpui386)
13193 continue;
13194
91d6fa6a 13195 cpu_arch_name = cpu_arch[j].name;
6305a203 13196 cpu_sub_arch_name = NULL;
91d6fa6a
NC
13197 cpu_arch_flags = cpu_arch[j].flags;
13198 cpu_arch_isa = cpu_arch[j].type;
13199 cpu_arch_isa_flags = cpu_arch[j].flags;
6305a203
L
13200 if (!cpu_arch_tune_set)
13201 {
13202 cpu_arch_tune = cpu_arch_isa;
13203 cpu_arch_tune_flags = cpu_arch_isa_flags;
13204 }
13205 break;
13206 }
91d6fa6a
NC
13207 else if (*cpu_arch [j].name == '.'
13208 && strcmp (arch, cpu_arch [j].name + 1) == 0)
6305a203 13209 {
33eaf5de 13210 /* ISA extension. */
6305a203 13211 i386_cpu_flags flags;
309d3373 13212
293f5f65
L
13213 flags = cpu_flags_or (cpu_arch_flags,
13214 cpu_arch[j].flags);
81486035 13215
5b64d091 13216 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
6305a203
L
13217 {
13218 if (cpu_sub_arch_name)
13219 {
13220 char *name = cpu_sub_arch_name;
13221 cpu_sub_arch_name = concat (name,
91d6fa6a 13222 cpu_arch[j].name,
1bf57e9f 13223 (const char *) NULL);
6305a203
L
13224 free (name);
13225 }
13226 else
91d6fa6a 13227 cpu_sub_arch_name = xstrdup (cpu_arch[j].name);
6305a203 13228 cpu_arch_flags = flags;
a586129e 13229 cpu_arch_isa_flags = flags;
6305a203 13230 }
0089dace
L
13231 else
13232 cpu_arch_isa_flags
13233 = cpu_flags_or (cpu_arch_isa_flags,
13234 cpu_arch[j].flags);
6305a203 13235 break;
ccc9c027 13236 }
9103f4f4 13237 }
6305a203 13238
293f5f65
L
13239 if (j >= ARRAY_SIZE (cpu_arch))
13240 {
33eaf5de 13241 /* Disable an ISA extension. */
293f5f65
L
13242 for (j = 0; j < ARRAY_SIZE (cpu_noarch); j++)
13243 if (strcmp (arch, cpu_noarch [j].name) == 0)
13244 {
13245 i386_cpu_flags flags;
13246
13247 flags = cpu_flags_and_not (cpu_arch_flags,
13248 cpu_noarch[j].flags);
13249 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
13250 {
13251 if (cpu_sub_arch_name)
13252 {
13253 char *name = cpu_sub_arch_name;
13254 cpu_sub_arch_name = concat (arch,
13255 (const char *) NULL);
13256 free (name);
13257 }
13258 else
13259 cpu_sub_arch_name = xstrdup (arch);
13260 cpu_arch_flags = flags;
13261 cpu_arch_isa_flags = flags;
13262 }
13263 break;
13264 }
13265
13266 if (j >= ARRAY_SIZE (cpu_noarch))
13267 j = ARRAY_SIZE (cpu_arch);
13268 }
13269
91d6fa6a 13270 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 13271 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
13272
13273 arch = next;
9103f4f4 13274 }
293f5f65
L
13275 while (next != NULL);
13276 free (saved);
9103f4f4
L
13277 break;
13278
13279 case OPTION_MTUNE:
13280 if (*arg == '.')
2b5d6a91 13281 as_fatal (_("invalid -mtune= option: `%s'"), arg);
91d6fa6a 13282 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 13283 {
91d6fa6a 13284 if (strcmp (arg, cpu_arch [j].name) == 0)
9103f4f4 13285 {
ccc9c027 13286 cpu_arch_tune_set = 1;
91d6fa6a
NC
13287 cpu_arch_tune = cpu_arch [j].type;
13288 cpu_arch_tune_flags = cpu_arch[j].flags;
9103f4f4
L
13289 break;
13290 }
13291 }
91d6fa6a 13292 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 13293 as_fatal (_("invalid -mtune= option: `%s'"), arg);
9103f4f4
L
13294 break;
13295
1efbbeb4
L
13296 case OPTION_MMNEMONIC:
13297 if (strcasecmp (arg, "att") == 0)
13298 intel_mnemonic = 0;
13299 else if (strcasecmp (arg, "intel") == 0)
13300 intel_mnemonic = 1;
13301 else
2b5d6a91 13302 as_fatal (_("invalid -mmnemonic= option: `%s'"), arg);
1efbbeb4
L
13303 break;
13304
13305 case OPTION_MSYNTAX:
13306 if (strcasecmp (arg, "att") == 0)
13307 intel_syntax = 0;
13308 else if (strcasecmp (arg, "intel") == 0)
13309 intel_syntax = 1;
13310 else
2b5d6a91 13311 as_fatal (_("invalid -msyntax= option: `%s'"), arg);
1efbbeb4
L
13312 break;
13313
13314 case OPTION_MINDEX_REG:
13315 allow_index_reg = 1;
13316 break;
13317
13318 case OPTION_MNAKED_REG:
13319 allow_naked_reg = 1;
13320 break;
13321
c0f3af97
L
13322 case OPTION_MSSE2AVX:
13323 sse2avx = 1;
13324 break;
13325
daf50ae7
L
13326 case OPTION_MSSE_CHECK:
13327 if (strcasecmp (arg, "error") == 0)
7bab8ab5 13328 sse_check = check_error;
daf50ae7 13329 else if (strcasecmp (arg, "warning") == 0)
7bab8ab5 13330 sse_check = check_warning;
daf50ae7 13331 else if (strcasecmp (arg, "none") == 0)
7bab8ab5 13332 sse_check = check_none;
daf50ae7 13333 else
2b5d6a91 13334 as_fatal (_("invalid -msse-check= option: `%s'"), arg);
daf50ae7
L
13335 break;
13336
7bab8ab5
JB
13337 case OPTION_MOPERAND_CHECK:
13338 if (strcasecmp (arg, "error") == 0)
13339 operand_check = check_error;
13340 else if (strcasecmp (arg, "warning") == 0)
13341 operand_check = check_warning;
13342 else if (strcasecmp (arg, "none") == 0)
13343 operand_check = check_none;
13344 else
13345 as_fatal (_("invalid -moperand-check= option: `%s'"), arg);
13346 break;
13347
539f890d
L
13348 case OPTION_MAVXSCALAR:
13349 if (strcasecmp (arg, "128") == 0)
13350 avxscalar = vex128;
13351 else if (strcasecmp (arg, "256") == 0)
13352 avxscalar = vex256;
13353 else
2b5d6a91 13354 as_fatal (_("invalid -mavxscalar= option: `%s'"), arg);
539f890d
L
13355 break;
13356
03751133
L
13357 case OPTION_MVEXWIG:
13358 if (strcmp (arg, "0") == 0)
40c9c8de 13359 vexwig = vexw0;
03751133 13360 else if (strcmp (arg, "1") == 0)
40c9c8de 13361 vexwig = vexw1;
03751133
L
13362 else
13363 as_fatal (_("invalid -mvexwig= option: `%s'"), arg);
13364 break;
13365
7e8b059b
L
13366 case OPTION_MADD_BND_PREFIX:
13367 add_bnd_prefix = 1;
13368 break;
13369
43234a1e
L
13370 case OPTION_MEVEXLIG:
13371 if (strcmp (arg, "128") == 0)
13372 evexlig = evexl128;
13373 else if (strcmp (arg, "256") == 0)
13374 evexlig = evexl256;
13375 else if (strcmp (arg, "512") == 0)
13376 evexlig = evexl512;
13377 else
13378 as_fatal (_("invalid -mevexlig= option: `%s'"), arg);
13379 break;
13380
d3d3c6db
IT
13381 case OPTION_MEVEXRCIG:
13382 if (strcmp (arg, "rne") == 0)
13383 evexrcig = rne;
13384 else if (strcmp (arg, "rd") == 0)
13385 evexrcig = rd;
13386 else if (strcmp (arg, "ru") == 0)
13387 evexrcig = ru;
13388 else if (strcmp (arg, "rz") == 0)
13389 evexrcig = rz;
13390 else
13391 as_fatal (_("invalid -mevexrcig= option: `%s'"), arg);
13392 break;
13393
43234a1e
L
13394 case OPTION_MEVEXWIG:
13395 if (strcmp (arg, "0") == 0)
13396 evexwig = evexw0;
13397 else if (strcmp (arg, "1") == 0)
13398 evexwig = evexw1;
13399 else
13400 as_fatal (_("invalid -mevexwig= option: `%s'"), arg);
13401 break;
13402
167ad85b
TG
13403# if defined (TE_PE) || defined (TE_PEP)
13404 case OPTION_MBIG_OBJ:
13405 use_big_obj = 1;
13406 break;
13407#endif
13408
d1982f93 13409 case OPTION_MOMIT_LOCK_PREFIX:
d022bddd
IT
13410 if (strcasecmp (arg, "yes") == 0)
13411 omit_lock_prefix = 1;
13412 else if (strcasecmp (arg, "no") == 0)
13413 omit_lock_prefix = 0;
13414 else
13415 as_fatal (_("invalid -momit-lock-prefix= option: `%s'"), arg);
13416 break;
13417
e4e00185
AS
13418 case OPTION_MFENCE_AS_LOCK_ADD:
13419 if (strcasecmp (arg, "yes") == 0)
13420 avoid_fence = 1;
13421 else if (strcasecmp (arg, "no") == 0)
13422 avoid_fence = 0;
13423 else
13424 as_fatal (_("invalid -mfence-as-lock-add= option: `%s'"), arg);
13425 break;
13426
ae531041
L
13427 case OPTION_MLFENCE_AFTER_LOAD:
13428 if (strcasecmp (arg, "yes") == 0)
13429 lfence_after_load = 1;
13430 else if (strcasecmp (arg, "no") == 0)
13431 lfence_after_load = 0;
13432 else
13433 as_fatal (_("invalid -mlfence-after-load= option: `%s'"), arg);
13434 break;
13435
13436 case OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH:
13437 if (strcasecmp (arg, "all") == 0)
a09f656b 13438 {
13439 lfence_before_indirect_branch = lfence_branch_all;
13440 if (lfence_before_ret == lfence_before_ret_none)
13441 lfence_before_ret = lfence_before_ret_shl;
13442 }
ae531041
L
13443 else if (strcasecmp (arg, "memory") == 0)
13444 lfence_before_indirect_branch = lfence_branch_memory;
13445 else if (strcasecmp (arg, "register") == 0)
13446 lfence_before_indirect_branch = lfence_branch_register;
13447 else if (strcasecmp (arg, "none") == 0)
13448 lfence_before_indirect_branch = lfence_branch_none;
13449 else
13450 as_fatal (_("invalid -mlfence-before-indirect-branch= option: `%s'"),
13451 arg);
13452 break;
13453
13454 case OPTION_MLFENCE_BEFORE_RET:
13455 if (strcasecmp (arg, "or") == 0)
13456 lfence_before_ret = lfence_before_ret_or;
13457 else if (strcasecmp (arg, "not") == 0)
13458 lfence_before_ret = lfence_before_ret_not;
a09f656b 13459 else if (strcasecmp (arg, "shl") == 0 || strcasecmp (arg, "yes") == 0)
13460 lfence_before_ret = lfence_before_ret_shl;
ae531041
L
13461 else if (strcasecmp (arg, "none") == 0)
13462 lfence_before_ret = lfence_before_ret_none;
13463 else
13464 as_fatal (_("invalid -mlfence-before-ret= option: `%s'"),
13465 arg);
13466 break;
13467
0cb4071e
L
13468 case OPTION_MRELAX_RELOCATIONS:
13469 if (strcasecmp (arg, "yes") == 0)
13470 generate_relax_relocations = 1;
13471 else if (strcasecmp (arg, "no") == 0)
13472 generate_relax_relocations = 0;
13473 else
13474 as_fatal (_("invalid -mrelax-relocations= option: `%s'"), arg);
13475 break;
13476
e379e5f3
L
13477 case OPTION_MALIGN_BRANCH_BOUNDARY:
13478 {
13479 char *end;
13480 long int align = strtoul (arg, &end, 0);
13481 if (*end == '\0')
13482 {
13483 if (align == 0)
13484 {
13485 align_branch_power = 0;
13486 break;
13487 }
13488 else if (align >= 16)
13489 {
13490 int align_power;
13491 for (align_power = 0;
13492 (align & 1) == 0;
13493 align >>= 1, align_power++)
13494 continue;
13495 /* Limit alignment power to 31. */
13496 if (align == 1 && align_power < 32)
13497 {
13498 align_branch_power = align_power;
13499 break;
13500 }
13501 }
13502 }
13503 as_fatal (_("invalid -malign-branch-boundary= value: %s"), arg);
13504 }
13505 break;
13506
13507 case OPTION_MALIGN_BRANCH_PREFIX_SIZE:
13508 {
13509 char *end;
13510 int align = strtoul (arg, &end, 0);
13511 /* Some processors only support 5 prefixes. */
13512 if (*end == '\0' && align >= 0 && align < 6)
13513 {
13514 align_branch_prefix_size = align;
13515 break;
13516 }
13517 as_fatal (_("invalid -malign-branch-prefix-size= value: %s"),
13518 arg);
13519 }
13520 break;
13521
13522 case OPTION_MALIGN_BRANCH:
13523 align_branch = 0;
13524 saved = xstrdup (arg);
13525 type = saved;
13526 do
13527 {
13528 next = strchr (type, '+');
13529 if (next)
13530 *next++ = '\0';
13531 if (strcasecmp (type, "jcc") == 0)
13532 align_branch |= align_branch_jcc_bit;
13533 else if (strcasecmp (type, "fused") == 0)
13534 align_branch |= align_branch_fused_bit;
13535 else if (strcasecmp (type, "jmp") == 0)
13536 align_branch |= align_branch_jmp_bit;
13537 else if (strcasecmp (type, "call") == 0)
13538 align_branch |= align_branch_call_bit;
13539 else if (strcasecmp (type, "ret") == 0)
13540 align_branch |= align_branch_ret_bit;
13541 else if (strcasecmp (type, "indirect") == 0)
13542 align_branch |= align_branch_indirect_bit;
13543 else
13544 as_fatal (_("invalid -malign-branch= option: `%s'"), arg);
13545 type = next;
13546 }
13547 while (next != NULL);
13548 free (saved);
13549 break;
13550
76cf450b
L
13551 case OPTION_MBRANCHES_WITH_32B_BOUNDARIES:
13552 align_branch_power = 5;
13553 align_branch_prefix_size = 5;
13554 align_branch = (align_branch_jcc_bit
13555 | align_branch_fused_bit
13556 | align_branch_jmp_bit);
13557 break;
13558
5db04b09 13559 case OPTION_MAMD64:
4b5aaf5f 13560 isa64 = amd64;
5db04b09
L
13561 break;
13562
13563 case OPTION_MINTEL64:
4b5aaf5f 13564 isa64 = intel64;
5db04b09
L
13565 break;
13566
b6f8c7c4
L
13567 case 'O':
13568 if (arg == NULL)
13569 {
13570 optimize = 1;
13571 /* Turn off -Os. */
13572 optimize_for_space = 0;
13573 }
13574 else if (*arg == 's')
13575 {
13576 optimize_for_space = 1;
13577 /* Turn on all encoding optimizations. */
41fd2579 13578 optimize = INT_MAX;
b6f8c7c4
L
13579 }
13580 else
13581 {
13582 optimize = atoi (arg);
13583 /* Turn off -Os. */
13584 optimize_for_space = 0;
13585 }
13586 break;
13587
252b5132
RH
13588 default:
13589 return 0;
13590 }
13591 return 1;
13592}
13593
8a2c8fef
L
13594#define MESSAGE_TEMPLATE \
13595" "
13596
293f5f65
L
13597static char *
13598output_message (FILE *stream, char *p, char *message, char *start,
13599 int *left_p, const char *name, int len)
13600{
13601 int size = sizeof (MESSAGE_TEMPLATE);
13602 int left = *left_p;
13603
13604 /* Reserve 2 spaces for ", " or ",\0" */
13605 left -= len + 2;
13606
13607 /* Check if there is any room. */
13608 if (left >= 0)
13609 {
13610 if (p != start)
13611 {
13612 *p++ = ',';
13613 *p++ = ' ';
13614 }
13615 p = mempcpy (p, name, len);
13616 }
13617 else
13618 {
13619 /* Output the current message now and start a new one. */
13620 *p++ = ',';
13621 *p = '\0';
13622 fprintf (stream, "%s\n", message);
13623 p = start;
13624 left = size - (start - message) - len - 2;
13625
13626 gas_assert (left >= 0);
13627
13628 p = mempcpy (p, name, len);
13629 }
13630
13631 *left_p = left;
13632 return p;
13633}
13634
8a2c8fef 13635static void
1ded5609 13636show_arch (FILE *stream, int ext, int check)
8a2c8fef
L
13637{
13638 static char message[] = MESSAGE_TEMPLATE;
13639 char *start = message + 27;
13640 char *p;
13641 int size = sizeof (MESSAGE_TEMPLATE);
13642 int left;
13643 const char *name;
13644 int len;
13645 unsigned int j;
13646
13647 p = start;
13648 left = size - (start - message);
13649 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13650 {
13651 /* Should it be skipped? */
13652 if (cpu_arch [j].skip)
13653 continue;
13654
13655 name = cpu_arch [j].name;
13656 len = cpu_arch [j].len;
13657 if (*name == '.')
13658 {
13659 /* It is an extension. Skip if we aren't asked to show it. */
13660 if (ext)
13661 {
13662 name++;
13663 len--;
13664 }
13665 else
13666 continue;
13667 }
13668 else if (ext)
13669 {
13670 /* It is an processor. Skip if we show only extension. */
13671 continue;
13672 }
1ded5609
JB
13673 else if (check && ! cpu_arch[j].flags.bitfield.cpui386)
13674 {
13675 /* It is an impossible processor - skip. */
13676 continue;
13677 }
8a2c8fef 13678
293f5f65 13679 p = output_message (stream, p, message, start, &left, name, len);
8a2c8fef
L
13680 }
13681
293f5f65
L
13682 /* Display disabled extensions. */
13683 if (ext)
13684 for (j = 0; j < ARRAY_SIZE (cpu_noarch); j++)
13685 {
13686 name = cpu_noarch [j].name;
13687 len = cpu_noarch [j].len;
13688 p = output_message (stream, p, message, start, &left, name,
13689 len);
13690 }
13691
8a2c8fef
L
13692 *p = '\0';
13693 fprintf (stream, "%s\n", message);
13694}
13695
252b5132 13696void
8a2c8fef 13697md_show_usage (FILE *stream)
252b5132 13698{
4cc782b5
ILT
13699#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13700 fprintf (stream, _("\
d4693039 13701 -Qy, -Qn ignored\n\
a38cf1db 13702 -V print assembler version number\n\
b3b91714
AM
13703 -k ignored\n"));
13704#endif
13705 fprintf (stream, _("\
12b55ccc 13706 -n Do not optimize code alignment\n\
b3b91714
AM
13707 -q quieten some warnings\n"));
13708#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13709 fprintf (stream, _("\
a38cf1db 13710 -s ignored\n"));
b3b91714 13711#endif
d7f449c0
L
13712#if defined BFD64 && (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
13713 || defined (TE_PE) || defined (TE_PEP))
751d281c 13714 fprintf (stream, _("\
570561f7 13715 --32/--64/--x32 generate 32bit/64bit/x32 code\n"));
751d281c 13716#endif
b3b91714
AM
13717#ifdef SVR4_COMMENT_CHARS
13718 fprintf (stream, _("\
13719 --divide do not treat `/' as a comment character\n"));
a38cf1db
AM
13720#else
13721 fprintf (stream, _("\
b3b91714 13722 --divide ignored\n"));
4cc782b5 13723#endif
9103f4f4 13724 fprintf (stream, _("\
6305a203 13725 -march=CPU[,+EXTENSION...]\n\
8a2c8fef 13726 generate code for CPU and EXTENSION, CPU is one of:\n"));
1ded5609 13727 show_arch (stream, 0, 1);
8a2c8fef
L
13728 fprintf (stream, _("\
13729 EXTENSION is combination of:\n"));
1ded5609 13730 show_arch (stream, 1, 0);
6305a203 13731 fprintf (stream, _("\
8a2c8fef 13732 -mtune=CPU optimize for CPU, CPU is one of:\n"));
1ded5609 13733 show_arch (stream, 0, 0);
ba104c83 13734 fprintf (stream, _("\
c0f3af97
L
13735 -msse2avx encode SSE instructions with VEX prefix\n"));
13736 fprintf (stream, _("\
7c5c05ef 13737 -msse-check=[none|error|warning] (default: warning)\n\
daf50ae7
L
13738 check SSE instructions\n"));
13739 fprintf (stream, _("\
7c5c05ef 13740 -moperand-check=[none|error|warning] (default: warning)\n\
7bab8ab5
JB
13741 check operand combinations for validity\n"));
13742 fprintf (stream, _("\
7c5c05ef
L
13743 -mavxscalar=[128|256] (default: 128)\n\
13744 encode scalar AVX instructions with specific vector\n\
539f890d
L
13745 length\n"));
13746 fprintf (stream, _("\
03751133
L
13747 -mvexwig=[0|1] (default: 0)\n\
13748 encode VEX instructions with specific VEX.W value\n\
13749 for VEX.W bit ignored instructions\n"));
13750 fprintf (stream, _("\
7c5c05ef
L
13751 -mevexlig=[128|256|512] (default: 128)\n\
13752 encode scalar EVEX instructions with specific vector\n\
43234a1e
L
13753 length\n"));
13754 fprintf (stream, _("\
7c5c05ef
L
13755 -mevexwig=[0|1] (default: 0)\n\
13756 encode EVEX instructions with specific EVEX.W value\n\
43234a1e
L
13757 for EVEX.W bit ignored instructions\n"));
13758 fprintf (stream, _("\
7c5c05ef 13759 -mevexrcig=[rne|rd|ru|rz] (default: rne)\n\
d3d3c6db
IT
13760 encode EVEX instructions with specific EVEX.RC value\n\
13761 for SAE-only ignored instructions\n"));
13762 fprintf (stream, _("\
7c5c05ef
L
13763 -mmnemonic=[att|intel] "));
13764 if (SYSV386_COMPAT)
13765 fprintf (stream, _("(default: att)\n"));
13766 else
13767 fprintf (stream, _("(default: intel)\n"));
13768 fprintf (stream, _("\
13769 use AT&T/Intel mnemonic\n"));
ba104c83 13770 fprintf (stream, _("\
7c5c05ef
L
13771 -msyntax=[att|intel] (default: att)\n\
13772 use AT&T/Intel syntax\n"));
ba104c83
L
13773 fprintf (stream, _("\
13774 -mindex-reg support pseudo index registers\n"));
13775 fprintf (stream, _("\
13776 -mnaked-reg don't require `%%' prefix for registers\n"));
13777 fprintf (stream, _("\
7e8b059b 13778 -madd-bnd-prefix add BND prefix for all valid branches\n"));
b4a3a7b4 13779#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8dcea932
L
13780 fprintf (stream, _("\
13781 -mshared disable branch optimization for shared code\n"));
b4a3a7b4
L
13782 fprintf (stream, _("\
13783 -mx86-used-note=[no|yes] "));
13784 if (DEFAULT_X86_USED_NOTE)
13785 fprintf (stream, _("(default: yes)\n"));
13786 else
13787 fprintf (stream, _("(default: no)\n"));
13788 fprintf (stream, _("\
13789 generate x86 used ISA and feature properties\n"));
13790#endif
13791#if defined (TE_PE) || defined (TE_PEP)
167ad85b
TG
13792 fprintf (stream, _("\
13793 -mbig-obj generate big object files\n"));
13794#endif
d022bddd 13795 fprintf (stream, _("\
7c5c05ef 13796 -momit-lock-prefix=[no|yes] (default: no)\n\
d022bddd 13797 strip all lock prefixes\n"));
5db04b09 13798 fprintf (stream, _("\
7c5c05ef 13799 -mfence-as-lock-add=[no|yes] (default: no)\n\
e4e00185
AS
13800 encode lfence, mfence and sfence as\n\
13801 lock addl $0x0, (%%{re}sp)\n"));
13802 fprintf (stream, _("\
7c5c05ef
L
13803 -mrelax-relocations=[no|yes] "));
13804 if (DEFAULT_GENERATE_X86_RELAX_RELOCATIONS)
13805 fprintf (stream, _("(default: yes)\n"));
13806 else
13807 fprintf (stream, _("(default: no)\n"));
13808 fprintf (stream, _("\
0cb4071e
L
13809 generate relax relocations\n"));
13810 fprintf (stream, _("\
e379e5f3
L
13811 -malign-branch-boundary=NUM (default: 0)\n\
13812 align branches within NUM byte boundary\n"));
13813 fprintf (stream, _("\
13814 -malign-branch=TYPE[+TYPE...] (default: jcc+fused+jmp)\n\
13815 TYPE is combination of jcc, fused, jmp, call, ret,\n\
13816 indirect\n\
13817 specify types of branches to align\n"));
13818 fprintf (stream, _("\
13819 -malign-branch-prefix-size=NUM (default: 5)\n\
13820 align branches with NUM prefixes per instruction\n"));
13821 fprintf (stream, _("\
76cf450b
L
13822 -mbranches-within-32B-boundaries\n\
13823 align branches within 32 byte boundary\n"));
13824 fprintf (stream, _("\
ae531041
L
13825 -mlfence-after-load=[no|yes] (default: no)\n\
13826 generate lfence after load\n"));
13827 fprintf (stream, _("\
13828 -mlfence-before-indirect-branch=[none|all|register|memory] (default: none)\n\
13829 generate lfence before indirect near branch\n"));
13830 fprintf (stream, _("\
a09f656b 13831 -mlfence-before-ret=[none|or|not|shl|yes] (default: none)\n\
ae531041
L
13832 generate lfence before ret\n"));
13833 fprintf (stream, _("\
7c5c05ef 13834 -mamd64 accept only AMD64 ISA [default]\n"));
5db04b09
L
13835 fprintf (stream, _("\
13836 -mintel64 accept only Intel64 ISA\n"));
252b5132
RH
13837}
13838
3e73aa7c 13839#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
321098a5 13840 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
e57f8c65 13841 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
252b5132
RH
13842
13843/* Pick the target format to use. */
13844
47926f60 13845const char *
e3bb37b5 13846i386_target_format (void)
252b5132 13847{
d34049e8 13848 if (startswith (default_arch, "x86_64"))
351f65ca
L
13849 {
13850 update_code_flag (CODE_64BIT, 1);
13851 if (default_arch[6] == '\0')
7f56bc95 13852 x86_elf_abi = X86_64_ABI;
351f65ca 13853 else
7f56bc95 13854 x86_elf_abi = X86_64_X32_ABI;
351f65ca 13855 }
3e73aa7c 13856 else if (!strcmp (default_arch, "i386"))
78f12dd3 13857 update_code_flag (CODE_32BIT, 1);
5197d474
L
13858 else if (!strcmp (default_arch, "iamcu"))
13859 {
13860 update_code_flag (CODE_32BIT, 1);
13861 if (cpu_arch_isa == PROCESSOR_UNKNOWN)
13862 {
13863 static const i386_cpu_flags iamcu_flags = CPU_IAMCU_FLAGS;
13864 cpu_arch_name = "iamcu";
13865 cpu_sub_arch_name = NULL;
13866 cpu_arch_flags = iamcu_flags;
13867 cpu_arch_isa = PROCESSOR_IAMCU;
13868 cpu_arch_isa_flags = iamcu_flags;
13869 if (!cpu_arch_tune_set)
13870 {
13871 cpu_arch_tune = cpu_arch_isa;
13872 cpu_arch_tune_flags = cpu_arch_isa_flags;
13873 }
13874 }
8d471ec1 13875 else if (cpu_arch_isa != PROCESSOR_IAMCU)
5197d474
L
13876 as_fatal (_("Intel MCU doesn't support `%s' architecture"),
13877 cpu_arch_name);
13878 }
3e73aa7c 13879 else
2b5d6a91 13880 as_fatal (_("unknown architecture"));
89507696
JB
13881
13882 if (cpu_flags_all_zero (&cpu_arch_isa_flags))
13883 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].flags;
13884 if (cpu_flags_all_zero (&cpu_arch_tune_flags))
13885 cpu_arch_tune_flags = cpu_arch[flag_code == CODE_64BIT].flags;
13886
252b5132
RH
13887 switch (OUTPUT_FLAVOR)
13888 {
9384f2ff 13889#if defined (OBJ_MAYBE_AOUT) || defined (OBJ_AOUT)
4c63da97 13890 case bfd_target_aout_flavour:
47926f60 13891 return AOUT_TARGET_FORMAT;
4c63da97 13892#endif
9384f2ff
AM
13893#if defined (OBJ_MAYBE_COFF) || defined (OBJ_COFF)
13894# if defined (TE_PE) || defined (TE_PEP)
13895 case bfd_target_coff_flavour:
167ad85b 13896 if (flag_code == CODE_64BIT)
eb19308f
JB
13897 {
13898 object_64bit = 1;
13899 return use_big_obj ? "pe-bigobj-x86-64" : "pe-x86-64";
13900 }
13901 return use_big_obj ? "pe-bigobj-i386" : "pe-i386";
9384f2ff 13902# elif defined (TE_GO32)
0561d57c
JK
13903 case bfd_target_coff_flavour:
13904 return "coff-go32";
9384f2ff 13905# else
252b5132
RH
13906 case bfd_target_coff_flavour:
13907 return "coff-i386";
9384f2ff 13908# endif
4c63da97 13909#endif
3e73aa7c 13910#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
252b5132 13911 case bfd_target_elf_flavour:
3e73aa7c 13912 {
351f65ca
L
13913 const char *format;
13914
13915 switch (x86_elf_abi)
4fa24527 13916 {
351f65ca
L
13917 default:
13918 format = ELF_TARGET_FORMAT;
e379e5f3
L
13919#ifndef TE_SOLARIS
13920 tls_get_addr = "___tls_get_addr";
13921#endif
351f65ca 13922 break;
7f56bc95 13923 case X86_64_ABI:
351f65ca 13924 use_rela_relocations = 1;
4fa24527 13925 object_64bit = 1;
e379e5f3
L
13926#ifndef TE_SOLARIS
13927 tls_get_addr = "__tls_get_addr";
13928#endif
351f65ca
L
13929 format = ELF_TARGET_FORMAT64;
13930 break;
7f56bc95 13931 case X86_64_X32_ABI:
4fa24527 13932 use_rela_relocations = 1;
351f65ca 13933 object_64bit = 1;
e379e5f3
L
13934#ifndef TE_SOLARIS
13935 tls_get_addr = "__tls_get_addr";
13936#endif
862be3fb 13937 disallow_64bit_reloc = 1;
351f65ca
L
13938 format = ELF_TARGET_FORMAT32;
13939 break;
4fa24527 13940 }
3632d14b 13941 if (cpu_arch_isa == PROCESSOR_L1OM)
8a9036a4 13942 {
7f56bc95 13943 if (x86_elf_abi != X86_64_ABI)
8a9036a4
L
13944 as_fatal (_("Intel L1OM is 64bit only"));
13945 return ELF_TARGET_L1OM_FORMAT;
13946 }
b49f93f6 13947 else if (cpu_arch_isa == PROCESSOR_K1OM)
7a9068fe
L
13948 {
13949 if (x86_elf_abi != X86_64_ABI)
13950 as_fatal (_("Intel K1OM is 64bit only"));
13951 return ELF_TARGET_K1OM_FORMAT;
13952 }
81486035
L
13953 else if (cpu_arch_isa == PROCESSOR_IAMCU)
13954 {
13955 if (x86_elf_abi != I386_ABI)
13956 as_fatal (_("Intel MCU is 32bit only"));
13957 return ELF_TARGET_IAMCU_FORMAT;
13958 }
8a9036a4 13959 else
351f65ca 13960 return format;
3e73aa7c 13961 }
e57f8c65
TG
13962#endif
13963#if defined (OBJ_MACH_O)
13964 case bfd_target_mach_o_flavour:
d382c579
TG
13965 if (flag_code == CODE_64BIT)
13966 {
13967 use_rela_relocations = 1;
13968 object_64bit = 1;
13969 return "mach-o-x86-64";
13970 }
13971 else
13972 return "mach-o-i386";
4c63da97 13973#endif
252b5132
RH
13974 default:
13975 abort ();
13976 return NULL;
13977 }
13978}
13979
47926f60 13980#endif /* OBJ_MAYBE_ more than one */
252b5132 13981\f
252b5132 13982symbolS *
7016a5d5 13983md_undefined_symbol (char *name)
252b5132 13984{
18dc2407
ILT
13985 if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
13986 && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
13987 && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
13988 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
24eab124
AM
13989 {
13990 if (!GOT_symbol)
13991 {
13992 if (symbol_find (name))
13993 as_bad (_("GOT already in symbol table"));
13994 GOT_symbol = symbol_new (name, undefined_section,
e01e1cee 13995 &zero_address_frag, 0);
24eab124
AM
13996 };
13997 return GOT_symbol;
13998 }
252b5132
RH
13999 return 0;
14000}
14001
14002/* Round up a section size to the appropriate boundary. */
47926f60 14003
252b5132 14004valueT
7016a5d5 14005md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
252b5132 14006{
4c63da97
AM
14007#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
14008 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
14009 {
14010 /* For a.out, force the section size to be aligned. If we don't do
14011 this, BFD will align it for us, but it will not write out the
14012 final bytes of the section. This may be a bug in BFD, but it is
14013 easier to fix it here since that is how the other a.out targets
14014 work. */
14015 int align;
14016
fd361982 14017 align = bfd_section_alignment (segment);
8d3842cd 14018 size = ((size + (1 << align) - 1) & (-((valueT) 1 << align)));
4c63da97 14019 }
252b5132
RH
14020#endif
14021
14022 return size;
14023}
14024
14025/* On the i386, PC-relative offsets are relative to the start of the
14026 next instruction. That is, the address of the offset, plus its
14027 size, since the offset is always the last part of the insn. */
14028
14029long
e3bb37b5 14030md_pcrel_from (fixS *fixP)
252b5132
RH
14031{
14032 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
14033}
14034
14035#ifndef I386COFF
14036
14037static void
e3bb37b5 14038s_bss (int ignore ATTRIBUTE_UNUSED)
252b5132 14039{
29b0f896 14040 int temp;
252b5132 14041
8a75718c
JB
14042#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14043 if (IS_ELF)
14044 obj_elf_section_change_hook ();
14045#endif
252b5132
RH
14046 temp = get_absolute_expression ();
14047 subseg_set (bss_section, (subsegT) temp);
14048 demand_empty_rest_of_line ();
14049}
14050
14051#endif
14052
e379e5f3
L
14053/* Remember constant directive. */
14054
14055void
14056i386_cons_align (int ignore ATTRIBUTE_UNUSED)
14057{
14058 if (last_insn.kind != last_insn_directive
14059 && (bfd_section_flags (now_seg) & SEC_CODE))
14060 {
14061 last_insn.seg = now_seg;
14062 last_insn.kind = last_insn_directive;
14063 last_insn.name = "constant directive";
14064 last_insn.file = as_where (&last_insn.line);
ae531041
L
14065 if (lfence_before_ret != lfence_before_ret_none)
14066 {
14067 if (lfence_before_indirect_branch != lfence_branch_none)
14068 as_warn (_("constant directive skips -mlfence-before-ret "
14069 "and -mlfence-before-indirect-branch"));
14070 else
14071 as_warn (_("constant directive skips -mlfence-before-ret"));
14072 }
14073 else if (lfence_before_indirect_branch != lfence_branch_none)
14074 as_warn (_("constant directive skips -mlfence-before-indirect-branch"));
e379e5f3
L
14075 }
14076}
14077
3abbafc2 14078int
e3bb37b5 14079i386_validate_fix (fixS *fixp)
252b5132 14080{
3abbafc2
JB
14081#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14082 if (fixp->fx_r_type == BFD_RELOC_SIZE32
14083 || fixp->fx_r_type == BFD_RELOC_SIZE64)
14084 return IS_ELF && fixp->fx_addsy
14085 && (!S_IS_DEFINED (fixp->fx_addsy)
14086 || S_IS_EXTERNAL (fixp->fx_addsy));
14087#endif
14088
02a86693 14089 if (fixp->fx_subsy)
252b5132 14090 {
02a86693 14091 if (fixp->fx_subsy == GOT_symbol)
23df1078 14092 {
02a86693
L
14093 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
14094 {
14095 if (!object_64bit)
14096 abort ();
14097#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14098 if (fixp->fx_tcbit2)
56ceb5b5
L
14099 fixp->fx_r_type = (fixp->fx_tcbit
14100 ? BFD_RELOC_X86_64_REX_GOTPCRELX
14101 : BFD_RELOC_X86_64_GOTPCRELX);
02a86693
L
14102 else
14103#endif
14104 fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
14105 }
d6ab8113 14106 else
02a86693
L
14107 {
14108 if (!object_64bit)
14109 fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
14110 else
14111 fixp->fx_r_type = BFD_RELOC_X86_64_GOTOFF64;
14112 }
14113 fixp->fx_subsy = 0;
23df1078 14114 }
252b5132 14115 }
02a86693 14116#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2585b7a5 14117 else
02a86693 14118 {
2585b7a5
L
14119 /* NB: Commit 292676c1 resolved PLT32 reloc aganst local symbol
14120 to section. Since PLT32 relocation must be against symbols,
14121 turn such PLT32 relocation into PC32 relocation. */
14122 if (fixp->fx_addsy
14123 && (fixp->fx_r_type == BFD_RELOC_386_PLT32
14124 || fixp->fx_r_type == BFD_RELOC_X86_64_PLT32)
14125 && symbol_section_p (fixp->fx_addsy))
14126 fixp->fx_r_type = BFD_RELOC_32_PCREL;
14127 if (!object_64bit)
14128 {
14129 if (fixp->fx_r_type == BFD_RELOC_386_GOT32
14130 && fixp->fx_tcbit2)
14131 fixp->fx_r_type = BFD_RELOC_386_GOT32X;
14132 }
02a86693
L
14133 }
14134#endif
3abbafc2
JB
14135
14136 return 1;
252b5132
RH
14137}
14138
252b5132 14139arelent *
7016a5d5 14140tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
14141{
14142 arelent *rel;
14143 bfd_reloc_code_real_type code;
14144
14145 switch (fixp->fx_r_type)
14146 {
8ce3d284 14147#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3abbafc2
JB
14148 symbolS *sym;
14149
8fd4256d
L
14150 case BFD_RELOC_SIZE32:
14151 case BFD_RELOC_SIZE64:
3abbafc2
JB
14152 if (fixp->fx_addsy
14153 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))
14154 && (!fixp->fx_subsy
14155 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))))
14156 sym = fixp->fx_addsy;
14157 else if (fixp->fx_subsy
14158 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))
14159 && (!fixp->fx_addsy
14160 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))))
14161 sym = fixp->fx_subsy;
14162 else
14163 sym = NULL;
14164 if (IS_ELF && sym && S_IS_DEFINED (sym) && !S_IS_EXTERNAL (sym))
8fd4256d
L
14165 {
14166 /* Resolve size relocation against local symbol to size of
14167 the symbol plus addend. */
3abbafc2 14168 valueT value = S_GET_SIZE (sym);
44f87162 14169
3abbafc2
JB
14170 if (symbol_get_bfdsym (sym)->flags & BSF_SECTION_SYM)
14171 value = bfd_section_size (S_GET_SEGMENT (sym));
14172 if (sym == fixp->fx_subsy)
14173 {
14174 value = -value;
14175 if (fixp->fx_addsy)
14176 value += S_GET_VALUE (fixp->fx_addsy);
14177 }
14178 else if (fixp->fx_subsy)
14179 value -= S_GET_VALUE (fixp->fx_subsy);
44f87162 14180 value += fixp->fx_offset;
8fd4256d 14181 if (fixp->fx_r_type == BFD_RELOC_SIZE32
d965814f 14182 && object_64bit
8fd4256d
L
14183 && !fits_in_unsigned_long (value))
14184 as_bad_where (fixp->fx_file, fixp->fx_line,
14185 _("symbol size computation overflow"));
14186 fixp->fx_addsy = NULL;
14187 fixp->fx_subsy = NULL;
14188 md_apply_fix (fixp, (valueT *) &value, NULL);
14189 return NULL;
14190 }
3abbafc2
JB
14191 if (!fixp->fx_addsy || fixp->fx_subsy)
14192 {
14193 as_bad_where (fixp->fx_file, fixp->fx_line,
14194 "unsupported expression involving @size");
14195 return NULL;
14196 }
8ce3d284 14197#endif
1a0670f3 14198 /* Fall through. */
8fd4256d 14199
3e73aa7c
JH
14200 case BFD_RELOC_X86_64_PLT32:
14201 case BFD_RELOC_X86_64_GOT32:
14202 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
14203 case BFD_RELOC_X86_64_GOTPCRELX:
14204 case BFD_RELOC_X86_64_REX_GOTPCRELX:
252b5132
RH
14205 case BFD_RELOC_386_PLT32:
14206 case BFD_RELOC_386_GOT32:
02a86693 14207 case BFD_RELOC_386_GOT32X:
252b5132
RH
14208 case BFD_RELOC_386_GOTOFF:
14209 case BFD_RELOC_386_GOTPC:
13ae64f3
JJ
14210 case BFD_RELOC_386_TLS_GD:
14211 case BFD_RELOC_386_TLS_LDM:
14212 case BFD_RELOC_386_TLS_LDO_32:
14213 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
14214 case BFD_RELOC_386_TLS_IE:
14215 case BFD_RELOC_386_TLS_GOTIE:
13ae64f3
JJ
14216 case BFD_RELOC_386_TLS_LE_32:
14217 case BFD_RELOC_386_TLS_LE:
67a4f2b7
AO
14218 case BFD_RELOC_386_TLS_GOTDESC:
14219 case BFD_RELOC_386_TLS_DESC_CALL:
bffbf940
JJ
14220 case BFD_RELOC_X86_64_TLSGD:
14221 case BFD_RELOC_X86_64_TLSLD:
14222 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 14223 case BFD_RELOC_X86_64_DTPOFF64:
bffbf940
JJ
14224 case BFD_RELOC_X86_64_GOTTPOFF:
14225 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113
JB
14226 case BFD_RELOC_X86_64_TPOFF64:
14227 case BFD_RELOC_X86_64_GOTOFF64:
14228 case BFD_RELOC_X86_64_GOTPC32:
7b81dfbb
AJ
14229 case BFD_RELOC_X86_64_GOT64:
14230 case BFD_RELOC_X86_64_GOTPCREL64:
14231 case BFD_RELOC_X86_64_GOTPC64:
14232 case BFD_RELOC_X86_64_GOTPLT64:
14233 case BFD_RELOC_X86_64_PLTOFF64:
67a4f2b7
AO
14234 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14235 case BFD_RELOC_X86_64_TLSDESC_CALL:
252b5132
RH
14236 case BFD_RELOC_RVA:
14237 case BFD_RELOC_VTABLE_ENTRY:
14238 case BFD_RELOC_VTABLE_INHERIT:
6482c264
NC
14239#ifdef TE_PE
14240 case BFD_RELOC_32_SECREL:
14241#endif
252b5132
RH
14242 code = fixp->fx_r_type;
14243 break;
dbbaec26
L
14244 case BFD_RELOC_X86_64_32S:
14245 if (!fixp->fx_pcrel)
14246 {
14247 /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32. */
14248 code = fixp->fx_r_type;
14249 break;
14250 }
1a0670f3 14251 /* Fall through. */
252b5132 14252 default:
93382f6d 14253 if (fixp->fx_pcrel)
252b5132 14254 {
93382f6d
AM
14255 switch (fixp->fx_size)
14256 {
14257 default:
b091f402
AM
14258 as_bad_where (fixp->fx_file, fixp->fx_line,
14259 _("can not do %d byte pc-relative relocation"),
14260 fixp->fx_size);
93382f6d
AM
14261 code = BFD_RELOC_32_PCREL;
14262 break;
14263 case 1: code = BFD_RELOC_8_PCREL; break;
14264 case 2: code = BFD_RELOC_16_PCREL; break;
d258b828 14265 case 4: code = BFD_RELOC_32_PCREL; break;
d6ab8113
JB
14266#ifdef BFD64
14267 case 8: code = BFD_RELOC_64_PCREL; break;
14268#endif
93382f6d
AM
14269 }
14270 }
14271 else
14272 {
14273 switch (fixp->fx_size)
14274 {
14275 default:
b091f402
AM
14276 as_bad_where (fixp->fx_file, fixp->fx_line,
14277 _("can not do %d byte relocation"),
14278 fixp->fx_size);
93382f6d
AM
14279 code = BFD_RELOC_32;
14280 break;
14281 case 1: code = BFD_RELOC_8; break;
14282 case 2: code = BFD_RELOC_16; break;
14283 case 4: code = BFD_RELOC_32; break;
937149dd 14284#ifdef BFD64
3e73aa7c 14285 case 8: code = BFD_RELOC_64; break;
937149dd 14286#endif
93382f6d 14287 }
252b5132
RH
14288 }
14289 break;
14290 }
252b5132 14291
d182319b
JB
14292 if ((code == BFD_RELOC_32
14293 || code == BFD_RELOC_32_PCREL
14294 || code == BFD_RELOC_X86_64_32S)
252b5132
RH
14295 && GOT_symbol
14296 && fixp->fx_addsy == GOT_symbol)
3e73aa7c 14297 {
4fa24527 14298 if (!object_64bit)
d6ab8113
JB
14299 code = BFD_RELOC_386_GOTPC;
14300 else
14301 code = BFD_RELOC_X86_64_GOTPC32;
3e73aa7c 14302 }
7b81dfbb
AJ
14303 if ((code == BFD_RELOC_64 || code == BFD_RELOC_64_PCREL)
14304 && GOT_symbol
14305 && fixp->fx_addsy == GOT_symbol)
14306 {
14307 code = BFD_RELOC_X86_64_GOTPC64;
14308 }
252b5132 14309
add39d23
TS
14310 rel = XNEW (arelent);
14311 rel->sym_ptr_ptr = XNEW (asymbol *);
49309057 14312 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
14313
14314 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
c87db184 14315
3e73aa7c
JH
14316 if (!use_rela_relocations)
14317 {
14318 /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
14319 vtable entry to be used in the relocation's section offset. */
14320 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
14321 rel->address = fixp->fx_offset;
fbeb56a4
DK
14322#if defined (OBJ_COFF) && defined (TE_PE)
14323 else if (fixp->fx_addsy && S_IS_WEAK (fixp->fx_addsy))
14324 rel->addend = fixp->fx_addnumber - (S_GET_VALUE (fixp->fx_addsy) * 2);
14325 else
14326#endif
c6682705 14327 rel->addend = 0;
3e73aa7c
JH
14328 }
14329 /* Use the rela in 64bit mode. */
252b5132 14330 else
3e73aa7c 14331 {
862be3fb
L
14332 if (disallow_64bit_reloc)
14333 switch (code)
14334 {
862be3fb
L
14335 case BFD_RELOC_X86_64_DTPOFF64:
14336 case BFD_RELOC_X86_64_TPOFF64:
14337 case BFD_RELOC_64_PCREL:
14338 case BFD_RELOC_X86_64_GOTOFF64:
14339 case BFD_RELOC_X86_64_GOT64:
14340 case BFD_RELOC_X86_64_GOTPCREL64:
14341 case BFD_RELOC_X86_64_GOTPC64:
14342 case BFD_RELOC_X86_64_GOTPLT64:
14343 case BFD_RELOC_X86_64_PLTOFF64:
14344 as_bad_where (fixp->fx_file, fixp->fx_line,
14345 _("cannot represent relocation type %s in x32 mode"),
14346 bfd_get_reloc_code_name (code));
14347 break;
14348 default:
14349 break;
14350 }
14351
062cd5e7
AS
14352 if (!fixp->fx_pcrel)
14353 rel->addend = fixp->fx_offset;
14354 else
14355 switch (code)
14356 {
14357 case BFD_RELOC_X86_64_PLT32:
14358 case BFD_RELOC_X86_64_GOT32:
14359 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
14360 case BFD_RELOC_X86_64_GOTPCRELX:
14361 case BFD_RELOC_X86_64_REX_GOTPCRELX:
bffbf940
JJ
14362 case BFD_RELOC_X86_64_TLSGD:
14363 case BFD_RELOC_X86_64_TLSLD:
14364 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7
AO
14365 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14366 case BFD_RELOC_X86_64_TLSDESC_CALL:
062cd5e7
AS
14367 rel->addend = fixp->fx_offset - fixp->fx_size;
14368 break;
14369 default:
14370 rel->addend = (section->vma
14371 - fixp->fx_size
14372 + fixp->fx_addnumber
14373 + md_pcrel_from (fixp));
14374 break;
14375 }
3e73aa7c
JH
14376 }
14377
252b5132
RH
14378 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
14379 if (rel->howto == NULL)
14380 {
14381 as_bad_where (fixp->fx_file, fixp->fx_line,
d0b47220 14382 _("cannot represent relocation type %s"),
252b5132
RH
14383 bfd_get_reloc_code_name (code));
14384 /* Set howto to a garbage value so that we can keep going. */
14385 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
9c2799c2 14386 gas_assert (rel->howto != NULL);
252b5132
RH
14387 }
14388
14389 return rel;
14390}
14391
ee86248c 14392#include "tc-i386-intel.c"
54cfded0 14393
a60de03c
JB
14394void
14395tc_x86_parse_to_dw2regnum (expressionS *exp)
54cfded0 14396{
a60de03c
JB
14397 int saved_naked_reg;
14398 char saved_register_dot;
54cfded0 14399
a60de03c
JB
14400 saved_naked_reg = allow_naked_reg;
14401 allow_naked_reg = 1;
14402 saved_register_dot = register_chars['.'];
14403 register_chars['.'] = '.';
14404 allow_pseudo_reg = 1;
14405 expression_and_evaluate (exp);
14406 allow_pseudo_reg = 0;
14407 register_chars['.'] = saved_register_dot;
14408 allow_naked_reg = saved_naked_reg;
14409
e96d56a1 14410 if (exp->X_op == O_register && exp->X_add_number >= 0)
54cfded0 14411 {
a60de03c
JB
14412 if ((addressT) exp->X_add_number < i386_regtab_size)
14413 {
14414 exp->X_op = O_constant;
14415 exp->X_add_number = i386_regtab[exp->X_add_number]
14416 .dw2_regnum[flag_code >> 1];
14417 }
14418 else
14419 exp->X_op = O_illegal;
54cfded0 14420 }
54cfded0
AM
14421}
14422
14423void
14424tc_x86_frame_initial_instructions (void)
14425{
a60de03c
JB
14426 static unsigned int sp_regno[2];
14427
14428 if (!sp_regno[flag_code >> 1])
14429 {
14430 char *saved_input = input_line_pointer;
14431 char sp[][4] = {"esp", "rsp"};
14432 expressionS exp;
a4447b93 14433
a60de03c
JB
14434 input_line_pointer = sp[flag_code >> 1];
14435 tc_x86_parse_to_dw2regnum (&exp);
9c2799c2 14436 gas_assert (exp.X_op == O_constant);
a60de03c
JB
14437 sp_regno[flag_code >> 1] = exp.X_add_number;
14438 input_line_pointer = saved_input;
14439 }
a4447b93 14440
61ff971f
L
14441 cfi_add_CFA_def_cfa (sp_regno[flag_code >> 1], -x86_cie_data_alignment);
14442 cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
54cfded0 14443}
d2b2c203 14444
d7921315
L
14445int
14446x86_dwarf2_addr_size (void)
14447{
14448#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
14449 if (x86_elf_abi == X86_64_X32_ABI)
14450 return 4;
14451#endif
14452 return bfd_arch_bits_per_address (stdoutput) / 8;
14453}
14454
d2b2c203
DJ
14455int
14456i386_elf_section_type (const char *str, size_t len)
14457{
14458 if (flag_code == CODE_64BIT
14459 && len == sizeof ("unwind") - 1
d34049e8 14460 && startswith (str, "unwind"))
d2b2c203
DJ
14461 return SHT_X86_64_UNWIND;
14462
14463 return -1;
14464}
bb41ade5 14465
ad5fec3b
EB
14466#ifdef TE_SOLARIS
14467void
14468i386_solaris_fix_up_eh_frame (segT sec)
14469{
14470 if (flag_code == CODE_64BIT)
14471 elf_section_type (sec) = SHT_X86_64_UNWIND;
14472}
14473#endif
14474
bb41ade5
AM
14475#ifdef TE_PE
14476void
14477tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
14478{
91d6fa6a 14479 expressionS exp;
bb41ade5 14480
91d6fa6a
NC
14481 exp.X_op = O_secrel;
14482 exp.X_add_symbol = symbol;
14483 exp.X_add_number = 0;
14484 emit_expr (&exp, size);
bb41ade5
AM
14485}
14486#endif
3b22753a
L
14487
14488#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14489/* For ELF on x86-64, add support for SHF_X86_64_LARGE. */
14490
01e1a5bc 14491bfd_vma
6d4af3c2 14492x86_64_section_letter (int letter, const char **ptr_msg)
3b22753a
L
14493{
14494 if (flag_code == CODE_64BIT)
14495 {
14496 if (letter == 'l')
14497 return SHF_X86_64_LARGE;
14498
8f3bae45 14499 *ptr_msg = _("bad .section directive: want a,l,w,x,M,S,G,T in string");
64e74474 14500 }
3b22753a 14501 else
8f3bae45 14502 *ptr_msg = _("bad .section directive: want a,w,x,M,S,G,T in string");
3b22753a
L
14503 return -1;
14504}
14505
01e1a5bc 14506bfd_vma
3b22753a
L
14507x86_64_section_word (char *str, size_t len)
14508{
08dedd66 14509 if (len == 5 && flag_code == CODE_64BIT && startswith (str, "large"))
3b22753a
L
14510 return SHF_X86_64_LARGE;
14511
14512 return -1;
14513}
14514
14515static void
14516handle_large_common (int small ATTRIBUTE_UNUSED)
14517{
14518 if (flag_code != CODE_64BIT)
14519 {
14520 s_comm_internal (0, elf_common_parse);
14521 as_warn (_(".largecomm supported only in 64bit mode, producing .comm"));
14522 }
14523 else
14524 {
14525 static segT lbss_section;
14526 asection *saved_com_section_ptr = elf_com_section_ptr;
14527 asection *saved_bss_section = bss_section;
14528
14529 if (lbss_section == NULL)
14530 {
14531 flagword applicable;
14532 segT seg = now_seg;
14533 subsegT subseg = now_subseg;
14534
14535 /* The .lbss section is for local .largecomm symbols. */
14536 lbss_section = subseg_new (".lbss", 0);
14537 applicable = bfd_applicable_section_flags (stdoutput);
fd361982 14538 bfd_set_section_flags (lbss_section, applicable & SEC_ALLOC);
3b22753a
L
14539 seg_info (lbss_section)->bss = 1;
14540
14541 subseg_set (seg, subseg);
14542 }
14543
14544 elf_com_section_ptr = &_bfd_elf_large_com_section;
14545 bss_section = lbss_section;
14546
14547 s_comm_internal (0, elf_common_parse);
14548
14549 elf_com_section_ptr = saved_com_section_ptr;
14550 bss_section = saved_bss_section;
14551 }
14552}
14553#endif /* OBJ_ELF || OBJ_MAYBE_ELF */
This page took 2.81514 seconds and 4 git commands to generate.