x86-64: have value properly checked when resolving fixup
[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
43234a1e 2460static INLINE int
b5014f7a 2461fits_in_disp8 (offsetT num)
43234a1e
L
2462{
2463 int shift = i.memshift;
2464 unsigned int mask;
2465
2466 if (shift == -1)
2467 abort ();
2468
2469 mask = (1 << shift) - 1;
2470
2471 /* Return 0 if NUM isn't properly aligned. */
2472 if ((num & mask))
2473 return 0;
2474
2475 /* Check if NUM will fit in 8bit after shift. */
2476 return fits_in_signed_byte (num >> shift);
2477}
2478
a683cc34
SP
2479static INLINE int
2480fits_in_imm4 (offsetT num)
2481{
2482 return (num & 0xf) == num;
2483}
2484
40fb9820 2485static i386_operand_type
e3bb37b5 2486smallest_imm_type (offsetT num)
252b5132 2487{
40fb9820 2488 i386_operand_type t;
7ab9ffdd 2489
0dfbf9d7 2490 operand_type_set (&t, 0);
40fb9820
L
2491 t.bitfield.imm64 = 1;
2492
2493 if (cpu_arch_tune != PROCESSOR_I486 && num == 1)
e413e4e9
AM
2494 {
2495 /* This code is disabled on the 486 because all the Imm1 forms
2496 in the opcode table are slower on the i486. They're the
2497 versions with the implicitly specified single-position
2498 displacement, which has another syntax if you really want to
2499 use that form. */
40fb9820
L
2500 t.bitfield.imm1 = 1;
2501 t.bitfield.imm8 = 1;
2502 t.bitfield.imm8s = 1;
2503 t.bitfield.imm16 = 1;
2504 t.bitfield.imm32 = 1;
2505 t.bitfield.imm32s = 1;
2506 }
2507 else if (fits_in_signed_byte (num))
2508 {
2509 t.bitfield.imm8 = 1;
2510 t.bitfield.imm8s = 1;
2511 t.bitfield.imm16 = 1;
2512 t.bitfield.imm32 = 1;
2513 t.bitfield.imm32s = 1;
2514 }
2515 else if (fits_in_unsigned_byte (num))
2516 {
2517 t.bitfield.imm8 = 1;
2518 t.bitfield.imm16 = 1;
2519 t.bitfield.imm32 = 1;
2520 t.bitfield.imm32s = 1;
2521 }
2522 else if (fits_in_signed_word (num) || fits_in_unsigned_word (num))
2523 {
2524 t.bitfield.imm16 = 1;
2525 t.bitfield.imm32 = 1;
2526 t.bitfield.imm32s = 1;
2527 }
2528 else if (fits_in_signed_long (num))
2529 {
2530 t.bitfield.imm32 = 1;
2531 t.bitfield.imm32s = 1;
2532 }
2533 else if (fits_in_unsigned_long (num))
2534 t.bitfield.imm32 = 1;
2535
2536 return t;
47926f60 2537}
252b5132 2538
847f7ad4 2539static offsetT
e3bb37b5 2540offset_in_range (offsetT val, int size)
847f7ad4 2541{
508866be 2542 addressT mask;
ba2adb93 2543
847f7ad4
AM
2544 switch (size)
2545 {
508866be
L
2546 case 1: mask = ((addressT) 1 << 8) - 1; break;
2547 case 2: mask = ((addressT) 1 << 16) - 1; break;
3b0ec529 2548 case 4: mask = ((addressT) 2 << 31) - 1; break;
3e73aa7c
JH
2549#ifdef BFD64
2550 case 8: mask = ((addressT) 2 << 63) - 1; break;
2551#endif
47926f60 2552 default: abort ();
847f7ad4
AM
2553 }
2554
47926f60 2555 if ((val & ~mask) != 0 && (val & ~mask) != ~mask)
847f7ad4
AM
2556 {
2557 char buf1[40], buf2[40];
2558
9a8041fd
JB
2559 bfd_sprintf_vma (stdoutput, buf1, val);
2560 bfd_sprintf_vma (stdoutput, buf2, val & mask);
847f7ad4
AM
2561 as_warn (_("%s shortened to %s"), buf1, buf2);
2562 }
2563 return val & mask;
2564}
2565
c32fa91d
L
2566enum PREFIX_GROUP
2567{
2568 PREFIX_EXIST = 0,
2569 PREFIX_LOCK,
2570 PREFIX_REP,
04ef582a 2571 PREFIX_DS,
c32fa91d
L
2572 PREFIX_OTHER
2573};
2574
2575/* Returns
2576 a. PREFIX_EXIST if attempting to add a prefix where one from the
2577 same class already exists.
2578 b. PREFIX_LOCK if lock prefix is added.
2579 c. PREFIX_REP if rep/repne prefix is added.
04ef582a
L
2580 d. PREFIX_DS if ds prefix is added.
2581 e. PREFIX_OTHER if other prefix is added.
c32fa91d
L
2582 */
2583
2584static enum PREFIX_GROUP
e3bb37b5 2585add_prefix (unsigned int prefix)
252b5132 2586{
c32fa91d 2587 enum PREFIX_GROUP ret = PREFIX_OTHER;
b1905489 2588 unsigned int q;
252b5132 2589
29b0f896
AM
2590 if (prefix >= REX_OPCODE && prefix < REX_OPCODE + 16
2591 && flag_code == CODE_64BIT)
b1905489 2592 {
161a04f6 2593 if ((i.prefix[REX_PREFIX] & prefix & REX_W)
44846f29
JB
2594 || (i.prefix[REX_PREFIX] & prefix & REX_R)
2595 || (i.prefix[REX_PREFIX] & prefix & REX_X)
2596 || (i.prefix[REX_PREFIX] & prefix & REX_B))
c32fa91d 2597 ret = PREFIX_EXIST;
b1905489
JB
2598 q = REX_PREFIX;
2599 }
3e73aa7c 2600 else
b1905489
JB
2601 {
2602 switch (prefix)
2603 {
2604 default:
2605 abort ();
2606
b1905489 2607 case DS_PREFIX_OPCODE:
04ef582a
L
2608 ret = PREFIX_DS;
2609 /* Fall through. */
2610 case CS_PREFIX_OPCODE:
b1905489
JB
2611 case ES_PREFIX_OPCODE:
2612 case FS_PREFIX_OPCODE:
2613 case GS_PREFIX_OPCODE:
2614 case SS_PREFIX_OPCODE:
2615 q = SEG_PREFIX;
2616 break;
2617
2618 case REPNE_PREFIX_OPCODE:
2619 case REPE_PREFIX_OPCODE:
c32fa91d
L
2620 q = REP_PREFIX;
2621 ret = PREFIX_REP;
2622 break;
2623
b1905489 2624 case LOCK_PREFIX_OPCODE:
c32fa91d
L
2625 q = LOCK_PREFIX;
2626 ret = PREFIX_LOCK;
b1905489
JB
2627 break;
2628
2629 case FWAIT_OPCODE:
2630 q = WAIT_PREFIX;
2631 break;
2632
2633 case ADDR_PREFIX_OPCODE:
2634 q = ADDR_PREFIX;
2635 break;
2636
2637 case DATA_PREFIX_OPCODE:
2638 q = DATA_PREFIX;
2639 break;
2640 }
2641 if (i.prefix[q] != 0)
c32fa91d 2642 ret = PREFIX_EXIST;
b1905489 2643 }
252b5132 2644
b1905489 2645 if (ret)
252b5132 2646 {
b1905489
JB
2647 if (!i.prefix[q])
2648 ++i.prefixes;
2649 i.prefix[q] |= prefix;
252b5132 2650 }
b1905489
JB
2651 else
2652 as_bad (_("same type of prefix used twice"));
252b5132 2653
252b5132
RH
2654 return ret;
2655}
2656
2657static void
78f12dd3 2658update_code_flag (int value, int check)
eecb386c 2659{
78f12dd3
L
2660 PRINTF_LIKE ((*as_error));
2661
1e9cc1c2 2662 flag_code = (enum flag_code) value;
40fb9820
L
2663 if (flag_code == CODE_64BIT)
2664 {
2665 cpu_arch_flags.bitfield.cpu64 = 1;
2666 cpu_arch_flags.bitfield.cpuno64 = 0;
40fb9820
L
2667 }
2668 else
2669 {
2670 cpu_arch_flags.bitfield.cpu64 = 0;
2671 cpu_arch_flags.bitfield.cpuno64 = 1;
40fb9820
L
2672 }
2673 if (value == CODE_64BIT && !cpu_arch_flags.bitfield.cpulm )
3e73aa7c 2674 {
78f12dd3
L
2675 if (check)
2676 as_error = as_fatal;
2677 else
2678 as_error = as_bad;
2679 (*as_error) (_("64bit mode not supported on `%s'."),
2680 cpu_arch_name ? cpu_arch_name : default_arch);
3e73aa7c 2681 }
40fb9820 2682 if (value == CODE_32BIT && !cpu_arch_flags.bitfield.cpui386)
3e73aa7c 2683 {
78f12dd3
L
2684 if (check)
2685 as_error = as_fatal;
2686 else
2687 as_error = as_bad;
2688 (*as_error) (_("32bit mode not supported on `%s'."),
2689 cpu_arch_name ? cpu_arch_name : default_arch);
3e73aa7c 2690 }
eecb386c
AM
2691 stackop_size = '\0';
2692}
2693
78f12dd3
L
2694static void
2695set_code_flag (int value)
2696{
2697 update_code_flag (value, 0);
2698}
2699
eecb386c 2700static void
e3bb37b5 2701set_16bit_gcc_code_flag (int new_code_flag)
252b5132 2702{
1e9cc1c2 2703 flag_code = (enum flag_code) new_code_flag;
40fb9820
L
2704 if (flag_code != CODE_16BIT)
2705 abort ();
2706 cpu_arch_flags.bitfield.cpu64 = 0;
2707 cpu_arch_flags.bitfield.cpuno64 = 1;
9306ca4a 2708 stackop_size = LONG_MNEM_SUFFIX;
252b5132
RH
2709}
2710
2711static void
e3bb37b5 2712set_intel_syntax (int syntax_flag)
252b5132
RH
2713{
2714 /* Find out if register prefixing is specified. */
2715 int ask_naked_reg = 0;
2716
2717 SKIP_WHITESPACE ();
29b0f896 2718 if (!is_end_of_line[(unsigned char) *input_line_pointer])
252b5132 2719 {
d02603dc
NC
2720 char *string;
2721 int e = get_symbol_name (&string);
252b5132 2722
47926f60 2723 if (strcmp (string, "prefix") == 0)
252b5132 2724 ask_naked_reg = 1;
47926f60 2725 else if (strcmp (string, "noprefix") == 0)
252b5132
RH
2726 ask_naked_reg = -1;
2727 else
d0b47220 2728 as_bad (_("bad argument to syntax directive."));
d02603dc 2729 (void) restore_line_pointer (e);
252b5132
RH
2730 }
2731 demand_empty_rest_of_line ();
c3332e24 2732
252b5132
RH
2733 intel_syntax = syntax_flag;
2734
2735 if (ask_naked_reg == 0)
f86103b7
AM
2736 allow_naked_reg = (intel_syntax
2737 && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
252b5132
RH
2738 else
2739 allow_naked_reg = (ask_naked_reg < 0);
9306ca4a 2740
ee86248c 2741 expr_set_rank (O_full_ptr, syntax_flag ? 10 : 0);
7ab9ffdd 2742
e4a3b5a4 2743 identifier_chars['%'] = intel_syntax && allow_naked_reg ? '%' : 0;
9306ca4a 2744 identifier_chars['$'] = intel_syntax ? '$' : 0;
e4a3b5a4 2745 register_prefix = allow_naked_reg ? "" : "%";
252b5132
RH
2746}
2747
1efbbeb4
L
2748static void
2749set_intel_mnemonic (int mnemonic_flag)
2750{
e1d4d893 2751 intel_mnemonic = mnemonic_flag;
1efbbeb4
L
2752}
2753
db51cc60
L
2754static void
2755set_allow_index_reg (int flag)
2756{
2757 allow_index_reg = flag;
2758}
2759
cb19c032 2760static void
7bab8ab5 2761set_check (int what)
cb19c032 2762{
7bab8ab5
JB
2763 enum check_kind *kind;
2764 const char *str;
2765
2766 if (what)
2767 {
2768 kind = &operand_check;
2769 str = "operand";
2770 }
2771 else
2772 {
2773 kind = &sse_check;
2774 str = "sse";
2775 }
2776
cb19c032
L
2777 SKIP_WHITESPACE ();
2778
2779 if (!is_end_of_line[(unsigned char) *input_line_pointer])
2780 {
d02603dc
NC
2781 char *string;
2782 int e = get_symbol_name (&string);
cb19c032
L
2783
2784 if (strcmp (string, "none") == 0)
7bab8ab5 2785 *kind = check_none;
cb19c032 2786 else if (strcmp (string, "warning") == 0)
7bab8ab5 2787 *kind = check_warning;
cb19c032 2788 else if (strcmp (string, "error") == 0)
7bab8ab5 2789 *kind = check_error;
cb19c032 2790 else
7bab8ab5 2791 as_bad (_("bad argument to %s_check directive."), str);
d02603dc 2792 (void) restore_line_pointer (e);
cb19c032
L
2793 }
2794 else
7bab8ab5 2795 as_bad (_("missing argument for %s_check directive"), str);
cb19c032
L
2796
2797 demand_empty_rest_of_line ();
2798}
2799
8a9036a4
L
2800static void
2801check_cpu_arch_compatible (const char *name ATTRIBUTE_UNUSED,
1e9cc1c2 2802 i386_cpu_flags new_flag ATTRIBUTE_UNUSED)
8a9036a4
L
2803{
2804#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2805 static const char *arch;
2806
2807 /* Intel LIOM is only supported on ELF. */
2808 if (!IS_ELF)
2809 return;
2810
2811 if (!arch)
2812 {
2813 /* Use cpu_arch_name if it is set in md_parse_option. Otherwise
2814 use default_arch. */
2815 arch = cpu_arch_name;
2816 if (!arch)
2817 arch = default_arch;
2818 }
2819
81486035
L
2820 /* If we are targeting Intel MCU, we must enable it. */
2821 if (get_elf_backend_data (stdoutput)->elf_machine_code != EM_IAMCU
2822 || new_flag.bitfield.cpuiamcu)
2823 return;
2824
3632d14b 2825 /* If we are targeting Intel L1OM, we must enable it. */
8a9036a4 2826 if (get_elf_backend_data (stdoutput)->elf_machine_code != EM_L1OM
1e9cc1c2 2827 || new_flag.bitfield.cpul1om)
8a9036a4 2828 return;
76ba9986 2829
7a9068fe
L
2830 /* If we are targeting Intel K1OM, we must enable it. */
2831 if (get_elf_backend_data (stdoutput)->elf_machine_code != EM_K1OM
2832 || new_flag.bitfield.cpuk1om)
2833 return;
2834
8a9036a4
L
2835 as_bad (_("`%s' is not supported on `%s'"), name, arch);
2836#endif
2837}
2838
e413e4e9 2839static void
e3bb37b5 2840set_cpu_arch (int dummy ATTRIBUTE_UNUSED)
e413e4e9 2841{
47926f60 2842 SKIP_WHITESPACE ();
e413e4e9 2843
29b0f896 2844 if (!is_end_of_line[(unsigned char) *input_line_pointer])
e413e4e9 2845 {
d02603dc
NC
2846 char *string;
2847 int e = get_symbol_name (&string);
91d6fa6a 2848 unsigned int j;
40fb9820 2849 i386_cpu_flags flags;
e413e4e9 2850
91d6fa6a 2851 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
e413e4e9 2852 {
91d6fa6a 2853 if (strcmp (string, cpu_arch[j].name) == 0)
e413e4e9 2854 {
91d6fa6a 2855 check_cpu_arch_compatible (string, cpu_arch[j].flags);
8a9036a4 2856
5c6af06e
JB
2857 if (*string != '.')
2858 {
91d6fa6a 2859 cpu_arch_name = cpu_arch[j].name;
5c6af06e 2860 cpu_sub_arch_name = NULL;
91d6fa6a 2861 cpu_arch_flags = cpu_arch[j].flags;
40fb9820
L
2862 if (flag_code == CODE_64BIT)
2863 {
2864 cpu_arch_flags.bitfield.cpu64 = 1;
2865 cpu_arch_flags.bitfield.cpuno64 = 0;
2866 }
2867 else
2868 {
2869 cpu_arch_flags.bitfield.cpu64 = 0;
2870 cpu_arch_flags.bitfield.cpuno64 = 1;
2871 }
91d6fa6a
NC
2872 cpu_arch_isa = cpu_arch[j].type;
2873 cpu_arch_isa_flags = cpu_arch[j].flags;
ccc9c027
L
2874 if (!cpu_arch_tune_set)
2875 {
2876 cpu_arch_tune = cpu_arch_isa;
2877 cpu_arch_tune_flags = cpu_arch_isa_flags;
2878 }
5c6af06e
JB
2879 break;
2880 }
40fb9820 2881
293f5f65
L
2882 flags = cpu_flags_or (cpu_arch_flags,
2883 cpu_arch[j].flags);
81486035 2884
5b64d091 2885 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
5c6af06e 2886 {
6305a203
L
2887 if (cpu_sub_arch_name)
2888 {
2889 char *name = cpu_sub_arch_name;
2890 cpu_sub_arch_name = concat (name,
91d6fa6a 2891 cpu_arch[j].name,
1bf57e9f 2892 (const char *) NULL);
6305a203
L
2893 free (name);
2894 }
2895 else
91d6fa6a 2896 cpu_sub_arch_name = xstrdup (cpu_arch[j].name);
40fb9820 2897 cpu_arch_flags = flags;
a586129e 2898 cpu_arch_isa_flags = flags;
5c6af06e 2899 }
0089dace
L
2900 else
2901 cpu_arch_isa_flags
2902 = cpu_flags_or (cpu_arch_isa_flags,
2903 cpu_arch[j].flags);
d02603dc 2904 (void) restore_line_pointer (e);
5c6af06e
JB
2905 demand_empty_rest_of_line ();
2906 return;
e413e4e9
AM
2907 }
2908 }
293f5f65
L
2909
2910 if (*string == '.' && j >= ARRAY_SIZE (cpu_arch))
2911 {
33eaf5de 2912 /* Disable an ISA extension. */
293f5f65
L
2913 for (j = 0; j < ARRAY_SIZE (cpu_noarch); j++)
2914 if (strcmp (string + 1, cpu_noarch [j].name) == 0)
2915 {
2916 flags = cpu_flags_and_not (cpu_arch_flags,
2917 cpu_noarch[j].flags);
2918 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
2919 {
2920 if (cpu_sub_arch_name)
2921 {
2922 char *name = cpu_sub_arch_name;
2923 cpu_sub_arch_name = concat (name, string,
2924 (const char *) NULL);
2925 free (name);
2926 }
2927 else
2928 cpu_sub_arch_name = xstrdup (string);
2929 cpu_arch_flags = flags;
2930 cpu_arch_isa_flags = flags;
2931 }
2932 (void) restore_line_pointer (e);
2933 demand_empty_rest_of_line ();
2934 return;
2935 }
2936
2937 j = ARRAY_SIZE (cpu_arch);
2938 }
2939
91d6fa6a 2940 if (j >= ARRAY_SIZE (cpu_arch))
e413e4e9
AM
2941 as_bad (_("no such architecture: `%s'"), string);
2942
2943 *input_line_pointer = e;
2944 }
2945 else
2946 as_bad (_("missing cpu architecture"));
2947
fddf5b5b
AM
2948 no_cond_jump_promotion = 0;
2949 if (*input_line_pointer == ','
29b0f896 2950 && !is_end_of_line[(unsigned char) input_line_pointer[1]])
fddf5b5b 2951 {
d02603dc
NC
2952 char *string;
2953 char e;
2954
2955 ++input_line_pointer;
2956 e = get_symbol_name (&string);
fddf5b5b
AM
2957
2958 if (strcmp (string, "nojumps") == 0)
2959 no_cond_jump_promotion = 1;
2960 else if (strcmp (string, "jumps") == 0)
2961 ;
2962 else
2963 as_bad (_("no such architecture modifier: `%s'"), string);
2964
d02603dc 2965 (void) restore_line_pointer (e);
fddf5b5b
AM
2966 }
2967
e413e4e9
AM
2968 demand_empty_rest_of_line ();
2969}
2970
8a9036a4
L
2971enum bfd_architecture
2972i386_arch (void)
2973{
3632d14b 2974 if (cpu_arch_isa == PROCESSOR_L1OM)
8a9036a4
L
2975 {
2976 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2977 || flag_code != CODE_64BIT)
2978 as_fatal (_("Intel L1OM is 64bit ELF only"));
2979 return bfd_arch_l1om;
2980 }
7a9068fe
L
2981 else if (cpu_arch_isa == PROCESSOR_K1OM)
2982 {
2983 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2984 || flag_code != CODE_64BIT)
2985 as_fatal (_("Intel K1OM is 64bit ELF only"));
2986 return bfd_arch_k1om;
2987 }
81486035
L
2988 else if (cpu_arch_isa == PROCESSOR_IAMCU)
2989 {
2990 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2991 || flag_code == CODE_64BIT)
2992 as_fatal (_("Intel MCU is 32bit ELF only"));
2993 return bfd_arch_iamcu;
2994 }
8a9036a4
L
2995 else
2996 return bfd_arch_i386;
2997}
2998
b9d79e03 2999unsigned long
7016a5d5 3000i386_mach (void)
b9d79e03 3001{
d34049e8 3002 if (startswith (default_arch, "x86_64"))
8a9036a4 3003 {
3632d14b 3004 if (cpu_arch_isa == PROCESSOR_L1OM)
8a9036a4 3005 {
351f65ca
L
3006 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
3007 || default_arch[6] != '\0')
8a9036a4
L
3008 as_fatal (_("Intel L1OM is 64bit ELF only"));
3009 return bfd_mach_l1om;
3010 }
7a9068fe
L
3011 else if (cpu_arch_isa == PROCESSOR_K1OM)
3012 {
3013 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
3014 || default_arch[6] != '\0')
3015 as_fatal (_("Intel K1OM is 64bit ELF only"));
3016 return bfd_mach_k1om;
3017 }
351f65ca 3018 else if (default_arch[6] == '\0')
8a9036a4 3019 return bfd_mach_x86_64;
351f65ca
L
3020 else
3021 return bfd_mach_x64_32;
8a9036a4 3022 }
5197d474
L
3023 else if (!strcmp (default_arch, "i386")
3024 || !strcmp (default_arch, "iamcu"))
81486035
L
3025 {
3026 if (cpu_arch_isa == PROCESSOR_IAMCU)
3027 {
3028 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
3029 as_fatal (_("Intel MCU is 32bit ELF only"));
3030 return bfd_mach_i386_iamcu;
3031 }
3032 else
3033 return bfd_mach_i386_i386;
3034 }
b9d79e03 3035 else
2b5d6a91 3036 as_fatal (_("unknown architecture"));
b9d79e03 3037}
b9d79e03 3038\f
252b5132 3039void
7016a5d5 3040md_begin (void)
252b5132 3041{
86fa6981
L
3042 /* Support pseudo prefixes like {disp32}. */
3043 lex_type ['{'] = LEX_BEGIN_NAME;
3044
47926f60 3045 /* Initialize op_hash hash table. */
629310ab 3046 op_hash = str_htab_create ();
252b5132
RH
3047
3048 {
d3ce72d0 3049 const insn_template *optab;
29b0f896 3050 templates *core_optab;
252b5132 3051
47926f60
KH
3052 /* Setup for loop. */
3053 optab = i386_optab;
add39d23 3054 core_optab = XNEW (templates);
252b5132
RH
3055 core_optab->start = optab;
3056
3057 while (1)
3058 {
3059 ++optab;
3060 if (optab->name == NULL
3061 || strcmp (optab->name, (optab - 1)->name) != 0)
3062 {
3063 /* different name --> ship out current template list;
47926f60 3064 add to hash table; & begin anew. */
252b5132 3065 core_optab->end = optab;
fe0e921f
AM
3066 if (str_hash_insert (op_hash, (optab - 1)->name, core_optab, 0))
3067 as_fatal (_("duplicate %s"), (optab - 1)->name);
3068
252b5132
RH
3069 if (optab->name == NULL)
3070 break;
add39d23 3071 core_optab = XNEW (templates);
252b5132
RH
3072 core_optab->start = optab;
3073 }
3074 }
3075 }
3076
47926f60 3077 /* Initialize reg_hash hash table. */
629310ab 3078 reg_hash = str_htab_create ();
252b5132 3079 {
29b0f896 3080 const reg_entry *regtab;
c3fe08fa 3081 unsigned int regtab_size = i386_regtab_size;
252b5132 3082
c3fe08fa 3083 for (regtab = i386_regtab; regtab_size--; regtab++)
6225c532 3084 {
6288d05f
JB
3085 switch (regtab->reg_type.bitfield.class)
3086 {
3087 case Reg:
34684862
JB
3088 if (regtab->reg_type.bitfield.dword)
3089 {
3090 if (regtab->reg_type.bitfield.instance == Accum)
3091 reg_eax = regtab;
3092 }
3093 else if (regtab->reg_type.bitfield.tbyte)
6288d05f
JB
3094 {
3095 /* There's no point inserting st(<N>) in the hash table, as
3096 parentheses aren't included in register_chars[] anyway. */
3097 if (regtab->reg_type.bitfield.instance != Accum)
3098 continue;
3099 reg_st0 = regtab;
3100 }
3101 break;
3102
5e042380
JB
3103 case SReg:
3104 switch (regtab->reg_num)
3105 {
3106 case 0: reg_es = regtab; break;
3107 case 2: reg_ss = regtab; break;
3108 case 3: reg_ds = regtab; break;
3109 }
3110 break;
3111
6288d05f
JB
3112 case RegMask:
3113 if (!regtab->reg_num)
3114 reg_k0 = regtab;
3115 break;
3116 }
3117
6225c532
JB
3118 if (str_hash_insert (reg_hash, regtab->reg_name, regtab, 0) != NULL)
3119 as_fatal (_("duplicate %s"), regtab->reg_name);
6225c532 3120 }
252b5132
RH
3121 }
3122
47926f60 3123 /* Fill in lexical tables: mnemonic_chars, operand_chars. */
252b5132 3124 {
29b0f896
AM
3125 int c;
3126 char *p;
252b5132
RH
3127
3128 for (c = 0; c < 256; c++)
3129 {
3882b010 3130 if (ISDIGIT (c))
252b5132
RH
3131 {
3132 digit_chars[c] = c;
3133 mnemonic_chars[c] = c;
3134 register_chars[c] = c;
3135 operand_chars[c] = c;
3136 }
3882b010 3137 else if (ISLOWER (c))
252b5132
RH
3138 {
3139 mnemonic_chars[c] = c;
3140 register_chars[c] = c;
3141 operand_chars[c] = c;
3142 }
3882b010 3143 else if (ISUPPER (c))
252b5132 3144 {
3882b010 3145 mnemonic_chars[c] = TOLOWER (c);
252b5132
RH
3146 register_chars[c] = mnemonic_chars[c];
3147 operand_chars[c] = c;
3148 }
43234a1e 3149 else if (c == '{' || c == '}')
86fa6981
L
3150 {
3151 mnemonic_chars[c] = c;
3152 operand_chars[c] = c;
3153 }
b3983e5f
JB
3154#ifdef SVR4_COMMENT_CHARS
3155 else if (c == '\\' && strchr (i386_comment_chars, '/'))
3156 operand_chars[c] = c;
3157#endif
252b5132 3158
3882b010 3159 if (ISALPHA (c) || ISDIGIT (c))
252b5132
RH
3160 identifier_chars[c] = c;
3161 else if (c >= 128)
3162 {
3163 identifier_chars[c] = c;
3164 operand_chars[c] = c;
3165 }
3166 }
3167
3168#ifdef LEX_AT
3169 identifier_chars['@'] = '@';
32137342
NC
3170#endif
3171#ifdef LEX_QM
3172 identifier_chars['?'] = '?';
3173 operand_chars['?'] = '?';
252b5132 3174#endif
252b5132 3175 digit_chars['-'] = '-';
c0f3af97 3176 mnemonic_chars['_'] = '_';
791fe849 3177 mnemonic_chars['-'] = '-';
0003779b 3178 mnemonic_chars['.'] = '.';
252b5132
RH
3179 identifier_chars['_'] = '_';
3180 identifier_chars['.'] = '.';
3181
3182 for (p = operand_special_chars; *p != '\0'; p++)
3183 operand_chars[(unsigned char) *p] = *p;
3184 }
3185
a4447b93
RH
3186 if (flag_code == CODE_64BIT)
3187 {
ca19b261
KT
3188#if defined (OBJ_COFF) && defined (TE_PE)
3189 x86_dwarf2_return_column = (OUTPUT_FLAVOR == bfd_target_coff_flavour
3190 ? 32 : 16);
3191#else
a4447b93 3192 x86_dwarf2_return_column = 16;
ca19b261 3193#endif
61ff971f 3194 x86_cie_data_alignment = -8;
a4447b93
RH
3195 }
3196 else
3197 {
3198 x86_dwarf2_return_column = 8;
3199 x86_cie_data_alignment = -4;
3200 }
e379e5f3
L
3201
3202 /* NB: FUSED_JCC_PADDING frag must have sufficient room so that it
3203 can be turned into BRANCH_PREFIX frag. */
3204 if (align_branch_prefix_size > MAX_FUSED_JCC_PADDING_SIZE)
3205 abort ();
252b5132
RH
3206}
3207
3208void
e3bb37b5 3209i386_print_statistics (FILE *file)
252b5132 3210{
629310ab
ML
3211 htab_print_statistics (file, "i386 opcode", op_hash);
3212 htab_print_statistics (file, "i386 register", reg_hash);
252b5132
RH
3213}
3214\f
252b5132
RH
3215#ifdef DEBUG386
3216
ce8a8b2f 3217/* Debugging routines for md_assemble. */
d3ce72d0 3218static void pte (insn_template *);
40fb9820 3219static void pt (i386_operand_type);
e3bb37b5
L
3220static void pe (expressionS *);
3221static void ps (symbolS *);
252b5132
RH
3222
3223static void
2c703856 3224pi (const char *line, i386_insn *x)
252b5132 3225{
09137c09 3226 unsigned int j;
252b5132
RH
3227
3228 fprintf (stdout, "%s: template ", line);
3229 pte (&x->tm);
09f131f2
JH
3230 fprintf (stdout, " address: base %s index %s scale %x\n",
3231 x->base_reg ? x->base_reg->reg_name : "none",
3232 x->index_reg ? x->index_reg->reg_name : "none",
3233 x->log2_scale_factor);
3234 fprintf (stdout, " modrm: mode %x reg %x reg/mem %x\n",
252b5132 3235 x->rm.mode, x->rm.reg, x->rm.regmem);
09f131f2
JH
3236 fprintf (stdout, " sib: base %x index %x scale %x\n",
3237 x->sib.base, x->sib.index, x->sib.scale);
3238 fprintf (stdout, " rex: 64bit %x extX %x extY %x extZ %x\n",
161a04f6
L
3239 (x->rex & REX_W) != 0,
3240 (x->rex & REX_R) != 0,
3241 (x->rex & REX_X) != 0,
3242 (x->rex & REX_B) != 0);
09137c09 3243 for (j = 0; j < x->operands; j++)
252b5132 3244 {
09137c09
SP
3245 fprintf (stdout, " #%d: ", j + 1);
3246 pt (x->types[j]);
252b5132 3247 fprintf (stdout, "\n");
bab6aec1 3248 if (x->types[j].bitfield.class == Reg
3528c362
JB
3249 || x->types[j].bitfield.class == RegMMX
3250 || x->types[j].bitfield.class == RegSIMD
dd6b8a0b 3251 || x->types[j].bitfield.class == RegMask
00cee14f 3252 || x->types[j].bitfield.class == SReg
4a5c67ed
JB
3253 || x->types[j].bitfield.class == RegCR
3254 || x->types[j].bitfield.class == RegDR
dd6b8a0b
JB
3255 || x->types[j].bitfield.class == RegTR
3256 || x->types[j].bitfield.class == RegBND)
09137c09
SP
3257 fprintf (stdout, "%s\n", x->op[j].regs->reg_name);
3258 if (operand_type_check (x->types[j], imm))
3259 pe (x->op[j].imms);
3260 if (operand_type_check (x->types[j], disp))
3261 pe (x->op[j].disps);
252b5132
RH
3262 }
3263}
3264
3265static void
d3ce72d0 3266pte (insn_template *t)
252b5132 3267{
b933fa4b 3268 static const unsigned char opc_pfx[] = { 0, 0x66, 0xf3, 0xf2 };
441f6aca
JB
3269 static const char *const opc_spc[] = {
3270 NULL, "0f", "0f38", "0f3a", NULL, NULL, NULL, NULL,
3271 "XOP08", "XOP09", "XOP0A",
3272 };
09137c09 3273 unsigned int j;
441f6aca 3274
252b5132 3275 fprintf (stdout, " %d operands ", t->operands);
441f6aca
JB
3276 if (opc_pfx[t->opcode_modifier.opcodeprefix])
3277 fprintf (stdout, "pfx %x ", opc_pfx[t->opcode_modifier.opcodeprefix]);
3278 if (opc_spc[t->opcode_modifier.opcodespace])
3279 fprintf (stdout, "space %s ", opc_spc[t->opcode_modifier.opcodespace]);
47926f60 3280 fprintf (stdout, "opcode %x ", t->base_opcode);
252b5132
RH
3281 if (t->extension_opcode != None)
3282 fprintf (stdout, "ext %x ", t->extension_opcode);
40fb9820 3283 if (t->opcode_modifier.d)
252b5132 3284 fprintf (stdout, "D");
40fb9820 3285 if (t->opcode_modifier.w)
252b5132
RH
3286 fprintf (stdout, "W");
3287 fprintf (stdout, "\n");
09137c09 3288 for (j = 0; j < t->operands; j++)
252b5132 3289 {
09137c09
SP
3290 fprintf (stdout, " #%d type ", j + 1);
3291 pt (t->operand_types[j]);
252b5132
RH
3292 fprintf (stdout, "\n");
3293 }
3294}
3295
3296static void
e3bb37b5 3297pe (expressionS *e)
252b5132 3298{
24eab124 3299 fprintf (stdout, " operation %d\n", e->X_op);
7b025ee8
JB
3300 fprintf (stdout, " add_number %" BFD_VMA_FMT "d (%" BFD_VMA_FMT "x)\n",
3301 e->X_add_number, e->X_add_number);
252b5132
RH
3302 if (e->X_add_symbol)
3303 {
3304 fprintf (stdout, " add_symbol ");
3305 ps (e->X_add_symbol);
3306 fprintf (stdout, "\n");
3307 }
3308 if (e->X_op_symbol)
3309 {
3310 fprintf (stdout, " op_symbol ");
3311 ps (e->X_op_symbol);
3312 fprintf (stdout, "\n");
3313 }
3314}
3315
3316static void
e3bb37b5 3317ps (symbolS *s)
252b5132
RH
3318{
3319 fprintf (stdout, "%s type %s%s",
3320 S_GET_NAME (s),
3321 S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
3322 segment_name (S_GET_SEGMENT (s)));
3323}
3324
7b81dfbb 3325static struct type_name
252b5132 3326 {
40fb9820
L
3327 i386_operand_type mask;
3328 const char *name;
252b5132 3329 }
7b81dfbb 3330const type_names[] =
252b5132 3331{
40fb9820
L
3332 { OPERAND_TYPE_REG8, "r8" },
3333 { OPERAND_TYPE_REG16, "r16" },
3334 { OPERAND_TYPE_REG32, "r32" },
3335 { OPERAND_TYPE_REG64, "r64" },
2c703856
JB
3336 { OPERAND_TYPE_ACC8, "acc8" },
3337 { OPERAND_TYPE_ACC16, "acc16" },
3338 { OPERAND_TYPE_ACC32, "acc32" },
3339 { OPERAND_TYPE_ACC64, "acc64" },
40fb9820
L
3340 { OPERAND_TYPE_IMM8, "i8" },
3341 { OPERAND_TYPE_IMM8, "i8s" },
3342 { OPERAND_TYPE_IMM16, "i16" },
3343 { OPERAND_TYPE_IMM32, "i32" },
3344 { OPERAND_TYPE_IMM32S, "i32s" },
3345 { OPERAND_TYPE_IMM64, "i64" },
3346 { OPERAND_TYPE_IMM1, "i1" },
3347 { OPERAND_TYPE_BASEINDEX, "BaseIndex" },
3348 { OPERAND_TYPE_DISP8, "d8" },
3349 { OPERAND_TYPE_DISP16, "d16" },
3350 { OPERAND_TYPE_DISP32, "d32" },
3351 { OPERAND_TYPE_DISP32S, "d32s" },
3352 { OPERAND_TYPE_DISP64, "d64" },
3353 { OPERAND_TYPE_INOUTPORTREG, "InOutPortReg" },
3354 { OPERAND_TYPE_SHIFTCOUNT, "ShiftCount" },
3355 { OPERAND_TYPE_CONTROL, "control reg" },
3356 { OPERAND_TYPE_TEST, "test reg" },
3357 { OPERAND_TYPE_DEBUG, "debug reg" },
3358 { OPERAND_TYPE_FLOATREG, "FReg" },
3359 { OPERAND_TYPE_FLOATACC, "FAcc" },
21df382b 3360 { OPERAND_TYPE_SREG, "SReg" },
40fb9820
L
3361 { OPERAND_TYPE_REGMMX, "rMMX" },
3362 { OPERAND_TYPE_REGXMM, "rXMM" },
0349dc08 3363 { OPERAND_TYPE_REGYMM, "rYMM" },
43234a1e 3364 { OPERAND_TYPE_REGZMM, "rZMM" },
260cd341 3365 { OPERAND_TYPE_REGTMM, "rTMM" },
43234a1e 3366 { OPERAND_TYPE_REGMASK, "Mask reg" },
252b5132
RH
3367};
3368
3369static void
40fb9820 3370pt (i386_operand_type t)
252b5132 3371{
40fb9820 3372 unsigned int j;
c6fb90c8 3373 i386_operand_type a;
252b5132 3374
40fb9820 3375 for (j = 0; j < ARRAY_SIZE (type_names); j++)
c6fb90c8
L
3376 {
3377 a = operand_type_and (t, type_names[j].mask);
2c703856 3378 if (operand_type_equal (&a, &type_names[j].mask))
c6fb90c8
L
3379 fprintf (stdout, "%s, ", type_names[j].name);
3380 }
252b5132
RH
3381 fflush (stdout);
3382}
3383
3384#endif /* DEBUG386 */
3385\f
252b5132 3386static bfd_reloc_code_real_type
3956db08 3387reloc (unsigned int size,
64e74474
AM
3388 int pcrel,
3389 int sign,
3390 bfd_reloc_code_real_type other)
252b5132 3391{
47926f60 3392 if (other != NO_RELOC)
3956db08 3393 {
91d6fa6a 3394 reloc_howto_type *rel;
3956db08
JB
3395
3396 if (size == 8)
3397 switch (other)
3398 {
64e74474
AM
3399 case BFD_RELOC_X86_64_GOT32:
3400 return BFD_RELOC_X86_64_GOT64;
3401 break;
553d1284
L
3402 case BFD_RELOC_X86_64_GOTPLT64:
3403 return BFD_RELOC_X86_64_GOTPLT64;
3404 break;
64e74474
AM
3405 case BFD_RELOC_X86_64_PLTOFF64:
3406 return BFD_RELOC_X86_64_PLTOFF64;
3407 break;
3408 case BFD_RELOC_X86_64_GOTPC32:
3409 other = BFD_RELOC_X86_64_GOTPC64;
3410 break;
3411 case BFD_RELOC_X86_64_GOTPCREL:
3412 other = BFD_RELOC_X86_64_GOTPCREL64;
3413 break;
3414 case BFD_RELOC_X86_64_TPOFF32:
3415 other = BFD_RELOC_X86_64_TPOFF64;
3416 break;
3417 case BFD_RELOC_X86_64_DTPOFF32:
3418 other = BFD_RELOC_X86_64_DTPOFF64;
3419 break;
3420 default:
3421 break;
3956db08 3422 }
e05278af 3423
8ce3d284 3424#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
3425 if (other == BFD_RELOC_SIZE32)
3426 {
3427 if (size == 8)
1ab668bf 3428 other = BFD_RELOC_SIZE64;
8fd4256d 3429 if (pcrel)
1ab668bf
AM
3430 {
3431 as_bad (_("there are no pc-relative size relocations"));
3432 return NO_RELOC;
3433 }
8fd4256d 3434 }
8ce3d284 3435#endif
8fd4256d 3436
e05278af 3437 /* Sign-checking 4-byte relocations in 16-/32-bit code is pointless. */
f2d8a97c 3438 if (size == 4 && (flag_code != CODE_64BIT || disallow_64bit_reloc))
e05278af
JB
3439 sign = -1;
3440
91d6fa6a
NC
3441 rel = bfd_reloc_type_lookup (stdoutput, other);
3442 if (!rel)
3956db08 3443 as_bad (_("unknown relocation (%u)"), other);
91d6fa6a 3444 else if (size != bfd_get_reloc_size (rel))
3956db08 3445 as_bad (_("%u-byte relocation cannot be applied to %u-byte field"),
91d6fa6a 3446 bfd_get_reloc_size (rel),
3956db08 3447 size);
91d6fa6a 3448 else if (pcrel && !rel->pc_relative)
3956db08 3449 as_bad (_("non-pc-relative relocation for pc-relative field"));
91d6fa6a 3450 else if ((rel->complain_on_overflow == complain_overflow_signed
3956db08 3451 && !sign)
91d6fa6a 3452 || (rel->complain_on_overflow == complain_overflow_unsigned
64e74474 3453 && sign > 0))
3956db08
JB
3454 as_bad (_("relocated field and relocation type differ in signedness"));
3455 else
3456 return other;
3457 return NO_RELOC;
3458 }
252b5132
RH
3459
3460 if (pcrel)
3461 {
3e73aa7c 3462 if (!sign)
3956db08 3463 as_bad (_("there are no unsigned pc-relative relocations"));
252b5132
RH
3464 switch (size)
3465 {
3466 case 1: return BFD_RELOC_8_PCREL;
3467 case 2: return BFD_RELOC_16_PCREL;
d258b828 3468 case 4: return BFD_RELOC_32_PCREL;
d6ab8113 3469 case 8: return BFD_RELOC_64_PCREL;
252b5132 3470 }
3956db08 3471 as_bad (_("cannot do %u byte pc-relative relocation"), size);
252b5132
RH
3472 }
3473 else
3474 {
3956db08 3475 if (sign > 0)
e5cb08ac 3476 switch (size)
3e73aa7c
JH
3477 {
3478 case 4: return BFD_RELOC_X86_64_32S;
3479 }
3480 else
3481 switch (size)
3482 {
3483 case 1: return BFD_RELOC_8;
3484 case 2: return BFD_RELOC_16;
3485 case 4: return BFD_RELOC_32;
3486 case 8: return BFD_RELOC_64;
3487 }
3956db08
JB
3488 as_bad (_("cannot do %s %u byte relocation"),
3489 sign > 0 ? "signed" : "unsigned", size);
252b5132
RH
3490 }
3491
0cc9e1d3 3492 return NO_RELOC;
252b5132
RH
3493}
3494
47926f60
KH
3495/* Here we decide which fixups can be adjusted to make them relative to
3496 the beginning of the section instead of the symbol. Basically we need
3497 to make sure that the dynamic relocations are done correctly, so in
3498 some cases we force the original symbol to be used. */
3499
252b5132 3500int
e3bb37b5 3501tc_i386_fix_adjustable (fixS *fixP ATTRIBUTE_UNUSED)
252b5132 3502{
6d249963 3503#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 3504 if (!IS_ELF)
31312f95
AM
3505 return 1;
3506
a161fe53
AM
3507 /* Don't adjust pc-relative references to merge sections in 64-bit
3508 mode. */
3509 if (use_rela_relocations
3510 && (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0
3511 && fixP->fx_pcrel)
252b5132 3512 return 0;
31312f95 3513
8d01d9a9
AJ
3514 /* The x86_64 GOTPCREL are represented as 32bit PCrel relocations
3515 and changed later by validate_fix. */
3516 if (GOT_symbol && fixP->fx_subsy == GOT_symbol
3517 && fixP->fx_r_type == BFD_RELOC_32_PCREL)
3518 return 0;
3519
8fd4256d
L
3520 /* Adjust_reloc_syms doesn't know about the GOT. Need to keep symbol
3521 for size relocations. */
3522 if (fixP->fx_r_type == BFD_RELOC_SIZE32
3523 || fixP->fx_r_type == BFD_RELOC_SIZE64
3524 || fixP->fx_r_type == BFD_RELOC_386_GOTOFF
252b5132 3525 || fixP->fx_r_type == BFD_RELOC_386_GOT32
02a86693 3526 || fixP->fx_r_type == BFD_RELOC_386_GOT32X
13ae64f3
JJ
3527 || fixP->fx_r_type == BFD_RELOC_386_TLS_GD
3528 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDM
3529 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDO_32
3530 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE_32
37e55690
JJ
3531 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE
3532 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTIE
13ae64f3
JJ
3533 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE_32
3534 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE
67a4f2b7
AO
3535 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTDESC
3536 || fixP->fx_r_type == BFD_RELOC_386_TLS_DESC_CALL
3e73aa7c 3537 || fixP->fx_r_type == BFD_RELOC_X86_64_GOT32
80b3ee89 3538 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCREL
56ceb5b5
L
3539 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCRELX
3540 || fixP->fx_r_type == BFD_RELOC_X86_64_REX_GOTPCRELX
bffbf940
JJ
3541 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSGD
3542 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSLD
3543 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF32
d6ab8113 3544 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF64
bffbf940
JJ
3545 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF
3546 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF32
d6ab8113
JB
3547 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF64
3548 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTOFF64
67a4f2b7
AO
3549 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
3550 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSDESC_CALL
252b5132
RH
3551 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3552 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3553 return 0;
31312f95 3554#endif
252b5132
RH
3555 return 1;
3556}
252b5132 3557
a9aabc23
JB
3558static INLINE bool
3559want_disp32 (const insn_template *t)
3560{
3561 return flag_code != CODE_64BIT
3562 || i.prefix[ADDR_PREFIX]
3563 || (t->base_opcode == 0x8d
3564 && t->opcode_modifier.opcodespace == SPACE_BASE
3565 && !i.types[1].bitfield.qword);
3566}
3567
b4cac588 3568static int
e3bb37b5 3569intel_float_operand (const char *mnemonic)
252b5132 3570{
9306ca4a
JB
3571 /* Note that the value returned is meaningful only for opcodes with (memory)
3572 operands, hence the code here is free to improperly handle opcodes that
3573 have no operands (for better performance and smaller code). */
3574
3575 if (mnemonic[0] != 'f')
3576 return 0; /* non-math */
3577
3578 switch (mnemonic[1])
3579 {
3580 /* fclex, fdecstp, fdisi, femms, feni, fincstp, finit, fsetpm, and
3581 the fs segment override prefix not currently handled because no
3582 call path can make opcodes without operands get here */
3583 case 'i':
3584 return 2 /* integer op */;
3585 case 'l':
3586 if (mnemonic[2] == 'd' && (mnemonic[3] == 'c' || mnemonic[3] == 'e'))
3587 return 3; /* fldcw/fldenv */
3588 break;
3589 case 'n':
3590 if (mnemonic[2] != 'o' /* fnop */)
3591 return 3; /* non-waiting control op */
3592 break;
3593 case 'r':
3594 if (mnemonic[2] == 's')
3595 return 3; /* frstor/frstpm */
3596 break;
3597 case 's':
3598 if (mnemonic[2] == 'a')
3599 return 3; /* fsave */
3600 if (mnemonic[2] == 't')
3601 {
3602 switch (mnemonic[3])
3603 {
3604 case 'c': /* fstcw */
3605 case 'd': /* fstdw */
3606 case 'e': /* fstenv */
3607 case 's': /* fsts[gw] */
3608 return 3;
3609 }
3610 }
3611 break;
3612 case 'x':
3613 if (mnemonic[2] == 'r' || mnemonic[2] == 's')
3614 return 0; /* fxsave/fxrstor are not really math ops */
3615 break;
3616 }
252b5132 3617
9306ca4a 3618 return 1;
252b5132
RH
3619}
3620
9a182d04
JB
3621static INLINE void
3622install_template (const insn_template *t)
3623{
3624 unsigned int l;
3625
3626 i.tm = *t;
3627
3628 /* Note that for pseudo prefixes this produces a length of 1. But for them
3629 the length isn't interesting at all. */
3630 for (l = 1; l < 4; ++l)
3631 if (!(t->base_opcode >> (8 * l)))
3632 break;
3633
3634 i.opcode_length = l;
3635}
3636
c0f3af97
L
3637/* Build the VEX prefix. */
3638
3639static void
d3ce72d0 3640build_vex_prefix (const insn_template *t)
c0f3af97
L
3641{
3642 unsigned int register_specifier;
c0f3af97 3643 unsigned int vector_length;
03751133 3644 unsigned int w;
c0f3af97
L
3645
3646 /* Check register specifier. */
3647 if (i.vex.register_specifier)
43234a1e
L
3648 {
3649 register_specifier =
3650 ~register_number (i.vex.register_specifier) & 0xf;
3651 gas_assert ((i.vex.register_specifier->reg_flags & RegVRex) == 0);
3652 }
c0f3af97
L
3653 else
3654 register_specifier = 0xf;
3655
79f0fa25
L
3656 /* Use 2-byte VEX prefix by swapping destination and source operand
3657 if there are more than 1 register operand. */
3658 if (i.reg_operands > 1
3659 && i.vec_encoding != vex_encoding_vex3
86fa6981 3660 && i.dir_encoding == dir_encoding_default
fa99fab2 3661 && i.operands == i.reg_operands
dbbc8b7e 3662 && operand_type_equal (&i.types[0], &i.types[i.operands - 1])
441f6aca 3663 && i.tm.opcode_modifier.opcodespace == SPACE_0F
dbbc8b7e 3664 && (i.tm.opcode_modifier.load || i.tm.opcode_modifier.d)
fa99fab2
L
3665 && i.rex == REX_B)
3666 {
3667 unsigned int xchg = i.operands - 1;
3668 union i386_op temp_op;
3669 i386_operand_type temp_type;
3670
3671 temp_type = i.types[xchg];
3672 i.types[xchg] = i.types[0];
3673 i.types[0] = temp_type;
3674 temp_op = i.op[xchg];
3675 i.op[xchg] = i.op[0];
3676 i.op[0] = temp_op;
3677
9c2799c2 3678 gas_assert (i.rm.mode == 3);
fa99fab2
L
3679
3680 i.rex = REX_R;
3681 xchg = i.rm.regmem;
3682 i.rm.regmem = i.rm.reg;
3683 i.rm.reg = xchg;
3684
dbbc8b7e
JB
3685 if (i.tm.opcode_modifier.d)
3686 i.tm.base_opcode ^= (i.tm.base_opcode & 0xee) != 0x6e
3687 ? Opcode_SIMD_FloatD : Opcode_SIMD_IntD;
3688 else /* Use the next insn. */
9a182d04 3689 install_template (&t[1]);
fa99fab2
L
3690 }
3691
79dec6b7
JB
3692 /* Use 2-byte VEX prefix by swapping commutative source operands if there
3693 are no memory operands and at least 3 register ones. */
3694 if (i.reg_operands >= 3
3695 && i.vec_encoding != vex_encoding_vex3
3696 && i.reg_operands == i.operands - i.imm_operands
3697 && i.tm.opcode_modifier.vex
3698 && i.tm.opcode_modifier.commutative
3699 && (i.tm.opcode_modifier.sse2avx || optimize > 1)
3700 && i.rex == REX_B
3701 && i.vex.register_specifier
3702 && !(i.vex.register_specifier->reg_flags & RegRex))
3703 {
3704 unsigned int xchg = i.operands - i.reg_operands;
3705 union i386_op temp_op;
3706 i386_operand_type temp_type;
3707
441f6aca 3708 gas_assert (i.tm.opcode_modifier.opcodespace == SPACE_0F);
79dec6b7
JB
3709 gas_assert (!i.tm.opcode_modifier.sae);
3710 gas_assert (operand_type_equal (&i.types[i.operands - 2],
3711 &i.types[i.operands - 3]));
3712 gas_assert (i.rm.mode == 3);
3713
3714 temp_type = i.types[xchg];
3715 i.types[xchg] = i.types[xchg + 1];
3716 i.types[xchg + 1] = temp_type;
3717 temp_op = i.op[xchg];
3718 i.op[xchg] = i.op[xchg + 1];
3719 i.op[xchg + 1] = temp_op;
3720
3721 i.rex = 0;
3722 xchg = i.rm.regmem | 8;
3723 i.rm.regmem = ~register_specifier & 0xf;
3724 gas_assert (!(i.rm.regmem & 8));
3725 i.vex.register_specifier += xchg - i.rm.regmem;
3726 register_specifier = ~xchg & 0xf;
3727 }
3728
539f890d
L
3729 if (i.tm.opcode_modifier.vex == VEXScalar)
3730 vector_length = avxscalar;
10c17abd
JB
3731 else if (i.tm.opcode_modifier.vex == VEX256)
3732 vector_length = 1;
539f890d 3733 else
10c17abd 3734 {
56522fc5 3735 unsigned int op;
10c17abd 3736
c7213af9
L
3737 /* Determine vector length from the last multi-length vector
3738 operand. */
10c17abd 3739 vector_length = 0;
56522fc5 3740 for (op = t->operands; op--;)
10c17abd
JB
3741 if (t->operand_types[op].bitfield.xmmword
3742 && t->operand_types[op].bitfield.ymmword
3743 && i.types[op].bitfield.ymmword)
3744 {
3745 vector_length = 1;
3746 break;
3747 }
3748 }
c0f3af97 3749
03751133
L
3750 /* Check the REX.W bit and VEXW. */
3751 if (i.tm.opcode_modifier.vexw == VEXWIG)
3752 w = (vexwig == vexw1 || (i.rex & REX_W)) ? 1 : 0;
3753 else if (i.tm.opcode_modifier.vexw)
3754 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3755 else
931d03b7 3756 w = (flag_code == CODE_64BIT ? i.rex & REX_W : vexwig == vexw1) ? 1 : 0;
03751133 3757
c0f3af97 3758 /* Use 2-byte VEX prefix if possible. */
03751133
L
3759 if (w == 0
3760 && i.vec_encoding != vex_encoding_vex3
441f6aca 3761 && i.tm.opcode_modifier.opcodespace == SPACE_0F
c0f3af97
L
3762 && (i.rex & (REX_W | REX_X | REX_B)) == 0)
3763 {
3764 /* 2-byte VEX prefix. */
3765 unsigned int r;
3766
3767 i.vex.length = 2;
3768 i.vex.bytes[0] = 0xc5;
3769
3770 /* Check the REX.R bit. */
3771 r = (i.rex & REX_R) ? 0 : 1;
3772 i.vex.bytes[1] = (r << 7
3773 | register_specifier << 3
3774 | vector_length << 2
35648716 3775 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3776 }
3777 else
3778 {
3779 /* 3-byte VEX prefix. */
f88c9eb0 3780 i.vex.length = 3;
f88c9eb0 3781
441f6aca 3782 switch (i.tm.opcode_modifier.opcodespace)
5dd85c99 3783 {
441f6aca
JB
3784 case SPACE_0F:
3785 case SPACE_0F38:
3786 case SPACE_0F3A:
80de6e00 3787 i.vex.bytes[0] = 0xc4;
7f399153 3788 break;
441f6aca
JB
3789 case SPACE_XOP08:
3790 case SPACE_XOP09:
3791 case SPACE_XOP0A:
f88c9eb0 3792 i.vex.bytes[0] = 0x8f;
7f399153
L
3793 break;
3794 default:
3795 abort ();
f88c9eb0 3796 }
c0f3af97 3797
c0f3af97
L
3798 /* The high 3 bits of the second VEX byte are 1's compliment
3799 of RXB bits from REX. */
441f6aca 3800 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | i.tm.opcode_modifier.opcodespace;
c0f3af97 3801
c0f3af97
L
3802 i.vex.bytes[2] = (w << 7
3803 | register_specifier << 3
3804 | vector_length << 2
35648716 3805 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3806 }
3807}
3808
5b7c81bd 3809static INLINE bool
e771e7c9
JB
3810is_evex_encoding (const insn_template *t)
3811{
7091c612 3812 return t->opcode_modifier.evex || t->opcode_modifier.disp8memshift
e771e7c9 3813 || t->opcode_modifier.broadcast || t->opcode_modifier.masking
a80195f1 3814 || t->opcode_modifier.sae;
e771e7c9
JB
3815}
3816
5b7c81bd 3817static INLINE bool
7a8655d2
JB
3818is_any_vex_encoding (const insn_template *t)
3819{
7b47a312 3820 return t->opcode_modifier.vex || is_evex_encoding (t);
7a8655d2
JB
3821}
3822
43234a1e
L
3823/* Build the EVEX prefix. */
3824
3825static void
3826build_evex_prefix (void)
3827{
35648716 3828 unsigned int register_specifier, w;
43234a1e
L
3829 rex_byte vrex_used = 0;
3830
3831 /* Check register specifier. */
3832 if (i.vex.register_specifier)
3833 {
3834 gas_assert ((i.vrex & REX_X) == 0);
3835
3836 register_specifier = i.vex.register_specifier->reg_num;
3837 if ((i.vex.register_specifier->reg_flags & RegRex))
3838 register_specifier += 8;
3839 /* The upper 16 registers are encoded in the fourth byte of the
3840 EVEX prefix. */
3841 if (!(i.vex.register_specifier->reg_flags & RegVRex))
3842 i.vex.bytes[3] = 0x8;
3843 register_specifier = ~register_specifier & 0xf;
3844 }
3845 else
3846 {
3847 register_specifier = 0xf;
3848
3849 /* Encode upper 16 vector index register in the fourth byte of
3850 the EVEX prefix. */
3851 if (!(i.vrex & REX_X))
3852 i.vex.bytes[3] = 0x8;
3853 else
3854 vrex_used |= REX_X;
3855 }
3856
43234a1e
L
3857 /* 4 byte EVEX prefix. */
3858 i.vex.length = 4;
3859 i.vex.bytes[0] = 0x62;
3860
43234a1e
L
3861 /* The high 3 bits of the second EVEX byte are 1's compliment of RXB
3862 bits from REX. */
441f6aca
JB
3863 gas_assert (i.tm.opcode_modifier.opcodespace >= SPACE_0F);
3864 gas_assert (i.tm.opcode_modifier.opcodespace <= SPACE_0F3A);
3865 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | i.tm.opcode_modifier.opcodespace;
43234a1e
L
3866
3867 /* The fifth bit of the second EVEX byte is 1's compliment of the
3868 REX_R bit in VREX. */
3869 if (!(i.vrex & REX_R))
3870 i.vex.bytes[1] |= 0x10;
3871 else
3872 vrex_used |= REX_R;
3873
3874 if ((i.reg_operands + i.imm_operands) == i.operands)
3875 {
3876 /* When all operands are registers, the REX_X bit in REX is not
3877 used. We reuse it to encode the upper 16 registers, which is
3878 indicated by the REX_B bit in VREX. The REX_X bit is encoded
3879 as 1's compliment. */
3880 if ((i.vrex & REX_B))
3881 {
3882 vrex_used |= REX_B;
3883 i.vex.bytes[1] &= ~0x40;
3884 }
3885 }
3886
3887 /* EVEX instructions shouldn't need the REX prefix. */
3888 i.vrex &= ~vrex_used;
3889 gas_assert (i.vrex == 0);
3890
6865c043
L
3891 /* Check the REX.W bit and VEXW. */
3892 if (i.tm.opcode_modifier.vexw == VEXWIG)
3893 w = (evexwig == evexw1 || (i.rex & REX_W)) ? 1 : 0;
3894 else if (i.tm.opcode_modifier.vexw)
3895 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3896 else
931d03b7 3897 w = (flag_code == CODE_64BIT ? i.rex & REX_W : evexwig == evexw1) ? 1 : 0;
43234a1e 3898
43234a1e 3899 /* The third byte of the EVEX prefix. */
35648716
JB
3900 i.vex.bytes[2] = ((w << 7)
3901 | (register_specifier << 3)
3902 | 4 /* Encode the U bit. */
3903 | i.tm.opcode_modifier.opcodeprefix);
43234a1e
L
3904
3905 /* The fourth byte of the EVEX prefix. */
3906 /* The zeroing-masking bit. */
6225c532 3907 if (i.mask.reg && i.mask.zeroing)
43234a1e
L
3908 i.vex.bytes[3] |= 0x80;
3909
3910 /* Don't always set the broadcast bit if there is no RC. */
ca5312a2 3911 if (i.rounding.type == rc_none)
43234a1e
L
3912 {
3913 /* Encode the vector length. */
3914 unsigned int vec_length;
3915
e771e7c9
JB
3916 if (!i.tm.opcode_modifier.evex
3917 || i.tm.opcode_modifier.evex == EVEXDYN)
3918 {
56522fc5 3919 unsigned int op;
e771e7c9 3920
c7213af9
L
3921 /* Determine vector length from the last multi-length vector
3922 operand. */
56522fc5 3923 for (op = i.operands; op--;)
e771e7c9
JB
3924 if (i.tm.operand_types[op].bitfield.xmmword
3925 + i.tm.operand_types[op].bitfield.ymmword
3926 + i.tm.operand_types[op].bitfield.zmmword > 1)
3927 {
3928 if (i.types[op].bitfield.zmmword)
c7213af9
L
3929 {
3930 i.tm.opcode_modifier.evex = EVEX512;
3931 break;
3932 }
e771e7c9 3933 else if (i.types[op].bitfield.ymmword)
c7213af9
L
3934 {
3935 i.tm.opcode_modifier.evex = EVEX256;
3936 break;
3937 }
e771e7c9 3938 else if (i.types[op].bitfield.xmmword)
c7213af9
L
3939 {
3940 i.tm.opcode_modifier.evex = EVEX128;
3941 break;
3942 }
5273a3cd 3943 else if (i.broadcast.type && op == i.broadcast.operand)
625cbd7a 3944 {
5273a3cd 3945 switch (i.broadcast.bytes)
625cbd7a
JB
3946 {
3947 case 64:
3948 i.tm.opcode_modifier.evex = EVEX512;
3949 break;
3950 case 32:
3951 i.tm.opcode_modifier.evex = EVEX256;
3952 break;
3953 case 16:
3954 i.tm.opcode_modifier.evex = EVEX128;
3955 break;
3956 default:
c7213af9 3957 abort ();
625cbd7a 3958 }
c7213af9 3959 break;
625cbd7a 3960 }
e771e7c9 3961 }
c7213af9 3962
56522fc5 3963 if (op >= MAX_OPERANDS)
c7213af9 3964 abort ();
e771e7c9
JB
3965 }
3966
43234a1e
L
3967 switch (i.tm.opcode_modifier.evex)
3968 {
3969 case EVEXLIG: /* LL' is ignored */
3970 vec_length = evexlig << 5;
3971 break;
3972 case EVEX128:
3973 vec_length = 0 << 5;
3974 break;
3975 case EVEX256:
3976 vec_length = 1 << 5;
3977 break;
3978 case EVEX512:
3979 vec_length = 2 << 5;
3980 break;
3981 default:
3982 abort ();
3983 break;
3984 }
3985 i.vex.bytes[3] |= vec_length;
3986 /* Encode the broadcast bit. */
5273a3cd 3987 if (i.broadcast.type)
43234a1e
L
3988 i.vex.bytes[3] |= 0x10;
3989 }
ca5312a2
JB
3990 else if (i.rounding.type != saeonly)
3991 i.vex.bytes[3] |= 0x10 | (i.rounding.type << 5);
43234a1e 3992 else
ca5312a2 3993 i.vex.bytes[3] |= 0x10 | (evexrcig << 5);
43234a1e 3994
6225c532
JB
3995 if (i.mask.reg)
3996 i.vex.bytes[3] |= i.mask.reg->reg_num;
43234a1e
L
3997}
3998
65da13b5
L
3999static void
4000process_immext (void)
4001{
4002 expressionS *exp;
4003
c0f3af97 4004 /* These AMD 3DNow! and SSE2 instructions have an opcode suffix
65da13b5
L
4005 which is coded in the same place as an 8-bit immediate field
4006 would be. Here we fake an 8-bit immediate operand from the
4007 opcode suffix stored in tm.extension_opcode.
4008
c1e679ec 4009 AVX instructions also use this encoding, for some of
c0f3af97 4010 3 argument instructions. */
65da13b5 4011
43234a1e 4012 gas_assert (i.imm_operands <= 1
7ab9ffdd 4013 && (i.operands <= 2
7a8655d2 4014 || (is_any_vex_encoding (&i.tm)
7ab9ffdd 4015 && i.operands <= 4)));
65da13b5
L
4016
4017 exp = &im_expressions[i.imm_operands++];
4018 i.op[i.operands].imms = exp;
4019 i.types[i.operands] = imm8;
4020 i.operands++;
4021 exp->X_op = O_constant;
4022 exp->X_add_number = i.tm.extension_opcode;
4023 i.tm.extension_opcode = None;
4024}
4025
42164a71
L
4026
4027static int
4028check_hle (void)
4029{
742732c7 4030 switch (i.tm.opcode_modifier.prefixok)
42164a71
L
4031 {
4032 default:
4033 abort ();
742732c7
JB
4034 case PrefixLock:
4035 case PrefixNone:
4036 case PrefixNoTrack:
4037 case PrefixRep:
165de32a
L
4038 as_bad (_("invalid instruction `%s' after `%s'"),
4039 i.tm.name, i.hle_prefix);
42164a71 4040 return 0;
742732c7 4041 case PrefixHLELock:
42164a71
L
4042 if (i.prefix[LOCK_PREFIX])
4043 return 1;
165de32a 4044 as_bad (_("missing `lock' with `%s'"), i.hle_prefix);
42164a71 4045 return 0;
742732c7 4046 case PrefixHLEAny:
42164a71 4047 return 1;
742732c7 4048 case PrefixHLERelease:
42164a71
L
4049 if (i.prefix[HLE_PREFIX] != XRELEASE_PREFIX_OPCODE)
4050 {
4051 as_bad (_("instruction `%s' after `xacquire' not allowed"),
4052 i.tm.name);
4053 return 0;
4054 }
8dc0818e 4055 if (i.mem_operands == 0 || !(i.flags[i.operands - 1] & Operand_Mem))
42164a71
L
4056 {
4057 as_bad (_("memory destination needed for instruction `%s'"
4058 " after `xrelease'"), i.tm.name);
4059 return 0;
4060 }
4061 return 1;
4062 }
4063}
4064
b6f8c7c4
L
4065/* Try the shortest encoding by shortening operand size. */
4066
4067static void
4068optimize_encoding (void)
4069{
a0a1771e 4070 unsigned int j;
b6f8c7c4
L
4071
4072 if (optimize_for_space
389d00a5 4073 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
b6f8c7c4
L
4074 && i.reg_operands == 1
4075 && i.imm_operands == 1
4076 && !i.types[1].bitfield.byte
4077 && i.op[0].imms->X_op == O_constant
4078 && fits_in_imm7 (i.op[0].imms->X_add_number)
72aea328 4079 && (i.tm.base_opcode == 0xa8
b6f8c7c4
L
4080 || (i.tm.base_opcode == 0xf6
4081 && i.tm.extension_opcode == 0x0)))
4082 {
4083 /* Optimize: -Os:
4084 test $imm7, %r64/%r32/%r16 -> test $imm7, %r8
4085 */
4086 unsigned int base_regnum = i.op[1].regs->reg_num;
4087 if (flag_code == CODE_64BIT || base_regnum < 4)
4088 {
4089 i.types[1].bitfield.byte = 1;
4090 /* Ignore the suffix. */
4091 i.suffix = 0;
7697afb6
JB
4092 /* Convert to byte registers. */
4093 if (i.types[1].bitfield.word)
4094 j = 16;
4095 else if (i.types[1].bitfield.dword)
4096 j = 32;
4097 else
4098 j = 48;
4099 if (!(i.op[1].regs->reg_flags & RegRex) && base_regnum < 4)
4100 j += 8;
4101 i.op[1].regs -= j;
b6f8c7c4
L
4102 }
4103 }
4104 else if (flag_code == CODE_64BIT
389d00a5 4105 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
d3d50934
L
4106 && ((i.types[1].bitfield.qword
4107 && i.reg_operands == 1
b6f8c7c4
L
4108 && i.imm_operands == 1
4109 && i.op[0].imms->X_op == O_constant
507916b8 4110 && ((i.tm.base_opcode == 0xb8
b6f8c7c4
L
4111 && i.tm.extension_opcode == None
4112 && fits_in_unsigned_long (i.op[0].imms->X_add_number))
4113 || (fits_in_imm31 (i.op[0].imms->X_add_number)
72aea328
JB
4114 && ((i.tm.base_opcode == 0x24
4115 || i.tm.base_opcode == 0xa8)
b6f8c7c4
L
4116 || (i.tm.base_opcode == 0x80
4117 && i.tm.extension_opcode == 0x4)
4118 || ((i.tm.base_opcode == 0xf6
507916b8 4119 || (i.tm.base_opcode | 1) == 0xc7)
b8364fa7
JB
4120 && i.tm.extension_opcode == 0x0)))
4121 || (fits_in_imm7 (i.op[0].imms->X_add_number)
4122 && i.tm.base_opcode == 0x83
4123 && i.tm.extension_opcode == 0x4)))
d3d50934
L
4124 || (i.types[0].bitfield.qword
4125 && ((i.reg_operands == 2
4126 && i.op[0].regs == i.op[1].regs
72aea328
JB
4127 && (i.tm.base_opcode == 0x30
4128 || i.tm.base_opcode == 0x28))
d3d50934
L
4129 || (i.reg_operands == 1
4130 && i.operands == 1
72aea328 4131 && i.tm.base_opcode == 0x30)))))
b6f8c7c4
L
4132 {
4133 /* Optimize: -O:
4134 andq $imm31, %r64 -> andl $imm31, %r32
b8364fa7 4135 andq $imm7, %r64 -> andl $imm7, %r32
b6f8c7c4
L
4136 testq $imm31, %r64 -> testl $imm31, %r32
4137 xorq %r64, %r64 -> xorl %r32, %r32
4138 subq %r64, %r64 -> subl %r32, %r32
4139 movq $imm31, %r64 -> movl $imm31, %r32
4140 movq $imm32, %r64 -> movl $imm32, %r32
4141 */
4142 i.tm.opcode_modifier.norex64 = 1;
507916b8 4143 if (i.tm.base_opcode == 0xb8 || (i.tm.base_opcode | 1) == 0xc7)
b6f8c7c4
L
4144 {
4145 /* Handle
4146 movq $imm31, %r64 -> movl $imm31, %r32
4147 movq $imm32, %r64 -> movl $imm32, %r32
4148 */
4149 i.tm.operand_types[0].bitfield.imm32 = 1;
4150 i.tm.operand_types[0].bitfield.imm32s = 0;
4151 i.tm.operand_types[0].bitfield.imm64 = 0;
4152 i.types[0].bitfield.imm32 = 1;
4153 i.types[0].bitfield.imm32s = 0;
4154 i.types[0].bitfield.imm64 = 0;
4155 i.types[1].bitfield.dword = 1;
4156 i.types[1].bitfield.qword = 0;
507916b8 4157 if ((i.tm.base_opcode | 1) == 0xc7)
b6f8c7c4
L
4158 {
4159 /* Handle
4160 movq $imm31, %r64 -> movl $imm31, %r32
4161 */
507916b8 4162 i.tm.base_opcode = 0xb8;
b6f8c7c4 4163 i.tm.extension_opcode = None;
507916b8 4164 i.tm.opcode_modifier.w = 0;
b6f8c7c4
L
4165 i.tm.opcode_modifier.modrm = 0;
4166 }
4167 }
4168 }
5641ec01
JB
4169 else if (optimize > 1
4170 && !optimize_for_space
389d00a5 4171 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
5641ec01
JB
4172 && i.reg_operands == 2
4173 && i.op[0].regs == i.op[1].regs
4174 && ((i.tm.base_opcode & ~(Opcode_D | 1)) == 0x8
4175 || (i.tm.base_opcode & ~(Opcode_D | 1)) == 0x20)
4176 && (flag_code != CODE_64BIT || !i.types[0].bitfield.dword))
4177 {
4178 /* Optimize: -O2:
4179 andb %rN, %rN -> testb %rN, %rN
4180 andw %rN, %rN -> testw %rN, %rN
4181 andq %rN, %rN -> testq %rN, %rN
4182 orb %rN, %rN -> testb %rN, %rN
4183 orw %rN, %rN -> testw %rN, %rN
4184 orq %rN, %rN -> testq %rN, %rN
4185
4186 and outside of 64-bit mode
4187
4188 andl %rN, %rN -> testl %rN, %rN
4189 orl %rN, %rN -> testl %rN, %rN
4190 */
4191 i.tm.base_opcode = 0x84 | (i.tm.base_opcode & 1);
4192 }
99112332 4193 else if (i.reg_operands == 3
b6f8c7c4
L
4194 && i.op[0].regs == i.op[1].regs
4195 && !i.types[2].bitfield.xmmword
4196 && (i.tm.opcode_modifier.vex
6225c532 4197 || ((!i.mask.reg || i.mask.zeroing)
ca5312a2 4198 && i.rounding.type == rc_none
e771e7c9 4199 && is_evex_encoding (&i.tm)
80c34c38 4200 && (i.vec_encoding != vex_encoding_evex
dd22218c 4201 || cpu_arch_isa_flags.bitfield.cpuavx512vl
80c34c38 4202 || i.tm.cpu_flags.bitfield.cpuavx512vl
7091c612 4203 || (i.tm.operand_types[2].bitfield.zmmword
dd22218c 4204 && i.types[2].bitfield.ymmword))))
b6f8c7c4 4205 && ((i.tm.base_opcode == 0x55
b6f8c7c4 4206 || i.tm.base_opcode == 0x57
35648716
JB
4207 || i.tm.base_opcode == 0xdf
4208 || i.tm.base_opcode == 0xef
4209 || i.tm.base_opcode == 0xf8
4210 || i.tm.base_opcode == 0xf9
4211 || i.tm.base_opcode == 0xfa
4212 || i.tm.base_opcode == 0xfb
1424ad86 4213 || i.tm.base_opcode == 0x42
35648716 4214 || i.tm.base_opcode == 0x47)
b6f8c7c4
L
4215 && i.tm.extension_opcode == None))
4216 {
99112332 4217 /* Optimize: -O1:
8305403a
L
4218 VOP, one of vandnps, vandnpd, vxorps, vxorpd, vpsubb, vpsubd,
4219 vpsubq and vpsubw:
b6f8c7c4
L
4220 EVEX VOP %zmmM, %zmmM, %zmmN
4221 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4222 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4223 EVEX VOP %ymmM, %ymmM, %ymmN
4224 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4225 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4226 VEX VOP %ymmM, %ymmM, %ymmN
4227 -> VEX VOP %xmmM, %xmmM, %xmmN
4228 VOP, one of vpandn and vpxor:
4229 VEX VOP %ymmM, %ymmM, %ymmN
4230 -> VEX VOP %xmmM, %xmmM, %xmmN
4231 VOP, one of vpandnd and vpandnq:
4232 EVEX VOP %zmmM, %zmmM, %zmmN
4233 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4234 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4235 EVEX VOP %ymmM, %ymmM, %ymmN
4236 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4237 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4238 VOP, one of vpxord and vpxorq:
4239 EVEX VOP %zmmM, %zmmM, %zmmN
4240 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4241 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4242 EVEX VOP %ymmM, %ymmM, %ymmN
4243 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4244 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
1424ad86
JB
4245 VOP, one of kxord and kxorq:
4246 VEX VOP %kM, %kM, %kN
4247 -> VEX kxorw %kM, %kM, %kN
4248 VOP, one of kandnd and kandnq:
4249 VEX VOP %kM, %kM, %kN
4250 -> VEX kandnw %kM, %kM, %kN
b6f8c7c4 4251 */
e771e7c9 4252 if (is_evex_encoding (&i.tm))
b6f8c7c4 4253 {
7b1d7ca1 4254 if (i.vec_encoding != vex_encoding_evex)
b6f8c7c4
L
4255 {
4256 i.tm.opcode_modifier.vex = VEX128;
4257 i.tm.opcode_modifier.vexw = VEXW0;
4258 i.tm.opcode_modifier.evex = 0;
4259 }
7b1d7ca1 4260 else if (optimize > 1)
dd22218c
L
4261 i.tm.opcode_modifier.evex = EVEX128;
4262 else
4263 return;
b6f8c7c4 4264 }
f74a6307 4265 else if (i.tm.operand_types[0].bitfield.class == RegMask)
1424ad86 4266 {
35648716 4267 i.tm.opcode_modifier.opcodeprefix = PREFIX_NONE;
1424ad86
JB
4268 i.tm.opcode_modifier.vexw = VEXW0;
4269 }
b6f8c7c4
L
4270 else
4271 i.tm.opcode_modifier.vex = VEX128;
4272
4273 if (i.tm.opcode_modifier.vex)
4274 for (j = 0; j < 3; j++)
4275 {
4276 i.types[j].bitfield.xmmword = 1;
4277 i.types[j].bitfield.ymmword = 0;
4278 }
4279 }
392a5972 4280 else if (i.vec_encoding != vex_encoding_evex
97ed31ae 4281 && !i.types[0].bitfield.zmmword
392a5972 4282 && !i.types[1].bitfield.zmmword
6225c532 4283 && !i.mask.reg
5273a3cd 4284 && !i.broadcast.type
97ed31ae 4285 && is_evex_encoding (&i.tm)
35648716
JB
4286 && ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4287 || (i.tm.base_opcode & ~4) == 0xdb
4288 || (i.tm.base_opcode & ~4) == 0xeb)
97ed31ae
L
4289 && i.tm.extension_opcode == None)
4290 {
4291 /* Optimize: -O1:
4292 VOP, one of vmovdqa32, vmovdqa64, vmovdqu8, vmovdqu16,
4293 vmovdqu32 and vmovdqu64:
4294 EVEX VOP %xmmM, %xmmN
4295 -> VEX vmovdqa|vmovdqu %xmmM, %xmmN (M and N < 16)
4296 EVEX VOP %ymmM, %ymmN
4297 -> VEX vmovdqa|vmovdqu %ymmM, %ymmN (M and N < 16)
4298 EVEX VOP %xmmM, mem
4299 -> VEX vmovdqa|vmovdqu %xmmM, mem (M < 16)
4300 EVEX VOP %ymmM, mem
4301 -> VEX vmovdqa|vmovdqu %ymmM, mem (M < 16)
4302 EVEX VOP mem, %xmmN
4303 -> VEX mvmovdqa|vmovdquem, %xmmN (N < 16)
4304 EVEX VOP mem, %ymmN
4305 -> VEX vmovdqa|vmovdqu mem, %ymmN (N < 16)
a0a1771e
JB
4306 VOP, one of vpand, vpandn, vpor, vpxor:
4307 EVEX VOP{d,q} %xmmL, %xmmM, %xmmN
4308 -> VEX VOP %xmmL, %xmmM, %xmmN (L, M, and N < 16)
4309 EVEX VOP{d,q} %ymmL, %ymmM, %ymmN
4310 -> VEX VOP %ymmL, %ymmM, %ymmN (L, M, and N < 16)
4311 EVEX VOP{d,q} mem, %xmmM, %xmmN
4312 -> VEX VOP mem, %xmmM, %xmmN (M and N < 16)
4313 EVEX VOP{d,q} mem, %ymmM, %ymmN
4314 -> VEX VOP mem, %ymmM, %ymmN (M and N < 16)
97ed31ae 4315 */
a0a1771e 4316 for (j = 0; j < i.operands; j++)
392a5972
L
4317 if (operand_type_check (i.types[j], disp)
4318 && i.op[j].disps->X_op == O_constant)
4319 {
4320 /* Since the VEX prefix has 2 or 3 bytes, the EVEX prefix
4321 has 4 bytes, EVEX Disp8 has 1 byte and VEX Disp32 has 4
4322 bytes, we choose EVEX Disp8 over VEX Disp32. */
4323 int evex_disp8, vex_disp8;
4324 unsigned int memshift = i.memshift;
4325 offsetT n = i.op[j].disps->X_add_number;
4326
4327 evex_disp8 = fits_in_disp8 (n);
4328 i.memshift = 0;
4329 vex_disp8 = fits_in_disp8 (n);
4330 if (evex_disp8 != vex_disp8)
4331 {
4332 i.memshift = memshift;
4333 return;
4334 }
4335
4336 i.types[j].bitfield.disp8 = vex_disp8;
4337 break;
4338 }
35648716
JB
4339 if ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4340 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2)
4341 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
97ed31ae
L
4342 i.tm.opcode_modifier.vex
4343 = i.types[0].bitfield.ymmword ? VEX256 : VEX128;
4344 i.tm.opcode_modifier.vexw = VEXW0;
79dec6b7 4345 /* VPAND, VPOR, and VPXOR are commutative. */
35648716 4346 if (i.reg_operands == 3 && i.tm.base_opcode != 0xdf)
79dec6b7 4347 i.tm.opcode_modifier.commutative = 1;
97ed31ae
L
4348 i.tm.opcode_modifier.evex = 0;
4349 i.tm.opcode_modifier.masking = 0;
a0a1771e 4350 i.tm.opcode_modifier.broadcast = 0;
97ed31ae
L
4351 i.tm.opcode_modifier.disp8memshift = 0;
4352 i.memshift = 0;
a0a1771e
JB
4353 if (j < i.operands)
4354 i.types[j].bitfield.disp8
4355 = fits_in_disp8 (i.op[j].disps->X_add_number);
97ed31ae 4356 }
b6f8c7c4
L
4357}
4358
ae531041
L
4359/* Return non-zero for load instruction. */
4360
4361static int
4362load_insn_p (void)
4363{
4364 unsigned int dest;
4365 int any_vex_p = is_any_vex_encoding (&i.tm);
4366 unsigned int base_opcode = i.tm.base_opcode | 1;
4367
4368 if (!any_vex_p)
4369 {
a09f656b 4370 /* Anysize insns: lea, invlpg, clflush, prefetchnta, prefetcht0,
4371 prefetcht1, prefetcht2, prefetchtw, bndmk, bndcl, bndcu, bndcn,
4372 bndstx, bndldx, prefetchwt1, clflushopt, clwb, cldemote. */
4373 if (i.tm.opcode_modifier.anysize)
ae531041
L
4374 return 0;
4375
389d00a5
JB
4376 /* pop. */
4377 if (strcmp (i.tm.name, "pop") == 0)
4378 return 1;
4379 }
4380
4381 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE)
4382 {
4383 /* popf, popa. */
4384 if (i.tm.base_opcode == 0x9d
a09f656b 4385 || i.tm.base_opcode == 0x61)
ae531041
L
4386 return 1;
4387
4388 /* movs, cmps, lods, scas. */
4389 if ((i.tm.base_opcode | 0xb) == 0xaf)
4390 return 1;
4391
a09f656b 4392 /* outs, xlatb. */
4393 if (base_opcode == 0x6f
4394 || i.tm.base_opcode == 0xd7)
ae531041 4395 return 1;
a09f656b 4396 /* NB: For AMD-specific insns with implicit memory operands,
4397 they're intentionally not covered. */
ae531041
L
4398 }
4399
4400 /* No memory operand. */
4401 if (!i.mem_operands)
4402 return 0;
4403
4404 if (any_vex_p)
4405 {
4406 /* vldmxcsr. */
4407 if (i.tm.base_opcode == 0xae
4408 && i.tm.opcode_modifier.vex
441f6aca 4409 && i.tm.opcode_modifier.opcodespace == SPACE_0F
35648716 4410 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
ae531041
L
4411 && i.tm.extension_opcode == 2)
4412 return 1;
4413 }
389d00a5 4414 else if (i.tm.opcode_modifier.opcodespace == SPACE_BASE)
ae531041
L
4415 {
4416 /* test, not, neg, mul, imul, div, idiv. */
4417 if ((i.tm.base_opcode == 0xf6 || i.tm.base_opcode == 0xf7)
4418 && i.tm.extension_opcode != 1)
4419 return 1;
4420
4421 /* inc, dec. */
4422 if (base_opcode == 0xff && i.tm.extension_opcode <= 1)
4423 return 1;
4424
4425 /* add, or, adc, sbb, and, sub, xor, cmp. */
4426 if (i.tm.base_opcode >= 0x80 && i.tm.base_opcode <= 0x83)
4427 return 1;
4428
ae531041
L
4429 /* rol, ror, rcl, rcr, shl/sal, shr, sar. */
4430 if ((base_opcode == 0xc1
4431 || (i.tm.base_opcode >= 0xd0 && i.tm.base_opcode <= 0xd3))
4432 && i.tm.extension_opcode != 6)
4433 return 1;
4434
ae531041 4435 /* Check for x87 instructions. */
389d00a5 4436 if (base_opcode >= 0xd8 && base_opcode <= 0xdf)
ae531041
L
4437 {
4438 /* Skip fst, fstp, fstenv, fstcw. */
4439 if (i.tm.base_opcode == 0xd9
4440 && (i.tm.extension_opcode == 2
4441 || i.tm.extension_opcode == 3
4442 || i.tm.extension_opcode == 6
4443 || i.tm.extension_opcode == 7))
4444 return 0;
4445
4446 /* Skip fisttp, fist, fistp, fstp. */
4447 if (i.tm.base_opcode == 0xdb
4448 && (i.tm.extension_opcode == 1
4449 || i.tm.extension_opcode == 2
4450 || i.tm.extension_opcode == 3
4451 || i.tm.extension_opcode == 7))
4452 return 0;
4453
4454 /* Skip fisttp, fst, fstp, fsave, fstsw. */
4455 if (i.tm.base_opcode == 0xdd
4456 && (i.tm.extension_opcode == 1
4457 || i.tm.extension_opcode == 2
4458 || i.tm.extension_opcode == 3
4459 || i.tm.extension_opcode == 6
4460 || i.tm.extension_opcode == 7))
4461 return 0;
4462
4463 /* Skip fisttp, fist, fistp, fbstp, fistp. */
4464 if (i.tm.base_opcode == 0xdf
4465 && (i.tm.extension_opcode == 1
4466 || i.tm.extension_opcode == 2
4467 || i.tm.extension_opcode == 3
4468 || i.tm.extension_opcode == 6
4469 || i.tm.extension_opcode == 7))
4470 return 0;
4471
4472 return 1;
4473 }
4474 }
389d00a5
JB
4475 else if (i.tm.opcode_modifier.opcodespace == SPACE_0F)
4476 {
4477 /* bt, bts, btr, btc. */
4478 if (i.tm.base_opcode == 0xba
4479 && (i.tm.extension_opcode >= 4 && i.tm.extension_opcode <= 7))
4480 return 1;
4481
4482 /* cmpxchg8b, cmpxchg16b, xrstors, vmptrld. */
4483 if (i.tm.base_opcode == 0xc7
4484 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
4485 && (i.tm.extension_opcode == 1 || i.tm.extension_opcode == 3
4486 || i.tm.extension_opcode == 6))
4487 return 1;
4488
4489 /* fxrstor, ldmxcsr, xrstor. */
4490 if (i.tm.base_opcode == 0xae
4491 && (i.tm.extension_opcode == 1
4492 || i.tm.extension_opcode == 2
4493 || i.tm.extension_opcode == 5))
4494 return 1;
4495
4496 /* lgdt, lidt, lmsw. */
4497 if (i.tm.base_opcode == 0x01
4498 && (i.tm.extension_opcode == 2
4499 || i.tm.extension_opcode == 3
4500 || i.tm.extension_opcode == 6))
4501 return 1;
4502 }
ae531041
L
4503
4504 dest = i.operands - 1;
4505
4506 /* Check fake imm8 operand and 3 source operands. */
4507 if ((i.tm.opcode_modifier.immext
4508 || i.tm.opcode_modifier.vexsources == VEX3SOURCES)
4509 && i.types[dest].bitfield.imm8)
4510 dest--;
4511
389d00a5
JB
4512 /* add, or, adc, sbb, and, sub, xor, cmp, test, xchg. */
4513 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
ae531041
L
4514 && (base_opcode == 0x1
4515 || base_opcode == 0x9
4516 || base_opcode == 0x11
4517 || base_opcode == 0x19
4518 || base_opcode == 0x21
4519 || base_opcode == 0x29
4520 || base_opcode == 0x31
4521 || base_opcode == 0x39
389d00a5
JB
4522 || (base_opcode | 2) == 0x87))
4523 return 1;
4524
4525 /* xadd. */
4526 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4527 && base_opcode == 0xc1)
ae531041
L
4528 return 1;
4529
4530 /* Check for load instruction. */
4531 return (i.types[dest].bitfield.class != ClassNone
4532 || i.types[dest].bitfield.instance == Accum);
4533}
4534
4535/* Output lfence, 0xfaee8, after instruction. */
4536
4537static void
4538insert_lfence_after (void)
4539{
4540 if (lfence_after_load && load_insn_p ())
4541 {
a09f656b 4542 /* There are also two REP string instructions that require
4543 special treatment. Specifically, the compare string (CMPS)
4544 and scan string (SCAS) instructions set EFLAGS in a manner
4545 that depends on the data being compared/scanned. When used
4546 with a REP prefix, the number of iterations may therefore
4547 vary depending on this data. If the data is a program secret
4548 chosen by the adversary using an LVI method,
4549 then this data-dependent behavior may leak some aspect
4550 of the secret. */
4551 if (((i.tm.base_opcode | 0x1) == 0xa7
4552 || (i.tm.base_opcode | 0x1) == 0xaf)
4553 && i.prefix[REP_PREFIX])
4554 {
4555 as_warn (_("`%s` changes flags which would affect control flow behavior"),
4556 i.tm.name);
4557 }
ae531041
L
4558 char *p = frag_more (3);
4559 *p++ = 0xf;
4560 *p++ = 0xae;
4561 *p = 0xe8;
4562 }
4563}
4564
4565/* Output lfence, 0xfaee8, before instruction. */
4566
4567static void
4568insert_lfence_before (void)
4569{
4570 char *p;
4571
389d00a5 4572 if (i.tm.opcode_modifier.opcodespace != SPACE_BASE)
ae531041
L
4573 return;
4574
4575 if (i.tm.base_opcode == 0xff
4576 && (i.tm.extension_opcode == 2 || i.tm.extension_opcode == 4))
4577 {
4578 /* Insert lfence before indirect branch if needed. */
4579
4580 if (lfence_before_indirect_branch == lfence_branch_none)
4581 return;
4582
4583 if (i.operands != 1)
4584 abort ();
4585
4586 if (i.reg_operands == 1)
4587 {
4588 /* Indirect branch via register. Don't insert lfence with
4589 -mlfence-after-load=yes. */
4590 if (lfence_after_load
4591 || lfence_before_indirect_branch == lfence_branch_memory)
4592 return;
4593 }
4594 else if (i.mem_operands == 1
4595 && lfence_before_indirect_branch != lfence_branch_register)
4596 {
4597 as_warn (_("indirect `%s` with memory operand should be avoided"),
4598 i.tm.name);
4599 return;
4600 }
4601 else
4602 return;
4603
4604 if (last_insn.kind != last_insn_other
4605 && last_insn.seg == now_seg)
4606 {
4607 as_warn_where (last_insn.file, last_insn.line,
4608 _("`%s` skips -mlfence-before-indirect-branch on `%s`"),
4609 last_insn.name, i.tm.name);
4610 return;
4611 }
4612
4613 p = frag_more (3);
4614 *p++ = 0xf;
4615 *p++ = 0xae;
4616 *p = 0xe8;
4617 return;
4618 }
4619
503648e4 4620 /* Output or/not/shl and lfence before near ret. */
ae531041
L
4621 if (lfence_before_ret != lfence_before_ret_none
4622 && (i.tm.base_opcode == 0xc2
503648e4 4623 || i.tm.base_opcode == 0xc3))
ae531041
L
4624 {
4625 if (last_insn.kind != last_insn_other
4626 && last_insn.seg == now_seg)
4627 {
4628 as_warn_where (last_insn.file, last_insn.line,
4629 _("`%s` skips -mlfence-before-ret on `%s`"),
4630 last_insn.name, i.tm.name);
4631 return;
4632 }
a09f656b 4633
a09f656b 4634 /* Near ret ingore operand size override under CPU64. */
503648e4 4635 char prefix = flag_code == CODE_64BIT
4636 ? 0x48
4637 : i.prefix[DATA_PREFIX] ? 0x66 : 0x0;
a09f656b 4638
4639 if (lfence_before_ret == lfence_before_ret_not)
4640 {
4641 /* not: 0xf71424, may add prefix
4642 for operand size override or 64-bit code. */
4643 p = frag_more ((prefix ? 2 : 0) + 6 + 3);
4644 if (prefix)
4645 *p++ = prefix;
ae531041
L
4646 *p++ = 0xf7;
4647 *p++ = 0x14;
4648 *p++ = 0x24;
a09f656b 4649 if (prefix)
4650 *p++ = prefix;
ae531041
L
4651 *p++ = 0xf7;
4652 *p++ = 0x14;
4653 *p++ = 0x24;
4654 }
a09f656b 4655 else
4656 {
4657 p = frag_more ((prefix ? 1 : 0) + 4 + 3);
4658 if (prefix)
4659 *p++ = prefix;
4660 if (lfence_before_ret == lfence_before_ret_or)
4661 {
4662 /* or: 0x830c2400, may add prefix
4663 for operand size override or 64-bit code. */
4664 *p++ = 0x83;
4665 *p++ = 0x0c;
4666 }
4667 else
4668 {
4669 /* shl: 0xc1242400, may add prefix
4670 for operand size override or 64-bit code. */
4671 *p++ = 0xc1;
4672 *p++ = 0x24;
4673 }
4674
4675 *p++ = 0x24;
4676 *p++ = 0x0;
4677 }
4678
ae531041
L
4679 *p++ = 0xf;
4680 *p++ = 0xae;
4681 *p = 0xe8;
4682 }
4683}
4684
252b5132
RH
4685/* This is the guts of the machine-dependent assembler. LINE points to a
4686 machine dependent instruction. This function is supposed to emit
4687 the frags/bytes it assembles to. */
4688
4689void
65da13b5 4690md_assemble (char *line)
252b5132 4691{
40fb9820 4692 unsigned int j;
83b16ac6 4693 char mnemonic[MAX_MNEM_SIZE], mnem_suffix;
d3ce72d0 4694 const insn_template *t;
252b5132 4695
47926f60 4696 /* Initialize globals. */
252b5132 4697 memset (&i, '\0', sizeof (i));
ca5312a2 4698 i.rounding.type = rc_none;
252b5132 4699 for (j = 0; j < MAX_OPERANDS; j++)
1ae12ab7 4700 i.reloc[j] = NO_RELOC;
252b5132
RH
4701 memset (disp_expressions, '\0', sizeof (disp_expressions));
4702 memset (im_expressions, '\0', sizeof (im_expressions));
ce8a8b2f 4703 save_stack_p = save_stack;
252b5132
RH
4704
4705 /* First parse an instruction mnemonic & call i386_operand for the operands.
4706 We assume that the scrubber has arranged it so that line[0] is the valid
47926f60 4707 start of a (possibly prefixed) mnemonic. */
252b5132 4708
29b0f896
AM
4709 line = parse_insn (line, mnemonic);
4710 if (line == NULL)
4711 return;
83b16ac6 4712 mnem_suffix = i.suffix;
252b5132 4713
29b0f896 4714 line = parse_operands (line, mnemonic);
ee86248c 4715 this_operand = -1;
8325cc63
JB
4716 xfree (i.memop1_string);
4717 i.memop1_string = NULL;
29b0f896
AM
4718 if (line == NULL)
4719 return;
252b5132 4720
29b0f896
AM
4721 /* Now we've parsed the mnemonic into a set of templates, and have the
4722 operands at hand. */
4723
b630c145 4724 /* All Intel opcodes have reversed operands except for "bound", "enter",
c0e54661
JB
4725 "invlpg*", "monitor*", "mwait*", "tpause", "umwait", "pvalidate",
4726 "rmpadjust", and "rmpupdate". We also don't reverse intersegment "jmp"
4727 and "call" instructions with 2 immediate operands so that the immediate
4728 segment precedes the offset consistently in Intel and AT&T modes. */
4d456e3d
L
4729 if (intel_syntax
4730 && i.operands > 1
29b0f896 4731 && (strcmp (mnemonic, "bound") != 0)
c0e54661 4732 && (strncmp (mnemonic, "invlpg", 6) != 0)
d34049e8
ML
4733 && !startswith (mnemonic, "monitor")
4734 && !startswith (mnemonic, "mwait")
c0e54661 4735 && (strcmp (mnemonic, "pvalidate") != 0)
d34049e8 4736 && !startswith (mnemonic, "rmp")
b630c145
JB
4737 && (strcmp (mnemonic, "tpause") != 0)
4738 && (strcmp (mnemonic, "umwait") != 0)
40fb9820
L
4739 && !(operand_type_check (i.types[0], imm)
4740 && operand_type_check (i.types[1], imm)))
29b0f896
AM
4741 swap_operands ();
4742
ec56d5c0
JB
4743 /* The order of the immediates should be reversed
4744 for 2 immediates extrq and insertq instructions */
4745 if (i.imm_operands == 2
4746 && (strcmp (mnemonic, "extrq") == 0
4747 || strcmp (mnemonic, "insertq") == 0))
4748 swap_2_operands (0, 1);
4749
29b0f896
AM
4750 if (i.imm_operands)
4751 optimize_imm ();
4752
a9aabc23 4753 if (i.disp_operands && !want_disp32 (current_templates->start))
cce08655
JB
4754 {
4755 for (j = 0; j < i.operands; ++j)
4756 {
4757 const expressionS *exp = i.op[j].disps;
4758
4759 if (!operand_type_check (i.types[j], disp))
4760 continue;
4761
4762 if (exp->X_op != O_constant)
4763 continue;
4764
4765 /* Since displacement is signed extended to 64bit, don't allow
4766 disp32 and turn off disp32s if they are out of range. */
4767 i.types[j].bitfield.disp32 = 0;
4768 if (fits_in_signed_long (exp->X_add_number))
4769 continue;
4770
4771 i.types[j].bitfield.disp32s = 0;
4772 if (i.types[j].bitfield.baseindex)
4773 {
4774 as_bad (_("0x%" BFD_VMA_FMT "x out of range of signed 32bit displacement"),
4775 exp->X_add_number);
4776 return;
4777 }
4778 }
4779 }
4780
b300c311
L
4781 /* Don't optimize displacement for movabs since it only takes 64bit
4782 displacement. */
4783 if (i.disp_operands
a501d77e 4784 && i.disp_encoding != disp_encoding_32bit
862be3fb
L
4785 && (flag_code != CODE_64BIT
4786 || strcmp (mnemonic, "movabs") != 0))
4787 optimize_disp ();
29b0f896
AM
4788
4789 /* Next, we find a template that matches the given insn,
4790 making sure the overlap of the given operands types is consistent
4791 with the template operand types. */
252b5132 4792
83b16ac6 4793 if (!(t = match_template (mnem_suffix)))
29b0f896 4794 return;
252b5132 4795
7bab8ab5 4796 if (sse_check != check_none
81f8a913 4797 && !i.tm.opcode_modifier.noavx
6e3e5c9e 4798 && !i.tm.cpu_flags.bitfield.cpuavx
569d50f1 4799 && !i.tm.cpu_flags.bitfield.cpuavx512f
daf50ae7
L
4800 && (i.tm.cpu_flags.bitfield.cpusse
4801 || i.tm.cpu_flags.bitfield.cpusse2
4802 || i.tm.cpu_flags.bitfield.cpusse3
4803 || i.tm.cpu_flags.bitfield.cpussse3
4804 || i.tm.cpu_flags.bitfield.cpusse4_1
6e3e5c9e
JB
4805 || i.tm.cpu_flags.bitfield.cpusse4_2
4806 || i.tm.cpu_flags.bitfield.cpupclmul
4807 || i.tm.cpu_flags.bitfield.cpuaes
569d50f1 4808 || i.tm.cpu_flags.bitfield.cpusha
6e3e5c9e 4809 || i.tm.cpu_flags.bitfield.cpugfni))
daf50ae7 4810 {
7bab8ab5 4811 (sse_check == check_warning
daf50ae7
L
4812 ? as_warn
4813 : as_bad) (_("SSE instruction `%s' is used"), i.tm.name);
4814 }
4815
40fb9820 4816 if (i.tm.opcode_modifier.fwait)
29b0f896
AM
4817 if (!add_prefix (FWAIT_OPCODE))
4818 return;
252b5132 4819
d5de92cf 4820 /* Check if REP prefix is OK. */
742732c7 4821 if (i.rep_prefix && i.tm.opcode_modifier.prefixok != PrefixRep)
d5de92cf
L
4822 {
4823 as_bad (_("invalid instruction `%s' after `%s'"),
4824 i.tm.name, i.rep_prefix);
4825 return;
4826 }
4827
c1ba0266
L
4828 /* Check for lock without a lockable instruction. Destination operand
4829 must be memory unless it is xchg (0x86). */
c32fa91d 4830 if (i.prefix[LOCK_PREFIX]
742732c7 4831 && (i.tm.opcode_modifier.prefixok < PrefixLock
c1ba0266
L
4832 || i.mem_operands == 0
4833 || (i.tm.base_opcode != 0x86
8dc0818e 4834 && !(i.flags[i.operands - 1] & Operand_Mem))))
c32fa91d
L
4835 {
4836 as_bad (_("expecting lockable instruction after `lock'"));
4837 return;
4838 }
4839
40d231b4
JB
4840 /* Check for data size prefix on VEX/XOP/EVEX encoded and SIMD insns. */
4841 if (i.prefix[DATA_PREFIX]
4842 && (is_any_vex_encoding (&i.tm)
4843 || i.tm.operand_types[i.imm_operands].bitfield.class >= RegMMX
4844 || i.tm.operand_types[i.imm_operands + 1].bitfield.class >= RegMMX))
7a8655d2
JB
4845 {
4846 as_bad (_("data size prefix invalid with `%s'"), i.tm.name);
4847 return;
4848 }
4849
42164a71 4850 /* Check if HLE prefix is OK. */
165de32a 4851 if (i.hle_prefix && !check_hle ())
42164a71
L
4852 return;
4853
7e8b059b
L
4854 /* Check BND prefix. */
4855 if (i.bnd_prefix && !i.tm.opcode_modifier.bndprefixok)
4856 as_bad (_("expecting valid branch instruction after `bnd'"));
4857
04ef582a 4858 /* Check NOTRACK prefix. */
742732c7 4859 if (i.notrack_prefix && i.tm.opcode_modifier.prefixok != PrefixNoTrack)
9fef80d6 4860 as_bad (_("expecting indirect branch instruction after `notrack'"));
04ef582a 4861
327e8c42
JB
4862 if (i.tm.cpu_flags.bitfield.cpumpx)
4863 {
4864 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
4865 as_bad (_("32-bit address isn't allowed in 64-bit MPX instructions."));
4866 else if (flag_code != CODE_16BIT
4867 ? i.prefix[ADDR_PREFIX]
4868 : i.mem_operands && !i.prefix[ADDR_PREFIX])
4869 as_bad (_("16-bit address isn't allowed in MPX instructions"));
4870 }
7e8b059b
L
4871
4872 /* Insert BND prefix. */
76d3a78a
JB
4873 if (add_bnd_prefix && i.tm.opcode_modifier.bndprefixok)
4874 {
4875 if (!i.prefix[BND_PREFIX])
4876 add_prefix (BND_PREFIX_OPCODE);
4877 else if (i.prefix[BND_PREFIX] != BND_PREFIX_OPCODE)
4878 {
4879 as_warn (_("replacing `rep'/`repe' prefix by `bnd'"));
4880 i.prefix[BND_PREFIX] = BND_PREFIX_OPCODE;
4881 }
4882 }
7e8b059b 4883
29b0f896 4884 /* Check string instruction segment overrides. */
51c8edf6 4885 if (i.tm.opcode_modifier.isstring >= IS_STRING_ES_OP0)
29b0f896 4886 {
51c8edf6 4887 gas_assert (i.mem_operands);
29b0f896 4888 if (!check_string ())
5dd0794d 4889 return;
fc0763e6 4890 i.disp_operands = 0;
29b0f896 4891 }
5dd0794d 4892
b6f8c7c4
L
4893 if (optimize && !i.no_optimize && i.tm.opcode_modifier.optimize)
4894 optimize_encoding ();
4895
29b0f896
AM
4896 if (!process_suffix ())
4897 return;
e413e4e9 4898
921eafea 4899 /* Update operand types and check extended states. */
bc0844ae 4900 for (j = 0; j < i.operands; j++)
921eafea
L
4901 {
4902 i.types[j] = operand_type_and (i.types[j], i.tm.operand_types[j]);
3d70986f 4903 switch (i.tm.operand_types[j].bitfield.class)
921eafea
L
4904 {
4905 default:
4906 break;
4907 case RegMMX:
4908 i.xstate |= xstate_mmx;
4909 break;
4910 case RegMask:
32930e4e 4911 i.xstate |= xstate_mask;
921eafea
L
4912 break;
4913 case RegSIMD:
3d70986f 4914 if (i.tm.operand_types[j].bitfield.tmmword)
921eafea 4915 i.xstate |= xstate_tmm;
3d70986f 4916 else if (i.tm.operand_types[j].bitfield.zmmword)
921eafea 4917 i.xstate |= xstate_zmm;
3d70986f 4918 else if (i.tm.operand_types[j].bitfield.ymmword)
921eafea 4919 i.xstate |= xstate_ymm;
3d70986f 4920 else if (i.tm.operand_types[j].bitfield.xmmword)
921eafea
L
4921 i.xstate |= xstate_xmm;
4922 break;
4923 }
4924 }
bc0844ae 4925
29b0f896
AM
4926 /* Make still unresolved immediate matches conform to size of immediate
4927 given in i.suffix. */
4928 if (!finalize_imm ())
4929 return;
252b5132 4930
40fb9820 4931 if (i.types[0].bitfield.imm1)
29b0f896 4932 i.imm_operands = 0; /* kludge for shift insns. */
252b5132 4933
9afe6eb8
L
4934 /* We only need to check those implicit registers for instructions
4935 with 3 operands or less. */
4936 if (i.operands <= 3)
4937 for (j = 0; j < i.operands; j++)
75e5731b
JB
4938 if (i.types[j].bitfield.instance != InstanceNone
4939 && !i.types[j].bitfield.xmmword)
9afe6eb8 4940 i.reg_operands--;
40fb9820 4941
29b0f896
AM
4942 /* For insns with operands there are more diddles to do to the opcode. */
4943 if (i.operands)
4944 {
4945 if (!process_operands ())
4946 return;
4947 }
8c190ce0 4948 else if (!quiet_warnings && i.tm.opcode_modifier.ugh)
29b0f896
AM
4949 {
4950 /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc. */
4951 as_warn (_("translating to `%sp'"), i.tm.name);
4952 }
252b5132 4953
7a8655d2 4954 if (is_any_vex_encoding (&i.tm))
9e5e5283 4955 {
c1dc7af5 4956 if (!cpu_arch_flags.bitfield.cpui286)
9e5e5283 4957 {
c1dc7af5 4958 as_bad (_("instruction `%s' isn't supported outside of protected mode."),
9e5e5283
L
4959 i.tm.name);
4960 return;
4961 }
c0f3af97 4962
0b9404fd
JB
4963 /* Check for explicit REX prefix. */
4964 if (i.prefix[REX_PREFIX] || i.rex_encoding)
4965 {
4966 as_bad (_("REX prefix invalid with `%s'"), i.tm.name);
4967 return;
4968 }
4969
9e5e5283
L
4970 if (i.tm.opcode_modifier.vex)
4971 build_vex_prefix (t);
4972 else
4973 build_evex_prefix ();
0b9404fd
JB
4974
4975 /* The individual REX.RXBW bits got consumed. */
4976 i.rex &= REX_OPCODE;
9e5e5283 4977 }
43234a1e 4978
5dd85c99
SP
4979 /* Handle conversion of 'int $3' --> special int3 insn. XOP or FMA4
4980 instructions may define INT_OPCODE as well, so avoid this corner
4981 case for those instructions that use MODRM. */
389d00a5
JB
4982 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
4983 && i.tm.base_opcode == INT_OPCODE
a6461c02
SP
4984 && !i.tm.opcode_modifier.modrm
4985 && i.op[0].imms->X_add_number == 3)
29b0f896
AM
4986 {
4987 i.tm.base_opcode = INT3_OPCODE;
4988 i.imm_operands = 0;
4989 }
252b5132 4990
0cfa3eb3
JB
4991 if ((i.tm.opcode_modifier.jump == JUMP
4992 || i.tm.opcode_modifier.jump == JUMP_BYTE
4993 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896
AM
4994 && i.op[0].disps->X_op == O_constant)
4995 {
4996 /* Convert "jmp constant" (and "call constant") to a jump (call) to
4997 the absolute address given by the constant. Since ix86 jumps and
4998 calls are pc relative, we need to generate a reloc. */
4999 i.op[0].disps->X_add_symbol = &abs_symbol;
5000 i.op[0].disps->X_op = O_symbol;
5001 }
252b5132 5002
29b0f896
AM
5003 /* For 8 bit registers we need an empty rex prefix. Also if the
5004 instruction already has a prefix, we need to convert old
5005 registers to new ones. */
773f551c 5006
bab6aec1 5007 if ((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte
29b0f896 5008 && (i.op[0].regs->reg_flags & RegRex64) != 0)
bab6aec1 5009 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte
29b0f896 5010 && (i.op[1].regs->reg_flags & RegRex64) != 0)
bab6aec1
JB
5011 || (((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte)
5012 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte))
29b0f896
AM
5013 && i.rex != 0))
5014 {
5015 int x;
726c5dcd 5016
29b0f896
AM
5017 i.rex |= REX_OPCODE;
5018 for (x = 0; x < 2; x++)
5019 {
5020 /* Look for 8 bit operand that uses old registers. */
bab6aec1 5021 if (i.types[x].bitfield.class == Reg && i.types[x].bitfield.byte
29b0f896 5022 && (i.op[x].regs->reg_flags & RegRex64) == 0)
773f551c 5023 {
3f93af61 5024 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
29b0f896
AM
5025 /* In case it is "hi" register, give up. */
5026 if (i.op[x].regs->reg_num > 3)
a540244d 5027 as_bad (_("can't encode register '%s%s' in an "
4eed87de 5028 "instruction requiring REX prefix."),
a540244d 5029 register_prefix, i.op[x].regs->reg_name);
773f551c 5030
29b0f896
AM
5031 /* Otherwise it is equivalent to the extended register.
5032 Since the encoding doesn't change this is merely
5033 cosmetic cleanup for debug output. */
5034
5035 i.op[x].regs = i.op[x].regs + 8;
773f551c 5036 }
29b0f896
AM
5037 }
5038 }
773f551c 5039
6b6b6807
L
5040 if (i.rex == 0 && i.rex_encoding)
5041 {
5042 /* Check if we can add a REX_OPCODE byte. Look for 8 bit operand
3f93af61 5043 that uses legacy register. If it is "hi" register, don't add
6b6b6807
L
5044 the REX_OPCODE byte. */
5045 int x;
5046 for (x = 0; x < 2; x++)
bab6aec1 5047 if (i.types[x].bitfield.class == Reg
6b6b6807
L
5048 && i.types[x].bitfield.byte
5049 && (i.op[x].regs->reg_flags & RegRex64) == 0
5050 && i.op[x].regs->reg_num > 3)
5051 {
3f93af61 5052 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
5b7c81bd 5053 i.rex_encoding = false;
6b6b6807
L
5054 break;
5055 }
5056
5057 if (i.rex_encoding)
5058 i.rex = REX_OPCODE;
5059 }
5060
7ab9ffdd 5061 if (i.rex != 0)
29b0f896
AM
5062 add_prefix (REX_OPCODE | i.rex);
5063
ae531041
L
5064 insert_lfence_before ();
5065
29b0f896
AM
5066 /* We are ready to output the insn. */
5067 output_insn ();
e379e5f3 5068
ae531041
L
5069 insert_lfence_after ();
5070
e379e5f3
L
5071 last_insn.seg = now_seg;
5072
5073 if (i.tm.opcode_modifier.isprefix)
5074 {
5075 last_insn.kind = last_insn_prefix;
5076 last_insn.name = i.tm.name;
5077 last_insn.file = as_where (&last_insn.line);
5078 }
5079 else
5080 last_insn.kind = last_insn_other;
29b0f896
AM
5081}
5082
5083static char *
e3bb37b5 5084parse_insn (char *line, char *mnemonic)
29b0f896
AM
5085{
5086 char *l = line;
5087 char *token_start = l;
5088 char *mnem_p;
5c6af06e 5089 int supported;
d3ce72d0 5090 const insn_template *t;
b6169b20 5091 char *dot_p = NULL;
29b0f896 5092
29b0f896
AM
5093 while (1)
5094 {
5095 mnem_p = mnemonic;
5096 while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
5097 {
b6169b20
L
5098 if (*mnem_p == '.')
5099 dot_p = mnem_p;
29b0f896
AM
5100 mnem_p++;
5101 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
45288df1 5102 {
29b0f896
AM
5103 as_bad (_("no such instruction: `%s'"), token_start);
5104 return NULL;
5105 }
5106 l++;
5107 }
5108 if (!is_space_char (*l)
5109 && *l != END_OF_INSN
e44823cf
JB
5110 && (intel_syntax
5111 || (*l != PREFIX_SEPARATOR
5112 && *l != ',')))
29b0f896
AM
5113 {
5114 as_bad (_("invalid character %s in mnemonic"),
5115 output_invalid (*l));
5116 return NULL;
5117 }
5118 if (token_start == l)
5119 {
e44823cf 5120 if (!intel_syntax && *l == PREFIX_SEPARATOR)
29b0f896
AM
5121 as_bad (_("expecting prefix; got nothing"));
5122 else
5123 as_bad (_("expecting mnemonic; got nothing"));
5124 return NULL;
5125 }
45288df1 5126
29b0f896 5127 /* Look up instruction (or prefix) via hash table. */
629310ab 5128 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
47926f60 5129
29b0f896
AM
5130 if (*l != END_OF_INSN
5131 && (!is_space_char (*l) || l[1] != END_OF_INSN)
5132 && current_templates
40fb9820 5133 && current_templates->start->opcode_modifier.isprefix)
29b0f896 5134 {
c6fb90c8 5135 if (!cpu_flags_check_cpu64 (current_templates->start->cpu_flags))
2dd88dca
JB
5136 {
5137 as_bad ((flag_code != CODE_64BIT
5138 ? _("`%s' is only supported in 64-bit mode")
5139 : _("`%s' is not supported in 64-bit mode")),
5140 current_templates->start->name);
5141 return NULL;
5142 }
29b0f896
AM
5143 /* If we are in 16-bit mode, do not allow addr16 or data16.
5144 Similarly, in 32-bit mode, do not allow addr32 or data32. */
673fe0f0
JB
5145 if ((current_templates->start->opcode_modifier.size == SIZE16
5146 || current_templates->start->opcode_modifier.size == SIZE32)
29b0f896 5147 && flag_code != CODE_64BIT
673fe0f0 5148 && ((current_templates->start->opcode_modifier.size == SIZE32)
29b0f896
AM
5149 ^ (flag_code == CODE_16BIT)))
5150 {
5151 as_bad (_("redundant %s prefix"),
5152 current_templates->start->name);
5153 return NULL;
45288df1 5154 }
31184569
JB
5155
5156 if (current_templates->start->base_opcode == PSEUDO_PREFIX)
29b0f896 5157 {
86fa6981 5158 /* Handle pseudo prefixes. */
31184569 5159 switch (current_templates->start->extension_opcode)
86fa6981 5160 {
41eb8e88 5161 case Prefix_Disp8:
86fa6981
L
5162 /* {disp8} */
5163 i.disp_encoding = disp_encoding_8bit;
5164 break;
41eb8e88
L
5165 case Prefix_Disp16:
5166 /* {disp16} */
5167 i.disp_encoding = disp_encoding_16bit;
5168 break;
5169 case Prefix_Disp32:
86fa6981
L
5170 /* {disp32} */
5171 i.disp_encoding = disp_encoding_32bit;
5172 break;
41eb8e88 5173 case Prefix_Load:
86fa6981
L
5174 /* {load} */
5175 i.dir_encoding = dir_encoding_load;
5176 break;
41eb8e88 5177 case Prefix_Store:
86fa6981
L
5178 /* {store} */
5179 i.dir_encoding = dir_encoding_store;
5180 break;
41eb8e88 5181 case Prefix_VEX:
42e04b36
L
5182 /* {vex} */
5183 i.vec_encoding = vex_encoding_vex;
86fa6981 5184 break;
41eb8e88 5185 case Prefix_VEX3:
86fa6981
L
5186 /* {vex3} */
5187 i.vec_encoding = vex_encoding_vex3;
5188 break;
41eb8e88 5189 case Prefix_EVEX:
86fa6981
L
5190 /* {evex} */
5191 i.vec_encoding = vex_encoding_evex;
5192 break;
41eb8e88 5193 case Prefix_REX:
6b6b6807 5194 /* {rex} */
5b7c81bd 5195 i.rex_encoding = true;
6b6b6807 5196 break;
41eb8e88 5197 case Prefix_NoOptimize:
b6f8c7c4 5198 /* {nooptimize} */
5b7c81bd 5199 i.no_optimize = true;
b6f8c7c4 5200 break;
86fa6981
L
5201 default:
5202 abort ();
5203 }
5204 }
5205 else
5206 {
5207 /* Add prefix, checking for repeated prefixes. */
4e9ac44a 5208 switch (add_prefix (current_templates->start->base_opcode))
86fa6981 5209 {
4e9ac44a
L
5210 case PREFIX_EXIST:
5211 return NULL;
5212 case PREFIX_DS:
d777820b 5213 if (current_templates->start->cpu_flags.bitfield.cpuibt)
4e9ac44a
L
5214 i.notrack_prefix = current_templates->start->name;
5215 break;
5216 case PREFIX_REP:
5217 if (current_templates->start->cpu_flags.bitfield.cpuhle)
5218 i.hle_prefix = current_templates->start->name;
5219 else if (current_templates->start->cpu_flags.bitfield.cpumpx)
5220 i.bnd_prefix = current_templates->start->name;
5221 else
5222 i.rep_prefix = current_templates->start->name;
5223 break;
5224 default:
5225 break;
86fa6981 5226 }
29b0f896
AM
5227 }
5228 /* Skip past PREFIX_SEPARATOR and reset token_start. */
5229 token_start = ++l;
5230 }
5231 else
5232 break;
5233 }
45288df1 5234
30a55f88 5235 if (!current_templates)
b6169b20 5236 {
07d5e953
JB
5237 /* Deprecated functionality (new code should use pseudo-prefixes instead):
5238 Check if we should swap operand or force 32bit displacement in
f8a5c266 5239 encoding. */
30a55f88 5240 if (mnem_p - 2 == dot_p && dot_p[1] == 's')
64c49ab3 5241 i.dir_encoding = dir_encoding_swap;
8d63c93e 5242 else if (mnem_p - 3 == dot_p
a501d77e
L
5243 && dot_p[1] == 'd'
5244 && dot_p[2] == '8')
5245 i.disp_encoding = disp_encoding_8bit;
8d63c93e 5246 else if (mnem_p - 4 == dot_p
f8a5c266
L
5247 && dot_p[1] == 'd'
5248 && dot_p[2] == '3'
5249 && dot_p[3] == '2')
a501d77e 5250 i.disp_encoding = disp_encoding_32bit;
30a55f88
L
5251 else
5252 goto check_suffix;
5253 mnem_p = dot_p;
5254 *dot_p = '\0';
629310ab 5255 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
b6169b20
L
5256 }
5257
29b0f896
AM
5258 if (!current_templates)
5259 {
dc1e8a47 5260 check_suffix:
1c529385 5261 if (mnem_p > mnemonic)
29b0f896 5262 {
1c529385
LH
5263 /* See if we can get a match by trimming off a suffix. */
5264 switch (mnem_p[-1])
29b0f896 5265 {
1c529385
LH
5266 case WORD_MNEM_SUFFIX:
5267 if (intel_syntax && (intel_float_operand (mnemonic) & 2))
29b0f896
AM
5268 i.suffix = SHORT_MNEM_SUFFIX;
5269 else
1c529385
LH
5270 /* Fall through. */
5271 case BYTE_MNEM_SUFFIX:
5272 case QWORD_MNEM_SUFFIX:
5273 i.suffix = mnem_p[-1];
29b0f896 5274 mnem_p[-1] = '\0';
fe0e921f
AM
5275 current_templates
5276 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5277 break;
5278 case SHORT_MNEM_SUFFIX:
5279 case LONG_MNEM_SUFFIX:
5280 if (!intel_syntax)
5281 {
5282 i.suffix = mnem_p[-1];
5283 mnem_p[-1] = '\0';
fe0e921f
AM
5284 current_templates
5285 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5286 }
5287 break;
5288
5289 /* Intel Syntax. */
5290 case 'd':
5291 if (intel_syntax)
5292 {
5293 if (intel_float_operand (mnemonic) == 1)
5294 i.suffix = SHORT_MNEM_SUFFIX;
5295 else
5296 i.suffix = LONG_MNEM_SUFFIX;
5297 mnem_p[-1] = '\0';
fe0e921f
AM
5298 current_templates
5299 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5300 }
5301 break;
29b0f896 5302 }
29b0f896 5303 }
1c529385 5304
29b0f896
AM
5305 if (!current_templates)
5306 {
5307 as_bad (_("no such instruction: `%s'"), token_start);
5308 return NULL;
5309 }
5310 }
252b5132 5311
0cfa3eb3
JB
5312 if (current_templates->start->opcode_modifier.jump == JUMP
5313 || current_templates->start->opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
5314 {
5315 /* Check for a branch hint. We allow ",pt" and ",pn" for
5316 predict taken and predict not taken respectively.
5317 I'm not sure that branch hints actually do anything on loop
5318 and jcxz insns (JumpByte) for current Pentium4 chips. They
5319 may work in the future and it doesn't hurt to accept them
5320 now. */
5321 if (l[0] == ',' && l[1] == 'p')
5322 {
5323 if (l[2] == 't')
5324 {
5325 if (!add_prefix (DS_PREFIX_OPCODE))
5326 return NULL;
5327 l += 3;
5328 }
5329 else if (l[2] == 'n')
5330 {
5331 if (!add_prefix (CS_PREFIX_OPCODE))
5332 return NULL;
5333 l += 3;
5334 }
5335 }
5336 }
5337 /* Any other comma loses. */
5338 if (*l == ',')
5339 {
5340 as_bad (_("invalid character %s in mnemonic"),
5341 output_invalid (*l));
5342 return NULL;
5343 }
252b5132 5344
29b0f896 5345 /* Check if instruction is supported on specified architecture. */
5c6af06e
JB
5346 supported = 0;
5347 for (t = current_templates->start; t < current_templates->end; ++t)
5348 {
c0f3af97
L
5349 supported |= cpu_flags_match (t);
5350 if (supported == CPU_FLAGS_PERFECT_MATCH)
548d0ee6
JB
5351 {
5352 if (!cpu_arch_flags.bitfield.cpui386 && (flag_code != CODE_16BIT))
5353 as_warn (_("use .code16 to ensure correct addressing mode"));
3629bb00 5354
548d0ee6
JB
5355 return l;
5356 }
29b0f896 5357 }
3629bb00 5358
548d0ee6
JB
5359 if (!(supported & CPU_FLAGS_64BIT_MATCH))
5360 as_bad (flag_code == CODE_64BIT
5361 ? _("`%s' is not supported in 64-bit mode")
5362 : _("`%s' is only supported in 64-bit mode"),
5363 current_templates->start->name);
5364 else
5365 as_bad (_("`%s' is not supported on `%s%s'"),
5366 current_templates->start->name,
5367 cpu_arch_name ? cpu_arch_name : default_arch,
5368 cpu_sub_arch_name ? cpu_sub_arch_name : "");
252b5132 5369
548d0ee6 5370 return NULL;
29b0f896 5371}
252b5132 5372
29b0f896 5373static char *
e3bb37b5 5374parse_operands (char *l, const char *mnemonic)
29b0f896
AM
5375{
5376 char *token_start;
3138f287 5377
29b0f896
AM
5378 /* 1 if operand is pending after ','. */
5379 unsigned int expecting_operand = 0;
252b5132 5380
29b0f896
AM
5381 /* Non-zero if operand parens not balanced. */
5382 unsigned int paren_not_balanced;
5383
5384 while (*l != END_OF_INSN)
5385 {
5386 /* Skip optional white space before operand. */
5387 if (is_space_char (*l))
5388 ++l;
d02603dc 5389 if (!is_operand_char (*l) && *l != END_OF_INSN && *l != '"')
29b0f896
AM
5390 {
5391 as_bad (_("invalid character %s before operand %d"),
5392 output_invalid (*l),
5393 i.operands + 1);
5394 return NULL;
5395 }
d02603dc 5396 token_start = l; /* After white space. */
29b0f896
AM
5397 paren_not_balanced = 0;
5398 while (paren_not_balanced || *l != ',')
5399 {
5400 if (*l == END_OF_INSN)
5401 {
5402 if (paren_not_balanced)
5403 {
5404 if (!intel_syntax)
5405 as_bad (_("unbalanced parenthesis in operand %d."),
5406 i.operands + 1);
5407 else
5408 as_bad (_("unbalanced brackets in operand %d."),
5409 i.operands + 1);
5410 return NULL;
5411 }
5412 else
5413 break; /* we are done */
5414 }
d02603dc 5415 else if (!is_operand_char (*l) && !is_space_char (*l) && *l != '"')
29b0f896
AM
5416 {
5417 as_bad (_("invalid character %s in operand %d"),
5418 output_invalid (*l),
5419 i.operands + 1);
5420 return NULL;
5421 }
5422 if (!intel_syntax)
5423 {
5424 if (*l == '(')
5425 ++paren_not_balanced;
5426 if (*l == ')')
5427 --paren_not_balanced;
5428 }
5429 else
5430 {
5431 if (*l == '[')
5432 ++paren_not_balanced;
5433 if (*l == ']')
5434 --paren_not_balanced;
5435 }
5436 l++;
5437 }
5438 if (l != token_start)
5439 { /* Yes, we've read in another operand. */
5440 unsigned int operand_ok;
5441 this_operand = i.operands++;
5442 if (i.operands > MAX_OPERANDS)
5443 {
5444 as_bad (_("spurious operands; (%d operands/instruction max)"),
5445 MAX_OPERANDS);
5446 return NULL;
5447 }
9d46ce34 5448 i.types[this_operand].bitfield.unspecified = 1;
29b0f896
AM
5449 /* Now parse operand adding info to 'i' as we go along. */
5450 END_STRING_AND_SAVE (l);
5451
1286ab78
L
5452 if (i.mem_operands > 1)
5453 {
5454 as_bad (_("too many memory references for `%s'"),
5455 mnemonic);
5456 return 0;
5457 }
5458
29b0f896
AM
5459 if (intel_syntax)
5460 operand_ok =
5461 i386_intel_operand (token_start,
5462 intel_float_operand (mnemonic));
5463 else
a7619375 5464 operand_ok = i386_att_operand (token_start);
29b0f896
AM
5465
5466 RESTORE_END_STRING (l);
5467 if (!operand_ok)
5468 return NULL;
5469 }
5470 else
5471 {
5472 if (expecting_operand)
5473 {
5474 expecting_operand_after_comma:
5475 as_bad (_("expecting operand after ','; got nothing"));
5476 return NULL;
5477 }
5478 if (*l == ',')
5479 {
5480 as_bad (_("expecting operand before ','; got nothing"));
5481 return NULL;
5482 }
5483 }
7f3f1ea2 5484
29b0f896
AM
5485 /* Now *l must be either ',' or END_OF_INSN. */
5486 if (*l == ',')
5487 {
5488 if (*++l == END_OF_INSN)
5489 {
5490 /* Just skip it, if it's \n complain. */
5491 goto expecting_operand_after_comma;
5492 }
5493 expecting_operand = 1;
5494 }
5495 }
5496 return l;
5497}
7f3f1ea2 5498
050dfa73 5499static void
783c187b 5500swap_2_operands (unsigned int xchg1, unsigned int xchg2)
050dfa73
MM
5501{
5502 union i386_op temp_op;
40fb9820 5503 i386_operand_type temp_type;
c48dadc9 5504 unsigned int temp_flags;
050dfa73 5505 enum bfd_reloc_code_real temp_reloc;
4eed87de 5506
050dfa73
MM
5507 temp_type = i.types[xchg2];
5508 i.types[xchg2] = i.types[xchg1];
5509 i.types[xchg1] = temp_type;
c48dadc9
JB
5510
5511 temp_flags = i.flags[xchg2];
5512 i.flags[xchg2] = i.flags[xchg1];
5513 i.flags[xchg1] = temp_flags;
5514
050dfa73
MM
5515 temp_op = i.op[xchg2];
5516 i.op[xchg2] = i.op[xchg1];
5517 i.op[xchg1] = temp_op;
c48dadc9 5518
050dfa73
MM
5519 temp_reloc = i.reloc[xchg2];
5520 i.reloc[xchg2] = i.reloc[xchg1];
5521 i.reloc[xchg1] = temp_reloc;
43234a1e 5522
6225c532 5523 if (i.mask.reg)
43234a1e 5524 {
6225c532
JB
5525 if (i.mask.operand == xchg1)
5526 i.mask.operand = xchg2;
5527 else if (i.mask.operand == xchg2)
5528 i.mask.operand = xchg1;
43234a1e 5529 }
5273a3cd 5530 if (i.broadcast.type)
43234a1e 5531 {
5273a3cd
JB
5532 if (i.broadcast.operand == xchg1)
5533 i.broadcast.operand = xchg2;
5534 else if (i.broadcast.operand == xchg2)
5535 i.broadcast.operand = xchg1;
43234a1e 5536 }
ca5312a2 5537 if (i.rounding.type != rc_none)
43234a1e 5538 {
ca5312a2
JB
5539 if (i.rounding.operand == xchg1)
5540 i.rounding.operand = xchg2;
5541 else if (i.rounding.operand == xchg2)
5542 i.rounding.operand = xchg1;
43234a1e 5543 }
050dfa73
MM
5544}
5545
29b0f896 5546static void
e3bb37b5 5547swap_operands (void)
29b0f896 5548{
b7c61d9a 5549 switch (i.operands)
050dfa73 5550 {
c0f3af97 5551 case 5:
b7c61d9a 5552 case 4:
4d456e3d 5553 swap_2_operands (1, i.operands - 2);
1a0670f3 5554 /* Fall through. */
b7c61d9a
L
5555 case 3:
5556 case 2:
4d456e3d 5557 swap_2_operands (0, i.operands - 1);
b7c61d9a
L
5558 break;
5559 default:
5560 abort ();
29b0f896 5561 }
29b0f896
AM
5562
5563 if (i.mem_operands == 2)
5564 {
5e042380 5565 const reg_entry *temp_seg;
29b0f896
AM
5566 temp_seg = i.seg[0];
5567 i.seg[0] = i.seg[1];
5568 i.seg[1] = temp_seg;
5569 }
5570}
252b5132 5571
29b0f896
AM
5572/* Try to ensure constant immediates are represented in the smallest
5573 opcode possible. */
5574static void
e3bb37b5 5575optimize_imm (void)
29b0f896
AM
5576{
5577 char guess_suffix = 0;
5578 int op;
252b5132 5579
29b0f896
AM
5580 if (i.suffix)
5581 guess_suffix = i.suffix;
5582 else if (i.reg_operands)
5583 {
5584 /* Figure out a suffix from the last register operand specified.
75e5731b
JB
5585 We can't do this properly yet, i.e. excluding special register
5586 instances, but the following works for instructions with
5587 immediates. In any case, we can't set i.suffix yet. */
29b0f896 5588 for (op = i.operands; --op >= 0;)
bab6aec1
JB
5589 if (i.types[op].bitfield.class != Reg)
5590 continue;
5591 else if (i.types[op].bitfield.byte)
7ab9ffdd 5592 {
40fb9820
L
5593 guess_suffix = BYTE_MNEM_SUFFIX;
5594 break;
5595 }
bab6aec1 5596 else if (i.types[op].bitfield.word)
252b5132 5597 {
40fb9820
L
5598 guess_suffix = WORD_MNEM_SUFFIX;
5599 break;
5600 }
bab6aec1 5601 else if (i.types[op].bitfield.dword)
40fb9820
L
5602 {
5603 guess_suffix = LONG_MNEM_SUFFIX;
5604 break;
5605 }
bab6aec1 5606 else if (i.types[op].bitfield.qword)
40fb9820
L
5607 {
5608 guess_suffix = QWORD_MNEM_SUFFIX;
29b0f896 5609 break;
252b5132 5610 }
29b0f896
AM
5611 }
5612 else if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
5613 guess_suffix = WORD_MNEM_SUFFIX;
5614
5615 for (op = i.operands; --op >= 0;)
40fb9820 5616 if (operand_type_check (i.types[op], imm))
29b0f896
AM
5617 {
5618 switch (i.op[op].imms->X_op)
252b5132 5619 {
29b0f896
AM
5620 case O_constant:
5621 /* If a suffix is given, this operand may be shortened. */
5622 switch (guess_suffix)
252b5132 5623 {
29b0f896 5624 case LONG_MNEM_SUFFIX:
40fb9820
L
5625 i.types[op].bitfield.imm32 = 1;
5626 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
5627 break;
5628 case WORD_MNEM_SUFFIX:
40fb9820
L
5629 i.types[op].bitfield.imm16 = 1;
5630 i.types[op].bitfield.imm32 = 1;
5631 i.types[op].bitfield.imm32s = 1;
5632 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
5633 break;
5634 case BYTE_MNEM_SUFFIX:
40fb9820
L
5635 i.types[op].bitfield.imm8 = 1;
5636 i.types[op].bitfield.imm8s = 1;
5637 i.types[op].bitfield.imm16 = 1;
5638 i.types[op].bitfield.imm32 = 1;
5639 i.types[op].bitfield.imm32s = 1;
5640 i.types[op].bitfield.imm64 = 1;
29b0f896 5641 break;
252b5132 5642 }
252b5132 5643
29b0f896
AM
5644 /* If this operand is at most 16 bits, convert it
5645 to a signed 16 bit number before trying to see
5646 whether it will fit in an even smaller size.
5647 This allows a 16-bit operand such as $0xffe0 to
5648 be recognised as within Imm8S range. */
40fb9820 5649 if ((i.types[op].bitfield.imm16)
29b0f896 5650 && (i.op[op].imms->X_add_number & ~(offsetT) 0xffff) == 0)
252b5132 5651 {
29b0f896
AM
5652 i.op[op].imms->X_add_number =
5653 (((i.op[op].imms->X_add_number & 0xffff) ^ 0x8000) - 0x8000);
5654 }
a28def75
L
5655#ifdef BFD64
5656 /* Store 32-bit immediate in 64-bit for 64-bit BFD. */
40fb9820 5657 if ((i.types[op].bitfield.imm32)
29b0f896
AM
5658 && ((i.op[op].imms->X_add_number & ~(((offsetT) 2 << 31) - 1))
5659 == 0))
5660 {
5661 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
5662 ^ ((offsetT) 1 << 31))
5663 - ((offsetT) 1 << 31));
5664 }
a28def75 5665#endif
40fb9820 5666 i.types[op]
c6fb90c8
L
5667 = operand_type_or (i.types[op],
5668 smallest_imm_type (i.op[op].imms->X_add_number));
252b5132 5669
29b0f896
AM
5670 /* We must avoid matching of Imm32 templates when 64bit
5671 only immediate is available. */
5672 if (guess_suffix == QWORD_MNEM_SUFFIX)
40fb9820 5673 i.types[op].bitfield.imm32 = 0;
29b0f896 5674 break;
252b5132 5675
29b0f896
AM
5676 case O_absent:
5677 case O_register:
5678 abort ();
5679
5680 /* Symbols and expressions. */
5681 default:
9cd96992
JB
5682 /* Convert symbolic operand to proper sizes for matching, but don't
5683 prevent matching a set of insns that only supports sizes other
5684 than those matching the insn suffix. */
5685 {
40fb9820 5686 i386_operand_type mask, allowed;
d3ce72d0 5687 const insn_template *t;
9cd96992 5688
0dfbf9d7
L
5689 operand_type_set (&mask, 0);
5690 operand_type_set (&allowed, 0);
40fb9820 5691
4eed87de
AM
5692 for (t = current_templates->start;
5693 t < current_templates->end;
5694 ++t)
bab6aec1
JB
5695 {
5696 allowed = operand_type_or (allowed, t->operand_types[op]);
5697 allowed = operand_type_and (allowed, anyimm);
5698 }
9cd96992
JB
5699 switch (guess_suffix)
5700 {
5701 case QWORD_MNEM_SUFFIX:
40fb9820
L
5702 mask.bitfield.imm64 = 1;
5703 mask.bitfield.imm32s = 1;
9cd96992
JB
5704 break;
5705 case LONG_MNEM_SUFFIX:
40fb9820 5706 mask.bitfield.imm32 = 1;
9cd96992
JB
5707 break;
5708 case WORD_MNEM_SUFFIX:
40fb9820 5709 mask.bitfield.imm16 = 1;
9cd96992
JB
5710 break;
5711 case BYTE_MNEM_SUFFIX:
40fb9820 5712 mask.bitfield.imm8 = 1;
9cd96992
JB
5713 break;
5714 default:
9cd96992
JB
5715 break;
5716 }
c6fb90c8 5717 allowed = operand_type_and (mask, allowed);
0dfbf9d7 5718 if (!operand_type_all_zero (&allowed))
c6fb90c8 5719 i.types[op] = operand_type_and (i.types[op], mask);
9cd96992 5720 }
29b0f896 5721 break;
252b5132 5722 }
29b0f896
AM
5723 }
5724}
47926f60 5725
29b0f896
AM
5726/* Try to use the smallest displacement type too. */
5727static void
e3bb37b5 5728optimize_disp (void)
29b0f896
AM
5729{
5730 int op;
3e73aa7c 5731
29b0f896 5732 for (op = i.operands; --op >= 0;)
40fb9820 5733 if (operand_type_check (i.types[op], disp))
252b5132 5734 {
b300c311 5735 if (i.op[op].disps->X_op == O_constant)
252b5132 5736 {
91d6fa6a 5737 offsetT op_disp = i.op[op].disps->X_add_number;
29b0f896 5738
40fb9820 5739 if (i.types[op].bitfield.disp16
91d6fa6a 5740 && (op_disp & ~(offsetT) 0xffff) == 0)
b300c311
L
5741 {
5742 /* If this operand is at most 16 bits, convert
5743 to a signed 16 bit number and don't use 64bit
5744 displacement. */
91d6fa6a 5745 op_disp = (((op_disp & 0xffff) ^ 0x8000) - 0x8000);
40fb9820 5746 i.types[op].bitfield.disp64 = 0;
b300c311 5747 }
91d6fa6a 5748 if (!op_disp && i.types[op].bitfield.baseindex)
b300c311 5749 {
40fb9820
L
5750 i.types[op].bitfield.disp8 = 0;
5751 i.types[op].bitfield.disp16 = 0;
5752 i.types[op].bitfield.disp32 = 0;
5753 i.types[op].bitfield.disp32s = 0;
5754 i.types[op].bitfield.disp64 = 0;
b300c311
L
5755 i.op[op].disps = 0;
5756 i.disp_operands--;
5757 }
28a167a4 5758#ifdef BFD64
b300c311
L
5759 else if (flag_code == CODE_64BIT)
5760 {
a9aabc23 5761 if (want_disp32 (current_templates->start)
28a167a4
JB
5762 && fits_in_unsigned_long (op_disp))
5763 i.types[op].bitfield.disp32 = 1;
5764
5765 /* Optimize 64-bit displacement to 32-bit for 64-bit BFD. */
5766 if (i.types[op].bitfield.disp32
5767 && (op_disp & ~(((offsetT) 2 << 31) - 1)) == 0)
5768 {
5769 /* If this operand is at most 32 bits, convert
5770 to a signed 32 bit number and don't use 64bit
5771 displacement. */
5772 op_disp &= (((offsetT) 2 << 31) - 1);
5773 op_disp = (op_disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
5774 i.types[op].bitfield.disp64 = 0;
5775 }
5776
91d6fa6a 5777 if (fits_in_signed_long (op_disp))
28a9d8f5 5778 {
40fb9820
L
5779 i.types[op].bitfield.disp64 = 0;
5780 i.types[op].bitfield.disp32s = 1;
28a9d8f5 5781 }
b300c311 5782 }
28a167a4 5783#endif
40fb9820
L
5784 if ((i.types[op].bitfield.disp32
5785 || i.types[op].bitfield.disp32s
5786 || i.types[op].bitfield.disp16)
b5014f7a 5787 && fits_in_disp8 (op_disp))
40fb9820 5788 i.types[op].bitfield.disp8 = 1;
252b5132 5789 }
67a4f2b7
AO
5790 else if (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
5791 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL)
5792 {
5793 fix_new_exp (frag_now, frag_more (0) - frag_now->fr_literal, 0,
5794 i.op[op].disps, 0, i.reloc[op]);
40fb9820
L
5795 i.types[op].bitfield.disp8 = 0;
5796 i.types[op].bitfield.disp16 = 0;
5797 i.types[op].bitfield.disp32 = 0;
5798 i.types[op].bitfield.disp32s = 0;
5799 i.types[op].bitfield.disp64 = 0;
67a4f2b7
AO
5800 }
5801 else
b300c311 5802 /* We only support 64bit displacement on constants. */
40fb9820 5803 i.types[op].bitfield.disp64 = 0;
252b5132 5804 }
29b0f896
AM
5805}
5806
4a1b91ea
L
5807/* Return 1 if there is a match in broadcast bytes between operand
5808 GIVEN and instruction template T. */
5809
5810static INLINE int
5811match_broadcast_size (const insn_template *t, unsigned int given)
5812{
5813 return ((t->opcode_modifier.broadcast == BYTE_BROADCAST
5814 && i.types[given].bitfield.byte)
5815 || (t->opcode_modifier.broadcast == WORD_BROADCAST
5816 && i.types[given].bitfield.word)
5817 || (t->opcode_modifier.broadcast == DWORD_BROADCAST
5818 && i.types[given].bitfield.dword)
5819 || (t->opcode_modifier.broadcast == QWORD_BROADCAST
5820 && i.types[given].bitfield.qword));
5821}
5822
6c30d220
L
5823/* Check if operands are valid for the instruction. */
5824
5825static int
5826check_VecOperands (const insn_template *t)
5827{
43234a1e 5828 unsigned int op;
e2195274 5829 i386_cpu_flags cpu;
e2195274
JB
5830
5831 /* Templates allowing for ZMMword as well as YMMword and/or XMMword for
5832 any one operand are implicity requiring AVX512VL support if the actual
5833 operand size is YMMword or XMMword. Since this function runs after
5834 template matching, there's no need to check for YMMword/XMMword in
5835 the template. */
5836 cpu = cpu_flags_and (t->cpu_flags, avx512);
5837 if (!cpu_flags_all_zero (&cpu)
5838 && !t->cpu_flags.bitfield.cpuavx512vl
5839 && !cpu_arch_flags.bitfield.cpuavx512vl)
5840 {
5841 for (op = 0; op < t->operands; ++op)
5842 {
5843 if (t->operand_types[op].bitfield.zmmword
5844 && (i.types[op].bitfield.ymmword
5845 || i.types[op].bitfield.xmmword))
5846 {
5847 i.error = unsupported;
5848 return 1;
5849 }
5850 }
5851 }
43234a1e 5852
6c30d220 5853 /* Without VSIB byte, we can't have a vector register for index. */
63112cd6 5854 if (!t->opcode_modifier.sib
6c30d220 5855 && i.index_reg
1b54b8d7
JB
5856 && (i.index_reg->reg_type.bitfield.xmmword
5857 || i.index_reg->reg_type.bitfield.ymmword
5858 || i.index_reg->reg_type.bitfield.zmmword))
6c30d220
L
5859 {
5860 i.error = unsupported_vector_index_register;
5861 return 1;
5862 }
5863
ad8ecc81
MZ
5864 /* Check if default mask is allowed. */
5865 if (t->opcode_modifier.nodefmask
6225c532 5866 && (!i.mask.reg || i.mask.reg->reg_num == 0))
ad8ecc81
MZ
5867 {
5868 i.error = no_default_mask;
5869 return 1;
5870 }
5871
7bab8ab5
JB
5872 /* For VSIB byte, we need a vector register for index, and all vector
5873 registers must be distinct. */
260cd341 5874 if (t->opcode_modifier.sib && t->opcode_modifier.sib != SIBMEM)
7bab8ab5
JB
5875 {
5876 if (!i.index_reg
63112cd6 5877 || !((t->opcode_modifier.sib == VECSIB128
1b54b8d7 5878 && i.index_reg->reg_type.bitfield.xmmword)
63112cd6 5879 || (t->opcode_modifier.sib == VECSIB256
1b54b8d7 5880 && i.index_reg->reg_type.bitfield.ymmword)
63112cd6 5881 || (t->opcode_modifier.sib == VECSIB512
1b54b8d7 5882 && i.index_reg->reg_type.bitfield.zmmword)))
7bab8ab5
JB
5883 {
5884 i.error = invalid_vsib_address;
5885 return 1;
5886 }
5887
6225c532
JB
5888 gas_assert (i.reg_operands == 2 || i.mask.reg);
5889 if (i.reg_operands == 2 && !i.mask.reg)
43234a1e 5890 {
3528c362 5891 gas_assert (i.types[0].bitfield.class == RegSIMD);
1b54b8d7
JB
5892 gas_assert (i.types[0].bitfield.xmmword
5893 || i.types[0].bitfield.ymmword);
3528c362 5894 gas_assert (i.types[2].bitfield.class == RegSIMD);
1b54b8d7
JB
5895 gas_assert (i.types[2].bitfield.xmmword
5896 || i.types[2].bitfield.ymmword);
43234a1e
L
5897 if (operand_check == check_none)
5898 return 0;
5899 if (register_number (i.op[0].regs)
5900 != register_number (i.index_reg)
5901 && register_number (i.op[2].regs)
5902 != register_number (i.index_reg)
5903 && register_number (i.op[0].regs)
5904 != register_number (i.op[2].regs))
5905 return 0;
5906 if (operand_check == check_error)
5907 {
5908 i.error = invalid_vector_register_set;
5909 return 1;
5910 }
5911 as_warn (_("mask, index, and destination registers should be distinct"));
5912 }
6225c532 5913 else if (i.reg_operands == 1 && i.mask.reg)
8444f82a 5914 {
3528c362 5915 if (i.types[1].bitfield.class == RegSIMD
1b54b8d7
JB
5916 && (i.types[1].bitfield.xmmword
5917 || i.types[1].bitfield.ymmword
5918 || i.types[1].bitfield.zmmword)
8444f82a
MZ
5919 && (register_number (i.op[1].regs)
5920 == register_number (i.index_reg)))
5921 {
5922 if (operand_check == check_error)
5923 {
5924 i.error = invalid_vector_register_set;
5925 return 1;
5926 }
5927 if (operand_check != check_none)
5928 as_warn (_("index and destination registers should be distinct"));
5929 }
5930 }
43234a1e 5931 }
7bab8ab5 5932
260cd341
LC
5933 /* For AMX instructions with three tmmword operands, all tmmword operand must be
5934 distinct */
5935 if (t->operand_types[0].bitfield.tmmword
5936 && i.reg_operands == 3)
5937 {
5938 if (register_number (i.op[0].regs)
5939 == register_number (i.op[1].regs)
5940 || register_number (i.op[0].regs)
5941 == register_number (i.op[2].regs)
5942 || register_number (i.op[1].regs)
5943 == register_number (i.op[2].regs))
5944 {
5945 i.error = invalid_tmm_register_set;
5946 return 1;
5947 }
5948 }
5949
43234a1e
L
5950 /* Check if broadcast is supported by the instruction and is applied
5951 to the memory operand. */
5273a3cd 5952 if (i.broadcast.type)
43234a1e 5953 {
8e6e0792 5954 i386_operand_type type, overlap;
43234a1e
L
5955
5956 /* Check if specified broadcast is supported in this instruction,
4a1b91ea 5957 and its broadcast bytes match the memory operand. */
5273a3cd 5958 op = i.broadcast.operand;
8e6e0792 5959 if (!t->opcode_modifier.broadcast
c48dadc9 5960 || !(i.flags[op] & Operand_Mem)
c39e5b26 5961 || (!i.types[op].bitfield.unspecified
4a1b91ea 5962 && !match_broadcast_size (t, op)))
43234a1e
L
5963 {
5964 bad_broadcast:
5965 i.error = unsupported_broadcast;
5966 return 1;
5967 }
8e6e0792 5968
5273a3cd
JB
5969 i.broadcast.bytes = ((1 << (t->opcode_modifier.broadcast - 1))
5970 * i.broadcast.type);
8e6e0792 5971 operand_type_set (&type, 0);
5273a3cd 5972 switch (i.broadcast.bytes)
8e6e0792 5973 {
4a1b91ea
L
5974 case 2:
5975 type.bitfield.word = 1;
5976 break;
5977 case 4:
5978 type.bitfield.dword = 1;
5979 break;
8e6e0792
JB
5980 case 8:
5981 type.bitfield.qword = 1;
5982 break;
5983 case 16:
5984 type.bitfield.xmmword = 1;
5985 break;
5986 case 32:
5987 type.bitfield.ymmword = 1;
5988 break;
5989 case 64:
5990 type.bitfield.zmmword = 1;
5991 break;
5992 default:
5993 goto bad_broadcast;
5994 }
5995
5996 overlap = operand_type_and (type, t->operand_types[op]);
bc49bfd8
JB
5997 if (t->operand_types[op].bitfield.class == RegSIMD
5998 && t->operand_types[op].bitfield.byte
5999 + t->operand_types[op].bitfield.word
6000 + t->operand_types[op].bitfield.dword
6001 + t->operand_types[op].bitfield.qword > 1)
6002 {
6003 overlap.bitfield.xmmword = 0;
6004 overlap.bitfield.ymmword = 0;
6005 overlap.bitfield.zmmword = 0;
6006 }
8e6e0792
JB
6007 if (operand_type_all_zero (&overlap))
6008 goto bad_broadcast;
6009
6010 if (t->opcode_modifier.checkregsize)
6011 {
6012 unsigned int j;
6013
e2195274 6014 type.bitfield.baseindex = 1;
8e6e0792
JB
6015 for (j = 0; j < i.operands; ++j)
6016 {
6017 if (j != op
6018 && !operand_type_register_match(i.types[j],
6019 t->operand_types[j],
6020 type,
6021 t->operand_types[op]))
6022 goto bad_broadcast;
6023 }
6024 }
43234a1e
L
6025 }
6026 /* If broadcast is supported in this instruction, we need to check if
6027 operand of one-element size isn't specified without broadcast. */
6028 else if (t->opcode_modifier.broadcast && i.mem_operands)
6029 {
6030 /* Find memory operand. */
6031 for (op = 0; op < i.operands; op++)
8dc0818e 6032 if (i.flags[op] & Operand_Mem)
43234a1e
L
6033 break;
6034 gas_assert (op < i.operands);
6035 /* Check size of the memory operand. */
4a1b91ea 6036 if (match_broadcast_size (t, op))
43234a1e
L
6037 {
6038 i.error = broadcast_needed;
6039 return 1;
6040 }
6041 }
c39e5b26
JB
6042 else
6043 op = MAX_OPERANDS - 1; /* Avoid uninitialized variable warning. */
43234a1e
L
6044
6045 /* Check if requested masking is supported. */
6225c532 6046 if (i.mask.reg)
43234a1e 6047 {
ae2387fe
JB
6048 switch (t->opcode_modifier.masking)
6049 {
6050 case BOTH_MASKING:
6051 break;
6052 case MERGING_MASKING:
6225c532 6053 if (i.mask.zeroing)
ae2387fe
JB
6054 {
6055 case 0:
6056 i.error = unsupported_masking;
6057 return 1;
6058 }
6059 break;
6060 case DYNAMIC_MASKING:
6061 /* Memory destinations allow only merging masking. */
6225c532 6062 if (i.mask.zeroing && i.mem_operands)
ae2387fe
JB
6063 {
6064 /* Find memory operand. */
6065 for (op = 0; op < i.operands; op++)
c48dadc9 6066 if (i.flags[op] & Operand_Mem)
ae2387fe
JB
6067 break;
6068 gas_assert (op < i.operands);
6069 if (op == i.operands - 1)
6070 {
6071 i.error = unsupported_masking;
6072 return 1;
6073 }
6074 }
6075 break;
6076 default:
6077 abort ();
6078 }
43234a1e
L
6079 }
6080
6081 /* Check if masking is applied to dest operand. */
6225c532 6082 if (i.mask.reg && (i.mask.operand != i.operands - 1))
43234a1e
L
6083 {
6084 i.error = mask_not_on_destination;
6085 return 1;
6086 }
6087
43234a1e 6088 /* Check RC/SAE. */
ca5312a2 6089 if (i.rounding.type != rc_none)
43234a1e 6090 {
a80195f1 6091 if (!t->opcode_modifier.sae
ca5312a2 6092 || (i.rounding.type != saeonly && !t->opcode_modifier.staticrounding))
43234a1e
L
6093 {
6094 i.error = unsupported_rc_sae;
6095 return 1;
6096 }
6097 /* If the instruction has several immediate operands and one of
6098 them is rounding, the rounding operand should be the last
6099 immediate operand. */
6100 if (i.imm_operands > 1
ca5312a2 6101 && i.rounding.operand != i.imm_operands - 1)
7bab8ab5 6102 {
43234a1e 6103 i.error = rc_sae_operand_not_last_imm;
7bab8ab5
JB
6104 return 1;
6105 }
6c30d220
L
6106 }
6107
da4977e0
JB
6108 /* Check the special Imm4 cases; must be the first operand. */
6109 if (t->cpu_flags.bitfield.cpuxop && t->operands == 5)
6110 {
6111 if (i.op[0].imms->X_op != O_constant
6112 || !fits_in_imm4 (i.op[0].imms->X_add_number))
6113 {
6114 i.error = bad_imm4;
6115 return 1;
6116 }
6117
6118 /* Turn off Imm<N> so that update_imm won't complain. */
6119 operand_type_set (&i.types[0], 0);
6120 }
6121
43234a1e 6122 /* Check vector Disp8 operand. */
b5014f7a
JB
6123 if (t->opcode_modifier.disp8memshift
6124 && i.disp_encoding != disp_encoding_32bit)
43234a1e 6125 {
5273a3cd 6126 if (i.broadcast.type)
4a1b91ea 6127 i.memshift = t->opcode_modifier.broadcast - 1;
7091c612 6128 else if (t->opcode_modifier.disp8memshift != DISP8_SHIFT_VL)
43234a1e 6129 i.memshift = t->opcode_modifier.disp8memshift;
7091c612
JB
6130 else
6131 {
6132 const i386_operand_type *type = NULL;
6133
6134 i.memshift = 0;
6135 for (op = 0; op < i.operands; op++)
8dc0818e 6136 if (i.flags[op] & Operand_Mem)
7091c612 6137 {
4174bfff
JB
6138 if (t->opcode_modifier.evex == EVEXLIG)
6139 i.memshift = 2 + (i.suffix == QWORD_MNEM_SUFFIX);
6140 else if (t->operand_types[op].bitfield.xmmword
6141 + t->operand_types[op].bitfield.ymmword
6142 + t->operand_types[op].bitfield.zmmword <= 1)
7091c612
JB
6143 type = &t->operand_types[op];
6144 else if (!i.types[op].bitfield.unspecified)
6145 type = &i.types[op];
6146 }
3528c362 6147 else if (i.types[op].bitfield.class == RegSIMD
4174bfff 6148 && t->opcode_modifier.evex != EVEXLIG)
7091c612
JB
6149 {
6150 if (i.types[op].bitfield.zmmword)
6151 i.memshift = 6;
6152 else if (i.types[op].bitfield.ymmword && i.memshift < 5)
6153 i.memshift = 5;
6154 else if (i.types[op].bitfield.xmmword && i.memshift < 4)
6155 i.memshift = 4;
6156 }
6157
6158 if (type)
6159 {
6160 if (type->bitfield.zmmword)
6161 i.memshift = 6;
6162 else if (type->bitfield.ymmword)
6163 i.memshift = 5;
6164 else if (type->bitfield.xmmword)
6165 i.memshift = 4;
6166 }
6167
6168 /* For the check in fits_in_disp8(). */
6169 if (i.memshift == 0)
6170 i.memshift = -1;
6171 }
43234a1e
L
6172
6173 for (op = 0; op < i.operands; op++)
6174 if (operand_type_check (i.types[op], disp)
6175 && i.op[op].disps->X_op == O_constant)
6176 {
b5014f7a 6177 if (fits_in_disp8 (i.op[op].disps->X_add_number))
43234a1e 6178 {
b5014f7a
JB
6179 i.types[op].bitfield.disp8 = 1;
6180 return 0;
43234a1e 6181 }
b5014f7a 6182 i.types[op].bitfield.disp8 = 0;
43234a1e
L
6183 }
6184 }
b5014f7a
JB
6185
6186 i.memshift = 0;
43234a1e 6187
6c30d220
L
6188 return 0;
6189}
6190
da4977e0 6191/* Check if encoding requirements are met by the instruction. */
a683cc34
SP
6192
6193static int
da4977e0 6194VEX_check_encoding (const insn_template *t)
a683cc34 6195{
da4977e0
JB
6196 if (i.vec_encoding == vex_encoding_error)
6197 {
6198 i.error = unsupported;
6199 return 1;
6200 }
6201
86fa6981 6202 if (i.vec_encoding == vex_encoding_evex)
43234a1e 6203 {
86fa6981 6204 /* This instruction must be encoded with EVEX prefix. */
e771e7c9 6205 if (!is_evex_encoding (t))
86fa6981
L
6206 {
6207 i.error = unsupported;
6208 return 1;
6209 }
6210 return 0;
43234a1e
L
6211 }
6212
a683cc34 6213 if (!t->opcode_modifier.vex)
86fa6981
L
6214 {
6215 /* This instruction template doesn't have VEX prefix. */
6216 if (i.vec_encoding != vex_encoding_default)
6217 {
6218 i.error = unsupported;
6219 return 1;
6220 }
6221 return 0;
6222 }
a683cc34 6223
a683cc34
SP
6224 return 0;
6225}
6226
d3ce72d0 6227static const insn_template *
83b16ac6 6228match_template (char mnem_suffix)
29b0f896
AM
6229{
6230 /* Points to template once we've found it. */
d3ce72d0 6231 const insn_template *t;
40fb9820 6232 i386_operand_type overlap0, overlap1, overlap2, overlap3;
c0f3af97 6233 i386_operand_type overlap4;
29b0f896 6234 unsigned int found_reverse_match;
dc2be329 6235 i386_opcode_modifier suffix_check;
40fb9820 6236 i386_operand_type operand_types [MAX_OPERANDS];
539e75ad 6237 int addr_prefix_disp;
45a4bb20 6238 unsigned int j, size_match, check_register;
5614d22c 6239 enum i386_error specific_error = 0;
29b0f896 6240
c0f3af97
L
6241#if MAX_OPERANDS != 5
6242# error "MAX_OPERANDS must be 5."
f48ff2ae
L
6243#endif
6244
29b0f896 6245 found_reverse_match = 0;
539e75ad 6246 addr_prefix_disp = -1;
40fb9820 6247
dc2be329 6248 /* Prepare for mnemonic suffix check. */
40fb9820 6249 memset (&suffix_check, 0, sizeof (suffix_check));
dc2be329
L
6250 switch (mnem_suffix)
6251 {
6252 case BYTE_MNEM_SUFFIX:
6253 suffix_check.no_bsuf = 1;
6254 break;
6255 case WORD_MNEM_SUFFIX:
6256 suffix_check.no_wsuf = 1;
6257 break;
6258 case SHORT_MNEM_SUFFIX:
6259 suffix_check.no_ssuf = 1;
6260 break;
6261 case LONG_MNEM_SUFFIX:
6262 suffix_check.no_lsuf = 1;
6263 break;
6264 case QWORD_MNEM_SUFFIX:
6265 suffix_check.no_qsuf = 1;
6266 break;
6267 default:
6268 /* NB: In Intel syntax, normally we can check for memory operand
6269 size when there is no mnemonic suffix. But jmp and call have
6270 2 different encodings with Dword memory operand size, one with
6271 No_ldSuf and the other without. i.suffix is set to
6272 LONG_DOUBLE_MNEM_SUFFIX to skip the one with No_ldSuf. */
6273 if (i.suffix == LONG_DOUBLE_MNEM_SUFFIX)
6274 suffix_check.no_ldsuf = 1;
83b16ac6
JB
6275 }
6276
01559ecc
L
6277 /* Must have right number of operands. */
6278 i.error = number_of_operands_mismatch;
6279
45aa61fe 6280 for (t = current_templates->start; t < current_templates->end; t++)
29b0f896 6281 {
539e75ad 6282 addr_prefix_disp = -1;
dbbc8b7e 6283 found_reverse_match = 0;
539e75ad 6284
29b0f896
AM
6285 if (i.operands != t->operands)
6286 continue;
6287
50aecf8c 6288 /* Check processor support. */
a65babc9 6289 i.error = unsupported;
45a4bb20 6290 if (cpu_flags_match (t) != CPU_FLAGS_PERFECT_MATCH)
50aecf8c
L
6291 continue;
6292
57392598
CL
6293 /* Check Pseudo Prefix. */
6294 i.error = unsupported;
6295 if (t->opcode_modifier.pseudovexprefix
6296 && !(i.vec_encoding == vex_encoding_vex
6297 || i.vec_encoding == vex_encoding_vex3))
6298 continue;
6299
e1d4d893 6300 /* Check AT&T mnemonic. */
a65babc9 6301 i.error = unsupported_with_intel_mnemonic;
e1d4d893 6302 if (intel_mnemonic && t->opcode_modifier.attmnemonic)
1efbbeb4
L
6303 continue;
6304
4b5aaf5f 6305 /* Check AT&T/Intel syntax. */
a65babc9 6306 i.error = unsupported_syntax;
5c07affc 6307 if ((intel_syntax && t->opcode_modifier.attsyntax)
4b5aaf5f 6308 || (!intel_syntax && t->opcode_modifier.intelsyntax))
1efbbeb4
L
6309 continue;
6310
4b5aaf5f
L
6311 /* Check Intel64/AMD64 ISA. */
6312 switch (isa64)
6313 {
6314 default:
6315 /* Default: Don't accept Intel64. */
6316 if (t->opcode_modifier.isa64 == INTEL64)
6317 continue;
6318 break;
6319 case amd64:
6320 /* -mamd64: Don't accept Intel64 and Intel64 only. */
6321 if (t->opcode_modifier.isa64 >= INTEL64)
6322 continue;
6323 break;
6324 case intel64:
6325 /* -mintel64: Don't accept AMD64. */
5990e377 6326 if (t->opcode_modifier.isa64 == AMD64 && flag_code == CODE_64BIT)
4b5aaf5f
L
6327 continue;
6328 break;
6329 }
6330
dc2be329 6331 /* Check the suffix. */
a65babc9 6332 i.error = invalid_instruction_suffix;
dc2be329
L
6333 if ((t->opcode_modifier.no_bsuf && suffix_check.no_bsuf)
6334 || (t->opcode_modifier.no_wsuf && suffix_check.no_wsuf)
6335 || (t->opcode_modifier.no_lsuf && suffix_check.no_lsuf)
6336 || (t->opcode_modifier.no_ssuf && suffix_check.no_ssuf)
6337 || (t->opcode_modifier.no_qsuf && suffix_check.no_qsuf)
6338 || (t->opcode_modifier.no_ldsuf && suffix_check.no_ldsuf))
83b16ac6 6339 continue;
29b0f896 6340
3ac21baa
JB
6341 size_match = operand_size_match (t);
6342 if (!size_match)
7d5e4556 6343 continue;
539e75ad 6344
6f2f06be
JB
6345 /* This is intentionally not
6346
0cfa3eb3 6347 if (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE))
6f2f06be
JB
6348
6349 as the case of a missing * on the operand is accepted (perhaps with
6350 a warning, issued further down). */
0cfa3eb3 6351 if (i.jumpabsolute && t->opcode_modifier.jump != JUMP_ABSOLUTE)
6f2f06be
JB
6352 {
6353 i.error = operand_type_mismatch;
6354 continue;
6355 }
6356
5c07affc
L
6357 for (j = 0; j < MAX_OPERANDS; j++)
6358 operand_types[j] = t->operand_types[j];
6359
e365e234
JB
6360 /* In general, don't allow
6361 - 64-bit operands outside of 64-bit mode,
6362 - 32-bit operands on pre-386. */
4873e243 6363 j = i.imm_operands + (t->operands > i.imm_operands + 1);
e365e234
JB
6364 if (((i.suffix == QWORD_MNEM_SUFFIX
6365 && flag_code != CODE_64BIT
389d00a5
JB
6366 && !(t->opcode_modifier.opcodespace == SPACE_0F
6367 && t->base_opcode == 0xc7
5e74b495 6368 && t->opcode_modifier.opcodeprefix == PREFIX_NONE
8b65b895 6369 && t->extension_opcode == 1) /* cmpxchg8b */)
e365e234
JB
6370 || (i.suffix == LONG_MNEM_SUFFIX
6371 && !cpu_arch_flags.bitfield.cpui386))
45aa61fe 6372 && (intel_syntax
3cd7f3e3 6373 ? (t->opcode_modifier.mnemonicsize != IGNORESIZE
45aa61fe
AM
6374 && !intel_float_operand (t->name))
6375 : intel_float_operand (t->name) != 2)
4873e243
JB
6376 && (t->operands == i.imm_operands
6377 || (operand_types[i.imm_operands].bitfield.class != RegMMX
6378 && operand_types[i.imm_operands].bitfield.class != RegSIMD
6379 && operand_types[i.imm_operands].bitfield.class != RegMask)
6380 || (operand_types[j].bitfield.class != RegMMX
6381 && operand_types[j].bitfield.class != RegSIMD
6382 && operand_types[j].bitfield.class != RegMask))
63112cd6 6383 && !t->opcode_modifier.sib)
192dc9c6
JB
6384 continue;
6385
29b0f896 6386 /* Do not verify operands when there are none. */
e365e234 6387 if (!t->operands)
da4977e0
JB
6388 {
6389 if (VEX_check_encoding (t))
6390 {
6391 specific_error = i.error;
6392 continue;
6393 }
6394
6395 /* We've found a match; break out of loop. */
6396 break;
6397 }
252b5132 6398
48bcea9f
JB
6399 if (!t->opcode_modifier.jump
6400 || t->opcode_modifier.jump == JUMP_ABSOLUTE)
6401 {
6402 /* There should be only one Disp operand. */
6403 for (j = 0; j < MAX_OPERANDS; j++)
6404 if (operand_type_check (operand_types[j], disp))
539e75ad 6405 break;
48bcea9f
JB
6406 if (j < MAX_OPERANDS)
6407 {
5b7c81bd 6408 bool override = (i.prefix[ADDR_PREFIX] != 0);
48bcea9f
JB
6409
6410 addr_prefix_disp = j;
6411
6412 /* Address size prefix will turn Disp64/Disp32S/Disp32/Disp16
6413 operand into Disp32/Disp32/Disp16/Disp32 operand. */
6414 switch (flag_code)
40fb9820 6415 {
48bcea9f
JB
6416 case CODE_16BIT:
6417 override = !override;
6418 /* Fall through. */
6419 case CODE_32BIT:
6420 if (operand_types[j].bitfield.disp32
6421 && operand_types[j].bitfield.disp16)
40fb9820 6422 {
48bcea9f
JB
6423 operand_types[j].bitfield.disp16 = override;
6424 operand_types[j].bitfield.disp32 = !override;
40fb9820 6425 }
48bcea9f
JB
6426 operand_types[j].bitfield.disp32s = 0;
6427 operand_types[j].bitfield.disp64 = 0;
6428 break;
6429
6430 case CODE_64BIT:
6431 if (operand_types[j].bitfield.disp32s
6432 || operand_types[j].bitfield.disp64)
40fb9820 6433 {
48bcea9f
JB
6434 operand_types[j].bitfield.disp64 &= !override;
6435 operand_types[j].bitfield.disp32s &= !override;
6436 operand_types[j].bitfield.disp32 = override;
40fb9820 6437 }
48bcea9f
JB
6438 operand_types[j].bitfield.disp16 = 0;
6439 break;
40fb9820 6440 }
539e75ad 6441 }
48bcea9f 6442 }
539e75ad 6443
02a86693 6444 /* Force 0x8b encoding for "mov foo@GOT, %eax". */
35648716
JB
6445 if (i.reloc[0] == BFD_RELOC_386_GOT32
6446 && t->base_opcode == 0xa0
6447 && t->opcode_modifier.opcodespace == SPACE_BASE)
02a86693
L
6448 continue;
6449
56ffb741 6450 /* We check register size if needed. */
e2195274
JB
6451 if (t->opcode_modifier.checkregsize)
6452 {
6453 check_register = (1 << t->operands) - 1;
5273a3cd
JB
6454 if (i.broadcast.type)
6455 check_register &= ~(1 << i.broadcast.operand);
e2195274
JB
6456 }
6457 else
6458 check_register = 0;
6459
c6fb90c8 6460 overlap0 = operand_type_and (i.types[0], operand_types[0]);
29b0f896
AM
6461 switch (t->operands)
6462 {
6463 case 1:
40fb9820 6464 if (!operand_type_match (overlap0, i.types[0]))
29b0f896
AM
6465 continue;
6466 break;
6467 case 2:
33eaf5de 6468 /* xchg %eax, %eax is a special case. It is an alias for nop
8b38ad71
L
6469 only in 32bit mode and we can use opcode 0x90. In 64bit
6470 mode, we can't use 0x90 for xchg %eax, %eax since it should
6471 zero-extend %eax to %rax. */
6472 if (flag_code == CODE_64BIT
6473 && t->base_opcode == 0x90
35648716 6474 && t->opcode_modifier.opcodespace == SPACE_BASE
75e5731b
JB
6475 && i.types[0].bitfield.instance == Accum
6476 && i.types[0].bitfield.dword
6477 && i.types[1].bitfield.instance == Accum
6478 && i.types[1].bitfield.dword)
8b38ad71 6479 continue;
1212781b
JB
6480 /* xrelease mov %eax, <disp> is another special case. It must not
6481 match the accumulator-only encoding of mov. */
6482 if (flag_code != CODE_64BIT
6483 && i.hle_prefix
6484 && t->base_opcode == 0xa0
35648716 6485 && t->opcode_modifier.opcodespace == SPACE_BASE
75e5731b 6486 && i.types[0].bitfield.instance == Accum
8dc0818e 6487 && (i.flags[1] & Operand_Mem))
1212781b 6488 continue;
f5eb1d70
JB
6489 /* Fall through. */
6490
6491 case 3:
3ac21baa
JB
6492 if (!(size_match & MATCH_STRAIGHT))
6493 goto check_reverse;
64c49ab3
JB
6494 /* Reverse direction of operands if swapping is possible in the first
6495 place (operands need to be symmetric) and
6496 - the load form is requested, and the template is a store form,
6497 - the store form is requested, and the template is a load form,
6498 - the non-default (swapped) form is requested. */
6499 overlap1 = operand_type_and (operand_types[0], operand_types[1]);
f5eb1d70 6500 if (t->opcode_modifier.d && i.reg_operands == i.operands
64c49ab3
JB
6501 && !operand_type_all_zero (&overlap1))
6502 switch (i.dir_encoding)
6503 {
6504 case dir_encoding_load:
6505 if (operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 6506 || t->opcode_modifier.regmem)
64c49ab3
JB
6507 goto check_reverse;
6508 break;
6509
6510 case dir_encoding_store:
6511 if (!operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 6512 && !t->opcode_modifier.regmem)
64c49ab3
JB
6513 goto check_reverse;
6514 break;
6515
6516 case dir_encoding_swap:
6517 goto check_reverse;
6518
6519 case dir_encoding_default:
6520 break;
6521 }
86fa6981 6522 /* If we want store form, we skip the current load. */
64c49ab3
JB
6523 if ((i.dir_encoding == dir_encoding_store
6524 || i.dir_encoding == dir_encoding_swap)
86fa6981
L
6525 && i.mem_operands == 0
6526 && t->opcode_modifier.load)
fa99fab2 6527 continue;
1a0670f3 6528 /* Fall through. */
f48ff2ae 6529 case 4:
c0f3af97 6530 case 5:
c6fb90c8 6531 overlap1 = operand_type_and (i.types[1], operand_types[1]);
40fb9820
L
6532 if (!operand_type_match (overlap0, i.types[0])
6533 || !operand_type_match (overlap1, i.types[1])
e2195274 6534 || ((check_register & 3) == 3
dc821c5f 6535 && !operand_type_register_match (i.types[0],
40fb9820 6536 operand_types[0],
dc821c5f 6537 i.types[1],
40fb9820 6538 operand_types[1])))
29b0f896
AM
6539 {
6540 /* Check if other direction is valid ... */
38e314eb 6541 if (!t->opcode_modifier.d)
29b0f896
AM
6542 continue;
6543
dc1e8a47 6544 check_reverse:
3ac21baa
JB
6545 if (!(size_match & MATCH_REVERSE))
6546 continue;
29b0f896 6547 /* Try reversing direction of operands. */
f5eb1d70
JB
6548 overlap0 = operand_type_and (i.types[0], operand_types[i.operands - 1]);
6549 overlap1 = operand_type_and (i.types[i.operands - 1], operand_types[0]);
40fb9820 6550 if (!operand_type_match (overlap0, i.types[0])
f5eb1d70 6551 || !operand_type_match (overlap1, i.types[i.operands - 1])
45664ddb 6552 || (check_register
dc821c5f 6553 && !operand_type_register_match (i.types[0],
f5eb1d70
JB
6554 operand_types[i.operands - 1],
6555 i.types[i.operands - 1],
45664ddb 6556 operand_types[0])))
29b0f896
AM
6557 {
6558 /* Does not match either direction. */
6559 continue;
6560 }
38e314eb 6561 /* found_reverse_match holds which of D or FloatR
29b0f896 6562 we've found. */
38e314eb
JB
6563 if (!t->opcode_modifier.d)
6564 found_reverse_match = 0;
6565 else if (operand_types[0].bitfield.tbyte)
8a2ed489 6566 found_reverse_match = Opcode_FloatD;
dbbc8b7e 6567 else if (operand_types[0].bitfield.xmmword
f5eb1d70 6568 || operand_types[i.operands - 1].bitfield.xmmword
3528c362
JB
6569 || operand_types[0].bitfield.class == RegMMX
6570 || operand_types[i.operands - 1].bitfield.class == RegMMX
dbbc8b7e
JB
6571 || is_any_vex_encoding(t))
6572 found_reverse_match = (t->base_opcode & 0xee) != 0x6e
6573 ? Opcode_SIMD_FloatD : Opcode_SIMD_IntD;
8a2ed489 6574 else
38e314eb 6575 found_reverse_match = Opcode_D;
40fb9820 6576 if (t->opcode_modifier.floatr)
8a2ed489 6577 found_reverse_match |= Opcode_FloatR;
29b0f896 6578 }
f48ff2ae 6579 else
29b0f896 6580 {
f48ff2ae 6581 /* Found a forward 2 operand match here. */
d1cbb4db
L
6582 switch (t->operands)
6583 {
c0f3af97
L
6584 case 5:
6585 overlap4 = operand_type_and (i.types[4],
6586 operand_types[4]);
1a0670f3 6587 /* Fall through. */
d1cbb4db 6588 case 4:
c6fb90c8
L
6589 overlap3 = operand_type_and (i.types[3],
6590 operand_types[3]);
1a0670f3 6591 /* Fall through. */
d1cbb4db 6592 case 3:
c6fb90c8
L
6593 overlap2 = operand_type_and (i.types[2],
6594 operand_types[2]);
d1cbb4db
L
6595 break;
6596 }
29b0f896 6597
f48ff2ae
L
6598 switch (t->operands)
6599 {
c0f3af97
L
6600 case 5:
6601 if (!operand_type_match (overlap4, i.types[4])
dc821c5f 6602 || !operand_type_register_match (i.types[3],
c0f3af97 6603 operand_types[3],
c0f3af97
L
6604 i.types[4],
6605 operand_types[4]))
6606 continue;
1a0670f3 6607 /* Fall through. */
f48ff2ae 6608 case 4:
40fb9820 6609 if (!operand_type_match (overlap3, i.types[3])
e2195274
JB
6610 || ((check_register & 0xa) == 0xa
6611 && !operand_type_register_match (i.types[1],
f7768225
JB
6612 operand_types[1],
6613 i.types[3],
e2195274
JB
6614 operand_types[3]))
6615 || ((check_register & 0xc) == 0xc
6616 && !operand_type_register_match (i.types[2],
6617 operand_types[2],
6618 i.types[3],
6619 operand_types[3])))
f48ff2ae 6620 continue;
1a0670f3 6621 /* Fall through. */
f48ff2ae
L
6622 case 3:
6623 /* Here we make use of the fact that there are no
23e42951 6624 reverse match 3 operand instructions. */
40fb9820 6625 if (!operand_type_match (overlap2, i.types[2])
e2195274
JB
6626 || ((check_register & 5) == 5
6627 && !operand_type_register_match (i.types[0],
23e42951
JB
6628 operand_types[0],
6629 i.types[2],
e2195274
JB
6630 operand_types[2]))
6631 || ((check_register & 6) == 6
6632 && !operand_type_register_match (i.types[1],
6633 operand_types[1],
6634 i.types[2],
6635 operand_types[2])))
f48ff2ae
L
6636 continue;
6637 break;
6638 }
29b0f896 6639 }
f48ff2ae 6640 /* Found either forward/reverse 2, 3 or 4 operand match here:
29b0f896
AM
6641 slip through to break. */
6642 }
c0f3af97 6643
da4977e0
JB
6644 /* Check if vector operands are valid. */
6645 if (check_VecOperands (t))
6646 {
6647 specific_error = i.error;
6648 continue;
6649 }
6650
6651 /* Check if VEX/EVEX encoding requirements can be satisfied. */
6652 if (VEX_check_encoding (t))
5614d22c
JB
6653 {
6654 specific_error = i.error;
6655 continue;
6656 }
a683cc34 6657
29b0f896
AM
6658 /* We've found a match; break out of loop. */
6659 break;
6660 }
6661
6662 if (t == current_templates->end)
6663 {
6664 /* We found no match. */
a65babc9 6665 const char *err_msg;
5614d22c 6666 switch (specific_error ? specific_error : i.error)
a65babc9
L
6667 {
6668 default:
6669 abort ();
86e026a4 6670 case operand_size_mismatch:
a65babc9
L
6671 err_msg = _("operand size mismatch");
6672 break;
6673 case operand_type_mismatch:
6674 err_msg = _("operand type mismatch");
6675 break;
6676 case register_type_mismatch:
6677 err_msg = _("register type mismatch");
6678 break;
6679 case number_of_operands_mismatch:
6680 err_msg = _("number of operands mismatch");
6681 break;
6682 case invalid_instruction_suffix:
6683 err_msg = _("invalid instruction suffix");
6684 break;
6685 case bad_imm4:
4a2608e3 6686 err_msg = _("constant doesn't fit in 4 bits");
a65babc9 6687 break;
a65babc9
L
6688 case unsupported_with_intel_mnemonic:
6689 err_msg = _("unsupported with Intel mnemonic");
6690 break;
6691 case unsupported_syntax:
6692 err_msg = _("unsupported syntax");
6693 break;
6694 case unsupported:
35262a23 6695 as_bad (_("unsupported instruction `%s'"),
10efe3f6
L
6696 current_templates->start->name);
6697 return NULL;
260cd341
LC
6698 case invalid_sib_address:
6699 err_msg = _("invalid SIB address");
6700 break;
6c30d220
L
6701 case invalid_vsib_address:
6702 err_msg = _("invalid VSIB address");
6703 break;
7bab8ab5
JB
6704 case invalid_vector_register_set:
6705 err_msg = _("mask, index, and destination registers must be distinct");
6706 break;
260cd341
LC
6707 case invalid_tmm_register_set:
6708 err_msg = _("all tmm registers must be distinct");
6709 break;
6c30d220
L
6710 case unsupported_vector_index_register:
6711 err_msg = _("unsupported vector index register");
6712 break;
43234a1e
L
6713 case unsupported_broadcast:
6714 err_msg = _("unsupported broadcast");
6715 break;
43234a1e
L
6716 case broadcast_needed:
6717 err_msg = _("broadcast is needed for operand of such type");
6718 break;
6719 case unsupported_masking:
6720 err_msg = _("unsupported masking");
6721 break;
6722 case mask_not_on_destination:
6723 err_msg = _("mask not on destination operand");
6724 break;
6725 case no_default_mask:
6726 err_msg = _("default mask isn't allowed");
6727 break;
6728 case unsupported_rc_sae:
6729 err_msg = _("unsupported static rounding/sae");
6730 break;
6731 case rc_sae_operand_not_last_imm:
6732 if (intel_syntax)
6733 err_msg = _("RC/SAE operand must precede immediate operands");
6734 else
6735 err_msg = _("RC/SAE operand must follow immediate operands");
6736 break;
6737 case invalid_register_operand:
6738 err_msg = _("invalid register operand");
6739 break;
a65babc9
L
6740 }
6741 as_bad (_("%s for `%s'"), err_msg,
891edac4 6742 current_templates->start->name);
fa99fab2 6743 return NULL;
29b0f896 6744 }
252b5132 6745
29b0f896
AM
6746 if (!quiet_warnings)
6747 {
6748 if (!intel_syntax
0cfa3eb3 6749 && (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE)))
6f2f06be 6750 as_warn (_("indirect %s without `*'"), t->name);
29b0f896 6751
40fb9820 6752 if (t->opcode_modifier.isprefix
3cd7f3e3 6753 && t->opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
6754 {
6755 /* Warn them that a data or address size prefix doesn't
6756 affect assembly of the next line of code. */
6757 as_warn (_("stand-alone `%s' prefix"), t->name);
6758 }
6759 }
6760
6761 /* Copy the template we found. */
9a182d04 6762 install_template (t);
539e75ad
L
6763
6764 if (addr_prefix_disp != -1)
6765 i.tm.operand_types[addr_prefix_disp]
6766 = operand_types[addr_prefix_disp];
6767
29b0f896
AM
6768 if (found_reverse_match)
6769 {
dfd69174
JB
6770 /* If we found a reverse match we must alter the opcode direction
6771 bit and clear/flip the regmem modifier one. found_reverse_match
6772 holds bits to change (different for int & float insns). */
29b0f896
AM
6773
6774 i.tm.base_opcode ^= found_reverse_match;
6775
f5eb1d70
JB
6776 i.tm.operand_types[0] = operand_types[i.operands - 1];
6777 i.tm.operand_types[i.operands - 1] = operand_types[0];
dfd69174
JB
6778
6779 /* Certain SIMD insns have their load forms specified in the opcode
6780 table, and hence we need to _set_ RegMem instead of clearing it.
6781 We need to avoid setting the bit though on insns like KMOVW. */
6782 i.tm.opcode_modifier.regmem
6783 = i.tm.opcode_modifier.modrm && i.tm.opcode_modifier.d
6784 && i.tm.operands > 2U - i.tm.opcode_modifier.sse2avx
6785 && !i.tm.opcode_modifier.regmem;
29b0f896
AM
6786 }
6787
fa99fab2 6788 return t;
29b0f896
AM
6789}
6790
6791static int
e3bb37b5 6792check_string (void)
29b0f896 6793{
51c8edf6
JB
6794 unsigned int es_op = i.tm.opcode_modifier.isstring - IS_STRING_ES_OP0;
6795 unsigned int op = i.tm.operand_types[0].bitfield.baseindex ? es_op : 0;
8dc0818e 6796
5e042380 6797 if (i.seg[op] != NULL && i.seg[op] != reg_es)
29b0f896 6798 {
51c8edf6
JB
6799 as_bad (_("`%s' operand %u must use `%ses' segment"),
6800 i.tm.name,
6801 intel_syntax ? i.tm.operands - es_op : es_op + 1,
6802 register_prefix);
6803 return 0;
29b0f896 6804 }
51c8edf6
JB
6805
6806 /* There's only ever one segment override allowed per instruction.
6807 This instruction possibly has a legal segment override on the
6808 second operand, so copy the segment to where non-string
6809 instructions store it, allowing common code. */
6810 i.seg[op] = i.seg[1];
6811
29b0f896
AM
6812 return 1;
6813}
6814
6815static int
543613e9 6816process_suffix (void)
29b0f896 6817{
5b7c81bd 6818 bool is_crc32 = false, is_movx = false;
8b65b895 6819
29b0f896
AM
6820 /* If matched instruction specifies an explicit instruction mnemonic
6821 suffix, use it. */
673fe0f0 6822 if (i.tm.opcode_modifier.size == SIZE16)
40fb9820 6823 i.suffix = WORD_MNEM_SUFFIX;
673fe0f0 6824 else if (i.tm.opcode_modifier.size == SIZE32)
40fb9820 6825 i.suffix = LONG_MNEM_SUFFIX;
673fe0f0 6826 else if (i.tm.opcode_modifier.size == SIZE64)
40fb9820 6827 i.suffix = QWORD_MNEM_SUFFIX;
13e600d0 6828 else if (i.reg_operands
c8f8eebc
JB
6829 && (i.operands > 1 || i.types[0].bitfield.class == Reg)
6830 && !i.tm.opcode_modifier.addrprefixopreg)
29b0f896 6831 {
65fca059 6832 unsigned int numop = i.operands;
389d00a5
JB
6833
6834 /* MOVSX/MOVZX */
6835 is_movx = (i.tm.opcode_modifier.opcodespace == SPACE_0F
6836 && (i.tm.base_opcode | 8) == 0xbe)
6837 || (i.tm.opcode_modifier.opcodespace == SPACE_BASE
6838 && i.tm.base_opcode == 0x63
6839 && i.tm.cpu_flags.bitfield.cpu64);
6840
8b65b895 6841 /* CRC32 */
389d00a5
JB
6842 is_crc32 = (i.tm.base_opcode == 0xf0
6843 && i.tm.opcode_modifier.opcodespace == SPACE_0F38
8b65b895 6844 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2);
65fca059
JB
6845
6846 /* movsx/movzx want only their source operand considered here, for the
6847 ambiguity checking below. The suffix will be replaced afterwards
6848 to represent the destination (register). */
389d00a5 6849 if (is_movx && (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63))
65fca059
JB
6850 --i.operands;
6851
643bb870 6852 /* crc32 needs REX.W set regardless of suffix / source operand size. */
8b65b895 6853 if (is_crc32 && i.tm.operand_types[1].bitfield.qword)
643bb870
JB
6854 i.rex |= REX_W;
6855
29b0f896 6856 /* If there's no instruction mnemonic suffix we try to invent one
13e600d0 6857 based on GPR operands. */
29b0f896
AM
6858 if (!i.suffix)
6859 {
6860 /* We take i.suffix from the last register operand specified,
6861 Destination register type is more significant than source
381d071f
L
6862 register type. crc32 in SSE4.2 prefers source register
6863 type. */
8b65b895 6864 unsigned int op = is_crc32 ? 1 : i.operands;
20592a94 6865
1a035124
JB
6866 while (op--)
6867 if (i.tm.operand_types[op].bitfield.instance == InstanceNone
6868 || i.tm.operand_types[op].bitfield.instance == Accum)
6869 {
6870 if (i.types[op].bitfield.class != Reg)
6871 continue;
6872 if (i.types[op].bitfield.byte)
6873 i.suffix = BYTE_MNEM_SUFFIX;
6874 else if (i.types[op].bitfield.word)
6875 i.suffix = WORD_MNEM_SUFFIX;
6876 else if (i.types[op].bitfield.dword)
6877 i.suffix = LONG_MNEM_SUFFIX;
6878 else if (i.types[op].bitfield.qword)
6879 i.suffix = QWORD_MNEM_SUFFIX;
6880 else
6881 continue;
6882 break;
6883 }
65fca059
JB
6884
6885 /* As an exception, movsx/movzx silently default to a byte source
6886 in AT&T mode. */
389d00a5 6887 if (is_movx && i.tm.opcode_modifier.w && !i.suffix && !intel_syntax)
65fca059 6888 i.suffix = BYTE_MNEM_SUFFIX;
29b0f896
AM
6889 }
6890 else if (i.suffix == BYTE_MNEM_SUFFIX)
6891 {
2eb952a4 6892 if (intel_syntax
3cd7f3e3 6893 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
2eb952a4
L
6894 && i.tm.opcode_modifier.no_bsuf)
6895 i.suffix = 0;
6896 else if (!check_byte_reg ())
29b0f896
AM
6897 return 0;
6898 }
6899 else if (i.suffix == LONG_MNEM_SUFFIX)
6900 {
2eb952a4 6901 if (intel_syntax
3cd7f3e3 6902 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
9f123b91
JB
6903 && i.tm.opcode_modifier.no_lsuf
6904 && !i.tm.opcode_modifier.todword
6905 && !i.tm.opcode_modifier.toqword)
2eb952a4
L
6906 i.suffix = 0;
6907 else if (!check_long_reg ())
29b0f896
AM
6908 return 0;
6909 }
6910 else if (i.suffix == QWORD_MNEM_SUFFIX)
6911 {
955e1e6a 6912 if (intel_syntax
3cd7f3e3 6913 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
9f123b91
JB
6914 && i.tm.opcode_modifier.no_qsuf
6915 && !i.tm.opcode_modifier.todword
6916 && !i.tm.opcode_modifier.toqword)
955e1e6a
L
6917 i.suffix = 0;
6918 else if (!check_qword_reg ())
29b0f896
AM
6919 return 0;
6920 }
6921 else if (i.suffix == WORD_MNEM_SUFFIX)
6922 {
2eb952a4 6923 if (intel_syntax
3cd7f3e3 6924 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
2eb952a4
L
6925 && i.tm.opcode_modifier.no_wsuf)
6926 i.suffix = 0;
6927 else if (!check_word_reg ())
29b0f896
AM
6928 return 0;
6929 }
3cd7f3e3
L
6930 else if (intel_syntax
6931 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
6932 /* Do nothing if the instruction is going to ignore the prefix. */
6933 ;
6934 else
6935 abort ();
65fca059
JB
6936
6937 /* Undo the movsx/movzx change done above. */
6938 i.operands = numop;
29b0f896 6939 }
3cd7f3e3
L
6940 else if (i.tm.opcode_modifier.mnemonicsize == DEFAULTSIZE
6941 && !i.suffix)
29b0f896 6942 {
13e600d0
JB
6943 i.suffix = stackop_size;
6944 if (stackop_size == LONG_MNEM_SUFFIX)
06f74c5c
L
6945 {
6946 /* stackop_size is set to LONG_MNEM_SUFFIX for the
6947 .code16gcc directive to support 16-bit mode with
6948 32-bit address. For IRET without a suffix, generate
6949 16-bit IRET (opcode 0xcf) to return from an interrupt
6950 handler. */
13e600d0
JB
6951 if (i.tm.base_opcode == 0xcf)
6952 {
6953 i.suffix = WORD_MNEM_SUFFIX;
6954 as_warn (_("generating 16-bit `iret' for .code16gcc directive"));
6955 }
6956 /* Warn about changed behavior for segment register push/pop. */
6957 else if ((i.tm.base_opcode | 1) == 0x07)
6958 as_warn (_("generating 32-bit `%s', unlike earlier gas versions"),
6959 i.tm.name);
06f74c5c 6960 }
29b0f896 6961 }
c006a730 6962 else if (!i.suffix
0cfa3eb3
JB
6963 && (i.tm.opcode_modifier.jump == JUMP_ABSOLUTE
6964 || i.tm.opcode_modifier.jump == JUMP_BYTE
6965 || i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT
389d00a5
JB
6966 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
6967 && i.tm.base_opcode == 0x01 /* [ls][gi]dt */
64e74474 6968 && i.tm.extension_opcode <= 3)))
9306ca4a
JB
6969 {
6970 switch (flag_code)
6971 {
6972 case CODE_64BIT:
40fb9820 6973 if (!i.tm.opcode_modifier.no_qsuf)
9306ca4a 6974 {
828c2a25
JB
6975 if (i.tm.opcode_modifier.jump == JUMP_BYTE
6976 || i.tm.opcode_modifier.no_lsuf)
6977 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a
JB
6978 break;
6979 }
1a0670f3 6980 /* Fall through. */
9306ca4a 6981 case CODE_32BIT:
40fb9820 6982 if (!i.tm.opcode_modifier.no_lsuf)
9306ca4a
JB
6983 i.suffix = LONG_MNEM_SUFFIX;
6984 break;
6985 case CODE_16BIT:
40fb9820 6986 if (!i.tm.opcode_modifier.no_wsuf)
9306ca4a
JB
6987 i.suffix = WORD_MNEM_SUFFIX;
6988 break;
6989 }
6990 }
252b5132 6991
c006a730 6992 if (!i.suffix
3cd7f3e3 6993 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8
JB
6994 /* Also cover lret/retf/iret in 64-bit mode. */
6995 || (flag_code == CODE_64BIT
6996 && !i.tm.opcode_modifier.no_lsuf
6997 && !i.tm.opcode_modifier.no_qsuf))
3cd7f3e3 6998 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
8bbb3ad8
JB
6999 /* Explicit sizing prefixes are assumed to disambiguate insns. */
7000 && !i.prefix[DATA_PREFIX] && !(i.prefix[REX_PREFIX] & REX_W)
62b3f548
JB
7001 /* Accept FLDENV et al without suffix. */
7002 && (i.tm.opcode_modifier.no_ssuf || i.tm.opcode_modifier.floatmf))
29b0f896 7003 {
6c0946d0 7004 unsigned int suffixes, evex = 0;
c006a730
JB
7005
7006 suffixes = !i.tm.opcode_modifier.no_bsuf;
7007 if (!i.tm.opcode_modifier.no_wsuf)
7008 suffixes |= 1 << 1;
7009 if (!i.tm.opcode_modifier.no_lsuf)
7010 suffixes |= 1 << 2;
7011 if (!i.tm.opcode_modifier.no_ldsuf)
7012 suffixes |= 1 << 3;
7013 if (!i.tm.opcode_modifier.no_ssuf)
7014 suffixes |= 1 << 4;
7015 if (flag_code == CODE_64BIT && !i.tm.opcode_modifier.no_qsuf)
7016 suffixes |= 1 << 5;
7017
6c0946d0
JB
7018 /* For [XYZ]MMWORD operands inspect operand sizes. While generally
7019 also suitable for AT&T syntax mode, it was requested that this be
7020 restricted to just Intel syntax. */
5273a3cd 7021 if (intel_syntax && is_any_vex_encoding (&i.tm) && !i.broadcast.type)
6c0946d0 7022 {
b9915cbc 7023 unsigned int op;
6c0946d0 7024
b9915cbc 7025 for (op = 0; op < i.tm.operands; ++op)
6c0946d0 7026 {
b9915cbc
JB
7027 if (is_evex_encoding (&i.tm)
7028 && !cpu_arch_flags.bitfield.cpuavx512vl)
6c0946d0 7029 {
b9915cbc
JB
7030 if (i.tm.operand_types[op].bitfield.ymmword)
7031 i.tm.operand_types[op].bitfield.xmmword = 0;
7032 if (i.tm.operand_types[op].bitfield.zmmword)
7033 i.tm.operand_types[op].bitfield.ymmword = 0;
7034 if (!i.tm.opcode_modifier.evex
7035 || i.tm.opcode_modifier.evex == EVEXDYN)
7036 i.tm.opcode_modifier.evex = EVEX512;
7037 }
6c0946d0 7038
b9915cbc
JB
7039 if (i.tm.operand_types[op].bitfield.xmmword
7040 + i.tm.operand_types[op].bitfield.ymmword
7041 + i.tm.operand_types[op].bitfield.zmmword < 2)
7042 continue;
6c0946d0 7043
b9915cbc
JB
7044 /* Any properly sized operand disambiguates the insn. */
7045 if (i.types[op].bitfield.xmmword
7046 || i.types[op].bitfield.ymmword
7047 || i.types[op].bitfield.zmmword)
7048 {
7049 suffixes &= ~(7 << 6);
7050 evex = 0;
7051 break;
7052 }
6c0946d0 7053
b9915cbc
JB
7054 if ((i.flags[op] & Operand_Mem)
7055 && i.tm.operand_types[op].bitfield.unspecified)
7056 {
7057 if (i.tm.operand_types[op].bitfield.xmmword)
7058 suffixes |= 1 << 6;
7059 if (i.tm.operand_types[op].bitfield.ymmword)
7060 suffixes |= 1 << 7;
7061 if (i.tm.operand_types[op].bitfield.zmmword)
7062 suffixes |= 1 << 8;
7063 if (is_evex_encoding (&i.tm))
7064 evex = EVEX512;
6c0946d0
JB
7065 }
7066 }
7067 }
7068
7069 /* Are multiple suffixes / operand sizes allowed? */
c006a730 7070 if (suffixes & (suffixes - 1))
9306ca4a 7071 {
873494c8 7072 if (intel_syntax
3cd7f3e3 7073 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8 7074 || operand_check == check_error))
9306ca4a 7075 {
c006a730 7076 as_bad (_("ambiguous operand size for `%s'"), i.tm.name);
9306ca4a
JB
7077 return 0;
7078 }
c006a730 7079 if (operand_check == check_error)
9306ca4a 7080 {
c006a730
JB
7081 as_bad (_("no instruction mnemonic suffix given and "
7082 "no register operands; can't size `%s'"), i.tm.name);
9306ca4a
JB
7083 return 0;
7084 }
c006a730 7085 if (operand_check == check_warning)
873494c8
JB
7086 as_warn (_("%s; using default for `%s'"),
7087 intel_syntax
7088 ? _("ambiguous operand size")
7089 : _("no instruction mnemonic suffix given and "
7090 "no register operands"),
7091 i.tm.name);
c006a730
JB
7092
7093 if (i.tm.opcode_modifier.floatmf)
7094 i.suffix = SHORT_MNEM_SUFFIX;
389d00a5 7095 else if (is_movx)
65fca059 7096 /* handled below */;
6c0946d0
JB
7097 else if (evex)
7098 i.tm.opcode_modifier.evex = evex;
c006a730
JB
7099 else if (flag_code == CODE_16BIT)
7100 i.suffix = WORD_MNEM_SUFFIX;
1a035124 7101 else if (!i.tm.opcode_modifier.no_lsuf)
c006a730 7102 i.suffix = LONG_MNEM_SUFFIX;
1a035124
JB
7103 else
7104 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a 7105 }
29b0f896 7106 }
252b5132 7107
389d00a5 7108 if (is_movx)
65fca059
JB
7109 {
7110 /* In Intel syntax, movsx/movzx must have a "suffix" (checked above).
7111 In AT&T syntax, if there is no suffix (warned about above), the default
7112 will be byte extension. */
7113 if (i.tm.opcode_modifier.w && i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
7114 i.tm.base_opcode |= 1;
7115
7116 /* For further processing, the suffix should represent the destination
7117 (register). This is already the case when one was used with
7118 mov[sz][bw]*, but we need to replace it for mov[sz]x, or if there was
7119 no suffix to begin with. */
7120 if (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63 || !i.suffix)
7121 {
7122 if (i.types[1].bitfield.word)
7123 i.suffix = WORD_MNEM_SUFFIX;
7124 else if (i.types[1].bitfield.qword)
7125 i.suffix = QWORD_MNEM_SUFFIX;
7126 else
7127 i.suffix = LONG_MNEM_SUFFIX;
7128
7129 i.tm.opcode_modifier.w = 0;
7130 }
7131 }
7132
50128d0c
JB
7133 if (!i.tm.opcode_modifier.modrm && i.reg_operands && i.tm.operands < 3)
7134 i.short_form = (i.tm.operand_types[0].bitfield.class == Reg)
7135 != (i.tm.operand_types[1].bitfield.class == Reg);
7136
d2224064
JB
7137 /* Change the opcode based on the operand size given by i.suffix. */
7138 switch (i.suffix)
29b0f896 7139 {
d2224064
JB
7140 /* Size floating point instruction. */
7141 case LONG_MNEM_SUFFIX:
7142 if (i.tm.opcode_modifier.floatmf)
7143 {
7144 i.tm.base_opcode ^= 4;
7145 break;
7146 }
7147 /* fall through */
7148 case WORD_MNEM_SUFFIX:
7149 case QWORD_MNEM_SUFFIX:
29b0f896 7150 /* It's not a byte, select word/dword operation. */
40fb9820 7151 if (i.tm.opcode_modifier.w)
29b0f896 7152 {
50128d0c 7153 if (i.short_form)
29b0f896
AM
7154 i.tm.base_opcode |= 8;
7155 else
7156 i.tm.base_opcode |= 1;
7157 }
d2224064
JB
7158 /* fall through */
7159 case SHORT_MNEM_SUFFIX:
29b0f896
AM
7160 /* Now select between word & dword operations via the operand
7161 size prefix, except for instructions that will ignore this
7162 prefix anyway. */
c8f8eebc 7163 if (i.suffix != QWORD_MNEM_SUFFIX
3cd7f3e3 7164 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
c8f8eebc
JB
7165 && !i.tm.opcode_modifier.floatmf
7166 && !is_any_vex_encoding (&i.tm)
7167 && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
7168 || (flag_code == CODE_64BIT
7169 && i.tm.opcode_modifier.jump == JUMP_BYTE)))
24eab124
AM
7170 {
7171 unsigned int prefix = DATA_PREFIX_OPCODE;
543613e9 7172
0cfa3eb3 7173 if (i.tm.opcode_modifier.jump == JUMP_BYTE) /* jcxz, loop */
29b0f896 7174 prefix = ADDR_PREFIX_OPCODE;
252b5132 7175
29b0f896
AM
7176 if (!add_prefix (prefix))
7177 return 0;
24eab124 7178 }
252b5132 7179
29b0f896
AM
7180 /* Set mode64 for an operand. */
7181 if (i.suffix == QWORD_MNEM_SUFFIX
9146926a 7182 && flag_code == CODE_64BIT
d2224064 7183 && !i.tm.opcode_modifier.norex64
4ed21b58 7184 && !i.tm.opcode_modifier.vexw
46e883c5 7185 /* Special case for xchg %rax,%rax. It is NOP and doesn't
d2224064
JB
7186 need rex64. */
7187 && ! (i.operands == 2
7188 && i.tm.base_opcode == 0x90
7189 && i.tm.extension_opcode == None
75e5731b
JB
7190 && i.types[0].bitfield.instance == Accum
7191 && i.types[0].bitfield.qword
7192 && i.types[1].bitfield.instance == Accum
7193 && i.types[1].bitfield.qword))
d2224064 7194 i.rex |= REX_W;
3e73aa7c 7195
d2224064 7196 break;
8bbb3ad8
JB
7197
7198 case 0:
f9a6a8f0 7199 /* Select word/dword/qword operation with explicit data sizing prefix
8bbb3ad8
JB
7200 when there are no suitable register operands. */
7201 if (i.tm.opcode_modifier.w
7202 && (i.prefix[DATA_PREFIX] || (i.prefix[REX_PREFIX] & REX_W))
7203 && (!i.reg_operands
7204 || (i.reg_operands == 1
7205 /* ShiftCount */
7206 && (i.tm.operand_types[0].bitfield.instance == RegC
7207 /* InOutPortReg */
7208 || i.tm.operand_types[0].bitfield.instance == RegD
7209 || i.tm.operand_types[1].bitfield.instance == RegD
7210 /* CRC32 */
8b65b895 7211 || is_crc32))))
8bbb3ad8
JB
7212 i.tm.base_opcode |= 1;
7213 break;
29b0f896 7214 }
7ecd2f8b 7215
c8f8eebc 7216 if (i.tm.opcode_modifier.addrprefixopreg)
c0a30a9f 7217 {
c8f8eebc
JB
7218 gas_assert (!i.suffix);
7219 gas_assert (i.reg_operands);
c0a30a9f 7220
c8f8eebc
JB
7221 if (i.tm.operand_types[0].bitfield.instance == Accum
7222 || i.operands == 1)
7223 {
7224 /* The address size override prefix changes the size of the
7225 first operand. */
7226 if (flag_code == CODE_64BIT
7227 && i.op[0].regs->reg_type.bitfield.word)
7228 {
7229 as_bad (_("16-bit addressing unavailable for `%s'"),
7230 i.tm.name);
7231 return 0;
7232 }
7233
7234 if ((flag_code == CODE_32BIT
7235 ? i.op[0].regs->reg_type.bitfield.word
7236 : i.op[0].regs->reg_type.bitfield.dword)
7237 && !add_prefix (ADDR_PREFIX_OPCODE))
7238 return 0;
7239 }
c0a30a9f
L
7240 else
7241 {
c8f8eebc
JB
7242 /* Check invalid register operand when the address size override
7243 prefix changes the size of register operands. */
7244 unsigned int op;
7245 enum { need_word, need_dword, need_qword } need;
7246
27f13469 7247 /* Check the register operand for the address size prefix if
b3a3496f 7248 the memory operand has no real registers, like symbol, DISP
829f3fe1 7249 or bogus (x32-only) symbol(%rip) when symbol(%eip) is meant. */
27f13469
L
7250 if (i.mem_operands == 1
7251 && i.reg_operands == 1
7252 && i.operands == 2
27f13469 7253 && i.types[1].bitfield.class == Reg
b3a3496f
L
7254 && (flag_code == CODE_32BIT
7255 ? i.op[1].regs->reg_type.bitfield.word
7256 : i.op[1].regs->reg_type.bitfield.dword)
7257 && ((i.base_reg == NULL && i.index_reg == NULL)
829f3fe1
JB
7258#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
7259 || (x86_elf_abi == X86_64_X32_ABI
7260 && i.base_reg
b3a3496f
L
7261 && i.base_reg->reg_num == RegIP
7262 && i.base_reg->reg_type.bitfield.qword))
829f3fe1
JB
7263#else
7264 || 0)
7265#endif
27f13469
L
7266 && !add_prefix (ADDR_PREFIX_OPCODE))
7267 return 0;
7268
c8f8eebc
JB
7269 if (flag_code == CODE_32BIT)
7270 need = i.prefix[ADDR_PREFIX] ? need_word : need_dword;
7271 else if (i.prefix[ADDR_PREFIX])
c0a30a9f
L
7272 need = need_dword;
7273 else
7274 need = flag_code == CODE_64BIT ? need_qword : need_word;
c0a30a9f 7275
c8f8eebc
JB
7276 for (op = 0; op < i.operands; op++)
7277 {
7278 if (i.types[op].bitfield.class != Reg)
7279 continue;
7280
7281 switch (need)
7282 {
7283 case need_word:
7284 if (i.op[op].regs->reg_type.bitfield.word)
7285 continue;
7286 break;
7287 case need_dword:
7288 if (i.op[op].regs->reg_type.bitfield.dword)
7289 continue;
7290 break;
7291 case need_qword:
7292 if (i.op[op].regs->reg_type.bitfield.qword)
7293 continue;
7294 break;
7295 }
7296
7297 as_bad (_("invalid register operand size for `%s'"),
7298 i.tm.name);
7299 return 0;
7300 }
7301 }
c0a30a9f
L
7302 }
7303
29b0f896
AM
7304 return 1;
7305}
3e73aa7c 7306
29b0f896 7307static int
543613e9 7308check_byte_reg (void)
29b0f896
AM
7309{
7310 int op;
543613e9 7311
29b0f896
AM
7312 for (op = i.operands; --op >= 0;)
7313 {
dc821c5f 7314 /* Skip non-register operands. */
bab6aec1 7315 if (i.types[op].bitfield.class != Reg)
dc821c5f
JB
7316 continue;
7317
29b0f896
AM
7318 /* If this is an eight bit register, it's OK. If it's the 16 or
7319 32 bit version of an eight bit register, we will just use the
7320 low portion, and that's OK too. */
dc821c5f 7321 if (i.types[op].bitfield.byte)
29b0f896
AM
7322 continue;
7323
5a819eb9 7324 /* I/O port address operands are OK too. */
75e5731b
JB
7325 if (i.tm.operand_types[op].bitfield.instance == RegD
7326 && i.tm.operand_types[op].bitfield.word)
5a819eb9
JB
7327 continue;
7328
9706160a 7329 /* crc32 only wants its source operand checked here. */
389d00a5
JB
7330 if (i.tm.base_opcode == 0xf0
7331 && i.tm.opcode_modifier.opcodespace == SPACE_0F38
8b65b895
L
7332 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2
7333 && op != 0)
9344ff29
L
7334 continue;
7335
29b0f896 7336 /* Any other register is bad. */
73c76375
JB
7337 as_bad (_("`%s%s' not allowed with `%s%c'"),
7338 register_prefix, i.op[op].regs->reg_name,
7339 i.tm.name, i.suffix);
7340 return 0;
29b0f896
AM
7341 }
7342 return 1;
7343}
7344
7345static int
e3bb37b5 7346check_long_reg (void)
29b0f896
AM
7347{
7348 int op;
7349
7350 for (op = i.operands; --op >= 0;)
dc821c5f 7351 /* Skip non-register operands. */
bab6aec1 7352 if (i.types[op].bitfield.class != Reg)
dc821c5f 7353 continue;
29b0f896
AM
7354 /* Reject eight bit registers, except where the template requires
7355 them. (eg. movzb) */
dc821c5f 7356 else if (i.types[op].bitfield.byte
bab6aec1 7357 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7358 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7359 && (i.tm.operand_types[op].bitfield.word
7360 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7361 {
a540244d
L
7362 as_bad (_("`%s%s' not allowed with `%s%c'"),
7363 register_prefix,
29b0f896
AM
7364 i.op[op].regs->reg_name,
7365 i.tm.name,
7366 i.suffix);
7367 return 0;
7368 }
be4c5e58
L
7369 /* Error if the e prefix on a general reg is missing. */
7370 else if (i.types[op].bitfield.word
bab6aec1 7371 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7372 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7373 && i.tm.operand_types[op].bitfield.dword)
29b0f896 7374 {
be4c5e58
L
7375 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7376 register_prefix, i.op[op].regs->reg_name,
7377 i.suffix);
7378 return 0;
252b5132 7379 }
e4630f71 7380 /* Warn if the r prefix on a general reg is present. */
dc821c5f 7381 else if (i.types[op].bitfield.qword
bab6aec1 7382 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7383 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7384 && i.tm.operand_types[op].bitfield.dword)
252b5132 7385 {
34828aad 7386 if (intel_syntax
65fca059 7387 && i.tm.opcode_modifier.toqword
3528c362 7388 && i.types[0].bitfield.class != RegSIMD)
34828aad 7389 {
ca61edf2 7390 /* Convert to QWORD. We want REX byte. */
34828aad
L
7391 i.suffix = QWORD_MNEM_SUFFIX;
7392 }
7393 else
7394 {
2b5d6a91 7395 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
34828aad
L
7396 register_prefix, i.op[op].regs->reg_name,
7397 i.suffix);
7398 return 0;
7399 }
29b0f896
AM
7400 }
7401 return 1;
7402}
252b5132 7403
29b0f896 7404static int
e3bb37b5 7405check_qword_reg (void)
29b0f896
AM
7406{
7407 int op;
252b5132 7408
29b0f896 7409 for (op = i.operands; --op >= 0; )
dc821c5f 7410 /* Skip non-register operands. */
bab6aec1 7411 if (i.types[op].bitfield.class != Reg)
dc821c5f 7412 continue;
29b0f896
AM
7413 /* Reject eight bit registers, except where the template requires
7414 them. (eg. movzb) */
dc821c5f 7415 else if (i.types[op].bitfield.byte
bab6aec1 7416 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7417 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7418 && (i.tm.operand_types[op].bitfield.word
7419 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7420 {
a540244d
L
7421 as_bad (_("`%s%s' not allowed with `%s%c'"),
7422 register_prefix,
29b0f896
AM
7423 i.op[op].regs->reg_name,
7424 i.tm.name,
7425 i.suffix);
7426 return 0;
7427 }
e4630f71 7428 /* Warn if the r prefix on a general reg is missing. */
dc821c5f
JB
7429 else if ((i.types[op].bitfield.word
7430 || i.types[op].bitfield.dword)
bab6aec1 7431 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7432 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7433 && i.tm.operand_types[op].bitfield.qword)
29b0f896
AM
7434 {
7435 /* Prohibit these changes in the 64bit mode, since the
7436 lowering is more complicated. */
34828aad 7437 if (intel_syntax
ca61edf2 7438 && i.tm.opcode_modifier.todword
3528c362 7439 && i.types[0].bitfield.class != RegSIMD)
34828aad 7440 {
ca61edf2 7441 /* Convert to DWORD. We don't want REX byte. */
34828aad
L
7442 i.suffix = LONG_MNEM_SUFFIX;
7443 }
7444 else
7445 {
2b5d6a91 7446 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
34828aad
L
7447 register_prefix, i.op[op].regs->reg_name,
7448 i.suffix);
7449 return 0;
7450 }
252b5132 7451 }
29b0f896
AM
7452 return 1;
7453}
252b5132 7454
29b0f896 7455static int
e3bb37b5 7456check_word_reg (void)
29b0f896
AM
7457{
7458 int op;
7459 for (op = i.operands; --op >= 0;)
dc821c5f 7460 /* Skip non-register operands. */
bab6aec1 7461 if (i.types[op].bitfield.class != Reg)
dc821c5f 7462 continue;
29b0f896
AM
7463 /* Reject eight bit registers, except where the template requires
7464 them. (eg. movzb) */
dc821c5f 7465 else if (i.types[op].bitfield.byte
bab6aec1 7466 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7467 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7468 && (i.tm.operand_types[op].bitfield.word
7469 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7470 {
a540244d
L
7471 as_bad (_("`%s%s' not allowed with `%s%c'"),
7472 register_prefix,
29b0f896
AM
7473 i.op[op].regs->reg_name,
7474 i.tm.name,
7475 i.suffix);
7476 return 0;
7477 }
9706160a
JB
7478 /* Error if the e or r prefix on a general reg is present. */
7479 else if ((i.types[op].bitfield.dword
dc821c5f 7480 || i.types[op].bitfield.qword)
bab6aec1 7481 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7482 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7483 && i.tm.operand_types[op].bitfield.word)
252b5132 7484 {
9706160a
JB
7485 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7486 register_prefix, i.op[op].regs->reg_name,
7487 i.suffix);
7488 return 0;
29b0f896
AM
7489 }
7490 return 1;
7491}
252b5132 7492
29b0f896 7493static int
40fb9820 7494update_imm (unsigned int j)
29b0f896 7495{
bc0844ae 7496 i386_operand_type overlap = i.types[j];
40fb9820
L
7497 if ((overlap.bitfield.imm8
7498 || overlap.bitfield.imm8s
7499 || overlap.bitfield.imm16
7500 || overlap.bitfield.imm32
7501 || overlap.bitfield.imm32s
7502 || overlap.bitfield.imm64)
0dfbf9d7
L
7503 && !operand_type_equal (&overlap, &imm8)
7504 && !operand_type_equal (&overlap, &imm8s)
7505 && !operand_type_equal (&overlap, &imm16)
7506 && !operand_type_equal (&overlap, &imm32)
7507 && !operand_type_equal (&overlap, &imm32s)
7508 && !operand_type_equal (&overlap, &imm64))
29b0f896
AM
7509 {
7510 if (i.suffix)
7511 {
40fb9820
L
7512 i386_operand_type temp;
7513
0dfbf9d7 7514 operand_type_set (&temp, 0);
7ab9ffdd 7515 if (i.suffix == BYTE_MNEM_SUFFIX)
40fb9820
L
7516 {
7517 temp.bitfield.imm8 = overlap.bitfield.imm8;
7518 temp.bitfield.imm8s = overlap.bitfield.imm8s;
7519 }
7520 else if (i.suffix == WORD_MNEM_SUFFIX)
7521 temp.bitfield.imm16 = overlap.bitfield.imm16;
7522 else if (i.suffix == QWORD_MNEM_SUFFIX)
7523 {
7524 temp.bitfield.imm64 = overlap.bitfield.imm64;
7525 temp.bitfield.imm32s = overlap.bitfield.imm32s;
7526 }
7527 else
7528 temp.bitfield.imm32 = overlap.bitfield.imm32;
7529 overlap = temp;
29b0f896 7530 }
0dfbf9d7
L
7531 else if (operand_type_equal (&overlap, &imm16_32_32s)
7532 || operand_type_equal (&overlap, &imm16_32)
7533 || operand_type_equal (&overlap, &imm16_32s))
29b0f896 7534 {
40fb9820 7535 if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
65da13b5 7536 overlap = imm16;
40fb9820 7537 else
65da13b5 7538 overlap = imm32s;
29b0f896 7539 }
8bbb3ad8
JB
7540 else if (i.prefix[REX_PREFIX] & REX_W)
7541 overlap = operand_type_and (overlap, imm32s);
7542 else if (i.prefix[DATA_PREFIX])
7543 overlap = operand_type_and (overlap,
7544 flag_code != CODE_16BIT ? imm16 : imm32);
0dfbf9d7
L
7545 if (!operand_type_equal (&overlap, &imm8)
7546 && !operand_type_equal (&overlap, &imm8s)
7547 && !operand_type_equal (&overlap, &imm16)
7548 && !operand_type_equal (&overlap, &imm32)
7549 && !operand_type_equal (&overlap, &imm32s)
7550 && !operand_type_equal (&overlap, &imm64))
29b0f896 7551 {
4eed87de
AM
7552 as_bad (_("no instruction mnemonic suffix given; "
7553 "can't determine immediate size"));
29b0f896
AM
7554 return 0;
7555 }
7556 }
40fb9820 7557 i.types[j] = overlap;
29b0f896 7558
40fb9820
L
7559 return 1;
7560}
7561
7562static int
7563finalize_imm (void)
7564{
bc0844ae 7565 unsigned int j, n;
29b0f896 7566
bc0844ae
L
7567 /* Update the first 2 immediate operands. */
7568 n = i.operands > 2 ? 2 : i.operands;
7569 if (n)
7570 {
7571 for (j = 0; j < n; j++)
7572 if (update_imm (j) == 0)
7573 return 0;
40fb9820 7574
bc0844ae
L
7575 /* The 3rd operand can't be immediate operand. */
7576 gas_assert (operand_type_check (i.types[2], imm) == 0);
7577 }
29b0f896
AM
7578
7579 return 1;
7580}
7581
7582static int
e3bb37b5 7583process_operands (void)
29b0f896
AM
7584{
7585 /* Default segment register this instruction will use for memory
7586 accesses. 0 means unknown. This is only for optimizing out
7587 unnecessary segment overrides. */
5e042380 7588 const reg_entry *default_seg = NULL;
29b0f896 7589
a5aeccd9
JB
7590 if (i.tm.opcode_modifier.sse2avx)
7591 {
7592 /* Legacy encoded insns allow explicit REX prefixes, so these prefixes
7593 need converting. */
7594 i.rex |= i.prefix[REX_PREFIX] & (REX_W | REX_R | REX_X | REX_B);
7595 i.prefix[REX_PREFIX] = 0;
7596 i.rex_encoding = 0;
7597 }
c423d21a
JB
7598 /* ImmExt should be processed after SSE2AVX. */
7599 else if (i.tm.opcode_modifier.immext)
7600 process_immext ();
a5aeccd9 7601
2426c15f 7602 if (i.tm.opcode_modifier.sse2avx && i.tm.opcode_modifier.vexvvvv)
29b0f896 7603 {
91d6fa6a
NC
7604 unsigned int dupl = i.operands;
7605 unsigned int dest = dupl - 1;
9fcfb3d7
L
7606 unsigned int j;
7607
c0f3af97 7608 /* The destination must be an xmm register. */
9c2799c2 7609 gas_assert (i.reg_operands
91d6fa6a 7610 && MAX_OPERANDS > dupl
7ab9ffdd 7611 && operand_type_equal (&i.types[dest], &regxmm));
c0f3af97 7612
75e5731b 7613 if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 7614 && i.tm.operand_types[0].bitfield.xmmword)
e2ec9d29 7615 {
8cd7925b 7616 if (i.tm.opcode_modifier.vexsources == VEX3SOURCES)
c0f3af97
L
7617 {
7618 /* Keep xmm0 for instructions with VEX prefix and 3
7619 sources. */
75e5731b 7620 i.tm.operand_types[0].bitfield.instance = InstanceNone;
3528c362 7621 i.tm.operand_types[0].bitfield.class = RegSIMD;
c0f3af97
L
7622 goto duplicate;
7623 }
e2ec9d29 7624 else
c0f3af97
L
7625 {
7626 /* We remove the first xmm0 and keep the number of
7627 operands unchanged, which in fact duplicates the
7628 destination. */
7629 for (j = 1; j < i.operands; j++)
7630 {
7631 i.op[j - 1] = i.op[j];
7632 i.types[j - 1] = i.types[j];
7633 i.tm.operand_types[j - 1] = i.tm.operand_types[j];
8dc0818e 7634 i.flags[j - 1] = i.flags[j];
c0f3af97
L
7635 }
7636 }
7637 }
7638 else if (i.tm.opcode_modifier.implicit1stxmm0)
7ab9ffdd 7639 {
91d6fa6a 7640 gas_assert ((MAX_OPERANDS - 1) > dupl
8cd7925b
L
7641 && (i.tm.opcode_modifier.vexsources
7642 == VEX3SOURCES));
c0f3af97
L
7643
7644 /* Add the implicit xmm0 for instructions with VEX prefix
7645 and 3 sources. */
7646 for (j = i.operands; j > 0; j--)
7647 {
7648 i.op[j] = i.op[j - 1];
7649 i.types[j] = i.types[j - 1];
7650 i.tm.operand_types[j] = i.tm.operand_types[j - 1];
8dc0818e 7651 i.flags[j] = i.flags[j - 1];
c0f3af97
L
7652 }
7653 i.op[0].regs
629310ab 7654 = (const reg_entry *) str_hash_find (reg_hash, "xmm0");
7ab9ffdd 7655 i.types[0] = regxmm;
c0f3af97
L
7656 i.tm.operand_types[0] = regxmm;
7657
7658 i.operands += 2;
7659 i.reg_operands += 2;
7660 i.tm.operands += 2;
7661
91d6fa6a 7662 dupl++;
c0f3af97 7663 dest++;
91d6fa6a
NC
7664 i.op[dupl] = i.op[dest];
7665 i.types[dupl] = i.types[dest];
7666 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 7667 i.flags[dupl] = i.flags[dest];
e2ec9d29 7668 }
c0f3af97
L
7669 else
7670 {
dc1e8a47 7671 duplicate:
c0f3af97
L
7672 i.operands++;
7673 i.reg_operands++;
7674 i.tm.operands++;
7675
91d6fa6a
NC
7676 i.op[dupl] = i.op[dest];
7677 i.types[dupl] = i.types[dest];
7678 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 7679 i.flags[dupl] = i.flags[dest];
c0f3af97
L
7680 }
7681
7682 if (i.tm.opcode_modifier.immext)
7683 process_immext ();
7684 }
75e5731b 7685 else if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 7686 && i.tm.operand_types[0].bitfield.xmmword)
c0f3af97
L
7687 {
7688 unsigned int j;
7689
9fcfb3d7
L
7690 for (j = 1; j < i.operands; j++)
7691 {
7692 i.op[j - 1] = i.op[j];
7693 i.types[j - 1] = i.types[j];
7694
7695 /* We need to adjust fields in i.tm since they are used by
7696 build_modrm_byte. */
7697 i.tm.operand_types [j - 1] = i.tm.operand_types [j];
8dc0818e
JB
7698
7699 i.flags[j - 1] = i.flags[j];
9fcfb3d7
L
7700 }
7701
e2ec9d29
L
7702 i.operands--;
7703 i.reg_operands--;
e2ec9d29
L
7704 i.tm.operands--;
7705 }
920d2ddc
IT
7706 else if (i.tm.opcode_modifier.implicitquadgroup)
7707 {
a477a8c4
JB
7708 unsigned int regnum, first_reg_in_group, last_reg_in_group;
7709
920d2ddc 7710 /* The second operand must be {x,y,z}mmN, where N is a multiple of 4. */
3528c362 7711 gas_assert (i.operands >= 2 && i.types[1].bitfield.class == RegSIMD);
a477a8c4
JB
7712 regnum = register_number (i.op[1].regs);
7713 first_reg_in_group = regnum & ~3;
7714 last_reg_in_group = first_reg_in_group + 3;
7715 if (regnum != first_reg_in_group)
7716 as_warn (_("source register `%s%s' implicitly denotes"
7717 " `%s%.3s%u' to `%s%.3s%u' source group in `%s'"),
7718 register_prefix, i.op[1].regs->reg_name,
7719 register_prefix, i.op[1].regs->reg_name, first_reg_in_group,
7720 register_prefix, i.op[1].regs->reg_name, last_reg_in_group,
7721 i.tm.name);
7722 }
e2ec9d29
L
7723 else if (i.tm.opcode_modifier.regkludge)
7724 {
7725 /* The imul $imm, %reg instruction is converted into
7726 imul $imm, %reg, %reg, and the clr %reg instruction
7727 is converted into xor %reg, %reg. */
7728
7729 unsigned int first_reg_op;
7730
7731 if (operand_type_check (i.types[0], reg))
7732 first_reg_op = 0;
7733 else
7734 first_reg_op = 1;
7735 /* Pretend we saw the extra register operand. */
9c2799c2 7736 gas_assert (i.reg_operands == 1
7ab9ffdd 7737 && i.op[first_reg_op + 1].regs == 0);
e2ec9d29
L
7738 i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
7739 i.types[first_reg_op + 1] = i.types[first_reg_op];
7740 i.operands++;
7741 i.reg_operands++;
29b0f896
AM
7742 }
7743
85b80b0f 7744 if (i.tm.opcode_modifier.modrm)
29b0f896
AM
7745 {
7746 /* The opcode is completed (modulo i.tm.extension_opcode which
52271982
AM
7747 must be put into the modrm byte). Now, we make the modrm and
7748 index base bytes based on all the info we've collected. */
29b0f896
AM
7749
7750 default_seg = build_modrm_byte ();
7751 }
00cee14f 7752 else if (i.types[0].bitfield.class == SReg)
85b80b0f
JB
7753 {
7754 if (flag_code != CODE_64BIT
7755 ? i.tm.base_opcode == POP_SEG_SHORT
7756 && i.op[0].regs->reg_num == 1
389d00a5 7757 : (i.tm.base_opcode | 1) == (POP_SEG386_SHORT & 0xff)
85b80b0f
JB
7758 && i.op[0].regs->reg_num < 4)
7759 {
7760 as_bad (_("you can't `%s %s%s'"),
7761 i.tm.name, register_prefix, i.op[0].regs->reg_name);
7762 return 0;
7763 }
389d00a5
JB
7764 if (i.op[0].regs->reg_num > 3
7765 && i.tm.opcode_modifier.opcodespace == SPACE_BASE )
85b80b0f 7766 {
389d00a5
JB
7767 i.tm.base_opcode ^= (POP_SEG_SHORT ^ POP_SEG386_SHORT) & 0xff;
7768 i.tm.opcode_modifier.opcodespace = SPACE_0F;
85b80b0f
JB
7769 }
7770 i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
7771 }
389d00a5
JB
7772 else if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
7773 && (i.tm.base_opcode & ~3) == MOV_AX_DISP32)
29b0f896 7774 {
5e042380 7775 default_seg = reg_ds;
29b0f896 7776 }
40fb9820 7777 else if (i.tm.opcode_modifier.isstring)
29b0f896
AM
7778 {
7779 /* For the string instructions that allow a segment override
7780 on one of their operands, the default segment is ds. */
5e042380 7781 default_seg = reg_ds;
29b0f896 7782 }
50128d0c 7783 else if (i.short_form)
85b80b0f
JB
7784 {
7785 /* The register or float register operand is in operand
7786 0 or 1. */
bab6aec1 7787 unsigned int op = i.tm.operand_types[0].bitfield.class != Reg;
85b80b0f
JB
7788
7789 /* Register goes in low 3 bits of opcode. */
7790 i.tm.base_opcode |= i.op[op].regs->reg_num;
7791 if ((i.op[op].regs->reg_flags & RegRex) != 0)
7792 i.rex |= REX_B;
7793 if (!quiet_warnings && i.tm.opcode_modifier.ugh)
7794 {
7795 /* Warn about some common errors, but press on regardless.
7796 The first case can be generated by gcc (<= 2.8.1). */
7797 if (i.operands == 2)
7798 {
7799 /* Reversed arguments on faddp, fsubp, etc. */
7800 as_warn (_("translating to `%s %s%s,%s%s'"), i.tm.name,
7801 register_prefix, i.op[!intel_syntax].regs->reg_name,
7802 register_prefix, i.op[intel_syntax].regs->reg_name);
7803 }
7804 else
7805 {
7806 /* Extraneous `l' suffix on fp insn. */
7807 as_warn (_("translating to `%s %s%s'"), i.tm.name,
7808 register_prefix, i.op[0].regs->reg_name);
7809 }
7810 }
7811 }
29b0f896 7812
514a8bb0 7813 if ((i.seg[0] || i.prefix[SEG_PREFIX])
514a8bb0 7814 && i.tm.base_opcode == 0x8d /* lea */
35648716 7815 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
514a8bb0 7816 && !is_any_vex_encoding(&i.tm))
92334ad2
JB
7817 {
7818 if (!quiet_warnings)
7819 as_warn (_("segment override on `%s' is ineffectual"), i.tm.name);
7820 if (optimize)
7821 {
7822 i.seg[0] = NULL;
7823 i.prefix[SEG_PREFIX] = 0;
7824 }
7825 }
52271982
AM
7826
7827 /* If a segment was explicitly specified, and the specified segment
b6773884
JB
7828 is neither the default nor the one already recorded from a prefix,
7829 use an opcode prefix to select it. If we never figured out what
7830 the default segment is, then default_seg will be zero at this
7831 point, and the specified segment prefix will always be used. */
7832 if (i.seg[0]
7833 && i.seg[0] != default_seg
5e042380 7834 && i386_seg_prefixes[i.seg[0]->reg_num] != i.prefix[SEG_PREFIX])
29b0f896 7835 {
5e042380 7836 if (!add_prefix (i386_seg_prefixes[i.seg[0]->reg_num]))
29b0f896
AM
7837 return 0;
7838 }
7839 return 1;
7840}
7841
a5aeccd9 7842static INLINE void set_rex_vrex (const reg_entry *r, unsigned int rex_bit,
5b7c81bd 7843 bool do_sse2avx)
a5aeccd9
JB
7844{
7845 if (r->reg_flags & RegRex)
7846 {
7847 if (i.rex & rex_bit)
7848 as_bad (_("same type of prefix used twice"));
7849 i.rex |= rex_bit;
7850 }
7851 else if (do_sse2avx && (i.rex & rex_bit) && i.vex.register_specifier)
7852 {
7853 gas_assert (i.vex.register_specifier == r);
7854 i.vex.register_specifier += 8;
7855 }
7856
7857 if (r->reg_flags & RegVRex)
7858 i.vrex |= rex_bit;
7859}
7860
5e042380 7861static const reg_entry *
e3bb37b5 7862build_modrm_byte (void)
29b0f896 7863{
5e042380 7864 const reg_entry *default_seg = NULL;
c0f3af97 7865 unsigned int source, dest;
8cd7925b 7866 int vex_3_sources;
c0f3af97 7867
8cd7925b 7868 vex_3_sources = i.tm.opcode_modifier.vexsources == VEX3SOURCES;
c0f3af97
L
7869 if (vex_3_sources)
7870 {
91d6fa6a 7871 unsigned int nds, reg_slot;
4c2c6516 7872 expressionS *exp;
c0f3af97 7873
6b8d3588 7874 dest = i.operands - 1;
c0f3af97 7875 nds = dest - 1;
922d8de8 7876
a683cc34 7877 /* There are 2 kinds of instructions:
bed3d976 7878 1. 5 operands: 4 register operands or 3 register operands
9d3bf266 7879 plus 1 memory operand plus one Imm4 operand, VexXDS, and
bed3d976 7880 VexW0 or VexW1. The destination must be either XMM, YMM or
43234a1e 7881 ZMM register.
bed3d976 7882 2. 4 operands: 4 register operands or 3 register operands
2f1bada2 7883 plus 1 memory operand, with VexXDS. */
922d8de8 7884 gas_assert ((i.reg_operands == 4
bed3d976
JB
7885 || (i.reg_operands == 3 && i.mem_operands == 1))
7886 && i.tm.opcode_modifier.vexvvvv == VEXXDS
dcd7e323 7887 && i.tm.opcode_modifier.vexw
3528c362 7888 && i.tm.operand_types[dest].bitfield.class == RegSIMD);
a683cc34 7889
48db9223
JB
7890 /* If VexW1 is set, the first non-immediate operand is the source and
7891 the second non-immediate one is encoded in the immediate operand. */
7892 if (i.tm.opcode_modifier.vexw == VEXW1)
7893 {
7894 source = i.imm_operands;
7895 reg_slot = i.imm_operands + 1;
7896 }
7897 else
7898 {
7899 source = i.imm_operands + 1;
7900 reg_slot = i.imm_operands;
7901 }
7902
a683cc34 7903 if (i.imm_operands == 0)
bed3d976
JB
7904 {
7905 /* When there is no immediate operand, generate an 8bit
7906 immediate operand to encode the first operand. */
7907 exp = &im_expressions[i.imm_operands++];
7908 i.op[i.operands].imms = exp;
7909 i.types[i.operands] = imm8;
7910 i.operands++;
7911
3528c362 7912 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
bed3d976
JB
7913 exp->X_op = O_constant;
7914 exp->X_add_number = register_number (i.op[reg_slot].regs) << 4;
43234a1e
L
7915 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
7916 }
922d8de8 7917 else
bed3d976 7918 {
9d3bf266
JB
7919 gas_assert (i.imm_operands == 1);
7920 gas_assert (fits_in_imm4 (i.op[0].imms->X_add_number));
7921 gas_assert (!i.tm.opcode_modifier.immext);
a683cc34 7922
9d3bf266
JB
7923 /* Turn on Imm8 again so that output_imm will generate it. */
7924 i.types[0].bitfield.imm8 = 1;
bed3d976 7925
3528c362 7926 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
9d3bf266 7927 i.op[0].imms->X_add_number
bed3d976 7928 |= register_number (i.op[reg_slot].regs) << 4;
43234a1e 7929 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
bed3d976 7930 }
a683cc34 7931
3528c362 7932 gas_assert (i.tm.operand_types[nds].bitfield.class == RegSIMD);
dae39acc 7933 i.vex.register_specifier = i.op[nds].regs;
c0f3af97
L
7934 }
7935 else
7936 source = dest = 0;
29b0f896
AM
7937
7938 /* i.reg_operands MUST be the number of real register operands;
c0f3af97
L
7939 implicit registers do not count. If there are 3 register
7940 operands, it must be a instruction with VexNDS. For a
7941 instruction with VexNDD, the destination register is encoded
7942 in VEX prefix. If there are 4 register operands, it must be
7943 a instruction with VEX prefix and 3 sources. */
7ab9ffdd
L
7944 if (i.mem_operands == 0
7945 && ((i.reg_operands == 2
2426c15f 7946 && i.tm.opcode_modifier.vexvvvv <= VEXXDS)
7ab9ffdd 7947 || (i.reg_operands == 3
2426c15f 7948 && i.tm.opcode_modifier.vexvvvv == VEXXDS)
7ab9ffdd 7949 || (i.reg_operands == 4 && vex_3_sources)))
29b0f896 7950 {
cab737b9
L
7951 switch (i.operands)
7952 {
7953 case 2:
7954 source = 0;
7955 break;
7956 case 3:
c81128dc
L
7957 /* When there are 3 operands, one of them may be immediate,
7958 which may be the first or the last operand. Otherwise,
c0f3af97
L
7959 the first operand must be shift count register (cl) or it
7960 is an instruction with VexNDS. */
9c2799c2 7961 gas_assert (i.imm_operands == 1
7ab9ffdd 7962 || (i.imm_operands == 0
2426c15f 7963 && (i.tm.opcode_modifier.vexvvvv == VEXXDS
75e5731b
JB
7964 || (i.types[0].bitfield.instance == RegC
7965 && i.types[0].bitfield.byte))));
40fb9820 7966 if (operand_type_check (i.types[0], imm)
75e5731b
JB
7967 || (i.types[0].bitfield.instance == RegC
7968 && i.types[0].bitfield.byte))
40fb9820
L
7969 source = 1;
7970 else
7971 source = 0;
cab737b9
L
7972 break;
7973 case 4:
368d64cc
L
7974 /* When there are 4 operands, the first two must be 8bit
7975 immediate operands. The source operand will be the 3rd
c0f3af97
L
7976 one.
7977
7978 For instructions with VexNDS, if the first operand
7979 an imm8, the source operand is the 2nd one. If the last
7980 operand is imm8, the source operand is the first one. */
9c2799c2 7981 gas_assert ((i.imm_operands == 2
7ab9ffdd
L
7982 && i.types[0].bitfield.imm8
7983 && i.types[1].bitfield.imm8)
2426c15f 7984 || (i.tm.opcode_modifier.vexvvvv == VEXXDS
7ab9ffdd
L
7985 && i.imm_operands == 1
7986 && (i.types[0].bitfield.imm8
43234a1e 7987 || i.types[i.operands - 1].bitfield.imm8
ca5312a2 7988 || i.rounding.type != rc_none)));
9f2670f2
L
7989 if (i.imm_operands == 2)
7990 source = 2;
7991 else
c0f3af97
L
7992 {
7993 if (i.types[0].bitfield.imm8)
7994 source = 1;
7995 else
7996 source = 0;
7997 }
c0f3af97
L
7998 break;
7999 case 5:
e771e7c9 8000 if (is_evex_encoding (&i.tm))
43234a1e
L
8001 {
8002 /* For EVEX instructions, when there are 5 operands, the
8003 first one must be immediate operand. If the second one
8004 is immediate operand, the source operand is the 3th
8005 one. If the last one is immediate operand, the source
8006 operand is the 2nd one. */
8007 gas_assert (i.imm_operands == 2
8008 && i.tm.opcode_modifier.sae
8009 && operand_type_check (i.types[0], imm));
8010 if (operand_type_check (i.types[1], imm))
8011 source = 2;
8012 else if (operand_type_check (i.types[4], imm))
8013 source = 1;
8014 else
8015 abort ();
8016 }
cab737b9
L
8017 break;
8018 default:
8019 abort ();
8020 }
8021
c0f3af97
L
8022 if (!vex_3_sources)
8023 {
8024 dest = source + 1;
8025
43234a1e
L
8026 /* RC/SAE operand could be between DEST and SRC. That happens
8027 when one operand is GPR and the other one is XMM/YMM/ZMM
8028 register. */
ca5312a2 8029 if (i.rounding.type != rc_none && i.rounding.operand == dest)
43234a1e
L
8030 dest++;
8031
2426c15f 8032 if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
c0f3af97 8033 {
43234a1e 8034 /* For instructions with VexNDS, the register-only source
c5d0745b 8035 operand must be a 32/64bit integer, XMM, YMM, ZMM, or mask
dfd69174 8036 register. It is encoded in VEX prefix. */
f12dc422
L
8037
8038 i386_operand_type op;
8039 unsigned int vvvv;
8040
c2ecccb3
L
8041 /* Swap two source operands if needed. */
8042 if (i.tm.opcode_modifier.swapsources)
f12dc422
L
8043 {
8044 vvvv = source;
8045 source = dest;
8046 }
8047 else
8048 vvvv = dest;
8049
8050 op = i.tm.operand_types[vvvv];
c0f3af97 8051 if ((dest + 1) >= i.operands
bab6aec1 8052 || ((op.bitfield.class != Reg
dc821c5f 8053 || (!op.bitfield.dword && !op.bitfield.qword))
3528c362 8054 && op.bitfield.class != RegSIMD
43234a1e 8055 && !operand_type_equal (&op, &regmask)))
c0f3af97 8056 abort ();
f12dc422 8057 i.vex.register_specifier = i.op[vvvv].regs;
c0f3af97
L
8058 dest++;
8059 }
8060 }
29b0f896
AM
8061
8062 i.rm.mode = 3;
dfd69174
JB
8063 /* One of the register operands will be encoded in the i.rm.reg
8064 field, the other in the combined i.rm.mode and i.rm.regmem
29b0f896
AM
8065 fields. If no form of this instruction supports a memory
8066 destination operand, then we assume the source operand may
8067 sometimes be a memory operand and so we need to store the
8068 destination in the i.rm.reg field. */
dfd69174 8069 if (!i.tm.opcode_modifier.regmem
40fb9820 8070 && operand_type_check (i.tm.operand_types[dest], anymem) == 0)
29b0f896
AM
8071 {
8072 i.rm.reg = i.op[dest].regs->reg_num;
8073 i.rm.regmem = i.op[source].regs->reg_num;
a5aeccd9 8074 set_rex_vrex (i.op[dest].regs, REX_R, i.tm.opcode_modifier.sse2avx);
5b7c81bd 8075 set_rex_vrex (i.op[source].regs, REX_B, false);
29b0f896
AM
8076 }
8077 else
8078 {
8079 i.rm.reg = i.op[source].regs->reg_num;
8080 i.rm.regmem = i.op[dest].regs->reg_num;
a5aeccd9 8081 set_rex_vrex (i.op[dest].regs, REX_B, i.tm.opcode_modifier.sse2avx);
5b7c81bd 8082 set_rex_vrex (i.op[source].regs, REX_R, false);
29b0f896 8083 }
e0c7f900 8084 if (flag_code != CODE_64BIT && (i.rex & REX_R))
c4a530c5 8085 {
4a5c67ed 8086 if (i.types[!i.tm.opcode_modifier.regmem].bitfield.class != RegCR)
c4a530c5 8087 abort ();
e0c7f900 8088 i.rex &= ~REX_R;
c4a530c5
JB
8089 add_prefix (LOCK_PREFIX_OPCODE);
8090 }
29b0f896
AM
8091 }
8092 else
8093 { /* If it's not 2 reg operands... */
c0f3af97
L
8094 unsigned int mem;
8095
29b0f896
AM
8096 if (i.mem_operands)
8097 {
8098 unsigned int fake_zero_displacement = 0;
99018f42 8099 unsigned int op;
4eed87de 8100
7ab9ffdd 8101 for (op = 0; op < i.operands; op++)
8dc0818e 8102 if (i.flags[op] & Operand_Mem)
7ab9ffdd 8103 break;
7ab9ffdd 8104 gas_assert (op < i.operands);
29b0f896 8105
63112cd6 8106 if (i.tm.opcode_modifier.sib)
6c30d220 8107 {
260cd341
LC
8108 /* The index register of VSIB shouldn't be RegIZ. */
8109 if (i.tm.opcode_modifier.sib != SIBMEM
8110 && i.index_reg->reg_num == RegIZ)
6c30d220
L
8111 abort ();
8112
8113 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8114 if (!i.base_reg)
8115 {
8116 i.sib.base = NO_BASE_REGISTER;
8117 i.sib.scale = i.log2_scale_factor;
8118 i.types[op].bitfield.disp8 = 0;
8119 i.types[op].bitfield.disp16 = 0;
8120 i.types[op].bitfield.disp64 = 0;
a9aabc23 8121 if (want_disp32 (&i.tm))
6c30d220
L
8122 {
8123 /* Must be 32 bit */
8124 i.types[op].bitfield.disp32 = 1;
8125 i.types[op].bitfield.disp32s = 0;
8126 }
8127 else
8128 {
8129 i.types[op].bitfield.disp32 = 0;
8130 i.types[op].bitfield.disp32s = 1;
8131 }
8132 }
260cd341
LC
8133
8134 /* Since the mandatory SIB always has index register, so
8135 the code logic remains unchanged. The non-mandatory SIB
8136 without index register is allowed and will be handled
8137 later. */
8138 if (i.index_reg)
8139 {
8140 if (i.index_reg->reg_num == RegIZ)
8141 i.sib.index = NO_INDEX_REGISTER;
8142 else
8143 i.sib.index = i.index_reg->reg_num;
5b7c81bd 8144 set_rex_vrex (i.index_reg, REX_X, false);
260cd341 8145 }
6c30d220
L
8146 }
8147
5e042380 8148 default_seg = reg_ds;
29b0f896
AM
8149
8150 if (i.base_reg == 0)
8151 {
8152 i.rm.mode = 0;
8153 if (!i.disp_operands)
9bb129e8 8154 fake_zero_displacement = 1;
29b0f896
AM
8155 if (i.index_reg == 0)
8156 {
73053c1f
JB
8157 i386_operand_type newdisp;
8158
260cd341
LC
8159 /* Both check for VSIB and mandatory non-vector SIB. */
8160 gas_assert (!i.tm.opcode_modifier.sib
8161 || i.tm.opcode_modifier.sib == SIBMEM);
29b0f896 8162 /* Operand is just <disp> */
20f0a1fc 8163 if (flag_code == CODE_64BIT)
29b0f896
AM
8164 {
8165 /* 64bit mode overwrites the 32bit absolute
8166 addressing by RIP relative addressing and
8167 absolute addressing is encoded by one of the
8168 redundant SIB forms. */
8169 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8170 i.sib.base = NO_BASE_REGISTER;
8171 i.sib.index = NO_INDEX_REGISTER;
a9aabc23 8172 newdisp = (want_disp32(&i.tm) ? disp32 : disp32s);
20f0a1fc 8173 }
fc225355
L
8174 else if ((flag_code == CODE_16BIT)
8175 ^ (i.prefix[ADDR_PREFIX] != 0))
20f0a1fc
NC
8176 {
8177 i.rm.regmem = NO_BASE_REGISTER_16;
73053c1f 8178 newdisp = disp16;
20f0a1fc
NC
8179 }
8180 else
8181 {
8182 i.rm.regmem = NO_BASE_REGISTER;
73053c1f 8183 newdisp = disp32;
29b0f896 8184 }
73053c1f
JB
8185 i.types[op] = operand_type_and_not (i.types[op], anydisp);
8186 i.types[op] = operand_type_or (i.types[op], newdisp);
29b0f896 8187 }
63112cd6 8188 else if (!i.tm.opcode_modifier.sib)
29b0f896 8189 {
6c30d220 8190 /* !i.base_reg && i.index_reg */
e968fc9b 8191 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8192 i.sib.index = NO_INDEX_REGISTER;
8193 else
8194 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8195 i.sib.base = NO_BASE_REGISTER;
8196 i.sib.scale = i.log2_scale_factor;
8197 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
40fb9820
L
8198 i.types[op].bitfield.disp8 = 0;
8199 i.types[op].bitfield.disp16 = 0;
8200 i.types[op].bitfield.disp64 = 0;
a9aabc23 8201 if (want_disp32 (&i.tm))
40fb9820
L
8202 {
8203 /* Must be 32 bit */
8204 i.types[op].bitfield.disp32 = 1;
8205 i.types[op].bitfield.disp32s = 0;
8206 }
29b0f896 8207 else
40fb9820
L
8208 {
8209 i.types[op].bitfield.disp32 = 0;
8210 i.types[op].bitfield.disp32s = 1;
8211 }
29b0f896 8212 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8213 i.rex |= REX_X;
29b0f896
AM
8214 }
8215 }
8216 /* RIP addressing for 64bit mode. */
e968fc9b 8217 else if (i.base_reg->reg_num == RegIP)
29b0f896 8218 {
63112cd6 8219 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896 8220 i.rm.regmem = NO_BASE_REGISTER;
40fb9820
L
8221 i.types[op].bitfield.disp8 = 0;
8222 i.types[op].bitfield.disp16 = 0;
8223 i.types[op].bitfield.disp32 = 0;
8224 i.types[op].bitfield.disp32s = 1;
8225 i.types[op].bitfield.disp64 = 0;
71903a11 8226 i.flags[op] |= Operand_PCrel;
20f0a1fc
NC
8227 if (! i.disp_operands)
8228 fake_zero_displacement = 1;
29b0f896 8229 }
dc821c5f 8230 else if (i.base_reg->reg_type.bitfield.word)
29b0f896 8231 {
63112cd6 8232 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896
AM
8233 switch (i.base_reg->reg_num)
8234 {
8235 case 3: /* (%bx) */
8236 if (i.index_reg == 0)
8237 i.rm.regmem = 7;
8238 else /* (%bx,%si) -> 0, or (%bx,%di) -> 1 */
8239 i.rm.regmem = i.index_reg->reg_num - 6;
8240 break;
8241 case 5: /* (%bp) */
5e042380 8242 default_seg = reg_ss;
29b0f896
AM
8243 if (i.index_reg == 0)
8244 {
8245 i.rm.regmem = 6;
40fb9820 8246 if (operand_type_check (i.types[op], disp) == 0)
29b0f896
AM
8247 {
8248 /* fake (%bp) into 0(%bp) */
41eb8e88 8249 if (i.disp_encoding == disp_encoding_16bit)
1a02d6b0
L
8250 i.types[op].bitfield.disp16 = 1;
8251 else
8252 i.types[op].bitfield.disp8 = 1;
252b5132 8253 fake_zero_displacement = 1;
29b0f896
AM
8254 }
8255 }
8256 else /* (%bp,%si) -> 2, or (%bp,%di) -> 3 */
8257 i.rm.regmem = i.index_reg->reg_num - 6 + 2;
8258 break;
8259 default: /* (%si) -> 4 or (%di) -> 5 */
8260 i.rm.regmem = i.base_reg->reg_num - 6 + 4;
8261 }
41eb8e88
L
8262 if (!fake_zero_displacement
8263 && !i.disp_operands
8264 && i.disp_encoding)
8265 {
8266 fake_zero_displacement = 1;
8267 if (i.disp_encoding == disp_encoding_8bit)
8268 i.types[op].bitfield.disp8 = 1;
8269 else
8270 i.types[op].bitfield.disp16 = 1;
8271 }
29b0f896
AM
8272 i.rm.mode = mode_from_disp_size (i.types[op]);
8273 }
8274 else /* i.base_reg and 32/64 bit mode */
8275 {
a9aabc23 8276 if (operand_type_check (i.types[op], disp))
40fb9820 8277 {
73053c1f
JB
8278 i.types[op].bitfield.disp16 = 0;
8279 i.types[op].bitfield.disp64 = 0;
a9aabc23 8280 if (!want_disp32 (&i.tm))
73053c1f
JB
8281 {
8282 i.types[op].bitfield.disp32 = 0;
8283 i.types[op].bitfield.disp32s = 1;
8284 }
40fb9820 8285 else
73053c1f
JB
8286 {
8287 i.types[op].bitfield.disp32 = 1;
8288 i.types[op].bitfield.disp32s = 0;
8289 }
40fb9820 8290 }
20f0a1fc 8291
63112cd6 8292 if (!i.tm.opcode_modifier.sib)
6c30d220 8293 i.rm.regmem = i.base_reg->reg_num;
29b0f896 8294 if ((i.base_reg->reg_flags & RegRex) != 0)
161a04f6 8295 i.rex |= REX_B;
29b0f896
AM
8296 i.sib.base = i.base_reg->reg_num;
8297 /* x86-64 ignores REX prefix bit here to avoid decoder
8298 complications. */
848930b2
JB
8299 if (!(i.base_reg->reg_flags & RegRex)
8300 && (i.base_reg->reg_num == EBP_REG_NUM
8301 || i.base_reg->reg_num == ESP_REG_NUM))
5e042380 8302 default_seg = reg_ss;
848930b2 8303 if (i.base_reg->reg_num == 5 && i.disp_operands == 0)
29b0f896 8304 {
848930b2 8305 fake_zero_displacement = 1;
1a02d6b0
L
8306 if (i.disp_encoding == disp_encoding_32bit)
8307 i.types[op].bitfield.disp32 = 1;
8308 else
8309 i.types[op].bitfield.disp8 = 1;
29b0f896
AM
8310 }
8311 i.sib.scale = i.log2_scale_factor;
8312 if (i.index_reg == 0)
8313 {
260cd341
LC
8314 /* Only check for VSIB. */
8315 gas_assert (i.tm.opcode_modifier.sib != VECSIB128
8316 && i.tm.opcode_modifier.sib != VECSIB256
8317 && i.tm.opcode_modifier.sib != VECSIB512);
8318
29b0f896
AM
8319 /* <disp>(%esp) becomes two byte modrm with no index
8320 register. We've already stored the code for esp
8321 in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
8322 Any base register besides %esp will not use the
8323 extra modrm byte. */
8324 i.sib.index = NO_INDEX_REGISTER;
29b0f896 8325 }
63112cd6 8326 else if (!i.tm.opcode_modifier.sib)
29b0f896 8327 {
e968fc9b 8328 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8329 i.sib.index = NO_INDEX_REGISTER;
8330 else
8331 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8332 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8333 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8334 i.rex |= REX_X;
29b0f896 8335 }
67a4f2b7
AO
8336
8337 if (i.disp_operands
8338 && (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
8339 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL))
8340 i.rm.mode = 0;
8341 else
a501d77e
L
8342 {
8343 if (!fake_zero_displacement
8344 && !i.disp_operands
8345 && i.disp_encoding)
8346 {
8347 fake_zero_displacement = 1;
8348 if (i.disp_encoding == disp_encoding_8bit)
8349 i.types[op].bitfield.disp8 = 1;
8350 else
8351 i.types[op].bitfield.disp32 = 1;
8352 }
8353 i.rm.mode = mode_from_disp_size (i.types[op]);
8354 }
29b0f896 8355 }
252b5132 8356
29b0f896
AM
8357 if (fake_zero_displacement)
8358 {
8359 /* Fakes a zero displacement assuming that i.types[op]
8360 holds the correct displacement size. */
8361 expressionS *exp;
8362
9c2799c2 8363 gas_assert (i.op[op].disps == 0);
29b0f896
AM
8364 exp = &disp_expressions[i.disp_operands++];
8365 i.op[op].disps = exp;
8366 exp->X_op = O_constant;
8367 exp->X_add_number = 0;
8368 exp->X_add_symbol = (symbolS *) 0;
8369 exp->X_op_symbol = (symbolS *) 0;
8370 }
c0f3af97
L
8371
8372 mem = op;
29b0f896 8373 }
c0f3af97
L
8374 else
8375 mem = ~0;
252b5132 8376
8c43a48b 8377 if (i.tm.opcode_modifier.vexsources == XOP2SOURCES)
5dd85c99
SP
8378 {
8379 if (operand_type_check (i.types[0], imm))
8380 i.vex.register_specifier = NULL;
8381 else
8382 {
8383 /* VEX.vvvv encodes one of the sources when the first
8384 operand is not an immediate. */
1ef99a7b 8385 if (i.tm.opcode_modifier.vexw == VEXW0)
5dd85c99
SP
8386 i.vex.register_specifier = i.op[0].regs;
8387 else
8388 i.vex.register_specifier = i.op[1].regs;
8389 }
8390
8391 /* Destination is a XMM register encoded in the ModRM.reg
8392 and VEX.R bit. */
8393 i.rm.reg = i.op[2].regs->reg_num;
8394 if ((i.op[2].regs->reg_flags & RegRex) != 0)
8395 i.rex |= REX_R;
8396
8397 /* ModRM.rm and VEX.B encodes the other source. */
8398 if (!i.mem_operands)
8399 {
8400 i.rm.mode = 3;
8401
1ef99a7b 8402 if (i.tm.opcode_modifier.vexw == VEXW0)
5dd85c99
SP
8403 i.rm.regmem = i.op[1].regs->reg_num;
8404 else
8405 i.rm.regmem = i.op[0].regs->reg_num;
8406
8407 if ((i.op[1].regs->reg_flags & RegRex) != 0)
8408 i.rex |= REX_B;
8409 }
8410 }
2426c15f 8411 else if (i.tm.opcode_modifier.vexvvvv == VEXLWP)
f88c9eb0
SP
8412 {
8413 i.vex.register_specifier = i.op[2].regs;
8414 if (!i.mem_operands)
8415 {
8416 i.rm.mode = 3;
8417 i.rm.regmem = i.op[1].regs->reg_num;
8418 if ((i.op[1].regs->reg_flags & RegRex) != 0)
8419 i.rex |= REX_B;
8420 }
8421 }
29b0f896
AM
8422 /* Fill in i.rm.reg or i.rm.regmem field with register operand
8423 (if any) based on i.tm.extension_opcode. Again, we must be
8424 careful to make sure that segment/control/debug/test/MMX
8425 registers are coded into the i.rm.reg field. */
f88c9eb0 8426 else if (i.reg_operands)
29b0f896 8427 {
99018f42 8428 unsigned int op;
7ab9ffdd
L
8429 unsigned int vex_reg = ~0;
8430
8431 for (op = 0; op < i.operands; op++)
921eafea
L
8432 if (i.types[op].bitfield.class == Reg
8433 || i.types[op].bitfield.class == RegBND
8434 || i.types[op].bitfield.class == RegMask
8435 || i.types[op].bitfield.class == SReg
8436 || i.types[op].bitfield.class == RegCR
8437 || i.types[op].bitfield.class == RegDR
8438 || i.types[op].bitfield.class == RegTR
8439 || i.types[op].bitfield.class == RegSIMD
8440 || i.types[op].bitfield.class == RegMMX)
8441 break;
c0209578 8442
7ab9ffdd
L
8443 if (vex_3_sources)
8444 op = dest;
2426c15f 8445 else if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
7ab9ffdd
L
8446 {
8447 /* For instructions with VexNDS, the register-only
8448 source operand is encoded in VEX prefix. */
8449 gas_assert (mem != (unsigned int) ~0);
c0f3af97 8450
7ab9ffdd 8451 if (op > mem)
c0f3af97 8452 {
7ab9ffdd
L
8453 vex_reg = op++;
8454 gas_assert (op < i.operands);
c0f3af97
L
8455 }
8456 else
c0f3af97 8457 {
f12dc422
L
8458 /* Check register-only source operand when two source
8459 operands are swapped. */
8460 if (!i.tm.operand_types[op].bitfield.baseindex
8461 && i.tm.operand_types[op + 1].bitfield.baseindex)
8462 {
8463 vex_reg = op;
8464 op += 2;
8465 gas_assert (mem == (vex_reg + 1)
8466 && op < i.operands);
8467 }
8468 else
8469 {
8470 vex_reg = op + 1;
8471 gas_assert (vex_reg < i.operands);
8472 }
c0f3af97 8473 }
7ab9ffdd 8474 }
2426c15f 8475 else if (i.tm.opcode_modifier.vexvvvv == VEXNDD)
7ab9ffdd 8476 {
f12dc422 8477 /* For instructions with VexNDD, the register destination
7ab9ffdd 8478 is encoded in VEX prefix. */
f12dc422
L
8479 if (i.mem_operands == 0)
8480 {
8481 /* There is no memory operand. */
8482 gas_assert ((op + 2) == i.operands);
8483 vex_reg = op + 1;
8484 }
8485 else
8d63c93e 8486 {
ed438a93
JB
8487 /* There are only 2 non-immediate operands. */
8488 gas_assert (op < i.imm_operands + 2
8489 && i.operands == i.imm_operands + 2);
8490 vex_reg = i.imm_operands + 1;
f12dc422 8491 }
7ab9ffdd
L
8492 }
8493 else
8494 gas_assert (op < i.operands);
99018f42 8495
7ab9ffdd
L
8496 if (vex_reg != (unsigned int) ~0)
8497 {
f12dc422 8498 i386_operand_type *type = &i.tm.operand_types[vex_reg];
7ab9ffdd 8499
bab6aec1 8500 if ((type->bitfield.class != Reg
dc821c5f 8501 || (!type->bitfield.dword && !type->bitfield.qword))
3528c362 8502 && type->bitfield.class != RegSIMD
43234a1e 8503 && !operand_type_equal (type, &regmask))
7ab9ffdd 8504 abort ();
f88c9eb0 8505
7ab9ffdd
L
8506 i.vex.register_specifier = i.op[vex_reg].regs;
8507 }
8508
1b9f0c97
L
8509 /* Don't set OP operand twice. */
8510 if (vex_reg != op)
7ab9ffdd 8511 {
1b9f0c97
L
8512 /* If there is an extension opcode to put here, the
8513 register number must be put into the regmem field. */
8514 if (i.tm.extension_opcode != None)
8515 {
8516 i.rm.regmem = i.op[op].regs->reg_num;
a5aeccd9
JB
8517 set_rex_vrex (i.op[op].regs, REX_B,
8518 i.tm.opcode_modifier.sse2avx);
1b9f0c97
L
8519 }
8520 else
8521 {
8522 i.rm.reg = i.op[op].regs->reg_num;
a5aeccd9
JB
8523 set_rex_vrex (i.op[op].regs, REX_R,
8524 i.tm.opcode_modifier.sse2avx);
1b9f0c97 8525 }
7ab9ffdd 8526 }
252b5132 8527
29b0f896
AM
8528 /* Now, if no memory operand has set i.rm.mode = 0, 1, 2 we
8529 must set it to 3 to indicate this is a register operand
8530 in the regmem field. */
8531 if (!i.mem_operands)
8532 i.rm.mode = 3;
8533 }
252b5132 8534
29b0f896 8535 /* Fill in i.rm.reg field with extension opcode (if any). */
c1e679ec 8536 if (i.tm.extension_opcode != None)
29b0f896
AM
8537 i.rm.reg = i.tm.extension_opcode;
8538 }
8539 return default_seg;
8540}
252b5132 8541
48ef937e
JB
8542static INLINE void
8543frag_opcode_byte (unsigned char byte)
8544{
8545 if (now_seg != absolute_section)
8546 FRAG_APPEND_1_CHAR (byte);
8547 else
8548 ++abs_section_offset;
8549}
8550
376cd056
JB
8551static unsigned int
8552flip_code16 (unsigned int code16)
8553{
8554 gas_assert (i.tm.operands == 1);
8555
8556 return !(i.prefix[REX_PREFIX] & REX_W)
8557 && (code16 ? i.tm.operand_types[0].bitfield.disp32
8558 || i.tm.operand_types[0].bitfield.disp32s
8559 : i.tm.operand_types[0].bitfield.disp16)
8560 ? CODE16 : 0;
8561}
8562
29b0f896 8563static void
e3bb37b5 8564output_branch (void)
29b0f896
AM
8565{
8566 char *p;
f8a5c266 8567 int size;
29b0f896
AM
8568 int code16;
8569 int prefix;
8570 relax_substateT subtype;
8571 symbolS *sym;
8572 offsetT off;
8573
48ef937e
JB
8574 if (now_seg == absolute_section)
8575 {
8576 as_bad (_("relaxable branches not supported in absolute section"));
8577 return;
8578 }
8579
f8a5c266 8580 code16 = flag_code == CODE_16BIT ? CODE16 : 0;
a501d77e 8581 size = i.disp_encoding == disp_encoding_32bit ? BIG : SMALL;
29b0f896
AM
8582
8583 prefix = 0;
8584 if (i.prefix[DATA_PREFIX] != 0)
252b5132 8585 {
29b0f896
AM
8586 prefix = 1;
8587 i.prefixes -= 1;
376cd056 8588 code16 ^= flip_code16(code16);
252b5132 8589 }
29b0f896
AM
8590 /* Pentium4 branch hints. */
8591 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8592 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
2f66722d 8593 {
29b0f896
AM
8594 prefix++;
8595 i.prefixes--;
8596 }
8597 if (i.prefix[REX_PREFIX] != 0)
8598 {
8599 prefix++;
8600 i.prefixes--;
2f66722d
AM
8601 }
8602
7e8b059b
L
8603 /* BND prefixed jump. */
8604 if (i.prefix[BND_PREFIX] != 0)
8605 {
6cb0a70e
JB
8606 prefix++;
8607 i.prefixes--;
7e8b059b
L
8608 }
8609
f2810fe0
JB
8610 if (i.prefixes != 0)
8611 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
29b0f896
AM
8612
8613 /* It's always a symbol; End frag & setup for relax.
8614 Make sure there is enough room in this frag for the largest
8615 instruction we may generate in md_convert_frag. This is 2
8616 bytes for the opcode and room for the prefix and largest
8617 displacement. */
8618 frag_grow (prefix + 2 + 4);
8619 /* Prefix and 1 opcode byte go in fr_fix. */
8620 p = frag_more (prefix + 1);
8621 if (i.prefix[DATA_PREFIX] != 0)
8622 *p++ = DATA_PREFIX_OPCODE;
8623 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
8624 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
8625 *p++ = i.prefix[SEG_PREFIX];
6cb0a70e
JB
8626 if (i.prefix[BND_PREFIX] != 0)
8627 *p++ = BND_PREFIX_OPCODE;
29b0f896
AM
8628 if (i.prefix[REX_PREFIX] != 0)
8629 *p++ = i.prefix[REX_PREFIX];
8630 *p = i.tm.base_opcode;
8631
8632 if ((unsigned char) *p == JUMP_PC_RELATIVE)
f8a5c266 8633 subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, size);
40fb9820 8634 else if (cpu_arch_flags.bitfield.cpui386)
f8a5c266 8635 subtype = ENCODE_RELAX_STATE (COND_JUMP, size);
29b0f896 8636 else
f8a5c266 8637 subtype = ENCODE_RELAX_STATE (COND_JUMP86, size);
29b0f896 8638 subtype |= code16;
3e73aa7c 8639
29b0f896
AM
8640 sym = i.op[0].disps->X_add_symbol;
8641 off = i.op[0].disps->X_add_number;
3e73aa7c 8642
29b0f896
AM
8643 if (i.op[0].disps->X_op != O_constant
8644 && i.op[0].disps->X_op != O_symbol)
3e73aa7c 8645 {
29b0f896
AM
8646 /* Handle complex expressions. */
8647 sym = make_expr_symbol (i.op[0].disps);
8648 off = 0;
8649 }
3e73aa7c 8650
29b0f896
AM
8651 /* 1 possible extra opcode + 4 byte displacement go in var part.
8652 Pass reloc in fr_var. */
d258b828 8653 frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
29b0f896 8654}
3e73aa7c 8655
bd7ab16b
L
8656#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8657/* Return TRUE iff PLT32 relocation should be used for branching to
8658 symbol S. */
8659
5b7c81bd 8660static bool
bd7ab16b
L
8661need_plt32_p (symbolS *s)
8662{
8663 /* PLT32 relocation is ELF only. */
8664 if (!IS_ELF)
5b7c81bd 8665 return false;
bd7ab16b 8666
a5def729
RO
8667#ifdef TE_SOLARIS
8668 /* Don't emit PLT32 relocation on Solaris: neither native linker nor
8669 krtld support it. */
5b7c81bd 8670 return false;
a5def729
RO
8671#endif
8672
bd7ab16b
L
8673 /* Since there is no need to prepare for PLT branch on x86-64, we
8674 can generate R_X86_64_PLT32, instead of R_X86_64_PC32, which can
8675 be used as a marker for 32-bit PC-relative branches. */
8676 if (!object_64bit)
5b7c81bd 8677 return false;
bd7ab16b 8678
44365e88 8679 if (s == NULL)
5b7c81bd 8680 return false;
44365e88 8681
bd7ab16b
L
8682 /* Weak or undefined symbol need PLT32 relocation. */
8683 if (S_IS_WEAK (s) || !S_IS_DEFINED (s))
5b7c81bd 8684 return true;
bd7ab16b
L
8685
8686 /* Non-global symbol doesn't need PLT32 relocation. */
8687 if (! S_IS_EXTERNAL (s))
5b7c81bd 8688 return false;
bd7ab16b
L
8689
8690 /* Other global symbols need PLT32 relocation. NB: Symbol with
8691 non-default visibilities are treated as normal global symbol
8692 so that PLT32 relocation can be used as a marker for 32-bit
8693 PC-relative branches. It is useful for linker relaxation. */
5b7c81bd 8694 return true;
bd7ab16b
L
8695}
8696#endif
8697
29b0f896 8698static void
e3bb37b5 8699output_jump (void)
29b0f896
AM
8700{
8701 char *p;
8702 int size;
3e02c1cc 8703 fixS *fixP;
bd7ab16b 8704 bfd_reloc_code_real_type jump_reloc = i.reloc[0];
29b0f896 8705
0cfa3eb3 8706 if (i.tm.opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
8707 {
8708 /* This is a loop or jecxz type instruction. */
8709 size = 1;
8710 if (i.prefix[ADDR_PREFIX] != 0)
8711 {
48ef937e 8712 frag_opcode_byte (ADDR_PREFIX_OPCODE);
29b0f896
AM
8713 i.prefixes -= 1;
8714 }
8715 /* Pentium4 branch hints. */
8716 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8717 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
8718 {
48ef937e 8719 frag_opcode_byte (i.prefix[SEG_PREFIX]);
29b0f896 8720 i.prefixes--;
3e73aa7c
JH
8721 }
8722 }
29b0f896
AM
8723 else
8724 {
8725 int code16;
3e73aa7c 8726
29b0f896
AM
8727 code16 = 0;
8728 if (flag_code == CODE_16BIT)
8729 code16 = CODE16;
3e73aa7c 8730
29b0f896
AM
8731 if (i.prefix[DATA_PREFIX] != 0)
8732 {
48ef937e 8733 frag_opcode_byte (DATA_PREFIX_OPCODE);
29b0f896 8734 i.prefixes -= 1;
376cd056 8735 code16 ^= flip_code16(code16);
29b0f896 8736 }
252b5132 8737
29b0f896
AM
8738 size = 4;
8739 if (code16)
8740 size = 2;
8741 }
9fcc94b6 8742
6cb0a70e
JB
8743 /* BND prefixed jump. */
8744 if (i.prefix[BND_PREFIX] != 0)
29b0f896 8745 {
48ef937e 8746 frag_opcode_byte (i.prefix[BND_PREFIX]);
29b0f896
AM
8747 i.prefixes -= 1;
8748 }
252b5132 8749
6cb0a70e 8750 if (i.prefix[REX_PREFIX] != 0)
7e8b059b 8751 {
48ef937e 8752 frag_opcode_byte (i.prefix[REX_PREFIX]);
7e8b059b
L
8753 i.prefixes -= 1;
8754 }
8755
f2810fe0
JB
8756 if (i.prefixes != 0)
8757 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
e0890092 8758
48ef937e
JB
8759 if (now_seg == absolute_section)
8760 {
9a182d04 8761 abs_section_offset += i.opcode_length + size;
48ef937e
JB
8762 return;
8763 }
8764
9a182d04
JB
8765 p = frag_more (i.opcode_length + size);
8766 switch (i.opcode_length)
42164a71
L
8767 {
8768 case 2:
8769 *p++ = i.tm.base_opcode >> 8;
1a0670f3 8770 /* Fall through. */
42164a71
L
8771 case 1:
8772 *p++ = i.tm.base_opcode;
8773 break;
8774 default:
8775 abort ();
8776 }
e0890092 8777
bd7ab16b
L
8778#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8779 if (size == 4
8780 && jump_reloc == NO_RELOC
8781 && need_plt32_p (i.op[0].disps->X_add_symbol))
8782 jump_reloc = BFD_RELOC_X86_64_PLT32;
8783#endif
8784
8785 jump_reloc = reloc (size, 1, 1, jump_reloc);
8786
3e02c1cc 8787 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
bd7ab16b 8788 i.op[0].disps, 1, jump_reloc);
3e02c1cc
AM
8789
8790 /* All jumps handled here are signed, but don't use a signed limit
8791 check for 32 and 16 bit jumps as we want to allow wrap around at
8792 4G and 64k respectively. */
8793 if (size == 1)
8794 fixP->fx_signed = 1;
29b0f896 8795}
e0890092 8796
29b0f896 8797static void
e3bb37b5 8798output_interseg_jump (void)
29b0f896
AM
8799{
8800 char *p;
8801 int size;
8802 int prefix;
8803 int code16;
252b5132 8804
29b0f896
AM
8805 code16 = 0;
8806 if (flag_code == CODE_16BIT)
8807 code16 = CODE16;
a217f122 8808
29b0f896
AM
8809 prefix = 0;
8810 if (i.prefix[DATA_PREFIX] != 0)
8811 {
8812 prefix = 1;
8813 i.prefixes -= 1;
8814 code16 ^= CODE16;
8815 }
6cb0a70e
JB
8816
8817 gas_assert (!i.prefix[REX_PREFIX]);
252b5132 8818
29b0f896
AM
8819 size = 4;
8820 if (code16)
8821 size = 2;
252b5132 8822
f2810fe0
JB
8823 if (i.prefixes != 0)
8824 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
252b5132 8825
48ef937e
JB
8826 if (now_seg == absolute_section)
8827 {
8828 abs_section_offset += prefix + 1 + 2 + size;
8829 return;
8830 }
8831
29b0f896
AM
8832 /* 1 opcode; 2 segment; offset */
8833 p = frag_more (prefix + 1 + 2 + size);
3e73aa7c 8834
29b0f896
AM
8835 if (i.prefix[DATA_PREFIX] != 0)
8836 *p++ = DATA_PREFIX_OPCODE;
252b5132 8837
29b0f896
AM
8838 if (i.prefix[REX_PREFIX] != 0)
8839 *p++ = i.prefix[REX_PREFIX];
252b5132 8840
29b0f896
AM
8841 *p++ = i.tm.base_opcode;
8842 if (i.op[1].imms->X_op == O_constant)
8843 {
8844 offsetT n = i.op[1].imms->X_add_number;
252b5132 8845
29b0f896
AM
8846 if (size == 2
8847 && !fits_in_unsigned_word (n)
8848 && !fits_in_signed_word (n))
8849 {
8850 as_bad (_("16-bit jump out of range"));
8851 return;
8852 }
8853 md_number_to_chars (p, n, size);
8854 }
8855 else
8856 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
d258b828 8857 i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
6d96a594
C
8858
8859 p += size;
8860 if (i.op[0].imms->X_op == O_constant)
8861 md_number_to_chars (p, (valueT) i.op[0].imms->X_add_number, 2);
8862 else
8863 fix_new_exp (frag_now, p - frag_now->fr_literal, 2,
8864 i.op[0].imms, 0, reloc (2, 0, 0, i.reloc[0]));
29b0f896 8865}
a217f122 8866
b4a3a7b4
L
8867#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8868void
8869x86_cleanup (void)
8870{
8871 char *p;
8872 asection *seg = now_seg;
8873 subsegT subseg = now_subseg;
8874 asection *sec;
8875 unsigned int alignment, align_size_1;
8876 unsigned int isa_1_descsz, feature_2_descsz, descsz;
8877 unsigned int isa_1_descsz_raw, feature_2_descsz_raw;
8878 unsigned int padding;
8879
8880 if (!IS_ELF || !x86_used_note)
8881 return;
8882
b4a3a7b4
L
8883 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X86;
8884
8885 /* The .note.gnu.property section layout:
8886
8887 Field Length Contents
8888 ---- ---- ----
8889 n_namsz 4 4
8890 n_descsz 4 The note descriptor size
8891 n_type 4 NT_GNU_PROPERTY_TYPE_0
8892 n_name 4 "GNU"
8893 n_desc n_descsz The program property array
8894 .... .... ....
8895 */
8896
8897 /* Create the .note.gnu.property section. */
8898 sec = subseg_new (NOTE_GNU_PROPERTY_SECTION_NAME, 0);
fd361982 8899 bfd_set_section_flags (sec,
b4a3a7b4
L
8900 (SEC_ALLOC
8901 | SEC_LOAD
8902 | SEC_DATA
8903 | SEC_HAS_CONTENTS
8904 | SEC_READONLY));
8905
8906 if (get_elf_backend_data (stdoutput)->s->elfclass == ELFCLASS64)
8907 {
8908 align_size_1 = 7;
8909 alignment = 3;
8910 }
8911 else
8912 {
8913 align_size_1 = 3;
8914 alignment = 2;
8915 }
8916
fd361982 8917 bfd_set_section_alignment (sec, alignment);
b4a3a7b4
L
8918 elf_section_type (sec) = SHT_NOTE;
8919
8920 /* GNU_PROPERTY_X86_ISA_1_USED: 4-byte type + 4-byte data size
8921 + 4-byte data */
8922 isa_1_descsz_raw = 4 + 4 + 4;
8923 /* Align GNU_PROPERTY_X86_ISA_1_USED. */
8924 isa_1_descsz = (isa_1_descsz_raw + align_size_1) & ~align_size_1;
8925
8926 feature_2_descsz_raw = isa_1_descsz;
8927 /* GNU_PROPERTY_X86_FEATURE_2_USED: 4-byte type + 4-byte data size
8928 + 4-byte data */
8929 feature_2_descsz_raw += 4 + 4 + 4;
8930 /* Align GNU_PROPERTY_X86_FEATURE_2_USED. */
8931 feature_2_descsz = ((feature_2_descsz_raw + align_size_1)
8932 & ~align_size_1);
8933
8934 descsz = feature_2_descsz;
8935 /* Section size: n_namsz + n_descsz + n_type + n_name + n_descsz. */
8936 p = frag_more (4 + 4 + 4 + 4 + descsz);
8937
8938 /* Write n_namsz. */
8939 md_number_to_chars (p, (valueT) 4, 4);
8940
8941 /* Write n_descsz. */
8942 md_number_to_chars (p + 4, (valueT) descsz, 4);
8943
8944 /* Write n_type. */
8945 md_number_to_chars (p + 4 * 2, (valueT) NT_GNU_PROPERTY_TYPE_0, 4);
8946
8947 /* Write n_name. */
8948 memcpy (p + 4 * 3, "GNU", 4);
8949
8950 /* Write 4-byte type. */
8951 md_number_to_chars (p + 4 * 4,
8952 (valueT) GNU_PROPERTY_X86_ISA_1_USED, 4);
8953
8954 /* Write 4-byte data size. */
8955 md_number_to_chars (p + 4 * 5, (valueT) 4, 4);
8956
8957 /* Write 4-byte data. */
8958 md_number_to_chars (p + 4 * 6, (valueT) x86_isa_1_used, 4);
8959
8960 /* Zero out paddings. */
8961 padding = isa_1_descsz - isa_1_descsz_raw;
8962 if (padding)
8963 memset (p + 4 * 7, 0, padding);
8964
8965 /* Write 4-byte type. */
8966 md_number_to_chars (p + isa_1_descsz + 4 * 4,
8967 (valueT) GNU_PROPERTY_X86_FEATURE_2_USED, 4);
8968
8969 /* Write 4-byte data size. */
8970 md_number_to_chars (p + isa_1_descsz + 4 * 5, (valueT) 4, 4);
8971
8972 /* Write 4-byte data. */
8973 md_number_to_chars (p + isa_1_descsz + 4 * 6,
8974 (valueT) x86_feature_2_used, 4);
8975
8976 /* Zero out paddings. */
8977 padding = feature_2_descsz - feature_2_descsz_raw;
8978 if (padding)
8979 memset (p + isa_1_descsz + 4 * 7, 0, padding);
8980
8981 /* We probably can't restore the current segment, for there likely
8982 isn't one yet... */
8983 if (seg && subseg)
8984 subseg_set (seg, subseg);
8985}
8986#endif
8987
9c33702b
JB
8988static unsigned int
8989encoding_length (const fragS *start_frag, offsetT start_off,
8990 const char *frag_now_ptr)
8991{
8992 unsigned int len = 0;
8993
8994 if (start_frag != frag_now)
8995 {
8996 const fragS *fr = start_frag;
8997
8998 do {
8999 len += fr->fr_fix;
9000 fr = fr->fr_next;
9001 } while (fr && fr != frag_now);
9002 }
9003
9004 return len - start_off + (frag_now_ptr - frag_now->fr_literal);
9005}
9006
e379e5f3 9007/* Return 1 for test, and, cmp, add, sub, inc and dec which may
79d72f45
HL
9008 be macro-fused with conditional jumps.
9009 NB: If TEST/AND/CMP/ADD/SUB/INC/DEC is of RIP relative address,
9010 or is one of the following format:
9011
9012 cmp m, imm
9013 add m, imm
9014 sub m, imm
9015 test m, imm
9016 and m, imm
9017 inc m
9018 dec m
9019
9020 it is unfusible. */
e379e5f3
L
9021
9022static int
79d72f45 9023maybe_fused_with_jcc_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
9024{
9025 /* No RIP address. */
9026 if (i.base_reg && i.base_reg->reg_num == RegIP)
9027 return 0;
9028
389d00a5
JB
9029 /* No opcodes outside of base encoding space. */
9030 if (i.tm.opcode_modifier.opcodespace != SPACE_BASE)
e379e5f3
L
9031 return 0;
9032
79d72f45
HL
9033 /* add, sub without add/sub m, imm. */
9034 if (i.tm.base_opcode <= 5
e379e5f3
L
9035 || (i.tm.base_opcode >= 0x28 && i.tm.base_opcode <= 0x2d)
9036 || ((i.tm.base_opcode | 3) == 0x83
79d72f45 9037 && (i.tm.extension_opcode == 0x5
e379e5f3 9038 || i.tm.extension_opcode == 0x0)))
79d72f45
HL
9039 {
9040 *mf_cmp_p = mf_cmp_alu_cmp;
9041 return !(i.mem_operands && i.imm_operands);
9042 }
e379e5f3 9043
79d72f45
HL
9044 /* and without and m, imm. */
9045 if ((i.tm.base_opcode >= 0x20 && i.tm.base_opcode <= 0x25)
9046 || ((i.tm.base_opcode | 3) == 0x83
9047 && i.tm.extension_opcode == 0x4))
9048 {
9049 *mf_cmp_p = mf_cmp_test_and;
9050 return !(i.mem_operands && i.imm_operands);
9051 }
9052
9053 /* test without test m imm. */
e379e5f3
L
9054 if ((i.tm.base_opcode | 1) == 0x85
9055 || (i.tm.base_opcode | 1) == 0xa9
9056 || ((i.tm.base_opcode | 1) == 0xf7
79d72f45
HL
9057 && i.tm.extension_opcode == 0))
9058 {
9059 *mf_cmp_p = mf_cmp_test_and;
9060 return !(i.mem_operands && i.imm_operands);
9061 }
9062
9063 /* cmp without cmp m, imm. */
9064 if ((i.tm.base_opcode >= 0x38 && i.tm.base_opcode <= 0x3d)
e379e5f3
L
9065 || ((i.tm.base_opcode | 3) == 0x83
9066 && (i.tm.extension_opcode == 0x7)))
79d72f45
HL
9067 {
9068 *mf_cmp_p = mf_cmp_alu_cmp;
9069 return !(i.mem_operands && i.imm_operands);
9070 }
e379e5f3 9071
79d72f45 9072 /* inc, dec without inc/dec m. */
e379e5f3
L
9073 if ((i.tm.cpu_flags.bitfield.cpuno64
9074 && (i.tm.base_opcode | 0xf) == 0x4f)
9075 || ((i.tm.base_opcode | 1) == 0xff
9076 && i.tm.extension_opcode <= 0x1))
79d72f45
HL
9077 {
9078 *mf_cmp_p = mf_cmp_incdec;
9079 return !i.mem_operands;
9080 }
e379e5f3
L
9081
9082 return 0;
9083}
9084
9085/* Return 1 if a FUSED_JCC_PADDING frag should be generated. */
9086
9087static int
79d72f45 9088add_fused_jcc_padding_frag_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
9089{
9090 /* NB: Don't work with COND_JUMP86 without i386. */
9091 if (!align_branch_power
9092 || now_seg == absolute_section
9093 || !cpu_arch_flags.bitfield.cpui386
9094 || !(align_branch & align_branch_fused_bit))
9095 return 0;
9096
79d72f45 9097 if (maybe_fused_with_jcc_p (mf_cmp_p))
e379e5f3
L
9098 {
9099 if (last_insn.kind == last_insn_other
9100 || last_insn.seg != now_seg)
9101 return 1;
9102 if (flag_debug)
9103 as_warn_where (last_insn.file, last_insn.line,
9104 _("`%s` skips -malign-branch-boundary on `%s`"),
9105 last_insn.name, i.tm.name);
9106 }
9107
9108 return 0;
9109}
9110
9111/* Return 1 if a BRANCH_PREFIX frag should be generated. */
9112
9113static int
9114add_branch_prefix_frag_p (void)
9115{
9116 /* NB: Don't work with COND_JUMP86 without i386. Don't add prefix
9117 to PadLock instructions since they include prefixes in opcode. */
9118 if (!align_branch_power
9119 || !align_branch_prefix_size
9120 || now_seg == absolute_section
9121 || i.tm.cpu_flags.bitfield.cpupadlock
9122 || !cpu_arch_flags.bitfield.cpui386)
9123 return 0;
9124
9125 /* Don't add prefix if it is a prefix or there is no operand in case
9126 that segment prefix is special. */
9127 if (!i.operands || i.tm.opcode_modifier.isprefix)
9128 return 0;
9129
9130 if (last_insn.kind == last_insn_other
9131 || last_insn.seg != now_seg)
9132 return 1;
9133
9134 if (flag_debug)
9135 as_warn_where (last_insn.file, last_insn.line,
9136 _("`%s` skips -malign-branch-boundary on `%s`"),
9137 last_insn.name, i.tm.name);
9138
9139 return 0;
9140}
9141
9142/* Return 1 if a BRANCH_PADDING frag should be generated. */
9143
9144static int
79d72f45
HL
9145add_branch_padding_frag_p (enum align_branch_kind *branch_p,
9146 enum mf_jcc_kind *mf_jcc_p)
e379e5f3
L
9147{
9148 int add_padding;
9149
9150 /* NB: Don't work with COND_JUMP86 without i386. */
9151 if (!align_branch_power
9152 || now_seg == absolute_section
389d00a5
JB
9153 || !cpu_arch_flags.bitfield.cpui386
9154 || i.tm.opcode_modifier.opcodespace != SPACE_BASE)
e379e5f3
L
9155 return 0;
9156
9157 add_padding = 0;
9158
9159 /* Check for jcc and direct jmp. */
9160 if (i.tm.opcode_modifier.jump == JUMP)
9161 {
9162 if (i.tm.base_opcode == JUMP_PC_RELATIVE)
9163 {
9164 *branch_p = align_branch_jmp;
9165 add_padding = align_branch & align_branch_jmp_bit;
9166 }
9167 else
9168 {
79d72f45
HL
9169 /* Because J<cc> and JN<cc> share same group in macro-fusible table,
9170 igore the lowest bit. */
9171 *mf_jcc_p = (i.tm.base_opcode & 0x0e) >> 1;
e379e5f3
L
9172 *branch_p = align_branch_jcc;
9173 if ((align_branch & align_branch_jcc_bit))
9174 add_padding = 1;
9175 }
9176 }
e379e5f3
L
9177 else if ((i.tm.base_opcode | 1) == 0xc3)
9178 {
9179 /* Near ret. */
9180 *branch_p = align_branch_ret;
9181 if ((align_branch & align_branch_ret_bit))
9182 add_padding = 1;
9183 }
9184 else
9185 {
9186 /* Check for indirect jmp, direct and indirect calls. */
9187 if (i.tm.base_opcode == 0xe8)
9188 {
9189 /* Direct call. */
9190 *branch_p = align_branch_call;
9191 if ((align_branch & align_branch_call_bit))
9192 add_padding = 1;
9193 }
9194 else if (i.tm.base_opcode == 0xff
9195 && (i.tm.extension_opcode == 2
9196 || i.tm.extension_opcode == 4))
9197 {
9198 /* Indirect call and jmp. */
9199 *branch_p = align_branch_indirect;
9200 if ((align_branch & align_branch_indirect_bit))
9201 add_padding = 1;
9202 }
9203
9204 if (add_padding
9205 && i.disp_operands
9206 && tls_get_addr
9207 && (i.op[0].disps->X_op == O_symbol
9208 || (i.op[0].disps->X_op == O_subtract
9209 && i.op[0].disps->X_op_symbol == GOT_symbol)))
9210 {
9211 symbolS *s = i.op[0].disps->X_add_symbol;
9212 /* No padding to call to global or undefined tls_get_addr. */
9213 if ((S_IS_EXTERNAL (s) || !S_IS_DEFINED (s))
9214 && strcmp (S_GET_NAME (s), tls_get_addr) == 0)
9215 return 0;
9216 }
9217 }
9218
9219 if (add_padding
9220 && last_insn.kind != last_insn_other
9221 && last_insn.seg == now_seg)
9222 {
9223 if (flag_debug)
9224 as_warn_where (last_insn.file, last_insn.line,
9225 _("`%s` skips -malign-branch-boundary on `%s`"),
9226 last_insn.name, i.tm.name);
9227 return 0;
9228 }
9229
9230 return add_padding;
9231}
9232
29b0f896 9233static void
e3bb37b5 9234output_insn (void)
29b0f896 9235{
2bbd9c25
JJ
9236 fragS *insn_start_frag;
9237 offsetT insn_start_off;
e379e5f3
L
9238 fragS *fragP = NULL;
9239 enum align_branch_kind branch = align_branch_none;
79d72f45
HL
9240 /* The initializer is arbitrary just to avoid uninitialized error.
9241 it's actually either assigned in add_branch_padding_frag_p
9242 or never be used. */
9243 enum mf_jcc_kind mf_jcc = mf_jcc_jo;
2bbd9c25 9244
b4a3a7b4 9245#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
48ef937e 9246 if (IS_ELF && x86_used_note && now_seg != absolute_section)
b4a3a7b4 9247 {
32930e4e
L
9248 if ((i.xstate & xstate_tmm) == xstate_tmm
9249 || i.tm.cpu_flags.bitfield.cpuamx_tile)
9250 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_TMM;
9251
b4a3a7b4
L
9252 if (i.tm.cpu_flags.bitfield.cpu8087
9253 || i.tm.cpu_flags.bitfield.cpu287
9254 || i.tm.cpu_flags.bitfield.cpu387
9255 || i.tm.cpu_flags.bitfield.cpu687
9256 || i.tm.cpu_flags.bitfield.cpufisttp)
9257 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X87;
014d61ea 9258
921eafea 9259 if ((i.xstate & xstate_mmx)
389d00a5
JB
9260 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
9261 && !is_any_vex_encoding (&i.tm)
9262 && (i.tm.base_opcode == 0x77 /* emms */
9263 || i.tm.base_opcode == 0x0e /* femms */)))
b4a3a7b4 9264 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MMX;
014d61ea 9265
32930e4e
L
9266 if (i.index_reg)
9267 {
9268 if (i.index_reg->reg_type.bitfield.zmmword)
9269 i.xstate |= xstate_zmm;
9270 else if (i.index_reg->reg_type.bitfield.ymmword)
9271 i.xstate |= xstate_ymm;
9272 else if (i.index_reg->reg_type.bitfield.xmmword)
9273 i.xstate |= xstate_xmm;
9274 }
014d61ea
JB
9275
9276 /* vzeroall / vzeroupper */
9277 if (i.tm.base_opcode == 0x77 && i.tm.cpu_flags.bitfield.cpuavx)
9278 i.xstate |= xstate_ymm;
9279
c4694f17 9280 if ((i.xstate & xstate_xmm)
389d00a5
JB
9281 /* ldmxcsr / stmxcsr / vldmxcsr / vstmxcsr */
9282 || (i.tm.base_opcode == 0xae
9283 && (i.tm.cpu_flags.bitfield.cpusse
9284 || i.tm.cpu_flags.bitfield.cpuavx))
c4694f17
TG
9285 || i.tm.cpu_flags.bitfield.cpuwidekl
9286 || i.tm.cpu_flags.bitfield.cpukl)
b4a3a7b4 9287 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XMM;
014d61ea 9288
921eafea 9289 if ((i.xstate & xstate_ymm) == xstate_ymm)
b4a3a7b4 9290 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_YMM;
921eafea 9291 if ((i.xstate & xstate_zmm) == xstate_zmm)
b4a3a7b4 9292 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_ZMM;
6225c532 9293 if (i.mask.reg || (i.xstate & xstate_mask) == xstate_mask)
32930e4e 9294 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MASK;
b4a3a7b4
L
9295 if (i.tm.cpu_flags.bitfield.cpufxsr)
9296 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_FXSR;
9297 if (i.tm.cpu_flags.bitfield.cpuxsave)
9298 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVE;
9299 if (i.tm.cpu_flags.bitfield.cpuxsaveopt)
9300 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT;
9301 if (i.tm.cpu_flags.bitfield.cpuxsavec)
9302 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEC;
b0ab0693
L
9303
9304 if (x86_feature_2_used
9305 || i.tm.cpu_flags.bitfield.cpucmov
9306 || i.tm.cpu_flags.bitfield.cpusyscall
389d00a5
JB
9307 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
9308 && i.tm.base_opcode == 0xc7
70e95837 9309 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
b0ab0693
L
9310 && i.tm.extension_opcode == 1) /* cmpxchg8b */)
9311 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_BASELINE;
9312 if (i.tm.cpu_flags.bitfield.cpusse3
9313 || i.tm.cpu_flags.bitfield.cpussse3
9314 || i.tm.cpu_flags.bitfield.cpusse4_1
9315 || i.tm.cpu_flags.bitfield.cpusse4_2
9316 || i.tm.cpu_flags.bitfield.cpucx16
9317 || i.tm.cpu_flags.bitfield.cpupopcnt
9318 /* LAHF-SAHF insns in 64-bit mode. */
9319 || (flag_code == CODE_64BIT
35648716
JB
9320 && (i.tm.base_opcode | 1) == 0x9f
9321 && i.tm.opcode_modifier.opcodespace == SPACE_BASE))
b0ab0693
L
9322 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V2;
9323 if (i.tm.cpu_flags.bitfield.cpuavx
9324 || i.tm.cpu_flags.bitfield.cpuavx2
9325 /* Any VEX encoded insns execpt for CpuAVX512F, CpuAVX512BW,
9326 CpuAVX512DQ, LPW, TBM and AMX. */
9327 || (i.tm.opcode_modifier.vex
9328 && !i.tm.cpu_flags.bitfield.cpuavx512f
9329 && !i.tm.cpu_flags.bitfield.cpuavx512bw
9330 && !i.tm.cpu_flags.bitfield.cpuavx512dq
9331 && !i.tm.cpu_flags.bitfield.cpulwp
9332 && !i.tm.cpu_flags.bitfield.cputbm
9333 && !(x86_feature_2_used & GNU_PROPERTY_X86_FEATURE_2_TMM))
9334 || i.tm.cpu_flags.bitfield.cpuf16c
9335 || i.tm.cpu_flags.bitfield.cpufma
9336 || i.tm.cpu_flags.bitfield.cpulzcnt
9337 || i.tm.cpu_flags.bitfield.cpumovbe
9338 || i.tm.cpu_flags.bitfield.cpuxsaves
9339 || (x86_feature_2_used
9340 & (GNU_PROPERTY_X86_FEATURE_2_XSAVE
9341 | GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT
9342 | GNU_PROPERTY_X86_FEATURE_2_XSAVEC)) != 0)
9343 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V3;
9344 if (i.tm.cpu_flags.bitfield.cpuavx512f
9345 || i.tm.cpu_flags.bitfield.cpuavx512bw
9346 || i.tm.cpu_flags.bitfield.cpuavx512dq
9347 || i.tm.cpu_flags.bitfield.cpuavx512vl
9348 /* Any EVEX encoded insns except for AVX512ER, AVX512PF and
9349 VNNIW. */
9350 || (i.tm.opcode_modifier.evex
9351 && !i.tm.cpu_flags.bitfield.cpuavx512er
9352 && !i.tm.cpu_flags.bitfield.cpuavx512pf
9353 && !i.tm.cpu_flags.bitfield.cpuavx512_4vnniw))
9354 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V4;
b4a3a7b4
L
9355 }
9356#endif
9357
29b0f896
AM
9358 /* Tie dwarf2 debug info to the address at the start of the insn.
9359 We can't do this after the insn has been output as the current
9360 frag may have been closed off. eg. by frag_var. */
9361 dwarf2_emit_insn (0);
9362
2bbd9c25
JJ
9363 insn_start_frag = frag_now;
9364 insn_start_off = frag_now_fix ();
9365
79d72f45 9366 if (add_branch_padding_frag_p (&branch, &mf_jcc))
e379e5f3
L
9367 {
9368 char *p;
9369 /* Branch can be 8 bytes. Leave some room for prefixes. */
9370 unsigned int max_branch_padding_size = 14;
9371
9372 /* Align section to boundary. */
9373 record_alignment (now_seg, align_branch_power);
9374
9375 /* Make room for padding. */
9376 frag_grow (max_branch_padding_size);
9377
9378 /* Start of the padding. */
9379 p = frag_more (0);
9380
9381 fragP = frag_now;
9382
9383 frag_var (rs_machine_dependent, max_branch_padding_size, 0,
9384 ENCODE_RELAX_STATE (BRANCH_PADDING, 0),
9385 NULL, 0, p);
9386
79d72f45 9387 fragP->tc_frag_data.mf_type = mf_jcc;
e379e5f3
L
9388 fragP->tc_frag_data.branch_type = branch;
9389 fragP->tc_frag_data.max_bytes = max_branch_padding_size;
9390 }
9391
29b0f896 9392 /* Output jumps. */
0cfa3eb3 9393 if (i.tm.opcode_modifier.jump == JUMP)
29b0f896 9394 output_branch ();
0cfa3eb3
JB
9395 else if (i.tm.opcode_modifier.jump == JUMP_BYTE
9396 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896 9397 output_jump ();
0cfa3eb3 9398 else if (i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT)
29b0f896
AM
9399 output_interseg_jump ();
9400 else
9401 {
9402 /* Output normal instructions here. */
9403 char *p;
9404 unsigned char *q;
47465058 9405 unsigned int j;
79d72f45 9406 enum mf_cmp_kind mf_cmp;
4dffcebc 9407
e4e00185 9408 if (avoid_fence
389d00a5
JB
9409 && (i.tm.base_opcode == 0xaee8
9410 || i.tm.base_opcode == 0xaef0
9411 || i.tm.base_opcode == 0xaef8))
48ef937e
JB
9412 {
9413 /* Encode lfence, mfence, and sfence as
9414 f0 83 04 24 00 lock addl $0x0, (%{re}sp). */
9415 if (now_seg != absolute_section)
9416 {
9417 offsetT val = 0x240483f0ULL;
9418
9419 p = frag_more (5);
9420 md_number_to_chars (p, val, 5);
9421 }
9422 else
9423 abs_section_offset += 5;
9424 return;
9425 }
e4e00185 9426
d022bddd
IT
9427 /* Some processors fail on LOCK prefix. This options makes
9428 assembler ignore LOCK prefix and serves as a workaround. */
9429 if (omit_lock_prefix)
9430 {
35648716
JB
9431 if (i.tm.base_opcode == LOCK_PREFIX_OPCODE
9432 && i.tm.opcode_modifier.isprefix)
d022bddd
IT
9433 return;
9434 i.prefix[LOCK_PREFIX] = 0;
9435 }
9436
e379e5f3
L
9437 if (branch)
9438 /* Skip if this is a branch. */
9439 ;
79d72f45 9440 else if (add_fused_jcc_padding_frag_p (&mf_cmp))
e379e5f3
L
9441 {
9442 /* Make room for padding. */
9443 frag_grow (MAX_FUSED_JCC_PADDING_SIZE);
9444 p = frag_more (0);
9445
9446 fragP = frag_now;
9447
9448 frag_var (rs_machine_dependent, MAX_FUSED_JCC_PADDING_SIZE, 0,
9449 ENCODE_RELAX_STATE (FUSED_JCC_PADDING, 0),
9450 NULL, 0, p);
9451
79d72f45 9452 fragP->tc_frag_data.mf_type = mf_cmp;
e379e5f3
L
9453 fragP->tc_frag_data.branch_type = align_branch_fused;
9454 fragP->tc_frag_data.max_bytes = MAX_FUSED_JCC_PADDING_SIZE;
9455 }
9456 else if (add_branch_prefix_frag_p ())
9457 {
9458 unsigned int max_prefix_size = align_branch_prefix_size;
9459
9460 /* Make room for padding. */
9461 frag_grow (max_prefix_size);
9462 p = frag_more (0);
9463
9464 fragP = frag_now;
9465
9466 frag_var (rs_machine_dependent, max_prefix_size, 0,
9467 ENCODE_RELAX_STATE (BRANCH_PREFIX, 0),
9468 NULL, 0, p);
9469
9470 fragP->tc_frag_data.max_bytes = max_prefix_size;
9471 }
9472
43234a1e
L
9473 /* Since the VEX/EVEX prefix contains the implicit prefix, we
9474 don't need the explicit prefix. */
9475 if (!i.tm.opcode_modifier.vex && !i.tm.opcode_modifier.evex)
bc4bd9ab 9476 {
7b47a312 9477 switch (i.tm.opcode_modifier.opcodeprefix)
bc4bd9ab 9478 {
7b47a312
L
9479 case PREFIX_0X66:
9480 add_prefix (0x66);
9481 break;
9482 case PREFIX_0XF2:
9483 add_prefix (0xf2);
9484 break;
9485 case PREFIX_0XF3:
8b65b895
L
9486 if (!i.tm.cpu_flags.bitfield.cpupadlock
9487 || (i.prefix[REP_PREFIX] != 0xf3))
9488 add_prefix (0xf3);
c0f3af97 9489 break;
7b47a312 9490 case PREFIX_NONE:
9a182d04 9491 switch (i.opcode_length)
c0f3af97 9492 {
7b47a312 9493 case 2:
7b47a312 9494 break;
9a182d04 9495 case 1:
7b47a312 9496 /* Check for pseudo prefixes. */
9a182d04
JB
9497 if (!i.tm.opcode_modifier.isprefix || i.tm.base_opcode)
9498 break;
7b47a312
L
9499 as_bad_where (insn_start_frag->fr_file,
9500 insn_start_frag->fr_line,
9501 _("pseudo prefix without instruction"));
9502 return;
9503 default:
9504 abort ();
4dffcebc 9505 }
c0f3af97 9506 break;
c0f3af97
L
9507 default:
9508 abort ();
bc4bd9ab 9509 }
c0f3af97 9510
6d19a37a 9511#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
cf61b747
L
9512 /* For x32, add a dummy REX_OPCODE prefix for mov/add with
9513 R_X86_64_GOTTPOFF relocation so that linker can safely
14470f07
L
9514 perform IE->LE optimization. A dummy REX_OPCODE prefix
9515 is also needed for lea with R_X86_64_GOTPC32_TLSDESC
9516 relocation for GDesc -> IE/LE optimization. */
cf61b747
L
9517 if (x86_elf_abi == X86_64_X32_ABI
9518 && i.operands == 2
14470f07
L
9519 && (i.reloc[0] == BFD_RELOC_X86_64_GOTTPOFF
9520 || i.reloc[0] == BFD_RELOC_X86_64_GOTPC32_TLSDESC)
cf61b747
L
9521 && i.prefix[REX_PREFIX] == 0)
9522 add_prefix (REX_OPCODE);
6d19a37a 9523#endif
cf61b747 9524
c0f3af97
L
9525 /* The prefix bytes. */
9526 for (j = ARRAY_SIZE (i.prefix), q = i.prefix; j > 0; j--, q++)
9527 if (*q)
48ef937e 9528 frag_opcode_byte (*q);
0f10071e 9529 }
ae5c1c7b 9530 else
c0f3af97
L
9531 {
9532 for (j = 0, q = i.prefix; j < ARRAY_SIZE (i.prefix); j++, q++)
9533 if (*q)
9534 switch (j)
9535 {
c0f3af97
L
9536 case SEG_PREFIX:
9537 case ADDR_PREFIX:
48ef937e 9538 frag_opcode_byte (*q);
c0f3af97
L
9539 break;
9540 default:
9541 /* There should be no other prefixes for instructions
9542 with VEX prefix. */
9543 abort ();
9544 }
9545
43234a1e
L
9546 /* For EVEX instructions i.vrex should become 0 after
9547 build_evex_prefix. For VEX instructions upper 16 registers
9548 aren't available, so VREX should be 0. */
9549 if (i.vrex)
9550 abort ();
c0f3af97 9551 /* Now the VEX prefix. */
48ef937e
JB
9552 if (now_seg != absolute_section)
9553 {
9554 p = frag_more (i.vex.length);
9555 for (j = 0; j < i.vex.length; j++)
9556 p[j] = i.vex.bytes[j];
9557 }
9558 else
9559 abs_section_offset += i.vex.length;
c0f3af97 9560 }
252b5132 9561
29b0f896 9562 /* Now the opcode; be careful about word order here! */
389d00a5
JB
9563 j = i.opcode_length;
9564 if (!i.vex.length)
9565 switch (i.tm.opcode_modifier.opcodespace)
9566 {
9567 case SPACE_BASE:
9568 break;
9569 case SPACE_0F:
9570 ++j;
9571 break;
9572 case SPACE_0F38:
9573 case SPACE_0F3A:
9574 j += 2;
9575 break;
9576 default:
9577 abort ();
9578 }
9579
48ef937e 9580 if (now_seg == absolute_section)
389d00a5
JB
9581 abs_section_offset += j;
9582 else if (j == 1)
29b0f896
AM
9583 {
9584 FRAG_APPEND_1_CHAR (i.tm.base_opcode);
9585 }
9586 else
9587 {
389d00a5
JB
9588 p = frag_more (j);
9589 if (!i.vex.length
9590 && i.tm.opcode_modifier.opcodespace != SPACE_BASE)
9591 {
9592 *p++ = 0x0f;
9593 if (i.tm.opcode_modifier.opcodespace != SPACE_0F)
9594 *p++ = i.tm.opcode_modifier.opcodespace == SPACE_0F38
9595 ? 0x38 : 0x3a;
9596 }
9597
9a182d04 9598 switch (i.opcode_length)
331d2d0d 9599 {
4dffcebc 9600 case 2:
389d00a5
JB
9601 /* Put out high byte first: can't use md_number_to_chars! */
9602 *p++ = (i.tm.base_opcode >> 8) & 0xff;
9603 /* Fall through. */
9604 case 1:
9605 *p = i.tm.base_opcode & 0xff;
4dffcebc
L
9606 break;
9607 default:
9608 abort ();
9609 break;
331d2d0d 9610 }
0f10071e 9611
29b0f896 9612 }
3e73aa7c 9613
29b0f896 9614 /* Now the modrm byte and sib byte (if present). */
40fb9820 9615 if (i.tm.opcode_modifier.modrm)
29b0f896 9616 {
48ef937e
JB
9617 frag_opcode_byte ((i.rm.regmem << 0)
9618 | (i.rm.reg << 3)
9619 | (i.rm.mode << 6));
29b0f896
AM
9620 /* If i.rm.regmem == ESP (4)
9621 && i.rm.mode != (Register mode)
9622 && not 16 bit
9623 ==> need second modrm byte. */
9624 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
9625 && i.rm.mode != 3
dc821c5f 9626 && !(i.base_reg && i.base_reg->reg_type.bitfield.word))
48ef937e
JB
9627 frag_opcode_byte ((i.sib.base << 0)
9628 | (i.sib.index << 3)
9629 | (i.sib.scale << 6));
29b0f896 9630 }
3e73aa7c 9631
29b0f896 9632 if (i.disp_operands)
2bbd9c25 9633 output_disp (insn_start_frag, insn_start_off);
3e73aa7c 9634
29b0f896 9635 if (i.imm_operands)
2bbd9c25 9636 output_imm (insn_start_frag, insn_start_off);
9c33702b
JB
9637
9638 /*
9639 * frag_now_fix () returning plain abs_section_offset when we're in the
9640 * absolute section, and abs_section_offset not getting updated as data
9641 * gets added to the frag breaks the logic below.
9642 */
9643 if (now_seg != absolute_section)
9644 {
9645 j = encoding_length (insn_start_frag, insn_start_off, frag_more (0));
9646 if (j > 15)
9647 as_warn (_("instruction length of %u bytes exceeds the limit of 15"),
9648 j);
e379e5f3
L
9649 else if (fragP)
9650 {
9651 /* NB: Don't add prefix with GOTPC relocation since
9652 output_disp() above depends on the fixed encoding
9653 length. Can't add prefix with TLS relocation since
9654 it breaks TLS linker optimization. */
9655 unsigned int max = i.has_gotpc_tls_reloc ? 0 : 15 - j;
9656 /* Prefix count on the current instruction. */
9657 unsigned int count = i.vex.length;
9658 unsigned int k;
9659 for (k = 0; k < ARRAY_SIZE (i.prefix); k++)
9660 /* REX byte is encoded in VEX/EVEX prefix. */
9661 if (i.prefix[k] && (k != REX_PREFIX || !i.vex.length))
9662 count++;
9663
9664 /* Count prefixes for extended opcode maps. */
9665 if (!i.vex.length)
389d00a5 9666 switch (i.tm.opcode_modifier.opcodespace)
e379e5f3 9667 {
389d00a5 9668 case SPACE_BASE:
e379e5f3 9669 break;
389d00a5
JB
9670 case SPACE_0F:
9671 count++;
e379e5f3 9672 break;
389d00a5
JB
9673 case SPACE_0F38:
9674 case SPACE_0F3A:
9675 count += 2;
e379e5f3
L
9676 break;
9677 default:
9678 abort ();
9679 }
9680
9681 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
9682 == BRANCH_PREFIX)
9683 {
9684 /* Set the maximum prefix size in BRANCH_PREFIX
9685 frag. */
9686 if (fragP->tc_frag_data.max_bytes > max)
9687 fragP->tc_frag_data.max_bytes = max;
9688 if (fragP->tc_frag_data.max_bytes > count)
9689 fragP->tc_frag_data.max_bytes -= count;
9690 else
9691 fragP->tc_frag_data.max_bytes = 0;
9692 }
9693 else
9694 {
9695 /* Remember the maximum prefix size in FUSED_JCC_PADDING
9696 frag. */
9697 unsigned int max_prefix_size;
9698 if (align_branch_prefix_size > max)
9699 max_prefix_size = max;
9700 else
9701 max_prefix_size = align_branch_prefix_size;
9702 if (max_prefix_size > count)
9703 fragP->tc_frag_data.max_prefix_length
9704 = max_prefix_size - count;
9705 }
9706
9707 /* Use existing segment prefix if possible. Use CS
9708 segment prefix in 64-bit mode. In 32-bit mode, use SS
9709 segment prefix with ESP/EBP base register and use DS
9710 segment prefix without ESP/EBP base register. */
9711 if (i.prefix[SEG_PREFIX])
9712 fragP->tc_frag_data.default_prefix = i.prefix[SEG_PREFIX];
9713 else if (flag_code == CODE_64BIT)
9714 fragP->tc_frag_data.default_prefix = CS_PREFIX_OPCODE;
9715 else if (i.base_reg
9716 && (i.base_reg->reg_num == 4
9717 || i.base_reg->reg_num == 5))
9718 fragP->tc_frag_data.default_prefix = SS_PREFIX_OPCODE;
9719 else
9720 fragP->tc_frag_data.default_prefix = DS_PREFIX_OPCODE;
9721 }
9c33702b 9722 }
29b0f896 9723 }
252b5132 9724
e379e5f3
L
9725 /* NB: Don't work with COND_JUMP86 without i386. */
9726 if (align_branch_power
9727 && now_seg != absolute_section
9728 && cpu_arch_flags.bitfield.cpui386)
9729 {
9730 /* Terminate each frag so that we can add prefix and check for
9731 fused jcc. */
9732 frag_wane (frag_now);
9733 frag_new (0);
9734 }
9735
29b0f896
AM
9736#ifdef DEBUG386
9737 if (flag_debug)
9738 {
7b81dfbb 9739 pi ("" /*line*/, &i);
29b0f896
AM
9740 }
9741#endif /* DEBUG386 */
9742}
252b5132 9743
e205caa7
L
9744/* Return the size of the displacement operand N. */
9745
9746static int
9747disp_size (unsigned int n)
9748{
9749 int size = 4;
43234a1e 9750
b5014f7a 9751 if (i.types[n].bitfield.disp64)
40fb9820
L
9752 size = 8;
9753 else if (i.types[n].bitfield.disp8)
9754 size = 1;
9755 else if (i.types[n].bitfield.disp16)
9756 size = 2;
e205caa7
L
9757 return size;
9758}
9759
9760/* Return the size of the immediate operand N. */
9761
9762static int
9763imm_size (unsigned int n)
9764{
9765 int size = 4;
40fb9820
L
9766 if (i.types[n].bitfield.imm64)
9767 size = 8;
9768 else if (i.types[n].bitfield.imm8 || i.types[n].bitfield.imm8s)
9769 size = 1;
9770 else if (i.types[n].bitfield.imm16)
9771 size = 2;
e205caa7
L
9772 return size;
9773}
9774
29b0f896 9775static void
64e74474 9776output_disp (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
9777{
9778 char *p;
9779 unsigned int n;
252b5132 9780
29b0f896
AM
9781 for (n = 0; n < i.operands; n++)
9782 {
b5014f7a 9783 if (operand_type_check (i.types[n], disp))
29b0f896 9784 {
48ef937e
JB
9785 int size = disp_size (n);
9786
9787 if (now_seg == absolute_section)
9788 abs_section_offset += size;
9789 else if (i.op[n].disps->X_op == O_constant)
29b0f896 9790 {
43234a1e 9791 offsetT val = i.op[n].disps->X_add_number;
252b5132 9792
629cfaf1
JB
9793 val = offset_in_range (val >> (size == 1 ? i.memshift : 0),
9794 size);
29b0f896
AM
9795 p = frag_more (size);
9796 md_number_to_chars (p, val, size);
9797 }
9798 else
9799 {
f86103b7 9800 enum bfd_reloc_code_real reloc_type;
40fb9820 9801 int sign = i.types[n].bitfield.disp32s;
29b0f896 9802 int pcrel = (i.flags[n] & Operand_PCrel) != 0;
02a86693 9803 fixS *fixP;
29b0f896 9804
e205caa7 9805 /* We can't have 8 bit displacement here. */
9c2799c2 9806 gas_assert (!i.types[n].bitfield.disp8);
e205caa7 9807
29b0f896
AM
9808 /* The PC relative address is computed relative
9809 to the instruction boundary, so in case immediate
9810 fields follows, we need to adjust the value. */
9811 if (pcrel && i.imm_operands)
9812 {
29b0f896 9813 unsigned int n1;
e205caa7 9814 int sz = 0;
252b5132 9815
29b0f896 9816 for (n1 = 0; n1 < i.operands; n1++)
40fb9820 9817 if (operand_type_check (i.types[n1], imm))
252b5132 9818 {
e205caa7
L
9819 /* Only one immediate is allowed for PC
9820 relative address. */
9c2799c2 9821 gas_assert (sz == 0);
e205caa7
L
9822 sz = imm_size (n1);
9823 i.op[n].disps->X_add_number -= sz;
252b5132 9824 }
29b0f896 9825 /* We should find the immediate. */
9c2799c2 9826 gas_assert (sz != 0);
29b0f896 9827 }
520dc8e8 9828
29b0f896 9829 p = frag_more (size);
d258b828 9830 reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
d6ab8113 9831 if (GOT_symbol
2bbd9c25 9832 && GOT_symbol == i.op[n].disps->X_add_symbol
d6ab8113 9833 && (((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
9834 || reloc_type == BFD_RELOC_X86_64_32S
9835 || (reloc_type == BFD_RELOC_64
9836 && object_64bit))
d6ab8113
JB
9837 && (i.op[n].disps->X_op == O_symbol
9838 || (i.op[n].disps->X_op == O_add
9839 && ((symbol_get_value_expression
9840 (i.op[n].disps->X_op_symbol)->X_op)
9841 == O_subtract))))
9842 || reloc_type == BFD_RELOC_32_PCREL))
2bbd9c25 9843 {
4fa24527 9844 if (!object_64bit)
7b81dfbb
AJ
9845 {
9846 reloc_type = BFD_RELOC_386_GOTPC;
5b7c81bd 9847 i.has_gotpc_tls_reloc = true;
d583596c
JB
9848 i.op[n].imms->X_add_number +=
9849 encoding_length (insn_start_frag, insn_start_off, p);
7b81dfbb
AJ
9850 }
9851 else if (reloc_type == BFD_RELOC_64)
9852 reloc_type = BFD_RELOC_X86_64_GOTPC64;
d6ab8113 9853 else
7b81dfbb
AJ
9854 /* Don't do the adjustment for x86-64, as there
9855 the pcrel addressing is relative to the _next_
9856 insn, and that is taken care of in other code. */
d6ab8113 9857 reloc_type = BFD_RELOC_X86_64_GOTPC32;
2bbd9c25 9858 }
e379e5f3
L
9859 else if (align_branch_power)
9860 {
9861 switch (reloc_type)
9862 {
9863 case BFD_RELOC_386_TLS_GD:
9864 case BFD_RELOC_386_TLS_LDM:
9865 case BFD_RELOC_386_TLS_IE:
9866 case BFD_RELOC_386_TLS_IE_32:
9867 case BFD_RELOC_386_TLS_GOTIE:
9868 case BFD_RELOC_386_TLS_GOTDESC:
9869 case BFD_RELOC_386_TLS_DESC_CALL:
9870 case BFD_RELOC_X86_64_TLSGD:
9871 case BFD_RELOC_X86_64_TLSLD:
9872 case BFD_RELOC_X86_64_GOTTPOFF:
9873 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
9874 case BFD_RELOC_X86_64_TLSDESC_CALL:
5b7c81bd 9875 i.has_gotpc_tls_reloc = true;
e379e5f3
L
9876 default:
9877 break;
9878 }
9879 }
02a86693
L
9880 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal,
9881 size, i.op[n].disps, pcrel,
9882 reloc_type);
9883 /* Check for "call/jmp *mem", "mov mem, %reg",
9884 "test %reg, mem" and "binop mem, %reg" where binop
9885 is one of adc, add, and, cmp, or, sbb, sub, xor
e60f4d3b
L
9886 instructions without data prefix. Always generate
9887 R_386_GOT32X for "sym*GOT" operand in 32-bit mode. */
9888 if (i.prefix[DATA_PREFIX] == 0
9889 && (generate_relax_relocations
9890 || (!object_64bit
9891 && i.rm.mode == 0
9892 && i.rm.regmem == 5))
0cb4071e
L
9893 && (i.rm.mode == 2
9894 || (i.rm.mode == 0 && i.rm.regmem == 5))
389d00a5 9895 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
02a86693
L
9896 && ((i.operands == 1
9897 && i.tm.base_opcode == 0xff
9898 && (i.rm.reg == 2 || i.rm.reg == 4))
9899 || (i.operands == 2
9900 && (i.tm.base_opcode == 0x8b
9901 || i.tm.base_opcode == 0x85
2ae4c703 9902 || (i.tm.base_opcode & ~0x38) == 0x03))))
02a86693
L
9903 {
9904 if (object_64bit)
9905 {
9906 fixP->fx_tcbit = i.rex != 0;
9907 if (i.base_reg
e968fc9b 9908 && (i.base_reg->reg_num == RegIP))
02a86693
L
9909 fixP->fx_tcbit2 = 1;
9910 }
9911 else
9912 fixP->fx_tcbit2 = 1;
9913 }
29b0f896
AM
9914 }
9915 }
9916 }
9917}
252b5132 9918
29b0f896 9919static void
64e74474 9920output_imm (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
9921{
9922 char *p;
9923 unsigned int n;
252b5132 9924
29b0f896
AM
9925 for (n = 0; n < i.operands; n++)
9926 {
43234a1e 9927 /* Skip SAE/RC Imm operand in EVEX. They are already handled. */
ca5312a2 9928 if (i.rounding.type != rc_none && n == i.rounding.operand)
43234a1e
L
9929 continue;
9930
40fb9820 9931 if (operand_type_check (i.types[n], imm))
29b0f896 9932 {
48ef937e
JB
9933 int size = imm_size (n);
9934
9935 if (now_seg == absolute_section)
9936 abs_section_offset += size;
9937 else if (i.op[n].imms->X_op == O_constant)
29b0f896 9938 {
29b0f896 9939 offsetT val;
b4cac588 9940
29b0f896
AM
9941 val = offset_in_range (i.op[n].imms->X_add_number,
9942 size);
9943 p = frag_more (size);
9944 md_number_to_chars (p, val, size);
9945 }
9946 else
9947 {
9948 /* Not absolute_section.
9949 Need a 32-bit fixup (don't support 8bit
9950 non-absolute imms). Try to support other
9951 sizes ... */
f86103b7 9952 enum bfd_reloc_code_real reloc_type;
e205caa7 9953 int sign;
29b0f896 9954
40fb9820 9955 if (i.types[n].bitfield.imm32s
a7d61044 9956 && (i.suffix == QWORD_MNEM_SUFFIX
40fb9820 9957 || (!i.suffix && i.tm.opcode_modifier.no_lsuf)))
29b0f896 9958 sign = 1;
e205caa7
L
9959 else
9960 sign = 0;
520dc8e8 9961
29b0f896 9962 p = frag_more (size);
d258b828 9963 reloc_type = reloc (size, 0, sign, i.reloc[n]);
f86103b7 9964
2bbd9c25
JJ
9965 /* This is tough to explain. We end up with this one if we
9966 * have operands that look like
9967 * "_GLOBAL_OFFSET_TABLE_+[.-.L284]". The goal here is to
9968 * obtain the absolute address of the GOT, and it is strongly
9969 * preferable from a performance point of view to avoid using
9970 * a runtime relocation for this. The actual sequence of
9971 * instructions often look something like:
9972 *
9973 * call .L66
9974 * .L66:
9975 * popl %ebx
9976 * addl $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
9977 *
9978 * The call and pop essentially return the absolute address
9979 * of the label .L66 and store it in %ebx. The linker itself
9980 * will ultimately change the first operand of the addl so
9981 * that %ebx points to the GOT, but to keep things simple, the
9982 * .o file must have this operand set so that it generates not
9983 * the absolute address of .L66, but the absolute address of
9984 * itself. This allows the linker itself simply treat a GOTPC
9985 * relocation as asking for a pcrel offset to the GOT to be
9986 * added in, and the addend of the relocation is stored in the
9987 * operand field for the instruction itself.
9988 *
9989 * Our job here is to fix the operand so that it would add
9990 * the correct offset so that %ebx would point to itself. The
9991 * thing that is tricky is that .-.L66 will point to the
9992 * beginning of the instruction, so we need to further modify
9993 * the operand so that it will point to itself. There are
9994 * other cases where you have something like:
9995 *
9996 * .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
9997 *
9998 * and here no correction would be required. Internally in
9999 * the assembler we treat operands of this form as not being
10000 * pcrel since the '.' is explicitly mentioned, and I wonder
10001 * whether it would simplify matters to do it this way. Who
10002 * knows. In earlier versions of the PIC patches, the
10003 * pcrel_adjust field was used to store the correction, but
10004 * since the expression is not pcrel, I felt it would be
10005 * confusing to do it this way. */
10006
d6ab8113 10007 if ((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
10008 || reloc_type == BFD_RELOC_X86_64_32S
10009 || reloc_type == BFD_RELOC_64)
29b0f896
AM
10010 && GOT_symbol
10011 && GOT_symbol == i.op[n].imms->X_add_symbol
10012 && (i.op[n].imms->X_op == O_symbol
10013 || (i.op[n].imms->X_op == O_add
10014 && ((symbol_get_value_expression
10015 (i.op[n].imms->X_op_symbol)->X_op)
10016 == O_subtract))))
10017 {
4fa24527 10018 if (!object_64bit)
d6ab8113 10019 reloc_type = BFD_RELOC_386_GOTPC;
7b81dfbb 10020 else if (size == 4)
d6ab8113 10021 reloc_type = BFD_RELOC_X86_64_GOTPC32;
7b81dfbb
AJ
10022 else if (size == 8)
10023 reloc_type = BFD_RELOC_X86_64_GOTPC64;
5b7c81bd 10024 i.has_gotpc_tls_reloc = true;
d583596c
JB
10025 i.op[n].imms->X_add_number +=
10026 encoding_length (insn_start_frag, insn_start_off, p);
29b0f896 10027 }
29b0f896
AM
10028 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
10029 i.op[n].imms, 0, reloc_type);
10030 }
10031 }
10032 }
252b5132
RH
10033}
10034\f
d182319b
JB
10035/* x86_cons_fix_new is called via the expression parsing code when a
10036 reloc is needed. We use this hook to get the correct .got reloc. */
d182319b
JB
10037static int cons_sign = -1;
10038
10039void
e3bb37b5 10040x86_cons_fix_new (fragS *frag, unsigned int off, unsigned int len,
62ebcb5c 10041 expressionS *exp, bfd_reloc_code_real_type r)
d182319b 10042{
d258b828 10043 r = reloc (len, 0, cons_sign, r);
d182319b
JB
10044
10045#ifdef TE_PE
10046 if (exp->X_op == O_secrel)
10047 {
10048 exp->X_op = O_symbol;
10049 r = BFD_RELOC_32_SECREL;
10050 }
10051#endif
10052
10053 fix_new_exp (frag, off, len, exp, 0, r);
10054}
10055
357d1bd8
L
10056/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
10057 purpose of the `.dc.a' internal pseudo-op. */
10058
10059int
10060x86_address_bytes (void)
10061{
10062 if ((stdoutput->arch_info->mach & bfd_mach_x64_32))
10063 return 4;
10064 return stdoutput->arch_info->bits_per_address / 8;
10065}
10066
d382c579
TG
10067#if !(defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined (OBJ_MACH_O)) \
10068 || defined (LEX_AT)
d258b828 10069# define lex_got(reloc, adjust, types) NULL
718ddfc0 10070#else
f3c180ae
AM
10071/* Parse operands of the form
10072 <symbol>@GOTOFF+<nnn>
10073 and similar .plt or .got references.
10074
10075 If we find one, set up the correct relocation in RELOC and copy the
10076 input string, minus the `@GOTOFF' into a malloc'd buffer for
10077 parsing by the calling routine. Return this buffer, and if ADJUST
10078 is non-null set it to the length of the string we removed from the
10079 input line. Otherwise return NULL. */
10080static char *
91d6fa6a 10081lex_got (enum bfd_reloc_code_real *rel,
64e74474 10082 int *adjust,
d258b828 10083 i386_operand_type *types)
f3c180ae 10084{
7b81dfbb
AJ
10085 /* Some of the relocations depend on the size of what field is to
10086 be relocated. But in our callers i386_immediate and i386_displacement
10087 we don't yet know the operand size (this will be set by insn
10088 matching). Hence we record the word32 relocation here,
10089 and adjust the reloc according to the real size in reloc(). */
f3c180ae
AM
10090 static const struct {
10091 const char *str;
cff8d58a 10092 int len;
4fa24527 10093 const enum bfd_reloc_code_real rel[2];
40fb9820 10094 const i386_operand_type types64;
5b7c81bd 10095 bool need_GOT_symbol;
f3c180ae 10096 } gotrel[] = {
8ce3d284 10097#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
10098 { STRING_COMMA_LEN ("SIZE"), { BFD_RELOC_SIZE32,
10099 BFD_RELOC_SIZE32 },
5b7c81bd 10100 OPERAND_TYPE_IMM32_64, false },
8ce3d284 10101#endif
cff8d58a
L
10102 { STRING_COMMA_LEN ("PLTOFF"), { _dummy_first_bfd_reloc_code_real,
10103 BFD_RELOC_X86_64_PLTOFF64 },
5b7c81bd 10104 OPERAND_TYPE_IMM64, true },
cff8d58a
L
10105 { STRING_COMMA_LEN ("PLT"), { BFD_RELOC_386_PLT32,
10106 BFD_RELOC_X86_64_PLT32 },
5b7c81bd 10107 OPERAND_TYPE_IMM32_32S_DISP32, false },
cff8d58a
L
10108 { STRING_COMMA_LEN ("GOTPLT"), { _dummy_first_bfd_reloc_code_real,
10109 BFD_RELOC_X86_64_GOTPLT64 },
5b7c81bd 10110 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
10111 { STRING_COMMA_LEN ("GOTOFF"), { BFD_RELOC_386_GOTOFF,
10112 BFD_RELOC_X86_64_GOTOFF64 },
5b7c81bd 10113 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
10114 { STRING_COMMA_LEN ("GOTPCREL"), { _dummy_first_bfd_reloc_code_real,
10115 BFD_RELOC_X86_64_GOTPCREL },
5b7c81bd 10116 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10117 { STRING_COMMA_LEN ("TLSGD"), { BFD_RELOC_386_TLS_GD,
10118 BFD_RELOC_X86_64_TLSGD },
5b7c81bd 10119 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10120 { STRING_COMMA_LEN ("TLSLDM"), { BFD_RELOC_386_TLS_LDM,
10121 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10122 OPERAND_TYPE_NONE, true },
cff8d58a
L
10123 { STRING_COMMA_LEN ("TLSLD"), { _dummy_first_bfd_reloc_code_real,
10124 BFD_RELOC_X86_64_TLSLD },
5b7c81bd 10125 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10126 { STRING_COMMA_LEN ("GOTTPOFF"), { BFD_RELOC_386_TLS_IE_32,
10127 BFD_RELOC_X86_64_GOTTPOFF },
5b7c81bd 10128 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10129 { STRING_COMMA_LEN ("TPOFF"), { BFD_RELOC_386_TLS_LE_32,
10130 BFD_RELOC_X86_64_TPOFF32 },
5b7c81bd 10131 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
10132 { STRING_COMMA_LEN ("NTPOFF"), { BFD_RELOC_386_TLS_LE,
10133 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10134 OPERAND_TYPE_NONE, true },
cff8d58a
L
10135 { STRING_COMMA_LEN ("DTPOFF"), { BFD_RELOC_386_TLS_LDO_32,
10136 BFD_RELOC_X86_64_DTPOFF32 },
5b7c81bd 10137 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
10138 { STRING_COMMA_LEN ("GOTNTPOFF"),{ BFD_RELOC_386_TLS_GOTIE,
10139 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10140 OPERAND_TYPE_NONE, true },
cff8d58a
L
10141 { STRING_COMMA_LEN ("INDNTPOFF"),{ BFD_RELOC_386_TLS_IE,
10142 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10143 OPERAND_TYPE_NONE, true },
cff8d58a
L
10144 { STRING_COMMA_LEN ("GOT"), { BFD_RELOC_386_GOT32,
10145 BFD_RELOC_X86_64_GOT32 },
5b7c81bd 10146 OPERAND_TYPE_IMM32_32S_64_DISP32, true },
cff8d58a
L
10147 { STRING_COMMA_LEN ("TLSDESC"), { BFD_RELOC_386_TLS_GOTDESC,
10148 BFD_RELOC_X86_64_GOTPC32_TLSDESC },
5b7c81bd 10149 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10150 { STRING_COMMA_LEN ("TLSCALL"), { BFD_RELOC_386_TLS_DESC_CALL,
10151 BFD_RELOC_X86_64_TLSDESC_CALL },
5b7c81bd 10152 OPERAND_TYPE_IMM32_32S_DISP32, true },
f3c180ae
AM
10153 };
10154 char *cp;
10155 unsigned int j;
10156
d382c579 10157#if defined (OBJ_MAYBE_ELF)
718ddfc0
JB
10158 if (!IS_ELF)
10159 return NULL;
d382c579 10160#endif
718ddfc0 10161
f3c180ae 10162 for (cp = input_line_pointer; *cp != '@'; cp++)
67c11a9b 10163 if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
f3c180ae
AM
10164 return NULL;
10165
47465058 10166 for (j = 0; j < ARRAY_SIZE (gotrel); j++)
f3c180ae 10167 {
cff8d58a 10168 int len = gotrel[j].len;
28f81592 10169 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
f3c180ae 10170 {
4fa24527 10171 if (gotrel[j].rel[object_64bit] != 0)
f3c180ae 10172 {
28f81592
AM
10173 int first, second;
10174 char *tmpbuf, *past_reloc;
f3c180ae 10175
91d6fa6a 10176 *rel = gotrel[j].rel[object_64bit];
f3c180ae 10177
3956db08
JB
10178 if (types)
10179 {
10180 if (flag_code != CODE_64BIT)
40fb9820
L
10181 {
10182 types->bitfield.imm32 = 1;
10183 types->bitfield.disp32 = 1;
10184 }
3956db08
JB
10185 else
10186 *types = gotrel[j].types64;
10187 }
10188
844bf810 10189 if (gotrel[j].need_GOT_symbol && GOT_symbol == NULL)
f3c180ae
AM
10190 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
10191
28f81592 10192 /* The length of the first part of our input line. */
f3c180ae 10193 first = cp - input_line_pointer;
28f81592
AM
10194
10195 /* The second part goes from after the reloc token until
67c11a9b 10196 (and including) an end_of_line char or comma. */
28f81592 10197 past_reloc = cp + 1 + len;
67c11a9b
AM
10198 cp = past_reloc;
10199 while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
10200 ++cp;
10201 second = cp + 1 - past_reloc;
28f81592
AM
10202
10203 /* Allocate and copy string. The trailing NUL shouldn't
10204 be necessary, but be safe. */
add39d23 10205 tmpbuf = XNEWVEC (char, first + second + 2);
f3c180ae 10206 memcpy (tmpbuf, input_line_pointer, first);
0787a12d
AM
10207 if (second != 0 && *past_reloc != ' ')
10208 /* Replace the relocation token with ' ', so that
10209 errors like foo@GOTOFF1 will be detected. */
10210 tmpbuf[first++] = ' ';
af89796a
L
10211 else
10212 /* Increment length by 1 if the relocation token is
10213 removed. */
10214 len++;
10215 if (adjust)
10216 *adjust = len;
0787a12d
AM
10217 memcpy (tmpbuf + first, past_reloc, second);
10218 tmpbuf[first + second] = '\0';
f3c180ae
AM
10219 return tmpbuf;
10220 }
10221
4fa24527
JB
10222 as_bad (_("@%s reloc is not supported with %d-bit output format"),
10223 gotrel[j].str, 1 << (5 + object_64bit));
f3c180ae
AM
10224 return NULL;
10225 }
10226 }
10227
10228 /* Might be a symbol version string. Don't as_bad here. */
10229 return NULL;
10230}
4e4f7c87 10231#endif
f3c180ae 10232
a988325c
NC
10233#ifdef TE_PE
10234#ifdef lex_got
10235#undef lex_got
10236#endif
10237/* Parse operands of the form
10238 <symbol>@SECREL32+<nnn>
10239
10240 If we find one, set up the correct relocation in RELOC and copy the
10241 input string, minus the `@SECREL32' into a malloc'd buffer for
10242 parsing by the calling routine. Return this buffer, and if ADJUST
10243 is non-null set it to the length of the string we removed from the
34bca508
L
10244 input line. Otherwise return NULL.
10245
a988325c
NC
10246 This function is copied from the ELF version above adjusted for PE targets. */
10247
10248static char *
10249lex_got (enum bfd_reloc_code_real *rel ATTRIBUTE_UNUSED,
10250 int *adjust ATTRIBUTE_UNUSED,
d258b828 10251 i386_operand_type *types)
a988325c
NC
10252{
10253 static const struct
10254 {
10255 const char *str;
10256 int len;
10257 const enum bfd_reloc_code_real rel[2];
10258 const i386_operand_type types64;
10259 }
10260 gotrel[] =
10261 {
10262 { STRING_COMMA_LEN ("SECREL32"), { BFD_RELOC_32_SECREL,
10263 BFD_RELOC_32_SECREL },
10264 OPERAND_TYPE_IMM32_32S_64_DISP32_64 },
10265 };
10266
10267 char *cp;
10268 unsigned j;
10269
10270 for (cp = input_line_pointer; *cp != '@'; cp++)
10271 if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
10272 return NULL;
10273
10274 for (j = 0; j < ARRAY_SIZE (gotrel); j++)
10275 {
10276 int len = gotrel[j].len;
10277
10278 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
10279 {
10280 if (gotrel[j].rel[object_64bit] != 0)
10281 {
10282 int first, second;
10283 char *tmpbuf, *past_reloc;
10284
10285 *rel = gotrel[j].rel[object_64bit];
10286 if (adjust)
10287 *adjust = len;
10288
10289 if (types)
10290 {
10291 if (flag_code != CODE_64BIT)
10292 {
10293 types->bitfield.imm32 = 1;
10294 types->bitfield.disp32 = 1;
10295 }
10296 else
10297 *types = gotrel[j].types64;
10298 }
10299
10300 /* The length of the first part of our input line. */
10301 first = cp - input_line_pointer;
10302
10303 /* The second part goes from after the reloc token until
10304 (and including) an end_of_line char or comma. */
10305 past_reloc = cp + 1 + len;
10306 cp = past_reloc;
10307 while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
10308 ++cp;
10309 second = cp + 1 - past_reloc;
10310
10311 /* Allocate and copy string. The trailing NUL shouldn't
10312 be necessary, but be safe. */
add39d23 10313 tmpbuf = XNEWVEC (char, first + second + 2);
a988325c
NC
10314 memcpy (tmpbuf, input_line_pointer, first);
10315 if (second != 0 && *past_reloc != ' ')
10316 /* Replace the relocation token with ' ', so that
10317 errors like foo@SECLREL321 will be detected. */
10318 tmpbuf[first++] = ' ';
10319 memcpy (tmpbuf + first, past_reloc, second);
10320 tmpbuf[first + second] = '\0';
10321 return tmpbuf;
10322 }
10323
10324 as_bad (_("@%s reloc is not supported with %d-bit output format"),
10325 gotrel[j].str, 1 << (5 + object_64bit));
10326 return NULL;
10327 }
10328 }
10329
10330 /* Might be a symbol version string. Don't as_bad here. */
10331 return NULL;
10332}
10333
10334#endif /* TE_PE */
10335
62ebcb5c 10336bfd_reloc_code_real_type
e3bb37b5 10337x86_cons (expressionS *exp, int size)
f3c180ae 10338{
62ebcb5c
AM
10339 bfd_reloc_code_real_type got_reloc = NO_RELOC;
10340
ee86248c
JB
10341 intel_syntax = -intel_syntax;
10342
3c7b9c2c 10343 exp->X_md = 0;
4fa24527 10344 if (size == 4 || (object_64bit && size == 8))
f3c180ae
AM
10345 {
10346 /* Handle @GOTOFF and the like in an expression. */
10347 char *save;
10348 char *gotfree_input_line;
4a57f2cf 10349 int adjust = 0;
f3c180ae
AM
10350
10351 save = input_line_pointer;
d258b828 10352 gotfree_input_line = lex_got (&got_reloc, &adjust, NULL);
f3c180ae
AM
10353 if (gotfree_input_line)
10354 input_line_pointer = gotfree_input_line;
10355
10356 expression (exp);
10357
10358 if (gotfree_input_line)
10359 {
10360 /* expression () has merrily parsed up to the end of line,
10361 or a comma - in the wrong buffer. Transfer how far
10362 input_line_pointer has moved to the right buffer. */
10363 input_line_pointer = (save
10364 + (input_line_pointer - gotfree_input_line)
10365 + adjust);
10366 free (gotfree_input_line);
3992d3b7
AM
10367 if (exp->X_op == O_constant
10368 || exp->X_op == O_absent
10369 || exp->X_op == O_illegal
0398aac5 10370 || exp->X_op == O_register
3992d3b7
AM
10371 || exp->X_op == O_big)
10372 {
10373 char c = *input_line_pointer;
10374 *input_line_pointer = 0;
10375 as_bad (_("missing or invalid expression `%s'"), save);
10376 *input_line_pointer = c;
10377 }
b9519cfe
L
10378 else if ((got_reloc == BFD_RELOC_386_PLT32
10379 || got_reloc == BFD_RELOC_X86_64_PLT32)
10380 && exp->X_op != O_symbol)
10381 {
10382 char c = *input_line_pointer;
10383 *input_line_pointer = 0;
10384 as_bad (_("invalid PLT expression `%s'"), save);
10385 *input_line_pointer = c;
10386 }
f3c180ae
AM
10387 }
10388 }
10389 else
10390 expression (exp);
ee86248c
JB
10391
10392 intel_syntax = -intel_syntax;
10393
10394 if (intel_syntax)
10395 i386_intel_simplify (exp);
62ebcb5c
AM
10396
10397 return got_reloc;
f3c180ae 10398}
f3c180ae 10399
9f32dd5b
L
10400static void
10401signed_cons (int size)
6482c264 10402{
d182319b
JB
10403 if (flag_code == CODE_64BIT)
10404 cons_sign = 1;
10405 cons (size);
10406 cons_sign = -1;
6482c264
NC
10407}
10408
d182319b 10409#ifdef TE_PE
6482c264 10410static void
7016a5d5 10411pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
6482c264
NC
10412{
10413 expressionS exp;
10414
10415 do
10416 {
10417 expression (&exp);
10418 if (exp.X_op == O_symbol)
10419 exp.X_op = O_secrel;
10420
10421 emit_expr (&exp, 4);
10422 }
10423 while (*input_line_pointer++ == ',');
10424
10425 input_line_pointer--;
10426 demand_empty_rest_of_line ();
10427}
6482c264
NC
10428#endif
10429
43234a1e
L
10430/* Handle Vector operations. */
10431
10432static char *
10433check_VecOperations (char *op_string, char *op_end)
10434{
10435 const reg_entry *mask;
10436 const char *saved;
10437 char *end_op;
10438
10439 while (*op_string
10440 && (op_end == NULL || op_string < op_end))
10441 {
10442 saved = op_string;
10443 if (*op_string == '{')
10444 {
10445 op_string++;
10446
10447 /* Check broadcasts. */
d34049e8 10448 if (startswith (op_string, "1to"))
43234a1e 10449 {
5273a3cd 10450 unsigned int bcst_type;
43234a1e 10451
5273a3cd 10452 if (i.broadcast.type)
43234a1e
L
10453 goto duplicated_vec_op;
10454
10455 op_string += 3;
10456 if (*op_string == '8')
8e6e0792 10457 bcst_type = 8;
b28d1bda 10458 else if (*op_string == '4')
8e6e0792 10459 bcst_type = 4;
b28d1bda 10460 else if (*op_string == '2')
8e6e0792 10461 bcst_type = 2;
43234a1e
L
10462 else if (*op_string == '1'
10463 && *(op_string+1) == '6')
10464 {
8e6e0792 10465 bcst_type = 16;
43234a1e
L
10466 op_string++;
10467 }
10468 else
10469 {
10470 as_bad (_("Unsupported broadcast: `%s'"), saved);
10471 return NULL;
10472 }
10473 op_string++;
10474
5273a3cd
JB
10475 i.broadcast.type = bcst_type;
10476 i.broadcast.operand = this_operand;
43234a1e
L
10477 }
10478 /* Check masking operation. */
10479 else if ((mask = parse_register (op_string, &end_op)) != NULL)
10480 {
8a6fb3f9
JB
10481 if (mask == &bad_reg)
10482 return NULL;
10483
43234a1e 10484 /* k0 can't be used for write mask. */
f74a6307 10485 if (mask->reg_type.bitfield.class != RegMask || !mask->reg_num)
43234a1e 10486 {
6d2cd6b2
JB
10487 as_bad (_("`%s%s' can't be used for write mask"),
10488 register_prefix, mask->reg_name);
43234a1e
L
10489 return NULL;
10490 }
10491
6225c532 10492 if (!i.mask.reg)
43234a1e 10493 {
6225c532
JB
10494 i.mask.reg = mask;
10495 i.mask.operand = this_operand;
43234a1e 10496 }
6225c532
JB
10497 else if (i.mask.reg->reg_num)
10498 goto duplicated_vec_op;
43234a1e
L
10499 else
10500 {
6225c532 10501 i.mask.reg = mask;
43234a1e
L
10502
10503 /* Only "{z}" is allowed here. No need to check
10504 zeroing mask explicitly. */
6225c532 10505 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
10506 {
10507 as_bad (_("invalid write mask `%s'"), saved);
10508 return NULL;
10509 }
10510 }
10511
10512 op_string = end_op;
10513 }
10514 /* Check zeroing-flag for masking operation. */
10515 else if (*op_string == 'z')
10516 {
6225c532 10517 if (!i.mask.reg)
43234a1e 10518 {
6225c532
JB
10519 i.mask.reg = reg_k0;
10520 i.mask.zeroing = 1;
10521 i.mask.operand = this_operand;
43234a1e
L
10522 }
10523 else
10524 {
6225c532 10525 if (i.mask.zeroing)
43234a1e
L
10526 {
10527 duplicated_vec_op:
10528 as_bad (_("duplicated `%s'"), saved);
10529 return NULL;
10530 }
10531
6225c532 10532 i.mask.zeroing = 1;
43234a1e
L
10533
10534 /* Only "{%k}" is allowed here. No need to check mask
10535 register explicitly. */
6225c532 10536 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
10537 {
10538 as_bad (_("invalid zeroing-masking `%s'"),
10539 saved);
10540 return NULL;
10541 }
10542 }
10543
10544 op_string++;
10545 }
10546 else
10547 goto unknown_vec_op;
10548
10549 if (*op_string != '}')
10550 {
10551 as_bad (_("missing `}' in `%s'"), saved);
10552 return NULL;
10553 }
10554 op_string++;
0ba3a731
L
10555
10556 /* Strip whitespace since the addition of pseudo prefixes
10557 changed how the scrubber treats '{'. */
10558 if (is_space_char (*op_string))
10559 ++op_string;
10560
43234a1e
L
10561 continue;
10562 }
10563 unknown_vec_op:
10564 /* We don't know this one. */
10565 as_bad (_("unknown vector operation: `%s'"), saved);
10566 return NULL;
10567 }
10568
6225c532 10569 if (i.mask.reg && i.mask.zeroing && !i.mask.reg->reg_num)
6d2cd6b2
JB
10570 {
10571 as_bad (_("zeroing-masking only allowed with write mask"));
10572 return NULL;
10573 }
10574
43234a1e
L
10575 return op_string;
10576}
10577
252b5132 10578static int
70e41ade 10579i386_immediate (char *imm_start)
252b5132
RH
10580{
10581 char *save_input_line_pointer;
f3c180ae 10582 char *gotfree_input_line;
252b5132 10583 segT exp_seg = 0;
47926f60 10584 expressionS *exp;
40fb9820
L
10585 i386_operand_type types;
10586
0dfbf9d7 10587 operand_type_set (&types, ~0);
252b5132
RH
10588
10589 if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
10590 {
31b2323c
L
10591 as_bad (_("at most %d immediate operands are allowed"),
10592 MAX_IMMEDIATE_OPERANDS);
252b5132
RH
10593 return 0;
10594 }
10595
10596 exp = &im_expressions[i.imm_operands++];
520dc8e8 10597 i.op[this_operand].imms = exp;
252b5132
RH
10598
10599 if (is_space_char (*imm_start))
10600 ++imm_start;
10601
10602 save_input_line_pointer = input_line_pointer;
10603 input_line_pointer = imm_start;
10604
d258b828 10605 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
10606 if (gotfree_input_line)
10607 input_line_pointer = gotfree_input_line;
252b5132
RH
10608
10609 exp_seg = expression (exp);
10610
83183c0c 10611 SKIP_WHITESPACE ();
43234a1e
L
10612
10613 /* Handle vector operations. */
10614 if (*input_line_pointer == '{')
10615 {
10616 input_line_pointer = check_VecOperations (input_line_pointer,
10617 NULL);
10618 if (input_line_pointer == NULL)
10619 return 0;
10620 }
10621
252b5132 10622 if (*input_line_pointer)
f3c180ae 10623 as_bad (_("junk `%s' after expression"), input_line_pointer);
252b5132
RH
10624
10625 input_line_pointer = save_input_line_pointer;
f3c180ae 10626 if (gotfree_input_line)
ee86248c
JB
10627 {
10628 free (gotfree_input_line);
10629
10630 if (exp->X_op == O_constant || exp->X_op == O_register)
10631 exp->X_op = O_illegal;
10632 }
10633
10634 return i386_finalize_immediate (exp_seg, exp, types, imm_start);
10635}
252b5132 10636
ee86248c
JB
10637static int
10638i386_finalize_immediate (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
10639 i386_operand_type types, const char *imm_start)
10640{
10641 if (exp->X_op == O_absent || exp->X_op == O_illegal || exp->X_op == O_big)
252b5132 10642 {
313c53d1
L
10643 if (imm_start)
10644 as_bad (_("missing or invalid immediate expression `%s'"),
10645 imm_start);
3992d3b7 10646 return 0;
252b5132 10647 }
3e73aa7c 10648 else if (exp->X_op == O_constant)
252b5132 10649 {
47926f60 10650 /* Size it properly later. */
40fb9820 10651 i.types[this_operand].bitfield.imm64 = 1;
13f864ae
L
10652 /* If not 64bit, sign extend val. */
10653 if (flag_code != CODE_64BIT
4eed87de
AM
10654 && (exp->X_add_number & ~(((addressT) 2 << 31) - 1)) == 0)
10655 exp->X_add_number
10656 = (exp->X_add_number ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
252b5132 10657 }
4c63da97 10658#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
f86103b7 10659 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
31312f95 10660 && exp_seg != absolute_section
47926f60 10661 && exp_seg != text_section
24eab124
AM
10662 && exp_seg != data_section
10663 && exp_seg != bss_section
10664 && exp_seg != undefined_section
f86103b7 10665 && !bfd_is_com_section (exp_seg))
252b5132 10666 {
d0b47220 10667 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
252b5132
RH
10668 return 0;
10669 }
10670#endif
a841bdf5 10671 else if (!intel_syntax && exp_seg == reg_section)
bb8f5920 10672 {
313c53d1
L
10673 if (imm_start)
10674 as_bad (_("illegal immediate register operand %s"), imm_start);
bb8f5920
L
10675 return 0;
10676 }
252b5132
RH
10677 else
10678 {
10679 /* This is an address. The size of the address will be
24eab124 10680 determined later, depending on destination register,
3e73aa7c 10681 suffix, or the default for the section. */
40fb9820
L
10682 i.types[this_operand].bitfield.imm8 = 1;
10683 i.types[this_operand].bitfield.imm16 = 1;
10684 i.types[this_operand].bitfield.imm32 = 1;
10685 i.types[this_operand].bitfield.imm32s = 1;
10686 i.types[this_operand].bitfield.imm64 = 1;
c6fb90c8
L
10687 i.types[this_operand] = operand_type_and (i.types[this_operand],
10688 types);
252b5132
RH
10689 }
10690
10691 return 1;
10692}
10693
551c1ca1 10694static char *
e3bb37b5 10695i386_scale (char *scale)
252b5132 10696{
551c1ca1
AM
10697 offsetT val;
10698 char *save = input_line_pointer;
252b5132 10699
551c1ca1
AM
10700 input_line_pointer = scale;
10701 val = get_absolute_expression ();
10702
10703 switch (val)
252b5132 10704 {
551c1ca1 10705 case 1:
252b5132
RH
10706 i.log2_scale_factor = 0;
10707 break;
551c1ca1 10708 case 2:
252b5132
RH
10709 i.log2_scale_factor = 1;
10710 break;
551c1ca1 10711 case 4:
252b5132
RH
10712 i.log2_scale_factor = 2;
10713 break;
551c1ca1 10714 case 8:
252b5132
RH
10715 i.log2_scale_factor = 3;
10716 break;
10717 default:
a724f0f4
JB
10718 {
10719 char sep = *input_line_pointer;
10720
10721 *input_line_pointer = '\0';
10722 as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
10723 scale);
10724 *input_line_pointer = sep;
10725 input_line_pointer = save;
10726 return NULL;
10727 }
252b5132 10728 }
29b0f896 10729 if (i.log2_scale_factor != 0 && i.index_reg == 0)
252b5132
RH
10730 {
10731 as_warn (_("scale factor of %d without an index register"),
24eab124 10732 1 << i.log2_scale_factor);
252b5132 10733 i.log2_scale_factor = 0;
252b5132 10734 }
551c1ca1
AM
10735 scale = input_line_pointer;
10736 input_line_pointer = save;
10737 return scale;
252b5132
RH
10738}
10739
252b5132 10740static int
e3bb37b5 10741i386_displacement (char *disp_start, char *disp_end)
252b5132 10742{
29b0f896 10743 expressionS *exp;
252b5132
RH
10744 segT exp_seg = 0;
10745 char *save_input_line_pointer;
f3c180ae 10746 char *gotfree_input_line;
40fb9820
L
10747 int override;
10748 i386_operand_type bigdisp, types = anydisp;
3992d3b7 10749 int ret;
252b5132 10750
31b2323c
L
10751 if (i.disp_operands == MAX_MEMORY_OPERANDS)
10752 {
10753 as_bad (_("at most %d displacement operands are allowed"),
10754 MAX_MEMORY_OPERANDS);
10755 return 0;
10756 }
10757
0dfbf9d7 10758 operand_type_set (&bigdisp, 0);
6f2f06be 10759 if (i.jumpabsolute
48bcea9f 10760 || i.types[this_operand].bitfield.baseindex
0cfa3eb3
JB
10761 || (current_templates->start->opcode_modifier.jump != JUMP
10762 && current_templates->start->opcode_modifier.jump != JUMP_DWORD))
e05278af 10763 {
48bcea9f 10764 i386_addressing_mode ();
e05278af 10765 override = (i.prefix[ADDR_PREFIX] != 0);
40fb9820
L
10766 if (flag_code == CODE_64BIT)
10767 {
10768 if (!override)
10769 {
10770 bigdisp.bitfield.disp32s = 1;
10771 bigdisp.bitfield.disp64 = 1;
10772 }
48bcea9f
JB
10773 else
10774 bigdisp.bitfield.disp32 = 1;
40fb9820
L
10775 }
10776 else if ((flag_code == CODE_16BIT) ^ override)
40fb9820 10777 bigdisp.bitfield.disp16 = 1;
48bcea9f
JB
10778 else
10779 bigdisp.bitfield.disp32 = 1;
e05278af
JB
10780 }
10781 else
10782 {
376cd056
JB
10783 /* For PC-relative branches, the width of the displacement may be
10784 dependent upon data size, but is never dependent upon address size.
10785 Also make sure to not unintentionally match against a non-PC-relative
10786 branch template. */
10787 static templates aux_templates;
10788 const insn_template *t = current_templates->start;
5b7c81bd 10789 bool has_intel64 = false;
376cd056
JB
10790
10791 aux_templates.start = t;
10792 while (++t < current_templates->end)
10793 {
10794 if (t->opcode_modifier.jump
10795 != current_templates->start->opcode_modifier.jump)
10796 break;
4b5aaf5f 10797 if ((t->opcode_modifier.isa64 >= INTEL64))
5b7c81bd 10798 has_intel64 = true;
376cd056
JB
10799 }
10800 if (t < current_templates->end)
10801 {
10802 aux_templates.end = t;
10803 current_templates = &aux_templates;
10804 }
10805
e05278af 10806 override = (i.prefix[DATA_PREFIX] != 0);
40fb9820
L
10807 if (flag_code == CODE_64BIT)
10808 {
376cd056
JB
10809 if ((override || i.suffix == WORD_MNEM_SUFFIX)
10810 && (!intel64 || !has_intel64))
40fb9820
L
10811 bigdisp.bitfield.disp16 = 1;
10812 else
48bcea9f 10813 bigdisp.bitfield.disp32s = 1;
40fb9820
L
10814 }
10815 else
e05278af
JB
10816 {
10817 if (!override)
10818 override = (i.suffix == (flag_code != CODE_16BIT
10819 ? WORD_MNEM_SUFFIX
10820 : LONG_MNEM_SUFFIX));
40fb9820
L
10821 bigdisp.bitfield.disp32 = 1;
10822 if ((flag_code == CODE_16BIT) ^ override)
10823 {
10824 bigdisp.bitfield.disp32 = 0;
10825 bigdisp.bitfield.disp16 = 1;
10826 }
e05278af 10827 }
e05278af 10828 }
c6fb90c8
L
10829 i.types[this_operand] = operand_type_or (i.types[this_operand],
10830 bigdisp);
252b5132
RH
10831
10832 exp = &disp_expressions[i.disp_operands];
520dc8e8 10833 i.op[this_operand].disps = exp;
252b5132
RH
10834 i.disp_operands++;
10835 save_input_line_pointer = input_line_pointer;
10836 input_line_pointer = disp_start;
10837 END_STRING_AND_SAVE (disp_end);
10838
10839#ifndef GCC_ASM_O_HACK
10840#define GCC_ASM_O_HACK 0
10841#endif
10842#if GCC_ASM_O_HACK
10843 END_STRING_AND_SAVE (disp_end + 1);
40fb9820 10844 if (i.types[this_operand].bitfield.baseIndex
24eab124 10845 && displacement_string_end[-1] == '+')
252b5132
RH
10846 {
10847 /* This hack is to avoid a warning when using the "o"
24eab124
AM
10848 constraint within gcc asm statements.
10849 For instance:
10850
10851 #define _set_tssldt_desc(n,addr,limit,type) \
10852 __asm__ __volatile__ ( \
10853 "movw %w2,%0\n\t" \
10854 "movw %w1,2+%0\n\t" \
10855 "rorl $16,%1\n\t" \
10856 "movb %b1,4+%0\n\t" \
10857 "movb %4,5+%0\n\t" \
10858 "movb $0,6+%0\n\t" \
10859 "movb %h1,7+%0\n\t" \
10860 "rorl $16,%1" \
10861 : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
10862
10863 This works great except that the output assembler ends
10864 up looking a bit weird if it turns out that there is
10865 no offset. You end up producing code that looks like:
10866
10867 #APP
10868 movw $235,(%eax)
10869 movw %dx,2+(%eax)
10870 rorl $16,%edx
10871 movb %dl,4+(%eax)
10872 movb $137,5+(%eax)
10873 movb $0,6+(%eax)
10874 movb %dh,7+(%eax)
10875 rorl $16,%edx
10876 #NO_APP
10877
47926f60 10878 So here we provide the missing zero. */
24eab124
AM
10879
10880 *displacement_string_end = '0';
252b5132
RH
10881 }
10882#endif
d258b828 10883 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
10884 if (gotfree_input_line)
10885 input_line_pointer = gotfree_input_line;
252b5132 10886
24eab124 10887 exp_seg = expression (exp);
252b5132 10888
636c26b0
AM
10889 SKIP_WHITESPACE ();
10890 if (*input_line_pointer)
10891 as_bad (_("junk `%s' after expression"), input_line_pointer);
10892#if GCC_ASM_O_HACK
10893 RESTORE_END_STRING (disp_end + 1);
10894#endif
636c26b0 10895 input_line_pointer = save_input_line_pointer;
636c26b0 10896 if (gotfree_input_line)
ee86248c
JB
10897 {
10898 free (gotfree_input_line);
10899
10900 if (exp->X_op == O_constant || exp->X_op == O_register)
10901 exp->X_op = O_illegal;
10902 }
10903
10904 ret = i386_finalize_displacement (exp_seg, exp, types, disp_start);
10905
10906 RESTORE_END_STRING (disp_end);
10907
10908 return ret;
10909}
10910
10911static int
10912i386_finalize_displacement (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
10913 i386_operand_type types, const char *disp_start)
10914{
10915 i386_operand_type bigdisp;
10916 int ret = 1;
636c26b0 10917
24eab124
AM
10918 /* We do this to make sure that the section symbol is in
10919 the symbol table. We will ultimately change the relocation
47926f60 10920 to be relative to the beginning of the section. */
1ae12ab7 10921 if (i.reloc[this_operand] == BFD_RELOC_386_GOTOFF
d6ab8113
JB
10922 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
10923 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
24eab124 10924 {
636c26b0 10925 if (exp->X_op != O_symbol)
3992d3b7 10926 goto inv_disp;
636c26b0 10927
e5cb08ac 10928 if (S_IS_LOCAL (exp->X_add_symbol)
c64efb4b
L
10929 && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section
10930 && S_GET_SEGMENT (exp->X_add_symbol) != expr_section)
24eab124 10931 section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
24eab124
AM
10932 exp->X_op = O_subtract;
10933 exp->X_op_symbol = GOT_symbol;
1ae12ab7 10934 if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
29b0f896 10935 i.reloc[this_operand] = BFD_RELOC_32_PCREL;
d6ab8113
JB
10936 else if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
10937 i.reloc[this_operand] = BFD_RELOC_64;
23df1078 10938 else
29b0f896 10939 i.reloc[this_operand] = BFD_RELOC_32;
24eab124 10940 }
252b5132 10941
3992d3b7
AM
10942 else if (exp->X_op == O_absent
10943 || exp->X_op == O_illegal
ee86248c 10944 || exp->X_op == O_big)
2daf4fd8 10945 {
3992d3b7
AM
10946 inv_disp:
10947 as_bad (_("missing or invalid displacement expression `%s'"),
2daf4fd8 10948 disp_start);
3992d3b7 10949 ret = 0;
2daf4fd8
AM
10950 }
10951
4c63da97 10952#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
3992d3b7
AM
10953 else if (exp->X_op != O_constant
10954 && OUTPUT_FLAVOR == bfd_target_aout_flavour
10955 && exp_seg != absolute_section
10956 && exp_seg != text_section
10957 && exp_seg != data_section
10958 && exp_seg != bss_section
10959 && exp_seg != undefined_section
10960 && !bfd_is_com_section (exp_seg))
24eab124 10961 {
d0b47220 10962 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
3992d3b7 10963 ret = 0;
24eab124 10964 }
252b5132 10965#endif
3956db08 10966
48bcea9f
JB
10967 if (current_templates->start->opcode_modifier.jump == JUMP_BYTE
10968 /* Constants get taken care of by optimize_disp(). */
10969 && exp->X_op != O_constant)
10970 i.types[this_operand].bitfield.disp8 = 1;
10971
40fb9820
L
10972 /* Check if this is a displacement only operand. */
10973 bigdisp = i.types[this_operand];
10974 bigdisp.bitfield.disp8 = 0;
10975 bigdisp.bitfield.disp16 = 0;
10976 bigdisp.bitfield.disp32 = 0;
10977 bigdisp.bitfield.disp32s = 0;
10978 bigdisp.bitfield.disp64 = 0;
0dfbf9d7 10979 if (operand_type_all_zero (&bigdisp))
c6fb90c8
L
10980 i.types[this_operand] = operand_type_and (i.types[this_operand],
10981 types);
3956db08 10982
3992d3b7 10983 return ret;
252b5132
RH
10984}
10985
2abc2bec
JB
10986/* Return the active addressing mode, taking address override and
10987 registers forming the address into consideration. Update the
10988 address override prefix if necessary. */
47926f60 10989
2abc2bec
JB
10990static enum flag_code
10991i386_addressing_mode (void)
252b5132 10992{
be05d201
L
10993 enum flag_code addr_mode;
10994
10995 if (i.prefix[ADDR_PREFIX])
10996 addr_mode = flag_code == CODE_32BIT ? CODE_16BIT : CODE_32BIT;
a23b33b3
JB
10997 else if (flag_code == CODE_16BIT
10998 && current_templates->start->cpu_flags.bitfield.cpumpx
10999 /* Avoid replacing the "16-bit addressing not allowed" diagnostic
11000 from md_assemble() by "is not a valid base/index expression"
11001 when there is a base and/or index. */
11002 && !i.types[this_operand].bitfield.baseindex)
11003 {
11004 /* MPX insn memory operands with neither base nor index must be forced
11005 to use 32-bit addressing in 16-bit mode. */
11006 addr_mode = CODE_32BIT;
11007 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
11008 ++i.prefixes;
11009 gas_assert (!i.types[this_operand].bitfield.disp16);
11010 gas_assert (!i.types[this_operand].bitfield.disp32);
11011 }
be05d201
L
11012 else
11013 {
11014 addr_mode = flag_code;
11015
24eab124 11016#if INFER_ADDR_PREFIX
be05d201
L
11017 if (i.mem_operands == 0)
11018 {
11019 /* Infer address prefix from the first memory operand. */
11020 const reg_entry *addr_reg = i.base_reg;
11021
11022 if (addr_reg == NULL)
11023 addr_reg = i.index_reg;
eecb386c 11024
be05d201
L
11025 if (addr_reg)
11026 {
e968fc9b 11027 if (addr_reg->reg_type.bitfield.dword)
be05d201
L
11028 addr_mode = CODE_32BIT;
11029 else if (flag_code != CODE_64BIT
dc821c5f 11030 && addr_reg->reg_type.bitfield.word)
be05d201
L
11031 addr_mode = CODE_16BIT;
11032
11033 if (addr_mode != flag_code)
11034 {
11035 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
11036 i.prefixes += 1;
11037 /* Change the size of any displacement too. At most one
11038 of Disp16 or Disp32 is set.
11039 FIXME. There doesn't seem to be any real need for
11040 separate Disp16 and Disp32 flags. The same goes for
11041 Imm16 and Imm32. Removing them would probably clean
11042 up the code quite a lot. */
11043 if (flag_code != CODE_64BIT
11044 && (i.types[this_operand].bitfield.disp16
11045 || i.types[this_operand].bitfield.disp32))
11046 i.types[this_operand]
11047 = operand_type_xor (i.types[this_operand], disp16_32);
11048 }
11049 }
11050 }
24eab124 11051#endif
be05d201
L
11052 }
11053
2abc2bec
JB
11054 return addr_mode;
11055}
11056
11057/* Make sure the memory operand we've been dealt is valid.
11058 Return 1 on success, 0 on a failure. */
11059
11060static int
11061i386_index_check (const char *operand_string)
11062{
11063 const char *kind = "base/index";
11064 enum flag_code addr_mode = i386_addressing_mode ();
a152332d 11065 const insn_template *t = current_templates->start;
2abc2bec 11066
a152332d
JB
11067 if (t->opcode_modifier.isstring
11068 && !t->cpu_flags.bitfield.cpupadlock
fc0763e6
JB
11069 && (current_templates->end[-1].opcode_modifier.isstring
11070 || i.mem_operands))
11071 {
11072 /* Memory operands of string insns are special in that they only allow
11073 a single register (rDI, rSI, or rBX) as their memory address. */
be05d201
L
11074 const reg_entry *expected_reg;
11075 static const char *di_si[][2] =
11076 {
11077 { "esi", "edi" },
11078 { "si", "di" },
11079 { "rsi", "rdi" }
11080 };
11081 static const char *bx[] = { "ebx", "bx", "rbx" };
fc0763e6
JB
11082
11083 kind = "string address";
11084
a152332d 11085 if (t->opcode_modifier.prefixok == PrefixRep)
fc0763e6 11086 {
51c8edf6
JB
11087 int es_op = current_templates->end[-1].opcode_modifier.isstring
11088 - IS_STRING_ES_OP0;
11089 int op = 0;
fc0763e6 11090
51c8edf6 11091 if (!current_templates->end[-1].operand_types[0].bitfield.baseindex
fc0763e6
JB
11092 || ((!i.mem_operands != !intel_syntax)
11093 && current_templates->end[-1].operand_types[1]
11094 .bitfield.baseindex))
51c8edf6 11095 op = 1;
fe0e921f
AM
11096 expected_reg
11097 = (const reg_entry *) str_hash_find (reg_hash,
11098 di_si[addr_mode][op == es_op]);
fc0763e6
JB
11099 }
11100 else
fe0e921f
AM
11101 expected_reg
11102 = (const reg_entry *)str_hash_find (reg_hash, bx[addr_mode]);
fc0763e6 11103
be05d201
L
11104 if (i.base_reg != expected_reg
11105 || i.index_reg
fc0763e6 11106 || operand_type_check (i.types[this_operand], disp))
fc0763e6 11107 {
be05d201
L
11108 /* The second memory operand must have the same size as
11109 the first one. */
11110 if (i.mem_operands
11111 && i.base_reg
11112 && !((addr_mode == CODE_64BIT
dc821c5f 11113 && i.base_reg->reg_type.bitfield.qword)
be05d201 11114 || (addr_mode == CODE_32BIT
dc821c5f
JB
11115 ? i.base_reg->reg_type.bitfield.dword
11116 : i.base_reg->reg_type.bitfield.word)))
be05d201
L
11117 goto bad_address;
11118
fc0763e6
JB
11119 as_warn (_("`%s' is not valid here (expected `%c%s%s%c')"),
11120 operand_string,
11121 intel_syntax ? '[' : '(',
11122 register_prefix,
be05d201 11123 expected_reg->reg_name,
fc0763e6 11124 intel_syntax ? ']' : ')');
be05d201 11125 return 1;
fc0763e6 11126 }
be05d201
L
11127 else
11128 return 1;
11129
dc1e8a47 11130 bad_address:
be05d201
L
11131 as_bad (_("`%s' is not a valid %s expression"),
11132 operand_string, kind);
11133 return 0;
3e73aa7c
JH
11134 }
11135 else
11136 {
be05d201
L
11137 if (addr_mode != CODE_16BIT)
11138 {
11139 /* 32-bit/64-bit checks. */
41eb8e88
L
11140 if (i.disp_encoding == disp_encoding_16bit)
11141 {
11142 bad_disp:
11143 as_bad (_("invalid `%s' prefix"),
11144 addr_mode == CODE_16BIT ? "{disp32}" : "{disp16}");
11145 return 0;
11146 }
11147
be05d201 11148 if ((i.base_reg
e968fc9b
JB
11149 && ((addr_mode == CODE_64BIT
11150 ? !i.base_reg->reg_type.bitfield.qword
11151 : !i.base_reg->reg_type.bitfield.dword)
11152 || (i.index_reg && i.base_reg->reg_num == RegIP)
11153 || i.base_reg->reg_num == RegIZ))
be05d201 11154 || (i.index_reg
1b54b8d7
JB
11155 && !i.index_reg->reg_type.bitfield.xmmword
11156 && !i.index_reg->reg_type.bitfield.ymmword
11157 && !i.index_reg->reg_type.bitfield.zmmword
be05d201 11158 && ((addr_mode == CODE_64BIT
e968fc9b
JB
11159 ? !i.index_reg->reg_type.bitfield.qword
11160 : !i.index_reg->reg_type.bitfield.dword)
be05d201
L
11161 || !i.index_reg->reg_type.bitfield.baseindex)))
11162 goto bad_address;
8178be5b 11163
260cd341 11164 /* bndmk, bndldx, bndstx and mandatory non-vector SIB have special restrictions. */
a152332d 11165 if ((t->opcode_modifier.opcodeprefix == PREFIX_0XF3
389d00a5
JB
11166 && t->opcode_modifier.opcodespace == SPACE_0F
11167 && t->base_opcode == 0x1b)
a152332d 11168 || (t->opcode_modifier.opcodeprefix == PREFIX_NONE
389d00a5
JB
11169 && t->opcode_modifier.opcodespace == SPACE_0F
11170 && (t->base_opcode & ~1) == 0x1a)
a152332d 11171 || t->opcode_modifier.sib == SIBMEM)
8178be5b
JB
11172 {
11173 /* They cannot use RIP-relative addressing. */
e968fc9b 11174 if (i.base_reg && i.base_reg->reg_num == RegIP)
8178be5b
JB
11175 {
11176 as_bad (_("`%s' cannot be used here"), operand_string);
11177 return 0;
11178 }
11179
11180 /* bndldx and bndstx ignore their scale factor. */
a152332d 11181 if (t->opcode_modifier.opcodeprefix == PREFIX_NONE
389d00a5
JB
11182 && t->opcode_modifier.opcodespace == SPACE_0F
11183 && (t->base_opcode & ~1) == 0x1a
8178be5b
JB
11184 && i.log2_scale_factor)
11185 as_warn (_("register scaling is being ignored here"));
11186 }
be05d201
L
11187 }
11188 else
3e73aa7c 11189 {
be05d201 11190 /* 16-bit checks. */
41eb8e88
L
11191 if (i.disp_encoding == disp_encoding_32bit)
11192 goto bad_disp;
11193
3e73aa7c 11194 if ((i.base_reg
dc821c5f 11195 && (!i.base_reg->reg_type.bitfield.word
40fb9820 11196 || !i.base_reg->reg_type.bitfield.baseindex))
3e73aa7c 11197 || (i.index_reg
dc821c5f 11198 && (!i.index_reg->reg_type.bitfield.word
40fb9820 11199 || !i.index_reg->reg_type.bitfield.baseindex
29b0f896
AM
11200 || !(i.base_reg
11201 && i.base_reg->reg_num < 6
11202 && i.index_reg->reg_num >= 6
11203 && i.log2_scale_factor == 0))))
be05d201 11204 goto bad_address;
3e73aa7c
JH
11205 }
11206 }
be05d201 11207 return 1;
24eab124 11208}
252b5132 11209
43234a1e
L
11210/* Handle vector immediates. */
11211
11212static int
11213RC_SAE_immediate (const char *imm_start)
11214{
11215 unsigned int match_found, j;
11216 const char *pstr = imm_start;
11217 expressionS *exp;
11218
11219 if (*pstr != '{')
11220 return 0;
11221
11222 pstr++;
11223 match_found = 0;
11224 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); j++)
11225 {
11226 if (!strncmp (pstr, RC_NamesTable[j].name, RC_NamesTable[j].len))
11227 {
ca5312a2 11228 if (i.rounding.type != rc_none)
43234a1e
L
11229 {
11230 as_bad (_("duplicated `%s'"), imm_start);
11231 return 0;
11232 }
ca5312a2
JB
11233
11234 i.rounding.type = RC_NamesTable[j].type;
11235 i.rounding.operand = this_operand;
11236
43234a1e
L
11237 pstr += RC_NamesTable[j].len;
11238 match_found = 1;
11239 break;
11240 }
11241 }
11242 if (!match_found)
11243 return 0;
11244
11245 if (*pstr++ != '}')
11246 {
11247 as_bad (_("Missing '}': '%s'"), imm_start);
11248 return 0;
11249 }
11250 /* RC/SAE immediate string should contain nothing more. */;
11251 if (*pstr != 0)
11252 {
11253 as_bad (_("Junk after '}': '%s'"), imm_start);
11254 return 0;
11255 }
11256
11257 exp = &im_expressions[i.imm_operands++];
11258 i.op[this_operand].imms = exp;
11259
11260 exp->X_op = O_constant;
11261 exp->X_add_number = 0;
11262 exp->X_add_symbol = (symbolS *) 0;
11263 exp->X_op_symbol = (symbolS *) 0;
11264
11265 i.types[this_operand].bitfield.imm8 = 1;
11266 return 1;
11267}
11268
8325cc63
JB
11269/* Only string instructions can have a second memory operand, so
11270 reduce current_templates to just those if it contains any. */
11271static int
11272maybe_adjust_templates (void)
11273{
11274 const insn_template *t;
11275
11276 gas_assert (i.mem_operands == 1);
11277
11278 for (t = current_templates->start; t < current_templates->end; ++t)
11279 if (t->opcode_modifier.isstring)
11280 break;
11281
11282 if (t < current_templates->end)
11283 {
11284 static templates aux_templates;
5b7c81bd 11285 bool recheck;
8325cc63
JB
11286
11287 aux_templates.start = t;
11288 for (; t < current_templates->end; ++t)
11289 if (!t->opcode_modifier.isstring)
11290 break;
11291 aux_templates.end = t;
11292
11293 /* Determine whether to re-check the first memory operand. */
11294 recheck = (aux_templates.start != current_templates->start
11295 || t != current_templates->end);
11296
11297 current_templates = &aux_templates;
11298
11299 if (recheck)
11300 {
11301 i.mem_operands = 0;
11302 if (i.memop1_string != NULL
11303 && i386_index_check (i.memop1_string) == 0)
11304 return 0;
11305 i.mem_operands = 1;
11306 }
11307 }
11308
11309 return 1;
11310}
11311
fc0763e6 11312/* Parse OPERAND_STRING into the i386_insn structure I. Returns zero
47926f60 11313 on error. */
252b5132 11314
252b5132 11315static int
a7619375 11316i386_att_operand (char *operand_string)
252b5132 11317{
af6bdddf
AM
11318 const reg_entry *r;
11319 char *end_op;
24eab124 11320 char *op_string = operand_string;
252b5132 11321
24eab124 11322 if (is_space_char (*op_string))
252b5132
RH
11323 ++op_string;
11324
24eab124 11325 /* We check for an absolute prefix (differentiating,
47926f60 11326 for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
24eab124
AM
11327 if (*op_string == ABSOLUTE_PREFIX)
11328 {
11329 ++op_string;
11330 if (is_space_char (*op_string))
11331 ++op_string;
5b7c81bd 11332 i.jumpabsolute = true;
24eab124 11333 }
252b5132 11334
47926f60 11335 /* Check if operand is a register. */
4d1bb795 11336 if ((r = parse_register (op_string, &end_op)) != NULL)
24eab124 11337 {
40fb9820
L
11338 i386_operand_type temp;
11339
8a6fb3f9
JB
11340 if (r == &bad_reg)
11341 return 0;
11342
24eab124
AM
11343 /* Check for a segment override by searching for ':' after a
11344 segment register. */
11345 op_string = end_op;
11346 if (is_space_char (*op_string))
11347 ++op_string;
00cee14f 11348 if (*op_string == ':' && r->reg_type.bitfield.class == SReg)
24eab124 11349 {
5e042380 11350 i.seg[i.mem_operands] = r;
252b5132 11351
24eab124 11352 /* Skip the ':' and whitespace. */
252b5132
RH
11353 ++op_string;
11354 if (is_space_char (*op_string))
24eab124 11355 ++op_string;
252b5132 11356
24eab124
AM
11357 if (!is_digit_char (*op_string)
11358 && !is_identifier_char (*op_string)
11359 && *op_string != '('
11360 && *op_string != ABSOLUTE_PREFIX)
11361 {
11362 as_bad (_("bad memory operand `%s'"), op_string);
11363 return 0;
11364 }
47926f60 11365 /* Handle case of %es:*foo. */
24eab124
AM
11366 if (*op_string == ABSOLUTE_PREFIX)
11367 {
11368 ++op_string;
11369 if (is_space_char (*op_string))
11370 ++op_string;
5b7c81bd 11371 i.jumpabsolute = true;
24eab124
AM
11372 }
11373 goto do_memory_reference;
11374 }
43234a1e
L
11375
11376 /* Handle vector operations. */
11377 if (*op_string == '{')
11378 {
11379 op_string = check_VecOperations (op_string, NULL);
11380 if (op_string == NULL)
11381 return 0;
11382 }
11383
24eab124
AM
11384 if (*op_string)
11385 {
d0b47220 11386 as_bad (_("junk `%s' after register"), op_string);
24eab124
AM
11387 return 0;
11388 }
40fb9820
L
11389 temp = r->reg_type;
11390 temp.bitfield.baseindex = 0;
c6fb90c8
L
11391 i.types[this_operand] = operand_type_or (i.types[this_operand],
11392 temp);
7d5e4556 11393 i.types[this_operand].bitfield.unspecified = 0;
520dc8e8 11394 i.op[this_operand].regs = r;
24eab124
AM
11395 i.reg_operands++;
11396 }
af6bdddf
AM
11397 else if (*op_string == REGISTER_PREFIX)
11398 {
11399 as_bad (_("bad register name `%s'"), op_string);
11400 return 0;
11401 }
24eab124 11402 else if (*op_string == IMMEDIATE_PREFIX)
ce8a8b2f 11403 {
24eab124 11404 ++op_string;
6f2f06be 11405 if (i.jumpabsolute)
24eab124 11406 {
d0b47220 11407 as_bad (_("immediate operand illegal with absolute jump"));
24eab124
AM
11408 return 0;
11409 }
11410 if (!i386_immediate (op_string))
11411 return 0;
11412 }
43234a1e
L
11413 else if (RC_SAE_immediate (operand_string))
11414 {
11415 /* If it is a RC or SAE immediate, do nothing. */
11416 ;
11417 }
24eab124
AM
11418 else if (is_digit_char (*op_string)
11419 || is_identifier_char (*op_string)
d02603dc 11420 || *op_string == '"'
e5cb08ac 11421 || *op_string == '(')
24eab124 11422 {
47926f60 11423 /* This is a memory reference of some sort. */
af6bdddf 11424 char *base_string;
252b5132 11425
47926f60 11426 /* Start and end of displacement string expression (if found). */
eecb386c
AM
11427 char *displacement_string_start;
11428 char *displacement_string_end;
43234a1e 11429 char *vop_start;
252b5132 11430
24eab124 11431 do_memory_reference:
8325cc63
JB
11432 if (i.mem_operands == 1 && !maybe_adjust_templates ())
11433 return 0;
24eab124 11434 if ((i.mem_operands == 1
40fb9820 11435 && !current_templates->start->opcode_modifier.isstring)
24eab124
AM
11436 || i.mem_operands == 2)
11437 {
11438 as_bad (_("too many memory references for `%s'"),
11439 current_templates->start->name);
11440 return 0;
11441 }
252b5132 11442
24eab124
AM
11443 /* Check for base index form. We detect the base index form by
11444 looking for an ')' at the end of the operand, searching
11445 for the '(' matching it, and finding a REGISTER_PREFIX or ','
11446 after the '('. */
af6bdddf 11447 base_string = op_string + strlen (op_string);
c3332e24 11448
43234a1e
L
11449 /* Handle vector operations. */
11450 vop_start = strchr (op_string, '{');
11451 if (vop_start && vop_start < base_string)
11452 {
11453 if (check_VecOperations (vop_start, base_string) == NULL)
11454 return 0;
11455 base_string = vop_start;
11456 }
11457
af6bdddf
AM
11458 --base_string;
11459 if (is_space_char (*base_string))
11460 --base_string;
252b5132 11461
47926f60 11462 /* If we only have a displacement, set-up for it to be parsed later. */
af6bdddf
AM
11463 displacement_string_start = op_string;
11464 displacement_string_end = base_string + 1;
252b5132 11465
24eab124
AM
11466 if (*base_string == ')')
11467 {
af6bdddf 11468 char *temp_string;
24eab124
AM
11469 unsigned int parens_balanced = 1;
11470 /* We've already checked that the number of left & right ()'s are
47926f60 11471 equal, so this loop will not be infinite. */
24eab124
AM
11472 do
11473 {
11474 base_string--;
11475 if (*base_string == ')')
11476 parens_balanced++;
11477 if (*base_string == '(')
11478 parens_balanced--;
11479 }
11480 while (parens_balanced);
c3332e24 11481
af6bdddf 11482 temp_string = base_string;
c3332e24 11483
24eab124 11484 /* Skip past '(' and whitespace. */
252b5132
RH
11485 ++base_string;
11486 if (is_space_char (*base_string))
24eab124 11487 ++base_string;
252b5132 11488
af6bdddf 11489 if (*base_string == ','
4eed87de
AM
11490 || ((i.base_reg = parse_register (base_string, &end_op))
11491 != NULL))
252b5132 11492 {
af6bdddf 11493 displacement_string_end = temp_string;
252b5132 11494
40fb9820 11495 i.types[this_operand].bitfield.baseindex = 1;
252b5132 11496
af6bdddf 11497 if (i.base_reg)
24eab124 11498 {
8a6fb3f9
JB
11499 if (i.base_reg == &bad_reg)
11500 return 0;
24eab124
AM
11501 base_string = end_op;
11502 if (is_space_char (*base_string))
11503 ++base_string;
af6bdddf
AM
11504 }
11505
11506 /* There may be an index reg or scale factor here. */
11507 if (*base_string == ',')
11508 {
11509 ++base_string;
11510 if (is_space_char (*base_string))
11511 ++base_string;
11512
4eed87de
AM
11513 if ((i.index_reg = parse_register (base_string, &end_op))
11514 != NULL)
24eab124 11515 {
8a6fb3f9
JB
11516 if (i.index_reg == &bad_reg)
11517 return 0;
af6bdddf 11518 base_string = end_op;
24eab124
AM
11519 if (is_space_char (*base_string))
11520 ++base_string;
af6bdddf
AM
11521 if (*base_string == ',')
11522 {
11523 ++base_string;
11524 if (is_space_char (*base_string))
11525 ++base_string;
11526 }
e5cb08ac 11527 else if (*base_string != ')')
af6bdddf 11528 {
4eed87de
AM
11529 as_bad (_("expecting `,' or `)' "
11530 "after index register in `%s'"),
af6bdddf
AM
11531 operand_string);
11532 return 0;
11533 }
24eab124 11534 }
af6bdddf 11535 else if (*base_string == REGISTER_PREFIX)
24eab124 11536 {
f76bf5e0
L
11537 end_op = strchr (base_string, ',');
11538 if (end_op)
11539 *end_op = '\0';
af6bdddf 11540 as_bad (_("bad register name `%s'"), base_string);
24eab124
AM
11541 return 0;
11542 }
252b5132 11543
47926f60 11544 /* Check for scale factor. */
551c1ca1 11545 if (*base_string != ')')
af6bdddf 11546 {
551c1ca1
AM
11547 char *end_scale = i386_scale (base_string);
11548
11549 if (!end_scale)
af6bdddf 11550 return 0;
24eab124 11551
551c1ca1 11552 base_string = end_scale;
af6bdddf
AM
11553 if (is_space_char (*base_string))
11554 ++base_string;
11555 if (*base_string != ')')
11556 {
4eed87de
AM
11557 as_bad (_("expecting `)' "
11558 "after scale factor in `%s'"),
af6bdddf
AM
11559 operand_string);
11560 return 0;
11561 }
11562 }
11563 else if (!i.index_reg)
24eab124 11564 {
4eed87de
AM
11565 as_bad (_("expecting index register or scale factor "
11566 "after `,'; got '%c'"),
af6bdddf 11567 *base_string);
24eab124
AM
11568 return 0;
11569 }
11570 }
af6bdddf 11571 else if (*base_string != ')')
24eab124 11572 {
4eed87de
AM
11573 as_bad (_("expecting `,' or `)' "
11574 "after base register in `%s'"),
af6bdddf 11575 operand_string);
24eab124
AM
11576 return 0;
11577 }
c3332e24 11578 }
af6bdddf 11579 else if (*base_string == REGISTER_PREFIX)
c3332e24 11580 {
f76bf5e0
L
11581 end_op = strchr (base_string, ',');
11582 if (end_op)
11583 *end_op = '\0';
af6bdddf 11584 as_bad (_("bad register name `%s'"), base_string);
24eab124 11585 return 0;
c3332e24 11586 }
24eab124
AM
11587 }
11588
11589 /* If there's an expression beginning the operand, parse it,
11590 assuming displacement_string_start and
11591 displacement_string_end are meaningful. */
11592 if (displacement_string_start != displacement_string_end)
11593 {
11594 if (!i386_displacement (displacement_string_start,
11595 displacement_string_end))
11596 return 0;
11597 }
11598
11599 /* Special case for (%dx) while doing input/output op. */
11600 if (i.base_reg
75e5731b
JB
11601 && i.base_reg->reg_type.bitfield.instance == RegD
11602 && i.base_reg->reg_type.bitfield.word
24eab124
AM
11603 && i.index_reg == 0
11604 && i.log2_scale_factor == 0
11605 && i.seg[i.mem_operands] == 0
40fb9820 11606 && !operand_type_check (i.types[this_operand], disp))
24eab124 11607 {
2fb5be8d 11608 i.types[this_operand] = i.base_reg->reg_type;
24eab124
AM
11609 return 1;
11610 }
11611
eecb386c
AM
11612 if (i386_index_check (operand_string) == 0)
11613 return 0;
c48dadc9 11614 i.flags[this_operand] |= Operand_Mem;
8325cc63
JB
11615 if (i.mem_operands == 0)
11616 i.memop1_string = xstrdup (operand_string);
24eab124
AM
11617 i.mem_operands++;
11618 }
11619 else
ce8a8b2f
AM
11620 {
11621 /* It's not a memory operand; argh! */
24eab124
AM
11622 as_bad (_("invalid char %s beginning operand %d `%s'"),
11623 output_invalid (*op_string),
11624 this_operand + 1,
11625 op_string);
11626 return 0;
11627 }
47926f60 11628 return 1; /* Normal return. */
252b5132
RH
11629}
11630\f
fa94de6b
RM
11631/* Calculate the maximum variable size (i.e., excluding fr_fix)
11632 that an rs_machine_dependent frag may reach. */
11633
11634unsigned int
11635i386_frag_max_var (fragS *frag)
11636{
11637 /* The only relaxable frags are for jumps.
11638 Unconditional jumps can grow by 4 bytes and others by 5 bytes. */
11639 gas_assert (frag->fr_type == rs_machine_dependent);
11640 return TYPE_FROM_RELAX_STATE (frag->fr_subtype) == UNCOND_JUMP ? 4 : 5;
11641}
11642
b084df0b
L
11643#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
11644static int
8dcea932 11645elf_symbol_resolved_in_segment_p (symbolS *fr_symbol, offsetT fr_var)
b084df0b
L
11646{
11647 /* STT_GNU_IFUNC symbol must go through PLT. */
11648 if ((symbol_get_bfdsym (fr_symbol)->flags
11649 & BSF_GNU_INDIRECT_FUNCTION) != 0)
11650 return 0;
11651
11652 if (!S_IS_EXTERNAL (fr_symbol))
11653 /* Symbol may be weak or local. */
11654 return !S_IS_WEAK (fr_symbol);
11655
8dcea932
L
11656 /* Global symbols with non-default visibility can't be preempted. */
11657 if (ELF_ST_VISIBILITY (S_GET_OTHER (fr_symbol)) != STV_DEFAULT)
11658 return 1;
11659
11660 if (fr_var != NO_RELOC)
11661 switch ((enum bfd_reloc_code_real) fr_var)
11662 {
11663 case BFD_RELOC_386_PLT32:
11664 case BFD_RELOC_X86_64_PLT32:
33eaf5de 11665 /* Symbol with PLT relocation may be preempted. */
8dcea932
L
11666 return 0;
11667 default:
11668 abort ();
11669 }
11670
b084df0b
L
11671 /* Global symbols with default visibility in a shared library may be
11672 preempted by another definition. */
8dcea932 11673 return !shared;
b084df0b
L
11674}
11675#endif
11676
79d72f45
HL
11677/* Table 3-2. Macro-Fusible Instructions in Haswell Microarchitecture
11678 Note also work for Skylake and Cascadelake.
11679---------------------------------------------------------------------
11680| JCC | ADD/SUB/CMP | INC/DEC | TEST/AND |
11681| ------ | ----------- | ------- | -------- |
11682| Jo | N | N | Y |
11683| Jno | N | N | Y |
11684| Jc/Jb | Y | N | Y |
11685| Jae/Jnb | Y | N | Y |
11686| Je/Jz | Y | Y | Y |
11687| Jne/Jnz | Y | Y | Y |
11688| Jna/Jbe | Y | N | Y |
11689| Ja/Jnbe | Y | N | Y |
11690| Js | N | N | Y |
11691| Jns | N | N | Y |
11692| Jp/Jpe | N | N | Y |
11693| Jnp/Jpo | N | N | Y |
11694| Jl/Jnge | Y | Y | Y |
11695| Jge/Jnl | Y | Y | Y |
11696| Jle/Jng | Y | Y | Y |
11697| Jg/Jnle | Y | Y | Y |
11698--------------------------------------------------------------------- */
11699static int
11700i386_macro_fusible_p (enum mf_cmp_kind mf_cmp, enum mf_jcc_kind mf_jcc)
11701{
11702 if (mf_cmp == mf_cmp_alu_cmp)
11703 return ((mf_jcc >= mf_jcc_jc && mf_jcc <= mf_jcc_jna)
11704 || mf_jcc == mf_jcc_jl || mf_jcc == mf_jcc_jle);
11705 if (mf_cmp == mf_cmp_incdec)
11706 return (mf_jcc == mf_jcc_je || mf_jcc == mf_jcc_jl
11707 || mf_jcc == mf_jcc_jle);
11708 if (mf_cmp == mf_cmp_test_and)
11709 return 1;
11710 return 0;
11711}
11712
e379e5f3
L
11713/* Return the next non-empty frag. */
11714
11715static fragS *
11716i386_next_non_empty_frag (fragS *fragP)
11717{
11718 /* There may be a frag with a ".fill 0" when there is no room in
11719 the current frag for frag_grow in output_insn. */
11720 for (fragP = fragP->fr_next;
11721 (fragP != NULL
11722 && fragP->fr_type == rs_fill
11723 && fragP->fr_fix == 0);
11724 fragP = fragP->fr_next)
11725 ;
11726 return fragP;
11727}
11728
11729/* Return the next jcc frag after BRANCH_PADDING. */
11730
11731static fragS *
79d72f45 11732i386_next_fusible_jcc_frag (fragS *maybe_cmp_fragP, fragS *pad_fragP)
e379e5f3 11733{
79d72f45
HL
11734 fragS *branch_fragP;
11735 if (!pad_fragP)
e379e5f3
L
11736 return NULL;
11737
79d72f45
HL
11738 if (pad_fragP->fr_type == rs_machine_dependent
11739 && (TYPE_FROM_RELAX_STATE (pad_fragP->fr_subtype)
e379e5f3
L
11740 == BRANCH_PADDING))
11741 {
79d72f45
HL
11742 branch_fragP = i386_next_non_empty_frag (pad_fragP);
11743 if (branch_fragP->fr_type != rs_machine_dependent)
e379e5f3 11744 return NULL;
79d72f45
HL
11745 if (TYPE_FROM_RELAX_STATE (branch_fragP->fr_subtype) == COND_JUMP
11746 && i386_macro_fusible_p (maybe_cmp_fragP->tc_frag_data.mf_type,
11747 pad_fragP->tc_frag_data.mf_type))
11748 return branch_fragP;
e379e5f3
L
11749 }
11750
11751 return NULL;
11752}
11753
11754/* Classify BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags. */
11755
11756static void
11757i386_classify_machine_dependent_frag (fragS *fragP)
11758{
11759 fragS *cmp_fragP;
11760 fragS *pad_fragP;
11761 fragS *branch_fragP;
11762 fragS *next_fragP;
11763 unsigned int max_prefix_length;
11764
11765 if (fragP->tc_frag_data.classified)
11766 return;
11767
11768 /* First scan for BRANCH_PADDING and FUSED_JCC_PADDING. Convert
11769 FUSED_JCC_PADDING and merge BRANCH_PADDING. */
11770 for (next_fragP = fragP;
11771 next_fragP != NULL;
11772 next_fragP = next_fragP->fr_next)
11773 {
11774 next_fragP->tc_frag_data.classified = 1;
11775 if (next_fragP->fr_type == rs_machine_dependent)
11776 switch (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype))
11777 {
11778 case BRANCH_PADDING:
11779 /* The BRANCH_PADDING frag must be followed by a branch
11780 frag. */
11781 branch_fragP = i386_next_non_empty_frag (next_fragP);
11782 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
11783 break;
11784 case FUSED_JCC_PADDING:
11785 /* Check if this is a fused jcc:
11786 FUSED_JCC_PADDING
11787 CMP like instruction
11788 BRANCH_PADDING
11789 COND_JUMP
11790 */
11791 cmp_fragP = i386_next_non_empty_frag (next_fragP);
11792 pad_fragP = i386_next_non_empty_frag (cmp_fragP);
79d72f45 11793 branch_fragP = i386_next_fusible_jcc_frag (next_fragP, pad_fragP);
e379e5f3
L
11794 if (branch_fragP)
11795 {
11796 /* The BRANCH_PADDING frag is merged with the
11797 FUSED_JCC_PADDING frag. */
11798 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
11799 /* CMP like instruction size. */
11800 next_fragP->tc_frag_data.cmp_size = cmp_fragP->fr_fix;
11801 frag_wane (pad_fragP);
11802 /* Skip to branch_fragP. */
11803 next_fragP = branch_fragP;
11804 }
11805 else if (next_fragP->tc_frag_data.max_prefix_length)
11806 {
11807 /* Turn FUSED_JCC_PADDING into BRANCH_PREFIX if it isn't
11808 a fused jcc. */
11809 next_fragP->fr_subtype
11810 = ENCODE_RELAX_STATE (BRANCH_PREFIX, 0);
11811 next_fragP->tc_frag_data.max_bytes
11812 = next_fragP->tc_frag_data.max_prefix_length;
11813 /* This will be updated in the BRANCH_PREFIX scan. */
11814 next_fragP->tc_frag_data.max_prefix_length = 0;
11815 }
11816 else
11817 frag_wane (next_fragP);
11818 break;
11819 }
11820 }
11821
11822 /* Stop if there is no BRANCH_PREFIX. */
11823 if (!align_branch_prefix_size)
11824 return;
11825
11826 /* Scan for BRANCH_PREFIX. */
11827 for (; fragP != NULL; fragP = fragP->fr_next)
11828 {
11829 if (fragP->fr_type != rs_machine_dependent
11830 || (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
11831 != BRANCH_PREFIX))
11832 continue;
11833
11834 /* Count all BRANCH_PREFIX frags before BRANCH_PADDING and
11835 COND_JUMP_PREFIX. */
11836 max_prefix_length = 0;
11837 for (next_fragP = fragP;
11838 next_fragP != NULL;
11839 next_fragP = next_fragP->fr_next)
11840 {
11841 if (next_fragP->fr_type == rs_fill)
11842 /* Skip rs_fill frags. */
11843 continue;
11844 else if (next_fragP->fr_type != rs_machine_dependent)
11845 /* Stop for all other frags. */
11846 break;
11847
11848 /* rs_machine_dependent frags. */
11849 if (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
11850 == BRANCH_PREFIX)
11851 {
11852 /* Count BRANCH_PREFIX frags. */
11853 if (max_prefix_length >= MAX_FUSED_JCC_PADDING_SIZE)
11854 {
11855 max_prefix_length = MAX_FUSED_JCC_PADDING_SIZE;
11856 frag_wane (next_fragP);
11857 }
11858 else
11859 max_prefix_length
11860 += next_fragP->tc_frag_data.max_bytes;
11861 }
11862 else if ((TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
11863 == BRANCH_PADDING)
11864 || (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
11865 == FUSED_JCC_PADDING))
11866 {
11867 /* Stop at BRANCH_PADDING and FUSED_JCC_PADDING. */
11868 fragP->tc_frag_data.u.padding_fragP = next_fragP;
11869 break;
11870 }
11871 else
11872 /* Stop for other rs_machine_dependent frags. */
11873 break;
11874 }
11875
11876 fragP->tc_frag_data.max_prefix_length = max_prefix_length;
11877
11878 /* Skip to the next frag. */
11879 fragP = next_fragP;
11880 }
11881}
11882
11883/* Compute padding size for
11884
11885 FUSED_JCC_PADDING
11886 CMP like instruction
11887 BRANCH_PADDING
11888 COND_JUMP/UNCOND_JUMP
11889
11890 or
11891
11892 BRANCH_PADDING
11893 COND_JUMP/UNCOND_JUMP
11894 */
11895
11896static int
11897i386_branch_padding_size (fragS *fragP, offsetT address)
11898{
11899 unsigned int offset, size, padding_size;
11900 fragS *branch_fragP = fragP->tc_frag_data.u.branch_fragP;
11901
11902 /* The start address of the BRANCH_PADDING or FUSED_JCC_PADDING frag. */
11903 if (!address)
11904 address = fragP->fr_address;
11905 address += fragP->fr_fix;
11906
11907 /* CMP like instrunction size. */
11908 size = fragP->tc_frag_data.cmp_size;
11909
11910 /* The base size of the branch frag. */
11911 size += branch_fragP->fr_fix;
11912
11913 /* Add opcode and displacement bytes for the rs_machine_dependent
11914 branch frag. */
11915 if (branch_fragP->fr_type == rs_machine_dependent)
11916 size += md_relax_table[branch_fragP->fr_subtype].rlx_length;
11917
11918 /* Check if branch is within boundary and doesn't end at the last
11919 byte. */
11920 offset = address & ((1U << align_branch_power) - 1);
11921 if ((offset + size) >= (1U << align_branch_power))
11922 /* Padding needed to avoid crossing boundary. */
11923 padding_size = (1U << align_branch_power) - offset;
11924 else
11925 /* No padding needed. */
11926 padding_size = 0;
11927
11928 /* The return value may be saved in tc_frag_data.length which is
11929 unsigned byte. */
11930 if (!fits_in_unsigned_byte (padding_size))
11931 abort ();
11932
11933 return padding_size;
11934}
11935
11936/* i386_generic_table_relax_frag()
11937
11938 Handle BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags to
11939 grow/shrink padding to align branch frags. Hand others to
11940 relax_frag(). */
11941
11942long
11943i386_generic_table_relax_frag (segT segment, fragS *fragP, long stretch)
11944{
11945 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
11946 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
11947 {
11948 long padding_size = i386_branch_padding_size (fragP, 0);
11949 long grow = padding_size - fragP->tc_frag_data.length;
11950
11951 /* When the BRANCH_PREFIX frag is used, the computed address
11952 must match the actual address and there should be no padding. */
11953 if (fragP->tc_frag_data.padding_address
11954 && (fragP->tc_frag_data.padding_address != fragP->fr_address
11955 || padding_size))
11956 abort ();
11957
11958 /* Update the padding size. */
11959 if (grow)
11960 fragP->tc_frag_data.length = padding_size;
11961
11962 return grow;
11963 }
11964 else if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
11965 {
11966 fragS *padding_fragP, *next_fragP;
11967 long padding_size, left_size, last_size;
11968
11969 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
11970 if (!padding_fragP)
11971 /* Use the padding set by the leading BRANCH_PREFIX frag. */
11972 return (fragP->tc_frag_data.length
11973 - fragP->tc_frag_data.last_length);
11974
11975 /* Compute the relative address of the padding frag in the very
11976 first time where the BRANCH_PREFIX frag sizes are zero. */
11977 if (!fragP->tc_frag_data.padding_address)
11978 fragP->tc_frag_data.padding_address
11979 = padding_fragP->fr_address - (fragP->fr_address - stretch);
11980
11981 /* First update the last length from the previous interation. */
11982 left_size = fragP->tc_frag_data.prefix_length;
11983 for (next_fragP = fragP;
11984 next_fragP != padding_fragP;
11985 next_fragP = next_fragP->fr_next)
11986 if (next_fragP->fr_type == rs_machine_dependent
11987 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
11988 == BRANCH_PREFIX))
11989 {
11990 if (left_size)
11991 {
11992 int max = next_fragP->tc_frag_data.max_bytes;
11993 if (max)
11994 {
11995 int size;
11996 if (max > left_size)
11997 size = left_size;
11998 else
11999 size = max;
12000 left_size -= size;
12001 next_fragP->tc_frag_data.last_length = size;
12002 }
12003 }
12004 else
12005 next_fragP->tc_frag_data.last_length = 0;
12006 }
12007
12008 /* Check the padding size for the padding frag. */
12009 padding_size = i386_branch_padding_size
12010 (padding_fragP, (fragP->fr_address
12011 + fragP->tc_frag_data.padding_address));
12012
12013 last_size = fragP->tc_frag_data.prefix_length;
12014 /* Check if there is change from the last interation. */
12015 if (padding_size == last_size)
12016 {
12017 /* Update the expected address of the padding frag. */
12018 padding_fragP->tc_frag_data.padding_address
12019 = (fragP->fr_address + padding_size
12020 + fragP->tc_frag_data.padding_address);
12021 return 0;
12022 }
12023
12024 if (padding_size > fragP->tc_frag_data.max_prefix_length)
12025 {
12026 /* No padding if there is no sufficient room. Clear the
12027 expected address of the padding frag. */
12028 padding_fragP->tc_frag_data.padding_address = 0;
12029 padding_size = 0;
12030 }
12031 else
12032 /* Store the expected address of the padding frag. */
12033 padding_fragP->tc_frag_data.padding_address
12034 = (fragP->fr_address + padding_size
12035 + fragP->tc_frag_data.padding_address);
12036
12037 fragP->tc_frag_data.prefix_length = padding_size;
12038
12039 /* Update the length for the current interation. */
12040 left_size = padding_size;
12041 for (next_fragP = fragP;
12042 next_fragP != padding_fragP;
12043 next_fragP = next_fragP->fr_next)
12044 if (next_fragP->fr_type == rs_machine_dependent
12045 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12046 == BRANCH_PREFIX))
12047 {
12048 if (left_size)
12049 {
12050 int max = next_fragP->tc_frag_data.max_bytes;
12051 if (max)
12052 {
12053 int size;
12054 if (max > left_size)
12055 size = left_size;
12056 else
12057 size = max;
12058 left_size -= size;
12059 next_fragP->tc_frag_data.length = size;
12060 }
12061 }
12062 else
12063 next_fragP->tc_frag_data.length = 0;
12064 }
12065
12066 return (fragP->tc_frag_data.length
12067 - fragP->tc_frag_data.last_length);
12068 }
12069 return relax_frag (segment, fragP, stretch);
12070}
12071
ee7fcc42
AM
12072/* md_estimate_size_before_relax()
12073
12074 Called just before relax() for rs_machine_dependent frags. The x86
12075 assembler uses these frags to handle variable size jump
12076 instructions.
12077
12078 Any symbol that is now undefined will not become defined.
12079 Return the correct fr_subtype in the frag.
12080 Return the initial "guess for variable size of frag" to caller.
12081 The guess is actually the growth beyond the fixed part. Whatever
12082 we do to grow the fixed or variable part contributes to our
12083 returned value. */
12084
252b5132 12085int
7016a5d5 12086md_estimate_size_before_relax (fragS *fragP, segT segment)
252b5132 12087{
e379e5f3
L
12088 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12089 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX
12090 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
12091 {
12092 i386_classify_machine_dependent_frag (fragP);
12093 return fragP->tc_frag_data.length;
12094 }
12095
252b5132 12096 /* We've already got fragP->fr_subtype right; all we have to do is
b98ef147
AM
12097 check for un-relaxable symbols. On an ELF system, we can't relax
12098 an externally visible symbol, because it may be overridden by a
12099 shared library. */
12100 if (S_GET_SEGMENT (fragP->fr_symbol) != segment
6d249963 12101#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12102 || (IS_ELF
8dcea932
L
12103 && !elf_symbol_resolved_in_segment_p (fragP->fr_symbol,
12104 fragP->fr_var))
fbeb56a4
DK
12105#endif
12106#if defined (OBJ_COFF) && defined (TE_PE)
7ab9ffdd 12107 || (OUTPUT_FLAVOR == bfd_target_coff_flavour
fbeb56a4 12108 && S_IS_WEAK (fragP->fr_symbol))
b98ef147
AM
12109#endif
12110 )
252b5132 12111 {
b98ef147
AM
12112 /* Symbol is undefined in this segment, or we need to keep a
12113 reloc so that weak symbols can be overridden. */
12114 int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
f86103b7 12115 enum bfd_reloc_code_real reloc_type;
ee7fcc42
AM
12116 unsigned char *opcode;
12117 int old_fr_fix;
f6af82bd 12118
ee7fcc42 12119 if (fragP->fr_var != NO_RELOC)
1e9cc1c2 12120 reloc_type = (enum bfd_reloc_code_real) fragP->fr_var;
b98ef147 12121 else if (size == 2)
f6af82bd 12122 reloc_type = BFD_RELOC_16_PCREL;
bd7ab16b
L
12123#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
12124 else if (need_plt32_p (fragP->fr_symbol))
12125 reloc_type = BFD_RELOC_X86_64_PLT32;
12126#endif
f6af82bd
AM
12127 else
12128 reloc_type = BFD_RELOC_32_PCREL;
252b5132 12129
ee7fcc42
AM
12130 old_fr_fix = fragP->fr_fix;
12131 opcode = (unsigned char *) fragP->fr_opcode;
12132
fddf5b5b 12133 switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
252b5132 12134 {
fddf5b5b
AM
12135 case UNCOND_JUMP:
12136 /* Make jmp (0xeb) a (d)word displacement jump. */
47926f60 12137 opcode[0] = 0xe9;
252b5132 12138 fragP->fr_fix += size;
062cd5e7
AS
12139 fix_new (fragP, old_fr_fix, size,
12140 fragP->fr_symbol,
12141 fragP->fr_offset, 1,
12142 reloc_type);
252b5132
RH
12143 break;
12144
fddf5b5b 12145 case COND_JUMP86:
412167cb
AM
12146 if (size == 2
12147 && (!no_cond_jump_promotion || fragP->fr_var != NO_RELOC))
fddf5b5b
AM
12148 {
12149 /* Negate the condition, and branch past an
12150 unconditional jump. */
12151 opcode[0] ^= 1;
12152 opcode[1] = 3;
12153 /* Insert an unconditional jump. */
12154 opcode[2] = 0xe9;
12155 /* We added two extra opcode bytes, and have a two byte
12156 offset. */
12157 fragP->fr_fix += 2 + 2;
062cd5e7
AS
12158 fix_new (fragP, old_fr_fix + 2, 2,
12159 fragP->fr_symbol,
12160 fragP->fr_offset, 1,
12161 reloc_type);
fddf5b5b
AM
12162 break;
12163 }
12164 /* Fall through. */
12165
12166 case COND_JUMP:
412167cb
AM
12167 if (no_cond_jump_promotion && fragP->fr_var == NO_RELOC)
12168 {
3e02c1cc
AM
12169 fixS *fixP;
12170
412167cb 12171 fragP->fr_fix += 1;
3e02c1cc
AM
12172 fixP = fix_new (fragP, old_fr_fix, 1,
12173 fragP->fr_symbol,
12174 fragP->fr_offset, 1,
12175 BFD_RELOC_8_PCREL);
12176 fixP->fx_signed = 1;
412167cb
AM
12177 break;
12178 }
93c2a809 12179
24eab124 12180 /* This changes the byte-displacement jump 0x7N
fddf5b5b 12181 to the (d)word-displacement jump 0x0f,0x8N. */
252b5132 12182 opcode[1] = opcode[0] + 0x10;
f6af82bd 12183 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
47926f60
KH
12184 /* We've added an opcode byte. */
12185 fragP->fr_fix += 1 + size;
062cd5e7
AS
12186 fix_new (fragP, old_fr_fix + 1, size,
12187 fragP->fr_symbol,
12188 fragP->fr_offset, 1,
12189 reloc_type);
252b5132 12190 break;
fddf5b5b
AM
12191
12192 default:
12193 BAD_CASE (fragP->fr_subtype);
12194 break;
252b5132
RH
12195 }
12196 frag_wane (fragP);
ee7fcc42 12197 return fragP->fr_fix - old_fr_fix;
252b5132 12198 }
93c2a809 12199
93c2a809
AM
12200 /* Guess size depending on current relax state. Initially the relax
12201 state will correspond to a short jump and we return 1, because
12202 the variable part of the frag (the branch offset) is one byte
12203 long. However, we can relax a section more than once and in that
12204 case we must either set fr_subtype back to the unrelaxed state,
12205 or return the value for the appropriate branch. */
12206 return md_relax_table[fragP->fr_subtype].rlx_length;
ee7fcc42
AM
12207}
12208
47926f60
KH
12209/* Called after relax() is finished.
12210
12211 In: Address of frag.
12212 fr_type == rs_machine_dependent.
12213 fr_subtype is what the address relaxed to.
12214
12215 Out: Any fixSs and constants are set up.
12216 Caller will turn frag into a ".space 0". */
12217
252b5132 12218void
7016a5d5
TG
12219md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec ATTRIBUTE_UNUSED,
12220 fragS *fragP)
252b5132 12221{
29b0f896 12222 unsigned char *opcode;
252b5132 12223 unsigned char *where_to_put_displacement = NULL;
847f7ad4
AM
12224 offsetT target_address;
12225 offsetT opcode_address;
252b5132 12226 unsigned int extension = 0;
847f7ad4 12227 offsetT displacement_from_opcode_start;
252b5132 12228
e379e5f3
L
12229 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12230 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING
12231 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12232 {
12233 /* Generate nop padding. */
12234 unsigned int size = fragP->tc_frag_data.length;
12235 if (size)
12236 {
12237 if (size > fragP->tc_frag_data.max_bytes)
12238 abort ();
12239
12240 if (flag_debug)
12241 {
12242 const char *msg;
12243 const char *branch = "branch";
12244 const char *prefix = "";
12245 fragS *padding_fragP;
12246 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
12247 == BRANCH_PREFIX)
12248 {
12249 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
12250 switch (fragP->tc_frag_data.default_prefix)
12251 {
12252 default:
12253 abort ();
12254 break;
12255 case CS_PREFIX_OPCODE:
12256 prefix = " cs";
12257 break;
12258 case DS_PREFIX_OPCODE:
12259 prefix = " ds";
12260 break;
12261 case ES_PREFIX_OPCODE:
12262 prefix = " es";
12263 break;
12264 case FS_PREFIX_OPCODE:
12265 prefix = " fs";
12266 break;
12267 case GS_PREFIX_OPCODE:
12268 prefix = " gs";
12269 break;
12270 case SS_PREFIX_OPCODE:
12271 prefix = " ss";
12272 break;
12273 }
12274 if (padding_fragP)
12275 msg = _("%s:%u: add %d%s at 0x%llx to align "
12276 "%s within %d-byte boundary\n");
12277 else
12278 msg = _("%s:%u: add additional %d%s at 0x%llx to "
12279 "align %s within %d-byte boundary\n");
12280 }
12281 else
12282 {
12283 padding_fragP = fragP;
12284 msg = _("%s:%u: add %d%s-byte nop at 0x%llx to align "
12285 "%s within %d-byte boundary\n");
12286 }
12287
12288 if (padding_fragP)
12289 switch (padding_fragP->tc_frag_data.branch_type)
12290 {
12291 case align_branch_jcc:
12292 branch = "jcc";
12293 break;
12294 case align_branch_fused:
12295 branch = "fused jcc";
12296 break;
12297 case align_branch_jmp:
12298 branch = "jmp";
12299 break;
12300 case align_branch_call:
12301 branch = "call";
12302 break;
12303 case align_branch_indirect:
12304 branch = "indiret branch";
12305 break;
12306 case align_branch_ret:
12307 branch = "ret";
12308 break;
12309 default:
12310 break;
12311 }
12312
12313 fprintf (stdout, msg,
12314 fragP->fr_file, fragP->fr_line, size, prefix,
12315 (long long) fragP->fr_address, branch,
12316 1 << align_branch_power);
12317 }
12318 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12319 memset (fragP->fr_opcode,
12320 fragP->tc_frag_data.default_prefix, size);
12321 else
12322 i386_generate_nops (fragP, (char *) fragP->fr_opcode,
12323 size, 0);
12324 fragP->fr_fix += size;
12325 }
12326 return;
12327 }
12328
252b5132
RH
12329 opcode = (unsigned char *) fragP->fr_opcode;
12330
47926f60 12331 /* Address we want to reach in file space. */
252b5132 12332 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
252b5132 12333
47926f60 12334 /* Address opcode resides at in file space. */
252b5132
RH
12335 opcode_address = fragP->fr_address + fragP->fr_fix;
12336
47926f60 12337 /* Displacement from opcode start to fill into instruction. */
252b5132
RH
12338 displacement_from_opcode_start = target_address - opcode_address;
12339
fddf5b5b 12340 if ((fragP->fr_subtype & BIG) == 0)
252b5132 12341 {
47926f60
KH
12342 /* Don't have to change opcode. */
12343 extension = 1; /* 1 opcode + 1 displacement */
252b5132 12344 where_to_put_displacement = &opcode[1];
fddf5b5b
AM
12345 }
12346 else
12347 {
12348 if (no_cond_jump_promotion
12349 && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
4eed87de
AM
12350 as_warn_where (fragP->fr_file, fragP->fr_line,
12351 _("long jump required"));
252b5132 12352
fddf5b5b
AM
12353 switch (fragP->fr_subtype)
12354 {
12355 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
12356 extension = 4; /* 1 opcode + 4 displacement */
12357 opcode[0] = 0xe9;
12358 where_to_put_displacement = &opcode[1];
12359 break;
252b5132 12360
fddf5b5b
AM
12361 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
12362 extension = 2; /* 1 opcode + 2 displacement */
12363 opcode[0] = 0xe9;
12364 where_to_put_displacement = &opcode[1];
12365 break;
252b5132 12366
fddf5b5b
AM
12367 case ENCODE_RELAX_STATE (COND_JUMP, BIG):
12368 case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
12369 extension = 5; /* 2 opcode + 4 displacement */
12370 opcode[1] = opcode[0] + 0x10;
12371 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12372 where_to_put_displacement = &opcode[2];
12373 break;
252b5132 12374
fddf5b5b
AM
12375 case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
12376 extension = 3; /* 2 opcode + 2 displacement */
12377 opcode[1] = opcode[0] + 0x10;
12378 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12379 where_to_put_displacement = &opcode[2];
12380 break;
252b5132 12381
fddf5b5b
AM
12382 case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
12383 extension = 4;
12384 opcode[0] ^= 1;
12385 opcode[1] = 3;
12386 opcode[2] = 0xe9;
12387 where_to_put_displacement = &opcode[3];
12388 break;
12389
12390 default:
12391 BAD_CASE (fragP->fr_subtype);
12392 break;
12393 }
252b5132 12394 }
fddf5b5b 12395
7b81dfbb
AJ
12396 /* If size if less then four we are sure that the operand fits,
12397 but if it's 4, then it could be that the displacement is larger
12398 then -/+ 2GB. */
12399 if (DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype) == 4
12400 && object_64bit
12401 && ((addressT) (displacement_from_opcode_start - extension
4eed87de
AM
12402 + ((addressT) 1 << 31))
12403 > (((addressT) 2 << 31) - 1)))
7b81dfbb
AJ
12404 {
12405 as_bad_where (fragP->fr_file, fragP->fr_line,
12406 _("jump target out of range"));
12407 /* Make us emit 0. */
12408 displacement_from_opcode_start = extension;
12409 }
47926f60 12410 /* Now put displacement after opcode. */
252b5132
RH
12411 md_number_to_chars ((char *) where_to_put_displacement,
12412 (valueT) (displacement_from_opcode_start - extension),
fddf5b5b 12413 DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
252b5132
RH
12414 fragP->fr_fix += extension;
12415}
12416\f
7016a5d5 12417/* Apply a fixup (fixP) to segment data, once it has been determined
252b5132
RH
12418 by our caller that we have all the info we need to fix it up.
12419
7016a5d5
TG
12420 Parameter valP is the pointer to the value of the bits.
12421
252b5132
RH
12422 On the 386, immediates, displacements, and data pointers are all in
12423 the same (little-endian) format, so we don't need to care about which
12424 we are handling. */
12425
94f592af 12426void
7016a5d5 12427md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 12428{
94f592af 12429 char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
c6682705 12430 valueT value = *valP;
252b5132 12431
f86103b7 12432#if !defined (TE_Mach)
93382f6d
AM
12433 if (fixP->fx_pcrel)
12434 {
12435 switch (fixP->fx_r_type)
12436 {
5865bb77
ILT
12437 default:
12438 break;
12439
d6ab8113
JB
12440 case BFD_RELOC_64:
12441 fixP->fx_r_type = BFD_RELOC_64_PCREL;
12442 break;
93382f6d 12443 case BFD_RELOC_32:
ae8887b5 12444 case BFD_RELOC_X86_64_32S:
93382f6d
AM
12445 fixP->fx_r_type = BFD_RELOC_32_PCREL;
12446 break;
12447 case BFD_RELOC_16:
12448 fixP->fx_r_type = BFD_RELOC_16_PCREL;
12449 break;
12450 case BFD_RELOC_8:
12451 fixP->fx_r_type = BFD_RELOC_8_PCREL;
12452 break;
12453 }
12454 }
252b5132 12455
a161fe53 12456 if (fixP->fx_addsy != NULL
31312f95 12457 && (fixP->fx_r_type == BFD_RELOC_32_PCREL
d6ab8113 12458 || fixP->fx_r_type == BFD_RELOC_64_PCREL
31312f95 12459 || fixP->fx_r_type == BFD_RELOC_16_PCREL
d258b828 12460 || fixP->fx_r_type == BFD_RELOC_8_PCREL)
31312f95 12461 && !use_rela_relocations)
252b5132 12462 {
31312f95
AM
12463 /* This is a hack. There should be a better way to handle this.
12464 This covers for the fact that bfd_install_relocation will
12465 subtract the current location (for partial_inplace, PC relative
12466 relocations); see more below. */
252b5132 12467#ifndef OBJ_AOUT
718ddfc0 12468 if (IS_ELF
252b5132
RH
12469#ifdef TE_PE
12470 || OUTPUT_FLAVOR == bfd_target_coff_flavour
12471#endif
12472 )
12473 value += fixP->fx_where + fixP->fx_frag->fr_address;
12474#endif
12475#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12476 if (IS_ELF)
252b5132 12477 {
6539b54b 12478 segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
2f66722d 12479
6539b54b 12480 if ((sym_seg == seg
2f66722d 12481 || (symbol_section_p (fixP->fx_addsy)
6539b54b 12482 && sym_seg != absolute_section))
af65af87 12483 && !generic_force_reloc (fixP))
2f66722d
AM
12484 {
12485 /* Yes, we add the values in twice. This is because
6539b54b
AM
12486 bfd_install_relocation subtracts them out again. I think
12487 bfd_install_relocation is broken, but I don't dare change
2f66722d
AM
12488 it. FIXME. */
12489 value += fixP->fx_where + fixP->fx_frag->fr_address;
12490 }
252b5132
RH
12491 }
12492#endif
12493#if defined (OBJ_COFF) && defined (TE_PE)
977cdf5a
NC
12494 /* For some reason, the PE format does not store a
12495 section address offset for a PC relative symbol. */
12496 if (S_GET_SEGMENT (fixP->fx_addsy) != seg
7be1c489 12497 || S_IS_WEAK (fixP->fx_addsy))
252b5132
RH
12498 value += md_pcrel_from (fixP);
12499#endif
12500 }
fbeb56a4 12501#if defined (OBJ_COFF) && defined (TE_PE)
f01c1a09
NC
12502 if (fixP->fx_addsy != NULL
12503 && S_IS_WEAK (fixP->fx_addsy)
12504 /* PR 16858: Do not modify weak function references. */
12505 && ! fixP->fx_pcrel)
fbeb56a4 12506 {
296a8689
NC
12507#if !defined (TE_PEP)
12508 /* For x86 PE weak function symbols are neither PC-relative
12509 nor do they set S_IS_FUNCTION. So the only reliable way
12510 to detect them is to check the flags of their containing
12511 section. */
12512 if (S_GET_SEGMENT (fixP->fx_addsy) != NULL
12513 && S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_CODE)
12514 ;
12515 else
12516#endif
fbeb56a4
DK
12517 value -= S_GET_VALUE (fixP->fx_addsy);
12518 }
12519#endif
252b5132
RH
12520
12521 /* Fix a few things - the dynamic linker expects certain values here,
0234cb7c 12522 and we must not disappoint it. */
252b5132 12523#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12524 if (IS_ELF && fixP->fx_addsy)
47926f60
KH
12525 switch (fixP->fx_r_type)
12526 {
12527 case BFD_RELOC_386_PLT32:
3e73aa7c 12528 case BFD_RELOC_X86_64_PLT32:
b9519cfe
L
12529 /* Make the jump instruction point to the address of the operand.
12530 At runtime we merely add the offset to the actual PLT entry.
12531 NB: Subtract the offset size only for jump instructions. */
12532 if (fixP->fx_pcrel)
12533 value = -4;
47926f60 12534 break;
31312f95 12535
13ae64f3
JJ
12536 case BFD_RELOC_386_TLS_GD:
12537 case BFD_RELOC_386_TLS_LDM:
13ae64f3 12538 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
12539 case BFD_RELOC_386_TLS_IE:
12540 case BFD_RELOC_386_TLS_GOTIE:
67a4f2b7 12541 case BFD_RELOC_386_TLS_GOTDESC:
bffbf940
JJ
12542 case BFD_RELOC_X86_64_TLSGD:
12543 case BFD_RELOC_X86_64_TLSLD:
12544 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7 12545 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
00f7efb6
JJ
12546 value = 0; /* Fully resolved at runtime. No addend. */
12547 /* Fallthrough */
12548 case BFD_RELOC_386_TLS_LE:
12549 case BFD_RELOC_386_TLS_LDO_32:
12550 case BFD_RELOC_386_TLS_LE_32:
12551 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 12552 case BFD_RELOC_X86_64_DTPOFF64:
00f7efb6 12553 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113 12554 case BFD_RELOC_X86_64_TPOFF64:
00f7efb6
JJ
12555 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12556 break;
12557
67a4f2b7
AO
12558 case BFD_RELOC_386_TLS_DESC_CALL:
12559 case BFD_RELOC_X86_64_TLSDESC_CALL:
12560 value = 0; /* Fully resolved at runtime. No addend. */
12561 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12562 fixP->fx_done = 0;
12563 return;
12564
47926f60
KH
12565 case BFD_RELOC_VTABLE_INHERIT:
12566 case BFD_RELOC_VTABLE_ENTRY:
12567 fixP->fx_done = 0;
94f592af 12568 return;
47926f60
KH
12569
12570 default:
12571 break;
12572 }
12573#endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) */
c6682705 12574 *valP = value;
f86103b7 12575#endif /* !defined (TE_Mach) */
3e73aa7c 12576
3e73aa7c 12577 /* Are we finished with this relocation now? */
c6682705 12578 if (fixP->fx_addsy == NULL)
b8188555
JB
12579 {
12580 fixP->fx_done = 1;
12581 switch (fixP->fx_r_type)
12582 {
12583 case BFD_RELOC_X86_64_32S:
12584 fixP->fx_signed = 1;
12585 break;
12586
12587 default:
12588 break;
12589 }
12590 }
fbeb56a4
DK
12591#if defined (OBJ_COFF) && defined (TE_PE)
12592 else if (fixP->fx_addsy != NULL && S_IS_WEAK (fixP->fx_addsy))
12593 {
12594 fixP->fx_done = 0;
12595 /* Remember value for tc_gen_reloc. */
12596 fixP->fx_addnumber = value;
12597 /* Clear out the frag for now. */
12598 value = 0;
12599 }
12600#endif
3e73aa7c
JH
12601 else if (use_rela_relocations)
12602 {
12603 fixP->fx_no_overflow = 1;
062cd5e7
AS
12604 /* Remember value for tc_gen_reloc. */
12605 fixP->fx_addnumber = value;
3e73aa7c
JH
12606 value = 0;
12607 }
f86103b7 12608
94f592af 12609 md_number_to_chars (p, value, fixP->fx_size);
252b5132 12610}
252b5132 12611\f
6d4af3c2 12612const char *
499ac353 12613md_atof (int type, char *litP, int *sizeP)
252b5132 12614{
499ac353
NC
12615 /* This outputs the LITTLENUMs in REVERSE order;
12616 in accord with the bigendian 386. */
5b7c81bd 12617 return ieee_md_atof (type, litP, sizeP, false);
252b5132
RH
12618}
12619\f
2d545b82 12620static char output_invalid_buf[sizeof (unsigned char) * 2 + 6];
252b5132 12621
252b5132 12622static char *
e3bb37b5 12623output_invalid (int c)
252b5132 12624{
3882b010 12625 if (ISPRINT (c))
f9f21a03
L
12626 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
12627 "'%c'", c);
252b5132 12628 else
f9f21a03 12629 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
2d545b82 12630 "(0x%x)", (unsigned char) c);
252b5132
RH
12631 return output_invalid_buf;
12632}
12633
8a6fb3f9
JB
12634/* Verify that @r can be used in the current context. */
12635
5b7c81bd 12636static bool check_register (const reg_entry *r)
8a6fb3f9
JB
12637{
12638 if (allow_pseudo_reg)
5b7c81bd 12639 return true;
8a6fb3f9
JB
12640
12641 if (operand_type_all_zero (&r->reg_type))
5b7c81bd 12642 return false;
8a6fb3f9
JB
12643
12644 if ((r->reg_type.bitfield.dword
12645 || (r->reg_type.bitfield.class == SReg && r->reg_num > 3)
12646 || r->reg_type.bitfield.class == RegCR
22e00a3f 12647 || r->reg_type.bitfield.class == RegDR)
8a6fb3f9 12648 && !cpu_arch_flags.bitfield.cpui386)
5b7c81bd 12649 return false;
8a6fb3f9 12650
22e00a3f
JB
12651 if (r->reg_type.bitfield.class == RegTR
12652 && (flag_code == CODE_64BIT
12653 || !cpu_arch_flags.bitfield.cpui386
12654 || cpu_arch_isa_flags.bitfield.cpui586
12655 || cpu_arch_isa_flags.bitfield.cpui686))
5b7c81bd 12656 return false;
22e00a3f 12657
8a6fb3f9 12658 if (r->reg_type.bitfield.class == RegMMX && !cpu_arch_flags.bitfield.cpummx)
5b7c81bd 12659 return false;
8a6fb3f9
JB
12660
12661 if (!cpu_arch_flags.bitfield.cpuavx512f)
12662 {
12663 if (r->reg_type.bitfield.zmmword
12664 || r->reg_type.bitfield.class == RegMask)
5b7c81bd 12665 return false;
8a6fb3f9
JB
12666
12667 if (!cpu_arch_flags.bitfield.cpuavx)
12668 {
12669 if (r->reg_type.bitfield.ymmword)
5b7c81bd 12670 return false;
8a6fb3f9
JB
12671
12672 if (!cpu_arch_flags.bitfield.cpusse && r->reg_type.bitfield.xmmword)
5b7c81bd 12673 return false;
8a6fb3f9
JB
12674 }
12675 }
12676
260cd341
LC
12677 if (r->reg_type.bitfield.tmmword
12678 && (!cpu_arch_flags.bitfield.cpuamx_tile
12679 || flag_code != CODE_64BIT))
5b7c81bd 12680 return false;
260cd341 12681
8a6fb3f9 12682 if (r->reg_type.bitfield.class == RegBND && !cpu_arch_flags.bitfield.cpumpx)
5b7c81bd 12683 return false;
8a6fb3f9
JB
12684
12685 /* Don't allow fake index register unless allow_index_reg isn't 0. */
12686 if (!allow_index_reg && r->reg_num == RegIZ)
5b7c81bd 12687 return false;
8a6fb3f9
JB
12688
12689 /* Upper 16 vector registers are only available with VREX in 64bit
12690 mode, and require EVEX encoding. */
12691 if (r->reg_flags & RegVRex)
12692 {
12693 if (!cpu_arch_flags.bitfield.cpuavx512f
12694 || flag_code != CODE_64BIT)
5b7c81bd 12695 return false;
8a6fb3f9 12696
da4977e0
JB
12697 if (i.vec_encoding == vex_encoding_default)
12698 i.vec_encoding = vex_encoding_evex;
12699 else if (i.vec_encoding != vex_encoding_evex)
12700 i.vec_encoding = vex_encoding_error;
8a6fb3f9
JB
12701 }
12702
12703 if (((r->reg_flags & (RegRex64 | RegRex)) || r->reg_type.bitfield.qword)
12704 && (!cpu_arch_flags.bitfield.cpulm || r->reg_type.bitfield.class != RegCR)
12705 && flag_code != CODE_64BIT)
5b7c81bd 12706 return false;
8a6fb3f9
JB
12707
12708 if (r->reg_type.bitfield.class == SReg && r->reg_num == RegFlat
12709 && !intel_syntax)
5b7c81bd 12710 return false;
8a6fb3f9 12711
5b7c81bd 12712 return true;
8a6fb3f9
JB
12713}
12714
af6bdddf 12715/* REG_STRING starts *before* REGISTER_PREFIX. */
252b5132
RH
12716
12717static const reg_entry *
4d1bb795 12718parse_real_register (char *reg_string, char **end_op)
252b5132 12719{
af6bdddf
AM
12720 char *s = reg_string;
12721 char *p;
252b5132
RH
12722 char reg_name_given[MAX_REG_NAME_SIZE + 1];
12723 const reg_entry *r;
12724
12725 /* Skip possible REGISTER_PREFIX and possible whitespace. */
12726 if (*s == REGISTER_PREFIX)
12727 ++s;
12728
12729 if (is_space_char (*s))
12730 ++s;
12731
12732 p = reg_name_given;
af6bdddf 12733 while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
252b5132
RH
12734 {
12735 if (p >= reg_name_given + MAX_REG_NAME_SIZE)
af6bdddf
AM
12736 return (const reg_entry *) NULL;
12737 s++;
252b5132
RH
12738 }
12739
6588847e
DN
12740 /* For naked regs, make sure that we are not dealing with an identifier.
12741 This prevents confusing an identifier like `eax_var' with register
12742 `eax'. */
12743 if (allow_naked_reg && identifier_chars[(unsigned char) *s])
12744 return (const reg_entry *) NULL;
12745
af6bdddf 12746 *end_op = s;
252b5132 12747
629310ab 12748 r = (const reg_entry *) str_hash_find (reg_hash, reg_name_given);
252b5132 12749
5f47d35b 12750 /* Handle floating point regs, allowing spaces in the (i) part. */
6288d05f 12751 if (r == reg_st0)
5f47d35b 12752 {
0e0eea78
JB
12753 if (!cpu_arch_flags.bitfield.cpu8087
12754 && !cpu_arch_flags.bitfield.cpu287
af32b722
JB
12755 && !cpu_arch_flags.bitfield.cpu387
12756 && !allow_pseudo_reg)
0e0eea78
JB
12757 return (const reg_entry *) NULL;
12758
5f47d35b
AM
12759 if (is_space_char (*s))
12760 ++s;
12761 if (*s == '(')
12762 {
af6bdddf 12763 ++s;
5f47d35b
AM
12764 if (is_space_char (*s))
12765 ++s;
12766 if (*s >= '0' && *s <= '7')
12767 {
db557034 12768 int fpr = *s - '0';
af6bdddf 12769 ++s;
5f47d35b
AM
12770 if (is_space_char (*s))
12771 ++s;
12772 if (*s == ')')
12773 {
12774 *end_op = s + 1;
6288d05f 12775 know (r[fpr].reg_num == fpr);
db557034 12776 return r + fpr;
5f47d35b 12777 }
5f47d35b 12778 }
47926f60 12779 /* We have "%st(" then garbage. */
5f47d35b
AM
12780 return (const reg_entry *) NULL;
12781 }
12782 }
12783
8a6fb3f9 12784 return r && check_register (r) ? r : NULL;
252b5132 12785}
4d1bb795
JB
12786
12787/* REG_STRING starts *before* REGISTER_PREFIX. */
12788
12789static const reg_entry *
12790parse_register (char *reg_string, char **end_op)
12791{
12792 const reg_entry *r;
12793
12794 if (*reg_string == REGISTER_PREFIX || allow_naked_reg)
12795 r = parse_real_register (reg_string, end_op);
12796 else
12797 r = NULL;
12798 if (!r)
12799 {
12800 char *save = input_line_pointer;
12801 char c;
12802 symbolS *symbolP;
12803
12804 input_line_pointer = reg_string;
d02603dc 12805 c = get_symbol_name (&reg_string);
4d1bb795
JB
12806 symbolP = symbol_find (reg_string);
12807 if (symbolP && S_GET_SEGMENT (symbolP) == reg_section)
12808 {
12809 const expressionS *e = symbol_get_value_expression (symbolP);
12810
0398aac5 12811 know (e->X_op == O_register);
4eed87de 12812 know (e->X_add_number >= 0
c3fe08fa 12813 && (valueT) e->X_add_number < i386_regtab_size);
4d1bb795 12814 r = i386_regtab + e->X_add_number;
8a6fb3f9
JB
12815 if (!check_register (r))
12816 {
12817 as_bad (_("register '%s%s' cannot be used here"),
12818 register_prefix, r->reg_name);
12819 r = &bad_reg;
12820 }
4d1bb795
JB
12821 *end_op = input_line_pointer;
12822 }
12823 *input_line_pointer = c;
12824 input_line_pointer = save;
12825 }
12826 return r;
12827}
12828
12829int
12830i386_parse_name (char *name, expressionS *e, char *nextcharP)
12831{
12832 const reg_entry *r;
12833 char *end = input_line_pointer;
12834
12835 *end = *nextcharP;
12836 r = parse_register (name, &input_line_pointer);
12837 if (r && end <= input_line_pointer)
12838 {
12839 *nextcharP = *input_line_pointer;
12840 *input_line_pointer = 0;
8a6fb3f9
JB
12841 if (r != &bad_reg)
12842 {
12843 e->X_op = O_register;
12844 e->X_add_number = r - i386_regtab;
12845 }
12846 else
12847 e->X_op = O_illegal;
4d1bb795
JB
12848 return 1;
12849 }
12850 input_line_pointer = end;
12851 *end = 0;
ee86248c 12852 return intel_syntax ? i386_intel_parse_name (name, e) : 0;
4d1bb795
JB
12853}
12854
12855void
12856md_operand (expressionS *e)
12857{
ee86248c
JB
12858 char *end;
12859 const reg_entry *r;
4d1bb795 12860
ee86248c
JB
12861 switch (*input_line_pointer)
12862 {
12863 case REGISTER_PREFIX:
12864 r = parse_real_register (input_line_pointer, &end);
4d1bb795
JB
12865 if (r)
12866 {
12867 e->X_op = O_register;
12868 e->X_add_number = r - i386_regtab;
12869 input_line_pointer = end;
12870 }
ee86248c
JB
12871 break;
12872
12873 case '[':
9c2799c2 12874 gas_assert (intel_syntax);
ee86248c
JB
12875 end = input_line_pointer++;
12876 expression (e);
12877 if (*input_line_pointer == ']')
12878 {
12879 ++input_line_pointer;
12880 e->X_op_symbol = make_expr_symbol (e);
12881 e->X_add_symbol = NULL;
12882 e->X_add_number = 0;
12883 e->X_op = O_index;
12884 }
12885 else
12886 {
12887 e->X_op = O_absent;
12888 input_line_pointer = end;
12889 }
12890 break;
4d1bb795
JB
12891 }
12892}
12893
252b5132 12894\f
4cc782b5 12895#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
b6f8c7c4 12896const char *md_shortopts = "kVQ:sqnO::";
252b5132 12897#else
b6f8c7c4 12898const char *md_shortopts = "qnO::";
252b5132 12899#endif
6e0b89ee 12900
3e73aa7c 12901#define OPTION_32 (OPTION_MD_BASE + 0)
b3b91714
AM
12902#define OPTION_64 (OPTION_MD_BASE + 1)
12903#define OPTION_DIVIDE (OPTION_MD_BASE + 2)
9103f4f4
L
12904#define OPTION_MARCH (OPTION_MD_BASE + 3)
12905#define OPTION_MTUNE (OPTION_MD_BASE + 4)
1efbbeb4
L
12906#define OPTION_MMNEMONIC (OPTION_MD_BASE + 5)
12907#define OPTION_MSYNTAX (OPTION_MD_BASE + 6)
12908#define OPTION_MINDEX_REG (OPTION_MD_BASE + 7)
12909#define OPTION_MNAKED_REG (OPTION_MD_BASE + 8)
bd5dea88 12910#define OPTION_MRELAX_RELOCATIONS (OPTION_MD_BASE + 9)
c0f3af97 12911#define OPTION_MSSE2AVX (OPTION_MD_BASE + 10)
daf50ae7 12912#define OPTION_MSSE_CHECK (OPTION_MD_BASE + 11)
7bab8ab5
JB
12913#define OPTION_MOPERAND_CHECK (OPTION_MD_BASE + 12)
12914#define OPTION_MAVXSCALAR (OPTION_MD_BASE + 13)
12915#define OPTION_X32 (OPTION_MD_BASE + 14)
7e8b059b 12916#define OPTION_MADD_BND_PREFIX (OPTION_MD_BASE + 15)
43234a1e
L
12917#define OPTION_MEVEXLIG (OPTION_MD_BASE + 16)
12918#define OPTION_MEVEXWIG (OPTION_MD_BASE + 17)
167ad85b 12919#define OPTION_MBIG_OBJ (OPTION_MD_BASE + 18)
d1982f93 12920#define OPTION_MOMIT_LOCK_PREFIX (OPTION_MD_BASE + 19)
d3d3c6db 12921#define OPTION_MEVEXRCIG (OPTION_MD_BASE + 20)
8dcea932 12922#define OPTION_MSHARED (OPTION_MD_BASE + 21)
5db04b09
L
12923#define OPTION_MAMD64 (OPTION_MD_BASE + 22)
12924#define OPTION_MINTEL64 (OPTION_MD_BASE + 23)
e4e00185 12925#define OPTION_MFENCE_AS_LOCK_ADD (OPTION_MD_BASE + 24)
b4a3a7b4 12926#define OPTION_X86_USED_NOTE (OPTION_MD_BASE + 25)
03751133 12927#define OPTION_MVEXWIG (OPTION_MD_BASE + 26)
e379e5f3
L
12928#define OPTION_MALIGN_BRANCH_BOUNDARY (OPTION_MD_BASE + 27)
12929#define OPTION_MALIGN_BRANCH_PREFIX_SIZE (OPTION_MD_BASE + 28)
12930#define OPTION_MALIGN_BRANCH (OPTION_MD_BASE + 29)
76cf450b 12931#define OPTION_MBRANCHES_WITH_32B_BOUNDARIES (OPTION_MD_BASE + 30)
ae531041
L
12932#define OPTION_MLFENCE_AFTER_LOAD (OPTION_MD_BASE + 31)
12933#define OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH (OPTION_MD_BASE + 32)
12934#define OPTION_MLFENCE_BEFORE_RET (OPTION_MD_BASE + 33)
b3b91714 12935
99ad8390
NC
12936struct option md_longopts[] =
12937{
3e73aa7c 12938 {"32", no_argument, NULL, OPTION_32},
321098a5 12939#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 12940 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c 12941 {"64", no_argument, NULL, OPTION_64},
351f65ca
L
12942#endif
12943#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 12944 {"x32", no_argument, NULL, OPTION_X32},
8dcea932 12945 {"mshared", no_argument, NULL, OPTION_MSHARED},
b4a3a7b4 12946 {"mx86-used-note", required_argument, NULL, OPTION_X86_USED_NOTE},
6e0b89ee 12947#endif
b3b91714 12948 {"divide", no_argument, NULL, OPTION_DIVIDE},
9103f4f4
L
12949 {"march", required_argument, NULL, OPTION_MARCH},
12950 {"mtune", required_argument, NULL, OPTION_MTUNE},
1efbbeb4
L
12951 {"mmnemonic", required_argument, NULL, OPTION_MMNEMONIC},
12952 {"msyntax", required_argument, NULL, OPTION_MSYNTAX},
12953 {"mindex-reg", no_argument, NULL, OPTION_MINDEX_REG},
12954 {"mnaked-reg", no_argument, NULL, OPTION_MNAKED_REG},
c0f3af97 12955 {"msse2avx", no_argument, NULL, OPTION_MSSE2AVX},
daf50ae7 12956 {"msse-check", required_argument, NULL, OPTION_MSSE_CHECK},
7bab8ab5 12957 {"moperand-check", required_argument, NULL, OPTION_MOPERAND_CHECK},
539f890d 12958 {"mavxscalar", required_argument, NULL, OPTION_MAVXSCALAR},
03751133 12959 {"mvexwig", required_argument, NULL, OPTION_MVEXWIG},
7e8b059b 12960 {"madd-bnd-prefix", no_argument, NULL, OPTION_MADD_BND_PREFIX},
43234a1e
L
12961 {"mevexlig", required_argument, NULL, OPTION_MEVEXLIG},
12962 {"mevexwig", required_argument, NULL, OPTION_MEVEXWIG},
167ad85b
TG
12963# if defined (TE_PE) || defined (TE_PEP)
12964 {"mbig-obj", no_argument, NULL, OPTION_MBIG_OBJ},
12965#endif
d1982f93 12966 {"momit-lock-prefix", required_argument, NULL, OPTION_MOMIT_LOCK_PREFIX},
e4e00185 12967 {"mfence-as-lock-add", required_argument, NULL, OPTION_MFENCE_AS_LOCK_ADD},
0cb4071e 12968 {"mrelax-relocations", required_argument, NULL, OPTION_MRELAX_RELOCATIONS},
d3d3c6db 12969 {"mevexrcig", required_argument, NULL, OPTION_MEVEXRCIG},
e379e5f3
L
12970 {"malign-branch-boundary", required_argument, NULL, OPTION_MALIGN_BRANCH_BOUNDARY},
12971 {"malign-branch-prefix-size", required_argument, NULL, OPTION_MALIGN_BRANCH_PREFIX_SIZE},
12972 {"malign-branch", required_argument, NULL, OPTION_MALIGN_BRANCH},
76cf450b 12973 {"mbranches-within-32B-boundaries", no_argument, NULL, OPTION_MBRANCHES_WITH_32B_BOUNDARIES},
ae531041
L
12974 {"mlfence-after-load", required_argument, NULL, OPTION_MLFENCE_AFTER_LOAD},
12975 {"mlfence-before-indirect-branch", required_argument, NULL,
12976 OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH},
12977 {"mlfence-before-ret", required_argument, NULL, OPTION_MLFENCE_BEFORE_RET},
5db04b09
L
12978 {"mamd64", no_argument, NULL, OPTION_MAMD64},
12979 {"mintel64", no_argument, NULL, OPTION_MINTEL64},
252b5132
RH
12980 {NULL, no_argument, NULL, 0}
12981};
12982size_t md_longopts_size = sizeof (md_longopts);
12983
12984int
17b9d67d 12985md_parse_option (int c, const char *arg)
252b5132 12986{
91d6fa6a 12987 unsigned int j;
e379e5f3 12988 char *arch, *next, *saved, *type;
9103f4f4 12989
252b5132
RH
12990 switch (c)
12991 {
12b55ccc
L
12992 case 'n':
12993 optimize_align_code = 0;
12994 break;
12995
a38cf1db
AM
12996 case 'q':
12997 quiet_warnings = 1;
252b5132
RH
12998 break;
12999
13000#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
a38cf1db
AM
13001 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
13002 should be emitted or not. FIXME: Not implemented. */
13003 case 'Q':
d4693039
JB
13004 if ((arg[0] != 'y' && arg[0] != 'n') || arg[1])
13005 return 0;
252b5132
RH
13006 break;
13007
13008 /* -V: SVR4 argument to print version ID. */
13009 case 'V':
13010 print_version_id ();
13011 break;
13012
a38cf1db
AM
13013 /* -k: Ignore for FreeBSD compatibility. */
13014 case 'k':
252b5132 13015 break;
4cc782b5
ILT
13016
13017 case 's':
13018 /* -s: On i386 Solaris, this tells the native assembler to use
29b0f896 13019 .stab instead of .stab.excl. We always use .stab anyhow. */
4cc782b5 13020 break;
8dcea932
L
13021
13022 case OPTION_MSHARED:
13023 shared = 1;
13024 break;
b4a3a7b4
L
13025
13026 case OPTION_X86_USED_NOTE:
13027 if (strcasecmp (arg, "yes") == 0)
13028 x86_used_note = 1;
13029 else if (strcasecmp (arg, "no") == 0)
13030 x86_used_note = 0;
13031 else
13032 as_fatal (_("invalid -mx86-used-note= option: `%s'"), arg);
13033 break;
13034
13035
99ad8390 13036#endif
321098a5 13037#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 13038 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c
JH
13039 case OPTION_64:
13040 {
13041 const char **list, **l;
13042
3e73aa7c
JH
13043 list = bfd_target_list ();
13044 for (l = list; *l != NULL; l++)
08dedd66 13045 if (startswith (*l, "elf64-x86-64")
99ad8390
NC
13046 || strcmp (*l, "coff-x86-64") == 0
13047 || strcmp (*l, "pe-x86-64") == 0
d382c579
TG
13048 || strcmp (*l, "pei-x86-64") == 0
13049 || strcmp (*l, "mach-o-x86-64") == 0)
6e0b89ee
AM
13050 {
13051 default_arch = "x86_64";
13052 break;
13053 }
3e73aa7c 13054 if (*l == NULL)
2b5d6a91 13055 as_fatal (_("no compiled in support for x86_64"));
3e73aa7c
JH
13056 free (list);
13057 }
13058 break;
13059#endif
252b5132 13060
351f65ca 13061#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 13062 case OPTION_X32:
351f65ca
L
13063 if (IS_ELF)
13064 {
13065 const char **list, **l;
13066
13067 list = bfd_target_list ();
13068 for (l = list; *l != NULL; l++)
08dedd66 13069 if (startswith (*l, "elf32-x86-64"))
351f65ca
L
13070 {
13071 default_arch = "x86_64:32";
13072 break;
13073 }
13074 if (*l == NULL)
2b5d6a91 13075 as_fatal (_("no compiled in support for 32bit x86_64"));
351f65ca
L
13076 free (list);
13077 }
13078 else
13079 as_fatal (_("32bit x86_64 is only supported for ELF"));
13080 break;
13081#endif
13082
6e0b89ee
AM
13083 case OPTION_32:
13084 default_arch = "i386";
13085 break;
13086
b3b91714
AM
13087 case OPTION_DIVIDE:
13088#ifdef SVR4_COMMENT_CHARS
13089 {
13090 char *n, *t;
13091 const char *s;
13092
add39d23 13093 n = XNEWVEC (char, strlen (i386_comment_chars) + 1);
b3b91714
AM
13094 t = n;
13095 for (s = i386_comment_chars; *s != '\0'; s++)
13096 if (*s != '/')
13097 *t++ = *s;
13098 *t = '\0';
13099 i386_comment_chars = n;
13100 }
13101#endif
13102 break;
13103
9103f4f4 13104 case OPTION_MARCH:
293f5f65
L
13105 saved = xstrdup (arg);
13106 arch = saved;
13107 /* Allow -march=+nosse. */
13108 if (*arch == '+')
13109 arch++;
6305a203 13110 do
9103f4f4 13111 {
6305a203 13112 if (*arch == '.')
2b5d6a91 13113 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
13114 next = strchr (arch, '+');
13115 if (next)
13116 *next++ = '\0';
91d6fa6a 13117 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 13118 {
91d6fa6a 13119 if (strcmp (arch, cpu_arch [j].name) == 0)
ccc9c027 13120 {
6305a203 13121 /* Processor. */
1ded5609
JB
13122 if (! cpu_arch[j].flags.bitfield.cpui386)
13123 continue;
13124
91d6fa6a 13125 cpu_arch_name = cpu_arch[j].name;
6305a203 13126 cpu_sub_arch_name = NULL;
91d6fa6a
NC
13127 cpu_arch_flags = cpu_arch[j].flags;
13128 cpu_arch_isa = cpu_arch[j].type;
13129 cpu_arch_isa_flags = cpu_arch[j].flags;
6305a203
L
13130 if (!cpu_arch_tune_set)
13131 {
13132 cpu_arch_tune = cpu_arch_isa;
13133 cpu_arch_tune_flags = cpu_arch_isa_flags;
13134 }
13135 break;
13136 }
91d6fa6a
NC
13137 else if (*cpu_arch [j].name == '.'
13138 && strcmp (arch, cpu_arch [j].name + 1) == 0)
6305a203 13139 {
33eaf5de 13140 /* ISA extension. */
6305a203 13141 i386_cpu_flags flags;
309d3373 13142
293f5f65
L
13143 flags = cpu_flags_or (cpu_arch_flags,
13144 cpu_arch[j].flags);
81486035 13145
5b64d091 13146 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
6305a203
L
13147 {
13148 if (cpu_sub_arch_name)
13149 {
13150 char *name = cpu_sub_arch_name;
13151 cpu_sub_arch_name = concat (name,
91d6fa6a 13152 cpu_arch[j].name,
1bf57e9f 13153 (const char *) NULL);
6305a203
L
13154 free (name);
13155 }
13156 else
91d6fa6a 13157 cpu_sub_arch_name = xstrdup (cpu_arch[j].name);
6305a203 13158 cpu_arch_flags = flags;
a586129e 13159 cpu_arch_isa_flags = flags;
6305a203 13160 }
0089dace
L
13161 else
13162 cpu_arch_isa_flags
13163 = cpu_flags_or (cpu_arch_isa_flags,
13164 cpu_arch[j].flags);
6305a203 13165 break;
ccc9c027 13166 }
9103f4f4 13167 }
6305a203 13168
293f5f65
L
13169 if (j >= ARRAY_SIZE (cpu_arch))
13170 {
33eaf5de 13171 /* Disable an ISA extension. */
293f5f65
L
13172 for (j = 0; j < ARRAY_SIZE (cpu_noarch); j++)
13173 if (strcmp (arch, cpu_noarch [j].name) == 0)
13174 {
13175 i386_cpu_flags flags;
13176
13177 flags = cpu_flags_and_not (cpu_arch_flags,
13178 cpu_noarch[j].flags);
13179 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
13180 {
13181 if (cpu_sub_arch_name)
13182 {
13183 char *name = cpu_sub_arch_name;
13184 cpu_sub_arch_name = concat (arch,
13185 (const char *) NULL);
13186 free (name);
13187 }
13188 else
13189 cpu_sub_arch_name = xstrdup (arch);
13190 cpu_arch_flags = flags;
13191 cpu_arch_isa_flags = flags;
13192 }
13193 break;
13194 }
13195
13196 if (j >= ARRAY_SIZE (cpu_noarch))
13197 j = ARRAY_SIZE (cpu_arch);
13198 }
13199
91d6fa6a 13200 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 13201 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
13202
13203 arch = next;
9103f4f4 13204 }
293f5f65
L
13205 while (next != NULL);
13206 free (saved);
9103f4f4
L
13207 break;
13208
13209 case OPTION_MTUNE:
13210 if (*arg == '.')
2b5d6a91 13211 as_fatal (_("invalid -mtune= option: `%s'"), arg);
91d6fa6a 13212 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 13213 {
91d6fa6a 13214 if (strcmp (arg, cpu_arch [j].name) == 0)
9103f4f4 13215 {
ccc9c027 13216 cpu_arch_tune_set = 1;
91d6fa6a
NC
13217 cpu_arch_tune = cpu_arch [j].type;
13218 cpu_arch_tune_flags = cpu_arch[j].flags;
9103f4f4
L
13219 break;
13220 }
13221 }
91d6fa6a 13222 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 13223 as_fatal (_("invalid -mtune= option: `%s'"), arg);
9103f4f4
L
13224 break;
13225
1efbbeb4
L
13226 case OPTION_MMNEMONIC:
13227 if (strcasecmp (arg, "att") == 0)
13228 intel_mnemonic = 0;
13229 else if (strcasecmp (arg, "intel") == 0)
13230 intel_mnemonic = 1;
13231 else
2b5d6a91 13232 as_fatal (_("invalid -mmnemonic= option: `%s'"), arg);
1efbbeb4
L
13233 break;
13234
13235 case OPTION_MSYNTAX:
13236 if (strcasecmp (arg, "att") == 0)
13237 intel_syntax = 0;
13238 else if (strcasecmp (arg, "intel") == 0)
13239 intel_syntax = 1;
13240 else
2b5d6a91 13241 as_fatal (_("invalid -msyntax= option: `%s'"), arg);
1efbbeb4
L
13242 break;
13243
13244 case OPTION_MINDEX_REG:
13245 allow_index_reg = 1;
13246 break;
13247
13248 case OPTION_MNAKED_REG:
13249 allow_naked_reg = 1;
13250 break;
13251
c0f3af97
L
13252 case OPTION_MSSE2AVX:
13253 sse2avx = 1;
13254 break;
13255
daf50ae7
L
13256 case OPTION_MSSE_CHECK:
13257 if (strcasecmp (arg, "error") == 0)
7bab8ab5 13258 sse_check = check_error;
daf50ae7 13259 else if (strcasecmp (arg, "warning") == 0)
7bab8ab5 13260 sse_check = check_warning;
daf50ae7 13261 else if (strcasecmp (arg, "none") == 0)
7bab8ab5 13262 sse_check = check_none;
daf50ae7 13263 else
2b5d6a91 13264 as_fatal (_("invalid -msse-check= option: `%s'"), arg);
daf50ae7
L
13265 break;
13266
7bab8ab5
JB
13267 case OPTION_MOPERAND_CHECK:
13268 if (strcasecmp (arg, "error") == 0)
13269 operand_check = check_error;
13270 else if (strcasecmp (arg, "warning") == 0)
13271 operand_check = check_warning;
13272 else if (strcasecmp (arg, "none") == 0)
13273 operand_check = check_none;
13274 else
13275 as_fatal (_("invalid -moperand-check= option: `%s'"), arg);
13276 break;
13277
539f890d
L
13278 case OPTION_MAVXSCALAR:
13279 if (strcasecmp (arg, "128") == 0)
13280 avxscalar = vex128;
13281 else if (strcasecmp (arg, "256") == 0)
13282 avxscalar = vex256;
13283 else
2b5d6a91 13284 as_fatal (_("invalid -mavxscalar= option: `%s'"), arg);
539f890d
L
13285 break;
13286
03751133
L
13287 case OPTION_MVEXWIG:
13288 if (strcmp (arg, "0") == 0)
40c9c8de 13289 vexwig = vexw0;
03751133 13290 else if (strcmp (arg, "1") == 0)
40c9c8de 13291 vexwig = vexw1;
03751133
L
13292 else
13293 as_fatal (_("invalid -mvexwig= option: `%s'"), arg);
13294 break;
13295
7e8b059b
L
13296 case OPTION_MADD_BND_PREFIX:
13297 add_bnd_prefix = 1;
13298 break;
13299
43234a1e
L
13300 case OPTION_MEVEXLIG:
13301 if (strcmp (arg, "128") == 0)
13302 evexlig = evexl128;
13303 else if (strcmp (arg, "256") == 0)
13304 evexlig = evexl256;
13305 else if (strcmp (arg, "512") == 0)
13306 evexlig = evexl512;
13307 else
13308 as_fatal (_("invalid -mevexlig= option: `%s'"), arg);
13309 break;
13310
d3d3c6db
IT
13311 case OPTION_MEVEXRCIG:
13312 if (strcmp (arg, "rne") == 0)
13313 evexrcig = rne;
13314 else if (strcmp (arg, "rd") == 0)
13315 evexrcig = rd;
13316 else if (strcmp (arg, "ru") == 0)
13317 evexrcig = ru;
13318 else if (strcmp (arg, "rz") == 0)
13319 evexrcig = rz;
13320 else
13321 as_fatal (_("invalid -mevexrcig= option: `%s'"), arg);
13322 break;
13323
43234a1e
L
13324 case OPTION_MEVEXWIG:
13325 if (strcmp (arg, "0") == 0)
13326 evexwig = evexw0;
13327 else if (strcmp (arg, "1") == 0)
13328 evexwig = evexw1;
13329 else
13330 as_fatal (_("invalid -mevexwig= option: `%s'"), arg);
13331 break;
13332
167ad85b
TG
13333# if defined (TE_PE) || defined (TE_PEP)
13334 case OPTION_MBIG_OBJ:
13335 use_big_obj = 1;
13336 break;
13337#endif
13338
d1982f93 13339 case OPTION_MOMIT_LOCK_PREFIX:
d022bddd
IT
13340 if (strcasecmp (arg, "yes") == 0)
13341 omit_lock_prefix = 1;
13342 else if (strcasecmp (arg, "no") == 0)
13343 omit_lock_prefix = 0;
13344 else
13345 as_fatal (_("invalid -momit-lock-prefix= option: `%s'"), arg);
13346 break;
13347
e4e00185
AS
13348 case OPTION_MFENCE_AS_LOCK_ADD:
13349 if (strcasecmp (arg, "yes") == 0)
13350 avoid_fence = 1;
13351 else if (strcasecmp (arg, "no") == 0)
13352 avoid_fence = 0;
13353 else
13354 as_fatal (_("invalid -mfence-as-lock-add= option: `%s'"), arg);
13355 break;
13356
ae531041
L
13357 case OPTION_MLFENCE_AFTER_LOAD:
13358 if (strcasecmp (arg, "yes") == 0)
13359 lfence_after_load = 1;
13360 else if (strcasecmp (arg, "no") == 0)
13361 lfence_after_load = 0;
13362 else
13363 as_fatal (_("invalid -mlfence-after-load= option: `%s'"), arg);
13364 break;
13365
13366 case OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH:
13367 if (strcasecmp (arg, "all") == 0)
a09f656b 13368 {
13369 lfence_before_indirect_branch = lfence_branch_all;
13370 if (lfence_before_ret == lfence_before_ret_none)
13371 lfence_before_ret = lfence_before_ret_shl;
13372 }
ae531041
L
13373 else if (strcasecmp (arg, "memory") == 0)
13374 lfence_before_indirect_branch = lfence_branch_memory;
13375 else if (strcasecmp (arg, "register") == 0)
13376 lfence_before_indirect_branch = lfence_branch_register;
13377 else if (strcasecmp (arg, "none") == 0)
13378 lfence_before_indirect_branch = lfence_branch_none;
13379 else
13380 as_fatal (_("invalid -mlfence-before-indirect-branch= option: `%s'"),
13381 arg);
13382 break;
13383
13384 case OPTION_MLFENCE_BEFORE_RET:
13385 if (strcasecmp (arg, "or") == 0)
13386 lfence_before_ret = lfence_before_ret_or;
13387 else if (strcasecmp (arg, "not") == 0)
13388 lfence_before_ret = lfence_before_ret_not;
a09f656b 13389 else if (strcasecmp (arg, "shl") == 0 || strcasecmp (arg, "yes") == 0)
13390 lfence_before_ret = lfence_before_ret_shl;
ae531041
L
13391 else if (strcasecmp (arg, "none") == 0)
13392 lfence_before_ret = lfence_before_ret_none;
13393 else
13394 as_fatal (_("invalid -mlfence-before-ret= option: `%s'"),
13395 arg);
13396 break;
13397
0cb4071e
L
13398 case OPTION_MRELAX_RELOCATIONS:
13399 if (strcasecmp (arg, "yes") == 0)
13400 generate_relax_relocations = 1;
13401 else if (strcasecmp (arg, "no") == 0)
13402 generate_relax_relocations = 0;
13403 else
13404 as_fatal (_("invalid -mrelax-relocations= option: `%s'"), arg);
13405 break;
13406
e379e5f3
L
13407 case OPTION_MALIGN_BRANCH_BOUNDARY:
13408 {
13409 char *end;
13410 long int align = strtoul (arg, &end, 0);
13411 if (*end == '\0')
13412 {
13413 if (align == 0)
13414 {
13415 align_branch_power = 0;
13416 break;
13417 }
13418 else if (align >= 16)
13419 {
13420 int align_power;
13421 for (align_power = 0;
13422 (align & 1) == 0;
13423 align >>= 1, align_power++)
13424 continue;
13425 /* Limit alignment power to 31. */
13426 if (align == 1 && align_power < 32)
13427 {
13428 align_branch_power = align_power;
13429 break;
13430 }
13431 }
13432 }
13433 as_fatal (_("invalid -malign-branch-boundary= value: %s"), arg);
13434 }
13435 break;
13436
13437 case OPTION_MALIGN_BRANCH_PREFIX_SIZE:
13438 {
13439 char *end;
13440 int align = strtoul (arg, &end, 0);
13441 /* Some processors only support 5 prefixes. */
13442 if (*end == '\0' && align >= 0 && align < 6)
13443 {
13444 align_branch_prefix_size = align;
13445 break;
13446 }
13447 as_fatal (_("invalid -malign-branch-prefix-size= value: %s"),
13448 arg);
13449 }
13450 break;
13451
13452 case OPTION_MALIGN_BRANCH:
13453 align_branch = 0;
13454 saved = xstrdup (arg);
13455 type = saved;
13456 do
13457 {
13458 next = strchr (type, '+');
13459 if (next)
13460 *next++ = '\0';
13461 if (strcasecmp (type, "jcc") == 0)
13462 align_branch |= align_branch_jcc_bit;
13463 else if (strcasecmp (type, "fused") == 0)
13464 align_branch |= align_branch_fused_bit;
13465 else if (strcasecmp (type, "jmp") == 0)
13466 align_branch |= align_branch_jmp_bit;
13467 else if (strcasecmp (type, "call") == 0)
13468 align_branch |= align_branch_call_bit;
13469 else if (strcasecmp (type, "ret") == 0)
13470 align_branch |= align_branch_ret_bit;
13471 else if (strcasecmp (type, "indirect") == 0)
13472 align_branch |= align_branch_indirect_bit;
13473 else
13474 as_fatal (_("invalid -malign-branch= option: `%s'"), arg);
13475 type = next;
13476 }
13477 while (next != NULL);
13478 free (saved);
13479 break;
13480
76cf450b
L
13481 case OPTION_MBRANCHES_WITH_32B_BOUNDARIES:
13482 align_branch_power = 5;
13483 align_branch_prefix_size = 5;
13484 align_branch = (align_branch_jcc_bit
13485 | align_branch_fused_bit
13486 | align_branch_jmp_bit);
13487 break;
13488
5db04b09 13489 case OPTION_MAMD64:
4b5aaf5f 13490 isa64 = amd64;
5db04b09
L
13491 break;
13492
13493 case OPTION_MINTEL64:
4b5aaf5f 13494 isa64 = intel64;
5db04b09
L
13495 break;
13496
b6f8c7c4
L
13497 case 'O':
13498 if (arg == NULL)
13499 {
13500 optimize = 1;
13501 /* Turn off -Os. */
13502 optimize_for_space = 0;
13503 }
13504 else if (*arg == 's')
13505 {
13506 optimize_for_space = 1;
13507 /* Turn on all encoding optimizations. */
41fd2579 13508 optimize = INT_MAX;
b6f8c7c4
L
13509 }
13510 else
13511 {
13512 optimize = atoi (arg);
13513 /* Turn off -Os. */
13514 optimize_for_space = 0;
13515 }
13516 break;
13517
252b5132
RH
13518 default:
13519 return 0;
13520 }
13521 return 1;
13522}
13523
8a2c8fef
L
13524#define MESSAGE_TEMPLATE \
13525" "
13526
293f5f65
L
13527static char *
13528output_message (FILE *stream, char *p, char *message, char *start,
13529 int *left_p, const char *name, int len)
13530{
13531 int size = sizeof (MESSAGE_TEMPLATE);
13532 int left = *left_p;
13533
13534 /* Reserve 2 spaces for ", " or ",\0" */
13535 left -= len + 2;
13536
13537 /* Check if there is any room. */
13538 if (left >= 0)
13539 {
13540 if (p != start)
13541 {
13542 *p++ = ',';
13543 *p++ = ' ';
13544 }
13545 p = mempcpy (p, name, len);
13546 }
13547 else
13548 {
13549 /* Output the current message now and start a new one. */
13550 *p++ = ',';
13551 *p = '\0';
13552 fprintf (stream, "%s\n", message);
13553 p = start;
13554 left = size - (start - message) - len - 2;
13555
13556 gas_assert (left >= 0);
13557
13558 p = mempcpy (p, name, len);
13559 }
13560
13561 *left_p = left;
13562 return p;
13563}
13564
8a2c8fef 13565static void
1ded5609 13566show_arch (FILE *stream, int ext, int check)
8a2c8fef
L
13567{
13568 static char message[] = MESSAGE_TEMPLATE;
13569 char *start = message + 27;
13570 char *p;
13571 int size = sizeof (MESSAGE_TEMPLATE);
13572 int left;
13573 const char *name;
13574 int len;
13575 unsigned int j;
13576
13577 p = start;
13578 left = size - (start - message);
13579 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13580 {
13581 /* Should it be skipped? */
13582 if (cpu_arch [j].skip)
13583 continue;
13584
13585 name = cpu_arch [j].name;
13586 len = cpu_arch [j].len;
13587 if (*name == '.')
13588 {
13589 /* It is an extension. Skip if we aren't asked to show it. */
13590 if (ext)
13591 {
13592 name++;
13593 len--;
13594 }
13595 else
13596 continue;
13597 }
13598 else if (ext)
13599 {
13600 /* It is an processor. Skip if we show only extension. */
13601 continue;
13602 }
1ded5609
JB
13603 else if (check && ! cpu_arch[j].flags.bitfield.cpui386)
13604 {
13605 /* It is an impossible processor - skip. */
13606 continue;
13607 }
8a2c8fef 13608
293f5f65 13609 p = output_message (stream, p, message, start, &left, name, len);
8a2c8fef
L
13610 }
13611
293f5f65
L
13612 /* Display disabled extensions. */
13613 if (ext)
13614 for (j = 0; j < ARRAY_SIZE (cpu_noarch); j++)
13615 {
13616 name = cpu_noarch [j].name;
13617 len = cpu_noarch [j].len;
13618 p = output_message (stream, p, message, start, &left, name,
13619 len);
13620 }
13621
8a2c8fef
L
13622 *p = '\0';
13623 fprintf (stream, "%s\n", message);
13624}
13625
252b5132 13626void
8a2c8fef 13627md_show_usage (FILE *stream)
252b5132 13628{
4cc782b5
ILT
13629#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13630 fprintf (stream, _("\
d4693039 13631 -Qy, -Qn ignored\n\
a38cf1db 13632 -V print assembler version number\n\
b3b91714
AM
13633 -k ignored\n"));
13634#endif
13635 fprintf (stream, _("\
12b55ccc 13636 -n Do not optimize code alignment\n\
b3b91714
AM
13637 -q quieten some warnings\n"));
13638#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13639 fprintf (stream, _("\
a38cf1db 13640 -s ignored\n"));
b3b91714 13641#endif
d7f449c0
L
13642#if defined BFD64 && (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
13643 || defined (TE_PE) || defined (TE_PEP))
751d281c 13644 fprintf (stream, _("\
570561f7 13645 --32/--64/--x32 generate 32bit/64bit/x32 code\n"));
751d281c 13646#endif
b3b91714
AM
13647#ifdef SVR4_COMMENT_CHARS
13648 fprintf (stream, _("\
13649 --divide do not treat `/' as a comment character\n"));
a38cf1db
AM
13650#else
13651 fprintf (stream, _("\
b3b91714 13652 --divide ignored\n"));
4cc782b5 13653#endif
9103f4f4 13654 fprintf (stream, _("\
6305a203 13655 -march=CPU[,+EXTENSION...]\n\
8a2c8fef 13656 generate code for CPU and EXTENSION, CPU is one of:\n"));
1ded5609 13657 show_arch (stream, 0, 1);
8a2c8fef
L
13658 fprintf (stream, _("\
13659 EXTENSION is combination of:\n"));
1ded5609 13660 show_arch (stream, 1, 0);
6305a203 13661 fprintf (stream, _("\
8a2c8fef 13662 -mtune=CPU optimize for CPU, CPU is one of:\n"));
1ded5609 13663 show_arch (stream, 0, 0);
ba104c83 13664 fprintf (stream, _("\
c0f3af97
L
13665 -msse2avx encode SSE instructions with VEX prefix\n"));
13666 fprintf (stream, _("\
7c5c05ef 13667 -msse-check=[none|error|warning] (default: warning)\n\
daf50ae7
L
13668 check SSE instructions\n"));
13669 fprintf (stream, _("\
7c5c05ef 13670 -moperand-check=[none|error|warning] (default: warning)\n\
7bab8ab5
JB
13671 check operand combinations for validity\n"));
13672 fprintf (stream, _("\
7c5c05ef
L
13673 -mavxscalar=[128|256] (default: 128)\n\
13674 encode scalar AVX instructions with specific vector\n\
539f890d
L
13675 length\n"));
13676 fprintf (stream, _("\
03751133
L
13677 -mvexwig=[0|1] (default: 0)\n\
13678 encode VEX instructions with specific VEX.W value\n\
13679 for VEX.W bit ignored instructions\n"));
13680 fprintf (stream, _("\
7c5c05ef
L
13681 -mevexlig=[128|256|512] (default: 128)\n\
13682 encode scalar EVEX instructions with specific vector\n\
43234a1e
L
13683 length\n"));
13684 fprintf (stream, _("\
7c5c05ef
L
13685 -mevexwig=[0|1] (default: 0)\n\
13686 encode EVEX instructions with specific EVEX.W value\n\
43234a1e
L
13687 for EVEX.W bit ignored instructions\n"));
13688 fprintf (stream, _("\
7c5c05ef 13689 -mevexrcig=[rne|rd|ru|rz] (default: rne)\n\
d3d3c6db
IT
13690 encode EVEX instructions with specific EVEX.RC value\n\
13691 for SAE-only ignored instructions\n"));
13692 fprintf (stream, _("\
7c5c05ef
L
13693 -mmnemonic=[att|intel] "));
13694 if (SYSV386_COMPAT)
13695 fprintf (stream, _("(default: att)\n"));
13696 else
13697 fprintf (stream, _("(default: intel)\n"));
13698 fprintf (stream, _("\
13699 use AT&T/Intel mnemonic\n"));
ba104c83 13700 fprintf (stream, _("\
7c5c05ef
L
13701 -msyntax=[att|intel] (default: att)\n\
13702 use AT&T/Intel syntax\n"));
ba104c83
L
13703 fprintf (stream, _("\
13704 -mindex-reg support pseudo index registers\n"));
13705 fprintf (stream, _("\
13706 -mnaked-reg don't require `%%' prefix for registers\n"));
13707 fprintf (stream, _("\
7e8b059b 13708 -madd-bnd-prefix add BND prefix for all valid branches\n"));
b4a3a7b4 13709#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8dcea932
L
13710 fprintf (stream, _("\
13711 -mshared disable branch optimization for shared code\n"));
b4a3a7b4
L
13712 fprintf (stream, _("\
13713 -mx86-used-note=[no|yes] "));
13714 if (DEFAULT_X86_USED_NOTE)
13715 fprintf (stream, _("(default: yes)\n"));
13716 else
13717 fprintf (stream, _("(default: no)\n"));
13718 fprintf (stream, _("\
13719 generate x86 used ISA and feature properties\n"));
13720#endif
13721#if defined (TE_PE) || defined (TE_PEP)
167ad85b
TG
13722 fprintf (stream, _("\
13723 -mbig-obj generate big object files\n"));
13724#endif
d022bddd 13725 fprintf (stream, _("\
7c5c05ef 13726 -momit-lock-prefix=[no|yes] (default: no)\n\
d022bddd 13727 strip all lock prefixes\n"));
5db04b09 13728 fprintf (stream, _("\
7c5c05ef 13729 -mfence-as-lock-add=[no|yes] (default: no)\n\
e4e00185
AS
13730 encode lfence, mfence and sfence as\n\
13731 lock addl $0x0, (%%{re}sp)\n"));
13732 fprintf (stream, _("\
7c5c05ef
L
13733 -mrelax-relocations=[no|yes] "));
13734 if (DEFAULT_GENERATE_X86_RELAX_RELOCATIONS)
13735 fprintf (stream, _("(default: yes)\n"));
13736 else
13737 fprintf (stream, _("(default: no)\n"));
13738 fprintf (stream, _("\
0cb4071e
L
13739 generate relax relocations\n"));
13740 fprintf (stream, _("\
e379e5f3
L
13741 -malign-branch-boundary=NUM (default: 0)\n\
13742 align branches within NUM byte boundary\n"));
13743 fprintf (stream, _("\
13744 -malign-branch=TYPE[+TYPE...] (default: jcc+fused+jmp)\n\
13745 TYPE is combination of jcc, fused, jmp, call, ret,\n\
13746 indirect\n\
13747 specify types of branches to align\n"));
13748 fprintf (stream, _("\
13749 -malign-branch-prefix-size=NUM (default: 5)\n\
13750 align branches with NUM prefixes per instruction\n"));
13751 fprintf (stream, _("\
76cf450b
L
13752 -mbranches-within-32B-boundaries\n\
13753 align branches within 32 byte boundary\n"));
13754 fprintf (stream, _("\
ae531041
L
13755 -mlfence-after-load=[no|yes] (default: no)\n\
13756 generate lfence after load\n"));
13757 fprintf (stream, _("\
13758 -mlfence-before-indirect-branch=[none|all|register|memory] (default: none)\n\
13759 generate lfence before indirect near branch\n"));
13760 fprintf (stream, _("\
a09f656b 13761 -mlfence-before-ret=[none|or|not|shl|yes] (default: none)\n\
ae531041
L
13762 generate lfence before ret\n"));
13763 fprintf (stream, _("\
7c5c05ef 13764 -mamd64 accept only AMD64 ISA [default]\n"));
5db04b09
L
13765 fprintf (stream, _("\
13766 -mintel64 accept only Intel64 ISA\n"));
252b5132
RH
13767}
13768
3e73aa7c 13769#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
321098a5 13770 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
e57f8c65 13771 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
252b5132
RH
13772
13773/* Pick the target format to use. */
13774
47926f60 13775const char *
e3bb37b5 13776i386_target_format (void)
252b5132 13777{
d34049e8 13778 if (startswith (default_arch, "x86_64"))
351f65ca
L
13779 {
13780 update_code_flag (CODE_64BIT, 1);
13781 if (default_arch[6] == '\0')
7f56bc95 13782 x86_elf_abi = X86_64_ABI;
351f65ca 13783 else
7f56bc95 13784 x86_elf_abi = X86_64_X32_ABI;
351f65ca 13785 }
3e73aa7c 13786 else if (!strcmp (default_arch, "i386"))
78f12dd3 13787 update_code_flag (CODE_32BIT, 1);
5197d474
L
13788 else if (!strcmp (default_arch, "iamcu"))
13789 {
13790 update_code_flag (CODE_32BIT, 1);
13791 if (cpu_arch_isa == PROCESSOR_UNKNOWN)
13792 {
13793 static const i386_cpu_flags iamcu_flags = CPU_IAMCU_FLAGS;
13794 cpu_arch_name = "iamcu";
13795 cpu_sub_arch_name = NULL;
13796 cpu_arch_flags = iamcu_flags;
13797 cpu_arch_isa = PROCESSOR_IAMCU;
13798 cpu_arch_isa_flags = iamcu_flags;
13799 if (!cpu_arch_tune_set)
13800 {
13801 cpu_arch_tune = cpu_arch_isa;
13802 cpu_arch_tune_flags = cpu_arch_isa_flags;
13803 }
13804 }
8d471ec1 13805 else if (cpu_arch_isa != PROCESSOR_IAMCU)
5197d474
L
13806 as_fatal (_("Intel MCU doesn't support `%s' architecture"),
13807 cpu_arch_name);
13808 }
3e73aa7c 13809 else
2b5d6a91 13810 as_fatal (_("unknown architecture"));
89507696
JB
13811
13812 if (cpu_flags_all_zero (&cpu_arch_isa_flags))
13813 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].flags;
13814 if (cpu_flags_all_zero (&cpu_arch_tune_flags))
13815 cpu_arch_tune_flags = cpu_arch[flag_code == CODE_64BIT].flags;
13816
252b5132
RH
13817 switch (OUTPUT_FLAVOR)
13818 {
9384f2ff 13819#if defined (OBJ_MAYBE_AOUT) || defined (OBJ_AOUT)
4c63da97 13820 case bfd_target_aout_flavour:
47926f60 13821 return AOUT_TARGET_FORMAT;
4c63da97 13822#endif
9384f2ff
AM
13823#if defined (OBJ_MAYBE_COFF) || defined (OBJ_COFF)
13824# if defined (TE_PE) || defined (TE_PEP)
13825 case bfd_target_coff_flavour:
167ad85b
TG
13826 if (flag_code == CODE_64BIT)
13827 return use_big_obj ? "pe-bigobj-x86-64" : "pe-x86-64";
13828 else
251dae91 13829 return use_big_obj ? "pe-bigobj-i386" : "pe-i386";
9384f2ff 13830# elif defined (TE_GO32)
0561d57c
JK
13831 case bfd_target_coff_flavour:
13832 return "coff-go32";
9384f2ff 13833# else
252b5132
RH
13834 case bfd_target_coff_flavour:
13835 return "coff-i386";
9384f2ff 13836# endif
4c63da97 13837#endif
3e73aa7c 13838#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
252b5132 13839 case bfd_target_elf_flavour:
3e73aa7c 13840 {
351f65ca
L
13841 const char *format;
13842
13843 switch (x86_elf_abi)
4fa24527 13844 {
351f65ca
L
13845 default:
13846 format = ELF_TARGET_FORMAT;
e379e5f3
L
13847#ifndef TE_SOLARIS
13848 tls_get_addr = "___tls_get_addr";
13849#endif
351f65ca 13850 break;
7f56bc95 13851 case X86_64_ABI:
351f65ca 13852 use_rela_relocations = 1;
4fa24527 13853 object_64bit = 1;
e379e5f3
L
13854#ifndef TE_SOLARIS
13855 tls_get_addr = "__tls_get_addr";
13856#endif
351f65ca
L
13857 format = ELF_TARGET_FORMAT64;
13858 break;
7f56bc95 13859 case X86_64_X32_ABI:
4fa24527 13860 use_rela_relocations = 1;
351f65ca 13861 object_64bit = 1;
e379e5f3
L
13862#ifndef TE_SOLARIS
13863 tls_get_addr = "__tls_get_addr";
13864#endif
862be3fb 13865 disallow_64bit_reloc = 1;
351f65ca
L
13866 format = ELF_TARGET_FORMAT32;
13867 break;
4fa24527 13868 }
3632d14b 13869 if (cpu_arch_isa == PROCESSOR_L1OM)
8a9036a4 13870 {
7f56bc95 13871 if (x86_elf_abi != X86_64_ABI)
8a9036a4
L
13872 as_fatal (_("Intel L1OM is 64bit only"));
13873 return ELF_TARGET_L1OM_FORMAT;
13874 }
b49f93f6 13875 else if (cpu_arch_isa == PROCESSOR_K1OM)
7a9068fe
L
13876 {
13877 if (x86_elf_abi != X86_64_ABI)
13878 as_fatal (_("Intel K1OM is 64bit only"));
13879 return ELF_TARGET_K1OM_FORMAT;
13880 }
81486035
L
13881 else if (cpu_arch_isa == PROCESSOR_IAMCU)
13882 {
13883 if (x86_elf_abi != I386_ABI)
13884 as_fatal (_("Intel MCU is 32bit only"));
13885 return ELF_TARGET_IAMCU_FORMAT;
13886 }
8a9036a4 13887 else
351f65ca 13888 return format;
3e73aa7c 13889 }
e57f8c65
TG
13890#endif
13891#if defined (OBJ_MACH_O)
13892 case bfd_target_mach_o_flavour:
d382c579
TG
13893 if (flag_code == CODE_64BIT)
13894 {
13895 use_rela_relocations = 1;
13896 object_64bit = 1;
13897 return "mach-o-x86-64";
13898 }
13899 else
13900 return "mach-o-i386";
4c63da97 13901#endif
252b5132
RH
13902 default:
13903 abort ();
13904 return NULL;
13905 }
13906}
13907
47926f60 13908#endif /* OBJ_MAYBE_ more than one */
252b5132 13909\f
252b5132 13910symbolS *
7016a5d5 13911md_undefined_symbol (char *name)
252b5132 13912{
18dc2407
ILT
13913 if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
13914 && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
13915 && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
13916 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
24eab124
AM
13917 {
13918 if (!GOT_symbol)
13919 {
13920 if (symbol_find (name))
13921 as_bad (_("GOT already in symbol table"));
13922 GOT_symbol = symbol_new (name, undefined_section,
e01e1cee 13923 &zero_address_frag, 0);
24eab124
AM
13924 };
13925 return GOT_symbol;
13926 }
252b5132
RH
13927 return 0;
13928}
13929
13930/* Round up a section size to the appropriate boundary. */
47926f60 13931
252b5132 13932valueT
7016a5d5 13933md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
252b5132 13934{
4c63da97
AM
13935#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
13936 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
13937 {
13938 /* For a.out, force the section size to be aligned. If we don't do
13939 this, BFD will align it for us, but it will not write out the
13940 final bytes of the section. This may be a bug in BFD, but it is
13941 easier to fix it here since that is how the other a.out targets
13942 work. */
13943 int align;
13944
fd361982 13945 align = bfd_section_alignment (segment);
8d3842cd 13946 size = ((size + (1 << align) - 1) & (-((valueT) 1 << align)));
4c63da97 13947 }
252b5132
RH
13948#endif
13949
13950 return size;
13951}
13952
13953/* On the i386, PC-relative offsets are relative to the start of the
13954 next instruction. That is, the address of the offset, plus its
13955 size, since the offset is always the last part of the insn. */
13956
13957long
e3bb37b5 13958md_pcrel_from (fixS *fixP)
252b5132
RH
13959{
13960 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
13961}
13962
13963#ifndef I386COFF
13964
13965static void
e3bb37b5 13966s_bss (int ignore ATTRIBUTE_UNUSED)
252b5132 13967{
29b0f896 13968 int temp;
252b5132 13969
8a75718c
JB
13970#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13971 if (IS_ELF)
13972 obj_elf_section_change_hook ();
13973#endif
252b5132
RH
13974 temp = get_absolute_expression ();
13975 subseg_set (bss_section, (subsegT) temp);
13976 demand_empty_rest_of_line ();
13977}
13978
13979#endif
13980
e379e5f3
L
13981/* Remember constant directive. */
13982
13983void
13984i386_cons_align (int ignore ATTRIBUTE_UNUSED)
13985{
13986 if (last_insn.kind != last_insn_directive
13987 && (bfd_section_flags (now_seg) & SEC_CODE))
13988 {
13989 last_insn.seg = now_seg;
13990 last_insn.kind = last_insn_directive;
13991 last_insn.name = "constant directive";
13992 last_insn.file = as_where (&last_insn.line);
ae531041
L
13993 if (lfence_before_ret != lfence_before_ret_none)
13994 {
13995 if (lfence_before_indirect_branch != lfence_branch_none)
13996 as_warn (_("constant directive skips -mlfence-before-ret "
13997 "and -mlfence-before-indirect-branch"));
13998 else
13999 as_warn (_("constant directive skips -mlfence-before-ret"));
14000 }
14001 else if (lfence_before_indirect_branch != lfence_branch_none)
14002 as_warn (_("constant directive skips -mlfence-before-indirect-branch"));
e379e5f3
L
14003 }
14004}
14005
252b5132 14006void
e3bb37b5 14007i386_validate_fix (fixS *fixp)
252b5132 14008{
02a86693 14009 if (fixp->fx_subsy)
252b5132 14010 {
02a86693 14011 if (fixp->fx_subsy == GOT_symbol)
23df1078 14012 {
02a86693
L
14013 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
14014 {
14015 if (!object_64bit)
14016 abort ();
14017#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14018 if (fixp->fx_tcbit2)
56ceb5b5
L
14019 fixp->fx_r_type = (fixp->fx_tcbit
14020 ? BFD_RELOC_X86_64_REX_GOTPCRELX
14021 : BFD_RELOC_X86_64_GOTPCRELX);
02a86693
L
14022 else
14023#endif
14024 fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
14025 }
d6ab8113 14026 else
02a86693
L
14027 {
14028 if (!object_64bit)
14029 fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
14030 else
14031 fixp->fx_r_type = BFD_RELOC_X86_64_GOTOFF64;
14032 }
14033 fixp->fx_subsy = 0;
23df1078 14034 }
252b5132 14035 }
02a86693 14036#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2585b7a5 14037 else
02a86693 14038 {
2585b7a5
L
14039 /* NB: Commit 292676c1 resolved PLT32 reloc aganst local symbol
14040 to section. Since PLT32 relocation must be against symbols,
14041 turn such PLT32 relocation into PC32 relocation. */
14042 if (fixp->fx_addsy
14043 && (fixp->fx_r_type == BFD_RELOC_386_PLT32
14044 || fixp->fx_r_type == BFD_RELOC_X86_64_PLT32)
14045 && symbol_section_p (fixp->fx_addsy))
14046 fixp->fx_r_type = BFD_RELOC_32_PCREL;
14047 if (!object_64bit)
14048 {
14049 if (fixp->fx_r_type == BFD_RELOC_386_GOT32
14050 && fixp->fx_tcbit2)
14051 fixp->fx_r_type = BFD_RELOC_386_GOT32X;
14052 }
02a86693
L
14053 }
14054#endif
252b5132
RH
14055}
14056
252b5132 14057arelent *
7016a5d5 14058tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
14059{
14060 arelent *rel;
14061 bfd_reloc_code_real_type code;
14062
14063 switch (fixp->fx_r_type)
14064 {
8ce3d284 14065#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
14066 case BFD_RELOC_SIZE32:
14067 case BFD_RELOC_SIZE64:
14068 if (S_IS_DEFINED (fixp->fx_addsy)
14069 && !S_IS_EXTERNAL (fixp->fx_addsy))
14070 {
14071 /* Resolve size relocation against local symbol to size of
14072 the symbol plus addend. */
14073 valueT value = S_GET_SIZE (fixp->fx_addsy) + fixp->fx_offset;
14074 if (fixp->fx_r_type == BFD_RELOC_SIZE32
14075 && !fits_in_unsigned_long (value))
14076 as_bad_where (fixp->fx_file, fixp->fx_line,
14077 _("symbol size computation overflow"));
14078 fixp->fx_addsy = NULL;
14079 fixp->fx_subsy = NULL;
14080 md_apply_fix (fixp, (valueT *) &value, NULL);
14081 return NULL;
14082 }
8ce3d284 14083#endif
1a0670f3 14084 /* Fall through. */
8fd4256d 14085
3e73aa7c
JH
14086 case BFD_RELOC_X86_64_PLT32:
14087 case BFD_RELOC_X86_64_GOT32:
14088 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
14089 case BFD_RELOC_X86_64_GOTPCRELX:
14090 case BFD_RELOC_X86_64_REX_GOTPCRELX:
252b5132
RH
14091 case BFD_RELOC_386_PLT32:
14092 case BFD_RELOC_386_GOT32:
02a86693 14093 case BFD_RELOC_386_GOT32X:
252b5132
RH
14094 case BFD_RELOC_386_GOTOFF:
14095 case BFD_RELOC_386_GOTPC:
13ae64f3
JJ
14096 case BFD_RELOC_386_TLS_GD:
14097 case BFD_RELOC_386_TLS_LDM:
14098 case BFD_RELOC_386_TLS_LDO_32:
14099 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
14100 case BFD_RELOC_386_TLS_IE:
14101 case BFD_RELOC_386_TLS_GOTIE:
13ae64f3
JJ
14102 case BFD_RELOC_386_TLS_LE_32:
14103 case BFD_RELOC_386_TLS_LE:
67a4f2b7
AO
14104 case BFD_RELOC_386_TLS_GOTDESC:
14105 case BFD_RELOC_386_TLS_DESC_CALL:
bffbf940
JJ
14106 case BFD_RELOC_X86_64_TLSGD:
14107 case BFD_RELOC_X86_64_TLSLD:
14108 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 14109 case BFD_RELOC_X86_64_DTPOFF64:
bffbf940
JJ
14110 case BFD_RELOC_X86_64_GOTTPOFF:
14111 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113
JB
14112 case BFD_RELOC_X86_64_TPOFF64:
14113 case BFD_RELOC_X86_64_GOTOFF64:
14114 case BFD_RELOC_X86_64_GOTPC32:
7b81dfbb
AJ
14115 case BFD_RELOC_X86_64_GOT64:
14116 case BFD_RELOC_X86_64_GOTPCREL64:
14117 case BFD_RELOC_X86_64_GOTPC64:
14118 case BFD_RELOC_X86_64_GOTPLT64:
14119 case BFD_RELOC_X86_64_PLTOFF64:
67a4f2b7
AO
14120 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14121 case BFD_RELOC_X86_64_TLSDESC_CALL:
252b5132
RH
14122 case BFD_RELOC_RVA:
14123 case BFD_RELOC_VTABLE_ENTRY:
14124 case BFD_RELOC_VTABLE_INHERIT:
6482c264
NC
14125#ifdef TE_PE
14126 case BFD_RELOC_32_SECREL:
14127#endif
252b5132
RH
14128 code = fixp->fx_r_type;
14129 break;
dbbaec26
L
14130 case BFD_RELOC_X86_64_32S:
14131 if (!fixp->fx_pcrel)
14132 {
14133 /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32. */
14134 code = fixp->fx_r_type;
14135 break;
14136 }
1a0670f3 14137 /* Fall through. */
252b5132 14138 default:
93382f6d 14139 if (fixp->fx_pcrel)
252b5132 14140 {
93382f6d
AM
14141 switch (fixp->fx_size)
14142 {
14143 default:
b091f402
AM
14144 as_bad_where (fixp->fx_file, fixp->fx_line,
14145 _("can not do %d byte pc-relative relocation"),
14146 fixp->fx_size);
93382f6d
AM
14147 code = BFD_RELOC_32_PCREL;
14148 break;
14149 case 1: code = BFD_RELOC_8_PCREL; break;
14150 case 2: code = BFD_RELOC_16_PCREL; break;
d258b828 14151 case 4: code = BFD_RELOC_32_PCREL; break;
d6ab8113
JB
14152#ifdef BFD64
14153 case 8: code = BFD_RELOC_64_PCREL; break;
14154#endif
93382f6d
AM
14155 }
14156 }
14157 else
14158 {
14159 switch (fixp->fx_size)
14160 {
14161 default:
b091f402
AM
14162 as_bad_where (fixp->fx_file, fixp->fx_line,
14163 _("can not do %d byte relocation"),
14164 fixp->fx_size);
93382f6d
AM
14165 code = BFD_RELOC_32;
14166 break;
14167 case 1: code = BFD_RELOC_8; break;
14168 case 2: code = BFD_RELOC_16; break;
14169 case 4: code = BFD_RELOC_32; break;
937149dd 14170#ifdef BFD64
3e73aa7c 14171 case 8: code = BFD_RELOC_64; break;
937149dd 14172#endif
93382f6d 14173 }
252b5132
RH
14174 }
14175 break;
14176 }
252b5132 14177
d182319b
JB
14178 if ((code == BFD_RELOC_32
14179 || code == BFD_RELOC_32_PCREL
14180 || code == BFD_RELOC_X86_64_32S)
252b5132
RH
14181 && GOT_symbol
14182 && fixp->fx_addsy == GOT_symbol)
3e73aa7c 14183 {
4fa24527 14184 if (!object_64bit)
d6ab8113
JB
14185 code = BFD_RELOC_386_GOTPC;
14186 else
14187 code = BFD_RELOC_X86_64_GOTPC32;
3e73aa7c 14188 }
7b81dfbb
AJ
14189 if ((code == BFD_RELOC_64 || code == BFD_RELOC_64_PCREL)
14190 && GOT_symbol
14191 && fixp->fx_addsy == GOT_symbol)
14192 {
14193 code = BFD_RELOC_X86_64_GOTPC64;
14194 }
252b5132 14195
add39d23
TS
14196 rel = XNEW (arelent);
14197 rel->sym_ptr_ptr = XNEW (asymbol *);
49309057 14198 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
14199
14200 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
c87db184 14201
3e73aa7c
JH
14202 if (!use_rela_relocations)
14203 {
14204 /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
14205 vtable entry to be used in the relocation's section offset. */
14206 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
14207 rel->address = fixp->fx_offset;
fbeb56a4
DK
14208#if defined (OBJ_COFF) && defined (TE_PE)
14209 else if (fixp->fx_addsy && S_IS_WEAK (fixp->fx_addsy))
14210 rel->addend = fixp->fx_addnumber - (S_GET_VALUE (fixp->fx_addsy) * 2);
14211 else
14212#endif
c6682705 14213 rel->addend = 0;
3e73aa7c
JH
14214 }
14215 /* Use the rela in 64bit mode. */
252b5132 14216 else
3e73aa7c 14217 {
862be3fb
L
14218 if (disallow_64bit_reloc)
14219 switch (code)
14220 {
862be3fb
L
14221 case BFD_RELOC_X86_64_DTPOFF64:
14222 case BFD_RELOC_X86_64_TPOFF64:
14223 case BFD_RELOC_64_PCREL:
14224 case BFD_RELOC_X86_64_GOTOFF64:
14225 case BFD_RELOC_X86_64_GOT64:
14226 case BFD_RELOC_X86_64_GOTPCREL64:
14227 case BFD_RELOC_X86_64_GOTPC64:
14228 case BFD_RELOC_X86_64_GOTPLT64:
14229 case BFD_RELOC_X86_64_PLTOFF64:
14230 as_bad_where (fixp->fx_file, fixp->fx_line,
14231 _("cannot represent relocation type %s in x32 mode"),
14232 bfd_get_reloc_code_name (code));
14233 break;
14234 default:
14235 break;
14236 }
14237
062cd5e7
AS
14238 if (!fixp->fx_pcrel)
14239 rel->addend = fixp->fx_offset;
14240 else
14241 switch (code)
14242 {
14243 case BFD_RELOC_X86_64_PLT32:
14244 case BFD_RELOC_X86_64_GOT32:
14245 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
14246 case BFD_RELOC_X86_64_GOTPCRELX:
14247 case BFD_RELOC_X86_64_REX_GOTPCRELX:
bffbf940
JJ
14248 case BFD_RELOC_X86_64_TLSGD:
14249 case BFD_RELOC_X86_64_TLSLD:
14250 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7
AO
14251 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14252 case BFD_RELOC_X86_64_TLSDESC_CALL:
062cd5e7
AS
14253 rel->addend = fixp->fx_offset - fixp->fx_size;
14254 break;
14255 default:
14256 rel->addend = (section->vma
14257 - fixp->fx_size
14258 + fixp->fx_addnumber
14259 + md_pcrel_from (fixp));
14260 break;
14261 }
3e73aa7c
JH
14262 }
14263
252b5132
RH
14264 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
14265 if (rel->howto == NULL)
14266 {
14267 as_bad_where (fixp->fx_file, fixp->fx_line,
d0b47220 14268 _("cannot represent relocation type %s"),
252b5132
RH
14269 bfd_get_reloc_code_name (code));
14270 /* Set howto to a garbage value so that we can keep going. */
14271 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
9c2799c2 14272 gas_assert (rel->howto != NULL);
252b5132
RH
14273 }
14274
14275 return rel;
14276}
14277
ee86248c 14278#include "tc-i386-intel.c"
54cfded0 14279
a60de03c
JB
14280void
14281tc_x86_parse_to_dw2regnum (expressionS *exp)
54cfded0 14282{
a60de03c
JB
14283 int saved_naked_reg;
14284 char saved_register_dot;
54cfded0 14285
a60de03c
JB
14286 saved_naked_reg = allow_naked_reg;
14287 allow_naked_reg = 1;
14288 saved_register_dot = register_chars['.'];
14289 register_chars['.'] = '.';
14290 allow_pseudo_reg = 1;
14291 expression_and_evaluate (exp);
14292 allow_pseudo_reg = 0;
14293 register_chars['.'] = saved_register_dot;
14294 allow_naked_reg = saved_naked_reg;
14295
e96d56a1 14296 if (exp->X_op == O_register && exp->X_add_number >= 0)
54cfded0 14297 {
a60de03c
JB
14298 if ((addressT) exp->X_add_number < i386_regtab_size)
14299 {
14300 exp->X_op = O_constant;
14301 exp->X_add_number = i386_regtab[exp->X_add_number]
14302 .dw2_regnum[flag_code >> 1];
14303 }
14304 else
14305 exp->X_op = O_illegal;
54cfded0 14306 }
54cfded0
AM
14307}
14308
14309void
14310tc_x86_frame_initial_instructions (void)
14311{
a60de03c
JB
14312 static unsigned int sp_regno[2];
14313
14314 if (!sp_regno[flag_code >> 1])
14315 {
14316 char *saved_input = input_line_pointer;
14317 char sp[][4] = {"esp", "rsp"};
14318 expressionS exp;
a4447b93 14319
a60de03c
JB
14320 input_line_pointer = sp[flag_code >> 1];
14321 tc_x86_parse_to_dw2regnum (&exp);
9c2799c2 14322 gas_assert (exp.X_op == O_constant);
a60de03c
JB
14323 sp_regno[flag_code >> 1] = exp.X_add_number;
14324 input_line_pointer = saved_input;
14325 }
a4447b93 14326
61ff971f
L
14327 cfi_add_CFA_def_cfa (sp_regno[flag_code >> 1], -x86_cie_data_alignment);
14328 cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
54cfded0 14329}
d2b2c203 14330
d7921315
L
14331int
14332x86_dwarf2_addr_size (void)
14333{
14334#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
14335 if (x86_elf_abi == X86_64_X32_ABI)
14336 return 4;
14337#endif
14338 return bfd_arch_bits_per_address (stdoutput) / 8;
14339}
14340
d2b2c203
DJ
14341int
14342i386_elf_section_type (const char *str, size_t len)
14343{
14344 if (flag_code == CODE_64BIT
14345 && len == sizeof ("unwind") - 1
d34049e8 14346 && startswith (str, "unwind"))
d2b2c203
DJ
14347 return SHT_X86_64_UNWIND;
14348
14349 return -1;
14350}
bb41ade5 14351
ad5fec3b
EB
14352#ifdef TE_SOLARIS
14353void
14354i386_solaris_fix_up_eh_frame (segT sec)
14355{
14356 if (flag_code == CODE_64BIT)
14357 elf_section_type (sec) = SHT_X86_64_UNWIND;
14358}
14359#endif
14360
bb41ade5
AM
14361#ifdef TE_PE
14362void
14363tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
14364{
91d6fa6a 14365 expressionS exp;
bb41ade5 14366
91d6fa6a
NC
14367 exp.X_op = O_secrel;
14368 exp.X_add_symbol = symbol;
14369 exp.X_add_number = 0;
14370 emit_expr (&exp, size);
bb41ade5
AM
14371}
14372#endif
3b22753a
L
14373
14374#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14375/* For ELF on x86-64, add support for SHF_X86_64_LARGE. */
14376
01e1a5bc 14377bfd_vma
6d4af3c2 14378x86_64_section_letter (int letter, const char **ptr_msg)
3b22753a
L
14379{
14380 if (flag_code == CODE_64BIT)
14381 {
14382 if (letter == 'l')
14383 return SHF_X86_64_LARGE;
14384
8f3bae45 14385 *ptr_msg = _("bad .section directive: want a,l,w,x,M,S,G,T in string");
64e74474 14386 }
3b22753a 14387 else
8f3bae45 14388 *ptr_msg = _("bad .section directive: want a,w,x,M,S,G,T in string");
3b22753a
L
14389 return -1;
14390}
14391
01e1a5bc 14392bfd_vma
3b22753a
L
14393x86_64_section_word (char *str, size_t len)
14394{
08dedd66 14395 if (len == 5 && flag_code == CODE_64BIT && startswith (str, "large"))
3b22753a
L
14396 return SHF_X86_64_LARGE;
14397
14398 return -1;
14399}
14400
14401static void
14402handle_large_common (int small ATTRIBUTE_UNUSED)
14403{
14404 if (flag_code != CODE_64BIT)
14405 {
14406 s_comm_internal (0, elf_common_parse);
14407 as_warn (_(".largecomm supported only in 64bit mode, producing .comm"));
14408 }
14409 else
14410 {
14411 static segT lbss_section;
14412 asection *saved_com_section_ptr = elf_com_section_ptr;
14413 asection *saved_bss_section = bss_section;
14414
14415 if (lbss_section == NULL)
14416 {
14417 flagword applicable;
14418 segT seg = now_seg;
14419 subsegT subseg = now_subseg;
14420
14421 /* The .lbss section is for local .largecomm symbols. */
14422 lbss_section = subseg_new (".lbss", 0);
14423 applicable = bfd_applicable_section_flags (stdoutput);
fd361982 14424 bfd_set_section_flags (lbss_section, applicable & SEC_ALLOC);
3b22753a
L
14425 seg_info (lbss_section)->bss = 1;
14426
14427 subseg_set (seg, subseg);
14428 }
14429
14430 elf_com_section_ptr = &_bfd_elf_large_com_section;
14431 bss_section = lbss_section;
14432
14433 s_comm_internal (0, elf_common_parse);
14434
14435 elf_com_section_ptr = saved_com_section_ptr;
14436 bss_section = saved_bss_section;
14437 }
14438}
14439#endif /* OBJ_ELF || OBJ_MAYBE_ELF */
This page took 2.62536 seconds and 4 git commands to generate.