x86: fold a few of the "alternative" NOP patterns
[binutils-gdb.git] / gas / config / tc-i386.c
1 /* tc-i386.c -- Assemble code for the Intel 80386
2 Copyright (C) 1989-2023 Free Software Foundation, Inc.
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
8 the Free Software Foundation; either version 3, or (at your option)
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
18 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
19 02110-1301, USA. */
20
21 /* Intel 80386 machine specific gas.
22 Written by Eliot Dresselhaus (eliot@mgm.mit.edu).
23 x86_64 support by Jan Hubicka (jh@suse.cz)
24 VIA PadLock support by Michal Ludvig (mludvig@suse.cz)
25 Bugs & suggestions are completely welcome. This is free software.
26 Please help us make it better. */
27
28 #include "as.h"
29 #include "safe-ctype.h"
30 #include "subsegs.h"
31 #include "dwarf2dbg.h"
32 #include "dw2gencfi.h"
33 #include "gen-sframe.h"
34 #include "sframe.h"
35 #include "elf/x86-64.h"
36 #include "opcodes/i386-init.h"
37 #include "opcodes/i386-mnem.h"
38 #include <limits.h>
39
40 #ifndef INFER_ADDR_PREFIX
41 #define INFER_ADDR_PREFIX 1
42 #endif
43
44 #ifndef DEFAULT_ARCH
45 #define DEFAULT_ARCH "i386"
46 #endif
47
48 #ifndef INLINE
49 #if __GNUC__ >= 2
50 #define INLINE __inline__
51 #else
52 #define INLINE
53 #endif
54 #endif
55
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,
60 REP_PREFIX/HLE_PREFIX, LOCK_PREFIX. */
61 #define WAIT_PREFIX 0
62 #define SEG_PREFIX 1
63 #define ADDR_PREFIX 2
64 #define DATA_PREFIX 3
65 #define REP_PREFIX 4
66 #define HLE_PREFIX REP_PREFIX
67 #define BND_PREFIX REP_PREFIX
68 #define LOCK_PREFIX 5
69 #define REX_PREFIX 6 /* must come last. */
70 #define MAX_PREFIXES 7 /* max prefixes per opcode */
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'
84
85 #define END_OF_INSN '\0'
86
87 #define OPERAND_TYPE_NONE { .bitfield = { .class = ClassNone } }
88
89 /* This matches the C -> StaticRounding alias in the opcode table. */
90 #define commutative staticrounding
91
92 /*
93 'templates' is for grouping together 'template' structures for opcodes
94 of the same name. This is only used for storing the insns in the grand
95 ole hash table of insns.
96 The templates themselves start at START and range up to (but not including)
97 END.
98 */
99 typedef struct
100 {
101 const insn_template *start;
102 const insn_template *end;
103 }
104 templates;
105
106 /* 386 operand encoding bytes: see 386 book for details of this. */
107 typedef struct
108 {
109 unsigned int regmem; /* codes register or memory operand */
110 unsigned int reg; /* codes register operand (or extended opcode) */
111 unsigned int mode; /* how to interpret regmem & reg */
112 }
113 modrm_byte;
114
115 /* x86-64 extension prefix. */
116 typedef int rex_byte;
117
118 /* 386 opcode byte to code indirect addressing. */
119 typedef struct
120 {
121 unsigned base;
122 unsigned index;
123 unsigned scale;
124 }
125 sib_byte;
126
127 /* x86 arch names, types and features */
128 typedef struct
129 {
130 const char *name; /* arch name */
131 unsigned int len:8; /* arch string length */
132 bool skip:1; /* show_arch should skip this. */
133 enum processor_type type; /* arch type */
134 enum { vsz_none, vsz_set, vsz_reset } vsz; /* vector size control */
135 i386_cpu_flags enable; /* cpu feature enable flags */
136 i386_cpu_flags disable; /* cpu feature disable flags */
137 }
138 arch_entry;
139
140 static void update_code_flag (int, int);
141 static void s_insn (int);
142 static void set_code_flag (int);
143 static void set_16bit_gcc_code_flag (int);
144 static void set_intel_syntax (int);
145 static void set_intel_mnemonic (int);
146 static void set_allow_index_reg (int);
147 static void set_check (int);
148 static void set_cpu_arch (int);
149 #ifdef TE_PE
150 static void pe_directive_secrel (int);
151 static void pe_directive_secidx (int);
152 #endif
153 static void signed_cons (int);
154 static char *output_invalid (int c);
155 static int i386_finalize_immediate (segT, expressionS *, i386_operand_type,
156 const char *);
157 static int i386_finalize_displacement (segT, expressionS *, i386_operand_type,
158 const char *);
159 static int i386_att_operand (char *);
160 static int i386_intel_operand (char *, int);
161 static int i386_intel_simplify (expressionS *);
162 static int i386_intel_parse_name (const char *, expressionS *);
163 static const reg_entry *parse_register (const char *, char **);
164 static const char *parse_insn (const char *, char *, bool);
165 static char *parse_operands (char *, const char *);
166 static void swap_operands (void);
167 static void swap_2_operands (unsigned int, unsigned int);
168 static enum i386_flag_code i386_addressing_mode (void);
169 static void optimize_imm (void);
170 static bool optimize_disp (const insn_template *t);
171 static const insn_template *match_template (char);
172 static int check_string (void);
173 static int process_suffix (void);
174 static int check_byte_reg (void);
175 static int check_long_reg (void);
176 static int check_qword_reg (void);
177 static int check_word_reg (void);
178 static int finalize_imm (void);
179 static int process_operands (void);
180 static const reg_entry *build_modrm_byte (void);
181 static void output_insn (void);
182 static void output_imm (fragS *, offsetT);
183 static void output_disp (fragS *, offsetT);
184 #ifndef I386COFF
185 static void s_bss (int);
186 #endif
187 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
188 static void handle_large_common (int small ATTRIBUTE_UNUSED);
189
190 /* GNU_PROPERTY_X86_ISA_1_USED. */
191 static unsigned int x86_isa_1_used;
192 /* GNU_PROPERTY_X86_FEATURE_2_USED. */
193 static unsigned int x86_feature_2_used;
194 /* Generate x86 used ISA and feature properties. */
195 static unsigned int x86_used_note = DEFAULT_X86_USED_NOTE;
196 #endif
197
198 static const char *default_arch = DEFAULT_ARCH;
199
200 /* parse_register() returns this when a register alias cannot be used. */
201 static const reg_entry bad_reg = { "<bad>", OPERAND_TYPE_NONE, 0, 0,
202 { Dw2Inval, Dw2Inval } };
203
204 static const reg_entry *reg_eax;
205 static const reg_entry *reg_ds;
206 static const reg_entry *reg_es;
207 static const reg_entry *reg_ss;
208 static const reg_entry *reg_st0;
209 static const reg_entry *reg_k0;
210
211 /* VEX prefix. */
212 typedef struct
213 {
214 /* VEX prefix is either 2 byte or 3 byte. EVEX is 4 byte. */
215 unsigned char bytes[4];
216 unsigned int length;
217 /* Destination or source register specifier. */
218 const reg_entry *register_specifier;
219 } vex_prefix;
220
221 /* 'md_assemble ()' gathers together information and puts it into a
222 i386_insn. */
223
224 union i386_op
225 {
226 expressionS *disps;
227 expressionS *imms;
228 const reg_entry *regs;
229 };
230
231 enum i386_error
232 {
233 no_error, /* Must be first. */
234 operand_size_mismatch,
235 operand_type_mismatch,
236 register_type_mismatch,
237 number_of_operands_mismatch,
238 invalid_instruction_suffix,
239 bad_imm4,
240 unsupported_with_intel_mnemonic,
241 unsupported_syntax,
242 unsupported,
243 unsupported_on_arch,
244 unsupported_64bit,
245 invalid_sib_address,
246 invalid_vsib_address,
247 invalid_vector_register_set,
248 invalid_tmm_register_set,
249 invalid_dest_and_src_register_set,
250 unsupported_vector_index_register,
251 unsupported_broadcast,
252 broadcast_needed,
253 unsupported_masking,
254 mask_not_on_destination,
255 no_default_mask,
256 unsupported_rc_sae,
257 invalid_register_operand,
258 internal_error,
259 };
260
261 struct _i386_insn
262 {
263 /* TM holds the template for the insn were currently assembling. */
264 insn_template tm;
265
266 /* SUFFIX holds the instruction size suffix for byte, word, dword
267 or qword, if given. */
268 char suffix;
269
270 /* OPCODE_LENGTH holds the number of base opcode bytes. */
271 unsigned char opcode_length;
272
273 /* OPERANDS gives the number of given operands. */
274 unsigned int operands;
275
276 /* REG_OPERANDS, DISP_OPERANDS, MEM_OPERANDS, IMM_OPERANDS give the number
277 of given register, displacement, memory operands and immediate
278 operands. */
279 unsigned int reg_operands, disp_operands, mem_operands, imm_operands;
280
281 /* TYPES [i] is the type (see above #defines) which tells us how to
282 use OP[i] for the corresponding operand. */
283 i386_operand_type types[MAX_OPERANDS];
284
285 /* Displacement expression, immediate expression, or register for each
286 operand. */
287 union i386_op op[MAX_OPERANDS];
288
289 /* Flags for operands. */
290 unsigned int flags[MAX_OPERANDS];
291 #define Operand_PCrel 1
292 #define Operand_Mem 2
293 #define Operand_Signed 4 /* .insn only */
294
295 /* Relocation type for operand */
296 enum bfd_reloc_code_real reloc[MAX_OPERANDS];
297
298 /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
299 the base index byte below. */
300 const reg_entry *base_reg;
301 const reg_entry *index_reg;
302 unsigned int log2_scale_factor;
303
304 /* SEG gives the seg_entries of this insn. They are zero unless
305 explicit segment overrides are given. */
306 const reg_entry *seg[2];
307
308 /* PREFIX holds all the given prefix opcodes (usually null).
309 PREFIXES is the number of prefix opcodes. */
310 unsigned int prefixes;
311 unsigned char prefix[MAX_PREFIXES];
312
313 /* .insn allows for reserved opcode spaces. */
314 unsigned char insn_opcode_space;
315
316 /* .insn also allows (requires) specifying immediate size. */
317 unsigned char imm_bits[MAX_OPERANDS];
318
319 /* Register is in low 3 bits of opcode. */
320 bool short_form;
321
322 /* The operand to a branch insn indicates an absolute branch. */
323 bool jumpabsolute;
324
325 /* The operand to a branch insn indicates a far branch. */
326 bool far_branch;
327
328 /* There is a memory operand of (%dx) which should be only used
329 with input/output instructions. */
330 bool input_output_operand;
331
332 /* Extended states. */
333 enum
334 {
335 /* Use MMX state. */
336 xstate_mmx = 1 << 0,
337 /* Use XMM state. */
338 xstate_xmm = 1 << 1,
339 /* Use YMM state. */
340 xstate_ymm = 1 << 2 | xstate_xmm,
341 /* Use ZMM state. */
342 xstate_zmm = 1 << 3 | xstate_ymm,
343 /* Use TMM state. */
344 xstate_tmm = 1 << 4,
345 /* Use MASK state. */
346 xstate_mask = 1 << 5
347 } xstate;
348
349 /* Has GOTPC or TLS relocation. */
350 bool has_gotpc_tls_reloc;
351
352 /* RM and SIB are the modrm byte and the sib byte where the
353 addressing modes of this insn are encoded. */
354 modrm_byte rm;
355 rex_byte rex;
356 rex_byte vrex;
357 sib_byte sib;
358 vex_prefix vex;
359
360 /* Masking attributes.
361
362 The struct describes masking, applied to OPERAND in the instruction.
363 REG is a pointer to the corresponding mask register. ZEROING tells
364 whether merging or zeroing mask is used. */
365 struct Mask_Operation
366 {
367 const reg_entry *reg;
368 unsigned int zeroing;
369 /* The operand where this operation is associated. */
370 unsigned int operand;
371 } mask;
372
373 /* Rounding control and SAE attributes. */
374 struct RC_Operation
375 {
376 enum rc_type
377 {
378 rc_none = -1,
379 rne,
380 rd,
381 ru,
382 rz,
383 saeonly
384 } type;
385 /* In Intel syntax the operand modifier form is supposed to be used, but
386 we continue to accept the immediate forms as well. */
387 bool modifier;
388 } rounding;
389
390 /* Broadcasting attributes.
391
392 The struct describes broadcasting, applied to OPERAND. TYPE is
393 expresses the broadcast factor. */
394 struct Broadcast_Operation
395 {
396 /* Type of broadcast: {1to2}, {1to4}, {1to8}, {1to16} or {1to32}. */
397 unsigned int type;
398
399 /* Index of broadcasted operand. */
400 unsigned int operand;
401
402 /* Number of bytes to broadcast. */
403 unsigned int bytes;
404 } broadcast;
405
406 /* Compressed disp8*N attribute. */
407 unsigned int memshift;
408
409 /* Prefer load or store in encoding. */
410 enum
411 {
412 dir_encoding_default = 0,
413 dir_encoding_load,
414 dir_encoding_store,
415 dir_encoding_swap
416 } dir_encoding;
417
418 /* Prefer 8bit, 16bit, 32bit displacement in encoding. */
419 enum
420 {
421 disp_encoding_default = 0,
422 disp_encoding_8bit,
423 disp_encoding_16bit,
424 disp_encoding_32bit
425 } disp_encoding;
426
427 /* Prefer the REX byte in encoding. */
428 bool rex_encoding;
429
430 /* Disable instruction size optimization. */
431 bool no_optimize;
432
433 /* How to encode vector instructions. */
434 enum
435 {
436 vex_encoding_default = 0,
437 vex_encoding_vex,
438 vex_encoding_vex3,
439 vex_encoding_evex,
440 vex_encoding_evex512,
441 vex_encoding_error
442 } vec_encoding;
443
444 /* REP prefix. */
445 const char *rep_prefix;
446
447 /* HLE prefix. */
448 const char *hle_prefix;
449
450 /* Have BND prefix. */
451 const char *bnd_prefix;
452
453 /* Have NOTRACK prefix. */
454 const char *notrack_prefix;
455
456 /* Error message. */
457 enum i386_error error;
458 };
459
460 typedef struct _i386_insn i386_insn;
461
462 /* Link RC type with corresponding string, that'll be looked for in
463 asm. */
464 struct RC_name
465 {
466 enum rc_type type;
467 const char *name;
468 unsigned int len;
469 };
470
471 static const struct RC_name RC_NamesTable[] =
472 {
473 { rne, STRING_COMMA_LEN ("rn-sae") },
474 { rd, STRING_COMMA_LEN ("rd-sae") },
475 { ru, STRING_COMMA_LEN ("ru-sae") },
476 { rz, STRING_COMMA_LEN ("rz-sae") },
477 { saeonly, STRING_COMMA_LEN ("sae") },
478 };
479
480 /* To be indexed by segment register number. */
481 static const unsigned char i386_seg_prefixes[] = {
482 ES_PREFIX_OPCODE,
483 CS_PREFIX_OPCODE,
484 SS_PREFIX_OPCODE,
485 DS_PREFIX_OPCODE,
486 FS_PREFIX_OPCODE,
487 GS_PREFIX_OPCODE
488 };
489
490 /* List of chars besides those in app.c:symbol_chars that can start an
491 operand. Used to prevent the scrubber eating vital white-space. */
492 const char extra_symbol_chars[] = "*%-([{}"
493 #ifdef LEX_AT
494 "@"
495 #endif
496 #ifdef LEX_QM
497 "?"
498 #endif
499 ;
500
501 #if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
502 && !defined (TE_GNU) \
503 && !defined (TE_LINUX) \
504 && !defined (TE_Haiku) \
505 && !defined (TE_FreeBSD) \
506 && !defined (TE_DragonFly) \
507 && !defined (TE_NetBSD))
508 /* This array holds the chars that always start a comment. If the
509 pre-processor is disabled, these aren't very useful. The option
510 --divide will remove '/' from this list. */
511 const char *i386_comment_chars = "#/";
512 #define SVR4_COMMENT_CHARS 1
513 #define PREFIX_SEPARATOR '\\'
514
515 #else
516 const char *i386_comment_chars = "#";
517 #define PREFIX_SEPARATOR '/'
518 #endif
519
520 /* This array holds the chars that only start a comment at the beginning of
521 a line. If the line seems to have the form '# 123 filename'
522 .line and .file directives will appear in the pre-processed output.
523 Note that input_file.c hand checks for '#' at the beginning of the
524 first line of the input file. This is because the compiler outputs
525 #NO_APP at the beginning of its output.
526 Also note that comments started like this one will always work if
527 '/' isn't otherwise defined. */
528 const char line_comment_chars[] = "#/";
529
530 const char line_separator_chars[] = ";";
531
532 /* Chars that can be used to separate mant from exp in floating point
533 nums. */
534 const char EXP_CHARS[] = "eE";
535
536 /* Chars that mean this number is a floating point constant
537 As in 0f12.456
538 or 0d1.2345e12. */
539 const char FLT_CHARS[] = "fFdDxXhHbB";
540
541 /* Tables for lexical analysis. */
542 static char mnemonic_chars[256];
543 static char register_chars[256];
544 static char operand_chars[256];
545
546 /* Lexical macros. */
547 #define is_operand_char(x) (operand_chars[(unsigned char) x])
548 #define is_register_char(x) (register_chars[(unsigned char) x])
549 #define is_space_char(x) ((x) == ' ')
550
551 /* All non-digit non-letter characters that may occur in an operand and
552 which aren't already in extra_symbol_chars[]. */
553 static const char operand_special_chars[] = "$+,)._~/<>|&^!=:@]";
554
555 /* md_assemble() always leaves the strings it's passed unaltered. To
556 effect this we maintain a stack of saved characters that we've smashed
557 with '\0's (indicating end of strings for various sub-fields of the
558 assembler instruction). */
559 static char save_stack[32];
560 static char *save_stack_p;
561 #define END_STRING_AND_SAVE(s) \
562 do { *save_stack_p++ = *(s); *(s) = '\0'; } while (0)
563 #define RESTORE_END_STRING(s) \
564 do { *(s) = *--save_stack_p; } while (0)
565
566 /* The instruction we're assembling. */
567 static i386_insn i;
568
569 /* Possible templates for current insn. */
570 static const templates *current_templates;
571
572 /* Per instruction expressionS buffers: max displacements & immediates. */
573 static expressionS disp_expressions[MAX_MEMORY_OPERANDS];
574 static expressionS im_expressions[MAX_IMMEDIATE_OPERANDS];
575
576 /* Current operand we are working on. */
577 static int this_operand = -1;
578
579 /* Are we processing a .insn directive? */
580 #define dot_insn() (i.tm.mnem_off == MN__insn)
581
582 enum i386_flag_code i386_flag_code;
583 #define flag_code i386_flag_code /* Permit to continue using original name. */
584 static unsigned int object_64bit;
585 static unsigned int disallow_64bit_reloc;
586 static int use_rela_relocations = 0;
587 /* __tls_get_addr/___tls_get_addr symbol for TLS. */
588 static const char *tls_get_addr;
589
590 #if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
591 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
592 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
593
594 /* The ELF ABI to use. */
595 enum x86_elf_abi
596 {
597 I386_ABI,
598 X86_64_ABI,
599 X86_64_X32_ABI
600 };
601
602 static enum x86_elf_abi x86_elf_abi = I386_ABI;
603 #endif
604
605 #if defined (TE_PE) || defined (TE_PEP)
606 /* Use big object file format. */
607 static int use_big_obj = 0;
608 #endif
609
610 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
611 /* 1 if generating code for a shared library. */
612 static int shared = 0;
613
614 unsigned int x86_sframe_cfa_sp_reg;
615 /* The other CFA base register for SFrame stack trace info. */
616 unsigned int x86_sframe_cfa_fp_reg;
617 unsigned int x86_sframe_cfa_ra_reg;
618
619 #endif
620
621 /* 1 for intel syntax,
622 0 if att syntax. */
623 static int intel_syntax = 0;
624
625 static enum x86_64_isa
626 {
627 amd64 = 1, /* AMD64 ISA. */
628 intel64 /* Intel64 ISA. */
629 } isa64;
630
631 /* 1 for intel mnemonic,
632 0 if att mnemonic. */
633 static int intel_mnemonic = !SYSV386_COMPAT;
634
635 /* 1 if pseudo registers are permitted. */
636 static int allow_pseudo_reg = 0;
637
638 /* 1 if register prefix % not required. */
639 static int allow_naked_reg = 0;
640
641 /* 1 if the assembler should add BND prefix for all control-transferring
642 instructions supporting it, even if this prefix wasn't specified
643 explicitly. */
644 static int add_bnd_prefix = 0;
645
646 /* 1 if pseudo index register, eiz/riz, is allowed . */
647 static int allow_index_reg = 0;
648
649 /* 1 if the assembler should ignore LOCK prefix, even if it was
650 specified explicitly. */
651 static int omit_lock_prefix = 0;
652
653 /* 1 if the assembler should encode lfence, mfence, and sfence as
654 "lock addl $0, (%{re}sp)". */
655 static int avoid_fence = 0;
656
657 /* 1 if lfence should be inserted after every load. */
658 static int lfence_after_load = 0;
659
660 /* Non-zero if lfence should be inserted before indirect branch. */
661 static enum lfence_before_indirect_branch_kind
662 {
663 lfence_branch_none = 0,
664 lfence_branch_register,
665 lfence_branch_memory,
666 lfence_branch_all
667 }
668 lfence_before_indirect_branch;
669
670 /* Non-zero if lfence should be inserted before ret. */
671 static enum lfence_before_ret_kind
672 {
673 lfence_before_ret_none = 0,
674 lfence_before_ret_not,
675 lfence_before_ret_or,
676 lfence_before_ret_shl
677 }
678 lfence_before_ret;
679
680 /* Types of previous instruction is .byte or prefix. */
681 static struct
682 {
683 segT seg;
684 const char *file;
685 const char *name;
686 unsigned int line;
687 enum last_insn_kind
688 {
689 last_insn_other = 0,
690 last_insn_directive,
691 last_insn_prefix
692 } kind;
693 } last_insn;
694
695 /* 1 if the assembler should generate relax relocations. */
696
697 static int generate_relax_relocations
698 = DEFAULT_GENERATE_X86_RELAX_RELOCATIONS;
699
700 static enum check_kind
701 {
702 check_none = 0,
703 check_warning,
704 check_error
705 }
706 sse_check, operand_check = check_warning;
707
708 /* Non-zero if branches should be aligned within power of 2 boundary. */
709 static int align_branch_power = 0;
710
711 /* Types of branches to align. */
712 enum align_branch_kind
713 {
714 align_branch_none = 0,
715 align_branch_jcc = 1,
716 align_branch_fused = 2,
717 align_branch_jmp = 3,
718 align_branch_call = 4,
719 align_branch_indirect = 5,
720 align_branch_ret = 6
721 };
722
723 /* Type bits of branches to align. */
724 enum align_branch_bit
725 {
726 align_branch_jcc_bit = 1 << align_branch_jcc,
727 align_branch_fused_bit = 1 << align_branch_fused,
728 align_branch_jmp_bit = 1 << align_branch_jmp,
729 align_branch_call_bit = 1 << align_branch_call,
730 align_branch_indirect_bit = 1 << align_branch_indirect,
731 align_branch_ret_bit = 1 << align_branch_ret
732 };
733
734 static unsigned int align_branch = (align_branch_jcc_bit
735 | align_branch_fused_bit
736 | align_branch_jmp_bit);
737
738 /* Types of condition jump used by macro-fusion. */
739 enum mf_jcc_kind
740 {
741 mf_jcc_jo = 0, /* base opcode 0x70 */
742 mf_jcc_jc, /* base opcode 0x72 */
743 mf_jcc_je, /* base opcode 0x74 */
744 mf_jcc_jna, /* base opcode 0x76 */
745 mf_jcc_js, /* base opcode 0x78 */
746 mf_jcc_jp, /* base opcode 0x7a */
747 mf_jcc_jl, /* base opcode 0x7c */
748 mf_jcc_jle, /* base opcode 0x7e */
749 };
750
751 /* Types of compare flag-modifying insntructions used by macro-fusion. */
752 enum mf_cmp_kind
753 {
754 mf_cmp_test_and, /* test/cmp */
755 mf_cmp_alu_cmp, /* add/sub/cmp */
756 mf_cmp_incdec /* inc/dec */
757 };
758
759 /* The maximum padding size for fused jcc. CMP like instruction can
760 be 9 bytes and jcc can be 6 bytes. Leave room just in case for
761 prefixes. */
762 #define MAX_FUSED_JCC_PADDING_SIZE 20
763
764 /* The maximum number of prefixes added for an instruction. */
765 static unsigned int align_branch_prefix_size = 5;
766
767 /* Optimization:
768 1. Clear the REX_W bit with register operand if possible.
769 2. Above plus use 128bit vector instruction to clear the full vector
770 register.
771 */
772 static int optimize = 0;
773
774 /* Optimization:
775 1. Clear the REX_W bit with register operand if possible.
776 2. Above plus use 128bit vector instruction to clear the full vector
777 register.
778 3. Above plus optimize "test{q,l,w} $imm8,%r{64,32,16}" to
779 "testb $imm7,%r8".
780 */
781 static int optimize_for_space = 0;
782
783 /* Register prefix used for error message. */
784 static const char *register_prefix = "%";
785
786 /* Used in 16 bit gcc mode to add an l suffix to call, ret, enter,
787 leave, push, and pop instructions so that gcc has the same stack
788 frame as in 32 bit mode. */
789 static char stackop_size = '\0';
790
791 /* Non-zero to optimize code alignment. */
792 int optimize_align_code = 1;
793
794 /* Non-zero to quieten some warnings. */
795 static int quiet_warnings = 0;
796
797 /* Guard to avoid repeated warnings about non-16-bit code on 16-bit CPUs. */
798 static bool pre_386_16bit_warned;
799
800 /* CPU name. */
801 static const char *cpu_arch_name = NULL;
802 static char *cpu_sub_arch_name = NULL;
803
804 /* CPU feature flags. */
805 i386_cpu_flags cpu_arch_flags = CPU_UNKNOWN_FLAGS;
806
807 /* If we have selected a cpu we are generating instructions for. */
808 static int cpu_arch_tune_set = 0;
809
810 /* Cpu we are generating instructions for. */
811 enum processor_type cpu_arch_tune = PROCESSOR_UNKNOWN;
812
813 /* CPU instruction set architecture used. */
814 enum processor_type cpu_arch_isa = PROCESSOR_UNKNOWN;
815
816 /* CPU feature flags of instruction set architecture used. */
817 i386_cpu_flags cpu_arch_isa_flags;
818
819 /* If set, conditional jumps are not automatically promoted to handle
820 larger than a byte offset. */
821 static bool no_cond_jump_promotion = false;
822
823 /* This will be set from an expression parser hook if there's any
824 applicable operator involved in an expression. */
825 static enum {
826 expr_operator_none,
827 expr_operator_present,
828 expr_large_value,
829 } expr_mode;
830
831 /* Encode SSE instructions with VEX prefix. */
832 static unsigned int sse2avx;
833
834 /* Encode aligned vector move as unaligned vector move. */
835 static unsigned int use_unaligned_vector_move;
836
837 /* Maximum permitted vector size. */
838 #define VSZ_DEFAULT VSZ512
839 static unsigned int vector_size = VSZ_DEFAULT;
840
841 /* Encode scalar AVX instructions with specific vector length. */
842 static enum
843 {
844 vex128 = 0,
845 vex256
846 } avxscalar;
847
848 /* Encode VEX WIG instructions with specific vex.w. */
849 static enum
850 {
851 vexw0 = 0,
852 vexw1
853 } vexwig;
854
855 /* Encode scalar EVEX LIG instructions with specific vector length. */
856 static enum
857 {
858 evexl128 = 0,
859 evexl256,
860 evexl512
861 } evexlig;
862
863 /* Encode EVEX WIG instructions with specific evex.w. */
864 static enum
865 {
866 evexw0 = 0,
867 evexw1
868 } evexwig;
869
870 /* Value to encode in EVEX RC bits, for SAE-only instructions. */
871 static enum rc_type evexrcig = rne;
872
873 /* Pre-defined "_GLOBAL_OFFSET_TABLE_". */
874 static symbolS *GOT_symbol;
875
876 /* The dwarf2 return column, adjusted for 32 or 64 bit. */
877 unsigned int x86_dwarf2_return_column;
878
879 /* The dwarf2 data alignment, adjusted for 32 or 64 bit. */
880 int x86_cie_data_alignment;
881
882 /* Interface to relax_segment.
883 There are 3 major relax states for 386 jump insns because the
884 different types of jumps add different sizes to frags when we're
885 figuring out what sort of jump to choose to reach a given label.
886
887 BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING are used to align
888 branches which are handled by md_estimate_size_before_relax() and
889 i386_generic_table_relax_frag(). */
890
891 /* Types. */
892 #define UNCOND_JUMP 0
893 #define COND_JUMP 1
894 #define COND_JUMP86 2
895 #define BRANCH_PADDING 3
896 #define BRANCH_PREFIX 4
897 #define FUSED_JCC_PADDING 5
898
899 /* Sizes. */
900 #define CODE16 1
901 #define SMALL 0
902 #define SMALL16 (SMALL | CODE16)
903 #define BIG 2
904 #define BIG16 (BIG | CODE16)
905
906 #ifndef INLINE
907 #ifdef __GNUC__
908 #define INLINE __inline__
909 #else
910 #define INLINE
911 #endif
912 #endif
913
914 #define ENCODE_RELAX_STATE(type, size) \
915 ((relax_substateT) (((type) << 2) | (size)))
916 #define TYPE_FROM_RELAX_STATE(s) \
917 ((s) >> 2)
918 #define DISP_SIZE_FROM_RELAX_STATE(s) \
919 ((((s) & 3) == BIG ? 4 : (((s) & 3) == BIG16 ? 2 : 1)))
920
921 /* This table is used by relax_frag to promote short jumps to long
922 ones where necessary. SMALL (short) jumps may be promoted to BIG
923 (32 bit long) ones, and SMALL16 jumps to BIG16 (16 bit long). We
924 don't allow a short jump in a 32 bit code segment to be promoted to
925 a 16 bit offset jump because it's slower (requires data size
926 prefix), and doesn't work, unless the destination is in the bottom
927 64k of the code segment (The top 16 bits of eip are zeroed). */
928
929 const relax_typeS md_relax_table[] =
930 {
931 /* The fields are:
932 1) most positive reach of this state,
933 2) most negative reach of this state,
934 3) how many bytes this mode will have in the variable part of the frag
935 4) which index into the table to try if we can't fit into this one. */
936
937 /* UNCOND_JUMP states. */
938 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG)},
939 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16)},
940 /* dword jmp adds 4 bytes to frag:
941 0 extra opcode bytes, 4 displacement bytes. */
942 {0, 0, 4, 0},
943 /* word jmp adds 2 byte2 to frag:
944 0 extra opcode bytes, 2 displacement bytes. */
945 {0, 0, 2, 0},
946
947 /* COND_JUMP states. */
948 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG)},
949 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG16)},
950 /* dword conditionals adds 5 bytes to frag:
951 1 extra opcode byte, 4 displacement bytes. */
952 {0, 0, 5, 0},
953 /* word conditionals add 3 bytes to frag:
954 1 extra opcode byte, 2 displacement bytes. */
955 {0, 0, 3, 0},
956
957 /* COND_JUMP86 states. */
958 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG)},
959 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG16)},
960 /* dword conditionals adds 5 bytes to frag:
961 1 extra opcode byte, 4 displacement bytes. */
962 {0, 0, 5, 0},
963 /* word conditionals add 4 bytes to frag:
964 1 displacement byte and a 3 byte long branch insn. */
965 {0, 0, 4, 0}
966 };
967
968 #define ARCH(n, t, f, s) \
969 { STRING_COMMA_LEN (#n), s, PROCESSOR_ ## t, vsz_none, CPU_ ## f ## _FLAGS, \
970 CPU_NONE_FLAGS }
971 #define SUBARCH(n, e, d, s) \
972 { STRING_COMMA_LEN (#n), s, PROCESSOR_NONE, vsz_none, CPU_ ## e ## _FLAGS, \
973 CPU_ ## d ## _FLAGS }
974 #define VECARCH(n, e, d, v) \
975 { STRING_COMMA_LEN (#n), false, PROCESSOR_NONE, vsz_ ## v, \
976 CPU_ ## e ## _FLAGS, CPU_ ## d ## _FLAGS }
977
978 static const arch_entry cpu_arch[] =
979 {
980 /* Do not replace the first two entries - i386_target_format() and
981 set_cpu_arch() rely on them being there in this order. */
982 ARCH (generic32, GENERIC32, GENERIC32, false),
983 ARCH (generic64, GENERIC64, GENERIC64, false),
984 ARCH (i8086, UNKNOWN, NONE, false),
985 ARCH (i186, UNKNOWN, 186, false),
986 ARCH (i286, UNKNOWN, 286, false),
987 ARCH (i386, I386, 386, false),
988 ARCH (i486, I486, 486, false),
989 ARCH (i586, PENTIUM, 586, false),
990 ARCH (pentium, PENTIUM, 586, false),
991 ARCH (i686, I686, 686, false),
992 ARCH (pentiumpro, PENTIUMPRO, PENTIUMPRO, false),
993 ARCH (pentiumii, PENTIUMPRO, P2, false),
994 ARCH (pentiumiii, PENTIUMPRO, P3, false),
995 ARCH (pentium4, PENTIUM4, P4, false),
996 ARCH (prescott, NOCONA, CORE, false),
997 ARCH (nocona, NOCONA, NOCONA, false),
998 ARCH (yonah, CORE, CORE, true),
999 ARCH (core, CORE, CORE, false),
1000 ARCH (merom, CORE2, CORE2, true),
1001 ARCH (core2, CORE2, CORE2, false),
1002 ARCH (corei7, COREI7, COREI7, false),
1003 ARCH (iamcu, IAMCU, IAMCU, false),
1004 ARCH (k6, K6, K6, false),
1005 ARCH (k6_2, K6, K6_2, false),
1006 ARCH (athlon, ATHLON, ATHLON, false),
1007 ARCH (sledgehammer, K8, K8, true),
1008 ARCH (opteron, K8, K8, false),
1009 ARCH (k8, K8, K8, false),
1010 ARCH (amdfam10, AMDFAM10, AMDFAM10, false),
1011 ARCH (bdver1, BD, BDVER1, false),
1012 ARCH (bdver2, BD, BDVER2, false),
1013 ARCH (bdver3, BD, BDVER3, false),
1014 ARCH (bdver4, BD, BDVER4, false),
1015 ARCH (znver1, ZNVER, ZNVER1, false),
1016 ARCH (znver2, ZNVER, ZNVER2, false),
1017 ARCH (znver3, ZNVER, ZNVER3, false),
1018 ARCH (znver4, ZNVER, ZNVER4, false),
1019 ARCH (btver1, BT, BTVER1, false),
1020 ARCH (btver2, BT, BTVER2, false),
1021
1022 SUBARCH (8087, 8087, ANY_8087, false),
1023 SUBARCH (87, NONE, ANY_8087, false), /* Disable only! */
1024 SUBARCH (287, 287, ANY_287, false),
1025 SUBARCH (387, 387, ANY_387, false),
1026 SUBARCH (687, 687, ANY_687, false),
1027 SUBARCH (cmov, CMOV, CMOV, false),
1028 SUBARCH (fxsr, FXSR, ANY_FXSR, false),
1029 SUBARCH (mmx, MMX, ANY_MMX, false),
1030 SUBARCH (sse, SSE, ANY_SSE, false),
1031 SUBARCH (sse2, SSE2, ANY_SSE2, false),
1032 SUBARCH (sse3, SSE3, ANY_SSE3, false),
1033 SUBARCH (sse4a, SSE4A, ANY_SSE4A, false),
1034 SUBARCH (ssse3, SSSE3, ANY_SSSE3, false),
1035 SUBARCH (sse4.1, SSE4_1, ANY_SSE4_1, false),
1036 SUBARCH (sse4.2, SSE4_2, ANY_SSE4_2, false),
1037 SUBARCH (sse4, SSE4_2, ANY_SSE4_1, false),
1038 VECARCH (avx, AVX, ANY_AVX, reset),
1039 VECARCH (avx2, AVX2, ANY_AVX2, reset),
1040 VECARCH (avx512f, AVX512F, ANY_AVX512F, reset),
1041 VECARCH (avx512cd, AVX512CD, ANY_AVX512CD, reset),
1042 VECARCH (avx512er, AVX512ER, ANY_AVX512ER, reset),
1043 VECARCH (avx512pf, AVX512PF, ANY_AVX512PF, reset),
1044 VECARCH (avx512dq, AVX512DQ, ANY_AVX512DQ, reset),
1045 VECARCH (avx512bw, AVX512BW, ANY_AVX512BW, reset),
1046 VECARCH (avx512vl, AVX512VL, ANY_AVX512VL, reset),
1047 SUBARCH (monitor, MONITOR, MONITOR, false),
1048 SUBARCH (vmx, VMX, ANY_VMX, false),
1049 SUBARCH (vmfunc, VMFUNC, ANY_VMFUNC, false),
1050 SUBARCH (smx, SMX, SMX, false),
1051 SUBARCH (xsave, XSAVE, ANY_XSAVE, false),
1052 SUBARCH (xsaveopt, XSAVEOPT, ANY_XSAVEOPT, false),
1053 SUBARCH (xsavec, XSAVEC, ANY_XSAVEC, false),
1054 SUBARCH (xsaves, XSAVES, ANY_XSAVES, false),
1055 SUBARCH (aes, AES, ANY_AES, false),
1056 SUBARCH (pclmul, PCLMULQDQ, ANY_PCLMULQDQ, false),
1057 SUBARCH (clmul, PCLMULQDQ, ANY_PCLMULQDQ, true),
1058 SUBARCH (fsgsbase, FSGSBASE, FSGSBASE, false),
1059 SUBARCH (rdrnd, RDRND, RDRND, false),
1060 SUBARCH (f16c, F16C, ANY_F16C, false),
1061 SUBARCH (bmi2, BMI2, BMI2, false),
1062 SUBARCH (fma, FMA, ANY_FMA, false),
1063 SUBARCH (fma4, FMA4, ANY_FMA4, false),
1064 SUBARCH (xop, XOP, ANY_XOP, false),
1065 SUBARCH (lwp, LWP, ANY_LWP, false),
1066 SUBARCH (movbe, MOVBE, MOVBE, false),
1067 SUBARCH (cx16, CX16, CX16, false),
1068 SUBARCH (lahf_sahf, LAHF_SAHF, LAHF_SAHF, false),
1069 SUBARCH (ept, EPT, ANY_EPT, false),
1070 SUBARCH (lzcnt, LZCNT, LZCNT, false),
1071 SUBARCH (popcnt, POPCNT, POPCNT, false),
1072 SUBARCH (hle, HLE, HLE, false),
1073 SUBARCH (rtm, RTM, ANY_RTM, false),
1074 SUBARCH (tsx, TSX, TSX, false),
1075 SUBARCH (invpcid, INVPCID, INVPCID, false),
1076 SUBARCH (clflush, CLFLUSH, CLFLUSH, false),
1077 SUBARCH (nop, NOP, NOP, false),
1078 SUBARCH (syscall, SYSCALL, SYSCALL, false),
1079 SUBARCH (rdtscp, RDTSCP, RDTSCP, false),
1080 SUBARCH (3dnow, 3DNOW, ANY_3DNOW, false),
1081 SUBARCH (3dnowa, 3DNOWA, ANY_3DNOWA, false),
1082 SUBARCH (padlock, PADLOCK, PADLOCK, false),
1083 SUBARCH (pacifica, SVME, ANY_SVME, true),
1084 SUBARCH (svme, SVME, ANY_SVME, false),
1085 SUBARCH (abm, ABM, ABM, false),
1086 SUBARCH (bmi, BMI, BMI, false),
1087 SUBARCH (tbm, TBM, TBM, false),
1088 SUBARCH (adx, ADX, ADX, false),
1089 SUBARCH (rdseed, RDSEED, RDSEED, false),
1090 SUBARCH (prfchw, PRFCHW, PRFCHW, false),
1091 SUBARCH (smap, SMAP, SMAP, false),
1092 SUBARCH (mpx, MPX, ANY_MPX, false),
1093 SUBARCH (sha, SHA, ANY_SHA, false),
1094 SUBARCH (clflushopt, CLFLUSHOPT, CLFLUSHOPT, false),
1095 SUBARCH (prefetchwt1, PREFETCHWT1, PREFETCHWT1, false),
1096 SUBARCH (se1, SE1, SE1, false),
1097 SUBARCH (clwb, CLWB, CLWB, false),
1098 VECARCH (avx512ifma, AVX512IFMA, ANY_AVX512IFMA, reset),
1099 VECARCH (avx512vbmi, AVX512VBMI, ANY_AVX512VBMI, reset),
1100 VECARCH (avx512_4fmaps, AVX512_4FMAPS, ANY_AVX512_4FMAPS, reset),
1101 VECARCH (avx512_4vnniw, AVX512_4VNNIW, ANY_AVX512_4VNNIW, reset),
1102 VECARCH (avx512_vpopcntdq, AVX512_VPOPCNTDQ, ANY_AVX512_VPOPCNTDQ, reset),
1103 VECARCH (avx512_vbmi2, AVX512_VBMI2, ANY_AVX512_VBMI2, reset),
1104 VECARCH (avx512_vnni, AVX512_VNNI, ANY_AVX512_VNNI, reset),
1105 VECARCH (avx512_bitalg, AVX512_BITALG, ANY_AVX512_BITALG, reset),
1106 VECARCH (avx_vnni, AVX_VNNI, ANY_AVX_VNNI, reset),
1107 SUBARCH (clzero, CLZERO, CLZERO, false),
1108 SUBARCH (mwaitx, MWAITX, MWAITX, false),
1109 SUBARCH (ospke, OSPKE, ANY_OSPKE, false),
1110 SUBARCH (rdpid, RDPID, RDPID, false),
1111 SUBARCH (ptwrite, PTWRITE, PTWRITE, false),
1112 SUBARCH (ibt, IBT, IBT, false),
1113 SUBARCH (shstk, SHSTK, SHSTK, false),
1114 SUBARCH (gfni, GFNI, ANY_GFNI, false),
1115 VECARCH (vaes, VAES, ANY_VAES, reset),
1116 VECARCH (vpclmulqdq, VPCLMULQDQ, ANY_VPCLMULQDQ, reset),
1117 SUBARCH (wbnoinvd, WBNOINVD, WBNOINVD, false),
1118 SUBARCH (pconfig, PCONFIG, PCONFIG, false),
1119 SUBARCH (waitpkg, WAITPKG, WAITPKG, false),
1120 SUBARCH (cldemote, CLDEMOTE, CLDEMOTE, false),
1121 SUBARCH (amx_int8, AMX_INT8, ANY_AMX_INT8, false),
1122 SUBARCH (amx_bf16, AMX_BF16, ANY_AMX_BF16, false),
1123 SUBARCH (amx_fp16, AMX_FP16, ANY_AMX_FP16, false),
1124 SUBARCH (amx_complex, AMX_COMPLEX, ANY_AMX_COMPLEX, false),
1125 SUBARCH (amx_tile, AMX_TILE, ANY_AMX_TILE, false),
1126 SUBARCH (movdiri, MOVDIRI, MOVDIRI, false),
1127 SUBARCH (movdir64b, MOVDIR64B, MOVDIR64B, false),
1128 VECARCH (avx512_bf16, AVX512_BF16, ANY_AVX512_BF16, reset),
1129 VECARCH (avx512_vp2intersect, AVX512_VP2INTERSECT,
1130 ANY_AVX512_VP2INTERSECT, reset),
1131 SUBARCH (tdx, TDX, TDX, false),
1132 SUBARCH (enqcmd, ENQCMD, ENQCMD, false),
1133 SUBARCH (serialize, SERIALIZE, SERIALIZE, false),
1134 SUBARCH (rdpru, RDPRU, RDPRU, false),
1135 SUBARCH (mcommit, MCOMMIT, MCOMMIT, false),
1136 SUBARCH (sev_es, SEV_ES, ANY_SEV_ES, false),
1137 SUBARCH (tsxldtrk, TSXLDTRK, ANY_TSXLDTRK, false),
1138 SUBARCH (kl, KL, ANY_KL, false),
1139 SUBARCH (widekl, WIDEKL, ANY_WIDEKL, false),
1140 SUBARCH (uintr, UINTR, UINTR, false),
1141 SUBARCH (hreset, HRESET, HRESET, false),
1142 VECARCH (avx512_fp16, AVX512_FP16, ANY_AVX512_FP16, reset),
1143 SUBARCH (prefetchi, PREFETCHI, PREFETCHI, false),
1144 VECARCH (avx_ifma, AVX_IFMA, ANY_AVX_IFMA, reset),
1145 VECARCH (avx_vnni_int8, AVX_VNNI_INT8, ANY_AVX_VNNI_INT8, reset),
1146 SUBARCH (cmpccxadd, CMPCCXADD, CMPCCXADD, false),
1147 SUBARCH (wrmsrns, WRMSRNS, WRMSRNS, false),
1148 SUBARCH (msrlist, MSRLIST, MSRLIST, false),
1149 VECARCH (avx_ne_convert, AVX_NE_CONVERT, ANY_AVX_NE_CONVERT, reset),
1150 SUBARCH (rao_int, RAO_INT, RAO_INT, false),
1151 SUBARCH (rmpquery, RMPQUERY, ANY_RMPQUERY, false),
1152 SUBARCH (fred, FRED, ANY_FRED, false),
1153 SUBARCH (lkgs, LKGS, ANY_LKGS, false),
1154 VECARCH (avx_vnni_int16, AVX_VNNI_INT16, ANY_AVX_VNNI_INT16, reset),
1155 VECARCH (sha512, SHA512, ANY_SHA512, reset),
1156 VECARCH (sm3, SM3, ANY_SM3, reset),
1157 VECARCH (sm4, SM4, ANY_SM4, reset),
1158 SUBARCH (pbndkb, PBNDKB, PBNDKB, false),
1159 VECARCH (avx10.1, AVX10_1, ANY_AVX512F, set),
1160 };
1161
1162 #undef SUBARCH
1163 #undef ARCH
1164
1165 #ifdef I386COFF
1166 /* Like s_lcomm_internal in gas/read.c but the alignment string
1167 is allowed to be optional. */
1168
1169 static symbolS *
1170 pe_lcomm_internal (int needs_align, symbolS *symbolP, addressT size)
1171 {
1172 addressT align = 0;
1173
1174 SKIP_WHITESPACE ();
1175
1176 if (needs_align
1177 && *input_line_pointer == ',')
1178 {
1179 align = parse_align (needs_align - 1);
1180
1181 if (align == (addressT) -1)
1182 return NULL;
1183 }
1184 else
1185 {
1186 if (size >= 8)
1187 align = 3;
1188 else if (size >= 4)
1189 align = 2;
1190 else if (size >= 2)
1191 align = 1;
1192 else
1193 align = 0;
1194 }
1195
1196 bss_alloc (symbolP, size, align);
1197 return symbolP;
1198 }
1199
1200 static void
1201 pe_lcomm (int needs_align)
1202 {
1203 s_comm_internal (needs_align * 2, pe_lcomm_internal);
1204 }
1205 #endif
1206
1207 const pseudo_typeS md_pseudo_table[] =
1208 {
1209 #if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
1210 {"align", s_align_bytes, 0},
1211 #else
1212 {"align", s_align_ptwo, 0},
1213 #endif
1214 {"arch", set_cpu_arch, 0},
1215 #ifndef I386COFF
1216 {"bss", s_bss, 0},
1217 #else
1218 {"lcomm", pe_lcomm, 1},
1219 #endif
1220 {"ffloat", float_cons, 'f'},
1221 {"dfloat", float_cons, 'd'},
1222 {"tfloat", float_cons, 'x'},
1223 {"hfloat", float_cons, 'h'},
1224 {"bfloat16", float_cons, 'b'},
1225 {"value", cons, 2},
1226 {"slong", signed_cons, 4},
1227 {"insn", s_insn, 0},
1228 {"noopt", s_ignore, 0},
1229 {"optim", s_ignore, 0},
1230 {"code16gcc", set_16bit_gcc_code_flag, CODE_16BIT},
1231 {"code16", set_code_flag, CODE_16BIT},
1232 {"code32", set_code_flag, CODE_32BIT},
1233 #ifdef BFD64
1234 {"code64", set_code_flag, CODE_64BIT},
1235 #endif
1236 {"intel_syntax", set_intel_syntax, 1},
1237 {"att_syntax", set_intel_syntax, 0},
1238 {"intel_mnemonic", set_intel_mnemonic, 1},
1239 {"att_mnemonic", set_intel_mnemonic, 0},
1240 {"allow_index_reg", set_allow_index_reg, 1},
1241 {"disallow_index_reg", set_allow_index_reg, 0},
1242 {"sse_check", set_check, 0},
1243 {"operand_check", set_check, 1},
1244 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1245 {"largecomm", handle_large_common, 0},
1246 #else
1247 {"file", dwarf2_directive_file, 0},
1248 {"loc", dwarf2_directive_loc, 0},
1249 {"loc_mark_labels", dwarf2_directive_loc_mark_labels, 0},
1250 #endif
1251 #ifdef TE_PE
1252 {"secrel32", pe_directive_secrel, 0},
1253 {"secidx", pe_directive_secidx, 0},
1254 #endif
1255 {0, 0, 0}
1256 };
1257
1258 /* For interface with expression (). */
1259 extern char *input_line_pointer;
1260
1261 /* Hash table for instruction mnemonic lookup. */
1262 static htab_t op_hash;
1263
1264 /* Hash table for register lookup. */
1265 static htab_t reg_hash;
1266 \f
1267 /* Various efficient no-op patterns for aligning code labels.
1268 Note: Don't try to assemble the instructions in the comments.
1269 0L and 0w are not legal. */
1270 static const unsigned char f32_1[] =
1271 {0x90}; /* nop */
1272 static const unsigned char f32_2[] =
1273 {0x66,0x90}; /* xchg %ax,%ax */
1274 static const unsigned char f32_3[] =
1275 {0x8d,0x76,0x00}; /* leal 0(%esi),%esi */
1276 #define f32_4 (f32_5 + 1) /* leal 0(%esi,%eiz),%esi */
1277 static const unsigned char f32_5[] =
1278 {0x2e,0x8d,0x74,0x26,0x00}; /* leal %cs:0(%esi,%eiz),%esi */
1279 static const unsigned char f32_6[] =
1280 {0x8d,0xb6,0x00,0x00,0x00,0x00}; /* leal 0L(%esi),%esi */
1281 #define f32_7 (f32_8 + 1) /* leal 0L(%esi,%eiz),%esi */
1282 static const unsigned char f32_8[] =
1283 {0x2e,0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal %cs:0L(%esi,%eiz),%esi */
1284 static const unsigned char f64_3[] =
1285 {0x48,0x89,0xf6}; /* mov %rsi,%rsi */
1286 static const unsigned char f64_4[] =
1287 {0x48,0x8d,0x76,0x00}; /* lea 0(%rsi),%rsi */
1288 #define f64_5 (f64_6 + 1) /* lea 0(%rsi,%riz),%rsi */
1289 static const unsigned char f64_6[] =
1290 {0x2e,0x48,0x8d,0x74,0x26,0x00}; /* lea %cs:0(%rsi,%riz),%rsi */
1291 static const unsigned char f64_7[] =
1292 {0x48,0x8d,0xb6,0x00,0x00,0x00,0x00}; /* lea 0L(%rsi),%rsi */
1293 #define f64_8 (f64_9 + 1) /* lea 0L(%rsi,%riz),%rsi */
1294 static const unsigned char f64_9[] =
1295 {0x2e,0x48,0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* lea %cs:0L(%rsi,%riz),%rsi */
1296 #define f16_2 (f64_3 + 1) /* mov %si,%si */
1297 static const unsigned char f16_3[] =
1298 {0x8d,0x74,0x00}; /* lea 0(%si),%si */
1299 #define f16_4 (f16_5 + 1) /* lea 0W(%si),%si */
1300 static const unsigned char f16_5[] =
1301 {0x2e,0x8d,0xb4,0x00,0x00}; /* lea %cs:0W(%si),%si */
1302 static const unsigned char jump_disp8[] =
1303 {0xeb}; /* jmp disp8 */
1304 static const unsigned char jump32_disp32[] =
1305 {0xe9}; /* jmp disp32 */
1306 static const unsigned char jump16_disp32[] =
1307 {0x66,0xe9}; /* jmp disp32 */
1308 /* 32-bit NOPs patterns. */
1309 static const unsigned char *const f32_patt[] = {
1310 f32_1, f32_2, f32_3, f32_4, f32_5, f32_6, f32_7, f32_8
1311 };
1312 /* 64-bit NOPs patterns. */
1313 static const unsigned char *const f64_patt[] = {
1314 f32_1, f32_2, f64_3, f64_4, f64_5, f64_6, f64_7, f64_8, f64_9
1315 };
1316 /* 16-bit NOPs patterns. */
1317 static const unsigned char *const f16_patt[] = {
1318 f32_1, f16_2, f16_3, f16_4, f16_5
1319 };
1320 /* nopl (%[re]ax) */
1321 static const unsigned char alt_3[] =
1322 {0x0f,0x1f,0x00};
1323 /* nopl 0(%[re]ax) */
1324 static const unsigned char alt_4[] =
1325 {0x0f,0x1f,0x40,0x00};
1326 /* nopl 0(%[re]ax,%[re]ax,1) */
1327 #define alt_5 (alt_6 + 1)
1328 /* nopw 0(%[re]ax,%[re]ax,1) */
1329 static const unsigned char alt_6[] =
1330 {0x66,0x0f,0x1f,0x44,0x00,0x00};
1331 /* nopl 0L(%[re]ax) */
1332 static const unsigned char alt_7[] =
1333 {0x0f,0x1f,0x80,0x00,0x00,0x00,0x00};
1334 /* nopl 0L(%[re]ax,%[re]ax,1) */
1335 #define alt_8 (alt_9 + 1)
1336 /* nopw 0L(%[re]ax,%[re]ax,1) */
1337 static const unsigned char alt_9[] =
1338 {0x66,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1339 /* nopw %cs:0L(%[re]ax,%[re]ax,1) */
1340 #define alt_10 (alt_11 + 1)
1341 /* data16 nopw %cs:0L(%eax,%eax,1) */
1342 static const unsigned char alt_11[] =
1343 {0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1344 /* 32-bit and 64-bit NOPs patterns. */
1345 static const unsigned char *const alt_patt[] = {
1346 f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
1347 alt_9, alt_10, alt_11
1348 };
1349
1350 /* Genenerate COUNT bytes of NOPs to WHERE from PATT with the maximum
1351 size of a single NOP instruction MAX_SINGLE_NOP_SIZE. */
1352
1353 static void
1354 i386_output_nops (char *where, const unsigned char *const *patt,
1355 int count, int max_single_nop_size)
1356
1357 {
1358 /* Place the longer NOP first. */
1359 int last;
1360 int offset;
1361 const unsigned char *nops;
1362
1363 if (max_single_nop_size < 1)
1364 {
1365 as_fatal (_("i386_output_nops called to generate nops of at most %d bytes!"),
1366 max_single_nop_size);
1367 return;
1368 }
1369
1370 nops = patt[max_single_nop_size - 1];
1371 last = count % max_single_nop_size;
1372
1373 count -= last;
1374 for (offset = 0; offset < count; offset += max_single_nop_size)
1375 memcpy (where + offset, nops, max_single_nop_size);
1376
1377 if (last)
1378 {
1379 nops = patt[last - 1];
1380 memcpy (where + offset, nops, last);
1381 }
1382 }
1383
1384 static INLINE int
1385 fits_in_imm7 (offsetT num)
1386 {
1387 return (num & 0x7f) == num;
1388 }
1389
1390 static INLINE int
1391 fits_in_imm31 (offsetT num)
1392 {
1393 return (num & 0x7fffffff) == num;
1394 }
1395
1396 /* Genenerate COUNT bytes of NOPs to WHERE with the maximum size of a
1397 single NOP instruction LIMIT. */
1398
1399 void
1400 i386_generate_nops (fragS *fragP, char *where, offsetT count, int limit)
1401 {
1402 const unsigned char *const *patt = NULL;
1403 int max_single_nop_size;
1404 /* Maximum number of NOPs before switching to jump over NOPs. */
1405 int max_number_of_nops;
1406
1407 switch (fragP->fr_type)
1408 {
1409 case rs_fill_nop:
1410 case rs_align_code:
1411 break;
1412 case rs_machine_dependent:
1413 /* Allow NOP padding for jumps and calls. */
1414 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
1415 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
1416 break;
1417 /* Fall through. */
1418 default:
1419 return;
1420 }
1421
1422 /* We need to decide which NOP sequence to use for 32bit and
1423 64bit. When -mtune= is used:
1424
1425 1. For PROCESSOR_I?86, PROCESSOR_PENTIUM, PROCESSOR_IAMCU, and
1426 PROCESSOR_GENERIC32, f32_patt will be used.
1427 2. For the rest, alt_patt will be used.
1428
1429 When -mtune= isn't used, alt_patt will be used if
1430 cpu_arch_isa_flags has CpuNop. Otherwise, f32_patt/f64_patt will
1431 be used.
1432
1433 When -march= or .arch is used, we can't use anything beyond
1434 cpu_arch_isa_flags. */
1435
1436 if (fragP->tc_frag_data.code == CODE_16BIT)
1437 {
1438 patt = f16_patt;
1439 max_single_nop_size = sizeof (f16_patt) / sizeof (f16_patt[0]);
1440 /* Limit number of NOPs to 2 in 16-bit mode. */
1441 max_number_of_nops = 2;
1442 }
1443 else
1444 {
1445 patt = fragP->tc_frag_data.code == CODE_64BIT ? f64_patt : f32_patt;
1446 if (fragP->tc_frag_data.isa == PROCESSOR_UNKNOWN)
1447 {
1448 /* PROCESSOR_UNKNOWN means that all ISAs may be used, unless
1449 explicitly disabled. */
1450 switch (fragP->tc_frag_data.tune)
1451 {
1452 case PROCESSOR_UNKNOWN:
1453 /* We use cpu_arch_isa_flags to check if we SHOULD
1454 optimize with nops. */
1455 if (fragP->tc_frag_data.isanop)
1456 patt = alt_patt;
1457 break;
1458
1459 case PROCESSOR_PENTIUMPRO:
1460 case PROCESSOR_PENTIUM4:
1461 case PROCESSOR_NOCONA:
1462 case PROCESSOR_CORE:
1463 case PROCESSOR_CORE2:
1464 case PROCESSOR_COREI7:
1465 case PROCESSOR_GENERIC64:
1466 case PROCESSOR_K6:
1467 case PROCESSOR_ATHLON:
1468 case PROCESSOR_K8:
1469 case PROCESSOR_AMDFAM10:
1470 case PROCESSOR_BD:
1471 case PROCESSOR_ZNVER:
1472 case PROCESSOR_BT:
1473 if (fragP->tc_frag_data.cpunop)
1474 patt = alt_patt;
1475 break;
1476
1477 case PROCESSOR_I386:
1478 case PROCESSOR_I486:
1479 case PROCESSOR_PENTIUM:
1480 case PROCESSOR_I686:
1481 case PROCESSOR_IAMCU:
1482 case PROCESSOR_GENERIC32:
1483 break;
1484 case PROCESSOR_NONE:
1485 abort ();
1486 }
1487 }
1488 else
1489 {
1490 switch (fragP->tc_frag_data.tune)
1491 {
1492 case PROCESSOR_UNKNOWN:
1493 /* When cpu_arch_isa is set, cpu_arch_tune shouldn't be
1494 PROCESSOR_UNKNOWN. */
1495 abort ();
1496 break;
1497
1498 default:
1499 /* We use cpu_arch_isa_flags to check if we CAN optimize
1500 with nops. */
1501 if (fragP->tc_frag_data.isanop)
1502 patt = alt_patt;
1503 break;
1504
1505 case PROCESSOR_NONE:
1506 abort ();
1507 }
1508 }
1509
1510 if (patt != alt_patt)
1511 {
1512 max_single_nop_size = patt == f32_patt ? ARRAY_SIZE (f32_patt)
1513 : ARRAY_SIZE (f64_patt);
1514 /* Limit number of NOPs to 2 for older processors. */
1515 max_number_of_nops = 2;
1516 }
1517 else
1518 {
1519 max_single_nop_size = sizeof (alt_patt) / sizeof (alt_patt[0]);
1520 /* Limit number of NOPs to 7 for newer processors. */
1521 max_number_of_nops = 7;
1522 }
1523 }
1524
1525 if (limit == 0)
1526 limit = max_single_nop_size;
1527
1528 if (fragP->fr_type == rs_fill_nop)
1529 {
1530 /* Output NOPs for .nop directive. */
1531 if (limit > max_single_nop_size)
1532 {
1533 as_bad_where (fragP->fr_file, fragP->fr_line,
1534 _("invalid single nop size: %d "
1535 "(expect within [0, %d])"),
1536 limit, max_single_nop_size);
1537 return;
1538 }
1539 }
1540 else if (fragP->fr_type != rs_machine_dependent)
1541 fragP->fr_var = count;
1542
1543 if ((count / max_single_nop_size) > max_number_of_nops)
1544 {
1545 /* Generate jump over NOPs. */
1546 offsetT disp = count - 2;
1547 if (fits_in_imm7 (disp))
1548 {
1549 /* Use "jmp disp8" if possible. */
1550 count = disp;
1551 where[0] = jump_disp8[0];
1552 where[1] = count;
1553 where += 2;
1554 }
1555 else
1556 {
1557 unsigned int size_of_jump;
1558
1559 if (flag_code == CODE_16BIT)
1560 {
1561 where[0] = jump16_disp32[0];
1562 where[1] = jump16_disp32[1];
1563 size_of_jump = 2;
1564 }
1565 else
1566 {
1567 where[0] = jump32_disp32[0];
1568 size_of_jump = 1;
1569 }
1570
1571 count -= size_of_jump + 4;
1572 if (!fits_in_imm31 (count))
1573 {
1574 as_bad_where (fragP->fr_file, fragP->fr_line,
1575 _("jump over nop padding out of range"));
1576 return;
1577 }
1578
1579 md_number_to_chars (where + size_of_jump, count, 4);
1580 where += size_of_jump + 4;
1581 }
1582 }
1583
1584 /* Generate multiple NOPs. */
1585 i386_output_nops (where, patt, count, limit);
1586 }
1587
1588 static INLINE int
1589 operand_type_all_zero (const union i386_operand_type *x)
1590 {
1591 switch (ARRAY_SIZE(x->array))
1592 {
1593 case 3:
1594 if (x->array[2])
1595 return 0;
1596 /* Fall through. */
1597 case 2:
1598 if (x->array[1])
1599 return 0;
1600 /* Fall through. */
1601 case 1:
1602 return !x->array[0];
1603 default:
1604 abort ();
1605 }
1606 }
1607
1608 static INLINE void
1609 operand_type_set (union i386_operand_type *x, unsigned int v)
1610 {
1611 switch (ARRAY_SIZE(x->array))
1612 {
1613 case 3:
1614 x->array[2] = v;
1615 /* Fall through. */
1616 case 2:
1617 x->array[1] = v;
1618 /* Fall through. */
1619 case 1:
1620 x->array[0] = v;
1621 /* Fall through. */
1622 break;
1623 default:
1624 abort ();
1625 }
1626
1627 x->bitfield.class = ClassNone;
1628 x->bitfield.instance = InstanceNone;
1629 }
1630
1631 static INLINE int
1632 operand_type_equal (const union i386_operand_type *x,
1633 const union i386_operand_type *y)
1634 {
1635 switch (ARRAY_SIZE(x->array))
1636 {
1637 case 3:
1638 if (x->array[2] != y->array[2])
1639 return 0;
1640 /* Fall through. */
1641 case 2:
1642 if (x->array[1] != y->array[1])
1643 return 0;
1644 /* Fall through. */
1645 case 1:
1646 return x->array[0] == y->array[0];
1647 break;
1648 default:
1649 abort ();
1650 }
1651 }
1652
1653 static INLINE bool
1654 is_cpu (const insn_template *t, enum i386_cpu cpu)
1655 {
1656 switch (cpu)
1657 {
1658 case Cpu287: return t->cpu.bitfield.cpu287;
1659 case Cpu387: return t->cpu.bitfield.cpu387;
1660 case Cpu3dnow: return t->cpu.bitfield.cpu3dnow;
1661 case Cpu3dnowA: return t->cpu.bitfield.cpu3dnowa;
1662 case CpuAVX: return t->cpu.bitfield.cpuavx;
1663 case CpuHLE: return t->cpu.bitfield.cpuhle;
1664 case CpuAVX512F: return t->cpu.bitfield.cpuavx512f;
1665 case CpuAVX512VL: return t->cpu.bitfield.cpuavx512vl;
1666 case Cpu64: return t->cpu.bitfield.cpu64;
1667 case CpuNo64: return t->cpu.bitfield.cpuno64;
1668 default:
1669 gas_assert (cpu < CpuAttrEnums);
1670 }
1671 return t->cpu.bitfield.isa == cpu + 1u;
1672 }
1673
1674 static i386_cpu_flags cpu_flags_from_attr (i386_cpu_attr a)
1675 {
1676 const unsigned int bps = sizeof (a.array[0]) * CHAR_BIT;
1677 i386_cpu_flags f = { .array[0] = 0 };
1678
1679 switch (ARRAY_SIZE(a.array))
1680 {
1681 case 1:
1682 f.array[CpuAttrEnums / bps]
1683 |= (a.array[0] >> CpuIsaBits) << (CpuAttrEnums % bps);
1684 if (CpuAttrEnums % bps > CpuIsaBits)
1685 f.array[CpuAttrEnums / bps + 1]
1686 = (a.array[0] >> CpuIsaBits) >> (bps - CpuAttrEnums % bps);
1687 break;
1688 default:
1689 abort ();
1690 }
1691
1692 if (a.bitfield.isa)
1693 f.array[(a.bitfield.isa - 1) / bps] |= 1u << ((a.bitfield.isa - 1) % bps);
1694
1695 return f;
1696 }
1697
1698 static INLINE int
1699 cpu_flags_all_zero (const union i386_cpu_flags *x)
1700 {
1701 switch (ARRAY_SIZE(x->array))
1702 {
1703 case 5:
1704 if (x->array[4])
1705 return 0;
1706 /* Fall through. */
1707 case 4:
1708 if (x->array[3])
1709 return 0;
1710 /* Fall through. */
1711 case 3:
1712 if (x->array[2])
1713 return 0;
1714 /* Fall through. */
1715 case 2:
1716 if (x->array[1])
1717 return 0;
1718 /* Fall through. */
1719 case 1:
1720 return !x->array[0];
1721 default:
1722 abort ();
1723 }
1724 }
1725
1726 static INLINE int
1727 cpu_flags_equal (const union i386_cpu_flags *x,
1728 const union i386_cpu_flags *y)
1729 {
1730 switch (ARRAY_SIZE(x->array))
1731 {
1732 case 5:
1733 if (x->array[4] != y->array[4])
1734 return 0;
1735 /* Fall through. */
1736 case 4:
1737 if (x->array[3] != y->array[3])
1738 return 0;
1739 /* Fall through. */
1740 case 3:
1741 if (x->array[2] != y->array[2])
1742 return 0;
1743 /* Fall through. */
1744 case 2:
1745 if (x->array[1] != y->array[1])
1746 return 0;
1747 /* Fall through. */
1748 case 1:
1749 return x->array[0] == y->array[0];
1750 break;
1751 default:
1752 abort ();
1753 }
1754 }
1755
1756 static INLINE int
1757 cpu_flags_check_cpu64 (const insn_template *t)
1758 {
1759 return flag_code == CODE_64BIT
1760 ? !t->cpu.bitfield.cpuno64
1761 : !t->cpu.bitfield.cpu64;
1762 }
1763
1764 static INLINE i386_cpu_flags
1765 cpu_flags_and (i386_cpu_flags x, i386_cpu_flags y)
1766 {
1767 switch (ARRAY_SIZE (x.array))
1768 {
1769 case 5:
1770 x.array [4] &= y.array [4];
1771 /* Fall through. */
1772 case 4:
1773 x.array [3] &= y.array [3];
1774 /* Fall through. */
1775 case 3:
1776 x.array [2] &= y.array [2];
1777 /* Fall through. */
1778 case 2:
1779 x.array [1] &= y.array [1];
1780 /* Fall through. */
1781 case 1:
1782 x.array [0] &= y.array [0];
1783 break;
1784 default:
1785 abort ();
1786 }
1787 return x;
1788 }
1789
1790 static INLINE i386_cpu_flags
1791 cpu_flags_or (i386_cpu_flags x, i386_cpu_flags y)
1792 {
1793 switch (ARRAY_SIZE (x.array))
1794 {
1795 case 5:
1796 x.array [4] |= y.array [4];
1797 /* Fall through. */
1798 case 4:
1799 x.array [3] |= y.array [3];
1800 /* Fall through. */
1801 case 3:
1802 x.array [2] |= y.array [2];
1803 /* Fall through. */
1804 case 2:
1805 x.array [1] |= y.array [1];
1806 /* Fall through. */
1807 case 1:
1808 x.array [0] |= y.array [0];
1809 break;
1810 default:
1811 abort ();
1812 }
1813 return x;
1814 }
1815
1816 static INLINE i386_cpu_flags
1817 cpu_flags_and_not (i386_cpu_flags x, i386_cpu_flags y)
1818 {
1819 switch (ARRAY_SIZE (x.array))
1820 {
1821 case 5:
1822 x.array [4] &= ~y.array [4];
1823 /* Fall through. */
1824 case 4:
1825 x.array [3] &= ~y.array [3];
1826 /* Fall through. */
1827 case 3:
1828 x.array [2] &= ~y.array [2];
1829 /* Fall through. */
1830 case 2:
1831 x.array [1] &= ~y.array [1];
1832 /* Fall through. */
1833 case 1:
1834 x.array [0] &= ~y.array [0];
1835 break;
1836 default:
1837 abort ();
1838 }
1839 return x;
1840 }
1841
1842 static const i386_cpu_flags avx512 = CPU_ANY_AVX512F_FLAGS;
1843
1844 static INLINE bool need_evex_encoding (void)
1845 {
1846 return i.vec_encoding == vex_encoding_evex
1847 || i.vec_encoding == vex_encoding_evex512
1848 || i.mask.reg;
1849 }
1850
1851 #define CPU_FLAGS_ARCH_MATCH 0x1
1852 #define CPU_FLAGS_64BIT_MATCH 0x2
1853
1854 #define CPU_FLAGS_PERFECT_MATCH \
1855 (CPU_FLAGS_ARCH_MATCH | CPU_FLAGS_64BIT_MATCH)
1856
1857 /* Return CPU flags match bits. */
1858
1859 static int
1860 cpu_flags_match (const insn_template *t)
1861 {
1862 i386_cpu_flags x = cpu_flags_from_attr (t->cpu);
1863 int match = cpu_flags_check_cpu64 (t) ? CPU_FLAGS_64BIT_MATCH : 0;
1864
1865 x.bitfield.cpu64 = 0;
1866 x.bitfield.cpuno64 = 0;
1867
1868 if (cpu_flags_all_zero (&x))
1869 {
1870 /* This instruction is available on all archs. */
1871 match |= CPU_FLAGS_ARCH_MATCH;
1872 }
1873 else
1874 {
1875 /* This instruction is available only on some archs. */
1876 i386_cpu_flags cpu = cpu_arch_flags;
1877
1878 /* Dual VEX/EVEX templates may need stripping of one of the flags. */
1879 if (t->opcode_modifier.vex && t->opcode_modifier.evex)
1880 {
1881 /* Dual AVX/AVX512F templates need to retain AVX512F only if we already
1882 know that EVEX encoding will be needed. */
1883 if ((x.bitfield.cpuavx || x.bitfield.cpuavx2)
1884 && x.bitfield.cpuavx512f)
1885 {
1886 if (need_evex_encoding ())
1887 {
1888 x.bitfield.cpuavx = 0;
1889 x.bitfield.cpuavx2 = 0;
1890 }
1891 /* need_evex_encoding() isn't reliable before operands were
1892 parsed. */
1893 else if (i.operands)
1894 {
1895 x.bitfield.cpuavx512f = 0;
1896 x.bitfield.cpuavx512vl = 0;
1897 if (x.bitfield.cpufma && !cpu.bitfield.cpufma)
1898 x.bitfield.cpuavx = 0;
1899 }
1900 }
1901 }
1902
1903 /* AVX512VL is no standalone feature - match it and then strip it. */
1904 if (x.bitfield.cpuavx512vl && !cpu.bitfield.cpuavx512vl)
1905 return match;
1906 x.bitfield.cpuavx512vl = 0;
1907
1908 /* AVX and AVX2 present at the same time express an operand size
1909 dependency - strip AVX2 for the purposes here. The operand size
1910 dependent check occurs in check_vecOperands(). */
1911 if (x.bitfield.cpuavx && x.bitfield.cpuavx2)
1912 x.bitfield.cpuavx2 = 0;
1913
1914 cpu = cpu_flags_and (x, cpu);
1915 if (!cpu_flags_all_zero (&cpu))
1916 {
1917 if (t->cpu.bitfield.cpuavx && t->cpu.bitfield.cpuavx512f)
1918 {
1919 if ((need_evex_encoding ()
1920 ? cpu.bitfield.cpuavx512f
1921 : cpu.bitfield.cpuavx)
1922 && (!x.bitfield.cpufma || cpu.bitfield.cpufma
1923 || cpu_arch_flags.bitfield.cpuavx512f)
1924 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
1925 && (!x.bitfield.cpuvaes || cpu.bitfield.cpuvaes)
1926 && (!x.bitfield.cpuvpclmulqdq || cpu.bitfield.cpuvpclmulqdq))
1927 match |= CPU_FLAGS_ARCH_MATCH;
1928 }
1929 else if (x.bitfield.cpuavx)
1930 {
1931 /* We need to check a few extra flags with AVX. */
1932 if (cpu.bitfield.cpuavx
1933 && (!t->opcode_modifier.sse2avx
1934 || (sse2avx && !i.prefix[DATA_PREFIX]))
1935 && (!x.bitfield.cpuaes || cpu.bitfield.cpuaes)
1936 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
1937 && (!x.bitfield.cpupclmulqdq || cpu.bitfield.cpupclmulqdq))
1938 match |= CPU_FLAGS_ARCH_MATCH;
1939 }
1940 else if (x.bitfield.cpuavx2 && cpu.bitfield.cpuavx2)
1941 match |= CPU_FLAGS_ARCH_MATCH;
1942 else if (x.bitfield.cpuavx512f)
1943 {
1944 /* We need to check a few extra flags with AVX512F. */
1945 if (cpu.bitfield.cpuavx512f
1946 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni))
1947 match |= CPU_FLAGS_ARCH_MATCH;
1948 }
1949 else
1950 match |= CPU_FLAGS_ARCH_MATCH;
1951 }
1952 }
1953 return match;
1954 }
1955
1956 static INLINE i386_operand_type
1957 operand_type_and (i386_operand_type x, i386_operand_type y)
1958 {
1959 if (x.bitfield.class != y.bitfield.class)
1960 x.bitfield.class = ClassNone;
1961 if (x.bitfield.instance != y.bitfield.instance)
1962 x.bitfield.instance = InstanceNone;
1963
1964 switch (ARRAY_SIZE (x.array))
1965 {
1966 case 3:
1967 x.array [2] &= y.array [2];
1968 /* Fall through. */
1969 case 2:
1970 x.array [1] &= y.array [1];
1971 /* Fall through. */
1972 case 1:
1973 x.array [0] &= y.array [0];
1974 break;
1975 default:
1976 abort ();
1977 }
1978 return x;
1979 }
1980
1981 static INLINE i386_operand_type
1982 operand_type_and_not (i386_operand_type x, i386_operand_type y)
1983 {
1984 gas_assert (y.bitfield.class == ClassNone);
1985 gas_assert (y.bitfield.instance == InstanceNone);
1986
1987 switch (ARRAY_SIZE (x.array))
1988 {
1989 case 3:
1990 x.array [2] &= ~y.array [2];
1991 /* Fall through. */
1992 case 2:
1993 x.array [1] &= ~y.array [1];
1994 /* Fall through. */
1995 case 1:
1996 x.array [0] &= ~y.array [0];
1997 break;
1998 default:
1999 abort ();
2000 }
2001 return x;
2002 }
2003
2004 static INLINE i386_operand_type
2005 operand_type_or (i386_operand_type x, i386_operand_type y)
2006 {
2007 gas_assert (x.bitfield.class == ClassNone ||
2008 y.bitfield.class == ClassNone ||
2009 x.bitfield.class == y.bitfield.class);
2010 gas_assert (x.bitfield.instance == InstanceNone ||
2011 y.bitfield.instance == InstanceNone ||
2012 x.bitfield.instance == y.bitfield.instance);
2013
2014 switch (ARRAY_SIZE (x.array))
2015 {
2016 case 3:
2017 x.array [2] |= y.array [2];
2018 /* Fall through. */
2019 case 2:
2020 x.array [1] |= y.array [1];
2021 /* Fall through. */
2022 case 1:
2023 x.array [0] |= y.array [0];
2024 break;
2025 default:
2026 abort ();
2027 }
2028 return x;
2029 }
2030
2031 static INLINE i386_operand_type
2032 operand_type_xor (i386_operand_type x, i386_operand_type y)
2033 {
2034 gas_assert (y.bitfield.class == ClassNone);
2035 gas_assert (y.bitfield.instance == InstanceNone);
2036
2037 switch (ARRAY_SIZE (x.array))
2038 {
2039 case 3:
2040 x.array [2] ^= y.array [2];
2041 /* Fall through. */
2042 case 2:
2043 x.array [1] ^= y.array [1];
2044 /* Fall through. */
2045 case 1:
2046 x.array [0] ^= y.array [0];
2047 break;
2048 default:
2049 abort ();
2050 }
2051 return x;
2052 }
2053
2054 static const i386_operand_type anydisp = {
2055 .bitfield = { .disp8 = 1, .disp16 = 1, .disp32 = 1, .disp64 = 1 }
2056 };
2057
2058 enum operand_type
2059 {
2060 reg,
2061 imm,
2062 disp,
2063 anymem
2064 };
2065
2066 static INLINE int
2067 operand_type_check (i386_operand_type t, enum operand_type c)
2068 {
2069 switch (c)
2070 {
2071 case reg:
2072 return t.bitfield.class == Reg;
2073
2074 case imm:
2075 return (t.bitfield.imm8
2076 || t.bitfield.imm8s
2077 || t.bitfield.imm16
2078 || t.bitfield.imm32
2079 || t.bitfield.imm32s
2080 || t.bitfield.imm64);
2081
2082 case disp:
2083 return (t.bitfield.disp8
2084 || t.bitfield.disp16
2085 || t.bitfield.disp32
2086 || t.bitfield.disp64);
2087
2088 case anymem:
2089 return (t.bitfield.disp8
2090 || t.bitfield.disp16
2091 || t.bitfield.disp32
2092 || t.bitfield.disp64
2093 || t.bitfield.baseindex);
2094
2095 default:
2096 abort ();
2097 }
2098
2099 return 0;
2100 }
2101
2102 /* Return 1 if there is no conflict in 8bit/16bit/32bit/64bit/80bit size
2103 between operand GIVEN and opeand WANTED for instruction template T. */
2104
2105 static INLINE int
2106 match_operand_size (const insn_template *t, unsigned int wanted,
2107 unsigned int given)
2108 {
2109 return !((i.types[given].bitfield.byte
2110 && !t->operand_types[wanted].bitfield.byte)
2111 || (i.types[given].bitfield.word
2112 && !t->operand_types[wanted].bitfield.word)
2113 || (i.types[given].bitfield.dword
2114 && !t->operand_types[wanted].bitfield.dword)
2115 || (i.types[given].bitfield.qword
2116 && (!t->operand_types[wanted].bitfield.qword
2117 /* Don't allow 64-bit (memory) operands outside of 64-bit
2118 mode, when they're used where a 64-bit GPR could also
2119 be used. Checking is needed for Intel Syntax only. */
2120 || (intel_syntax
2121 && flag_code != CODE_64BIT
2122 && (t->operand_types[wanted].bitfield.class == Reg
2123 || t->operand_types[wanted].bitfield.class == Accum
2124 || t->opcode_modifier.isstring))))
2125 || (i.types[given].bitfield.tbyte
2126 && !t->operand_types[wanted].bitfield.tbyte));
2127 }
2128
2129 /* Return 1 if there is no conflict in SIMD register between operand
2130 GIVEN and opeand WANTED for instruction template T. */
2131
2132 static INLINE int
2133 match_simd_size (const insn_template *t, unsigned int wanted,
2134 unsigned int given)
2135 {
2136 return !((i.types[given].bitfield.xmmword
2137 && !t->operand_types[wanted].bitfield.xmmword)
2138 || (i.types[given].bitfield.ymmword
2139 && !t->operand_types[wanted].bitfield.ymmword)
2140 || (i.types[given].bitfield.zmmword
2141 && !t->operand_types[wanted].bitfield.zmmword)
2142 || (i.types[given].bitfield.tmmword
2143 && !t->operand_types[wanted].bitfield.tmmword));
2144 }
2145
2146 /* Return 1 if there is no conflict in any size between operand GIVEN
2147 and opeand WANTED for instruction template T. */
2148
2149 static INLINE int
2150 match_mem_size (const insn_template *t, unsigned int wanted,
2151 unsigned int given)
2152 {
2153 return (match_operand_size (t, wanted, given)
2154 && !((i.types[given].bitfield.unspecified
2155 && !i.broadcast.type
2156 && !i.broadcast.bytes
2157 && !t->operand_types[wanted].bitfield.unspecified)
2158 || (i.types[given].bitfield.fword
2159 && !t->operand_types[wanted].bitfield.fword)
2160 /* For scalar opcode templates to allow register and memory
2161 operands at the same time, some special casing is needed
2162 here. Also for v{,p}broadcast*, {,v}pmov{s,z}*, and
2163 down-conversion vpmov*. */
2164 || ((t->operand_types[wanted].bitfield.class == RegSIMD
2165 && t->operand_types[wanted].bitfield.byte
2166 + t->operand_types[wanted].bitfield.word
2167 + t->operand_types[wanted].bitfield.dword
2168 + t->operand_types[wanted].bitfield.qword
2169 > !!t->opcode_modifier.broadcast)
2170 ? (i.types[given].bitfield.xmmword
2171 || i.types[given].bitfield.ymmword
2172 || i.types[given].bitfield.zmmword)
2173 : !match_simd_size(t, wanted, given))));
2174 }
2175
2176 /* Return value has MATCH_STRAIGHT set if there is no size conflict on any
2177 operands for instruction template T, and it has MATCH_REVERSE set if there
2178 is no size conflict on any operands for the template with operands reversed
2179 (and the template allows for reversing in the first place). */
2180
2181 #define MATCH_STRAIGHT 1
2182 #define MATCH_REVERSE 2
2183
2184 static INLINE unsigned int
2185 operand_size_match (const insn_template *t)
2186 {
2187 unsigned int j, match = MATCH_STRAIGHT;
2188
2189 /* Don't check non-absolute jump instructions. */
2190 if (t->opcode_modifier.jump
2191 && t->opcode_modifier.jump != JUMP_ABSOLUTE)
2192 return match;
2193
2194 /* Check memory and accumulator operand size. */
2195 for (j = 0; j < i.operands; j++)
2196 {
2197 if (i.types[j].bitfield.class != Reg
2198 && i.types[j].bitfield.class != RegSIMD
2199 && t->opcode_modifier.operandconstraint == ANY_SIZE)
2200 continue;
2201
2202 if (t->operand_types[j].bitfield.class == Reg
2203 && !match_operand_size (t, j, j))
2204 {
2205 match = 0;
2206 break;
2207 }
2208
2209 if (t->operand_types[j].bitfield.class == RegSIMD
2210 && !match_simd_size (t, j, j))
2211 {
2212 match = 0;
2213 break;
2214 }
2215
2216 if (t->operand_types[j].bitfield.instance == Accum
2217 && (!match_operand_size (t, j, j) || !match_simd_size (t, j, j)))
2218 {
2219 match = 0;
2220 break;
2221 }
2222
2223 if ((i.flags[j] & Operand_Mem) && !match_mem_size (t, j, j))
2224 {
2225 match = 0;
2226 break;
2227 }
2228 }
2229
2230 if (!t->opcode_modifier.d)
2231 return match;
2232
2233 /* Check reverse. */
2234 gas_assert (i.operands >= 2);
2235
2236 for (j = 0; j < i.operands; j++)
2237 {
2238 unsigned int given = i.operands - j - 1;
2239
2240 /* For FMA4 and XOP insns VEX.W controls just the first two
2241 register operands. */
2242 if (is_cpu (t, CpuFMA4) || is_cpu (t, CpuXOP))
2243 given = j < 2 ? 1 - j : j;
2244
2245 if (t->operand_types[j].bitfield.class == Reg
2246 && !match_operand_size (t, j, given))
2247 return match;
2248
2249 if (t->operand_types[j].bitfield.class == RegSIMD
2250 && !match_simd_size (t, j, given))
2251 return match;
2252
2253 if (t->operand_types[j].bitfield.instance == Accum
2254 && (!match_operand_size (t, j, given)
2255 || !match_simd_size (t, j, given)))
2256 return match;
2257
2258 if ((i.flags[given] & Operand_Mem) && !match_mem_size (t, j, given))
2259 return match;
2260 }
2261
2262 return match | MATCH_REVERSE;
2263 }
2264
2265 static INLINE int
2266 operand_type_match (i386_operand_type overlap,
2267 i386_operand_type given)
2268 {
2269 i386_operand_type temp = overlap;
2270
2271 temp.bitfield.unspecified = 0;
2272 temp.bitfield.byte = 0;
2273 temp.bitfield.word = 0;
2274 temp.bitfield.dword = 0;
2275 temp.bitfield.fword = 0;
2276 temp.bitfield.qword = 0;
2277 temp.bitfield.tbyte = 0;
2278 temp.bitfield.xmmword = 0;
2279 temp.bitfield.ymmword = 0;
2280 temp.bitfield.zmmword = 0;
2281 temp.bitfield.tmmword = 0;
2282 if (operand_type_all_zero (&temp))
2283 goto mismatch;
2284
2285 if (given.bitfield.baseindex == overlap.bitfield.baseindex)
2286 return 1;
2287
2288 mismatch:
2289 i.error = operand_type_mismatch;
2290 return 0;
2291 }
2292
2293 /* If given types g0 and g1 are registers they must be of the same type
2294 unless the expected operand type register overlap is null.
2295 Intel syntax sized memory operands are also checked here. */
2296
2297 static INLINE int
2298 operand_type_register_match (i386_operand_type g0,
2299 i386_operand_type t0,
2300 i386_operand_type g1,
2301 i386_operand_type t1)
2302 {
2303 if (g0.bitfield.class != Reg
2304 && g0.bitfield.class != RegSIMD
2305 && (g0.bitfield.unspecified
2306 || !operand_type_check (g0, anymem)))
2307 return 1;
2308
2309 if (g1.bitfield.class != Reg
2310 && g1.bitfield.class != RegSIMD
2311 && (g1.bitfield.unspecified
2312 || !operand_type_check (g1, anymem)))
2313 return 1;
2314
2315 if (g0.bitfield.byte == g1.bitfield.byte
2316 && g0.bitfield.word == g1.bitfield.word
2317 && g0.bitfield.dword == g1.bitfield.dword
2318 && g0.bitfield.qword == g1.bitfield.qword
2319 && g0.bitfield.xmmword == g1.bitfield.xmmword
2320 && g0.bitfield.ymmword == g1.bitfield.ymmword
2321 && g0.bitfield.zmmword == g1.bitfield.zmmword)
2322 return 1;
2323
2324 /* If expectations overlap in no more than a single size, all is fine. */
2325 g0 = operand_type_and (t0, t1);
2326 if (g0.bitfield.byte
2327 + g0.bitfield.word
2328 + g0.bitfield.dword
2329 + g0.bitfield.qword
2330 + g0.bitfield.xmmword
2331 + g0.bitfield.ymmword
2332 + g0.bitfield.zmmword <= 1)
2333 return 1;
2334
2335 i.error = register_type_mismatch;
2336
2337 return 0;
2338 }
2339
2340 static INLINE unsigned int
2341 register_number (const reg_entry *r)
2342 {
2343 unsigned int nr = r->reg_num;
2344
2345 if (r->reg_flags & RegRex)
2346 nr += 8;
2347
2348 if (r->reg_flags & RegVRex)
2349 nr += 16;
2350
2351 return nr;
2352 }
2353
2354 static INLINE unsigned int
2355 mode_from_disp_size (i386_operand_type t)
2356 {
2357 if (t.bitfield.disp8)
2358 return 1;
2359 else if (t.bitfield.disp16
2360 || t.bitfield.disp32)
2361 return 2;
2362 else
2363 return 0;
2364 }
2365
2366 static INLINE int
2367 fits_in_signed_byte (addressT num)
2368 {
2369 return num + 0x80 <= 0xff;
2370 }
2371
2372 static INLINE int
2373 fits_in_unsigned_byte (addressT num)
2374 {
2375 return num <= 0xff;
2376 }
2377
2378 static INLINE int
2379 fits_in_unsigned_word (addressT num)
2380 {
2381 return num <= 0xffff;
2382 }
2383
2384 static INLINE int
2385 fits_in_signed_word (addressT num)
2386 {
2387 return num + 0x8000 <= 0xffff;
2388 }
2389
2390 static INLINE int
2391 fits_in_signed_long (addressT num ATTRIBUTE_UNUSED)
2392 {
2393 #ifndef BFD64
2394 return 1;
2395 #else
2396 return num + 0x80000000 <= 0xffffffff;
2397 #endif
2398 } /* fits_in_signed_long() */
2399
2400 static INLINE int
2401 fits_in_unsigned_long (addressT num ATTRIBUTE_UNUSED)
2402 {
2403 #ifndef BFD64
2404 return 1;
2405 #else
2406 return num <= 0xffffffff;
2407 #endif
2408 } /* fits_in_unsigned_long() */
2409
2410 static INLINE valueT extend_to_32bit_address (addressT num)
2411 {
2412 #ifdef BFD64
2413 if (fits_in_unsigned_long(num))
2414 return (num ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
2415
2416 if (!fits_in_signed_long (num))
2417 return num & 0xffffffff;
2418 #endif
2419
2420 return num;
2421 }
2422
2423 static INLINE int
2424 fits_in_disp8 (offsetT num)
2425 {
2426 int shift = i.memshift;
2427 unsigned int mask;
2428
2429 if (shift == -1)
2430 abort ();
2431
2432 mask = (1 << shift) - 1;
2433
2434 /* Return 0 if NUM isn't properly aligned. */
2435 if ((num & mask))
2436 return 0;
2437
2438 /* Check if NUM will fit in 8bit after shift. */
2439 return fits_in_signed_byte (num >> shift);
2440 }
2441
2442 static INLINE int
2443 fits_in_imm4 (offsetT num)
2444 {
2445 /* Despite the name, check for imm3 if we're dealing with EVEX. */
2446 return (num & (i.vec_encoding != vex_encoding_evex ? 0xf : 7)) == num;
2447 }
2448
2449 static i386_operand_type
2450 smallest_imm_type (offsetT num)
2451 {
2452 i386_operand_type t;
2453
2454 operand_type_set (&t, 0);
2455 t.bitfield.imm64 = 1;
2456
2457 if (cpu_arch_tune != PROCESSOR_I486 && num == 1)
2458 {
2459 /* This code is disabled on the 486 because all the Imm1 forms
2460 in the opcode table are slower on the i486. They're the
2461 versions with the implicitly specified single-position
2462 displacement, which has another syntax if you really want to
2463 use that form. */
2464 t.bitfield.imm1 = 1;
2465 t.bitfield.imm8 = 1;
2466 t.bitfield.imm8s = 1;
2467 t.bitfield.imm16 = 1;
2468 t.bitfield.imm32 = 1;
2469 t.bitfield.imm32s = 1;
2470 }
2471 else if (fits_in_signed_byte (num))
2472 {
2473 if (fits_in_unsigned_byte (num))
2474 t.bitfield.imm8 = 1;
2475 t.bitfield.imm8s = 1;
2476 t.bitfield.imm16 = 1;
2477 t.bitfield.imm32 = 1;
2478 t.bitfield.imm32s = 1;
2479 }
2480 else if (fits_in_unsigned_byte (num))
2481 {
2482 t.bitfield.imm8 = 1;
2483 t.bitfield.imm16 = 1;
2484 t.bitfield.imm32 = 1;
2485 t.bitfield.imm32s = 1;
2486 }
2487 else if (fits_in_signed_word (num) || fits_in_unsigned_word (num))
2488 {
2489 t.bitfield.imm16 = 1;
2490 t.bitfield.imm32 = 1;
2491 t.bitfield.imm32s = 1;
2492 }
2493 else if (fits_in_signed_long (num))
2494 {
2495 t.bitfield.imm32 = 1;
2496 t.bitfield.imm32s = 1;
2497 }
2498 else if (fits_in_unsigned_long (num))
2499 t.bitfield.imm32 = 1;
2500
2501 return t;
2502 }
2503
2504 static offsetT
2505 offset_in_range (offsetT val, int size)
2506 {
2507 addressT mask;
2508
2509 switch (size)
2510 {
2511 case 1: mask = ((addressT) 1 << 8) - 1; break;
2512 case 2: mask = ((addressT) 1 << 16) - 1; break;
2513 #ifdef BFD64
2514 case 4: mask = ((addressT) 1 << 32) - 1; break;
2515 #endif
2516 case sizeof (val): return val;
2517 default: abort ();
2518 }
2519
2520 if ((val & ~mask) != 0 && (-val & ~mask) != 0)
2521 as_warn (_("0x%" PRIx64 " shortened to 0x%" PRIx64),
2522 (uint64_t) val, (uint64_t) (val & mask));
2523
2524 return val & mask;
2525 }
2526
2527 static INLINE const char *insn_name (const insn_template *t)
2528 {
2529 return &i386_mnemonics[t->mnem_off];
2530 }
2531
2532 enum PREFIX_GROUP
2533 {
2534 PREFIX_EXIST = 0,
2535 PREFIX_LOCK,
2536 PREFIX_REP,
2537 PREFIX_DS,
2538 PREFIX_OTHER
2539 };
2540
2541 /* Returns
2542 a. PREFIX_EXIST if attempting to add a prefix where one from the
2543 same class already exists.
2544 b. PREFIX_LOCK if lock prefix is added.
2545 c. PREFIX_REP if rep/repne prefix is added.
2546 d. PREFIX_DS if ds prefix is added.
2547 e. PREFIX_OTHER if other prefix is added.
2548 */
2549
2550 static enum PREFIX_GROUP
2551 add_prefix (unsigned int prefix)
2552 {
2553 enum PREFIX_GROUP ret = PREFIX_OTHER;
2554 unsigned int q;
2555
2556 if (prefix >= REX_OPCODE && prefix < REX_OPCODE + 16
2557 && flag_code == CODE_64BIT)
2558 {
2559 if ((i.prefix[REX_PREFIX] & prefix & REX_W)
2560 || (i.prefix[REX_PREFIX] & prefix & REX_R)
2561 || (i.prefix[REX_PREFIX] & prefix & REX_X)
2562 || (i.prefix[REX_PREFIX] & prefix & REX_B))
2563 ret = PREFIX_EXIST;
2564 q = REX_PREFIX;
2565 }
2566 else
2567 {
2568 switch (prefix)
2569 {
2570 default:
2571 abort ();
2572
2573 case DS_PREFIX_OPCODE:
2574 ret = PREFIX_DS;
2575 /* Fall through. */
2576 case CS_PREFIX_OPCODE:
2577 case ES_PREFIX_OPCODE:
2578 case FS_PREFIX_OPCODE:
2579 case GS_PREFIX_OPCODE:
2580 case SS_PREFIX_OPCODE:
2581 q = SEG_PREFIX;
2582 break;
2583
2584 case REPNE_PREFIX_OPCODE:
2585 case REPE_PREFIX_OPCODE:
2586 q = REP_PREFIX;
2587 ret = PREFIX_REP;
2588 break;
2589
2590 case LOCK_PREFIX_OPCODE:
2591 q = LOCK_PREFIX;
2592 ret = PREFIX_LOCK;
2593 break;
2594
2595 case FWAIT_OPCODE:
2596 q = WAIT_PREFIX;
2597 break;
2598
2599 case ADDR_PREFIX_OPCODE:
2600 q = ADDR_PREFIX;
2601 break;
2602
2603 case DATA_PREFIX_OPCODE:
2604 q = DATA_PREFIX;
2605 break;
2606 }
2607 if (i.prefix[q] != 0)
2608 ret = PREFIX_EXIST;
2609 }
2610
2611 if (ret)
2612 {
2613 if (!i.prefix[q])
2614 ++i.prefixes;
2615 i.prefix[q] |= prefix;
2616 }
2617 else
2618 as_bad (_("same type of prefix used twice"));
2619
2620 return ret;
2621 }
2622
2623 static void
2624 update_code_flag (int value, int check)
2625 {
2626 PRINTF_LIKE ((*as_error)) = check ? as_fatal : as_bad;
2627
2628 if (value == CODE_64BIT && !cpu_arch_flags.bitfield.cpu64 )
2629 {
2630 as_error (_("64bit mode not supported on `%s'."),
2631 cpu_arch_name ? cpu_arch_name : default_arch);
2632 return;
2633 }
2634
2635 if (value == CODE_32BIT && !cpu_arch_flags.bitfield.cpui386)
2636 {
2637 as_error (_("32bit mode not supported on `%s'."),
2638 cpu_arch_name ? cpu_arch_name : default_arch);
2639 return;
2640 }
2641
2642 flag_code = (enum flag_code) value;
2643
2644 stackop_size = '\0';
2645 }
2646
2647 static void
2648 set_code_flag (int value)
2649 {
2650 update_code_flag (value, 0);
2651 }
2652
2653 static void
2654 set_16bit_gcc_code_flag (int new_code_flag)
2655 {
2656 flag_code = (enum flag_code) new_code_flag;
2657 if (flag_code != CODE_16BIT)
2658 abort ();
2659 stackop_size = LONG_MNEM_SUFFIX;
2660 }
2661
2662 static void
2663 set_intel_syntax (int syntax_flag)
2664 {
2665 /* Find out if register prefixing is specified. */
2666 int ask_naked_reg = 0;
2667
2668 SKIP_WHITESPACE ();
2669 if (!is_end_of_line[(unsigned char) *input_line_pointer])
2670 {
2671 char *string;
2672 int e = get_symbol_name (&string);
2673
2674 if (strcmp (string, "prefix") == 0)
2675 ask_naked_reg = 1;
2676 else if (strcmp (string, "noprefix") == 0)
2677 ask_naked_reg = -1;
2678 else
2679 as_bad (_("bad argument to syntax directive."));
2680 (void) restore_line_pointer (e);
2681 }
2682 demand_empty_rest_of_line ();
2683
2684 intel_syntax = syntax_flag;
2685
2686 if (ask_naked_reg == 0)
2687 allow_naked_reg = (intel_syntax
2688 && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
2689 else
2690 allow_naked_reg = (ask_naked_reg < 0);
2691
2692 expr_set_rank (O_full_ptr, syntax_flag ? 10 : 0);
2693
2694 register_prefix = allow_naked_reg ? "" : "%";
2695 }
2696
2697 static void
2698 set_intel_mnemonic (int mnemonic_flag)
2699 {
2700 intel_mnemonic = mnemonic_flag;
2701 }
2702
2703 static void
2704 set_allow_index_reg (int flag)
2705 {
2706 allow_index_reg = flag;
2707 }
2708
2709 static void
2710 set_check (int what)
2711 {
2712 enum check_kind *kind;
2713 const char *str;
2714
2715 if (what)
2716 {
2717 kind = &operand_check;
2718 str = "operand";
2719 }
2720 else
2721 {
2722 kind = &sse_check;
2723 str = "sse";
2724 }
2725
2726 SKIP_WHITESPACE ();
2727
2728 if (!is_end_of_line[(unsigned char) *input_line_pointer])
2729 {
2730 char *string;
2731 int e = get_symbol_name (&string);
2732
2733 if (strcmp (string, "none") == 0)
2734 *kind = check_none;
2735 else if (strcmp (string, "warning") == 0)
2736 *kind = check_warning;
2737 else if (strcmp (string, "error") == 0)
2738 *kind = check_error;
2739 else
2740 as_bad (_("bad argument to %s_check directive."), str);
2741 (void) restore_line_pointer (e);
2742 }
2743 else
2744 as_bad (_("missing argument for %s_check directive"), str);
2745
2746 demand_empty_rest_of_line ();
2747 }
2748
2749 static void
2750 check_cpu_arch_compatible (const char *name ATTRIBUTE_UNUSED,
2751 i386_cpu_flags new_flag ATTRIBUTE_UNUSED)
2752 {
2753 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2754 static const char *arch;
2755
2756 /* Intel MCU is only supported on ELF. */
2757 if (!IS_ELF)
2758 return;
2759
2760 if (!arch)
2761 {
2762 /* Use cpu_arch_name if it is set in md_parse_option. Otherwise
2763 use default_arch. */
2764 arch = cpu_arch_name;
2765 if (!arch)
2766 arch = default_arch;
2767 }
2768
2769 /* If we are targeting Intel MCU, we must enable it. */
2770 if ((get_elf_backend_data (stdoutput)->elf_machine_code == EM_IAMCU)
2771 == new_flag.bitfield.cpuiamcu)
2772 return;
2773
2774 as_bad (_("`%s' is not supported on `%s'"), name, arch);
2775 #endif
2776 }
2777
2778 static void
2779 extend_cpu_sub_arch_name (const char *pfx, const char *name)
2780 {
2781 if (cpu_sub_arch_name)
2782 cpu_sub_arch_name = reconcat (cpu_sub_arch_name, cpu_sub_arch_name,
2783 pfx, name, (const char *) NULL);
2784 else
2785 cpu_sub_arch_name = concat (pfx, name, (const char *) NULL);
2786 }
2787
2788 static void isa_enable (unsigned int idx)
2789 {
2790 i386_cpu_flags flags = cpu_flags_or (cpu_arch_flags, cpu_arch[idx].enable);
2791
2792 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
2793 {
2794 extend_cpu_sub_arch_name (".", cpu_arch[idx].name);
2795 cpu_arch_flags = flags;
2796 }
2797
2798 cpu_arch_isa_flags = cpu_flags_or (cpu_arch_isa_flags, cpu_arch[idx].enable);
2799 }
2800
2801 static void isa_disable (unsigned int idx)
2802 {
2803 i386_cpu_flags flags
2804 = cpu_flags_and_not (cpu_arch_flags, cpu_arch[idx].disable);
2805
2806 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
2807 {
2808 extend_cpu_sub_arch_name (".no", cpu_arch[idx].name);
2809 cpu_arch_flags = flags;
2810 }
2811
2812 cpu_arch_isa_flags
2813 = cpu_flags_and_not (cpu_arch_isa_flags, cpu_arch[idx].disable);
2814 }
2815
2816 static void
2817 set_cpu_arch (int dummy ATTRIBUTE_UNUSED)
2818 {
2819 typedef struct arch_stack_entry
2820 {
2821 const struct arch_stack_entry *prev;
2822 const char *name;
2823 char *sub_name;
2824 i386_cpu_flags flags;
2825 i386_cpu_flags isa_flags;
2826 enum processor_type isa;
2827 enum flag_code flag_code;
2828 unsigned int vector_size;
2829 char stackop_size;
2830 bool no_cond_jump_promotion;
2831 } arch_stack_entry;
2832 static const arch_stack_entry *arch_stack_top;
2833 char *s;
2834 int e;
2835 const char *string;
2836 unsigned int j = 0;
2837
2838 SKIP_WHITESPACE ();
2839
2840 if (is_end_of_line[(unsigned char) *input_line_pointer])
2841 {
2842 as_bad (_("missing cpu architecture"));
2843 input_line_pointer++;
2844 return;
2845 }
2846
2847 e = get_symbol_name (&s);
2848 string = s;
2849
2850 if (strcmp (string, "push") == 0)
2851 {
2852 arch_stack_entry *top = XNEW (arch_stack_entry);
2853
2854 top->name = cpu_arch_name;
2855 if (cpu_sub_arch_name)
2856 top->sub_name = xstrdup (cpu_sub_arch_name);
2857 else
2858 top->sub_name = NULL;
2859 top->flags = cpu_arch_flags;
2860 top->isa = cpu_arch_isa;
2861 top->isa_flags = cpu_arch_isa_flags;
2862 top->flag_code = flag_code;
2863 top->vector_size = vector_size;
2864 top->stackop_size = stackop_size;
2865 top->no_cond_jump_promotion = no_cond_jump_promotion;
2866
2867 top->prev = arch_stack_top;
2868 arch_stack_top = top;
2869
2870 (void) restore_line_pointer (e);
2871 demand_empty_rest_of_line ();
2872 return;
2873 }
2874
2875 if (strcmp (string, "pop") == 0)
2876 {
2877 const arch_stack_entry *top = arch_stack_top;
2878
2879 if (!top)
2880 as_bad (_(".arch stack is empty"));
2881 else if (top->flag_code != flag_code
2882 || top->stackop_size != stackop_size)
2883 {
2884 static const unsigned int bits[] = {
2885 [CODE_16BIT] = 16,
2886 [CODE_32BIT] = 32,
2887 [CODE_64BIT] = 64,
2888 };
2889
2890 as_bad (_("this `.arch pop' requires `.code%u%s' to be in effect"),
2891 bits[top->flag_code],
2892 top->stackop_size == LONG_MNEM_SUFFIX ? "gcc" : "");
2893 }
2894 else
2895 {
2896 arch_stack_top = top->prev;
2897
2898 cpu_arch_name = top->name;
2899 free (cpu_sub_arch_name);
2900 cpu_sub_arch_name = top->sub_name;
2901 cpu_arch_flags = top->flags;
2902 cpu_arch_isa = top->isa;
2903 cpu_arch_isa_flags = top->isa_flags;
2904 vector_size = top->vector_size;
2905 no_cond_jump_promotion = top->no_cond_jump_promotion;
2906
2907 XDELETE (top);
2908 }
2909
2910 (void) restore_line_pointer (e);
2911 demand_empty_rest_of_line ();
2912 return;
2913 }
2914
2915 if (strcmp (string, "default") == 0)
2916 {
2917 if (strcmp (default_arch, "iamcu") == 0)
2918 string = default_arch;
2919 else
2920 {
2921 static const i386_cpu_flags cpu_unknown_flags = CPU_UNKNOWN_FLAGS;
2922
2923 cpu_arch_name = NULL;
2924 free (cpu_sub_arch_name);
2925 cpu_sub_arch_name = NULL;
2926 cpu_arch_flags = cpu_unknown_flags;
2927 cpu_arch_isa = PROCESSOR_UNKNOWN;
2928 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
2929 if (!cpu_arch_tune_set)
2930 cpu_arch_tune = PROCESSOR_UNKNOWN;
2931
2932 vector_size = VSZ_DEFAULT;
2933
2934 j = ARRAY_SIZE (cpu_arch) + 1;
2935 }
2936 }
2937
2938 for (; j < ARRAY_SIZE (cpu_arch); j++)
2939 {
2940 if (strcmp (string + (*string == '.'), cpu_arch[j].name) == 0
2941 && (*string == '.') == (cpu_arch[j].type == PROCESSOR_NONE))
2942 {
2943 if (*string != '.')
2944 {
2945 check_cpu_arch_compatible (string, cpu_arch[j].enable);
2946
2947 if (flag_code == CODE_64BIT && !cpu_arch[j].enable.bitfield.cpu64 )
2948 {
2949 as_bad (_("64bit mode not supported on `%s'."),
2950 cpu_arch[j].name);
2951 (void) restore_line_pointer (e);
2952 ignore_rest_of_line ();
2953 return;
2954 }
2955
2956 if (flag_code == CODE_32BIT && !cpu_arch[j].enable.bitfield.cpui386)
2957 {
2958 as_bad (_("32bit mode not supported on `%s'."),
2959 cpu_arch[j].name);
2960 (void) restore_line_pointer (e);
2961 ignore_rest_of_line ();
2962 return;
2963 }
2964
2965 cpu_arch_name = cpu_arch[j].name;
2966 free (cpu_sub_arch_name);
2967 cpu_sub_arch_name = NULL;
2968 cpu_arch_flags = cpu_arch[j].enable;
2969 cpu_arch_isa = cpu_arch[j].type;
2970 cpu_arch_isa_flags = cpu_arch[j].enable;
2971 if (!cpu_arch_tune_set)
2972 cpu_arch_tune = cpu_arch_isa;
2973
2974 vector_size = VSZ_DEFAULT;
2975
2976 pre_386_16bit_warned = false;
2977 break;
2978 }
2979
2980 if (cpu_flags_all_zero (&cpu_arch[j].enable))
2981 continue;
2982
2983 isa_enable (j);
2984
2985 (void) restore_line_pointer (e);
2986
2987 switch (cpu_arch[j].vsz)
2988 {
2989 default:
2990 break;
2991
2992 case vsz_set:
2993 #ifdef SVR4_COMMENT_CHARS
2994 if (*input_line_pointer == ':' || *input_line_pointer == '/')
2995 #else
2996 if (*input_line_pointer == '/')
2997 #endif
2998 {
2999 ++input_line_pointer;
3000 switch (get_absolute_expression ())
3001 {
3002 case 512: vector_size = VSZ512; break;
3003 case 256: vector_size = VSZ256; break;
3004 case 128: vector_size = VSZ128; break;
3005 default:
3006 as_bad (_("Unrecognized vector size specifier"));
3007 ignore_rest_of_line ();
3008 return;
3009 }
3010 break;
3011 }
3012 /* Fall through. */
3013 case vsz_reset:
3014 vector_size = VSZ_DEFAULT;
3015 break;
3016 }
3017
3018 demand_empty_rest_of_line ();
3019 return;
3020 }
3021 }
3022
3023 if (startswith (string, ".no") && j >= ARRAY_SIZE (cpu_arch))
3024 {
3025 /* Disable an ISA extension. */
3026 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
3027 if (cpu_arch[j].type == PROCESSOR_NONE
3028 && strcmp (string + 3, cpu_arch[j].name) == 0)
3029 {
3030 isa_disable (j);
3031
3032 if (cpu_arch[j].vsz == vsz_set)
3033 vector_size = VSZ_DEFAULT;
3034
3035 (void) restore_line_pointer (e);
3036 demand_empty_rest_of_line ();
3037 return;
3038 }
3039 }
3040
3041 if (j == ARRAY_SIZE (cpu_arch))
3042 as_bad (_("no such architecture: `%s'"), string);
3043
3044 *input_line_pointer = e;
3045
3046 no_cond_jump_promotion = 0;
3047 if (*input_line_pointer == ','
3048 && !is_end_of_line[(unsigned char) input_line_pointer[1]])
3049 {
3050 ++input_line_pointer;
3051 e = get_symbol_name (&s);
3052 string = s;
3053
3054 if (strcmp (string, "nojumps") == 0)
3055 no_cond_jump_promotion = 1;
3056 else if (strcmp (string, "jumps") == 0)
3057 ;
3058 else
3059 as_bad (_("no such architecture modifier: `%s'"), string);
3060
3061 (void) restore_line_pointer (e);
3062 }
3063
3064 demand_empty_rest_of_line ();
3065 }
3066
3067 enum bfd_architecture
3068 i386_arch (void)
3069 {
3070 if (cpu_arch_isa == PROCESSOR_IAMCU)
3071 {
3072 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
3073 || flag_code == CODE_64BIT)
3074 as_fatal (_("Intel MCU is 32bit ELF only"));
3075 return bfd_arch_iamcu;
3076 }
3077 else
3078 return bfd_arch_i386;
3079 }
3080
3081 unsigned long
3082 i386_mach (void)
3083 {
3084 if (startswith (default_arch, "x86_64"))
3085 {
3086 if (default_arch[6] == '\0')
3087 return bfd_mach_x86_64;
3088 else
3089 return bfd_mach_x64_32;
3090 }
3091 else if (!strcmp (default_arch, "i386")
3092 || !strcmp (default_arch, "iamcu"))
3093 {
3094 if (cpu_arch_isa == PROCESSOR_IAMCU)
3095 {
3096 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
3097 as_fatal (_("Intel MCU is 32bit ELF only"));
3098 return bfd_mach_i386_iamcu;
3099 }
3100 else
3101 return bfd_mach_i386_i386;
3102 }
3103 else
3104 as_fatal (_("unknown architecture"));
3105 }
3106 \f
3107 #include "opcodes/i386-tbl.h"
3108
3109 void
3110 md_begin (void)
3111 {
3112 /* Support pseudo prefixes like {disp32}. */
3113 lex_type ['{'] = LEX_BEGIN_NAME;
3114
3115 /* Initialize op_hash hash table. */
3116 op_hash = str_htab_create ();
3117
3118 {
3119 const insn_template *const *sets = i386_op_sets;
3120 const insn_template *const *end = sets + ARRAY_SIZE (i386_op_sets) - 1;
3121
3122 /* Type checks to compensate for the conversion through void * which
3123 occurs during hash table insertion / lookup. */
3124 (void) sizeof (sets == &current_templates->start);
3125 (void) sizeof (end == &current_templates->end);
3126 for (; sets < end; ++sets)
3127 if (str_hash_insert (op_hash, insn_name (*sets), sets, 0))
3128 as_fatal (_("duplicate %s"), insn_name (*sets));
3129 }
3130
3131 /* Initialize reg_hash hash table. */
3132 reg_hash = str_htab_create ();
3133 {
3134 const reg_entry *regtab;
3135 unsigned int regtab_size = i386_regtab_size;
3136
3137 for (regtab = i386_regtab; regtab_size--; regtab++)
3138 {
3139 switch (regtab->reg_type.bitfield.class)
3140 {
3141 case Reg:
3142 if (regtab->reg_type.bitfield.dword)
3143 {
3144 if (regtab->reg_type.bitfield.instance == Accum)
3145 reg_eax = regtab;
3146 }
3147 else if (regtab->reg_type.bitfield.tbyte)
3148 {
3149 /* There's no point inserting st(<N>) in the hash table, as
3150 parentheses aren't included in register_chars[] anyway. */
3151 if (regtab->reg_type.bitfield.instance != Accum)
3152 continue;
3153 reg_st0 = regtab;
3154 }
3155 break;
3156
3157 case SReg:
3158 switch (regtab->reg_num)
3159 {
3160 case 0: reg_es = regtab; break;
3161 case 2: reg_ss = regtab; break;
3162 case 3: reg_ds = regtab; break;
3163 }
3164 break;
3165
3166 case RegMask:
3167 if (!regtab->reg_num)
3168 reg_k0 = regtab;
3169 break;
3170 }
3171
3172 if (str_hash_insert (reg_hash, regtab->reg_name, regtab, 0) != NULL)
3173 as_fatal (_("duplicate %s"), regtab->reg_name);
3174 }
3175 }
3176
3177 /* Fill in lexical tables: mnemonic_chars, operand_chars. */
3178 {
3179 int c;
3180 const char *p;
3181
3182 for (c = 0; c < 256; c++)
3183 {
3184 if (ISDIGIT (c) || ISLOWER (c))
3185 {
3186 mnemonic_chars[c] = c;
3187 register_chars[c] = c;
3188 operand_chars[c] = c;
3189 }
3190 else if (ISUPPER (c))
3191 {
3192 mnemonic_chars[c] = TOLOWER (c);
3193 register_chars[c] = mnemonic_chars[c];
3194 operand_chars[c] = c;
3195 }
3196 #ifdef SVR4_COMMENT_CHARS
3197 else if (c == '\\' && strchr (i386_comment_chars, '/'))
3198 operand_chars[c] = c;
3199 #endif
3200
3201 if (c >= 128)
3202 operand_chars[c] = c;
3203 }
3204
3205 mnemonic_chars['_'] = '_';
3206 mnemonic_chars['-'] = '-';
3207 mnemonic_chars['.'] = '.';
3208
3209 for (p = extra_symbol_chars; *p != '\0'; p++)
3210 operand_chars[(unsigned char) *p] = *p;
3211 for (p = operand_special_chars; *p != '\0'; p++)
3212 operand_chars[(unsigned char) *p] = *p;
3213 }
3214
3215 if (flag_code == CODE_64BIT)
3216 {
3217 #if defined (OBJ_COFF) && defined (TE_PE)
3218 x86_dwarf2_return_column = (OUTPUT_FLAVOR == bfd_target_coff_flavour
3219 ? 32 : 16);
3220 #else
3221 x86_dwarf2_return_column = 16;
3222 #endif
3223 x86_cie_data_alignment = -8;
3224 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3225 x86_sframe_cfa_sp_reg = 7;
3226 x86_sframe_cfa_fp_reg = 6;
3227 #endif
3228 }
3229 else
3230 {
3231 x86_dwarf2_return_column = 8;
3232 x86_cie_data_alignment = -4;
3233 }
3234
3235 /* NB: FUSED_JCC_PADDING frag must have sufficient room so that it
3236 can be turned into BRANCH_PREFIX frag. */
3237 if (align_branch_prefix_size > MAX_FUSED_JCC_PADDING_SIZE)
3238 abort ();
3239 }
3240
3241 void
3242 i386_print_statistics (FILE *file)
3243 {
3244 htab_print_statistics (file, "i386 opcode", op_hash);
3245 htab_print_statistics (file, "i386 register", reg_hash);
3246 }
3247
3248 void
3249 i386_md_end (void)
3250 {
3251 htab_delete (op_hash);
3252 htab_delete (reg_hash);
3253 }
3254 \f
3255 #ifdef DEBUG386
3256
3257 /* Debugging routines for md_assemble. */
3258 static void pte (insn_template *);
3259 static void pt (i386_operand_type);
3260 static void pe (expressionS *);
3261 static void ps (symbolS *);
3262
3263 static void
3264 pi (const char *line, i386_insn *x)
3265 {
3266 unsigned int j;
3267
3268 fprintf (stdout, "%s: template ", line);
3269 pte (&x->tm);
3270 fprintf (stdout, " address: base %s index %s scale %x\n",
3271 x->base_reg ? x->base_reg->reg_name : "none",
3272 x->index_reg ? x->index_reg->reg_name : "none",
3273 x->log2_scale_factor);
3274 fprintf (stdout, " modrm: mode %x reg %x reg/mem %x\n",
3275 x->rm.mode, x->rm.reg, x->rm.regmem);
3276 fprintf (stdout, " sib: base %x index %x scale %x\n",
3277 x->sib.base, x->sib.index, x->sib.scale);
3278 fprintf (stdout, " rex: 64bit %x extX %x extY %x extZ %x\n",
3279 (x->rex & REX_W) != 0,
3280 (x->rex & REX_R) != 0,
3281 (x->rex & REX_X) != 0,
3282 (x->rex & REX_B) != 0);
3283 for (j = 0; j < x->operands; j++)
3284 {
3285 fprintf (stdout, " #%d: ", j + 1);
3286 pt (x->types[j]);
3287 fprintf (stdout, "\n");
3288 if (x->types[j].bitfield.class == Reg
3289 || x->types[j].bitfield.class == RegMMX
3290 || x->types[j].bitfield.class == RegSIMD
3291 || x->types[j].bitfield.class == RegMask
3292 || x->types[j].bitfield.class == SReg
3293 || x->types[j].bitfield.class == RegCR
3294 || x->types[j].bitfield.class == RegDR
3295 || x->types[j].bitfield.class == RegTR
3296 || x->types[j].bitfield.class == RegBND)
3297 fprintf (stdout, "%s\n", x->op[j].regs->reg_name);
3298 if (operand_type_check (x->types[j], imm))
3299 pe (x->op[j].imms);
3300 if (operand_type_check (x->types[j], disp))
3301 pe (x->op[j].disps);
3302 }
3303 }
3304
3305 static void
3306 pte (insn_template *t)
3307 {
3308 static const unsigned char opc_pfx[] = { 0, 0x66, 0xf3, 0xf2 };
3309 static const char *const opc_spc[] = {
3310 NULL, "0f", "0f38", "0f3a", NULL, "evexmap5", "evexmap6", NULL,
3311 "XOP08", "XOP09", "XOP0A",
3312 };
3313 unsigned int j;
3314
3315 fprintf (stdout, " %d operands ", t->operands);
3316 if (opc_pfx[t->opcode_modifier.opcodeprefix])
3317 fprintf (stdout, "pfx %x ", opc_pfx[t->opcode_modifier.opcodeprefix]);
3318 if (opc_spc[t->opcode_space])
3319 fprintf (stdout, "space %s ", opc_spc[t->opcode_space]);
3320 fprintf (stdout, "opcode %x ", t->base_opcode);
3321 if (t->extension_opcode != None)
3322 fprintf (stdout, "ext %x ", t->extension_opcode);
3323 if (t->opcode_modifier.d)
3324 fprintf (stdout, "D");
3325 if (t->opcode_modifier.w)
3326 fprintf (stdout, "W");
3327 fprintf (stdout, "\n");
3328 for (j = 0; j < t->operands; j++)
3329 {
3330 fprintf (stdout, " #%d type ", j + 1);
3331 pt (t->operand_types[j]);
3332 fprintf (stdout, "\n");
3333 }
3334 }
3335
3336 static void
3337 pe (expressionS *e)
3338 {
3339 fprintf (stdout, " operation %d\n", e->X_op);
3340 fprintf (stdout, " add_number %" PRId64 " (%" PRIx64 ")\n",
3341 (int64_t) e->X_add_number, (uint64_t) (valueT) e->X_add_number);
3342 if (e->X_add_symbol)
3343 {
3344 fprintf (stdout, " add_symbol ");
3345 ps (e->X_add_symbol);
3346 fprintf (stdout, "\n");
3347 }
3348 if (e->X_op_symbol)
3349 {
3350 fprintf (stdout, " op_symbol ");
3351 ps (e->X_op_symbol);
3352 fprintf (stdout, "\n");
3353 }
3354 }
3355
3356 static void
3357 ps (symbolS *s)
3358 {
3359 fprintf (stdout, "%s type %s%s",
3360 S_GET_NAME (s),
3361 S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
3362 segment_name (S_GET_SEGMENT (s)));
3363 }
3364
3365 static struct type_name
3366 {
3367 i386_operand_type mask;
3368 const char *name;
3369 }
3370 const type_names[] =
3371 {
3372 { { .bitfield = { .class = Reg, .byte = 1 } }, "r8" },
3373 { { .bitfield = { .class = Reg, .word = 1 } }, "r16" },
3374 { { .bitfield = { .class = Reg, .dword = 1 } }, "r32" },
3375 { { .bitfield = { .class = Reg, .qword = 1 } }, "r64" },
3376 { { .bitfield = { .instance = Accum, .byte = 1 } }, "acc8" },
3377 { { .bitfield = { .instance = Accum, .word = 1 } }, "acc16" },
3378 { { .bitfield = { .instance = Accum, .dword = 1 } }, "acc32" },
3379 { { .bitfield = { .instance = Accum, .qword = 1 } }, "acc64" },
3380 { { .bitfield = { .imm8 = 1 } }, "i8" },
3381 { { .bitfield = { .imm8s = 1 } }, "i8s" },
3382 { { .bitfield = { .imm16 = 1 } }, "i16" },
3383 { { .bitfield = { .imm32 = 1 } }, "i32" },
3384 { { .bitfield = { .imm32s = 1 } }, "i32s" },
3385 { { .bitfield = { .imm64 = 1 } }, "i64" },
3386 { { .bitfield = { .imm1 = 1 } }, "i1" },
3387 { { .bitfield = { .baseindex = 1 } }, "BaseIndex" },
3388 { { .bitfield = { .disp8 = 1 } }, "d8" },
3389 { { .bitfield = { .disp16 = 1 } }, "d16" },
3390 { { .bitfield = { .disp32 = 1 } }, "d32" },
3391 { { .bitfield = { .disp64 = 1 } }, "d64" },
3392 { { .bitfield = { .instance = RegD, .word = 1 } }, "InOutPortReg" },
3393 { { .bitfield = { .instance = RegC, .byte = 1 } }, "ShiftCount" },
3394 { { .bitfield = { .class = RegCR } }, "control reg" },
3395 { { .bitfield = { .class = RegTR } }, "test reg" },
3396 { { .bitfield = { .class = RegDR } }, "debug reg" },
3397 { { .bitfield = { .class = Reg, .tbyte = 1 } }, "FReg" },
3398 { { .bitfield = { .instance = Accum, .tbyte = 1 } }, "FAcc" },
3399 { { .bitfield = { .class = SReg } }, "SReg" },
3400 { { .bitfield = { .class = RegMMX } }, "rMMX" },
3401 { { .bitfield = { .class = RegSIMD, .xmmword = 1 } }, "rXMM" },
3402 { { .bitfield = { .class = RegSIMD, .ymmword = 1 } }, "rYMM" },
3403 { { .bitfield = { .class = RegSIMD, .zmmword = 1 } }, "rZMM" },
3404 { { .bitfield = { .class = RegSIMD, .tmmword = 1 } }, "rTMM" },
3405 { { .bitfield = { .class = RegMask } }, "Mask reg" },
3406 };
3407
3408 static void
3409 pt (i386_operand_type t)
3410 {
3411 unsigned int j;
3412 i386_operand_type a;
3413
3414 for (j = 0; j < ARRAY_SIZE (type_names); j++)
3415 {
3416 a = operand_type_and (t, type_names[j].mask);
3417 if (operand_type_equal (&a, &type_names[j].mask))
3418 fprintf (stdout, "%s, ", type_names[j].name);
3419 }
3420 fflush (stdout);
3421 }
3422
3423 #endif /* DEBUG386 */
3424 \f
3425 static bfd_reloc_code_real_type
3426 reloc (unsigned int size,
3427 int pcrel,
3428 int sign,
3429 bfd_reloc_code_real_type other)
3430 {
3431 if (other != NO_RELOC)
3432 {
3433 reloc_howto_type *rel;
3434
3435 if (size == 8)
3436 switch (other)
3437 {
3438 case BFD_RELOC_X86_64_GOT32:
3439 return BFD_RELOC_X86_64_GOT64;
3440 break;
3441 case BFD_RELOC_X86_64_GOTPLT64:
3442 return BFD_RELOC_X86_64_GOTPLT64;
3443 break;
3444 case BFD_RELOC_X86_64_PLTOFF64:
3445 return BFD_RELOC_X86_64_PLTOFF64;
3446 break;
3447 case BFD_RELOC_X86_64_GOTPC32:
3448 other = BFD_RELOC_X86_64_GOTPC64;
3449 break;
3450 case BFD_RELOC_X86_64_GOTPCREL:
3451 other = BFD_RELOC_X86_64_GOTPCREL64;
3452 break;
3453 case BFD_RELOC_X86_64_TPOFF32:
3454 other = BFD_RELOC_X86_64_TPOFF64;
3455 break;
3456 case BFD_RELOC_X86_64_DTPOFF32:
3457 other = BFD_RELOC_X86_64_DTPOFF64;
3458 break;
3459 default:
3460 break;
3461 }
3462
3463 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3464 if (other == BFD_RELOC_SIZE32)
3465 {
3466 if (size == 8)
3467 other = BFD_RELOC_SIZE64;
3468 if (pcrel)
3469 {
3470 as_bad (_("there are no pc-relative size relocations"));
3471 return NO_RELOC;
3472 }
3473 }
3474 #endif
3475
3476 /* Sign-checking 4-byte relocations in 16-/32-bit code is pointless. */
3477 if (size == 4 && (flag_code != CODE_64BIT || disallow_64bit_reloc))
3478 sign = -1;
3479
3480 rel = bfd_reloc_type_lookup (stdoutput, other);
3481 if (!rel)
3482 as_bad (_("unknown relocation (%u)"), other);
3483 else if (size != bfd_get_reloc_size (rel))
3484 as_bad (_("%u-byte relocation cannot be applied to %u-byte field"),
3485 bfd_get_reloc_size (rel),
3486 size);
3487 else if (pcrel && !rel->pc_relative)
3488 as_bad (_("non-pc-relative relocation for pc-relative field"));
3489 else if ((rel->complain_on_overflow == complain_overflow_signed
3490 && !sign)
3491 || (rel->complain_on_overflow == complain_overflow_unsigned
3492 && sign > 0))
3493 as_bad (_("relocated field and relocation type differ in signedness"));
3494 else
3495 return other;
3496 return NO_RELOC;
3497 }
3498
3499 if (pcrel)
3500 {
3501 if (!sign)
3502 as_bad (_("there are no unsigned pc-relative relocations"));
3503 switch (size)
3504 {
3505 case 1: return BFD_RELOC_8_PCREL;
3506 case 2: return BFD_RELOC_16_PCREL;
3507 case 4: return BFD_RELOC_32_PCREL;
3508 case 8: return BFD_RELOC_64_PCREL;
3509 }
3510 as_bad (_("cannot do %u byte pc-relative relocation"), size);
3511 }
3512 else
3513 {
3514 if (sign > 0)
3515 switch (size)
3516 {
3517 case 4: return BFD_RELOC_X86_64_32S;
3518 }
3519 else
3520 switch (size)
3521 {
3522 case 1: return BFD_RELOC_8;
3523 case 2: return BFD_RELOC_16;
3524 case 4: return BFD_RELOC_32;
3525 case 8: return BFD_RELOC_64;
3526 }
3527 as_bad (_("cannot do %s %u byte relocation"),
3528 sign > 0 ? "signed" : "unsigned", size);
3529 }
3530
3531 return NO_RELOC;
3532 }
3533
3534 /* Here we decide which fixups can be adjusted to make them relative to
3535 the beginning of the section instead of the symbol. Basically we need
3536 to make sure that the dynamic relocations are done correctly, so in
3537 some cases we force the original symbol to be used. */
3538
3539 int
3540 tc_i386_fix_adjustable (fixS *fixP ATTRIBUTE_UNUSED)
3541 {
3542 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3543 if (!IS_ELF)
3544 return 1;
3545
3546 /* Don't adjust pc-relative references to merge sections in 64-bit
3547 mode. */
3548 if (use_rela_relocations
3549 && (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0
3550 && fixP->fx_pcrel)
3551 return 0;
3552
3553 /* The x86_64 GOTPCREL are represented as 32bit PCrel relocations
3554 and changed later by validate_fix. */
3555 if (GOT_symbol && fixP->fx_subsy == GOT_symbol
3556 && fixP->fx_r_type == BFD_RELOC_32_PCREL)
3557 return 0;
3558
3559 /* Adjust_reloc_syms doesn't know about the GOT. Need to keep symbol
3560 for size relocations. */
3561 if (fixP->fx_r_type == BFD_RELOC_SIZE32
3562 || fixP->fx_r_type == BFD_RELOC_SIZE64
3563 || fixP->fx_r_type == BFD_RELOC_386_GOTOFF
3564 || fixP->fx_r_type == BFD_RELOC_386_GOT32
3565 || fixP->fx_r_type == BFD_RELOC_386_GOT32X
3566 || fixP->fx_r_type == BFD_RELOC_386_TLS_GD
3567 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDM
3568 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDO_32
3569 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE_32
3570 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE
3571 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTIE
3572 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE_32
3573 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE
3574 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTDESC
3575 || fixP->fx_r_type == BFD_RELOC_386_TLS_DESC_CALL
3576 || fixP->fx_r_type == BFD_RELOC_X86_64_GOT32
3577 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCREL
3578 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCRELX
3579 || fixP->fx_r_type == BFD_RELOC_X86_64_REX_GOTPCRELX
3580 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSGD
3581 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSLD
3582 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF32
3583 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF64
3584 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF
3585 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF32
3586 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF64
3587 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTOFF64
3588 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
3589 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSDESC_CALL
3590 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3591 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3592 return 0;
3593 #endif
3594 return 1;
3595 }
3596
3597 static INLINE bool
3598 want_disp32 (const insn_template *t)
3599 {
3600 return flag_code != CODE_64BIT
3601 || i.prefix[ADDR_PREFIX]
3602 || (t->mnem_off == MN_lea
3603 && (!i.types[1].bitfield.qword
3604 || t->opcode_modifier.size == SIZE32));
3605 }
3606
3607 static int
3608 intel_float_operand (const char *mnemonic)
3609 {
3610 /* Note that the value returned is meaningful only for opcodes with (memory)
3611 operands, hence the code here is free to improperly handle opcodes that
3612 have no operands (for better performance and smaller code). */
3613
3614 if (mnemonic[0] != 'f')
3615 return 0; /* non-math */
3616
3617 switch (mnemonic[1])
3618 {
3619 /* fclex, fdecstp, fdisi, femms, feni, fincstp, finit, fsetpm, and
3620 the fs segment override prefix not currently handled because no
3621 call path can make opcodes without operands get here */
3622 case 'i':
3623 return 2 /* integer op */;
3624 case 'l':
3625 if (mnemonic[2] == 'd' && (mnemonic[3] == 'c' || mnemonic[3] == 'e'))
3626 return 3; /* fldcw/fldenv */
3627 break;
3628 case 'n':
3629 if (mnemonic[2] != 'o' /* fnop */)
3630 return 3; /* non-waiting control op */
3631 break;
3632 case 'r':
3633 if (mnemonic[2] == 's')
3634 return 3; /* frstor/frstpm */
3635 break;
3636 case 's':
3637 if (mnemonic[2] == 'a')
3638 return 3; /* fsave */
3639 if (mnemonic[2] == 't')
3640 {
3641 switch (mnemonic[3])
3642 {
3643 case 'c': /* fstcw */
3644 case 'd': /* fstdw */
3645 case 'e': /* fstenv */
3646 case 's': /* fsts[gw] */
3647 return 3;
3648 }
3649 }
3650 break;
3651 case 'x':
3652 if (mnemonic[2] == 'r' || mnemonic[2] == 's')
3653 return 0; /* fxsave/fxrstor are not really math ops */
3654 break;
3655 }
3656
3657 return 1;
3658 }
3659
3660 static INLINE void
3661 install_template (const insn_template *t)
3662 {
3663 unsigned int l;
3664
3665 i.tm = *t;
3666
3667 /* Dual VEX/EVEX templates need stripping one of the possible variants. */
3668 if (t->opcode_modifier.vex && t->opcode_modifier.evex)
3669 {
3670 if ((is_cpu (t, CpuAVX) || is_cpu (t, CpuAVX2))
3671 && is_cpu (t, CpuAVX512F))
3672 {
3673 if (need_evex_encoding ())
3674 {
3675 i.tm.opcode_modifier.vex = 0;
3676 i.tm.cpu.bitfield.cpuavx = 0;
3677 if (is_cpu (&i.tm, CpuAVX2))
3678 i.tm.cpu.bitfield.isa = 0;
3679 }
3680 else
3681 {
3682 i.tm.opcode_modifier.evex = 0;
3683 i.tm.cpu.bitfield.cpuavx512f = 0;
3684 }
3685 }
3686 }
3687
3688 /* Note that for pseudo prefixes this produces a length of 1. But for them
3689 the length isn't interesting at all. */
3690 for (l = 1; l < 4; ++l)
3691 if (!(t->base_opcode >> (8 * l)))
3692 break;
3693
3694 i.opcode_length = l;
3695 }
3696
3697 /* Build the VEX prefix. */
3698
3699 static void
3700 build_vex_prefix (const insn_template *t)
3701 {
3702 unsigned int register_specifier;
3703 unsigned int vector_length;
3704 unsigned int w;
3705
3706 /* Check register specifier. */
3707 if (i.vex.register_specifier)
3708 {
3709 register_specifier =
3710 ~register_number (i.vex.register_specifier) & 0xf;
3711 gas_assert ((i.vex.register_specifier->reg_flags & RegVRex) == 0);
3712 }
3713 else
3714 register_specifier = 0xf;
3715
3716 /* Use 2-byte VEX prefix by swapping destination and source operand
3717 if there are more than 1 register operand. */
3718 if (i.reg_operands > 1
3719 && i.vec_encoding != vex_encoding_vex3
3720 && i.dir_encoding == dir_encoding_default
3721 && i.operands == i.reg_operands
3722 && operand_type_equal (&i.types[0], &i.types[i.operands - 1])
3723 && i.tm.opcode_space == SPACE_0F
3724 && (i.tm.opcode_modifier.load || i.tm.opcode_modifier.d)
3725 && i.rex == REX_B)
3726 {
3727 unsigned int xchg;
3728
3729 swap_2_operands (0, i.operands - 1);
3730
3731 gas_assert (i.rm.mode == 3);
3732
3733 i.rex = REX_R;
3734 xchg = i.rm.regmem;
3735 i.rm.regmem = i.rm.reg;
3736 i.rm.reg = xchg;
3737
3738 if (i.tm.opcode_modifier.d)
3739 i.tm.base_opcode ^= (i.tm.base_opcode & 0xee) != 0x6e
3740 ? Opcode_ExtD : Opcode_SIMD_IntD;
3741 else /* Use the next insn. */
3742 install_template (&t[1]);
3743 }
3744
3745 /* Use 2-byte VEX prefix by swapping commutative source operands if there
3746 are no memory operands and at least 3 register ones. */
3747 if (i.reg_operands >= 3
3748 && i.vec_encoding != vex_encoding_vex3
3749 && i.reg_operands == i.operands - i.imm_operands
3750 && i.tm.opcode_modifier.vex
3751 && i.tm.opcode_modifier.commutative
3752 && (i.tm.opcode_modifier.sse2avx
3753 || (optimize > 1 && !i.no_optimize))
3754 && i.rex == REX_B
3755 && i.vex.register_specifier
3756 && !(i.vex.register_specifier->reg_flags & RegRex))
3757 {
3758 unsigned int xchg = i.operands - i.reg_operands;
3759
3760 gas_assert (i.tm.opcode_space == SPACE_0F);
3761 gas_assert (!i.tm.opcode_modifier.sae);
3762 gas_assert (operand_type_equal (&i.types[i.operands - 2],
3763 &i.types[i.operands - 3]));
3764 gas_assert (i.rm.mode == 3);
3765
3766 swap_2_operands (xchg, xchg + 1);
3767
3768 i.rex = 0;
3769 xchg = i.rm.regmem | 8;
3770 i.rm.regmem = ~register_specifier & 0xf;
3771 gas_assert (!(i.rm.regmem & 8));
3772 i.vex.register_specifier += xchg - i.rm.regmem;
3773 register_specifier = ~xchg & 0xf;
3774 }
3775
3776 if (i.tm.opcode_modifier.vex == VEXScalar)
3777 vector_length = avxscalar;
3778 else if (i.tm.opcode_modifier.vex == VEX256)
3779 vector_length = 1;
3780 else if (dot_insn () && i.tm.opcode_modifier.vex == VEX128)
3781 vector_length = 0;
3782 else
3783 {
3784 unsigned int op;
3785
3786 /* Determine vector length from the last multi-length vector
3787 operand. */
3788 vector_length = 0;
3789 for (op = t->operands; op--;)
3790 if (t->operand_types[op].bitfield.xmmword
3791 && t->operand_types[op].bitfield.ymmword
3792 && i.types[op].bitfield.ymmword)
3793 {
3794 vector_length = 1;
3795 break;
3796 }
3797 }
3798
3799 /* Check the REX.W bit and VEXW. */
3800 if (i.tm.opcode_modifier.vexw == VEXWIG)
3801 w = (vexwig == vexw1 || (i.rex & REX_W)) ? 1 : 0;
3802 else if (i.tm.opcode_modifier.vexw)
3803 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3804 else
3805 w = (flag_code == CODE_64BIT ? i.rex & REX_W : vexwig == vexw1) ? 1 : 0;
3806
3807 /* Use 2-byte VEX prefix if possible. */
3808 if (w == 0
3809 && i.vec_encoding != vex_encoding_vex3
3810 && i.tm.opcode_space == SPACE_0F
3811 && (i.rex & (REX_W | REX_X | REX_B)) == 0)
3812 {
3813 /* 2-byte VEX prefix. */
3814 unsigned int r;
3815
3816 i.vex.length = 2;
3817 i.vex.bytes[0] = 0xc5;
3818
3819 /* Check the REX.R bit. */
3820 r = (i.rex & REX_R) ? 0 : 1;
3821 i.vex.bytes[1] = (r << 7
3822 | register_specifier << 3
3823 | vector_length << 2
3824 | i.tm.opcode_modifier.opcodeprefix);
3825 }
3826 else
3827 {
3828 /* 3-byte VEX prefix. */
3829 i.vex.length = 3;
3830
3831 switch (i.tm.opcode_space)
3832 {
3833 case SPACE_0F:
3834 case SPACE_0F38:
3835 case SPACE_0F3A:
3836 i.vex.bytes[0] = 0xc4;
3837 break;
3838 case SPACE_XOP08:
3839 case SPACE_XOP09:
3840 case SPACE_XOP0A:
3841 i.vex.bytes[0] = 0x8f;
3842 break;
3843 default:
3844 abort ();
3845 }
3846
3847 /* The high 3 bits of the second VEX byte are 1's compliment
3848 of RXB bits from REX. */
3849 i.vex.bytes[1] = ((~i.rex & 7) << 5)
3850 | (!dot_insn () ? i.tm.opcode_space
3851 : i.insn_opcode_space);
3852
3853 i.vex.bytes[2] = (w << 7
3854 | register_specifier << 3
3855 | vector_length << 2
3856 | i.tm.opcode_modifier.opcodeprefix);
3857 }
3858 }
3859
3860 static INLINE bool
3861 is_evex_encoding (const insn_template *t)
3862 {
3863 return t->opcode_modifier.evex || t->opcode_modifier.disp8memshift
3864 || t->opcode_modifier.broadcast || t->opcode_modifier.masking
3865 || t->opcode_modifier.sae;
3866 }
3867
3868 static INLINE bool
3869 is_any_vex_encoding (const insn_template *t)
3870 {
3871 return t->opcode_modifier.vex || is_evex_encoding (t);
3872 }
3873
3874 static unsigned int
3875 get_broadcast_bytes (const insn_template *t, bool diag)
3876 {
3877 unsigned int op, bytes;
3878 const i386_operand_type *types;
3879
3880 if (i.broadcast.type)
3881 return (1 << (t->opcode_modifier.broadcast - 1)) * i.broadcast.type;
3882
3883 gas_assert (intel_syntax);
3884
3885 for (op = 0; op < t->operands; ++op)
3886 if (t->operand_types[op].bitfield.baseindex)
3887 break;
3888
3889 gas_assert (op < t->operands);
3890
3891 if (t->opcode_modifier.evex
3892 && t->opcode_modifier.evex != EVEXDYN)
3893 switch (i.broadcast.bytes)
3894 {
3895 case 1:
3896 if (t->operand_types[op].bitfield.word)
3897 return 2;
3898 /* Fall through. */
3899 case 2:
3900 if (t->operand_types[op].bitfield.dword)
3901 return 4;
3902 /* Fall through. */
3903 case 4:
3904 if (t->operand_types[op].bitfield.qword)
3905 return 8;
3906 /* Fall through. */
3907 case 8:
3908 if (t->operand_types[op].bitfield.xmmword)
3909 return 16;
3910 if (t->operand_types[op].bitfield.ymmword)
3911 return 32;
3912 if (t->operand_types[op].bitfield.zmmword)
3913 return 64;
3914 /* Fall through. */
3915 default:
3916 abort ();
3917 }
3918
3919 gas_assert (op + 1 < t->operands);
3920
3921 if (t->operand_types[op + 1].bitfield.xmmword
3922 + t->operand_types[op + 1].bitfield.ymmword
3923 + t->operand_types[op + 1].bitfield.zmmword > 1)
3924 {
3925 types = &i.types[op + 1];
3926 diag = false;
3927 }
3928 else /* Ambiguous - guess with a preference to non-AVX512VL forms. */
3929 types = &t->operand_types[op];
3930
3931 if (types->bitfield.zmmword)
3932 bytes = 64;
3933 else if (types->bitfield.ymmword)
3934 bytes = 32;
3935 else
3936 bytes = 16;
3937
3938 if (diag)
3939 as_warn (_("ambiguous broadcast for `%s', using %u-bit form"),
3940 insn_name (t), bytes * 8);
3941
3942 return bytes;
3943 }
3944
3945 /* Build the EVEX prefix. */
3946
3947 static void
3948 build_evex_prefix (void)
3949 {
3950 unsigned int register_specifier, w;
3951 rex_byte vrex_used = 0;
3952
3953 /* Check register specifier. */
3954 if (i.vex.register_specifier)
3955 {
3956 gas_assert ((i.vrex & REX_X) == 0);
3957
3958 register_specifier = i.vex.register_specifier->reg_num;
3959 if ((i.vex.register_specifier->reg_flags & RegRex))
3960 register_specifier += 8;
3961 /* The upper 16 registers are encoded in the fourth byte of the
3962 EVEX prefix. */
3963 if (!(i.vex.register_specifier->reg_flags & RegVRex))
3964 i.vex.bytes[3] = 0x8;
3965 register_specifier = ~register_specifier & 0xf;
3966 }
3967 else
3968 {
3969 register_specifier = 0xf;
3970
3971 /* Encode upper 16 vector index register in the fourth byte of
3972 the EVEX prefix. */
3973 if (!(i.vrex & REX_X))
3974 i.vex.bytes[3] = 0x8;
3975 else
3976 vrex_used |= REX_X;
3977 }
3978
3979 /* 4 byte EVEX prefix. */
3980 i.vex.length = 4;
3981 i.vex.bytes[0] = 0x62;
3982
3983 /* The high 3 bits of the second EVEX byte are 1's compliment of RXB
3984 bits from REX. */
3985 gas_assert (i.tm.opcode_space >= SPACE_0F);
3986 gas_assert (i.tm.opcode_space <= SPACE_EVEXMAP6);
3987 i.vex.bytes[1] = ((~i.rex & 7) << 5)
3988 | (!dot_insn () ? i.tm.opcode_space
3989 : i.insn_opcode_space);
3990
3991 /* The fifth bit of the second EVEX byte is 1's compliment of the
3992 REX_R bit in VREX. */
3993 if (!(i.vrex & REX_R))
3994 i.vex.bytes[1] |= 0x10;
3995 else
3996 vrex_used |= REX_R;
3997
3998 if ((i.reg_operands + i.imm_operands) == i.operands)
3999 {
4000 /* When all operands are registers, the REX_X bit in REX is not
4001 used. We reuse it to encode the upper 16 registers, which is
4002 indicated by the REX_B bit in VREX. The REX_X bit is encoded
4003 as 1's compliment. */
4004 if ((i.vrex & REX_B))
4005 {
4006 vrex_used |= REX_B;
4007 i.vex.bytes[1] &= ~0x40;
4008 }
4009 }
4010
4011 /* EVEX instructions shouldn't need the REX prefix. */
4012 i.vrex &= ~vrex_used;
4013 gas_assert (i.vrex == 0);
4014
4015 /* Check the REX.W bit and VEXW. */
4016 if (i.tm.opcode_modifier.vexw == VEXWIG)
4017 w = (evexwig == evexw1 || (i.rex & REX_W)) ? 1 : 0;
4018 else if (i.tm.opcode_modifier.vexw)
4019 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
4020 else
4021 w = (flag_code == CODE_64BIT ? i.rex & REX_W : evexwig == evexw1) ? 1 : 0;
4022
4023 /* The third byte of the EVEX prefix. */
4024 i.vex.bytes[2] = ((w << 7)
4025 | (register_specifier << 3)
4026 | 4 /* Encode the U bit. */
4027 | i.tm.opcode_modifier.opcodeprefix);
4028
4029 /* The fourth byte of the EVEX prefix. */
4030 /* The zeroing-masking bit. */
4031 if (i.mask.reg && i.mask.zeroing)
4032 i.vex.bytes[3] |= 0x80;
4033
4034 /* Don't always set the broadcast bit if there is no RC. */
4035 if (i.rounding.type == rc_none)
4036 {
4037 /* Encode the vector length. */
4038 unsigned int vec_length;
4039
4040 if (!i.tm.opcode_modifier.evex
4041 || i.tm.opcode_modifier.evex == EVEXDYN)
4042 {
4043 unsigned int op;
4044
4045 /* Determine vector length from the last multi-length vector
4046 operand. */
4047 for (op = i.operands; op--;)
4048 if (i.tm.operand_types[op].bitfield.xmmword
4049 + i.tm.operand_types[op].bitfield.ymmword
4050 + i.tm.operand_types[op].bitfield.zmmword > 1)
4051 {
4052 if (i.types[op].bitfield.zmmword)
4053 {
4054 i.tm.opcode_modifier.evex = EVEX512;
4055 break;
4056 }
4057 else if (i.types[op].bitfield.ymmword)
4058 {
4059 i.tm.opcode_modifier.evex = EVEX256;
4060 break;
4061 }
4062 else if (i.types[op].bitfield.xmmword)
4063 {
4064 i.tm.opcode_modifier.evex = EVEX128;
4065 break;
4066 }
4067 else if ((i.broadcast.type || i.broadcast.bytes)
4068 && op == i.broadcast.operand)
4069 {
4070 switch (get_broadcast_bytes (&i.tm, true))
4071 {
4072 case 64:
4073 i.tm.opcode_modifier.evex = EVEX512;
4074 break;
4075 case 32:
4076 i.tm.opcode_modifier.evex = EVEX256;
4077 break;
4078 case 16:
4079 i.tm.opcode_modifier.evex = EVEX128;
4080 break;
4081 default:
4082 abort ();
4083 }
4084 break;
4085 }
4086 }
4087
4088 if (op >= MAX_OPERANDS)
4089 abort ();
4090 }
4091
4092 switch (i.tm.opcode_modifier.evex)
4093 {
4094 case EVEXLIG: /* LL' is ignored */
4095 vec_length = evexlig << 5;
4096 break;
4097 case EVEX128:
4098 vec_length = 0 << 5;
4099 break;
4100 case EVEX256:
4101 vec_length = 1 << 5;
4102 break;
4103 case EVEX512:
4104 vec_length = 2 << 5;
4105 break;
4106 case EVEX_L3:
4107 if (dot_insn ())
4108 {
4109 vec_length = 3 << 5;
4110 break;
4111 }
4112 /* Fall through. */
4113 default:
4114 abort ();
4115 break;
4116 }
4117 i.vex.bytes[3] |= vec_length;
4118 /* Encode the broadcast bit. */
4119 if (i.broadcast.type || i.broadcast.bytes)
4120 i.vex.bytes[3] |= 0x10;
4121 }
4122 else if (i.rounding.type != saeonly)
4123 i.vex.bytes[3] |= 0x10 | (i.rounding.type << 5);
4124 else
4125 i.vex.bytes[3] |= 0x10 | (evexrcig << 5);
4126
4127 if (i.mask.reg)
4128 i.vex.bytes[3] |= i.mask.reg->reg_num;
4129 }
4130
4131 static void
4132 process_immext (void)
4133 {
4134 expressionS *exp;
4135
4136 /* These AMD 3DNow! and SSE2 instructions have an opcode suffix
4137 which is coded in the same place as an 8-bit immediate field
4138 would be. Here we fake an 8-bit immediate operand from the
4139 opcode suffix stored in tm.extension_opcode.
4140
4141 AVX instructions also use this encoding, for some of
4142 3 argument instructions. */
4143
4144 gas_assert (i.imm_operands <= 1
4145 && (i.operands <= 2
4146 || (is_any_vex_encoding (&i.tm)
4147 && i.operands <= 4)));
4148
4149 exp = &im_expressions[i.imm_operands++];
4150 i.op[i.operands].imms = exp;
4151 i.types[i.operands].bitfield.imm8 = 1;
4152 i.operands++;
4153 exp->X_op = O_constant;
4154 exp->X_add_number = i.tm.extension_opcode;
4155 i.tm.extension_opcode = None;
4156 }
4157
4158
4159 static int
4160 check_hle (void)
4161 {
4162 switch (i.tm.opcode_modifier.prefixok)
4163 {
4164 default:
4165 abort ();
4166 case PrefixLock:
4167 case PrefixNone:
4168 case PrefixNoTrack:
4169 case PrefixRep:
4170 as_bad (_("invalid instruction `%s' after `%s'"),
4171 insn_name (&i.tm), i.hle_prefix);
4172 return 0;
4173 case PrefixHLELock:
4174 if (i.prefix[LOCK_PREFIX])
4175 return 1;
4176 as_bad (_("missing `lock' with `%s'"), i.hle_prefix);
4177 return 0;
4178 case PrefixHLEAny:
4179 return 1;
4180 case PrefixHLERelease:
4181 if (i.prefix[HLE_PREFIX] != XRELEASE_PREFIX_OPCODE)
4182 {
4183 as_bad (_("instruction `%s' after `xacquire' not allowed"),
4184 insn_name (&i.tm));
4185 return 0;
4186 }
4187 if (i.mem_operands == 0 || !(i.flags[i.operands - 1] & Operand_Mem))
4188 {
4189 as_bad (_("memory destination needed for instruction `%s'"
4190 " after `xrelease'"), insn_name (&i.tm));
4191 return 0;
4192 }
4193 return 1;
4194 }
4195 }
4196
4197 /* Encode aligned vector move as unaligned vector move. */
4198
4199 static void
4200 encode_with_unaligned_vector_move (void)
4201 {
4202 switch (i.tm.base_opcode)
4203 {
4204 case 0x28: /* Load instructions. */
4205 case 0x29: /* Store instructions. */
4206 /* movaps/movapd/vmovaps/vmovapd. */
4207 if (i.tm.opcode_space == SPACE_0F
4208 && i.tm.opcode_modifier.opcodeprefix <= PREFIX_0X66)
4209 i.tm.base_opcode = 0x10 | (i.tm.base_opcode & 1);
4210 break;
4211 case 0x6f: /* Load instructions. */
4212 case 0x7f: /* Store instructions. */
4213 /* movdqa/vmovdqa/vmovdqa64/vmovdqa32. */
4214 if (i.tm.opcode_space == SPACE_0F
4215 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0X66)
4216 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
4217 break;
4218 default:
4219 break;
4220 }
4221 }
4222
4223 /* Try the shortest encoding by shortening operand size. */
4224
4225 static void
4226 optimize_encoding (void)
4227 {
4228 unsigned int j;
4229
4230 if (i.tm.mnem_off == MN_lea)
4231 {
4232 /* Optimize: -O:
4233 lea symbol, %rN -> mov $symbol, %rN
4234 lea (%rM), %rN -> mov %rM, %rN
4235 lea (,%rM,1), %rN -> mov %rM, %rN
4236
4237 and in 32-bit mode for 16-bit addressing
4238
4239 lea (%rM), %rN -> movzx %rM, %rN
4240
4241 and in 64-bit mode zap 32-bit addressing in favor of using a
4242 32-bit (or less) destination.
4243 */
4244 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
4245 {
4246 if (!i.op[1].regs->reg_type.bitfield.word)
4247 i.tm.opcode_modifier.size = SIZE32;
4248 i.prefix[ADDR_PREFIX] = 0;
4249 }
4250
4251 if (!i.index_reg && !i.base_reg)
4252 {
4253 /* Handle:
4254 lea symbol, %rN -> mov $symbol, %rN
4255 */
4256 if (flag_code == CODE_64BIT)
4257 {
4258 /* Don't transform a relocation to a 16-bit one. */
4259 if (i.op[0].disps
4260 && i.op[0].disps->X_op != O_constant
4261 && i.op[1].regs->reg_type.bitfield.word)
4262 return;
4263
4264 if (!i.op[1].regs->reg_type.bitfield.qword
4265 || i.tm.opcode_modifier.size == SIZE32)
4266 {
4267 i.tm.base_opcode = 0xb8;
4268 i.tm.opcode_modifier.modrm = 0;
4269 if (!i.op[1].regs->reg_type.bitfield.word)
4270 i.types[0].bitfield.imm32 = 1;
4271 else
4272 {
4273 i.tm.opcode_modifier.size = SIZE16;
4274 i.types[0].bitfield.imm16 = 1;
4275 }
4276 }
4277 else
4278 {
4279 /* Subject to further optimization below. */
4280 i.tm.base_opcode = 0xc7;
4281 i.tm.extension_opcode = 0;
4282 i.types[0].bitfield.imm32s = 1;
4283 i.types[0].bitfield.baseindex = 0;
4284 }
4285 }
4286 /* Outside of 64-bit mode address and operand sizes have to match if
4287 a relocation is involved, as otherwise we wouldn't (currently) or
4288 even couldn't express the relocation correctly. */
4289 else if (i.op[0].disps
4290 && i.op[0].disps->X_op != O_constant
4291 && ((!i.prefix[ADDR_PREFIX])
4292 != (flag_code == CODE_32BIT
4293 ? i.op[1].regs->reg_type.bitfield.dword
4294 : i.op[1].regs->reg_type.bitfield.word)))
4295 return;
4296 /* In 16-bit mode converting LEA with 16-bit addressing and a 32-bit
4297 destination is going to grow encoding size. */
4298 else if (flag_code == CODE_16BIT
4299 && (optimize <= 1 || optimize_for_space)
4300 && !i.prefix[ADDR_PREFIX]
4301 && i.op[1].regs->reg_type.bitfield.dword)
4302 return;
4303 else
4304 {
4305 i.tm.base_opcode = 0xb8;
4306 i.tm.opcode_modifier.modrm = 0;
4307 if (i.op[1].regs->reg_type.bitfield.dword)
4308 i.types[0].bitfield.imm32 = 1;
4309 else
4310 i.types[0].bitfield.imm16 = 1;
4311
4312 if (i.op[0].disps
4313 && i.op[0].disps->X_op == O_constant
4314 && i.op[1].regs->reg_type.bitfield.dword
4315 /* NB: Add () to !i.prefix[ADDR_PREFIX] to silence
4316 GCC 5. */
4317 && (!i.prefix[ADDR_PREFIX]) != (flag_code == CODE_32BIT))
4318 i.op[0].disps->X_add_number &= 0xffff;
4319 }
4320
4321 i.tm.operand_types[0] = i.types[0];
4322 i.imm_operands = 1;
4323 if (!i.op[0].imms)
4324 {
4325 i.op[0].imms = &im_expressions[0];
4326 i.op[0].imms->X_op = O_absent;
4327 }
4328 }
4329 else if (i.op[0].disps
4330 && (i.op[0].disps->X_op != O_constant
4331 || i.op[0].disps->X_add_number))
4332 return;
4333 else
4334 {
4335 /* Handle:
4336 lea (%rM), %rN -> mov %rM, %rN
4337 lea (,%rM,1), %rN -> mov %rM, %rN
4338 lea (%rM), %rN -> movzx %rM, %rN
4339 */
4340 const reg_entry *addr_reg;
4341
4342 if (!i.index_reg && i.base_reg->reg_num != RegIP)
4343 addr_reg = i.base_reg;
4344 else if (!i.base_reg
4345 && i.index_reg->reg_num != RegIZ
4346 && !i.log2_scale_factor)
4347 addr_reg = i.index_reg;
4348 else
4349 return;
4350
4351 if (addr_reg->reg_type.bitfield.word
4352 && i.op[1].regs->reg_type.bitfield.dword)
4353 {
4354 if (flag_code != CODE_32BIT)
4355 return;
4356 i.tm.opcode_space = SPACE_0F;
4357 i.tm.base_opcode = 0xb7;
4358 }
4359 else
4360 i.tm.base_opcode = 0x8b;
4361
4362 if (addr_reg->reg_type.bitfield.dword
4363 && i.op[1].regs->reg_type.bitfield.qword)
4364 i.tm.opcode_modifier.size = SIZE32;
4365
4366 i.op[0].regs = addr_reg;
4367 i.reg_operands = 2;
4368 }
4369
4370 i.mem_operands = 0;
4371 i.disp_operands = 0;
4372 i.prefix[ADDR_PREFIX] = 0;
4373 i.prefix[SEG_PREFIX] = 0;
4374 i.seg[0] = NULL;
4375 }
4376
4377 if (optimize_for_space
4378 && i.tm.mnem_off == MN_test
4379 && i.reg_operands == 1
4380 && i.imm_operands == 1
4381 && !i.types[1].bitfield.byte
4382 && i.op[0].imms->X_op == O_constant
4383 && fits_in_imm7 (i.op[0].imms->X_add_number))
4384 {
4385 /* Optimize: -Os:
4386 test $imm7, %r64/%r32/%r16 -> test $imm7, %r8
4387 */
4388 unsigned int base_regnum = i.op[1].regs->reg_num;
4389 if (flag_code == CODE_64BIT || base_regnum < 4)
4390 {
4391 i.types[1].bitfield.byte = 1;
4392 /* Ignore the suffix. */
4393 i.suffix = 0;
4394 /* Convert to byte registers. */
4395 if (i.types[1].bitfield.word)
4396 j = 16;
4397 else if (i.types[1].bitfield.dword)
4398 j = 32;
4399 else
4400 j = 48;
4401 if (!(i.op[1].regs->reg_flags & RegRex) && base_regnum < 4)
4402 j += 8;
4403 i.op[1].regs -= j;
4404 }
4405 }
4406 else if (flag_code == CODE_64BIT
4407 && i.tm.opcode_space == SPACE_BASE
4408 && ((i.types[1].bitfield.qword
4409 && i.reg_operands == 1
4410 && i.imm_operands == 1
4411 && i.op[0].imms->X_op == O_constant
4412 && ((i.tm.base_opcode == 0xb8
4413 && i.tm.extension_opcode == None
4414 && fits_in_unsigned_long (i.op[0].imms->X_add_number))
4415 || (fits_in_imm31 (i.op[0].imms->X_add_number)
4416 && (i.tm.base_opcode == 0x24
4417 || (i.tm.base_opcode == 0x80
4418 && i.tm.extension_opcode == 0x4)
4419 || i.tm.mnem_off == MN_test
4420 || ((i.tm.base_opcode | 1) == 0xc7
4421 && i.tm.extension_opcode == 0x0)))
4422 || (fits_in_imm7 (i.op[0].imms->X_add_number)
4423 && i.tm.base_opcode == 0x83
4424 && i.tm.extension_opcode == 0x4)))
4425 || (i.types[0].bitfield.qword
4426 && ((i.reg_operands == 2
4427 && i.op[0].regs == i.op[1].regs
4428 && (i.tm.mnem_off == MN_xor
4429 || i.tm.mnem_off == MN_sub))
4430 || i.tm.mnem_off == MN_clr))))
4431 {
4432 /* Optimize: -O:
4433 andq $imm31, %r64 -> andl $imm31, %r32
4434 andq $imm7, %r64 -> andl $imm7, %r32
4435 testq $imm31, %r64 -> testl $imm31, %r32
4436 xorq %r64, %r64 -> xorl %r32, %r32
4437 subq %r64, %r64 -> subl %r32, %r32
4438 movq $imm31, %r64 -> movl $imm31, %r32
4439 movq $imm32, %r64 -> movl $imm32, %r32
4440 */
4441 i.tm.opcode_modifier.size = SIZE32;
4442 if (i.imm_operands)
4443 {
4444 i.types[0].bitfield.imm32 = 1;
4445 i.types[0].bitfield.imm32s = 0;
4446 i.types[0].bitfield.imm64 = 0;
4447 }
4448 else
4449 {
4450 i.types[0].bitfield.dword = 1;
4451 i.types[0].bitfield.qword = 0;
4452 }
4453 i.types[1].bitfield.dword = 1;
4454 i.types[1].bitfield.qword = 0;
4455 if (i.tm.mnem_off == MN_mov || i.tm.mnem_off == MN_lea)
4456 {
4457 /* Handle
4458 movq $imm31, %r64 -> movl $imm31, %r32
4459 movq $imm32, %r64 -> movl $imm32, %r32
4460 */
4461 i.tm.operand_types[0].bitfield.imm32 = 1;
4462 i.tm.operand_types[0].bitfield.imm32s = 0;
4463 i.tm.operand_types[0].bitfield.imm64 = 0;
4464 if ((i.tm.base_opcode | 1) == 0xc7)
4465 {
4466 /* Handle
4467 movq $imm31, %r64 -> movl $imm31, %r32
4468 */
4469 i.tm.base_opcode = 0xb8;
4470 i.tm.extension_opcode = None;
4471 i.tm.opcode_modifier.w = 0;
4472 i.tm.opcode_modifier.modrm = 0;
4473 }
4474 }
4475 }
4476 else if (optimize > 1
4477 && !optimize_for_space
4478 && i.reg_operands == 2
4479 && i.op[0].regs == i.op[1].regs
4480 && (i.tm.mnem_off == MN_and || i.tm.mnem_off == MN_or)
4481 && (flag_code != CODE_64BIT || !i.types[0].bitfield.dword))
4482 {
4483 /* Optimize: -O2:
4484 andb %rN, %rN -> testb %rN, %rN
4485 andw %rN, %rN -> testw %rN, %rN
4486 andq %rN, %rN -> testq %rN, %rN
4487 orb %rN, %rN -> testb %rN, %rN
4488 orw %rN, %rN -> testw %rN, %rN
4489 orq %rN, %rN -> testq %rN, %rN
4490
4491 and outside of 64-bit mode
4492
4493 andl %rN, %rN -> testl %rN, %rN
4494 orl %rN, %rN -> testl %rN, %rN
4495 */
4496 i.tm.base_opcode = 0x84 | (i.tm.base_opcode & 1);
4497 }
4498 else if (i.tm.base_opcode == 0xba
4499 && i.tm.opcode_space == SPACE_0F
4500 && i.reg_operands == 1
4501 && i.op[0].imms->X_op == O_constant
4502 && i.op[0].imms->X_add_number >= 0)
4503 {
4504 /* Optimize: -O:
4505 btw $n, %rN -> btl $n, %rN (outside of 16-bit mode, n < 16)
4506 btq $n, %rN -> btl $n, %rN (in 64-bit mode, n < 32, N < 8)
4507 btl $n, %rN -> btw $n, %rN (in 16-bit mode, n < 16)
4508
4509 With <BT> one of bts, btr, and bts also:
4510 <BT>w $n, %rN -> btl $n, %rN (in 32-bit mode, n < 16)
4511 <BT>l $n, %rN -> btw $n, %rN (in 16-bit mode, n < 16)
4512 */
4513 switch (flag_code)
4514 {
4515 case CODE_64BIT:
4516 if (i.tm.extension_opcode != 4)
4517 break;
4518 if (i.types[1].bitfield.qword
4519 && i.op[0].imms->X_add_number < 32
4520 && !(i.op[1].regs->reg_flags & RegRex))
4521 i.tm.opcode_modifier.size = SIZE32;
4522 /* Fall through. */
4523 case CODE_32BIT:
4524 if (i.types[1].bitfield.word
4525 && i.op[0].imms->X_add_number < 16)
4526 i.tm.opcode_modifier.size = SIZE32;
4527 break;
4528 case CODE_16BIT:
4529 if (i.op[0].imms->X_add_number < 16)
4530 i.tm.opcode_modifier.size = SIZE16;
4531 break;
4532 }
4533 }
4534 else if (i.reg_operands == 3
4535 && i.op[0].regs == i.op[1].regs
4536 && !i.types[2].bitfield.xmmword
4537 && (i.tm.opcode_modifier.vex
4538 || ((!i.mask.reg || i.mask.zeroing)
4539 && is_evex_encoding (&i.tm)
4540 && (i.vec_encoding != vex_encoding_evex
4541 || cpu_arch_isa_flags.bitfield.cpuavx512vl
4542 || is_cpu (&i.tm, CpuAVX512VL)
4543 || (i.tm.operand_types[2].bitfield.zmmword
4544 && i.types[2].bitfield.ymmword))))
4545 && i.tm.opcode_space == SPACE_0F
4546 && ((i.tm.base_opcode | 2) == 0x57
4547 || i.tm.base_opcode == 0xdf
4548 || i.tm.base_opcode == 0xef
4549 || (i.tm.base_opcode | 3) == 0xfb
4550 || i.tm.base_opcode == 0x42
4551 || i.tm.base_opcode == 0x47))
4552 {
4553 /* Optimize: -O1:
4554 VOP, one of vandnps, vandnpd, vxorps, vxorpd, vpsubb, vpsubd,
4555 vpsubq and vpsubw:
4556 EVEX VOP %zmmM, %zmmM, %zmmN
4557 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
4558 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4559 EVEX VOP %ymmM, %ymmM, %ymmN
4560 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
4561 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4562 VEX VOP %ymmM, %ymmM, %ymmN
4563 -> VEX VOP %xmmM, %xmmM, %xmmN
4564 VOP, one of vpandn and vpxor:
4565 VEX VOP %ymmM, %ymmM, %ymmN
4566 -> VEX VOP %xmmM, %xmmM, %xmmN
4567 VOP, one of vpandnd and vpandnq:
4568 EVEX VOP %zmmM, %zmmM, %zmmN
4569 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
4570 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4571 EVEX VOP %ymmM, %ymmM, %ymmN
4572 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
4573 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4574 VOP, one of vpxord and vpxorq:
4575 EVEX VOP %zmmM, %zmmM, %zmmN
4576 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
4577 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4578 EVEX VOP %ymmM, %ymmM, %ymmN
4579 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
4580 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4581 VOP, one of kxord and kxorq:
4582 VEX VOP %kM, %kM, %kN
4583 -> VEX kxorw %kM, %kM, %kN
4584 VOP, one of kandnd and kandnq:
4585 VEX VOP %kM, %kM, %kN
4586 -> VEX kandnw %kM, %kM, %kN
4587 */
4588 if (is_evex_encoding (&i.tm))
4589 {
4590 if (i.vec_encoding != vex_encoding_evex)
4591 {
4592 i.tm.opcode_modifier.vex = VEX128;
4593 i.tm.opcode_modifier.vexw = VEXW0;
4594 i.tm.opcode_modifier.evex = 0;
4595 i.vec_encoding = vex_encoding_vex;
4596 i.mask.reg = NULL;
4597 }
4598 else if (optimize > 1)
4599 i.tm.opcode_modifier.evex = EVEX128;
4600 else
4601 return;
4602 }
4603 else if (i.tm.operand_types[0].bitfield.class == RegMask)
4604 {
4605 i.tm.opcode_modifier.opcodeprefix = PREFIX_NONE;
4606 i.tm.opcode_modifier.vexw = VEXW0;
4607 }
4608 else
4609 i.tm.opcode_modifier.vex = VEX128;
4610
4611 if (i.tm.opcode_modifier.vex)
4612 for (j = 0; j < 3; j++)
4613 {
4614 i.types[j].bitfield.xmmword = 1;
4615 i.types[j].bitfield.ymmword = 0;
4616 }
4617 }
4618 else if (i.vec_encoding != vex_encoding_evex
4619 && !i.types[0].bitfield.zmmword
4620 && !i.types[1].bitfield.zmmword
4621 && !i.mask.reg
4622 && !i.broadcast.type
4623 && !i.broadcast.bytes
4624 && is_evex_encoding (&i.tm)
4625 && ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4626 || (i.tm.base_opcode & ~4) == 0xdb
4627 || (i.tm.base_opcode & ~4) == 0xeb)
4628 && i.tm.extension_opcode == None)
4629 {
4630 /* Optimize: -O1:
4631 VOP, one of vmovdqa32, vmovdqa64, vmovdqu8, vmovdqu16,
4632 vmovdqu32 and vmovdqu64:
4633 EVEX VOP %xmmM, %xmmN
4634 -> VEX vmovdqa|vmovdqu %xmmM, %xmmN (M and N < 16)
4635 EVEX VOP %ymmM, %ymmN
4636 -> VEX vmovdqa|vmovdqu %ymmM, %ymmN (M and N < 16)
4637 EVEX VOP %xmmM, mem
4638 -> VEX vmovdqa|vmovdqu %xmmM, mem (M < 16)
4639 EVEX VOP %ymmM, mem
4640 -> VEX vmovdqa|vmovdqu %ymmM, mem (M < 16)
4641 EVEX VOP mem, %xmmN
4642 -> VEX mvmovdqa|vmovdquem, %xmmN (N < 16)
4643 EVEX VOP mem, %ymmN
4644 -> VEX vmovdqa|vmovdqu mem, %ymmN (N < 16)
4645 VOP, one of vpand, vpandn, vpor, vpxor:
4646 EVEX VOP{d,q} %xmmL, %xmmM, %xmmN
4647 -> VEX VOP %xmmL, %xmmM, %xmmN (L, M, and N < 16)
4648 EVEX VOP{d,q} %ymmL, %ymmM, %ymmN
4649 -> VEX VOP %ymmL, %ymmM, %ymmN (L, M, and N < 16)
4650 EVEX VOP{d,q} mem, %xmmM, %xmmN
4651 -> VEX VOP mem, %xmmM, %xmmN (M and N < 16)
4652 EVEX VOP{d,q} mem, %ymmM, %ymmN
4653 -> VEX VOP mem, %ymmM, %ymmN (M and N < 16)
4654 */
4655 for (j = 0; j < i.operands; j++)
4656 if (operand_type_check (i.types[j], disp)
4657 && i.op[j].disps->X_op == O_constant)
4658 {
4659 /* Since the VEX prefix has 2 or 3 bytes, the EVEX prefix
4660 has 4 bytes, EVEX Disp8 has 1 byte and VEX Disp32 has 4
4661 bytes, we choose EVEX Disp8 over VEX Disp32. */
4662 int evex_disp8, vex_disp8;
4663 unsigned int memshift = i.memshift;
4664 offsetT n = i.op[j].disps->X_add_number;
4665
4666 evex_disp8 = fits_in_disp8 (n);
4667 i.memshift = 0;
4668 vex_disp8 = fits_in_disp8 (n);
4669 if (evex_disp8 != vex_disp8)
4670 {
4671 i.memshift = memshift;
4672 return;
4673 }
4674
4675 i.types[j].bitfield.disp8 = vex_disp8;
4676 break;
4677 }
4678 if ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4679 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2)
4680 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
4681 i.tm.opcode_modifier.vex
4682 = i.types[0].bitfield.ymmword ? VEX256 : VEX128;
4683 i.tm.opcode_modifier.vexw = VEXW0;
4684 /* VPAND, VPOR, and VPXOR are commutative. */
4685 if (i.reg_operands == 3 && i.tm.base_opcode != 0xdf)
4686 i.tm.opcode_modifier.commutative = 1;
4687 i.tm.opcode_modifier.evex = 0;
4688 i.tm.opcode_modifier.masking = 0;
4689 i.tm.opcode_modifier.broadcast = 0;
4690 i.tm.opcode_modifier.disp8memshift = 0;
4691 i.memshift = 0;
4692 if (j < i.operands)
4693 i.types[j].bitfield.disp8
4694 = fits_in_disp8 (i.op[j].disps->X_add_number);
4695 }
4696 else if (optimize_for_space
4697 && i.tm.base_opcode == 0x29
4698 && i.tm.opcode_space == SPACE_0F38
4699 && i.operands == i.reg_operands
4700 && i.op[0].regs == i.op[1].regs
4701 && (!i.tm.opcode_modifier.vex
4702 || !(i.op[0].regs->reg_flags & RegRex))
4703 && !is_evex_encoding (&i.tm))
4704 {
4705 /* Optimize: -Os:
4706 pcmpeqq %xmmN, %xmmN -> pcmpeqd %xmmN, %xmmN
4707 vpcmpeqq %xmmN, %xmmN, %xmmM -> vpcmpeqd %xmmN, %xmmN, %xmmM (N < 8)
4708 vpcmpeqq %ymmN, %ymmN, %ymmM -> vpcmpeqd %ymmN, %ymmN, %ymmM (N < 8)
4709 */
4710 i.tm.opcode_space = SPACE_0F;
4711 i.tm.base_opcode = 0x76;
4712 }
4713 else if (((i.tm.base_opcode >= 0x64
4714 && i.tm.base_opcode <= 0x66
4715 && i.tm.opcode_space == SPACE_0F)
4716 || (i.tm.base_opcode == 0x37
4717 && i.tm.opcode_space == SPACE_0F38))
4718 && i.operands == i.reg_operands
4719 && i.op[0].regs == i.op[1].regs
4720 && !is_evex_encoding (&i.tm))
4721 {
4722 /* Optimize: -O:
4723 pcmpgt[bwd] %mmN, %mmN -> pxor %mmN, %mmN
4724 pcmpgt[bwdq] %xmmN, %xmmN -> pxor %xmmN, %xmmN
4725 vpcmpgt[bwdq] %xmmN, %xmmN, %xmmM -> vpxor %xmmN, %xmmN, %xmmM (N < 8)
4726 vpcmpgt[bwdq] %xmmN, %xmmN, %xmmM -> vpxor %xmm0, %xmm0, %xmmM (N > 7)
4727 vpcmpgt[bwdq] %ymmN, %ymmN, %ymmM -> vpxor %ymmN, %ymmN, %ymmM (N < 8)
4728 vpcmpgt[bwdq] %ymmN, %ymmN, %ymmM -> vpxor %ymm0, %ymm0, %ymmM (N > 7)
4729 */
4730 i.tm.opcode_space = SPACE_0F;
4731 i.tm.base_opcode = 0xef;
4732 if (i.tm.opcode_modifier.vex && (i.op[0].regs->reg_flags & RegRex))
4733 {
4734 if (i.operands == 2)
4735 {
4736 gas_assert (i.tm.opcode_modifier.sse2avx);
4737
4738 i.operands = 3;
4739 i.reg_operands = 3;
4740 i.tm.operands = 3;
4741
4742 i.op[2].regs = i.op[0].regs;
4743 i.types[2] = i.types[0];
4744 i.flags[2] = i.flags[0];
4745 i.tm.operand_types[2] = i.tm.operand_types[0];
4746
4747 i.tm.opcode_modifier.sse2avx = 0;
4748 }
4749 i.op[0].regs -= i.op[0].regs->reg_num + 8;
4750 i.op[1].regs = i.op[0].regs;
4751 }
4752 }
4753 else if (optimize_for_space
4754 && i.tm.base_opcode == 0x59
4755 && i.tm.opcode_space == SPACE_0F38
4756 && i.operands == i.reg_operands
4757 && i.tm.opcode_modifier.vex
4758 && !(i.op[0].regs->reg_flags & RegRex)
4759 && i.op[0].regs->reg_type.bitfield.xmmword
4760 && i.vec_encoding != vex_encoding_vex3)
4761 {
4762 /* Optimize: -Os:
4763 vpbroadcastq %xmmN, %xmmM -> vpunpcklqdq %xmmN, %xmmN, %xmmM (N < 8)
4764 */
4765 i.tm.opcode_space = SPACE_0F;
4766 i.tm.base_opcode = 0x6c;
4767 i.tm.opcode_modifier.vexvvvv = 1;
4768
4769 ++i.operands;
4770 ++i.reg_operands;
4771 ++i.tm.operands;
4772
4773 i.op[2].regs = i.op[0].regs;
4774 i.types[2] = i.types[0];
4775 i.flags[2] = i.flags[0];
4776 i.tm.operand_types[2] = i.tm.operand_types[0];
4777
4778 swap_2_operands (1, 2);
4779 }
4780 }
4781
4782 /* Return non-zero for load instruction. */
4783
4784 static int
4785 load_insn_p (void)
4786 {
4787 unsigned int dest;
4788 int any_vex_p = is_any_vex_encoding (&i.tm);
4789 unsigned int base_opcode = i.tm.base_opcode | 1;
4790
4791 if (!any_vex_p)
4792 {
4793 /* Anysize insns: lea, invlpg, clflush, prefetch*, bndmk, bndcl, bndcu,
4794 bndcn, bndstx, bndldx, clflushopt, clwb, cldemote. */
4795 if (i.tm.opcode_modifier.operandconstraint == ANY_SIZE)
4796 return 0;
4797
4798 /* pop. */
4799 if (i.tm.mnem_off == MN_pop)
4800 return 1;
4801 }
4802
4803 if (i.tm.opcode_space == SPACE_BASE)
4804 {
4805 /* popf, popa. */
4806 if (i.tm.base_opcode == 0x9d
4807 || i.tm.base_opcode == 0x61)
4808 return 1;
4809
4810 /* movs, cmps, lods, scas. */
4811 if ((i.tm.base_opcode | 0xb) == 0xaf)
4812 return 1;
4813
4814 /* outs, xlatb. */
4815 if (base_opcode == 0x6f
4816 || i.tm.base_opcode == 0xd7)
4817 return 1;
4818 /* NB: For AMD-specific insns with implicit memory operands,
4819 they're intentionally not covered. */
4820 }
4821
4822 /* No memory operand. */
4823 if (!i.mem_operands)
4824 return 0;
4825
4826 if (any_vex_p)
4827 {
4828 if (i.tm.mnem_off == MN_vldmxcsr)
4829 return 1;
4830 }
4831 else if (i.tm.opcode_space == SPACE_BASE)
4832 {
4833 /* test, not, neg, mul, imul, div, idiv. */
4834 if (base_opcode == 0xf7 && i.tm.extension_opcode != 1)
4835 return 1;
4836
4837 /* inc, dec. */
4838 if (base_opcode == 0xff && i.tm.extension_opcode <= 1)
4839 return 1;
4840
4841 /* add, or, adc, sbb, and, sub, xor, cmp. */
4842 if (i.tm.base_opcode >= 0x80 && i.tm.base_opcode <= 0x83)
4843 return 1;
4844
4845 /* rol, ror, rcl, rcr, shl/sal, shr, sar. */
4846 if ((base_opcode == 0xc1 || (base_opcode | 2) == 0xd3)
4847 && i.tm.extension_opcode != 6)
4848 return 1;
4849
4850 /* Check for x87 instructions. */
4851 if ((base_opcode | 6) == 0xdf)
4852 {
4853 /* Skip fst, fstp, fstenv, fstcw. */
4854 if (i.tm.base_opcode == 0xd9
4855 && (i.tm.extension_opcode == 2
4856 || i.tm.extension_opcode == 3
4857 || i.tm.extension_opcode == 6
4858 || i.tm.extension_opcode == 7))
4859 return 0;
4860
4861 /* Skip fisttp, fist, fistp, fstp. */
4862 if (i.tm.base_opcode == 0xdb
4863 && (i.tm.extension_opcode == 1
4864 || i.tm.extension_opcode == 2
4865 || i.tm.extension_opcode == 3
4866 || i.tm.extension_opcode == 7))
4867 return 0;
4868
4869 /* Skip fisttp, fst, fstp, fsave, fstsw. */
4870 if (i.tm.base_opcode == 0xdd
4871 && (i.tm.extension_opcode == 1
4872 || i.tm.extension_opcode == 2
4873 || i.tm.extension_opcode == 3
4874 || i.tm.extension_opcode == 6
4875 || i.tm.extension_opcode == 7))
4876 return 0;
4877
4878 /* Skip fisttp, fist, fistp, fbstp, fistp. */
4879 if (i.tm.base_opcode == 0xdf
4880 && (i.tm.extension_opcode == 1
4881 || i.tm.extension_opcode == 2
4882 || i.tm.extension_opcode == 3
4883 || i.tm.extension_opcode == 6
4884 || i.tm.extension_opcode == 7))
4885 return 0;
4886
4887 return 1;
4888 }
4889 }
4890 else if (i.tm.opcode_space == SPACE_0F)
4891 {
4892 /* bt, bts, btr, btc. */
4893 if (i.tm.base_opcode == 0xba
4894 && (i.tm.extension_opcode | 3) == 7)
4895 return 1;
4896
4897 /* cmpxchg8b, cmpxchg16b, xrstors, vmptrld. */
4898 if (i.tm.base_opcode == 0xc7
4899 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
4900 && (i.tm.extension_opcode == 1 || i.tm.extension_opcode == 3
4901 || i.tm.extension_opcode == 6))
4902 return 1;
4903
4904 /* fxrstor, ldmxcsr, xrstor. */
4905 if (i.tm.base_opcode == 0xae
4906 && (i.tm.extension_opcode == 1
4907 || i.tm.extension_opcode == 2
4908 || i.tm.extension_opcode == 5))
4909 return 1;
4910
4911 /* lgdt, lidt, lmsw. */
4912 if (i.tm.base_opcode == 0x01
4913 && (i.tm.extension_opcode == 2
4914 || i.tm.extension_opcode == 3
4915 || i.tm.extension_opcode == 6))
4916 return 1;
4917 }
4918
4919 dest = i.operands - 1;
4920
4921 /* Check fake imm8 operand and 3 source operands. */
4922 if ((i.tm.opcode_modifier.immext
4923 || i.reg_operands + i.mem_operands == 4)
4924 && i.types[dest].bitfield.imm8)
4925 dest--;
4926
4927 /* add, or, adc, sbb, and, sub, xor, cmp, test, xchg. */
4928 if (i.tm.opcode_space == SPACE_BASE
4929 && ((base_opcode | 0x38) == 0x39
4930 || (base_opcode | 2) == 0x87))
4931 return 1;
4932
4933 if (i.tm.mnem_off == MN_xadd)
4934 return 1;
4935
4936 /* Check for load instruction. */
4937 return (i.types[dest].bitfield.class != ClassNone
4938 || i.types[dest].bitfield.instance == Accum);
4939 }
4940
4941 /* Output lfence, 0xfaee8, after instruction. */
4942
4943 static void
4944 insert_lfence_after (void)
4945 {
4946 if (lfence_after_load && load_insn_p ())
4947 {
4948 /* There are also two REP string instructions that require
4949 special treatment. Specifically, the compare string (CMPS)
4950 and scan string (SCAS) instructions set EFLAGS in a manner
4951 that depends on the data being compared/scanned. When used
4952 with a REP prefix, the number of iterations may therefore
4953 vary depending on this data. If the data is a program secret
4954 chosen by the adversary using an LVI method,
4955 then this data-dependent behavior may leak some aspect
4956 of the secret. */
4957 if (((i.tm.base_opcode | 0x9) == 0xaf)
4958 && i.prefix[REP_PREFIX])
4959 {
4960 as_warn (_("`%s` changes flags which would affect control flow behavior"),
4961 insn_name (&i.tm));
4962 }
4963 char *p = frag_more (3);
4964 *p++ = 0xf;
4965 *p++ = 0xae;
4966 *p = 0xe8;
4967 }
4968 }
4969
4970 /* Output lfence, 0xfaee8, before instruction. */
4971
4972 static void
4973 insert_lfence_before (void)
4974 {
4975 char *p;
4976
4977 if (i.tm.opcode_space != SPACE_BASE)
4978 return;
4979
4980 if (i.tm.base_opcode == 0xff
4981 && (i.tm.extension_opcode == 2 || i.tm.extension_opcode == 4))
4982 {
4983 /* Insert lfence before indirect branch if needed. */
4984
4985 if (lfence_before_indirect_branch == lfence_branch_none)
4986 return;
4987
4988 if (i.operands != 1)
4989 abort ();
4990
4991 if (i.reg_operands == 1)
4992 {
4993 /* Indirect branch via register. Don't insert lfence with
4994 -mlfence-after-load=yes. */
4995 if (lfence_after_load
4996 || lfence_before_indirect_branch == lfence_branch_memory)
4997 return;
4998 }
4999 else if (i.mem_operands == 1
5000 && lfence_before_indirect_branch != lfence_branch_register)
5001 {
5002 as_warn (_("indirect `%s` with memory operand should be avoided"),
5003 insn_name (&i.tm));
5004 return;
5005 }
5006 else
5007 return;
5008
5009 if (last_insn.kind != last_insn_other
5010 && last_insn.seg == now_seg)
5011 {
5012 as_warn_where (last_insn.file, last_insn.line,
5013 _("`%s` skips -mlfence-before-indirect-branch on `%s`"),
5014 last_insn.name, insn_name (&i.tm));
5015 return;
5016 }
5017
5018 p = frag_more (3);
5019 *p++ = 0xf;
5020 *p++ = 0xae;
5021 *p = 0xe8;
5022 return;
5023 }
5024
5025 /* Output or/not/shl and lfence before near ret. */
5026 if (lfence_before_ret != lfence_before_ret_none
5027 && (i.tm.base_opcode | 1) == 0xc3)
5028 {
5029 if (last_insn.kind != last_insn_other
5030 && last_insn.seg == now_seg)
5031 {
5032 as_warn_where (last_insn.file, last_insn.line,
5033 _("`%s` skips -mlfence-before-ret on `%s`"),
5034 last_insn.name, insn_name (&i.tm));
5035 return;
5036 }
5037
5038 /* Near ret ingore operand size override under CPU64. */
5039 char prefix = flag_code == CODE_64BIT
5040 ? 0x48
5041 : i.prefix[DATA_PREFIX] ? 0x66 : 0x0;
5042
5043 if (lfence_before_ret == lfence_before_ret_not)
5044 {
5045 /* not: 0xf71424, may add prefix
5046 for operand size override or 64-bit code. */
5047 p = frag_more ((prefix ? 2 : 0) + 6 + 3);
5048 if (prefix)
5049 *p++ = prefix;
5050 *p++ = 0xf7;
5051 *p++ = 0x14;
5052 *p++ = 0x24;
5053 if (prefix)
5054 *p++ = prefix;
5055 *p++ = 0xf7;
5056 *p++ = 0x14;
5057 *p++ = 0x24;
5058 }
5059 else
5060 {
5061 p = frag_more ((prefix ? 1 : 0) + 4 + 3);
5062 if (prefix)
5063 *p++ = prefix;
5064 if (lfence_before_ret == lfence_before_ret_or)
5065 {
5066 /* or: 0x830c2400, may add prefix
5067 for operand size override or 64-bit code. */
5068 *p++ = 0x83;
5069 *p++ = 0x0c;
5070 }
5071 else
5072 {
5073 /* shl: 0xc1242400, may add prefix
5074 for operand size override or 64-bit code. */
5075 *p++ = 0xc1;
5076 *p++ = 0x24;
5077 }
5078
5079 *p++ = 0x24;
5080 *p++ = 0x0;
5081 }
5082
5083 *p++ = 0xf;
5084 *p++ = 0xae;
5085 *p = 0xe8;
5086 }
5087 }
5088
5089 /* Shared helper for md_assemble() and s_insn(). */
5090 static void init_globals (void)
5091 {
5092 unsigned int j;
5093
5094 memset (&i, '\0', sizeof (i));
5095 i.rounding.type = rc_none;
5096 for (j = 0; j < MAX_OPERANDS; j++)
5097 i.reloc[j] = NO_RELOC;
5098 memset (disp_expressions, '\0', sizeof (disp_expressions));
5099 memset (im_expressions, '\0', sizeof (im_expressions));
5100 save_stack_p = save_stack;
5101 }
5102
5103 /* Helper for md_assemble() to decide whether to prepare for a possible 2nd
5104 parsing pass. Instead of introducing a rarely use new insn attribute this
5105 utilizes a common pattern between affected templates. It is deemed
5106 acceptable that this will lead to unnecessary pass 2 preparations in a
5107 limited set of cases. */
5108 static INLINE bool may_need_pass2 (const insn_template *t)
5109 {
5110 return t->opcode_modifier.sse2avx
5111 /* Note that all SSE2AVX templates have at least one operand. */
5112 ? t->operand_types[t->operands - 1].bitfield.class == RegSIMD
5113 : (t->opcode_space == SPACE_0F
5114 && (t->base_opcode | 1) == 0xbf)
5115 || (t->opcode_space == SPACE_BASE
5116 && t->base_opcode == 0x63);
5117 }
5118
5119 /* This is the guts of the machine-dependent assembler. LINE points to a
5120 machine dependent instruction. This function is supposed to emit
5121 the frags/bytes it assembles to. */
5122
5123 void
5124 md_assemble (char *line)
5125 {
5126 unsigned int j;
5127 char mnemonic[MAX_MNEM_SIZE], mnem_suffix = 0, *copy = NULL;
5128 const char *end, *pass1_mnem = NULL;
5129 enum i386_error pass1_err = 0;
5130 const insn_template *t;
5131
5132 /* Initialize globals. */
5133 current_templates = NULL;
5134 retry:
5135 init_globals ();
5136
5137 /* First parse an instruction mnemonic & call i386_operand for the operands.
5138 We assume that the scrubber has arranged it so that line[0] is the valid
5139 start of a (possibly prefixed) mnemonic. */
5140
5141 end = parse_insn (line, mnemonic, false);
5142 if (end == NULL)
5143 {
5144 if (pass1_mnem != NULL)
5145 goto match_error;
5146 if (i.error != no_error)
5147 {
5148 gas_assert (current_templates != NULL);
5149 if (may_need_pass2 (current_templates->start) && !i.suffix)
5150 goto no_match;
5151 /* No point in trying a 2nd pass - it'll only find the same suffix
5152 again. */
5153 mnem_suffix = i.suffix;
5154 goto match_error;
5155 }
5156 return;
5157 }
5158 t = current_templates->start;
5159 if (may_need_pass2 (t))
5160 {
5161 /* Make a copy of the full line in case we need to retry. */
5162 copy = xstrdup (line);
5163 }
5164 line += end - line;
5165 mnem_suffix = i.suffix;
5166
5167 line = parse_operands (line, mnemonic);
5168 this_operand = -1;
5169 if (line == NULL)
5170 {
5171 free (copy);
5172 return;
5173 }
5174
5175 /* Now we've parsed the mnemonic into a set of templates, and have the
5176 operands at hand. */
5177
5178 /* All Intel opcodes have reversed operands except for "bound", "enter",
5179 "invlpg*", "monitor*", "mwait*", "tpause", "umwait", "pvalidate",
5180 "rmpadjust", "rmpupdate", and "rmpquery". We also don't reverse
5181 intersegment "jmp" and "call" instructions with 2 immediate operands so
5182 that the immediate segment precedes the offset consistently in Intel and
5183 AT&T modes. */
5184 if (intel_syntax
5185 && i.operands > 1
5186 && (t->mnem_off != MN_bound)
5187 && !startswith (mnemonic, "invlpg")
5188 && !startswith (mnemonic, "monitor")
5189 && !startswith (mnemonic, "mwait")
5190 && (t->mnem_off != MN_pvalidate)
5191 && !startswith (mnemonic, "rmp")
5192 && (t->mnem_off != MN_tpause)
5193 && (t->mnem_off != MN_umwait)
5194 && !(i.operands == 2
5195 && operand_type_check (i.types[0], imm)
5196 && operand_type_check (i.types[1], imm)))
5197 swap_operands ();
5198
5199 /* The order of the immediates should be reversed
5200 for 2 immediates extrq and insertq instructions */
5201 if (i.imm_operands == 2
5202 && (t->mnem_off == MN_extrq || t->mnem_off == MN_insertq))
5203 swap_2_operands (0, 1);
5204
5205 if (i.imm_operands)
5206 optimize_imm ();
5207
5208 if (i.disp_operands && !optimize_disp (t))
5209 return;
5210
5211 /* Next, we find a template that matches the given insn,
5212 making sure the overlap of the given operands types is consistent
5213 with the template operand types. */
5214
5215 if (!(t = match_template (mnem_suffix)))
5216 {
5217 const char *err_msg;
5218
5219 if (copy && !mnem_suffix)
5220 {
5221 line = copy;
5222 copy = NULL;
5223 no_match:
5224 pass1_err = i.error;
5225 pass1_mnem = insn_name (current_templates->start);
5226 goto retry;
5227 }
5228
5229 /* If a non-/only-64bit template (group) was found in pass 1, and if
5230 _some_ template (group) was found in pass 2, squash pass 1's
5231 error. */
5232 if (pass1_err == unsupported_64bit)
5233 pass1_mnem = NULL;
5234
5235 match_error:
5236 free (copy);
5237
5238 switch (pass1_mnem ? pass1_err : i.error)
5239 {
5240 default:
5241 abort ();
5242 case operand_size_mismatch:
5243 err_msg = _("operand size mismatch");
5244 break;
5245 case operand_type_mismatch:
5246 err_msg = _("operand type mismatch");
5247 break;
5248 case register_type_mismatch:
5249 err_msg = _("register type mismatch");
5250 break;
5251 case number_of_operands_mismatch:
5252 err_msg = _("number of operands mismatch");
5253 break;
5254 case invalid_instruction_suffix:
5255 err_msg = _("invalid instruction suffix");
5256 break;
5257 case bad_imm4:
5258 err_msg = _("constant doesn't fit in 4 bits");
5259 break;
5260 case unsupported_with_intel_mnemonic:
5261 err_msg = _("unsupported with Intel mnemonic");
5262 break;
5263 case unsupported_syntax:
5264 err_msg = _("unsupported syntax");
5265 break;
5266 case unsupported:
5267 as_bad (_("unsupported instruction `%s'"),
5268 pass1_mnem ? pass1_mnem : insn_name (current_templates->start));
5269 return;
5270 case unsupported_on_arch:
5271 as_bad (_("`%s' is not supported on `%s%s'"),
5272 pass1_mnem ? pass1_mnem : insn_name (current_templates->start),
5273 cpu_arch_name ? cpu_arch_name : default_arch,
5274 cpu_sub_arch_name ? cpu_sub_arch_name : "");
5275 return;
5276 case unsupported_64bit:
5277 if (ISLOWER (mnem_suffix))
5278 {
5279 if (flag_code == CODE_64BIT)
5280 as_bad (_("`%s%c' is not supported in 64-bit mode"),
5281 pass1_mnem ? pass1_mnem : insn_name (current_templates->start),
5282 mnem_suffix);
5283 else
5284 as_bad (_("`%s%c' is only supported in 64-bit mode"),
5285 pass1_mnem ? pass1_mnem : insn_name (current_templates->start),
5286 mnem_suffix);
5287 }
5288 else
5289 {
5290 if (flag_code == CODE_64BIT)
5291 as_bad (_("`%s' is not supported in 64-bit mode"),
5292 pass1_mnem ? pass1_mnem : insn_name (current_templates->start));
5293 else
5294 as_bad (_("`%s' is only supported in 64-bit mode"),
5295 pass1_mnem ? pass1_mnem : insn_name (current_templates->start));
5296 }
5297 return;
5298 case invalid_sib_address:
5299 err_msg = _("invalid SIB address");
5300 break;
5301 case invalid_vsib_address:
5302 err_msg = _("invalid VSIB address");
5303 break;
5304 case invalid_vector_register_set:
5305 err_msg = _("mask, index, and destination registers must be distinct");
5306 break;
5307 case invalid_tmm_register_set:
5308 err_msg = _("all tmm registers must be distinct");
5309 break;
5310 case invalid_dest_and_src_register_set:
5311 err_msg = _("destination and source registers must be distinct");
5312 break;
5313 case unsupported_vector_index_register:
5314 err_msg = _("unsupported vector index register");
5315 break;
5316 case unsupported_broadcast:
5317 err_msg = _("unsupported broadcast");
5318 break;
5319 case broadcast_needed:
5320 err_msg = _("broadcast is needed for operand of such type");
5321 break;
5322 case unsupported_masking:
5323 err_msg = _("unsupported masking");
5324 break;
5325 case mask_not_on_destination:
5326 err_msg = _("mask not on destination operand");
5327 break;
5328 case no_default_mask:
5329 err_msg = _("default mask isn't allowed");
5330 break;
5331 case unsupported_rc_sae:
5332 err_msg = _("unsupported static rounding/sae");
5333 break;
5334 case invalid_register_operand:
5335 err_msg = _("invalid register operand");
5336 break;
5337 case internal_error:
5338 err_msg = _("internal error");
5339 break;
5340 }
5341 as_bad (_("%s for `%s'"), err_msg,
5342 pass1_mnem ? pass1_mnem : insn_name (current_templates->start));
5343 return;
5344 }
5345
5346 free (copy);
5347
5348 if (sse_check != check_none
5349 /* The opcode space check isn't strictly needed; it's there only to
5350 bypass the logic below when easily possible. */
5351 && t->opcode_space >= SPACE_0F
5352 && t->opcode_space <= SPACE_0F3A
5353 && !is_cpu (&i.tm, CpuSSE4a)
5354 && !is_any_vex_encoding (t))
5355 {
5356 bool simd = false;
5357
5358 for (j = 0; j < t->operands; ++j)
5359 {
5360 if (t->operand_types[j].bitfield.class == RegMMX)
5361 break;
5362 if (t->operand_types[j].bitfield.class == RegSIMD)
5363 simd = true;
5364 }
5365
5366 if (j >= t->operands && simd)
5367 (sse_check == check_warning
5368 ? as_warn
5369 : as_bad) (_("SSE instruction `%s' is used"), insn_name (&i.tm));
5370 }
5371
5372 if (i.tm.opcode_modifier.fwait)
5373 if (!add_prefix (FWAIT_OPCODE))
5374 return;
5375
5376 /* Check if REP prefix is OK. */
5377 if (i.rep_prefix && i.tm.opcode_modifier.prefixok != PrefixRep)
5378 {
5379 as_bad (_("invalid instruction `%s' after `%s'"),
5380 insn_name (&i.tm), i.rep_prefix);
5381 return;
5382 }
5383
5384 /* Check for lock without a lockable instruction. Destination operand
5385 must be memory unless it is xchg (0x86). */
5386 if (i.prefix[LOCK_PREFIX])
5387 {
5388 if (i.tm.opcode_modifier.prefixok < PrefixLock
5389 || i.mem_operands == 0
5390 || (i.tm.base_opcode != 0x86
5391 && !(i.flags[i.operands - 1] & Operand_Mem)))
5392 {
5393 as_bad (_("expecting lockable instruction after `lock'"));
5394 return;
5395 }
5396
5397 /* Zap the redundant prefix from XCHG when optimizing. */
5398 if (i.tm.base_opcode == 0x86 && optimize && !i.no_optimize)
5399 i.prefix[LOCK_PREFIX] = 0;
5400 }
5401
5402 if (is_any_vex_encoding (&i.tm)
5403 || i.tm.operand_types[i.imm_operands].bitfield.class >= RegMMX
5404 || i.tm.operand_types[i.imm_operands + 1].bitfield.class >= RegMMX)
5405 {
5406 /* Check for data size prefix on VEX/XOP/EVEX encoded and SIMD insns. */
5407 if (i.prefix[DATA_PREFIX])
5408 {
5409 as_bad (_("data size prefix invalid with `%s'"), insn_name (&i.tm));
5410 return;
5411 }
5412
5413 /* Don't allow e.g. KMOV in TLS code sequences. */
5414 for (j = i.imm_operands; j < i.operands; ++j)
5415 switch (i.reloc[j])
5416 {
5417 case BFD_RELOC_386_TLS_GOTIE:
5418 case BFD_RELOC_386_TLS_LE_32:
5419 case BFD_RELOC_X86_64_GOTTPOFF:
5420 case BFD_RELOC_X86_64_TLSLD:
5421 as_bad (_("TLS relocation cannot be used with `%s'"), insn_name (&i.tm));
5422 return;
5423 default:
5424 break;
5425 }
5426 }
5427
5428 /* Check if HLE prefix is OK. */
5429 if (i.hle_prefix && !check_hle ())
5430 return;
5431
5432 /* Check BND prefix. */
5433 if (i.bnd_prefix && !i.tm.opcode_modifier.bndprefixok)
5434 as_bad (_("expecting valid branch instruction after `bnd'"));
5435
5436 /* Check NOTRACK prefix. */
5437 if (i.notrack_prefix && i.tm.opcode_modifier.prefixok != PrefixNoTrack)
5438 as_bad (_("expecting indirect branch instruction after `notrack'"));
5439
5440 if (is_cpu (&i.tm, CpuMPX))
5441 {
5442 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
5443 as_bad (_("32-bit address isn't allowed in 64-bit MPX instructions."));
5444 else if (flag_code != CODE_16BIT
5445 ? i.prefix[ADDR_PREFIX]
5446 : i.mem_operands && !i.prefix[ADDR_PREFIX])
5447 as_bad (_("16-bit address isn't allowed in MPX instructions"));
5448 }
5449
5450 /* Insert BND prefix. */
5451 if (add_bnd_prefix && i.tm.opcode_modifier.bndprefixok)
5452 {
5453 if (!i.prefix[BND_PREFIX])
5454 add_prefix (BND_PREFIX_OPCODE);
5455 else if (i.prefix[BND_PREFIX] != BND_PREFIX_OPCODE)
5456 {
5457 as_warn (_("replacing `rep'/`repe' prefix by `bnd'"));
5458 i.prefix[BND_PREFIX] = BND_PREFIX_OPCODE;
5459 }
5460 }
5461
5462 /* Check string instruction segment overrides. */
5463 if (i.tm.opcode_modifier.isstring >= IS_STRING_ES_OP0)
5464 {
5465 gas_assert (i.mem_operands);
5466 if (!check_string ())
5467 return;
5468 i.disp_operands = 0;
5469 }
5470
5471 /* The memory operand of (%dx) should be only used with input/output
5472 instructions (base opcodes: 0x6c, 0x6e, 0xec, 0xee). */
5473 if (i.input_output_operand
5474 && ((i.tm.base_opcode | 0x82) != 0xee
5475 || i.tm.opcode_space != SPACE_BASE))
5476 {
5477 as_bad (_("input/output port address isn't allowed with `%s'"),
5478 insn_name (&i.tm));
5479 return;
5480 }
5481
5482 if (optimize && !i.no_optimize && i.tm.opcode_modifier.optimize)
5483 optimize_encoding ();
5484
5485 /* Past optimization there's no need to distinguish vex_encoding_evex and
5486 vex_encoding_evex512 anymore. */
5487 if (i.vec_encoding == vex_encoding_evex512)
5488 i.vec_encoding = vex_encoding_evex;
5489
5490 if (use_unaligned_vector_move)
5491 encode_with_unaligned_vector_move ();
5492
5493 if (!process_suffix ())
5494 return;
5495
5496 /* Check if IP-relative addressing requirements can be satisfied. */
5497 if (is_cpu (&i.tm, CpuPREFETCHI)
5498 && !(i.base_reg && i.base_reg->reg_num == RegIP))
5499 as_warn (_("'%s' only supports RIP-relative address"), insn_name (&i.tm));
5500
5501 /* Update operand types and check extended states. */
5502 for (j = 0; j < i.operands; j++)
5503 {
5504 i.types[j] = operand_type_and (i.types[j], i.tm.operand_types[j]);
5505 switch (i.tm.operand_types[j].bitfield.class)
5506 {
5507 default:
5508 break;
5509 case RegMMX:
5510 i.xstate |= xstate_mmx;
5511 break;
5512 case RegMask:
5513 i.xstate |= xstate_mask;
5514 break;
5515 case RegSIMD:
5516 if (i.tm.operand_types[j].bitfield.tmmword)
5517 i.xstate |= xstate_tmm;
5518 else if (i.tm.operand_types[j].bitfield.zmmword
5519 && !i.tm.opcode_modifier.vex
5520 && vector_size >= VSZ512)
5521 i.xstate |= xstate_zmm;
5522 else if (i.tm.operand_types[j].bitfield.ymmword
5523 && vector_size >= VSZ256)
5524 i.xstate |= xstate_ymm;
5525 else if (i.tm.operand_types[j].bitfield.xmmword)
5526 i.xstate |= xstate_xmm;
5527 break;
5528 }
5529 }
5530
5531 /* Make still unresolved immediate matches conform to size of immediate
5532 given in i.suffix. */
5533 if (!finalize_imm ())
5534 return;
5535
5536 if (i.types[0].bitfield.imm1)
5537 i.imm_operands = 0; /* kludge for shift insns. */
5538
5539 /* For insns with operands there are more diddles to do to the opcode. */
5540 if (i.operands)
5541 {
5542 if (!process_operands ())
5543 return;
5544 }
5545 else if (!quiet_warnings && i.tm.opcode_modifier.operandconstraint == UGH)
5546 {
5547 /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc. */
5548 as_warn (_("translating to `%sp'"), insn_name (&i.tm));
5549 }
5550
5551 if (is_any_vex_encoding (&i.tm))
5552 {
5553 if (!cpu_arch_flags.bitfield.cpui286)
5554 {
5555 as_bad (_("instruction `%s' isn't supported outside of protected mode."),
5556 insn_name (&i.tm));
5557 return;
5558 }
5559
5560 /* Check for explicit REX prefix. */
5561 if (i.prefix[REX_PREFIX] || i.rex_encoding)
5562 {
5563 as_bad (_("REX prefix invalid with `%s'"), insn_name (&i.tm));
5564 return;
5565 }
5566
5567 if (i.tm.opcode_modifier.vex)
5568 build_vex_prefix (t);
5569 else
5570 build_evex_prefix ();
5571
5572 /* The individual REX.RXBW bits got consumed. */
5573 i.rex &= REX_OPCODE;
5574 }
5575
5576 /* Handle conversion of 'int $3' --> special int3 insn. */
5577 if (i.tm.mnem_off == MN_int
5578 && i.op[0].imms->X_add_number == 3)
5579 {
5580 i.tm.base_opcode = INT3_OPCODE;
5581 i.imm_operands = 0;
5582 }
5583
5584 if ((i.tm.opcode_modifier.jump == JUMP
5585 || i.tm.opcode_modifier.jump == JUMP_BYTE
5586 || i.tm.opcode_modifier.jump == JUMP_DWORD)
5587 && i.op[0].disps->X_op == O_constant)
5588 {
5589 /* Convert "jmp constant" (and "call constant") to a jump (call) to
5590 the absolute address given by the constant. Since ix86 jumps and
5591 calls are pc relative, we need to generate a reloc. */
5592 i.op[0].disps->X_add_symbol = &abs_symbol;
5593 i.op[0].disps->X_op = O_symbol;
5594 }
5595
5596 /* For 8 bit registers we need an empty rex prefix. Also if the
5597 instruction already has a prefix, we need to convert old
5598 registers to new ones. */
5599
5600 if ((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte
5601 && (i.op[0].regs->reg_flags & RegRex64) != 0)
5602 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte
5603 && (i.op[1].regs->reg_flags & RegRex64) != 0)
5604 || (((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte)
5605 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte))
5606 && i.rex != 0))
5607 {
5608 int x;
5609
5610 i.rex |= REX_OPCODE;
5611 for (x = 0; x < 2; x++)
5612 {
5613 /* Look for 8 bit operand that uses old registers. */
5614 if (i.types[x].bitfield.class == Reg && i.types[x].bitfield.byte
5615 && (i.op[x].regs->reg_flags & RegRex64) == 0)
5616 {
5617 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
5618 /* In case it is "hi" register, give up. */
5619 if (i.op[x].regs->reg_num > 3)
5620 as_bad (_("can't encode register '%s%s' in an "
5621 "instruction requiring REX prefix."),
5622 register_prefix, i.op[x].regs->reg_name);
5623
5624 /* Otherwise it is equivalent to the extended register.
5625 Since the encoding doesn't change this is merely
5626 cosmetic cleanup for debug output. */
5627
5628 i.op[x].regs = i.op[x].regs + 8;
5629 }
5630 }
5631 }
5632
5633 if (i.rex == 0 && i.rex_encoding)
5634 {
5635 /* Check if we can add a REX_OPCODE byte. Look for 8 bit operand
5636 that uses legacy register. If it is "hi" register, don't add
5637 the REX_OPCODE byte. */
5638 int x;
5639 for (x = 0; x < 2; x++)
5640 if (i.types[x].bitfield.class == Reg
5641 && i.types[x].bitfield.byte
5642 && (i.op[x].regs->reg_flags & RegRex64) == 0
5643 && i.op[x].regs->reg_num > 3)
5644 {
5645 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
5646 i.rex_encoding = false;
5647 break;
5648 }
5649
5650 if (i.rex_encoding)
5651 i.rex = REX_OPCODE;
5652 }
5653
5654 if (i.rex != 0)
5655 add_prefix (REX_OPCODE | i.rex);
5656
5657 insert_lfence_before ();
5658
5659 /* We are ready to output the insn. */
5660 output_insn ();
5661
5662 insert_lfence_after ();
5663
5664 last_insn.seg = now_seg;
5665
5666 if (i.tm.opcode_modifier.isprefix)
5667 {
5668 last_insn.kind = last_insn_prefix;
5669 last_insn.name = insn_name (&i.tm);
5670 last_insn.file = as_where (&last_insn.line);
5671 }
5672 else
5673 last_insn.kind = last_insn_other;
5674 }
5675
5676 /* The Q suffix is generally valid only in 64-bit mode, with very few
5677 exceptions: fild, fistp, fisttp, and cmpxchg8b. Note that for fild
5678 and fisttp only one of their two templates is matched below: That's
5679 sufficient since other relevant attributes are the same between both
5680 respective templates. */
5681 static INLINE bool q_suffix_allowed(const insn_template *t)
5682 {
5683 return flag_code == CODE_64BIT
5684 || (t->opcode_space == SPACE_BASE
5685 && t->base_opcode == 0xdf
5686 && (t->extension_opcode & 1)) /* fild / fistp / fisttp */
5687 || t->mnem_off == MN_cmpxchg8b;
5688 }
5689
5690 static const char *
5691 parse_insn (const char *line, char *mnemonic, bool prefix_only)
5692 {
5693 const char *l = line, *token_start = l;
5694 char *mnem_p;
5695 bool pass1 = !current_templates;
5696 int supported;
5697 const insn_template *t;
5698 char *dot_p = NULL;
5699
5700 while (1)
5701 {
5702 mnem_p = mnemonic;
5703 /* Pseudo-prefixes start with an opening figure brace. */
5704 if ((*mnem_p = *l) == '{')
5705 {
5706 ++mnem_p;
5707 ++l;
5708 }
5709 while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
5710 {
5711 if (*mnem_p == '.')
5712 dot_p = mnem_p;
5713 mnem_p++;
5714 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
5715 {
5716 too_long:
5717 as_bad (_("no such instruction: `%s'"), token_start);
5718 return NULL;
5719 }
5720 l++;
5721 }
5722 /* Pseudo-prefixes end with a closing figure brace. */
5723 if (*mnemonic == '{' && *l == '}')
5724 {
5725 *mnem_p++ = *l++;
5726 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
5727 goto too_long;
5728 *mnem_p = '\0';
5729
5730 /* Point l at the closing brace if there's no other separator. */
5731 if (*l != END_OF_INSN && !is_space_char (*l)
5732 && *l != PREFIX_SEPARATOR)
5733 --l;
5734 }
5735 else if (!is_space_char (*l)
5736 && *l != END_OF_INSN
5737 && (intel_syntax
5738 || (*l != PREFIX_SEPARATOR && *l != ',')))
5739 {
5740 if (prefix_only)
5741 break;
5742 as_bad (_("invalid character %s in mnemonic"),
5743 output_invalid (*l));
5744 return NULL;
5745 }
5746 if (token_start == l)
5747 {
5748 if (!intel_syntax && *l == PREFIX_SEPARATOR)
5749 as_bad (_("expecting prefix; got nothing"));
5750 else
5751 as_bad (_("expecting mnemonic; got nothing"));
5752 return NULL;
5753 }
5754
5755 /* Look up instruction (or prefix) via hash table. */
5756 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
5757
5758 if (*l != END_OF_INSN
5759 && (!is_space_char (*l) || l[1] != END_OF_INSN)
5760 && current_templates
5761 && current_templates->start->opcode_modifier.isprefix)
5762 {
5763 if (!cpu_flags_check_cpu64 (current_templates->start))
5764 {
5765 as_bad ((flag_code != CODE_64BIT
5766 ? _("`%s' is only supported in 64-bit mode")
5767 : _("`%s' is not supported in 64-bit mode")),
5768 insn_name (current_templates->start));
5769 return NULL;
5770 }
5771 /* If we are in 16-bit mode, do not allow addr16 or data16.
5772 Similarly, in 32-bit mode, do not allow addr32 or data32. */
5773 if ((current_templates->start->opcode_modifier.size == SIZE16
5774 || current_templates->start->opcode_modifier.size == SIZE32)
5775 && flag_code != CODE_64BIT
5776 && ((current_templates->start->opcode_modifier.size == SIZE32)
5777 ^ (flag_code == CODE_16BIT)))
5778 {
5779 as_bad (_("redundant %s prefix"),
5780 insn_name (current_templates->start));
5781 return NULL;
5782 }
5783
5784 if (current_templates->start->base_opcode == PSEUDO_PREFIX)
5785 {
5786 /* Handle pseudo prefixes. */
5787 switch (current_templates->start->extension_opcode)
5788 {
5789 case Prefix_Disp8:
5790 /* {disp8} */
5791 i.disp_encoding = disp_encoding_8bit;
5792 break;
5793 case Prefix_Disp16:
5794 /* {disp16} */
5795 i.disp_encoding = disp_encoding_16bit;
5796 break;
5797 case Prefix_Disp32:
5798 /* {disp32} */
5799 i.disp_encoding = disp_encoding_32bit;
5800 break;
5801 case Prefix_Load:
5802 /* {load} */
5803 i.dir_encoding = dir_encoding_load;
5804 break;
5805 case Prefix_Store:
5806 /* {store} */
5807 i.dir_encoding = dir_encoding_store;
5808 break;
5809 case Prefix_VEX:
5810 /* {vex} */
5811 i.vec_encoding = vex_encoding_vex;
5812 break;
5813 case Prefix_VEX3:
5814 /* {vex3} */
5815 i.vec_encoding = vex_encoding_vex3;
5816 break;
5817 case Prefix_EVEX:
5818 /* {evex} */
5819 i.vec_encoding = vex_encoding_evex;
5820 break;
5821 case Prefix_REX:
5822 /* {rex} */
5823 i.rex_encoding = true;
5824 break;
5825 case Prefix_NoOptimize:
5826 /* {nooptimize} */
5827 i.no_optimize = true;
5828 break;
5829 default:
5830 abort ();
5831 }
5832 }
5833 else
5834 {
5835 /* Add prefix, checking for repeated prefixes. */
5836 switch (add_prefix (current_templates->start->base_opcode))
5837 {
5838 case PREFIX_EXIST:
5839 return NULL;
5840 case PREFIX_DS:
5841 if (is_cpu (current_templates->start, CpuIBT))
5842 i.notrack_prefix = insn_name (current_templates->start);
5843 break;
5844 case PREFIX_REP:
5845 if (is_cpu (current_templates->start, CpuHLE))
5846 i.hle_prefix = insn_name (current_templates->start);
5847 else if (is_cpu (current_templates->start, CpuMPX))
5848 i.bnd_prefix = insn_name (current_templates->start);
5849 else
5850 i.rep_prefix = insn_name (current_templates->start);
5851 break;
5852 default:
5853 break;
5854 }
5855 }
5856 /* Skip past PREFIX_SEPARATOR and reset token_start. */
5857 token_start = ++l;
5858 }
5859 else
5860 break;
5861 }
5862
5863 if (prefix_only)
5864 return token_start;
5865
5866 if (!current_templates)
5867 {
5868 /* Deprecated functionality (new code should use pseudo-prefixes instead):
5869 Check if we should swap operand or force 32bit displacement in
5870 encoding. */
5871 if (mnem_p - 2 == dot_p && dot_p[1] == 's')
5872 i.dir_encoding = dir_encoding_swap;
5873 else if (mnem_p - 3 == dot_p
5874 && dot_p[1] == 'd'
5875 && dot_p[2] == '8')
5876 i.disp_encoding = disp_encoding_8bit;
5877 else if (mnem_p - 4 == dot_p
5878 && dot_p[1] == 'd'
5879 && dot_p[2] == '3'
5880 && dot_p[3] == '2')
5881 i.disp_encoding = disp_encoding_32bit;
5882 else
5883 goto check_suffix;
5884 mnem_p = dot_p;
5885 *dot_p = '\0';
5886 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
5887 }
5888
5889 if (!current_templates || !pass1)
5890 {
5891 current_templates = NULL;
5892
5893 check_suffix:
5894 if (mnem_p > mnemonic)
5895 {
5896 /* See if we can get a match by trimming off a suffix. */
5897 switch (mnem_p[-1])
5898 {
5899 case WORD_MNEM_SUFFIX:
5900 if (intel_syntax && (intel_float_operand (mnemonic) & 2))
5901 i.suffix = SHORT_MNEM_SUFFIX;
5902 else
5903 /* Fall through. */
5904 case BYTE_MNEM_SUFFIX:
5905 case QWORD_MNEM_SUFFIX:
5906 i.suffix = mnem_p[-1];
5907 mnem_p[-1] = '\0';
5908 current_templates
5909 = (const templates *) str_hash_find (op_hash, mnemonic);
5910 break;
5911 case SHORT_MNEM_SUFFIX:
5912 case LONG_MNEM_SUFFIX:
5913 if (!intel_syntax)
5914 {
5915 i.suffix = mnem_p[-1];
5916 mnem_p[-1] = '\0';
5917 current_templates
5918 = (const templates *) str_hash_find (op_hash, mnemonic);
5919 }
5920 break;
5921
5922 /* Intel Syntax. */
5923 case 'd':
5924 if (intel_syntax)
5925 {
5926 if (intel_float_operand (mnemonic) == 1)
5927 i.suffix = SHORT_MNEM_SUFFIX;
5928 else
5929 i.suffix = LONG_MNEM_SUFFIX;
5930 mnem_p[-1] = '\0';
5931 current_templates
5932 = (const templates *) str_hash_find (op_hash, mnemonic);
5933 }
5934 /* For compatibility reasons accept MOVSD and CMPSD without
5935 operands even in AT&T mode. */
5936 else if (*l == END_OF_INSN
5937 || (is_space_char (*l) && l[1] == END_OF_INSN))
5938 {
5939 mnem_p[-1] = '\0';
5940 current_templates
5941 = (const templates *) str_hash_find (op_hash, mnemonic);
5942 if (current_templates != NULL
5943 /* MOVS or CMPS */
5944 && (current_templates->start->base_opcode | 2) == 0xa6
5945 && current_templates->start->opcode_space
5946 == SPACE_BASE
5947 && mnem_p[-2] == 's')
5948 {
5949 as_warn (_("found `%sd'; assuming `%sl' was meant"),
5950 mnemonic, mnemonic);
5951 i.suffix = LONG_MNEM_SUFFIX;
5952 }
5953 else
5954 {
5955 current_templates = NULL;
5956 mnem_p[-1] = 'd';
5957 }
5958 }
5959 break;
5960 }
5961 }
5962
5963 if (!current_templates)
5964 {
5965 if (pass1)
5966 as_bad (_("no such instruction: `%s'"), token_start);
5967 return NULL;
5968 }
5969 }
5970
5971 if (current_templates->start->opcode_modifier.jump == JUMP
5972 || current_templates->start->opcode_modifier.jump == JUMP_BYTE)
5973 {
5974 /* Check for a branch hint. We allow ",pt" and ",pn" for
5975 predict taken and predict not taken respectively.
5976 I'm not sure that branch hints actually do anything on loop
5977 and jcxz insns (JumpByte) for current Pentium4 chips. They
5978 may work in the future and it doesn't hurt to accept them
5979 now. */
5980 if (l[0] == ',' && l[1] == 'p')
5981 {
5982 if (l[2] == 't')
5983 {
5984 if (!add_prefix (DS_PREFIX_OPCODE))
5985 return NULL;
5986 l += 3;
5987 }
5988 else if (l[2] == 'n')
5989 {
5990 if (!add_prefix (CS_PREFIX_OPCODE))
5991 return NULL;
5992 l += 3;
5993 }
5994 }
5995 }
5996 /* Any other comma loses. */
5997 if (*l == ',')
5998 {
5999 as_bad (_("invalid character %s in mnemonic"),
6000 output_invalid (*l));
6001 return NULL;
6002 }
6003
6004 /* Check if instruction is supported on specified architecture. */
6005 supported = 0;
6006 for (t = current_templates->start; t < current_templates->end; ++t)
6007 {
6008 supported |= cpu_flags_match (t);
6009
6010 if (i.suffix == QWORD_MNEM_SUFFIX && !q_suffix_allowed (t))
6011 supported &= ~CPU_FLAGS_64BIT_MATCH;
6012
6013 if (supported == CPU_FLAGS_PERFECT_MATCH)
6014 return l;
6015 }
6016
6017 if (pass1)
6018 {
6019 if (supported & CPU_FLAGS_64BIT_MATCH)
6020 i.error = unsupported_on_arch;
6021 else
6022 i.error = unsupported_64bit;
6023 }
6024
6025 return NULL;
6026 }
6027
6028 static char *
6029 parse_operands (char *l, const char *mnemonic)
6030 {
6031 char *token_start;
6032
6033 /* 1 if operand is pending after ','. */
6034 unsigned int expecting_operand = 0;
6035
6036 while (*l != END_OF_INSN)
6037 {
6038 /* Non-zero if operand parens not balanced. */
6039 unsigned int paren_not_balanced = 0;
6040 /* True if inside double quotes. */
6041 bool in_quotes = false;
6042
6043 /* Skip optional white space before operand. */
6044 if (is_space_char (*l))
6045 ++l;
6046 if (!is_operand_char (*l) && *l != END_OF_INSN && *l != '"')
6047 {
6048 as_bad (_("invalid character %s before operand %d"),
6049 output_invalid (*l),
6050 i.operands + 1);
6051 return NULL;
6052 }
6053 token_start = l; /* After white space. */
6054 while (in_quotes || paren_not_balanced || *l != ',')
6055 {
6056 if (*l == END_OF_INSN)
6057 {
6058 if (in_quotes)
6059 {
6060 as_bad (_("unbalanced double quotes in operand %d."),
6061 i.operands + 1);
6062 return NULL;
6063 }
6064 if (paren_not_balanced)
6065 {
6066 know (!intel_syntax);
6067 as_bad (_("unbalanced parenthesis in operand %d."),
6068 i.operands + 1);
6069 return NULL;
6070 }
6071 else
6072 break; /* we are done */
6073 }
6074 else if (*l == '\\' && l[1] == '"')
6075 ++l;
6076 else if (*l == '"')
6077 in_quotes = !in_quotes;
6078 else if (!in_quotes && !is_operand_char (*l) && !is_space_char (*l))
6079 {
6080 as_bad (_("invalid character %s in operand %d"),
6081 output_invalid (*l),
6082 i.operands + 1);
6083 return NULL;
6084 }
6085 if (!intel_syntax && !in_quotes)
6086 {
6087 if (*l == '(')
6088 ++paren_not_balanced;
6089 if (*l == ')')
6090 --paren_not_balanced;
6091 }
6092 l++;
6093 }
6094 if (l != token_start)
6095 { /* Yes, we've read in another operand. */
6096 unsigned int operand_ok;
6097 this_operand = i.operands++;
6098 if (i.operands > MAX_OPERANDS)
6099 {
6100 as_bad (_("spurious operands; (%d operands/instruction max)"),
6101 MAX_OPERANDS);
6102 return NULL;
6103 }
6104 i.types[this_operand].bitfield.unspecified = 1;
6105 /* Now parse operand adding info to 'i' as we go along. */
6106 END_STRING_AND_SAVE (l);
6107
6108 if (i.mem_operands > 1)
6109 {
6110 as_bad (_("too many memory references for `%s'"),
6111 mnemonic);
6112 return 0;
6113 }
6114
6115 if (intel_syntax)
6116 operand_ok =
6117 i386_intel_operand (token_start,
6118 intel_float_operand (mnemonic));
6119 else
6120 operand_ok = i386_att_operand (token_start);
6121
6122 RESTORE_END_STRING (l);
6123 if (!operand_ok)
6124 return NULL;
6125 }
6126 else
6127 {
6128 if (expecting_operand)
6129 {
6130 expecting_operand_after_comma:
6131 as_bad (_("expecting operand after ','; got nothing"));
6132 return NULL;
6133 }
6134 if (*l == ',')
6135 {
6136 as_bad (_("expecting operand before ','; got nothing"));
6137 return NULL;
6138 }
6139 }
6140
6141 /* Now *l must be either ',' or END_OF_INSN. */
6142 if (*l == ',')
6143 {
6144 if (*++l == END_OF_INSN)
6145 {
6146 /* Just skip it, if it's \n complain. */
6147 goto expecting_operand_after_comma;
6148 }
6149 expecting_operand = 1;
6150 }
6151 }
6152 return l;
6153 }
6154
6155 static void
6156 swap_2_operands (unsigned int xchg1, unsigned int xchg2)
6157 {
6158 union i386_op temp_op;
6159 i386_operand_type temp_type;
6160 unsigned int temp_flags;
6161 enum bfd_reloc_code_real temp_reloc;
6162
6163 temp_type = i.types[xchg2];
6164 i.types[xchg2] = i.types[xchg1];
6165 i.types[xchg1] = temp_type;
6166
6167 temp_flags = i.flags[xchg2];
6168 i.flags[xchg2] = i.flags[xchg1];
6169 i.flags[xchg1] = temp_flags;
6170
6171 temp_op = i.op[xchg2];
6172 i.op[xchg2] = i.op[xchg1];
6173 i.op[xchg1] = temp_op;
6174
6175 temp_reloc = i.reloc[xchg2];
6176 i.reloc[xchg2] = i.reloc[xchg1];
6177 i.reloc[xchg1] = temp_reloc;
6178
6179 temp_flags = i.imm_bits[xchg2];
6180 i.imm_bits[xchg2] = i.imm_bits[xchg1];
6181 i.imm_bits[xchg1] = temp_flags;
6182
6183 if (i.mask.reg)
6184 {
6185 if (i.mask.operand == xchg1)
6186 i.mask.operand = xchg2;
6187 else if (i.mask.operand == xchg2)
6188 i.mask.operand = xchg1;
6189 }
6190 if (i.broadcast.type || i.broadcast.bytes)
6191 {
6192 if (i.broadcast.operand == xchg1)
6193 i.broadcast.operand = xchg2;
6194 else if (i.broadcast.operand == xchg2)
6195 i.broadcast.operand = xchg1;
6196 }
6197 }
6198
6199 static void
6200 swap_operands (void)
6201 {
6202 switch (i.operands)
6203 {
6204 case 5:
6205 case 4:
6206 swap_2_operands (1, i.operands - 2);
6207 /* Fall through. */
6208 case 3:
6209 case 2:
6210 swap_2_operands (0, i.operands - 1);
6211 break;
6212 default:
6213 abort ();
6214 }
6215
6216 if (i.mem_operands == 2)
6217 {
6218 const reg_entry *temp_seg;
6219 temp_seg = i.seg[0];
6220 i.seg[0] = i.seg[1];
6221 i.seg[1] = temp_seg;
6222 }
6223 }
6224
6225 /* Try to ensure constant immediates are represented in the smallest
6226 opcode possible. */
6227 static void
6228 optimize_imm (void)
6229 {
6230 char guess_suffix = 0;
6231 int op;
6232
6233 if (i.suffix)
6234 guess_suffix = i.suffix;
6235 else if (i.reg_operands)
6236 {
6237 /* Figure out a suffix from the last register operand specified.
6238 We can't do this properly yet, i.e. excluding special register
6239 instances, but the following works for instructions with
6240 immediates. In any case, we can't set i.suffix yet. */
6241 for (op = i.operands; --op >= 0;)
6242 if (i.types[op].bitfield.class != Reg)
6243 continue;
6244 else if (i.types[op].bitfield.byte)
6245 {
6246 guess_suffix = BYTE_MNEM_SUFFIX;
6247 break;
6248 }
6249 else if (i.types[op].bitfield.word)
6250 {
6251 guess_suffix = WORD_MNEM_SUFFIX;
6252 break;
6253 }
6254 else if (i.types[op].bitfield.dword)
6255 {
6256 guess_suffix = LONG_MNEM_SUFFIX;
6257 break;
6258 }
6259 else if (i.types[op].bitfield.qword)
6260 {
6261 guess_suffix = QWORD_MNEM_SUFFIX;
6262 break;
6263 }
6264 }
6265 else if ((flag_code == CODE_16BIT)
6266 ^ (i.prefix[DATA_PREFIX] != 0 && !(i.prefix[REX_PREFIX] & REX_W)))
6267 guess_suffix = WORD_MNEM_SUFFIX;
6268 else if (flag_code != CODE_64BIT
6269 || (!(i.prefix[REX_PREFIX] & REX_W)
6270 /* A more generic (but also more involved) way of dealing
6271 with the special case(s) would be to go look for
6272 DefaultSize attributes on any of the templates. */
6273 && current_templates->start->mnem_off != MN_push))
6274 guess_suffix = LONG_MNEM_SUFFIX;
6275
6276 for (op = i.operands; --op >= 0;)
6277 if (operand_type_check (i.types[op], imm))
6278 {
6279 switch (i.op[op].imms->X_op)
6280 {
6281 case O_constant:
6282 /* If a suffix is given, this operand may be shortened. */
6283 switch (guess_suffix)
6284 {
6285 case LONG_MNEM_SUFFIX:
6286 i.types[op].bitfield.imm32 = 1;
6287 i.types[op].bitfield.imm64 = 1;
6288 break;
6289 case WORD_MNEM_SUFFIX:
6290 i.types[op].bitfield.imm16 = 1;
6291 i.types[op].bitfield.imm32 = 1;
6292 i.types[op].bitfield.imm32s = 1;
6293 i.types[op].bitfield.imm64 = 1;
6294 break;
6295 case BYTE_MNEM_SUFFIX:
6296 i.types[op].bitfield.imm8 = 1;
6297 i.types[op].bitfield.imm8s = 1;
6298 i.types[op].bitfield.imm16 = 1;
6299 i.types[op].bitfield.imm32 = 1;
6300 i.types[op].bitfield.imm32s = 1;
6301 i.types[op].bitfield.imm64 = 1;
6302 break;
6303 }
6304
6305 /* If this operand is at most 16 bits, convert it
6306 to a signed 16 bit number before trying to see
6307 whether it will fit in an even smaller size.
6308 This allows a 16-bit operand such as $0xffe0 to
6309 be recognised as within Imm8S range. */
6310 if ((i.types[op].bitfield.imm16)
6311 && fits_in_unsigned_word (i.op[op].imms->X_add_number))
6312 {
6313 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
6314 ^ 0x8000) - 0x8000);
6315 }
6316 #ifdef BFD64
6317 /* Store 32-bit immediate in 64-bit for 64-bit BFD. */
6318 if ((i.types[op].bitfield.imm32)
6319 && fits_in_unsigned_long (i.op[op].imms->X_add_number))
6320 {
6321 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
6322 ^ ((offsetT) 1 << 31))
6323 - ((offsetT) 1 << 31));
6324 }
6325 #endif
6326 i.types[op]
6327 = operand_type_or (i.types[op],
6328 smallest_imm_type (i.op[op].imms->X_add_number));
6329
6330 /* We must avoid matching of Imm32 templates when 64bit
6331 only immediate is available. */
6332 if (guess_suffix == QWORD_MNEM_SUFFIX)
6333 i.types[op].bitfield.imm32 = 0;
6334 break;
6335
6336 case O_absent:
6337 case O_register:
6338 abort ();
6339
6340 /* Symbols and expressions. */
6341 default:
6342 /* Convert symbolic operand to proper sizes for matching, but don't
6343 prevent matching a set of insns that only supports sizes other
6344 than those matching the insn suffix. */
6345 {
6346 i386_operand_type mask, allowed;
6347 const insn_template *t = current_templates->start;
6348
6349 operand_type_set (&mask, 0);
6350 switch (guess_suffix)
6351 {
6352 case QWORD_MNEM_SUFFIX:
6353 mask.bitfield.imm64 = 1;
6354 mask.bitfield.imm32s = 1;
6355 break;
6356 case LONG_MNEM_SUFFIX:
6357 mask.bitfield.imm32 = 1;
6358 break;
6359 case WORD_MNEM_SUFFIX:
6360 mask.bitfield.imm16 = 1;
6361 break;
6362 case BYTE_MNEM_SUFFIX:
6363 mask.bitfield.imm8 = 1;
6364 break;
6365 default:
6366 break;
6367 }
6368
6369 allowed = operand_type_and (t->operand_types[op], mask);
6370 while (++t < current_templates->end)
6371 {
6372 allowed = operand_type_or (allowed, t->operand_types[op]);
6373 allowed = operand_type_and (allowed, mask);
6374 }
6375
6376 if (!operand_type_all_zero (&allowed))
6377 i.types[op] = operand_type_and (i.types[op], mask);
6378 }
6379 break;
6380 }
6381 }
6382 }
6383
6384 /* Try to use the smallest displacement type too. */
6385 static bool
6386 optimize_disp (const insn_template *t)
6387 {
6388 unsigned int op;
6389
6390 if (!want_disp32 (t)
6391 && (!t->opcode_modifier.jump
6392 || i.jumpabsolute || i.types[0].bitfield.baseindex))
6393 {
6394 for (op = 0; op < i.operands; ++op)
6395 {
6396 const expressionS *exp = i.op[op].disps;
6397
6398 if (!operand_type_check (i.types[op], disp))
6399 continue;
6400
6401 if (exp->X_op != O_constant)
6402 continue;
6403
6404 /* Since displacement is signed extended to 64bit, don't allow
6405 disp32 if it is out of range. */
6406 if (fits_in_signed_long (exp->X_add_number))
6407 continue;
6408
6409 i.types[op].bitfield.disp32 = 0;
6410 if (i.types[op].bitfield.baseindex)
6411 {
6412 as_bad (_("0x%" PRIx64 " out of range of signed 32bit displacement"),
6413 (uint64_t) exp->X_add_number);
6414 return false;
6415 }
6416 }
6417 }
6418
6419 /* Don't optimize displacement for movabs since it only takes 64bit
6420 displacement. */
6421 if (i.disp_encoding > disp_encoding_8bit
6422 || (flag_code == CODE_64BIT && t->mnem_off == MN_movabs))
6423 return true;
6424
6425 for (op = i.operands; op-- > 0;)
6426 if (operand_type_check (i.types[op], disp))
6427 {
6428 if (i.op[op].disps->X_op == O_constant)
6429 {
6430 offsetT op_disp = i.op[op].disps->X_add_number;
6431
6432 if (!op_disp && i.types[op].bitfield.baseindex)
6433 {
6434 i.types[op] = operand_type_and_not (i.types[op], anydisp);
6435 i.op[op].disps = NULL;
6436 i.disp_operands--;
6437 continue;
6438 }
6439
6440 if (i.types[op].bitfield.disp16
6441 && fits_in_unsigned_word (op_disp))
6442 {
6443 /* If this operand is at most 16 bits, convert
6444 to a signed 16 bit number and don't use 64bit
6445 displacement. */
6446 op_disp = ((op_disp ^ 0x8000) - 0x8000);
6447 i.types[op].bitfield.disp64 = 0;
6448 }
6449
6450 #ifdef BFD64
6451 /* Optimize 64-bit displacement to 32-bit for 64-bit BFD. */
6452 if ((flag_code != CODE_64BIT
6453 ? i.types[op].bitfield.disp32
6454 : want_disp32 (t)
6455 && (!t->opcode_modifier.jump
6456 || i.jumpabsolute || i.types[op].bitfield.baseindex))
6457 && fits_in_unsigned_long (op_disp))
6458 {
6459 /* If this operand is at most 32 bits, convert
6460 to a signed 32 bit number and don't use 64bit
6461 displacement. */
6462 op_disp = (op_disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
6463 i.types[op].bitfield.disp64 = 0;
6464 i.types[op].bitfield.disp32 = 1;
6465 }
6466
6467 if (flag_code == CODE_64BIT && fits_in_signed_long (op_disp))
6468 {
6469 i.types[op].bitfield.disp64 = 0;
6470 i.types[op].bitfield.disp32 = 1;
6471 }
6472 #endif
6473 if ((i.types[op].bitfield.disp32
6474 || i.types[op].bitfield.disp16)
6475 && fits_in_disp8 (op_disp))
6476 i.types[op].bitfield.disp8 = 1;
6477
6478 i.op[op].disps->X_add_number = op_disp;
6479 }
6480 else if (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
6481 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL)
6482 {
6483 fix_new_exp (frag_now, frag_more (0) - frag_now->fr_literal, 0,
6484 i.op[op].disps, 0, i.reloc[op]);
6485 i.types[op] = operand_type_and_not (i.types[op], anydisp);
6486 }
6487 else
6488 /* We only support 64bit displacement on constants. */
6489 i.types[op].bitfield.disp64 = 0;
6490 }
6491
6492 return true;
6493 }
6494
6495 /* Return 1 if there is a match in broadcast bytes between operand
6496 GIVEN and instruction template T. */
6497
6498 static INLINE int
6499 match_broadcast_size (const insn_template *t, unsigned int given)
6500 {
6501 return ((t->opcode_modifier.broadcast == BYTE_BROADCAST
6502 && i.types[given].bitfield.byte)
6503 || (t->opcode_modifier.broadcast == WORD_BROADCAST
6504 && i.types[given].bitfield.word)
6505 || (t->opcode_modifier.broadcast == DWORD_BROADCAST
6506 && i.types[given].bitfield.dword)
6507 || (t->opcode_modifier.broadcast == QWORD_BROADCAST
6508 && i.types[given].bitfield.qword));
6509 }
6510
6511 /* Check if operands are valid for the instruction. */
6512
6513 static int
6514 check_VecOperands (const insn_template *t)
6515 {
6516 unsigned int op;
6517 i386_cpu_flags cpu;
6518
6519 /* Templates allowing for ZMMword as well as YMMword and/or XMMword for
6520 any one operand are implicity requiring AVX512VL support if the actual
6521 operand size is YMMword or XMMword. Since this function runs after
6522 template matching, there's no need to check for YMMword/XMMword in
6523 the template. */
6524 cpu = cpu_flags_and (cpu_flags_from_attr (t->cpu), avx512);
6525 if (!cpu_flags_all_zero (&cpu)
6526 && !is_cpu (t, CpuAVX512VL)
6527 && !cpu_arch_flags.bitfield.cpuavx512vl
6528 && (!t->opcode_modifier.vex || need_evex_encoding ()))
6529 {
6530 for (op = 0; op < t->operands; ++op)
6531 {
6532 if (t->operand_types[op].bitfield.zmmword
6533 && (i.types[op].bitfield.ymmword
6534 || i.types[op].bitfield.xmmword))
6535 {
6536 i.error = unsupported;
6537 return 1;
6538 }
6539 }
6540 }
6541
6542 /* Somewhat similarly, templates specifying both AVX and AVX2 are
6543 requiring AVX2 support if the actual operand size is YMMword. */
6544 if (is_cpu (t, CpuAVX) && is_cpu (t, CpuAVX2)
6545 && !cpu_arch_flags.bitfield.cpuavx2)
6546 {
6547 for (op = 0; op < t->operands; ++op)
6548 {
6549 if (t->operand_types[op].bitfield.xmmword
6550 && i.types[op].bitfield.ymmword)
6551 {
6552 i.error = unsupported;
6553 return 1;
6554 }
6555 }
6556 }
6557
6558 /* Without VSIB byte, we can't have a vector register for index. */
6559 if (!t->opcode_modifier.sib
6560 && i.index_reg
6561 && (i.index_reg->reg_type.bitfield.xmmword
6562 || i.index_reg->reg_type.bitfield.ymmword
6563 || i.index_reg->reg_type.bitfield.zmmword))
6564 {
6565 i.error = unsupported_vector_index_register;
6566 return 1;
6567 }
6568
6569 /* Check if default mask is allowed. */
6570 if (t->opcode_modifier.operandconstraint == NO_DEFAULT_MASK
6571 && (!i.mask.reg || i.mask.reg->reg_num == 0))
6572 {
6573 i.error = no_default_mask;
6574 return 1;
6575 }
6576
6577 /* For VSIB byte, we need a vector register for index, and all vector
6578 registers must be distinct. */
6579 if (t->opcode_modifier.sib && t->opcode_modifier.sib != SIBMEM)
6580 {
6581 if (!i.index_reg
6582 || !((t->opcode_modifier.sib == VECSIB128
6583 && i.index_reg->reg_type.bitfield.xmmword)
6584 || (t->opcode_modifier.sib == VECSIB256
6585 && i.index_reg->reg_type.bitfield.ymmword)
6586 || (t->opcode_modifier.sib == VECSIB512
6587 && i.index_reg->reg_type.bitfield.zmmword)))
6588 {
6589 i.error = invalid_vsib_address;
6590 return 1;
6591 }
6592
6593 gas_assert (i.reg_operands == 2 || i.mask.reg);
6594 if (i.reg_operands == 2 && !i.mask.reg)
6595 {
6596 gas_assert (i.types[0].bitfield.class == RegSIMD);
6597 gas_assert (i.types[0].bitfield.xmmword
6598 || i.types[0].bitfield.ymmword);
6599 gas_assert (i.types[2].bitfield.class == RegSIMD);
6600 gas_assert (i.types[2].bitfield.xmmword
6601 || i.types[2].bitfield.ymmword);
6602 if (operand_check == check_none)
6603 return 0;
6604 if (register_number (i.op[0].regs)
6605 != register_number (i.index_reg)
6606 && register_number (i.op[2].regs)
6607 != register_number (i.index_reg)
6608 && register_number (i.op[0].regs)
6609 != register_number (i.op[2].regs))
6610 return 0;
6611 if (operand_check == check_error)
6612 {
6613 i.error = invalid_vector_register_set;
6614 return 1;
6615 }
6616 as_warn (_("mask, index, and destination registers should be distinct"));
6617 }
6618 else if (i.reg_operands == 1 && i.mask.reg)
6619 {
6620 if (i.types[1].bitfield.class == RegSIMD
6621 && (i.types[1].bitfield.xmmword
6622 || i.types[1].bitfield.ymmword
6623 || i.types[1].bitfield.zmmword)
6624 && (register_number (i.op[1].regs)
6625 == register_number (i.index_reg)))
6626 {
6627 if (operand_check == check_error)
6628 {
6629 i.error = invalid_vector_register_set;
6630 return 1;
6631 }
6632 if (operand_check != check_none)
6633 as_warn (_("index and destination registers should be distinct"));
6634 }
6635 }
6636 }
6637
6638 /* For AMX instructions with 3 TMM register operands, all operands
6639 must be distinct. */
6640 if (i.reg_operands == 3
6641 && t->operand_types[0].bitfield.tmmword
6642 && (i.op[0].regs == i.op[1].regs
6643 || i.op[0].regs == i.op[2].regs
6644 || i.op[1].regs == i.op[2].regs))
6645 {
6646 i.error = invalid_tmm_register_set;
6647 return 1;
6648 }
6649
6650 /* For some special instructions require that destination must be distinct
6651 from source registers. */
6652 if (t->opcode_modifier.operandconstraint == DISTINCT_DEST)
6653 {
6654 unsigned int dest_reg = i.operands - 1;
6655
6656 know (i.operands >= 3);
6657
6658 /* #UD if dest_reg == src1_reg or dest_reg == src2_reg. */
6659 if (i.op[dest_reg - 1].regs == i.op[dest_reg].regs
6660 || (i.reg_operands > 2
6661 && i.op[dest_reg - 2].regs == i.op[dest_reg].regs))
6662 {
6663 i.error = invalid_dest_and_src_register_set;
6664 return 1;
6665 }
6666 }
6667
6668 /* Check if broadcast is supported by the instruction and is applied
6669 to the memory operand. */
6670 if (i.broadcast.type || i.broadcast.bytes)
6671 {
6672 i386_operand_type type, overlap;
6673
6674 /* Check if specified broadcast is supported in this instruction,
6675 and its broadcast bytes match the memory operand. */
6676 op = i.broadcast.operand;
6677 if (!t->opcode_modifier.broadcast
6678 || !(i.flags[op] & Operand_Mem)
6679 || (!i.types[op].bitfield.unspecified
6680 && !match_broadcast_size (t, op)))
6681 {
6682 bad_broadcast:
6683 i.error = unsupported_broadcast;
6684 return 1;
6685 }
6686
6687 operand_type_set (&type, 0);
6688 switch (get_broadcast_bytes (t, false))
6689 {
6690 case 2:
6691 type.bitfield.word = 1;
6692 break;
6693 case 4:
6694 type.bitfield.dword = 1;
6695 break;
6696 case 8:
6697 type.bitfield.qword = 1;
6698 break;
6699 case 16:
6700 type.bitfield.xmmword = 1;
6701 break;
6702 case 32:
6703 if (vector_size < VSZ256)
6704 goto bad_broadcast;
6705 type.bitfield.ymmword = 1;
6706 break;
6707 case 64:
6708 if (vector_size < VSZ512)
6709 goto bad_broadcast;
6710 type.bitfield.zmmword = 1;
6711 break;
6712 default:
6713 goto bad_broadcast;
6714 }
6715
6716 overlap = operand_type_and (type, t->operand_types[op]);
6717 if (t->operand_types[op].bitfield.class == RegSIMD
6718 && t->operand_types[op].bitfield.byte
6719 + t->operand_types[op].bitfield.word
6720 + t->operand_types[op].bitfield.dword
6721 + t->operand_types[op].bitfield.qword > 1)
6722 {
6723 overlap.bitfield.xmmword = 0;
6724 overlap.bitfield.ymmword = 0;
6725 overlap.bitfield.zmmword = 0;
6726 }
6727 if (operand_type_all_zero (&overlap))
6728 goto bad_broadcast;
6729
6730 if (t->opcode_modifier.checkoperandsize)
6731 {
6732 unsigned int j;
6733
6734 type.bitfield.baseindex = 1;
6735 for (j = 0; j < i.operands; ++j)
6736 {
6737 if (j != op
6738 && !operand_type_register_match(i.types[j],
6739 t->operand_types[j],
6740 type,
6741 t->operand_types[op]))
6742 goto bad_broadcast;
6743 }
6744 }
6745 }
6746 /* If broadcast is supported in this instruction, we need to check if
6747 operand of one-element size isn't specified without broadcast. */
6748 else if (t->opcode_modifier.broadcast && i.mem_operands)
6749 {
6750 /* Find memory operand. */
6751 for (op = 0; op < i.operands; op++)
6752 if (i.flags[op] & Operand_Mem)
6753 break;
6754 gas_assert (op < i.operands);
6755 /* Check size of the memory operand. */
6756 if (match_broadcast_size (t, op))
6757 {
6758 i.error = broadcast_needed;
6759 return 1;
6760 }
6761 }
6762 else
6763 op = MAX_OPERANDS - 1; /* Avoid uninitialized variable warning. */
6764
6765 /* Check if requested masking is supported. */
6766 if (i.mask.reg)
6767 {
6768 if (!t->opcode_modifier.masking)
6769 {
6770 i.error = unsupported_masking;
6771 return 1;
6772 }
6773
6774 /* Common rules for masking:
6775 - mask register destinations permit only zeroing-masking, without
6776 that actually being expressed by a {z} operand suffix or EVEX.z,
6777 - memory destinations allow only merging-masking,
6778 - scatter/gather insns (i.e. ones using vSIB) only allow merging-
6779 masking. */
6780 if (i.mask.zeroing
6781 && (t->operand_types[t->operands - 1].bitfield.class == RegMask
6782 || (i.flags[t->operands - 1] & Operand_Mem)
6783 || t->opcode_modifier.sib))
6784 {
6785 i.error = unsupported_masking;
6786 return 1;
6787 }
6788 }
6789
6790 /* Check if masking is applied to dest operand. */
6791 if (i.mask.reg && (i.mask.operand != i.operands - 1))
6792 {
6793 i.error = mask_not_on_destination;
6794 return 1;
6795 }
6796
6797 /* Check RC/SAE. */
6798 if (i.rounding.type != rc_none)
6799 {
6800 if (!t->opcode_modifier.sae
6801 || ((i.rounding.type != saeonly) != t->opcode_modifier.staticrounding)
6802 || i.mem_operands)
6803 {
6804 i.error = unsupported_rc_sae;
6805 return 1;
6806 }
6807
6808 /* Non-EVEX.LIG forms need to have a ZMM register as at least one
6809 operand. */
6810 if (t->opcode_modifier.evex != EVEXLIG)
6811 {
6812 for (op = 0; op < t->operands; ++op)
6813 if (i.types[op].bitfield.zmmword)
6814 break;
6815 if (op >= t->operands)
6816 {
6817 i.error = operand_size_mismatch;
6818 return 1;
6819 }
6820 }
6821 }
6822
6823 /* Check the special Imm4 cases; must be the first operand. */
6824 if (is_cpu (t, CpuXOP) && t->operands == 5)
6825 {
6826 if (i.op[0].imms->X_op != O_constant
6827 || !fits_in_imm4 (i.op[0].imms->X_add_number))
6828 {
6829 i.error = bad_imm4;
6830 return 1;
6831 }
6832
6833 /* Turn off Imm<N> so that update_imm won't complain. */
6834 operand_type_set (&i.types[0], 0);
6835 }
6836
6837 /* Check vector Disp8 operand. */
6838 if (t->opcode_modifier.disp8memshift
6839 && (!t->opcode_modifier.vex
6840 || need_evex_encoding ())
6841 && i.disp_encoding <= disp_encoding_8bit)
6842 {
6843 if (i.broadcast.type || i.broadcast.bytes)
6844 i.memshift = t->opcode_modifier.broadcast - 1;
6845 else if (t->opcode_modifier.disp8memshift != DISP8_SHIFT_VL)
6846 i.memshift = t->opcode_modifier.disp8memshift;
6847 else
6848 {
6849 const i386_operand_type *type = NULL, *fallback = NULL;
6850
6851 i.memshift = 0;
6852 for (op = 0; op < i.operands; op++)
6853 if (i.flags[op] & Operand_Mem)
6854 {
6855 if (t->opcode_modifier.evex == EVEXLIG)
6856 i.memshift = 2 + (i.suffix == QWORD_MNEM_SUFFIX);
6857 else if (t->operand_types[op].bitfield.xmmword
6858 + t->operand_types[op].bitfield.ymmword
6859 + t->operand_types[op].bitfield.zmmword <= 1)
6860 type = &t->operand_types[op];
6861 else if (!i.types[op].bitfield.unspecified)
6862 type = &i.types[op];
6863 else /* Ambiguities get resolved elsewhere. */
6864 fallback = &t->operand_types[op];
6865 }
6866 else if (i.types[op].bitfield.class == RegSIMD
6867 && t->opcode_modifier.evex != EVEXLIG)
6868 {
6869 if (i.types[op].bitfield.zmmword)
6870 i.memshift = 6;
6871 else if (i.types[op].bitfield.ymmword && i.memshift < 5)
6872 i.memshift = 5;
6873 else if (i.types[op].bitfield.xmmword && i.memshift < 4)
6874 i.memshift = 4;
6875 }
6876
6877 if (!type && !i.memshift)
6878 type = fallback;
6879 if (type)
6880 {
6881 if (type->bitfield.zmmword)
6882 i.memshift = 6;
6883 else if (type->bitfield.ymmword)
6884 i.memshift = 5;
6885 else if (type->bitfield.xmmword)
6886 i.memshift = 4;
6887 }
6888
6889 /* For the check in fits_in_disp8(). */
6890 if (i.memshift == 0)
6891 i.memshift = -1;
6892 }
6893
6894 for (op = 0; op < i.operands; op++)
6895 if (operand_type_check (i.types[op], disp)
6896 && i.op[op].disps->X_op == O_constant)
6897 {
6898 if (fits_in_disp8 (i.op[op].disps->X_add_number))
6899 {
6900 i.types[op].bitfield.disp8 = 1;
6901 return 0;
6902 }
6903 i.types[op].bitfield.disp8 = 0;
6904 }
6905 }
6906
6907 i.memshift = 0;
6908
6909 return 0;
6910 }
6911
6912 /* Check if encoding requirements are met by the instruction. */
6913
6914 static int
6915 VEX_check_encoding (const insn_template *t)
6916 {
6917 if (i.vec_encoding == vex_encoding_error)
6918 {
6919 i.error = unsupported;
6920 return 1;
6921 }
6922
6923 /* Vector size restrictions. */
6924 if ((vector_size < VSZ512
6925 && (t->opcode_modifier.evex == EVEX512
6926 || t->opcode_modifier.vsz >= VSZ512))
6927 || (vector_size < VSZ256
6928 && (t->opcode_modifier.evex == EVEX256
6929 || t->opcode_modifier.vex == VEX256
6930 || t->opcode_modifier.vsz >= VSZ256)))
6931 {
6932 i.error = unsupported;
6933 return 1;
6934 }
6935
6936 if (i.vec_encoding == vex_encoding_evex
6937 || i.vec_encoding == vex_encoding_evex512)
6938 {
6939 /* This instruction must be encoded with EVEX prefix. */
6940 if (!is_evex_encoding (t))
6941 {
6942 i.error = unsupported;
6943 return 1;
6944 }
6945 return 0;
6946 }
6947
6948 if (!t->opcode_modifier.vex)
6949 {
6950 /* This instruction template doesn't have VEX prefix. */
6951 if (i.vec_encoding != vex_encoding_default)
6952 {
6953 i.error = unsupported;
6954 return 1;
6955 }
6956 return 0;
6957 }
6958
6959 return 0;
6960 }
6961
6962 /* Helper function for the progress() macro in match_template(). */
6963 static INLINE enum i386_error progress (enum i386_error new,
6964 enum i386_error last,
6965 unsigned int line, unsigned int *line_p)
6966 {
6967 if (line <= *line_p)
6968 return last;
6969 *line_p = line;
6970 return new;
6971 }
6972
6973 static const insn_template *
6974 match_template (char mnem_suffix)
6975 {
6976 /* Points to template once we've found it. */
6977 const insn_template *t;
6978 i386_operand_type overlap0, overlap1, overlap2, overlap3;
6979 i386_operand_type overlap4;
6980 unsigned int found_reverse_match;
6981 i386_operand_type operand_types [MAX_OPERANDS];
6982 int addr_prefix_disp;
6983 unsigned int j, size_match, check_register, errline = __LINE__;
6984 enum i386_error specific_error = number_of_operands_mismatch;
6985 #define progress(err) progress (err, specific_error, __LINE__, &errline)
6986
6987 #if MAX_OPERANDS != 5
6988 # error "MAX_OPERANDS must be 5."
6989 #endif
6990
6991 found_reverse_match = 0;
6992 addr_prefix_disp = -1;
6993
6994 for (t = current_templates->start; t < current_templates->end; t++)
6995 {
6996 addr_prefix_disp = -1;
6997 found_reverse_match = 0;
6998
6999 /* Must have right number of operands. */
7000 if (i.operands != t->operands)
7001 continue;
7002
7003 /* Check processor support. */
7004 specific_error = progress (unsupported);
7005 if (cpu_flags_match (t) != CPU_FLAGS_PERFECT_MATCH)
7006 continue;
7007
7008 /* Check AT&T mnemonic. */
7009 specific_error = progress (unsupported_with_intel_mnemonic);
7010 if (intel_mnemonic && t->opcode_modifier.attmnemonic)
7011 continue;
7012
7013 /* Check AT&T/Intel syntax. */
7014 specific_error = progress (unsupported_syntax);
7015 if ((intel_syntax && t->opcode_modifier.attsyntax)
7016 || (!intel_syntax && t->opcode_modifier.intelsyntax))
7017 continue;
7018
7019 /* Check Intel64/AMD64 ISA. */
7020 switch (isa64)
7021 {
7022 default:
7023 /* Default: Don't accept Intel64. */
7024 if (t->opcode_modifier.isa64 == INTEL64)
7025 continue;
7026 break;
7027 case amd64:
7028 /* -mamd64: Don't accept Intel64 and Intel64 only. */
7029 if (t->opcode_modifier.isa64 >= INTEL64)
7030 continue;
7031 break;
7032 case intel64:
7033 /* -mintel64: Don't accept AMD64. */
7034 if (t->opcode_modifier.isa64 == AMD64 && flag_code == CODE_64BIT)
7035 continue;
7036 break;
7037 }
7038
7039 /* Check the suffix. */
7040 specific_error = progress (invalid_instruction_suffix);
7041 if ((t->opcode_modifier.no_bsuf && mnem_suffix == BYTE_MNEM_SUFFIX)
7042 || (t->opcode_modifier.no_wsuf && mnem_suffix == WORD_MNEM_SUFFIX)
7043 || (t->opcode_modifier.no_lsuf && mnem_suffix == LONG_MNEM_SUFFIX)
7044 || (t->opcode_modifier.no_ssuf && mnem_suffix == SHORT_MNEM_SUFFIX)
7045 || (t->opcode_modifier.no_qsuf && mnem_suffix == QWORD_MNEM_SUFFIX))
7046 continue;
7047
7048 specific_error = progress (operand_size_mismatch);
7049 size_match = operand_size_match (t);
7050 if (!size_match)
7051 continue;
7052
7053 /* This is intentionally not
7054
7055 if (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE))
7056
7057 as the case of a missing * on the operand is accepted (perhaps with
7058 a warning, issued further down). */
7059 specific_error = progress (operand_type_mismatch);
7060 if (i.jumpabsolute && t->opcode_modifier.jump != JUMP_ABSOLUTE)
7061 continue;
7062
7063 /* In Intel syntax, normally we can check for memory operand size when
7064 there is no mnemonic suffix. But jmp and call have 2 different
7065 encodings with Dword memory operand size. Skip the "near" one
7066 (permitting a register operand) when "far" was requested. */
7067 if (i.far_branch
7068 && t->opcode_modifier.jump == JUMP_ABSOLUTE
7069 && t->operand_types[0].bitfield.class == Reg)
7070 continue;
7071
7072 for (j = 0; j < MAX_OPERANDS; j++)
7073 operand_types[j] = t->operand_types[j];
7074
7075 /* In general, don't allow 32-bit operands on pre-386. */
7076 specific_error = progress (mnem_suffix ? invalid_instruction_suffix
7077 : operand_size_mismatch);
7078 j = i.imm_operands + (t->operands > i.imm_operands + 1);
7079 if (i.suffix == LONG_MNEM_SUFFIX
7080 && !cpu_arch_flags.bitfield.cpui386
7081 && (intel_syntax
7082 ? (t->opcode_modifier.mnemonicsize != IGNORESIZE
7083 && !intel_float_operand (insn_name (t)))
7084 : intel_float_operand (insn_name (t)) != 2)
7085 && (t->operands == i.imm_operands
7086 || (operand_types[i.imm_operands].bitfield.class != RegMMX
7087 && operand_types[i.imm_operands].bitfield.class != RegSIMD
7088 && operand_types[i.imm_operands].bitfield.class != RegMask)
7089 || (operand_types[j].bitfield.class != RegMMX
7090 && operand_types[j].bitfield.class != RegSIMD
7091 && operand_types[j].bitfield.class != RegMask))
7092 && !t->opcode_modifier.sib)
7093 continue;
7094
7095 /* Do not verify operands when there are none. */
7096 if (!t->operands)
7097 {
7098 if (VEX_check_encoding (t))
7099 {
7100 specific_error = progress (i.error);
7101 continue;
7102 }
7103
7104 /* We've found a match; break out of loop. */
7105 break;
7106 }
7107
7108 if (!t->opcode_modifier.jump
7109 || t->opcode_modifier.jump == JUMP_ABSOLUTE)
7110 {
7111 /* There should be only one Disp operand. */
7112 for (j = 0; j < MAX_OPERANDS; j++)
7113 if (operand_type_check (operand_types[j], disp))
7114 break;
7115 if (j < MAX_OPERANDS)
7116 {
7117 bool override = (i.prefix[ADDR_PREFIX] != 0);
7118
7119 addr_prefix_disp = j;
7120
7121 /* Address size prefix will turn Disp64 operand into Disp32 and
7122 Disp32/Disp16 one into Disp16/Disp32 respectively. */
7123 switch (flag_code)
7124 {
7125 case CODE_16BIT:
7126 override = !override;
7127 /* Fall through. */
7128 case CODE_32BIT:
7129 if (operand_types[j].bitfield.disp32
7130 && operand_types[j].bitfield.disp16)
7131 {
7132 operand_types[j].bitfield.disp16 = override;
7133 operand_types[j].bitfield.disp32 = !override;
7134 }
7135 gas_assert (!operand_types[j].bitfield.disp64);
7136 break;
7137
7138 case CODE_64BIT:
7139 if (operand_types[j].bitfield.disp64)
7140 {
7141 gas_assert (!operand_types[j].bitfield.disp32);
7142 operand_types[j].bitfield.disp32 = override;
7143 operand_types[j].bitfield.disp64 = !override;
7144 }
7145 operand_types[j].bitfield.disp16 = 0;
7146 break;
7147 }
7148 }
7149 }
7150
7151 /* We check register size if needed. */
7152 if (t->opcode_modifier.checkoperandsize)
7153 {
7154 check_register = (1 << t->operands) - 1;
7155 if (i.broadcast.type || i.broadcast.bytes)
7156 check_register &= ~(1 << i.broadcast.operand);
7157 }
7158 else
7159 check_register = 0;
7160
7161 overlap0 = operand_type_and (i.types[0], operand_types[0]);
7162 switch (t->operands)
7163 {
7164 case 1:
7165 if (!operand_type_match (overlap0, i.types[0]))
7166 continue;
7167
7168 /* Allow the ModR/M encoding to be requested by using the {load} or
7169 {store} pseudo prefix on an applicable insn. */
7170 if (!t->opcode_modifier.modrm
7171 && i.reg_operands == 1
7172 && ((i.dir_encoding == dir_encoding_load
7173 && t->mnem_off != MN_pop)
7174 || (i.dir_encoding == dir_encoding_store
7175 && t->mnem_off != MN_push))
7176 /* Avoid BSWAP. */
7177 && t->mnem_off != MN_bswap)
7178 continue;
7179 break;
7180
7181 case 2:
7182 /* xchg %eax, %eax is a special case. It is an alias for nop
7183 only in 32bit mode and we can use opcode 0x90. In 64bit
7184 mode, we can't use 0x90 for xchg %eax, %eax since it should
7185 zero-extend %eax to %rax. */
7186 if (t->base_opcode == 0x90
7187 && t->opcode_space == SPACE_BASE)
7188 {
7189 if (flag_code == CODE_64BIT
7190 && i.types[0].bitfield.instance == Accum
7191 && i.types[0].bitfield.dword
7192 && i.types[1].bitfield.instance == Accum)
7193 continue;
7194
7195 /* Allow the ModR/M encoding to be requested by using the
7196 {load} or {store} pseudo prefix. */
7197 if (i.dir_encoding == dir_encoding_load
7198 || i.dir_encoding == dir_encoding_store)
7199 continue;
7200 }
7201
7202 if (t->base_opcode == MOV_AX_DISP32
7203 && t->opcode_space == SPACE_BASE
7204 && t->mnem_off != MN_movabs)
7205 {
7206 /* Force 0x8b encoding for "mov foo@GOT, %eax". */
7207 if (i.reloc[0] == BFD_RELOC_386_GOT32)
7208 continue;
7209
7210 /* xrelease mov %eax, <disp> is another special case. It must not
7211 match the accumulator-only encoding of mov. */
7212 if (i.hle_prefix)
7213 continue;
7214
7215 /* Allow the ModR/M encoding to be requested by using a suitable
7216 {load} or {store} pseudo prefix. */
7217 if (i.dir_encoding == (i.types[0].bitfield.instance == Accum
7218 ? dir_encoding_store
7219 : dir_encoding_load)
7220 && !i.types[0].bitfield.disp64
7221 && !i.types[1].bitfield.disp64)
7222 continue;
7223 }
7224
7225 /* Allow the ModR/M encoding to be requested by using the {load} or
7226 {store} pseudo prefix on an applicable insn. */
7227 if (!t->opcode_modifier.modrm
7228 && i.reg_operands == 1
7229 && i.imm_operands == 1
7230 && (i.dir_encoding == dir_encoding_load
7231 || i.dir_encoding == dir_encoding_store)
7232 && t->opcode_space == SPACE_BASE)
7233 {
7234 if (t->base_opcode == 0xb0 /* mov $imm, %reg */
7235 && i.dir_encoding == dir_encoding_store)
7236 continue;
7237
7238 if ((t->base_opcode | 0x38) == 0x3c /* <alu> $imm, %acc */
7239 && (t->base_opcode != 0x3c /* cmp $imm, %acc */
7240 || i.dir_encoding == dir_encoding_load))
7241 continue;
7242
7243 if (t->base_opcode == 0xa8 /* test $imm, %acc */
7244 && i.dir_encoding == dir_encoding_load)
7245 continue;
7246 }
7247 /* Fall through. */
7248
7249 case 3:
7250 if (!(size_match & MATCH_STRAIGHT))
7251 goto check_reverse;
7252 /* Reverse direction of operands if swapping is possible in the first
7253 place (operands need to be symmetric) and
7254 - the load form is requested, and the template is a store form,
7255 - the store form is requested, and the template is a load form,
7256 - the non-default (swapped) form is requested. */
7257 overlap1 = operand_type_and (operand_types[0], operand_types[1]);
7258 if (t->opcode_modifier.d && i.reg_operands == i.operands
7259 && !operand_type_all_zero (&overlap1))
7260 switch (i.dir_encoding)
7261 {
7262 case dir_encoding_load:
7263 if (operand_type_check (operand_types[i.operands - 1], anymem)
7264 || t->opcode_modifier.regmem)
7265 goto check_reverse;
7266 break;
7267
7268 case dir_encoding_store:
7269 if (!operand_type_check (operand_types[i.operands - 1], anymem)
7270 && !t->opcode_modifier.regmem)
7271 goto check_reverse;
7272 break;
7273
7274 case dir_encoding_swap:
7275 goto check_reverse;
7276
7277 case dir_encoding_default:
7278 break;
7279 }
7280 /* If we want store form, we skip the current load. */
7281 if ((i.dir_encoding == dir_encoding_store
7282 || i.dir_encoding == dir_encoding_swap)
7283 && i.mem_operands == 0
7284 && t->opcode_modifier.load)
7285 continue;
7286 /* Fall through. */
7287 case 4:
7288 case 5:
7289 overlap1 = operand_type_and (i.types[1], operand_types[1]);
7290 if (!operand_type_match (overlap0, i.types[0])
7291 || !operand_type_match (overlap1, i.types[1])
7292 || ((check_register & 3) == 3
7293 && !operand_type_register_match (i.types[0],
7294 operand_types[0],
7295 i.types[1],
7296 operand_types[1])))
7297 {
7298 specific_error = progress (i.error);
7299
7300 /* Check if other direction is valid ... */
7301 if (!t->opcode_modifier.d)
7302 continue;
7303
7304 check_reverse:
7305 if (!(size_match & MATCH_REVERSE))
7306 continue;
7307 /* Try reversing direction of operands. */
7308 j = is_cpu (t, CpuFMA4)
7309 || is_cpu (t, CpuXOP) ? 1 : i.operands - 1;
7310 overlap0 = operand_type_and (i.types[0], operand_types[j]);
7311 overlap1 = operand_type_and (i.types[j], operand_types[0]);
7312 overlap2 = operand_type_and (i.types[1], operand_types[1]);
7313 gas_assert (t->operands != 3 || !check_register);
7314 if (!operand_type_match (overlap0, i.types[0])
7315 || !operand_type_match (overlap1, i.types[j])
7316 || (t->operands == 3
7317 && !operand_type_match (overlap2, i.types[1]))
7318 || (check_register
7319 && !operand_type_register_match (i.types[0],
7320 operand_types[j],
7321 i.types[j],
7322 operand_types[0])))
7323 {
7324 /* Does not match either direction. */
7325 specific_error = progress (i.error);
7326 continue;
7327 }
7328 /* found_reverse_match holds which variant of D
7329 we've found. */
7330 if (!t->opcode_modifier.d)
7331 found_reverse_match = 0;
7332 else if (operand_types[0].bitfield.tbyte)
7333 {
7334 if (t->opcode_modifier.operandconstraint != UGH)
7335 found_reverse_match = Opcode_FloatD;
7336 else
7337 found_reverse_match = ~0;
7338 /* FSUB{,R} and FDIV{,R} may need a 2nd bit flipped. */
7339 if ((t->extension_opcode & 4)
7340 && (intel_syntax || intel_mnemonic))
7341 found_reverse_match |= Opcode_FloatR;
7342 }
7343 else if (is_cpu (t, CpuFMA4) || is_cpu (t, CpuXOP))
7344 {
7345 found_reverse_match = Opcode_VexW;
7346 goto check_operands_345;
7347 }
7348 else if (t->opcode_space != SPACE_BASE
7349 && (t->opcode_space != SPACE_0F
7350 /* MOV to/from CR/DR/TR, as an exception, follow
7351 the base opcode space encoding model. */
7352 || (t->base_opcode | 7) != 0x27))
7353 found_reverse_match = (t->base_opcode & 0xee) != 0x6e
7354 ? Opcode_ExtD : Opcode_SIMD_IntD;
7355 else if (!t->opcode_modifier.commutative)
7356 found_reverse_match = Opcode_D;
7357 else
7358 found_reverse_match = ~0;
7359 }
7360 else
7361 {
7362 /* Found a forward 2 operand match here. */
7363 check_operands_345:
7364 switch (t->operands)
7365 {
7366 case 5:
7367 overlap4 = operand_type_and (i.types[4], operand_types[4]);
7368 if (!operand_type_match (overlap4, i.types[4])
7369 || !operand_type_register_match (i.types[3],
7370 operand_types[3],
7371 i.types[4],
7372 operand_types[4]))
7373 {
7374 specific_error = progress (i.error);
7375 continue;
7376 }
7377 /* Fall through. */
7378 case 4:
7379 overlap3 = operand_type_and (i.types[3], operand_types[3]);
7380 if (!operand_type_match (overlap3, i.types[3])
7381 || ((check_register & 0xa) == 0xa
7382 && !operand_type_register_match (i.types[1],
7383 operand_types[1],
7384 i.types[3],
7385 operand_types[3]))
7386 || ((check_register & 0xc) == 0xc
7387 && !operand_type_register_match (i.types[2],
7388 operand_types[2],
7389 i.types[3],
7390 operand_types[3])))
7391 {
7392 specific_error = progress (i.error);
7393 continue;
7394 }
7395 /* Fall through. */
7396 case 3:
7397 overlap2 = operand_type_and (i.types[2], operand_types[2]);
7398 if (!operand_type_match (overlap2, i.types[2])
7399 || ((check_register & 5) == 5
7400 && !operand_type_register_match (i.types[0],
7401 operand_types[0],
7402 i.types[2],
7403 operand_types[2]))
7404 || ((check_register & 6) == 6
7405 && !operand_type_register_match (i.types[1],
7406 operand_types[1],
7407 i.types[2],
7408 operand_types[2])))
7409 {
7410 specific_error = progress (i.error);
7411 continue;
7412 }
7413 break;
7414 }
7415 }
7416 /* Found either forward/reverse 2, 3 or 4 operand match here:
7417 slip through to break. */
7418 }
7419
7420 /* Check if VEX/EVEX encoding requirements can be satisfied. */
7421 if (VEX_check_encoding (t))
7422 {
7423 specific_error = progress (i.error);
7424 continue;
7425 }
7426
7427 /* Check if vector operands are valid. */
7428 if (check_VecOperands (t))
7429 {
7430 specific_error = progress (i.error);
7431 continue;
7432 }
7433
7434 /* Check whether to use the shorter VEX encoding for certain insns where
7435 the EVEX enconding comes first in the table. This requires the respective
7436 AVX-* feature to be explicitly enabled. */
7437 if (t == current_templates->start
7438 && t->opcode_modifier.disp8memshift
7439 && !t->opcode_modifier.vex
7440 && !need_evex_encoding ()
7441 && t + 1 < current_templates->end
7442 && t[1].opcode_modifier.vex)
7443 {
7444 i386_cpu_flags cpu;
7445 unsigned int memshift = i.memshift;
7446
7447 i.memshift = 0;
7448 cpu = cpu_flags_and (cpu_flags_from_attr (t[1].cpu), cpu_arch_isa_flags);
7449 if (!cpu_flags_all_zero (&cpu)
7450 && (!i.types[0].bitfield.disp8
7451 || !operand_type_check (i.types[0], disp)
7452 || i.op[0].disps->X_op != O_constant
7453 || fits_in_disp8 (i.op[0].disps->X_add_number)))
7454 {
7455 specific_error = progress (internal_error);
7456 continue;
7457 }
7458 i.memshift = memshift;
7459 }
7460
7461 /* We've found a match; break out of loop. */
7462 break;
7463 }
7464
7465 #undef progress
7466
7467 if (t == current_templates->end)
7468 {
7469 /* We found no match. */
7470 i.error = specific_error;
7471 return NULL;
7472 }
7473
7474 if (!quiet_warnings)
7475 {
7476 if (!intel_syntax
7477 && (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE)))
7478 as_warn (_("indirect %s without `*'"), insn_name (t));
7479
7480 if (t->opcode_modifier.isprefix
7481 && t->opcode_modifier.mnemonicsize == IGNORESIZE)
7482 {
7483 /* Warn them that a data or address size prefix doesn't
7484 affect assembly of the next line of code. */
7485 as_warn (_("stand-alone `%s' prefix"), insn_name (t));
7486 }
7487 }
7488
7489 /* Copy the template we found. */
7490 install_template (t);
7491
7492 if (addr_prefix_disp != -1)
7493 i.tm.operand_types[addr_prefix_disp]
7494 = operand_types[addr_prefix_disp];
7495
7496 switch (found_reverse_match)
7497 {
7498 case 0:
7499 break;
7500
7501 case Opcode_FloatR:
7502 case Opcode_FloatR | Opcode_FloatD:
7503 i.tm.extension_opcode ^= Opcode_FloatR >> 3;
7504 found_reverse_match &= Opcode_FloatD;
7505
7506 /* Fall through. */
7507 default:
7508 /* If we found a reverse match we must alter the opcode direction
7509 bit and clear/flip the regmem modifier one. found_reverse_match
7510 holds bits to change (different for int & float insns). */
7511
7512 i.tm.base_opcode ^= found_reverse_match;
7513
7514 /* Certain SIMD insns have their load forms specified in the opcode
7515 table, and hence we need to _set_ RegMem instead of clearing it.
7516 We need to avoid setting the bit though on insns like KMOVW. */
7517 i.tm.opcode_modifier.regmem
7518 = i.tm.opcode_modifier.modrm && i.tm.opcode_modifier.d
7519 && i.tm.operands > 2U - i.tm.opcode_modifier.sse2avx
7520 && !i.tm.opcode_modifier.regmem;
7521
7522 /* Fall through. */
7523 case ~0:
7524 i.tm.operand_types[0] = operand_types[i.operands - 1];
7525 i.tm.operand_types[i.operands - 1] = operand_types[0];
7526 break;
7527
7528 case Opcode_VexW:
7529 /* Only the first two register operands need reversing, alongside
7530 flipping VEX.W. */
7531 i.tm.opcode_modifier.vexw ^= VEXW0 ^ VEXW1;
7532
7533 j = i.tm.operand_types[0].bitfield.imm8;
7534 i.tm.operand_types[j] = operand_types[j + 1];
7535 i.tm.operand_types[j + 1] = operand_types[j];
7536 break;
7537 }
7538
7539 return t;
7540 }
7541
7542 static int
7543 check_string (void)
7544 {
7545 unsigned int es_op = i.tm.opcode_modifier.isstring - IS_STRING_ES_OP0;
7546 unsigned int op = i.tm.operand_types[0].bitfield.baseindex ? es_op : 0;
7547
7548 if (i.seg[op] != NULL && i.seg[op] != reg_es)
7549 {
7550 as_bad (_("`%s' operand %u must use `%ses' segment"),
7551 insn_name (&i.tm),
7552 intel_syntax ? i.tm.operands - es_op : es_op + 1,
7553 register_prefix);
7554 return 0;
7555 }
7556
7557 /* There's only ever one segment override allowed per instruction.
7558 This instruction possibly has a legal segment override on the
7559 second operand, so copy the segment to where non-string
7560 instructions store it, allowing common code. */
7561 i.seg[op] = i.seg[1];
7562
7563 return 1;
7564 }
7565
7566 static int
7567 process_suffix (void)
7568 {
7569 bool is_movx = false;
7570
7571 /* If matched instruction specifies an explicit instruction mnemonic
7572 suffix, use it. */
7573 if (i.tm.opcode_modifier.size == SIZE16)
7574 i.suffix = WORD_MNEM_SUFFIX;
7575 else if (i.tm.opcode_modifier.size == SIZE32)
7576 i.suffix = LONG_MNEM_SUFFIX;
7577 else if (i.tm.opcode_modifier.size == SIZE64)
7578 i.suffix = QWORD_MNEM_SUFFIX;
7579 else if (i.reg_operands
7580 && (i.operands > 1 || i.types[0].bitfield.class == Reg)
7581 && i.tm.opcode_modifier.operandconstraint != ADDR_PREFIX_OP_REG)
7582 {
7583 unsigned int numop = i.operands;
7584
7585 /* MOVSX/MOVZX */
7586 is_movx = (i.tm.opcode_space == SPACE_0F
7587 && (i.tm.base_opcode | 8) == 0xbe)
7588 || (i.tm.opcode_space == SPACE_BASE
7589 && i.tm.base_opcode == 0x63
7590 && is_cpu (&i.tm, Cpu64));
7591
7592 /* movsx/movzx want only their source operand considered here, for the
7593 ambiguity checking below. The suffix will be replaced afterwards
7594 to represent the destination (register). */
7595 if (is_movx && (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63))
7596 --i.operands;
7597
7598 /* crc32 needs REX.W set regardless of suffix / source operand size. */
7599 if (i.tm.mnem_off == MN_crc32 && i.tm.operand_types[1].bitfield.qword)
7600 i.rex |= REX_W;
7601
7602 /* If there's no instruction mnemonic suffix we try to invent one
7603 based on GPR operands. */
7604 if (!i.suffix)
7605 {
7606 /* We take i.suffix from the last register operand specified,
7607 Destination register type is more significant than source
7608 register type. crc32 in SSE4.2 prefers source register
7609 type. */
7610 unsigned int op = i.tm.mnem_off == MN_crc32 ? 1 : i.operands;
7611
7612 while (op--)
7613 if (i.tm.operand_types[op].bitfield.instance == InstanceNone
7614 || i.tm.operand_types[op].bitfield.instance == Accum)
7615 {
7616 if (i.types[op].bitfield.class != Reg)
7617 continue;
7618 if (i.types[op].bitfield.byte)
7619 i.suffix = BYTE_MNEM_SUFFIX;
7620 else if (i.types[op].bitfield.word)
7621 i.suffix = WORD_MNEM_SUFFIX;
7622 else if (i.types[op].bitfield.dword)
7623 i.suffix = LONG_MNEM_SUFFIX;
7624 else if (i.types[op].bitfield.qword)
7625 i.suffix = QWORD_MNEM_SUFFIX;
7626 else
7627 continue;
7628 break;
7629 }
7630
7631 /* As an exception, movsx/movzx silently default to a byte source
7632 in AT&T mode. */
7633 if (is_movx && i.tm.opcode_modifier.w && !i.suffix && !intel_syntax)
7634 i.suffix = BYTE_MNEM_SUFFIX;
7635 }
7636 else if (i.suffix == BYTE_MNEM_SUFFIX)
7637 {
7638 if (!check_byte_reg ())
7639 return 0;
7640 }
7641 else if (i.suffix == LONG_MNEM_SUFFIX)
7642 {
7643 if (!check_long_reg ())
7644 return 0;
7645 }
7646 else if (i.suffix == QWORD_MNEM_SUFFIX)
7647 {
7648 if (!check_qword_reg ())
7649 return 0;
7650 }
7651 else if (i.suffix == WORD_MNEM_SUFFIX)
7652 {
7653 if (!check_word_reg ())
7654 return 0;
7655 }
7656 else if (intel_syntax
7657 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
7658 /* Do nothing if the instruction is going to ignore the prefix. */
7659 ;
7660 else
7661 abort ();
7662
7663 /* Undo the movsx/movzx change done above. */
7664 i.operands = numop;
7665 }
7666 else if (i.tm.opcode_modifier.mnemonicsize == DEFAULTSIZE
7667 && !i.suffix)
7668 {
7669 i.suffix = stackop_size;
7670 if (stackop_size == LONG_MNEM_SUFFIX)
7671 {
7672 /* stackop_size is set to LONG_MNEM_SUFFIX for the
7673 .code16gcc directive to support 16-bit mode with
7674 32-bit address. For IRET without a suffix, generate
7675 16-bit IRET (opcode 0xcf) to return from an interrupt
7676 handler. */
7677 if (i.tm.base_opcode == 0xcf)
7678 {
7679 i.suffix = WORD_MNEM_SUFFIX;
7680 as_warn (_("generating 16-bit `iret' for .code16gcc directive"));
7681 }
7682 /* Warn about changed behavior for segment register push/pop. */
7683 else if ((i.tm.base_opcode | 1) == 0x07)
7684 as_warn (_("generating 32-bit `%s', unlike earlier gas versions"),
7685 insn_name (&i.tm));
7686 }
7687 }
7688 else if (!i.suffix
7689 && (i.tm.opcode_modifier.jump == JUMP_ABSOLUTE
7690 || i.tm.opcode_modifier.jump == JUMP_BYTE
7691 || i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT
7692 || (i.tm.opcode_space == SPACE_0F
7693 && i.tm.base_opcode == 0x01 /* [ls][gi]dt */
7694 && i.tm.extension_opcode <= 3)))
7695 {
7696 switch (flag_code)
7697 {
7698 case CODE_64BIT:
7699 if (!i.tm.opcode_modifier.no_qsuf)
7700 {
7701 if (i.tm.opcode_modifier.jump == JUMP_BYTE
7702 || i.tm.opcode_modifier.no_lsuf)
7703 i.suffix = QWORD_MNEM_SUFFIX;
7704 break;
7705 }
7706 /* Fall through. */
7707 case CODE_32BIT:
7708 if (!i.tm.opcode_modifier.no_lsuf)
7709 i.suffix = LONG_MNEM_SUFFIX;
7710 break;
7711 case CODE_16BIT:
7712 if (!i.tm.opcode_modifier.no_wsuf)
7713 i.suffix = WORD_MNEM_SUFFIX;
7714 break;
7715 }
7716 }
7717
7718 if (!i.suffix
7719 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
7720 /* Also cover lret/retf/iret in 64-bit mode. */
7721 || (flag_code == CODE_64BIT
7722 && !i.tm.opcode_modifier.no_lsuf
7723 && !i.tm.opcode_modifier.no_qsuf))
7724 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
7725 /* Explicit sizing prefixes are assumed to disambiguate insns. */
7726 && !i.prefix[DATA_PREFIX] && !(i.prefix[REX_PREFIX] & REX_W)
7727 /* Accept FLDENV et al without suffix. */
7728 && (i.tm.opcode_modifier.no_ssuf || i.tm.opcode_modifier.floatmf))
7729 {
7730 unsigned int suffixes, evex = 0;
7731
7732 suffixes = !i.tm.opcode_modifier.no_bsuf;
7733 if (!i.tm.opcode_modifier.no_wsuf)
7734 suffixes |= 1 << 1;
7735 if (!i.tm.opcode_modifier.no_lsuf)
7736 suffixes |= 1 << 2;
7737 if (!i.tm.opcode_modifier.no_ssuf)
7738 suffixes |= 1 << 4;
7739 if (flag_code == CODE_64BIT && !i.tm.opcode_modifier.no_qsuf)
7740 suffixes |= 1 << 5;
7741
7742 /* For [XYZ]MMWORD operands inspect operand sizes. While generally
7743 also suitable for AT&T syntax mode, it was requested that this be
7744 restricted to just Intel syntax. */
7745 if (intel_syntax && is_any_vex_encoding (&i.tm)
7746 && !i.broadcast.type && !i.broadcast.bytes)
7747 {
7748 unsigned int op;
7749
7750 for (op = 0; op < i.tm.operands; ++op)
7751 {
7752 if (vector_size < VSZ512)
7753 {
7754 i.tm.operand_types[op].bitfield.zmmword = 0;
7755 if (vector_size < VSZ256)
7756 {
7757 i.tm.operand_types[op].bitfield.ymmword = 0;
7758 if (i.tm.operand_types[op].bitfield.xmmword
7759 && (i.tm.opcode_modifier.evex == EVEXDYN
7760 || (!i.tm.opcode_modifier.evex
7761 && is_evex_encoding (&i.tm))))
7762 i.tm.opcode_modifier.evex = EVEX128;
7763 }
7764 else if (i.tm.operand_types[op].bitfield.ymmword
7765 && !i.tm.operand_types[op].bitfield.xmmword
7766 && (i.tm.opcode_modifier.evex == EVEXDYN
7767 || (!i.tm.opcode_modifier.evex
7768 && is_evex_encoding (&i.tm))))
7769 i.tm.opcode_modifier.evex = EVEX256;
7770 }
7771 else if (is_evex_encoding (&i.tm)
7772 && !cpu_arch_flags.bitfield.cpuavx512vl)
7773 {
7774 if (i.tm.operand_types[op].bitfield.ymmword)
7775 i.tm.operand_types[op].bitfield.xmmword = 0;
7776 if (i.tm.operand_types[op].bitfield.zmmword)
7777 i.tm.operand_types[op].bitfield.ymmword = 0;
7778 if (!i.tm.opcode_modifier.evex
7779 || i.tm.opcode_modifier.evex == EVEXDYN)
7780 i.tm.opcode_modifier.evex = EVEX512;
7781 }
7782
7783 if (i.tm.operand_types[op].bitfield.xmmword
7784 + i.tm.operand_types[op].bitfield.ymmword
7785 + i.tm.operand_types[op].bitfield.zmmword < 2)
7786 continue;
7787
7788 /* Any properly sized operand disambiguates the insn. */
7789 if (i.types[op].bitfield.xmmword
7790 || i.types[op].bitfield.ymmword
7791 || i.types[op].bitfield.zmmword)
7792 {
7793 suffixes &= ~(7 << 6);
7794 evex = 0;
7795 break;
7796 }
7797
7798 if ((i.flags[op] & Operand_Mem)
7799 && i.tm.operand_types[op].bitfield.unspecified)
7800 {
7801 if (i.tm.operand_types[op].bitfield.xmmword)
7802 suffixes |= 1 << 6;
7803 if (i.tm.operand_types[op].bitfield.ymmword)
7804 suffixes |= 1 << 7;
7805 if (i.tm.operand_types[op].bitfield.zmmword)
7806 suffixes |= 1 << 8;
7807 if (is_evex_encoding (&i.tm))
7808 evex = EVEX512;
7809 }
7810 }
7811 }
7812
7813 /* Are multiple suffixes / operand sizes allowed? */
7814 if (suffixes & (suffixes - 1))
7815 {
7816 if (intel_syntax
7817 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
7818 || operand_check == check_error))
7819 {
7820 as_bad (_("ambiguous operand size for `%s'"), insn_name (&i.tm));
7821 return 0;
7822 }
7823 if (operand_check == check_error)
7824 {
7825 as_bad (_("no instruction mnemonic suffix given and "
7826 "no register operands; can't size `%s'"), insn_name (&i.tm));
7827 return 0;
7828 }
7829 if (operand_check == check_warning)
7830 as_warn (_("%s; using default for `%s'"),
7831 intel_syntax
7832 ? _("ambiguous operand size")
7833 : _("no instruction mnemonic suffix given and "
7834 "no register operands"),
7835 insn_name (&i.tm));
7836
7837 if (i.tm.opcode_modifier.floatmf)
7838 i.suffix = SHORT_MNEM_SUFFIX;
7839 else if (is_movx)
7840 /* handled below */;
7841 else if (evex)
7842 i.tm.opcode_modifier.evex = evex;
7843 else if (flag_code == CODE_16BIT)
7844 i.suffix = WORD_MNEM_SUFFIX;
7845 else if (!i.tm.opcode_modifier.no_lsuf)
7846 i.suffix = LONG_MNEM_SUFFIX;
7847 else
7848 i.suffix = QWORD_MNEM_SUFFIX;
7849 }
7850 }
7851
7852 if (is_movx)
7853 {
7854 /* In Intel syntax, movsx/movzx must have a "suffix" (checked above).
7855 In AT&T syntax, if there is no suffix (warned about above), the default
7856 will be byte extension. */
7857 if (i.tm.opcode_modifier.w && i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
7858 i.tm.base_opcode |= 1;
7859
7860 /* For further processing, the suffix should represent the destination
7861 (register). This is already the case when one was used with
7862 mov[sz][bw]*, but we need to replace it for mov[sz]x, or if there was
7863 no suffix to begin with. */
7864 if (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63 || !i.suffix)
7865 {
7866 if (i.types[1].bitfield.word)
7867 i.suffix = WORD_MNEM_SUFFIX;
7868 else if (i.types[1].bitfield.qword)
7869 i.suffix = QWORD_MNEM_SUFFIX;
7870 else
7871 i.suffix = LONG_MNEM_SUFFIX;
7872
7873 i.tm.opcode_modifier.w = 0;
7874 }
7875 }
7876
7877 if (!i.tm.opcode_modifier.modrm && i.reg_operands && i.tm.operands < 3)
7878 i.short_form = (i.tm.operand_types[0].bitfield.class == Reg)
7879 != (i.tm.operand_types[1].bitfield.class == Reg);
7880
7881 /* Change the opcode based on the operand size given by i.suffix. */
7882 switch (i.suffix)
7883 {
7884 /* Size floating point instruction. */
7885 case LONG_MNEM_SUFFIX:
7886 if (i.tm.opcode_modifier.floatmf)
7887 {
7888 i.tm.base_opcode ^= 4;
7889 break;
7890 }
7891 /* fall through */
7892 case WORD_MNEM_SUFFIX:
7893 case QWORD_MNEM_SUFFIX:
7894 /* It's not a byte, select word/dword operation. */
7895 if (i.tm.opcode_modifier.w)
7896 {
7897 if (i.short_form)
7898 i.tm.base_opcode |= 8;
7899 else
7900 i.tm.base_opcode |= 1;
7901 }
7902 /* fall through */
7903 case SHORT_MNEM_SUFFIX:
7904 /* Now select between word & dword operations via the operand
7905 size prefix, except for instructions that will ignore this
7906 prefix anyway. */
7907 if (i.suffix != QWORD_MNEM_SUFFIX
7908 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
7909 && !i.tm.opcode_modifier.floatmf
7910 && !is_any_vex_encoding (&i.tm)
7911 && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
7912 || (flag_code == CODE_64BIT
7913 && i.tm.opcode_modifier.jump == JUMP_BYTE)))
7914 {
7915 unsigned int prefix = DATA_PREFIX_OPCODE;
7916
7917 if (i.tm.opcode_modifier.jump == JUMP_BYTE) /* jcxz, loop */
7918 prefix = ADDR_PREFIX_OPCODE;
7919
7920 if (!add_prefix (prefix))
7921 return 0;
7922 }
7923
7924 /* Set mode64 for an operand. */
7925 if (i.suffix == QWORD_MNEM_SUFFIX
7926 && flag_code == CODE_64BIT
7927 && !i.tm.opcode_modifier.norex64
7928 && !i.tm.opcode_modifier.vexw
7929 /* Special case for xchg %rax,%rax. It is NOP and doesn't
7930 need rex64. */
7931 && ! (i.operands == 2
7932 && i.tm.base_opcode == 0x90
7933 && i.tm.opcode_space == SPACE_BASE
7934 && i.types[0].bitfield.instance == Accum
7935 && i.types[0].bitfield.qword
7936 && i.types[1].bitfield.instance == Accum))
7937 i.rex |= REX_W;
7938
7939 break;
7940
7941 case 0:
7942 /* Select word/dword/qword operation with explicit data sizing prefix
7943 when there are no suitable register operands. */
7944 if (i.tm.opcode_modifier.w
7945 && (i.prefix[DATA_PREFIX] || (i.prefix[REX_PREFIX] & REX_W))
7946 && (!i.reg_operands
7947 || (i.reg_operands == 1
7948 /* ShiftCount */
7949 && (i.tm.operand_types[0].bitfield.instance == RegC
7950 /* InOutPortReg */
7951 || i.tm.operand_types[0].bitfield.instance == RegD
7952 || i.tm.operand_types[1].bitfield.instance == RegD
7953 || i.tm.mnem_off == MN_crc32))))
7954 i.tm.base_opcode |= 1;
7955 break;
7956 }
7957
7958 if (i.tm.opcode_modifier.operandconstraint == ADDR_PREFIX_OP_REG)
7959 {
7960 gas_assert (!i.suffix);
7961 gas_assert (i.reg_operands);
7962
7963 if (i.tm.operand_types[0].bitfield.instance == Accum
7964 || i.operands == 1)
7965 {
7966 /* The address size override prefix changes the size of the
7967 first operand. */
7968 if (flag_code == CODE_64BIT
7969 && i.op[0].regs->reg_type.bitfield.word)
7970 {
7971 as_bad (_("16-bit addressing unavailable for `%s'"),
7972 insn_name (&i.tm));
7973 return 0;
7974 }
7975
7976 if ((flag_code == CODE_32BIT
7977 ? i.op[0].regs->reg_type.bitfield.word
7978 : i.op[0].regs->reg_type.bitfield.dword)
7979 && !add_prefix (ADDR_PREFIX_OPCODE))
7980 return 0;
7981 }
7982 else
7983 {
7984 /* Check invalid register operand when the address size override
7985 prefix changes the size of register operands. */
7986 unsigned int op;
7987 enum { need_word, need_dword, need_qword } need;
7988
7989 /* Check the register operand for the address size prefix if
7990 the memory operand has no real registers, like symbol, DISP
7991 or bogus (x32-only) symbol(%rip) when symbol(%eip) is meant. */
7992 if (i.mem_operands == 1
7993 && i.reg_operands == 1
7994 && i.operands == 2
7995 && i.types[1].bitfield.class == Reg
7996 && (flag_code == CODE_32BIT
7997 ? i.op[1].regs->reg_type.bitfield.word
7998 : i.op[1].regs->reg_type.bitfield.dword)
7999 && ((i.base_reg == NULL && i.index_reg == NULL)
8000 #if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
8001 || (x86_elf_abi == X86_64_X32_ABI
8002 && i.base_reg
8003 && i.base_reg->reg_num == RegIP
8004 && i.base_reg->reg_type.bitfield.qword))
8005 #else
8006 || 0)
8007 #endif
8008 && !add_prefix (ADDR_PREFIX_OPCODE))
8009 return 0;
8010
8011 if (flag_code == CODE_32BIT)
8012 need = i.prefix[ADDR_PREFIX] ? need_word : need_dword;
8013 else if (i.prefix[ADDR_PREFIX])
8014 need = need_dword;
8015 else
8016 need = flag_code == CODE_64BIT ? need_qword : need_word;
8017
8018 for (op = 0; op < i.operands; op++)
8019 {
8020 if (i.types[op].bitfield.class != Reg)
8021 continue;
8022
8023 switch (need)
8024 {
8025 case need_word:
8026 if (i.op[op].regs->reg_type.bitfield.word)
8027 continue;
8028 break;
8029 case need_dword:
8030 if (i.op[op].regs->reg_type.bitfield.dword)
8031 continue;
8032 break;
8033 case need_qword:
8034 if (i.op[op].regs->reg_type.bitfield.qword)
8035 continue;
8036 break;
8037 }
8038
8039 as_bad (_("invalid register operand size for `%s'"),
8040 insn_name (&i.tm));
8041 return 0;
8042 }
8043 }
8044 }
8045
8046 return 1;
8047 }
8048
8049 static int
8050 check_byte_reg (void)
8051 {
8052 int op;
8053
8054 for (op = i.operands; --op >= 0;)
8055 {
8056 /* Skip non-register operands. */
8057 if (i.types[op].bitfield.class != Reg)
8058 continue;
8059
8060 /* If this is an eight bit register, it's OK. If it's the 16 or
8061 32 bit version of an eight bit register, we will just use the
8062 low portion, and that's OK too. */
8063 if (i.types[op].bitfield.byte)
8064 continue;
8065
8066 /* I/O port address operands are OK too. */
8067 if (i.tm.operand_types[op].bitfield.instance == RegD
8068 && i.tm.operand_types[op].bitfield.word)
8069 continue;
8070
8071 /* crc32 only wants its source operand checked here. */
8072 if (i.tm.mnem_off == MN_crc32 && op != 0)
8073 continue;
8074
8075 /* Any other register is bad. */
8076 as_bad (_("`%s%s' not allowed with `%s%c'"),
8077 register_prefix, i.op[op].regs->reg_name,
8078 insn_name (&i.tm), i.suffix);
8079 return 0;
8080 }
8081 return 1;
8082 }
8083
8084 static int
8085 check_long_reg (void)
8086 {
8087 int op;
8088
8089 for (op = i.operands; --op >= 0;)
8090 /* Skip non-register operands. */
8091 if (i.types[op].bitfield.class != Reg)
8092 continue;
8093 /* Reject eight bit registers, except where the template requires
8094 them. (eg. movzb) */
8095 else if (i.types[op].bitfield.byte
8096 && (i.tm.operand_types[op].bitfield.class == Reg
8097 || i.tm.operand_types[op].bitfield.instance == Accum)
8098 && (i.tm.operand_types[op].bitfield.word
8099 || i.tm.operand_types[op].bitfield.dword))
8100 {
8101 as_bad (_("`%s%s' not allowed with `%s%c'"),
8102 register_prefix,
8103 i.op[op].regs->reg_name,
8104 insn_name (&i.tm),
8105 i.suffix);
8106 return 0;
8107 }
8108 /* Error if the e prefix on a general reg is missing. */
8109 else if (i.types[op].bitfield.word
8110 && (i.tm.operand_types[op].bitfield.class == Reg
8111 || i.tm.operand_types[op].bitfield.instance == Accum)
8112 && i.tm.operand_types[op].bitfield.dword)
8113 {
8114 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
8115 register_prefix, i.op[op].regs->reg_name,
8116 i.suffix);
8117 return 0;
8118 }
8119 /* Warn if the r prefix on a general reg is present. */
8120 else if (i.types[op].bitfield.qword
8121 && (i.tm.operand_types[op].bitfield.class == Reg
8122 || i.tm.operand_types[op].bitfield.instance == Accum)
8123 && i.tm.operand_types[op].bitfield.dword)
8124 {
8125 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
8126 register_prefix, i.op[op].regs->reg_name, i.suffix);
8127 return 0;
8128 }
8129 return 1;
8130 }
8131
8132 static int
8133 check_qword_reg (void)
8134 {
8135 int op;
8136
8137 for (op = i.operands; --op >= 0; )
8138 /* Skip non-register operands. */
8139 if (i.types[op].bitfield.class != Reg)
8140 continue;
8141 /* Reject eight bit registers, except where the template requires
8142 them. (eg. movzb) */
8143 else if (i.types[op].bitfield.byte
8144 && (i.tm.operand_types[op].bitfield.class == Reg
8145 || i.tm.operand_types[op].bitfield.instance == Accum)
8146 && (i.tm.operand_types[op].bitfield.word
8147 || i.tm.operand_types[op].bitfield.dword))
8148 {
8149 as_bad (_("`%s%s' not allowed with `%s%c'"),
8150 register_prefix,
8151 i.op[op].regs->reg_name,
8152 insn_name (&i.tm),
8153 i.suffix);
8154 return 0;
8155 }
8156 /* Warn if the r prefix on a general reg is missing. */
8157 else if ((i.types[op].bitfield.word
8158 || i.types[op].bitfield.dword)
8159 && (i.tm.operand_types[op].bitfield.class == Reg
8160 || i.tm.operand_types[op].bitfield.instance == Accum)
8161 && i.tm.operand_types[op].bitfield.qword)
8162 {
8163 /* Prohibit these changes in the 64bit mode, since the
8164 lowering is more complicated. */
8165 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
8166 register_prefix, i.op[op].regs->reg_name, i.suffix);
8167 return 0;
8168 }
8169 return 1;
8170 }
8171
8172 static int
8173 check_word_reg (void)
8174 {
8175 int op;
8176 for (op = i.operands; --op >= 0;)
8177 /* Skip non-register operands. */
8178 if (i.types[op].bitfield.class != Reg)
8179 continue;
8180 /* Reject eight bit registers, except where the template requires
8181 them. (eg. movzb) */
8182 else if (i.types[op].bitfield.byte
8183 && (i.tm.operand_types[op].bitfield.class == Reg
8184 || i.tm.operand_types[op].bitfield.instance == Accum)
8185 && (i.tm.operand_types[op].bitfield.word
8186 || i.tm.operand_types[op].bitfield.dword))
8187 {
8188 as_bad (_("`%s%s' not allowed with `%s%c'"),
8189 register_prefix,
8190 i.op[op].regs->reg_name,
8191 insn_name (&i.tm),
8192 i.suffix);
8193 return 0;
8194 }
8195 /* Error if the e or r prefix on a general reg is present. */
8196 else if ((i.types[op].bitfield.dword
8197 || i.types[op].bitfield.qword)
8198 && (i.tm.operand_types[op].bitfield.class == Reg
8199 || i.tm.operand_types[op].bitfield.instance == Accum)
8200 && i.tm.operand_types[op].bitfield.word)
8201 {
8202 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
8203 register_prefix, i.op[op].regs->reg_name,
8204 i.suffix);
8205 return 0;
8206 }
8207 return 1;
8208 }
8209
8210 static int
8211 update_imm (unsigned int j)
8212 {
8213 i386_operand_type overlap = i.types[j];
8214
8215 if (i.tm.operand_types[j].bitfield.imm8
8216 && i.tm.operand_types[j].bitfield.imm8s
8217 && overlap.bitfield.imm8 && overlap.bitfield.imm8s)
8218 {
8219 /* This combination is used on 8-bit immediates where e.g. $~0 is
8220 desirable to permit. We're past operand type matching, so simply
8221 put things back in the shape they were before introducing the
8222 distinction between Imm8, Imm8S, and Imm8|Imm8S. */
8223 overlap.bitfield.imm8s = 0;
8224 }
8225
8226 if (overlap.bitfield.imm8
8227 + overlap.bitfield.imm8s
8228 + overlap.bitfield.imm16
8229 + overlap.bitfield.imm32
8230 + overlap.bitfield.imm32s
8231 + overlap.bitfield.imm64 > 1)
8232 {
8233 static const i386_operand_type imm16 = { .bitfield = { .imm16 = 1 } };
8234 static const i386_operand_type imm32 = { .bitfield = { .imm32 = 1 } };
8235 static const i386_operand_type imm32s = { .bitfield = { .imm32s = 1 } };
8236 static const i386_operand_type imm16_32 = { .bitfield =
8237 { .imm16 = 1, .imm32 = 1 }
8238 };
8239 static const i386_operand_type imm16_32s = { .bitfield =
8240 { .imm16 = 1, .imm32s = 1 }
8241 };
8242 static const i386_operand_type imm16_32_32s = { .bitfield =
8243 { .imm16 = 1, .imm32 = 1, .imm32s = 1 }
8244 };
8245
8246 if (i.suffix)
8247 {
8248 i386_operand_type temp;
8249
8250 operand_type_set (&temp, 0);
8251 if (i.suffix == BYTE_MNEM_SUFFIX)
8252 {
8253 temp.bitfield.imm8 = overlap.bitfield.imm8;
8254 temp.bitfield.imm8s = overlap.bitfield.imm8s;
8255 }
8256 else if (i.suffix == WORD_MNEM_SUFFIX)
8257 temp.bitfield.imm16 = overlap.bitfield.imm16;
8258 else if (i.suffix == QWORD_MNEM_SUFFIX)
8259 {
8260 temp.bitfield.imm64 = overlap.bitfield.imm64;
8261 temp.bitfield.imm32s = overlap.bitfield.imm32s;
8262 }
8263 else
8264 temp.bitfield.imm32 = overlap.bitfield.imm32;
8265 overlap = temp;
8266 }
8267 else if (operand_type_equal (&overlap, &imm16_32_32s)
8268 || operand_type_equal (&overlap, &imm16_32)
8269 || operand_type_equal (&overlap, &imm16_32s))
8270 {
8271 if ((flag_code == CODE_16BIT)
8272 ^ (i.prefix[DATA_PREFIX] != 0 && !(i.prefix[REX_PREFIX] & REX_W)))
8273 overlap = imm16;
8274 else
8275 overlap = imm32s;
8276 }
8277 else if (i.prefix[REX_PREFIX] & REX_W)
8278 overlap = operand_type_and (overlap, imm32s);
8279 else if (i.prefix[DATA_PREFIX])
8280 overlap = operand_type_and (overlap,
8281 flag_code != CODE_16BIT ? imm16 : imm32);
8282 if (overlap.bitfield.imm8
8283 + overlap.bitfield.imm8s
8284 + overlap.bitfield.imm16
8285 + overlap.bitfield.imm32
8286 + overlap.bitfield.imm32s
8287 + overlap.bitfield.imm64 != 1)
8288 {
8289 as_bad (_("no instruction mnemonic suffix given; "
8290 "can't determine immediate size"));
8291 return 0;
8292 }
8293 }
8294 i.types[j] = overlap;
8295
8296 return 1;
8297 }
8298
8299 static int
8300 finalize_imm (void)
8301 {
8302 unsigned int j, n;
8303
8304 /* Update the first 2 immediate operands. */
8305 n = i.operands > 2 ? 2 : i.operands;
8306 if (n)
8307 {
8308 for (j = 0; j < n; j++)
8309 if (update_imm (j) == 0)
8310 return 0;
8311
8312 /* The 3rd operand can't be immediate operand. */
8313 gas_assert (operand_type_check (i.types[2], imm) == 0);
8314 }
8315
8316 return 1;
8317 }
8318
8319 static INLINE void set_rex_vrex (const reg_entry *r, unsigned int rex_bit,
8320 bool do_sse2avx)
8321 {
8322 if (r->reg_flags & RegRex)
8323 {
8324 if (i.rex & rex_bit)
8325 as_bad (_("same type of prefix used twice"));
8326 i.rex |= rex_bit;
8327 }
8328 else if (do_sse2avx && (i.rex & rex_bit) && i.vex.register_specifier)
8329 {
8330 gas_assert (i.vex.register_specifier == r);
8331 i.vex.register_specifier += 8;
8332 }
8333
8334 if (r->reg_flags & RegVRex)
8335 i.vrex |= rex_bit;
8336 }
8337
8338 static int
8339 process_operands (void)
8340 {
8341 /* Default segment register this instruction will use for memory
8342 accesses. 0 means unknown. This is only for optimizing out
8343 unnecessary segment overrides. */
8344 const reg_entry *default_seg = NULL;
8345
8346 /* We only need to check those implicit registers for instructions
8347 with 3 operands or less. */
8348 if (i.operands <= 3)
8349 for (unsigned int j = 0; j < i.operands; j++)
8350 if (i.types[j].bitfield.instance != InstanceNone)
8351 i.reg_operands--;
8352
8353 if (i.tm.opcode_modifier.sse2avx)
8354 {
8355 /* Legacy encoded insns allow explicit REX prefixes, so these prefixes
8356 need converting. */
8357 i.rex |= i.prefix[REX_PREFIX] & (REX_W | REX_R | REX_X | REX_B);
8358 i.prefix[REX_PREFIX] = 0;
8359 i.rex_encoding = 0;
8360 }
8361 /* ImmExt should be processed after SSE2AVX. */
8362 else if (i.tm.opcode_modifier.immext)
8363 process_immext ();
8364
8365 /* TILEZERO is unusual in that it has a single operand encoded in ModR/M.reg,
8366 not ModR/M.rm. To avoid special casing this in build_modrm_byte(), fake a
8367 new destination operand here, while converting the source one to register
8368 number 0. */
8369 if (i.tm.mnem_off == MN_tilezero)
8370 {
8371 i.op[1].regs = i.op[0].regs;
8372 i.op[0].regs -= i.op[0].regs->reg_num;
8373 i.types[1] = i.types[0];
8374 i.tm.operand_types[1] = i.tm.operand_types[0];
8375 i.flags[1] = i.flags[0];
8376 i.operands++;
8377 i.reg_operands++;
8378 i.tm.operands++;
8379 }
8380
8381 if (i.tm.opcode_modifier.sse2avx && i.tm.opcode_modifier.vexvvvv)
8382 {
8383 static const i386_operand_type regxmm = {
8384 .bitfield = { .class = RegSIMD, .xmmword = 1 }
8385 };
8386 unsigned int dupl = i.operands;
8387 unsigned int dest = dupl - 1;
8388 unsigned int j;
8389
8390 /* The destination must be an xmm register. */
8391 gas_assert (i.reg_operands
8392 && MAX_OPERANDS > dupl
8393 && operand_type_equal (&i.types[dest], &regxmm));
8394
8395 if (i.tm.operand_types[0].bitfield.instance == Accum
8396 && i.tm.operand_types[0].bitfield.xmmword)
8397 {
8398 /* Keep xmm0 for instructions with VEX prefix and 3
8399 sources. */
8400 i.tm.operand_types[0].bitfield.instance = InstanceNone;
8401 i.tm.operand_types[0].bitfield.class = RegSIMD;
8402 i.reg_operands++;
8403 goto duplicate;
8404 }
8405
8406 if (i.tm.opcode_modifier.operandconstraint == IMPLICIT_1ST_XMM0)
8407 {
8408 gas_assert ((MAX_OPERANDS - 1) > dupl);
8409
8410 /* Add the implicit xmm0 for instructions with VEX prefix
8411 and 3 sources. */
8412 for (j = i.operands; j > 0; j--)
8413 {
8414 i.op[j] = i.op[j - 1];
8415 i.types[j] = i.types[j - 1];
8416 i.tm.operand_types[j] = i.tm.operand_types[j - 1];
8417 i.flags[j] = i.flags[j - 1];
8418 }
8419 i.op[0].regs
8420 = (const reg_entry *) str_hash_find (reg_hash, "xmm0");
8421 i.types[0] = regxmm;
8422 i.tm.operand_types[0] = regxmm;
8423
8424 i.operands += 2;
8425 i.reg_operands += 2;
8426 i.tm.operands += 2;
8427
8428 dupl++;
8429 dest++;
8430 i.op[dupl] = i.op[dest];
8431 i.types[dupl] = i.types[dest];
8432 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8433 i.flags[dupl] = i.flags[dest];
8434 }
8435 else
8436 {
8437 duplicate:
8438 i.operands++;
8439 i.reg_operands++;
8440 i.tm.operands++;
8441
8442 i.op[dupl] = i.op[dest];
8443 i.types[dupl] = i.types[dest];
8444 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8445 i.flags[dupl] = i.flags[dest];
8446 }
8447
8448 if (i.tm.opcode_modifier.immext)
8449 process_immext ();
8450 }
8451 else if (i.tm.operand_types[0].bitfield.instance == Accum
8452 && i.tm.opcode_modifier.modrm)
8453 {
8454 unsigned int j;
8455
8456 for (j = 1; j < i.operands; j++)
8457 {
8458 i.op[j - 1] = i.op[j];
8459 i.types[j - 1] = i.types[j];
8460
8461 /* We need to adjust fields in i.tm since they are used by
8462 build_modrm_byte. */
8463 i.tm.operand_types [j - 1] = i.tm.operand_types [j];
8464
8465 i.flags[j - 1] = i.flags[j];
8466 }
8467
8468 /* No adjustment to i.reg_operands: This was already done at the top
8469 of the function. */
8470 i.operands--;
8471 i.tm.operands--;
8472 }
8473 else if (i.tm.opcode_modifier.operandconstraint == IMPLICIT_QUAD_GROUP)
8474 {
8475 unsigned int regnum, first_reg_in_group, last_reg_in_group;
8476
8477 /* The second operand must be {x,y,z}mmN, where N is a multiple of 4. */
8478 gas_assert (i.operands >= 2 && i.types[1].bitfield.class == RegSIMD);
8479 regnum = register_number (i.op[1].regs);
8480 first_reg_in_group = regnum & ~3;
8481 last_reg_in_group = first_reg_in_group + 3;
8482 if (regnum != first_reg_in_group)
8483 as_warn (_("source register `%s%s' implicitly denotes"
8484 " `%s%.3s%u' to `%s%.3s%u' source group in `%s'"),
8485 register_prefix, i.op[1].regs->reg_name,
8486 register_prefix, i.op[1].regs->reg_name, first_reg_in_group,
8487 register_prefix, i.op[1].regs->reg_name, last_reg_in_group,
8488 insn_name (&i.tm));
8489 }
8490 else if (i.tm.opcode_modifier.operandconstraint == REG_KLUDGE)
8491 {
8492 /* The imul $imm, %reg instruction is converted into
8493 imul $imm, %reg, %reg, and the clr %reg instruction
8494 is converted into xor %reg, %reg. */
8495
8496 unsigned int first_reg_op;
8497
8498 if (operand_type_check (i.types[0], reg))
8499 first_reg_op = 0;
8500 else
8501 first_reg_op = 1;
8502 /* Pretend we saw the extra register operand. */
8503 gas_assert (i.reg_operands == 1
8504 && i.op[first_reg_op + 1].regs == 0);
8505 i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
8506 i.types[first_reg_op + 1] = i.types[first_reg_op];
8507 i.operands++;
8508 i.reg_operands++;
8509 }
8510
8511 if (i.tm.opcode_modifier.modrm)
8512 {
8513 /* The opcode is completed (modulo i.tm.extension_opcode which
8514 must be put into the modrm byte). Now, we make the modrm and
8515 index base bytes based on all the info we've collected. */
8516
8517 default_seg = build_modrm_byte ();
8518
8519 if (!quiet_warnings && i.tm.opcode_modifier.operandconstraint == UGH)
8520 {
8521 /* Warn about some common errors, but press on regardless. */
8522 if (i.operands == 2)
8523 {
8524 /* Reversed arguments on faddp or fmulp. */
8525 as_warn (_("translating to `%s %s%s,%s%s'"), insn_name (&i.tm),
8526 register_prefix, i.op[!intel_syntax].regs->reg_name,
8527 register_prefix, i.op[intel_syntax].regs->reg_name);
8528 }
8529 else if (i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
8530 {
8531 /* Extraneous `l' suffix on fp insn. */
8532 as_warn (_("translating to `%s %s%s'"), insn_name (&i.tm),
8533 register_prefix, i.op[0].regs->reg_name);
8534 }
8535 }
8536 }
8537 else if (i.types[0].bitfield.class == SReg && !dot_insn ())
8538 {
8539 if (flag_code != CODE_64BIT
8540 ? i.tm.base_opcode == POP_SEG_SHORT
8541 && i.op[0].regs->reg_num == 1
8542 : (i.tm.base_opcode | 1) == (POP_SEG386_SHORT & 0xff)
8543 && i.op[0].regs->reg_num < 4)
8544 {
8545 as_bad (_("you can't `%s %s%s'"),
8546 insn_name (&i.tm), register_prefix, i.op[0].regs->reg_name);
8547 return 0;
8548 }
8549 if (i.op[0].regs->reg_num > 3
8550 && i.tm.opcode_space == SPACE_BASE )
8551 {
8552 i.tm.base_opcode ^= (POP_SEG_SHORT ^ POP_SEG386_SHORT) & 0xff;
8553 i.tm.opcode_space = SPACE_0F;
8554 }
8555 i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
8556 }
8557 else if (i.tm.opcode_space == SPACE_BASE
8558 && (i.tm.base_opcode & ~3) == MOV_AX_DISP32)
8559 {
8560 default_seg = reg_ds;
8561 }
8562 else if (i.tm.opcode_modifier.isstring)
8563 {
8564 /* For the string instructions that allow a segment override
8565 on one of their operands, the default segment is ds. */
8566 default_seg = reg_ds;
8567 }
8568 else if (i.short_form)
8569 {
8570 /* The register operand is in the 1st or 2nd non-immediate operand. */
8571 const reg_entry *r = i.op[i.imm_operands].regs;
8572
8573 if (!dot_insn ()
8574 && r->reg_type.bitfield.instance == Accum
8575 && i.op[i.imm_operands + 1].regs)
8576 r = i.op[i.imm_operands + 1].regs;
8577 /* Register goes in low 3 bits of opcode. */
8578 i.tm.base_opcode |= r->reg_num;
8579 set_rex_vrex (r, REX_B, false);
8580
8581 if (dot_insn () && i.reg_operands == 2)
8582 {
8583 gas_assert (is_any_vex_encoding (&i.tm)
8584 || i.vec_encoding != vex_encoding_default);
8585 i.vex.register_specifier = i.op[i.operands - 1].regs;
8586 }
8587 }
8588 else if (i.reg_operands == 1
8589 && !i.flags[i.operands - 1]
8590 && i.tm.operand_types[i.operands - 1].bitfield.instance
8591 == InstanceNone)
8592 {
8593 gas_assert (is_any_vex_encoding (&i.tm)
8594 || i.vec_encoding != vex_encoding_default);
8595 i.vex.register_specifier = i.op[i.operands - 1].regs;
8596 }
8597
8598 if ((i.seg[0] || i.prefix[SEG_PREFIX])
8599 && i.tm.mnem_off == MN_lea)
8600 {
8601 if (!quiet_warnings)
8602 as_warn (_("segment override on `%s' is ineffectual"), insn_name (&i.tm));
8603 if (optimize && !i.no_optimize)
8604 {
8605 i.seg[0] = NULL;
8606 i.prefix[SEG_PREFIX] = 0;
8607 }
8608 }
8609
8610 /* If a segment was explicitly specified, and the specified segment
8611 is neither the default nor the one already recorded from a prefix,
8612 use an opcode prefix to select it. If we never figured out what
8613 the default segment is, then default_seg will be zero at this
8614 point, and the specified segment prefix will always be used. */
8615 if (i.seg[0]
8616 && i.seg[0] != default_seg
8617 && i386_seg_prefixes[i.seg[0]->reg_num] != i.prefix[SEG_PREFIX])
8618 {
8619 if (!add_prefix (i386_seg_prefixes[i.seg[0]->reg_num]))
8620 return 0;
8621 }
8622 return 1;
8623 }
8624
8625 static const reg_entry *
8626 build_modrm_byte (void)
8627 {
8628 const reg_entry *default_seg = NULL;
8629 unsigned int source = i.imm_operands - i.tm.opcode_modifier.immext
8630 /* Compensate for kludge in md_assemble(). */
8631 + i.tm.operand_types[0].bitfield.imm1;
8632 unsigned int dest = i.operands - 1 - i.tm.opcode_modifier.immext;
8633 unsigned int v, op, reg_slot = ~0;
8634
8635 /* Accumulator (in particular %st), shift count (%cl), and alike need
8636 to be skipped just like immediate operands do. */
8637 if (i.tm.operand_types[source].bitfield.instance)
8638 ++source;
8639 while (i.tm.operand_types[dest].bitfield.instance)
8640 --dest;
8641
8642 for (op = source; op < i.operands; ++op)
8643 if (i.tm.operand_types[op].bitfield.baseindex)
8644 break;
8645
8646 if (i.reg_operands + i.mem_operands + (i.tm.extension_opcode != None) == 4)
8647 {
8648 expressionS *exp;
8649
8650 /* There are 2 kinds of instructions:
8651 1. 5 operands: 4 register operands or 3 register operands
8652 plus 1 memory operand plus one Imm4 operand, VexXDS, and
8653 VexW0 or VexW1. The destination must be either XMM, YMM or
8654 ZMM register.
8655 2. 4 operands: 4 register operands or 3 register operands
8656 plus 1 memory operand, with VexXDS.
8657 3. Other equivalent combinations when coming from s_insn(). */
8658 gas_assert (i.tm.opcode_modifier.vexvvvv
8659 && i.tm.opcode_modifier.vexw);
8660 gas_assert (dot_insn ()
8661 || i.tm.operand_types[dest].bitfield.class == RegSIMD);
8662
8663 /* Of the first two non-immediate operands the one with the template
8664 not allowing for a memory one is encoded in the immediate operand. */
8665 if (source == op)
8666 reg_slot = source + 1;
8667 else
8668 reg_slot = source++;
8669
8670 if (!dot_insn ())
8671 {
8672 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
8673 gas_assert (!(i.op[reg_slot].regs->reg_flags & RegVRex));
8674 }
8675 else
8676 gas_assert (i.tm.operand_types[reg_slot].bitfield.class != ClassNone);
8677
8678 if (i.imm_operands == 0)
8679 {
8680 /* When there is no immediate operand, generate an 8bit
8681 immediate operand to encode the first operand. */
8682 exp = &im_expressions[i.imm_operands++];
8683 i.op[i.operands].imms = exp;
8684 i.types[i.operands].bitfield.imm8 = 1;
8685 i.operands++;
8686
8687 exp->X_op = O_constant;
8688 }
8689 else
8690 {
8691 gas_assert (i.imm_operands == 1);
8692 gas_assert (fits_in_imm4 (i.op[0].imms->X_add_number));
8693 gas_assert (!i.tm.opcode_modifier.immext);
8694
8695 /* Turn on Imm8 again so that output_imm will generate it. */
8696 i.types[0].bitfield.imm8 = 1;
8697
8698 exp = i.op[0].imms;
8699 }
8700 exp->X_add_number |= register_number (i.op[reg_slot].regs)
8701 << (3 + !(is_evex_encoding (&i.tm)
8702 || i.vec_encoding == vex_encoding_evex));
8703 }
8704
8705 for (v = source + 1; v < dest; ++v)
8706 if (v != reg_slot)
8707 break;
8708 if (v >= dest)
8709 v = ~0;
8710 if (i.tm.extension_opcode != None)
8711 {
8712 if (dest != source)
8713 v = dest;
8714 dest = ~0;
8715 }
8716 gas_assert (source < dest);
8717 if (i.tm.opcode_modifier.operandconstraint == SWAP_SOURCES
8718 && source != op)
8719 {
8720 unsigned int tmp = source;
8721
8722 source = v;
8723 v = tmp;
8724 }
8725
8726 if (v < MAX_OPERANDS)
8727 {
8728 gas_assert (i.tm.opcode_modifier.vexvvvv);
8729 i.vex.register_specifier = i.op[v].regs;
8730 }
8731
8732 if (op < i.operands)
8733 {
8734 if (i.mem_operands)
8735 {
8736 unsigned int fake_zero_displacement = 0;
8737
8738 gas_assert (i.flags[op] & Operand_Mem);
8739
8740 if (i.tm.opcode_modifier.sib)
8741 {
8742 /* The index register of VSIB shouldn't be RegIZ. */
8743 if (i.tm.opcode_modifier.sib != SIBMEM
8744 && i.index_reg->reg_num == RegIZ)
8745 abort ();
8746
8747 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8748 if (!i.base_reg)
8749 {
8750 i.sib.base = NO_BASE_REGISTER;
8751 i.sib.scale = i.log2_scale_factor;
8752 i.types[op] = operand_type_and_not (i.types[op], anydisp);
8753 i.types[op].bitfield.disp32 = 1;
8754 }
8755
8756 /* Since the mandatory SIB always has index register, so
8757 the code logic remains unchanged. The non-mandatory SIB
8758 without index register is allowed and will be handled
8759 later. */
8760 if (i.index_reg)
8761 {
8762 if (i.index_reg->reg_num == RegIZ)
8763 i.sib.index = NO_INDEX_REGISTER;
8764 else
8765 i.sib.index = i.index_reg->reg_num;
8766 set_rex_vrex (i.index_reg, REX_X, false);
8767 }
8768 }
8769
8770 default_seg = reg_ds;
8771
8772 if (i.base_reg == 0)
8773 {
8774 i.rm.mode = 0;
8775 if (!i.disp_operands)
8776 fake_zero_displacement = 1;
8777 if (i.index_reg == 0)
8778 {
8779 /* Both check for VSIB and mandatory non-vector SIB. */
8780 gas_assert (!i.tm.opcode_modifier.sib
8781 || i.tm.opcode_modifier.sib == SIBMEM);
8782 /* Operand is just <disp> */
8783 i.types[op] = operand_type_and_not (i.types[op], anydisp);
8784 if (flag_code == CODE_64BIT)
8785 {
8786 /* 64bit mode overwrites the 32bit absolute
8787 addressing by RIP relative addressing and
8788 absolute addressing is encoded by one of the
8789 redundant SIB forms. */
8790 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8791 i.sib.base = NO_BASE_REGISTER;
8792 i.sib.index = NO_INDEX_REGISTER;
8793 i.types[op].bitfield.disp32 = 1;
8794 }
8795 else if ((flag_code == CODE_16BIT)
8796 ^ (i.prefix[ADDR_PREFIX] != 0))
8797 {
8798 i.rm.regmem = NO_BASE_REGISTER_16;
8799 i.types[op].bitfield.disp16 = 1;
8800 }
8801 else
8802 {
8803 i.rm.regmem = NO_BASE_REGISTER;
8804 i.types[op].bitfield.disp32 = 1;
8805 }
8806 }
8807 else if (!i.tm.opcode_modifier.sib)
8808 {
8809 /* !i.base_reg && i.index_reg */
8810 if (i.index_reg->reg_num == RegIZ)
8811 i.sib.index = NO_INDEX_REGISTER;
8812 else
8813 i.sib.index = i.index_reg->reg_num;
8814 i.sib.base = NO_BASE_REGISTER;
8815 i.sib.scale = i.log2_scale_factor;
8816 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8817 i.types[op] = operand_type_and_not (i.types[op], anydisp);
8818 i.types[op].bitfield.disp32 = 1;
8819 if ((i.index_reg->reg_flags & RegRex) != 0)
8820 i.rex |= REX_X;
8821 }
8822 }
8823 /* RIP addressing for 64bit mode. */
8824 else if (i.base_reg->reg_num == RegIP)
8825 {
8826 gas_assert (!i.tm.opcode_modifier.sib);
8827 i.rm.regmem = NO_BASE_REGISTER;
8828 i.types[op].bitfield.disp8 = 0;
8829 i.types[op].bitfield.disp16 = 0;
8830 i.types[op].bitfield.disp32 = 1;
8831 i.types[op].bitfield.disp64 = 0;
8832 i.flags[op] |= Operand_PCrel;
8833 if (! i.disp_operands)
8834 fake_zero_displacement = 1;
8835 }
8836 else if (i.base_reg->reg_type.bitfield.word)
8837 {
8838 gas_assert (!i.tm.opcode_modifier.sib);
8839 switch (i.base_reg->reg_num)
8840 {
8841 case 3: /* (%bx) */
8842 if (i.index_reg == 0)
8843 i.rm.regmem = 7;
8844 else /* (%bx,%si) -> 0, or (%bx,%di) -> 1 */
8845 i.rm.regmem = i.index_reg->reg_num - 6;
8846 break;
8847 case 5: /* (%bp) */
8848 default_seg = reg_ss;
8849 if (i.index_reg == 0)
8850 {
8851 i.rm.regmem = 6;
8852 if (operand_type_check (i.types[op], disp) == 0)
8853 {
8854 /* fake (%bp) into 0(%bp) */
8855 if (i.disp_encoding == disp_encoding_16bit)
8856 i.types[op].bitfield.disp16 = 1;
8857 else
8858 i.types[op].bitfield.disp8 = 1;
8859 fake_zero_displacement = 1;
8860 }
8861 }
8862 else /* (%bp,%si) -> 2, or (%bp,%di) -> 3 */
8863 i.rm.regmem = i.index_reg->reg_num - 6 + 2;
8864 break;
8865 default: /* (%si) -> 4 or (%di) -> 5 */
8866 i.rm.regmem = i.base_reg->reg_num - 6 + 4;
8867 }
8868 if (!fake_zero_displacement
8869 && !i.disp_operands
8870 && i.disp_encoding)
8871 {
8872 fake_zero_displacement = 1;
8873 if (i.disp_encoding == disp_encoding_8bit)
8874 i.types[op].bitfield.disp8 = 1;
8875 else
8876 i.types[op].bitfield.disp16 = 1;
8877 }
8878 i.rm.mode = mode_from_disp_size (i.types[op]);
8879 }
8880 else /* i.base_reg and 32/64 bit mode */
8881 {
8882 if (operand_type_check (i.types[op], disp))
8883 {
8884 i.types[op].bitfield.disp16 = 0;
8885 i.types[op].bitfield.disp64 = 0;
8886 i.types[op].bitfield.disp32 = 1;
8887 }
8888
8889 if (!i.tm.opcode_modifier.sib)
8890 i.rm.regmem = i.base_reg->reg_num;
8891 if ((i.base_reg->reg_flags & RegRex) != 0)
8892 i.rex |= REX_B;
8893 i.sib.base = i.base_reg->reg_num;
8894 /* x86-64 ignores REX prefix bit here to avoid decoder
8895 complications. */
8896 if (!(i.base_reg->reg_flags & RegRex)
8897 && (i.base_reg->reg_num == EBP_REG_NUM
8898 || i.base_reg->reg_num == ESP_REG_NUM))
8899 default_seg = reg_ss;
8900 if (i.base_reg->reg_num == 5 && i.disp_operands == 0)
8901 {
8902 fake_zero_displacement = 1;
8903 if (i.disp_encoding == disp_encoding_32bit)
8904 i.types[op].bitfield.disp32 = 1;
8905 else
8906 i.types[op].bitfield.disp8 = 1;
8907 }
8908 i.sib.scale = i.log2_scale_factor;
8909 if (i.index_reg == 0)
8910 {
8911 /* Only check for VSIB. */
8912 gas_assert (i.tm.opcode_modifier.sib != VECSIB128
8913 && i.tm.opcode_modifier.sib != VECSIB256
8914 && i.tm.opcode_modifier.sib != VECSIB512);
8915
8916 /* <disp>(%esp) becomes two byte modrm with no index
8917 register. We've already stored the code for esp
8918 in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
8919 Any base register besides %esp will not use the
8920 extra modrm byte. */
8921 i.sib.index = NO_INDEX_REGISTER;
8922 }
8923 else if (!i.tm.opcode_modifier.sib)
8924 {
8925 if (i.index_reg->reg_num == RegIZ)
8926 i.sib.index = NO_INDEX_REGISTER;
8927 else
8928 i.sib.index = i.index_reg->reg_num;
8929 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8930 if ((i.index_reg->reg_flags & RegRex) != 0)
8931 i.rex |= REX_X;
8932 }
8933
8934 if (i.disp_operands
8935 && (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
8936 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL))
8937 i.rm.mode = 0;
8938 else
8939 {
8940 if (!fake_zero_displacement
8941 && !i.disp_operands
8942 && i.disp_encoding)
8943 {
8944 fake_zero_displacement = 1;
8945 if (i.disp_encoding == disp_encoding_8bit)
8946 i.types[op].bitfield.disp8 = 1;
8947 else
8948 i.types[op].bitfield.disp32 = 1;
8949 }
8950 i.rm.mode = mode_from_disp_size (i.types[op]);
8951 }
8952 }
8953
8954 if (fake_zero_displacement)
8955 {
8956 /* Fakes a zero displacement assuming that i.types[op]
8957 holds the correct displacement size. */
8958 expressionS *exp;
8959
8960 gas_assert (i.op[op].disps == 0);
8961 exp = &disp_expressions[i.disp_operands++];
8962 i.op[op].disps = exp;
8963 exp->X_op = O_constant;
8964 exp->X_add_number = 0;
8965 exp->X_add_symbol = (symbolS *) 0;
8966 exp->X_op_symbol = (symbolS *) 0;
8967 }
8968 }
8969 else
8970 {
8971 i.rm.mode = 3;
8972 i.rm.regmem = i.op[op].regs->reg_num;
8973 set_rex_vrex (i.op[op].regs, REX_B, false);
8974 }
8975
8976 if (op == dest)
8977 dest = ~0;
8978 if (op == source)
8979 source = ~0;
8980 }
8981 else
8982 {
8983 i.rm.mode = 3;
8984 if (!i.tm.opcode_modifier.regmem)
8985 {
8986 gas_assert (source < MAX_OPERANDS);
8987 i.rm.regmem = i.op[source].regs->reg_num;
8988 set_rex_vrex (i.op[source].regs, REX_B,
8989 dest >= MAX_OPERANDS && i.tm.opcode_modifier.sse2avx);
8990 source = ~0;
8991 }
8992 else
8993 {
8994 gas_assert (dest < MAX_OPERANDS);
8995 i.rm.regmem = i.op[dest].regs->reg_num;
8996 set_rex_vrex (i.op[dest].regs, REX_B, i.tm.opcode_modifier.sse2avx);
8997 dest = ~0;
8998 }
8999 }
9000
9001 /* Fill in i.rm.reg field with extension opcode (if any) or the
9002 appropriate register. */
9003 if (i.tm.extension_opcode != None)
9004 i.rm.reg = i.tm.extension_opcode;
9005 else if (!i.tm.opcode_modifier.regmem && dest < MAX_OPERANDS)
9006 {
9007 i.rm.reg = i.op[dest].regs->reg_num;
9008 set_rex_vrex (i.op[dest].regs, REX_R, i.tm.opcode_modifier.sse2avx);
9009 }
9010 else
9011 {
9012 gas_assert (source < MAX_OPERANDS);
9013 i.rm.reg = i.op[source].regs->reg_num;
9014 set_rex_vrex (i.op[source].regs, REX_R, false);
9015 }
9016
9017 if (flag_code != CODE_64BIT && (i.rex & REX_R))
9018 {
9019 gas_assert (i.types[!i.tm.opcode_modifier.regmem].bitfield.class == RegCR);
9020 i.rex &= ~REX_R;
9021 add_prefix (LOCK_PREFIX_OPCODE);
9022 }
9023
9024 return default_seg;
9025 }
9026
9027 static INLINE void
9028 frag_opcode_byte (unsigned char byte)
9029 {
9030 if (now_seg != absolute_section)
9031 FRAG_APPEND_1_CHAR (byte);
9032 else
9033 ++abs_section_offset;
9034 }
9035
9036 static unsigned int
9037 flip_code16 (unsigned int code16)
9038 {
9039 gas_assert (i.tm.operands == 1);
9040
9041 return !(i.prefix[REX_PREFIX] & REX_W)
9042 && (code16 ? i.tm.operand_types[0].bitfield.disp32
9043 : i.tm.operand_types[0].bitfield.disp16)
9044 ? CODE16 : 0;
9045 }
9046
9047 static void
9048 output_branch (void)
9049 {
9050 char *p;
9051 int size;
9052 int code16;
9053 int prefix;
9054 relax_substateT subtype;
9055 symbolS *sym;
9056 offsetT off;
9057
9058 if (now_seg == absolute_section)
9059 {
9060 as_bad (_("relaxable branches not supported in absolute section"));
9061 return;
9062 }
9063
9064 code16 = flag_code == CODE_16BIT ? CODE16 : 0;
9065 size = i.disp_encoding > disp_encoding_8bit ? BIG : SMALL;
9066
9067 prefix = 0;
9068 if (i.prefix[DATA_PREFIX] != 0)
9069 {
9070 prefix = 1;
9071 i.prefixes -= 1;
9072 code16 ^= flip_code16(code16);
9073 }
9074 /* Pentium4 branch hints. */
9075 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
9076 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
9077 {
9078 prefix++;
9079 i.prefixes--;
9080 }
9081 if (i.prefix[REX_PREFIX] != 0)
9082 {
9083 prefix++;
9084 i.prefixes--;
9085 }
9086
9087 /* BND prefixed jump. */
9088 if (i.prefix[BND_PREFIX] != 0)
9089 {
9090 prefix++;
9091 i.prefixes--;
9092 }
9093
9094 if (i.prefixes != 0)
9095 as_warn (_("skipping prefixes on `%s'"), insn_name (&i.tm));
9096
9097 /* It's always a symbol; End frag & setup for relax.
9098 Make sure there is enough room in this frag for the largest
9099 instruction we may generate in md_convert_frag. This is 2
9100 bytes for the opcode and room for the prefix and largest
9101 displacement. */
9102 frag_grow (prefix + 2 + 4);
9103 /* Prefix and 1 opcode byte go in fr_fix. */
9104 p = frag_more (prefix + 1);
9105 if (i.prefix[DATA_PREFIX] != 0)
9106 *p++ = DATA_PREFIX_OPCODE;
9107 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
9108 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
9109 *p++ = i.prefix[SEG_PREFIX];
9110 if (i.prefix[BND_PREFIX] != 0)
9111 *p++ = BND_PREFIX_OPCODE;
9112 if (i.prefix[REX_PREFIX] != 0)
9113 *p++ = i.prefix[REX_PREFIX];
9114 *p = i.tm.base_opcode;
9115
9116 if ((unsigned char) *p == JUMP_PC_RELATIVE)
9117 subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, size);
9118 else if (cpu_arch_flags.bitfield.cpui386)
9119 subtype = ENCODE_RELAX_STATE (COND_JUMP, size);
9120 else
9121 subtype = ENCODE_RELAX_STATE (COND_JUMP86, size);
9122 subtype |= code16;
9123
9124 sym = i.op[0].disps->X_add_symbol;
9125 off = i.op[0].disps->X_add_number;
9126
9127 if (i.op[0].disps->X_op != O_constant
9128 && i.op[0].disps->X_op != O_symbol)
9129 {
9130 /* Handle complex expressions. */
9131 sym = make_expr_symbol (i.op[0].disps);
9132 off = 0;
9133 }
9134
9135 /* 1 possible extra opcode + 4 byte displacement go in var part.
9136 Pass reloc in fr_var. */
9137 frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
9138 }
9139
9140 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9141 /* Return TRUE iff PLT32 relocation should be used for branching to
9142 symbol S. */
9143
9144 static bool
9145 need_plt32_p (symbolS *s)
9146 {
9147 /* PLT32 relocation is ELF only. */
9148 if (!IS_ELF)
9149 return false;
9150
9151 #ifdef TE_SOLARIS
9152 /* Don't emit PLT32 relocation on Solaris: neither native linker nor
9153 krtld support it. */
9154 return false;
9155 #endif
9156
9157 /* Since there is no need to prepare for PLT branch on x86-64, we
9158 can generate R_X86_64_PLT32, instead of R_X86_64_PC32, which can
9159 be used as a marker for 32-bit PC-relative branches. */
9160 if (!object_64bit)
9161 return false;
9162
9163 if (s == NULL)
9164 return false;
9165
9166 /* Weak or undefined symbol need PLT32 relocation. */
9167 if (S_IS_WEAK (s) || !S_IS_DEFINED (s))
9168 return true;
9169
9170 /* Non-global symbol doesn't need PLT32 relocation. */
9171 if (! S_IS_EXTERNAL (s))
9172 return false;
9173
9174 /* Other global symbols need PLT32 relocation. NB: Symbol with
9175 non-default visibilities are treated as normal global symbol
9176 so that PLT32 relocation can be used as a marker for 32-bit
9177 PC-relative branches. It is useful for linker relaxation. */
9178 return true;
9179 }
9180 #endif
9181
9182 static void
9183 output_jump (void)
9184 {
9185 char *p;
9186 int size;
9187 fixS *fixP;
9188 bfd_reloc_code_real_type jump_reloc = i.reloc[0];
9189
9190 if (i.tm.opcode_modifier.jump == JUMP_BYTE)
9191 {
9192 /* This is a loop or jecxz type instruction. */
9193 size = 1;
9194 if (i.prefix[ADDR_PREFIX] != 0)
9195 {
9196 frag_opcode_byte (ADDR_PREFIX_OPCODE);
9197 i.prefixes -= 1;
9198 }
9199 /* Pentium4 branch hints. */
9200 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
9201 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
9202 {
9203 frag_opcode_byte (i.prefix[SEG_PREFIX]);
9204 i.prefixes--;
9205 }
9206 }
9207 else
9208 {
9209 int code16;
9210
9211 code16 = 0;
9212 if (flag_code == CODE_16BIT)
9213 code16 = CODE16;
9214
9215 if (i.prefix[DATA_PREFIX] != 0)
9216 {
9217 frag_opcode_byte (DATA_PREFIX_OPCODE);
9218 i.prefixes -= 1;
9219 code16 ^= flip_code16(code16);
9220 }
9221
9222 size = 4;
9223 if (code16)
9224 size = 2;
9225 }
9226
9227 /* BND prefixed jump. */
9228 if (i.prefix[BND_PREFIX] != 0)
9229 {
9230 frag_opcode_byte (i.prefix[BND_PREFIX]);
9231 i.prefixes -= 1;
9232 }
9233
9234 if (i.prefix[REX_PREFIX] != 0)
9235 {
9236 frag_opcode_byte (i.prefix[REX_PREFIX]);
9237 i.prefixes -= 1;
9238 }
9239
9240 if (i.prefixes != 0)
9241 as_warn (_("skipping prefixes on `%s'"), insn_name (&i.tm));
9242
9243 if (now_seg == absolute_section)
9244 {
9245 abs_section_offset += i.opcode_length + size;
9246 return;
9247 }
9248
9249 p = frag_more (i.opcode_length + size);
9250 switch (i.opcode_length)
9251 {
9252 case 2:
9253 *p++ = i.tm.base_opcode >> 8;
9254 /* Fall through. */
9255 case 1:
9256 *p++ = i.tm.base_opcode;
9257 break;
9258 default:
9259 abort ();
9260 }
9261
9262 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9263 if (flag_code == CODE_64BIT && size == 4
9264 && jump_reloc == NO_RELOC && i.op[0].disps->X_add_number == 0
9265 && need_plt32_p (i.op[0].disps->X_add_symbol))
9266 jump_reloc = BFD_RELOC_X86_64_PLT32;
9267 #endif
9268
9269 jump_reloc = reloc (size, 1, 1, jump_reloc);
9270
9271 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
9272 i.op[0].disps, 1, jump_reloc);
9273
9274 /* All jumps handled here are signed, but don't unconditionally use a
9275 signed limit check for 32 and 16 bit jumps as we want to allow wrap
9276 around at 4G (outside of 64-bit mode) and 64k (except for XBEGIN)
9277 respectively. */
9278 switch (size)
9279 {
9280 case 1:
9281 fixP->fx_signed = 1;
9282 break;
9283
9284 case 2:
9285 if (i.tm.mnem_off == MN_xbegin)
9286 fixP->fx_signed = 1;
9287 break;
9288
9289 case 4:
9290 if (flag_code == CODE_64BIT)
9291 fixP->fx_signed = 1;
9292 break;
9293 }
9294 }
9295
9296 static void
9297 output_interseg_jump (void)
9298 {
9299 char *p;
9300 int size;
9301 int prefix;
9302 int code16;
9303
9304 code16 = 0;
9305 if (flag_code == CODE_16BIT)
9306 code16 = CODE16;
9307
9308 prefix = 0;
9309 if (i.prefix[DATA_PREFIX] != 0)
9310 {
9311 prefix = 1;
9312 i.prefixes -= 1;
9313 code16 ^= CODE16;
9314 }
9315
9316 gas_assert (!i.prefix[REX_PREFIX]);
9317
9318 size = 4;
9319 if (code16)
9320 size = 2;
9321
9322 if (i.prefixes != 0)
9323 as_warn (_("skipping prefixes on `%s'"), insn_name (&i.tm));
9324
9325 if (now_seg == absolute_section)
9326 {
9327 abs_section_offset += prefix + 1 + 2 + size;
9328 return;
9329 }
9330
9331 /* 1 opcode; 2 segment; offset */
9332 p = frag_more (prefix + 1 + 2 + size);
9333
9334 if (i.prefix[DATA_PREFIX] != 0)
9335 *p++ = DATA_PREFIX_OPCODE;
9336
9337 if (i.prefix[REX_PREFIX] != 0)
9338 *p++ = i.prefix[REX_PREFIX];
9339
9340 *p++ = i.tm.base_opcode;
9341 if (i.op[1].imms->X_op == O_constant)
9342 {
9343 offsetT n = i.op[1].imms->X_add_number;
9344
9345 if (size == 2
9346 && !fits_in_unsigned_word (n)
9347 && !fits_in_signed_word (n))
9348 {
9349 as_bad (_("16-bit jump out of range"));
9350 return;
9351 }
9352 md_number_to_chars (p, n, size);
9353 }
9354 else
9355 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
9356 i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
9357
9358 p += size;
9359 if (i.op[0].imms->X_op == O_constant)
9360 md_number_to_chars (p, (valueT) i.op[0].imms->X_add_number, 2);
9361 else
9362 fix_new_exp (frag_now, p - frag_now->fr_literal, 2,
9363 i.op[0].imms, 0, reloc (2, 0, 0, i.reloc[0]));
9364 }
9365
9366 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9367 void
9368 x86_cleanup (void)
9369 {
9370 char *p;
9371 asection *seg = now_seg;
9372 subsegT subseg = now_subseg;
9373 asection *sec;
9374 unsigned int alignment, align_size_1;
9375 unsigned int isa_1_descsz, feature_2_descsz, descsz;
9376 unsigned int isa_1_descsz_raw, feature_2_descsz_raw;
9377 unsigned int padding;
9378
9379 if (!IS_ELF || !x86_used_note)
9380 return;
9381
9382 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X86;
9383
9384 /* The .note.gnu.property section layout:
9385
9386 Field Length Contents
9387 ---- ---- ----
9388 n_namsz 4 4
9389 n_descsz 4 The note descriptor size
9390 n_type 4 NT_GNU_PROPERTY_TYPE_0
9391 n_name 4 "GNU"
9392 n_desc n_descsz The program property array
9393 .... .... ....
9394 */
9395
9396 /* Create the .note.gnu.property section. */
9397 sec = subseg_new (NOTE_GNU_PROPERTY_SECTION_NAME, 0);
9398 bfd_set_section_flags (sec,
9399 (SEC_ALLOC
9400 | SEC_LOAD
9401 | SEC_DATA
9402 | SEC_HAS_CONTENTS
9403 | SEC_READONLY));
9404
9405 if (get_elf_backend_data (stdoutput)->s->elfclass == ELFCLASS64)
9406 {
9407 align_size_1 = 7;
9408 alignment = 3;
9409 }
9410 else
9411 {
9412 align_size_1 = 3;
9413 alignment = 2;
9414 }
9415
9416 bfd_set_section_alignment (sec, alignment);
9417 elf_section_type (sec) = SHT_NOTE;
9418
9419 /* GNU_PROPERTY_X86_ISA_1_USED: 4-byte type + 4-byte data size
9420 + 4-byte data */
9421 isa_1_descsz_raw = 4 + 4 + 4;
9422 /* Align GNU_PROPERTY_X86_ISA_1_USED. */
9423 isa_1_descsz = (isa_1_descsz_raw + align_size_1) & ~align_size_1;
9424
9425 feature_2_descsz_raw = isa_1_descsz;
9426 /* GNU_PROPERTY_X86_FEATURE_2_USED: 4-byte type + 4-byte data size
9427 + 4-byte data */
9428 feature_2_descsz_raw += 4 + 4 + 4;
9429 /* Align GNU_PROPERTY_X86_FEATURE_2_USED. */
9430 feature_2_descsz = ((feature_2_descsz_raw + align_size_1)
9431 & ~align_size_1);
9432
9433 descsz = feature_2_descsz;
9434 /* Section size: n_namsz + n_descsz + n_type + n_name + n_descsz. */
9435 p = frag_more (4 + 4 + 4 + 4 + descsz);
9436
9437 /* Write n_namsz. */
9438 md_number_to_chars (p, (valueT) 4, 4);
9439
9440 /* Write n_descsz. */
9441 md_number_to_chars (p + 4, (valueT) descsz, 4);
9442
9443 /* Write n_type. */
9444 md_number_to_chars (p + 4 * 2, (valueT) NT_GNU_PROPERTY_TYPE_0, 4);
9445
9446 /* Write n_name. */
9447 memcpy (p + 4 * 3, "GNU", 4);
9448
9449 /* Write 4-byte type. */
9450 md_number_to_chars (p + 4 * 4,
9451 (valueT) GNU_PROPERTY_X86_ISA_1_USED, 4);
9452
9453 /* Write 4-byte data size. */
9454 md_number_to_chars (p + 4 * 5, (valueT) 4, 4);
9455
9456 /* Write 4-byte data. */
9457 md_number_to_chars (p + 4 * 6, (valueT) x86_isa_1_used, 4);
9458
9459 /* Zero out paddings. */
9460 padding = isa_1_descsz - isa_1_descsz_raw;
9461 if (padding)
9462 memset (p + 4 * 7, 0, padding);
9463
9464 /* Write 4-byte type. */
9465 md_number_to_chars (p + isa_1_descsz + 4 * 4,
9466 (valueT) GNU_PROPERTY_X86_FEATURE_2_USED, 4);
9467
9468 /* Write 4-byte data size. */
9469 md_number_to_chars (p + isa_1_descsz + 4 * 5, (valueT) 4, 4);
9470
9471 /* Write 4-byte data. */
9472 md_number_to_chars (p + isa_1_descsz + 4 * 6,
9473 (valueT) x86_feature_2_used, 4);
9474
9475 /* Zero out paddings. */
9476 padding = feature_2_descsz - feature_2_descsz_raw;
9477 if (padding)
9478 memset (p + isa_1_descsz + 4 * 7, 0, padding);
9479
9480 /* We probably can't restore the current segment, for there likely
9481 isn't one yet... */
9482 if (seg && subseg)
9483 subseg_set (seg, subseg);
9484 }
9485
9486 bool
9487 x86_support_sframe_p (void)
9488 {
9489 /* At this time, SFrame stack trace is supported for AMD64 ABI only. */
9490 return (x86_elf_abi == X86_64_ABI);
9491 }
9492
9493 bool
9494 x86_sframe_ra_tracking_p (void)
9495 {
9496 /* In AMD64, return address is always stored on the stack at a fixed offset
9497 from the CFA (provided via x86_sframe_cfa_ra_offset ()).
9498 Do not track explicitly via an SFrame Frame Row Entry. */
9499 return false;
9500 }
9501
9502 offsetT
9503 x86_sframe_cfa_ra_offset (void)
9504 {
9505 gas_assert (x86_elf_abi == X86_64_ABI);
9506 return (offsetT) -8;
9507 }
9508
9509 unsigned char
9510 x86_sframe_get_abi_arch (void)
9511 {
9512 unsigned char sframe_abi_arch = 0;
9513
9514 if (x86_support_sframe_p ())
9515 {
9516 gas_assert (!target_big_endian);
9517 sframe_abi_arch = SFRAME_ABI_AMD64_ENDIAN_LITTLE;
9518 }
9519
9520 return sframe_abi_arch;
9521 }
9522
9523 #endif
9524
9525 static unsigned int
9526 encoding_length (const fragS *start_frag, offsetT start_off,
9527 const char *frag_now_ptr)
9528 {
9529 unsigned int len = 0;
9530
9531 if (start_frag != frag_now)
9532 {
9533 const fragS *fr = start_frag;
9534
9535 do {
9536 len += fr->fr_fix;
9537 fr = fr->fr_next;
9538 } while (fr && fr != frag_now);
9539 }
9540
9541 return len - start_off + (frag_now_ptr - frag_now->fr_literal);
9542 }
9543
9544 /* Return 1 for test, and, cmp, add, sub, inc and dec which may
9545 be macro-fused with conditional jumps.
9546 NB: If TEST/AND/CMP/ADD/SUB/INC/DEC is of RIP relative address,
9547 or is one of the following format:
9548
9549 cmp m, imm
9550 add m, imm
9551 sub m, imm
9552 test m, imm
9553 and m, imm
9554 inc m
9555 dec m
9556
9557 it is unfusible. */
9558
9559 static int
9560 maybe_fused_with_jcc_p (enum mf_cmp_kind* mf_cmp_p)
9561 {
9562 /* No RIP address. */
9563 if (i.base_reg && i.base_reg->reg_num == RegIP)
9564 return 0;
9565
9566 /* No opcodes outside of base encoding space. */
9567 if (i.tm.opcode_space != SPACE_BASE)
9568 return 0;
9569
9570 /* add, sub without add/sub m, imm. */
9571 if (i.tm.base_opcode <= 5
9572 || (i.tm.base_opcode >= 0x28 && i.tm.base_opcode <= 0x2d)
9573 || ((i.tm.base_opcode | 3) == 0x83
9574 && (i.tm.extension_opcode == 0x5
9575 || i.tm.extension_opcode == 0x0)))
9576 {
9577 *mf_cmp_p = mf_cmp_alu_cmp;
9578 return !(i.mem_operands && i.imm_operands);
9579 }
9580
9581 /* and without and m, imm. */
9582 if ((i.tm.base_opcode >= 0x20 && i.tm.base_opcode <= 0x25)
9583 || ((i.tm.base_opcode | 3) == 0x83
9584 && i.tm.extension_opcode == 0x4))
9585 {
9586 *mf_cmp_p = mf_cmp_test_and;
9587 return !(i.mem_operands && i.imm_operands);
9588 }
9589
9590 /* test without test m imm. */
9591 if ((i.tm.base_opcode | 1) == 0x85
9592 || (i.tm.base_opcode | 1) == 0xa9
9593 || ((i.tm.base_opcode | 1) == 0xf7
9594 && i.tm.extension_opcode == 0))
9595 {
9596 *mf_cmp_p = mf_cmp_test_and;
9597 return !(i.mem_operands && i.imm_operands);
9598 }
9599
9600 /* cmp without cmp m, imm. */
9601 if ((i.tm.base_opcode >= 0x38 && i.tm.base_opcode <= 0x3d)
9602 || ((i.tm.base_opcode | 3) == 0x83
9603 && (i.tm.extension_opcode == 0x7)))
9604 {
9605 *mf_cmp_p = mf_cmp_alu_cmp;
9606 return !(i.mem_operands && i.imm_operands);
9607 }
9608
9609 /* inc, dec without inc/dec m. */
9610 if ((is_cpu (&i.tm, CpuNo64)
9611 && (i.tm.base_opcode | 0xf) == 0x4f)
9612 || ((i.tm.base_opcode | 1) == 0xff
9613 && i.tm.extension_opcode <= 0x1))
9614 {
9615 *mf_cmp_p = mf_cmp_incdec;
9616 return !i.mem_operands;
9617 }
9618
9619 return 0;
9620 }
9621
9622 /* Return 1 if a FUSED_JCC_PADDING frag should be generated. */
9623
9624 static int
9625 add_fused_jcc_padding_frag_p (enum mf_cmp_kind* mf_cmp_p)
9626 {
9627 /* NB: Don't work with COND_JUMP86 without i386. */
9628 if (!align_branch_power
9629 || now_seg == absolute_section
9630 || !cpu_arch_flags.bitfield.cpui386
9631 || !(align_branch & align_branch_fused_bit))
9632 return 0;
9633
9634 if (maybe_fused_with_jcc_p (mf_cmp_p))
9635 {
9636 if (last_insn.kind == last_insn_other
9637 || last_insn.seg != now_seg)
9638 return 1;
9639 if (flag_debug)
9640 as_warn_where (last_insn.file, last_insn.line,
9641 _("`%s` skips -malign-branch-boundary on `%s`"),
9642 last_insn.name, insn_name (&i.tm));
9643 }
9644
9645 return 0;
9646 }
9647
9648 /* Return 1 if a BRANCH_PREFIX frag should be generated. */
9649
9650 static int
9651 add_branch_prefix_frag_p (void)
9652 {
9653 /* NB: Don't work with COND_JUMP86 without i386. Don't add prefix
9654 to PadLock instructions since they include prefixes in opcode. */
9655 if (!align_branch_power
9656 || !align_branch_prefix_size
9657 || now_seg == absolute_section
9658 || is_cpu (&i.tm, CpuPadLock)
9659 || !cpu_arch_flags.bitfield.cpui386)
9660 return 0;
9661
9662 /* Don't add prefix if it is a prefix or there is no operand in case
9663 that segment prefix is special. */
9664 if (!i.operands || i.tm.opcode_modifier.isprefix)
9665 return 0;
9666
9667 if (last_insn.kind == last_insn_other
9668 || last_insn.seg != now_seg)
9669 return 1;
9670
9671 if (flag_debug)
9672 as_warn_where (last_insn.file, last_insn.line,
9673 _("`%s` skips -malign-branch-boundary on `%s`"),
9674 last_insn.name, insn_name (&i.tm));
9675
9676 return 0;
9677 }
9678
9679 /* Return 1 if a BRANCH_PADDING frag should be generated. */
9680
9681 static int
9682 add_branch_padding_frag_p (enum align_branch_kind *branch_p,
9683 enum mf_jcc_kind *mf_jcc_p)
9684 {
9685 int add_padding;
9686
9687 /* NB: Don't work with COND_JUMP86 without i386. */
9688 if (!align_branch_power
9689 || now_seg == absolute_section
9690 || !cpu_arch_flags.bitfield.cpui386
9691 || i.tm.opcode_space != SPACE_BASE)
9692 return 0;
9693
9694 add_padding = 0;
9695
9696 /* Check for jcc and direct jmp. */
9697 if (i.tm.opcode_modifier.jump == JUMP)
9698 {
9699 if (i.tm.base_opcode == JUMP_PC_RELATIVE)
9700 {
9701 *branch_p = align_branch_jmp;
9702 add_padding = align_branch & align_branch_jmp_bit;
9703 }
9704 else
9705 {
9706 /* Because J<cc> and JN<cc> share same group in macro-fusible table,
9707 igore the lowest bit. */
9708 *mf_jcc_p = (i.tm.base_opcode & 0x0e) >> 1;
9709 *branch_p = align_branch_jcc;
9710 if ((align_branch & align_branch_jcc_bit))
9711 add_padding = 1;
9712 }
9713 }
9714 else if ((i.tm.base_opcode | 1) == 0xc3)
9715 {
9716 /* Near ret. */
9717 *branch_p = align_branch_ret;
9718 if ((align_branch & align_branch_ret_bit))
9719 add_padding = 1;
9720 }
9721 else
9722 {
9723 /* Check for indirect jmp, direct and indirect calls. */
9724 if (i.tm.base_opcode == 0xe8)
9725 {
9726 /* Direct call. */
9727 *branch_p = align_branch_call;
9728 if ((align_branch & align_branch_call_bit))
9729 add_padding = 1;
9730 }
9731 else if (i.tm.base_opcode == 0xff
9732 && (i.tm.extension_opcode == 2
9733 || i.tm.extension_opcode == 4))
9734 {
9735 /* Indirect call and jmp. */
9736 *branch_p = align_branch_indirect;
9737 if ((align_branch & align_branch_indirect_bit))
9738 add_padding = 1;
9739 }
9740
9741 if (add_padding
9742 && i.disp_operands
9743 && tls_get_addr
9744 && (i.op[0].disps->X_op == O_symbol
9745 || (i.op[0].disps->X_op == O_subtract
9746 && i.op[0].disps->X_op_symbol == GOT_symbol)))
9747 {
9748 symbolS *s = i.op[0].disps->X_add_symbol;
9749 /* No padding to call to global or undefined tls_get_addr. */
9750 if ((S_IS_EXTERNAL (s) || !S_IS_DEFINED (s))
9751 && strcmp (S_GET_NAME (s), tls_get_addr) == 0)
9752 return 0;
9753 }
9754 }
9755
9756 if (add_padding
9757 && last_insn.kind != last_insn_other
9758 && last_insn.seg == now_seg)
9759 {
9760 if (flag_debug)
9761 as_warn_where (last_insn.file, last_insn.line,
9762 _("`%s` skips -malign-branch-boundary on `%s`"),
9763 last_insn.name, insn_name (&i.tm));
9764 return 0;
9765 }
9766
9767 return add_padding;
9768 }
9769
9770 static void
9771 output_insn (void)
9772 {
9773 fragS *insn_start_frag;
9774 offsetT insn_start_off;
9775 fragS *fragP = NULL;
9776 enum align_branch_kind branch = align_branch_none;
9777 /* The initializer is arbitrary just to avoid uninitialized error.
9778 it's actually either assigned in add_branch_padding_frag_p
9779 or never be used. */
9780 enum mf_jcc_kind mf_jcc = mf_jcc_jo;
9781
9782 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9783 if (IS_ELF && x86_used_note && now_seg != absolute_section)
9784 {
9785 if ((i.xstate & xstate_tmm) == xstate_tmm
9786 || is_cpu (&i.tm, CpuAMX_TILE))
9787 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_TMM;
9788
9789 if (is_cpu (&i.tm, Cpu8087)
9790 || is_cpu (&i.tm, Cpu287)
9791 || is_cpu (&i.tm, Cpu387)
9792 || is_cpu (&i.tm, Cpu687)
9793 || is_cpu (&i.tm, CpuFISTTP))
9794 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X87;
9795
9796 if ((i.xstate & xstate_mmx)
9797 || i.tm.mnem_off == MN_emms
9798 || i.tm.mnem_off == MN_femms)
9799 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MMX;
9800
9801 if (i.index_reg)
9802 {
9803 if (i.index_reg->reg_type.bitfield.zmmword)
9804 i.xstate |= xstate_zmm;
9805 else if (i.index_reg->reg_type.bitfield.ymmword)
9806 i.xstate |= xstate_ymm;
9807 else if (i.index_reg->reg_type.bitfield.xmmword)
9808 i.xstate |= xstate_xmm;
9809 }
9810
9811 /* vzeroall / vzeroupper */
9812 if (i.tm.base_opcode == 0x77 && is_cpu (&i.tm, CpuAVX))
9813 i.xstate |= xstate_ymm;
9814
9815 if ((i.xstate & xstate_xmm)
9816 /* ldmxcsr / stmxcsr / vldmxcsr / vstmxcsr */
9817 || (i.tm.base_opcode == 0xae
9818 && (is_cpu (&i.tm, CpuSSE)
9819 || is_cpu (&i.tm, CpuAVX)))
9820 || is_cpu (&i.tm, CpuWideKL)
9821 || is_cpu (&i.tm, CpuKL))
9822 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XMM;
9823
9824 if ((i.xstate & xstate_ymm) == xstate_ymm)
9825 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_YMM;
9826 if ((i.xstate & xstate_zmm) == xstate_zmm)
9827 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_ZMM;
9828 if (i.mask.reg || (i.xstate & xstate_mask) == xstate_mask)
9829 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MASK;
9830 if (is_cpu (&i.tm, CpuFXSR))
9831 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_FXSR;
9832 if (is_cpu (&i.tm, CpuXsave))
9833 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVE;
9834 if (is_cpu (&i.tm, CpuXsaveopt))
9835 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT;
9836 if (is_cpu (&i.tm, CpuXSAVEC))
9837 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEC;
9838
9839 if (x86_feature_2_used
9840 || is_cpu (&i.tm, CpuCMOV)
9841 || is_cpu (&i.tm, CpuSYSCALL)
9842 || i.tm.mnem_off == MN_cmpxchg8b)
9843 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_BASELINE;
9844 if (is_cpu (&i.tm, CpuSSE3)
9845 || is_cpu (&i.tm, CpuSSSE3)
9846 || is_cpu (&i.tm, CpuSSE4_1)
9847 || is_cpu (&i.tm, CpuSSE4_2)
9848 || is_cpu (&i.tm, CpuCX16)
9849 || is_cpu (&i.tm, CpuPOPCNT)
9850 /* LAHF-SAHF insns in 64-bit mode. */
9851 || (flag_code == CODE_64BIT
9852 && (i.tm.base_opcode | 1) == 0x9f
9853 && i.tm.opcode_space == SPACE_BASE))
9854 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V2;
9855 if (is_cpu (&i.tm, CpuAVX)
9856 || is_cpu (&i.tm, CpuAVX2)
9857 /* Any VEX encoded insns execpt for AVX512F, AVX512BW, AVX512DQ,
9858 XOP, FMA4, LPW, TBM, and AMX. */
9859 || (i.tm.opcode_modifier.vex
9860 && !is_cpu (&i.tm, CpuAVX512F)
9861 && !is_cpu (&i.tm, CpuAVX512BW)
9862 && !is_cpu (&i.tm, CpuAVX512DQ)
9863 && !is_cpu (&i.tm, CpuXOP)
9864 && !is_cpu (&i.tm, CpuFMA4)
9865 && !is_cpu (&i.tm, CpuLWP)
9866 && !is_cpu (&i.tm, CpuTBM)
9867 && !(x86_feature_2_used & GNU_PROPERTY_X86_FEATURE_2_TMM))
9868 || is_cpu (&i.tm, CpuF16C)
9869 || is_cpu (&i.tm, CpuFMA)
9870 || is_cpu (&i.tm, CpuLZCNT)
9871 || is_cpu (&i.tm, CpuMovbe)
9872 || is_cpu (&i.tm, CpuXSAVES)
9873 || (x86_feature_2_used
9874 & (GNU_PROPERTY_X86_FEATURE_2_XSAVE
9875 | GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT
9876 | GNU_PROPERTY_X86_FEATURE_2_XSAVEC)) != 0)
9877 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V3;
9878 if (is_cpu (&i.tm, CpuAVX512F)
9879 || is_cpu (&i.tm, CpuAVX512BW)
9880 || is_cpu (&i.tm, CpuAVX512DQ)
9881 || is_cpu (&i.tm, CpuAVX512VL)
9882 /* Any EVEX encoded insns except for AVX512ER, AVX512PF,
9883 AVX512-4FMAPS, and AVX512-4VNNIW. */
9884 || (i.tm.opcode_modifier.evex
9885 && !is_cpu (&i.tm, CpuAVX512ER)
9886 && !is_cpu (&i.tm, CpuAVX512PF)
9887 && !is_cpu (&i.tm, CpuAVX512_4FMAPS)
9888 && !is_cpu (&i.tm, CpuAVX512_4VNNIW)))
9889 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V4;
9890 }
9891 #endif
9892
9893 /* Tie dwarf2 debug info to the address at the start of the insn.
9894 We can't do this after the insn has been output as the current
9895 frag may have been closed off. eg. by frag_var. */
9896 dwarf2_emit_insn (0);
9897
9898 insn_start_frag = frag_now;
9899 insn_start_off = frag_now_fix ();
9900
9901 if (add_branch_padding_frag_p (&branch, &mf_jcc))
9902 {
9903 char *p;
9904 /* Branch can be 8 bytes. Leave some room for prefixes. */
9905 unsigned int max_branch_padding_size = 14;
9906
9907 /* Align section to boundary. */
9908 record_alignment (now_seg, align_branch_power);
9909
9910 /* Make room for padding. */
9911 frag_grow (max_branch_padding_size);
9912
9913 /* Start of the padding. */
9914 p = frag_more (0);
9915
9916 fragP = frag_now;
9917
9918 frag_var (rs_machine_dependent, max_branch_padding_size, 0,
9919 ENCODE_RELAX_STATE (BRANCH_PADDING, 0),
9920 NULL, 0, p);
9921
9922 fragP->tc_frag_data.mf_type = mf_jcc;
9923 fragP->tc_frag_data.branch_type = branch;
9924 fragP->tc_frag_data.max_bytes = max_branch_padding_size;
9925 }
9926
9927 if (!cpu_arch_flags.bitfield.cpui386 && (flag_code != CODE_16BIT)
9928 && !pre_386_16bit_warned)
9929 {
9930 as_warn (_("use .code16 to ensure correct addressing mode"));
9931 pre_386_16bit_warned = true;
9932 }
9933
9934 /* Output jumps. */
9935 if (i.tm.opcode_modifier.jump == JUMP)
9936 output_branch ();
9937 else if (i.tm.opcode_modifier.jump == JUMP_BYTE
9938 || i.tm.opcode_modifier.jump == JUMP_DWORD)
9939 output_jump ();
9940 else if (i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT)
9941 output_interseg_jump ();
9942 else
9943 {
9944 /* Output normal instructions here. */
9945 char *p;
9946 unsigned char *q;
9947 unsigned int j;
9948 enum mf_cmp_kind mf_cmp;
9949
9950 if (avoid_fence
9951 && (i.tm.base_opcode == 0xaee8
9952 || i.tm.base_opcode == 0xaef0
9953 || i.tm.base_opcode == 0xaef8))
9954 {
9955 /* Encode lfence, mfence, and sfence as
9956 f0 83 04 24 00 lock addl $0x0, (%{re}sp). */
9957 if (flag_code == CODE_16BIT)
9958 as_bad (_("Cannot convert `%s' in 16-bit mode"), insn_name (&i.tm));
9959 else if (omit_lock_prefix)
9960 as_bad (_("Cannot convert `%s' with `-momit-lock-prefix=yes' in effect"),
9961 insn_name (&i.tm));
9962 else if (now_seg != absolute_section)
9963 {
9964 offsetT val = 0x240483f0ULL;
9965
9966 p = frag_more (5);
9967 md_number_to_chars (p, val, 5);
9968 }
9969 else
9970 abs_section_offset += 5;
9971 return;
9972 }
9973
9974 /* Some processors fail on LOCK prefix. This options makes
9975 assembler ignore LOCK prefix and serves as a workaround. */
9976 if (omit_lock_prefix)
9977 {
9978 if (i.tm.base_opcode == LOCK_PREFIX_OPCODE
9979 && i.tm.opcode_modifier.isprefix)
9980 return;
9981 i.prefix[LOCK_PREFIX] = 0;
9982 }
9983
9984 if (branch)
9985 /* Skip if this is a branch. */
9986 ;
9987 else if (add_fused_jcc_padding_frag_p (&mf_cmp))
9988 {
9989 /* Make room for padding. */
9990 frag_grow (MAX_FUSED_JCC_PADDING_SIZE);
9991 p = frag_more (0);
9992
9993 fragP = frag_now;
9994
9995 frag_var (rs_machine_dependent, MAX_FUSED_JCC_PADDING_SIZE, 0,
9996 ENCODE_RELAX_STATE (FUSED_JCC_PADDING, 0),
9997 NULL, 0, p);
9998
9999 fragP->tc_frag_data.mf_type = mf_cmp;
10000 fragP->tc_frag_data.branch_type = align_branch_fused;
10001 fragP->tc_frag_data.max_bytes = MAX_FUSED_JCC_PADDING_SIZE;
10002 }
10003 else if (add_branch_prefix_frag_p ())
10004 {
10005 unsigned int max_prefix_size = align_branch_prefix_size;
10006
10007 /* Make room for padding. */
10008 frag_grow (max_prefix_size);
10009 p = frag_more (0);
10010
10011 fragP = frag_now;
10012
10013 frag_var (rs_machine_dependent, max_prefix_size, 0,
10014 ENCODE_RELAX_STATE (BRANCH_PREFIX, 0),
10015 NULL, 0, p);
10016
10017 fragP->tc_frag_data.max_bytes = max_prefix_size;
10018 }
10019
10020 /* Since the VEX/EVEX prefix contains the implicit prefix, we
10021 don't need the explicit prefix. */
10022 if (!is_any_vex_encoding (&i.tm))
10023 {
10024 switch (i.tm.opcode_modifier.opcodeprefix)
10025 {
10026 case PREFIX_0X66:
10027 add_prefix (0x66);
10028 break;
10029 case PREFIX_0XF2:
10030 add_prefix (0xf2);
10031 break;
10032 case PREFIX_0XF3:
10033 if (!is_cpu (&i.tm, CpuPadLock)
10034 || (i.prefix[REP_PREFIX] != 0xf3))
10035 add_prefix (0xf3);
10036 break;
10037 case PREFIX_NONE:
10038 switch (i.opcode_length)
10039 {
10040 case 2:
10041 break;
10042 case 1:
10043 /* Check for pseudo prefixes. */
10044 if (!i.tm.opcode_modifier.isprefix || i.tm.base_opcode)
10045 break;
10046 as_bad_where (insn_start_frag->fr_file,
10047 insn_start_frag->fr_line,
10048 _("pseudo prefix without instruction"));
10049 return;
10050 default:
10051 abort ();
10052 }
10053 break;
10054 default:
10055 abort ();
10056 }
10057
10058 #if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
10059 /* For x32, add a dummy REX_OPCODE prefix for mov/add with
10060 R_X86_64_GOTTPOFF relocation so that linker can safely
10061 perform IE->LE optimization. A dummy REX_OPCODE prefix
10062 is also needed for lea with R_X86_64_GOTPC32_TLSDESC
10063 relocation for GDesc -> IE/LE optimization. */
10064 if (x86_elf_abi == X86_64_X32_ABI
10065 && i.operands == 2
10066 && (i.reloc[0] == BFD_RELOC_X86_64_GOTTPOFF
10067 || i.reloc[0] == BFD_RELOC_X86_64_GOTPC32_TLSDESC)
10068 && i.prefix[REX_PREFIX] == 0)
10069 add_prefix (REX_OPCODE);
10070 #endif
10071
10072 /* The prefix bytes. */
10073 for (j = ARRAY_SIZE (i.prefix), q = i.prefix; j > 0; j--, q++)
10074 if (*q)
10075 frag_opcode_byte (*q);
10076 }
10077 else
10078 {
10079 for (j = 0, q = i.prefix; j < ARRAY_SIZE (i.prefix); j++, q++)
10080 if (*q)
10081 switch (j)
10082 {
10083 case SEG_PREFIX:
10084 case ADDR_PREFIX:
10085 frag_opcode_byte (*q);
10086 break;
10087 default:
10088 /* There should be no other prefixes for instructions
10089 with VEX prefix. */
10090 abort ();
10091 }
10092
10093 /* For EVEX instructions i.vrex should become 0 after
10094 build_evex_prefix. For VEX instructions upper 16 registers
10095 aren't available, so VREX should be 0. */
10096 if (i.vrex)
10097 abort ();
10098 /* Now the VEX prefix. */
10099 if (now_seg != absolute_section)
10100 {
10101 p = frag_more (i.vex.length);
10102 for (j = 0; j < i.vex.length; j++)
10103 p[j] = i.vex.bytes[j];
10104 }
10105 else
10106 abs_section_offset += i.vex.length;
10107 }
10108
10109 /* Now the opcode; be careful about word order here! */
10110 j = i.opcode_length;
10111 if (!i.vex.length)
10112 switch (i.tm.opcode_space)
10113 {
10114 case SPACE_BASE:
10115 break;
10116 case SPACE_0F:
10117 ++j;
10118 break;
10119 case SPACE_0F38:
10120 case SPACE_0F3A:
10121 j += 2;
10122 break;
10123 default:
10124 abort ();
10125 }
10126
10127 if (now_seg == absolute_section)
10128 abs_section_offset += j;
10129 else if (j == 1)
10130 {
10131 FRAG_APPEND_1_CHAR (i.tm.base_opcode);
10132 }
10133 else
10134 {
10135 p = frag_more (j);
10136 if (!i.vex.length
10137 && i.tm.opcode_space != SPACE_BASE)
10138 {
10139 *p++ = 0x0f;
10140 if (i.tm.opcode_space != SPACE_0F)
10141 *p++ = i.tm.opcode_space == SPACE_0F38
10142 ? 0x38 : 0x3a;
10143 }
10144
10145 switch (i.opcode_length)
10146 {
10147 case 2:
10148 /* Put out high byte first: can't use md_number_to_chars! */
10149 *p++ = (i.tm.base_opcode >> 8) & 0xff;
10150 /* Fall through. */
10151 case 1:
10152 *p = i.tm.base_opcode & 0xff;
10153 break;
10154 default:
10155 abort ();
10156 break;
10157 }
10158
10159 }
10160
10161 /* Now the modrm byte and sib byte (if present). */
10162 if (i.tm.opcode_modifier.modrm)
10163 {
10164 frag_opcode_byte ((i.rm.regmem << 0)
10165 | (i.rm.reg << 3)
10166 | (i.rm.mode << 6));
10167 /* If i.rm.regmem == ESP (4)
10168 && i.rm.mode != (Register mode)
10169 && not 16 bit
10170 ==> need second modrm byte. */
10171 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
10172 && i.rm.mode != 3
10173 && !(i.base_reg && i.base_reg->reg_type.bitfield.word))
10174 frag_opcode_byte ((i.sib.base << 0)
10175 | (i.sib.index << 3)
10176 | (i.sib.scale << 6));
10177 }
10178
10179 if (i.disp_operands)
10180 output_disp (insn_start_frag, insn_start_off);
10181
10182 if (i.imm_operands)
10183 output_imm (insn_start_frag, insn_start_off);
10184
10185 /*
10186 * frag_now_fix () returning plain abs_section_offset when we're in the
10187 * absolute section, and abs_section_offset not getting updated as data
10188 * gets added to the frag breaks the logic below.
10189 */
10190 if (now_seg != absolute_section)
10191 {
10192 j = encoding_length (insn_start_frag, insn_start_off, frag_more (0));
10193 if (j > 15)
10194 as_warn (_("instruction length of %u bytes exceeds the limit of 15"),
10195 j);
10196 else if (fragP)
10197 {
10198 /* NB: Don't add prefix with GOTPC relocation since
10199 output_disp() above depends on the fixed encoding
10200 length. Can't add prefix with TLS relocation since
10201 it breaks TLS linker optimization. */
10202 unsigned int max = i.has_gotpc_tls_reloc ? 0 : 15 - j;
10203 /* Prefix count on the current instruction. */
10204 unsigned int count = i.vex.length;
10205 unsigned int k;
10206 for (k = 0; k < ARRAY_SIZE (i.prefix); k++)
10207 /* REX byte is encoded in VEX/EVEX prefix. */
10208 if (i.prefix[k] && (k != REX_PREFIX || !i.vex.length))
10209 count++;
10210
10211 /* Count prefixes for extended opcode maps. */
10212 if (!i.vex.length)
10213 switch (i.tm.opcode_space)
10214 {
10215 case SPACE_BASE:
10216 break;
10217 case SPACE_0F:
10218 count++;
10219 break;
10220 case SPACE_0F38:
10221 case SPACE_0F3A:
10222 count += 2;
10223 break;
10224 default:
10225 abort ();
10226 }
10227
10228 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
10229 == BRANCH_PREFIX)
10230 {
10231 /* Set the maximum prefix size in BRANCH_PREFIX
10232 frag. */
10233 if (fragP->tc_frag_data.max_bytes > max)
10234 fragP->tc_frag_data.max_bytes = max;
10235 if (fragP->tc_frag_data.max_bytes > count)
10236 fragP->tc_frag_data.max_bytes -= count;
10237 else
10238 fragP->tc_frag_data.max_bytes = 0;
10239 }
10240 else
10241 {
10242 /* Remember the maximum prefix size in FUSED_JCC_PADDING
10243 frag. */
10244 unsigned int max_prefix_size;
10245 if (align_branch_prefix_size > max)
10246 max_prefix_size = max;
10247 else
10248 max_prefix_size = align_branch_prefix_size;
10249 if (max_prefix_size > count)
10250 fragP->tc_frag_data.max_prefix_length
10251 = max_prefix_size - count;
10252 }
10253
10254 /* Use existing segment prefix if possible. Use CS
10255 segment prefix in 64-bit mode. In 32-bit mode, use SS
10256 segment prefix with ESP/EBP base register and use DS
10257 segment prefix without ESP/EBP base register. */
10258 if (i.prefix[SEG_PREFIX])
10259 fragP->tc_frag_data.default_prefix = i.prefix[SEG_PREFIX];
10260 else if (flag_code == CODE_64BIT)
10261 fragP->tc_frag_data.default_prefix = CS_PREFIX_OPCODE;
10262 else if (i.base_reg
10263 && (i.base_reg->reg_num == 4
10264 || i.base_reg->reg_num == 5))
10265 fragP->tc_frag_data.default_prefix = SS_PREFIX_OPCODE;
10266 else
10267 fragP->tc_frag_data.default_prefix = DS_PREFIX_OPCODE;
10268 }
10269 }
10270 }
10271
10272 /* NB: Don't work with COND_JUMP86 without i386. */
10273 if (align_branch_power
10274 && now_seg != absolute_section
10275 && cpu_arch_flags.bitfield.cpui386)
10276 {
10277 /* Terminate each frag so that we can add prefix and check for
10278 fused jcc. */
10279 frag_wane (frag_now);
10280 frag_new (0);
10281 }
10282
10283 #ifdef DEBUG386
10284 if (flag_debug)
10285 {
10286 pi ("" /*line*/, &i);
10287 }
10288 #endif /* DEBUG386 */
10289 }
10290
10291 /* Return the size of the displacement operand N. */
10292
10293 static int
10294 disp_size (unsigned int n)
10295 {
10296 int size = 4;
10297
10298 if (i.types[n].bitfield.disp64)
10299 size = 8;
10300 else if (i.types[n].bitfield.disp8)
10301 size = 1;
10302 else if (i.types[n].bitfield.disp16)
10303 size = 2;
10304 return size;
10305 }
10306
10307 /* Return the size of the immediate operand N. */
10308
10309 static int
10310 imm_size (unsigned int n)
10311 {
10312 int size = 4;
10313 if (i.types[n].bitfield.imm64)
10314 size = 8;
10315 else if (i.types[n].bitfield.imm8 || i.types[n].bitfield.imm8s)
10316 size = 1;
10317 else if (i.types[n].bitfield.imm16)
10318 size = 2;
10319 return size;
10320 }
10321
10322 static void
10323 output_disp (fragS *insn_start_frag, offsetT insn_start_off)
10324 {
10325 char *p;
10326 unsigned int n;
10327
10328 for (n = 0; n < i.operands; n++)
10329 {
10330 if (operand_type_check (i.types[n], disp))
10331 {
10332 int size = disp_size (n);
10333
10334 if (now_seg == absolute_section)
10335 abs_section_offset += size;
10336 else if (i.op[n].disps->X_op == O_constant)
10337 {
10338 offsetT val = i.op[n].disps->X_add_number;
10339
10340 val = offset_in_range (val >> (size == 1 ? i.memshift : 0),
10341 size);
10342 p = frag_more (size);
10343 md_number_to_chars (p, val, size);
10344 }
10345 else
10346 {
10347 enum bfd_reloc_code_real reloc_type;
10348 bool pcrel = (i.flags[n] & Operand_PCrel) != 0;
10349 bool sign = (flag_code == CODE_64BIT && size == 4
10350 && (!want_disp32 (&i.tm)
10351 || (i.tm.opcode_modifier.jump && !i.jumpabsolute
10352 && !i.types[n].bitfield.baseindex)))
10353 || pcrel;
10354 fixS *fixP;
10355
10356 /* We can't have 8 bit displacement here. */
10357 gas_assert (!i.types[n].bitfield.disp8);
10358
10359 /* The PC relative address is computed relative
10360 to the instruction boundary, so in case immediate
10361 fields follows, we need to adjust the value. */
10362 if (pcrel && i.imm_operands)
10363 {
10364 unsigned int n1;
10365 int sz = 0;
10366
10367 for (n1 = 0; n1 < i.operands; n1++)
10368 if (operand_type_check (i.types[n1], imm))
10369 {
10370 /* Only one immediate is allowed for PC
10371 relative address, except with .insn. */
10372 gas_assert (sz == 0 || dot_insn ());
10373 sz += imm_size (n1);
10374 }
10375 /* We should find at least one immediate. */
10376 gas_assert (sz != 0);
10377 i.op[n].disps->X_add_number -= sz;
10378 }
10379
10380 p = frag_more (size);
10381 reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
10382 if (GOT_symbol
10383 && GOT_symbol == i.op[n].disps->X_add_symbol
10384 && (((reloc_type == BFD_RELOC_32
10385 || reloc_type == BFD_RELOC_X86_64_32S
10386 || (reloc_type == BFD_RELOC_64
10387 && object_64bit))
10388 && (i.op[n].disps->X_op == O_symbol
10389 || (i.op[n].disps->X_op == O_add
10390 && ((symbol_get_value_expression
10391 (i.op[n].disps->X_op_symbol)->X_op)
10392 == O_subtract))))
10393 || reloc_type == BFD_RELOC_32_PCREL))
10394 {
10395 if (!object_64bit)
10396 {
10397 reloc_type = BFD_RELOC_386_GOTPC;
10398 i.has_gotpc_tls_reloc = true;
10399 i.op[n].disps->X_add_number +=
10400 encoding_length (insn_start_frag, insn_start_off, p);
10401 }
10402 else if (reloc_type == BFD_RELOC_64)
10403 reloc_type = BFD_RELOC_X86_64_GOTPC64;
10404 else
10405 /* Don't do the adjustment for x86-64, as there
10406 the pcrel addressing is relative to the _next_
10407 insn, and that is taken care of in other code. */
10408 reloc_type = BFD_RELOC_X86_64_GOTPC32;
10409 }
10410 else if (align_branch_power)
10411 {
10412 switch (reloc_type)
10413 {
10414 case BFD_RELOC_386_TLS_GD:
10415 case BFD_RELOC_386_TLS_LDM:
10416 case BFD_RELOC_386_TLS_IE:
10417 case BFD_RELOC_386_TLS_IE_32:
10418 case BFD_RELOC_386_TLS_GOTIE:
10419 case BFD_RELOC_386_TLS_GOTDESC:
10420 case BFD_RELOC_386_TLS_DESC_CALL:
10421 case BFD_RELOC_X86_64_TLSGD:
10422 case BFD_RELOC_X86_64_TLSLD:
10423 case BFD_RELOC_X86_64_GOTTPOFF:
10424 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
10425 case BFD_RELOC_X86_64_TLSDESC_CALL:
10426 i.has_gotpc_tls_reloc = true;
10427 default:
10428 break;
10429 }
10430 }
10431 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal,
10432 size, i.op[n].disps, pcrel,
10433 reloc_type);
10434
10435 if (flag_code == CODE_64BIT && size == 4 && pcrel
10436 && !i.prefix[ADDR_PREFIX])
10437 fixP->fx_signed = 1;
10438
10439 /* Check for "call/jmp *mem", "mov mem, %reg",
10440 "test %reg, mem" and "binop mem, %reg" where binop
10441 is one of adc, add, and, cmp, or, sbb, sub, xor
10442 instructions without data prefix. Always generate
10443 R_386_GOT32X for "sym*GOT" operand in 32-bit mode. */
10444 if (i.prefix[DATA_PREFIX] == 0
10445 && (generate_relax_relocations
10446 || (!object_64bit
10447 && i.rm.mode == 0
10448 && i.rm.regmem == 5))
10449 && (i.rm.mode == 2
10450 || (i.rm.mode == 0 && i.rm.regmem == 5))
10451 && i.tm.opcode_space == SPACE_BASE
10452 && ((i.operands == 1
10453 && i.tm.base_opcode == 0xff
10454 && (i.rm.reg == 2 || i.rm.reg == 4))
10455 || (i.operands == 2
10456 && (i.tm.base_opcode == 0x8b
10457 || i.tm.base_opcode == 0x85
10458 || (i.tm.base_opcode & ~0x38) == 0x03))))
10459 {
10460 if (object_64bit)
10461 {
10462 fixP->fx_tcbit = i.rex != 0;
10463 if (i.base_reg
10464 && (i.base_reg->reg_num == RegIP))
10465 fixP->fx_tcbit2 = 1;
10466 }
10467 else
10468 fixP->fx_tcbit2 = 1;
10469 }
10470 }
10471 }
10472 }
10473 }
10474
10475 static void
10476 output_imm (fragS *insn_start_frag, offsetT insn_start_off)
10477 {
10478 char *p;
10479 unsigned int n;
10480
10481 for (n = 0; n < i.operands; n++)
10482 {
10483 if (operand_type_check (i.types[n], imm))
10484 {
10485 int size = imm_size (n);
10486
10487 if (now_seg == absolute_section)
10488 abs_section_offset += size;
10489 else if (i.op[n].imms->X_op == O_constant)
10490 {
10491 offsetT val;
10492
10493 val = offset_in_range (i.op[n].imms->X_add_number,
10494 size);
10495 p = frag_more (size);
10496 md_number_to_chars (p, val, size);
10497 }
10498 else
10499 {
10500 /* Not absolute_section.
10501 Need a 32-bit fixup (don't support 8bit
10502 non-absolute imms). Try to support other
10503 sizes ... */
10504 enum bfd_reloc_code_real reloc_type;
10505 int sign;
10506
10507 if (i.types[n].bitfield.imm32s
10508 && (i.suffix == QWORD_MNEM_SUFFIX
10509 || (!i.suffix && i.tm.opcode_modifier.no_lsuf)
10510 || (i.prefix[REX_PREFIX] & REX_W)
10511 || dot_insn ()))
10512 sign = 1;
10513 else
10514 sign = 0;
10515
10516 p = frag_more (size);
10517 reloc_type = reloc (size, 0, sign, i.reloc[n]);
10518
10519 /* This is tough to explain. We end up with this one if we
10520 * have operands that look like
10521 * "_GLOBAL_OFFSET_TABLE_+[.-.L284]". The goal here is to
10522 * obtain the absolute address of the GOT, and it is strongly
10523 * preferable from a performance point of view to avoid using
10524 * a runtime relocation for this. The actual sequence of
10525 * instructions often look something like:
10526 *
10527 * call .L66
10528 * .L66:
10529 * popl %ebx
10530 * addl $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
10531 *
10532 * The call and pop essentially return the absolute address
10533 * of the label .L66 and store it in %ebx. The linker itself
10534 * will ultimately change the first operand of the addl so
10535 * that %ebx points to the GOT, but to keep things simple, the
10536 * .o file must have this operand set so that it generates not
10537 * the absolute address of .L66, but the absolute address of
10538 * itself. This allows the linker itself simply treat a GOTPC
10539 * relocation as asking for a pcrel offset to the GOT to be
10540 * added in, and the addend of the relocation is stored in the
10541 * operand field for the instruction itself.
10542 *
10543 * Our job here is to fix the operand so that it would add
10544 * the correct offset so that %ebx would point to itself. The
10545 * thing that is tricky is that .-.L66 will point to the
10546 * beginning of the instruction, so we need to further modify
10547 * the operand so that it will point to itself. There are
10548 * other cases where you have something like:
10549 *
10550 * .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
10551 *
10552 * and here no correction would be required. Internally in
10553 * the assembler we treat operands of this form as not being
10554 * pcrel since the '.' is explicitly mentioned, and I wonder
10555 * whether it would simplify matters to do it this way. Who
10556 * knows. In earlier versions of the PIC patches, the
10557 * pcrel_adjust field was used to store the correction, but
10558 * since the expression is not pcrel, I felt it would be
10559 * confusing to do it this way. */
10560
10561 if ((reloc_type == BFD_RELOC_32
10562 || reloc_type == BFD_RELOC_X86_64_32S
10563 || reloc_type == BFD_RELOC_64)
10564 && GOT_symbol
10565 && GOT_symbol == i.op[n].imms->X_add_symbol
10566 && (i.op[n].imms->X_op == O_symbol
10567 || (i.op[n].imms->X_op == O_add
10568 && ((symbol_get_value_expression
10569 (i.op[n].imms->X_op_symbol)->X_op)
10570 == O_subtract))))
10571 {
10572 if (!object_64bit)
10573 reloc_type = BFD_RELOC_386_GOTPC;
10574 else if (size == 4)
10575 reloc_type = BFD_RELOC_X86_64_GOTPC32;
10576 else if (size == 8)
10577 reloc_type = BFD_RELOC_X86_64_GOTPC64;
10578 i.has_gotpc_tls_reloc = true;
10579 i.op[n].imms->X_add_number +=
10580 encoding_length (insn_start_frag, insn_start_off, p);
10581 }
10582 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
10583 i.op[n].imms, 0, reloc_type);
10584 }
10585 }
10586 }
10587 }
10588 \f
10589 /* x86_cons_fix_new is called via the expression parsing code when a
10590 reloc is needed. We use this hook to get the correct .got reloc. */
10591 static int cons_sign = -1;
10592
10593 void
10594 x86_cons_fix_new (fragS *frag, unsigned int off, unsigned int len,
10595 expressionS *exp, bfd_reloc_code_real_type r)
10596 {
10597 r = reloc (len, 0, cons_sign, r);
10598
10599 #ifdef TE_PE
10600 if (exp->X_op == O_secrel)
10601 {
10602 exp->X_op = O_symbol;
10603 r = BFD_RELOC_32_SECREL;
10604 }
10605 else if (exp->X_op == O_secidx)
10606 r = BFD_RELOC_16_SECIDX;
10607 #endif
10608
10609 fix_new_exp (frag, off, len, exp, 0, r);
10610 }
10611
10612 /* Export the ABI address size for use by TC_ADDRESS_BYTES for the
10613 purpose of the `.dc.a' internal pseudo-op. */
10614
10615 int
10616 x86_address_bytes (void)
10617 {
10618 if ((stdoutput->arch_info->mach & bfd_mach_x64_32))
10619 return 4;
10620 return stdoutput->arch_info->bits_per_address / 8;
10621 }
10622
10623 #if (!(defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined (OBJ_MACH_O)) \
10624 || defined (LEX_AT)) && !defined (TE_PE)
10625 # define lex_got(reloc, adjust, types) NULL
10626 #else
10627 /* Parse operands of the form
10628 <symbol>@GOTOFF+<nnn>
10629 and similar .plt or .got references.
10630
10631 If we find one, set up the correct relocation in RELOC and copy the
10632 input string, minus the `@GOTOFF' into a malloc'd buffer for
10633 parsing by the calling routine. Return this buffer, and if ADJUST
10634 is non-null set it to the length of the string we removed from the
10635 input line. Otherwise return NULL. */
10636 static char *
10637 lex_got (enum bfd_reloc_code_real *rel,
10638 int *adjust,
10639 i386_operand_type *types)
10640 {
10641 /* Some of the relocations depend on the size of what field is to
10642 be relocated. But in our callers i386_immediate and i386_displacement
10643 we don't yet know the operand size (this will be set by insn
10644 matching). Hence we record the word32 relocation here,
10645 and adjust the reloc according to the real size in reloc(). */
10646 static const struct
10647 {
10648 const char *str;
10649 int len;
10650 const enum bfd_reloc_code_real rel[2];
10651 const i386_operand_type types64;
10652 bool need_GOT_symbol;
10653 }
10654 gotrel[] =
10655 {
10656
10657 #define OPERAND_TYPE_IMM32_32S_DISP32 { .bitfield = \
10658 { .imm32 = 1, .imm32s = 1, .disp32 = 1 } }
10659 #define OPERAND_TYPE_IMM32_32S_64_DISP32 { .bitfield = \
10660 { .imm32 = 1, .imm32s = 1, .imm64 = 1, .disp32 = 1 } }
10661 #define OPERAND_TYPE_IMM32_32S_64_DISP32_64 { .bitfield = \
10662 { .imm32 = 1, .imm32s = 1, .imm64 = 1, .disp32 = 1, .disp64 = 1 } }
10663 #define OPERAND_TYPE_IMM64_DISP64 { .bitfield = \
10664 { .imm64 = 1, .disp64 = 1 } }
10665
10666 #ifndef TE_PE
10667 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
10668 { STRING_COMMA_LEN ("SIZE"), { BFD_RELOC_SIZE32,
10669 BFD_RELOC_SIZE32 },
10670 { .bitfield = { .imm32 = 1, .imm64 = 1 } }, false },
10671 #endif
10672 { STRING_COMMA_LEN ("PLTOFF"), { _dummy_first_bfd_reloc_code_real,
10673 BFD_RELOC_X86_64_PLTOFF64 },
10674 { .bitfield = { .imm64 = 1 } }, true },
10675 { STRING_COMMA_LEN ("PLT"), { BFD_RELOC_386_PLT32,
10676 BFD_RELOC_X86_64_PLT32 },
10677 OPERAND_TYPE_IMM32_32S_DISP32, false },
10678 { STRING_COMMA_LEN ("GOTPLT"), { _dummy_first_bfd_reloc_code_real,
10679 BFD_RELOC_X86_64_GOTPLT64 },
10680 OPERAND_TYPE_IMM64_DISP64, true },
10681 { STRING_COMMA_LEN ("GOTOFF"), { BFD_RELOC_386_GOTOFF,
10682 BFD_RELOC_X86_64_GOTOFF64 },
10683 OPERAND_TYPE_IMM64_DISP64, true },
10684 { STRING_COMMA_LEN ("GOTPCREL"), { _dummy_first_bfd_reloc_code_real,
10685 BFD_RELOC_X86_64_GOTPCREL },
10686 OPERAND_TYPE_IMM32_32S_DISP32, true },
10687 { STRING_COMMA_LEN ("TLSGD"), { BFD_RELOC_386_TLS_GD,
10688 BFD_RELOC_X86_64_TLSGD },
10689 OPERAND_TYPE_IMM32_32S_DISP32, true },
10690 { STRING_COMMA_LEN ("TLSLDM"), { BFD_RELOC_386_TLS_LDM,
10691 _dummy_first_bfd_reloc_code_real },
10692 OPERAND_TYPE_NONE, true },
10693 { STRING_COMMA_LEN ("TLSLD"), { _dummy_first_bfd_reloc_code_real,
10694 BFD_RELOC_X86_64_TLSLD },
10695 OPERAND_TYPE_IMM32_32S_DISP32, true },
10696 { STRING_COMMA_LEN ("GOTTPOFF"), { BFD_RELOC_386_TLS_IE_32,
10697 BFD_RELOC_X86_64_GOTTPOFF },
10698 OPERAND_TYPE_IMM32_32S_DISP32, true },
10699 { STRING_COMMA_LEN ("TPOFF"), { BFD_RELOC_386_TLS_LE_32,
10700 BFD_RELOC_X86_64_TPOFF32 },
10701 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
10702 { STRING_COMMA_LEN ("NTPOFF"), { BFD_RELOC_386_TLS_LE,
10703 _dummy_first_bfd_reloc_code_real },
10704 OPERAND_TYPE_NONE, true },
10705 { STRING_COMMA_LEN ("DTPOFF"), { BFD_RELOC_386_TLS_LDO_32,
10706 BFD_RELOC_X86_64_DTPOFF32 },
10707 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
10708 { STRING_COMMA_LEN ("GOTNTPOFF"),{ BFD_RELOC_386_TLS_GOTIE,
10709 _dummy_first_bfd_reloc_code_real },
10710 OPERAND_TYPE_NONE, true },
10711 { STRING_COMMA_LEN ("INDNTPOFF"),{ BFD_RELOC_386_TLS_IE,
10712 _dummy_first_bfd_reloc_code_real },
10713 OPERAND_TYPE_NONE, true },
10714 { STRING_COMMA_LEN ("GOT"), { BFD_RELOC_386_GOT32,
10715 BFD_RELOC_X86_64_GOT32 },
10716 OPERAND_TYPE_IMM32_32S_64_DISP32, true },
10717 { STRING_COMMA_LEN ("TLSDESC"), { BFD_RELOC_386_TLS_GOTDESC,
10718 BFD_RELOC_X86_64_GOTPC32_TLSDESC },
10719 OPERAND_TYPE_IMM32_32S_DISP32, true },
10720 { STRING_COMMA_LEN ("TLSCALL"), { BFD_RELOC_386_TLS_DESC_CALL,
10721 BFD_RELOC_X86_64_TLSDESC_CALL },
10722 OPERAND_TYPE_IMM32_32S_DISP32, true },
10723 #else /* TE_PE */
10724 { STRING_COMMA_LEN ("SECREL32"), { BFD_RELOC_32_SECREL,
10725 BFD_RELOC_32_SECREL },
10726 OPERAND_TYPE_IMM32_32S_64_DISP32_64, false },
10727 #endif
10728
10729 #undef OPERAND_TYPE_IMM32_32S_DISP32
10730 #undef OPERAND_TYPE_IMM32_32S_64_DISP32
10731 #undef OPERAND_TYPE_IMM32_32S_64_DISP32_64
10732 #undef OPERAND_TYPE_IMM64_DISP64
10733
10734 };
10735 char *cp;
10736 unsigned int j;
10737
10738 #if defined (OBJ_MAYBE_ELF) && !defined (TE_PE)
10739 if (!IS_ELF)
10740 return NULL;
10741 #endif
10742
10743 for (cp = input_line_pointer; *cp != '@'; cp++)
10744 if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
10745 return NULL;
10746
10747 for (j = 0; j < ARRAY_SIZE (gotrel); j++)
10748 {
10749 int len = gotrel[j].len;
10750 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
10751 {
10752 if (gotrel[j].rel[object_64bit] != 0)
10753 {
10754 int first, second;
10755 char *tmpbuf, *past_reloc;
10756
10757 *rel = gotrel[j].rel[object_64bit];
10758
10759 if (types)
10760 {
10761 if (flag_code != CODE_64BIT)
10762 {
10763 types->bitfield.imm32 = 1;
10764 types->bitfield.disp32 = 1;
10765 }
10766 else
10767 *types = gotrel[j].types64;
10768 }
10769
10770 if (gotrel[j].need_GOT_symbol && GOT_symbol == NULL)
10771 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
10772
10773 /* The length of the first part of our input line. */
10774 first = cp - input_line_pointer;
10775
10776 /* The second part goes from after the reloc token until
10777 (and including) an end_of_line char or comma. */
10778 past_reloc = cp + 1 + len;
10779 cp = past_reloc;
10780 while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
10781 ++cp;
10782 second = cp + 1 - past_reloc;
10783
10784 /* Allocate and copy string. The trailing NUL shouldn't
10785 be necessary, but be safe. */
10786 tmpbuf = XNEWVEC (char, first + second + 2);
10787 memcpy (tmpbuf, input_line_pointer, first);
10788 if (second != 0 && *past_reloc != ' ')
10789 /* Replace the relocation token with ' ', so that
10790 errors like foo@GOTOFF1 will be detected. */
10791 tmpbuf[first++] = ' ';
10792 else
10793 /* Increment length by 1 if the relocation token is
10794 removed. */
10795 len++;
10796 if (adjust)
10797 *adjust = len;
10798 memcpy (tmpbuf + first, past_reloc, second);
10799 tmpbuf[first + second] = '\0';
10800 return tmpbuf;
10801 }
10802
10803 as_bad (_("@%s reloc is not supported with %d-bit output format"),
10804 gotrel[j].str, 1 << (5 + object_64bit));
10805 return NULL;
10806 }
10807 }
10808
10809 /* Might be a symbol version string. Don't as_bad here. */
10810 return NULL;
10811 }
10812 #endif
10813
10814 bfd_reloc_code_real_type
10815 x86_cons (expressionS *exp, int size)
10816 {
10817 bfd_reloc_code_real_type got_reloc = NO_RELOC;
10818
10819 intel_syntax = -intel_syntax;
10820 exp->X_md = 0;
10821 expr_mode = expr_operator_none;
10822
10823 #if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
10824 && !defined (LEX_AT)) \
10825 || defined (TE_PE)
10826 if (size == 4 || (object_64bit && size == 8))
10827 {
10828 /* Handle @GOTOFF and the like in an expression. */
10829 char *save;
10830 char *gotfree_input_line;
10831 int adjust = 0;
10832
10833 save = input_line_pointer;
10834 gotfree_input_line = lex_got (&got_reloc, &adjust, NULL);
10835 if (gotfree_input_line)
10836 input_line_pointer = gotfree_input_line;
10837
10838 expression (exp);
10839
10840 if (gotfree_input_line)
10841 {
10842 /* expression () has merrily parsed up to the end of line,
10843 or a comma - in the wrong buffer. Transfer how far
10844 input_line_pointer has moved to the right buffer. */
10845 input_line_pointer = (save
10846 + (input_line_pointer - gotfree_input_line)
10847 + adjust);
10848 free (gotfree_input_line);
10849 if (exp->X_op == O_constant
10850 || exp->X_op == O_absent
10851 || exp->X_op == O_illegal
10852 || exp->X_op == O_register
10853 || exp->X_op == O_big)
10854 {
10855 char c = *input_line_pointer;
10856 *input_line_pointer = 0;
10857 as_bad (_("missing or invalid expression `%s'"), save);
10858 *input_line_pointer = c;
10859 }
10860 else if ((got_reloc == BFD_RELOC_386_PLT32
10861 || got_reloc == BFD_RELOC_X86_64_PLT32)
10862 && exp->X_op != O_symbol)
10863 {
10864 char c = *input_line_pointer;
10865 *input_line_pointer = 0;
10866 as_bad (_("invalid PLT expression `%s'"), save);
10867 *input_line_pointer = c;
10868 }
10869 }
10870 }
10871 else
10872 #endif
10873 expression (exp);
10874
10875 intel_syntax = -intel_syntax;
10876
10877 if (intel_syntax)
10878 i386_intel_simplify (exp);
10879
10880 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
10881 if (size <= 4 && expr_mode == expr_operator_present
10882 && exp->X_op == O_constant && !object_64bit)
10883 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
10884
10885 return got_reloc;
10886 }
10887
10888 static void
10889 signed_cons (int size)
10890 {
10891 if (object_64bit)
10892 cons_sign = 1;
10893 cons (size);
10894 cons_sign = -1;
10895 }
10896
10897 static void
10898 s_insn (int dummy ATTRIBUTE_UNUSED)
10899 {
10900 char mnemonic[MAX_MNEM_SIZE], *line = input_line_pointer, *ptr;
10901 char *saved_ilp = find_end_of_line (line, false), saved_char;
10902 const char *end;
10903 unsigned int j;
10904 valueT val;
10905 bool vex = false, xop = false, evex = false;
10906 static const templates tt = { &i.tm, &i.tm + 1 };
10907
10908 init_globals ();
10909
10910 saved_char = *saved_ilp;
10911 *saved_ilp = 0;
10912
10913 end = parse_insn (line, mnemonic, true);
10914 if (end == NULL)
10915 {
10916 bad:
10917 *saved_ilp = saved_char;
10918 ignore_rest_of_line ();
10919 i.tm.mnem_off = 0;
10920 return;
10921 }
10922 line += end - line;
10923
10924 current_templates = &tt;
10925 i.tm.mnem_off = MN__insn;
10926 i.tm.extension_opcode = None;
10927
10928 if (startswith (line, "VEX")
10929 && (line[3] == '.' || is_space_char (line[3])))
10930 {
10931 vex = true;
10932 line += 3;
10933 }
10934 else if (startswith (line, "XOP") && ISDIGIT (line[3]))
10935 {
10936 char *e;
10937 unsigned long n = strtoul (line + 3, &e, 16);
10938
10939 if (e == line + 5 && n >= 0x08 && n <= 0x1f
10940 && (*e == '.' || is_space_char (*e)))
10941 {
10942 xop = true;
10943 /* Arrange for build_vex_prefix() to emit 0x8f. */
10944 i.tm.opcode_space = SPACE_XOP08;
10945 i.insn_opcode_space = n;
10946 line = e;
10947 }
10948 }
10949 else if (startswith (line, "EVEX")
10950 && (line[4] == '.' || is_space_char (line[4])))
10951 {
10952 evex = true;
10953 line += 4;
10954 }
10955
10956 if (vex || xop
10957 ? i.vec_encoding == vex_encoding_evex
10958 : evex
10959 ? i.vec_encoding == vex_encoding_vex
10960 || i.vec_encoding == vex_encoding_vex3
10961 : i.vec_encoding != vex_encoding_default)
10962 {
10963 as_bad (_("pseudo-prefix conflicts with encoding specifier"));
10964 goto bad;
10965 }
10966
10967 if (line > end && i.vec_encoding == vex_encoding_default)
10968 i.vec_encoding = evex ? vex_encoding_evex : vex_encoding_vex;
10969
10970 if (i.vec_encoding != vex_encoding_default)
10971 {
10972 /* Only address size and segment override prefixes are permitted with
10973 VEX/XOP/EVEX encodings. */
10974 const unsigned char *p = i.prefix;
10975
10976 for (j = 0; j < ARRAY_SIZE (i.prefix); ++j, ++p)
10977 {
10978 if (!*p)
10979 continue;
10980
10981 switch (j)
10982 {
10983 case SEG_PREFIX:
10984 case ADDR_PREFIX:
10985 break;
10986 default:
10987 as_bad (_("illegal prefix used with VEX/XOP/EVEX"));
10988 goto bad;
10989 }
10990 }
10991 }
10992
10993 if (line > end && *line == '.')
10994 {
10995 /* Length specifier (VEX.L, XOP.L, EVEX.L'L). */
10996 switch (line[1])
10997 {
10998 case 'L':
10999 switch (line[2])
11000 {
11001 case '0':
11002 if (evex)
11003 i.tm.opcode_modifier.evex = EVEX128;
11004 else
11005 i.tm.opcode_modifier.vex = VEX128;
11006 break;
11007
11008 case '1':
11009 if (evex)
11010 i.tm.opcode_modifier.evex = EVEX256;
11011 else
11012 i.tm.opcode_modifier.vex = VEX256;
11013 break;
11014
11015 case '2':
11016 if (evex)
11017 i.tm.opcode_modifier.evex = EVEX512;
11018 break;
11019
11020 case '3':
11021 if (evex)
11022 i.tm.opcode_modifier.evex = EVEX_L3;
11023 break;
11024
11025 case 'I':
11026 if (line[3] == 'G')
11027 {
11028 if (evex)
11029 i.tm.opcode_modifier.evex = EVEXLIG;
11030 else
11031 i.tm.opcode_modifier.vex = VEXScalar; /* LIG */
11032 ++line;
11033 }
11034 break;
11035 }
11036
11037 if (i.tm.opcode_modifier.vex || i.tm.opcode_modifier.evex)
11038 line += 3;
11039 break;
11040
11041 case '1':
11042 if (line[2] == '2' && line[3] == '8')
11043 {
11044 if (evex)
11045 i.tm.opcode_modifier.evex = EVEX128;
11046 else
11047 i.tm.opcode_modifier.vex = VEX128;
11048 line += 4;
11049 }
11050 break;
11051
11052 case '2':
11053 if (line[2] == '5' && line[3] == '6')
11054 {
11055 if (evex)
11056 i.tm.opcode_modifier.evex = EVEX256;
11057 else
11058 i.tm.opcode_modifier.vex = VEX256;
11059 line += 4;
11060 }
11061 break;
11062
11063 case '5':
11064 if (evex && line[2] == '1' && line[3] == '2')
11065 {
11066 i.tm.opcode_modifier.evex = EVEX512;
11067 line += 4;
11068 }
11069 break;
11070 }
11071 }
11072
11073 if (line > end && *line == '.')
11074 {
11075 /* embedded prefix (VEX.pp, XOP.pp, EVEX.pp). */
11076 switch (line[1])
11077 {
11078 case 'N':
11079 if (line[2] == 'P')
11080 line += 3;
11081 break;
11082
11083 case '6':
11084 if (line[2] == '6')
11085 {
11086 i.tm.opcode_modifier.opcodeprefix = PREFIX_0X66;
11087 line += 3;
11088 }
11089 break;
11090
11091 case 'F': case 'f':
11092 if (line[2] == '3')
11093 {
11094 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
11095 line += 3;
11096 }
11097 else if (line[2] == '2')
11098 {
11099 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF2;
11100 line += 3;
11101 }
11102 break;
11103 }
11104 }
11105
11106 if (line > end && !xop && *line == '.')
11107 {
11108 /* Encoding space (VEX.mmmmm, EVEX.mmmm). */
11109 switch (line[1])
11110 {
11111 case '0':
11112 if (TOUPPER (line[2]) != 'F')
11113 break;
11114 if (line[3] == '.' || is_space_char (line[3]))
11115 {
11116 i.insn_opcode_space = SPACE_0F;
11117 line += 3;
11118 }
11119 else if (line[3] == '3'
11120 && (line[4] == '8' || TOUPPER (line[4]) == 'A')
11121 && (line[5] == '.' || is_space_char (line[5])))
11122 {
11123 i.insn_opcode_space = line[4] == '8' ? SPACE_0F38 : SPACE_0F3A;
11124 line += 5;
11125 }
11126 break;
11127
11128 case 'M':
11129 if (ISDIGIT (line[2]) && line[2] != '0')
11130 {
11131 char *e;
11132 unsigned long n = strtoul (line + 2, &e, 10);
11133
11134 if (n <= (evex ? 15 : 31)
11135 && (*e == '.' || is_space_char (*e)))
11136 {
11137 i.insn_opcode_space = n;
11138 line = e;
11139 }
11140 }
11141 break;
11142 }
11143 }
11144
11145 if (line > end && *line == '.' && line[1] == 'W')
11146 {
11147 /* VEX.W, XOP.W, EVEX.W */
11148 switch (line[2])
11149 {
11150 case '0':
11151 i.tm.opcode_modifier.vexw = VEXW0;
11152 break;
11153
11154 case '1':
11155 i.tm.opcode_modifier.vexw = VEXW1;
11156 break;
11157
11158 case 'I':
11159 if (line[3] == 'G')
11160 {
11161 i.tm.opcode_modifier.vexw = VEXWIG;
11162 ++line;
11163 }
11164 break;
11165 }
11166
11167 if (i.tm.opcode_modifier.vexw)
11168 line += 3;
11169 }
11170
11171 if (line > end && *line && !is_space_char (*line))
11172 {
11173 /* Improve diagnostic a little. */
11174 if (*line == '.' && line[1] && !is_space_char (line[1]))
11175 ++line;
11176 goto done;
11177 }
11178
11179 /* Before processing the opcode expression, find trailing "+r" or
11180 "/<digit>" specifiers. */
11181 for (ptr = line; ; ++ptr)
11182 {
11183 unsigned long n;
11184 char *e;
11185
11186 ptr = strpbrk (ptr, "+/,");
11187 if (ptr == NULL || *ptr == ',')
11188 break;
11189
11190 if (*ptr == '+' && ptr[1] == 'r'
11191 && (ptr[2] == ',' || (is_space_char (ptr[2]) && ptr[3] == ',')))
11192 {
11193 *ptr = ' ';
11194 ptr[1] = ' ';
11195 i.short_form = true;
11196 break;
11197 }
11198
11199 if (*ptr == '/' && ISDIGIT (ptr[1])
11200 && (n = strtoul (ptr + 1, &e, 8)) < 8
11201 && e == ptr + 2
11202 && (ptr[2] == ',' || (is_space_char (ptr[2]) && ptr[3] == ',')))
11203 {
11204 *ptr = ' ';
11205 ptr[1] = ' ';
11206 i.tm.extension_opcode = n;
11207 i.tm.opcode_modifier.modrm = 1;
11208 break;
11209 }
11210 }
11211
11212 input_line_pointer = line;
11213 val = get_absolute_expression ();
11214 line = input_line_pointer;
11215
11216 if (i.short_form && (val & 7))
11217 as_warn ("`+r' assumes low three opcode bits to be clear");
11218
11219 for (j = 1; j < sizeof(val); ++j)
11220 if (!(val >> (j * 8)))
11221 break;
11222
11223 /* Trim off a prefix if present. */
11224 if (j > 1 && !vex && !xop && !evex)
11225 {
11226 uint8_t byte = val >> ((j - 1) * 8);
11227
11228 switch (byte)
11229 {
11230 case DATA_PREFIX_OPCODE:
11231 case REPE_PREFIX_OPCODE:
11232 case REPNE_PREFIX_OPCODE:
11233 if (!add_prefix (byte))
11234 goto bad;
11235 val &= ((uint64_t)1 << (--j * 8)) - 1;
11236 break;
11237 }
11238 }
11239
11240 /* Trim off encoding space. */
11241 if (j > 1 && !i.insn_opcode_space && (val >> ((j - 1) * 8)) == 0x0f)
11242 {
11243 uint8_t byte = val >> ((--j - 1) * 8);
11244
11245 i.insn_opcode_space = SPACE_0F;
11246 switch (byte & -(j > 1))
11247 {
11248 case 0x38:
11249 i.insn_opcode_space = SPACE_0F38;
11250 --j;
11251 break;
11252 case 0x3a:
11253 i.insn_opcode_space = SPACE_0F3A;
11254 --j;
11255 break;
11256 }
11257 i.tm.opcode_space = i.insn_opcode_space;
11258 val &= ((uint64_t)1 << (j * 8)) - 1;
11259 }
11260 if (!i.tm.opcode_space && (vex || evex))
11261 /* Arrange for build_vex_prefix() to properly emit 0xC4/0xC5.
11262 Also avoid hitting abort() there or in build_evex_prefix(). */
11263 i.tm.opcode_space = i.insn_opcode_space == SPACE_0F ? SPACE_0F
11264 : SPACE_0F38;
11265
11266 if (j > 2)
11267 {
11268 as_bad (_("opcode residual (%#"PRIx64") too wide"), (uint64_t) val);
11269 goto bad;
11270 }
11271 i.opcode_length = j;
11272
11273 /* Handle operands, if any. */
11274 if (*line == ',')
11275 {
11276 i386_operand_type combined;
11277 expressionS *disp_exp = NULL;
11278 bool changed;
11279
11280 i.memshift = -1;
11281
11282 ptr = parse_operands (line + 1, &i386_mnemonics[MN__insn]);
11283 this_operand = -1;
11284 if (!ptr)
11285 goto bad;
11286 line = ptr;
11287
11288 if (!i.operands)
11289 {
11290 as_bad (_("expecting operand after ','; got nothing"));
11291 goto done;
11292 }
11293
11294 if (i.mem_operands > 1)
11295 {
11296 as_bad (_("too many memory references for `%s'"),
11297 &i386_mnemonics[MN__insn]);
11298 goto done;
11299 }
11300
11301 /* No need to distinguish vex_encoding_evex and vex_encoding_evex512. */
11302 if (i.vec_encoding == vex_encoding_evex512)
11303 i.vec_encoding = vex_encoding_evex;
11304
11305 /* Are we to emit ModR/M encoding? */
11306 if (!i.short_form
11307 && (i.mem_operands
11308 || i.reg_operands > (i.vec_encoding != vex_encoding_default)
11309 || i.tm.extension_opcode != None))
11310 i.tm.opcode_modifier.modrm = 1;
11311
11312 if (!i.tm.opcode_modifier.modrm
11313 && (i.reg_operands
11314 > i.short_form + 0U + (i.vec_encoding != vex_encoding_default)
11315 || i.mem_operands))
11316 {
11317 as_bad (_("too many register/memory operands"));
11318 goto done;
11319 }
11320
11321 /* Enforce certain constraints on operands. */
11322 switch (i.reg_operands + i.mem_operands
11323 + (i.tm.extension_opcode != None))
11324 {
11325 case 0:
11326 if (i.short_form)
11327 {
11328 as_bad (_("too few register/memory operands"));
11329 goto done;
11330 }
11331 /* Fall through. */
11332 case 1:
11333 if (i.tm.opcode_modifier.modrm)
11334 {
11335 as_bad (_("too few register/memory operands"));
11336 goto done;
11337 }
11338 break;
11339
11340 case 2:
11341 break;
11342
11343 case 4:
11344 if (i.imm_operands
11345 && (i.op[0].imms->X_op != O_constant
11346 || !fits_in_imm4 (i.op[0].imms->X_add_number)))
11347 {
11348 as_bad (_("constant doesn't fit in %d bits"), evex ? 3 : 4);
11349 goto done;
11350 }
11351 /* Fall through. */
11352 case 3:
11353 if (i.vec_encoding != vex_encoding_default)
11354 {
11355 i.tm.opcode_modifier.vexvvvv = 1;
11356 break;
11357 }
11358 /* Fall through. */
11359 default:
11360 as_bad (_("too many register/memory operands"));
11361 goto done;
11362 }
11363
11364 /* Bring operands into canonical order (imm, mem, reg). */
11365 do
11366 {
11367 changed = false;
11368
11369 for (j = 1; j < i.operands; ++j)
11370 {
11371 if ((!operand_type_check (i.types[j - 1], imm)
11372 && operand_type_check (i.types[j], imm))
11373 || (i.types[j - 1].bitfield.class != ClassNone
11374 && i.types[j].bitfield.class == ClassNone))
11375 {
11376 swap_2_operands (j - 1, j);
11377 changed = true;
11378 }
11379 }
11380 }
11381 while (changed);
11382
11383 /* For Intel syntax swap the order of register operands. */
11384 if (intel_syntax)
11385 switch (i.reg_operands)
11386 {
11387 case 0:
11388 case 1:
11389 break;
11390
11391 case 4:
11392 swap_2_operands (i.imm_operands + i.mem_operands + 1, i.operands - 2);
11393 /* Fall through. */
11394 case 3:
11395 case 2:
11396 swap_2_operands (i.imm_operands + i.mem_operands, i.operands - 1);
11397 break;
11398
11399 default:
11400 abort ();
11401 }
11402
11403 /* Enforce constraints when using VSIB. */
11404 if (i.index_reg
11405 && (i.index_reg->reg_type.bitfield.xmmword
11406 || i.index_reg->reg_type.bitfield.ymmword
11407 || i.index_reg->reg_type.bitfield.zmmword))
11408 {
11409 if (i.vec_encoding == vex_encoding_default)
11410 {
11411 as_bad (_("VSIB unavailable with legacy encoding"));
11412 goto done;
11413 }
11414
11415 if (i.vec_encoding == vex_encoding_evex
11416 && i.reg_operands > 1)
11417 {
11418 /* We could allow two register operands, encoding the 2nd one in
11419 an 8-bit immediate like for 4-register-operand insns, but that
11420 would require ugly fiddling with process_operands() and/or
11421 build_modrm_byte(). */
11422 as_bad (_("too many register operands with VSIB"));
11423 goto done;
11424 }
11425
11426 i.tm.opcode_modifier.sib = 1;
11427 }
11428
11429 /* Establish operand size encoding. */
11430 operand_type_set (&combined, 0);
11431
11432 for (j = i.imm_operands; j < i.operands; ++j)
11433 {
11434 i.types[j].bitfield.instance = InstanceNone;
11435
11436 if (operand_type_check (i.types[j], disp))
11437 {
11438 i.types[j].bitfield.baseindex = 1;
11439 disp_exp = i.op[j].disps;
11440 }
11441
11442 if (evex && i.types[j].bitfield.baseindex)
11443 {
11444 unsigned int n = i.memshift;
11445
11446 if (i.types[j].bitfield.byte)
11447 n = 0;
11448 else if (i.types[j].bitfield.word)
11449 n = 1;
11450 else if (i.types[j].bitfield.dword)
11451 n = 2;
11452 else if (i.types[j].bitfield.qword)
11453 n = 3;
11454 else if (i.types[j].bitfield.xmmword)
11455 n = 4;
11456 else if (i.types[j].bitfield.ymmword)
11457 n = 5;
11458 else if (i.types[j].bitfield.zmmword)
11459 n = 6;
11460
11461 if (i.memshift < 32 && n != i.memshift)
11462 as_warn ("conflicting memory operand size specifiers");
11463 i.memshift = n;
11464 }
11465
11466 if ((i.broadcast.type || i.broadcast.bytes)
11467 && j == i.broadcast.operand)
11468 continue;
11469
11470 combined = operand_type_or (combined, i.types[j]);
11471 combined.bitfield.class = ClassNone;
11472 }
11473
11474 switch ((i.broadcast.type ? i.broadcast.type : 1)
11475 << (i.memshift < 32 ? i.memshift : 0))
11476 {
11477 case 64: combined.bitfield.zmmword = 1; break;
11478 case 32: combined.bitfield.ymmword = 1; break;
11479 case 16: combined.bitfield.xmmword = 1; break;
11480 case 8: combined.bitfield.qword = 1; break;
11481 case 4: combined.bitfield.dword = 1; break;
11482 }
11483
11484 if (i.vec_encoding == vex_encoding_default)
11485 {
11486 if (flag_code == CODE_64BIT && combined.bitfield.qword)
11487 i.rex |= REX_W;
11488 else if ((flag_code == CODE_16BIT ? combined.bitfield.dword
11489 : combined.bitfield.word)
11490 && !add_prefix (DATA_PREFIX_OPCODE))
11491 goto done;
11492 }
11493 else if (!i.tm.opcode_modifier.vexw)
11494 {
11495 if (flag_code == CODE_64BIT)
11496 {
11497 if (combined.bitfield.qword)
11498 i.tm.opcode_modifier.vexw = VEXW1;
11499 else if (combined.bitfield.dword)
11500 i.tm.opcode_modifier.vexw = VEXW0;
11501 }
11502
11503 if (!i.tm.opcode_modifier.vexw)
11504 i.tm.opcode_modifier.vexw = VEXWIG;
11505 }
11506
11507 if (vex || xop)
11508 {
11509 if (!i.tm.opcode_modifier.vex)
11510 {
11511 if (combined.bitfield.ymmword)
11512 i.tm.opcode_modifier.vex = VEX256;
11513 else if (combined.bitfield.xmmword)
11514 i.tm.opcode_modifier.vex = VEX128;
11515 }
11516 }
11517 else if (evex)
11518 {
11519 if (!i.tm.opcode_modifier.evex)
11520 {
11521 /* Do _not_ consider AVX512VL here. */
11522 if (i.rounding.type != rc_none || combined.bitfield.zmmword)
11523 i.tm.opcode_modifier.evex = EVEX512;
11524 else if (combined.bitfield.ymmword)
11525 i.tm.opcode_modifier.evex = EVEX256;
11526 else if (combined.bitfield.xmmword)
11527 i.tm.opcode_modifier.evex = EVEX128;
11528 }
11529
11530 if (i.memshift >= 32)
11531 {
11532 unsigned int n = 0;
11533
11534 switch (i.tm.opcode_modifier.evex)
11535 {
11536 case EVEX512: n = 64; break;
11537 case EVEX256: n = 32; break;
11538 case EVEX128: n = 16; break;
11539 }
11540
11541 if (i.broadcast.type)
11542 n /= i.broadcast.type;
11543
11544 if (n > 0)
11545 for (i.memshift = 0; !(n & 1); n >>= 1)
11546 ++i.memshift;
11547 else if (disp_exp != NULL && disp_exp->X_op == O_constant
11548 && disp_exp->X_add_number != 0
11549 && i.disp_encoding != disp_encoding_32bit)
11550 {
11551 if (!quiet_warnings)
11552 as_warn ("cannot determine memory operand size");
11553 i.disp_encoding = disp_encoding_32bit;
11554 }
11555 }
11556 }
11557
11558 if (i.memshift >= 32)
11559 i.memshift = 0;
11560 else if (!evex)
11561 i.vec_encoding = vex_encoding_error;
11562
11563 if (i.disp_operands && !optimize_disp (&i.tm))
11564 goto done;
11565
11566 /* Establish size for immediate operands. */
11567 for (j = 0; j < i.imm_operands; ++j)
11568 {
11569 expressionS *expP = i.op[j].imms;
11570
11571 gas_assert (operand_type_check (i.types[j], imm));
11572 operand_type_set (&i.types[j], 0);
11573
11574 if (i.imm_bits[j] > 32)
11575 i.types[j].bitfield.imm64 = 1;
11576 else if (i.imm_bits[j] > 16)
11577 {
11578 if (flag_code == CODE_64BIT && (i.flags[j] & Operand_Signed))
11579 i.types[j].bitfield.imm32s = 1;
11580 else
11581 i.types[j].bitfield.imm32 = 1;
11582 }
11583 else if (i.imm_bits[j] > 8)
11584 i.types[j].bitfield.imm16 = 1;
11585 else if (i.imm_bits[j] > 0)
11586 {
11587 if (i.flags[j] & Operand_Signed)
11588 i.types[j].bitfield.imm8s = 1;
11589 else
11590 i.types[j].bitfield.imm8 = 1;
11591 }
11592 else if (expP->X_op == O_constant)
11593 {
11594 i.types[j] = smallest_imm_type (expP->X_add_number);
11595 i.types[j].bitfield.imm1 = 0;
11596 /* Oddly enough imm_size() checks imm64 first, so the bit needs
11597 zapping since smallest_imm_type() sets it unconditionally. */
11598 if (flag_code != CODE_64BIT)
11599 {
11600 i.types[j].bitfield.imm64 = 0;
11601 i.types[j].bitfield.imm32s = 0;
11602 i.types[j].bitfield.imm32 = 1;
11603 }
11604 else if (i.types[j].bitfield.imm32 || i.types[j].bitfield.imm32s)
11605 i.types[j].bitfield.imm64 = 0;
11606 }
11607 else
11608 /* Non-constant expressions are sized heuristically. */
11609 switch (flag_code)
11610 {
11611 case CODE_64BIT: i.types[j].bitfield.imm32s = 1; break;
11612 case CODE_32BIT: i.types[j].bitfield.imm32 = 1; break;
11613 case CODE_16BIT: i.types[j].bitfield.imm16 = 1; break;
11614 }
11615 }
11616
11617 for (j = 0; j < i.operands; ++j)
11618 i.tm.operand_types[j] = i.types[j];
11619
11620 process_operands ();
11621 }
11622
11623 /* Don't set opcode until after processing operands, to avoid any
11624 potential special casing there. */
11625 i.tm.base_opcode |= val;
11626
11627 if (i.vec_encoding == vex_encoding_error
11628 || (i.vec_encoding != vex_encoding_evex
11629 ? i.broadcast.type || i.broadcast.bytes
11630 || i.rounding.type != rc_none
11631 || i.mask.reg
11632 : (i.mem_operands && i.rounding.type != rc_none)
11633 || ((i.broadcast.type || i.broadcast.bytes)
11634 && !(i.flags[i.broadcast.operand] & Operand_Mem))))
11635 {
11636 as_bad (_("conflicting .insn operands"));
11637 goto done;
11638 }
11639
11640 if (vex || xop)
11641 {
11642 if (!i.tm.opcode_modifier.vex)
11643 i.tm.opcode_modifier.vex = VEXScalar; /* LIG */
11644
11645 build_vex_prefix (NULL);
11646 i.rex &= REX_OPCODE;
11647 }
11648 else if (evex)
11649 {
11650 if (!i.tm.opcode_modifier.evex)
11651 i.tm.opcode_modifier.evex = EVEXLIG;
11652
11653 build_evex_prefix ();
11654 i.rex &= REX_OPCODE;
11655 }
11656 else if (i.rex != 0)
11657 add_prefix (REX_OPCODE | i.rex);
11658
11659 output_insn ();
11660
11661 done:
11662 *saved_ilp = saved_char;
11663 input_line_pointer = line;
11664
11665 demand_empty_rest_of_line ();
11666
11667 /* Make sure dot_insn() won't yield "true" anymore. */
11668 i.tm.mnem_off = 0;
11669 }
11670
11671 #ifdef TE_PE
11672 static void
11673 pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
11674 {
11675 expressionS exp;
11676
11677 do
11678 {
11679 expression (&exp);
11680 if (exp.X_op == O_symbol)
11681 exp.X_op = O_secrel;
11682
11683 emit_expr (&exp, 4);
11684 }
11685 while (*input_line_pointer++ == ',');
11686
11687 input_line_pointer--;
11688 demand_empty_rest_of_line ();
11689 }
11690
11691 static void
11692 pe_directive_secidx (int dummy ATTRIBUTE_UNUSED)
11693 {
11694 expressionS exp;
11695
11696 do
11697 {
11698 expression (&exp);
11699 if (exp.X_op == O_symbol)
11700 exp.X_op = O_secidx;
11701
11702 emit_expr (&exp, 2);
11703 }
11704 while (*input_line_pointer++ == ',');
11705
11706 input_line_pointer--;
11707 demand_empty_rest_of_line ();
11708 }
11709 #endif
11710
11711 /* Handle Rounding Control / SAE specifiers. */
11712
11713 static char *
11714 RC_SAE_specifier (const char *pstr)
11715 {
11716 unsigned int j;
11717
11718 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); j++)
11719 {
11720 if (!strncmp (pstr, RC_NamesTable[j].name, RC_NamesTable[j].len))
11721 {
11722 if (i.rounding.type != rc_none)
11723 {
11724 as_bad (_("duplicated `{%s}'"), RC_NamesTable[j].name);
11725 return NULL;
11726 }
11727
11728 if (i.vec_encoding == vex_encoding_default)
11729 i.vec_encoding = vex_encoding_evex512;
11730 else if (i.vec_encoding != vex_encoding_evex
11731 && i.vec_encoding != vex_encoding_evex512)
11732 return NULL;
11733
11734 i.rounding.type = RC_NamesTable[j].type;
11735
11736 return (char *)(pstr + RC_NamesTable[j].len);
11737 }
11738 }
11739
11740 return NULL;
11741 }
11742
11743 /* Handle Vector operations. */
11744
11745 static char *
11746 check_VecOperations (char *op_string)
11747 {
11748 const reg_entry *mask;
11749 const char *saved;
11750 char *end_op;
11751
11752 while (*op_string)
11753 {
11754 saved = op_string;
11755 if (*op_string == '{')
11756 {
11757 op_string++;
11758
11759 /* Check broadcasts. */
11760 if (startswith (op_string, "1to"))
11761 {
11762 unsigned int bcst_type;
11763
11764 if (i.broadcast.type)
11765 goto duplicated_vec_op;
11766
11767 op_string += 3;
11768 if (*op_string == '8')
11769 bcst_type = 8;
11770 else if (*op_string == '4')
11771 bcst_type = 4;
11772 else if (*op_string == '2')
11773 bcst_type = 2;
11774 else if (*op_string == '1'
11775 && *(op_string+1) == '6')
11776 {
11777 bcst_type = 16;
11778 op_string++;
11779 }
11780 else if (*op_string == '3'
11781 && *(op_string+1) == '2')
11782 {
11783 bcst_type = 32;
11784 op_string++;
11785 }
11786 else
11787 {
11788 as_bad (_("Unsupported broadcast: `%s'"), saved);
11789 return NULL;
11790 }
11791 op_string++;
11792
11793 if (i.vec_encoding == vex_encoding_default)
11794 i.vec_encoding = vex_encoding_evex;
11795 else if (i.vec_encoding != vex_encoding_evex
11796 && i.vec_encoding != vex_encoding_evex512)
11797 goto unknown_vec_op;
11798
11799 i.broadcast.type = bcst_type;
11800 i.broadcast.operand = this_operand;
11801
11802 /* For .insn a data size specifier may be appended. */
11803 if (dot_insn () && *op_string == ':')
11804 goto dot_insn_modifier;
11805 }
11806 /* Check .insn special cases. */
11807 else if (dot_insn () && *op_string == ':')
11808 {
11809 dot_insn_modifier:
11810 switch (op_string[1])
11811 {
11812 unsigned long n;
11813
11814 case 'd':
11815 if (i.memshift < 32)
11816 goto duplicated_vec_op;
11817
11818 n = strtoul (op_string + 2, &end_op, 0);
11819 if (n)
11820 for (i.memshift = 0; !(n & 1); n >>= 1)
11821 ++i.memshift;
11822 if (i.memshift < 32 && n == 1)
11823 op_string = end_op;
11824 break;
11825
11826 case 's': case 'u':
11827 /* This isn't really a "vector" operation, but a sign/size
11828 specifier for immediate operands of .insn. Note that AT&T
11829 syntax handles the same in i386_immediate(). */
11830 if (!intel_syntax)
11831 break;
11832
11833 if (i.imm_bits[this_operand])
11834 goto duplicated_vec_op;
11835
11836 n = strtoul (op_string + 2, &end_op, 0);
11837 if (n && n <= (flag_code == CODE_64BIT ? 64 : 32))
11838 {
11839 i.imm_bits[this_operand] = n;
11840 if (op_string[1] == 's')
11841 i.flags[this_operand] |= Operand_Signed;
11842 op_string = end_op;
11843 }
11844 break;
11845 }
11846 }
11847 /* Check masking operation. */
11848 else if ((mask = parse_register (op_string, &end_op)) != NULL)
11849 {
11850 if (mask == &bad_reg)
11851 return NULL;
11852
11853 /* k0 can't be used for write mask. */
11854 if (mask->reg_type.bitfield.class != RegMask || !mask->reg_num)
11855 {
11856 as_bad (_("`%s%s' can't be used for write mask"),
11857 register_prefix, mask->reg_name);
11858 return NULL;
11859 }
11860
11861 if (!i.mask.reg)
11862 {
11863 i.mask.reg = mask;
11864 i.mask.operand = this_operand;
11865 }
11866 else if (i.mask.reg->reg_num)
11867 goto duplicated_vec_op;
11868 else
11869 {
11870 i.mask.reg = mask;
11871
11872 /* Only "{z}" is allowed here. No need to check
11873 zeroing mask explicitly. */
11874 if (i.mask.operand != (unsigned int) this_operand)
11875 {
11876 as_bad (_("invalid write mask `%s'"), saved);
11877 return NULL;
11878 }
11879 }
11880
11881 op_string = end_op;
11882 }
11883 /* Check zeroing-flag for masking operation. */
11884 else if (*op_string == 'z')
11885 {
11886 if (!i.mask.reg)
11887 {
11888 i.mask.reg = reg_k0;
11889 i.mask.zeroing = 1;
11890 i.mask.operand = this_operand;
11891 }
11892 else
11893 {
11894 if (i.mask.zeroing)
11895 {
11896 duplicated_vec_op:
11897 as_bad (_("duplicated `%s'"), saved);
11898 return NULL;
11899 }
11900
11901 i.mask.zeroing = 1;
11902
11903 /* Only "{%k}" is allowed here. No need to check mask
11904 register explicitly. */
11905 if (i.mask.operand != (unsigned int) this_operand)
11906 {
11907 as_bad (_("invalid zeroing-masking `%s'"),
11908 saved);
11909 return NULL;
11910 }
11911 }
11912
11913 op_string++;
11914 }
11915 else if (intel_syntax
11916 && (op_string = RC_SAE_specifier (op_string)) != NULL)
11917 i.rounding.modifier = true;
11918 else
11919 goto unknown_vec_op;
11920
11921 if (*op_string != '}')
11922 {
11923 as_bad (_("missing `}' in `%s'"), saved);
11924 return NULL;
11925 }
11926 op_string++;
11927
11928 /* Strip whitespace since the addition of pseudo prefixes
11929 changed how the scrubber treats '{'. */
11930 if (is_space_char (*op_string))
11931 ++op_string;
11932
11933 continue;
11934 }
11935 unknown_vec_op:
11936 /* We don't know this one. */
11937 as_bad (_("unknown vector operation: `%s'"), saved);
11938 return NULL;
11939 }
11940
11941 if (i.mask.reg && i.mask.zeroing && !i.mask.reg->reg_num)
11942 {
11943 as_bad (_("zeroing-masking only allowed with write mask"));
11944 return NULL;
11945 }
11946
11947 return op_string;
11948 }
11949
11950 static int
11951 i386_immediate (char *imm_start)
11952 {
11953 char *save_input_line_pointer;
11954 char *gotfree_input_line;
11955 segT exp_seg = 0;
11956 expressionS *exp;
11957 i386_operand_type types;
11958
11959 operand_type_set (&types, ~0);
11960
11961 if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
11962 {
11963 as_bad (_("at most %d immediate operands are allowed"),
11964 MAX_IMMEDIATE_OPERANDS);
11965 return 0;
11966 }
11967
11968 exp = &im_expressions[i.imm_operands++];
11969 i.op[this_operand].imms = exp;
11970
11971 if (is_space_char (*imm_start))
11972 ++imm_start;
11973
11974 save_input_line_pointer = input_line_pointer;
11975 input_line_pointer = imm_start;
11976
11977 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
11978 if (gotfree_input_line)
11979 input_line_pointer = gotfree_input_line;
11980
11981 expr_mode = expr_operator_none;
11982 exp_seg = expression (exp);
11983
11984 /* For .insn immediates there may be a size specifier. */
11985 if (dot_insn () && *input_line_pointer == '{' && input_line_pointer[1] == ':'
11986 && (input_line_pointer[2] == 's' || input_line_pointer[2] == 'u'))
11987 {
11988 char *e;
11989 unsigned long n = strtoul (input_line_pointer + 3, &e, 0);
11990
11991 if (*e == '}' && n && n <= (flag_code == CODE_64BIT ? 64 : 32))
11992 {
11993 i.imm_bits[this_operand] = n;
11994 if (input_line_pointer[2] == 's')
11995 i.flags[this_operand] |= Operand_Signed;
11996 input_line_pointer = e + 1;
11997 }
11998 }
11999
12000 SKIP_WHITESPACE ();
12001 if (*input_line_pointer)
12002 as_bad (_("junk `%s' after expression"), input_line_pointer);
12003
12004 input_line_pointer = save_input_line_pointer;
12005 if (gotfree_input_line)
12006 {
12007 free (gotfree_input_line);
12008
12009 if (exp->X_op == O_constant)
12010 exp->X_op = O_illegal;
12011 }
12012
12013 if (exp_seg == reg_section)
12014 {
12015 as_bad (_("illegal immediate register operand %s"), imm_start);
12016 return 0;
12017 }
12018
12019 return i386_finalize_immediate (exp_seg, exp, types, imm_start);
12020 }
12021
12022 static int
12023 i386_finalize_immediate (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
12024 i386_operand_type types, const char *imm_start)
12025 {
12026 if (exp->X_op == O_absent || exp->X_op == O_illegal || exp->X_op == O_big)
12027 {
12028 if (imm_start)
12029 as_bad (_("missing or invalid immediate expression `%s'"),
12030 imm_start);
12031 return 0;
12032 }
12033 else if (exp->X_op == O_constant)
12034 {
12035 /* Size it properly later. */
12036 i.types[this_operand].bitfield.imm64 = 1;
12037
12038 /* If not 64bit, sign/zero extend val, to account for wraparound
12039 when !BFD64. */
12040 if (expr_mode == expr_operator_present
12041 && flag_code != CODE_64BIT && !object_64bit)
12042 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
12043 }
12044 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
12045 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
12046 && exp_seg != absolute_section
12047 && exp_seg != text_section
12048 && exp_seg != data_section
12049 && exp_seg != bss_section
12050 && exp_seg != undefined_section
12051 && !bfd_is_com_section (exp_seg))
12052 {
12053 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
12054 return 0;
12055 }
12056 #endif
12057 else
12058 {
12059 /* This is an address. The size of the address will be
12060 determined later, depending on destination register,
12061 suffix, or the default for the section. */
12062 i.types[this_operand].bitfield.imm8 = 1;
12063 i.types[this_operand].bitfield.imm16 = 1;
12064 i.types[this_operand].bitfield.imm32 = 1;
12065 i.types[this_operand].bitfield.imm32s = 1;
12066 i.types[this_operand].bitfield.imm64 = 1;
12067 i.types[this_operand] = operand_type_and (i.types[this_operand],
12068 types);
12069 }
12070
12071 return 1;
12072 }
12073
12074 static char *
12075 i386_scale (char *scale)
12076 {
12077 offsetT val;
12078 char *save = input_line_pointer;
12079
12080 input_line_pointer = scale;
12081 val = get_absolute_expression ();
12082
12083 switch (val)
12084 {
12085 case 1:
12086 i.log2_scale_factor = 0;
12087 break;
12088 case 2:
12089 i.log2_scale_factor = 1;
12090 break;
12091 case 4:
12092 i.log2_scale_factor = 2;
12093 break;
12094 case 8:
12095 i.log2_scale_factor = 3;
12096 break;
12097 default:
12098 {
12099 char sep = *input_line_pointer;
12100
12101 *input_line_pointer = '\0';
12102 as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
12103 scale);
12104 *input_line_pointer = sep;
12105 input_line_pointer = save;
12106 return NULL;
12107 }
12108 }
12109 if (i.log2_scale_factor != 0 && i.index_reg == 0)
12110 {
12111 as_warn (_("scale factor of %d without an index register"),
12112 1 << i.log2_scale_factor);
12113 i.log2_scale_factor = 0;
12114 }
12115 scale = input_line_pointer;
12116 input_line_pointer = save;
12117 return scale;
12118 }
12119
12120 static int
12121 i386_displacement (char *disp_start, char *disp_end)
12122 {
12123 expressionS *exp;
12124 segT exp_seg = 0;
12125 char *save_input_line_pointer;
12126 char *gotfree_input_line;
12127 int override;
12128 i386_operand_type bigdisp, types = anydisp;
12129 int ret;
12130
12131 if (i.disp_operands == MAX_MEMORY_OPERANDS)
12132 {
12133 as_bad (_("at most %d displacement operands are allowed"),
12134 MAX_MEMORY_OPERANDS);
12135 return 0;
12136 }
12137
12138 operand_type_set (&bigdisp, 0);
12139 if (i.jumpabsolute
12140 || i.types[this_operand].bitfield.baseindex
12141 || (current_templates->start->opcode_modifier.jump != JUMP
12142 && current_templates->start->opcode_modifier.jump != JUMP_DWORD))
12143 {
12144 i386_addressing_mode ();
12145 override = (i.prefix[ADDR_PREFIX] != 0);
12146 if (flag_code == CODE_64BIT)
12147 {
12148 bigdisp.bitfield.disp32 = 1;
12149 if (!override)
12150 bigdisp.bitfield.disp64 = 1;
12151 }
12152 else if ((flag_code == CODE_16BIT) ^ override)
12153 bigdisp.bitfield.disp16 = 1;
12154 else
12155 bigdisp.bitfield.disp32 = 1;
12156 }
12157 else
12158 {
12159 /* For PC-relative branches, the width of the displacement may be
12160 dependent upon data size, but is never dependent upon address size.
12161 Also make sure to not unintentionally match against a non-PC-relative
12162 branch template. */
12163 static templates aux_templates;
12164 const insn_template *t = current_templates->start;
12165 bool has_intel64 = false;
12166
12167 aux_templates.start = t;
12168 while (++t < current_templates->end)
12169 {
12170 if (t->opcode_modifier.jump
12171 != current_templates->start->opcode_modifier.jump)
12172 break;
12173 if ((t->opcode_modifier.isa64 >= INTEL64))
12174 has_intel64 = true;
12175 }
12176 if (t < current_templates->end)
12177 {
12178 aux_templates.end = t;
12179 current_templates = &aux_templates;
12180 }
12181
12182 override = (i.prefix[DATA_PREFIX] != 0);
12183 if (flag_code == CODE_64BIT)
12184 {
12185 if ((override || i.suffix == WORD_MNEM_SUFFIX)
12186 && (!intel64 || !has_intel64))
12187 bigdisp.bitfield.disp16 = 1;
12188 else
12189 bigdisp.bitfield.disp32 = 1;
12190 }
12191 else
12192 {
12193 if (!override)
12194 override = (i.suffix == (flag_code != CODE_16BIT
12195 ? WORD_MNEM_SUFFIX
12196 : LONG_MNEM_SUFFIX));
12197 bigdisp.bitfield.disp32 = 1;
12198 if ((flag_code == CODE_16BIT) ^ override)
12199 {
12200 bigdisp.bitfield.disp32 = 0;
12201 bigdisp.bitfield.disp16 = 1;
12202 }
12203 }
12204 }
12205 i.types[this_operand] = operand_type_or (i.types[this_operand],
12206 bigdisp);
12207
12208 exp = &disp_expressions[i.disp_operands];
12209 i.op[this_operand].disps = exp;
12210 i.disp_operands++;
12211 save_input_line_pointer = input_line_pointer;
12212 input_line_pointer = disp_start;
12213 END_STRING_AND_SAVE (disp_end);
12214
12215 #ifndef GCC_ASM_O_HACK
12216 #define GCC_ASM_O_HACK 0
12217 #endif
12218 #if GCC_ASM_O_HACK
12219 END_STRING_AND_SAVE (disp_end + 1);
12220 if (i.types[this_operand].bitfield.baseIndex
12221 && displacement_string_end[-1] == '+')
12222 {
12223 /* This hack is to avoid a warning when using the "o"
12224 constraint within gcc asm statements.
12225 For instance:
12226
12227 #define _set_tssldt_desc(n,addr,limit,type) \
12228 __asm__ __volatile__ ( \
12229 "movw %w2,%0\n\t" \
12230 "movw %w1,2+%0\n\t" \
12231 "rorl $16,%1\n\t" \
12232 "movb %b1,4+%0\n\t" \
12233 "movb %4,5+%0\n\t" \
12234 "movb $0,6+%0\n\t" \
12235 "movb %h1,7+%0\n\t" \
12236 "rorl $16,%1" \
12237 : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
12238
12239 This works great except that the output assembler ends
12240 up looking a bit weird if it turns out that there is
12241 no offset. You end up producing code that looks like:
12242
12243 #APP
12244 movw $235,(%eax)
12245 movw %dx,2+(%eax)
12246 rorl $16,%edx
12247 movb %dl,4+(%eax)
12248 movb $137,5+(%eax)
12249 movb $0,6+(%eax)
12250 movb %dh,7+(%eax)
12251 rorl $16,%edx
12252 #NO_APP
12253
12254 So here we provide the missing zero. */
12255
12256 *displacement_string_end = '0';
12257 }
12258 #endif
12259 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
12260 if (gotfree_input_line)
12261 input_line_pointer = gotfree_input_line;
12262
12263 expr_mode = expr_operator_none;
12264 exp_seg = expression (exp);
12265
12266 SKIP_WHITESPACE ();
12267 if (*input_line_pointer)
12268 as_bad (_("junk `%s' after expression"), input_line_pointer);
12269 #if GCC_ASM_O_HACK
12270 RESTORE_END_STRING (disp_end + 1);
12271 #endif
12272 input_line_pointer = save_input_line_pointer;
12273 if (gotfree_input_line)
12274 {
12275 free (gotfree_input_line);
12276
12277 if (exp->X_op == O_constant || exp->X_op == O_register)
12278 exp->X_op = O_illegal;
12279 }
12280
12281 ret = i386_finalize_displacement (exp_seg, exp, types, disp_start);
12282
12283 RESTORE_END_STRING (disp_end);
12284
12285 return ret;
12286 }
12287
12288 static int
12289 i386_finalize_displacement (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
12290 i386_operand_type types, const char *disp_start)
12291 {
12292 int ret = 1;
12293
12294 /* We do this to make sure that the section symbol is in
12295 the symbol table. We will ultimately change the relocation
12296 to be relative to the beginning of the section. */
12297 if (i.reloc[this_operand] == BFD_RELOC_386_GOTOFF
12298 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
12299 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
12300 {
12301 if (exp->X_op != O_symbol)
12302 goto inv_disp;
12303
12304 if (S_IS_LOCAL (exp->X_add_symbol)
12305 && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section
12306 && S_GET_SEGMENT (exp->X_add_symbol) != expr_section)
12307 section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
12308 exp->X_op = O_subtract;
12309 exp->X_op_symbol = GOT_symbol;
12310 if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
12311 i.reloc[this_operand] = BFD_RELOC_32_PCREL;
12312 else if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
12313 i.reloc[this_operand] = BFD_RELOC_64;
12314 else
12315 i.reloc[this_operand] = BFD_RELOC_32;
12316 }
12317
12318 else if (exp->X_op == O_absent
12319 || exp->X_op == O_illegal
12320 || exp->X_op == O_big)
12321 {
12322 inv_disp:
12323 as_bad (_("missing or invalid displacement expression `%s'"),
12324 disp_start);
12325 ret = 0;
12326 }
12327
12328 else if (exp->X_op == O_constant)
12329 {
12330 /* Sizing gets taken care of by optimize_disp().
12331
12332 If not 64bit, sign/zero extend val, to account for wraparound
12333 when !BFD64. */
12334 if (expr_mode == expr_operator_present
12335 && flag_code != CODE_64BIT && !object_64bit)
12336 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
12337 }
12338
12339 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
12340 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
12341 && exp_seg != absolute_section
12342 && exp_seg != text_section
12343 && exp_seg != data_section
12344 && exp_seg != bss_section
12345 && exp_seg != undefined_section
12346 && !bfd_is_com_section (exp_seg))
12347 {
12348 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
12349 ret = 0;
12350 }
12351 #endif
12352
12353 else if (current_templates->start->opcode_modifier.jump == JUMP_BYTE)
12354 i.types[this_operand].bitfield.disp8 = 1;
12355
12356 /* Check if this is a displacement only operand. */
12357 if (!i.types[this_operand].bitfield.baseindex)
12358 i.types[this_operand] =
12359 operand_type_or (operand_type_and_not (i.types[this_operand], anydisp),
12360 operand_type_and (i.types[this_operand], types));
12361
12362 return ret;
12363 }
12364
12365 /* Return the active addressing mode, taking address override and
12366 registers forming the address into consideration. Update the
12367 address override prefix if necessary. */
12368
12369 static enum flag_code
12370 i386_addressing_mode (void)
12371 {
12372 enum flag_code addr_mode;
12373
12374 if (i.prefix[ADDR_PREFIX])
12375 addr_mode = flag_code == CODE_32BIT ? CODE_16BIT : CODE_32BIT;
12376 else if (flag_code == CODE_16BIT
12377 && is_cpu (current_templates->start, CpuMPX)
12378 /* Avoid replacing the "16-bit addressing not allowed" diagnostic
12379 from md_assemble() by "is not a valid base/index expression"
12380 when there is a base and/or index. */
12381 && !i.types[this_operand].bitfield.baseindex)
12382 {
12383 /* MPX insn memory operands with neither base nor index must be forced
12384 to use 32-bit addressing in 16-bit mode. */
12385 addr_mode = CODE_32BIT;
12386 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
12387 ++i.prefixes;
12388 gas_assert (!i.types[this_operand].bitfield.disp16);
12389 gas_assert (!i.types[this_operand].bitfield.disp32);
12390 }
12391 else
12392 {
12393 addr_mode = flag_code;
12394
12395 #if INFER_ADDR_PREFIX
12396 if (i.mem_operands == 0)
12397 {
12398 /* Infer address prefix from the first memory operand. */
12399 const reg_entry *addr_reg = i.base_reg;
12400
12401 if (addr_reg == NULL)
12402 addr_reg = i.index_reg;
12403
12404 if (addr_reg)
12405 {
12406 if (addr_reg->reg_type.bitfield.dword)
12407 addr_mode = CODE_32BIT;
12408 else if (flag_code != CODE_64BIT
12409 && addr_reg->reg_type.bitfield.word)
12410 addr_mode = CODE_16BIT;
12411
12412 if (addr_mode != flag_code)
12413 {
12414 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
12415 i.prefixes += 1;
12416 /* Change the size of any displacement too. At most one
12417 of Disp16 or Disp32 is set.
12418 FIXME. There doesn't seem to be any real need for
12419 separate Disp16 and Disp32 flags. The same goes for
12420 Imm16 and Imm32. Removing them would probably clean
12421 up the code quite a lot. */
12422 if (flag_code != CODE_64BIT
12423 && (i.types[this_operand].bitfield.disp16
12424 || i.types[this_operand].bitfield.disp32))
12425 {
12426 static const i386_operand_type disp16_32 = {
12427 .bitfield = { .disp16 = 1, .disp32 = 1 }
12428 };
12429
12430 i.types[this_operand]
12431 = operand_type_xor (i.types[this_operand], disp16_32);
12432 }
12433 }
12434 }
12435 }
12436 #endif
12437 }
12438
12439 return addr_mode;
12440 }
12441
12442 /* Make sure the memory operand we've been dealt is valid.
12443 Return 1 on success, 0 on a failure. */
12444
12445 static int
12446 i386_index_check (const char *operand_string)
12447 {
12448 const char *kind = "base/index";
12449 enum flag_code addr_mode = i386_addressing_mode ();
12450 const insn_template *t = current_templates->end - 1;
12451
12452 if (t->opcode_modifier.isstring)
12453 {
12454 /* Memory operands of string insns are special in that they only allow
12455 a single register (rDI, rSI, or rBX) as their memory address. */
12456 const reg_entry *expected_reg;
12457 static const char di_si[][2][4] =
12458 {
12459 { "esi", "edi" },
12460 { "si", "di" },
12461 { "rsi", "rdi" }
12462 };
12463 static const char bx[][4] = { "ebx", "bx", "rbx" };
12464
12465 kind = "string address";
12466
12467 if (t->opcode_modifier.prefixok == PrefixRep)
12468 {
12469 int es_op = t->opcode_modifier.isstring - IS_STRING_ES_OP0;
12470 int op = 0;
12471
12472 if (!t->operand_types[0].bitfield.baseindex
12473 || ((!i.mem_operands != !intel_syntax)
12474 && t->operand_types[1].bitfield.baseindex))
12475 op = 1;
12476 expected_reg
12477 = (const reg_entry *) str_hash_find (reg_hash,
12478 di_si[addr_mode][op == es_op]);
12479 }
12480 else
12481 expected_reg
12482 = (const reg_entry *)str_hash_find (reg_hash, bx[addr_mode]);
12483
12484 if (i.base_reg != expected_reg
12485 || i.index_reg
12486 || operand_type_check (i.types[this_operand], disp))
12487 {
12488 /* The second memory operand must have the same size as
12489 the first one. */
12490 if (i.mem_operands
12491 && i.base_reg
12492 && !((addr_mode == CODE_64BIT
12493 && i.base_reg->reg_type.bitfield.qword)
12494 || (addr_mode == CODE_32BIT
12495 ? i.base_reg->reg_type.bitfield.dword
12496 : i.base_reg->reg_type.bitfield.word)))
12497 goto bad_address;
12498
12499 as_warn (_("`%s' is not valid here (expected `%c%s%s%c')"),
12500 operand_string,
12501 intel_syntax ? '[' : '(',
12502 register_prefix,
12503 expected_reg->reg_name,
12504 intel_syntax ? ']' : ')');
12505 return 1;
12506 }
12507 else
12508 return 1;
12509
12510 bad_address:
12511 as_bad (_("`%s' is not a valid %s expression"),
12512 operand_string, kind);
12513 return 0;
12514 }
12515 else
12516 {
12517 t = current_templates->start;
12518
12519 if (addr_mode != CODE_16BIT)
12520 {
12521 /* 32-bit/64-bit checks. */
12522 if (i.disp_encoding == disp_encoding_16bit)
12523 {
12524 bad_disp:
12525 as_bad (_("invalid `%s' prefix"),
12526 addr_mode == CODE_16BIT ? "{disp32}" : "{disp16}");
12527 return 0;
12528 }
12529
12530 if ((i.base_reg
12531 && ((addr_mode == CODE_64BIT
12532 ? !i.base_reg->reg_type.bitfield.qword
12533 : !i.base_reg->reg_type.bitfield.dword)
12534 || (i.index_reg && i.base_reg->reg_num == RegIP)
12535 || i.base_reg->reg_num == RegIZ))
12536 || (i.index_reg
12537 && !i.index_reg->reg_type.bitfield.xmmword
12538 && !i.index_reg->reg_type.bitfield.ymmword
12539 && !i.index_reg->reg_type.bitfield.zmmword
12540 && ((addr_mode == CODE_64BIT
12541 ? !i.index_reg->reg_type.bitfield.qword
12542 : !i.index_reg->reg_type.bitfield.dword)
12543 || !i.index_reg->reg_type.bitfield.baseindex)))
12544 goto bad_address;
12545
12546 /* bndmk, bndldx, bndstx and mandatory non-vector SIB have special restrictions. */
12547 if (t->mnem_off == MN_bndmk
12548 || t->mnem_off == MN_bndldx
12549 || t->mnem_off == MN_bndstx
12550 || t->opcode_modifier.sib == SIBMEM)
12551 {
12552 /* They cannot use RIP-relative addressing. */
12553 if (i.base_reg && i.base_reg->reg_num == RegIP)
12554 {
12555 as_bad (_("`%s' cannot be used here"), operand_string);
12556 return 0;
12557 }
12558
12559 /* bndldx and bndstx ignore their scale factor. */
12560 if ((t->mnem_off == MN_bndldx || t->mnem_off == MN_bndstx)
12561 && i.log2_scale_factor)
12562 as_warn (_("register scaling is being ignored here"));
12563 }
12564 }
12565 else
12566 {
12567 /* 16-bit checks. */
12568 if (i.disp_encoding == disp_encoding_32bit)
12569 goto bad_disp;
12570
12571 if ((i.base_reg
12572 && (!i.base_reg->reg_type.bitfield.word
12573 || !i.base_reg->reg_type.bitfield.baseindex))
12574 || (i.index_reg
12575 && (!i.index_reg->reg_type.bitfield.word
12576 || !i.index_reg->reg_type.bitfield.baseindex
12577 || !(i.base_reg
12578 && i.base_reg->reg_num < 6
12579 && i.index_reg->reg_num >= 6
12580 && i.log2_scale_factor == 0))))
12581 goto bad_address;
12582 }
12583 }
12584 return 1;
12585 }
12586
12587 /* Handle vector immediates. */
12588
12589 static int
12590 RC_SAE_immediate (const char *imm_start)
12591 {
12592 const char *pstr = imm_start;
12593
12594 if (*pstr != '{')
12595 return 0;
12596
12597 pstr = RC_SAE_specifier (pstr + 1);
12598 if (pstr == NULL)
12599 return 0;
12600
12601 if (*pstr++ != '}')
12602 {
12603 as_bad (_("Missing '}': '%s'"), imm_start);
12604 return 0;
12605 }
12606 /* RC/SAE immediate string should contain nothing more. */;
12607 if (*pstr != 0)
12608 {
12609 as_bad (_("Junk after '}': '%s'"), imm_start);
12610 return 0;
12611 }
12612
12613 /* Internally this doesn't count as an operand. */
12614 --i.operands;
12615
12616 return 1;
12617 }
12618
12619 static INLINE bool starts_memory_operand (char c)
12620 {
12621 return ISDIGIT (c)
12622 || is_name_beginner (c)
12623 || strchr ("([\"+-!~", c);
12624 }
12625
12626 /* Parse OPERAND_STRING into the i386_insn structure I. Returns zero
12627 on error. */
12628
12629 static int
12630 i386_att_operand (char *operand_string)
12631 {
12632 const reg_entry *r;
12633 char *end_op;
12634 char *op_string = operand_string;
12635
12636 if (is_space_char (*op_string))
12637 ++op_string;
12638
12639 /* We check for an absolute prefix (differentiating,
12640 for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
12641 if (*op_string == ABSOLUTE_PREFIX
12642 && current_templates->start->opcode_modifier.jump)
12643 {
12644 ++op_string;
12645 if (is_space_char (*op_string))
12646 ++op_string;
12647 i.jumpabsolute = true;
12648 }
12649
12650 /* Check if operand is a register. */
12651 if ((r = parse_register (op_string, &end_op)) != NULL)
12652 {
12653 i386_operand_type temp;
12654
12655 if (r == &bad_reg)
12656 return 0;
12657
12658 /* Check for a segment override by searching for ':' after a
12659 segment register. */
12660 op_string = end_op;
12661 if (is_space_char (*op_string))
12662 ++op_string;
12663 if (*op_string == ':' && r->reg_type.bitfield.class == SReg)
12664 {
12665 i.seg[i.mem_operands] = r;
12666
12667 /* Skip the ':' and whitespace. */
12668 ++op_string;
12669 if (is_space_char (*op_string))
12670 ++op_string;
12671
12672 /* Handle case of %es:*foo. */
12673 if (!i.jumpabsolute && *op_string == ABSOLUTE_PREFIX
12674 && current_templates->start->opcode_modifier.jump)
12675 {
12676 ++op_string;
12677 if (is_space_char (*op_string))
12678 ++op_string;
12679 i.jumpabsolute = true;
12680 }
12681
12682 if (!starts_memory_operand (*op_string))
12683 {
12684 as_bad (_("bad memory operand `%s'"), op_string);
12685 return 0;
12686 }
12687 goto do_memory_reference;
12688 }
12689
12690 /* Handle vector operations. */
12691 if (*op_string == '{')
12692 {
12693 op_string = check_VecOperations (op_string);
12694 if (op_string == NULL)
12695 return 0;
12696 }
12697
12698 if (*op_string)
12699 {
12700 as_bad (_("junk `%s' after register"), op_string);
12701 return 0;
12702 }
12703
12704 /* Reject pseudo registers for .insn. */
12705 if (dot_insn () && r->reg_type.bitfield.class == ClassNone)
12706 {
12707 as_bad (_("`%s%s' cannot be used here"),
12708 register_prefix, r->reg_name);
12709 return 0;
12710 }
12711
12712 temp = r->reg_type;
12713 temp.bitfield.baseindex = 0;
12714 i.types[this_operand] = operand_type_or (i.types[this_operand],
12715 temp);
12716 i.types[this_operand].bitfield.unspecified = 0;
12717 i.op[this_operand].regs = r;
12718 i.reg_operands++;
12719
12720 /* A GPR may follow an RC or SAE immediate only if a (vector) register
12721 operand was also present earlier on. */
12722 if (i.rounding.type != rc_none && temp.bitfield.class == Reg
12723 && i.reg_operands == 1)
12724 {
12725 unsigned int j;
12726
12727 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); ++j)
12728 if (i.rounding.type == RC_NamesTable[j].type)
12729 break;
12730 as_bad (_("`%s': misplaced `{%s}'"),
12731 insn_name (current_templates->start), RC_NamesTable[j].name);
12732 return 0;
12733 }
12734 }
12735 else if (*op_string == REGISTER_PREFIX)
12736 {
12737 as_bad (_("bad register name `%s'"), op_string);
12738 return 0;
12739 }
12740 else if (*op_string == IMMEDIATE_PREFIX)
12741 {
12742 ++op_string;
12743 if (i.jumpabsolute)
12744 {
12745 as_bad (_("immediate operand illegal with absolute jump"));
12746 return 0;
12747 }
12748 if (!i386_immediate (op_string))
12749 return 0;
12750 if (i.rounding.type != rc_none)
12751 {
12752 as_bad (_("`%s': RC/SAE operand must follow immediate operands"),
12753 insn_name (current_templates->start));
12754 return 0;
12755 }
12756 }
12757 else if (RC_SAE_immediate (operand_string))
12758 {
12759 /* If it is a RC or SAE immediate, do the necessary placement check:
12760 Only another immediate or a GPR may precede it. */
12761 if (i.mem_operands || i.reg_operands + i.imm_operands > 1
12762 || (i.reg_operands == 1
12763 && i.op[0].regs->reg_type.bitfield.class != Reg))
12764 {
12765 as_bad (_("`%s': misplaced `%s'"),
12766 insn_name (current_templates->start), operand_string);
12767 return 0;
12768 }
12769 }
12770 else if (starts_memory_operand (*op_string))
12771 {
12772 /* This is a memory reference of some sort. */
12773 char *base_string;
12774
12775 /* Start and end of displacement string expression (if found). */
12776 char *displacement_string_start;
12777 char *displacement_string_end;
12778
12779 do_memory_reference:
12780 /* Check for base index form. We detect the base index form by
12781 looking for an ')' at the end of the operand, searching
12782 for the '(' matching it, and finding a REGISTER_PREFIX or ','
12783 after the '('. */
12784 base_string = op_string + strlen (op_string);
12785
12786 /* Handle vector operations. */
12787 --base_string;
12788 if (is_space_char (*base_string))
12789 --base_string;
12790
12791 if (*base_string == '}')
12792 {
12793 char *vop_start = NULL;
12794
12795 while (base_string-- > op_string)
12796 {
12797 if (*base_string == '"')
12798 break;
12799 if (*base_string != '{')
12800 continue;
12801
12802 vop_start = base_string;
12803
12804 --base_string;
12805 if (is_space_char (*base_string))
12806 --base_string;
12807
12808 if (*base_string != '}')
12809 break;
12810
12811 vop_start = NULL;
12812 }
12813
12814 if (!vop_start)
12815 {
12816 as_bad (_("unbalanced figure braces"));
12817 return 0;
12818 }
12819
12820 if (check_VecOperations (vop_start) == NULL)
12821 return 0;
12822 }
12823
12824 /* If we only have a displacement, set-up for it to be parsed later. */
12825 displacement_string_start = op_string;
12826 displacement_string_end = base_string + 1;
12827
12828 if (*base_string == ')')
12829 {
12830 char *temp_string;
12831 unsigned int parens_not_balanced = 0;
12832 bool in_quotes = false;
12833
12834 /* We've already checked that the number of left & right ()'s are
12835 equal, and that there's a matching set of double quotes. */
12836 end_op = base_string;
12837 for (temp_string = op_string; temp_string < end_op; temp_string++)
12838 {
12839 if (*temp_string == '\\' && temp_string[1] == '"')
12840 ++temp_string;
12841 else if (*temp_string == '"')
12842 in_quotes = !in_quotes;
12843 else if (!in_quotes)
12844 {
12845 if (*temp_string == '(' && !parens_not_balanced++)
12846 base_string = temp_string;
12847 if (*temp_string == ')')
12848 --parens_not_balanced;
12849 }
12850 }
12851
12852 temp_string = base_string;
12853
12854 /* Skip past '(' and whitespace. */
12855 gas_assert (*base_string == '(');
12856 ++base_string;
12857 if (is_space_char (*base_string))
12858 ++base_string;
12859
12860 if (*base_string == ','
12861 || ((i.base_reg = parse_register (base_string, &end_op))
12862 != NULL))
12863 {
12864 displacement_string_end = temp_string;
12865
12866 i.types[this_operand].bitfield.baseindex = 1;
12867
12868 if (i.base_reg)
12869 {
12870 if (i.base_reg == &bad_reg)
12871 return 0;
12872 base_string = end_op;
12873 if (is_space_char (*base_string))
12874 ++base_string;
12875 }
12876
12877 /* There may be an index reg or scale factor here. */
12878 if (*base_string == ',')
12879 {
12880 ++base_string;
12881 if (is_space_char (*base_string))
12882 ++base_string;
12883
12884 if ((i.index_reg = parse_register (base_string, &end_op))
12885 != NULL)
12886 {
12887 if (i.index_reg == &bad_reg)
12888 return 0;
12889 base_string = end_op;
12890 if (is_space_char (*base_string))
12891 ++base_string;
12892 if (*base_string == ',')
12893 {
12894 ++base_string;
12895 if (is_space_char (*base_string))
12896 ++base_string;
12897 }
12898 else if (*base_string != ')')
12899 {
12900 as_bad (_("expecting `,' or `)' "
12901 "after index register in `%s'"),
12902 operand_string);
12903 return 0;
12904 }
12905 }
12906 else if (*base_string == REGISTER_PREFIX)
12907 {
12908 end_op = strchr (base_string, ',');
12909 if (end_op)
12910 *end_op = '\0';
12911 as_bad (_("bad register name `%s'"), base_string);
12912 return 0;
12913 }
12914
12915 /* Check for scale factor. */
12916 if (*base_string != ')')
12917 {
12918 char *end_scale = i386_scale (base_string);
12919
12920 if (!end_scale)
12921 return 0;
12922
12923 base_string = end_scale;
12924 if (is_space_char (*base_string))
12925 ++base_string;
12926 if (*base_string != ')')
12927 {
12928 as_bad (_("expecting `)' "
12929 "after scale factor in `%s'"),
12930 operand_string);
12931 return 0;
12932 }
12933 }
12934 else if (!i.index_reg)
12935 {
12936 as_bad (_("expecting index register or scale factor "
12937 "after `,'; got '%c'"),
12938 *base_string);
12939 return 0;
12940 }
12941 }
12942 else if (*base_string != ')')
12943 {
12944 as_bad (_("expecting `,' or `)' "
12945 "after base register in `%s'"),
12946 operand_string);
12947 return 0;
12948 }
12949 }
12950 else if (*base_string == REGISTER_PREFIX)
12951 {
12952 end_op = strchr (base_string, ',');
12953 if (end_op)
12954 *end_op = '\0';
12955 as_bad (_("bad register name `%s'"), base_string);
12956 return 0;
12957 }
12958 }
12959
12960 /* If there's an expression beginning the operand, parse it,
12961 assuming displacement_string_start and
12962 displacement_string_end are meaningful. */
12963 if (displacement_string_start != displacement_string_end)
12964 {
12965 if (!i386_displacement (displacement_string_start,
12966 displacement_string_end))
12967 return 0;
12968 }
12969
12970 /* Special case for (%dx) while doing input/output op. */
12971 if (i.base_reg
12972 && i.base_reg->reg_type.bitfield.instance == RegD
12973 && i.base_reg->reg_type.bitfield.word
12974 && i.index_reg == 0
12975 && i.log2_scale_factor == 0
12976 && i.seg[i.mem_operands] == 0
12977 && !operand_type_check (i.types[this_operand], disp))
12978 {
12979 i.types[this_operand] = i.base_reg->reg_type;
12980 i.input_output_operand = true;
12981 return 1;
12982 }
12983
12984 if (i386_index_check (operand_string) == 0)
12985 return 0;
12986 i.flags[this_operand] |= Operand_Mem;
12987 i.mem_operands++;
12988 }
12989 else
12990 {
12991 /* It's not a memory operand; argh! */
12992 as_bad (_("invalid char %s beginning operand %d `%s'"),
12993 output_invalid (*op_string),
12994 this_operand + 1,
12995 op_string);
12996 return 0;
12997 }
12998 return 1; /* Normal return. */
12999 }
13000 \f
13001 /* Calculate the maximum variable size (i.e., excluding fr_fix)
13002 that an rs_machine_dependent frag may reach. */
13003
13004 unsigned int
13005 i386_frag_max_var (fragS *frag)
13006 {
13007 /* The only relaxable frags are for jumps.
13008 Unconditional jumps can grow by 4 bytes and others by 5 bytes. */
13009 gas_assert (frag->fr_type == rs_machine_dependent);
13010 return TYPE_FROM_RELAX_STATE (frag->fr_subtype) == UNCOND_JUMP ? 4 : 5;
13011 }
13012
13013 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13014 static int
13015 elf_symbol_resolved_in_segment_p (symbolS *fr_symbol, offsetT fr_var)
13016 {
13017 /* STT_GNU_IFUNC symbol must go through PLT. */
13018 if ((symbol_get_bfdsym (fr_symbol)->flags
13019 & BSF_GNU_INDIRECT_FUNCTION) != 0)
13020 return 0;
13021
13022 if (!S_IS_EXTERNAL (fr_symbol))
13023 /* Symbol may be weak or local. */
13024 return !S_IS_WEAK (fr_symbol);
13025
13026 /* Global symbols with non-default visibility can't be preempted. */
13027 if (ELF_ST_VISIBILITY (S_GET_OTHER (fr_symbol)) != STV_DEFAULT)
13028 return 1;
13029
13030 if (fr_var != NO_RELOC)
13031 switch ((enum bfd_reloc_code_real) fr_var)
13032 {
13033 case BFD_RELOC_386_PLT32:
13034 case BFD_RELOC_X86_64_PLT32:
13035 /* Symbol with PLT relocation may be preempted. */
13036 return 0;
13037 default:
13038 abort ();
13039 }
13040
13041 /* Global symbols with default visibility in a shared library may be
13042 preempted by another definition. */
13043 return !shared;
13044 }
13045 #endif
13046
13047 /* Table 3-2. Macro-Fusible Instructions in Haswell Microarchitecture
13048 Note also work for Skylake and Cascadelake.
13049 ---------------------------------------------------------------------
13050 | JCC | ADD/SUB/CMP | INC/DEC | TEST/AND |
13051 | ------ | ----------- | ------- | -------- |
13052 | Jo | N | N | Y |
13053 | Jno | N | N | Y |
13054 | Jc/Jb | Y | N | Y |
13055 | Jae/Jnb | Y | N | Y |
13056 | Je/Jz | Y | Y | Y |
13057 | Jne/Jnz | Y | Y | Y |
13058 | Jna/Jbe | Y | N | Y |
13059 | Ja/Jnbe | Y | N | Y |
13060 | Js | N | N | Y |
13061 | Jns | N | N | Y |
13062 | Jp/Jpe | N | N | Y |
13063 | Jnp/Jpo | N | N | Y |
13064 | Jl/Jnge | Y | Y | Y |
13065 | Jge/Jnl | Y | Y | Y |
13066 | Jle/Jng | Y | Y | Y |
13067 | Jg/Jnle | Y | Y | Y |
13068 --------------------------------------------------------------------- */
13069 static int
13070 i386_macro_fusible_p (enum mf_cmp_kind mf_cmp, enum mf_jcc_kind mf_jcc)
13071 {
13072 if (mf_cmp == mf_cmp_alu_cmp)
13073 return ((mf_jcc >= mf_jcc_jc && mf_jcc <= mf_jcc_jna)
13074 || mf_jcc == mf_jcc_jl || mf_jcc == mf_jcc_jle);
13075 if (mf_cmp == mf_cmp_incdec)
13076 return (mf_jcc == mf_jcc_je || mf_jcc == mf_jcc_jl
13077 || mf_jcc == mf_jcc_jle);
13078 if (mf_cmp == mf_cmp_test_and)
13079 return 1;
13080 return 0;
13081 }
13082
13083 /* Return the next non-empty frag. */
13084
13085 static fragS *
13086 i386_next_non_empty_frag (fragS *fragP)
13087 {
13088 /* There may be a frag with a ".fill 0" when there is no room in
13089 the current frag for frag_grow in output_insn. */
13090 for (fragP = fragP->fr_next;
13091 (fragP != NULL
13092 && fragP->fr_type == rs_fill
13093 && fragP->fr_fix == 0);
13094 fragP = fragP->fr_next)
13095 ;
13096 return fragP;
13097 }
13098
13099 /* Return the next jcc frag after BRANCH_PADDING. */
13100
13101 static fragS *
13102 i386_next_fusible_jcc_frag (fragS *maybe_cmp_fragP, fragS *pad_fragP)
13103 {
13104 fragS *branch_fragP;
13105 if (!pad_fragP)
13106 return NULL;
13107
13108 if (pad_fragP->fr_type == rs_machine_dependent
13109 && (TYPE_FROM_RELAX_STATE (pad_fragP->fr_subtype)
13110 == BRANCH_PADDING))
13111 {
13112 branch_fragP = i386_next_non_empty_frag (pad_fragP);
13113 if (branch_fragP->fr_type != rs_machine_dependent)
13114 return NULL;
13115 if (TYPE_FROM_RELAX_STATE (branch_fragP->fr_subtype) == COND_JUMP
13116 && i386_macro_fusible_p (maybe_cmp_fragP->tc_frag_data.mf_type,
13117 pad_fragP->tc_frag_data.mf_type))
13118 return branch_fragP;
13119 }
13120
13121 return NULL;
13122 }
13123
13124 /* Classify BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags. */
13125
13126 static void
13127 i386_classify_machine_dependent_frag (fragS *fragP)
13128 {
13129 fragS *cmp_fragP;
13130 fragS *pad_fragP;
13131 fragS *branch_fragP;
13132 fragS *next_fragP;
13133 unsigned int max_prefix_length;
13134
13135 if (fragP->tc_frag_data.classified)
13136 return;
13137
13138 /* First scan for BRANCH_PADDING and FUSED_JCC_PADDING. Convert
13139 FUSED_JCC_PADDING and merge BRANCH_PADDING. */
13140 for (next_fragP = fragP;
13141 next_fragP != NULL;
13142 next_fragP = next_fragP->fr_next)
13143 {
13144 next_fragP->tc_frag_data.classified = 1;
13145 if (next_fragP->fr_type == rs_machine_dependent)
13146 switch (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype))
13147 {
13148 case BRANCH_PADDING:
13149 /* The BRANCH_PADDING frag must be followed by a branch
13150 frag. */
13151 branch_fragP = i386_next_non_empty_frag (next_fragP);
13152 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
13153 break;
13154 case FUSED_JCC_PADDING:
13155 /* Check if this is a fused jcc:
13156 FUSED_JCC_PADDING
13157 CMP like instruction
13158 BRANCH_PADDING
13159 COND_JUMP
13160 */
13161 cmp_fragP = i386_next_non_empty_frag (next_fragP);
13162 pad_fragP = i386_next_non_empty_frag (cmp_fragP);
13163 branch_fragP = i386_next_fusible_jcc_frag (next_fragP, pad_fragP);
13164 if (branch_fragP)
13165 {
13166 /* The BRANCH_PADDING frag is merged with the
13167 FUSED_JCC_PADDING frag. */
13168 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
13169 /* CMP like instruction size. */
13170 next_fragP->tc_frag_data.cmp_size = cmp_fragP->fr_fix;
13171 frag_wane (pad_fragP);
13172 /* Skip to branch_fragP. */
13173 next_fragP = branch_fragP;
13174 }
13175 else if (next_fragP->tc_frag_data.max_prefix_length)
13176 {
13177 /* Turn FUSED_JCC_PADDING into BRANCH_PREFIX if it isn't
13178 a fused jcc. */
13179 next_fragP->fr_subtype
13180 = ENCODE_RELAX_STATE (BRANCH_PREFIX, 0);
13181 next_fragP->tc_frag_data.max_bytes
13182 = next_fragP->tc_frag_data.max_prefix_length;
13183 /* This will be updated in the BRANCH_PREFIX scan. */
13184 next_fragP->tc_frag_data.max_prefix_length = 0;
13185 }
13186 else
13187 frag_wane (next_fragP);
13188 break;
13189 }
13190 }
13191
13192 /* Stop if there is no BRANCH_PREFIX. */
13193 if (!align_branch_prefix_size)
13194 return;
13195
13196 /* Scan for BRANCH_PREFIX. */
13197 for (; fragP != NULL; fragP = fragP->fr_next)
13198 {
13199 if (fragP->fr_type != rs_machine_dependent
13200 || (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
13201 != BRANCH_PREFIX))
13202 continue;
13203
13204 /* Count all BRANCH_PREFIX frags before BRANCH_PADDING and
13205 COND_JUMP_PREFIX. */
13206 max_prefix_length = 0;
13207 for (next_fragP = fragP;
13208 next_fragP != NULL;
13209 next_fragP = next_fragP->fr_next)
13210 {
13211 if (next_fragP->fr_type == rs_fill)
13212 /* Skip rs_fill frags. */
13213 continue;
13214 else if (next_fragP->fr_type != rs_machine_dependent)
13215 /* Stop for all other frags. */
13216 break;
13217
13218 /* rs_machine_dependent frags. */
13219 if (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
13220 == BRANCH_PREFIX)
13221 {
13222 /* Count BRANCH_PREFIX frags. */
13223 if (max_prefix_length >= MAX_FUSED_JCC_PADDING_SIZE)
13224 {
13225 max_prefix_length = MAX_FUSED_JCC_PADDING_SIZE;
13226 frag_wane (next_fragP);
13227 }
13228 else
13229 max_prefix_length
13230 += next_fragP->tc_frag_data.max_bytes;
13231 }
13232 else if ((TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
13233 == BRANCH_PADDING)
13234 || (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
13235 == FUSED_JCC_PADDING))
13236 {
13237 /* Stop at BRANCH_PADDING and FUSED_JCC_PADDING. */
13238 fragP->tc_frag_data.u.padding_fragP = next_fragP;
13239 break;
13240 }
13241 else
13242 /* Stop for other rs_machine_dependent frags. */
13243 break;
13244 }
13245
13246 fragP->tc_frag_data.max_prefix_length = max_prefix_length;
13247
13248 /* Skip to the next frag. */
13249 fragP = next_fragP;
13250 }
13251 }
13252
13253 /* Compute padding size for
13254
13255 FUSED_JCC_PADDING
13256 CMP like instruction
13257 BRANCH_PADDING
13258 COND_JUMP/UNCOND_JUMP
13259
13260 or
13261
13262 BRANCH_PADDING
13263 COND_JUMP/UNCOND_JUMP
13264 */
13265
13266 static int
13267 i386_branch_padding_size (fragS *fragP, offsetT address)
13268 {
13269 unsigned int offset, size, padding_size;
13270 fragS *branch_fragP = fragP->tc_frag_data.u.branch_fragP;
13271
13272 /* The start address of the BRANCH_PADDING or FUSED_JCC_PADDING frag. */
13273 if (!address)
13274 address = fragP->fr_address;
13275 address += fragP->fr_fix;
13276
13277 /* CMP like instrunction size. */
13278 size = fragP->tc_frag_data.cmp_size;
13279
13280 /* The base size of the branch frag. */
13281 size += branch_fragP->fr_fix;
13282
13283 /* Add opcode and displacement bytes for the rs_machine_dependent
13284 branch frag. */
13285 if (branch_fragP->fr_type == rs_machine_dependent)
13286 size += md_relax_table[branch_fragP->fr_subtype].rlx_length;
13287
13288 /* Check if branch is within boundary and doesn't end at the last
13289 byte. */
13290 offset = address & ((1U << align_branch_power) - 1);
13291 if ((offset + size) >= (1U << align_branch_power))
13292 /* Padding needed to avoid crossing boundary. */
13293 padding_size = (1U << align_branch_power) - offset;
13294 else
13295 /* No padding needed. */
13296 padding_size = 0;
13297
13298 /* The return value may be saved in tc_frag_data.length which is
13299 unsigned byte. */
13300 if (!fits_in_unsigned_byte (padding_size))
13301 abort ();
13302
13303 return padding_size;
13304 }
13305
13306 /* i386_generic_table_relax_frag()
13307
13308 Handle BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags to
13309 grow/shrink padding to align branch frags. Hand others to
13310 relax_frag(). */
13311
13312 long
13313 i386_generic_table_relax_frag (segT segment, fragS *fragP, long stretch)
13314 {
13315 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
13316 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
13317 {
13318 long padding_size = i386_branch_padding_size (fragP, 0);
13319 long grow = padding_size - fragP->tc_frag_data.length;
13320
13321 /* When the BRANCH_PREFIX frag is used, the computed address
13322 must match the actual address and there should be no padding. */
13323 if (fragP->tc_frag_data.padding_address
13324 && (fragP->tc_frag_data.padding_address != fragP->fr_address
13325 || padding_size))
13326 abort ();
13327
13328 /* Update the padding size. */
13329 if (grow)
13330 fragP->tc_frag_data.length = padding_size;
13331
13332 return grow;
13333 }
13334 else if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
13335 {
13336 fragS *padding_fragP, *next_fragP;
13337 long padding_size, left_size, last_size;
13338
13339 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
13340 if (!padding_fragP)
13341 /* Use the padding set by the leading BRANCH_PREFIX frag. */
13342 return (fragP->tc_frag_data.length
13343 - fragP->tc_frag_data.last_length);
13344
13345 /* Compute the relative address of the padding frag in the very
13346 first time where the BRANCH_PREFIX frag sizes are zero. */
13347 if (!fragP->tc_frag_data.padding_address)
13348 fragP->tc_frag_data.padding_address
13349 = padding_fragP->fr_address - (fragP->fr_address - stretch);
13350
13351 /* First update the last length from the previous interation. */
13352 left_size = fragP->tc_frag_data.prefix_length;
13353 for (next_fragP = fragP;
13354 next_fragP != padding_fragP;
13355 next_fragP = next_fragP->fr_next)
13356 if (next_fragP->fr_type == rs_machine_dependent
13357 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
13358 == BRANCH_PREFIX))
13359 {
13360 if (left_size)
13361 {
13362 int max = next_fragP->tc_frag_data.max_bytes;
13363 if (max)
13364 {
13365 int size;
13366 if (max > left_size)
13367 size = left_size;
13368 else
13369 size = max;
13370 left_size -= size;
13371 next_fragP->tc_frag_data.last_length = size;
13372 }
13373 }
13374 else
13375 next_fragP->tc_frag_data.last_length = 0;
13376 }
13377
13378 /* Check the padding size for the padding frag. */
13379 padding_size = i386_branch_padding_size
13380 (padding_fragP, (fragP->fr_address
13381 + fragP->tc_frag_data.padding_address));
13382
13383 last_size = fragP->tc_frag_data.prefix_length;
13384 /* Check if there is change from the last interation. */
13385 if (padding_size == last_size)
13386 {
13387 /* Update the expected address of the padding frag. */
13388 padding_fragP->tc_frag_data.padding_address
13389 = (fragP->fr_address + padding_size
13390 + fragP->tc_frag_data.padding_address);
13391 return 0;
13392 }
13393
13394 if (padding_size > fragP->tc_frag_data.max_prefix_length)
13395 {
13396 /* No padding if there is no sufficient room. Clear the
13397 expected address of the padding frag. */
13398 padding_fragP->tc_frag_data.padding_address = 0;
13399 padding_size = 0;
13400 }
13401 else
13402 /* Store the expected address of the padding frag. */
13403 padding_fragP->tc_frag_data.padding_address
13404 = (fragP->fr_address + padding_size
13405 + fragP->tc_frag_data.padding_address);
13406
13407 fragP->tc_frag_data.prefix_length = padding_size;
13408
13409 /* Update the length for the current interation. */
13410 left_size = padding_size;
13411 for (next_fragP = fragP;
13412 next_fragP != padding_fragP;
13413 next_fragP = next_fragP->fr_next)
13414 if (next_fragP->fr_type == rs_machine_dependent
13415 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
13416 == BRANCH_PREFIX))
13417 {
13418 if (left_size)
13419 {
13420 int max = next_fragP->tc_frag_data.max_bytes;
13421 if (max)
13422 {
13423 int size;
13424 if (max > left_size)
13425 size = left_size;
13426 else
13427 size = max;
13428 left_size -= size;
13429 next_fragP->tc_frag_data.length = size;
13430 }
13431 }
13432 else
13433 next_fragP->tc_frag_data.length = 0;
13434 }
13435
13436 return (fragP->tc_frag_data.length
13437 - fragP->tc_frag_data.last_length);
13438 }
13439 return relax_frag (segment, fragP, stretch);
13440 }
13441
13442 /* md_estimate_size_before_relax()
13443
13444 Called just before relax() for rs_machine_dependent frags. The x86
13445 assembler uses these frags to handle variable size jump
13446 instructions.
13447
13448 Any symbol that is now undefined will not become defined.
13449 Return the correct fr_subtype in the frag.
13450 Return the initial "guess for variable size of frag" to caller.
13451 The guess is actually the growth beyond the fixed part. Whatever
13452 we do to grow the fixed or variable part contributes to our
13453 returned value. */
13454
13455 int
13456 md_estimate_size_before_relax (fragS *fragP, segT segment)
13457 {
13458 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
13459 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX
13460 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
13461 {
13462 i386_classify_machine_dependent_frag (fragP);
13463 return fragP->tc_frag_data.length;
13464 }
13465
13466 /* We've already got fragP->fr_subtype right; all we have to do is
13467 check for un-relaxable symbols. On an ELF system, we can't relax
13468 an externally visible symbol, because it may be overridden by a
13469 shared library. */
13470 if (S_GET_SEGMENT (fragP->fr_symbol) != segment
13471 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13472 || (IS_ELF
13473 && !elf_symbol_resolved_in_segment_p (fragP->fr_symbol,
13474 fragP->fr_var))
13475 #endif
13476 #if defined (OBJ_COFF) && defined (TE_PE)
13477 || (OUTPUT_FLAVOR == bfd_target_coff_flavour
13478 && S_IS_WEAK (fragP->fr_symbol))
13479 #endif
13480 )
13481 {
13482 /* Symbol is undefined in this segment, or we need to keep a
13483 reloc so that weak symbols can be overridden. */
13484 int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
13485 enum bfd_reloc_code_real reloc_type;
13486 unsigned char *opcode;
13487 int old_fr_fix;
13488 fixS *fixP = NULL;
13489
13490 if (fragP->fr_var != NO_RELOC)
13491 reloc_type = (enum bfd_reloc_code_real) fragP->fr_var;
13492 else if (size == 2)
13493 reloc_type = BFD_RELOC_16_PCREL;
13494 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13495 else if (fragP->tc_frag_data.code == CODE_64BIT
13496 && fragP->fr_offset == 0
13497 && need_plt32_p (fragP->fr_symbol))
13498 reloc_type = BFD_RELOC_X86_64_PLT32;
13499 #endif
13500 else
13501 reloc_type = BFD_RELOC_32_PCREL;
13502
13503 old_fr_fix = fragP->fr_fix;
13504 opcode = (unsigned char *) fragP->fr_opcode;
13505
13506 switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
13507 {
13508 case UNCOND_JUMP:
13509 /* Make jmp (0xeb) a (d)word displacement jump. */
13510 opcode[0] = 0xe9;
13511 fragP->fr_fix += size;
13512 fixP = fix_new (fragP, old_fr_fix, size,
13513 fragP->fr_symbol,
13514 fragP->fr_offset, 1,
13515 reloc_type);
13516 break;
13517
13518 case COND_JUMP86:
13519 if (size == 2
13520 && (!no_cond_jump_promotion || fragP->fr_var != NO_RELOC))
13521 {
13522 /* Negate the condition, and branch past an
13523 unconditional jump. */
13524 opcode[0] ^= 1;
13525 opcode[1] = 3;
13526 /* Insert an unconditional jump. */
13527 opcode[2] = 0xe9;
13528 /* We added two extra opcode bytes, and have a two byte
13529 offset. */
13530 fragP->fr_fix += 2 + 2;
13531 fix_new (fragP, old_fr_fix + 2, 2,
13532 fragP->fr_symbol,
13533 fragP->fr_offset, 1,
13534 reloc_type);
13535 break;
13536 }
13537 /* Fall through. */
13538
13539 case COND_JUMP:
13540 if (no_cond_jump_promotion && fragP->fr_var == NO_RELOC)
13541 {
13542 fragP->fr_fix += 1;
13543 fixP = fix_new (fragP, old_fr_fix, 1,
13544 fragP->fr_symbol,
13545 fragP->fr_offset, 1,
13546 BFD_RELOC_8_PCREL);
13547 fixP->fx_signed = 1;
13548 break;
13549 }
13550
13551 /* This changes the byte-displacement jump 0x7N
13552 to the (d)word-displacement jump 0x0f,0x8N. */
13553 opcode[1] = opcode[0] + 0x10;
13554 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
13555 /* We've added an opcode byte. */
13556 fragP->fr_fix += 1 + size;
13557 fixP = fix_new (fragP, old_fr_fix + 1, size,
13558 fragP->fr_symbol,
13559 fragP->fr_offset, 1,
13560 reloc_type);
13561 break;
13562
13563 default:
13564 BAD_CASE (fragP->fr_subtype);
13565 break;
13566 }
13567
13568 /* All jumps handled here are signed, but don't unconditionally use a
13569 signed limit check for 32 and 16 bit jumps as we want to allow wrap
13570 around at 4G (outside of 64-bit mode) and 64k. */
13571 if (size == 4 && flag_code == CODE_64BIT)
13572 fixP->fx_signed = 1;
13573
13574 frag_wane (fragP);
13575 return fragP->fr_fix - old_fr_fix;
13576 }
13577
13578 /* Guess size depending on current relax state. Initially the relax
13579 state will correspond to a short jump and we return 1, because
13580 the variable part of the frag (the branch offset) is one byte
13581 long. However, we can relax a section more than once and in that
13582 case we must either set fr_subtype back to the unrelaxed state,
13583 or return the value for the appropriate branch. */
13584 return md_relax_table[fragP->fr_subtype].rlx_length;
13585 }
13586
13587 /* Called after relax() is finished.
13588
13589 In: Address of frag.
13590 fr_type == rs_machine_dependent.
13591 fr_subtype is what the address relaxed to.
13592
13593 Out: Any fixSs and constants are set up.
13594 Caller will turn frag into a ".space 0". */
13595
13596 void
13597 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec ATTRIBUTE_UNUSED,
13598 fragS *fragP)
13599 {
13600 unsigned char *opcode;
13601 unsigned char *where_to_put_displacement = NULL;
13602 offsetT target_address;
13603 offsetT opcode_address;
13604 unsigned int extension = 0;
13605 offsetT displacement_from_opcode_start;
13606
13607 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
13608 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING
13609 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
13610 {
13611 /* Generate nop padding. */
13612 unsigned int size = fragP->tc_frag_data.length;
13613 if (size)
13614 {
13615 if (size > fragP->tc_frag_data.max_bytes)
13616 abort ();
13617
13618 if (flag_debug)
13619 {
13620 const char *msg;
13621 const char *branch = "branch";
13622 const char *prefix = "";
13623 fragS *padding_fragP;
13624 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
13625 == BRANCH_PREFIX)
13626 {
13627 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
13628 switch (fragP->tc_frag_data.default_prefix)
13629 {
13630 default:
13631 abort ();
13632 break;
13633 case CS_PREFIX_OPCODE:
13634 prefix = " cs";
13635 break;
13636 case DS_PREFIX_OPCODE:
13637 prefix = " ds";
13638 break;
13639 case ES_PREFIX_OPCODE:
13640 prefix = " es";
13641 break;
13642 case FS_PREFIX_OPCODE:
13643 prefix = " fs";
13644 break;
13645 case GS_PREFIX_OPCODE:
13646 prefix = " gs";
13647 break;
13648 case SS_PREFIX_OPCODE:
13649 prefix = " ss";
13650 break;
13651 }
13652 if (padding_fragP)
13653 msg = _("%s:%u: add %d%s at 0x%llx to align "
13654 "%s within %d-byte boundary\n");
13655 else
13656 msg = _("%s:%u: add additional %d%s at 0x%llx to "
13657 "align %s within %d-byte boundary\n");
13658 }
13659 else
13660 {
13661 padding_fragP = fragP;
13662 msg = _("%s:%u: add %d%s-byte nop at 0x%llx to align "
13663 "%s within %d-byte boundary\n");
13664 }
13665
13666 if (padding_fragP)
13667 switch (padding_fragP->tc_frag_data.branch_type)
13668 {
13669 case align_branch_jcc:
13670 branch = "jcc";
13671 break;
13672 case align_branch_fused:
13673 branch = "fused jcc";
13674 break;
13675 case align_branch_jmp:
13676 branch = "jmp";
13677 break;
13678 case align_branch_call:
13679 branch = "call";
13680 break;
13681 case align_branch_indirect:
13682 branch = "indiret branch";
13683 break;
13684 case align_branch_ret:
13685 branch = "ret";
13686 break;
13687 default:
13688 break;
13689 }
13690
13691 fprintf (stdout, msg,
13692 fragP->fr_file, fragP->fr_line, size, prefix,
13693 (long long) fragP->fr_address, branch,
13694 1 << align_branch_power);
13695 }
13696 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
13697 memset (fragP->fr_opcode,
13698 fragP->tc_frag_data.default_prefix, size);
13699 else
13700 i386_generate_nops (fragP, (char *) fragP->fr_opcode,
13701 size, 0);
13702 fragP->fr_fix += size;
13703 }
13704 return;
13705 }
13706
13707 opcode = (unsigned char *) fragP->fr_opcode;
13708
13709 /* Address we want to reach in file space. */
13710 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
13711
13712 /* Address opcode resides at in file space. */
13713 opcode_address = fragP->fr_address + fragP->fr_fix;
13714
13715 /* Displacement from opcode start to fill into instruction. */
13716 displacement_from_opcode_start = target_address - opcode_address;
13717
13718 if ((fragP->fr_subtype & BIG) == 0)
13719 {
13720 /* Don't have to change opcode. */
13721 extension = 1; /* 1 opcode + 1 displacement */
13722 where_to_put_displacement = &opcode[1];
13723 }
13724 else
13725 {
13726 if (no_cond_jump_promotion
13727 && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
13728 as_warn_where (fragP->fr_file, fragP->fr_line,
13729 _("long jump required"));
13730
13731 switch (fragP->fr_subtype)
13732 {
13733 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
13734 extension = 4; /* 1 opcode + 4 displacement */
13735 opcode[0] = 0xe9;
13736 where_to_put_displacement = &opcode[1];
13737 break;
13738
13739 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
13740 extension = 2; /* 1 opcode + 2 displacement */
13741 opcode[0] = 0xe9;
13742 where_to_put_displacement = &opcode[1];
13743 break;
13744
13745 case ENCODE_RELAX_STATE (COND_JUMP, BIG):
13746 case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
13747 extension = 5; /* 2 opcode + 4 displacement */
13748 opcode[1] = opcode[0] + 0x10;
13749 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
13750 where_to_put_displacement = &opcode[2];
13751 break;
13752
13753 case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
13754 extension = 3; /* 2 opcode + 2 displacement */
13755 opcode[1] = opcode[0] + 0x10;
13756 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
13757 where_to_put_displacement = &opcode[2];
13758 break;
13759
13760 case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
13761 extension = 4;
13762 opcode[0] ^= 1;
13763 opcode[1] = 3;
13764 opcode[2] = 0xe9;
13765 where_to_put_displacement = &opcode[3];
13766 break;
13767
13768 default:
13769 BAD_CASE (fragP->fr_subtype);
13770 break;
13771 }
13772 }
13773
13774 /* If size if less then four we are sure that the operand fits,
13775 but if it's 4, then it could be that the displacement is larger
13776 then -/+ 2GB. */
13777 if (DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype) == 4
13778 && object_64bit
13779 && ((addressT) (displacement_from_opcode_start - extension
13780 + ((addressT) 1 << 31))
13781 > (((addressT) 2 << 31) - 1)))
13782 {
13783 as_bad_where (fragP->fr_file, fragP->fr_line,
13784 _("jump target out of range"));
13785 /* Make us emit 0. */
13786 displacement_from_opcode_start = extension;
13787 }
13788 /* Now put displacement after opcode. */
13789 md_number_to_chars ((char *) where_to_put_displacement,
13790 (valueT) (displacement_from_opcode_start - extension),
13791 DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
13792 fragP->fr_fix += extension;
13793 }
13794 \f
13795 /* Apply a fixup (fixP) to segment data, once it has been determined
13796 by our caller that we have all the info we need to fix it up.
13797
13798 Parameter valP is the pointer to the value of the bits.
13799
13800 On the 386, immediates, displacements, and data pointers are all in
13801 the same (little-endian) format, so we don't need to care about which
13802 we are handling. */
13803
13804 void
13805 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
13806 {
13807 char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
13808 valueT value = *valP;
13809
13810 #if !defined (TE_Mach)
13811 if (fixP->fx_pcrel)
13812 {
13813 switch (fixP->fx_r_type)
13814 {
13815 default:
13816 break;
13817
13818 case BFD_RELOC_64:
13819 fixP->fx_r_type = BFD_RELOC_64_PCREL;
13820 break;
13821 case BFD_RELOC_32:
13822 case BFD_RELOC_X86_64_32S:
13823 fixP->fx_r_type = BFD_RELOC_32_PCREL;
13824 break;
13825 case BFD_RELOC_16:
13826 fixP->fx_r_type = BFD_RELOC_16_PCREL;
13827 break;
13828 case BFD_RELOC_8:
13829 fixP->fx_r_type = BFD_RELOC_8_PCREL;
13830 break;
13831 }
13832 }
13833
13834 if (fixP->fx_addsy != NULL
13835 && (fixP->fx_r_type == BFD_RELOC_32_PCREL
13836 || fixP->fx_r_type == BFD_RELOC_64_PCREL
13837 || fixP->fx_r_type == BFD_RELOC_16_PCREL
13838 || fixP->fx_r_type == BFD_RELOC_8_PCREL)
13839 && !use_rela_relocations)
13840 {
13841 /* This is a hack. There should be a better way to handle this.
13842 This covers for the fact that bfd_install_relocation will
13843 subtract the current location (for partial_inplace, PC relative
13844 relocations); see more below. */
13845 #ifndef OBJ_AOUT
13846 if (IS_ELF
13847 #ifdef TE_PE
13848 || OUTPUT_FLAVOR == bfd_target_coff_flavour
13849 #endif
13850 )
13851 value += fixP->fx_where + fixP->fx_frag->fr_address;
13852 #endif
13853 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13854 if (IS_ELF)
13855 {
13856 segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
13857
13858 if ((sym_seg == seg
13859 || (symbol_section_p (fixP->fx_addsy)
13860 && sym_seg != absolute_section))
13861 && !generic_force_reloc (fixP))
13862 {
13863 /* Yes, we add the values in twice. This is because
13864 bfd_install_relocation subtracts them out again. I think
13865 bfd_install_relocation is broken, but I don't dare change
13866 it. FIXME. */
13867 value += fixP->fx_where + fixP->fx_frag->fr_address;
13868 }
13869 }
13870 #endif
13871 #if defined (OBJ_COFF) && defined (TE_PE)
13872 /* For some reason, the PE format does not store a
13873 section address offset for a PC relative symbol. */
13874 if (S_GET_SEGMENT (fixP->fx_addsy) != seg
13875 || S_IS_WEAK (fixP->fx_addsy))
13876 value += md_pcrel_from (fixP);
13877 #endif
13878 }
13879 #if defined (OBJ_COFF) && defined (TE_PE)
13880 if (fixP->fx_addsy != NULL
13881 && S_IS_WEAK (fixP->fx_addsy)
13882 /* PR 16858: Do not modify weak function references. */
13883 && ! fixP->fx_pcrel)
13884 {
13885 #if !defined (TE_PEP)
13886 /* For x86 PE weak function symbols are neither PC-relative
13887 nor do they set S_IS_FUNCTION. So the only reliable way
13888 to detect them is to check the flags of their containing
13889 section. */
13890 if (S_GET_SEGMENT (fixP->fx_addsy) != NULL
13891 && S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_CODE)
13892 ;
13893 else
13894 #endif
13895 value -= S_GET_VALUE (fixP->fx_addsy);
13896 }
13897 #endif
13898
13899 /* Fix a few things - the dynamic linker expects certain values here,
13900 and we must not disappoint it. */
13901 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13902 if (IS_ELF && fixP->fx_addsy)
13903 switch (fixP->fx_r_type)
13904 {
13905 case BFD_RELOC_386_PLT32:
13906 case BFD_RELOC_X86_64_PLT32:
13907 /* Make the jump instruction point to the address of the operand.
13908 At runtime we merely add the offset to the actual PLT entry.
13909 NB: Subtract the offset size only for jump instructions. */
13910 if (fixP->fx_pcrel)
13911 value = -4;
13912 break;
13913
13914 case BFD_RELOC_386_TLS_GD:
13915 case BFD_RELOC_386_TLS_LDM:
13916 case BFD_RELOC_386_TLS_IE_32:
13917 case BFD_RELOC_386_TLS_IE:
13918 case BFD_RELOC_386_TLS_GOTIE:
13919 case BFD_RELOC_386_TLS_GOTDESC:
13920 case BFD_RELOC_X86_64_TLSGD:
13921 case BFD_RELOC_X86_64_TLSLD:
13922 case BFD_RELOC_X86_64_GOTTPOFF:
13923 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
13924 value = 0; /* Fully resolved at runtime. No addend. */
13925 /* Fallthrough */
13926 case BFD_RELOC_386_TLS_LE:
13927 case BFD_RELOC_386_TLS_LDO_32:
13928 case BFD_RELOC_386_TLS_LE_32:
13929 case BFD_RELOC_X86_64_DTPOFF32:
13930 case BFD_RELOC_X86_64_DTPOFF64:
13931 case BFD_RELOC_X86_64_TPOFF32:
13932 case BFD_RELOC_X86_64_TPOFF64:
13933 S_SET_THREAD_LOCAL (fixP->fx_addsy);
13934 break;
13935
13936 case BFD_RELOC_386_TLS_DESC_CALL:
13937 case BFD_RELOC_X86_64_TLSDESC_CALL:
13938 value = 0; /* Fully resolved at runtime. No addend. */
13939 S_SET_THREAD_LOCAL (fixP->fx_addsy);
13940 fixP->fx_done = 0;
13941 return;
13942
13943 case BFD_RELOC_VTABLE_INHERIT:
13944 case BFD_RELOC_VTABLE_ENTRY:
13945 fixP->fx_done = 0;
13946 return;
13947
13948 default:
13949 break;
13950 }
13951 #endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) */
13952
13953 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
13954 if (!object_64bit)
13955 value = extend_to_32bit_address (value);
13956
13957 *valP = value;
13958 #endif /* !defined (TE_Mach) */
13959
13960 /* Are we finished with this relocation now? */
13961 if (fixP->fx_addsy == NULL)
13962 {
13963 fixP->fx_done = 1;
13964 switch (fixP->fx_r_type)
13965 {
13966 case BFD_RELOC_X86_64_32S:
13967 fixP->fx_signed = 1;
13968 break;
13969
13970 default:
13971 break;
13972 }
13973 }
13974 #if defined (OBJ_COFF) && defined (TE_PE)
13975 else if (fixP->fx_addsy != NULL && S_IS_WEAK (fixP->fx_addsy))
13976 {
13977 fixP->fx_done = 0;
13978 /* Remember value for tc_gen_reloc. */
13979 fixP->fx_addnumber = value;
13980 /* Clear out the frag for now. */
13981 value = 0;
13982 }
13983 #endif
13984 else if (use_rela_relocations)
13985 {
13986 if (!disallow_64bit_reloc || fixP->fx_r_type == NO_RELOC)
13987 fixP->fx_no_overflow = 1;
13988 /* Remember value for tc_gen_reloc. */
13989 fixP->fx_addnumber = value;
13990 value = 0;
13991 }
13992
13993 md_number_to_chars (p, value, fixP->fx_size);
13994 }
13995 \f
13996 const char *
13997 md_atof (int type, char *litP, int *sizeP)
13998 {
13999 /* This outputs the LITTLENUMs in REVERSE order;
14000 in accord with the bigendian 386. */
14001 return ieee_md_atof (type, litP, sizeP, false);
14002 }
14003 \f
14004 static char output_invalid_buf[sizeof (unsigned char) * 2 + 6];
14005
14006 static char *
14007 output_invalid (int c)
14008 {
14009 if (ISPRINT (c))
14010 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
14011 "'%c'", c);
14012 else
14013 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
14014 "(0x%x)", (unsigned char) c);
14015 return output_invalid_buf;
14016 }
14017
14018 /* Verify that @r can be used in the current context. */
14019
14020 static bool check_register (const reg_entry *r)
14021 {
14022 if (allow_pseudo_reg)
14023 return true;
14024
14025 if (operand_type_all_zero (&r->reg_type))
14026 return false;
14027
14028 if ((r->reg_type.bitfield.dword
14029 || (r->reg_type.bitfield.class == SReg && r->reg_num > 3)
14030 || r->reg_type.bitfield.class == RegCR
14031 || r->reg_type.bitfield.class == RegDR)
14032 && !cpu_arch_flags.bitfield.cpui386)
14033 return false;
14034
14035 if (r->reg_type.bitfield.class == RegTR
14036 && (flag_code == CODE_64BIT
14037 || !cpu_arch_flags.bitfield.cpui386
14038 || cpu_arch_isa_flags.bitfield.cpui586
14039 || cpu_arch_isa_flags.bitfield.cpui686))
14040 return false;
14041
14042 if (r->reg_type.bitfield.class == RegMMX && !cpu_arch_flags.bitfield.cpummx)
14043 return false;
14044
14045 if (!cpu_arch_flags.bitfield.cpuavx512f)
14046 {
14047 if (r->reg_type.bitfield.zmmword
14048 || r->reg_type.bitfield.class == RegMask)
14049 return false;
14050
14051 if (!cpu_arch_flags.bitfield.cpuavx)
14052 {
14053 if (r->reg_type.bitfield.ymmword)
14054 return false;
14055
14056 if (!cpu_arch_flags.bitfield.cpusse && r->reg_type.bitfield.xmmword)
14057 return false;
14058 }
14059 }
14060
14061 if (r->reg_type.bitfield.zmmword)
14062 {
14063 if (vector_size < VSZ512)
14064 return false;
14065
14066 if (i.vec_encoding == vex_encoding_default)
14067 i.vec_encoding = vex_encoding_evex512;
14068 else if (i.vec_encoding != vex_encoding_evex
14069 && i.vec_encoding != vex_encoding_evex512)
14070 i.vec_encoding = vex_encoding_error;
14071 }
14072
14073 if (vector_size < VSZ256 && r->reg_type.bitfield.ymmword)
14074 return false;
14075
14076 if (r->reg_type.bitfield.tmmword
14077 && (!cpu_arch_flags.bitfield.cpuamx_tile
14078 || flag_code != CODE_64BIT))
14079 return false;
14080
14081 if (r->reg_type.bitfield.class == RegBND && !cpu_arch_flags.bitfield.cpumpx)
14082 return false;
14083
14084 /* Don't allow fake index register unless allow_index_reg isn't 0. */
14085 if (!allow_index_reg && r->reg_num == RegIZ)
14086 return false;
14087
14088 /* Upper 16 vector registers are only available with VREX in 64bit
14089 mode, and require EVEX encoding. */
14090 if (r->reg_flags & RegVRex)
14091 {
14092 if (!cpu_arch_flags.bitfield.cpuavx512f
14093 || flag_code != CODE_64BIT)
14094 return false;
14095
14096 if (i.vec_encoding == vex_encoding_default
14097 || i.vec_encoding == vex_encoding_evex512)
14098 i.vec_encoding = vex_encoding_evex;
14099 else if (i.vec_encoding != vex_encoding_evex)
14100 i.vec_encoding = vex_encoding_error;
14101 }
14102
14103 if (((r->reg_flags & (RegRex64 | RegRex)) || r->reg_type.bitfield.qword)
14104 && (!cpu_arch_flags.bitfield.cpu64
14105 || r->reg_type.bitfield.class != RegCR
14106 || dot_insn ())
14107 && flag_code != CODE_64BIT)
14108 return false;
14109
14110 if (r->reg_type.bitfield.class == SReg && r->reg_num == RegFlat
14111 && !intel_syntax)
14112 return false;
14113
14114 return true;
14115 }
14116
14117 /* REG_STRING starts *before* REGISTER_PREFIX. */
14118
14119 static const reg_entry *
14120 parse_real_register (const char *reg_string, char **end_op)
14121 {
14122 const char *s = reg_string;
14123 char *p;
14124 char reg_name_given[MAX_REG_NAME_SIZE + 1];
14125 const reg_entry *r;
14126
14127 /* Skip possible REGISTER_PREFIX and possible whitespace. */
14128 if (*s == REGISTER_PREFIX)
14129 ++s;
14130
14131 if (is_space_char (*s))
14132 ++s;
14133
14134 p = reg_name_given;
14135 while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
14136 {
14137 if (p >= reg_name_given + MAX_REG_NAME_SIZE)
14138 return (const reg_entry *) NULL;
14139 s++;
14140 }
14141
14142 if (is_part_of_name (*s))
14143 return (const reg_entry *) NULL;
14144
14145 *end_op = (char *) s;
14146
14147 r = (const reg_entry *) str_hash_find (reg_hash, reg_name_given);
14148
14149 /* Handle floating point regs, allowing spaces in the (i) part. */
14150 if (r == reg_st0)
14151 {
14152 if (!cpu_arch_flags.bitfield.cpu8087
14153 && !cpu_arch_flags.bitfield.cpu287
14154 && !cpu_arch_flags.bitfield.cpu387
14155 && !allow_pseudo_reg)
14156 return (const reg_entry *) NULL;
14157
14158 if (is_space_char (*s))
14159 ++s;
14160 if (*s == '(')
14161 {
14162 ++s;
14163 if (is_space_char (*s))
14164 ++s;
14165 if (*s >= '0' && *s <= '7')
14166 {
14167 int fpr = *s - '0';
14168 ++s;
14169 if (is_space_char (*s))
14170 ++s;
14171 if (*s == ')')
14172 {
14173 *end_op = (char *) s + 1;
14174 know (r[fpr].reg_num == fpr);
14175 return r + fpr;
14176 }
14177 }
14178 /* We have "%st(" then garbage. */
14179 return (const reg_entry *) NULL;
14180 }
14181 }
14182
14183 return r && check_register (r) ? r : NULL;
14184 }
14185
14186 /* REG_STRING starts *before* REGISTER_PREFIX. */
14187
14188 static const reg_entry *
14189 parse_register (const char *reg_string, char **end_op)
14190 {
14191 const reg_entry *r;
14192
14193 if (*reg_string == REGISTER_PREFIX || allow_naked_reg)
14194 r = parse_real_register (reg_string, end_op);
14195 else
14196 r = NULL;
14197 if (!r)
14198 {
14199 char *save = input_line_pointer;
14200 char *buf = xstrdup (reg_string), *name;
14201 symbolS *symbolP;
14202
14203 input_line_pointer = buf;
14204 get_symbol_name (&name);
14205 symbolP = symbol_find (name);
14206 while (symbolP && symbol_equated_p (symbolP))
14207 {
14208 const expressionS *e = symbol_get_value_expression(symbolP);
14209
14210 if (e->X_add_number)
14211 break;
14212 symbolP = e->X_add_symbol;
14213 }
14214 if (symbolP && S_GET_SEGMENT (symbolP) == reg_section)
14215 {
14216 const expressionS *e = symbol_get_value_expression (symbolP);
14217
14218 if (e->X_op == O_register)
14219 {
14220 know (e->X_add_number >= 0
14221 && (valueT) e->X_add_number < i386_regtab_size);
14222 r = i386_regtab + e->X_add_number;
14223 *end_op = (char *) reg_string + (input_line_pointer - buf);
14224 }
14225 if (r && !check_register (r))
14226 {
14227 as_bad (_("register '%s%s' cannot be used here"),
14228 register_prefix, r->reg_name);
14229 r = &bad_reg;
14230 }
14231 }
14232 input_line_pointer = save;
14233 free (buf);
14234 }
14235 return r;
14236 }
14237
14238 int
14239 i386_parse_name (char *name, expressionS *e, char *nextcharP)
14240 {
14241 const reg_entry *r = NULL;
14242 char *end = input_line_pointer;
14243
14244 /* We only know the terminating character here. It being double quote could
14245 be the closing one of a quoted symbol name, or an opening one from a
14246 following string (or another quoted symbol name). Since the latter can't
14247 be valid syntax for anything, bailing in either case is good enough. */
14248 if (*nextcharP == '"')
14249 return 0;
14250
14251 *end = *nextcharP;
14252 if (*name == REGISTER_PREFIX || allow_naked_reg)
14253 r = parse_real_register (name, &input_line_pointer);
14254 if (r && end <= input_line_pointer)
14255 {
14256 *nextcharP = *input_line_pointer;
14257 *input_line_pointer = 0;
14258 e->X_op = O_register;
14259 e->X_add_number = r - i386_regtab;
14260 return 1;
14261 }
14262 input_line_pointer = end;
14263 *end = 0;
14264 return intel_syntax ? i386_intel_parse_name (name, e) : 0;
14265 }
14266
14267 void
14268 md_operand (expressionS *e)
14269 {
14270 char *end;
14271 const reg_entry *r;
14272
14273 switch (*input_line_pointer)
14274 {
14275 case REGISTER_PREFIX:
14276 r = parse_real_register (input_line_pointer, &end);
14277 if (r)
14278 {
14279 e->X_op = O_register;
14280 e->X_add_number = r - i386_regtab;
14281 input_line_pointer = end;
14282 }
14283 break;
14284
14285 case '[':
14286 gas_assert (intel_syntax);
14287 end = input_line_pointer++;
14288 expression (e);
14289 if (*input_line_pointer == ']')
14290 {
14291 ++input_line_pointer;
14292 e->X_op_symbol = make_expr_symbol (e);
14293 e->X_add_symbol = NULL;
14294 e->X_add_number = 0;
14295 e->X_op = O_index;
14296 }
14297 else
14298 {
14299 e->X_op = O_absent;
14300 input_line_pointer = end;
14301 }
14302 break;
14303 }
14304 }
14305
14306 #ifdef BFD64
14307 /* To maintain consistency with !BFD64 builds of gas record, whether any
14308 (binary) operator was involved in an expression. As expressions are
14309 evaluated in only 32 bits when !BFD64, we use this to decide whether to
14310 truncate results. */
14311 bool i386_record_operator (operatorT op,
14312 const expressionS *left,
14313 const expressionS *right)
14314 {
14315 if (op == O_absent)
14316 return false;
14317
14318 if (!left)
14319 {
14320 /* Since the expression parser applies unary operators fine to bignum
14321 operands, we don't need to be concerned of respective operands not
14322 fitting in 32 bits. */
14323 if (right->X_op == O_constant && right->X_unsigned
14324 && !fits_in_unsigned_long (right->X_add_number))
14325 return false;
14326 }
14327 /* This isn't entirely right: The pattern can also result when constant
14328 expressions are folded (e.g. 0xffffffff + 1). */
14329 else if ((left->X_op == O_constant && left->X_unsigned
14330 && !fits_in_unsigned_long (left->X_add_number))
14331 || (right->X_op == O_constant && right->X_unsigned
14332 && !fits_in_unsigned_long (right->X_add_number)))
14333 expr_mode = expr_large_value;
14334
14335 if (expr_mode != expr_large_value)
14336 expr_mode = expr_operator_present;
14337
14338 return false;
14339 }
14340 #endif
14341 \f
14342 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14343 const char *md_shortopts = "kVQ:sqnO::";
14344 #else
14345 const char *md_shortopts = "qnO::";
14346 #endif
14347
14348 #define OPTION_32 (OPTION_MD_BASE + 0)
14349 #define OPTION_64 (OPTION_MD_BASE + 1)
14350 #define OPTION_DIVIDE (OPTION_MD_BASE + 2)
14351 #define OPTION_MARCH (OPTION_MD_BASE + 3)
14352 #define OPTION_MTUNE (OPTION_MD_BASE + 4)
14353 #define OPTION_MMNEMONIC (OPTION_MD_BASE + 5)
14354 #define OPTION_MSYNTAX (OPTION_MD_BASE + 6)
14355 #define OPTION_MINDEX_REG (OPTION_MD_BASE + 7)
14356 #define OPTION_MNAKED_REG (OPTION_MD_BASE + 8)
14357 #define OPTION_MRELAX_RELOCATIONS (OPTION_MD_BASE + 9)
14358 #define OPTION_MSSE2AVX (OPTION_MD_BASE + 10)
14359 #define OPTION_MSSE_CHECK (OPTION_MD_BASE + 11)
14360 #define OPTION_MOPERAND_CHECK (OPTION_MD_BASE + 12)
14361 #define OPTION_MAVXSCALAR (OPTION_MD_BASE + 13)
14362 #define OPTION_X32 (OPTION_MD_BASE + 14)
14363 #define OPTION_MADD_BND_PREFIX (OPTION_MD_BASE + 15)
14364 #define OPTION_MEVEXLIG (OPTION_MD_BASE + 16)
14365 #define OPTION_MEVEXWIG (OPTION_MD_BASE + 17)
14366 #define OPTION_MBIG_OBJ (OPTION_MD_BASE + 18)
14367 #define OPTION_MOMIT_LOCK_PREFIX (OPTION_MD_BASE + 19)
14368 #define OPTION_MEVEXRCIG (OPTION_MD_BASE + 20)
14369 #define OPTION_MSHARED (OPTION_MD_BASE + 21)
14370 #define OPTION_MAMD64 (OPTION_MD_BASE + 22)
14371 #define OPTION_MINTEL64 (OPTION_MD_BASE + 23)
14372 #define OPTION_MFENCE_AS_LOCK_ADD (OPTION_MD_BASE + 24)
14373 #define OPTION_X86_USED_NOTE (OPTION_MD_BASE + 25)
14374 #define OPTION_MVEXWIG (OPTION_MD_BASE + 26)
14375 #define OPTION_MALIGN_BRANCH_BOUNDARY (OPTION_MD_BASE + 27)
14376 #define OPTION_MALIGN_BRANCH_PREFIX_SIZE (OPTION_MD_BASE + 28)
14377 #define OPTION_MALIGN_BRANCH (OPTION_MD_BASE + 29)
14378 #define OPTION_MBRANCHES_WITH_32B_BOUNDARIES (OPTION_MD_BASE + 30)
14379 #define OPTION_MLFENCE_AFTER_LOAD (OPTION_MD_BASE + 31)
14380 #define OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH (OPTION_MD_BASE + 32)
14381 #define OPTION_MLFENCE_BEFORE_RET (OPTION_MD_BASE + 33)
14382 #define OPTION_MUSE_UNALIGNED_VECTOR_MOVE (OPTION_MD_BASE + 34)
14383
14384 struct option md_longopts[] =
14385 {
14386 {"32", no_argument, NULL, OPTION_32},
14387 #if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
14388 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
14389 {"64", no_argument, NULL, OPTION_64},
14390 #endif
14391 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14392 {"x32", no_argument, NULL, OPTION_X32},
14393 {"mshared", no_argument, NULL, OPTION_MSHARED},
14394 {"mx86-used-note", required_argument, NULL, OPTION_X86_USED_NOTE},
14395 #endif
14396 {"divide", no_argument, NULL, OPTION_DIVIDE},
14397 {"march", required_argument, NULL, OPTION_MARCH},
14398 {"mtune", required_argument, NULL, OPTION_MTUNE},
14399 {"mmnemonic", required_argument, NULL, OPTION_MMNEMONIC},
14400 {"msyntax", required_argument, NULL, OPTION_MSYNTAX},
14401 {"mindex-reg", no_argument, NULL, OPTION_MINDEX_REG},
14402 {"mnaked-reg", no_argument, NULL, OPTION_MNAKED_REG},
14403 {"msse2avx", no_argument, NULL, OPTION_MSSE2AVX},
14404 {"muse-unaligned-vector-move", no_argument, NULL, OPTION_MUSE_UNALIGNED_VECTOR_MOVE},
14405 {"msse-check", required_argument, NULL, OPTION_MSSE_CHECK},
14406 {"moperand-check", required_argument, NULL, OPTION_MOPERAND_CHECK},
14407 {"mavxscalar", required_argument, NULL, OPTION_MAVXSCALAR},
14408 {"mvexwig", required_argument, NULL, OPTION_MVEXWIG},
14409 {"madd-bnd-prefix", no_argument, NULL, OPTION_MADD_BND_PREFIX},
14410 {"mevexlig", required_argument, NULL, OPTION_MEVEXLIG},
14411 {"mevexwig", required_argument, NULL, OPTION_MEVEXWIG},
14412 # if defined (TE_PE) || defined (TE_PEP)
14413 {"mbig-obj", no_argument, NULL, OPTION_MBIG_OBJ},
14414 #endif
14415 {"momit-lock-prefix", required_argument, NULL, OPTION_MOMIT_LOCK_PREFIX},
14416 {"mfence-as-lock-add", required_argument, NULL, OPTION_MFENCE_AS_LOCK_ADD},
14417 {"mrelax-relocations", required_argument, NULL, OPTION_MRELAX_RELOCATIONS},
14418 {"mevexrcig", required_argument, NULL, OPTION_MEVEXRCIG},
14419 {"malign-branch-boundary", required_argument, NULL, OPTION_MALIGN_BRANCH_BOUNDARY},
14420 {"malign-branch-prefix-size", required_argument, NULL, OPTION_MALIGN_BRANCH_PREFIX_SIZE},
14421 {"malign-branch", required_argument, NULL, OPTION_MALIGN_BRANCH},
14422 {"mbranches-within-32B-boundaries", no_argument, NULL, OPTION_MBRANCHES_WITH_32B_BOUNDARIES},
14423 {"mlfence-after-load", required_argument, NULL, OPTION_MLFENCE_AFTER_LOAD},
14424 {"mlfence-before-indirect-branch", required_argument, NULL,
14425 OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH},
14426 {"mlfence-before-ret", required_argument, NULL, OPTION_MLFENCE_BEFORE_RET},
14427 {"mamd64", no_argument, NULL, OPTION_MAMD64},
14428 {"mintel64", no_argument, NULL, OPTION_MINTEL64},
14429 {NULL, no_argument, NULL, 0}
14430 };
14431 size_t md_longopts_size = sizeof (md_longopts);
14432
14433 int
14434 md_parse_option (int c, const char *arg)
14435 {
14436 unsigned int j;
14437 char *arch, *next, *saved, *type;
14438
14439 switch (c)
14440 {
14441 case 'n':
14442 optimize_align_code = 0;
14443 break;
14444
14445 case 'q':
14446 quiet_warnings = 1;
14447 break;
14448
14449 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14450 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
14451 should be emitted or not. FIXME: Not implemented. */
14452 case 'Q':
14453 if ((arg[0] != 'y' && arg[0] != 'n') || arg[1])
14454 return 0;
14455 break;
14456
14457 /* -V: SVR4 argument to print version ID. */
14458 case 'V':
14459 print_version_id ();
14460 break;
14461
14462 /* -k: Ignore for FreeBSD compatibility. */
14463 case 'k':
14464 break;
14465
14466 case 's':
14467 /* -s: On i386 Solaris, this tells the native assembler to use
14468 .stab instead of .stab.excl. We always use .stab anyhow. */
14469 break;
14470
14471 case OPTION_MSHARED:
14472 shared = 1;
14473 break;
14474
14475 case OPTION_X86_USED_NOTE:
14476 if (strcasecmp (arg, "yes") == 0)
14477 x86_used_note = 1;
14478 else if (strcasecmp (arg, "no") == 0)
14479 x86_used_note = 0;
14480 else
14481 as_fatal (_("invalid -mx86-used-note= option: `%s'"), arg);
14482 break;
14483
14484
14485 #endif
14486 #if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
14487 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
14488 case OPTION_64:
14489 {
14490 const char **list, **l;
14491
14492 list = bfd_target_list ();
14493 for (l = list; *l != NULL; l++)
14494 if (startswith (*l, "elf64-x86-64")
14495 || strcmp (*l, "coff-x86-64") == 0
14496 || strcmp (*l, "pe-x86-64") == 0
14497 || strcmp (*l, "pei-x86-64") == 0
14498 || strcmp (*l, "mach-o-x86-64") == 0)
14499 {
14500 default_arch = "x86_64";
14501 break;
14502 }
14503 if (*l == NULL)
14504 as_fatal (_("no compiled in support for x86_64"));
14505 free (list);
14506 }
14507 break;
14508 #endif
14509
14510 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14511 case OPTION_X32:
14512 if (IS_ELF)
14513 {
14514 const char **list, **l;
14515
14516 list = bfd_target_list ();
14517 for (l = list; *l != NULL; l++)
14518 if (startswith (*l, "elf32-x86-64"))
14519 {
14520 default_arch = "x86_64:32";
14521 break;
14522 }
14523 if (*l == NULL)
14524 as_fatal (_("no compiled in support for 32bit x86_64"));
14525 free (list);
14526 }
14527 else
14528 as_fatal (_("32bit x86_64 is only supported for ELF"));
14529 break;
14530 #endif
14531
14532 case OPTION_32:
14533 {
14534 const char **list, **l;
14535
14536 list = bfd_target_list ();
14537 for (l = list; *l != NULL; l++)
14538 if (strstr (*l, "-i386")
14539 || strstr (*l, "-go32"))
14540 {
14541 default_arch = "i386";
14542 break;
14543 }
14544 if (*l == NULL)
14545 as_fatal (_("no compiled in support for ix86"));
14546 free (list);
14547 }
14548 break;
14549
14550 case OPTION_DIVIDE:
14551 #ifdef SVR4_COMMENT_CHARS
14552 {
14553 char *n, *t;
14554 const char *s;
14555
14556 n = XNEWVEC (char, strlen (i386_comment_chars) + 1);
14557 t = n;
14558 for (s = i386_comment_chars; *s != '\0'; s++)
14559 if (*s != '/')
14560 *t++ = *s;
14561 *t = '\0';
14562 i386_comment_chars = n;
14563 }
14564 #endif
14565 break;
14566
14567 case OPTION_MARCH:
14568 saved = xstrdup (arg);
14569 arch = saved;
14570 /* Allow -march=+nosse. */
14571 if (*arch == '+')
14572 arch++;
14573 do
14574 {
14575 char *vsz;
14576
14577 if (*arch == '.')
14578 as_fatal (_("invalid -march= option: `%s'"), arg);
14579 next = strchr (arch, '+');
14580 if (next)
14581 *next++ = '\0';
14582 vsz = strchr (arch, '/');
14583 if (vsz)
14584 *vsz++ = '\0';
14585 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
14586 {
14587 if (vsz && cpu_arch[j].vsz != vsz_set)
14588 continue;
14589
14590 if (arch == saved && cpu_arch[j].type != PROCESSOR_NONE
14591 && strcmp (arch, cpu_arch[j].name) == 0)
14592 {
14593 /* Processor. */
14594 if (! cpu_arch[j].enable.bitfield.cpui386)
14595 continue;
14596
14597 cpu_arch_name = cpu_arch[j].name;
14598 free (cpu_sub_arch_name);
14599 cpu_sub_arch_name = NULL;
14600 cpu_arch_flags = cpu_arch[j].enable;
14601 cpu_arch_isa = cpu_arch[j].type;
14602 cpu_arch_isa_flags = cpu_arch[j].enable;
14603 if (!cpu_arch_tune_set)
14604 cpu_arch_tune = cpu_arch_isa;
14605 vector_size = VSZ_DEFAULT;
14606 break;
14607 }
14608 else if (cpu_arch[j].type == PROCESSOR_NONE
14609 && strcmp (arch, cpu_arch[j].name) == 0
14610 && !cpu_flags_all_zero (&cpu_arch[j].enable))
14611 {
14612 /* ISA extension. */
14613 isa_enable (j);
14614
14615 switch (cpu_arch[j].vsz)
14616 {
14617 default:
14618 break;
14619
14620 case vsz_set:
14621 if (vsz)
14622 {
14623 char *end;
14624 unsigned long val = strtoul (vsz, &end, 0);
14625
14626 if (*end)
14627 val = 0;
14628 switch (val)
14629 {
14630 case 512: vector_size = VSZ512; break;
14631 case 256: vector_size = VSZ256; break;
14632 case 128: vector_size = VSZ128; break;
14633 default:
14634 as_warn (_("Unrecognized vector size specifier ignored"));
14635 break;
14636 }
14637 break;
14638 }
14639 /* Fall through. */
14640 case vsz_reset:
14641 vector_size = VSZ_DEFAULT;
14642 break;
14643 }
14644
14645 break;
14646 }
14647 }
14648
14649 if (j >= ARRAY_SIZE (cpu_arch) && startswith (arch, "no"))
14650 {
14651 /* Disable an ISA extension. */
14652 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
14653 if (cpu_arch[j].type == PROCESSOR_NONE
14654 && strcmp (arch + 2, cpu_arch[j].name) == 0)
14655 {
14656 isa_disable (j);
14657 if (cpu_arch[j].vsz == vsz_set)
14658 vector_size = VSZ_DEFAULT;
14659 break;
14660 }
14661 }
14662
14663 if (j >= ARRAY_SIZE (cpu_arch))
14664 as_fatal (_("invalid -march= option: `%s'"), arg);
14665
14666 arch = next;
14667 }
14668 while (next != NULL);
14669 free (saved);
14670 break;
14671
14672 case OPTION_MTUNE:
14673 if (*arg == '.')
14674 as_fatal (_("invalid -mtune= option: `%s'"), arg);
14675 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
14676 {
14677 if (cpu_arch[j].type != PROCESSOR_NONE
14678 && strcmp (arg, cpu_arch[j].name) == 0)
14679 {
14680 cpu_arch_tune_set = 1;
14681 cpu_arch_tune = cpu_arch [j].type;
14682 break;
14683 }
14684 }
14685 if (j >= ARRAY_SIZE (cpu_arch))
14686 as_fatal (_("invalid -mtune= option: `%s'"), arg);
14687 break;
14688
14689 case OPTION_MMNEMONIC:
14690 if (strcasecmp (arg, "att") == 0)
14691 intel_mnemonic = 0;
14692 else if (strcasecmp (arg, "intel") == 0)
14693 intel_mnemonic = 1;
14694 else
14695 as_fatal (_("invalid -mmnemonic= option: `%s'"), arg);
14696 break;
14697
14698 case OPTION_MSYNTAX:
14699 if (strcasecmp (arg, "att") == 0)
14700 intel_syntax = 0;
14701 else if (strcasecmp (arg, "intel") == 0)
14702 intel_syntax = 1;
14703 else
14704 as_fatal (_("invalid -msyntax= option: `%s'"), arg);
14705 break;
14706
14707 case OPTION_MINDEX_REG:
14708 allow_index_reg = 1;
14709 break;
14710
14711 case OPTION_MNAKED_REG:
14712 allow_naked_reg = 1;
14713 break;
14714
14715 case OPTION_MSSE2AVX:
14716 sse2avx = 1;
14717 break;
14718
14719 case OPTION_MUSE_UNALIGNED_VECTOR_MOVE:
14720 use_unaligned_vector_move = 1;
14721 break;
14722
14723 case OPTION_MSSE_CHECK:
14724 if (strcasecmp (arg, "error") == 0)
14725 sse_check = check_error;
14726 else if (strcasecmp (arg, "warning") == 0)
14727 sse_check = check_warning;
14728 else if (strcasecmp (arg, "none") == 0)
14729 sse_check = check_none;
14730 else
14731 as_fatal (_("invalid -msse-check= option: `%s'"), arg);
14732 break;
14733
14734 case OPTION_MOPERAND_CHECK:
14735 if (strcasecmp (arg, "error") == 0)
14736 operand_check = check_error;
14737 else if (strcasecmp (arg, "warning") == 0)
14738 operand_check = check_warning;
14739 else if (strcasecmp (arg, "none") == 0)
14740 operand_check = check_none;
14741 else
14742 as_fatal (_("invalid -moperand-check= option: `%s'"), arg);
14743 break;
14744
14745 case OPTION_MAVXSCALAR:
14746 if (strcasecmp (arg, "128") == 0)
14747 avxscalar = vex128;
14748 else if (strcasecmp (arg, "256") == 0)
14749 avxscalar = vex256;
14750 else
14751 as_fatal (_("invalid -mavxscalar= option: `%s'"), arg);
14752 break;
14753
14754 case OPTION_MVEXWIG:
14755 if (strcmp (arg, "0") == 0)
14756 vexwig = vexw0;
14757 else if (strcmp (arg, "1") == 0)
14758 vexwig = vexw1;
14759 else
14760 as_fatal (_("invalid -mvexwig= option: `%s'"), arg);
14761 break;
14762
14763 case OPTION_MADD_BND_PREFIX:
14764 add_bnd_prefix = 1;
14765 break;
14766
14767 case OPTION_MEVEXLIG:
14768 if (strcmp (arg, "128") == 0)
14769 evexlig = evexl128;
14770 else if (strcmp (arg, "256") == 0)
14771 evexlig = evexl256;
14772 else if (strcmp (arg, "512") == 0)
14773 evexlig = evexl512;
14774 else
14775 as_fatal (_("invalid -mevexlig= option: `%s'"), arg);
14776 break;
14777
14778 case OPTION_MEVEXRCIG:
14779 if (strcmp (arg, "rne") == 0)
14780 evexrcig = rne;
14781 else if (strcmp (arg, "rd") == 0)
14782 evexrcig = rd;
14783 else if (strcmp (arg, "ru") == 0)
14784 evexrcig = ru;
14785 else if (strcmp (arg, "rz") == 0)
14786 evexrcig = rz;
14787 else
14788 as_fatal (_("invalid -mevexrcig= option: `%s'"), arg);
14789 break;
14790
14791 case OPTION_MEVEXWIG:
14792 if (strcmp (arg, "0") == 0)
14793 evexwig = evexw0;
14794 else if (strcmp (arg, "1") == 0)
14795 evexwig = evexw1;
14796 else
14797 as_fatal (_("invalid -mevexwig= option: `%s'"), arg);
14798 break;
14799
14800 # if defined (TE_PE) || defined (TE_PEP)
14801 case OPTION_MBIG_OBJ:
14802 use_big_obj = 1;
14803 break;
14804 #endif
14805
14806 case OPTION_MOMIT_LOCK_PREFIX:
14807 if (strcasecmp (arg, "yes") == 0)
14808 omit_lock_prefix = 1;
14809 else if (strcasecmp (arg, "no") == 0)
14810 omit_lock_prefix = 0;
14811 else
14812 as_fatal (_("invalid -momit-lock-prefix= option: `%s'"), arg);
14813 break;
14814
14815 case OPTION_MFENCE_AS_LOCK_ADD:
14816 if (strcasecmp (arg, "yes") == 0)
14817 avoid_fence = 1;
14818 else if (strcasecmp (arg, "no") == 0)
14819 avoid_fence = 0;
14820 else
14821 as_fatal (_("invalid -mfence-as-lock-add= option: `%s'"), arg);
14822 break;
14823
14824 case OPTION_MLFENCE_AFTER_LOAD:
14825 if (strcasecmp (arg, "yes") == 0)
14826 lfence_after_load = 1;
14827 else if (strcasecmp (arg, "no") == 0)
14828 lfence_after_load = 0;
14829 else
14830 as_fatal (_("invalid -mlfence-after-load= option: `%s'"), arg);
14831 break;
14832
14833 case OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH:
14834 if (strcasecmp (arg, "all") == 0)
14835 {
14836 lfence_before_indirect_branch = lfence_branch_all;
14837 if (lfence_before_ret == lfence_before_ret_none)
14838 lfence_before_ret = lfence_before_ret_shl;
14839 }
14840 else if (strcasecmp (arg, "memory") == 0)
14841 lfence_before_indirect_branch = lfence_branch_memory;
14842 else if (strcasecmp (arg, "register") == 0)
14843 lfence_before_indirect_branch = lfence_branch_register;
14844 else if (strcasecmp (arg, "none") == 0)
14845 lfence_before_indirect_branch = lfence_branch_none;
14846 else
14847 as_fatal (_("invalid -mlfence-before-indirect-branch= option: `%s'"),
14848 arg);
14849 break;
14850
14851 case OPTION_MLFENCE_BEFORE_RET:
14852 if (strcasecmp (arg, "or") == 0)
14853 lfence_before_ret = lfence_before_ret_or;
14854 else if (strcasecmp (arg, "not") == 0)
14855 lfence_before_ret = lfence_before_ret_not;
14856 else if (strcasecmp (arg, "shl") == 0 || strcasecmp (arg, "yes") == 0)
14857 lfence_before_ret = lfence_before_ret_shl;
14858 else if (strcasecmp (arg, "none") == 0)
14859 lfence_before_ret = lfence_before_ret_none;
14860 else
14861 as_fatal (_("invalid -mlfence-before-ret= option: `%s'"),
14862 arg);
14863 break;
14864
14865 case OPTION_MRELAX_RELOCATIONS:
14866 if (strcasecmp (arg, "yes") == 0)
14867 generate_relax_relocations = 1;
14868 else if (strcasecmp (arg, "no") == 0)
14869 generate_relax_relocations = 0;
14870 else
14871 as_fatal (_("invalid -mrelax-relocations= option: `%s'"), arg);
14872 break;
14873
14874 case OPTION_MALIGN_BRANCH_BOUNDARY:
14875 {
14876 char *end;
14877 long int align = strtoul (arg, &end, 0);
14878 if (*end == '\0')
14879 {
14880 if (align == 0)
14881 {
14882 align_branch_power = 0;
14883 break;
14884 }
14885 else if (align >= 16)
14886 {
14887 int align_power;
14888 for (align_power = 0;
14889 (align & 1) == 0;
14890 align >>= 1, align_power++)
14891 continue;
14892 /* Limit alignment power to 31. */
14893 if (align == 1 && align_power < 32)
14894 {
14895 align_branch_power = align_power;
14896 break;
14897 }
14898 }
14899 }
14900 as_fatal (_("invalid -malign-branch-boundary= value: %s"), arg);
14901 }
14902 break;
14903
14904 case OPTION_MALIGN_BRANCH_PREFIX_SIZE:
14905 {
14906 char *end;
14907 int align = strtoul (arg, &end, 0);
14908 /* Some processors only support 5 prefixes. */
14909 if (*end == '\0' && align >= 0 && align < 6)
14910 {
14911 align_branch_prefix_size = align;
14912 break;
14913 }
14914 as_fatal (_("invalid -malign-branch-prefix-size= value: %s"),
14915 arg);
14916 }
14917 break;
14918
14919 case OPTION_MALIGN_BRANCH:
14920 align_branch = 0;
14921 saved = xstrdup (arg);
14922 type = saved;
14923 do
14924 {
14925 next = strchr (type, '+');
14926 if (next)
14927 *next++ = '\0';
14928 if (strcasecmp (type, "jcc") == 0)
14929 align_branch |= align_branch_jcc_bit;
14930 else if (strcasecmp (type, "fused") == 0)
14931 align_branch |= align_branch_fused_bit;
14932 else if (strcasecmp (type, "jmp") == 0)
14933 align_branch |= align_branch_jmp_bit;
14934 else if (strcasecmp (type, "call") == 0)
14935 align_branch |= align_branch_call_bit;
14936 else if (strcasecmp (type, "ret") == 0)
14937 align_branch |= align_branch_ret_bit;
14938 else if (strcasecmp (type, "indirect") == 0)
14939 align_branch |= align_branch_indirect_bit;
14940 else
14941 as_fatal (_("invalid -malign-branch= option: `%s'"), arg);
14942 type = next;
14943 }
14944 while (next != NULL);
14945 free (saved);
14946 break;
14947
14948 case OPTION_MBRANCHES_WITH_32B_BOUNDARIES:
14949 align_branch_power = 5;
14950 align_branch_prefix_size = 5;
14951 align_branch = (align_branch_jcc_bit
14952 | align_branch_fused_bit
14953 | align_branch_jmp_bit);
14954 break;
14955
14956 case OPTION_MAMD64:
14957 isa64 = amd64;
14958 break;
14959
14960 case OPTION_MINTEL64:
14961 isa64 = intel64;
14962 break;
14963
14964 case 'O':
14965 if (arg == NULL)
14966 {
14967 optimize = 1;
14968 /* Turn off -Os. */
14969 optimize_for_space = 0;
14970 }
14971 else if (*arg == 's')
14972 {
14973 optimize_for_space = 1;
14974 /* Turn on all encoding optimizations. */
14975 optimize = INT_MAX;
14976 }
14977 else
14978 {
14979 optimize = atoi (arg);
14980 /* Turn off -Os. */
14981 optimize_for_space = 0;
14982 }
14983 break;
14984
14985 default:
14986 return 0;
14987 }
14988 return 1;
14989 }
14990
14991 #define MESSAGE_TEMPLATE \
14992 " "
14993
14994 static char *
14995 output_message (FILE *stream, char *p, char *message, char *start,
14996 int *left_p, const char *name, int len)
14997 {
14998 int size = sizeof (MESSAGE_TEMPLATE);
14999 int left = *left_p;
15000
15001 /* Reserve 2 spaces for ", " or ",\0" */
15002 left -= len + 2;
15003
15004 /* Check if there is any room. */
15005 if (left >= 0)
15006 {
15007 if (p != start)
15008 {
15009 *p++ = ',';
15010 *p++ = ' ';
15011 }
15012 p = mempcpy (p, name, len);
15013 }
15014 else
15015 {
15016 /* Output the current message now and start a new one. */
15017 *p++ = ',';
15018 *p = '\0';
15019 fprintf (stream, "%s\n", message);
15020 p = start;
15021 left = size - (start - message) - len - 2;
15022
15023 gas_assert (left >= 0);
15024
15025 p = mempcpy (p, name, len);
15026 }
15027
15028 *left_p = left;
15029 return p;
15030 }
15031
15032 static void
15033 show_arch (FILE *stream, int ext, int check)
15034 {
15035 static char message[] = MESSAGE_TEMPLATE;
15036 char *start = message + 27;
15037 char *p;
15038 int size = sizeof (MESSAGE_TEMPLATE);
15039 int left;
15040 const char *name;
15041 int len;
15042 unsigned int j;
15043
15044 p = start;
15045 left = size - (start - message);
15046
15047 if (!ext && check)
15048 {
15049 p = output_message (stream, p, message, start, &left,
15050 STRING_COMMA_LEN ("default"));
15051 p = output_message (stream, p, message, start, &left,
15052 STRING_COMMA_LEN ("push"));
15053 p = output_message (stream, p, message, start, &left,
15054 STRING_COMMA_LEN ("pop"));
15055 }
15056
15057 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
15058 {
15059 /* Should it be skipped? */
15060 if (cpu_arch [j].skip)
15061 continue;
15062
15063 name = cpu_arch [j].name;
15064 len = cpu_arch [j].len;
15065 if (cpu_arch[j].type == PROCESSOR_NONE)
15066 {
15067 /* It is an extension. Skip if we aren't asked to show it. */
15068 if (!ext || cpu_flags_all_zero (&cpu_arch[j].enable))
15069 continue;
15070 }
15071 else if (ext)
15072 {
15073 /* It is an processor. Skip if we show only extension. */
15074 continue;
15075 }
15076 else if (check && ! cpu_arch[j].enable.bitfield.cpui386)
15077 {
15078 /* It is an impossible processor - skip. */
15079 continue;
15080 }
15081
15082 p = output_message (stream, p, message, start, &left, name, len);
15083 }
15084
15085 /* Display disabled extensions. */
15086 if (ext)
15087 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
15088 {
15089 char *str;
15090
15091 if (cpu_arch[j].type != PROCESSOR_NONE
15092 || !cpu_flags_all_zero (&cpu_arch[j].enable))
15093 continue;
15094 str = xasprintf ("no%s", cpu_arch[j].name);
15095 p = output_message (stream, p, message, start, &left, str,
15096 strlen (str));
15097 free (str);
15098 }
15099
15100 *p = '\0';
15101 fprintf (stream, "%s\n", message);
15102 }
15103
15104 void
15105 md_show_usage (FILE *stream)
15106 {
15107 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15108 fprintf (stream, _("\
15109 -Qy, -Qn ignored\n\
15110 -V print assembler version number\n\
15111 -k ignored\n"));
15112 #endif
15113 fprintf (stream, _("\
15114 -n do not optimize code alignment\n\
15115 -O{012s} attempt some code optimizations\n\
15116 -q quieten some warnings\n"));
15117 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15118 fprintf (stream, _("\
15119 -s ignored\n"));
15120 #endif
15121 #ifdef BFD64
15122 # if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15123 fprintf (stream, _("\
15124 --32/--64/--x32 generate 32bit/64bit/x32 object\n"));
15125 # elif defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O)
15126 fprintf (stream, _("\
15127 --32/--64 generate 32bit/64bit object\n"));
15128 # endif
15129 #endif
15130 #ifdef SVR4_COMMENT_CHARS
15131 fprintf (stream, _("\
15132 --divide do not treat `/' as a comment character\n"));
15133 #else
15134 fprintf (stream, _("\
15135 --divide ignored\n"));
15136 #endif
15137 fprintf (stream, _("\
15138 -march=CPU[,+EXTENSION...]\n\
15139 generate code for CPU and EXTENSION, CPU is one of:\n"));
15140 show_arch (stream, 0, 1);
15141 fprintf (stream, _("\
15142 EXTENSION is combination of (possibly \"no\"-prefixed):\n"));
15143 show_arch (stream, 1, 0);
15144 fprintf (stream, _("\
15145 -mtune=CPU optimize for CPU, CPU is one of:\n"));
15146 show_arch (stream, 0, 0);
15147 fprintf (stream, _("\
15148 -msse2avx encode SSE instructions with VEX prefix\n"));
15149 fprintf (stream, _("\
15150 -muse-unaligned-vector-move\n\
15151 encode aligned vector move as unaligned vector move\n"));
15152 fprintf (stream, _("\
15153 -msse-check=[none|error|warning] (default: warning)\n\
15154 check SSE instructions\n"));
15155 fprintf (stream, _("\
15156 -moperand-check=[none|error|warning] (default: warning)\n\
15157 check operand combinations for validity\n"));
15158 fprintf (stream, _("\
15159 -mavxscalar=[128|256] (default: 128)\n\
15160 encode scalar AVX instructions with specific vector\n\
15161 length\n"));
15162 fprintf (stream, _("\
15163 -mvexwig=[0|1] (default: 0)\n\
15164 encode VEX instructions with specific VEX.W value\n\
15165 for VEX.W bit ignored instructions\n"));
15166 fprintf (stream, _("\
15167 -mevexlig=[128|256|512] (default: 128)\n\
15168 encode scalar EVEX instructions with specific vector\n\
15169 length\n"));
15170 fprintf (stream, _("\
15171 -mevexwig=[0|1] (default: 0)\n\
15172 encode EVEX instructions with specific EVEX.W value\n\
15173 for EVEX.W bit ignored instructions\n"));
15174 fprintf (stream, _("\
15175 -mevexrcig=[rne|rd|ru|rz] (default: rne)\n\
15176 encode EVEX instructions with specific EVEX.RC value\n\
15177 for SAE-only ignored instructions\n"));
15178 fprintf (stream, _("\
15179 -mmnemonic=[att|intel] "));
15180 if (SYSV386_COMPAT)
15181 fprintf (stream, _("(default: att)\n"));
15182 else
15183 fprintf (stream, _("(default: intel)\n"));
15184 fprintf (stream, _("\
15185 use AT&T/Intel mnemonic\n"));
15186 fprintf (stream, _("\
15187 -msyntax=[att|intel] (default: att)\n\
15188 use AT&T/Intel syntax\n"));
15189 fprintf (stream, _("\
15190 -mindex-reg support pseudo index registers\n"));
15191 fprintf (stream, _("\
15192 -mnaked-reg don't require `%%' prefix for registers\n"));
15193 fprintf (stream, _("\
15194 -madd-bnd-prefix add BND prefix for all valid branches\n"));
15195 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15196 fprintf (stream, _("\
15197 -mshared disable branch optimization for shared code\n"));
15198 fprintf (stream, _("\
15199 -mx86-used-note=[no|yes] "));
15200 if (DEFAULT_X86_USED_NOTE)
15201 fprintf (stream, _("(default: yes)\n"));
15202 else
15203 fprintf (stream, _("(default: no)\n"));
15204 fprintf (stream, _("\
15205 generate x86 used ISA and feature properties\n"));
15206 #endif
15207 #if defined (TE_PE) || defined (TE_PEP)
15208 fprintf (stream, _("\
15209 -mbig-obj generate big object files\n"));
15210 #endif
15211 fprintf (stream, _("\
15212 -momit-lock-prefix=[no|yes] (default: no)\n\
15213 strip all lock prefixes\n"));
15214 fprintf (stream, _("\
15215 -mfence-as-lock-add=[no|yes] (default: no)\n\
15216 encode lfence, mfence and sfence as\n\
15217 lock addl $0x0, (%%{re}sp)\n"));
15218 fprintf (stream, _("\
15219 -mrelax-relocations=[no|yes] "));
15220 if (DEFAULT_GENERATE_X86_RELAX_RELOCATIONS)
15221 fprintf (stream, _("(default: yes)\n"));
15222 else
15223 fprintf (stream, _("(default: no)\n"));
15224 fprintf (stream, _("\
15225 generate relax relocations\n"));
15226 fprintf (stream, _("\
15227 -malign-branch-boundary=NUM (default: 0)\n\
15228 align branches within NUM byte boundary\n"));
15229 fprintf (stream, _("\
15230 -malign-branch=TYPE[+TYPE...] (default: jcc+fused+jmp)\n\
15231 TYPE is combination of jcc, fused, jmp, call, ret,\n\
15232 indirect\n\
15233 specify types of branches to align\n"));
15234 fprintf (stream, _("\
15235 -malign-branch-prefix-size=NUM (default: 5)\n\
15236 align branches with NUM prefixes per instruction\n"));
15237 fprintf (stream, _("\
15238 -mbranches-within-32B-boundaries\n\
15239 align branches within 32 byte boundary\n"));
15240 fprintf (stream, _("\
15241 -mlfence-after-load=[no|yes] (default: no)\n\
15242 generate lfence after load\n"));
15243 fprintf (stream, _("\
15244 -mlfence-before-indirect-branch=[none|all|register|memory] (default: none)\n\
15245 generate lfence before indirect near branch\n"));
15246 fprintf (stream, _("\
15247 -mlfence-before-ret=[none|or|not|shl|yes] (default: none)\n\
15248 generate lfence before ret\n"));
15249 fprintf (stream, _("\
15250 -mamd64 accept only AMD64 ISA [default]\n"));
15251 fprintf (stream, _("\
15252 -mintel64 accept only Intel64 ISA\n"));
15253 }
15254
15255 #if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
15256 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
15257 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
15258
15259 /* Pick the target format to use. */
15260
15261 const char *
15262 i386_target_format (void)
15263 {
15264 if (startswith (default_arch, "x86_64"))
15265 {
15266 update_code_flag (CODE_64BIT, 1);
15267 if (default_arch[6] == '\0')
15268 x86_elf_abi = X86_64_ABI;
15269 else
15270 x86_elf_abi = X86_64_X32_ABI;
15271 }
15272 else if (!strcmp (default_arch, "i386"))
15273 update_code_flag (CODE_32BIT, 1);
15274 else if (!strcmp (default_arch, "iamcu"))
15275 {
15276 update_code_flag (CODE_32BIT, 1);
15277 if (cpu_arch_isa == PROCESSOR_UNKNOWN)
15278 {
15279 static const i386_cpu_flags iamcu_flags = CPU_IAMCU_FLAGS;
15280 cpu_arch_name = "iamcu";
15281 free (cpu_sub_arch_name);
15282 cpu_sub_arch_name = NULL;
15283 cpu_arch_flags = iamcu_flags;
15284 cpu_arch_isa = PROCESSOR_IAMCU;
15285 cpu_arch_isa_flags = iamcu_flags;
15286 if (!cpu_arch_tune_set)
15287 cpu_arch_tune = PROCESSOR_IAMCU;
15288 }
15289 else if (cpu_arch_isa != PROCESSOR_IAMCU)
15290 as_fatal (_("Intel MCU doesn't support `%s' architecture"),
15291 cpu_arch_name);
15292 }
15293 else
15294 as_fatal (_("unknown architecture"));
15295
15296 if (cpu_flags_all_zero (&cpu_arch_isa_flags))
15297 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
15298
15299 switch (OUTPUT_FLAVOR)
15300 {
15301 #if defined (OBJ_MAYBE_AOUT) || defined (OBJ_AOUT)
15302 case bfd_target_aout_flavour:
15303 return AOUT_TARGET_FORMAT;
15304 #endif
15305 #if defined (OBJ_MAYBE_COFF) || defined (OBJ_COFF)
15306 # if defined (TE_PE) || defined (TE_PEP)
15307 case bfd_target_coff_flavour:
15308 if (flag_code == CODE_64BIT)
15309 {
15310 object_64bit = 1;
15311 return use_big_obj ? "pe-bigobj-x86-64" : "pe-x86-64";
15312 }
15313 return use_big_obj ? "pe-bigobj-i386" : "pe-i386";
15314 # elif defined (TE_GO32)
15315 case bfd_target_coff_flavour:
15316 return "coff-go32";
15317 # else
15318 case bfd_target_coff_flavour:
15319 return "coff-i386";
15320 # endif
15321 #endif
15322 #if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
15323 case bfd_target_elf_flavour:
15324 {
15325 const char *format;
15326
15327 switch (x86_elf_abi)
15328 {
15329 default:
15330 format = ELF_TARGET_FORMAT;
15331 #ifndef TE_SOLARIS
15332 tls_get_addr = "___tls_get_addr";
15333 #endif
15334 break;
15335 case X86_64_ABI:
15336 use_rela_relocations = 1;
15337 object_64bit = 1;
15338 #ifndef TE_SOLARIS
15339 tls_get_addr = "__tls_get_addr";
15340 #endif
15341 format = ELF_TARGET_FORMAT64;
15342 break;
15343 case X86_64_X32_ABI:
15344 use_rela_relocations = 1;
15345 object_64bit = 1;
15346 #ifndef TE_SOLARIS
15347 tls_get_addr = "__tls_get_addr";
15348 #endif
15349 disallow_64bit_reloc = 1;
15350 format = ELF_TARGET_FORMAT32;
15351 break;
15352 }
15353 if (cpu_arch_isa == PROCESSOR_IAMCU)
15354 {
15355 if (x86_elf_abi != I386_ABI)
15356 as_fatal (_("Intel MCU is 32bit only"));
15357 return ELF_TARGET_IAMCU_FORMAT;
15358 }
15359 else
15360 return format;
15361 }
15362 #endif
15363 #if defined (OBJ_MACH_O)
15364 case bfd_target_mach_o_flavour:
15365 if (flag_code == CODE_64BIT)
15366 {
15367 use_rela_relocations = 1;
15368 object_64bit = 1;
15369 return "mach-o-x86-64";
15370 }
15371 else
15372 return "mach-o-i386";
15373 #endif
15374 default:
15375 abort ();
15376 return NULL;
15377 }
15378 }
15379
15380 #endif /* OBJ_MAYBE_ more than one */
15381 \f
15382 symbolS *
15383 md_undefined_symbol (char *name)
15384 {
15385 if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
15386 && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
15387 && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
15388 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
15389 {
15390 if (!GOT_symbol)
15391 {
15392 if (symbol_find (name))
15393 as_bad (_("GOT already in symbol table"));
15394 GOT_symbol = symbol_new (name, undefined_section,
15395 &zero_address_frag, 0);
15396 };
15397 return GOT_symbol;
15398 }
15399 return 0;
15400 }
15401
15402 /* Round up a section size to the appropriate boundary. */
15403
15404 valueT
15405 md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
15406 {
15407 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
15408 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
15409 {
15410 /* For a.out, force the section size to be aligned. If we don't do
15411 this, BFD will align it for us, but it will not write out the
15412 final bytes of the section. This may be a bug in BFD, but it is
15413 easier to fix it here since that is how the other a.out targets
15414 work. */
15415 int align;
15416
15417 align = bfd_section_alignment (segment);
15418 size = ((size + (1 << align) - 1) & (-((valueT) 1 << align)));
15419 }
15420 #endif
15421
15422 return size;
15423 }
15424
15425 /* On the i386, PC-relative offsets are relative to the start of the
15426 next instruction. That is, the address of the offset, plus its
15427 size, since the offset is always the last part of the insn. */
15428
15429 long
15430 md_pcrel_from (fixS *fixP)
15431 {
15432 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
15433 }
15434
15435 #ifndef I386COFF
15436
15437 static void
15438 s_bss (int ignore ATTRIBUTE_UNUSED)
15439 {
15440 int temp;
15441
15442 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15443 if (IS_ELF)
15444 obj_elf_section_change_hook ();
15445 #endif
15446 temp = get_absolute_expression ();
15447 subseg_set (bss_section, (subsegT) temp);
15448 demand_empty_rest_of_line ();
15449 }
15450
15451 #endif
15452
15453 /* Remember constant directive. */
15454
15455 void
15456 i386_cons_align (int ignore ATTRIBUTE_UNUSED)
15457 {
15458 if (last_insn.kind != last_insn_directive
15459 && (bfd_section_flags (now_seg) & SEC_CODE))
15460 {
15461 last_insn.seg = now_seg;
15462 last_insn.kind = last_insn_directive;
15463 last_insn.name = "constant directive";
15464 last_insn.file = as_where (&last_insn.line);
15465 if (lfence_before_ret != lfence_before_ret_none)
15466 {
15467 if (lfence_before_indirect_branch != lfence_branch_none)
15468 as_warn (_("constant directive skips -mlfence-before-ret "
15469 "and -mlfence-before-indirect-branch"));
15470 else
15471 as_warn (_("constant directive skips -mlfence-before-ret"));
15472 }
15473 else if (lfence_before_indirect_branch != lfence_branch_none)
15474 as_warn (_("constant directive skips -mlfence-before-indirect-branch"));
15475 }
15476 }
15477
15478 int
15479 i386_validate_fix (fixS *fixp)
15480 {
15481 if (fixp->fx_addsy && S_GET_SEGMENT(fixp->fx_addsy) == reg_section)
15482 {
15483 reloc_howto_type *howto;
15484
15485 howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
15486 as_bad_where (fixp->fx_file, fixp->fx_line,
15487 _("invalid %s relocation against register"),
15488 howto ? howto->name : "<unknown>");
15489 return 0;
15490 }
15491
15492 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15493 if (fixp->fx_r_type == BFD_RELOC_SIZE32
15494 || fixp->fx_r_type == BFD_RELOC_SIZE64)
15495 return IS_ELF && fixp->fx_addsy
15496 && (!S_IS_DEFINED (fixp->fx_addsy)
15497 || S_IS_EXTERNAL (fixp->fx_addsy));
15498 #endif
15499
15500 if (fixp->fx_subsy)
15501 {
15502 if (fixp->fx_subsy == GOT_symbol)
15503 {
15504 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
15505 {
15506 if (!object_64bit)
15507 abort ();
15508 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15509 if (fixp->fx_tcbit2)
15510 fixp->fx_r_type = (fixp->fx_tcbit
15511 ? BFD_RELOC_X86_64_REX_GOTPCRELX
15512 : BFD_RELOC_X86_64_GOTPCRELX);
15513 else
15514 #endif
15515 fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
15516 }
15517 else
15518 {
15519 if (!object_64bit)
15520 fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
15521 else
15522 fixp->fx_r_type = BFD_RELOC_X86_64_GOTOFF64;
15523 }
15524 fixp->fx_subsy = 0;
15525 }
15526 }
15527 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15528 else
15529 {
15530 /* NB: Commit 292676c1 resolved PLT32 reloc aganst local symbol
15531 to section. Since PLT32 relocation must be against symbols,
15532 turn such PLT32 relocation into PC32 relocation. */
15533 if (fixp->fx_addsy
15534 && (fixp->fx_r_type == BFD_RELOC_386_PLT32
15535 || fixp->fx_r_type == BFD_RELOC_X86_64_PLT32)
15536 && symbol_section_p (fixp->fx_addsy))
15537 fixp->fx_r_type = BFD_RELOC_32_PCREL;
15538 if (!object_64bit)
15539 {
15540 if (fixp->fx_r_type == BFD_RELOC_386_GOT32
15541 && fixp->fx_tcbit2)
15542 fixp->fx_r_type = BFD_RELOC_386_GOT32X;
15543 }
15544 }
15545 #endif
15546
15547 return 1;
15548 }
15549
15550 arelent *
15551 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
15552 {
15553 arelent *rel;
15554 bfd_reloc_code_real_type code;
15555
15556 switch (fixp->fx_r_type)
15557 {
15558 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15559 symbolS *sym;
15560
15561 case BFD_RELOC_SIZE32:
15562 case BFD_RELOC_SIZE64:
15563 if (fixp->fx_addsy
15564 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))
15565 && (!fixp->fx_subsy
15566 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))))
15567 sym = fixp->fx_addsy;
15568 else if (fixp->fx_subsy
15569 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))
15570 && (!fixp->fx_addsy
15571 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))))
15572 sym = fixp->fx_subsy;
15573 else
15574 sym = NULL;
15575 if (IS_ELF && sym && S_IS_DEFINED (sym) && !S_IS_EXTERNAL (sym))
15576 {
15577 /* Resolve size relocation against local symbol to size of
15578 the symbol plus addend. */
15579 valueT value = S_GET_SIZE (sym);
15580
15581 if (symbol_get_bfdsym (sym)->flags & BSF_SECTION_SYM)
15582 value = bfd_section_size (S_GET_SEGMENT (sym));
15583 if (sym == fixp->fx_subsy)
15584 {
15585 value = -value;
15586 if (fixp->fx_addsy)
15587 value += S_GET_VALUE (fixp->fx_addsy);
15588 }
15589 else if (fixp->fx_subsy)
15590 value -= S_GET_VALUE (fixp->fx_subsy);
15591 value += fixp->fx_offset;
15592 if (fixp->fx_r_type == BFD_RELOC_SIZE32
15593 && object_64bit
15594 && !fits_in_unsigned_long (value))
15595 as_bad_where (fixp->fx_file, fixp->fx_line,
15596 _("symbol size computation overflow"));
15597 fixp->fx_addsy = NULL;
15598 fixp->fx_subsy = NULL;
15599 md_apply_fix (fixp, (valueT *) &value, NULL);
15600 return NULL;
15601 }
15602 if (!fixp->fx_addsy || fixp->fx_subsy)
15603 {
15604 as_bad_where (fixp->fx_file, fixp->fx_line,
15605 "unsupported expression involving @size");
15606 return NULL;
15607 }
15608 #endif
15609 /* Fall through. */
15610
15611 case BFD_RELOC_X86_64_PLT32:
15612 case BFD_RELOC_X86_64_GOT32:
15613 case BFD_RELOC_X86_64_GOTPCREL:
15614 case BFD_RELOC_X86_64_GOTPCRELX:
15615 case BFD_RELOC_X86_64_REX_GOTPCRELX:
15616 case BFD_RELOC_386_PLT32:
15617 case BFD_RELOC_386_GOT32:
15618 case BFD_RELOC_386_GOT32X:
15619 case BFD_RELOC_386_GOTOFF:
15620 case BFD_RELOC_386_GOTPC:
15621 case BFD_RELOC_386_TLS_GD:
15622 case BFD_RELOC_386_TLS_LDM:
15623 case BFD_RELOC_386_TLS_LDO_32:
15624 case BFD_RELOC_386_TLS_IE_32:
15625 case BFD_RELOC_386_TLS_IE:
15626 case BFD_RELOC_386_TLS_GOTIE:
15627 case BFD_RELOC_386_TLS_LE_32:
15628 case BFD_RELOC_386_TLS_LE:
15629 case BFD_RELOC_386_TLS_GOTDESC:
15630 case BFD_RELOC_386_TLS_DESC_CALL:
15631 case BFD_RELOC_X86_64_TLSGD:
15632 case BFD_RELOC_X86_64_TLSLD:
15633 case BFD_RELOC_X86_64_DTPOFF32:
15634 case BFD_RELOC_X86_64_DTPOFF64:
15635 case BFD_RELOC_X86_64_GOTTPOFF:
15636 case BFD_RELOC_X86_64_TPOFF32:
15637 case BFD_RELOC_X86_64_TPOFF64:
15638 case BFD_RELOC_X86_64_GOTOFF64:
15639 case BFD_RELOC_X86_64_GOTPC32:
15640 case BFD_RELOC_X86_64_GOT64:
15641 case BFD_RELOC_X86_64_GOTPCREL64:
15642 case BFD_RELOC_X86_64_GOTPC64:
15643 case BFD_RELOC_X86_64_GOTPLT64:
15644 case BFD_RELOC_X86_64_PLTOFF64:
15645 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
15646 case BFD_RELOC_X86_64_TLSDESC_CALL:
15647 case BFD_RELOC_RVA:
15648 case BFD_RELOC_VTABLE_ENTRY:
15649 case BFD_RELOC_VTABLE_INHERIT:
15650 #ifdef TE_PE
15651 case BFD_RELOC_32_SECREL:
15652 case BFD_RELOC_16_SECIDX:
15653 #endif
15654 code = fixp->fx_r_type;
15655 break;
15656 case BFD_RELOC_X86_64_32S:
15657 if (!fixp->fx_pcrel)
15658 {
15659 /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32. */
15660 code = fixp->fx_r_type;
15661 break;
15662 }
15663 /* Fall through. */
15664 default:
15665 if (fixp->fx_pcrel)
15666 {
15667 switch (fixp->fx_size)
15668 {
15669 default:
15670 as_bad_where (fixp->fx_file, fixp->fx_line,
15671 _("can not do %d byte pc-relative relocation"),
15672 fixp->fx_size);
15673 code = BFD_RELOC_32_PCREL;
15674 break;
15675 case 1: code = BFD_RELOC_8_PCREL; break;
15676 case 2: code = BFD_RELOC_16_PCREL; break;
15677 case 4: code = BFD_RELOC_32_PCREL; break;
15678 #ifdef BFD64
15679 case 8: code = BFD_RELOC_64_PCREL; break;
15680 #endif
15681 }
15682 }
15683 else
15684 {
15685 switch (fixp->fx_size)
15686 {
15687 default:
15688 as_bad_where (fixp->fx_file, fixp->fx_line,
15689 _("can not do %d byte relocation"),
15690 fixp->fx_size);
15691 code = BFD_RELOC_32;
15692 break;
15693 case 1: code = BFD_RELOC_8; break;
15694 case 2: code = BFD_RELOC_16; break;
15695 case 4: code = BFD_RELOC_32; break;
15696 #ifdef BFD64
15697 case 8: code = BFD_RELOC_64; break;
15698 #endif
15699 }
15700 }
15701 break;
15702 }
15703
15704 if ((code == BFD_RELOC_32
15705 || code == BFD_RELOC_32_PCREL
15706 || code == BFD_RELOC_X86_64_32S)
15707 && GOT_symbol
15708 && fixp->fx_addsy == GOT_symbol)
15709 {
15710 if (!object_64bit)
15711 code = BFD_RELOC_386_GOTPC;
15712 else
15713 code = BFD_RELOC_X86_64_GOTPC32;
15714 }
15715 if ((code == BFD_RELOC_64 || code == BFD_RELOC_64_PCREL)
15716 && GOT_symbol
15717 && fixp->fx_addsy == GOT_symbol)
15718 {
15719 code = BFD_RELOC_X86_64_GOTPC64;
15720 }
15721
15722 rel = XNEW (arelent);
15723 rel->sym_ptr_ptr = XNEW (asymbol *);
15724 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
15725
15726 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
15727
15728 if (!use_rela_relocations)
15729 {
15730 /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
15731 vtable entry to be used in the relocation's section offset. */
15732 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
15733 rel->address = fixp->fx_offset;
15734 #if defined (OBJ_COFF) && defined (TE_PE)
15735 else if (fixp->fx_addsy && S_IS_WEAK (fixp->fx_addsy))
15736 rel->addend = fixp->fx_addnumber - (S_GET_VALUE (fixp->fx_addsy) * 2);
15737 else
15738 #endif
15739 rel->addend = 0;
15740 }
15741 /* Use the rela in 64bit mode. */
15742 else
15743 {
15744 if (disallow_64bit_reloc)
15745 switch (code)
15746 {
15747 case BFD_RELOC_X86_64_DTPOFF64:
15748 case BFD_RELOC_X86_64_TPOFF64:
15749 case BFD_RELOC_64_PCREL:
15750 case BFD_RELOC_X86_64_GOTOFF64:
15751 case BFD_RELOC_X86_64_GOT64:
15752 case BFD_RELOC_X86_64_GOTPCREL64:
15753 case BFD_RELOC_X86_64_GOTPC64:
15754 case BFD_RELOC_X86_64_GOTPLT64:
15755 case BFD_RELOC_X86_64_PLTOFF64:
15756 as_bad_where (fixp->fx_file, fixp->fx_line,
15757 _("cannot represent relocation type %s in x32 mode"),
15758 bfd_get_reloc_code_name (code));
15759 break;
15760 default:
15761 break;
15762 }
15763
15764 if (!fixp->fx_pcrel)
15765 rel->addend = fixp->fx_offset;
15766 else
15767 switch (code)
15768 {
15769 case BFD_RELOC_X86_64_PLT32:
15770 case BFD_RELOC_X86_64_GOT32:
15771 case BFD_RELOC_X86_64_GOTPCREL:
15772 case BFD_RELOC_X86_64_GOTPCRELX:
15773 case BFD_RELOC_X86_64_REX_GOTPCRELX:
15774 case BFD_RELOC_X86_64_TLSGD:
15775 case BFD_RELOC_X86_64_TLSLD:
15776 case BFD_RELOC_X86_64_GOTTPOFF:
15777 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
15778 case BFD_RELOC_X86_64_TLSDESC_CALL:
15779 rel->addend = fixp->fx_offset - fixp->fx_size;
15780 break;
15781 default:
15782 rel->addend = (section->vma
15783 - fixp->fx_size
15784 + fixp->fx_addnumber
15785 + md_pcrel_from (fixp));
15786 break;
15787 }
15788 }
15789
15790 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
15791 if (rel->howto == NULL)
15792 {
15793 as_bad_where (fixp->fx_file, fixp->fx_line,
15794 _("cannot represent relocation type %s"),
15795 bfd_get_reloc_code_name (code));
15796 /* Set howto to a garbage value so that we can keep going. */
15797 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
15798 gas_assert (rel->howto != NULL);
15799 }
15800
15801 return rel;
15802 }
15803
15804 #include "tc-i386-intel.c"
15805
15806 void
15807 tc_x86_parse_to_dw2regnum (expressionS *exp)
15808 {
15809 int saved_naked_reg;
15810 char saved_register_dot;
15811
15812 saved_naked_reg = allow_naked_reg;
15813 allow_naked_reg = 1;
15814 saved_register_dot = register_chars['.'];
15815 register_chars['.'] = '.';
15816 allow_pseudo_reg = 1;
15817 expression_and_evaluate (exp);
15818 allow_pseudo_reg = 0;
15819 register_chars['.'] = saved_register_dot;
15820 allow_naked_reg = saved_naked_reg;
15821
15822 if (exp->X_op == O_register && exp->X_add_number >= 0)
15823 {
15824 if ((addressT) exp->X_add_number < i386_regtab_size)
15825 {
15826 exp->X_op = O_constant;
15827 exp->X_add_number = i386_regtab[exp->X_add_number]
15828 .dw2_regnum[flag_code >> 1];
15829 }
15830 else
15831 exp->X_op = O_illegal;
15832 }
15833 }
15834
15835 void
15836 tc_x86_frame_initial_instructions (void)
15837 {
15838 static unsigned int sp_regno[2];
15839
15840 if (!sp_regno[flag_code >> 1])
15841 {
15842 char *saved_input = input_line_pointer;
15843 char sp[][4] = {"esp", "rsp"};
15844 expressionS exp;
15845
15846 input_line_pointer = sp[flag_code >> 1];
15847 tc_x86_parse_to_dw2regnum (&exp);
15848 gas_assert (exp.X_op == O_constant);
15849 sp_regno[flag_code >> 1] = exp.X_add_number;
15850 input_line_pointer = saved_input;
15851 }
15852
15853 cfi_add_CFA_def_cfa (sp_regno[flag_code >> 1], -x86_cie_data_alignment);
15854 cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
15855 }
15856
15857 int
15858 x86_dwarf2_addr_size (void)
15859 {
15860 #if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
15861 if (x86_elf_abi == X86_64_X32_ABI)
15862 return 4;
15863 #endif
15864 return bfd_arch_bits_per_address (stdoutput) / 8;
15865 }
15866
15867 int
15868 i386_elf_section_type (const char *str, size_t len)
15869 {
15870 if (flag_code == CODE_64BIT
15871 && len == sizeof ("unwind") - 1
15872 && startswith (str, "unwind"))
15873 return SHT_X86_64_UNWIND;
15874
15875 return -1;
15876 }
15877
15878 #ifdef TE_SOLARIS
15879 void
15880 i386_solaris_fix_up_eh_frame (segT sec)
15881 {
15882 if (flag_code == CODE_64BIT)
15883 elf_section_type (sec) = SHT_X86_64_UNWIND;
15884 }
15885 #endif
15886
15887 #ifdef TE_PE
15888 void
15889 tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
15890 {
15891 expressionS exp;
15892
15893 exp.X_op = O_secrel;
15894 exp.X_add_symbol = symbol;
15895 exp.X_add_number = 0;
15896 emit_expr (&exp, size);
15897 }
15898 #endif
15899
15900 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15901 /* For ELF on x86-64, add support for SHF_X86_64_LARGE. */
15902
15903 bfd_vma
15904 x86_64_section_letter (int letter, const char **ptr_msg)
15905 {
15906 if (flag_code == CODE_64BIT)
15907 {
15908 if (letter == 'l')
15909 return SHF_X86_64_LARGE;
15910
15911 *ptr_msg = _("bad .section directive: want a,l,w,x,M,S,G,T in string");
15912 }
15913 else
15914 *ptr_msg = _("bad .section directive: want a,w,x,M,S,G,T in string");
15915 return -1;
15916 }
15917
15918 static void
15919 handle_large_common (int small ATTRIBUTE_UNUSED)
15920 {
15921 if (flag_code != CODE_64BIT)
15922 {
15923 s_comm_internal (0, elf_common_parse);
15924 as_warn (_(".largecomm supported only in 64bit mode, producing .comm"));
15925 }
15926 else
15927 {
15928 static segT lbss_section;
15929 asection *saved_com_section_ptr = elf_com_section_ptr;
15930 asection *saved_bss_section = bss_section;
15931
15932 if (lbss_section == NULL)
15933 {
15934 flagword applicable;
15935 segT seg = now_seg;
15936 subsegT subseg = now_subseg;
15937
15938 /* The .lbss section is for local .largecomm symbols. */
15939 lbss_section = subseg_new (".lbss", 0);
15940 applicable = bfd_applicable_section_flags (stdoutput);
15941 bfd_set_section_flags (lbss_section, applicable & SEC_ALLOC);
15942 seg_info (lbss_section)->bss = 1;
15943
15944 subseg_set (seg, subseg);
15945 }
15946
15947 elf_com_section_ptr = &_bfd_elf_large_com_section;
15948 bss_section = lbss_section;
15949
15950 s_comm_internal (0, elf_common_parse);
15951
15952 elf_com_section_ptr = saved_com_section_ptr;
15953 bss_section = saved_bss_section;
15954 }
15955 }
15956 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */