x86: adjust st(<N>) parsing
[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"
252b5132 35
41fd2579
L
36#ifdef HAVE_LIMITS_H
37#include <limits.h>
38#else
39#ifdef HAVE_SYS_PARAM_H
40#include <sys/param.h>
41#endif
42#ifndef INT_MAX
43#define INT_MAX (int) (((unsigned) (-1)) >> 1)
44#endif
45#endif
46
c3332e24 47#ifndef INFER_ADDR_PREFIX
eecb386c 48#define INFER_ADDR_PREFIX 1
c3332e24
AM
49#endif
50
29b0f896
AM
51#ifndef DEFAULT_ARCH
52#define DEFAULT_ARCH "i386"
246fcdee 53#endif
252b5132 54
edde18a5
AM
55#ifndef INLINE
56#if __GNUC__ >= 2
57#define INLINE __inline__
58#else
59#define INLINE
60#endif
61#endif
62
6305a203
L
63/* Prefixes will be emitted in the order defined below.
64 WAIT_PREFIX must be the first prefix since FWAIT is really is an
65 instruction, and so must come before any prefixes.
66 The preferred prefix order is SEG_PREFIX, ADDR_PREFIX, DATA_PREFIX,
42164a71 67 REP_PREFIX/HLE_PREFIX, LOCK_PREFIX. */
6305a203
L
68#define WAIT_PREFIX 0
69#define SEG_PREFIX 1
70#define ADDR_PREFIX 2
71#define DATA_PREFIX 3
c32fa91d 72#define REP_PREFIX 4
42164a71 73#define HLE_PREFIX REP_PREFIX
7e8b059b 74#define BND_PREFIX REP_PREFIX
c32fa91d 75#define LOCK_PREFIX 5
4e9ac44a
L
76#define REX_PREFIX 6 /* must come last. */
77#define MAX_PREFIXES 7 /* max prefixes per opcode */
6305a203
L
78
79/* we define the syntax here (modulo base,index,scale syntax) */
80#define REGISTER_PREFIX '%'
81#define IMMEDIATE_PREFIX '$'
82#define ABSOLUTE_PREFIX '*'
83
84/* these are the instruction mnemonic suffixes in AT&T syntax or
85 memory operand size in Intel syntax. */
86#define WORD_MNEM_SUFFIX 'w'
87#define BYTE_MNEM_SUFFIX 'b'
88#define SHORT_MNEM_SUFFIX 's'
89#define LONG_MNEM_SUFFIX 'l'
90#define QWORD_MNEM_SUFFIX 'q'
6305a203
L
91/* Intel Syntax. Use a non-ascii letter since since it never appears
92 in instructions. */
93#define LONG_DOUBLE_MNEM_SUFFIX '\1'
94
95#define END_OF_INSN '\0'
96
79dec6b7
JB
97/* This matches the C -> StaticRounding alias in the opcode table. */
98#define commutative staticrounding
99
6305a203
L
100/*
101 'templates' is for grouping together 'template' structures for opcodes
102 of the same name. This is only used for storing the insns in the grand
103 ole hash table of insns.
104 The templates themselves start at START and range up to (but not including)
105 END.
106 */
107typedef struct
108{
d3ce72d0
NC
109 const insn_template *start;
110 const insn_template *end;
6305a203
L
111}
112templates;
113
114/* 386 operand encoding bytes: see 386 book for details of this. */
115typedef struct
116{
117 unsigned int regmem; /* codes register or memory operand */
118 unsigned int reg; /* codes register operand (or extended opcode) */
119 unsigned int mode; /* how to interpret regmem & reg */
120}
121modrm_byte;
122
123/* x86-64 extension prefix. */
124typedef int rex_byte;
125
6305a203
L
126/* 386 opcode byte to code indirect addressing. */
127typedef struct
128{
129 unsigned base;
130 unsigned index;
131 unsigned scale;
132}
133sib_byte;
134
6305a203
L
135/* x86 arch names, types and features */
136typedef struct
137{
138 const char *name; /* arch name */
8a2c8fef 139 unsigned int len; /* arch string length */
6305a203
L
140 enum processor_type type; /* arch type */
141 i386_cpu_flags flags; /* cpu feature flags */
8a2c8fef 142 unsigned int skip; /* show_arch should skip this. */
6305a203
L
143}
144arch_entry;
145
293f5f65
L
146/* Used to turn off indicated flags. */
147typedef struct
148{
149 const char *name; /* arch name */
150 unsigned int len; /* arch string length */
151 i386_cpu_flags flags; /* cpu feature flags */
152}
153noarch_entry;
154
78f12dd3 155static void update_code_flag (int, int);
e3bb37b5
L
156static void set_code_flag (int);
157static void set_16bit_gcc_code_flag (int);
158static void set_intel_syntax (int);
1efbbeb4 159static void set_intel_mnemonic (int);
db51cc60 160static void set_allow_index_reg (int);
7bab8ab5 161static void set_check (int);
e3bb37b5 162static void set_cpu_arch (int);
6482c264 163#ifdef TE_PE
e3bb37b5 164static void pe_directive_secrel (int);
6482c264 165#endif
e3bb37b5
L
166static void signed_cons (int);
167static char *output_invalid (int c);
ee86248c
JB
168static int i386_finalize_immediate (segT, expressionS *, i386_operand_type,
169 const char *);
170static int i386_finalize_displacement (segT, expressionS *, i386_operand_type,
171 const char *);
a7619375 172static int i386_att_operand (char *);
e3bb37b5 173static int i386_intel_operand (char *, int);
ee86248c
JB
174static int i386_intel_simplify (expressionS *);
175static int i386_intel_parse_name (const char *, expressionS *);
e3bb37b5
L
176static const reg_entry *parse_register (char *, char **);
177static char *parse_insn (char *, char *);
178static char *parse_operands (char *, const char *);
179static void swap_operands (void);
783c187b 180static void swap_2_operands (unsigned int, unsigned int);
48bcea9f 181static enum flag_code i386_addressing_mode (void);
e3bb37b5
L
182static void optimize_imm (void);
183static void optimize_disp (void);
83b16ac6 184static const insn_template *match_template (char);
e3bb37b5
L
185static int check_string (void);
186static int process_suffix (void);
187static int check_byte_reg (void);
188static int check_long_reg (void);
189static int check_qword_reg (void);
190static int check_word_reg (void);
191static int finalize_imm (void);
192static int process_operands (void);
193static const seg_entry *build_modrm_byte (void);
194static void output_insn (void);
195static void output_imm (fragS *, offsetT);
196static void output_disp (fragS *, offsetT);
29b0f896 197#ifndef I386COFF
e3bb37b5 198static void s_bss (int);
252b5132 199#endif
17d4e2a2
L
200#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
201static void handle_large_common (int small ATTRIBUTE_UNUSED);
b4a3a7b4
L
202
203/* GNU_PROPERTY_X86_ISA_1_USED. */
204static unsigned int x86_isa_1_used;
205/* GNU_PROPERTY_X86_FEATURE_2_USED. */
206static unsigned int x86_feature_2_used;
207/* Generate x86 used ISA and feature properties. */
208static unsigned int x86_used_note = DEFAULT_X86_USED_NOTE;
17d4e2a2 209#endif
252b5132 210
a847613f 211static const char *default_arch = DEFAULT_ARCH;
3e73aa7c 212
8a6fb3f9
JB
213/* parse_register() returns this when a register alias cannot be used. */
214static const reg_entry bad_reg = { "<bad>", OPERAND_TYPE_NONE, 0, 0,
215 { Dw2Inval, Dw2Inval } };
216
6288d05f 217static const reg_entry *reg_st0;
6225c532
JB
218static const reg_entry *reg_k0;
219
c0f3af97
L
220/* VEX prefix. */
221typedef struct
222{
43234a1e
L
223 /* VEX prefix is either 2 byte or 3 byte. EVEX is 4 byte. */
224 unsigned char bytes[4];
c0f3af97
L
225 unsigned int length;
226 /* Destination or source register specifier. */
227 const reg_entry *register_specifier;
228} vex_prefix;
229
252b5132 230/* 'md_assemble ()' gathers together information and puts it into a
47926f60 231 i386_insn. */
252b5132 232
520dc8e8
AM
233union i386_op
234 {
235 expressionS *disps;
236 expressionS *imms;
237 const reg_entry *regs;
238 };
239
a65babc9
L
240enum i386_error
241 {
86e026a4 242 operand_size_mismatch,
a65babc9
L
243 operand_type_mismatch,
244 register_type_mismatch,
245 number_of_operands_mismatch,
246 invalid_instruction_suffix,
247 bad_imm4,
a65babc9
L
248 unsupported_with_intel_mnemonic,
249 unsupported_syntax,
6c30d220 250 unsupported,
260cd341 251 invalid_sib_address,
6c30d220 252 invalid_vsib_address,
7bab8ab5 253 invalid_vector_register_set,
260cd341 254 invalid_tmm_register_set,
43234a1e
L
255 unsupported_vector_index_register,
256 unsupported_broadcast,
43234a1e
L
257 broadcast_needed,
258 unsupported_masking,
259 mask_not_on_destination,
260 no_default_mask,
261 unsupported_rc_sae,
262 rc_sae_operand_not_last_imm,
263 invalid_register_operand,
a65babc9
L
264 };
265
252b5132
RH
266struct _i386_insn
267 {
47926f60 268 /* TM holds the template for the insn were currently assembling. */
d3ce72d0 269 insn_template tm;
252b5132 270
7d5e4556
L
271 /* SUFFIX holds the instruction size suffix for byte, word, dword
272 or qword, if given. */
252b5132
RH
273 char suffix;
274
9a182d04
JB
275 /* OPCODE_LENGTH holds the number of base opcode bytes. */
276 unsigned char opcode_length;
277
47926f60 278 /* OPERANDS gives the number of given operands. */
252b5132
RH
279 unsigned int operands;
280
281 /* REG_OPERANDS, DISP_OPERANDS, MEM_OPERANDS, IMM_OPERANDS give the number
282 of given register, displacement, memory operands and immediate
47926f60 283 operands. */
252b5132
RH
284 unsigned int reg_operands, disp_operands, mem_operands, imm_operands;
285
286 /* TYPES [i] is the type (see above #defines) which tells us how to
520dc8e8 287 use OP[i] for the corresponding operand. */
40fb9820 288 i386_operand_type types[MAX_OPERANDS];
252b5132 289
520dc8e8
AM
290 /* Displacement expression, immediate expression, or register for each
291 operand. */
292 union i386_op op[MAX_OPERANDS];
252b5132 293
3e73aa7c
JH
294 /* Flags for operands. */
295 unsigned int flags[MAX_OPERANDS];
296#define Operand_PCrel 1
c48dadc9 297#define Operand_Mem 2
3e73aa7c 298
252b5132 299 /* Relocation type for operand */
f86103b7 300 enum bfd_reloc_code_real reloc[MAX_OPERANDS];
252b5132 301
252b5132
RH
302 /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
303 the base index byte below. */
304 const reg_entry *base_reg;
305 const reg_entry *index_reg;
306 unsigned int log2_scale_factor;
307
308 /* SEG gives the seg_entries of this insn. They are zero unless
47926f60 309 explicit segment overrides are given. */
ce8a8b2f 310 const seg_entry *seg[2];
252b5132 311
8325cc63
JB
312 /* Copied first memory operand string, for re-checking. */
313 char *memop1_string;
314
252b5132
RH
315 /* PREFIX holds all the given prefix opcodes (usually null).
316 PREFIXES is the number of prefix opcodes. */
317 unsigned int prefixes;
318 unsigned char prefix[MAX_PREFIXES];
319
50128d0c
JB
320 /* Register is in low 3 bits of opcode. */
321 bfd_boolean short_form;
322
6f2f06be
JB
323 /* The operand to a branch insn indicates an absolute branch. */
324 bfd_boolean jumpabsolute;
325
921eafea
L
326 /* Extended states. */
327 enum
328 {
329 /* Use MMX state. */
330 xstate_mmx = 1 << 0,
331 /* Use XMM state. */
332 xstate_xmm = 1 << 1,
333 /* Use YMM state. */
334 xstate_ymm = 1 << 2 | xstate_xmm,
335 /* Use ZMM state. */
336 xstate_zmm = 1 << 3 | xstate_ymm,
337 /* Use TMM state. */
32930e4e
L
338 xstate_tmm = 1 << 4,
339 /* Use MASK state. */
340 xstate_mask = 1 << 5
921eafea 341 } xstate;
260cd341 342
e379e5f3
L
343 /* Has GOTPC or TLS relocation. */
344 bfd_boolean has_gotpc_tls_reloc;
345
252b5132 346 /* RM and SIB are the modrm byte and the sib byte where the
c1e679ec 347 addressing modes of this insn are encoded. */
252b5132 348 modrm_byte rm;
3e73aa7c 349 rex_byte rex;
43234a1e 350 rex_byte vrex;
252b5132 351 sib_byte sib;
c0f3af97 352 vex_prefix vex;
b6169b20 353
6225c532
JB
354 /* Masking attributes.
355
356 The struct describes masking, applied to OPERAND in the instruction.
357 REG is a pointer to the corresponding mask register. ZEROING tells
358 whether merging or zeroing mask is used. */
359 struct Mask_Operation
360 {
361 const reg_entry *reg;
362 unsigned int zeroing;
363 /* The operand where this operation is associated. */
364 unsigned int operand;
365 } mask;
43234a1e
L
366
367 /* Rounding control and SAE attributes. */
ca5312a2
JB
368 struct RC_Operation
369 {
370 enum rc_type
371 {
372 rc_none = -1,
373 rne,
374 rd,
375 ru,
376 rz,
377 saeonly
378 } type;
379
380 unsigned int operand;
381 } rounding;
43234a1e 382
5273a3cd
JB
383 /* Broadcasting attributes.
384
385 The struct describes broadcasting, applied to OPERAND. TYPE is
386 expresses the broadcast factor. */
387 struct Broadcast_Operation
388 {
389 /* Type of broadcast: {1to2}, {1to4}, {1to8}, or {1to16}. */
390 unsigned int type;
391
392 /* Index of broadcasted operand. */
393 unsigned int operand;
394
395 /* Number of bytes to broadcast. */
396 unsigned int bytes;
397 } broadcast;
43234a1e
L
398
399 /* Compressed disp8*N attribute. */
400 unsigned int memshift;
401
86fa6981
L
402 /* Prefer load or store in encoding. */
403 enum
404 {
405 dir_encoding_default = 0,
406 dir_encoding_load,
64c49ab3
JB
407 dir_encoding_store,
408 dir_encoding_swap
86fa6981 409 } dir_encoding;
891edac4 410
41eb8e88 411 /* Prefer 8bit, 16bit, 32bit displacement in encoding. */
a501d77e
L
412 enum
413 {
414 disp_encoding_default = 0,
415 disp_encoding_8bit,
41eb8e88 416 disp_encoding_16bit,
a501d77e
L
417 disp_encoding_32bit
418 } disp_encoding;
f8a5c266 419
6b6b6807
L
420 /* Prefer the REX byte in encoding. */
421 bfd_boolean rex_encoding;
422
b6f8c7c4
L
423 /* Disable instruction size optimization. */
424 bfd_boolean no_optimize;
425
86fa6981
L
426 /* How to encode vector instructions. */
427 enum
428 {
429 vex_encoding_default = 0,
42e04b36 430 vex_encoding_vex,
86fa6981 431 vex_encoding_vex3,
da4977e0
JB
432 vex_encoding_evex,
433 vex_encoding_error
86fa6981
L
434 } vec_encoding;
435
d5de92cf
L
436 /* REP prefix. */
437 const char *rep_prefix;
438
165de32a
L
439 /* HLE prefix. */
440 const char *hle_prefix;
42164a71 441
7e8b059b
L
442 /* Have BND prefix. */
443 const char *bnd_prefix;
444
04ef582a
L
445 /* Have NOTRACK prefix. */
446 const char *notrack_prefix;
447
891edac4 448 /* Error message. */
a65babc9 449 enum i386_error error;
252b5132
RH
450 };
451
452typedef struct _i386_insn i386_insn;
453
43234a1e
L
454/* Link RC type with corresponding string, that'll be looked for in
455 asm. */
456struct RC_name
457{
458 enum rc_type type;
459 const char *name;
460 unsigned int len;
461};
462
463static const struct RC_name RC_NamesTable[] =
464{
465 { rne, STRING_COMMA_LEN ("rn-sae") },
466 { rd, STRING_COMMA_LEN ("rd-sae") },
467 { ru, STRING_COMMA_LEN ("ru-sae") },
468 { rz, STRING_COMMA_LEN ("rz-sae") },
469 { saeonly, STRING_COMMA_LEN ("sae") },
470};
471
252b5132
RH
472/* List of chars besides those in app.c:symbol_chars that can start an
473 operand. Used to prevent the scrubber eating vital white-space. */
86fa6981 474const char extra_symbol_chars[] = "*%-([{}"
252b5132 475#ifdef LEX_AT
32137342
NC
476 "@"
477#endif
478#ifdef LEX_QM
479 "?"
252b5132 480#endif
32137342 481 ;
252b5132 482
b3983e5f
JB
483#if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
484 && !defined (TE_GNU) \
485 && !defined (TE_LINUX) \
486 && !defined (TE_FreeBSD) \
487 && !defined (TE_DragonFly) \
488 && !defined (TE_NetBSD))
252b5132 489/* This array holds the chars that always start a comment. If the
b3b91714
AM
490 pre-processor is disabled, these aren't very useful. The option
491 --divide will remove '/' from this list. */
492const char *i386_comment_chars = "#/";
493#define SVR4_COMMENT_CHARS 1
252b5132 494#define PREFIX_SEPARATOR '\\'
252b5132 495
b3b91714
AM
496#else
497const char *i386_comment_chars = "#";
498#define PREFIX_SEPARATOR '/'
499#endif
500
252b5132
RH
501/* This array holds the chars that only start a comment at the beginning of
502 a line. If the line seems to have the form '# 123 filename'
ce8a8b2f
AM
503 .line and .file directives will appear in the pre-processed output.
504 Note that input_file.c hand checks for '#' at the beginning of the
252b5132 505 first line of the input file. This is because the compiler outputs
ce8a8b2f
AM
506 #NO_APP at the beginning of its output.
507 Also note that comments started like this one will always work if
252b5132 508 '/' isn't otherwise defined. */
b3b91714 509const char line_comment_chars[] = "#/";
252b5132 510
63a0b638 511const char line_separator_chars[] = ";";
252b5132 512
ce8a8b2f
AM
513/* Chars that can be used to separate mant from exp in floating point
514 nums. */
252b5132
RH
515const char EXP_CHARS[] = "eE";
516
ce8a8b2f
AM
517/* Chars that mean this number is a floating point constant
518 As in 0f12.456
519 or 0d1.2345e12. */
252b5132
RH
520const char FLT_CHARS[] = "fFdDxX";
521
ce8a8b2f 522/* Tables for lexical analysis. */
252b5132
RH
523static char mnemonic_chars[256];
524static char register_chars[256];
525static char operand_chars[256];
526static char identifier_chars[256];
527static char digit_chars[256];
528
ce8a8b2f 529/* Lexical macros. */
252b5132
RH
530#define is_mnemonic_char(x) (mnemonic_chars[(unsigned char) x])
531#define is_operand_char(x) (operand_chars[(unsigned char) x])
532#define is_register_char(x) (register_chars[(unsigned char) x])
533#define is_space_char(x) ((x) == ' ')
534#define is_identifier_char(x) (identifier_chars[(unsigned char) x])
535#define is_digit_char(x) (digit_chars[(unsigned char) x])
536
0234cb7c 537/* All non-digit non-letter characters that may occur in an operand. */
252b5132
RH
538static char operand_special_chars[] = "%$-+(,)*._~/<>|&^!:[@]";
539
540/* md_assemble() always leaves the strings it's passed unaltered. To
541 effect this we maintain a stack of saved characters that we've smashed
542 with '\0's (indicating end of strings for various sub-fields of the
47926f60 543 assembler instruction). */
252b5132 544static char save_stack[32];
ce8a8b2f 545static char *save_stack_p;
252b5132
RH
546#define END_STRING_AND_SAVE(s) \
547 do { *save_stack_p++ = *(s); *(s) = '\0'; } while (0)
548#define RESTORE_END_STRING(s) \
549 do { *(s) = *--save_stack_p; } while (0)
550
47926f60 551/* The instruction we're assembling. */
252b5132
RH
552static i386_insn i;
553
554/* Possible templates for current insn. */
555static const templates *current_templates;
556
31b2323c
L
557/* Per instruction expressionS buffers: max displacements & immediates. */
558static expressionS disp_expressions[MAX_MEMORY_OPERANDS];
559static expressionS im_expressions[MAX_IMMEDIATE_OPERANDS];
252b5132 560
47926f60 561/* Current operand we are working on. */
ee86248c 562static int this_operand = -1;
252b5132 563
3e73aa7c
JH
564/* We support four different modes. FLAG_CODE variable is used to distinguish
565 these. */
566
567enum flag_code {
568 CODE_32BIT,
569 CODE_16BIT,
570 CODE_64BIT };
571
572static enum flag_code flag_code;
4fa24527 573static unsigned int object_64bit;
862be3fb 574static unsigned int disallow_64bit_reloc;
3e73aa7c 575static int use_rela_relocations = 0;
e379e5f3
L
576/* __tls_get_addr/___tls_get_addr symbol for TLS. */
577static const char *tls_get_addr;
3e73aa7c 578
7af8ed2d
NC
579#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
580 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
581 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
582
351f65ca
L
583/* The ELF ABI to use. */
584enum x86_elf_abi
585{
586 I386_ABI,
7f56bc95
L
587 X86_64_ABI,
588 X86_64_X32_ABI
351f65ca
L
589};
590
591static enum x86_elf_abi x86_elf_abi = I386_ABI;
7af8ed2d 592#endif
351f65ca 593
167ad85b
TG
594#if defined (TE_PE) || defined (TE_PEP)
595/* Use big object file format. */
596static int use_big_obj = 0;
597#endif
598
8dcea932
L
599#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
600/* 1 if generating code for a shared library. */
601static int shared = 0;
602#endif
603
47926f60
KH
604/* 1 for intel syntax,
605 0 if att syntax. */
606static int intel_syntax = 0;
252b5132 607
4b5aaf5f
L
608static enum x86_64_isa
609{
610 amd64 = 1, /* AMD64 ISA. */
611 intel64 /* Intel64 ISA. */
612} isa64;
e89c5eaa 613
1efbbeb4
L
614/* 1 for intel mnemonic,
615 0 if att mnemonic. */
616static int intel_mnemonic = !SYSV386_COMPAT;
617
a60de03c
JB
618/* 1 if pseudo registers are permitted. */
619static int allow_pseudo_reg = 0;
620
47926f60
KH
621/* 1 if register prefix % not required. */
622static int allow_naked_reg = 0;
252b5132 623
33eaf5de 624/* 1 if the assembler should add BND prefix for all control-transferring
7e8b059b
L
625 instructions supporting it, even if this prefix wasn't specified
626 explicitly. */
627static int add_bnd_prefix = 0;
628
ba104c83 629/* 1 if pseudo index register, eiz/riz, is allowed . */
db51cc60
L
630static int allow_index_reg = 0;
631
d022bddd
IT
632/* 1 if the assembler should ignore LOCK prefix, even if it was
633 specified explicitly. */
634static int omit_lock_prefix = 0;
635
e4e00185
AS
636/* 1 if the assembler should encode lfence, mfence, and sfence as
637 "lock addl $0, (%{re}sp)". */
638static int avoid_fence = 0;
639
ae531041
L
640/* 1 if lfence should be inserted after every load. */
641static int lfence_after_load = 0;
642
643/* Non-zero if lfence should be inserted before indirect branch. */
644static enum lfence_before_indirect_branch_kind
645 {
646 lfence_branch_none = 0,
647 lfence_branch_register,
648 lfence_branch_memory,
649 lfence_branch_all
650 }
651lfence_before_indirect_branch;
652
653/* Non-zero if lfence should be inserted before ret. */
654static enum lfence_before_ret_kind
655 {
656 lfence_before_ret_none = 0,
657 lfence_before_ret_not,
a09f656b 658 lfence_before_ret_or,
659 lfence_before_ret_shl
ae531041
L
660 }
661lfence_before_ret;
662
663/* Types of previous instruction is .byte or prefix. */
e379e5f3
L
664static struct
665 {
666 segT seg;
667 const char *file;
668 const char *name;
669 unsigned int line;
670 enum last_insn_kind
671 {
672 last_insn_other = 0,
673 last_insn_directive,
674 last_insn_prefix
675 } kind;
676 } last_insn;
677
0cb4071e
L
678/* 1 if the assembler should generate relax relocations. */
679
680static int generate_relax_relocations
681 = DEFAULT_GENERATE_X86_RELAX_RELOCATIONS;
682
7bab8ab5 683static enum check_kind
daf50ae7 684 {
7bab8ab5
JB
685 check_none = 0,
686 check_warning,
687 check_error
daf50ae7 688 }
7bab8ab5 689sse_check, operand_check = check_warning;
daf50ae7 690
e379e5f3
L
691/* Non-zero if branches should be aligned within power of 2 boundary. */
692static int align_branch_power = 0;
693
694/* Types of branches to align. */
695enum align_branch_kind
696 {
697 align_branch_none = 0,
698 align_branch_jcc = 1,
699 align_branch_fused = 2,
700 align_branch_jmp = 3,
701 align_branch_call = 4,
702 align_branch_indirect = 5,
703 align_branch_ret = 6
704 };
705
706/* Type bits of branches to align. */
707enum align_branch_bit
708 {
709 align_branch_jcc_bit = 1 << align_branch_jcc,
710 align_branch_fused_bit = 1 << align_branch_fused,
711 align_branch_jmp_bit = 1 << align_branch_jmp,
712 align_branch_call_bit = 1 << align_branch_call,
713 align_branch_indirect_bit = 1 << align_branch_indirect,
714 align_branch_ret_bit = 1 << align_branch_ret
715 };
716
717static unsigned int align_branch = (align_branch_jcc_bit
718 | align_branch_fused_bit
719 | align_branch_jmp_bit);
720
79d72f45
HL
721/* Types of condition jump used by macro-fusion. */
722enum mf_jcc_kind
723 {
724 mf_jcc_jo = 0, /* base opcode 0x70 */
725 mf_jcc_jc, /* base opcode 0x72 */
726 mf_jcc_je, /* base opcode 0x74 */
727 mf_jcc_jna, /* base opcode 0x76 */
728 mf_jcc_js, /* base opcode 0x78 */
729 mf_jcc_jp, /* base opcode 0x7a */
730 mf_jcc_jl, /* base opcode 0x7c */
731 mf_jcc_jle, /* base opcode 0x7e */
732 };
733
734/* Types of compare flag-modifying insntructions used by macro-fusion. */
735enum mf_cmp_kind
736 {
737 mf_cmp_test_and, /* test/cmp */
738 mf_cmp_alu_cmp, /* add/sub/cmp */
739 mf_cmp_incdec /* inc/dec */
740 };
741
e379e5f3
L
742/* The maximum padding size for fused jcc. CMP like instruction can
743 be 9 bytes and jcc can be 6 bytes. Leave room just in case for
744 prefixes. */
745#define MAX_FUSED_JCC_PADDING_SIZE 20
746
747/* The maximum number of prefixes added for an instruction. */
748static unsigned int align_branch_prefix_size = 5;
749
b6f8c7c4
L
750/* Optimization:
751 1. Clear the REX_W bit with register operand if possible.
752 2. Above plus use 128bit vector instruction to clear the full vector
753 register.
754 */
755static int optimize = 0;
756
757/* Optimization:
758 1. Clear the REX_W bit with register operand if possible.
759 2. Above plus use 128bit vector instruction to clear the full vector
760 register.
761 3. Above plus optimize "test{q,l,w} $imm8,%r{64,32,16}" to
762 "testb $imm7,%r8".
763 */
764static int optimize_for_space = 0;
765
2ca3ace5
L
766/* Register prefix used for error message. */
767static const char *register_prefix = "%";
768
47926f60
KH
769/* Used in 16 bit gcc mode to add an l suffix to call, ret, enter,
770 leave, push, and pop instructions so that gcc has the same stack
771 frame as in 32 bit mode. */
772static char stackop_size = '\0';
eecb386c 773
12b55ccc
L
774/* Non-zero to optimize code alignment. */
775int optimize_align_code = 1;
776
47926f60
KH
777/* Non-zero to quieten some warnings. */
778static int quiet_warnings = 0;
a38cf1db 779
47926f60
KH
780/* CPU name. */
781static const char *cpu_arch_name = NULL;
6305a203 782static char *cpu_sub_arch_name = NULL;
a38cf1db 783
47926f60 784/* CPU feature flags. */
40fb9820
L
785static i386_cpu_flags cpu_arch_flags = CPU_UNKNOWN_FLAGS;
786
ccc9c027
L
787/* If we have selected a cpu we are generating instructions for. */
788static int cpu_arch_tune_set = 0;
789
9103f4f4 790/* Cpu we are generating instructions for. */
fbf3f584 791enum processor_type cpu_arch_tune = PROCESSOR_UNKNOWN;
9103f4f4
L
792
793/* CPU feature flags of cpu we are generating instructions for. */
40fb9820 794static i386_cpu_flags cpu_arch_tune_flags;
9103f4f4 795
ccc9c027 796/* CPU instruction set architecture used. */
fbf3f584 797enum processor_type cpu_arch_isa = PROCESSOR_UNKNOWN;
ccc9c027 798
9103f4f4 799/* CPU feature flags of instruction set architecture used. */
fbf3f584 800i386_cpu_flags cpu_arch_isa_flags;
9103f4f4 801
fddf5b5b
AM
802/* If set, conditional jumps are not automatically promoted to handle
803 larger than a byte offset. */
804static unsigned int no_cond_jump_promotion = 0;
805
c0f3af97
L
806/* Encode SSE instructions with VEX prefix. */
807static unsigned int sse2avx;
808
539f890d
L
809/* Encode scalar AVX instructions with specific vector length. */
810static enum
811 {
812 vex128 = 0,
813 vex256
814 } avxscalar;
815
03751133
L
816/* Encode VEX WIG instructions with specific vex.w. */
817static enum
818 {
819 vexw0 = 0,
820 vexw1
821 } vexwig;
822
43234a1e
L
823/* Encode scalar EVEX LIG instructions with specific vector length. */
824static enum
825 {
826 evexl128 = 0,
827 evexl256,
828 evexl512
829 } evexlig;
830
831/* Encode EVEX WIG instructions with specific evex.w. */
832static enum
833 {
834 evexw0 = 0,
835 evexw1
836 } evexwig;
837
d3d3c6db
IT
838/* Value to encode in EVEX RC bits, for SAE-only instructions. */
839static enum rc_type evexrcig = rne;
840
29b0f896 841/* Pre-defined "_GLOBAL_OFFSET_TABLE_". */
87c245cc 842static symbolS *GOT_symbol;
29b0f896 843
a4447b93
RH
844/* The dwarf2 return column, adjusted for 32 or 64 bit. */
845unsigned int x86_dwarf2_return_column;
846
847/* The dwarf2 data alignment, adjusted for 32 or 64 bit. */
848int x86_cie_data_alignment;
849
252b5132 850/* Interface to relax_segment.
fddf5b5b
AM
851 There are 3 major relax states for 386 jump insns because the
852 different types of jumps add different sizes to frags when we're
e379e5f3
L
853 figuring out what sort of jump to choose to reach a given label.
854
855 BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING are used to align
856 branches which are handled by md_estimate_size_before_relax() and
857 i386_generic_table_relax_frag(). */
252b5132 858
47926f60 859/* Types. */
93c2a809
AM
860#define UNCOND_JUMP 0
861#define COND_JUMP 1
862#define COND_JUMP86 2
e379e5f3
L
863#define BRANCH_PADDING 3
864#define BRANCH_PREFIX 4
865#define FUSED_JCC_PADDING 5
fddf5b5b 866
47926f60 867/* Sizes. */
252b5132
RH
868#define CODE16 1
869#define SMALL 0
29b0f896 870#define SMALL16 (SMALL | CODE16)
252b5132 871#define BIG 2
29b0f896 872#define BIG16 (BIG | CODE16)
252b5132
RH
873
874#ifndef INLINE
875#ifdef __GNUC__
876#define INLINE __inline__
877#else
878#define INLINE
879#endif
880#endif
881
fddf5b5b
AM
882#define ENCODE_RELAX_STATE(type, size) \
883 ((relax_substateT) (((type) << 2) | (size)))
884#define TYPE_FROM_RELAX_STATE(s) \
885 ((s) >> 2)
886#define DISP_SIZE_FROM_RELAX_STATE(s) \
887 ((((s) & 3) == BIG ? 4 : (((s) & 3) == BIG16 ? 2 : 1)))
252b5132
RH
888
889/* This table is used by relax_frag to promote short jumps to long
890 ones where necessary. SMALL (short) jumps may be promoted to BIG
891 (32 bit long) ones, and SMALL16 jumps to BIG16 (16 bit long). We
892 don't allow a short jump in a 32 bit code segment to be promoted to
893 a 16 bit offset jump because it's slower (requires data size
894 prefix), and doesn't work, unless the destination is in the bottom
895 64k of the code segment (The top 16 bits of eip are zeroed). */
896
897const relax_typeS md_relax_table[] =
898{
24eab124
AM
899 /* The fields are:
900 1) most positive reach of this state,
901 2) most negative reach of this state,
93c2a809 902 3) how many bytes this mode will have in the variable part of the frag
ce8a8b2f 903 4) which index into the table to try if we can't fit into this one. */
252b5132 904
fddf5b5b 905 /* UNCOND_JUMP states. */
93c2a809
AM
906 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG)},
907 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16)},
908 /* dword jmp adds 4 bytes to frag:
909 0 extra opcode bytes, 4 displacement bytes. */
252b5132 910 {0, 0, 4, 0},
93c2a809
AM
911 /* word jmp adds 2 byte2 to frag:
912 0 extra opcode bytes, 2 displacement bytes. */
252b5132
RH
913 {0, 0, 2, 0},
914
93c2a809
AM
915 /* COND_JUMP states. */
916 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG)},
917 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG16)},
918 /* dword conditionals adds 5 bytes to frag:
919 1 extra opcode byte, 4 displacement bytes. */
920 {0, 0, 5, 0},
fddf5b5b 921 /* word conditionals add 3 bytes to frag:
93c2a809
AM
922 1 extra opcode byte, 2 displacement bytes. */
923 {0, 0, 3, 0},
924
925 /* COND_JUMP86 states. */
926 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG)},
927 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG16)},
928 /* dword conditionals adds 5 bytes to frag:
929 1 extra opcode byte, 4 displacement bytes. */
930 {0, 0, 5, 0},
931 /* word conditionals add 4 bytes to frag:
932 1 displacement byte and a 3 byte long branch insn. */
933 {0, 0, 4, 0}
252b5132
RH
934};
935
9103f4f4
L
936static const arch_entry cpu_arch[] =
937{
89507696
JB
938 /* Do not replace the first two entries - i386_target_format()
939 relies on them being there in this order. */
8a2c8fef 940 { STRING_COMMA_LEN ("generic32"), PROCESSOR_GENERIC32,
293f5f65 941 CPU_GENERIC32_FLAGS, 0 },
8a2c8fef 942 { STRING_COMMA_LEN ("generic64"), PROCESSOR_GENERIC64,
293f5f65 943 CPU_GENERIC64_FLAGS, 0 },
8a2c8fef 944 { STRING_COMMA_LEN ("i8086"), PROCESSOR_UNKNOWN,
293f5f65 945 CPU_NONE_FLAGS, 0 },
8a2c8fef 946 { STRING_COMMA_LEN ("i186"), PROCESSOR_UNKNOWN,
293f5f65 947 CPU_I186_FLAGS, 0 },
8a2c8fef 948 { STRING_COMMA_LEN ("i286"), PROCESSOR_UNKNOWN,
293f5f65 949 CPU_I286_FLAGS, 0 },
8a2c8fef 950 { STRING_COMMA_LEN ("i386"), PROCESSOR_I386,
293f5f65 951 CPU_I386_FLAGS, 0 },
8a2c8fef 952 { STRING_COMMA_LEN ("i486"), PROCESSOR_I486,
293f5f65 953 CPU_I486_FLAGS, 0 },
8a2c8fef 954 { STRING_COMMA_LEN ("i586"), PROCESSOR_PENTIUM,
293f5f65 955 CPU_I586_FLAGS, 0 },
8a2c8fef 956 { STRING_COMMA_LEN ("i686"), PROCESSOR_PENTIUMPRO,
293f5f65 957 CPU_I686_FLAGS, 0 },
8a2c8fef 958 { STRING_COMMA_LEN ("pentium"), PROCESSOR_PENTIUM,
293f5f65 959 CPU_I586_FLAGS, 0 },
8a2c8fef 960 { STRING_COMMA_LEN ("pentiumpro"), PROCESSOR_PENTIUMPRO,
293f5f65 961 CPU_PENTIUMPRO_FLAGS, 0 },
8a2c8fef 962 { STRING_COMMA_LEN ("pentiumii"), PROCESSOR_PENTIUMPRO,
293f5f65 963 CPU_P2_FLAGS, 0 },
8a2c8fef 964 { STRING_COMMA_LEN ("pentiumiii"),PROCESSOR_PENTIUMPRO,
293f5f65 965 CPU_P3_FLAGS, 0 },
8a2c8fef 966 { STRING_COMMA_LEN ("pentium4"), PROCESSOR_PENTIUM4,
293f5f65 967 CPU_P4_FLAGS, 0 },
8a2c8fef 968 { STRING_COMMA_LEN ("prescott"), PROCESSOR_NOCONA,
293f5f65 969 CPU_CORE_FLAGS, 0 },
8a2c8fef 970 { STRING_COMMA_LEN ("nocona"), PROCESSOR_NOCONA,
293f5f65 971 CPU_NOCONA_FLAGS, 0 },
8a2c8fef 972 { STRING_COMMA_LEN ("yonah"), PROCESSOR_CORE,
293f5f65 973 CPU_CORE_FLAGS, 1 },
8a2c8fef 974 { STRING_COMMA_LEN ("core"), PROCESSOR_CORE,
293f5f65 975 CPU_CORE_FLAGS, 0 },
8a2c8fef 976 { STRING_COMMA_LEN ("merom"), PROCESSOR_CORE2,
293f5f65 977 CPU_CORE2_FLAGS, 1 },
8a2c8fef 978 { STRING_COMMA_LEN ("core2"), PROCESSOR_CORE2,
293f5f65 979 CPU_CORE2_FLAGS, 0 },
8a2c8fef 980 { STRING_COMMA_LEN ("corei7"), PROCESSOR_COREI7,
293f5f65 981 CPU_COREI7_FLAGS, 0 },
8a2c8fef 982 { STRING_COMMA_LEN ("l1om"), PROCESSOR_L1OM,
293f5f65 983 CPU_L1OM_FLAGS, 0 },
7a9068fe 984 { STRING_COMMA_LEN ("k1om"), PROCESSOR_K1OM,
293f5f65 985 CPU_K1OM_FLAGS, 0 },
81486035 986 { STRING_COMMA_LEN ("iamcu"), PROCESSOR_IAMCU,
293f5f65 987 CPU_IAMCU_FLAGS, 0 },
8a2c8fef 988 { STRING_COMMA_LEN ("k6"), PROCESSOR_K6,
293f5f65 989 CPU_K6_FLAGS, 0 },
8a2c8fef 990 { STRING_COMMA_LEN ("k6_2"), PROCESSOR_K6,
293f5f65 991 CPU_K6_2_FLAGS, 0 },
8a2c8fef 992 { STRING_COMMA_LEN ("athlon"), PROCESSOR_ATHLON,
293f5f65 993 CPU_ATHLON_FLAGS, 0 },
8a2c8fef 994 { STRING_COMMA_LEN ("sledgehammer"), PROCESSOR_K8,
293f5f65 995 CPU_K8_FLAGS, 1 },
8a2c8fef 996 { STRING_COMMA_LEN ("opteron"), PROCESSOR_K8,
293f5f65 997 CPU_K8_FLAGS, 0 },
8a2c8fef 998 { STRING_COMMA_LEN ("k8"), PROCESSOR_K8,
293f5f65 999 CPU_K8_FLAGS, 0 },
8a2c8fef 1000 { STRING_COMMA_LEN ("amdfam10"), PROCESSOR_AMDFAM10,
293f5f65 1001 CPU_AMDFAM10_FLAGS, 0 },
8aedb9fe 1002 { STRING_COMMA_LEN ("bdver1"), PROCESSOR_BD,
293f5f65 1003 CPU_BDVER1_FLAGS, 0 },
8aedb9fe 1004 { STRING_COMMA_LEN ("bdver2"), PROCESSOR_BD,
293f5f65 1005 CPU_BDVER2_FLAGS, 0 },
5e5c50d3 1006 { STRING_COMMA_LEN ("bdver3"), PROCESSOR_BD,
293f5f65 1007 CPU_BDVER3_FLAGS, 0 },
c7b0bd56 1008 { STRING_COMMA_LEN ("bdver4"), PROCESSOR_BD,
293f5f65 1009 CPU_BDVER4_FLAGS, 0 },
029f3522 1010 { STRING_COMMA_LEN ("znver1"), PROCESSOR_ZNVER,
293f5f65 1011 CPU_ZNVER1_FLAGS, 0 },
a9660a6f
AP
1012 { STRING_COMMA_LEN ("znver2"), PROCESSOR_ZNVER,
1013 CPU_ZNVER2_FLAGS, 0 },
646cc3e0
GG
1014 { STRING_COMMA_LEN ("znver3"), PROCESSOR_ZNVER,
1015 CPU_ZNVER3_FLAGS, 0 },
7b458c12 1016 { STRING_COMMA_LEN ("btver1"), PROCESSOR_BT,
293f5f65 1017 CPU_BTVER1_FLAGS, 0 },
7b458c12 1018 { STRING_COMMA_LEN ("btver2"), PROCESSOR_BT,
293f5f65 1019 CPU_BTVER2_FLAGS, 0 },
8a2c8fef 1020 { STRING_COMMA_LEN (".8087"), PROCESSOR_UNKNOWN,
293f5f65 1021 CPU_8087_FLAGS, 0 },
8a2c8fef 1022 { STRING_COMMA_LEN (".287"), PROCESSOR_UNKNOWN,
293f5f65 1023 CPU_287_FLAGS, 0 },
8a2c8fef 1024 { STRING_COMMA_LEN (".387"), PROCESSOR_UNKNOWN,
293f5f65 1025 CPU_387_FLAGS, 0 },
1848e567
L
1026 { STRING_COMMA_LEN (".687"), PROCESSOR_UNKNOWN,
1027 CPU_687_FLAGS, 0 },
d871f3f4
L
1028 { STRING_COMMA_LEN (".cmov"), PROCESSOR_UNKNOWN,
1029 CPU_CMOV_FLAGS, 0 },
1030 { STRING_COMMA_LEN (".fxsr"), PROCESSOR_UNKNOWN,
1031 CPU_FXSR_FLAGS, 0 },
8a2c8fef 1032 { STRING_COMMA_LEN (".mmx"), PROCESSOR_UNKNOWN,
293f5f65 1033 CPU_MMX_FLAGS, 0 },
8a2c8fef 1034 { STRING_COMMA_LEN (".sse"), PROCESSOR_UNKNOWN,
293f5f65 1035 CPU_SSE_FLAGS, 0 },
8a2c8fef 1036 { STRING_COMMA_LEN (".sse2"), PROCESSOR_UNKNOWN,
293f5f65 1037 CPU_SSE2_FLAGS, 0 },
8a2c8fef 1038 { STRING_COMMA_LEN (".sse3"), PROCESSOR_UNKNOWN,
293f5f65 1039 CPU_SSE3_FLAGS, 0 },
af5c13b0
L
1040 { STRING_COMMA_LEN (".sse4a"), PROCESSOR_UNKNOWN,
1041 CPU_SSE4A_FLAGS, 0 },
8a2c8fef 1042 { STRING_COMMA_LEN (".ssse3"), PROCESSOR_UNKNOWN,
293f5f65 1043 CPU_SSSE3_FLAGS, 0 },
8a2c8fef 1044 { STRING_COMMA_LEN (".sse4.1"), PROCESSOR_UNKNOWN,
293f5f65 1045 CPU_SSE4_1_FLAGS, 0 },
8a2c8fef 1046 { STRING_COMMA_LEN (".sse4.2"), PROCESSOR_UNKNOWN,
293f5f65 1047 CPU_SSE4_2_FLAGS, 0 },
8a2c8fef 1048 { STRING_COMMA_LEN (".sse4"), PROCESSOR_UNKNOWN,
293f5f65 1049 CPU_SSE4_2_FLAGS, 0 },
8a2c8fef 1050 { STRING_COMMA_LEN (".avx"), PROCESSOR_UNKNOWN,
293f5f65 1051 CPU_AVX_FLAGS, 0 },
6c30d220 1052 { STRING_COMMA_LEN (".avx2"), PROCESSOR_UNKNOWN,
293f5f65 1053 CPU_AVX2_FLAGS, 0 },
43234a1e 1054 { STRING_COMMA_LEN (".avx512f"), PROCESSOR_UNKNOWN,
293f5f65 1055 CPU_AVX512F_FLAGS, 0 },
43234a1e 1056 { STRING_COMMA_LEN (".avx512cd"), PROCESSOR_UNKNOWN,
293f5f65 1057 CPU_AVX512CD_FLAGS, 0 },
43234a1e 1058 { STRING_COMMA_LEN (".avx512er"), PROCESSOR_UNKNOWN,
293f5f65 1059 CPU_AVX512ER_FLAGS, 0 },
43234a1e 1060 { STRING_COMMA_LEN (".avx512pf"), PROCESSOR_UNKNOWN,
293f5f65 1061 CPU_AVX512PF_FLAGS, 0 },
1dfc6506 1062 { STRING_COMMA_LEN (".avx512dq"), PROCESSOR_UNKNOWN,
293f5f65 1063 CPU_AVX512DQ_FLAGS, 0 },
1dfc6506 1064 { STRING_COMMA_LEN (".avx512bw"), PROCESSOR_UNKNOWN,
293f5f65 1065 CPU_AVX512BW_FLAGS, 0 },
1dfc6506 1066 { STRING_COMMA_LEN (".avx512vl"), PROCESSOR_UNKNOWN,
293f5f65 1067 CPU_AVX512VL_FLAGS, 0 },
8a2c8fef 1068 { STRING_COMMA_LEN (".vmx"), PROCESSOR_UNKNOWN,
293f5f65 1069 CPU_VMX_FLAGS, 0 },
8729a6f6 1070 { STRING_COMMA_LEN (".vmfunc"), PROCESSOR_UNKNOWN,
293f5f65 1071 CPU_VMFUNC_FLAGS, 0 },
8a2c8fef 1072 { STRING_COMMA_LEN (".smx"), PROCESSOR_UNKNOWN,
293f5f65 1073 CPU_SMX_FLAGS, 0 },
8a2c8fef 1074 { STRING_COMMA_LEN (".xsave"), PROCESSOR_UNKNOWN,
293f5f65 1075 CPU_XSAVE_FLAGS, 0 },
c7b8aa3a 1076 { STRING_COMMA_LEN (".xsaveopt"), PROCESSOR_UNKNOWN,
293f5f65 1077 CPU_XSAVEOPT_FLAGS, 0 },
1dfc6506 1078 { STRING_COMMA_LEN (".xsavec"), PROCESSOR_UNKNOWN,
293f5f65 1079 CPU_XSAVEC_FLAGS, 0 },
1dfc6506 1080 { STRING_COMMA_LEN (".xsaves"), PROCESSOR_UNKNOWN,
293f5f65 1081 CPU_XSAVES_FLAGS, 0 },
8a2c8fef 1082 { STRING_COMMA_LEN (".aes"), PROCESSOR_UNKNOWN,
293f5f65 1083 CPU_AES_FLAGS, 0 },
8a2c8fef 1084 { STRING_COMMA_LEN (".pclmul"), PROCESSOR_UNKNOWN,
293f5f65 1085 CPU_PCLMUL_FLAGS, 0 },
8a2c8fef 1086 { STRING_COMMA_LEN (".clmul"), PROCESSOR_UNKNOWN,
293f5f65 1087 CPU_PCLMUL_FLAGS, 1 },
c7b8aa3a 1088 { STRING_COMMA_LEN (".fsgsbase"), PROCESSOR_UNKNOWN,
293f5f65 1089 CPU_FSGSBASE_FLAGS, 0 },
c7b8aa3a 1090 { STRING_COMMA_LEN (".rdrnd"), PROCESSOR_UNKNOWN,
293f5f65 1091 CPU_RDRND_FLAGS, 0 },
c7b8aa3a 1092 { STRING_COMMA_LEN (".f16c"), PROCESSOR_UNKNOWN,
293f5f65 1093 CPU_F16C_FLAGS, 0 },
6c30d220 1094 { STRING_COMMA_LEN (".bmi2"), PROCESSOR_UNKNOWN,
293f5f65 1095 CPU_BMI2_FLAGS, 0 },
8a2c8fef 1096 { STRING_COMMA_LEN (".fma"), PROCESSOR_UNKNOWN,
293f5f65 1097 CPU_FMA_FLAGS, 0 },
8a2c8fef 1098 { STRING_COMMA_LEN (".fma4"), PROCESSOR_UNKNOWN,
293f5f65 1099 CPU_FMA4_FLAGS, 0 },
8a2c8fef 1100 { STRING_COMMA_LEN (".xop"), PROCESSOR_UNKNOWN,
293f5f65 1101 CPU_XOP_FLAGS, 0 },
8a2c8fef 1102 { STRING_COMMA_LEN (".lwp"), PROCESSOR_UNKNOWN,
293f5f65 1103 CPU_LWP_FLAGS, 0 },
8a2c8fef 1104 { STRING_COMMA_LEN (".movbe"), PROCESSOR_UNKNOWN,
293f5f65 1105 CPU_MOVBE_FLAGS, 0 },
60aa667e 1106 { STRING_COMMA_LEN (".cx16"), PROCESSOR_UNKNOWN,
293f5f65 1107 CPU_CX16_FLAGS, 0 },
8a2c8fef 1108 { STRING_COMMA_LEN (".ept"), PROCESSOR_UNKNOWN,
293f5f65 1109 CPU_EPT_FLAGS, 0 },
6c30d220 1110 { STRING_COMMA_LEN (".lzcnt"), PROCESSOR_UNKNOWN,
293f5f65 1111 CPU_LZCNT_FLAGS, 0 },
272a84b1
L
1112 { STRING_COMMA_LEN (".popcnt"), PROCESSOR_UNKNOWN,
1113 CPU_POPCNT_FLAGS, 0 },
42164a71 1114 { STRING_COMMA_LEN (".hle"), PROCESSOR_UNKNOWN,
293f5f65 1115 CPU_HLE_FLAGS, 0 },
42164a71 1116 { STRING_COMMA_LEN (".rtm"), PROCESSOR_UNKNOWN,
293f5f65 1117 CPU_RTM_FLAGS, 0 },
6c30d220 1118 { STRING_COMMA_LEN (".invpcid"), PROCESSOR_UNKNOWN,
293f5f65 1119 CPU_INVPCID_FLAGS, 0 },
8a2c8fef 1120 { STRING_COMMA_LEN (".clflush"), PROCESSOR_UNKNOWN,
293f5f65 1121 CPU_CLFLUSH_FLAGS, 0 },
22109423 1122 { STRING_COMMA_LEN (".nop"), PROCESSOR_UNKNOWN,
293f5f65 1123 CPU_NOP_FLAGS, 0 },
8a2c8fef 1124 { STRING_COMMA_LEN (".syscall"), PROCESSOR_UNKNOWN,
293f5f65 1125 CPU_SYSCALL_FLAGS, 0 },
8a2c8fef 1126 { STRING_COMMA_LEN (".rdtscp"), PROCESSOR_UNKNOWN,
293f5f65 1127 CPU_RDTSCP_FLAGS, 0 },
8a2c8fef 1128 { STRING_COMMA_LEN (".3dnow"), PROCESSOR_UNKNOWN,
293f5f65 1129 CPU_3DNOW_FLAGS, 0 },
8a2c8fef 1130 { STRING_COMMA_LEN (".3dnowa"), PROCESSOR_UNKNOWN,
293f5f65 1131 CPU_3DNOWA_FLAGS, 0 },
8a2c8fef 1132 { STRING_COMMA_LEN (".padlock"), PROCESSOR_UNKNOWN,
293f5f65 1133 CPU_PADLOCK_FLAGS, 0 },
8a2c8fef 1134 { STRING_COMMA_LEN (".pacifica"), PROCESSOR_UNKNOWN,
293f5f65 1135 CPU_SVME_FLAGS, 1 },
8a2c8fef 1136 { STRING_COMMA_LEN (".svme"), PROCESSOR_UNKNOWN,
293f5f65 1137 CPU_SVME_FLAGS, 0 },
8a2c8fef 1138 { STRING_COMMA_LEN (".sse4a"), PROCESSOR_UNKNOWN,
293f5f65 1139 CPU_SSE4A_FLAGS, 0 },
8a2c8fef 1140 { STRING_COMMA_LEN (".abm"), PROCESSOR_UNKNOWN,
293f5f65 1141 CPU_ABM_FLAGS, 0 },
87973e9f 1142 { STRING_COMMA_LEN (".bmi"), PROCESSOR_UNKNOWN,
293f5f65 1143 CPU_BMI_FLAGS, 0 },
2a2a0f38 1144 { STRING_COMMA_LEN (".tbm"), PROCESSOR_UNKNOWN,
293f5f65 1145 CPU_TBM_FLAGS, 0 },
e2e1fcde 1146 { STRING_COMMA_LEN (".adx"), PROCESSOR_UNKNOWN,
293f5f65 1147 CPU_ADX_FLAGS, 0 },
e2e1fcde 1148 { STRING_COMMA_LEN (".rdseed"), PROCESSOR_UNKNOWN,
293f5f65 1149 CPU_RDSEED_FLAGS, 0 },
e2e1fcde 1150 { STRING_COMMA_LEN (".prfchw"), PROCESSOR_UNKNOWN,
293f5f65 1151 CPU_PRFCHW_FLAGS, 0 },
5c111e37 1152 { STRING_COMMA_LEN (".smap"), PROCESSOR_UNKNOWN,
293f5f65 1153 CPU_SMAP_FLAGS, 0 },
7e8b059b 1154 { STRING_COMMA_LEN (".mpx"), PROCESSOR_UNKNOWN,
293f5f65 1155 CPU_MPX_FLAGS, 0 },
a0046408 1156 { STRING_COMMA_LEN (".sha"), PROCESSOR_UNKNOWN,
293f5f65 1157 CPU_SHA_FLAGS, 0 },
963f3586 1158 { STRING_COMMA_LEN (".clflushopt"), PROCESSOR_UNKNOWN,
293f5f65 1159 CPU_CLFLUSHOPT_FLAGS, 0 },
dcf893b5 1160 { STRING_COMMA_LEN (".prefetchwt1"), PROCESSOR_UNKNOWN,
293f5f65 1161 CPU_PREFETCHWT1_FLAGS, 0 },
2cf200a4 1162 { STRING_COMMA_LEN (".se1"), PROCESSOR_UNKNOWN,
293f5f65 1163 CPU_SE1_FLAGS, 0 },
c5e7287a 1164 { STRING_COMMA_LEN (".clwb"), PROCESSOR_UNKNOWN,
293f5f65 1165 CPU_CLWB_FLAGS, 0 },
2cc1b5aa 1166 { STRING_COMMA_LEN (".avx512ifma"), PROCESSOR_UNKNOWN,
293f5f65 1167 CPU_AVX512IFMA_FLAGS, 0 },
14f195c9 1168 { STRING_COMMA_LEN (".avx512vbmi"), PROCESSOR_UNKNOWN,
293f5f65 1169 CPU_AVX512VBMI_FLAGS, 0 },
920d2ddc
IT
1170 { STRING_COMMA_LEN (".avx512_4fmaps"), PROCESSOR_UNKNOWN,
1171 CPU_AVX512_4FMAPS_FLAGS, 0 },
47acf0bd
IT
1172 { STRING_COMMA_LEN (".avx512_4vnniw"), PROCESSOR_UNKNOWN,
1173 CPU_AVX512_4VNNIW_FLAGS, 0 },
620214f7
IT
1174 { STRING_COMMA_LEN (".avx512_vpopcntdq"), PROCESSOR_UNKNOWN,
1175 CPU_AVX512_VPOPCNTDQ_FLAGS, 0 },
53467f57
IT
1176 { STRING_COMMA_LEN (".avx512_vbmi2"), PROCESSOR_UNKNOWN,
1177 CPU_AVX512_VBMI2_FLAGS, 0 },
8cfcb765
IT
1178 { STRING_COMMA_LEN (".avx512_vnni"), PROCESSOR_UNKNOWN,
1179 CPU_AVX512_VNNI_FLAGS, 0 },
ee6872be
IT
1180 { STRING_COMMA_LEN (".avx512_bitalg"), PROCESSOR_UNKNOWN,
1181 CPU_AVX512_BITALG_FLAGS, 0 },
58bf9b6a
L
1182 { STRING_COMMA_LEN (".avx_vnni"), PROCESSOR_UNKNOWN,
1183 CPU_AVX_VNNI_FLAGS, 0 },
029f3522 1184 { STRING_COMMA_LEN (".clzero"), PROCESSOR_UNKNOWN,
293f5f65 1185 CPU_CLZERO_FLAGS, 0 },
9916071f 1186 { STRING_COMMA_LEN (".mwaitx"), PROCESSOR_UNKNOWN,
293f5f65 1187 CPU_MWAITX_FLAGS, 0 },
8eab4136 1188 { STRING_COMMA_LEN (".ospke"), PROCESSOR_UNKNOWN,
293f5f65 1189 CPU_OSPKE_FLAGS, 0 },
8bc52696 1190 { STRING_COMMA_LEN (".rdpid"), PROCESSOR_UNKNOWN,
293f5f65 1191 CPU_RDPID_FLAGS, 0 },
6b40c462
L
1192 { STRING_COMMA_LEN (".ptwrite"), PROCESSOR_UNKNOWN,
1193 CPU_PTWRITE_FLAGS, 0 },
d777820b
IT
1194 { STRING_COMMA_LEN (".ibt"), PROCESSOR_UNKNOWN,
1195 CPU_IBT_FLAGS, 0 },
1196 { STRING_COMMA_LEN (".shstk"), PROCESSOR_UNKNOWN,
1197 CPU_SHSTK_FLAGS, 0 },
48521003
IT
1198 { STRING_COMMA_LEN (".gfni"), PROCESSOR_UNKNOWN,
1199 CPU_GFNI_FLAGS, 0 },
8dcf1fad
IT
1200 { STRING_COMMA_LEN (".vaes"), PROCESSOR_UNKNOWN,
1201 CPU_VAES_FLAGS, 0 },
ff1982d5
IT
1202 { STRING_COMMA_LEN (".vpclmulqdq"), PROCESSOR_UNKNOWN,
1203 CPU_VPCLMULQDQ_FLAGS, 0 },
3233d7d0
IT
1204 { STRING_COMMA_LEN (".wbnoinvd"), PROCESSOR_UNKNOWN,
1205 CPU_WBNOINVD_FLAGS, 0 },
be3a8dca
IT
1206 { STRING_COMMA_LEN (".pconfig"), PROCESSOR_UNKNOWN,
1207 CPU_PCONFIG_FLAGS, 0 },
de89d0a3
IT
1208 { STRING_COMMA_LEN (".waitpkg"), PROCESSOR_UNKNOWN,
1209 CPU_WAITPKG_FLAGS, 0 },
c48935d7
IT
1210 { STRING_COMMA_LEN (".cldemote"), PROCESSOR_UNKNOWN,
1211 CPU_CLDEMOTE_FLAGS, 0 },
260cd341
LC
1212 { STRING_COMMA_LEN (".amx_int8"), PROCESSOR_UNKNOWN,
1213 CPU_AMX_INT8_FLAGS, 0 },
1214 { STRING_COMMA_LEN (".amx_bf16"), PROCESSOR_UNKNOWN,
1215 CPU_AMX_BF16_FLAGS, 0 },
1216 { STRING_COMMA_LEN (".amx_tile"), PROCESSOR_UNKNOWN,
1217 CPU_AMX_TILE_FLAGS, 0 },
c0a30a9f
L
1218 { STRING_COMMA_LEN (".movdiri"), PROCESSOR_UNKNOWN,
1219 CPU_MOVDIRI_FLAGS, 0 },
1220 { STRING_COMMA_LEN (".movdir64b"), PROCESSOR_UNKNOWN,
1221 CPU_MOVDIR64B_FLAGS, 0 },
d6aab7a1
XG
1222 { STRING_COMMA_LEN (".avx512_bf16"), PROCESSOR_UNKNOWN,
1223 CPU_AVX512_BF16_FLAGS, 0 },
9186c494
L
1224 { STRING_COMMA_LEN (".avx512_vp2intersect"), PROCESSOR_UNKNOWN,
1225 CPU_AVX512_VP2INTERSECT_FLAGS, 0 },
81d54bb7
CL
1226 { STRING_COMMA_LEN (".tdx"), PROCESSOR_UNKNOWN,
1227 CPU_TDX_FLAGS, 0 },
dd455cf5
L
1228 { STRING_COMMA_LEN (".enqcmd"), PROCESSOR_UNKNOWN,
1229 CPU_ENQCMD_FLAGS, 0 },
4b27d27c
L
1230 { STRING_COMMA_LEN (".serialize"), PROCESSOR_UNKNOWN,
1231 CPU_SERIALIZE_FLAGS, 0 },
142861df
JB
1232 { STRING_COMMA_LEN (".rdpru"), PROCESSOR_UNKNOWN,
1233 CPU_RDPRU_FLAGS, 0 },
1234 { STRING_COMMA_LEN (".mcommit"), PROCESSOR_UNKNOWN,
1235 CPU_MCOMMIT_FLAGS, 0 },
a847e322
JB
1236 { STRING_COMMA_LEN (".sev_es"), PROCESSOR_UNKNOWN,
1237 CPU_SEV_ES_FLAGS, 0 },
bb651e8b
CL
1238 { STRING_COMMA_LEN (".tsxldtrk"), PROCESSOR_UNKNOWN,
1239 CPU_TSXLDTRK_FLAGS, 0 },
c4694f17
TG
1240 { STRING_COMMA_LEN (".kl"), PROCESSOR_UNKNOWN,
1241 CPU_KL_FLAGS, 0 },
1242 { STRING_COMMA_LEN (".widekl"), PROCESSOR_UNKNOWN,
1243 CPU_WIDEKL_FLAGS, 0 },
f64c42a9
LC
1244 { STRING_COMMA_LEN (".uintr"), PROCESSOR_UNKNOWN,
1245 CPU_UINTR_FLAGS, 0 },
c1fa250a
LC
1246 { STRING_COMMA_LEN (".hreset"), PROCESSOR_UNKNOWN,
1247 CPU_HRESET_FLAGS, 0 },
293f5f65
L
1248};
1249
1250static const noarch_entry cpu_noarch[] =
1251{
1252 { STRING_COMMA_LEN ("no87"), CPU_ANY_X87_FLAGS },
1848e567
L
1253 { STRING_COMMA_LEN ("no287"), CPU_ANY_287_FLAGS },
1254 { STRING_COMMA_LEN ("no387"), CPU_ANY_387_FLAGS },
1255 { STRING_COMMA_LEN ("no687"), CPU_ANY_687_FLAGS },
d871f3f4
L
1256 { STRING_COMMA_LEN ("nocmov"), CPU_ANY_CMOV_FLAGS },
1257 { STRING_COMMA_LEN ("nofxsr"), CPU_ANY_FXSR_FLAGS },
293f5f65
L
1258 { STRING_COMMA_LEN ("nommx"), CPU_ANY_MMX_FLAGS },
1259 { STRING_COMMA_LEN ("nosse"), CPU_ANY_SSE_FLAGS },
1848e567
L
1260 { STRING_COMMA_LEN ("nosse2"), CPU_ANY_SSE2_FLAGS },
1261 { STRING_COMMA_LEN ("nosse3"), CPU_ANY_SSE3_FLAGS },
af5c13b0 1262 { STRING_COMMA_LEN ("nosse4a"), CPU_ANY_SSE4A_FLAGS },
1848e567
L
1263 { STRING_COMMA_LEN ("nossse3"), CPU_ANY_SSSE3_FLAGS },
1264 { STRING_COMMA_LEN ("nosse4.1"), CPU_ANY_SSE4_1_FLAGS },
1265 { STRING_COMMA_LEN ("nosse4.2"), CPU_ANY_SSE4_2_FLAGS },
af5c13b0 1266 { STRING_COMMA_LEN ("nosse4"), CPU_ANY_SSE4_1_FLAGS },
293f5f65 1267 { STRING_COMMA_LEN ("noavx"), CPU_ANY_AVX_FLAGS },
1848e567 1268 { STRING_COMMA_LEN ("noavx2"), CPU_ANY_AVX2_FLAGS },
144b71e2
L
1269 { STRING_COMMA_LEN ("noavx512f"), CPU_ANY_AVX512F_FLAGS },
1270 { STRING_COMMA_LEN ("noavx512cd"), CPU_ANY_AVX512CD_FLAGS },
1271 { STRING_COMMA_LEN ("noavx512er"), CPU_ANY_AVX512ER_FLAGS },
1272 { STRING_COMMA_LEN ("noavx512pf"), CPU_ANY_AVX512PF_FLAGS },
1273 { STRING_COMMA_LEN ("noavx512dq"), CPU_ANY_AVX512DQ_FLAGS },
1274 { STRING_COMMA_LEN ("noavx512bw"), CPU_ANY_AVX512BW_FLAGS },
1275 { STRING_COMMA_LEN ("noavx512vl"), CPU_ANY_AVX512VL_FLAGS },
1276 { STRING_COMMA_LEN ("noavx512ifma"), CPU_ANY_AVX512IFMA_FLAGS },
1277 { STRING_COMMA_LEN ("noavx512vbmi"), CPU_ANY_AVX512VBMI_FLAGS },
920d2ddc 1278 { STRING_COMMA_LEN ("noavx512_4fmaps"), CPU_ANY_AVX512_4FMAPS_FLAGS },
47acf0bd 1279 { STRING_COMMA_LEN ("noavx512_4vnniw"), CPU_ANY_AVX512_4VNNIW_FLAGS },
620214f7 1280 { STRING_COMMA_LEN ("noavx512_vpopcntdq"), CPU_ANY_AVX512_VPOPCNTDQ_FLAGS },
53467f57 1281 { STRING_COMMA_LEN ("noavx512_vbmi2"), CPU_ANY_AVX512_VBMI2_FLAGS },
8cfcb765 1282 { STRING_COMMA_LEN ("noavx512_vnni"), CPU_ANY_AVX512_VNNI_FLAGS },
ee6872be 1283 { STRING_COMMA_LEN ("noavx512_bitalg"), CPU_ANY_AVX512_BITALG_FLAGS },
58bf9b6a 1284 { STRING_COMMA_LEN ("noavx_vnni"), CPU_ANY_AVX_VNNI_FLAGS },
d777820b
IT
1285 { STRING_COMMA_LEN ("noibt"), CPU_ANY_IBT_FLAGS },
1286 { STRING_COMMA_LEN ("noshstk"), CPU_ANY_SHSTK_FLAGS },
260cd341
LC
1287 { STRING_COMMA_LEN ("noamx_int8"), CPU_ANY_AMX_INT8_FLAGS },
1288 { STRING_COMMA_LEN ("noamx_bf16"), CPU_ANY_AMX_BF16_FLAGS },
1289 { STRING_COMMA_LEN ("noamx_tile"), CPU_ANY_AMX_TILE_FLAGS },
c0a30a9f
L
1290 { STRING_COMMA_LEN ("nomovdiri"), CPU_ANY_MOVDIRI_FLAGS },
1291 { STRING_COMMA_LEN ("nomovdir64b"), CPU_ANY_MOVDIR64B_FLAGS },
d6aab7a1 1292 { STRING_COMMA_LEN ("noavx512_bf16"), CPU_ANY_AVX512_BF16_FLAGS },
708a2fff
CL
1293 { STRING_COMMA_LEN ("noavx512_vp2intersect"),
1294 CPU_ANY_AVX512_VP2INTERSECT_FLAGS },
81d54bb7 1295 { STRING_COMMA_LEN ("notdx"), CPU_ANY_TDX_FLAGS },
dd455cf5 1296 { STRING_COMMA_LEN ("noenqcmd"), CPU_ANY_ENQCMD_FLAGS },
4b27d27c 1297 { STRING_COMMA_LEN ("noserialize"), CPU_ANY_SERIALIZE_FLAGS },
bb651e8b 1298 { STRING_COMMA_LEN ("notsxldtrk"), CPU_ANY_TSXLDTRK_FLAGS },
c4694f17
TG
1299 { STRING_COMMA_LEN ("nokl"), CPU_ANY_KL_FLAGS },
1300 { STRING_COMMA_LEN ("nowidekl"), CPU_ANY_WIDEKL_FLAGS },
f64c42a9 1301 { STRING_COMMA_LEN ("nouintr"), CPU_ANY_UINTR_FLAGS },
c1fa250a 1302 { STRING_COMMA_LEN ("nohreset"), CPU_ANY_HRESET_FLAGS },
e413e4e9
AM
1303};
1304
704209c0 1305#ifdef I386COFF
a6c24e68
NC
1306/* Like s_lcomm_internal in gas/read.c but the alignment string
1307 is allowed to be optional. */
1308
1309static symbolS *
1310pe_lcomm_internal (int needs_align, symbolS *symbolP, addressT size)
1311{
1312 addressT align = 0;
1313
1314 SKIP_WHITESPACE ();
1315
7ab9ffdd 1316 if (needs_align
a6c24e68
NC
1317 && *input_line_pointer == ',')
1318 {
1319 align = parse_align (needs_align - 1);
7ab9ffdd 1320
a6c24e68
NC
1321 if (align == (addressT) -1)
1322 return NULL;
1323 }
1324 else
1325 {
1326 if (size >= 8)
1327 align = 3;
1328 else if (size >= 4)
1329 align = 2;
1330 else if (size >= 2)
1331 align = 1;
1332 else
1333 align = 0;
1334 }
1335
1336 bss_alloc (symbolP, size, align);
1337 return symbolP;
1338}
1339
704209c0 1340static void
a6c24e68
NC
1341pe_lcomm (int needs_align)
1342{
1343 s_comm_internal (needs_align * 2, pe_lcomm_internal);
1344}
704209c0 1345#endif
a6c24e68 1346
29b0f896
AM
1347const pseudo_typeS md_pseudo_table[] =
1348{
1349#if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
1350 {"align", s_align_bytes, 0},
1351#else
1352 {"align", s_align_ptwo, 0},
1353#endif
1354 {"arch", set_cpu_arch, 0},
1355#ifndef I386COFF
1356 {"bss", s_bss, 0},
a6c24e68
NC
1357#else
1358 {"lcomm", pe_lcomm, 1},
29b0f896
AM
1359#endif
1360 {"ffloat", float_cons, 'f'},
1361 {"dfloat", float_cons, 'd'},
1362 {"tfloat", float_cons, 'x'},
1363 {"value", cons, 2},
d182319b 1364 {"slong", signed_cons, 4},
29b0f896
AM
1365 {"noopt", s_ignore, 0},
1366 {"optim", s_ignore, 0},
1367 {"code16gcc", set_16bit_gcc_code_flag, CODE_16BIT},
1368 {"code16", set_code_flag, CODE_16BIT},
1369 {"code32", set_code_flag, CODE_32BIT},
da5f19a2 1370#ifdef BFD64
29b0f896 1371 {"code64", set_code_flag, CODE_64BIT},
da5f19a2 1372#endif
29b0f896
AM
1373 {"intel_syntax", set_intel_syntax, 1},
1374 {"att_syntax", set_intel_syntax, 0},
1efbbeb4
L
1375 {"intel_mnemonic", set_intel_mnemonic, 1},
1376 {"att_mnemonic", set_intel_mnemonic, 0},
db51cc60
L
1377 {"allow_index_reg", set_allow_index_reg, 1},
1378 {"disallow_index_reg", set_allow_index_reg, 0},
7bab8ab5
JB
1379 {"sse_check", set_check, 0},
1380 {"operand_check", set_check, 1},
3b22753a
L
1381#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1382 {"largecomm", handle_large_common, 0},
07a53e5c 1383#else
68d20676 1384 {"file", dwarf2_directive_file, 0},
07a53e5c
RH
1385 {"loc", dwarf2_directive_loc, 0},
1386 {"loc_mark_labels", dwarf2_directive_loc_mark_labels, 0},
3b22753a 1387#endif
6482c264
NC
1388#ifdef TE_PE
1389 {"secrel32", pe_directive_secrel, 0},
1390#endif
29b0f896
AM
1391 {0, 0, 0}
1392};
1393
1394/* For interface with expression (). */
1395extern char *input_line_pointer;
1396
1397/* Hash table for instruction mnemonic lookup. */
629310ab 1398static htab_t op_hash;
29b0f896
AM
1399
1400/* Hash table for register lookup. */
629310ab 1401static htab_t reg_hash;
29b0f896 1402\f
ce8a8b2f
AM
1403 /* Various efficient no-op patterns for aligning code labels.
1404 Note: Don't try to assemble the instructions in the comments.
1405 0L and 0w are not legal. */
62a02d25
L
1406static const unsigned char f32_1[] =
1407 {0x90}; /* nop */
1408static const unsigned char f32_2[] =
1409 {0x66,0x90}; /* xchg %ax,%ax */
1410static const unsigned char f32_3[] =
1411 {0x8d,0x76,0x00}; /* leal 0(%esi),%esi */
1412static const unsigned char f32_4[] =
1413 {0x8d,0x74,0x26,0x00}; /* leal 0(%esi,1),%esi */
62a02d25
L
1414static const unsigned char f32_6[] =
1415 {0x8d,0xb6,0x00,0x00,0x00,0x00}; /* leal 0L(%esi),%esi */
1416static const unsigned char f32_7[] =
1417 {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal 0L(%esi,1),%esi */
62a02d25 1418static const unsigned char f16_3[] =
3ae729d5 1419 {0x8d,0x74,0x00}; /* lea 0(%si),%si */
62a02d25 1420static const unsigned char f16_4[] =
3ae729d5
L
1421 {0x8d,0xb4,0x00,0x00}; /* lea 0W(%si),%si */
1422static const unsigned char jump_disp8[] =
1423 {0xeb}; /* jmp disp8 */
1424static const unsigned char jump32_disp32[] =
1425 {0xe9}; /* jmp disp32 */
1426static const unsigned char jump16_disp32[] =
1427 {0x66,0xe9}; /* jmp disp32 */
62a02d25
L
1428/* 32-bit NOPs patterns. */
1429static const unsigned char *const f32_patt[] = {
3ae729d5 1430 f32_1, f32_2, f32_3, f32_4, NULL, f32_6, f32_7
62a02d25
L
1431};
1432/* 16-bit NOPs patterns. */
1433static const unsigned char *const f16_patt[] = {
3ae729d5 1434 f32_1, f32_2, f16_3, f16_4
62a02d25
L
1435};
1436/* nopl (%[re]ax) */
1437static const unsigned char alt_3[] =
1438 {0x0f,0x1f,0x00};
1439/* nopl 0(%[re]ax) */
1440static const unsigned char alt_4[] =
1441 {0x0f,0x1f,0x40,0x00};
1442/* nopl 0(%[re]ax,%[re]ax,1) */
1443static const unsigned char alt_5[] =
1444 {0x0f,0x1f,0x44,0x00,0x00};
1445/* nopw 0(%[re]ax,%[re]ax,1) */
1446static const unsigned char alt_6[] =
1447 {0x66,0x0f,0x1f,0x44,0x00,0x00};
1448/* nopl 0L(%[re]ax) */
1449static const unsigned char alt_7[] =
1450 {0x0f,0x1f,0x80,0x00,0x00,0x00,0x00};
1451/* nopl 0L(%[re]ax,%[re]ax,1) */
1452static const unsigned char alt_8[] =
1453 {0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1454/* nopw 0L(%[re]ax,%[re]ax,1) */
1455static const unsigned char alt_9[] =
1456 {0x66,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1457/* nopw %cs:0L(%[re]ax,%[re]ax,1) */
1458static const unsigned char alt_10[] =
1459 {0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
3ae729d5
L
1460/* data16 nopw %cs:0L(%eax,%eax,1) */
1461static const unsigned char alt_11[] =
1462 {0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
62a02d25
L
1463/* 32-bit and 64-bit NOPs patterns. */
1464static const unsigned char *const alt_patt[] = {
1465 f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
3ae729d5 1466 alt_9, alt_10, alt_11
62a02d25
L
1467};
1468
1469/* Genenerate COUNT bytes of NOPs to WHERE from PATT with the maximum
1470 size of a single NOP instruction MAX_SINGLE_NOP_SIZE. */
1471
1472static void
1473i386_output_nops (char *where, const unsigned char *const *patt,
1474 int count, int max_single_nop_size)
1475
1476{
3ae729d5
L
1477 /* Place the longer NOP first. */
1478 int last;
1479 int offset;
3076e594
NC
1480 const unsigned char *nops;
1481
1482 if (max_single_nop_size < 1)
1483 {
1484 as_fatal (_("i386_output_nops called to generate nops of at most %d bytes!"),
1485 max_single_nop_size);
1486 return;
1487 }
1488
1489 nops = patt[max_single_nop_size - 1];
3ae729d5
L
1490
1491 /* Use the smaller one if the requsted one isn't available. */
1492 if (nops == NULL)
62a02d25 1493 {
3ae729d5
L
1494 max_single_nop_size--;
1495 nops = patt[max_single_nop_size - 1];
62a02d25
L
1496 }
1497
3ae729d5
L
1498 last = count % max_single_nop_size;
1499
1500 count -= last;
1501 for (offset = 0; offset < count; offset += max_single_nop_size)
1502 memcpy (where + offset, nops, max_single_nop_size);
1503
1504 if (last)
1505 {
1506 nops = patt[last - 1];
1507 if (nops == NULL)
1508 {
1509 /* Use the smaller one plus one-byte NOP if the needed one
1510 isn't available. */
1511 last--;
1512 nops = patt[last - 1];
1513 memcpy (where + offset, nops, last);
1514 where[offset + last] = *patt[0];
1515 }
1516 else
1517 memcpy (where + offset, nops, last);
1518 }
62a02d25
L
1519}
1520
3ae729d5
L
1521static INLINE int
1522fits_in_imm7 (offsetT num)
1523{
1524 return (num & 0x7f) == num;
1525}
1526
1527static INLINE int
1528fits_in_imm31 (offsetT num)
1529{
1530 return (num & 0x7fffffff) == num;
1531}
62a02d25
L
1532
1533/* Genenerate COUNT bytes of NOPs to WHERE with the maximum size of a
1534 single NOP instruction LIMIT. */
1535
1536void
3ae729d5 1537i386_generate_nops (fragS *fragP, char *where, offsetT count, int limit)
62a02d25 1538{
3ae729d5 1539 const unsigned char *const *patt = NULL;
62a02d25 1540 int max_single_nop_size;
3ae729d5
L
1541 /* Maximum number of NOPs before switching to jump over NOPs. */
1542 int max_number_of_nops;
62a02d25 1543
3ae729d5 1544 switch (fragP->fr_type)
62a02d25 1545 {
3ae729d5
L
1546 case rs_fill_nop:
1547 case rs_align_code:
1548 break;
e379e5f3
L
1549 case rs_machine_dependent:
1550 /* Allow NOP padding for jumps and calls. */
1551 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
1552 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
1553 break;
1554 /* Fall through. */
3ae729d5 1555 default:
62a02d25
L
1556 return;
1557 }
1558
ccc9c027
L
1559 /* We need to decide which NOP sequence to use for 32bit and
1560 64bit. When -mtune= is used:
4eed87de 1561
76bc74dc
L
1562 1. For PROCESSOR_I386, PROCESSOR_I486, PROCESSOR_PENTIUM and
1563 PROCESSOR_GENERIC32, f32_patt will be used.
80b8656c
L
1564 2. For the rest, alt_patt will be used.
1565
1566 When -mtune= isn't used, alt_patt will be used if
22109423 1567 cpu_arch_isa_flags has CpuNop. Otherwise, f32_patt will
76bc74dc 1568 be used.
ccc9c027
L
1569
1570 When -march= or .arch is used, we can't use anything beyond
1571 cpu_arch_isa_flags. */
1572
1573 if (flag_code == CODE_16BIT)
1574 {
3ae729d5
L
1575 patt = f16_patt;
1576 max_single_nop_size = sizeof (f16_patt) / sizeof (f16_patt[0]);
1577 /* Limit number of NOPs to 2 in 16-bit mode. */
1578 max_number_of_nops = 2;
252b5132 1579 }
33fef721 1580 else
ccc9c027 1581 {
fbf3f584 1582 if (fragP->tc_frag_data.isa == PROCESSOR_UNKNOWN)
ccc9c027
L
1583 {
1584 /* PROCESSOR_UNKNOWN means that all ISAs may be used. */
1585 switch (cpu_arch_tune)
1586 {
1587 case PROCESSOR_UNKNOWN:
1588 /* We use cpu_arch_isa_flags to check if we SHOULD
22109423
L
1589 optimize with nops. */
1590 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1591 patt = alt_patt;
ccc9c027
L
1592 else
1593 patt = f32_patt;
1594 break;
ccc9c027
L
1595 case PROCESSOR_PENTIUM4:
1596 case PROCESSOR_NOCONA:
ef05d495 1597 case PROCESSOR_CORE:
76bc74dc 1598 case PROCESSOR_CORE2:
bd5295b2 1599 case PROCESSOR_COREI7:
3632d14b 1600 case PROCESSOR_L1OM:
7a9068fe 1601 case PROCESSOR_K1OM:
76bc74dc 1602 case PROCESSOR_GENERIC64:
ccc9c027
L
1603 case PROCESSOR_K6:
1604 case PROCESSOR_ATHLON:
1605 case PROCESSOR_K8:
4eed87de 1606 case PROCESSOR_AMDFAM10:
8aedb9fe 1607 case PROCESSOR_BD:
029f3522 1608 case PROCESSOR_ZNVER:
7b458c12 1609 case PROCESSOR_BT:
80b8656c 1610 patt = alt_patt;
ccc9c027 1611 break;
76bc74dc 1612 case PROCESSOR_I386:
ccc9c027
L
1613 case PROCESSOR_I486:
1614 case PROCESSOR_PENTIUM:
2dde1948 1615 case PROCESSOR_PENTIUMPRO:
81486035 1616 case PROCESSOR_IAMCU:
ccc9c027
L
1617 case PROCESSOR_GENERIC32:
1618 patt = f32_patt;
1619 break;
4eed87de 1620 }
ccc9c027
L
1621 }
1622 else
1623 {
fbf3f584 1624 switch (fragP->tc_frag_data.tune)
ccc9c027
L
1625 {
1626 case PROCESSOR_UNKNOWN:
e6a14101 1627 /* When cpu_arch_isa is set, cpu_arch_tune shouldn't be
ccc9c027
L
1628 PROCESSOR_UNKNOWN. */
1629 abort ();
1630 break;
1631
76bc74dc 1632 case PROCESSOR_I386:
ccc9c027
L
1633 case PROCESSOR_I486:
1634 case PROCESSOR_PENTIUM:
81486035 1635 case PROCESSOR_IAMCU:
ccc9c027
L
1636 case PROCESSOR_K6:
1637 case PROCESSOR_ATHLON:
1638 case PROCESSOR_K8:
4eed87de 1639 case PROCESSOR_AMDFAM10:
8aedb9fe 1640 case PROCESSOR_BD:
029f3522 1641 case PROCESSOR_ZNVER:
7b458c12 1642 case PROCESSOR_BT:
ccc9c027
L
1643 case PROCESSOR_GENERIC32:
1644 /* We use cpu_arch_isa_flags to check if we CAN optimize
22109423
L
1645 with nops. */
1646 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1647 patt = alt_patt;
ccc9c027
L
1648 else
1649 patt = f32_patt;
1650 break;
76bc74dc
L
1651 case PROCESSOR_PENTIUMPRO:
1652 case PROCESSOR_PENTIUM4:
1653 case PROCESSOR_NOCONA:
1654 case PROCESSOR_CORE:
ef05d495 1655 case PROCESSOR_CORE2:
bd5295b2 1656 case PROCESSOR_COREI7:
3632d14b 1657 case PROCESSOR_L1OM:
7a9068fe 1658 case PROCESSOR_K1OM:
22109423 1659 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1660 patt = alt_patt;
ccc9c027
L
1661 else
1662 patt = f32_patt;
1663 break;
1664 case PROCESSOR_GENERIC64:
80b8656c 1665 patt = alt_patt;
ccc9c027 1666 break;
4eed87de 1667 }
ccc9c027
L
1668 }
1669
76bc74dc
L
1670 if (patt == f32_patt)
1671 {
3ae729d5
L
1672 max_single_nop_size = sizeof (f32_patt) / sizeof (f32_patt[0]);
1673 /* Limit number of NOPs to 2 for older processors. */
1674 max_number_of_nops = 2;
76bc74dc
L
1675 }
1676 else
1677 {
3ae729d5
L
1678 max_single_nop_size = sizeof (alt_patt) / sizeof (alt_patt[0]);
1679 /* Limit number of NOPs to 7 for newer processors. */
1680 max_number_of_nops = 7;
1681 }
1682 }
1683
1684 if (limit == 0)
1685 limit = max_single_nop_size;
1686
1687 if (fragP->fr_type == rs_fill_nop)
1688 {
1689 /* Output NOPs for .nop directive. */
1690 if (limit > max_single_nop_size)
1691 {
1692 as_bad_where (fragP->fr_file, fragP->fr_line,
1693 _("invalid single nop size: %d "
1694 "(expect within [0, %d])"),
1695 limit, max_single_nop_size);
1696 return;
1697 }
1698 }
e379e5f3 1699 else if (fragP->fr_type != rs_machine_dependent)
3ae729d5
L
1700 fragP->fr_var = count;
1701
1702 if ((count / max_single_nop_size) > max_number_of_nops)
1703 {
1704 /* Generate jump over NOPs. */
1705 offsetT disp = count - 2;
1706 if (fits_in_imm7 (disp))
1707 {
1708 /* Use "jmp disp8" if possible. */
1709 count = disp;
1710 where[0] = jump_disp8[0];
1711 where[1] = count;
1712 where += 2;
1713 }
1714 else
1715 {
1716 unsigned int size_of_jump;
1717
1718 if (flag_code == CODE_16BIT)
1719 {
1720 where[0] = jump16_disp32[0];
1721 where[1] = jump16_disp32[1];
1722 size_of_jump = 2;
1723 }
1724 else
1725 {
1726 where[0] = jump32_disp32[0];
1727 size_of_jump = 1;
1728 }
1729
1730 count -= size_of_jump + 4;
1731 if (!fits_in_imm31 (count))
1732 {
1733 as_bad_where (fragP->fr_file, fragP->fr_line,
1734 _("jump over nop padding out of range"));
1735 return;
1736 }
1737
1738 md_number_to_chars (where + size_of_jump, count, 4);
1739 where += size_of_jump + 4;
76bc74dc 1740 }
ccc9c027 1741 }
3ae729d5
L
1742
1743 /* Generate multiple NOPs. */
1744 i386_output_nops (where, patt, count, limit);
252b5132
RH
1745}
1746
c6fb90c8 1747static INLINE int
0dfbf9d7 1748operand_type_all_zero (const union i386_operand_type *x)
40fb9820 1749{
0dfbf9d7 1750 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1751 {
1752 case 3:
0dfbf9d7 1753 if (x->array[2])
c6fb90c8 1754 return 0;
1a0670f3 1755 /* Fall through. */
c6fb90c8 1756 case 2:
0dfbf9d7 1757 if (x->array[1])
c6fb90c8 1758 return 0;
1a0670f3 1759 /* Fall through. */
c6fb90c8 1760 case 1:
0dfbf9d7 1761 return !x->array[0];
c6fb90c8
L
1762 default:
1763 abort ();
1764 }
40fb9820
L
1765}
1766
c6fb90c8 1767static INLINE void
0dfbf9d7 1768operand_type_set (union i386_operand_type *x, unsigned int v)
40fb9820 1769{
0dfbf9d7 1770 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1771 {
1772 case 3:
0dfbf9d7 1773 x->array[2] = v;
1a0670f3 1774 /* Fall through. */
c6fb90c8 1775 case 2:
0dfbf9d7 1776 x->array[1] = v;
1a0670f3 1777 /* Fall through. */
c6fb90c8 1778 case 1:
0dfbf9d7 1779 x->array[0] = v;
1a0670f3 1780 /* Fall through. */
c6fb90c8
L
1781 break;
1782 default:
1783 abort ();
1784 }
bab6aec1
JB
1785
1786 x->bitfield.class = ClassNone;
75e5731b 1787 x->bitfield.instance = InstanceNone;
c6fb90c8 1788}
40fb9820 1789
c6fb90c8 1790static INLINE int
0dfbf9d7
L
1791operand_type_equal (const union i386_operand_type *x,
1792 const union i386_operand_type *y)
c6fb90c8 1793{
0dfbf9d7 1794 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1795 {
1796 case 3:
0dfbf9d7 1797 if (x->array[2] != y->array[2])
c6fb90c8 1798 return 0;
1a0670f3 1799 /* Fall through. */
c6fb90c8 1800 case 2:
0dfbf9d7 1801 if (x->array[1] != y->array[1])
c6fb90c8 1802 return 0;
1a0670f3 1803 /* Fall through. */
c6fb90c8 1804 case 1:
0dfbf9d7 1805 return x->array[0] == y->array[0];
c6fb90c8
L
1806 break;
1807 default:
1808 abort ();
1809 }
1810}
40fb9820 1811
0dfbf9d7
L
1812static INLINE int
1813cpu_flags_all_zero (const union i386_cpu_flags *x)
1814{
1815 switch (ARRAY_SIZE(x->array))
1816 {
53467f57
IT
1817 case 4:
1818 if (x->array[3])
1819 return 0;
1820 /* Fall through. */
0dfbf9d7
L
1821 case 3:
1822 if (x->array[2])
1823 return 0;
1a0670f3 1824 /* Fall through. */
0dfbf9d7
L
1825 case 2:
1826 if (x->array[1])
1827 return 0;
1a0670f3 1828 /* Fall through. */
0dfbf9d7
L
1829 case 1:
1830 return !x->array[0];
1831 default:
1832 abort ();
1833 }
1834}
1835
0dfbf9d7
L
1836static INLINE int
1837cpu_flags_equal (const union i386_cpu_flags *x,
1838 const union i386_cpu_flags *y)
1839{
1840 switch (ARRAY_SIZE(x->array))
1841 {
53467f57
IT
1842 case 4:
1843 if (x->array[3] != y->array[3])
1844 return 0;
1845 /* Fall through. */
0dfbf9d7
L
1846 case 3:
1847 if (x->array[2] != y->array[2])
1848 return 0;
1a0670f3 1849 /* Fall through. */
0dfbf9d7
L
1850 case 2:
1851 if (x->array[1] != y->array[1])
1852 return 0;
1a0670f3 1853 /* Fall through. */
0dfbf9d7
L
1854 case 1:
1855 return x->array[0] == y->array[0];
1856 break;
1857 default:
1858 abort ();
1859 }
1860}
c6fb90c8
L
1861
1862static INLINE int
1863cpu_flags_check_cpu64 (i386_cpu_flags f)
1864{
1865 return !((flag_code == CODE_64BIT && f.bitfield.cpuno64)
1866 || (flag_code != CODE_64BIT && f.bitfield.cpu64));
40fb9820
L
1867}
1868
c6fb90c8
L
1869static INLINE i386_cpu_flags
1870cpu_flags_and (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1871{
c6fb90c8
L
1872 switch (ARRAY_SIZE (x.array))
1873 {
53467f57
IT
1874 case 4:
1875 x.array [3] &= y.array [3];
1876 /* Fall through. */
c6fb90c8
L
1877 case 3:
1878 x.array [2] &= y.array [2];
1a0670f3 1879 /* Fall through. */
c6fb90c8
L
1880 case 2:
1881 x.array [1] &= y.array [1];
1a0670f3 1882 /* Fall through. */
c6fb90c8
L
1883 case 1:
1884 x.array [0] &= y.array [0];
1885 break;
1886 default:
1887 abort ();
1888 }
1889 return x;
1890}
40fb9820 1891
c6fb90c8
L
1892static INLINE i386_cpu_flags
1893cpu_flags_or (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1894{
c6fb90c8 1895 switch (ARRAY_SIZE (x.array))
40fb9820 1896 {
53467f57
IT
1897 case 4:
1898 x.array [3] |= y.array [3];
1899 /* Fall through. */
c6fb90c8
L
1900 case 3:
1901 x.array [2] |= y.array [2];
1a0670f3 1902 /* Fall through. */
c6fb90c8
L
1903 case 2:
1904 x.array [1] |= y.array [1];
1a0670f3 1905 /* Fall through. */
c6fb90c8
L
1906 case 1:
1907 x.array [0] |= y.array [0];
40fb9820
L
1908 break;
1909 default:
1910 abort ();
1911 }
40fb9820
L
1912 return x;
1913}
1914
309d3373
JB
1915static INLINE i386_cpu_flags
1916cpu_flags_and_not (i386_cpu_flags x, i386_cpu_flags y)
1917{
1918 switch (ARRAY_SIZE (x.array))
1919 {
53467f57
IT
1920 case 4:
1921 x.array [3] &= ~y.array [3];
1922 /* Fall through. */
309d3373
JB
1923 case 3:
1924 x.array [2] &= ~y.array [2];
1a0670f3 1925 /* Fall through. */
309d3373
JB
1926 case 2:
1927 x.array [1] &= ~y.array [1];
1a0670f3 1928 /* Fall through. */
309d3373
JB
1929 case 1:
1930 x.array [0] &= ~y.array [0];
1931 break;
1932 default:
1933 abort ();
1934 }
1935 return x;
1936}
1937
6c0946d0
JB
1938static const i386_cpu_flags avx512 = CPU_ANY_AVX512F_FLAGS;
1939
c0f3af97
L
1940#define CPU_FLAGS_ARCH_MATCH 0x1
1941#define CPU_FLAGS_64BIT_MATCH 0x2
1942
c0f3af97 1943#define CPU_FLAGS_PERFECT_MATCH \
db12e14e 1944 (CPU_FLAGS_ARCH_MATCH | CPU_FLAGS_64BIT_MATCH)
c0f3af97
L
1945
1946/* Return CPU flags match bits. */
3629bb00 1947
40fb9820 1948static int
d3ce72d0 1949cpu_flags_match (const insn_template *t)
40fb9820 1950{
c0f3af97
L
1951 i386_cpu_flags x = t->cpu_flags;
1952 int match = cpu_flags_check_cpu64 (x) ? CPU_FLAGS_64BIT_MATCH : 0;
40fb9820
L
1953
1954 x.bitfield.cpu64 = 0;
1955 x.bitfield.cpuno64 = 0;
1956
0dfbf9d7 1957 if (cpu_flags_all_zero (&x))
c0f3af97
L
1958 {
1959 /* This instruction is available on all archs. */
db12e14e 1960 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 1961 }
3629bb00
L
1962 else
1963 {
c0f3af97 1964 /* This instruction is available only on some archs. */
3629bb00
L
1965 i386_cpu_flags cpu = cpu_arch_flags;
1966
ab592e75
JB
1967 /* AVX512VL is no standalone feature - match it and then strip it. */
1968 if (x.bitfield.cpuavx512vl && !cpu.bitfield.cpuavx512vl)
1969 return match;
1970 x.bitfield.cpuavx512vl = 0;
1971
3629bb00 1972 cpu = cpu_flags_and (x, cpu);
c0f3af97
L
1973 if (!cpu_flags_all_zero (&cpu))
1974 {
57392598 1975 if (x.bitfield.cpuavx)
a5ff0eb2 1976 {
929f69fa 1977 /* We need to check a few extra flags with AVX. */
b9d49817 1978 if (cpu.bitfield.cpuavx
40d231b4
JB
1979 && (!t->opcode_modifier.sse2avx
1980 || (sse2avx && !i.prefix[DATA_PREFIX]))
b9d49817 1981 && (!x.bitfield.cpuaes || cpu.bitfield.cpuaes)
929f69fa 1982 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
b9d49817
JB
1983 && (!x.bitfield.cpupclmul || cpu.bitfield.cpupclmul))
1984 match |= CPU_FLAGS_ARCH_MATCH;
a5ff0eb2 1985 }
929f69fa
JB
1986 else if (x.bitfield.cpuavx512f)
1987 {
1988 /* We need to check a few extra flags with AVX512F. */
1989 if (cpu.bitfield.cpuavx512f
1990 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
1991 && (!x.bitfield.cpuvaes || cpu.bitfield.cpuvaes)
1992 && (!x.bitfield.cpuvpclmulqdq || cpu.bitfield.cpuvpclmulqdq))
1993 match |= CPU_FLAGS_ARCH_MATCH;
1994 }
a5ff0eb2 1995 else
db12e14e 1996 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 1997 }
3629bb00 1998 }
c0f3af97 1999 return match;
40fb9820
L
2000}
2001
c6fb90c8
L
2002static INLINE i386_operand_type
2003operand_type_and (i386_operand_type x, i386_operand_type y)
40fb9820 2004{
bab6aec1
JB
2005 if (x.bitfield.class != y.bitfield.class)
2006 x.bitfield.class = ClassNone;
75e5731b
JB
2007 if (x.bitfield.instance != y.bitfield.instance)
2008 x.bitfield.instance = InstanceNone;
bab6aec1 2009
c6fb90c8
L
2010 switch (ARRAY_SIZE (x.array))
2011 {
2012 case 3:
2013 x.array [2] &= y.array [2];
1a0670f3 2014 /* Fall through. */
c6fb90c8
L
2015 case 2:
2016 x.array [1] &= y.array [1];
1a0670f3 2017 /* Fall through. */
c6fb90c8
L
2018 case 1:
2019 x.array [0] &= y.array [0];
2020 break;
2021 default:
2022 abort ();
2023 }
2024 return x;
40fb9820
L
2025}
2026
73053c1f
JB
2027static INLINE i386_operand_type
2028operand_type_and_not (i386_operand_type x, i386_operand_type y)
2029{
bab6aec1 2030 gas_assert (y.bitfield.class == ClassNone);
75e5731b 2031 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 2032
73053c1f
JB
2033 switch (ARRAY_SIZE (x.array))
2034 {
2035 case 3:
2036 x.array [2] &= ~y.array [2];
2037 /* Fall through. */
2038 case 2:
2039 x.array [1] &= ~y.array [1];
2040 /* Fall through. */
2041 case 1:
2042 x.array [0] &= ~y.array [0];
2043 break;
2044 default:
2045 abort ();
2046 }
2047 return x;
2048}
2049
c6fb90c8
L
2050static INLINE i386_operand_type
2051operand_type_or (i386_operand_type x, i386_operand_type y)
40fb9820 2052{
bab6aec1
JB
2053 gas_assert (x.bitfield.class == ClassNone ||
2054 y.bitfield.class == ClassNone ||
2055 x.bitfield.class == y.bitfield.class);
75e5731b
JB
2056 gas_assert (x.bitfield.instance == InstanceNone ||
2057 y.bitfield.instance == InstanceNone ||
2058 x.bitfield.instance == y.bitfield.instance);
bab6aec1 2059
c6fb90c8 2060 switch (ARRAY_SIZE (x.array))
40fb9820 2061 {
c6fb90c8
L
2062 case 3:
2063 x.array [2] |= y.array [2];
1a0670f3 2064 /* Fall through. */
c6fb90c8
L
2065 case 2:
2066 x.array [1] |= y.array [1];
1a0670f3 2067 /* Fall through. */
c6fb90c8
L
2068 case 1:
2069 x.array [0] |= y.array [0];
40fb9820
L
2070 break;
2071 default:
2072 abort ();
2073 }
c6fb90c8
L
2074 return x;
2075}
40fb9820 2076
c6fb90c8
L
2077static INLINE i386_operand_type
2078operand_type_xor (i386_operand_type x, i386_operand_type y)
2079{
bab6aec1 2080 gas_assert (y.bitfield.class == ClassNone);
75e5731b 2081 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 2082
c6fb90c8
L
2083 switch (ARRAY_SIZE (x.array))
2084 {
2085 case 3:
2086 x.array [2] ^= y.array [2];
1a0670f3 2087 /* Fall through. */
c6fb90c8
L
2088 case 2:
2089 x.array [1] ^= y.array [1];
1a0670f3 2090 /* Fall through. */
c6fb90c8
L
2091 case 1:
2092 x.array [0] ^= y.array [0];
2093 break;
2094 default:
2095 abort ();
2096 }
40fb9820
L
2097 return x;
2098}
2099
40fb9820
L
2100static const i386_operand_type disp16 = OPERAND_TYPE_DISP16;
2101static const i386_operand_type disp32 = OPERAND_TYPE_DISP32;
2102static const i386_operand_type disp32s = OPERAND_TYPE_DISP32S;
2103static const i386_operand_type disp16_32 = OPERAND_TYPE_DISP16_32;
bab6aec1
JB
2104static const i386_operand_type anydisp = OPERAND_TYPE_ANYDISP;
2105static const i386_operand_type anyimm = OPERAND_TYPE_ANYIMM;
40fb9820 2106static const i386_operand_type regxmm = OPERAND_TYPE_REGXMM;
43234a1e 2107static const i386_operand_type regmask = OPERAND_TYPE_REGMASK;
40fb9820
L
2108static const i386_operand_type imm8 = OPERAND_TYPE_IMM8;
2109static const i386_operand_type imm8s = OPERAND_TYPE_IMM8S;
2110static const i386_operand_type imm16 = OPERAND_TYPE_IMM16;
2111static const i386_operand_type imm32 = OPERAND_TYPE_IMM32;
2112static const i386_operand_type imm32s = OPERAND_TYPE_IMM32S;
2113static const i386_operand_type imm64 = OPERAND_TYPE_IMM64;
2114static const i386_operand_type imm16_32 = OPERAND_TYPE_IMM16_32;
2115static const i386_operand_type imm16_32s = OPERAND_TYPE_IMM16_32S;
2116static const i386_operand_type imm16_32_32s = OPERAND_TYPE_IMM16_32_32S;
2117
2118enum operand_type
2119{
2120 reg,
40fb9820
L
2121 imm,
2122 disp,
2123 anymem
2124};
2125
c6fb90c8 2126static INLINE int
40fb9820
L
2127operand_type_check (i386_operand_type t, enum operand_type c)
2128{
2129 switch (c)
2130 {
2131 case reg:
bab6aec1 2132 return t.bitfield.class == Reg;
40fb9820 2133
40fb9820
L
2134 case imm:
2135 return (t.bitfield.imm8
2136 || t.bitfield.imm8s
2137 || t.bitfield.imm16
2138 || t.bitfield.imm32
2139 || t.bitfield.imm32s
2140 || t.bitfield.imm64);
2141
2142 case disp:
2143 return (t.bitfield.disp8
2144 || t.bitfield.disp16
2145 || t.bitfield.disp32
2146 || t.bitfield.disp32s
2147 || t.bitfield.disp64);
2148
2149 case anymem:
2150 return (t.bitfield.disp8
2151 || t.bitfield.disp16
2152 || t.bitfield.disp32
2153 || t.bitfield.disp32s
2154 || t.bitfield.disp64
2155 || t.bitfield.baseindex);
2156
2157 default:
2158 abort ();
2159 }
2cfe26b6
AM
2160
2161 return 0;
40fb9820
L
2162}
2163
7a54636a
L
2164/* Return 1 if there is no conflict in 8bit/16bit/32bit/64bit/80bit size
2165 between operand GIVEN and opeand WANTED for instruction template T. */
5c07affc
L
2166
2167static INLINE int
7a54636a
L
2168match_operand_size (const insn_template *t, unsigned int wanted,
2169 unsigned int given)
5c07affc 2170{
3ac21baa
JB
2171 return !((i.types[given].bitfield.byte
2172 && !t->operand_types[wanted].bitfield.byte)
2173 || (i.types[given].bitfield.word
2174 && !t->operand_types[wanted].bitfield.word)
2175 || (i.types[given].bitfield.dword
2176 && !t->operand_types[wanted].bitfield.dword)
2177 || (i.types[given].bitfield.qword
2178 && !t->operand_types[wanted].bitfield.qword)
2179 || (i.types[given].bitfield.tbyte
2180 && !t->operand_types[wanted].bitfield.tbyte));
5c07affc
L
2181}
2182
dd40ce22
L
2183/* Return 1 if there is no conflict in SIMD register between operand
2184 GIVEN and opeand WANTED for instruction template T. */
1b54b8d7
JB
2185
2186static INLINE int
dd40ce22
L
2187match_simd_size (const insn_template *t, unsigned int wanted,
2188 unsigned int given)
1b54b8d7 2189{
3ac21baa
JB
2190 return !((i.types[given].bitfield.xmmword
2191 && !t->operand_types[wanted].bitfield.xmmword)
2192 || (i.types[given].bitfield.ymmword
2193 && !t->operand_types[wanted].bitfield.ymmword)
2194 || (i.types[given].bitfield.zmmword
260cd341
LC
2195 && !t->operand_types[wanted].bitfield.zmmword)
2196 || (i.types[given].bitfield.tmmword
2197 && !t->operand_types[wanted].bitfield.tmmword));
1b54b8d7
JB
2198}
2199
7a54636a
L
2200/* Return 1 if there is no conflict in any size between operand GIVEN
2201 and opeand WANTED for instruction template T. */
5c07affc
L
2202
2203static INLINE int
dd40ce22
L
2204match_mem_size (const insn_template *t, unsigned int wanted,
2205 unsigned int given)
5c07affc 2206{
7a54636a 2207 return (match_operand_size (t, wanted, given)
3ac21baa 2208 && !((i.types[given].bitfield.unspecified
5273a3cd 2209 && !i.broadcast.type
3ac21baa
JB
2210 && !t->operand_types[wanted].bitfield.unspecified)
2211 || (i.types[given].bitfield.fword
2212 && !t->operand_types[wanted].bitfield.fword)
1b54b8d7
JB
2213 /* For scalar opcode templates to allow register and memory
2214 operands at the same time, some special casing is needed
d6793fa1
JB
2215 here. Also for v{,p}broadcast*, {,v}pmov{s,z}*, and
2216 down-conversion vpmov*. */
3528c362 2217 || ((t->operand_types[wanted].bitfield.class == RegSIMD
bc49bfd8
JB
2218 && t->operand_types[wanted].bitfield.byte
2219 + t->operand_types[wanted].bitfield.word
2220 + t->operand_types[wanted].bitfield.dword
2221 + t->operand_types[wanted].bitfield.qword
2222 > !!t->opcode_modifier.broadcast)
3ac21baa
JB
2223 ? (i.types[given].bitfield.xmmword
2224 || i.types[given].bitfield.ymmword
2225 || i.types[given].bitfield.zmmword)
2226 : !match_simd_size(t, wanted, given))));
5c07affc
L
2227}
2228
3ac21baa
JB
2229/* Return value has MATCH_STRAIGHT set if there is no size conflict on any
2230 operands for instruction template T, and it has MATCH_REVERSE set if there
2231 is no size conflict on any operands for the template with operands reversed
2232 (and the template allows for reversing in the first place). */
5c07affc 2233
3ac21baa
JB
2234#define MATCH_STRAIGHT 1
2235#define MATCH_REVERSE 2
2236
2237static INLINE unsigned int
d3ce72d0 2238operand_size_match (const insn_template *t)
5c07affc 2239{
3ac21baa 2240 unsigned int j, match = MATCH_STRAIGHT;
5c07affc 2241
0cfa3eb3 2242 /* Don't check non-absolute jump instructions. */
5c07affc 2243 if (t->opcode_modifier.jump
0cfa3eb3 2244 && t->opcode_modifier.jump != JUMP_ABSOLUTE)
5c07affc
L
2245 return match;
2246
2247 /* Check memory and accumulator operand size. */
2248 for (j = 0; j < i.operands; j++)
2249 {
3528c362
JB
2250 if (i.types[j].bitfield.class != Reg
2251 && i.types[j].bitfield.class != RegSIMD
601e8564 2252 && t->opcode_modifier.anysize)
5c07affc
L
2253 continue;
2254
bab6aec1 2255 if (t->operand_types[j].bitfield.class == Reg
7a54636a 2256 && !match_operand_size (t, j, j))
5c07affc
L
2257 {
2258 match = 0;
2259 break;
2260 }
2261
3528c362 2262 if (t->operand_types[j].bitfield.class == RegSIMD
3ac21baa 2263 && !match_simd_size (t, j, j))
1b54b8d7
JB
2264 {
2265 match = 0;
2266 break;
2267 }
2268
75e5731b 2269 if (t->operand_types[j].bitfield.instance == Accum
7a54636a 2270 && (!match_operand_size (t, j, j) || !match_simd_size (t, j, j)))
1b54b8d7
JB
2271 {
2272 match = 0;
2273 break;
2274 }
2275
c48dadc9 2276 if ((i.flags[j] & Operand_Mem) && !match_mem_size (t, j, j))
5c07affc
L
2277 {
2278 match = 0;
2279 break;
2280 }
2281 }
2282
3ac21baa 2283 if (!t->opcode_modifier.d)
891edac4 2284 {
dc1e8a47 2285 mismatch:
3ac21baa
JB
2286 if (!match)
2287 i.error = operand_size_mismatch;
2288 return match;
891edac4 2289 }
5c07affc
L
2290
2291 /* Check reverse. */
f5eb1d70 2292 gas_assert (i.operands >= 2 && i.operands <= 3);
5c07affc 2293
f5eb1d70 2294 for (j = 0; j < i.operands; j++)
5c07affc 2295 {
f5eb1d70
JB
2296 unsigned int given = i.operands - j - 1;
2297
bab6aec1 2298 if (t->operand_types[j].bitfield.class == Reg
f5eb1d70 2299 && !match_operand_size (t, j, given))
891edac4 2300 goto mismatch;
5c07affc 2301
3528c362 2302 if (t->operand_types[j].bitfield.class == RegSIMD
f5eb1d70 2303 && !match_simd_size (t, j, given))
dbbc8b7e
JB
2304 goto mismatch;
2305
75e5731b 2306 if (t->operand_types[j].bitfield.instance == Accum
f5eb1d70
JB
2307 && (!match_operand_size (t, j, given)
2308 || !match_simd_size (t, j, given)))
dbbc8b7e
JB
2309 goto mismatch;
2310
f5eb1d70 2311 if ((i.flags[given] & Operand_Mem) && !match_mem_size (t, j, given))
891edac4 2312 goto mismatch;
5c07affc
L
2313 }
2314
3ac21baa 2315 return match | MATCH_REVERSE;
5c07affc
L
2316}
2317
c6fb90c8 2318static INLINE int
40fb9820
L
2319operand_type_match (i386_operand_type overlap,
2320 i386_operand_type given)
2321{
2322 i386_operand_type temp = overlap;
2323
7d5e4556 2324 temp.bitfield.unspecified = 0;
5c07affc
L
2325 temp.bitfield.byte = 0;
2326 temp.bitfield.word = 0;
2327 temp.bitfield.dword = 0;
2328 temp.bitfield.fword = 0;
2329 temp.bitfield.qword = 0;
2330 temp.bitfield.tbyte = 0;
2331 temp.bitfield.xmmword = 0;
c0f3af97 2332 temp.bitfield.ymmword = 0;
43234a1e 2333 temp.bitfield.zmmword = 0;
260cd341 2334 temp.bitfield.tmmword = 0;
0dfbf9d7 2335 if (operand_type_all_zero (&temp))
891edac4 2336 goto mismatch;
40fb9820 2337
6f2f06be 2338 if (given.bitfield.baseindex == overlap.bitfield.baseindex)
891edac4
L
2339 return 1;
2340
dc1e8a47 2341 mismatch:
a65babc9 2342 i.error = operand_type_mismatch;
891edac4 2343 return 0;
40fb9820
L
2344}
2345
7d5e4556 2346/* If given types g0 and g1 are registers they must be of the same type
10c17abd 2347 unless the expected operand type register overlap is null.
5de4d9ef 2348 Some Intel syntax memory operand size checking also happens here. */
40fb9820 2349
c6fb90c8 2350static INLINE int
dc821c5f 2351operand_type_register_match (i386_operand_type g0,
40fb9820 2352 i386_operand_type t0,
40fb9820
L
2353 i386_operand_type g1,
2354 i386_operand_type t1)
2355{
bab6aec1 2356 if (g0.bitfield.class != Reg
3528c362 2357 && g0.bitfield.class != RegSIMD
10c17abd
JB
2358 && (!operand_type_check (g0, anymem)
2359 || g0.bitfield.unspecified
5de4d9ef
JB
2360 || (t0.bitfield.class != Reg
2361 && t0.bitfield.class != RegSIMD)))
40fb9820
L
2362 return 1;
2363
bab6aec1 2364 if (g1.bitfield.class != Reg
3528c362 2365 && g1.bitfield.class != RegSIMD
10c17abd
JB
2366 && (!operand_type_check (g1, anymem)
2367 || g1.bitfield.unspecified
5de4d9ef
JB
2368 || (t1.bitfield.class != Reg
2369 && t1.bitfield.class != RegSIMD)))
40fb9820
L
2370 return 1;
2371
dc821c5f
JB
2372 if (g0.bitfield.byte == g1.bitfield.byte
2373 && g0.bitfield.word == g1.bitfield.word
2374 && g0.bitfield.dword == g1.bitfield.dword
10c17abd
JB
2375 && g0.bitfield.qword == g1.bitfield.qword
2376 && g0.bitfield.xmmword == g1.bitfield.xmmword
2377 && g0.bitfield.ymmword == g1.bitfield.ymmword
2378 && g0.bitfield.zmmword == g1.bitfield.zmmword)
40fb9820
L
2379 return 1;
2380
dc821c5f
JB
2381 if (!(t0.bitfield.byte & t1.bitfield.byte)
2382 && !(t0.bitfield.word & t1.bitfield.word)
2383 && !(t0.bitfield.dword & t1.bitfield.dword)
10c17abd
JB
2384 && !(t0.bitfield.qword & t1.bitfield.qword)
2385 && !(t0.bitfield.xmmword & t1.bitfield.xmmword)
2386 && !(t0.bitfield.ymmword & t1.bitfield.ymmword)
2387 && !(t0.bitfield.zmmword & t1.bitfield.zmmword))
891edac4
L
2388 return 1;
2389
a65babc9 2390 i.error = register_type_mismatch;
891edac4
L
2391
2392 return 0;
40fb9820
L
2393}
2394
4c692bc7
JB
2395static INLINE unsigned int
2396register_number (const reg_entry *r)
2397{
2398 unsigned int nr = r->reg_num;
2399
2400 if (r->reg_flags & RegRex)
2401 nr += 8;
2402
200cbe0f
L
2403 if (r->reg_flags & RegVRex)
2404 nr += 16;
2405
4c692bc7
JB
2406 return nr;
2407}
2408
252b5132 2409static INLINE unsigned int
40fb9820 2410mode_from_disp_size (i386_operand_type t)
252b5132 2411{
b5014f7a 2412 if (t.bitfield.disp8)
40fb9820
L
2413 return 1;
2414 else if (t.bitfield.disp16
2415 || t.bitfield.disp32
2416 || t.bitfield.disp32s)
2417 return 2;
2418 else
2419 return 0;
252b5132
RH
2420}
2421
2422static INLINE int
65879393 2423fits_in_signed_byte (addressT num)
252b5132 2424{
65879393 2425 return num + 0x80 <= 0xff;
47926f60 2426}
252b5132
RH
2427
2428static INLINE int
65879393 2429fits_in_unsigned_byte (addressT num)
252b5132 2430{
65879393 2431 return num <= 0xff;
47926f60 2432}
252b5132
RH
2433
2434static INLINE int
65879393 2435fits_in_unsigned_word (addressT num)
252b5132 2436{
65879393 2437 return num <= 0xffff;
47926f60 2438}
252b5132
RH
2439
2440static INLINE int
65879393 2441fits_in_signed_word (addressT num)
252b5132 2442{
65879393 2443 return num + 0x8000 <= 0xffff;
47926f60 2444}
2a962e6d 2445
3e73aa7c 2446static INLINE int
65879393 2447fits_in_signed_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2448{
2449#ifndef BFD64
2450 return 1;
2451#else
65879393 2452 return num + 0x80000000 <= 0xffffffff;
3e73aa7c
JH
2453#endif
2454} /* fits_in_signed_long() */
2a962e6d 2455
3e73aa7c 2456static INLINE int
65879393 2457fits_in_unsigned_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2458{
2459#ifndef BFD64
2460 return 1;
2461#else
65879393 2462 return num <= 0xffffffff;
3e73aa7c
JH
2463#endif
2464} /* fits_in_unsigned_long() */
252b5132 2465
43234a1e 2466static INLINE int
b5014f7a 2467fits_in_disp8 (offsetT num)
43234a1e
L
2468{
2469 int shift = i.memshift;
2470 unsigned int mask;
2471
2472 if (shift == -1)
2473 abort ();
2474
2475 mask = (1 << shift) - 1;
2476
2477 /* Return 0 if NUM isn't properly aligned. */
2478 if ((num & mask))
2479 return 0;
2480
2481 /* Check if NUM will fit in 8bit after shift. */
2482 return fits_in_signed_byte (num >> shift);
2483}
2484
a683cc34
SP
2485static INLINE int
2486fits_in_imm4 (offsetT num)
2487{
2488 return (num & 0xf) == num;
2489}
2490
40fb9820 2491static i386_operand_type
e3bb37b5 2492smallest_imm_type (offsetT num)
252b5132 2493{
40fb9820 2494 i386_operand_type t;
7ab9ffdd 2495
0dfbf9d7 2496 operand_type_set (&t, 0);
40fb9820
L
2497 t.bitfield.imm64 = 1;
2498
2499 if (cpu_arch_tune != PROCESSOR_I486 && num == 1)
e413e4e9
AM
2500 {
2501 /* This code is disabled on the 486 because all the Imm1 forms
2502 in the opcode table are slower on the i486. They're the
2503 versions with the implicitly specified single-position
2504 displacement, which has another syntax if you really want to
2505 use that form. */
40fb9820
L
2506 t.bitfield.imm1 = 1;
2507 t.bitfield.imm8 = 1;
2508 t.bitfield.imm8s = 1;
2509 t.bitfield.imm16 = 1;
2510 t.bitfield.imm32 = 1;
2511 t.bitfield.imm32s = 1;
2512 }
2513 else if (fits_in_signed_byte (num))
2514 {
2515 t.bitfield.imm8 = 1;
2516 t.bitfield.imm8s = 1;
2517 t.bitfield.imm16 = 1;
2518 t.bitfield.imm32 = 1;
2519 t.bitfield.imm32s = 1;
2520 }
2521 else if (fits_in_unsigned_byte (num))
2522 {
2523 t.bitfield.imm8 = 1;
2524 t.bitfield.imm16 = 1;
2525 t.bitfield.imm32 = 1;
2526 t.bitfield.imm32s = 1;
2527 }
2528 else if (fits_in_signed_word (num) || fits_in_unsigned_word (num))
2529 {
2530 t.bitfield.imm16 = 1;
2531 t.bitfield.imm32 = 1;
2532 t.bitfield.imm32s = 1;
2533 }
2534 else if (fits_in_signed_long (num))
2535 {
2536 t.bitfield.imm32 = 1;
2537 t.bitfield.imm32s = 1;
2538 }
2539 else if (fits_in_unsigned_long (num))
2540 t.bitfield.imm32 = 1;
2541
2542 return t;
47926f60 2543}
252b5132 2544
847f7ad4 2545static offsetT
e3bb37b5 2546offset_in_range (offsetT val, int size)
847f7ad4 2547{
508866be 2548 addressT mask;
ba2adb93 2549
847f7ad4
AM
2550 switch (size)
2551 {
508866be
L
2552 case 1: mask = ((addressT) 1 << 8) - 1; break;
2553 case 2: mask = ((addressT) 1 << 16) - 1; break;
3b0ec529 2554 case 4: mask = ((addressT) 2 << 31) - 1; break;
3e73aa7c
JH
2555#ifdef BFD64
2556 case 8: mask = ((addressT) 2 << 63) - 1; break;
2557#endif
47926f60 2558 default: abort ();
847f7ad4
AM
2559 }
2560
47926f60 2561 if ((val & ~mask) != 0 && (val & ~mask) != ~mask)
847f7ad4
AM
2562 {
2563 char buf1[40], buf2[40];
2564
2565 sprint_value (buf1, val);
2566 sprint_value (buf2, val & mask);
2567 as_warn (_("%s shortened to %s"), buf1, buf2);
2568 }
2569 return val & mask;
2570}
2571
c32fa91d
L
2572enum PREFIX_GROUP
2573{
2574 PREFIX_EXIST = 0,
2575 PREFIX_LOCK,
2576 PREFIX_REP,
04ef582a 2577 PREFIX_DS,
c32fa91d
L
2578 PREFIX_OTHER
2579};
2580
2581/* Returns
2582 a. PREFIX_EXIST if attempting to add a prefix where one from the
2583 same class already exists.
2584 b. PREFIX_LOCK if lock prefix is added.
2585 c. PREFIX_REP if rep/repne prefix is added.
04ef582a
L
2586 d. PREFIX_DS if ds prefix is added.
2587 e. PREFIX_OTHER if other prefix is added.
c32fa91d
L
2588 */
2589
2590static enum PREFIX_GROUP
e3bb37b5 2591add_prefix (unsigned int prefix)
252b5132 2592{
c32fa91d 2593 enum PREFIX_GROUP ret = PREFIX_OTHER;
b1905489 2594 unsigned int q;
252b5132 2595
29b0f896
AM
2596 if (prefix >= REX_OPCODE && prefix < REX_OPCODE + 16
2597 && flag_code == CODE_64BIT)
b1905489 2598 {
161a04f6 2599 if ((i.prefix[REX_PREFIX] & prefix & REX_W)
44846f29
JB
2600 || (i.prefix[REX_PREFIX] & prefix & REX_R)
2601 || (i.prefix[REX_PREFIX] & prefix & REX_X)
2602 || (i.prefix[REX_PREFIX] & prefix & REX_B))
c32fa91d 2603 ret = PREFIX_EXIST;
b1905489
JB
2604 q = REX_PREFIX;
2605 }
3e73aa7c 2606 else
b1905489
JB
2607 {
2608 switch (prefix)
2609 {
2610 default:
2611 abort ();
2612
b1905489 2613 case DS_PREFIX_OPCODE:
04ef582a
L
2614 ret = PREFIX_DS;
2615 /* Fall through. */
2616 case CS_PREFIX_OPCODE:
b1905489
JB
2617 case ES_PREFIX_OPCODE:
2618 case FS_PREFIX_OPCODE:
2619 case GS_PREFIX_OPCODE:
2620 case SS_PREFIX_OPCODE:
2621 q = SEG_PREFIX;
2622 break;
2623
2624 case REPNE_PREFIX_OPCODE:
2625 case REPE_PREFIX_OPCODE:
c32fa91d
L
2626 q = REP_PREFIX;
2627 ret = PREFIX_REP;
2628 break;
2629
b1905489 2630 case LOCK_PREFIX_OPCODE:
c32fa91d
L
2631 q = LOCK_PREFIX;
2632 ret = PREFIX_LOCK;
b1905489
JB
2633 break;
2634
2635 case FWAIT_OPCODE:
2636 q = WAIT_PREFIX;
2637 break;
2638
2639 case ADDR_PREFIX_OPCODE:
2640 q = ADDR_PREFIX;
2641 break;
2642
2643 case DATA_PREFIX_OPCODE:
2644 q = DATA_PREFIX;
2645 break;
2646 }
2647 if (i.prefix[q] != 0)
c32fa91d 2648 ret = PREFIX_EXIST;
b1905489 2649 }
252b5132 2650
b1905489 2651 if (ret)
252b5132 2652 {
b1905489
JB
2653 if (!i.prefix[q])
2654 ++i.prefixes;
2655 i.prefix[q] |= prefix;
252b5132 2656 }
b1905489
JB
2657 else
2658 as_bad (_("same type of prefix used twice"));
252b5132 2659
252b5132
RH
2660 return ret;
2661}
2662
2663static void
78f12dd3 2664update_code_flag (int value, int check)
eecb386c 2665{
78f12dd3
L
2666 PRINTF_LIKE ((*as_error));
2667
1e9cc1c2 2668 flag_code = (enum flag_code) value;
40fb9820
L
2669 if (flag_code == CODE_64BIT)
2670 {
2671 cpu_arch_flags.bitfield.cpu64 = 1;
2672 cpu_arch_flags.bitfield.cpuno64 = 0;
40fb9820
L
2673 }
2674 else
2675 {
2676 cpu_arch_flags.bitfield.cpu64 = 0;
2677 cpu_arch_flags.bitfield.cpuno64 = 1;
40fb9820
L
2678 }
2679 if (value == CODE_64BIT && !cpu_arch_flags.bitfield.cpulm )
3e73aa7c 2680 {
78f12dd3
L
2681 if (check)
2682 as_error = as_fatal;
2683 else
2684 as_error = as_bad;
2685 (*as_error) (_("64bit mode not supported on `%s'."),
2686 cpu_arch_name ? cpu_arch_name : default_arch);
3e73aa7c 2687 }
40fb9820 2688 if (value == CODE_32BIT && !cpu_arch_flags.bitfield.cpui386)
3e73aa7c 2689 {
78f12dd3
L
2690 if (check)
2691 as_error = as_fatal;
2692 else
2693 as_error = as_bad;
2694 (*as_error) (_("32bit mode not supported on `%s'."),
2695 cpu_arch_name ? cpu_arch_name : default_arch);
3e73aa7c 2696 }
eecb386c
AM
2697 stackop_size = '\0';
2698}
2699
78f12dd3
L
2700static void
2701set_code_flag (int value)
2702{
2703 update_code_flag (value, 0);
2704}
2705
eecb386c 2706static void
e3bb37b5 2707set_16bit_gcc_code_flag (int new_code_flag)
252b5132 2708{
1e9cc1c2 2709 flag_code = (enum flag_code) new_code_flag;
40fb9820
L
2710 if (flag_code != CODE_16BIT)
2711 abort ();
2712 cpu_arch_flags.bitfield.cpu64 = 0;
2713 cpu_arch_flags.bitfield.cpuno64 = 1;
9306ca4a 2714 stackop_size = LONG_MNEM_SUFFIX;
252b5132
RH
2715}
2716
2717static void
e3bb37b5 2718set_intel_syntax (int syntax_flag)
252b5132
RH
2719{
2720 /* Find out if register prefixing is specified. */
2721 int ask_naked_reg = 0;
2722
2723 SKIP_WHITESPACE ();
29b0f896 2724 if (!is_end_of_line[(unsigned char) *input_line_pointer])
252b5132 2725 {
d02603dc
NC
2726 char *string;
2727 int e = get_symbol_name (&string);
252b5132 2728
47926f60 2729 if (strcmp (string, "prefix") == 0)
252b5132 2730 ask_naked_reg = 1;
47926f60 2731 else if (strcmp (string, "noprefix") == 0)
252b5132
RH
2732 ask_naked_reg = -1;
2733 else
d0b47220 2734 as_bad (_("bad argument to syntax directive."));
d02603dc 2735 (void) restore_line_pointer (e);
252b5132
RH
2736 }
2737 demand_empty_rest_of_line ();
c3332e24 2738
252b5132
RH
2739 intel_syntax = syntax_flag;
2740
2741 if (ask_naked_reg == 0)
f86103b7
AM
2742 allow_naked_reg = (intel_syntax
2743 && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
252b5132
RH
2744 else
2745 allow_naked_reg = (ask_naked_reg < 0);
9306ca4a 2746
ee86248c 2747 expr_set_rank (O_full_ptr, syntax_flag ? 10 : 0);
7ab9ffdd 2748
e4a3b5a4 2749 identifier_chars['%'] = intel_syntax && allow_naked_reg ? '%' : 0;
9306ca4a 2750 identifier_chars['$'] = intel_syntax ? '$' : 0;
e4a3b5a4 2751 register_prefix = allow_naked_reg ? "" : "%";
252b5132
RH
2752}
2753
1efbbeb4
L
2754static void
2755set_intel_mnemonic (int mnemonic_flag)
2756{
e1d4d893 2757 intel_mnemonic = mnemonic_flag;
1efbbeb4
L
2758}
2759
db51cc60
L
2760static void
2761set_allow_index_reg (int flag)
2762{
2763 allow_index_reg = flag;
2764}
2765
cb19c032 2766static void
7bab8ab5 2767set_check (int what)
cb19c032 2768{
7bab8ab5
JB
2769 enum check_kind *kind;
2770 const char *str;
2771
2772 if (what)
2773 {
2774 kind = &operand_check;
2775 str = "operand";
2776 }
2777 else
2778 {
2779 kind = &sse_check;
2780 str = "sse";
2781 }
2782
cb19c032
L
2783 SKIP_WHITESPACE ();
2784
2785 if (!is_end_of_line[(unsigned char) *input_line_pointer])
2786 {
d02603dc
NC
2787 char *string;
2788 int e = get_symbol_name (&string);
cb19c032
L
2789
2790 if (strcmp (string, "none") == 0)
7bab8ab5 2791 *kind = check_none;
cb19c032 2792 else if (strcmp (string, "warning") == 0)
7bab8ab5 2793 *kind = check_warning;
cb19c032 2794 else if (strcmp (string, "error") == 0)
7bab8ab5 2795 *kind = check_error;
cb19c032 2796 else
7bab8ab5 2797 as_bad (_("bad argument to %s_check directive."), str);
d02603dc 2798 (void) restore_line_pointer (e);
cb19c032
L
2799 }
2800 else
7bab8ab5 2801 as_bad (_("missing argument for %s_check directive"), str);
cb19c032
L
2802
2803 demand_empty_rest_of_line ();
2804}
2805
8a9036a4
L
2806static void
2807check_cpu_arch_compatible (const char *name ATTRIBUTE_UNUSED,
1e9cc1c2 2808 i386_cpu_flags new_flag ATTRIBUTE_UNUSED)
8a9036a4
L
2809{
2810#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2811 static const char *arch;
2812
2813 /* Intel LIOM is only supported on ELF. */
2814 if (!IS_ELF)
2815 return;
2816
2817 if (!arch)
2818 {
2819 /* Use cpu_arch_name if it is set in md_parse_option. Otherwise
2820 use default_arch. */
2821 arch = cpu_arch_name;
2822 if (!arch)
2823 arch = default_arch;
2824 }
2825
81486035
L
2826 /* If we are targeting Intel MCU, we must enable it. */
2827 if (get_elf_backend_data (stdoutput)->elf_machine_code != EM_IAMCU
2828 || new_flag.bitfield.cpuiamcu)
2829 return;
2830
3632d14b 2831 /* If we are targeting Intel L1OM, we must enable it. */
8a9036a4 2832 if (get_elf_backend_data (stdoutput)->elf_machine_code != EM_L1OM
1e9cc1c2 2833 || new_flag.bitfield.cpul1om)
8a9036a4 2834 return;
76ba9986 2835
7a9068fe
L
2836 /* If we are targeting Intel K1OM, we must enable it. */
2837 if (get_elf_backend_data (stdoutput)->elf_machine_code != EM_K1OM
2838 || new_flag.bitfield.cpuk1om)
2839 return;
2840
8a9036a4
L
2841 as_bad (_("`%s' is not supported on `%s'"), name, arch);
2842#endif
2843}
2844
e413e4e9 2845static void
e3bb37b5 2846set_cpu_arch (int dummy ATTRIBUTE_UNUSED)
e413e4e9 2847{
47926f60 2848 SKIP_WHITESPACE ();
e413e4e9 2849
29b0f896 2850 if (!is_end_of_line[(unsigned char) *input_line_pointer])
e413e4e9 2851 {
d02603dc
NC
2852 char *string;
2853 int e = get_symbol_name (&string);
91d6fa6a 2854 unsigned int j;
40fb9820 2855 i386_cpu_flags flags;
e413e4e9 2856
91d6fa6a 2857 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
e413e4e9 2858 {
91d6fa6a 2859 if (strcmp (string, cpu_arch[j].name) == 0)
e413e4e9 2860 {
91d6fa6a 2861 check_cpu_arch_compatible (string, cpu_arch[j].flags);
8a9036a4 2862
5c6af06e
JB
2863 if (*string != '.')
2864 {
91d6fa6a 2865 cpu_arch_name = cpu_arch[j].name;
5c6af06e 2866 cpu_sub_arch_name = NULL;
91d6fa6a 2867 cpu_arch_flags = cpu_arch[j].flags;
40fb9820
L
2868 if (flag_code == CODE_64BIT)
2869 {
2870 cpu_arch_flags.bitfield.cpu64 = 1;
2871 cpu_arch_flags.bitfield.cpuno64 = 0;
2872 }
2873 else
2874 {
2875 cpu_arch_flags.bitfield.cpu64 = 0;
2876 cpu_arch_flags.bitfield.cpuno64 = 1;
2877 }
91d6fa6a
NC
2878 cpu_arch_isa = cpu_arch[j].type;
2879 cpu_arch_isa_flags = cpu_arch[j].flags;
ccc9c027
L
2880 if (!cpu_arch_tune_set)
2881 {
2882 cpu_arch_tune = cpu_arch_isa;
2883 cpu_arch_tune_flags = cpu_arch_isa_flags;
2884 }
5c6af06e
JB
2885 break;
2886 }
40fb9820 2887
293f5f65
L
2888 flags = cpu_flags_or (cpu_arch_flags,
2889 cpu_arch[j].flags);
81486035 2890
5b64d091 2891 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
5c6af06e 2892 {
6305a203
L
2893 if (cpu_sub_arch_name)
2894 {
2895 char *name = cpu_sub_arch_name;
2896 cpu_sub_arch_name = concat (name,
91d6fa6a 2897 cpu_arch[j].name,
1bf57e9f 2898 (const char *) NULL);
6305a203
L
2899 free (name);
2900 }
2901 else
91d6fa6a 2902 cpu_sub_arch_name = xstrdup (cpu_arch[j].name);
40fb9820 2903 cpu_arch_flags = flags;
a586129e 2904 cpu_arch_isa_flags = flags;
5c6af06e 2905 }
0089dace
L
2906 else
2907 cpu_arch_isa_flags
2908 = cpu_flags_or (cpu_arch_isa_flags,
2909 cpu_arch[j].flags);
d02603dc 2910 (void) restore_line_pointer (e);
5c6af06e
JB
2911 demand_empty_rest_of_line ();
2912 return;
e413e4e9
AM
2913 }
2914 }
293f5f65
L
2915
2916 if (*string == '.' && j >= ARRAY_SIZE (cpu_arch))
2917 {
33eaf5de 2918 /* Disable an ISA extension. */
293f5f65
L
2919 for (j = 0; j < ARRAY_SIZE (cpu_noarch); j++)
2920 if (strcmp (string + 1, cpu_noarch [j].name) == 0)
2921 {
2922 flags = cpu_flags_and_not (cpu_arch_flags,
2923 cpu_noarch[j].flags);
2924 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
2925 {
2926 if (cpu_sub_arch_name)
2927 {
2928 char *name = cpu_sub_arch_name;
2929 cpu_sub_arch_name = concat (name, string,
2930 (const char *) NULL);
2931 free (name);
2932 }
2933 else
2934 cpu_sub_arch_name = xstrdup (string);
2935 cpu_arch_flags = flags;
2936 cpu_arch_isa_flags = flags;
2937 }
2938 (void) restore_line_pointer (e);
2939 demand_empty_rest_of_line ();
2940 return;
2941 }
2942
2943 j = ARRAY_SIZE (cpu_arch);
2944 }
2945
91d6fa6a 2946 if (j >= ARRAY_SIZE (cpu_arch))
e413e4e9
AM
2947 as_bad (_("no such architecture: `%s'"), string);
2948
2949 *input_line_pointer = e;
2950 }
2951 else
2952 as_bad (_("missing cpu architecture"));
2953
fddf5b5b
AM
2954 no_cond_jump_promotion = 0;
2955 if (*input_line_pointer == ','
29b0f896 2956 && !is_end_of_line[(unsigned char) input_line_pointer[1]])
fddf5b5b 2957 {
d02603dc
NC
2958 char *string;
2959 char e;
2960
2961 ++input_line_pointer;
2962 e = get_symbol_name (&string);
fddf5b5b
AM
2963
2964 if (strcmp (string, "nojumps") == 0)
2965 no_cond_jump_promotion = 1;
2966 else if (strcmp (string, "jumps") == 0)
2967 ;
2968 else
2969 as_bad (_("no such architecture modifier: `%s'"), string);
2970
d02603dc 2971 (void) restore_line_pointer (e);
fddf5b5b
AM
2972 }
2973
e413e4e9
AM
2974 demand_empty_rest_of_line ();
2975}
2976
8a9036a4
L
2977enum bfd_architecture
2978i386_arch (void)
2979{
3632d14b 2980 if (cpu_arch_isa == PROCESSOR_L1OM)
8a9036a4
L
2981 {
2982 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2983 || flag_code != CODE_64BIT)
2984 as_fatal (_("Intel L1OM is 64bit ELF only"));
2985 return bfd_arch_l1om;
2986 }
7a9068fe
L
2987 else if (cpu_arch_isa == PROCESSOR_K1OM)
2988 {
2989 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2990 || flag_code != CODE_64BIT)
2991 as_fatal (_("Intel K1OM is 64bit ELF only"));
2992 return bfd_arch_k1om;
2993 }
81486035
L
2994 else if (cpu_arch_isa == PROCESSOR_IAMCU)
2995 {
2996 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2997 || flag_code == CODE_64BIT)
2998 as_fatal (_("Intel MCU is 32bit ELF only"));
2999 return bfd_arch_iamcu;
3000 }
8a9036a4
L
3001 else
3002 return bfd_arch_i386;
3003}
3004
b9d79e03 3005unsigned long
7016a5d5 3006i386_mach (void)
b9d79e03 3007{
351f65ca 3008 if (!strncmp (default_arch, "x86_64", 6))
8a9036a4 3009 {
3632d14b 3010 if (cpu_arch_isa == PROCESSOR_L1OM)
8a9036a4 3011 {
351f65ca
L
3012 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
3013 || default_arch[6] != '\0')
8a9036a4
L
3014 as_fatal (_("Intel L1OM is 64bit ELF only"));
3015 return bfd_mach_l1om;
3016 }
7a9068fe
L
3017 else if (cpu_arch_isa == PROCESSOR_K1OM)
3018 {
3019 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
3020 || default_arch[6] != '\0')
3021 as_fatal (_("Intel K1OM is 64bit ELF only"));
3022 return bfd_mach_k1om;
3023 }
351f65ca 3024 else if (default_arch[6] == '\0')
8a9036a4 3025 return bfd_mach_x86_64;
351f65ca
L
3026 else
3027 return bfd_mach_x64_32;
8a9036a4 3028 }
5197d474
L
3029 else if (!strcmp (default_arch, "i386")
3030 || !strcmp (default_arch, "iamcu"))
81486035
L
3031 {
3032 if (cpu_arch_isa == PROCESSOR_IAMCU)
3033 {
3034 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
3035 as_fatal (_("Intel MCU is 32bit ELF only"));
3036 return bfd_mach_i386_iamcu;
3037 }
3038 else
3039 return bfd_mach_i386_i386;
3040 }
b9d79e03 3041 else
2b5d6a91 3042 as_fatal (_("unknown architecture"));
b9d79e03 3043}
b9d79e03 3044\f
252b5132 3045void
7016a5d5 3046md_begin (void)
252b5132 3047{
86fa6981
L
3048 /* Support pseudo prefixes like {disp32}. */
3049 lex_type ['{'] = LEX_BEGIN_NAME;
3050
47926f60 3051 /* Initialize op_hash hash table. */
629310ab 3052 op_hash = str_htab_create ();
252b5132
RH
3053
3054 {
d3ce72d0 3055 const insn_template *optab;
29b0f896 3056 templates *core_optab;
252b5132 3057
47926f60
KH
3058 /* Setup for loop. */
3059 optab = i386_optab;
add39d23 3060 core_optab = XNEW (templates);
252b5132
RH
3061 core_optab->start = optab;
3062
3063 while (1)
3064 {
3065 ++optab;
3066 if (optab->name == NULL
3067 || strcmp (optab->name, (optab - 1)->name) != 0)
3068 {
3069 /* different name --> ship out current template list;
47926f60 3070 add to hash table; & begin anew. */
252b5132 3071 core_optab->end = optab;
fe0e921f
AM
3072 if (str_hash_insert (op_hash, (optab - 1)->name, core_optab, 0))
3073 as_fatal (_("duplicate %s"), (optab - 1)->name);
3074
252b5132
RH
3075 if (optab->name == NULL)
3076 break;
add39d23 3077 core_optab = XNEW (templates);
252b5132
RH
3078 core_optab->start = optab;
3079 }
3080 }
3081 }
3082
47926f60 3083 /* Initialize reg_hash hash table. */
629310ab 3084 reg_hash = str_htab_create ();
252b5132 3085 {
29b0f896 3086 const reg_entry *regtab;
c3fe08fa 3087 unsigned int regtab_size = i386_regtab_size;
252b5132 3088
c3fe08fa 3089 for (regtab = i386_regtab; regtab_size--; regtab++)
6225c532 3090 {
6288d05f
JB
3091 switch (regtab->reg_type.bitfield.class)
3092 {
3093 case Reg:
3094 if (regtab->reg_type.bitfield.tbyte)
3095 {
3096 /* There's no point inserting st(<N>) in the hash table, as
3097 parentheses aren't included in register_chars[] anyway. */
3098 if (regtab->reg_type.bitfield.instance != Accum)
3099 continue;
3100 reg_st0 = regtab;
3101 }
3102 break;
3103
3104 case RegMask:
3105 if (!regtab->reg_num)
3106 reg_k0 = regtab;
3107 break;
3108 }
3109
6225c532
JB
3110 if (str_hash_insert (reg_hash, regtab->reg_name, regtab, 0) != NULL)
3111 as_fatal (_("duplicate %s"), regtab->reg_name);
6225c532 3112 }
252b5132
RH
3113 }
3114
47926f60 3115 /* Fill in lexical tables: mnemonic_chars, operand_chars. */
252b5132 3116 {
29b0f896
AM
3117 int c;
3118 char *p;
252b5132
RH
3119
3120 for (c = 0; c < 256; c++)
3121 {
3882b010 3122 if (ISDIGIT (c))
252b5132
RH
3123 {
3124 digit_chars[c] = c;
3125 mnemonic_chars[c] = c;
3126 register_chars[c] = c;
3127 operand_chars[c] = c;
3128 }
3882b010 3129 else if (ISLOWER (c))
252b5132
RH
3130 {
3131 mnemonic_chars[c] = c;
3132 register_chars[c] = c;
3133 operand_chars[c] = c;
3134 }
3882b010 3135 else if (ISUPPER (c))
252b5132 3136 {
3882b010 3137 mnemonic_chars[c] = TOLOWER (c);
252b5132
RH
3138 register_chars[c] = mnemonic_chars[c];
3139 operand_chars[c] = c;
3140 }
43234a1e 3141 else if (c == '{' || c == '}')
86fa6981
L
3142 {
3143 mnemonic_chars[c] = c;
3144 operand_chars[c] = c;
3145 }
b3983e5f
JB
3146#ifdef SVR4_COMMENT_CHARS
3147 else if (c == '\\' && strchr (i386_comment_chars, '/'))
3148 operand_chars[c] = c;
3149#endif
252b5132 3150
3882b010 3151 if (ISALPHA (c) || ISDIGIT (c))
252b5132
RH
3152 identifier_chars[c] = c;
3153 else if (c >= 128)
3154 {
3155 identifier_chars[c] = c;
3156 operand_chars[c] = c;
3157 }
3158 }
3159
3160#ifdef LEX_AT
3161 identifier_chars['@'] = '@';
32137342
NC
3162#endif
3163#ifdef LEX_QM
3164 identifier_chars['?'] = '?';
3165 operand_chars['?'] = '?';
252b5132 3166#endif
252b5132 3167 digit_chars['-'] = '-';
c0f3af97 3168 mnemonic_chars['_'] = '_';
791fe849 3169 mnemonic_chars['-'] = '-';
0003779b 3170 mnemonic_chars['.'] = '.';
252b5132
RH
3171 identifier_chars['_'] = '_';
3172 identifier_chars['.'] = '.';
3173
3174 for (p = operand_special_chars; *p != '\0'; p++)
3175 operand_chars[(unsigned char) *p] = *p;
3176 }
3177
a4447b93
RH
3178 if (flag_code == CODE_64BIT)
3179 {
ca19b261
KT
3180#if defined (OBJ_COFF) && defined (TE_PE)
3181 x86_dwarf2_return_column = (OUTPUT_FLAVOR == bfd_target_coff_flavour
3182 ? 32 : 16);
3183#else
a4447b93 3184 x86_dwarf2_return_column = 16;
ca19b261 3185#endif
61ff971f 3186 x86_cie_data_alignment = -8;
a4447b93
RH
3187 }
3188 else
3189 {
3190 x86_dwarf2_return_column = 8;
3191 x86_cie_data_alignment = -4;
3192 }
e379e5f3
L
3193
3194 /* NB: FUSED_JCC_PADDING frag must have sufficient room so that it
3195 can be turned into BRANCH_PREFIX frag. */
3196 if (align_branch_prefix_size > MAX_FUSED_JCC_PADDING_SIZE)
3197 abort ();
252b5132
RH
3198}
3199
3200void
e3bb37b5 3201i386_print_statistics (FILE *file)
252b5132 3202{
629310ab
ML
3203 htab_print_statistics (file, "i386 opcode", op_hash);
3204 htab_print_statistics (file, "i386 register", reg_hash);
252b5132
RH
3205}
3206\f
252b5132
RH
3207#ifdef DEBUG386
3208
ce8a8b2f 3209/* Debugging routines for md_assemble. */
d3ce72d0 3210static void pte (insn_template *);
40fb9820 3211static void pt (i386_operand_type);
e3bb37b5
L
3212static void pe (expressionS *);
3213static void ps (symbolS *);
252b5132
RH
3214
3215static void
2c703856 3216pi (const char *line, i386_insn *x)
252b5132 3217{
09137c09 3218 unsigned int j;
252b5132
RH
3219
3220 fprintf (stdout, "%s: template ", line);
3221 pte (&x->tm);
09f131f2
JH
3222 fprintf (stdout, " address: base %s index %s scale %x\n",
3223 x->base_reg ? x->base_reg->reg_name : "none",
3224 x->index_reg ? x->index_reg->reg_name : "none",
3225 x->log2_scale_factor);
3226 fprintf (stdout, " modrm: mode %x reg %x reg/mem %x\n",
252b5132 3227 x->rm.mode, x->rm.reg, x->rm.regmem);
09f131f2
JH
3228 fprintf (stdout, " sib: base %x index %x scale %x\n",
3229 x->sib.base, x->sib.index, x->sib.scale);
3230 fprintf (stdout, " rex: 64bit %x extX %x extY %x extZ %x\n",
161a04f6
L
3231 (x->rex & REX_W) != 0,
3232 (x->rex & REX_R) != 0,
3233 (x->rex & REX_X) != 0,
3234 (x->rex & REX_B) != 0);
09137c09 3235 for (j = 0; j < x->operands; j++)
252b5132 3236 {
09137c09
SP
3237 fprintf (stdout, " #%d: ", j + 1);
3238 pt (x->types[j]);
252b5132 3239 fprintf (stdout, "\n");
bab6aec1 3240 if (x->types[j].bitfield.class == Reg
3528c362
JB
3241 || x->types[j].bitfield.class == RegMMX
3242 || x->types[j].bitfield.class == RegSIMD
dd6b8a0b 3243 || x->types[j].bitfield.class == RegMask
00cee14f 3244 || x->types[j].bitfield.class == SReg
4a5c67ed
JB
3245 || x->types[j].bitfield.class == RegCR
3246 || x->types[j].bitfield.class == RegDR
dd6b8a0b
JB
3247 || x->types[j].bitfield.class == RegTR
3248 || x->types[j].bitfield.class == RegBND)
09137c09
SP
3249 fprintf (stdout, "%s\n", x->op[j].regs->reg_name);
3250 if (operand_type_check (x->types[j], imm))
3251 pe (x->op[j].imms);
3252 if (operand_type_check (x->types[j], disp))
3253 pe (x->op[j].disps);
252b5132
RH
3254 }
3255}
3256
3257static void
d3ce72d0 3258pte (insn_template *t)
252b5132 3259{
b933fa4b 3260 static const unsigned char opc_pfx[] = { 0, 0x66, 0xf3, 0xf2 };
441f6aca
JB
3261 static const char *const opc_spc[] = {
3262 NULL, "0f", "0f38", "0f3a", NULL, NULL, NULL, NULL,
3263 "XOP08", "XOP09", "XOP0A",
3264 };
09137c09 3265 unsigned int j;
441f6aca 3266
252b5132 3267 fprintf (stdout, " %d operands ", t->operands);
441f6aca
JB
3268 if (opc_pfx[t->opcode_modifier.opcodeprefix])
3269 fprintf (stdout, "pfx %x ", opc_pfx[t->opcode_modifier.opcodeprefix]);
3270 if (opc_spc[t->opcode_modifier.opcodespace])
3271 fprintf (stdout, "space %s ", opc_spc[t->opcode_modifier.opcodespace]);
47926f60 3272 fprintf (stdout, "opcode %x ", t->base_opcode);
252b5132
RH
3273 if (t->extension_opcode != None)
3274 fprintf (stdout, "ext %x ", t->extension_opcode);
40fb9820 3275 if (t->opcode_modifier.d)
252b5132 3276 fprintf (stdout, "D");
40fb9820 3277 if (t->opcode_modifier.w)
252b5132
RH
3278 fprintf (stdout, "W");
3279 fprintf (stdout, "\n");
09137c09 3280 for (j = 0; j < t->operands; j++)
252b5132 3281 {
09137c09
SP
3282 fprintf (stdout, " #%d type ", j + 1);
3283 pt (t->operand_types[j]);
252b5132
RH
3284 fprintf (stdout, "\n");
3285 }
3286}
3287
3288static void
e3bb37b5 3289pe (expressionS *e)
252b5132 3290{
24eab124 3291 fprintf (stdout, " operation %d\n", e->X_op);
b77ad1d4
AM
3292 fprintf (stdout, " add_number %ld (%lx)\n",
3293 (long) e->X_add_number, (long) e->X_add_number);
252b5132
RH
3294 if (e->X_add_symbol)
3295 {
3296 fprintf (stdout, " add_symbol ");
3297 ps (e->X_add_symbol);
3298 fprintf (stdout, "\n");
3299 }
3300 if (e->X_op_symbol)
3301 {
3302 fprintf (stdout, " op_symbol ");
3303 ps (e->X_op_symbol);
3304 fprintf (stdout, "\n");
3305 }
3306}
3307
3308static void
e3bb37b5 3309ps (symbolS *s)
252b5132
RH
3310{
3311 fprintf (stdout, "%s type %s%s",
3312 S_GET_NAME (s),
3313 S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
3314 segment_name (S_GET_SEGMENT (s)));
3315}
3316
7b81dfbb 3317static struct type_name
252b5132 3318 {
40fb9820
L
3319 i386_operand_type mask;
3320 const char *name;
252b5132 3321 }
7b81dfbb 3322const type_names[] =
252b5132 3323{
40fb9820
L
3324 { OPERAND_TYPE_REG8, "r8" },
3325 { OPERAND_TYPE_REG16, "r16" },
3326 { OPERAND_TYPE_REG32, "r32" },
3327 { OPERAND_TYPE_REG64, "r64" },
2c703856
JB
3328 { OPERAND_TYPE_ACC8, "acc8" },
3329 { OPERAND_TYPE_ACC16, "acc16" },
3330 { OPERAND_TYPE_ACC32, "acc32" },
3331 { OPERAND_TYPE_ACC64, "acc64" },
40fb9820
L
3332 { OPERAND_TYPE_IMM8, "i8" },
3333 { OPERAND_TYPE_IMM8, "i8s" },
3334 { OPERAND_TYPE_IMM16, "i16" },
3335 { OPERAND_TYPE_IMM32, "i32" },
3336 { OPERAND_TYPE_IMM32S, "i32s" },
3337 { OPERAND_TYPE_IMM64, "i64" },
3338 { OPERAND_TYPE_IMM1, "i1" },
3339 { OPERAND_TYPE_BASEINDEX, "BaseIndex" },
3340 { OPERAND_TYPE_DISP8, "d8" },
3341 { OPERAND_TYPE_DISP16, "d16" },
3342 { OPERAND_TYPE_DISP32, "d32" },
3343 { OPERAND_TYPE_DISP32S, "d32s" },
3344 { OPERAND_TYPE_DISP64, "d64" },
3345 { OPERAND_TYPE_INOUTPORTREG, "InOutPortReg" },
3346 { OPERAND_TYPE_SHIFTCOUNT, "ShiftCount" },
3347 { OPERAND_TYPE_CONTROL, "control reg" },
3348 { OPERAND_TYPE_TEST, "test reg" },
3349 { OPERAND_TYPE_DEBUG, "debug reg" },
3350 { OPERAND_TYPE_FLOATREG, "FReg" },
3351 { OPERAND_TYPE_FLOATACC, "FAcc" },
21df382b 3352 { OPERAND_TYPE_SREG, "SReg" },
40fb9820
L
3353 { OPERAND_TYPE_REGMMX, "rMMX" },
3354 { OPERAND_TYPE_REGXMM, "rXMM" },
0349dc08 3355 { OPERAND_TYPE_REGYMM, "rYMM" },
43234a1e 3356 { OPERAND_TYPE_REGZMM, "rZMM" },
260cd341 3357 { OPERAND_TYPE_REGTMM, "rTMM" },
43234a1e 3358 { OPERAND_TYPE_REGMASK, "Mask reg" },
252b5132
RH
3359};
3360
3361static void
40fb9820 3362pt (i386_operand_type t)
252b5132 3363{
40fb9820 3364 unsigned int j;
c6fb90c8 3365 i386_operand_type a;
252b5132 3366
40fb9820 3367 for (j = 0; j < ARRAY_SIZE (type_names); j++)
c6fb90c8
L
3368 {
3369 a = operand_type_and (t, type_names[j].mask);
2c703856 3370 if (operand_type_equal (&a, &type_names[j].mask))
c6fb90c8
L
3371 fprintf (stdout, "%s, ", type_names[j].name);
3372 }
252b5132
RH
3373 fflush (stdout);
3374}
3375
3376#endif /* DEBUG386 */
3377\f
252b5132 3378static bfd_reloc_code_real_type
3956db08 3379reloc (unsigned int size,
64e74474
AM
3380 int pcrel,
3381 int sign,
3382 bfd_reloc_code_real_type other)
252b5132 3383{
47926f60 3384 if (other != NO_RELOC)
3956db08 3385 {
91d6fa6a 3386 reloc_howto_type *rel;
3956db08
JB
3387
3388 if (size == 8)
3389 switch (other)
3390 {
64e74474
AM
3391 case BFD_RELOC_X86_64_GOT32:
3392 return BFD_RELOC_X86_64_GOT64;
3393 break;
553d1284
L
3394 case BFD_RELOC_X86_64_GOTPLT64:
3395 return BFD_RELOC_X86_64_GOTPLT64;
3396 break;
64e74474
AM
3397 case BFD_RELOC_X86_64_PLTOFF64:
3398 return BFD_RELOC_X86_64_PLTOFF64;
3399 break;
3400 case BFD_RELOC_X86_64_GOTPC32:
3401 other = BFD_RELOC_X86_64_GOTPC64;
3402 break;
3403 case BFD_RELOC_X86_64_GOTPCREL:
3404 other = BFD_RELOC_X86_64_GOTPCREL64;
3405 break;
3406 case BFD_RELOC_X86_64_TPOFF32:
3407 other = BFD_RELOC_X86_64_TPOFF64;
3408 break;
3409 case BFD_RELOC_X86_64_DTPOFF32:
3410 other = BFD_RELOC_X86_64_DTPOFF64;
3411 break;
3412 default:
3413 break;
3956db08 3414 }
e05278af 3415
8ce3d284 3416#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
3417 if (other == BFD_RELOC_SIZE32)
3418 {
3419 if (size == 8)
1ab668bf 3420 other = BFD_RELOC_SIZE64;
8fd4256d 3421 if (pcrel)
1ab668bf
AM
3422 {
3423 as_bad (_("there are no pc-relative size relocations"));
3424 return NO_RELOC;
3425 }
8fd4256d 3426 }
8ce3d284 3427#endif
8fd4256d 3428
e05278af 3429 /* Sign-checking 4-byte relocations in 16-/32-bit code is pointless. */
f2d8a97c 3430 if (size == 4 && (flag_code != CODE_64BIT || disallow_64bit_reloc))
e05278af
JB
3431 sign = -1;
3432
91d6fa6a
NC
3433 rel = bfd_reloc_type_lookup (stdoutput, other);
3434 if (!rel)
3956db08 3435 as_bad (_("unknown relocation (%u)"), other);
91d6fa6a 3436 else if (size != bfd_get_reloc_size (rel))
3956db08 3437 as_bad (_("%u-byte relocation cannot be applied to %u-byte field"),
91d6fa6a 3438 bfd_get_reloc_size (rel),
3956db08 3439 size);
91d6fa6a 3440 else if (pcrel && !rel->pc_relative)
3956db08 3441 as_bad (_("non-pc-relative relocation for pc-relative field"));
91d6fa6a 3442 else if ((rel->complain_on_overflow == complain_overflow_signed
3956db08 3443 && !sign)
91d6fa6a 3444 || (rel->complain_on_overflow == complain_overflow_unsigned
64e74474 3445 && sign > 0))
3956db08
JB
3446 as_bad (_("relocated field and relocation type differ in signedness"));
3447 else
3448 return other;
3449 return NO_RELOC;
3450 }
252b5132
RH
3451
3452 if (pcrel)
3453 {
3e73aa7c 3454 if (!sign)
3956db08 3455 as_bad (_("there are no unsigned pc-relative relocations"));
252b5132
RH
3456 switch (size)
3457 {
3458 case 1: return BFD_RELOC_8_PCREL;
3459 case 2: return BFD_RELOC_16_PCREL;
d258b828 3460 case 4: return BFD_RELOC_32_PCREL;
d6ab8113 3461 case 8: return BFD_RELOC_64_PCREL;
252b5132 3462 }
3956db08 3463 as_bad (_("cannot do %u byte pc-relative relocation"), size);
252b5132
RH
3464 }
3465 else
3466 {
3956db08 3467 if (sign > 0)
e5cb08ac 3468 switch (size)
3e73aa7c
JH
3469 {
3470 case 4: return BFD_RELOC_X86_64_32S;
3471 }
3472 else
3473 switch (size)
3474 {
3475 case 1: return BFD_RELOC_8;
3476 case 2: return BFD_RELOC_16;
3477 case 4: return BFD_RELOC_32;
3478 case 8: return BFD_RELOC_64;
3479 }
3956db08
JB
3480 as_bad (_("cannot do %s %u byte relocation"),
3481 sign > 0 ? "signed" : "unsigned", size);
252b5132
RH
3482 }
3483
0cc9e1d3 3484 return NO_RELOC;
252b5132
RH
3485}
3486
47926f60
KH
3487/* Here we decide which fixups can be adjusted to make them relative to
3488 the beginning of the section instead of the symbol. Basically we need
3489 to make sure that the dynamic relocations are done correctly, so in
3490 some cases we force the original symbol to be used. */
3491
252b5132 3492int
e3bb37b5 3493tc_i386_fix_adjustable (fixS *fixP ATTRIBUTE_UNUSED)
252b5132 3494{
6d249963 3495#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 3496 if (!IS_ELF)
31312f95
AM
3497 return 1;
3498
a161fe53
AM
3499 /* Don't adjust pc-relative references to merge sections in 64-bit
3500 mode. */
3501 if (use_rela_relocations
3502 && (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0
3503 && fixP->fx_pcrel)
252b5132 3504 return 0;
31312f95 3505
8d01d9a9
AJ
3506 /* The x86_64 GOTPCREL are represented as 32bit PCrel relocations
3507 and changed later by validate_fix. */
3508 if (GOT_symbol && fixP->fx_subsy == GOT_symbol
3509 && fixP->fx_r_type == BFD_RELOC_32_PCREL)
3510 return 0;
3511
8fd4256d
L
3512 /* Adjust_reloc_syms doesn't know about the GOT. Need to keep symbol
3513 for size relocations. */
3514 if (fixP->fx_r_type == BFD_RELOC_SIZE32
3515 || fixP->fx_r_type == BFD_RELOC_SIZE64
3516 || fixP->fx_r_type == BFD_RELOC_386_GOTOFF
252b5132 3517 || fixP->fx_r_type == BFD_RELOC_386_GOT32
02a86693 3518 || fixP->fx_r_type == BFD_RELOC_386_GOT32X
13ae64f3
JJ
3519 || fixP->fx_r_type == BFD_RELOC_386_TLS_GD
3520 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDM
3521 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDO_32
3522 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE_32
37e55690
JJ
3523 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE
3524 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTIE
13ae64f3
JJ
3525 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE_32
3526 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE
67a4f2b7
AO
3527 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTDESC
3528 || fixP->fx_r_type == BFD_RELOC_386_TLS_DESC_CALL
3e73aa7c 3529 || fixP->fx_r_type == BFD_RELOC_X86_64_GOT32
80b3ee89 3530 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCREL
56ceb5b5
L
3531 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCRELX
3532 || fixP->fx_r_type == BFD_RELOC_X86_64_REX_GOTPCRELX
bffbf940
JJ
3533 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSGD
3534 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSLD
3535 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF32
d6ab8113 3536 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF64
bffbf940
JJ
3537 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF
3538 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF32
d6ab8113
JB
3539 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF64
3540 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTOFF64
67a4f2b7
AO
3541 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
3542 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSDESC_CALL
252b5132
RH
3543 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3544 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3545 return 0;
31312f95 3546#endif
252b5132
RH
3547 return 1;
3548}
252b5132 3549
b4cac588 3550static int
e3bb37b5 3551intel_float_operand (const char *mnemonic)
252b5132 3552{
9306ca4a
JB
3553 /* Note that the value returned is meaningful only for opcodes with (memory)
3554 operands, hence the code here is free to improperly handle opcodes that
3555 have no operands (for better performance and smaller code). */
3556
3557 if (mnemonic[0] != 'f')
3558 return 0; /* non-math */
3559
3560 switch (mnemonic[1])
3561 {
3562 /* fclex, fdecstp, fdisi, femms, feni, fincstp, finit, fsetpm, and
3563 the fs segment override prefix not currently handled because no
3564 call path can make opcodes without operands get here */
3565 case 'i':
3566 return 2 /* integer op */;
3567 case 'l':
3568 if (mnemonic[2] == 'd' && (mnemonic[3] == 'c' || mnemonic[3] == 'e'))
3569 return 3; /* fldcw/fldenv */
3570 break;
3571 case 'n':
3572 if (mnemonic[2] != 'o' /* fnop */)
3573 return 3; /* non-waiting control op */
3574 break;
3575 case 'r':
3576 if (mnemonic[2] == 's')
3577 return 3; /* frstor/frstpm */
3578 break;
3579 case 's':
3580 if (mnemonic[2] == 'a')
3581 return 3; /* fsave */
3582 if (mnemonic[2] == 't')
3583 {
3584 switch (mnemonic[3])
3585 {
3586 case 'c': /* fstcw */
3587 case 'd': /* fstdw */
3588 case 'e': /* fstenv */
3589 case 's': /* fsts[gw] */
3590 return 3;
3591 }
3592 }
3593 break;
3594 case 'x':
3595 if (mnemonic[2] == 'r' || mnemonic[2] == 's')
3596 return 0; /* fxsave/fxrstor are not really math ops */
3597 break;
3598 }
252b5132 3599
9306ca4a 3600 return 1;
252b5132
RH
3601}
3602
9a182d04
JB
3603static INLINE void
3604install_template (const insn_template *t)
3605{
3606 unsigned int l;
3607
3608 i.tm = *t;
3609
3610 /* Note that for pseudo prefixes this produces a length of 1. But for them
3611 the length isn't interesting at all. */
3612 for (l = 1; l < 4; ++l)
3613 if (!(t->base_opcode >> (8 * l)))
3614 break;
3615
3616 i.opcode_length = l;
3617}
3618
c0f3af97
L
3619/* Build the VEX prefix. */
3620
3621static void
d3ce72d0 3622build_vex_prefix (const insn_template *t)
c0f3af97
L
3623{
3624 unsigned int register_specifier;
c0f3af97 3625 unsigned int vector_length;
03751133 3626 unsigned int w;
c0f3af97
L
3627
3628 /* Check register specifier. */
3629 if (i.vex.register_specifier)
43234a1e
L
3630 {
3631 register_specifier =
3632 ~register_number (i.vex.register_specifier) & 0xf;
3633 gas_assert ((i.vex.register_specifier->reg_flags & RegVRex) == 0);
3634 }
c0f3af97
L
3635 else
3636 register_specifier = 0xf;
3637
79f0fa25
L
3638 /* Use 2-byte VEX prefix by swapping destination and source operand
3639 if there are more than 1 register operand. */
3640 if (i.reg_operands > 1
3641 && i.vec_encoding != vex_encoding_vex3
86fa6981 3642 && i.dir_encoding == dir_encoding_default
fa99fab2 3643 && i.operands == i.reg_operands
dbbc8b7e 3644 && operand_type_equal (&i.types[0], &i.types[i.operands - 1])
441f6aca 3645 && i.tm.opcode_modifier.opcodespace == SPACE_0F
dbbc8b7e 3646 && (i.tm.opcode_modifier.load || i.tm.opcode_modifier.d)
fa99fab2
L
3647 && i.rex == REX_B)
3648 {
3649 unsigned int xchg = i.operands - 1;
3650 union i386_op temp_op;
3651 i386_operand_type temp_type;
3652
3653 temp_type = i.types[xchg];
3654 i.types[xchg] = i.types[0];
3655 i.types[0] = temp_type;
3656 temp_op = i.op[xchg];
3657 i.op[xchg] = i.op[0];
3658 i.op[0] = temp_op;
3659
9c2799c2 3660 gas_assert (i.rm.mode == 3);
fa99fab2
L
3661
3662 i.rex = REX_R;
3663 xchg = i.rm.regmem;
3664 i.rm.regmem = i.rm.reg;
3665 i.rm.reg = xchg;
3666
dbbc8b7e
JB
3667 if (i.tm.opcode_modifier.d)
3668 i.tm.base_opcode ^= (i.tm.base_opcode & 0xee) != 0x6e
3669 ? Opcode_SIMD_FloatD : Opcode_SIMD_IntD;
3670 else /* Use the next insn. */
9a182d04 3671 install_template (&t[1]);
fa99fab2
L
3672 }
3673
79dec6b7
JB
3674 /* Use 2-byte VEX prefix by swapping commutative source operands if there
3675 are no memory operands and at least 3 register ones. */
3676 if (i.reg_operands >= 3
3677 && i.vec_encoding != vex_encoding_vex3
3678 && i.reg_operands == i.operands - i.imm_operands
3679 && i.tm.opcode_modifier.vex
3680 && i.tm.opcode_modifier.commutative
3681 && (i.tm.opcode_modifier.sse2avx || optimize > 1)
3682 && i.rex == REX_B
3683 && i.vex.register_specifier
3684 && !(i.vex.register_specifier->reg_flags & RegRex))
3685 {
3686 unsigned int xchg = i.operands - i.reg_operands;
3687 union i386_op temp_op;
3688 i386_operand_type temp_type;
3689
441f6aca 3690 gas_assert (i.tm.opcode_modifier.opcodespace == SPACE_0F);
79dec6b7
JB
3691 gas_assert (!i.tm.opcode_modifier.sae);
3692 gas_assert (operand_type_equal (&i.types[i.operands - 2],
3693 &i.types[i.operands - 3]));
3694 gas_assert (i.rm.mode == 3);
3695
3696 temp_type = i.types[xchg];
3697 i.types[xchg] = i.types[xchg + 1];
3698 i.types[xchg + 1] = temp_type;
3699 temp_op = i.op[xchg];
3700 i.op[xchg] = i.op[xchg + 1];
3701 i.op[xchg + 1] = temp_op;
3702
3703 i.rex = 0;
3704 xchg = i.rm.regmem | 8;
3705 i.rm.regmem = ~register_specifier & 0xf;
3706 gas_assert (!(i.rm.regmem & 8));
3707 i.vex.register_specifier += xchg - i.rm.regmem;
3708 register_specifier = ~xchg & 0xf;
3709 }
3710
539f890d
L
3711 if (i.tm.opcode_modifier.vex == VEXScalar)
3712 vector_length = avxscalar;
10c17abd
JB
3713 else if (i.tm.opcode_modifier.vex == VEX256)
3714 vector_length = 1;
539f890d 3715 else
10c17abd 3716 {
56522fc5 3717 unsigned int op;
10c17abd 3718
c7213af9
L
3719 /* Determine vector length from the last multi-length vector
3720 operand. */
10c17abd 3721 vector_length = 0;
56522fc5 3722 for (op = t->operands; op--;)
10c17abd
JB
3723 if (t->operand_types[op].bitfield.xmmword
3724 && t->operand_types[op].bitfield.ymmword
3725 && i.types[op].bitfield.ymmword)
3726 {
3727 vector_length = 1;
3728 break;
3729 }
3730 }
c0f3af97 3731
03751133
L
3732 /* Check the REX.W bit and VEXW. */
3733 if (i.tm.opcode_modifier.vexw == VEXWIG)
3734 w = (vexwig == vexw1 || (i.rex & REX_W)) ? 1 : 0;
3735 else if (i.tm.opcode_modifier.vexw)
3736 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3737 else
931d03b7 3738 w = (flag_code == CODE_64BIT ? i.rex & REX_W : vexwig == vexw1) ? 1 : 0;
03751133 3739
c0f3af97 3740 /* Use 2-byte VEX prefix if possible. */
03751133
L
3741 if (w == 0
3742 && i.vec_encoding != vex_encoding_vex3
441f6aca 3743 && i.tm.opcode_modifier.opcodespace == SPACE_0F
c0f3af97
L
3744 && (i.rex & (REX_W | REX_X | REX_B)) == 0)
3745 {
3746 /* 2-byte VEX prefix. */
3747 unsigned int r;
3748
3749 i.vex.length = 2;
3750 i.vex.bytes[0] = 0xc5;
3751
3752 /* Check the REX.R bit. */
3753 r = (i.rex & REX_R) ? 0 : 1;
3754 i.vex.bytes[1] = (r << 7
3755 | register_specifier << 3
3756 | vector_length << 2
35648716 3757 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3758 }
3759 else
3760 {
3761 /* 3-byte VEX prefix. */
f88c9eb0 3762 i.vex.length = 3;
f88c9eb0 3763
441f6aca 3764 switch (i.tm.opcode_modifier.opcodespace)
5dd85c99 3765 {
441f6aca
JB
3766 case SPACE_0F:
3767 case SPACE_0F38:
3768 case SPACE_0F3A:
80de6e00 3769 i.vex.bytes[0] = 0xc4;
7f399153 3770 break;
441f6aca
JB
3771 case SPACE_XOP08:
3772 case SPACE_XOP09:
3773 case SPACE_XOP0A:
f88c9eb0 3774 i.vex.bytes[0] = 0x8f;
7f399153
L
3775 break;
3776 default:
3777 abort ();
f88c9eb0 3778 }
c0f3af97 3779
c0f3af97
L
3780 /* The high 3 bits of the second VEX byte are 1's compliment
3781 of RXB bits from REX. */
441f6aca 3782 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | i.tm.opcode_modifier.opcodespace;
c0f3af97 3783
c0f3af97
L
3784 i.vex.bytes[2] = (w << 7
3785 | register_specifier << 3
3786 | vector_length << 2
35648716 3787 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3788 }
3789}
3790
e771e7c9
JB
3791static INLINE bfd_boolean
3792is_evex_encoding (const insn_template *t)
3793{
7091c612 3794 return t->opcode_modifier.evex || t->opcode_modifier.disp8memshift
e771e7c9 3795 || t->opcode_modifier.broadcast || t->opcode_modifier.masking
a80195f1 3796 || t->opcode_modifier.sae;
e771e7c9
JB
3797}
3798
7a8655d2
JB
3799static INLINE bfd_boolean
3800is_any_vex_encoding (const insn_template *t)
3801{
7b47a312 3802 return t->opcode_modifier.vex || is_evex_encoding (t);
7a8655d2
JB
3803}
3804
43234a1e
L
3805/* Build the EVEX prefix. */
3806
3807static void
3808build_evex_prefix (void)
3809{
35648716 3810 unsigned int register_specifier, w;
43234a1e
L
3811 rex_byte vrex_used = 0;
3812
3813 /* Check register specifier. */
3814 if (i.vex.register_specifier)
3815 {
3816 gas_assert ((i.vrex & REX_X) == 0);
3817
3818 register_specifier = i.vex.register_specifier->reg_num;
3819 if ((i.vex.register_specifier->reg_flags & RegRex))
3820 register_specifier += 8;
3821 /* The upper 16 registers are encoded in the fourth byte of the
3822 EVEX prefix. */
3823 if (!(i.vex.register_specifier->reg_flags & RegVRex))
3824 i.vex.bytes[3] = 0x8;
3825 register_specifier = ~register_specifier & 0xf;
3826 }
3827 else
3828 {
3829 register_specifier = 0xf;
3830
3831 /* Encode upper 16 vector index register in the fourth byte of
3832 the EVEX prefix. */
3833 if (!(i.vrex & REX_X))
3834 i.vex.bytes[3] = 0x8;
3835 else
3836 vrex_used |= REX_X;
3837 }
3838
43234a1e
L
3839 /* 4 byte EVEX prefix. */
3840 i.vex.length = 4;
3841 i.vex.bytes[0] = 0x62;
3842
43234a1e
L
3843 /* The high 3 bits of the second EVEX byte are 1's compliment of RXB
3844 bits from REX. */
441f6aca
JB
3845 gas_assert (i.tm.opcode_modifier.opcodespace >= SPACE_0F);
3846 gas_assert (i.tm.opcode_modifier.opcodespace <= SPACE_0F3A);
3847 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | i.tm.opcode_modifier.opcodespace;
43234a1e
L
3848
3849 /* The fifth bit of the second EVEX byte is 1's compliment of the
3850 REX_R bit in VREX. */
3851 if (!(i.vrex & REX_R))
3852 i.vex.bytes[1] |= 0x10;
3853 else
3854 vrex_used |= REX_R;
3855
3856 if ((i.reg_operands + i.imm_operands) == i.operands)
3857 {
3858 /* When all operands are registers, the REX_X bit in REX is not
3859 used. We reuse it to encode the upper 16 registers, which is
3860 indicated by the REX_B bit in VREX. The REX_X bit is encoded
3861 as 1's compliment. */
3862 if ((i.vrex & REX_B))
3863 {
3864 vrex_used |= REX_B;
3865 i.vex.bytes[1] &= ~0x40;
3866 }
3867 }
3868
3869 /* EVEX instructions shouldn't need the REX prefix. */
3870 i.vrex &= ~vrex_used;
3871 gas_assert (i.vrex == 0);
3872
6865c043
L
3873 /* Check the REX.W bit and VEXW. */
3874 if (i.tm.opcode_modifier.vexw == VEXWIG)
3875 w = (evexwig == evexw1 || (i.rex & REX_W)) ? 1 : 0;
3876 else if (i.tm.opcode_modifier.vexw)
3877 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3878 else
931d03b7 3879 w = (flag_code == CODE_64BIT ? i.rex & REX_W : evexwig == evexw1) ? 1 : 0;
43234a1e 3880
43234a1e 3881 /* The third byte of the EVEX prefix. */
35648716
JB
3882 i.vex.bytes[2] = ((w << 7)
3883 | (register_specifier << 3)
3884 | 4 /* Encode the U bit. */
3885 | i.tm.opcode_modifier.opcodeprefix);
43234a1e
L
3886
3887 /* The fourth byte of the EVEX prefix. */
3888 /* The zeroing-masking bit. */
6225c532 3889 if (i.mask.reg && i.mask.zeroing)
43234a1e
L
3890 i.vex.bytes[3] |= 0x80;
3891
3892 /* Don't always set the broadcast bit if there is no RC. */
ca5312a2 3893 if (i.rounding.type == rc_none)
43234a1e
L
3894 {
3895 /* Encode the vector length. */
3896 unsigned int vec_length;
3897
e771e7c9
JB
3898 if (!i.tm.opcode_modifier.evex
3899 || i.tm.opcode_modifier.evex == EVEXDYN)
3900 {
56522fc5 3901 unsigned int op;
e771e7c9 3902
c7213af9
L
3903 /* Determine vector length from the last multi-length vector
3904 operand. */
56522fc5 3905 for (op = i.operands; op--;)
e771e7c9
JB
3906 if (i.tm.operand_types[op].bitfield.xmmword
3907 + i.tm.operand_types[op].bitfield.ymmword
3908 + i.tm.operand_types[op].bitfield.zmmword > 1)
3909 {
3910 if (i.types[op].bitfield.zmmword)
c7213af9
L
3911 {
3912 i.tm.opcode_modifier.evex = EVEX512;
3913 break;
3914 }
e771e7c9 3915 else if (i.types[op].bitfield.ymmword)
c7213af9
L
3916 {
3917 i.tm.opcode_modifier.evex = EVEX256;
3918 break;
3919 }
e771e7c9 3920 else if (i.types[op].bitfield.xmmword)
c7213af9
L
3921 {
3922 i.tm.opcode_modifier.evex = EVEX128;
3923 break;
3924 }
5273a3cd 3925 else if (i.broadcast.type && op == i.broadcast.operand)
625cbd7a 3926 {
5273a3cd 3927 switch (i.broadcast.bytes)
625cbd7a
JB
3928 {
3929 case 64:
3930 i.tm.opcode_modifier.evex = EVEX512;
3931 break;
3932 case 32:
3933 i.tm.opcode_modifier.evex = EVEX256;
3934 break;
3935 case 16:
3936 i.tm.opcode_modifier.evex = EVEX128;
3937 break;
3938 default:
c7213af9 3939 abort ();
625cbd7a 3940 }
c7213af9 3941 break;
625cbd7a 3942 }
e771e7c9 3943 }
c7213af9 3944
56522fc5 3945 if (op >= MAX_OPERANDS)
c7213af9 3946 abort ();
e771e7c9
JB
3947 }
3948
43234a1e
L
3949 switch (i.tm.opcode_modifier.evex)
3950 {
3951 case EVEXLIG: /* LL' is ignored */
3952 vec_length = evexlig << 5;
3953 break;
3954 case EVEX128:
3955 vec_length = 0 << 5;
3956 break;
3957 case EVEX256:
3958 vec_length = 1 << 5;
3959 break;
3960 case EVEX512:
3961 vec_length = 2 << 5;
3962 break;
3963 default:
3964 abort ();
3965 break;
3966 }
3967 i.vex.bytes[3] |= vec_length;
3968 /* Encode the broadcast bit. */
5273a3cd 3969 if (i.broadcast.type)
43234a1e
L
3970 i.vex.bytes[3] |= 0x10;
3971 }
ca5312a2
JB
3972 else if (i.rounding.type != saeonly)
3973 i.vex.bytes[3] |= 0x10 | (i.rounding.type << 5);
43234a1e 3974 else
ca5312a2 3975 i.vex.bytes[3] |= 0x10 | (evexrcig << 5);
43234a1e 3976
6225c532
JB
3977 if (i.mask.reg)
3978 i.vex.bytes[3] |= i.mask.reg->reg_num;
43234a1e
L
3979}
3980
65da13b5
L
3981static void
3982process_immext (void)
3983{
3984 expressionS *exp;
3985
c0f3af97 3986 /* These AMD 3DNow! and SSE2 instructions have an opcode suffix
65da13b5
L
3987 which is coded in the same place as an 8-bit immediate field
3988 would be. Here we fake an 8-bit immediate operand from the
3989 opcode suffix stored in tm.extension_opcode.
3990
c1e679ec 3991 AVX instructions also use this encoding, for some of
c0f3af97 3992 3 argument instructions. */
65da13b5 3993
43234a1e 3994 gas_assert (i.imm_operands <= 1
7ab9ffdd 3995 && (i.operands <= 2
7a8655d2 3996 || (is_any_vex_encoding (&i.tm)
7ab9ffdd 3997 && i.operands <= 4)));
65da13b5
L
3998
3999 exp = &im_expressions[i.imm_operands++];
4000 i.op[i.operands].imms = exp;
4001 i.types[i.operands] = imm8;
4002 i.operands++;
4003 exp->X_op = O_constant;
4004 exp->X_add_number = i.tm.extension_opcode;
4005 i.tm.extension_opcode = None;
4006}
4007
42164a71
L
4008
4009static int
4010check_hle (void)
4011{
742732c7 4012 switch (i.tm.opcode_modifier.prefixok)
42164a71
L
4013 {
4014 default:
4015 abort ();
742732c7
JB
4016 case PrefixLock:
4017 case PrefixNone:
4018 case PrefixNoTrack:
4019 case PrefixRep:
165de32a
L
4020 as_bad (_("invalid instruction `%s' after `%s'"),
4021 i.tm.name, i.hle_prefix);
42164a71 4022 return 0;
742732c7 4023 case PrefixHLELock:
42164a71
L
4024 if (i.prefix[LOCK_PREFIX])
4025 return 1;
165de32a 4026 as_bad (_("missing `lock' with `%s'"), i.hle_prefix);
42164a71 4027 return 0;
742732c7 4028 case PrefixHLEAny:
42164a71 4029 return 1;
742732c7 4030 case PrefixHLERelease:
42164a71
L
4031 if (i.prefix[HLE_PREFIX] != XRELEASE_PREFIX_OPCODE)
4032 {
4033 as_bad (_("instruction `%s' after `xacquire' not allowed"),
4034 i.tm.name);
4035 return 0;
4036 }
8dc0818e 4037 if (i.mem_operands == 0 || !(i.flags[i.operands - 1] & Operand_Mem))
42164a71
L
4038 {
4039 as_bad (_("memory destination needed for instruction `%s'"
4040 " after `xrelease'"), i.tm.name);
4041 return 0;
4042 }
4043 return 1;
4044 }
4045}
4046
b6f8c7c4
L
4047/* Try the shortest encoding by shortening operand size. */
4048
4049static void
4050optimize_encoding (void)
4051{
a0a1771e 4052 unsigned int j;
b6f8c7c4
L
4053
4054 if (optimize_for_space
389d00a5 4055 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
b6f8c7c4
L
4056 && i.reg_operands == 1
4057 && i.imm_operands == 1
4058 && !i.types[1].bitfield.byte
4059 && i.op[0].imms->X_op == O_constant
4060 && fits_in_imm7 (i.op[0].imms->X_add_number)
72aea328 4061 && (i.tm.base_opcode == 0xa8
b6f8c7c4
L
4062 || (i.tm.base_opcode == 0xf6
4063 && i.tm.extension_opcode == 0x0)))
4064 {
4065 /* Optimize: -Os:
4066 test $imm7, %r64/%r32/%r16 -> test $imm7, %r8
4067 */
4068 unsigned int base_regnum = i.op[1].regs->reg_num;
4069 if (flag_code == CODE_64BIT || base_regnum < 4)
4070 {
4071 i.types[1].bitfield.byte = 1;
4072 /* Ignore the suffix. */
4073 i.suffix = 0;
7697afb6
JB
4074 /* Convert to byte registers. */
4075 if (i.types[1].bitfield.word)
4076 j = 16;
4077 else if (i.types[1].bitfield.dword)
4078 j = 32;
4079 else
4080 j = 48;
4081 if (!(i.op[1].regs->reg_flags & RegRex) && base_regnum < 4)
4082 j += 8;
4083 i.op[1].regs -= j;
b6f8c7c4
L
4084 }
4085 }
4086 else if (flag_code == CODE_64BIT
389d00a5 4087 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
d3d50934
L
4088 && ((i.types[1].bitfield.qword
4089 && i.reg_operands == 1
b6f8c7c4
L
4090 && i.imm_operands == 1
4091 && i.op[0].imms->X_op == O_constant
507916b8 4092 && ((i.tm.base_opcode == 0xb8
b6f8c7c4
L
4093 && i.tm.extension_opcode == None
4094 && fits_in_unsigned_long (i.op[0].imms->X_add_number))
4095 || (fits_in_imm31 (i.op[0].imms->X_add_number)
72aea328
JB
4096 && ((i.tm.base_opcode == 0x24
4097 || i.tm.base_opcode == 0xa8)
b6f8c7c4
L
4098 || (i.tm.base_opcode == 0x80
4099 && i.tm.extension_opcode == 0x4)
4100 || ((i.tm.base_opcode == 0xf6
507916b8 4101 || (i.tm.base_opcode | 1) == 0xc7)
b8364fa7
JB
4102 && i.tm.extension_opcode == 0x0)))
4103 || (fits_in_imm7 (i.op[0].imms->X_add_number)
4104 && i.tm.base_opcode == 0x83
4105 && i.tm.extension_opcode == 0x4)))
d3d50934
L
4106 || (i.types[0].bitfield.qword
4107 && ((i.reg_operands == 2
4108 && i.op[0].regs == i.op[1].regs
72aea328
JB
4109 && (i.tm.base_opcode == 0x30
4110 || i.tm.base_opcode == 0x28))
d3d50934
L
4111 || (i.reg_operands == 1
4112 && i.operands == 1
72aea328 4113 && i.tm.base_opcode == 0x30)))))
b6f8c7c4
L
4114 {
4115 /* Optimize: -O:
4116 andq $imm31, %r64 -> andl $imm31, %r32
b8364fa7 4117 andq $imm7, %r64 -> andl $imm7, %r32
b6f8c7c4
L
4118 testq $imm31, %r64 -> testl $imm31, %r32
4119 xorq %r64, %r64 -> xorl %r32, %r32
4120 subq %r64, %r64 -> subl %r32, %r32
4121 movq $imm31, %r64 -> movl $imm31, %r32
4122 movq $imm32, %r64 -> movl $imm32, %r32
4123 */
4124 i.tm.opcode_modifier.norex64 = 1;
507916b8 4125 if (i.tm.base_opcode == 0xb8 || (i.tm.base_opcode | 1) == 0xc7)
b6f8c7c4
L
4126 {
4127 /* Handle
4128 movq $imm31, %r64 -> movl $imm31, %r32
4129 movq $imm32, %r64 -> movl $imm32, %r32
4130 */
4131 i.tm.operand_types[0].bitfield.imm32 = 1;
4132 i.tm.operand_types[0].bitfield.imm32s = 0;
4133 i.tm.operand_types[0].bitfield.imm64 = 0;
4134 i.types[0].bitfield.imm32 = 1;
4135 i.types[0].bitfield.imm32s = 0;
4136 i.types[0].bitfield.imm64 = 0;
4137 i.types[1].bitfield.dword = 1;
4138 i.types[1].bitfield.qword = 0;
507916b8 4139 if ((i.tm.base_opcode | 1) == 0xc7)
b6f8c7c4
L
4140 {
4141 /* Handle
4142 movq $imm31, %r64 -> movl $imm31, %r32
4143 */
507916b8 4144 i.tm.base_opcode = 0xb8;
b6f8c7c4 4145 i.tm.extension_opcode = None;
507916b8 4146 i.tm.opcode_modifier.w = 0;
b6f8c7c4
L
4147 i.tm.opcode_modifier.modrm = 0;
4148 }
4149 }
4150 }
5641ec01
JB
4151 else if (optimize > 1
4152 && !optimize_for_space
389d00a5 4153 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
5641ec01
JB
4154 && i.reg_operands == 2
4155 && i.op[0].regs == i.op[1].regs
4156 && ((i.tm.base_opcode & ~(Opcode_D | 1)) == 0x8
4157 || (i.tm.base_opcode & ~(Opcode_D | 1)) == 0x20)
4158 && (flag_code != CODE_64BIT || !i.types[0].bitfield.dword))
4159 {
4160 /* Optimize: -O2:
4161 andb %rN, %rN -> testb %rN, %rN
4162 andw %rN, %rN -> testw %rN, %rN
4163 andq %rN, %rN -> testq %rN, %rN
4164 orb %rN, %rN -> testb %rN, %rN
4165 orw %rN, %rN -> testw %rN, %rN
4166 orq %rN, %rN -> testq %rN, %rN
4167
4168 and outside of 64-bit mode
4169
4170 andl %rN, %rN -> testl %rN, %rN
4171 orl %rN, %rN -> testl %rN, %rN
4172 */
4173 i.tm.base_opcode = 0x84 | (i.tm.base_opcode & 1);
4174 }
99112332 4175 else if (i.reg_operands == 3
b6f8c7c4
L
4176 && i.op[0].regs == i.op[1].regs
4177 && !i.types[2].bitfield.xmmword
4178 && (i.tm.opcode_modifier.vex
6225c532 4179 || ((!i.mask.reg || i.mask.zeroing)
ca5312a2 4180 && i.rounding.type == rc_none
e771e7c9 4181 && is_evex_encoding (&i.tm)
80c34c38 4182 && (i.vec_encoding != vex_encoding_evex
dd22218c 4183 || cpu_arch_isa_flags.bitfield.cpuavx512vl
80c34c38 4184 || i.tm.cpu_flags.bitfield.cpuavx512vl
7091c612 4185 || (i.tm.operand_types[2].bitfield.zmmword
dd22218c 4186 && i.types[2].bitfield.ymmword))))
b6f8c7c4 4187 && ((i.tm.base_opcode == 0x55
b6f8c7c4 4188 || i.tm.base_opcode == 0x57
35648716
JB
4189 || i.tm.base_opcode == 0xdf
4190 || i.tm.base_opcode == 0xef
4191 || i.tm.base_opcode == 0xf8
4192 || i.tm.base_opcode == 0xf9
4193 || i.tm.base_opcode == 0xfa
4194 || i.tm.base_opcode == 0xfb
1424ad86 4195 || i.tm.base_opcode == 0x42
35648716 4196 || i.tm.base_opcode == 0x47)
b6f8c7c4
L
4197 && i.tm.extension_opcode == None))
4198 {
99112332 4199 /* Optimize: -O1:
8305403a
L
4200 VOP, one of vandnps, vandnpd, vxorps, vxorpd, vpsubb, vpsubd,
4201 vpsubq and vpsubw:
b6f8c7c4
L
4202 EVEX VOP %zmmM, %zmmM, %zmmN
4203 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4204 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4205 EVEX VOP %ymmM, %ymmM, %ymmN
4206 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4207 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4208 VEX VOP %ymmM, %ymmM, %ymmN
4209 -> VEX VOP %xmmM, %xmmM, %xmmN
4210 VOP, one of vpandn and vpxor:
4211 VEX VOP %ymmM, %ymmM, %ymmN
4212 -> VEX VOP %xmmM, %xmmM, %xmmN
4213 VOP, one of vpandnd and vpandnq:
4214 EVEX VOP %zmmM, %zmmM, %zmmN
4215 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4216 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4217 EVEX VOP %ymmM, %ymmM, %ymmN
4218 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4219 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4220 VOP, one of vpxord and vpxorq:
4221 EVEX VOP %zmmM, %zmmM, %zmmN
4222 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4223 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4224 EVEX VOP %ymmM, %ymmM, %ymmN
4225 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4226 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
1424ad86
JB
4227 VOP, one of kxord and kxorq:
4228 VEX VOP %kM, %kM, %kN
4229 -> VEX kxorw %kM, %kM, %kN
4230 VOP, one of kandnd and kandnq:
4231 VEX VOP %kM, %kM, %kN
4232 -> VEX kandnw %kM, %kM, %kN
b6f8c7c4 4233 */
e771e7c9 4234 if (is_evex_encoding (&i.tm))
b6f8c7c4 4235 {
7b1d7ca1 4236 if (i.vec_encoding != vex_encoding_evex)
b6f8c7c4
L
4237 {
4238 i.tm.opcode_modifier.vex = VEX128;
4239 i.tm.opcode_modifier.vexw = VEXW0;
4240 i.tm.opcode_modifier.evex = 0;
4241 }
7b1d7ca1 4242 else if (optimize > 1)
dd22218c
L
4243 i.tm.opcode_modifier.evex = EVEX128;
4244 else
4245 return;
b6f8c7c4 4246 }
f74a6307 4247 else if (i.tm.operand_types[0].bitfield.class == RegMask)
1424ad86 4248 {
35648716 4249 i.tm.opcode_modifier.opcodeprefix = PREFIX_NONE;
1424ad86
JB
4250 i.tm.opcode_modifier.vexw = VEXW0;
4251 }
b6f8c7c4
L
4252 else
4253 i.tm.opcode_modifier.vex = VEX128;
4254
4255 if (i.tm.opcode_modifier.vex)
4256 for (j = 0; j < 3; j++)
4257 {
4258 i.types[j].bitfield.xmmword = 1;
4259 i.types[j].bitfield.ymmword = 0;
4260 }
4261 }
392a5972 4262 else if (i.vec_encoding != vex_encoding_evex
97ed31ae 4263 && !i.types[0].bitfield.zmmword
392a5972 4264 && !i.types[1].bitfield.zmmword
6225c532 4265 && !i.mask.reg
5273a3cd 4266 && !i.broadcast.type
97ed31ae 4267 && is_evex_encoding (&i.tm)
35648716
JB
4268 && ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4269 || (i.tm.base_opcode & ~4) == 0xdb
4270 || (i.tm.base_opcode & ~4) == 0xeb)
97ed31ae
L
4271 && i.tm.extension_opcode == None)
4272 {
4273 /* Optimize: -O1:
4274 VOP, one of vmovdqa32, vmovdqa64, vmovdqu8, vmovdqu16,
4275 vmovdqu32 and vmovdqu64:
4276 EVEX VOP %xmmM, %xmmN
4277 -> VEX vmovdqa|vmovdqu %xmmM, %xmmN (M and N < 16)
4278 EVEX VOP %ymmM, %ymmN
4279 -> VEX vmovdqa|vmovdqu %ymmM, %ymmN (M and N < 16)
4280 EVEX VOP %xmmM, mem
4281 -> VEX vmovdqa|vmovdqu %xmmM, mem (M < 16)
4282 EVEX VOP %ymmM, mem
4283 -> VEX vmovdqa|vmovdqu %ymmM, mem (M < 16)
4284 EVEX VOP mem, %xmmN
4285 -> VEX mvmovdqa|vmovdquem, %xmmN (N < 16)
4286 EVEX VOP mem, %ymmN
4287 -> VEX vmovdqa|vmovdqu mem, %ymmN (N < 16)
a0a1771e
JB
4288 VOP, one of vpand, vpandn, vpor, vpxor:
4289 EVEX VOP{d,q} %xmmL, %xmmM, %xmmN
4290 -> VEX VOP %xmmL, %xmmM, %xmmN (L, M, and N < 16)
4291 EVEX VOP{d,q} %ymmL, %ymmM, %ymmN
4292 -> VEX VOP %ymmL, %ymmM, %ymmN (L, M, and N < 16)
4293 EVEX VOP{d,q} mem, %xmmM, %xmmN
4294 -> VEX VOP mem, %xmmM, %xmmN (M and N < 16)
4295 EVEX VOP{d,q} mem, %ymmM, %ymmN
4296 -> VEX VOP mem, %ymmM, %ymmN (M and N < 16)
97ed31ae 4297 */
a0a1771e 4298 for (j = 0; j < i.operands; j++)
392a5972
L
4299 if (operand_type_check (i.types[j], disp)
4300 && i.op[j].disps->X_op == O_constant)
4301 {
4302 /* Since the VEX prefix has 2 or 3 bytes, the EVEX prefix
4303 has 4 bytes, EVEX Disp8 has 1 byte and VEX Disp32 has 4
4304 bytes, we choose EVEX Disp8 over VEX Disp32. */
4305 int evex_disp8, vex_disp8;
4306 unsigned int memshift = i.memshift;
4307 offsetT n = i.op[j].disps->X_add_number;
4308
4309 evex_disp8 = fits_in_disp8 (n);
4310 i.memshift = 0;
4311 vex_disp8 = fits_in_disp8 (n);
4312 if (evex_disp8 != vex_disp8)
4313 {
4314 i.memshift = memshift;
4315 return;
4316 }
4317
4318 i.types[j].bitfield.disp8 = vex_disp8;
4319 break;
4320 }
35648716
JB
4321 if ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4322 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2)
4323 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
97ed31ae
L
4324 i.tm.opcode_modifier.vex
4325 = i.types[0].bitfield.ymmword ? VEX256 : VEX128;
4326 i.tm.opcode_modifier.vexw = VEXW0;
79dec6b7 4327 /* VPAND, VPOR, and VPXOR are commutative. */
35648716 4328 if (i.reg_operands == 3 && i.tm.base_opcode != 0xdf)
79dec6b7 4329 i.tm.opcode_modifier.commutative = 1;
97ed31ae
L
4330 i.tm.opcode_modifier.evex = 0;
4331 i.tm.opcode_modifier.masking = 0;
a0a1771e 4332 i.tm.opcode_modifier.broadcast = 0;
97ed31ae
L
4333 i.tm.opcode_modifier.disp8memshift = 0;
4334 i.memshift = 0;
a0a1771e
JB
4335 if (j < i.operands)
4336 i.types[j].bitfield.disp8
4337 = fits_in_disp8 (i.op[j].disps->X_add_number);
97ed31ae 4338 }
b6f8c7c4
L
4339}
4340
ae531041
L
4341/* Return non-zero for load instruction. */
4342
4343static int
4344load_insn_p (void)
4345{
4346 unsigned int dest;
4347 int any_vex_p = is_any_vex_encoding (&i.tm);
4348 unsigned int base_opcode = i.tm.base_opcode | 1;
4349
4350 if (!any_vex_p)
4351 {
a09f656b 4352 /* Anysize insns: lea, invlpg, clflush, prefetchnta, prefetcht0,
4353 prefetcht1, prefetcht2, prefetchtw, bndmk, bndcl, bndcu, bndcn,
4354 bndstx, bndldx, prefetchwt1, clflushopt, clwb, cldemote. */
4355 if (i.tm.opcode_modifier.anysize)
ae531041
L
4356 return 0;
4357
389d00a5
JB
4358 /* pop. */
4359 if (strcmp (i.tm.name, "pop") == 0)
4360 return 1;
4361 }
4362
4363 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE)
4364 {
4365 /* popf, popa. */
4366 if (i.tm.base_opcode == 0x9d
a09f656b 4367 || i.tm.base_opcode == 0x61)
ae531041
L
4368 return 1;
4369
4370 /* movs, cmps, lods, scas. */
4371 if ((i.tm.base_opcode | 0xb) == 0xaf)
4372 return 1;
4373
a09f656b 4374 /* outs, xlatb. */
4375 if (base_opcode == 0x6f
4376 || i.tm.base_opcode == 0xd7)
ae531041 4377 return 1;
a09f656b 4378 /* NB: For AMD-specific insns with implicit memory operands,
4379 they're intentionally not covered. */
ae531041
L
4380 }
4381
4382 /* No memory operand. */
4383 if (!i.mem_operands)
4384 return 0;
4385
4386 if (any_vex_p)
4387 {
4388 /* vldmxcsr. */
4389 if (i.tm.base_opcode == 0xae
4390 && i.tm.opcode_modifier.vex
441f6aca 4391 && i.tm.opcode_modifier.opcodespace == SPACE_0F
35648716 4392 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
ae531041
L
4393 && i.tm.extension_opcode == 2)
4394 return 1;
4395 }
389d00a5 4396 else if (i.tm.opcode_modifier.opcodespace == SPACE_BASE)
ae531041
L
4397 {
4398 /* test, not, neg, mul, imul, div, idiv. */
4399 if ((i.tm.base_opcode == 0xf6 || i.tm.base_opcode == 0xf7)
4400 && i.tm.extension_opcode != 1)
4401 return 1;
4402
4403 /* inc, dec. */
4404 if (base_opcode == 0xff && i.tm.extension_opcode <= 1)
4405 return 1;
4406
4407 /* add, or, adc, sbb, and, sub, xor, cmp. */
4408 if (i.tm.base_opcode >= 0x80 && i.tm.base_opcode <= 0x83)
4409 return 1;
4410
ae531041
L
4411 /* rol, ror, rcl, rcr, shl/sal, shr, sar. */
4412 if ((base_opcode == 0xc1
4413 || (i.tm.base_opcode >= 0xd0 && i.tm.base_opcode <= 0xd3))
4414 && i.tm.extension_opcode != 6)
4415 return 1;
4416
ae531041 4417 /* Check for x87 instructions. */
389d00a5 4418 if (base_opcode >= 0xd8 && base_opcode <= 0xdf)
ae531041
L
4419 {
4420 /* Skip fst, fstp, fstenv, fstcw. */
4421 if (i.tm.base_opcode == 0xd9
4422 && (i.tm.extension_opcode == 2
4423 || i.tm.extension_opcode == 3
4424 || i.tm.extension_opcode == 6
4425 || i.tm.extension_opcode == 7))
4426 return 0;
4427
4428 /* Skip fisttp, fist, fistp, fstp. */
4429 if (i.tm.base_opcode == 0xdb
4430 && (i.tm.extension_opcode == 1
4431 || i.tm.extension_opcode == 2
4432 || i.tm.extension_opcode == 3
4433 || i.tm.extension_opcode == 7))
4434 return 0;
4435
4436 /* Skip fisttp, fst, fstp, fsave, fstsw. */
4437 if (i.tm.base_opcode == 0xdd
4438 && (i.tm.extension_opcode == 1
4439 || i.tm.extension_opcode == 2
4440 || i.tm.extension_opcode == 3
4441 || i.tm.extension_opcode == 6
4442 || i.tm.extension_opcode == 7))
4443 return 0;
4444
4445 /* Skip fisttp, fist, fistp, fbstp, fistp. */
4446 if (i.tm.base_opcode == 0xdf
4447 && (i.tm.extension_opcode == 1
4448 || i.tm.extension_opcode == 2
4449 || i.tm.extension_opcode == 3
4450 || i.tm.extension_opcode == 6
4451 || i.tm.extension_opcode == 7))
4452 return 0;
4453
4454 return 1;
4455 }
4456 }
389d00a5
JB
4457 else if (i.tm.opcode_modifier.opcodespace == SPACE_0F)
4458 {
4459 /* bt, bts, btr, btc. */
4460 if (i.tm.base_opcode == 0xba
4461 && (i.tm.extension_opcode >= 4 && i.tm.extension_opcode <= 7))
4462 return 1;
4463
4464 /* cmpxchg8b, cmpxchg16b, xrstors, vmptrld. */
4465 if (i.tm.base_opcode == 0xc7
4466 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
4467 && (i.tm.extension_opcode == 1 || i.tm.extension_opcode == 3
4468 || i.tm.extension_opcode == 6))
4469 return 1;
4470
4471 /* fxrstor, ldmxcsr, xrstor. */
4472 if (i.tm.base_opcode == 0xae
4473 && (i.tm.extension_opcode == 1
4474 || i.tm.extension_opcode == 2
4475 || i.tm.extension_opcode == 5))
4476 return 1;
4477
4478 /* lgdt, lidt, lmsw. */
4479 if (i.tm.base_opcode == 0x01
4480 && (i.tm.extension_opcode == 2
4481 || i.tm.extension_opcode == 3
4482 || i.tm.extension_opcode == 6))
4483 return 1;
4484 }
ae531041
L
4485
4486 dest = i.operands - 1;
4487
4488 /* Check fake imm8 operand and 3 source operands. */
4489 if ((i.tm.opcode_modifier.immext
4490 || i.tm.opcode_modifier.vexsources == VEX3SOURCES)
4491 && i.types[dest].bitfield.imm8)
4492 dest--;
4493
389d00a5
JB
4494 /* add, or, adc, sbb, and, sub, xor, cmp, test, xchg. */
4495 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
ae531041
L
4496 && (base_opcode == 0x1
4497 || base_opcode == 0x9
4498 || base_opcode == 0x11
4499 || base_opcode == 0x19
4500 || base_opcode == 0x21
4501 || base_opcode == 0x29
4502 || base_opcode == 0x31
4503 || base_opcode == 0x39
389d00a5
JB
4504 || (base_opcode | 2) == 0x87))
4505 return 1;
4506
4507 /* xadd. */
4508 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4509 && base_opcode == 0xc1)
ae531041
L
4510 return 1;
4511
4512 /* Check for load instruction. */
4513 return (i.types[dest].bitfield.class != ClassNone
4514 || i.types[dest].bitfield.instance == Accum);
4515}
4516
4517/* Output lfence, 0xfaee8, after instruction. */
4518
4519static void
4520insert_lfence_after (void)
4521{
4522 if (lfence_after_load && load_insn_p ())
4523 {
a09f656b 4524 /* There are also two REP string instructions that require
4525 special treatment. Specifically, the compare string (CMPS)
4526 and scan string (SCAS) instructions set EFLAGS in a manner
4527 that depends on the data being compared/scanned. When used
4528 with a REP prefix, the number of iterations may therefore
4529 vary depending on this data. If the data is a program secret
4530 chosen by the adversary using an LVI method,
4531 then this data-dependent behavior may leak some aspect
4532 of the secret. */
4533 if (((i.tm.base_opcode | 0x1) == 0xa7
4534 || (i.tm.base_opcode | 0x1) == 0xaf)
4535 && i.prefix[REP_PREFIX])
4536 {
4537 as_warn (_("`%s` changes flags which would affect control flow behavior"),
4538 i.tm.name);
4539 }
ae531041
L
4540 char *p = frag_more (3);
4541 *p++ = 0xf;
4542 *p++ = 0xae;
4543 *p = 0xe8;
4544 }
4545}
4546
4547/* Output lfence, 0xfaee8, before instruction. */
4548
4549static void
4550insert_lfence_before (void)
4551{
4552 char *p;
4553
389d00a5 4554 if (i.tm.opcode_modifier.opcodespace != SPACE_BASE)
ae531041
L
4555 return;
4556
4557 if (i.tm.base_opcode == 0xff
4558 && (i.tm.extension_opcode == 2 || i.tm.extension_opcode == 4))
4559 {
4560 /* Insert lfence before indirect branch if needed. */
4561
4562 if (lfence_before_indirect_branch == lfence_branch_none)
4563 return;
4564
4565 if (i.operands != 1)
4566 abort ();
4567
4568 if (i.reg_operands == 1)
4569 {
4570 /* Indirect branch via register. Don't insert lfence with
4571 -mlfence-after-load=yes. */
4572 if (lfence_after_load
4573 || lfence_before_indirect_branch == lfence_branch_memory)
4574 return;
4575 }
4576 else if (i.mem_operands == 1
4577 && lfence_before_indirect_branch != lfence_branch_register)
4578 {
4579 as_warn (_("indirect `%s` with memory operand should be avoided"),
4580 i.tm.name);
4581 return;
4582 }
4583 else
4584 return;
4585
4586 if (last_insn.kind != last_insn_other
4587 && last_insn.seg == now_seg)
4588 {
4589 as_warn_where (last_insn.file, last_insn.line,
4590 _("`%s` skips -mlfence-before-indirect-branch on `%s`"),
4591 last_insn.name, i.tm.name);
4592 return;
4593 }
4594
4595 p = frag_more (3);
4596 *p++ = 0xf;
4597 *p++ = 0xae;
4598 *p = 0xe8;
4599 return;
4600 }
4601
503648e4 4602 /* Output or/not/shl and lfence before near ret. */
ae531041
L
4603 if (lfence_before_ret != lfence_before_ret_none
4604 && (i.tm.base_opcode == 0xc2
503648e4 4605 || i.tm.base_opcode == 0xc3))
ae531041
L
4606 {
4607 if (last_insn.kind != last_insn_other
4608 && last_insn.seg == now_seg)
4609 {
4610 as_warn_where (last_insn.file, last_insn.line,
4611 _("`%s` skips -mlfence-before-ret on `%s`"),
4612 last_insn.name, i.tm.name);
4613 return;
4614 }
a09f656b 4615
a09f656b 4616 /* Near ret ingore operand size override under CPU64. */
503648e4 4617 char prefix = flag_code == CODE_64BIT
4618 ? 0x48
4619 : i.prefix[DATA_PREFIX] ? 0x66 : 0x0;
a09f656b 4620
4621 if (lfence_before_ret == lfence_before_ret_not)
4622 {
4623 /* not: 0xf71424, may add prefix
4624 for operand size override or 64-bit code. */
4625 p = frag_more ((prefix ? 2 : 0) + 6 + 3);
4626 if (prefix)
4627 *p++ = prefix;
ae531041
L
4628 *p++ = 0xf7;
4629 *p++ = 0x14;
4630 *p++ = 0x24;
a09f656b 4631 if (prefix)
4632 *p++ = prefix;
ae531041
L
4633 *p++ = 0xf7;
4634 *p++ = 0x14;
4635 *p++ = 0x24;
4636 }
a09f656b 4637 else
4638 {
4639 p = frag_more ((prefix ? 1 : 0) + 4 + 3);
4640 if (prefix)
4641 *p++ = prefix;
4642 if (lfence_before_ret == lfence_before_ret_or)
4643 {
4644 /* or: 0x830c2400, may add prefix
4645 for operand size override or 64-bit code. */
4646 *p++ = 0x83;
4647 *p++ = 0x0c;
4648 }
4649 else
4650 {
4651 /* shl: 0xc1242400, may add prefix
4652 for operand size override or 64-bit code. */
4653 *p++ = 0xc1;
4654 *p++ = 0x24;
4655 }
4656
4657 *p++ = 0x24;
4658 *p++ = 0x0;
4659 }
4660
ae531041
L
4661 *p++ = 0xf;
4662 *p++ = 0xae;
4663 *p = 0xe8;
4664 }
4665}
4666
252b5132
RH
4667/* This is the guts of the machine-dependent assembler. LINE points to a
4668 machine dependent instruction. This function is supposed to emit
4669 the frags/bytes it assembles to. */
4670
4671void
65da13b5 4672md_assemble (char *line)
252b5132 4673{
40fb9820 4674 unsigned int j;
83b16ac6 4675 char mnemonic[MAX_MNEM_SIZE], mnem_suffix;
d3ce72d0 4676 const insn_template *t;
252b5132 4677
47926f60 4678 /* Initialize globals. */
252b5132 4679 memset (&i, '\0', sizeof (i));
ca5312a2 4680 i.rounding.type = rc_none;
252b5132 4681 for (j = 0; j < MAX_OPERANDS; j++)
1ae12ab7 4682 i.reloc[j] = NO_RELOC;
252b5132
RH
4683 memset (disp_expressions, '\0', sizeof (disp_expressions));
4684 memset (im_expressions, '\0', sizeof (im_expressions));
ce8a8b2f 4685 save_stack_p = save_stack;
252b5132
RH
4686
4687 /* First parse an instruction mnemonic & call i386_operand for the operands.
4688 We assume that the scrubber has arranged it so that line[0] is the valid
47926f60 4689 start of a (possibly prefixed) mnemonic. */
252b5132 4690
29b0f896
AM
4691 line = parse_insn (line, mnemonic);
4692 if (line == NULL)
4693 return;
83b16ac6 4694 mnem_suffix = i.suffix;
252b5132 4695
29b0f896 4696 line = parse_operands (line, mnemonic);
ee86248c 4697 this_operand = -1;
8325cc63
JB
4698 xfree (i.memop1_string);
4699 i.memop1_string = NULL;
29b0f896
AM
4700 if (line == NULL)
4701 return;
252b5132 4702
29b0f896
AM
4703 /* Now we've parsed the mnemonic into a set of templates, and have the
4704 operands at hand. */
4705
b630c145 4706 /* All Intel opcodes have reversed operands except for "bound", "enter",
c0e54661
JB
4707 "invlpg*", "monitor*", "mwait*", "tpause", "umwait", "pvalidate",
4708 "rmpadjust", and "rmpupdate". We also don't reverse intersegment "jmp"
4709 and "call" instructions with 2 immediate operands so that the immediate
4710 segment precedes the offset consistently in Intel and AT&T modes. */
4d456e3d
L
4711 if (intel_syntax
4712 && i.operands > 1
29b0f896 4713 && (strcmp (mnemonic, "bound") != 0)
c0e54661 4714 && (strncmp (mnemonic, "invlpg", 6) != 0)
eedb0f2c
JB
4715 && (strncmp (mnemonic, "monitor", 7) != 0)
4716 && (strncmp (mnemonic, "mwait", 5) != 0)
c0e54661
JB
4717 && (strcmp (mnemonic, "pvalidate") != 0)
4718 && (strncmp (mnemonic, "rmp", 3) != 0)
b630c145
JB
4719 && (strcmp (mnemonic, "tpause") != 0)
4720 && (strcmp (mnemonic, "umwait") != 0)
40fb9820
L
4721 && !(operand_type_check (i.types[0], imm)
4722 && operand_type_check (i.types[1], imm)))
29b0f896
AM
4723 swap_operands ();
4724
ec56d5c0
JB
4725 /* The order of the immediates should be reversed
4726 for 2 immediates extrq and insertq instructions */
4727 if (i.imm_operands == 2
4728 && (strcmp (mnemonic, "extrq") == 0
4729 || strcmp (mnemonic, "insertq") == 0))
4730 swap_2_operands (0, 1);
4731
29b0f896
AM
4732 if (i.imm_operands)
4733 optimize_imm ();
4734
b300c311
L
4735 /* Don't optimize displacement for movabs since it only takes 64bit
4736 displacement. */
4737 if (i.disp_operands
a501d77e 4738 && i.disp_encoding != disp_encoding_32bit
862be3fb
L
4739 && (flag_code != CODE_64BIT
4740 || strcmp (mnemonic, "movabs") != 0))
4741 optimize_disp ();
29b0f896
AM
4742
4743 /* Next, we find a template that matches the given insn,
4744 making sure the overlap of the given operands types is consistent
4745 with the template operand types. */
252b5132 4746
83b16ac6 4747 if (!(t = match_template (mnem_suffix)))
29b0f896 4748 return;
252b5132 4749
7bab8ab5 4750 if (sse_check != check_none
81f8a913 4751 && !i.tm.opcode_modifier.noavx
6e3e5c9e 4752 && !i.tm.cpu_flags.bitfield.cpuavx
569d50f1 4753 && !i.tm.cpu_flags.bitfield.cpuavx512f
daf50ae7
L
4754 && (i.tm.cpu_flags.bitfield.cpusse
4755 || i.tm.cpu_flags.bitfield.cpusse2
4756 || i.tm.cpu_flags.bitfield.cpusse3
4757 || i.tm.cpu_flags.bitfield.cpussse3
4758 || i.tm.cpu_flags.bitfield.cpusse4_1
6e3e5c9e
JB
4759 || i.tm.cpu_flags.bitfield.cpusse4_2
4760 || i.tm.cpu_flags.bitfield.cpupclmul
4761 || i.tm.cpu_flags.bitfield.cpuaes
569d50f1 4762 || i.tm.cpu_flags.bitfield.cpusha
6e3e5c9e 4763 || i.tm.cpu_flags.bitfield.cpugfni))
daf50ae7 4764 {
7bab8ab5 4765 (sse_check == check_warning
daf50ae7
L
4766 ? as_warn
4767 : as_bad) (_("SSE instruction `%s' is used"), i.tm.name);
4768 }
4769
40fb9820 4770 if (i.tm.opcode_modifier.fwait)
29b0f896
AM
4771 if (!add_prefix (FWAIT_OPCODE))
4772 return;
252b5132 4773
d5de92cf 4774 /* Check if REP prefix is OK. */
742732c7 4775 if (i.rep_prefix && i.tm.opcode_modifier.prefixok != PrefixRep)
d5de92cf
L
4776 {
4777 as_bad (_("invalid instruction `%s' after `%s'"),
4778 i.tm.name, i.rep_prefix);
4779 return;
4780 }
4781
c1ba0266
L
4782 /* Check for lock without a lockable instruction. Destination operand
4783 must be memory unless it is xchg (0x86). */
c32fa91d 4784 if (i.prefix[LOCK_PREFIX]
742732c7 4785 && (i.tm.opcode_modifier.prefixok < PrefixLock
c1ba0266
L
4786 || i.mem_operands == 0
4787 || (i.tm.base_opcode != 0x86
8dc0818e 4788 && !(i.flags[i.operands - 1] & Operand_Mem))))
c32fa91d
L
4789 {
4790 as_bad (_("expecting lockable instruction after `lock'"));
4791 return;
4792 }
4793
40d231b4
JB
4794 /* Check for data size prefix on VEX/XOP/EVEX encoded and SIMD insns. */
4795 if (i.prefix[DATA_PREFIX]
4796 && (is_any_vex_encoding (&i.tm)
4797 || i.tm.operand_types[i.imm_operands].bitfield.class >= RegMMX
4798 || i.tm.operand_types[i.imm_operands + 1].bitfield.class >= RegMMX))
7a8655d2
JB
4799 {
4800 as_bad (_("data size prefix invalid with `%s'"), i.tm.name);
4801 return;
4802 }
4803
42164a71 4804 /* Check if HLE prefix is OK. */
165de32a 4805 if (i.hle_prefix && !check_hle ())
42164a71
L
4806 return;
4807
7e8b059b
L
4808 /* Check BND prefix. */
4809 if (i.bnd_prefix && !i.tm.opcode_modifier.bndprefixok)
4810 as_bad (_("expecting valid branch instruction after `bnd'"));
4811
04ef582a 4812 /* Check NOTRACK prefix. */
742732c7 4813 if (i.notrack_prefix && i.tm.opcode_modifier.prefixok != PrefixNoTrack)
9fef80d6 4814 as_bad (_("expecting indirect branch instruction after `notrack'"));
04ef582a 4815
327e8c42
JB
4816 if (i.tm.cpu_flags.bitfield.cpumpx)
4817 {
4818 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
4819 as_bad (_("32-bit address isn't allowed in 64-bit MPX instructions."));
4820 else if (flag_code != CODE_16BIT
4821 ? i.prefix[ADDR_PREFIX]
4822 : i.mem_operands && !i.prefix[ADDR_PREFIX])
4823 as_bad (_("16-bit address isn't allowed in MPX instructions"));
4824 }
7e8b059b
L
4825
4826 /* Insert BND prefix. */
76d3a78a
JB
4827 if (add_bnd_prefix && i.tm.opcode_modifier.bndprefixok)
4828 {
4829 if (!i.prefix[BND_PREFIX])
4830 add_prefix (BND_PREFIX_OPCODE);
4831 else if (i.prefix[BND_PREFIX] != BND_PREFIX_OPCODE)
4832 {
4833 as_warn (_("replacing `rep'/`repe' prefix by `bnd'"));
4834 i.prefix[BND_PREFIX] = BND_PREFIX_OPCODE;
4835 }
4836 }
7e8b059b 4837
29b0f896 4838 /* Check string instruction segment overrides. */
51c8edf6 4839 if (i.tm.opcode_modifier.isstring >= IS_STRING_ES_OP0)
29b0f896 4840 {
51c8edf6 4841 gas_assert (i.mem_operands);
29b0f896 4842 if (!check_string ())
5dd0794d 4843 return;
fc0763e6 4844 i.disp_operands = 0;
29b0f896 4845 }
5dd0794d 4846
b6f8c7c4
L
4847 if (optimize && !i.no_optimize && i.tm.opcode_modifier.optimize)
4848 optimize_encoding ();
4849
29b0f896
AM
4850 if (!process_suffix ())
4851 return;
e413e4e9 4852
921eafea 4853 /* Update operand types and check extended states. */
bc0844ae 4854 for (j = 0; j < i.operands; j++)
921eafea
L
4855 {
4856 i.types[j] = operand_type_and (i.types[j], i.tm.operand_types[j]);
3d70986f 4857 switch (i.tm.operand_types[j].bitfield.class)
921eafea
L
4858 {
4859 default:
4860 break;
4861 case RegMMX:
4862 i.xstate |= xstate_mmx;
4863 break;
4864 case RegMask:
32930e4e 4865 i.xstate |= xstate_mask;
921eafea
L
4866 break;
4867 case RegSIMD:
3d70986f 4868 if (i.tm.operand_types[j].bitfield.tmmword)
921eafea 4869 i.xstate |= xstate_tmm;
3d70986f 4870 else if (i.tm.operand_types[j].bitfield.zmmword)
921eafea 4871 i.xstate |= xstate_zmm;
3d70986f 4872 else if (i.tm.operand_types[j].bitfield.ymmword)
921eafea 4873 i.xstate |= xstate_ymm;
3d70986f 4874 else if (i.tm.operand_types[j].bitfield.xmmword)
921eafea
L
4875 i.xstate |= xstate_xmm;
4876 break;
4877 }
4878 }
bc0844ae 4879
29b0f896
AM
4880 /* Make still unresolved immediate matches conform to size of immediate
4881 given in i.suffix. */
4882 if (!finalize_imm ())
4883 return;
252b5132 4884
40fb9820 4885 if (i.types[0].bitfield.imm1)
29b0f896 4886 i.imm_operands = 0; /* kludge for shift insns. */
252b5132 4887
9afe6eb8
L
4888 /* We only need to check those implicit registers for instructions
4889 with 3 operands or less. */
4890 if (i.operands <= 3)
4891 for (j = 0; j < i.operands; j++)
75e5731b
JB
4892 if (i.types[j].bitfield.instance != InstanceNone
4893 && !i.types[j].bitfield.xmmword)
9afe6eb8 4894 i.reg_operands--;
40fb9820 4895
29b0f896
AM
4896 /* For insns with operands there are more diddles to do to the opcode. */
4897 if (i.operands)
4898 {
4899 if (!process_operands ())
4900 return;
4901 }
8c190ce0 4902 else if (!quiet_warnings && i.tm.opcode_modifier.ugh)
29b0f896
AM
4903 {
4904 /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc. */
4905 as_warn (_("translating to `%sp'"), i.tm.name);
4906 }
252b5132 4907
7a8655d2 4908 if (is_any_vex_encoding (&i.tm))
9e5e5283 4909 {
c1dc7af5 4910 if (!cpu_arch_flags.bitfield.cpui286)
9e5e5283 4911 {
c1dc7af5 4912 as_bad (_("instruction `%s' isn't supported outside of protected mode."),
9e5e5283
L
4913 i.tm.name);
4914 return;
4915 }
c0f3af97 4916
0b9404fd
JB
4917 /* Check for explicit REX prefix. */
4918 if (i.prefix[REX_PREFIX] || i.rex_encoding)
4919 {
4920 as_bad (_("REX prefix invalid with `%s'"), i.tm.name);
4921 return;
4922 }
4923
9e5e5283
L
4924 if (i.tm.opcode_modifier.vex)
4925 build_vex_prefix (t);
4926 else
4927 build_evex_prefix ();
0b9404fd
JB
4928
4929 /* The individual REX.RXBW bits got consumed. */
4930 i.rex &= REX_OPCODE;
9e5e5283 4931 }
43234a1e 4932
5dd85c99
SP
4933 /* Handle conversion of 'int $3' --> special int3 insn. XOP or FMA4
4934 instructions may define INT_OPCODE as well, so avoid this corner
4935 case for those instructions that use MODRM. */
389d00a5
JB
4936 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
4937 && i.tm.base_opcode == INT_OPCODE
a6461c02
SP
4938 && !i.tm.opcode_modifier.modrm
4939 && i.op[0].imms->X_add_number == 3)
29b0f896
AM
4940 {
4941 i.tm.base_opcode = INT3_OPCODE;
4942 i.imm_operands = 0;
4943 }
252b5132 4944
0cfa3eb3
JB
4945 if ((i.tm.opcode_modifier.jump == JUMP
4946 || i.tm.opcode_modifier.jump == JUMP_BYTE
4947 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896
AM
4948 && i.op[0].disps->X_op == O_constant)
4949 {
4950 /* Convert "jmp constant" (and "call constant") to a jump (call) to
4951 the absolute address given by the constant. Since ix86 jumps and
4952 calls are pc relative, we need to generate a reloc. */
4953 i.op[0].disps->X_add_symbol = &abs_symbol;
4954 i.op[0].disps->X_op = O_symbol;
4955 }
252b5132 4956
29b0f896
AM
4957 /* For 8 bit registers we need an empty rex prefix. Also if the
4958 instruction already has a prefix, we need to convert old
4959 registers to new ones. */
773f551c 4960
bab6aec1 4961 if ((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte
29b0f896 4962 && (i.op[0].regs->reg_flags & RegRex64) != 0)
bab6aec1 4963 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte
29b0f896 4964 && (i.op[1].regs->reg_flags & RegRex64) != 0)
bab6aec1
JB
4965 || (((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte)
4966 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte))
29b0f896
AM
4967 && i.rex != 0))
4968 {
4969 int x;
726c5dcd 4970
29b0f896
AM
4971 i.rex |= REX_OPCODE;
4972 for (x = 0; x < 2; x++)
4973 {
4974 /* Look for 8 bit operand that uses old registers. */
bab6aec1 4975 if (i.types[x].bitfield.class == Reg && i.types[x].bitfield.byte
29b0f896 4976 && (i.op[x].regs->reg_flags & RegRex64) == 0)
773f551c 4977 {
3f93af61 4978 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
29b0f896
AM
4979 /* In case it is "hi" register, give up. */
4980 if (i.op[x].regs->reg_num > 3)
a540244d 4981 as_bad (_("can't encode register '%s%s' in an "
4eed87de 4982 "instruction requiring REX prefix."),
a540244d 4983 register_prefix, i.op[x].regs->reg_name);
773f551c 4984
29b0f896
AM
4985 /* Otherwise it is equivalent to the extended register.
4986 Since the encoding doesn't change this is merely
4987 cosmetic cleanup for debug output. */
4988
4989 i.op[x].regs = i.op[x].regs + 8;
773f551c 4990 }
29b0f896
AM
4991 }
4992 }
773f551c 4993
6b6b6807
L
4994 if (i.rex == 0 && i.rex_encoding)
4995 {
4996 /* Check if we can add a REX_OPCODE byte. Look for 8 bit operand
3f93af61 4997 that uses legacy register. If it is "hi" register, don't add
6b6b6807
L
4998 the REX_OPCODE byte. */
4999 int x;
5000 for (x = 0; x < 2; x++)
bab6aec1 5001 if (i.types[x].bitfield.class == Reg
6b6b6807
L
5002 && i.types[x].bitfield.byte
5003 && (i.op[x].regs->reg_flags & RegRex64) == 0
5004 && i.op[x].regs->reg_num > 3)
5005 {
3f93af61 5006 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
6b6b6807
L
5007 i.rex_encoding = FALSE;
5008 break;
5009 }
5010
5011 if (i.rex_encoding)
5012 i.rex = REX_OPCODE;
5013 }
5014
7ab9ffdd 5015 if (i.rex != 0)
29b0f896
AM
5016 add_prefix (REX_OPCODE | i.rex);
5017
ae531041
L
5018 insert_lfence_before ();
5019
29b0f896
AM
5020 /* We are ready to output the insn. */
5021 output_insn ();
e379e5f3 5022
ae531041
L
5023 insert_lfence_after ();
5024
e379e5f3
L
5025 last_insn.seg = now_seg;
5026
5027 if (i.tm.opcode_modifier.isprefix)
5028 {
5029 last_insn.kind = last_insn_prefix;
5030 last_insn.name = i.tm.name;
5031 last_insn.file = as_where (&last_insn.line);
5032 }
5033 else
5034 last_insn.kind = last_insn_other;
29b0f896
AM
5035}
5036
5037static char *
e3bb37b5 5038parse_insn (char *line, char *mnemonic)
29b0f896
AM
5039{
5040 char *l = line;
5041 char *token_start = l;
5042 char *mnem_p;
5c6af06e 5043 int supported;
d3ce72d0 5044 const insn_template *t;
b6169b20 5045 char *dot_p = NULL;
29b0f896 5046
29b0f896
AM
5047 while (1)
5048 {
5049 mnem_p = mnemonic;
5050 while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
5051 {
b6169b20
L
5052 if (*mnem_p == '.')
5053 dot_p = mnem_p;
29b0f896
AM
5054 mnem_p++;
5055 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
45288df1 5056 {
29b0f896
AM
5057 as_bad (_("no such instruction: `%s'"), token_start);
5058 return NULL;
5059 }
5060 l++;
5061 }
5062 if (!is_space_char (*l)
5063 && *l != END_OF_INSN
e44823cf
JB
5064 && (intel_syntax
5065 || (*l != PREFIX_SEPARATOR
5066 && *l != ',')))
29b0f896
AM
5067 {
5068 as_bad (_("invalid character %s in mnemonic"),
5069 output_invalid (*l));
5070 return NULL;
5071 }
5072 if (token_start == l)
5073 {
e44823cf 5074 if (!intel_syntax && *l == PREFIX_SEPARATOR)
29b0f896
AM
5075 as_bad (_("expecting prefix; got nothing"));
5076 else
5077 as_bad (_("expecting mnemonic; got nothing"));
5078 return NULL;
5079 }
45288df1 5080
29b0f896 5081 /* Look up instruction (or prefix) via hash table. */
629310ab 5082 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
47926f60 5083
29b0f896
AM
5084 if (*l != END_OF_INSN
5085 && (!is_space_char (*l) || l[1] != END_OF_INSN)
5086 && current_templates
40fb9820 5087 && current_templates->start->opcode_modifier.isprefix)
29b0f896 5088 {
c6fb90c8 5089 if (!cpu_flags_check_cpu64 (current_templates->start->cpu_flags))
2dd88dca
JB
5090 {
5091 as_bad ((flag_code != CODE_64BIT
5092 ? _("`%s' is only supported in 64-bit mode")
5093 : _("`%s' is not supported in 64-bit mode")),
5094 current_templates->start->name);
5095 return NULL;
5096 }
29b0f896
AM
5097 /* If we are in 16-bit mode, do not allow addr16 or data16.
5098 Similarly, in 32-bit mode, do not allow addr32 or data32. */
673fe0f0
JB
5099 if ((current_templates->start->opcode_modifier.size == SIZE16
5100 || current_templates->start->opcode_modifier.size == SIZE32)
29b0f896 5101 && flag_code != CODE_64BIT
673fe0f0 5102 && ((current_templates->start->opcode_modifier.size == SIZE32)
29b0f896
AM
5103 ^ (flag_code == CODE_16BIT)))
5104 {
5105 as_bad (_("redundant %s prefix"),
5106 current_templates->start->name);
5107 return NULL;
45288df1 5108 }
31184569
JB
5109
5110 if (current_templates->start->base_opcode == PSEUDO_PREFIX)
29b0f896 5111 {
86fa6981 5112 /* Handle pseudo prefixes. */
31184569 5113 switch (current_templates->start->extension_opcode)
86fa6981 5114 {
41eb8e88 5115 case Prefix_Disp8:
86fa6981
L
5116 /* {disp8} */
5117 i.disp_encoding = disp_encoding_8bit;
5118 break;
41eb8e88
L
5119 case Prefix_Disp16:
5120 /* {disp16} */
5121 i.disp_encoding = disp_encoding_16bit;
5122 break;
5123 case Prefix_Disp32:
86fa6981
L
5124 /* {disp32} */
5125 i.disp_encoding = disp_encoding_32bit;
5126 break;
41eb8e88 5127 case Prefix_Load:
86fa6981
L
5128 /* {load} */
5129 i.dir_encoding = dir_encoding_load;
5130 break;
41eb8e88 5131 case Prefix_Store:
86fa6981
L
5132 /* {store} */
5133 i.dir_encoding = dir_encoding_store;
5134 break;
41eb8e88 5135 case Prefix_VEX:
42e04b36
L
5136 /* {vex} */
5137 i.vec_encoding = vex_encoding_vex;
86fa6981 5138 break;
41eb8e88 5139 case Prefix_VEX3:
86fa6981
L
5140 /* {vex3} */
5141 i.vec_encoding = vex_encoding_vex3;
5142 break;
41eb8e88 5143 case Prefix_EVEX:
86fa6981
L
5144 /* {evex} */
5145 i.vec_encoding = vex_encoding_evex;
5146 break;
41eb8e88 5147 case Prefix_REX:
6b6b6807
L
5148 /* {rex} */
5149 i.rex_encoding = TRUE;
5150 break;
41eb8e88 5151 case Prefix_NoOptimize:
b6f8c7c4
L
5152 /* {nooptimize} */
5153 i.no_optimize = TRUE;
5154 break;
86fa6981
L
5155 default:
5156 abort ();
5157 }
5158 }
5159 else
5160 {
5161 /* Add prefix, checking for repeated prefixes. */
4e9ac44a 5162 switch (add_prefix (current_templates->start->base_opcode))
86fa6981 5163 {
4e9ac44a
L
5164 case PREFIX_EXIST:
5165 return NULL;
5166 case PREFIX_DS:
d777820b 5167 if (current_templates->start->cpu_flags.bitfield.cpuibt)
4e9ac44a
L
5168 i.notrack_prefix = current_templates->start->name;
5169 break;
5170 case PREFIX_REP:
5171 if (current_templates->start->cpu_flags.bitfield.cpuhle)
5172 i.hle_prefix = current_templates->start->name;
5173 else if (current_templates->start->cpu_flags.bitfield.cpumpx)
5174 i.bnd_prefix = current_templates->start->name;
5175 else
5176 i.rep_prefix = current_templates->start->name;
5177 break;
5178 default:
5179 break;
86fa6981 5180 }
29b0f896
AM
5181 }
5182 /* Skip past PREFIX_SEPARATOR and reset token_start. */
5183 token_start = ++l;
5184 }
5185 else
5186 break;
5187 }
45288df1 5188
30a55f88 5189 if (!current_templates)
b6169b20 5190 {
07d5e953
JB
5191 /* Deprecated functionality (new code should use pseudo-prefixes instead):
5192 Check if we should swap operand or force 32bit displacement in
f8a5c266 5193 encoding. */
30a55f88 5194 if (mnem_p - 2 == dot_p && dot_p[1] == 's')
64c49ab3 5195 i.dir_encoding = dir_encoding_swap;
8d63c93e 5196 else if (mnem_p - 3 == dot_p
a501d77e
L
5197 && dot_p[1] == 'd'
5198 && dot_p[2] == '8')
5199 i.disp_encoding = disp_encoding_8bit;
8d63c93e 5200 else if (mnem_p - 4 == dot_p
f8a5c266
L
5201 && dot_p[1] == 'd'
5202 && dot_p[2] == '3'
5203 && dot_p[3] == '2')
a501d77e 5204 i.disp_encoding = disp_encoding_32bit;
30a55f88
L
5205 else
5206 goto check_suffix;
5207 mnem_p = dot_p;
5208 *dot_p = '\0';
629310ab 5209 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
b6169b20
L
5210 }
5211
29b0f896
AM
5212 if (!current_templates)
5213 {
dc1e8a47 5214 check_suffix:
1c529385 5215 if (mnem_p > mnemonic)
29b0f896 5216 {
1c529385
LH
5217 /* See if we can get a match by trimming off a suffix. */
5218 switch (mnem_p[-1])
29b0f896 5219 {
1c529385
LH
5220 case WORD_MNEM_SUFFIX:
5221 if (intel_syntax && (intel_float_operand (mnemonic) & 2))
29b0f896
AM
5222 i.suffix = SHORT_MNEM_SUFFIX;
5223 else
1c529385
LH
5224 /* Fall through. */
5225 case BYTE_MNEM_SUFFIX:
5226 case QWORD_MNEM_SUFFIX:
5227 i.suffix = mnem_p[-1];
29b0f896 5228 mnem_p[-1] = '\0';
fe0e921f
AM
5229 current_templates
5230 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5231 break;
5232 case SHORT_MNEM_SUFFIX:
5233 case LONG_MNEM_SUFFIX:
5234 if (!intel_syntax)
5235 {
5236 i.suffix = mnem_p[-1];
5237 mnem_p[-1] = '\0';
fe0e921f
AM
5238 current_templates
5239 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5240 }
5241 break;
5242
5243 /* Intel Syntax. */
5244 case 'd':
5245 if (intel_syntax)
5246 {
5247 if (intel_float_operand (mnemonic) == 1)
5248 i.suffix = SHORT_MNEM_SUFFIX;
5249 else
5250 i.suffix = LONG_MNEM_SUFFIX;
5251 mnem_p[-1] = '\0';
fe0e921f
AM
5252 current_templates
5253 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5254 }
5255 break;
29b0f896 5256 }
29b0f896 5257 }
1c529385 5258
29b0f896
AM
5259 if (!current_templates)
5260 {
5261 as_bad (_("no such instruction: `%s'"), token_start);
5262 return NULL;
5263 }
5264 }
252b5132 5265
0cfa3eb3
JB
5266 if (current_templates->start->opcode_modifier.jump == JUMP
5267 || current_templates->start->opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
5268 {
5269 /* Check for a branch hint. We allow ",pt" and ",pn" for
5270 predict taken and predict not taken respectively.
5271 I'm not sure that branch hints actually do anything on loop
5272 and jcxz insns (JumpByte) for current Pentium4 chips. They
5273 may work in the future and it doesn't hurt to accept them
5274 now. */
5275 if (l[0] == ',' && l[1] == 'p')
5276 {
5277 if (l[2] == 't')
5278 {
5279 if (!add_prefix (DS_PREFIX_OPCODE))
5280 return NULL;
5281 l += 3;
5282 }
5283 else if (l[2] == 'n')
5284 {
5285 if (!add_prefix (CS_PREFIX_OPCODE))
5286 return NULL;
5287 l += 3;
5288 }
5289 }
5290 }
5291 /* Any other comma loses. */
5292 if (*l == ',')
5293 {
5294 as_bad (_("invalid character %s in mnemonic"),
5295 output_invalid (*l));
5296 return NULL;
5297 }
252b5132 5298
29b0f896 5299 /* Check if instruction is supported on specified architecture. */
5c6af06e
JB
5300 supported = 0;
5301 for (t = current_templates->start; t < current_templates->end; ++t)
5302 {
c0f3af97
L
5303 supported |= cpu_flags_match (t);
5304 if (supported == CPU_FLAGS_PERFECT_MATCH)
548d0ee6
JB
5305 {
5306 if (!cpu_arch_flags.bitfield.cpui386 && (flag_code != CODE_16BIT))
5307 as_warn (_("use .code16 to ensure correct addressing mode"));
3629bb00 5308
548d0ee6
JB
5309 return l;
5310 }
29b0f896 5311 }
3629bb00 5312
548d0ee6
JB
5313 if (!(supported & CPU_FLAGS_64BIT_MATCH))
5314 as_bad (flag_code == CODE_64BIT
5315 ? _("`%s' is not supported in 64-bit mode")
5316 : _("`%s' is only supported in 64-bit mode"),
5317 current_templates->start->name);
5318 else
5319 as_bad (_("`%s' is not supported on `%s%s'"),
5320 current_templates->start->name,
5321 cpu_arch_name ? cpu_arch_name : default_arch,
5322 cpu_sub_arch_name ? cpu_sub_arch_name : "");
252b5132 5323
548d0ee6 5324 return NULL;
29b0f896 5325}
252b5132 5326
29b0f896 5327static char *
e3bb37b5 5328parse_operands (char *l, const char *mnemonic)
29b0f896
AM
5329{
5330 char *token_start;
3138f287 5331
29b0f896
AM
5332 /* 1 if operand is pending after ','. */
5333 unsigned int expecting_operand = 0;
252b5132 5334
29b0f896
AM
5335 /* Non-zero if operand parens not balanced. */
5336 unsigned int paren_not_balanced;
5337
5338 while (*l != END_OF_INSN)
5339 {
5340 /* Skip optional white space before operand. */
5341 if (is_space_char (*l))
5342 ++l;
d02603dc 5343 if (!is_operand_char (*l) && *l != END_OF_INSN && *l != '"')
29b0f896
AM
5344 {
5345 as_bad (_("invalid character %s before operand %d"),
5346 output_invalid (*l),
5347 i.operands + 1);
5348 return NULL;
5349 }
d02603dc 5350 token_start = l; /* After white space. */
29b0f896
AM
5351 paren_not_balanced = 0;
5352 while (paren_not_balanced || *l != ',')
5353 {
5354 if (*l == END_OF_INSN)
5355 {
5356 if (paren_not_balanced)
5357 {
5358 if (!intel_syntax)
5359 as_bad (_("unbalanced parenthesis in operand %d."),
5360 i.operands + 1);
5361 else
5362 as_bad (_("unbalanced brackets in operand %d."),
5363 i.operands + 1);
5364 return NULL;
5365 }
5366 else
5367 break; /* we are done */
5368 }
d02603dc 5369 else if (!is_operand_char (*l) && !is_space_char (*l) && *l != '"')
29b0f896
AM
5370 {
5371 as_bad (_("invalid character %s in operand %d"),
5372 output_invalid (*l),
5373 i.operands + 1);
5374 return NULL;
5375 }
5376 if (!intel_syntax)
5377 {
5378 if (*l == '(')
5379 ++paren_not_balanced;
5380 if (*l == ')')
5381 --paren_not_balanced;
5382 }
5383 else
5384 {
5385 if (*l == '[')
5386 ++paren_not_balanced;
5387 if (*l == ']')
5388 --paren_not_balanced;
5389 }
5390 l++;
5391 }
5392 if (l != token_start)
5393 { /* Yes, we've read in another operand. */
5394 unsigned int operand_ok;
5395 this_operand = i.operands++;
5396 if (i.operands > MAX_OPERANDS)
5397 {
5398 as_bad (_("spurious operands; (%d operands/instruction max)"),
5399 MAX_OPERANDS);
5400 return NULL;
5401 }
9d46ce34 5402 i.types[this_operand].bitfield.unspecified = 1;
29b0f896
AM
5403 /* Now parse operand adding info to 'i' as we go along. */
5404 END_STRING_AND_SAVE (l);
5405
1286ab78
L
5406 if (i.mem_operands > 1)
5407 {
5408 as_bad (_("too many memory references for `%s'"),
5409 mnemonic);
5410 return 0;
5411 }
5412
29b0f896
AM
5413 if (intel_syntax)
5414 operand_ok =
5415 i386_intel_operand (token_start,
5416 intel_float_operand (mnemonic));
5417 else
a7619375 5418 operand_ok = i386_att_operand (token_start);
29b0f896
AM
5419
5420 RESTORE_END_STRING (l);
5421 if (!operand_ok)
5422 return NULL;
5423 }
5424 else
5425 {
5426 if (expecting_operand)
5427 {
5428 expecting_operand_after_comma:
5429 as_bad (_("expecting operand after ','; got nothing"));
5430 return NULL;
5431 }
5432 if (*l == ',')
5433 {
5434 as_bad (_("expecting operand before ','; got nothing"));
5435 return NULL;
5436 }
5437 }
7f3f1ea2 5438
29b0f896
AM
5439 /* Now *l must be either ',' or END_OF_INSN. */
5440 if (*l == ',')
5441 {
5442 if (*++l == END_OF_INSN)
5443 {
5444 /* Just skip it, if it's \n complain. */
5445 goto expecting_operand_after_comma;
5446 }
5447 expecting_operand = 1;
5448 }
5449 }
5450 return l;
5451}
7f3f1ea2 5452
050dfa73 5453static void
783c187b 5454swap_2_operands (unsigned int xchg1, unsigned int xchg2)
050dfa73
MM
5455{
5456 union i386_op temp_op;
40fb9820 5457 i386_operand_type temp_type;
c48dadc9 5458 unsigned int temp_flags;
050dfa73 5459 enum bfd_reloc_code_real temp_reloc;
4eed87de 5460
050dfa73
MM
5461 temp_type = i.types[xchg2];
5462 i.types[xchg2] = i.types[xchg1];
5463 i.types[xchg1] = temp_type;
c48dadc9
JB
5464
5465 temp_flags = i.flags[xchg2];
5466 i.flags[xchg2] = i.flags[xchg1];
5467 i.flags[xchg1] = temp_flags;
5468
050dfa73
MM
5469 temp_op = i.op[xchg2];
5470 i.op[xchg2] = i.op[xchg1];
5471 i.op[xchg1] = temp_op;
c48dadc9 5472
050dfa73
MM
5473 temp_reloc = i.reloc[xchg2];
5474 i.reloc[xchg2] = i.reloc[xchg1];
5475 i.reloc[xchg1] = temp_reloc;
43234a1e 5476
6225c532 5477 if (i.mask.reg)
43234a1e 5478 {
6225c532
JB
5479 if (i.mask.operand == xchg1)
5480 i.mask.operand = xchg2;
5481 else if (i.mask.operand == xchg2)
5482 i.mask.operand = xchg1;
43234a1e 5483 }
5273a3cd 5484 if (i.broadcast.type)
43234a1e 5485 {
5273a3cd
JB
5486 if (i.broadcast.operand == xchg1)
5487 i.broadcast.operand = xchg2;
5488 else if (i.broadcast.operand == xchg2)
5489 i.broadcast.operand = xchg1;
43234a1e 5490 }
ca5312a2 5491 if (i.rounding.type != rc_none)
43234a1e 5492 {
ca5312a2
JB
5493 if (i.rounding.operand == xchg1)
5494 i.rounding.operand = xchg2;
5495 else if (i.rounding.operand == xchg2)
5496 i.rounding.operand = xchg1;
43234a1e 5497 }
050dfa73
MM
5498}
5499
29b0f896 5500static void
e3bb37b5 5501swap_operands (void)
29b0f896 5502{
b7c61d9a 5503 switch (i.operands)
050dfa73 5504 {
c0f3af97 5505 case 5:
b7c61d9a 5506 case 4:
4d456e3d 5507 swap_2_operands (1, i.operands - 2);
1a0670f3 5508 /* Fall through. */
b7c61d9a
L
5509 case 3:
5510 case 2:
4d456e3d 5511 swap_2_operands (0, i.operands - 1);
b7c61d9a
L
5512 break;
5513 default:
5514 abort ();
29b0f896 5515 }
29b0f896
AM
5516
5517 if (i.mem_operands == 2)
5518 {
5519 const seg_entry *temp_seg;
5520 temp_seg = i.seg[0];
5521 i.seg[0] = i.seg[1];
5522 i.seg[1] = temp_seg;
5523 }
5524}
252b5132 5525
29b0f896
AM
5526/* Try to ensure constant immediates are represented in the smallest
5527 opcode possible. */
5528static void
e3bb37b5 5529optimize_imm (void)
29b0f896
AM
5530{
5531 char guess_suffix = 0;
5532 int op;
252b5132 5533
29b0f896
AM
5534 if (i.suffix)
5535 guess_suffix = i.suffix;
5536 else if (i.reg_operands)
5537 {
5538 /* Figure out a suffix from the last register operand specified.
75e5731b
JB
5539 We can't do this properly yet, i.e. excluding special register
5540 instances, but the following works for instructions with
5541 immediates. In any case, we can't set i.suffix yet. */
29b0f896 5542 for (op = i.operands; --op >= 0;)
bab6aec1
JB
5543 if (i.types[op].bitfield.class != Reg)
5544 continue;
5545 else if (i.types[op].bitfield.byte)
7ab9ffdd 5546 {
40fb9820
L
5547 guess_suffix = BYTE_MNEM_SUFFIX;
5548 break;
5549 }
bab6aec1 5550 else if (i.types[op].bitfield.word)
252b5132 5551 {
40fb9820
L
5552 guess_suffix = WORD_MNEM_SUFFIX;
5553 break;
5554 }
bab6aec1 5555 else if (i.types[op].bitfield.dword)
40fb9820
L
5556 {
5557 guess_suffix = LONG_MNEM_SUFFIX;
5558 break;
5559 }
bab6aec1 5560 else if (i.types[op].bitfield.qword)
40fb9820
L
5561 {
5562 guess_suffix = QWORD_MNEM_SUFFIX;
29b0f896 5563 break;
252b5132 5564 }
29b0f896
AM
5565 }
5566 else if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
5567 guess_suffix = WORD_MNEM_SUFFIX;
5568
5569 for (op = i.operands; --op >= 0;)
40fb9820 5570 if (operand_type_check (i.types[op], imm))
29b0f896
AM
5571 {
5572 switch (i.op[op].imms->X_op)
252b5132 5573 {
29b0f896
AM
5574 case O_constant:
5575 /* If a suffix is given, this operand may be shortened. */
5576 switch (guess_suffix)
252b5132 5577 {
29b0f896 5578 case LONG_MNEM_SUFFIX:
40fb9820
L
5579 i.types[op].bitfield.imm32 = 1;
5580 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
5581 break;
5582 case WORD_MNEM_SUFFIX:
40fb9820
L
5583 i.types[op].bitfield.imm16 = 1;
5584 i.types[op].bitfield.imm32 = 1;
5585 i.types[op].bitfield.imm32s = 1;
5586 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
5587 break;
5588 case BYTE_MNEM_SUFFIX:
40fb9820
L
5589 i.types[op].bitfield.imm8 = 1;
5590 i.types[op].bitfield.imm8s = 1;
5591 i.types[op].bitfield.imm16 = 1;
5592 i.types[op].bitfield.imm32 = 1;
5593 i.types[op].bitfield.imm32s = 1;
5594 i.types[op].bitfield.imm64 = 1;
29b0f896 5595 break;
252b5132 5596 }
252b5132 5597
29b0f896
AM
5598 /* If this operand is at most 16 bits, convert it
5599 to a signed 16 bit number before trying to see
5600 whether it will fit in an even smaller size.
5601 This allows a 16-bit operand such as $0xffe0 to
5602 be recognised as within Imm8S range. */
40fb9820 5603 if ((i.types[op].bitfield.imm16)
29b0f896 5604 && (i.op[op].imms->X_add_number & ~(offsetT) 0xffff) == 0)
252b5132 5605 {
29b0f896
AM
5606 i.op[op].imms->X_add_number =
5607 (((i.op[op].imms->X_add_number & 0xffff) ^ 0x8000) - 0x8000);
5608 }
a28def75
L
5609#ifdef BFD64
5610 /* Store 32-bit immediate in 64-bit for 64-bit BFD. */
40fb9820 5611 if ((i.types[op].bitfield.imm32)
29b0f896
AM
5612 && ((i.op[op].imms->X_add_number & ~(((offsetT) 2 << 31) - 1))
5613 == 0))
5614 {
5615 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
5616 ^ ((offsetT) 1 << 31))
5617 - ((offsetT) 1 << 31));
5618 }
a28def75 5619#endif
40fb9820 5620 i.types[op]
c6fb90c8
L
5621 = operand_type_or (i.types[op],
5622 smallest_imm_type (i.op[op].imms->X_add_number));
252b5132 5623
29b0f896
AM
5624 /* We must avoid matching of Imm32 templates when 64bit
5625 only immediate is available. */
5626 if (guess_suffix == QWORD_MNEM_SUFFIX)
40fb9820 5627 i.types[op].bitfield.imm32 = 0;
29b0f896 5628 break;
252b5132 5629
29b0f896
AM
5630 case O_absent:
5631 case O_register:
5632 abort ();
5633
5634 /* Symbols and expressions. */
5635 default:
9cd96992
JB
5636 /* Convert symbolic operand to proper sizes for matching, but don't
5637 prevent matching a set of insns that only supports sizes other
5638 than those matching the insn suffix. */
5639 {
40fb9820 5640 i386_operand_type mask, allowed;
d3ce72d0 5641 const insn_template *t;
9cd96992 5642
0dfbf9d7
L
5643 operand_type_set (&mask, 0);
5644 operand_type_set (&allowed, 0);
40fb9820 5645
4eed87de
AM
5646 for (t = current_templates->start;
5647 t < current_templates->end;
5648 ++t)
bab6aec1
JB
5649 {
5650 allowed = operand_type_or (allowed, t->operand_types[op]);
5651 allowed = operand_type_and (allowed, anyimm);
5652 }
9cd96992
JB
5653 switch (guess_suffix)
5654 {
5655 case QWORD_MNEM_SUFFIX:
40fb9820
L
5656 mask.bitfield.imm64 = 1;
5657 mask.bitfield.imm32s = 1;
9cd96992
JB
5658 break;
5659 case LONG_MNEM_SUFFIX:
40fb9820 5660 mask.bitfield.imm32 = 1;
9cd96992
JB
5661 break;
5662 case WORD_MNEM_SUFFIX:
40fb9820 5663 mask.bitfield.imm16 = 1;
9cd96992
JB
5664 break;
5665 case BYTE_MNEM_SUFFIX:
40fb9820 5666 mask.bitfield.imm8 = 1;
9cd96992
JB
5667 break;
5668 default:
9cd96992
JB
5669 break;
5670 }
c6fb90c8 5671 allowed = operand_type_and (mask, allowed);
0dfbf9d7 5672 if (!operand_type_all_zero (&allowed))
c6fb90c8 5673 i.types[op] = operand_type_and (i.types[op], mask);
9cd96992 5674 }
29b0f896 5675 break;
252b5132 5676 }
29b0f896
AM
5677 }
5678}
47926f60 5679
29b0f896
AM
5680/* Try to use the smallest displacement type too. */
5681static void
e3bb37b5 5682optimize_disp (void)
29b0f896
AM
5683{
5684 int op;
3e73aa7c 5685
29b0f896 5686 for (op = i.operands; --op >= 0;)
40fb9820 5687 if (operand_type_check (i.types[op], disp))
252b5132 5688 {
b300c311 5689 if (i.op[op].disps->X_op == O_constant)
252b5132 5690 {
91d6fa6a 5691 offsetT op_disp = i.op[op].disps->X_add_number;
29b0f896 5692
40fb9820 5693 if (i.types[op].bitfield.disp16
91d6fa6a 5694 && (op_disp & ~(offsetT) 0xffff) == 0)
b300c311
L
5695 {
5696 /* If this operand is at most 16 bits, convert
5697 to a signed 16 bit number and don't use 64bit
5698 displacement. */
91d6fa6a 5699 op_disp = (((op_disp & 0xffff) ^ 0x8000) - 0x8000);
40fb9820 5700 i.types[op].bitfield.disp64 = 0;
b300c311 5701 }
a28def75
L
5702#ifdef BFD64
5703 /* Optimize 64-bit displacement to 32-bit for 64-bit BFD. */
40fb9820 5704 if (i.types[op].bitfield.disp32
91d6fa6a 5705 && (op_disp & ~(((offsetT) 2 << 31) - 1)) == 0)
b300c311
L
5706 {
5707 /* If this operand is at most 32 bits, convert
5708 to a signed 32 bit number and don't use 64bit
5709 displacement. */
91d6fa6a
NC
5710 op_disp &= (((offsetT) 2 << 31) - 1);
5711 op_disp = (op_disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
40fb9820 5712 i.types[op].bitfield.disp64 = 0;
b300c311 5713 }
a28def75 5714#endif
91d6fa6a 5715 if (!op_disp && i.types[op].bitfield.baseindex)
b300c311 5716 {
40fb9820
L
5717 i.types[op].bitfield.disp8 = 0;
5718 i.types[op].bitfield.disp16 = 0;
5719 i.types[op].bitfield.disp32 = 0;
5720 i.types[op].bitfield.disp32s = 0;
5721 i.types[op].bitfield.disp64 = 0;
b300c311
L
5722 i.op[op].disps = 0;
5723 i.disp_operands--;
5724 }
5725 else if (flag_code == CODE_64BIT)
5726 {
91d6fa6a 5727 if (fits_in_signed_long (op_disp))
28a9d8f5 5728 {
40fb9820
L
5729 i.types[op].bitfield.disp64 = 0;
5730 i.types[op].bitfield.disp32s = 1;
28a9d8f5 5731 }
0e1147d9 5732 if (i.prefix[ADDR_PREFIX]
91d6fa6a 5733 && fits_in_unsigned_long (op_disp))
40fb9820 5734 i.types[op].bitfield.disp32 = 1;
b300c311 5735 }
40fb9820
L
5736 if ((i.types[op].bitfield.disp32
5737 || i.types[op].bitfield.disp32s
5738 || i.types[op].bitfield.disp16)
b5014f7a 5739 && fits_in_disp8 (op_disp))
40fb9820 5740 i.types[op].bitfield.disp8 = 1;
252b5132 5741 }
67a4f2b7
AO
5742 else if (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
5743 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL)
5744 {
5745 fix_new_exp (frag_now, frag_more (0) - frag_now->fr_literal, 0,
5746 i.op[op].disps, 0, i.reloc[op]);
40fb9820
L
5747 i.types[op].bitfield.disp8 = 0;
5748 i.types[op].bitfield.disp16 = 0;
5749 i.types[op].bitfield.disp32 = 0;
5750 i.types[op].bitfield.disp32s = 0;
5751 i.types[op].bitfield.disp64 = 0;
67a4f2b7
AO
5752 }
5753 else
b300c311 5754 /* We only support 64bit displacement on constants. */
40fb9820 5755 i.types[op].bitfield.disp64 = 0;
252b5132 5756 }
29b0f896
AM
5757}
5758
4a1b91ea
L
5759/* Return 1 if there is a match in broadcast bytes between operand
5760 GIVEN and instruction template T. */
5761
5762static INLINE int
5763match_broadcast_size (const insn_template *t, unsigned int given)
5764{
5765 return ((t->opcode_modifier.broadcast == BYTE_BROADCAST
5766 && i.types[given].bitfield.byte)
5767 || (t->opcode_modifier.broadcast == WORD_BROADCAST
5768 && i.types[given].bitfield.word)
5769 || (t->opcode_modifier.broadcast == DWORD_BROADCAST
5770 && i.types[given].bitfield.dword)
5771 || (t->opcode_modifier.broadcast == QWORD_BROADCAST
5772 && i.types[given].bitfield.qword));
5773}
5774
6c30d220
L
5775/* Check if operands are valid for the instruction. */
5776
5777static int
5778check_VecOperands (const insn_template *t)
5779{
43234a1e 5780 unsigned int op;
e2195274 5781 i386_cpu_flags cpu;
e2195274
JB
5782
5783 /* Templates allowing for ZMMword as well as YMMword and/or XMMword for
5784 any one operand are implicity requiring AVX512VL support if the actual
5785 operand size is YMMword or XMMword. Since this function runs after
5786 template matching, there's no need to check for YMMword/XMMword in
5787 the template. */
5788 cpu = cpu_flags_and (t->cpu_flags, avx512);
5789 if (!cpu_flags_all_zero (&cpu)
5790 && !t->cpu_flags.bitfield.cpuavx512vl
5791 && !cpu_arch_flags.bitfield.cpuavx512vl)
5792 {
5793 for (op = 0; op < t->operands; ++op)
5794 {
5795 if (t->operand_types[op].bitfield.zmmword
5796 && (i.types[op].bitfield.ymmword
5797 || i.types[op].bitfield.xmmword))
5798 {
5799 i.error = unsupported;
5800 return 1;
5801 }
5802 }
5803 }
43234a1e 5804
6c30d220 5805 /* Without VSIB byte, we can't have a vector register for index. */
63112cd6 5806 if (!t->opcode_modifier.sib
6c30d220 5807 && i.index_reg
1b54b8d7
JB
5808 && (i.index_reg->reg_type.bitfield.xmmword
5809 || i.index_reg->reg_type.bitfield.ymmword
5810 || i.index_reg->reg_type.bitfield.zmmword))
6c30d220
L
5811 {
5812 i.error = unsupported_vector_index_register;
5813 return 1;
5814 }
5815
ad8ecc81
MZ
5816 /* Check if default mask is allowed. */
5817 if (t->opcode_modifier.nodefmask
6225c532 5818 && (!i.mask.reg || i.mask.reg->reg_num == 0))
ad8ecc81
MZ
5819 {
5820 i.error = no_default_mask;
5821 return 1;
5822 }
5823
7bab8ab5
JB
5824 /* For VSIB byte, we need a vector register for index, and all vector
5825 registers must be distinct. */
260cd341 5826 if (t->opcode_modifier.sib && t->opcode_modifier.sib != SIBMEM)
7bab8ab5
JB
5827 {
5828 if (!i.index_reg
63112cd6 5829 || !((t->opcode_modifier.sib == VECSIB128
1b54b8d7 5830 && i.index_reg->reg_type.bitfield.xmmword)
63112cd6 5831 || (t->opcode_modifier.sib == VECSIB256
1b54b8d7 5832 && i.index_reg->reg_type.bitfield.ymmword)
63112cd6 5833 || (t->opcode_modifier.sib == VECSIB512
1b54b8d7 5834 && i.index_reg->reg_type.bitfield.zmmword)))
7bab8ab5
JB
5835 {
5836 i.error = invalid_vsib_address;
5837 return 1;
5838 }
5839
6225c532
JB
5840 gas_assert (i.reg_operands == 2 || i.mask.reg);
5841 if (i.reg_operands == 2 && !i.mask.reg)
43234a1e 5842 {
3528c362 5843 gas_assert (i.types[0].bitfield.class == RegSIMD);
1b54b8d7
JB
5844 gas_assert (i.types[0].bitfield.xmmword
5845 || i.types[0].bitfield.ymmword);
3528c362 5846 gas_assert (i.types[2].bitfield.class == RegSIMD);
1b54b8d7
JB
5847 gas_assert (i.types[2].bitfield.xmmword
5848 || i.types[2].bitfield.ymmword);
43234a1e
L
5849 if (operand_check == check_none)
5850 return 0;
5851 if (register_number (i.op[0].regs)
5852 != register_number (i.index_reg)
5853 && register_number (i.op[2].regs)
5854 != register_number (i.index_reg)
5855 && register_number (i.op[0].regs)
5856 != register_number (i.op[2].regs))
5857 return 0;
5858 if (operand_check == check_error)
5859 {
5860 i.error = invalid_vector_register_set;
5861 return 1;
5862 }
5863 as_warn (_("mask, index, and destination registers should be distinct"));
5864 }
6225c532 5865 else if (i.reg_operands == 1 && i.mask.reg)
8444f82a 5866 {
3528c362 5867 if (i.types[1].bitfield.class == RegSIMD
1b54b8d7
JB
5868 && (i.types[1].bitfield.xmmword
5869 || i.types[1].bitfield.ymmword
5870 || i.types[1].bitfield.zmmword)
8444f82a
MZ
5871 && (register_number (i.op[1].regs)
5872 == register_number (i.index_reg)))
5873 {
5874 if (operand_check == check_error)
5875 {
5876 i.error = invalid_vector_register_set;
5877 return 1;
5878 }
5879 if (operand_check != check_none)
5880 as_warn (_("index and destination registers should be distinct"));
5881 }
5882 }
43234a1e 5883 }
7bab8ab5 5884
260cd341
LC
5885 /* For AMX instructions with three tmmword operands, all tmmword operand must be
5886 distinct */
5887 if (t->operand_types[0].bitfield.tmmword
5888 && i.reg_operands == 3)
5889 {
5890 if (register_number (i.op[0].regs)
5891 == register_number (i.op[1].regs)
5892 || register_number (i.op[0].regs)
5893 == register_number (i.op[2].regs)
5894 || register_number (i.op[1].regs)
5895 == register_number (i.op[2].regs))
5896 {
5897 i.error = invalid_tmm_register_set;
5898 return 1;
5899 }
5900 }
5901
43234a1e
L
5902 /* Check if broadcast is supported by the instruction and is applied
5903 to the memory operand. */
5273a3cd 5904 if (i.broadcast.type)
43234a1e 5905 {
8e6e0792 5906 i386_operand_type type, overlap;
43234a1e
L
5907
5908 /* Check if specified broadcast is supported in this instruction,
4a1b91ea 5909 and its broadcast bytes match the memory operand. */
5273a3cd 5910 op = i.broadcast.operand;
8e6e0792 5911 if (!t->opcode_modifier.broadcast
c48dadc9 5912 || !(i.flags[op] & Operand_Mem)
c39e5b26 5913 || (!i.types[op].bitfield.unspecified
4a1b91ea 5914 && !match_broadcast_size (t, op)))
43234a1e
L
5915 {
5916 bad_broadcast:
5917 i.error = unsupported_broadcast;
5918 return 1;
5919 }
8e6e0792 5920
5273a3cd
JB
5921 i.broadcast.bytes = ((1 << (t->opcode_modifier.broadcast - 1))
5922 * i.broadcast.type);
8e6e0792 5923 operand_type_set (&type, 0);
5273a3cd 5924 switch (i.broadcast.bytes)
8e6e0792 5925 {
4a1b91ea
L
5926 case 2:
5927 type.bitfield.word = 1;
5928 break;
5929 case 4:
5930 type.bitfield.dword = 1;
5931 break;
8e6e0792
JB
5932 case 8:
5933 type.bitfield.qword = 1;
5934 break;
5935 case 16:
5936 type.bitfield.xmmword = 1;
5937 break;
5938 case 32:
5939 type.bitfield.ymmword = 1;
5940 break;
5941 case 64:
5942 type.bitfield.zmmword = 1;
5943 break;
5944 default:
5945 goto bad_broadcast;
5946 }
5947
5948 overlap = operand_type_and (type, t->operand_types[op]);
bc49bfd8
JB
5949 if (t->operand_types[op].bitfield.class == RegSIMD
5950 && t->operand_types[op].bitfield.byte
5951 + t->operand_types[op].bitfield.word
5952 + t->operand_types[op].bitfield.dword
5953 + t->operand_types[op].bitfield.qword > 1)
5954 {
5955 overlap.bitfield.xmmword = 0;
5956 overlap.bitfield.ymmword = 0;
5957 overlap.bitfield.zmmword = 0;
5958 }
8e6e0792
JB
5959 if (operand_type_all_zero (&overlap))
5960 goto bad_broadcast;
5961
5962 if (t->opcode_modifier.checkregsize)
5963 {
5964 unsigned int j;
5965
e2195274 5966 type.bitfield.baseindex = 1;
8e6e0792
JB
5967 for (j = 0; j < i.operands; ++j)
5968 {
5969 if (j != op
5970 && !operand_type_register_match(i.types[j],
5971 t->operand_types[j],
5972 type,
5973 t->operand_types[op]))
5974 goto bad_broadcast;
5975 }
5976 }
43234a1e
L
5977 }
5978 /* If broadcast is supported in this instruction, we need to check if
5979 operand of one-element size isn't specified without broadcast. */
5980 else if (t->opcode_modifier.broadcast && i.mem_operands)
5981 {
5982 /* Find memory operand. */
5983 for (op = 0; op < i.operands; op++)
8dc0818e 5984 if (i.flags[op] & Operand_Mem)
43234a1e
L
5985 break;
5986 gas_assert (op < i.operands);
5987 /* Check size of the memory operand. */
4a1b91ea 5988 if (match_broadcast_size (t, op))
43234a1e
L
5989 {
5990 i.error = broadcast_needed;
5991 return 1;
5992 }
5993 }
c39e5b26
JB
5994 else
5995 op = MAX_OPERANDS - 1; /* Avoid uninitialized variable warning. */
43234a1e
L
5996
5997 /* Check if requested masking is supported. */
6225c532 5998 if (i.mask.reg)
43234a1e 5999 {
ae2387fe
JB
6000 switch (t->opcode_modifier.masking)
6001 {
6002 case BOTH_MASKING:
6003 break;
6004 case MERGING_MASKING:
6225c532 6005 if (i.mask.zeroing)
ae2387fe
JB
6006 {
6007 case 0:
6008 i.error = unsupported_masking;
6009 return 1;
6010 }
6011 break;
6012 case DYNAMIC_MASKING:
6013 /* Memory destinations allow only merging masking. */
6225c532 6014 if (i.mask.zeroing && i.mem_operands)
ae2387fe
JB
6015 {
6016 /* Find memory operand. */
6017 for (op = 0; op < i.operands; op++)
c48dadc9 6018 if (i.flags[op] & Operand_Mem)
ae2387fe
JB
6019 break;
6020 gas_assert (op < i.operands);
6021 if (op == i.operands - 1)
6022 {
6023 i.error = unsupported_masking;
6024 return 1;
6025 }
6026 }
6027 break;
6028 default:
6029 abort ();
6030 }
43234a1e
L
6031 }
6032
6033 /* Check if masking is applied to dest operand. */
6225c532 6034 if (i.mask.reg && (i.mask.operand != i.operands - 1))
43234a1e
L
6035 {
6036 i.error = mask_not_on_destination;
6037 return 1;
6038 }
6039
43234a1e 6040 /* Check RC/SAE. */
ca5312a2 6041 if (i.rounding.type != rc_none)
43234a1e 6042 {
a80195f1 6043 if (!t->opcode_modifier.sae
ca5312a2 6044 || (i.rounding.type != saeonly && !t->opcode_modifier.staticrounding))
43234a1e
L
6045 {
6046 i.error = unsupported_rc_sae;
6047 return 1;
6048 }
6049 /* If the instruction has several immediate operands and one of
6050 them is rounding, the rounding operand should be the last
6051 immediate operand. */
6052 if (i.imm_operands > 1
ca5312a2 6053 && i.rounding.operand != i.imm_operands - 1)
7bab8ab5 6054 {
43234a1e 6055 i.error = rc_sae_operand_not_last_imm;
7bab8ab5
JB
6056 return 1;
6057 }
6c30d220
L
6058 }
6059
da4977e0
JB
6060 /* Check the special Imm4 cases; must be the first operand. */
6061 if (t->cpu_flags.bitfield.cpuxop && t->operands == 5)
6062 {
6063 if (i.op[0].imms->X_op != O_constant
6064 || !fits_in_imm4 (i.op[0].imms->X_add_number))
6065 {
6066 i.error = bad_imm4;
6067 return 1;
6068 }
6069
6070 /* Turn off Imm<N> so that update_imm won't complain. */
6071 operand_type_set (&i.types[0], 0);
6072 }
6073
43234a1e 6074 /* Check vector Disp8 operand. */
b5014f7a
JB
6075 if (t->opcode_modifier.disp8memshift
6076 && i.disp_encoding != disp_encoding_32bit)
43234a1e 6077 {
5273a3cd 6078 if (i.broadcast.type)
4a1b91ea 6079 i.memshift = t->opcode_modifier.broadcast - 1;
7091c612 6080 else if (t->opcode_modifier.disp8memshift != DISP8_SHIFT_VL)
43234a1e 6081 i.memshift = t->opcode_modifier.disp8memshift;
7091c612
JB
6082 else
6083 {
6084 const i386_operand_type *type = NULL;
6085
6086 i.memshift = 0;
6087 for (op = 0; op < i.operands; op++)
8dc0818e 6088 if (i.flags[op] & Operand_Mem)
7091c612 6089 {
4174bfff
JB
6090 if (t->opcode_modifier.evex == EVEXLIG)
6091 i.memshift = 2 + (i.suffix == QWORD_MNEM_SUFFIX);
6092 else if (t->operand_types[op].bitfield.xmmword
6093 + t->operand_types[op].bitfield.ymmword
6094 + t->operand_types[op].bitfield.zmmword <= 1)
7091c612
JB
6095 type = &t->operand_types[op];
6096 else if (!i.types[op].bitfield.unspecified)
6097 type = &i.types[op];
6098 }
3528c362 6099 else if (i.types[op].bitfield.class == RegSIMD
4174bfff 6100 && t->opcode_modifier.evex != EVEXLIG)
7091c612
JB
6101 {
6102 if (i.types[op].bitfield.zmmword)
6103 i.memshift = 6;
6104 else if (i.types[op].bitfield.ymmword && i.memshift < 5)
6105 i.memshift = 5;
6106 else if (i.types[op].bitfield.xmmword && i.memshift < 4)
6107 i.memshift = 4;
6108 }
6109
6110 if (type)
6111 {
6112 if (type->bitfield.zmmword)
6113 i.memshift = 6;
6114 else if (type->bitfield.ymmword)
6115 i.memshift = 5;
6116 else if (type->bitfield.xmmword)
6117 i.memshift = 4;
6118 }
6119
6120 /* For the check in fits_in_disp8(). */
6121 if (i.memshift == 0)
6122 i.memshift = -1;
6123 }
43234a1e
L
6124
6125 for (op = 0; op < i.operands; op++)
6126 if (operand_type_check (i.types[op], disp)
6127 && i.op[op].disps->X_op == O_constant)
6128 {
b5014f7a 6129 if (fits_in_disp8 (i.op[op].disps->X_add_number))
43234a1e 6130 {
b5014f7a
JB
6131 i.types[op].bitfield.disp8 = 1;
6132 return 0;
43234a1e 6133 }
b5014f7a 6134 i.types[op].bitfield.disp8 = 0;
43234a1e
L
6135 }
6136 }
b5014f7a
JB
6137
6138 i.memshift = 0;
43234a1e 6139
6c30d220
L
6140 return 0;
6141}
6142
da4977e0 6143/* Check if encoding requirements are met by the instruction. */
a683cc34
SP
6144
6145static int
da4977e0 6146VEX_check_encoding (const insn_template *t)
a683cc34 6147{
da4977e0
JB
6148 if (i.vec_encoding == vex_encoding_error)
6149 {
6150 i.error = unsupported;
6151 return 1;
6152 }
6153
86fa6981 6154 if (i.vec_encoding == vex_encoding_evex)
43234a1e 6155 {
86fa6981 6156 /* This instruction must be encoded with EVEX prefix. */
e771e7c9 6157 if (!is_evex_encoding (t))
86fa6981
L
6158 {
6159 i.error = unsupported;
6160 return 1;
6161 }
6162 return 0;
43234a1e
L
6163 }
6164
a683cc34 6165 if (!t->opcode_modifier.vex)
86fa6981
L
6166 {
6167 /* This instruction template doesn't have VEX prefix. */
6168 if (i.vec_encoding != vex_encoding_default)
6169 {
6170 i.error = unsupported;
6171 return 1;
6172 }
6173 return 0;
6174 }
a683cc34 6175
a683cc34
SP
6176 return 0;
6177}
6178
d3ce72d0 6179static const insn_template *
83b16ac6 6180match_template (char mnem_suffix)
29b0f896
AM
6181{
6182 /* Points to template once we've found it. */
d3ce72d0 6183 const insn_template *t;
40fb9820 6184 i386_operand_type overlap0, overlap1, overlap2, overlap3;
c0f3af97 6185 i386_operand_type overlap4;
29b0f896 6186 unsigned int found_reverse_match;
dc2be329 6187 i386_opcode_modifier suffix_check;
40fb9820 6188 i386_operand_type operand_types [MAX_OPERANDS];
539e75ad 6189 int addr_prefix_disp;
45a4bb20 6190 unsigned int j, size_match, check_register;
5614d22c 6191 enum i386_error specific_error = 0;
29b0f896 6192
c0f3af97
L
6193#if MAX_OPERANDS != 5
6194# error "MAX_OPERANDS must be 5."
f48ff2ae
L
6195#endif
6196
29b0f896 6197 found_reverse_match = 0;
539e75ad 6198 addr_prefix_disp = -1;
40fb9820 6199
dc2be329 6200 /* Prepare for mnemonic suffix check. */
40fb9820 6201 memset (&suffix_check, 0, sizeof (suffix_check));
dc2be329
L
6202 switch (mnem_suffix)
6203 {
6204 case BYTE_MNEM_SUFFIX:
6205 suffix_check.no_bsuf = 1;
6206 break;
6207 case WORD_MNEM_SUFFIX:
6208 suffix_check.no_wsuf = 1;
6209 break;
6210 case SHORT_MNEM_SUFFIX:
6211 suffix_check.no_ssuf = 1;
6212 break;
6213 case LONG_MNEM_SUFFIX:
6214 suffix_check.no_lsuf = 1;
6215 break;
6216 case QWORD_MNEM_SUFFIX:
6217 suffix_check.no_qsuf = 1;
6218 break;
6219 default:
6220 /* NB: In Intel syntax, normally we can check for memory operand
6221 size when there is no mnemonic suffix. But jmp and call have
6222 2 different encodings with Dword memory operand size, one with
6223 No_ldSuf and the other without. i.suffix is set to
6224 LONG_DOUBLE_MNEM_SUFFIX to skip the one with No_ldSuf. */
6225 if (i.suffix == LONG_DOUBLE_MNEM_SUFFIX)
6226 suffix_check.no_ldsuf = 1;
83b16ac6
JB
6227 }
6228
01559ecc
L
6229 /* Must have right number of operands. */
6230 i.error = number_of_operands_mismatch;
6231
45aa61fe 6232 for (t = current_templates->start; t < current_templates->end; t++)
29b0f896 6233 {
539e75ad 6234 addr_prefix_disp = -1;
dbbc8b7e 6235 found_reverse_match = 0;
539e75ad 6236
29b0f896
AM
6237 if (i.operands != t->operands)
6238 continue;
6239
50aecf8c 6240 /* Check processor support. */
a65babc9 6241 i.error = unsupported;
45a4bb20 6242 if (cpu_flags_match (t) != CPU_FLAGS_PERFECT_MATCH)
50aecf8c
L
6243 continue;
6244
57392598
CL
6245 /* Check Pseudo Prefix. */
6246 i.error = unsupported;
6247 if (t->opcode_modifier.pseudovexprefix
6248 && !(i.vec_encoding == vex_encoding_vex
6249 || i.vec_encoding == vex_encoding_vex3))
6250 continue;
6251
e1d4d893 6252 /* Check AT&T mnemonic. */
a65babc9 6253 i.error = unsupported_with_intel_mnemonic;
e1d4d893 6254 if (intel_mnemonic && t->opcode_modifier.attmnemonic)
1efbbeb4
L
6255 continue;
6256
4b5aaf5f 6257 /* Check AT&T/Intel syntax. */
a65babc9 6258 i.error = unsupported_syntax;
5c07affc 6259 if ((intel_syntax && t->opcode_modifier.attsyntax)
4b5aaf5f 6260 || (!intel_syntax && t->opcode_modifier.intelsyntax))
1efbbeb4
L
6261 continue;
6262
4b5aaf5f
L
6263 /* Check Intel64/AMD64 ISA. */
6264 switch (isa64)
6265 {
6266 default:
6267 /* Default: Don't accept Intel64. */
6268 if (t->opcode_modifier.isa64 == INTEL64)
6269 continue;
6270 break;
6271 case amd64:
6272 /* -mamd64: Don't accept Intel64 and Intel64 only. */
6273 if (t->opcode_modifier.isa64 >= INTEL64)
6274 continue;
6275 break;
6276 case intel64:
6277 /* -mintel64: Don't accept AMD64. */
5990e377 6278 if (t->opcode_modifier.isa64 == AMD64 && flag_code == CODE_64BIT)
4b5aaf5f
L
6279 continue;
6280 break;
6281 }
6282
dc2be329 6283 /* Check the suffix. */
a65babc9 6284 i.error = invalid_instruction_suffix;
dc2be329
L
6285 if ((t->opcode_modifier.no_bsuf && suffix_check.no_bsuf)
6286 || (t->opcode_modifier.no_wsuf && suffix_check.no_wsuf)
6287 || (t->opcode_modifier.no_lsuf && suffix_check.no_lsuf)
6288 || (t->opcode_modifier.no_ssuf && suffix_check.no_ssuf)
6289 || (t->opcode_modifier.no_qsuf && suffix_check.no_qsuf)
6290 || (t->opcode_modifier.no_ldsuf && suffix_check.no_ldsuf))
83b16ac6 6291 continue;
29b0f896 6292
3ac21baa
JB
6293 size_match = operand_size_match (t);
6294 if (!size_match)
7d5e4556 6295 continue;
539e75ad 6296
6f2f06be
JB
6297 /* This is intentionally not
6298
0cfa3eb3 6299 if (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE))
6f2f06be
JB
6300
6301 as the case of a missing * on the operand is accepted (perhaps with
6302 a warning, issued further down). */
0cfa3eb3 6303 if (i.jumpabsolute && t->opcode_modifier.jump != JUMP_ABSOLUTE)
6f2f06be
JB
6304 {
6305 i.error = operand_type_mismatch;
6306 continue;
6307 }
6308
5c07affc
L
6309 for (j = 0; j < MAX_OPERANDS; j++)
6310 operand_types[j] = t->operand_types[j];
6311
e365e234
JB
6312 /* In general, don't allow
6313 - 64-bit operands outside of 64-bit mode,
6314 - 32-bit operands on pre-386. */
4873e243 6315 j = i.imm_operands + (t->operands > i.imm_operands + 1);
e365e234
JB
6316 if (((i.suffix == QWORD_MNEM_SUFFIX
6317 && flag_code != CODE_64BIT
389d00a5
JB
6318 && !(t->opcode_modifier.opcodespace == SPACE_0F
6319 && t->base_opcode == 0xc7
5e74b495 6320 && t->opcode_modifier.opcodeprefix == PREFIX_NONE
8b65b895 6321 && t->extension_opcode == 1) /* cmpxchg8b */)
e365e234
JB
6322 || (i.suffix == LONG_MNEM_SUFFIX
6323 && !cpu_arch_flags.bitfield.cpui386))
45aa61fe 6324 && (intel_syntax
3cd7f3e3 6325 ? (t->opcode_modifier.mnemonicsize != IGNORESIZE
45aa61fe
AM
6326 && !intel_float_operand (t->name))
6327 : intel_float_operand (t->name) != 2)
4873e243
JB
6328 && (t->operands == i.imm_operands
6329 || (operand_types[i.imm_operands].bitfield.class != RegMMX
6330 && operand_types[i.imm_operands].bitfield.class != RegSIMD
6331 && operand_types[i.imm_operands].bitfield.class != RegMask)
6332 || (operand_types[j].bitfield.class != RegMMX
6333 && operand_types[j].bitfield.class != RegSIMD
6334 && operand_types[j].bitfield.class != RegMask))
63112cd6 6335 && !t->opcode_modifier.sib)
192dc9c6
JB
6336 continue;
6337
29b0f896 6338 /* Do not verify operands when there are none. */
e365e234 6339 if (!t->operands)
da4977e0
JB
6340 {
6341 if (VEX_check_encoding (t))
6342 {
6343 specific_error = i.error;
6344 continue;
6345 }
6346
6347 /* We've found a match; break out of loop. */
6348 break;
6349 }
252b5132 6350
48bcea9f
JB
6351 if (!t->opcode_modifier.jump
6352 || t->opcode_modifier.jump == JUMP_ABSOLUTE)
6353 {
6354 /* There should be only one Disp operand. */
6355 for (j = 0; j < MAX_OPERANDS; j++)
6356 if (operand_type_check (operand_types[j], disp))
539e75ad 6357 break;
48bcea9f
JB
6358 if (j < MAX_OPERANDS)
6359 {
6360 bfd_boolean override = (i.prefix[ADDR_PREFIX] != 0);
6361
6362 addr_prefix_disp = j;
6363
6364 /* Address size prefix will turn Disp64/Disp32S/Disp32/Disp16
6365 operand into Disp32/Disp32/Disp16/Disp32 operand. */
6366 switch (flag_code)
40fb9820 6367 {
48bcea9f
JB
6368 case CODE_16BIT:
6369 override = !override;
6370 /* Fall through. */
6371 case CODE_32BIT:
6372 if (operand_types[j].bitfield.disp32
6373 && operand_types[j].bitfield.disp16)
40fb9820 6374 {
48bcea9f
JB
6375 operand_types[j].bitfield.disp16 = override;
6376 operand_types[j].bitfield.disp32 = !override;
40fb9820 6377 }
48bcea9f
JB
6378 operand_types[j].bitfield.disp32s = 0;
6379 operand_types[j].bitfield.disp64 = 0;
6380 break;
6381
6382 case CODE_64BIT:
6383 if (operand_types[j].bitfield.disp32s
6384 || operand_types[j].bitfield.disp64)
40fb9820 6385 {
48bcea9f
JB
6386 operand_types[j].bitfield.disp64 &= !override;
6387 operand_types[j].bitfield.disp32s &= !override;
6388 operand_types[j].bitfield.disp32 = override;
40fb9820 6389 }
48bcea9f
JB
6390 operand_types[j].bitfield.disp16 = 0;
6391 break;
40fb9820 6392 }
539e75ad 6393 }
48bcea9f 6394 }
539e75ad 6395
02a86693 6396 /* Force 0x8b encoding for "mov foo@GOT, %eax". */
35648716
JB
6397 if (i.reloc[0] == BFD_RELOC_386_GOT32
6398 && t->base_opcode == 0xa0
6399 && t->opcode_modifier.opcodespace == SPACE_BASE)
02a86693
L
6400 continue;
6401
56ffb741 6402 /* We check register size if needed. */
e2195274
JB
6403 if (t->opcode_modifier.checkregsize)
6404 {
6405 check_register = (1 << t->operands) - 1;
5273a3cd
JB
6406 if (i.broadcast.type)
6407 check_register &= ~(1 << i.broadcast.operand);
e2195274
JB
6408 }
6409 else
6410 check_register = 0;
6411
c6fb90c8 6412 overlap0 = operand_type_and (i.types[0], operand_types[0]);
29b0f896
AM
6413 switch (t->operands)
6414 {
6415 case 1:
40fb9820 6416 if (!operand_type_match (overlap0, i.types[0]))
29b0f896
AM
6417 continue;
6418 break;
6419 case 2:
33eaf5de 6420 /* xchg %eax, %eax is a special case. It is an alias for nop
8b38ad71
L
6421 only in 32bit mode and we can use opcode 0x90. In 64bit
6422 mode, we can't use 0x90 for xchg %eax, %eax since it should
6423 zero-extend %eax to %rax. */
6424 if (flag_code == CODE_64BIT
6425 && t->base_opcode == 0x90
35648716 6426 && t->opcode_modifier.opcodespace == SPACE_BASE
75e5731b
JB
6427 && i.types[0].bitfield.instance == Accum
6428 && i.types[0].bitfield.dword
6429 && i.types[1].bitfield.instance == Accum
6430 && i.types[1].bitfield.dword)
8b38ad71 6431 continue;
1212781b
JB
6432 /* xrelease mov %eax, <disp> is another special case. It must not
6433 match the accumulator-only encoding of mov. */
6434 if (flag_code != CODE_64BIT
6435 && i.hle_prefix
6436 && t->base_opcode == 0xa0
35648716 6437 && t->opcode_modifier.opcodespace == SPACE_BASE
75e5731b 6438 && i.types[0].bitfield.instance == Accum
8dc0818e 6439 && (i.flags[1] & Operand_Mem))
1212781b 6440 continue;
f5eb1d70
JB
6441 /* Fall through. */
6442
6443 case 3:
3ac21baa
JB
6444 if (!(size_match & MATCH_STRAIGHT))
6445 goto check_reverse;
64c49ab3
JB
6446 /* Reverse direction of operands if swapping is possible in the first
6447 place (operands need to be symmetric) and
6448 - the load form is requested, and the template is a store form,
6449 - the store form is requested, and the template is a load form,
6450 - the non-default (swapped) form is requested. */
6451 overlap1 = operand_type_and (operand_types[0], operand_types[1]);
f5eb1d70 6452 if (t->opcode_modifier.d && i.reg_operands == i.operands
64c49ab3
JB
6453 && !operand_type_all_zero (&overlap1))
6454 switch (i.dir_encoding)
6455 {
6456 case dir_encoding_load:
6457 if (operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 6458 || t->opcode_modifier.regmem)
64c49ab3
JB
6459 goto check_reverse;
6460 break;
6461
6462 case dir_encoding_store:
6463 if (!operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 6464 && !t->opcode_modifier.regmem)
64c49ab3
JB
6465 goto check_reverse;
6466 break;
6467
6468 case dir_encoding_swap:
6469 goto check_reverse;
6470
6471 case dir_encoding_default:
6472 break;
6473 }
86fa6981 6474 /* If we want store form, we skip the current load. */
64c49ab3
JB
6475 if ((i.dir_encoding == dir_encoding_store
6476 || i.dir_encoding == dir_encoding_swap)
86fa6981
L
6477 && i.mem_operands == 0
6478 && t->opcode_modifier.load)
fa99fab2 6479 continue;
1a0670f3 6480 /* Fall through. */
f48ff2ae 6481 case 4:
c0f3af97 6482 case 5:
c6fb90c8 6483 overlap1 = operand_type_and (i.types[1], operand_types[1]);
40fb9820
L
6484 if (!operand_type_match (overlap0, i.types[0])
6485 || !operand_type_match (overlap1, i.types[1])
e2195274 6486 || ((check_register & 3) == 3
dc821c5f 6487 && !operand_type_register_match (i.types[0],
40fb9820 6488 operand_types[0],
dc821c5f 6489 i.types[1],
40fb9820 6490 operand_types[1])))
29b0f896
AM
6491 {
6492 /* Check if other direction is valid ... */
38e314eb 6493 if (!t->opcode_modifier.d)
29b0f896
AM
6494 continue;
6495
dc1e8a47 6496 check_reverse:
3ac21baa
JB
6497 if (!(size_match & MATCH_REVERSE))
6498 continue;
29b0f896 6499 /* Try reversing direction of operands. */
f5eb1d70
JB
6500 overlap0 = operand_type_and (i.types[0], operand_types[i.operands - 1]);
6501 overlap1 = operand_type_and (i.types[i.operands - 1], operand_types[0]);
40fb9820 6502 if (!operand_type_match (overlap0, i.types[0])
f5eb1d70 6503 || !operand_type_match (overlap1, i.types[i.operands - 1])
45664ddb 6504 || (check_register
dc821c5f 6505 && !operand_type_register_match (i.types[0],
f5eb1d70
JB
6506 operand_types[i.operands - 1],
6507 i.types[i.operands - 1],
45664ddb 6508 operand_types[0])))
29b0f896
AM
6509 {
6510 /* Does not match either direction. */
6511 continue;
6512 }
38e314eb 6513 /* found_reverse_match holds which of D or FloatR
29b0f896 6514 we've found. */
38e314eb
JB
6515 if (!t->opcode_modifier.d)
6516 found_reverse_match = 0;
6517 else if (operand_types[0].bitfield.tbyte)
8a2ed489 6518 found_reverse_match = Opcode_FloatD;
dbbc8b7e 6519 else if (operand_types[0].bitfield.xmmword
f5eb1d70 6520 || operand_types[i.operands - 1].bitfield.xmmword
3528c362
JB
6521 || operand_types[0].bitfield.class == RegMMX
6522 || operand_types[i.operands - 1].bitfield.class == RegMMX
dbbc8b7e
JB
6523 || is_any_vex_encoding(t))
6524 found_reverse_match = (t->base_opcode & 0xee) != 0x6e
6525 ? Opcode_SIMD_FloatD : Opcode_SIMD_IntD;
8a2ed489 6526 else
38e314eb 6527 found_reverse_match = Opcode_D;
40fb9820 6528 if (t->opcode_modifier.floatr)
8a2ed489 6529 found_reverse_match |= Opcode_FloatR;
29b0f896 6530 }
f48ff2ae 6531 else
29b0f896 6532 {
f48ff2ae 6533 /* Found a forward 2 operand match here. */
d1cbb4db
L
6534 switch (t->operands)
6535 {
c0f3af97
L
6536 case 5:
6537 overlap4 = operand_type_and (i.types[4],
6538 operand_types[4]);
1a0670f3 6539 /* Fall through. */
d1cbb4db 6540 case 4:
c6fb90c8
L
6541 overlap3 = operand_type_and (i.types[3],
6542 operand_types[3]);
1a0670f3 6543 /* Fall through. */
d1cbb4db 6544 case 3:
c6fb90c8
L
6545 overlap2 = operand_type_and (i.types[2],
6546 operand_types[2]);
d1cbb4db
L
6547 break;
6548 }
29b0f896 6549
f48ff2ae
L
6550 switch (t->operands)
6551 {
c0f3af97
L
6552 case 5:
6553 if (!operand_type_match (overlap4, i.types[4])
dc821c5f 6554 || !operand_type_register_match (i.types[3],
c0f3af97 6555 operand_types[3],
c0f3af97
L
6556 i.types[4],
6557 operand_types[4]))
6558 continue;
1a0670f3 6559 /* Fall through. */
f48ff2ae 6560 case 4:
40fb9820 6561 if (!operand_type_match (overlap3, i.types[3])
e2195274
JB
6562 || ((check_register & 0xa) == 0xa
6563 && !operand_type_register_match (i.types[1],
f7768225
JB
6564 operand_types[1],
6565 i.types[3],
e2195274
JB
6566 operand_types[3]))
6567 || ((check_register & 0xc) == 0xc
6568 && !operand_type_register_match (i.types[2],
6569 operand_types[2],
6570 i.types[3],
6571 operand_types[3])))
f48ff2ae 6572 continue;
1a0670f3 6573 /* Fall through. */
f48ff2ae
L
6574 case 3:
6575 /* Here we make use of the fact that there are no
23e42951 6576 reverse match 3 operand instructions. */
40fb9820 6577 if (!operand_type_match (overlap2, i.types[2])
e2195274
JB
6578 || ((check_register & 5) == 5
6579 && !operand_type_register_match (i.types[0],
23e42951
JB
6580 operand_types[0],
6581 i.types[2],
e2195274
JB
6582 operand_types[2]))
6583 || ((check_register & 6) == 6
6584 && !operand_type_register_match (i.types[1],
6585 operand_types[1],
6586 i.types[2],
6587 operand_types[2])))
f48ff2ae
L
6588 continue;
6589 break;
6590 }
29b0f896 6591 }
f48ff2ae 6592 /* Found either forward/reverse 2, 3 or 4 operand match here:
29b0f896
AM
6593 slip through to break. */
6594 }
c0f3af97 6595
da4977e0
JB
6596 /* Check if vector operands are valid. */
6597 if (check_VecOperands (t))
6598 {
6599 specific_error = i.error;
6600 continue;
6601 }
6602
6603 /* Check if VEX/EVEX encoding requirements can be satisfied. */
6604 if (VEX_check_encoding (t))
5614d22c
JB
6605 {
6606 specific_error = i.error;
6607 continue;
6608 }
a683cc34 6609
29b0f896
AM
6610 /* We've found a match; break out of loop. */
6611 break;
6612 }
6613
6614 if (t == current_templates->end)
6615 {
6616 /* We found no match. */
a65babc9 6617 const char *err_msg;
5614d22c 6618 switch (specific_error ? specific_error : i.error)
a65babc9
L
6619 {
6620 default:
6621 abort ();
86e026a4 6622 case operand_size_mismatch:
a65babc9
L
6623 err_msg = _("operand size mismatch");
6624 break;
6625 case operand_type_mismatch:
6626 err_msg = _("operand type mismatch");
6627 break;
6628 case register_type_mismatch:
6629 err_msg = _("register type mismatch");
6630 break;
6631 case number_of_operands_mismatch:
6632 err_msg = _("number of operands mismatch");
6633 break;
6634 case invalid_instruction_suffix:
6635 err_msg = _("invalid instruction suffix");
6636 break;
6637 case bad_imm4:
4a2608e3 6638 err_msg = _("constant doesn't fit in 4 bits");
a65babc9 6639 break;
a65babc9
L
6640 case unsupported_with_intel_mnemonic:
6641 err_msg = _("unsupported with Intel mnemonic");
6642 break;
6643 case unsupported_syntax:
6644 err_msg = _("unsupported syntax");
6645 break;
6646 case unsupported:
35262a23 6647 as_bad (_("unsupported instruction `%s'"),
10efe3f6
L
6648 current_templates->start->name);
6649 return NULL;
260cd341
LC
6650 case invalid_sib_address:
6651 err_msg = _("invalid SIB address");
6652 break;
6c30d220
L
6653 case invalid_vsib_address:
6654 err_msg = _("invalid VSIB address");
6655 break;
7bab8ab5
JB
6656 case invalid_vector_register_set:
6657 err_msg = _("mask, index, and destination registers must be distinct");
6658 break;
260cd341
LC
6659 case invalid_tmm_register_set:
6660 err_msg = _("all tmm registers must be distinct");
6661 break;
6c30d220
L
6662 case unsupported_vector_index_register:
6663 err_msg = _("unsupported vector index register");
6664 break;
43234a1e
L
6665 case unsupported_broadcast:
6666 err_msg = _("unsupported broadcast");
6667 break;
43234a1e
L
6668 case broadcast_needed:
6669 err_msg = _("broadcast is needed for operand of such type");
6670 break;
6671 case unsupported_masking:
6672 err_msg = _("unsupported masking");
6673 break;
6674 case mask_not_on_destination:
6675 err_msg = _("mask not on destination operand");
6676 break;
6677 case no_default_mask:
6678 err_msg = _("default mask isn't allowed");
6679 break;
6680 case unsupported_rc_sae:
6681 err_msg = _("unsupported static rounding/sae");
6682 break;
6683 case rc_sae_operand_not_last_imm:
6684 if (intel_syntax)
6685 err_msg = _("RC/SAE operand must precede immediate operands");
6686 else
6687 err_msg = _("RC/SAE operand must follow immediate operands");
6688 break;
6689 case invalid_register_operand:
6690 err_msg = _("invalid register operand");
6691 break;
a65babc9
L
6692 }
6693 as_bad (_("%s for `%s'"), err_msg,
891edac4 6694 current_templates->start->name);
fa99fab2 6695 return NULL;
29b0f896 6696 }
252b5132 6697
29b0f896
AM
6698 if (!quiet_warnings)
6699 {
6700 if (!intel_syntax
0cfa3eb3 6701 && (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE)))
6f2f06be 6702 as_warn (_("indirect %s without `*'"), t->name);
29b0f896 6703
40fb9820 6704 if (t->opcode_modifier.isprefix
3cd7f3e3 6705 && t->opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
6706 {
6707 /* Warn them that a data or address size prefix doesn't
6708 affect assembly of the next line of code. */
6709 as_warn (_("stand-alone `%s' prefix"), t->name);
6710 }
6711 }
6712
6713 /* Copy the template we found. */
9a182d04 6714 install_template (t);
539e75ad
L
6715
6716 if (addr_prefix_disp != -1)
6717 i.tm.operand_types[addr_prefix_disp]
6718 = operand_types[addr_prefix_disp];
6719
29b0f896
AM
6720 if (found_reverse_match)
6721 {
dfd69174
JB
6722 /* If we found a reverse match we must alter the opcode direction
6723 bit and clear/flip the regmem modifier one. found_reverse_match
6724 holds bits to change (different for int & float insns). */
29b0f896
AM
6725
6726 i.tm.base_opcode ^= found_reverse_match;
6727
f5eb1d70
JB
6728 i.tm.operand_types[0] = operand_types[i.operands - 1];
6729 i.tm.operand_types[i.operands - 1] = operand_types[0];
dfd69174
JB
6730
6731 /* Certain SIMD insns have their load forms specified in the opcode
6732 table, and hence we need to _set_ RegMem instead of clearing it.
6733 We need to avoid setting the bit though on insns like KMOVW. */
6734 i.tm.opcode_modifier.regmem
6735 = i.tm.opcode_modifier.modrm && i.tm.opcode_modifier.d
6736 && i.tm.operands > 2U - i.tm.opcode_modifier.sse2avx
6737 && !i.tm.opcode_modifier.regmem;
29b0f896
AM
6738 }
6739
fa99fab2 6740 return t;
29b0f896
AM
6741}
6742
6743static int
e3bb37b5 6744check_string (void)
29b0f896 6745{
51c8edf6
JB
6746 unsigned int es_op = i.tm.opcode_modifier.isstring - IS_STRING_ES_OP0;
6747 unsigned int op = i.tm.operand_types[0].bitfield.baseindex ? es_op : 0;
8dc0818e 6748
51c8edf6 6749 if (i.seg[op] != NULL && i.seg[op] != &es)
29b0f896 6750 {
51c8edf6
JB
6751 as_bad (_("`%s' operand %u must use `%ses' segment"),
6752 i.tm.name,
6753 intel_syntax ? i.tm.operands - es_op : es_op + 1,
6754 register_prefix);
6755 return 0;
29b0f896 6756 }
51c8edf6
JB
6757
6758 /* There's only ever one segment override allowed per instruction.
6759 This instruction possibly has a legal segment override on the
6760 second operand, so copy the segment to where non-string
6761 instructions store it, allowing common code. */
6762 i.seg[op] = i.seg[1];
6763
29b0f896
AM
6764 return 1;
6765}
6766
6767static int
543613e9 6768process_suffix (void)
29b0f896 6769{
389d00a5 6770 bfd_boolean is_crc32 = FALSE, is_movx = FALSE;
8b65b895 6771
29b0f896
AM
6772 /* If matched instruction specifies an explicit instruction mnemonic
6773 suffix, use it. */
673fe0f0 6774 if (i.tm.opcode_modifier.size == SIZE16)
40fb9820 6775 i.suffix = WORD_MNEM_SUFFIX;
673fe0f0 6776 else if (i.tm.opcode_modifier.size == SIZE32)
40fb9820 6777 i.suffix = LONG_MNEM_SUFFIX;
673fe0f0 6778 else if (i.tm.opcode_modifier.size == SIZE64)
40fb9820 6779 i.suffix = QWORD_MNEM_SUFFIX;
13e600d0 6780 else if (i.reg_operands
c8f8eebc
JB
6781 && (i.operands > 1 || i.types[0].bitfield.class == Reg)
6782 && !i.tm.opcode_modifier.addrprefixopreg)
29b0f896 6783 {
65fca059 6784 unsigned int numop = i.operands;
389d00a5
JB
6785
6786 /* MOVSX/MOVZX */
6787 is_movx = (i.tm.opcode_modifier.opcodespace == SPACE_0F
6788 && (i.tm.base_opcode | 8) == 0xbe)
6789 || (i.tm.opcode_modifier.opcodespace == SPACE_BASE
6790 && i.tm.base_opcode == 0x63
6791 && i.tm.cpu_flags.bitfield.cpu64);
6792
8b65b895 6793 /* CRC32 */
389d00a5
JB
6794 is_crc32 = (i.tm.base_opcode == 0xf0
6795 && i.tm.opcode_modifier.opcodespace == SPACE_0F38
8b65b895 6796 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2);
65fca059
JB
6797
6798 /* movsx/movzx want only their source operand considered here, for the
6799 ambiguity checking below. The suffix will be replaced afterwards
6800 to represent the destination (register). */
389d00a5 6801 if (is_movx && (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63))
65fca059
JB
6802 --i.operands;
6803
643bb870 6804 /* crc32 needs REX.W set regardless of suffix / source operand size. */
8b65b895 6805 if (is_crc32 && i.tm.operand_types[1].bitfield.qword)
643bb870
JB
6806 i.rex |= REX_W;
6807
29b0f896 6808 /* If there's no instruction mnemonic suffix we try to invent one
13e600d0 6809 based on GPR operands. */
29b0f896
AM
6810 if (!i.suffix)
6811 {
6812 /* We take i.suffix from the last register operand specified,
6813 Destination register type is more significant than source
381d071f
L
6814 register type. crc32 in SSE4.2 prefers source register
6815 type. */
8b65b895 6816 unsigned int op = is_crc32 ? 1 : i.operands;
20592a94 6817
1a035124
JB
6818 while (op--)
6819 if (i.tm.operand_types[op].bitfield.instance == InstanceNone
6820 || i.tm.operand_types[op].bitfield.instance == Accum)
6821 {
6822 if (i.types[op].bitfield.class != Reg)
6823 continue;
6824 if (i.types[op].bitfield.byte)
6825 i.suffix = BYTE_MNEM_SUFFIX;
6826 else if (i.types[op].bitfield.word)
6827 i.suffix = WORD_MNEM_SUFFIX;
6828 else if (i.types[op].bitfield.dword)
6829 i.suffix = LONG_MNEM_SUFFIX;
6830 else if (i.types[op].bitfield.qword)
6831 i.suffix = QWORD_MNEM_SUFFIX;
6832 else
6833 continue;
6834 break;
6835 }
65fca059
JB
6836
6837 /* As an exception, movsx/movzx silently default to a byte source
6838 in AT&T mode. */
389d00a5 6839 if (is_movx && i.tm.opcode_modifier.w && !i.suffix && !intel_syntax)
65fca059 6840 i.suffix = BYTE_MNEM_SUFFIX;
29b0f896
AM
6841 }
6842 else if (i.suffix == BYTE_MNEM_SUFFIX)
6843 {
2eb952a4 6844 if (intel_syntax
3cd7f3e3 6845 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
2eb952a4
L
6846 && i.tm.opcode_modifier.no_bsuf)
6847 i.suffix = 0;
6848 else if (!check_byte_reg ())
29b0f896
AM
6849 return 0;
6850 }
6851 else if (i.suffix == LONG_MNEM_SUFFIX)
6852 {
2eb952a4 6853 if (intel_syntax
3cd7f3e3 6854 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
9f123b91
JB
6855 && i.tm.opcode_modifier.no_lsuf
6856 && !i.tm.opcode_modifier.todword
6857 && !i.tm.opcode_modifier.toqword)
2eb952a4
L
6858 i.suffix = 0;
6859 else if (!check_long_reg ())
29b0f896
AM
6860 return 0;
6861 }
6862 else if (i.suffix == QWORD_MNEM_SUFFIX)
6863 {
955e1e6a 6864 if (intel_syntax
3cd7f3e3 6865 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
9f123b91
JB
6866 && i.tm.opcode_modifier.no_qsuf
6867 && !i.tm.opcode_modifier.todword
6868 && !i.tm.opcode_modifier.toqword)
955e1e6a
L
6869 i.suffix = 0;
6870 else if (!check_qword_reg ())
29b0f896
AM
6871 return 0;
6872 }
6873 else if (i.suffix == WORD_MNEM_SUFFIX)
6874 {
2eb952a4 6875 if (intel_syntax
3cd7f3e3 6876 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
2eb952a4
L
6877 && i.tm.opcode_modifier.no_wsuf)
6878 i.suffix = 0;
6879 else if (!check_word_reg ())
29b0f896
AM
6880 return 0;
6881 }
3cd7f3e3
L
6882 else if (intel_syntax
6883 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
6884 /* Do nothing if the instruction is going to ignore the prefix. */
6885 ;
6886 else
6887 abort ();
65fca059
JB
6888
6889 /* Undo the movsx/movzx change done above. */
6890 i.operands = numop;
29b0f896 6891 }
3cd7f3e3
L
6892 else if (i.tm.opcode_modifier.mnemonicsize == DEFAULTSIZE
6893 && !i.suffix)
29b0f896 6894 {
13e600d0
JB
6895 i.suffix = stackop_size;
6896 if (stackop_size == LONG_MNEM_SUFFIX)
06f74c5c
L
6897 {
6898 /* stackop_size is set to LONG_MNEM_SUFFIX for the
6899 .code16gcc directive to support 16-bit mode with
6900 32-bit address. For IRET without a suffix, generate
6901 16-bit IRET (opcode 0xcf) to return from an interrupt
6902 handler. */
13e600d0
JB
6903 if (i.tm.base_opcode == 0xcf)
6904 {
6905 i.suffix = WORD_MNEM_SUFFIX;
6906 as_warn (_("generating 16-bit `iret' for .code16gcc directive"));
6907 }
6908 /* Warn about changed behavior for segment register push/pop. */
6909 else if ((i.tm.base_opcode | 1) == 0x07)
6910 as_warn (_("generating 32-bit `%s', unlike earlier gas versions"),
6911 i.tm.name);
06f74c5c 6912 }
29b0f896 6913 }
c006a730 6914 else if (!i.suffix
0cfa3eb3
JB
6915 && (i.tm.opcode_modifier.jump == JUMP_ABSOLUTE
6916 || i.tm.opcode_modifier.jump == JUMP_BYTE
6917 || i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT
389d00a5
JB
6918 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
6919 && i.tm.base_opcode == 0x01 /* [ls][gi]dt */
64e74474 6920 && i.tm.extension_opcode <= 3)))
9306ca4a
JB
6921 {
6922 switch (flag_code)
6923 {
6924 case CODE_64BIT:
40fb9820 6925 if (!i.tm.opcode_modifier.no_qsuf)
9306ca4a 6926 {
828c2a25
JB
6927 if (i.tm.opcode_modifier.jump == JUMP_BYTE
6928 || i.tm.opcode_modifier.no_lsuf)
6929 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a
JB
6930 break;
6931 }
1a0670f3 6932 /* Fall through. */
9306ca4a 6933 case CODE_32BIT:
40fb9820 6934 if (!i.tm.opcode_modifier.no_lsuf)
9306ca4a
JB
6935 i.suffix = LONG_MNEM_SUFFIX;
6936 break;
6937 case CODE_16BIT:
40fb9820 6938 if (!i.tm.opcode_modifier.no_wsuf)
9306ca4a
JB
6939 i.suffix = WORD_MNEM_SUFFIX;
6940 break;
6941 }
6942 }
252b5132 6943
c006a730 6944 if (!i.suffix
3cd7f3e3 6945 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8
JB
6946 /* Also cover lret/retf/iret in 64-bit mode. */
6947 || (flag_code == CODE_64BIT
6948 && !i.tm.opcode_modifier.no_lsuf
6949 && !i.tm.opcode_modifier.no_qsuf))
3cd7f3e3 6950 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
8bbb3ad8
JB
6951 /* Explicit sizing prefixes are assumed to disambiguate insns. */
6952 && !i.prefix[DATA_PREFIX] && !(i.prefix[REX_PREFIX] & REX_W)
62b3f548
JB
6953 /* Accept FLDENV et al without suffix. */
6954 && (i.tm.opcode_modifier.no_ssuf || i.tm.opcode_modifier.floatmf))
29b0f896 6955 {
6c0946d0 6956 unsigned int suffixes, evex = 0;
c006a730
JB
6957
6958 suffixes = !i.tm.opcode_modifier.no_bsuf;
6959 if (!i.tm.opcode_modifier.no_wsuf)
6960 suffixes |= 1 << 1;
6961 if (!i.tm.opcode_modifier.no_lsuf)
6962 suffixes |= 1 << 2;
6963 if (!i.tm.opcode_modifier.no_ldsuf)
6964 suffixes |= 1 << 3;
6965 if (!i.tm.opcode_modifier.no_ssuf)
6966 suffixes |= 1 << 4;
6967 if (flag_code == CODE_64BIT && !i.tm.opcode_modifier.no_qsuf)
6968 suffixes |= 1 << 5;
6969
6c0946d0
JB
6970 /* For [XYZ]MMWORD operands inspect operand sizes. While generally
6971 also suitable for AT&T syntax mode, it was requested that this be
6972 restricted to just Intel syntax. */
5273a3cd 6973 if (intel_syntax && is_any_vex_encoding (&i.tm) && !i.broadcast.type)
6c0946d0 6974 {
b9915cbc 6975 unsigned int op;
6c0946d0 6976
b9915cbc 6977 for (op = 0; op < i.tm.operands; ++op)
6c0946d0 6978 {
b9915cbc
JB
6979 if (is_evex_encoding (&i.tm)
6980 && !cpu_arch_flags.bitfield.cpuavx512vl)
6c0946d0 6981 {
b9915cbc
JB
6982 if (i.tm.operand_types[op].bitfield.ymmword)
6983 i.tm.operand_types[op].bitfield.xmmword = 0;
6984 if (i.tm.operand_types[op].bitfield.zmmword)
6985 i.tm.operand_types[op].bitfield.ymmword = 0;
6986 if (!i.tm.opcode_modifier.evex
6987 || i.tm.opcode_modifier.evex == EVEXDYN)
6988 i.tm.opcode_modifier.evex = EVEX512;
6989 }
6c0946d0 6990
b9915cbc
JB
6991 if (i.tm.operand_types[op].bitfield.xmmword
6992 + i.tm.operand_types[op].bitfield.ymmword
6993 + i.tm.operand_types[op].bitfield.zmmword < 2)
6994 continue;
6c0946d0 6995
b9915cbc
JB
6996 /* Any properly sized operand disambiguates the insn. */
6997 if (i.types[op].bitfield.xmmword
6998 || i.types[op].bitfield.ymmword
6999 || i.types[op].bitfield.zmmword)
7000 {
7001 suffixes &= ~(7 << 6);
7002 evex = 0;
7003 break;
7004 }
6c0946d0 7005
b9915cbc
JB
7006 if ((i.flags[op] & Operand_Mem)
7007 && i.tm.operand_types[op].bitfield.unspecified)
7008 {
7009 if (i.tm.operand_types[op].bitfield.xmmword)
7010 suffixes |= 1 << 6;
7011 if (i.tm.operand_types[op].bitfield.ymmword)
7012 suffixes |= 1 << 7;
7013 if (i.tm.operand_types[op].bitfield.zmmword)
7014 suffixes |= 1 << 8;
7015 if (is_evex_encoding (&i.tm))
7016 evex = EVEX512;
6c0946d0
JB
7017 }
7018 }
7019 }
7020
7021 /* Are multiple suffixes / operand sizes allowed? */
c006a730 7022 if (suffixes & (suffixes - 1))
9306ca4a 7023 {
873494c8 7024 if (intel_syntax
3cd7f3e3 7025 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8 7026 || operand_check == check_error))
9306ca4a 7027 {
c006a730 7028 as_bad (_("ambiguous operand size for `%s'"), i.tm.name);
9306ca4a
JB
7029 return 0;
7030 }
c006a730 7031 if (operand_check == check_error)
9306ca4a 7032 {
c006a730
JB
7033 as_bad (_("no instruction mnemonic suffix given and "
7034 "no register operands; can't size `%s'"), i.tm.name);
9306ca4a
JB
7035 return 0;
7036 }
c006a730 7037 if (operand_check == check_warning)
873494c8
JB
7038 as_warn (_("%s; using default for `%s'"),
7039 intel_syntax
7040 ? _("ambiguous operand size")
7041 : _("no instruction mnemonic suffix given and "
7042 "no register operands"),
7043 i.tm.name);
c006a730
JB
7044
7045 if (i.tm.opcode_modifier.floatmf)
7046 i.suffix = SHORT_MNEM_SUFFIX;
389d00a5 7047 else if (is_movx)
65fca059 7048 /* handled below */;
6c0946d0
JB
7049 else if (evex)
7050 i.tm.opcode_modifier.evex = evex;
c006a730
JB
7051 else if (flag_code == CODE_16BIT)
7052 i.suffix = WORD_MNEM_SUFFIX;
1a035124 7053 else if (!i.tm.opcode_modifier.no_lsuf)
c006a730 7054 i.suffix = LONG_MNEM_SUFFIX;
1a035124
JB
7055 else
7056 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a 7057 }
29b0f896 7058 }
252b5132 7059
389d00a5 7060 if (is_movx)
65fca059
JB
7061 {
7062 /* In Intel syntax, movsx/movzx must have a "suffix" (checked above).
7063 In AT&T syntax, if there is no suffix (warned about above), the default
7064 will be byte extension. */
7065 if (i.tm.opcode_modifier.w && i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
7066 i.tm.base_opcode |= 1;
7067
7068 /* For further processing, the suffix should represent the destination
7069 (register). This is already the case when one was used with
7070 mov[sz][bw]*, but we need to replace it for mov[sz]x, or if there was
7071 no suffix to begin with. */
7072 if (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63 || !i.suffix)
7073 {
7074 if (i.types[1].bitfield.word)
7075 i.suffix = WORD_MNEM_SUFFIX;
7076 else if (i.types[1].bitfield.qword)
7077 i.suffix = QWORD_MNEM_SUFFIX;
7078 else
7079 i.suffix = LONG_MNEM_SUFFIX;
7080
7081 i.tm.opcode_modifier.w = 0;
7082 }
7083 }
7084
50128d0c
JB
7085 if (!i.tm.opcode_modifier.modrm && i.reg_operands && i.tm.operands < 3)
7086 i.short_form = (i.tm.operand_types[0].bitfield.class == Reg)
7087 != (i.tm.operand_types[1].bitfield.class == Reg);
7088
d2224064
JB
7089 /* Change the opcode based on the operand size given by i.suffix. */
7090 switch (i.suffix)
29b0f896 7091 {
d2224064
JB
7092 /* Size floating point instruction. */
7093 case LONG_MNEM_SUFFIX:
7094 if (i.tm.opcode_modifier.floatmf)
7095 {
7096 i.tm.base_opcode ^= 4;
7097 break;
7098 }
7099 /* fall through */
7100 case WORD_MNEM_SUFFIX:
7101 case QWORD_MNEM_SUFFIX:
29b0f896 7102 /* It's not a byte, select word/dword operation. */
40fb9820 7103 if (i.tm.opcode_modifier.w)
29b0f896 7104 {
50128d0c 7105 if (i.short_form)
29b0f896
AM
7106 i.tm.base_opcode |= 8;
7107 else
7108 i.tm.base_opcode |= 1;
7109 }
d2224064
JB
7110 /* fall through */
7111 case SHORT_MNEM_SUFFIX:
29b0f896
AM
7112 /* Now select between word & dword operations via the operand
7113 size prefix, except for instructions that will ignore this
7114 prefix anyway. */
c8f8eebc 7115 if (i.suffix != QWORD_MNEM_SUFFIX
3cd7f3e3 7116 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
c8f8eebc
JB
7117 && !i.tm.opcode_modifier.floatmf
7118 && !is_any_vex_encoding (&i.tm)
7119 && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
7120 || (flag_code == CODE_64BIT
7121 && i.tm.opcode_modifier.jump == JUMP_BYTE)))
24eab124
AM
7122 {
7123 unsigned int prefix = DATA_PREFIX_OPCODE;
543613e9 7124
0cfa3eb3 7125 if (i.tm.opcode_modifier.jump == JUMP_BYTE) /* jcxz, loop */
29b0f896 7126 prefix = ADDR_PREFIX_OPCODE;
252b5132 7127
29b0f896
AM
7128 if (!add_prefix (prefix))
7129 return 0;
24eab124 7130 }
252b5132 7131
29b0f896
AM
7132 /* Set mode64 for an operand. */
7133 if (i.suffix == QWORD_MNEM_SUFFIX
9146926a 7134 && flag_code == CODE_64BIT
d2224064 7135 && !i.tm.opcode_modifier.norex64
4ed21b58 7136 && !i.tm.opcode_modifier.vexw
46e883c5 7137 /* Special case for xchg %rax,%rax. It is NOP and doesn't
d2224064
JB
7138 need rex64. */
7139 && ! (i.operands == 2
7140 && i.tm.base_opcode == 0x90
7141 && i.tm.extension_opcode == None
75e5731b
JB
7142 && i.types[0].bitfield.instance == Accum
7143 && i.types[0].bitfield.qword
7144 && i.types[1].bitfield.instance == Accum
7145 && i.types[1].bitfield.qword))
d2224064 7146 i.rex |= REX_W;
3e73aa7c 7147
d2224064 7148 break;
8bbb3ad8
JB
7149
7150 case 0:
f9a6a8f0 7151 /* Select word/dword/qword operation with explicit data sizing prefix
8bbb3ad8
JB
7152 when there are no suitable register operands. */
7153 if (i.tm.opcode_modifier.w
7154 && (i.prefix[DATA_PREFIX] || (i.prefix[REX_PREFIX] & REX_W))
7155 && (!i.reg_operands
7156 || (i.reg_operands == 1
7157 /* ShiftCount */
7158 && (i.tm.operand_types[0].bitfield.instance == RegC
7159 /* InOutPortReg */
7160 || i.tm.operand_types[0].bitfield.instance == RegD
7161 || i.tm.operand_types[1].bitfield.instance == RegD
7162 /* CRC32 */
8b65b895 7163 || is_crc32))))
8bbb3ad8
JB
7164 i.tm.base_opcode |= 1;
7165 break;
29b0f896 7166 }
7ecd2f8b 7167
c8f8eebc 7168 if (i.tm.opcode_modifier.addrprefixopreg)
c0a30a9f 7169 {
c8f8eebc
JB
7170 gas_assert (!i.suffix);
7171 gas_assert (i.reg_operands);
c0a30a9f 7172
c8f8eebc
JB
7173 if (i.tm.operand_types[0].bitfield.instance == Accum
7174 || i.operands == 1)
7175 {
7176 /* The address size override prefix changes the size of the
7177 first operand. */
7178 if (flag_code == CODE_64BIT
7179 && i.op[0].regs->reg_type.bitfield.word)
7180 {
7181 as_bad (_("16-bit addressing unavailable for `%s'"),
7182 i.tm.name);
7183 return 0;
7184 }
7185
7186 if ((flag_code == CODE_32BIT
7187 ? i.op[0].regs->reg_type.bitfield.word
7188 : i.op[0].regs->reg_type.bitfield.dword)
7189 && !add_prefix (ADDR_PREFIX_OPCODE))
7190 return 0;
7191 }
c0a30a9f
L
7192 else
7193 {
c8f8eebc
JB
7194 /* Check invalid register operand when the address size override
7195 prefix changes the size of register operands. */
7196 unsigned int op;
7197 enum { need_word, need_dword, need_qword } need;
7198
27f13469 7199 /* Check the register operand for the address size prefix if
b3a3496f 7200 the memory operand has no real registers, like symbol, DISP
829f3fe1 7201 or bogus (x32-only) symbol(%rip) when symbol(%eip) is meant. */
27f13469
L
7202 if (i.mem_operands == 1
7203 && i.reg_operands == 1
7204 && i.operands == 2
27f13469 7205 && i.types[1].bitfield.class == Reg
b3a3496f
L
7206 && (flag_code == CODE_32BIT
7207 ? i.op[1].regs->reg_type.bitfield.word
7208 : i.op[1].regs->reg_type.bitfield.dword)
7209 && ((i.base_reg == NULL && i.index_reg == NULL)
829f3fe1
JB
7210#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
7211 || (x86_elf_abi == X86_64_X32_ABI
7212 && i.base_reg
b3a3496f
L
7213 && i.base_reg->reg_num == RegIP
7214 && i.base_reg->reg_type.bitfield.qword))
829f3fe1
JB
7215#else
7216 || 0)
7217#endif
27f13469
L
7218 && !add_prefix (ADDR_PREFIX_OPCODE))
7219 return 0;
7220
c8f8eebc
JB
7221 if (flag_code == CODE_32BIT)
7222 need = i.prefix[ADDR_PREFIX] ? need_word : need_dword;
7223 else if (i.prefix[ADDR_PREFIX])
c0a30a9f
L
7224 need = need_dword;
7225 else
7226 need = flag_code == CODE_64BIT ? need_qword : need_word;
c0a30a9f 7227
c8f8eebc
JB
7228 for (op = 0; op < i.operands; op++)
7229 {
7230 if (i.types[op].bitfield.class != Reg)
7231 continue;
7232
7233 switch (need)
7234 {
7235 case need_word:
7236 if (i.op[op].regs->reg_type.bitfield.word)
7237 continue;
7238 break;
7239 case need_dword:
7240 if (i.op[op].regs->reg_type.bitfield.dword)
7241 continue;
7242 break;
7243 case need_qword:
7244 if (i.op[op].regs->reg_type.bitfield.qword)
7245 continue;
7246 break;
7247 }
7248
7249 as_bad (_("invalid register operand size for `%s'"),
7250 i.tm.name);
7251 return 0;
7252 }
7253 }
c0a30a9f
L
7254 }
7255
29b0f896
AM
7256 return 1;
7257}
3e73aa7c 7258
29b0f896 7259static int
543613e9 7260check_byte_reg (void)
29b0f896
AM
7261{
7262 int op;
543613e9 7263
29b0f896
AM
7264 for (op = i.operands; --op >= 0;)
7265 {
dc821c5f 7266 /* Skip non-register operands. */
bab6aec1 7267 if (i.types[op].bitfield.class != Reg)
dc821c5f
JB
7268 continue;
7269
29b0f896
AM
7270 /* If this is an eight bit register, it's OK. If it's the 16 or
7271 32 bit version of an eight bit register, we will just use the
7272 low portion, and that's OK too. */
dc821c5f 7273 if (i.types[op].bitfield.byte)
29b0f896
AM
7274 continue;
7275
5a819eb9 7276 /* I/O port address operands are OK too. */
75e5731b
JB
7277 if (i.tm.operand_types[op].bitfield.instance == RegD
7278 && i.tm.operand_types[op].bitfield.word)
5a819eb9
JB
7279 continue;
7280
9706160a 7281 /* crc32 only wants its source operand checked here. */
389d00a5
JB
7282 if (i.tm.base_opcode == 0xf0
7283 && i.tm.opcode_modifier.opcodespace == SPACE_0F38
8b65b895
L
7284 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2
7285 && op != 0)
9344ff29
L
7286 continue;
7287
29b0f896 7288 /* Any other register is bad. */
73c76375
JB
7289 as_bad (_("`%s%s' not allowed with `%s%c'"),
7290 register_prefix, i.op[op].regs->reg_name,
7291 i.tm.name, i.suffix);
7292 return 0;
29b0f896
AM
7293 }
7294 return 1;
7295}
7296
7297static int
e3bb37b5 7298check_long_reg (void)
29b0f896
AM
7299{
7300 int op;
7301
7302 for (op = i.operands; --op >= 0;)
dc821c5f 7303 /* Skip non-register operands. */
bab6aec1 7304 if (i.types[op].bitfield.class != Reg)
dc821c5f 7305 continue;
29b0f896
AM
7306 /* Reject eight bit registers, except where the template requires
7307 them. (eg. movzb) */
dc821c5f 7308 else if (i.types[op].bitfield.byte
bab6aec1 7309 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7310 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7311 && (i.tm.operand_types[op].bitfield.word
7312 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7313 {
a540244d
L
7314 as_bad (_("`%s%s' not allowed with `%s%c'"),
7315 register_prefix,
29b0f896
AM
7316 i.op[op].regs->reg_name,
7317 i.tm.name,
7318 i.suffix);
7319 return 0;
7320 }
be4c5e58
L
7321 /* Error if the e prefix on a general reg is missing. */
7322 else if (i.types[op].bitfield.word
bab6aec1 7323 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7324 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7325 && i.tm.operand_types[op].bitfield.dword)
29b0f896 7326 {
be4c5e58
L
7327 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7328 register_prefix, i.op[op].regs->reg_name,
7329 i.suffix);
7330 return 0;
252b5132 7331 }
e4630f71 7332 /* Warn if the r prefix on a general reg is present. */
dc821c5f 7333 else if (i.types[op].bitfield.qword
bab6aec1 7334 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7335 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7336 && i.tm.operand_types[op].bitfield.dword)
252b5132 7337 {
34828aad 7338 if (intel_syntax
65fca059 7339 && i.tm.opcode_modifier.toqword
3528c362 7340 && i.types[0].bitfield.class != RegSIMD)
34828aad 7341 {
ca61edf2 7342 /* Convert to QWORD. We want REX byte. */
34828aad
L
7343 i.suffix = QWORD_MNEM_SUFFIX;
7344 }
7345 else
7346 {
2b5d6a91 7347 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
34828aad
L
7348 register_prefix, i.op[op].regs->reg_name,
7349 i.suffix);
7350 return 0;
7351 }
29b0f896
AM
7352 }
7353 return 1;
7354}
252b5132 7355
29b0f896 7356static int
e3bb37b5 7357check_qword_reg (void)
29b0f896
AM
7358{
7359 int op;
252b5132 7360
29b0f896 7361 for (op = i.operands; --op >= 0; )
dc821c5f 7362 /* Skip non-register operands. */
bab6aec1 7363 if (i.types[op].bitfield.class != Reg)
dc821c5f 7364 continue;
29b0f896
AM
7365 /* Reject eight bit registers, except where the template requires
7366 them. (eg. movzb) */
dc821c5f 7367 else if (i.types[op].bitfield.byte
bab6aec1 7368 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7369 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7370 && (i.tm.operand_types[op].bitfield.word
7371 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7372 {
a540244d
L
7373 as_bad (_("`%s%s' not allowed with `%s%c'"),
7374 register_prefix,
29b0f896
AM
7375 i.op[op].regs->reg_name,
7376 i.tm.name,
7377 i.suffix);
7378 return 0;
7379 }
e4630f71 7380 /* Warn if the r prefix on a general reg is missing. */
dc821c5f
JB
7381 else if ((i.types[op].bitfield.word
7382 || i.types[op].bitfield.dword)
bab6aec1 7383 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7384 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7385 && i.tm.operand_types[op].bitfield.qword)
29b0f896
AM
7386 {
7387 /* Prohibit these changes in the 64bit mode, since the
7388 lowering is more complicated. */
34828aad 7389 if (intel_syntax
ca61edf2 7390 && i.tm.opcode_modifier.todword
3528c362 7391 && i.types[0].bitfield.class != RegSIMD)
34828aad 7392 {
ca61edf2 7393 /* Convert to DWORD. We don't want REX byte. */
34828aad
L
7394 i.suffix = LONG_MNEM_SUFFIX;
7395 }
7396 else
7397 {
2b5d6a91 7398 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
34828aad
L
7399 register_prefix, i.op[op].regs->reg_name,
7400 i.suffix);
7401 return 0;
7402 }
252b5132 7403 }
29b0f896
AM
7404 return 1;
7405}
252b5132 7406
29b0f896 7407static int
e3bb37b5 7408check_word_reg (void)
29b0f896
AM
7409{
7410 int op;
7411 for (op = i.operands; --op >= 0;)
dc821c5f 7412 /* Skip non-register operands. */
bab6aec1 7413 if (i.types[op].bitfield.class != Reg)
dc821c5f 7414 continue;
29b0f896
AM
7415 /* Reject eight bit registers, except where the template requires
7416 them. (eg. movzb) */
dc821c5f 7417 else if (i.types[op].bitfield.byte
bab6aec1 7418 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7419 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7420 && (i.tm.operand_types[op].bitfield.word
7421 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7422 {
a540244d
L
7423 as_bad (_("`%s%s' not allowed with `%s%c'"),
7424 register_prefix,
29b0f896
AM
7425 i.op[op].regs->reg_name,
7426 i.tm.name,
7427 i.suffix);
7428 return 0;
7429 }
9706160a
JB
7430 /* Error if the e or r prefix on a general reg is present. */
7431 else if ((i.types[op].bitfield.dword
dc821c5f 7432 || i.types[op].bitfield.qword)
bab6aec1 7433 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7434 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7435 && i.tm.operand_types[op].bitfield.word)
252b5132 7436 {
9706160a
JB
7437 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7438 register_prefix, i.op[op].regs->reg_name,
7439 i.suffix);
7440 return 0;
29b0f896
AM
7441 }
7442 return 1;
7443}
252b5132 7444
29b0f896 7445static int
40fb9820 7446update_imm (unsigned int j)
29b0f896 7447{
bc0844ae 7448 i386_operand_type overlap = i.types[j];
40fb9820
L
7449 if ((overlap.bitfield.imm8
7450 || overlap.bitfield.imm8s
7451 || overlap.bitfield.imm16
7452 || overlap.bitfield.imm32
7453 || overlap.bitfield.imm32s
7454 || overlap.bitfield.imm64)
0dfbf9d7
L
7455 && !operand_type_equal (&overlap, &imm8)
7456 && !operand_type_equal (&overlap, &imm8s)
7457 && !operand_type_equal (&overlap, &imm16)
7458 && !operand_type_equal (&overlap, &imm32)
7459 && !operand_type_equal (&overlap, &imm32s)
7460 && !operand_type_equal (&overlap, &imm64))
29b0f896
AM
7461 {
7462 if (i.suffix)
7463 {
40fb9820
L
7464 i386_operand_type temp;
7465
0dfbf9d7 7466 operand_type_set (&temp, 0);
7ab9ffdd 7467 if (i.suffix == BYTE_MNEM_SUFFIX)
40fb9820
L
7468 {
7469 temp.bitfield.imm8 = overlap.bitfield.imm8;
7470 temp.bitfield.imm8s = overlap.bitfield.imm8s;
7471 }
7472 else if (i.suffix == WORD_MNEM_SUFFIX)
7473 temp.bitfield.imm16 = overlap.bitfield.imm16;
7474 else if (i.suffix == QWORD_MNEM_SUFFIX)
7475 {
7476 temp.bitfield.imm64 = overlap.bitfield.imm64;
7477 temp.bitfield.imm32s = overlap.bitfield.imm32s;
7478 }
7479 else
7480 temp.bitfield.imm32 = overlap.bitfield.imm32;
7481 overlap = temp;
29b0f896 7482 }
0dfbf9d7
L
7483 else if (operand_type_equal (&overlap, &imm16_32_32s)
7484 || operand_type_equal (&overlap, &imm16_32)
7485 || operand_type_equal (&overlap, &imm16_32s))
29b0f896 7486 {
40fb9820 7487 if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
65da13b5 7488 overlap = imm16;
40fb9820 7489 else
65da13b5 7490 overlap = imm32s;
29b0f896 7491 }
8bbb3ad8
JB
7492 else if (i.prefix[REX_PREFIX] & REX_W)
7493 overlap = operand_type_and (overlap, imm32s);
7494 else if (i.prefix[DATA_PREFIX])
7495 overlap = operand_type_and (overlap,
7496 flag_code != CODE_16BIT ? imm16 : imm32);
0dfbf9d7
L
7497 if (!operand_type_equal (&overlap, &imm8)
7498 && !operand_type_equal (&overlap, &imm8s)
7499 && !operand_type_equal (&overlap, &imm16)
7500 && !operand_type_equal (&overlap, &imm32)
7501 && !operand_type_equal (&overlap, &imm32s)
7502 && !operand_type_equal (&overlap, &imm64))
29b0f896 7503 {
4eed87de
AM
7504 as_bad (_("no instruction mnemonic suffix given; "
7505 "can't determine immediate size"));
29b0f896
AM
7506 return 0;
7507 }
7508 }
40fb9820 7509 i.types[j] = overlap;
29b0f896 7510
40fb9820
L
7511 return 1;
7512}
7513
7514static int
7515finalize_imm (void)
7516{
bc0844ae 7517 unsigned int j, n;
29b0f896 7518
bc0844ae
L
7519 /* Update the first 2 immediate operands. */
7520 n = i.operands > 2 ? 2 : i.operands;
7521 if (n)
7522 {
7523 for (j = 0; j < n; j++)
7524 if (update_imm (j) == 0)
7525 return 0;
40fb9820 7526
bc0844ae
L
7527 /* The 3rd operand can't be immediate operand. */
7528 gas_assert (operand_type_check (i.types[2], imm) == 0);
7529 }
29b0f896
AM
7530
7531 return 1;
7532}
7533
7534static int
e3bb37b5 7535process_operands (void)
29b0f896
AM
7536{
7537 /* Default segment register this instruction will use for memory
7538 accesses. 0 means unknown. This is only for optimizing out
7539 unnecessary segment overrides. */
7540 const seg_entry *default_seg = 0;
7541
a5aeccd9
JB
7542 if (i.tm.opcode_modifier.sse2avx)
7543 {
7544 /* Legacy encoded insns allow explicit REX prefixes, so these prefixes
7545 need converting. */
7546 i.rex |= i.prefix[REX_PREFIX] & (REX_W | REX_R | REX_X | REX_B);
7547 i.prefix[REX_PREFIX] = 0;
7548 i.rex_encoding = 0;
7549 }
c423d21a
JB
7550 /* ImmExt should be processed after SSE2AVX. */
7551 else if (i.tm.opcode_modifier.immext)
7552 process_immext ();
a5aeccd9 7553
2426c15f 7554 if (i.tm.opcode_modifier.sse2avx && i.tm.opcode_modifier.vexvvvv)
29b0f896 7555 {
91d6fa6a
NC
7556 unsigned int dupl = i.operands;
7557 unsigned int dest = dupl - 1;
9fcfb3d7
L
7558 unsigned int j;
7559
c0f3af97 7560 /* The destination must be an xmm register. */
9c2799c2 7561 gas_assert (i.reg_operands
91d6fa6a 7562 && MAX_OPERANDS > dupl
7ab9ffdd 7563 && operand_type_equal (&i.types[dest], &regxmm));
c0f3af97 7564
75e5731b 7565 if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 7566 && i.tm.operand_types[0].bitfield.xmmword)
e2ec9d29 7567 {
8cd7925b 7568 if (i.tm.opcode_modifier.vexsources == VEX3SOURCES)
c0f3af97
L
7569 {
7570 /* Keep xmm0 for instructions with VEX prefix and 3
7571 sources. */
75e5731b 7572 i.tm.operand_types[0].bitfield.instance = InstanceNone;
3528c362 7573 i.tm.operand_types[0].bitfield.class = RegSIMD;
c0f3af97
L
7574 goto duplicate;
7575 }
e2ec9d29 7576 else
c0f3af97
L
7577 {
7578 /* We remove the first xmm0 and keep the number of
7579 operands unchanged, which in fact duplicates the
7580 destination. */
7581 for (j = 1; j < i.operands; j++)
7582 {
7583 i.op[j - 1] = i.op[j];
7584 i.types[j - 1] = i.types[j];
7585 i.tm.operand_types[j - 1] = i.tm.operand_types[j];
8dc0818e 7586 i.flags[j - 1] = i.flags[j];
c0f3af97
L
7587 }
7588 }
7589 }
7590 else if (i.tm.opcode_modifier.implicit1stxmm0)
7ab9ffdd 7591 {
91d6fa6a 7592 gas_assert ((MAX_OPERANDS - 1) > dupl
8cd7925b
L
7593 && (i.tm.opcode_modifier.vexsources
7594 == VEX3SOURCES));
c0f3af97
L
7595
7596 /* Add the implicit xmm0 for instructions with VEX prefix
7597 and 3 sources. */
7598 for (j = i.operands; j > 0; j--)
7599 {
7600 i.op[j] = i.op[j - 1];
7601 i.types[j] = i.types[j - 1];
7602 i.tm.operand_types[j] = i.tm.operand_types[j - 1];
8dc0818e 7603 i.flags[j] = i.flags[j - 1];
c0f3af97
L
7604 }
7605 i.op[0].regs
629310ab 7606 = (const reg_entry *) str_hash_find (reg_hash, "xmm0");
7ab9ffdd 7607 i.types[0] = regxmm;
c0f3af97
L
7608 i.tm.operand_types[0] = regxmm;
7609
7610 i.operands += 2;
7611 i.reg_operands += 2;
7612 i.tm.operands += 2;
7613
91d6fa6a 7614 dupl++;
c0f3af97 7615 dest++;
91d6fa6a
NC
7616 i.op[dupl] = i.op[dest];
7617 i.types[dupl] = i.types[dest];
7618 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 7619 i.flags[dupl] = i.flags[dest];
e2ec9d29 7620 }
c0f3af97
L
7621 else
7622 {
dc1e8a47 7623 duplicate:
c0f3af97
L
7624 i.operands++;
7625 i.reg_operands++;
7626 i.tm.operands++;
7627
91d6fa6a
NC
7628 i.op[dupl] = i.op[dest];
7629 i.types[dupl] = i.types[dest];
7630 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 7631 i.flags[dupl] = i.flags[dest];
c0f3af97
L
7632 }
7633
7634 if (i.tm.opcode_modifier.immext)
7635 process_immext ();
7636 }
75e5731b 7637 else if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 7638 && i.tm.operand_types[0].bitfield.xmmword)
c0f3af97
L
7639 {
7640 unsigned int j;
7641
9fcfb3d7
L
7642 for (j = 1; j < i.operands; j++)
7643 {
7644 i.op[j - 1] = i.op[j];
7645 i.types[j - 1] = i.types[j];
7646
7647 /* We need to adjust fields in i.tm since they are used by
7648 build_modrm_byte. */
7649 i.tm.operand_types [j - 1] = i.tm.operand_types [j];
8dc0818e
JB
7650
7651 i.flags[j - 1] = i.flags[j];
9fcfb3d7
L
7652 }
7653
e2ec9d29
L
7654 i.operands--;
7655 i.reg_operands--;
e2ec9d29
L
7656 i.tm.operands--;
7657 }
920d2ddc
IT
7658 else if (i.tm.opcode_modifier.implicitquadgroup)
7659 {
a477a8c4
JB
7660 unsigned int regnum, first_reg_in_group, last_reg_in_group;
7661
920d2ddc 7662 /* The second operand must be {x,y,z}mmN, where N is a multiple of 4. */
3528c362 7663 gas_assert (i.operands >= 2 && i.types[1].bitfield.class == RegSIMD);
a477a8c4
JB
7664 regnum = register_number (i.op[1].regs);
7665 first_reg_in_group = regnum & ~3;
7666 last_reg_in_group = first_reg_in_group + 3;
7667 if (regnum != first_reg_in_group)
7668 as_warn (_("source register `%s%s' implicitly denotes"
7669 " `%s%.3s%u' to `%s%.3s%u' source group in `%s'"),
7670 register_prefix, i.op[1].regs->reg_name,
7671 register_prefix, i.op[1].regs->reg_name, first_reg_in_group,
7672 register_prefix, i.op[1].regs->reg_name, last_reg_in_group,
7673 i.tm.name);
7674 }
e2ec9d29
L
7675 else if (i.tm.opcode_modifier.regkludge)
7676 {
7677 /* The imul $imm, %reg instruction is converted into
7678 imul $imm, %reg, %reg, and the clr %reg instruction
7679 is converted into xor %reg, %reg. */
7680
7681 unsigned int first_reg_op;
7682
7683 if (operand_type_check (i.types[0], reg))
7684 first_reg_op = 0;
7685 else
7686 first_reg_op = 1;
7687 /* Pretend we saw the extra register operand. */
9c2799c2 7688 gas_assert (i.reg_operands == 1
7ab9ffdd 7689 && i.op[first_reg_op + 1].regs == 0);
e2ec9d29
L
7690 i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
7691 i.types[first_reg_op + 1] = i.types[first_reg_op];
7692 i.operands++;
7693 i.reg_operands++;
29b0f896
AM
7694 }
7695
85b80b0f 7696 if (i.tm.opcode_modifier.modrm)
29b0f896
AM
7697 {
7698 /* The opcode is completed (modulo i.tm.extension_opcode which
52271982
AM
7699 must be put into the modrm byte). Now, we make the modrm and
7700 index base bytes based on all the info we've collected. */
29b0f896
AM
7701
7702 default_seg = build_modrm_byte ();
7703 }
00cee14f 7704 else if (i.types[0].bitfield.class == SReg)
85b80b0f
JB
7705 {
7706 if (flag_code != CODE_64BIT
7707 ? i.tm.base_opcode == POP_SEG_SHORT
7708 && i.op[0].regs->reg_num == 1
389d00a5 7709 : (i.tm.base_opcode | 1) == (POP_SEG386_SHORT & 0xff)
85b80b0f
JB
7710 && i.op[0].regs->reg_num < 4)
7711 {
7712 as_bad (_("you can't `%s %s%s'"),
7713 i.tm.name, register_prefix, i.op[0].regs->reg_name);
7714 return 0;
7715 }
389d00a5
JB
7716 if (i.op[0].regs->reg_num > 3
7717 && i.tm.opcode_modifier.opcodespace == SPACE_BASE )
85b80b0f 7718 {
389d00a5
JB
7719 i.tm.base_opcode ^= (POP_SEG_SHORT ^ POP_SEG386_SHORT) & 0xff;
7720 i.tm.opcode_modifier.opcodespace = SPACE_0F;
85b80b0f
JB
7721 }
7722 i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
7723 }
389d00a5
JB
7724 else if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
7725 && (i.tm.base_opcode & ~3) == MOV_AX_DISP32)
29b0f896
AM
7726 {
7727 default_seg = &ds;
7728 }
40fb9820 7729 else if (i.tm.opcode_modifier.isstring)
29b0f896
AM
7730 {
7731 /* For the string instructions that allow a segment override
7732 on one of their operands, the default segment is ds. */
7733 default_seg = &ds;
7734 }
50128d0c 7735 else if (i.short_form)
85b80b0f
JB
7736 {
7737 /* The register or float register operand is in operand
7738 0 or 1. */
bab6aec1 7739 unsigned int op = i.tm.operand_types[0].bitfield.class != Reg;
85b80b0f
JB
7740
7741 /* Register goes in low 3 bits of opcode. */
7742 i.tm.base_opcode |= i.op[op].regs->reg_num;
7743 if ((i.op[op].regs->reg_flags & RegRex) != 0)
7744 i.rex |= REX_B;
7745 if (!quiet_warnings && i.tm.opcode_modifier.ugh)
7746 {
7747 /* Warn about some common errors, but press on regardless.
7748 The first case can be generated by gcc (<= 2.8.1). */
7749 if (i.operands == 2)
7750 {
7751 /* Reversed arguments on faddp, fsubp, etc. */
7752 as_warn (_("translating to `%s %s%s,%s%s'"), i.tm.name,
7753 register_prefix, i.op[!intel_syntax].regs->reg_name,
7754 register_prefix, i.op[intel_syntax].regs->reg_name);
7755 }
7756 else
7757 {
7758 /* Extraneous `l' suffix on fp insn. */
7759 as_warn (_("translating to `%s %s%s'"), i.tm.name,
7760 register_prefix, i.op[0].regs->reg_name);
7761 }
7762 }
7763 }
29b0f896 7764
514a8bb0 7765 if ((i.seg[0] || i.prefix[SEG_PREFIX])
514a8bb0 7766 && i.tm.base_opcode == 0x8d /* lea */
35648716 7767 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
514a8bb0 7768 && !is_any_vex_encoding(&i.tm))
92334ad2
JB
7769 {
7770 if (!quiet_warnings)
7771 as_warn (_("segment override on `%s' is ineffectual"), i.tm.name);
7772 if (optimize)
7773 {
7774 i.seg[0] = NULL;
7775 i.prefix[SEG_PREFIX] = 0;
7776 }
7777 }
52271982
AM
7778
7779 /* If a segment was explicitly specified, and the specified segment
b6773884
JB
7780 is neither the default nor the one already recorded from a prefix,
7781 use an opcode prefix to select it. If we never figured out what
7782 the default segment is, then default_seg will be zero at this
7783 point, and the specified segment prefix will always be used. */
7784 if (i.seg[0]
7785 && i.seg[0] != default_seg
7786 && i.seg[0]->seg_prefix != i.prefix[SEG_PREFIX])
29b0f896
AM
7787 {
7788 if (!add_prefix (i.seg[0]->seg_prefix))
7789 return 0;
7790 }
7791 return 1;
7792}
7793
a5aeccd9
JB
7794static INLINE void set_rex_vrex (const reg_entry *r, unsigned int rex_bit,
7795 bfd_boolean do_sse2avx)
7796{
7797 if (r->reg_flags & RegRex)
7798 {
7799 if (i.rex & rex_bit)
7800 as_bad (_("same type of prefix used twice"));
7801 i.rex |= rex_bit;
7802 }
7803 else if (do_sse2avx && (i.rex & rex_bit) && i.vex.register_specifier)
7804 {
7805 gas_assert (i.vex.register_specifier == r);
7806 i.vex.register_specifier += 8;
7807 }
7808
7809 if (r->reg_flags & RegVRex)
7810 i.vrex |= rex_bit;
7811}
7812
29b0f896 7813static const seg_entry *
e3bb37b5 7814build_modrm_byte (void)
29b0f896
AM
7815{
7816 const seg_entry *default_seg = 0;
c0f3af97 7817 unsigned int source, dest;
8cd7925b 7818 int vex_3_sources;
c0f3af97 7819
8cd7925b 7820 vex_3_sources = i.tm.opcode_modifier.vexsources == VEX3SOURCES;
c0f3af97
L
7821 if (vex_3_sources)
7822 {
91d6fa6a 7823 unsigned int nds, reg_slot;
4c2c6516 7824 expressionS *exp;
c0f3af97 7825
6b8d3588 7826 dest = i.operands - 1;
c0f3af97 7827 nds = dest - 1;
922d8de8 7828
a683cc34 7829 /* There are 2 kinds of instructions:
bed3d976 7830 1. 5 operands: 4 register operands or 3 register operands
9d3bf266 7831 plus 1 memory operand plus one Imm4 operand, VexXDS, and
bed3d976 7832 VexW0 or VexW1. The destination must be either XMM, YMM or
43234a1e 7833 ZMM register.
bed3d976 7834 2. 4 operands: 4 register operands or 3 register operands
2f1bada2 7835 plus 1 memory operand, with VexXDS. */
922d8de8 7836 gas_assert ((i.reg_operands == 4
bed3d976
JB
7837 || (i.reg_operands == 3 && i.mem_operands == 1))
7838 && i.tm.opcode_modifier.vexvvvv == VEXXDS
dcd7e323 7839 && i.tm.opcode_modifier.vexw
3528c362 7840 && i.tm.operand_types[dest].bitfield.class == RegSIMD);
a683cc34 7841
48db9223
JB
7842 /* If VexW1 is set, the first non-immediate operand is the source and
7843 the second non-immediate one is encoded in the immediate operand. */
7844 if (i.tm.opcode_modifier.vexw == VEXW1)
7845 {
7846 source = i.imm_operands;
7847 reg_slot = i.imm_operands + 1;
7848 }
7849 else
7850 {
7851 source = i.imm_operands + 1;
7852 reg_slot = i.imm_operands;
7853 }
7854
a683cc34 7855 if (i.imm_operands == 0)
bed3d976
JB
7856 {
7857 /* When there is no immediate operand, generate an 8bit
7858 immediate operand to encode the first operand. */
7859 exp = &im_expressions[i.imm_operands++];
7860 i.op[i.operands].imms = exp;
7861 i.types[i.operands] = imm8;
7862 i.operands++;
7863
3528c362 7864 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
bed3d976
JB
7865 exp->X_op = O_constant;
7866 exp->X_add_number = register_number (i.op[reg_slot].regs) << 4;
43234a1e
L
7867 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
7868 }
922d8de8 7869 else
bed3d976 7870 {
9d3bf266
JB
7871 gas_assert (i.imm_operands == 1);
7872 gas_assert (fits_in_imm4 (i.op[0].imms->X_add_number));
7873 gas_assert (!i.tm.opcode_modifier.immext);
a683cc34 7874
9d3bf266
JB
7875 /* Turn on Imm8 again so that output_imm will generate it. */
7876 i.types[0].bitfield.imm8 = 1;
bed3d976 7877
3528c362 7878 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
9d3bf266 7879 i.op[0].imms->X_add_number
bed3d976 7880 |= register_number (i.op[reg_slot].regs) << 4;
43234a1e 7881 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
bed3d976 7882 }
a683cc34 7883
3528c362 7884 gas_assert (i.tm.operand_types[nds].bitfield.class == RegSIMD);
dae39acc 7885 i.vex.register_specifier = i.op[nds].regs;
c0f3af97
L
7886 }
7887 else
7888 source = dest = 0;
29b0f896
AM
7889
7890 /* i.reg_operands MUST be the number of real register operands;
c0f3af97
L
7891 implicit registers do not count. If there are 3 register
7892 operands, it must be a instruction with VexNDS. For a
7893 instruction with VexNDD, the destination register is encoded
7894 in VEX prefix. If there are 4 register operands, it must be
7895 a instruction with VEX prefix and 3 sources. */
7ab9ffdd
L
7896 if (i.mem_operands == 0
7897 && ((i.reg_operands == 2
2426c15f 7898 && i.tm.opcode_modifier.vexvvvv <= VEXXDS)
7ab9ffdd 7899 || (i.reg_operands == 3
2426c15f 7900 && i.tm.opcode_modifier.vexvvvv == VEXXDS)
7ab9ffdd 7901 || (i.reg_operands == 4 && vex_3_sources)))
29b0f896 7902 {
cab737b9
L
7903 switch (i.operands)
7904 {
7905 case 2:
7906 source = 0;
7907 break;
7908 case 3:
c81128dc
L
7909 /* When there are 3 operands, one of them may be immediate,
7910 which may be the first or the last operand. Otherwise,
c0f3af97
L
7911 the first operand must be shift count register (cl) or it
7912 is an instruction with VexNDS. */
9c2799c2 7913 gas_assert (i.imm_operands == 1
7ab9ffdd 7914 || (i.imm_operands == 0
2426c15f 7915 && (i.tm.opcode_modifier.vexvvvv == VEXXDS
75e5731b
JB
7916 || (i.types[0].bitfield.instance == RegC
7917 && i.types[0].bitfield.byte))));
40fb9820 7918 if (operand_type_check (i.types[0], imm)
75e5731b
JB
7919 || (i.types[0].bitfield.instance == RegC
7920 && i.types[0].bitfield.byte))
40fb9820
L
7921 source = 1;
7922 else
7923 source = 0;
cab737b9
L
7924 break;
7925 case 4:
368d64cc
L
7926 /* When there are 4 operands, the first two must be 8bit
7927 immediate operands. The source operand will be the 3rd
c0f3af97
L
7928 one.
7929
7930 For instructions with VexNDS, if the first operand
7931 an imm8, the source operand is the 2nd one. If the last
7932 operand is imm8, the source operand is the first one. */
9c2799c2 7933 gas_assert ((i.imm_operands == 2
7ab9ffdd
L
7934 && i.types[0].bitfield.imm8
7935 && i.types[1].bitfield.imm8)
2426c15f 7936 || (i.tm.opcode_modifier.vexvvvv == VEXXDS
7ab9ffdd
L
7937 && i.imm_operands == 1
7938 && (i.types[0].bitfield.imm8
43234a1e 7939 || i.types[i.operands - 1].bitfield.imm8
ca5312a2 7940 || i.rounding.type != rc_none)));
9f2670f2
L
7941 if (i.imm_operands == 2)
7942 source = 2;
7943 else
c0f3af97
L
7944 {
7945 if (i.types[0].bitfield.imm8)
7946 source = 1;
7947 else
7948 source = 0;
7949 }
c0f3af97
L
7950 break;
7951 case 5:
e771e7c9 7952 if (is_evex_encoding (&i.tm))
43234a1e
L
7953 {
7954 /* For EVEX instructions, when there are 5 operands, the
7955 first one must be immediate operand. If the second one
7956 is immediate operand, the source operand is the 3th
7957 one. If the last one is immediate operand, the source
7958 operand is the 2nd one. */
7959 gas_assert (i.imm_operands == 2
7960 && i.tm.opcode_modifier.sae
7961 && operand_type_check (i.types[0], imm));
7962 if (operand_type_check (i.types[1], imm))
7963 source = 2;
7964 else if (operand_type_check (i.types[4], imm))
7965 source = 1;
7966 else
7967 abort ();
7968 }
cab737b9
L
7969 break;
7970 default:
7971 abort ();
7972 }
7973
c0f3af97
L
7974 if (!vex_3_sources)
7975 {
7976 dest = source + 1;
7977
43234a1e
L
7978 /* RC/SAE operand could be between DEST and SRC. That happens
7979 when one operand is GPR and the other one is XMM/YMM/ZMM
7980 register. */
ca5312a2 7981 if (i.rounding.type != rc_none && i.rounding.operand == dest)
43234a1e
L
7982 dest++;
7983
2426c15f 7984 if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
c0f3af97 7985 {
43234a1e 7986 /* For instructions with VexNDS, the register-only source
c5d0745b 7987 operand must be a 32/64bit integer, XMM, YMM, ZMM, or mask
dfd69174 7988 register. It is encoded in VEX prefix. */
f12dc422
L
7989
7990 i386_operand_type op;
7991 unsigned int vvvv;
7992
c2ecccb3
L
7993 /* Swap two source operands if needed. */
7994 if (i.tm.opcode_modifier.swapsources)
f12dc422
L
7995 {
7996 vvvv = source;
7997 source = dest;
7998 }
7999 else
8000 vvvv = dest;
8001
8002 op = i.tm.operand_types[vvvv];
c0f3af97 8003 if ((dest + 1) >= i.operands
bab6aec1 8004 || ((op.bitfield.class != Reg
dc821c5f 8005 || (!op.bitfield.dword && !op.bitfield.qword))
3528c362 8006 && op.bitfield.class != RegSIMD
43234a1e 8007 && !operand_type_equal (&op, &regmask)))
c0f3af97 8008 abort ();
f12dc422 8009 i.vex.register_specifier = i.op[vvvv].regs;
c0f3af97
L
8010 dest++;
8011 }
8012 }
29b0f896
AM
8013
8014 i.rm.mode = 3;
dfd69174
JB
8015 /* One of the register operands will be encoded in the i.rm.reg
8016 field, the other in the combined i.rm.mode and i.rm.regmem
29b0f896
AM
8017 fields. If no form of this instruction supports a memory
8018 destination operand, then we assume the source operand may
8019 sometimes be a memory operand and so we need to store the
8020 destination in the i.rm.reg field. */
dfd69174 8021 if (!i.tm.opcode_modifier.regmem
40fb9820 8022 && operand_type_check (i.tm.operand_types[dest], anymem) == 0)
29b0f896
AM
8023 {
8024 i.rm.reg = i.op[dest].regs->reg_num;
8025 i.rm.regmem = i.op[source].regs->reg_num;
a5aeccd9
JB
8026 set_rex_vrex (i.op[dest].regs, REX_R, i.tm.opcode_modifier.sse2avx);
8027 set_rex_vrex (i.op[source].regs, REX_B, FALSE);
29b0f896
AM
8028 }
8029 else
8030 {
8031 i.rm.reg = i.op[source].regs->reg_num;
8032 i.rm.regmem = i.op[dest].regs->reg_num;
a5aeccd9
JB
8033 set_rex_vrex (i.op[dest].regs, REX_B, i.tm.opcode_modifier.sse2avx);
8034 set_rex_vrex (i.op[source].regs, REX_R, FALSE);
29b0f896 8035 }
e0c7f900 8036 if (flag_code != CODE_64BIT && (i.rex & REX_R))
c4a530c5 8037 {
4a5c67ed 8038 if (i.types[!i.tm.opcode_modifier.regmem].bitfield.class != RegCR)
c4a530c5 8039 abort ();
e0c7f900 8040 i.rex &= ~REX_R;
c4a530c5
JB
8041 add_prefix (LOCK_PREFIX_OPCODE);
8042 }
29b0f896
AM
8043 }
8044 else
8045 { /* If it's not 2 reg operands... */
c0f3af97
L
8046 unsigned int mem;
8047
29b0f896
AM
8048 if (i.mem_operands)
8049 {
8050 unsigned int fake_zero_displacement = 0;
99018f42 8051 unsigned int op;
4eed87de 8052
7ab9ffdd 8053 for (op = 0; op < i.operands; op++)
8dc0818e 8054 if (i.flags[op] & Operand_Mem)
7ab9ffdd 8055 break;
7ab9ffdd 8056 gas_assert (op < i.operands);
29b0f896 8057
63112cd6 8058 if (i.tm.opcode_modifier.sib)
6c30d220 8059 {
260cd341
LC
8060 /* The index register of VSIB shouldn't be RegIZ. */
8061 if (i.tm.opcode_modifier.sib != SIBMEM
8062 && i.index_reg->reg_num == RegIZ)
6c30d220
L
8063 abort ();
8064
8065 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8066 if (!i.base_reg)
8067 {
8068 i.sib.base = NO_BASE_REGISTER;
8069 i.sib.scale = i.log2_scale_factor;
8070 i.types[op].bitfield.disp8 = 0;
8071 i.types[op].bitfield.disp16 = 0;
8072 i.types[op].bitfield.disp64 = 0;
43083a50 8073 if (flag_code != CODE_64BIT || i.prefix[ADDR_PREFIX])
6c30d220
L
8074 {
8075 /* Must be 32 bit */
8076 i.types[op].bitfield.disp32 = 1;
8077 i.types[op].bitfield.disp32s = 0;
8078 }
8079 else
8080 {
8081 i.types[op].bitfield.disp32 = 0;
8082 i.types[op].bitfield.disp32s = 1;
8083 }
8084 }
260cd341
LC
8085
8086 /* Since the mandatory SIB always has index register, so
8087 the code logic remains unchanged. The non-mandatory SIB
8088 without index register is allowed and will be handled
8089 later. */
8090 if (i.index_reg)
8091 {
8092 if (i.index_reg->reg_num == RegIZ)
8093 i.sib.index = NO_INDEX_REGISTER;
8094 else
8095 i.sib.index = i.index_reg->reg_num;
8096 set_rex_vrex (i.index_reg, REX_X, FALSE);
8097 }
6c30d220
L
8098 }
8099
29b0f896
AM
8100 default_seg = &ds;
8101
8102 if (i.base_reg == 0)
8103 {
8104 i.rm.mode = 0;
8105 if (!i.disp_operands)
9bb129e8 8106 fake_zero_displacement = 1;
29b0f896
AM
8107 if (i.index_reg == 0)
8108 {
73053c1f
JB
8109 i386_operand_type newdisp;
8110
260cd341
LC
8111 /* Both check for VSIB and mandatory non-vector SIB. */
8112 gas_assert (!i.tm.opcode_modifier.sib
8113 || i.tm.opcode_modifier.sib == SIBMEM);
29b0f896 8114 /* Operand is just <disp> */
20f0a1fc 8115 if (flag_code == CODE_64BIT)
29b0f896
AM
8116 {
8117 /* 64bit mode overwrites the 32bit absolute
8118 addressing by RIP relative addressing and
8119 absolute addressing is encoded by one of the
8120 redundant SIB forms. */
8121 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8122 i.sib.base = NO_BASE_REGISTER;
8123 i.sib.index = NO_INDEX_REGISTER;
73053c1f 8124 newdisp = (!i.prefix[ADDR_PREFIX] ? disp32s : disp32);
20f0a1fc 8125 }
fc225355
L
8126 else if ((flag_code == CODE_16BIT)
8127 ^ (i.prefix[ADDR_PREFIX] != 0))
20f0a1fc
NC
8128 {
8129 i.rm.regmem = NO_BASE_REGISTER_16;
73053c1f 8130 newdisp = disp16;
20f0a1fc
NC
8131 }
8132 else
8133 {
8134 i.rm.regmem = NO_BASE_REGISTER;
73053c1f 8135 newdisp = disp32;
29b0f896 8136 }
73053c1f
JB
8137 i.types[op] = operand_type_and_not (i.types[op], anydisp);
8138 i.types[op] = operand_type_or (i.types[op], newdisp);
29b0f896 8139 }
63112cd6 8140 else if (!i.tm.opcode_modifier.sib)
29b0f896 8141 {
6c30d220 8142 /* !i.base_reg && i.index_reg */
e968fc9b 8143 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8144 i.sib.index = NO_INDEX_REGISTER;
8145 else
8146 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8147 i.sib.base = NO_BASE_REGISTER;
8148 i.sib.scale = i.log2_scale_factor;
8149 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
40fb9820
L
8150 i.types[op].bitfield.disp8 = 0;
8151 i.types[op].bitfield.disp16 = 0;
8152 i.types[op].bitfield.disp64 = 0;
43083a50 8153 if (flag_code != CODE_64BIT || i.prefix[ADDR_PREFIX])
40fb9820
L
8154 {
8155 /* Must be 32 bit */
8156 i.types[op].bitfield.disp32 = 1;
8157 i.types[op].bitfield.disp32s = 0;
8158 }
29b0f896 8159 else
40fb9820
L
8160 {
8161 i.types[op].bitfield.disp32 = 0;
8162 i.types[op].bitfield.disp32s = 1;
8163 }
29b0f896 8164 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8165 i.rex |= REX_X;
29b0f896
AM
8166 }
8167 }
8168 /* RIP addressing for 64bit mode. */
e968fc9b 8169 else if (i.base_reg->reg_num == RegIP)
29b0f896 8170 {
63112cd6 8171 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896 8172 i.rm.regmem = NO_BASE_REGISTER;
40fb9820
L
8173 i.types[op].bitfield.disp8 = 0;
8174 i.types[op].bitfield.disp16 = 0;
8175 i.types[op].bitfield.disp32 = 0;
8176 i.types[op].bitfield.disp32s = 1;
8177 i.types[op].bitfield.disp64 = 0;
71903a11 8178 i.flags[op] |= Operand_PCrel;
20f0a1fc
NC
8179 if (! i.disp_operands)
8180 fake_zero_displacement = 1;
29b0f896 8181 }
dc821c5f 8182 else if (i.base_reg->reg_type.bitfield.word)
29b0f896 8183 {
63112cd6 8184 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896
AM
8185 switch (i.base_reg->reg_num)
8186 {
8187 case 3: /* (%bx) */
8188 if (i.index_reg == 0)
8189 i.rm.regmem = 7;
8190 else /* (%bx,%si) -> 0, or (%bx,%di) -> 1 */
8191 i.rm.regmem = i.index_reg->reg_num - 6;
8192 break;
8193 case 5: /* (%bp) */
8194 default_seg = &ss;
8195 if (i.index_reg == 0)
8196 {
8197 i.rm.regmem = 6;
40fb9820 8198 if (operand_type_check (i.types[op], disp) == 0)
29b0f896
AM
8199 {
8200 /* fake (%bp) into 0(%bp) */
41eb8e88 8201 if (i.disp_encoding == disp_encoding_16bit)
1a02d6b0
L
8202 i.types[op].bitfield.disp16 = 1;
8203 else
8204 i.types[op].bitfield.disp8 = 1;
252b5132 8205 fake_zero_displacement = 1;
29b0f896
AM
8206 }
8207 }
8208 else /* (%bp,%si) -> 2, or (%bp,%di) -> 3 */
8209 i.rm.regmem = i.index_reg->reg_num - 6 + 2;
8210 break;
8211 default: /* (%si) -> 4 or (%di) -> 5 */
8212 i.rm.regmem = i.base_reg->reg_num - 6 + 4;
8213 }
41eb8e88
L
8214 if (!fake_zero_displacement
8215 && !i.disp_operands
8216 && i.disp_encoding)
8217 {
8218 fake_zero_displacement = 1;
8219 if (i.disp_encoding == disp_encoding_8bit)
8220 i.types[op].bitfield.disp8 = 1;
8221 else
8222 i.types[op].bitfield.disp16 = 1;
8223 }
29b0f896
AM
8224 i.rm.mode = mode_from_disp_size (i.types[op]);
8225 }
8226 else /* i.base_reg and 32/64 bit mode */
8227 {
8228 if (flag_code == CODE_64BIT
40fb9820
L
8229 && operand_type_check (i.types[op], disp))
8230 {
73053c1f
JB
8231 i.types[op].bitfield.disp16 = 0;
8232 i.types[op].bitfield.disp64 = 0;
40fb9820 8233 if (i.prefix[ADDR_PREFIX] == 0)
73053c1f
JB
8234 {
8235 i.types[op].bitfield.disp32 = 0;
8236 i.types[op].bitfield.disp32s = 1;
8237 }
40fb9820 8238 else
73053c1f
JB
8239 {
8240 i.types[op].bitfield.disp32 = 1;
8241 i.types[op].bitfield.disp32s = 0;
8242 }
40fb9820 8243 }
20f0a1fc 8244
63112cd6 8245 if (!i.tm.opcode_modifier.sib)
6c30d220 8246 i.rm.regmem = i.base_reg->reg_num;
29b0f896 8247 if ((i.base_reg->reg_flags & RegRex) != 0)
161a04f6 8248 i.rex |= REX_B;
29b0f896
AM
8249 i.sib.base = i.base_reg->reg_num;
8250 /* x86-64 ignores REX prefix bit here to avoid decoder
8251 complications. */
848930b2
JB
8252 if (!(i.base_reg->reg_flags & RegRex)
8253 && (i.base_reg->reg_num == EBP_REG_NUM
8254 || i.base_reg->reg_num == ESP_REG_NUM))
29b0f896 8255 default_seg = &ss;
848930b2 8256 if (i.base_reg->reg_num == 5 && i.disp_operands == 0)
29b0f896 8257 {
848930b2 8258 fake_zero_displacement = 1;
1a02d6b0
L
8259 if (i.disp_encoding == disp_encoding_32bit)
8260 i.types[op].bitfield.disp32 = 1;
8261 else
8262 i.types[op].bitfield.disp8 = 1;
29b0f896
AM
8263 }
8264 i.sib.scale = i.log2_scale_factor;
8265 if (i.index_reg == 0)
8266 {
260cd341
LC
8267 /* Only check for VSIB. */
8268 gas_assert (i.tm.opcode_modifier.sib != VECSIB128
8269 && i.tm.opcode_modifier.sib != VECSIB256
8270 && i.tm.opcode_modifier.sib != VECSIB512);
8271
29b0f896
AM
8272 /* <disp>(%esp) becomes two byte modrm with no index
8273 register. We've already stored the code for esp
8274 in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
8275 Any base register besides %esp will not use the
8276 extra modrm byte. */
8277 i.sib.index = NO_INDEX_REGISTER;
29b0f896 8278 }
63112cd6 8279 else if (!i.tm.opcode_modifier.sib)
29b0f896 8280 {
e968fc9b 8281 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8282 i.sib.index = NO_INDEX_REGISTER;
8283 else
8284 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8285 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8286 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8287 i.rex |= REX_X;
29b0f896 8288 }
67a4f2b7
AO
8289
8290 if (i.disp_operands
8291 && (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
8292 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL))
8293 i.rm.mode = 0;
8294 else
a501d77e
L
8295 {
8296 if (!fake_zero_displacement
8297 && !i.disp_operands
8298 && i.disp_encoding)
8299 {
8300 fake_zero_displacement = 1;
8301 if (i.disp_encoding == disp_encoding_8bit)
8302 i.types[op].bitfield.disp8 = 1;
8303 else
8304 i.types[op].bitfield.disp32 = 1;
8305 }
8306 i.rm.mode = mode_from_disp_size (i.types[op]);
8307 }
29b0f896 8308 }
252b5132 8309
29b0f896
AM
8310 if (fake_zero_displacement)
8311 {
8312 /* Fakes a zero displacement assuming that i.types[op]
8313 holds the correct displacement size. */
8314 expressionS *exp;
8315
9c2799c2 8316 gas_assert (i.op[op].disps == 0);
29b0f896
AM
8317 exp = &disp_expressions[i.disp_operands++];
8318 i.op[op].disps = exp;
8319 exp->X_op = O_constant;
8320 exp->X_add_number = 0;
8321 exp->X_add_symbol = (symbolS *) 0;
8322 exp->X_op_symbol = (symbolS *) 0;
8323 }
c0f3af97
L
8324
8325 mem = op;
29b0f896 8326 }
c0f3af97
L
8327 else
8328 mem = ~0;
252b5132 8329
8c43a48b 8330 if (i.tm.opcode_modifier.vexsources == XOP2SOURCES)
5dd85c99
SP
8331 {
8332 if (operand_type_check (i.types[0], imm))
8333 i.vex.register_specifier = NULL;
8334 else
8335 {
8336 /* VEX.vvvv encodes one of the sources when the first
8337 operand is not an immediate. */
1ef99a7b 8338 if (i.tm.opcode_modifier.vexw == VEXW0)
5dd85c99
SP
8339 i.vex.register_specifier = i.op[0].regs;
8340 else
8341 i.vex.register_specifier = i.op[1].regs;
8342 }
8343
8344 /* Destination is a XMM register encoded in the ModRM.reg
8345 and VEX.R bit. */
8346 i.rm.reg = i.op[2].regs->reg_num;
8347 if ((i.op[2].regs->reg_flags & RegRex) != 0)
8348 i.rex |= REX_R;
8349
8350 /* ModRM.rm and VEX.B encodes the other source. */
8351 if (!i.mem_operands)
8352 {
8353 i.rm.mode = 3;
8354
1ef99a7b 8355 if (i.tm.opcode_modifier.vexw == VEXW0)
5dd85c99
SP
8356 i.rm.regmem = i.op[1].regs->reg_num;
8357 else
8358 i.rm.regmem = i.op[0].regs->reg_num;
8359
8360 if ((i.op[1].regs->reg_flags & RegRex) != 0)
8361 i.rex |= REX_B;
8362 }
8363 }
2426c15f 8364 else if (i.tm.opcode_modifier.vexvvvv == VEXLWP)
f88c9eb0
SP
8365 {
8366 i.vex.register_specifier = i.op[2].regs;
8367 if (!i.mem_operands)
8368 {
8369 i.rm.mode = 3;
8370 i.rm.regmem = i.op[1].regs->reg_num;
8371 if ((i.op[1].regs->reg_flags & RegRex) != 0)
8372 i.rex |= REX_B;
8373 }
8374 }
29b0f896
AM
8375 /* Fill in i.rm.reg or i.rm.regmem field with register operand
8376 (if any) based on i.tm.extension_opcode. Again, we must be
8377 careful to make sure that segment/control/debug/test/MMX
8378 registers are coded into the i.rm.reg field. */
f88c9eb0 8379 else if (i.reg_operands)
29b0f896 8380 {
99018f42 8381 unsigned int op;
7ab9ffdd
L
8382 unsigned int vex_reg = ~0;
8383
8384 for (op = 0; op < i.operands; op++)
921eafea
L
8385 if (i.types[op].bitfield.class == Reg
8386 || i.types[op].bitfield.class == RegBND
8387 || i.types[op].bitfield.class == RegMask
8388 || i.types[op].bitfield.class == SReg
8389 || i.types[op].bitfield.class == RegCR
8390 || i.types[op].bitfield.class == RegDR
8391 || i.types[op].bitfield.class == RegTR
8392 || i.types[op].bitfield.class == RegSIMD
8393 || i.types[op].bitfield.class == RegMMX)
8394 break;
c0209578 8395
7ab9ffdd
L
8396 if (vex_3_sources)
8397 op = dest;
2426c15f 8398 else if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
7ab9ffdd
L
8399 {
8400 /* For instructions with VexNDS, the register-only
8401 source operand is encoded in VEX prefix. */
8402 gas_assert (mem != (unsigned int) ~0);
c0f3af97 8403
7ab9ffdd 8404 if (op > mem)
c0f3af97 8405 {
7ab9ffdd
L
8406 vex_reg = op++;
8407 gas_assert (op < i.operands);
c0f3af97
L
8408 }
8409 else
c0f3af97 8410 {
f12dc422
L
8411 /* Check register-only source operand when two source
8412 operands are swapped. */
8413 if (!i.tm.operand_types[op].bitfield.baseindex
8414 && i.tm.operand_types[op + 1].bitfield.baseindex)
8415 {
8416 vex_reg = op;
8417 op += 2;
8418 gas_assert (mem == (vex_reg + 1)
8419 && op < i.operands);
8420 }
8421 else
8422 {
8423 vex_reg = op + 1;
8424 gas_assert (vex_reg < i.operands);
8425 }
c0f3af97 8426 }
7ab9ffdd 8427 }
2426c15f 8428 else if (i.tm.opcode_modifier.vexvvvv == VEXNDD)
7ab9ffdd 8429 {
f12dc422 8430 /* For instructions with VexNDD, the register destination
7ab9ffdd 8431 is encoded in VEX prefix. */
f12dc422
L
8432 if (i.mem_operands == 0)
8433 {
8434 /* There is no memory operand. */
8435 gas_assert ((op + 2) == i.operands);
8436 vex_reg = op + 1;
8437 }
8438 else
8d63c93e 8439 {
ed438a93
JB
8440 /* There are only 2 non-immediate operands. */
8441 gas_assert (op < i.imm_operands + 2
8442 && i.operands == i.imm_operands + 2);
8443 vex_reg = i.imm_operands + 1;
f12dc422 8444 }
7ab9ffdd
L
8445 }
8446 else
8447 gas_assert (op < i.operands);
99018f42 8448
7ab9ffdd
L
8449 if (vex_reg != (unsigned int) ~0)
8450 {
f12dc422 8451 i386_operand_type *type = &i.tm.operand_types[vex_reg];
7ab9ffdd 8452
bab6aec1 8453 if ((type->bitfield.class != Reg
dc821c5f 8454 || (!type->bitfield.dword && !type->bitfield.qword))
3528c362 8455 && type->bitfield.class != RegSIMD
43234a1e 8456 && !operand_type_equal (type, &regmask))
7ab9ffdd 8457 abort ();
f88c9eb0 8458
7ab9ffdd
L
8459 i.vex.register_specifier = i.op[vex_reg].regs;
8460 }
8461
1b9f0c97
L
8462 /* Don't set OP operand twice. */
8463 if (vex_reg != op)
7ab9ffdd 8464 {
1b9f0c97
L
8465 /* If there is an extension opcode to put here, the
8466 register number must be put into the regmem field. */
8467 if (i.tm.extension_opcode != None)
8468 {
8469 i.rm.regmem = i.op[op].regs->reg_num;
a5aeccd9
JB
8470 set_rex_vrex (i.op[op].regs, REX_B,
8471 i.tm.opcode_modifier.sse2avx);
1b9f0c97
L
8472 }
8473 else
8474 {
8475 i.rm.reg = i.op[op].regs->reg_num;
a5aeccd9
JB
8476 set_rex_vrex (i.op[op].regs, REX_R,
8477 i.tm.opcode_modifier.sse2avx);
1b9f0c97 8478 }
7ab9ffdd 8479 }
252b5132 8480
29b0f896
AM
8481 /* Now, if no memory operand has set i.rm.mode = 0, 1, 2 we
8482 must set it to 3 to indicate this is a register operand
8483 in the regmem field. */
8484 if (!i.mem_operands)
8485 i.rm.mode = 3;
8486 }
252b5132 8487
29b0f896 8488 /* Fill in i.rm.reg field with extension opcode (if any). */
c1e679ec 8489 if (i.tm.extension_opcode != None)
29b0f896
AM
8490 i.rm.reg = i.tm.extension_opcode;
8491 }
8492 return default_seg;
8493}
252b5132 8494
48ef937e
JB
8495static INLINE void
8496frag_opcode_byte (unsigned char byte)
8497{
8498 if (now_seg != absolute_section)
8499 FRAG_APPEND_1_CHAR (byte);
8500 else
8501 ++abs_section_offset;
8502}
8503
376cd056
JB
8504static unsigned int
8505flip_code16 (unsigned int code16)
8506{
8507 gas_assert (i.tm.operands == 1);
8508
8509 return !(i.prefix[REX_PREFIX] & REX_W)
8510 && (code16 ? i.tm.operand_types[0].bitfield.disp32
8511 || i.tm.operand_types[0].bitfield.disp32s
8512 : i.tm.operand_types[0].bitfield.disp16)
8513 ? CODE16 : 0;
8514}
8515
29b0f896 8516static void
e3bb37b5 8517output_branch (void)
29b0f896
AM
8518{
8519 char *p;
f8a5c266 8520 int size;
29b0f896
AM
8521 int code16;
8522 int prefix;
8523 relax_substateT subtype;
8524 symbolS *sym;
8525 offsetT off;
8526
48ef937e
JB
8527 if (now_seg == absolute_section)
8528 {
8529 as_bad (_("relaxable branches not supported in absolute section"));
8530 return;
8531 }
8532
f8a5c266 8533 code16 = flag_code == CODE_16BIT ? CODE16 : 0;
a501d77e 8534 size = i.disp_encoding == disp_encoding_32bit ? BIG : SMALL;
29b0f896
AM
8535
8536 prefix = 0;
8537 if (i.prefix[DATA_PREFIX] != 0)
252b5132 8538 {
29b0f896
AM
8539 prefix = 1;
8540 i.prefixes -= 1;
376cd056 8541 code16 ^= flip_code16(code16);
252b5132 8542 }
29b0f896
AM
8543 /* Pentium4 branch hints. */
8544 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8545 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
2f66722d 8546 {
29b0f896
AM
8547 prefix++;
8548 i.prefixes--;
8549 }
8550 if (i.prefix[REX_PREFIX] != 0)
8551 {
8552 prefix++;
8553 i.prefixes--;
2f66722d
AM
8554 }
8555
7e8b059b
L
8556 /* BND prefixed jump. */
8557 if (i.prefix[BND_PREFIX] != 0)
8558 {
6cb0a70e
JB
8559 prefix++;
8560 i.prefixes--;
7e8b059b
L
8561 }
8562
f2810fe0
JB
8563 if (i.prefixes != 0)
8564 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
29b0f896
AM
8565
8566 /* It's always a symbol; End frag & setup for relax.
8567 Make sure there is enough room in this frag for the largest
8568 instruction we may generate in md_convert_frag. This is 2
8569 bytes for the opcode and room for the prefix and largest
8570 displacement. */
8571 frag_grow (prefix + 2 + 4);
8572 /* Prefix and 1 opcode byte go in fr_fix. */
8573 p = frag_more (prefix + 1);
8574 if (i.prefix[DATA_PREFIX] != 0)
8575 *p++ = DATA_PREFIX_OPCODE;
8576 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
8577 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
8578 *p++ = i.prefix[SEG_PREFIX];
6cb0a70e
JB
8579 if (i.prefix[BND_PREFIX] != 0)
8580 *p++ = BND_PREFIX_OPCODE;
29b0f896
AM
8581 if (i.prefix[REX_PREFIX] != 0)
8582 *p++ = i.prefix[REX_PREFIX];
8583 *p = i.tm.base_opcode;
8584
8585 if ((unsigned char) *p == JUMP_PC_RELATIVE)
f8a5c266 8586 subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, size);
40fb9820 8587 else if (cpu_arch_flags.bitfield.cpui386)
f8a5c266 8588 subtype = ENCODE_RELAX_STATE (COND_JUMP, size);
29b0f896 8589 else
f8a5c266 8590 subtype = ENCODE_RELAX_STATE (COND_JUMP86, size);
29b0f896 8591 subtype |= code16;
3e73aa7c 8592
29b0f896
AM
8593 sym = i.op[0].disps->X_add_symbol;
8594 off = i.op[0].disps->X_add_number;
3e73aa7c 8595
29b0f896
AM
8596 if (i.op[0].disps->X_op != O_constant
8597 && i.op[0].disps->X_op != O_symbol)
3e73aa7c 8598 {
29b0f896
AM
8599 /* Handle complex expressions. */
8600 sym = make_expr_symbol (i.op[0].disps);
8601 off = 0;
8602 }
3e73aa7c 8603
29b0f896
AM
8604 /* 1 possible extra opcode + 4 byte displacement go in var part.
8605 Pass reloc in fr_var. */
d258b828 8606 frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
29b0f896 8607}
3e73aa7c 8608
bd7ab16b
L
8609#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8610/* Return TRUE iff PLT32 relocation should be used for branching to
8611 symbol S. */
8612
8613static bfd_boolean
8614need_plt32_p (symbolS *s)
8615{
8616 /* PLT32 relocation is ELF only. */
8617 if (!IS_ELF)
8618 return FALSE;
8619
a5def729
RO
8620#ifdef TE_SOLARIS
8621 /* Don't emit PLT32 relocation on Solaris: neither native linker nor
8622 krtld support it. */
8623 return FALSE;
8624#endif
8625
bd7ab16b
L
8626 /* Since there is no need to prepare for PLT branch on x86-64, we
8627 can generate R_X86_64_PLT32, instead of R_X86_64_PC32, which can
8628 be used as a marker for 32-bit PC-relative branches. */
8629 if (!object_64bit)
8630 return FALSE;
8631
44365e88
AM
8632 if (s == NULL)
8633 return FALSE;
8634
bd7ab16b
L
8635 /* Weak or undefined symbol need PLT32 relocation. */
8636 if (S_IS_WEAK (s) || !S_IS_DEFINED (s))
8637 return TRUE;
8638
8639 /* Non-global symbol doesn't need PLT32 relocation. */
8640 if (! S_IS_EXTERNAL (s))
8641 return FALSE;
8642
8643 /* Other global symbols need PLT32 relocation. NB: Symbol with
8644 non-default visibilities are treated as normal global symbol
8645 so that PLT32 relocation can be used as a marker for 32-bit
8646 PC-relative branches. It is useful for linker relaxation. */
8647 return TRUE;
8648}
8649#endif
8650
29b0f896 8651static void
e3bb37b5 8652output_jump (void)
29b0f896
AM
8653{
8654 char *p;
8655 int size;
3e02c1cc 8656 fixS *fixP;
bd7ab16b 8657 bfd_reloc_code_real_type jump_reloc = i.reloc[0];
29b0f896 8658
0cfa3eb3 8659 if (i.tm.opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
8660 {
8661 /* This is a loop or jecxz type instruction. */
8662 size = 1;
8663 if (i.prefix[ADDR_PREFIX] != 0)
8664 {
48ef937e 8665 frag_opcode_byte (ADDR_PREFIX_OPCODE);
29b0f896
AM
8666 i.prefixes -= 1;
8667 }
8668 /* Pentium4 branch hints. */
8669 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8670 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
8671 {
48ef937e 8672 frag_opcode_byte (i.prefix[SEG_PREFIX]);
29b0f896 8673 i.prefixes--;
3e73aa7c
JH
8674 }
8675 }
29b0f896
AM
8676 else
8677 {
8678 int code16;
3e73aa7c 8679
29b0f896
AM
8680 code16 = 0;
8681 if (flag_code == CODE_16BIT)
8682 code16 = CODE16;
3e73aa7c 8683
29b0f896
AM
8684 if (i.prefix[DATA_PREFIX] != 0)
8685 {
48ef937e 8686 frag_opcode_byte (DATA_PREFIX_OPCODE);
29b0f896 8687 i.prefixes -= 1;
376cd056 8688 code16 ^= flip_code16(code16);
29b0f896 8689 }
252b5132 8690
29b0f896
AM
8691 size = 4;
8692 if (code16)
8693 size = 2;
8694 }
9fcc94b6 8695
6cb0a70e
JB
8696 /* BND prefixed jump. */
8697 if (i.prefix[BND_PREFIX] != 0)
29b0f896 8698 {
48ef937e 8699 frag_opcode_byte (i.prefix[BND_PREFIX]);
29b0f896
AM
8700 i.prefixes -= 1;
8701 }
252b5132 8702
6cb0a70e 8703 if (i.prefix[REX_PREFIX] != 0)
7e8b059b 8704 {
48ef937e 8705 frag_opcode_byte (i.prefix[REX_PREFIX]);
7e8b059b
L
8706 i.prefixes -= 1;
8707 }
8708
f2810fe0
JB
8709 if (i.prefixes != 0)
8710 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
e0890092 8711
48ef937e
JB
8712 if (now_seg == absolute_section)
8713 {
9a182d04 8714 abs_section_offset += i.opcode_length + size;
48ef937e
JB
8715 return;
8716 }
8717
9a182d04
JB
8718 p = frag_more (i.opcode_length + size);
8719 switch (i.opcode_length)
42164a71
L
8720 {
8721 case 2:
8722 *p++ = i.tm.base_opcode >> 8;
1a0670f3 8723 /* Fall through. */
42164a71
L
8724 case 1:
8725 *p++ = i.tm.base_opcode;
8726 break;
8727 default:
8728 abort ();
8729 }
e0890092 8730
bd7ab16b
L
8731#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8732 if (size == 4
8733 && jump_reloc == NO_RELOC
8734 && need_plt32_p (i.op[0].disps->X_add_symbol))
8735 jump_reloc = BFD_RELOC_X86_64_PLT32;
8736#endif
8737
8738 jump_reloc = reloc (size, 1, 1, jump_reloc);
8739
3e02c1cc 8740 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
bd7ab16b 8741 i.op[0].disps, 1, jump_reloc);
3e02c1cc
AM
8742
8743 /* All jumps handled here are signed, but don't use a signed limit
8744 check for 32 and 16 bit jumps as we want to allow wrap around at
8745 4G and 64k respectively. */
8746 if (size == 1)
8747 fixP->fx_signed = 1;
29b0f896 8748}
e0890092 8749
29b0f896 8750static void
e3bb37b5 8751output_interseg_jump (void)
29b0f896
AM
8752{
8753 char *p;
8754 int size;
8755 int prefix;
8756 int code16;
252b5132 8757
29b0f896
AM
8758 code16 = 0;
8759 if (flag_code == CODE_16BIT)
8760 code16 = CODE16;
a217f122 8761
29b0f896
AM
8762 prefix = 0;
8763 if (i.prefix[DATA_PREFIX] != 0)
8764 {
8765 prefix = 1;
8766 i.prefixes -= 1;
8767 code16 ^= CODE16;
8768 }
6cb0a70e
JB
8769
8770 gas_assert (!i.prefix[REX_PREFIX]);
252b5132 8771
29b0f896
AM
8772 size = 4;
8773 if (code16)
8774 size = 2;
252b5132 8775
f2810fe0
JB
8776 if (i.prefixes != 0)
8777 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
252b5132 8778
48ef937e
JB
8779 if (now_seg == absolute_section)
8780 {
8781 abs_section_offset += prefix + 1 + 2 + size;
8782 return;
8783 }
8784
29b0f896
AM
8785 /* 1 opcode; 2 segment; offset */
8786 p = frag_more (prefix + 1 + 2 + size);
3e73aa7c 8787
29b0f896
AM
8788 if (i.prefix[DATA_PREFIX] != 0)
8789 *p++ = DATA_PREFIX_OPCODE;
252b5132 8790
29b0f896
AM
8791 if (i.prefix[REX_PREFIX] != 0)
8792 *p++ = i.prefix[REX_PREFIX];
252b5132 8793
29b0f896
AM
8794 *p++ = i.tm.base_opcode;
8795 if (i.op[1].imms->X_op == O_constant)
8796 {
8797 offsetT n = i.op[1].imms->X_add_number;
252b5132 8798
29b0f896
AM
8799 if (size == 2
8800 && !fits_in_unsigned_word (n)
8801 && !fits_in_signed_word (n))
8802 {
8803 as_bad (_("16-bit jump out of range"));
8804 return;
8805 }
8806 md_number_to_chars (p, n, size);
8807 }
8808 else
8809 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
d258b828 8810 i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
6d96a594
C
8811
8812 p += size;
8813 if (i.op[0].imms->X_op == O_constant)
8814 md_number_to_chars (p, (valueT) i.op[0].imms->X_add_number, 2);
8815 else
8816 fix_new_exp (frag_now, p - frag_now->fr_literal, 2,
8817 i.op[0].imms, 0, reloc (2, 0, 0, i.reloc[0]));
29b0f896 8818}
a217f122 8819
b4a3a7b4
L
8820#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8821void
8822x86_cleanup (void)
8823{
8824 char *p;
8825 asection *seg = now_seg;
8826 subsegT subseg = now_subseg;
8827 asection *sec;
8828 unsigned int alignment, align_size_1;
8829 unsigned int isa_1_descsz, feature_2_descsz, descsz;
8830 unsigned int isa_1_descsz_raw, feature_2_descsz_raw;
8831 unsigned int padding;
8832
8833 if (!IS_ELF || !x86_used_note)
8834 return;
8835
b4a3a7b4
L
8836 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X86;
8837
8838 /* The .note.gnu.property section layout:
8839
8840 Field Length Contents
8841 ---- ---- ----
8842 n_namsz 4 4
8843 n_descsz 4 The note descriptor size
8844 n_type 4 NT_GNU_PROPERTY_TYPE_0
8845 n_name 4 "GNU"
8846 n_desc n_descsz The program property array
8847 .... .... ....
8848 */
8849
8850 /* Create the .note.gnu.property section. */
8851 sec = subseg_new (NOTE_GNU_PROPERTY_SECTION_NAME, 0);
fd361982 8852 bfd_set_section_flags (sec,
b4a3a7b4
L
8853 (SEC_ALLOC
8854 | SEC_LOAD
8855 | SEC_DATA
8856 | SEC_HAS_CONTENTS
8857 | SEC_READONLY));
8858
8859 if (get_elf_backend_data (stdoutput)->s->elfclass == ELFCLASS64)
8860 {
8861 align_size_1 = 7;
8862 alignment = 3;
8863 }
8864 else
8865 {
8866 align_size_1 = 3;
8867 alignment = 2;
8868 }
8869
fd361982 8870 bfd_set_section_alignment (sec, alignment);
b4a3a7b4
L
8871 elf_section_type (sec) = SHT_NOTE;
8872
8873 /* GNU_PROPERTY_X86_ISA_1_USED: 4-byte type + 4-byte data size
8874 + 4-byte data */
8875 isa_1_descsz_raw = 4 + 4 + 4;
8876 /* Align GNU_PROPERTY_X86_ISA_1_USED. */
8877 isa_1_descsz = (isa_1_descsz_raw + align_size_1) & ~align_size_1;
8878
8879 feature_2_descsz_raw = isa_1_descsz;
8880 /* GNU_PROPERTY_X86_FEATURE_2_USED: 4-byte type + 4-byte data size
8881 + 4-byte data */
8882 feature_2_descsz_raw += 4 + 4 + 4;
8883 /* Align GNU_PROPERTY_X86_FEATURE_2_USED. */
8884 feature_2_descsz = ((feature_2_descsz_raw + align_size_1)
8885 & ~align_size_1);
8886
8887 descsz = feature_2_descsz;
8888 /* Section size: n_namsz + n_descsz + n_type + n_name + n_descsz. */
8889 p = frag_more (4 + 4 + 4 + 4 + descsz);
8890
8891 /* Write n_namsz. */
8892 md_number_to_chars (p, (valueT) 4, 4);
8893
8894 /* Write n_descsz. */
8895 md_number_to_chars (p + 4, (valueT) descsz, 4);
8896
8897 /* Write n_type. */
8898 md_number_to_chars (p + 4 * 2, (valueT) NT_GNU_PROPERTY_TYPE_0, 4);
8899
8900 /* Write n_name. */
8901 memcpy (p + 4 * 3, "GNU", 4);
8902
8903 /* Write 4-byte type. */
8904 md_number_to_chars (p + 4 * 4,
8905 (valueT) GNU_PROPERTY_X86_ISA_1_USED, 4);
8906
8907 /* Write 4-byte data size. */
8908 md_number_to_chars (p + 4 * 5, (valueT) 4, 4);
8909
8910 /* Write 4-byte data. */
8911 md_number_to_chars (p + 4 * 6, (valueT) x86_isa_1_used, 4);
8912
8913 /* Zero out paddings. */
8914 padding = isa_1_descsz - isa_1_descsz_raw;
8915 if (padding)
8916 memset (p + 4 * 7, 0, padding);
8917
8918 /* Write 4-byte type. */
8919 md_number_to_chars (p + isa_1_descsz + 4 * 4,
8920 (valueT) GNU_PROPERTY_X86_FEATURE_2_USED, 4);
8921
8922 /* Write 4-byte data size. */
8923 md_number_to_chars (p + isa_1_descsz + 4 * 5, (valueT) 4, 4);
8924
8925 /* Write 4-byte data. */
8926 md_number_to_chars (p + isa_1_descsz + 4 * 6,
8927 (valueT) x86_feature_2_used, 4);
8928
8929 /* Zero out paddings. */
8930 padding = feature_2_descsz - feature_2_descsz_raw;
8931 if (padding)
8932 memset (p + isa_1_descsz + 4 * 7, 0, padding);
8933
8934 /* We probably can't restore the current segment, for there likely
8935 isn't one yet... */
8936 if (seg && subseg)
8937 subseg_set (seg, subseg);
8938}
8939#endif
8940
9c33702b
JB
8941static unsigned int
8942encoding_length (const fragS *start_frag, offsetT start_off,
8943 const char *frag_now_ptr)
8944{
8945 unsigned int len = 0;
8946
8947 if (start_frag != frag_now)
8948 {
8949 const fragS *fr = start_frag;
8950
8951 do {
8952 len += fr->fr_fix;
8953 fr = fr->fr_next;
8954 } while (fr && fr != frag_now);
8955 }
8956
8957 return len - start_off + (frag_now_ptr - frag_now->fr_literal);
8958}
8959
e379e5f3 8960/* Return 1 for test, and, cmp, add, sub, inc and dec which may
79d72f45
HL
8961 be macro-fused with conditional jumps.
8962 NB: If TEST/AND/CMP/ADD/SUB/INC/DEC is of RIP relative address,
8963 or is one of the following format:
8964
8965 cmp m, imm
8966 add m, imm
8967 sub m, imm
8968 test m, imm
8969 and m, imm
8970 inc m
8971 dec m
8972
8973 it is unfusible. */
e379e5f3
L
8974
8975static int
79d72f45 8976maybe_fused_with_jcc_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
8977{
8978 /* No RIP address. */
8979 if (i.base_reg && i.base_reg->reg_num == RegIP)
8980 return 0;
8981
389d00a5
JB
8982 /* No opcodes outside of base encoding space. */
8983 if (i.tm.opcode_modifier.opcodespace != SPACE_BASE)
e379e5f3
L
8984 return 0;
8985
79d72f45
HL
8986 /* add, sub without add/sub m, imm. */
8987 if (i.tm.base_opcode <= 5
e379e5f3
L
8988 || (i.tm.base_opcode >= 0x28 && i.tm.base_opcode <= 0x2d)
8989 || ((i.tm.base_opcode | 3) == 0x83
79d72f45 8990 && (i.tm.extension_opcode == 0x5
e379e5f3 8991 || i.tm.extension_opcode == 0x0)))
79d72f45
HL
8992 {
8993 *mf_cmp_p = mf_cmp_alu_cmp;
8994 return !(i.mem_operands && i.imm_operands);
8995 }
e379e5f3 8996
79d72f45
HL
8997 /* and without and m, imm. */
8998 if ((i.tm.base_opcode >= 0x20 && i.tm.base_opcode <= 0x25)
8999 || ((i.tm.base_opcode | 3) == 0x83
9000 && i.tm.extension_opcode == 0x4))
9001 {
9002 *mf_cmp_p = mf_cmp_test_and;
9003 return !(i.mem_operands && i.imm_operands);
9004 }
9005
9006 /* test without test m imm. */
e379e5f3
L
9007 if ((i.tm.base_opcode | 1) == 0x85
9008 || (i.tm.base_opcode | 1) == 0xa9
9009 || ((i.tm.base_opcode | 1) == 0xf7
79d72f45
HL
9010 && i.tm.extension_opcode == 0))
9011 {
9012 *mf_cmp_p = mf_cmp_test_and;
9013 return !(i.mem_operands && i.imm_operands);
9014 }
9015
9016 /* cmp without cmp m, imm. */
9017 if ((i.tm.base_opcode >= 0x38 && i.tm.base_opcode <= 0x3d)
e379e5f3
L
9018 || ((i.tm.base_opcode | 3) == 0x83
9019 && (i.tm.extension_opcode == 0x7)))
79d72f45
HL
9020 {
9021 *mf_cmp_p = mf_cmp_alu_cmp;
9022 return !(i.mem_operands && i.imm_operands);
9023 }
e379e5f3 9024
79d72f45 9025 /* inc, dec without inc/dec m. */
e379e5f3
L
9026 if ((i.tm.cpu_flags.bitfield.cpuno64
9027 && (i.tm.base_opcode | 0xf) == 0x4f)
9028 || ((i.tm.base_opcode | 1) == 0xff
9029 && i.tm.extension_opcode <= 0x1))
79d72f45
HL
9030 {
9031 *mf_cmp_p = mf_cmp_incdec;
9032 return !i.mem_operands;
9033 }
e379e5f3
L
9034
9035 return 0;
9036}
9037
9038/* Return 1 if a FUSED_JCC_PADDING frag should be generated. */
9039
9040static int
79d72f45 9041add_fused_jcc_padding_frag_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
9042{
9043 /* NB: Don't work with COND_JUMP86 without i386. */
9044 if (!align_branch_power
9045 || now_seg == absolute_section
9046 || !cpu_arch_flags.bitfield.cpui386
9047 || !(align_branch & align_branch_fused_bit))
9048 return 0;
9049
79d72f45 9050 if (maybe_fused_with_jcc_p (mf_cmp_p))
e379e5f3
L
9051 {
9052 if (last_insn.kind == last_insn_other
9053 || last_insn.seg != now_seg)
9054 return 1;
9055 if (flag_debug)
9056 as_warn_where (last_insn.file, last_insn.line,
9057 _("`%s` skips -malign-branch-boundary on `%s`"),
9058 last_insn.name, i.tm.name);
9059 }
9060
9061 return 0;
9062}
9063
9064/* Return 1 if a BRANCH_PREFIX frag should be generated. */
9065
9066static int
9067add_branch_prefix_frag_p (void)
9068{
9069 /* NB: Don't work with COND_JUMP86 without i386. Don't add prefix
9070 to PadLock instructions since they include prefixes in opcode. */
9071 if (!align_branch_power
9072 || !align_branch_prefix_size
9073 || now_seg == absolute_section
9074 || i.tm.cpu_flags.bitfield.cpupadlock
9075 || !cpu_arch_flags.bitfield.cpui386)
9076 return 0;
9077
9078 /* Don't add prefix if it is a prefix or there is no operand in case
9079 that segment prefix is special. */
9080 if (!i.operands || i.tm.opcode_modifier.isprefix)
9081 return 0;
9082
9083 if (last_insn.kind == last_insn_other
9084 || last_insn.seg != now_seg)
9085 return 1;
9086
9087 if (flag_debug)
9088 as_warn_where (last_insn.file, last_insn.line,
9089 _("`%s` skips -malign-branch-boundary on `%s`"),
9090 last_insn.name, i.tm.name);
9091
9092 return 0;
9093}
9094
9095/* Return 1 if a BRANCH_PADDING frag should be generated. */
9096
9097static int
79d72f45
HL
9098add_branch_padding_frag_p (enum align_branch_kind *branch_p,
9099 enum mf_jcc_kind *mf_jcc_p)
e379e5f3
L
9100{
9101 int add_padding;
9102
9103 /* NB: Don't work with COND_JUMP86 without i386. */
9104 if (!align_branch_power
9105 || now_seg == absolute_section
389d00a5
JB
9106 || !cpu_arch_flags.bitfield.cpui386
9107 || i.tm.opcode_modifier.opcodespace != SPACE_BASE)
e379e5f3
L
9108 return 0;
9109
9110 add_padding = 0;
9111
9112 /* Check for jcc and direct jmp. */
9113 if (i.tm.opcode_modifier.jump == JUMP)
9114 {
9115 if (i.tm.base_opcode == JUMP_PC_RELATIVE)
9116 {
9117 *branch_p = align_branch_jmp;
9118 add_padding = align_branch & align_branch_jmp_bit;
9119 }
9120 else
9121 {
79d72f45
HL
9122 /* Because J<cc> and JN<cc> share same group in macro-fusible table,
9123 igore the lowest bit. */
9124 *mf_jcc_p = (i.tm.base_opcode & 0x0e) >> 1;
e379e5f3
L
9125 *branch_p = align_branch_jcc;
9126 if ((align_branch & align_branch_jcc_bit))
9127 add_padding = 1;
9128 }
9129 }
e379e5f3
L
9130 else if ((i.tm.base_opcode | 1) == 0xc3)
9131 {
9132 /* Near ret. */
9133 *branch_p = align_branch_ret;
9134 if ((align_branch & align_branch_ret_bit))
9135 add_padding = 1;
9136 }
9137 else
9138 {
9139 /* Check for indirect jmp, direct and indirect calls. */
9140 if (i.tm.base_opcode == 0xe8)
9141 {
9142 /* Direct call. */
9143 *branch_p = align_branch_call;
9144 if ((align_branch & align_branch_call_bit))
9145 add_padding = 1;
9146 }
9147 else if (i.tm.base_opcode == 0xff
9148 && (i.tm.extension_opcode == 2
9149 || i.tm.extension_opcode == 4))
9150 {
9151 /* Indirect call and jmp. */
9152 *branch_p = align_branch_indirect;
9153 if ((align_branch & align_branch_indirect_bit))
9154 add_padding = 1;
9155 }
9156
9157 if (add_padding
9158 && i.disp_operands
9159 && tls_get_addr
9160 && (i.op[0].disps->X_op == O_symbol
9161 || (i.op[0].disps->X_op == O_subtract
9162 && i.op[0].disps->X_op_symbol == GOT_symbol)))
9163 {
9164 symbolS *s = i.op[0].disps->X_add_symbol;
9165 /* No padding to call to global or undefined tls_get_addr. */
9166 if ((S_IS_EXTERNAL (s) || !S_IS_DEFINED (s))
9167 && strcmp (S_GET_NAME (s), tls_get_addr) == 0)
9168 return 0;
9169 }
9170 }
9171
9172 if (add_padding
9173 && last_insn.kind != last_insn_other
9174 && last_insn.seg == now_seg)
9175 {
9176 if (flag_debug)
9177 as_warn_where (last_insn.file, last_insn.line,
9178 _("`%s` skips -malign-branch-boundary on `%s`"),
9179 last_insn.name, i.tm.name);
9180 return 0;
9181 }
9182
9183 return add_padding;
9184}
9185
29b0f896 9186static void
e3bb37b5 9187output_insn (void)
29b0f896 9188{
2bbd9c25
JJ
9189 fragS *insn_start_frag;
9190 offsetT insn_start_off;
e379e5f3
L
9191 fragS *fragP = NULL;
9192 enum align_branch_kind branch = align_branch_none;
79d72f45
HL
9193 /* The initializer is arbitrary just to avoid uninitialized error.
9194 it's actually either assigned in add_branch_padding_frag_p
9195 or never be used. */
9196 enum mf_jcc_kind mf_jcc = mf_jcc_jo;
2bbd9c25 9197
b4a3a7b4 9198#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
48ef937e 9199 if (IS_ELF && x86_used_note && now_seg != absolute_section)
b4a3a7b4 9200 {
32930e4e
L
9201 if ((i.xstate & xstate_tmm) == xstate_tmm
9202 || i.tm.cpu_flags.bitfield.cpuamx_tile)
9203 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_TMM;
9204
b4a3a7b4
L
9205 if (i.tm.cpu_flags.bitfield.cpu8087
9206 || i.tm.cpu_flags.bitfield.cpu287
9207 || i.tm.cpu_flags.bitfield.cpu387
9208 || i.tm.cpu_flags.bitfield.cpu687
9209 || i.tm.cpu_flags.bitfield.cpufisttp)
9210 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X87;
014d61ea 9211
921eafea 9212 if ((i.xstate & xstate_mmx)
389d00a5
JB
9213 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
9214 && !is_any_vex_encoding (&i.tm)
9215 && (i.tm.base_opcode == 0x77 /* emms */
9216 || i.tm.base_opcode == 0x0e /* femms */)))
b4a3a7b4 9217 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MMX;
014d61ea 9218
32930e4e
L
9219 if (i.index_reg)
9220 {
9221 if (i.index_reg->reg_type.bitfield.zmmword)
9222 i.xstate |= xstate_zmm;
9223 else if (i.index_reg->reg_type.bitfield.ymmword)
9224 i.xstate |= xstate_ymm;
9225 else if (i.index_reg->reg_type.bitfield.xmmword)
9226 i.xstate |= xstate_xmm;
9227 }
014d61ea
JB
9228
9229 /* vzeroall / vzeroupper */
9230 if (i.tm.base_opcode == 0x77 && i.tm.cpu_flags.bitfield.cpuavx)
9231 i.xstate |= xstate_ymm;
9232
c4694f17 9233 if ((i.xstate & xstate_xmm)
389d00a5
JB
9234 /* ldmxcsr / stmxcsr / vldmxcsr / vstmxcsr */
9235 || (i.tm.base_opcode == 0xae
9236 && (i.tm.cpu_flags.bitfield.cpusse
9237 || i.tm.cpu_flags.bitfield.cpuavx))
c4694f17
TG
9238 || i.tm.cpu_flags.bitfield.cpuwidekl
9239 || i.tm.cpu_flags.bitfield.cpukl)
b4a3a7b4 9240 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XMM;
014d61ea 9241
921eafea 9242 if ((i.xstate & xstate_ymm) == xstate_ymm)
b4a3a7b4 9243 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_YMM;
921eafea 9244 if ((i.xstate & xstate_zmm) == xstate_zmm)
b4a3a7b4 9245 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_ZMM;
6225c532 9246 if (i.mask.reg || (i.xstate & xstate_mask) == xstate_mask)
32930e4e 9247 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MASK;
b4a3a7b4
L
9248 if (i.tm.cpu_flags.bitfield.cpufxsr)
9249 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_FXSR;
9250 if (i.tm.cpu_flags.bitfield.cpuxsave)
9251 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVE;
9252 if (i.tm.cpu_flags.bitfield.cpuxsaveopt)
9253 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT;
9254 if (i.tm.cpu_flags.bitfield.cpuxsavec)
9255 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEC;
b0ab0693
L
9256
9257 if (x86_feature_2_used
9258 || i.tm.cpu_flags.bitfield.cpucmov
9259 || i.tm.cpu_flags.bitfield.cpusyscall
389d00a5
JB
9260 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
9261 && i.tm.base_opcode == 0xc7
70e95837 9262 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
b0ab0693
L
9263 && i.tm.extension_opcode == 1) /* cmpxchg8b */)
9264 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_BASELINE;
9265 if (i.tm.cpu_flags.bitfield.cpusse3
9266 || i.tm.cpu_flags.bitfield.cpussse3
9267 || i.tm.cpu_flags.bitfield.cpusse4_1
9268 || i.tm.cpu_flags.bitfield.cpusse4_2
9269 || i.tm.cpu_flags.bitfield.cpucx16
9270 || i.tm.cpu_flags.bitfield.cpupopcnt
9271 /* LAHF-SAHF insns in 64-bit mode. */
9272 || (flag_code == CODE_64BIT
35648716
JB
9273 && (i.tm.base_opcode | 1) == 0x9f
9274 && i.tm.opcode_modifier.opcodespace == SPACE_BASE))
b0ab0693
L
9275 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V2;
9276 if (i.tm.cpu_flags.bitfield.cpuavx
9277 || i.tm.cpu_flags.bitfield.cpuavx2
9278 /* Any VEX encoded insns execpt for CpuAVX512F, CpuAVX512BW,
9279 CpuAVX512DQ, LPW, TBM and AMX. */
9280 || (i.tm.opcode_modifier.vex
9281 && !i.tm.cpu_flags.bitfield.cpuavx512f
9282 && !i.tm.cpu_flags.bitfield.cpuavx512bw
9283 && !i.tm.cpu_flags.bitfield.cpuavx512dq
9284 && !i.tm.cpu_flags.bitfield.cpulwp
9285 && !i.tm.cpu_flags.bitfield.cputbm
9286 && !(x86_feature_2_used & GNU_PROPERTY_X86_FEATURE_2_TMM))
9287 || i.tm.cpu_flags.bitfield.cpuf16c
9288 || i.tm.cpu_flags.bitfield.cpufma
9289 || i.tm.cpu_flags.bitfield.cpulzcnt
9290 || i.tm.cpu_flags.bitfield.cpumovbe
9291 || i.tm.cpu_flags.bitfield.cpuxsaves
9292 || (x86_feature_2_used
9293 & (GNU_PROPERTY_X86_FEATURE_2_XSAVE
9294 | GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT
9295 | GNU_PROPERTY_X86_FEATURE_2_XSAVEC)) != 0)
9296 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V3;
9297 if (i.tm.cpu_flags.bitfield.cpuavx512f
9298 || i.tm.cpu_flags.bitfield.cpuavx512bw
9299 || i.tm.cpu_flags.bitfield.cpuavx512dq
9300 || i.tm.cpu_flags.bitfield.cpuavx512vl
9301 /* Any EVEX encoded insns except for AVX512ER, AVX512PF and
9302 VNNIW. */
9303 || (i.tm.opcode_modifier.evex
9304 && !i.tm.cpu_flags.bitfield.cpuavx512er
9305 && !i.tm.cpu_flags.bitfield.cpuavx512pf
9306 && !i.tm.cpu_flags.bitfield.cpuavx512_4vnniw))
9307 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V4;
b4a3a7b4
L
9308 }
9309#endif
9310
29b0f896
AM
9311 /* Tie dwarf2 debug info to the address at the start of the insn.
9312 We can't do this after the insn has been output as the current
9313 frag may have been closed off. eg. by frag_var. */
9314 dwarf2_emit_insn (0);
9315
2bbd9c25
JJ
9316 insn_start_frag = frag_now;
9317 insn_start_off = frag_now_fix ();
9318
79d72f45 9319 if (add_branch_padding_frag_p (&branch, &mf_jcc))
e379e5f3
L
9320 {
9321 char *p;
9322 /* Branch can be 8 bytes. Leave some room for prefixes. */
9323 unsigned int max_branch_padding_size = 14;
9324
9325 /* Align section to boundary. */
9326 record_alignment (now_seg, align_branch_power);
9327
9328 /* Make room for padding. */
9329 frag_grow (max_branch_padding_size);
9330
9331 /* Start of the padding. */
9332 p = frag_more (0);
9333
9334 fragP = frag_now;
9335
9336 frag_var (rs_machine_dependent, max_branch_padding_size, 0,
9337 ENCODE_RELAX_STATE (BRANCH_PADDING, 0),
9338 NULL, 0, p);
9339
79d72f45 9340 fragP->tc_frag_data.mf_type = mf_jcc;
e379e5f3
L
9341 fragP->tc_frag_data.branch_type = branch;
9342 fragP->tc_frag_data.max_bytes = max_branch_padding_size;
9343 }
9344
29b0f896 9345 /* Output jumps. */
0cfa3eb3 9346 if (i.tm.opcode_modifier.jump == JUMP)
29b0f896 9347 output_branch ();
0cfa3eb3
JB
9348 else if (i.tm.opcode_modifier.jump == JUMP_BYTE
9349 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896 9350 output_jump ();
0cfa3eb3 9351 else if (i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT)
29b0f896
AM
9352 output_interseg_jump ();
9353 else
9354 {
9355 /* Output normal instructions here. */
9356 char *p;
9357 unsigned char *q;
47465058 9358 unsigned int j;
79d72f45 9359 enum mf_cmp_kind mf_cmp;
4dffcebc 9360
e4e00185 9361 if (avoid_fence
389d00a5
JB
9362 && (i.tm.base_opcode == 0xaee8
9363 || i.tm.base_opcode == 0xaef0
9364 || i.tm.base_opcode == 0xaef8))
48ef937e
JB
9365 {
9366 /* Encode lfence, mfence, and sfence as
9367 f0 83 04 24 00 lock addl $0x0, (%{re}sp). */
9368 if (now_seg != absolute_section)
9369 {
9370 offsetT val = 0x240483f0ULL;
9371
9372 p = frag_more (5);
9373 md_number_to_chars (p, val, 5);
9374 }
9375 else
9376 abs_section_offset += 5;
9377 return;
9378 }
e4e00185 9379
d022bddd
IT
9380 /* Some processors fail on LOCK prefix. This options makes
9381 assembler ignore LOCK prefix and serves as a workaround. */
9382 if (omit_lock_prefix)
9383 {
35648716
JB
9384 if (i.tm.base_opcode == LOCK_PREFIX_OPCODE
9385 && i.tm.opcode_modifier.isprefix)
d022bddd
IT
9386 return;
9387 i.prefix[LOCK_PREFIX] = 0;
9388 }
9389
e379e5f3
L
9390 if (branch)
9391 /* Skip if this is a branch. */
9392 ;
79d72f45 9393 else if (add_fused_jcc_padding_frag_p (&mf_cmp))
e379e5f3
L
9394 {
9395 /* Make room for padding. */
9396 frag_grow (MAX_FUSED_JCC_PADDING_SIZE);
9397 p = frag_more (0);
9398
9399 fragP = frag_now;
9400
9401 frag_var (rs_machine_dependent, MAX_FUSED_JCC_PADDING_SIZE, 0,
9402 ENCODE_RELAX_STATE (FUSED_JCC_PADDING, 0),
9403 NULL, 0, p);
9404
79d72f45 9405 fragP->tc_frag_data.mf_type = mf_cmp;
e379e5f3
L
9406 fragP->tc_frag_data.branch_type = align_branch_fused;
9407 fragP->tc_frag_data.max_bytes = MAX_FUSED_JCC_PADDING_SIZE;
9408 }
9409 else if (add_branch_prefix_frag_p ())
9410 {
9411 unsigned int max_prefix_size = align_branch_prefix_size;
9412
9413 /* Make room for padding. */
9414 frag_grow (max_prefix_size);
9415 p = frag_more (0);
9416
9417 fragP = frag_now;
9418
9419 frag_var (rs_machine_dependent, max_prefix_size, 0,
9420 ENCODE_RELAX_STATE (BRANCH_PREFIX, 0),
9421 NULL, 0, p);
9422
9423 fragP->tc_frag_data.max_bytes = max_prefix_size;
9424 }
9425
43234a1e
L
9426 /* Since the VEX/EVEX prefix contains the implicit prefix, we
9427 don't need the explicit prefix. */
9428 if (!i.tm.opcode_modifier.vex && !i.tm.opcode_modifier.evex)
bc4bd9ab 9429 {
7b47a312 9430 switch (i.tm.opcode_modifier.opcodeprefix)
bc4bd9ab 9431 {
7b47a312
L
9432 case PREFIX_0X66:
9433 add_prefix (0x66);
9434 break;
9435 case PREFIX_0XF2:
9436 add_prefix (0xf2);
9437 break;
9438 case PREFIX_0XF3:
8b65b895
L
9439 if (!i.tm.cpu_flags.bitfield.cpupadlock
9440 || (i.prefix[REP_PREFIX] != 0xf3))
9441 add_prefix (0xf3);
c0f3af97 9442 break;
7b47a312 9443 case PREFIX_NONE:
9a182d04 9444 switch (i.opcode_length)
c0f3af97 9445 {
7b47a312 9446 case 2:
7b47a312 9447 break;
9a182d04 9448 case 1:
7b47a312 9449 /* Check for pseudo prefixes. */
9a182d04
JB
9450 if (!i.tm.opcode_modifier.isprefix || i.tm.base_opcode)
9451 break;
7b47a312
L
9452 as_bad_where (insn_start_frag->fr_file,
9453 insn_start_frag->fr_line,
9454 _("pseudo prefix without instruction"));
9455 return;
9456 default:
9457 abort ();
4dffcebc 9458 }
c0f3af97 9459 break;
c0f3af97
L
9460 default:
9461 abort ();
bc4bd9ab 9462 }
c0f3af97 9463
6d19a37a 9464#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
cf61b747
L
9465 /* For x32, add a dummy REX_OPCODE prefix for mov/add with
9466 R_X86_64_GOTTPOFF relocation so that linker can safely
14470f07
L
9467 perform IE->LE optimization. A dummy REX_OPCODE prefix
9468 is also needed for lea with R_X86_64_GOTPC32_TLSDESC
9469 relocation for GDesc -> IE/LE optimization. */
cf61b747
L
9470 if (x86_elf_abi == X86_64_X32_ABI
9471 && i.operands == 2
14470f07
L
9472 && (i.reloc[0] == BFD_RELOC_X86_64_GOTTPOFF
9473 || i.reloc[0] == BFD_RELOC_X86_64_GOTPC32_TLSDESC)
cf61b747
L
9474 && i.prefix[REX_PREFIX] == 0)
9475 add_prefix (REX_OPCODE);
6d19a37a 9476#endif
cf61b747 9477
c0f3af97
L
9478 /* The prefix bytes. */
9479 for (j = ARRAY_SIZE (i.prefix), q = i.prefix; j > 0; j--, q++)
9480 if (*q)
48ef937e 9481 frag_opcode_byte (*q);
0f10071e 9482 }
ae5c1c7b 9483 else
c0f3af97
L
9484 {
9485 for (j = 0, q = i.prefix; j < ARRAY_SIZE (i.prefix); j++, q++)
9486 if (*q)
9487 switch (j)
9488 {
c0f3af97
L
9489 case SEG_PREFIX:
9490 case ADDR_PREFIX:
48ef937e 9491 frag_opcode_byte (*q);
c0f3af97
L
9492 break;
9493 default:
9494 /* There should be no other prefixes for instructions
9495 with VEX prefix. */
9496 abort ();
9497 }
9498
43234a1e
L
9499 /* For EVEX instructions i.vrex should become 0 after
9500 build_evex_prefix. For VEX instructions upper 16 registers
9501 aren't available, so VREX should be 0. */
9502 if (i.vrex)
9503 abort ();
c0f3af97 9504 /* Now the VEX prefix. */
48ef937e
JB
9505 if (now_seg != absolute_section)
9506 {
9507 p = frag_more (i.vex.length);
9508 for (j = 0; j < i.vex.length; j++)
9509 p[j] = i.vex.bytes[j];
9510 }
9511 else
9512 abs_section_offset += i.vex.length;
c0f3af97 9513 }
252b5132 9514
29b0f896 9515 /* Now the opcode; be careful about word order here! */
389d00a5
JB
9516 j = i.opcode_length;
9517 if (!i.vex.length)
9518 switch (i.tm.opcode_modifier.opcodespace)
9519 {
9520 case SPACE_BASE:
9521 break;
9522 case SPACE_0F:
9523 ++j;
9524 break;
9525 case SPACE_0F38:
9526 case SPACE_0F3A:
9527 j += 2;
9528 break;
9529 default:
9530 abort ();
9531 }
9532
48ef937e 9533 if (now_seg == absolute_section)
389d00a5
JB
9534 abs_section_offset += j;
9535 else if (j == 1)
29b0f896
AM
9536 {
9537 FRAG_APPEND_1_CHAR (i.tm.base_opcode);
9538 }
9539 else
9540 {
389d00a5
JB
9541 p = frag_more (j);
9542 if (!i.vex.length
9543 && i.tm.opcode_modifier.opcodespace != SPACE_BASE)
9544 {
9545 *p++ = 0x0f;
9546 if (i.tm.opcode_modifier.opcodespace != SPACE_0F)
9547 *p++ = i.tm.opcode_modifier.opcodespace == SPACE_0F38
9548 ? 0x38 : 0x3a;
9549 }
9550
9a182d04 9551 switch (i.opcode_length)
331d2d0d 9552 {
4dffcebc 9553 case 2:
389d00a5
JB
9554 /* Put out high byte first: can't use md_number_to_chars! */
9555 *p++ = (i.tm.base_opcode >> 8) & 0xff;
9556 /* Fall through. */
9557 case 1:
9558 *p = i.tm.base_opcode & 0xff;
4dffcebc
L
9559 break;
9560 default:
9561 abort ();
9562 break;
331d2d0d 9563 }
0f10071e 9564
29b0f896 9565 }
3e73aa7c 9566
29b0f896 9567 /* Now the modrm byte and sib byte (if present). */
40fb9820 9568 if (i.tm.opcode_modifier.modrm)
29b0f896 9569 {
48ef937e
JB
9570 frag_opcode_byte ((i.rm.regmem << 0)
9571 | (i.rm.reg << 3)
9572 | (i.rm.mode << 6));
29b0f896
AM
9573 /* If i.rm.regmem == ESP (4)
9574 && i.rm.mode != (Register mode)
9575 && not 16 bit
9576 ==> need second modrm byte. */
9577 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
9578 && i.rm.mode != 3
dc821c5f 9579 && !(i.base_reg && i.base_reg->reg_type.bitfield.word))
48ef937e
JB
9580 frag_opcode_byte ((i.sib.base << 0)
9581 | (i.sib.index << 3)
9582 | (i.sib.scale << 6));
29b0f896 9583 }
3e73aa7c 9584
29b0f896 9585 if (i.disp_operands)
2bbd9c25 9586 output_disp (insn_start_frag, insn_start_off);
3e73aa7c 9587
29b0f896 9588 if (i.imm_operands)
2bbd9c25 9589 output_imm (insn_start_frag, insn_start_off);
9c33702b
JB
9590
9591 /*
9592 * frag_now_fix () returning plain abs_section_offset when we're in the
9593 * absolute section, and abs_section_offset not getting updated as data
9594 * gets added to the frag breaks the logic below.
9595 */
9596 if (now_seg != absolute_section)
9597 {
9598 j = encoding_length (insn_start_frag, insn_start_off, frag_more (0));
9599 if (j > 15)
9600 as_warn (_("instruction length of %u bytes exceeds the limit of 15"),
9601 j);
e379e5f3
L
9602 else if (fragP)
9603 {
9604 /* NB: Don't add prefix with GOTPC relocation since
9605 output_disp() above depends on the fixed encoding
9606 length. Can't add prefix with TLS relocation since
9607 it breaks TLS linker optimization. */
9608 unsigned int max = i.has_gotpc_tls_reloc ? 0 : 15 - j;
9609 /* Prefix count on the current instruction. */
9610 unsigned int count = i.vex.length;
9611 unsigned int k;
9612 for (k = 0; k < ARRAY_SIZE (i.prefix); k++)
9613 /* REX byte is encoded in VEX/EVEX prefix. */
9614 if (i.prefix[k] && (k != REX_PREFIX || !i.vex.length))
9615 count++;
9616
9617 /* Count prefixes for extended opcode maps. */
9618 if (!i.vex.length)
389d00a5 9619 switch (i.tm.opcode_modifier.opcodespace)
e379e5f3 9620 {
389d00a5 9621 case SPACE_BASE:
e379e5f3 9622 break;
389d00a5
JB
9623 case SPACE_0F:
9624 count++;
e379e5f3 9625 break;
389d00a5
JB
9626 case SPACE_0F38:
9627 case SPACE_0F3A:
9628 count += 2;
e379e5f3
L
9629 break;
9630 default:
9631 abort ();
9632 }
9633
9634 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
9635 == BRANCH_PREFIX)
9636 {
9637 /* Set the maximum prefix size in BRANCH_PREFIX
9638 frag. */
9639 if (fragP->tc_frag_data.max_bytes > max)
9640 fragP->tc_frag_data.max_bytes = max;
9641 if (fragP->tc_frag_data.max_bytes > count)
9642 fragP->tc_frag_data.max_bytes -= count;
9643 else
9644 fragP->tc_frag_data.max_bytes = 0;
9645 }
9646 else
9647 {
9648 /* Remember the maximum prefix size in FUSED_JCC_PADDING
9649 frag. */
9650 unsigned int max_prefix_size;
9651 if (align_branch_prefix_size > max)
9652 max_prefix_size = max;
9653 else
9654 max_prefix_size = align_branch_prefix_size;
9655 if (max_prefix_size > count)
9656 fragP->tc_frag_data.max_prefix_length
9657 = max_prefix_size - count;
9658 }
9659
9660 /* Use existing segment prefix if possible. Use CS
9661 segment prefix in 64-bit mode. In 32-bit mode, use SS
9662 segment prefix with ESP/EBP base register and use DS
9663 segment prefix without ESP/EBP base register. */
9664 if (i.prefix[SEG_PREFIX])
9665 fragP->tc_frag_data.default_prefix = i.prefix[SEG_PREFIX];
9666 else if (flag_code == CODE_64BIT)
9667 fragP->tc_frag_data.default_prefix = CS_PREFIX_OPCODE;
9668 else if (i.base_reg
9669 && (i.base_reg->reg_num == 4
9670 || i.base_reg->reg_num == 5))
9671 fragP->tc_frag_data.default_prefix = SS_PREFIX_OPCODE;
9672 else
9673 fragP->tc_frag_data.default_prefix = DS_PREFIX_OPCODE;
9674 }
9c33702b 9675 }
29b0f896 9676 }
252b5132 9677
e379e5f3
L
9678 /* NB: Don't work with COND_JUMP86 without i386. */
9679 if (align_branch_power
9680 && now_seg != absolute_section
9681 && cpu_arch_flags.bitfield.cpui386)
9682 {
9683 /* Terminate each frag so that we can add prefix and check for
9684 fused jcc. */
9685 frag_wane (frag_now);
9686 frag_new (0);
9687 }
9688
29b0f896
AM
9689#ifdef DEBUG386
9690 if (flag_debug)
9691 {
7b81dfbb 9692 pi ("" /*line*/, &i);
29b0f896
AM
9693 }
9694#endif /* DEBUG386 */
9695}
252b5132 9696
e205caa7
L
9697/* Return the size of the displacement operand N. */
9698
9699static int
9700disp_size (unsigned int n)
9701{
9702 int size = 4;
43234a1e 9703
b5014f7a 9704 if (i.types[n].bitfield.disp64)
40fb9820
L
9705 size = 8;
9706 else if (i.types[n].bitfield.disp8)
9707 size = 1;
9708 else if (i.types[n].bitfield.disp16)
9709 size = 2;
e205caa7
L
9710 return size;
9711}
9712
9713/* Return the size of the immediate operand N. */
9714
9715static int
9716imm_size (unsigned int n)
9717{
9718 int size = 4;
40fb9820
L
9719 if (i.types[n].bitfield.imm64)
9720 size = 8;
9721 else if (i.types[n].bitfield.imm8 || i.types[n].bitfield.imm8s)
9722 size = 1;
9723 else if (i.types[n].bitfield.imm16)
9724 size = 2;
e205caa7
L
9725 return size;
9726}
9727
29b0f896 9728static void
64e74474 9729output_disp (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
9730{
9731 char *p;
9732 unsigned int n;
252b5132 9733
29b0f896
AM
9734 for (n = 0; n < i.operands; n++)
9735 {
b5014f7a 9736 if (operand_type_check (i.types[n], disp))
29b0f896 9737 {
48ef937e
JB
9738 int size = disp_size (n);
9739
9740 if (now_seg == absolute_section)
9741 abs_section_offset += size;
9742 else if (i.op[n].disps->X_op == O_constant)
29b0f896 9743 {
43234a1e 9744 offsetT val = i.op[n].disps->X_add_number;
252b5132 9745
629cfaf1
JB
9746 val = offset_in_range (val >> (size == 1 ? i.memshift : 0),
9747 size);
29b0f896
AM
9748 p = frag_more (size);
9749 md_number_to_chars (p, val, size);
9750 }
9751 else
9752 {
f86103b7 9753 enum bfd_reloc_code_real reloc_type;
40fb9820 9754 int sign = i.types[n].bitfield.disp32s;
29b0f896 9755 int pcrel = (i.flags[n] & Operand_PCrel) != 0;
02a86693 9756 fixS *fixP;
29b0f896 9757
e205caa7 9758 /* We can't have 8 bit displacement here. */
9c2799c2 9759 gas_assert (!i.types[n].bitfield.disp8);
e205caa7 9760
29b0f896
AM
9761 /* The PC relative address is computed relative
9762 to the instruction boundary, so in case immediate
9763 fields follows, we need to adjust the value. */
9764 if (pcrel && i.imm_operands)
9765 {
29b0f896 9766 unsigned int n1;
e205caa7 9767 int sz = 0;
252b5132 9768
29b0f896 9769 for (n1 = 0; n1 < i.operands; n1++)
40fb9820 9770 if (operand_type_check (i.types[n1], imm))
252b5132 9771 {
e205caa7
L
9772 /* Only one immediate is allowed for PC
9773 relative address. */
9c2799c2 9774 gas_assert (sz == 0);
e205caa7
L
9775 sz = imm_size (n1);
9776 i.op[n].disps->X_add_number -= sz;
252b5132 9777 }
29b0f896 9778 /* We should find the immediate. */
9c2799c2 9779 gas_assert (sz != 0);
29b0f896 9780 }
520dc8e8 9781
29b0f896 9782 p = frag_more (size);
d258b828 9783 reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
d6ab8113 9784 if (GOT_symbol
2bbd9c25 9785 && GOT_symbol == i.op[n].disps->X_add_symbol
d6ab8113 9786 && (((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
9787 || reloc_type == BFD_RELOC_X86_64_32S
9788 || (reloc_type == BFD_RELOC_64
9789 && object_64bit))
d6ab8113
JB
9790 && (i.op[n].disps->X_op == O_symbol
9791 || (i.op[n].disps->X_op == O_add
9792 && ((symbol_get_value_expression
9793 (i.op[n].disps->X_op_symbol)->X_op)
9794 == O_subtract))))
9795 || reloc_type == BFD_RELOC_32_PCREL))
2bbd9c25 9796 {
4fa24527 9797 if (!object_64bit)
7b81dfbb
AJ
9798 {
9799 reloc_type = BFD_RELOC_386_GOTPC;
e379e5f3 9800 i.has_gotpc_tls_reloc = TRUE;
d583596c
JB
9801 i.op[n].imms->X_add_number +=
9802 encoding_length (insn_start_frag, insn_start_off, p);
7b81dfbb
AJ
9803 }
9804 else if (reloc_type == BFD_RELOC_64)
9805 reloc_type = BFD_RELOC_X86_64_GOTPC64;
d6ab8113 9806 else
7b81dfbb
AJ
9807 /* Don't do the adjustment for x86-64, as there
9808 the pcrel addressing is relative to the _next_
9809 insn, and that is taken care of in other code. */
d6ab8113 9810 reloc_type = BFD_RELOC_X86_64_GOTPC32;
2bbd9c25 9811 }
e379e5f3
L
9812 else if (align_branch_power)
9813 {
9814 switch (reloc_type)
9815 {
9816 case BFD_RELOC_386_TLS_GD:
9817 case BFD_RELOC_386_TLS_LDM:
9818 case BFD_RELOC_386_TLS_IE:
9819 case BFD_RELOC_386_TLS_IE_32:
9820 case BFD_RELOC_386_TLS_GOTIE:
9821 case BFD_RELOC_386_TLS_GOTDESC:
9822 case BFD_RELOC_386_TLS_DESC_CALL:
9823 case BFD_RELOC_X86_64_TLSGD:
9824 case BFD_RELOC_X86_64_TLSLD:
9825 case BFD_RELOC_X86_64_GOTTPOFF:
9826 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
9827 case BFD_RELOC_X86_64_TLSDESC_CALL:
9828 i.has_gotpc_tls_reloc = TRUE;
9829 default:
9830 break;
9831 }
9832 }
02a86693
L
9833 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal,
9834 size, i.op[n].disps, pcrel,
9835 reloc_type);
9836 /* Check for "call/jmp *mem", "mov mem, %reg",
9837 "test %reg, mem" and "binop mem, %reg" where binop
9838 is one of adc, add, and, cmp, or, sbb, sub, xor
e60f4d3b
L
9839 instructions without data prefix. Always generate
9840 R_386_GOT32X for "sym*GOT" operand in 32-bit mode. */
9841 if (i.prefix[DATA_PREFIX] == 0
9842 && (generate_relax_relocations
9843 || (!object_64bit
9844 && i.rm.mode == 0
9845 && i.rm.regmem == 5))
0cb4071e
L
9846 && (i.rm.mode == 2
9847 || (i.rm.mode == 0 && i.rm.regmem == 5))
389d00a5 9848 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
02a86693
L
9849 && ((i.operands == 1
9850 && i.tm.base_opcode == 0xff
9851 && (i.rm.reg == 2 || i.rm.reg == 4))
9852 || (i.operands == 2
9853 && (i.tm.base_opcode == 0x8b
9854 || i.tm.base_opcode == 0x85
2ae4c703 9855 || (i.tm.base_opcode & ~0x38) == 0x03))))
02a86693
L
9856 {
9857 if (object_64bit)
9858 {
9859 fixP->fx_tcbit = i.rex != 0;
9860 if (i.base_reg
e968fc9b 9861 && (i.base_reg->reg_num == RegIP))
02a86693
L
9862 fixP->fx_tcbit2 = 1;
9863 }
9864 else
9865 fixP->fx_tcbit2 = 1;
9866 }
29b0f896
AM
9867 }
9868 }
9869 }
9870}
252b5132 9871
29b0f896 9872static void
64e74474 9873output_imm (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
9874{
9875 char *p;
9876 unsigned int n;
252b5132 9877
29b0f896
AM
9878 for (n = 0; n < i.operands; n++)
9879 {
43234a1e 9880 /* Skip SAE/RC Imm operand in EVEX. They are already handled. */
ca5312a2 9881 if (i.rounding.type != rc_none && n == i.rounding.operand)
43234a1e
L
9882 continue;
9883
40fb9820 9884 if (operand_type_check (i.types[n], imm))
29b0f896 9885 {
48ef937e
JB
9886 int size = imm_size (n);
9887
9888 if (now_seg == absolute_section)
9889 abs_section_offset += size;
9890 else if (i.op[n].imms->X_op == O_constant)
29b0f896 9891 {
29b0f896 9892 offsetT val;
b4cac588 9893
29b0f896
AM
9894 val = offset_in_range (i.op[n].imms->X_add_number,
9895 size);
9896 p = frag_more (size);
9897 md_number_to_chars (p, val, size);
9898 }
9899 else
9900 {
9901 /* Not absolute_section.
9902 Need a 32-bit fixup (don't support 8bit
9903 non-absolute imms). Try to support other
9904 sizes ... */
f86103b7 9905 enum bfd_reloc_code_real reloc_type;
e205caa7 9906 int sign;
29b0f896 9907
40fb9820 9908 if (i.types[n].bitfield.imm32s
a7d61044 9909 && (i.suffix == QWORD_MNEM_SUFFIX
40fb9820 9910 || (!i.suffix && i.tm.opcode_modifier.no_lsuf)))
29b0f896 9911 sign = 1;
e205caa7
L
9912 else
9913 sign = 0;
520dc8e8 9914
29b0f896 9915 p = frag_more (size);
d258b828 9916 reloc_type = reloc (size, 0, sign, i.reloc[n]);
f86103b7 9917
2bbd9c25
JJ
9918 /* This is tough to explain. We end up with this one if we
9919 * have operands that look like
9920 * "_GLOBAL_OFFSET_TABLE_+[.-.L284]". The goal here is to
9921 * obtain the absolute address of the GOT, and it is strongly
9922 * preferable from a performance point of view to avoid using
9923 * a runtime relocation for this. The actual sequence of
9924 * instructions often look something like:
9925 *
9926 * call .L66
9927 * .L66:
9928 * popl %ebx
9929 * addl $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
9930 *
9931 * The call and pop essentially return the absolute address
9932 * of the label .L66 and store it in %ebx. The linker itself
9933 * will ultimately change the first operand of the addl so
9934 * that %ebx points to the GOT, but to keep things simple, the
9935 * .o file must have this operand set so that it generates not
9936 * the absolute address of .L66, but the absolute address of
9937 * itself. This allows the linker itself simply treat a GOTPC
9938 * relocation as asking for a pcrel offset to the GOT to be
9939 * added in, and the addend of the relocation is stored in the
9940 * operand field for the instruction itself.
9941 *
9942 * Our job here is to fix the operand so that it would add
9943 * the correct offset so that %ebx would point to itself. The
9944 * thing that is tricky is that .-.L66 will point to the
9945 * beginning of the instruction, so we need to further modify
9946 * the operand so that it will point to itself. There are
9947 * other cases where you have something like:
9948 *
9949 * .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
9950 *
9951 * and here no correction would be required. Internally in
9952 * the assembler we treat operands of this form as not being
9953 * pcrel since the '.' is explicitly mentioned, and I wonder
9954 * whether it would simplify matters to do it this way. Who
9955 * knows. In earlier versions of the PIC patches, the
9956 * pcrel_adjust field was used to store the correction, but
9957 * since the expression is not pcrel, I felt it would be
9958 * confusing to do it this way. */
9959
d6ab8113 9960 if ((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
9961 || reloc_type == BFD_RELOC_X86_64_32S
9962 || reloc_type == BFD_RELOC_64)
29b0f896
AM
9963 && GOT_symbol
9964 && GOT_symbol == i.op[n].imms->X_add_symbol
9965 && (i.op[n].imms->X_op == O_symbol
9966 || (i.op[n].imms->X_op == O_add
9967 && ((symbol_get_value_expression
9968 (i.op[n].imms->X_op_symbol)->X_op)
9969 == O_subtract))))
9970 {
4fa24527 9971 if (!object_64bit)
d6ab8113 9972 reloc_type = BFD_RELOC_386_GOTPC;
7b81dfbb 9973 else if (size == 4)
d6ab8113 9974 reloc_type = BFD_RELOC_X86_64_GOTPC32;
7b81dfbb
AJ
9975 else if (size == 8)
9976 reloc_type = BFD_RELOC_X86_64_GOTPC64;
e379e5f3 9977 i.has_gotpc_tls_reloc = TRUE;
d583596c
JB
9978 i.op[n].imms->X_add_number +=
9979 encoding_length (insn_start_frag, insn_start_off, p);
29b0f896 9980 }
29b0f896
AM
9981 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
9982 i.op[n].imms, 0, reloc_type);
9983 }
9984 }
9985 }
252b5132
RH
9986}
9987\f
d182319b
JB
9988/* x86_cons_fix_new is called via the expression parsing code when a
9989 reloc is needed. We use this hook to get the correct .got reloc. */
d182319b
JB
9990static int cons_sign = -1;
9991
9992void
e3bb37b5 9993x86_cons_fix_new (fragS *frag, unsigned int off, unsigned int len,
62ebcb5c 9994 expressionS *exp, bfd_reloc_code_real_type r)
d182319b 9995{
d258b828 9996 r = reloc (len, 0, cons_sign, r);
d182319b
JB
9997
9998#ifdef TE_PE
9999 if (exp->X_op == O_secrel)
10000 {
10001 exp->X_op = O_symbol;
10002 r = BFD_RELOC_32_SECREL;
10003 }
10004#endif
10005
10006 fix_new_exp (frag, off, len, exp, 0, r);
10007}
10008
357d1bd8
L
10009/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
10010 purpose of the `.dc.a' internal pseudo-op. */
10011
10012int
10013x86_address_bytes (void)
10014{
10015 if ((stdoutput->arch_info->mach & bfd_mach_x64_32))
10016 return 4;
10017 return stdoutput->arch_info->bits_per_address / 8;
10018}
10019
d382c579
TG
10020#if !(defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined (OBJ_MACH_O)) \
10021 || defined (LEX_AT)
d258b828 10022# define lex_got(reloc, adjust, types) NULL
718ddfc0 10023#else
f3c180ae
AM
10024/* Parse operands of the form
10025 <symbol>@GOTOFF+<nnn>
10026 and similar .plt or .got references.
10027
10028 If we find one, set up the correct relocation in RELOC and copy the
10029 input string, minus the `@GOTOFF' into a malloc'd buffer for
10030 parsing by the calling routine. Return this buffer, and if ADJUST
10031 is non-null set it to the length of the string we removed from the
10032 input line. Otherwise return NULL. */
10033static char *
91d6fa6a 10034lex_got (enum bfd_reloc_code_real *rel,
64e74474 10035 int *adjust,
d258b828 10036 i386_operand_type *types)
f3c180ae 10037{
7b81dfbb
AJ
10038 /* Some of the relocations depend on the size of what field is to
10039 be relocated. But in our callers i386_immediate and i386_displacement
10040 we don't yet know the operand size (this will be set by insn
10041 matching). Hence we record the word32 relocation here,
10042 and adjust the reloc according to the real size in reloc(). */
f3c180ae
AM
10043 static const struct {
10044 const char *str;
cff8d58a 10045 int len;
4fa24527 10046 const enum bfd_reloc_code_real rel[2];
40fb9820 10047 const i386_operand_type types64;
844bf810 10048 bfd_boolean need_GOT_symbol;
f3c180ae 10049 } gotrel[] = {
8ce3d284 10050#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
10051 { STRING_COMMA_LEN ("SIZE"), { BFD_RELOC_SIZE32,
10052 BFD_RELOC_SIZE32 },
844bf810 10053 OPERAND_TYPE_IMM32_64, FALSE },
8ce3d284 10054#endif
cff8d58a
L
10055 { STRING_COMMA_LEN ("PLTOFF"), { _dummy_first_bfd_reloc_code_real,
10056 BFD_RELOC_X86_64_PLTOFF64 },
844bf810 10057 OPERAND_TYPE_IMM64, TRUE },
cff8d58a
L
10058 { STRING_COMMA_LEN ("PLT"), { BFD_RELOC_386_PLT32,
10059 BFD_RELOC_X86_64_PLT32 },
844bf810 10060 OPERAND_TYPE_IMM32_32S_DISP32, FALSE },
cff8d58a
L
10061 { STRING_COMMA_LEN ("GOTPLT"), { _dummy_first_bfd_reloc_code_real,
10062 BFD_RELOC_X86_64_GOTPLT64 },
844bf810 10063 OPERAND_TYPE_IMM64_DISP64, TRUE },
cff8d58a
L
10064 { STRING_COMMA_LEN ("GOTOFF"), { BFD_RELOC_386_GOTOFF,
10065 BFD_RELOC_X86_64_GOTOFF64 },
844bf810 10066 OPERAND_TYPE_IMM64_DISP64, TRUE },
cff8d58a
L
10067 { STRING_COMMA_LEN ("GOTPCREL"), { _dummy_first_bfd_reloc_code_real,
10068 BFD_RELOC_X86_64_GOTPCREL },
844bf810 10069 OPERAND_TYPE_IMM32_32S_DISP32, TRUE },
cff8d58a
L
10070 { STRING_COMMA_LEN ("TLSGD"), { BFD_RELOC_386_TLS_GD,
10071 BFD_RELOC_X86_64_TLSGD },
844bf810 10072 OPERAND_TYPE_IMM32_32S_DISP32, TRUE },
cff8d58a
L
10073 { STRING_COMMA_LEN ("TLSLDM"), { BFD_RELOC_386_TLS_LDM,
10074 _dummy_first_bfd_reloc_code_real },
844bf810 10075 OPERAND_TYPE_NONE, TRUE },
cff8d58a
L
10076 { STRING_COMMA_LEN ("TLSLD"), { _dummy_first_bfd_reloc_code_real,
10077 BFD_RELOC_X86_64_TLSLD },
844bf810 10078 OPERAND_TYPE_IMM32_32S_DISP32, TRUE },
cff8d58a
L
10079 { STRING_COMMA_LEN ("GOTTPOFF"), { BFD_RELOC_386_TLS_IE_32,
10080 BFD_RELOC_X86_64_GOTTPOFF },
844bf810 10081 OPERAND_TYPE_IMM32_32S_DISP32, TRUE },
cff8d58a
L
10082 { STRING_COMMA_LEN ("TPOFF"), { BFD_RELOC_386_TLS_LE_32,
10083 BFD_RELOC_X86_64_TPOFF32 },
844bf810 10084 OPERAND_TYPE_IMM32_32S_64_DISP32_64, TRUE },
cff8d58a
L
10085 { STRING_COMMA_LEN ("NTPOFF"), { BFD_RELOC_386_TLS_LE,
10086 _dummy_first_bfd_reloc_code_real },
844bf810 10087 OPERAND_TYPE_NONE, TRUE },
cff8d58a
L
10088 { STRING_COMMA_LEN ("DTPOFF"), { BFD_RELOC_386_TLS_LDO_32,
10089 BFD_RELOC_X86_64_DTPOFF32 },
844bf810 10090 OPERAND_TYPE_IMM32_32S_64_DISP32_64, TRUE },
cff8d58a
L
10091 { STRING_COMMA_LEN ("GOTNTPOFF"),{ BFD_RELOC_386_TLS_GOTIE,
10092 _dummy_first_bfd_reloc_code_real },
844bf810 10093 OPERAND_TYPE_NONE, TRUE },
cff8d58a
L
10094 { STRING_COMMA_LEN ("INDNTPOFF"),{ BFD_RELOC_386_TLS_IE,
10095 _dummy_first_bfd_reloc_code_real },
844bf810 10096 OPERAND_TYPE_NONE, TRUE },
cff8d58a
L
10097 { STRING_COMMA_LEN ("GOT"), { BFD_RELOC_386_GOT32,
10098 BFD_RELOC_X86_64_GOT32 },
844bf810 10099 OPERAND_TYPE_IMM32_32S_64_DISP32, TRUE },
cff8d58a
L
10100 { STRING_COMMA_LEN ("TLSDESC"), { BFD_RELOC_386_TLS_GOTDESC,
10101 BFD_RELOC_X86_64_GOTPC32_TLSDESC },
844bf810 10102 OPERAND_TYPE_IMM32_32S_DISP32, TRUE },
cff8d58a
L
10103 { STRING_COMMA_LEN ("TLSCALL"), { BFD_RELOC_386_TLS_DESC_CALL,
10104 BFD_RELOC_X86_64_TLSDESC_CALL },
844bf810 10105 OPERAND_TYPE_IMM32_32S_DISP32, TRUE },
f3c180ae
AM
10106 };
10107 char *cp;
10108 unsigned int j;
10109
d382c579 10110#if defined (OBJ_MAYBE_ELF)
718ddfc0
JB
10111 if (!IS_ELF)
10112 return NULL;
d382c579 10113#endif
718ddfc0 10114
f3c180ae 10115 for (cp = input_line_pointer; *cp != '@'; cp++)
67c11a9b 10116 if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
f3c180ae
AM
10117 return NULL;
10118
47465058 10119 for (j = 0; j < ARRAY_SIZE (gotrel); j++)
f3c180ae 10120 {
cff8d58a 10121 int len = gotrel[j].len;
28f81592 10122 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
f3c180ae 10123 {
4fa24527 10124 if (gotrel[j].rel[object_64bit] != 0)
f3c180ae 10125 {
28f81592
AM
10126 int first, second;
10127 char *tmpbuf, *past_reloc;
f3c180ae 10128
91d6fa6a 10129 *rel = gotrel[j].rel[object_64bit];
f3c180ae 10130
3956db08
JB
10131 if (types)
10132 {
10133 if (flag_code != CODE_64BIT)
40fb9820
L
10134 {
10135 types->bitfield.imm32 = 1;
10136 types->bitfield.disp32 = 1;
10137 }
3956db08
JB
10138 else
10139 *types = gotrel[j].types64;
10140 }
10141
844bf810 10142 if (gotrel[j].need_GOT_symbol && GOT_symbol == NULL)
f3c180ae
AM
10143 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
10144
28f81592 10145 /* The length of the first part of our input line. */
f3c180ae 10146 first = cp - input_line_pointer;
28f81592
AM
10147
10148 /* The second part goes from after the reloc token until
67c11a9b 10149 (and including) an end_of_line char or comma. */
28f81592 10150 past_reloc = cp + 1 + len;
67c11a9b
AM
10151 cp = past_reloc;
10152 while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
10153 ++cp;
10154 second = cp + 1 - past_reloc;
28f81592
AM
10155
10156 /* Allocate and copy string. The trailing NUL shouldn't
10157 be necessary, but be safe. */
add39d23 10158 tmpbuf = XNEWVEC (char, first + second + 2);
f3c180ae 10159 memcpy (tmpbuf, input_line_pointer, first);
0787a12d
AM
10160 if (second != 0 && *past_reloc != ' ')
10161 /* Replace the relocation token with ' ', so that
10162 errors like foo@GOTOFF1 will be detected. */
10163 tmpbuf[first++] = ' ';
af89796a
L
10164 else
10165 /* Increment length by 1 if the relocation token is
10166 removed. */
10167 len++;
10168 if (adjust)
10169 *adjust = len;
0787a12d
AM
10170 memcpy (tmpbuf + first, past_reloc, second);
10171 tmpbuf[first + second] = '\0';
f3c180ae
AM
10172 return tmpbuf;
10173 }
10174
4fa24527
JB
10175 as_bad (_("@%s reloc is not supported with %d-bit output format"),
10176 gotrel[j].str, 1 << (5 + object_64bit));
f3c180ae
AM
10177 return NULL;
10178 }
10179 }
10180
10181 /* Might be a symbol version string. Don't as_bad here. */
10182 return NULL;
10183}
4e4f7c87 10184#endif
f3c180ae 10185
a988325c
NC
10186#ifdef TE_PE
10187#ifdef lex_got
10188#undef lex_got
10189#endif
10190/* Parse operands of the form
10191 <symbol>@SECREL32+<nnn>
10192
10193 If we find one, set up the correct relocation in RELOC and copy the
10194 input string, minus the `@SECREL32' into a malloc'd buffer for
10195 parsing by the calling routine. Return this buffer, and if ADJUST
10196 is non-null set it to the length of the string we removed from the
34bca508
L
10197 input line. Otherwise return NULL.
10198
a988325c
NC
10199 This function is copied from the ELF version above adjusted for PE targets. */
10200
10201static char *
10202lex_got (enum bfd_reloc_code_real *rel ATTRIBUTE_UNUSED,
10203 int *adjust ATTRIBUTE_UNUSED,
d258b828 10204 i386_operand_type *types)
a988325c
NC
10205{
10206 static const struct
10207 {
10208 const char *str;
10209 int len;
10210 const enum bfd_reloc_code_real rel[2];
10211 const i386_operand_type types64;
10212 }
10213 gotrel[] =
10214 {
10215 { STRING_COMMA_LEN ("SECREL32"), { BFD_RELOC_32_SECREL,
10216 BFD_RELOC_32_SECREL },
10217 OPERAND_TYPE_IMM32_32S_64_DISP32_64 },
10218 };
10219
10220 char *cp;
10221 unsigned j;
10222
10223 for (cp = input_line_pointer; *cp != '@'; cp++)
10224 if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
10225 return NULL;
10226
10227 for (j = 0; j < ARRAY_SIZE (gotrel); j++)
10228 {
10229 int len = gotrel[j].len;
10230
10231 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
10232 {
10233 if (gotrel[j].rel[object_64bit] != 0)
10234 {
10235 int first, second;
10236 char *tmpbuf, *past_reloc;
10237
10238 *rel = gotrel[j].rel[object_64bit];
10239 if (adjust)
10240 *adjust = len;
10241
10242 if (types)
10243 {
10244 if (flag_code != CODE_64BIT)
10245 {
10246 types->bitfield.imm32 = 1;
10247 types->bitfield.disp32 = 1;
10248 }
10249 else
10250 *types = gotrel[j].types64;
10251 }
10252
10253 /* The length of the first part of our input line. */
10254 first = cp - input_line_pointer;
10255
10256 /* The second part goes from after the reloc token until
10257 (and including) an end_of_line char or comma. */
10258 past_reloc = cp + 1 + len;
10259 cp = past_reloc;
10260 while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
10261 ++cp;
10262 second = cp + 1 - past_reloc;
10263
10264 /* Allocate and copy string. The trailing NUL shouldn't
10265 be necessary, but be safe. */
add39d23 10266 tmpbuf = XNEWVEC (char, first + second + 2);
a988325c
NC
10267 memcpy (tmpbuf, input_line_pointer, first);
10268 if (second != 0 && *past_reloc != ' ')
10269 /* Replace the relocation token with ' ', so that
10270 errors like foo@SECLREL321 will be detected. */
10271 tmpbuf[first++] = ' ';
10272 memcpy (tmpbuf + first, past_reloc, second);
10273 tmpbuf[first + second] = '\0';
10274 return tmpbuf;
10275 }
10276
10277 as_bad (_("@%s reloc is not supported with %d-bit output format"),
10278 gotrel[j].str, 1 << (5 + object_64bit));
10279 return NULL;
10280 }
10281 }
10282
10283 /* Might be a symbol version string. Don't as_bad here. */
10284 return NULL;
10285}
10286
10287#endif /* TE_PE */
10288
62ebcb5c 10289bfd_reloc_code_real_type
e3bb37b5 10290x86_cons (expressionS *exp, int size)
f3c180ae 10291{
62ebcb5c
AM
10292 bfd_reloc_code_real_type got_reloc = NO_RELOC;
10293
ee86248c
JB
10294 intel_syntax = -intel_syntax;
10295
3c7b9c2c 10296 exp->X_md = 0;
4fa24527 10297 if (size == 4 || (object_64bit && size == 8))
f3c180ae
AM
10298 {
10299 /* Handle @GOTOFF and the like in an expression. */
10300 char *save;
10301 char *gotfree_input_line;
4a57f2cf 10302 int adjust = 0;
f3c180ae
AM
10303
10304 save = input_line_pointer;
d258b828 10305 gotfree_input_line = lex_got (&got_reloc, &adjust, NULL);
f3c180ae
AM
10306 if (gotfree_input_line)
10307 input_line_pointer = gotfree_input_line;
10308
10309 expression (exp);
10310
10311 if (gotfree_input_line)
10312 {
10313 /* expression () has merrily parsed up to the end of line,
10314 or a comma - in the wrong buffer. Transfer how far
10315 input_line_pointer has moved to the right buffer. */
10316 input_line_pointer = (save
10317 + (input_line_pointer - gotfree_input_line)
10318 + adjust);
10319 free (gotfree_input_line);
3992d3b7
AM
10320 if (exp->X_op == O_constant
10321 || exp->X_op == O_absent
10322 || exp->X_op == O_illegal
0398aac5 10323 || exp->X_op == O_register
3992d3b7
AM
10324 || exp->X_op == O_big)
10325 {
10326 char c = *input_line_pointer;
10327 *input_line_pointer = 0;
10328 as_bad (_("missing or invalid expression `%s'"), save);
10329 *input_line_pointer = c;
10330 }
b9519cfe
L
10331 else if ((got_reloc == BFD_RELOC_386_PLT32
10332 || got_reloc == BFD_RELOC_X86_64_PLT32)
10333 && exp->X_op != O_symbol)
10334 {
10335 char c = *input_line_pointer;
10336 *input_line_pointer = 0;
10337 as_bad (_("invalid PLT expression `%s'"), save);
10338 *input_line_pointer = c;
10339 }
f3c180ae
AM
10340 }
10341 }
10342 else
10343 expression (exp);
ee86248c
JB
10344
10345 intel_syntax = -intel_syntax;
10346
10347 if (intel_syntax)
10348 i386_intel_simplify (exp);
62ebcb5c
AM
10349
10350 return got_reloc;
f3c180ae 10351}
f3c180ae 10352
9f32dd5b
L
10353static void
10354signed_cons (int size)
6482c264 10355{
d182319b
JB
10356 if (flag_code == CODE_64BIT)
10357 cons_sign = 1;
10358 cons (size);
10359 cons_sign = -1;
6482c264
NC
10360}
10361
d182319b 10362#ifdef TE_PE
6482c264 10363static void
7016a5d5 10364pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
6482c264
NC
10365{
10366 expressionS exp;
10367
10368 do
10369 {
10370 expression (&exp);
10371 if (exp.X_op == O_symbol)
10372 exp.X_op = O_secrel;
10373
10374 emit_expr (&exp, 4);
10375 }
10376 while (*input_line_pointer++ == ',');
10377
10378 input_line_pointer--;
10379 demand_empty_rest_of_line ();
10380}
6482c264
NC
10381#endif
10382
43234a1e
L
10383/* Handle Vector operations. */
10384
10385static char *
10386check_VecOperations (char *op_string, char *op_end)
10387{
10388 const reg_entry *mask;
10389 const char *saved;
10390 char *end_op;
10391
10392 while (*op_string
10393 && (op_end == NULL || op_string < op_end))
10394 {
10395 saved = op_string;
10396 if (*op_string == '{')
10397 {
10398 op_string++;
10399
10400 /* Check broadcasts. */
10401 if (strncmp (op_string, "1to", 3) == 0)
10402 {
5273a3cd 10403 unsigned int bcst_type;
43234a1e 10404
5273a3cd 10405 if (i.broadcast.type)
43234a1e
L
10406 goto duplicated_vec_op;
10407
10408 op_string += 3;
10409 if (*op_string == '8')
8e6e0792 10410 bcst_type = 8;
b28d1bda 10411 else if (*op_string == '4')
8e6e0792 10412 bcst_type = 4;
b28d1bda 10413 else if (*op_string == '2')
8e6e0792 10414 bcst_type = 2;
43234a1e
L
10415 else if (*op_string == '1'
10416 && *(op_string+1) == '6')
10417 {
8e6e0792 10418 bcst_type = 16;
43234a1e
L
10419 op_string++;
10420 }
10421 else
10422 {
10423 as_bad (_("Unsupported broadcast: `%s'"), saved);
10424 return NULL;
10425 }
10426 op_string++;
10427
5273a3cd
JB
10428 i.broadcast.type = bcst_type;
10429 i.broadcast.operand = this_operand;
43234a1e
L
10430 }
10431 /* Check masking operation. */
10432 else if ((mask = parse_register (op_string, &end_op)) != NULL)
10433 {
8a6fb3f9
JB
10434 if (mask == &bad_reg)
10435 return NULL;
10436
43234a1e 10437 /* k0 can't be used for write mask. */
f74a6307 10438 if (mask->reg_type.bitfield.class != RegMask || !mask->reg_num)
43234a1e 10439 {
6d2cd6b2
JB
10440 as_bad (_("`%s%s' can't be used for write mask"),
10441 register_prefix, mask->reg_name);
43234a1e
L
10442 return NULL;
10443 }
10444
6225c532 10445 if (!i.mask.reg)
43234a1e 10446 {
6225c532
JB
10447 i.mask.reg = mask;
10448 i.mask.operand = this_operand;
43234a1e 10449 }
6225c532
JB
10450 else if (i.mask.reg->reg_num)
10451 goto duplicated_vec_op;
43234a1e
L
10452 else
10453 {
6225c532 10454 i.mask.reg = mask;
43234a1e
L
10455
10456 /* Only "{z}" is allowed here. No need to check
10457 zeroing mask explicitly. */
6225c532 10458 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
10459 {
10460 as_bad (_("invalid write mask `%s'"), saved);
10461 return NULL;
10462 }
10463 }
10464
10465 op_string = end_op;
10466 }
10467 /* Check zeroing-flag for masking operation. */
10468 else if (*op_string == 'z')
10469 {
6225c532 10470 if (!i.mask.reg)
43234a1e 10471 {
6225c532
JB
10472 i.mask.reg = reg_k0;
10473 i.mask.zeroing = 1;
10474 i.mask.operand = this_operand;
43234a1e
L
10475 }
10476 else
10477 {
6225c532 10478 if (i.mask.zeroing)
43234a1e
L
10479 {
10480 duplicated_vec_op:
10481 as_bad (_("duplicated `%s'"), saved);
10482 return NULL;
10483 }
10484
6225c532 10485 i.mask.zeroing = 1;
43234a1e
L
10486
10487 /* Only "{%k}" is allowed here. No need to check mask
10488 register explicitly. */
6225c532 10489 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
10490 {
10491 as_bad (_("invalid zeroing-masking `%s'"),
10492 saved);
10493 return NULL;
10494 }
10495 }
10496
10497 op_string++;
10498 }
10499 else
10500 goto unknown_vec_op;
10501
10502 if (*op_string != '}')
10503 {
10504 as_bad (_("missing `}' in `%s'"), saved);
10505 return NULL;
10506 }
10507 op_string++;
0ba3a731
L
10508
10509 /* Strip whitespace since the addition of pseudo prefixes
10510 changed how the scrubber treats '{'. */
10511 if (is_space_char (*op_string))
10512 ++op_string;
10513
43234a1e
L
10514 continue;
10515 }
10516 unknown_vec_op:
10517 /* We don't know this one. */
10518 as_bad (_("unknown vector operation: `%s'"), saved);
10519 return NULL;
10520 }
10521
6225c532 10522 if (i.mask.reg && i.mask.zeroing && !i.mask.reg->reg_num)
6d2cd6b2
JB
10523 {
10524 as_bad (_("zeroing-masking only allowed with write mask"));
10525 return NULL;
10526 }
10527
43234a1e
L
10528 return op_string;
10529}
10530
252b5132 10531static int
70e41ade 10532i386_immediate (char *imm_start)
252b5132
RH
10533{
10534 char *save_input_line_pointer;
f3c180ae 10535 char *gotfree_input_line;
252b5132 10536 segT exp_seg = 0;
47926f60 10537 expressionS *exp;
40fb9820
L
10538 i386_operand_type types;
10539
0dfbf9d7 10540 operand_type_set (&types, ~0);
252b5132
RH
10541
10542 if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
10543 {
31b2323c
L
10544 as_bad (_("at most %d immediate operands are allowed"),
10545 MAX_IMMEDIATE_OPERANDS);
252b5132
RH
10546 return 0;
10547 }
10548
10549 exp = &im_expressions[i.imm_operands++];
520dc8e8 10550 i.op[this_operand].imms = exp;
252b5132
RH
10551
10552 if (is_space_char (*imm_start))
10553 ++imm_start;
10554
10555 save_input_line_pointer = input_line_pointer;
10556 input_line_pointer = imm_start;
10557
d258b828 10558 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
10559 if (gotfree_input_line)
10560 input_line_pointer = gotfree_input_line;
252b5132
RH
10561
10562 exp_seg = expression (exp);
10563
83183c0c 10564 SKIP_WHITESPACE ();
43234a1e
L
10565
10566 /* Handle vector operations. */
10567 if (*input_line_pointer == '{')
10568 {
10569 input_line_pointer = check_VecOperations (input_line_pointer,
10570 NULL);
10571 if (input_line_pointer == NULL)
10572 return 0;
10573 }
10574
252b5132 10575 if (*input_line_pointer)
f3c180ae 10576 as_bad (_("junk `%s' after expression"), input_line_pointer);
252b5132
RH
10577
10578 input_line_pointer = save_input_line_pointer;
f3c180ae 10579 if (gotfree_input_line)
ee86248c
JB
10580 {
10581 free (gotfree_input_line);
10582
10583 if (exp->X_op == O_constant || exp->X_op == O_register)
10584 exp->X_op = O_illegal;
10585 }
10586
10587 return i386_finalize_immediate (exp_seg, exp, types, imm_start);
10588}
252b5132 10589
ee86248c
JB
10590static int
10591i386_finalize_immediate (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
10592 i386_operand_type types, const char *imm_start)
10593{
10594 if (exp->X_op == O_absent || exp->X_op == O_illegal || exp->X_op == O_big)
252b5132 10595 {
313c53d1
L
10596 if (imm_start)
10597 as_bad (_("missing or invalid immediate expression `%s'"),
10598 imm_start);
3992d3b7 10599 return 0;
252b5132 10600 }
3e73aa7c 10601 else if (exp->X_op == O_constant)
252b5132 10602 {
47926f60 10603 /* Size it properly later. */
40fb9820 10604 i.types[this_operand].bitfield.imm64 = 1;
13f864ae
L
10605 /* If not 64bit, sign extend val. */
10606 if (flag_code != CODE_64BIT
4eed87de
AM
10607 && (exp->X_add_number & ~(((addressT) 2 << 31) - 1)) == 0)
10608 exp->X_add_number
10609 = (exp->X_add_number ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
252b5132 10610 }
4c63da97 10611#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
f86103b7 10612 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
31312f95 10613 && exp_seg != absolute_section
47926f60 10614 && exp_seg != text_section
24eab124
AM
10615 && exp_seg != data_section
10616 && exp_seg != bss_section
10617 && exp_seg != undefined_section
f86103b7 10618 && !bfd_is_com_section (exp_seg))
252b5132 10619 {
d0b47220 10620 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
252b5132
RH
10621 return 0;
10622 }
10623#endif
a841bdf5 10624 else if (!intel_syntax && exp_seg == reg_section)
bb8f5920 10625 {
313c53d1
L
10626 if (imm_start)
10627 as_bad (_("illegal immediate register operand %s"), imm_start);
bb8f5920
L
10628 return 0;
10629 }
252b5132
RH
10630 else
10631 {
10632 /* This is an address. The size of the address will be
24eab124 10633 determined later, depending on destination register,
3e73aa7c 10634 suffix, or the default for the section. */
40fb9820
L
10635 i.types[this_operand].bitfield.imm8 = 1;
10636 i.types[this_operand].bitfield.imm16 = 1;
10637 i.types[this_operand].bitfield.imm32 = 1;
10638 i.types[this_operand].bitfield.imm32s = 1;
10639 i.types[this_operand].bitfield.imm64 = 1;
c6fb90c8
L
10640 i.types[this_operand] = operand_type_and (i.types[this_operand],
10641 types);
252b5132
RH
10642 }
10643
10644 return 1;
10645}
10646
551c1ca1 10647static char *
e3bb37b5 10648i386_scale (char *scale)
252b5132 10649{
551c1ca1
AM
10650 offsetT val;
10651 char *save = input_line_pointer;
252b5132 10652
551c1ca1
AM
10653 input_line_pointer = scale;
10654 val = get_absolute_expression ();
10655
10656 switch (val)
252b5132 10657 {
551c1ca1 10658 case 1:
252b5132
RH
10659 i.log2_scale_factor = 0;
10660 break;
551c1ca1 10661 case 2:
252b5132
RH
10662 i.log2_scale_factor = 1;
10663 break;
551c1ca1 10664 case 4:
252b5132
RH
10665 i.log2_scale_factor = 2;
10666 break;
551c1ca1 10667 case 8:
252b5132
RH
10668 i.log2_scale_factor = 3;
10669 break;
10670 default:
a724f0f4
JB
10671 {
10672 char sep = *input_line_pointer;
10673
10674 *input_line_pointer = '\0';
10675 as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
10676 scale);
10677 *input_line_pointer = sep;
10678 input_line_pointer = save;
10679 return NULL;
10680 }
252b5132 10681 }
29b0f896 10682 if (i.log2_scale_factor != 0 && i.index_reg == 0)
252b5132
RH
10683 {
10684 as_warn (_("scale factor of %d without an index register"),
24eab124 10685 1 << i.log2_scale_factor);
252b5132 10686 i.log2_scale_factor = 0;
252b5132 10687 }
551c1ca1
AM
10688 scale = input_line_pointer;
10689 input_line_pointer = save;
10690 return scale;
252b5132
RH
10691}
10692
252b5132 10693static int
e3bb37b5 10694i386_displacement (char *disp_start, char *disp_end)
252b5132 10695{
29b0f896 10696 expressionS *exp;
252b5132
RH
10697 segT exp_seg = 0;
10698 char *save_input_line_pointer;
f3c180ae 10699 char *gotfree_input_line;
40fb9820
L
10700 int override;
10701 i386_operand_type bigdisp, types = anydisp;
3992d3b7 10702 int ret;
252b5132 10703
31b2323c
L
10704 if (i.disp_operands == MAX_MEMORY_OPERANDS)
10705 {
10706 as_bad (_("at most %d displacement operands are allowed"),
10707 MAX_MEMORY_OPERANDS);
10708 return 0;
10709 }
10710
0dfbf9d7 10711 operand_type_set (&bigdisp, 0);
6f2f06be 10712 if (i.jumpabsolute
48bcea9f 10713 || i.types[this_operand].bitfield.baseindex
0cfa3eb3
JB
10714 || (current_templates->start->opcode_modifier.jump != JUMP
10715 && current_templates->start->opcode_modifier.jump != JUMP_DWORD))
e05278af 10716 {
48bcea9f 10717 i386_addressing_mode ();
e05278af 10718 override = (i.prefix[ADDR_PREFIX] != 0);
40fb9820
L
10719 if (flag_code == CODE_64BIT)
10720 {
10721 if (!override)
10722 {
10723 bigdisp.bitfield.disp32s = 1;
10724 bigdisp.bitfield.disp64 = 1;
10725 }
48bcea9f
JB
10726 else
10727 bigdisp.bitfield.disp32 = 1;
40fb9820
L
10728 }
10729 else if ((flag_code == CODE_16BIT) ^ override)
40fb9820 10730 bigdisp.bitfield.disp16 = 1;
48bcea9f
JB
10731 else
10732 bigdisp.bitfield.disp32 = 1;
e05278af
JB
10733 }
10734 else
10735 {
376cd056
JB
10736 /* For PC-relative branches, the width of the displacement may be
10737 dependent upon data size, but is never dependent upon address size.
10738 Also make sure to not unintentionally match against a non-PC-relative
10739 branch template. */
10740 static templates aux_templates;
10741 const insn_template *t = current_templates->start;
10742 bfd_boolean has_intel64 = FALSE;
10743
10744 aux_templates.start = t;
10745 while (++t < current_templates->end)
10746 {
10747 if (t->opcode_modifier.jump
10748 != current_templates->start->opcode_modifier.jump)
10749 break;
4b5aaf5f 10750 if ((t->opcode_modifier.isa64 >= INTEL64))
376cd056
JB
10751 has_intel64 = TRUE;
10752 }
10753 if (t < current_templates->end)
10754 {
10755 aux_templates.end = t;
10756 current_templates = &aux_templates;
10757 }
10758
e05278af 10759 override = (i.prefix[DATA_PREFIX] != 0);
40fb9820
L
10760 if (flag_code == CODE_64BIT)
10761 {
376cd056
JB
10762 if ((override || i.suffix == WORD_MNEM_SUFFIX)
10763 && (!intel64 || !has_intel64))
40fb9820
L
10764 bigdisp.bitfield.disp16 = 1;
10765 else
48bcea9f 10766 bigdisp.bitfield.disp32s = 1;
40fb9820
L
10767 }
10768 else
e05278af
JB
10769 {
10770 if (!override)
10771 override = (i.suffix == (flag_code != CODE_16BIT
10772 ? WORD_MNEM_SUFFIX
10773 : LONG_MNEM_SUFFIX));
40fb9820
L
10774 bigdisp.bitfield.disp32 = 1;
10775 if ((flag_code == CODE_16BIT) ^ override)
10776 {
10777 bigdisp.bitfield.disp32 = 0;
10778 bigdisp.bitfield.disp16 = 1;
10779 }
e05278af 10780 }
e05278af 10781 }
c6fb90c8
L
10782 i.types[this_operand] = operand_type_or (i.types[this_operand],
10783 bigdisp);
252b5132
RH
10784
10785 exp = &disp_expressions[i.disp_operands];
520dc8e8 10786 i.op[this_operand].disps = exp;
252b5132
RH
10787 i.disp_operands++;
10788 save_input_line_pointer = input_line_pointer;
10789 input_line_pointer = disp_start;
10790 END_STRING_AND_SAVE (disp_end);
10791
10792#ifndef GCC_ASM_O_HACK
10793#define GCC_ASM_O_HACK 0
10794#endif
10795#if GCC_ASM_O_HACK
10796 END_STRING_AND_SAVE (disp_end + 1);
40fb9820 10797 if (i.types[this_operand].bitfield.baseIndex
24eab124 10798 && displacement_string_end[-1] == '+')
252b5132
RH
10799 {
10800 /* This hack is to avoid a warning when using the "o"
24eab124
AM
10801 constraint within gcc asm statements.
10802 For instance:
10803
10804 #define _set_tssldt_desc(n,addr,limit,type) \
10805 __asm__ __volatile__ ( \
10806 "movw %w2,%0\n\t" \
10807 "movw %w1,2+%0\n\t" \
10808 "rorl $16,%1\n\t" \
10809 "movb %b1,4+%0\n\t" \
10810 "movb %4,5+%0\n\t" \
10811 "movb $0,6+%0\n\t" \
10812 "movb %h1,7+%0\n\t" \
10813 "rorl $16,%1" \
10814 : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
10815
10816 This works great except that the output assembler ends
10817 up looking a bit weird if it turns out that there is
10818 no offset. You end up producing code that looks like:
10819
10820 #APP
10821 movw $235,(%eax)
10822 movw %dx,2+(%eax)
10823 rorl $16,%edx
10824 movb %dl,4+(%eax)
10825 movb $137,5+(%eax)
10826 movb $0,6+(%eax)
10827 movb %dh,7+(%eax)
10828 rorl $16,%edx
10829 #NO_APP
10830
47926f60 10831 So here we provide the missing zero. */
24eab124
AM
10832
10833 *displacement_string_end = '0';
252b5132
RH
10834 }
10835#endif
d258b828 10836 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
10837 if (gotfree_input_line)
10838 input_line_pointer = gotfree_input_line;
252b5132 10839
24eab124 10840 exp_seg = expression (exp);
252b5132 10841
636c26b0
AM
10842 SKIP_WHITESPACE ();
10843 if (*input_line_pointer)
10844 as_bad (_("junk `%s' after expression"), input_line_pointer);
10845#if GCC_ASM_O_HACK
10846 RESTORE_END_STRING (disp_end + 1);
10847#endif
636c26b0 10848 input_line_pointer = save_input_line_pointer;
636c26b0 10849 if (gotfree_input_line)
ee86248c
JB
10850 {
10851 free (gotfree_input_line);
10852
10853 if (exp->X_op == O_constant || exp->X_op == O_register)
10854 exp->X_op = O_illegal;
10855 }
10856
10857 ret = i386_finalize_displacement (exp_seg, exp, types, disp_start);
10858
10859 RESTORE_END_STRING (disp_end);
10860
10861 return ret;
10862}
10863
10864static int
10865i386_finalize_displacement (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
10866 i386_operand_type types, const char *disp_start)
10867{
10868 i386_operand_type bigdisp;
10869 int ret = 1;
636c26b0 10870
24eab124
AM
10871 /* We do this to make sure that the section symbol is in
10872 the symbol table. We will ultimately change the relocation
47926f60 10873 to be relative to the beginning of the section. */
1ae12ab7 10874 if (i.reloc[this_operand] == BFD_RELOC_386_GOTOFF
d6ab8113
JB
10875 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
10876 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
24eab124 10877 {
636c26b0 10878 if (exp->X_op != O_symbol)
3992d3b7 10879 goto inv_disp;
636c26b0 10880
e5cb08ac 10881 if (S_IS_LOCAL (exp->X_add_symbol)
c64efb4b
L
10882 && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section
10883 && S_GET_SEGMENT (exp->X_add_symbol) != expr_section)
24eab124 10884 section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
24eab124
AM
10885 exp->X_op = O_subtract;
10886 exp->X_op_symbol = GOT_symbol;
1ae12ab7 10887 if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
29b0f896 10888 i.reloc[this_operand] = BFD_RELOC_32_PCREL;
d6ab8113
JB
10889 else if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
10890 i.reloc[this_operand] = BFD_RELOC_64;
23df1078 10891 else
29b0f896 10892 i.reloc[this_operand] = BFD_RELOC_32;
24eab124 10893 }
252b5132 10894
3992d3b7
AM
10895 else if (exp->X_op == O_absent
10896 || exp->X_op == O_illegal
ee86248c 10897 || exp->X_op == O_big)
2daf4fd8 10898 {
3992d3b7
AM
10899 inv_disp:
10900 as_bad (_("missing or invalid displacement expression `%s'"),
2daf4fd8 10901 disp_start);
3992d3b7 10902 ret = 0;
2daf4fd8
AM
10903 }
10904
0e1147d9
L
10905 else if (flag_code == CODE_64BIT
10906 && !i.prefix[ADDR_PREFIX]
10907 && exp->X_op == O_constant)
10908 {
10909 /* Since displacement is signed extended to 64bit, don't allow
10910 disp32 and turn off disp32s if they are out of range. */
10911 i.types[this_operand].bitfield.disp32 = 0;
10912 if (!fits_in_signed_long (exp->X_add_number))
10913 {
10914 i.types[this_operand].bitfield.disp32s = 0;
10915 if (i.types[this_operand].bitfield.baseindex)
10916 {
10917 as_bad (_("0x%lx out range of signed 32bit displacement"),
10918 (long) exp->X_add_number);
10919 ret = 0;
10920 }
10921 }
10922 }
10923
4c63da97 10924#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
3992d3b7
AM
10925 else if (exp->X_op != O_constant
10926 && OUTPUT_FLAVOR == bfd_target_aout_flavour
10927 && exp_seg != absolute_section
10928 && exp_seg != text_section
10929 && exp_seg != data_section
10930 && exp_seg != bss_section
10931 && exp_seg != undefined_section
10932 && !bfd_is_com_section (exp_seg))
24eab124 10933 {
d0b47220 10934 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
3992d3b7 10935 ret = 0;
24eab124 10936 }
252b5132 10937#endif
3956db08 10938
48bcea9f
JB
10939 if (current_templates->start->opcode_modifier.jump == JUMP_BYTE
10940 /* Constants get taken care of by optimize_disp(). */
10941 && exp->X_op != O_constant)
10942 i.types[this_operand].bitfield.disp8 = 1;
10943
40fb9820
L
10944 /* Check if this is a displacement only operand. */
10945 bigdisp = i.types[this_operand];
10946 bigdisp.bitfield.disp8 = 0;
10947 bigdisp.bitfield.disp16 = 0;
10948 bigdisp.bitfield.disp32 = 0;
10949 bigdisp.bitfield.disp32s = 0;
10950 bigdisp.bitfield.disp64 = 0;
0dfbf9d7 10951 if (operand_type_all_zero (&bigdisp))
c6fb90c8
L
10952 i.types[this_operand] = operand_type_and (i.types[this_operand],
10953 types);
3956db08 10954
3992d3b7 10955 return ret;
252b5132
RH
10956}
10957
2abc2bec
JB
10958/* Return the active addressing mode, taking address override and
10959 registers forming the address into consideration. Update the
10960 address override prefix if necessary. */
47926f60 10961
2abc2bec
JB
10962static enum flag_code
10963i386_addressing_mode (void)
252b5132 10964{
be05d201
L
10965 enum flag_code addr_mode;
10966
10967 if (i.prefix[ADDR_PREFIX])
10968 addr_mode = flag_code == CODE_32BIT ? CODE_16BIT : CODE_32BIT;
a23b33b3
JB
10969 else if (flag_code == CODE_16BIT
10970 && current_templates->start->cpu_flags.bitfield.cpumpx
10971 /* Avoid replacing the "16-bit addressing not allowed" diagnostic
10972 from md_assemble() by "is not a valid base/index expression"
10973 when there is a base and/or index. */
10974 && !i.types[this_operand].bitfield.baseindex)
10975 {
10976 /* MPX insn memory operands with neither base nor index must be forced
10977 to use 32-bit addressing in 16-bit mode. */
10978 addr_mode = CODE_32BIT;
10979 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
10980 ++i.prefixes;
10981 gas_assert (!i.types[this_operand].bitfield.disp16);
10982 gas_assert (!i.types[this_operand].bitfield.disp32);
10983 }
be05d201
L
10984 else
10985 {
10986 addr_mode = flag_code;
10987
24eab124 10988#if INFER_ADDR_PREFIX
be05d201
L
10989 if (i.mem_operands == 0)
10990 {
10991 /* Infer address prefix from the first memory operand. */
10992 const reg_entry *addr_reg = i.base_reg;
10993
10994 if (addr_reg == NULL)
10995 addr_reg = i.index_reg;
eecb386c 10996
be05d201
L
10997 if (addr_reg)
10998 {
e968fc9b 10999 if (addr_reg->reg_type.bitfield.dword)
be05d201
L
11000 addr_mode = CODE_32BIT;
11001 else if (flag_code != CODE_64BIT
dc821c5f 11002 && addr_reg->reg_type.bitfield.word)
be05d201
L
11003 addr_mode = CODE_16BIT;
11004
11005 if (addr_mode != flag_code)
11006 {
11007 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
11008 i.prefixes += 1;
11009 /* Change the size of any displacement too. At most one
11010 of Disp16 or Disp32 is set.
11011 FIXME. There doesn't seem to be any real need for
11012 separate Disp16 and Disp32 flags. The same goes for
11013 Imm16 and Imm32. Removing them would probably clean
11014 up the code quite a lot. */
11015 if (flag_code != CODE_64BIT
11016 && (i.types[this_operand].bitfield.disp16
11017 || i.types[this_operand].bitfield.disp32))
11018 i.types[this_operand]
11019 = operand_type_xor (i.types[this_operand], disp16_32);
11020 }
11021 }
11022 }
24eab124 11023#endif
be05d201
L
11024 }
11025
2abc2bec
JB
11026 return addr_mode;
11027}
11028
11029/* Make sure the memory operand we've been dealt is valid.
11030 Return 1 on success, 0 on a failure. */
11031
11032static int
11033i386_index_check (const char *operand_string)
11034{
11035 const char *kind = "base/index";
11036 enum flag_code addr_mode = i386_addressing_mode ();
a152332d 11037 const insn_template *t = current_templates->start;
2abc2bec 11038
a152332d
JB
11039 if (t->opcode_modifier.isstring
11040 && !t->cpu_flags.bitfield.cpupadlock
fc0763e6
JB
11041 && (current_templates->end[-1].opcode_modifier.isstring
11042 || i.mem_operands))
11043 {
11044 /* Memory operands of string insns are special in that they only allow
11045 a single register (rDI, rSI, or rBX) as their memory address. */
be05d201
L
11046 const reg_entry *expected_reg;
11047 static const char *di_si[][2] =
11048 {
11049 { "esi", "edi" },
11050 { "si", "di" },
11051 { "rsi", "rdi" }
11052 };
11053 static const char *bx[] = { "ebx", "bx", "rbx" };
fc0763e6
JB
11054
11055 kind = "string address";
11056
a152332d 11057 if (t->opcode_modifier.prefixok == PrefixRep)
fc0763e6 11058 {
51c8edf6
JB
11059 int es_op = current_templates->end[-1].opcode_modifier.isstring
11060 - IS_STRING_ES_OP0;
11061 int op = 0;
fc0763e6 11062
51c8edf6 11063 if (!current_templates->end[-1].operand_types[0].bitfield.baseindex
fc0763e6
JB
11064 || ((!i.mem_operands != !intel_syntax)
11065 && current_templates->end[-1].operand_types[1]
11066 .bitfield.baseindex))
51c8edf6 11067 op = 1;
fe0e921f
AM
11068 expected_reg
11069 = (const reg_entry *) str_hash_find (reg_hash,
11070 di_si[addr_mode][op == es_op]);
fc0763e6
JB
11071 }
11072 else
fe0e921f
AM
11073 expected_reg
11074 = (const reg_entry *)str_hash_find (reg_hash, bx[addr_mode]);
fc0763e6 11075
be05d201
L
11076 if (i.base_reg != expected_reg
11077 || i.index_reg
fc0763e6 11078 || operand_type_check (i.types[this_operand], disp))
fc0763e6 11079 {
be05d201
L
11080 /* The second memory operand must have the same size as
11081 the first one. */
11082 if (i.mem_operands
11083 && i.base_reg
11084 && !((addr_mode == CODE_64BIT
dc821c5f 11085 && i.base_reg->reg_type.bitfield.qword)
be05d201 11086 || (addr_mode == CODE_32BIT
dc821c5f
JB
11087 ? i.base_reg->reg_type.bitfield.dword
11088 : i.base_reg->reg_type.bitfield.word)))
be05d201
L
11089 goto bad_address;
11090
fc0763e6
JB
11091 as_warn (_("`%s' is not valid here (expected `%c%s%s%c')"),
11092 operand_string,
11093 intel_syntax ? '[' : '(',
11094 register_prefix,
be05d201 11095 expected_reg->reg_name,
fc0763e6 11096 intel_syntax ? ']' : ')');
be05d201 11097 return 1;
fc0763e6 11098 }
be05d201
L
11099 else
11100 return 1;
11101
dc1e8a47 11102 bad_address:
be05d201
L
11103 as_bad (_("`%s' is not a valid %s expression"),
11104 operand_string, kind);
11105 return 0;
3e73aa7c
JH
11106 }
11107 else
11108 {
be05d201
L
11109 if (addr_mode != CODE_16BIT)
11110 {
11111 /* 32-bit/64-bit checks. */
41eb8e88
L
11112 if (i.disp_encoding == disp_encoding_16bit)
11113 {
11114 bad_disp:
11115 as_bad (_("invalid `%s' prefix"),
11116 addr_mode == CODE_16BIT ? "{disp32}" : "{disp16}");
11117 return 0;
11118 }
11119
be05d201 11120 if ((i.base_reg
e968fc9b
JB
11121 && ((addr_mode == CODE_64BIT
11122 ? !i.base_reg->reg_type.bitfield.qword
11123 : !i.base_reg->reg_type.bitfield.dword)
11124 || (i.index_reg && i.base_reg->reg_num == RegIP)
11125 || i.base_reg->reg_num == RegIZ))
be05d201 11126 || (i.index_reg
1b54b8d7
JB
11127 && !i.index_reg->reg_type.bitfield.xmmword
11128 && !i.index_reg->reg_type.bitfield.ymmword
11129 && !i.index_reg->reg_type.bitfield.zmmword
be05d201 11130 && ((addr_mode == CODE_64BIT
e968fc9b
JB
11131 ? !i.index_reg->reg_type.bitfield.qword
11132 : !i.index_reg->reg_type.bitfield.dword)
be05d201
L
11133 || !i.index_reg->reg_type.bitfield.baseindex)))
11134 goto bad_address;
8178be5b 11135
260cd341 11136 /* bndmk, bndldx, bndstx and mandatory non-vector SIB have special restrictions. */
a152332d 11137 if ((t->opcode_modifier.opcodeprefix == PREFIX_0XF3
389d00a5
JB
11138 && t->opcode_modifier.opcodespace == SPACE_0F
11139 && t->base_opcode == 0x1b)
a152332d 11140 || (t->opcode_modifier.opcodeprefix == PREFIX_NONE
389d00a5
JB
11141 && t->opcode_modifier.opcodespace == SPACE_0F
11142 && (t->base_opcode & ~1) == 0x1a)
a152332d 11143 || t->opcode_modifier.sib == SIBMEM)
8178be5b
JB
11144 {
11145 /* They cannot use RIP-relative addressing. */
e968fc9b 11146 if (i.base_reg && i.base_reg->reg_num == RegIP)
8178be5b
JB
11147 {
11148 as_bad (_("`%s' cannot be used here"), operand_string);
11149 return 0;
11150 }
11151
11152 /* bndldx and bndstx ignore their scale factor. */
a152332d 11153 if (t->opcode_modifier.opcodeprefix == PREFIX_NONE
389d00a5
JB
11154 && t->opcode_modifier.opcodespace == SPACE_0F
11155 && (t->base_opcode & ~1) == 0x1a
8178be5b
JB
11156 && i.log2_scale_factor)
11157 as_warn (_("register scaling is being ignored here"));
11158 }
be05d201
L
11159 }
11160 else
3e73aa7c 11161 {
be05d201 11162 /* 16-bit checks. */
41eb8e88
L
11163 if (i.disp_encoding == disp_encoding_32bit)
11164 goto bad_disp;
11165
3e73aa7c 11166 if ((i.base_reg
dc821c5f 11167 && (!i.base_reg->reg_type.bitfield.word
40fb9820 11168 || !i.base_reg->reg_type.bitfield.baseindex))
3e73aa7c 11169 || (i.index_reg
dc821c5f 11170 && (!i.index_reg->reg_type.bitfield.word
40fb9820 11171 || !i.index_reg->reg_type.bitfield.baseindex
29b0f896
AM
11172 || !(i.base_reg
11173 && i.base_reg->reg_num < 6
11174 && i.index_reg->reg_num >= 6
11175 && i.log2_scale_factor == 0))))
be05d201 11176 goto bad_address;
3e73aa7c
JH
11177 }
11178 }
be05d201 11179 return 1;
24eab124 11180}
252b5132 11181
43234a1e
L
11182/* Handle vector immediates. */
11183
11184static int
11185RC_SAE_immediate (const char *imm_start)
11186{
11187 unsigned int match_found, j;
11188 const char *pstr = imm_start;
11189 expressionS *exp;
11190
11191 if (*pstr != '{')
11192 return 0;
11193
11194 pstr++;
11195 match_found = 0;
11196 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); j++)
11197 {
11198 if (!strncmp (pstr, RC_NamesTable[j].name, RC_NamesTable[j].len))
11199 {
ca5312a2 11200 if (i.rounding.type != rc_none)
43234a1e
L
11201 {
11202 as_bad (_("duplicated `%s'"), imm_start);
11203 return 0;
11204 }
ca5312a2
JB
11205
11206 i.rounding.type = RC_NamesTable[j].type;
11207 i.rounding.operand = this_operand;
11208
43234a1e
L
11209 pstr += RC_NamesTable[j].len;
11210 match_found = 1;
11211 break;
11212 }
11213 }
11214 if (!match_found)
11215 return 0;
11216
11217 if (*pstr++ != '}')
11218 {
11219 as_bad (_("Missing '}': '%s'"), imm_start);
11220 return 0;
11221 }
11222 /* RC/SAE immediate string should contain nothing more. */;
11223 if (*pstr != 0)
11224 {
11225 as_bad (_("Junk after '}': '%s'"), imm_start);
11226 return 0;
11227 }
11228
11229 exp = &im_expressions[i.imm_operands++];
11230 i.op[this_operand].imms = exp;
11231
11232 exp->X_op = O_constant;
11233 exp->X_add_number = 0;
11234 exp->X_add_symbol = (symbolS *) 0;
11235 exp->X_op_symbol = (symbolS *) 0;
11236
11237 i.types[this_operand].bitfield.imm8 = 1;
11238 return 1;
11239}
11240
8325cc63
JB
11241/* Only string instructions can have a second memory operand, so
11242 reduce current_templates to just those if it contains any. */
11243static int
11244maybe_adjust_templates (void)
11245{
11246 const insn_template *t;
11247
11248 gas_assert (i.mem_operands == 1);
11249
11250 for (t = current_templates->start; t < current_templates->end; ++t)
11251 if (t->opcode_modifier.isstring)
11252 break;
11253
11254 if (t < current_templates->end)
11255 {
11256 static templates aux_templates;
11257 bfd_boolean recheck;
11258
11259 aux_templates.start = t;
11260 for (; t < current_templates->end; ++t)
11261 if (!t->opcode_modifier.isstring)
11262 break;
11263 aux_templates.end = t;
11264
11265 /* Determine whether to re-check the first memory operand. */
11266 recheck = (aux_templates.start != current_templates->start
11267 || t != current_templates->end);
11268
11269 current_templates = &aux_templates;
11270
11271 if (recheck)
11272 {
11273 i.mem_operands = 0;
11274 if (i.memop1_string != NULL
11275 && i386_index_check (i.memop1_string) == 0)
11276 return 0;
11277 i.mem_operands = 1;
11278 }
11279 }
11280
11281 return 1;
11282}
11283
fc0763e6 11284/* Parse OPERAND_STRING into the i386_insn structure I. Returns zero
47926f60 11285 on error. */
252b5132 11286
252b5132 11287static int
a7619375 11288i386_att_operand (char *operand_string)
252b5132 11289{
af6bdddf
AM
11290 const reg_entry *r;
11291 char *end_op;
24eab124 11292 char *op_string = operand_string;
252b5132 11293
24eab124 11294 if (is_space_char (*op_string))
252b5132
RH
11295 ++op_string;
11296
24eab124 11297 /* We check for an absolute prefix (differentiating,
47926f60 11298 for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
24eab124
AM
11299 if (*op_string == ABSOLUTE_PREFIX)
11300 {
11301 ++op_string;
11302 if (is_space_char (*op_string))
11303 ++op_string;
6f2f06be 11304 i.jumpabsolute = TRUE;
24eab124 11305 }
252b5132 11306
47926f60 11307 /* Check if operand is a register. */
4d1bb795 11308 if ((r = parse_register (op_string, &end_op)) != NULL)
24eab124 11309 {
40fb9820
L
11310 i386_operand_type temp;
11311
8a6fb3f9
JB
11312 if (r == &bad_reg)
11313 return 0;
11314
24eab124
AM
11315 /* Check for a segment override by searching for ':' after a
11316 segment register. */
11317 op_string = end_op;
11318 if (is_space_char (*op_string))
11319 ++op_string;
00cee14f 11320 if (*op_string == ':' && r->reg_type.bitfield.class == SReg)
24eab124
AM
11321 {
11322 switch (r->reg_num)
11323 {
11324 case 0:
11325 i.seg[i.mem_operands] = &es;
11326 break;
11327 case 1:
11328 i.seg[i.mem_operands] = &cs;
11329 break;
11330 case 2:
11331 i.seg[i.mem_operands] = &ss;
11332 break;
11333 case 3:
11334 i.seg[i.mem_operands] = &ds;
11335 break;
11336 case 4:
11337 i.seg[i.mem_operands] = &fs;
11338 break;
11339 case 5:
11340 i.seg[i.mem_operands] = &gs;
11341 break;
11342 }
252b5132 11343
24eab124 11344 /* Skip the ':' and whitespace. */
252b5132
RH
11345 ++op_string;
11346 if (is_space_char (*op_string))
24eab124 11347 ++op_string;
252b5132 11348
24eab124
AM
11349 if (!is_digit_char (*op_string)
11350 && !is_identifier_char (*op_string)
11351 && *op_string != '('
11352 && *op_string != ABSOLUTE_PREFIX)
11353 {
11354 as_bad (_("bad memory operand `%s'"), op_string);
11355 return 0;
11356 }
47926f60 11357 /* Handle case of %es:*foo. */
24eab124
AM
11358 if (*op_string == ABSOLUTE_PREFIX)
11359 {
11360 ++op_string;
11361 if (is_space_char (*op_string))
11362 ++op_string;
6f2f06be 11363 i.jumpabsolute = TRUE;
24eab124
AM
11364 }
11365 goto do_memory_reference;
11366 }
43234a1e
L
11367
11368 /* Handle vector operations. */
11369 if (*op_string == '{')
11370 {
11371 op_string = check_VecOperations (op_string, NULL);
11372 if (op_string == NULL)
11373 return 0;
11374 }
11375
24eab124
AM
11376 if (*op_string)
11377 {
d0b47220 11378 as_bad (_("junk `%s' after register"), op_string);
24eab124
AM
11379 return 0;
11380 }
40fb9820
L
11381 temp = r->reg_type;
11382 temp.bitfield.baseindex = 0;
c6fb90c8
L
11383 i.types[this_operand] = operand_type_or (i.types[this_operand],
11384 temp);
7d5e4556 11385 i.types[this_operand].bitfield.unspecified = 0;
520dc8e8 11386 i.op[this_operand].regs = r;
24eab124
AM
11387 i.reg_operands++;
11388 }
af6bdddf
AM
11389 else if (*op_string == REGISTER_PREFIX)
11390 {
11391 as_bad (_("bad register name `%s'"), op_string);
11392 return 0;
11393 }
24eab124 11394 else if (*op_string == IMMEDIATE_PREFIX)
ce8a8b2f 11395 {
24eab124 11396 ++op_string;
6f2f06be 11397 if (i.jumpabsolute)
24eab124 11398 {
d0b47220 11399 as_bad (_("immediate operand illegal with absolute jump"));
24eab124
AM
11400 return 0;
11401 }
11402 if (!i386_immediate (op_string))
11403 return 0;
11404 }
43234a1e
L
11405 else if (RC_SAE_immediate (operand_string))
11406 {
11407 /* If it is a RC or SAE immediate, do nothing. */
11408 ;
11409 }
24eab124
AM
11410 else if (is_digit_char (*op_string)
11411 || is_identifier_char (*op_string)
d02603dc 11412 || *op_string == '"'
e5cb08ac 11413 || *op_string == '(')
24eab124 11414 {
47926f60 11415 /* This is a memory reference of some sort. */
af6bdddf 11416 char *base_string;
252b5132 11417
47926f60 11418 /* Start and end of displacement string expression (if found). */
eecb386c
AM
11419 char *displacement_string_start;
11420 char *displacement_string_end;
43234a1e 11421 char *vop_start;
252b5132 11422
24eab124 11423 do_memory_reference:
8325cc63
JB
11424 if (i.mem_operands == 1 && !maybe_adjust_templates ())
11425 return 0;
24eab124 11426 if ((i.mem_operands == 1
40fb9820 11427 && !current_templates->start->opcode_modifier.isstring)
24eab124
AM
11428 || i.mem_operands == 2)
11429 {
11430 as_bad (_("too many memory references for `%s'"),
11431 current_templates->start->name);
11432 return 0;
11433 }
252b5132 11434
24eab124
AM
11435 /* Check for base index form. We detect the base index form by
11436 looking for an ')' at the end of the operand, searching
11437 for the '(' matching it, and finding a REGISTER_PREFIX or ','
11438 after the '('. */
af6bdddf 11439 base_string = op_string + strlen (op_string);
c3332e24 11440
43234a1e
L
11441 /* Handle vector operations. */
11442 vop_start = strchr (op_string, '{');
11443 if (vop_start && vop_start < base_string)
11444 {
11445 if (check_VecOperations (vop_start, base_string) == NULL)
11446 return 0;
11447 base_string = vop_start;
11448 }
11449
af6bdddf
AM
11450 --base_string;
11451 if (is_space_char (*base_string))
11452 --base_string;
252b5132 11453
47926f60 11454 /* If we only have a displacement, set-up for it to be parsed later. */
af6bdddf
AM
11455 displacement_string_start = op_string;
11456 displacement_string_end = base_string + 1;
252b5132 11457
24eab124
AM
11458 if (*base_string == ')')
11459 {
af6bdddf 11460 char *temp_string;
24eab124
AM
11461 unsigned int parens_balanced = 1;
11462 /* We've already checked that the number of left & right ()'s are
47926f60 11463 equal, so this loop will not be infinite. */
24eab124
AM
11464 do
11465 {
11466 base_string--;
11467 if (*base_string == ')')
11468 parens_balanced++;
11469 if (*base_string == '(')
11470 parens_balanced--;
11471 }
11472 while (parens_balanced);
c3332e24 11473
af6bdddf 11474 temp_string = base_string;
c3332e24 11475
24eab124 11476 /* Skip past '(' and whitespace. */
252b5132
RH
11477 ++base_string;
11478 if (is_space_char (*base_string))
24eab124 11479 ++base_string;
252b5132 11480
af6bdddf 11481 if (*base_string == ','
4eed87de
AM
11482 || ((i.base_reg = parse_register (base_string, &end_op))
11483 != NULL))
252b5132 11484 {
af6bdddf 11485 displacement_string_end = temp_string;
252b5132 11486
40fb9820 11487 i.types[this_operand].bitfield.baseindex = 1;
252b5132 11488
af6bdddf 11489 if (i.base_reg)
24eab124 11490 {
8a6fb3f9
JB
11491 if (i.base_reg == &bad_reg)
11492 return 0;
24eab124
AM
11493 base_string = end_op;
11494 if (is_space_char (*base_string))
11495 ++base_string;
af6bdddf
AM
11496 }
11497
11498 /* There may be an index reg or scale factor here. */
11499 if (*base_string == ',')
11500 {
11501 ++base_string;
11502 if (is_space_char (*base_string))
11503 ++base_string;
11504
4eed87de
AM
11505 if ((i.index_reg = parse_register (base_string, &end_op))
11506 != NULL)
24eab124 11507 {
8a6fb3f9
JB
11508 if (i.index_reg == &bad_reg)
11509 return 0;
af6bdddf 11510 base_string = end_op;
24eab124
AM
11511 if (is_space_char (*base_string))
11512 ++base_string;
af6bdddf
AM
11513 if (*base_string == ',')
11514 {
11515 ++base_string;
11516 if (is_space_char (*base_string))
11517 ++base_string;
11518 }
e5cb08ac 11519 else if (*base_string != ')')
af6bdddf 11520 {
4eed87de
AM
11521 as_bad (_("expecting `,' or `)' "
11522 "after index register in `%s'"),
af6bdddf
AM
11523 operand_string);
11524 return 0;
11525 }
24eab124 11526 }
af6bdddf 11527 else if (*base_string == REGISTER_PREFIX)
24eab124 11528 {
f76bf5e0
L
11529 end_op = strchr (base_string, ',');
11530 if (end_op)
11531 *end_op = '\0';
af6bdddf 11532 as_bad (_("bad register name `%s'"), base_string);
24eab124
AM
11533 return 0;
11534 }
252b5132 11535
47926f60 11536 /* Check for scale factor. */
551c1ca1 11537 if (*base_string != ')')
af6bdddf 11538 {
551c1ca1
AM
11539 char *end_scale = i386_scale (base_string);
11540
11541 if (!end_scale)
af6bdddf 11542 return 0;
24eab124 11543
551c1ca1 11544 base_string = end_scale;
af6bdddf
AM
11545 if (is_space_char (*base_string))
11546 ++base_string;
11547 if (*base_string != ')')
11548 {
4eed87de
AM
11549 as_bad (_("expecting `)' "
11550 "after scale factor in `%s'"),
af6bdddf
AM
11551 operand_string);
11552 return 0;
11553 }
11554 }
11555 else if (!i.index_reg)
24eab124 11556 {
4eed87de
AM
11557 as_bad (_("expecting index register or scale factor "
11558 "after `,'; got '%c'"),
af6bdddf 11559 *base_string);
24eab124
AM
11560 return 0;
11561 }
11562 }
af6bdddf 11563 else if (*base_string != ')')
24eab124 11564 {
4eed87de
AM
11565 as_bad (_("expecting `,' or `)' "
11566 "after base register in `%s'"),
af6bdddf 11567 operand_string);
24eab124
AM
11568 return 0;
11569 }
c3332e24 11570 }
af6bdddf 11571 else if (*base_string == REGISTER_PREFIX)
c3332e24 11572 {
f76bf5e0
L
11573 end_op = strchr (base_string, ',');
11574 if (end_op)
11575 *end_op = '\0';
af6bdddf 11576 as_bad (_("bad register name `%s'"), base_string);
24eab124 11577 return 0;
c3332e24 11578 }
24eab124
AM
11579 }
11580
11581 /* If there's an expression beginning the operand, parse it,
11582 assuming displacement_string_start and
11583 displacement_string_end are meaningful. */
11584 if (displacement_string_start != displacement_string_end)
11585 {
11586 if (!i386_displacement (displacement_string_start,
11587 displacement_string_end))
11588 return 0;
11589 }
11590
11591 /* Special case for (%dx) while doing input/output op. */
11592 if (i.base_reg
75e5731b
JB
11593 && i.base_reg->reg_type.bitfield.instance == RegD
11594 && i.base_reg->reg_type.bitfield.word
24eab124
AM
11595 && i.index_reg == 0
11596 && i.log2_scale_factor == 0
11597 && i.seg[i.mem_operands] == 0
40fb9820 11598 && !operand_type_check (i.types[this_operand], disp))
24eab124 11599 {
2fb5be8d 11600 i.types[this_operand] = i.base_reg->reg_type;
24eab124
AM
11601 return 1;
11602 }
11603
eecb386c
AM
11604 if (i386_index_check (operand_string) == 0)
11605 return 0;
c48dadc9 11606 i.flags[this_operand] |= Operand_Mem;
8325cc63
JB
11607 if (i.mem_operands == 0)
11608 i.memop1_string = xstrdup (operand_string);
24eab124
AM
11609 i.mem_operands++;
11610 }
11611 else
ce8a8b2f
AM
11612 {
11613 /* It's not a memory operand; argh! */
24eab124
AM
11614 as_bad (_("invalid char %s beginning operand %d `%s'"),
11615 output_invalid (*op_string),
11616 this_operand + 1,
11617 op_string);
11618 return 0;
11619 }
47926f60 11620 return 1; /* Normal return. */
252b5132
RH
11621}
11622\f
fa94de6b
RM
11623/* Calculate the maximum variable size (i.e., excluding fr_fix)
11624 that an rs_machine_dependent frag may reach. */
11625
11626unsigned int
11627i386_frag_max_var (fragS *frag)
11628{
11629 /* The only relaxable frags are for jumps.
11630 Unconditional jumps can grow by 4 bytes and others by 5 bytes. */
11631 gas_assert (frag->fr_type == rs_machine_dependent);
11632 return TYPE_FROM_RELAX_STATE (frag->fr_subtype) == UNCOND_JUMP ? 4 : 5;
11633}
11634
b084df0b
L
11635#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
11636static int
8dcea932 11637elf_symbol_resolved_in_segment_p (symbolS *fr_symbol, offsetT fr_var)
b084df0b
L
11638{
11639 /* STT_GNU_IFUNC symbol must go through PLT. */
11640 if ((symbol_get_bfdsym (fr_symbol)->flags
11641 & BSF_GNU_INDIRECT_FUNCTION) != 0)
11642 return 0;
11643
11644 if (!S_IS_EXTERNAL (fr_symbol))
11645 /* Symbol may be weak or local. */
11646 return !S_IS_WEAK (fr_symbol);
11647
8dcea932
L
11648 /* Global symbols with non-default visibility can't be preempted. */
11649 if (ELF_ST_VISIBILITY (S_GET_OTHER (fr_symbol)) != STV_DEFAULT)
11650 return 1;
11651
11652 if (fr_var != NO_RELOC)
11653 switch ((enum bfd_reloc_code_real) fr_var)
11654 {
11655 case BFD_RELOC_386_PLT32:
11656 case BFD_RELOC_X86_64_PLT32:
33eaf5de 11657 /* Symbol with PLT relocation may be preempted. */
8dcea932
L
11658 return 0;
11659 default:
11660 abort ();
11661 }
11662
b084df0b
L
11663 /* Global symbols with default visibility in a shared library may be
11664 preempted by another definition. */
8dcea932 11665 return !shared;
b084df0b
L
11666}
11667#endif
11668
79d72f45
HL
11669/* Table 3-2. Macro-Fusible Instructions in Haswell Microarchitecture
11670 Note also work for Skylake and Cascadelake.
11671---------------------------------------------------------------------
11672| JCC | ADD/SUB/CMP | INC/DEC | TEST/AND |
11673| ------ | ----------- | ------- | -------- |
11674| Jo | N | N | Y |
11675| Jno | N | N | Y |
11676| Jc/Jb | Y | N | Y |
11677| Jae/Jnb | Y | N | Y |
11678| Je/Jz | Y | Y | Y |
11679| Jne/Jnz | Y | Y | Y |
11680| Jna/Jbe | Y | N | Y |
11681| Ja/Jnbe | Y | N | Y |
11682| Js | N | N | Y |
11683| Jns | N | N | Y |
11684| Jp/Jpe | N | N | Y |
11685| Jnp/Jpo | N | N | Y |
11686| Jl/Jnge | Y | Y | Y |
11687| Jge/Jnl | Y | Y | Y |
11688| Jle/Jng | Y | Y | Y |
11689| Jg/Jnle | Y | Y | Y |
11690--------------------------------------------------------------------- */
11691static int
11692i386_macro_fusible_p (enum mf_cmp_kind mf_cmp, enum mf_jcc_kind mf_jcc)
11693{
11694 if (mf_cmp == mf_cmp_alu_cmp)
11695 return ((mf_jcc >= mf_jcc_jc && mf_jcc <= mf_jcc_jna)
11696 || mf_jcc == mf_jcc_jl || mf_jcc == mf_jcc_jle);
11697 if (mf_cmp == mf_cmp_incdec)
11698 return (mf_jcc == mf_jcc_je || mf_jcc == mf_jcc_jl
11699 || mf_jcc == mf_jcc_jle);
11700 if (mf_cmp == mf_cmp_test_and)
11701 return 1;
11702 return 0;
11703}
11704
e379e5f3
L
11705/* Return the next non-empty frag. */
11706
11707static fragS *
11708i386_next_non_empty_frag (fragS *fragP)
11709{
11710 /* There may be a frag with a ".fill 0" when there is no room in
11711 the current frag for frag_grow in output_insn. */
11712 for (fragP = fragP->fr_next;
11713 (fragP != NULL
11714 && fragP->fr_type == rs_fill
11715 && fragP->fr_fix == 0);
11716 fragP = fragP->fr_next)
11717 ;
11718 return fragP;
11719}
11720
11721/* Return the next jcc frag after BRANCH_PADDING. */
11722
11723static fragS *
79d72f45 11724i386_next_fusible_jcc_frag (fragS *maybe_cmp_fragP, fragS *pad_fragP)
e379e5f3 11725{
79d72f45
HL
11726 fragS *branch_fragP;
11727 if (!pad_fragP)
e379e5f3
L
11728 return NULL;
11729
79d72f45
HL
11730 if (pad_fragP->fr_type == rs_machine_dependent
11731 && (TYPE_FROM_RELAX_STATE (pad_fragP->fr_subtype)
e379e5f3
L
11732 == BRANCH_PADDING))
11733 {
79d72f45
HL
11734 branch_fragP = i386_next_non_empty_frag (pad_fragP);
11735 if (branch_fragP->fr_type != rs_machine_dependent)
e379e5f3 11736 return NULL;
79d72f45
HL
11737 if (TYPE_FROM_RELAX_STATE (branch_fragP->fr_subtype) == COND_JUMP
11738 && i386_macro_fusible_p (maybe_cmp_fragP->tc_frag_data.mf_type,
11739 pad_fragP->tc_frag_data.mf_type))
11740 return branch_fragP;
e379e5f3
L
11741 }
11742
11743 return NULL;
11744}
11745
11746/* Classify BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags. */
11747
11748static void
11749i386_classify_machine_dependent_frag (fragS *fragP)
11750{
11751 fragS *cmp_fragP;
11752 fragS *pad_fragP;
11753 fragS *branch_fragP;
11754 fragS *next_fragP;
11755 unsigned int max_prefix_length;
11756
11757 if (fragP->tc_frag_data.classified)
11758 return;
11759
11760 /* First scan for BRANCH_PADDING and FUSED_JCC_PADDING. Convert
11761 FUSED_JCC_PADDING and merge BRANCH_PADDING. */
11762 for (next_fragP = fragP;
11763 next_fragP != NULL;
11764 next_fragP = next_fragP->fr_next)
11765 {
11766 next_fragP->tc_frag_data.classified = 1;
11767 if (next_fragP->fr_type == rs_machine_dependent)
11768 switch (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype))
11769 {
11770 case BRANCH_PADDING:
11771 /* The BRANCH_PADDING frag must be followed by a branch
11772 frag. */
11773 branch_fragP = i386_next_non_empty_frag (next_fragP);
11774 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
11775 break;
11776 case FUSED_JCC_PADDING:
11777 /* Check if this is a fused jcc:
11778 FUSED_JCC_PADDING
11779 CMP like instruction
11780 BRANCH_PADDING
11781 COND_JUMP
11782 */
11783 cmp_fragP = i386_next_non_empty_frag (next_fragP);
11784 pad_fragP = i386_next_non_empty_frag (cmp_fragP);
79d72f45 11785 branch_fragP = i386_next_fusible_jcc_frag (next_fragP, pad_fragP);
e379e5f3
L
11786 if (branch_fragP)
11787 {
11788 /* The BRANCH_PADDING frag is merged with the
11789 FUSED_JCC_PADDING frag. */
11790 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
11791 /* CMP like instruction size. */
11792 next_fragP->tc_frag_data.cmp_size = cmp_fragP->fr_fix;
11793 frag_wane (pad_fragP);
11794 /* Skip to branch_fragP. */
11795 next_fragP = branch_fragP;
11796 }
11797 else if (next_fragP->tc_frag_data.max_prefix_length)
11798 {
11799 /* Turn FUSED_JCC_PADDING into BRANCH_PREFIX if it isn't
11800 a fused jcc. */
11801 next_fragP->fr_subtype
11802 = ENCODE_RELAX_STATE (BRANCH_PREFIX, 0);
11803 next_fragP->tc_frag_data.max_bytes
11804 = next_fragP->tc_frag_data.max_prefix_length;
11805 /* This will be updated in the BRANCH_PREFIX scan. */
11806 next_fragP->tc_frag_data.max_prefix_length = 0;
11807 }
11808 else
11809 frag_wane (next_fragP);
11810 break;
11811 }
11812 }
11813
11814 /* Stop if there is no BRANCH_PREFIX. */
11815 if (!align_branch_prefix_size)
11816 return;
11817
11818 /* Scan for BRANCH_PREFIX. */
11819 for (; fragP != NULL; fragP = fragP->fr_next)
11820 {
11821 if (fragP->fr_type != rs_machine_dependent
11822 || (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
11823 != BRANCH_PREFIX))
11824 continue;
11825
11826 /* Count all BRANCH_PREFIX frags before BRANCH_PADDING and
11827 COND_JUMP_PREFIX. */
11828 max_prefix_length = 0;
11829 for (next_fragP = fragP;
11830 next_fragP != NULL;
11831 next_fragP = next_fragP->fr_next)
11832 {
11833 if (next_fragP->fr_type == rs_fill)
11834 /* Skip rs_fill frags. */
11835 continue;
11836 else if (next_fragP->fr_type != rs_machine_dependent)
11837 /* Stop for all other frags. */
11838 break;
11839
11840 /* rs_machine_dependent frags. */
11841 if (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
11842 == BRANCH_PREFIX)
11843 {
11844 /* Count BRANCH_PREFIX frags. */
11845 if (max_prefix_length >= MAX_FUSED_JCC_PADDING_SIZE)
11846 {
11847 max_prefix_length = MAX_FUSED_JCC_PADDING_SIZE;
11848 frag_wane (next_fragP);
11849 }
11850 else
11851 max_prefix_length
11852 += next_fragP->tc_frag_data.max_bytes;
11853 }
11854 else if ((TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
11855 == BRANCH_PADDING)
11856 || (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
11857 == FUSED_JCC_PADDING))
11858 {
11859 /* Stop at BRANCH_PADDING and FUSED_JCC_PADDING. */
11860 fragP->tc_frag_data.u.padding_fragP = next_fragP;
11861 break;
11862 }
11863 else
11864 /* Stop for other rs_machine_dependent frags. */
11865 break;
11866 }
11867
11868 fragP->tc_frag_data.max_prefix_length = max_prefix_length;
11869
11870 /* Skip to the next frag. */
11871 fragP = next_fragP;
11872 }
11873}
11874
11875/* Compute padding size for
11876
11877 FUSED_JCC_PADDING
11878 CMP like instruction
11879 BRANCH_PADDING
11880 COND_JUMP/UNCOND_JUMP
11881
11882 or
11883
11884 BRANCH_PADDING
11885 COND_JUMP/UNCOND_JUMP
11886 */
11887
11888static int
11889i386_branch_padding_size (fragS *fragP, offsetT address)
11890{
11891 unsigned int offset, size, padding_size;
11892 fragS *branch_fragP = fragP->tc_frag_data.u.branch_fragP;
11893
11894 /* The start address of the BRANCH_PADDING or FUSED_JCC_PADDING frag. */
11895 if (!address)
11896 address = fragP->fr_address;
11897 address += fragP->fr_fix;
11898
11899 /* CMP like instrunction size. */
11900 size = fragP->tc_frag_data.cmp_size;
11901
11902 /* The base size of the branch frag. */
11903 size += branch_fragP->fr_fix;
11904
11905 /* Add opcode and displacement bytes for the rs_machine_dependent
11906 branch frag. */
11907 if (branch_fragP->fr_type == rs_machine_dependent)
11908 size += md_relax_table[branch_fragP->fr_subtype].rlx_length;
11909
11910 /* Check if branch is within boundary and doesn't end at the last
11911 byte. */
11912 offset = address & ((1U << align_branch_power) - 1);
11913 if ((offset + size) >= (1U << align_branch_power))
11914 /* Padding needed to avoid crossing boundary. */
11915 padding_size = (1U << align_branch_power) - offset;
11916 else
11917 /* No padding needed. */
11918 padding_size = 0;
11919
11920 /* The return value may be saved in tc_frag_data.length which is
11921 unsigned byte. */
11922 if (!fits_in_unsigned_byte (padding_size))
11923 abort ();
11924
11925 return padding_size;
11926}
11927
11928/* i386_generic_table_relax_frag()
11929
11930 Handle BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags to
11931 grow/shrink padding to align branch frags. Hand others to
11932 relax_frag(). */
11933
11934long
11935i386_generic_table_relax_frag (segT segment, fragS *fragP, long stretch)
11936{
11937 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
11938 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
11939 {
11940 long padding_size = i386_branch_padding_size (fragP, 0);
11941 long grow = padding_size - fragP->tc_frag_data.length;
11942
11943 /* When the BRANCH_PREFIX frag is used, the computed address
11944 must match the actual address and there should be no padding. */
11945 if (fragP->tc_frag_data.padding_address
11946 && (fragP->tc_frag_data.padding_address != fragP->fr_address
11947 || padding_size))
11948 abort ();
11949
11950 /* Update the padding size. */
11951 if (grow)
11952 fragP->tc_frag_data.length = padding_size;
11953
11954 return grow;
11955 }
11956 else if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
11957 {
11958 fragS *padding_fragP, *next_fragP;
11959 long padding_size, left_size, last_size;
11960
11961 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
11962 if (!padding_fragP)
11963 /* Use the padding set by the leading BRANCH_PREFIX frag. */
11964 return (fragP->tc_frag_data.length
11965 - fragP->tc_frag_data.last_length);
11966
11967 /* Compute the relative address of the padding frag in the very
11968 first time where the BRANCH_PREFIX frag sizes are zero. */
11969 if (!fragP->tc_frag_data.padding_address)
11970 fragP->tc_frag_data.padding_address
11971 = padding_fragP->fr_address - (fragP->fr_address - stretch);
11972
11973 /* First update the last length from the previous interation. */
11974 left_size = fragP->tc_frag_data.prefix_length;
11975 for (next_fragP = fragP;
11976 next_fragP != padding_fragP;
11977 next_fragP = next_fragP->fr_next)
11978 if (next_fragP->fr_type == rs_machine_dependent
11979 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
11980 == BRANCH_PREFIX))
11981 {
11982 if (left_size)
11983 {
11984 int max = next_fragP->tc_frag_data.max_bytes;
11985 if (max)
11986 {
11987 int size;
11988 if (max > left_size)
11989 size = left_size;
11990 else
11991 size = max;
11992 left_size -= size;
11993 next_fragP->tc_frag_data.last_length = size;
11994 }
11995 }
11996 else
11997 next_fragP->tc_frag_data.last_length = 0;
11998 }
11999
12000 /* Check the padding size for the padding frag. */
12001 padding_size = i386_branch_padding_size
12002 (padding_fragP, (fragP->fr_address
12003 + fragP->tc_frag_data.padding_address));
12004
12005 last_size = fragP->tc_frag_data.prefix_length;
12006 /* Check if there is change from the last interation. */
12007 if (padding_size == last_size)
12008 {
12009 /* Update the expected address of the padding frag. */
12010 padding_fragP->tc_frag_data.padding_address
12011 = (fragP->fr_address + padding_size
12012 + fragP->tc_frag_data.padding_address);
12013 return 0;
12014 }
12015
12016 if (padding_size > fragP->tc_frag_data.max_prefix_length)
12017 {
12018 /* No padding if there is no sufficient room. Clear the
12019 expected address of the padding frag. */
12020 padding_fragP->tc_frag_data.padding_address = 0;
12021 padding_size = 0;
12022 }
12023 else
12024 /* Store the expected address of the padding frag. */
12025 padding_fragP->tc_frag_data.padding_address
12026 = (fragP->fr_address + padding_size
12027 + fragP->tc_frag_data.padding_address);
12028
12029 fragP->tc_frag_data.prefix_length = padding_size;
12030
12031 /* Update the length for the current interation. */
12032 left_size = padding_size;
12033 for (next_fragP = fragP;
12034 next_fragP != padding_fragP;
12035 next_fragP = next_fragP->fr_next)
12036 if (next_fragP->fr_type == rs_machine_dependent
12037 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12038 == BRANCH_PREFIX))
12039 {
12040 if (left_size)
12041 {
12042 int max = next_fragP->tc_frag_data.max_bytes;
12043 if (max)
12044 {
12045 int size;
12046 if (max > left_size)
12047 size = left_size;
12048 else
12049 size = max;
12050 left_size -= size;
12051 next_fragP->tc_frag_data.length = size;
12052 }
12053 }
12054 else
12055 next_fragP->tc_frag_data.length = 0;
12056 }
12057
12058 return (fragP->tc_frag_data.length
12059 - fragP->tc_frag_data.last_length);
12060 }
12061 return relax_frag (segment, fragP, stretch);
12062}
12063
ee7fcc42
AM
12064/* md_estimate_size_before_relax()
12065
12066 Called just before relax() for rs_machine_dependent frags. The x86
12067 assembler uses these frags to handle variable size jump
12068 instructions.
12069
12070 Any symbol that is now undefined will not become defined.
12071 Return the correct fr_subtype in the frag.
12072 Return the initial "guess for variable size of frag" to caller.
12073 The guess is actually the growth beyond the fixed part. Whatever
12074 we do to grow the fixed or variable part contributes to our
12075 returned value. */
12076
252b5132 12077int
7016a5d5 12078md_estimate_size_before_relax (fragS *fragP, segT segment)
252b5132 12079{
e379e5f3
L
12080 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12081 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX
12082 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
12083 {
12084 i386_classify_machine_dependent_frag (fragP);
12085 return fragP->tc_frag_data.length;
12086 }
12087
252b5132 12088 /* We've already got fragP->fr_subtype right; all we have to do is
b98ef147
AM
12089 check for un-relaxable symbols. On an ELF system, we can't relax
12090 an externally visible symbol, because it may be overridden by a
12091 shared library. */
12092 if (S_GET_SEGMENT (fragP->fr_symbol) != segment
6d249963 12093#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12094 || (IS_ELF
8dcea932
L
12095 && !elf_symbol_resolved_in_segment_p (fragP->fr_symbol,
12096 fragP->fr_var))
fbeb56a4
DK
12097#endif
12098#if defined (OBJ_COFF) && defined (TE_PE)
7ab9ffdd 12099 || (OUTPUT_FLAVOR == bfd_target_coff_flavour
fbeb56a4 12100 && S_IS_WEAK (fragP->fr_symbol))
b98ef147
AM
12101#endif
12102 )
252b5132 12103 {
b98ef147
AM
12104 /* Symbol is undefined in this segment, or we need to keep a
12105 reloc so that weak symbols can be overridden. */
12106 int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
f86103b7 12107 enum bfd_reloc_code_real reloc_type;
ee7fcc42
AM
12108 unsigned char *opcode;
12109 int old_fr_fix;
f6af82bd 12110
ee7fcc42 12111 if (fragP->fr_var != NO_RELOC)
1e9cc1c2 12112 reloc_type = (enum bfd_reloc_code_real) fragP->fr_var;
b98ef147 12113 else if (size == 2)
f6af82bd 12114 reloc_type = BFD_RELOC_16_PCREL;
bd7ab16b
L
12115#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
12116 else if (need_plt32_p (fragP->fr_symbol))
12117 reloc_type = BFD_RELOC_X86_64_PLT32;
12118#endif
f6af82bd
AM
12119 else
12120 reloc_type = BFD_RELOC_32_PCREL;
252b5132 12121
ee7fcc42
AM
12122 old_fr_fix = fragP->fr_fix;
12123 opcode = (unsigned char *) fragP->fr_opcode;
12124
fddf5b5b 12125 switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
252b5132 12126 {
fddf5b5b
AM
12127 case UNCOND_JUMP:
12128 /* Make jmp (0xeb) a (d)word displacement jump. */
47926f60 12129 opcode[0] = 0xe9;
252b5132 12130 fragP->fr_fix += size;
062cd5e7
AS
12131 fix_new (fragP, old_fr_fix, size,
12132 fragP->fr_symbol,
12133 fragP->fr_offset, 1,
12134 reloc_type);
252b5132
RH
12135 break;
12136
fddf5b5b 12137 case COND_JUMP86:
412167cb
AM
12138 if (size == 2
12139 && (!no_cond_jump_promotion || fragP->fr_var != NO_RELOC))
fddf5b5b
AM
12140 {
12141 /* Negate the condition, and branch past an
12142 unconditional jump. */
12143 opcode[0] ^= 1;
12144 opcode[1] = 3;
12145 /* Insert an unconditional jump. */
12146 opcode[2] = 0xe9;
12147 /* We added two extra opcode bytes, and have a two byte
12148 offset. */
12149 fragP->fr_fix += 2 + 2;
062cd5e7
AS
12150 fix_new (fragP, old_fr_fix + 2, 2,
12151 fragP->fr_symbol,
12152 fragP->fr_offset, 1,
12153 reloc_type);
fddf5b5b
AM
12154 break;
12155 }
12156 /* Fall through. */
12157
12158 case COND_JUMP:
412167cb
AM
12159 if (no_cond_jump_promotion && fragP->fr_var == NO_RELOC)
12160 {
3e02c1cc
AM
12161 fixS *fixP;
12162
412167cb 12163 fragP->fr_fix += 1;
3e02c1cc
AM
12164 fixP = fix_new (fragP, old_fr_fix, 1,
12165 fragP->fr_symbol,
12166 fragP->fr_offset, 1,
12167 BFD_RELOC_8_PCREL);
12168 fixP->fx_signed = 1;
412167cb
AM
12169 break;
12170 }
93c2a809 12171
24eab124 12172 /* This changes the byte-displacement jump 0x7N
fddf5b5b 12173 to the (d)word-displacement jump 0x0f,0x8N. */
252b5132 12174 opcode[1] = opcode[0] + 0x10;
f6af82bd 12175 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
47926f60
KH
12176 /* We've added an opcode byte. */
12177 fragP->fr_fix += 1 + size;
062cd5e7
AS
12178 fix_new (fragP, old_fr_fix + 1, size,
12179 fragP->fr_symbol,
12180 fragP->fr_offset, 1,
12181 reloc_type);
252b5132 12182 break;
fddf5b5b
AM
12183
12184 default:
12185 BAD_CASE (fragP->fr_subtype);
12186 break;
252b5132
RH
12187 }
12188 frag_wane (fragP);
ee7fcc42 12189 return fragP->fr_fix - old_fr_fix;
252b5132 12190 }
93c2a809 12191
93c2a809
AM
12192 /* Guess size depending on current relax state. Initially the relax
12193 state will correspond to a short jump and we return 1, because
12194 the variable part of the frag (the branch offset) is one byte
12195 long. However, we can relax a section more than once and in that
12196 case we must either set fr_subtype back to the unrelaxed state,
12197 or return the value for the appropriate branch. */
12198 return md_relax_table[fragP->fr_subtype].rlx_length;
ee7fcc42
AM
12199}
12200
47926f60
KH
12201/* Called after relax() is finished.
12202
12203 In: Address of frag.
12204 fr_type == rs_machine_dependent.
12205 fr_subtype is what the address relaxed to.
12206
12207 Out: Any fixSs and constants are set up.
12208 Caller will turn frag into a ".space 0". */
12209
252b5132 12210void
7016a5d5
TG
12211md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec ATTRIBUTE_UNUSED,
12212 fragS *fragP)
252b5132 12213{
29b0f896 12214 unsigned char *opcode;
252b5132 12215 unsigned char *where_to_put_displacement = NULL;
847f7ad4
AM
12216 offsetT target_address;
12217 offsetT opcode_address;
252b5132 12218 unsigned int extension = 0;
847f7ad4 12219 offsetT displacement_from_opcode_start;
252b5132 12220
e379e5f3
L
12221 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12222 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING
12223 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12224 {
12225 /* Generate nop padding. */
12226 unsigned int size = fragP->tc_frag_data.length;
12227 if (size)
12228 {
12229 if (size > fragP->tc_frag_data.max_bytes)
12230 abort ();
12231
12232 if (flag_debug)
12233 {
12234 const char *msg;
12235 const char *branch = "branch";
12236 const char *prefix = "";
12237 fragS *padding_fragP;
12238 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
12239 == BRANCH_PREFIX)
12240 {
12241 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
12242 switch (fragP->tc_frag_data.default_prefix)
12243 {
12244 default:
12245 abort ();
12246 break;
12247 case CS_PREFIX_OPCODE:
12248 prefix = " cs";
12249 break;
12250 case DS_PREFIX_OPCODE:
12251 prefix = " ds";
12252 break;
12253 case ES_PREFIX_OPCODE:
12254 prefix = " es";
12255 break;
12256 case FS_PREFIX_OPCODE:
12257 prefix = " fs";
12258 break;
12259 case GS_PREFIX_OPCODE:
12260 prefix = " gs";
12261 break;
12262 case SS_PREFIX_OPCODE:
12263 prefix = " ss";
12264 break;
12265 }
12266 if (padding_fragP)
12267 msg = _("%s:%u: add %d%s at 0x%llx to align "
12268 "%s within %d-byte boundary\n");
12269 else
12270 msg = _("%s:%u: add additional %d%s at 0x%llx to "
12271 "align %s within %d-byte boundary\n");
12272 }
12273 else
12274 {
12275 padding_fragP = fragP;
12276 msg = _("%s:%u: add %d%s-byte nop at 0x%llx to align "
12277 "%s within %d-byte boundary\n");
12278 }
12279
12280 if (padding_fragP)
12281 switch (padding_fragP->tc_frag_data.branch_type)
12282 {
12283 case align_branch_jcc:
12284 branch = "jcc";
12285 break;
12286 case align_branch_fused:
12287 branch = "fused jcc";
12288 break;
12289 case align_branch_jmp:
12290 branch = "jmp";
12291 break;
12292 case align_branch_call:
12293 branch = "call";
12294 break;
12295 case align_branch_indirect:
12296 branch = "indiret branch";
12297 break;
12298 case align_branch_ret:
12299 branch = "ret";
12300 break;
12301 default:
12302 break;
12303 }
12304
12305 fprintf (stdout, msg,
12306 fragP->fr_file, fragP->fr_line, size, prefix,
12307 (long long) fragP->fr_address, branch,
12308 1 << align_branch_power);
12309 }
12310 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12311 memset (fragP->fr_opcode,
12312 fragP->tc_frag_data.default_prefix, size);
12313 else
12314 i386_generate_nops (fragP, (char *) fragP->fr_opcode,
12315 size, 0);
12316 fragP->fr_fix += size;
12317 }
12318 return;
12319 }
12320
252b5132
RH
12321 opcode = (unsigned char *) fragP->fr_opcode;
12322
47926f60 12323 /* Address we want to reach in file space. */
252b5132 12324 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
252b5132 12325
47926f60 12326 /* Address opcode resides at in file space. */
252b5132
RH
12327 opcode_address = fragP->fr_address + fragP->fr_fix;
12328
47926f60 12329 /* Displacement from opcode start to fill into instruction. */
252b5132
RH
12330 displacement_from_opcode_start = target_address - opcode_address;
12331
fddf5b5b 12332 if ((fragP->fr_subtype & BIG) == 0)
252b5132 12333 {
47926f60
KH
12334 /* Don't have to change opcode. */
12335 extension = 1; /* 1 opcode + 1 displacement */
252b5132 12336 where_to_put_displacement = &opcode[1];
fddf5b5b
AM
12337 }
12338 else
12339 {
12340 if (no_cond_jump_promotion
12341 && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
4eed87de
AM
12342 as_warn_where (fragP->fr_file, fragP->fr_line,
12343 _("long jump required"));
252b5132 12344
fddf5b5b
AM
12345 switch (fragP->fr_subtype)
12346 {
12347 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
12348 extension = 4; /* 1 opcode + 4 displacement */
12349 opcode[0] = 0xe9;
12350 where_to_put_displacement = &opcode[1];
12351 break;
252b5132 12352
fddf5b5b
AM
12353 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
12354 extension = 2; /* 1 opcode + 2 displacement */
12355 opcode[0] = 0xe9;
12356 where_to_put_displacement = &opcode[1];
12357 break;
252b5132 12358
fddf5b5b
AM
12359 case ENCODE_RELAX_STATE (COND_JUMP, BIG):
12360 case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
12361 extension = 5; /* 2 opcode + 4 displacement */
12362 opcode[1] = opcode[0] + 0x10;
12363 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12364 where_to_put_displacement = &opcode[2];
12365 break;
252b5132 12366
fddf5b5b
AM
12367 case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
12368 extension = 3; /* 2 opcode + 2 displacement */
12369 opcode[1] = opcode[0] + 0x10;
12370 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12371 where_to_put_displacement = &opcode[2];
12372 break;
252b5132 12373
fddf5b5b
AM
12374 case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
12375 extension = 4;
12376 opcode[0] ^= 1;
12377 opcode[1] = 3;
12378 opcode[2] = 0xe9;
12379 where_to_put_displacement = &opcode[3];
12380 break;
12381
12382 default:
12383 BAD_CASE (fragP->fr_subtype);
12384 break;
12385 }
252b5132 12386 }
fddf5b5b 12387
7b81dfbb
AJ
12388 /* If size if less then four we are sure that the operand fits,
12389 but if it's 4, then it could be that the displacement is larger
12390 then -/+ 2GB. */
12391 if (DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype) == 4
12392 && object_64bit
12393 && ((addressT) (displacement_from_opcode_start - extension
4eed87de
AM
12394 + ((addressT) 1 << 31))
12395 > (((addressT) 2 << 31) - 1)))
7b81dfbb
AJ
12396 {
12397 as_bad_where (fragP->fr_file, fragP->fr_line,
12398 _("jump target out of range"));
12399 /* Make us emit 0. */
12400 displacement_from_opcode_start = extension;
12401 }
47926f60 12402 /* Now put displacement after opcode. */
252b5132
RH
12403 md_number_to_chars ((char *) where_to_put_displacement,
12404 (valueT) (displacement_from_opcode_start - extension),
fddf5b5b 12405 DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
252b5132
RH
12406 fragP->fr_fix += extension;
12407}
12408\f
7016a5d5 12409/* Apply a fixup (fixP) to segment data, once it has been determined
252b5132
RH
12410 by our caller that we have all the info we need to fix it up.
12411
7016a5d5
TG
12412 Parameter valP is the pointer to the value of the bits.
12413
252b5132
RH
12414 On the 386, immediates, displacements, and data pointers are all in
12415 the same (little-endian) format, so we don't need to care about which
12416 we are handling. */
12417
94f592af 12418void
7016a5d5 12419md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 12420{
94f592af 12421 char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
c6682705 12422 valueT value = *valP;
252b5132 12423
f86103b7 12424#if !defined (TE_Mach)
93382f6d
AM
12425 if (fixP->fx_pcrel)
12426 {
12427 switch (fixP->fx_r_type)
12428 {
5865bb77
ILT
12429 default:
12430 break;
12431
d6ab8113
JB
12432 case BFD_RELOC_64:
12433 fixP->fx_r_type = BFD_RELOC_64_PCREL;
12434 break;
93382f6d 12435 case BFD_RELOC_32:
ae8887b5 12436 case BFD_RELOC_X86_64_32S:
93382f6d
AM
12437 fixP->fx_r_type = BFD_RELOC_32_PCREL;
12438 break;
12439 case BFD_RELOC_16:
12440 fixP->fx_r_type = BFD_RELOC_16_PCREL;
12441 break;
12442 case BFD_RELOC_8:
12443 fixP->fx_r_type = BFD_RELOC_8_PCREL;
12444 break;
12445 }
12446 }
252b5132 12447
a161fe53 12448 if (fixP->fx_addsy != NULL
31312f95 12449 && (fixP->fx_r_type == BFD_RELOC_32_PCREL
d6ab8113 12450 || fixP->fx_r_type == BFD_RELOC_64_PCREL
31312f95 12451 || fixP->fx_r_type == BFD_RELOC_16_PCREL
d258b828 12452 || fixP->fx_r_type == BFD_RELOC_8_PCREL)
31312f95 12453 && !use_rela_relocations)
252b5132 12454 {
31312f95
AM
12455 /* This is a hack. There should be a better way to handle this.
12456 This covers for the fact that bfd_install_relocation will
12457 subtract the current location (for partial_inplace, PC relative
12458 relocations); see more below. */
252b5132 12459#ifndef OBJ_AOUT
718ddfc0 12460 if (IS_ELF
252b5132
RH
12461#ifdef TE_PE
12462 || OUTPUT_FLAVOR == bfd_target_coff_flavour
12463#endif
12464 )
12465 value += fixP->fx_where + fixP->fx_frag->fr_address;
12466#endif
12467#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12468 if (IS_ELF)
252b5132 12469 {
6539b54b 12470 segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
2f66722d 12471
6539b54b 12472 if ((sym_seg == seg
2f66722d 12473 || (symbol_section_p (fixP->fx_addsy)
6539b54b 12474 && sym_seg != absolute_section))
af65af87 12475 && !generic_force_reloc (fixP))
2f66722d
AM
12476 {
12477 /* Yes, we add the values in twice. This is because
6539b54b
AM
12478 bfd_install_relocation subtracts them out again. I think
12479 bfd_install_relocation is broken, but I don't dare change
2f66722d
AM
12480 it. FIXME. */
12481 value += fixP->fx_where + fixP->fx_frag->fr_address;
12482 }
252b5132
RH
12483 }
12484#endif
12485#if defined (OBJ_COFF) && defined (TE_PE)
977cdf5a
NC
12486 /* For some reason, the PE format does not store a
12487 section address offset for a PC relative symbol. */
12488 if (S_GET_SEGMENT (fixP->fx_addsy) != seg
7be1c489 12489 || S_IS_WEAK (fixP->fx_addsy))
252b5132
RH
12490 value += md_pcrel_from (fixP);
12491#endif
12492 }
fbeb56a4 12493#if defined (OBJ_COFF) && defined (TE_PE)
f01c1a09
NC
12494 if (fixP->fx_addsy != NULL
12495 && S_IS_WEAK (fixP->fx_addsy)
12496 /* PR 16858: Do not modify weak function references. */
12497 && ! fixP->fx_pcrel)
fbeb56a4 12498 {
296a8689
NC
12499#if !defined (TE_PEP)
12500 /* For x86 PE weak function symbols are neither PC-relative
12501 nor do they set S_IS_FUNCTION. So the only reliable way
12502 to detect them is to check the flags of their containing
12503 section. */
12504 if (S_GET_SEGMENT (fixP->fx_addsy) != NULL
12505 && S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_CODE)
12506 ;
12507 else
12508#endif
fbeb56a4
DK
12509 value -= S_GET_VALUE (fixP->fx_addsy);
12510 }
12511#endif
252b5132
RH
12512
12513 /* Fix a few things - the dynamic linker expects certain values here,
0234cb7c 12514 and we must not disappoint it. */
252b5132 12515#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12516 if (IS_ELF && fixP->fx_addsy)
47926f60
KH
12517 switch (fixP->fx_r_type)
12518 {
12519 case BFD_RELOC_386_PLT32:
3e73aa7c 12520 case BFD_RELOC_X86_64_PLT32:
b9519cfe
L
12521 /* Make the jump instruction point to the address of the operand.
12522 At runtime we merely add the offset to the actual PLT entry.
12523 NB: Subtract the offset size only for jump instructions. */
12524 if (fixP->fx_pcrel)
12525 value = -4;
47926f60 12526 break;
31312f95 12527
13ae64f3
JJ
12528 case BFD_RELOC_386_TLS_GD:
12529 case BFD_RELOC_386_TLS_LDM:
13ae64f3 12530 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
12531 case BFD_RELOC_386_TLS_IE:
12532 case BFD_RELOC_386_TLS_GOTIE:
67a4f2b7 12533 case BFD_RELOC_386_TLS_GOTDESC:
bffbf940
JJ
12534 case BFD_RELOC_X86_64_TLSGD:
12535 case BFD_RELOC_X86_64_TLSLD:
12536 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7 12537 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
00f7efb6
JJ
12538 value = 0; /* Fully resolved at runtime. No addend. */
12539 /* Fallthrough */
12540 case BFD_RELOC_386_TLS_LE:
12541 case BFD_RELOC_386_TLS_LDO_32:
12542 case BFD_RELOC_386_TLS_LE_32:
12543 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 12544 case BFD_RELOC_X86_64_DTPOFF64:
00f7efb6 12545 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113 12546 case BFD_RELOC_X86_64_TPOFF64:
00f7efb6
JJ
12547 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12548 break;
12549
67a4f2b7
AO
12550 case BFD_RELOC_386_TLS_DESC_CALL:
12551 case BFD_RELOC_X86_64_TLSDESC_CALL:
12552 value = 0; /* Fully resolved at runtime. No addend. */
12553 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12554 fixP->fx_done = 0;
12555 return;
12556
47926f60
KH
12557 case BFD_RELOC_VTABLE_INHERIT:
12558 case BFD_RELOC_VTABLE_ENTRY:
12559 fixP->fx_done = 0;
94f592af 12560 return;
47926f60
KH
12561
12562 default:
12563 break;
12564 }
12565#endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) */
c6682705 12566 *valP = value;
f86103b7 12567#endif /* !defined (TE_Mach) */
3e73aa7c 12568
3e73aa7c 12569 /* Are we finished with this relocation now? */
c6682705 12570 if (fixP->fx_addsy == NULL)
3e73aa7c 12571 fixP->fx_done = 1;
fbeb56a4
DK
12572#if defined (OBJ_COFF) && defined (TE_PE)
12573 else if (fixP->fx_addsy != NULL && S_IS_WEAK (fixP->fx_addsy))
12574 {
12575 fixP->fx_done = 0;
12576 /* Remember value for tc_gen_reloc. */
12577 fixP->fx_addnumber = value;
12578 /* Clear out the frag for now. */
12579 value = 0;
12580 }
12581#endif
3e73aa7c
JH
12582 else if (use_rela_relocations)
12583 {
12584 fixP->fx_no_overflow = 1;
062cd5e7
AS
12585 /* Remember value for tc_gen_reloc. */
12586 fixP->fx_addnumber = value;
3e73aa7c
JH
12587 value = 0;
12588 }
f86103b7 12589
94f592af 12590 md_number_to_chars (p, value, fixP->fx_size);
252b5132 12591}
252b5132 12592\f
6d4af3c2 12593const char *
499ac353 12594md_atof (int type, char *litP, int *sizeP)
252b5132 12595{
499ac353
NC
12596 /* This outputs the LITTLENUMs in REVERSE order;
12597 in accord with the bigendian 386. */
12598 return ieee_md_atof (type, litP, sizeP, FALSE);
252b5132
RH
12599}
12600\f
2d545b82 12601static char output_invalid_buf[sizeof (unsigned char) * 2 + 6];
252b5132 12602
252b5132 12603static char *
e3bb37b5 12604output_invalid (int c)
252b5132 12605{
3882b010 12606 if (ISPRINT (c))
f9f21a03
L
12607 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
12608 "'%c'", c);
252b5132 12609 else
f9f21a03 12610 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
2d545b82 12611 "(0x%x)", (unsigned char) c);
252b5132
RH
12612 return output_invalid_buf;
12613}
12614
8a6fb3f9
JB
12615/* Verify that @r can be used in the current context. */
12616
12617static bfd_boolean check_register (const reg_entry *r)
12618{
12619 if (allow_pseudo_reg)
12620 return TRUE;
12621
12622 if (operand_type_all_zero (&r->reg_type))
12623 return FALSE;
12624
12625 if ((r->reg_type.bitfield.dword
12626 || (r->reg_type.bitfield.class == SReg && r->reg_num > 3)
12627 || r->reg_type.bitfield.class == RegCR
22e00a3f 12628 || r->reg_type.bitfield.class == RegDR)
8a6fb3f9
JB
12629 && !cpu_arch_flags.bitfield.cpui386)
12630 return FALSE;
12631
22e00a3f
JB
12632 if (r->reg_type.bitfield.class == RegTR
12633 && (flag_code == CODE_64BIT
12634 || !cpu_arch_flags.bitfield.cpui386
12635 || cpu_arch_isa_flags.bitfield.cpui586
12636 || cpu_arch_isa_flags.bitfield.cpui686))
12637 return FALSE;
12638
8a6fb3f9
JB
12639 if (r->reg_type.bitfield.class == RegMMX && !cpu_arch_flags.bitfield.cpummx)
12640 return FALSE;
12641
12642 if (!cpu_arch_flags.bitfield.cpuavx512f)
12643 {
12644 if (r->reg_type.bitfield.zmmword
12645 || r->reg_type.bitfield.class == RegMask)
12646 return FALSE;
12647
12648 if (!cpu_arch_flags.bitfield.cpuavx)
12649 {
12650 if (r->reg_type.bitfield.ymmword)
12651 return FALSE;
12652
12653 if (!cpu_arch_flags.bitfield.cpusse && r->reg_type.bitfield.xmmword)
12654 return FALSE;
12655 }
12656 }
12657
260cd341
LC
12658 if (r->reg_type.bitfield.tmmword
12659 && (!cpu_arch_flags.bitfield.cpuamx_tile
12660 || flag_code != CODE_64BIT))
12661 return FALSE;
12662
8a6fb3f9
JB
12663 if (r->reg_type.bitfield.class == RegBND && !cpu_arch_flags.bitfield.cpumpx)
12664 return FALSE;
12665
12666 /* Don't allow fake index register unless allow_index_reg isn't 0. */
12667 if (!allow_index_reg && r->reg_num == RegIZ)
12668 return FALSE;
12669
12670 /* Upper 16 vector registers are only available with VREX in 64bit
12671 mode, and require EVEX encoding. */
12672 if (r->reg_flags & RegVRex)
12673 {
12674 if (!cpu_arch_flags.bitfield.cpuavx512f
12675 || flag_code != CODE_64BIT)
12676 return FALSE;
12677
da4977e0
JB
12678 if (i.vec_encoding == vex_encoding_default)
12679 i.vec_encoding = vex_encoding_evex;
12680 else if (i.vec_encoding != vex_encoding_evex)
12681 i.vec_encoding = vex_encoding_error;
8a6fb3f9
JB
12682 }
12683
12684 if (((r->reg_flags & (RegRex64 | RegRex)) || r->reg_type.bitfield.qword)
12685 && (!cpu_arch_flags.bitfield.cpulm || r->reg_type.bitfield.class != RegCR)
12686 && flag_code != CODE_64BIT)
12687 return FALSE;
12688
12689 if (r->reg_type.bitfield.class == SReg && r->reg_num == RegFlat
12690 && !intel_syntax)
12691 return FALSE;
12692
12693 return TRUE;
12694}
12695
af6bdddf 12696/* REG_STRING starts *before* REGISTER_PREFIX. */
252b5132
RH
12697
12698static const reg_entry *
4d1bb795 12699parse_real_register (char *reg_string, char **end_op)
252b5132 12700{
af6bdddf
AM
12701 char *s = reg_string;
12702 char *p;
252b5132
RH
12703 char reg_name_given[MAX_REG_NAME_SIZE + 1];
12704 const reg_entry *r;
12705
12706 /* Skip possible REGISTER_PREFIX and possible whitespace. */
12707 if (*s == REGISTER_PREFIX)
12708 ++s;
12709
12710 if (is_space_char (*s))
12711 ++s;
12712
12713 p = reg_name_given;
af6bdddf 12714 while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
252b5132
RH
12715 {
12716 if (p >= reg_name_given + MAX_REG_NAME_SIZE)
af6bdddf
AM
12717 return (const reg_entry *) NULL;
12718 s++;
252b5132
RH
12719 }
12720
6588847e
DN
12721 /* For naked regs, make sure that we are not dealing with an identifier.
12722 This prevents confusing an identifier like `eax_var' with register
12723 `eax'. */
12724 if (allow_naked_reg && identifier_chars[(unsigned char) *s])
12725 return (const reg_entry *) NULL;
12726
af6bdddf 12727 *end_op = s;
252b5132 12728
629310ab 12729 r = (const reg_entry *) str_hash_find (reg_hash, reg_name_given);
252b5132 12730
5f47d35b 12731 /* Handle floating point regs, allowing spaces in the (i) part. */
6288d05f 12732 if (r == reg_st0)
5f47d35b 12733 {
0e0eea78
JB
12734 if (!cpu_arch_flags.bitfield.cpu8087
12735 && !cpu_arch_flags.bitfield.cpu287
af32b722
JB
12736 && !cpu_arch_flags.bitfield.cpu387
12737 && !allow_pseudo_reg)
0e0eea78
JB
12738 return (const reg_entry *) NULL;
12739
5f47d35b
AM
12740 if (is_space_char (*s))
12741 ++s;
12742 if (*s == '(')
12743 {
af6bdddf 12744 ++s;
5f47d35b
AM
12745 if (is_space_char (*s))
12746 ++s;
12747 if (*s >= '0' && *s <= '7')
12748 {
db557034 12749 int fpr = *s - '0';
af6bdddf 12750 ++s;
5f47d35b
AM
12751 if (is_space_char (*s))
12752 ++s;
12753 if (*s == ')')
12754 {
12755 *end_op = s + 1;
6288d05f 12756 know (r[fpr].reg_num == fpr);
db557034 12757 return r + fpr;
5f47d35b 12758 }
5f47d35b 12759 }
47926f60 12760 /* We have "%st(" then garbage. */
5f47d35b
AM
12761 return (const reg_entry *) NULL;
12762 }
12763 }
12764
8a6fb3f9 12765 return r && check_register (r) ? r : NULL;
252b5132 12766}
4d1bb795
JB
12767
12768/* REG_STRING starts *before* REGISTER_PREFIX. */
12769
12770static const reg_entry *
12771parse_register (char *reg_string, char **end_op)
12772{
12773 const reg_entry *r;
12774
12775 if (*reg_string == REGISTER_PREFIX || allow_naked_reg)
12776 r = parse_real_register (reg_string, end_op);
12777 else
12778 r = NULL;
12779 if (!r)
12780 {
12781 char *save = input_line_pointer;
12782 char c;
12783 symbolS *symbolP;
12784
12785 input_line_pointer = reg_string;
d02603dc 12786 c = get_symbol_name (&reg_string);
4d1bb795
JB
12787 symbolP = symbol_find (reg_string);
12788 if (symbolP && S_GET_SEGMENT (symbolP) == reg_section)
12789 {
12790 const expressionS *e = symbol_get_value_expression (symbolP);
12791
0398aac5 12792 know (e->X_op == O_register);
4eed87de 12793 know (e->X_add_number >= 0
c3fe08fa 12794 && (valueT) e->X_add_number < i386_regtab_size);
4d1bb795 12795 r = i386_regtab + e->X_add_number;
8a6fb3f9
JB
12796 if (!check_register (r))
12797 {
12798 as_bad (_("register '%s%s' cannot be used here"),
12799 register_prefix, r->reg_name);
12800 r = &bad_reg;
12801 }
4d1bb795
JB
12802 *end_op = input_line_pointer;
12803 }
12804 *input_line_pointer = c;
12805 input_line_pointer = save;
12806 }
12807 return r;
12808}
12809
12810int
12811i386_parse_name (char *name, expressionS *e, char *nextcharP)
12812{
12813 const reg_entry *r;
12814 char *end = input_line_pointer;
12815
12816 *end = *nextcharP;
12817 r = parse_register (name, &input_line_pointer);
12818 if (r && end <= input_line_pointer)
12819 {
12820 *nextcharP = *input_line_pointer;
12821 *input_line_pointer = 0;
8a6fb3f9
JB
12822 if (r != &bad_reg)
12823 {
12824 e->X_op = O_register;
12825 e->X_add_number = r - i386_regtab;
12826 }
12827 else
12828 e->X_op = O_illegal;
4d1bb795
JB
12829 return 1;
12830 }
12831 input_line_pointer = end;
12832 *end = 0;
ee86248c 12833 return intel_syntax ? i386_intel_parse_name (name, e) : 0;
4d1bb795
JB
12834}
12835
12836void
12837md_operand (expressionS *e)
12838{
ee86248c
JB
12839 char *end;
12840 const reg_entry *r;
4d1bb795 12841
ee86248c
JB
12842 switch (*input_line_pointer)
12843 {
12844 case REGISTER_PREFIX:
12845 r = parse_real_register (input_line_pointer, &end);
4d1bb795
JB
12846 if (r)
12847 {
12848 e->X_op = O_register;
12849 e->X_add_number = r - i386_regtab;
12850 input_line_pointer = end;
12851 }
ee86248c
JB
12852 break;
12853
12854 case '[':
9c2799c2 12855 gas_assert (intel_syntax);
ee86248c
JB
12856 end = input_line_pointer++;
12857 expression (e);
12858 if (*input_line_pointer == ']')
12859 {
12860 ++input_line_pointer;
12861 e->X_op_symbol = make_expr_symbol (e);
12862 e->X_add_symbol = NULL;
12863 e->X_add_number = 0;
12864 e->X_op = O_index;
12865 }
12866 else
12867 {
12868 e->X_op = O_absent;
12869 input_line_pointer = end;
12870 }
12871 break;
4d1bb795
JB
12872 }
12873}
12874
252b5132 12875\f
4cc782b5 12876#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
b6f8c7c4 12877const char *md_shortopts = "kVQ:sqnO::";
252b5132 12878#else
b6f8c7c4 12879const char *md_shortopts = "qnO::";
252b5132 12880#endif
6e0b89ee 12881
3e73aa7c 12882#define OPTION_32 (OPTION_MD_BASE + 0)
b3b91714
AM
12883#define OPTION_64 (OPTION_MD_BASE + 1)
12884#define OPTION_DIVIDE (OPTION_MD_BASE + 2)
9103f4f4
L
12885#define OPTION_MARCH (OPTION_MD_BASE + 3)
12886#define OPTION_MTUNE (OPTION_MD_BASE + 4)
1efbbeb4
L
12887#define OPTION_MMNEMONIC (OPTION_MD_BASE + 5)
12888#define OPTION_MSYNTAX (OPTION_MD_BASE + 6)
12889#define OPTION_MINDEX_REG (OPTION_MD_BASE + 7)
12890#define OPTION_MNAKED_REG (OPTION_MD_BASE + 8)
bd5dea88 12891#define OPTION_MRELAX_RELOCATIONS (OPTION_MD_BASE + 9)
c0f3af97 12892#define OPTION_MSSE2AVX (OPTION_MD_BASE + 10)
daf50ae7 12893#define OPTION_MSSE_CHECK (OPTION_MD_BASE + 11)
7bab8ab5
JB
12894#define OPTION_MOPERAND_CHECK (OPTION_MD_BASE + 12)
12895#define OPTION_MAVXSCALAR (OPTION_MD_BASE + 13)
12896#define OPTION_X32 (OPTION_MD_BASE + 14)
7e8b059b 12897#define OPTION_MADD_BND_PREFIX (OPTION_MD_BASE + 15)
43234a1e
L
12898#define OPTION_MEVEXLIG (OPTION_MD_BASE + 16)
12899#define OPTION_MEVEXWIG (OPTION_MD_BASE + 17)
167ad85b 12900#define OPTION_MBIG_OBJ (OPTION_MD_BASE + 18)
d1982f93 12901#define OPTION_MOMIT_LOCK_PREFIX (OPTION_MD_BASE + 19)
d3d3c6db 12902#define OPTION_MEVEXRCIG (OPTION_MD_BASE + 20)
8dcea932 12903#define OPTION_MSHARED (OPTION_MD_BASE + 21)
5db04b09
L
12904#define OPTION_MAMD64 (OPTION_MD_BASE + 22)
12905#define OPTION_MINTEL64 (OPTION_MD_BASE + 23)
e4e00185 12906#define OPTION_MFENCE_AS_LOCK_ADD (OPTION_MD_BASE + 24)
b4a3a7b4 12907#define OPTION_X86_USED_NOTE (OPTION_MD_BASE + 25)
03751133 12908#define OPTION_MVEXWIG (OPTION_MD_BASE + 26)
e379e5f3
L
12909#define OPTION_MALIGN_BRANCH_BOUNDARY (OPTION_MD_BASE + 27)
12910#define OPTION_MALIGN_BRANCH_PREFIX_SIZE (OPTION_MD_BASE + 28)
12911#define OPTION_MALIGN_BRANCH (OPTION_MD_BASE + 29)
76cf450b 12912#define OPTION_MBRANCHES_WITH_32B_BOUNDARIES (OPTION_MD_BASE + 30)
ae531041
L
12913#define OPTION_MLFENCE_AFTER_LOAD (OPTION_MD_BASE + 31)
12914#define OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH (OPTION_MD_BASE + 32)
12915#define OPTION_MLFENCE_BEFORE_RET (OPTION_MD_BASE + 33)
b3b91714 12916
99ad8390
NC
12917struct option md_longopts[] =
12918{
3e73aa7c 12919 {"32", no_argument, NULL, OPTION_32},
321098a5 12920#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 12921 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c 12922 {"64", no_argument, NULL, OPTION_64},
351f65ca
L
12923#endif
12924#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 12925 {"x32", no_argument, NULL, OPTION_X32},
8dcea932 12926 {"mshared", no_argument, NULL, OPTION_MSHARED},
b4a3a7b4 12927 {"mx86-used-note", required_argument, NULL, OPTION_X86_USED_NOTE},
6e0b89ee 12928#endif
b3b91714 12929 {"divide", no_argument, NULL, OPTION_DIVIDE},
9103f4f4
L
12930 {"march", required_argument, NULL, OPTION_MARCH},
12931 {"mtune", required_argument, NULL, OPTION_MTUNE},
1efbbeb4
L
12932 {"mmnemonic", required_argument, NULL, OPTION_MMNEMONIC},
12933 {"msyntax", required_argument, NULL, OPTION_MSYNTAX},
12934 {"mindex-reg", no_argument, NULL, OPTION_MINDEX_REG},
12935 {"mnaked-reg", no_argument, NULL, OPTION_MNAKED_REG},
c0f3af97 12936 {"msse2avx", no_argument, NULL, OPTION_MSSE2AVX},
daf50ae7 12937 {"msse-check", required_argument, NULL, OPTION_MSSE_CHECK},
7bab8ab5 12938 {"moperand-check", required_argument, NULL, OPTION_MOPERAND_CHECK},
539f890d 12939 {"mavxscalar", required_argument, NULL, OPTION_MAVXSCALAR},
03751133 12940 {"mvexwig", required_argument, NULL, OPTION_MVEXWIG},
7e8b059b 12941 {"madd-bnd-prefix", no_argument, NULL, OPTION_MADD_BND_PREFIX},
43234a1e
L
12942 {"mevexlig", required_argument, NULL, OPTION_MEVEXLIG},
12943 {"mevexwig", required_argument, NULL, OPTION_MEVEXWIG},
167ad85b
TG
12944# if defined (TE_PE) || defined (TE_PEP)
12945 {"mbig-obj", no_argument, NULL, OPTION_MBIG_OBJ},
12946#endif
d1982f93 12947 {"momit-lock-prefix", required_argument, NULL, OPTION_MOMIT_LOCK_PREFIX},
e4e00185 12948 {"mfence-as-lock-add", required_argument, NULL, OPTION_MFENCE_AS_LOCK_ADD},
0cb4071e 12949 {"mrelax-relocations", required_argument, NULL, OPTION_MRELAX_RELOCATIONS},
d3d3c6db 12950 {"mevexrcig", required_argument, NULL, OPTION_MEVEXRCIG},
e379e5f3
L
12951 {"malign-branch-boundary", required_argument, NULL, OPTION_MALIGN_BRANCH_BOUNDARY},
12952 {"malign-branch-prefix-size", required_argument, NULL, OPTION_MALIGN_BRANCH_PREFIX_SIZE},
12953 {"malign-branch", required_argument, NULL, OPTION_MALIGN_BRANCH},
76cf450b 12954 {"mbranches-within-32B-boundaries", no_argument, NULL, OPTION_MBRANCHES_WITH_32B_BOUNDARIES},
ae531041
L
12955 {"mlfence-after-load", required_argument, NULL, OPTION_MLFENCE_AFTER_LOAD},
12956 {"mlfence-before-indirect-branch", required_argument, NULL,
12957 OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH},
12958 {"mlfence-before-ret", required_argument, NULL, OPTION_MLFENCE_BEFORE_RET},
5db04b09
L
12959 {"mamd64", no_argument, NULL, OPTION_MAMD64},
12960 {"mintel64", no_argument, NULL, OPTION_MINTEL64},
252b5132
RH
12961 {NULL, no_argument, NULL, 0}
12962};
12963size_t md_longopts_size = sizeof (md_longopts);
12964
12965int
17b9d67d 12966md_parse_option (int c, const char *arg)
252b5132 12967{
91d6fa6a 12968 unsigned int j;
e379e5f3 12969 char *arch, *next, *saved, *type;
9103f4f4 12970
252b5132
RH
12971 switch (c)
12972 {
12b55ccc
L
12973 case 'n':
12974 optimize_align_code = 0;
12975 break;
12976
a38cf1db
AM
12977 case 'q':
12978 quiet_warnings = 1;
252b5132
RH
12979 break;
12980
12981#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
a38cf1db
AM
12982 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
12983 should be emitted or not. FIXME: Not implemented. */
12984 case 'Q':
d4693039
JB
12985 if ((arg[0] != 'y' && arg[0] != 'n') || arg[1])
12986 return 0;
252b5132
RH
12987 break;
12988
12989 /* -V: SVR4 argument to print version ID. */
12990 case 'V':
12991 print_version_id ();
12992 break;
12993
a38cf1db
AM
12994 /* -k: Ignore for FreeBSD compatibility. */
12995 case 'k':
252b5132 12996 break;
4cc782b5
ILT
12997
12998 case 's':
12999 /* -s: On i386 Solaris, this tells the native assembler to use
29b0f896 13000 .stab instead of .stab.excl. We always use .stab anyhow. */
4cc782b5 13001 break;
8dcea932
L
13002
13003 case OPTION_MSHARED:
13004 shared = 1;
13005 break;
b4a3a7b4
L
13006
13007 case OPTION_X86_USED_NOTE:
13008 if (strcasecmp (arg, "yes") == 0)
13009 x86_used_note = 1;
13010 else if (strcasecmp (arg, "no") == 0)
13011 x86_used_note = 0;
13012 else
13013 as_fatal (_("invalid -mx86-used-note= option: `%s'"), arg);
13014 break;
13015
13016
99ad8390 13017#endif
321098a5 13018#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 13019 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c
JH
13020 case OPTION_64:
13021 {
13022 const char **list, **l;
13023
3e73aa7c
JH
13024 list = bfd_target_list ();
13025 for (l = list; *l != NULL; l++)
08dedd66 13026 if (startswith (*l, "elf64-x86-64")
99ad8390
NC
13027 || strcmp (*l, "coff-x86-64") == 0
13028 || strcmp (*l, "pe-x86-64") == 0
d382c579
TG
13029 || strcmp (*l, "pei-x86-64") == 0
13030 || strcmp (*l, "mach-o-x86-64") == 0)
6e0b89ee
AM
13031 {
13032 default_arch = "x86_64";
13033 break;
13034 }
3e73aa7c 13035 if (*l == NULL)
2b5d6a91 13036 as_fatal (_("no compiled in support for x86_64"));
3e73aa7c
JH
13037 free (list);
13038 }
13039 break;
13040#endif
252b5132 13041
351f65ca 13042#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 13043 case OPTION_X32:
351f65ca
L
13044 if (IS_ELF)
13045 {
13046 const char **list, **l;
13047
13048 list = bfd_target_list ();
13049 for (l = list; *l != NULL; l++)
08dedd66 13050 if (startswith (*l, "elf32-x86-64"))
351f65ca
L
13051 {
13052 default_arch = "x86_64:32";
13053 break;
13054 }
13055 if (*l == NULL)
2b5d6a91 13056 as_fatal (_("no compiled in support for 32bit x86_64"));
351f65ca
L
13057 free (list);
13058 }
13059 else
13060 as_fatal (_("32bit x86_64 is only supported for ELF"));
13061 break;
13062#endif
13063
6e0b89ee
AM
13064 case OPTION_32:
13065 default_arch = "i386";
13066 break;
13067
b3b91714
AM
13068 case OPTION_DIVIDE:
13069#ifdef SVR4_COMMENT_CHARS
13070 {
13071 char *n, *t;
13072 const char *s;
13073
add39d23 13074 n = XNEWVEC (char, strlen (i386_comment_chars) + 1);
b3b91714
AM
13075 t = n;
13076 for (s = i386_comment_chars; *s != '\0'; s++)
13077 if (*s != '/')
13078 *t++ = *s;
13079 *t = '\0';
13080 i386_comment_chars = n;
13081 }
13082#endif
13083 break;
13084
9103f4f4 13085 case OPTION_MARCH:
293f5f65
L
13086 saved = xstrdup (arg);
13087 arch = saved;
13088 /* Allow -march=+nosse. */
13089 if (*arch == '+')
13090 arch++;
6305a203 13091 do
9103f4f4 13092 {
6305a203 13093 if (*arch == '.')
2b5d6a91 13094 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
13095 next = strchr (arch, '+');
13096 if (next)
13097 *next++ = '\0';
91d6fa6a 13098 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 13099 {
91d6fa6a 13100 if (strcmp (arch, cpu_arch [j].name) == 0)
ccc9c027 13101 {
6305a203 13102 /* Processor. */
1ded5609
JB
13103 if (! cpu_arch[j].flags.bitfield.cpui386)
13104 continue;
13105
91d6fa6a 13106 cpu_arch_name = cpu_arch[j].name;
6305a203 13107 cpu_sub_arch_name = NULL;
91d6fa6a
NC
13108 cpu_arch_flags = cpu_arch[j].flags;
13109 cpu_arch_isa = cpu_arch[j].type;
13110 cpu_arch_isa_flags = cpu_arch[j].flags;
6305a203
L
13111 if (!cpu_arch_tune_set)
13112 {
13113 cpu_arch_tune = cpu_arch_isa;
13114 cpu_arch_tune_flags = cpu_arch_isa_flags;
13115 }
13116 break;
13117 }
91d6fa6a
NC
13118 else if (*cpu_arch [j].name == '.'
13119 && strcmp (arch, cpu_arch [j].name + 1) == 0)
6305a203 13120 {
33eaf5de 13121 /* ISA extension. */
6305a203 13122 i386_cpu_flags flags;
309d3373 13123
293f5f65
L
13124 flags = cpu_flags_or (cpu_arch_flags,
13125 cpu_arch[j].flags);
81486035 13126
5b64d091 13127 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
6305a203
L
13128 {
13129 if (cpu_sub_arch_name)
13130 {
13131 char *name = cpu_sub_arch_name;
13132 cpu_sub_arch_name = concat (name,
91d6fa6a 13133 cpu_arch[j].name,
1bf57e9f 13134 (const char *) NULL);
6305a203
L
13135 free (name);
13136 }
13137 else
91d6fa6a 13138 cpu_sub_arch_name = xstrdup (cpu_arch[j].name);
6305a203 13139 cpu_arch_flags = flags;
a586129e 13140 cpu_arch_isa_flags = flags;
6305a203 13141 }
0089dace
L
13142 else
13143 cpu_arch_isa_flags
13144 = cpu_flags_or (cpu_arch_isa_flags,
13145 cpu_arch[j].flags);
6305a203 13146 break;
ccc9c027 13147 }
9103f4f4 13148 }
6305a203 13149
293f5f65
L
13150 if (j >= ARRAY_SIZE (cpu_arch))
13151 {
33eaf5de 13152 /* Disable an ISA extension. */
293f5f65
L
13153 for (j = 0; j < ARRAY_SIZE (cpu_noarch); j++)
13154 if (strcmp (arch, cpu_noarch [j].name) == 0)
13155 {
13156 i386_cpu_flags flags;
13157
13158 flags = cpu_flags_and_not (cpu_arch_flags,
13159 cpu_noarch[j].flags);
13160 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
13161 {
13162 if (cpu_sub_arch_name)
13163 {
13164 char *name = cpu_sub_arch_name;
13165 cpu_sub_arch_name = concat (arch,
13166 (const char *) NULL);
13167 free (name);
13168 }
13169 else
13170 cpu_sub_arch_name = xstrdup (arch);
13171 cpu_arch_flags = flags;
13172 cpu_arch_isa_flags = flags;
13173 }
13174 break;
13175 }
13176
13177 if (j >= ARRAY_SIZE (cpu_noarch))
13178 j = ARRAY_SIZE (cpu_arch);
13179 }
13180
91d6fa6a 13181 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 13182 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
13183
13184 arch = next;
9103f4f4 13185 }
293f5f65
L
13186 while (next != NULL);
13187 free (saved);
9103f4f4
L
13188 break;
13189
13190 case OPTION_MTUNE:
13191 if (*arg == '.')
2b5d6a91 13192 as_fatal (_("invalid -mtune= option: `%s'"), arg);
91d6fa6a 13193 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 13194 {
91d6fa6a 13195 if (strcmp (arg, cpu_arch [j].name) == 0)
9103f4f4 13196 {
ccc9c027 13197 cpu_arch_tune_set = 1;
91d6fa6a
NC
13198 cpu_arch_tune = cpu_arch [j].type;
13199 cpu_arch_tune_flags = cpu_arch[j].flags;
9103f4f4
L
13200 break;
13201 }
13202 }
91d6fa6a 13203 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 13204 as_fatal (_("invalid -mtune= option: `%s'"), arg);
9103f4f4
L
13205 break;
13206
1efbbeb4
L
13207 case OPTION_MMNEMONIC:
13208 if (strcasecmp (arg, "att") == 0)
13209 intel_mnemonic = 0;
13210 else if (strcasecmp (arg, "intel") == 0)
13211 intel_mnemonic = 1;
13212 else
2b5d6a91 13213 as_fatal (_("invalid -mmnemonic= option: `%s'"), arg);
1efbbeb4
L
13214 break;
13215
13216 case OPTION_MSYNTAX:
13217 if (strcasecmp (arg, "att") == 0)
13218 intel_syntax = 0;
13219 else if (strcasecmp (arg, "intel") == 0)
13220 intel_syntax = 1;
13221 else
2b5d6a91 13222 as_fatal (_("invalid -msyntax= option: `%s'"), arg);
1efbbeb4
L
13223 break;
13224
13225 case OPTION_MINDEX_REG:
13226 allow_index_reg = 1;
13227 break;
13228
13229 case OPTION_MNAKED_REG:
13230 allow_naked_reg = 1;
13231 break;
13232
c0f3af97
L
13233 case OPTION_MSSE2AVX:
13234 sse2avx = 1;
13235 break;
13236
daf50ae7
L
13237 case OPTION_MSSE_CHECK:
13238 if (strcasecmp (arg, "error") == 0)
7bab8ab5 13239 sse_check = check_error;
daf50ae7 13240 else if (strcasecmp (arg, "warning") == 0)
7bab8ab5 13241 sse_check = check_warning;
daf50ae7 13242 else if (strcasecmp (arg, "none") == 0)
7bab8ab5 13243 sse_check = check_none;
daf50ae7 13244 else
2b5d6a91 13245 as_fatal (_("invalid -msse-check= option: `%s'"), arg);
daf50ae7
L
13246 break;
13247
7bab8ab5
JB
13248 case OPTION_MOPERAND_CHECK:
13249 if (strcasecmp (arg, "error") == 0)
13250 operand_check = check_error;
13251 else if (strcasecmp (arg, "warning") == 0)
13252 operand_check = check_warning;
13253 else if (strcasecmp (arg, "none") == 0)
13254 operand_check = check_none;
13255 else
13256 as_fatal (_("invalid -moperand-check= option: `%s'"), arg);
13257 break;
13258
539f890d
L
13259 case OPTION_MAVXSCALAR:
13260 if (strcasecmp (arg, "128") == 0)
13261 avxscalar = vex128;
13262 else if (strcasecmp (arg, "256") == 0)
13263 avxscalar = vex256;
13264 else
2b5d6a91 13265 as_fatal (_("invalid -mavxscalar= option: `%s'"), arg);
539f890d
L
13266 break;
13267
03751133
L
13268 case OPTION_MVEXWIG:
13269 if (strcmp (arg, "0") == 0)
40c9c8de 13270 vexwig = vexw0;
03751133 13271 else if (strcmp (arg, "1") == 0)
40c9c8de 13272 vexwig = vexw1;
03751133
L
13273 else
13274 as_fatal (_("invalid -mvexwig= option: `%s'"), arg);
13275 break;
13276
7e8b059b
L
13277 case OPTION_MADD_BND_PREFIX:
13278 add_bnd_prefix = 1;
13279 break;
13280
43234a1e
L
13281 case OPTION_MEVEXLIG:
13282 if (strcmp (arg, "128") == 0)
13283 evexlig = evexl128;
13284 else if (strcmp (arg, "256") == 0)
13285 evexlig = evexl256;
13286 else if (strcmp (arg, "512") == 0)
13287 evexlig = evexl512;
13288 else
13289 as_fatal (_("invalid -mevexlig= option: `%s'"), arg);
13290 break;
13291
d3d3c6db
IT
13292 case OPTION_MEVEXRCIG:
13293 if (strcmp (arg, "rne") == 0)
13294 evexrcig = rne;
13295 else if (strcmp (arg, "rd") == 0)
13296 evexrcig = rd;
13297 else if (strcmp (arg, "ru") == 0)
13298 evexrcig = ru;
13299 else if (strcmp (arg, "rz") == 0)
13300 evexrcig = rz;
13301 else
13302 as_fatal (_("invalid -mevexrcig= option: `%s'"), arg);
13303 break;
13304
43234a1e
L
13305 case OPTION_MEVEXWIG:
13306 if (strcmp (arg, "0") == 0)
13307 evexwig = evexw0;
13308 else if (strcmp (arg, "1") == 0)
13309 evexwig = evexw1;
13310 else
13311 as_fatal (_("invalid -mevexwig= option: `%s'"), arg);
13312 break;
13313
167ad85b
TG
13314# if defined (TE_PE) || defined (TE_PEP)
13315 case OPTION_MBIG_OBJ:
13316 use_big_obj = 1;
13317 break;
13318#endif
13319
d1982f93 13320 case OPTION_MOMIT_LOCK_PREFIX:
d022bddd
IT
13321 if (strcasecmp (arg, "yes") == 0)
13322 omit_lock_prefix = 1;
13323 else if (strcasecmp (arg, "no") == 0)
13324 omit_lock_prefix = 0;
13325 else
13326 as_fatal (_("invalid -momit-lock-prefix= option: `%s'"), arg);
13327 break;
13328
e4e00185
AS
13329 case OPTION_MFENCE_AS_LOCK_ADD:
13330 if (strcasecmp (arg, "yes") == 0)
13331 avoid_fence = 1;
13332 else if (strcasecmp (arg, "no") == 0)
13333 avoid_fence = 0;
13334 else
13335 as_fatal (_("invalid -mfence-as-lock-add= option: `%s'"), arg);
13336 break;
13337
ae531041
L
13338 case OPTION_MLFENCE_AFTER_LOAD:
13339 if (strcasecmp (arg, "yes") == 0)
13340 lfence_after_load = 1;
13341 else if (strcasecmp (arg, "no") == 0)
13342 lfence_after_load = 0;
13343 else
13344 as_fatal (_("invalid -mlfence-after-load= option: `%s'"), arg);
13345 break;
13346
13347 case OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH:
13348 if (strcasecmp (arg, "all") == 0)
a09f656b 13349 {
13350 lfence_before_indirect_branch = lfence_branch_all;
13351 if (lfence_before_ret == lfence_before_ret_none)
13352 lfence_before_ret = lfence_before_ret_shl;
13353 }
ae531041
L
13354 else if (strcasecmp (arg, "memory") == 0)
13355 lfence_before_indirect_branch = lfence_branch_memory;
13356 else if (strcasecmp (arg, "register") == 0)
13357 lfence_before_indirect_branch = lfence_branch_register;
13358 else if (strcasecmp (arg, "none") == 0)
13359 lfence_before_indirect_branch = lfence_branch_none;
13360 else
13361 as_fatal (_("invalid -mlfence-before-indirect-branch= option: `%s'"),
13362 arg);
13363 break;
13364
13365 case OPTION_MLFENCE_BEFORE_RET:
13366 if (strcasecmp (arg, "or") == 0)
13367 lfence_before_ret = lfence_before_ret_or;
13368 else if (strcasecmp (arg, "not") == 0)
13369 lfence_before_ret = lfence_before_ret_not;
a09f656b 13370 else if (strcasecmp (arg, "shl") == 0 || strcasecmp (arg, "yes") == 0)
13371 lfence_before_ret = lfence_before_ret_shl;
ae531041
L
13372 else if (strcasecmp (arg, "none") == 0)
13373 lfence_before_ret = lfence_before_ret_none;
13374 else
13375 as_fatal (_("invalid -mlfence-before-ret= option: `%s'"),
13376 arg);
13377 break;
13378
0cb4071e
L
13379 case OPTION_MRELAX_RELOCATIONS:
13380 if (strcasecmp (arg, "yes") == 0)
13381 generate_relax_relocations = 1;
13382 else if (strcasecmp (arg, "no") == 0)
13383 generate_relax_relocations = 0;
13384 else
13385 as_fatal (_("invalid -mrelax-relocations= option: `%s'"), arg);
13386 break;
13387
e379e5f3
L
13388 case OPTION_MALIGN_BRANCH_BOUNDARY:
13389 {
13390 char *end;
13391 long int align = strtoul (arg, &end, 0);
13392 if (*end == '\0')
13393 {
13394 if (align == 0)
13395 {
13396 align_branch_power = 0;
13397 break;
13398 }
13399 else if (align >= 16)
13400 {
13401 int align_power;
13402 for (align_power = 0;
13403 (align & 1) == 0;
13404 align >>= 1, align_power++)
13405 continue;
13406 /* Limit alignment power to 31. */
13407 if (align == 1 && align_power < 32)
13408 {
13409 align_branch_power = align_power;
13410 break;
13411 }
13412 }
13413 }
13414 as_fatal (_("invalid -malign-branch-boundary= value: %s"), arg);
13415 }
13416 break;
13417
13418 case OPTION_MALIGN_BRANCH_PREFIX_SIZE:
13419 {
13420 char *end;
13421 int align = strtoul (arg, &end, 0);
13422 /* Some processors only support 5 prefixes. */
13423 if (*end == '\0' && align >= 0 && align < 6)
13424 {
13425 align_branch_prefix_size = align;
13426 break;
13427 }
13428 as_fatal (_("invalid -malign-branch-prefix-size= value: %s"),
13429 arg);
13430 }
13431 break;
13432
13433 case OPTION_MALIGN_BRANCH:
13434 align_branch = 0;
13435 saved = xstrdup (arg);
13436 type = saved;
13437 do
13438 {
13439 next = strchr (type, '+');
13440 if (next)
13441 *next++ = '\0';
13442 if (strcasecmp (type, "jcc") == 0)
13443 align_branch |= align_branch_jcc_bit;
13444 else if (strcasecmp (type, "fused") == 0)
13445 align_branch |= align_branch_fused_bit;
13446 else if (strcasecmp (type, "jmp") == 0)
13447 align_branch |= align_branch_jmp_bit;
13448 else if (strcasecmp (type, "call") == 0)
13449 align_branch |= align_branch_call_bit;
13450 else if (strcasecmp (type, "ret") == 0)
13451 align_branch |= align_branch_ret_bit;
13452 else if (strcasecmp (type, "indirect") == 0)
13453 align_branch |= align_branch_indirect_bit;
13454 else
13455 as_fatal (_("invalid -malign-branch= option: `%s'"), arg);
13456 type = next;
13457 }
13458 while (next != NULL);
13459 free (saved);
13460 break;
13461
76cf450b
L
13462 case OPTION_MBRANCHES_WITH_32B_BOUNDARIES:
13463 align_branch_power = 5;
13464 align_branch_prefix_size = 5;
13465 align_branch = (align_branch_jcc_bit
13466 | align_branch_fused_bit
13467 | align_branch_jmp_bit);
13468 break;
13469
5db04b09 13470 case OPTION_MAMD64:
4b5aaf5f 13471 isa64 = amd64;
5db04b09
L
13472 break;
13473
13474 case OPTION_MINTEL64:
4b5aaf5f 13475 isa64 = intel64;
5db04b09
L
13476 break;
13477
b6f8c7c4
L
13478 case 'O':
13479 if (arg == NULL)
13480 {
13481 optimize = 1;
13482 /* Turn off -Os. */
13483 optimize_for_space = 0;
13484 }
13485 else if (*arg == 's')
13486 {
13487 optimize_for_space = 1;
13488 /* Turn on all encoding optimizations. */
41fd2579 13489 optimize = INT_MAX;
b6f8c7c4
L
13490 }
13491 else
13492 {
13493 optimize = atoi (arg);
13494 /* Turn off -Os. */
13495 optimize_for_space = 0;
13496 }
13497 break;
13498
252b5132
RH
13499 default:
13500 return 0;
13501 }
13502 return 1;
13503}
13504
8a2c8fef
L
13505#define MESSAGE_TEMPLATE \
13506" "
13507
293f5f65
L
13508static char *
13509output_message (FILE *stream, char *p, char *message, char *start,
13510 int *left_p, const char *name, int len)
13511{
13512 int size = sizeof (MESSAGE_TEMPLATE);
13513 int left = *left_p;
13514
13515 /* Reserve 2 spaces for ", " or ",\0" */
13516 left -= len + 2;
13517
13518 /* Check if there is any room. */
13519 if (left >= 0)
13520 {
13521 if (p != start)
13522 {
13523 *p++ = ',';
13524 *p++ = ' ';
13525 }
13526 p = mempcpy (p, name, len);
13527 }
13528 else
13529 {
13530 /* Output the current message now and start a new one. */
13531 *p++ = ',';
13532 *p = '\0';
13533 fprintf (stream, "%s\n", message);
13534 p = start;
13535 left = size - (start - message) - len - 2;
13536
13537 gas_assert (left >= 0);
13538
13539 p = mempcpy (p, name, len);
13540 }
13541
13542 *left_p = left;
13543 return p;
13544}
13545
8a2c8fef 13546static void
1ded5609 13547show_arch (FILE *stream, int ext, int check)
8a2c8fef
L
13548{
13549 static char message[] = MESSAGE_TEMPLATE;
13550 char *start = message + 27;
13551 char *p;
13552 int size = sizeof (MESSAGE_TEMPLATE);
13553 int left;
13554 const char *name;
13555 int len;
13556 unsigned int j;
13557
13558 p = start;
13559 left = size - (start - message);
13560 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13561 {
13562 /* Should it be skipped? */
13563 if (cpu_arch [j].skip)
13564 continue;
13565
13566 name = cpu_arch [j].name;
13567 len = cpu_arch [j].len;
13568 if (*name == '.')
13569 {
13570 /* It is an extension. Skip if we aren't asked to show it. */
13571 if (ext)
13572 {
13573 name++;
13574 len--;
13575 }
13576 else
13577 continue;
13578 }
13579 else if (ext)
13580 {
13581 /* It is an processor. Skip if we show only extension. */
13582 continue;
13583 }
1ded5609
JB
13584 else if (check && ! cpu_arch[j].flags.bitfield.cpui386)
13585 {
13586 /* It is an impossible processor - skip. */
13587 continue;
13588 }
8a2c8fef 13589
293f5f65 13590 p = output_message (stream, p, message, start, &left, name, len);
8a2c8fef
L
13591 }
13592
293f5f65
L
13593 /* Display disabled extensions. */
13594 if (ext)
13595 for (j = 0; j < ARRAY_SIZE (cpu_noarch); j++)
13596 {
13597 name = cpu_noarch [j].name;
13598 len = cpu_noarch [j].len;
13599 p = output_message (stream, p, message, start, &left, name,
13600 len);
13601 }
13602
8a2c8fef
L
13603 *p = '\0';
13604 fprintf (stream, "%s\n", message);
13605}
13606
252b5132 13607void
8a2c8fef 13608md_show_usage (FILE *stream)
252b5132 13609{
4cc782b5
ILT
13610#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13611 fprintf (stream, _("\
d4693039 13612 -Qy, -Qn ignored\n\
a38cf1db 13613 -V print assembler version number\n\
b3b91714
AM
13614 -k ignored\n"));
13615#endif
13616 fprintf (stream, _("\
12b55ccc 13617 -n Do not optimize code alignment\n\
b3b91714
AM
13618 -q quieten some warnings\n"));
13619#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13620 fprintf (stream, _("\
a38cf1db 13621 -s ignored\n"));
b3b91714 13622#endif
d7f449c0
L
13623#if defined BFD64 && (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
13624 || defined (TE_PE) || defined (TE_PEP))
751d281c 13625 fprintf (stream, _("\
570561f7 13626 --32/--64/--x32 generate 32bit/64bit/x32 code\n"));
751d281c 13627#endif
b3b91714
AM
13628#ifdef SVR4_COMMENT_CHARS
13629 fprintf (stream, _("\
13630 --divide do not treat `/' as a comment character\n"));
a38cf1db
AM
13631#else
13632 fprintf (stream, _("\
b3b91714 13633 --divide ignored\n"));
4cc782b5 13634#endif
9103f4f4 13635 fprintf (stream, _("\
6305a203 13636 -march=CPU[,+EXTENSION...]\n\
8a2c8fef 13637 generate code for CPU and EXTENSION, CPU is one of:\n"));
1ded5609 13638 show_arch (stream, 0, 1);
8a2c8fef
L
13639 fprintf (stream, _("\
13640 EXTENSION is combination of:\n"));
1ded5609 13641 show_arch (stream, 1, 0);
6305a203 13642 fprintf (stream, _("\
8a2c8fef 13643 -mtune=CPU optimize for CPU, CPU is one of:\n"));
1ded5609 13644 show_arch (stream, 0, 0);
ba104c83 13645 fprintf (stream, _("\
c0f3af97
L
13646 -msse2avx encode SSE instructions with VEX prefix\n"));
13647 fprintf (stream, _("\
7c5c05ef 13648 -msse-check=[none|error|warning] (default: warning)\n\
daf50ae7
L
13649 check SSE instructions\n"));
13650 fprintf (stream, _("\
7c5c05ef 13651 -moperand-check=[none|error|warning] (default: warning)\n\
7bab8ab5
JB
13652 check operand combinations for validity\n"));
13653 fprintf (stream, _("\
7c5c05ef
L
13654 -mavxscalar=[128|256] (default: 128)\n\
13655 encode scalar AVX instructions with specific vector\n\
539f890d
L
13656 length\n"));
13657 fprintf (stream, _("\
03751133
L
13658 -mvexwig=[0|1] (default: 0)\n\
13659 encode VEX instructions with specific VEX.W value\n\
13660 for VEX.W bit ignored instructions\n"));
13661 fprintf (stream, _("\
7c5c05ef
L
13662 -mevexlig=[128|256|512] (default: 128)\n\
13663 encode scalar EVEX instructions with specific vector\n\
43234a1e
L
13664 length\n"));
13665 fprintf (stream, _("\
7c5c05ef
L
13666 -mevexwig=[0|1] (default: 0)\n\
13667 encode EVEX instructions with specific EVEX.W value\n\
43234a1e
L
13668 for EVEX.W bit ignored instructions\n"));
13669 fprintf (stream, _("\
7c5c05ef 13670 -mevexrcig=[rne|rd|ru|rz] (default: rne)\n\
d3d3c6db
IT
13671 encode EVEX instructions with specific EVEX.RC value\n\
13672 for SAE-only ignored instructions\n"));
13673 fprintf (stream, _("\
7c5c05ef
L
13674 -mmnemonic=[att|intel] "));
13675 if (SYSV386_COMPAT)
13676 fprintf (stream, _("(default: att)\n"));
13677 else
13678 fprintf (stream, _("(default: intel)\n"));
13679 fprintf (stream, _("\
13680 use AT&T/Intel mnemonic\n"));
ba104c83 13681 fprintf (stream, _("\
7c5c05ef
L
13682 -msyntax=[att|intel] (default: att)\n\
13683 use AT&T/Intel syntax\n"));
ba104c83
L
13684 fprintf (stream, _("\
13685 -mindex-reg support pseudo index registers\n"));
13686 fprintf (stream, _("\
13687 -mnaked-reg don't require `%%' prefix for registers\n"));
13688 fprintf (stream, _("\
7e8b059b 13689 -madd-bnd-prefix add BND prefix for all valid branches\n"));
b4a3a7b4 13690#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8dcea932
L
13691 fprintf (stream, _("\
13692 -mshared disable branch optimization for shared code\n"));
b4a3a7b4
L
13693 fprintf (stream, _("\
13694 -mx86-used-note=[no|yes] "));
13695 if (DEFAULT_X86_USED_NOTE)
13696 fprintf (stream, _("(default: yes)\n"));
13697 else
13698 fprintf (stream, _("(default: no)\n"));
13699 fprintf (stream, _("\
13700 generate x86 used ISA and feature properties\n"));
13701#endif
13702#if defined (TE_PE) || defined (TE_PEP)
167ad85b
TG
13703 fprintf (stream, _("\
13704 -mbig-obj generate big object files\n"));
13705#endif
d022bddd 13706 fprintf (stream, _("\
7c5c05ef 13707 -momit-lock-prefix=[no|yes] (default: no)\n\
d022bddd 13708 strip all lock prefixes\n"));
5db04b09 13709 fprintf (stream, _("\
7c5c05ef 13710 -mfence-as-lock-add=[no|yes] (default: no)\n\
e4e00185
AS
13711 encode lfence, mfence and sfence as\n\
13712 lock addl $0x0, (%%{re}sp)\n"));
13713 fprintf (stream, _("\
7c5c05ef
L
13714 -mrelax-relocations=[no|yes] "));
13715 if (DEFAULT_GENERATE_X86_RELAX_RELOCATIONS)
13716 fprintf (stream, _("(default: yes)\n"));
13717 else
13718 fprintf (stream, _("(default: no)\n"));
13719 fprintf (stream, _("\
0cb4071e
L
13720 generate relax relocations\n"));
13721 fprintf (stream, _("\
e379e5f3
L
13722 -malign-branch-boundary=NUM (default: 0)\n\
13723 align branches within NUM byte boundary\n"));
13724 fprintf (stream, _("\
13725 -malign-branch=TYPE[+TYPE...] (default: jcc+fused+jmp)\n\
13726 TYPE is combination of jcc, fused, jmp, call, ret,\n\
13727 indirect\n\
13728 specify types of branches to align\n"));
13729 fprintf (stream, _("\
13730 -malign-branch-prefix-size=NUM (default: 5)\n\
13731 align branches with NUM prefixes per instruction\n"));
13732 fprintf (stream, _("\
76cf450b
L
13733 -mbranches-within-32B-boundaries\n\
13734 align branches within 32 byte boundary\n"));
13735 fprintf (stream, _("\
ae531041
L
13736 -mlfence-after-load=[no|yes] (default: no)\n\
13737 generate lfence after load\n"));
13738 fprintf (stream, _("\
13739 -mlfence-before-indirect-branch=[none|all|register|memory] (default: none)\n\
13740 generate lfence before indirect near branch\n"));
13741 fprintf (stream, _("\
a09f656b 13742 -mlfence-before-ret=[none|or|not|shl|yes] (default: none)\n\
ae531041
L
13743 generate lfence before ret\n"));
13744 fprintf (stream, _("\
7c5c05ef 13745 -mamd64 accept only AMD64 ISA [default]\n"));
5db04b09
L
13746 fprintf (stream, _("\
13747 -mintel64 accept only Intel64 ISA\n"));
252b5132
RH
13748}
13749
3e73aa7c 13750#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
321098a5 13751 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
e57f8c65 13752 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
252b5132
RH
13753
13754/* Pick the target format to use. */
13755
47926f60 13756const char *
e3bb37b5 13757i386_target_format (void)
252b5132 13758{
351f65ca
L
13759 if (!strncmp (default_arch, "x86_64", 6))
13760 {
13761 update_code_flag (CODE_64BIT, 1);
13762 if (default_arch[6] == '\0')
7f56bc95 13763 x86_elf_abi = X86_64_ABI;
351f65ca 13764 else
7f56bc95 13765 x86_elf_abi = X86_64_X32_ABI;
351f65ca 13766 }
3e73aa7c 13767 else if (!strcmp (default_arch, "i386"))
78f12dd3 13768 update_code_flag (CODE_32BIT, 1);
5197d474
L
13769 else if (!strcmp (default_arch, "iamcu"))
13770 {
13771 update_code_flag (CODE_32BIT, 1);
13772 if (cpu_arch_isa == PROCESSOR_UNKNOWN)
13773 {
13774 static const i386_cpu_flags iamcu_flags = CPU_IAMCU_FLAGS;
13775 cpu_arch_name = "iamcu";
13776 cpu_sub_arch_name = NULL;
13777 cpu_arch_flags = iamcu_flags;
13778 cpu_arch_isa = PROCESSOR_IAMCU;
13779 cpu_arch_isa_flags = iamcu_flags;
13780 if (!cpu_arch_tune_set)
13781 {
13782 cpu_arch_tune = cpu_arch_isa;
13783 cpu_arch_tune_flags = cpu_arch_isa_flags;
13784 }
13785 }
8d471ec1 13786 else if (cpu_arch_isa != PROCESSOR_IAMCU)
5197d474
L
13787 as_fatal (_("Intel MCU doesn't support `%s' architecture"),
13788 cpu_arch_name);
13789 }
3e73aa7c 13790 else
2b5d6a91 13791 as_fatal (_("unknown architecture"));
89507696
JB
13792
13793 if (cpu_flags_all_zero (&cpu_arch_isa_flags))
13794 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].flags;
13795 if (cpu_flags_all_zero (&cpu_arch_tune_flags))
13796 cpu_arch_tune_flags = cpu_arch[flag_code == CODE_64BIT].flags;
13797
252b5132
RH
13798 switch (OUTPUT_FLAVOR)
13799 {
9384f2ff 13800#if defined (OBJ_MAYBE_AOUT) || defined (OBJ_AOUT)
4c63da97 13801 case bfd_target_aout_flavour:
47926f60 13802 return AOUT_TARGET_FORMAT;
4c63da97 13803#endif
9384f2ff
AM
13804#if defined (OBJ_MAYBE_COFF) || defined (OBJ_COFF)
13805# if defined (TE_PE) || defined (TE_PEP)
13806 case bfd_target_coff_flavour:
167ad85b
TG
13807 if (flag_code == CODE_64BIT)
13808 return use_big_obj ? "pe-bigobj-x86-64" : "pe-x86-64";
13809 else
251dae91 13810 return use_big_obj ? "pe-bigobj-i386" : "pe-i386";
9384f2ff 13811# elif defined (TE_GO32)
0561d57c
JK
13812 case bfd_target_coff_flavour:
13813 return "coff-go32";
9384f2ff 13814# else
252b5132
RH
13815 case bfd_target_coff_flavour:
13816 return "coff-i386";
9384f2ff 13817# endif
4c63da97 13818#endif
3e73aa7c 13819#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
252b5132 13820 case bfd_target_elf_flavour:
3e73aa7c 13821 {
351f65ca
L
13822 const char *format;
13823
13824 switch (x86_elf_abi)
4fa24527 13825 {
351f65ca
L
13826 default:
13827 format = ELF_TARGET_FORMAT;
e379e5f3
L
13828#ifndef TE_SOLARIS
13829 tls_get_addr = "___tls_get_addr";
13830#endif
351f65ca 13831 break;
7f56bc95 13832 case X86_64_ABI:
351f65ca 13833 use_rela_relocations = 1;
4fa24527 13834 object_64bit = 1;
e379e5f3
L
13835#ifndef TE_SOLARIS
13836 tls_get_addr = "__tls_get_addr";
13837#endif
351f65ca
L
13838 format = ELF_TARGET_FORMAT64;
13839 break;
7f56bc95 13840 case X86_64_X32_ABI:
4fa24527 13841 use_rela_relocations = 1;
351f65ca 13842 object_64bit = 1;
e379e5f3
L
13843#ifndef TE_SOLARIS
13844 tls_get_addr = "__tls_get_addr";
13845#endif
862be3fb 13846 disallow_64bit_reloc = 1;
351f65ca
L
13847 format = ELF_TARGET_FORMAT32;
13848 break;
4fa24527 13849 }
3632d14b 13850 if (cpu_arch_isa == PROCESSOR_L1OM)
8a9036a4 13851 {
7f56bc95 13852 if (x86_elf_abi != X86_64_ABI)
8a9036a4
L
13853 as_fatal (_("Intel L1OM is 64bit only"));
13854 return ELF_TARGET_L1OM_FORMAT;
13855 }
b49f93f6 13856 else if (cpu_arch_isa == PROCESSOR_K1OM)
7a9068fe
L
13857 {
13858 if (x86_elf_abi != X86_64_ABI)
13859 as_fatal (_("Intel K1OM is 64bit only"));
13860 return ELF_TARGET_K1OM_FORMAT;
13861 }
81486035
L
13862 else if (cpu_arch_isa == PROCESSOR_IAMCU)
13863 {
13864 if (x86_elf_abi != I386_ABI)
13865 as_fatal (_("Intel MCU is 32bit only"));
13866 return ELF_TARGET_IAMCU_FORMAT;
13867 }
8a9036a4 13868 else
351f65ca 13869 return format;
3e73aa7c 13870 }
e57f8c65
TG
13871#endif
13872#if defined (OBJ_MACH_O)
13873 case bfd_target_mach_o_flavour:
d382c579
TG
13874 if (flag_code == CODE_64BIT)
13875 {
13876 use_rela_relocations = 1;
13877 object_64bit = 1;
13878 return "mach-o-x86-64";
13879 }
13880 else
13881 return "mach-o-i386";
4c63da97 13882#endif
252b5132
RH
13883 default:
13884 abort ();
13885 return NULL;
13886 }
13887}
13888
47926f60 13889#endif /* OBJ_MAYBE_ more than one */
252b5132 13890\f
252b5132 13891symbolS *
7016a5d5 13892md_undefined_symbol (char *name)
252b5132 13893{
18dc2407
ILT
13894 if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
13895 && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
13896 && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
13897 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
24eab124
AM
13898 {
13899 if (!GOT_symbol)
13900 {
13901 if (symbol_find (name))
13902 as_bad (_("GOT already in symbol table"));
13903 GOT_symbol = symbol_new (name, undefined_section,
e01e1cee 13904 &zero_address_frag, 0);
24eab124
AM
13905 };
13906 return GOT_symbol;
13907 }
252b5132
RH
13908 return 0;
13909}
13910
13911/* Round up a section size to the appropriate boundary. */
47926f60 13912
252b5132 13913valueT
7016a5d5 13914md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
252b5132 13915{
4c63da97
AM
13916#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
13917 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
13918 {
13919 /* For a.out, force the section size to be aligned. If we don't do
13920 this, BFD will align it for us, but it will not write out the
13921 final bytes of the section. This may be a bug in BFD, but it is
13922 easier to fix it here since that is how the other a.out targets
13923 work. */
13924 int align;
13925
fd361982 13926 align = bfd_section_alignment (segment);
8d3842cd 13927 size = ((size + (1 << align) - 1) & (-((valueT) 1 << align)));
4c63da97 13928 }
252b5132
RH
13929#endif
13930
13931 return size;
13932}
13933
13934/* On the i386, PC-relative offsets are relative to the start of the
13935 next instruction. That is, the address of the offset, plus its
13936 size, since the offset is always the last part of the insn. */
13937
13938long
e3bb37b5 13939md_pcrel_from (fixS *fixP)
252b5132
RH
13940{
13941 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
13942}
13943
13944#ifndef I386COFF
13945
13946static void
e3bb37b5 13947s_bss (int ignore ATTRIBUTE_UNUSED)
252b5132 13948{
29b0f896 13949 int temp;
252b5132 13950
8a75718c
JB
13951#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13952 if (IS_ELF)
13953 obj_elf_section_change_hook ();
13954#endif
252b5132
RH
13955 temp = get_absolute_expression ();
13956 subseg_set (bss_section, (subsegT) temp);
13957 demand_empty_rest_of_line ();
13958}
13959
13960#endif
13961
e379e5f3
L
13962/* Remember constant directive. */
13963
13964void
13965i386_cons_align (int ignore ATTRIBUTE_UNUSED)
13966{
13967 if (last_insn.kind != last_insn_directive
13968 && (bfd_section_flags (now_seg) & SEC_CODE))
13969 {
13970 last_insn.seg = now_seg;
13971 last_insn.kind = last_insn_directive;
13972 last_insn.name = "constant directive";
13973 last_insn.file = as_where (&last_insn.line);
ae531041
L
13974 if (lfence_before_ret != lfence_before_ret_none)
13975 {
13976 if (lfence_before_indirect_branch != lfence_branch_none)
13977 as_warn (_("constant directive skips -mlfence-before-ret "
13978 "and -mlfence-before-indirect-branch"));
13979 else
13980 as_warn (_("constant directive skips -mlfence-before-ret"));
13981 }
13982 else if (lfence_before_indirect_branch != lfence_branch_none)
13983 as_warn (_("constant directive skips -mlfence-before-indirect-branch"));
e379e5f3
L
13984 }
13985}
13986
252b5132 13987void
e3bb37b5 13988i386_validate_fix (fixS *fixp)
252b5132 13989{
02a86693 13990 if (fixp->fx_subsy)
252b5132 13991 {
02a86693 13992 if (fixp->fx_subsy == GOT_symbol)
23df1078 13993 {
02a86693
L
13994 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
13995 {
13996 if (!object_64bit)
13997 abort ();
13998#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13999 if (fixp->fx_tcbit2)
56ceb5b5
L
14000 fixp->fx_r_type = (fixp->fx_tcbit
14001 ? BFD_RELOC_X86_64_REX_GOTPCRELX
14002 : BFD_RELOC_X86_64_GOTPCRELX);
02a86693
L
14003 else
14004#endif
14005 fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
14006 }
d6ab8113 14007 else
02a86693
L
14008 {
14009 if (!object_64bit)
14010 fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
14011 else
14012 fixp->fx_r_type = BFD_RELOC_X86_64_GOTOFF64;
14013 }
14014 fixp->fx_subsy = 0;
23df1078 14015 }
252b5132 14016 }
02a86693 14017#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2585b7a5 14018 else
02a86693 14019 {
2585b7a5
L
14020 /* NB: Commit 292676c1 resolved PLT32 reloc aganst local symbol
14021 to section. Since PLT32 relocation must be against symbols,
14022 turn such PLT32 relocation into PC32 relocation. */
14023 if (fixp->fx_addsy
14024 && (fixp->fx_r_type == BFD_RELOC_386_PLT32
14025 || fixp->fx_r_type == BFD_RELOC_X86_64_PLT32)
14026 && symbol_section_p (fixp->fx_addsy))
14027 fixp->fx_r_type = BFD_RELOC_32_PCREL;
14028 if (!object_64bit)
14029 {
14030 if (fixp->fx_r_type == BFD_RELOC_386_GOT32
14031 && fixp->fx_tcbit2)
14032 fixp->fx_r_type = BFD_RELOC_386_GOT32X;
14033 }
02a86693
L
14034 }
14035#endif
252b5132
RH
14036}
14037
252b5132 14038arelent *
7016a5d5 14039tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
14040{
14041 arelent *rel;
14042 bfd_reloc_code_real_type code;
14043
14044 switch (fixp->fx_r_type)
14045 {
8ce3d284 14046#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
14047 case BFD_RELOC_SIZE32:
14048 case BFD_RELOC_SIZE64:
14049 if (S_IS_DEFINED (fixp->fx_addsy)
14050 && !S_IS_EXTERNAL (fixp->fx_addsy))
14051 {
14052 /* Resolve size relocation against local symbol to size of
14053 the symbol plus addend. */
14054 valueT value = S_GET_SIZE (fixp->fx_addsy) + fixp->fx_offset;
14055 if (fixp->fx_r_type == BFD_RELOC_SIZE32
14056 && !fits_in_unsigned_long (value))
14057 as_bad_where (fixp->fx_file, fixp->fx_line,
14058 _("symbol size computation overflow"));
14059 fixp->fx_addsy = NULL;
14060 fixp->fx_subsy = NULL;
14061 md_apply_fix (fixp, (valueT *) &value, NULL);
14062 return NULL;
14063 }
8ce3d284 14064#endif
1a0670f3 14065 /* Fall through. */
8fd4256d 14066
3e73aa7c
JH
14067 case BFD_RELOC_X86_64_PLT32:
14068 case BFD_RELOC_X86_64_GOT32:
14069 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
14070 case BFD_RELOC_X86_64_GOTPCRELX:
14071 case BFD_RELOC_X86_64_REX_GOTPCRELX:
252b5132
RH
14072 case BFD_RELOC_386_PLT32:
14073 case BFD_RELOC_386_GOT32:
02a86693 14074 case BFD_RELOC_386_GOT32X:
252b5132
RH
14075 case BFD_RELOC_386_GOTOFF:
14076 case BFD_RELOC_386_GOTPC:
13ae64f3
JJ
14077 case BFD_RELOC_386_TLS_GD:
14078 case BFD_RELOC_386_TLS_LDM:
14079 case BFD_RELOC_386_TLS_LDO_32:
14080 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
14081 case BFD_RELOC_386_TLS_IE:
14082 case BFD_RELOC_386_TLS_GOTIE:
13ae64f3
JJ
14083 case BFD_RELOC_386_TLS_LE_32:
14084 case BFD_RELOC_386_TLS_LE:
67a4f2b7
AO
14085 case BFD_RELOC_386_TLS_GOTDESC:
14086 case BFD_RELOC_386_TLS_DESC_CALL:
bffbf940
JJ
14087 case BFD_RELOC_X86_64_TLSGD:
14088 case BFD_RELOC_X86_64_TLSLD:
14089 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 14090 case BFD_RELOC_X86_64_DTPOFF64:
bffbf940
JJ
14091 case BFD_RELOC_X86_64_GOTTPOFF:
14092 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113
JB
14093 case BFD_RELOC_X86_64_TPOFF64:
14094 case BFD_RELOC_X86_64_GOTOFF64:
14095 case BFD_RELOC_X86_64_GOTPC32:
7b81dfbb
AJ
14096 case BFD_RELOC_X86_64_GOT64:
14097 case BFD_RELOC_X86_64_GOTPCREL64:
14098 case BFD_RELOC_X86_64_GOTPC64:
14099 case BFD_RELOC_X86_64_GOTPLT64:
14100 case BFD_RELOC_X86_64_PLTOFF64:
67a4f2b7
AO
14101 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14102 case BFD_RELOC_X86_64_TLSDESC_CALL:
252b5132
RH
14103 case BFD_RELOC_RVA:
14104 case BFD_RELOC_VTABLE_ENTRY:
14105 case BFD_RELOC_VTABLE_INHERIT:
6482c264
NC
14106#ifdef TE_PE
14107 case BFD_RELOC_32_SECREL:
14108#endif
252b5132
RH
14109 code = fixp->fx_r_type;
14110 break;
dbbaec26
L
14111 case BFD_RELOC_X86_64_32S:
14112 if (!fixp->fx_pcrel)
14113 {
14114 /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32. */
14115 code = fixp->fx_r_type;
14116 break;
14117 }
1a0670f3 14118 /* Fall through. */
252b5132 14119 default:
93382f6d 14120 if (fixp->fx_pcrel)
252b5132 14121 {
93382f6d
AM
14122 switch (fixp->fx_size)
14123 {
14124 default:
b091f402
AM
14125 as_bad_where (fixp->fx_file, fixp->fx_line,
14126 _("can not do %d byte pc-relative relocation"),
14127 fixp->fx_size);
93382f6d
AM
14128 code = BFD_RELOC_32_PCREL;
14129 break;
14130 case 1: code = BFD_RELOC_8_PCREL; break;
14131 case 2: code = BFD_RELOC_16_PCREL; break;
d258b828 14132 case 4: code = BFD_RELOC_32_PCREL; break;
d6ab8113
JB
14133#ifdef BFD64
14134 case 8: code = BFD_RELOC_64_PCREL; break;
14135#endif
93382f6d
AM
14136 }
14137 }
14138 else
14139 {
14140 switch (fixp->fx_size)
14141 {
14142 default:
b091f402
AM
14143 as_bad_where (fixp->fx_file, fixp->fx_line,
14144 _("can not do %d byte relocation"),
14145 fixp->fx_size);
93382f6d
AM
14146 code = BFD_RELOC_32;
14147 break;
14148 case 1: code = BFD_RELOC_8; break;
14149 case 2: code = BFD_RELOC_16; break;
14150 case 4: code = BFD_RELOC_32; break;
937149dd 14151#ifdef BFD64
3e73aa7c 14152 case 8: code = BFD_RELOC_64; break;
937149dd 14153#endif
93382f6d 14154 }
252b5132
RH
14155 }
14156 break;
14157 }
252b5132 14158
d182319b
JB
14159 if ((code == BFD_RELOC_32
14160 || code == BFD_RELOC_32_PCREL
14161 || code == BFD_RELOC_X86_64_32S)
252b5132
RH
14162 && GOT_symbol
14163 && fixp->fx_addsy == GOT_symbol)
3e73aa7c 14164 {
4fa24527 14165 if (!object_64bit)
d6ab8113
JB
14166 code = BFD_RELOC_386_GOTPC;
14167 else
14168 code = BFD_RELOC_X86_64_GOTPC32;
3e73aa7c 14169 }
7b81dfbb
AJ
14170 if ((code == BFD_RELOC_64 || code == BFD_RELOC_64_PCREL)
14171 && GOT_symbol
14172 && fixp->fx_addsy == GOT_symbol)
14173 {
14174 code = BFD_RELOC_X86_64_GOTPC64;
14175 }
252b5132 14176
add39d23
TS
14177 rel = XNEW (arelent);
14178 rel->sym_ptr_ptr = XNEW (asymbol *);
49309057 14179 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
14180
14181 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
c87db184 14182
3e73aa7c
JH
14183 if (!use_rela_relocations)
14184 {
14185 /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
14186 vtable entry to be used in the relocation's section offset. */
14187 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
14188 rel->address = fixp->fx_offset;
fbeb56a4
DK
14189#if defined (OBJ_COFF) && defined (TE_PE)
14190 else if (fixp->fx_addsy && S_IS_WEAK (fixp->fx_addsy))
14191 rel->addend = fixp->fx_addnumber - (S_GET_VALUE (fixp->fx_addsy) * 2);
14192 else
14193#endif
c6682705 14194 rel->addend = 0;
3e73aa7c
JH
14195 }
14196 /* Use the rela in 64bit mode. */
252b5132 14197 else
3e73aa7c 14198 {
862be3fb
L
14199 if (disallow_64bit_reloc)
14200 switch (code)
14201 {
862be3fb
L
14202 case BFD_RELOC_X86_64_DTPOFF64:
14203 case BFD_RELOC_X86_64_TPOFF64:
14204 case BFD_RELOC_64_PCREL:
14205 case BFD_RELOC_X86_64_GOTOFF64:
14206 case BFD_RELOC_X86_64_GOT64:
14207 case BFD_RELOC_X86_64_GOTPCREL64:
14208 case BFD_RELOC_X86_64_GOTPC64:
14209 case BFD_RELOC_X86_64_GOTPLT64:
14210 case BFD_RELOC_X86_64_PLTOFF64:
14211 as_bad_where (fixp->fx_file, fixp->fx_line,
14212 _("cannot represent relocation type %s in x32 mode"),
14213 bfd_get_reloc_code_name (code));
14214 break;
14215 default:
14216 break;
14217 }
14218
062cd5e7
AS
14219 if (!fixp->fx_pcrel)
14220 rel->addend = fixp->fx_offset;
14221 else
14222 switch (code)
14223 {
14224 case BFD_RELOC_X86_64_PLT32:
14225 case BFD_RELOC_X86_64_GOT32:
14226 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
14227 case BFD_RELOC_X86_64_GOTPCRELX:
14228 case BFD_RELOC_X86_64_REX_GOTPCRELX:
bffbf940
JJ
14229 case BFD_RELOC_X86_64_TLSGD:
14230 case BFD_RELOC_X86_64_TLSLD:
14231 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7
AO
14232 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14233 case BFD_RELOC_X86_64_TLSDESC_CALL:
062cd5e7
AS
14234 rel->addend = fixp->fx_offset - fixp->fx_size;
14235 break;
14236 default:
14237 rel->addend = (section->vma
14238 - fixp->fx_size
14239 + fixp->fx_addnumber
14240 + md_pcrel_from (fixp));
14241 break;
14242 }
3e73aa7c
JH
14243 }
14244
252b5132
RH
14245 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
14246 if (rel->howto == NULL)
14247 {
14248 as_bad_where (fixp->fx_file, fixp->fx_line,
d0b47220 14249 _("cannot represent relocation type %s"),
252b5132
RH
14250 bfd_get_reloc_code_name (code));
14251 /* Set howto to a garbage value so that we can keep going. */
14252 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
9c2799c2 14253 gas_assert (rel->howto != NULL);
252b5132
RH
14254 }
14255
14256 return rel;
14257}
14258
ee86248c 14259#include "tc-i386-intel.c"
54cfded0 14260
a60de03c
JB
14261void
14262tc_x86_parse_to_dw2regnum (expressionS *exp)
54cfded0 14263{
a60de03c
JB
14264 int saved_naked_reg;
14265 char saved_register_dot;
54cfded0 14266
a60de03c
JB
14267 saved_naked_reg = allow_naked_reg;
14268 allow_naked_reg = 1;
14269 saved_register_dot = register_chars['.'];
14270 register_chars['.'] = '.';
14271 allow_pseudo_reg = 1;
14272 expression_and_evaluate (exp);
14273 allow_pseudo_reg = 0;
14274 register_chars['.'] = saved_register_dot;
14275 allow_naked_reg = saved_naked_reg;
14276
e96d56a1 14277 if (exp->X_op == O_register && exp->X_add_number >= 0)
54cfded0 14278 {
a60de03c
JB
14279 if ((addressT) exp->X_add_number < i386_regtab_size)
14280 {
14281 exp->X_op = O_constant;
14282 exp->X_add_number = i386_regtab[exp->X_add_number]
14283 .dw2_regnum[flag_code >> 1];
14284 }
14285 else
14286 exp->X_op = O_illegal;
54cfded0 14287 }
54cfded0
AM
14288}
14289
14290void
14291tc_x86_frame_initial_instructions (void)
14292{
a60de03c
JB
14293 static unsigned int sp_regno[2];
14294
14295 if (!sp_regno[flag_code >> 1])
14296 {
14297 char *saved_input = input_line_pointer;
14298 char sp[][4] = {"esp", "rsp"};
14299 expressionS exp;
a4447b93 14300
a60de03c
JB
14301 input_line_pointer = sp[flag_code >> 1];
14302 tc_x86_parse_to_dw2regnum (&exp);
9c2799c2 14303 gas_assert (exp.X_op == O_constant);
a60de03c
JB
14304 sp_regno[flag_code >> 1] = exp.X_add_number;
14305 input_line_pointer = saved_input;
14306 }
a4447b93 14307
61ff971f
L
14308 cfi_add_CFA_def_cfa (sp_regno[flag_code >> 1], -x86_cie_data_alignment);
14309 cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
54cfded0 14310}
d2b2c203 14311
d7921315
L
14312int
14313x86_dwarf2_addr_size (void)
14314{
14315#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
14316 if (x86_elf_abi == X86_64_X32_ABI)
14317 return 4;
14318#endif
14319 return bfd_arch_bits_per_address (stdoutput) / 8;
14320}
14321
d2b2c203
DJ
14322int
14323i386_elf_section_type (const char *str, size_t len)
14324{
14325 if (flag_code == CODE_64BIT
14326 && len == sizeof ("unwind") - 1
14327 && strncmp (str, "unwind", 6) == 0)
14328 return SHT_X86_64_UNWIND;
14329
14330 return -1;
14331}
bb41ade5 14332
ad5fec3b
EB
14333#ifdef TE_SOLARIS
14334void
14335i386_solaris_fix_up_eh_frame (segT sec)
14336{
14337 if (flag_code == CODE_64BIT)
14338 elf_section_type (sec) = SHT_X86_64_UNWIND;
14339}
14340#endif
14341
bb41ade5
AM
14342#ifdef TE_PE
14343void
14344tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
14345{
91d6fa6a 14346 expressionS exp;
bb41ade5 14347
91d6fa6a
NC
14348 exp.X_op = O_secrel;
14349 exp.X_add_symbol = symbol;
14350 exp.X_add_number = 0;
14351 emit_expr (&exp, size);
bb41ade5
AM
14352}
14353#endif
3b22753a
L
14354
14355#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14356/* For ELF on x86-64, add support for SHF_X86_64_LARGE. */
14357
01e1a5bc 14358bfd_vma
6d4af3c2 14359x86_64_section_letter (int letter, const char **ptr_msg)
3b22753a
L
14360{
14361 if (flag_code == CODE_64BIT)
14362 {
14363 if (letter == 'l')
14364 return SHF_X86_64_LARGE;
14365
8f3bae45 14366 *ptr_msg = _("bad .section directive: want a,l,w,x,M,S,G,T in string");
64e74474 14367 }
3b22753a 14368 else
8f3bae45 14369 *ptr_msg = _("bad .section directive: want a,w,x,M,S,G,T in string");
3b22753a
L
14370 return -1;
14371}
14372
01e1a5bc 14373bfd_vma
3b22753a
L
14374x86_64_section_word (char *str, size_t len)
14375{
08dedd66 14376 if (len == 5 && flag_code == CODE_64BIT && startswith (str, "large"))
3b22753a
L
14377 return SHF_X86_64_LARGE;
14378
14379 return -1;
14380}
14381
14382static void
14383handle_large_common (int small ATTRIBUTE_UNUSED)
14384{
14385 if (flag_code != CODE_64BIT)
14386 {
14387 s_comm_internal (0, elf_common_parse);
14388 as_warn (_(".largecomm supported only in 64bit mode, producing .comm"));
14389 }
14390 else
14391 {
14392 static segT lbss_section;
14393 asection *saved_com_section_ptr = elf_com_section_ptr;
14394 asection *saved_bss_section = bss_section;
14395
14396 if (lbss_section == NULL)
14397 {
14398 flagword applicable;
14399 segT seg = now_seg;
14400 subsegT subseg = now_subseg;
14401
14402 /* The .lbss section is for local .largecomm symbols. */
14403 lbss_section = subseg_new (".lbss", 0);
14404 applicable = bfd_applicable_section_flags (stdoutput);
fd361982 14405 bfd_set_section_flags (lbss_section, applicable & SEC_ALLOC);
3b22753a
L
14406 seg_info (lbss_section)->bss = 1;
14407
14408 subseg_set (seg, subseg);
14409 }
14410
14411 elf_com_section_ptr = &_bfd_elf_large_com_section;
14412 bss_section = lbss_section;
14413
14414 s_comm_internal (0, elf_common_parse);
14415
14416 elf_com_section_ptr = saved_com_section_ptr;
14417 bss_section = saved_bss_section;
14418 }
14419}
14420#endif /* OBJ_ELF || OBJ_MAYBE_ELF */
This page took 2.480697 seconds and 4 git commands to generate.