x86: add code comment on deprecated status of pseudo-suffixes
[deliverable/binutils-gdb.git] / gas / config / tc-i386.c
CommitLineData
b534c6d3 1/* tc-i386.c -- Assemble code for the Intel 80386
219d1afa 2 Copyright (C) 1989-2018 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
252b5132
RH
36#ifndef REGISTER_WARNINGS
37#define REGISTER_WARNINGS 1
38#endif
39
c3332e24 40#ifndef INFER_ADDR_PREFIX
eecb386c 41#define INFER_ADDR_PREFIX 1
c3332e24
AM
42#endif
43
29b0f896
AM
44#ifndef DEFAULT_ARCH
45#define DEFAULT_ARCH "i386"
246fcdee 46#endif
252b5132 47
edde18a5
AM
48#ifndef INLINE
49#if __GNUC__ >= 2
50#define INLINE __inline__
51#else
52#define INLINE
53#endif
54#endif
55
6305a203
L
56/* Prefixes will be emitted in the order defined below.
57 WAIT_PREFIX must be the first prefix since FWAIT is really is an
58 instruction, and so must come before any prefixes.
59 The preferred prefix order is SEG_PREFIX, ADDR_PREFIX, DATA_PREFIX,
42164a71 60 REP_PREFIX/HLE_PREFIX, LOCK_PREFIX. */
6305a203
L
61#define WAIT_PREFIX 0
62#define SEG_PREFIX 1
63#define ADDR_PREFIX 2
64#define DATA_PREFIX 3
c32fa91d 65#define REP_PREFIX 4
42164a71 66#define HLE_PREFIX REP_PREFIX
7e8b059b 67#define BND_PREFIX REP_PREFIX
c32fa91d 68#define LOCK_PREFIX 5
4e9ac44a
L
69#define REX_PREFIX 6 /* must come last. */
70#define MAX_PREFIXES 7 /* max prefixes per opcode */
6305a203
L
71
72/* we define the syntax here (modulo base,index,scale syntax) */
73#define REGISTER_PREFIX '%'
74#define IMMEDIATE_PREFIX '$'
75#define ABSOLUTE_PREFIX '*'
76
77/* these are the instruction mnemonic suffixes in AT&T syntax or
78 memory operand size in Intel syntax. */
79#define WORD_MNEM_SUFFIX 'w'
80#define BYTE_MNEM_SUFFIX 'b'
81#define SHORT_MNEM_SUFFIX 's'
82#define LONG_MNEM_SUFFIX 'l'
83#define QWORD_MNEM_SUFFIX 'q'
6305a203
L
84/* Intel Syntax. Use a non-ascii letter since since it never appears
85 in instructions. */
86#define LONG_DOUBLE_MNEM_SUFFIX '\1'
87
88#define END_OF_INSN '\0'
89
90/*
91 'templates' is for grouping together 'template' structures for opcodes
92 of the same name. This is only used for storing the insns in the grand
93 ole hash table of insns.
94 The templates themselves start at START and range up to (but not including)
95 END.
96 */
97typedef struct
98{
d3ce72d0
NC
99 const insn_template *start;
100 const insn_template *end;
6305a203
L
101}
102templates;
103
104/* 386 operand encoding bytes: see 386 book for details of this. */
105typedef struct
106{
107 unsigned int regmem; /* codes register or memory operand */
108 unsigned int reg; /* codes register operand (or extended opcode) */
109 unsigned int mode; /* how to interpret regmem & reg */
110}
111modrm_byte;
112
113/* x86-64 extension prefix. */
114typedef int rex_byte;
115
6305a203
L
116/* 386 opcode byte to code indirect addressing. */
117typedef struct
118{
119 unsigned base;
120 unsigned index;
121 unsigned scale;
122}
123sib_byte;
124
6305a203
L
125/* x86 arch names, types and features */
126typedef struct
127{
128 const char *name; /* arch name */
8a2c8fef 129 unsigned int len; /* arch string length */
6305a203
L
130 enum processor_type type; /* arch type */
131 i386_cpu_flags flags; /* cpu feature flags */
8a2c8fef 132 unsigned int skip; /* show_arch should skip this. */
6305a203
L
133}
134arch_entry;
135
293f5f65
L
136/* Used to turn off indicated flags. */
137typedef struct
138{
139 const char *name; /* arch name */
140 unsigned int len; /* arch string length */
141 i386_cpu_flags flags; /* cpu feature flags */
142}
143noarch_entry;
144
78f12dd3 145static void update_code_flag (int, int);
e3bb37b5
L
146static void set_code_flag (int);
147static void set_16bit_gcc_code_flag (int);
148static void set_intel_syntax (int);
1efbbeb4 149static void set_intel_mnemonic (int);
db51cc60 150static void set_allow_index_reg (int);
7bab8ab5 151static void set_check (int);
e3bb37b5 152static void set_cpu_arch (int);
6482c264 153#ifdef TE_PE
e3bb37b5 154static void pe_directive_secrel (int);
6482c264 155#endif
e3bb37b5
L
156static void signed_cons (int);
157static char *output_invalid (int c);
ee86248c
JB
158static int i386_finalize_immediate (segT, expressionS *, i386_operand_type,
159 const char *);
160static int i386_finalize_displacement (segT, expressionS *, i386_operand_type,
161 const char *);
a7619375 162static int i386_att_operand (char *);
e3bb37b5 163static int i386_intel_operand (char *, int);
ee86248c
JB
164static int i386_intel_simplify (expressionS *);
165static int i386_intel_parse_name (const char *, expressionS *);
e3bb37b5
L
166static const reg_entry *parse_register (char *, char **);
167static char *parse_insn (char *, char *);
168static char *parse_operands (char *, const char *);
169static void swap_operands (void);
4d456e3d 170static void swap_2_operands (int, int);
e3bb37b5
L
171static void optimize_imm (void);
172static void optimize_disp (void);
83b16ac6 173static const insn_template *match_template (char);
e3bb37b5
L
174static int check_string (void);
175static int process_suffix (void);
176static int check_byte_reg (void);
177static int check_long_reg (void);
178static int check_qword_reg (void);
179static int check_word_reg (void);
180static int finalize_imm (void);
181static int process_operands (void);
182static const seg_entry *build_modrm_byte (void);
183static void output_insn (void);
184static void output_imm (fragS *, offsetT);
185static void output_disp (fragS *, offsetT);
29b0f896 186#ifndef I386COFF
e3bb37b5 187static void s_bss (int);
252b5132 188#endif
17d4e2a2
L
189#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
190static void handle_large_common (int small ATTRIBUTE_UNUSED);
b4a3a7b4
L
191
192/* GNU_PROPERTY_X86_ISA_1_USED. */
193static unsigned int x86_isa_1_used;
194/* GNU_PROPERTY_X86_FEATURE_2_USED. */
195static unsigned int x86_feature_2_used;
196/* Generate x86 used ISA and feature properties. */
197static unsigned int x86_used_note = DEFAULT_X86_USED_NOTE;
17d4e2a2 198#endif
252b5132 199
a847613f 200static const char *default_arch = DEFAULT_ARCH;
3e73aa7c 201
43234a1e
L
202/* This struct describes rounding control and SAE in the instruction. */
203struct RC_Operation
204{
205 enum rc_type
206 {
207 rne = 0,
208 rd,
209 ru,
210 rz,
211 saeonly
212 } type;
213 int operand;
214};
215
216static struct RC_Operation rc_op;
217
218/* The struct describes masking, applied to OPERAND in the instruction.
219 MASK is a pointer to the corresponding mask register. ZEROING tells
220 whether merging or zeroing mask is used. */
221struct Mask_Operation
222{
223 const reg_entry *mask;
224 unsigned int zeroing;
225 /* The operand where this operation is associated. */
226 int operand;
227};
228
229static struct Mask_Operation mask_op;
230
231/* The struct describes broadcasting, applied to OPERAND. FACTOR is
232 broadcast factor. */
233struct Broadcast_Operation
234{
8e6e0792 235 /* Type of broadcast: {1to2}, {1to4}, {1to8}, or {1to16}. */
43234a1e
L
236 int type;
237
238 /* Index of broadcasted operand. */
239 int operand;
4a1b91ea
L
240
241 /* Number of bytes to broadcast. */
242 int bytes;
43234a1e
L
243};
244
245static struct Broadcast_Operation broadcast_op;
246
c0f3af97
L
247/* VEX prefix. */
248typedef struct
249{
43234a1e
L
250 /* VEX prefix is either 2 byte or 3 byte. EVEX is 4 byte. */
251 unsigned char bytes[4];
c0f3af97
L
252 unsigned int length;
253 /* Destination or source register specifier. */
254 const reg_entry *register_specifier;
255} vex_prefix;
256
252b5132 257/* 'md_assemble ()' gathers together information and puts it into a
47926f60 258 i386_insn. */
252b5132 259
520dc8e8
AM
260union i386_op
261 {
262 expressionS *disps;
263 expressionS *imms;
264 const reg_entry *regs;
265 };
266
a65babc9
L
267enum i386_error
268 {
86e026a4 269 operand_size_mismatch,
a65babc9
L
270 operand_type_mismatch,
271 register_type_mismatch,
272 number_of_operands_mismatch,
273 invalid_instruction_suffix,
274 bad_imm4,
a65babc9
L
275 unsupported_with_intel_mnemonic,
276 unsupported_syntax,
6c30d220
L
277 unsupported,
278 invalid_vsib_address,
7bab8ab5 279 invalid_vector_register_set,
43234a1e
L
280 unsupported_vector_index_register,
281 unsupported_broadcast,
43234a1e
L
282 broadcast_needed,
283 unsupported_masking,
284 mask_not_on_destination,
285 no_default_mask,
286 unsupported_rc_sae,
287 rc_sae_operand_not_last_imm,
288 invalid_register_operand,
a65babc9
L
289 };
290
252b5132
RH
291struct _i386_insn
292 {
47926f60 293 /* TM holds the template for the insn were currently assembling. */
d3ce72d0 294 insn_template tm;
252b5132 295
7d5e4556
L
296 /* SUFFIX holds the instruction size suffix for byte, word, dword
297 or qword, if given. */
252b5132
RH
298 char suffix;
299
47926f60 300 /* OPERANDS gives the number of given operands. */
252b5132
RH
301 unsigned int operands;
302
303 /* REG_OPERANDS, DISP_OPERANDS, MEM_OPERANDS, IMM_OPERANDS give the number
304 of given register, displacement, memory operands and immediate
47926f60 305 operands. */
252b5132
RH
306 unsigned int reg_operands, disp_operands, mem_operands, imm_operands;
307
308 /* TYPES [i] is the type (see above #defines) which tells us how to
520dc8e8 309 use OP[i] for the corresponding operand. */
40fb9820 310 i386_operand_type types[MAX_OPERANDS];
252b5132 311
520dc8e8
AM
312 /* Displacement expression, immediate expression, or register for each
313 operand. */
314 union i386_op op[MAX_OPERANDS];
252b5132 315
3e73aa7c
JH
316 /* Flags for operands. */
317 unsigned int flags[MAX_OPERANDS];
318#define Operand_PCrel 1
c48dadc9 319#define Operand_Mem 2
3e73aa7c 320
252b5132 321 /* Relocation type for operand */
f86103b7 322 enum bfd_reloc_code_real reloc[MAX_OPERANDS];
252b5132 323
252b5132
RH
324 /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
325 the base index byte below. */
326 const reg_entry *base_reg;
327 const reg_entry *index_reg;
328 unsigned int log2_scale_factor;
329
330 /* SEG gives the seg_entries of this insn. They are zero unless
47926f60 331 explicit segment overrides are given. */
ce8a8b2f 332 const seg_entry *seg[2];
252b5132 333
8325cc63
JB
334 /* Copied first memory operand string, for re-checking. */
335 char *memop1_string;
336
252b5132
RH
337 /* PREFIX holds all the given prefix opcodes (usually null).
338 PREFIXES is the number of prefix opcodes. */
339 unsigned int prefixes;
340 unsigned char prefix[MAX_PREFIXES];
341
b4a3a7b4
L
342 /* Has MMX register operands. */
343 bfd_boolean has_regmmx;
344
345 /* Has XMM register operands. */
346 bfd_boolean has_regxmm;
347
348 /* Has YMM register operands. */
349 bfd_boolean has_regymm;
350
351 /* Has ZMM register operands. */
352 bfd_boolean has_regzmm;
353
252b5132 354 /* RM and SIB are the modrm byte and the sib byte where the
c1e679ec 355 addressing modes of this insn are encoded. */
252b5132 356 modrm_byte rm;
3e73aa7c 357 rex_byte rex;
43234a1e 358 rex_byte vrex;
252b5132 359 sib_byte sib;
c0f3af97 360 vex_prefix vex;
b6169b20 361
43234a1e
L
362 /* Masking attributes. */
363 struct Mask_Operation *mask;
364
365 /* Rounding control and SAE attributes. */
366 struct RC_Operation *rounding;
367
368 /* Broadcasting attributes. */
369 struct Broadcast_Operation *broadcast;
370
371 /* Compressed disp8*N attribute. */
372 unsigned int memshift;
373
86fa6981
L
374 /* Prefer load or store in encoding. */
375 enum
376 {
377 dir_encoding_default = 0,
378 dir_encoding_load,
379 dir_encoding_store
380 } dir_encoding;
891edac4 381
a501d77e
L
382 /* Prefer 8bit or 32bit displacement in encoding. */
383 enum
384 {
385 disp_encoding_default = 0,
386 disp_encoding_8bit,
387 disp_encoding_32bit
388 } disp_encoding;
f8a5c266 389
6b6b6807
L
390 /* Prefer the REX byte in encoding. */
391 bfd_boolean rex_encoding;
392
b6f8c7c4
L
393 /* Disable instruction size optimization. */
394 bfd_boolean no_optimize;
395
86fa6981
L
396 /* How to encode vector instructions. */
397 enum
398 {
399 vex_encoding_default = 0,
400 vex_encoding_vex2,
401 vex_encoding_vex3,
402 vex_encoding_evex
403 } vec_encoding;
404
d5de92cf
L
405 /* REP prefix. */
406 const char *rep_prefix;
407
165de32a
L
408 /* HLE prefix. */
409 const char *hle_prefix;
42164a71 410
7e8b059b
L
411 /* Have BND prefix. */
412 const char *bnd_prefix;
413
04ef582a
L
414 /* Have NOTRACK prefix. */
415 const char *notrack_prefix;
416
891edac4 417 /* Error message. */
a65babc9 418 enum i386_error error;
252b5132
RH
419 };
420
421typedef struct _i386_insn i386_insn;
422
43234a1e
L
423/* Link RC type with corresponding string, that'll be looked for in
424 asm. */
425struct RC_name
426{
427 enum rc_type type;
428 const char *name;
429 unsigned int len;
430};
431
432static const struct RC_name RC_NamesTable[] =
433{
434 { rne, STRING_COMMA_LEN ("rn-sae") },
435 { rd, STRING_COMMA_LEN ("rd-sae") },
436 { ru, STRING_COMMA_LEN ("ru-sae") },
437 { rz, STRING_COMMA_LEN ("rz-sae") },
438 { saeonly, STRING_COMMA_LEN ("sae") },
439};
440
252b5132
RH
441/* List of chars besides those in app.c:symbol_chars that can start an
442 operand. Used to prevent the scrubber eating vital white-space. */
86fa6981 443const char extra_symbol_chars[] = "*%-([{}"
252b5132 444#ifdef LEX_AT
32137342
NC
445 "@"
446#endif
447#ifdef LEX_QM
448 "?"
252b5132 449#endif
32137342 450 ;
252b5132 451
29b0f896
AM
452#if (defined (TE_I386AIX) \
453 || ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
3896cfd5 454 && !defined (TE_GNU) \
29b0f896 455 && !defined (TE_LINUX) \
8d63c93e 456 && !defined (TE_NACL) \
29b0f896 457 && !defined (TE_FreeBSD) \
5b806d27 458 && !defined (TE_DragonFly) \
29b0f896 459 && !defined (TE_NetBSD)))
252b5132 460/* This array holds the chars that always start a comment. If the
b3b91714
AM
461 pre-processor is disabled, these aren't very useful. The option
462 --divide will remove '/' from this list. */
463const char *i386_comment_chars = "#/";
464#define SVR4_COMMENT_CHARS 1
252b5132 465#define PREFIX_SEPARATOR '\\'
252b5132 466
b3b91714
AM
467#else
468const char *i386_comment_chars = "#";
469#define PREFIX_SEPARATOR '/'
470#endif
471
252b5132
RH
472/* This array holds the chars that only start a comment at the beginning of
473 a line. If the line seems to have the form '# 123 filename'
ce8a8b2f
AM
474 .line and .file directives will appear in the pre-processed output.
475 Note that input_file.c hand checks for '#' at the beginning of the
252b5132 476 first line of the input file. This is because the compiler outputs
ce8a8b2f
AM
477 #NO_APP at the beginning of its output.
478 Also note that comments started like this one will always work if
252b5132 479 '/' isn't otherwise defined. */
b3b91714 480const char line_comment_chars[] = "#/";
252b5132 481
63a0b638 482const char line_separator_chars[] = ";";
252b5132 483
ce8a8b2f
AM
484/* Chars that can be used to separate mant from exp in floating point
485 nums. */
252b5132
RH
486const char EXP_CHARS[] = "eE";
487
ce8a8b2f
AM
488/* Chars that mean this number is a floating point constant
489 As in 0f12.456
490 or 0d1.2345e12. */
252b5132
RH
491const char FLT_CHARS[] = "fFdDxX";
492
ce8a8b2f 493/* Tables for lexical analysis. */
252b5132
RH
494static char mnemonic_chars[256];
495static char register_chars[256];
496static char operand_chars[256];
497static char identifier_chars[256];
498static char digit_chars[256];
499
ce8a8b2f 500/* Lexical macros. */
252b5132
RH
501#define is_mnemonic_char(x) (mnemonic_chars[(unsigned char) x])
502#define is_operand_char(x) (operand_chars[(unsigned char) x])
503#define is_register_char(x) (register_chars[(unsigned char) x])
504#define is_space_char(x) ((x) == ' ')
505#define is_identifier_char(x) (identifier_chars[(unsigned char) x])
506#define is_digit_char(x) (digit_chars[(unsigned char) x])
507
0234cb7c 508/* All non-digit non-letter characters that may occur in an operand. */
252b5132
RH
509static char operand_special_chars[] = "%$-+(,)*._~/<>|&^!:[@]";
510
511/* md_assemble() always leaves the strings it's passed unaltered. To
512 effect this we maintain a stack of saved characters that we've smashed
513 with '\0's (indicating end of strings for various sub-fields of the
47926f60 514 assembler instruction). */
252b5132 515static char save_stack[32];
ce8a8b2f 516static char *save_stack_p;
252b5132
RH
517#define END_STRING_AND_SAVE(s) \
518 do { *save_stack_p++ = *(s); *(s) = '\0'; } while (0)
519#define RESTORE_END_STRING(s) \
520 do { *(s) = *--save_stack_p; } while (0)
521
47926f60 522/* The instruction we're assembling. */
252b5132
RH
523static i386_insn i;
524
525/* Possible templates for current insn. */
526static const templates *current_templates;
527
31b2323c
L
528/* Per instruction expressionS buffers: max displacements & immediates. */
529static expressionS disp_expressions[MAX_MEMORY_OPERANDS];
530static expressionS im_expressions[MAX_IMMEDIATE_OPERANDS];
252b5132 531
47926f60 532/* Current operand we are working on. */
ee86248c 533static int this_operand = -1;
252b5132 534
3e73aa7c
JH
535/* We support four different modes. FLAG_CODE variable is used to distinguish
536 these. */
537
538enum flag_code {
539 CODE_32BIT,
540 CODE_16BIT,
541 CODE_64BIT };
542
543static enum flag_code flag_code;
4fa24527 544static unsigned int object_64bit;
862be3fb 545static unsigned int disallow_64bit_reloc;
3e73aa7c
JH
546static int use_rela_relocations = 0;
547
7af8ed2d
NC
548#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
549 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
550 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
551
351f65ca
L
552/* The ELF ABI to use. */
553enum x86_elf_abi
554{
555 I386_ABI,
7f56bc95
L
556 X86_64_ABI,
557 X86_64_X32_ABI
351f65ca
L
558};
559
560static enum x86_elf_abi x86_elf_abi = I386_ABI;
7af8ed2d 561#endif
351f65ca 562
167ad85b
TG
563#if defined (TE_PE) || defined (TE_PEP)
564/* Use big object file format. */
565static int use_big_obj = 0;
566#endif
567
8dcea932
L
568#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
569/* 1 if generating code for a shared library. */
570static int shared = 0;
571#endif
572
47926f60
KH
573/* 1 for intel syntax,
574 0 if att syntax. */
575static int intel_syntax = 0;
252b5132 576
e89c5eaa
L
577/* 1 for Intel64 ISA,
578 0 if AMD64 ISA. */
579static int intel64;
580
1efbbeb4
L
581/* 1 for intel mnemonic,
582 0 if att mnemonic. */
583static int intel_mnemonic = !SYSV386_COMPAT;
584
a60de03c
JB
585/* 1 if pseudo registers are permitted. */
586static int allow_pseudo_reg = 0;
587
47926f60
KH
588/* 1 if register prefix % not required. */
589static int allow_naked_reg = 0;
252b5132 590
33eaf5de 591/* 1 if the assembler should add BND prefix for all control-transferring
7e8b059b
L
592 instructions supporting it, even if this prefix wasn't specified
593 explicitly. */
594static int add_bnd_prefix = 0;
595
ba104c83 596/* 1 if pseudo index register, eiz/riz, is allowed . */
db51cc60
L
597static int allow_index_reg = 0;
598
d022bddd
IT
599/* 1 if the assembler should ignore LOCK prefix, even if it was
600 specified explicitly. */
601static int omit_lock_prefix = 0;
602
e4e00185
AS
603/* 1 if the assembler should encode lfence, mfence, and sfence as
604 "lock addl $0, (%{re}sp)". */
605static int avoid_fence = 0;
606
0cb4071e
L
607/* 1 if the assembler should generate relax relocations. */
608
609static int generate_relax_relocations
610 = DEFAULT_GENERATE_X86_RELAX_RELOCATIONS;
611
7bab8ab5 612static enum check_kind
daf50ae7 613 {
7bab8ab5
JB
614 check_none = 0,
615 check_warning,
616 check_error
daf50ae7 617 }
7bab8ab5 618sse_check, operand_check = check_warning;
daf50ae7 619
b6f8c7c4
L
620/* Optimization:
621 1. Clear the REX_W bit with register operand if possible.
622 2. Above plus use 128bit vector instruction to clear the full vector
623 register.
624 */
625static int optimize = 0;
626
627/* Optimization:
628 1. Clear the REX_W bit with register operand if possible.
629 2. Above plus use 128bit vector instruction to clear the full vector
630 register.
631 3. Above plus optimize "test{q,l,w} $imm8,%r{64,32,16}" to
632 "testb $imm7,%r8".
633 */
634static int optimize_for_space = 0;
635
2ca3ace5
L
636/* Register prefix used for error message. */
637static const char *register_prefix = "%";
638
47926f60
KH
639/* Used in 16 bit gcc mode to add an l suffix to call, ret, enter,
640 leave, push, and pop instructions so that gcc has the same stack
641 frame as in 32 bit mode. */
642static char stackop_size = '\0';
eecb386c 643
12b55ccc
L
644/* Non-zero to optimize code alignment. */
645int optimize_align_code = 1;
646
47926f60
KH
647/* Non-zero to quieten some warnings. */
648static int quiet_warnings = 0;
a38cf1db 649
47926f60
KH
650/* CPU name. */
651static const char *cpu_arch_name = NULL;
6305a203 652static char *cpu_sub_arch_name = NULL;
a38cf1db 653
47926f60 654/* CPU feature flags. */
40fb9820
L
655static i386_cpu_flags cpu_arch_flags = CPU_UNKNOWN_FLAGS;
656
ccc9c027
L
657/* If we have selected a cpu we are generating instructions for. */
658static int cpu_arch_tune_set = 0;
659
9103f4f4 660/* Cpu we are generating instructions for. */
fbf3f584 661enum processor_type cpu_arch_tune = PROCESSOR_UNKNOWN;
9103f4f4
L
662
663/* CPU feature flags of cpu we are generating instructions for. */
40fb9820 664static i386_cpu_flags cpu_arch_tune_flags;
9103f4f4 665
ccc9c027 666/* CPU instruction set architecture used. */
fbf3f584 667enum processor_type cpu_arch_isa = PROCESSOR_UNKNOWN;
ccc9c027 668
9103f4f4 669/* CPU feature flags of instruction set architecture used. */
fbf3f584 670i386_cpu_flags cpu_arch_isa_flags;
9103f4f4 671
fddf5b5b
AM
672/* If set, conditional jumps are not automatically promoted to handle
673 larger than a byte offset. */
674static unsigned int no_cond_jump_promotion = 0;
675
c0f3af97
L
676/* Encode SSE instructions with VEX prefix. */
677static unsigned int sse2avx;
678
539f890d
L
679/* Encode scalar AVX instructions with specific vector length. */
680static enum
681 {
682 vex128 = 0,
683 vex256
684 } avxscalar;
685
43234a1e
L
686/* Encode scalar EVEX LIG instructions with specific vector length. */
687static enum
688 {
689 evexl128 = 0,
690 evexl256,
691 evexl512
692 } evexlig;
693
694/* Encode EVEX WIG instructions with specific evex.w. */
695static enum
696 {
697 evexw0 = 0,
698 evexw1
699 } evexwig;
700
d3d3c6db
IT
701/* Value to encode in EVEX RC bits, for SAE-only instructions. */
702static enum rc_type evexrcig = rne;
703
29b0f896 704/* Pre-defined "_GLOBAL_OFFSET_TABLE_". */
87c245cc 705static symbolS *GOT_symbol;
29b0f896 706
a4447b93
RH
707/* The dwarf2 return column, adjusted for 32 or 64 bit. */
708unsigned int x86_dwarf2_return_column;
709
710/* The dwarf2 data alignment, adjusted for 32 or 64 bit. */
711int x86_cie_data_alignment;
712
252b5132 713/* Interface to relax_segment.
fddf5b5b
AM
714 There are 3 major relax states for 386 jump insns because the
715 different types of jumps add different sizes to frags when we're
716 figuring out what sort of jump to choose to reach a given label. */
252b5132 717
47926f60 718/* Types. */
93c2a809
AM
719#define UNCOND_JUMP 0
720#define COND_JUMP 1
721#define COND_JUMP86 2
fddf5b5b 722
47926f60 723/* Sizes. */
252b5132
RH
724#define CODE16 1
725#define SMALL 0
29b0f896 726#define SMALL16 (SMALL | CODE16)
252b5132 727#define BIG 2
29b0f896 728#define BIG16 (BIG | CODE16)
252b5132
RH
729
730#ifndef INLINE
731#ifdef __GNUC__
732#define INLINE __inline__
733#else
734#define INLINE
735#endif
736#endif
737
fddf5b5b
AM
738#define ENCODE_RELAX_STATE(type, size) \
739 ((relax_substateT) (((type) << 2) | (size)))
740#define TYPE_FROM_RELAX_STATE(s) \
741 ((s) >> 2)
742#define DISP_SIZE_FROM_RELAX_STATE(s) \
743 ((((s) & 3) == BIG ? 4 : (((s) & 3) == BIG16 ? 2 : 1)))
252b5132
RH
744
745/* This table is used by relax_frag to promote short jumps to long
746 ones where necessary. SMALL (short) jumps may be promoted to BIG
747 (32 bit long) ones, and SMALL16 jumps to BIG16 (16 bit long). We
748 don't allow a short jump in a 32 bit code segment to be promoted to
749 a 16 bit offset jump because it's slower (requires data size
750 prefix), and doesn't work, unless the destination is in the bottom
751 64k of the code segment (The top 16 bits of eip are zeroed). */
752
753const relax_typeS md_relax_table[] =
754{
24eab124
AM
755 /* The fields are:
756 1) most positive reach of this state,
757 2) most negative reach of this state,
93c2a809 758 3) how many bytes this mode will have in the variable part of the frag
ce8a8b2f 759 4) which index into the table to try if we can't fit into this one. */
252b5132 760
fddf5b5b 761 /* UNCOND_JUMP states. */
93c2a809
AM
762 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG)},
763 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16)},
764 /* dword jmp adds 4 bytes to frag:
765 0 extra opcode bytes, 4 displacement bytes. */
252b5132 766 {0, 0, 4, 0},
93c2a809
AM
767 /* word jmp adds 2 byte2 to frag:
768 0 extra opcode bytes, 2 displacement bytes. */
252b5132
RH
769 {0, 0, 2, 0},
770
93c2a809
AM
771 /* COND_JUMP states. */
772 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG)},
773 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG16)},
774 /* dword conditionals adds 5 bytes to frag:
775 1 extra opcode byte, 4 displacement bytes. */
776 {0, 0, 5, 0},
fddf5b5b 777 /* word conditionals add 3 bytes to frag:
93c2a809
AM
778 1 extra opcode byte, 2 displacement bytes. */
779 {0, 0, 3, 0},
780
781 /* COND_JUMP86 states. */
782 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG)},
783 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG16)},
784 /* dword conditionals adds 5 bytes to frag:
785 1 extra opcode byte, 4 displacement bytes. */
786 {0, 0, 5, 0},
787 /* word conditionals add 4 bytes to frag:
788 1 displacement byte and a 3 byte long branch insn. */
789 {0, 0, 4, 0}
252b5132
RH
790};
791
9103f4f4
L
792static const arch_entry cpu_arch[] =
793{
89507696
JB
794 /* Do not replace the first two entries - i386_target_format()
795 relies on them being there in this order. */
8a2c8fef 796 { STRING_COMMA_LEN ("generic32"), PROCESSOR_GENERIC32,
293f5f65 797 CPU_GENERIC32_FLAGS, 0 },
8a2c8fef 798 { STRING_COMMA_LEN ("generic64"), PROCESSOR_GENERIC64,
293f5f65 799 CPU_GENERIC64_FLAGS, 0 },
8a2c8fef 800 { STRING_COMMA_LEN ("i8086"), PROCESSOR_UNKNOWN,
293f5f65 801 CPU_NONE_FLAGS, 0 },
8a2c8fef 802 { STRING_COMMA_LEN ("i186"), PROCESSOR_UNKNOWN,
293f5f65 803 CPU_I186_FLAGS, 0 },
8a2c8fef 804 { STRING_COMMA_LEN ("i286"), PROCESSOR_UNKNOWN,
293f5f65 805 CPU_I286_FLAGS, 0 },
8a2c8fef 806 { STRING_COMMA_LEN ("i386"), PROCESSOR_I386,
293f5f65 807 CPU_I386_FLAGS, 0 },
8a2c8fef 808 { STRING_COMMA_LEN ("i486"), PROCESSOR_I486,
293f5f65 809 CPU_I486_FLAGS, 0 },
8a2c8fef 810 { STRING_COMMA_LEN ("i586"), PROCESSOR_PENTIUM,
293f5f65 811 CPU_I586_FLAGS, 0 },
8a2c8fef 812 { STRING_COMMA_LEN ("i686"), PROCESSOR_PENTIUMPRO,
293f5f65 813 CPU_I686_FLAGS, 0 },
8a2c8fef 814 { STRING_COMMA_LEN ("pentium"), PROCESSOR_PENTIUM,
293f5f65 815 CPU_I586_FLAGS, 0 },
8a2c8fef 816 { STRING_COMMA_LEN ("pentiumpro"), PROCESSOR_PENTIUMPRO,
293f5f65 817 CPU_PENTIUMPRO_FLAGS, 0 },
8a2c8fef 818 { STRING_COMMA_LEN ("pentiumii"), PROCESSOR_PENTIUMPRO,
293f5f65 819 CPU_P2_FLAGS, 0 },
8a2c8fef 820 { STRING_COMMA_LEN ("pentiumiii"),PROCESSOR_PENTIUMPRO,
293f5f65 821 CPU_P3_FLAGS, 0 },
8a2c8fef 822 { STRING_COMMA_LEN ("pentium4"), PROCESSOR_PENTIUM4,
293f5f65 823 CPU_P4_FLAGS, 0 },
8a2c8fef 824 { STRING_COMMA_LEN ("prescott"), PROCESSOR_NOCONA,
293f5f65 825 CPU_CORE_FLAGS, 0 },
8a2c8fef 826 { STRING_COMMA_LEN ("nocona"), PROCESSOR_NOCONA,
293f5f65 827 CPU_NOCONA_FLAGS, 0 },
8a2c8fef 828 { STRING_COMMA_LEN ("yonah"), PROCESSOR_CORE,
293f5f65 829 CPU_CORE_FLAGS, 1 },
8a2c8fef 830 { STRING_COMMA_LEN ("core"), PROCESSOR_CORE,
293f5f65 831 CPU_CORE_FLAGS, 0 },
8a2c8fef 832 { STRING_COMMA_LEN ("merom"), PROCESSOR_CORE2,
293f5f65 833 CPU_CORE2_FLAGS, 1 },
8a2c8fef 834 { STRING_COMMA_LEN ("core2"), PROCESSOR_CORE2,
293f5f65 835 CPU_CORE2_FLAGS, 0 },
8a2c8fef 836 { STRING_COMMA_LEN ("corei7"), PROCESSOR_COREI7,
293f5f65 837 CPU_COREI7_FLAGS, 0 },
8a2c8fef 838 { STRING_COMMA_LEN ("l1om"), PROCESSOR_L1OM,
293f5f65 839 CPU_L1OM_FLAGS, 0 },
7a9068fe 840 { STRING_COMMA_LEN ("k1om"), PROCESSOR_K1OM,
293f5f65 841 CPU_K1OM_FLAGS, 0 },
81486035 842 { STRING_COMMA_LEN ("iamcu"), PROCESSOR_IAMCU,
293f5f65 843 CPU_IAMCU_FLAGS, 0 },
8a2c8fef 844 { STRING_COMMA_LEN ("k6"), PROCESSOR_K6,
293f5f65 845 CPU_K6_FLAGS, 0 },
8a2c8fef 846 { STRING_COMMA_LEN ("k6_2"), PROCESSOR_K6,
293f5f65 847 CPU_K6_2_FLAGS, 0 },
8a2c8fef 848 { STRING_COMMA_LEN ("athlon"), PROCESSOR_ATHLON,
293f5f65 849 CPU_ATHLON_FLAGS, 0 },
8a2c8fef 850 { STRING_COMMA_LEN ("sledgehammer"), PROCESSOR_K8,
293f5f65 851 CPU_K8_FLAGS, 1 },
8a2c8fef 852 { STRING_COMMA_LEN ("opteron"), PROCESSOR_K8,
293f5f65 853 CPU_K8_FLAGS, 0 },
8a2c8fef 854 { STRING_COMMA_LEN ("k8"), PROCESSOR_K8,
293f5f65 855 CPU_K8_FLAGS, 0 },
8a2c8fef 856 { STRING_COMMA_LEN ("amdfam10"), PROCESSOR_AMDFAM10,
293f5f65 857 CPU_AMDFAM10_FLAGS, 0 },
8aedb9fe 858 { STRING_COMMA_LEN ("bdver1"), PROCESSOR_BD,
293f5f65 859 CPU_BDVER1_FLAGS, 0 },
8aedb9fe 860 { STRING_COMMA_LEN ("bdver2"), PROCESSOR_BD,
293f5f65 861 CPU_BDVER2_FLAGS, 0 },
5e5c50d3 862 { STRING_COMMA_LEN ("bdver3"), PROCESSOR_BD,
293f5f65 863 CPU_BDVER3_FLAGS, 0 },
c7b0bd56 864 { STRING_COMMA_LEN ("bdver4"), PROCESSOR_BD,
293f5f65 865 CPU_BDVER4_FLAGS, 0 },
029f3522 866 { STRING_COMMA_LEN ("znver1"), PROCESSOR_ZNVER,
293f5f65 867 CPU_ZNVER1_FLAGS, 0 },
a9660a6f
AP
868 { STRING_COMMA_LEN ("znver2"), PROCESSOR_ZNVER,
869 CPU_ZNVER2_FLAGS, 0 },
7b458c12 870 { STRING_COMMA_LEN ("btver1"), PROCESSOR_BT,
293f5f65 871 CPU_BTVER1_FLAGS, 0 },
7b458c12 872 { STRING_COMMA_LEN ("btver2"), PROCESSOR_BT,
293f5f65 873 CPU_BTVER2_FLAGS, 0 },
8a2c8fef 874 { STRING_COMMA_LEN (".8087"), PROCESSOR_UNKNOWN,
293f5f65 875 CPU_8087_FLAGS, 0 },
8a2c8fef 876 { STRING_COMMA_LEN (".287"), PROCESSOR_UNKNOWN,
293f5f65 877 CPU_287_FLAGS, 0 },
8a2c8fef 878 { STRING_COMMA_LEN (".387"), PROCESSOR_UNKNOWN,
293f5f65 879 CPU_387_FLAGS, 0 },
1848e567
L
880 { STRING_COMMA_LEN (".687"), PROCESSOR_UNKNOWN,
881 CPU_687_FLAGS, 0 },
d871f3f4
L
882 { STRING_COMMA_LEN (".cmov"), PROCESSOR_UNKNOWN,
883 CPU_CMOV_FLAGS, 0 },
884 { STRING_COMMA_LEN (".fxsr"), PROCESSOR_UNKNOWN,
885 CPU_FXSR_FLAGS, 0 },
8a2c8fef 886 { STRING_COMMA_LEN (".mmx"), PROCESSOR_UNKNOWN,
293f5f65 887 CPU_MMX_FLAGS, 0 },
8a2c8fef 888 { STRING_COMMA_LEN (".sse"), PROCESSOR_UNKNOWN,
293f5f65 889 CPU_SSE_FLAGS, 0 },
8a2c8fef 890 { STRING_COMMA_LEN (".sse2"), PROCESSOR_UNKNOWN,
293f5f65 891 CPU_SSE2_FLAGS, 0 },
8a2c8fef 892 { STRING_COMMA_LEN (".sse3"), PROCESSOR_UNKNOWN,
293f5f65 893 CPU_SSE3_FLAGS, 0 },
8a2c8fef 894 { STRING_COMMA_LEN (".ssse3"), PROCESSOR_UNKNOWN,
293f5f65 895 CPU_SSSE3_FLAGS, 0 },
8a2c8fef 896 { STRING_COMMA_LEN (".sse4.1"), PROCESSOR_UNKNOWN,
293f5f65 897 CPU_SSE4_1_FLAGS, 0 },
8a2c8fef 898 { STRING_COMMA_LEN (".sse4.2"), PROCESSOR_UNKNOWN,
293f5f65 899 CPU_SSE4_2_FLAGS, 0 },
8a2c8fef 900 { STRING_COMMA_LEN (".sse4"), PROCESSOR_UNKNOWN,
293f5f65 901 CPU_SSE4_2_FLAGS, 0 },
8a2c8fef 902 { STRING_COMMA_LEN (".avx"), PROCESSOR_UNKNOWN,
293f5f65 903 CPU_AVX_FLAGS, 0 },
6c30d220 904 { STRING_COMMA_LEN (".avx2"), PROCESSOR_UNKNOWN,
293f5f65 905 CPU_AVX2_FLAGS, 0 },
43234a1e 906 { STRING_COMMA_LEN (".avx512f"), PROCESSOR_UNKNOWN,
293f5f65 907 CPU_AVX512F_FLAGS, 0 },
43234a1e 908 { STRING_COMMA_LEN (".avx512cd"), PROCESSOR_UNKNOWN,
293f5f65 909 CPU_AVX512CD_FLAGS, 0 },
43234a1e 910 { STRING_COMMA_LEN (".avx512er"), PROCESSOR_UNKNOWN,
293f5f65 911 CPU_AVX512ER_FLAGS, 0 },
43234a1e 912 { STRING_COMMA_LEN (".avx512pf"), PROCESSOR_UNKNOWN,
293f5f65 913 CPU_AVX512PF_FLAGS, 0 },
1dfc6506 914 { STRING_COMMA_LEN (".avx512dq"), PROCESSOR_UNKNOWN,
293f5f65 915 CPU_AVX512DQ_FLAGS, 0 },
1dfc6506 916 { STRING_COMMA_LEN (".avx512bw"), PROCESSOR_UNKNOWN,
293f5f65 917 CPU_AVX512BW_FLAGS, 0 },
1dfc6506 918 { STRING_COMMA_LEN (".avx512vl"), PROCESSOR_UNKNOWN,
293f5f65 919 CPU_AVX512VL_FLAGS, 0 },
8a2c8fef 920 { STRING_COMMA_LEN (".vmx"), PROCESSOR_UNKNOWN,
293f5f65 921 CPU_VMX_FLAGS, 0 },
8729a6f6 922 { STRING_COMMA_LEN (".vmfunc"), PROCESSOR_UNKNOWN,
293f5f65 923 CPU_VMFUNC_FLAGS, 0 },
8a2c8fef 924 { STRING_COMMA_LEN (".smx"), PROCESSOR_UNKNOWN,
293f5f65 925 CPU_SMX_FLAGS, 0 },
8a2c8fef 926 { STRING_COMMA_LEN (".xsave"), PROCESSOR_UNKNOWN,
293f5f65 927 CPU_XSAVE_FLAGS, 0 },
c7b8aa3a 928 { STRING_COMMA_LEN (".xsaveopt"), PROCESSOR_UNKNOWN,
293f5f65 929 CPU_XSAVEOPT_FLAGS, 0 },
1dfc6506 930 { STRING_COMMA_LEN (".xsavec"), PROCESSOR_UNKNOWN,
293f5f65 931 CPU_XSAVEC_FLAGS, 0 },
1dfc6506 932 { STRING_COMMA_LEN (".xsaves"), PROCESSOR_UNKNOWN,
293f5f65 933 CPU_XSAVES_FLAGS, 0 },
8a2c8fef 934 { STRING_COMMA_LEN (".aes"), PROCESSOR_UNKNOWN,
293f5f65 935 CPU_AES_FLAGS, 0 },
8a2c8fef 936 { STRING_COMMA_LEN (".pclmul"), PROCESSOR_UNKNOWN,
293f5f65 937 CPU_PCLMUL_FLAGS, 0 },
8a2c8fef 938 { STRING_COMMA_LEN (".clmul"), PROCESSOR_UNKNOWN,
293f5f65 939 CPU_PCLMUL_FLAGS, 1 },
c7b8aa3a 940 { STRING_COMMA_LEN (".fsgsbase"), PROCESSOR_UNKNOWN,
293f5f65 941 CPU_FSGSBASE_FLAGS, 0 },
c7b8aa3a 942 { STRING_COMMA_LEN (".rdrnd"), PROCESSOR_UNKNOWN,
293f5f65 943 CPU_RDRND_FLAGS, 0 },
c7b8aa3a 944 { STRING_COMMA_LEN (".f16c"), PROCESSOR_UNKNOWN,
293f5f65 945 CPU_F16C_FLAGS, 0 },
6c30d220 946 { STRING_COMMA_LEN (".bmi2"), PROCESSOR_UNKNOWN,
293f5f65 947 CPU_BMI2_FLAGS, 0 },
8a2c8fef 948 { STRING_COMMA_LEN (".fma"), PROCESSOR_UNKNOWN,
293f5f65 949 CPU_FMA_FLAGS, 0 },
8a2c8fef 950 { STRING_COMMA_LEN (".fma4"), PROCESSOR_UNKNOWN,
293f5f65 951 CPU_FMA4_FLAGS, 0 },
8a2c8fef 952 { STRING_COMMA_LEN (".xop"), PROCESSOR_UNKNOWN,
293f5f65 953 CPU_XOP_FLAGS, 0 },
8a2c8fef 954 { STRING_COMMA_LEN (".lwp"), PROCESSOR_UNKNOWN,
293f5f65 955 CPU_LWP_FLAGS, 0 },
8a2c8fef 956 { STRING_COMMA_LEN (".movbe"), PROCESSOR_UNKNOWN,
293f5f65 957 CPU_MOVBE_FLAGS, 0 },
60aa667e 958 { STRING_COMMA_LEN (".cx16"), PROCESSOR_UNKNOWN,
293f5f65 959 CPU_CX16_FLAGS, 0 },
8a2c8fef 960 { STRING_COMMA_LEN (".ept"), PROCESSOR_UNKNOWN,
293f5f65 961 CPU_EPT_FLAGS, 0 },
6c30d220 962 { STRING_COMMA_LEN (".lzcnt"), PROCESSOR_UNKNOWN,
293f5f65 963 CPU_LZCNT_FLAGS, 0 },
42164a71 964 { STRING_COMMA_LEN (".hle"), PROCESSOR_UNKNOWN,
293f5f65 965 CPU_HLE_FLAGS, 0 },
42164a71 966 { STRING_COMMA_LEN (".rtm"), PROCESSOR_UNKNOWN,
293f5f65 967 CPU_RTM_FLAGS, 0 },
6c30d220 968 { STRING_COMMA_LEN (".invpcid"), PROCESSOR_UNKNOWN,
293f5f65 969 CPU_INVPCID_FLAGS, 0 },
8a2c8fef 970 { STRING_COMMA_LEN (".clflush"), PROCESSOR_UNKNOWN,
293f5f65 971 CPU_CLFLUSH_FLAGS, 0 },
22109423 972 { STRING_COMMA_LEN (".nop"), PROCESSOR_UNKNOWN,
293f5f65 973 CPU_NOP_FLAGS, 0 },
8a2c8fef 974 { STRING_COMMA_LEN (".syscall"), PROCESSOR_UNKNOWN,
293f5f65 975 CPU_SYSCALL_FLAGS, 0 },
8a2c8fef 976 { STRING_COMMA_LEN (".rdtscp"), PROCESSOR_UNKNOWN,
293f5f65 977 CPU_RDTSCP_FLAGS, 0 },
8a2c8fef 978 { STRING_COMMA_LEN (".3dnow"), PROCESSOR_UNKNOWN,
293f5f65 979 CPU_3DNOW_FLAGS, 0 },
8a2c8fef 980 { STRING_COMMA_LEN (".3dnowa"), PROCESSOR_UNKNOWN,
293f5f65 981 CPU_3DNOWA_FLAGS, 0 },
8a2c8fef 982 { STRING_COMMA_LEN (".padlock"), PROCESSOR_UNKNOWN,
293f5f65 983 CPU_PADLOCK_FLAGS, 0 },
8a2c8fef 984 { STRING_COMMA_LEN (".pacifica"), PROCESSOR_UNKNOWN,
293f5f65 985 CPU_SVME_FLAGS, 1 },
8a2c8fef 986 { STRING_COMMA_LEN (".svme"), PROCESSOR_UNKNOWN,
293f5f65 987 CPU_SVME_FLAGS, 0 },
8a2c8fef 988 { STRING_COMMA_LEN (".sse4a"), PROCESSOR_UNKNOWN,
293f5f65 989 CPU_SSE4A_FLAGS, 0 },
8a2c8fef 990 { STRING_COMMA_LEN (".abm"), PROCESSOR_UNKNOWN,
293f5f65 991 CPU_ABM_FLAGS, 0 },
87973e9f 992 { STRING_COMMA_LEN (".bmi"), PROCESSOR_UNKNOWN,
293f5f65 993 CPU_BMI_FLAGS, 0 },
2a2a0f38 994 { STRING_COMMA_LEN (".tbm"), PROCESSOR_UNKNOWN,
293f5f65 995 CPU_TBM_FLAGS, 0 },
e2e1fcde 996 { STRING_COMMA_LEN (".adx"), PROCESSOR_UNKNOWN,
293f5f65 997 CPU_ADX_FLAGS, 0 },
e2e1fcde 998 { STRING_COMMA_LEN (".rdseed"), PROCESSOR_UNKNOWN,
293f5f65 999 CPU_RDSEED_FLAGS, 0 },
e2e1fcde 1000 { STRING_COMMA_LEN (".prfchw"), PROCESSOR_UNKNOWN,
293f5f65 1001 CPU_PRFCHW_FLAGS, 0 },
5c111e37 1002 { STRING_COMMA_LEN (".smap"), PROCESSOR_UNKNOWN,
293f5f65 1003 CPU_SMAP_FLAGS, 0 },
7e8b059b 1004 { STRING_COMMA_LEN (".mpx"), PROCESSOR_UNKNOWN,
293f5f65 1005 CPU_MPX_FLAGS, 0 },
a0046408 1006 { STRING_COMMA_LEN (".sha"), PROCESSOR_UNKNOWN,
293f5f65 1007 CPU_SHA_FLAGS, 0 },
963f3586 1008 { STRING_COMMA_LEN (".clflushopt"), PROCESSOR_UNKNOWN,
293f5f65 1009 CPU_CLFLUSHOPT_FLAGS, 0 },
dcf893b5 1010 { STRING_COMMA_LEN (".prefetchwt1"), PROCESSOR_UNKNOWN,
293f5f65 1011 CPU_PREFETCHWT1_FLAGS, 0 },
2cf200a4 1012 { STRING_COMMA_LEN (".se1"), PROCESSOR_UNKNOWN,
293f5f65 1013 CPU_SE1_FLAGS, 0 },
c5e7287a 1014 { STRING_COMMA_LEN (".clwb"), PROCESSOR_UNKNOWN,
293f5f65 1015 CPU_CLWB_FLAGS, 0 },
2cc1b5aa 1016 { STRING_COMMA_LEN (".avx512ifma"), PROCESSOR_UNKNOWN,
293f5f65 1017 CPU_AVX512IFMA_FLAGS, 0 },
14f195c9 1018 { STRING_COMMA_LEN (".avx512vbmi"), PROCESSOR_UNKNOWN,
293f5f65 1019 CPU_AVX512VBMI_FLAGS, 0 },
920d2ddc
IT
1020 { STRING_COMMA_LEN (".avx512_4fmaps"), PROCESSOR_UNKNOWN,
1021 CPU_AVX512_4FMAPS_FLAGS, 0 },
47acf0bd
IT
1022 { STRING_COMMA_LEN (".avx512_4vnniw"), PROCESSOR_UNKNOWN,
1023 CPU_AVX512_4VNNIW_FLAGS, 0 },
620214f7
IT
1024 { STRING_COMMA_LEN (".avx512_vpopcntdq"), PROCESSOR_UNKNOWN,
1025 CPU_AVX512_VPOPCNTDQ_FLAGS, 0 },
53467f57
IT
1026 { STRING_COMMA_LEN (".avx512_vbmi2"), PROCESSOR_UNKNOWN,
1027 CPU_AVX512_VBMI2_FLAGS, 0 },
8cfcb765
IT
1028 { STRING_COMMA_LEN (".avx512_vnni"), PROCESSOR_UNKNOWN,
1029 CPU_AVX512_VNNI_FLAGS, 0 },
ee6872be
IT
1030 { STRING_COMMA_LEN (".avx512_bitalg"), PROCESSOR_UNKNOWN,
1031 CPU_AVX512_BITALG_FLAGS, 0 },
029f3522 1032 { STRING_COMMA_LEN (".clzero"), PROCESSOR_UNKNOWN,
293f5f65 1033 CPU_CLZERO_FLAGS, 0 },
9916071f 1034 { STRING_COMMA_LEN (".mwaitx"), PROCESSOR_UNKNOWN,
293f5f65 1035 CPU_MWAITX_FLAGS, 0 },
8eab4136 1036 { STRING_COMMA_LEN (".ospke"), PROCESSOR_UNKNOWN,
293f5f65 1037 CPU_OSPKE_FLAGS, 0 },
8bc52696 1038 { STRING_COMMA_LEN (".rdpid"), PROCESSOR_UNKNOWN,
293f5f65 1039 CPU_RDPID_FLAGS, 0 },
6b40c462
L
1040 { STRING_COMMA_LEN (".ptwrite"), PROCESSOR_UNKNOWN,
1041 CPU_PTWRITE_FLAGS, 0 },
d777820b
IT
1042 { STRING_COMMA_LEN (".ibt"), PROCESSOR_UNKNOWN,
1043 CPU_IBT_FLAGS, 0 },
1044 { STRING_COMMA_LEN (".shstk"), PROCESSOR_UNKNOWN,
1045 CPU_SHSTK_FLAGS, 0 },
48521003
IT
1046 { STRING_COMMA_LEN (".gfni"), PROCESSOR_UNKNOWN,
1047 CPU_GFNI_FLAGS, 0 },
8dcf1fad
IT
1048 { STRING_COMMA_LEN (".vaes"), PROCESSOR_UNKNOWN,
1049 CPU_VAES_FLAGS, 0 },
ff1982d5
IT
1050 { STRING_COMMA_LEN (".vpclmulqdq"), PROCESSOR_UNKNOWN,
1051 CPU_VPCLMULQDQ_FLAGS, 0 },
3233d7d0
IT
1052 { STRING_COMMA_LEN (".wbnoinvd"), PROCESSOR_UNKNOWN,
1053 CPU_WBNOINVD_FLAGS, 0 },
be3a8dca
IT
1054 { STRING_COMMA_LEN (".pconfig"), PROCESSOR_UNKNOWN,
1055 CPU_PCONFIG_FLAGS, 0 },
de89d0a3
IT
1056 { STRING_COMMA_LEN (".waitpkg"), PROCESSOR_UNKNOWN,
1057 CPU_WAITPKG_FLAGS, 0 },
c48935d7
IT
1058 { STRING_COMMA_LEN (".cldemote"), PROCESSOR_UNKNOWN,
1059 CPU_CLDEMOTE_FLAGS, 0 },
c0a30a9f
L
1060 { STRING_COMMA_LEN (".movdiri"), PROCESSOR_UNKNOWN,
1061 CPU_MOVDIRI_FLAGS, 0 },
1062 { STRING_COMMA_LEN (".movdir64b"), PROCESSOR_UNKNOWN,
1063 CPU_MOVDIR64B_FLAGS, 0 },
293f5f65
L
1064};
1065
1066static const noarch_entry cpu_noarch[] =
1067{
1068 { STRING_COMMA_LEN ("no87"), CPU_ANY_X87_FLAGS },
1848e567
L
1069 { STRING_COMMA_LEN ("no287"), CPU_ANY_287_FLAGS },
1070 { STRING_COMMA_LEN ("no387"), CPU_ANY_387_FLAGS },
1071 { STRING_COMMA_LEN ("no687"), CPU_ANY_687_FLAGS },
d871f3f4
L
1072 { STRING_COMMA_LEN ("nocmov"), CPU_ANY_CMOV_FLAGS },
1073 { STRING_COMMA_LEN ("nofxsr"), CPU_ANY_FXSR_FLAGS },
293f5f65
L
1074 { STRING_COMMA_LEN ("nommx"), CPU_ANY_MMX_FLAGS },
1075 { STRING_COMMA_LEN ("nosse"), CPU_ANY_SSE_FLAGS },
1848e567
L
1076 { STRING_COMMA_LEN ("nosse2"), CPU_ANY_SSE2_FLAGS },
1077 { STRING_COMMA_LEN ("nosse3"), CPU_ANY_SSE3_FLAGS },
1078 { STRING_COMMA_LEN ("nossse3"), CPU_ANY_SSSE3_FLAGS },
1079 { STRING_COMMA_LEN ("nosse4.1"), CPU_ANY_SSE4_1_FLAGS },
1080 { STRING_COMMA_LEN ("nosse4.2"), CPU_ANY_SSE4_2_FLAGS },
1081 { STRING_COMMA_LEN ("nosse4"), CPU_ANY_SSE4_1_FLAGS },
293f5f65 1082 { STRING_COMMA_LEN ("noavx"), CPU_ANY_AVX_FLAGS },
1848e567 1083 { STRING_COMMA_LEN ("noavx2"), CPU_ANY_AVX2_FLAGS },
144b71e2
L
1084 { STRING_COMMA_LEN ("noavx512f"), CPU_ANY_AVX512F_FLAGS },
1085 { STRING_COMMA_LEN ("noavx512cd"), CPU_ANY_AVX512CD_FLAGS },
1086 { STRING_COMMA_LEN ("noavx512er"), CPU_ANY_AVX512ER_FLAGS },
1087 { STRING_COMMA_LEN ("noavx512pf"), CPU_ANY_AVX512PF_FLAGS },
1088 { STRING_COMMA_LEN ("noavx512dq"), CPU_ANY_AVX512DQ_FLAGS },
1089 { STRING_COMMA_LEN ("noavx512bw"), CPU_ANY_AVX512BW_FLAGS },
1090 { STRING_COMMA_LEN ("noavx512vl"), CPU_ANY_AVX512VL_FLAGS },
1091 { STRING_COMMA_LEN ("noavx512ifma"), CPU_ANY_AVX512IFMA_FLAGS },
1092 { STRING_COMMA_LEN ("noavx512vbmi"), CPU_ANY_AVX512VBMI_FLAGS },
920d2ddc 1093 { STRING_COMMA_LEN ("noavx512_4fmaps"), CPU_ANY_AVX512_4FMAPS_FLAGS },
47acf0bd 1094 { STRING_COMMA_LEN ("noavx512_4vnniw"), CPU_ANY_AVX512_4VNNIW_FLAGS },
620214f7 1095 { STRING_COMMA_LEN ("noavx512_vpopcntdq"), CPU_ANY_AVX512_VPOPCNTDQ_FLAGS },
53467f57 1096 { STRING_COMMA_LEN ("noavx512_vbmi2"), CPU_ANY_AVX512_VBMI2_FLAGS },
8cfcb765 1097 { STRING_COMMA_LEN ("noavx512_vnni"), CPU_ANY_AVX512_VNNI_FLAGS },
ee6872be 1098 { STRING_COMMA_LEN ("noavx512_bitalg"), CPU_ANY_AVX512_BITALG_FLAGS },
d777820b
IT
1099 { STRING_COMMA_LEN ("noibt"), CPU_ANY_IBT_FLAGS },
1100 { STRING_COMMA_LEN ("noshstk"), CPU_ANY_SHSTK_FLAGS },
c0a30a9f
L
1101 { STRING_COMMA_LEN ("nomovdiri"), CPU_ANY_MOVDIRI_FLAGS },
1102 { STRING_COMMA_LEN ("nomovdir64b"), CPU_ANY_MOVDIR64B_FLAGS },
e413e4e9
AM
1103};
1104
704209c0 1105#ifdef I386COFF
a6c24e68
NC
1106/* Like s_lcomm_internal in gas/read.c but the alignment string
1107 is allowed to be optional. */
1108
1109static symbolS *
1110pe_lcomm_internal (int needs_align, symbolS *symbolP, addressT size)
1111{
1112 addressT align = 0;
1113
1114 SKIP_WHITESPACE ();
1115
7ab9ffdd 1116 if (needs_align
a6c24e68
NC
1117 && *input_line_pointer == ',')
1118 {
1119 align = parse_align (needs_align - 1);
7ab9ffdd 1120
a6c24e68
NC
1121 if (align == (addressT) -1)
1122 return NULL;
1123 }
1124 else
1125 {
1126 if (size >= 8)
1127 align = 3;
1128 else if (size >= 4)
1129 align = 2;
1130 else if (size >= 2)
1131 align = 1;
1132 else
1133 align = 0;
1134 }
1135
1136 bss_alloc (symbolP, size, align);
1137 return symbolP;
1138}
1139
704209c0 1140static void
a6c24e68
NC
1141pe_lcomm (int needs_align)
1142{
1143 s_comm_internal (needs_align * 2, pe_lcomm_internal);
1144}
704209c0 1145#endif
a6c24e68 1146
29b0f896
AM
1147const pseudo_typeS md_pseudo_table[] =
1148{
1149#if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
1150 {"align", s_align_bytes, 0},
1151#else
1152 {"align", s_align_ptwo, 0},
1153#endif
1154 {"arch", set_cpu_arch, 0},
1155#ifndef I386COFF
1156 {"bss", s_bss, 0},
a6c24e68
NC
1157#else
1158 {"lcomm", pe_lcomm, 1},
29b0f896
AM
1159#endif
1160 {"ffloat", float_cons, 'f'},
1161 {"dfloat", float_cons, 'd'},
1162 {"tfloat", float_cons, 'x'},
1163 {"value", cons, 2},
d182319b 1164 {"slong", signed_cons, 4},
29b0f896
AM
1165 {"noopt", s_ignore, 0},
1166 {"optim", s_ignore, 0},
1167 {"code16gcc", set_16bit_gcc_code_flag, CODE_16BIT},
1168 {"code16", set_code_flag, CODE_16BIT},
1169 {"code32", set_code_flag, CODE_32BIT},
da5f19a2 1170#ifdef BFD64
29b0f896 1171 {"code64", set_code_flag, CODE_64BIT},
da5f19a2 1172#endif
29b0f896
AM
1173 {"intel_syntax", set_intel_syntax, 1},
1174 {"att_syntax", set_intel_syntax, 0},
1efbbeb4
L
1175 {"intel_mnemonic", set_intel_mnemonic, 1},
1176 {"att_mnemonic", set_intel_mnemonic, 0},
db51cc60
L
1177 {"allow_index_reg", set_allow_index_reg, 1},
1178 {"disallow_index_reg", set_allow_index_reg, 0},
7bab8ab5
JB
1179 {"sse_check", set_check, 0},
1180 {"operand_check", set_check, 1},
3b22753a
L
1181#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1182 {"largecomm", handle_large_common, 0},
07a53e5c 1183#else
68d20676 1184 {"file", dwarf2_directive_file, 0},
07a53e5c
RH
1185 {"loc", dwarf2_directive_loc, 0},
1186 {"loc_mark_labels", dwarf2_directive_loc_mark_labels, 0},
3b22753a 1187#endif
6482c264
NC
1188#ifdef TE_PE
1189 {"secrel32", pe_directive_secrel, 0},
1190#endif
29b0f896
AM
1191 {0, 0, 0}
1192};
1193
1194/* For interface with expression (). */
1195extern char *input_line_pointer;
1196
1197/* Hash table for instruction mnemonic lookup. */
1198static struct hash_control *op_hash;
1199
1200/* Hash table for register lookup. */
1201static struct hash_control *reg_hash;
1202\f
ce8a8b2f
AM
1203 /* Various efficient no-op patterns for aligning code labels.
1204 Note: Don't try to assemble the instructions in the comments.
1205 0L and 0w are not legal. */
62a02d25
L
1206static const unsigned char f32_1[] =
1207 {0x90}; /* nop */
1208static const unsigned char f32_2[] =
1209 {0x66,0x90}; /* xchg %ax,%ax */
1210static const unsigned char f32_3[] =
1211 {0x8d,0x76,0x00}; /* leal 0(%esi),%esi */
1212static const unsigned char f32_4[] =
1213 {0x8d,0x74,0x26,0x00}; /* leal 0(%esi,1),%esi */
62a02d25
L
1214static const unsigned char f32_6[] =
1215 {0x8d,0xb6,0x00,0x00,0x00,0x00}; /* leal 0L(%esi),%esi */
1216static const unsigned char f32_7[] =
1217 {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal 0L(%esi,1),%esi */
62a02d25 1218static const unsigned char f16_3[] =
3ae729d5 1219 {0x8d,0x74,0x00}; /* lea 0(%si),%si */
62a02d25 1220static const unsigned char f16_4[] =
3ae729d5
L
1221 {0x8d,0xb4,0x00,0x00}; /* lea 0W(%si),%si */
1222static const unsigned char jump_disp8[] =
1223 {0xeb}; /* jmp disp8 */
1224static const unsigned char jump32_disp32[] =
1225 {0xe9}; /* jmp disp32 */
1226static const unsigned char jump16_disp32[] =
1227 {0x66,0xe9}; /* jmp disp32 */
62a02d25
L
1228/* 32-bit NOPs patterns. */
1229static const unsigned char *const f32_patt[] = {
3ae729d5 1230 f32_1, f32_2, f32_3, f32_4, NULL, f32_6, f32_7
62a02d25
L
1231};
1232/* 16-bit NOPs patterns. */
1233static const unsigned char *const f16_patt[] = {
3ae729d5 1234 f32_1, f32_2, f16_3, f16_4
62a02d25
L
1235};
1236/* nopl (%[re]ax) */
1237static const unsigned char alt_3[] =
1238 {0x0f,0x1f,0x00};
1239/* nopl 0(%[re]ax) */
1240static const unsigned char alt_4[] =
1241 {0x0f,0x1f,0x40,0x00};
1242/* nopl 0(%[re]ax,%[re]ax,1) */
1243static const unsigned char alt_5[] =
1244 {0x0f,0x1f,0x44,0x00,0x00};
1245/* nopw 0(%[re]ax,%[re]ax,1) */
1246static const unsigned char alt_6[] =
1247 {0x66,0x0f,0x1f,0x44,0x00,0x00};
1248/* nopl 0L(%[re]ax) */
1249static const unsigned char alt_7[] =
1250 {0x0f,0x1f,0x80,0x00,0x00,0x00,0x00};
1251/* nopl 0L(%[re]ax,%[re]ax,1) */
1252static const unsigned char alt_8[] =
1253 {0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1254/* nopw 0L(%[re]ax,%[re]ax,1) */
1255static const unsigned char alt_9[] =
1256 {0x66,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1257/* nopw %cs:0L(%[re]ax,%[re]ax,1) */
1258static const unsigned char alt_10[] =
1259 {0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
3ae729d5
L
1260/* data16 nopw %cs:0L(%eax,%eax,1) */
1261static const unsigned char alt_11[] =
1262 {0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
62a02d25
L
1263/* 32-bit and 64-bit NOPs patterns. */
1264static const unsigned char *const alt_patt[] = {
1265 f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
3ae729d5 1266 alt_9, alt_10, alt_11
62a02d25
L
1267};
1268
1269/* Genenerate COUNT bytes of NOPs to WHERE from PATT with the maximum
1270 size of a single NOP instruction MAX_SINGLE_NOP_SIZE. */
1271
1272static void
1273i386_output_nops (char *where, const unsigned char *const *patt,
1274 int count, int max_single_nop_size)
1275
1276{
3ae729d5
L
1277 /* Place the longer NOP first. */
1278 int last;
1279 int offset;
1280 const unsigned char *nops = patt[max_single_nop_size - 1];
1281
1282 /* Use the smaller one if the requsted one isn't available. */
1283 if (nops == NULL)
62a02d25 1284 {
3ae729d5
L
1285 max_single_nop_size--;
1286 nops = patt[max_single_nop_size - 1];
62a02d25
L
1287 }
1288
3ae729d5
L
1289 last = count % max_single_nop_size;
1290
1291 count -= last;
1292 for (offset = 0; offset < count; offset += max_single_nop_size)
1293 memcpy (where + offset, nops, max_single_nop_size);
1294
1295 if (last)
1296 {
1297 nops = patt[last - 1];
1298 if (nops == NULL)
1299 {
1300 /* Use the smaller one plus one-byte NOP if the needed one
1301 isn't available. */
1302 last--;
1303 nops = patt[last - 1];
1304 memcpy (where + offset, nops, last);
1305 where[offset + last] = *patt[0];
1306 }
1307 else
1308 memcpy (where + offset, nops, last);
1309 }
62a02d25
L
1310}
1311
3ae729d5
L
1312static INLINE int
1313fits_in_imm7 (offsetT num)
1314{
1315 return (num & 0x7f) == num;
1316}
1317
1318static INLINE int
1319fits_in_imm31 (offsetT num)
1320{
1321 return (num & 0x7fffffff) == num;
1322}
62a02d25
L
1323
1324/* Genenerate COUNT bytes of NOPs to WHERE with the maximum size of a
1325 single NOP instruction LIMIT. */
1326
1327void
3ae729d5 1328i386_generate_nops (fragS *fragP, char *where, offsetT count, int limit)
62a02d25 1329{
3ae729d5 1330 const unsigned char *const *patt = NULL;
62a02d25 1331 int max_single_nop_size;
3ae729d5
L
1332 /* Maximum number of NOPs before switching to jump over NOPs. */
1333 int max_number_of_nops;
62a02d25 1334
3ae729d5 1335 switch (fragP->fr_type)
62a02d25 1336 {
3ae729d5
L
1337 case rs_fill_nop:
1338 case rs_align_code:
1339 break;
1340 default:
62a02d25
L
1341 return;
1342 }
1343
ccc9c027
L
1344 /* We need to decide which NOP sequence to use for 32bit and
1345 64bit. When -mtune= is used:
4eed87de 1346
76bc74dc
L
1347 1. For PROCESSOR_I386, PROCESSOR_I486, PROCESSOR_PENTIUM and
1348 PROCESSOR_GENERIC32, f32_patt will be used.
80b8656c
L
1349 2. For the rest, alt_patt will be used.
1350
1351 When -mtune= isn't used, alt_patt will be used if
22109423 1352 cpu_arch_isa_flags has CpuNop. Otherwise, f32_patt will
76bc74dc 1353 be used.
ccc9c027
L
1354
1355 When -march= or .arch is used, we can't use anything beyond
1356 cpu_arch_isa_flags. */
1357
1358 if (flag_code == CODE_16BIT)
1359 {
3ae729d5
L
1360 patt = f16_patt;
1361 max_single_nop_size = sizeof (f16_patt) / sizeof (f16_patt[0]);
1362 /* Limit number of NOPs to 2 in 16-bit mode. */
1363 max_number_of_nops = 2;
252b5132 1364 }
33fef721 1365 else
ccc9c027 1366 {
fbf3f584 1367 if (fragP->tc_frag_data.isa == PROCESSOR_UNKNOWN)
ccc9c027
L
1368 {
1369 /* PROCESSOR_UNKNOWN means that all ISAs may be used. */
1370 switch (cpu_arch_tune)
1371 {
1372 case PROCESSOR_UNKNOWN:
1373 /* We use cpu_arch_isa_flags to check if we SHOULD
22109423
L
1374 optimize with nops. */
1375 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1376 patt = alt_patt;
ccc9c027
L
1377 else
1378 patt = f32_patt;
1379 break;
ccc9c027
L
1380 case PROCESSOR_PENTIUM4:
1381 case PROCESSOR_NOCONA:
ef05d495 1382 case PROCESSOR_CORE:
76bc74dc 1383 case PROCESSOR_CORE2:
bd5295b2 1384 case PROCESSOR_COREI7:
3632d14b 1385 case PROCESSOR_L1OM:
7a9068fe 1386 case PROCESSOR_K1OM:
76bc74dc 1387 case PROCESSOR_GENERIC64:
ccc9c027
L
1388 case PROCESSOR_K6:
1389 case PROCESSOR_ATHLON:
1390 case PROCESSOR_K8:
4eed87de 1391 case PROCESSOR_AMDFAM10:
8aedb9fe 1392 case PROCESSOR_BD:
029f3522 1393 case PROCESSOR_ZNVER:
7b458c12 1394 case PROCESSOR_BT:
80b8656c 1395 patt = alt_patt;
ccc9c027 1396 break;
76bc74dc 1397 case PROCESSOR_I386:
ccc9c027
L
1398 case PROCESSOR_I486:
1399 case PROCESSOR_PENTIUM:
2dde1948 1400 case PROCESSOR_PENTIUMPRO:
81486035 1401 case PROCESSOR_IAMCU:
ccc9c027
L
1402 case PROCESSOR_GENERIC32:
1403 patt = f32_patt;
1404 break;
4eed87de 1405 }
ccc9c027
L
1406 }
1407 else
1408 {
fbf3f584 1409 switch (fragP->tc_frag_data.tune)
ccc9c027
L
1410 {
1411 case PROCESSOR_UNKNOWN:
e6a14101 1412 /* When cpu_arch_isa is set, cpu_arch_tune shouldn't be
ccc9c027
L
1413 PROCESSOR_UNKNOWN. */
1414 abort ();
1415 break;
1416
76bc74dc 1417 case PROCESSOR_I386:
ccc9c027
L
1418 case PROCESSOR_I486:
1419 case PROCESSOR_PENTIUM:
81486035 1420 case PROCESSOR_IAMCU:
ccc9c027
L
1421 case PROCESSOR_K6:
1422 case PROCESSOR_ATHLON:
1423 case PROCESSOR_K8:
4eed87de 1424 case PROCESSOR_AMDFAM10:
8aedb9fe 1425 case PROCESSOR_BD:
029f3522 1426 case PROCESSOR_ZNVER:
7b458c12 1427 case PROCESSOR_BT:
ccc9c027
L
1428 case PROCESSOR_GENERIC32:
1429 /* We use cpu_arch_isa_flags to check if we CAN optimize
22109423
L
1430 with nops. */
1431 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1432 patt = alt_patt;
ccc9c027
L
1433 else
1434 patt = f32_patt;
1435 break;
76bc74dc
L
1436 case PROCESSOR_PENTIUMPRO:
1437 case PROCESSOR_PENTIUM4:
1438 case PROCESSOR_NOCONA:
1439 case PROCESSOR_CORE:
ef05d495 1440 case PROCESSOR_CORE2:
bd5295b2 1441 case PROCESSOR_COREI7:
3632d14b 1442 case PROCESSOR_L1OM:
7a9068fe 1443 case PROCESSOR_K1OM:
22109423 1444 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1445 patt = alt_patt;
ccc9c027
L
1446 else
1447 patt = f32_patt;
1448 break;
1449 case PROCESSOR_GENERIC64:
80b8656c 1450 patt = alt_patt;
ccc9c027 1451 break;
4eed87de 1452 }
ccc9c027
L
1453 }
1454
76bc74dc
L
1455 if (patt == f32_patt)
1456 {
3ae729d5
L
1457 max_single_nop_size = sizeof (f32_patt) / sizeof (f32_patt[0]);
1458 /* Limit number of NOPs to 2 for older processors. */
1459 max_number_of_nops = 2;
76bc74dc
L
1460 }
1461 else
1462 {
3ae729d5
L
1463 max_single_nop_size = sizeof (alt_patt) / sizeof (alt_patt[0]);
1464 /* Limit number of NOPs to 7 for newer processors. */
1465 max_number_of_nops = 7;
1466 }
1467 }
1468
1469 if (limit == 0)
1470 limit = max_single_nop_size;
1471
1472 if (fragP->fr_type == rs_fill_nop)
1473 {
1474 /* Output NOPs for .nop directive. */
1475 if (limit > max_single_nop_size)
1476 {
1477 as_bad_where (fragP->fr_file, fragP->fr_line,
1478 _("invalid single nop size: %d "
1479 "(expect within [0, %d])"),
1480 limit, max_single_nop_size);
1481 return;
1482 }
1483 }
1484 else
1485 fragP->fr_var = count;
1486
1487 if ((count / max_single_nop_size) > max_number_of_nops)
1488 {
1489 /* Generate jump over NOPs. */
1490 offsetT disp = count - 2;
1491 if (fits_in_imm7 (disp))
1492 {
1493 /* Use "jmp disp8" if possible. */
1494 count = disp;
1495 where[0] = jump_disp8[0];
1496 where[1] = count;
1497 where += 2;
1498 }
1499 else
1500 {
1501 unsigned int size_of_jump;
1502
1503 if (flag_code == CODE_16BIT)
1504 {
1505 where[0] = jump16_disp32[0];
1506 where[1] = jump16_disp32[1];
1507 size_of_jump = 2;
1508 }
1509 else
1510 {
1511 where[0] = jump32_disp32[0];
1512 size_of_jump = 1;
1513 }
1514
1515 count -= size_of_jump + 4;
1516 if (!fits_in_imm31 (count))
1517 {
1518 as_bad_where (fragP->fr_file, fragP->fr_line,
1519 _("jump over nop padding out of range"));
1520 return;
1521 }
1522
1523 md_number_to_chars (where + size_of_jump, count, 4);
1524 where += size_of_jump + 4;
76bc74dc 1525 }
ccc9c027 1526 }
3ae729d5
L
1527
1528 /* Generate multiple NOPs. */
1529 i386_output_nops (where, patt, count, limit);
252b5132
RH
1530}
1531
c6fb90c8 1532static INLINE int
0dfbf9d7 1533operand_type_all_zero (const union i386_operand_type *x)
40fb9820 1534{
0dfbf9d7 1535 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1536 {
1537 case 3:
0dfbf9d7 1538 if (x->array[2])
c6fb90c8 1539 return 0;
1a0670f3 1540 /* Fall through. */
c6fb90c8 1541 case 2:
0dfbf9d7 1542 if (x->array[1])
c6fb90c8 1543 return 0;
1a0670f3 1544 /* Fall through. */
c6fb90c8 1545 case 1:
0dfbf9d7 1546 return !x->array[0];
c6fb90c8
L
1547 default:
1548 abort ();
1549 }
40fb9820
L
1550}
1551
c6fb90c8 1552static INLINE void
0dfbf9d7 1553operand_type_set (union i386_operand_type *x, unsigned int v)
40fb9820 1554{
0dfbf9d7 1555 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1556 {
1557 case 3:
0dfbf9d7 1558 x->array[2] = v;
1a0670f3 1559 /* Fall through. */
c6fb90c8 1560 case 2:
0dfbf9d7 1561 x->array[1] = v;
1a0670f3 1562 /* Fall through. */
c6fb90c8 1563 case 1:
0dfbf9d7 1564 x->array[0] = v;
1a0670f3 1565 /* Fall through. */
c6fb90c8
L
1566 break;
1567 default:
1568 abort ();
1569 }
1570}
40fb9820 1571
c6fb90c8 1572static INLINE int
0dfbf9d7
L
1573operand_type_equal (const union i386_operand_type *x,
1574 const union i386_operand_type *y)
c6fb90c8 1575{
0dfbf9d7 1576 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1577 {
1578 case 3:
0dfbf9d7 1579 if (x->array[2] != y->array[2])
c6fb90c8 1580 return 0;
1a0670f3 1581 /* Fall through. */
c6fb90c8 1582 case 2:
0dfbf9d7 1583 if (x->array[1] != y->array[1])
c6fb90c8 1584 return 0;
1a0670f3 1585 /* Fall through. */
c6fb90c8 1586 case 1:
0dfbf9d7 1587 return x->array[0] == y->array[0];
c6fb90c8
L
1588 break;
1589 default:
1590 abort ();
1591 }
1592}
40fb9820 1593
0dfbf9d7
L
1594static INLINE int
1595cpu_flags_all_zero (const union i386_cpu_flags *x)
1596{
1597 switch (ARRAY_SIZE(x->array))
1598 {
53467f57
IT
1599 case 4:
1600 if (x->array[3])
1601 return 0;
1602 /* Fall through. */
0dfbf9d7
L
1603 case 3:
1604 if (x->array[2])
1605 return 0;
1a0670f3 1606 /* Fall through. */
0dfbf9d7
L
1607 case 2:
1608 if (x->array[1])
1609 return 0;
1a0670f3 1610 /* Fall through. */
0dfbf9d7
L
1611 case 1:
1612 return !x->array[0];
1613 default:
1614 abort ();
1615 }
1616}
1617
0dfbf9d7
L
1618static INLINE int
1619cpu_flags_equal (const union i386_cpu_flags *x,
1620 const union i386_cpu_flags *y)
1621{
1622 switch (ARRAY_SIZE(x->array))
1623 {
53467f57
IT
1624 case 4:
1625 if (x->array[3] != y->array[3])
1626 return 0;
1627 /* Fall through. */
0dfbf9d7
L
1628 case 3:
1629 if (x->array[2] != y->array[2])
1630 return 0;
1a0670f3 1631 /* Fall through. */
0dfbf9d7
L
1632 case 2:
1633 if (x->array[1] != y->array[1])
1634 return 0;
1a0670f3 1635 /* Fall through. */
0dfbf9d7
L
1636 case 1:
1637 return x->array[0] == y->array[0];
1638 break;
1639 default:
1640 abort ();
1641 }
1642}
c6fb90c8
L
1643
1644static INLINE int
1645cpu_flags_check_cpu64 (i386_cpu_flags f)
1646{
1647 return !((flag_code == CODE_64BIT && f.bitfield.cpuno64)
1648 || (flag_code != CODE_64BIT && f.bitfield.cpu64));
40fb9820
L
1649}
1650
c6fb90c8
L
1651static INLINE i386_cpu_flags
1652cpu_flags_and (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1653{
c6fb90c8
L
1654 switch (ARRAY_SIZE (x.array))
1655 {
53467f57
IT
1656 case 4:
1657 x.array [3] &= y.array [3];
1658 /* Fall through. */
c6fb90c8
L
1659 case 3:
1660 x.array [2] &= y.array [2];
1a0670f3 1661 /* Fall through. */
c6fb90c8
L
1662 case 2:
1663 x.array [1] &= y.array [1];
1a0670f3 1664 /* Fall through. */
c6fb90c8
L
1665 case 1:
1666 x.array [0] &= y.array [0];
1667 break;
1668 default:
1669 abort ();
1670 }
1671 return x;
1672}
40fb9820 1673
c6fb90c8
L
1674static INLINE i386_cpu_flags
1675cpu_flags_or (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1676{
c6fb90c8 1677 switch (ARRAY_SIZE (x.array))
40fb9820 1678 {
53467f57
IT
1679 case 4:
1680 x.array [3] |= y.array [3];
1681 /* Fall through. */
c6fb90c8
L
1682 case 3:
1683 x.array [2] |= y.array [2];
1a0670f3 1684 /* Fall through. */
c6fb90c8
L
1685 case 2:
1686 x.array [1] |= y.array [1];
1a0670f3 1687 /* Fall through. */
c6fb90c8
L
1688 case 1:
1689 x.array [0] |= y.array [0];
40fb9820
L
1690 break;
1691 default:
1692 abort ();
1693 }
40fb9820
L
1694 return x;
1695}
1696
309d3373
JB
1697static INLINE i386_cpu_flags
1698cpu_flags_and_not (i386_cpu_flags x, i386_cpu_flags y)
1699{
1700 switch (ARRAY_SIZE (x.array))
1701 {
53467f57
IT
1702 case 4:
1703 x.array [3] &= ~y.array [3];
1704 /* Fall through. */
309d3373
JB
1705 case 3:
1706 x.array [2] &= ~y.array [2];
1a0670f3 1707 /* Fall through. */
309d3373
JB
1708 case 2:
1709 x.array [1] &= ~y.array [1];
1a0670f3 1710 /* Fall through. */
309d3373
JB
1711 case 1:
1712 x.array [0] &= ~y.array [0];
1713 break;
1714 default:
1715 abort ();
1716 }
1717 return x;
1718}
1719
c0f3af97
L
1720#define CPU_FLAGS_ARCH_MATCH 0x1
1721#define CPU_FLAGS_64BIT_MATCH 0x2
1722
c0f3af97 1723#define CPU_FLAGS_PERFECT_MATCH \
db12e14e 1724 (CPU_FLAGS_ARCH_MATCH | CPU_FLAGS_64BIT_MATCH)
c0f3af97
L
1725
1726/* Return CPU flags match bits. */
3629bb00 1727
40fb9820 1728static int
d3ce72d0 1729cpu_flags_match (const insn_template *t)
40fb9820 1730{
c0f3af97
L
1731 i386_cpu_flags x = t->cpu_flags;
1732 int match = cpu_flags_check_cpu64 (x) ? CPU_FLAGS_64BIT_MATCH : 0;
40fb9820
L
1733
1734 x.bitfield.cpu64 = 0;
1735 x.bitfield.cpuno64 = 0;
1736
0dfbf9d7 1737 if (cpu_flags_all_zero (&x))
c0f3af97
L
1738 {
1739 /* This instruction is available on all archs. */
db12e14e 1740 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 1741 }
3629bb00
L
1742 else
1743 {
c0f3af97 1744 /* This instruction is available only on some archs. */
3629bb00
L
1745 i386_cpu_flags cpu = cpu_arch_flags;
1746
ab592e75
JB
1747 /* AVX512VL is no standalone feature - match it and then strip it. */
1748 if (x.bitfield.cpuavx512vl && !cpu.bitfield.cpuavx512vl)
1749 return match;
1750 x.bitfield.cpuavx512vl = 0;
1751
3629bb00 1752 cpu = cpu_flags_and (x, cpu);
c0f3af97
L
1753 if (!cpu_flags_all_zero (&cpu))
1754 {
a5ff0eb2
L
1755 if (x.bitfield.cpuavx)
1756 {
929f69fa 1757 /* We need to check a few extra flags with AVX. */
b9d49817
JB
1758 if (cpu.bitfield.cpuavx
1759 && (!t->opcode_modifier.sse2avx || sse2avx)
1760 && (!x.bitfield.cpuaes || cpu.bitfield.cpuaes)
929f69fa 1761 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
b9d49817
JB
1762 && (!x.bitfield.cpupclmul || cpu.bitfield.cpupclmul))
1763 match |= CPU_FLAGS_ARCH_MATCH;
a5ff0eb2 1764 }
929f69fa
JB
1765 else if (x.bitfield.cpuavx512f)
1766 {
1767 /* We need to check a few extra flags with AVX512F. */
1768 if (cpu.bitfield.cpuavx512f
1769 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
1770 && (!x.bitfield.cpuvaes || cpu.bitfield.cpuvaes)
1771 && (!x.bitfield.cpuvpclmulqdq || cpu.bitfield.cpuvpclmulqdq))
1772 match |= CPU_FLAGS_ARCH_MATCH;
1773 }
a5ff0eb2 1774 else
db12e14e 1775 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 1776 }
3629bb00 1777 }
c0f3af97 1778 return match;
40fb9820
L
1779}
1780
c6fb90c8
L
1781static INLINE i386_operand_type
1782operand_type_and (i386_operand_type x, i386_operand_type y)
40fb9820 1783{
c6fb90c8
L
1784 switch (ARRAY_SIZE (x.array))
1785 {
1786 case 3:
1787 x.array [2] &= y.array [2];
1a0670f3 1788 /* Fall through. */
c6fb90c8
L
1789 case 2:
1790 x.array [1] &= y.array [1];
1a0670f3 1791 /* Fall through. */
c6fb90c8
L
1792 case 1:
1793 x.array [0] &= y.array [0];
1794 break;
1795 default:
1796 abort ();
1797 }
1798 return x;
40fb9820
L
1799}
1800
73053c1f
JB
1801static INLINE i386_operand_type
1802operand_type_and_not (i386_operand_type x, i386_operand_type y)
1803{
1804 switch (ARRAY_SIZE (x.array))
1805 {
1806 case 3:
1807 x.array [2] &= ~y.array [2];
1808 /* Fall through. */
1809 case 2:
1810 x.array [1] &= ~y.array [1];
1811 /* Fall through. */
1812 case 1:
1813 x.array [0] &= ~y.array [0];
1814 break;
1815 default:
1816 abort ();
1817 }
1818 return x;
1819}
1820
c6fb90c8
L
1821static INLINE i386_operand_type
1822operand_type_or (i386_operand_type x, i386_operand_type y)
40fb9820 1823{
c6fb90c8 1824 switch (ARRAY_SIZE (x.array))
40fb9820 1825 {
c6fb90c8
L
1826 case 3:
1827 x.array [2] |= y.array [2];
1a0670f3 1828 /* Fall through. */
c6fb90c8
L
1829 case 2:
1830 x.array [1] |= y.array [1];
1a0670f3 1831 /* Fall through. */
c6fb90c8
L
1832 case 1:
1833 x.array [0] |= y.array [0];
40fb9820
L
1834 break;
1835 default:
1836 abort ();
1837 }
c6fb90c8
L
1838 return x;
1839}
40fb9820 1840
c6fb90c8
L
1841static INLINE i386_operand_type
1842operand_type_xor (i386_operand_type x, i386_operand_type y)
1843{
1844 switch (ARRAY_SIZE (x.array))
1845 {
1846 case 3:
1847 x.array [2] ^= y.array [2];
1a0670f3 1848 /* Fall through. */
c6fb90c8
L
1849 case 2:
1850 x.array [1] ^= y.array [1];
1a0670f3 1851 /* Fall through. */
c6fb90c8
L
1852 case 1:
1853 x.array [0] ^= y.array [0];
1854 break;
1855 default:
1856 abort ();
1857 }
40fb9820
L
1858 return x;
1859}
1860
1861static const i386_operand_type acc32 = OPERAND_TYPE_ACC32;
1862static const i386_operand_type acc64 = OPERAND_TYPE_ACC64;
40fb9820
L
1863static const i386_operand_type disp16 = OPERAND_TYPE_DISP16;
1864static const i386_operand_type disp32 = OPERAND_TYPE_DISP32;
1865static const i386_operand_type disp32s = OPERAND_TYPE_DISP32S;
1866static const i386_operand_type disp16_32 = OPERAND_TYPE_DISP16_32;
1867static const i386_operand_type anydisp
1868 = OPERAND_TYPE_ANYDISP;
40fb9820 1869static const i386_operand_type regxmm = OPERAND_TYPE_REGXMM;
43234a1e 1870static const i386_operand_type regmask = OPERAND_TYPE_REGMASK;
40fb9820
L
1871static const i386_operand_type imm8 = OPERAND_TYPE_IMM8;
1872static const i386_operand_type imm8s = OPERAND_TYPE_IMM8S;
1873static const i386_operand_type imm16 = OPERAND_TYPE_IMM16;
1874static const i386_operand_type imm32 = OPERAND_TYPE_IMM32;
1875static const i386_operand_type imm32s = OPERAND_TYPE_IMM32S;
1876static const i386_operand_type imm64 = OPERAND_TYPE_IMM64;
1877static const i386_operand_type imm16_32 = OPERAND_TYPE_IMM16_32;
1878static const i386_operand_type imm16_32s = OPERAND_TYPE_IMM16_32S;
1879static const i386_operand_type imm16_32_32s = OPERAND_TYPE_IMM16_32_32S;
a683cc34 1880static const i386_operand_type vec_imm4 = OPERAND_TYPE_VEC_IMM4;
40fb9820
L
1881
1882enum operand_type
1883{
1884 reg,
40fb9820
L
1885 imm,
1886 disp,
1887 anymem
1888};
1889
c6fb90c8 1890static INLINE int
40fb9820
L
1891operand_type_check (i386_operand_type t, enum operand_type c)
1892{
1893 switch (c)
1894 {
1895 case reg:
dc821c5f 1896 return t.bitfield.reg;
40fb9820 1897
40fb9820
L
1898 case imm:
1899 return (t.bitfield.imm8
1900 || t.bitfield.imm8s
1901 || t.bitfield.imm16
1902 || t.bitfield.imm32
1903 || t.bitfield.imm32s
1904 || t.bitfield.imm64);
1905
1906 case disp:
1907 return (t.bitfield.disp8
1908 || t.bitfield.disp16
1909 || t.bitfield.disp32
1910 || t.bitfield.disp32s
1911 || t.bitfield.disp64);
1912
1913 case anymem:
1914 return (t.bitfield.disp8
1915 || t.bitfield.disp16
1916 || t.bitfield.disp32
1917 || t.bitfield.disp32s
1918 || t.bitfield.disp64
1919 || t.bitfield.baseindex);
1920
1921 default:
1922 abort ();
1923 }
2cfe26b6
AM
1924
1925 return 0;
40fb9820
L
1926}
1927
7a54636a
L
1928/* Return 1 if there is no conflict in 8bit/16bit/32bit/64bit/80bit size
1929 between operand GIVEN and opeand WANTED for instruction template T. */
5c07affc
L
1930
1931static INLINE int
7a54636a
L
1932match_operand_size (const insn_template *t, unsigned int wanted,
1933 unsigned int given)
5c07affc 1934{
3ac21baa
JB
1935 return !((i.types[given].bitfield.byte
1936 && !t->operand_types[wanted].bitfield.byte)
1937 || (i.types[given].bitfield.word
1938 && !t->operand_types[wanted].bitfield.word)
1939 || (i.types[given].bitfield.dword
1940 && !t->operand_types[wanted].bitfield.dword)
1941 || (i.types[given].bitfield.qword
1942 && !t->operand_types[wanted].bitfield.qword)
1943 || (i.types[given].bitfield.tbyte
1944 && !t->operand_types[wanted].bitfield.tbyte));
5c07affc
L
1945}
1946
dd40ce22
L
1947/* Return 1 if there is no conflict in SIMD register between operand
1948 GIVEN and opeand WANTED for instruction template T. */
1b54b8d7
JB
1949
1950static INLINE int
dd40ce22
L
1951match_simd_size (const insn_template *t, unsigned int wanted,
1952 unsigned int given)
1b54b8d7 1953{
3ac21baa
JB
1954 return !((i.types[given].bitfield.xmmword
1955 && !t->operand_types[wanted].bitfield.xmmword)
1956 || (i.types[given].bitfield.ymmword
1957 && !t->operand_types[wanted].bitfield.ymmword)
1958 || (i.types[given].bitfield.zmmword
1959 && !t->operand_types[wanted].bitfield.zmmword));
1b54b8d7
JB
1960}
1961
7a54636a
L
1962/* Return 1 if there is no conflict in any size between operand GIVEN
1963 and opeand WANTED for instruction template T. */
5c07affc
L
1964
1965static INLINE int
dd40ce22
L
1966match_mem_size (const insn_template *t, unsigned int wanted,
1967 unsigned int given)
5c07affc 1968{
7a54636a 1969 return (match_operand_size (t, wanted, given)
3ac21baa 1970 && !((i.types[given].bitfield.unspecified
af508cb9 1971 && !i.broadcast
3ac21baa
JB
1972 && !t->operand_types[wanted].bitfield.unspecified)
1973 || (i.types[given].bitfield.fword
1974 && !t->operand_types[wanted].bitfield.fword)
1b54b8d7
JB
1975 /* For scalar opcode templates to allow register and memory
1976 operands at the same time, some special casing is needed
d6793fa1
JB
1977 here. Also for v{,p}broadcast*, {,v}pmov{s,z}*, and
1978 down-conversion vpmov*. */
3ac21baa 1979 || ((t->operand_types[wanted].bitfield.regsimd
1b54b8d7 1980 && !t->opcode_modifier.broadcast
3ac21baa
JB
1981 && (t->operand_types[wanted].bitfield.byte
1982 || t->operand_types[wanted].bitfield.word
1983 || t->operand_types[wanted].bitfield.dword
1984 || t->operand_types[wanted].bitfield.qword))
1985 ? (i.types[given].bitfield.xmmword
1986 || i.types[given].bitfield.ymmword
1987 || i.types[given].bitfield.zmmword)
1988 : !match_simd_size(t, wanted, given))));
5c07affc
L
1989}
1990
3ac21baa
JB
1991/* Return value has MATCH_STRAIGHT set if there is no size conflict on any
1992 operands for instruction template T, and it has MATCH_REVERSE set if there
1993 is no size conflict on any operands for the template with operands reversed
1994 (and the template allows for reversing in the first place). */
5c07affc 1995
3ac21baa
JB
1996#define MATCH_STRAIGHT 1
1997#define MATCH_REVERSE 2
1998
1999static INLINE unsigned int
d3ce72d0 2000operand_size_match (const insn_template *t)
5c07affc 2001{
3ac21baa 2002 unsigned int j, match = MATCH_STRAIGHT;
5c07affc
L
2003
2004 /* Don't check jump instructions. */
2005 if (t->opcode_modifier.jump
2006 || t->opcode_modifier.jumpbyte
2007 || t->opcode_modifier.jumpdword
2008 || t->opcode_modifier.jumpintersegment)
2009 return match;
2010
2011 /* Check memory and accumulator operand size. */
2012 for (j = 0; j < i.operands; j++)
2013 {
1b54b8d7
JB
2014 if (!i.types[j].bitfield.reg && !i.types[j].bitfield.regsimd
2015 && t->operand_types[j].bitfield.anysize)
5c07affc
L
2016 continue;
2017
1b54b8d7 2018 if (t->operand_types[j].bitfield.reg
7a54636a 2019 && !match_operand_size (t, j, j))
5c07affc
L
2020 {
2021 match = 0;
2022 break;
2023 }
2024
1b54b8d7 2025 if (t->operand_types[j].bitfield.regsimd
3ac21baa 2026 && !match_simd_size (t, j, j))
1b54b8d7
JB
2027 {
2028 match = 0;
2029 break;
2030 }
2031
2032 if (t->operand_types[j].bitfield.acc
7a54636a 2033 && (!match_operand_size (t, j, j) || !match_simd_size (t, j, j)))
1b54b8d7
JB
2034 {
2035 match = 0;
2036 break;
2037 }
2038
c48dadc9 2039 if ((i.flags[j] & Operand_Mem) && !match_mem_size (t, j, j))
5c07affc
L
2040 {
2041 match = 0;
2042 break;
2043 }
2044 }
2045
3ac21baa 2046 if (!t->opcode_modifier.d)
891edac4
L
2047 {
2048mismatch:
3ac21baa
JB
2049 if (!match)
2050 i.error = operand_size_mismatch;
2051 return match;
891edac4 2052 }
5c07affc
L
2053
2054 /* Check reverse. */
9c2799c2 2055 gas_assert (i.operands == 2);
5c07affc 2056
5c07affc
L
2057 for (j = 0; j < 2; j++)
2058 {
dc821c5f
JB
2059 if ((t->operand_types[j].bitfield.reg
2060 || t->operand_types[j].bitfield.acc)
7a54636a 2061 && !match_operand_size (t, j, !j))
891edac4 2062 goto mismatch;
5c07affc 2063
c48dadc9 2064 if ((i.flags[!j] & Operand_Mem) && !match_mem_size (t, j, !j))
891edac4 2065 goto mismatch;
5c07affc
L
2066 }
2067
3ac21baa 2068 return match | MATCH_REVERSE;
5c07affc
L
2069}
2070
c6fb90c8 2071static INLINE int
40fb9820
L
2072operand_type_match (i386_operand_type overlap,
2073 i386_operand_type given)
2074{
2075 i386_operand_type temp = overlap;
2076
2077 temp.bitfield.jumpabsolute = 0;
7d5e4556 2078 temp.bitfield.unspecified = 0;
5c07affc
L
2079 temp.bitfield.byte = 0;
2080 temp.bitfield.word = 0;
2081 temp.bitfield.dword = 0;
2082 temp.bitfield.fword = 0;
2083 temp.bitfield.qword = 0;
2084 temp.bitfield.tbyte = 0;
2085 temp.bitfield.xmmword = 0;
c0f3af97 2086 temp.bitfield.ymmword = 0;
43234a1e 2087 temp.bitfield.zmmword = 0;
0dfbf9d7 2088 if (operand_type_all_zero (&temp))
891edac4 2089 goto mismatch;
40fb9820 2090
891edac4
L
2091 if (given.bitfield.baseindex == overlap.bitfield.baseindex
2092 && given.bitfield.jumpabsolute == overlap.bitfield.jumpabsolute)
2093 return 1;
2094
2095mismatch:
a65babc9 2096 i.error = operand_type_mismatch;
891edac4 2097 return 0;
40fb9820
L
2098}
2099
7d5e4556 2100/* If given types g0 and g1 are registers they must be of the same type
10c17abd
JB
2101 unless the expected operand type register overlap is null.
2102 Memory operand size of certain SIMD instructions is also being checked
2103 here. */
40fb9820 2104
c6fb90c8 2105static INLINE int
dc821c5f 2106operand_type_register_match (i386_operand_type g0,
40fb9820 2107 i386_operand_type t0,
40fb9820
L
2108 i386_operand_type g1,
2109 i386_operand_type t1)
2110{
10c17abd
JB
2111 if (!g0.bitfield.reg
2112 && !g0.bitfield.regsimd
2113 && (!operand_type_check (g0, anymem)
2114 || g0.bitfield.unspecified
2115 || !t0.bitfield.regsimd))
40fb9820
L
2116 return 1;
2117
10c17abd
JB
2118 if (!g1.bitfield.reg
2119 && !g1.bitfield.regsimd
2120 && (!operand_type_check (g1, anymem)
2121 || g1.bitfield.unspecified
2122 || !t1.bitfield.regsimd))
40fb9820
L
2123 return 1;
2124
dc821c5f
JB
2125 if (g0.bitfield.byte == g1.bitfield.byte
2126 && g0.bitfield.word == g1.bitfield.word
2127 && g0.bitfield.dword == g1.bitfield.dword
10c17abd
JB
2128 && g0.bitfield.qword == g1.bitfield.qword
2129 && g0.bitfield.xmmword == g1.bitfield.xmmword
2130 && g0.bitfield.ymmword == g1.bitfield.ymmword
2131 && g0.bitfield.zmmword == g1.bitfield.zmmword)
40fb9820
L
2132 return 1;
2133
dc821c5f
JB
2134 if (!(t0.bitfield.byte & t1.bitfield.byte)
2135 && !(t0.bitfield.word & t1.bitfield.word)
2136 && !(t0.bitfield.dword & t1.bitfield.dword)
10c17abd
JB
2137 && !(t0.bitfield.qword & t1.bitfield.qword)
2138 && !(t0.bitfield.xmmword & t1.bitfield.xmmword)
2139 && !(t0.bitfield.ymmword & t1.bitfield.ymmword)
2140 && !(t0.bitfield.zmmword & t1.bitfield.zmmword))
891edac4
L
2141 return 1;
2142
a65babc9 2143 i.error = register_type_mismatch;
891edac4
L
2144
2145 return 0;
40fb9820
L
2146}
2147
4c692bc7
JB
2148static INLINE unsigned int
2149register_number (const reg_entry *r)
2150{
2151 unsigned int nr = r->reg_num;
2152
2153 if (r->reg_flags & RegRex)
2154 nr += 8;
2155
200cbe0f
L
2156 if (r->reg_flags & RegVRex)
2157 nr += 16;
2158
4c692bc7
JB
2159 return nr;
2160}
2161
252b5132 2162static INLINE unsigned int
40fb9820 2163mode_from_disp_size (i386_operand_type t)
252b5132 2164{
b5014f7a 2165 if (t.bitfield.disp8)
40fb9820
L
2166 return 1;
2167 else if (t.bitfield.disp16
2168 || t.bitfield.disp32
2169 || t.bitfield.disp32s)
2170 return 2;
2171 else
2172 return 0;
252b5132
RH
2173}
2174
2175static INLINE int
65879393 2176fits_in_signed_byte (addressT num)
252b5132 2177{
65879393 2178 return num + 0x80 <= 0xff;
47926f60 2179}
252b5132
RH
2180
2181static INLINE int
65879393 2182fits_in_unsigned_byte (addressT num)
252b5132 2183{
65879393 2184 return num <= 0xff;
47926f60 2185}
252b5132
RH
2186
2187static INLINE int
65879393 2188fits_in_unsigned_word (addressT num)
252b5132 2189{
65879393 2190 return num <= 0xffff;
47926f60 2191}
252b5132
RH
2192
2193static INLINE int
65879393 2194fits_in_signed_word (addressT num)
252b5132 2195{
65879393 2196 return num + 0x8000 <= 0xffff;
47926f60 2197}
2a962e6d 2198
3e73aa7c 2199static INLINE int
65879393 2200fits_in_signed_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2201{
2202#ifndef BFD64
2203 return 1;
2204#else
65879393 2205 return num + 0x80000000 <= 0xffffffff;
3e73aa7c
JH
2206#endif
2207} /* fits_in_signed_long() */
2a962e6d 2208
3e73aa7c 2209static INLINE int
65879393 2210fits_in_unsigned_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2211{
2212#ifndef BFD64
2213 return 1;
2214#else
65879393 2215 return num <= 0xffffffff;
3e73aa7c
JH
2216#endif
2217} /* fits_in_unsigned_long() */
252b5132 2218
43234a1e 2219static INLINE int
b5014f7a 2220fits_in_disp8 (offsetT num)
43234a1e
L
2221{
2222 int shift = i.memshift;
2223 unsigned int mask;
2224
2225 if (shift == -1)
2226 abort ();
2227
2228 mask = (1 << shift) - 1;
2229
2230 /* Return 0 if NUM isn't properly aligned. */
2231 if ((num & mask))
2232 return 0;
2233
2234 /* Check if NUM will fit in 8bit after shift. */
2235 return fits_in_signed_byte (num >> shift);
2236}
2237
a683cc34
SP
2238static INLINE int
2239fits_in_imm4 (offsetT num)
2240{
2241 return (num & 0xf) == num;
2242}
2243
40fb9820 2244static i386_operand_type
e3bb37b5 2245smallest_imm_type (offsetT num)
252b5132 2246{
40fb9820 2247 i386_operand_type t;
7ab9ffdd 2248
0dfbf9d7 2249 operand_type_set (&t, 0);
40fb9820
L
2250 t.bitfield.imm64 = 1;
2251
2252 if (cpu_arch_tune != PROCESSOR_I486 && num == 1)
e413e4e9
AM
2253 {
2254 /* This code is disabled on the 486 because all the Imm1 forms
2255 in the opcode table are slower on the i486. They're the
2256 versions with the implicitly specified single-position
2257 displacement, which has another syntax if you really want to
2258 use that form. */
40fb9820
L
2259 t.bitfield.imm1 = 1;
2260 t.bitfield.imm8 = 1;
2261 t.bitfield.imm8s = 1;
2262 t.bitfield.imm16 = 1;
2263 t.bitfield.imm32 = 1;
2264 t.bitfield.imm32s = 1;
2265 }
2266 else if (fits_in_signed_byte (num))
2267 {
2268 t.bitfield.imm8 = 1;
2269 t.bitfield.imm8s = 1;
2270 t.bitfield.imm16 = 1;
2271 t.bitfield.imm32 = 1;
2272 t.bitfield.imm32s = 1;
2273 }
2274 else if (fits_in_unsigned_byte (num))
2275 {
2276 t.bitfield.imm8 = 1;
2277 t.bitfield.imm16 = 1;
2278 t.bitfield.imm32 = 1;
2279 t.bitfield.imm32s = 1;
2280 }
2281 else if (fits_in_signed_word (num) || fits_in_unsigned_word (num))
2282 {
2283 t.bitfield.imm16 = 1;
2284 t.bitfield.imm32 = 1;
2285 t.bitfield.imm32s = 1;
2286 }
2287 else if (fits_in_signed_long (num))
2288 {
2289 t.bitfield.imm32 = 1;
2290 t.bitfield.imm32s = 1;
2291 }
2292 else if (fits_in_unsigned_long (num))
2293 t.bitfield.imm32 = 1;
2294
2295 return t;
47926f60 2296}
252b5132 2297
847f7ad4 2298static offsetT
e3bb37b5 2299offset_in_range (offsetT val, int size)
847f7ad4 2300{
508866be 2301 addressT mask;
ba2adb93 2302
847f7ad4
AM
2303 switch (size)
2304 {
508866be
L
2305 case 1: mask = ((addressT) 1 << 8) - 1; break;
2306 case 2: mask = ((addressT) 1 << 16) - 1; break;
3b0ec529 2307 case 4: mask = ((addressT) 2 << 31) - 1; break;
3e73aa7c
JH
2308#ifdef BFD64
2309 case 8: mask = ((addressT) 2 << 63) - 1; break;
2310#endif
47926f60 2311 default: abort ();
847f7ad4
AM
2312 }
2313
9de868bf
L
2314#ifdef BFD64
2315 /* If BFD64, sign extend val for 32bit address mode. */
2316 if (flag_code != CODE_64BIT
2317 || i.prefix[ADDR_PREFIX])
3e73aa7c
JH
2318 if ((val & ~(((addressT) 2 << 31) - 1)) == 0)
2319 val = (val ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
fa289fb8 2320#endif
ba2adb93 2321
47926f60 2322 if ((val & ~mask) != 0 && (val & ~mask) != ~mask)
847f7ad4
AM
2323 {
2324 char buf1[40], buf2[40];
2325
2326 sprint_value (buf1, val);
2327 sprint_value (buf2, val & mask);
2328 as_warn (_("%s shortened to %s"), buf1, buf2);
2329 }
2330 return val & mask;
2331}
2332
c32fa91d
L
2333enum PREFIX_GROUP
2334{
2335 PREFIX_EXIST = 0,
2336 PREFIX_LOCK,
2337 PREFIX_REP,
04ef582a 2338 PREFIX_DS,
c32fa91d
L
2339 PREFIX_OTHER
2340};
2341
2342/* Returns
2343 a. PREFIX_EXIST if attempting to add a prefix where one from the
2344 same class already exists.
2345 b. PREFIX_LOCK if lock prefix is added.
2346 c. PREFIX_REP if rep/repne prefix is added.
04ef582a
L
2347 d. PREFIX_DS if ds prefix is added.
2348 e. PREFIX_OTHER if other prefix is added.
c32fa91d
L
2349 */
2350
2351static enum PREFIX_GROUP
e3bb37b5 2352add_prefix (unsigned int prefix)
252b5132 2353{
c32fa91d 2354 enum PREFIX_GROUP ret = PREFIX_OTHER;
b1905489 2355 unsigned int q;
252b5132 2356
29b0f896
AM
2357 if (prefix >= REX_OPCODE && prefix < REX_OPCODE + 16
2358 && flag_code == CODE_64BIT)
b1905489 2359 {
161a04f6 2360 if ((i.prefix[REX_PREFIX] & prefix & REX_W)
44846f29
JB
2361 || (i.prefix[REX_PREFIX] & prefix & REX_R)
2362 || (i.prefix[REX_PREFIX] & prefix & REX_X)
2363 || (i.prefix[REX_PREFIX] & prefix & REX_B))
c32fa91d 2364 ret = PREFIX_EXIST;
b1905489
JB
2365 q = REX_PREFIX;
2366 }
3e73aa7c 2367 else
b1905489
JB
2368 {
2369 switch (prefix)
2370 {
2371 default:
2372 abort ();
2373
b1905489 2374 case DS_PREFIX_OPCODE:
04ef582a
L
2375 ret = PREFIX_DS;
2376 /* Fall through. */
2377 case CS_PREFIX_OPCODE:
b1905489
JB
2378 case ES_PREFIX_OPCODE:
2379 case FS_PREFIX_OPCODE:
2380 case GS_PREFIX_OPCODE:
2381 case SS_PREFIX_OPCODE:
2382 q = SEG_PREFIX;
2383 break;
2384
2385 case REPNE_PREFIX_OPCODE:
2386 case REPE_PREFIX_OPCODE:
c32fa91d
L
2387 q = REP_PREFIX;
2388 ret = PREFIX_REP;
2389 break;
2390
b1905489 2391 case LOCK_PREFIX_OPCODE:
c32fa91d
L
2392 q = LOCK_PREFIX;
2393 ret = PREFIX_LOCK;
b1905489
JB
2394 break;
2395
2396 case FWAIT_OPCODE:
2397 q = WAIT_PREFIX;
2398 break;
2399
2400 case ADDR_PREFIX_OPCODE:
2401 q = ADDR_PREFIX;
2402 break;
2403
2404 case DATA_PREFIX_OPCODE:
2405 q = DATA_PREFIX;
2406 break;
2407 }
2408 if (i.prefix[q] != 0)
c32fa91d 2409 ret = PREFIX_EXIST;
b1905489 2410 }
252b5132 2411
b1905489 2412 if (ret)
252b5132 2413 {
b1905489
JB
2414 if (!i.prefix[q])
2415 ++i.prefixes;
2416 i.prefix[q] |= prefix;
252b5132 2417 }
b1905489
JB
2418 else
2419 as_bad (_("same type of prefix used twice"));
252b5132 2420
252b5132
RH
2421 return ret;
2422}
2423
2424static void
78f12dd3 2425update_code_flag (int value, int check)
eecb386c 2426{
78f12dd3
L
2427 PRINTF_LIKE ((*as_error));
2428
1e9cc1c2 2429 flag_code = (enum flag_code) value;
40fb9820
L
2430 if (flag_code == CODE_64BIT)
2431 {
2432 cpu_arch_flags.bitfield.cpu64 = 1;
2433 cpu_arch_flags.bitfield.cpuno64 = 0;
40fb9820
L
2434 }
2435 else
2436 {
2437 cpu_arch_flags.bitfield.cpu64 = 0;
2438 cpu_arch_flags.bitfield.cpuno64 = 1;
40fb9820
L
2439 }
2440 if (value == CODE_64BIT && !cpu_arch_flags.bitfield.cpulm )
3e73aa7c 2441 {
78f12dd3
L
2442 if (check)
2443 as_error = as_fatal;
2444 else
2445 as_error = as_bad;
2446 (*as_error) (_("64bit mode not supported on `%s'."),
2447 cpu_arch_name ? cpu_arch_name : default_arch);
3e73aa7c 2448 }
40fb9820 2449 if (value == CODE_32BIT && !cpu_arch_flags.bitfield.cpui386)
3e73aa7c 2450 {
78f12dd3
L
2451 if (check)
2452 as_error = as_fatal;
2453 else
2454 as_error = as_bad;
2455 (*as_error) (_("32bit mode not supported on `%s'."),
2456 cpu_arch_name ? cpu_arch_name : default_arch);
3e73aa7c 2457 }
eecb386c
AM
2458 stackop_size = '\0';
2459}
2460
78f12dd3
L
2461static void
2462set_code_flag (int value)
2463{
2464 update_code_flag (value, 0);
2465}
2466
eecb386c 2467static void
e3bb37b5 2468set_16bit_gcc_code_flag (int new_code_flag)
252b5132 2469{
1e9cc1c2 2470 flag_code = (enum flag_code) new_code_flag;
40fb9820
L
2471 if (flag_code != CODE_16BIT)
2472 abort ();
2473 cpu_arch_flags.bitfield.cpu64 = 0;
2474 cpu_arch_flags.bitfield.cpuno64 = 1;
9306ca4a 2475 stackop_size = LONG_MNEM_SUFFIX;
252b5132
RH
2476}
2477
2478static void
e3bb37b5 2479set_intel_syntax (int syntax_flag)
252b5132
RH
2480{
2481 /* Find out if register prefixing is specified. */
2482 int ask_naked_reg = 0;
2483
2484 SKIP_WHITESPACE ();
29b0f896 2485 if (!is_end_of_line[(unsigned char) *input_line_pointer])
252b5132 2486 {
d02603dc
NC
2487 char *string;
2488 int e = get_symbol_name (&string);
252b5132 2489
47926f60 2490 if (strcmp (string, "prefix") == 0)
252b5132 2491 ask_naked_reg = 1;
47926f60 2492 else if (strcmp (string, "noprefix") == 0)
252b5132
RH
2493 ask_naked_reg = -1;
2494 else
d0b47220 2495 as_bad (_("bad argument to syntax directive."));
d02603dc 2496 (void) restore_line_pointer (e);
252b5132
RH
2497 }
2498 demand_empty_rest_of_line ();
c3332e24 2499
252b5132
RH
2500 intel_syntax = syntax_flag;
2501
2502 if (ask_naked_reg == 0)
f86103b7
AM
2503 allow_naked_reg = (intel_syntax
2504 && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
252b5132
RH
2505 else
2506 allow_naked_reg = (ask_naked_reg < 0);
9306ca4a 2507
ee86248c 2508 expr_set_rank (O_full_ptr, syntax_flag ? 10 : 0);
7ab9ffdd 2509
e4a3b5a4 2510 identifier_chars['%'] = intel_syntax && allow_naked_reg ? '%' : 0;
9306ca4a 2511 identifier_chars['$'] = intel_syntax ? '$' : 0;
e4a3b5a4 2512 register_prefix = allow_naked_reg ? "" : "%";
252b5132
RH
2513}
2514
1efbbeb4
L
2515static void
2516set_intel_mnemonic (int mnemonic_flag)
2517{
e1d4d893 2518 intel_mnemonic = mnemonic_flag;
1efbbeb4
L
2519}
2520
db51cc60
L
2521static void
2522set_allow_index_reg (int flag)
2523{
2524 allow_index_reg = flag;
2525}
2526
cb19c032 2527static void
7bab8ab5 2528set_check (int what)
cb19c032 2529{
7bab8ab5
JB
2530 enum check_kind *kind;
2531 const char *str;
2532
2533 if (what)
2534 {
2535 kind = &operand_check;
2536 str = "operand";
2537 }
2538 else
2539 {
2540 kind = &sse_check;
2541 str = "sse";
2542 }
2543
cb19c032
L
2544 SKIP_WHITESPACE ();
2545
2546 if (!is_end_of_line[(unsigned char) *input_line_pointer])
2547 {
d02603dc
NC
2548 char *string;
2549 int e = get_symbol_name (&string);
cb19c032
L
2550
2551 if (strcmp (string, "none") == 0)
7bab8ab5 2552 *kind = check_none;
cb19c032 2553 else if (strcmp (string, "warning") == 0)
7bab8ab5 2554 *kind = check_warning;
cb19c032 2555 else if (strcmp (string, "error") == 0)
7bab8ab5 2556 *kind = check_error;
cb19c032 2557 else
7bab8ab5 2558 as_bad (_("bad argument to %s_check directive."), str);
d02603dc 2559 (void) restore_line_pointer (e);
cb19c032
L
2560 }
2561 else
7bab8ab5 2562 as_bad (_("missing argument for %s_check directive"), str);
cb19c032
L
2563
2564 demand_empty_rest_of_line ();
2565}
2566
8a9036a4
L
2567static void
2568check_cpu_arch_compatible (const char *name ATTRIBUTE_UNUSED,
1e9cc1c2 2569 i386_cpu_flags new_flag ATTRIBUTE_UNUSED)
8a9036a4
L
2570{
2571#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2572 static const char *arch;
2573
2574 /* Intel LIOM is only supported on ELF. */
2575 if (!IS_ELF)
2576 return;
2577
2578 if (!arch)
2579 {
2580 /* Use cpu_arch_name if it is set in md_parse_option. Otherwise
2581 use default_arch. */
2582 arch = cpu_arch_name;
2583 if (!arch)
2584 arch = default_arch;
2585 }
2586
81486035
L
2587 /* If we are targeting Intel MCU, we must enable it. */
2588 if (get_elf_backend_data (stdoutput)->elf_machine_code != EM_IAMCU
2589 || new_flag.bitfield.cpuiamcu)
2590 return;
2591
3632d14b 2592 /* If we are targeting Intel L1OM, we must enable it. */
8a9036a4 2593 if (get_elf_backend_data (stdoutput)->elf_machine_code != EM_L1OM
1e9cc1c2 2594 || new_flag.bitfield.cpul1om)
8a9036a4 2595 return;
76ba9986 2596
7a9068fe
L
2597 /* If we are targeting Intel K1OM, we must enable it. */
2598 if (get_elf_backend_data (stdoutput)->elf_machine_code != EM_K1OM
2599 || new_flag.bitfield.cpuk1om)
2600 return;
2601
8a9036a4
L
2602 as_bad (_("`%s' is not supported on `%s'"), name, arch);
2603#endif
2604}
2605
e413e4e9 2606static void
e3bb37b5 2607set_cpu_arch (int dummy ATTRIBUTE_UNUSED)
e413e4e9 2608{
47926f60 2609 SKIP_WHITESPACE ();
e413e4e9 2610
29b0f896 2611 if (!is_end_of_line[(unsigned char) *input_line_pointer])
e413e4e9 2612 {
d02603dc
NC
2613 char *string;
2614 int e = get_symbol_name (&string);
91d6fa6a 2615 unsigned int j;
40fb9820 2616 i386_cpu_flags flags;
e413e4e9 2617
91d6fa6a 2618 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
e413e4e9 2619 {
91d6fa6a 2620 if (strcmp (string, cpu_arch[j].name) == 0)
e413e4e9 2621 {
91d6fa6a 2622 check_cpu_arch_compatible (string, cpu_arch[j].flags);
8a9036a4 2623
5c6af06e
JB
2624 if (*string != '.')
2625 {
91d6fa6a 2626 cpu_arch_name = cpu_arch[j].name;
5c6af06e 2627 cpu_sub_arch_name = NULL;
91d6fa6a 2628 cpu_arch_flags = cpu_arch[j].flags;
40fb9820
L
2629 if (flag_code == CODE_64BIT)
2630 {
2631 cpu_arch_flags.bitfield.cpu64 = 1;
2632 cpu_arch_flags.bitfield.cpuno64 = 0;
2633 }
2634 else
2635 {
2636 cpu_arch_flags.bitfield.cpu64 = 0;
2637 cpu_arch_flags.bitfield.cpuno64 = 1;
2638 }
91d6fa6a
NC
2639 cpu_arch_isa = cpu_arch[j].type;
2640 cpu_arch_isa_flags = cpu_arch[j].flags;
ccc9c027
L
2641 if (!cpu_arch_tune_set)
2642 {
2643 cpu_arch_tune = cpu_arch_isa;
2644 cpu_arch_tune_flags = cpu_arch_isa_flags;
2645 }
5c6af06e
JB
2646 break;
2647 }
40fb9820 2648
293f5f65
L
2649 flags = cpu_flags_or (cpu_arch_flags,
2650 cpu_arch[j].flags);
81486035 2651
5b64d091 2652 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
5c6af06e 2653 {
6305a203
L
2654 if (cpu_sub_arch_name)
2655 {
2656 char *name = cpu_sub_arch_name;
2657 cpu_sub_arch_name = concat (name,
91d6fa6a 2658 cpu_arch[j].name,
1bf57e9f 2659 (const char *) NULL);
6305a203
L
2660 free (name);
2661 }
2662 else
91d6fa6a 2663 cpu_sub_arch_name = xstrdup (cpu_arch[j].name);
40fb9820 2664 cpu_arch_flags = flags;
a586129e 2665 cpu_arch_isa_flags = flags;
5c6af06e 2666 }
0089dace
L
2667 else
2668 cpu_arch_isa_flags
2669 = cpu_flags_or (cpu_arch_isa_flags,
2670 cpu_arch[j].flags);
d02603dc 2671 (void) restore_line_pointer (e);
5c6af06e
JB
2672 demand_empty_rest_of_line ();
2673 return;
e413e4e9
AM
2674 }
2675 }
293f5f65
L
2676
2677 if (*string == '.' && j >= ARRAY_SIZE (cpu_arch))
2678 {
33eaf5de 2679 /* Disable an ISA extension. */
293f5f65
L
2680 for (j = 0; j < ARRAY_SIZE (cpu_noarch); j++)
2681 if (strcmp (string + 1, cpu_noarch [j].name) == 0)
2682 {
2683 flags = cpu_flags_and_not (cpu_arch_flags,
2684 cpu_noarch[j].flags);
2685 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
2686 {
2687 if (cpu_sub_arch_name)
2688 {
2689 char *name = cpu_sub_arch_name;
2690 cpu_sub_arch_name = concat (name, string,
2691 (const char *) NULL);
2692 free (name);
2693 }
2694 else
2695 cpu_sub_arch_name = xstrdup (string);
2696 cpu_arch_flags = flags;
2697 cpu_arch_isa_flags = flags;
2698 }
2699 (void) restore_line_pointer (e);
2700 demand_empty_rest_of_line ();
2701 return;
2702 }
2703
2704 j = ARRAY_SIZE (cpu_arch);
2705 }
2706
91d6fa6a 2707 if (j >= ARRAY_SIZE (cpu_arch))
e413e4e9
AM
2708 as_bad (_("no such architecture: `%s'"), string);
2709
2710 *input_line_pointer = e;
2711 }
2712 else
2713 as_bad (_("missing cpu architecture"));
2714
fddf5b5b
AM
2715 no_cond_jump_promotion = 0;
2716 if (*input_line_pointer == ','
29b0f896 2717 && !is_end_of_line[(unsigned char) input_line_pointer[1]])
fddf5b5b 2718 {
d02603dc
NC
2719 char *string;
2720 char e;
2721
2722 ++input_line_pointer;
2723 e = get_symbol_name (&string);
fddf5b5b
AM
2724
2725 if (strcmp (string, "nojumps") == 0)
2726 no_cond_jump_promotion = 1;
2727 else if (strcmp (string, "jumps") == 0)
2728 ;
2729 else
2730 as_bad (_("no such architecture modifier: `%s'"), string);
2731
d02603dc 2732 (void) restore_line_pointer (e);
fddf5b5b
AM
2733 }
2734
e413e4e9
AM
2735 demand_empty_rest_of_line ();
2736}
2737
8a9036a4
L
2738enum bfd_architecture
2739i386_arch (void)
2740{
3632d14b 2741 if (cpu_arch_isa == PROCESSOR_L1OM)
8a9036a4
L
2742 {
2743 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2744 || flag_code != CODE_64BIT)
2745 as_fatal (_("Intel L1OM is 64bit ELF only"));
2746 return bfd_arch_l1om;
2747 }
7a9068fe
L
2748 else if (cpu_arch_isa == PROCESSOR_K1OM)
2749 {
2750 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2751 || flag_code != CODE_64BIT)
2752 as_fatal (_("Intel K1OM is 64bit ELF only"));
2753 return bfd_arch_k1om;
2754 }
81486035
L
2755 else if (cpu_arch_isa == PROCESSOR_IAMCU)
2756 {
2757 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2758 || flag_code == CODE_64BIT)
2759 as_fatal (_("Intel MCU is 32bit ELF only"));
2760 return bfd_arch_iamcu;
2761 }
8a9036a4
L
2762 else
2763 return bfd_arch_i386;
2764}
2765
b9d79e03 2766unsigned long
7016a5d5 2767i386_mach (void)
b9d79e03 2768{
351f65ca 2769 if (!strncmp (default_arch, "x86_64", 6))
8a9036a4 2770 {
3632d14b 2771 if (cpu_arch_isa == PROCESSOR_L1OM)
8a9036a4 2772 {
351f65ca
L
2773 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2774 || default_arch[6] != '\0')
8a9036a4
L
2775 as_fatal (_("Intel L1OM is 64bit ELF only"));
2776 return bfd_mach_l1om;
2777 }
7a9068fe
L
2778 else if (cpu_arch_isa == PROCESSOR_K1OM)
2779 {
2780 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2781 || default_arch[6] != '\0')
2782 as_fatal (_("Intel K1OM is 64bit ELF only"));
2783 return bfd_mach_k1om;
2784 }
351f65ca 2785 else if (default_arch[6] == '\0')
8a9036a4 2786 return bfd_mach_x86_64;
351f65ca
L
2787 else
2788 return bfd_mach_x64_32;
8a9036a4 2789 }
5197d474
L
2790 else if (!strcmp (default_arch, "i386")
2791 || !strcmp (default_arch, "iamcu"))
81486035
L
2792 {
2793 if (cpu_arch_isa == PROCESSOR_IAMCU)
2794 {
2795 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
2796 as_fatal (_("Intel MCU is 32bit ELF only"));
2797 return bfd_mach_i386_iamcu;
2798 }
2799 else
2800 return bfd_mach_i386_i386;
2801 }
b9d79e03 2802 else
2b5d6a91 2803 as_fatal (_("unknown architecture"));
b9d79e03 2804}
b9d79e03 2805\f
252b5132 2806void
7016a5d5 2807md_begin (void)
252b5132
RH
2808{
2809 const char *hash_err;
2810
86fa6981
L
2811 /* Support pseudo prefixes like {disp32}. */
2812 lex_type ['{'] = LEX_BEGIN_NAME;
2813
47926f60 2814 /* Initialize op_hash hash table. */
252b5132
RH
2815 op_hash = hash_new ();
2816
2817 {
d3ce72d0 2818 const insn_template *optab;
29b0f896 2819 templates *core_optab;
252b5132 2820
47926f60
KH
2821 /* Setup for loop. */
2822 optab = i386_optab;
add39d23 2823 core_optab = XNEW (templates);
252b5132
RH
2824 core_optab->start = optab;
2825
2826 while (1)
2827 {
2828 ++optab;
2829 if (optab->name == NULL
2830 || strcmp (optab->name, (optab - 1)->name) != 0)
2831 {
2832 /* different name --> ship out current template list;
47926f60 2833 add to hash table; & begin anew. */
252b5132
RH
2834 core_optab->end = optab;
2835 hash_err = hash_insert (op_hash,
2836 (optab - 1)->name,
5a49b8ac 2837 (void *) core_optab);
252b5132
RH
2838 if (hash_err)
2839 {
b37df7c4 2840 as_fatal (_("can't hash %s: %s"),
252b5132
RH
2841 (optab - 1)->name,
2842 hash_err);
2843 }
2844 if (optab->name == NULL)
2845 break;
add39d23 2846 core_optab = XNEW (templates);
252b5132
RH
2847 core_optab->start = optab;
2848 }
2849 }
2850 }
2851
47926f60 2852 /* Initialize reg_hash hash table. */
252b5132
RH
2853 reg_hash = hash_new ();
2854 {
29b0f896 2855 const reg_entry *regtab;
c3fe08fa 2856 unsigned int regtab_size = i386_regtab_size;
252b5132 2857
c3fe08fa 2858 for (regtab = i386_regtab; regtab_size--; regtab++)
252b5132 2859 {
5a49b8ac 2860 hash_err = hash_insert (reg_hash, regtab->reg_name, (void *) regtab);
252b5132 2861 if (hash_err)
b37df7c4 2862 as_fatal (_("can't hash %s: %s"),
3e73aa7c
JH
2863 regtab->reg_name,
2864 hash_err);
252b5132
RH
2865 }
2866 }
2867
47926f60 2868 /* Fill in lexical tables: mnemonic_chars, operand_chars. */
252b5132 2869 {
29b0f896
AM
2870 int c;
2871 char *p;
252b5132
RH
2872
2873 for (c = 0; c < 256; c++)
2874 {
3882b010 2875 if (ISDIGIT (c))
252b5132
RH
2876 {
2877 digit_chars[c] = c;
2878 mnemonic_chars[c] = c;
2879 register_chars[c] = c;
2880 operand_chars[c] = c;
2881 }
3882b010 2882 else if (ISLOWER (c))
252b5132
RH
2883 {
2884 mnemonic_chars[c] = c;
2885 register_chars[c] = c;
2886 operand_chars[c] = c;
2887 }
3882b010 2888 else if (ISUPPER (c))
252b5132 2889 {
3882b010 2890 mnemonic_chars[c] = TOLOWER (c);
252b5132
RH
2891 register_chars[c] = mnemonic_chars[c];
2892 operand_chars[c] = c;
2893 }
43234a1e 2894 else if (c == '{' || c == '}')
86fa6981
L
2895 {
2896 mnemonic_chars[c] = c;
2897 operand_chars[c] = c;
2898 }
252b5132 2899
3882b010 2900 if (ISALPHA (c) || ISDIGIT (c))
252b5132
RH
2901 identifier_chars[c] = c;
2902 else if (c >= 128)
2903 {
2904 identifier_chars[c] = c;
2905 operand_chars[c] = c;
2906 }
2907 }
2908
2909#ifdef LEX_AT
2910 identifier_chars['@'] = '@';
32137342
NC
2911#endif
2912#ifdef LEX_QM
2913 identifier_chars['?'] = '?';
2914 operand_chars['?'] = '?';
252b5132 2915#endif
252b5132 2916 digit_chars['-'] = '-';
c0f3af97 2917 mnemonic_chars['_'] = '_';
791fe849 2918 mnemonic_chars['-'] = '-';
0003779b 2919 mnemonic_chars['.'] = '.';
252b5132
RH
2920 identifier_chars['_'] = '_';
2921 identifier_chars['.'] = '.';
2922
2923 for (p = operand_special_chars; *p != '\0'; p++)
2924 operand_chars[(unsigned char) *p] = *p;
2925 }
2926
a4447b93
RH
2927 if (flag_code == CODE_64BIT)
2928 {
ca19b261
KT
2929#if defined (OBJ_COFF) && defined (TE_PE)
2930 x86_dwarf2_return_column = (OUTPUT_FLAVOR == bfd_target_coff_flavour
2931 ? 32 : 16);
2932#else
a4447b93 2933 x86_dwarf2_return_column = 16;
ca19b261 2934#endif
61ff971f 2935 x86_cie_data_alignment = -8;
a4447b93
RH
2936 }
2937 else
2938 {
2939 x86_dwarf2_return_column = 8;
2940 x86_cie_data_alignment = -4;
2941 }
252b5132
RH
2942}
2943
2944void
e3bb37b5 2945i386_print_statistics (FILE *file)
252b5132
RH
2946{
2947 hash_print_statistics (file, "i386 opcode", op_hash);
2948 hash_print_statistics (file, "i386 register", reg_hash);
2949}
2950\f
252b5132
RH
2951#ifdef DEBUG386
2952
ce8a8b2f 2953/* Debugging routines for md_assemble. */
d3ce72d0 2954static void pte (insn_template *);
40fb9820 2955static void pt (i386_operand_type);
e3bb37b5
L
2956static void pe (expressionS *);
2957static void ps (symbolS *);
252b5132
RH
2958
2959static void
e3bb37b5 2960pi (char *line, i386_insn *x)
252b5132 2961{
09137c09 2962 unsigned int j;
252b5132
RH
2963
2964 fprintf (stdout, "%s: template ", line);
2965 pte (&x->tm);
09f131f2
JH
2966 fprintf (stdout, " address: base %s index %s scale %x\n",
2967 x->base_reg ? x->base_reg->reg_name : "none",
2968 x->index_reg ? x->index_reg->reg_name : "none",
2969 x->log2_scale_factor);
2970 fprintf (stdout, " modrm: mode %x reg %x reg/mem %x\n",
252b5132 2971 x->rm.mode, x->rm.reg, x->rm.regmem);
09f131f2
JH
2972 fprintf (stdout, " sib: base %x index %x scale %x\n",
2973 x->sib.base, x->sib.index, x->sib.scale);
2974 fprintf (stdout, " rex: 64bit %x extX %x extY %x extZ %x\n",
161a04f6
L
2975 (x->rex & REX_W) != 0,
2976 (x->rex & REX_R) != 0,
2977 (x->rex & REX_X) != 0,
2978 (x->rex & REX_B) != 0);
09137c09 2979 for (j = 0; j < x->operands; j++)
252b5132 2980 {
09137c09
SP
2981 fprintf (stdout, " #%d: ", j + 1);
2982 pt (x->types[j]);
252b5132 2983 fprintf (stdout, "\n");
dc821c5f 2984 if (x->types[j].bitfield.reg
09137c09 2985 || x->types[j].bitfield.regmmx
1b54b8d7 2986 || x->types[j].bitfield.regsimd
09137c09
SP
2987 || x->types[j].bitfield.sreg2
2988 || x->types[j].bitfield.sreg3
2989 || x->types[j].bitfield.control
2990 || x->types[j].bitfield.debug
2991 || x->types[j].bitfield.test)
2992 fprintf (stdout, "%s\n", x->op[j].regs->reg_name);
2993 if (operand_type_check (x->types[j], imm))
2994 pe (x->op[j].imms);
2995 if (operand_type_check (x->types[j], disp))
2996 pe (x->op[j].disps);
252b5132
RH
2997 }
2998}
2999
3000static void
d3ce72d0 3001pte (insn_template *t)
252b5132 3002{
09137c09 3003 unsigned int j;
252b5132 3004 fprintf (stdout, " %d operands ", t->operands);
47926f60 3005 fprintf (stdout, "opcode %x ", t->base_opcode);
252b5132
RH
3006 if (t->extension_opcode != None)
3007 fprintf (stdout, "ext %x ", t->extension_opcode);
40fb9820 3008 if (t->opcode_modifier.d)
252b5132 3009 fprintf (stdout, "D");
40fb9820 3010 if (t->opcode_modifier.w)
252b5132
RH
3011 fprintf (stdout, "W");
3012 fprintf (stdout, "\n");
09137c09 3013 for (j = 0; j < t->operands; j++)
252b5132 3014 {
09137c09
SP
3015 fprintf (stdout, " #%d type ", j + 1);
3016 pt (t->operand_types[j]);
252b5132
RH
3017 fprintf (stdout, "\n");
3018 }
3019}
3020
3021static void
e3bb37b5 3022pe (expressionS *e)
252b5132 3023{
24eab124 3024 fprintf (stdout, " operation %d\n", e->X_op);
b77ad1d4
AM
3025 fprintf (stdout, " add_number %ld (%lx)\n",
3026 (long) e->X_add_number, (long) e->X_add_number);
252b5132
RH
3027 if (e->X_add_symbol)
3028 {
3029 fprintf (stdout, " add_symbol ");
3030 ps (e->X_add_symbol);
3031 fprintf (stdout, "\n");
3032 }
3033 if (e->X_op_symbol)
3034 {
3035 fprintf (stdout, " op_symbol ");
3036 ps (e->X_op_symbol);
3037 fprintf (stdout, "\n");
3038 }
3039}
3040
3041static void
e3bb37b5 3042ps (symbolS *s)
252b5132
RH
3043{
3044 fprintf (stdout, "%s type %s%s",
3045 S_GET_NAME (s),
3046 S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
3047 segment_name (S_GET_SEGMENT (s)));
3048}
3049
7b81dfbb 3050static struct type_name
252b5132 3051 {
40fb9820
L
3052 i386_operand_type mask;
3053 const char *name;
252b5132 3054 }
7b81dfbb 3055const type_names[] =
252b5132 3056{
40fb9820
L
3057 { OPERAND_TYPE_REG8, "r8" },
3058 { OPERAND_TYPE_REG16, "r16" },
3059 { OPERAND_TYPE_REG32, "r32" },
3060 { OPERAND_TYPE_REG64, "r64" },
3061 { OPERAND_TYPE_IMM8, "i8" },
3062 { OPERAND_TYPE_IMM8, "i8s" },
3063 { OPERAND_TYPE_IMM16, "i16" },
3064 { OPERAND_TYPE_IMM32, "i32" },
3065 { OPERAND_TYPE_IMM32S, "i32s" },
3066 { OPERAND_TYPE_IMM64, "i64" },
3067 { OPERAND_TYPE_IMM1, "i1" },
3068 { OPERAND_TYPE_BASEINDEX, "BaseIndex" },
3069 { OPERAND_TYPE_DISP8, "d8" },
3070 { OPERAND_TYPE_DISP16, "d16" },
3071 { OPERAND_TYPE_DISP32, "d32" },
3072 { OPERAND_TYPE_DISP32S, "d32s" },
3073 { OPERAND_TYPE_DISP64, "d64" },
3074 { OPERAND_TYPE_INOUTPORTREG, "InOutPortReg" },
3075 { OPERAND_TYPE_SHIFTCOUNT, "ShiftCount" },
3076 { OPERAND_TYPE_CONTROL, "control reg" },
3077 { OPERAND_TYPE_TEST, "test reg" },
3078 { OPERAND_TYPE_DEBUG, "debug reg" },
3079 { OPERAND_TYPE_FLOATREG, "FReg" },
3080 { OPERAND_TYPE_FLOATACC, "FAcc" },
3081 { OPERAND_TYPE_SREG2, "SReg2" },
3082 { OPERAND_TYPE_SREG3, "SReg3" },
3083 { OPERAND_TYPE_ACC, "Acc" },
3084 { OPERAND_TYPE_JUMPABSOLUTE, "Jump Absolute" },
3085 { OPERAND_TYPE_REGMMX, "rMMX" },
3086 { OPERAND_TYPE_REGXMM, "rXMM" },
0349dc08 3087 { OPERAND_TYPE_REGYMM, "rYMM" },
43234a1e
L
3088 { OPERAND_TYPE_REGZMM, "rZMM" },
3089 { OPERAND_TYPE_REGMASK, "Mask reg" },
40fb9820 3090 { OPERAND_TYPE_ESSEG, "es" },
252b5132
RH
3091};
3092
3093static void
40fb9820 3094pt (i386_operand_type t)
252b5132 3095{
40fb9820 3096 unsigned int j;
c6fb90c8 3097 i386_operand_type a;
252b5132 3098
40fb9820 3099 for (j = 0; j < ARRAY_SIZE (type_names); j++)
c6fb90c8
L
3100 {
3101 a = operand_type_and (t, type_names[j].mask);
0349dc08 3102 if (!operand_type_all_zero (&a))
c6fb90c8
L
3103 fprintf (stdout, "%s, ", type_names[j].name);
3104 }
252b5132
RH
3105 fflush (stdout);
3106}
3107
3108#endif /* DEBUG386 */
3109\f
252b5132 3110static bfd_reloc_code_real_type
3956db08 3111reloc (unsigned int size,
64e74474
AM
3112 int pcrel,
3113 int sign,
3114 bfd_reloc_code_real_type other)
252b5132 3115{
47926f60 3116 if (other != NO_RELOC)
3956db08 3117 {
91d6fa6a 3118 reloc_howto_type *rel;
3956db08
JB
3119
3120 if (size == 8)
3121 switch (other)
3122 {
64e74474
AM
3123 case BFD_RELOC_X86_64_GOT32:
3124 return BFD_RELOC_X86_64_GOT64;
3125 break;
553d1284
L
3126 case BFD_RELOC_X86_64_GOTPLT64:
3127 return BFD_RELOC_X86_64_GOTPLT64;
3128 break;
64e74474
AM
3129 case BFD_RELOC_X86_64_PLTOFF64:
3130 return BFD_RELOC_X86_64_PLTOFF64;
3131 break;
3132 case BFD_RELOC_X86_64_GOTPC32:
3133 other = BFD_RELOC_X86_64_GOTPC64;
3134 break;
3135 case BFD_RELOC_X86_64_GOTPCREL:
3136 other = BFD_RELOC_X86_64_GOTPCREL64;
3137 break;
3138 case BFD_RELOC_X86_64_TPOFF32:
3139 other = BFD_RELOC_X86_64_TPOFF64;
3140 break;
3141 case BFD_RELOC_X86_64_DTPOFF32:
3142 other = BFD_RELOC_X86_64_DTPOFF64;
3143 break;
3144 default:
3145 break;
3956db08 3146 }
e05278af 3147
8ce3d284 3148#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
3149 if (other == BFD_RELOC_SIZE32)
3150 {
3151 if (size == 8)
1ab668bf 3152 other = BFD_RELOC_SIZE64;
8fd4256d 3153 if (pcrel)
1ab668bf
AM
3154 {
3155 as_bad (_("there are no pc-relative size relocations"));
3156 return NO_RELOC;
3157 }
8fd4256d 3158 }
8ce3d284 3159#endif
8fd4256d 3160
e05278af 3161 /* Sign-checking 4-byte relocations in 16-/32-bit code is pointless. */
f2d8a97c 3162 if (size == 4 && (flag_code != CODE_64BIT || disallow_64bit_reloc))
e05278af
JB
3163 sign = -1;
3164
91d6fa6a
NC
3165 rel = bfd_reloc_type_lookup (stdoutput, other);
3166 if (!rel)
3956db08 3167 as_bad (_("unknown relocation (%u)"), other);
91d6fa6a 3168 else if (size != bfd_get_reloc_size (rel))
3956db08 3169 as_bad (_("%u-byte relocation cannot be applied to %u-byte field"),
91d6fa6a 3170 bfd_get_reloc_size (rel),
3956db08 3171 size);
91d6fa6a 3172 else if (pcrel && !rel->pc_relative)
3956db08 3173 as_bad (_("non-pc-relative relocation for pc-relative field"));
91d6fa6a 3174 else if ((rel->complain_on_overflow == complain_overflow_signed
3956db08 3175 && !sign)
91d6fa6a 3176 || (rel->complain_on_overflow == complain_overflow_unsigned
64e74474 3177 && sign > 0))
3956db08
JB
3178 as_bad (_("relocated field and relocation type differ in signedness"));
3179 else
3180 return other;
3181 return NO_RELOC;
3182 }
252b5132
RH
3183
3184 if (pcrel)
3185 {
3e73aa7c 3186 if (!sign)
3956db08 3187 as_bad (_("there are no unsigned pc-relative relocations"));
252b5132
RH
3188 switch (size)
3189 {
3190 case 1: return BFD_RELOC_8_PCREL;
3191 case 2: return BFD_RELOC_16_PCREL;
d258b828 3192 case 4: return BFD_RELOC_32_PCREL;
d6ab8113 3193 case 8: return BFD_RELOC_64_PCREL;
252b5132 3194 }
3956db08 3195 as_bad (_("cannot do %u byte pc-relative relocation"), size);
252b5132
RH
3196 }
3197 else
3198 {
3956db08 3199 if (sign > 0)
e5cb08ac 3200 switch (size)
3e73aa7c
JH
3201 {
3202 case 4: return BFD_RELOC_X86_64_32S;
3203 }
3204 else
3205 switch (size)
3206 {
3207 case 1: return BFD_RELOC_8;
3208 case 2: return BFD_RELOC_16;
3209 case 4: return BFD_RELOC_32;
3210 case 8: return BFD_RELOC_64;
3211 }
3956db08
JB
3212 as_bad (_("cannot do %s %u byte relocation"),
3213 sign > 0 ? "signed" : "unsigned", size);
252b5132
RH
3214 }
3215
0cc9e1d3 3216 return NO_RELOC;
252b5132
RH
3217}
3218
47926f60
KH
3219/* Here we decide which fixups can be adjusted to make them relative to
3220 the beginning of the section instead of the symbol. Basically we need
3221 to make sure that the dynamic relocations are done correctly, so in
3222 some cases we force the original symbol to be used. */
3223
252b5132 3224int
e3bb37b5 3225tc_i386_fix_adjustable (fixS *fixP ATTRIBUTE_UNUSED)
252b5132 3226{
6d249963 3227#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 3228 if (!IS_ELF)
31312f95
AM
3229 return 1;
3230
a161fe53
AM
3231 /* Don't adjust pc-relative references to merge sections in 64-bit
3232 mode. */
3233 if (use_rela_relocations
3234 && (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0
3235 && fixP->fx_pcrel)
252b5132 3236 return 0;
31312f95 3237
8d01d9a9
AJ
3238 /* The x86_64 GOTPCREL are represented as 32bit PCrel relocations
3239 and changed later by validate_fix. */
3240 if (GOT_symbol && fixP->fx_subsy == GOT_symbol
3241 && fixP->fx_r_type == BFD_RELOC_32_PCREL)
3242 return 0;
3243
8fd4256d
L
3244 /* Adjust_reloc_syms doesn't know about the GOT. Need to keep symbol
3245 for size relocations. */
3246 if (fixP->fx_r_type == BFD_RELOC_SIZE32
3247 || fixP->fx_r_type == BFD_RELOC_SIZE64
3248 || fixP->fx_r_type == BFD_RELOC_386_GOTOFF
252b5132
RH
3249 || fixP->fx_r_type == BFD_RELOC_386_PLT32
3250 || fixP->fx_r_type == BFD_RELOC_386_GOT32
02a86693 3251 || fixP->fx_r_type == BFD_RELOC_386_GOT32X
13ae64f3
JJ
3252 || fixP->fx_r_type == BFD_RELOC_386_TLS_GD
3253 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDM
3254 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDO_32
3255 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE_32
37e55690
JJ
3256 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE
3257 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTIE
13ae64f3
JJ
3258 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE_32
3259 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE
67a4f2b7
AO
3260 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTDESC
3261 || fixP->fx_r_type == BFD_RELOC_386_TLS_DESC_CALL
3e73aa7c
JH
3262 || fixP->fx_r_type == BFD_RELOC_X86_64_PLT32
3263 || fixP->fx_r_type == BFD_RELOC_X86_64_GOT32
80b3ee89 3264 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCREL
56ceb5b5
L
3265 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCRELX
3266 || fixP->fx_r_type == BFD_RELOC_X86_64_REX_GOTPCRELX
bffbf940
JJ
3267 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSGD
3268 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSLD
3269 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF32
d6ab8113 3270 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF64
bffbf940
JJ
3271 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF
3272 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF32
d6ab8113
JB
3273 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF64
3274 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTOFF64
67a4f2b7
AO
3275 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
3276 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSDESC_CALL
252b5132
RH
3277 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3278 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3279 return 0;
31312f95 3280#endif
252b5132
RH
3281 return 1;
3282}
252b5132 3283
b4cac588 3284static int
e3bb37b5 3285intel_float_operand (const char *mnemonic)
252b5132 3286{
9306ca4a
JB
3287 /* Note that the value returned is meaningful only for opcodes with (memory)
3288 operands, hence the code here is free to improperly handle opcodes that
3289 have no operands (for better performance and smaller code). */
3290
3291 if (mnemonic[0] != 'f')
3292 return 0; /* non-math */
3293
3294 switch (mnemonic[1])
3295 {
3296 /* fclex, fdecstp, fdisi, femms, feni, fincstp, finit, fsetpm, and
3297 the fs segment override prefix not currently handled because no
3298 call path can make opcodes without operands get here */
3299 case 'i':
3300 return 2 /* integer op */;
3301 case 'l':
3302 if (mnemonic[2] == 'd' && (mnemonic[3] == 'c' || mnemonic[3] == 'e'))
3303 return 3; /* fldcw/fldenv */
3304 break;
3305 case 'n':
3306 if (mnemonic[2] != 'o' /* fnop */)
3307 return 3; /* non-waiting control op */
3308 break;
3309 case 'r':
3310 if (mnemonic[2] == 's')
3311 return 3; /* frstor/frstpm */
3312 break;
3313 case 's':
3314 if (mnemonic[2] == 'a')
3315 return 3; /* fsave */
3316 if (mnemonic[2] == 't')
3317 {
3318 switch (mnemonic[3])
3319 {
3320 case 'c': /* fstcw */
3321 case 'd': /* fstdw */
3322 case 'e': /* fstenv */
3323 case 's': /* fsts[gw] */
3324 return 3;
3325 }
3326 }
3327 break;
3328 case 'x':
3329 if (mnemonic[2] == 'r' || mnemonic[2] == 's')
3330 return 0; /* fxsave/fxrstor are not really math ops */
3331 break;
3332 }
252b5132 3333
9306ca4a 3334 return 1;
252b5132
RH
3335}
3336
c0f3af97
L
3337/* Build the VEX prefix. */
3338
3339static void
d3ce72d0 3340build_vex_prefix (const insn_template *t)
c0f3af97
L
3341{
3342 unsigned int register_specifier;
3343 unsigned int implied_prefix;
3344 unsigned int vector_length;
3345
3346 /* Check register specifier. */
3347 if (i.vex.register_specifier)
43234a1e
L
3348 {
3349 register_specifier =
3350 ~register_number (i.vex.register_specifier) & 0xf;
3351 gas_assert ((i.vex.register_specifier->reg_flags & RegVRex) == 0);
3352 }
c0f3af97
L
3353 else
3354 register_specifier = 0xf;
3355
33eaf5de 3356 /* Use 2-byte VEX prefix by swapping destination and source
fa99fab2 3357 operand. */
86fa6981
L
3358 if (i.vec_encoding != vex_encoding_vex3
3359 && i.dir_encoding == dir_encoding_default
fa99fab2 3360 && i.operands == i.reg_operands
7f399153 3361 && i.tm.opcode_modifier.vexopcode == VEX0F
86fa6981 3362 && i.tm.opcode_modifier.load
fa99fab2
L
3363 && i.rex == REX_B)
3364 {
3365 unsigned int xchg = i.operands - 1;
3366 union i386_op temp_op;
3367 i386_operand_type temp_type;
3368
3369 temp_type = i.types[xchg];
3370 i.types[xchg] = i.types[0];
3371 i.types[0] = temp_type;
3372 temp_op = i.op[xchg];
3373 i.op[xchg] = i.op[0];
3374 i.op[0] = temp_op;
3375
9c2799c2 3376 gas_assert (i.rm.mode == 3);
fa99fab2
L
3377
3378 i.rex = REX_R;
3379 xchg = i.rm.regmem;
3380 i.rm.regmem = i.rm.reg;
3381 i.rm.reg = xchg;
3382
3383 /* Use the next insn. */
3384 i.tm = t[1];
3385 }
3386
539f890d
L
3387 if (i.tm.opcode_modifier.vex == VEXScalar)
3388 vector_length = avxscalar;
10c17abd
JB
3389 else if (i.tm.opcode_modifier.vex == VEX256)
3390 vector_length = 1;
539f890d 3391 else
10c17abd 3392 {
56522fc5 3393 unsigned int op;
10c17abd 3394
c7213af9
L
3395 /* Determine vector length from the last multi-length vector
3396 operand. */
10c17abd 3397 vector_length = 0;
56522fc5 3398 for (op = t->operands; op--;)
10c17abd
JB
3399 if (t->operand_types[op].bitfield.xmmword
3400 && t->operand_types[op].bitfield.ymmword
3401 && i.types[op].bitfield.ymmword)
3402 {
3403 vector_length = 1;
3404 break;
3405 }
3406 }
c0f3af97
L
3407
3408 switch ((i.tm.base_opcode >> 8) & 0xff)
3409 {
3410 case 0:
3411 implied_prefix = 0;
3412 break;
3413 case DATA_PREFIX_OPCODE:
3414 implied_prefix = 1;
3415 break;
3416 case REPE_PREFIX_OPCODE:
3417 implied_prefix = 2;
3418 break;
3419 case REPNE_PREFIX_OPCODE:
3420 implied_prefix = 3;
3421 break;
3422 default:
3423 abort ();
3424 }
3425
3426 /* Use 2-byte VEX prefix if possible. */
86fa6981
L
3427 if (i.vec_encoding != vex_encoding_vex3
3428 && i.tm.opcode_modifier.vexopcode == VEX0F
04251de0 3429 && i.tm.opcode_modifier.vexw != VEXW1
c0f3af97
L
3430 && (i.rex & (REX_W | REX_X | REX_B)) == 0)
3431 {
3432 /* 2-byte VEX prefix. */
3433 unsigned int r;
3434
3435 i.vex.length = 2;
3436 i.vex.bytes[0] = 0xc5;
3437
3438 /* Check the REX.R bit. */
3439 r = (i.rex & REX_R) ? 0 : 1;
3440 i.vex.bytes[1] = (r << 7
3441 | register_specifier << 3
3442 | vector_length << 2
3443 | implied_prefix);
3444 }
3445 else
3446 {
3447 /* 3-byte VEX prefix. */
3448 unsigned int m, w;
3449
f88c9eb0 3450 i.vex.length = 3;
f88c9eb0 3451
7f399153 3452 switch (i.tm.opcode_modifier.vexopcode)
5dd85c99 3453 {
7f399153
L
3454 case VEX0F:
3455 m = 0x1;
80de6e00 3456 i.vex.bytes[0] = 0xc4;
7f399153
L
3457 break;
3458 case VEX0F38:
3459 m = 0x2;
80de6e00 3460 i.vex.bytes[0] = 0xc4;
7f399153
L
3461 break;
3462 case VEX0F3A:
3463 m = 0x3;
80de6e00 3464 i.vex.bytes[0] = 0xc4;
7f399153
L
3465 break;
3466 case XOP08:
5dd85c99
SP
3467 m = 0x8;
3468 i.vex.bytes[0] = 0x8f;
7f399153
L
3469 break;
3470 case XOP09:
f88c9eb0
SP
3471 m = 0x9;
3472 i.vex.bytes[0] = 0x8f;
7f399153
L
3473 break;
3474 case XOP0A:
f88c9eb0
SP
3475 m = 0xa;
3476 i.vex.bytes[0] = 0x8f;
7f399153
L
3477 break;
3478 default:
3479 abort ();
f88c9eb0 3480 }
c0f3af97 3481
c0f3af97
L
3482 /* The high 3 bits of the second VEX byte are 1's compliment
3483 of RXB bits from REX. */
3484 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | m;
3485
3486 /* Check the REX.W bit. */
3487 w = (i.rex & REX_W) ? 1 : 0;
b28d1bda
IT
3488 if (i.tm.opcode_modifier.vexw == VEXW1)
3489 w = 1;
c0f3af97
L
3490
3491 i.vex.bytes[2] = (w << 7
3492 | register_specifier << 3
3493 | vector_length << 2
3494 | implied_prefix);
3495 }
3496}
3497
e771e7c9
JB
3498static INLINE bfd_boolean
3499is_evex_encoding (const insn_template *t)
3500{
7091c612 3501 return t->opcode_modifier.evex || t->opcode_modifier.disp8memshift
e771e7c9
JB
3502 || t->opcode_modifier.broadcast || t->opcode_modifier.masking
3503 || t->opcode_modifier.staticrounding || t->opcode_modifier.sae;
3504}
3505
7a8655d2
JB
3506static INLINE bfd_boolean
3507is_any_vex_encoding (const insn_template *t)
3508{
3509 return t->opcode_modifier.vex || t->opcode_modifier.vexopcode
3510 || is_evex_encoding (t);
3511}
3512
43234a1e
L
3513/* Build the EVEX prefix. */
3514
3515static void
3516build_evex_prefix (void)
3517{
3518 unsigned int register_specifier;
3519 unsigned int implied_prefix;
3520 unsigned int m, w;
3521 rex_byte vrex_used = 0;
3522
3523 /* Check register specifier. */
3524 if (i.vex.register_specifier)
3525 {
3526 gas_assert ((i.vrex & REX_X) == 0);
3527
3528 register_specifier = i.vex.register_specifier->reg_num;
3529 if ((i.vex.register_specifier->reg_flags & RegRex))
3530 register_specifier += 8;
3531 /* The upper 16 registers are encoded in the fourth byte of the
3532 EVEX prefix. */
3533 if (!(i.vex.register_specifier->reg_flags & RegVRex))
3534 i.vex.bytes[3] = 0x8;
3535 register_specifier = ~register_specifier & 0xf;
3536 }
3537 else
3538 {
3539 register_specifier = 0xf;
3540
3541 /* Encode upper 16 vector index register in the fourth byte of
3542 the EVEX prefix. */
3543 if (!(i.vrex & REX_X))
3544 i.vex.bytes[3] = 0x8;
3545 else
3546 vrex_used |= REX_X;
3547 }
3548
3549 switch ((i.tm.base_opcode >> 8) & 0xff)
3550 {
3551 case 0:
3552 implied_prefix = 0;
3553 break;
3554 case DATA_PREFIX_OPCODE:
3555 implied_prefix = 1;
3556 break;
3557 case REPE_PREFIX_OPCODE:
3558 implied_prefix = 2;
3559 break;
3560 case REPNE_PREFIX_OPCODE:
3561 implied_prefix = 3;
3562 break;
3563 default:
3564 abort ();
3565 }
3566
3567 /* 4 byte EVEX prefix. */
3568 i.vex.length = 4;
3569 i.vex.bytes[0] = 0x62;
3570
3571 /* mmmm bits. */
3572 switch (i.tm.opcode_modifier.vexopcode)
3573 {
3574 case VEX0F:
3575 m = 1;
3576 break;
3577 case VEX0F38:
3578 m = 2;
3579 break;
3580 case VEX0F3A:
3581 m = 3;
3582 break;
3583 default:
3584 abort ();
3585 break;
3586 }
3587
3588 /* The high 3 bits of the second EVEX byte are 1's compliment of RXB
3589 bits from REX. */
3590 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | m;
3591
3592 /* The fifth bit of the second EVEX byte is 1's compliment of the
3593 REX_R bit in VREX. */
3594 if (!(i.vrex & REX_R))
3595 i.vex.bytes[1] |= 0x10;
3596 else
3597 vrex_used |= REX_R;
3598
3599 if ((i.reg_operands + i.imm_operands) == i.operands)
3600 {
3601 /* When all operands are registers, the REX_X bit in REX is not
3602 used. We reuse it to encode the upper 16 registers, which is
3603 indicated by the REX_B bit in VREX. The REX_X bit is encoded
3604 as 1's compliment. */
3605 if ((i.vrex & REX_B))
3606 {
3607 vrex_used |= REX_B;
3608 i.vex.bytes[1] &= ~0x40;
3609 }
3610 }
3611
3612 /* EVEX instructions shouldn't need the REX prefix. */
3613 i.vrex &= ~vrex_used;
3614 gas_assert (i.vrex == 0);
3615
3616 /* Check the REX.W bit. */
3617 w = (i.rex & REX_W) ? 1 : 0;
3618 if (i.tm.opcode_modifier.vexw)
3619 {
3620 if (i.tm.opcode_modifier.vexw == VEXW1)
3621 w = 1;
3622 }
3623 /* If w is not set it means we are dealing with WIG instruction. */
3624 else if (!w)
3625 {
3626 if (evexwig == evexw1)
3627 w = 1;
3628 }
3629
3630 /* Encode the U bit. */
3631 implied_prefix |= 0x4;
3632
3633 /* The third byte of the EVEX prefix. */
3634 i.vex.bytes[2] = (w << 7 | register_specifier << 3 | implied_prefix);
3635
3636 /* The fourth byte of the EVEX prefix. */
3637 /* The zeroing-masking bit. */
3638 if (i.mask && i.mask->zeroing)
3639 i.vex.bytes[3] |= 0x80;
3640
3641 /* Don't always set the broadcast bit if there is no RC. */
3642 if (!i.rounding)
3643 {
3644 /* Encode the vector length. */
3645 unsigned int vec_length;
3646
e771e7c9
JB
3647 if (!i.tm.opcode_modifier.evex
3648 || i.tm.opcode_modifier.evex == EVEXDYN)
3649 {
56522fc5 3650 unsigned int op;
e771e7c9 3651
c7213af9
L
3652 /* Determine vector length from the last multi-length vector
3653 operand. */
e771e7c9 3654 vec_length = 0;
56522fc5 3655 for (op = i.operands; op--;)
e771e7c9
JB
3656 if (i.tm.operand_types[op].bitfield.xmmword
3657 + i.tm.operand_types[op].bitfield.ymmword
3658 + i.tm.operand_types[op].bitfield.zmmword > 1)
3659 {
3660 if (i.types[op].bitfield.zmmword)
c7213af9
L
3661 {
3662 i.tm.opcode_modifier.evex = EVEX512;
3663 break;
3664 }
e771e7c9 3665 else if (i.types[op].bitfield.ymmword)
c7213af9
L
3666 {
3667 i.tm.opcode_modifier.evex = EVEX256;
3668 break;
3669 }
e771e7c9 3670 else if (i.types[op].bitfield.xmmword)
c7213af9
L
3671 {
3672 i.tm.opcode_modifier.evex = EVEX128;
3673 break;
3674 }
625cbd7a
JB
3675 else if (i.broadcast && (int) op == i.broadcast->operand)
3676 {
4a1b91ea 3677 switch (i.broadcast->bytes)
625cbd7a
JB
3678 {
3679 case 64:
3680 i.tm.opcode_modifier.evex = EVEX512;
3681 break;
3682 case 32:
3683 i.tm.opcode_modifier.evex = EVEX256;
3684 break;
3685 case 16:
3686 i.tm.opcode_modifier.evex = EVEX128;
3687 break;
3688 default:
c7213af9 3689 abort ();
625cbd7a 3690 }
c7213af9 3691 break;
625cbd7a 3692 }
e771e7c9 3693 }
c7213af9 3694
56522fc5 3695 if (op >= MAX_OPERANDS)
c7213af9 3696 abort ();
e771e7c9
JB
3697 }
3698
43234a1e
L
3699 switch (i.tm.opcode_modifier.evex)
3700 {
3701 case EVEXLIG: /* LL' is ignored */
3702 vec_length = evexlig << 5;
3703 break;
3704 case EVEX128:
3705 vec_length = 0 << 5;
3706 break;
3707 case EVEX256:
3708 vec_length = 1 << 5;
3709 break;
3710 case EVEX512:
3711 vec_length = 2 << 5;
3712 break;
3713 default:
3714 abort ();
3715 break;
3716 }
3717 i.vex.bytes[3] |= vec_length;
3718 /* Encode the broadcast bit. */
3719 if (i.broadcast)
3720 i.vex.bytes[3] |= 0x10;
3721 }
3722 else
3723 {
3724 if (i.rounding->type != saeonly)
3725 i.vex.bytes[3] |= 0x10 | (i.rounding->type << 5);
3726 else
d3d3c6db 3727 i.vex.bytes[3] |= 0x10 | (evexrcig << 5);
43234a1e
L
3728 }
3729
3730 if (i.mask && i.mask->mask)
3731 i.vex.bytes[3] |= i.mask->mask->reg_num;
3732}
3733
65da13b5
L
3734static void
3735process_immext (void)
3736{
3737 expressionS *exp;
3738
4c692bc7
JB
3739 if ((i.tm.cpu_flags.bitfield.cpusse3 || i.tm.cpu_flags.bitfield.cpusvme)
3740 && i.operands > 0)
65da13b5 3741 {
4c692bc7
JB
3742 /* MONITOR/MWAIT as well as SVME instructions have fixed operands
3743 with an opcode suffix which is coded in the same place as an
3744 8-bit immediate field would be.
3745 Here we check those operands and remove them afterwards. */
65da13b5
L
3746 unsigned int x;
3747
3748 for (x = 0; x < i.operands; x++)
4c692bc7 3749 if (register_number (i.op[x].regs) != x)
65da13b5 3750 as_bad (_("can't use register '%s%s' as operand %d in '%s'."),
1fed0ba1
L
3751 register_prefix, i.op[x].regs->reg_name, x + 1,
3752 i.tm.name);
3753
3754 i.operands = 0;
65da13b5
L
3755 }
3756
9916071f
AP
3757 if (i.tm.cpu_flags.bitfield.cpumwaitx && i.operands > 0)
3758 {
3759 /* MONITORX/MWAITX instructions have fixed operands with an opcode
3760 suffix which is coded in the same place as an 8-bit immediate
3761 field would be.
3762 Here we check those operands and remove them afterwards. */
3763 unsigned int x;
3764
3765 if (i.operands != 3)
3766 abort();
3767
3768 for (x = 0; x < 2; x++)
3769 if (register_number (i.op[x].regs) != x)
3770 goto bad_register_operand;
3771
3772 /* Check for third operand for mwaitx/monitorx insn. */
3773 if (register_number (i.op[x].regs)
3774 != (x + (i.tm.extension_opcode == 0xfb)))
3775 {
3776bad_register_operand:
3777 as_bad (_("can't use register '%s%s' as operand %d in '%s'."),
3778 register_prefix, i.op[x].regs->reg_name, x+1,
3779 i.tm.name);
3780 }
3781
3782 i.operands = 0;
3783 }
3784
c0f3af97 3785 /* These AMD 3DNow! and SSE2 instructions have an opcode suffix
65da13b5
L
3786 which is coded in the same place as an 8-bit immediate field
3787 would be. Here we fake an 8-bit immediate operand from the
3788 opcode suffix stored in tm.extension_opcode.
3789
c1e679ec 3790 AVX instructions also use this encoding, for some of
c0f3af97 3791 3 argument instructions. */
65da13b5 3792
43234a1e 3793 gas_assert (i.imm_operands <= 1
7ab9ffdd 3794 && (i.operands <= 2
7a8655d2 3795 || (is_any_vex_encoding (&i.tm)
7ab9ffdd 3796 && i.operands <= 4)));
65da13b5
L
3797
3798 exp = &im_expressions[i.imm_operands++];
3799 i.op[i.operands].imms = exp;
3800 i.types[i.operands] = imm8;
3801 i.operands++;
3802 exp->X_op = O_constant;
3803 exp->X_add_number = i.tm.extension_opcode;
3804 i.tm.extension_opcode = None;
3805}
3806
42164a71
L
3807
3808static int
3809check_hle (void)
3810{
3811 switch (i.tm.opcode_modifier.hleprefixok)
3812 {
3813 default:
3814 abort ();
82c2def5 3815 case HLEPrefixNone:
165de32a
L
3816 as_bad (_("invalid instruction `%s' after `%s'"),
3817 i.tm.name, i.hle_prefix);
42164a71 3818 return 0;
82c2def5 3819 case HLEPrefixLock:
42164a71
L
3820 if (i.prefix[LOCK_PREFIX])
3821 return 1;
165de32a 3822 as_bad (_("missing `lock' with `%s'"), i.hle_prefix);
42164a71 3823 return 0;
82c2def5 3824 case HLEPrefixAny:
42164a71 3825 return 1;
82c2def5 3826 case HLEPrefixRelease:
42164a71
L
3827 if (i.prefix[HLE_PREFIX] != XRELEASE_PREFIX_OPCODE)
3828 {
3829 as_bad (_("instruction `%s' after `xacquire' not allowed"),
3830 i.tm.name);
3831 return 0;
3832 }
3833 if (i.mem_operands == 0
3834 || !operand_type_check (i.types[i.operands - 1], anymem))
3835 {
3836 as_bad (_("memory destination needed for instruction `%s'"
3837 " after `xrelease'"), i.tm.name);
3838 return 0;
3839 }
3840 return 1;
3841 }
3842}
3843
b6f8c7c4
L
3844/* Try the shortest encoding by shortening operand size. */
3845
3846static void
3847optimize_encoding (void)
3848{
3849 int j;
3850
3851 if (optimize_for_space
3852 && i.reg_operands == 1
3853 && i.imm_operands == 1
3854 && !i.types[1].bitfield.byte
3855 && i.op[0].imms->X_op == O_constant
3856 && fits_in_imm7 (i.op[0].imms->X_add_number)
3857 && ((i.tm.base_opcode == 0xa8
3858 && i.tm.extension_opcode == None)
3859 || (i.tm.base_opcode == 0xf6
3860 && i.tm.extension_opcode == 0x0)))
3861 {
3862 /* Optimize: -Os:
3863 test $imm7, %r64/%r32/%r16 -> test $imm7, %r8
3864 */
3865 unsigned int base_regnum = i.op[1].regs->reg_num;
3866 if (flag_code == CODE_64BIT || base_regnum < 4)
3867 {
3868 i.types[1].bitfield.byte = 1;
3869 /* Ignore the suffix. */
3870 i.suffix = 0;
3871 if (base_regnum >= 4
3872 && !(i.op[1].regs->reg_flags & RegRex))
3873 {
3874 /* Handle SP, BP, SI and DI registers. */
3875 if (i.types[1].bitfield.word)
3876 j = 16;
3877 else if (i.types[1].bitfield.dword)
3878 j = 32;
3879 else
3880 j = 48;
3881 i.op[1].regs -= j;
3882 }
3883 }
3884 }
3885 else if (flag_code == CODE_64BIT
d3d50934
L
3886 && ((i.types[1].bitfield.qword
3887 && i.reg_operands == 1
b6f8c7c4
L
3888 && i.imm_operands == 1
3889 && i.op[0].imms->X_op == O_constant
3890 && ((i.tm.base_opcode == 0xb0
3891 && i.tm.extension_opcode == None
3892 && fits_in_unsigned_long (i.op[0].imms->X_add_number))
3893 || (fits_in_imm31 (i.op[0].imms->X_add_number)
3894 && (((i.tm.base_opcode == 0x24
3895 || i.tm.base_opcode == 0xa8)
3896 && i.tm.extension_opcode == None)
3897 || (i.tm.base_opcode == 0x80
3898 && i.tm.extension_opcode == 0x4)
3899 || ((i.tm.base_opcode == 0xf6
3900 || i.tm.base_opcode == 0xc6)
3901 && i.tm.extension_opcode == 0x0)))))
d3d50934
L
3902 || (i.types[0].bitfield.qword
3903 && ((i.reg_operands == 2
3904 && i.op[0].regs == i.op[1].regs
3905 && ((i.tm.base_opcode == 0x30
3906 || i.tm.base_opcode == 0x28)
3907 && i.tm.extension_opcode == None))
3908 || (i.reg_operands == 1
3909 && i.operands == 1
3910 && i.tm.base_opcode == 0x30
3911 && i.tm.extension_opcode == None)))))
b6f8c7c4
L
3912 {
3913 /* Optimize: -O:
3914 andq $imm31, %r64 -> andl $imm31, %r32
3915 testq $imm31, %r64 -> testl $imm31, %r32
3916 xorq %r64, %r64 -> xorl %r32, %r32
3917 subq %r64, %r64 -> subl %r32, %r32
3918 movq $imm31, %r64 -> movl $imm31, %r32
3919 movq $imm32, %r64 -> movl $imm32, %r32
3920 */
3921 i.tm.opcode_modifier.norex64 = 1;
3922 if (i.tm.base_opcode == 0xb0 || i.tm.base_opcode == 0xc6)
3923 {
3924 /* Handle
3925 movq $imm31, %r64 -> movl $imm31, %r32
3926 movq $imm32, %r64 -> movl $imm32, %r32
3927 */
3928 i.tm.operand_types[0].bitfield.imm32 = 1;
3929 i.tm.operand_types[0].bitfield.imm32s = 0;
3930 i.tm.operand_types[0].bitfield.imm64 = 0;
3931 i.types[0].bitfield.imm32 = 1;
3932 i.types[0].bitfield.imm32s = 0;
3933 i.types[0].bitfield.imm64 = 0;
3934 i.types[1].bitfield.dword = 1;
3935 i.types[1].bitfield.qword = 0;
3936 if (i.tm.base_opcode == 0xc6)
3937 {
3938 /* Handle
3939 movq $imm31, %r64 -> movl $imm31, %r32
3940 */
3941 i.tm.base_opcode = 0xb0;
3942 i.tm.extension_opcode = None;
3943 i.tm.opcode_modifier.shortform = 1;
3944 i.tm.opcode_modifier.modrm = 0;
3945 }
3946 }
3947 }
3948 else if (optimize > 1
3949 && i.reg_operands == 3
3950 && i.op[0].regs == i.op[1].regs
3951 && !i.types[2].bitfield.xmmword
3952 && (i.tm.opcode_modifier.vex
7a69eac3 3953 || ((!i.mask || i.mask->zeroing)
b6f8c7c4 3954 && !i.rounding
e771e7c9 3955 && is_evex_encoding (&i.tm)
80c34c38
L
3956 && (i.vec_encoding != vex_encoding_evex
3957 || i.tm.cpu_flags.bitfield.cpuavx512vl
7091c612
JB
3958 || (i.tm.operand_types[2].bitfield.zmmword
3959 && i.types[2].bitfield.ymmword)
0089dace 3960 || cpu_arch_isa_flags.bitfield.cpuavx512vl)))
b6f8c7c4
L
3961 && ((i.tm.base_opcode == 0x55
3962 || i.tm.base_opcode == 0x6655
3963 || i.tm.base_opcode == 0x66df
3964 || i.tm.base_opcode == 0x57
3965 || i.tm.base_opcode == 0x6657
8305403a
L
3966 || i.tm.base_opcode == 0x66ef
3967 || i.tm.base_opcode == 0x66f8
3968 || i.tm.base_opcode == 0x66f9
3969 || i.tm.base_opcode == 0x66fa
1424ad86
JB
3970 || i.tm.base_opcode == 0x66fb
3971 || i.tm.base_opcode == 0x42
3972 || i.tm.base_opcode == 0x6642
3973 || i.tm.base_opcode == 0x47
3974 || i.tm.base_opcode == 0x6647)
b6f8c7c4
L
3975 && i.tm.extension_opcode == None))
3976 {
3977 /* Optimize: -O2:
8305403a
L
3978 VOP, one of vandnps, vandnpd, vxorps, vxorpd, vpsubb, vpsubd,
3979 vpsubq and vpsubw:
b6f8c7c4
L
3980 EVEX VOP %zmmM, %zmmM, %zmmN
3981 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
3982 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16)
3983 EVEX VOP %ymmM, %ymmM, %ymmN
3984 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
3985 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16)
3986 VEX VOP %ymmM, %ymmM, %ymmN
3987 -> VEX VOP %xmmM, %xmmM, %xmmN
3988 VOP, one of vpandn and vpxor:
3989 VEX VOP %ymmM, %ymmM, %ymmN
3990 -> VEX VOP %xmmM, %xmmM, %xmmN
3991 VOP, one of vpandnd and vpandnq:
3992 EVEX VOP %zmmM, %zmmM, %zmmN
3993 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
3994 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16)
3995 EVEX VOP %ymmM, %ymmM, %ymmN
3996 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
3997 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16)
3998 VOP, one of vpxord and vpxorq:
3999 EVEX VOP %zmmM, %zmmM, %zmmN
4000 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
4001 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16)
4002 EVEX VOP %ymmM, %ymmM, %ymmN
4003 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
4004 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16)
1424ad86
JB
4005 VOP, one of kxord and kxorq:
4006 VEX VOP %kM, %kM, %kN
4007 -> VEX kxorw %kM, %kM, %kN
4008 VOP, one of kandnd and kandnq:
4009 VEX VOP %kM, %kM, %kN
4010 -> VEX kandnw %kM, %kM, %kN
b6f8c7c4 4011 */
e771e7c9 4012 if (is_evex_encoding (&i.tm))
b6f8c7c4 4013 {
0089dace 4014 if (i.vec_encoding == vex_encoding_evex)
b6f8c7c4
L
4015 i.tm.opcode_modifier.evex = EVEX128;
4016 else
4017 {
4018 i.tm.opcode_modifier.vex = VEX128;
4019 i.tm.opcode_modifier.vexw = VEXW0;
4020 i.tm.opcode_modifier.evex = 0;
4021 }
4022 }
1424ad86
JB
4023 else if (i.tm.operand_types[0].bitfield.regmask)
4024 {
4025 i.tm.base_opcode &= 0xff;
4026 i.tm.opcode_modifier.vexw = VEXW0;
4027 }
b6f8c7c4
L
4028 else
4029 i.tm.opcode_modifier.vex = VEX128;
4030
4031 if (i.tm.opcode_modifier.vex)
4032 for (j = 0; j < 3; j++)
4033 {
4034 i.types[j].bitfield.xmmword = 1;
4035 i.types[j].bitfield.ymmword = 0;
4036 }
4037 }
4038}
4039
252b5132
RH
4040/* This is the guts of the machine-dependent assembler. LINE points to a
4041 machine dependent instruction. This function is supposed to emit
4042 the frags/bytes it assembles to. */
4043
4044void
65da13b5 4045md_assemble (char *line)
252b5132 4046{
40fb9820 4047 unsigned int j;
83b16ac6 4048 char mnemonic[MAX_MNEM_SIZE], mnem_suffix;
d3ce72d0 4049 const insn_template *t;
252b5132 4050
47926f60 4051 /* Initialize globals. */
252b5132
RH
4052 memset (&i, '\0', sizeof (i));
4053 for (j = 0; j < MAX_OPERANDS; j++)
1ae12ab7 4054 i.reloc[j] = NO_RELOC;
252b5132
RH
4055 memset (disp_expressions, '\0', sizeof (disp_expressions));
4056 memset (im_expressions, '\0', sizeof (im_expressions));
ce8a8b2f 4057 save_stack_p = save_stack;
252b5132
RH
4058
4059 /* First parse an instruction mnemonic & call i386_operand for the operands.
4060 We assume that the scrubber has arranged it so that line[0] is the valid
47926f60 4061 start of a (possibly prefixed) mnemonic. */
252b5132 4062
29b0f896
AM
4063 line = parse_insn (line, mnemonic);
4064 if (line == NULL)
4065 return;
83b16ac6 4066 mnem_suffix = i.suffix;
252b5132 4067
29b0f896 4068 line = parse_operands (line, mnemonic);
ee86248c 4069 this_operand = -1;
8325cc63
JB
4070 xfree (i.memop1_string);
4071 i.memop1_string = NULL;
29b0f896
AM
4072 if (line == NULL)
4073 return;
252b5132 4074
29b0f896
AM
4075 /* Now we've parsed the mnemonic into a set of templates, and have the
4076 operands at hand. */
4077
4078 /* All intel opcodes have reversed operands except for "bound" and
4079 "enter". We also don't reverse intersegment "jmp" and "call"
4080 instructions with 2 immediate operands so that the immediate segment
050dfa73 4081 precedes the offset, as it does when in AT&T mode. */
4d456e3d
L
4082 if (intel_syntax
4083 && i.operands > 1
29b0f896 4084 && (strcmp (mnemonic, "bound") != 0)
30123838 4085 && (strcmp (mnemonic, "invlpga") != 0)
40fb9820
L
4086 && !(operand_type_check (i.types[0], imm)
4087 && operand_type_check (i.types[1], imm)))
29b0f896
AM
4088 swap_operands ();
4089
ec56d5c0
JB
4090 /* The order of the immediates should be reversed
4091 for 2 immediates extrq and insertq instructions */
4092 if (i.imm_operands == 2
4093 && (strcmp (mnemonic, "extrq") == 0
4094 || strcmp (mnemonic, "insertq") == 0))
4095 swap_2_operands (0, 1);
4096
29b0f896
AM
4097 if (i.imm_operands)
4098 optimize_imm ();
4099
b300c311
L
4100 /* Don't optimize displacement for movabs since it only takes 64bit
4101 displacement. */
4102 if (i.disp_operands
a501d77e 4103 && i.disp_encoding != disp_encoding_32bit
862be3fb
L
4104 && (flag_code != CODE_64BIT
4105 || strcmp (mnemonic, "movabs") != 0))
4106 optimize_disp ();
29b0f896
AM
4107
4108 /* Next, we find a template that matches the given insn,
4109 making sure the overlap of the given operands types is consistent
4110 with the template operand types. */
252b5132 4111
83b16ac6 4112 if (!(t = match_template (mnem_suffix)))
29b0f896 4113 return;
252b5132 4114
7bab8ab5 4115 if (sse_check != check_none
81f8a913 4116 && !i.tm.opcode_modifier.noavx
6e3e5c9e 4117 && !i.tm.cpu_flags.bitfield.cpuavx
daf50ae7
L
4118 && (i.tm.cpu_flags.bitfield.cpusse
4119 || i.tm.cpu_flags.bitfield.cpusse2
4120 || i.tm.cpu_flags.bitfield.cpusse3
4121 || i.tm.cpu_flags.bitfield.cpussse3
4122 || i.tm.cpu_flags.bitfield.cpusse4_1
6e3e5c9e
JB
4123 || i.tm.cpu_flags.bitfield.cpusse4_2
4124 || i.tm.cpu_flags.bitfield.cpupclmul
4125 || i.tm.cpu_flags.bitfield.cpuaes
4126 || i.tm.cpu_flags.bitfield.cpugfni))
daf50ae7 4127 {
7bab8ab5 4128 (sse_check == check_warning
daf50ae7
L
4129 ? as_warn
4130 : as_bad) (_("SSE instruction `%s' is used"), i.tm.name);
4131 }
4132
321fd21e
L
4133 /* Zap movzx and movsx suffix. The suffix has been set from
4134 "word ptr" or "byte ptr" on the source operand in Intel syntax
4135 or extracted from mnemonic in AT&T syntax. But we'll use
4136 the destination register to choose the suffix for encoding. */
4137 if ((i.tm.base_opcode & ~9) == 0x0fb6)
cd61ebfe 4138 {
321fd21e
L
4139 /* In Intel syntax, there must be a suffix. In AT&T syntax, if
4140 there is no suffix, the default will be byte extension. */
4141 if (i.reg_operands != 2
4142 && !i.suffix
7ab9ffdd 4143 && intel_syntax)
321fd21e
L
4144 as_bad (_("ambiguous operand size for `%s'"), i.tm.name);
4145
4146 i.suffix = 0;
cd61ebfe 4147 }
24eab124 4148
40fb9820 4149 if (i.tm.opcode_modifier.fwait)
29b0f896
AM
4150 if (!add_prefix (FWAIT_OPCODE))
4151 return;
252b5132 4152
d5de92cf
L
4153 /* Check if REP prefix is OK. */
4154 if (i.rep_prefix && !i.tm.opcode_modifier.repprefixok)
4155 {
4156 as_bad (_("invalid instruction `%s' after `%s'"),
4157 i.tm.name, i.rep_prefix);
4158 return;
4159 }
4160
c1ba0266
L
4161 /* Check for lock without a lockable instruction. Destination operand
4162 must be memory unless it is xchg (0x86). */
c32fa91d
L
4163 if (i.prefix[LOCK_PREFIX]
4164 && (!i.tm.opcode_modifier.islockable
c1ba0266
L
4165 || i.mem_operands == 0
4166 || (i.tm.base_opcode != 0x86
4167 && !operand_type_check (i.types[i.operands - 1], anymem))))
c32fa91d
L
4168 {
4169 as_bad (_("expecting lockable instruction after `lock'"));
4170 return;
4171 }
4172
7a8655d2
JB
4173 /* Check for data size prefix on VEX/XOP/EVEX encoded insns. */
4174 if (i.prefix[DATA_PREFIX] && is_any_vex_encoding (&i.tm))
4175 {
4176 as_bad (_("data size prefix invalid with `%s'"), i.tm.name);
4177 return;
4178 }
4179
42164a71 4180 /* Check if HLE prefix is OK. */
165de32a 4181 if (i.hle_prefix && !check_hle ())
42164a71
L
4182 return;
4183
7e8b059b
L
4184 /* Check BND prefix. */
4185 if (i.bnd_prefix && !i.tm.opcode_modifier.bndprefixok)
4186 as_bad (_("expecting valid branch instruction after `bnd'"));
4187
04ef582a 4188 /* Check NOTRACK prefix. */
9fef80d6
L
4189 if (i.notrack_prefix && !i.tm.opcode_modifier.notrackprefixok)
4190 as_bad (_("expecting indirect branch instruction after `notrack'"));
04ef582a 4191
327e8c42
JB
4192 if (i.tm.cpu_flags.bitfield.cpumpx)
4193 {
4194 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
4195 as_bad (_("32-bit address isn't allowed in 64-bit MPX instructions."));
4196 else if (flag_code != CODE_16BIT
4197 ? i.prefix[ADDR_PREFIX]
4198 : i.mem_operands && !i.prefix[ADDR_PREFIX])
4199 as_bad (_("16-bit address isn't allowed in MPX instructions"));
4200 }
7e8b059b
L
4201
4202 /* Insert BND prefix. */
76d3a78a
JB
4203 if (add_bnd_prefix && i.tm.opcode_modifier.bndprefixok)
4204 {
4205 if (!i.prefix[BND_PREFIX])
4206 add_prefix (BND_PREFIX_OPCODE);
4207 else if (i.prefix[BND_PREFIX] != BND_PREFIX_OPCODE)
4208 {
4209 as_warn (_("replacing `rep'/`repe' prefix by `bnd'"));
4210 i.prefix[BND_PREFIX] = BND_PREFIX_OPCODE;
4211 }
4212 }
7e8b059b 4213
29b0f896 4214 /* Check string instruction segment overrides. */
40fb9820 4215 if (i.tm.opcode_modifier.isstring && i.mem_operands != 0)
29b0f896
AM
4216 {
4217 if (!check_string ())
5dd0794d 4218 return;
fc0763e6 4219 i.disp_operands = 0;
29b0f896 4220 }
5dd0794d 4221
b6f8c7c4
L
4222 if (optimize && !i.no_optimize && i.tm.opcode_modifier.optimize)
4223 optimize_encoding ();
4224
29b0f896
AM
4225 if (!process_suffix ())
4226 return;
e413e4e9 4227
bc0844ae
L
4228 /* Update operand types. */
4229 for (j = 0; j < i.operands; j++)
4230 i.types[j] = operand_type_and (i.types[j], i.tm.operand_types[j]);
4231
29b0f896
AM
4232 /* Make still unresolved immediate matches conform to size of immediate
4233 given in i.suffix. */
4234 if (!finalize_imm ())
4235 return;
252b5132 4236
40fb9820 4237 if (i.types[0].bitfield.imm1)
29b0f896 4238 i.imm_operands = 0; /* kludge for shift insns. */
252b5132 4239
9afe6eb8
L
4240 /* We only need to check those implicit registers for instructions
4241 with 3 operands or less. */
4242 if (i.operands <= 3)
4243 for (j = 0; j < i.operands; j++)
4244 if (i.types[j].bitfield.inoutportreg
4245 || i.types[j].bitfield.shiftcount
1b54b8d7 4246 || (i.types[j].bitfield.acc && !i.types[j].bitfield.xmmword))
9afe6eb8 4247 i.reg_operands--;
40fb9820 4248
c0f3af97
L
4249 /* ImmExt should be processed after SSE2AVX. */
4250 if (!i.tm.opcode_modifier.sse2avx
4251 && i.tm.opcode_modifier.immext)
65da13b5 4252 process_immext ();
252b5132 4253
29b0f896
AM
4254 /* For insns with operands there are more diddles to do to the opcode. */
4255 if (i.operands)
4256 {
4257 if (!process_operands ())
4258 return;
4259 }
40fb9820 4260 else if (!quiet_warnings && i.tm.opcode_modifier.ugh)
29b0f896
AM
4261 {
4262 /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc. */
4263 as_warn (_("translating to `%sp'"), i.tm.name);
4264 }
252b5132 4265
7a8655d2 4266 if (is_any_vex_encoding (&i.tm))
9e5e5283
L
4267 {
4268 if (flag_code == CODE_16BIT)
4269 {
4270 as_bad (_("instruction `%s' isn't supported in 16-bit mode."),
4271 i.tm.name);
4272 return;
4273 }
c0f3af97 4274
9e5e5283
L
4275 if (i.tm.opcode_modifier.vex)
4276 build_vex_prefix (t);
4277 else
4278 build_evex_prefix ();
4279 }
43234a1e 4280
5dd85c99
SP
4281 /* Handle conversion of 'int $3' --> special int3 insn. XOP or FMA4
4282 instructions may define INT_OPCODE as well, so avoid this corner
4283 case for those instructions that use MODRM. */
4284 if (i.tm.base_opcode == INT_OPCODE
a6461c02
SP
4285 && !i.tm.opcode_modifier.modrm
4286 && i.op[0].imms->X_add_number == 3)
29b0f896
AM
4287 {
4288 i.tm.base_opcode = INT3_OPCODE;
4289 i.imm_operands = 0;
4290 }
252b5132 4291
40fb9820
L
4292 if ((i.tm.opcode_modifier.jump
4293 || i.tm.opcode_modifier.jumpbyte
4294 || i.tm.opcode_modifier.jumpdword)
29b0f896
AM
4295 && i.op[0].disps->X_op == O_constant)
4296 {
4297 /* Convert "jmp constant" (and "call constant") to a jump (call) to
4298 the absolute address given by the constant. Since ix86 jumps and
4299 calls are pc relative, we need to generate a reloc. */
4300 i.op[0].disps->X_add_symbol = &abs_symbol;
4301 i.op[0].disps->X_op = O_symbol;
4302 }
252b5132 4303
40fb9820 4304 if (i.tm.opcode_modifier.rex64)
161a04f6 4305 i.rex |= REX_W;
252b5132 4306
29b0f896
AM
4307 /* For 8 bit registers we need an empty rex prefix. Also if the
4308 instruction already has a prefix, we need to convert old
4309 registers to new ones. */
773f551c 4310
dc821c5f 4311 if ((i.types[0].bitfield.reg && i.types[0].bitfield.byte
29b0f896 4312 && (i.op[0].regs->reg_flags & RegRex64) != 0)
dc821c5f 4313 || (i.types[1].bitfield.reg && i.types[1].bitfield.byte
29b0f896 4314 && (i.op[1].regs->reg_flags & RegRex64) != 0)
dc821c5f
JB
4315 || (((i.types[0].bitfield.reg && i.types[0].bitfield.byte)
4316 || (i.types[1].bitfield.reg && i.types[1].bitfield.byte))
29b0f896
AM
4317 && i.rex != 0))
4318 {
4319 int x;
726c5dcd 4320
29b0f896
AM
4321 i.rex |= REX_OPCODE;
4322 for (x = 0; x < 2; x++)
4323 {
4324 /* Look for 8 bit operand that uses old registers. */
dc821c5f 4325 if (i.types[x].bitfield.reg && i.types[x].bitfield.byte
29b0f896 4326 && (i.op[x].regs->reg_flags & RegRex64) == 0)
773f551c 4327 {
29b0f896
AM
4328 /* In case it is "hi" register, give up. */
4329 if (i.op[x].regs->reg_num > 3)
a540244d 4330 as_bad (_("can't encode register '%s%s' in an "
4eed87de 4331 "instruction requiring REX prefix."),
a540244d 4332 register_prefix, i.op[x].regs->reg_name);
773f551c 4333
29b0f896
AM
4334 /* Otherwise it is equivalent to the extended register.
4335 Since the encoding doesn't change this is merely
4336 cosmetic cleanup for debug output. */
4337
4338 i.op[x].regs = i.op[x].regs + 8;
773f551c 4339 }
29b0f896
AM
4340 }
4341 }
773f551c 4342
6b6b6807
L
4343 if (i.rex == 0 && i.rex_encoding)
4344 {
4345 /* Check if we can add a REX_OPCODE byte. Look for 8 bit operand
4346 that uses legacy register. If it is "hi" register, don't add
4347 the REX_OPCODE byte. */
4348 int x;
4349 for (x = 0; x < 2; x++)
4350 if (i.types[x].bitfield.reg
4351 && i.types[x].bitfield.byte
4352 && (i.op[x].regs->reg_flags & RegRex64) == 0
4353 && i.op[x].regs->reg_num > 3)
4354 {
4355 i.rex_encoding = FALSE;
4356 break;
4357 }
4358
4359 if (i.rex_encoding)
4360 i.rex = REX_OPCODE;
4361 }
4362
7ab9ffdd 4363 if (i.rex != 0)
29b0f896
AM
4364 add_prefix (REX_OPCODE | i.rex);
4365
4366 /* We are ready to output the insn. */
4367 output_insn ();
4368}
4369
4370static char *
e3bb37b5 4371parse_insn (char *line, char *mnemonic)
29b0f896
AM
4372{
4373 char *l = line;
4374 char *token_start = l;
4375 char *mnem_p;
5c6af06e 4376 int supported;
d3ce72d0 4377 const insn_template *t;
b6169b20 4378 char *dot_p = NULL;
29b0f896 4379
29b0f896
AM
4380 while (1)
4381 {
4382 mnem_p = mnemonic;
4383 while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
4384 {
b6169b20
L
4385 if (*mnem_p == '.')
4386 dot_p = mnem_p;
29b0f896
AM
4387 mnem_p++;
4388 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
45288df1 4389 {
29b0f896
AM
4390 as_bad (_("no such instruction: `%s'"), token_start);
4391 return NULL;
4392 }
4393 l++;
4394 }
4395 if (!is_space_char (*l)
4396 && *l != END_OF_INSN
e44823cf
JB
4397 && (intel_syntax
4398 || (*l != PREFIX_SEPARATOR
4399 && *l != ',')))
29b0f896
AM
4400 {
4401 as_bad (_("invalid character %s in mnemonic"),
4402 output_invalid (*l));
4403 return NULL;
4404 }
4405 if (token_start == l)
4406 {
e44823cf 4407 if (!intel_syntax && *l == PREFIX_SEPARATOR)
29b0f896
AM
4408 as_bad (_("expecting prefix; got nothing"));
4409 else
4410 as_bad (_("expecting mnemonic; got nothing"));
4411 return NULL;
4412 }
45288df1 4413
29b0f896 4414 /* Look up instruction (or prefix) via hash table. */
d3ce72d0 4415 current_templates = (const templates *) hash_find (op_hash, mnemonic);
47926f60 4416
29b0f896
AM
4417 if (*l != END_OF_INSN
4418 && (!is_space_char (*l) || l[1] != END_OF_INSN)
4419 && current_templates
40fb9820 4420 && current_templates->start->opcode_modifier.isprefix)
29b0f896 4421 {
c6fb90c8 4422 if (!cpu_flags_check_cpu64 (current_templates->start->cpu_flags))
2dd88dca
JB
4423 {
4424 as_bad ((flag_code != CODE_64BIT
4425 ? _("`%s' is only supported in 64-bit mode")
4426 : _("`%s' is not supported in 64-bit mode")),
4427 current_templates->start->name);
4428 return NULL;
4429 }
29b0f896
AM
4430 /* If we are in 16-bit mode, do not allow addr16 or data16.
4431 Similarly, in 32-bit mode, do not allow addr32 or data32. */
40fb9820
L
4432 if ((current_templates->start->opcode_modifier.size16
4433 || current_templates->start->opcode_modifier.size32)
29b0f896 4434 && flag_code != CODE_64BIT
40fb9820 4435 && (current_templates->start->opcode_modifier.size32
29b0f896
AM
4436 ^ (flag_code == CODE_16BIT)))
4437 {
4438 as_bad (_("redundant %s prefix"),
4439 current_templates->start->name);
4440 return NULL;
45288df1 4441 }
86fa6981 4442 if (current_templates->start->opcode_length == 0)
29b0f896 4443 {
86fa6981
L
4444 /* Handle pseudo prefixes. */
4445 switch (current_templates->start->base_opcode)
4446 {
4447 case 0x0:
4448 /* {disp8} */
4449 i.disp_encoding = disp_encoding_8bit;
4450 break;
4451 case 0x1:
4452 /* {disp32} */
4453 i.disp_encoding = disp_encoding_32bit;
4454 break;
4455 case 0x2:
4456 /* {load} */
4457 i.dir_encoding = dir_encoding_load;
4458 break;
4459 case 0x3:
4460 /* {store} */
4461 i.dir_encoding = dir_encoding_store;
4462 break;
4463 case 0x4:
4464 /* {vex2} */
4465 i.vec_encoding = vex_encoding_vex2;
4466 break;
4467 case 0x5:
4468 /* {vex3} */
4469 i.vec_encoding = vex_encoding_vex3;
4470 break;
4471 case 0x6:
4472 /* {evex} */
4473 i.vec_encoding = vex_encoding_evex;
4474 break;
6b6b6807
L
4475 case 0x7:
4476 /* {rex} */
4477 i.rex_encoding = TRUE;
4478 break;
b6f8c7c4
L
4479 case 0x8:
4480 /* {nooptimize} */
4481 i.no_optimize = TRUE;
4482 break;
86fa6981
L
4483 default:
4484 abort ();
4485 }
4486 }
4487 else
4488 {
4489 /* Add prefix, checking for repeated prefixes. */
4e9ac44a 4490 switch (add_prefix (current_templates->start->base_opcode))
86fa6981 4491 {
4e9ac44a
L
4492 case PREFIX_EXIST:
4493 return NULL;
4494 case PREFIX_DS:
d777820b 4495 if (current_templates->start->cpu_flags.bitfield.cpuibt)
4e9ac44a
L
4496 i.notrack_prefix = current_templates->start->name;
4497 break;
4498 case PREFIX_REP:
4499 if (current_templates->start->cpu_flags.bitfield.cpuhle)
4500 i.hle_prefix = current_templates->start->name;
4501 else if (current_templates->start->cpu_flags.bitfield.cpumpx)
4502 i.bnd_prefix = current_templates->start->name;
4503 else
4504 i.rep_prefix = current_templates->start->name;
4505 break;
4506 default:
4507 break;
86fa6981 4508 }
29b0f896
AM
4509 }
4510 /* Skip past PREFIX_SEPARATOR and reset token_start. */
4511 token_start = ++l;
4512 }
4513 else
4514 break;
4515 }
45288df1 4516
30a55f88 4517 if (!current_templates)
b6169b20 4518 {
07d5e953
JB
4519 /* Deprecated functionality (new code should use pseudo-prefixes instead):
4520 Check if we should swap operand or force 32bit displacement in
f8a5c266 4521 encoding. */
30a55f88 4522 if (mnem_p - 2 == dot_p && dot_p[1] == 's')
86fa6981 4523 i.dir_encoding = dir_encoding_store;
8d63c93e 4524 else if (mnem_p - 3 == dot_p
a501d77e
L
4525 && dot_p[1] == 'd'
4526 && dot_p[2] == '8')
4527 i.disp_encoding = disp_encoding_8bit;
8d63c93e 4528 else if (mnem_p - 4 == dot_p
f8a5c266
L
4529 && dot_p[1] == 'd'
4530 && dot_p[2] == '3'
4531 && dot_p[3] == '2')
a501d77e 4532 i.disp_encoding = disp_encoding_32bit;
30a55f88
L
4533 else
4534 goto check_suffix;
4535 mnem_p = dot_p;
4536 *dot_p = '\0';
d3ce72d0 4537 current_templates = (const templates *) hash_find (op_hash, mnemonic);
b6169b20
L
4538 }
4539
29b0f896
AM
4540 if (!current_templates)
4541 {
b6169b20 4542check_suffix:
29b0f896
AM
4543 /* See if we can get a match by trimming off a suffix. */
4544 switch (mnem_p[-1])
4545 {
4546 case WORD_MNEM_SUFFIX:
9306ca4a
JB
4547 if (intel_syntax && (intel_float_operand (mnemonic) & 2))
4548 i.suffix = SHORT_MNEM_SUFFIX;
4549 else
1a0670f3 4550 /* Fall through. */
29b0f896
AM
4551 case BYTE_MNEM_SUFFIX:
4552 case QWORD_MNEM_SUFFIX:
4553 i.suffix = mnem_p[-1];
4554 mnem_p[-1] = '\0';
d3ce72d0
NC
4555 current_templates = (const templates *) hash_find (op_hash,
4556 mnemonic);
29b0f896
AM
4557 break;
4558 case SHORT_MNEM_SUFFIX:
4559 case LONG_MNEM_SUFFIX:
4560 if (!intel_syntax)
4561 {
4562 i.suffix = mnem_p[-1];
4563 mnem_p[-1] = '\0';
d3ce72d0
NC
4564 current_templates = (const templates *) hash_find (op_hash,
4565 mnemonic);
29b0f896
AM
4566 }
4567 break;
252b5132 4568
29b0f896
AM
4569 /* Intel Syntax. */
4570 case 'd':
4571 if (intel_syntax)
4572 {
9306ca4a 4573 if (intel_float_operand (mnemonic) == 1)
29b0f896
AM
4574 i.suffix = SHORT_MNEM_SUFFIX;
4575 else
4576 i.suffix = LONG_MNEM_SUFFIX;
4577 mnem_p[-1] = '\0';
d3ce72d0
NC
4578 current_templates = (const templates *) hash_find (op_hash,
4579 mnemonic);
29b0f896
AM
4580 }
4581 break;
4582 }
4583 if (!current_templates)
4584 {
4585 as_bad (_("no such instruction: `%s'"), token_start);
4586 return NULL;
4587 }
4588 }
252b5132 4589
40fb9820
L
4590 if (current_templates->start->opcode_modifier.jump
4591 || current_templates->start->opcode_modifier.jumpbyte)
29b0f896
AM
4592 {
4593 /* Check for a branch hint. We allow ",pt" and ",pn" for
4594 predict taken and predict not taken respectively.
4595 I'm not sure that branch hints actually do anything on loop
4596 and jcxz insns (JumpByte) for current Pentium4 chips. They
4597 may work in the future and it doesn't hurt to accept them
4598 now. */
4599 if (l[0] == ',' && l[1] == 'p')
4600 {
4601 if (l[2] == 't')
4602 {
4603 if (!add_prefix (DS_PREFIX_OPCODE))
4604 return NULL;
4605 l += 3;
4606 }
4607 else if (l[2] == 'n')
4608 {
4609 if (!add_prefix (CS_PREFIX_OPCODE))
4610 return NULL;
4611 l += 3;
4612 }
4613 }
4614 }
4615 /* Any other comma loses. */
4616 if (*l == ',')
4617 {
4618 as_bad (_("invalid character %s in mnemonic"),
4619 output_invalid (*l));
4620 return NULL;
4621 }
252b5132 4622
29b0f896 4623 /* Check if instruction is supported on specified architecture. */
5c6af06e
JB
4624 supported = 0;
4625 for (t = current_templates->start; t < current_templates->end; ++t)
4626 {
c0f3af97
L
4627 supported |= cpu_flags_match (t);
4628 if (supported == CPU_FLAGS_PERFECT_MATCH)
548d0ee6
JB
4629 {
4630 if (!cpu_arch_flags.bitfield.cpui386 && (flag_code != CODE_16BIT))
4631 as_warn (_("use .code16 to ensure correct addressing mode"));
3629bb00 4632
548d0ee6
JB
4633 return l;
4634 }
29b0f896 4635 }
3629bb00 4636
548d0ee6
JB
4637 if (!(supported & CPU_FLAGS_64BIT_MATCH))
4638 as_bad (flag_code == CODE_64BIT
4639 ? _("`%s' is not supported in 64-bit mode")
4640 : _("`%s' is only supported in 64-bit mode"),
4641 current_templates->start->name);
4642 else
4643 as_bad (_("`%s' is not supported on `%s%s'"),
4644 current_templates->start->name,
4645 cpu_arch_name ? cpu_arch_name : default_arch,
4646 cpu_sub_arch_name ? cpu_sub_arch_name : "");
252b5132 4647
548d0ee6 4648 return NULL;
29b0f896 4649}
252b5132 4650
29b0f896 4651static char *
e3bb37b5 4652parse_operands (char *l, const char *mnemonic)
29b0f896
AM
4653{
4654 char *token_start;
3138f287 4655
29b0f896
AM
4656 /* 1 if operand is pending after ','. */
4657 unsigned int expecting_operand = 0;
252b5132 4658
29b0f896
AM
4659 /* Non-zero if operand parens not balanced. */
4660 unsigned int paren_not_balanced;
4661
4662 while (*l != END_OF_INSN)
4663 {
4664 /* Skip optional white space before operand. */
4665 if (is_space_char (*l))
4666 ++l;
d02603dc 4667 if (!is_operand_char (*l) && *l != END_OF_INSN && *l != '"')
29b0f896
AM
4668 {
4669 as_bad (_("invalid character %s before operand %d"),
4670 output_invalid (*l),
4671 i.operands + 1);
4672 return NULL;
4673 }
d02603dc 4674 token_start = l; /* After white space. */
29b0f896
AM
4675 paren_not_balanced = 0;
4676 while (paren_not_balanced || *l != ',')
4677 {
4678 if (*l == END_OF_INSN)
4679 {
4680 if (paren_not_balanced)
4681 {
4682 if (!intel_syntax)
4683 as_bad (_("unbalanced parenthesis in operand %d."),
4684 i.operands + 1);
4685 else
4686 as_bad (_("unbalanced brackets in operand %d."),
4687 i.operands + 1);
4688 return NULL;
4689 }
4690 else
4691 break; /* we are done */
4692 }
d02603dc 4693 else if (!is_operand_char (*l) && !is_space_char (*l) && *l != '"')
29b0f896
AM
4694 {
4695 as_bad (_("invalid character %s in operand %d"),
4696 output_invalid (*l),
4697 i.operands + 1);
4698 return NULL;
4699 }
4700 if (!intel_syntax)
4701 {
4702 if (*l == '(')
4703 ++paren_not_balanced;
4704 if (*l == ')')
4705 --paren_not_balanced;
4706 }
4707 else
4708 {
4709 if (*l == '[')
4710 ++paren_not_balanced;
4711 if (*l == ']')
4712 --paren_not_balanced;
4713 }
4714 l++;
4715 }
4716 if (l != token_start)
4717 { /* Yes, we've read in another operand. */
4718 unsigned int operand_ok;
4719 this_operand = i.operands++;
4720 if (i.operands > MAX_OPERANDS)
4721 {
4722 as_bad (_("spurious operands; (%d operands/instruction max)"),
4723 MAX_OPERANDS);
4724 return NULL;
4725 }
9d46ce34 4726 i.types[this_operand].bitfield.unspecified = 1;
29b0f896
AM
4727 /* Now parse operand adding info to 'i' as we go along. */
4728 END_STRING_AND_SAVE (l);
4729
1286ab78
L
4730 if (i.mem_operands > 1)
4731 {
4732 as_bad (_("too many memory references for `%s'"),
4733 mnemonic);
4734 return 0;
4735 }
4736
29b0f896
AM
4737 if (intel_syntax)
4738 operand_ok =
4739 i386_intel_operand (token_start,
4740 intel_float_operand (mnemonic));
4741 else
a7619375 4742 operand_ok = i386_att_operand (token_start);
29b0f896
AM
4743
4744 RESTORE_END_STRING (l);
4745 if (!operand_ok)
4746 return NULL;
4747 }
4748 else
4749 {
4750 if (expecting_operand)
4751 {
4752 expecting_operand_after_comma:
4753 as_bad (_("expecting operand after ','; got nothing"));
4754 return NULL;
4755 }
4756 if (*l == ',')
4757 {
4758 as_bad (_("expecting operand before ','; got nothing"));
4759 return NULL;
4760 }
4761 }
7f3f1ea2 4762
29b0f896
AM
4763 /* Now *l must be either ',' or END_OF_INSN. */
4764 if (*l == ',')
4765 {
4766 if (*++l == END_OF_INSN)
4767 {
4768 /* Just skip it, if it's \n complain. */
4769 goto expecting_operand_after_comma;
4770 }
4771 expecting_operand = 1;
4772 }
4773 }
4774 return l;
4775}
7f3f1ea2 4776
050dfa73 4777static void
4d456e3d 4778swap_2_operands (int xchg1, int xchg2)
050dfa73
MM
4779{
4780 union i386_op temp_op;
40fb9820 4781 i386_operand_type temp_type;
c48dadc9 4782 unsigned int temp_flags;
050dfa73 4783 enum bfd_reloc_code_real temp_reloc;
4eed87de 4784
050dfa73
MM
4785 temp_type = i.types[xchg2];
4786 i.types[xchg2] = i.types[xchg1];
4787 i.types[xchg1] = temp_type;
c48dadc9
JB
4788
4789 temp_flags = i.flags[xchg2];
4790 i.flags[xchg2] = i.flags[xchg1];
4791 i.flags[xchg1] = temp_flags;
4792
050dfa73
MM
4793 temp_op = i.op[xchg2];
4794 i.op[xchg2] = i.op[xchg1];
4795 i.op[xchg1] = temp_op;
c48dadc9 4796
050dfa73
MM
4797 temp_reloc = i.reloc[xchg2];
4798 i.reloc[xchg2] = i.reloc[xchg1];
4799 i.reloc[xchg1] = temp_reloc;
43234a1e
L
4800
4801 if (i.mask)
4802 {
4803 if (i.mask->operand == xchg1)
4804 i.mask->operand = xchg2;
4805 else if (i.mask->operand == xchg2)
4806 i.mask->operand = xchg1;
4807 }
4808 if (i.broadcast)
4809 {
4810 if (i.broadcast->operand == xchg1)
4811 i.broadcast->operand = xchg2;
4812 else if (i.broadcast->operand == xchg2)
4813 i.broadcast->operand = xchg1;
4814 }
4815 if (i.rounding)
4816 {
4817 if (i.rounding->operand == xchg1)
4818 i.rounding->operand = xchg2;
4819 else if (i.rounding->operand == xchg2)
4820 i.rounding->operand = xchg1;
4821 }
050dfa73
MM
4822}
4823
29b0f896 4824static void
e3bb37b5 4825swap_operands (void)
29b0f896 4826{
b7c61d9a 4827 switch (i.operands)
050dfa73 4828 {
c0f3af97 4829 case 5:
b7c61d9a 4830 case 4:
4d456e3d 4831 swap_2_operands (1, i.operands - 2);
1a0670f3 4832 /* Fall through. */
b7c61d9a
L
4833 case 3:
4834 case 2:
4d456e3d 4835 swap_2_operands (0, i.operands - 1);
b7c61d9a
L
4836 break;
4837 default:
4838 abort ();
29b0f896 4839 }
29b0f896
AM
4840
4841 if (i.mem_operands == 2)
4842 {
4843 const seg_entry *temp_seg;
4844 temp_seg = i.seg[0];
4845 i.seg[0] = i.seg[1];
4846 i.seg[1] = temp_seg;
4847 }
4848}
252b5132 4849
29b0f896
AM
4850/* Try to ensure constant immediates are represented in the smallest
4851 opcode possible. */
4852static void
e3bb37b5 4853optimize_imm (void)
29b0f896
AM
4854{
4855 char guess_suffix = 0;
4856 int op;
252b5132 4857
29b0f896
AM
4858 if (i.suffix)
4859 guess_suffix = i.suffix;
4860 else if (i.reg_operands)
4861 {
4862 /* Figure out a suffix from the last register operand specified.
4863 We can't do this properly yet, ie. excluding InOutPortReg,
4864 but the following works for instructions with immediates.
4865 In any case, we can't set i.suffix yet. */
4866 for (op = i.operands; --op >= 0;)
dc821c5f 4867 if (i.types[op].bitfield.reg && i.types[op].bitfield.byte)
7ab9ffdd 4868 {
40fb9820
L
4869 guess_suffix = BYTE_MNEM_SUFFIX;
4870 break;
4871 }
dc821c5f 4872 else if (i.types[op].bitfield.reg && i.types[op].bitfield.word)
252b5132 4873 {
40fb9820
L
4874 guess_suffix = WORD_MNEM_SUFFIX;
4875 break;
4876 }
dc821c5f 4877 else if (i.types[op].bitfield.reg && i.types[op].bitfield.dword)
40fb9820
L
4878 {
4879 guess_suffix = LONG_MNEM_SUFFIX;
4880 break;
4881 }
dc821c5f 4882 else if (i.types[op].bitfield.reg && i.types[op].bitfield.qword)
40fb9820
L
4883 {
4884 guess_suffix = QWORD_MNEM_SUFFIX;
29b0f896 4885 break;
252b5132 4886 }
29b0f896
AM
4887 }
4888 else if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
4889 guess_suffix = WORD_MNEM_SUFFIX;
4890
4891 for (op = i.operands; --op >= 0;)
40fb9820 4892 if (operand_type_check (i.types[op], imm))
29b0f896
AM
4893 {
4894 switch (i.op[op].imms->X_op)
252b5132 4895 {
29b0f896
AM
4896 case O_constant:
4897 /* If a suffix is given, this operand may be shortened. */
4898 switch (guess_suffix)
252b5132 4899 {
29b0f896 4900 case LONG_MNEM_SUFFIX:
40fb9820
L
4901 i.types[op].bitfield.imm32 = 1;
4902 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
4903 break;
4904 case WORD_MNEM_SUFFIX:
40fb9820
L
4905 i.types[op].bitfield.imm16 = 1;
4906 i.types[op].bitfield.imm32 = 1;
4907 i.types[op].bitfield.imm32s = 1;
4908 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
4909 break;
4910 case BYTE_MNEM_SUFFIX:
40fb9820
L
4911 i.types[op].bitfield.imm8 = 1;
4912 i.types[op].bitfield.imm8s = 1;
4913 i.types[op].bitfield.imm16 = 1;
4914 i.types[op].bitfield.imm32 = 1;
4915 i.types[op].bitfield.imm32s = 1;
4916 i.types[op].bitfield.imm64 = 1;
29b0f896 4917 break;
252b5132 4918 }
252b5132 4919
29b0f896
AM
4920 /* If this operand is at most 16 bits, convert it
4921 to a signed 16 bit number before trying to see
4922 whether it will fit in an even smaller size.
4923 This allows a 16-bit operand such as $0xffe0 to
4924 be recognised as within Imm8S range. */
40fb9820 4925 if ((i.types[op].bitfield.imm16)
29b0f896 4926 && (i.op[op].imms->X_add_number & ~(offsetT) 0xffff) == 0)
252b5132 4927 {
29b0f896
AM
4928 i.op[op].imms->X_add_number =
4929 (((i.op[op].imms->X_add_number & 0xffff) ^ 0x8000) - 0x8000);
4930 }
a28def75
L
4931#ifdef BFD64
4932 /* Store 32-bit immediate in 64-bit for 64-bit BFD. */
40fb9820 4933 if ((i.types[op].bitfield.imm32)
29b0f896
AM
4934 && ((i.op[op].imms->X_add_number & ~(((offsetT) 2 << 31) - 1))
4935 == 0))
4936 {
4937 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
4938 ^ ((offsetT) 1 << 31))
4939 - ((offsetT) 1 << 31));
4940 }
a28def75 4941#endif
40fb9820 4942 i.types[op]
c6fb90c8
L
4943 = operand_type_or (i.types[op],
4944 smallest_imm_type (i.op[op].imms->X_add_number));
252b5132 4945
29b0f896
AM
4946 /* We must avoid matching of Imm32 templates when 64bit
4947 only immediate is available. */
4948 if (guess_suffix == QWORD_MNEM_SUFFIX)
40fb9820 4949 i.types[op].bitfield.imm32 = 0;
29b0f896 4950 break;
252b5132 4951
29b0f896
AM
4952 case O_absent:
4953 case O_register:
4954 abort ();
4955
4956 /* Symbols and expressions. */
4957 default:
9cd96992
JB
4958 /* Convert symbolic operand to proper sizes for matching, but don't
4959 prevent matching a set of insns that only supports sizes other
4960 than those matching the insn suffix. */
4961 {
40fb9820 4962 i386_operand_type mask, allowed;
d3ce72d0 4963 const insn_template *t;
9cd96992 4964
0dfbf9d7
L
4965 operand_type_set (&mask, 0);
4966 operand_type_set (&allowed, 0);
40fb9820 4967
4eed87de
AM
4968 for (t = current_templates->start;
4969 t < current_templates->end;
4970 ++t)
c6fb90c8
L
4971 allowed = operand_type_or (allowed,
4972 t->operand_types[op]);
9cd96992
JB
4973 switch (guess_suffix)
4974 {
4975 case QWORD_MNEM_SUFFIX:
40fb9820
L
4976 mask.bitfield.imm64 = 1;
4977 mask.bitfield.imm32s = 1;
9cd96992
JB
4978 break;
4979 case LONG_MNEM_SUFFIX:
40fb9820 4980 mask.bitfield.imm32 = 1;
9cd96992
JB
4981 break;
4982 case WORD_MNEM_SUFFIX:
40fb9820 4983 mask.bitfield.imm16 = 1;
9cd96992
JB
4984 break;
4985 case BYTE_MNEM_SUFFIX:
40fb9820 4986 mask.bitfield.imm8 = 1;
9cd96992
JB
4987 break;
4988 default:
9cd96992
JB
4989 break;
4990 }
c6fb90c8 4991 allowed = operand_type_and (mask, allowed);
0dfbf9d7 4992 if (!operand_type_all_zero (&allowed))
c6fb90c8 4993 i.types[op] = operand_type_and (i.types[op], mask);
9cd96992 4994 }
29b0f896 4995 break;
252b5132 4996 }
29b0f896
AM
4997 }
4998}
47926f60 4999
29b0f896
AM
5000/* Try to use the smallest displacement type too. */
5001static void
e3bb37b5 5002optimize_disp (void)
29b0f896
AM
5003{
5004 int op;
3e73aa7c 5005
29b0f896 5006 for (op = i.operands; --op >= 0;)
40fb9820 5007 if (operand_type_check (i.types[op], disp))
252b5132 5008 {
b300c311 5009 if (i.op[op].disps->X_op == O_constant)
252b5132 5010 {
91d6fa6a 5011 offsetT op_disp = i.op[op].disps->X_add_number;
29b0f896 5012
40fb9820 5013 if (i.types[op].bitfield.disp16
91d6fa6a 5014 && (op_disp & ~(offsetT) 0xffff) == 0)
b300c311
L
5015 {
5016 /* If this operand is at most 16 bits, convert
5017 to a signed 16 bit number and don't use 64bit
5018 displacement. */
91d6fa6a 5019 op_disp = (((op_disp & 0xffff) ^ 0x8000) - 0x8000);
40fb9820 5020 i.types[op].bitfield.disp64 = 0;
b300c311 5021 }
a28def75
L
5022#ifdef BFD64
5023 /* Optimize 64-bit displacement to 32-bit for 64-bit BFD. */
40fb9820 5024 if (i.types[op].bitfield.disp32
91d6fa6a 5025 && (op_disp & ~(((offsetT) 2 << 31) - 1)) == 0)
b300c311
L
5026 {
5027 /* If this operand is at most 32 bits, convert
5028 to a signed 32 bit number and don't use 64bit
5029 displacement. */
91d6fa6a
NC
5030 op_disp &= (((offsetT) 2 << 31) - 1);
5031 op_disp = (op_disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
40fb9820 5032 i.types[op].bitfield.disp64 = 0;
b300c311 5033 }
a28def75 5034#endif
91d6fa6a 5035 if (!op_disp && i.types[op].bitfield.baseindex)
b300c311 5036 {
40fb9820
L
5037 i.types[op].bitfield.disp8 = 0;
5038 i.types[op].bitfield.disp16 = 0;
5039 i.types[op].bitfield.disp32 = 0;
5040 i.types[op].bitfield.disp32s = 0;
5041 i.types[op].bitfield.disp64 = 0;
b300c311
L
5042 i.op[op].disps = 0;
5043 i.disp_operands--;
5044 }
5045 else if (flag_code == CODE_64BIT)
5046 {
91d6fa6a 5047 if (fits_in_signed_long (op_disp))
28a9d8f5 5048 {
40fb9820
L
5049 i.types[op].bitfield.disp64 = 0;
5050 i.types[op].bitfield.disp32s = 1;
28a9d8f5 5051 }
0e1147d9 5052 if (i.prefix[ADDR_PREFIX]
91d6fa6a 5053 && fits_in_unsigned_long (op_disp))
40fb9820 5054 i.types[op].bitfield.disp32 = 1;
b300c311 5055 }
40fb9820
L
5056 if ((i.types[op].bitfield.disp32
5057 || i.types[op].bitfield.disp32s
5058 || i.types[op].bitfield.disp16)
b5014f7a 5059 && fits_in_disp8 (op_disp))
40fb9820 5060 i.types[op].bitfield.disp8 = 1;
252b5132 5061 }
67a4f2b7
AO
5062 else if (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
5063 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL)
5064 {
5065 fix_new_exp (frag_now, frag_more (0) - frag_now->fr_literal, 0,
5066 i.op[op].disps, 0, i.reloc[op]);
40fb9820
L
5067 i.types[op].bitfield.disp8 = 0;
5068 i.types[op].bitfield.disp16 = 0;
5069 i.types[op].bitfield.disp32 = 0;
5070 i.types[op].bitfield.disp32s = 0;
5071 i.types[op].bitfield.disp64 = 0;
67a4f2b7
AO
5072 }
5073 else
b300c311 5074 /* We only support 64bit displacement on constants. */
40fb9820 5075 i.types[op].bitfield.disp64 = 0;
252b5132 5076 }
29b0f896
AM
5077}
5078
4a1b91ea
L
5079/* Return 1 if there is a match in broadcast bytes between operand
5080 GIVEN and instruction template T. */
5081
5082static INLINE int
5083match_broadcast_size (const insn_template *t, unsigned int given)
5084{
5085 return ((t->opcode_modifier.broadcast == BYTE_BROADCAST
5086 && i.types[given].bitfield.byte)
5087 || (t->opcode_modifier.broadcast == WORD_BROADCAST
5088 && i.types[given].bitfield.word)
5089 || (t->opcode_modifier.broadcast == DWORD_BROADCAST
5090 && i.types[given].bitfield.dword)
5091 || (t->opcode_modifier.broadcast == QWORD_BROADCAST
5092 && i.types[given].bitfield.qword));
5093}
5094
6c30d220
L
5095/* Check if operands are valid for the instruction. */
5096
5097static int
5098check_VecOperands (const insn_template *t)
5099{
43234a1e 5100 unsigned int op;
e2195274
JB
5101 i386_cpu_flags cpu;
5102 static const i386_cpu_flags avx512 = CPU_ANY_AVX512F_FLAGS;
5103
5104 /* Templates allowing for ZMMword as well as YMMword and/or XMMword for
5105 any one operand are implicity requiring AVX512VL support if the actual
5106 operand size is YMMword or XMMword. Since this function runs after
5107 template matching, there's no need to check for YMMword/XMMword in
5108 the template. */
5109 cpu = cpu_flags_and (t->cpu_flags, avx512);
5110 if (!cpu_flags_all_zero (&cpu)
5111 && !t->cpu_flags.bitfield.cpuavx512vl
5112 && !cpu_arch_flags.bitfield.cpuavx512vl)
5113 {
5114 for (op = 0; op < t->operands; ++op)
5115 {
5116 if (t->operand_types[op].bitfield.zmmword
5117 && (i.types[op].bitfield.ymmword
5118 || i.types[op].bitfield.xmmword))
5119 {
5120 i.error = unsupported;
5121 return 1;
5122 }
5123 }
5124 }
43234a1e 5125
6c30d220
L
5126 /* Without VSIB byte, we can't have a vector register for index. */
5127 if (!t->opcode_modifier.vecsib
5128 && i.index_reg
1b54b8d7
JB
5129 && (i.index_reg->reg_type.bitfield.xmmword
5130 || i.index_reg->reg_type.bitfield.ymmword
5131 || i.index_reg->reg_type.bitfield.zmmword))
6c30d220
L
5132 {
5133 i.error = unsupported_vector_index_register;
5134 return 1;
5135 }
5136
ad8ecc81
MZ
5137 /* Check if default mask is allowed. */
5138 if (t->opcode_modifier.nodefmask
5139 && (!i.mask || i.mask->mask->reg_num == 0))
5140 {
5141 i.error = no_default_mask;
5142 return 1;
5143 }
5144
7bab8ab5
JB
5145 /* For VSIB byte, we need a vector register for index, and all vector
5146 registers must be distinct. */
5147 if (t->opcode_modifier.vecsib)
5148 {
5149 if (!i.index_reg
6c30d220 5150 || !((t->opcode_modifier.vecsib == VecSIB128
1b54b8d7 5151 && i.index_reg->reg_type.bitfield.xmmword)
6c30d220 5152 || (t->opcode_modifier.vecsib == VecSIB256
1b54b8d7 5153 && i.index_reg->reg_type.bitfield.ymmword)
43234a1e 5154 || (t->opcode_modifier.vecsib == VecSIB512
1b54b8d7 5155 && i.index_reg->reg_type.bitfield.zmmword)))
7bab8ab5
JB
5156 {
5157 i.error = invalid_vsib_address;
5158 return 1;
5159 }
5160
43234a1e
L
5161 gas_assert (i.reg_operands == 2 || i.mask);
5162 if (i.reg_operands == 2 && !i.mask)
5163 {
1b54b8d7
JB
5164 gas_assert (i.types[0].bitfield.regsimd);
5165 gas_assert (i.types[0].bitfield.xmmword
5166 || i.types[0].bitfield.ymmword);
5167 gas_assert (i.types[2].bitfield.regsimd);
5168 gas_assert (i.types[2].bitfield.xmmword
5169 || i.types[2].bitfield.ymmword);
43234a1e
L
5170 if (operand_check == check_none)
5171 return 0;
5172 if (register_number (i.op[0].regs)
5173 != register_number (i.index_reg)
5174 && register_number (i.op[2].regs)
5175 != register_number (i.index_reg)
5176 && register_number (i.op[0].regs)
5177 != register_number (i.op[2].regs))
5178 return 0;
5179 if (operand_check == check_error)
5180 {
5181 i.error = invalid_vector_register_set;
5182 return 1;
5183 }
5184 as_warn (_("mask, index, and destination registers should be distinct"));
5185 }
8444f82a
MZ
5186 else if (i.reg_operands == 1 && i.mask)
5187 {
1b54b8d7
JB
5188 if (i.types[1].bitfield.regsimd
5189 && (i.types[1].bitfield.xmmword
5190 || i.types[1].bitfield.ymmword
5191 || i.types[1].bitfield.zmmword)
8444f82a
MZ
5192 && (register_number (i.op[1].regs)
5193 == register_number (i.index_reg)))
5194 {
5195 if (operand_check == check_error)
5196 {
5197 i.error = invalid_vector_register_set;
5198 return 1;
5199 }
5200 if (operand_check != check_none)
5201 as_warn (_("index and destination registers should be distinct"));
5202 }
5203 }
43234a1e 5204 }
7bab8ab5 5205
43234a1e
L
5206 /* Check if broadcast is supported by the instruction and is applied
5207 to the memory operand. */
5208 if (i.broadcast)
5209 {
8e6e0792 5210 i386_operand_type type, overlap;
43234a1e
L
5211
5212 /* Check if specified broadcast is supported in this instruction,
4a1b91ea 5213 and its broadcast bytes match the memory operand. */
32546502 5214 op = i.broadcast->operand;
8e6e0792 5215 if (!t->opcode_modifier.broadcast
c48dadc9 5216 || !(i.flags[op] & Operand_Mem)
c39e5b26 5217 || (!i.types[op].bitfield.unspecified
4a1b91ea 5218 && !match_broadcast_size (t, op)))
43234a1e
L
5219 {
5220 bad_broadcast:
5221 i.error = unsupported_broadcast;
5222 return 1;
5223 }
8e6e0792 5224
4a1b91ea
L
5225 i.broadcast->bytes = ((1 << (t->opcode_modifier.broadcast - 1))
5226 * i.broadcast->type);
8e6e0792 5227 operand_type_set (&type, 0);
4a1b91ea 5228 switch (i.broadcast->bytes)
8e6e0792 5229 {
4a1b91ea
L
5230 case 2:
5231 type.bitfield.word = 1;
5232 break;
5233 case 4:
5234 type.bitfield.dword = 1;
5235 break;
8e6e0792
JB
5236 case 8:
5237 type.bitfield.qword = 1;
5238 break;
5239 case 16:
5240 type.bitfield.xmmword = 1;
5241 break;
5242 case 32:
5243 type.bitfield.ymmword = 1;
5244 break;
5245 case 64:
5246 type.bitfield.zmmword = 1;
5247 break;
5248 default:
5249 goto bad_broadcast;
5250 }
5251
5252 overlap = operand_type_and (type, t->operand_types[op]);
5253 if (operand_type_all_zero (&overlap))
5254 goto bad_broadcast;
5255
5256 if (t->opcode_modifier.checkregsize)
5257 {
5258 unsigned int j;
5259
e2195274 5260 type.bitfield.baseindex = 1;
8e6e0792
JB
5261 for (j = 0; j < i.operands; ++j)
5262 {
5263 if (j != op
5264 && !operand_type_register_match(i.types[j],
5265 t->operand_types[j],
5266 type,
5267 t->operand_types[op]))
5268 goto bad_broadcast;
5269 }
5270 }
43234a1e
L
5271 }
5272 /* If broadcast is supported in this instruction, we need to check if
5273 operand of one-element size isn't specified without broadcast. */
5274 else if (t->opcode_modifier.broadcast && i.mem_operands)
5275 {
5276 /* Find memory operand. */
5277 for (op = 0; op < i.operands; op++)
5278 if (operand_type_check (i.types[op], anymem))
5279 break;
5280 gas_assert (op < i.operands);
5281 /* Check size of the memory operand. */
4a1b91ea 5282 if (match_broadcast_size (t, op))
43234a1e
L
5283 {
5284 i.error = broadcast_needed;
5285 return 1;
5286 }
5287 }
c39e5b26
JB
5288 else
5289 op = MAX_OPERANDS - 1; /* Avoid uninitialized variable warning. */
43234a1e
L
5290
5291 /* Check if requested masking is supported. */
ae2387fe 5292 if (i.mask)
43234a1e 5293 {
ae2387fe
JB
5294 switch (t->opcode_modifier.masking)
5295 {
5296 case BOTH_MASKING:
5297 break;
5298 case MERGING_MASKING:
5299 if (i.mask->zeroing)
5300 {
5301 case 0:
5302 i.error = unsupported_masking;
5303 return 1;
5304 }
5305 break;
5306 case DYNAMIC_MASKING:
5307 /* Memory destinations allow only merging masking. */
5308 if (i.mask->zeroing && i.mem_operands)
5309 {
5310 /* Find memory operand. */
5311 for (op = 0; op < i.operands; op++)
c48dadc9 5312 if (i.flags[op] & Operand_Mem)
ae2387fe
JB
5313 break;
5314 gas_assert (op < i.operands);
5315 if (op == i.operands - 1)
5316 {
5317 i.error = unsupported_masking;
5318 return 1;
5319 }
5320 }
5321 break;
5322 default:
5323 abort ();
5324 }
43234a1e
L
5325 }
5326
5327 /* Check if masking is applied to dest operand. */
5328 if (i.mask && (i.mask->operand != (int) (i.operands - 1)))
5329 {
5330 i.error = mask_not_on_destination;
5331 return 1;
5332 }
5333
43234a1e
L
5334 /* Check RC/SAE. */
5335 if (i.rounding)
5336 {
5337 if ((i.rounding->type != saeonly
5338 && !t->opcode_modifier.staticrounding)
5339 || (i.rounding->type == saeonly
5340 && (t->opcode_modifier.staticrounding
5341 || !t->opcode_modifier.sae)))
5342 {
5343 i.error = unsupported_rc_sae;
5344 return 1;
5345 }
5346 /* If the instruction has several immediate operands and one of
5347 them is rounding, the rounding operand should be the last
5348 immediate operand. */
5349 if (i.imm_operands > 1
5350 && i.rounding->operand != (int) (i.imm_operands - 1))
7bab8ab5 5351 {
43234a1e 5352 i.error = rc_sae_operand_not_last_imm;
7bab8ab5
JB
5353 return 1;
5354 }
6c30d220
L
5355 }
5356
43234a1e 5357 /* Check vector Disp8 operand. */
b5014f7a
JB
5358 if (t->opcode_modifier.disp8memshift
5359 && i.disp_encoding != disp_encoding_32bit)
43234a1e
L
5360 {
5361 if (i.broadcast)
4a1b91ea 5362 i.memshift = t->opcode_modifier.broadcast - 1;
7091c612 5363 else if (t->opcode_modifier.disp8memshift != DISP8_SHIFT_VL)
43234a1e 5364 i.memshift = t->opcode_modifier.disp8memshift;
7091c612
JB
5365 else
5366 {
5367 const i386_operand_type *type = NULL;
5368
5369 i.memshift = 0;
5370 for (op = 0; op < i.operands; op++)
5371 if (operand_type_check (i.types[op], anymem))
5372 {
4174bfff
JB
5373 if (t->opcode_modifier.evex == EVEXLIG)
5374 i.memshift = 2 + (i.suffix == QWORD_MNEM_SUFFIX);
5375 else if (t->operand_types[op].bitfield.xmmword
5376 + t->operand_types[op].bitfield.ymmword
5377 + t->operand_types[op].bitfield.zmmword <= 1)
7091c612
JB
5378 type = &t->operand_types[op];
5379 else if (!i.types[op].bitfield.unspecified)
5380 type = &i.types[op];
5381 }
4174bfff
JB
5382 else if (i.types[op].bitfield.regsimd
5383 && t->opcode_modifier.evex != EVEXLIG)
7091c612
JB
5384 {
5385 if (i.types[op].bitfield.zmmword)
5386 i.memshift = 6;
5387 else if (i.types[op].bitfield.ymmword && i.memshift < 5)
5388 i.memshift = 5;
5389 else if (i.types[op].bitfield.xmmword && i.memshift < 4)
5390 i.memshift = 4;
5391 }
5392
5393 if (type)
5394 {
5395 if (type->bitfield.zmmword)
5396 i.memshift = 6;
5397 else if (type->bitfield.ymmword)
5398 i.memshift = 5;
5399 else if (type->bitfield.xmmword)
5400 i.memshift = 4;
5401 }
5402
5403 /* For the check in fits_in_disp8(). */
5404 if (i.memshift == 0)
5405 i.memshift = -1;
5406 }
43234a1e
L
5407
5408 for (op = 0; op < i.operands; op++)
5409 if (operand_type_check (i.types[op], disp)
5410 && i.op[op].disps->X_op == O_constant)
5411 {
b5014f7a 5412 if (fits_in_disp8 (i.op[op].disps->X_add_number))
43234a1e 5413 {
b5014f7a
JB
5414 i.types[op].bitfield.disp8 = 1;
5415 return 0;
43234a1e 5416 }
b5014f7a 5417 i.types[op].bitfield.disp8 = 0;
43234a1e
L
5418 }
5419 }
b5014f7a
JB
5420
5421 i.memshift = 0;
43234a1e 5422
6c30d220
L
5423 return 0;
5424}
5425
43f3e2ee 5426/* Check if operands are valid for the instruction. Update VEX
a683cc34
SP
5427 operand types. */
5428
5429static int
5430VEX_check_operands (const insn_template *t)
5431{
86fa6981 5432 if (i.vec_encoding == vex_encoding_evex)
43234a1e 5433 {
86fa6981 5434 /* This instruction must be encoded with EVEX prefix. */
e771e7c9 5435 if (!is_evex_encoding (t))
86fa6981
L
5436 {
5437 i.error = unsupported;
5438 return 1;
5439 }
5440 return 0;
43234a1e
L
5441 }
5442
a683cc34 5443 if (!t->opcode_modifier.vex)
86fa6981
L
5444 {
5445 /* This instruction template doesn't have VEX prefix. */
5446 if (i.vec_encoding != vex_encoding_default)
5447 {
5448 i.error = unsupported;
5449 return 1;
5450 }
5451 return 0;
5452 }
a683cc34
SP
5453
5454 /* Only check VEX_Imm4, which must be the first operand. */
5455 if (t->operand_types[0].bitfield.vec_imm4)
5456 {
5457 if (i.op[0].imms->X_op != O_constant
5458 || !fits_in_imm4 (i.op[0].imms->X_add_number))
891edac4 5459 {
a65babc9 5460 i.error = bad_imm4;
891edac4
L
5461 return 1;
5462 }
a683cc34
SP
5463
5464 /* Turn off Imm8 so that update_imm won't complain. */
5465 i.types[0] = vec_imm4;
5466 }
5467
5468 return 0;
5469}
5470
d3ce72d0 5471static const insn_template *
83b16ac6 5472match_template (char mnem_suffix)
29b0f896
AM
5473{
5474 /* Points to template once we've found it. */
d3ce72d0 5475 const insn_template *t;
40fb9820 5476 i386_operand_type overlap0, overlap1, overlap2, overlap3;
c0f3af97 5477 i386_operand_type overlap4;
29b0f896 5478 unsigned int found_reverse_match;
83b16ac6 5479 i386_opcode_modifier suffix_check, mnemsuf_check;
40fb9820 5480 i386_operand_type operand_types [MAX_OPERANDS];
539e75ad 5481 int addr_prefix_disp;
a5c311ca 5482 unsigned int j;
3ac21baa 5483 unsigned int found_cpu_match, size_match;
45664ddb 5484 unsigned int check_register;
5614d22c 5485 enum i386_error specific_error = 0;
29b0f896 5486
c0f3af97
L
5487#if MAX_OPERANDS != 5
5488# error "MAX_OPERANDS must be 5."
f48ff2ae
L
5489#endif
5490
29b0f896 5491 found_reverse_match = 0;
539e75ad 5492 addr_prefix_disp = -1;
40fb9820
L
5493
5494 memset (&suffix_check, 0, sizeof (suffix_check));
e2195274
JB
5495 if (intel_syntax && i.broadcast)
5496 /* nothing */;
5497 else if (i.suffix == BYTE_MNEM_SUFFIX)
40fb9820
L
5498 suffix_check.no_bsuf = 1;
5499 else if (i.suffix == WORD_MNEM_SUFFIX)
5500 suffix_check.no_wsuf = 1;
5501 else if (i.suffix == SHORT_MNEM_SUFFIX)
5502 suffix_check.no_ssuf = 1;
5503 else if (i.suffix == LONG_MNEM_SUFFIX)
5504 suffix_check.no_lsuf = 1;
5505 else if (i.suffix == QWORD_MNEM_SUFFIX)
5506 suffix_check.no_qsuf = 1;
5507 else if (i.suffix == LONG_DOUBLE_MNEM_SUFFIX)
7ce189b3 5508 suffix_check.no_ldsuf = 1;
29b0f896 5509
83b16ac6
JB
5510 memset (&mnemsuf_check, 0, sizeof (mnemsuf_check));
5511 if (intel_syntax)
5512 {
5513 switch (mnem_suffix)
5514 {
5515 case BYTE_MNEM_SUFFIX: mnemsuf_check.no_bsuf = 1; break;
5516 case WORD_MNEM_SUFFIX: mnemsuf_check.no_wsuf = 1; break;
5517 case SHORT_MNEM_SUFFIX: mnemsuf_check.no_ssuf = 1; break;
5518 case LONG_MNEM_SUFFIX: mnemsuf_check.no_lsuf = 1; break;
5519 case QWORD_MNEM_SUFFIX: mnemsuf_check.no_qsuf = 1; break;
5520 }
5521 }
5522
01559ecc
L
5523 /* Must have right number of operands. */
5524 i.error = number_of_operands_mismatch;
5525
45aa61fe 5526 for (t = current_templates->start; t < current_templates->end; t++)
29b0f896 5527 {
539e75ad
L
5528 addr_prefix_disp = -1;
5529
29b0f896
AM
5530 if (i.operands != t->operands)
5531 continue;
5532
50aecf8c 5533 /* Check processor support. */
a65babc9 5534 i.error = unsupported;
c0f3af97
L
5535 found_cpu_match = (cpu_flags_match (t)
5536 == CPU_FLAGS_PERFECT_MATCH);
50aecf8c
L
5537 if (!found_cpu_match)
5538 continue;
5539
e1d4d893 5540 /* Check AT&T mnemonic. */
a65babc9 5541 i.error = unsupported_with_intel_mnemonic;
e1d4d893 5542 if (intel_mnemonic && t->opcode_modifier.attmnemonic)
1efbbeb4
L
5543 continue;
5544
e92bae62 5545 /* Check AT&T/Intel syntax and Intel64/AMD64 ISA. */
a65babc9 5546 i.error = unsupported_syntax;
5c07affc 5547 if ((intel_syntax && t->opcode_modifier.attsyntax)
e92bae62
L
5548 || (!intel_syntax && t->opcode_modifier.intelsyntax)
5549 || (intel64 && t->opcode_modifier.amd64)
5550 || (!intel64 && t->opcode_modifier.intel64))
1efbbeb4
L
5551 continue;
5552
20592a94 5553 /* Check the suffix, except for some instructions in intel mode. */
a65babc9 5554 i.error = invalid_instruction_suffix;
567e4e96
L
5555 if ((!intel_syntax || !t->opcode_modifier.ignoresize)
5556 && ((t->opcode_modifier.no_bsuf && suffix_check.no_bsuf)
5557 || (t->opcode_modifier.no_wsuf && suffix_check.no_wsuf)
5558 || (t->opcode_modifier.no_lsuf && suffix_check.no_lsuf)
5559 || (t->opcode_modifier.no_ssuf && suffix_check.no_ssuf)
5560 || (t->opcode_modifier.no_qsuf && suffix_check.no_qsuf)
5561 || (t->opcode_modifier.no_ldsuf && suffix_check.no_ldsuf)))
29b0f896 5562 continue;
83b16ac6
JB
5563 /* In Intel mode all mnemonic suffixes must be explicitly allowed. */
5564 if ((t->opcode_modifier.no_bsuf && mnemsuf_check.no_bsuf)
5565 || (t->opcode_modifier.no_wsuf && mnemsuf_check.no_wsuf)
5566 || (t->opcode_modifier.no_lsuf && mnemsuf_check.no_lsuf)
5567 || (t->opcode_modifier.no_ssuf && mnemsuf_check.no_ssuf)
5568 || (t->opcode_modifier.no_qsuf && mnemsuf_check.no_qsuf)
5569 || (t->opcode_modifier.no_ldsuf && mnemsuf_check.no_ldsuf))
5570 continue;
29b0f896 5571
3ac21baa
JB
5572 size_match = operand_size_match (t);
5573 if (!size_match)
7d5e4556 5574 continue;
539e75ad 5575
5c07affc
L
5576 for (j = 0; j < MAX_OPERANDS; j++)
5577 operand_types[j] = t->operand_types[j];
5578
45aa61fe
AM
5579 /* In general, don't allow 64-bit operands in 32-bit mode. */
5580 if (i.suffix == QWORD_MNEM_SUFFIX
5581 && flag_code != CODE_64BIT
5582 && (intel_syntax
40fb9820 5583 ? (!t->opcode_modifier.ignoresize
625cbd7a 5584 && !t->opcode_modifier.broadcast
45aa61fe
AM
5585 && !intel_float_operand (t->name))
5586 : intel_float_operand (t->name) != 2)
40fb9820 5587 && ((!operand_types[0].bitfield.regmmx
1b54b8d7 5588 && !operand_types[0].bitfield.regsimd)
40fb9820 5589 || (!operand_types[t->operands > 1].bitfield.regmmx
1b54b8d7 5590 && !operand_types[t->operands > 1].bitfield.regsimd))
45aa61fe
AM
5591 && (t->base_opcode != 0x0fc7
5592 || t->extension_opcode != 1 /* cmpxchg8b */))
5593 continue;
5594
192dc9c6
JB
5595 /* In general, don't allow 32-bit operands on pre-386. */
5596 else if (i.suffix == LONG_MNEM_SUFFIX
5597 && !cpu_arch_flags.bitfield.cpui386
5598 && (intel_syntax
5599 ? (!t->opcode_modifier.ignoresize
5600 && !intel_float_operand (t->name))
5601 : intel_float_operand (t->name) != 2)
5602 && ((!operand_types[0].bitfield.regmmx
1b54b8d7 5603 && !operand_types[0].bitfield.regsimd)
192dc9c6 5604 || (!operand_types[t->operands > 1].bitfield.regmmx
1b54b8d7 5605 && !operand_types[t->operands > 1].bitfield.regsimd)))
192dc9c6
JB
5606 continue;
5607
29b0f896 5608 /* Do not verify operands when there are none. */
50aecf8c 5609 else
29b0f896 5610 {
c6fb90c8 5611 if (!t->operands)
2dbab7d5
L
5612 /* We've found a match; break out of loop. */
5613 break;
29b0f896 5614 }
252b5132 5615
539e75ad
L
5616 /* Address size prefix will turn Disp64/Disp32/Disp16 operand
5617 into Disp32/Disp16/Disp32 operand. */
5618 if (i.prefix[ADDR_PREFIX] != 0)
5619 {
40fb9820 5620 /* There should be only one Disp operand. */
539e75ad
L
5621 switch (flag_code)
5622 {
5623 case CODE_16BIT:
40fb9820
L
5624 for (j = 0; j < MAX_OPERANDS; j++)
5625 {
5626 if (operand_types[j].bitfield.disp16)
5627 {
5628 addr_prefix_disp = j;
5629 operand_types[j].bitfield.disp32 = 1;
5630 operand_types[j].bitfield.disp16 = 0;
5631 break;
5632 }
5633 }
539e75ad
L
5634 break;
5635 case CODE_32BIT:
40fb9820
L
5636 for (j = 0; j < MAX_OPERANDS; j++)
5637 {
5638 if (operand_types[j].bitfield.disp32)
5639 {
5640 addr_prefix_disp = j;
5641 operand_types[j].bitfield.disp32 = 0;
5642 operand_types[j].bitfield.disp16 = 1;
5643 break;
5644 }
5645 }
539e75ad
L
5646 break;
5647 case CODE_64BIT:
40fb9820
L
5648 for (j = 0; j < MAX_OPERANDS; j++)
5649 {
5650 if (operand_types[j].bitfield.disp64)
5651 {
5652 addr_prefix_disp = j;
5653 operand_types[j].bitfield.disp64 = 0;
5654 operand_types[j].bitfield.disp32 = 1;
5655 break;
5656 }
5657 }
539e75ad
L
5658 break;
5659 }
539e75ad
L
5660 }
5661
02a86693
L
5662 /* Force 0x8b encoding for "mov foo@GOT, %eax". */
5663 if (i.reloc[0] == BFD_RELOC_386_GOT32 && t->base_opcode == 0xa0)
5664 continue;
5665
56ffb741 5666 /* We check register size if needed. */
e2195274
JB
5667 if (t->opcode_modifier.checkregsize)
5668 {
5669 check_register = (1 << t->operands) - 1;
5670 if (i.broadcast)
5671 check_register &= ~(1 << i.broadcast->operand);
5672 }
5673 else
5674 check_register = 0;
5675
c6fb90c8 5676 overlap0 = operand_type_and (i.types[0], operand_types[0]);
29b0f896
AM
5677 switch (t->operands)
5678 {
5679 case 1:
40fb9820 5680 if (!operand_type_match (overlap0, i.types[0]))
29b0f896
AM
5681 continue;
5682 break;
5683 case 2:
33eaf5de 5684 /* xchg %eax, %eax is a special case. It is an alias for nop
8b38ad71
L
5685 only in 32bit mode and we can use opcode 0x90. In 64bit
5686 mode, we can't use 0x90 for xchg %eax, %eax since it should
5687 zero-extend %eax to %rax. */
5688 if (flag_code == CODE_64BIT
5689 && t->base_opcode == 0x90
0dfbf9d7
L
5690 && operand_type_equal (&i.types [0], &acc32)
5691 && operand_type_equal (&i.types [1], &acc32))
8b38ad71 5692 continue;
1212781b
JB
5693 /* xrelease mov %eax, <disp> is another special case. It must not
5694 match the accumulator-only encoding of mov. */
5695 if (flag_code != CODE_64BIT
5696 && i.hle_prefix
5697 && t->base_opcode == 0xa0
5698 && i.types[0].bitfield.acc
5699 && operand_type_check (i.types[1], anymem))
5700 continue;
3ac21baa
JB
5701 if (!(size_match & MATCH_STRAIGHT))
5702 goto check_reverse;
86fa6981
L
5703 /* If we want store form, we reverse direction of operands. */
5704 if (i.dir_encoding == dir_encoding_store
5705 && t->opcode_modifier.d)
5706 goto check_reverse;
1a0670f3 5707 /* Fall through. */
b6169b20 5708
29b0f896 5709 case 3:
86fa6981
L
5710 /* If we want store form, we skip the current load. */
5711 if (i.dir_encoding == dir_encoding_store
5712 && i.mem_operands == 0
5713 && t->opcode_modifier.load)
fa99fab2 5714 continue;
1a0670f3 5715 /* Fall through. */
f48ff2ae 5716 case 4:
c0f3af97 5717 case 5:
c6fb90c8 5718 overlap1 = operand_type_and (i.types[1], operand_types[1]);
40fb9820
L
5719 if (!operand_type_match (overlap0, i.types[0])
5720 || !operand_type_match (overlap1, i.types[1])
e2195274 5721 || ((check_register & 3) == 3
dc821c5f 5722 && !operand_type_register_match (i.types[0],
40fb9820 5723 operand_types[0],
dc821c5f 5724 i.types[1],
40fb9820 5725 operand_types[1])))
29b0f896
AM
5726 {
5727 /* Check if other direction is valid ... */
38e314eb 5728 if (!t->opcode_modifier.d)
29b0f896
AM
5729 continue;
5730
b6169b20 5731check_reverse:
3ac21baa
JB
5732 if (!(size_match & MATCH_REVERSE))
5733 continue;
29b0f896 5734 /* Try reversing direction of operands. */
c6fb90c8
L
5735 overlap0 = operand_type_and (i.types[0], operand_types[1]);
5736 overlap1 = operand_type_and (i.types[1], operand_types[0]);
40fb9820
L
5737 if (!operand_type_match (overlap0, i.types[0])
5738 || !operand_type_match (overlap1, i.types[1])
45664ddb 5739 || (check_register
dc821c5f 5740 && !operand_type_register_match (i.types[0],
45664ddb 5741 operand_types[1],
45664ddb
L
5742 i.types[1],
5743 operand_types[0])))
29b0f896
AM
5744 {
5745 /* Does not match either direction. */
5746 continue;
5747 }
38e314eb 5748 /* found_reverse_match holds which of D or FloatR
29b0f896 5749 we've found. */
38e314eb
JB
5750 if (!t->opcode_modifier.d)
5751 found_reverse_match = 0;
5752 else if (operand_types[0].bitfield.tbyte)
8a2ed489
L
5753 found_reverse_match = Opcode_FloatD;
5754 else
38e314eb 5755 found_reverse_match = Opcode_D;
40fb9820 5756 if (t->opcode_modifier.floatr)
8a2ed489 5757 found_reverse_match |= Opcode_FloatR;
29b0f896 5758 }
f48ff2ae 5759 else
29b0f896 5760 {
f48ff2ae 5761 /* Found a forward 2 operand match here. */
d1cbb4db
L
5762 switch (t->operands)
5763 {
c0f3af97
L
5764 case 5:
5765 overlap4 = operand_type_and (i.types[4],
5766 operand_types[4]);
1a0670f3 5767 /* Fall through. */
d1cbb4db 5768 case 4:
c6fb90c8
L
5769 overlap3 = operand_type_and (i.types[3],
5770 operand_types[3]);
1a0670f3 5771 /* Fall through. */
d1cbb4db 5772 case 3:
c6fb90c8
L
5773 overlap2 = operand_type_and (i.types[2],
5774 operand_types[2]);
d1cbb4db
L
5775 break;
5776 }
29b0f896 5777
f48ff2ae
L
5778 switch (t->operands)
5779 {
c0f3af97
L
5780 case 5:
5781 if (!operand_type_match (overlap4, i.types[4])
dc821c5f 5782 || !operand_type_register_match (i.types[3],
c0f3af97 5783 operand_types[3],
c0f3af97
L
5784 i.types[4],
5785 operand_types[4]))
5786 continue;
1a0670f3 5787 /* Fall through. */
f48ff2ae 5788 case 4:
40fb9820 5789 if (!operand_type_match (overlap3, i.types[3])
e2195274
JB
5790 || ((check_register & 0xa) == 0xa
5791 && !operand_type_register_match (i.types[1],
f7768225
JB
5792 operand_types[1],
5793 i.types[3],
e2195274
JB
5794 operand_types[3]))
5795 || ((check_register & 0xc) == 0xc
5796 && !operand_type_register_match (i.types[2],
5797 operand_types[2],
5798 i.types[3],
5799 operand_types[3])))
f48ff2ae 5800 continue;
1a0670f3 5801 /* Fall through. */
f48ff2ae
L
5802 case 3:
5803 /* Here we make use of the fact that there are no
23e42951 5804 reverse match 3 operand instructions. */
40fb9820 5805 if (!operand_type_match (overlap2, i.types[2])
e2195274
JB
5806 || ((check_register & 5) == 5
5807 && !operand_type_register_match (i.types[0],
23e42951
JB
5808 operand_types[0],
5809 i.types[2],
e2195274
JB
5810 operand_types[2]))
5811 || ((check_register & 6) == 6
5812 && !operand_type_register_match (i.types[1],
5813 operand_types[1],
5814 i.types[2],
5815 operand_types[2])))
f48ff2ae
L
5816 continue;
5817 break;
5818 }
29b0f896 5819 }
f48ff2ae 5820 /* Found either forward/reverse 2, 3 or 4 operand match here:
29b0f896
AM
5821 slip through to break. */
5822 }
3629bb00 5823 if (!found_cpu_match)
29b0f896
AM
5824 {
5825 found_reverse_match = 0;
5826 continue;
5827 }
c0f3af97 5828
5614d22c
JB
5829 /* Check if vector and VEX operands are valid. */
5830 if (check_VecOperands (t) || VEX_check_operands (t))
5831 {
5832 specific_error = i.error;
5833 continue;
5834 }
a683cc34 5835
29b0f896
AM
5836 /* We've found a match; break out of loop. */
5837 break;
5838 }
5839
5840 if (t == current_templates->end)
5841 {
5842 /* We found no match. */
a65babc9 5843 const char *err_msg;
5614d22c 5844 switch (specific_error ? specific_error : i.error)
a65babc9
L
5845 {
5846 default:
5847 abort ();
86e026a4 5848 case operand_size_mismatch:
a65babc9
L
5849 err_msg = _("operand size mismatch");
5850 break;
5851 case operand_type_mismatch:
5852 err_msg = _("operand type mismatch");
5853 break;
5854 case register_type_mismatch:
5855 err_msg = _("register type mismatch");
5856 break;
5857 case number_of_operands_mismatch:
5858 err_msg = _("number of operands mismatch");
5859 break;
5860 case invalid_instruction_suffix:
5861 err_msg = _("invalid instruction suffix");
5862 break;
5863 case bad_imm4:
4a2608e3 5864 err_msg = _("constant doesn't fit in 4 bits");
a65babc9 5865 break;
a65babc9
L
5866 case unsupported_with_intel_mnemonic:
5867 err_msg = _("unsupported with Intel mnemonic");
5868 break;
5869 case unsupported_syntax:
5870 err_msg = _("unsupported syntax");
5871 break;
5872 case unsupported:
35262a23 5873 as_bad (_("unsupported instruction `%s'"),
10efe3f6
L
5874 current_templates->start->name);
5875 return NULL;
6c30d220
L
5876 case invalid_vsib_address:
5877 err_msg = _("invalid VSIB address");
5878 break;
7bab8ab5
JB
5879 case invalid_vector_register_set:
5880 err_msg = _("mask, index, and destination registers must be distinct");
5881 break;
6c30d220
L
5882 case unsupported_vector_index_register:
5883 err_msg = _("unsupported vector index register");
5884 break;
43234a1e
L
5885 case unsupported_broadcast:
5886 err_msg = _("unsupported broadcast");
5887 break;
43234a1e
L
5888 case broadcast_needed:
5889 err_msg = _("broadcast is needed for operand of such type");
5890 break;
5891 case unsupported_masking:
5892 err_msg = _("unsupported masking");
5893 break;
5894 case mask_not_on_destination:
5895 err_msg = _("mask not on destination operand");
5896 break;
5897 case no_default_mask:
5898 err_msg = _("default mask isn't allowed");
5899 break;
5900 case unsupported_rc_sae:
5901 err_msg = _("unsupported static rounding/sae");
5902 break;
5903 case rc_sae_operand_not_last_imm:
5904 if (intel_syntax)
5905 err_msg = _("RC/SAE operand must precede immediate operands");
5906 else
5907 err_msg = _("RC/SAE operand must follow immediate operands");
5908 break;
5909 case invalid_register_operand:
5910 err_msg = _("invalid register operand");
5911 break;
a65babc9
L
5912 }
5913 as_bad (_("%s for `%s'"), err_msg,
891edac4 5914 current_templates->start->name);
fa99fab2 5915 return NULL;
29b0f896 5916 }
252b5132 5917
29b0f896
AM
5918 if (!quiet_warnings)
5919 {
5920 if (!intel_syntax
40fb9820
L
5921 && (i.types[0].bitfield.jumpabsolute
5922 != operand_types[0].bitfield.jumpabsolute))
29b0f896
AM
5923 {
5924 as_warn (_("indirect %s without `*'"), t->name);
5925 }
5926
40fb9820
L
5927 if (t->opcode_modifier.isprefix
5928 && t->opcode_modifier.ignoresize)
29b0f896
AM
5929 {
5930 /* Warn them that a data or address size prefix doesn't
5931 affect assembly of the next line of code. */
5932 as_warn (_("stand-alone `%s' prefix"), t->name);
5933 }
5934 }
5935
5936 /* Copy the template we found. */
5937 i.tm = *t;
539e75ad
L
5938
5939 if (addr_prefix_disp != -1)
5940 i.tm.operand_types[addr_prefix_disp]
5941 = operand_types[addr_prefix_disp];
5942
29b0f896
AM
5943 if (found_reverse_match)
5944 {
5945 /* If we found a reverse match we must alter the opcode
5946 direction bit. found_reverse_match holds bits to change
5947 (different for int & float insns). */
5948
5949 i.tm.base_opcode ^= found_reverse_match;
5950
539e75ad
L
5951 i.tm.operand_types[0] = operand_types[1];
5952 i.tm.operand_types[1] = operand_types[0];
29b0f896
AM
5953 }
5954
fa99fab2 5955 return t;
29b0f896
AM
5956}
5957
5958static int
e3bb37b5 5959check_string (void)
29b0f896 5960{
40fb9820
L
5961 int mem_op = operand_type_check (i.types[0], anymem) ? 0 : 1;
5962 if (i.tm.operand_types[mem_op].bitfield.esseg)
29b0f896
AM
5963 {
5964 if (i.seg[0] != NULL && i.seg[0] != &es)
5965 {
a87af027 5966 as_bad (_("`%s' operand %d must use `%ses' segment"),
29b0f896 5967 i.tm.name,
a87af027
JB
5968 mem_op + 1,
5969 register_prefix);
29b0f896
AM
5970 return 0;
5971 }
5972 /* There's only ever one segment override allowed per instruction.
5973 This instruction possibly has a legal segment override on the
5974 second operand, so copy the segment to where non-string
5975 instructions store it, allowing common code. */
5976 i.seg[0] = i.seg[1];
5977 }
40fb9820 5978 else if (i.tm.operand_types[mem_op + 1].bitfield.esseg)
29b0f896
AM
5979 {
5980 if (i.seg[1] != NULL && i.seg[1] != &es)
5981 {
a87af027 5982 as_bad (_("`%s' operand %d must use `%ses' segment"),
29b0f896 5983 i.tm.name,
a87af027
JB
5984 mem_op + 2,
5985 register_prefix);
29b0f896
AM
5986 return 0;
5987 }
5988 }
5989 return 1;
5990}
5991
5992static int
543613e9 5993process_suffix (void)
29b0f896
AM
5994{
5995 /* If matched instruction specifies an explicit instruction mnemonic
5996 suffix, use it. */
40fb9820
L
5997 if (i.tm.opcode_modifier.size16)
5998 i.suffix = WORD_MNEM_SUFFIX;
5999 else if (i.tm.opcode_modifier.size32)
6000 i.suffix = LONG_MNEM_SUFFIX;
6001 else if (i.tm.opcode_modifier.size64)
6002 i.suffix = QWORD_MNEM_SUFFIX;
29b0f896
AM
6003 else if (i.reg_operands)
6004 {
6005 /* If there's no instruction mnemonic suffix we try to invent one
6006 based on register operands. */
6007 if (!i.suffix)
6008 {
6009 /* We take i.suffix from the last register operand specified,
6010 Destination register type is more significant than source
381d071f
L
6011 register type. crc32 in SSE4.2 prefers source register
6012 type. */
6013 if (i.tm.base_opcode == 0xf20f38f1)
6014 {
dc821c5f 6015 if (i.types[0].bitfield.reg && i.types[0].bitfield.word)
40fb9820 6016 i.suffix = WORD_MNEM_SUFFIX;
dc821c5f 6017 else if (i.types[0].bitfield.reg && i.types[0].bitfield.dword)
40fb9820 6018 i.suffix = LONG_MNEM_SUFFIX;
dc821c5f 6019 else if (i.types[0].bitfield.reg && i.types[0].bitfield.qword)
40fb9820 6020 i.suffix = QWORD_MNEM_SUFFIX;
381d071f 6021 }
9344ff29 6022 else if (i.tm.base_opcode == 0xf20f38f0)
20592a94 6023 {
dc821c5f 6024 if (i.types[0].bitfield.reg && i.types[0].bitfield.byte)
20592a94
L
6025 i.suffix = BYTE_MNEM_SUFFIX;
6026 }
381d071f
L
6027
6028 if (!i.suffix)
6029 {
6030 int op;
6031
20592a94
L
6032 if (i.tm.base_opcode == 0xf20f38f1
6033 || i.tm.base_opcode == 0xf20f38f0)
6034 {
6035 /* We have to know the operand size for crc32. */
6036 as_bad (_("ambiguous memory operand size for `%s`"),
6037 i.tm.name);
6038 return 0;
6039 }
6040
381d071f 6041 for (op = i.operands; --op >= 0;)
b76bc5d5
JB
6042 if (!i.tm.operand_types[op].bitfield.inoutportreg
6043 && !i.tm.operand_types[op].bitfield.shiftcount)
381d071f 6044 {
8819ada6
JB
6045 if (!i.types[op].bitfield.reg)
6046 continue;
6047 if (i.types[op].bitfield.byte)
6048 i.suffix = BYTE_MNEM_SUFFIX;
6049 else if (i.types[op].bitfield.word)
6050 i.suffix = WORD_MNEM_SUFFIX;
6051 else if (i.types[op].bitfield.dword)
6052 i.suffix = LONG_MNEM_SUFFIX;
6053 else if (i.types[op].bitfield.qword)
6054 i.suffix = QWORD_MNEM_SUFFIX;
6055 else
6056 continue;
6057 break;
381d071f
L
6058 }
6059 }
29b0f896
AM
6060 }
6061 else if (i.suffix == BYTE_MNEM_SUFFIX)
6062 {
2eb952a4
L
6063 if (intel_syntax
6064 && i.tm.opcode_modifier.ignoresize
6065 && i.tm.opcode_modifier.no_bsuf)
6066 i.suffix = 0;
6067 else if (!check_byte_reg ())
29b0f896
AM
6068 return 0;
6069 }
6070 else if (i.suffix == LONG_MNEM_SUFFIX)
6071 {
2eb952a4
L
6072 if (intel_syntax
6073 && i.tm.opcode_modifier.ignoresize
9f123b91
JB
6074 && i.tm.opcode_modifier.no_lsuf
6075 && !i.tm.opcode_modifier.todword
6076 && !i.tm.opcode_modifier.toqword)
2eb952a4
L
6077 i.suffix = 0;
6078 else if (!check_long_reg ())
29b0f896
AM
6079 return 0;
6080 }
6081 else if (i.suffix == QWORD_MNEM_SUFFIX)
6082 {
955e1e6a
L
6083 if (intel_syntax
6084 && i.tm.opcode_modifier.ignoresize
9f123b91
JB
6085 && i.tm.opcode_modifier.no_qsuf
6086 && !i.tm.opcode_modifier.todword
6087 && !i.tm.opcode_modifier.toqword)
955e1e6a
L
6088 i.suffix = 0;
6089 else if (!check_qword_reg ())
29b0f896
AM
6090 return 0;
6091 }
6092 else if (i.suffix == WORD_MNEM_SUFFIX)
6093 {
2eb952a4
L
6094 if (intel_syntax
6095 && i.tm.opcode_modifier.ignoresize
6096 && i.tm.opcode_modifier.no_wsuf)
6097 i.suffix = 0;
6098 else if (!check_word_reg ())
29b0f896
AM
6099 return 0;
6100 }
40fb9820 6101 else if (intel_syntax && i.tm.opcode_modifier.ignoresize)
29b0f896
AM
6102 /* Do nothing if the instruction is going to ignore the prefix. */
6103 ;
6104 else
6105 abort ();
6106 }
40fb9820 6107 else if (i.tm.opcode_modifier.defaultsize
9306ca4a
JB
6108 && !i.suffix
6109 /* exclude fldenv/frstor/fsave/fstenv */
40fb9820 6110 && i.tm.opcode_modifier.no_ssuf)
29b0f896
AM
6111 {
6112 i.suffix = stackop_size;
6113 }
9306ca4a
JB
6114 else if (intel_syntax
6115 && !i.suffix
40fb9820
L
6116 && (i.tm.operand_types[0].bitfield.jumpabsolute
6117 || i.tm.opcode_modifier.jumpbyte
6118 || i.tm.opcode_modifier.jumpintersegment
64e74474
AM
6119 || (i.tm.base_opcode == 0x0f01 /* [ls][gi]dt */
6120 && i.tm.extension_opcode <= 3)))
9306ca4a
JB
6121 {
6122 switch (flag_code)
6123 {
6124 case CODE_64BIT:
40fb9820 6125 if (!i.tm.opcode_modifier.no_qsuf)
9306ca4a
JB
6126 {
6127 i.suffix = QWORD_MNEM_SUFFIX;
6128 break;
6129 }
1a0670f3 6130 /* Fall through. */
9306ca4a 6131 case CODE_32BIT:
40fb9820 6132 if (!i.tm.opcode_modifier.no_lsuf)
9306ca4a
JB
6133 i.suffix = LONG_MNEM_SUFFIX;
6134 break;
6135 case CODE_16BIT:
40fb9820 6136 if (!i.tm.opcode_modifier.no_wsuf)
9306ca4a
JB
6137 i.suffix = WORD_MNEM_SUFFIX;
6138 break;
6139 }
6140 }
252b5132 6141
9306ca4a 6142 if (!i.suffix)
29b0f896 6143 {
9306ca4a
JB
6144 if (!intel_syntax)
6145 {
40fb9820 6146 if (i.tm.opcode_modifier.w)
9306ca4a 6147 {
4eed87de
AM
6148 as_bad (_("no instruction mnemonic suffix given and "
6149 "no register operands; can't size instruction"));
9306ca4a
JB
6150 return 0;
6151 }
6152 }
6153 else
6154 {
40fb9820 6155 unsigned int suffixes;
7ab9ffdd 6156
40fb9820
L
6157 suffixes = !i.tm.opcode_modifier.no_bsuf;
6158 if (!i.tm.opcode_modifier.no_wsuf)
6159 suffixes |= 1 << 1;
6160 if (!i.tm.opcode_modifier.no_lsuf)
6161 suffixes |= 1 << 2;
fc4adea1 6162 if (!i.tm.opcode_modifier.no_ldsuf)
40fb9820
L
6163 suffixes |= 1 << 3;
6164 if (!i.tm.opcode_modifier.no_ssuf)
6165 suffixes |= 1 << 4;
c2b9da16 6166 if (flag_code == CODE_64BIT && !i.tm.opcode_modifier.no_qsuf)
40fb9820
L
6167 suffixes |= 1 << 5;
6168
6169 /* There are more than suffix matches. */
6170 if (i.tm.opcode_modifier.w
9306ca4a 6171 || ((suffixes & (suffixes - 1))
40fb9820
L
6172 && !i.tm.opcode_modifier.defaultsize
6173 && !i.tm.opcode_modifier.ignoresize))
9306ca4a
JB
6174 {
6175 as_bad (_("ambiguous operand size for `%s'"), i.tm.name);
6176 return 0;
6177 }
6178 }
29b0f896 6179 }
252b5132 6180
d2224064
JB
6181 /* Change the opcode based on the operand size given by i.suffix. */
6182 switch (i.suffix)
29b0f896 6183 {
d2224064
JB
6184 /* Size floating point instruction. */
6185 case LONG_MNEM_SUFFIX:
6186 if (i.tm.opcode_modifier.floatmf)
6187 {
6188 i.tm.base_opcode ^= 4;
6189 break;
6190 }
6191 /* fall through */
6192 case WORD_MNEM_SUFFIX:
6193 case QWORD_MNEM_SUFFIX:
29b0f896 6194 /* It's not a byte, select word/dword operation. */
40fb9820 6195 if (i.tm.opcode_modifier.w)
29b0f896 6196 {
40fb9820 6197 if (i.tm.opcode_modifier.shortform)
29b0f896
AM
6198 i.tm.base_opcode |= 8;
6199 else
6200 i.tm.base_opcode |= 1;
6201 }
d2224064
JB
6202 /* fall through */
6203 case SHORT_MNEM_SUFFIX:
29b0f896
AM
6204 /* Now select between word & dword operations via the operand
6205 size prefix, except for instructions that will ignore this
6206 prefix anyway. */
75c0a438
L
6207 if (i.reg_operands > 0
6208 && i.types[0].bitfield.reg
6209 && i.tm.opcode_modifier.addrprefixopreg
6210 && (i.tm.opcode_modifier.immext
6211 || i.operands == 1))
cb712a9e 6212 {
ca61edf2
L
6213 /* The address size override prefix changes the size of the
6214 first operand. */
40fb9820 6215 if ((flag_code == CODE_32BIT
75c0a438 6216 && i.op[0].regs->reg_type.bitfield.word)
40fb9820 6217 || (flag_code != CODE_32BIT
75c0a438 6218 && i.op[0].regs->reg_type.bitfield.dword))
cb712a9e
L
6219 if (!add_prefix (ADDR_PREFIX_OPCODE))
6220 return 0;
6221 }
6222 else if (i.suffix != QWORD_MNEM_SUFFIX
40fb9820
L
6223 && !i.tm.opcode_modifier.ignoresize
6224 && !i.tm.opcode_modifier.floatmf
7a8655d2
JB
6225 && !i.tm.opcode_modifier.vex
6226 && !i.tm.opcode_modifier.vexopcode
6227 && !is_evex_encoding (&i.tm)
cb712a9e
L
6228 && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
6229 || (flag_code == CODE_64BIT
40fb9820 6230 && i.tm.opcode_modifier.jumpbyte)))
24eab124
AM
6231 {
6232 unsigned int prefix = DATA_PREFIX_OPCODE;
543613e9 6233
40fb9820 6234 if (i.tm.opcode_modifier.jumpbyte) /* jcxz, loop */
29b0f896 6235 prefix = ADDR_PREFIX_OPCODE;
252b5132 6236
29b0f896
AM
6237 if (!add_prefix (prefix))
6238 return 0;
24eab124 6239 }
252b5132 6240
29b0f896
AM
6241 /* Set mode64 for an operand. */
6242 if (i.suffix == QWORD_MNEM_SUFFIX
9146926a 6243 && flag_code == CODE_64BIT
d2224064 6244 && !i.tm.opcode_modifier.norex64
46e883c5 6245 /* Special case for xchg %rax,%rax. It is NOP and doesn't
d2224064
JB
6246 need rex64. */
6247 && ! (i.operands == 2
6248 && i.tm.base_opcode == 0x90
6249 && i.tm.extension_opcode == None
6250 && operand_type_equal (&i.types [0], &acc64)
6251 && operand_type_equal (&i.types [1], &acc64)))
6252 i.rex |= REX_W;
3e73aa7c 6253
d2224064 6254 break;
29b0f896 6255 }
7ecd2f8b 6256
c0a30a9f
L
6257 if (i.reg_operands != 0
6258 && i.operands > 1
6259 && i.tm.opcode_modifier.addrprefixopreg
6260 && !i.tm.opcode_modifier.immext)
6261 {
6262 /* Check invalid register operand when the address size override
6263 prefix changes the size of register operands. */
6264 unsigned int op;
6265 enum { need_word, need_dword, need_qword } need;
6266
6267 if (flag_code == CODE_32BIT)
6268 need = i.prefix[ADDR_PREFIX] ? need_word : need_dword;
6269 else
6270 {
6271 if (i.prefix[ADDR_PREFIX])
6272 need = need_dword;
6273 else
6274 need = flag_code == CODE_64BIT ? need_qword : need_word;
6275 }
6276
6277 for (op = 0; op < i.operands; op++)
6278 if (i.types[op].bitfield.reg
6279 && ((need == need_word
6280 && !i.op[op].regs->reg_type.bitfield.word)
6281 || (need == need_dword
6282 && !i.op[op].regs->reg_type.bitfield.dword)
6283 || (need == need_qword
6284 && !i.op[op].regs->reg_type.bitfield.qword)))
6285 {
6286 as_bad (_("invalid register operand size for `%s'"),
6287 i.tm.name);
6288 return 0;
6289 }
6290 }
6291
29b0f896
AM
6292 return 1;
6293}
3e73aa7c 6294
29b0f896 6295static int
543613e9 6296check_byte_reg (void)
29b0f896
AM
6297{
6298 int op;
543613e9 6299
29b0f896
AM
6300 for (op = i.operands; --op >= 0;)
6301 {
dc821c5f
JB
6302 /* Skip non-register operands. */
6303 if (!i.types[op].bitfield.reg)
6304 continue;
6305
29b0f896
AM
6306 /* If this is an eight bit register, it's OK. If it's the 16 or
6307 32 bit version of an eight bit register, we will just use the
6308 low portion, and that's OK too. */
dc821c5f 6309 if (i.types[op].bitfield.byte)
29b0f896
AM
6310 continue;
6311
5a819eb9
JB
6312 /* I/O port address operands are OK too. */
6313 if (i.tm.operand_types[op].bitfield.inoutportreg)
6314 continue;
6315
9344ff29
L
6316 /* crc32 doesn't generate this warning. */
6317 if (i.tm.base_opcode == 0xf20f38f0)
6318 continue;
6319
dc821c5f
JB
6320 if ((i.types[op].bitfield.word
6321 || i.types[op].bitfield.dword
6322 || i.types[op].bitfield.qword)
5a819eb9
JB
6323 && i.op[op].regs->reg_num < 4
6324 /* Prohibit these changes in 64bit mode, since the lowering
6325 would be more complicated. */
6326 && flag_code != CODE_64BIT)
29b0f896 6327 {
29b0f896 6328#if REGISTER_WARNINGS
5a819eb9 6329 if (!quiet_warnings)
a540244d
L
6330 as_warn (_("using `%s%s' instead of `%s%s' due to `%c' suffix"),
6331 register_prefix,
dc821c5f 6332 (i.op[op].regs + (i.types[op].bitfield.word
29b0f896
AM
6333 ? REGNAM_AL - REGNAM_AX
6334 : REGNAM_AL - REGNAM_EAX))->reg_name,
a540244d 6335 register_prefix,
29b0f896
AM
6336 i.op[op].regs->reg_name,
6337 i.suffix);
6338#endif
6339 continue;
6340 }
6341 /* Any other register is bad. */
dc821c5f 6342 if (i.types[op].bitfield.reg
40fb9820 6343 || i.types[op].bitfield.regmmx
1b54b8d7 6344 || i.types[op].bitfield.regsimd
40fb9820
L
6345 || i.types[op].bitfield.sreg2
6346 || i.types[op].bitfield.sreg3
6347 || i.types[op].bitfield.control
6348 || i.types[op].bitfield.debug
ca0d63fe 6349 || i.types[op].bitfield.test)
29b0f896 6350 {
a540244d
L
6351 as_bad (_("`%s%s' not allowed with `%s%c'"),
6352 register_prefix,
29b0f896
AM
6353 i.op[op].regs->reg_name,
6354 i.tm.name,
6355 i.suffix);
6356 return 0;
6357 }
6358 }
6359 return 1;
6360}
6361
6362static int
e3bb37b5 6363check_long_reg (void)
29b0f896
AM
6364{
6365 int op;
6366
6367 for (op = i.operands; --op >= 0;)
dc821c5f
JB
6368 /* Skip non-register operands. */
6369 if (!i.types[op].bitfield.reg)
6370 continue;
29b0f896
AM
6371 /* Reject eight bit registers, except where the template requires
6372 them. (eg. movzb) */
dc821c5f
JB
6373 else if (i.types[op].bitfield.byte
6374 && (i.tm.operand_types[op].bitfield.reg
6375 || i.tm.operand_types[op].bitfield.acc)
6376 && (i.tm.operand_types[op].bitfield.word
6377 || i.tm.operand_types[op].bitfield.dword))
29b0f896 6378 {
a540244d
L
6379 as_bad (_("`%s%s' not allowed with `%s%c'"),
6380 register_prefix,
29b0f896
AM
6381 i.op[op].regs->reg_name,
6382 i.tm.name,
6383 i.suffix);
6384 return 0;
6385 }
e4630f71 6386 /* Warn if the e prefix on a general reg is missing. */
29b0f896 6387 else if ((!quiet_warnings || flag_code == CODE_64BIT)
dc821c5f
JB
6388 && i.types[op].bitfield.word
6389 && (i.tm.operand_types[op].bitfield.reg
6390 || i.tm.operand_types[op].bitfield.acc)
6391 && i.tm.operand_types[op].bitfield.dword)
29b0f896
AM
6392 {
6393 /* Prohibit these changes in the 64bit mode, since the
6394 lowering is more complicated. */
6395 if (flag_code == CODE_64BIT)
252b5132 6396 {
2b5d6a91 6397 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
2ca3ace5 6398 register_prefix, i.op[op].regs->reg_name,
29b0f896
AM
6399 i.suffix);
6400 return 0;
252b5132 6401 }
29b0f896 6402#if REGISTER_WARNINGS
cecf1424
JB
6403 as_warn (_("using `%s%s' instead of `%s%s' due to `%c' suffix"),
6404 register_prefix,
6405 (i.op[op].regs + REGNAM_EAX - REGNAM_AX)->reg_name,
6406 register_prefix, i.op[op].regs->reg_name, i.suffix);
29b0f896 6407#endif
252b5132 6408 }
e4630f71 6409 /* Warn if the r prefix on a general reg is present. */
dc821c5f
JB
6410 else if (i.types[op].bitfield.qword
6411 && (i.tm.operand_types[op].bitfield.reg
6412 || i.tm.operand_types[op].bitfield.acc)
6413 && i.tm.operand_types[op].bitfield.dword)
252b5132 6414 {
34828aad 6415 if (intel_syntax
ca61edf2 6416 && i.tm.opcode_modifier.toqword
1b54b8d7 6417 && !i.types[0].bitfield.regsimd)
34828aad 6418 {
ca61edf2 6419 /* Convert to QWORD. We want REX byte. */
34828aad
L
6420 i.suffix = QWORD_MNEM_SUFFIX;
6421 }
6422 else
6423 {
2b5d6a91 6424 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
34828aad
L
6425 register_prefix, i.op[op].regs->reg_name,
6426 i.suffix);
6427 return 0;
6428 }
29b0f896
AM
6429 }
6430 return 1;
6431}
252b5132 6432
29b0f896 6433static int
e3bb37b5 6434check_qword_reg (void)
29b0f896
AM
6435{
6436 int op;
252b5132 6437
29b0f896 6438 for (op = i.operands; --op >= 0; )
dc821c5f
JB
6439 /* Skip non-register operands. */
6440 if (!i.types[op].bitfield.reg)
6441 continue;
29b0f896
AM
6442 /* Reject eight bit registers, except where the template requires
6443 them. (eg. movzb) */
dc821c5f
JB
6444 else if (i.types[op].bitfield.byte
6445 && (i.tm.operand_types[op].bitfield.reg
6446 || i.tm.operand_types[op].bitfield.acc)
6447 && (i.tm.operand_types[op].bitfield.word
6448 || i.tm.operand_types[op].bitfield.dword))
29b0f896 6449 {
a540244d
L
6450 as_bad (_("`%s%s' not allowed with `%s%c'"),
6451 register_prefix,
29b0f896
AM
6452 i.op[op].regs->reg_name,
6453 i.tm.name,
6454 i.suffix);
6455 return 0;
6456 }
e4630f71 6457 /* Warn if the r prefix on a general reg is missing. */
dc821c5f
JB
6458 else if ((i.types[op].bitfield.word
6459 || i.types[op].bitfield.dword)
6460 && (i.tm.operand_types[op].bitfield.reg
6461 || i.tm.operand_types[op].bitfield.acc)
6462 && i.tm.operand_types[op].bitfield.qword)
29b0f896
AM
6463 {
6464 /* Prohibit these changes in the 64bit mode, since the
6465 lowering is more complicated. */
34828aad 6466 if (intel_syntax
ca61edf2 6467 && i.tm.opcode_modifier.todword
1b54b8d7 6468 && !i.types[0].bitfield.regsimd)
34828aad 6469 {
ca61edf2 6470 /* Convert to DWORD. We don't want REX byte. */
34828aad
L
6471 i.suffix = LONG_MNEM_SUFFIX;
6472 }
6473 else
6474 {
2b5d6a91 6475 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
34828aad
L
6476 register_prefix, i.op[op].regs->reg_name,
6477 i.suffix);
6478 return 0;
6479 }
252b5132 6480 }
29b0f896
AM
6481 return 1;
6482}
252b5132 6483
29b0f896 6484static int
e3bb37b5 6485check_word_reg (void)
29b0f896
AM
6486{
6487 int op;
6488 for (op = i.operands; --op >= 0;)
dc821c5f
JB
6489 /* Skip non-register operands. */
6490 if (!i.types[op].bitfield.reg)
6491 continue;
29b0f896
AM
6492 /* Reject eight bit registers, except where the template requires
6493 them. (eg. movzb) */
dc821c5f
JB
6494 else if (i.types[op].bitfield.byte
6495 && (i.tm.operand_types[op].bitfield.reg
6496 || i.tm.operand_types[op].bitfield.acc)
6497 && (i.tm.operand_types[op].bitfield.word
6498 || i.tm.operand_types[op].bitfield.dword))
29b0f896 6499 {
a540244d
L
6500 as_bad (_("`%s%s' not allowed with `%s%c'"),
6501 register_prefix,
29b0f896
AM
6502 i.op[op].regs->reg_name,
6503 i.tm.name,
6504 i.suffix);
6505 return 0;
6506 }
e4630f71 6507 /* Warn if the e or r prefix on a general reg is present. */
29b0f896 6508 else if ((!quiet_warnings || flag_code == CODE_64BIT)
dc821c5f
JB
6509 && (i.types[op].bitfield.dword
6510 || i.types[op].bitfield.qword)
6511 && (i.tm.operand_types[op].bitfield.reg
6512 || i.tm.operand_types[op].bitfield.acc)
6513 && i.tm.operand_types[op].bitfield.word)
252b5132 6514 {
29b0f896
AM
6515 /* Prohibit these changes in the 64bit mode, since the
6516 lowering is more complicated. */
6517 if (flag_code == CODE_64BIT)
252b5132 6518 {
2b5d6a91 6519 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
2ca3ace5 6520 register_prefix, i.op[op].regs->reg_name,
29b0f896
AM
6521 i.suffix);
6522 return 0;
252b5132 6523 }
29b0f896 6524#if REGISTER_WARNINGS
cecf1424
JB
6525 as_warn (_("using `%s%s' instead of `%s%s' due to `%c' suffix"),
6526 register_prefix,
6527 (i.op[op].regs + REGNAM_AX - REGNAM_EAX)->reg_name,
6528 register_prefix, i.op[op].regs->reg_name, i.suffix);
29b0f896
AM
6529#endif
6530 }
6531 return 1;
6532}
252b5132 6533
29b0f896 6534static int
40fb9820 6535update_imm (unsigned int j)
29b0f896 6536{
bc0844ae 6537 i386_operand_type overlap = i.types[j];
40fb9820
L
6538 if ((overlap.bitfield.imm8
6539 || overlap.bitfield.imm8s
6540 || overlap.bitfield.imm16
6541 || overlap.bitfield.imm32
6542 || overlap.bitfield.imm32s
6543 || overlap.bitfield.imm64)
0dfbf9d7
L
6544 && !operand_type_equal (&overlap, &imm8)
6545 && !operand_type_equal (&overlap, &imm8s)
6546 && !operand_type_equal (&overlap, &imm16)
6547 && !operand_type_equal (&overlap, &imm32)
6548 && !operand_type_equal (&overlap, &imm32s)
6549 && !operand_type_equal (&overlap, &imm64))
29b0f896
AM
6550 {
6551 if (i.suffix)
6552 {
40fb9820
L
6553 i386_operand_type temp;
6554
0dfbf9d7 6555 operand_type_set (&temp, 0);
7ab9ffdd 6556 if (i.suffix == BYTE_MNEM_SUFFIX)
40fb9820
L
6557 {
6558 temp.bitfield.imm8 = overlap.bitfield.imm8;
6559 temp.bitfield.imm8s = overlap.bitfield.imm8s;
6560 }
6561 else if (i.suffix == WORD_MNEM_SUFFIX)
6562 temp.bitfield.imm16 = overlap.bitfield.imm16;
6563 else if (i.suffix == QWORD_MNEM_SUFFIX)
6564 {
6565 temp.bitfield.imm64 = overlap.bitfield.imm64;
6566 temp.bitfield.imm32s = overlap.bitfield.imm32s;
6567 }
6568 else
6569 temp.bitfield.imm32 = overlap.bitfield.imm32;
6570 overlap = temp;
29b0f896 6571 }
0dfbf9d7
L
6572 else if (operand_type_equal (&overlap, &imm16_32_32s)
6573 || operand_type_equal (&overlap, &imm16_32)
6574 || operand_type_equal (&overlap, &imm16_32s))
29b0f896 6575 {
40fb9820 6576 if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
65da13b5 6577 overlap = imm16;
40fb9820 6578 else
65da13b5 6579 overlap = imm32s;
29b0f896 6580 }
0dfbf9d7
L
6581 if (!operand_type_equal (&overlap, &imm8)
6582 && !operand_type_equal (&overlap, &imm8s)
6583 && !operand_type_equal (&overlap, &imm16)
6584 && !operand_type_equal (&overlap, &imm32)
6585 && !operand_type_equal (&overlap, &imm32s)
6586 && !operand_type_equal (&overlap, &imm64))
29b0f896 6587 {
4eed87de
AM
6588 as_bad (_("no instruction mnemonic suffix given; "
6589 "can't determine immediate size"));
29b0f896
AM
6590 return 0;
6591 }
6592 }
40fb9820 6593 i.types[j] = overlap;
29b0f896 6594
40fb9820
L
6595 return 1;
6596}
6597
6598static int
6599finalize_imm (void)
6600{
bc0844ae 6601 unsigned int j, n;
29b0f896 6602
bc0844ae
L
6603 /* Update the first 2 immediate operands. */
6604 n = i.operands > 2 ? 2 : i.operands;
6605 if (n)
6606 {
6607 for (j = 0; j < n; j++)
6608 if (update_imm (j) == 0)
6609 return 0;
40fb9820 6610
bc0844ae
L
6611 /* The 3rd operand can't be immediate operand. */
6612 gas_assert (operand_type_check (i.types[2], imm) == 0);
6613 }
29b0f896
AM
6614
6615 return 1;
6616}
6617
6618static int
e3bb37b5 6619process_operands (void)
29b0f896
AM
6620{
6621 /* Default segment register this instruction will use for memory
6622 accesses. 0 means unknown. This is only for optimizing out
6623 unnecessary segment overrides. */
6624 const seg_entry *default_seg = 0;
6625
2426c15f 6626 if (i.tm.opcode_modifier.sse2avx && i.tm.opcode_modifier.vexvvvv)
29b0f896 6627 {
91d6fa6a
NC
6628 unsigned int dupl = i.operands;
6629 unsigned int dest = dupl - 1;
9fcfb3d7
L
6630 unsigned int j;
6631
c0f3af97 6632 /* The destination must be an xmm register. */
9c2799c2 6633 gas_assert (i.reg_operands
91d6fa6a 6634 && MAX_OPERANDS > dupl
7ab9ffdd 6635 && operand_type_equal (&i.types[dest], &regxmm));
c0f3af97 6636
1b54b8d7
JB
6637 if (i.tm.operand_types[0].bitfield.acc
6638 && i.tm.operand_types[0].bitfield.xmmword)
e2ec9d29 6639 {
8cd7925b 6640 if (i.tm.opcode_modifier.vexsources == VEX3SOURCES)
c0f3af97
L
6641 {
6642 /* Keep xmm0 for instructions with VEX prefix and 3
6643 sources. */
1b54b8d7
JB
6644 i.tm.operand_types[0].bitfield.acc = 0;
6645 i.tm.operand_types[0].bitfield.regsimd = 1;
c0f3af97
L
6646 goto duplicate;
6647 }
e2ec9d29 6648 else
c0f3af97
L
6649 {
6650 /* We remove the first xmm0 and keep the number of
6651 operands unchanged, which in fact duplicates the
6652 destination. */
6653 for (j = 1; j < i.operands; j++)
6654 {
6655 i.op[j - 1] = i.op[j];
6656 i.types[j - 1] = i.types[j];
6657 i.tm.operand_types[j - 1] = i.tm.operand_types[j];
6658 }
6659 }
6660 }
6661 else if (i.tm.opcode_modifier.implicit1stxmm0)
7ab9ffdd 6662 {
91d6fa6a 6663 gas_assert ((MAX_OPERANDS - 1) > dupl
8cd7925b
L
6664 && (i.tm.opcode_modifier.vexsources
6665 == VEX3SOURCES));
c0f3af97
L
6666
6667 /* Add the implicit xmm0 for instructions with VEX prefix
6668 and 3 sources. */
6669 for (j = i.operands; j > 0; j--)
6670 {
6671 i.op[j] = i.op[j - 1];
6672 i.types[j] = i.types[j - 1];
6673 i.tm.operand_types[j] = i.tm.operand_types[j - 1];
6674 }
6675 i.op[0].regs
6676 = (const reg_entry *) hash_find (reg_hash, "xmm0");
7ab9ffdd 6677 i.types[0] = regxmm;
c0f3af97
L
6678 i.tm.operand_types[0] = regxmm;
6679
6680 i.operands += 2;
6681 i.reg_operands += 2;
6682 i.tm.operands += 2;
6683
91d6fa6a 6684 dupl++;
c0f3af97 6685 dest++;
91d6fa6a
NC
6686 i.op[dupl] = i.op[dest];
6687 i.types[dupl] = i.types[dest];
6688 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
e2ec9d29 6689 }
c0f3af97
L
6690 else
6691 {
6692duplicate:
6693 i.operands++;
6694 i.reg_operands++;
6695 i.tm.operands++;
6696
91d6fa6a
NC
6697 i.op[dupl] = i.op[dest];
6698 i.types[dupl] = i.types[dest];
6699 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
c0f3af97
L
6700 }
6701
6702 if (i.tm.opcode_modifier.immext)
6703 process_immext ();
6704 }
1b54b8d7
JB
6705 else if (i.tm.operand_types[0].bitfield.acc
6706 && i.tm.operand_types[0].bitfield.xmmword)
c0f3af97
L
6707 {
6708 unsigned int j;
6709
9fcfb3d7
L
6710 for (j = 1; j < i.operands; j++)
6711 {
6712 i.op[j - 1] = i.op[j];
6713 i.types[j - 1] = i.types[j];
6714
6715 /* We need to adjust fields in i.tm since they are used by
6716 build_modrm_byte. */
6717 i.tm.operand_types [j - 1] = i.tm.operand_types [j];
6718 }
6719
e2ec9d29
L
6720 i.operands--;
6721 i.reg_operands--;
e2ec9d29
L
6722 i.tm.operands--;
6723 }
920d2ddc
IT
6724 else if (i.tm.opcode_modifier.implicitquadgroup)
6725 {
a477a8c4
JB
6726 unsigned int regnum, first_reg_in_group, last_reg_in_group;
6727
920d2ddc 6728 /* The second operand must be {x,y,z}mmN, where N is a multiple of 4. */
10c17abd 6729 gas_assert (i.operands >= 2 && i.types[1].bitfield.regsimd);
a477a8c4
JB
6730 regnum = register_number (i.op[1].regs);
6731 first_reg_in_group = regnum & ~3;
6732 last_reg_in_group = first_reg_in_group + 3;
6733 if (regnum != first_reg_in_group)
6734 as_warn (_("source register `%s%s' implicitly denotes"
6735 " `%s%.3s%u' to `%s%.3s%u' source group in `%s'"),
6736 register_prefix, i.op[1].regs->reg_name,
6737 register_prefix, i.op[1].regs->reg_name, first_reg_in_group,
6738 register_prefix, i.op[1].regs->reg_name, last_reg_in_group,
6739 i.tm.name);
6740 }
e2ec9d29
L
6741 else if (i.tm.opcode_modifier.regkludge)
6742 {
6743 /* The imul $imm, %reg instruction is converted into
6744 imul $imm, %reg, %reg, and the clr %reg instruction
6745 is converted into xor %reg, %reg. */
6746
6747 unsigned int first_reg_op;
6748
6749 if (operand_type_check (i.types[0], reg))
6750 first_reg_op = 0;
6751 else
6752 first_reg_op = 1;
6753 /* Pretend we saw the extra register operand. */
9c2799c2 6754 gas_assert (i.reg_operands == 1
7ab9ffdd 6755 && i.op[first_reg_op + 1].regs == 0);
e2ec9d29
L
6756 i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
6757 i.types[first_reg_op + 1] = i.types[first_reg_op];
6758 i.operands++;
6759 i.reg_operands++;
29b0f896
AM
6760 }
6761
40fb9820 6762 if (i.tm.opcode_modifier.shortform)
29b0f896 6763 {
40fb9820
L
6764 if (i.types[0].bitfield.sreg2
6765 || i.types[0].bitfield.sreg3)
29b0f896 6766 {
4eed87de
AM
6767 if (i.tm.base_opcode == POP_SEG_SHORT
6768 && i.op[0].regs->reg_num == 1)
29b0f896 6769 {
a87af027 6770 as_bad (_("you can't `pop %scs'"), register_prefix);
4eed87de 6771 return 0;
29b0f896 6772 }
4eed87de
AM
6773 i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
6774 if ((i.op[0].regs->reg_flags & RegRex) != 0)
161a04f6 6775 i.rex |= REX_B;
4eed87de
AM
6776 }
6777 else
6778 {
7ab9ffdd 6779 /* The register or float register operand is in operand
85f10a01 6780 0 or 1. */
40fb9820 6781 unsigned int op;
7ab9ffdd 6782
ca0d63fe 6783 if ((i.types[0].bitfield.reg && i.types[0].bitfield.tbyte)
7ab9ffdd
L
6784 || operand_type_check (i.types[0], reg))
6785 op = 0;
6786 else
6787 op = 1;
4eed87de
AM
6788 /* Register goes in low 3 bits of opcode. */
6789 i.tm.base_opcode |= i.op[op].regs->reg_num;
6790 if ((i.op[op].regs->reg_flags & RegRex) != 0)
161a04f6 6791 i.rex |= REX_B;
40fb9820 6792 if (!quiet_warnings && i.tm.opcode_modifier.ugh)
29b0f896 6793 {
4eed87de
AM
6794 /* Warn about some common errors, but press on regardless.
6795 The first case can be generated by gcc (<= 2.8.1). */
6796 if (i.operands == 2)
6797 {
6798 /* Reversed arguments on faddp, fsubp, etc. */
a540244d 6799 as_warn (_("translating to `%s %s%s,%s%s'"), i.tm.name,
d8a1b51e
JB
6800 register_prefix, i.op[!intel_syntax].regs->reg_name,
6801 register_prefix, i.op[intel_syntax].regs->reg_name);
4eed87de
AM
6802 }
6803 else
6804 {
6805 /* Extraneous `l' suffix on fp insn. */
a540244d
L
6806 as_warn (_("translating to `%s %s%s'"), i.tm.name,
6807 register_prefix, i.op[0].regs->reg_name);
4eed87de 6808 }
29b0f896
AM
6809 }
6810 }
6811 }
40fb9820 6812 else if (i.tm.opcode_modifier.modrm)
29b0f896
AM
6813 {
6814 /* The opcode is completed (modulo i.tm.extension_opcode which
52271982
AM
6815 must be put into the modrm byte). Now, we make the modrm and
6816 index base bytes based on all the info we've collected. */
29b0f896
AM
6817
6818 default_seg = build_modrm_byte ();
6819 }
8a2ed489 6820 else if ((i.tm.base_opcode & ~0x3) == MOV_AX_DISP32)
29b0f896
AM
6821 {
6822 default_seg = &ds;
6823 }
40fb9820 6824 else if (i.tm.opcode_modifier.isstring)
29b0f896
AM
6825 {
6826 /* For the string instructions that allow a segment override
6827 on one of their operands, the default segment is ds. */
6828 default_seg = &ds;
6829 }
6830
75178d9d
L
6831 if (i.tm.base_opcode == 0x8d /* lea */
6832 && i.seg[0]
6833 && !quiet_warnings)
30123838 6834 as_warn (_("segment override on `%s' is ineffectual"), i.tm.name);
52271982
AM
6835
6836 /* If a segment was explicitly specified, and the specified segment
6837 is not the default, use an opcode prefix to select it. If we
6838 never figured out what the default segment is, then default_seg
6839 will be zero at this point, and the specified segment prefix will
6840 always be used. */
29b0f896
AM
6841 if ((i.seg[0]) && (i.seg[0] != default_seg))
6842 {
6843 if (!add_prefix (i.seg[0]->seg_prefix))
6844 return 0;
6845 }
6846 return 1;
6847}
6848
6849static const seg_entry *
e3bb37b5 6850build_modrm_byte (void)
29b0f896
AM
6851{
6852 const seg_entry *default_seg = 0;
c0f3af97 6853 unsigned int source, dest;
8cd7925b 6854 int vex_3_sources;
c0f3af97 6855
8cd7925b 6856 vex_3_sources = i.tm.opcode_modifier.vexsources == VEX3SOURCES;
c0f3af97
L
6857 if (vex_3_sources)
6858 {
91d6fa6a 6859 unsigned int nds, reg_slot;
4c2c6516 6860 expressionS *exp;
c0f3af97 6861
6b8d3588 6862 dest = i.operands - 1;
c0f3af97 6863 nds = dest - 1;
922d8de8 6864
a683cc34 6865 /* There are 2 kinds of instructions:
bed3d976
JB
6866 1. 5 operands: 4 register operands or 3 register operands
6867 plus 1 memory operand plus one Vec_Imm4 operand, VexXDS, and
6868 VexW0 or VexW1. The destination must be either XMM, YMM or
43234a1e 6869 ZMM register.
bed3d976 6870 2. 4 operands: 4 register operands or 3 register operands
2f1bada2 6871 plus 1 memory operand, with VexXDS. */
922d8de8 6872 gas_assert ((i.reg_operands == 4
bed3d976
JB
6873 || (i.reg_operands == 3 && i.mem_operands == 1))
6874 && i.tm.opcode_modifier.vexvvvv == VEXXDS
dcd7e323
JB
6875 && i.tm.opcode_modifier.vexw
6876 && i.tm.operand_types[dest].bitfield.regsimd);
a683cc34 6877
48db9223
JB
6878 /* If VexW1 is set, the first non-immediate operand is the source and
6879 the second non-immediate one is encoded in the immediate operand. */
6880 if (i.tm.opcode_modifier.vexw == VEXW1)
6881 {
6882 source = i.imm_operands;
6883 reg_slot = i.imm_operands + 1;
6884 }
6885 else
6886 {
6887 source = i.imm_operands + 1;
6888 reg_slot = i.imm_operands;
6889 }
6890
a683cc34 6891 if (i.imm_operands == 0)
bed3d976
JB
6892 {
6893 /* When there is no immediate operand, generate an 8bit
6894 immediate operand to encode the first operand. */
6895 exp = &im_expressions[i.imm_operands++];
6896 i.op[i.operands].imms = exp;
6897 i.types[i.operands] = imm8;
6898 i.operands++;
6899
6900 gas_assert (i.tm.operand_types[reg_slot].bitfield.regsimd);
6901 exp->X_op = O_constant;
6902 exp->X_add_number = register_number (i.op[reg_slot].regs) << 4;
43234a1e
L
6903 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
6904 }
922d8de8 6905 else
bed3d976
JB
6906 {
6907 unsigned int imm_slot;
a683cc34 6908
2f1bada2
JB
6909 gas_assert (i.imm_operands == 1 && i.types[0].bitfield.vec_imm4);
6910
bed3d976
JB
6911 if (i.tm.opcode_modifier.immext)
6912 {
6913 /* When ImmExt is set, the immediate byte is the last
6914 operand. */
6915 imm_slot = i.operands - 1;
6916 source--;
6917 reg_slot--;
6918 }
6919 else
6920 {
6921 imm_slot = 0;
6922
6923 /* Turn on Imm8 so that output_imm will generate it. */
6924 i.types[imm_slot].bitfield.imm8 = 1;
6925 }
6926
6927 gas_assert (i.tm.operand_types[reg_slot].bitfield.regsimd);
6928 i.op[imm_slot].imms->X_add_number
6929 |= register_number (i.op[reg_slot].regs) << 4;
43234a1e 6930 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
bed3d976 6931 }
a683cc34 6932
10c17abd 6933 gas_assert (i.tm.operand_types[nds].bitfield.regsimd);
dae39acc 6934 i.vex.register_specifier = i.op[nds].regs;
c0f3af97
L
6935 }
6936 else
6937 source = dest = 0;
29b0f896
AM
6938
6939 /* i.reg_operands MUST be the number of real register operands;
c0f3af97
L
6940 implicit registers do not count. If there are 3 register
6941 operands, it must be a instruction with VexNDS. For a
6942 instruction with VexNDD, the destination register is encoded
6943 in VEX prefix. If there are 4 register operands, it must be
6944 a instruction with VEX prefix and 3 sources. */
7ab9ffdd
L
6945 if (i.mem_operands == 0
6946 && ((i.reg_operands == 2
2426c15f 6947 && i.tm.opcode_modifier.vexvvvv <= VEXXDS)
7ab9ffdd 6948 || (i.reg_operands == 3
2426c15f 6949 && i.tm.opcode_modifier.vexvvvv == VEXXDS)
7ab9ffdd 6950 || (i.reg_operands == 4 && vex_3_sources)))
29b0f896 6951 {
cab737b9
L
6952 switch (i.operands)
6953 {
6954 case 2:
6955 source = 0;
6956 break;
6957 case 3:
c81128dc
L
6958 /* When there are 3 operands, one of them may be immediate,
6959 which may be the first or the last operand. Otherwise,
c0f3af97
L
6960 the first operand must be shift count register (cl) or it
6961 is an instruction with VexNDS. */
9c2799c2 6962 gas_assert (i.imm_operands == 1
7ab9ffdd 6963 || (i.imm_operands == 0
2426c15f 6964 && (i.tm.opcode_modifier.vexvvvv == VEXXDS
7ab9ffdd 6965 || i.types[0].bitfield.shiftcount)));
40fb9820
L
6966 if (operand_type_check (i.types[0], imm)
6967 || i.types[0].bitfield.shiftcount)
6968 source = 1;
6969 else
6970 source = 0;
cab737b9
L
6971 break;
6972 case 4:
368d64cc
L
6973 /* When there are 4 operands, the first two must be 8bit
6974 immediate operands. The source operand will be the 3rd
c0f3af97
L
6975 one.
6976
6977 For instructions with VexNDS, if the first operand
6978 an imm8, the source operand is the 2nd one. If the last
6979 operand is imm8, the source operand is the first one. */
9c2799c2 6980 gas_assert ((i.imm_operands == 2
7ab9ffdd
L
6981 && i.types[0].bitfield.imm8
6982 && i.types[1].bitfield.imm8)
2426c15f 6983 || (i.tm.opcode_modifier.vexvvvv == VEXXDS
7ab9ffdd
L
6984 && i.imm_operands == 1
6985 && (i.types[0].bitfield.imm8
43234a1e
L
6986 || i.types[i.operands - 1].bitfield.imm8
6987 || i.rounding)));
9f2670f2
L
6988 if (i.imm_operands == 2)
6989 source = 2;
6990 else
c0f3af97
L
6991 {
6992 if (i.types[0].bitfield.imm8)
6993 source = 1;
6994 else
6995 source = 0;
6996 }
c0f3af97
L
6997 break;
6998 case 5:
e771e7c9 6999 if (is_evex_encoding (&i.tm))
43234a1e
L
7000 {
7001 /* For EVEX instructions, when there are 5 operands, the
7002 first one must be immediate operand. If the second one
7003 is immediate operand, the source operand is the 3th
7004 one. If the last one is immediate operand, the source
7005 operand is the 2nd one. */
7006 gas_assert (i.imm_operands == 2
7007 && i.tm.opcode_modifier.sae
7008 && operand_type_check (i.types[0], imm));
7009 if (operand_type_check (i.types[1], imm))
7010 source = 2;
7011 else if (operand_type_check (i.types[4], imm))
7012 source = 1;
7013 else
7014 abort ();
7015 }
cab737b9
L
7016 break;
7017 default:
7018 abort ();
7019 }
7020
c0f3af97
L
7021 if (!vex_3_sources)
7022 {
7023 dest = source + 1;
7024
43234a1e
L
7025 /* RC/SAE operand could be between DEST and SRC. That happens
7026 when one operand is GPR and the other one is XMM/YMM/ZMM
7027 register. */
7028 if (i.rounding && i.rounding->operand == (int) dest)
7029 dest++;
7030
2426c15f 7031 if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
c0f3af97 7032 {
43234a1e 7033 /* For instructions with VexNDS, the register-only source
c5d0745b 7034 operand must be a 32/64bit integer, XMM, YMM, ZMM, or mask
43234a1e
L
7035 register. It is encoded in VEX prefix. We need to
7036 clear RegMem bit before calling operand_type_equal. */
f12dc422
L
7037
7038 i386_operand_type op;
7039 unsigned int vvvv;
7040
7041 /* Check register-only source operand when two source
7042 operands are swapped. */
7043 if (!i.tm.operand_types[source].bitfield.baseindex
7044 && i.tm.operand_types[dest].bitfield.baseindex)
7045 {
7046 vvvv = source;
7047 source = dest;
7048 }
7049 else
7050 vvvv = dest;
7051
7052 op = i.tm.operand_types[vvvv];
fa99fab2 7053 op.bitfield.regmem = 0;
c0f3af97 7054 if ((dest + 1) >= i.operands
dc821c5f
JB
7055 || ((!op.bitfield.reg
7056 || (!op.bitfield.dword && !op.bitfield.qword))
10c17abd 7057 && !op.bitfield.regsimd
43234a1e 7058 && !operand_type_equal (&op, &regmask)))
c0f3af97 7059 abort ();
f12dc422 7060 i.vex.register_specifier = i.op[vvvv].regs;
c0f3af97
L
7061 dest++;
7062 }
7063 }
29b0f896
AM
7064
7065 i.rm.mode = 3;
7066 /* One of the register operands will be encoded in the i.tm.reg
7067 field, the other in the combined i.tm.mode and i.tm.regmem
7068 fields. If no form of this instruction supports a memory
7069 destination operand, then we assume the source operand may
7070 sometimes be a memory operand and so we need to store the
7071 destination in the i.rm.reg field. */
40fb9820
L
7072 if (!i.tm.operand_types[dest].bitfield.regmem
7073 && operand_type_check (i.tm.operand_types[dest], anymem) == 0)
29b0f896
AM
7074 {
7075 i.rm.reg = i.op[dest].regs->reg_num;
7076 i.rm.regmem = i.op[source].regs->reg_num;
b4a3a7b4
L
7077 if (i.op[dest].regs->reg_type.bitfield.regmmx
7078 || i.op[source].regs->reg_type.bitfield.regmmx)
7079 i.has_regmmx = TRUE;
7080 else if (i.op[dest].regs->reg_type.bitfield.regsimd
7081 || i.op[source].regs->reg_type.bitfield.regsimd)
7082 {
7083 if (i.types[dest].bitfield.zmmword
7084 || i.types[source].bitfield.zmmword)
7085 i.has_regzmm = TRUE;
7086 else if (i.types[dest].bitfield.ymmword
7087 || i.types[source].bitfield.ymmword)
7088 i.has_regymm = TRUE;
7089 else
7090 i.has_regxmm = TRUE;
7091 }
29b0f896 7092 if ((i.op[dest].regs->reg_flags & RegRex) != 0)
161a04f6 7093 i.rex |= REX_R;
43234a1e
L
7094 if ((i.op[dest].regs->reg_flags & RegVRex) != 0)
7095 i.vrex |= REX_R;
29b0f896 7096 if ((i.op[source].regs->reg_flags & RegRex) != 0)
161a04f6 7097 i.rex |= REX_B;
43234a1e
L
7098 if ((i.op[source].regs->reg_flags & RegVRex) != 0)
7099 i.vrex |= REX_B;
29b0f896
AM
7100 }
7101 else
7102 {
7103 i.rm.reg = i.op[source].regs->reg_num;
7104 i.rm.regmem = i.op[dest].regs->reg_num;
7105 if ((i.op[dest].regs->reg_flags & RegRex) != 0)
161a04f6 7106 i.rex |= REX_B;
43234a1e
L
7107 if ((i.op[dest].regs->reg_flags & RegVRex) != 0)
7108 i.vrex |= REX_B;
29b0f896 7109 if ((i.op[source].regs->reg_flags & RegRex) != 0)
161a04f6 7110 i.rex |= REX_R;
43234a1e
L
7111 if ((i.op[source].regs->reg_flags & RegVRex) != 0)
7112 i.vrex |= REX_R;
29b0f896 7113 }
e0c7f900 7114 if (flag_code != CODE_64BIT && (i.rex & REX_R))
c4a530c5 7115 {
e0c7f900 7116 if (!i.types[i.tm.operand_types[0].bitfield.regmem].bitfield.control)
c4a530c5 7117 abort ();
e0c7f900 7118 i.rex &= ~REX_R;
c4a530c5
JB
7119 add_prefix (LOCK_PREFIX_OPCODE);
7120 }
29b0f896
AM
7121 }
7122 else
7123 { /* If it's not 2 reg operands... */
c0f3af97
L
7124 unsigned int mem;
7125
29b0f896
AM
7126 if (i.mem_operands)
7127 {
7128 unsigned int fake_zero_displacement = 0;
99018f42 7129 unsigned int op;
4eed87de 7130
7ab9ffdd
L
7131 for (op = 0; op < i.operands; op++)
7132 if (operand_type_check (i.types[op], anymem))
7133 break;
7ab9ffdd 7134 gas_assert (op < i.operands);
29b0f896 7135
6c30d220
L
7136 if (i.tm.opcode_modifier.vecsib)
7137 {
e968fc9b 7138 if (i.index_reg->reg_num == RegIZ)
6c30d220
L
7139 abort ();
7140
7141 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
7142 if (!i.base_reg)
7143 {
7144 i.sib.base = NO_BASE_REGISTER;
7145 i.sib.scale = i.log2_scale_factor;
7146 i.types[op].bitfield.disp8 = 0;
7147 i.types[op].bitfield.disp16 = 0;
7148 i.types[op].bitfield.disp64 = 0;
43083a50 7149 if (flag_code != CODE_64BIT || i.prefix[ADDR_PREFIX])
6c30d220
L
7150 {
7151 /* Must be 32 bit */
7152 i.types[op].bitfield.disp32 = 1;
7153 i.types[op].bitfield.disp32s = 0;
7154 }
7155 else
7156 {
7157 i.types[op].bitfield.disp32 = 0;
7158 i.types[op].bitfield.disp32s = 1;
7159 }
7160 }
7161 i.sib.index = i.index_reg->reg_num;
7162 if ((i.index_reg->reg_flags & RegRex) != 0)
7163 i.rex |= REX_X;
43234a1e
L
7164 if ((i.index_reg->reg_flags & RegVRex) != 0)
7165 i.vrex |= REX_X;
6c30d220
L
7166 }
7167
29b0f896
AM
7168 default_seg = &ds;
7169
7170 if (i.base_reg == 0)
7171 {
7172 i.rm.mode = 0;
7173 if (!i.disp_operands)
9bb129e8 7174 fake_zero_displacement = 1;
29b0f896
AM
7175 if (i.index_reg == 0)
7176 {
73053c1f
JB
7177 i386_operand_type newdisp;
7178
6c30d220 7179 gas_assert (!i.tm.opcode_modifier.vecsib);
29b0f896 7180 /* Operand is just <disp> */
20f0a1fc 7181 if (flag_code == CODE_64BIT)
29b0f896
AM
7182 {
7183 /* 64bit mode overwrites the 32bit absolute
7184 addressing by RIP relative addressing and
7185 absolute addressing is encoded by one of the
7186 redundant SIB forms. */
7187 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
7188 i.sib.base = NO_BASE_REGISTER;
7189 i.sib.index = NO_INDEX_REGISTER;
73053c1f 7190 newdisp = (!i.prefix[ADDR_PREFIX] ? disp32s : disp32);
20f0a1fc 7191 }
fc225355
L
7192 else if ((flag_code == CODE_16BIT)
7193 ^ (i.prefix[ADDR_PREFIX] != 0))
20f0a1fc
NC
7194 {
7195 i.rm.regmem = NO_BASE_REGISTER_16;
73053c1f 7196 newdisp = disp16;
20f0a1fc
NC
7197 }
7198 else
7199 {
7200 i.rm.regmem = NO_BASE_REGISTER;
73053c1f 7201 newdisp = disp32;
29b0f896 7202 }
73053c1f
JB
7203 i.types[op] = operand_type_and_not (i.types[op], anydisp);
7204 i.types[op] = operand_type_or (i.types[op], newdisp);
29b0f896 7205 }
6c30d220 7206 else if (!i.tm.opcode_modifier.vecsib)
29b0f896 7207 {
6c30d220 7208 /* !i.base_reg && i.index_reg */
e968fc9b 7209 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
7210 i.sib.index = NO_INDEX_REGISTER;
7211 else
7212 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
7213 i.sib.base = NO_BASE_REGISTER;
7214 i.sib.scale = i.log2_scale_factor;
7215 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
40fb9820
L
7216 i.types[op].bitfield.disp8 = 0;
7217 i.types[op].bitfield.disp16 = 0;
7218 i.types[op].bitfield.disp64 = 0;
43083a50 7219 if (flag_code != CODE_64BIT || i.prefix[ADDR_PREFIX])
40fb9820
L
7220 {
7221 /* Must be 32 bit */
7222 i.types[op].bitfield.disp32 = 1;
7223 i.types[op].bitfield.disp32s = 0;
7224 }
29b0f896 7225 else
40fb9820
L
7226 {
7227 i.types[op].bitfield.disp32 = 0;
7228 i.types[op].bitfield.disp32s = 1;
7229 }
29b0f896 7230 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 7231 i.rex |= REX_X;
29b0f896
AM
7232 }
7233 }
7234 /* RIP addressing for 64bit mode. */
e968fc9b 7235 else if (i.base_reg->reg_num == RegIP)
29b0f896 7236 {
6c30d220 7237 gas_assert (!i.tm.opcode_modifier.vecsib);
29b0f896 7238 i.rm.regmem = NO_BASE_REGISTER;
40fb9820
L
7239 i.types[op].bitfield.disp8 = 0;
7240 i.types[op].bitfield.disp16 = 0;
7241 i.types[op].bitfield.disp32 = 0;
7242 i.types[op].bitfield.disp32s = 1;
7243 i.types[op].bitfield.disp64 = 0;
71903a11 7244 i.flags[op] |= Operand_PCrel;
20f0a1fc
NC
7245 if (! i.disp_operands)
7246 fake_zero_displacement = 1;
29b0f896 7247 }
dc821c5f 7248 else if (i.base_reg->reg_type.bitfield.word)
29b0f896 7249 {
6c30d220 7250 gas_assert (!i.tm.opcode_modifier.vecsib);
29b0f896
AM
7251 switch (i.base_reg->reg_num)
7252 {
7253 case 3: /* (%bx) */
7254 if (i.index_reg == 0)
7255 i.rm.regmem = 7;
7256 else /* (%bx,%si) -> 0, or (%bx,%di) -> 1 */
7257 i.rm.regmem = i.index_reg->reg_num - 6;
7258 break;
7259 case 5: /* (%bp) */
7260 default_seg = &ss;
7261 if (i.index_reg == 0)
7262 {
7263 i.rm.regmem = 6;
40fb9820 7264 if (operand_type_check (i.types[op], disp) == 0)
29b0f896
AM
7265 {
7266 /* fake (%bp) into 0(%bp) */
b5014f7a 7267 i.types[op].bitfield.disp8 = 1;
252b5132 7268 fake_zero_displacement = 1;
29b0f896
AM
7269 }
7270 }
7271 else /* (%bp,%si) -> 2, or (%bp,%di) -> 3 */
7272 i.rm.regmem = i.index_reg->reg_num - 6 + 2;
7273 break;
7274 default: /* (%si) -> 4 or (%di) -> 5 */
7275 i.rm.regmem = i.base_reg->reg_num - 6 + 4;
7276 }
7277 i.rm.mode = mode_from_disp_size (i.types[op]);
7278 }
7279 else /* i.base_reg and 32/64 bit mode */
7280 {
7281 if (flag_code == CODE_64BIT
40fb9820
L
7282 && operand_type_check (i.types[op], disp))
7283 {
73053c1f
JB
7284 i.types[op].bitfield.disp16 = 0;
7285 i.types[op].bitfield.disp64 = 0;
40fb9820 7286 if (i.prefix[ADDR_PREFIX] == 0)
73053c1f
JB
7287 {
7288 i.types[op].bitfield.disp32 = 0;
7289 i.types[op].bitfield.disp32s = 1;
7290 }
40fb9820 7291 else
73053c1f
JB
7292 {
7293 i.types[op].bitfield.disp32 = 1;
7294 i.types[op].bitfield.disp32s = 0;
7295 }
40fb9820 7296 }
20f0a1fc 7297
6c30d220
L
7298 if (!i.tm.opcode_modifier.vecsib)
7299 i.rm.regmem = i.base_reg->reg_num;
29b0f896 7300 if ((i.base_reg->reg_flags & RegRex) != 0)
161a04f6 7301 i.rex |= REX_B;
29b0f896
AM
7302 i.sib.base = i.base_reg->reg_num;
7303 /* x86-64 ignores REX prefix bit here to avoid decoder
7304 complications. */
848930b2
JB
7305 if (!(i.base_reg->reg_flags & RegRex)
7306 && (i.base_reg->reg_num == EBP_REG_NUM
7307 || i.base_reg->reg_num == ESP_REG_NUM))
29b0f896 7308 default_seg = &ss;
848930b2 7309 if (i.base_reg->reg_num == 5 && i.disp_operands == 0)
29b0f896 7310 {
848930b2 7311 fake_zero_displacement = 1;
b5014f7a 7312 i.types[op].bitfield.disp8 = 1;
29b0f896
AM
7313 }
7314 i.sib.scale = i.log2_scale_factor;
7315 if (i.index_reg == 0)
7316 {
6c30d220 7317 gas_assert (!i.tm.opcode_modifier.vecsib);
29b0f896
AM
7318 /* <disp>(%esp) becomes two byte modrm with no index
7319 register. We've already stored the code for esp
7320 in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
7321 Any base register besides %esp will not use the
7322 extra modrm byte. */
7323 i.sib.index = NO_INDEX_REGISTER;
29b0f896 7324 }
6c30d220 7325 else if (!i.tm.opcode_modifier.vecsib)
29b0f896 7326 {
e968fc9b 7327 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
7328 i.sib.index = NO_INDEX_REGISTER;
7329 else
7330 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
7331 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
7332 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 7333 i.rex |= REX_X;
29b0f896 7334 }
67a4f2b7
AO
7335
7336 if (i.disp_operands
7337 && (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
7338 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL))
7339 i.rm.mode = 0;
7340 else
a501d77e
L
7341 {
7342 if (!fake_zero_displacement
7343 && !i.disp_operands
7344 && i.disp_encoding)
7345 {
7346 fake_zero_displacement = 1;
7347 if (i.disp_encoding == disp_encoding_8bit)
7348 i.types[op].bitfield.disp8 = 1;
7349 else
7350 i.types[op].bitfield.disp32 = 1;
7351 }
7352 i.rm.mode = mode_from_disp_size (i.types[op]);
7353 }
29b0f896 7354 }
252b5132 7355
29b0f896
AM
7356 if (fake_zero_displacement)
7357 {
7358 /* Fakes a zero displacement assuming that i.types[op]
7359 holds the correct displacement size. */
7360 expressionS *exp;
7361
9c2799c2 7362 gas_assert (i.op[op].disps == 0);
29b0f896
AM
7363 exp = &disp_expressions[i.disp_operands++];
7364 i.op[op].disps = exp;
7365 exp->X_op = O_constant;
7366 exp->X_add_number = 0;
7367 exp->X_add_symbol = (symbolS *) 0;
7368 exp->X_op_symbol = (symbolS *) 0;
7369 }
c0f3af97
L
7370
7371 mem = op;
29b0f896 7372 }
c0f3af97
L
7373 else
7374 mem = ~0;
252b5132 7375
8c43a48b 7376 if (i.tm.opcode_modifier.vexsources == XOP2SOURCES)
5dd85c99
SP
7377 {
7378 if (operand_type_check (i.types[0], imm))
7379 i.vex.register_specifier = NULL;
7380 else
7381 {
7382 /* VEX.vvvv encodes one of the sources when the first
7383 operand is not an immediate. */
1ef99a7b 7384 if (i.tm.opcode_modifier.vexw == VEXW0)
5dd85c99
SP
7385 i.vex.register_specifier = i.op[0].regs;
7386 else
7387 i.vex.register_specifier = i.op[1].regs;
7388 }
7389
7390 /* Destination is a XMM register encoded in the ModRM.reg
7391 and VEX.R bit. */
7392 i.rm.reg = i.op[2].regs->reg_num;
7393 if ((i.op[2].regs->reg_flags & RegRex) != 0)
7394 i.rex |= REX_R;
7395
7396 /* ModRM.rm and VEX.B encodes the other source. */
7397 if (!i.mem_operands)
7398 {
7399 i.rm.mode = 3;
7400
1ef99a7b 7401 if (i.tm.opcode_modifier.vexw == VEXW0)
5dd85c99
SP
7402 i.rm.regmem = i.op[1].regs->reg_num;
7403 else
7404 i.rm.regmem = i.op[0].regs->reg_num;
7405
7406 if ((i.op[1].regs->reg_flags & RegRex) != 0)
7407 i.rex |= REX_B;
7408 }
7409 }
2426c15f 7410 else if (i.tm.opcode_modifier.vexvvvv == VEXLWP)
f88c9eb0
SP
7411 {
7412 i.vex.register_specifier = i.op[2].regs;
7413 if (!i.mem_operands)
7414 {
7415 i.rm.mode = 3;
7416 i.rm.regmem = i.op[1].regs->reg_num;
7417 if ((i.op[1].regs->reg_flags & RegRex) != 0)
7418 i.rex |= REX_B;
7419 }
7420 }
29b0f896
AM
7421 /* Fill in i.rm.reg or i.rm.regmem field with register operand
7422 (if any) based on i.tm.extension_opcode. Again, we must be
7423 careful to make sure that segment/control/debug/test/MMX
7424 registers are coded into the i.rm.reg field. */
f88c9eb0 7425 else if (i.reg_operands)
29b0f896 7426 {
99018f42 7427 unsigned int op;
7ab9ffdd
L
7428 unsigned int vex_reg = ~0;
7429
7430 for (op = 0; op < i.operands; op++)
b4a3a7b4
L
7431 {
7432 if (i.types[op].bitfield.reg
7433 || i.types[op].bitfield.regbnd
7434 || i.types[op].bitfield.regmask
7435 || i.types[op].bitfield.sreg2
7436 || i.types[op].bitfield.sreg3
7437 || i.types[op].bitfield.control
7438 || i.types[op].bitfield.debug
7439 || i.types[op].bitfield.test)
7440 break;
7441 if (i.types[op].bitfield.regsimd)
7442 {
7443 if (i.types[op].bitfield.zmmword)
7444 i.has_regzmm = TRUE;
7445 else if (i.types[op].bitfield.ymmword)
7446 i.has_regymm = TRUE;
7447 else
7448 i.has_regxmm = TRUE;
7449 break;
7450 }
7451 if (i.types[op].bitfield.regmmx)
7452 {
7453 i.has_regmmx = TRUE;
7454 break;
7455 }
7456 }
c0209578 7457
7ab9ffdd
L
7458 if (vex_3_sources)
7459 op = dest;
2426c15f 7460 else if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
7ab9ffdd
L
7461 {
7462 /* For instructions with VexNDS, the register-only
7463 source operand is encoded in VEX prefix. */
7464 gas_assert (mem != (unsigned int) ~0);
c0f3af97 7465
7ab9ffdd 7466 if (op > mem)
c0f3af97 7467 {
7ab9ffdd
L
7468 vex_reg = op++;
7469 gas_assert (op < i.operands);
c0f3af97
L
7470 }
7471 else
c0f3af97 7472 {
f12dc422
L
7473 /* Check register-only source operand when two source
7474 operands are swapped. */
7475 if (!i.tm.operand_types[op].bitfield.baseindex
7476 && i.tm.operand_types[op + 1].bitfield.baseindex)
7477 {
7478 vex_reg = op;
7479 op += 2;
7480 gas_assert (mem == (vex_reg + 1)
7481 && op < i.operands);
7482 }
7483 else
7484 {
7485 vex_reg = op + 1;
7486 gas_assert (vex_reg < i.operands);
7487 }
c0f3af97 7488 }
7ab9ffdd 7489 }
2426c15f 7490 else if (i.tm.opcode_modifier.vexvvvv == VEXNDD)
7ab9ffdd 7491 {
f12dc422 7492 /* For instructions with VexNDD, the register destination
7ab9ffdd 7493 is encoded in VEX prefix. */
f12dc422
L
7494 if (i.mem_operands == 0)
7495 {
7496 /* There is no memory operand. */
7497 gas_assert ((op + 2) == i.operands);
7498 vex_reg = op + 1;
7499 }
7500 else
8d63c93e 7501 {
ed438a93
JB
7502 /* There are only 2 non-immediate operands. */
7503 gas_assert (op < i.imm_operands + 2
7504 && i.operands == i.imm_operands + 2);
7505 vex_reg = i.imm_operands + 1;
f12dc422 7506 }
7ab9ffdd
L
7507 }
7508 else
7509 gas_assert (op < i.operands);
99018f42 7510
7ab9ffdd
L
7511 if (vex_reg != (unsigned int) ~0)
7512 {
f12dc422 7513 i386_operand_type *type = &i.tm.operand_types[vex_reg];
7ab9ffdd 7514
dc821c5f
JB
7515 if ((!type->bitfield.reg
7516 || (!type->bitfield.dword && !type->bitfield.qword))
10c17abd 7517 && !type->bitfield.regsimd
43234a1e 7518 && !operand_type_equal (type, &regmask))
7ab9ffdd 7519 abort ();
f88c9eb0 7520
7ab9ffdd
L
7521 i.vex.register_specifier = i.op[vex_reg].regs;
7522 }
7523
1b9f0c97
L
7524 /* Don't set OP operand twice. */
7525 if (vex_reg != op)
7ab9ffdd 7526 {
1b9f0c97
L
7527 /* If there is an extension opcode to put here, the
7528 register number must be put into the regmem field. */
7529 if (i.tm.extension_opcode != None)
7530 {
7531 i.rm.regmem = i.op[op].regs->reg_num;
7532 if ((i.op[op].regs->reg_flags & RegRex) != 0)
7533 i.rex |= REX_B;
43234a1e
L
7534 if ((i.op[op].regs->reg_flags & RegVRex) != 0)
7535 i.vrex |= REX_B;
1b9f0c97
L
7536 }
7537 else
7538 {
7539 i.rm.reg = i.op[op].regs->reg_num;
7540 if ((i.op[op].regs->reg_flags & RegRex) != 0)
7541 i.rex |= REX_R;
43234a1e
L
7542 if ((i.op[op].regs->reg_flags & RegVRex) != 0)
7543 i.vrex |= REX_R;
1b9f0c97 7544 }
7ab9ffdd 7545 }
252b5132 7546
29b0f896
AM
7547 /* Now, if no memory operand has set i.rm.mode = 0, 1, 2 we
7548 must set it to 3 to indicate this is a register operand
7549 in the regmem field. */
7550 if (!i.mem_operands)
7551 i.rm.mode = 3;
7552 }
252b5132 7553
29b0f896 7554 /* Fill in i.rm.reg field with extension opcode (if any). */
c1e679ec 7555 if (i.tm.extension_opcode != None)
29b0f896
AM
7556 i.rm.reg = i.tm.extension_opcode;
7557 }
7558 return default_seg;
7559}
252b5132 7560
29b0f896 7561static void
e3bb37b5 7562output_branch (void)
29b0f896
AM
7563{
7564 char *p;
f8a5c266 7565 int size;
29b0f896
AM
7566 int code16;
7567 int prefix;
7568 relax_substateT subtype;
7569 symbolS *sym;
7570 offsetT off;
7571
f8a5c266 7572 code16 = flag_code == CODE_16BIT ? CODE16 : 0;
a501d77e 7573 size = i.disp_encoding == disp_encoding_32bit ? BIG : SMALL;
29b0f896
AM
7574
7575 prefix = 0;
7576 if (i.prefix[DATA_PREFIX] != 0)
252b5132 7577 {
29b0f896
AM
7578 prefix = 1;
7579 i.prefixes -= 1;
7580 code16 ^= CODE16;
252b5132 7581 }
29b0f896
AM
7582 /* Pentium4 branch hints. */
7583 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
7584 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
2f66722d 7585 {
29b0f896
AM
7586 prefix++;
7587 i.prefixes--;
7588 }
7589 if (i.prefix[REX_PREFIX] != 0)
7590 {
7591 prefix++;
7592 i.prefixes--;
2f66722d
AM
7593 }
7594
7e8b059b
L
7595 /* BND prefixed jump. */
7596 if (i.prefix[BND_PREFIX] != 0)
7597 {
7598 FRAG_APPEND_1_CHAR (i.prefix[BND_PREFIX]);
7599 i.prefixes -= 1;
7600 }
7601
29b0f896
AM
7602 if (i.prefixes != 0 && !intel_syntax)
7603 as_warn (_("skipping prefixes on this instruction"));
7604
7605 /* It's always a symbol; End frag & setup for relax.
7606 Make sure there is enough room in this frag for the largest
7607 instruction we may generate in md_convert_frag. This is 2
7608 bytes for the opcode and room for the prefix and largest
7609 displacement. */
7610 frag_grow (prefix + 2 + 4);
7611 /* Prefix and 1 opcode byte go in fr_fix. */
7612 p = frag_more (prefix + 1);
7613 if (i.prefix[DATA_PREFIX] != 0)
7614 *p++ = DATA_PREFIX_OPCODE;
7615 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
7616 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
7617 *p++ = i.prefix[SEG_PREFIX];
7618 if (i.prefix[REX_PREFIX] != 0)
7619 *p++ = i.prefix[REX_PREFIX];
7620 *p = i.tm.base_opcode;
7621
7622 if ((unsigned char) *p == JUMP_PC_RELATIVE)
f8a5c266 7623 subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, size);
40fb9820 7624 else if (cpu_arch_flags.bitfield.cpui386)
f8a5c266 7625 subtype = ENCODE_RELAX_STATE (COND_JUMP, size);
29b0f896 7626 else
f8a5c266 7627 subtype = ENCODE_RELAX_STATE (COND_JUMP86, size);
29b0f896 7628 subtype |= code16;
3e73aa7c 7629
29b0f896
AM
7630 sym = i.op[0].disps->X_add_symbol;
7631 off = i.op[0].disps->X_add_number;
3e73aa7c 7632
29b0f896
AM
7633 if (i.op[0].disps->X_op != O_constant
7634 && i.op[0].disps->X_op != O_symbol)
3e73aa7c 7635 {
29b0f896
AM
7636 /* Handle complex expressions. */
7637 sym = make_expr_symbol (i.op[0].disps);
7638 off = 0;
7639 }
3e73aa7c 7640
29b0f896
AM
7641 /* 1 possible extra opcode + 4 byte displacement go in var part.
7642 Pass reloc in fr_var. */
d258b828 7643 frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
29b0f896 7644}
3e73aa7c 7645
bd7ab16b
L
7646#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
7647/* Return TRUE iff PLT32 relocation should be used for branching to
7648 symbol S. */
7649
7650static bfd_boolean
7651need_plt32_p (symbolS *s)
7652{
7653 /* PLT32 relocation is ELF only. */
7654 if (!IS_ELF)
7655 return FALSE;
7656
7657 /* Since there is no need to prepare for PLT branch on x86-64, we
7658 can generate R_X86_64_PLT32, instead of R_X86_64_PC32, which can
7659 be used as a marker for 32-bit PC-relative branches. */
7660 if (!object_64bit)
7661 return FALSE;
7662
7663 /* Weak or undefined symbol need PLT32 relocation. */
7664 if (S_IS_WEAK (s) || !S_IS_DEFINED (s))
7665 return TRUE;
7666
7667 /* Non-global symbol doesn't need PLT32 relocation. */
7668 if (! S_IS_EXTERNAL (s))
7669 return FALSE;
7670
7671 /* Other global symbols need PLT32 relocation. NB: Symbol with
7672 non-default visibilities are treated as normal global symbol
7673 so that PLT32 relocation can be used as a marker for 32-bit
7674 PC-relative branches. It is useful for linker relaxation. */
7675 return TRUE;
7676}
7677#endif
7678
29b0f896 7679static void
e3bb37b5 7680output_jump (void)
29b0f896
AM
7681{
7682 char *p;
7683 int size;
3e02c1cc 7684 fixS *fixP;
bd7ab16b 7685 bfd_reloc_code_real_type jump_reloc = i.reloc[0];
29b0f896 7686
40fb9820 7687 if (i.tm.opcode_modifier.jumpbyte)
29b0f896
AM
7688 {
7689 /* This is a loop or jecxz type instruction. */
7690 size = 1;
7691 if (i.prefix[ADDR_PREFIX] != 0)
7692 {
7693 FRAG_APPEND_1_CHAR (ADDR_PREFIX_OPCODE);
7694 i.prefixes -= 1;
7695 }
7696 /* Pentium4 branch hints. */
7697 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
7698 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
7699 {
7700 FRAG_APPEND_1_CHAR (i.prefix[SEG_PREFIX]);
7701 i.prefixes--;
3e73aa7c
JH
7702 }
7703 }
29b0f896
AM
7704 else
7705 {
7706 int code16;
3e73aa7c 7707
29b0f896
AM
7708 code16 = 0;
7709 if (flag_code == CODE_16BIT)
7710 code16 = CODE16;
3e73aa7c 7711
29b0f896
AM
7712 if (i.prefix[DATA_PREFIX] != 0)
7713 {
7714 FRAG_APPEND_1_CHAR (DATA_PREFIX_OPCODE);
7715 i.prefixes -= 1;
7716 code16 ^= CODE16;
7717 }
252b5132 7718
29b0f896
AM
7719 size = 4;
7720 if (code16)
7721 size = 2;
7722 }
9fcc94b6 7723
29b0f896
AM
7724 if (i.prefix[REX_PREFIX] != 0)
7725 {
7726 FRAG_APPEND_1_CHAR (i.prefix[REX_PREFIX]);
7727 i.prefixes -= 1;
7728 }
252b5132 7729
7e8b059b
L
7730 /* BND prefixed jump. */
7731 if (i.prefix[BND_PREFIX] != 0)
7732 {
7733 FRAG_APPEND_1_CHAR (i.prefix[BND_PREFIX]);
7734 i.prefixes -= 1;
7735 }
7736
29b0f896
AM
7737 if (i.prefixes != 0 && !intel_syntax)
7738 as_warn (_("skipping prefixes on this instruction"));
e0890092 7739
42164a71
L
7740 p = frag_more (i.tm.opcode_length + size);
7741 switch (i.tm.opcode_length)
7742 {
7743 case 2:
7744 *p++ = i.tm.base_opcode >> 8;
1a0670f3 7745 /* Fall through. */
42164a71
L
7746 case 1:
7747 *p++ = i.tm.base_opcode;
7748 break;
7749 default:
7750 abort ();
7751 }
e0890092 7752
bd7ab16b
L
7753#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
7754 if (size == 4
7755 && jump_reloc == NO_RELOC
7756 && need_plt32_p (i.op[0].disps->X_add_symbol))
7757 jump_reloc = BFD_RELOC_X86_64_PLT32;
7758#endif
7759
7760 jump_reloc = reloc (size, 1, 1, jump_reloc);
7761
3e02c1cc 7762 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
bd7ab16b 7763 i.op[0].disps, 1, jump_reloc);
3e02c1cc
AM
7764
7765 /* All jumps handled here are signed, but don't use a signed limit
7766 check for 32 and 16 bit jumps as we want to allow wrap around at
7767 4G and 64k respectively. */
7768 if (size == 1)
7769 fixP->fx_signed = 1;
29b0f896 7770}
e0890092 7771
29b0f896 7772static void
e3bb37b5 7773output_interseg_jump (void)
29b0f896
AM
7774{
7775 char *p;
7776 int size;
7777 int prefix;
7778 int code16;
252b5132 7779
29b0f896
AM
7780 code16 = 0;
7781 if (flag_code == CODE_16BIT)
7782 code16 = CODE16;
a217f122 7783
29b0f896
AM
7784 prefix = 0;
7785 if (i.prefix[DATA_PREFIX] != 0)
7786 {
7787 prefix = 1;
7788 i.prefixes -= 1;
7789 code16 ^= CODE16;
7790 }
7791 if (i.prefix[REX_PREFIX] != 0)
7792 {
7793 prefix++;
7794 i.prefixes -= 1;
7795 }
252b5132 7796
29b0f896
AM
7797 size = 4;
7798 if (code16)
7799 size = 2;
252b5132 7800
29b0f896
AM
7801 if (i.prefixes != 0 && !intel_syntax)
7802 as_warn (_("skipping prefixes on this instruction"));
252b5132 7803
29b0f896
AM
7804 /* 1 opcode; 2 segment; offset */
7805 p = frag_more (prefix + 1 + 2 + size);
3e73aa7c 7806
29b0f896
AM
7807 if (i.prefix[DATA_PREFIX] != 0)
7808 *p++ = DATA_PREFIX_OPCODE;
252b5132 7809
29b0f896
AM
7810 if (i.prefix[REX_PREFIX] != 0)
7811 *p++ = i.prefix[REX_PREFIX];
252b5132 7812
29b0f896
AM
7813 *p++ = i.tm.base_opcode;
7814 if (i.op[1].imms->X_op == O_constant)
7815 {
7816 offsetT n = i.op[1].imms->X_add_number;
252b5132 7817
29b0f896
AM
7818 if (size == 2
7819 && !fits_in_unsigned_word (n)
7820 && !fits_in_signed_word (n))
7821 {
7822 as_bad (_("16-bit jump out of range"));
7823 return;
7824 }
7825 md_number_to_chars (p, n, size);
7826 }
7827 else
7828 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
d258b828 7829 i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
29b0f896
AM
7830 if (i.op[0].imms->X_op != O_constant)
7831 as_bad (_("can't handle non absolute segment in `%s'"),
7832 i.tm.name);
7833 md_number_to_chars (p + size, (valueT) i.op[0].imms->X_add_number, 2);
7834}
a217f122 7835
b4a3a7b4
L
7836#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
7837void
7838x86_cleanup (void)
7839{
7840 char *p;
7841 asection *seg = now_seg;
7842 subsegT subseg = now_subseg;
7843 asection *sec;
7844 unsigned int alignment, align_size_1;
7845 unsigned int isa_1_descsz, feature_2_descsz, descsz;
7846 unsigned int isa_1_descsz_raw, feature_2_descsz_raw;
7847 unsigned int padding;
7848
7849 if (!IS_ELF || !x86_used_note)
7850 return;
7851
7852 x86_isa_1_used |= GNU_PROPERTY_X86_UINT32_VALID;
7853 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X86;
7854
7855 /* The .note.gnu.property section layout:
7856
7857 Field Length Contents
7858 ---- ---- ----
7859 n_namsz 4 4
7860 n_descsz 4 The note descriptor size
7861 n_type 4 NT_GNU_PROPERTY_TYPE_0
7862 n_name 4 "GNU"
7863 n_desc n_descsz The program property array
7864 .... .... ....
7865 */
7866
7867 /* Create the .note.gnu.property section. */
7868 sec = subseg_new (NOTE_GNU_PROPERTY_SECTION_NAME, 0);
7869 bfd_set_section_flags (stdoutput, sec,
7870 (SEC_ALLOC
7871 | SEC_LOAD
7872 | SEC_DATA
7873 | SEC_HAS_CONTENTS
7874 | SEC_READONLY));
7875
7876 if (get_elf_backend_data (stdoutput)->s->elfclass == ELFCLASS64)
7877 {
7878 align_size_1 = 7;
7879 alignment = 3;
7880 }
7881 else
7882 {
7883 align_size_1 = 3;
7884 alignment = 2;
7885 }
7886
7887 bfd_set_section_alignment (stdoutput, sec, alignment);
7888 elf_section_type (sec) = SHT_NOTE;
7889
7890 /* GNU_PROPERTY_X86_ISA_1_USED: 4-byte type + 4-byte data size
7891 + 4-byte data */
7892 isa_1_descsz_raw = 4 + 4 + 4;
7893 /* Align GNU_PROPERTY_X86_ISA_1_USED. */
7894 isa_1_descsz = (isa_1_descsz_raw + align_size_1) & ~align_size_1;
7895
7896 feature_2_descsz_raw = isa_1_descsz;
7897 /* GNU_PROPERTY_X86_FEATURE_2_USED: 4-byte type + 4-byte data size
7898 + 4-byte data */
7899 feature_2_descsz_raw += 4 + 4 + 4;
7900 /* Align GNU_PROPERTY_X86_FEATURE_2_USED. */
7901 feature_2_descsz = ((feature_2_descsz_raw + align_size_1)
7902 & ~align_size_1);
7903
7904 descsz = feature_2_descsz;
7905 /* Section size: n_namsz + n_descsz + n_type + n_name + n_descsz. */
7906 p = frag_more (4 + 4 + 4 + 4 + descsz);
7907
7908 /* Write n_namsz. */
7909 md_number_to_chars (p, (valueT) 4, 4);
7910
7911 /* Write n_descsz. */
7912 md_number_to_chars (p + 4, (valueT) descsz, 4);
7913
7914 /* Write n_type. */
7915 md_number_to_chars (p + 4 * 2, (valueT) NT_GNU_PROPERTY_TYPE_0, 4);
7916
7917 /* Write n_name. */
7918 memcpy (p + 4 * 3, "GNU", 4);
7919
7920 /* Write 4-byte type. */
7921 md_number_to_chars (p + 4 * 4,
7922 (valueT) GNU_PROPERTY_X86_ISA_1_USED, 4);
7923
7924 /* Write 4-byte data size. */
7925 md_number_to_chars (p + 4 * 5, (valueT) 4, 4);
7926
7927 /* Write 4-byte data. */
7928 md_number_to_chars (p + 4 * 6, (valueT) x86_isa_1_used, 4);
7929
7930 /* Zero out paddings. */
7931 padding = isa_1_descsz - isa_1_descsz_raw;
7932 if (padding)
7933 memset (p + 4 * 7, 0, padding);
7934
7935 /* Write 4-byte type. */
7936 md_number_to_chars (p + isa_1_descsz + 4 * 4,
7937 (valueT) GNU_PROPERTY_X86_FEATURE_2_USED, 4);
7938
7939 /* Write 4-byte data size. */
7940 md_number_to_chars (p + isa_1_descsz + 4 * 5, (valueT) 4, 4);
7941
7942 /* Write 4-byte data. */
7943 md_number_to_chars (p + isa_1_descsz + 4 * 6,
7944 (valueT) x86_feature_2_used, 4);
7945
7946 /* Zero out paddings. */
7947 padding = feature_2_descsz - feature_2_descsz_raw;
7948 if (padding)
7949 memset (p + isa_1_descsz + 4 * 7, 0, padding);
7950
7951 /* We probably can't restore the current segment, for there likely
7952 isn't one yet... */
7953 if (seg && subseg)
7954 subseg_set (seg, subseg);
7955}
7956#endif
7957
29b0f896 7958static void
e3bb37b5 7959output_insn (void)
29b0f896 7960{
2bbd9c25
JJ
7961 fragS *insn_start_frag;
7962 offsetT insn_start_off;
7963
b4a3a7b4
L
7964#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
7965 if (IS_ELF && x86_used_note)
7966 {
7967 if (i.tm.cpu_flags.bitfield.cpucmov)
7968 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_CMOV;
7969 if (i.tm.cpu_flags.bitfield.cpusse)
7970 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_SSE;
7971 if (i.tm.cpu_flags.bitfield.cpusse2)
7972 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_SSE2;
7973 if (i.tm.cpu_flags.bitfield.cpusse3)
7974 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_SSE3;
7975 if (i.tm.cpu_flags.bitfield.cpussse3)
7976 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_SSSE3;
7977 if (i.tm.cpu_flags.bitfield.cpusse4_1)
7978 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_SSE4_1;
7979 if (i.tm.cpu_flags.bitfield.cpusse4_2)
7980 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_SSE4_2;
7981 if (i.tm.cpu_flags.bitfield.cpuavx)
7982 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_AVX;
7983 if (i.tm.cpu_flags.bitfield.cpuavx2)
7984 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_AVX2;
7985 if (i.tm.cpu_flags.bitfield.cpufma)
7986 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_FMA;
7987 if (i.tm.cpu_flags.bitfield.cpuavx512f)
7988 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_AVX512F;
7989 if (i.tm.cpu_flags.bitfield.cpuavx512cd)
7990 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_AVX512CD;
7991 if (i.tm.cpu_flags.bitfield.cpuavx512er)
7992 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_AVX512ER;
7993 if (i.tm.cpu_flags.bitfield.cpuavx512pf)
7994 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_AVX512PF;
7995 if (i.tm.cpu_flags.bitfield.cpuavx512vl)
7996 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_AVX512VL;
7997 if (i.tm.cpu_flags.bitfield.cpuavx512dq)
7998 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_AVX512DQ;
7999 if (i.tm.cpu_flags.bitfield.cpuavx512bw)
8000 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_AVX512BW;
8001 if (i.tm.cpu_flags.bitfield.cpuavx512_4fmaps)
8002 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_AVX512_4FMAPS;
8003 if (i.tm.cpu_flags.bitfield.cpuavx512_4vnniw)
8004 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_AVX512_4VNNIW;
8005 if (i.tm.cpu_flags.bitfield.cpuavx512_bitalg)
8006 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_AVX512_BITALG;
8007 if (i.tm.cpu_flags.bitfield.cpuavx512ifma)
8008 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_AVX512_IFMA;
8009 if (i.tm.cpu_flags.bitfield.cpuavx512vbmi)
8010 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_AVX512_VBMI;
8011 if (i.tm.cpu_flags.bitfield.cpuavx512_vbmi2)
8012 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_AVX512_VBMI2;
8013 if (i.tm.cpu_flags.bitfield.cpuavx512_vnni)
8014 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_AVX512_VNNI;
8015
8016 if (i.tm.cpu_flags.bitfield.cpu8087
8017 || i.tm.cpu_flags.bitfield.cpu287
8018 || i.tm.cpu_flags.bitfield.cpu387
8019 || i.tm.cpu_flags.bitfield.cpu687
8020 || i.tm.cpu_flags.bitfield.cpufisttp)
8021 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X87;
8022 /* Don't set GNU_PROPERTY_X86_FEATURE_2_MMX for prefetchtXXX nor
8023 Xfence instructions. */
8024 if (i.tm.base_opcode != 0xf18
8025 && i.tm.base_opcode != 0xf0d
8026 && i.tm.base_opcode != 0xfae
8027 && (i.has_regmmx
8028 || i.tm.cpu_flags.bitfield.cpummx
8029 || i.tm.cpu_flags.bitfield.cpua3dnow
8030 || i.tm.cpu_flags.bitfield.cpua3dnowa))
8031 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MMX;
8032 if (i.has_regxmm)
8033 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XMM;
8034 if (i.has_regymm)
8035 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_YMM;
8036 if (i.has_regzmm)
8037 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_ZMM;
8038 if (i.tm.cpu_flags.bitfield.cpufxsr)
8039 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_FXSR;
8040 if (i.tm.cpu_flags.bitfield.cpuxsave)
8041 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVE;
8042 if (i.tm.cpu_flags.bitfield.cpuxsaveopt)
8043 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT;
8044 if (i.tm.cpu_flags.bitfield.cpuxsavec)
8045 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEC;
8046 }
8047#endif
8048
29b0f896
AM
8049 /* Tie dwarf2 debug info to the address at the start of the insn.
8050 We can't do this after the insn has been output as the current
8051 frag may have been closed off. eg. by frag_var. */
8052 dwarf2_emit_insn (0);
8053
2bbd9c25
JJ
8054 insn_start_frag = frag_now;
8055 insn_start_off = frag_now_fix ();
8056
29b0f896 8057 /* Output jumps. */
40fb9820 8058 if (i.tm.opcode_modifier.jump)
29b0f896 8059 output_branch ();
40fb9820
L
8060 else if (i.tm.opcode_modifier.jumpbyte
8061 || i.tm.opcode_modifier.jumpdword)
29b0f896 8062 output_jump ();
40fb9820 8063 else if (i.tm.opcode_modifier.jumpintersegment)
29b0f896
AM
8064 output_interseg_jump ();
8065 else
8066 {
8067 /* Output normal instructions here. */
8068 char *p;
8069 unsigned char *q;
47465058 8070 unsigned int j;
331d2d0d 8071 unsigned int prefix;
4dffcebc 8072
e4e00185
AS
8073 if (avoid_fence
8074 && i.tm.base_opcode == 0xfae
8075 && i.operands == 1
8076 && i.imm_operands == 1
8077 && (i.op[0].imms->X_add_number == 0xe8
8078 || i.op[0].imms->X_add_number == 0xf0
8079 || i.op[0].imms->X_add_number == 0xf8))
8080 {
8081 /* Encode lfence, mfence, and sfence as
8082 f0 83 04 24 00 lock addl $0x0, (%{re}sp). */
8083 offsetT val = 0x240483f0ULL;
8084 p = frag_more (5);
8085 md_number_to_chars (p, val, 5);
8086 return;
8087 }
8088
d022bddd
IT
8089 /* Some processors fail on LOCK prefix. This options makes
8090 assembler ignore LOCK prefix and serves as a workaround. */
8091 if (omit_lock_prefix)
8092 {
8093 if (i.tm.base_opcode == LOCK_PREFIX_OPCODE)
8094 return;
8095 i.prefix[LOCK_PREFIX] = 0;
8096 }
8097
43234a1e
L
8098 /* Since the VEX/EVEX prefix contains the implicit prefix, we
8099 don't need the explicit prefix. */
8100 if (!i.tm.opcode_modifier.vex && !i.tm.opcode_modifier.evex)
bc4bd9ab 8101 {
c0f3af97 8102 switch (i.tm.opcode_length)
bc4bd9ab 8103 {
c0f3af97
L
8104 case 3:
8105 if (i.tm.base_opcode & 0xff000000)
4dffcebc 8106 {
c0f3af97 8107 prefix = (i.tm.base_opcode >> 24) & 0xff;
bd59a631 8108 add_prefix (prefix);
c0f3af97
L
8109 }
8110 break;
8111 case 2:
8112 if ((i.tm.base_opcode & 0xff0000) != 0)
8113 {
8114 prefix = (i.tm.base_opcode >> 16) & 0xff;
bd59a631
JB
8115 if (!i.tm.cpu_flags.bitfield.cpupadlock
8116 || prefix != REPE_PREFIX_OPCODE
8117 || (i.prefix[REP_PREFIX] != REPE_PREFIX_OPCODE))
4dffcebc
L
8118 add_prefix (prefix);
8119 }
c0f3af97
L
8120 break;
8121 case 1:
8122 break;
390c91cf
L
8123 case 0:
8124 /* Check for pseudo prefixes. */
8125 as_bad_where (insn_start_frag->fr_file,
8126 insn_start_frag->fr_line,
8127 _("pseudo prefix without instruction"));
8128 return;
c0f3af97
L
8129 default:
8130 abort ();
bc4bd9ab 8131 }
c0f3af97 8132
6d19a37a 8133#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
cf61b747
L
8134 /* For x32, add a dummy REX_OPCODE prefix for mov/add with
8135 R_X86_64_GOTTPOFF relocation so that linker can safely
8136 perform IE->LE optimization. */
8137 if (x86_elf_abi == X86_64_X32_ABI
8138 && i.operands == 2
8139 && i.reloc[0] == BFD_RELOC_X86_64_GOTTPOFF
8140 && i.prefix[REX_PREFIX] == 0)
8141 add_prefix (REX_OPCODE);
6d19a37a 8142#endif
cf61b747 8143
c0f3af97
L
8144 /* The prefix bytes. */
8145 for (j = ARRAY_SIZE (i.prefix), q = i.prefix; j > 0; j--, q++)
8146 if (*q)
8147 FRAG_APPEND_1_CHAR (*q);
0f10071e 8148 }
ae5c1c7b 8149 else
c0f3af97
L
8150 {
8151 for (j = 0, q = i.prefix; j < ARRAY_SIZE (i.prefix); j++, q++)
8152 if (*q)
8153 switch (j)
8154 {
8155 case REX_PREFIX:
8156 /* REX byte is encoded in VEX prefix. */
8157 break;
8158 case SEG_PREFIX:
8159 case ADDR_PREFIX:
8160 FRAG_APPEND_1_CHAR (*q);
8161 break;
8162 default:
8163 /* There should be no other prefixes for instructions
8164 with VEX prefix. */
8165 abort ();
8166 }
8167
43234a1e
L
8168 /* For EVEX instructions i.vrex should become 0 after
8169 build_evex_prefix. For VEX instructions upper 16 registers
8170 aren't available, so VREX should be 0. */
8171 if (i.vrex)
8172 abort ();
c0f3af97
L
8173 /* Now the VEX prefix. */
8174 p = frag_more (i.vex.length);
8175 for (j = 0; j < i.vex.length; j++)
8176 p[j] = i.vex.bytes[j];
8177 }
252b5132 8178
29b0f896 8179 /* Now the opcode; be careful about word order here! */
4dffcebc 8180 if (i.tm.opcode_length == 1)
29b0f896
AM
8181 {
8182 FRAG_APPEND_1_CHAR (i.tm.base_opcode);
8183 }
8184 else
8185 {
4dffcebc 8186 switch (i.tm.opcode_length)
331d2d0d 8187 {
43234a1e
L
8188 case 4:
8189 p = frag_more (4);
8190 *p++ = (i.tm.base_opcode >> 24) & 0xff;
8191 *p++ = (i.tm.base_opcode >> 16) & 0xff;
8192 break;
4dffcebc 8193 case 3:
331d2d0d
L
8194 p = frag_more (3);
8195 *p++ = (i.tm.base_opcode >> 16) & 0xff;
4dffcebc
L
8196 break;
8197 case 2:
8198 p = frag_more (2);
8199 break;
8200 default:
8201 abort ();
8202 break;
331d2d0d 8203 }
0f10071e 8204
29b0f896
AM
8205 /* Put out high byte first: can't use md_number_to_chars! */
8206 *p++ = (i.tm.base_opcode >> 8) & 0xff;
8207 *p = i.tm.base_opcode & 0xff;
8208 }
3e73aa7c 8209
29b0f896 8210 /* Now the modrm byte and sib byte (if present). */
40fb9820 8211 if (i.tm.opcode_modifier.modrm)
29b0f896 8212 {
4a3523fa
L
8213 FRAG_APPEND_1_CHAR ((i.rm.regmem << 0
8214 | i.rm.reg << 3
8215 | i.rm.mode << 6));
29b0f896
AM
8216 /* If i.rm.regmem == ESP (4)
8217 && i.rm.mode != (Register mode)
8218 && not 16 bit
8219 ==> need second modrm byte. */
8220 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
8221 && i.rm.mode != 3
dc821c5f 8222 && !(i.base_reg && i.base_reg->reg_type.bitfield.word))
4a3523fa
L
8223 FRAG_APPEND_1_CHAR ((i.sib.base << 0
8224 | i.sib.index << 3
8225 | i.sib.scale << 6));
29b0f896 8226 }
3e73aa7c 8227
29b0f896 8228 if (i.disp_operands)
2bbd9c25 8229 output_disp (insn_start_frag, insn_start_off);
3e73aa7c 8230
29b0f896 8231 if (i.imm_operands)
2bbd9c25 8232 output_imm (insn_start_frag, insn_start_off);
29b0f896 8233 }
252b5132 8234
29b0f896
AM
8235#ifdef DEBUG386
8236 if (flag_debug)
8237 {
7b81dfbb 8238 pi ("" /*line*/, &i);
29b0f896
AM
8239 }
8240#endif /* DEBUG386 */
8241}
252b5132 8242
e205caa7
L
8243/* Return the size of the displacement operand N. */
8244
8245static int
8246disp_size (unsigned int n)
8247{
8248 int size = 4;
43234a1e 8249
b5014f7a 8250 if (i.types[n].bitfield.disp64)
40fb9820
L
8251 size = 8;
8252 else if (i.types[n].bitfield.disp8)
8253 size = 1;
8254 else if (i.types[n].bitfield.disp16)
8255 size = 2;
e205caa7
L
8256 return size;
8257}
8258
8259/* Return the size of the immediate operand N. */
8260
8261static int
8262imm_size (unsigned int n)
8263{
8264 int size = 4;
40fb9820
L
8265 if (i.types[n].bitfield.imm64)
8266 size = 8;
8267 else if (i.types[n].bitfield.imm8 || i.types[n].bitfield.imm8s)
8268 size = 1;
8269 else if (i.types[n].bitfield.imm16)
8270 size = 2;
e205caa7
L
8271 return size;
8272}
8273
29b0f896 8274static void
64e74474 8275output_disp (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
8276{
8277 char *p;
8278 unsigned int n;
252b5132 8279
29b0f896
AM
8280 for (n = 0; n < i.operands; n++)
8281 {
b5014f7a 8282 if (operand_type_check (i.types[n], disp))
29b0f896
AM
8283 {
8284 if (i.op[n].disps->X_op == O_constant)
8285 {
e205caa7 8286 int size = disp_size (n);
43234a1e 8287 offsetT val = i.op[n].disps->X_add_number;
252b5132 8288
629cfaf1
JB
8289 val = offset_in_range (val >> (size == 1 ? i.memshift : 0),
8290 size);
29b0f896
AM
8291 p = frag_more (size);
8292 md_number_to_chars (p, val, size);
8293 }
8294 else
8295 {
f86103b7 8296 enum bfd_reloc_code_real reloc_type;
e205caa7 8297 int size = disp_size (n);
40fb9820 8298 int sign = i.types[n].bitfield.disp32s;
29b0f896 8299 int pcrel = (i.flags[n] & Operand_PCrel) != 0;
02a86693 8300 fixS *fixP;
29b0f896 8301
e205caa7 8302 /* We can't have 8 bit displacement here. */
9c2799c2 8303 gas_assert (!i.types[n].bitfield.disp8);
e205caa7 8304
29b0f896
AM
8305 /* The PC relative address is computed relative
8306 to the instruction boundary, so in case immediate
8307 fields follows, we need to adjust the value. */
8308 if (pcrel && i.imm_operands)
8309 {
29b0f896 8310 unsigned int n1;
e205caa7 8311 int sz = 0;
252b5132 8312
29b0f896 8313 for (n1 = 0; n1 < i.operands; n1++)
40fb9820 8314 if (operand_type_check (i.types[n1], imm))
252b5132 8315 {
e205caa7
L
8316 /* Only one immediate is allowed for PC
8317 relative address. */
9c2799c2 8318 gas_assert (sz == 0);
e205caa7
L
8319 sz = imm_size (n1);
8320 i.op[n].disps->X_add_number -= sz;
252b5132 8321 }
29b0f896 8322 /* We should find the immediate. */
9c2799c2 8323 gas_assert (sz != 0);
29b0f896 8324 }
520dc8e8 8325
29b0f896 8326 p = frag_more (size);
d258b828 8327 reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
d6ab8113 8328 if (GOT_symbol
2bbd9c25 8329 && GOT_symbol == i.op[n].disps->X_add_symbol
d6ab8113 8330 && (((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
8331 || reloc_type == BFD_RELOC_X86_64_32S
8332 || (reloc_type == BFD_RELOC_64
8333 && object_64bit))
d6ab8113
JB
8334 && (i.op[n].disps->X_op == O_symbol
8335 || (i.op[n].disps->X_op == O_add
8336 && ((symbol_get_value_expression
8337 (i.op[n].disps->X_op_symbol)->X_op)
8338 == O_subtract))))
8339 || reloc_type == BFD_RELOC_32_PCREL))
2bbd9c25
JJ
8340 {
8341 offsetT add;
8342
8343 if (insn_start_frag == frag_now)
8344 add = (p - frag_now->fr_literal) - insn_start_off;
8345 else
8346 {
8347 fragS *fr;
8348
8349 add = insn_start_frag->fr_fix - insn_start_off;
8350 for (fr = insn_start_frag->fr_next;
8351 fr && fr != frag_now; fr = fr->fr_next)
8352 add += fr->fr_fix;
8353 add += p - frag_now->fr_literal;
8354 }
8355
4fa24527 8356 if (!object_64bit)
7b81dfbb
AJ
8357 {
8358 reloc_type = BFD_RELOC_386_GOTPC;
8359 i.op[n].imms->X_add_number += add;
8360 }
8361 else if (reloc_type == BFD_RELOC_64)
8362 reloc_type = BFD_RELOC_X86_64_GOTPC64;
d6ab8113 8363 else
7b81dfbb
AJ
8364 /* Don't do the adjustment for x86-64, as there
8365 the pcrel addressing is relative to the _next_
8366 insn, and that is taken care of in other code. */
d6ab8113 8367 reloc_type = BFD_RELOC_X86_64_GOTPC32;
2bbd9c25 8368 }
02a86693
L
8369 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal,
8370 size, i.op[n].disps, pcrel,
8371 reloc_type);
8372 /* Check for "call/jmp *mem", "mov mem, %reg",
8373 "test %reg, mem" and "binop mem, %reg" where binop
8374 is one of adc, add, and, cmp, or, sbb, sub, xor
0cb4071e
L
8375 instructions. Always generate R_386_GOT32X for
8376 "sym*GOT" operand in 32-bit mode. */
8377 if ((generate_relax_relocations
8378 || (!object_64bit
8379 && i.rm.mode == 0
8380 && i.rm.regmem == 5))
8381 && (i.rm.mode == 2
8382 || (i.rm.mode == 0 && i.rm.regmem == 5))
02a86693
L
8383 && ((i.operands == 1
8384 && i.tm.base_opcode == 0xff
8385 && (i.rm.reg == 2 || i.rm.reg == 4))
8386 || (i.operands == 2
8387 && (i.tm.base_opcode == 0x8b
8388 || i.tm.base_opcode == 0x85
8389 || (i.tm.base_opcode & 0xc7) == 0x03))))
8390 {
8391 if (object_64bit)
8392 {
8393 fixP->fx_tcbit = i.rex != 0;
8394 if (i.base_reg
e968fc9b 8395 && (i.base_reg->reg_num == RegIP))
02a86693
L
8396 fixP->fx_tcbit2 = 1;
8397 }
8398 else
8399 fixP->fx_tcbit2 = 1;
8400 }
29b0f896
AM
8401 }
8402 }
8403 }
8404}
252b5132 8405
29b0f896 8406static void
64e74474 8407output_imm (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
8408{
8409 char *p;
8410 unsigned int n;
252b5132 8411
29b0f896
AM
8412 for (n = 0; n < i.operands; n++)
8413 {
43234a1e
L
8414 /* Skip SAE/RC Imm operand in EVEX. They are already handled. */
8415 if (i.rounding && (int) n == i.rounding->operand)
8416 continue;
8417
40fb9820 8418 if (operand_type_check (i.types[n], imm))
29b0f896
AM
8419 {
8420 if (i.op[n].imms->X_op == O_constant)
8421 {
e205caa7 8422 int size = imm_size (n);
29b0f896 8423 offsetT val;
b4cac588 8424
29b0f896
AM
8425 val = offset_in_range (i.op[n].imms->X_add_number,
8426 size);
8427 p = frag_more (size);
8428 md_number_to_chars (p, val, size);
8429 }
8430 else
8431 {
8432 /* Not absolute_section.
8433 Need a 32-bit fixup (don't support 8bit
8434 non-absolute imms). Try to support other
8435 sizes ... */
f86103b7 8436 enum bfd_reloc_code_real reloc_type;
e205caa7
L
8437 int size = imm_size (n);
8438 int sign;
29b0f896 8439
40fb9820 8440 if (i.types[n].bitfield.imm32s
a7d61044 8441 && (i.suffix == QWORD_MNEM_SUFFIX
40fb9820 8442 || (!i.suffix && i.tm.opcode_modifier.no_lsuf)))
29b0f896 8443 sign = 1;
e205caa7
L
8444 else
8445 sign = 0;
520dc8e8 8446
29b0f896 8447 p = frag_more (size);
d258b828 8448 reloc_type = reloc (size, 0, sign, i.reloc[n]);
f86103b7 8449
2bbd9c25
JJ
8450 /* This is tough to explain. We end up with this one if we
8451 * have operands that look like
8452 * "_GLOBAL_OFFSET_TABLE_+[.-.L284]". The goal here is to
8453 * obtain the absolute address of the GOT, and it is strongly
8454 * preferable from a performance point of view to avoid using
8455 * a runtime relocation for this. The actual sequence of
8456 * instructions often look something like:
8457 *
8458 * call .L66
8459 * .L66:
8460 * popl %ebx
8461 * addl $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
8462 *
8463 * The call and pop essentially return the absolute address
8464 * of the label .L66 and store it in %ebx. The linker itself
8465 * will ultimately change the first operand of the addl so
8466 * that %ebx points to the GOT, but to keep things simple, the
8467 * .o file must have this operand set so that it generates not
8468 * the absolute address of .L66, but the absolute address of
8469 * itself. This allows the linker itself simply treat a GOTPC
8470 * relocation as asking for a pcrel offset to the GOT to be
8471 * added in, and the addend of the relocation is stored in the
8472 * operand field for the instruction itself.
8473 *
8474 * Our job here is to fix the operand so that it would add
8475 * the correct offset so that %ebx would point to itself. The
8476 * thing that is tricky is that .-.L66 will point to the
8477 * beginning of the instruction, so we need to further modify
8478 * the operand so that it will point to itself. There are
8479 * other cases where you have something like:
8480 *
8481 * .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
8482 *
8483 * and here no correction would be required. Internally in
8484 * the assembler we treat operands of this form as not being
8485 * pcrel since the '.' is explicitly mentioned, and I wonder
8486 * whether it would simplify matters to do it this way. Who
8487 * knows. In earlier versions of the PIC patches, the
8488 * pcrel_adjust field was used to store the correction, but
8489 * since the expression is not pcrel, I felt it would be
8490 * confusing to do it this way. */
8491
d6ab8113 8492 if ((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
8493 || reloc_type == BFD_RELOC_X86_64_32S
8494 || reloc_type == BFD_RELOC_64)
29b0f896
AM
8495 && GOT_symbol
8496 && GOT_symbol == i.op[n].imms->X_add_symbol
8497 && (i.op[n].imms->X_op == O_symbol
8498 || (i.op[n].imms->X_op == O_add
8499 && ((symbol_get_value_expression
8500 (i.op[n].imms->X_op_symbol)->X_op)
8501 == O_subtract))))
8502 {
2bbd9c25
JJ
8503 offsetT add;
8504
8505 if (insn_start_frag == frag_now)
8506 add = (p - frag_now->fr_literal) - insn_start_off;
8507 else
8508 {
8509 fragS *fr;
8510
8511 add = insn_start_frag->fr_fix - insn_start_off;
8512 for (fr = insn_start_frag->fr_next;
8513 fr && fr != frag_now; fr = fr->fr_next)
8514 add += fr->fr_fix;
8515 add += p - frag_now->fr_literal;
8516 }
8517
4fa24527 8518 if (!object_64bit)
d6ab8113 8519 reloc_type = BFD_RELOC_386_GOTPC;
7b81dfbb 8520 else if (size == 4)
d6ab8113 8521 reloc_type = BFD_RELOC_X86_64_GOTPC32;
7b81dfbb
AJ
8522 else if (size == 8)
8523 reloc_type = BFD_RELOC_X86_64_GOTPC64;
2bbd9c25 8524 i.op[n].imms->X_add_number += add;
29b0f896 8525 }
29b0f896
AM
8526 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
8527 i.op[n].imms, 0, reloc_type);
8528 }
8529 }
8530 }
252b5132
RH
8531}
8532\f
d182319b
JB
8533/* x86_cons_fix_new is called via the expression parsing code when a
8534 reloc is needed. We use this hook to get the correct .got reloc. */
d182319b
JB
8535static int cons_sign = -1;
8536
8537void
e3bb37b5 8538x86_cons_fix_new (fragS *frag, unsigned int off, unsigned int len,
62ebcb5c 8539 expressionS *exp, bfd_reloc_code_real_type r)
d182319b 8540{
d258b828 8541 r = reloc (len, 0, cons_sign, r);
d182319b
JB
8542
8543#ifdef TE_PE
8544 if (exp->X_op == O_secrel)
8545 {
8546 exp->X_op = O_symbol;
8547 r = BFD_RELOC_32_SECREL;
8548 }
8549#endif
8550
8551 fix_new_exp (frag, off, len, exp, 0, r);
8552}
8553
357d1bd8
L
8554/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
8555 purpose of the `.dc.a' internal pseudo-op. */
8556
8557int
8558x86_address_bytes (void)
8559{
8560 if ((stdoutput->arch_info->mach & bfd_mach_x64_32))
8561 return 4;
8562 return stdoutput->arch_info->bits_per_address / 8;
8563}
8564
d382c579
TG
8565#if !(defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined (OBJ_MACH_O)) \
8566 || defined (LEX_AT)
d258b828 8567# define lex_got(reloc, adjust, types) NULL
718ddfc0 8568#else
f3c180ae
AM
8569/* Parse operands of the form
8570 <symbol>@GOTOFF+<nnn>
8571 and similar .plt or .got references.
8572
8573 If we find one, set up the correct relocation in RELOC and copy the
8574 input string, minus the `@GOTOFF' into a malloc'd buffer for
8575 parsing by the calling routine. Return this buffer, and if ADJUST
8576 is non-null set it to the length of the string we removed from the
8577 input line. Otherwise return NULL. */
8578static char *
91d6fa6a 8579lex_got (enum bfd_reloc_code_real *rel,
64e74474 8580 int *adjust,
d258b828 8581 i386_operand_type *types)
f3c180ae 8582{
7b81dfbb
AJ
8583 /* Some of the relocations depend on the size of what field is to
8584 be relocated. But in our callers i386_immediate and i386_displacement
8585 we don't yet know the operand size (this will be set by insn
8586 matching). Hence we record the word32 relocation here,
8587 and adjust the reloc according to the real size in reloc(). */
f3c180ae
AM
8588 static const struct {
8589 const char *str;
cff8d58a 8590 int len;
4fa24527 8591 const enum bfd_reloc_code_real rel[2];
40fb9820 8592 const i386_operand_type types64;
f3c180ae 8593 } gotrel[] = {
8ce3d284 8594#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
8595 { STRING_COMMA_LEN ("SIZE"), { BFD_RELOC_SIZE32,
8596 BFD_RELOC_SIZE32 },
8597 OPERAND_TYPE_IMM32_64 },
8ce3d284 8598#endif
cff8d58a
L
8599 { STRING_COMMA_LEN ("PLTOFF"), { _dummy_first_bfd_reloc_code_real,
8600 BFD_RELOC_X86_64_PLTOFF64 },
40fb9820 8601 OPERAND_TYPE_IMM64 },
cff8d58a
L
8602 { STRING_COMMA_LEN ("PLT"), { BFD_RELOC_386_PLT32,
8603 BFD_RELOC_X86_64_PLT32 },
40fb9820 8604 OPERAND_TYPE_IMM32_32S_DISP32 },
cff8d58a
L
8605 { STRING_COMMA_LEN ("GOTPLT"), { _dummy_first_bfd_reloc_code_real,
8606 BFD_RELOC_X86_64_GOTPLT64 },
40fb9820 8607 OPERAND_TYPE_IMM64_DISP64 },
cff8d58a
L
8608 { STRING_COMMA_LEN ("GOTOFF"), { BFD_RELOC_386_GOTOFF,
8609 BFD_RELOC_X86_64_GOTOFF64 },
40fb9820 8610 OPERAND_TYPE_IMM64_DISP64 },
cff8d58a
L
8611 { STRING_COMMA_LEN ("GOTPCREL"), { _dummy_first_bfd_reloc_code_real,
8612 BFD_RELOC_X86_64_GOTPCREL },
40fb9820 8613 OPERAND_TYPE_IMM32_32S_DISP32 },
cff8d58a
L
8614 { STRING_COMMA_LEN ("TLSGD"), { BFD_RELOC_386_TLS_GD,
8615 BFD_RELOC_X86_64_TLSGD },
40fb9820 8616 OPERAND_TYPE_IMM32_32S_DISP32 },
cff8d58a
L
8617 { STRING_COMMA_LEN ("TLSLDM"), { BFD_RELOC_386_TLS_LDM,
8618 _dummy_first_bfd_reloc_code_real },
40fb9820 8619 OPERAND_TYPE_NONE },
cff8d58a
L
8620 { STRING_COMMA_LEN ("TLSLD"), { _dummy_first_bfd_reloc_code_real,
8621 BFD_RELOC_X86_64_TLSLD },
40fb9820 8622 OPERAND_TYPE_IMM32_32S_DISP32 },
cff8d58a
L
8623 { STRING_COMMA_LEN ("GOTTPOFF"), { BFD_RELOC_386_TLS_IE_32,
8624 BFD_RELOC_X86_64_GOTTPOFF },
40fb9820 8625 OPERAND_TYPE_IMM32_32S_DISP32 },
cff8d58a
L
8626 { STRING_COMMA_LEN ("TPOFF"), { BFD_RELOC_386_TLS_LE_32,
8627 BFD_RELOC_X86_64_TPOFF32 },
40fb9820 8628 OPERAND_TYPE_IMM32_32S_64_DISP32_64 },
cff8d58a
L
8629 { STRING_COMMA_LEN ("NTPOFF"), { BFD_RELOC_386_TLS_LE,
8630 _dummy_first_bfd_reloc_code_real },
40fb9820 8631 OPERAND_TYPE_NONE },
cff8d58a
L
8632 { STRING_COMMA_LEN ("DTPOFF"), { BFD_RELOC_386_TLS_LDO_32,
8633 BFD_RELOC_X86_64_DTPOFF32 },
40fb9820 8634 OPERAND_TYPE_IMM32_32S_64_DISP32_64 },
cff8d58a
L
8635 { STRING_COMMA_LEN ("GOTNTPOFF"),{ BFD_RELOC_386_TLS_GOTIE,
8636 _dummy_first_bfd_reloc_code_real },
40fb9820 8637 OPERAND_TYPE_NONE },
cff8d58a
L
8638 { STRING_COMMA_LEN ("INDNTPOFF"),{ BFD_RELOC_386_TLS_IE,
8639 _dummy_first_bfd_reloc_code_real },
40fb9820 8640 OPERAND_TYPE_NONE },
cff8d58a
L
8641 { STRING_COMMA_LEN ("GOT"), { BFD_RELOC_386_GOT32,
8642 BFD_RELOC_X86_64_GOT32 },
40fb9820 8643 OPERAND_TYPE_IMM32_32S_64_DISP32 },
cff8d58a
L
8644 { STRING_COMMA_LEN ("TLSDESC"), { BFD_RELOC_386_TLS_GOTDESC,
8645 BFD_RELOC_X86_64_GOTPC32_TLSDESC },
40fb9820 8646 OPERAND_TYPE_IMM32_32S_DISP32 },
cff8d58a
L
8647 { STRING_COMMA_LEN ("TLSCALL"), { BFD_RELOC_386_TLS_DESC_CALL,
8648 BFD_RELOC_X86_64_TLSDESC_CALL },
40fb9820 8649 OPERAND_TYPE_IMM32_32S_DISP32 },
f3c180ae
AM
8650 };
8651 char *cp;
8652 unsigned int j;
8653
d382c579 8654#if defined (OBJ_MAYBE_ELF)
718ddfc0
JB
8655 if (!IS_ELF)
8656 return NULL;
d382c579 8657#endif
718ddfc0 8658
f3c180ae 8659 for (cp = input_line_pointer; *cp != '@'; cp++)
67c11a9b 8660 if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
f3c180ae
AM
8661 return NULL;
8662
47465058 8663 for (j = 0; j < ARRAY_SIZE (gotrel); j++)
f3c180ae 8664 {
cff8d58a 8665 int len = gotrel[j].len;
28f81592 8666 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
f3c180ae 8667 {
4fa24527 8668 if (gotrel[j].rel[object_64bit] != 0)
f3c180ae 8669 {
28f81592
AM
8670 int first, second;
8671 char *tmpbuf, *past_reloc;
f3c180ae 8672
91d6fa6a 8673 *rel = gotrel[j].rel[object_64bit];
f3c180ae 8674
3956db08
JB
8675 if (types)
8676 {
8677 if (flag_code != CODE_64BIT)
40fb9820
L
8678 {
8679 types->bitfield.imm32 = 1;
8680 types->bitfield.disp32 = 1;
8681 }
3956db08
JB
8682 else
8683 *types = gotrel[j].types64;
8684 }
8685
8fd4256d 8686 if (j != 0 && GOT_symbol == NULL)
f3c180ae
AM
8687 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
8688
28f81592 8689 /* The length of the first part of our input line. */
f3c180ae 8690 first = cp - input_line_pointer;
28f81592
AM
8691
8692 /* The second part goes from after the reloc token until
67c11a9b 8693 (and including) an end_of_line char or comma. */
28f81592 8694 past_reloc = cp + 1 + len;
67c11a9b
AM
8695 cp = past_reloc;
8696 while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
8697 ++cp;
8698 second = cp + 1 - past_reloc;
28f81592
AM
8699
8700 /* Allocate and copy string. The trailing NUL shouldn't
8701 be necessary, but be safe. */
add39d23 8702 tmpbuf = XNEWVEC (char, first + second + 2);
f3c180ae 8703 memcpy (tmpbuf, input_line_pointer, first);
0787a12d
AM
8704 if (second != 0 && *past_reloc != ' ')
8705 /* Replace the relocation token with ' ', so that
8706 errors like foo@GOTOFF1 will be detected. */
8707 tmpbuf[first++] = ' ';
af89796a
L
8708 else
8709 /* Increment length by 1 if the relocation token is
8710 removed. */
8711 len++;
8712 if (adjust)
8713 *adjust = len;
0787a12d
AM
8714 memcpy (tmpbuf + first, past_reloc, second);
8715 tmpbuf[first + second] = '\0';
f3c180ae
AM
8716 return tmpbuf;
8717 }
8718
4fa24527
JB
8719 as_bad (_("@%s reloc is not supported with %d-bit output format"),
8720 gotrel[j].str, 1 << (5 + object_64bit));
f3c180ae
AM
8721 return NULL;
8722 }
8723 }
8724
8725 /* Might be a symbol version string. Don't as_bad here. */
8726 return NULL;
8727}
4e4f7c87 8728#endif
f3c180ae 8729
a988325c
NC
8730#ifdef TE_PE
8731#ifdef lex_got
8732#undef lex_got
8733#endif
8734/* Parse operands of the form
8735 <symbol>@SECREL32+<nnn>
8736
8737 If we find one, set up the correct relocation in RELOC and copy the
8738 input string, minus the `@SECREL32' into a malloc'd buffer for
8739 parsing by the calling routine. Return this buffer, and if ADJUST
8740 is non-null set it to the length of the string we removed from the
34bca508
L
8741 input line. Otherwise return NULL.
8742
a988325c
NC
8743 This function is copied from the ELF version above adjusted for PE targets. */
8744
8745static char *
8746lex_got (enum bfd_reloc_code_real *rel ATTRIBUTE_UNUSED,
8747 int *adjust ATTRIBUTE_UNUSED,
d258b828 8748 i386_operand_type *types)
a988325c
NC
8749{
8750 static const struct
8751 {
8752 const char *str;
8753 int len;
8754 const enum bfd_reloc_code_real rel[2];
8755 const i386_operand_type types64;
8756 }
8757 gotrel[] =
8758 {
8759 { STRING_COMMA_LEN ("SECREL32"), { BFD_RELOC_32_SECREL,
8760 BFD_RELOC_32_SECREL },
8761 OPERAND_TYPE_IMM32_32S_64_DISP32_64 },
8762 };
8763
8764 char *cp;
8765 unsigned j;
8766
8767 for (cp = input_line_pointer; *cp != '@'; cp++)
8768 if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
8769 return NULL;
8770
8771 for (j = 0; j < ARRAY_SIZE (gotrel); j++)
8772 {
8773 int len = gotrel[j].len;
8774
8775 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
8776 {
8777 if (gotrel[j].rel[object_64bit] != 0)
8778 {
8779 int first, second;
8780 char *tmpbuf, *past_reloc;
8781
8782 *rel = gotrel[j].rel[object_64bit];
8783 if (adjust)
8784 *adjust = len;
8785
8786 if (types)
8787 {
8788 if (flag_code != CODE_64BIT)
8789 {
8790 types->bitfield.imm32 = 1;
8791 types->bitfield.disp32 = 1;
8792 }
8793 else
8794 *types = gotrel[j].types64;
8795 }
8796
8797 /* The length of the first part of our input line. */
8798 first = cp - input_line_pointer;
8799
8800 /* The second part goes from after the reloc token until
8801 (and including) an end_of_line char or comma. */
8802 past_reloc = cp + 1 + len;
8803 cp = past_reloc;
8804 while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
8805 ++cp;
8806 second = cp + 1 - past_reloc;
8807
8808 /* Allocate and copy string. The trailing NUL shouldn't
8809 be necessary, but be safe. */
add39d23 8810 tmpbuf = XNEWVEC (char, first + second + 2);
a988325c
NC
8811 memcpy (tmpbuf, input_line_pointer, first);
8812 if (second != 0 && *past_reloc != ' ')
8813 /* Replace the relocation token with ' ', so that
8814 errors like foo@SECLREL321 will be detected. */
8815 tmpbuf[first++] = ' ';
8816 memcpy (tmpbuf + first, past_reloc, second);
8817 tmpbuf[first + second] = '\0';
8818 return tmpbuf;
8819 }
8820
8821 as_bad (_("@%s reloc is not supported with %d-bit output format"),
8822 gotrel[j].str, 1 << (5 + object_64bit));
8823 return NULL;
8824 }
8825 }
8826
8827 /* Might be a symbol version string. Don't as_bad here. */
8828 return NULL;
8829}
8830
8831#endif /* TE_PE */
8832
62ebcb5c 8833bfd_reloc_code_real_type
e3bb37b5 8834x86_cons (expressionS *exp, int size)
f3c180ae 8835{
62ebcb5c
AM
8836 bfd_reloc_code_real_type got_reloc = NO_RELOC;
8837
ee86248c
JB
8838 intel_syntax = -intel_syntax;
8839
3c7b9c2c 8840 exp->X_md = 0;
4fa24527 8841 if (size == 4 || (object_64bit && size == 8))
f3c180ae
AM
8842 {
8843 /* Handle @GOTOFF and the like in an expression. */
8844 char *save;
8845 char *gotfree_input_line;
4a57f2cf 8846 int adjust = 0;
f3c180ae
AM
8847
8848 save = input_line_pointer;
d258b828 8849 gotfree_input_line = lex_got (&got_reloc, &adjust, NULL);
f3c180ae
AM
8850 if (gotfree_input_line)
8851 input_line_pointer = gotfree_input_line;
8852
8853 expression (exp);
8854
8855 if (gotfree_input_line)
8856 {
8857 /* expression () has merrily parsed up to the end of line,
8858 or a comma - in the wrong buffer. Transfer how far
8859 input_line_pointer has moved to the right buffer. */
8860 input_line_pointer = (save
8861 + (input_line_pointer - gotfree_input_line)
8862 + adjust);
8863 free (gotfree_input_line);
3992d3b7
AM
8864 if (exp->X_op == O_constant
8865 || exp->X_op == O_absent
8866 || exp->X_op == O_illegal
0398aac5 8867 || exp->X_op == O_register
3992d3b7
AM
8868 || exp->X_op == O_big)
8869 {
8870 char c = *input_line_pointer;
8871 *input_line_pointer = 0;
8872 as_bad (_("missing or invalid expression `%s'"), save);
8873 *input_line_pointer = c;
8874 }
f3c180ae
AM
8875 }
8876 }
8877 else
8878 expression (exp);
ee86248c
JB
8879
8880 intel_syntax = -intel_syntax;
8881
8882 if (intel_syntax)
8883 i386_intel_simplify (exp);
62ebcb5c
AM
8884
8885 return got_reloc;
f3c180ae 8886}
f3c180ae 8887
9f32dd5b
L
8888static void
8889signed_cons (int size)
6482c264 8890{
d182319b
JB
8891 if (flag_code == CODE_64BIT)
8892 cons_sign = 1;
8893 cons (size);
8894 cons_sign = -1;
6482c264
NC
8895}
8896
d182319b 8897#ifdef TE_PE
6482c264 8898static void
7016a5d5 8899pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
6482c264
NC
8900{
8901 expressionS exp;
8902
8903 do
8904 {
8905 expression (&exp);
8906 if (exp.X_op == O_symbol)
8907 exp.X_op = O_secrel;
8908
8909 emit_expr (&exp, 4);
8910 }
8911 while (*input_line_pointer++ == ',');
8912
8913 input_line_pointer--;
8914 demand_empty_rest_of_line ();
8915}
6482c264
NC
8916#endif
8917
43234a1e
L
8918/* Handle Vector operations. */
8919
8920static char *
8921check_VecOperations (char *op_string, char *op_end)
8922{
8923 const reg_entry *mask;
8924 const char *saved;
8925 char *end_op;
8926
8927 while (*op_string
8928 && (op_end == NULL || op_string < op_end))
8929 {
8930 saved = op_string;
8931 if (*op_string == '{')
8932 {
8933 op_string++;
8934
8935 /* Check broadcasts. */
8936 if (strncmp (op_string, "1to", 3) == 0)
8937 {
8938 int bcst_type;
8939
8940 if (i.broadcast)
8941 goto duplicated_vec_op;
8942
8943 op_string += 3;
8944 if (*op_string == '8')
8e6e0792 8945 bcst_type = 8;
b28d1bda 8946 else if (*op_string == '4')
8e6e0792 8947 bcst_type = 4;
b28d1bda 8948 else if (*op_string == '2')
8e6e0792 8949 bcst_type = 2;
43234a1e
L
8950 else if (*op_string == '1'
8951 && *(op_string+1) == '6')
8952 {
8e6e0792 8953 bcst_type = 16;
43234a1e
L
8954 op_string++;
8955 }
8956 else
8957 {
8958 as_bad (_("Unsupported broadcast: `%s'"), saved);
8959 return NULL;
8960 }
8961 op_string++;
8962
8963 broadcast_op.type = bcst_type;
8964 broadcast_op.operand = this_operand;
1f75763a 8965 broadcast_op.bytes = 0;
43234a1e
L
8966 i.broadcast = &broadcast_op;
8967 }
8968 /* Check masking operation. */
8969 else if ((mask = parse_register (op_string, &end_op)) != NULL)
8970 {
8971 /* k0 can't be used for write mask. */
6d2cd6b2 8972 if (!mask->reg_type.bitfield.regmask || mask->reg_num == 0)
43234a1e 8973 {
6d2cd6b2
JB
8974 as_bad (_("`%s%s' can't be used for write mask"),
8975 register_prefix, mask->reg_name);
43234a1e
L
8976 return NULL;
8977 }
8978
8979 if (!i.mask)
8980 {
8981 mask_op.mask = mask;
8982 mask_op.zeroing = 0;
8983 mask_op.operand = this_operand;
8984 i.mask = &mask_op;
8985 }
8986 else
8987 {
8988 if (i.mask->mask)
8989 goto duplicated_vec_op;
8990
8991 i.mask->mask = mask;
8992
8993 /* Only "{z}" is allowed here. No need to check
8994 zeroing mask explicitly. */
8995 if (i.mask->operand != this_operand)
8996 {
8997 as_bad (_("invalid write mask `%s'"), saved);
8998 return NULL;
8999 }
9000 }
9001
9002 op_string = end_op;
9003 }
9004 /* Check zeroing-flag for masking operation. */
9005 else if (*op_string == 'z')
9006 {
9007 if (!i.mask)
9008 {
9009 mask_op.mask = NULL;
9010 mask_op.zeroing = 1;
9011 mask_op.operand = this_operand;
9012 i.mask = &mask_op;
9013 }
9014 else
9015 {
9016 if (i.mask->zeroing)
9017 {
9018 duplicated_vec_op:
9019 as_bad (_("duplicated `%s'"), saved);
9020 return NULL;
9021 }
9022
9023 i.mask->zeroing = 1;
9024
9025 /* Only "{%k}" is allowed here. No need to check mask
9026 register explicitly. */
9027 if (i.mask->operand != this_operand)
9028 {
9029 as_bad (_("invalid zeroing-masking `%s'"),
9030 saved);
9031 return NULL;
9032 }
9033 }
9034
9035 op_string++;
9036 }
9037 else
9038 goto unknown_vec_op;
9039
9040 if (*op_string != '}')
9041 {
9042 as_bad (_("missing `}' in `%s'"), saved);
9043 return NULL;
9044 }
9045 op_string++;
0ba3a731
L
9046
9047 /* Strip whitespace since the addition of pseudo prefixes
9048 changed how the scrubber treats '{'. */
9049 if (is_space_char (*op_string))
9050 ++op_string;
9051
43234a1e
L
9052 continue;
9053 }
9054 unknown_vec_op:
9055 /* We don't know this one. */
9056 as_bad (_("unknown vector operation: `%s'"), saved);
9057 return NULL;
9058 }
9059
6d2cd6b2
JB
9060 if (i.mask && i.mask->zeroing && !i.mask->mask)
9061 {
9062 as_bad (_("zeroing-masking only allowed with write mask"));
9063 return NULL;
9064 }
9065
43234a1e
L
9066 return op_string;
9067}
9068
252b5132 9069static int
70e41ade 9070i386_immediate (char *imm_start)
252b5132
RH
9071{
9072 char *save_input_line_pointer;
f3c180ae 9073 char *gotfree_input_line;
252b5132 9074 segT exp_seg = 0;
47926f60 9075 expressionS *exp;
40fb9820
L
9076 i386_operand_type types;
9077
0dfbf9d7 9078 operand_type_set (&types, ~0);
252b5132
RH
9079
9080 if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
9081 {
31b2323c
L
9082 as_bad (_("at most %d immediate operands are allowed"),
9083 MAX_IMMEDIATE_OPERANDS);
252b5132
RH
9084 return 0;
9085 }
9086
9087 exp = &im_expressions[i.imm_operands++];
520dc8e8 9088 i.op[this_operand].imms = exp;
252b5132
RH
9089
9090 if (is_space_char (*imm_start))
9091 ++imm_start;
9092
9093 save_input_line_pointer = input_line_pointer;
9094 input_line_pointer = imm_start;
9095
d258b828 9096 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
9097 if (gotfree_input_line)
9098 input_line_pointer = gotfree_input_line;
252b5132
RH
9099
9100 exp_seg = expression (exp);
9101
83183c0c 9102 SKIP_WHITESPACE ();
43234a1e
L
9103
9104 /* Handle vector operations. */
9105 if (*input_line_pointer == '{')
9106 {
9107 input_line_pointer = check_VecOperations (input_line_pointer,
9108 NULL);
9109 if (input_line_pointer == NULL)
9110 return 0;
9111 }
9112
252b5132 9113 if (*input_line_pointer)
f3c180ae 9114 as_bad (_("junk `%s' after expression"), input_line_pointer);
252b5132
RH
9115
9116 input_line_pointer = save_input_line_pointer;
f3c180ae 9117 if (gotfree_input_line)
ee86248c
JB
9118 {
9119 free (gotfree_input_line);
9120
9121 if (exp->X_op == O_constant || exp->X_op == O_register)
9122 exp->X_op = O_illegal;
9123 }
9124
9125 return i386_finalize_immediate (exp_seg, exp, types, imm_start);
9126}
252b5132 9127
ee86248c
JB
9128static int
9129i386_finalize_immediate (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
9130 i386_operand_type types, const char *imm_start)
9131{
9132 if (exp->X_op == O_absent || exp->X_op == O_illegal || exp->X_op == O_big)
252b5132 9133 {
313c53d1
L
9134 if (imm_start)
9135 as_bad (_("missing or invalid immediate expression `%s'"),
9136 imm_start);
3992d3b7 9137 return 0;
252b5132 9138 }
3e73aa7c 9139 else if (exp->X_op == O_constant)
252b5132 9140 {
47926f60 9141 /* Size it properly later. */
40fb9820 9142 i.types[this_operand].bitfield.imm64 = 1;
13f864ae
L
9143 /* If not 64bit, sign extend val. */
9144 if (flag_code != CODE_64BIT
4eed87de
AM
9145 && (exp->X_add_number & ~(((addressT) 2 << 31) - 1)) == 0)
9146 exp->X_add_number
9147 = (exp->X_add_number ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
252b5132 9148 }
4c63da97 9149#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
f86103b7 9150 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
31312f95 9151 && exp_seg != absolute_section
47926f60 9152 && exp_seg != text_section
24eab124
AM
9153 && exp_seg != data_section
9154 && exp_seg != bss_section
9155 && exp_seg != undefined_section
f86103b7 9156 && !bfd_is_com_section (exp_seg))
252b5132 9157 {
d0b47220 9158 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
252b5132
RH
9159 return 0;
9160 }
9161#endif
a841bdf5 9162 else if (!intel_syntax && exp_seg == reg_section)
bb8f5920 9163 {
313c53d1
L
9164 if (imm_start)
9165 as_bad (_("illegal immediate register operand %s"), imm_start);
bb8f5920
L
9166 return 0;
9167 }
252b5132
RH
9168 else
9169 {
9170 /* This is an address. The size of the address will be
24eab124 9171 determined later, depending on destination register,
3e73aa7c 9172 suffix, or the default for the section. */
40fb9820
L
9173 i.types[this_operand].bitfield.imm8 = 1;
9174 i.types[this_operand].bitfield.imm16 = 1;
9175 i.types[this_operand].bitfield.imm32 = 1;
9176 i.types[this_operand].bitfield.imm32s = 1;
9177 i.types[this_operand].bitfield.imm64 = 1;
c6fb90c8
L
9178 i.types[this_operand] = operand_type_and (i.types[this_operand],
9179 types);
252b5132
RH
9180 }
9181
9182 return 1;
9183}
9184
551c1ca1 9185static char *
e3bb37b5 9186i386_scale (char *scale)
252b5132 9187{
551c1ca1
AM
9188 offsetT val;
9189 char *save = input_line_pointer;
252b5132 9190
551c1ca1
AM
9191 input_line_pointer = scale;
9192 val = get_absolute_expression ();
9193
9194 switch (val)
252b5132 9195 {
551c1ca1 9196 case 1:
252b5132
RH
9197 i.log2_scale_factor = 0;
9198 break;
551c1ca1 9199 case 2:
252b5132
RH
9200 i.log2_scale_factor = 1;
9201 break;
551c1ca1 9202 case 4:
252b5132
RH
9203 i.log2_scale_factor = 2;
9204 break;
551c1ca1 9205 case 8:
252b5132
RH
9206 i.log2_scale_factor = 3;
9207 break;
9208 default:
a724f0f4
JB
9209 {
9210 char sep = *input_line_pointer;
9211
9212 *input_line_pointer = '\0';
9213 as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
9214 scale);
9215 *input_line_pointer = sep;
9216 input_line_pointer = save;
9217 return NULL;
9218 }
252b5132 9219 }
29b0f896 9220 if (i.log2_scale_factor != 0 && i.index_reg == 0)
252b5132
RH
9221 {
9222 as_warn (_("scale factor of %d without an index register"),
24eab124 9223 1 << i.log2_scale_factor);
252b5132 9224 i.log2_scale_factor = 0;
252b5132 9225 }
551c1ca1
AM
9226 scale = input_line_pointer;
9227 input_line_pointer = save;
9228 return scale;
252b5132
RH
9229}
9230
252b5132 9231static int
e3bb37b5 9232i386_displacement (char *disp_start, char *disp_end)
252b5132 9233{
29b0f896 9234 expressionS *exp;
252b5132
RH
9235 segT exp_seg = 0;
9236 char *save_input_line_pointer;
f3c180ae 9237 char *gotfree_input_line;
40fb9820
L
9238 int override;
9239 i386_operand_type bigdisp, types = anydisp;
3992d3b7 9240 int ret;
252b5132 9241
31b2323c
L
9242 if (i.disp_operands == MAX_MEMORY_OPERANDS)
9243 {
9244 as_bad (_("at most %d displacement operands are allowed"),
9245 MAX_MEMORY_OPERANDS);
9246 return 0;
9247 }
9248
0dfbf9d7 9249 operand_type_set (&bigdisp, 0);
40fb9820
L
9250 if ((i.types[this_operand].bitfield.jumpabsolute)
9251 || (!current_templates->start->opcode_modifier.jump
9252 && !current_templates->start->opcode_modifier.jumpdword))
e05278af 9253 {
40fb9820 9254 bigdisp.bitfield.disp32 = 1;
e05278af 9255 override = (i.prefix[ADDR_PREFIX] != 0);
40fb9820
L
9256 if (flag_code == CODE_64BIT)
9257 {
9258 if (!override)
9259 {
9260 bigdisp.bitfield.disp32s = 1;
9261 bigdisp.bitfield.disp64 = 1;
9262 }
9263 }
9264 else if ((flag_code == CODE_16BIT) ^ override)
9265 {
9266 bigdisp.bitfield.disp32 = 0;
9267 bigdisp.bitfield.disp16 = 1;
9268 }
e05278af
JB
9269 }
9270 else
9271 {
9272 /* For PC-relative branches, the width of the displacement
9273 is dependent upon data size, not address size. */
e05278af 9274 override = (i.prefix[DATA_PREFIX] != 0);
40fb9820
L
9275 if (flag_code == CODE_64BIT)
9276 {
9277 if (override || i.suffix == WORD_MNEM_SUFFIX)
9278 bigdisp.bitfield.disp16 = 1;
9279 else
9280 {
9281 bigdisp.bitfield.disp32 = 1;
9282 bigdisp.bitfield.disp32s = 1;
9283 }
9284 }
9285 else
e05278af
JB
9286 {
9287 if (!override)
9288 override = (i.suffix == (flag_code != CODE_16BIT
9289 ? WORD_MNEM_SUFFIX
9290 : LONG_MNEM_SUFFIX));
40fb9820
L
9291 bigdisp.bitfield.disp32 = 1;
9292 if ((flag_code == CODE_16BIT) ^ override)
9293 {
9294 bigdisp.bitfield.disp32 = 0;
9295 bigdisp.bitfield.disp16 = 1;
9296 }
e05278af 9297 }
e05278af 9298 }
c6fb90c8
L
9299 i.types[this_operand] = operand_type_or (i.types[this_operand],
9300 bigdisp);
252b5132
RH
9301
9302 exp = &disp_expressions[i.disp_operands];
520dc8e8 9303 i.op[this_operand].disps = exp;
252b5132
RH
9304 i.disp_operands++;
9305 save_input_line_pointer = input_line_pointer;
9306 input_line_pointer = disp_start;
9307 END_STRING_AND_SAVE (disp_end);
9308
9309#ifndef GCC_ASM_O_HACK
9310#define GCC_ASM_O_HACK 0
9311#endif
9312#if GCC_ASM_O_HACK
9313 END_STRING_AND_SAVE (disp_end + 1);
40fb9820 9314 if (i.types[this_operand].bitfield.baseIndex
24eab124 9315 && displacement_string_end[-1] == '+')
252b5132
RH
9316 {
9317 /* This hack is to avoid a warning when using the "o"
24eab124
AM
9318 constraint within gcc asm statements.
9319 For instance:
9320
9321 #define _set_tssldt_desc(n,addr,limit,type) \
9322 __asm__ __volatile__ ( \
9323 "movw %w2,%0\n\t" \
9324 "movw %w1,2+%0\n\t" \
9325 "rorl $16,%1\n\t" \
9326 "movb %b1,4+%0\n\t" \
9327 "movb %4,5+%0\n\t" \
9328 "movb $0,6+%0\n\t" \
9329 "movb %h1,7+%0\n\t" \
9330 "rorl $16,%1" \
9331 : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
9332
9333 This works great except that the output assembler ends
9334 up looking a bit weird if it turns out that there is
9335 no offset. You end up producing code that looks like:
9336
9337 #APP
9338 movw $235,(%eax)
9339 movw %dx,2+(%eax)
9340 rorl $16,%edx
9341 movb %dl,4+(%eax)
9342 movb $137,5+(%eax)
9343 movb $0,6+(%eax)
9344 movb %dh,7+(%eax)
9345 rorl $16,%edx
9346 #NO_APP
9347
47926f60 9348 So here we provide the missing zero. */
24eab124
AM
9349
9350 *displacement_string_end = '0';
252b5132
RH
9351 }
9352#endif
d258b828 9353 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
9354 if (gotfree_input_line)
9355 input_line_pointer = gotfree_input_line;
252b5132 9356
24eab124 9357 exp_seg = expression (exp);
252b5132 9358
636c26b0
AM
9359 SKIP_WHITESPACE ();
9360 if (*input_line_pointer)
9361 as_bad (_("junk `%s' after expression"), input_line_pointer);
9362#if GCC_ASM_O_HACK
9363 RESTORE_END_STRING (disp_end + 1);
9364#endif
636c26b0 9365 input_line_pointer = save_input_line_pointer;
636c26b0 9366 if (gotfree_input_line)
ee86248c
JB
9367 {
9368 free (gotfree_input_line);
9369
9370 if (exp->X_op == O_constant || exp->X_op == O_register)
9371 exp->X_op = O_illegal;
9372 }
9373
9374 ret = i386_finalize_displacement (exp_seg, exp, types, disp_start);
9375
9376 RESTORE_END_STRING (disp_end);
9377
9378 return ret;
9379}
9380
9381static int
9382i386_finalize_displacement (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
9383 i386_operand_type types, const char *disp_start)
9384{
9385 i386_operand_type bigdisp;
9386 int ret = 1;
636c26b0 9387
24eab124
AM
9388 /* We do this to make sure that the section symbol is in
9389 the symbol table. We will ultimately change the relocation
47926f60 9390 to be relative to the beginning of the section. */
1ae12ab7 9391 if (i.reloc[this_operand] == BFD_RELOC_386_GOTOFF
d6ab8113
JB
9392 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
9393 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
24eab124 9394 {
636c26b0 9395 if (exp->X_op != O_symbol)
3992d3b7 9396 goto inv_disp;
636c26b0 9397
e5cb08ac 9398 if (S_IS_LOCAL (exp->X_add_symbol)
c64efb4b
L
9399 && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section
9400 && S_GET_SEGMENT (exp->X_add_symbol) != expr_section)
24eab124 9401 section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
24eab124
AM
9402 exp->X_op = O_subtract;
9403 exp->X_op_symbol = GOT_symbol;
1ae12ab7 9404 if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
29b0f896 9405 i.reloc[this_operand] = BFD_RELOC_32_PCREL;
d6ab8113
JB
9406 else if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
9407 i.reloc[this_operand] = BFD_RELOC_64;
23df1078 9408 else
29b0f896 9409 i.reloc[this_operand] = BFD_RELOC_32;
24eab124 9410 }
252b5132 9411
3992d3b7
AM
9412 else if (exp->X_op == O_absent
9413 || exp->X_op == O_illegal
ee86248c 9414 || exp->X_op == O_big)
2daf4fd8 9415 {
3992d3b7
AM
9416 inv_disp:
9417 as_bad (_("missing or invalid displacement expression `%s'"),
2daf4fd8 9418 disp_start);
3992d3b7 9419 ret = 0;
2daf4fd8
AM
9420 }
9421
0e1147d9
L
9422 else if (flag_code == CODE_64BIT
9423 && !i.prefix[ADDR_PREFIX]
9424 && exp->X_op == O_constant)
9425 {
9426 /* Since displacement is signed extended to 64bit, don't allow
9427 disp32 and turn off disp32s if they are out of range. */
9428 i.types[this_operand].bitfield.disp32 = 0;
9429 if (!fits_in_signed_long (exp->X_add_number))
9430 {
9431 i.types[this_operand].bitfield.disp32s = 0;
9432 if (i.types[this_operand].bitfield.baseindex)
9433 {
9434 as_bad (_("0x%lx out range of signed 32bit displacement"),
9435 (long) exp->X_add_number);
9436 ret = 0;
9437 }
9438 }
9439 }
9440
4c63da97 9441#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
3992d3b7
AM
9442 else if (exp->X_op != O_constant
9443 && OUTPUT_FLAVOR == bfd_target_aout_flavour
9444 && exp_seg != absolute_section
9445 && exp_seg != text_section
9446 && exp_seg != data_section
9447 && exp_seg != bss_section
9448 && exp_seg != undefined_section
9449 && !bfd_is_com_section (exp_seg))
24eab124 9450 {
d0b47220 9451 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
3992d3b7 9452 ret = 0;
24eab124 9453 }
252b5132 9454#endif
3956db08 9455
40fb9820
L
9456 /* Check if this is a displacement only operand. */
9457 bigdisp = i.types[this_operand];
9458 bigdisp.bitfield.disp8 = 0;
9459 bigdisp.bitfield.disp16 = 0;
9460 bigdisp.bitfield.disp32 = 0;
9461 bigdisp.bitfield.disp32s = 0;
9462 bigdisp.bitfield.disp64 = 0;
0dfbf9d7 9463 if (operand_type_all_zero (&bigdisp))
c6fb90c8
L
9464 i.types[this_operand] = operand_type_and (i.types[this_operand],
9465 types);
3956db08 9466
3992d3b7 9467 return ret;
252b5132
RH
9468}
9469
2abc2bec
JB
9470/* Return the active addressing mode, taking address override and
9471 registers forming the address into consideration. Update the
9472 address override prefix if necessary. */
47926f60 9473
2abc2bec
JB
9474static enum flag_code
9475i386_addressing_mode (void)
252b5132 9476{
be05d201
L
9477 enum flag_code addr_mode;
9478
9479 if (i.prefix[ADDR_PREFIX])
9480 addr_mode = flag_code == CODE_32BIT ? CODE_16BIT : CODE_32BIT;
9481 else
9482 {
9483 addr_mode = flag_code;
9484
24eab124 9485#if INFER_ADDR_PREFIX
be05d201
L
9486 if (i.mem_operands == 0)
9487 {
9488 /* Infer address prefix from the first memory operand. */
9489 const reg_entry *addr_reg = i.base_reg;
9490
9491 if (addr_reg == NULL)
9492 addr_reg = i.index_reg;
eecb386c 9493
be05d201
L
9494 if (addr_reg)
9495 {
e968fc9b 9496 if (addr_reg->reg_type.bitfield.dword)
be05d201
L
9497 addr_mode = CODE_32BIT;
9498 else if (flag_code != CODE_64BIT
dc821c5f 9499 && addr_reg->reg_type.bitfield.word)
be05d201
L
9500 addr_mode = CODE_16BIT;
9501
9502 if (addr_mode != flag_code)
9503 {
9504 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
9505 i.prefixes += 1;
9506 /* Change the size of any displacement too. At most one
9507 of Disp16 or Disp32 is set.
9508 FIXME. There doesn't seem to be any real need for
9509 separate Disp16 and Disp32 flags. The same goes for
9510 Imm16 and Imm32. Removing them would probably clean
9511 up the code quite a lot. */
9512 if (flag_code != CODE_64BIT
9513 && (i.types[this_operand].bitfield.disp16
9514 || i.types[this_operand].bitfield.disp32))
9515 i.types[this_operand]
9516 = operand_type_xor (i.types[this_operand], disp16_32);
9517 }
9518 }
9519 }
24eab124 9520#endif
be05d201
L
9521 }
9522
2abc2bec
JB
9523 return addr_mode;
9524}
9525
9526/* Make sure the memory operand we've been dealt is valid.
9527 Return 1 on success, 0 on a failure. */
9528
9529static int
9530i386_index_check (const char *operand_string)
9531{
9532 const char *kind = "base/index";
9533 enum flag_code addr_mode = i386_addressing_mode ();
9534
fc0763e6
JB
9535 if (current_templates->start->opcode_modifier.isstring
9536 && !current_templates->start->opcode_modifier.immext
9537 && (current_templates->end[-1].opcode_modifier.isstring
9538 || i.mem_operands))
9539 {
9540 /* Memory operands of string insns are special in that they only allow
9541 a single register (rDI, rSI, or rBX) as their memory address. */
be05d201
L
9542 const reg_entry *expected_reg;
9543 static const char *di_si[][2] =
9544 {
9545 { "esi", "edi" },
9546 { "si", "di" },
9547 { "rsi", "rdi" }
9548 };
9549 static const char *bx[] = { "ebx", "bx", "rbx" };
fc0763e6
JB
9550
9551 kind = "string address";
9552
8325cc63 9553 if (current_templates->start->opcode_modifier.repprefixok)
fc0763e6
JB
9554 {
9555 i386_operand_type type = current_templates->end[-1].operand_types[0];
9556
9557 if (!type.bitfield.baseindex
9558 || ((!i.mem_operands != !intel_syntax)
9559 && current_templates->end[-1].operand_types[1]
9560 .bitfield.baseindex))
9561 type = current_templates->end[-1].operand_types[1];
be05d201
L
9562 expected_reg = hash_find (reg_hash,
9563 di_si[addr_mode][type.bitfield.esseg]);
9564
fc0763e6
JB
9565 }
9566 else
be05d201 9567 expected_reg = hash_find (reg_hash, bx[addr_mode]);
fc0763e6 9568
be05d201
L
9569 if (i.base_reg != expected_reg
9570 || i.index_reg
fc0763e6 9571 || operand_type_check (i.types[this_operand], disp))
fc0763e6 9572 {
be05d201
L
9573 /* The second memory operand must have the same size as
9574 the first one. */
9575 if (i.mem_operands
9576 && i.base_reg
9577 && !((addr_mode == CODE_64BIT
dc821c5f 9578 && i.base_reg->reg_type.bitfield.qword)
be05d201 9579 || (addr_mode == CODE_32BIT
dc821c5f
JB
9580 ? i.base_reg->reg_type.bitfield.dword
9581 : i.base_reg->reg_type.bitfield.word)))
be05d201
L
9582 goto bad_address;
9583
fc0763e6
JB
9584 as_warn (_("`%s' is not valid here (expected `%c%s%s%c')"),
9585 operand_string,
9586 intel_syntax ? '[' : '(',
9587 register_prefix,
be05d201 9588 expected_reg->reg_name,
fc0763e6 9589 intel_syntax ? ']' : ')');
be05d201 9590 return 1;
fc0763e6 9591 }
be05d201
L
9592 else
9593 return 1;
9594
9595bad_address:
9596 as_bad (_("`%s' is not a valid %s expression"),
9597 operand_string, kind);
9598 return 0;
3e73aa7c
JH
9599 }
9600 else
9601 {
be05d201
L
9602 if (addr_mode != CODE_16BIT)
9603 {
9604 /* 32-bit/64-bit checks. */
9605 if ((i.base_reg
e968fc9b
JB
9606 && ((addr_mode == CODE_64BIT
9607 ? !i.base_reg->reg_type.bitfield.qword
9608 : !i.base_reg->reg_type.bitfield.dword)
9609 || (i.index_reg && i.base_reg->reg_num == RegIP)
9610 || i.base_reg->reg_num == RegIZ))
be05d201 9611 || (i.index_reg
1b54b8d7
JB
9612 && !i.index_reg->reg_type.bitfield.xmmword
9613 && !i.index_reg->reg_type.bitfield.ymmword
9614 && !i.index_reg->reg_type.bitfield.zmmword
be05d201 9615 && ((addr_mode == CODE_64BIT
e968fc9b
JB
9616 ? !i.index_reg->reg_type.bitfield.qword
9617 : !i.index_reg->reg_type.bitfield.dword)
be05d201
L
9618 || !i.index_reg->reg_type.bitfield.baseindex)))
9619 goto bad_address;
8178be5b
JB
9620
9621 /* bndmk, bndldx, and bndstx have special restrictions. */
9622 if (current_templates->start->base_opcode == 0xf30f1b
9623 || (current_templates->start->base_opcode & ~1) == 0x0f1a)
9624 {
9625 /* They cannot use RIP-relative addressing. */
e968fc9b 9626 if (i.base_reg && i.base_reg->reg_num == RegIP)
8178be5b
JB
9627 {
9628 as_bad (_("`%s' cannot be used here"), operand_string);
9629 return 0;
9630 }
9631
9632 /* bndldx and bndstx ignore their scale factor. */
9633 if (current_templates->start->base_opcode != 0xf30f1b
9634 && i.log2_scale_factor)
9635 as_warn (_("register scaling is being ignored here"));
9636 }
be05d201
L
9637 }
9638 else
3e73aa7c 9639 {
be05d201 9640 /* 16-bit checks. */
3e73aa7c 9641 if ((i.base_reg
dc821c5f 9642 && (!i.base_reg->reg_type.bitfield.word
40fb9820 9643 || !i.base_reg->reg_type.bitfield.baseindex))
3e73aa7c 9644 || (i.index_reg
dc821c5f 9645 && (!i.index_reg->reg_type.bitfield.word
40fb9820 9646 || !i.index_reg->reg_type.bitfield.baseindex
29b0f896
AM
9647 || !(i.base_reg
9648 && i.base_reg->reg_num < 6
9649 && i.index_reg->reg_num >= 6
9650 && i.log2_scale_factor == 0))))
be05d201 9651 goto bad_address;
3e73aa7c
JH
9652 }
9653 }
be05d201 9654 return 1;
24eab124 9655}
252b5132 9656
43234a1e
L
9657/* Handle vector immediates. */
9658
9659static int
9660RC_SAE_immediate (const char *imm_start)
9661{
9662 unsigned int match_found, j;
9663 const char *pstr = imm_start;
9664 expressionS *exp;
9665
9666 if (*pstr != '{')
9667 return 0;
9668
9669 pstr++;
9670 match_found = 0;
9671 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); j++)
9672 {
9673 if (!strncmp (pstr, RC_NamesTable[j].name, RC_NamesTable[j].len))
9674 {
9675 if (!i.rounding)
9676 {
9677 rc_op.type = RC_NamesTable[j].type;
9678 rc_op.operand = this_operand;
9679 i.rounding = &rc_op;
9680 }
9681 else
9682 {
9683 as_bad (_("duplicated `%s'"), imm_start);
9684 return 0;
9685 }
9686 pstr += RC_NamesTable[j].len;
9687 match_found = 1;
9688 break;
9689 }
9690 }
9691 if (!match_found)
9692 return 0;
9693
9694 if (*pstr++ != '}')
9695 {
9696 as_bad (_("Missing '}': '%s'"), imm_start);
9697 return 0;
9698 }
9699 /* RC/SAE immediate string should contain nothing more. */;
9700 if (*pstr != 0)
9701 {
9702 as_bad (_("Junk after '}': '%s'"), imm_start);
9703 return 0;
9704 }
9705
9706 exp = &im_expressions[i.imm_operands++];
9707 i.op[this_operand].imms = exp;
9708
9709 exp->X_op = O_constant;
9710 exp->X_add_number = 0;
9711 exp->X_add_symbol = (symbolS *) 0;
9712 exp->X_op_symbol = (symbolS *) 0;
9713
9714 i.types[this_operand].bitfield.imm8 = 1;
9715 return 1;
9716}
9717
8325cc63
JB
9718/* Only string instructions can have a second memory operand, so
9719 reduce current_templates to just those if it contains any. */
9720static int
9721maybe_adjust_templates (void)
9722{
9723 const insn_template *t;
9724
9725 gas_assert (i.mem_operands == 1);
9726
9727 for (t = current_templates->start; t < current_templates->end; ++t)
9728 if (t->opcode_modifier.isstring)
9729 break;
9730
9731 if (t < current_templates->end)
9732 {
9733 static templates aux_templates;
9734 bfd_boolean recheck;
9735
9736 aux_templates.start = t;
9737 for (; t < current_templates->end; ++t)
9738 if (!t->opcode_modifier.isstring)
9739 break;
9740 aux_templates.end = t;
9741
9742 /* Determine whether to re-check the first memory operand. */
9743 recheck = (aux_templates.start != current_templates->start
9744 || t != current_templates->end);
9745
9746 current_templates = &aux_templates;
9747
9748 if (recheck)
9749 {
9750 i.mem_operands = 0;
9751 if (i.memop1_string != NULL
9752 && i386_index_check (i.memop1_string) == 0)
9753 return 0;
9754 i.mem_operands = 1;
9755 }
9756 }
9757
9758 return 1;
9759}
9760
fc0763e6 9761/* Parse OPERAND_STRING into the i386_insn structure I. Returns zero
47926f60 9762 on error. */
252b5132 9763
252b5132 9764static int
a7619375 9765i386_att_operand (char *operand_string)
252b5132 9766{
af6bdddf
AM
9767 const reg_entry *r;
9768 char *end_op;
24eab124 9769 char *op_string = operand_string;
252b5132 9770
24eab124 9771 if (is_space_char (*op_string))
252b5132
RH
9772 ++op_string;
9773
24eab124 9774 /* We check for an absolute prefix (differentiating,
47926f60 9775 for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
24eab124
AM
9776 if (*op_string == ABSOLUTE_PREFIX)
9777 {
9778 ++op_string;
9779 if (is_space_char (*op_string))
9780 ++op_string;
40fb9820 9781 i.types[this_operand].bitfield.jumpabsolute = 1;
24eab124 9782 }
252b5132 9783
47926f60 9784 /* Check if operand is a register. */
4d1bb795 9785 if ((r = parse_register (op_string, &end_op)) != NULL)
24eab124 9786 {
40fb9820
L
9787 i386_operand_type temp;
9788
24eab124
AM
9789 /* Check for a segment override by searching for ':' after a
9790 segment register. */
9791 op_string = end_op;
9792 if (is_space_char (*op_string))
9793 ++op_string;
40fb9820
L
9794 if (*op_string == ':'
9795 && (r->reg_type.bitfield.sreg2
9796 || r->reg_type.bitfield.sreg3))
24eab124
AM
9797 {
9798 switch (r->reg_num)
9799 {
9800 case 0:
9801 i.seg[i.mem_operands] = &es;
9802 break;
9803 case 1:
9804 i.seg[i.mem_operands] = &cs;
9805 break;
9806 case 2:
9807 i.seg[i.mem_operands] = &ss;
9808 break;
9809 case 3:
9810 i.seg[i.mem_operands] = &ds;
9811 break;
9812 case 4:
9813 i.seg[i.mem_operands] = &fs;
9814 break;
9815 case 5:
9816 i.seg[i.mem_operands] = &gs;
9817 break;
9818 }
252b5132 9819
24eab124 9820 /* Skip the ':' and whitespace. */
252b5132
RH
9821 ++op_string;
9822 if (is_space_char (*op_string))
24eab124 9823 ++op_string;
252b5132 9824
24eab124
AM
9825 if (!is_digit_char (*op_string)
9826 && !is_identifier_char (*op_string)
9827 && *op_string != '('
9828 && *op_string != ABSOLUTE_PREFIX)
9829 {
9830 as_bad (_("bad memory operand `%s'"), op_string);
9831 return 0;
9832 }
47926f60 9833 /* Handle case of %es:*foo. */
24eab124
AM
9834 if (*op_string == ABSOLUTE_PREFIX)
9835 {
9836 ++op_string;
9837 if (is_space_char (*op_string))
9838 ++op_string;
40fb9820 9839 i.types[this_operand].bitfield.jumpabsolute = 1;
24eab124
AM
9840 }
9841 goto do_memory_reference;
9842 }
43234a1e
L
9843
9844 /* Handle vector operations. */
9845 if (*op_string == '{')
9846 {
9847 op_string = check_VecOperations (op_string, NULL);
9848 if (op_string == NULL)
9849 return 0;
9850 }
9851
24eab124
AM
9852 if (*op_string)
9853 {
d0b47220 9854 as_bad (_("junk `%s' after register"), op_string);
24eab124
AM
9855 return 0;
9856 }
40fb9820
L
9857 temp = r->reg_type;
9858 temp.bitfield.baseindex = 0;
c6fb90c8
L
9859 i.types[this_operand] = operand_type_or (i.types[this_operand],
9860 temp);
7d5e4556 9861 i.types[this_operand].bitfield.unspecified = 0;
520dc8e8 9862 i.op[this_operand].regs = r;
24eab124
AM
9863 i.reg_operands++;
9864 }
af6bdddf
AM
9865 else if (*op_string == REGISTER_PREFIX)
9866 {
9867 as_bad (_("bad register name `%s'"), op_string);
9868 return 0;
9869 }
24eab124 9870 else if (*op_string == IMMEDIATE_PREFIX)
ce8a8b2f 9871 {
24eab124 9872 ++op_string;
40fb9820 9873 if (i.types[this_operand].bitfield.jumpabsolute)
24eab124 9874 {
d0b47220 9875 as_bad (_("immediate operand illegal with absolute jump"));
24eab124
AM
9876 return 0;
9877 }
9878 if (!i386_immediate (op_string))
9879 return 0;
9880 }
43234a1e
L
9881 else if (RC_SAE_immediate (operand_string))
9882 {
9883 /* If it is a RC or SAE immediate, do nothing. */
9884 ;
9885 }
24eab124
AM
9886 else if (is_digit_char (*op_string)
9887 || is_identifier_char (*op_string)
d02603dc 9888 || *op_string == '"'
e5cb08ac 9889 || *op_string == '(')
24eab124 9890 {
47926f60 9891 /* This is a memory reference of some sort. */
af6bdddf 9892 char *base_string;
252b5132 9893
47926f60 9894 /* Start and end of displacement string expression (if found). */
eecb386c
AM
9895 char *displacement_string_start;
9896 char *displacement_string_end;
43234a1e 9897 char *vop_start;
252b5132 9898
24eab124 9899 do_memory_reference:
8325cc63
JB
9900 if (i.mem_operands == 1 && !maybe_adjust_templates ())
9901 return 0;
24eab124 9902 if ((i.mem_operands == 1
40fb9820 9903 && !current_templates->start->opcode_modifier.isstring)
24eab124
AM
9904 || i.mem_operands == 2)
9905 {
9906 as_bad (_("too many memory references for `%s'"),
9907 current_templates->start->name);
9908 return 0;
9909 }
252b5132 9910
24eab124
AM
9911 /* Check for base index form. We detect the base index form by
9912 looking for an ')' at the end of the operand, searching
9913 for the '(' matching it, and finding a REGISTER_PREFIX or ','
9914 after the '('. */
af6bdddf 9915 base_string = op_string + strlen (op_string);
c3332e24 9916
43234a1e
L
9917 /* Handle vector operations. */
9918 vop_start = strchr (op_string, '{');
9919 if (vop_start && vop_start < base_string)
9920 {
9921 if (check_VecOperations (vop_start, base_string) == NULL)
9922 return 0;
9923 base_string = vop_start;
9924 }
9925
af6bdddf
AM
9926 --base_string;
9927 if (is_space_char (*base_string))
9928 --base_string;
252b5132 9929
47926f60 9930 /* If we only have a displacement, set-up for it to be parsed later. */
af6bdddf
AM
9931 displacement_string_start = op_string;
9932 displacement_string_end = base_string + 1;
252b5132 9933
24eab124
AM
9934 if (*base_string == ')')
9935 {
af6bdddf 9936 char *temp_string;
24eab124
AM
9937 unsigned int parens_balanced = 1;
9938 /* We've already checked that the number of left & right ()'s are
47926f60 9939 equal, so this loop will not be infinite. */
24eab124
AM
9940 do
9941 {
9942 base_string--;
9943 if (*base_string == ')')
9944 parens_balanced++;
9945 if (*base_string == '(')
9946 parens_balanced--;
9947 }
9948 while (parens_balanced);
c3332e24 9949
af6bdddf 9950 temp_string = base_string;
c3332e24 9951
24eab124 9952 /* Skip past '(' and whitespace. */
252b5132
RH
9953 ++base_string;
9954 if (is_space_char (*base_string))
24eab124 9955 ++base_string;
252b5132 9956
af6bdddf 9957 if (*base_string == ','
4eed87de
AM
9958 || ((i.base_reg = parse_register (base_string, &end_op))
9959 != NULL))
252b5132 9960 {
af6bdddf 9961 displacement_string_end = temp_string;
252b5132 9962
40fb9820 9963 i.types[this_operand].bitfield.baseindex = 1;
252b5132 9964
af6bdddf 9965 if (i.base_reg)
24eab124 9966 {
24eab124
AM
9967 base_string = end_op;
9968 if (is_space_char (*base_string))
9969 ++base_string;
af6bdddf
AM
9970 }
9971
9972 /* There may be an index reg or scale factor here. */
9973 if (*base_string == ',')
9974 {
9975 ++base_string;
9976 if (is_space_char (*base_string))
9977 ++base_string;
9978
4eed87de
AM
9979 if ((i.index_reg = parse_register (base_string, &end_op))
9980 != NULL)
24eab124 9981 {
af6bdddf 9982 base_string = end_op;
24eab124
AM
9983 if (is_space_char (*base_string))
9984 ++base_string;
af6bdddf
AM
9985 if (*base_string == ',')
9986 {
9987 ++base_string;
9988 if (is_space_char (*base_string))
9989 ++base_string;
9990 }
e5cb08ac 9991 else if (*base_string != ')')
af6bdddf 9992 {
4eed87de
AM
9993 as_bad (_("expecting `,' or `)' "
9994 "after index register in `%s'"),
af6bdddf
AM
9995 operand_string);
9996 return 0;
9997 }
24eab124 9998 }
af6bdddf 9999 else if (*base_string == REGISTER_PREFIX)
24eab124 10000 {
f76bf5e0
L
10001 end_op = strchr (base_string, ',');
10002 if (end_op)
10003 *end_op = '\0';
af6bdddf 10004 as_bad (_("bad register name `%s'"), base_string);
24eab124
AM
10005 return 0;
10006 }
252b5132 10007
47926f60 10008 /* Check for scale factor. */
551c1ca1 10009 if (*base_string != ')')
af6bdddf 10010 {
551c1ca1
AM
10011 char *end_scale = i386_scale (base_string);
10012
10013 if (!end_scale)
af6bdddf 10014 return 0;
24eab124 10015
551c1ca1 10016 base_string = end_scale;
af6bdddf
AM
10017 if (is_space_char (*base_string))
10018 ++base_string;
10019 if (*base_string != ')')
10020 {
4eed87de
AM
10021 as_bad (_("expecting `)' "
10022 "after scale factor in `%s'"),
af6bdddf
AM
10023 operand_string);
10024 return 0;
10025 }
10026 }
10027 else if (!i.index_reg)
24eab124 10028 {
4eed87de
AM
10029 as_bad (_("expecting index register or scale factor "
10030 "after `,'; got '%c'"),
af6bdddf 10031 *base_string);
24eab124
AM
10032 return 0;
10033 }
10034 }
af6bdddf 10035 else if (*base_string != ')')
24eab124 10036 {
4eed87de
AM
10037 as_bad (_("expecting `,' or `)' "
10038 "after base register in `%s'"),
af6bdddf 10039 operand_string);
24eab124
AM
10040 return 0;
10041 }
c3332e24 10042 }
af6bdddf 10043 else if (*base_string == REGISTER_PREFIX)
c3332e24 10044 {
f76bf5e0
L
10045 end_op = strchr (base_string, ',');
10046 if (end_op)
10047 *end_op = '\0';
af6bdddf 10048 as_bad (_("bad register name `%s'"), base_string);
24eab124 10049 return 0;
c3332e24 10050 }
24eab124
AM
10051 }
10052
10053 /* If there's an expression beginning the operand, parse it,
10054 assuming displacement_string_start and
10055 displacement_string_end are meaningful. */
10056 if (displacement_string_start != displacement_string_end)
10057 {
10058 if (!i386_displacement (displacement_string_start,
10059 displacement_string_end))
10060 return 0;
10061 }
10062
10063 /* Special case for (%dx) while doing input/output op. */
10064 if (i.base_reg
2fb5be8d 10065 && i.base_reg->reg_type.bitfield.inoutportreg
24eab124
AM
10066 && i.index_reg == 0
10067 && i.log2_scale_factor == 0
10068 && i.seg[i.mem_operands] == 0
40fb9820 10069 && !operand_type_check (i.types[this_operand], disp))
24eab124 10070 {
2fb5be8d 10071 i.types[this_operand] = i.base_reg->reg_type;
24eab124
AM
10072 return 1;
10073 }
10074
eecb386c
AM
10075 if (i386_index_check (operand_string) == 0)
10076 return 0;
c48dadc9 10077 i.flags[this_operand] |= Operand_Mem;
8325cc63
JB
10078 if (i.mem_operands == 0)
10079 i.memop1_string = xstrdup (operand_string);
24eab124
AM
10080 i.mem_operands++;
10081 }
10082 else
ce8a8b2f
AM
10083 {
10084 /* It's not a memory operand; argh! */
24eab124
AM
10085 as_bad (_("invalid char %s beginning operand %d `%s'"),
10086 output_invalid (*op_string),
10087 this_operand + 1,
10088 op_string);
10089 return 0;
10090 }
47926f60 10091 return 1; /* Normal return. */
252b5132
RH
10092}
10093\f
fa94de6b
RM
10094/* Calculate the maximum variable size (i.e., excluding fr_fix)
10095 that an rs_machine_dependent frag may reach. */
10096
10097unsigned int
10098i386_frag_max_var (fragS *frag)
10099{
10100 /* The only relaxable frags are for jumps.
10101 Unconditional jumps can grow by 4 bytes and others by 5 bytes. */
10102 gas_assert (frag->fr_type == rs_machine_dependent);
10103 return TYPE_FROM_RELAX_STATE (frag->fr_subtype) == UNCOND_JUMP ? 4 : 5;
10104}
10105
b084df0b
L
10106#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
10107static int
8dcea932 10108elf_symbol_resolved_in_segment_p (symbolS *fr_symbol, offsetT fr_var)
b084df0b
L
10109{
10110 /* STT_GNU_IFUNC symbol must go through PLT. */
10111 if ((symbol_get_bfdsym (fr_symbol)->flags
10112 & BSF_GNU_INDIRECT_FUNCTION) != 0)
10113 return 0;
10114
10115 if (!S_IS_EXTERNAL (fr_symbol))
10116 /* Symbol may be weak or local. */
10117 return !S_IS_WEAK (fr_symbol);
10118
8dcea932
L
10119 /* Global symbols with non-default visibility can't be preempted. */
10120 if (ELF_ST_VISIBILITY (S_GET_OTHER (fr_symbol)) != STV_DEFAULT)
10121 return 1;
10122
10123 if (fr_var != NO_RELOC)
10124 switch ((enum bfd_reloc_code_real) fr_var)
10125 {
10126 case BFD_RELOC_386_PLT32:
10127 case BFD_RELOC_X86_64_PLT32:
33eaf5de 10128 /* Symbol with PLT relocation may be preempted. */
8dcea932
L
10129 return 0;
10130 default:
10131 abort ();
10132 }
10133
b084df0b
L
10134 /* Global symbols with default visibility in a shared library may be
10135 preempted by another definition. */
8dcea932 10136 return !shared;
b084df0b
L
10137}
10138#endif
10139
ee7fcc42
AM
10140/* md_estimate_size_before_relax()
10141
10142 Called just before relax() for rs_machine_dependent frags. The x86
10143 assembler uses these frags to handle variable size jump
10144 instructions.
10145
10146 Any symbol that is now undefined will not become defined.
10147 Return the correct fr_subtype in the frag.
10148 Return the initial "guess for variable size of frag" to caller.
10149 The guess is actually the growth beyond the fixed part. Whatever
10150 we do to grow the fixed or variable part contributes to our
10151 returned value. */
10152
252b5132 10153int
7016a5d5 10154md_estimate_size_before_relax (fragS *fragP, segT segment)
252b5132 10155{
252b5132 10156 /* We've already got fragP->fr_subtype right; all we have to do is
b98ef147
AM
10157 check for un-relaxable symbols. On an ELF system, we can't relax
10158 an externally visible symbol, because it may be overridden by a
10159 shared library. */
10160 if (S_GET_SEGMENT (fragP->fr_symbol) != segment
6d249963 10161#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 10162 || (IS_ELF
8dcea932
L
10163 && !elf_symbol_resolved_in_segment_p (fragP->fr_symbol,
10164 fragP->fr_var))
fbeb56a4
DK
10165#endif
10166#if defined (OBJ_COFF) && defined (TE_PE)
7ab9ffdd 10167 || (OUTPUT_FLAVOR == bfd_target_coff_flavour
fbeb56a4 10168 && S_IS_WEAK (fragP->fr_symbol))
b98ef147
AM
10169#endif
10170 )
252b5132 10171 {
b98ef147
AM
10172 /* Symbol is undefined in this segment, or we need to keep a
10173 reloc so that weak symbols can be overridden. */
10174 int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
f86103b7 10175 enum bfd_reloc_code_real reloc_type;
ee7fcc42
AM
10176 unsigned char *opcode;
10177 int old_fr_fix;
f6af82bd 10178
ee7fcc42 10179 if (fragP->fr_var != NO_RELOC)
1e9cc1c2 10180 reloc_type = (enum bfd_reloc_code_real) fragP->fr_var;
b98ef147 10181 else if (size == 2)
f6af82bd 10182 reloc_type = BFD_RELOC_16_PCREL;
bd7ab16b
L
10183#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
10184 else if (need_plt32_p (fragP->fr_symbol))
10185 reloc_type = BFD_RELOC_X86_64_PLT32;
10186#endif
f6af82bd
AM
10187 else
10188 reloc_type = BFD_RELOC_32_PCREL;
252b5132 10189
ee7fcc42
AM
10190 old_fr_fix = fragP->fr_fix;
10191 opcode = (unsigned char *) fragP->fr_opcode;
10192
fddf5b5b 10193 switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
252b5132 10194 {
fddf5b5b
AM
10195 case UNCOND_JUMP:
10196 /* Make jmp (0xeb) a (d)word displacement jump. */
47926f60 10197 opcode[0] = 0xe9;
252b5132 10198 fragP->fr_fix += size;
062cd5e7
AS
10199 fix_new (fragP, old_fr_fix, size,
10200 fragP->fr_symbol,
10201 fragP->fr_offset, 1,
10202 reloc_type);
252b5132
RH
10203 break;
10204
fddf5b5b 10205 case COND_JUMP86:
412167cb
AM
10206 if (size == 2
10207 && (!no_cond_jump_promotion || fragP->fr_var != NO_RELOC))
fddf5b5b
AM
10208 {
10209 /* Negate the condition, and branch past an
10210 unconditional jump. */
10211 opcode[0] ^= 1;
10212 opcode[1] = 3;
10213 /* Insert an unconditional jump. */
10214 opcode[2] = 0xe9;
10215 /* We added two extra opcode bytes, and have a two byte
10216 offset. */
10217 fragP->fr_fix += 2 + 2;
062cd5e7
AS
10218 fix_new (fragP, old_fr_fix + 2, 2,
10219 fragP->fr_symbol,
10220 fragP->fr_offset, 1,
10221 reloc_type);
fddf5b5b
AM
10222 break;
10223 }
10224 /* Fall through. */
10225
10226 case COND_JUMP:
412167cb
AM
10227 if (no_cond_jump_promotion && fragP->fr_var == NO_RELOC)
10228 {
3e02c1cc
AM
10229 fixS *fixP;
10230
412167cb 10231 fragP->fr_fix += 1;
3e02c1cc
AM
10232 fixP = fix_new (fragP, old_fr_fix, 1,
10233 fragP->fr_symbol,
10234 fragP->fr_offset, 1,
10235 BFD_RELOC_8_PCREL);
10236 fixP->fx_signed = 1;
412167cb
AM
10237 break;
10238 }
93c2a809 10239
24eab124 10240 /* This changes the byte-displacement jump 0x7N
fddf5b5b 10241 to the (d)word-displacement jump 0x0f,0x8N. */
252b5132 10242 opcode[1] = opcode[0] + 0x10;
f6af82bd 10243 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
47926f60
KH
10244 /* We've added an opcode byte. */
10245 fragP->fr_fix += 1 + size;
062cd5e7
AS
10246 fix_new (fragP, old_fr_fix + 1, size,
10247 fragP->fr_symbol,
10248 fragP->fr_offset, 1,
10249 reloc_type);
252b5132 10250 break;
fddf5b5b
AM
10251
10252 default:
10253 BAD_CASE (fragP->fr_subtype);
10254 break;
252b5132
RH
10255 }
10256 frag_wane (fragP);
ee7fcc42 10257 return fragP->fr_fix - old_fr_fix;
252b5132 10258 }
93c2a809 10259
93c2a809
AM
10260 /* Guess size depending on current relax state. Initially the relax
10261 state will correspond to a short jump and we return 1, because
10262 the variable part of the frag (the branch offset) is one byte
10263 long. However, we can relax a section more than once and in that
10264 case we must either set fr_subtype back to the unrelaxed state,
10265 or return the value for the appropriate branch. */
10266 return md_relax_table[fragP->fr_subtype].rlx_length;
ee7fcc42
AM
10267}
10268
47926f60
KH
10269/* Called after relax() is finished.
10270
10271 In: Address of frag.
10272 fr_type == rs_machine_dependent.
10273 fr_subtype is what the address relaxed to.
10274
10275 Out: Any fixSs and constants are set up.
10276 Caller will turn frag into a ".space 0". */
10277
252b5132 10278void
7016a5d5
TG
10279md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec ATTRIBUTE_UNUSED,
10280 fragS *fragP)
252b5132 10281{
29b0f896 10282 unsigned char *opcode;
252b5132 10283 unsigned char *where_to_put_displacement = NULL;
847f7ad4
AM
10284 offsetT target_address;
10285 offsetT opcode_address;
252b5132 10286 unsigned int extension = 0;
847f7ad4 10287 offsetT displacement_from_opcode_start;
252b5132
RH
10288
10289 opcode = (unsigned char *) fragP->fr_opcode;
10290
47926f60 10291 /* Address we want to reach in file space. */
252b5132 10292 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
252b5132 10293
47926f60 10294 /* Address opcode resides at in file space. */
252b5132
RH
10295 opcode_address = fragP->fr_address + fragP->fr_fix;
10296
47926f60 10297 /* Displacement from opcode start to fill into instruction. */
252b5132
RH
10298 displacement_from_opcode_start = target_address - opcode_address;
10299
fddf5b5b 10300 if ((fragP->fr_subtype & BIG) == 0)
252b5132 10301 {
47926f60
KH
10302 /* Don't have to change opcode. */
10303 extension = 1; /* 1 opcode + 1 displacement */
252b5132 10304 where_to_put_displacement = &opcode[1];
fddf5b5b
AM
10305 }
10306 else
10307 {
10308 if (no_cond_jump_promotion
10309 && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
4eed87de
AM
10310 as_warn_where (fragP->fr_file, fragP->fr_line,
10311 _("long jump required"));
252b5132 10312
fddf5b5b
AM
10313 switch (fragP->fr_subtype)
10314 {
10315 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
10316 extension = 4; /* 1 opcode + 4 displacement */
10317 opcode[0] = 0xe9;
10318 where_to_put_displacement = &opcode[1];
10319 break;
252b5132 10320
fddf5b5b
AM
10321 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
10322 extension = 2; /* 1 opcode + 2 displacement */
10323 opcode[0] = 0xe9;
10324 where_to_put_displacement = &opcode[1];
10325 break;
252b5132 10326
fddf5b5b
AM
10327 case ENCODE_RELAX_STATE (COND_JUMP, BIG):
10328 case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
10329 extension = 5; /* 2 opcode + 4 displacement */
10330 opcode[1] = opcode[0] + 0x10;
10331 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
10332 where_to_put_displacement = &opcode[2];
10333 break;
252b5132 10334
fddf5b5b
AM
10335 case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
10336 extension = 3; /* 2 opcode + 2 displacement */
10337 opcode[1] = opcode[0] + 0x10;
10338 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
10339 where_to_put_displacement = &opcode[2];
10340 break;
252b5132 10341
fddf5b5b
AM
10342 case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
10343 extension = 4;
10344 opcode[0] ^= 1;
10345 opcode[1] = 3;
10346 opcode[2] = 0xe9;
10347 where_to_put_displacement = &opcode[3];
10348 break;
10349
10350 default:
10351 BAD_CASE (fragP->fr_subtype);
10352 break;
10353 }
252b5132 10354 }
fddf5b5b 10355
7b81dfbb
AJ
10356 /* If size if less then four we are sure that the operand fits,
10357 but if it's 4, then it could be that the displacement is larger
10358 then -/+ 2GB. */
10359 if (DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype) == 4
10360 && object_64bit
10361 && ((addressT) (displacement_from_opcode_start - extension
4eed87de
AM
10362 + ((addressT) 1 << 31))
10363 > (((addressT) 2 << 31) - 1)))
7b81dfbb
AJ
10364 {
10365 as_bad_where (fragP->fr_file, fragP->fr_line,
10366 _("jump target out of range"));
10367 /* Make us emit 0. */
10368 displacement_from_opcode_start = extension;
10369 }
47926f60 10370 /* Now put displacement after opcode. */
252b5132
RH
10371 md_number_to_chars ((char *) where_to_put_displacement,
10372 (valueT) (displacement_from_opcode_start - extension),
fddf5b5b 10373 DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
252b5132
RH
10374 fragP->fr_fix += extension;
10375}
10376\f
7016a5d5 10377/* Apply a fixup (fixP) to segment data, once it has been determined
252b5132
RH
10378 by our caller that we have all the info we need to fix it up.
10379
7016a5d5
TG
10380 Parameter valP is the pointer to the value of the bits.
10381
252b5132
RH
10382 On the 386, immediates, displacements, and data pointers are all in
10383 the same (little-endian) format, so we don't need to care about which
10384 we are handling. */
10385
94f592af 10386void
7016a5d5 10387md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 10388{
94f592af 10389 char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
c6682705 10390 valueT value = *valP;
252b5132 10391
f86103b7 10392#if !defined (TE_Mach)
93382f6d
AM
10393 if (fixP->fx_pcrel)
10394 {
10395 switch (fixP->fx_r_type)
10396 {
5865bb77
ILT
10397 default:
10398 break;
10399
d6ab8113
JB
10400 case BFD_RELOC_64:
10401 fixP->fx_r_type = BFD_RELOC_64_PCREL;
10402 break;
93382f6d 10403 case BFD_RELOC_32:
ae8887b5 10404 case BFD_RELOC_X86_64_32S:
93382f6d
AM
10405 fixP->fx_r_type = BFD_RELOC_32_PCREL;
10406 break;
10407 case BFD_RELOC_16:
10408 fixP->fx_r_type = BFD_RELOC_16_PCREL;
10409 break;
10410 case BFD_RELOC_8:
10411 fixP->fx_r_type = BFD_RELOC_8_PCREL;
10412 break;
10413 }
10414 }
252b5132 10415
a161fe53 10416 if (fixP->fx_addsy != NULL
31312f95 10417 && (fixP->fx_r_type == BFD_RELOC_32_PCREL
d6ab8113 10418 || fixP->fx_r_type == BFD_RELOC_64_PCREL
31312f95 10419 || fixP->fx_r_type == BFD_RELOC_16_PCREL
d258b828 10420 || fixP->fx_r_type == BFD_RELOC_8_PCREL)
31312f95 10421 && !use_rela_relocations)
252b5132 10422 {
31312f95
AM
10423 /* This is a hack. There should be a better way to handle this.
10424 This covers for the fact that bfd_install_relocation will
10425 subtract the current location (for partial_inplace, PC relative
10426 relocations); see more below. */
252b5132 10427#ifndef OBJ_AOUT
718ddfc0 10428 if (IS_ELF
252b5132
RH
10429#ifdef TE_PE
10430 || OUTPUT_FLAVOR == bfd_target_coff_flavour
10431#endif
10432 )
10433 value += fixP->fx_where + fixP->fx_frag->fr_address;
10434#endif
10435#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 10436 if (IS_ELF)
252b5132 10437 {
6539b54b 10438 segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
2f66722d 10439
6539b54b 10440 if ((sym_seg == seg
2f66722d 10441 || (symbol_section_p (fixP->fx_addsy)
6539b54b 10442 && sym_seg != absolute_section))
af65af87 10443 && !generic_force_reloc (fixP))
2f66722d
AM
10444 {
10445 /* Yes, we add the values in twice. This is because
6539b54b
AM
10446 bfd_install_relocation subtracts them out again. I think
10447 bfd_install_relocation is broken, but I don't dare change
2f66722d
AM
10448 it. FIXME. */
10449 value += fixP->fx_where + fixP->fx_frag->fr_address;
10450 }
252b5132
RH
10451 }
10452#endif
10453#if defined (OBJ_COFF) && defined (TE_PE)
977cdf5a
NC
10454 /* For some reason, the PE format does not store a
10455 section address offset for a PC relative symbol. */
10456 if (S_GET_SEGMENT (fixP->fx_addsy) != seg
7be1c489 10457 || S_IS_WEAK (fixP->fx_addsy))
252b5132
RH
10458 value += md_pcrel_from (fixP);
10459#endif
10460 }
fbeb56a4 10461#if defined (OBJ_COFF) && defined (TE_PE)
f01c1a09
NC
10462 if (fixP->fx_addsy != NULL
10463 && S_IS_WEAK (fixP->fx_addsy)
10464 /* PR 16858: Do not modify weak function references. */
10465 && ! fixP->fx_pcrel)
fbeb56a4 10466 {
296a8689
NC
10467#if !defined (TE_PEP)
10468 /* For x86 PE weak function symbols are neither PC-relative
10469 nor do they set S_IS_FUNCTION. So the only reliable way
10470 to detect them is to check the flags of their containing
10471 section. */
10472 if (S_GET_SEGMENT (fixP->fx_addsy) != NULL
10473 && S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_CODE)
10474 ;
10475 else
10476#endif
fbeb56a4
DK
10477 value -= S_GET_VALUE (fixP->fx_addsy);
10478 }
10479#endif
252b5132
RH
10480
10481 /* Fix a few things - the dynamic linker expects certain values here,
0234cb7c 10482 and we must not disappoint it. */
252b5132 10483#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 10484 if (IS_ELF && fixP->fx_addsy)
47926f60
KH
10485 switch (fixP->fx_r_type)
10486 {
10487 case BFD_RELOC_386_PLT32:
3e73aa7c 10488 case BFD_RELOC_X86_64_PLT32:
47926f60
KH
10489 /* Make the jump instruction point to the address of the operand. At
10490 runtime we merely add the offset to the actual PLT entry. */
10491 value = -4;
10492 break;
31312f95 10493
13ae64f3
JJ
10494 case BFD_RELOC_386_TLS_GD:
10495 case BFD_RELOC_386_TLS_LDM:
13ae64f3 10496 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
10497 case BFD_RELOC_386_TLS_IE:
10498 case BFD_RELOC_386_TLS_GOTIE:
67a4f2b7 10499 case BFD_RELOC_386_TLS_GOTDESC:
bffbf940
JJ
10500 case BFD_RELOC_X86_64_TLSGD:
10501 case BFD_RELOC_X86_64_TLSLD:
10502 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7 10503 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
00f7efb6
JJ
10504 value = 0; /* Fully resolved at runtime. No addend. */
10505 /* Fallthrough */
10506 case BFD_RELOC_386_TLS_LE:
10507 case BFD_RELOC_386_TLS_LDO_32:
10508 case BFD_RELOC_386_TLS_LE_32:
10509 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 10510 case BFD_RELOC_X86_64_DTPOFF64:
00f7efb6 10511 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113 10512 case BFD_RELOC_X86_64_TPOFF64:
00f7efb6
JJ
10513 S_SET_THREAD_LOCAL (fixP->fx_addsy);
10514 break;
10515
67a4f2b7
AO
10516 case BFD_RELOC_386_TLS_DESC_CALL:
10517 case BFD_RELOC_X86_64_TLSDESC_CALL:
10518 value = 0; /* Fully resolved at runtime. No addend. */
10519 S_SET_THREAD_LOCAL (fixP->fx_addsy);
10520 fixP->fx_done = 0;
10521 return;
10522
47926f60
KH
10523 case BFD_RELOC_VTABLE_INHERIT:
10524 case BFD_RELOC_VTABLE_ENTRY:
10525 fixP->fx_done = 0;
94f592af 10526 return;
47926f60
KH
10527
10528 default:
10529 break;
10530 }
10531#endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) */
c6682705 10532 *valP = value;
f86103b7 10533#endif /* !defined (TE_Mach) */
3e73aa7c 10534
3e73aa7c 10535 /* Are we finished with this relocation now? */
c6682705 10536 if (fixP->fx_addsy == NULL)
3e73aa7c 10537 fixP->fx_done = 1;
fbeb56a4
DK
10538#if defined (OBJ_COFF) && defined (TE_PE)
10539 else if (fixP->fx_addsy != NULL && S_IS_WEAK (fixP->fx_addsy))
10540 {
10541 fixP->fx_done = 0;
10542 /* Remember value for tc_gen_reloc. */
10543 fixP->fx_addnumber = value;
10544 /* Clear out the frag for now. */
10545 value = 0;
10546 }
10547#endif
3e73aa7c
JH
10548 else if (use_rela_relocations)
10549 {
10550 fixP->fx_no_overflow = 1;
062cd5e7
AS
10551 /* Remember value for tc_gen_reloc. */
10552 fixP->fx_addnumber = value;
3e73aa7c
JH
10553 value = 0;
10554 }
f86103b7 10555
94f592af 10556 md_number_to_chars (p, value, fixP->fx_size);
252b5132 10557}
252b5132 10558\f
6d4af3c2 10559const char *
499ac353 10560md_atof (int type, char *litP, int *sizeP)
252b5132 10561{
499ac353
NC
10562 /* This outputs the LITTLENUMs in REVERSE order;
10563 in accord with the bigendian 386. */
10564 return ieee_md_atof (type, litP, sizeP, FALSE);
252b5132
RH
10565}
10566\f
2d545b82 10567static char output_invalid_buf[sizeof (unsigned char) * 2 + 6];
252b5132 10568
252b5132 10569static char *
e3bb37b5 10570output_invalid (int c)
252b5132 10571{
3882b010 10572 if (ISPRINT (c))
f9f21a03
L
10573 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
10574 "'%c'", c);
252b5132 10575 else
f9f21a03 10576 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
2d545b82 10577 "(0x%x)", (unsigned char) c);
252b5132
RH
10578 return output_invalid_buf;
10579}
10580
af6bdddf 10581/* REG_STRING starts *before* REGISTER_PREFIX. */
252b5132
RH
10582
10583static const reg_entry *
4d1bb795 10584parse_real_register (char *reg_string, char **end_op)
252b5132 10585{
af6bdddf
AM
10586 char *s = reg_string;
10587 char *p;
252b5132
RH
10588 char reg_name_given[MAX_REG_NAME_SIZE + 1];
10589 const reg_entry *r;
10590
10591 /* Skip possible REGISTER_PREFIX and possible whitespace. */
10592 if (*s == REGISTER_PREFIX)
10593 ++s;
10594
10595 if (is_space_char (*s))
10596 ++s;
10597
10598 p = reg_name_given;
af6bdddf 10599 while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
252b5132
RH
10600 {
10601 if (p >= reg_name_given + MAX_REG_NAME_SIZE)
af6bdddf
AM
10602 return (const reg_entry *) NULL;
10603 s++;
252b5132
RH
10604 }
10605
6588847e
DN
10606 /* For naked regs, make sure that we are not dealing with an identifier.
10607 This prevents confusing an identifier like `eax_var' with register
10608 `eax'. */
10609 if (allow_naked_reg && identifier_chars[(unsigned char) *s])
10610 return (const reg_entry *) NULL;
10611
af6bdddf 10612 *end_op = s;
252b5132
RH
10613
10614 r = (const reg_entry *) hash_find (reg_hash, reg_name_given);
10615
5f47d35b 10616 /* Handle floating point regs, allowing spaces in the (i) part. */
47926f60 10617 if (r == i386_regtab /* %st is first entry of table */)
5f47d35b 10618 {
0e0eea78
JB
10619 if (!cpu_arch_flags.bitfield.cpu8087
10620 && !cpu_arch_flags.bitfield.cpu287
10621 && !cpu_arch_flags.bitfield.cpu387)
10622 return (const reg_entry *) NULL;
10623
5f47d35b
AM
10624 if (is_space_char (*s))
10625 ++s;
10626 if (*s == '(')
10627 {
af6bdddf 10628 ++s;
5f47d35b
AM
10629 if (is_space_char (*s))
10630 ++s;
10631 if (*s >= '0' && *s <= '7')
10632 {
db557034 10633 int fpr = *s - '0';
af6bdddf 10634 ++s;
5f47d35b
AM
10635 if (is_space_char (*s))
10636 ++s;
10637 if (*s == ')')
10638 {
10639 *end_op = s + 1;
1e9cc1c2 10640 r = (const reg_entry *) hash_find (reg_hash, "st(0)");
db557034
AM
10641 know (r);
10642 return r + fpr;
5f47d35b 10643 }
5f47d35b 10644 }
47926f60 10645 /* We have "%st(" then garbage. */
5f47d35b
AM
10646 return (const reg_entry *) NULL;
10647 }
10648 }
10649
a60de03c
JB
10650 if (r == NULL || allow_pseudo_reg)
10651 return r;
10652
0dfbf9d7 10653 if (operand_type_all_zero (&r->reg_type))
a60de03c
JB
10654 return (const reg_entry *) NULL;
10655
dc821c5f 10656 if ((r->reg_type.bitfield.dword
192dc9c6
JB
10657 || r->reg_type.bitfield.sreg3
10658 || r->reg_type.bitfield.control
10659 || r->reg_type.bitfield.debug
10660 || r->reg_type.bitfield.test)
10661 && !cpu_arch_flags.bitfield.cpui386)
10662 return (const reg_entry *) NULL;
10663
6e041cf4 10664 if (r->reg_type.bitfield.regmmx && !cpu_arch_flags.bitfield.cpummx)
192dc9c6
JB
10665 return (const reg_entry *) NULL;
10666
6e041cf4
JB
10667 if (!cpu_arch_flags.bitfield.cpuavx512f)
10668 {
10669 if (r->reg_type.bitfield.zmmword || r->reg_type.bitfield.regmask)
10670 return (const reg_entry *) NULL;
40f12533 10671
6e041cf4
JB
10672 if (!cpu_arch_flags.bitfield.cpuavx)
10673 {
10674 if (r->reg_type.bitfield.ymmword)
10675 return (const reg_entry *) NULL;
1848e567 10676
6e041cf4
JB
10677 if (!cpu_arch_flags.bitfield.cpusse && r->reg_type.bitfield.xmmword)
10678 return (const reg_entry *) NULL;
10679 }
10680 }
43234a1e 10681
1adf7f56
JB
10682 if (r->reg_type.bitfield.regbnd && !cpu_arch_flags.bitfield.cpumpx)
10683 return (const reg_entry *) NULL;
10684
db51cc60 10685 /* Don't allow fake index register unless allow_index_reg isn't 0. */
e968fc9b 10686 if (!allow_index_reg && r->reg_num == RegIZ)
db51cc60
L
10687 return (const reg_entry *) NULL;
10688
1d3f8286
JB
10689 /* Upper 16 vector registers are only available with VREX in 64bit
10690 mode, and require EVEX encoding. */
10691 if (r->reg_flags & RegVRex)
43234a1e 10692 {
e951d5ca 10693 if (!cpu_arch_flags.bitfield.cpuavx512f
43234a1e
L
10694 || flag_code != CODE_64BIT)
10695 return (const reg_entry *) NULL;
1d3f8286
JB
10696
10697 i.vec_encoding = vex_encoding_evex;
43234a1e
L
10698 }
10699
4787f4a5
JB
10700 if (((r->reg_flags & (RegRex64 | RegRex)) || r->reg_type.bitfield.qword)
10701 && (!cpu_arch_flags.bitfield.cpulm || !r->reg_type.bitfield.control)
1ae00879 10702 && flag_code != CODE_64BIT)
20f0a1fc 10703 return (const reg_entry *) NULL;
1ae00879 10704
b7240065
JB
10705 if (r->reg_type.bitfield.sreg3 && r->reg_num == RegFlat && !intel_syntax)
10706 return (const reg_entry *) NULL;
10707
252b5132
RH
10708 return r;
10709}
4d1bb795
JB
10710
10711/* REG_STRING starts *before* REGISTER_PREFIX. */
10712
10713static const reg_entry *
10714parse_register (char *reg_string, char **end_op)
10715{
10716 const reg_entry *r;
10717
10718 if (*reg_string == REGISTER_PREFIX || allow_naked_reg)
10719 r = parse_real_register (reg_string, end_op);
10720 else
10721 r = NULL;
10722 if (!r)
10723 {
10724 char *save = input_line_pointer;
10725 char c;
10726 symbolS *symbolP;
10727
10728 input_line_pointer = reg_string;
d02603dc 10729 c = get_symbol_name (&reg_string);
4d1bb795
JB
10730 symbolP = symbol_find (reg_string);
10731 if (symbolP && S_GET_SEGMENT (symbolP) == reg_section)
10732 {
10733 const expressionS *e = symbol_get_value_expression (symbolP);
10734
0398aac5 10735 know (e->X_op == O_register);
4eed87de 10736 know (e->X_add_number >= 0
c3fe08fa 10737 && (valueT) e->X_add_number < i386_regtab_size);
4d1bb795 10738 r = i386_regtab + e->X_add_number;
d3bb6b49 10739 if ((r->reg_flags & RegVRex))
86fa6981 10740 i.vec_encoding = vex_encoding_evex;
4d1bb795
JB
10741 *end_op = input_line_pointer;
10742 }
10743 *input_line_pointer = c;
10744 input_line_pointer = save;
10745 }
10746 return r;
10747}
10748
10749int
10750i386_parse_name (char *name, expressionS *e, char *nextcharP)
10751{
10752 const reg_entry *r;
10753 char *end = input_line_pointer;
10754
10755 *end = *nextcharP;
10756 r = parse_register (name, &input_line_pointer);
10757 if (r && end <= input_line_pointer)
10758 {
10759 *nextcharP = *input_line_pointer;
10760 *input_line_pointer = 0;
10761 e->X_op = O_register;
10762 e->X_add_number = r - i386_regtab;
10763 return 1;
10764 }
10765 input_line_pointer = end;
10766 *end = 0;
ee86248c 10767 return intel_syntax ? i386_intel_parse_name (name, e) : 0;
4d1bb795
JB
10768}
10769
10770void
10771md_operand (expressionS *e)
10772{
ee86248c
JB
10773 char *end;
10774 const reg_entry *r;
4d1bb795 10775
ee86248c
JB
10776 switch (*input_line_pointer)
10777 {
10778 case REGISTER_PREFIX:
10779 r = parse_real_register (input_line_pointer, &end);
4d1bb795
JB
10780 if (r)
10781 {
10782 e->X_op = O_register;
10783 e->X_add_number = r - i386_regtab;
10784 input_line_pointer = end;
10785 }
ee86248c
JB
10786 break;
10787
10788 case '[':
9c2799c2 10789 gas_assert (intel_syntax);
ee86248c
JB
10790 end = input_line_pointer++;
10791 expression (e);
10792 if (*input_line_pointer == ']')
10793 {
10794 ++input_line_pointer;
10795 e->X_op_symbol = make_expr_symbol (e);
10796 e->X_add_symbol = NULL;
10797 e->X_add_number = 0;
10798 e->X_op = O_index;
10799 }
10800 else
10801 {
10802 e->X_op = O_absent;
10803 input_line_pointer = end;
10804 }
10805 break;
4d1bb795
JB
10806 }
10807}
10808
252b5132 10809\f
4cc782b5 10810#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
b6f8c7c4 10811const char *md_shortopts = "kVQ:sqnO::";
252b5132 10812#else
b6f8c7c4 10813const char *md_shortopts = "qnO::";
252b5132 10814#endif
6e0b89ee 10815
3e73aa7c 10816#define OPTION_32 (OPTION_MD_BASE + 0)
b3b91714
AM
10817#define OPTION_64 (OPTION_MD_BASE + 1)
10818#define OPTION_DIVIDE (OPTION_MD_BASE + 2)
9103f4f4
L
10819#define OPTION_MARCH (OPTION_MD_BASE + 3)
10820#define OPTION_MTUNE (OPTION_MD_BASE + 4)
1efbbeb4
L
10821#define OPTION_MMNEMONIC (OPTION_MD_BASE + 5)
10822#define OPTION_MSYNTAX (OPTION_MD_BASE + 6)
10823#define OPTION_MINDEX_REG (OPTION_MD_BASE + 7)
10824#define OPTION_MNAKED_REG (OPTION_MD_BASE + 8)
bd5dea88 10825#define OPTION_MRELAX_RELOCATIONS (OPTION_MD_BASE + 9)
c0f3af97 10826#define OPTION_MSSE2AVX (OPTION_MD_BASE + 10)
daf50ae7 10827#define OPTION_MSSE_CHECK (OPTION_MD_BASE + 11)
7bab8ab5
JB
10828#define OPTION_MOPERAND_CHECK (OPTION_MD_BASE + 12)
10829#define OPTION_MAVXSCALAR (OPTION_MD_BASE + 13)
10830#define OPTION_X32 (OPTION_MD_BASE + 14)
7e8b059b 10831#define OPTION_MADD_BND_PREFIX (OPTION_MD_BASE + 15)
43234a1e
L
10832#define OPTION_MEVEXLIG (OPTION_MD_BASE + 16)
10833#define OPTION_MEVEXWIG (OPTION_MD_BASE + 17)
167ad85b 10834#define OPTION_MBIG_OBJ (OPTION_MD_BASE + 18)
d1982f93 10835#define OPTION_MOMIT_LOCK_PREFIX (OPTION_MD_BASE + 19)
d3d3c6db 10836#define OPTION_MEVEXRCIG (OPTION_MD_BASE + 20)
8dcea932 10837#define OPTION_MSHARED (OPTION_MD_BASE + 21)
5db04b09
L
10838#define OPTION_MAMD64 (OPTION_MD_BASE + 22)
10839#define OPTION_MINTEL64 (OPTION_MD_BASE + 23)
e4e00185 10840#define OPTION_MFENCE_AS_LOCK_ADD (OPTION_MD_BASE + 24)
b4a3a7b4 10841#define OPTION_X86_USED_NOTE (OPTION_MD_BASE + 25)
b3b91714 10842
99ad8390
NC
10843struct option md_longopts[] =
10844{
3e73aa7c 10845 {"32", no_argument, NULL, OPTION_32},
321098a5 10846#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 10847 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c 10848 {"64", no_argument, NULL, OPTION_64},
351f65ca
L
10849#endif
10850#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 10851 {"x32", no_argument, NULL, OPTION_X32},
8dcea932 10852 {"mshared", no_argument, NULL, OPTION_MSHARED},
b4a3a7b4 10853 {"mx86-used-note", required_argument, NULL, OPTION_X86_USED_NOTE},
6e0b89ee 10854#endif
b3b91714 10855 {"divide", no_argument, NULL, OPTION_DIVIDE},
9103f4f4
L
10856 {"march", required_argument, NULL, OPTION_MARCH},
10857 {"mtune", required_argument, NULL, OPTION_MTUNE},
1efbbeb4
L
10858 {"mmnemonic", required_argument, NULL, OPTION_MMNEMONIC},
10859 {"msyntax", required_argument, NULL, OPTION_MSYNTAX},
10860 {"mindex-reg", no_argument, NULL, OPTION_MINDEX_REG},
10861 {"mnaked-reg", no_argument, NULL, OPTION_MNAKED_REG},
c0f3af97 10862 {"msse2avx", no_argument, NULL, OPTION_MSSE2AVX},
daf50ae7 10863 {"msse-check", required_argument, NULL, OPTION_MSSE_CHECK},
7bab8ab5 10864 {"moperand-check", required_argument, NULL, OPTION_MOPERAND_CHECK},
539f890d 10865 {"mavxscalar", required_argument, NULL, OPTION_MAVXSCALAR},
7e8b059b 10866 {"madd-bnd-prefix", no_argument, NULL, OPTION_MADD_BND_PREFIX},
43234a1e
L
10867 {"mevexlig", required_argument, NULL, OPTION_MEVEXLIG},
10868 {"mevexwig", required_argument, NULL, OPTION_MEVEXWIG},
167ad85b
TG
10869# if defined (TE_PE) || defined (TE_PEP)
10870 {"mbig-obj", no_argument, NULL, OPTION_MBIG_OBJ},
10871#endif
d1982f93 10872 {"momit-lock-prefix", required_argument, NULL, OPTION_MOMIT_LOCK_PREFIX},
e4e00185 10873 {"mfence-as-lock-add", required_argument, NULL, OPTION_MFENCE_AS_LOCK_ADD},
0cb4071e 10874 {"mrelax-relocations", required_argument, NULL, OPTION_MRELAX_RELOCATIONS},
d3d3c6db 10875 {"mevexrcig", required_argument, NULL, OPTION_MEVEXRCIG},
5db04b09
L
10876 {"mamd64", no_argument, NULL, OPTION_MAMD64},
10877 {"mintel64", no_argument, NULL, OPTION_MINTEL64},
252b5132
RH
10878 {NULL, no_argument, NULL, 0}
10879};
10880size_t md_longopts_size = sizeof (md_longopts);
10881
10882int
17b9d67d 10883md_parse_option (int c, const char *arg)
252b5132 10884{
91d6fa6a 10885 unsigned int j;
293f5f65 10886 char *arch, *next, *saved;
9103f4f4 10887
252b5132
RH
10888 switch (c)
10889 {
12b55ccc
L
10890 case 'n':
10891 optimize_align_code = 0;
10892 break;
10893
a38cf1db
AM
10894 case 'q':
10895 quiet_warnings = 1;
252b5132
RH
10896 break;
10897
10898#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
a38cf1db
AM
10899 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
10900 should be emitted or not. FIXME: Not implemented. */
10901 case 'Q':
252b5132
RH
10902 break;
10903
10904 /* -V: SVR4 argument to print version ID. */
10905 case 'V':
10906 print_version_id ();
10907 break;
10908
a38cf1db
AM
10909 /* -k: Ignore for FreeBSD compatibility. */
10910 case 'k':
252b5132 10911 break;
4cc782b5
ILT
10912
10913 case 's':
10914 /* -s: On i386 Solaris, this tells the native assembler to use
29b0f896 10915 .stab instead of .stab.excl. We always use .stab anyhow. */
4cc782b5 10916 break;
8dcea932
L
10917
10918 case OPTION_MSHARED:
10919 shared = 1;
10920 break;
b4a3a7b4
L
10921
10922 case OPTION_X86_USED_NOTE:
10923 if (strcasecmp (arg, "yes") == 0)
10924 x86_used_note = 1;
10925 else if (strcasecmp (arg, "no") == 0)
10926 x86_used_note = 0;
10927 else
10928 as_fatal (_("invalid -mx86-used-note= option: `%s'"), arg);
10929 break;
10930
10931
99ad8390 10932#endif
321098a5 10933#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 10934 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c
JH
10935 case OPTION_64:
10936 {
10937 const char **list, **l;
10938
3e73aa7c
JH
10939 list = bfd_target_list ();
10940 for (l = list; *l != NULL; l++)
8620418b 10941 if (CONST_STRNEQ (*l, "elf64-x86-64")
99ad8390
NC
10942 || strcmp (*l, "coff-x86-64") == 0
10943 || strcmp (*l, "pe-x86-64") == 0
d382c579
TG
10944 || strcmp (*l, "pei-x86-64") == 0
10945 || strcmp (*l, "mach-o-x86-64") == 0)
6e0b89ee
AM
10946 {
10947 default_arch = "x86_64";
10948 break;
10949 }
3e73aa7c 10950 if (*l == NULL)
2b5d6a91 10951 as_fatal (_("no compiled in support for x86_64"));
3e73aa7c
JH
10952 free (list);
10953 }
10954 break;
10955#endif
252b5132 10956
351f65ca 10957#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 10958 case OPTION_X32:
351f65ca
L
10959 if (IS_ELF)
10960 {
10961 const char **list, **l;
10962
10963 list = bfd_target_list ();
10964 for (l = list; *l != NULL; l++)
10965 if (CONST_STRNEQ (*l, "elf32-x86-64"))
10966 {
10967 default_arch = "x86_64:32";
10968 break;
10969 }
10970 if (*l == NULL)
2b5d6a91 10971 as_fatal (_("no compiled in support for 32bit x86_64"));
351f65ca
L
10972 free (list);
10973 }
10974 else
10975 as_fatal (_("32bit x86_64 is only supported for ELF"));
10976 break;
10977#endif
10978
6e0b89ee
AM
10979 case OPTION_32:
10980 default_arch = "i386";
10981 break;
10982
b3b91714
AM
10983 case OPTION_DIVIDE:
10984#ifdef SVR4_COMMENT_CHARS
10985 {
10986 char *n, *t;
10987 const char *s;
10988
add39d23 10989 n = XNEWVEC (char, strlen (i386_comment_chars) + 1);
b3b91714
AM
10990 t = n;
10991 for (s = i386_comment_chars; *s != '\0'; s++)
10992 if (*s != '/')
10993 *t++ = *s;
10994 *t = '\0';
10995 i386_comment_chars = n;
10996 }
10997#endif
10998 break;
10999
9103f4f4 11000 case OPTION_MARCH:
293f5f65
L
11001 saved = xstrdup (arg);
11002 arch = saved;
11003 /* Allow -march=+nosse. */
11004 if (*arch == '+')
11005 arch++;
6305a203 11006 do
9103f4f4 11007 {
6305a203 11008 if (*arch == '.')
2b5d6a91 11009 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
11010 next = strchr (arch, '+');
11011 if (next)
11012 *next++ = '\0';
91d6fa6a 11013 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 11014 {
91d6fa6a 11015 if (strcmp (arch, cpu_arch [j].name) == 0)
ccc9c027 11016 {
6305a203 11017 /* Processor. */
1ded5609
JB
11018 if (! cpu_arch[j].flags.bitfield.cpui386)
11019 continue;
11020
91d6fa6a 11021 cpu_arch_name = cpu_arch[j].name;
6305a203 11022 cpu_sub_arch_name = NULL;
91d6fa6a
NC
11023 cpu_arch_flags = cpu_arch[j].flags;
11024 cpu_arch_isa = cpu_arch[j].type;
11025 cpu_arch_isa_flags = cpu_arch[j].flags;
6305a203
L
11026 if (!cpu_arch_tune_set)
11027 {
11028 cpu_arch_tune = cpu_arch_isa;
11029 cpu_arch_tune_flags = cpu_arch_isa_flags;
11030 }
11031 break;
11032 }
91d6fa6a
NC
11033 else if (*cpu_arch [j].name == '.'
11034 && strcmp (arch, cpu_arch [j].name + 1) == 0)
6305a203 11035 {
33eaf5de 11036 /* ISA extension. */
6305a203 11037 i386_cpu_flags flags;
309d3373 11038
293f5f65
L
11039 flags = cpu_flags_or (cpu_arch_flags,
11040 cpu_arch[j].flags);
81486035 11041
5b64d091 11042 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
6305a203
L
11043 {
11044 if (cpu_sub_arch_name)
11045 {
11046 char *name = cpu_sub_arch_name;
11047 cpu_sub_arch_name = concat (name,
91d6fa6a 11048 cpu_arch[j].name,
1bf57e9f 11049 (const char *) NULL);
6305a203
L
11050 free (name);
11051 }
11052 else
91d6fa6a 11053 cpu_sub_arch_name = xstrdup (cpu_arch[j].name);
6305a203 11054 cpu_arch_flags = flags;
a586129e 11055 cpu_arch_isa_flags = flags;
6305a203 11056 }
0089dace
L
11057 else
11058 cpu_arch_isa_flags
11059 = cpu_flags_or (cpu_arch_isa_flags,
11060 cpu_arch[j].flags);
6305a203 11061 break;
ccc9c027 11062 }
9103f4f4 11063 }
6305a203 11064
293f5f65
L
11065 if (j >= ARRAY_SIZE (cpu_arch))
11066 {
33eaf5de 11067 /* Disable an ISA extension. */
293f5f65
L
11068 for (j = 0; j < ARRAY_SIZE (cpu_noarch); j++)
11069 if (strcmp (arch, cpu_noarch [j].name) == 0)
11070 {
11071 i386_cpu_flags flags;
11072
11073 flags = cpu_flags_and_not (cpu_arch_flags,
11074 cpu_noarch[j].flags);
11075 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
11076 {
11077 if (cpu_sub_arch_name)
11078 {
11079 char *name = cpu_sub_arch_name;
11080 cpu_sub_arch_name = concat (arch,
11081 (const char *) NULL);
11082 free (name);
11083 }
11084 else
11085 cpu_sub_arch_name = xstrdup (arch);
11086 cpu_arch_flags = flags;
11087 cpu_arch_isa_flags = flags;
11088 }
11089 break;
11090 }
11091
11092 if (j >= ARRAY_SIZE (cpu_noarch))
11093 j = ARRAY_SIZE (cpu_arch);
11094 }
11095
91d6fa6a 11096 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 11097 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
11098
11099 arch = next;
9103f4f4 11100 }
293f5f65
L
11101 while (next != NULL);
11102 free (saved);
9103f4f4
L
11103 break;
11104
11105 case OPTION_MTUNE:
11106 if (*arg == '.')
2b5d6a91 11107 as_fatal (_("invalid -mtune= option: `%s'"), arg);
91d6fa6a 11108 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 11109 {
91d6fa6a 11110 if (strcmp (arg, cpu_arch [j].name) == 0)
9103f4f4 11111 {
ccc9c027 11112 cpu_arch_tune_set = 1;
91d6fa6a
NC
11113 cpu_arch_tune = cpu_arch [j].type;
11114 cpu_arch_tune_flags = cpu_arch[j].flags;
9103f4f4
L
11115 break;
11116 }
11117 }
91d6fa6a 11118 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 11119 as_fatal (_("invalid -mtune= option: `%s'"), arg);
9103f4f4
L
11120 break;
11121
1efbbeb4
L
11122 case OPTION_MMNEMONIC:
11123 if (strcasecmp (arg, "att") == 0)
11124 intel_mnemonic = 0;
11125 else if (strcasecmp (arg, "intel") == 0)
11126 intel_mnemonic = 1;
11127 else
2b5d6a91 11128 as_fatal (_("invalid -mmnemonic= option: `%s'"), arg);
1efbbeb4
L
11129 break;
11130
11131 case OPTION_MSYNTAX:
11132 if (strcasecmp (arg, "att") == 0)
11133 intel_syntax = 0;
11134 else if (strcasecmp (arg, "intel") == 0)
11135 intel_syntax = 1;
11136 else
2b5d6a91 11137 as_fatal (_("invalid -msyntax= option: `%s'"), arg);
1efbbeb4
L
11138 break;
11139
11140 case OPTION_MINDEX_REG:
11141 allow_index_reg = 1;
11142 break;
11143
11144 case OPTION_MNAKED_REG:
11145 allow_naked_reg = 1;
11146 break;
11147
c0f3af97
L
11148 case OPTION_MSSE2AVX:
11149 sse2avx = 1;
11150 break;
11151
daf50ae7
L
11152 case OPTION_MSSE_CHECK:
11153 if (strcasecmp (arg, "error") == 0)
7bab8ab5 11154 sse_check = check_error;
daf50ae7 11155 else if (strcasecmp (arg, "warning") == 0)
7bab8ab5 11156 sse_check = check_warning;
daf50ae7 11157 else if (strcasecmp (arg, "none") == 0)
7bab8ab5 11158 sse_check = check_none;
daf50ae7 11159 else
2b5d6a91 11160 as_fatal (_("invalid -msse-check= option: `%s'"), arg);
daf50ae7
L
11161 break;
11162
7bab8ab5
JB
11163 case OPTION_MOPERAND_CHECK:
11164 if (strcasecmp (arg, "error") == 0)
11165 operand_check = check_error;
11166 else if (strcasecmp (arg, "warning") == 0)
11167 operand_check = check_warning;
11168 else if (strcasecmp (arg, "none") == 0)
11169 operand_check = check_none;
11170 else
11171 as_fatal (_("invalid -moperand-check= option: `%s'"), arg);
11172 break;
11173
539f890d
L
11174 case OPTION_MAVXSCALAR:
11175 if (strcasecmp (arg, "128") == 0)
11176 avxscalar = vex128;
11177 else if (strcasecmp (arg, "256") == 0)
11178 avxscalar = vex256;
11179 else
2b5d6a91 11180 as_fatal (_("invalid -mavxscalar= option: `%s'"), arg);
539f890d
L
11181 break;
11182
7e8b059b
L
11183 case OPTION_MADD_BND_PREFIX:
11184 add_bnd_prefix = 1;
11185 break;
11186
43234a1e
L
11187 case OPTION_MEVEXLIG:
11188 if (strcmp (arg, "128") == 0)
11189 evexlig = evexl128;
11190 else if (strcmp (arg, "256") == 0)
11191 evexlig = evexl256;
11192 else if (strcmp (arg, "512") == 0)
11193 evexlig = evexl512;
11194 else
11195 as_fatal (_("invalid -mevexlig= option: `%s'"), arg);
11196 break;
11197
d3d3c6db
IT
11198 case OPTION_MEVEXRCIG:
11199 if (strcmp (arg, "rne") == 0)
11200 evexrcig = rne;
11201 else if (strcmp (arg, "rd") == 0)
11202 evexrcig = rd;
11203 else if (strcmp (arg, "ru") == 0)
11204 evexrcig = ru;
11205 else if (strcmp (arg, "rz") == 0)
11206 evexrcig = rz;
11207 else
11208 as_fatal (_("invalid -mevexrcig= option: `%s'"), arg);
11209 break;
11210
43234a1e
L
11211 case OPTION_MEVEXWIG:
11212 if (strcmp (arg, "0") == 0)
11213 evexwig = evexw0;
11214 else if (strcmp (arg, "1") == 0)
11215 evexwig = evexw1;
11216 else
11217 as_fatal (_("invalid -mevexwig= option: `%s'"), arg);
11218 break;
11219
167ad85b
TG
11220# if defined (TE_PE) || defined (TE_PEP)
11221 case OPTION_MBIG_OBJ:
11222 use_big_obj = 1;
11223 break;
11224#endif
11225
d1982f93 11226 case OPTION_MOMIT_LOCK_PREFIX:
d022bddd
IT
11227 if (strcasecmp (arg, "yes") == 0)
11228 omit_lock_prefix = 1;
11229 else if (strcasecmp (arg, "no") == 0)
11230 omit_lock_prefix = 0;
11231 else
11232 as_fatal (_("invalid -momit-lock-prefix= option: `%s'"), arg);
11233 break;
11234
e4e00185
AS
11235 case OPTION_MFENCE_AS_LOCK_ADD:
11236 if (strcasecmp (arg, "yes") == 0)
11237 avoid_fence = 1;
11238 else if (strcasecmp (arg, "no") == 0)
11239 avoid_fence = 0;
11240 else
11241 as_fatal (_("invalid -mfence-as-lock-add= option: `%s'"), arg);
11242 break;
11243
0cb4071e
L
11244 case OPTION_MRELAX_RELOCATIONS:
11245 if (strcasecmp (arg, "yes") == 0)
11246 generate_relax_relocations = 1;
11247 else if (strcasecmp (arg, "no") == 0)
11248 generate_relax_relocations = 0;
11249 else
11250 as_fatal (_("invalid -mrelax-relocations= option: `%s'"), arg);
11251 break;
11252
5db04b09 11253 case OPTION_MAMD64:
e89c5eaa 11254 intel64 = 0;
5db04b09
L
11255 break;
11256
11257 case OPTION_MINTEL64:
e89c5eaa 11258 intel64 = 1;
5db04b09
L
11259 break;
11260
b6f8c7c4
L
11261 case 'O':
11262 if (arg == NULL)
11263 {
11264 optimize = 1;
11265 /* Turn off -Os. */
11266 optimize_for_space = 0;
11267 }
11268 else if (*arg == 's')
11269 {
11270 optimize_for_space = 1;
11271 /* Turn on all encoding optimizations. */
11272 optimize = -1;
11273 }
11274 else
11275 {
11276 optimize = atoi (arg);
11277 /* Turn off -Os. */
11278 optimize_for_space = 0;
11279 }
11280 break;
11281
252b5132
RH
11282 default:
11283 return 0;
11284 }
11285 return 1;
11286}
11287
8a2c8fef
L
11288#define MESSAGE_TEMPLATE \
11289" "
11290
293f5f65
L
11291static char *
11292output_message (FILE *stream, char *p, char *message, char *start,
11293 int *left_p, const char *name, int len)
11294{
11295 int size = sizeof (MESSAGE_TEMPLATE);
11296 int left = *left_p;
11297
11298 /* Reserve 2 spaces for ", " or ",\0" */
11299 left -= len + 2;
11300
11301 /* Check if there is any room. */
11302 if (left >= 0)
11303 {
11304 if (p != start)
11305 {
11306 *p++ = ',';
11307 *p++ = ' ';
11308 }
11309 p = mempcpy (p, name, len);
11310 }
11311 else
11312 {
11313 /* Output the current message now and start a new one. */
11314 *p++ = ',';
11315 *p = '\0';
11316 fprintf (stream, "%s\n", message);
11317 p = start;
11318 left = size - (start - message) - len - 2;
11319
11320 gas_assert (left >= 0);
11321
11322 p = mempcpy (p, name, len);
11323 }
11324
11325 *left_p = left;
11326 return p;
11327}
11328
8a2c8fef 11329static void
1ded5609 11330show_arch (FILE *stream, int ext, int check)
8a2c8fef
L
11331{
11332 static char message[] = MESSAGE_TEMPLATE;
11333 char *start = message + 27;
11334 char *p;
11335 int size = sizeof (MESSAGE_TEMPLATE);
11336 int left;
11337 const char *name;
11338 int len;
11339 unsigned int j;
11340
11341 p = start;
11342 left = size - (start - message);
11343 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
11344 {
11345 /* Should it be skipped? */
11346 if (cpu_arch [j].skip)
11347 continue;
11348
11349 name = cpu_arch [j].name;
11350 len = cpu_arch [j].len;
11351 if (*name == '.')
11352 {
11353 /* It is an extension. Skip if we aren't asked to show it. */
11354 if (ext)
11355 {
11356 name++;
11357 len--;
11358 }
11359 else
11360 continue;
11361 }
11362 else if (ext)
11363 {
11364 /* It is an processor. Skip if we show only extension. */
11365 continue;
11366 }
1ded5609
JB
11367 else if (check && ! cpu_arch[j].flags.bitfield.cpui386)
11368 {
11369 /* It is an impossible processor - skip. */
11370 continue;
11371 }
8a2c8fef 11372
293f5f65 11373 p = output_message (stream, p, message, start, &left, name, len);
8a2c8fef
L
11374 }
11375
293f5f65
L
11376 /* Display disabled extensions. */
11377 if (ext)
11378 for (j = 0; j < ARRAY_SIZE (cpu_noarch); j++)
11379 {
11380 name = cpu_noarch [j].name;
11381 len = cpu_noarch [j].len;
11382 p = output_message (stream, p, message, start, &left, name,
11383 len);
11384 }
11385
8a2c8fef
L
11386 *p = '\0';
11387 fprintf (stream, "%s\n", message);
11388}
11389
252b5132 11390void
8a2c8fef 11391md_show_usage (FILE *stream)
252b5132 11392{
4cc782b5
ILT
11393#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
11394 fprintf (stream, _("\
a38cf1db
AM
11395 -Q ignored\n\
11396 -V print assembler version number\n\
b3b91714
AM
11397 -k ignored\n"));
11398#endif
11399 fprintf (stream, _("\
12b55ccc 11400 -n Do not optimize code alignment\n\
b3b91714
AM
11401 -q quieten some warnings\n"));
11402#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
11403 fprintf (stream, _("\
a38cf1db 11404 -s ignored\n"));
b3b91714 11405#endif
d7f449c0
L
11406#if defined BFD64 && (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
11407 || defined (TE_PE) || defined (TE_PEP))
751d281c 11408 fprintf (stream, _("\
570561f7 11409 --32/--64/--x32 generate 32bit/64bit/x32 code\n"));
751d281c 11410#endif
b3b91714
AM
11411#ifdef SVR4_COMMENT_CHARS
11412 fprintf (stream, _("\
11413 --divide do not treat `/' as a comment character\n"));
a38cf1db
AM
11414#else
11415 fprintf (stream, _("\
b3b91714 11416 --divide ignored\n"));
4cc782b5 11417#endif
9103f4f4 11418 fprintf (stream, _("\
6305a203 11419 -march=CPU[,+EXTENSION...]\n\
8a2c8fef 11420 generate code for CPU and EXTENSION, CPU is one of:\n"));
1ded5609 11421 show_arch (stream, 0, 1);
8a2c8fef
L
11422 fprintf (stream, _("\
11423 EXTENSION is combination of:\n"));
1ded5609 11424 show_arch (stream, 1, 0);
6305a203 11425 fprintf (stream, _("\
8a2c8fef 11426 -mtune=CPU optimize for CPU, CPU is one of:\n"));
1ded5609 11427 show_arch (stream, 0, 0);
ba104c83 11428 fprintf (stream, _("\
c0f3af97
L
11429 -msse2avx encode SSE instructions with VEX prefix\n"));
11430 fprintf (stream, _("\
7c5c05ef 11431 -msse-check=[none|error|warning] (default: warning)\n\
daf50ae7
L
11432 check SSE instructions\n"));
11433 fprintf (stream, _("\
7c5c05ef 11434 -moperand-check=[none|error|warning] (default: warning)\n\
7bab8ab5
JB
11435 check operand combinations for validity\n"));
11436 fprintf (stream, _("\
7c5c05ef
L
11437 -mavxscalar=[128|256] (default: 128)\n\
11438 encode scalar AVX instructions with specific vector\n\
539f890d
L
11439 length\n"));
11440 fprintf (stream, _("\
7c5c05ef
L
11441 -mevexlig=[128|256|512] (default: 128)\n\
11442 encode scalar EVEX instructions with specific vector\n\
43234a1e
L
11443 length\n"));
11444 fprintf (stream, _("\
7c5c05ef
L
11445 -mevexwig=[0|1] (default: 0)\n\
11446 encode EVEX instructions with specific EVEX.W value\n\
43234a1e
L
11447 for EVEX.W bit ignored instructions\n"));
11448 fprintf (stream, _("\
7c5c05ef 11449 -mevexrcig=[rne|rd|ru|rz] (default: rne)\n\
d3d3c6db
IT
11450 encode EVEX instructions with specific EVEX.RC value\n\
11451 for SAE-only ignored instructions\n"));
11452 fprintf (stream, _("\
7c5c05ef
L
11453 -mmnemonic=[att|intel] "));
11454 if (SYSV386_COMPAT)
11455 fprintf (stream, _("(default: att)\n"));
11456 else
11457 fprintf (stream, _("(default: intel)\n"));
11458 fprintf (stream, _("\
11459 use AT&T/Intel mnemonic\n"));
ba104c83 11460 fprintf (stream, _("\
7c5c05ef
L
11461 -msyntax=[att|intel] (default: att)\n\
11462 use AT&T/Intel syntax\n"));
ba104c83
L
11463 fprintf (stream, _("\
11464 -mindex-reg support pseudo index registers\n"));
11465 fprintf (stream, _("\
11466 -mnaked-reg don't require `%%' prefix for registers\n"));
11467 fprintf (stream, _("\
7e8b059b 11468 -madd-bnd-prefix add BND prefix for all valid branches\n"));
b4a3a7b4 11469#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8dcea932
L
11470 fprintf (stream, _("\
11471 -mshared disable branch optimization for shared code\n"));
b4a3a7b4
L
11472 fprintf (stream, _("\
11473 -mx86-used-note=[no|yes] "));
11474 if (DEFAULT_X86_USED_NOTE)
11475 fprintf (stream, _("(default: yes)\n"));
11476 else
11477 fprintf (stream, _("(default: no)\n"));
11478 fprintf (stream, _("\
11479 generate x86 used ISA and feature properties\n"));
11480#endif
11481#if defined (TE_PE) || defined (TE_PEP)
167ad85b
TG
11482 fprintf (stream, _("\
11483 -mbig-obj generate big object files\n"));
11484#endif
d022bddd 11485 fprintf (stream, _("\
7c5c05ef 11486 -momit-lock-prefix=[no|yes] (default: no)\n\
d022bddd 11487 strip all lock prefixes\n"));
5db04b09 11488 fprintf (stream, _("\
7c5c05ef 11489 -mfence-as-lock-add=[no|yes] (default: no)\n\
e4e00185
AS
11490 encode lfence, mfence and sfence as\n\
11491 lock addl $0x0, (%%{re}sp)\n"));
11492 fprintf (stream, _("\
7c5c05ef
L
11493 -mrelax-relocations=[no|yes] "));
11494 if (DEFAULT_GENERATE_X86_RELAX_RELOCATIONS)
11495 fprintf (stream, _("(default: yes)\n"));
11496 else
11497 fprintf (stream, _("(default: no)\n"));
11498 fprintf (stream, _("\
0cb4071e
L
11499 generate relax relocations\n"));
11500 fprintf (stream, _("\
7c5c05ef 11501 -mamd64 accept only AMD64 ISA [default]\n"));
5db04b09
L
11502 fprintf (stream, _("\
11503 -mintel64 accept only Intel64 ISA\n"));
252b5132
RH
11504}
11505
3e73aa7c 11506#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
321098a5 11507 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
e57f8c65 11508 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
252b5132
RH
11509
11510/* Pick the target format to use. */
11511
47926f60 11512const char *
e3bb37b5 11513i386_target_format (void)
252b5132 11514{
351f65ca
L
11515 if (!strncmp (default_arch, "x86_64", 6))
11516 {
11517 update_code_flag (CODE_64BIT, 1);
11518 if (default_arch[6] == '\0')
7f56bc95 11519 x86_elf_abi = X86_64_ABI;
351f65ca 11520 else
7f56bc95 11521 x86_elf_abi = X86_64_X32_ABI;
351f65ca 11522 }
3e73aa7c 11523 else if (!strcmp (default_arch, "i386"))
78f12dd3 11524 update_code_flag (CODE_32BIT, 1);
5197d474
L
11525 else if (!strcmp (default_arch, "iamcu"))
11526 {
11527 update_code_flag (CODE_32BIT, 1);
11528 if (cpu_arch_isa == PROCESSOR_UNKNOWN)
11529 {
11530 static const i386_cpu_flags iamcu_flags = CPU_IAMCU_FLAGS;
11531 cpu_arch_name = "iamcu";
11532 cpu_sub_arch_name = NULL;
11533 cpu_arch_flags = iamcu_flags;
11534 cpu_arch_isa = PROCESSOR_IAMCU;
11535 cpu_arch_isa_flags = iamcu_flags;
11536 if (!cpu_arch_tune_set)
11537 {
11538 cpu_arch_tune = cpu_arch_isa;
11539 cpu_arch_tune_flags = cpu_arch_isa_flags;
11540 }
11541 }
8d471ec1 11542 else if (cpu_arch_isa != PROCESSOR_IAMCU)
5197d474
L
11543 as_fatal (_("Intel MCU doesn't support `%s' architecture"),
11544 cpu_arch_name);
11545 }
3e73aa7c 11546 else
2b5d6a91 11547 as_fatal (_("unknown architecture"));
89507696
JB
11548
11549 if (cpu_flags_all_zero (&cpu_arch_isa_flags))
11550 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].flags;
11551 if (cpu_flags_all_zero (&cpu_arch_tune_flags))
11552 cpu_arch_tune_flags = cpu_arch[flag_code == CODE_64BIT].flags;
11553
252b5132
RH
11554 switch (OUTPUT_FLAVOR)
11555 {
9384f2ff 11556#if defined (OBJ_MAYBE_AOUT) || defined (OBJ_AOUT)
4c63da97 11557 case bfd_target_aout_flavour:
47926f60 11558 return AOUT_TARGET_FORMAT;
4c63da97 11559#endif
9384f2ff
AM
11560#if defined (OBJ_MAYBE_COFF) || defined (OBJ_COFF)
11561# if defined (TE_PE) || defined (TE_PEP)
11562 case bfd_target_coff_flavour:
167ad85b
TG
11563 if (flag_code == CODE_64BIT)
11564 return use_big_obj ? "pe-bigobj-x86-64" : "pe-x86-64";
11565 else
11566 return "pe-i386";
9384f2ff 11567# elif defined (TE_GO32)
0561d57c
JK
11568 case bfd_target_coff_flavour:
11569 return "coff-go32";
9384f2ff 11570# else
252b5132
RH
11571 case bfd_target_coff_flavour:
11572 return "coff-i386";
9384f2ff 11573# endif
4c63da97 11574#endif
3e73aa7c 11575#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
252b5132 11576 case bfd_target_elf_flavour:
3e73aa7c 11577 {
351f65ca
L
11578 const char *format;
11579
11580 switch (x86_elf_abi)
4fa24527 11581 {
351f65ca
L
11582 default:
11583 format = ELF_TARGET_FORMAT;
11584 break;
7f56bc95 11585 case X86_64_ABI:
351f65ca 11586 use_rela_relocations = 1;
4fa24527 11587 object_64bit = 1;
351f65ca
L
11588 format = ELF_TARGET_FORMAT64;
11589 break;
7f56bc95 11590 case X86_64_X32_ABI:
4fa24527 11591 use_rela_relocations = 1;
351f65ca 11592 object_64bit = 1;
862be3fb 11593 disallow_64bit_reloc = 1;
351f65ca
L
11594 format = ELF_TARGET_FORMAT32;
11595 break;
4fa24527 11596 }
3632d14b 11597 if (cpu_arch_isa == PROCESSOR_L1OM)
8a9036a4 11598 {
7f56bc95 11599 if (x86_elf_abi != X86_64_ABI)
8a9036a4
L
11600 as_fatal (_("Intel L1OM is 64bit only"));
11601 return ELF_TARGET_L1OM_FORMAT;
11602 }
b49f93f6 11603 else if (cpu_arch_isa == PROCESSOR_K1OM)
7a9068fe
L
11604 {
11605 if (x86_elf_abi != X86_64_ABI)
11606 as_fatal (_("Intel K1OM is 64bit only"));
11607 return ELF_TARGET_K1OM_FORMAT;
11608 }
81486035
L
11609 else if (cpu_arch_isa == PROCESSOR_IAMCU)
11610 {
11611 if (x86_elf_abi != I386_ABI)
11612 as_fatal (_("Intel MCU is 32bit only"));
11613 return ELF_TARGET_IAMCU_FORMAT;
11614 }
8a9036a4 11615 else
351f65ca 11616 return format;
3e73aa7c 11617 }
e57f8c65
TG
11618#endif
11619#if defined (OBJ_MACH_O)
11620 case bfd_target_mach_o_flavour:
d382c579
TG
11621 if (flag_code == CODE_64BIT)
11622 {
11623 use_rela_relocations = 1;
11624 object_64bit = 1;
11625 return "mach-o-x86-64";
11626 }
11627 else
11628 return "mach-o-i386";
4c63da97 11629#endif
252b5132
RH
11630 default:
11631 abort ();
11632 return NULL;
11633 }
11634}
11635
47926f60 11636#endif /* OBJ_MAYBE_ more than one */
252b5132 11637\f
252b5132 11638symbolS *
7016a5d5 11639md_undefined_symbol (char *name)
252b5132 11640{
18dc2407
ILT
11641 if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
11642 && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
11643 && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
11644 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
24eab124
AM
11645 {
11646 if (!GOT_symbol)
11647 {
11648 if (symbol_find (name))
11649 as_bad (_("GOT already in symbol table"));
11650 GOT_symbol = symbol_new (name, undefined_section,
11651 (valueT) 0, &zero_address_frag);
11652 };
11653 return GOT_symbol;
11654 }
252b5132
RH
11655 return 0;
11656}
11657
11658/* Round up a section size to the appropriate boundary. */
47926f60 11659
252b5132 11660valueT
7016a5d5 11661md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
252b5132 11662{
4c63da97
AM
11663#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
11664 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
11665 {
11666 /* For a.out, force the section size to be aligned. If we don't do
11667 this, BFD will align it for us, but it will not write out the
11668 final bytes of the section. This may be a bug in BFD, but it is
11669 easier to fix it here since that is how the other a.out targets
11670 work. */
11671 int align;
11672
11673 align = bfd_get_section_alignment (stdoutput, segment);
8d3842cd 11674 size = ((size + (1 << align) - 1) & (-((valueT) 1 << align)));
4c63da97 11675 }
252b5132
RH
11676#endif
11677
11678 return size;
11679}
11680
11681/* On the i386, PC-relative offsets are relative to the start of the
11682 next instruction. That is, the address of the offset, plus its
11683 size, since the offset is always the last part of the insn. */
11684
11685long
e3bb37b5 11686md_pcrel_from (fixS *fixP)
252b5132
RH
11687{
11688 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
11689}
11690
11691#ifndef I386COFF
11692
11693static void
e3bb37b5 11694s_bss (int ignore ATTRIBUTE_UNUSED)
252b5132 11695{
29b0f896 11696 int temp;
252b5132 11697
8a75718c
JB
11698#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
11699 if (IS_ELF)
11700 obj_elf_section_change_hook ();
11701#endif
252b5132
RH
11702 temp = get_absolute_expression ();
11703 subseg_set (bss_section, (subsegT) temp);
11704 demand_empty_rest_of_line ();
11705}
11706
11707#endif
11708
252b5132 11709void
e3bb37b5 11710i386_validate_fix (fixS *fixp)
252b5132 11711{
02a86693 11712 if (fixp->fx_subsy)
252b5132 11713 {
02a86693 11714 if (fixp->fx_subsy == GOT_symbol)
23df1078 11715 {
02a86693
L
11716 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
11717 {
11718 if (!object_64bit)
11719 abort ();
11720#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
11721 if (fixp->fx_tcbit2)
56ceb5b5
L
11722 fixp->fx_r_type = (fixp->fx_tcbit
11723 ? BFD_RELOC_X86_64_REX_GOTPCRELX
11724 : BFD_RELOC_X86_64_GOTPCRELX);
02a86693
L
11725 else
11726#endif
11727 fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
11728 }
d6ab8113 11729 else
02a86693
L
11730 {
11731 if (!object_64bit)
11732 fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
11733 else
11734 fixp->fx_r_type = BFD_RELOC_X86_64_GOTOFF64;
11735 }
11736 fixp->fx_subsy = 0;
23df1078 11737 }
252b5132 11738 }
02a86693
L
11739#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
11740 else if (!object_64bit)
11741 {
11742 if (fixp->fx_r_type == BFD_RELOC_386_GOT32
11743 && fixp->fx_tcbit2)
11744 fixp->fx_r_type = BFD_RELOC_386_GOT32X;
11745 }
11746#endif
252b5132
RH
11747}
11748
252b5132 11749arelent *
7016a5d5 11750tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
11751{
11752 arelent *rel;
11753 bfd_reloc_code_real_type code;
11754
11755 switch (fixp->fx_r_type)
11756 {
8ce3d284 11757#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
11758 case BFD_RELOC_SIZE32:
11759 case BFD_RELOC_SIZE64:
11760 if (S_IS_DEFINED (fixp->fx_addsy)
11761 && !S_IS_EXTERNAL (fixp->fx_addsy))
11762 {
11763 /* Resolve size relocation against local symbol to size of
11764 the symbol plus addend. */
11765 valueT value = S_GET_SIZE (fixp->fx_addsy) + fixp->fx_offset;
11766 if (fixp->fx_r_type == BFD_RELOC_SIZE32
11767 && !fits_in_unsigned_long (value))
11768 as_bad_where (fixp->fx_file, fixp->fx_line,
11769 _("symbol size computation overflow"));
11770 fixp->fx_addsy = NULL;
11771 fixp->fx_subsy = NULL;
11772 md_apply_fix (fixp, (valueT *) &value, NULL);
11773 return NULL;
11774 }
8ce3d284 11775#endif
1a0670f3 11776 /* Fall through. */
8fd4256d 11777
3e73aa7c
JH
11778 case BFD_RELOC_X86_64_PLT32:
11779 case BFD_RELOC_X86_64_GOT32:
11780 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
11781 case BFD_RELOC_X86_64_GOTPCRELX:
11782 case BFD_RELOC_X86_64_REX_GOTPCRELX:
252b5132
RH
11783 case BFD_RELOC_386_PLT32:
11784 case BFD_RELOC_386_GOT32:
02a86693 11785 case BFD_RELOC_386_GOT32X:
252b5132
RH
11786 case BFD_RELOC_386_GOTOFF:
11787 case BFD_RELOC_386_GOTPC:
13ae64f3
JJ
11788 case BFD_RELOC_386_TLS_GD:
11789 case BFD_RELOC_386_TLS_LDM:
11790 case BFD_RELOC_386_TLS_LDO_32:
11791 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
11792 case BFD_RELOC_386_TLS_IE:
11793 case BFD_RELOC_386_TLS_GOTIE:
13ae64f3
JJ
11794 case BFD_RELOC_386_TLS_LE_32:
11795 case BFD_RELOC_386_TLS_LE:
67a4f2b7
AO
11796 case BFD_RELOC_386_TLS_GOTDESC:
11797 case BFD_RELOC_386_TLS_DESC_CALL:
bffbf940
JJ
11798 case BFD_RELOC_X86_64_TLSGD:
11799 case BFD_RELOC_X86_64_TLSLD:
11800 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 11801 case BFD_RELOC_X86_64_DTPOFF64:
bffbf940
JJ
11802 case BFD_RELOC_X86_64_GOTTPOFF:
11803 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113
JB
11804 case BFD_RELOC_X86_64_TPOFF64:
11805 case BFD_RELOC_X86_64_GOTOFF64:
11806 case BFD_RELOC_X86_64_GOTPC32:
7b81dfbb
AJ
11807 case BFD_RELOC_X86_64_GOT64:
11808 case BFD_RELOC_X86_64_GOTPCREL64:
11809 case BFD_RELOC_X86_64_GOTPC64:
11810 case BFD_RELOC_X86_64_GOTPLT64:
11811 case BFD_RELOC_X86_64_PLTOFF64:
67a4f2b7
AO
11812 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
11813 case BFD_RELOC_X86_64_TLSDESC_CALL:
252b5132
RH
11814 case BFD_RELOC_RVA:
11815 case BFD_RELOC_VTABLE_ENTRY:
11816 case BFD_RELOC_VTABLE_INHERIT:
6482c264
NC
11817#ifdef TE_PE
11818 case BFD_RELOC_32_SECREL:
11819#endif
252b5132
RH
11820 code = fixp->fx_r_type;
11821 break;
dbbaec26
L
11822 case BFD_RELOC_X86_64_32S:
11823 if (!fixp->fx_pcrel)
11824 {
11825 /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32. */
11826 code = fixp->fx_r_type;
11827 break;
11828 }
1a0670f3 11829 /* Fall through. */
252b5132 11830 default:
93382f6d 11831 if (fixp->fx_pcrel)
252b5132 11832 {
93382f6d
AM
11833 switch (fixp->fx_size)
11834 {
11835 default:
b091f402
AM
11836 as_bad_where (fixp->fx_file, fixp->fx_line,
11837 _("can not do %d byte pc-relative relocation"),
11838 fixp->fx_size);
93382f6d
AM
11839 code = BFD_RELOC_32_PCREL;
11840 break;
11841 case 1: code = BFD_RELOC_8_PCREL; break;
11842 case 2: code = BFD_RELOC_16_PCREL; break;
d258b828 11843 case 4: code = BFD_RELOC_32_PCREL; break;
d6ab8113
JB
11844#ifdef BFD64
11845 case 8: code = BFD_RELOC_64_PCREL; break;
11846#endif
93382f6d
AM
11847 }
11848 }
11849 else
11850 {
11851 switch (fixp->fx_size)
11852 {
11853 default:
b091f402
AM
11854 as_bad_where (fixp->fx_file, fixp->fx_line,
11855 _("can not do %d byte relocation"),
11856 fixp->fx_size);
93382f6d
AM
11857 code = BFD_RELOC_32;
11858 break;
11859 case 1: code = BFD_RELOC_8; break;
11860 case 2: code = BFD_RELOC_16; break;
11861 case 4: code = BFD_RELOC_32; break;
937149dd 11862#ifdef BFD64
3e73aa7c 11863 case 8: code = BFD_RELOC_64; break;
937149dd 11864#endif
93382f6d 11865 }
252b5132
RH
11866 }
11867 break;
11868 }
252b5132 11869
d182319b
JB
11870 if ((code == BFD_RELOC_32
11871 || code == BFD_RELOC_32_PCREL
11872 || code == BFD_RELOC_X86_64_32S)
252b5132
RH
11873 && GOT_symbol
11874 && fixp->fx_addsy == GOT_symbol)
3e73aa7c 11875 {
4fa24527 11876 if (!object_64bit)
d6ab8113
JB
11877 code = BFD_RELOC_386_GOTPC;
11878 else
11879 code = BFD_RELOC_X86_64_GOTPC32;
3e73aa7c 11880 }
7b81dfbb
AJ
11881 if ((code == BFD_RELOC_64 || code == BFD_RELOC_64_PCREL)
11882 && GOT_symbol
11883 && fixp->fx_addsy == GOT_symbol)
11884 {
11885 code = BFD_RELOC_X86_64_GOTPC64;
11886 }
252b5132 11887
add39d23
TS
11888 rel = XNEW (arelent);
11889 rel->sym_ptr_ptr = XNEW (asymbol *);
49309057 11890 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
11891
11892 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
c87db184 11893
3e73aa7c
JH
11894 if (!use_rela_relocations)
11895 {
11896 /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
11897 vtable entry to be used in the relocation's section offset. */
11898 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
11899 rel->address = fixp->fx_offset;
fbeb56a4
DK
11900#if defined (OBJ_COFF) && defined (TE_PE)
11901 else if (fixp->fx_addsy && S_IS_WEAK (fixp->fx_addsy))
11902 rel->addend = fixp->fx_addnumber - (S_GET_VALUE (fixp->fx_addsy) * 2);
11903 else
11904#endif
c6682705 11905 rel->addend = 0;
3e73aa7c
JH
11906 }
11907 /* Use the rela in 64bit mode. */
252b5132 11908 else
3e73aa7c 11909 {
862be3fb
L
11910 if (disallow_64bit_reloc)
11911 switch (code)
11912 {
862be3fb
L
11913 case BFD_RELOC_X86_64_DTPOFF64:
11914 case BFD_RELOC_X86_64_TPOFF64:
11915 case BFD_RELOC_64_PCREL:
11916 case BFD_RELOC_X86_64_GOTOFF64:
11917 case BFD_RELOC_X86_64_GOT64:
11918 case BFD_RELOC_X86_64_GOTPCREL64:
11919 case BFD_RELOC_X86_64_GOTPC64:
11920 case BFD_RELOC_X86_64_GOTPLT64:
11921 case BFD_RELOC_X86_64_PLTOFF64:
11922 as_bad_where (fixp->fx_file, fixp->fx_line,
11923 _("cannot represent relocation type %s in x32 mode"),
11924 bfd_get_reloc_code_name (code));
11925 break;
11926 default:
11927 break;
11928 }
11929
062cd5e7
AS
11930 if (!fixp->fx_pcrel)
11931 rel->addend = fixp->fx_offset;
11932 else
11933 switch (code)
11934 {
11935 case BFD_RELOC_X86_64_PLT32:
11936 case BFD_RELOC_X86_64_GOT32:
11937 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
11938 case BFD_RELOC_X86_64_GOTPCRELX:
11939 case BFD_RELOC_X86_64_REX_GOTPCRELX:
bffbf940
JJ
11940 case BFD_RELOC_X86_64_TLSGD:
11941 case BFD_RELOC_X86_64_TLSLD:
11942 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7
AO
11943 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
11944 case BFD_RELOC_X86_64_TLSDESC_CALL:
062cd5e7
AS
11945 rel->addend = fixp->fx_offset - fixp->fx_size;
11946 break;
11947 default:
11948 rel->addend = (section->vma
11949 - fixp->fx_size
11950 + fixp->fx_addnumber
11951 + md_pcrel_from (fixp));
11952 break;
11953 }
3e73aa7c
JH
11954 }
11955
252b5132
RH
11956 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
11957 if (rel->howto == NULL)
11958 {
11959 as_bad_where (fixp->fx_file, fixp->fx_line,
d0b47220 11960 _("cannot represent relocation type %s"),
252b5132
RH
11961 bfd_get_reloc_code_name (code));
11962 /* Set howto to a garbage value so that we can keep going. */
11963 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
9c2799c2 11964 gas_assert (rel->howto != NULL);
252b5132
RH
11965 }
11966
11967 return rel;
11968}
11969
ee86248c 11970#include "tc-i386-intel.c"
54cfded0 11971
a60de03c
JB
11972void
11973tc_x86_parse_to_dw2regnum (expressionS *exp)
54cfded0 11974{
a60de03c
JB
11975 int saved_naked_reg;
11976 char saved_register_dot;
54cfded0 11977
a60de03c
JB
11978 saved_naked_reg = allow_naked_reg;
11979 allow_naked_reg = 1;
11980 saved_register_dot = register_chars['.'];
11981 register_chars['.'] = '.';
11982 allow_pseudo_reg = 1;
11983 expression_and_evaluate (exp);
11984 allow_pseudo_reg = 0;
11985 register_chars['.'] = saved_register_dot;
11986 allow_naked_reg = saved_naked_reg;
11987
e96d56a1 11988 if (exp->X_op == O_register && exp->X_add_number >= 0)
54cfded0 11989 {
a60de03c
JB
11990 if ((addressT) exp->X_add_number < i386_regtab_size)
11991 {
11992 exp->X_op = O_constant;
11993 exp->X_add_number = i386_regtab[exp->X_add_number]
11994 .dw2_regnum[flag_code >> 1];
11995 }
11996 else
11997 exp->X_op = O_illegal;
54cfded0 11998 }
54cfded0
AM
11999}
12000
12001void
12002tc_x86_frame_initial_instructions (void)
12003{
a60de03c
JB
12004 static unsigned int sp_regno[2];
12005
12006 if (!sp_regno[flag_code >> 1])
12007 {
12008 char *saved_input = input_line_pointer;
12009 char sp[][4] = {"esp", "rsp"};
12010 expressionS exp;
a4447b93 12011
a60de03c
JB
12012 input_line_pointer = sp[flag_code >> 1];
12013 tc_x86_parse_to_dw2regnum (&exp);
9c2799c2 12014 gas_assert (exp.X_op == O_constant);
a60de03c
JB
12015 sp_regno[flag_code >> 1] = exp.X_add_number;
12016 input_line_pointer = saved_input;
12017 }
a4447b93 12018
61ff971f
L
12019 cfi_add_CFA_def_cfa (sp_regno[flag_code >> 1], -x86_cie_data_alignment);
12020 cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
54cfded0 12021}
d2b2c203 12022
d7921315
L
12023int
12024x86_dwarf2_addr_size (void)
12025{
12026#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
12027 if (x86_elf_abi == X86_64_X32_ABI)
12028 return 4;
12029#endif
12030 return bfd_arch_bits_per_address (stdoutput) / 8;
12031}
12032
d2b2c203
DJ
12033int
12034i386_elf_section_type (const char *str, size_t len)
12035{
12036 if (flag_code == CODE_64BIT
12037 && len == sizeof ("unwind") - 1
12038 && strncmp (str, "unwind", 6) == 0)
12039 return SHT_X86_64_UNWIND;
12040
12041 return -1;
12042}
bb41ade5 12043
ad5fec3b
EB
12044#ifdef TE_SOLARIS
12045void
12046i386_solaris_fix_up_eh_frame (segT sec)
12047{
12048 if (flag_code == CODE_64BIT)
12049 elf_section_type (sec) = SHT_X86_64_UNWIND;
12050}
12051#endif
12052
bb41ade5
AM
12053#ifdef TE_PE
12054void
12055tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
12056{
91d6fa6a 12057 expressionS exp;
bb41ade5 12058
91d6fa6a
NC
12059 exp.X_op = O_secrel;
12060 exp.X_add_symbol = symbol;
12061 exp.X_add_number = 0;
12062 emit_expr (&exp, size);
bb41ade5
AM
12063}
12064#endif
3b22753a
L
12065
12066#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
12067/* For ELF on x86-64, add support for SHF_X86_64_LARGE. */
12068
01e1a5bc 12069bfd_vma
6d4af3c2 12070x86_64_section_letter (int letter, const char **ptr_msg)
3b22753a
L
12071{
12072 if (flag_code == CODE_64BIT)
12073 {
12074 if (letter == 'l')
12075 return SHF_X86_64_LARGE;
12076
8f3bae45 12077 *ptr_msg = _("bad .section directive: want a,l,w,x,M,S,G,T in string");
64e74474 12078 }
3b22753a 12079 else
8f3bae45 12080 *ptr_msg = _("bad .section directive: want a,w,x,M,S,G,T in string");
3b22753a
L
12081 return -1;
12082}
12083
01e1a5bc 12084bfd_vma
3b22753a
L
12085x86_64_section_word (char *str, size_t len)
12086{
8620418b 12087 if (len == 5 && flag_code == CODE_64BIT && CONST_STRNEQ (str, "large"))
3b22753a
L
12088 return SHF_X86_64_LARGE;
12089
12090 return -1;
12091}
12092
12093static void
12094handle_large_common (int small ATTRIBUTE_UNUSED)
12095{
12096 if (flag_code != CODE_64BIT)
12097 {
12098 s_comm_internal (0, elf_common_parse);
12099 as_warn (_(".largecomm supported only in 64bit mode, producing .comm"));
12100 }
12101 else
12102 {
12103 static segT lbss_section;
12104 asection *saved_com_section_ptr = elf_com_section_ptr;
12105 asection *saved_bss_section = bss_section;
12106
12107 if (lbss_section == NULL)
12108 {
12109 flagword applicable;
12110 segT seg = now_seg;
12111 subsegT subseg = now_subseg;
12112
12113 /* The .lbss section is for local .largecomm symbols. */
12114 lbss_section = subseg_new (".lbss", 0);
12115 applicable = bfd_applicable_section_flags (stdoutput);
12116 bfd_set_section_flags (stdoutput, lbss_section,
12117 applicable & SEC_ALLOC);
12118 seg_info (lbss_section)->bss = 1;
12119
12120 subseg_set (seg, subseg);
12121 }
12122
12123 elf_com_section_ptr = &_bfd_elf_large_com_section;
12124 bss_section = lbss_section;
12125
12126 s_comm_internal (0, elf_common_parse);
12127
12128 elf_com_section_ptr = saved_com_section_ptr;
12129 bss_section = saved_bss_section;
12130 }
12131}
12132#endif /* OBJ_ELF || OBJ_MAYBE_ELF */
This page took 2.791004 seconds and 4 git commands to generate.