x86-64: defer 32-bit signed displacement check
[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
b4cac588 3558static int
e3bb37b5 3559intel_float_operand (const char *mnemonic)
252b5132 3560{
9306ca4a
JB
3561 /* Note that the value returned is meaningful only for opcodes with (memory)
3562 operands, hence the code here is free to improperly handle opcodes that
3563 have no operands (for better performance and smaller code). */
3564
3565 if (mnemonic[0] != 'f')
3566 return 0; /* non-math */
3567
3568 switch (mnemonic[1])
3569 {
3570 /* fclex, fdecstp, fdisi, femms, feni, fincstp, finit, fsetpm, and
3571 the fs segment override prefix not currently handled because no
3572 call path can make opcodes without operands get here */
3573 case 'i':
3574 return 2 /* integer op */;
3575 case 'l':
3576 if (mnemonic[2] == 'd' && (mnemonic[3] == 'c' || mnemonic[3] == 'e'))
3577 return 3; /* fldcw/fldenv */
3578 break;
3579 case 'n':
3580 if (mnemonic[2] != 'o' /* fnop */)
3581 return 3; /* non-waiting control op */
3582 break;
3583 case 'r':
3584 if (mnemonic[2] == 's')
3585 return 3; /* frstor/frstpm */
3586 break;
3587 case 's':
3588 if (mnemonic[2] == 'a')
3589 return 3; /* fsave */
3590 if (mnemonic[2] == 't')
3591 {
3592 switch (mnemonic[3])
3593 {
3594 case 'c': /* fstcw */
3595 case 'd': /* fstdw */
3596 case 'e': /* fstenv */
3597 case 's': /* fsts[gw] */
3598 return 3;
3599 }
3600 }
3601 break;
3602 case 'x':
3603 if (mnemonic[2] == 'r' || mnemonic[2] == 's')
3604 return 0; /* fxsave/fxrstor are not really math ops */
3605 break;
3606 }
252b5132 3607
9306ca4a 3608 return 1;
252b5132
RH
3609}
3610
9a182d04
JB
3611static INLINE void
3612install_template (const insn_template *t)
3613{
3614 unsigned int l;
3615
3616 i.tm = *t;
3617
3618 /* Note that for pseudo prefixes this produces a length of 1. But for them
3619 the length isn't interesting at all. */
3620 for (l = 1; l < 4; ++l)
3621 if (!(t->base_opcode >> (8 * l)))
3622 break;
3623
3624 i.opcode_length = l;
3625}
3626
c0f3af97
L
3627/* Build the VEX prefix. */
3628
3629static void
d3ce72d0 3630build_vex_prefix (const insn_template *t)
c0f3af97
L
3631{
3632 unsigned int register_specifier;
c0f3af97 3633 unsigned int vector_length;
03751133 3634 unsigned int w;
c0f3af97
L
3635
3636 /* Check register specifier. */
3637 if (i.vex.register_specifier)
43234a1e
L
3638 {
3639 register_specifier =
3640 ~register_number (i.vex.register_specifier) & 0xf;
3641 gas_assert ((i.vex.register_specifier->reg_flags & RegVRex) == 0);
3642 }
c0f3af97
L
3643 else
3644 register_specifier = 0xf;
3645
79f0fa25
L
3646 /* Use 2-byte VEX prefix by swapping destination and source operand
3647 if there are more than 1 register operand. */
3648 if (i.reg_operands > 1
3649 && i.vec_encoding != vex_encoding_vex3
86fa6981 3650 && i.dir_encoding == dir_encoding_default
fa99fab2 3651 && i.operands == i.reg_operands
dbbc8b7e 3652 && operand_type_equal (&i.types[0], &i.types[i.operands - 1])
441f6aca 3653 && i.tm.opcode_modifier.opcodespace == SPACE_0F
dbbc8b7e 3654 && (i.tm.opcode_modifier.load || i.tm.opcode_modifier.d)
fa99fab2
L
3655 && i.rex == REX_B)
3656 {
3657 unsigned int xchg = i.operands - 1;
3658 union i386_op temp_op;
3659 i386_operand_type temp_type;
3660
3661 temp_type = i.types[xchg];
3662 i.types[xchg] = i.types[0];
3663 i.types[0] = temp_type;
3664 temp_op = i.op[xchg];
3665 i.op[xchg] = i.op[0];
3666 i.op[0] = temp_op;
3667
9c2799c2 3668 gas_assert (i.rm.mode == 3);
fa99fab2
L
3669
3670 i.rex = REX_R;
3671 xchg = i.rm.regmem;
3672 i.rm.regmem = i.rm.reg;
3673 i.rm.reg = xchg;
3674
dbbc8b7e
JB
3675 if (i.tm.opcode_modifier.d)
3676 i.tm.base_opcode ^= (i.tm.base_opcode & 0xee) != 0x6e
3677 ? Opcode_SIMD_FloatD : Opcode_SIMD_IntD;
3678 else /* Use the next insn. */
9a182d04 3679 install_template (&t[1]);
fa99fab2
L
3680 }
3681
79dec6b7
JB
3682 /* Use 2-byte VEX prefix by swapping commutative source operands if there
3683 are no memory operands and at least 3 register ones. */
3684 if (i.reg_operands >= 3
3685 && i.vec_encoding != vex_encoding_vex3
3686 && i.reg_operands == i.operands - i.imm_operands
3687 && i.tm.opcode_modifier.vex
3688 && i.tm.opcode_modifier.commutative
3689 && (i.tm.opcode_modifier.sse2avx || optimize > 1)
3690 && i.rex == REX_B
3691 && i.vex.register_specifier
3692 && !(i.vex.register_specifier->reg_flags & RegRex))
3693 {
3694 unsigned int xchg = i.operands - i.reg_operands;
3695 union i386_op temp_op;
3696 i386_operand_type temp_type;
3697
441f6aca 3698 gas_assert (i.tm.opcode_modifier.opcodespace == SPACE_0F);
79dec6b7
JB
3699 gas_assert (!i.tm.opcode_modifier.sae);
3700 gas_assert (operand_type_equal (&i.types[i.operands - 2],
3701 &i.types[i.operands - 3]));
3702 gas_assert (i.rm.mode == 3);
3703
3704 temp_type = i.types[xchg];
3705 i.types[xchg] = i.types[xchg + 1];
3706 i.types[xchg + 1] = temp_type;
3707 temp_op = i.op[xchg];
3708 i.op[xchg] = i.op[xchg + 1];
3709 i.op[xchg + 1] = temp_op;
3710
3711 i.rex = 0;
3712 xchg = i.rm.regmem | 8;
3713 i.rm.regmem = ~register_specifier & 0xf;
3714 gas_assert (!(i.rm.regmem & 8));
3715 i.vex.register_specifier += xchg - i.rm.regmem;
3716 register_specifier = ~xchg & 0xf;
3717 }
3718
539f890d
L
3719 if (i.tm.opcode_modifier.vex == VEXScalar)
3720 vector_length = avxscalar;
10c17abd
JB
3721 else if (i.tm.opcode_modifier.vex == VEX256)
3722 vector_length = 1;
539f890d 3723 else
10c17abd 3724 {
56522fc5 3725 unsigned int op;
10c17abd 3726
c7213af9
L
3727 /* Determine vector length from the last multi-length vector
3728 operand. */
10c17abd 3729 vector_length = 0;
56522fc5 3730 for (op = t->operands; op--;)
10c17abd
JB
3731 if (t->operand_types[op].bitfield.xmmword
3732 && t->operand_types[op].bitfield.ymmword
3733 && i.types[op].bitfield.ymmword)
3734 {
3735 vector_length = 1;
3736 break;
3737 }
3738 }
c0f3af97 3739
03751133
L
3740 /* Check the REX.W bit and VEXW. */
3741 if (i.tm.opcode_modifier.vexw == VEXWIG)
3742 w = (vexwig == vexw1 || (i.rex & REX_W)) ? 1 : 0;
3743 else if (i.tm.opcode_modifier.vexw)
3744 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3745 else
931d03b7 3746 w = (flag_code == CODE_64BIT ? i.rex & REX_W : vexwig == vexw1) ? 1 : 0;
03751133 3747
c0f3af97 3748 /* Use 2-byte VEX prefix if possible. */
03751133
L
3749 if (w == 0
3750 && i.vec_encoding != vex_encoding_vex3
441f6aca 3751 && i.tm.opcode_modifier.opcodespace == SPACE_0F
c0f3af97
L
3752 && (i.rex & (REX_W | REX_X | REX_B)) == 0)
3753 {
3754 /* 2-byte VEX prefix. */
3755 unsigned int r;
3756
3757 i.vex.length = 2;
3758 i.vex.bytes[0] = 0xc5;
3759
3760 /* Check the REX.R bit. */
3761 r = (i.rex & REX_R) ? 0 : 1;
3762 i.vex.bytes[1] = (r << 7
3763 | register_specifier << 3
3764 | vector_length << 2
35648716 3765 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3766 }
3767 else
3768 {
3769 /* 3-byte VEX prefix. */
f88c9eb0 3770 i.vex.length = 3;
f88c9eb0 3771
441f6aca 3772 switch (i.tm.opcode_modifier.opcodespace)
5dd85c99 3773 {
441f6aca
JB
3774 case SPACE_0F:
3775 case SPACE_0F38:
3776 case SPACE_0F3A:
80de6e00 3777 i.vex.bytes[0] = 0xc4;
7f399153 3778 break;
441f6aca
JB
3779 case SPACE_XOP08:
3780 case SPACE_XOP09:
3781 case SPACE_XOP0A:
f88c9eb0 3782 i.vex.bytes[0] = 0x8f;
7f399153
L
3783 break;
3784 default:
3785 abort ();
f88c9eb0 3786 }
c0f3af97 3787
c0f3af97
L
3788 /* The high 3 bits of the second VEX byte are 1's compliment
3789 of RXB bits from REX. */
441f6aca 3790 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | i.tm.opcode_modifier.opcodespace;
c0f3af97 3791
c0f3af97
L
3792 i.vex.bytes[2] = (w << 7
3793 | register_specifier << 3
3794 | vector_length << 2
35648716 3795 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3796 }
3797}
3798
5b7c81bd 3799static INLINE bool
e771e7c9
JB
3800is_evex_encoding (const insn_template *t)
3801{
7091c612 3802 return t->opcode_modifier.evex || t->opcode_modifier.disp8memshift
e771e7c9 3803 || t->opcode_modifier.broadcast || t->opcode_modifier.masking
a80195f1 3804 || t->opcode_modifier.sae;
e771e7c9
JB
3805}
3806
5b7c81bd 3807static INLINE bool
7a8655d2
JB
3808is_any_vex_encoding (const insn_template *t)
3809{
7b47a312 3810 return t->opcode_modifier.vex || is_evex_encoding (t);
7a8655d2
JB
3811}
3812
43234a1e
L
3813/* Build the EVEX prefix. */
3814
3815static void
3816build_evex_prefix (void)
3817{
35648716 3818 unsigned int register_specifier, w;
43234a1e
L
3819 rex_byte vrex_used = 0;
3820
3821 /* Check register specifier. */
3822 if (i.vex.register_specifier)
3823 {
3824 gas_assert ((i.vrex & REX_X) == 0);
3825
3826 register_specifier = i.vex.register_specifier->reg_num;
3827 if ((i.vex.register_specifier->reg_flags & RegRex))
3828 register_specifier += 8;
3829 /* The upper 16 registers are encoded in the fourth byte of the
3830 EVEX prefix. */
3831 if (!(i.vex.register_specifier->reg_flags & RegVRex))
3832 i.vex.bytes[3] = 0x8;
3833 register_specifier = ~register_specifier & 0xf;
3834 }
3835 else
3836 {
3837 register_specifier = 0xf;
3838
3839 /* Encode upper 16 vector index register in the fourth byte of
3840 the EVEX prefix. */
3841 if (!(i.vrex & REX_X))
3842 i.vex.bytes[3] = 0x8;
3843 else
3844 vrex_used |= REX_X;
3845 }
3846
43234a1e
L
3847 /* 4 byte EVEX prefix. */
3848 i.vex.length = 4;
3849 i.vex.bytes[0] = 0x62;
3850
43234a1e
L
3851 /* The high 3 bits of the second EVEX byte are 1's compliment of RXB
3852 bits from REX. */
441f6aca
JB
3853 gas_assert (i.tm.opcode_modifier.opcodespace >= SPACE_0F);
3854 gas_assert (i.tm.opcode_modifier.opcodespace <= SPACE_0F3A);
3855 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | i.tm.opcode_modifier.opcodespace;
43234a1e
L
3856
3857 /* The fifth bit of the second EVEX byte is 1's compliment of the
3858 REX_R bit in VREX. */
3859 if (!(i.vrex & REX_R))
3860 i.vex.bytes[1] |= 0x10;
3861 else
3862 vrex_used |= REX_R;
3863
3864 if ((i.reg_operands + i.imm_operands) == i.operands)
3865 {
3866 /* When all operands are registers, the REX_X bit in REX is not
3867 used. We reuse it to encode the upper 16 registers, which is
3868 indicated by the REX_B bit in VREX. The REX_X bit is encoded
3869 as 1's compliment. */
3870 if ((i.vrex & REX_B))
3871 {
3872 vrex_used |= REX_B;
3873 i.vex.bytes[1] &= ~0x40;
3874 }
3875 }
3876
3877 /* EVEX instructions shouldn't need the REX prefix. */
3878 i.vrex &= ~vrex_used;
3879 gas_assert (i.vrex == 0);
3880
6865c043
L
3881 /* Check the REX.W bit and VEXW. */
3882 if (i.tm.opcode_modifier.vexw == VEXWIG)
3883 w = (evexwig == evexw1 || (i.rex & REX_W)) ? 1 : 0;
3884 else if (i.tm.opcode_modifier.vexw)
3885 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3886 else
931d03b7 3887 w = (flag_code == CODE_64BIT ? i.rex & REX_W : evexwig == evexw1) ? 1 : 0;
43234a1e 3888
43234a1e 3889 /* The third byte of the EVEX prefix. */
35648716
JB
3890 i.vex.bytes[2] = ((w << 7)
3891 | (register_specifier << 3)
3892 | 4 /* Encode the U bit. */
3893 | i.tm.opcode_modifier.opcodeprefix);
43234a1e
L
3894
3895 /* The fourth byte of the EVEX prefix. */
3896 /* The zeroing-masking bit. */
6225c532 3897 if (i.mask.reg && i.mask.zeroing)
43234a1e
L
3898 i.vex.bytes[3] |= 0x80;
3899
3900 /* Don't always set the broadcast bit if there is no RC. */
ca5312a2 3901 if (i.rounding.type == rc_none)
43234a1e
L
3902 {
3903 /* Encode the vector length. */
3904 unsigned int vec_length;
3905
e771e7c9
JB
3906 if (!i.tm.opcode_modifier.evex
3907 || i.tm.opcode_modifier.evex == EVEXDYN)
3908 {
56522fc5 3909 unsigned int op;
e771e7c9 3910
c7213af9
L
3911 /* Determine vector length from the last multi-length vector
3912 operand. */
56522fc5 3913 for (op = i.operands; op--;)
e771e7c9
JB
3914 if (i.tm.operand_types[op].bitfield.xmmword
3915 + i.tm.operand_types[op].bitfield.ymmword
3916 + i.tm.operand_types[op].bitfield.zmmword > 1)
3917 {
3918 if (i.types[op].bitfield.zmmword)
c7213af9
L
3919 {
3920 i.tm.opcode_modifier.evex = EVEX512;
3921 break;
3922 }
e771e7c9 3923 else if (i.types[op].bitfield.ymmword)
c7213af9
L
3924 {
3925 i.tm.opcode_modifier.evex = EVEX256;
3926 break;
3927 }
e771e7c9 3928 else if (i.types[op].bitfield.xmmword)
c7213af9
L
3929 {
3930 i.tm.opcode_modifier.evex = EVEX128;
3931 break;
3932 }
5273a3cd 3933 else if (i.broadcast.type && op == i.broadcast.operand)
625cbd7a 3934 {
5273a3cd 3935 switch (i.broadcast.bytes)
625cbd7a
JB
3936 {
3937 case 64:
3938 i.tm.opcode_modifier.evex = EVEX512;
3939 break;
3940 case 32:
3941 i.tm.opcode_modifier.evex = EVEX256;
3942 break;
3943 case 16:
3944 i.tm.opcode_modifier.evex = EVEX128;
3945 break;
3946 default:
c7213af9 3947 abort ();
625cbd7a 3948 }
c7213af9 3949 break;
625cbd7a 3950 }
e771e7c9 3951 }
c7213af9 3952
56522fc5 3953 if (op >= MAX_OPERANDS)
c7213af9 3954 abort ();
e771e7c9
JB
3955 }
3956
43234a1e
L
3957 switch (i.tm.opcode_modifier.evex)
3958 {
3959 case EVEXLIG: /* LL' is ignored */
3960 vec_length = evexlig << 5;
3961 break;
3962 case EVEX128:
3963 vec_length = 0 << 5;
3964 break;
3965 case EVEX256:
3966 vec_length = 1 << 5;
3967 break;
3968 case EVEX512:
3969 vec_length = 2 << 5;
3970 break;
3971 default:
3972 abort ();
3973 break;
3974 }
3975 i.vex.bytes[3] |= vec_length;
3976 /* Encode the broadcast bit. */
5273a3cd 3977 if (i.broadcast.type)
43234a1e
L
3978 i.vex.bytes[3] |= 0x10;
3979 }
ca5312a2
JB
3980 else if (i.rounding.type != saeonly)
3981 i.vex.bytes[3] |= 0x10 | (i.rounding.type << 5);
43234a1e 3982 else
ca5312a2 3983 i.vex.bytes[3] |= 0x10 | (evexrcig << 5);
43234a1e 3984
6225c532
JB
3985 if (i.mask.reg)
3986 i.vex.bytes[3] |= i.mask.reg->reg_num;
43234a1e
L
3987}
3988
65da13b5
L
3989static void
3990process_immext (void)
3991{
3992 expressionS *exp;
3993
c0f3af97 3994 /* These AMD 3DNow! and SSE2 instructions have an opcode suffix
65da13b5
L
3995 which is coded in the same place as an 8-bit immediate field
3996 would be. Here we fake an 8-bit immediate operand from the
3997 opcode suffix stored in tm.extension_opcode.
3998
c1e679ec 3999 AVX instructions also use this encoding, for some of
c0f3af97 4000 3 argument instructions. */
65da13b5 4001
43234a1e 4002 gas_assert (i.imm_operands <= 1
7ab9ffdd 4003 && (i.operands <= 2
7a8655d2 4004 || (is_any_vex_encoding (&i.tm)
7ab9ffdd 4005 && i.operands <= 4)));
65da13b5
L
4006
4007 exp = &im_expressions[i.imm_operands++];
4008 i.op[i.operands].imms = exp;
4009 i.types[i.operands] = imm8;
4010 i.operands++;
4011 exp->X_op = O_constant;
4012 exp->X_add_number = i.tm.extension_opcode;
4013 i.tm.extension_opcode = None;
4014}
4015
42164a71
L
4016
4017static int
4018check_hle (void)
4019{
742732c7 4020 switch (i.tm.opcode_modifier.prefixok)
42164a71
L
4021 {
4022 default:
4023 abort ();
742732c7
JB
4024 case PrefixLock:
4025 case PrefixNone:
4026 case PrefixNoTrack:
4027 case PrefixRep:
165de32a
L
4028 as_bad (_("invalid instruction `%s' after `%s'"),
4029 i.tm.name, i.hle_prefix);
42164a71 4030 return 0;
742732c7 4031 case PrefixHLELock:
42164a71
L
4032 if (i.prefix[LOCK_PREFIX])
4033 return 1;
165de32a 4034 as_bad (_("missing `lock' with `%s'"), i.hle_prefix);
42164a71 4035 return 0;
742732c7 4036 case PrefixHLEAny:
42164a71 4037 return 1;
742732c7 4038 case PrefixHLERelease:
42164a71
L
4039 if (i.prefix[HLE_PREFIX] != XRELEASE_PREFIX_OPCODE)
4040 {
4041 as_bad (_("instruction `%s' after `xacquire' not allowed"),
4042 i.tm.name);
4043 return 0;
4044 }
8dc0818e 4045 if (i.mem_operands == 0 || !(i.flags[i.operands - 1] & Operand_Mem))
42164a71
L
4046 {
4047 as_bad (_("memory destination needed for instruction `%s'"
4048 " after `xrelease'"), i.tm.name);
4049 return 0;
4050 }
4051 return 1;
4052 }
4053}
4054
b6f8c7c4
L
4055/* Try the shortest encoding by shortening operand size. */
4056
4057static void
4058optimize_encoding (void)
4059{
a0a1771e 4060 unsigned int j;
b6f8c7c4
L
4061
4062 if (optimize_for_space
389d00a5 4063 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
b6f8c7c4
L
4064 && i.reg_operands == 1
4065 && i.imm_operands == 1
4066 && !i.types[1].bitfield.byte
4067 && i.op[0].imms->X_op == O_constant
4068 && fits_in_imm7 (i.op[0].imms->X_add_number)
72aea328 4069 && (i.tm.base_opcode == 0xa8
b6f8c7c4
L
4070 || (i.tm.base_opcode == 0xf6
4071 && i.tm.extension_opcode == 0x0)))
4072 {
4073 /* Optimize: -Os:
4074 test $imm7, %r64/%r32/%r16 -> test $imm7, %r8
4075 */
4076 unsigned int base_regnum = i.op[1].regs->reg_num;
4077 if (flag_code == CODE_64BIT || base_regnum < 4)
4078 {
4079 i.types[1].bitfield.byte = 1;
4080 /* Ignore the suffix. */
4081 i.suffix = 0;
7697afb6
JB
4082 /* Convert to byte registers. */
4083 if (i.types[1].bitfield.word)
4084 j = 16;
4085 else if (i.types[1].bitfield.dword)
4086 j = 32;
4087 else
4088 j = 48;
4089 if (!(i.op[1].regs->reg_flags & RegRex) && base_regnum < 4)
4090 j += 8;
4091 i.op[1].regs -= j;
b6f8c7c4
L
4092 }
4093 }
4094 else if (flag_code == CODE_64BIT
389d00a5 4095 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
d3d50934
L
4096 && ((i.types[1].bitfield.qword
4097 && i.reg_operands == 1
b6f8c7c4
L
4098 && i.imm_operands == 1
4099 && i.op[0].imms->X_op == O_constant
507916b8 4100 && ((i.tm.base_opcode == 0xb8
b6f8c7c4
L
4101 && i.tm.extension_opcode == None
4102 && fits_in_unsigned_long (i.op[0].imms->X_add_number))
4103 || (fits_in_imm31 (i.op[0].imms->X_add_number)
72aea328
JB
4104 && ((i.tm.base_opcode == 0x24
4105 || i.tm.base_opcode == 0xa8)
b6f8c7c4
L
4106 || (i.tm.base_opcode == 0x80
4107 && i.tm.extension_opcode == 0x4)
4108 || ((i.tm.base_opcode == 0xf6
507916b8 4109 || (i.tm.base_opcode | 1) == 0xc7)
b8364fa7
JB
4110 && i.tm.extension_opcode == 0x0)))
4111 || (fits_in_imm7 (i.op[0].imms->X_add_number)
4112 && i.tm.base_opcode == 0x83
4113 && i.tm.extension_opcode == 0x4)))
d3d50934
L
4114 || (i.types[0].bitfield.qword
4115 && ((i.reg_operands == 2
4116 && i.op[0].regs == i.op[1].regs
72aea328
JB
4117 && (i.tm.base_opcode == 0x30
4118 || i.tm.base_opcode == 0x28))
d3d50934
L
4119 || (i.reg_operands == 1
4120 && i.operands == 1
72aea328 4121 && i.tm.base_opcode == 0x30)))))
b6f8c7c4
L
4122 {
4123 /* Optimize: -O:
4124 andq $imm31, %r64 -> andl $imm31, %r32
b8364fa7 4125 andq $imm7, %r64 -> andl $imm7, %r32
b6f8c7c4
L
4126 testq $imm31, %r64 -> testl $imm31, %r32
4127 xorq %r64, %r64 -> xorl %r32, %r32
4128 subq %r64, %r64 -> subl %r32, %r32
4129 movq $imm31, %r64 -> movl $imm31, %r32
4130 movq $imm32, %r64 -> movl $imm32, %r32
4131 */
4132 i.tm.opcode_modifier.norex64 = 1;
507916b8 4133 if (i.tm.base_opcode == 0xb8 || (i.tm.base_opcode | 1) == 0xc7)
b6f8c7c4
L
4134 {
4135 /* Handle
4136 movq $imm31, %r64 -> movl $imm31, %r32
4137 movq $imm32, %r64 -> movl $imm32, %r32
4138 */
4139 i.tm.operand_types[0].bitfield.imm32 = 1;
4140 i.tm.operand_types[0].bitfield.imm32s = 0;
4141 i.tm.operand_types[0].bitfield.imm64 = 0;
4142 i.types[0].bitfield.imm32 = 1;
4143 i.types[0].bitfield.imm32s = 0;
4144 i.types[0].bitfield.imm64 = 0;
4145 i.types[1].bitfield.dword = 1;
4146 i.types[1].bitfield.qword = 0;
507916b8 4147 if ((i.tm.base_opcode | 1) == 0xc7)
b6f8c7c4
L
4148 {
4149 /* Handle
4150 movq $imm31, %r64 -> movl $imm31, %r32
4151 */
507916b8 4152 i.tm.base_opcode = 0xb8;
b6f8c7c4 4153 i.tm.extension_opcode = None;
507916b8 4154 i.tm.opcode_modifier.w = 0;
b6f8c7c4
L
4155 i.tm.opcode_modifier.modrm = 0;
4156 }
4157 }
4158 }
5641ec01
JB
4159 else if (optimize > 1
4160 && !optimize_for_space
389d00a5 4161 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
5641ec01
JB
4162 && i.reg_operands == 2
4163 && i.op[0].regs == i.op[1].regs
4164 && ((i.tm.base_opcode & ~(Opcode_D | 1)) == 0x8
4165 || (i.tm.base_opcode & ~(Opcode_D | 1)) == 0x20)
4166 && (flag_code != CODE_64BIT || !i.types[0].bitfield.dword))
4167 {
4168 /* Optimize: -O2:
4169 andb %rN, %rN -> testb %rN, %rN
4170 andw %rN, %rN -> testw %rN, %rN
4171 andq %rN, %rN -> testq %rN, %rN
4172 orb %rN, %rN -> testb %rN, %rN
4173 orw %rN, %rN -> testw %rN, %rN
4174 orq %rN, %rN -> testq %rN, %rN
4175
4176 and outside of 64-bit mode
4177
4178 andl %rN, %rN -> testl %rN, %rN
4179 orl %rN, %rN -> testl %rN, %rN
4180 */
4181 i.tm.base_opcode = 0x84 | (i.tm.base_opcode & 1);
4182 }
99112332 4183 else if (i.reg_operands == 3
b6f8c7c4
L
4184 && i.op[0].regs == i.op[1].regs
4185 && !i.types[2].bitfield.xmmword
4186 && (i.tm.opcode_modifier.vex
6225c532 4187 || ((!i.mask.reg || i.mask.zeroing)
ca5312a2 4188 && i.rounding.type == rc_none
e771e7c9 4189 && is_evex_encoding (&i.tm)
80c34c38 4190 && (i.vec_encoding != vex_encoding_evex
dd22218c 4191 || cpu_arch_isa_flags.bitfield.cpuavx512vl
80c34c38 4192 || i.tm.cpu_flags.bitfield.cpuavx512vl
7091c612 4193 || (i.tm.operand_types[2].bitfield.zmmword
dd22218c 4194 && i.types[2].bitfield.ymmword))))
b6f8c7c4 4195 && ((i.tm.base_opcode == 0x55
b6f8c7c4 4196 || i.tm.base_opcode == 0x57
35648716
JB
4197 || i.tm.base_opcode == 0xdf
4198 || i.tm.base_opcode == 0xef
4199 || i.tm.base_opcode == 0xf8
4200 || i.tm.base_opcode == 0xf9
4201 || i.tm.base_opcode == 0xfa
4202 || i.tm.base_opcode == 0xfb
1424ad86 4203 || i.tm.base_opcode == 0x42
35648716 4204 || i.tm.base_opcode == 0x47)
b6f8c7c4
L
4205 && i.tm.extension_opcode == None))
4206 {
99112332 4207 /* Optimize: -O1:
8305403a
L
4208 VOP, one of vandnps, vandnpd, vxorps, vxorpd, vpsubb, vpsubd,
4209 vpsubq and vpsubw:
b6f8c7c4
L
4210 EVEX VOP %zmmM, %zmmM, %zmmN
4211 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4212 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4213 EVEX VOP %ymmM, %ymmM, %ymmN
4214 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4215 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4216 VEX VOP %ymmM, %ymmM, %ymmN
4217 -> VEX VOP %xmmM, %xmmM, %xmmN
4218 VOP, one of vpandn and vpxor:
4219 VEX VOP %ymmM, %ymmM, %ymmN
4220 -> VEX VOP %xmmM, %xmmM, %xmmN
4221 VOP, one of vpandnd and vpandnq:
4222 EVEX VOP %zmmM, %zmmM, %zmmN
4223 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4224 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4225 EVEX VOP %ymmM, %ymmM, %ymmN
4226 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4227 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4228 VOP, one of vpxord and vpxorq:
4229 EVEX VOP %zmmM, %zmmM, %zmmN
4230 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4231 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4232 EVEX VOP %ymmM, %ymmM, %ymmN
4233 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4234 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
1424ad86
JB
4235 VOP, one of kxord and kxorq:
4236 VEX VOP %kM, %kM, %kN
4237 -> VEX kxorw %kM, %kM, %kN
4238 VOP, one of kandnd and kandnq:
4239 VEX VOP %kM, %kM, %kN
4240 -> VEX kandnw %kM, %kM, %kN
b6f8c7c4 4241 */
e771e7c9 4242 if (is_evex_encoding (&i.tm))
b6f8c7c4 4243 {
7b1d7ca1 4244 if (i.vec_encoding != vex_encoding_evex)
b6f8c7c4
L
4245 {
4246 i.tm.opcode_modifier.vex = VEX128;
4247 i.tm.opcode_modifier.vexw = VEXW0;
4248 i.tm.opcode_modifier.evex = 0;
4249 }
7b1d7ca1 4250 else if (optimize > 1)
dd22218c
L
4251 i.tm.opcode_modifier.evex = EVEX128;
4252 else
4253 return;
b6f8c7c4 4254 }
f74a6307 4255 else if (i.tm.operand_types[0].bitfield.class == RegMask)
1424ad86 4256 {
35648716 4257 i.tm.opcode_modifier.opcodeprefix = PREFIX_NONE;
1424ad86
JB
4258 i.tm.opcode_modifier.vexw = VEXW0;
4259 }
b6f8c7c4
L
4260 else
4261 i.tm.opcode_modifier.vex = VEX128;
4262
4263 if (i.tm.opcode_modifier.vex)
4264 for (j = 0; j < 3; j++)
4265 {
4266 i.types[j].bitfield.xmmword = 1;
4267 i.types[j].bitfield.ymmword = 0;
4268 }
4269 }
392a5972 4270 else if (i.vec_encoding != vex_encoding_evex
97ed31ae 4271 && !i.types[0].bitfield.zmmword
392a5972 4272 && !i.types[1].bitfield.zmmword
6225c532 4273 && !i.mask.reg
5273a3cd 4274 && !i.broadcast.type
97ed31ae 4275 && is_evex_encoding (&i.tm)
35648716
JB
4276 && ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4277 || (i.tm.base_opcode & ~4) == 0xdb
4278 || (i.tm.base_opcode & ~4) == 0xeb)
97ed31ae
L
4279 && i.tm.extension_opcode == None)
4280 {
4281 /* Optimize: -O1:
4282 VOP, one of vmovdqa32, vmovdqa64, vmovdqu8, vmovdqu16,
4283 vmovdqu32 and vmovdqu64:
4284 EVEX VOP %xmmM, %xmmN
4285 -> VEX vmovdqa|vmovdqu %xmmM, %xmmN (M and N < 16)
4286 EVEX VOP %ymmM, %ymmN
4287 -> VEX vmovdqa|vmovdqu %ymmM, %ymmN (M and N < 16)
4288 EVEX VOP %xmmM, mem
4289 -> VEX vmovdqa|vmovdqu %xmmM, mem (M < 16)
4290 EVEX VOP %ymmM, mem
4291 -> VEX vmovdqa|vmovdqu %ymmM, mem (M < 16)
4292 EVEX VOP mem, %xmmN
4293 -> VEX mvmovdqa|vmovdquem, %xmmN (N < 16)
4294 EVEX VOP mem, %ymmN
4295 -> VEX vmovdqa|vmovdqu mem, %ymmN (N < 16)
a0a1771e
JB
4296 VOP, one of vpand, vpandn, vpor, vpxor:
4297 EVEX VOP{d,q} %xmmL, %xmmM, %xmmN
4298 -> VEX VOP %xmmL, %xmmM, %xmmN (L, M, and N < 16)
4299 EVEX VOP{d,q} %ymmL, %ymmM, %ymmN
4300 -> VEX VOP %ymmL, %ymmM, %ymmN (L, M, and N < 16)
4301 EVEX VOP{d,q} mem, %xmmM, %xmmN
4302 -> VEX VOP mem, %xmmM, %xmmN (M and N < 16)
4303 EVEX VOP{d,q} mem, %ymmM, %ymmN
4304 -> VEX VOP mem, %ymmM, %ymmN (M and N < 16)
97ed31ae 4305 */
a0a1771e 4306 for (j = 0; j < i.operands; j++)
392a5972
L
4307 if (operand_type_check (i.types[j], disp)
4308 && i.op[j].disps->X_op == O_constant)
4309 {
4310 /* Since the VEX prefix has 2 or 3 bytes, the EVEX prefix
4311 has 4 bytes, EVEX Disp8 has 1 byte and VEX Disp32 has 4
4312 bytes, we choose EVEX Disp8 over VEX Disp32. */
4313 int evex_disp8, vex_disp8;
4314 unsigned int memshift = i.memshift;
4315 offsetT n = i.op[j].disps->X_add_number;
4316
4317 evex_disp8 = fits_in_disp8 (n);
4318 i.memshift = 0;
4319 vex_disp8 = fits_in_disp8 (n);
4320 if (evex_disp8 != vex_disp8)
4321 {
4322 i.memshift = memshift;
4323 return;
4324 }
4325
4326 i.types[j].bitfield.disp8 = vex_disp8;
4327 break;
4328 }
35648716
JB
4329 if ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4330 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2)
4331 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
97ed31ae
L
4332 i.tm.opcode_modifier.vex
4333 = i.types[0].bitfield.ymmword ? VEX256 : VEX128;
4334 i.tm.opcode_modifier.vexw = VEXW0;
79dec6b7 4335 /* VPAND, VPOR, and VPXOR are commutative. */
35648716 4336 if (i.reg_operands == 3 && i.tm.base_opcode != 0xdf)
79dec6b7 4337 i.tm.opcode_modifier.commutative = 1;
97ed31ae
L
4338 i.tm.opcode_modifier.evex = 0;
4339 i.tm.opcode_modifier.masking = 0;
a0a1771e 4340 i.tm.opcode_modifier.broadcast = 0;
97ed31ae
L
4341 i.tm.opcode_modifier.disp8memshift = 0;
4342 i.memshift = 0;
a0a1771e
JB
4343 if (j < i.operands)
4344 i.types[j].bitfield.disp8
4345 = fits_in_disp8 (i.op[j].disps->X_add_number);
97ed31ae 4346 }
b6f8c7c4
L
4347}
4348
ae531041
L
4349/* Return non-zero for load instruction. */
4350
4351static int
4352load_insn_p (void)
4353{
4354 unsigned int dest;
4355 int any_vex_p = is_any_vex_encoding (&i.tm);
4356 unsigned int base_opcode = i.tm.base_opcode | 1;
4357
4358 if (!any_vex_p)
4359 {
a09f656b 4360 /* Anysize insns: lea, invlpg, clflush, prefetchnta, prefetcht0,
4361 prefetcht1, prefetcht2, prefetchtw, bndmk, bndcl, bndcu, bndcn,
4362 bndstx, bndldx, prefetchwt1, clflushopt, clwb, cldemote. */
4363 if (i.tm.opcode_modifier.anysize)
ae531041
L
4364 return 0;
4365
389d00a5
JB
4366 /* pop. */
4367 if (strcmp (i.tm.name, "pop") == 0)
4368 return 1;
4369 }
4370
4371 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE)
4372 {
4373 /* popf, popa. */
4374 if (i.tm.base_opcode == 0x9d
a09f656b 4375 || i.tm.base_opcode == 0x61)
ae531041
L
4376 return 1;
4377
4378 /* movs, cmps, lods, scas. */
4379 if ((i.tm.base_opcode | 0xb) == 0xaf)
4380 return 1;
4381
a09f656b 4382 /* outs, xlatb. */
4383 if (base_opcode == 0x6f
4384 || i.tm.base_opcode == 0xd7)
ae531041 4385 return 1;
a09f656b 4386 /* NB: For AMD-specific insns with implicit memory operands,
4387 they're intentionally not covered. */
ae531041
L
4388 }
4389
4390 /* No memory operand. */
4391 if (!i.mem_operands)
4392 return 0;
4393
4394 if (any_vex_p)
4395 {
4396 /* vldmxcsr. */
4397 if (i.tm.base_opcode == 0xae
4398 && i.tm.opcode_modifier.vex
441f6aca 4399 && i.tm.opcode_modifier.opcodespace == SPACE_0F
35648716 4400 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
ae531041
L
4401 && i.tm.extension_opcode == 2)
4402 return 1;
4403 }
389d00a5 4404 else if (i.tm.opcode_modifier.opcodespace == SPACE_BASE)
ae531041
L
4405 {
4406 /* test, not, neg, mul, imul, div, idiv. */
4407 if ((i.tm.base_opcode == 0xf6 || i.tm.base_opcode == 0xf7)
4408 && i.tm.extension_opcode != 1)
4409 return 1;
4410
4411 /* inc, dec. */
4412 if (base_opcode == 0xff && i.tm.extension_opcode <= 1)
4413 return 1;
4414
4415 /* add, or, adc, sbb, and, sub, xor, cmp. */
4416 if (i.tm.base_opcode >= 0x80 && i.tm.base_opcode <= 0x83)
4417 return 1;
4418
ae531041
L
4419 /* rol, ror, rcl, rcr, shl/sal, shr, sar. */
4420 if ((base_opcode == 0xc1
4421 || (i.tm.base_opcode >= 0xd0 && i.tm.base_opcode <= 0xd3))
4422 && i.tm.extension_opcode != 6)
4423 return 1;
4424
ae531041 4425 /* Check for x87 instructions. */
389d00a5 4426 if (base_opcode >= 0xd8 && base_opcode <= 0xdf)
ae531041
L
4427 {
4428 /* Skip fst, fstp, fstenv, fstcw. */
4429 if (i.tm.base_opcode == 0xd9
4430 && (i.tm.extension_opcode == 2
4431 || i.tm.extension_opcode == 3
4432 || i.tm.extension_opcode == 6
4433 || i.tm.extension_opcode == 7))
4434 return 0;
4435
4436 /* Skip fisttp, fist, fistp, fstp. */
4437 if (i.tm.base_opcode == 0xdb
4438 && (i.tm.extension_opcode == 1
4439 || i.tm.extension_opcode == 2
4440 || i.tm.extension_opcode == 3
4441 || i.tm.extension_opcode == 7))
4442 return 0;
4443
4444 /* Skip fisttp, fst, fstp, fsave, fstsw. */
4445 if (i.tm.base_opcode == 0xdd
4446 && (i.tm.extension_opcode == 1
4447 || i.tm.extension_opcode == 2
4448 || i.tm.extension_opcode == 3
4449 || i.tm.extension_opcode == 6
4450 || i.tm.extension_opcode == 7))
4451 return 0;
4452
4453 /* Skip fisttp, fist, fistp, fbstp, fistp. */
4454 if (i.tm.base_opcode == 0xdf
4455 && (i.tm.extension_opcode == 1
4456 || i.tm.extension_opcode == 2
4457 || i.tm.extension_opcode == 3
4458 || i.tm.extension_opcode == 6
4459 || i.tm.extension_opcode == 7))
4460 return 0;
4461
4462 return 1;
4463 }
4464 }
389d00a5
JB
4465 else if (i.tm.opcode_modifier.opcodespace == SPACE_0F)
4466 {
4467 /* bt, bts, btr, btc. */
4468 if (i.tm.base_opcode == 0xba
4469 && (i.tm.extension_opcode >= 4 && i.tm.extension_opcode <= 7))
4470 return 1;
4471
4472 /* cmpxchg8b, cmpxchg16b, xrstors, vmptrld. */
4473 if (i.tm.base_opcode == 0xc7
4474 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
4475 && (i.tm.extension_opcode == 1 || i.tm.extension_opcode == 3
4476 || i.tm.extension_opcode == 6))
4477 return 1;
4478
4479 /* fxrstor, ldmxcsr, xrstor. */
4480 if (i.tm.base_opcode == 0xae
4481 && (i.tm.extension_opcode == 1
4482 || i.tm.extension_opcode == 2
4483 || i.tm.extension_opcode == 5))
4484 return 1;
4485
4486 /* lgdt, lidt, lmsw. */
4487 if (i.tm.base_opcode == 0x01
4488 && (i.tm.extension_opcode == 2
4489 || i.tm.extension_opcode == 3
4490 || i.tm.extension_opcode == 6))
4491 return 1;
4492 }
ae531041
L
4493
4494 dest = i.operands - 1;
4495
4496 /* Check fake imm8 operand and 3 source operands. */
4497 if ((i.tm.opcode_modifier.immext
4498 || i.tm.opcode_modifier.vexsources == VEX3SOURCES)
4499 && i.types[dest].bitfield.imm8)
4500 dest--;
4501
389d00a5
JB
4502 /* add, or, adc, sbb, and, sub, xor, cmp, test, xchg. */
4503 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
ae531041
L
4504 && (base_opcode == 0x1
4505 || base_opcode == 0x9
4506 || base_opcode == 0x11
4507 || base_opcode == 0x19
4508 || base_opcode == 0x21
4509 || base_opcode == 0x29
4510 || base_opcode == 0x31
4511 || base_opcode == 0x39
389d00a5
JB
4512 || (base_opcode | 2) == 0x87))
4513 return 1;
4514
4515 /* xadd. */
4516 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4517 && base_opcode == 0xc1)
ae531041
L
4518 return 1;
4519
4520 /* Check for load instruction. */
4521 return (i.types[dest].bitfield.class != ClassNone
4522 || i.types[dest].bitfield.instance == Accum);
4523}
4524
4525/* Output lfence, 0xfaee8, after instruction. */
4526
4527static void
4528insert_lfence_after (void)
4529{
4530 if (lfence_after_load && load_insn_p ())
4531 {
a09f656b 4532 /* There are also two REP string instructions that require
4533 special treatment. Specifically, the compare string (CMPS)
4534 and scan string (SCAS) instructions set EFLAGS in a manner
4535 that depends on the data being compared/scanned. When used
4536 with a REP prefix, the number of iterations may therefore
4537 vary depending on this data. If the data is a program secret
4538 chosen by the adversary using an LVI method,
4539 then this data-dependent behavior may leak some aspect
4540 of the secret. */
4541 if (((i.tm.base_opcode | 0x1) == 0xa7
4542 || (i.tm.base_opcode | 0x1) == 0xaf)
4543 && i.prefix[REP_PREFIX])
4544 {
4545 as_warn (_("`%s` changes flags which would affect control flow behavior"),
4546 i.tm.name);
4547 }
ae531041
L
4548 char *p = frag_more (3);
4549 *p++ = 0xf;
4550 *p++ = 0xae;
4551 *p = 0xe8;
4552 }
4553}
4554
4555/* Output lfence, 0xfaee8, before instruction. */
4556
4557static void
4558insert_lfence_before (void)
4559{
4560 char *p;
4561
389d00a5 4562 if (i.tm.opcode_modifier.opcodespace != SPACE_BASE)
ae531041
L
4563 return;
4564
4565 if (i.tm.base_opcode == 0xff
4566 && (i.tm.extension_opcode == 2 || i.tm.extension_opcode == 4))
4567 {
4568 /* Insert lfence before indirect branch if needed. */
4569
4570 if (lfence_before_indirect_branch == lfence_branch_none)
4571 return;
4572
4573 if (i.operands != 1)
4574 abort ();
4575
4576 if (i.reg_operands == 1)
4577 {
4578 /* Indirect branch via register. Don't insert lfence with
4579 -mlfence-after-load=yes. */
4580 if (lfence_after_load
4581 || lfence_before_indirect_branch == lfence_branch_memory)
4582 return;
4583 }
4584 else if (i.mem_operands == 1
4585 && lfence_before_indirect_branch != lfence_branch_register)
4586 {
4587 as_warn (_("indirect `%s` with memory operand should be avoided"),
4588 i.tm.name);
4589 return;
4590 }
4591 else
4592 return;
4593
4594 if (last_insn.kind != last_insn_other
4595 && last_insn.seg == now_seg)
4596 {
4597 as_warn_where (last_insn.file, last_insn.line,
4598 _("`%s` skips -mlfence-before-indirect-branch on `%s`"),
4599 last_insn.name, i.tm.name);
4600 return;
4601 }
4602
4603 p = frag_more (3);
4604 *p++ = 0xf;
4605 *p++ = 0xae;
4606 *p = 0xe8;
4607 return;
4608 }
4609
503648e4 4610 /* Output or/not/shl and lfence before near ret. */
ae531041
L
4611 if (lfence_before_ret != lfence_before_ret_none
4612 && (i.tm.base_opcode == 0xc2
503648e4 4613 || i.tm.base_opcode == 0xc3))
ae531041
L
4614 {
4615 if (last_insn.kind != last_insn_other
4616 && last_insn.seg == now_seg)
4617 {
4618 as_warn_where (last_insn.file, last_insn.line,
4619 _("`%s` skips -mlfence-before-ret on `%s`"),
4620 last_insn.name, i.tm.name);
4621 return;
4622 }
a09f656b 4623
a09f656b 4624 /* Near ret ingore operand size override under CPU64. */
503648e4 4625 char prefix = flag_code == CODE_64BIT
4626 ? 0x48
4627 : i.prefix[DATA_PREFIX] ? 0x66 : 0x0;
a09f656b 4628
4629 if (lfence_before_ret == lfence_before_ret_not)
4630 {
4631 /* not: 0xf71424, may add prefix
4632 for operand size override or 64-bit code. */
4633 p = frag_more ((prefix ? 2 : 0) + 6 + 3);
4634 if (prefix)
4635 *p++ = prefix;
ae531041
L
4636 *p++ = 0xf7;
4637 *p++ = 0x14;
4638 *p++ = 0x24;
a09f656b 4639 if (prefix)
4640 *p++ = prefix;
ae531041
L
4641 *p++ = 0xf7;
4642 *p++ = 0x14;
4643 *p++ = 0x24;
4644 }
a09f656b 4645 else
4646 {
4647 p = frag_more ((prefix ? 1 : 0) + 4 + 3);
4648 if (prefix)
4649 *p++ = prefix;
4650 if (lfence_before_ret == lfence_before_ret_or)
4651 {
4652 /* or: 0x830c2400, may add prefix
4653 for operand size override or 64-bit code. */
4654 *p++ = 0x83;
4655 *p++ = 0x0c;
4656 }
4657 else
4658 {
4659 /* shl: 0xc1242400, may add prefix
4660 for operand size override or 64-bit code. */
4661 *p++ = 0xc1;
4662 *p++ = 0x24;
4663 }
4664
4665 *p++ = 0x24;
4666 *p++ = 0x0;
4667 }
4668
ae531041
L
4669 *p++ = 0xf;
4670 *p++ = 0xae;
4671 *p = 0xe8;
4672 }
4673}
4674
252b5132
RH
4675/* This is the guts of the machine-dependent assembler. LINE points to a
4676 machine dependent instruction. This function is supposed to emit
4677 the frags/bytes it assembles to. */
4678
4679void
65da13b5 4680md_assemble (char *line)
252b5132 4681{
40fb9820 4682 unsigned int j;
83b16ac6 4683 char mnemonic[MAX_MNEM_SIZE], mnem_suffix;
d3ce72d0 4684 const insn_template *t;
252b5132 4685
47926f60 4686 /* Initialize globals. */
252b5132 4687 memset (&i, '\0', sizeof (i));
ca5312a2 4688 i.rounding.type = rc_none;
252b5132 4689 for (j = 0; j < MAX_OPERANDS; j++)
1ae12ab7 4690 i.reloc[j] = NO_RELOC;
252b5132
RH
4691 memset (disp_expressions, '\0', sizeof (disp_expressions));
4692 memset (im_expressions, '\0', sizeof (im_expressions));
ce8a8b2f 4693 save_stack_p = save_stack;
252b5132
RH
4694
4695 /* First parse an instruction mnemonic & call i386_operand for the operands.
4696 We assume that the scrubber has arranged it so that line[0] is the valid
47926f60 4697 start of a (possibly prefixed) mnemonic. */
252b5132 4698
29b0f896
AM
4699 line = parse_insn (line, mnemonic);
4700 if (line == NULL)
4701 return;
83b16ac6 4702 mnem_suffix = i.suffix;
252b5132 4703
29b0f896 4704 line = parse_operands (line, mnemonic);
ee86248c 4705 this_operand = -1;
8325cc63
JB
4706 xfree (i.memop1_string);
4707 i.memop1_string = NULL;
29b0f896
AM
4708 if (line == NULL)
4709 return;
252b5132 4710
29b0f896
AM
4711 /* Now we've parsed the mnemonic into a set of templates, and have the
4712 operands at hand. */
4713
b630c145 4714 /* All Intel opcodes have reversed operands except for "bound", "enter",
c0e54661
JB
4715 "invlpg*", "monitor*", "mwait*", "tpause", "umwait", "pvalidate",
4716 "rmpadjust", and "rmpupdate". We also don't reverse intersegment "jmp"
4717 and "call" instructions with 2 immediate operands so that the immediate
4718 segment precedes the offset consistently in Intel and AT&T modes. */
4d456e3d
L
4719 if (intel_syntax
4720 && i.operands > 1
29b0f896 4721 && (strcmp (mnemonic, "bound") != 0)
c0e54661 4722 && (strncmp (mnemonic, "invlpg", 6) != 0)
d34049e8
ML
4723 && !startswith (mnemonic, "monitor")
4724 && !startswith (mnemonic, "mwait")
c0e54661 4725 && (strcmp (mnemonic, "pvalidate") != 0)
d34049e8 4726 && !startswith (mnemonic, "rmp")
b630c145
JB
4727 && (strcmp (mnemonic, "tpause") != 0)
4728 && (strcmp (mnemonic, "umwait") != 0)
40fb9820
L
4729 && !(operand_type_check (i.types[0], imm)
4730 && operand_type_check (i.types[1], imm)))
29b0f896
AM
4731 swap_operands ();
4732
ec56d5c0
JB
4733 /* The order of the immediates should be reversed
4734 for 2 immediates extrq and insertq instructions */
4735 if (i.imm_operands == 2
4736 && (strcmp (mnemonic, "extrq") == 0
4737 || strcmp (mnemonic, "insertq") == 0))
4738 swap_2_operands (0, 1);
4739
29b0f896
AM
4740 if (i.imm_operands)
4741 optimize_imm ();
4742
cce08655
JB
4743 if (i.disp_operands && flag_code == CODE_64BIT && !i.prefix[ADDR_PREFIX])
4744 {
4745 for (j = 0; j < i.operands; ++j)
4746 {
4747 const expressionS *exp = i.op[j].disps;
4748
4749 if (!operand_type_check (i.types[j], disp))
4750 continue;
4751
4752 if (exp->X_op != O_constant)
4753 continue;
4754
4755 /* Since displacement is signed extended to 64bit, don't allow
4756 disp32 and turn off disp32s if they are out of range. */
4757 i.types[j].bitfield.disp32 = 0;
4758 if (fits_in_signed_long (exp->X_add_number))
4759 continue;
4760
4761 i.types[j].bitfield.disp32s = 0;
4762 if (i.types[j].bitfield.baseindex)
4763 {
4764 as_bad (_("0x%" BFD_VMA_FMT "x out of range of signed 32bit displacement"),
4765 exp->X_add_number);
4766 return;
4767 }
4768 }
4769 }
4770
b300c311
L
4771 /* Don't optimize displacement for movabs since it only takes 64bit
4772 displacement. */
4773 if (i.disp_operands
a501d77e 4774 && i.disp_encoding != disp_encoding_32bit
862be3fb
L
4775 && (flag_code != CODE_64BIT
4776 || strcmp (mnemonic, "movabs") != 0))
4777 optimize_disp ();
29b0f896
AM
4778
4779 /* Next, we find a template that matches the given insn,
4780 making sure the overlap of the given operands types is consistent
4781 with the template operand types. */
252b5132 4782
83b16ac6 4783 if (!(t = match_template (mnem_suffix)))
29b0f896 4784 return;
252b5132 4785
7bab8ab5 4786 if (sse_check != check_none
81f8a913 4787 && !i.tm.opcode_modifier.noavx
6e3e5c9e 4788 && !i.tm.cpu_flags.bitfield.cpuavx
569d50f1 4789 && !i.tm.cpu_flags.bitfield.cpuavx512f
daf50ae7
L
4790 && (i.tm.cpu_flags.bitfield.cpusse
4791 || i.tm.cpu_flags.bitfield.cpusse2
4792 || i.tm.cpu_flags.bitfield.cpusse3
4793 || i.tm.cpu_flags.bitfield.cpussse3
4794 || i.tm.cpu_flags.bitfield.cpusse4_1
6e3e5c9e
JB
4795 || i.tm.cpu_flags.bitfield.cpusse4_2
4796 || i.tm.cpu_flags.bitfield.cpupclmul
4797 || i.tm.cpu_flags.bitfield.cpuaes
569d50f1 4798 || i.tm.cpu_flags.bitfield.cpusha
6e3e5c9e 4799 || i.tm.cpu_flags.bitfield.cpugfni))
daf50ae7 4800 {
7bab8ab5 4801 (sse_check == check_warning
daf50ae7
L
4802 ? as_warn
4803 : as_bad) (_("SSE instruction `%s' is used"), i.tm.name);
4804 }
4805
40fb9820 4806 if (i.tm.opcode_modifier.fwait)
29b0f896
AM
4807 if (!add_prefix (FWAIT_OPCODE))
4808 return;
252b5132 4809
d5de92cf 4810 /* Check if REP prefix is OK. */
742732c7 4811 if (i.rep_prefix && i.tm.opcode_modifier.prefixok != PrefixRep)
d5de92cf
L
4812 {
4813 as_bad (_("invalid instruction `%s' after `%s'"),
4814 i.tm.name, i.rep_prefix);
4815 return;
4816 }
4817
c1ba0266
L
4818 /* Check for lock without a lockable instruction. Destination operand
4819 must be memory unless it is xchg (0x86). */
c32fa91d 4820 if (i.prefix[LOCK_PREFIX]
742732c7 4821 && (i.tm.opcode_modifier.prefixok < PrefixLock
c1ba0266
L
4822 || i.mem_operands == 0
4823 || (i.tm.base_opcode != 0x86
8dc0818e 4824 && !(i.flags[i.operands - 1] & Operand_Mem))))
c32fa91d
L
4825 {
4826 as_bad (_("expecting lockable instruction after `lock'"));
4827 return;
4828 }
4829
40d231b4
JB
4830 /* Check for data size prefix on VEX/XOP/EVEX encoded and SIMD insns. */
4831 if (i.prefix[DATA_PREFIX]
4832 && (is_any_vex_encoding (&i.tm)
4833 || i.tm.operand_types[i.imm_operands].bitfield.class >= RegMMX
4834 || i.tm.operand_types[i.imm_operands + 1].bitfield.class >= RegMMX))
7a8655d2
JB
4835 {
4836 as_bad (_("data size prefix invalid with `%s'"), i.tm.name);
4837 return;
4838 }
4839
42164a71 4840 /* Check if HLE prefix is OK. */
165de32a 4841 if (i.hle_prefix && !check_hle ())
42164a71
L
4842 return;
4843
7e8b059b
L
4844 /* Check BND prefix. */
4845 if (i.bnd_prefix && !i.tm.opcode_modifier.bndprefixok)
4846 as_bad (_("expecting valid branch instruction after `bnd'"));
4847
04ef582a 4848 /* Check NOTRACK prefix. */
742732c7 4849 if (i.notrack_prefix && i.tm.opcode_modifier.prefixok != PrefixNoTrack)
9fef80d6 4850 as_bad (_("expecting indirect branch instruction after `notrack'"));
04ef582a 4851
327e8c42
JB
4852 if (i.tm.cpu_flags.bitfield.cpumpx)
4853 {
4854 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
4855 as_bad (_("32-bit address isn't allowed in 64-bit MPX instructions."));
4856 else if (flag_code != CODE_16BIT
4857 ? i.prefix[ADDR_PREFIX]
4858 : i.mem_operands && !i.prefix[ADDR_PREFIX])
4859 as_bad (_("16-bit address isn't allowed in MPX instructions"));
4860 }
7e8b059b
L
4861
4862 /* Insert BND prefix. */
76d3a78a
JB
4863 if (add_bnd_prefix && i.tm.opcode_modifier.bndprefixok)
4864 {
4865 if (!i.prefix[BND_PREFIX])
4866 add_prefix (BND_PREFIX_OPCODE);
4867 else if (i.prefix[BND_PREFIX] != BND_PREFIX_OPCODE)
4868 {
4869 as_warn (_("replacing `rep'/`repe' prefix by `bnd'"));
4870 i.prefix[BND_PREFIX] = BND_PREFIX_OPCODE;
4871 }
4872 }
7e8b059b 4873
29b0f896 4874 /* Check string instruction segment overrides. */
51c8edf6 4875 if (i.tm.opcode_modifier.isstring >= IS_STRING_ES_OP0)
29b0f896 4876 {
51c8edf6 4877 gas_assert (i.mem_operands);
29b0f896 4878 if (!check_string ())
5dd0794d 4879 return;
fc0763e6 4880 i.disp_operands = 0;
29b0f896 4881 }
5dd0794d 4882
b6f8c7c4
L
4883 if (optimize && !i.no_optimize && i.tm.opcode_modifier.optimize)
4884 optimize_encoding ();
4885
29b0f896
AM
4886 if (!process_suffix ())
4887 return;
e413e4e9 4888
921eafea 4889 /* Update operand types and check extended states. */
bc0844ae 4890 for (j = 0; j < i.operands; j++)
921eafea
L
4891 {
4892 i.types[j] = operand_type_and (i.types[j], i.tm.operand_types[j]);
3d70986f 4893 switch (i.tm.operand_types[j].bitfield.class)
921eafea
L
4894 {
4895 default:
4896 break;
4897 case RegMMX:
4898 i.xstate |= xstate_mmx;
4899 break;
4900 case RegMask:
32930e4e 4901 i.xstate |= xstate_mask;
921eafea
L
4902 break;
4903 case RegSIMD:
3d70986f 4904 if (i.tm.operand_types[j].bitfield.tmmword)
921eafea 4905 i.xstate |= xstate_tmm;
3d70986f 4906 else if (i.tm.operand_types[j].bitfield.zmmword)
921eafea 4907 i.xstate |= xstate_zmm;
3d70986f 4908 else if (i.tm.operand_types[j].bitfield.ymmword)
921eafea 4909 i.xstate |= xstate_ymm;
3d70986f 4910 else if (i.tm.operand_types[j].bitfield.xmmword)
921eafea
L
4911 i.xstate |= xstate_xmm;
4912 break;
4913 }
4914 }
bc0844ae 4915
29b0f896
AM
4916 /* Make still unresolved immediate matches conform to size of immediate
4917 given in i.suffix. */
4918 if (!finalize_imm ())
4919 return;
252b5132 4920
40fb9820 4921 if (i.types[0].bitfield.imm1)
29b0f896 4922 i.imm_operands = 0; /* kludge for shift insns. */
252b5132 4923
9afe6eb8
L
4924 /* We only need to check those implicit registers for instructions
4925 with 3 operands or less. */
4926 if (i.operands <= 3)
4927 for (j = 0; j < i.operands; j++)
75e5731b
JB
4928 if (i.types[j].bitfield.instance != InstanceNone
4929 && !i.types[j].bitfield.xmmword)
9afe6eb8 4930 i.reg_operands--;
40fb9820 4931
29b0f896
AM
4932 /* For insns with operands there are more diddles to do to the opcode. */
4933 if (i.operands)
4934 {
4935 if (!process_operands ())
4936 return;
4937 }
8c190ce0 4938 else if (!quiet_warnings && i.tm.opcode_modifier.ugh)
29b0f896
AM
4939 {
4940 /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc. */
4941 as_warn (_("translating to `%sp'"), i.tm.name);
4942 }
252b5132 4943
7a8655d2 4944 if (is_any_vex_encoding (&i.tm))
9e5e5283 4945 {
c1dc7af5 4946 if (!cpu_arch_flags.bitfield.cpui286)
9e5e5283 4947 {
c1dc7af5 4948 as_bad (_("instruction `%s' isn't supported outside of protected mode."),
9e5e5283
L
4949 i.tm.name);
4950 return;
4951 }
c0f3af97 4952
0b9404fd
JB
4953 /* Check for explicit REX prefix. */
4954 if (i.prefix[REX_PREFIX] || i.rex_encoding)
4955 {
4956 as_bad (_("REX prefix invalid with `%s'"), i.tm.name);
4957 return;
4958 }
4959
9e5e5283
L
4960 if (i.tm.opcode_modifier.vex)
4961 build_vex_prefix (t);
4962 else
4963 build_evex_prefix ();
0b9404fd
JB
4964
4965 /* The individual REX.RXBW bits got consumed. */
4966 i.rex &= REX_OPCODE;
9e5e5283 4967 }
43234a1e 4968
5dd85c99
SP
4969 /* Handle conversion of 'int $3' --> special int3 insn. XOP or FMA4
4970 instructions may define INT_OPCODE as well, so avoid this corner
4971 case for those instructions that use MODRM. */
389d00a5
JB
4972 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
4973 && i.tm.base_opcode == INT_OPCODE
a6461c02
SP
4974 && !i.tm.opcode_modifier.modrm
4975 && i.op[0].imms->X_add_number == 3)
29b0f896
AM
4976 {
4977 i.tm.base_opcode = INT3_OPCODE;
4978 i.imm_operands = 0;
4979 }
252b5132 4980
0cfa3eb3
JB
4981 if ((i.tm.opcode_modifier.jump == JUMP
4982 || i.tm.opcode_modifier.jump == JUMP_BYTE
4983 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896
AM
4984 && i.op[0].disps->X_op == O_constant)
4985 {
4986 /* Convert "jmp constant" (and "call constant") to a jump (call) to
4987 the absolute address given by the constant. Since ix86 jumps and
4988 calls are pc relative, we need to generate a reloc. */
4989 i.op[0].disps->X_add_symbol = &abs_symbol;
4990 i.op[0].disps->X_op = O_symbol;
4991 }
252b5132 4992
29b0f896
AM
4993 /* For 8 bit registers we need an empty rex prefix. Also if the
4994 instruction already has a prefix, we need to convert old
4995 registers to new ones. */
773f551c 4996
bab6aec1 4997 if ((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte
29b0f896 4998 && (i.op[0].regs->reg_flags & RegRex64) != 0)
bab6aec1 4999 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte
29b0f896 5000 && (i.op[1].regs->reg_flags & RegRex64) != 0)
bab6aec1
JB
5001 || (((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte)
5002 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte))
29b0f896
AM
5003 && i.rex != 0))
5004 {
5005 int x;
726c5dcd 5006
29b0f896
AM
5007 i.rex |= REX_OPCODE;
5008 for (x = 0; x < 2; x++)
5009 {
5010 /* Look for 8 bit operand that uses old registers. */
bab6aec1 5011 if (i.types[x].bitfield.class == Reg && i.types[x].bitfield.byte
29b0f896 5012 && (i.op[x].regs->reg_flags & RegRex64) == 0)
773f551c 5013 {
3f93af61 5014 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
29b0f896
AM
5015 /* In case it is "hi" register, give up. */
5016 if (i.op[x].regs->reg_num > 3)
a540244d 5017 as_bad (_("can't encode register '%s%s' in an "
4eed87de 5018 "instruction requiring REX prefix."),
a540244d 5019 register_prefix, i.op[x].regs->reg_name);
773f551c 5020
29b0f896
AM
5021 /* Otherwise it is equivalent to the extended register.
5022 Since the encoding doesn't change this is merely
5023 cosmetic cleanup for debug output. */
5024
5025 i.op[x].regs = i.op[x].regs + 8;
773f551c 5026 }
29b0f896
AM
5027 }
5028 }
773f551c 5029
6b6b6807
L
5030 if (i.rex == 0 && i.rex_encoding)
5031 {
5032 /* Check if we can add a REX_OPCODE byte. Look for 8 bit operand
3f93af61 5033 that uses legacy register. If it is "hi" register, don't add
6b6b6807
L
5034 the REX_OPCODE byte. */
5035 int x;
5036 for (x = 0; x < 2; x++)
bab6aec1 5037 if (i.types[x].bitfield.class == Reg
6b6b6807
L
5038 && i.types[x].bitfield.byte
5039 && (i.op[x].regs->reg_flags & RegRex64) == 0
5040 && i.op[x].regs->reg_num > 3)
5041 {
3f93af61 5042 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
5b7c81bd 5043 i.rex_encoding = false;
6b6b6807
L
5044 break;
5045 }
5046
5047 if (i.rex_encoding)
5048 i.rex = REX_OPCODE;
5049 }
5050
7ab9ffdd 5051 if (i.rex != 0)
29b0f896
AM
5052 add_prefix (REX_OPCODE | i.rex);
5053
ae531041
L
5054 insert_lfence_before ();
5055
29b0f896
AM
5056 /* We are ready to output the insn. */
5057 output_insn ();
e379e5f3 5058
ae531041
L
5059 insert_lfence_after ();
5060
e379e5f3
L
5061 last_insn.seg = now_seg;
5062
5063 if (i.tm.opcode_modifier.isprefix)
5064 {
5065 last_insn.kind = last_insn_prefix;
5066 last_insn.name = i.tm.name;
5067 last_insn.file = as_where (&last_insn.line);
5068 }
5069 else
5070 last_insn.kind = last_insn_other;
29b0f896
AM
5071}
5072
5073static char *
e3bb37b5 5074parse_insn (char *line, char *mnemonic)
29b0f896
AM
5075{
5076 char *l = line;
5077 char *token_start = l;
5078 char *mnem_p;
5c6af06e 5079 int supported;
d3ce72d0 5080 const insn_template *t;
b6169b20 5081 char *dot_p = NULL;
29b0f896 5082
29b0f896
AM
5083 while (1)
5084 {
5085 mnem_p = mnemonic;
5086 while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
5087 {
b6169b20
L
5088 if (*mnem_p == '.')
5089 dot_p = mnem_p;
29b0f896
AM
5090 mnem_p++;
5091 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
45288df1 5092 {
29b0f896
AM
5093 as_bad (_("no such instruction: `%s'"), token_start);
5094 return NULL;
5095 }
5096 l++;
5097 }
5098 if (!is_space_char (*l)
5099 && *l != END_OF_INSN
e44823cf
JB
5100 && (intel_syntax
5101 || (*l != PREFIX_SEPARATOR
5102 && *l != ',')))
29b0f896
AM
5103 {
5104 as_bad (_("invalid character %s in mnemonic"),
5105 output_invalid (*l));
5106 return NULL;
5107 }
5108 if (token_start == l)
5109 {
e44823cf 5110 if (!intel_syntax && *l == PREFIX_SEPARATOR)
29b0f896
AM
5111 as_bad (_("expecting prefix; got nothing"));
5112 else
5113 as_bad (_("expecting mnemonic; got nothing"));
5114 return NULL;
5115 }
45288df1 5116
29b0f896 5117 /* Look up instruction (or prefix) via hash table. */
629310ab 5118 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
47926f60 5119
29b0f896
AM
5120 if (*l != END_OF_INSN
5121 && (!is_space_char (*l) || l[1] != END_OF_INSN)
5122 && current_templates
40fb9820 5123 && current_templates->start->opcode_modifier.isprefix)
29b0f896 5124 {
c6fb90c8 5125 if (!cpu_flags_check_cpu64 (current_templates->start->cpu_flags))
2dd88dca
JB
5126 {
5127 as_bad ((flag_code != CODE_64BIT
5128 ? _("`%s' is only supported in 64-bit mode")
5129 : _("`%s' is not supported in 64-bit mode")),
5130 current_templates->start->name);
5131 return NULL;
5132 }
29b0f896
AM
5133 /* If we are in 16-bit mode, do not allow addr16 or data16.
5134 Similarly, in 32-bit mode, do not allow addr32 or data32. */
673fe0f0
JB
5135 if ((current_templates->start->opcode_modifier.size == SIZE16
5136 || current_templates->start->opcode_modifier.size == SIZE32)
29b0f896 5137 && flag_code != CODE_64BIT
673fe0f0 5138 && ((current_templates->start->opcode_modifier.size == SIZE32)
29b0f896
AM
5139 ^ (flag_code == CODE_16BIT)))
5140 {
5141 as_bad (_("redundant %s prefix"),
5142 current_templates->start->name);
5143 return NULL;
45288df1 5144 }
31184569
JB
5145
5146 if (current_templates->start->base_opcode == PSEUDO_PREFIX)
29b0f896 5147 {
86fa6981 5148 /* Handle pseudo prefixes. */
31184569 5149 switch (current_templates->start->extension_opcode)
86fa6981 5150 {
41eb8e88 5151 case Prefix_Disp8:
86fa6981
L
5152 /* {disp8} */
5153 i.disp_encoding = disp_encoding_8bit;
5154 break;
41eb8e88
L
5155 case Prefix_Disp16:
5156 /* {disp16} */
5157 i.disp_encoding = disp_encoding_16bit;
5158 break;
5159 case Prefix_Disp32:
86fa6981
L
5160 /* {disp32} */
5161 i.disp_encoding = disp_encoding_32bit;
5162 break;
41eb8e88 5163 case Prefix_Load:
86fa6981
L
5164 /* {load} */
5165 i.dir_encoding = dir_encoding_load;
5166 break;
41eb8e88 5167 case Prefix_Store:
86fa6981
L
5168 /* {store} */
5169 i.dir_encoding = dir_encoding_store;
5170 break;
41eb8e88 5171 case Prefix_VEX:
42e04b36
L
5172 /* {vex} */
5173 i.vec_encoding = vex_encoding_vex;
86fa6981 5174 break;
41eb8e88 5175 case Prefix_VEX3:
86fa6981
L
5176 /* {vex3} */
5177 i.vec_encoding = vex_encoding_vex3;
5178 break;
41eb8e88 5179 case Prefix_EVEX:
86fa6981
L
5180 /* {evex} */
5181 i.vec_encoding = vex_encoding_evex;
5182 break;
41eb8e88 5183 case Prefix_REX:
6b6b6807 5184 /* {rex} */
5b7c81bd 5185 i.rex_encoding = true;
6b6b6807 5186 break;
41eb8e88 5187 case Prefix_NoOptimize:
b6f8c7c4 5188 /* {nooptimize} */
5b7c81bd 5189 i.no_optimize = true;
b6f8c7c4 5190 break;
86fa6981
L
5191 default:
5192 abort ();
5193 }
5194 }
5195 else
5196 {
5197 /* Add prefix, checking for repeated prefixes. */
4e9ac44a 5198 switch (add_prefix (current_templates->start->base_opcode))
86fa6981 5199 {
4e9ac44a
L
5200 case PREFIX_EXIST:
5201 return NULL;
5202 case PREFIX_DS:
d777820b 5203 if (current_templates->start->cpu_flags.bitfield.cpuibt)
4e9ac44a
L
5204 i.notrack_prefix = current_templates->start->name;
5205 break;
5206 case PREFIX_REP:
5207 if (current_templates->start->cpu_flags.bitfield.cpuhle)
5208 i.hle_prefix = current_templates->start->name;
5209 else if (current_templates->start->cpu_flags.bitfield.cpumpx)
5210 i.bnd_prefix = current_templates->start->name;
5211 else
5212 i.rep_prefix = current_templates->start->name;
5213 break;
5214 default:
5215 break;
86fa6981 5216 }
29b0f896
AM
5217 }
5218 /* Skip past PREFIX_SEPARATOR and reset token_start. */
5219 token_start = ++l;
5220 }
5221 else
5222 break;
5223 }
45288df1 5224
30a55f88 5225 if (!current_templates)
b6169b20 5226 {
07d5e953
JB
5227 /* Deprecated functionality (new code should use pseudo-prefixes instead):
5228 Check if we should swap operand or force 32bit displacement in
f8a5c266 5229 encoding. */
30a55f88 5230 if (mnem_p - 2 == dot_p && dot_p[1] == 's')
64c49ab3 5231 i.dir_encoding = dir_encoding_swap;
8d63c93e 5232 else if (mnem_p - 3 == dot_p
a501d77e
L
5233 && dot_p[1] == 'd'
5234 && dot_p[2] == '8')
5235 i.disp_encoding = disp_encoding_8bit;
8d63c93e 5236 else if (mnem_p - 4 == dot_p
f8a5c266
L
5237 && dot_p[1] == 'd'
5238 && dot_p[2] == '3'
5239 && dot_p[3] == '2')
a501d77e 5240 i.disp_encoding = disp_encoding_32bit;
30a55f88
L
5241 else
5242 goto check_suffix;
5243 mnem_p = dot_p;
5244 *dot_p = '\0';
629310ab 5245 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
b6169b20
L
5246 }
5247
29b0f896
AM
5248 if (!current_templates)
5249 {
dc1e8a47 5250 check_suffix:
1c529385 5251 if (mnem_p > mnemonic)
29b0f896 5252 {
1c529385
LH
5253 /* See if we can get a match by trimming off a suffix. */
5254 switch (mnem_p[-1])
29b0f896 5255 {
1c529385
LH
5256 case WORD_MNEM_SUFFIX:
5257 if (intel_syntax && (intel_float_operand (mnemonic) & 2))
29b0f896
AM
5258 i.suffix = SHORT_MNEM_SUFFIX;
5259 else
1c529385
LH
5260 /* Fall through. */
5261 case BYTE_MNEM_SUFFIX:
5262 case QWORD_MNEM_SUFFIX:
5263 i.suffix = mnem_p[-1];
29b0f896 5264 mnem_p[-1] = '\0';
fe0e921f
AM
5265 current_templates
5266 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5267 break;
5268 case SHORT_MNEM_SUFFIX:
5269 case LONG_MNEM_SUFFIX:
5270 if (!intel_syntax)
5271 {
5272 i.suffix = mnem_p[-1];
5273 mnem_p[-1] = '\0';
fe0e921f
AM
5274 current_templates
5275 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5276 }
5277 break;
5278
5279 /* Intel Syntax. */
5280 case 'd':
5281 if (intel_syntax)
5282 {
5283 if (intel_float_operand (mnemonic) == 1)
5284 i.suffix = SHORT_MNEM_SUFFIX;
5285 else
5286 i.suffix = LONG_MNEM_SUFFIX;
5287 mnem_p[-1] = '\0';
fe0e921f
AM
5288 current_templates
5289 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5290 }
5291 break;
29b0f896 5292 }
29b0f896 5293 }
1c529385 5294
29b0f896
AM
5295 if (!current_templates)
5296 {
5297 as_bad (_("no such instruction: `%s'"), token_start);
5298 return NULL;
5299 }
5300 }
252b5132 5301
0cfa3eb3
JB
5302 if (current_templates->start->opcode_modifier.jump == JUMP
5303 || current_templates->start->opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
5304 {
5305 /* Check for a branch hint. We allow ",pt" and ",pn" for
5306 predict taken and predict not taken respectively.
5307 I'm not sure that branch hints actually do anything on loop
5308 and jcxz insns (JumpByte) for current Pentium4 chips. They
5309 may work in the future and it doesn't hurt to accept them
5310 now. */
5311 if (l[0] == ',' && l[1] == 'p')
5312 {
5313 if (l[2] == 't')
5314 {
5315 if (!add_prefix (DS_PREFIX_OPCODE))
5316 return NULL;
5317 l += 3;
5318 }
5319 else if (l[2] == 'n')
5320 {
5321 if (!add_prefix (CS_PREFIX_OPCODE))
5322 return NULL;
5323 l += 3;
5324 }
5325 }
5326 }
5327 /* Any other comma loses. */
5328 if (*l == ',')
5329 {
5330 as_bad (_("invalid character %s in mnemonic"),
5331 output_invalid (*l));
5332 return NULL;
5333 }
252b5132 5334
29b0f896 5335 /* Check if instruction is supported on specified architecture. */
5c6af06e
JB
5336 supported = 0;
5337 for (t = current_templates->start; t < current_templates->end; ++t)
5338 {
c0f3af97
L
5339 supported |= cpu_flags_match (t);
5340 if (supported == CPU_FLAGS_PERFECT_MATCH)
548d0ee6
JB
5341 {
5342 if (!cpu_arch_flags.bitfield.cpui386 && (flag_code != CODE_16BIT))
5343 as_warn (_("use .code16 to ensure correct addressing mode"));
3629bb00 5344
548d0ee6
JB
5345 return l;
5346 }
29b0f896 5347 }
3629bb00 5348
548d0ee6
JB
5349 if (!(supported & CPU_FLAGS_64BIT_MATCH))
5350 as_bad (flag_code == CODE_64BIT
5351 ? _("`%s' is not supported in 64-bit mode")
5352 : _("`%s' is only supported in 64-bit mode"),
5353 current_templates->start->name);
5354 else
5355 as_bad (_("`%s' is not supported on `%s%s'"),
5356 current_templates->start->name,
5357 cpu_arch_name ? cpu_arch_name : default_arch,
5358 cpu_sub_arch_name ? cpu_sub_arch_name : "");
252b5132 5359
548d0ee6 5360 return NULL;
29b0f896 5361}
252b5132 5362
29b0f896 5363static char *
e3bb37b5 5364parse_operands (char *l, const char *mnemonic)
29b0f896
AM
5365{
5366 char *token_start;
3138f287 5367
29b0f896
AM
5368 /* 1 if operand is pending after ','. */
5369 unsigned int expecting_operand = 0;
252b5132 5370
29b0f896
AM
5371 /* Non-zero if operand parens not balanced. */
5372 unsigned int paren_not_balanced;
5373
5374 while (*l != END_OF_INSN)
5375 {
5376 /* Skip optional white space before operand. */
5377 if (is_space_char (*l))
5378 ++l;
d02603dc 5379 if (!is_operand_char (*l) && *l != END_OF_INSN && *l != '"')
29b0f896
AM
5380 {
5381 as_bad (_("invalid character %s before operand %d"),
5382 output_invalid (*l),
5383 i.operands + 1);
5384 return NULL;
5385 }
d02603dc 5386 token_start = l; /* After white space. */
29b0f896
AM
5387 paren_not_balanced = 0;
5388 while (paren_not_balanced || *l != ',')
5389 {
5390 if (*l == END_OF_INSN)
5391 {
5392 if (paren_not_balanced)
5393 {
5394 if (!intel_syntax)
5395 as_bad (_("unbalanced parenthesis in operand %d."),
5396 i.operands + 1);
5397 else
5398 as_bad (_("unbalanced brackets in operand %d."),
5399 i.operands + 1);
5400 return NULL;
5401 }
5402 else
5403 break; /* we are done */
5404 }
d02603dc 5405 else if (!is_operand_char (*l) && !is_space_char (*l) && *l != '"')
29b0f896
AM
5406 {
5407 as_bad (_("invalid character %s in operand %d"),
5408 output_invalid (*l),
5409 i.operands + 1);
5410 return NULL;
5411 }
5412 if (!intel_syntax)
5413 {
5414 if (*l == '(')
5415 ++paren_not_balanced;
5416 if (*l == ')')
5417 --paren_not_balanced;
5418 }
5419 else
5420 {
5421 if (*l == '[')
5422 ++paren_not_balanced;
5423 if (*l == ']')
5424 --paren_not_balanced;
5425 }
5426 l++;
5427 }
5428 if (l != token_start)
5429 { /* Yes, we've read in another operand. */
5430 unsigned int operand_ok;
5431 this_operand = i.operands++;
5432 if (i.operands > MAX_OPERANDS)
5433 {
5434 as_bad (_("spurious operands; (%d operands/instruction max)"),
5435 MAX_OPERANDS);
5436 return NULL;
5437 }
9d46ce34 5438 i.types[this_operand].bitfield.unspecified = 1;
29b0f896
AM
5439 /* Now parse operand adding info to 'i' as we go along. */
5440 END_STRING_AND_SAVE (l);
5441
1286ab78
L
5442 if (i.mem_operands > 1)
5443 {
5444 as_bad (_("too many memory references for `%s'"),
5445 mnemonic);
5446 return 0;
5447 }
5448
29b0f896
AM
5449 if (intel_syntax)
5450 operand_ok =
5451 i386_intel_operand (token_start,
5452 intel_float_operand (mnemonic));
5453 else
a7619375 5454 operand_ok = i386_att_operand (token_start);
29b0f896
AM
5455
5456 RESTORE_END_STRING (l);
5457 if (!operand_ok)
5458 return NULL;
5459 }
5460 else
5461 {
5462 if (expecting_operand)
5463 {
5464 expecting_operand_after_comma:
5465 as_bad (_("expecting operand after ','; got nothing"));
5466 return NULL;
5467 }
5468 if (*l == ',')
5469 {
5470 as_bad (_("expecting operand before ','; got nothing"));
5471 return NULL;
5472 }
5473 }
7f3f1ea2 5474
29b0f896
AM
5475 /* Now *l must be either ',' or END_OF_INSN. */
5476 if (*l == ',')
5477 {
5478 if (*++l == END_OF_INSN)
5479 {
5480 /* Just skip it, if it's \n complain. */
5481 goto expecting_operand_after_comma;
5482 }
5483 expecting_operand = 1;
5484 }
5485 }
5486 return l;
5487}
7f3f1ea2 5488
050dfa73 5489static void
783c187b 5490swap_2_operands (unsigned int xchg1, unsigned int xchg2)
050dfa73
MM
5491{
5492 union i386_op temp_op;
40fb9820 5493 i386_operand_type temp_type;
c48dadc9 5494 unsigned int temp_flags;
050dfa73 5495 enum bfd_reloc_code_real temp_reloc;
4eed87de 5496
050dfa73
MM
5497 temp_type = i.types[xchg2];
5498 i.types[xchg2] = i.types[xchg1];
5499 i.types[xchg1] = temp_type;
c48dadc9
JB
5500
5501 temp_flags = i.flags[xchg2];
5502 i.flags[xchg2] = i.flags[xchg1];
5503 i.flags[xchg1] = temp_flags;
5504
050dfa73
MM
5505 temp_op = i.op[xchg2];
5506 i.op[xchg2] = i.op[xchg1];
5507 i.op[xchg1] = temp_op;
c48dadc9 5508
050dfa73
MM
5509 temp_reloc = i.reloc[xchg2];
5510 i.reloc[xchg2] = i.reloc[xchg1];
5511 i.reloc[xchg1] = temp_reloc;
43234a1e 5512
6225c532 5513 if (i.mask.reg)
43234a1e 5514 {
6225c532
JB
5515 if (i.mask.operand == xchg1)
5516 i.mask.operand = xchg2;
5517 else if (i.mask.operand == xchg2)
5518 i.mask.operand = xchg1;
43234a1e 5519 }
5273a3cd 5520 if (i.broadcast.type)
43234a1e 5521 {
5273a3cd
JB
5522 if (i.broadcast.operand == xchg1)
5523 i.broadcast.operand = xchg2;
5524 else if (i.broadcast.operand == xchg2)
5525 i.broadcast.operand = xchg1;
43234a1e 5526 }
ca5312a2 5527 if (i.rounding.type != rc_none)
43234a1e 5528 {
ca5312a2
JB
5529 if (i.rounding.operand == xchg1)
5530 i.rounding.operand = xchg2;
5531 else if (i.rounding.operand == xchg2)
5532 i.rounding.operand = xchg1;
43234a1e 5533 }
050dfa73
MM
5534}
5535
29b0f896 5536static void
e3bb37b5 5537swap_operands (void)
29b0f896 5538{
b7c61d9a 5539 switch (i.operands)
050dfa73 5540 {
c0f3af97 5541 case 5:
b7c61d9a 5542 case 4:
4d456e3d 5543 swap_2_operands (1, i.operands - 2);
1a0670f3 5544 /* Fall through. */
b7c61d9a
L
5545 case 3:
5546 case 2:
4d456e3d 5547 swap_2_operands (0, i.operands - 1);
b7c61d9a
L
5548 break;
5549 default:
5550 abort ();
29b0f896 5551 }
29b0f896
AM
5552
5553 if (i.mem_operands == 2)
5554 {
5e042380 5555 const reg_entry *temp_seg;
29b0f896
AM
5556 temp_seg = i.seg[0];
5557 i.seg[0] = i.seg[1];
5558 i.seg[1] = temp_seg;
5559 }
5560}
252b5132 5561
29b0f896
AM
5562/* Try to ensure constant immediates are represented in the smallest
5563 opcode possible. */
5564static void
e3bb37b5 5565optimize_imm (void)
29b0f896
AM
5566{
5567 char guess_suffix = 0;
5568 int op;
252b5132 5569
29b0f896
AM
5570 if (i.suffix)
5571 guess_suffix = i.suffix;
5572 else if (i.reg_operands)
5573 {
5574 /* Figure out a suffix from the last register operand specified.
75e5731b
JB
5575 We can't do this properly yet, i.e. excluding special register
5576 instances, but the following works for instructions with
5577 immediates. In any case, we can't set i.suffix yet. */
29b0f896 5578 for (op = i.operands; --op >= 0;)
bab6aec1
JB
5579 if (i.types[op].bitfield.class != Reg)
5580 continue;
5581 else if (i.types[op].bitfield.byte)
7ab9ffdd 5582 {
40fb9820
L
5583 guess_suffix = BYTE_MNEM_SUFFIX;
5584 break;
5585 }
bab6aec1 5586 else if (i.types[op].bitfield.word)
252b5132 5587 {
40fb9820
L
5588 guess_suffix = WORD_MNEM_SUFFIX;
5589 break;
5590 }
bab6aec1 5591 else if (i.types[op].bitfield.dword)
40fb9820
L
5592 {
5593 guess_suffix = LONG_MNEM_SUFFIX;
5594 break;
5595 }
bab6aec1 5596 else if (i.types[op].bitfield.qword)
40fb9820
L
5597 {
5598 guess_suffix = QWORD_MNEM_SUFFIX;
29b0f896 5599 break;
252b5132 5600 }
29b0f896
AM
5601 }
5602 else if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
5603 guess_suffix = WORD_MNEM_SUFFIX;
5604
5605 for (op = i.operands; --op >= 0;)
40fb9820 5606 if (operand_type_check (i.types[op], imm))
29b0f896
AM
5607 {
5608 switch (i.op[op].imms->X_op)
252b5132 5609 {
29b0f896
AM
5610 case O_constant:
5611 /* If a suffix is given, this operand may be shortened. */
5612 switch (guess_suffix)
252b5132 5613 {
29b0f896 5614 case LONG_MNEM_SUFFIX:
40fb9820
L
5615 i.types[op].bitfield.imm32 = 1;
5616 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
5617 break;
5618 case WORD_MNEM_SUFFIX:
40fb9820
L
5619 i.types[op].bitfield.imm16 = 1;
5620 i.types[op].bitfield.imm32 = 1;
5621 i.types[op].bitfield.imm32s = 1;
5622 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
5623 break;
5624 case BYTE_MNEM_SUFFIX:
40fb9820
L
5625 i.types[op].bitfield.imm8 = 1;
5626 i.types[op].bitfield.imm8s = 1;
5627 i.types[op].bitfield.imm16 = 1;
5628 i.types[op].bitfield.imm32 = 1;
5629 i.types[op].bitfield.imm32s = 1;
5630 i.types[op].bitfield.imm64 = 1;
29b0f896 5631 break;
252b5132 5632 }
252b5132 5633
29b0f896
AM
5634 /* If this operand is at most 16 bits, convert it
5635 to a signed 16 bit number before trying to see
5636 whether it will fit in an even smaller size.
5637 This allows a 16-bit operand such as $0xffe0 to
5638 be recognised as within Imm8S range. */
40fb9820 5639 if ((i.types[op].bitfield.imm16)
29b0f896 5640 && (i.op[op].imms->X_add_number & ~(offsetT) 0xffff) == 0)
252b5132 5641 {
29b0f896
AM
5642 i.op[op].imms->X_add_number =
5643 (((i.op[op].imms->X_add_number & 0xffff) ^ 0x8000) - 0x8000);
5644 }
a28def75
L
5645#ifdef BFD64
5646 /* Store 32-bit immediate in 64-bit for 64-bit BFD. */
40fb9820 5647 if ((i.types[op].bitfield.imm32)
29b0f896
AM
5648 && ((i.op[op].imms->X_add_number & ~(((offsetT) 2 << 31) - 1))
5649 == 0))
5650 {
5651 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
5652 ^ ((offsetT) 1 << 31))
5653 - ((offsetT) 1 << 31));
5654 }
a28def75 5655#endif
40fb9820 5656 i.types[op]
c6fb90c8
L
5657 = operand_type_or (i.types[op],
5658 smallest_imm_type (i.op[op].imms->X_add_number));
252b5132 5659
29b0f896
AM
5660 /* We must avoid matching of Imm32 templates when 64bit
5661 only immediate is available. */
5662 if (guess_suffix == QWORD_MNEM_SUFFIX)
40fb9820 5663 i.types[op].bitfield.imm32 = 0;
29b0f896 5664 break;
252b5132 5665
29b0f896
AM
5666 case O_absent:
5667 case O_register:
5668 abort ();
5669
5670 /* Symbols and expressions. */
5671 default:
9cd96992
JB
5672 /* Convert symbolic operand to proper sizes for matching, but don't
5673 prevent matching a set of insns that only supports sizes other
5674 than those matching the insn suffix. */
5675 {
40fb9820 5676 i386_operand_type mask, allowed;
d3ce72d0 5677 const insn_template *t;
9cd96992 5678
0dfbf9d7
L
5679 operand_type_set (&mask, 0);
5680 operand_type_set (&allowed, 0);
40fb9820 5681
4eed87de
AM
5682 for (t = current_templates->start;
5683 t < current_templates->end;
5684 ++t)
bab6aec1
JB
5685 {
5686 allowed = operand_type_or (allowed, t->operand_types[op]);
5687 allowed = operand_type_and (allowed, anyimm);
5688 }
9cd96992
JB
5689 switch (guess_suffix)
5690 {
5691 case QWORD_MNEM_SUFFIX:
40fb9820
L
5692 mask.bitfield.imm64 = 1;
5693 mask.bitfield.imm32s = 1;
9cd96992
JB
5694 break;
5695 case LONG_MNEM_SUFFIX:
40fb9820 5696 mask.bitfield.imm32 = 1;
9cd96992
JB
5697 break;
5698 case WORD_MNEM_SUFFIX:
40fb9820 5699 mask.bitfield.imm16 = 1;
9cd96992
JB
5700 break;
5701 case BYTE_MNEM_SUFFIX:
40fb9820 5702 mask.bitfield.imm8 = 1;
9cd96992
JB
5703 break;
5704 default:
9cd96992
JB
5705 break;
5706 }
c6fb90c8 5707 allowed = operand_type_and (mask, allowed);
0dfbf9d7 5708 if (!operand_type_all_zero (&allowed))
c6fb90c8 5709 i.types[op] = operand_type_and (i.types[op], mask);
9cd96992 5710 }
29b0f896 5711 break;
252b5132 5712 }
29b0f896
AM
5713 }
5714}
47926f60 5715
29b0f896
AM
5716/* Try to use the smallest displacement type too. */
5717static void
e3bb37b5 5718optimize_disp (void)
29b0f896
AM
5719{
5720 int op;
3e73aa7c 5721
29b0f896 5722 for (op = i.operands; --op >= 0;)
40fb9820 5723 if (operand_type_check (i.types[op], disp))
252b5132 5724 {
b300c311 5725 if (i.op[op].disps->X_op == O_constant)
252b5132 5726 {
91d6fa6a 5727 offsetT op_disp = i.op[op].disps->X_add_number;
29b0f896 5728
40fb9820 5729 if (i.types[op].bitfield.disp16
91d6fa6a 5730 && (op_disp & ~(offsetT) 0xffff) == 0)
b300c311
L
5731 {
5732 /* If this operand is at most 16 bits, convert
5733 to a signed 16 bit number and don't use 64bit
5734 displacement. */
91d6fa6a 5735 op_disp = (((op_disp & 0xffff) ^ 0x8000) - 0x8000);
40fb9820 5736 i.types[op].bitfield.disp64 = 0;
b300c311 5737 }
91d6fa6a 5738 if (!op_disp && i.types[op].bitfield.baseindex)
b300c311 5739 {
40fb9820
L
5740 i.types[op].bitfield.disp8 = 0;
5741 i.types[op].bitfield.disp16 = 0;
5742 i.types[op].bitfield.disp32 = 0;
5743 i.types[op].bitfield.disp32s = 0;
5744 i.types[op].bitfield.disp64 = 0;
b300c311
L
5745 i.op[op].disps = 0;
5746 i.disp_operands--;
5747 }
28a167a4 5748#ifdef BFD64
b300c311
L
5749 else if (flag_code == CODE_64BIT)
5750 {
28a167a4
JB
5751 if (i.prefix[ADDR_PREFIX]
5752 && fits_in_unsigned_long (op_disp))
5753 i.types[op].bitfield.disp32 = 1;
5754
5755 /* Optimize 64-bit displacement to 32-bit for 64-bit BFD. */
5756 if (i.types[op].bitfield.disp32
5757 && (op_disp & ~(((offsetT) 2 << 31) - 1)) == 0)
5758 {
5759 /* If this operand is at most 32 bits, convert
5760 to a signed 32 bit number and don't use 64bit
5761 displacement. */
5762 op_disp &= (((offsetT) 2 << 31) - 1);
5763 op_disp = (op_disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
5764 i.types[op].bitfield.disp64 = 0;
5765 }
5766
91d6fa6a 5767 if (fits_in_signed_long (op_disp))
28a9d8f5 5768 {
40fb9820
L
5769 i.types[op].bitfield.disp64 = 0;
5770 i.types[op].bitfield.disp32s = 1;
28a9d8f5 5771 }
b300c311 5772 }
28a167a4 5773#endif
40fb9820
L
5774 if ((i.types[op].bitfield.disp32
5775 || i.types[op].bitfield.disp32s
5776 || i.types[op].bitfield.disp16)
b5014f7a 5777 && fits_in_disp8 (op_disp))
40fb9820 5778 i.types[op].bitfield.disp8 = 1;
252b5132 5779 }
67a4f2b7
AO
5780 else if (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
5781 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL)
5782 {
5783 fix_new_exp (frag_now, frag_more (0) - frag_now->fr_literal, 0,
5784 i.op[op].disps, 0, i.reloc[op]);
40fb9820
L
5785 i.types[op].bitfield.disp8 = 0;
5786 i.types[op].bitfield.disp16 = 0;
5787 i.types[op].bitfield.disp32 = 0;
5788 i.types[op].bitfield.disp32s = 0;
5789 i.types[op].bitfield.disp64 = 0;
67a4f2b7
AO
5790 }
5791 else
b300c311 5792 /* We only support 64bit displacement on constants. */
40fb9820 5793 i.types[op].bitfield.disp64 = 0;
252b5132 5794 }
29b0f896
AM
5795}
5796
4a1b91ea
L
5797/* Return 1 if there is a match in broadcast bytes between operand
5798 GIVEN and instruction template T. */
5799
5800static INLINE int
5801match_broadcast_size (const insn_template *t, unsigned int given)
5802{
5803 return ((t->opcode_modifier.broadcast == BYTE_BROADCAST
5804 && i.types[given].bitfield.byte)
5805 || (t->opcode_modifier.broadcast == WORD_BROADCAST
5806 && i.types[given].bitfield.word)
5807 || (t->opcode_modifier.broadcast == DWORD_BROADCAST
5808 && i.types[given].bitfield.dword)
5809 || (t->opcode_modifier.broadcast == QWORD_BROADCAST
5810 && i.types[given].bitfield.qword));
5811}
5812
6c30d220
L
5813/* Check if operands are valid for the instruction. */
5814
5815static int
5816check_VecOperands (const insn_template *t)
5817{
43234a1e 5818 unsigned int op;
e2195274 5819 i386_cpu_flags cpu;
e2195274
JB
5820
5821 /* Templates allowing for ZMMword as well as YMMword and/or XMMword for
5822 any one operand are implicity requiring AVX512VL support if the actual
5823 operand size is YMMword or XMMword. Since this function runs after
5824 template matching, there's no need to check for YMMword/XMMword in
5825 the template. */
5826 cpu = cpu_flags_and (t->cpu_flags, avx512);
5827 if (!cpu_flags_all_zero (&cpu)
5828 && !t->cpu_flags.bitfield.cpuavx512vl
5829 && !cpu_arch_flags.bitfield.cpuavx512vl)
5830 {
5831 for (op = 0; op < t->operands; ++op)
5832 {
5833 if (t->operand_types[op].bitfield.zmmword
5834 && (i.types[op].bitfield.ymmword
5835 || i.types[op].bitfield.xmmword))
5836 {
5837 i.error = unsupported;
5838 return 1;
5839 }
5840 }
5841 }
43234a1e 5842
6c30d220 5843 /* Without VSIB byte, we can't have a vector register for index. */
63112cd6 5844 if (!t->opcode_modifier.sib
6c30d220 5845 && i.index_reg
1b54b8d7
JB
5846 && (i.index_reg->reg_type.bitfield.xmmword
5847 || i.index_reg->reg_type.bitfield.ymmword
5848 || i.index_reg->reg_type.bitfield.zmmword))
6c30d220
L
5849 {
5850 i.error = unsupported_vector_index_register;
5851 return 1;
5852 }
5853
ad8ecc81
MZ
5854 /* Check if default mask is allowed. */
5855 if (t->opcode_modifier.nodefmask
6225c532 5856 && (!i.mask.reg || i.mask.reg->reg_num == 0))
ad8ecc81
MZ
5857 {
5858 i.error = no_default_mask;
5859 return 1;
5860 }
5861
7bab8ab5
JB
5862 /* For VSIB byte, we need a vector register for index, and all vector
5863 registers must be distinct. */
260cd341 5864 if (t->opcode_modifier.sib && t->opcode_modifier.sib != SIBMEM)
7bab8ab5
JB
5865 {
5866 if (!i.index_reg
63112cd6 5867 || !((t->opcode_modifier.sib == VECSIB128
1b54b8d7 5868 && i.index_reg->reg_type.bitfield.xmmword)
63112cd6 5869 || (t->opcode_modifier.sib == VECSIB256
1b54b8d7 5870 && i.index_reg->reg_type.bitfield.ymmword)
63112cd6 5871 || (t->opcode_modifier.sib == VECSIB512
1b54b8d7 5872 && i.index_reg->reg_type.bitfield.zmmword)))
7bab8ab5
JB
5873 {
5874 i.error = invalid_vsib_address;
5875 return 1;
5876 }
5877
6225c532
JB
5878 gas_assert (i.reg_operands == 2 || i.mask.reg);
5879 if (i.reg_operands == 2 && !i.mask.reg)
43234a1e 5880 {
3528c362 5881 gas_assert (i.types[0].bitfield.class == RegSIMD);
1b54b8d7
JB
5882 gas_assert (i.types[0].bitfield.xmmword
5883 || i.types[0].bitfield.ymmword);
3528c362 5884 gas_assert (i.types[2].bitfield.class == RegSIMD);
1b54b8d7
JB
5885 gas_assert (i.types[2].bitfield.xmmword
5886 || i.types[2].bitfield.ymmword);
43234a1e
L
5887 if (operand_check == check_none)
5888 return 0;
5889 if (register_number (i.op[0].regs)
5890 != register_number (i.index_reg)
5891 && register_number (i.op[2].regs)
5892 != register_number (i.index_reg)
5893 && register_number (i.op[0].regs)
5894 != register_number (i.op[2].regs))
5895 return 0;
5896 if (operand_check == check_error)
5897 {
5898 i.error = invalid_vector_register_set;
5899 return 1;
5900 }
5901 as_warn (_("mask, index, and destination registers should be distinct"));
5902 }
6225c532 5903 else if (i.reg_operands == 1 && i.mask.reg)
8444f82a 5904 {
3528c362 5905 if (i.types[1].bitfield.class == RegSIMD
1b54b8d7
JB
5906 && (i.types[1].bitfield.xmmword
5907 || i.types[1].bitfield.ymmword
5908 || i.types[1].bitfield.zmmword)
8444f82a
MZ
5909 && (register_number (i.op[1].regs)
5910 == register_number (i.index_reg)))
5911 {
5912 if (operand_check == check_error)
5913 {
5914 i.error = invalid_vector_register_set;
5915 return 1;
5916 }
5917 if (operand_check != check_none)
5918 as_warn (_("index and destination registers should be distinct"));
5919 }
5920 }
43234a1e 5921 }
7bab8ab5 5922
260cd341
LC
5923 /* For AMX instructions with three tmmword operands, all tmmword operand must be
5924 distinct */
5925 if (t->operand_types[0].bitfield.tmmword
5926 && i.reg_operands == 3)
5927 {
5928 if (register_number (i.op[0].regs)
5929 == register_number (i.op[1].regs)
5930 || register_number (i.op[0].regs)
5931 == register_number (i.op[2].regs)
5932 || register_number (i.op[1].regs)
5933 == register_number (i.op[2].regs))
5934 {
5935 i.error = invalid_tmm_register_set;
5936 return 1;
5937 }
5938 }
5939
43234a1e
L
5940 /* Check if broadcast is supported by the instruction and is applied
5941 to the memory operand. */
5273a3cd 5942 if (i.broadcast.type)
43234a1e 5943 {
8e6e0792 5944 i386_operand_type type, overlap;
43234a1e
L
5945
5946 /* Check if specified broadcast is supported in this instruction,
4a1b91ea 5947 and its broadcast bytes match the memory operand. */
5273a3cd 5948 op = i.broadcast.operand;
8e6e0792 5949 if (!t->opcode_modifier.broadcast
c48dadc9 5950 || !(i.flags[op] & Operand_Mem)
c39e5b26 5951 || (!i.types[op].bitfield.unspecified
4a1b91ea 5952 && !match_broadcast_size (t, op)))
43234a1e
L
5953 {
5954 bad_broadcast:
5955 i.error = unsupported_broadcast;
5956 return 1;
5957 }
8e6e0792 5958
5273a3cd
JB
5959 i.broadcast.bytes = ((1 << (t->opcode_modifier.broadcast - 1))
5960 * i.broadcast.type);
8e6e0792 5961 operand_type_set (&type, 0);
5273a3cd 5962 switch (i.broadcast.bytes)
8e6e0792 5963 {
4a1b91ea
L
5964 case 2:
5965 type.bitfield.word = 1;
5966 break;
5967 case 4:
5968 type.bitfield.dword = 1;
5969 break;
8e6e0792
JB
5970 case 8:
5971 type.bitfield.qword = 1;
5972 break;
5973 case 16:
5974 type.bitfield.xmmword = 1;
5975 break;
5976 case 32:
5977 type.bitfield.ymmword = 1;
5978 break;
5979 case 64:
5980 type.bitfield.zmmword = 1;
5981 break;
5982 default:
5983 goto bad_broadcast;
5984 }
5985
5986 overlap = operand_type_and (type, t->operand_types[op]);
bc49bfd8
JB
5987 if (t->operand_types[op].bitfield.class == RegSIMD
5988 && t->operand_types[op].bitfield.byte
5989 + t->operand_types[op].bitfield.word
5990 + t->operand_types[op].bitfield.dword
5991 + t->operand_types[op].bitfield.qword > 1)
5992 {
5993 overlap.bitfield.xmmword = 0;
5994 overlap.bitfield.ymmword = 0;
5995 overlap.bitfield.zmmword = 0;
5996 }
8e6e0792
JB
5997 if (operand_type_all_zero (&overlap))
5998 goto bad_broadcast;
5999
6000 if (t->opcode_modifier.checkregsize)
6001 {
6002 unsigned int j;
6003
e2195274 6004 type.bitfield.baseindex = 1;
8e6e0792
JB
6005 for (j = 0; j < i.operands; ++j)
6006 {
6007 if (j != op
6008 && !operand_type_register_match(i.types[j],
6009 t->operand_types[j],
6010 type,
6011 t->operand_types[op]))
6012 goto bad_broadcast;
6013 }
6014 }
43234a1e
L
6015 }
6016 /* If broadcast is supported in this instruction, we need to check if
6017 operand of one-element size isn't specified without broadcast. */
6018 else if (t->opcode_modifier.broadcast && i.mem_operands)
6019 {
6020 /* Find memory operand. */
6021 for (op = 0; op < i.operands; op++)
8dc0818e 6022 if (i.flags[op] & Operand_Mem)
43234a1e
L
6023 break;
6024 gas_assert (op < i.operands);
6025 /* Check size of the memory operand. */
4a1b91ea 6026 if (match_broadcast_size (t, op))
43234a1e
L
6027 {
6028 i.error = broadcast_needed;
6029 return 1;
6030 }
6031 }
c39e5b26
JB
6032 else
6033 op = MAX_OPERANDS - 1; /* Avoid uninitialized variable warning. */
43234a1e
L
6034
6035 /* Check if requested masking is supported. */
6225c532 6036 if (i.mask.reg)
43234a1e 6037 {
ae2387fe
JB
6038 switch (t->opcode_modifier.masking)
6039 {
6040 case BOTH_MASKING:
6041 break;
6042 case MERGING_MASKING:
6225c532 6043 if (i.mask.zeroing)
ae2387fe
JB
6044 {
6045 case 0:
6046 i.error = unsupported_masking;
6047 return 1;
6048 }
6049 break;
6050 case DYNAMIC_MASKING:
6051 /* Memory destinations allow only merging masking. */
6225c532 6052 if (i.mask.zeroing && i.mem_operands)
ae2387fe
JB
6053 {
6054 /* Find memory operand. */
6055 for (op = 0; op < i.operands; op++)
c48dadc9 6056 if (i.flags[op] & Operand_Mem)
ae2387fe
JB
6057 break;
6058 gas_assert (op < i.operands);
6059 if (op == i.operands - 1)
6060 {
6061 i.error = unsupported_masking;
6062 return 1;
6063 }
6064 }
6065 break;
6066 default:
6067 abort ();
6068 }
43234a1e
L
6069 }
6070
6071 /* Check if masking is applied to dest operand. */
6225c532 6072 if (i.mask.reg && (i.mask.operand != i.operands - 1))
43234a1e
L
6073 {
6074 i.error = mask_not_on_destination;
6075 return 1;
6076 }
6077
43234a1e 6078 /* Check RC/SAE. */
ca5312a2 6079 if (i.rounding.type != rc_none)
43234a1e 6080 {
a80195f1 6081 if (!t->opcode_modifier.sae
ca5312a2 6082 || (i.rounding.type != saeonly && !t->opcode_modifier.staticrounding))
43234a1e
L
6083 {
6084 i.error = unsupported_rc_sae;
6085 return 1;
6086 }
6087 /* If the instruction has several immediate operands and one of
6088 them is rounding, the rounding operand should be the last
6089 immediate operand. */
6090 if (i.imm_operands > 1
ca5312a2 6091 && i.rounding.operand != i.imm_operands - 1)
7bab8ab5 6092 {
43234a1e 6093 i.error = rc_sae_operand_not_last_imm;
7bab8ab5
JB
6094 return 1;
6095 }
6c30d220
L
6096 }
6097
da4977e0
JB
6098 /* Check the special Imm4 cases; must be the first operand. */
6099 if (t->cpu_flags.bitfield.cpuxop && t->operands == 5)
6100 {
6101 if (i.op[0].imms->X_op != O_constant
6102 || !fits_in_imm4 (i.op[0].imms->X_add_number))
6103 {
6104 i.error = bad_imm4;
6105 return 1;
6106 }
6107
6108 /* Turn off Imm<N> so that update_imm won't complain. */
6109 operand_type_set (&i.types[0], 0);
6110 }
6111
43234a1e 6112 /* Check vector Disp8 operand. */
b5014f7a
JB
6113 if (t->opcode_modifier.disp8memshift
6114 && i.disp_encoding != disp_encoding_32bit)
43234a1e 6115 {
5273a3cd 6116 if (i.broadcast.type)
4a1b91ea 6117 i.memshift = t->opcode_modifier.broadcast - 1;
7091c612 6118 else if (t->opcode_modifier.disp8memshift != DISP8_SHIFT_VL)
43234a1e 6119 i.memshift = t->opcode_modifier.disp8memshift;
7091c612
JB
6120 else
6121 {
6122 const i386_operand_type *type = NULL;
6123
6124 i.memshift = 0;
6125 for (op = 0; op < i.operands; op++)
8dc0818e 6126 if (i.flags[op] & Operand_Mem)
7091c612 6127 {
4174bfff
JB
6128 if (t->opcode_modifier.evex == EVEXLIG)
6129 i.memshift = 2 + (i.suffix == QWORD_MNEM_SUFFIX);
6130 else if (t->operand_types[op].bitfield.xmmword
6131 + t->operand_types[op].bitfield.ymmword
6132 + t->operand_types[op].bitfield.zmmword <= 1)
7091c612
JB
6133 type = &t->operand_types[op];
6134 else if (!i.types[op].bitfield.unspecified)
6135 type = &i.types[op];
6136 }
3528c362 6137 else if (i.types[op].bitfield.class == RegSIMD
4174bfff 6138 && t->opcode_modifier.evex != EVEXLIG)
7091c612
JB
6139 {
6140 if (i.types[op].bitfield.zmmword)
6141 i.memshift = 6;
6142 else if (i.types[op].bitfield.ymmword && i.memshift < 5)
6143 i.memshift = 5;
6144 else if (i.types[op].bitfield.xmmword && i.memshift < 4)
6145 i.memshift = 4;
6146 }
6147
6148 if (type)
6149 {
6150 if (type->bitfield.zmmword)
6151 i.memshift = 6;
6152 else if (type->bitfield.ymmword)
6153 i.memshift = 5;
6154 else if (type->bitfield.xmmword)
6155 i.memshift = 4;
6156 }
6157
6158 /* For the check in fits_in_disp8(). */
6159 if (i.memshift == 0)
6160 i.memshift = -1;
6161 }
43234a1e
L
6162
6163 for (op = 0; op < i.operands; op++)
6164 if (operand_type_check (i.types[op], disp)
6165 && i.op[op].disps->X_op == O_constant)
6166 {
b5014f7a 6167 if (fits_in_disp8 (i.op[op].disps->X_add_number))
43234a1e 6168 {
b5014f7a
JB
6169 i.types[op].bitfield.disp8 = 1;
6170 return 0;
43234a1e 6171 }
b5014f7a 6172 i.types[op].bitfield.disp8 = 0;
43234a1e
L
6173 }
6174 }
b5014f7a
JB
6175
6176 i.memshift = 0;
43234a1e 6177
6c30d220
L
6178 return 0;
6179}
6180
da4977e0 6181/* Check if encoding requirements are met by the instruction. */
a683cc34
SP
6182
6183static int
da4977e0 6184VEX_check_encoding (const insn_template *t)
a683cc34 6185{
da4977e0
JB
6186 if (i.vec_encoding == vex_encoding_error)
6187 {
6188 i.error = unsupported;
6189 return 1;
6190 }
6191
86fa6981 6192 if (i.vec_encoding == vex_encoding_evex)
43234a1e 6193 {
86fa6981 6194 /* This instruction must be encoded with EVEX prefix. */
e771e7c9 6195 if (!is_evex_encoding (t))
86fa6981
L
6196 {
6197 i.error = unsupported;
6198 return 1;
6199 }
6200 return 0;
43234a1e
L
6201 }
6202
a683cc34 6203 if (!t->opcode_modifier.vex)
86fa6981
L
6204 {
6205 /* This instruction template doesn't have VEX prefix. */
6206 if (i.vec_encoding != vex_encoding_default)
6207 {
6208 i.error = unsupported;
6209 return 1;
6210 }
6211 return 0;
6212 }
a683cc34 6213
a683cc34
SP
6214 return 0;
6215}
6216
d3ce72d0 6217static const insn_template *
83b16ac6 6218match_template (char mnem_suffix)
29b0f896
AM
6219{
6220 /* Points to template once we've found it. */
d3ce72d0 6221 const insn_template *t;
40fb9820 6222 i386_operand_type overlap0, overlap1, overlap2, overlap3;
c0f3af97 6223 i386_operand_type overlap4;
29b0f896 6224 unsigned int found_reverse_match;
dc2be329 6225 i386_opcode_modifier suffix_check;
40fb9820 6226 i386_operand_type operand_types [MAX_OPERANDS];
539e75ad 6227 int addr_prefix_disp;
45a4bb20 6228 unsigned int j, size_match, check_register;
5614d22c 6229 enum i386_error specific_error = 0;
29b0f896 6230
c0f3af97
L
6231#if MAX_OPERANDS != 5
6232# error "MAX_OPERANDS must be 5."
f48ff2ae
L
6233#endif
6234
29b0f896 6235 found_reverse_match = 0;
539e75ad 6236 addr_prefix_disp = -1;
40fb9820 6237
dc2be329 6238 /* Prepare for mnemonic suffix check. */
40fb9820 6239 memset (&suffix_check, 0, sizeof (suffix_check));
dc2be329
L
6240 switch (mnem_suffix)
6241 {
6242 case BYTE_MNEM_SUFFIX:
6243 suffix_check.no_bsuf = 1;
6244 break;
6245 case WORD_MNEM_SUFFIX:
6246 suffix_check.no_wsuf = 1;
6247 break;
6248 case SHORT_MNEM_SUFFIX:
6249 suffix_check.no_ssuf = 1;
6250 break;
6251 case LONG_MNEM_SUFFIX:
6252 suffix_check.no_lsuf = 1;
6253 break;
6254 case QWORD_MNEM_SUFFIX:
6255 suffix_check.no_qsuf = 1;
6256 break;
6257 default:
6258 /* NB: In Intel syntax, normally we can check for memory operand
6259 size when there is no mnemonic suffix. But jmp and call have
6260 2 different encodings with Dword memory operand size, one with
6261 No_ldSuf and the other without. i.suffix is set to
6262 LONG_DOUBLE_MNEM_SUFFIX to skip the one with No_ldSuf. */
6263 if (i.suffix == LONG_DOUBLE_MNEM_SUFFIX)
6264 suffix_check.no_ldsuf = 1;
83b16ac6
JB
6265 }
6266
01559ecc
L
6267 /* Must have right number of operands. */
6268 i.error = number_of_operands_mismatch;
6269
45aa61fe 6270 for (t = current_templates->start; t < current_templates->end; t++)
29b0f896 6271 {
539e75ad 6272 addr_prefix_disp = -1;
dbbc8b7e 6273 found_reverse_match = 0;
539e75ad 6274
29b0f896
AM
6275 if (i.operands != t->operands)
6276 continue;
6277
50aecf8c 6278 /* Check processor support. */
a65babc9 6279 i.error = unsupported;
45a4bb20 6280 if (cpu_flags_match (t) != CPU_FLAGS_PERFECT_MATCH)
50aecf8c
L
6281 continue;
6282
57392598
CL
6283 /* Check Pseudo Prefix. */
6284 i.error = unsupported;
6285 if (t->opcode_modifier.pseudovexprefix
6286 && !(i.vec_encoding == vex_encoding_vex
6287 || i.vec_encoding == vex_encoding_vex3))
6288 continue;
6289
e1d4d893 6290 /* Check AT&T mnemonic. */
a65babc9 6291 i.error = unsupported_with_intel_mnemonic;
e1d4d893 6292 if (intel_mnemonic && t->opcode_modifier.attmnemonic)
1efbbeb4
L
6293 continue;
6294
4b5aaf5f 6295 /* Check AT&T/Intel syntax. */
a65babc9 6296 i.error = unsupported_syntax;
5c07affc 6297 if ((intel_syntax && t->opcode_modifier.attsyntax)
4b5aaf5f 6298 || (!intel_syntax && t->opcode_modifier.intelsyntax))
1efbbeb4
L
6299 continue;
6300
4b5aaf5f
L
6301 /* Check Intel64/AMD64 ISA. */
6302 switch (isa64)
6303 {
6304 default:
6305 /* Default: Don't accept Intel64. */
6306 if (t->opcode_modifier.isa64 == INTEL64)
6307 continue;
6308 break;
6309 case amd64:
6310 /* -mamd64: Don't accept Intel64 and Intel64 only. */
6311 if (t->opcode_modifier.isa64 >= INTEL64)
6312 continue;
6313 break;
6314 case intel64:
6315 /* -mintel64: Don't accept AMD64. */
5990e377 6316 if (t->opcode_modifier.isa64 == AMD64 && flag_code == CODE_64BIT)
4b5aaf5f
L
6317 continue;
6318 break;
6319 }
6320
dc2be329 6321 /* Check the suffix. */
a65babc9 6322 i.error = invalid_instruction_suffix;
dc2be329
L
6323 if ((t->opcode_modifier.no_bsuf && suffix_check.no_bsuf)
6324 || (t->opcode_modifier.no_wsuf && suffix_check.no_wsuf)
6325 || (t->opcode_modifier.no_lsuf && suffix_check.no_lsuf)
6326 || (t->opcode_modifier.no_ssuf && suffix_check.no_ssuf)
6327 || (t->opcode_modifier.no_qsuf && suffix_check.no_qsuf)
6328 || (t->opcode_modifier.no_ldsuf && suffix_check.no_ldsuf))
83b16ac6 6329 continue;
29b0f896 6330
3ac21baa
JB
6331 size_match = operand_size_match (t);
6332 if (!size_match)
7d5e4556 6333 continue;
539e75ad 6334
6f2f06be
JB
6335 /* This is intentionally not
6336
0cfa3eb3 6337 if (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE))
6f2f06be
JB
6338
6339 as the case of a missing * on the operand is accepted (perhaps with
6340 a warning, issued further down). */
0cfa3eb3 6341 if (i.jumpabsolute && t->opcode_modifier.jump != JUMP_ABSOLUTE)
6f2f06be
JB
6342 {
6343 i.error = operand_type_mismatch;
6344 continue;
6345 }
6346
5c07affc
L
6347 for (j = 0; j < MAX_OPERANDS; j++)
6348 operand_types[j] = t->operand_types[j];
6349
e365e234
JB
6350 /* In general, don't allow
6351 - 64-bit operands outside of 64-bit mode,
6352 - 32-bit operands on pre-386. */
4873e243 6353 j = i.imm_operands + (t->operands > i.imm_operands + 1);
e365e234
JB
6354 if (((i.suffix == QWORD_MNEM_SUFFIX
6355 && flag_code != CODE_64BIT
389d00a5
JB
6356 && !(t->opcode_modifier.opcodespace == SPACE_0F
6357 && t->base_opcode == 0xc7
5e74b495 6358 && t->opcode_modifier.opcodeprefix == PREFIX_NONE
8b65b895 6359 && t->extension_opcode == 1) /* cmpxchg8b */)
e365e234
JB
6360 || (i.suffix == LONG_MNEM_SUFFIX
6361 && !cpu_arch_flags.bitfield.cpui386))
45aa61fe 6362 && (intel_syntax
3cd7f3e3 6363 ? (t->opcode_modifier.mnemonicsize != IGNORESIZE
45aa61fe
AM
6364 && !intel_float_operand (t->name))
6365 : intel_float_operand (t->name) != 2)
4873e243
JB
6366 && (t->operands == i.imm_operands
6367 || (operand_types[i.imm_operands].bitfield.class != RegMMX
6368 && operand_types[i.imm_operands].bitfield.class != RegSIMD
6369 && operand_types[i.imm_operands].bitfield.class != RegMask)
6370 || (operand_types[j].bitfield.class != RegMMX
6371 && operand_types[j].bitfield.class != RegSIMD
6372 && operand_types[j].bitfield.class != RegMask))
63112cd6 6373 && !t->opcode_modifier.sib)
192dc9c6
JB
6374 continue;
6375
29b0f896 6376 /* Do not verify operands when there are none. */
e365e234 6377 if (!t->operands)
da4977e0
JB
6378 {
6379 if (VEX_check_encoding (t))
6380 {
6381 specific_error = i.error;
6382 continue;
6383 }
6384
6385 /* We've found a match; break out of loop. */
6386 break;
6387 }
252b5132 6388
48bcea9f
JB
6389 if (!t->opcode_modifier.jump
6390 || t->opcode_modifier.jump == JUMP_ABSOLUTE)
6391 {
6392 /* There should be only one Disp operand. */
6393 for (j = 0; j < MAX_OPERANDS; j++)
6394 if (operand_type_check (operand_types[j], disp))
539e75ad 6395 break;
48bcea9f
JB
6396 if (j < MAX_OPERANDS)
6397 {
5b7c81bd 6398 bool override = (i.prefix[ADDR_PREFIX] != 0);
48bcea9f
JB
6399
6400 addr_prefix_disp = j;
6401
6402 /* Address size prefix will turn Disp64/Disp32S/Disp32/Disp16
6403 operand into Disp32/Disp32/Disp16/Disp32 operand. */
6404 switch (flag_code)
40fb9820 6405 {
48bcea9f
JB
6406 case CODE_16BIT:
6407 override = !override;
6408 /* Fall through. */
6409 case CODE_32BIT:
6410 if (operand_types[j].bitfield.disp32
6411 && operand_types[j].bitfield.disp16)
40fb9820 6412 {
48bcea9f
JB
6413 operand_types[j].bitfield.disp16 = override;
6414 operand_types[j].bitfield.disp32 = !override;
40fb9820 6415 }
48bcea9f
JB
6416 operand_types[j].bitfield.disp32s = 0;
6417 operand_types[j].bitfield.disp64 = 0;
6418 break;
6419
6420 case CODE_64BIT:
6421 if (operand_types[j].bitfield.disp32s
6422 || operand_types[j].bitfield.disp64)
40fb9820 6423 {
48bcea9f
JB
6424 operand_types[j].bitfield.disp64 &= !override;
6425 operand_types[j].bitfield.disp32s &= !override;
6426 operand_types[j].bitfield.disp32 = override;
40fb9820 6427 }
48bcea9f
JB
6428 operand_types[j].bitfield.disp16 = 0;
6429 break;
40fb9820 6430 }
539e75ad 6431 }
48bcea9f 6432 }
539e75ad 6433
02a86693 6434 /* Force 0x8b encoding for "mov foo@GOT, %eax". */
35648716
JB
6435 if (i.reloc[0] == BFD_RELOC_386_GOT32
6436 && t->base_opcode == 0xa0
6437 && t->opcode_modifier.opcodespace == SPACE_BASE)
02a86693
L
6438 continue;
6439
56ffb741 6440 /* We check register size if needed. */
e2195274
JB
6441 if (t->opcode_modifier.checkregsize)
6442 {
6443 check_register = (1 << t->operands) - 1;
5273a3cd
JB
6444 if (i.broadcast.type)
6445 check_register &= ~(1 << i.broadcast.operand);
e2195274
JB
6446 }
6447 else
6448 check_register = 0;
6449
c6fb90c8 6450 overlap0 = operand_type_and (i.types[0], operand_types[0]);
29b0f896
AM
6451 switch (t->operands)
6452 {
6453 case 1:
40fb9820 6454 if (!operand_type_match (overlap0, i.types[0]))
29b0f896
AM
6455 continue;
6456 break;
6457 case 2:
33eaf5de 6458 /* xchg %eax, %eax is a special case. It is an alias for nop
8b38ad71
L
6459 only in 32bit mode and we can use opcode 0x90. In 64bit
6460 mode, we can't use 0x90 for xchg %eax, %eax since it should
6461 zero-extend %eax to %rax. */
6462 if (flag_code == CODE_64BIT
6463 && t->base_opcode == 0x90
35648716 6464 && t->opcode_modifier.opcodespace == SPACE_BASE
75e5731b
JB
6465 && i.types[0].bitfield.instance == Accum
6466 && i.types[0].bitfield.dword
6467 && i.types[1].bitfield.instance == Accum
6468 && i.types[1].bitfield.dword)
8b38ad71 6469 continue;
1212781b
JB
6470 /* xrelease mov %eax, <disp> is another special case. It must not
6471 match the accumulator-only encoding of mov. */
6472 if (flag_code != CODE_64BIT
6473 && i.hle_prefix
6474 && t->base_opcode == 0xa0
35648716 6475 && t->opcode_modifier.opcodespace == SPACE_BASE
75e5731b 6476 && i.types[0].bitfield.instance == Accum
8dc0818e 6477 && (i.flags[1] & Operand_Mem))
1212781b 6478 continue;
f5eb1d70
JB
6479 /* Fall through. */
6480
6481 case 3:
3ac21baa
JB
6482 if (!(size_match & MATCH_STRAIGHT))
6483 goto check_reverse;
64c49ab3
JB
6484 /* Reverse direction of operands if swapping is possible in the first
6485 place (operands need to be symmetric) and
6486 - the load form is requested, and the template is a store form,
6487 - the store form is requested, and the template is a load form,
6488 - the non-default (swapped) form is requested. */
6489 overlap1 = operand_type_and (operand_types[0], operand_types[1]);
f5eb1d70 6490 if (t->opcode_modifier.d && i.reg_operands == i.operands
64c49ab3
JB
6491 && !operand_type_all_zero (&overlap1))
6492 switch (i.dir_encoding)
6493 {
6494 case dir_encoding_load:
6495 if (operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 6496 || t->opcode_modifier.regmem)
64c49ab3
JB
6497 goto check_reverse;
6498 break;
6499
6500 case dir_encoding_store:
6501 if (!operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 6502 && !t->opcode_modifier.regmem)
64c49ab3
JB
6503 goto check_reverse;
6504 break;
6505
6506 case dir_encoding_swap:
6507 goto check_reverse;
6508
6509 case dir_encoding_default:
6510 break;
6511 }
86fa6981 6512 /* If we want store form, we skip the current load. */
64c49ab3
JB
6513 if ((i.dir_encoding == dir_encoding_store
6514 || i.dir_encoding == dir_encoding_swap)
86fa6981
L
6515 && i.mem_operands == 0
6516 && t->opcode_modifier.load)
fa99fab2 6517 continue;
1a0670f3 6518 /* Fall through. */
f48ff2ae 6519 case 4:
c0f3af97 6520 case 5:
c6fb90c8 6521 overlap1 = operand_type_and (i.types[1], operand_types[1]);
40fb9820
L
6522 if (!operand_type_match (overlap0, i.types[0])
6523 || !operand_type_match (overlap1, i.types[1])
e2195274 6524 || ((check_register & 3) == 3
dc821c5f 6525 && !operand_type_register_match (i.types[0],
40fb9820 6526 operand_types[0],
dc821c5f 6527 i.types[1],
40fb9820 6528 operand_types[1])))
29b0f896
AM
6529 {
6530 /* Check if other direction is valid ... */
38e314eb 6531 if (!t->opcode_modifier.d)
29b0f896
AM
6532 continue;
6533
dc1e8a47 6534 check_reverse:
3ac21baa
JB
6535 if (!(size_match & MATCH_REVERSE))
6536 continue;
29b0f896 6537 /* Try reversing direction of operands. */
f5eb1d70
JB
6538 overlap0 = operand_type_and (i.types[0], operand_types[i.operands - 1]);
6539 overlap1 = operand_type_and (i.types[i.operands - 1], operand_types[0]);
40fb9820 6540 if (!operand_type_match (overlap0, i.types[0])
f5eb1d70 6541 || !operand_type_match (overlap1, i.types[i.operands - 1])
45664ddb 6542 || (check_register
dc821c5f 6543 && !operand_type_register_match (i.types[0],
f5eb1d70
JB
6544 operand_types[i.operands - 1],
6545 i.types[i.operands - 1],
45664ddb 6546 operand_types[0])))
29b0f896
AM
6547 {
6548 /* Does not match either direction. */
6549 continue;
6550 }
38e314eb 6551 /* found_reverse_match holds which of D or FloatR
29b0f896 6552 we've found. */
38e314eb
JB
6553 if (!t->opcode_modifier.d)
6554 found_reverse_match = 0;
6555 else if (operand_types[0].bitfield.tbyte)
8a2ed489 6556 found_reverse_match = Opcode_FloatD;
dbbc8b7e 6557 else if (operand_types[0].bitfield.xmmword
f5eb1d70 6558 || operand_types[i.operands - 1].bitfield.xmmword
3528c362
JB
6559 || operand_types[0].bitfield.class == RegMMX
6560 || operand_types[i.operands - 1].bitfield.class == RegMMX
dbbc8b7e
JB
6561 || is_any_vex_encoding(t))
6562 found_reverse_match = (t->base_opcode & 0xee) != 0x6e
6563 ? Opcode_SIMD_FloatD : Opcode_SIMD_IntD;
8a2ed489 6564 else
38e314eb 6565 found_reverse_match = Opcode_D;
40fb9820 6566 if (t->opcode_modifier.floatr)
8a2ed489 6567 found_reverse_match |= Opcode_FloatR;
29b0f896 6568 }
f48ff2ae 6569 else
29b0f896 6570 {
f48ff2ae 6571 /* Found a forward 2 operand match here. */
d1cbb4db
L
6572 switch (t->operands)
6573 {
c0f3af97
L
6574 case 5:
6575 overlap4 = operand_type_and (i.types[4],
6576 operand_types[4]);
1a0670f3 6577 /* Fall through. */
d1cbb4db 6578 case 4:
c6fb90c8
L
6579 overlap3 = operand_type_and (i.types[3],
6580 operand_types[3]);
1a0670f3 6581 /* Fall through. */
d1cbb4db 6582 case 3:
c6fb90c8
L
6583 overlap2 = operand_type_and (i.types[2],
6584 operand_types[2]);
d1cbb4db
L
6585 break;
6586 }
29b0f896 6587
f48ff2ae
L
6588 switch (t->operands)
6589 {
c0f3af97
L
6590 case 5:
6591 if (!operand_type_match (overlap4, i.types[4])
dc821c5f 6592 || !operand_type_register_match (i.types[3],
c0f3af97 6593 operand_types[3],
c0f3af97
L
6594 i.types[4],
6595 operand_types[4]))
6596 continue;
1a0670f3 6597 /* Fall through. */
f48ff2ae 6598 case 4:
40fb9820 6599 if (!operand_type_match (overlap3, i.types[3])
e2195274
JB
6600 || ((check_register & 0xa) == 0xa
6601 && !operand_type_register_match (i.types[1],
f7768225
JB
6602 operand_types[1],
6603 i.types[3],
e2195274
JB
6604 operand_types[3]))
6605 || ((check_register & 0xc) == 0xc
6606 && !operand_type_register_match (i.types[2],
6607 operand_types[2],
6608 i.types[3],
6609 operand_types[3])))
f48ff2ae 6610 continue;
1a0670f3 6611 /* Fall through. */
f48ff2ae
L
6612 case 3:
6613 /* Here we make use of the fact that there are no
23e42951 6614 reverse match 3 operand instructions. */
40fb9820 6615 if (!operand_type_match (overlap2, i.types[2])
e2195274
JB
6616 || ((check_register & 5) == 5
6617 && !operand_type_register_match (i.types[0],
23e42951
JB
6618 operand_types[0],
6619 i.types[2],
e2195274
JB
6620 operand_types[2]))
6621 || ((check_register & 6) == 6
6622 && !operand_type_register_match (i.types[1],
6623 operand_types[1],
6624 i.types[2],
6625 operand_types[2])))
f48ff2ae
L
6626 continue;
6627 break;
6628 }
29b0f896 6629 }
f48ff2ae 6630 /* Found either forward/reverse 2, 3 or 4 operand match here:
29b0f896
AM
6631 slip through to break. */
6632 }
c0f3af97 6633
da4977e0
JB
6634 /* Check if vector operands are valid. */
6635 if (check_VecOperands (t))
6636 {
6637 specific_error = i.error;
6638 continue;
6639 }
6640
6641 /* Check if VEX/EVEX encoding requirements can be satisfied. */
6642 if (VEX_check_encoding (t))
5614d22c
JB
6643 {
6644 specific_error = i.error;
6645 continue;
6646 }
a683cc34 6647
29b0f896
AM
6648 /* We've found a match; break out of loop. */
6649 break;
6650 }
6651
6652 if (t == current_templates->end)
6653 {
6654 /* We found no match. */
a65babc9 6655 const char *err_msg;
5614d22c 6656 switch (specific_error ? specific_error : i.error)
a65babc9
L
6657 {
6658 default:
6659 abort ();
86e026a4 6660 case operand_size_mismatch:
a65babc9
L
6661 err_msg = _("operand size mismatch");
6662 break;
6663 case operand_type_mismatch:
6664 err_msg = _("operand type mismatch");
6665 break;
6666 case register_type_mismatch:
6667 err_msg = _("register type mismatch");
6668 break;
6669 case number_of_operands_mismatch:
6670 err_msg = _("number of operands mismatch");
6671 break;
6672 case invalid_instruction_suffix:
6673 err_msg = _("invalid instruction suffix");
6674 break;
6675 case bad_imm4:
4a2608e3 6676 err_msg = _("constant doesn't fit in 4 bits");
a65babc9 6677 break;
a65babc9
L
6678 case unsupported_with_intel_mnemonic:
6679 err_msg = _("unsupported with Intel mnemonic");
6680 break;
6681 case unsupported_syntax:
6682 err_msg = _("unsupported syntax");
6683 break;
6684 case unsupported:
35262a23 6685 as_bad (_("unsupported instruction `%s'"),
10efe3f6
L
6686 current_templates->start->name);
6687 return NULL;
260cd341
LC
6688 case invalid_sib_address:
6689 err_msg = _("invalid SIB address");
6690 break;
6c30d220
L
6691 case invalid_vsib_address:
6692 err_msg = _("invalid VSIB address");
6693 break;
7bab8ab5
JB
6694 case invalid_vector_register_set:
6695 err_msg = _("mask, index, and destination registers must be distinct");
6696 break;
260cd341
LC
6697 case invalid_tmm_register_set:
6698 err_msg = _("all tmm registers must be distinct");
6699 break;
6c30d220
L
6700 case unsupported_vector_index_register:
6701 err_msg = _("unsupported vector index register");
6702 break;
43234a1e
L
6703 case unsupported_broadcast:
6704 err_msg = _("unsupported broadcast");
6705 break;
43234a1e
L
6706 case broadcast_needed:
6707 err_msg = _("broadcast is needed for operand of such type");
6708 break;
6709 case unsupported_masking:
6710 err_msg = _("unsupported masking");
6711 break;
6712 case mask_not_on_destination:
6713 err_msg = _("mask not on destination operand");
6714 break;
6715 case no_default_mask:
6716 err_msg = _("default mask isn't allowed");
6717 break;
6718 case unsupported_rc_sae:
6719 err_msg = _("unsupported static rounding/sae");
6720 break;
6721 case rc_sae_operand_not_last_imm:
6722 if (intel_syntax)
6723 err_msg = _("RC/SAE operand must precede immediate operands");
6724 else
6725 err_msg = _("RC/SAE operand must follow immediate operands");
6726 break;
6727 case invalid_register_operand:
6728 err_msg = _("invalid register operand");
6729 break;
a65babc9
L
6730 }
6731 as_bad (_("%s for `%s'"), err_msg,
891edac4 6732 current_templates->start->name);
fa99fab2 6733 return NULL;
29b0f896 6734 }
252b5132 6735
29b0f896
AM
6736 if (!quiet_warnings)
6737 {
6738 if (!intel_syntax
0cfa3eb3 6739 && (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE)))
6f2f06be 6740 as_warn (_("indirect %s without `*'"), t->name);
29b0f896 6741
40fb9820 6742 if (t->opcode_modifier.isprefix
3cd7f3e3 6743 && t->opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
6744 {
6745 /* Warn them that a data or address size prefix doesn't
6746 affect assembly of the next line of code. */
6747 as_warn (_("stand-alone `%s' prefix"), t->name);
6748 }
6749 }
6750
6751 /* Copy the template we found. */
9a182d04 6752 install_template (t);
539e75ad
L
6753
6754 if (addr_prefix_disp != -1)
6755 i.tm.operand_types[addr_prefix_disp]
6756 = operand_types[addr_prefix_disp];
6757
29b0f896
AM
6758 if (found_reverse_match)
6759 {
dfd69174
JB
6760 /* If we found a reverse match we must alter the opcode direction
6761 bit and clear/flip the regmem modifier one. found_reverse_match
6762 holds bits to change (different for int & float insns). */
29b0f896
AM
6763
6764 i.tm.base_opcode ^= found_reverse_match;
6765
f5eb1d70
JB
6766 i.tm.operand_types[0] = operand_types[i.operands - 1];
6767 i.tm.operand_types[i.operands - 1] = operand_types[0];
dfd69174
JB
6768
6769 /* Certain SIMD insns have their load forms specified in the opcode
6770 table, and hence we need to _set_ RegMem instead of clearing it.
6771 We need to avoid setting the bit though on insns like KMOVW. */
6772 i.tm.opcode_modifier.regmem
6773 = i.tm.opcode_modifier.modrm && i.tm.opcode_modifier.d
6774 && i.tm.operands > 2U - i.tm.opcode_modifier.sse2avx
6775 && !i.tm.opcode_modifier.regmem;
29b0f896
AM
6776 }
6777
fa99fab2 6778 return t;
29b0f896
AM
6779}
6780
6781static int
e3bb37b5 6782check_string (void)
29b0f896 6783{
51c8edf6
JB
6784 unsigned int es_op = i.tm.opcode_modifier.isstring - IS_STRING_ES_OP0;
6785 unsigned int op = i.tm.operand_types[0].bitfield.baseindex ? es_op : 0;
8dc0818e 6786
5e042380 6787 if (i.seg[op] != NULL && i.seg[op] != reg_es)
29b0f896 6788 {
51c8edf6
JB
6789 as_bad (_("`%s' operand %u must use `%ses' segment"),
6790 i.tm.name,
6791 intel_syntax ? i.tm.operands - es_op : es_op + 1,
6792 register_prefix);
6793 return 0;
29b0f896 6794 }
51c8edf6
JB
6795
6796 /* There's only ever one segment override allowed per instruction.
6797 This instruction possibly has a legal segment override on the
6798 second operand, so copy the segment to where non-string
6799 instructions store it, allowing common code. */
6800 i.seg[op] = i.seg[1];
6801
29b0f896
AM
6802 return 1;
6803}
6804
6805static int
543613e9 6806process_suffix (void)
29b0f896 6807{
5b7c81bd 6808 bool is_crc32 = false, is_movx = false;
8b65b895 6809
29b0f896
AM
6810 /* If matched instruction specifies an explicit instruction mnemonic
6811 suffix, use it. */
673fe0f0 6812 if (i.tm.opcode_modifier.size == SIZE16)
40fb9820 6813 i.suffix = WORD_MNEM_SUFFIX;
673fe0f0 6814 else if (i.tm.opcode_modifier.size == SIZE32)
40fb9820 6815 i.suffix = LONG_MNEM_SUFFIX;
673fe0f0 6816 else if (i.tm.opcode_modifier.size == SIZE64)
40fb9820 6817 i.suffix = QWORD_MNEM_SUFFIX;
13e600d0 6818 else if (i.reg_operands
c8f8eebc
JB
6819 && (i.operands > 1 || i.types[0].bitfield.class == Reg)
6820 && !i.tm.opcode_modifier.addrprefixopreg)
29b0f896 6821 {
65fca059 6822 unsigned int numop = i.operands;
389d00a5
JB
6823
6824 /* MOVSX/MOVZX */
6825 is_movx = (i.tm.opcode_modifier.opcodespace == SPACE_0F
6826 && (i.tm.base_opcode | 8) == 0xbe)
6827 || (i.tm.opcode_modifier.opcodespace == SPACE_BASE
6828 && i.tm.base_opcode == 0x63
6829 && i.tm.cpu_flags.bitfield.cpu64);
6830
8b65b895 6831 /* CRC32 */
389d00a5
JB
6832 is_crc32 = (i.tm.base_opcode == 0xf0
6833 && i.tm.opcode_modifier.opcodespace == SPACE_0F38
8b65b895 6834 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2);
65fca059
JB
6835
6836 /* movsx/movzx want only their source operand considered here, for the
6837 ambiguity checking below. The suffix will be replaced afterwards
6838 to represent the destination (register). */
389d00a5 6839 if (is_movx && (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63))
65fca059
JB
6840 --i.operands;
6841
643bb870 6842 /* crc32 needs REX.W set regardless of suffix / source operand size. */
8b65b895 6843 if (is_crc32 && i.tm.operand_types[1].bitfield.qword)
643bb870
JB
6844 i.rex |= REX_W;
6845
29b0f896 6846 /* If there's no instruction mnemonic suffix we try to invent one
13e600d0 6847 based on GPR operands. */
29b0f896
AM
6848 if (!i.suffix)
6849 {
6850 /* We take i.suffix from the last register operand specified,
6851 Destination register type is more significant than source
381d071f
L
6852 register type. crc32 in SSE4.2 prefers source register
6853 type. */
8b65b895 6854 unsigned int op = is_crc32 ? 1 : i.operands;
20592a94 6855
1a035124
JB
6856 while (op--)
6857 if (i.tm.operand_types[op].bitfield.instance == InstanceNone
6858 || i.tm.operand_types[op].bitfield.instance == Accum)
6859 {
6860 if (i.types[op].bitfield.class != Reg)
6861 continue;
6862 if (i.types[op].bitfield.byte)
6863 i.suffix = BYTE_MNEM_SUFFIX;
6864 else if (i.types[op].bitfield.word)
6865 i.suffix = WORD_MNEM_SUFFIX;
6866 else if (i.types[op].bitfield.dword)
6867 i.suffix = LONG_MNEM_SUFFIX;
6868 else if (i.types[op].bitfield.qword)
6869 i.suffix = QWORD_MNEM_SUFFIX;
6870 else
6871 continue;
6872 break;
6873 }
65fca059
JB
6874
6875 /* As an exception, movsx/movzx silently default to a byte source
6876 in AT&T mode. */
389d00a5 6877 if (is_movx && i.tm.opcode_modifier.w && !i.suffix && !intel_syntax)
65fca059 6878 i.suffix = BYTE_MNEM_SUFFIX;
29b0f896
AM
6879 }
6880 else if (i.suffix == BYTE_MNEM_SUFFIX)
6881 {
2eb952a4 6882 if (intel_syntax
3cd7f3e3 6883 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
2eb952a4
L
6884 && i.tm.opcode_modifier.no_bsuf)
6885 i.suffix = 0;
6886 else if (!check_byte_reg ())
29b0f896
AM
6887 return 0;
6888 }
6889 else if (i.suffix == LONG_MNEM_SUFFIX)
6890 {
2eb952a4 6891 if (intel_syntax
3cd7f3e3 6892 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
9f123b91
JB
6893 && i.tm.opcode_modifier.no_lsuf
6894 && !i.tm.opcode_modifier.todword
6895 && !i.tm.opcode_modifier.toqword)
2eb952a4
L
6896 i.suffix = 0;
6897 else if (!check_long_reg ())
29b0f896
AM
6898 return 0;
6899 }
6900 else if (i.suffix == QWORD_MNEM_SUFFIX)
6901 {
955e1e6a 6902 if (intel_syntax
3cd7f3e3 6903 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
9f123b91
JB
6904 && i.tm.opcode_modifier.no_qsuf
6905 && !i.tm.opcode_modifier.todword
6906 && !i.tm.opcode_modifier.toqword)
955e1e6a
L
6907 i.suffix = 0;
6908 else if (!check_qword_reg ())
29b0f896
AM
6909 return 0;
6910 }
6911 else if (i.suffix == WORD_MNEM_SUFFIX)
6912 {
2eb952a4 6913 if (intel_syntax
3cd7f3e3 6914 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
2eb952a4
L
6915 && i.tm.opcode_modifier.no_wsuf)
6916 i.suffix = 0;
6917 else if (!check_word_reg ())
29b0f896
AM
6918 return 0;
6919 }
3cd7f3e3
L
6920 else if (intel_syntax
6921 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
6922 /* Do nothing if the instruction is going to ignore the prefix. */
6923 ;
6924 else
6925 abort ();
65fca059
JB
6926
6927 /* Undo the movsx/movzx change done above. */
6928 i.operands = numop;
29b0f896 6929 }
3cd7f3e3
L
6930 else if (i.tm.opcode_modifier.mnemonicsize == DEFAULTSIZE
6931 && !i.suffix)
29b0f896 6932 {
13e600d0
JB
6933 i.suffix = stackop_size;
6934 if (stackop_size == LONG_MNEM_SUFFIX)
06f74c5c
L
6935 {
6936 /* stackop_size is set to LONG_MNEM_SUFFIX for the
6937 .code16gcc directive to support 16-bit mode with
6938 32-bit address. For IRET without a suffix, generate
6939 16-bit IRET (opcode 0xcf) to return from an interrupt
6940 handler. */
13e600d0
JB
6941 if (i.tm.base_opcode == 0xcf)
6942 {
6943 i.suffix = WORD_MNEM_SUFFIX;
6944 as_warn (_("generating 16-bit `iret' for .code16gcc directive"));
6945 }
6946 /* Warn about changed behavior for segment register push/pop. */
6947 else if ((i.tm.base_opcode | 1) == 0x07)
6948 as_warn (_("generating 32-bit `%s', unlike earlier gas versions"),
6949 i.tm.name);
06f74c5c 6950 }
29b0f896 6951 }
c006a730 6952 else if (!i.suffix
0cfa3eb3
JB
6953 && (i.tm.opcode_modifier.jump == JUMP_ABSOLUTE
6954 || i.tm.opcode_modifier.jump == JUMP_BYTE
6955 || i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT
389d00a5
JB
6956 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
6957 && i.tm.base_opcode == 0x01 /* [ls][gi]dt */
64e74474 6958 && i.tm.extension_opcode <= 3)))
9306ca4a
JB
6959 {
6960 switch (flag_code)
6961 {
6962 case CODE_64BIT:
40fb9820 6963 if (!i.tm.opcode_modifier.no_qsuf)
9306ca4a 6964 {
828c2a25
JB
6965 if (i.tm.opcode_modifier.jump == JUMP_BYTE
6966 || i.tm.opcode_modifier.no_lsuf)
6967 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a
JB
6968 break;
6969 }
1a0670f3 6970 /* Fall through. */
9306ca4a 6971 case CODE_32BIT:
40fb9820 6972 if (!i.tm.opcode_modifier.no_lsuf)
9306ca4a
JB
6973 i.suffix = LONG_MNEM_SUFFIX;
6974 break;
6975 case CODE_16BIT:
40fb9820 6976 if (!i.tm.opcode_modifier.no_wsuf)
9306ca4a
JB
6977 i.suffix = WORD_MNEM_SUFFIX;
6978 break;
6979 }
6980 }
252b5132 6981
c006a730 6982 if (!i.suffix
3cd7f3e3 6983 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8
JB
6984 /* Also cover lret/retf/iret in 64-bit mode. */
6985 || (flag_code == CODE_64BIT
6986 && !i.tm.opcode_modifier.no_lsuf
6987 && !i.tm.opcode_modifier.no_qsuf))
3cd7f3e3 6988 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
8bbb3ad8
JB
6989 /* Explicit sizing prefixes are assumed to disambiguate insns. */
6990 && !i.prefix[DATA_PREFIX] && !(i.prefix[REX_PREFIX] & REX_W)
62b3f548
JB
6991 /* Accept FLDENV et al without suffix. */
6992 && (i.tm.opcode_modifier.no_ssuf || i.tm.opcode_modifier.floatmf))
29b0f896 6993 {
6c0946d0 6994 unsigned int suffixes, evex = 0;
c006a730
JB
6995
6996 suffixes = !i.tm.opcode_modifier.no_bsuf;
6997 if (!i.tm.opcode_modifier.no_wsuf)
6998 suffixes |= 1 << 1;
6999 if (!i.tm.opcode_modifier.no_lsuf)
7000 suffixes |= 1 << 2;
7001 if (!i.tm.opcode_modifier.no_ldsuf)
7002 suffixes |= 1 << 3;
7003 if (!i.tm.opcode_modifier.no_ssuf)
7004 suffixes |= 1 << 4;
7005 if (flag_code == CODE_64BIT && !i.tm.opcode_modifier.no_qsuf)
7006 suffixes |= 1 << 5;
7007
6c0946d0
JB
7008 /* For [XYZ]MMWORD operands inspect operand sizes. While generally
7009 also suitable for AT&T syntax mode, it was requested that this be
7010 restricted to just Intel syntax. */
5273a3cd 7011 if (intel_syntax && is_any_vex_encoding (&i.tm) && !i.broadcast.type)
6c0946d0 7012 {
b9915cbc 7013 unsigned int op;
6c0946d0 7014
b9915cbc 7015 for (op = 0; op < i.tm.operands; ++op)
6c0946d0 7016 {
b9915cbc
JB
7017 if (is_evex_encoding (&i.tm)
7018 && !cpu_arch_flags.bitfield.cpuavx512vl)
6c0946d0 7019 {
b9915cbc
JB
7020 if (i.tm.operand_types[op].bitfield.ymmword)
7021 i.tm.operand_types[op].bitfield.xmmword = 0;
7022 if (i.tm.operand_types[op].bitfield.zmmword)
7023 i.tm.operand_types[op].bitfield.ymmword = 0;
7024 if (!i.tm.opcode_modifier.evex
7025 || i.tm.opcode_modifier.evex == EVEXDYN)
7026 i.tm.opcode_modifier.evex = EVEX512;
7027 }
6c0946d0 7028
b9915cbc
JB
7029 if (i.tm.operand_types[op].bitfield.xmmword
7030 + i.tm.operand_types[op].bitfield.ymmword
7031 + i.tm.operand_types[op].bitfield.zmmword < 2)
7032 continue;
6c0946d0 7033
b9915cbc
JB
7034 /* Any properly sized operand disambiguates the insn. */
7035 if (i.types[op].bitfield.xmmword
7036 || i.types[op].bitfield.ymmword
7037 || i.types[op].bitfield.zmmword)
7038 {
7039 suffixes &= ~(7 << 6);
7040 evex = 0;
7041 break;
7042 }
6c0946d0 7043
b9915cbc
JB
7044 if ((i.flags[op] & Operand_Mem)
7045 && i.tm.operand_types[op].bitfield.unspecified)
7046 {
7047 if (i.tm.operand_types[op].bitfield.xmmword)
7048 suffixes |= 1 << 6;
7049 if (i.tm.operand_types[op].bitfield.ymmword)
7050 suffixes |= 1 << 7;
7051 if (i.tm.operand_types[op].bitfield.zmmword)
7052 suffixes |= 1 << 8;
7053 if (is_evex_encoding (&i.tm))
7054 evex = EVEX512;
6c0946d0
JB
7055 }
7056 }
7057 }
7058
7059 /* Are multiple suffixes / operand sizes allowed? */
c006a730 7060 if (suffixes & (suffixes - 1))
9306ca4a 7061 {
873494c8 7062 if (intel_syntax
3cd7f3e3 7063 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8 7064 || operand_check == check_error))
9306ca4a 7065 {
c006a730 7066 as_bad (_("ambiguous operand size for `%s'"), i.tm.name);
9306ca4a
JB
7067 return 0;
7068 }
c006a730 7069 if (operand_check == check_error)
9306ca4a 7070 {
c006a730
JB
7071 as_bad (_("no instruction mnemonic suffix given and "
7072 "no register operands; can't size `%s'"), i.tm.name);
9306ca4a
JB
7073 return 0;
7074 }
c006a730 7075 if (operand_check == check_warning)
873494c8
JB
7076 as_warn (_("%s; using default for `%s'"),
7077 intel_syntax
7078 ? _("ambiguous operand size")
7079 : _("no instruction mnemonic suffix given and "
7080 "no register operands"),
7081 i.tm.name);
c006a730
JB
7082
7083 if (i.tm.opcode_modifier.floatmf)
7084 i.suffix = SHORT_MNEM_SUFFIX;
389d00a5 7085 else if (is_movx)
65fca059 7086 /* handled below */;
6c0946d0
JB
7087 else if (evex)
7088 i.tm.opcode_modifier.evex = evex;
c006a730
JB
7089 else if (flag_code == CODE_16BIT)
7090 i.suffix = WORD_MNEM_SUFFIX;
1a035124 7091 else if (!i.tm.opcode_modifier.no_lsuf)
c006a730 7092 i.suffix = LONG_MNEM_SUFFIX;
1a035124
JB
7093 else
7094 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a 7095 }
29b0f896 7096 }
252b5132 7097
389d00a5 7098 if (is_movx)
65fca059
JB
7099 {
7100 /* In Intel syntax, movsx/movzx must have a "suffix" (checked above).
7101 In AT&T syntax, if there is no suffix (warned about above), the default
7102 will be byte extension. */
7103 if (i.tm.opcode_modifier.w && i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
7104 i.tm.base_opcode |= 1;
7105
7106 /* For further processing, the suffix should represent the destination
7107 (register). This is already the case when one was used with
7108 mov[sz][bw]*, but we need to replace it for mov[sz]x, or if there was
7109 no suffix to begin with. */
7110 if (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63 || !i.suffix)
7111 {
7112 if (i.types[1].bitfield.word)
7113 i.suffix = WORD_MNEM_SUFFIX;
7114 else if (i.types[1].bitfield.qword)
7115 i.suffix = QWORD_MNEM_SUFFIX;
7116 else
7117 i.suffix = LONG_MNEM_SUFFIX;
7118
7119 i.tm.opcode_modifier.w = 0;
7120 }
7121 }
7122
50128d0c
JB
7123 if (!i.tm.opcode_modifier.modrm && i.reg_operands && i.tm.operands < 3)
7124 i.short_form = (i.tm.operand_types[0].bitfield.class == Reg)
7125 != (i.tm.operand_types[1].bitfield.class == Reg);
7126
d2224064
JB
7127 /* Change the opcode based on the operand size given by i.suffix. */
7128 switch (i.suffix)
29b0f896 7129 {
d2224064
JB
7130 /* Size floating point instruction. */
7131 case LONG_MNEM_SUFFIX:
7132 if (i.tm.opcode_modifier.floatmf)
7133 {
7134 i.tm.base_opcode ^= 4;
7135 break;
7136 }
7137 /* fall through */
7138 case WORD_MNEM_SUFFIX:
7139 case QWORD_MNEM_SUFFIX:
29b0f896 7140 /* It's not a byte, select word/dword operation. */
40fb9820 7141 if (i.tm.opcode_modifier.w)
29b0f896 7142 {
50128d0c 7143 if (i.short_form)
29b0f896
AM
7144 i.tm.base_opcode |= 8;
7145 else
7146 i.tm.base_opcode |= 1;
7147 }
d2224064
JB
7148 /* fall through */
7149 case SHORT_MNEM_SUFFIX:
29b0f896
AM
7150 /* Now select between word & dword operations via the operand
7151 size prefix, except for instructions that will ignore this
7152 prefix anyway. */
c8f8eebc 7153 if (i.suffix != QWORD_MNEM_SUFFIX
3cd7f3e3 7154 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
c8f8eebc
JB
7155 && !i.tm.opcode_modifier.floatmf
7156 && !is_any_vex_encoding (&i.tm)
7157 && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
7158 || (flag_code == CODE_64BIT
7159 && i.tm.opcode_modifier.jump == JUMP_BYTE)))
24eab124
AM
7160 {
7161 unsigned int prefix = DATA_PREFIX_OPCODE;
543613e9 7162
0cfa3eb3 7163 if (i.tm.opcode_modifier.jump == JUMP_BYTE) /* jcxz, loop */
29b0f896 7164 prefix = ADDR_PREFIX_OPCODE;
252b5132 7165
29b0f896
AM
7166 if (!add_prefix (prefix))
7167 return 0;
24eab124 7168 }
252b5132 7169
29b0f896
AM
7170 /* Set mode64 for an operand. */
7171 if (i.suffix == QWORD_MNEM_SUFFIX
9146926a 7172 && flag_code == CODE_64BIT
d2224064 7173 && !i.tm.opcode_modifier.norex64
4ed21b58 7174 && !i.tm.opcode_modifier.vexw
46e883c5 7175 /* Special case for xchg %rax,%rax. It is NOP and doesn't
d2224064
JB
7176 need rex64. */
7177 && ! (i.operands == 2
7178 && i.tm.base_opcode == 0x90
7179 && i.tm.extension_opcode == None
75e5731b
JB
7180 && i.types[0].bitfield.instance == Accum
7181 && i.types[0].bitfield.qword
7182 && i.types[1].bitfield.instance == Accum
7183 && i.types[1].bitfield.qword))
d2224064 7184 i.rex |= REX_W;
3e73aa7c 7185
d2224064 7186 break;
8bbb3ad8
JB
7187
7188 case 0:
f9a6a8f0 7189 /* Select word/dword/qword operation with explicit data sizing prefix
8bbb3ad8
JB
7190 when there are no suitable register operands. */
7191 if (i.tm.opcode_modifier.w
7192 && (i.prefix[DATA_PREFIX] || (i.prefix[REX_PREFIX] & REX_W))
7193 && (!i.reg_operands
7194 || (i.reg_operands == 1
7195 /* ShiftCount */
7196 && (i.tm.operand_types[0].bitfield.instance == RegC
7197 /* InOutPortReg */
7198 || i.tm.operand_types[0].bitfield.instance == RegD
7199 || i.tm.operand_types[1].bitfield.instance == RegD
7200 /* CRC32 */
8b65b895 7201 || is_crc32))))
8bbb3ad8
JB
7202 i.tm.base_opcode |= 1;
7203 break;
29b0f896 7204 }
7ecd2f8b 7205
c8f8eebc 7206 if (i.tm.opcode_modifier.addrprefixopreg)
c0a30a9f 7207 {
c8f8eebc
JB
7208 gas_assert (!i.suffix);
7209 gas_assert (i.reg_operands);
c0a30a9f 7210
c8f8eebc
JB
7211 if (i.tm.operand_types[0].bitfield.instance == Accum
7212 || i.operands == 1)
7213 {
7214 /* The address size override prefix changes the size of the
7215 first operand. */
7216 if (flag_code == CODE_64BIT
7217 && i.op[0].regs->reg_type.bitfield.word)
7218 {
7219 as_bad (_("16-bit addressing unavailable for `%s'"),
7220 i.tm.name);
7221 return 0;
7222 }
7223
7224 if ((flag_code == CODE_32BIT
7225 ? i.op[0].regs->reg_type.bitfield.word
7226 : i.op[0].regs->reg_type.bitfield.dword)
7227 && !add_prefix (ADDR_PREFIX_OPCODE))
7228 return 0;
7229 }
c0a30a9f
L
7230 else
7231 {
c8f8eebc
JB
7232 /* Check invalid register operand when the address size override
7233 prefix changes the size of register operands. */
7234 unsigned int op;
7235 enum { need_word, need_dword, need_qword } need;
7236
27f13469 7237 /* Check the register operand for the address size prefix if
b3a3496f 7238 the memory operand has no real registers, like symbol, DISP
829f3fe1 7239 or bogus (x32-only) symbol(%rip) when symbol(%eip) is meant. */
27f13469
L
7240 if (i.mem_operands == 1
7241 && i.reg_operands == 1
7242 && i.operands == 2
27f13469 7243 && i.types[1].bitfield.class == Reg
b3a3496f
L
7244 && (flag_code == CODE_32BIT
7245 ? i.op[1].regs->reg_type.bitfield.word
7246 : i.op[1].regs->reg_type.bitfield.dword)
7247 && ((i.base_reg == NULL && i.index_reg == NULL)
829f3fe1
JB
7248#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
7249 || (x86_elf_abi == X86_64_X32_ABI
7250 && i.base_reg
b3a3496f
L
7251 && i.base_reg->reg_num == RegIP
7252 && i.base_reg->reg_type.bitfield.qword))
829f3fe1
JB
7253#else
7254 || 0)
7255#endif
27f13469
L
7256 && !add_prefix (ADDR_PREFIX_OPCODE))
7257 return 0;
7258
c8f8eebc
JB
7259 if (flag_code == CODE_32BIT)
7260 need = i.prefix[ADDR_PREFIX] ? need_word : need_dword;
7261 else if (i.prefix[ADDR_PREFIX])
c0a30a9f
L
7262 need = need_dword;
7263 else
7264 need = flag_code == CODE_64BIT ? need_qword : need_word;
c0a30a9f 7265
c8f8eebc
JB
7266 for (op = 0; op < i.operands; op++)
7267 {
7268 if (i.types[op].bitfield.class != Reg)
7269 continue;
7270
7271 switch (need)
7272 {
7273 case need_word:
7274 if (i.op[op].regs->reg_type.bitfield.word)
7275 continue;
7276 break;
7277 case need_dword:
7278 if (i.op[op].regs->reg_type.bitfield.dword)
7279 continue;
7280 break;
7281 case need_qword:
7282 if (i.op[op].regs->reg_type.bitfield.qword)
7283 continue;
7284 break;
7285 }
7286
7287 as_bad (_("invalid register operand size for `%s'"),
7288 i.tm.name);
7289 return 0;
7290 }
7291 }
c0a30a9f
L
7292 }
7293
29b0f896
AM
7294 return 1;
7295}
3e73aa7c 7296
29b0f896 7297static int
543613e9 7298check_byte_reg (void)
29b0f896
AM
7299{
7300 int op;
543613e9 7301
29b0f896
AM
7302 for (op = i.operands; --op >= 0;)
7303 {
dc821c5f 7304 /* Skip non-register operands. */
bab6aec1 7305 if (i.types[op].bitfield.class != Reg)
dc821c5f
JB
7306 continue;
7307
29b0f896
AM
7308 /* If this is an eight bit register, it's OK. If it's the 16 or
7309 32 bit version of an eight bit register, we will just use the
7310 low portion, and that's OK too. */
dc821c5f 7311 if (i.types[op].bitfield.byte)
29b0f896
AM
7312 continue;
7313
5a819eb9 7314 /* I/O port address operands are OK too. */
75e5731b
JB
7315 if (i.tm.operand_types[op].bitfield.instance == RegD
7316 && i.tm.operand_types[op].bitfield.word)
5a819eb9
JB
7317 continue;
7318
9706160a 7319 /* crc32 only wants its source operand checked here. */
389d00a5
JB
7320 if (i.tm.base_opcode == 0xf0
7321 && i.tm.opcode_modifier.opcodespace == SPACE_0F38
8b65b895
L
7322 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2
7323 && op != 0)
9344ff29
L
7324 continue;
7325
29b0f896 7326 /* Any other register is bad. */
73c76375
JB
7327 as_bad (_("`%s%s' not allowed with `%s%c'"),
7328 register_prefix, i.op[op].regs->reg_name,
7329 i.tm.name, i.suffix);
7330 return 0;
29b0f896
AM
7331 }
7332 return 1;
7333}
7334
7335static int
e3bb37b5 7336check_long_reg (void)
29b0f896
AM
7337{
7338 int op;
7339
7340 for (op = i.operands; --op >= 0;)
dc821c5f 7341 /* Skip non-register operands. */
bab6aec1 7342 if (i.types[op].bitfield.class != Reg)
dc821c5f 7343 continue;
29b0f896
AM
7344 /* Reject eight bit registers, except where the template requires
7345 them. (eg. movzb) */
dc821c5f 7346 else if (i.types[op].bitfield.byte
bab6aec1 7347 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7348 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7349 && (i.tm.operand_types[op].bitfield.word
7350 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7351 {
a540244d
L
7352 as_bad (_("`%s%s' not allowed with `%s%c'"),
7353 register_prefix,
29b0f896
AM
7354 i.op[op].regs->reg_name,
7355 i.tm.name,
7356 i.suffix);
7357 return 0;
7358 }
be4c5e58
L
7359 /* Error if the e prefix on a general reg is missing. */
7360 else if (i.types[op].bitfield.word
bab6aec1 7361 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7362 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7363 && i.tm.operand_types[op].bitfield.dword)
29b0f896 7364 {
be4c5e58
L
7365 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7366 register_prefix, i.op[op].regs->reg_name,
7367 i.suffix);
7368 return 0;
252b5132 7369 }
e4630f71 7370 /* Warn if the r prefix on a general reg is present. */
dc821c5f 7371 else if (i.types[op].bitfield.qword
bab6aec1 7372 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7373 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7374 && i.tm.operand_types[op].bitfield.dword)
252b5132 7375 {
34828aad 7376 if (intel_syntax
65fca059 7377 && i.tm.opcode_modifier.toqword
3528c362 7378 && i.types[0].bitfield.class != RegSIMD)
34828aad 7379 {
ca61edf2 7380 /* Convert to QWORD. We want REX byte. */
34828aad
L
7381 i.suffix = QWORD_MNEM_SUFFIX;
7382 }
7383 else
7384 {
2b5d6a91 7385 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
34828aad
L
7386 register_prefix, i.op[op].regs->reg_name,
7387 i.suffix);
7388 return 0;
7389 }
29b0f896
AM
7390 }
7391 return 1;
7392}
252b5132 7393
29b0f896 7394static int
e3bb37b5 7395check_qword_reg (void)
29b0f896
AM
7396{
7397 int op;
252b5132 7398
29b0f896 7399 for (op = i.operands; --op >= 0; )
dc821c5f 7400 /* Skip non-register operands. */
bab6aec1 7401 if (i.types[op].bitfield.class != Reg)
dc821c5f 7402 continue;
29b0f896
AM
7403 /* Reject eight bit registers, except where the template requires
7404 them. (eg. movzb) */
dc821c5f 7405 else if (i.types[op].bitfield.byte
bab6aec1 7406 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7407 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7408 && (i.tm.operand_types[op].bitfield.word
7409 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7410 {
a540244d
L
7411 as_bad (_("`%s%s' not allowed with `%s%c'"),
7412 register_prefix,
29b0f896
AM
7413 i.op[op].regs->reg_name,
7414 i.tm.name,
7415 i.suffix);
7416 return 0;
7417 }
e4630f71 7418 /* Warn if the r prefix on a general reg is missing. */
dc821c5f
JB
7419 else if ((i.types[op].bitfield.word
7420 || i.types[op].bitfield.dword)
bab6aec1 7421 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7422 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7423 && i.tm.operand_types[op].bitfield.qword)
29b0f896
AM
7424 {
7425 /* Prohibit these changes in the 64bit mode, since the
7426 lowering is more complicated. */
34828aad 7427 if (intel_syntax
ca61edf2 7428 && i.tm.opcode_modifier.todword
3528c362 7429 && i.types[0].bitfield.class != RegSIMD)
34828aad 7430 {
ca61edf2 7431 /* Convert to DWORD. We don't want REX byte. */
34828aad
L
7432 i.suffix = LONG_MNEM_SUFFIX;
7433 }
7434 else
7435 {
2b5d6a91 7436 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
34828aad
L
7437 register_prefix, i.op[op].regs->reg_name,
7438 i.suffix);
7439 return 0;
7440 }
252b5132 7441 }
29b0f896
AM
7442 return 1;
7443}
252b5132 7444
29b0f896 7445static int
e3bb37b5 7446check_word_reg (void)
29b0f896
AM
7447{
7448 int op;
7449 for (op = i.operands; --op >= 0;)
dc821c5f 7450 /* Skip non-register operands. */
bab6aec1 7451 if (i.types[op].bitfield.class != Reg)
dc821c5f 7452 continue;
29b0f896
AM
7453 /* Reject eight bit registers, except where the template requires
7454 them. (eg. movzb) */
dc821c5f 7455 else if (i.types[op].bitfield.byte
bab6aec1 7456 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7457 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7458 && (i.tm.operand_types[op].bitfield.word
7459 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7460 {
a540244d
L
7461 as_bad (_("`%s%s' not allowed with `%s%c'"),
7462 register_prefix,
29b0f896
AM
7463 i.op[op].regs->reg_name,
7464 i.tm.name,
7465 i.suffix);
7466 return 0;
7467 }
9706160a
JB
7468 /* Error if the e or r prefix on a general reg is present. */
7469 else if ((i.types[op].bitfield.dword
dc821c5f 7470 || i.types[op].bitfield.qword)
bab6aec1 7471 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7472 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7473 && i.tm.operand_types[op].bitfield.word)
252b5132 7474 {
9706160a
JB
7475 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7476 register_prefix, i.op[op].regs->reg_name,
7477 i.suffix);
7478 return 0;
29b0f896
AM
7479 }
7480 return 1;
7481}
252b5132 7482
29b0f896 7483static int
40fb9820 7484update_imm (unsigned int j)
29b0f896 7485{
bc0844ae 7486 i386_operand_type overlap = i.types[j];
40fb9820
L
7487 if ((overlap.bitfield.imm8
7488 || overlap.bitfield.imm8s
7489 || overlap.bitfield.imm16
7490 || overlap.bitfield.imm32
7491 || overlap.bitfield.imm32s
7492 || overlap.bitfield.imm64)
0dfbf9d7
L
7493 && !operand_type_equal (&overlap, &imm8)
7494 && !operand_type_equal (&overlap, &imm8s)
7495 && !operand_type_equal (&overlap, &imm16)
7496 && !operand_type_equal (&overlap, &imm32)
7497 && !operand_type_equal (&overlap, &imm32s)
7498 && !operand_type_equal (&overlap, &imm64))
29b0f896
AM
7499 {
7500 if (i.suffix)
7501 {
40fb9820
L
7502 i386_operand_type temp;
7503
0dfbf9d7 7504 operand_type_set (&temp, 0);
7ab9ffdd 7505 if (i.suffix == BYTE_MNEM_SUFFIX)
40fb9820
L
7506 {
7507 temp.bitfield.imm8 = overlap.bitfield.imm8;
7508 temp.bitfield.imm8s = overlap.bitfield.imm8s;
7509 }
7510 else if (i.suffix == WORD_MNEM_SUFFIX)
7511 temp.bitfield.imm16 = overlap.bitfield.imm16;
7512 else if (i.suffix == QWORD_MNEM_SUFFIX)
7513 {
7514 temp.bitfield.imm64 = overlap.bitfield.imm64;
7515 temp.bitfield.imm32s = overlap.bitfield.imm32s;
7516 }
7517 else
7518 temp.bitfield.imm32 = overlap.bitfield.imm32;
7519 overlap = temp;
29b0f896 7520 }
0dfbf9d7
L
7521 else if (operand_type_equal (&overlap, &imm16_32_32s)
7522 || operand_type_equal (&overlap, &imm16_32)
7523 || operand_type_equal (&overlap, &imm16_32s))
29b0f896 7524 {
40fb9820 7525 if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
65da13b5 7526 overlap = imm16;
40fb9820 7527 else
65da13b5 7528 overlap = imm32s;
29b0f896 7529 }
8bbb3ad8
JB
7530 else if (i.prefix[REX_PREFIX] & REX_W)
7531 overlap = operand_type_and (overlap, imm32s);
7532 else if (i.prefix[DATA_PREFIX])
7533 overlap = operand_type_and (overlap,
7534 flag_code != CODE_16BIT ? imm16 : imm32);
0dfbf9d7
L
7535 if (!operand_type_equal (&overlap, &imm8)
7536 && !operand_type_equal (&overlap, &imm8s)
7537 && !operand_type_equal (&overlap, &imm16)
7538 && !operand_type_equal (&overlap, &imm32)
7539 && !operand_type_equal (&overlap, &imm32s)
7540 && !operand_type_equal (&overlap, &imm64))
29b0f896 7541 {
4eed87de
AM
7542 as_bad (_("no instruction mnemonic suffix given; "
7543 "can't determine immediate size"));
29b0f896
AM
7544 return 0;
7545 }
7546 }
40fb9820 7547 i.types[j] = overlap;
29b0f896 7548
40fb9820
L
7549 return 1;
7550}
7551
7552static int
7553finalize_imm (void)
7554{
bc0844ae 7555 unsigned int j, n;
29b0f896 7556
bc0844ae
L
7557 /* Update the first 2 immediate operands. */
7558 n = i.operands > 2 ? 2 : i.operands;
7559 if (n)
7560 {
7561 for (j = 0; j < n; j++)
7562 if (update_imm (j) == 0)
7563 return 0;
40fb9820 7564
bc0844ae
L
7565 /* The 3rd operand can't be immediate operand. */
7566 gas_assert (operand_type_check (i.types[2], imm) == 0);
7567 }
29b0f896
AM
7568
7569 return 1;
7570}
7571
7572static int
e3bb37b5 7573process_operands (void)
29b0f896
AM
7574{
7575 /* Default segment register this instruction will use for memory
7576 accesses. 0 means unknown. This is only for optimizing out
7577 unnecessary segment overrides. */
5e042380 7578 const reg_entry *default_seg = NULL;
29b0f896 7579
a5aeccd9
JB
7580 if (i.tm.opcode_modifier.sse2avx)
7581 {
7582 /* Legacy encoded insns allow explicit REX prefixes, so these prefixes
7583 need converting. */
7584 i.rex |= i.prefix[REX_PREFIX] & (REX_W | REX_R | REX_X | REX_B);
7585 i.prefix[REX_PREFIX] = 0;
7586 i.rex_encoding = 0;
7587 }
c423d21a
JB
7588 /* ImmExt should be processed after SSE2AVX. */
7589 else if (i.tm.opcode_modifier.immext)
7590 process_immext ();
a5aeccd9 7591
2426c15f 7592 if (i.tm.opcode_modifier.sse2avx && i.tm.opcode_modifier.vexvvvv)
29b0f896 7593 {
91d6fa6a
NC
7594 unsigned int dupl = i.operands;
7595 unsigned int dest = dupl - 1;
9fcfb3d7
L
7596 unsigned int j;
7597
c0f3af97 7598 /* The destination must be an xmm register. */
9c2799c2 7599 gas_assert (i.reg_operands
91d6fa6a 7600 && MAX_OPERANDS > dupl
7ab9ffdd 7601 && operand_type_equal (&i.types[dest], &regxmm));
c0f3af97 7602
75e5731b 7603 if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 7604 && i.tm.operand_types[0].bitfield.xmmword)
e2ec9d29 7605 {
8cd7925b 7606 if (i.tm.opcode_modifier.vexsources == VEX3SOURCES)
c0f3af97
L
7607 {
7608 /* Keep xmm0 for instructions with VEX prefix and 3
7609 sources. */
75e5731b 7610 i.tm.operand_types[0].bitfield.instance = InstanceNone;
3528c362 7611 i.tm.operand_types[0].bitfield.class = RegSIMD;
c0f3af97
L
7612 goto duplicate;
7613 }
e2ec9d29 7614 else
c0f3af97
L
7615 {
7616 /* We remove the first xmm0 and keep the number of
7617 operands unchanged, which in fact duplicates the
7618 destination. */
7619 for (j = 1; j < i.operands; j++)
7620 {
7621 i.op[j - 1] = i.op[j];
7622 i.types[j - 1] = i.types[j];
7623 i.tm.operand_types[j - 1] = i.tm.operand_types[j];
8dc0818e 7624 i.flags[j - 1] = i.flags[j];
c0f3af97
L
7625 }
7626 }
7627 }
7628 else if (i.tm.opcode_modifier.implicit1stxmm0)
7ab9ffdd 7629 {
91d6fa6a 7630 gas_assert ((MAX_OPERANDS - 1) > dupl
8cd7925b
L
7631 && (i.tm.opcode_modifier.vexsources
7632 == VEX3SOURCES));
c0f3af97
L
7633
7634 /* Add the implicit xmm0 for instructions with VEX prefix
7635 and 3 sources. */
7636 for (j = i.operands; j > 0; j--)
7637 {
7638 i.op[j] = i.op[j - 1];
7639 i.types[j] = i.types[j - 1];
7640 i.tm.operand_types[j] = i.tm.operand_types[j - 1];
8dc0818e 7641 i.flags[j] = i.flags[j - 1];
c0f3af97
L
7642 }
7643 i.op[0].regs
629310ab 7644 = (const reg_entry *) str_hash_find (reg_hash, "xmm0");
7ab9ffdd 7645 i.types[0] = regxmm;
c0f3af97
L
7646 i.tm.operand_types[0] = regxmm;
7647
7648 i.operands += 2;
7649 i.reg_operands += 2;
7650 i.tm.operands += 2;
7651
91d6fa6a 7652 dupl++;
c0f3af97 7653 dest++;
91d6fa6a
NC
7654 i.op[dupl] = i.op[dest];
7655 i.types[dupl] = i.types[dest];
7656 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 7657 i.flags[dupl] = i.flags[dest];
e2ec9d29 7658 }
c0f3af97
L
7659 else
7660 {
dc1e8a47 7661 duplicate:
c0f3af97
L
7662 i.operands++;
7663 i.reg_operands++;
7664 i.tm.operands++;
7665
91d6fa6a
NC
7666 i.op[dupl] = i.op[dest];
7667 i.types[dupl] = i.types[dest];
7668 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 7669 i.flags[dupl] = i.flags[dest];
c0f3af97
L
7670 }
7671
7672 if (i.tm.opcode_modifier.immext)
7673 process_immext ();
7674 }
75e5731b 7675 else if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 7676 && i.tm.operand_types[0].bitfield.xmmword)
c0f3af97
L
7677 {
7678 unsigned int j;
7679
9fcfb3d7
L
7680 for (j = 1; j < i.operands; j++)
7681 {
7682 i.op[j - 1] = i.op[j];
7683 i.types[j - 1] = i.types[j];
7684
7685 /* We need to adjust fields in i.tm since they are used by
7686 build_modrm_byte. */
7687 i.tm.operand_types [j - 1] = i.tm.operand_types [j];
8dc0818e
JB
7688
7689 i.flags[j - 1] = i.flags[j];
9fcfb3d7
L
7690 }
7691
e2ec9d29
L
7692 i.operands--;
7693 i.reg_operands--;
e2ec9d29
L
7694 i.tm.operands--;
7695 }
920d2ddc
IT
7696 else if (i.tm.opcode_modifier.implicitquadgroup)
7697 {
a477a8c4
JB
7698 unsigned int regnum, first_reg_in_group, last_reg_in_group;
7699
920d2ddc 7700 /* The second operand must be {x,y,z}mmN, where N is a multiple of 4. */
3528c362 7701 gas_assert (i.operands >= 2 && i.types[1].bitfield.class == RegSIMD);
a477a8c4
JB
7702 regnum = register_number (i.op[1].regs);
7703 first_reg_in_group = regnum & ~3;
7704 last_reg_in_group = first_reg_in_group + 3;
7705 if (regnum != first_reg_in_group)
7706 as_warn (_("source register `%s%s' implicitly denotes"
7707 " `%s%.3s%u' to `%s%.3s%u' source group in `%s'"),
7708 register_prefix, i.op[1].regs->reg_name,
7709 register_prefix, i.op[1].regs->reg_name, first_reg_in_group,
7710 register_prefix, i.op[1].regs->reg_name, last_reg_in_group,
7711 i.tm.name);
7712 }
e2ec9d29
L
7713 else if (i.tm.opcode_modifier.regkludge)
7714 {
7715 /* The imul $imm, %reg instruction is converted into
7716 imul $imm, %reg, %reg, and the clr %reg instruction
7717 is converted into xor %reg, %reg. */
7718
7719 unsigned int first_reg_op;
7720
7721 if (operand_type_check (i.types[0], reg))
7722 first_reg_op = 0;
7723 else
7724 first_reg_op = 1;
7725 /* Pretend we saw the extra register operand. */
9c2799c2 7726 gas_assert (i.reg_operands == 1
7ab9ffdd 7727 && i.op[first_reg_op + 1].regs == 0);
e2ec9d29
L
7728 i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
7729 i.types[first_reg_op + 1] = i.types[first_reg_op];
7730 i.operands++;
7731 i.reg_operands++;
29b0f896
AM
7732 }
7733
85b80b0f 7734 if (i.tm.opcode_modifier.modrm)
29b0f896
AM
7735 {
7736 /* The opcode is completed (modulo i.tm.extension_opcode which
52271982
AM
7737 must be put into the modrm byte). Now, we make the modrm and
7738 index base bytes based on all the info we've collected. */
29b0f896
AM
7739
7740 default_seg = build_modrm_byte ();
7741 }
00cee14f 7742 else if (i.types[0].bitfield.class == SReg)
85b80b0f
JB
7743 {
7744 if (flag_code != CODE_64BIT
7745 ? i.tm.base_opcode == POP_SEG_SHORT
7746 && i.op[0].regs->reg_num == 1
389d00a5 7747 : (i.tm.base_opcode | 1) == (POP_SEG386_SHORT & 0xff)
85b80b0f
JB
7748 && i.op[0].regs->reg_num < 4)
7749 {
7750 as_bad (_("you can't `%s %s%s'"),
7751 i.tm.name, register_prefix, i.op[0].regs->reg_name);
7752 return 0;
7753 }
389d00a5
JB
7754 if (i.op[0].regs->reg_num > 3
7755 && i.tm.opcode_modifier.opcodespace == SPACE_BASE )
85b80b0f 7756 {
389d00a5
JB
7757 i.tm.base_opcode ^= (POP_SEG_SHORT ^ POP_SEG386_SHORT) & 0xff;
7758 i.tm.opcode_modifier.opcodespace = SPACE_0F;
85b80b0f
JB
7759 }
7760 i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
7761 }
389d00a5
JB
7762 else if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
7763 && (i.tm.base_opcode & ~3) == MOV_AX_DISP32)
29b0f896 7764 {
5e042380 7765 default_seg = reg_ds;
29b0f896 7766 }
40fb9820 7767 else if (i.tm.opcode_modifier.isstring)
29b0f896
AM
7768 {
7769 /* For the string instructions that allow a segment override
7770 on one of their operands, the default segment is ds. */
5e042380 7771 default_seg = reg_ds;
29b0f896 7772 }
50128d0c 7773 else if (i.short_form)
85b80b0f
JB
7774 {
7775 /* The register or float register operand is in operand
7776 0 or 1. */
bab6aec1 7777 unsigned int op = i.tm.operand_types[0].bitfield.class != Reg;
85b80b0f
JB
7778
7779 /* Register goes in low 3 bits of opcode. */
7780 i.tm.base_opcode |= i.op[op].regs->reg_num;
7781 if ((i.op[op].regs->reg_flags & RegRex) != 0)
7782 i.rex |= REX_B;
7783 if (!quiet_warnings && i.tm.opcode_modifier.ugh)
7784 {
7785 /* Warn about some common errors, but press on regardless.
7786 The first case can be generated by gcc (<= 2.8.1). */
7787 if (i.operands == 2)
7788 {
7789 /* Reversed arguments on faddp, fsubp, etc. */
7790 as_warn (_("translating to `%s %s%s,%s%s'"), i.tm.name,
7791 register_prefix, i.op[!intel_syntax].regs->reg_name,
7792 register_prefix, i.op[intel_syntax].regs->reg_name);
7793 }
7794 else
7795 {
7796 /* Extraneous `l' suffix on fp insn. */
7797 as_warn (_("translating to `%s %s%s'"), i.tm.name,
7798 register_prefix, i.op[0].regs->reg_name);
7799 }
7800 }
7801 }
29b0f896 7802
514a8bb0 7803 if ((i.seg[0] || i.prefix[SEG_PREFIX])
514a8bb0 7804 && i.tm.base_opcode == 0x8d /* lea */
35648716 7805 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
514a8bb0 7806 && !is_any_vex_encoding(&i.tm))
92334ad2
JB
7807 {
7808 if (!quiet_warnings)
7809 as_warn (_("segment override on `%s' is ineffectual"), i.tm.name);
7810 if (optimize)
7811 {
7812 i.seg[0] = NULL;
7813 i.prefix[SEG_PREFIX] = 0;
7814 }
7815 }
52271982
AM
7816
7817 /* If a segment was explicitly specified, and the specified segment
b6773884
JB
7818 is neither the default nor the one already recorded from a prefix,
7819 use an opcode prefix to select it. If we never figured out what
7820 the default segment is, then default_seg will be zero at this
7821 point, and the specified segment prefix will always be used. */
7822 if (i.seg[0]
7823 && i.seg[0] != default_seg
5e042380 7824 && i386_seg_prefixes[i.seg[0]->reg_num] != i.prefix[SEG_PREFIX])
29b0f896 7825 {
5e042380 7826 if (!add_prefix (i386_seg_prefixes[i.seg[0]->reg_num]))
29b0f896
AM
7827 return 0;
7828 }
7829 return 1;
7830}
7831
a5aeccd9 7832static INLINE void set_rex_vrex (const reg_entry *r, unsigned int rex_bit,
5b7c81bd 7833 bool do_sse2avx)
a5aeccd9
JB
7834{
7835 if (r->reg_flags & RegRex)
7836 {
7837 if (i.rex & rex_bit)
7838 as_bad (_("same type of prefix used twice"));
7839 i.rex |= rex_bit;
7840 }
7841 else if (do_sse2avx && (i.rex & rex_bit) && i.vex.register_specifier)
7842 {
7843 gas_assert (i.vex.register_specifier == r);
7844 i.vex.register_specifier += 8;
7845 }
7846
7847 if (r->reg_flags & RegVRex)
7848 i.vrex |= rex_bit;
7849}
7850
5e042380 7851static const reg_entry *
e3bb37b5 7852build_modrm_byte (void)
29b0f896 7853{
5e042380 7854 const reg_entry *default_seg = NULL;
c0f3af97 7855 unsigned int source, dest;
8cd7925b 7856 int vex_3_sources;
c0f3af97 7857
8cd7925b 7858 vex_3_sources = i.tm.opcode_modifier.vexsources == VEX3SOURCES;
c0f3af97
L
7859 if (vex_3_sources)
7860 {
91d6fa6a 7861 unsigned int nds, reg_slot;
4c2c6516 7862 expressionS *exp;
c0f3af97 7863
6b8d3588 7864 dest = i.operands - 1;
c0f3af97 7865 nds = dest - 1;
922d8de8 7866
a683cc34 7867 /* There are 2 kinds of instructions:
bed3d976 7868 1. 5 operands: 4 register operands or 3 register operands
9d3bf266 7869 plus 1 memory operand plus one Imm4 operand, VexXDS, and
bed3d976 7870 VexW0 or VexW1. The destination must be either XMM, YMM or
43234a1e 7871 ZMM register.
bed3d976 7872 2. 4 operands: 4 register operands or 3 register operands
2f1bada2 7873 plus 1 memory operand, with VexXDS. */
922d8de8 7874 gas_assert ((i.reg_operands == 4
bed3d976
JB
7875 || (i.reg_operands == 3 && i.mem_operands == 1))
7876 && i.tm.opcode_modifier.vexvvvv == VEXXDS
dcd7e323 7877 && i.tm.opcode_modifier.vexw
3528c362 7878 && i.tm.operand_types[dest].bitfield.class == RegSIMD);
a683cc34 7879
48db9223
JB
7880 /* If VexW1 is set, the first non-immediate operand is the source and
7881 the second non-immediate one is encoded in the immediate operand. */
7882 if (i.tm.opcode_modifier.vexw == VEXW1)
7883 {
7884 source = i.imm_operands;
7885 reg_slot = i.imm_operands + 1;
7886 }
7887 else
7888 {
7889 source = i.imm_operands + 1;
7890 reg_slot = i.imm_operands;
7891 }
7892
a683cc34 7893 if (i.imm_operands == 0)
bed3d976
JB
7894 {
7895 /* When there is no immediate operand, generate an 8bit
7896 immediate operand to encode the first operand. */
7897 exp = &im_expressions[i.imm_operands++];
7898 i.op[i.operands].imms = exp;
7899 i.types[i.operands] = imm8;
7900 i.operands++;
7901
3528c362 7902 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
bed3d976
JB
7903 exp->X_op = O_constant;
7904 exp->X_add_number = register_number (i.op[reg_slot].regs) << 4;
43234a1e
L
7905 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
7906 }
922d8de8 7907 else
bed3d976 7908 {
9d3bf266
JB
7909 gas_assert (i.imm_operands == 1);
7910 gas_assert (fits_in_imm4 (i.op[0].imms->X_add_number));
7911 gas_assert (!i.tm.opcode_modifier.immext);
a683cc34 7912
9d3bf266
JB
7913 /* Turn on Imm8 again so that output_imm will generate it. */
7914 i.types[0].bitfield.imm8 = 1;
bed3d976 7915
3528c362 7916 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
9d3bf266 7917 i.op[0].imms->X_add_number
bed3d976 7918 |= register_number (i.op[reg_slot].regs) << 4;
43234a1e 7919 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
bed3d976 7920 }
a683cc34 7921
3528c362 7922 gas_assert (i.tm.operand_types[nds].bitfield.class == RegSIMD);
dae39acc 7923 i.vex.register_specifier = i.op[nds].regs;
c0f3af97
L
7924 }
7925 else
7926 source = dest = 0;
29b0f896
AM
7927
7928 /* i.reg_operands MUST be the number of real register operands;
c0f3af97
L
7929 implicit registers do not count. If there are 3 register
7930 operands, it must be a instruction with VexNDS. For a
7931 instruction with VexNDD, the destination register is encoded
7932 in VEX prefix. If there are 4 register operands, it must be
7933 a instruction with VEX prefix and 3 sources. */
7ab9ffdd
L
7934 if (i.mem_operands == 0
7935 && ((i.reg_operands == 2
2426c15f 7936 && i.tm.opcode_modifier.vexvvvv <= VEXXDS)
7ab9ffdd 7937 || (i.reg_operands == 3
2426c15f 7938 && i.tm.opcode_modifier.vexvvvv == VEXXDS)
7ab9ffdd 7939 || (i.reg_operands == 4 && vex_3_sources)))
29b0f896 7940 {
cab737b9
L
7941 switch (i.operands)
7942 {
7943 case 2:
7944 source = 0;
7945 break;
7946 case 3:
c81128dc
L
7947 /* When there are 3 operands, one of them may be immediate,
7948 which may be the first or the last operand. Otherwise,
c0f3af97
L
7949 the first operand must be shift count register (cl) or it
7950 is an instruction with VexNDS. */
9c2799c2 7951 gas_assert (i.imm_operands == 1
7ab9ffdd 7952 || (i.imm_operands == 0
2426c15f 7953 && (i.tm.opcode_modifier.vexvvvv == VEXXDS
75e5731b
JB
7954 || (i.types[0].bitfield.instance == RegC
7955 && i.types[0].bitfield.byte))));
40fb9820 7956 if (operand_type_check (i.types[0], imm)
75e5731b
JB
7957 || (i.types[0].bitfield.instance == RegC
7958 && i.types[0].bitfield.byte))
40fb9820
L
7959 source = 1;
7960 else
7961 source = 0;
cab737b9
L
7962 break;
7963 case 4:
368d64cc
L
7964 /* When there are 4 operands, the first two must be 8bit
7965 immediate operands. The source operand will be the 3rd
c0f3af97
L
7966 one.
7967
7968 For instructions with VexNDS, if the first operand
7969 an imm8, the source operand is the 2nd one. If the last
7970 operand is imm8, the source operand is the first one. */
9c2799c2 7971 gas_assert ((i.imm_operands == 2
7ab9ffdd
L
7972 && i.types[0].bitfield.imm8
7973 && i.types[1].bitfield.imm8)
2426c15f 7974 || (i.tm.opcode_modifier.vexvvvv == VEXXDS
7ab9ffdd
L
7975 && i.imm_operands == 1
7976 && (i.types[0].bitfield.imm8
43234a1e 7977 || i.types[i.operands - 1].bitfield.imm8
ca5312a2 7978 || i.rounding.type != rc_none)));
9f2670f2
L
7979 if (i.imm_operands == 2)
7980 source = 2;
7981 else
c0f3af97
L
7982 {
7983 if (i.types[0].bitfield.imm8)
7984 source = 1;
7985 else
7986 source = 0;
7987 }
c0f3af97
L
7988 break;
7989 case 5:
e771e7c9 7990 if (is_evex_encoding (&i.tm))
43234a1e
L
7991 {
7992 /* For EVEX instructions, when there are 5 operands, the
7993 first one must be immediate operand. If the second one
7994 is immediate operand, the source operand is the 3th
7995 one. If the last one is immediate operand, the source
7996 operand is the 2nd one. */
7997 gas_assert (i.imm_operands == 2
7998 && i.tm.opcode_modifier.sae
7999 && operand_type_check (i.types[0], imm));
8000 if (operand_type_check (i.types[1], imm))
8001 source = 2;
8002 else if (operand_type_check (i.types[4], imm))
8003 source = 1;
8004 else
8005 abort ();
8006 }
cab737b9
L
8007 break;
8008 default:
8009 abort ();
8010 }
8011
c0f3af97
L
8012 if (!vex_3_sources)
8013 {
8014 dest = source + 1;
8015
43234a1e
L
8016 /* RC/SAE operand could be between DEST and SRC. That happens
8017 when one operand is GPR and the other one is XMM/YMM/ZMM
8018 register. */
ca5312a2 8019 if (i.rounding.type != rc_none && i.rounding.operand == dest)
43234a1e
L
8020 dest++;
8021
2426c15f 8022 if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
c0f3af97 8023 {
43234a1e 8024 /* For instructions with VexNDS, the register-only source
c5d0745b 8025 operand must be a 32/64bit integer, XMM, YMM, ZMM, or mask
dfd69174 8026 register. It is encoded in VEX prefix. */
f12dc422
L
8027
8028 i386_operand_type op;
8029 unsigned int vvvv;
8030
c2ecccb3
L
8031 /* Swap two source operands if needed. */
8032 if (i.tm.opcode_modifier.swapsources)
f12dc422
L
8033 {
8034 vvvv = source;
8035 source = dest;
8036 }
8037 else
8038 vvvv = dest;
8039
8040 op = i.tm.operand_types[vvvv];
c0f3af97 8041 if ((dest + 1) >= i.operands
bab6aec1 8042 || ((op.bitfield.class != Reg
dc821c5f 8043 || (!op.bitfield.dword && !op.bitfield.qword))
3528c362 8044 && op.bitfield.class != RegSIMD
43234a1e 8045 && !operand_type_equal (&op, &regmask)))
c0f3af97 8046 abort ();
f12dc422 8047 i.vex.register_specifier = i.op[vvvv].regs;
c0f3af97
L
8048 dest++;
8049 }
8050 }
29b0f896
AM
8051
8052 i.rm.mode = 3;
dfd69174
JB
8053 /* One of the register operands will be encoded in the i.rm.reg
8054 field, the other in the combined i.rm.mode and i.rm.regmem
29b0f896
AM
8055 fields. If no form of this instruction supports a memory
8056 destination operand, then we assume the source operand may
8057 sometimes be a memory operand and so we need to store the
8058 destination in the i.rm.reg field. */
dfd69174 8059 if (!i.tm.opcode_modifier.regmem
40fb9820 8060 && operand_type_check (i.tm.operand_types[dest], anymem) == 0)
29b0f896
AM
8061 {
8062 i.rm.reg = i.op[dest].regs->reg_num;
8063 i.rm.regmem = i.op[source].regs->reg_num;
a5aeccd9 8064 set_rex_vrex (i.op[dest].regs, REX_R, i.tm.opcode_modifier.sse2avx);
5b7c81bd 8065 set_rex_vrex (i.op[source].regs, REX_B, false);
29b0f896
AM
8066 }
8067 else
8068 {
8069 i.rm.reg = i.op[source].regs->reg_num;
8070 i.rm.regmem = i.op[dest].regs->reg_num;
a5aeccd9 8071 set_rex_vrex (i.op[dest].regs, REX_B, i.tm.opcode_modifier.sse2avx);
5b7c81bd 8072 set_rex_vrex (i.op[source].regs, REX_R, false);
29b0f896 8073 }
e0c7f900 8074 if (flag_code != CODE_64BIT && (i.rex & REX_R))
c4a530c5 8075 {
4a5c67ed 8076 if (i.types[!i.tm.opcode_modifier.regmem].bitfield.class != RegCR)
c4a530c5 8077 abort ();
e0c7f900 8078 i.rex &= ~REX_R;
c4a530c5
JB
8079 add_prefix (LOCK_PREFIX_OPCODE);
8080 }
29b0f896
AM
8081 }
8082 else
8083 { /* If it's not 2 reg operands... */
c0f3af97
L
8084 unsigned int mem;
8085
29b0f896
AM
8086 if (i.mem_operands)
8087 {
8088 unsigned int fake_zero_displacement = 0;
99018f42 8089 unsigned int op;
4eed87de 8090
7ab9ffdd 8091 for (op = 0; op < i.operands; op++)
8dc0818e 8092 if (i.flags[op] & Operand_Mem)
7ab9ffdd 8093 break;
7ab9ffdd 8094 gas_assert (op < i.operands);
29b0f896 8095
63112cd6 8096 if (i.tm.opcode_modifier.sib)
6c30d220 8097 {
260cd341
LC
8098 /* The index register of VSIB shouldn't be RegIZ. */
8099 if (i.tm.opcode_modifier.sib != SIBMEM
8100 && i.index_reg->reg_num == RegIZ)
6c30d220
L
8101 abort ();
8102
8103 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8104 if (!i.base_reg)
8105 {
8106 i.sib.base = NO_BASE_REGISTER;
8107 i.sib.scale = i.log2_scale_factor;
8108 i.types[op].bitfield.disp8 = 0;
8109 i.types[op].bitfield.disp16 = 0;
8110 i.types[op].bitfield.disp64 = 0;
43083a50 8111 if (flag_code != CODE_64BIT || i.prefix[ADDR_PREFIX])
6c30d220
L
8112 {
8113 /* Must be 32 bit */
8114 i.types[op].bitfield.disp32 = 1;
8115 i.types[op].bitfield.disp32s = 0;
8116 }
8117 else
8118 {
8119 i.types[op].bitfield.disp32 = 0;
8120 i.types[op].bitfield.disp32s = 1;
8121 }
8122 }
260cd341
LC
8123
8124 /* Since the mandatory SIB always has index register, so
8125 the code logic remains unchanged. The non-mandatory SIB
8126 without index register is allowed and will be handled
8127 later. */
8128 if (i.index_reg)
8129 {
8130 if (i.index_reg->reg_num == RegIZ)
8131 i.sib.index = NO_INDEX_REGISTER;
8132 else
8133 i.sib.index = i.index_reg->reg_num;
5b7c81bd 8134 set_rex_vrex (i.index_reg, REX_X, false);
260cd341 8135 }
6c30d220
L
8136 }
8137
5e042380 8138 default_seg = reg_ds;
29b0f896
AM
8139
8140 if (i.base_reg == 0)
8141 {
8142 i.rm.mode = 0;
8143 if (!i.disp_operands)
9bb129e8 8144 fake_zero_displacement = 1;
29b0f896
AM
8145 if (i.index_reg == 0)
8146 {
73053c1f
JB
8147 i386_operand_type newdisp;
8148
260cd341
LC
8149 /* Both check for VSIB and mandatory non-vector SIB. */
8150 gas_assert (!i.tm.opcode_modifier.sib
8151 || i.tm.opcode_modifier.sib == SIBMEM);
29b0f896 8152 /* Operand is just <disp> */
20f0a1fc 8153 if (flag_code == CODE_64BIT)
29b0f896
AM
8154 {
8155 /* 64bit mode overwrites the 32bit absolute
8156 addressing by RIP relative addressing and
8157 absolute addressing is encoded by one of the
8158 redundant SIB forms. */
8159 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8160 i.sib.base = NO_BASE_REGISTER;
8161 i.sib.index = NO_INDEX_REGISTER;
73053c1f 8162 newdisp = (!i.prefix[ADDR_PREFIX] ? disp32s : disp32);
20f0a1fc 8163 }
fc225355
L
8164 else if ((flag_code == CODE_16BIT)
8165 ^ (i.prefix[ADDR_PREFIX] != 0))
20f0a1fc
NC
8166 {
8167 i.rm.regmem = NO_BASE_REGISTER_16;
73053c1f 8168 newdisp = disp16;
20f0a1fc
NC
8169 }
8170 else
8171 {
8172 i.rm.regmem = NO_BASE_REGISTER;
73053c1f 8173 newdisp = disp32;
29b0f896 8174 }
73053c1f
JB
8175 i.types[op] = operand_type_and_not (i.types[op], anydisp);
8176 i.types[op] = operand_type_or (i.types[op], newdisp);
29b0f896 8177 }
63112cd6 8178 else if (!i.tm.opcode_modifier.sib)
29b0f896 8179 {
6c30d220 8180 /* !i.base_reg && i.index_reg */
e968fc9b 8181 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8182 i.sib.index = NO_INDEX_REGISTER;
8183 else
8184 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8185 i.sib.base = NO_BASE_REGISTER;
8186 i.sib.scale = i.log2_scale_factor;
8187 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
40fb9820
L
8188 i.types[op].bitfield.disp8 = 0;
8189 i.types[op].bitfield.disp16 = 0;
8190 i.types[op].bitfield.disp64 = 0;
43083a50 8191 if (flag_code != CODE_64BIT || i.prefix[ADDR_PREFIX])
40fb9820
L
8192 {
8193 /* Must be 32 bit */
8194 i.types[op].bitfield.disp32 = 1;
8195 i.types[op].bitfield.disp32s = 0;
8196 }
29b0f896 8197 else
40fb9820
L
8198 {
8199 i.types[op].bitfield.disp32 = 0;
8200 i.types[op].bitfield.disp32s = 1;
8201 }
29b0f896 8202 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8203 i.rex |= REX_X;
29b0f896
AM
8204 }
8205 }
8206 /* RIP addressing for 64bit mode. */
e968fc9b 8207 else if (i.base_reg->reg_num == RegIP)
29b0f896 8208 {
63112cd6 8209 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896 8210 i.rm.regmem = NO_BASE_REGISTER;
40fb9820
L
8211 i.types[op].bitfield.disp8 = 0;
8212 i.types[op].bitfield.disp16 = 0;
8213 i.types[op].bitfield.disp32 = 0;
8214 i.types[op].bitfield.disp32s = 1;
8215 i.types[op].bitfield.disp64 = 0;
71903a11 8216 i.flags[op] |= Operand_PCrel;
20f0a1fc
NC
8217 if (! i.disp_operands)
8218 fake_zero_displacement = 1;
29b0f896 8219 }
dc821c5f 8220 else if (i.base_reg->reg_type.bitfield.word)
29b0f896 8221 {
63112cd6 8222 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896
AM
8223 switch (i.base_reg->reg_num)
8224 {
8225 case 3: /* (%bx) */
8226 if (i.index_reg == 0)
8227 i.rm.regmem = 7;
8228 else /* (%bx,%si) -> 0, or (%bx,%di) -> 1 */
8229 i.rm.regmem = i.index_reg->reg_num - 6;
8230 break;
8231 case 5: /* (%bp) */
5e042380 8232 default_seg = reg_ss;
29b0f896
AM
8233 if (i.index_reg == 0)
8234 {
8235 i.rm.regmem = 6;
40fb9820 8236 if (operand_type_check (i.types[op], disp) == 0)
29b0f896
AM
8237 {
8238 /* fake (%bp) into 0(%bp) */
41eb8e88 8239 if (i.disp_encoding == disp_encoding_16bit)
1a02d6b0
L
8240 i.types[op].bitfield.disp16 = 1;
8241 else
8242 i.types[op].bitfield.disp8 = 1;
252b5132 8243 fake_zero_displacement = 1;
29b0f896
AM
8244 }
8245 }
8246 else /* (%bp,%si) -> 2, or (%bp,%di) -> 3 */
8247 i.rm.regmem = i.index_reg->reg_num - 6 + 2;
8248 break;
8249 default: /* (%si) -> 4 or (%di) -> 5 */
8250 i.rm.regmem = i.base_reg->reg_num - 6 + 4;
8251 }
41eb8e88
L
8252 if (!fake_zero_displacement
8253 && !i.disp_operands
8254 && i.disp_encoding)
8255 {
8256 fake_zero_displacement = 1;
8257 if (i.disp_encoding == disp_encoding_8bit)
8258 i.types[op].bitfield.disp8 = 1;
8259 else
8260 i.types[op].bitfield.disp16 = 1;
8261 }
29b0f896
AM
8262 i.rm.mode = mode_from_disp_size (i.types[op]);
8263 }
8264 else /* i.base_reg and 32/64 bit mode */
8265 {
8266 if (flag_code == CODE_64BIT
40fb9820
L
8267 && operand_type_check (i.types[op], disp))
8268 {
73053c1f
JB
8269 i.types[op].bitfield.disp16 = 0;
8270 i.types[op].bitfield.disp64 = 0;
40fb9820 8271 if (i.prefix[ADDR_PREFIX] == 0)
73053c1f
JB
8272 {
8273 i.types[op].bitfield.disp32 = 0;
8274 i.types[op].bitfield.disp32s = 1;
8275 }
40fb9820 8276 else
73053c1f
JB
8277 {
8278 i.types[op].bitfield.disp32 = 1;
8279 i.types[op].bitfield.disp32s = 0;
8280 }
40fb9820 8281 }
20f0a1fc 8282
63112cd6 8283 if (!i.tm.opcode_modifier.sib)
6c30d220 8284 i.rm.regmem = i.base_reg->reg_num;
29b0f896 8285 if ((i.base_reg->reg_flags & RegRex) != 0)
161a04f6 8286 i.rex |= REX_B;
29b0f896
AM
8287 i.sib.base = i.base_reg->reg_num;
8288 /* x86-64 ignores REX prefix bit here to avoid decoder
8289 complications. */
848930b2
JB
8290 if (!(i.base_reg->reg_flags & RegRex)
8291 && (i.base_reg->reg_num == EBP_REG_NUM
8292 || i.base_reg->reg_num == ESP_REG_NUM))
5e042380 8293 default_seg = reg_ss;
848930b2 8294 if (i.base_reg->reg_num == 5 && i.disp_operands == 0)
29b0f896 8295 {
848930b2 8296 fake_zero_displacement = 1;
1a02d6b0
L
8297 if (i.disp_encoding == disp_encoding_32bit)
8298 i.types[op].bitfield.disp32 = 1;
8299 else
8300 i.types[op].bitfield.disp8 = 1;
29b0f896
AM
8301 }
8302 i.sib.scale = i.log2_scale_factor;
8303 if (i.index_reg == 0)
8304 {
260cd341
LC
8305 /* Only check for VSIB. */
8306 gas_assert (i.tm.opcode_modifier.sib != VECSIB128
8307 && i.tm.opcode_modifier.sib != VECSIB256
8308 && i.tm.opcode_modifier.sib != VECSIB512);
8309
29b0f896
AM
8310 /* <disp>(%esp) becomes two byte modrm with no index
8311 register. We've already stored the code for esp
8312 in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
8313 Any base register besides %esp will not use the
8314 extra modrm byte. */
8315 i.sib.index = NO_INDEX_REGISTER;
29b0f896 8316 }
63112cd6 8317 else if (!i.tm.opcode_modifier.sib)
29b0f896 8318 {
e968fc9b 8319 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8320 i.sib.index = NO_INDEX_REGISTER;
8321 else
8322 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8323 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8324 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8325 i.rex |= REX_X;
29b0f896 8326 }
67a4f2b7
AO
8327
8328 if (i.disp_operands
8329 && (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
8330 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL))
8331 i.rm.mode = 0;
8332 else
a501d77e
L
8333 {
8334 if (!fake_zero_displacement
8335 && !i.disp_operands
8336 && i.disp_encoding)
8337 {
8338 fake_zero_displacement = 1;
8339 if (i.disp_encoding == disp_encoding_8bit)
8340 i.types[op].bitfield.disp8 = 1;
8341 else
8342 i.types[op].bitfield.disp32 = 1;
8343 }
8344 i.rm.mode = mode_from_disp_size (i.types[op]);
8345 }
29b0f896 8346 }
252b5132 8347
29b0f896
AM
8348 if (fake_zero_displacement)
8349 {
8350 /* Fakes a zero displacement assuming that i.types[op]
8351 holds the correct displacement size. */
8352 expressionS *exp;
8353
9c2799c2 8354 gas_assert (i.op[op].disps == 0);
29b0f896
AM
8355 exp = &disp_expressions[i.disp_operands++];
8356 i.op[op].disps = exp;
8357 exp->X_op = O_constant;
8358 exp->X_add_number = 0;
8359 exp->X_add_symbol = (symbolS *) 0;
8360 exp->X_op_symbol = (symbolS *) 0;
8361 }
c0f3af97
L
8362
8363 mem = op;
29b0f896 8364 }
c0f3af97
L
8365 else
8366 mem = ~0;
252b5132 8367
8c43a48b 8368 if (i.tm.opcode_modifier.vexsources == XOP2SOURCES)
5dd85c99
SP
8369 {
8370 if (operand_type_check (i.types[0], imm))
8371 i.vex.register_specifier = NULL;
8372 else
8373 {
8374 /* VEX.vvvv encodes one of the sources when the first
8375 operand is not an immediate. */
1ef99a7b 8376 if (i.tm.opcode_modifier.vexw == VEXW0)
5dd85c99
SP
8377 i.vex.register_specifier = i.op[0].regs;
8378 else
8379 i.vex.register_specifier = i.op[1].regs;
8380 }
8381
8382 /* Destination is a XMM register encoded in the ModRM.reg
8383 and VEX.R bit. */
8384 i.rm.reg = i.op[2].regs->reg_num;
8385 if ((i.op[2].regs->reg_flags & RegRex) != 0)
8386 i.rex |= REX_R;
8387
8388 /* ModRM.rm and VEX.B encodes the other source. */
8389 if (!i.mem_operands)
8390 {
8391 i.rm.mode = 3;
8392
1ef99a7b 8393 if (i.tm.opcode_modifier.vexw == VEXW0)
5dd85c99
SP
8394 i.rm.regmem = i.op[1].regs->reg_num;
8395 else
8396 i.rm.regmem = i.op[0].regs->reg_num;
8397
8398 if ((i.op[1].regs->reg_flags & RegRex) != 0)
8399 i.rex |= REX_B;
8400 }
8401 }
2426c15f 8402 else if (i.tm.opcode_modifier.vexvvvv == VEXLWP)
f88c9eb0
SP
8403 {
8404 i.vex.register_specifier = i.op[2].regs;
8405 if (!i.mem_operands)
8406 {
8407 i.rm.mode = 3;
8408 i.rm.regmem = i.op[1].regs->reg_num;
8409 if ((i.op[1].regs->reg_flags & RegRex) != 0)
8410 i.rex |= REX_B;
8411 }
8412 }
29b0f896
AM
8413 /* Fill in i.rm.reg or i.rm.regmem field with register operand
8414 (if any) based on i.tm.extension_opcode. Again, we must be
8415 careful to make sure that segment/control/debug/test/MMX
8416 registers are coded into the i.rm.reg field. */
f88c9eb0 8417 else if (i.reg_operands)
29b0f896 8418 {
99018f42 8419 unsigned int op;
7ab9ffdd
L
8420 unsigned int vex_reg = ~0;
8421
8422 for (op = 0; op < i.operands; op++)
921eafea
L
8423 if (i.types[op].bitfield.class == Reg
8424 || i.types[op].bitfield.class == RegBND
8425 || i.types[op].bitfield.class == RegMask
8426 || i.types[op].bitfield.class == SReg
8427 || i.types[op].bitfield.class == RegCR
8428 || i.types[op].bitfield.class == RegDR
8429 || i.types[op].bitfield.class == RegTR
8430 || i.types[op].bitfield.class == RegSIMD
8431 || i.types[op].bitfield.class == RegMMX)
8432 break;
c0209578 8433
7ab9ffdd
L
8434 if (vex_3_sources)
8435 op = dest;
2426c15f 8436 else if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
7ab9ffdd
L
8437 {
8438 /* For instructions with VexNDS, the register-only
8439 source operand is encoded in VEX prefix. */
8440 gas_assert (mem != (unsigned int) ~0);
c0f3af97 8441
7ab9ffdd 8442 if (op > mem)
c0f3af97 8443 {
7ab9ffdd
L
8444 vex_reg = op++;
8445 gas_assert (op < i.operands);
c0f3af97
L
8446 }
8447 else
c0f3af97 8448 {
f12dc422
L
8449 /* Check register-only source operand when two source
8450 operands are swapped. */
8451 if (!i.tm.operand_types[op].bitfield.baseindex
8452 && i.tm.operand_types[op + 1].bitfield.baseindex)
8453 {
8454 vex_reg = op;
8455 op += 2;
8456 gas_assert (mem == (vex_reg + 1)
8457 && op < i.operands);
8458 }
8459 else
8460 {
8461 vex_reg = op + 1;
8462 gas_assert (vex_reg < i.operands);
8463 }
c0f3af97 8464 }
7ab9ffdd 8465 }
2426c15f 8466 else if (i.tm.opcode_modifier.vexvvvv == VEXNDD)
7ab9ffdd 8467 {
f12dc422 8468 /* For instructions with VexNDD, the register destination
7ab9ffdd 8469 is encoded in VEX prefix. */
f12dc422
L
8470 if (i.mem_operands == 0)
8471 {
8472 /* There is no memory operand. */
8473 gas_assert ((op + 2) == i.operands);
8474 vex_reg = op + 1;
8475 }
8476 else
8d63c93e 8477 {
ed438a93
JB
8478 /* There are only 2 non-immediate operands. */
8479 gas_assert (op < i.imm_operands + 2
8480 && i.operands == i.imm_operands + 2);
8481 vex_reg = i.imm_operands + 1;
f12dc422 8482 }
7ab9ffdd
L
8483 }
8484 else
8485 gas_assert (op < i.operands);
99018f42 8486
7ab9ffdd
L
8487 if (vex_reg != (unsigned int) ~0)
8488 {
f12dc422 8489 i386_operand_type *type = &i.tm.operand_types[vex_reg];
7ab9ffdd 8490
bab6aec1 8491 if ((type->bitfield.class != Reg
dc821c5f 8492 || (!type->bitfield.dword && !type->bitfield.qword))
3528c362 8493 && type->bitfield.class != RegSIMD
43234a1e 8494 && !operand_type_equal (type, &regmask))
7ab9ffdd 8495 abort ();
f88c9eb0 8496
7ab9ffdd
L
8497 i.vex.register_specifier = i.op[vex_reg].regs;
8498 }
8499
1b9f0c97
L
8500 /* Don't set OP operand twice. */
8501 if (vex_reg != op)
7ab9ffdd 8502 {
1b9f0c97
L
8503 /* If there is an extension opcode to put here, the
8504 register number must be put into the regmem field. */
8505 if (i.tm.extension_opcode != None)
8506 {
8507 i.rm.regmem = i.op[op].regs->reg_num;
a5aeccd9
JB
8508 set_rex_vrex (i.op[op].regs, REX_B,
8509 i.tm.opcode_modifier.sse2avx);
1b9f0c97
L
8510 }
8511 else
8512 {
8513 i.rm.reg = i.op[op].regs->reg_num;
a5aeccd9
JB
8514 set_rex_vrex (i.op[op].regs, REX_R,
8515 i.tm.opcode_modifier.sse2avx);
1b9f0c97 8516 }
7ab9ffdd 8517 }
252b5132 8518
29b0f896
AM
8519 /* Now, if no memory operand has set i.rm.mode = 0, 1, 2 we
8520 must set it to 3 to indicate this is a register operand
8521 in the regmem field. */
8522 if (!i.mem_operands)
8523 i.rm.mode = 3;
8524 }
252b5132 8525
29b0f896 8526 /* Fill in i.rm.reg field with extension opcode (if any). */
c1e679ec 8527 if (i.tm.extension_opcode != None)
29b0f896
AM
8528 i.rm.reg = i.tm.extension_opcode;
8529 }
8530 return default_seg;
8531}
252b5132 8532
48ef937e
JB
8533static INLINE void
8534frag_opcode_byte (unsigned char byte)
8535{
8536 if (now_seg != absolute_section)
8537 FRAG_APPEND_1_CHAR (byte);
8538 else
8539 ++abs_section_offset;
8540}
8541
376cd056
JB
8542static unsigned int
8543flip_code16 (unsigned int code16)
8544{
8545 gas_assert (i.tm.operands == 1);
8546
8547 return !(i.prefix[REX_PREFIX] & REX_W)
8548 && (code16 ? i.tm.operand_types[0].bitfield.disp32
8549 || i.tm.operand_types[0].bitfield.disp32s
8550 : i.tm.operand_types[0].bitfield.disp16)
8551 ? CODE16 : 0;
8552}
8553
29b0f896 8554static void
e3bb37b5 8555output_branch (void)
29b0f896
AM
8556{
8557 char *p;
f8a5c266 8558 int size;
29b0f896
AM
8559 int code16;
8560 int prefix;
8561 relax_substateT subtype;
8562 symbolS *sym;
8563 offsetT off;
8564
48ef937e
JB
8565 if (now_seg == absolute_section)
8566 {
8567 as_bad (_("relaxable branches not supported in absolute section"));
8568 return;
8569 }
8570
f8a5c266 8571 code16 = flag_code == CODE_16BIT ? CODE16 : 0;
a501d77e 8572 size = i.disp_encoding == disp_encoding_32bit ? BIG : SMALL;
29b0f896
AM
8573
8574 prefix = 0;
8575 if (i.prefix[DATA_PREFIX] != 0)
252b5132 8576 {
29b0f896
AM
8577 prefix = 1;
8578 i.prefixes -= 1;
376cd056 8579 code16 ^= flip_code16(code16);
252b5132 8580 }
29b0f896
AM
8581 /* Pentium4 branch hints. */
8582 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8583 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
2f66722d 8584 {
29b0f896
AM
8585 prefix++;
8586 i.prefixes--;
8587 }
8588 if (i.prefix[REX_PREFIX] != 0)
8589 {
8590 prefix++;
8591 i.prefixes--;
2f66722d
AM
8592 }
8593
7e8b059b
L
8594 /* BND prefixed jump. */
8595 if (i.prefix[BND_PREFIX] != 0)
8596 {
6cb0a70e
JB
8597 prefix++;
8598 i.prefixes--;
7e8b059b
L
8599 }
8600
f2810fe0
JB
8601 if (i.prefixes != 0)
8602 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
29b0f896
AM
8603
8604 /* It's always a symbol; End frag & setup for relax.
8605 Make sure there is enough room in this frag for the largest
8606 instruction we may generate in md_convert_frag. This is 2
8607 bytes for the opcode and room for the prefix and largest
8608 displacement. */
8609 frag_grow (prefix + 2 + 4);
8610 /* Prefix and 1 opcode byte go in fr_fix. */
8611 p = frag_more (prefix + 1);
8612 if (i.prefix[DATA_PREFIX] != 0)
8613 *p++ = DATA_PREFIX_OPCODE;
8614 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
8615 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
8616 *p++ = i.prefix[SEG_PREFIX];
6cb0a70e
JB
8617 if (i.prefix[BND_PREFIX] != 0)
8618 *p++ = BND_PREFIX_OPCODE;
29b0f896
AM
8619 if (i.prefix[REX_PREFIX] != 0)
8620 *p++ = i.prefix[REX_PREFIX];
8621 *p = i.tm.base_opcode;
8622
8623 if ((unsigned char) *p == JUMP_PC_RELATIVE)
f8a5c266 8624 subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, size);
40fb9820 8625 else if (cpu_arch_flags.bitfield.cpui386)
f8a5c266 8626 subtype = ENCODE_RELAX_STATE (COND_JUMP, size);
29b0f896 8627 else
f8a5c266 8628 subtype = ENCODE_RELAX_STATE (COND_JUMP86, size);
29b0f896 8629 subtype |= code16;
3e73aa7c 8630
29b0f896
AM
8631 sym = i.op[0].disps->X_add_symbol;
8632 off = i.op[0].disps->X_add_number;
3e73aa7c 8633
29b0f896
AM
8634 if (i.op[0].disps->X_op != O_constant
8635 && i.op[0].disps->X_op != O_symbol)
3e73aa7c 8636 {
29b0f896
AM
8637 /* Handle complex expressions. */
8638 sym = make_expr_symbol (i.op[0].disps);
8639 off = 0;
8640 }
3e73aa7c 8641
29b0f896
AM
8642 /* 1 possible extra opcode + 4 byte displacement go in var part.
8643 Pass reloc in fr_var. */
d258b828 8644 frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
29b0f896 8645}
3e73aa7c 8646
bd7ab16b
L
8647#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8648/* Return TRUE iff PLT32 relocation should be used for branching to
8649 symbol S. */
8650
5b7c81bd 8651static bool
bd7ab16b
L
8652need_plt32_p (symbolS *s)
8653{
8654 /* PLT32 relocation is ELF only. */
8655 if (!IS_ELF)
5b7c81bd 8656 return false;
bd7ab16b 8657
a5def729
RO
8658#ifdef TE_SOLARIS
8659 /* Don't emit PLT32 relocation on Solaris: neither native linker nor
8660 krtld support it. */
5b7c81bd 8661 return false;
a5def729
RO
8662#endif
8663
bd7ab16b
L
8664 /* Since there is no need to prepare for PLT branch on x86-64, we
8665 can generate R_X86_64_PLT32, instead of R_X86_64_PC32, which can
8666 be used as a marker for 32-bit PC-relative branches. */
8667 if (!object_64bit)
5b7c81bd 8668 return false;
bd7ab16b 8669
44365e88 8670 if (s == NULL)
5b7c81bd 8671 return false;
44365e88 8672
bd7ab16b
L
8673 /* Weak or undefined symbol need PLT32 relocation. */
8674 if (S_IS_WEAK (s) || !S_IS_DEFINED (s))
5b7c81bd 8675 return true;
bd7ab16b
L
8676
8677 /* Non-global symbol doesn't need PLT32 relocation. */
8678 if (! S_IS_EXTERNAL (s))
5b7c81bd 8679 return false;
bd7ab16b
L
8680
8681 /* Other global symbols need PLT32 relocation. NB: Symbol with
8682 non-default visibilities are treated as normal global symbol
8683 so that PLT32 relocation can be used as a marker for 32-bit
8684 PC-relative branches. It is useful for linker relaxation. */
5b7c81bd 8685 return true;
bd7ab16b
L
8686}
8687#endif
8688
29b0f896 8689static void
e3bb37b5 8690output_jump (void)
29b0f896
AM
8691{
8692 char *p;
8693 int size;
3e02c1cc 8694 fixS *fixP;
bd7ab16b 8695 bfd_reloc_code_real_type jump_reloc = i.reloc[0];
29b0f896 8696
0cfa3eb3 8697 if (i.tm.opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
8698 {
8699 /* This is a loop or jecxz type instruction. */
8700 size = 1;
8701 if (i.prefix[ADDR_PREFIX] != 0)
8702 {
48ef937e 8703 frag_opcode_byte (ADDR_PREFIX_OPCODE);
29b0f896
AM
8704 i.prefixes -= 1;
8705 }
8706 /* Pentium4 branch hints. */
8707 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8708 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
8709 {
48ef937e 8710 frag_opcode_byte (i.prefix[SEG_PREFIX]);
29b0f896 8711 i.prefixes--;
3e73aa7c
JH
8712 }
8713 }
29b0f896
AM
8714 else
8715 {
8716 int code16;
3e73aa7c 8717
29b0f896
AM
8718 code16 = 0;
8719 if (flag_code == CODE_16BIT)
8720 code16 = CODE16;
3e73aa7c 8721
29b0f896
AM
8722 if (i.prefix[DATA_PREFIX] != 0)
8723 {
48ef937e 8724 frag_opcode_byte (DATA_PREFIX_OPCODE);
29b0f896 8725 i.prefixes -= 1;
376cd056 8726 code16 ^= flip_code16(code16);
29b0f896 8727 }
252b5132 8728
29b0f896
AM
8729 size = 4;
8730 if (code16)
8731 size = 2;
8732 }
9fcc94b6 8733
6cb0a70e
JB
8734 /* BND prefixed jump. */
8735 if (i.prefix[BND_PREFIX] != 0)
29b0f896 8736 {
48ef937e 8737 frag_opcode_byte (i.prefix[BND_PREFIX]);
29b0f896
AM
8738 i.prefixes -= 1;
8739 }
252b5132 8740
6cb0a70e 8741 if (i.prefix[REX_PREFIX] != 0)
7e8b059b 8742 {
48ef937e 8743 frag_opcode_byte (i.prefix[REX_PREFIX]);
7e8b059b
L
8744 i.prefixes -= 1;
8745 }
8746
f2810fe0
JB
8747 if (i.prefixes != 0)
8748 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
e0890092 8749
48ef937e
JB
8750 if (now_seg == absolute_section)
8751 {
9a182d04 8752 abs_section_offset += i.opcode_length + size;
48ef937e
JB
8753 return;
8754 }
8755
9a182d04
JB
8756 p = frag_more (i.opcode_length + size);
8757 switch (i.opcode_length)
42164a71
L
8758 {
8759 case 2:
8760 *p++ = i.tm.base_opcode >> 8;
1a0670f3 8761 /* Fall through. */
42164a71
L
8762 case 1:
8763 *p++ = i.tm.base_opcode;
8764 break;
8765 default:
8766 abort ();
8767 }
e0890092 8768
bd7ab16b
L
8769#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8770 if (size == 4
8771 && jump_reloc == NO_RELOC
8772 && need_plt32_p (i.op[0].disps->X_add_symbol))
8773 jump_reloc = BFD_RELOC_X86_64_PLT32;
8774#endif
8775
8776 jump_reloc = reloc (size, 1, 1, jump_reloc);
8777
3e02c1cc 8778 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
bd7ab16b 8779 i.op[0].disps, 1, jump_reloc);
3e02c1cc
AM
8780
8781 /* All jumps handled here are signed, but don't use a signed limit
8782 check for 32 and 16 bit jumps as we want to allow wrap around at
8783 4G and 64k respectively. */
8784 if (size == 1)
8785 fixP->fx_signed = 1;
29b0f896 8786}
e0890092 8787
29b0f896 8788static void
e3bb37b5 8789output_interseg_jump (void)
29b0f896
AM
8790{
8791 char *p;
8792 int size;
8793 int prefix;
8794 int code16;
252b5132 8795
29b0f896
AM
8796 code16 = 0;
8797 if (flag_code == CODE_16BIT)
8798 code16 = CODE16;
a217f122 8799
29b0f896
AM
8800 prefix = 0;
8801 if (i.prefix[DATA_PREFIX] != 0)
8802 {
8803 prefix = 1;
8804 i.prefixes -= 1;
8805 code16 ^= CODE16;
8806 }
6cb0a70e
JB
8807
8808 gas_assert (!i.prefix[REX_PREFIX]);
252b5132 8809
29b0f896
AM
8810 size = 4;
8811 if (code16)
8812 size = 2;
252b5132 8813
f2810fe0
JB
8814 if (i.prefixes != 0)
8815 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
252b5132 8816
48ef937e
JB
8817 if (now_seg == absolute_section)
8818 {
8819 abs_section_offset += prefix + 1 + 2 + size;
8820 return;
8821 }
8822
29b0f896
AM
8823 /* 1 opcode; 2 segment; offset */
8824 p = frag_more (prefix + 1 + 2 + size);
3e73aa7c 8825
29b0f896
AM
8826 if (i.prefix[DATA_PREFIX] != 0)
8827 *p++ = DATA_PREFIX_OPCODE;
252b5132 8828
29b0f896
AM
8829 if (i.prefix[REX_PREFIX] != 0)
8830 *p++ = i.prefix[REX_PREFIX];
252b5132 8831
29b0f896
AM
8832 *p++ = i.tm.base_opcode;
8833 if (i.op[1].imms->X_op == O_constant)
8834 {
8835 offsetT n = i.op[1].imms->X_add_number;
252b5132 8836
29b0f896
AM
8837 if (size == 2
8838 && !fits_in_unsigned_word (n)
8839 && !fits_in_signed_word (n))
8840 {
8841 as_bad (_("16-bit jump out of range"));
8842 return;
8843 }
8844 md_number_to_chars (p, n, size);
8845 }
8846 else
8847 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
d258b828 8848 i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
6d96a594
C
8849
8850 p += size;
8851 if (i.op[0].imms->X_op == O_constant)
8852 md_number_to_chars (p, (valueT) i.op[0].imms->X_add_number, 2);
8853 else
8854 fix_new_exp (frag_now, p - frag_now->fr_literal, 2,
8855 i.op[0].imms, 0, reloc (2, 0, 0, i.reloc[0]));
29b0f896 8856}
a217f122 8857
b4a3a7b4
L
8858#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8859void
8860x86_cleanup (void)
8861{
8862 char *p;
8863 asection *seg = now_seg;
8864 subsegT subseg = now_subseg;
8865 asection *sec;
8866 unsigned int alignment, align_size_1;
8867 unsigned int isa_1_descsz, feature_2_descsz, descsz;
8868 unsigned int isa_1_descsz_raw, feature_2_descsz_raw;
8869 unsigned int padding;
8870
8871 if (!IS_ELF || !x86_used_note)
8872 return;
8873
b4a3a7b4
L
8874 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X86;
8875
8876 /* The .note.gnu.property section layout:
8877
8878 Field Length Contents
8879 ---- ---- ----
8880 n_namsz 4 4
8881 n_descsz 4 The note descriptor size
8882 n_type 4 NT_GNU_PROPERTY_TYPE_0
8883 n_name 4 "GNU"
8884 n_desc n_descsz The program property array
8885 .... .... ....
8886 */
8887
8888 /* Create the .note.gnu.property section. */
8889 sec = subseg_new (NOTE_GNU_PROPERTY_SECTION_NAME, 0);
fd361982 8890 bfd_set_section_flags (sec,
b4a3a7b4
L
8891 (SEC_ALLOC
8892 | SEC_LOAD
8893 | SEC_DATA
8894 | SEC_HAS_CONTENTS
8895 | SEC_READONLY));
8896
8897 if (get_elf_backend_data (stdoutput)->s->elfclass == ELFCLASS64)
8898 {
8899 align_size_1 = 7;
8900 alignment = 3;
8901 }
8902 else
8903 {
8904 align_size_1 = 3;
8905 alignment = 2;
8906 }
8907
fd361982 8908 bfd_set_section_alignment (sec, alignment);
b4a3a7b4
L
8909 elf_section_type (sec) = SHT_NOTE;
8910
8911 /* GNU_PROPERTY_X86_ISA_1_USED: 4-byte type + 4-byte data size
8912 + 4-byte data */
8913 isa_1_descsz_raw = 4 + 4 + 4;
8914 /* Align GNU_PROPERTY_X86_ISA_1_USED. */
8915 isa_1_descsz = (isa_1_descsz_raw + align_size_1) & ~align_size_1;
8916
8917 feature_2_descsz_raw = isa_1_descsz;
8918 /* GNU_PROPERTY_X86_FEATURE_2_USED: 4-byte type + 4-byte data size
8919 + 4-byte data */
8920 feature_2_descsz_raw += 4 + 4 + 4;
8921 /* Align GNU_PROPERTY_X86_FEATURE_2_USED. */
8922 feature_2_descsz = ((feature_2_descsz_raw + align_size_1)
8923 & ~align_size_1);
8924
8925 descsz = feature_2_descsz;
8926 /* Section size: n_namsz + n_descsz + n_type + n_name + n_descsz. */
8927 p = frag_more (4 + 4 + 4 + 4 + descsz);
8928
8929 /* Write n_namsz. */
8930 md_number_to_chars (p, (valueT) 4, 4);
8931
8932 /* Write n_descsz. */
8933 md_number_to_chars (p + 4, (valueT) descsz, 4);
8934
8935 /* Write n_type. */
8936 md_number_to_chars (p + 4 * 2, (valueT) NT_GNU_PROPERTY_TYPE_0, 4);
8937
8938 /* Write n_name. */
8939 memcpy (p + 4 * 3, "GNU", 4);
8940
8941 /* Write 4-byte type. */
8942 md_number_to_chars (p + 4 * 4,
8943 (valueT) GNU_PROPERTY_X86_ISA_1_USED, 4);
8944
8945 /* Write 4-byte data size. */
8946 md_number_to_chars (p + 4 * 5, (valueT) 4, 4);
8947
8948 /* Write 4-byte data. */
8949 md_number_to_chars (p + 4 * 6, (valueT) x86_isa_1_used, 4);
8950
8951 /* Zero out paddings. */
8952 padding = isa_1_descsz - isa_1_descsz_raw;
8953 if (padding)
8954 memset (p + 4 * 7, 0, padding);
8955
8956 /* Write 4-byte type. */
8957 md_number_to_chars (p + isa_1_descsz + 4 * 4,
8958 (valueT) GNU_PROPERTY_X86_FEATURE_2_USED, 4);
8959
8960 /* Write 4-byte data size. */
8961 md_number_to_chars (p + isa_1_descsz + 4 * 5, (valueT) 4, 4);
8962
8963 /* Write 4-byte data. */
8964 md_number_to_chars (p + isa_1_descsz + 4 * 6,
8965 (valueT) x86_feature_2_used, 4);
8966
8967 /* Zero out paddings. */
8968 padding = feature_2_descsz - feature_2_descsz_raw;
8969 if (padding)
8970 memset (p + isa_1_descsz + 4 * 7, 0, padding);
8971
8972 /* We probably can't restore the current segment, for there likely
8973 isn't one yet... */
8974 if (seg && subseg)
8975 subseg_set (seg, subseg);
8976}
8977#endif
8978
9c33702b
JB
8979static unsigned int
8980encoding_length (const fragS *start_frag, offsetT start_off,
8981 const char *frag_now_ptr)
8982{
8983 unsigned int len = 0;
8984
8985 if (start_frag != frag_now)
8986 {
8987 const fragS *fr = start_frag;
8988
8989 do {
8990 len += fr->fr_fix;
8991 fr = fr->fr_next;
8992 } while (fr && fr != frag_now);
8993 }
8994
8995 return len - start_off + (frag_now_ptr - frag_now->fr_literal);
8996}
8997
e379e5f3 8998/* Return 1 for test, and, cmp, add, sub, inc and dec which may
79d72f45
HL
8999 be macro-fused with conditional jumps.
9000 NB: If TEST/AND/CMP/ADD/SUB/INC/DEC is of RIP relative address,
9001 or is one of the following format:
9002
9003 cmp m, imm
9004 add m, imm
9005 sub m, imm
9006 test m, imm
9007 and m, imm
9008 inc m
9009 dec m
9010
9011 it is unfusible. */
e379e5f3
L
9012
9013static int
79d72f45 9014maybe_fused_with_jcc_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
9015{
9016 /* No RIP address. */
9017 if (i.base_reg && i.base_reg->reg_num == RegIP)
9018 return 0;
9019
389d00a5
JB
9020 /* No opcodes outside of base encoding space. */
9021 if (i.tm.opcode_modifier.opcodespace != SPACE_BASE)
e379e5f3
L
9022 return 0;
9023
79d72f45
HL
9024 /* add, sub without add/sub m, imm. */
9025 if (i.tm.base_opcode <= 5
e379e5f3
L
9026 || (i.tm.base_opcode >= 0x28 && i.tm.base_opcode <= 0x2d)
9027 || ((i.tm.base_opcode | 3) == 0x83
79d72f45 9028 && (i.tm.extension_opcode == 0x5
e379e5f3 9029 || i.tm.extension_opcode == 0x0)))
79d72f45
HL
9030 {
9031 *mf_cmp_p = mf_cmp_alu_cmp;
9032 return !(i.mem_operands && i.imm_operands);
9033 }
e379e5f3 9034
79d72f45
HL
9035 /* and without and m, imm. */
9036 if ((i.tm.base_opcode >= 0x20 && i.tm.base_opcode <= 0x25)
9037 || ((i.tm.base_opcode | 3) == 0x83
9038 && i.tm.extension_opcode == 0x4))
9039 {
9040 *mf_cmp_p = mf_cmp_test_and;
9041 return !(i.mem_operands && i.imm_operands);
9042 }
9043
9044 /* test without test m imm. */
e379e5f3
L
9045 if ((i.tm.base_opcode | 1) == 0x85
9046 || (i.tm.base_opcode | 1) == 0xa9
9047 || ((i.tm.base_opcode | 1) == 0xf7
79d72f45
HL
9048 && i.tm.extension_opcode == 0))
9049 {
9050 *mf_cmp_p = mf_cmp_test_and;
9051 return !(i.mem_operands && i.imm_operands);
9052 }
9053
9054 /* cmp without cmp m, imm. */
9055 if ((i.tm.base_opcode >= 0x38 && i.tm.base_opcode <= 0x3d)
e379e5f3
L
9056 || ((i.tm.base_opcode | 3) == 0x83
9057 && (i.tm.extension_opcode == 0x7)))
79d72f45
HL
9058 {
9059 *mf_cmp_p = mf_cmp_alu_cmp;
9060 return !(i.mem_operands && i.imm_operands);
9061 }
e379e5f3 9062
79d72f45 9063 /* inc, dec without inc/dec m. */
e379e5f3
L
9064 if ((i.tm.cpu_flags.bitfield.cpuno64
9065 && (i.tm.base_opcode | 0xf) == 0x4f)
9066 || ((i.tm.base_opcode | 1) == 0xff
9067 && i.tm.extension_opcode <= 0x1))
79d72f45
HL
9068 {
9069 *mf_cmp_p = mf_cmp_incdec;
9070 return !i.mem_operands;
9071 }
e379e5f3
L
9072
9073 return 0;
9074}
9075
9076/* Return 1 if a FUSED_JCC_PADDING frag should be generated. */
9077
9078static int
79d72f45 9079add_fused_jcc_padding_frag_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
9080{
9081 /* NB: Don't work with COND_JUMP86 without i386. */
9082 if (!align_branch_power
9083 || now_seg == absolute_section
9084 || !cpu_arch_flags.bitfield.cpui386
9085 || !(align_branch & align_branch_fused_bit))
9086 return 0;
9087
79d72f45 9088 if (maybe_fused_with_jcc_p (mf_cmp_p))
e379e5f3
L
9089 {
9090 if (last_insn.kind == last_insn_other
9091 || last_insn.seg != now_seg)
9092 return 1;
9093 if (flag_debug)
9094 as_warn_where (last_insn.file, last_insn.line,
9095 _("`%s` skips -malign-branch-boundary on `%s`"),
9096 last_insn.name, i.tm.name);
9097 }
9098
9099 return 0;
9100}
9101
9102/* Return 1 if a BRANCH_PREFIX frag should be generated. */
9103
9104static int
9105add_branch_prefix_frag_p (void)
9106{
9107 /* NB: Don't work with COND_JUMP86 without i386. Don't add prefix
9108 to PadLock instructions since they include prefixes in opcode. */
9109 if (!align_branch_power
9110 || !align_branch_prefix_size
9111 || now_seg == absolute_section
9112 || i.tm.cpu_flags.bitfield.cpupadlock
9113 || !cpu_arch_flags.bitfield.cpui386)
9114 return 0;
9115
9116 /* Don't add prefix if it is a prefix or there is no operand in case
9117 that segment prefix is special. */
9118 if (!i.operands || i.tm.opcode_modifier.isprefix)
9119 return 0;
9120
9121 if (last_insn.kind == last_insn_other
9122 || last_insn.seg != now_seg)
9123 return 1;
9124
9125 if (flag_debug)
9126 as_warn_where (last_insn.file, last_insn.line,
9127 _("`%s` skips -malign-branch-boundary on `%s`"),
9128 last_insn.name, i.tm.name);
9129
9130 return 0;
9131}
9132
9133/* Return 1 if a BRANCH_PADDING frag should be generated. */
9134
9135static int
79d72f45
HL
9136add_branch_padding_frag_p (enum align_branch_kind *branch_p,
9137 enum mf_jcc_kind *mf_jcc_p)
e379e5f3
L
9138{
9139 int add_padding;
9140
9141 /* NB: Don't work with COND_JUMP86 without i386. */
9142 if (!align_branch_power
9143 || now_seg == absolute_section
389d00a5
JB
9144 || !cpu_arch_flags.bitfield.cpui386
9145 || i.tm.opcode_modifier.opcodespace != SPACE_BASE)
e379e5f3
L
9146 return 0;
9147
9148 add_padding = 0;
9149
9150 /* Check for jcc and direct jmp. */
9151 if (i.tm.opcode_modifier.jump == JUMP)
9152 {
9153 if (i.tm.base_opcode == JUMP_PC_RELATIVE)
9154 {
9155 *branch_p = align_branch_jmp;
9156 add_padding = align_branch & align_branch_jmp_bit;
9157 }
9158 else
9159 {
79d72f45
HL
9160 /* Because J<cc> and JN<cc> share same group in macro-fusible table,
9161 igore the lowest bit. */
9162 *mf_jcc_p = (i.tm.base_opcode & 0x0e) >> 1;
e379e5f3
L
9163 *branch_p = align_branch_jcc;
9164 if ((align_branch & align_branch_jcc_bit))
9165 add_padding = 1;
9166 }
9167 }
e379e5f3
L
9168 else if ((i.tm.base_opcode | 1) == 0xc3)
9169 {
9170 /* Near ret. */
9171 *branch_p = align_branch_ret;
9172 if ((align_branch & align_branch_ret_bit))
9173 add_padding = 1;
9174 }
9175 else
9176 {
9177 /* Check for indirect jmp, direct and indirect calls. */
9178 if (i.tm.base_opcode == 0xe8)
9179 {
9180 /* Direct call. */
9181 *branch_p = align_branch_call;
9182 if ((align_branch & align_branch_call_bit))
9183 add_padding = 1;
9184 }
9185 else if (i.tm.base_opcode == 0xff
9186 && (i.tm.extension_opcode == 2
9187 || i.tm.extension_opcode == 4))
9188 {
9189 /* Indirect call and jmp. */
9190 *branch_p = align_branch_indirect;
9191 if ((align_branch & align_branch_indirect_bit))
9192 add_padding = 1;
9193 }
9194
9195 if (add_padding
9196 && i.disp_operands
9197 && tls_get_addr
9198 && (i.op[0].disps->X_op == O_symbol
9199 || (i.op[0].disps->X_op == O_subtract
9200 && i.op[0].disps->X_op_symbol == GOT_symbol)))
9201 {
9202 symbolS *s = i.op[0].disps->X_add_symbol;
9203 /* No padding to call to global or undefined tls_get_addr. */
9204 if ((S_IS_EXTERNAL (s) || !S_IS_DEFINED (s))
9205 && strcmp (S_GET_NAME (s), tls_get_addr) == 0)
9206 return 0;
9207 }
9208 }
9209
9210 if (add_padding
9211 && last_insn.kind != last_insn_other
9212 && last_insn.seg == now_seg)
9213 {
9214 if (flag_debug)
9215 as_warn_where (last_insn.file, last_insn.line,
9216 _("`%s` skips -malign-branch-boundary on `%s`"),
9217 last_insn.name, i.tm.name);
9218 return 0;
9219 }
9220
9221 return add_padding;
9222}
9223
29b0f896 9224static void
e3bb37b5 9225output_insn (void)
29b0f896 9226{
2bbd9c25
JJ
9227 fragS *insn_start_frag;
9228 offsetT insn_start_off;
e379e5f3
L
9229 fragS *fragP = NULL;
9230 enum align_branch_kind branch = align_branch_none;
79d72f45
HL
9231 /* The initializer is arbitrary just to avoid uninitialized error.
9232 it's actually either assigned in add_branch_padding_frag_p
9233 or never be used. */
9234 enum mf_jcc_kind mf_jcc = mf_jcc_jo;
2bbd9c25 9235
b4a3a7b4 9236#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
48ef937e 9237 if (IS_ELF && x86_used_note && now_seg != absolute_section)
b4a3a7b4 9238 {
32930e4e
L
9239 if ((i.xstate & xstate_tmm) == xstate_tmm
9240 || i.tm.cpu_flags.bitfield.cpuamx_tile)
9241 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_TMM;
9242
b4a3a7b4
L
9243 if (i.tm.cpu_flags.bitfield.cpu8087
9244 || i.tm.cpu_flags.bitfield.cpu287
9245 || i.tm.cpu_flags.bitfield.cpu387
9246 || i.tm.cpu_flags.bitfield.cpu687
9247 || i.tm.cpu_flags.bitfield.cpufisttp)
9248 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X87;
014d61ea 9249
921eafea 9250 if ((i.xstate & xstate_mmx)
389d00a5
JB
9251 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
9252 && !is_any_vex_encoding (&i.tm)
9253 && (i.tm.base_opcode == 0x77 /* emms */
9254 || i.tm.base_opcode == 0x0e /* femms */)))
b4a3a7b4 9255 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MMX;
014d61ea 9256
32930e4e
L
9257 if (i.index_reg)
9258 {
9259 if (i.index_reg->reg_type.bitfield.zmmword)
9260 i.xstate |= xstate_zmm;
9261 else if (i.index_reg->reg_type.bitfield.ymmword)
9262 i.xstate |= xstate_ymm;
9263 else if (i.index_reg->reg_type.bitfield.xmmword)
9264 i.xstate |= xstate_xmm;
9265 }
014d61ea
JB
9266
9267 /* vzeroall / vzeroupper */
9268 if (i.tm.base_opcode == 0x77 && i.tm.cpu_flags.bitfield.cpuavx)
9269 i.xstate |= xstate_ymm;
9270
c4694f17 9271 if ((i.xstate & xstate_xmm)
389d00a5
JB
9272 /* ldmxcsr / stmxcsr / vldmxcsr / vstmxcsr */
9273 || (i.tm.base_opcode == 0xae
9274 && (i.tm.cpu_flags.bitfield.cpusse
9275 || i.tm.cpu_flags.bitfield.cpuavx))
c4694f17
TG
9276 || i.tm.cpu_flags.bitfield.cpuwidekl
9277 || i.tm.cpu_flags.bitfield.cpukl)
b4a3a7b4 9278 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XMM;
014d61ea 9279
921eafea 9280 if ((i.xstate & xstate_ymm) == xstate_ymm)
b4a3a7b4 9281 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_YMM;
921eafea 9282 if ((i.xstate & xstate_zmm) == xstate_zmm)
b4a3a7b4 9283 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_ZMM;
6225c532 9284 if (i.mask.reg || (i.xstate & xstate_mask) == xstate_mask)
32930e4e 9285 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MASK;
b4a3a7b4
L
9286 if (i.tm.cpu_flags.bitfield.cpufxsr)
9287 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_FXSR;
9288 if (i.tm.cpu_flags.bitfield.cpuxsave)
9289 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVE;
9290 if (i.tm.cpu_flags.bitfield.cpuxsaveopt)
9291 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT;
9292 if (i.tm.cpu_flags.bitfield.cpuxsavec)
9293 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEC;
b0ab0693
L
9294
9295 if (x86_feature_2_used
9296 || i.tm.cpu_flags.bitfield.cpucmov
9297 || i.tm.cpu_flags.bitfield.cpusyscall
389d00a5
JB
9298 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
9299 && i.tm.base_opcode == 0xc7
70e95837 9300 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
b0ab0693
L
9301 && i.tm.extension_opcode == 1) /* cmpxchg8b */)
9302 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_BASELINE;
9303 if (i.tm.cpu_flags.bitfield.cpusse3
9304 || i.tm.cpu_flags.bitfield.cpussse3
9305 || i.tm.cpu_flags.bitfield.cpusse4_1
9306 || i.tm.cpu_flags.bitfield.cpusse4_2
9307 || i.tm.cpu_flags.bitfield.cpucx16
9308 || i.tm.cpu_flags.bitfield.cpupopcnt
9309 /* LAHF-SAHF insns in 64-bit mode. */
9310 || (flag_code == CODE_64BIT
35648716
JB
9311 && (i.tm.base_opcode | 1) == 0x9f
9312 && i.tm.opcode_modifier.opcodespace == SPACE_BASE))
b0ab0693
L
9313 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V2;
9314 if (i.tm.cpu_flags.bitfield.cpuavx
9315 || i.tm.cpu_flags.bitfield.cpuavx2
9316 /* Any VEX encoded insns execpt for CpuAVX512F, CpuAVX512BW,
9317 CpuAVX512DQ, LPW, TBM and AMX. */
9318 || (i.tm.opcode_modifier.vex
9319 && !i.tm.cpu_flags.bitfield.cpuavx512f
9320 && !i.tm.cpu_flags.bitfield.cpuavx512bw
9321 && !i.tm.cpu_flags.bitfield.cpuavx512dq
9322 && !i.tm.cpu_flags.bitfield.cpulwp
9323 && !i.tm.cpu_flags.bitfield.cputbm
9324 && !(x86_feature_2_used & GNU_PROPERTY_X86_FEATURE_2_TMM))
9325 || i.tm.cpu_flags.bitfield.cpuf16c
9326 || i.tm.cpu_flags.bitfield.cpufma
9327 || i.tm.cpu_flags.bitfield.cpulzcnt
9328 || i.tm.cpu_flags.bitfield.cpumovbe
9329 || i.tm.cpu_flags.bitfield.cpuxsaves
9330 || (x86_feature_2_used
9331 & (GNU_PROPERTY_X86_FEATURE_2_XSAVE
9332 | GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT
9333 | GNU_PROPERTY_X86_FEATURE_2_XSAVEC)) != 0)
9334 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V3;
9335 if (i.tm.cpu_flags.bitfield.cpuavx512f
9336 || i.tm.cpu_flags.bitfield.cpuavx512bw
9337 || i.tm.cpu_flags.bitfield.cpuavx512dq
9338 || i.tm.cpu_flags.bitfield.cpuavx512vl
9339 /* Any EVEX encoded insns except for AVX512ER, AVX512PF and
9340 VNNIW. */
9341 || (i.tm.opcode_modifier.evex
9342 && !i.tm.cpu_flags.bitfield.cpuavx512er
9343 && !i.tm.cpu_flags.bitfield.cpuavx512pf
9344 && !i.tm.cpu_flags.bitfield.cpuavx512_4vnniw))
9345 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V4;
b4a3a7b4
L
9346 }
9347#endif
9348
29b0f896
AM
9349 /* Tie dwarf2 debug info to the address at the start of the insn.
9350 We can't do this after the insn has been output as the current
9351 frag may have been closed off. eg. by frag_var. */
9352 dwarf2_emit_insn (0);
9353
2bbd9c25
JJ
9354 insn_start_frag = frag_now;
9355 insn_start_off = frag_now_fix ();
9356
79d72f45 9357 if (add_branch_padding_frag_p (&branch, &mf_jcc))
e379e5f3
L
9358 {
9359 char *p;
9360 /* Branch can be 8 bytes. Leave some room for prefixes. */
9361 unsigned int max_branch_padding_size = 14;
9362
9363 /* Align section to boundary. */
9364 record_alignment (now_seg, align_branch_power);
9365
9366 /* Make room for padding. */
9367 frag_grow (max_branch_padding_size);
9368
9369 /* Start of the padding. */
9370 p = frag_more (0);
9371
9372 fragP = frag_now;
9373
9374 frag_var (rs_machine_dependent, max_branch_padding_size, 0,
9375 ENCODE_RELAX_STATE (BRANCH_PADDING, 0),
9376 NULL, 0, p);
9377
79d72f45 9378 fragP->tc_frag_data.mf_type = mf_jcc;
e379e5f3
L
9379 fragP->tc_frag_data.branch_type = branch;
9380 fragP->tc_frag_data.max_bytes = max_branch_padding_size;
9381 }
9382
29b0f896 9383 /* Output jumps. */
0cfa3eb3 9384 if (i.tm.opcode_modifier.jump == JUMP)
29b0f896 9385 output_branch ();
0cfa3eb3
JB
9386 else if (i.tm.opcode_modifier.jump == JUMP_BYTE
9387 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896 9388 output_jump ();
0cfa3eb3 9389 else if (i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT)
29b0f896
AM
9390 output_interseg_jump ();
9391 else
9392 {
9393 /* Output normal instructions here. */
9394 char *p;
9395 unsigned char *q;
47465058 9396 unsigned int j;
79d72f45 9397 enum mf_cmp_kind mf_cmp;
4dffcebc 9398
e4e00185 9399 if (avoid_fence
389d00a5
JB
9400 && (i.tm.base_opcode == 0xaee8
9401 || i.tm.base_opcode == 0xaef0
9402 || i.tm.base_opcode == 0xaef8))
48ef937e
JB
9403 {
9404 /* Encode lfence, mfence, and sfence as
9405 f0 83 04 24 00 lock addl $0x0, (%{re}sp). */
9406 if (now_seg != absolute_section)
9407 {
9408 offsetT val = 0x240483f0ULL;
9409
9410 p = frag_more (5);
9411 md_number_to_chars (p, val, 5);
9412 }
9413 else
9414 abs_section_offset += 5;
9415 return;
9416 }
e4e00185 9417
d022bddd
IT
9418 /* Some processors fail on LOCK prefix. This options makes
9419 assembler ignore LOCK prefix and serves as a workaround. */
9420 if (omit_lock_prefix)
9421 {
35648716
JB
9422 if (i.tm.base_opcode == LOCK_PREFIX_OPCODE
9423 && i.tm.opcode_modifier.isprefix)
d022bddd
IT
9424 return;
9425 i.prefix[LOCK_PREFIX] = 0;
9426 }
9427
e379e5f3
L
9428 if (branch)
9429 /* Skip if this is a branch. */
9430 ;
79d72f45 9431 else if (add_fused_jcc_padding_frag_p (&mf_cmp))
e379e5f3
L
9432 {
9433 /* Make room for padding. */
9434 frag_grow (MAX_FUSED_JCC_PADDING_SIZE);
9435 p = frag_more (0);
9436
9437 fragP = frag_now;
9438
9439 frag_var (rs_machine_dependent, MAX_FUSED_JCC_PADDING_SIZE, 0,
9440 ENCODE_RELAX_STATE (FUSED_JCC_PADDING, 0),
9441 NULL, 0, p);
9442
79d72f45 9443 fragP->tc_frag_data.mf_type = mf_cmp;
e379e5f3
L
9444 fragP->tc_frag_data.branch_type = align_branch_fused;
9445 fragP->tc_frag_data.max_bytes = MAX_FUSED_JCC_PADDING_SIZE;
9446 }
9447 else if (add_branch_prefix_frag_p ())
9448 {
9449 unsigned int max_prefix_size = align_branch_prefix_size;
9450
9451 /* Make room for padding. */
9452 frag_grow (max_prefix_size);
9453 p = frag_more (0);
9454
9455 fragP = frag_now;
9456
9457 frag_var (rs_machine_dependent, max_prefix_size, 0,
9458 ENCODE_RELAX_STATE (BRANCH_PREFIX, 0),
9459 NULL, 0, p);
9460
9461 fragP->tc_frag_data.max_bytes = max_prefix_size;
9462 }
9463
43234a1e
L
9464 /* Since the VEX/EVEX prefix contains the implicit prefix, we
9465 don't need the explicit prefix. */
9466 if (!i.tm.opcode_modifier.vex && !i.tm.opcode_modifier.evex)
bc4bd9ab 9467 {
7b47a312 9468 switch (i.tm.opcode_modifier.opcodeprefix)
bc4bd9ab 9469 {
7b47a312
L
9470 case PREFIX_0X66:
9471 add_prefix (0x66);
9472 break;
9473 case PREFIX_0XF2:
9474 add_prefix (0xf2);
9475 break;
9476 case PREFIX_0XF3:
8b65b895
L
9477 if (!i.tm.cpu_flags.bitfield.cpupadlock
9478 || (i.prefix[REP_PREFIX] != 0xf3))
9479 add_prefix (0xf3);
c0f3af97 9480 break;
7b47a312 9481 case PREFIX_NONE:
9a182d04 9482 switch (i.opcode_length)
c0f3af97 9483 {
7b47a312 9484 case 2:
7b47a312 9485 break;
9a182d04 9486 case 1:
7b47a312 9487 /* Check for pseudo prefixes. */
9a182d04
JB
9488 if (!i.tm.opcode_modifier.isprefix || i.tm.base_opcode)
9489 break;
7b47a312
L
9490 as_bad_where (insn_start_frag->fr_file,
9491 insn_start_frag->fr_line,
9492 _("pseudo prefix without instruction"));
9493 return;
9494 default:
9495 abort ();
4dffcebc 9496 }
c0f3af97 9497 break;
c0f3af97
L
9498 default:
9499 abort ();
bc4bd9ab 9500 }
c0f3af97 9501
6d19a37a 9502#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
cf61b747
L
9503 /* For x32, add a dummy REX_OPCODE prefix for mov/add with
9504 R_X86_64_GOTTPOFF relocation so that linker can safely
14470f07
L
9505 perform IE->LE optimization. A dummy REX_OPCODE prefix
9506 is also needed for lea with R_X86_64_GOTPC32_TLSDESC
9507 relocation for GDesc -> IE/LE optimization. */
cf61b747
L
9508 if (x86_elf_abi == X86_64_X32_ABI
9509 && i.operands == 2
14470f07
L
9510 && (i.reloc[0] == BFD_RELOC_X86_64_GOTTPOFF
9511 || i.reloc[0] == BFD_RELOC_X86_64_GOTPC32_TLSDESC)
cf61b747
L
9512 && i.prefix[REX_PREFIX] == 0)
9513 add_prefix (REX_OPCODE);
6d19a37a 9514#endif
cf61b747 9515
c0f3af97
L
9516 /* The prefix bytes. */
9517 for (j = ARRAY_SIZE (i.prefix), q = i.prefix; j > 0; j--, q++)
9518 if (*q)
48ef937e 9519 frag_opcode_byte (*q);
0f10071e 9520 }
ae5c1c7b 9521 else
c0f3af97
L
9522 {
9523 for (j = 0, q = i.prefix; j < ARRAY_SIZE (i.prefix); j++, q++)
9524 if (*q)
9525 switch (j)
9526 {
c0f3af97
L
9527 case SEG_PREFIX:
9528 case ADDR_PREFIX:
48ef937e 9529 frag_opcode_byte (*q);
c0f3af97
L
9530 break;
9531 default:
9532 /* There should be no other prefixes for instructions
9533 with VEX prefix. */
9534 abort ();
9535 }
9536
43234a1e
L
9537 /* For EVEX instructions i.vrex should become 0 after
9538 build_evex_prefix. For VEX instructions upper 16 registers
9539 aren't available, so VREX should be 0. */
9540 if (i.vrex)
9541 abort ();
c0f3af97 9542 /* Now the VEX prefix. */
48ef937e
JB
9543 if (now_seg != absolute_section)
9544 {
9545 p = frag_more (i.vex.length);
9546 for (j = 0; j < i.vex.length; j++)
9547 p[j] = i.vex.bytes[j];
9548 }
9549 else
9550 abs_section_offset += i.vex.length;
c0f3af97 9551 }
252b5132 9552
29b0f896 9553 /* Now the opcode; be careful about word order here! */
389d00a5
JB
9554 j = i.opcode_length;
9555 if (!i.vex.length)
9556 switch (i.tm.opcode_modifier.opcodespace)
9557 {
9558 case SPACE_BASE:
9559 break;
9560 case SPACE_0F:
9561 ++j;
9562 break;
9563 case SPACE_0F38:
9564 case SPACE_0F3A:
9565 j += 2;
9566 break;
9567 default:
9568 abort ();
9569 }
9570
48ef937e 9571 if (now_seg == absolute_section)
389d00a5
JB
9572 abs_section_offset += j;
9573 else if (j == 1)
29b0f896
AM
9574 {
9575 FRAG_APPEND_1_CHAR (i.tm.base_opcode);
9576 }
9577 else
9578 {
389d00a5
JB
9579 p = frag_more (j);
9580 if (!i.vex.length
9581 && i.tm.opcode_modifier.opcodespace != SPACE_BASE)
9582 {
9583 *p++ = 0x0f;
9584 if (i.tm.opcode_modifier.opcodespace != SPACE_0F)
9585 *p++ = i.tm.opcode_modifier.opcodespace == SPACE_0F38
9586 ? 0x38 : 0x3a;
9587 }
9588
9a182d04 9589 switch (i.opcode_length)
331d2d0d 9590 {
4dffcebc 9591 case 2:
389d00a5
JB
9592 /* Put out high byte first: can't use md_number_to_chars! */
9593 *p++ = (i.tm.base_opcode >> 8) & 0xff;
9594 /* Fall through. */
9595 case 1:
9596 *p = i.tm.base_opcode & 0xff;
4dffcebc
L
9597 break;
9598 default:
9599 abort ();
9600 break;
331d2d0d 9601 }
0f10071e 9602
29b0f896 9603 }
3e73aa7c 9604
29b0f896 9605 /* Now the modrm byte and sib byte (if present). */
40fb9820 9606 if (i.tm.opcode_modifier.modrm)
29b0f896 9607 {
48ef937e
JB
9608 frag_opcode_byte ((i.rm.regmem << 0)
9609 | (i.rm.reg << 3)
9610 | (i.rm.mode << 6));
29b0f896
AM
9611 /* If i.rm.regmem == ESP (4)
9612 && i.rm.mode != (Register mode)
9613 && not 16 bit
9614 ==> need second modrm byte. */
9615 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
9616 && i.rm.mode != 3
dc821c5f 9617 && !(i.base_reg && i.base_reg->reg_type.bitfield.word))
48ef937e
JB
9618 frag_opcode_byte ((i.sib.base << 0)
9619 | (i.sib.index << 3)
9620 | (i.sib.scale << 6));
29b0f896 9621 }
3e73aa7c 9622
29b0f896 9623 if (i.disp_operands)
2bbd9c25 9624 output_disp (insn_start_frag, insn_start_off);
3e73aa7c 9625
29b0f896 9626 if (i.imm_operands)
2bbd9c25 9627 output_imm (insn_start_frag, insn_start_off);
9c33702b
JB
9628
9629 /*
9630 * frag_now_fix () returning plain abs_section_offset when we're in the
9631 * absolute section, and abs_section_offset not getting updated as data
9632 * gets added to the frag breaks the logic below.
9633 */
9634 if (now_seg != absolute_section)
9635 {
9636 j = encoding_length (insn_start_frag, insn_start_off, frag_more (0));
9637 if (j > 15)
9638 as_warn (_("instruction length of %u bytes exceeds the limit of 15"),
9639 j);
e379e5f3
L
9640 else if (fragP)
9641 {
9642 /* NB: Don't add prefix with GOTPC relocation since
9643 output_disp() above depends on the fixed encoding
9644 length. Can't add prefix with TLS relocation since
9645 it breaks TLS linker optimization. */
9646 unsigned int max = i.has_gotpc_tls_reloc ? 0 : 15 - j;
9647 /* Prefix count on the current instruction. */
9648 unsigned int count = i.vex.length;
9649 unsigned int k;
9650 for (k = 0; k < ARRAY_SIZE (i.prefix); k++)
9651 /* REX byte is encoded in VEX/EVEX prefix. */
9652 if (i.prefix[k] && (k != REX_PREFIX || !i.vex.length))
9653 count++;
9654
9655 /* Count prefixes for extended opcode maps. */
9656 if (!i.vex.length)
389d00a5 9657 switch (i.tm.opcode_modifier.opcodespace)
e379e5f3 9658 {
389d00a5 9659 case SPACE_BASE:
e379e5f3 9660 break;
389d00a5
JB
9661 case SPACE_0F:
9662 count++;
e379e5f3 9663 break;
389d00a5
JB
9664 case SPACE_0F38:
9665 case SPACE_0F3A:
9666 count += 2;
e379e5f3
L
9667 break;
9668 default:
9669 abort ();
9670 }
9671
9672 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
9673 == BRANCH_PREFIX)
9674 {
9675 /* Set the maximum prefix size in BRANCH_PREFIX
9676 frag. */
9677 if (fragP->tc_frag_data.max_bytes > max)
9678 fragP->tc_frag_data.max_bytes = max;
9679 if (fragP->tc_frag_data.max_bytes > count)
9680 fragP->tc_frag_data.max_bytes -= count;
9681 else
9682 fragP->tc_frag_data.max_bytes = 0;
9683 }
9684 else
9685 {
9686 /* Remember the maximum prefix size in FUSED_JCC_PADDING
9687 frag. */
9688 unsigned int max_prefix_size;
9689 if (align_branch_prefix_size > max)
9690 max_prefix_size = max;
9691 else
9692 max_prefix_size = align_branch_prefix_size;
9693 if (max_prefix_size > count)
9694 fragP->tc_frag_data.max_prefix_length
9695 = max_prefix_size - count;
9696 }
9697
9698 /* Use existing segment prefix if possible. Use CS
9699 segment prefix in 64-bit mode. In 32-bit mode, use SS
9700 segment prefix with ESP/EBP base register and use DS
9701 segment prefix without ESP/EBP base register. */
9702 if (i.prefix[SEG_PREFIX])
9703 fragP->tc_frag_data.default_prefix = i.prefix[SEG_PREFIX];
9704 else if (flag_code == CODE_64BIT)
9705 fragP->tc_frag_data.default_prefix = CS_PREFIX_OPCODE;
9706 else if (i.base_reg
9707 && (i.base_reg->reg_num == 4
9708 || i.base_reg->reg_num == 5))
9709 fragP->tc_frag_data.default_prefix = SS_PREFIX_OPCODE;
9710 else
9711 fragP->tc_frag_data.default_prefix = DS_PREFIX_OPCODE;
9712 }
9c33702b 9713 }
29b0f896 9714 }
252b5132 9715
e379e5f3
L
9716 /* NB: Don't work with COND_JUMP86 without i386. */
9717 if (align_branch_power
9718 && now_seg != absolute_section
9719 && cpu_arch_flags.bitfield.cpui386)
9720 {
9721 /* Terminate each frag so that we can add prefix and check for
9722 fused jcc. */
9723 frag_wane (frag_now);
9724 frag_new (0);
9725 }
9726
29b0f896
AM
9727#ifdef DEBUG386
9728 if (flag_debug)
9729 {
7b81dfbb 9730 pi ("" /*line*/, &i);
29b0f896
AM
9731 }
9732#endif /* DEBUG386 */
9733}
252b5132 9734
e205caa7
L
9735/* Return the size of the displacement operand N. */
9736
9737static int
9738disp_size (unsigned int n)
9739{
9740 int size = 4;
43234a1e 9741
b5014f7a 9742 if (i.types[n].bitfield.disp64)
40fb9820
L
9743 size = 8;
9744 else if (i.types[n].bitfield.disp8)
9745 size = 1;
9746 else if (i.types[n].bitfield.disp16)
9747 size = 2;
e205caa7
L
9748 return size;
9749}
9750
9751/* Return the size of the immediate operand N. */
9752
9753static int
9754imm_size (unsigned int n)
9755{
9756 int size = 4;
40fb9820
L
9757 if (i.types[n].bitfield.imm64)
9758 size = 8;
9759 else if (i.types[n].bitfield.imm8 || i.types[n].bitfield.imm8s)
9760 size = 1;
9761 else if (i.types[n].bitfield.imm16)
9762 size = 2;
e205caa7
L
9763 return size;
9764}
9765
29b0f896 9766static void
64e74474 9767output_disp (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
9768{
9769 char *p;
9770 unsigned int n;
252b5132 9771
29b0f896
AM
9772 for (n = 0; n < i.operands; n++)
9773 {
b5014f7a 9774 if (operand_type_check (i.types[n], disp))
29b0f896 9775 {
48ef937e
JB
9776 int size = disp_size (n);
9777
9778 if (now_seg == absolute_section)
9779 abs_section_offset += size;
9780 else if (i.op[n].disps->X_op == O_constant)
29b0f896 9781 {
43234a1e 9782 offsetT val = i.op[n].disps->X_add_number;
252b5132 9783
629cfaf1
JB
9784 val = offset_in_range (val >> (size == 1 ? i.memshift : 0),
9785 size);
29b0f896
AM
9786 p = frag_more (size);
9787 md_number_to_chars (p, val, size);
9788 }
9789 else
9790 {
f86103b7 9791 enum bfd_reloc_code_real reloc_type;
40fb9820 9792 int sign = i.types[n].bitfield.disp32s;
29b0f896 9793 int pcrel = (i.flags[n] & Operand_PCrel) != 0;
02a86693 9794 fixS *fixP;
29b0f896 9795
e205caa7 9796 /* We can't have 8 bit displacement here. */
9c2799c2 9797 gas_assert (!i.types[n].bitfield.disp8);
e205caa7 9798
29b0f896
AM
9799 /* The PC relative address is computed relative
9800 to the instruction boundary, so in case immediate
9801 fields follows, we need to adjust the value. */
9802 if (pcrel && i.imm_operands)
9803 {
29b0f896 9804 unsigned int n1;
e205caa7 9805 int sz = 0;
252b5132 9806
29b0f896 9807 for (n1 = 0; n1 < i.operands; n1++)
40fb9820 9808 if (operand_type_check (i.types[n1], imm))
252b5132 9809 {
e205caa7
L
9810 /* Only one immediate is allowed for PC
9811 relative address. */
9c2799c2 9812 gas_assert (sz == 0);
e205caa7
L
9813 sz = imm_size (n1);
9814 i.op[n].disps->X_add_number -= sz;
252b5132 9815 }
29b0f896 9816 /* We should find the immediate. */
9c2799c2 9817 gas_assert (sz != 0);
29b0f896 9818 }
520dc8e8 9819
29b0f896 9820 p = frag_more (size);
d258b828 9821 reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
d6ab8113 9822 if (GOT_symbol
2bbd9c25 9823 && GOT_symbol == i.op[n].disps->X_add_symbol
d6ab8113 9824 && (((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
9825 || reloc_type == BFD_RELOC_X86_64_32S
9826 || (reloc_type == BFD_RELOC_64
9827 && object_64bit))
d6ab8113
JB
9828 && (i.op[n].disps->X_op == O_symbol
9829 || (i.op[n].disps->X_op == O_add
9830 && ((symbol_get_value_expression
9831 (i.op[n].disps->X_op_symbol)->X_op)
9832 == O_subtract))))
9833 || reloc_type == BFD_RELOC_32_PCREL))
2bbd9c25 9834 {
4fa24527 9835 if (!object_64bit)
7b81dfbb
AJ
9836 {
9837 reloc_type = BFD_RELOC_386_GOTPC;
5b7c81bd 9838 i.has_gotpc_tls_reloc = true;
d583596c
JB
9839 i.op[n].imms->X_add_number +=
9840 encoding_length (insn_start_frag, insn_start_off, p);
7b81dfbb
AJ
9841 }
9842 else if (reloc_type == BFD_RELOC_64)
9843 reloc_type = BFD_RELOC_X86_64_GOTPC64;
d6ab8113 9844 else
7b81dfbb
AJ
9845 /* Don't do the adjustment for x86-64, as there
9846 the pcrel addressing is relative to the _next_
9847 insn, and that is taken care of in other code. */
d6ab8113 9848 reloc_type = BFD_RELOC_X86_64_GOTPC32;
2bbd9c25 9849 }
e379e5f3
L
9850 else if (align_branch_power)
9851 {
9852 switch (reloc_type)
9853 {
9854 case BFD_RELOC_386_TLS_GD:
9855 case BFD_RELOC_386_TLS_LDM:
9856 case BFD_RELOC_386_TLS_IE:
9857 case BFD_RELOC_386_TLS_IE_32:
9858 case BFD_RELOC_386_TLS_GOTIE:
9859 case BFD_RELOC_386_TLS_GOTDESC:
9860 case BFD_RELOC_386_TLS_DESC_CALL:
9861 case BFD_RELOC_X86_64_TLSGD:
9862 case BFD_RELOC_X86_64_TLSLD:
9863 case BFD_RELOC_X86_64_GOTTPOFF:
9864 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
9865 case BFD_RELOC_X86_64_TLSDESC_CALL:
5b7c81bd 9866 i.has_gotpc_tls_reloc = true;
e379e5f3
L
9867 default:
9868 break;
9869 }
9870 }
02a86693
L
9871 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal,
9872 size, i.op[n].disps, pcrel,
9873 reloc_type);
9874 /* Check for "call/jmp *mem", "mov mem, %reg",
9875 "test %reg, mem" and "binop mem, %reg" where binop
9876 is one of adc, add, and, cmp, or, sbb, sub, xor
e60f4d3b
L
9877 instructions without data prefix. Always generate
9878 R_386_GOT32X for "sym*GOT" operand in 32-bit mode. */
9879 if (i.prefix[DATA_PREFIX] == 0
9880 && (generate_relax_relocations
9881 || (!object_64bit
9882 && i.rm.mode == 0
9883 && i.rm.regmem == 5))
0cb4071e
L
9884 && (i.rm.mode == 2
9885 || (i.rm.mode == 0 && i.rm.regmem == 5))
389d00a5 9886 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
02a86693
L
9887 && ((i.operands == 1
9888 && i.tm.base_opcode == 0xff
9889 && (i.rm.reg == 2 || i.rm.reg == 4))
9890 || (i.operands == 2
9891 && (i.tm.base_opcode == 0x8b
9892 || i.tm.base_opcode == 0x85
2ae4c703 9893 || (i.tm.base_opcode & ~0x38) == 0x03))))
02a86693
L
9894 {
9895 if (object_64bit)
9896 {
9897 fixP->fx_tcbit = i.rex != 0;
9898 if (i.base_reg
e968fc9b 9899 && (i.base_reg->reg_num == RegIP))
02a86693
L
9900 fixP->fx_tcbit2 = 1;
9901 }
9902 else
9903 fixP->fx_tcbit2 = 1;
9904 }
29b0f896
AM
9905 }
9906 }
9907 }
9908}
252b5132 9909
29b0f896 9910static void
64e74474 9911output_imm (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
9912{
9913 char *p;
9914 unsigned int n;
252b5132 9915
29b0f896
AM
9916 for (n = 0; n < i.operands; n++)
9917 {
43234a1e 9918 /* Skip SAE/RC Imm operand in EVEX. They are already handled. */
ca5312a2 9919 if (i.rounding.type != rc_none && n == i.rounding.operand)
43234a1e
L
9920 continue;
9921
40fb9820 9922 if (operand_type_check (i.types[n], imm))
29b0f896 9923 {
48ef937e
JB
9924 int size = imm_size (n);
9925
9926 if (now_seg == absolute_section)
9927 abs_section_offset += size;
9928 else if (i.op[n].imms->X_op == O_constant)
29b0f896 9929 {
29b0f896 9930 offsetT val;
b4cac588 9931
29b0f896
AM
9932 val = offset_in_range (i.op[n].imms->X_add_number,
9933 size);
9934 p = frag_more (size);
9935 md_number_to_chars (p, val, size);
9936 }
9937 else
9938 {
9939 /* Not absolute_section.
9940 Need a 32-bit fixup (don't support 8bit
9941 non-absolute imms). Try to support other
9942 sizes ... */
f86103b7 9943 enum bfd_reloc_code_real reloc_type;
e205caa7 9944 int sign;
29b0f896 9945
40fb9820 9946 if (i.types[n].bitfield.imm32s
a7d61044 9947 && (i.suffix == QWORD_MNEM_SUFFIX
40fb9820 9948 || (!i.suffix && i.tm.opcode_modifier.no_lsuf)))
29b0f896 9949 sign = 1;
e205caa7
L
9950 else
9951 sign = 0;
520dc8e8 9952
29b0f896 9953 p = frag_more (size);
d258b828 9954 reloc_type = reloc (size, 0, sign, i.reloc[n]);
f86103b7 9955
2bbd9c25
JJ
9956 /* This is tough to explain. We end up with this one if we
9957 * have operands that look like
9958 * "_GLOBAL_OFFSET_TABLE_+[.-.L284]". The goal here is to
9959 * obtain the absolute address of the GOT, and it is strongly
9960 * preferable from a performance point of view to avoid using
9961 * a runtime relocation for this. The actual sequence of
9962 * instructions often look something like:
9963 *
9964 * call .L66
9965 * .L66:
9966 * popl %ebx
9967 * addl $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
9968 *
9969 * The call and pop essentially return the absolute address
9970 * of the label .L66 and store it in %ebx. The linker itself
9971 * will ultimately change the first operand of the addl so
9972 * that %ebx points to the GOT, but to keep things simple, the
9973 * .o file must have this operand set so that it generates not
9974 * the absolute address of .L66, but the absolute address of
9975 * itself. This allows the linker itself simply treat a GOTPC
9976 * relocation as asking for a pcrel offset to the GOT to be
9977 * added in, and the addend of the relocation is stored in the
9978 * operand field for the instruction itself.
9979 *
9980 * Our job here is to fix the operand so that it would add
9981 * the correct offset so that %ebx would point to itself. The
9982 * thing that is tricky is that .-.L66 will point to the
9983 * beginning of the instruction, so we need to further modify
9984 * the operand so that it will point to itself. There are
9985 * other cases where you have something like:
9986 *
9987 * .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
9988 *
9989 * and here no correction would be required. Internally in
9990 * the assembler we treat operands of this form as not being
9991 * pcrel since the '.' is explicitly mentioned, and I wonder
9992 * whether it would simplify matters to do it this way. Who
9993 * knows. In earlier versions of the PIC patches, the
9994 * pcrel_adjust field was used to store the correction, but
9995 * since the expression is not pcrel, I felt it would be
9996 * confusing to do it this way. */
9997
d6ab8113 9998 if ((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
9999 || reloc_type == BFD_RELOC_X86_64_32S
10000 || reloc_type == BFD_RELOC_64)
29b0f896
AM
10001 && GOT_symbol
10002 && GOT_symbol == i.op[n].imms->X_add_symbol
10003 && (i.op[n].imms->X_op == O_symbol
10004 || (i.op[n].imms->X_op == O_add
10005 && ((symbol_get_value_expression
10006 (i.op[n].imms->X_op_symbol)->X_op)
10007 == O_subtract))))
10008 {
4fa24527 10009 if (!object_64bit)
d6ab8113 10010 reloc_type = BFD_RELOC_386_GOTPC;
7b81dfbb 10011 else if (size == 4)
d6ab8113 10012 reloc_type = BFD_RELOC_X86_64_GOTPC32;
7b81dfbb
AJ
10013 else if (size == 8)
10014 reloc_type = BFD_RELOC_X86_64_GOTPC64;
5b7c81bd 10015 i.has_gotpc_tls_reloc = true;
d583596c
JB
10016 i.op[n].imms->X_add_number +=
10017 encoding_length (insn_start_frag, insn_start_off, p);
29b0f896 10018 }
29b0f896
AM
10019 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
10020 i.op[n].imms, 0, reloc_type);
10021 }
10022 }
10023 }
252b5132
RH
10024}
10025\f
d182319b
JB
10026/* x86_cons_fix_new is called via the expression parsing code when a
10027 reloc is needed. We use this hook to get the correct .got reloc. */
d182319b
JB
10028static int cons_sign = -1;
10029
10030void
e3bb37b5 10031x86_cons_fix_new (fragS *frag, unsigned int off, unsigned int len,
62ebcb5c 10032 expressionS *exp, bfd_reloc_code_real_type r)
d182319b 10033{
d258b828 10034 r = reloc (len, 0, cons_sign, r);
d182319b
JB
10035
10036#ifdef TE_PE
10037 if (exp->X_op == O_secrel)
10038 {
10039 exp->X_op = O_symbol;
10040 r = BFD_RELOC_32_SECREL;
10041 }
10042#endif
10043
10044 fix_new_exp (frag, off, len, exp, 0, r);
10045}
10046
357d1bd8
L
10047/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
10048 purpose of the `.dc.a' internal pseudo-op. */
10049
10050int
10051x86_address_bytes (void)
10052{
10053 if ((stdoutput->arch_info->mach & bfd_mach_x64_32))
10054 return 4;
10055 return stdoutput->arch_info->bits_per_address / 8;
10056}
10057
d382c579
TG
10058#if !(defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined (OBJ_MACH_O)) \
10059 || defined (LEX_AT)
d258b828 10060# define lex_got(reloc, adjust, types) NULL
718ddfc0 10061#else
f3c180ae
AM
10062/* Parse operands of the form
10063 <symbol>@GOTOFF+<nnn>
10064 and similar .plt or .got references.
10065
10066 If we find one, set up the correct relocation in RELOC and copy the
10067 input string, minus the `@GOTOFF' into a malloc'd buffer for
10068 parsing by the calling routine. Return this buffer, and if ADJUST
10069 is non-null set it to the length of the string we removed from the
10070 input line. Otherwise return NULL. */
10071static char *
91d6fa6a 10072lex_got (enum bfd_reloc_code_real *rel,
64e74474 10073 int *adjust,
d258b828 10074 i386_operand_type *types)
f3c180ae 10075{
7b81dfbb
AJ
10076 /* Some of the relocations depend on the size of what field is to
10077 be relocated. But in our callers i386_immediate and i386_displacement
10078 we don't yet know the operand size (this will be set by insn
10079 matching). Hence we record the word32 relocation here,
10080 and adjust the reloc according to the real size in reloc(). */
f3c180ae
AM
10081 static const struct {
10082 const char *str;
cff8d58a 10083 int len;
4fa24527 10084 const enum bfd_reloc_code_real rel[2];
40fb9820 10085 const i386_operand_type types64;
5b7c81bd 10086 bool need_GOT_symbol;
f3c180ae 10087 } gotrel[] = {
8ce3d284 10088#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
10089 { STRING_COMMA_LEN ("SIZE"), { BFD_RELOC_SIZE32,
10090 BFD_RELOC_SIZE32 },
5b7c81bd 10091 OPERAND_TYPE_IMM32_64, false },
8ce3d284 10092#endif
cff8d58a
L
10093 { STRING_COMMA_LEN ("PLTOFF"), { _dummy_first_bfd_reloc_code_real,
10094 BFD_RELOC_X86_64_PLTOFF64 },
5b7c81bd 10095 OPERAND_TYPE_IMM64, true },
cff8d58a
L
10096 { STRING_COMMA_LEN ("PLT"), { BFD_RELOC_386_PLT32,
10097 BFD_RELOC_X86_64_PLT32 },
5b7c81bd 10098 OPERAND_TYPE_IMM32_32S_DISP32, false },
cff8d58a
L
10099 { STRING_COMMA_LEN ("GOTPLT"), { _dummy_first_bfd_reloc_code_real,
10100 BFD_RELOC_X86_64_GOTPLT64 },
5b7c81bd 10101 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
10102 { STRING_COMMA_LEN ("GOTOFF"), { BFD_RELOC_386_GOTOFF,
10103 BFD_RELOC_X86_64_GOTOFF64 },
5b7c81bd 10104 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
10105 { STRING_COMMA_LEN ("GOTPCREL"), { _dummy_first_bfd_reloc_code_real,
10106 BFD_RELOC_X86_64_GOTPCREL },
5b7c81bd 10107 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10108 { STRING_COMMA_LEN ("TLSGD"), { BFD_RELOC_386_TLS_GD,
10109 BFD_RELOC_X86_64_TLSGD },
5b7c81bd 10110 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10111 { STRING_COMMA_LEN ("TLSLDM"), { BFD_RELOC_386_TLS_LDM,
10112 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10113 OPERAND_TYPE_NONE, true },
cff8d58a
L
10114 { STRING_COMMA_LEN ("TLSLD"), { _dummy_first_bfd_reloc_code_real,
10115 BFD_RELOC_X86_64_TLSLD },
5b7c81bd 10116 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10117 { STRING_COMMA_LEN ("GOTTPOFF"), { BFD_RELOC_386_TLS_IE_32,
10118 BFD_RELOC_X86_64_GOTTPOFF },
5b7c81bd 10119 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10120 { STRING_COMMA_LEN ("TPOFF"), { BFD_RELOC_386_TLS_LE_32,
10121 BFD_RELOC_X86_64_TPOFF32 },
5b7c81bd 10122 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
10123 { STRING_COMMA_LEN ("NTPOFF"), { BFD_RELOC_386_TLS_LE,
10124 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10125 OPERAND_TYPE_NONE, true },
cff8d58a
L
10126 { STRING_COMMA_LEN ("DTPOFF"), { BFD_RELOC_386_TLS_LDO_32,
10127 BFD_RELOC_X86_64_DTPOFF32 },
5b7c81bd 10128 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
10129 { STRING_COMMA_LEN ("GOTNTPOFF"),{ BFD_RELOC_386_TLS_GOTIE,
10130 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10131 OPERAND_TYPE_NONE, true },
cff8d58a
L
10132 { STRING_COMMA_LEN ("INDNTPOFF"),{ BFD_RELOC_386_TLS_IE,
10133 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10134 OPERAND_TYPE_NONE, true },
cff8d58a
L
10135 { STRING_COMMA_LEN ("GOT"), { BFD_RELOC_386_GOT32,
10136 BFD_RELOC_X86_64_GOT32 },
5b7c81bd 10137 OPERAND_TYPE_IMM32_32S_64_DISP32, true },
cff8d58a
L
10138 { STRING_COMMA_LEN ("TLSDESC"), { BFD_RELOC_386_TLS_GOTDESC,
10139 BFD_RELOC_X86_64_GOTPC32_TLSDESC },
5b7c81bd 10140 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10141 { STRING_COMMA_LEN ("TLSCALL"), { BFD_RELOC_386_TLS_DESC_CALL,
10142 BFD_RELOC_X86_64_TLSDESC_CALL },
5b7c81bd 10143 OPERAND_TYPE_IMM32_32S_DISP32, true },
f3c180ae
AM
10144 };
10145 char *cp;
10146 unsigned int j;
10147
d382c579 10148#if defined (OBJ_MAYBE_ELF)
718ddfc0
JB
10149 if (!IS_ELF)
10150 return NULL;
d382c579 10151#endif
718ddfc0 10152
f3c180ae 10153 for (cp = input_line_pointer; *cp != '@'; cp++)
67c11a9b 10154 if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
f3c180ae
AM
10155 return NULL;
10156
47465058 10157 for (j = 0; j < ARRAY_SIZE (gotrel); j++)
f3c180ae 10158 {
cff8d58a 10159 int len = gotrel[j].len;
28f81592 10160 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
f3c180ae 10161 {
4fa24527 10162 if (gotrel[j].rel[object_64bit] != 0)
f3c180ae 10163 {
28f81592
AM
10164 int first, second;
10165 char *tmpbuf, *past_reloc;
f3c180ae 10166
91d6fa6a 10167 *rel = gotrel[j].rel[object_64bit];
f3c180ae 10168
3956db08
JB
10169 if (types)
10170 {
10171 if (flag_code != CODE_64BIT)
40fb9820
L
10172 {
10173 types->bitfield.imm32 = 1;
10174 types->bitfield.disp32 = 1;
10175 }
3956db08
JB
10176 else
10177 *types = gotrel[j].types64;
10178 }
10179
844bf810 10180 if (gotrel[j].need_GOT_symbol && GOT_symbol == NULL)
f3c180ae
AM
10181 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
10182
28f81592 10183 /* The length of the first part of our input line. */
f3c180ae 10184 first = cp - input_line_pointer;
28f81592
AM
10185
10186 /* The second part goes from after the reloc token until
67c11a9b 10187 (and including) an end_of_line char or comma. */
28f81592 10188 past_reloc = cp + 1 + len;
67c11a9b
AM
10189 cp = past_reloc;
10190 while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
10191 ++cp;
10192 second = cp + 1 - past_reloc;
28f81592
AM
10193
10194 /* Allocate and copy string. The trailing NUL shouldn't
10195 be necessary, but be safe. */
add39d23 10196 tmpbuf = XNEWVEC (char, first + second + 2);
f3c180ae 10197 memcpy (tmpbuf, input_line_pointer, first);
0787a12d
AM
10198 if (second != 0 && *past_reloc != ' ')
10199 /* Replace the relocation token with ' ', so that
10200 errors like foo@GOTOFF1 will be detected. */
10201 tmpbuf[first++] = ' ';
af89796a
L
10202 else
10203 /* Increment length by 1 if the relocation token is
10204 removed. */
10205 len++;
10206 if (adjust)
10207 *adjust = len;
0787a12d
AM
10208 memcpy (tmpbuf + first, past_reloc, second);
10209 tmpbuf[first + second] = '\0';
f3c180ae
AM
10210 return tmpbuf;
10211 }
10212
4fa24527
JB
10213 as_bad (_("@%s reloc is not supported with %d-bit output format"),
10214 gotrel[j].str, 1 << (5 + object_64bit));
f3c180ae
AM
10215 return NULL;
10216 }
10217 }
10218
10219 /* Might be a symbol version string. Don't as_bad here. */
10220 return NULL;
10221}
4e4f7c87 10222#endif
f3c180ae 10223
a988325c
NC
10224#ifdef TE_PE
10225#ifdef lex_got
10226#undef lex_got
10227#endif
10228/* Parse operands of the form
10229 <symbol>@SECREL32+<nnn>
10230
10231 If we find one, set up the correct relocation in RELOC and copy the
10232 input string, minus the `@SECREL32' into a malloc'd buffer for
10233 parsing by the calling routine. Return this buffer, and if ADJUST
10234 is non-null set it to the length of the string we removed from the
34bca508
L
10235 input line. Otherwise return NULL.
10236
a988325c
NC
10237 This function is copied from the ELF version above adjusted for PE targets. */
10238
10239static char *
10240lex_got (enum bfd_reloc_code_real *rel ATTRIBUTE_UNUSED,
10241 int *adjust ATTRIBUTE_UNUSED,
d258b828 10242 i386_operand_type *types)
a988325c
NC
10243{
10244 static const struct
10245 {
10246 const char *str;
10247 int len;
10248 const enum bfd_reloc_code_real rel[2];
10249 const i386_operand_type types64;
10250 }
10251 gotrel[] =
10252 {
10253 { STRING_COMMA_LEN ("SECREL32"), { BFD_RELOC_32_SECREL,
10254 BFD_RELOC_32_SECREL },
10255 OPERAND_TYPE_IMM32_32S_64_DISP32_64 },
10256 };
10257
10258 char *cp;
10259 unsigned j;
10260
10261 for (cp = input_line_pointer; *cp != '@'; cp++)
10262 if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
10263 return NULL;
10264
10265 for (j = 0; j < ARRAY_SIZE (gotrel); j++)
10266 {
10267 int len = gotrel[j].len;
10268
10269 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
10270 {
10271 if (gotrel[j].rel[object_64bit] != 0)
10272 {
10273 int first, second;
10274 char *tmpbuf, *past_reloc;
10275
10276 *rel = gotrel[j].rel[object_64bit];
10277 if (adjust)
10278 *adjust = len;
10279
10280 if (types)
10281 {
10282 if (flag_code != CODE_64BIT)
10283 {
10284 types->bitfield.imm32 = 1;
10285 types->bitfield.disp32 = 1;
10286 }
10287 else
10288 *types = gotrel[j].types64;
10289 }
10290
10291 /* The length of the first part of our input line. */
10292 first = cp - input_line_pointer;
10293
10294 /* The second part goes from after the reloc token until
10295 (and including) an end_of_line char or comma. */
10296 past_reloc = cp + 1 + len;
10297 cp = past_reloc;
10298 while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
10299 ++cp;
10300 second = cp + 1 - past_reloc;
10301
10302 /* Allocate and copy string. The trailing NUL shouldn't
10303 be necessary, but be safe. */
add39d23 10304 tmpbuf = XNEWVEC (char, first + second + 2);
a988325c
NC
10305 memcpy (tmpbuf, input_line_pointer, first);
10306 if (second != 0 && *past_reloc != ' ')
10307 /* Replace the relocation token with ' ', so that
10308 errors like foo@SECLREL321 will be detected. */
10309 tmpbuf[first++] = ' ';
10310 memcpy (tmpbuf + first, past_reloc, second);
10311 tmpbuf[first + second] = '\0';
10312 return tmpbuf;
10313 }
10314
10315 as_bad (_("@%s reloc is not supported with %d-bit output format"),
10316 gotrel[j].str, 1 << (5 + object_64bit));
10317 return NULL;
10318 }
10319 }
10320
10321 /* Might be a symbol version string. Don't as_bad here. */
10322 return NULL;
10323}
10324
10325#endif /* TE_PE */
10326
62ebcb5c 10327bfd_reloc_code_real_type
e3bb37b5 10328x86_cons (expressionS *exp, int size)
f3c180ae 10329{
62ebcb5c
AM
10330 bfd_reloc_code_real_type got_reloc = NO_RELOC;
10331
ee86248c
JB
10332 intel_syntax = -intel_syntax;
10333
3c7b9c2c 10334 exp->X_md = 0;
4fa24527 10335 if (size == 4 || (object_64bit && size == 8))
f3c180ae
AM
10336 {
10337 /* Handle @GOTOFF and the like in an expression. */
10338 char *save;
10339 char *gotfree_input_line;
4a57f2cf 10340 int adjust = 0;
f3c180ae
AM
10341
10342 save = input_line_pointer;
d258b828 10343 gotfree_input_line = lex_got (&got_reloc, &adjust, NULL);
f3c180ae
AM
10344 if (gotfree_input_line)
10345 input_line_pointer = gotfree_input_line;
10346
10347 expression (exp);
10348
10349 if (gotfree_input_line)
10350 {
10351 /* expression () has merrily parsed up to the end of line,
10352 or a comma - in the wrong buffer. Transfer how far
10353 input_line_pointer has moved to the right buffer. */
10354 input_line_pointer = (save
10355 + (input_line_pointer - gotfree_input_line)
10356 + adjust);
10357 free (gotfree_input_line);
3992d3b7
AM
10358 if (exp->X_op == O_constant
10359 || exp->X_op == O_absent
10360 || exp->X_op == O_illegal
0398aac5 10361 || exp->X_op == O_register
3992d3b7
AM
10362 || exp->X_op == O_big)
10363 {
10364 char c = *input_line_pointer;
10365 *input_line_pointer = 0;
10366 as_bad (_("missing or invalid expression `%s'"), save);
10367 *input_line_pointer = c;
10368 }
b9519cfe
L
10369 else if ((got_reloc == BFD_RELOC_386_PLT32
10370 || got_reloc == BFD_RELOC_X86_64_PLT32)
10371 && exp->X_op != O_symbol)
10372 {
10373 char c = *input_line_pointer;
10374 *input_line_pointer = 0;
10375 as_bad (_("invalid PLT expression `%s'"), save);
10376 *input_line_pointer = c;
10377 }
f3c180ae
AM
10378 }
10379 }
10380 else
10381 expression (exp);
ee86248c
JB
10382
10383 intel_syntax = -intel_syntax;
10384
10385 if (intel_syntax)
10386 i386_intel_simplify (exp);
62ebcb5c
AM
10387
10388 return got_reloc;
f3c180ae 10389}
f3c180ae 10390
9f32dd5b
L
10391static void
10392signed_cons (int size)
6482c264 10393{
d182319b
JB
10394 if (flag_code == CODE_64BIT)
10395 cons_sign = 1;
10396 cons (size);
10397 cons_sign = -1;
6482c264
NC
10398}
10399
d182319b 10400#ifdef TE_PE
6482c264 10401static void
7016a5d5 10402pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
6482c264
NC
10403{
10404 expressionS exp;
10405
10406 do
10407 {
10408 expression (&exp);
10409 if (exp.X_op == O_symbol)
10410 exp.X_op = O_secrel;
10411
10412 emit_expr (&exp, 4);
10413 }
10414 while (*input_line_pointer++ == ',');
10415
10416 input_line_pointer--;
10417 demand_empty_rest_of_line ();
10418}
6482c264
NC
10419#endif
10420
43234a1e
L
10421/* Handle Vector operations. */
10422
10423static char *
10424check_VecOperations (char *op_string, char *op_end)
10425{
10426 const reg_entry *mask;
10427 const char *saved;
10428 char *end_op;
10429
10430 while (*op_string
10431 && (op_end == NULL || op_string < op_end))
10432 {
10433 saved = op_string;
10434 if (*op_string == '{')
10435 {
10436 op_string++;
10437
10438 /* Check broadcasts. */
d34049e8 10439 if (startswith (op_string, "1to"))
43234a1e 10440 {
5273a3cd 10441 unsigned int bcst_type;
43234a1e 10442
5273a3cd 10443 if (i.broadcast.type)
43234a1e
L
10444 goto duplicated_vec_op;
10445
10446 op_string += 3;
10447 if (*op_string == '8')
8e6e0792 10448 bcst_type = 8;
b28d1bda 10449 else if (*op_string == '4')
8e6e0792 10450 bcst_type = 4;
b28d1bda 10451 else if (*op_string == '2')
8e6e0792 10452 bcst_type = 2;
43234a1e
L
10453 else if (*op_string == '1'
10454 && *(op_string+1) == '6')
10455 {
8e6e0792 10456 bcst_type = 16;
43234a1e
L
10457 op_string++;
10458 }
10459 else
10460 {
10461 as_bad (_("Unsupported broadcast: `%s'"), saved);
10462 return NULL;
10463 }
10464 op_string++;
10465
5273a3cd
JB
10466 i.broadcast.type = bcst_type;
10467 i.broadcast.operand = this_operand;
43234a1e
L
10468 }
10469 /* Check masking operation. */
10470 else if ((mask = parse_register (op_string, &end_op)) != NULL)
10471 {
8a6fb3f9
JB
10472 if (mask == &bad_reg)
10473 return NULL;
10474
43234a1e 10475 /* k0 can't be used for write mask. */
f74a6307 10476 if (mask->reg_type.bitfield.class != RegMask || !mask->reg_num)
43234a1e 10477 {
6d2cd6b2
JB
10478 as_bad (_("`%s%s' can't be used for write mask"),
10479 register_prefix, mask->reg_name);
43234a1e
L
10480 return NULL;
10481 }
10482
6225c532 10483 if (!i.mask.reg)
43234a1e 10484 {
6225c532
JB
10485 i.mask.reg = mask;
10486 i.mask.operand = this_operand;
43234a1e 10487 }
6225c532
JB
10488 else if (i.mask.reg->reg_num)
10489 goto duplicated_vec_op;
43234a1e
L
10490 else
10491 {
6225c532 10492 i.mask.reg = mask;
43234a1e
L
10493
10494 /* Only "{z}" is allowed here. No need to check
10495 zeroing mask explicitly. */
6225c532 10496 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
10497 {
10498 as_bad (_("invalid write mask `%s'"), saved);
10499 return NULL;
10500 }
10501 }
10502
10503 op_string = end_op;
10504 }
10505 /* Check zeroing-flag for masking operation. */
10506 else if (*op_string == 'z')
10507 {
6225c532 10508 if (!i.mask.reg)
43234a1e 10509 {
6225c532
JB
10510 i.mask.reg = reg_k0;
10511 i.mask.zeroing = 1;
10512 i.mask.operand = this_operand;
43234a1e
L
10513 }
10514 else
10515 {
6225c532 10516 if (i.mask.zeroing)
43234a1e
L
10517 {
10518 duplicated_vec_op:
10519 as_bad (_("duplicated `%s'"), saved);
10520 return NULL;
10521 }
10522
6225c532 10523 i.mask.zeroing = 1;
43234a1e
L
10524
10525 /* Only "{%k}" is allowed here. No need to check mask
10526 register explicitly. */
6225c532 10527 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
10528 {
10529 as_bad (_("invalid zeroing-masking `%s'"),
10530 saved);
10531 return NULL;
10532 }
10533 }
10534
10535 op_string++;
10536 }
10537 else
10538 goto unknown_vec_op;
10539
10540 if (*op_string != '}')
10541 {
10542 as_bad (_("missing `}' in `%s'"), saved);
10543 return NULL;
10544 }
10545 op_string++;
0ba3a731
L
10546
10547 /* Strip whitespace since the addition of pseudo prefixes
10548 changed how the scrubber treats '{'. */
10549 if (is_space_char (*op_string))
10550 ++op_string;
10551
43234a1e
L
10552 continue;
10553 }
10554 unknown_vec_op:
10555 /* We don't know this one. */
10556 as_bad (_("unknown vector operation: `%s'"), saved);
10557 return NULL;
10558 }
10559
6225c532 10560 if (i.mask.reg && i.mask.zeroing && !i.mask.reg->reg_num)
6d2cd6b2
JB
10561 {
10562 as_bad (_("zeroing-masking only allowed with write mask"));
10563 return NULL;
10564 }
10565
43234a1e
L
10566 return op_string;
10567}
10568
252b5132 10569static int
70e41ade 10570i386_immediate (char *imm_start)
252b5132
RH
10571{
10572 char *save_input_line_pointer;
f3c180ae 10573 char *gotfree_input_line;
252b5132 10574 segT exp_seg = 0;
47926f60 10575 expressionS *exp;
40fb9820
L
10576 i386_operand_type types;
10577
0dfbf9d7 10578 operand_type_set (&types, ~0);
252b5132
RH
10579
10580 if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
10581 {
31b2323c
L
10582 as_bad (_("at most %d immediate operands are allowed"),
10583 MAX_IMMEDIATE_OPERANDS);
252b5132
RH
10584 return 0;
10585 }
10586
10587 exp = &im_expressions[i.imm_operands++];
520dc8e8 10588 i.op[this_operand].imms = exp;
252b5132
RH
10589
10590 if (is_space_char (*imm_start))
10591 ++imm_start;
10592
10593 save_input_line_pointer = input_line_pointer;
10594 input_line_pointer = imm_start;
10595
d258b828 10596 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
10597 if (gotfree_input_line)
10598 input_line_pointer = gotfree_input_line;
252b5132
RH
10599
10600 exp_seg = expression (exp);
10601
83183c0c 10602 SKIP_WHITESPACE ();
43234a1e
L
10603
10604 /* Handle vector operations. */
10605 if (*input_line_pointer == '{')
10606 {
10607 input_line_pointer = check_VecOperations (input_line_pointer,
10608 NULL);
10609 if (input_line_pointer == NULL)
10610 return 0;
10611 }
10612
252b5132 10613 if (*input_line_pointer)
f3c180ae 10614 as_bad (_("junk `%s' after expression"), input_line_pointer);
252b5132
RH
10615
10616 input_line_pointer = save_input_line_pointer;
f3c180ae 10617 if (gotfree_input_line)
ee86248c
JB
10618 {
10619 free (gotfree_input_line);
10620
10621 if (exp->X_op == O_constant || exp->X_op == O_register)
10622 exp->X_op = O_illegal;
10623 }
10624
10625 return i386_finalize_immediate (exp_seg, exp, types, imm_start);
10626}
252b5132 10627
ee86248c
JB
10628static int
10629i386_finalize_immediate (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
10630 i386_operand_type types, const char *imm_start)
10631{
10632 if (exp->X_op == O_absent || exp->X_op == O_illegal || exp->X_op == O_big)
252b5132 10633 {
313c53d1
L
10634 if (imm_start)
10635 as_bad (_("missing or invalid immediate expression `%s'"),
10636 imm_start);
3992d3b7 10637 return 0;
252b5132 10638 }
3e73aa7c 10639 else if (exp->X_op == O_constant)
252b5132 10640 {
47926f60 10641 /* Size it properly later. */
40fb9820 10642 i.types[this_operand].bitfield.imm64 = 1;
13f864ae
L
10643 /* If not 64bit, sign extend val. */
10644 if (flag_code != CODE_64BIT
4eed87de
AM
10645 && (exp->X_add_number & ~(((addressT) 2 << 31) - 1)) == 0)
10646 exp->X_add_number
10647 = (exp->X_add_number ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
252b5132 10648 }
4c63da97 10649#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
f86103b7 10650 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
31312f95 10651 && exp_seg != absolute_section
47926f60 10652 && exp_seg != text_section
24eab124
AM
10653 && exp_seg != data_section
10654 && exp_seg != bss_section
10655 && exp_seg != undefined_section
f86103b7 10656 && !bfd_is_com_section (exp_seg))
252b5132 10657 {
d0b47220 10658 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
252b5132
RH
10659 return 0;
10660 }
10661#endif
a841bdf5 10662 else if (!intel_syntax && exp_seg == reg_section)
bb8f5920 10663 {
313c53d1
L
10664 if (imm_start)
10665 as_bad (_("illegal immediate register operand %s"), imm_start);
bb8f5920
L
10666 return 0;
10667 }
252b5132
RH
10668 else
10669 {
10670 /* This is an address. The size of the address will be
24eab124 10671 determined later, depending on destination register,
3e73aa7c 10672 suffix, or the default for the section. */
40fb9820
L
10673 i.types[this_operand].bitfield.imm8 = 1;
10674 i.types[this_operand].bitfield.imm16 = 1;
10675 i.types[this_operand].bitfield.imm32 = 1;
10676 i.types[this_operand].bitfield.imm32s = 1;
10677 i.types[this_operand].bitfield.imm64 = 1;
c6fb90c8
L
10678 i.types[this_operand] = operand_type_and (i.types[this_operand],
10679 types);
252b5132
RH
10680 }
10681
10682 return 1;
10683}
10684
551c1ca1 10685static char *
e3bb37b5 10686i386_scale (char *scale)
252b5132 10687{
551c1ca1
AM
10688 offsetT val;
10689 char *save = input_line_pointer;
252b5132 10690
551c1ca1
AM
10691 input_line_pointer = scale;
10692 val = get_absolute_expression ();
10693
10694 switch (val)
252b5132 10695 {
551c1ca1 10696 case 1:
252b5132
RH
10697 i.log2_scale_factor = 0;
10698 break;
551c1ca1 10699 case 2:
252b5132
RH
10700 i.log2_scale_factor = 1;
10701 break;
551c1ca1 10702 case 4:
252b5132
RH
10703 i.log2_scale_factor = 2;
10704 break;
551c1ca1 10705 case 8:
252b5132
RH
10706 i.log2_scale_factor = 3;
10707 break;
10708 default:
a724f0f4
JB
10709 {
10710 char sep = *input_line_pointer;
10711
10712 *input_line_pointer = '\0';
10713 as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
10714 scale);
10715 *input_line_pointer = sep;
10716 input_line_pointer = save;
10717 return NULL;
10718 }
252b5132 10719 }
29b0f896 10720 if (i.log2_scale_factor != 0 && i.index_reg == 0)
252b5132
RH
10721 {
10722 as_warn (_("scale factor of %d without an index register"),
24eab124 10723 1 << i.log2_scale_factor);
252b5132 10724 i.log2_scale_factor = 0;
252b5132 10725 }
551c1ca1
AM
10726 scale = input_line_pointer;
10727 input_line_pointer = save;
10728 return scale;
252b5132
RH
10729}
10730
252b5132 10731static int
e3bb37b5 10732i386_displacement (char *disp_start, char *disp_end)
252b5132 10733{
29b0f896 10734 expressionS *exp;
252b5132
RH
10735 segT exp_seg = 0;
10736 char *save_input_line_pointer;
f3c180ae 10737 char *gotfree_input_line;
40fb9820
L
10738 int override;
10739 i386_operand_type bigdisp, types = anydisp;
3992d3b7 10740 int ret;
252b5132 10741
31b2323c
L
10742 if (i.disp_operands == MAX_MEMORY_OPERANDS)
10743 {
10744 as_bad (_("at most %d displacement operands are allowed"),
10745 MAX_MEMORY_OPERANDS);
10746 return 0;
10747 }
10748
0dfbf9d7 10749 operand_type_set (&bigdisp, 0);
6f2f06be 10750 if (i.jumpabsolute
48bcea9f 10751 || i.types[this_operand].bitfield.baseindex
0cfa3eb3
JB
10752 || (current_templates->start->opcode_modifier.jump != JUMP
10753 && current_templates->start->opcode_modifier.jump != JUMP_DWORD))
e05278af 10754 {
48bcea9f 10755 i386_addressing_mode ();
e05278af 10756 override = (i.prefix[ADDR_PREFIX] != 0);
40fb9820
L
10757 if (flag_code == CODE_64BIT)
10758 {
10759 if (!override)
10760 {
10761 bigdisp.bitfield.disp32s = 1;
10762 bigdisp.bitfield.disp64 = 1;
10763 }
48bcea9f
JB
10764 else
10765 bigdisp.bitfield.disp32 = 1;
40fb9820
L
10766 }
10767 else if ((flag_code == CODE_16BIT) ^ override)
40fb9820 10768 bigdisp.bitfield.disp16 = 1;
48bcea9f
JB
10769 else
10770 bigdisp.bitfield.disp32 = 1;
e05278af
JB
10771 }
10772 else
10773 {
376cd056
JB
10774 /* For PC-relative branches, the width of the displacement may be
10775 dependent upon data size, but is never dependent upon address size.
10776 Also make sure to not unintentionally match against a non-PC-relative
10777 branch template. */
10778 static templates aux_templates;
10779 const insn_template *t = current_templates->start;
5b7c81bd 10780 bool has_intel64 = false;
376cd056
JB
10781
10782 aux_templates.start = t;
10783 while (++t < current_templates->end)
10784 {
10785 if (t->opcode_modifier.jump
10786 != current_templates->start->opcode_modifier.jump)
10787 break;
4b5aaf5f 10788 if ((t->opcode_modifier.isa64 >= INTEL64))
5b7c81bd 10789 has_intel64 = true;
376cd056
JB
10790 }
10791 if (t < current_templates->end)
10792 {
10793 aux_templates.end = t;
10794 current_templates = &aux_templates;
10795 }
10796
e05278af 10797 override = (i.prefix[DATA_PREFIX] != 0);
40fb9820
L
10798 if (flag_code == CODE_64BIT)
10799 {
376cd056
JB
10800 if ((override || i.suffix == WORD_MNEM_SUFFIX)
10801 && (!intel64 || !has_intel64))
40fb9820
L
10802 bigdisp.bitfield.disp16 = 1;
10803 else
48bcea9f 10804 bigdisp.bitfield.disp32s = 1;
40fb9820
L
10805 }
10806 else
e05278af
JB
10807 {
10808 if (!override)
10809 override = (i.suffix == (flag_code != CODE_16BIT
10810 ? WORD_MNEM_SUFFIX
10811 : LONG_MNEM_SUFFIX));
40fb9820
L
10812 bigdisp.bitfield.disp32 = 1;
10813 if ((flag_code == CODE_16BIT) ^ override)
10814 {
10815 bigdisp.bitfield.disp32 = 0;
10816 bigdisp.bitfield.disp16 = 1;
10817 }
e05278af 10818 }
e05278af 10819 }
c6fb90c8
L
10820 i.types[this_operand] = operand_type_or (i.types[this_operand],
10821 bigdisp);
252b5132
RH
10822
10823 exp = &disp_expressions[i.disp_operands];
520dc8e8 10824 i.op[this_operand].disps = exp;
252b5132
RH
10825 i.disp_operands++;
10826 save_input_line_pointer = input_line_pointer;
10827 input_line_pointer = disp_start;
10828 END_STRING_AND_SAVE (disp_end);
10829
10830#ifndef GCC_ASM_O_HACK
10831#define GCC_ASM_O_HACK 0
10832#endif
10833#if GCC_ASM_O_HACK
10834 END_STRING_AND_SAVE (disp_end + 1);
40fb9820 10835 if (i.types[this_operand].bitfield.baseIndex
24eab124 10836 && displacement_string_end[-1] == '+')
252b5132
RH
10837 {
10838 /* This hack is to avoid a warning when using the "o"
24eab124
AM
10839 constraint within gcc asm statements.
10840 For instance:
10841
10842 #define _set_tssldt_desc(n,addr,limit,type) \
10843 __asm__ __volatile__ ( \
10844 "movw %w2,%0\n\t" \
10845 "movw %w1,2+%0\n\t" \
10846 "rorl $16,%1\n\t" \
10847 "movb %b1,4+%0\n\t" \
10848 "movb %4,5+%0\n\t" \
10849 "movb $0,6+%0\n\t" \
10850 "movb %h1,7+%0\n\t" \
10851 "rorl $16,%1" \
10852 : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
10853
10854 This works great except that the output assembler ends
10855 up looking a bit weird if it turns out that there is
10856 no offset. You end up producing code that looks like:
10857
10858 #APP
10859 movw $235,(%eax)
10860 movw %dx,2+(%eax)
10861 rorl $16,%edx
10862 movb %dl,4+(%eax)
10863 movb $137,5+(%eax)
10864 movb $0,6+(%eax)
10865 movb %dh,7+(%eax)
10866 rorl $16,%edx
10867 #NO_APP
10868
47926f60 10869 So here we provide the missing zero. */
24eab124
AM
10870
10871 *displacement_string_end = '0';
252b5132
RH
10872 }
10873#endif
d258b828 10874 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
10875 if (gotfree_input_line)
10876 input_line_pointer = gotfree_input_line;
252b5132 10877
24eab124 10878 exp_seg = expression (exp);
252b5132 10879
636c26b0
AM
10880 SKIP_WHITESPACE ();
10881 if (*input_line_pointer)
10882 as_bad (_("junk `%s' after expression"), input_line_pointer);
10883#if GCC_ASM_O_HACK
10884 RESTORE_END_STRING (disp_end + 1);
10885#endif
636c26b0 10886 input_line_pointer = save_input_line_pointer;
636c26b0 10887 if (gotfree_input_line)
ee86248c
JB
10888 {
10889 free (gotfree_input_line);
10890
10891 if (exp->X_op == O_constant || exp->X_op == O_register)
10892 exp->X_op = O_illegal;
10893 }
10894
10895 ret = i386_finalize_displacement (exp_seg, exp, types, disp_start);
10896
10897 RESTORE_END_STRING (disp_end);
10898
10899 return ret;
10900}
10901
10902static int
10903i386_finalize_displacement (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
10904 i386_operand_type types, const char *disp_start)
10905{
10906 i386_operand_type bigdisp;
10907 int ret = 1;
636c26b0 10908
24eab124
AM
10909 /* We do this to make sure that the section symbol is in
10910 the symbol table. We will ultimately change the relocation
47926f60 10911 to be relative to the beginning of the section. */
1ae12ab7 10912 if (i.reloc[this_operand] == BFD_RELOC_386_GOTOFF
d6ab8113
JB
10913 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
10914 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
24eab124 10915 {
636c26b0 10916 if (exp->X_op != O_symbol)
3992d3b7 10917 goto inv_disp;
636c26b0 10918
e5cb08ac 10919 if (S_IS_LOCAL (exp->X_add_symbol)
c64efb4b
L
10920 && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section
10921 && S_GET_SEGMENT (exp->X_add_symbol) != expr_section)
24eab124 10922 section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
24eab124
AM
10923 exp->X_op = O_subtract;
10924 exp->X_op_symbol = GOT_symbol;
1ae12ab7 10925 if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
29b0f896 10926 i.reloc[this_operand] = BFD_RELOC_32_PCREL;
d6ab8113
JB
10927 else if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
10928 i.reloc[this_operand] = BFD_RELOC_64;
23df1078 10929 else
29b0f896 10930 i.reloc[this_operand] = BFD_RELOC_32;
24eab124 10931 }
252b5132 10932
3992d3b7
AM
10933 else if (exp->X_op == O_absent
10934 || exp->X_op == O_illegal
ee86248c 10935 || exp->X_op == O_big)
2daf4fd8 10936 {
3992d3b7
AM
10937 inv_disp:
10938 as_bad (_("missing or invalid displacement expression `%s'"),
2daf4fd8 10939 disp_start);
3992d3b7 10940 ret = 0;
2daf4fd8
AM
10941 }
10942
4c63da97 10943#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
3992d3b7
AM
10944 else if (exp->X_op != O_constant
10945 && OUTPUT_FLAVOR == bfd_target_aout_flavour
10946 && exp_seg != absolute_section
10947 && exp_seg != text_section
10948 && exp_seg != data_section
10949 && exp_seg != bss_section
10950 && exp_seg != undefined_section
10951 && !bfd_is_com_section (exp_seg))
24eab124 10952 {
d0b47220 10953 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
3992d3b7 10954 ret = 0;
24eab124 10955 }
252b5132 10956#endif
3956db08 10957
48bcea9f
JB
10958 if (current_templates->start->opcode_modifier.jump == JUMP_BYTE
10959 /* Constants get taken care of by optimize_disp(). */
10960 && exp->X_op != O_constant)
10961 i.types[this_operand].bitfield.disp8 = 1;
10962
40fb9820
L
10963 /* Check if this is a displacement only operand. */
10964 bigdisp = i.types[this_operand];
10965 bigdisp.bitfield.disp8 = 0;
10966 bigdisp.bitfield.disp16 = 0;
10967 bigdisp.bitfield.disp32 = 0;
10968 bigdisp.bitfield.disp32s = 0;
10969 bigdisp.bitfield.disp64 = 0;
0dfbf9d7 10970 if (operand_type_all_zero (&bigdisp))
c6fb90c8
L
10971 i.types[this_operand] = operand_type_and (i.types[this_operand],
10972 types);
3956db08 10973
3992d3b7 10974 return ret;
252b5132
RH
10975}
10976
2abc2bec
JB
10977/* Return the active addressing mode, taking address override and
10978 registers forming the address into consideration. Update the
10979 address override prefix if necessary. */
47926f60 10980
2abc2bec
JB
10981static enum flag_code
10982i386_addressing_mode (void)
252b5132 10983{
be05d201
L
10984 enum flag_code addr_mode;
10985
10986 if (i.prefix[ADDR_PREFIX])
10987 addr_mode = flag_code == CODE_32BIT ? CODE_16BIT : CODE_32BIT;
a23b33b3
JB
10988 else if (flag_code == CODE_16BIT
10989 && current_templates->start->cpu_flags.bitfield.cpumpx
10990 /* Avoid replacing the "16-bit addressing not allowed" diagnostic
10991 from md_assemble() by "is not a valid base/index expression"
10992 when there is a base and/or index. */
10993 && !i.types[this_operand].bitfield.baseindex)
10994 {
10995 /* MPX insn memory operands with neither base nor index must be forced
10996 to use 32-bit addressing in 16-bit mode. */
10997 addr_mode = CODE_32BIT;
10998 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
10999 ++i.prefixes;
11000 gas_assert (!i.types[this_operand].bitfield.disp16);
11001 gas_assert (!i.types[this_operand].bitfield.disp32);
11002 }
be05d201
L
11003 else
11004 {
11005 addr_mode = flag_code;
11006
24eab124 11007#if INFER_ADDR_PREFIX
be05d201
L
11008 if (i.mem_operands == 0)
11009 {
11010 /* Infer address prefix from the first memory operand. */
11011 const reg_entry *addr_reg = i.base_reg;
11012
11013 if (addr_reg == NULL)
11014 addr_reg = i.index_reg;
eecb386c 11015
be05d201
L
11016 if (addr_reg)
11017 {
e968fc9b 11018 if (addr_reg->reg_type.bitfield.dword)
be05d201
L
11019 addr_mode = CODE_32BIT;
11020 else if (flag_code != CODE_64BIT
dc821c5f 11021 && addr_reg->reg_type.bitfield.word)
be05d201
L
11022 addr_mode = CODE_16BIT;
11023
11024 if (addr_mode != flag_code)
11025 {
11026 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
11027 i.prefixes += 1;
11028 /* Change the size of any displacement too. At most one
11029 of Disp16 or Disp32 is set.
11030 FIXME. There doesn't seem to be any real need for
11031 separate Disp16 and Disp32 flags. The same goes for
11032 Imm16 and Imm32. Removing them would probably clean
11033 up the code quite a lot. */
11034 if (flag_code != CODE_64BIT
11035 && (i.types[this_operand].bitfield.disp16
11036 || i.types[this_operand].bitfield.disp32))
11037 i.types[this_operand]
11038 = operand_type_xor (i.types[this_operand], disp16_32);
11039 }
11040 }
11041 }
24eab124 11042#endif
be05d201
L
11043 }
11044
2abc2bec
JB
11045 return addr_mode;
11046}
11047
11048/* Make sure the memory operand we've been dealt is valid.
11049 Return 1 on success, 0 on a failure. */
11050
11051static int
11052i386_index_check (const char *operand_string)
11053{
11054 const char *kind = "base/index";
11055 enum flag_code addr_mode = i386_addressing_mode ();
a152332d 11056 const insn_template *t = current_templates->start;
2abc2bec 11057
a152332d
JB
11058 if (t->opcode_modifier.isstring
11059 && !t->cpu_flags.bitfield.cpupadlock
fc0763e6
JB
11060 && (current_templates->end[-1].opcode_modifier.isstring
11061 || i.mem_operands))
11062 {
11063 /* Memory operands of string insns are special in that they only allow
11064 a single register (rDI, rSI, or rBX) as their memory address. */
be05d201
L
11065 const reg_entry *expected_reg;
11066 static const char *di_si[][2] =
11067 {
11068 { "esi", "edi" },
11069 { "si", "di" },
11070 { "rsi", "rdi" }
11071 };
11072 static const char *bx[] = { "ebx", "bx", "rbx" };
fc0763e6
JB
11073
11074 kind = "string address";
11075
a152332d 11076 if (t->opcode_modifier.prefixok == PrefixRep)
fc0763e6 11077 {
51c8edf6
JB
11078 int es_op = current_templates->end[-1].opcode_modifier.isstring
11079 - IS_STRING_ES_OP0;
11080 int op = 0;
fc0763e6 11081
51c8edf6 11082 if (!current_templates->end[-1].operand_types[0].bitfield.baseindex
fc0763e6
JB
11083 || ((!i.mem_operands != !intel_syntax)
11084 && current_templates->end[-1].operand_types[1]
11085 .bitfield.baseindex))
51c8edf6 11086 op = 1;
fe0e921f
AM
11087 expected_reg
11088 = (const reg_entry *) str_hash_find (reg_hash,
11089 di_si[addr_mode][op == es_op]);
fc0763e6
JB
11090 }
11091 else
fe0e921f
AM
11092 expected_reg
11093 = (const reg_entry *)str_hash_find (reg_hash, bx[addr_mode]);
fc0763e6 11094
be05d201
L
11095 if (i.base_reg != expected_reg
11096 || i.index_reg
fc0763e6 11097 || operand_type_check (i.types[this_operand], disp))
fc0763e6 11098 {
be05d201
L
11099 /* The second memory operand must have the same size as
11100 the first one. */
11101 if (i.mem_operands
11102 && i.base_reg
11103 && !((addr_mode == CODE_64BIT
dc821c5f 11104 && i.base_reg->reg_type.bitfield.qword)
be05d201 11105 || (addr_mode == CODE_32BIT
dc821c5f
JB
11106 ? i.base_reg->reg_type.bitfield.dword
11107 : i.base_reg->reg_type.bitfield.word)))
be05d201
L
11108 goto bad_address;
11109
fc0763e6
JB
11110 as_warn (_("`%s' is not valid here (expected `%c%s%s%c')"),
11111 operand_string,
11112 intel_syntax ? '[' : '(',
11113 register_prefix,
be05d201 11114 expected_reg->reg_name,
fc0763e6 11115 intel_syntax ? ']' : ')');
be05d201 11116 return 1;
fc0763e6 11117 }
be05d201
L
11118 else
11119 return 1;
11120
dc1e8a47 11121 bad_address:
be05d201
L
11122 as_bad (_("`%s' is not a valid %s expression"),
11123 operand_string, kind);
11124 return 0;
3e73aa7c
JH
11125 }
11126 else
11127 {
be05d201
L
11128 if (addr_mode != CODE_16BIT)
11129 {
11130 /* 32-bit/64-bit checks. */
41eb8e88
L
11131 if (i.disp_encoding == disp_encoding_16bit)
11132 {
11133 bad_disp:
11134 as_bad (_("invalid `%s' prefix"),
11135 addr_mode == CODE_16BIT ? "{disp32}" : "{disp16}");
11136 return 0;
11137 }
11138
be05d201 11139 if ((i.base_reg
e968fc9b
JB
11140 && ((addr_mode == CODE_64BIT
11141 ? !i.base_reg->reg_type.bitfield.qword
11142 : !i.base_reg->reg_type.bitfield.dword)
11143 || (i.index_reg && i.base_reg->reg_num == RegIP)
11144 || i.base_reg->reg_num == RegIZ))
be05d201 11145 || (i.index_reg
1b54b8d7
JB
11146 && !i.index_reg->reg_type.bitfield.xmmword
11147 && !i.index_reg->reg_type.bitfield.ymmword
11148 && !i.index_reg->reg_type.bitfield.zmmword
be05d201 11149 && ((addr_mode == CODE_64BIT
e968fc9b
JB
11150 ? !i.index_reg->reg_type.bitfield.qword
11151 : !i.index_reg->reg_type.bitfield.dword)
be05d201
L
11152 || !i.index_reg->reg_type.bitfield.baseindex)))
11153 goto bad_address;
8178be5b 11154
260cd341 11155 /* bndmk, bndldx, bndstx and mandatory non-vector SIB have special restrictions. */
a152332d 11156 if ((t->opcode_modifier.opcodeprefix == PREFIX_0XF3
389d00a5
JB
11157 && t->opcode_modifier.opcodespace == SPACE_0F
11158 && t->base_opcode == 0x1b)
a152332d 11159 || (t->opcode_modifier.opcodeprefix == PREFIX_NONE
389d00a5
JB
11160 && t->opcode_modifier.opcodespace == SPACE_0F
11161 && (t->base_opcode & ~1) == 0x1a)
a152332d 11162 || t->opcode_modifier.sib == SIBMEM)
8178be5b
JB
11163 {
11164 /* They cannot use RIP-relative addressing. */
e968fc9b 11165 if (i.base_reg && i.base_reg->reg_num == RegIP)
8178be5b
JB
11166 {
11167 as_bad (_("`%s' cannot be used here"), operand_string);
11168 return 0;
11169 }
11170
11171 /* bndldx and bndstx ignore their scale factor. */
a152332d 11172 if (t->opcode_modifier.opcodeprefix == PREFIX_NONE
389d00a5
JB
11173 && t->opcode_modifier.opcodespace == SPACE_0F
11174 && (t->base_opcode & ~1) == 0x1a
8178be5b
JB
11175 && i.log2_scale_factor)
11176 as_warn (_("register scaling is being ignored here"));
11177 }
be05d201
L
11178 }
11179 else
3e73aa7c 11180 {
be05d201 11181 /* 16-bit checks. */
41eb8e88
L
11182 if (i.disp_encoding == disp_encoding_32bit)
11183 goto bad_disp;
11184
3e73aa7c 11185 if ((i.base_reg
dc821c5f 11186 && (!i.base_reg->reg_type.bitfield.word
40fb9820 11187 || !i.base_reg->reg_type.bitfield.baseindex))
3e73aa7c 11188 || (i.index_reg
dc821c5f 11189 && (!i.index_reg->reg_type.bitfield.word
40fb9820 11190 || !i.index_reg->reg_type.bitfield.baseindex
29b0f896
AM
11191 || !(i.base_reg
11192 && i.base_reg->reg_num < 6
11193 && i.index_reg->reg_num >= 6
11194 && i.log2_scale_factor == 0))))
be05d201 11195 goto bad_address;
3e73aa7c
JH
11196 }
11197 }
be05d201 11198 return 1;
24eab124 11199}
252b5132 11200
43234a1e
L
11201/* Handle vector immediates. */
11202
11203static int
11204RC_SAE_immediate (const char *imm_start)
11205{
11206 unsigned int match_found, j;
11207 const char *pstr = imm_start;
11208 expressionS *exp;
11209
11210 if (*pstr != '{')
11211 return 0;
11212
11213 pstr++;
11214 match_found = 0;
11215 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); j++)
11216 {
11217 if (!strncmp (pstr, RC_NamesTable[j].name, RC_NamesTable[j].len))
11218 {
ca5312a2 11219 if (i.rounding.type != rc_none)
43234a1e
L
11220 {
11221 as_bad (_("duplicated `%s'"), imm_start);
11222 return 0;
11223 }
ca5312a2
JB
11224
11225 i.rounding.type = RC_NamesTable[j].type;
11226 i.rounding.operand = this_operand;
11227
43234a1e
L
11228 pstr += RC_NamesTable[j].len;
11229 match_found = 1;
11230 break;
11231 }
11232 }
11233 if (!match_found)
11234 return 0;
11235
11236 if (*pstr++ != '}')
11237 {
11238 as_bad (_("Missing '}': '%s'"), imm_start);
11239 return 0;
11240 }
11241 /* RC/SAE immediate string should contain nothing more. */;
11242 if (*pstr != 0)
11243 {
11244 as_bad (_("Junk after '}': '%s'"), imm_start);
11245 return 0;
11246 }
11247
11248 exp = &im_expressions[i.imm_operands++];
11249 i.op[this_operand].imms = exp;
11250
11251 exp->X_op = O_constant;
11252 exp->X_add_number = 0;
11253 exp->X_add_symbol = (symbolS *) 0;
11254 exp->X_op_symbol = (symbolS *) 0;
11255
11256 i.types[this_operand].bitfield.imm8 = 1;
11257 return 1;
11258}
11259
8325cc63
JB
11260/* Only string instructions can have a second memory operand, so
11261 reduce current_templates to just those if it contains any. */
11262static int
11263maybe_adjust_templates (void)
11264{
11265 const insn_template *t;
11266
11267 gas_assert (i.mem_operands == 1);
11268
11269 for (t = current_templates->start; t < current_templates->end; ++t)
11270 if (t->opcode_modifier.isstring)
11271 break;
11272
11273 if (t < current_templates->end)
11274 {
11275 static templates aux_templates;
5b7c81bd 11276 bool recheck;
8325cc63
JB
11277
11278 aux_templates.start = t;
11279 for (; t < current_templates->end; ++t)
11280 if (!t->opcode_modifier.isstring)
11281 break;
11282 aux_templates.end = t;
11283
11284 /* Determine whether to re-check the first memory operand. */
11285 recheck = (aux_templates.start != current_templates->start
11286 || t != current_templates->end);
11287
11288 current_templates = &aux_templates;
11289
11290 if (recheck)
11291 {
11292 i.mem_operands = 0;
11293 if (i.memop1_string != NULL
11294 && i386_index_check (i.memop1_string) == 0)
11295 return 0;
11296 i.mem_operands = 1;
11297 }
11298 }
11299
11300 return 1;
11301}
11302
fc0763e6 11303/* Parse OPERAND_STRING into the i386_insn structure I. Returns zero
47926f60 11304 on error. */
252b5132 11305
252b5132 11306static int
a7619375 11307i386_att_operand (char *operand_string)
252b5132 11308{
af6bdddf
AM
11309 const reg_entry *r;
11310 char *end_op;
24eab124 11311 char *op_string = operand_string;
252b5132 11312
24eab124 11313 if (is_space_char (*op_string))
252b5132
RH
11314 ++op_string;
11315
24eab124 11316 /* We check for an absolute prefix (differentiating,
47926f60 11317 for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
24eab124
AM
11318 if (*op_string == ABSOLUTE_PREFIX)
11319 {
11320 ++op_string;
11321 if (is_space_char (*op_string))
11322 ++op_string;
5b7c81bd 11323 i.jumpabsolute = true;
24eab124 11324 }
252b5132 11325
47926f60 11326 /* Check if operand is a register. */
4d1bb795 11327 if ((r = parse_register (op_string, &end_op)) != NULL)
24eab124 11328 {
40fb9820
L
11329 i386_operand_type temp;
11330
8a6fb3f9
JB
11331 if (r == &bad_reg)
11332 return 0;
11333
24eab124
AM
11334 /* Check for a segment override by searching for ':' after a
11335 segment register. */
11336 op_string = end_op;
11337 if (is_space_char (*op_string))
11338 ++op_string;
00cee14f 11339 if (*op_string == ':' && r->reg_type.bitfield.class == SReg)
24eab124 11340 {
5e042380 11341 i.seg[i.mem_operands] = r;
252b5132 11342
24eab124 11343 /* Skip the ':' and whitespace. */
252b5132
RH
11344 ++op_string;
11345 if (is_space_char (*op_string))
24eab124 11346 ++op_string;
252b5132 11347
24eab124
AM
11348 if (!is_digit_char (*op_string)
11349 && !is_identifier_char (*op_string)
11350 && *op_string != '('
11351 && *op_string != ABSOLUTE_PREFIX)
11352 {
11353 as_bad (_("bad memory operand `%s'"), op_string);
11354 return 0;
11355 }
47926f60 11356 /* Handle case of %es:*foo. */
24eab124
AM
11357 if (*op_string == ABSOLUTE_PREFIX)
11358 {
11359 ++op_string;
11360 if (is_space_char (*op_string))
11361 ++op_string;
5b7c81bd 11362 i.jumpabsolute = true;
24eab124
AM
11363 }
11364 goto do_memory_reference;
11365 }
43234a1e
L
11366
11367 /* Handle vector operations. */
11368 if (*op_string == '{')
11369 {
11370 op_string = check_VecOperations (op_string, NULL);
11371 if (op_string == NULL)
11372 return 0;
11373 }
11374
24eab124
AM
11375 if (*op_string)
11376 {
d0b47220 11377 as_bad (_("junk `%s' after register"), op_string);
24eab124
AM
11378 return 0;
11379 }
40fb9820
L
11380 temp = r->reg_type;
11381 temp.bitfield.baseindex = 0;
c6fb90c8
L
11382 i.types[this_operand] = operand_type_or (i.types[this_operand],
11383 temp);
7d5e4556 11384 i.types[this_operand].bitfield.unspecified = 0;
520dc8e8 11385 i.op[this_operand].regs = r;
24eab124
AM
11386 i.reg_operands++;
11387 }
af6bdddf
AM
11388 else if (*op_string == REGISTER_PREFIX)
11389 {
11390 as_bad (_("bad register name `%s'"), op_string);
11391 return 0;
11392 }
24eab124 11393 else if (*op_string == IMMEDIATE_PREFIX)
ce8a8b2f 11394 {
24eab124 11395 ++op_string;
6f2f06be 11396 if (i.jumpabsolute)
24eab124 11397 {
d0b47220 11398 as_bad (_("immediate operand illegal with absolute jump"));
24eab124
AM
11399 return 0;
11400 }
11401 if (!i386_immediate (op_string))
11402 return 0;
11403 }
43234a1e
L
11404 else if (RC_SAE_immediate (operand_string))
11405 {
11406 /* If it is a RC or SAE immediate, do nothing. */
11407 ;
11408 }
24eab124
AM
11409 else if (is_digit_char (*op_string)
11410 || is_identifier_char (*op_string)
d02603dc 11411 || *op_string == '"'
e5cb08ac 11412 || *op_string == '(')
24eab124 11413 {
47926f60 11414 /* This is a memory reference of some sort. */
af6bdddf 11415 char *base_string;
252b5132 11416
47926f60 11417 /* Start and end of displacement string expression (if found). */
eecb386c
AM
11418 char *displacement_string_start;
11419 char *displacement_string_end;
43234a1e 11420 char *vop_start;
252b5132 11421
24eab124 11422 do_memory_reference:
8325cc63
JB
11423 if (i.mem_operands == 1 && !maybe_adjust_templates ())
11424 return 0;
24eab124 11425 if ((i.mem_operands == 1
40fb9820 11426 && !current_templates->start->opcode_modifier.isstring)
24eab124
AM
11427 || i.mem_operands == 2)
11428 {
11429 as_bad (_("too many memory references for `%s'"),
11430 current_templates->start->name);
11431 return 0;
11432 }
252b5132 11433
24eab124
AM
11434 /* Check for base index form. We detect the base index form by
11435 looking for an ')' at the end of the operand, searching
11436 for the '(' matching it, and finding a REGISTER_PREFIX or ','
11437 after the '('. */
af6bdddf 11438 base_string = op_string + strlen (op_string);
c3332e24 11439
43234a1e
L
11440 /* Handle vector operations. */
11441 vop_start = strchr (op_string, '{');
11442 if (vop_start && vop_start < base_string)
11443 {
11444 if (check_VecOperations (vop_start, base_string) == NULL)
11445 return 0;
11446 base_string = vop_start;
11447 }
11448
af6bdddf
AM
11449 --base_string;
11450 if (is_space_char (*base_string))
11451 --base_string;
252b5132 11452
47926f60 11453 /* If we only have a displacement, set-up for it to be parsed later. */
af6bdddf
AM
11454 displacement_string_start = op_string;
11455 displacement_string_end = base_string + 1;
252b5132 11456
24eab124
AM
11457 if (*base_string == ')')
11458 {
af6bdddf 11459 char *temp_string;
24eab124
AM
11460 unsigned int parens_balanced = 1;
11461 /* We've already checked that the number of left & right ()'s are
47926f60 11462 equal, so this loop will not be infinite. */
24eab124
AM
11463 do
11464 {
11465 base_string--;
11466 if (*base_string == ')')
11467 parens_balanced++;
11468 if (*base_string == '(')
11469 parens_balanced--;
11470 }
11471 while (parens_balanced);
c3332e24 11472
af6bdddf 11473 temp_string = base_string;
c3332e24 11474
24eab124 11475 /* Skip past '(' and whitespace. */
252b5132
RH
11476 ++base_string;
11477 if (is_space_char (*base_string))
24eab124 11478 ++base_string;
252b5132 11479
af6bdddf 11480 if (*base_string == ','
4eed87de
AM
11481 || ((i.base_reg = parse_register (base_string, &end_op))
11482 != NULL))
252b5132 11483 {
af6bdddf 11484 displacement_string_end = temp_string;
252b5132 11485
40fb9820 11486 i.types[this_operand].bitfield.baseindex = 1;
252b5132 11487
af6bdddf 11488 if (i.base_reg)
24eab124 11489 {
8a6fb3f9
JB
11490 if (i.base_reg == &bad_reg)
11491 return 0;
24eab124
AM
11492 base_string = end_op;
11493 if (is_space_char (*base_string))
11494 ++base_string;
af6bdddf
AM
11495 }
11496
11497 /* There may be an index reg or scale factor here. */
11498 if (*base_string == ',')
11499 {
11500 ++base_string;
11501 if (is_space_char (*base_string))
11502 ++base_string;
11503
4eed87de
AM
11504 if ((i.index_reg = parse_register (base_string, &end_op))
11505 != NULL)
24eab124 11506 {
8a6fb3f9
JB
11507 if (i.index_reg == &bad_reg)
11508 return 0;
af6bdddf 11509 base_string = end_op;
24eab124
AM
11510 if (is_space_char (*base_string))
11511 ++base_string;
af6bdddf
AM
11512 if (*base_string == ',')
11513 {
11514 ++base_string;
11515 if (is_space_char (*base_string))
11516 ++base_string;
11517 }
e5cb08ac 11518 else if (*base_string != ')')
af6bdddf 11519 {
4eed87de
AM
11520 as_bad (_("expecting `,' or `)' "
11521 "after index register in `%s'"),
af6bdddf
AM
11522 operand_string);
11523 return 0;
11524 }
24eab124 11525 }
af6bdddf 11526 else if (*base_string == REGISTER_PREFIX)
24eab124 11527 {
f76bf5e0
L
11528 end_op = strchr (base_string, ',');
11529 if (end_op)
11530 *end_op = '\0';
af6bdddf 11531 as_bad (_("bad register name `%s'"), base_string);
24eab124
AM
11532 return 0;
11533 }
252b5132 11534
47926f60 11535 /* Check for scale factor. */
551c1ca1 11536 if (*base_string != ')')
af6bdddf 11537 {
551c1ca1
AM
11538 char *end_scale = i386_scale (base_string);
11539
11540 if (!end_scale)
af6bdddf 11541 return 0;
24eab124 11542
551c1ca1 11543 base_string = end_scale;
af6bdddf
AM
11544 if (is_space_char (*base_string))
11545 ++base_string;
11546 if (*base_string != ')')
11547 {
4eed87de
AM
11548 as_bad (_("expecting `)' "
11549 "after scale factor in `%s'"),
af6bdddf
AM
11550 operand_string);
11551 return 0;
11552 }
11553 }
11554 else if (!i.index_reg)
24eab124 11555 {
4eed87de
AM
11556 as_bad (_("expecting index register or scale factor "
11557 "after `,'; got '%c'"),
af6bdddf 11558 *base_string);
24eab124
AM
11559 return 0;
11560 }
11561 }
af6bdddf 11562 else if (*base_string != ')')
24eab124 11563 {
4eed87de
AM
11564 as_bad (_("expecting `,' or `)' "
11565 "after base register in `%s'"),
af6bdddf 11566 operand_string);
24eab124
AM
11567 return 0;
11568 }
c3332e24 11569 }
af6bdddf 11570 else if (*base_string == REGISTER_PREFIX)
c3332e24 11571 {
f76bf5e0
L
11572 end_op = strchr (base_string, ',');
11573 if (end_op)
11574 *end_op = '\0';
af6bdddf 11575 as_bad (_("bad register name `%s'"), base_string);
24eab124 11576 return 0;
c3332e24 11577 }
24eab124
AM
11578 }
11579
11580 /* If there's an expression beginning the operand, parse it,
11581 assuming displacement_string_start and
11582 displacement_string_end are meaningful. */
11583 if (displacement_string_start != displacement_string_end)
11584 {
11585 if (!i386_displacement (displacement_string_start,
11586 displacement_string_end))
11587 return 0;
11588 }
11589
11590 /* Special case for (%dx) while doing input/output op. */
11591 if (i.base_reg
75e5731b
JB
11592 && i.base_reg->reg_type.bitfield.instance == RegD
11593 && i.base_reg->reg_type.bitfield.word
24eab124
AM
11594 && i.index_reg == 0
11595 && i.log2_scale_factor == 0
11596 && i.seg[i.mem_operands] == 0
40fb9820 11597 && !operand_type_check (i.types[this_operand], disp))
24eab124 11598 {
2fb5be8d 11599 i.types[this_operand] = i.base_reg->reg_type;
24eab124
AM
11600 return 1;
11601 }
11602
eecb386c
AM
11603 if (i386_index_check (operand_string) == 0)
11604 return 0;
c48dadc9 11605 i.flags[this_operand] |= Operand_Mem;
8325cc63
JB
11606 if (i.mem_operands == 0)
11607 i.memop1_string = xstrdup (operand_string);
24eab124
AM
11608 i.mem_operands++;
11609 }
11610 else
ce8a8b2f
AM
11611 {
11612 /* It's not a memory operand; argh! */
24eab124
AM
11613 as_bad (_("invalid char %s beginning operand %d `%s'"),
11614 output_invalid (*op_string),
11615 this_operand + 1,
11616 op_string);
11617 return 0;
11618 }
47926f60 11619 return 1; /* Normal return. */
252b5132
RH
11620}
11621\f
fa94de6b
RM
11622/* Calculate the maximum variable size (i.e., excluding fr_fix)
11623 that an rs_machine_dependent frag may reach. */
11624
11625unsigned int
11626i386_frag_max_var (fragS *frag)
11627{
11628 /* The only relaxable frags are for jumps.
11629 Unconditional jumps can grow by 4 bytes and others by 5 bytes. */
11630 gas_assert (frag->fr_type == rs_machine_dependent);
11631 return TYPE_FROM_RELAX_STATE (frag->fr_subtype) == UNCOND_JUMP ? 4 : 5;
11632}
11633
b084df0b
L
11634#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
11635static int
8dcea932 11636elf_symbol_resolved_in_segment_p (symbolS *fr_symbol, offsetT fr_var)
b084df0b
L
11637{
11638 /* STT_GNU_IFUNC symbol must go through PLT. */
11639 if ((symbol_get_bfdsym (fr_symbol)->flags
11640 & BSF_GNU_INDIRECT_FUNCTION) != 0)
11641 return 0;
11642
11643 if (!S_IS_EXTERNAL (fr_symbol))
11644 /* Symbol may be weak or local. */
11645 return !S_IS_WEAK (fr_symbol);
11646
8dcea932
L
11647 /* Global symbols with non-default visibility can't be preempted. */
11648 if (ELF_ST_VISIBILITY (S_GET_OTHER (fr_symbol)) != STV_DEFAULT)
11649 return 1;
11650
11651 if (fr_var != NO_RELOC)
11652 switch ((enum bfd_reloc_code_real) fr_var)
11653 {
11654 case BFD_RELOC_386_PLT32:
11655 case BFD_RELOC_X86_64_PLT32:
33eaf5de 11656 /* Symbol with PLT relocation may be preempted. */
8dcea932
L
11657 return 0;
11658 default:
11659 abort ();
11660 }
11661
b084df0b
L
11662 /* Global symbols with default visibility in a shared library may be
11663 preempted by another definition. */
8dcea932 11664 return !shared;
b084df0b
L
11665}
11666#endif
11667
79d72f45
HL
11668/* Table 3-2. Macro-Fusible Instructions in Haswell Microarchitecture
11669 Note also work for Skylake and Cascadelake.
11670---------------------------------------------------------------------
11671| JCC | ADD/SUB/CMP | INC/DEC | TEST/AND |
11672| ------ | ----------- | ------- | -------- |
11673| Jo | N | N | Y |
11674| Jno | N | N | Y |
11675| Jc/Jb | Y | N | Y |
11676| Jae/Jnb | Y | N | Y |
11677| Je/Jz | Y | Y | Y |
11678| Jne/Jnz | Y | Y | Y |
11679| Jna/Jbe | Y | N | Y |
11680| Ja/Jnbe | Y | N | Y |
11681| Js | N | N | Y |
11682| Jns | N | N | Y |
11683| Jp/Jpe | N | N | Y |
11684| Jnp/Jpo | N | N | Y |
11685| Jl/Jnge | Y | Y | Y |
11686| Jge/Jnl | Y | Y | Y |
11687| Jle/Jng | Y | Y | Y |
11688| Jg/Jnle | Y | Y | Y |
11689--------------------------------------------------------------------- */
11690static int
11691i386_macro_fusible_p (enum mf_cmp_kind mf_cmp, enum mf_jcc_kind mf_jcc)
11692{
11693 if (mf_cmp == mf_cmp_alu_cmp)
11694 return ((mf_jcc >= mf_jcc_jc && mf_jcc <= mf_jcc_jna)
11695 || mf_jcc == mf_jcc_jl || mf_jcc == mf_jcc_jle);
11696 if (mf_cmp == mf_cmp_incdec)
11697 return (mf_jcc == mf_jcc_je || mf_jcc == mf_jcc_jl
11698 || mf_jcc == mf_jcc_jle);
11699 if (mf_cmp == mf_cmp_test_and)
11700 return 1;
11701 return 0;
11702}
11703
e379e5f3
L
11704/* Return the next non-empty frag. */
11705
11706static fragS *
11707i386_next_non_empty_frag (fragS *fragP)
11708{
11709 /* There may be a frag with a ".fill 0" when there is no room in
11710 the current frag for frag_grow in output_insn. */
11711 for (fragP = fragP->fr_next;
11712 (fragP != NULL
11713 && fragP->fr_type == rs_fill
11714 && fragP->fr_fix == 0);
11715 fragP = fragP->fr_next)
11716 ;
11717 return fragP;
11718}
11719
11720/* Return the next jcc frag after BRANCH_PADDING. */
11721
11722static fragS *
79d72f45 11723i386_next_fusible_jcc_frag (fragS *maybe_cmp_fragP, fragS *pad_fragP)
e379e5f3 11724{
79d72f45
HL
11725 fragS *branch_fragP;
11726 if (!pad_fragP)
e379e5f3
L
11727 return NULL;
11728
79d72f45
HL
11729 if (pad_fragP->fr_type == rs_machine_dependent
11730 && (TYPE_FROM_RELAX_STATE (pad_fragP->fr_subtype)
e379e5f3
L
11731 == BRANCH_PADDING))
11732 {
79d72f45
HL
11733 branch_fragP = i386_next_non_empty_frag (pad_fragP);
11734 if (branch_fragP->fr_type != rs_machine_dependent)
e379e5f3 11735 return NULL;
79d72f45
HL
11736 if (TYPE_FROM_RELAX_STATE (branch_fragP->fr_subtype) == COND_JUMP
11737 && i386_macro_fusible_p (maybe_cmp_fragP->tc_frag_data.mf_type,
11738 pad_fragP->tc_frag_data.mf_type))
11739 return branch_fragP;
e379e5f3
L
11740 }
11741
11742 return NULL;
11743}
11744
11745/* Classify BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags. */
11746
11747static void
11748i386_classify_machine_dependent_frag (fragS *fragP)
11749{
11750 fragS *cmp_fragP;
11751 fragS *pad_fragP;
11752 fragS *branch_fragP;
11753 fragS *next_fragP;
11754 unsigned int max_prefix_length;
11755
11756 if (fragP->tc_frag_data.classified)
11757 return;
11758
11759 /* First scan for BRANCH_PADDING and FUSED_JCC_PADDING. Convert
11760 FUSED_JCC_PADDING and merge BRANCH_PADDING. */
11761 for (next_fragP = fragP;
11762 next_fragP != NULL;
11763 next_fragP = next_fragP->fr_next)
11764 {
11765 next_fragP->tc_frag_data.classified = 1;
11766 if (next_fragP->fr_type == rs_machine_dependent)
11767 switch (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype))
11768 {
11769 case BRANCH_PADDING:
11770 /* The BRANCH_PADDING frag must be followed by a branch
11771 frag. */
11772 branch_fragP = i386_next_non_empty_frag (next_fragP);
11773 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
11774 break;
11775 case FUSED_JCC_PADDING:
11776 /* Check if this is a fused jcc:
11777 FUSED_JCC_PADDING
11778 CMP like instruction
11779 BRANCH_PADDING
11780 COND_JUMP
11781 */
11782 cmp_fragP = i386_next_non_empty_frag (next_fragP);
11783 pad_fragP = i386_next_non_empty_frag (cmp_fragP);
79d72f45 11784 branch_fragP = i386_next_fusible_jcc_frag (next_fragP, pad_fragP);
e379e5f3
L
11785 if (branch_fragP)
11786 {
11787 /* The BRANCH_PADDING frag is merged with the
11788 FUSED_JCC_PADDING frag. */
11789 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
11790 /* CMP like instruction size. */
11791 next_fragP->tc_frag_data.cmp_size = cmp_fragP->fr_fix;
11792 frag_wane (pad_fragP);
11793 /* Skip to branch_fragP. */
11794 next_fragP = branch_fragP;
11795 }
11796 else if (next_fragP->tc_frag_data.max_prefix_length)
11797 {
11798 /* Turn FUSED_JCC_PADDING into BRANCH_PREFIX if it isn't
11799 a fused jcc. */
11800 next_fragP->fr_subtype
11801 = ENCODE_RELAX_STATE (BRANCH_PREFIX, 0);
11802 next_fragP->tc_frag_data.max_bytes
11803 = next_fragP->tc_frag_data.max_prefix_length;
11804 /* This will be updated in the BRANCH_PREFIX scan. */
11805 next_fragP->tc_frag_data.max_prefix_length = 0;
11806 }
11807 else
11808 frag_wane (next_fragP);
11809 break;
11810 }
11811 }
11812
11813 /* Stop if there is no BRANCH_PREFIX. */
11814 if (!align_branch_prefix_size)
11815 return;
11816
11817 /* Scan for BRANCH_PREFIX. */
11818 for (; fragP != NULL; fragP = fragP->fr_next)
11819 {
11820 if (fragP->fr_type != rs_machine_dependent
11821 || (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
11822 != BRANCH_PREFIX))
11823 continue;
11824
11825 /* Count all BRANCH_PREFIX frags before BRANCH_PADDING and
11826 COND_JUMP_PREFIX. */
11827 max_prefix_length = 0;
11828 for (next_fragP = fragP;
11829 next_fragP != NULL;
11830 next_fragP = next_fragP->fr_next)
11831 {
11832 if (next_fragP->fr_type == rs_fill)
11833 /* Skip rs_fill frags. */
11834 continue;
11835 else if (next_fragP->fr_type != rs_machine_dependent)
11836 /* Stop for all other frags. */
11837 break;
11838
11839 /* rs_machine_dependent frags. */
11840 if (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
11841 == BRANCH_PREFIX)
11842 {
11843 /* Count BRANCH_PREFIX frags. */
11844 if (max_prefix_length >= MAX_FUSED_JCC_PADDING_SIZE)
11845 {
11846 max_prefix_length = MAX_FUSED_JCC_PADDING_SIZE;
11847 frag_wane (next_fragP);
11848 }
11849 else
11850 max_prefix_length
11851 += next_fragP->tc_frag_data.max_bytes;
11852 }
11853 else if ((TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
11854 == BRANCH_PADDING)
11855 || (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
11856 == FUSED_JCC_PADDING))
11857 {
11858 /* Stop at BRANCH_PADDING and FUSED_JCC_PADDING. */
11859 fragP->tc_frag_data.u.padding_fragP = next_fragP;
11860 break;
11861 }
11862 else
11863 /* Stop for other rs_machine_dependent frags. */
11864 break;
11865 }
11866
11867 fragP->tc_frag_data.max_prefix_length = max_prefix_length;
11868
11869 /* Skip to the next frag. */
11870 fragP = next_fragP;
11871 }
11872}
11873
11874/* Compute padding size for
11875
11876 FUSED_JCC_PADDING
11877 CMP like instruction
11878 BRANCH_PADDING
11879 COND_JUMP/UNCOND_JUMP
11880
11881 or
11882
11883 BRANCH_PADDING
11884 COND_JUMP/UNCOND_JUMP
11885 */
11886
11887static int
11888i386_branch_padding_size (fragS *fragP, offsetT address)
11889{
11890 unsigned int offset, size, padding_size;
11891 fragS *branch_fragP = fragP->tc_frag_data.u.branch_fragP;
11892
11893 /* The start address of the BRANCH_PADDING or FUSED_JCC_PADDING frag. */
11894 if (!address)
11895 address = fragP->fr_address;
11896 address += fragP->fr_fix;
11897
11898 /* CMP like instrunction size. */
11899 size = fragP->tc_frag_data.cmp_size;
11900
11901 /* The base size of the branch frag. */
11902 size += branch_fragP->fr_fix;
11903
11904 /* Add opcode and displacement bytes for the rs_machine_dependent
11905 branch frag. */
11906 if (branch_fragP->fr_type == rs_machine_dependent)
11907 size += md_relax_table[branch_fragP->fr_subtype].rlx_length;
11908
11909 /* Check if branch is within boundary and doesn't end at the last
11910 byte. */
11911 offset = address & ((1U << align_branch_power) - 1);
11912 if ((offset + size) >= (1U << align_branch_power))
11913 /* Padding needed to avoid crossing boundary. */
11914 padding_size = (1U << align_branch_power) - offset;
11915 else
11916 /* No padding needed. */
11917 padding_size = 0;
11918
11919 /* The return value may be saved in tc_frag_data.length which is
11920 unsigned byte. */
11921 if (!fits_in_unsigned_byte (padding_size))
11922 abort ();
11923
11924 return padding_size;
11925}
11926
11927/* i386_generic_table_relax_frag()
11928
11929 Handle BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags to
11930 grow/shrink padding to align branch frags. Hand others to
11931 relax_frag(). */
11932
11933long
11934i386_generic_table_relax_frag (segT segment, fragS *fragP, long stretch)
11935{
11936 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
11937 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
11938 {
11939 long padding_size = i386_branch_padding_size (fragP, 0);
11940 long grow = padding_size - fragP->tc_frag_data.length;
11941
11942 /* When the BRANCH_PREFIX frag is used, the computed address
11943 must match the actual address and there should be no padding. */
11944 if (fragP->tc_frag_data.padding_address
11945 && (fragP->tc_frag_data.padding_address != fragP->fr_address
11946 || padding_size))
11947 abort ();
11948
11949 /* Update the padding size. */
11950 if (grow)
11951 fragP->tc_frag_data.length = padding_size;
11952
11953 return grow;
11954 }
11955 else if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
11956 {
11957 fragS *padding_fragP, *next_fragP;
11958 long padding_size, left_size, last_size;
11959
11960 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
11961 if (!padding_fragP)
11962 /* Use the padding set by the leading BRANCH_PREFIX frag. */
11963 return (fragP->tc_frag_data.length
11964 - fragP->tc_frag_data.last_length);
11965
11966 /* Compute the relative address of the padding frag in the very
11967 first time where the BRANCH_PREFIX frag sizes are zero. */
11968 if (!fragP->tc_frag_data.padding_address)
11969 fragP->tc_frag_data.padding_address
11970 = padding_fragP->fr_address - (fragP->fr_address - stretch);
11971
11972 /* First update the last length from the previous interation. */
11973 left_size = fragP->tc_frag_data.prefix_length;
11974 for (next_fragP = fragP;
11975 next_fragP != padding_fragP;
11976 next_fragP = next_fragP->fr_next)
11977 if (next_fragP->fr_type == rs_machine_dependent
11978 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
11979 == BRANCH_PREFIX))
11980 {
11981 if (left_size)
11982 {
11983 int max = next_fragP->tc_frag_data.max_bytes;
11984 if (max)
11985 {
11986 int size;
11987 if (max > left_size)
11988 size = left_size;
11989 else
11990 size = max;
11991 left_size -= size;
11992 next_fragP->tc_frag_data.last_length = size;
11993 }
11994 }
11995 else
11996 next_fragP->tc_frag_data.last_length = 0;
11997 }
11998
11999 /* Check the padding size for the padding frag. */
12000 padding_size = i386_branch_padding_size
12001 (padding_fragP, (fragP->fr_address
12002 + fragP->tc_frag_data.padding_address));
12003
12004 last_size = fragP->tc_frag_data.prefix_length;
12005 /* Check if there is change from the last interation. */
12006 if (padding_size == last_size)
12007 {
12008 /* Update the expected address of the padding frag. */
12009 padding_fragP->tc_frag_data.padding_address
12010 = (fragP->fr_address + padding_size
12011 + fragP->tc_frag_data.padding_address);
12012 return 0;
12013 }
12014
12015 if (padding_size > fragP->tc_frag_data.max_prefix_length)
12016 {
12017 /* No padding if there is no sufficient room. Clear the
12018 expected address of the padding frag. */
12019 padding_fragP->tc_frag_data.padding_address = 0;
12020 padding_size = 0;
12021 }
12022 else
12023 /* Store the expected address of the padding frag. */
12024 padding_fragP->tc_frag_data.padding_address
12025 = (fragP->fr_address + padding_size
12026 + fragP->tc_frag_data.padding_address);
12027
12028 fragP->tc_frag_data.prefix_length = padding_size;
12029
12030 /* Update the length for the current interation. */
12031 left_size = padding_size;
12032 for (next_fragP = fragP;
12033 next_fragP != padding_fragP;
12034 next_fragP = next_fragP->fr_next)
12035 if (next_fragP->fr_type == rs_machine_dependent
12036 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12037 == BRANCH_PREFIX))
12038 {
12039 if (left_size)
12040 {
12041 int max = next_fragP->tc_frag_data.max_bytes;
12042 if (max)
12043 {
12044 int size;
12045 if (max > left_size)
12046 size = left_size;
12047 else
12048 size = max;
12049 left_size -= size;
12050 next_fragP->tc_frag_data.length = size;
12051 }
12052 }
12053 else
12054 next_fragP->tc_frag_data.length = 0;
12055 }
12056
12057 return (fragP->tc_frag_data.length
12058 - fragP->tc_frag_data.last_length);
12059 }
12060 return relax_frag (segment, fragP, stretch);
12061}
12062
ee7fcc42
AM
12063/* md_estimate_size_before_relax()
12064
12065 Called just before relax() for rs_machine_dependent frags. The x86
12066 assembler uses these frags to handle variable size jump
12067 instructions.
12068
12069 Any symbol that is now undefined will not become defined.
12070 Return the correct fr_subtype in the frag.
12071 Return the initial "guess for variable size of frag" to caller.
12072 The guess is actually the growth beyond the fixed part. Whatever
12073 we do to grow the fixed or variable part contributes to our
12074 returned value. */
12075
252b5132 12076int
7016a5d5 12077md_estimate_size_before_relax (fragS *fragP, segT segment)
252b5132 12078{
e379e5f3
L
12079 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12080 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX
12081 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
12082 {
12083 i386_classify_machine_dependent_frag (fragP);
12084 return fragP->tc_frag_data.length;
12085 }
12086
252b5132 12087 /* We've already got fragP->fr_subtype right; all we have to do is
b98ef147
AM
12088 check for un-relaxable symbols. On an ELF system, we can't relax
12089 an externally visible symbol, because it may be overridden by a
12090 shared library. */
12091 if (S_GET_SEGMENT (fragP->fr_symbol) != segment
6d249963 12092#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12093 || (IS_ELF
8dcea932
L
12094 && !elf_symbol_resolved_in_segment_p (fragP->fr_symbol,
12095 fragP->fr_var))
fbeb56a4
DK
12096#endif
12097#if defined (OBJ_COFF) && defined (TE_PE)
7ab9ffdd 12098 || (OUTPUT_FLAVOR == bfd_target_coff_flavour
fbeb56a4 12099 && S_IS_WEAK (fragP->fr_symbol))
b98ef147
AM
12100#endif
12101 )
252b5132 12102 {
b98ef147
AM
12103 /* Symbol is undefined in this segment, or we need to keep a
12104 reloc so that weak symbols can be overridden. */
12105 int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
f86103b7 12106 enum bfd_reloc_code_real reloc_type;
ee7fcc42
AM
12107 unsigned char *opcode;
12108 int old_fr_fix;
f6af82bd 12109
ee7fcc42 12110 if (fragP->fr_var != NO_RELOC)
1e9cc1c2 12111 reloc_type = (enum bfd_reloc_code_real) fragP->fr_var;
b98ef147 12112 else if (size == 2)
f6af82bd 12113 reloc_type = BFD_RELOC_16_PCREL;
bd7ab16b
L
12114#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
12115 else if (need_plt32_p (fragP->fr_symbol))
12116 reloc_type = BFD_RELOC_X86_64_PLT32;
12117#endif
f6af82bd
AM
12118 else
12119 reloc_type = BFD_RELOC_32_PCREL;
252b5132 12120
ee7fcc42
AM
12121 old_fr_fix = fragP->fr_fix;
12122 opcode = (unsigned char *) fragP->fr_opcode;
12123
fddf5b5b 12124 switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
252b5132 12125 {
fddf5b5b
AM
12126 case UNCOND_JUMP:
12127 /* Make jmp (0xeb) a (d)word displacement jump. */
47926f60 12128 opcode[0] = 0xe9;
252b5132 12129 fragP->fr_fix += size;
062cd5e7
AS
12130 fix_new (fragP, old_fr_fix, size,
12131 fragP->fr_symbol,
12132 fragP->fr_offset, 1,
12133 reloc_type);
252b5132
RH
12134 break;
12135
fddf5b5b 12136 case COND_JUMP86:
412167cb
AM
12137 if (size == 2
12138 && (!no_cond_jump_promotion || fragP->fr_var != NO_RELOC))
fddf5b5b
AM
12139 {
12140 /* Negate the condition, and branch past an
12141 unconditional jump. */
12142 opcode[0] ^= 1;
12143 opcode[1] = 3;
12144 /* Insert an unconditional jump. */
12145 opcode[2] = 0xe9;
12146 /* We added two extra opcode bytes, and have a two byte
12147 offset. */
12148 fragP->fr_fix += 2 + 2;
062cd5e7
AS
12149 fix_new (fragP, old_fr_fix + 2, 2,
12150 fragP->fr_symbol,
12151 fragP->fr_offset, 1,
12152 reloc_type);
fddf5b5b
AM
12153 break;
12154 }
12155 /* Fall through. */
12156
12157 case COND_JUMP:
412167cb
AM
12158 if (no_cond_jump_promotion && fragP->fr_var == NO_RELOC)
12159 {
3e02c1cc
AM
12160 fixS *fixP;
12161
412167cb 12162 fragP->fr_fix += 1;
3e02c1cc
AM
12163 fixP = fix_new (fragP, old_fr_fix, 1,
12164 fragP->fr_symbol,
12165 fragP->fr_offset, 1,
12166 BFD_RELOC_8_PCREL);
12167 fixP->fx_signed = 1;
412167cb
AM
12168 break;
12169 }
93c2a809 12170
24eab124 12171 /* This changes the byte-displacement jump 0x7N
fddf5b5b 12172 to the (d)word-displacement jump 0x0f,0x8N. */
252b5132 12173 opcode[1] = opcode[0] + 0x10;
f6af82bd 12174 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
47926f60
KH
12175 /* We've added an opcode byte. */
12176 fragP->fr_fix += 1 + size;
062cd5e7
AS
12177 fix_new (fragP, old_fr_fix + 1, size,
12178 fragP->fr_symbol,
12179 fragP->fr_offset, 1,
12180 reloc_type);
252b5132 12181 break;
fddf5b5b
AM
12182
12183 default:
12184 BAD_CASE (fragP->fr_subtype);
12185 break;
252b5132
RH
12186 }
12187 frag_wane (fragP);
ee7fcc42 12188 return fragP->fr_fix - old_fr_fix;
252b5132 12189 }
93c2a809 12190
93c2a809
AM
12191 /* Guess size depending on current relax state. Initially the relax
12192 state will correspond to a short jump and we return 1, because
12193 the variable part of the frag (the branch offset) is one byte
12194 long. However, we can relax a section more than once and in that
12195 case we must either set fr_subtype back to the unrelaxed state,
12196 or return the value for the appropriate branch. */
12197 return md_relax_table[fragP->fr_subtype].rlx_length;
ee7fcc42
AM
12198}
12199
47926f60
KH
12200/* Called after relax() is finished.
12201
12202 In: Address of frag.
12203 fr_type == rs_machine_dependent.
12204 fr_subtype is what the address relaxed to.
12205
12206 Out: Any fixSs and constants are set up.
12207 Caller will turn frag into a ".space 0". */
12208
252b5132 12209void
7016a5d5
TG
12210md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec ATTRIBUTE_UNUSED,
12211 fragS *fragP)
252b5132 12212{
29b0f896 12213 unsigned char *opcode;
252b5132 12214 unsigned char *where_to_put_displacement = NULL;
847f7ad4
AM
12215 offsetT target_address;
12216 offsetT opcode_address;
252b5132 12217 unsigned int extension = 0;
847f7ad4 12218 offsetT displacement_from_opcode_start;
252b5132 12219
e379e5f3
L
12220 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12221 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING
12222 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12223 {
12224 /* Generate nop padding. */
12225 unsigned int size = fragP->tc_frag_data.length;
12226 if (size)
12227 {
12228 if (size > fragP->tc_frag_data.max_bytes)
12229 abort ();
12230
12231 if (flag_debug)
12232 {
12233 const char *msg;
12234 const char *branch = "branch";
12235 const char *prefix = "";
12236 fragS *padding_fragP;
12237 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
12238 == BRANCH_PREFIX)
12239 {
12240 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
12241 switch (fragP->tc_frag_data.default_prefix)
12242 {
12243 default:
12244 abort ();
12245 break;
12246 case CS_PREFIX_OPCODE:
12247 prefix = " cs";
12248 break;
12249 case DS_PREFIX_OPCODE:
12250 prefix = " ds";
12251 break;
12252 case ES_PREFIX_OPCODE:
12253 prefix = " es";
12254 break;
12255 case FS_PREFIX_OPCODE:
12256 prefix = " fs";
12257 break;
12258 case GS_PREFIX_OPCODE:
12259 prefix = " gs";
12260 break;
12261 case SS_PREFIX_OPCODE:
12262 prefix = " ss";
12263 break;
12264 }
12265 if (padding_fragP)
12266 msg = _("%s:%u: add %d%s at 0x%llx to align "
12267 "%s within %d-byte boundary\n");
12268 else
12269 msg = _("%s:%u: add additional %d%s at 0x%llx to "
12270 "align %s within %d-byte boundary\n");
12271 }
12272 else
12273 {
12274 padding_fragP = fragP;
12275 msg = _("%s:%u: add %d%s-byte nop at 0x%llx to align "
12276 "%s within %d-byte boundary\n");
12277 }
12278
12279 if (padding_fragP)
12280 switch (padding_fragP->tc_frag_data.branch_type)
12281 {
12282 case align_branch_jcc:
12283 branch = "jcc";
12284 break;
12285 case align_branch_fused:
12286 branch = "fused jcc";
12287 break;
12288 case align_branch_jmp:
12289 branch = "jmp";
12290 break;
12291 case align_branch_call:
12292 branch = "call";
12293 break;
12294 case align_branch_indirect:
12295 branch = "indiret branch";
12296 break;
12297 case align_branch_ret:
12298 branch = "ret";
12299 break;
12300 default:
12301 break;
12302 }
12303
12304 fprintf (stdout, msg,
12305 fragP->fr_file, fragP->fr_line, size, prefix,
12306 (long long) fragP->fr_address, branch,
12307 1 << align_branch_power);
12308 }
12309 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12310 memset (fragP->fr_opcode,
12311 fragP->tc_frag_data.default_prefix, size);
12312 else
12313 i386_generate_nops (fragP, (char *) fragP->fr_opcode,
12314 size, 0);
12315 fragP->fr_fix += size;
12316 }
12317 return;
12318 }
12319
252b5132
RH
12320 opcode = (unsigned char *) fragP->fr_opcode;
12321
47926f60 12322 /* Address we want to reach in file space. */
252b5132 12323 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
252b5132 12324
47926f60 12325 /* Address opcode resides at in file space. */
252b5132
RH
12326 opcode_address = fragP->fr_address + fragP->fr_fix;
12327
47926f60 12328 /* Displacement from opcode start to fill into instruction. */
252b5132
RH
12329 displacement_from_opcode_start = target_address - opcode_address;
12330
fddf5b5b 12331 if ((fragP->fr_subtype & BIG) == 0)
252b5132 12332 {
47926f60
KH
12333 /* Don't have to change opcode. */
12334 extension = 1; /* 1 opcode + 1 displacement */
252b5132 12335 where_to_put_displacement = &opcode[1];
fddf5b5b
AM
12336 }
12337 else
12338 {
12339 if (no_cond_jump_promotion
12340 && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
4eed87de
AM
12341 as_warn_where (fragP->fr_file, fragP->fr_line,
12342 _("long jump required"));
252b5132 12343
fddf5b5b
AM
12344 switch (fragP->fr_subtype)
12345 {
12346 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
12347 extension = 4; /* 1 opcode + 4 displacement */
12348 opcode[0] = 0xe9;
12349 where_to_put_displacement = &opcode[1];
12350 break;
252b5132 12351
fddf5b5b
AM
12352 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
12353 extension = 2; /* 1 opcode + 2 displacement */
12354 opcode[0] = 0xe9;
12355 where_to_put_displacement = &opcode[1];
12356 break;
252b5132 12357
fddf5b5b
AM
12358 case ENCODE_RELAX_STATE (COND_JUMP, BIG):
12359 case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
12360 extension = 5; /* 2 opcode + 4 displacement */
12361 opcode[1] = opcode[0] + 0x10;
12362 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12363 where_to_put_displacement = &opcode[2];
12364 break;
252b5132 12365
fddf5b5b
AM
12366 case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
12367 extension = 3; /* 2 opcode + 2 displacement */
12368 opcode[1] = opcode[0] + 0x10;
12369 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12370 where_to_put_displacement = &opcode[2];
12371 break;
252b5132 12372
fddf5b5b
AM
12373 case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
12374 extension = 4;
12375 opcode[0] ^= 1;
12376 opcode[1] = 3;
12377 opcode[2] = 0xe9;
12378 where_to_put_displacement = &opcode[3];
12379 break;
12380
12381 default:
12382 BAD_CASE (fragP->fr_subtype);
12383 break;
12384 }
252b5132 12385 }
fddf5b5b 12386
7b81dfbb
AJ
12387 /* If size if less then four we are sure that the operand fits,
12388 but if it's 4, then it could be that the displacement is larger
12389 then -/+ 2GB. */
12390 if (DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype) == 4
12391 && object_64bit
12392 && ((addressT) (displacement_from_opcode_start - extension
4eed87de
AM
12393 + ((addressT) 1 << 31))
12394 > (((addressT) 2 << 31) - 1)))
7b81dfbb
AJ
12395 {
12396 as_bad_where (fragP->fr_file, fragP->fr_line,
12397 _("jump target out of range"));
12398 /* Make us emit 0. */
12399 displacement_from_opcode_start = extension;
12400 }
47926f60 12401 /* Now put displacement after opcode. */
252b5132
RH
12402 md_number_to_chars ((char *) where_to_put_displacement,
12403 (valueT) (displacement_from_opcode_start - extension),
fddf5b5b 12404 DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
252b5132
RH
12405 fragP->fr_fix += extension;
12406}
12407\f
7016a5d5 12408/* Apply a fixup (fixP) to segment data, once it has been determined
252b5132
RH
12409 by our caller that we have all the info we need to fix it up.
12410
7016a5d5
TG
12411 Parameter valP is the pointer to the value of the bits.
12412
252b5132
RH
12413 On the 386, immediates, displacements, and data pointers are all in
12414 the same (little-endian) format, so we don't need to care about which
12415 we are handling. */
12416
94f592af 12417void
7016a5d5 12418md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 12419{
94f592af 12420 char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
c6682705 12421 valueT value = *valP;
252b5132 12422
f86103b7 12423#if !defined (TE_Mach)
93382f6d
AM
12424 if (fixP->fx_pcrel)
12425 {
12426 switch (fixP->fx_r_type)
12427 {
5865bb77
ILT
12428 default:
12429 break;
12430
d6ab8113
JB
12431 case BFD_RELOC_64:
12432 fixP->fx_r_type = BFD_RELOC_64_PCREL;
12433 break;
93382f6d 12434 case BFD_RELOC_32:
ae8887b5 12435 case BFD_RELOC_X86_64_32S:
93382f6d
AM
12436 fixP->fx_r_type = BFD_RELOC_32_PCREL;
12437 break;
12438 case BFD_RELOC_16:
12439 fixP->fx_r_type = BFD_RELOC_16_PCREL;
12440 break;
12441 case BFD_RELOC_8:
12442 fixP->fx_r_type = BFD_RELOC_8_PCREL;
12443 break;
12444 }
12445 }
252b5132 12446
a161fe53 12447 if (fixP->fx_addsy != NULL
31312f95 12448 && (fixP->fx_r_type == BFD_RELOC_32_PCREL
d6ab8113 12449 || fixP->fx_r_type == BFD_RELOC_64_PCREL
31312f95 12450 || fixP->fx_r_type == BFD_RELOC_16_PCREL
d258b828 12451 || fixP->fx_r_type == BFD_RELOC_8_PCREL)
31312f95 12452 && !use_rela_relocations)
252b5132 12453 {
31312f95
AM
12454 /* This is a hack. There should be a better way to handle this.
12455 This covers for the fact that bfd_install_relocation will
12456 subtract the current location (for partial_inplace, PC relative
12457 relocations); see more below. */
252b5132 12458#ifndef OBJ_AOUT
718ddfc0 12459 if (IS_ELF
252b5132
RH
12460#ifdef TE_PE
12461 || OUTPUT_FLAVOR == bfd_target_coff_flavour
12462#endif
12463 )
12464 value += fixP->fx_where + fixP->fx_frag->fr_address;
12465#endif
12466#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12467 if (IS_ELF)
252b5132 12468 {
6539b54b 12469 segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
2f66722d 12470
6539b54b 12471 if ((sym_seg == seg
2f66722d 12472 || (symbol_section_p (fixP->fx_addsy)
6539b54b 12473 && sym_seg != absolute_section))
af65af87 12474 && !generic_force_reloc (fixP))
2f66722d
AM
12475 {
12476 /* Yes, we add the values in twice. This is because
6539b54b
AM
12477 bfd_install_relocation subtracts them out again. I think
12478 bfd_install_relocation is broken, but I don't dare change
2f66722d
AM
12479 it. FIXME. */
12480 value += fixP->fx_where + fixP->fx_frag->fr_address;
12481 }
252b5132
RH
12482 }
12483#endif
12484#if defined (OBJ_COFF) && defined (TE_PE)
977cdf5a
NC
12485 /* For some reason, the PE format does not store a
12486 section address offset for a PC relative symbol. */
12487 if (S_GET_SEGMENT (fixP->fx_addsy) != seg
7be1c489 12488 || S_IS_WEAK (fixP->fx_addsy))
252b5132
RH
12489 value += md_pcrel_from (fixP);
12490#endif
12491 }
fbeb56a4 12492#if defined (OBJ_COFF) && defined (TE_PE)
f01c1a09
NC
12493 if (fixP->fx_addsy != NULL
12494 && S_IS_WEAK (fixP->fx_addsy)
12495 /* PR 16858: Do not modify weak function references. */
12496 && ! fixP->fx_pcrel)
fbeb56a4 12497 {
296a8689
NC
12498#if !defined (TE_PEP)
12499 /* For x86 PE weak function symbols are neither PC-relative
12500 nor do they set S_IS_FUNCTION. So the only reliable way
12501 to detect them is to check the flags of their containing
12502 section. */
12503 if (S_GET_SEGMENT (fixP->fx_addsy) != NULL
12504 && S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_CODE)
12505 ;
12506 else
12507#endif
fbeb56a4
DK
12508 value -= S_GET_VALUE (fixP->fx_addsy);
12509 }
12510#endif
252b5132
RH
12511
12512 /* Fix a few things - the dynamic linker expects certain values here,
0234cb7c 12513 and we must not disappoint it. */
252b5132 12514#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12515 if (IS_ELF && fixP->fx_addsy)
47926f60
KH
12516 switch (fixP->fx_r_type)
12517 {
12518 case BFD_RELOC_386_PLT32:
3e73aa7c 12519 case BFD_RELOC_X86_64_PLT32:
b9519cfe
L
12520 /* Make the jump instruction point to the address of the operand.
12521 At runtime we merely add the offset to the actual PLT entry.
12522 NB: Subtract the offset size only for jump instructions. */
12523 if (fixP->fx_pcrel)
12524 value = -4;
47926f60 12525 break;
31312f95 12526
13ae64f3
JJ
12527 case BFD_RELOC_386_TLS_GD:
12528 case BFD_RELOC_386_TLS_LDM:
13ae64f3 12529 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
12530 case BFD_RELOC_386_TLS_IE:
12531 case BFD_RELOC_386_TLS_GOTIE:
67a4f2b7 12532 case BFD_RELOC_386_TLS_GOTDESC:
bffbf940
JJ
12533 case BFD_RELOC_X86_64_TLSGD:
12534 case BFD_RELOC_X86_64_TLSLD:
12535 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7 12536 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
00f7efb6
JJ
12537 value = 0; /* Fully resolved at runtime. No addend. */
12538 /* Fallthrough */
12539 case BFD_RELOC_386_TLS_LE:
12540 case BFD_RELOC_386_TLS_LDO_32:
12541 case BFD_RELOC_386_TLS_LE_32:
12542 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 12543 case BFD_RELOC_X86_64_DTPOFF64:
00f7efb6 12544 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113 12545 case BFD_RELOC_X86_64_TPOFF64:
00f7efb6
JJ
12546 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12547 break;
12548
67a4f2b7
AO
12549 case BFD_RELOC_386_TLS_DESC_CALL:
12550 case BFD_RELOC_X86_64_TLSDESC_CALL:
12551 value = 0; /* Fully resolved at runtime. No addend. */
12552 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12553 fixP->fx_done = 0;
12554 return;
12555
47926f60
KH
12556 case BFD_RELOC_VTABLE_INHERIT:
12557 case BFD_RELOC_VTABLE_ENTRY:
12558 fixP->fx_done = 0;
94f592af 12559 return;
47926f60
KH
12560
12561 default:
12562 break;
12563 }
12564#endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) */
c6682705 12565 *valP = value;
f86103b7 12566#endif /* !defined (TE_Mach) */
3e73aa7c 12567
3e73aa7c 12568 /* Are we finished with this relocation now? */
c6682705 12569 if (fixP->fx_addsy == NULL)
3e73aa7c 12570 fixP->fx_done = 1;
fbeb56a4
DK
12571#if defined (OBJ_COFF) && defined (TE_PE)
12572 else if (fixP->fx_addsy != NULL && S_IS_WEAK (fixP->fx_addsy))
12573 {
12574 fixP->fx_done = 0;
12575 /* Remember value for tc_gen_reloc. */
12576 fixP->fx_addnumber = value;
12577 /* Clear out the frag for now. */
12578 value = 0;
12579 }
12580#endif
3e73aa7c
JH
12581 else if (use_rela_relocations)
12582 {
12583 fixP->fx_no_overflow = 1;
062cd5e7
AS
12584 /* Remember value for tc_gen_reloc. */
12585 fixP->fx_addnumber = value;
3e73aa7c
JH
12586 value = 0;
12587 }
f86103b7 12588
94f592af 12589 md_number_to_chars (p, value, fixP->fx_size);
252b5132 12590}
252b5132 12591\f
6d4af3c2 12592const char *
499ac353 12593md_atof (int type, char *litP, int *sizeP)
252b5132 12594{
499ac353
NC
12595 /* This outputs the LITTLENUMs in REVERSE order;
12596 in accord with the bigendian 386. */
5b7c81bd 12597 return ieee_md_atof (type, litP, sizeP, false);
252b5132
RH
12598}
12599\f
2d545b82 12600static char output_invalid_buf[sizeof (unsigned char) * 2 + 6];
252b5132 12601
252b5132 12602static char *
e3bb37b5 12603output_invalid (int c)
252b5132 12604{
3882b010 12605 if (ISPRINT (c))
f9f21a03
L
12606 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
12607 "'%c'", c);
252b5132 12608 else
f9f21a03 12609 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
2d545b82 12610 "(0x%x)", (unsigned char) c);
252b5132
RH
12611 return output_invalid_buf;
12612}
12613
8a6fb3f9
JB
12614/* Verify that @r can be used in the current context. */
12615
5b7c81bd 12616static bool check_register (const reg_entry *r)
8a6fb3f9
JB
12617{
12618 if (allow_pseudo_reg)
5b7c81bd 12619 return true;
8a6fb3f9
JB
12620
12621 if (operand_type_all_zero (&r->reg_type))
5b7c81bd 12622 return false;
8a6fb3f9
JB
12623
12624 if ((r->reg_type.bitfield.dword
12625 || (r->reg_type.bitfield.class == SReg && r->reg_num > 3)
12626 || r->reg_type.bitfield.class == RegCR
22e00a3f 12627 || r->reg_type.bitfield.class == RegDR)
8a6fb3f9 12628 && !cpu_arch_flags.bitfield.cpui386)
5b7c81bd 12629 return false;
8a6fb3f9 12630
22e00a3f
JB
12631 if (r->reg_type.bitfield.class == RegTR
12632 && (flag_code == CODE_64BIT
12633 || !cpu_arch_flags.bitfield.cpui386
12634 || cpu_arch_isa_flags.bitfield.cpui586
12635 || cpu_arch_isa_flags.bitfield.cpui686))
5b7c81bd 12636 return false;
22e00a3f 12637
8a6fb3f9 12638 if (r->reg_type.bitfield.class == RegMMX && !cpu_arch_flags.bitfield.cpummx)
5b7c81bd 12639 return false;
8a6fb3f9
JB
12640
12641 if (!cpu_arch_flags.bitfield.cpuavx512f)
12642 {
12643 if (r->reg_type.bitfield.zmmword
12644 || r->reg_type.bitfield.class == RegMask)
5b7c81bd 12645 return false;
8a6fb3f9
JB
12646
12647 if (!cpu_arch_flags.bitfield.cpuavx)
12648 {
12649 if (r->reg_type.bitfield.ymmword)
5b7c81bd 12650 return false;
8a6fb3f9
JB
12651
12652 if (!cpu_arch_flags.bitfield.cpusse && r->reg_type.bitfield.xmmword)
5b7c81bd 12653 return false;
8a6fb3f9
JB
12654 }
12655 }
12656
260cd341
LC
12657 if (r->reg_type.bitfield.tmmword
12658 && (!cpu_arch_flags.bitfield.cpuamx_tile
12659 || flag_code != CODE_64BIT))
5b7c81bd 12660 return false;
260cd341 12661
8a6fb3f9 12662 if (r->reg_type.bitfield.class == RegBND && !cpu_arch_flags.bitfield.cpumpx)
5b7c81bd 12663 return false;
8a6fb3f9
JB
12664
12665 /* Don't allow fake index register unless allow_index_reg isn't 0. */
12666 if (!allow_index_reg && r->reg_num == RegIZ)
5b7c81bd 12667 return false;
8a6fb3f9
JB
12668
12669 /* Upper 16 vector registers are only available with VREX in 64bit
12670 mode, and require EVEX encoding. */
12671 if (r->reg_flags & RegVRex)
12672 {
12673 if (!cpu_arch_flags.bitfield.cpuavx512f
12674 || flag_code != CODE_64BIT)
5b7c81bd 12675 return false;
8a6fb3f9 12676
da4977e0
JB
12677 if (i.vec_encoding == vex_encoding_default)
12678 i.vec_encoding = vex_encoding_evex;
12679 else if (i.vec_encoding != vex_encoding_evex)
12680 i.vec_encoding = vex_encoding_error;
8a6fb3f9
JB
12681 }
12682
12683 if (((r->reg_flags & (RegRex64 | RegRex)) || r->reg_type.bitfield.qword)
12684 && (!cpu_arch_flags.bitfield.cpulm || r->reg_type.bitfield.class != RegCR)
12685 && flag_code != CODE_64BIT)
5b7c81bd 12686 return false;
8a6fb3f9
JB
12687
12688 if (r->reg_type.bitfield.class == SReg && r->reg_num == RegFlat
12689 && !intel_syntax)
5b7c81bd 12690 return false;
8a6fb3f9 12691
5b7c81bd 12692 return true;
8a6fb3f9
JB
12693}
12694
af6bdddf 12695/* REG_STRING starts *before* REGISTER_PREFIX. */
252b5132
RH
12696
12697static const reg_entry *
4d1bb795 12698parse_real_register (char *reg_string, char **end_op)
252b5132 12699{
af6bdddf
AM
12700 char *s = reg_string;
12701 char *p;
252b5132
RH
12702 char reg_name_given[MAX_REG_NAME_SIZE + 1];
12703 const reg_entry *r;
12704
12705 /* Skip possible REGISTER_PREFIX and possible whitespace. */
12706 if (*s == REGISTER_PREFIX)
12707 ++s;
12708
12709 if (is_space_char (*s))
12710 ++s;
12711
12712 p = reg_name_given;
af6bdddf 12713 while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
252b5132
RH
12714 {
12715 if (p >= reg_name_given + MAX_REG_NAME_SIZE)
af6bdddf
AM
12716 return (const reg_entry *) NULL;
12717 s++;
252b5132
RH
12718 }
12719
6588847e
DN
12720 /* For naked regs, make sure that we are not dealing with an identifier.
12721 This prevents confusing an identifier like `eax_var' with register
12722 `eax'. */
12723 if (allow_naked_reg && identifier_chars[(unsigned char) *s])
12724 return (const reg_entry *) NULL;
12725
af6bdddf 12726 *end_op = s;
252b5132 12727
629310ab 12728 r = (const reg_entry *) str_hash_find (reg_hash, reg_name_given);
252b5132 12729
5f47d35b 12730 /* Handle floating point regs, allowing spaces in the (i) part. */
6288d05f 12731 if (r == reg_st0)
5f47d35b 12732 {
0e0eea78
JB
12733 if (!cpu_arch_flags.bitfield.cpu8087
12734 && !cpu_arch_flags.bitfield.cpu287
af32b722
JB
12735 && !cpu_arch_flags.bitfield.cpu387
12736 && !allow_pseudo_reg)
0e0eea78
JB
12737 return (const reg_entry *) NULL;
12738
5f47d35b
AM
12739 if (is_space_char (*s))
12740 ++s;
12741 if (*s == '(')
12742 {
af6bdddf 12743 ++s;
5f47d35b
AM
12744 if (is_space_char (*s))
12745 ++s;
12746 if (*s >= '0' && *s <= '7')
12747 {
db557034 12748 int fpr = *s - '0';
af6bdddf 12749 ++s;
5f47d35b
AM
12750 if (is_space_char (*s))
12751 ++s;
12752 if (*s == ')')
12753 {
12754 *end_op = s + 1;
6288d05f 12755 know (r[fpr].reg_num == fpr);
db557034 12756 return r + fpr;
5f47d35b 12757 }
5f47d35b 12758 }
47926f60 12759 /* We have "%st(" then garbage. */
5f47d35b
AM
12760 return (const reg_entry *) NULL;
12761 }
12762 }
12763
8a6fb3f9 12764 return r && check_register (r) ? r : NULL;
252b5132 12765}
4d1bb795
JB
12766
12767/* REG_STRING starts *before* REGISTER_PREFIX. */
12768
12769static const reg_entry *
12770parse_register (char *reg_string, char **end_op)
12771{
12772 const reg_entry *r;
12773
12774 if (*reg_string == REGISTER_PREFIX || allow_naked_reg)
12775 r = parse_real_register (reg_string, end_op);
12776 else
12777 r = NULL;
12778 if (!r)
12779 {
12780 char *save = input_line_pointer;
12781 char c;
12782 symbolS *symbolP;
12783
12784 input_line_pointer = reg_string;
d02603dc 12785 c = get_symbol_name (&reg_string);
4d1bb795
JB
12786 symbolP = symbol_find (reg_string);
12787 if (symbolP && S_GET_SEGMENT (symbolP) == reg_section)
12788 {
12789 const expressionS *e = symbol_get_value_expression (symbolP);
12790
0398aac5 12791 know (e->X_op == O_register);
4eed87de 12792 know (e->X_add_number >= 0
c3fe08fa 12793 && (valueT) e->X_add_number < i386_regtab_size);
4d1bb795 12794 r = i386_regtab + e->X_add_number;
8a6fb3f9
JB
12795 if (!check_register (r))
12796 {
12797 as_bad (_("register '%s%s' cannot be used here"),
12798 register_prefix, r->reg_name);
12799 r = &bad_reg;
12800 }
4d1bb795
JB
12801 *end_op = input_line_pointer;
12802 }
12803 *input_line_pointer = c;
12804 input_line_pointer = save;
12805 }
12806 return r;
12807}
12808
12809int
12810i386_parse_name (char *name, expressionS *e, char *nextcharP)
12811{
12812 const reg_entry *r;
12813 char *end = input_line_pointer;
12814
12815 *end = *nextcharP;
12816 r = parse_register (name, &input_line_pointer);
12817 if (r && end <= input_line_pointer)
12818 {
12819 *nextcharP = *input_line_pointer;
12820 *input_line_pointer = 0;
8a6fb3f9
JB
12821 if (r != &bad_reg)
12822 {
12823 e->X_op = O_register;
12824 e->X_add_number = r - i386_regtab;
12825 }
12826 else
12827 e->X_op = O_illegal;
4d1bb795
JB
12828 return 1;
12829 }
12830 input_line_pointer = end;
12831 *end = 0;
ee86248c 12832 return intel_syntax ? i386_intel_parse_name (name, e) : 0;
4d1bb795
JB
12833}
12834
12835void
12836md_operand (expressionS *e)
12837{
ee86248c
JB
12838 char *end;
12839 const reg_entry *r;
4d1bb795 12840
ee86248c
JB
12841 switch (*input_line_pointer)
12842 {
12843 case REGISTER_PREFIX:
12844 r = parse_real_register (input_line_pointer, &end);
4d1bb795
JB
12845 if (r)
12846 {
12847 e->X_op = O_register;
12848 e->X_add_number = r - i386_regtab;
12849 input_line_pointer = end;
12850 }
ee86248c
JB
12851 break;
12852
12853 case '[':
9c2799c2 12854 gas_assert (intel_syntax);
ee86248c
JB
12855 end = input_line_pointer++;
12856 expression (e);
12857 if (*input_line_pointer == ']')
12858 {
12859 ++input_line_pointer;
12860 e->X_op_symbol = make_expr_symbol (e);
12861 e->X_add_symbol = NULL;
12862 e->X_add_number = 0;
12863 e->X_op = O_index;
12864 }
12865 else
12866 {
12867 e->X_op = O_absent;
12868 input_line_pointer = end;
12869 }
12870 break;
4d1bb795
JB
12871 }
12872}
12873
252b5132 12874\f
4cc782b5 12875#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
b6f8c7c4 12876const char *md_shortopts = "kVQ:sqnO::";
252b5132 12877#else
b6f8c7c4 12878const char *md_shortopts = "qnO::";
252b5132 12879#endif
6e0b89ee 12880
3e73aa7c 12881#define OPTION_32 (OPTION_MD_BASE + 0)
b3b91714
AM
12882#define OPTION_64 (OPTION_MD_BASE + 1)
12883#define OPTION_DIVIDE (OPTION_MD_BASE + 2)
9103f4f4
L
12884#define OPTION_MARCH (OPTION_MD_BASE + 3)
12885#define OPTION_MTUNE (OPTION_MD_BASE + 4)
1efbbeb4
L
12886#define OPTION_MMNEMONIC (OPTION_MD_BASE + 5)
12887#define OPTION_MSYNTAX (OPTION_MD_BASE + 6)
12888#define OPTION_MINDEX_REG (OPTION_MD_BASE + 7)
12889#define OPTION_MNAKED_REG (OPTION_MD_BASE + 8)
bd5dea88 12890#define OPTION_MRELAX_RELOCATIONS (OPTION_MD_BASE + 9)
c0f3af97 12891#define OPTION_MSSE2AVX (OPTION_MD_BASE + 10)
daf50ae7 12892#define OPTION_MSSE_CHECK (OPTION_MD_BASE + 11)
7bab8ab5
JB
12893#define OPTION_MOPERAND_CHECK (OPTION_MD_BASE + 12)
12894#define OPTION_MAVXSCALAR (OPTION_MD_BASE + 13)
12895#define OPTION_X32 (OPTION_MD_BASE + 14)
7e8b059b 12896#define OPTION_MADD_BND_PREFIX (OPTION_MD_BASE + 15)
43234a1e
L
12897#define OPTION_MEVEXLIG (OPTION_MD_BASE + 16)
12898#define OPTION_MEVEXWIG (OPTION_MD_BASE + 17)
167ad85b 12899#define OPTION_MBIG_OBJ (OPTION_MD_BASE + 18)
d1982f93 12900#define OPTION_MOMIT_LOCK_PREFIX (OPTION_MD_BASE + 19)
d3d3c6db 12901#define OPTION_MEVEXRCIG (OPTION_MD_BASE + 20)
8dcea932 12902#define OPTION_MSHARED (OPTION_MD_BASE + 21)
5db04b09
L
12903#define OPTION_MAMD64 (OPTION_MD_BASE + 22)
12904#define OPTION_MINTEL64 (OPTION_MD_BASE + 23)
e4e00185 12905#define OPTION_MFENCE_AS_LOCK_ADD (OPTION_MD_BASE + 24)
b4a3a7b4 12906#define OPTION_X86_USED_NOTE (OPTION_MD_BASE + 25)
03751133 12907#define OPTION_MVEXWIG (OPTION_MD_BASE + 26)
e379e5f3
L
12908#define OPTION_MALIGN_BRANCH_BOUNDARY (OPTION_MD_BASE + 27)
12909#define OPTION_MALIGN_BRANCH_PREFIX_SIZE (OPTION_MD_BASE + 28)
12910#define OPTION_MALIGN_BRANCH (OPTION_MD_BASE + 29)
76cf450b 12911#define OPTION_MBRANCHES_WITH_32B_BOUNDARIES (OPTION_MD_BASE + 30)
ae531041
L
12912#define OPTION_MLFENCE_AFTER_LOAD (OPTION_MD_BASE + 31)
12913#define OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH (OPTION_MD_BASE + 32)
12914#define OPTION_MLFENCE_BEFORE_RET (OPTION_MD_BASE + 33)
b3b91714 12915
99ad8390
NC
12916struct option md_longopts[] =
12917{
3e73aa7c 12918 {"32", no_argument, NULL, OPTION_32},
321098a5 12919#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 12920 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c 12921 {"64", no_argument, NULL, OPTION_64},
351f65ca
L
12922#endif
12923#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 12924 {"x32", no_argument, NULL, OPTION_X32},
8dcea932 12925 {"mshared", no_argument, NULL, OPTION_MSHARED},
b4a3a7b4 12926 {"mx86-used-note", required_argument, NULL, OPTION_X86_USED_NOTE},
6e0b89ee 12927#endif
b3b91714 12928 {"divide", no_argument, NULL, OPTION_DIVIDE},
9103f4f4
L
12929 {"march", required_argument, NULL, OPTION_MARCH},
12930 {"mtune", required_argument, NULL, OPTION_MTUNE},
1efbbeb4
L
12931 {"mmnemonic", required_argument, NULL, OPTION_MMNEMONIC},
12932 {"msyntax", required_argument, NULL, OPTION_MSYNTAX},
12933 {"mindex-reg", no_argument, NULL, OPTION_MINDEX_REG},
12934 {"mnaked-reg", no_argument, NULL, OPTION_MNAKED_REG},
c0f3af97 12935 {"msse2avx", no_argument, NULL, OPTION_MSSE2AVX},
daf50ae7 12936 {"msse-check", required_argument, NULL, OPTION_MSSE_CHECK},
7bab8ab5 12937 {"moperand-check", required_argument, NULL, OPTION_MOPERAND_CHECK},
539f890d 12938 {"mavxscalar", required_argument, NULL, OPTION_MAVXSCALAR},
03751133 12939 {"mvexwig", required_argument, NULL, OPTION_MVEXWIG},
7e8b059b 12940 {"madd-bnd-prefix", no_argument, NULL, OPTION_MADD_BND_PREFIX},
43234a1e
L
12941 {"mevexlig", required_argument, NULL, OPTION_MEVEXLIG},
12942 {"mevexwig", required_argument, NULL, OPTION_MEVEXWIG},
167ad85b
TG
12943# if defined (TE_PE) || defined (TE_PEP)
12944 {"mbig-obj", no_argument, NULL, OPTION_MBIG_OBJ},
12945#endif
d1982f93 12946 {"momit-lock-prefix", required_argument, NULL, OPTION_MOMIT_LOCK_PREFIX},
e4e00185 12947 {"mfence-as-lock-add", required_argument, NULL, OPTION_MFENCE_AS_LOCK_ADD},
0cb4071e 12948 {"mrelax-relocations", required_argument, NULL, OPTION_MRELAX_RELOCATIONS},
d3d3c6db 12949 {"mevexrcig", required_argument, NULL, OPTION_MEVEXRCIG},
e379e5f3
L
12950 {"malign-branch-boundary", required_argument, NULL, OPTION_MALIGN_BRANCH_BOUNDARY},
12951 {"malign-branch-prefix-size", required_argument, NULL, OPTION_MALIGN_BRANCH_PREFIX_SIZE},
12952 {"malign-branch", required_argument, NULL, OPTION_MALIGN_BRANCH},
76cf450b 12953 {"mbranches-within-32B-boundaries", no_argument, NULL, OPTION_MBRANCHES_WITH_32B_BOUNDARIES},
ae531041
L
12954 {"mlfence-after-load", required_argument, NULL, OPTION_MLFENCE_AFTER_LOAD},
12955 {"mlfence-before-indirect-branch", required_argument, NULL,
12956 OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH},
12957 {"mlfence-before-ret", required_argument, NULL, OPTION_MLFENCE_BEFORE_RET},
5db04b09
L
12958 {"mamd64", no_argument, NULL, OPTION_MAMD64},
12959 {"mintel64", no_argument, NULL, OPTION_MINTEL64},
252b5132
RH
12960 {NULL, no_argument, NULL, 0}
12961};
12962size_t md_longopts_size = sizeof (md_longopts);
12963
12964int
17b9d67d 12965md_parse_option (int c, const char *arg)
252b5132 12966{
91d6fa6a 12967 unsigned int j;
e379e5f3 12968 char *arch, *next, *saved, *type;
9103f4f4 12969
252b5132
RH
12970 switch (c)
12971 {
12b55ccc
L
12972 case 'n':
12973 optimize_align_code = 0;
12974 break;
12975
a38cf1db
AM
12976 case 'q':
12977 quiet_warnings = 1;
252b5132
RH
12978 break;
12979
12980#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
a38cf1db
AM
12981 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
12982 should be emitted or not. FIXME: Not implemented. */
12983 case 'Q':
d4693039
JB
12984 if ((arg[0] != 'y' && arg[0] != 'n') || arg[1])
12985 return 0;
252b5132
RH
12986 break;
12987
12988 /* -V: SVR4 argument to print version ID. */
12989 case 'V':
12990 print_version_id ();
12991 break;
12992
a38cf1db
AM
12993 /* -k: Ignore for FreeBSD compatibility. */
12994 case 'k':
252b5132 12995 break;
4cc782b5
ILT
12996
12997 case 's':
12998 /* -s: On i386 Solaris, this tells the native assembler to use
29b0f896 12999 .stab instead of .stab.excl. We always use .stab anyhow. */
4cc782b5 13000 break;
8dcea932
L
13001
13002 case OPTION_MSHARED:
13003 shared = 1;
13004 break;
b4a3a7b4
L
13005
13006 case OPTION_X86_USED_NOTE:
13007 if (strcasecmp (arg, "yes") == 0)
13008 x86_used_note = 1;
13009 else if (strcasecmp (arg, "no") == 0)
13010 x86_used_note = 0;
13011 else
13012 as_fatal (_("invalid -mx86-used-note= option: `%s'"), arg);
13013 break;
13014
13015
99ad8390 13016#endif
321098a5 13017#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 13018 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c
JH
13019 case OPTION_64:
13020 {
13021 const char **list, **l;
13022
3e73aa7c
JH
13023 list = bfd_target_list ();
13024 for (l = list; *l != NULL; l++)
08dedd66 13025 if (startswith (*l, "elf64-x86-64")
99ad8390
NC
13026 || strcmp (*l, "coff-x86-64") == 0
13027 || strcmp (*l, "pe-x86-64") == 0
d382c579
TG
13028 || strcmp (*l, "pei-x86-64") == 0
13029 || strcmp (*l, "mach-o-x86-64") == 0)
6e0b89ee
AM
13030 {
13031 default_arch = "x86_64";
13032 break;
13033 }
3e73aa7c 13034 if (*l == NULL)
2b5d6a91 13035 as_fatal (_("no compiled in support for x86_64"));
3e73aa7c
JH
13036 free (list);
13037 }
13038 break;
13039#endif
252b5132 13040
351f65ca 13041#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 13042 case OPTION_X32:
351f65ca
L
13043 if (IS_ELF)
13044 {
13045 const char **list, **l;
13046
13047 list = bfd_target_list ();
13048 for (l = list; *l != NULL; l++)
08dedd66 13049 if (startswith (*l, "elf32-x86-64"))
351f65ca
L
13050 {
13051 default_arch = "x86_64:32";
13052 break;
13053 }
13054 if (*l == NULL)
2b5d6a91 13055 as_fatal (_("no compiled in support for 32bit x86_64"));
351f65ca
L
13056 free (list);
13057 }
13058 else
13059 as_fatal (_("32bit x86_64 is only supported for ELF"));
13060 break;
13061#endif
13062
6e0b89ee
AM
13063 case OPTION_32:
13064 default_arch = "i386";
13065 break;
13066
b3b91714
AM
13067 case OPTION_DIVIDE:
13068#ifdef SVR4_COMMENT_CHARS
13069 {
13070 char *n, *t;
13071 const char *s;
13072
add39d23 13073 n = XNEWVEC (char, strlen (i386_comment_chars) + 1);
b3b91714
AM
13074 t = n;
13075 for (s = i386_comment_chars; *s != '\0'; s++)
13076 if (*s != '/')
13077 *t++ = *s;
13078 *t = '\0';
13079 i386_comment_chars = n;
13080 }
13081#endif
13082 break;
13083
9103f4f4 13084 case OPTION_MARCH:
293f5f65
L
13085 saved = xstrdup (arg);
13086 arch = saved;
13087 /* Allow -march=+nosse. */
13088 if (*arch == '+')
13089 arch++;
6305a203 13090 do
9103f4f4 13091 {
6305a203 13092 if (*arch == '.')
2b5d6a91 13093 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
13094 next = strchr (arch, '+');
13095 if (next)
13096 *next++ = '\0';
91d6fa6a 13097 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 13098 {
91d6fa6a 13099 if (strcmp (arch, cpu_arch [j].name) == 0)
ccc9c027 13100 {
6305a203 13101 /* Processor. */
1ded5609
JB
13102 if (! cpu_arch[j].flags.bitfield.cpui386)
13103 continue;
13104
91d6fa6a 13105 cpu_arch_name = cpu_arch[j].name;
6305a203 13106 cpu_sub_arch_name = NULL;
91d6fa6a
NC
13107 cpu_arch_flags = cpu_arch[j].flags;
13108 cpu_arch_isa = cpu_arch[j].type;
13109 cpu_arch_isa_flags = cpu_arch[j].flags;
6305a203
L
13110 if (!cpu_arch_tune_set)
13111 {
13112 cpu_arch_tune = cpu_arch_isa;
13113 cpu_arch_tune_flags = cpu_arch_isa_flags;
13114 }
13115 break;
13116 }
91d6fa6a
NC
13117 else if (*cpu_arch [j].name == '.'
13118 && strcmp (arch, cpu_arch [j].name + 1) == 0)
6305a203 13119 {
33eaf5de 13120 /* ISA extension. */
6305a203 13121 i386_cpu_flags flags;
309d3373 13122
293f5f65
L
13123 flags = cpu_flags_or (cpu_arch_flags,
13124 cpu_arch[j].flags);
81486035 13125
5b64d091 13126 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
6305a203
L
13127 {
13128 if (cpu_sub_arch_name)
13129 {
13130 char *name = cpu_sub_arch_name;
13131 cpu_sub_arch_name = concat (name,
91d6fa6a 13132 cpu_arch[j].name,
1bf57e9f 13133 (const char *) NULL);
6305a203
L
13134 free (name);
13135 }
13136 else
91d6fa6a 13137 cpu_sub_arch_name = xstrdup (cpu_arch[j].name);
6305a203 13138 cpu_arch_flags = flags;
a586129e 13139 cpu_arch_isa_flags = flags;
6305a203 13140 }
0089dace
L
13141 else
13142 cpu_arch_isa_flags
13143 = cpu_flags_or (cpu_arch_isa_flags,
13144 cpu_arch[j].flags);
6305a203 13145 break;
ccc9c027 13146 }
9103f4f4 13147 }
6305a203 13148
293f5f65
L
13149 if (j >= ARRAY_SIZE (cpu_arch))
13150 {
33eaf5de 13151 /* Disable an ISA extension. */
293f5f65
L
13152 for (j = 0; j < ARRAY_SIZE (cpu_noarch); j++)
13153 if (strcmp (arch, cpu_noarch [j].name) == 0)
13154 {
13155 i386_cpu_flags flags;
13156
13157 flags = cpu_flags_and_not (cpu_arch_flags,
13158 cpu_noarch[j].flags);
13159 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
13160 {
13161 if (cpu_sub_arch_name)
13162 {
13163 char *name = cpu_sub_arch_name;
13164 cpu_sub_arch_name = concat (arch,
13165 (const char *) NULL);
13166 free (name);
13167 }
13168 else
13169 cpu_sub_arch_name = xstrdup (arch);
13170 cpu_arch_flags = flags;
13171 cpu_arch_isa_flags = flags;
13172 }
13173 break;
13174 }
13175
13176 if (j >= ARRAY_SIZE (cpu_noarch))
13177 j = ARRAY_SIZE (cpu_arch);
13178 }
13179
91d6fa6a 13180 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 13181 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
13182
13183 arch = next;
9103f4f4 13184 }
293f5f65
L
13185 while (next != NULL);
13186 free (saved);
9103f4f4
L
13187 break;
13188
13189 case OPTION_MTUNE:
13190 if (*arg == '.')
2b5d6a91 13191 as_fatal (_("invalid -mtune= option: `%s'"), arg);
91d6fa6a 13192 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 13193 {
91d6fa6a 13194 if (strcmp (arg, cpu_arch [j].name) == 0)
9103f4f4 13195 {
ccc9c027 13196 cpu_arch_tune_set = 1;
91d6fa6a
NC
13197 cpu_arch_tune = cpu_arch [j].type;
13198 cpu_arch_tune_flags = cpu_arch[j].flags;
9103f4f4
L
13199 break;
13200 }
13201 }
91d6fa6a 13202 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 13203 as_fatal (_("invalid -mtune= option: `%s'"), arg);
9103f4f4
L
13204 break;
13205
1efbbeb4
L
13206 case OPTION_MMNEMONIC:
13207 if (strcasecmp (arg, "att") == 0)
13208 intel_mnemonic = 0;
13209 else if (strcasecmp (arg, "intel") == 0)
13210 intel_mnemonic = 1;
13211 else
2b5d6a91 13212 as_fatal (_("invalid -mmnemonic= option: `%s'"), arg);
1efbbeb4
L
13213 break;
13214
13215 case OPTION_MSYNTAX:
13216 if (strcasecmp (arg, "att") == 0)
13217 intel_syntax = 0;
13218 else if (strcasecmp (arg, "intel") == 0)
13219 intel_syntax = 1;
13220 else
2b5d6a91 13221 as_fatal (_("invalid -msyntax= option: `%s'"), arg);
1efbbeb4
L
13222 break;
13223
13224 case OPTION_MINDEX_REG:
13225 allow_index_reg = 1;
13226 break;
13227
13228 case OPTION_MNAKED_REG:
13229 allow_naked_reg = 1;
13230 break;
13231
c0f3af97
L
13232 case OPTION_MSSE2AVX:
13233 sse2avx = 1;
13234 break;
13235
daf50ae7
L
13236 case OPTION_MSSE_CHECK:
13237 if (strcasecmp (arg, "error") == 0)
7bab8ab5 13238 sse_check = check_error;
daf50ae7 13239 else if (strcasecmp (arg, "warning") == 0)
7bab8ab5 13240 sse_check = check_warning;
daf50ae7 13241 else if (strcasecmp (arg, "none") == 0)
7bab8ab5 13242 sse_check = check_none;
daf50ae7 13243 else
2b5d6a91 13244 as_fatal (_("invalid -msse-check= option: `%s'"), arg);
daf50ae7
L
13245 break;
13246
7bab8ab5
JB
13247 case OPTION_MOPERAND_CHECK:
13248 if (strcasecmp (arg, "error") == 0)
13249 operand_check = check_error;
13250 else if (strcasecmp (arg, "warning") == 0)
13251 operand_check = check_warning;
13252 else if (strcasecmp (arg, "none") == 0)
13253 operand_check = check_none;
13254 else
13255 as_fatal (_("invalid -moperand-check= option: `%s'"), arg);
13256 break;
13257
539f890d
L
13258 case OPTION_MAVXSCALAR:
13259 if (strcasecmp (arg, "128") == 0)
13260 avxscalar = vex128;
13261 else if (strcasecmp (arg, "256") == 0)
13262 avxscalar = vex256;
13263 else
2b5d6a91 13264 as_fatal (_("invalid -mavxscalar= option: `%s'"), arg);
539f890d
L
13265 break;
13266
03751133
L
13267 case OPTION_MVEXWIG:
13268 if (strcmp (arg, "0") == 0)
40c9c8de 13269 vexwig = vexw0;
03751133 13270 else if (strcmp (arg, "1") == 0)
40c9c8de 13271 vexwig = vexw1;
03751133
L
13272 else
13273 as_fatal (_("invalid -mvexwig= option: `%s'"), arg);
13274 break;
13275
7e8b059b
L
13276 case OPTION_MADD_BND_PREFIX:
13277 add_bnd_prefix = 1;
13278 break;
13279
43234a1e
L
13280 case OPTION_MEVEXLIG:
13281 if (strcmp (arg, "128") == 0)
13282 evexlig = evexl128;
13283 else if (strcmp (arg, "256") == 0)
13284 evexlig = evexl256;
13285 else if (strcmp (arg, "512") == 0)
13286 evexlig = evexl512;
13287 else
13288 as_fatal (_("invalid -mevexlig= option: `%s'"), arg);
13289 break;
13290
d3d3c6db
IT
13291 case OPTION_MEVEXRCIG:
13292 if (strcmp (arg, "rne") == 0)
13293 evexrcig = rne;
13294 else if (strcmp (arg, "rd") == 0)
13295 evexrcig = rd;
13296 else if (strcmp (arg, "ru") == 0)
13297 evexrcig = ru;
13298 else if (strcmp (arg, "rz") == 0)
13299 evexrcig = rz;
13300 else
13301 as_fatal (_("invalid -mevexrcig= option: `%s'"), arg);
13302 break;
13303
43234a1e
L
13304 case OPTION_MEVEXWIG:
13305 if (strcmp (arg, "0") == 0)
13306 evexwig = evexw0;
13307 else if (strcmp (arg, "1") == 0)
13308 evexwig = evexw1;
13309 else
13310 as_fatal (_("invalid -mevexwig= option: `%s'"), arg);
13311 break;
13312
167ad85b
TG
13313# if defined (TE_PE) || defined (TE_PEP)
13314 case OPTION_MBIG_OBJ:
13315 use_big_obj = 1;
13316 break;
13317#endif
13318
d1982f93 13319 case OPTION_MOMIT_LOCK_PREFIX:
d022bddd
IT
13320 if (strcasecmp (arg, "yes") == 0)
13321 omit_lock_prefix = 1;
13322 else if (strcasecmp (arg, "no") == 0)
13323 omit_lock_prefix = 0;
13324 else
13325 as_fatal (_("invalid -momit-lock-prefix= option: `%s'"), arg);
13326 break;
13327
e4e00185
AS
13328 case OPTION_MFENCE_AS_LOCK_ADD:
13329 if (strcasecmp (arg, "yes") == 0)
13330 avoid_fence = 1;
13331 else if (strcasecmp (arg, "no") == 0)
13332 avoid_fence = 0;
13333 else
13334 as_fatal (_("invalid -mfence-as-lock-add= option: `%s'"), arg);
13335 break;
13336
ae531041
L
13337 case OPTION_MLFENCE_AFTER_LOAD:
13338 if (strcasecmp (arg, "yes") == 0)
13339 lfence_after_load = 1;
13340 else if (strcasecmp (arg, "no") == 0)
13341 lfence_after_load = 0;
13342 else
13343 as_fatal (_("invalid -mlfence-after-load= option: `%s'"), arg);
13344 break;
13345
13346 case OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH:
13347 if (strcasecmp (arg, "all") == 0)
a09f656b 13348 {
13349 lfence_before_indirect_branch = lfence_branch_all;
13350 if (lfence_before_ret == lfence_before_ret_none)
13351 lfence_before_ret = lfence_before_ret_shl;
13352 }
ae531041
L
13353 else if (strcasecmp (arg, "memory") == 0)
13354 lfence_before_indirect_branch = lfence_branch_memory;
13355 else if (strcasecmp (arg, "register") == 0)
13356 lfence_before_indirect_branch = lfence_branch_register;
13357 else if (strcasecmp (arg, "none") == 0)
13358 lfence_before_indirect_branch = lfence_branch_none;
13359 else
13360 as_fatal (_("invalid -mlfence-before-indirect-branch= option: `%s'"),
13361 arg);
13362 break;
13363
13364 case OPTION_MLFENCE_BEFORE_RET:
13365 if (strcasecmp (arg, "or") == 0)
13366 lfence_before_ret = lfence_before_ret_or;
13367 else if (strcasecmp (arg, "not") == 0)
13368 lfence_before_ret = lfence_before_ret_not;
a09f656b 13369 else if (strcasecmp (arg, "shl") == 0 || strcasecmp (arg, "yes") == 0)
13370 lfence_before_ret = lfence_before_ret_shl;
ae531041
L
13371 else if (strcasecmp (arg, "none") == 0)
13372 lfence_before_ret = lfence_before_ret_none;
13373 else
13374 as_fatal (_("invalid -mlfence-before-ret= option: `%s'"),
13375 arg);
13376 break;
13377
0cb4071e
L
13378 case OPTION_MRELAX_RELOCATIONS:
13379 if (strcasecmp (arg, "yes") == 0)
13380 generate_relax_relocations = 1;
13381 else if (strcasecmp (arg, "no") == 0)
13382 generate_relax_relocations = 0;
13383 else
13384 as_fatal (_("invalid -mrelax-relocations= option: `%s'"), arg);
13385 break;
13386
e379e5f3
L
13387 case OPTION_MALIGN_BRANCH_BOUNDARY:
13388 {
13389 char *end;
13390 long int align = strtoul (arg, &end, 0);
13391 if (*end == '\0')
13392 {
13393 if (align == 0)
13394 {
13395 align_branch_power = 0;
13396 break;
13397 }
13398 else if (align >= 16)
13399 {
13400 int align_power;
13401 for (align_power = 0;
13402 (align & 1) == 0;
13403 align >>= 1, align_power++)
13404 continue;
13405 /* Limit alignment power to 31. */
13406 if (align == 1 && align_power < 32)
13407 {
13408 align_branch_power = align_power;
13409 break;
13410 }
13411 }
13412 }
13413 as_fatal (_("invalid -malign-branch-boundary= value: %s"), arg);
13414 }
13415 break;
13416
13417 case OPTION_MALIGN_BRANCH_PREFIX_SIZE:
13418 {
13419 char *end;
13420 int align = strtoul (arg, &end, 0);
13421 /* Some processors only support 5 prefixes. */
13422 if (*end == '\0' && align >= 0 && align < 6)
13423 {
13424 align_branch_prefix_size = align;
13425 break;
13426 }
13427 as_fatal (_("invalid -malign-branch-prefix-size= value: %s"),
13428 arg);
13429 }
13430 break;
13431
13432 case OPTION_MALIGN_BRANCH:
13433 align_branch = 0;
13434 saved = xstrdup (arg);
13435 type = saved;
13436 do
13437 {
13438 next = strchr (type, '+');
13439 if (next)
13440 *next++ = '\0';
13441 if (strcasecmp (type, "jcc") == 0)
13442 align_branch |= align_branch_jcc_bit;
13443 else if (strcasecmp (type, "fused") == 0)
13444 align_branch |= align_branch_fused_bit;
13445 else if (strcasecmp (type, "jmp") == 0)
13446 align_branch |= align_branch_jmp_bit;
13447 else if (strcasecmp (type, "call") == 0)
13448 align_branch |= align_branch_call_bit;
13449 else if (strcasecmp (type, "ret") == 0)
13450 align_branch |= align_branch_ret_bit;
13451 else if (strcasecmp (type, "indirect") == 0)
13452 align_branch |= align_branch_indirect_bit;
13453 else
13454 as_fatal (_("invalid -malign-branch= option: `%s'"), arg);
13455 type = next;
13456 }
13457 while (next != NULL);
13458 free (saved);
13459 break;
13460
76cf450b
L
13461 case OPTION_MBRANCHES_WITH_32B_BOUNDARIES:
13462 align_branch_power = 5;
13463 align_branch_prefix_size = 5;
13464 align_branch = (align_branch_jcc_bit
13465 | align_branch_fused_bit
13466 | align_branch_jmp_bit);
13467 break;
13468
5db04b09 13469 case OPTION_MAMD64:
4b5aaf5f 13470 isa64 = amd64;
5db04b09
L
13471 break;
13472
13473 case OPTION_MINTEL64:
4b5aaf5f 13474 isa64 = intel64;
5db04b09
L
13475 break;
13476
b6f8c7c4
L
13477 case 'O':
13478 if (arg == NULL)
13479 {
13480 optimize = 1;
13481 /* Turn off -Os. */
13482 optimize_for_space = 0;
13483 }
13484 else if (*arg == 's')
13485 {
13486 optimize_for_space = 1;
13487 /* Turn on all encoding optimizations. */
41fd2579 13488 optimize = INT_MAX;
b6f8c7c4
L
13489 }
13490 else
13491 {
13492 optimize = atoi (arg);
13493 /* Turn off -Os. */
13494 optimize_for_space = 0;
13495 }
13496 break;
13497
252b5132
RH
13498 default:
13499 return 0;
13500 }
13501 return 1;
13502}
13503
8a2c8fef
L
13504#define MESSAGE_TEMPLATE \
13505" "
13506
293f5f65
L
13507static char *
13508output_message (FILE *stream, char *p, char *message, char *start,
13509 int *left_p, const char *name, int len)
13510{
13511 int size = sizeof (MESSAGE_TEMPLATE);
13512 int left = *left_p;
13513
13514 /* Reserve 2 spaces for ", " or ",\0" */
13515 left -= len + 2;
13516
13517 /* Check if there is any room. */
13518 if (left >= 0)
13519 {
13520 if (p != start)
13521 {
13522 *p++ = ',';
13523 *p++ = ' ';
13524 }
13525 p = mempcpy (p, name, len);
13526 }
13527 else
13528 {
13529 /* Output the current message now and start a new one. */
13530 *p++ = ',';
13531 *p = '\0';
13532 fprintf (stream, "%s\n", message);
13533 p = start;
13534 left = size - (start - message) - len - 2;
13535
13536 gas_assert (left >= 0);
13537
13538 p = mempcpy (p, name, len);
13539 }
13540
13541 *left_p = left;
13542 return p;
13543}
13544
8a2c8fef 13545static void
1ded5609 13546show_arch (FILE *stream, int ext, int check)
8a2c8fef
L
13547{
13548 static char message[] = MESSAGE_TEMPLATE;
13549 char *start = message + 27;
13550 char *p;
13551 int size = sizeof (MESSAGE_TEMPLATE);
13552 int left;
13553 const char *name;
13554 int len;
13555 unsigned int j;
13556
13557 p = start;
13558 left = size - (start - message);
13559 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13560 {
13561 /* Should it be skipped? */
13562 if (cpu_arch [j].skip)
13563 continue;
13564
13565 name = cpu_arch [j].name;
13566 len = cpu_arch [j].len;
13567 if (*name == '.')
13568 {
13569 /* It is an extension. Skip if we aren't asked to show it. */
13570 if (ext)
13571 {
13572 name++;
13573 len--;
13574 }
13575 else
13576 continue;
13577 }
13578 else if (ext)
13579 {
13580 /* It is an processor. Skip if we show only extension. */
13581 continue;
13582 }
1ded5609
JB
13583 else if (check && ! cpu_arch[j].flags.bitfield.cpui386)
13584 {
13585 /* It is an impossible processor - skip. */
13586 continue;
13587 }
8a2c8fef 13588
293f5f65 13589 p = output_message (stream, p, message, start, &left, name, len);
8a2c8fef
L
13590 }
13591
293f5f65
L
13592 /* Display disabled extensions. */
13593 if (ext)
13594 for (j = 0; j < ARRAY_SIZE (cpu_noarch); j++)
13595 {
13596 name = cpu_noarch [j].name;
13597 len = cpu_noarch [j].len;
13598 p = output_message (stream, p, message, start, &left, name,
13599 len);
13600 }
13601
8a2c8fef
L
13602 *p = '\0';
13603 fprintf (stream, "%s\n", message);
13604}
13605
252b5132 13606void
8a2c8fef 13607md_show_usage (FILE *stream)
252b5132 13608{
4cc782b5
ILT
13609#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13610 fprintf (stream, _("\
d4693039 13611 -Qy, -Qn ignored\n\
a38cf1db 13612 -V print assembler version number\n\
b3b91714
AM
13613 -k ignored\n"));
13614#endif
13615 fprintf (stream, _("\
12b55ccc 13616 -n Do not optimize code alignment\n\
b3b91714
AM
13617 -q quieten some warnings\n"));
13618#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13619 fprintf (stream, _("\
a38cf1db 13620 -s ignored\n"));
b3b91714 13621#endif
d7f449c0
L
13622#if defined BFD64 && (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
13623 || defined (TE_PE) || defined (TE_PEP))
751d281c 13624 fprintf (stream, _("\
570561f7 13625 --32/--64/--x32 generate 32bit/64bit/x32 code\n"));
751d281c 13626#endif
b3b91714
AM
13627#ifdef SVR4_COMMENT_CHARS
13628 fprintf (stream, _("\
13629 --divide do not treat `/' as a comment character\n"));
a38cf1db
AM
13630#else
13631 fprintf (stream, _("\
b3b91714 13632 --divide ignored\n"));
4cc782b5 13633#endif
9103f4f4 13634 fprintf (stream, _("\
6305a203 13635 -march=CPU[,+EXTENSION...]\n\
8a2c8fef 13636 generate code for CPU and EXTENSION, CPU is one of:\n"));
1ded5609 13637 show_arch (stream, 0, 1);
8a2c8fef
L
13638 fprintf (stream, _("\
13639 EXTENSION is combination of:\n"));
1ded5609 13640 show_arch (stream, 1, 0);
6305a203 13641 fprintf (stream, _("\
8a2c8fef 13642 -mtune=CPU optimize for CPU, CPU is one of:\n"));
1ded5609 13643 show_arch (stream, 0, 0);
ba104c83 13644 fprintf (stream, _("\
c0f3af97
L
13645 -msse2avx encode SSE instructions with VEX prefix\n"));
13646 fprintf (stream, _("\
7c5c05ef 13647 -msse-check=[none|error|warning] (default: warning)\n\
daf50ae7
L
13648 check SSE instructions\n"));
13649 fprintf (stream, _("\
7c5c05ef 13650 -moperand-check=[none|error|warning] (default: warning)\n\
7bab8ab5
JB
13651 check operand combinations for validity\n"));
13652 fprintf (stream, _("\
7c5c05ef
L
13653 -mavxscalar=[128|256] (default: 128)\n\
13654 encode scalar AVX instructions with specific vector\n\
539f890d
L
13655 length\n"));
13656 fprintf (stream, _("\
03751133
L
13657 -mvexwig=[0|1] (default: 0)\n\
13658 encode VEX instructions with specific VEX.W value\n\
13659 for VEX.W bit ignored instructions\n"));
13660 fprintf (stream, _("\
7c5c05ef
L
13661 -mevexlig=[128|256|512] (default: 128)\n\
13662 encode scalar EVEX instructions with specific vector\n\
43234a1e
L
13663 length\n"));
13664 fprintf (stream, _("\
7c5c05ef
L
13665 -mevexwig=[0|1] (default: 0)\n\
13666 encode EVEX instructions with specific EVEX.W value\n\
43234a1e
L
13667 for EVEX.W bit ignored instructions\n"));
13668 fprintf (stream, _("\
7c5c05ef 13669 -mevexrcig=[rne|rd|ru|rz] (default: rne)\n\
d3d3c6db
IT
13670 encode EVEX instructions with specific EVEX.RC value\n\
13671 for SAE-only ignored instructions\n"));
13672 fprintf (stream, _("\
7c5c05ef
L
13673 -mmnemonic=[att|intel] "));
13674 if (SYSV386_COMPAT)
13675 fprintf (stream, _("(default: att)\n"));
13676 else
13677 fprintf (stream, _("(default: intel)\n"));
13678 fprintf (stream, _("\
13679 use AT&T/Intel mnemonic\n"));
ba104c83 13680 fprintf (stream, _("\
7c5c05ef
L
13681 -msyntax=[att|intel] (default: att)\n\
13682 use AT&T/Intel syntax\n"));
ba104c83
L
13683 fprintf (stream, _("\
13684 -mindex-reg support pseudo index registers\n"));
13685 fprintf (stream, _("\
13686 -mnaked-reg don't require `%%' prefix for registers\n"));
13687 fprintf (stream, _("\
7e8b059b 13688 -madd-bnd-prefix add BND prefix for all valid branches\n"));
b4a3a7b4 13689#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8dcea932
L
13690 fprintf (stream, _("\
13691 -mshared disable branch optimization for shared code\n"));
b4a3a7b4
L
13692 fprintf (stream, _("\
13693 -mx86-used-note=[no|yes] "));
13694 if (DEFAULT_X86_USED_NOTE)
13695 fprintf (stream, _("(default: yes)\n"));
13696 else
13697 fprintf (stream, _("(default: no)\n"));
13698 fprintf (stream, _("\
13699 generate x86 used ISA and feature properties\n"));
13700#endif
13701#if defined (TE_PE) || defined (TE_PEP)
167ad85b
TG
13702 fprintf (stream, _("\
13703 -mbig-obj generate big object files\n"));
13704#endif
d022bddd 13705 fprintf (stream, _("\
7c5c05ef 13706 -momit-lock-prefix=[no|yes] (default: no)\n\
d022bddd 13707 strip all lock prefixes\n"));
5db04b09 13708 fprintf (stream, _("\
7c5c05ef 13709 -mfence-as-lock-add=[no|yes] (default: no)\n\
e4e00185
AS
13710 encode lfence, mfence and sfence as\n\
13711 lock addl $0x0, (%%{re}sp)\n"));
13712 fprintf (stream, _("\
7c5c05ef
L
13713 -mrelax-relocations=[no|yes] "));
13714 if (DEFAULT_GENERATE_X86_RELAX_RELOCATIONS)
13715 fprintf (stream, _("(default: yes)\n"));
13716 else
13717 fprintf (stream, _("(default: no)\n"));
13718 fprintf (stream, _("\
0cb4071e
L
13719 generate relax relocations\n"));
13720 fprintf (stream, _("\
e379e5f3
L
13721 -malign-branch-boundary=NUM (default: 0)\n\
13722 align branches within NUM byte boundary\n"));
13723 fprintf (stream, _("\
13724 -malign-branch=TYPE[+TYPE...] (default: jcc+fused+jmp)\n\
13725 TYPE is combination of jcc, fused, jmp, call, ret,\n\
13726 indirect\n\
13727 specify types of branches to align\n"));
13728 fprintf (stream, _("\
13729 -malign-branch-prefix-size=NUM (default: 5)\n\
13730 align branches with NUM prefixes per instruction\n"));
13731 fprintf (stream, _("\
76cf450b
L
13732 -mbranches-within-32B-boundaries\n\
13733 align branches within 32 byte boundary\n"));
13734 fprintf (stream, _("\
ae531041
L
13735 -mlfence-after-load=[no|yes] (default: no)\n\
13736 generate lfence after load\n"));
13737 fprintf (stream, _("\
13738 -mlfence-before-indirect-branch=[none|all|register|memory] (default: none)\n\
13739 generate lfence before indirect near branch\n"));
13740 fprintf (stream, _("\
a09f656b 13741 -mlfence-before-ret=[none|or|not|shl|yes] (default: none)\n\
ae531041
L
13742 generate lfence before ret\n"));
13743 fprintf (stream, _("\
7c5c05ef 13744 -mamd64 accept only AMD64 ISA [default]\n"));
5db04b09
L
13745 fprintf (stream, _("\
13746 -mintel64 accept only Intel64 ISA\n"));
252b5132
RH
13747}
13748
3e73aa7c 13749#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
321098a5 13750 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
e57f8c65 13751 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
252b5132
RH
13752
13753/* Pick the target format to use. */
13754
47926f60 13755const char *
e3bb37b5 13756i386_target_format (void)
252b5132 13757{
d34049e8 13758 if (startswith (default_arch, "x86_64"))
351f65ca
L
13759 {
13760 update_code_flag (CODE_64BIT, 1);
13761 if (default_arch[6] == '\0')
7f56bc95 13762 x86_elf_abi = X86_64_ABI;
351f65ca 13763 else
7f56bc95 13764 x86_elf_abi = X86_64_X32_ABI;
351f65ca 13765 }
3e73aa7c 13766 else if (!strcmp (default_arch, "i386"))
78f12dd3 13767 update_code_flag (CODE_32BIT, 1);
5197d474
L
13768 else if (!strcmp (default_arch, "iamcu"))
13769 {
13770 update_code_flag (CODE_32BIT, 1);
13771 if (cpu_arch_isa == PROCESSOR_UNKNOWN)
13772 {
13773 static const i386_cpu_flags iamcu_flags = CPU_IAMCU_FLAGS;
13774 cpu_arch_name = "iamcu";
13775 cpu_sub_arch_name = NULL;
13776 cpu_arch_flags = iamcu_flags;
13777 cpu_arch_isa = PROCESSOR_IAMCU;
13778 cpu_arch_isa_flags = iamcu_flags;
13779 if (!cpu_arch_tune_set)
13780 {
13781 cpu_arch_tune = cpu_arch_isa;
13782 cpu_arch_tune_flags = cpu_arch_isa_flags;
13783 }
13784 }
8d471ec1 13785 else if (cpu_arch_isa != PROCESSOR_IAMCU)
5197d474
L
13786 as_fatal (_("Intel MCU doesn't support `%s' architecture"),
13787 cpu_arch_name);
13788 }
3e73aa7c 13789 else
2b5d6a91 13790 as_fatal (_("unknown architecture"));
89507696
JB
13791
13792 if (cpu_flags_all_zero (&cpu_arch_isa_flags))
13793 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].flags;
13794 if (cpu_flags_all_zero (&cpu_arch_tune_flags))
13795 cpu_arch_tune_flags = cpu_arch[flag_code == CODE_64BIT].flags;
13796
252b5132
RH
13797 switch (OUTPUT_FLAVOR)
13798 {
9384f2ff 13799#if defined (OBJ_MAYBE_AOUT) || defined (OBJ_AOUT)
4c63da97 13800 case bfd_target_aout_flavour:
47926f60 13801 return AOUT_TARGET_FORMAT;
4c63da97 13802#endif
9384f2ff
AM
13803#if defined (OBJ_MAYBE_COFF) || defined (OBJ_COFF)
13804# if defined (TE_PE) || defined (TE_PEP)
13805 case bfd_target_coff_flavour:
167ad85b
TG
13806 if (flag_code == CODE_64BIT)
13807 return use_big_obj ? "pe-bigobj-x86-64" : "pe-x86-64";
13808 else
251dae91 13809 return use_big_obj ? "pe-bigobj-i386" : "pe-i386";
9384f2ff 13810# elif defined (TE_GO32)
0561d57c
JK
13811 case bfd_target_coff_flavour:
13812 return "coff-go32";
9384f2ff 13813# else
252b5132
RH
13814 case bfd_target_coff_flavour:
13815 return "coff-i386";
9384f2ff 13816# endif
4c63da97 13817#endif
3e73aa7c 13818#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
252b5132 13819 case bfd_target_elf_flavour:
3e73aa7c 13820 {
351f65ca
L
13821 const char *format;
13822
13823 switch (x86_elf_abi)
4fa24527 13824 {
351f65ca
L
13825 default:
13826 format = ELF_TARGET_FORMAT;
e379e5f3
L
13827#ifndef TE_SOLARIS
13828 tls_get_addr = "___tls_get_addr";
13829#endif
351f65ca 13830 break;
7f56bc95 13831 case X86_64_ABI:
351f65ca 13832 use_rela_relocations = 1;
4fa24527 13833 object_64bit = 1;
e379e5f3
L
13834#ifndef TE_SOLARIS
13835 tls_get_addr = "__tls_get_addr";
13836#endif
351f65ca
L
13837 format = ELF_TARGET_FORMAT64;
13838 break;
7f56bc95 13839 case X86_64_X32_ABI:
4fa24527 13840 use_rela_relocations = 1;
351f65ca 13841 object_64bit = 1;
e379e5f3
L
13842#ifndef TE_SOLARIS
13843 tls_get_addr = "__tls_get_addr";
13844#endif
862be3fb 13845 disallow_64bit_reloc = 1;
351f65ca
L
13846 format = ELF_TARGET_FORMAT32;
13847 break;
4fa24527 13848 }
3632d14b 13849 if (cpu_arch_isa == PROCESSOR_L1OM)
8a9036a4 13850 {
7f56bc95 13851 if (x86_elf_abi != X86_64_ABI)
8a9036a4
L
13852 as_fatal (_("Intel L1OM is 64bit only"));
13853 return ELF_TARGET_L1OM_FORMAT;
13854 }
b49f93f6 13855 else if (cpu_arch_isa == PROCESSOR_K1OM)
7a9068fe
L
13856 {
13857 if (x86_elf_abi != X86_64_ABI)
13858 as_fatal (_("Intel K1OM is 64bit only"));
13859 return ELF_TARGET_K1OM_FORMAT;
13860 }
81486035
L
13861 else if (cpu_arch_isa == PROCESSOR_IAMCU)
13862 {
13863 if (x86_elf_abi != I386_ABI)
13864 as_fatal (_("Intel MCU is 32bit only"));
13865 return ELF_TARGET_IAMCU_FORMAT;
13866 }
8a9036a4 13867 else
351f65ca 13868 return format;
3e73aa7c 13869 }
e57f8c65
TG
13870#endif
13871#if defined (OBJ_MACH_O)
13872 case bfd_target_mach_o_flavour:
d382c579
TG
13873 if (flag_code == CODE_64BIT)
13874 {
13875 use_rela_relocations = 1;
13876 object_64bit = 1;
13877 return "mach-o-x86-64";
13878 }
13879 else
13880 return "mach-o-i386";
4c63da97 13881#endif
252b5132
RH
13882 default:
13883 abort ();
13884 return NULL;
13885 }
13886}
13887
47926f60 13888#endif /* OBJ_MAYBE_ more than one */
252b5132 13889\f
252b5132 13890symbolS *
7016a5d5 13891md_undefined_symbol (char *name)
252b5132 13892{
18dc2407
ILT
13893 if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
13894 && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
13895 && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
13896 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
24eab124
AM
13897 {
13898 if (!GOT_symbol)
13899 {
13900 if (symbol_find (name))
13901 as_bad (_("GOT already in symbol table"));
13902 GOT_symbol = symbol_new (name, undefined_section,
e01e1cee 13903 &zero_address_frag, 0);
24eab124
AM
13904 };
13905 return GOT_symbol;
13906 }
252b5132
RH
13907 return 0;
13908}
13909
13910/* Round up a section size to the appropriate boundary. */
47926f60 13911
252b5132 13912valueT
7016a5d5 13913md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
252b5132 13914{
4c63da97
AM
13915#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
13916 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
13917 {
13918 /* For a.out, force the section size to be aligned. If we don't do
13919 this, BFD will align it for us, but it will not write out the
13920 final bytes of the section. This may be a bug in BFD, but it is
13921 easier to fix it here since that is how the other a.out targets
13922 work. */
13923 int align;
13924
fd361982 13925 align = bfd_section_alignment (segment);
8d3842cd 13926 size = ((size + (1 << align) - 1) & (-((valueT) 1 << align)));
4c63da97 13927 }
252b5132
RH
13928#endif
13929
13930 return size;
13931}
13932
13933/* On the i386, PC-relative offsets are relative to the start of the
13934 next instruction. That is, the address of the offset, plus its
13935 size, since the offset is always the last part of the insn. */
13936
13937long
e3bb37b5 13938md_pcrel_from (fixS *fixP)
252b5132
RH
13939{
13940 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
13941}
13942
13943#ifndef I386COFF
13944
13945static void
e3bb37b5 13946s_bss (int ignore ATTRIBUTE_UNUSED)
252b5132 13947{
29b0f896 13948 int temp;
252b5132 13949
8a75718c
JB
13950#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13951 if (IS_ELF)
13952 obj_elf_section_change_hook ();
13953#endif
252b5132
RH
13954 temp = get_absolute_expression ();
13955 subseg_set (bss_section, (subsegT) temp);
13956 demand_empty_rest_of_line ();
13957}
13958
13959#endif
13960
e379e5f3
L
13961/* Remember constant directive. */
13962
13963void
13964i386_cons_align (int ignore ATTRIBUTE_UNUSED)
13965{
13966 if (last_insn.kind != last_insn_directive
13967 && (bfd_section_flags (now_seg) & SEC_CODE))
13968 {
13969 last_insn.seg = now_seg;
13970 last_insn.kind = last_insn_directive;
13971 last_insn.name = "constant directive";
13972 last_insn.file = as_where (&last_insn.line);
ae531041
L
13973 if (lfence_before_ret != lfence_before_ret_none)
13974 {
13975 if (lfence_before_indirect_branch != lfence_branch_none)
13976 as_warn (_("constant directive skips -mlfence-before-ret "
13977 "and -mlfence-before-indirect-branch"));
13978 else
13979 as_warn (_("constant directive skips -mlfence-before-ret"));
13980 }
13981 else if (lfence_before_indirect_branch != lfence_branch_none)
13982 as_warn (_("constant directive skips -mlfence-before-indirect-branch"));
e379e5f3
L
13983 }
13984}
13985
252b5132 13986void
e3bb37b5 13987i386_validate_fix (fixS *fixp)
252b5132 13988{
02a86693 13989 if (fixp->fx_subsy)
252b5132 13990 {
02a86693 13991 if (fixp->fx_subsy == GOT_symbol)
23df1078 13992 {
02a86693
L
13993 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
13994 {
13995 if (!object_64bit)
13996 abort ();
13997#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13998 if (fixp->fx_tcbit2)
56ceb5b5
L
13999 fixp->fx_r_type = (fixp->fx_tcbit
14000 ? BFD_RELOC_X86_64_REX_GOTPCRELX
14001 : BFD_RELOC_X86_64_GOTPCRELX);
02a86693
L
14002 else
14003#endif
14004 fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
14005 }
d6ab8113 14006 else
02a86693
L
14007 {
14008 if (!object_64bit)
14009 fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
14010 else
14011 fixp->fx_r_type = BFD_RELOC_X86_64_GOTOFF64;
14012 }
14013 fixp->fx_subsy = 0;
23df1078 14014 }
252b5132 14015 }
02a86693 14016#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2585b7a5 14017 else
02a86693 14018 {
2585b7a5
L
14019 /* NB: Commit 292676c1 resolved PLT32 reloc aganst local symbol
14020 to section. Since PLT32 relocation must be against symbols,
14021 turn such PLT32 relocation into PC32 relocation. */
14022 if (fixp->fx_addsy
14023 && (fixp->fx_r_type == BFD_RELOC_386_PLT32
14024 || fixp->fx_r_type == BFD_RELOC_X86_64_PLT32)
14025 && symbol_section_p (fixp->fx_addsy))
14026 fixp->fx_r_type = BFD_RELOC_32_PCREL;
14027 if (!object_64bit)
14028 {
14029 if (fixp->fx_r_type == BFD_RELOC_386_GOT32
14030 && fixp->fx_tcbit2)
14031 fixp->fx_r_type = BFD_RELOC_386_GOT32X;
14032 }
02a86693
L
14033 }
14034#endif
252b5132
RH
14035}
14036
252b5132 14037arelent *
7016a5d5 14038tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
14039{
14040 arelent *rel;
14041 bfd_reloc_code_real_type code;
14042
14043 switch (fixp->fx_r_type)
14044 {
8ce3d284 14045#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
14046 case BFD_RELOC_SIZE32:
14047 case BFD_RELOC_SIZE64:
14048 if (S_IS_DEFINED (fixp->fx_addsy)
14049 && !S_IS_EXTERNAL (fixp->fx_addsy))
14050 {
14051 /* Resolve size relocation against local symbol to size of
14052 the symbol plus addend. */
14053 valueT value = S_GET_SIZE (fixp->fx_addsy) + fixp->fx_offset;
14054 if (fixp->fx_r_type == BFD_RELOC_SIZE32
14055 && !fits_in_unsigned_long (value))
14056 as_bad_where (fixp->fx_file, fixp->fx_line,
14057 _("symbol size computation overflow"));
14058 fixp->fx_addsy = NULL;
14059 fixp->fx_subsy = NULL;
14060 md_apply_fix (fixp, (valueT *) &value, NULL);
14061 return NULL;
14062 }
8ce3d284 14063#endif
1a0670f3 14064 /* Fall through. */
8fd4256d 14065
3e73aa7c
JH
14066 case BFD_RELOC_X86_64_PLT32:
14067 case BFD_RELOC_X86_64_GOT32:
14068 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
14069 case BFD_RELOC_X86_64_GOTPCRELX:
14070 case BFD_RELOC_X86_64_REX_GOTPCRELX:
252b5132
RH
14071 case BFD_RELOC_386_PLT32:
14072 case BFD_RELOC_386_GOT32:
02a86693 14073 case BFD_RELOC_386_GOT32X:
252b5132
RH
14074 case BFD_RELOC_386_GOTOFF:
14075 case BFD_RELOC_386_GOTPC:
13ae64f3
JJ
14076 case BFD_RELOC_386_TLS_GD:
14077 case BFD_RELOC_386_TLS_LDM:
14078 case BFD_RELOC_386_TLS_LDO_32:
14079 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
14080 case BFD_RELOC_386_TLS_IE:
14081 case BFD_RELOC_386_TLS_GOTIE:
13ae64f3
JJ
14082 case BFD_RELOC_386_TLS_LE_32:
14083 case BFD_RELOC_386_TLS_LE:
67a4f2b7
AO
14084 case BFD_RELOC_386_TLS_GOTDESC:
14085 case BFD_RELOC_386_TLS_DESC_CALL:
bffbf940
JJ
14086 case BFD_RELOC_X86_64_TLSGD:
14087 case BFD_RELOC_X86_64_TLSLD:
14088 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 14089 case BFD_RELOC_X86_64_DTPOFF64:
bffbf940
JJ
14090 case BFD_RELOC_X86_64_GOTTPOFF:
14091 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113
JB
14092 case BFD_RELOC_X86_64_TPOFF64:
14093 case BFD_RELOC_X86_64_GOTOFF64:
14094 case BFD_RELOC_X86_64_GOTPC32:
7b81dfbb
AJ
14095 case BFD_RELOC_X86_64_GOT64:
14096 case BFD_RELOC_X86_64_GOTPCREL64:
14097 case BFD_RELOC_X86_64_GOTPC64:
14098 case BFD_RELOC_X86_64_GOTPLT64:
14099 case BFD_RELOC_X86_64_PLTOFF64:
67a4f2b7
AO
14100 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14101 case BFD_RELOC_X86_64_TLSDESC_CALL:
252b5132
RH
14102 case BFD_RELOC_RVA:
14103 case BFD_RELOC_VTABLE_ENTRY:
14104 case BFD_RELOC_VTABLE_INHERIT:
6482c264
NC
14105#ifdef TE_PE
14106 case BFD_RELOC_32_SECREL:
14107#endif
252b5132
RH
14108 code = fixp->fx_r_type;
14109 break;
dbbaec26
L
14110 case BFD_RELOC_X86_64_32S:
14111 if (!fixp->fx_pcrel)
14112 {
14113 /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32. */
14114 code = fixp->fx_r_type;
14115 break;
14116 }
1a0670f3 14117 /* Fall through. */
252b5132 14118 default:
93382f6d 14119 if (fixp->fx_pcrel)
252b5132 14120 {
93382f6d
AM
14121 switch (fixp->fx_size)
14122 {
14123 default:
b091f402
AM
14124 as_bad_where (fixp->fx_file, fixp->fx_line,
14125 _("can not do %d byte pc-relative relocation"),
14126 fixp->fx_size);
93382f6d
AM
14127 code = BFD_RELOC_32_PCREL;
14128 break;
14129 case 1: code = BFD_RELOC_8_PCREL; break;
14130 case 2: code = BFD_RELOC_16_PCREL; break;
d258b828 14131 case 4: code = BFD_RELOC_32_PCREL; break;
d6ab8113
JB
14132#ifdef BFD64
14133 case 8: code = BFD_RELOC_64_PCREL; break;
14134#endif
93382f6d
AM
14135 }
14136 }
14137 else
14138 {
14139 switch (fixp->fx_size)
14140 {
14141 default:
b091f402
AM
14142 as_bad_where (fixp->fx_file, fixp->fx_line,
14143 _("can not do %d byte relocation"),
14144 fixp->fx_size);
93382f6d
AM
14145 code = BFD_RELOC_32;
14146 break;
14147 case 1: code = BFD_RELOC_8; break;
14148 case 2: code = BFD_RELOC_16; break;
14149 case 4: code = BFD_RELOC_32; break;
937149dd 14150#ifdef BFD64
3e73aa7c 14151 case 8: code = BFD_RELOC_64; break;
937149dd 14152#endif
93382f6d 14153 }
252b5132
RH
14154 }
14155 break;
14156 }
252b5132 14157
d182319b
JB
14158 if ((code == BFD_RELOC_32
14159 || code == BFD_RELOC_32_PCREL
14160 || code == BFD_RELOC_X86_64_32S)
252b5132
RH
14161 && GOT_symbol
14162 && fixp->fx_addsy == GOT_symbol)
3e73aa7c 14163 {
4fa24527 14164 if (!object_64bit)
d6ab8113
JB
14165 code = BFD_RELOC_386_GOTPC;
14166 else
14167 code = BFD_RELOC_X86_64_GOTPC32;
3e73aa7c 14168 }
7b81dfbb
AJ
14169 if ((code == BFD_RELOC_64 || code == BFD_RELOC_64_PCREL)
14170 && GOT_symbol
14171 && fixp->fx_addsy == GOT_symbol)
14172 {
14173 code = BFD_RELOC_X86_64_GOTPC64;
14174 }
252b5132 14175
add39d23
TS
14176 rel = XNEW (arelent);
14177 rel->sym_ptr_ptr = XNEW (asymbol *);
49309057 14178 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
14179
14180 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
c87db184 14181
3e73aa7c
JH
14182 if (!use_rela_relocations)
14183 {
14184 /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
14185 vtable entry to be used in the relocation's section offset. */
14186 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
14187 rel->address = fixp->fx_offset;
fbeb56a4
DK
14188#if defined (OBJ_COFF) && defined (TE_PE)
14189 else if (fixp->fx_addsy && S_IS_WEAK (fixp->fx_addsy))
14190 rel->addend = fixp->fx_addnumber - (S_GET_VALUE (fixp->fx_addsy) * 2);
14191 else
14192#endif
c6682705 14193 rel->addend = 0;
3e73aa7c
JH
14194 }
14195 /* Use the rela in 64bit mode. */
252b5132 14196 else
3e73aa7c 14197 {
862be3fb
L
14198 if (disallow_64bit_reloc)
14199 switch (code)
14200 {
862be3fb
L
14201 case BFD_RELOC_X86_64_DTPOFF64:
14202 case BFD_RELOC_X86_64_TPOFF64:
14203 case BFD_RELOC_64_PCREL:
14204 case BFD_RELOC_X86_64_GOTOFF64:
14205 case BFD_RELOC_X86_64_GOT64:
14206 case BFD_RELOC_X86_64_GOTPCREL64:
14207 case BFD_RELOC_X86_64_GOTPC64:
14208 case BFD_RELOC_X86_64_GOTPLT64:
14209 case BFD_RELOC_X86_64_PLTOFF64:
14210 as_bad_where (fixp->fx_file, fixp->fx_line,
14211 _("cannot represent relocation type %s in x32 mode"),
14212 bfd_get_reloc_code_name (code));
14213 break;
14214 default:
14215 break;
14216 }
14217
062cd5e7
AS
14218 if (!fixp->fx_pcrel)
14219 rel->addend = fixp->fx_offset;
14220 else
14221 switch (code)
14222 {
14223 case BFD_RELOC_X86_64_PLT32:
14224 case BFD_RELOC_X86_64_GOT32:
14225 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
14226 case BFD_RELOC_X86_64_GOTPCRELX:
14227 case BFD_RELOC_X86_64_REX_GOTPCRELX:
bffbf940
JJ
14228 case BFD_RELOC_X86_64_TLSGD:
14229 case BFD_RELOC_X86_64_TLSLD:
14230 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7
AO
14231 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14232 case BFD_RELOC_X86_64_TLSDESC_CALL:
062cd5e7
AS
14233 rel->addend = fixp->fx_offset - fixp->fx_size;
14234 break;
14235 default:
14236 rel->addend = (section->vma
14237 - fixp->fx_size
14238 + fixp->fx_addnumber
14239 + md_pcrel_from (fixp));
14240 break;
14241 }
3e73aa7c
JH
14242 }
14243
252b5132
RH
14244 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
14245 if (rel->howto == NULL)
14246 {
14247 as_bad_where (fixp->fx_file, fixp->fx_line,
d0b47220 14248 _("cannot represent relocation type %s"),
252b5132
RH
14249 bfd_get_reloc_code_name (code));
14250 /* Set howto to a garbage value so that we can keep going. */
14251 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
9c2799c2 14252 gas_assert (rel->howto != NULL);
252b5132
RH
14253 }
14254
14255 return rel;
14256}
14257
ee86248c 14258#include "tc-i386-intel.c"
54cfded0 14259
a60de03c
JB
14260void
14261tc_x86_parse_to_dw2regnum (expressionS *exp)
54cfded0 14262{
a60de03c
JB
14263 int saved_naked_reg;
14264 char saved_register_dot;
54cfded0 14265
a60de03c
JB
14266 saved_naked_reg = allow_naked_reg;
14267 allow_naked_reg = 1;
14268 saved_register_dot = register_chars['.'];
14269 register_chars['.'] = '.';
14270 allow_pseudo_reg = 1;
14271 expression_and_evaluate (exp);
14272 allow_pseudo_reg = 0;
14273 register_chars['.'] = saved_register_dot;
14274 allow_naked_reg = saved_naked_reg;
14275
e96d56a1 14276 if (exp->X_op == O_register && exp->X_add_number >= 0)
54cfded0 14277 {
a60de03c
JB
14278 if ((addressT) exp->X_add_number < i386_regtab_size)
14279 {
14280 exp->X_op = O_constant;
14281 exp->X_add_number = i386_regtab[exp->X_add_number]
14282 .dw2_regnum[flag_code >> 1];
14283 }
14284 else
14285 exp->X_op = O_illegal;
54cfded0 14286 }
54cfded0
AM
14287}
14288
14289void
14290tc_x86_frame_initial_instructions (void)
14291{
a60de03c
JB
14292 static unsigned int sp_regno[2];
14293
14294 if (!sp_regno[flag_code >> 1])
14295 {
14296 char *saved_input = input_line_pointer;
14297 char sp[][4] = {"esp", "rsp"};
14298 expressionS exp;
a4447b93 14299
a60de03c
JB
14300 input_line_pointer = sp[flag_code >> 1];
14301 tc_x86_parse_to_dw2regnum (&exp);
9c2799c2 14302 gas_assert (exp.X_op == O_constant);
a60de03c
JB
14303 sp_regno[flag_code >> 1] = exp.X_add_number;
14304 input_line_pointer = saved_input;
14305 }
a4447b93 14306
61ff971f
L
14307 cfi_add_CFA_def_cfa (sp_regno[flag_code >> 1], -x86_cie_data_alignment);
14308 cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
54cfded0 14309}
d2b2c203 14310
d7921315
L
14311int
14312x86_dwarf2_addr_size (void)
14313{
14314#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
14315 if (x86_elf_abi == X86_64_X32_ABI)
14316 return 4;
14317#endif
14318 return bfd_arch_bits_per_address (stdoutput) / 8;
14319}
14320
d2b2c203
DJ
14321int
14322i386_elf_section_type (const char *str, size_t len)
14323{
14324 if (flag_code == CODE_64BIT
14325 && len == sizeof ("unwind") - 1
d34049e8 14326 && startswith (str, "unwind"))
d2b2c203
DJ
14327 return SHT_X86_64_UNWIND;
14328
14329 return -1;
14330}
bb41ade5 14331
ad5fec3b
EB
14332#ifdef TE_SOLARIS
14333void
14334i386_solaris_fix_up_eh_frame (segT sec)
14335{
14336 if (flag_code == CODE_64BIT)
14337 elf_section_type (sec) = SHT_X86_64_UNWIND;
14338}
14339#endif
14340
bb41ade5
AM
14341#ifdef TE_PE
14342void
14343tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
14344{
91d6fa6a 14345 expressionS exp;
bb41ade5 14346
91d6fa6a
NC
14347 exp.X_op = O_secrel;
14348 exp.X_add_symbol = symbol;
14349 exp.X_add_number = 0;
14350 emit_expr (&exp, size);
bb41ade5
AM
14351}
14352#endif
3b22753a
L
14353
14354#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14355/* For ELF on x86-64, add support for SHF_X86_64_LARGE. */
14356
01e1a5bc 14357bfd_vma
6d4af3c2 14358x86_64_section_letter (int letter, const char **ptr_msg)
3b22753a
L
14359{
14360 if (flag_code == CODE_64BIT)
14361 {
14362 if (letter == 'l')
14363 return SHF_X86_64_LARGE;
14364
8f3bae45 14365 *ptr_msg = _("bad .section directive: want a,l,w,x,M,S,G,T in string");
64e74474 14366 }
3b22753a 14367 else
8f3bae45 14368 *ptr_msg = _("bad .section directive: want a,w,x,M,S,G,T in string");
3b22753a
L
14369 return -1;
14370}
14371
01e1a5bc 14372bfd_vma
3b22753a
L
14373x86_64_section_word (char *str, size_t len)
14374{
08dedd66 14375 if (len == 5 && flag_code == CODE_64BIT && startswith (str, "large"))
3b22753a
L
14376 return SHF_X86_64_LARGE;
14377
14378 return -1;
14379}
14380
14381static void
14382handle_large_common (int small ATTRIBUTE_UNUSED)
14383{
14384 if (flag_code != CODE_64BIT)
14385 {
14386 s_comm_internal (0, elf_common_parse);
14387 as_warn (_(".largecomm supported only in 64bit mode, producing .comm"));
14388 }
14389 else
14390 {
14391 static segT lbss_section;
14392 asection *saved_com_section_ptr = elf_com_section_ptr;
14393 asection *saved_bss_section = bss_section;
14394
14395 if (lbss_section == NULL)
14396 {
14397 flagword applicable;
14398 segT seg = now_seg;
14399 subsegT subseg = now_subseg;
14400
14401 /* The .lbss section is for local .largecomm symbols. */
14402 lbss_section = subseg_new (".lbss", 0);
14403 applicable = bfd_applicable_section_flags (stdoutput);
fd361982 14404 bfd_set_section_flags (lbss_section, applicable & SEC_ALLOC);
3b22753a
L
14405 seg_info (lbss_section)->bss = 1;
14406
14407 subseg_set (seg, subseg);
14408 }
14409
14410 elf_com_section_ptr = &_bfd_elf_large_com_section;
14411 bss_section = lbss_section;
14412
14413 s_comm_internal (0, elf_common_parse);
14414
14415 elf_com_section_ptr = saved_com_section_ptr;
14416 bss_section = saved_bss_section;
14417 }
14418}
14419#endif /* OBJ_ELF || OBJ_MAYBE_ELF */
This page took 2.719508 seconds and 4 git commands to generate.