x86: fold FMA VEX and EVEX templates
[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 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 };
259
260 struct _i386_insn
261 {
262 /* TM holds the template for the insn were currently assembling. */
263 insn_template tm;
264
265 /* SUFFIX holds the instruction size suffix for byte, word, dword
266 or qword, if given. */
267 char suffix;
268
269 /* OPCODE_LENGTH holds the number of base opcode bytes. */
270 unsigned char opcode_length;
271
272 /* OPERANDS gives the number of given operands. */
273 unsigned int operands;
274
275 /* REG_OPERANDS, DISP_OPERANDS, MEM_OPERANDS, IMM_OPERANDS give the number
276 of given register, displacement, memory operands and immediate
277 operands. */
278 unsigned int reg_operands, disp_operands, mem_operands, imm_operands;
279
280 /* TYPES [i] is the type (see above #defines) which tells us how to
281 use OP[i] for the corresponding operand. */
282 i386_operand_type types[MAX_OPERANDS];
283
284 /* Displacement expression, immediate expression, or register for each
285 operand. */
286 union i386_op op[MAX_OPERANDS];
287
288 /* Flags for operands. */
289 unsigned int flags[MAX_OPERANDS];
290 #define Operand_PCrel 1
291 #define Operand_Mem 2
292 #define Operand_Signed 4 /* .insn only */
293
294 /* Relocation type for operand */
295 enum bfd_reloc_code_real reloc[MAX_OPERANDS];
296
297 /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
298 the base index byte below. */
299 const reg_entry *base_reg;
300 const reg_entry *index_reg;
301 unsigned int log2_scale_factor;
302
303 /* SEG gives the seg_entries of this insn. They are zero unless
304 explicit segment overrides are given. */
305 const reg_entry *seg[2];
306
307 /* PREFIX holds all the given prefix opcodes (usually null).
308 PREFIXES is the number of prefix opcodes. */
309 unsigned int prefixes;
310 unsigned char prefix[MAX_PREFIXES];
311
312 /* .insn allows for reserved opcode spaces. */
313 unsigned char insn_opcode_space;
314
315 /* .insn also allows (requires) specifying immediate size. */
316 unsigned char imm_bits[MAX_OPERANDS];
317
318 /* Register is in low 3 bits of opcode. */
319 bool short_form;
320
321 /* The operand to a branch insn indicates an absolute branch. */
322 bool jumpabsolute;
323
324 /* The operand to a branch insn indicates a far branch. */
325 bool far_branch;
326
327 /* There is a memory operand of (%dx) which should be only used
328 with input/output instructions. */
329 bool input_output_operand;
330
331 /* Extended states. */
332 enum
333 {
334 /* Use MMX state. */
335 xstate_mmx = 1 << 0,
336 /* Use XMM state. */
337 xstate_xmm = 1 << 1,
338 /* Use YMM state. */
339 xstate_ymm = 1 << 2 | xstate_xmm,
340 /* Use ZMM state. */
341 xstate_zmm = 1 << 3 | xstate_ymm,
342 /* Use TMM state. */
343 xstate_tmm = 1 << 4,
344 /* Use MASK state. */
345 xstate_mask = 1 << 5
346 } xstate;
347
348 /* Has GOTPC or TLS relocation. */
349 bool has_gotpc_tls_reloc;
350
351 /* RM and SIB are the modrm byte and the sib byte where the
352 addressing modes of this insn are encoded. */
353 modrm_byte rm;
354 rex_byte rex;
355 rex_byte vrex;
356 sib_byte sib;
357 vex_prefix vex;
358
359 /* Masking attributes.
360
361 The struct describes masking, applied to OPERAND in the instruction.
362 REG is a pointer to the corresponding mask register. ZEROING tells
363 whether merging or zeroing mask is used. */
364 struct Mask_Operation
365 {
366 const reg_entry *reg;
367 unsigned int zeroing;
368 /* The operand where this operation is associated. */
369 unsigned int operand;
370 } mask;
371
372 /* Rounding control and SAE attributes. */
373 struct RC_Operation
374 {
375 enum rc_type
376 {
377 rc_none = -1,
378 rne,
379 rd,
380 ru,
381 rz,
382 saeonly
383 } type;
384 /* In Intel syntax the operand modifier form is supposed to be used, but
385 we continue to accept the immediate forms as well. */
386 bool modifier;
387 } rounding;
388
389 /* Broadcasting attributes.
390
391 The struct describes broadcasting, applied to OPERAND. TYPE is
392 expresses the broadcast factor. */
393 struct Broadcast_Operation
394 {
395 /* Type of broadcast: {1to2}, {1to4}, {1to8}, {1to16} or {1to32}. */
396 unsigned int type;
397
398 /* Index of broadcasted operand. */
399 unsigned int operand;
400
401 /* Number of bytes to broadcast. */
402 unsigned int bytes;
403 } broadcast;
404
405 /* Compressed disp8*N attribute. */
406 unsigned int memshift;
407
408 /* Prefer load or store in encoding. */
409 enum
410 {
411 dir_encoding_default = 0,
412 dir_encoding_load,
413 dir_encoding_store,
414 dir_encoding_swap
415 } dir_encoding;
416
417 /* Prefer 8bit, 16bit, 32bit displacement in encoding. */
418 enum
419 {
420 disp_encoding_default = 0,
421 disp_encoding_8bit,
422 disp_encoding_16bit,
423 disp_encoding_32bit
424 } disp_encoding;
425
426 /* Prefer the REX byte in encoding. */
427 bool rex_encoding;
428
429 /* Disable instruction size optimization. */
430 bool no_optimize;
431
432 /* How to encode vector instructions. */
433 enum
434 {
435 vex_encoding_default = 0,
436 vex_encoding_vex,
437 vex_encoding_vex3,
438 vex_encoding_evex,
439 vex_encoding_evex512,
440 vex_encoding_error
441 } vec_encoding;
442
443 /* REP prefix. */
444 const char *rep_prefix;
445
446 /* HLE prefix. */
447 const char *hle_prefix;
448
449 /* Have BND prefix. */
450 const char *bnd_prefix;
451
452 /* Have NOTRACK prefix. */
453 const char *notrack_prefix;
454
455 /* Error message. */
456 enum i386_error error;
457 };
458
459 typedef struct _i386_insn i386_insn;
460
461 /* Link RC type with corresponding string, that'll be looked for in
462 asm. */
463 struct RC_name
464 {
465 enum rc_type type;
466 const char *name;
467 unsigned int len;
468 };
469
470 static const struct RC_name RC_NamesTable[] =
471 {
472 { rne, STRING_COMMA_LEN ("rn-sae") },
473 { rd, STRING_COMMA_LEN ("rd-sae") },
474 { ru, STRING_COMMA_LEN ("ru-sae") },
475 { rz, STRING_COMMA_LEN ("rz-sae") },
476 { saeonly, STRING_COMMA_LEN ("sae") },
477 };
478
479 /* To be indexed by segment register number. */
480 static const unsigned char i386_seg_prefixes[] = {
481 ES_PREFIX_OPCODE,
482 CS_PREFIX_OPCODE,
483 SS_PREFIX_OPCODE,
484 DS_PREFIX_OPCODE,
485 FS_PREFIX_OPCODE,
486 GS_PREFIX_OPCODE
487 };
488
489 /* List of chars besides those in app.c:symbol_chars that can start an
490 operand. Used to prevent the scrubber eating vital white-space. */
491 const char extra_symbol_chars[] = "*%-([{}"
492 #ifdef LEX_AT
493 "@"
494 #endif
495 #ifdef LEX_QM
496 "?"
497 #endif
498 ;
499
500 #if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
501 && !defined (TE_GNU) \
502 && !defined (TE_LINUX) \
503 && !defined (TE_Haiku) \
504 && !defined (TE_FreeBSD) \
505 && !defined (TE_DragonFly) \
506 && !defined (TE_NetBSD))
507 /* This array holds the chars that always start a comment. If the
508 pre-processor is disabled, these aren't very useful. The option
509 --divide will remove '/' from this list. */
510 const char *i386_comment_chars = "#/";
511 #define SVR4_COMMENT_CHARS 1
512 #define PREFIX_SEPARATOR '\\'
513
514 #else
515 const char *i386_comment_chars = "#";
516 #define PREFIX_SEPARATOR '/'
517 #endif
518
519 /* This array holds the chars that only start a comment at the beginning of
520 a line. If the line seems to have the form '# 123 filename'
521 .line and .file directives will appear in the pre-processed output.
522 Note that input_file.c hand checks for '#' at the beginning of the
523 first line of the input file. This is because the compiler outputs
524 #NO_APP at the beginning of its output.
525 Also note that comments started like this one will always work if
526 '/' isn't otherwise defined. */
527 const char line_comment_chars[] = "#/";
528
529 const char line_separator_chars[] = ";";
530
531 /* Chars that can be used to separate mant from exp in floating point
532 nums. */
533 const char EXP_CHARS[] = "eE";
534
535 /* Chars that mean this number is a floating point constant
536 As in 0f12.456
537 or 0d1.2345e12. */
538 const char FLT_CHARS[] = "fFdDxXhHbB";
539
540 /* Tables for lexical analysis. */
541 static char mnemonic_chars[256];
542 static char register_chars[256];
543 static char operand_chars[256];
544
545 /* Lexical macros. */
546 #define is_operand_char(x) (operand_chars[(unsigned char) x])
547 #define is_register_char(x) (register_chars[(unsigned char) x])
548 #define is_space_char(x) ((x) == ' ')
549
550 /* All non-digit non-letter characters that may occur in an operand and
551 which aren't already in extra_symbol_chars[]. */
552 static const char operand_special_chars[] = "$+,)._~/<>|&^!=:@]";
553
554 /* md_assemble() always leaves the strings it's passed unaltered. To
555 effect this we maintain a stack of saved characters that we've smashed
556 with '\0's (indicating end of strings for various sub-fields of the
557 assembler instruction). */
558 static char save_stack[32];
559 static char *save_stack_p;
560 #define END_STRING_AND_SAVE(s) \
561 do { *save_stack_p++ = *(s); *(s) = '\0'; } while (0)
562 #define RESTORE_END_STRING(s) \
563 do { *(s) = *--save_stack_p; } while (0)
564
565 /* The instruction we're assembling. */
566 static i386_insn i;
567
568 /* Possible templates for current insn. */
569 static const templates *current_templates;
570
571 /* Per instruction expressionS buffers: max displacements & immediates. */
572 static expressionS disp_expressions[MAX_MEMORY_OPERANDS];
573 static expressionS im_expressions[MAX_IMMEDIATE_OPERANDS];
574
575 /* Current operand we are working on. */
576 static int this_operand = -1;
577
578 /* Are we processing a .insn directive? */
579 #define dot_insn() (i.tm.mnem_off == MN__insn)
580
581 /* We support four different modes. FLAG_CODE variable is used to distinguish
582 these. */
583
584 enum flag_code {
585 CODE_32BIT,
586 CODE_16BIT,
587 CODE_64BIT };
588
589 static enum flag_code flag_code;
590 static unsigned int object_64bit;
591 static unsigned int disallow_64bit_reloc;
592 static int use_rela_relocations = 0;
593 /* __tls_get_addr/___tls_get_addr symbol for TLS. */
594 static const char *tls_get_addr;
595
596 #if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
597 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
598 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
599
600 /* The ELF ABI to use. */
601 enum x86_elf_abi
602 {
603 I386_ABI,
604 X86_64_ABI,
605 X86_64_X32_ABI
606 };
607
608 static enum x86_elf_abi x86_elf_abi = I386_ABI;
609 #endif
610
611 #if defined (TE_PE) || defined (TE_PEP)
612 /* Use big object file format. */
613 static int use_big_obj = 0;
614 #endif
615
616 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
617 /* 1 if generating code for a shared library. */
618 static int shared = 0;
619
620 unsigned int x86_sframe_cfa_sp_reg;
621 /* The other CFA base register for SFrame stack trace info. */
622 unsigned int x86_sframe_cfa_fp_reg;
623 unsigned int x86_sframe_cfa_ra_reg;
624
625 #endif
626
627 /* 1 for intel syntax,
628 0 if att syntax. */
629 static int intel_syntax = 0;
630
631 static enum x86_64_isa
632 {
633 amd64 = 1, /* AMD64 ISA. */
634 intel64 /* Intel64 ISA. */
635 } isa64;
636
637 /* 1 for intel mnemonic,
638 0 if att mnemonic. */
639 static int intel_mnemonic = !SYSV386_COMPAT;
640
641 /* 1 if pseudo registers are permitted. */
642 static int allow_pseudo_reg = 0;
643
644 /* 1 if register prefix % not required. */
645 static int allow_naked_reg = 0;
646
647 /* 1 if the assembler should add BND prefix for all control-transferring
648 instructions supporting it, even if this prefix wasn't specified
649 explicitly. */
650 static int add_bnd_prefix = 0;
651
652 /* 1 if pseudo index register, eiz/riz, is allowed . */
653 static int allow_index_reg = 0;
654
655 /* 1 if the assembler should ignore LOCK prefix, even if it was
656 specified explicitly. */
657 static int omit_lock_prefix = 0;
658
659 /* 1 if the assembler should encode lfence, mfence, and sfence as
660 "lock addl $0, (%{re}sp)". */
661 static int avoid_fence = 0;
662
663 /* 1 if lfence should be inserted after every load. */
664 static int lfence_after_load = 0;
665
666 /* Non-zero if lfence should be inserted before indirect branch. */
667 static enum lfence_before_indirect_branch_kind
668 {
669 lfence_branch_none = 0,
670 lfence_branch_register,
671 lfence_branch_memory,
672 lfence_branch_all
673 }
674 lfence_before_indirect_branch;
675
676 /* Non-zero if lfence should be inserted before ret. */
677 static enum lfence_before_ret_kind
678 {
679 lfence_before_ret_none = 0,
680 lfence_before_ret_not,
681 lfence_before_ret_or,
682 lfence_before_ret_shl
683 }
684 lfence_before_ret;
685
686 /* Types of previous instruction is .byte or prefix. */
687 static struct
688 {
689 segT seg;
690 const char *file;
691 const char *name;
692 unsigned int line;
693 enum last_insn_kind
694 {
695 last_insn_other = 0,
696 last_insn_directive,
697 last_insn_prefix
698 } kind;
699 } last_insn;
700
701 /* 1 if the assembler should generate relax relocations. */
702
703 static int generate_relax_relocations
704 = DEFAULT_GENERATE_X86_RELAX_RELOCATIONS;
705
706 static enum check_kind
707 {
708 check_none = 0,
709 check_warning,
710 check_error
711 }
712 sse_check, operand_check = check_warning;
713
714 /* Non-zero if branches should be aligned within power of 2 boundary. */
715 static int align_branch_power = 0;
716
717 /* Types of branches to align. */
718 enum align_branch_kind
719 {
720 align_branch_none = 0,
721 align_branch_jcc = 1,
722 align_branch_fused = 2,
723 align_branch_jmp = 3,
724 align_branch_call = 4,
725 align_branch_indirect = 5,
726 align_branch_ret = 6
727 };
728
729 /* Type bits of branches to align. */
730 enum align_branch_bit
731 {
732 align_branch_jcc_bit = 1 << align_branch_jcc,
733 align_branch_fused_bit = 1 << align_branch_fused,
734 align_branch_jmp_bit = 1 << align_branch_jmp,
735 align_branch_call_bit = 1 << align_branch_call,
736 align_branch_indirect_bit = 1 << align_branch_indirect,
737 align_branch_ret_bit = 1 << align_branch_ret
738 };
739
740 static unsigned int align_branch = (align_branch_jcc_bit
741 | align_branch_fused_bit
742 | align_branch_jmp_bit);
743
744 /* Types of condition jump used by macro-fusion. */
745 enum mf_jcc_kind
746 {
747 mf_jcc_jo = 0, /* base opcode 0x70 */
748 mf_jcc_jc, /* base opcode 0x72 */
749 mf_jcc_je, /* base opcode 0x74 */
750 mf_jcc_jna, /* base opcode 0x76 */
751 mf_jcc_js, /* base opcode 0x78 */
752 mf_jcc_jp, /* base opcode 0x7a */
753 mf_jcc_jl, /* base opcode 0x7c */
754 mf_jcc_jle, /* base opcode 0x7e */
755 };
756
757 /* Types of compare flag-modifying insntructions used by macro-fusion. */
758 enum mf_cmp_kind
759 {
760 mf_cmp_test_and, /* test/cmp */
761 mf_cmp_alu_cmp, /* add/sub/cmp */
762 mf_cmp_incdec /* inc/dec */
763 };
764
765 /* The maximum padding size for fused jcc. CMP like instruction can
766 be 9 bytes and jcc can be 6 bytes. Leave room just in case for
767 prefixes. */
768 #define MAX_FUSED_JCC_PADDING_SIZE 20
769
770 /* The maximum number of prefixes added for an instruction. */
771 static unsigned int align_branch_prefix_size = 5;
772
773 /* Optimization:
774 1. Clear the REX_W bit with register operand if possible.
775 2. Above plus use 128bit vector instruction to clear the full vector
776 register.
777 */
778 static int optimize = 0;
779
780 /* Optimization:
781 1. Clear the REX_W bit with register operand if possible.
782 2. Above plus use 128bit vector instruction to clear the full vector
783 register.
784 3. Above plus optimize "test{q,l,w} $imm8,%r{64,32,16}" to
785 "testb $imm7,%r8".
786 */
787 static int optimize_for_space = 0;
788
789 /* Register prefix used for error message. */
790 static const char *register_prefix = "%";
791
792 /* Used in 16 bit gcc mode to add an l suffix to call, ret, enter,
793 leave, push, and pop instructions so that gcc has the same stack
794 frame as in 32 bit mode. */
795 static char stackop_size = '\0';
796
797 /* Non-zero to optimize code alignment. */
798 int optimize_align_code = 1;
799
800 /* Non-zero to quieten some warnings. */
801 static int quiet_warnings = 0;
802
803 /* Guard to avoid repeated warnings about non-16-bit code on 16-bit CPUs. */
804 static bool pre_386_16bit_warned;
805
806 /* CPU name. */
807 static const char *cpu_arch_name = NULL;
808 static char *cpu_sub_arch_name = NULL;
809
810 /* CPU feature flags. */
811 static i386_cpu_flags cpu_arch_flags = CPU_UNKNOWN_FLAGS;
812
813 /* If we have selected a cpu we are generating instructions for. */
814 static int cpu_arch_tune_set = 0;
815
816 /* Cpu we are generating instructions for. */
817 enum processor_type cpu_arch_tune = PROCESSOR_UNKNOWN;
818
819 /* CPU feature flags of cpu we are generating instructions for. */
820 static i386_cpu_flags cpu_arch_tune_flags;
821
822 /* CPU instruction set architecture used. */
823 enum processor_type cpu_arch_isa = PROCESSOR_UNKNOWN;
824
825 /* CPU feature flags of instruction set architecture used. */
826 i386_cpu_flags cpu_arch_isa_flags;
827
828 /* If set, conditional jumps are not automatically promoted to handle
829 larger than a byte offset. */
830 static bool no_cond_jump_promotion = false;
831
832 /* This will be set from an expression parser hook if there's any
833 applicable operator involved in an expression. */
834 static enum {
835 expr_operator_none,
836 expr_operator_present,
837 expr_large_value,
838 } expr_mode;
839
840 /* Encode SSE instructions with VEX prefix. */
841 static unsigned int sse2avx;
842
843 /* Encode aligned vector move as unaligned vector move. */
844 static unsigned int use_unaligned_vector_move;
845
846 /* Maximum permitted vector size. */
847 #define VSZ_DEFAULT VSZ512
848 static unsigned int vector_size = VSZ_DEFAULT;
849
850 /* Encode scalar AVX instructions with specific vector length. */
851 static enum
852 {
853 vex128 = 0,
854 vex256
855 } avxscalar;
856
857 /* Encode VEX WIG instructions with specific vex.w. */
858 static enum
859 {
860 vexw0 = 0,
861 vexw1
862 } vexwig;
863
864 /* Encode scalar EVEX LIG instructions with specific vector length. */
865 static enum
866 {
867 evexl128 = 0,
868 evexl256,
869 evexl512
870 } evexlig;
871
872 /* Encode EVEX WIG instructions with specific evex.w. */
873 static enum
874 {
875 evexw0 = 0,
876 evexw1
877 } evexwig;
878
879 /* Value to encode in EVEX RC bits, for SAE-only instructions. */
880 static enum rc_type evexrcig = rne;
881
882 /* Pre-defined "_GLOBAL_OFFSET_TABLE_". */
883 static symbolS *GOT_symbol;
884
885 /* The dwarf2 return column, adjusted for 32 or 64 bit. */
886 unsigned int x86_dwarf2_return_column;
887
888 /* The dwarf2 data alignment, adjusted for 32 or 64 bit. */
889 int x86_cie_data_alignment;
890
891 /* Interface to relax_segment.
892 There are 3 major relax states for 386 jump insns because the
893 different types of jumps add different sizes to frags when we're
894 figuring out what sort of jump to choose to reach a given label.
895
896 BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING are used to align
897 branches which are handled by md_estimate_size_before_relax() and
898 i386_generic_table_relax_frag(). */
899
900 /* Types. */
901 #define UNCOND_JUMP 0
902 #define COND_JUMP 1
903 #define COND_JUMP86 2
904 #define BRANCH_PADDING 3
905 #define BRANCH_PREFIX 4
906 #define FUSED_JCC_PADDING 5
907
908 /* Sizes. */
909 #define CODE16 1
910 #define SMALL 0
911 #define SMALL16 (SMALL | CODE16)
912 #define BIG 2
913 #define BIG16 (BIG | CODE16)
914
915 #ifndef INLINE
916 #ifdef __GNUC__
917 #define INLINE __inline__
918 #else
919 #define INLINE
920 #endif
921 #endif
922
923 #define ENCODE_RELAX_STATE(type, size) \
924 ((relax_substateT) (((type) << 2) | (size)))
925 #define TYPE_FROM_RELAX_STATE(s) \
926 ((s) >> 2)
927 #define DISP_SIZE_FROM_RELAX_STATE(s) \
928 ((((s) & 3) == BIG ? 4 : (((s) & 3) == BIG16 ? 2 : 1)))
929
930 /* This table is used by relax_frag to promote short jumps to long
931 ones where necessary. SMALL (short) jumps may be promoted to BIG
932 (32 bit long) ones, and SMALL16 jumps to BIG16 (16 bit long). We
933 don't allow a short jump in a 32 bit code segment to be promoted to
934 a 16 bit offset jump because it's slower (requires data size
935 prefix), and doesn't work, unless the destination is in the bottom
936 64k of the code segment (The top 16 bits of eip are zeroed). */
937
938 const relax_typeS md_relax_table[] =
939 {
940 /* The fields are:
941 1) most positive reach of this state,
942 2) most negative reach of this state,
943 3) how many bytes this mode will have in the variable part of the frag
944 4) which index into the table to try if we can't fit into this one. */
945
946 /* UNCOND_JUMP states. */
947 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG)},
948 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16)},
949 /* dword jmp adds 4 bytes to frag:
950 0 extra opcode bytes, 4 displacement bytes. */
951 {0, 0, 4, 0},
952 /* word jmp adds 2 byte2 to frag:
953 0 extra opcode bytes, 2 displacement bytes. */
954 {0, 0, 2, 0},
955
956 /* COND_JUMP states. */
957 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG)},
958 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG16)},
959 /* dword conditionals adds 5 bytes to frag:
960 1 extra opcode byte, 4 displacement bytes. */
961 {0, 0, 5, 0},
962 /* word conditionals add 3 bytes to frag:
963 1 extra opcode byte, 2 displacement bytes. */
964 {0, 0, 3, 0},
965
966 /* COND_JUMP86 states. */
967 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG)},
968 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG16)},
969 /* dword conditionals adds 5 bytes to frag:
970 1 extra opcode byte, 4 displacement bytes. */
971 {0, 0, 5, 0},
972 /* word conditionals add 4 bytes to frag:
973 1 displacement byte and a 3 byte long branch insn. */
974 {0, 0, 4, 0}
975 };
976
977 #define ARCH(n, t, f, s) \
978 { STRING_COMMA_LEN (#n), s, PROCESSOR_ ## t, vsz_none, CPU_ ## f ## _FLAGS, \
979 CPU_NONE_FLAGS }
980 #define SUBARCH(n, e, d, s) \
981 { STRING_COMMA_LEN (#n), s, PROCESSOR_NONE, vsz_none, CPU_ ## e ## _FLAGS, \
982 CPU_ ## d ## _FLAGS }
983 #define VECARCH(n, e, d, v) \
984 { STRING_COMMA_LEN (#n), false, PROCESSOR_NONE, vsz_ ## v, \
985 CPU_ ## e ## _FLAGS, CPU_ ## d ## _FLAGS }
986
987 static const arch_entry cpu_arch[] =
988 {
989 /* Do not replace the first two entries - i386_target_format() and
990 set_cpu_arch() rely on them being there in this order. */
991 ARCH (generic32, GENERIC32, GENERIC32, false),
992 ARCH (generic64, GENERIC64, GENERIC64, false),
993 ARCH (i8086, UNKNOWN, NONE, false),
994 ARCH (i186, UNKNOWN, 186, false),
995 ARCH (i286, UNKNOWN, 286, false),
996 ARCH (i386, I386, 386, false),
997 ARCH (i486, I486, 486, false),
998 ARCH (i586, PENTIUM, 586, false),
999 ARCH (i686, PENTIUMPRO, 686, false),
1000 ARCH (pentium, PENTIUM, 586, false),
1001 ARCH (pentiumpro, PENTIUMPRO, PENTIUMPRO, false),
1002 ARCH (pentiumii, PENTIUMPRO, P2, false),
1003 ARCH (pentiumiii, PENTIUMPRO, P3, false),
1004 ARCH (pentium4, PENTIUM4, P4, false),
1005 ARCH (prescott, NOCONA, CORE, false),
1006 ARCH (nocona, NOCONA, NOCONA, false),
1007 ARCH (yonah, CORE, CORE, true),
1008 ARCH (core, CORE, CORE, false),
1009 ARCH (merom, CORE2, CORE2, true),
1010 ARCH (core2, CORE2, CORE2, false),
1011 ARCH (corei7, COREI7, COREI7, false),
1012 ARCH (iamcu, IAMCU, IAMCU, false),
1013 ARCH (k6, K6, K6, false),
1014 ARCH (k6_2, K6, K6_2, false),
1015 ARCH (athlon, ATHLON, ATHLON, false),
1016 ARCH (sledgehammer, K8, K8, true),
1017 ARCH (opteron, K8, K8, false),
1018 ARCH (k8, K8, K8, false),
1019 ARCH (amdfam10, AMDFAM10, AMDFAM10, false),
1020 ARCH (bdver1, BD, BDVER1, false),
1021 ARCH (bdver2, BD, BDVER2, false),
1022 ARCH (bdver3, BD, BDVER3, false),
1023 ARCH (bdver4, BD, BDVER4, false),
1024 ARCH (znver1, ZNVER, ZNVER1, false),
1025 ARCH (znver2, ZNVER, ZNVER2, false),
1026 ARCH (znver3, ZNVER, ZNVER3, false),
1027 ARCH (znver4, ZNVER, ZNVER4, false),
1028 ARCH (btver1, BT, BTVER1, false),
1029 ARCH (btver2, BT, BTVER2, false),
1030
1031 SUBARCH (8087, 8087, ANY_8087, false),
1032 SUBARCH (87, NONE, ANY_8087, false), /* Disable only! */
1033 SUBARCH (287, 287, ANY_287, false),
1034 SUBARCH (387, 387, ANY_387, false),
1035 SUBARCH (687, 687, ANY_687, false),
1036 SUBARCH (cmov, CMOV, CMOV, false),
1037 SUBARCH (fxsr, FXSR, ANY_FXSR, false),
1038 SUBARCH (mmx, MMX, ANY_MMX, false),
1039 SUBARCH (sse, SSE, ANY_SSE, false),
1040 SUBARCH (sse2, SSE2, ANY_SSE2, false),
1041 SUBARCH (sse3, SSE3, ANY_SSE3, false),
1042 SUBARCH (sse4a, SSE4A, ANY_SSE4A, false),
1043 SUBARCH (ssse3, SSSE3, ANY_SSSE3, false),
1044 SUBARCH (sse4.1, SSE4_1, ANY_SSE4_1, false),
1045 SUBARCH (sse4.2, SSE4_2, ANY_SSE4_2, false),
1046 SUBARCH (sse4, SSE4_2, ANY_SSE4_1, false),
1047 VECARCH (avx, AVX, ANY_AVX, reset),
1048 VECARCH (avx2, AVX2, ANY_AVX2, reset),
1049 VECARCH (avx512f, AVX512F, ANY_AVX512F, reset),
1050 VECARCH (avx512cd, AVX512CD, ANY_AVX512CD, reset),
1051 VECARCH (avx512er, AVX512ER, ANY_AVX512ER, reset),
1052 VECARCH (avx512pf, AVX512PF, ANY_AVX512PF, reset),
1053 VECARCH (avx512dq, AVX512DQ, ANY_AVX512DQ, reset),
1054 VECARCH (avx512bw, AVX512BW, ANY_AVX512BW, reset),
1055 VECARCH (avx512vl, AVX512VL, ANY_AVX512VL, reset),
1056 SUBARCH (monitor, MONITOR, MONITOR, false),
1057 SUBARCH (vmx, VMX, ANY_VMX, false),
1058 SUBARCH (vmfunc, VMFUNC, ANY_VMFUNC, false),
1059 SUBARCH (smx, SMX, SMX, false),
1060 SUBARCH (xsave, XSAVE, ANY_XSAVE, false),
1061 SUBARCH (xsaveopt, XSAVEOPT, ANY_XSAVEOPT, false),
1062 SUBARCH (xsavec, XSAVEC, ANY_XSAVEC, false),
1063 SUBARCH (xsaves, XSAVES, ANY_XSAVES, false),
1064 SUBARCH (aes, AES, ANY_AES, false),
1065 SUBARCH (pclmul, PCLMULQDQ, ANY_PCLMULQDQ, false),
1066 SUBARCH (clmul, PCLMULQDQ, ANY_PCLMULQDQ, true),
1067 SUBARCH (fsgsbase, FSGSBASE, FSGSBASE, false),
1068 SUBARCH (rdrnd, RDRND, RDRND, false),
1069 SUBARCH (f16c, F16C, ANY_F16C, false),
1070 SUBARCH (bmi2, BMI2, BMI2, false),
1071 SUBARCH (fma, FMA, ANY_FMA, false),
1072 SUBARCH (fma4, FMA4, ANY_FMA4, false),
1073 SUBARCH (xop, XOP, ANY_XOP, false),
1074 SUBARCH (lwp, LWP, ANY_LWP, false),
1075 SUBARCH (movbe, MOVBE, MOVBE, false),
1076 SUBARCH (cx16, CX16, CX16, false),
1077 SUBARCH (lahf_sahf, LAHF_SAHF, LAHF_SAHF, false),
1078 SUBARCH (ept, EPT, ANY_EPT, false),
1079 SUBARCH (lzcnt, LZCNT, LZCNT, false),
1080 SUBARCH (popcnt, POPCNT, POPCNT, false),
1081 SUBARCH (hle, HLE, HLE, false),
1082 SUBARCH (rtm, RTM, ANY_RTM, false),
1083 SUBARCH (tsx, TSX, TSX, false),
1084 SUBARCH (invpcid, INVPCID, INVPCID, false),
1085 SUBARCH (clflush, CLFLUSH, CLFLUSH, false),
1086 SUBARCH (nop, NOP, NOP, false),
1087 SUBARCH (syscall, SYSCALL, SYSCALL, false),
1088 SUBARCH (rdtscp, RDTSCP, RDTSCP, false),
1089 SUBARCH (3dnow, 3DNOW, ANY_3DNOW, false),
1090 SUBARCH (3dnowa, 3DNOWA, ANY_3DNOWA, false),
1091 SUBARCH (padlock, PADLOCK, PADLOCK, false),
1092 SUBARCH (pacifica, SVME, ANY_SVME, true),
1093 SUBARCH (svme, SVME, ANY_SVME, false),
1094 SUBARCH (abm, ABM, ABM, false),
1095 SUBARCH (bmi, BMI, BMI, false),
1096 SUBARCH (tbm, TBM, TBM, false),
1097 SUBARCH (adx, ADX, ADX, false),
1098 SUBARCH (rdseed, RDSEED, RDSEED, false),
1099 SUBARCH (prfchw, PRFCHW, PRFCHW, false),
1100 SUBARCH (smap, SMAP, SMAP, false),
1101 SUBARCH (mpx, MPX, ANY_MPX, false),
1102 SUBARCH (sha, SHA, ANY_SHA, false),
1103 SUBARCH (clflushopt, CLFLUSHOPT, CLFLUSHOPT, false),
1104 SUBARCH (prefetchwt1, PREFETCHWT1, PREFETCHWT1, false),
1105 SUBARCH (se1, SE1, SE1, false),
1106 SUBARCH (clwb, CLWB, CLWB, false),
1107 VECARCH (avx512ifma, AVX512IFMA, ANY_AVX512IFMA, reset),
1108 VECARCH (avx512vbmi, AVX512VBMI, ANY_AVX512VBMI, reset),
1109 VECARCH (avx512_4fmaps, AVX512_4FMAPS, ANY_AVX512_4FMAPS, reset),
1110 VECARCH (avx512_4vnniw, AVX512_4VNNIW, ANY_AVX512_4VNNIW, reset),
1111 VECARCH (avx512_vpopcntdq, AVX512_VPOPCNTDQ, ANY_AVX512_VPOPCNTDQ, reset),
1112 VECARCH (avx512_vbmi2, AVX512_VBMI2, ANY_AVX512_VBMI2, reset),
1113 VECARCH (avx512_vnni, AVX512_VNNI, ANY_AVX512_VNNI, reset),
1114 VECARCH (avx512_bitalg, AVX512_BITALG, ANY_AVX512_BITALG, reset),
1115 VECARCH (avx_vnni, AVX_VNNI, ANY_AVX_VNNI, reset),
1116 SUBARCH (clzero, CLZERO, CLZERO, false),
1117 SUBARCH (mwaitx, MWAITX, MWAITX, false),
1118 SUBARCH (ospke, OSPKE, ANY_OSPKE, false),
1119 SUBARCH (rdpid, RDPID, RDPID, false),
1120 SUBARCH (ptwrite, PTWRITE, PTWRITE, false),
1121 SUBARCH (ibt, IBT, IBT, false),
1122 SUBARCH (shstk, SHSTK, SHSTK, false),
1123 SUBARCH (gfni, GFNI, ANY_GFNI, false),
1124 VECARCH (vaes, VAES, ANY_VAES, reset),
1125 VECARCH (vpclmulqdq, VPCLMULQDQ, ANY_VPCLMULQDQ, reset),
1126 SUBARCH (wbnoinvd, WBNOINVD, WBNOINVD, false),
1127 SUBARCH (pconfig, PCONFIG, PCONFIG, false),
1128 SUBARCH (waitpkg, WAITPKG, WAITPKG, false),
1129 SUBARCH (cldemote, CLDEMOTE, CLDEMOTE, false),
1130 SUBARCH (amx_int8, AMX_INT8, ANY_AMX_INT8, false),
1131 SUBARCH (amx_bf16, AMX_BF16, ANY_AMX_BF16, false),
1132 SUBARCH (amx_fp16, AMX_FP16, ANY_AMX_FP16, false),
1133 SUBARCH (amx_complex, AMX_COMPLEX, ANY_AMX_COMPLEX, false),
1134 SUBARCH (amx_tile, AMX_TILE, ANY_AMX_TILE, false),
1135 SUBARCH (movdiri, MOVDIRI, MOVDIRI, false),
1136 SUBARCH (movdir64b, MOVDIR64B, MOVDIR64B, false),
1137 VECARCH (avx512_bf16, AVX512_BF16, ANY_AVX512_BF16, reset),
1138 VECARCH (avx512_vp2intersect, AVX512_VP2INTERSECT,
1139 ANY_AVX512_VP2INTERSECT, reset),
1140 SUBARCH (tdx, TDX, TDX, false),
1141 SUBARCH (enqcmd, ENQCMD, ENQCMD, false),
1142 SUBARCH (serialize, SERIALIZE, SERIALIZE, false),
1143 SUBARCH (rdpru, RDPRU, RDPRU, false),
1144 SUBARCH (mcommit, MCOMMIT, MCOMMIT, false),
1145 SUBARCH (sev_es, SEV_ES, ANY_SEV_ES, false),
1146 SUBARCH (tsxldtrk, TSXLDTRK, ANY_TSXLDTRK, false),
1147 SUBARCH (kl, KL, ANY_KL, false),
1148 SUBARCH (widekl, WIDEKL, ANY_WIDEKL, false),
1149 SUBARCH (uintr, UINTR, UINTR, false),
1150 SUBARCH (hreset, HRESET, HRESET, false),
1151 VECARCH (avx512_fp16, AVX512_FP16, ANY_AVX512_FP16, reset),
1152 SUBARCH (prefetchi, PREFETCHI, PREFETCHI, false),
1153 VECARCH (avx_ifma, AVX_IFMA, ANY_AVX_IFMA, reset),
1154 VECARCH (avx_vnni_int8, AVX_VNNI_INT8, ANY_AVX_VNNI_INT8, reset),
1155 SUBARCH (cmpccxadd, CMPCCXADD, CMPCCXADD, false),
1156 SUBARCH (wrmsrns, WRMSRNS, WRMSRNS, false),
1157 SUBARCH (msrlist, MSRLIST, MSRLIST, false),
1158 VECARCH (avx_ne_convert, AVX_NE_CONVERT, ANY_AVX_NE_CONVERT, reset),
1159 SUBARCH (rao_int, RAO_INT, RAO_INT, false),
1160 SUBARCH (rmpquery, RMPQUERY, ANY_RMPQUERY, false),
1161 SUBARCH (fred, FRED, ANY_FRED, false),
1162 SUBARCH (lkgs, LKGS, ANY_LKGS, false),
1163 VECARCH (avx_vnni_int16, AVX_VNNI_INT16, ANY_AVX_VNNI_INT16, reset),
1164 VECARCH (sha512, SHA512, ANY_SHA512, reset),
1165 VECARCH (sm3, SM3, ANY_SM3, reset),
1166 VECARCH (sm4, SM4, ANY_SM4, reset),
1167 SUBARCH (pbndkb, PBNDKB, PBNDKB, false),
1168 VECARCH (avx10.1, AVX10_1, ANY_AVX512F, set),
1169 };
1170
1171 #undef SUBARCH
1172 #undef ARCH
1173
1174 #ifdef I386COFF
1175 /* Like s_lcomm_internal in gas/read.c but the alignment string
1176 is allowed to be optional. */
1177
1178 static symbolS *
1179 pe_lcomm_internal (int needs_align, symbolS *symbolP, addressT size)
1180 {
1181 addressT align = 0;
1182
1183 SKIP_WHITESPACE ();
1184
1185 if (needs_align
1186 && *input_line_pointer == ',')
1187 {
1188 align = parse_align (needs_align - 1);
1189
1190 if (align == (addressT) -1)
1191 return NULL;
1192 }
1193 else
1194 {
1195 if (size >= 8)
1196 align = 3;
1197 else if (size >= 4)
1198 align = 2;
1199 else if (size >= 2)
1200 align = 1;
1201 else
1202 align = 0;
1203 }
1204
1205 bss_alloc (symbolP, size, align);
1206 return symbolP;
1207 }
1208
1209 static void
1210 pe_lcomm (int needs_align)
1211 {
1212 s_comm_internal (needs_align * 2, pe_lcomm_internal);
1213 }
1214 #endif
1215
1216 const pseudo_typeS md_pseudo_table[] =
1217 {
1218 #if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
1219 {"align", s_align_bytes, 0},
1220 #else
1221 {"align", s_align_ptwo, 0},
1222 #endif
1223 {"arch", set_cpu_arch, 0},
1224 #ifndef I386COFF
1225 {"bss", s_bss, 0},
1226 #else
1227 {"lcomm", pe_lcomm, 1},
1228 #endif
1229 {"ffloat", float_cons, 'f'},
1230 {"dfloat", float_cons, 'd'},
1231 {"tfloat", float_cons, 'x'},
1232 {"hfloat", float_cons, 'h'},
1233 {"bfloat16", float_cons, 'b'},
1234 {"value", cons, 2},
1235 {"slong", signed_cons, 4},
1236 {"insn", s_insn, 0},
1237 {"noopt", s_ignore, 0},
1238 {"optim", s_ignore, 0},
1239 {"code16gcc", set_16bit_gcc_code_flag, CODE_16BIT},
1240 {"code16", set_code_flag, CODE_16BIT},
1241 {"code32", set_code_flag, CODE_32BIT},
1242 #ifdef BFD64
1243 {"code64", set_code_flag, CODE_64BIT},
1244 #endif
1245 {"intel_syntax", set_intel_syntax, 1},
1246 {"att_syntax", set_intel_syntax, 0},
1247 {"intel_mnemonic", set_intel_mnemonic, 1},
1248 {"att_mnemonic", set_intel_mnemonic, 0},
1249 {"allow_index_reg", set_allow_index_reg, 1},
1250 {"disallow_index_reg", set_allow_index_reg, 0},
1251 {"sse_check", set_check, 0},
1252 {"operand_check", set_check, 1},
1253 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1254 {"largecomm", handle_large_common, 0},
1255 #else
1256 {"file", dwarf2_directive_file, 0},
1257 {"loc", dwarf2_directive_loc, 0},
1258 {"loc_mark_labels", dwarf2_directive_loc_mark_labels, 0},
1259 #endif
1260 #ifdef TE_PE
1261 {"secrel32", pe_directive_secrel, 0},
1262 {"secidx", pe_directive_secidx, 0},
1263 #endif
1264 {0, 0, 0}
1265 };
1266
1267 /* For interface with expression (). */
1268 extern char *input_line_pointer;
1269
1270 /* Hash table for instruction mnemonic lookup. */
1271 static htab_t op_hash;
1272
1273 /* Hash table for register lookup. */
1274 static htab_t reg_hash;
1275 \f
1276 /* Various efficient no-op patterns for aligning code labels.
1277 Note: Don't try to assemble the instructions in the comments.
1278 0L and 0w are not legal. */
1279 static const unsigned char f32_1[] =
1280 {0x90}; /* nop */
1281 static const unsigned char f32_2[] =
1282 {0x66,0x90}; /* xchg %ax,%ax */
1283 static const unsigned char f32_3[] =
1284 {0x8d,0x76,0x00}; /* leal 0(%esi),%esi */
1285 static const unsigned char f32_4[] =
1286 {0x8d,0x74,0x26,0x00}; /* leal 0(%esi,1),%esi */
1287 static const unsigned char f32_6[] =
1288 {0x8d,0xb6,0x00,0x00,0x00,0x00}; /* leal 0L(%esi),%esi */
1289 static const unsigned char f32_7[] =
1290 {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal 0L(%esi,1),%esi */
1291 static const unsigned char f16_3[] =
1292 {0x8d,0x74,0x00}; /* lea 0(%si),%si */
1293 static const unsigned char f16_4[] =
1294 {0x8d,0xb4,0x00,0x00}; /* lea 0W(%si),%si */
1295 static const unsigned char jump_disp8[] =
1296 {0xeb}; /* jmp disp8 */
1297 static const unsigned char jump32_disp32[] =
1298 {0xe9}; /* jmp disp32 */
1299 static const unsigned char jump16_disp32[] =
1300 {0x66,0xe9}; /* jmp disp32 */
1301 /* 32-bit NOPs patterns. */
1302 static const unsigned char *const f32_patt[] = {
1303 f32_1, f32_2, f32_3, f32_4, NULL, f32_6, f32_7
1304 };
1305 /* 16-bit NOPs patterns. */
1306 static const unsigned char *const f16_patt[] = {
1307 f32_1, f32_2, f16_3, f16_4
1308 };
1309 /* nopl (%[re]ax) */
1310 static const unsigned char alt_3[] =
1311 {0x0f,0x1f,0x00};
1312 /* nopl 0(%[re]ax) */
1313 static const unsigned char alt_4[] =
1314 {0x0f,0x1f,0x40,0x00};
1315 /* nopl 0(%[re]ax,%[re]ax,1) */
1316 static const unsigned char alt_5[] =
1317 {0x0f,0x1f,0x44,0x00,0x00};
1318 /* nopw 0(%[re]ax,%[re]ax,1) */
1319 static const unsigned char alt_6[] =
1320 {0x66,0x0f,0x1f,0x44,0x00,0x00};
1321 /* nopl 0L(%[re]ax) */
1322 static const unsigned char alt_7[] =
1323 {0x0f,0x1f,0x80,0x00,0x00,0x00,0x00};
1324 /* nopl 0L(%[re]ax,%[re]ax,1) */
1325 static const unsigned char alt_8[] =
1326 {0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1327 /* nopw 0L(%[re]ax,%[re]ax,1) */
1328 static const unsigned char alt_9[] =
1329 {0x66,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1330 /* nopw %cs:0L(%[re]ax,%[re]ax,1) */
1331 static const unsigned char alt_10[] =
1332 {0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1333 /* data16 nopw %cs:0L(%eax,%eax,1) */
1334 static const unsigned char alt_11[] =
1335 {0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1336 /* 32-bit and 64-bit NOPs patterns. */
1337 static const unsigned char *const alt_patt[] = {
1338 f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
1339 alt_9, alt_10, alt_11
1340 };
1341
1342 /* Genenerate COUNT bytes of NOPs to WHERE from PATT with the maximum
1343 size of a single NOP instruction MAX_SINGLE_NOP_SIZE. */
1344
1345 static void
1346 i386_output_nops (char *where, const unsigned char *const *patt,
1347 int count, int max_single_nop_size)
1348
1349 {
1350 /* Place the longer NOP first. */
1351 int last;
1352 int offset;
1353 const unsigned char *nops;
1354
1355 if (max_single_nop_size < 1)
1356 {
1357 as_fatal (_("i386_output_nops called to generate nops of at most %d bytes!"),
1358 max_single_nop_size);
1359 return;
1360 }
1361
1362 nops = patt[max_single_nop_size - 1];
1363
1364 /* Use the smaller one if the requsted one isn't available. */
1365 if (nops == NULL)
1366 {
1367 max_single_nop_size--;
1368 nops = patt[max_single_nop_size - 1];
1369 }
1370
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 if (nops == NULL)
1381 {
1382 /* Use the smaller one plus one-byte NOP if the needed one
1383 isn't available. */
1384 last--;
1385 nops = patt[last - 1];
1386 memcpy (where + offset, nops, last);
1387 where[offset + last] = *patt[0];
1388 }
1389 else
1390 memcpy (where + offset, nops, last);
1391 }
1392 }
1393
1394 static INLINE int
1395 fits_in_imm7 (offsetT num)
1396 {
1397 return (num & 0x7f) == num;
1398 }
1399
1400 static INLINE int
1401 fits_in_imm31 (offsetT num)
1402 {
1403 return (num & 0x7fffffff) == num;
1404 }
1405
1406 /* Genenerate COUNT bytes of NOPs to WHERE with the maximum size of a
1407 single NOP instruction LIMIT. */
1408
1409 void
1410 i386_generate_nops (fragS *fragP, char *where, offsetT count, int limit)
1411 {
1412 const unsigned char *const *patt = NULL;
1413 int max_single_nop_size;
1414 /* Maximum number of NOPs before switching to jump over NOPs. */
1415 int max_number_of_nops;
1416
1417 switch (fragP->fr_type)
1418 {
1419 case rs_fill_nop:
1420 case rs_align_code:
1421 break;
1422 case rs_machine_dependent:
1423 /* Allow NOP padding for jumps and calls. */
1424 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
1425 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
1426 break;
1427 /* Fall through. */
1428 default:
1429 return;
1430 }
1431
1432 /* We need to decide which NOP sequence to use for 32bit and
1433 64bit. When -mtune= is used:
1434
1435 1. For PROCESSOR_I386, PROCESSOR_I486, PROCESSOR_PENTIUM and
1436 PROCESSOR_GENERIC32, f32_patt will be used.
1437 2. For the rest, alt_patt will be used.
1438
1439 When -mtune= isn't used, alt_patt will be used if
1440 cpu_arch_isa_flags has CpuNop. Otherwise, f32_patt will
1441 be used.
1442
1443 When -march= or .arch is used, we can't use anything beyond
1444 cpu_arch_isa_flags. */
1445
1446 if (flag_code == CODE_16BIT)
1447 {
1448 patt = f16_patt;
1449 max_single_nop_size = sizeof (f16_patt) / sizeof (f16_patt[0]);
1450 /* Limit number of NOPs to 2 in 16-bit mode. */
1451 max_number_of_nops = 2;
1452 }
1453 else
1454 {
1455 if (fragP->tc_frag_data.isa == PROCESSOR_UNKNOWN)
1456 {
1457 /* PROCESSOR_UNKNOWN means that all ISAs may be used. */
1458 switch (cpu_arch_tune)
1459 {
1460 case PROCESSOR_UNKNOWN:
1461 /* We use cpu_arch_isa_flags to check if we SHOULD
1462 optimize with nops. */
1463 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
1464 patt = alt_patt;
1465 else
1466 patt = f32_patt;
1467 break;
1468 case PROCESSOR_PENTIUM4:
1469 case PROCESSOR_NOCONA:
1470 case PROCESSOR_CORE:
1471 case PROCESSOR_CORE2:
1472 case PROCESSOR_COREI7:
1473 case PROCESSOR_GENERIC64:
1474 case PROCESSOR_K6:
1475 case PROCESSOR_ATHLON:
1476 case PROCESSOR_K8:
1477 case PROCESSOR_AMDFAM10:
1478 case PROCESSOR_BD:
1479 case PROCESSOR_ZNVER:
1480 case PROCESSOR_BT:
1481 patt = alt_patt;
1482 break;
1483 case PROCESSOR_I386:
1484 case PROCESSOR_I486:
1485 case PROCESSOR_PENTIUM:
1486 case PROCESSOR_PENTIUMPRO:
1487 case PROCESSOR_IAMCU:
1488 case PROCESSOR_GENERIC32:
1489 patt = f32_patt;
1490 break;
1491 case PROCESSOR_NONE:
1492 abort ();
1493 }
1494 }
1495 else
1496 {
1497 switch (fragP->tc_frag_data.tune)
1498 {
1499 case PROCESSOR_UNKNOWN:
1500 /* When cpu_arch_isa is set, cpu_arch_tune shouldn't be
1501 PROCESSOR_UNKNOWN. */
1502 abort ();
1503 break;
1504
1505 case PROCESSOR_I386:
1506 case PROCESSOR_I486:
1507 case PROCESSOR_PENTIUM:
1508 case PROCESSOR_IAMCU:
1509 case PROCESSOR_K6:
1510 case PROCESSOR_ATHLON:
1511 case PROCESSOR_K8:
1512 case PROCESSOR_AMDFAM10:
1513 case PROCESSOR_BD:
1514 case PROCESSOR_ZNVER:
1515 case PROCESSOR_BT:
1516 case PROCESSOR_GENERIC32:
1517 /* We use cpu_arch_isa_flags to check if we CAN optimize
1518 with nops. */
1519 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
1520 patt = alt_patt;
1521 else
1522 patt = f32_patt;
1523 break;
1524 case PROCESSOR_PENTIUMPRO:
1525 case PROCESSOR_PENTIUM4:
1526 case PROCESSOR_NOCONA:
1527 case PROCESSOR_CORE:
1528 case PROCESSOR_CORE2:
1529 case PROCESSOR_COREI7:
1530 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
1531 patt = alt_patt;
1532 else
1533 patt = f32_patt;
1534 break;
1535 case PROCESSOR_GENERIC64:
1536 patt = alt_patt;
1537 break;
1538 case PROCESSOR_NONE:
1539 abort ();
1540 }
1541 }
1542
1543 if (patt == f32_patt)
1544 {
1545 max_single_nop_size = sizeof (f32_patt) / sizeof (f32_patt[0]);
1546 /* Limit number of NOPs to 2 for older processors. */
1547 max_number_of_nops = 2;
1548 }
1549 else
1550 {
1551 max_single_nop_size = sizeof (alt_patt) / sizeof (alt_patt[0]);
1552 /* Limit number of NOPs to 7 for newer processors. */
1553 max_number_of_nops = 7;
1554 }
1555 }
1556
1557 if (limit == 0)
1558 limit = max_single_nop_size;
1559
1560 if (fragP->fr_type == rs_fill_nop)
1561 {
1562 /* Output NOPs for .nop directive. */
1563 if (limit > max_single_nop_size)
1564 {
1565 as_bad_where (fragP->fr_file, fragP->fr_line,
1566 _("invalid single nop size: %d "
1567 "(expect within [0, %d])"),
1568 limit, max_single_nop_size);
1569 return;
1570 }
1571 }
1572 else if (fragP->fr_type != rs_machine_dependent)
1573 fragP->fr_var = count;
1574
1575 if ((count / max_single_nop_size) > max_number_of_nops)
1576 {
1577 /* Generate jump over NOPs. */
1578 offsetT disp = count - 2;
1579 if (fits_in_imm7 (disp))
1580 {
1581 /* Use "jmp disp8" if possible. */
1582 count = disp;
1583 where[0] = jump_disp8[0];
1584 where[1] = count;
1585 where += 2;
1586 }
1587 else
1588 {
1589 unsigned int size_of_jump;
1590
1591 if (flag_code == CODE_16BIT)
1592 {
1593 where[0] = jump16_disp32[0];
1594 where[1] = jump16_disp32[1];
1595 size_of_jump = 2;
1596 }
1597 else
1598 {
1599 where[0] = jump32_disp32[0];
1600 size_of_jump = 1;
1601 }
1602
1603 count -= size_of_jump + 4;
1604 if (!fits_in_imm31 (count))
1605 {
1606 as_bad_where (fragP->fr_file, fragP->fr_line,
1607 _("jump over nop padding out of range"));
1608 return;
1609 }
1610
1611 md_number_to_chars (where + size_of_jump, count, 4);
1612 where += size_of_jump + 4;
1613 }
1614 }
1615
1616 /* Generate multiple NOPs. */
1617 i386_output_nops (where, patt, count, limit);
1618 }
1619
1620 static INLINE int
1621 operand_type_all_zero (const union i386_operand_type *x)
1622 {
1623 switch (ARRAY_SIZE(x->array))
1624 {
1625 case 3:
1626 if (x->array[2])
1627 return 0;
1628 /* Fall through. */
1629 case 2:
1630 if (x->array[1])
1631 return 0;
1632 /* Fall through. */
1633 case 1:
1634 return !x->array[0];
1635 default:
1636 abort ();
1637 }
1638 }
1639
1640 static INLINE void
1641 operand_type_set (union i386_operand_type *x, unsigned int v)
1642 {
1643 switch (ARRAY_SIZE(x->array))
1644 {
1645 case 3:
1646 x->array[2] = v;
1647 /* Fall through. */
1648 case 2:
1649 x->array[1] = v;
1650 /* Fall through. */
1651 case 1:
1652 x->array[0] = v;
1653 /* Fall through. */
1654 break;
1655 default:
1656 abort ();
1657 }
1658
1659 x->bitfield.class = ClassNone;
1660 x->bitfield.instance = InstanceNone;
1661 }
1662
1663 static INLINE int
1664 operand_type_equal (const union i386_operand_type *x,
1665 const union i386_operand_type *y)
1666 {
1667 switch (ARRAY_SIZE(x->array))
1668 {
1669 case 3:
1670 if (x->array[2] != y->array[2])
1671 return 0;
1672 /* Fall through. */
1673 case 2:
1674 if (x->array[1] != y->array[1])
1675 return 0;
1676 /* Fall through. */
1677 case 1:
1678 return x->array[0] == y->array[0];
1679 break;
1680 default:
1681 abort ();
1682 }
1683 }
1684
1685 static INLINE bool
1686 is_cpu (const insn_template *t, enum i386_cpu cpu)
1687 {
1688 switch (cpu)
1689 {
1690 case Cpu287: return t->cpu.bitfield.cpu287;
1691 case Cpu387: return t->cpu.bitfield.cpu387;
1692 case Cpu3dnow: return t->cpu.bitfield.cpu3dnow;
1693 case Cpu3dnowA: return t->cpu.bitfield.cpu3dnowa;
1694 case CpuAVX: return t->cpu.bitfield.cpuavx;
1695 case CpuHLE: return t->cpu.bitfield.cpuhle;
1696 case CpuAVX512F: return t->cpu.bitfield.cpuavx512f;
1697 case CpuAVX512VL: return t->cpu.bitfield.cpuavx512vl;
1698 case Cpu64: return t->cpu.bitfield.cpu64;
1699 case CpuNo64: return t->cpu.bitfield.cpuno64;
1700 default:
1701 gas_assert (cpu < CpuAttrEnums);
1702 }
1703 return t->cpu.bitfield.isa == cpu + 1u;
1704 }
1705
1706 static i386_cpu_flags cpu_flags_from_attr (i386_cpu_attr a)
1707 {
1708 const unsigned int bps = sizeof (a.array[0]) * CHAR_BIT;
1709 i386_cpu_flags f = { .array[0] = 0 };
1710
1711 switch (ARRAY_SIZE(a.array))
1712 {
1713 case 1:
1714 f.array[CpuAttrEnums / bps]
1715 |= (a.array[0] >> CpuIsaBits) << (CpuAttrEnums % bps);
1716 if (CpuAttrEnums % bps > CpuIsaBits)
1717 f.array[CpuAttrEnums / bps + 1]
1718 = (a.array[0] >> CpuIsaBits) >> (bps - CpuAttrEnums % bps);
1719 break;
1720 default:
1721 abort ();
1722 }
1723
1724 if (a.bitfield.isa)
1725 f.array[(a.bitfield.isa - 1) / bps] |= 1u << ((a.bitfield.isa - 1) % bps);
1726
1727 return f;
1728 }
1729
1730 static INLINE int
1731 cpu_flags_all_zero (const union i386_cpu_flags *x)
1732 {
1733 switch (ARRAY_SIZE(x->array))
1734 {
1735 case 5:
1736 if (x->array[4])
1737 return 0;
1738 /* Fall through. */
1739 case 4:
1740 if (x->array[3])
1741 return 0;
1742 /* Fall through. */
1743 case 3:
1744 if (x->array[2])
1745 return 0;
1746 /* Fall through. */
1747 case 2:
1748 if (x->array[1])
1749 return 0;
1750 /* Fall through. */
1751 case 1:
1752 return !x->array[0];
1753 default:
1754 abort ();
1755 }
1756 }
1757
1758 static INLINE int
1759 cpu_flags_equal (const union i386_cpu_flags *x,
1760 const union i386_cpu_flags *y)
1761 {
1762 switch (ARRAY_SIZE(x->array))
1763 {
1764 case 5:
1765 if (x->array[4] != y->array[4])
1766 return 0;
1767 /* Fall through. */
1768 case 4:
1769 if (x->array[3] != y->array[3])
1770 return 0;
1771 /* Fall through. */
1772 case 3:
1773 if (x->array[2] != y->array[2])
1774 return 0;
1775 /* Fall through. */
1776 case 2:
1777 if (x->array[1] != y->array[1])
1778 return 0;
1779 /* Fall through. */
1780 case 1:
1781 return x->array[0] == y->array[0];
1782 break;
1783 default:
1784 abort ();
1785 }
1786 }
1787
1788 static INLINE int
1789 cpu_flags_check_cpu64 (const insn_template *t)
1790 {
1791 return flag_code == CODE_64BIT
1792 ? !t->cpu.bitfield.cpuno64
1793 : !t->cpu.bitfield.cpu64;
1794 }
1795
1796 static INLINE i386_cpu_flags
1797 cpu_flags_and (i386_cpu_flags x, i386_cpu_flags y)
1798 {
1799 switch (ARRAY_SIZE (x.array))
1800 {
1801 case 5:
1802 x.array [4] &= y.array [4];
1803 /* Fall through. */
1804 case 4:
1805 x.array [3] &= y.array [3];
1806 /* Fall through. */
1807 case 3:
1808 x.array [2] &= y.array [2];
1809 /* Fall through. */
1810 case 2:
1811 x.array [1] &= y.array [1];
1812 /* Fall through. */
1813 case 1:
1814 x.array [0] &= y.array [0];
1815 break;
1816 default:
1817 abort ();
1818 }
1819 return x;
1820 }
1821
1822 static INLINE i386_cpu_flags
1823 cpu_flags_or (i386_cpu_flags x, i386_cpu_flags y)
1824 {
1825 switch (ARRAY_SIZE (x.array))
1826 {
1827 case 5:
1828 x.array [4] |= y.array [4];
1829 /* Fall through. */
1830 case 4:
1831 x.array [3] |= y.array [3];
1832 /* Fall through. */
1833 case 3:
1834 x.array [2] |= y.array [2];
1835 /* Fall through. */
1836 case 2:
1837 x.array [1] |= y.array [1];
1838 /* Fall through. */
1839 case 1:
1840 x.array [0] |= y.array [0];
1841 break;
1842 default:
1843 abort ();
1844 }
1845 return x;
1846 }
1847
1848 static INLINE i386_cpu_flags
1849 cpu_flags_and_not (i386_cpu_flags x, i386_cpu_flags y)
1850 {
1851 switch (ARRAY_SIZE (x.array))
1852 {
1853 case 5:
1854 x.array [4] &= ~y.array [4];
1855 /* Fall through. */
1856 case 4:
1857 x.array [3] &= ~y.array [3];
1858 /* Fall through. */
1859 case 3:
1860 x.array [2] &= ~y.array [2];
1861 /* Fall through. */
1862 case 2:
1863 x.array [1] &= ~y.array [1];
1864 /* Fall through. */
1865 case 1:
1866 x.array [0] &= ~y.array [0];
1867 break;
1868 default:
1869 abort ();
1870 }
1871 return x;
1872 }
1873
1874 static const i386_cpu_flags avx512 = CPU_ANY_AVX512F_FLAGS;
1875
1876 static INLINE bool need_evex_encoding (void)
1877 {
1878 return i.vec_encoding == vex_encoding_evex
1879 || i.vec_encoding == vex_encoding_evex512
1880 || i.mask.reg;
1881 }
1882
1883 #define CPU_FLAGS_ARCH_MATCH 0x1
1884 #define CPU_FLAGS_64BIT_MATCH 0x2
1885
1886 #define CPU_FLAGS_PERFECT_MATCH \
1887 (CPU_FLAGS_ARCH_MATCH | CPU_FLAGS_64BIT_MATCH)
1888
1889 /* Return CPU flags match bits. */
1890
1891 static int
1892 cpu_flags_match (const insn_template *t)
1893 {
1894 i386_cpu_flags x = cpu_flags_from_attr (t->cpu);
1895 int match = cpu_flags_check_cpu64 (t) ? CPU_FLAGS_64BIT_MATCH : 0;
1896
1897 x.bitfield.cpu64 = 0;
1898 x.bitfield.cpuno64 = 0;
1899
1900 if (cpu_flags_all_zero (&x))
1901 {
1902 /* This instruction is available on all archs. */
1903 match |= CPU_FLAGS_ARCH_MATCH;
1904 }
1905 else
1906 {
1907 /* This instruction is available only on some archs. */
1908 i386_cpu_flags cpu = cpu_arch_flags;
1909
1910 /* Dual VEX/EVEX templates may need stripping of one of the flags. */
1911 if (t->opcode_modifier.vex && t->opcode_modifier.evex)
1912 {
1913 /* Dual AVX/AVX512F templates need to retain AVX512F only if we already
1914 know that EVEX encoding will be needed. */
1915 if ((x.bitfield.cpuavx || x.bitfield.cpuavx2)
1916 && x.bitfield.cpuavx512f)
1917 {
1918 if (need_evex_encoding ())
1919 {
1920 x.bitfield.cpuavx = 0;
1921 x.bitfield.cpuavx2 = 0;
1922 }
1923 /* need_evex_encoding() isn't reliable before operands were
1924 parsed. */
1925 else if (i.operands)
1926 {
1927 x.bitfield.cpuavx512f = 0;
1928 x.bitfield.cpuavx512vl = 0;
1929 if (x.bitfield.cpufma && !cpu.bitfield.cpufma)
1930 x.bitfield.cpuavx = 0;
1931 }
1932 }
1933 }
1934
1935 /* AVX512VL is no standalone feature - match it and then strip it. */
1936 if (x.bitfield.cpuavx512vl && !cpu.bitfield.cpuavx512vl)
1937 return match;
1938 x.bitfield.cpuavx512vl = 0;
1939
1940 /* AVX and AVX2 present at the same time express an operand size
1941 dependency - strip AVX2 for the purposes here. The operand size
1942 dependent check occurs in check_vecOperands(). */
1943 if (x.bitfield.cpuavx && x.bitfield.cpuavx2)
1944 x.bitfield.cpuavx2 = 0;
1945
1946 cpu = cpu_flags_and (x, cpu);
1947 if (!cpu_flags_all_zero (&cpu))
1948 {
1949 if (t->cpu.bitfield.cpuavx && t->cpu.bitfield.cpuavx512f)
1950 {
1951 if ((need_evex_encoding ()
1952 ? cpu.bitfield.cpuavx512f
1953 : cpu.bitfield.cpuavx)
1954 && (!x.bitfield.cpufma || cpu.bitfield.cpufma
1955 || cpu_arch_flags.bitfield.cpuavx512f)
1956 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
1957 && (!x.bitfield.cpuvaes || cpu.bitfield.cpuvaes)
1958 && (!x.bitfield.cpuvpclmulqdq || cpu.bitfield.cpuvpclmulqdq))
1959 match |= CPU_FLAGS_ARCH_MATCH;
1960 }
1961 else if (x.bitfield.cpuavx)
1962 {
1963 /* We need to check a few extra flags with AVX. */
1964 if (cpu.bitfield.cpuavx
1965 && (!t->opcode_modifier.sse2avx
1966 || (sse2avx && !i.prefix[DATA_PREFIX]))
1967 && (!x.bitfield.cpuaes || cpu.bitfield.cpuaes)
1968 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
1969 && (!x.bitfield.cpupclmulqdq || cpu.bitfield.cpupclmulqdq))
1970 match |= CPU_FLAGS_ARCH_MATCH;
1971 }
1972 else if (x.bitfield.cpuavx2 && cpu.bitfield.cpuavx2)
1973 match |= CPU_FLAGS_ARCH_MATCH;
1974 else if (x.bitfield.cpuavx512f)
1975 {
1976 /* We need to check a few extra flags with AVX512F. */
1977 if (cpu.bitfield.cpuavx512f
1978 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni))
1979 match |= CPU_FLAGS_ARCH_MATCH;
1980 }
1981 else
1982 match |= CPU_FLAGS_ARCH_MATCH;
1983 }
1984 }
1985 return match;
1986 }
1987
1988 static INLINE i386_operand_type
1989 operand_type_and (i386_operand_type x, i386_operand_type y)
1990 {
1991 if (x.bitfield.class != y.bitfield.class)
1992 x.bitfield.class = ClassNone;
1993 if (x.bitfield.instance != y.bitfield.instance)
1994 x.bitfield.instance = InstanceNone;
1995
1996 switch (ARRAY_SIZE (x.array))
1997 {
1998 case 3:
1999 x.array [2] &= y.array [2];
2000 /* Fall through. */
2001 case 2:
2002 x.array [1] &= y.array [1];
2003 /* Fall through. */
2004 case 1:
2005 x.array [0] &= y.array [0];
2006 break;
2007 default:
2008 abort ();
2009 }
2010 return x;
2011 }
2012
2013 static INLINE i386_operand_type
2014 operand_type_and_not (i386_operand_type x, i386_operand_type y)
2015 {
2016 gas_assert (y.bitfield.class == ClassNone);
2017 gas_assert (y.bitfield.instance == InstanceNone);
2018
2019 switch (ARRAY_SIZE (x.array))
2020 {
2021 case 3:
2022 x.array [2] &= ~y.array [2];
2023 /* Fall through. */
2024 case 2:
2025 x.array [1] &= ~y.array [1];
2026 /* Fall through. */
2027 case 1:
2028 x.array [0] &= ~y.array [0];
2029 break;
2030 default:
2031 abort ();
2032 }
2033 return x;
2034 }
2035
2036 static INLINE i386_operand_type
2037 operand_type_or (i386_operand_type x, i386_operand_type y)
2038 {
2039 gas_assert (x.bitfield.class == ClassNone ||
2040 y.bitfield.class == ClassNone ||
2041 x.bitfield.class == y.bitfield.class);
2042 gas_assert (x.bitfield.instance == InstanceNone ||
2043 y.bitfield.instance == InstanceNone ||
2044 x.bitfield.instance == y.bitfield.instance);
2045
2046 switch (ARRAY_SIZE (x.array))
2047 {
2048 case 3:
2049 x.array [2] |= y.array [2];
2050 /* Fall through. */
2051 case 2:
2052 x.array [1] |= y.array [1];
2053 /* Fall through. */
2054 case 1:
2055 x.array [0] |= y.array [0];
2056 break;
2057 default:
2058 abort ();
2059 }
2060 return x;
2061 }
2062
2063 static INLINE i386_operand_type
2064 operand_type_xor (i386_operand_type x, i386_operand_type y)
2065 {
2066 gas_assert (y.bitfield.class == ClassNone);
2067 gas_assert (y.bitfield.instance == InstanceNone);
2068
2069 switch (ARRAY_SIZE (x.array))
2070 {
2071 case 3:
2072 x.array [2] ^= y.array [2];
2073 /* Fall through. */
2074 case 2:
2075 x.array [1] ^= y.array [1];
2076 /* Fall through. */
2077 case 1:
2078 x.array [0] ^= y.array [0];
2079 break;
2080 default:
2081 abort ();
2082 }
2083 return x;
2084 }
2085
2086 static const i386_operand_type anydisp = {
2087 .bitfield = { .disp8 = 1, .disp16 = 1, .disp32 = 1, .disp64 = 1 }
2088 };
2089
2090 enum operand_type
2091 {
2092 reg,
2093 imm,
2094 disp,
2095 anymem
2096 };
2097
2098 static INLINE int
2099 operand_type_check (i386_operand_type t, enum operand_type c)
2100 {
2101 switch (c)
2102 {
2103 case reg:
2104 return t.bitfield.class == Reg;
2105
2106 case imm:
2107 return (t.bitfield.imm8
2108 || t.bitfield.imm8s
2109 || t.bitfield.imm16
2110 || t.bitfield.imm32
2111 || t.bitfield.imm32s
2112 || t.bitfield.imm64);
2113
2114 case disp:
2115 return (t.bitfield.disp8
2116 || t.bitfield.disp16
2117 || t.bitfield.disp32
2118 || t.bitfield.disp64);
2119
2120 case anymem:
2121 return (t.bitfield.disp8
2122 || t.bitfield.disp16
2123 || t.bitfield.disp32
2124 || t.bitfield.disp64
2125 || t.bitfield.baseindex);
2126
2127 default:
2128 abort ();
2129 }
2130
2131 return 0;
2132 }
2133
2134 /* Return 1 if there is no conflict in 8bit/16bit/32bit/64bit/80bit size
2135 between operand GIVEN and opeand WANTED for instruction template T. */
2136
2137 static INLINE int
2138 match_operand_size (const insn_template *t, unsigned int wanted,
2139 unsigned int given)
2140 {
2141 return !((i.types[given].bitfield.byte
2142 && !t->operand_types[wanted].bitfield.byte)
2143 || (i.types[given].bitfield.word
2144 && !t->operand_types[wanted].bitfield.word)
2145 || (i.types[given].bitfield.dword
2146 && !t->operand_types[wanted].bitfield.dword)
2147 || (i.types[given].bitfield.qword
2148 && (!t->operand_types[wanted].bitfield.qword
2149 /* Don't allow 64-bit (memory) operands outside of 64-bit
2150 mode, when they're used where a 64-bit GPR could also
2151 be used. Checking is needed for Intel Syntax only. */
2152 || (intel_syntax
2153 && flag_code != CODE_64BIT
2154 && (t->operand_types[wanted].bitfield.class == Reg
2155 || t->operand_types[wanted].bitfield.class == Accum
2156 || t->opcode_modifier.isstring))))
2157 || (i.types[given].bitfield.tbyte
2158 && !t->operand_types[wanted].bitfield.tbyte));
2159 }
2160
2161 /* Return 1 if there is no conflict in SIMD register between operand
2162 GIVEN and opeand WANTED for instruction template T. */
2163
2164 static INLINE int
2165 match_simd_size (const insn_template *t, unsigned int wanted,
2166 unsigned int given)
2167 {
2168 return !((i.types[given].bitfield.xmmword
2169 && !t->operand_types[wanted].bitfield.xmmword)
2170 || (i.types[given].bitfield.ymmword
2171 && !t->operand_types[wanted].bitfield.ymmword)
2172 || (i.types[given].bitfield.zmmword
2173 && !t->operand_types[wanted].bitfield.zmmword)
2174 || (i.types[given].bitfield.tmmword
2175 && !t->operand_types[wanted].bitfield.tmmword));
2176 }
2177
2178 /* Return 1 if there is no conflict in any size between operand GIVEN
2179 and opeand WANTED for instruction template T. */
2180
2181 static INLINE int
2182 match_mem_size (const insn_template *t, unsigned int wanted,
2183 unsigned int given)
2184 {
2185 return (match_operand_size (t, wanted, given)
2186 && !((i.types[given].bitfield.unspecified
2187 && !i.broadcast.type
2188 && !i.broadcast.bytes
2189 && !t->operand_types[wanted].bitfield.unspecified)
2190 || (i.types[given].bitfield.fword
2191 && !t->operand_types[wanted].bitfield.fword)
2192 /* For scalar opcode templates to allow register and memory
2193 operands at the same time, some special casing is needed
2194 here. Also for v{,p}broadcast*, {,v}pmov{s,z}*, and
2195 down-conversion vpmov*. */
2196 || ((t->operand_types[wanted].bitfield.class == RegSIMD
2197 && t->operand_types[wanted].bitfield.byte
2198 + t->operand_types[wanted].bitfield.word
2199 + t->operand_types[wanted].bitfield.dword
2200 + t->operand_types[wanted].bitfield.qword
2201 > !!t->opcode_modifier.broadcast)
2202 ? (i.types[given].bitfield.xmmword
2203 || i.types[given].bitfield.ymmword
2204 || i.types[given].bitfield.zmmword)
2205 : !match_simd_size(t, wanted, given))));
2206 }
2207
2208 /* Return value has MATCH_STRAIGHT set if there is no size conflict on any
2209 operands for instruction template T, and it has MATCH_REVERSE set if there
2210 is no size conflict on any operands for the template with operands reversed
2211 (and the template allows for reversing in the first place). */
2212
2213 #define MATCH_STRAIGHT 1
2214 #define MATCH_REVERSE 2
2215
2216 static INLINE unsigned int
2217 operand_size_match (const insn_template *t)
2218 {
2219 unsigned int j, match = MATCH_STRAIGHT;
2220
2221 /* Don't check non-absolute jump instructions. */
2222 if (t->opcode_modifier.jump
2223 && t->opcode_modifier.jump != JUMP_ABSOLUTE)
2224 return match;
2225
2226 /* Check memory and accumulator operand size. */
2227 for (j = 0; j < i.operands; j++)
2228 {
2229 if (i.types[j].bitfield.class != Reg
2230 && i.types[j].bitfield.class != RegSIMD
2231 && t->opcode_modifier.operandconstraint == ANY_SIZE)
2232 continue;
2233
2234 if (t->operand_types[j].bitfield.class == Reg
2235 && !match_operand_size (t, j, j))
2236 {
2237 match = 0;
2238 break;
2239 }
2240
2241 if (t->operand_types[j].bitfield.class == RegSIMD
2242 && !match_simd_size (t, j, j))
2243 {
2244 match = 0;
2245 break;
2246 }
2247
2248 if (t->operand_types[j].bitfield.instance == Accum
2249 && (!match_operand_size (t, j, j) || !match_simd_size (t, j, j)))
2250 {
2251 match = 0;
2252 break;
2253 }
2254
2255 if ((i.flags[j] & Operand_Mem) && !match_mem_size (t, j, j))
2256 {
2257 match = 0;
2258 break;
2259 }
2260 }
2261
2262 if (!t->opcode_modifier.d)
2263 return match;
2264
2265 /* Check reverse. */
2266 gas_assert (i.operands >= 2);
2267
2268 for (j = 0; j < i.operands; j++)
2269 {
2270 unsigned int given = i.operands - j - 1;
2271
2272 /* For FMA4 and XOP insns VEX.W controls just the first two
2273 register operands. */
2274 if (is_cpu (t, CpuFMA4) || is_cpu (t, CpuXOP))
2275 given = j < 2 ? 1 - j : j;
2276
2277 if (t->operand_types[j].bitfield.class == Reg
2278 && !match_operand_size (t, j, given))
2279 return match;
2280
2281 if (t->operand_types[j].bitfield.class == RegSIMD
2282 && !match_simd_size (t, j, given))
2283 return match;
2284
2285 if (t->operand_types[j].bitfield.instance == Accum
2286 && (!match_operand_size (t, j, given)
2287 || !match_simd_size (t, j, given)))
2288 return match;
2289
2290 if ((i.flags[given] & Operand_Mem) && !match_mem_size (t, j, given))
2291 return match;
2292 }
2293
2294 return match | MATCH_REVERSE;
2295 }
2296
2297 static INLINE int
2298 operand_type_match (i386_operand_type overlap,
2299 i386_operand_type given)
2300 {
2301 i386_operand_type temp = overlap;
2302
2303 temp.bitfield.unspecified = 0;
2304 temp.bitfield.byte = 0;
2305 temp.bitfield.word = 0;
2306 temp.bitfield.dword = 0;
2307 temp.bitfield.fword = 0;
2308 temp.bitfield.qword = 0;
2309 temp.bitfield.tbyte = 0;
2310 temp.bitfield.xmmword = 0;
2311 temp.bitfield.ymmword = 0;
2312 temp.bitfield.zmmword = 0;
2313 temp.bitfield.tmmword = 0;
2314 if (operand_type_all_zero (&temp))
2315 goto mismatch;
2316
2317 if (given.bitfield.baseindex == overlap.bitfield.baseindex)
2318 return 1;
2319
2320 mismatch:
2321 i.error = operand_type_mismatch;
2322 return 0;
2323 }
2324
2325 /* If given types g0 and g1 are registers they must be of the same type
2326 unless the expected operand type register overlap is null.
2327 Intel syntax sized memory operands are also checked here. */
2328
2329 static INLINE int
2330 operand_type_register_match (i386_operand_type g0,
2331 i386_operand_type t0,
2332 i386_operand_type g1,
2333 i386_operand_type t1)
2334 {
2335 if (g0.bitfield.class != Reg
2336 && g0.bitfield.class != RegSIMD
2337 && (g0.bitfield.unspecified
2338 || !operand_type_check (g0, anymem)))
2339 return 1;
2340
2341 if (g1.bitfield.class != Reg
2342 && g1.bitfield.class != RegSIMD
2343 && (g1.bitfield.unspecified
2344 || !operand_type_check (g1, anymem)))
2345 return 1;
2346
2347 if (g0.bitfield.byte == g1.bitfield.byte
2348 && g0.bitfield.word == g1.bitfield.word
2349 && g0.bitfield.dword == g1.bitfield.dword
2350 && g0.bitfield.qword == g1.bitfield.qword
2351 && g0.bitfield.xmmword == g1.bitfield.xmmword
2352 && g0.bitfield.ymmword == g1.bitfield.ymmword
2353 && g0.bitfield.zmmword == g1.bitfield.zmmword)
2354 return 1;
2355
2356 /* If expectations overlap in no more than a single size, all is fine. */
2357 g0 = operand_type_and (t0, t1);
2358 if (g0.bitfield.byte
2359 + g0.bitfield.word
2360 + g0.bitfield.dword
2361 + g0.bitfield.qword
2362 + g0.bitfield.xmmword
2363 + g0.bitfield.ymmword
2364 + g0.bitfield.zmmword <= 1)
2365 return 1;
2366
2367 i.error = register_type_mismatch;
2368
2369 return 0;
2370 }
2371
2372 static INLINE unsigned int
2373 register_number (const reg_entry *r)
2374 {
2375 unsigned int nr = r->reg_num;
2376
2377 if (r->reg_flags & RegRex)
2378 nr += 8;
2379
2380 if (r->reg_flags & RegVRex)
2381 nr += 16;
2382
2383 return nr;
2384 }
2385
2386 static INLINE unsigned int
2387 mode_from_disp_size (i386_operand_type t)
2388 {
2389 if (t.bitfield.disp8)
2390 return 1;
2391 else if (t.bitfield.disp16
2392 || t.bitfield.disp32)
2393 return 2;
2394 else
2395 return 0;
2396 }
2397
2398 static INLINE int
2399 fits_in_signed_byte (addressT num)
2400 {
2401 return num + 0x80 <= 0xff;
2402 }
2403
2404 static INLINE int
2405 fits_in_unsigned_byte (addressT num)
2406 {
2407 return num <= 0xff;
2408 }
2409
2410 static INLINE int
2411 fits_in_unsigned_word (addressT num)
2412 {
2413 return num <= 0xffff;
2414 }
2415
2416 static INLINE int
2417 fits_in_signed_word (addressT num)
2418 {
2419 return num + 0x8000 <= 0xffff;
2420 }
2421
2422 static INLINE int
2423 fits_in_signed_long (addressT num ATTRIBUTE_UNUSED)
2424 {
2425 #ifndef BFD64
2426 return 1;
2427 #else
2428 return num + 0x80000000 <= 0xffffffff;
2429 #endif
2430 } /* fits_in_signed_long() */
2431
2432 static INLINE int
2433 fits_in_unsigned_long (addressT num ATTRIBUTE_UNUSED)
2434 {
2435 #ifndef BFD64
2436 return 1;
2437 #else
2438 return num <= 0xffffffff;
2439 #endif
2440 } /* fits_in_unsigned_long() */
2441
2442 static INLINE valueT extend_to_32bit_address (addressT num)
2443 {
2444 #ifdef BFD64
2445 if (fits_in_unsigned_long(num))
2446 return (num ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
2447
2448 if (!fits_in_signed_long (num))
2449 return num & 0xffffffff;
2450 #endif
2451
2452 return num;
2453 }
2454
2455 static INLINE int
2456 fits_in_disp8 (offsetT num)
2457 {
2458 int shift = i.memshift;
2459 unsigned int mask;
2460
2461 if (shift == -1)
2462 abort ();
2463
2464 mask = (1 << shift) - 1;
2465
2466 /* Return 0 if NUM isn't properly aligned. */
2467 if ((num & mask))
2468 return 0;
2469
2470 /* Check if NUM will fit in 8bit after shift. */
2471 return fits_in_signed_byte (num >> shift);
2472 }
2473
2474 static INLINE int
2475 fits_in_imm4 (offsetT num)
2476 {
2477 /* Despite the name, check for imm3 if we're dealing with EVEX. */
2478 return (num & (i.vec_encoding != vex_encoding_evex ? 0xf : 7)) == num;
2479 }
2480
2481 static i386_operand_type
2482 smallest_imm_type (offsetT num)
2483 {
2484 i386_operand_type t;
2485
2486 operand_type_set (&t, 0);
2487 t.bitfield.imm64 = 1;
2488
2489 if (cpu_arch_tune != PROCESSOR_I486 && num == 1)
2490 {
2491 /* This code is disabled on the 486 because all the Imm1 forms
2492 in the opcode table are slower on the i486. They're the
2493 versions with the implicitly specified single-position
2494 displacement, which has another syntax if you really want to
2495 use that form. */
2496 t.bitfield.imm1 = 1;
2497 t.bitfield.imm8 = 1;
2498 t.bitfield.imm8s = 1;
2499 t.bitfield.imm16 = 1;
2500 t.bitfield.imm32 = 1;
2501 t.bitfield.imm32s = 1;
2502 }
2503 else if (fits_in_signed_byte (num))
2504 {
2505 if (fits_in_unsigned_byte (num))
2506 t.bitfield.imm8 = 1;
2507 t.bitfield.imm8s = 1;
2508 t.bitfield.imm16 = 1;
2509 t.bitfield.imm32 = 1;
2510 t.bitfield.imm32s = 1;
2511 }
2512 else if (fits_in_unsigned_byte (num))
2513 {
2514 t.bitfield.imm8 = 1;
2515 t.bitfield.imm16 = 1;
2516 t.bitfield.imm32 = 1;
2517 t.bitfield.imm32s = 1;
2518 }
2519 else if (fits_in_signed_word (num) || fits_in_unsigned_word (num))
2520 {
2521 t.bitfield.imm16 = 1;
2522 t.bitfield.imm32 = 1;
2523 t.bitfield.imm32s = 1;
2524 }
2525 else if (fits_in_signed_long (num))
2526 {
2527 t.bitfield.imm32 = 1;
2528 t.bitfield.imm32s = 1;
2529 }
2530 else if (fits_in_unsigned_long (num))
2531 t.bitfield.imm32 = 1;
2532
2533 return t;
2534 }
2535
2536 static offsetT
2537 offset_in_range (offsetT val, int size)
2538 {
2539 addressT mask;
2540
2541 switch (size)
2542 {
2543 case 1: mask = ((addressT) 1 << 8) - 1; break;
2544 case 2: mask = ((addressT) 1 << 16) - 1; break;
2545 #ifdef BFD64
2546 case 4: mask = ((addressT) 1 << 32) - 1; break;
2547 #endif
2548 case sizeof (val): return val;
2549 default: abort ();
2550 }
2551
2552 if ((val & ~mask) != 0 && (-val & ~mask) != 0)
2553 as_warn (_("0x%" PRIx64 " shortened to 0x%" PRIx64),
2554 (uint64_t) val, (uint64_t) (val & mask));
2555
2556 return val & mask;
2557 }
2558
2559 static INLINE const char *insn_name (const insn_template *t)
2560 {
2561 return &i386_mnemonics[t->mnem_off];
2562 }
2563
2564 enum PREFIX_GROUP
2565 {
2566 PREFIX_EXIST = 0,
2567 PREFIX_LOCK,
2568 PREFIX_REP,
2569 PREFIX_DS,
2570 PREFIX_OTHER
2571 };
2572
2573 /* Returns
2574 a. PREFIX_EXIST if attempting to add a prefix where one from the
2575 same class already exists.
2576 b. PREFIX_LOCK if lock prefix is added.
2577 c. PREFIX_REP if rep/repne prefix is added.
2578 d. PREFIX_DS if ds prefix is added.
2579 e. PREFIX_OTHER if other prefix is added.
2580 */
2581
2582 static enum PREFIX_GROUP
2583 add_prefix (unsigned int prefix)
2584 {
2585 enum PREFIX_GROUP ret = PREFIX_OTHER;
2586 unsigned int q;
2587
2588 if (prefix >= REX_OPCODE && prefix < REX_OPCODE + 16
2589 && flag_code == CODE_64BIT)
2590 {
2591 if ((i.prefix[REX_PREFIX] & prefix & REX_W)
2592 || (i.prefix[REX_PREFIX] & prefix & REX_R)
2593 || (i.prefix[REX_PREFIX] & prefix & REX_X)
2594 || (i.prefix[REX_PREFIX] & prefix & REX_B))
2595 ret = PREFIX_EXIST;
2596 q = REX_PREFIX;
2597 }
2598 else
2599 {
2600 switch (prefix)
2601 {
2602 default:
2603 abort ();
2604
2605 case DS_PREFIX_OPCODE:
2606 ret = PREFIX_DS;
2607 /* Fall through. */
2608 case CS_PREFIX_OPCODE:
2609 case ES_PREFIX_OPCODE:
2610 case FS_PREFIX_OPCODE:
2611 case GS_PREFIX_OPCODE:
2612 case SS_PREFIX_OPCODE:
2613 q = SEG_PREFIX;
2614 break;
2615
2616 case REPNE_PREFIX_OPCODE:
2617 case REPE_PREFIX_OPCODE:
2618 q = REP_PREFIX;
2619 ret = PREFIX_REP;
2620 break;
2621
2622 case LOCK_PREFIX_OPCODE:
2623 q = LOCK_PREFIX;
2624 ret = PREFIX_LOCK;
2625 break;
2626
2627 case FWAIT_OPCODE:
2628 q = WAIT_PREFIX;
2629 break;
2630
2631 case ADDR_PREFIX_OPCODE:
2632 q = ADDR_PREFIX;
2633 break;
2634
2635 case DATA_PREFIX_OPCODE:
2636 q = DATA_PREFIX;
2637 break;
2638 }
2639 if (i.prefix[q] != 0)
2640 ret = PREFIX_EXIST;
2641 }
2642
2643 if (ret)
2644 {
2645 if (!i.prefix[q])
2646 ++i.prefixes;
2647 i.prefix[q] |= prefix;
2648 }
2649 else
2650 as_bad (_("same type of prefix used twice"));
2651
2652 return ret;
2653 }
2654
2655 static void
2656 update_code_flag (int value, int check)
2657 {
2658 PRINTF_LIKE ((*as_error)) = check ? as_fatal : as_bad;
2659
2660 if (value == CODE_64BIT && !cpu_arch_flags.bitfield.cpu64 )
2661 {
2662 as_error (_("64bit mode not supported on `%s'."),
2663 cpu_arch_name ? cpu_arch_name : default_arch);
2664 return;
2665 }
2666
2667 if (value == CODE_32BIT && !cpu_arch_flags.bitfield.cpui386)
2668 {
2669 as_error (_("32bit mode not supported on `%s'."),
2670 cpu_arch_name ? cpu_arch_name : default_arch);
2671 return;
2672 }
2673
2674 flag_code = (enum flag_code) value;
2675
2676 stackop_size = '\0';
2677 }
2678
2679 static void
2680 set_code_flag (int value)
2681 {
2682 update_code_flag (value, 0);
2683 }
2684
2685 static void
2686 set_16bit_gcc_code_flag (int new_code_flag)
2687 {
2688 flag_code = (enum flag_code) new_code_flag;
2689 if (flag_code != CODE_16BIT)
2690 abort ();
2691 stackop_size = LONG_MNEM_SUFFIX;
2692 }
2693
2694 static void
2695 set_intel_syntax (int syntax_flag)
2696 {
2697 /* Find out if register prefixing is specified. */
2698 int ask_naked_reg = 0;
2699
2700 SKIP_WHITESPACE ();
2701 if (!is_end_of_line[(unsigned char) *input_line_pointer])
2702 {
2703 char *string;
2704 int e = get_symbol_name (&string);
2705
2706 if (strcmp (string, "prefix") == 0)
2707 ask_naked_reg = 1;
2708 else if (strcmp (string, "noprefix") == 0)
2709 ask_naked_reg = -1;
2710 else
2711 as_bad (_("bad argument to syntax directive."));
2712 (void) restore_line_pointer (e);
2713 }
2714 demand_empty_rest_of_line ();
2715
2716 intel_syntax = syntax_flag;
2717
2718 if (ask_naked_reg == 0)
2719 allow_naked_reg = (intel_syntax
2720 && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
2721 else
2722 allow_naked_reg = (ask_naked_reg < 0);
2723
2724 expr_set_rank (O_full_ptr, syntax_flag ? 10 : 0);
2725
2726 register_prefix = allow_naked_reg ? "" : "%";
2727 }
2728
2729 static void
2730 set_intel_mnemonic (int mnemonic_flag)
2731 {
2732 intel_mnemonic = mnemonic_flag;
2733 }
2734
2735 static void
2736 set_allow_index_reg (int flag)
2737 {
2738 allow_index_reg = flag;
2739 }
2740
2741 static void
2742 set_check (int what)
2743 {
2744 enum check_kind *kind;
2745 const char *str;
2746
2747 if (what)
2748 {
2749 kind = &operand_check;
2750 str = "operand";
2751 }
2752 else
2753 {
2754 kind = &sse_check;
2755 str = "sse";
2756 }
2757
2758 SKIP_WHITESPACE ();
2759
2760 if (!is_end_of_line[(unsigned char) *input_line_pointer])
2761 {
2762 char *string;
2763 int e = get_symbol_name (&string);
2764
2765 if (strcmp (string, "none") == 0)
2766 *kind = check_none;
2767 else if (strcmp (string, "warning") == 0)
2768 *kind = check_warning;
2769 else if (strcmp (string, "error") == 0)
2770 *kind = check_error;
2771 else
2772 as_bad (_("bad argument to %s_check directive."), str);
2773 (void) restore_line_pointer (e);
2774 }
2775 else
2776 as_bad (_("missing argument for %s_check directive"), str);
2777
2778 demand_empty_rest_of_line ();
2779 }
2780
2781 static void
2782 check_cpu_arch_compatible (const char *name ATTRIBUTE_UNUSED,
2783 i386_cpu_flags new_flag ATTRIBUTE_UNUSED)
2784 {
2785 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2786 static const char *arch;
2787
2788 /* Intel MCU is only supported on ELF. */
2789 if (!IS_ELF)
2790 return;
2791
2792 if (!arch)
2793 {
2794 /* Use cpu_arch_name if it is set in md_parse_option. Otherwise
2795 use default_arch. */
2796 arch = cpu_arch_name;
2797 if (!arch)
2798 arch = default_arch;
2799 }
2800
2801 /* If we are targeting Intel MCU, we must enable it. */
2802 if ((get_elf_backend_data (stdoutput)->elf_machine_code == EM_IAMCU)
2803 == new_flag.bitfield.cpuiamcu)
2804 return;
2805
2806 as_bad (_("`%s' is not supported on `%s'"), name, arch);
2807 #endif
2808 }
2809
2810 static void
2811 extend_cpu_sub_arch_name (const char *name)
2812 {
2813 if (cpu_sub_arch_name)
2814 cpu_sub_arch_name = reconcat (cpu_sub_arch_name, cpu_sub_arch_name,
2815 ".", name, (const char *) NULL);
2816 else
2817 cpu_sub_arch_name = concat (".", name, (const char *) NULL);
2818 }
2819
2820 static void
2821 set_cpu_arch (int dummy ATTRIBUTE_UNUSED)
2822 {
2823 typedef struct arch_stack_entry
2824 {
2825 const struct arch_stack_entry *prev;
2826 const char *name;
2827 char *sub_name;
2828 i386_cpu_flags flags;
2829 i386_cpu_flags isa_flags;
2830 enum processor_type isa;
2831 enum flag_code flag_code;
2832 unsigned int vector_size;
2833 char stackop_size;
2834 bool no_cond_jump_promotion;
2835 } arch_stack_entry;
2836 static const arch_stack_entry *arch_stack_top;
2837 char *s;
2838 int e;
2839 const char *string;
2840 unsigned int j = 0;
2841 i386_cpu_flags flags;
2842
2843 SKIP_WHITESPACE ();
2844
2845 if (is_end_of_line[(unsigned char) *input_line_pointer])
2846 {
2847 as_bad (_("missing cpu architecture"));
2848 input_line_pointer++;
2849 return;
2850 }
2851
2852 e = get_symbol_name (&s);
2853 string = s;
2854
2855 if (strcmp (string, "push") == 0)
2856 {
2857 arch_stack_entry *top = XNEW (arch_stack_entry);
2858
2859 top->name = cpu_arch_name;
2860 if (cpu_sub_arch_name)
2861 top->sub_name = xstrdup (cpu_sub_arch_name);
2862 else
2863 top->sub_name = NULL;
2864 top->flags = cpu_arch_flags;
2865 top->isa = cpu_arch_isa;
2866 top->isa_flags = cpu_arch_isa_flags;
2867 top->flag_code = flag_code;
2868 top->vector_size = vector_size;
2869 top->stackop_size = stackop_size;
2870 top->no_cond_jump_promotion = no_cond_jump_promotion;
2871
2872 top->prev = arch_stack_top;
2873 arch_stack_top = top;
2874
2875 (void) restore_line_pointer (e);
2876 demand_empty_rest_of_line ();
2877 return;
2878 }
2879
2880 if (strcmp (string, "pop") == 0)
2881 {
2882 const arch_stack_entry *top = arch_stack_top;
2883
2884 if (!top)
2885 as_bad (_(".arch stack is empty"));
2886 else if (top->flag_code != flag_code
2887 || top->stackop_size != stackop_size)
2888 {
2889 static const unsigned int bits[] = {
2890 [CODE_16BIT] = 16,
2891 [CODE_32BIT] = 32,
2892 [CODE_64BIT] = 64,
2893 };
2894
2895 as_bad (_("this `.arch pop' requires `.code%u%s' to be in effect"),
2896 bits[top->flag_code],
2897 top->stackop_size == LONG_MNEM_SUFFIX ? "gcc" : "");
2898 }
2899 else
2900 {
2901 arch_stack_top = top->prev;
2902
2903 cpu_arch_name = top->name;
2904 free (cpu_sub_arch_name);
2905 cpu_sub_arch_name = top->sub_name;
2906 cpu_arch_flags = top->flags;
2907 cpu_arch_isa = top->isa;
2908 cpu_arch_isa_flags = top->isa_flags;
2909 vector_size = top->vector_size;
2910 no_cond_jump_promotion = top->no_cond_jump_promotion;
2911
2912 XDELETE (top);
2913 }
2914
2915 (void) restore_line_pointer (e);
2916 demand_empty_rest_of_line ();
2917 return;
2918 }
2919
2920 if (strcmp (string, "default") == 0)
2921 {
2922 if (strcmp (default_arch, "iamcu") == 0)
2923 string = default_arch;
2924 else
2925 {
2926 static const i386_cpu_flags cpu_unknown_flags = CPU_UNKNOWN_FLAGS;
2927
2928 cpu_arch_name = NULL;
2929 free (cpu_sub_arch_name);
2930 cpu_sub_arch_name = NULL;
2931 cpu_arch_flags = cpu_unknown_flags;
2932 cpu_arch_isa = PROCESSOR_UNKNOWN;
2933 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
2934 if (!cpu_arch_tune_set)
2935 {
2936 cpu_arch_tune = cpu_arch_isa;
2937 cpu_arch_tune_flags = cpu_arch_isa_flags;
2938 }
2939
2940 vector_size = VSZ_DEFAULT;
2941
2942 j = ARRAY_SIZE (cpu_arch) + 1;
2943 }
2944 }
2945
2946 for (; j < ARRAY_SIZE (cpu_arch); j++)
2947 {
2948 if (strcmp (string + (*string == '.'), cpu_arch[j].name) == 0
2949 && (*string == '.') == (cpu_arch[j].type == PROCESSOR_NONE))
2950 {
2951 if (*string != '.')
2952 {
2953 check_cpu_arch_compatible (string, cpu_arch[j].enable);
2954
2955 if (flag_code == CODE_64BIT && !cpu_arch[j].enable.bitfield.cpu64 )
2956 {
2957 as_bad (_("64bit mode not supported on `%s'."),
2958 cpu_arch[j].name);
2959 (void) restore_line_pointer (e);
2960 ignore_rest_of_line ();
2961 return;
2962 }
2963
2964 if (flag_code == CODE_32BIT && !cpu_arch[j].enable.bitfield.cpui386)
2965 {
2966 as_bad (_("32bit mode not supported on `%s'."),
2967 cpu_arch[j].name);
2968 (void) restore_line_pointer (e);
2969 ignore_rest_of_line ();
2970 return;
2971 }
2972
2973 cpu_arch_name = cpu_arch[j].name;
2974 free (cpu_sub_arch_name);
2975 cpu_sub_arch_name = NULL;
2976 cpu_arch_flags = cpu_arch[j].enable;
2977 cpu_arch_isa = cpu_arch[j].type;
2978 cpu_arch_isa_flags = cpu_arch[j].enable;
2979 if (!cpu_arch_tune_set)
2980 {
2981 cpu_arch_tune = cpu_arch_isa;
2982 cpu_arch_tune_flags = cpu_arch_isa_flags;
2983 }
2984
2985 vector_size = VSZ_DEFAULT;
2986
2987 pre_386_16bit_warned = false;
2988 break;
2989 }
2990
2991 if (cpu_flags_all_zero (&cpu_arch[j].enable))
2992 continue;
2993
2994 flags = cpu_flags_or (cpu_arch_flags, cpu_arch[j].enable);
2995
2996 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
2997 {
2998 extend_cpu_sub_arch_name (string + 1);
2999 cpu_arch_flags = flags;
3000 cpu_arch_isa_flags = flags;
3001 }
3002 else
3003 cpu_arch_isa_flags
3004 = cpu_flags_or (cpu_arch_isa_flags, cpu_arch[j].enable);
3005
3006 (void) restore_line_pointer (e);
3007
3008 switch (cpu_arch[j].vsz)
3009 {
3010 default:
3011 break;
3012
3013 case vsz_set:
3014 #ifdef SVR4_COMMENT_CHARS
3015 if (*input_line_pointer == ':' || *input_line_pointer == '/')
3016 #else
3017 if (*input_line_pointer == '/')
3018 #endif
3019 {
3020 ++input_line_pointer;
3021 switch (get_absolute_expression ())
3022 {
3023 case 512: vector_size = VSZ512; break;
3024 case 256: vector_size = VSZ256; break;
3025 case 128: vector_size = VSZ128; break;
3026 default:
3027 as_bad (_("Unrecognized vector size specifier"));
3028 ignore_rest_of_line ();
3029 return;
3030 }
3031 break;
3032 }
3033 /* Fall through. */
3034 case vsz_reset:
3035 vector_size = VSZ_DEFAULT;
3036 break;
3037 }
3038
3039 demand_empty_rest_of_line ();
3040 return;
3041 }
3042 }
3043
3044 if (startswith (string, ".no") && j >= ARRAY_SIZE (cpu_arch))
3045 {
3046 /* Disable an ISA extension. */
3047 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
3048 if (cpu_arch[j].type == PROCESSOR_NONE
3049 && strcmp (string + 3, cpu_arch[j].name) == 0)
3050 {
3051 flags = cpu_flags_and_not (cpu_arch_flags, cpu_arch[j].disable);
3052 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
3053 {
3054 extend_cpu_sub_arch_name (string + 1);
3055 cpu_arch_flags = flags;
3056 cpu_arch_isa_flags = flags;
3057 }
3058
3059 if (cpu_arch[j].vsz == vsz_set)
3060 vector_size = VSZ_DEFAULT;
3061
3062 (void) restore_line_pointer (e);
3063 demand_empty_rest_of_line ();
3064 return;
3065 }
3066 }
3067
3068 if (j == ARRAY_SIZE (cpu_arch))
3069 as_bad (_("no such architecture: `%s'"), string);
3070
3071 *input_line_pointer = e;
3072
3073 no_cond_jump_promotion = 0;
3074 if (*input_line_pointer == ','
3075 && !is_end_of_line[(unsigned char) input_line_pointer[1]])
3076 {
3077 ++input_line_pointer;
3078 e = get_symbol_name (&s);
3079 string = s;
3080
3081 if (strcmp (string, "nojumps") == 0)
3082 no_cond_jump_promotion = 1;
3083 else if (strcmp (string, "jumps") == 0)
3084 ;
3085 else
3086 as_bad (_("no such architecture modifier: `%s'"), string);
3087
3088 (void) restore_line_pointer (e);
3089 }
3090
3091 demand_empty_rest_of_line ();
3092 }
3093
3094 enum bfd_architecture
3095 i386_arch (void)
3096 {
3097 if (cpu_arch_isa == PROCESSOR_IAMCU)
3098 {
3099 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
3100 || flag_code == CODE_64BIT)
3101 as_fatal (_("Intel MCU is 32bit ELF only"));
3102 return bfd_arch_iamcu;
3103 }
3104 else
3105 return bfd_arch_i386;
3106 }
3107
3108 unsigned long
3109 i386_mach (void)
3110 {
3111 if (startswith (default_arch, "x86_64"))
3112 {
3113 if (default_arch[6] == '\0')
3114 return bfd_mach_x86_64;
3115 else
3116 return bfd_mach_x64_32;
3117 }
3118 else if (!strcmp (default_arch, "i386")
3119 || !strcmp (default_arch, "iamcu"))
3120 {
3121 if (cpu_arch_isa == PROCESSOR_IAMCU)
3122 {
3123 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
3124 as_fatal (_("Intel MCU is 32bit ELF only"));
3125 return bfd_mach_i386_iamcu;
3126 }
3127 else
3128 return bfd_mach_i386_i386;
3129 }
3130 else
3131 as_fatal (_("unknown architecture"));
3132 }
3133 \f
3134 #include "opcodes/i386-tbl.h"
3135
3136 void
3137 md_begin (void)
3138 {
3139 /* Support pseudo prefixes like {disp32}. */
3140 lex_type ['{'] = LEX_BEGIN_NAME;
3141
3142 /* Initialize op_hash hash table. */
3143 op_hash = str_htab_create ();
3144
3145 {
3146 const insn_template *const *sets = i386_op_sets;
3147 const insn_template *const *end = sets + ARRAY_SIZE (i386_op_sets) - 1;
3148
3149 /* Type checks to compensate for the conversion through void * which
3150 occurs during hash table insertion / lookup. */
3151 (void) sizeof (sets == &current_templates->start);
3152 (void) sizeof (end == &current_templates->end);
3153 for (; sets < end; ++sets)
3154 if (str_hash_insert (op_hash, insn_name (*sets), sets, 0))
3155 as_fatal (_("duplicate %s"), insn_name (*sets));
3156 }
3157
3158 /* Initialize reg_hash hash table. */
3159 reg_hash = str_htab_create ();
3160 {
3161 const reg_entry *regtab;
3162 unsigned int regtab_size = i386_regtab_size;
3163
3164 for (regtab = i386_regtab; regtab_size--; regtab++)
3165 {
3166 switch (regtab->reg_type.bitfield.class)
3167 {
3168 case Reg:
3169 if (regtab->reg_type.bitfield.dword)
3170 {
3171 if (regtab->reg_type.bitfield.instance == Accum)
3172 reg_eax = regtab;
3173 }
3174 else if (regtab->reg_type.bitfield.tbyte)
3175 {
3176 /* There's no point inserting st(<N>) in the hash table, as
3177 parentheses aren't included in register_chars[] anyway. */
3178 if (regtab->reg_type.bitfield.instance != Accum)
3179 continue;
3180 reg_st0 = regtab;
3181 }
3182 break;
3183
3184 case SReg:
3185 switch (regtab->reg_num)
3186 {
3187 case 0: reg_es = regtab; break;
3188 case 2: reg_ss = regtab; break;
3189 case 3: reg_ds = regtab; break;
3190 }
3191 break;
3192
3193 case RegMask:
3194 if (!regtab->reg_num)
3195 reg_k0 = regtab;
3196 break;
3197 }
3198
3199 if (str_hash_insert (reg_hash, regtab->reg_name, regtab, 0) != NULL)
3200 as_fatal (_("duplicate %s"), regtab->reg_name);
3201 }
3202 }
3203
3204 /* Fill in lexical tables: mnemonic_chars, operand_chars. */
3205 {
3206 int c;
3207 const char *p;
3208
3209 for (c = 0; c < 256; c++)
3210 {
3211 if (ISDIGIT (c) || ISLOWER (c))
3212 {
3213 mnemonic_chars[c] = c;
3214 register_chars[c] = c;
3215 operand_chars[c] = c;
3216 }
3217 else if (ISUPPER (c))
3218 {
3219 mnemonic_chars[c] = TOLOWER (c);
3220 register_chars[c] = mnemonic_chars[c];
3221 operand_chars[c] = c;
3222 }
3223 #ifdef SVR4_COMMENT_CHARS
3224 else if (c == '\\' && strchr (i386_comment_chars, '/'))
3225 operand_chars[c] = c;
3226 #endif
3227
3228 if (c >= 128)
3229 operand_chars[c] = c;
3230 }
3231
3232 mnemonic_chars['_'] = '_';
3233 mnemonic_chars['-'] = '-';
3234 mnemonic_chars['.'] = '.';
3235
3236 for (p = extra_symbol_chars; *p != '\0'; p++)
3237 operand_chars[(unsigned char) *p] = *p;
3238 for (p = operand_special_chars; *p != '\0'; p++)
3239 operand_chars[(unsigned char) *p] = *p;
3240 }
3241
3242 if (flag_code == CODE_64BIT)
3243 {
3244 #if defined (OBJ_COFF) && defined (TE_PE)
3245 x86_dwarf2_return_column = (OUTPUT_FLAVOR == bfd_target_coff_flavour
3246 ? 32 : 16);
3247 #else
3248 x86_dwarf2_return_column = 16;
3249 #endif
3250 x86_cie_data_alignment = -8;
3251 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3252 x86_sframe_cfa_sp_reg = 7;
3253 x86_sframe_cfa_fp_reg = 6;
3254 #endif
3255 }
3256 else
3257 {
3258 x86_dwarf2_return_column = 8;
3259 x86_cie_data_alignment = -4;
3260 }
3261
3262 /* NB: FUSED_JCC_PADDING frag must have sufficient room so that it
3263 can be turned into BRANCH_PREFIX frag. */
3264 if (align_branch_prefix_size > MAX_FUSED_JCC_PADDING_SIZE)
3265 abort ();
3266 }
3267
3268 void
3269 i386_print_statistics (FILE *file)
3270 {
3271 htab_print_statistics (file, "i386 opcode", op_hash);
3272 htab_print_statistics (file, "i386 register", reg_hash);
3273 }
3274
3275 void
3276 i386_md_end (void)
3277 {
3278 htab_delete (op_hash);
3279 htab_delete (reg_hash);
3280 }
3281 \f
3282 #ifdef DEBUG386
3283
3284 /* Debugging routines for md_assemble. */
3285 static void pte (insn_template *);
3286 static void pt (i386_operand_type);
3287 static void pe (expressionS *);
3288 static void ps (symbolS *);
3289
3290 static void
3291 pi (const char *line, i386_insn *x)
3292 {
3293 unsigned int j;
3294
3295 fprintf (stdout, "%s: template ", line);
3296 pte (&x->tm);
3297 fprintf (stdout, " address: base %s index %s scale %x\n",
3298 x->base_reg ? x->base_reg->reg_name : "none",
3299 x->index_reg ? x->index_reg->reg_name : "none",
3300 x->log2_scale_factor);
3301 fprintf (stdout, " modrm: mode %x reg %x reg/mem %x\n",
3302 x->rm.mode, x->rm.reg, x->rm.regmem);
3303 fprintf (stdout, " sib: base %x index %x scale %x\n",
3304 x->sib.base, x->sib.index, x->sib.scale);
3305 fprintf (stdout, " rex: 64bit %x extX %x extY %x extZ %x\n",
3306 (x->rex & REX_W) != 0,
3307 (x->rex & REX_R) != 0,
3308 (x->rex & REX_X) != 0,
3309 (x->rex & REX_B) != 0);
3310 for (j = 0; j < x->operands; j++)
3311 {
3312 fprintf (stdout, " #%d: ", j + 1);
3313 pt (x->types[j]);
3314 fprintf (stdout, "\n");
3315 if (x->types[j].bitfield.class == Reg
3316 || x->types[j].bitfield.class == RegMMX
3317 || x->types[j].bitfield.class == RegSIMD
3318 || x->types[j].bitfield.class == RegMask
3319 || x->types[j].bitfield.class == SReg
3320 || x->types[j].bitfield.class == RegCR
3321 || x->types[j].bitfield.class == RegDR
3322 || x->types[j].bitfield.class == RegTR
3323 || x->types[j].bitfield.class == RegBND)
3324 fprintf (stdout, "%s\n", x->op[j].regs->reg_name);
3325 if (operand_type_check (x->types[j], imm))
3326 pe (x->op[j].imms);
3327 if (operand_type_check (x->types[j], disp))
3328 pe (x->op[j].disps);
3329 }
3330 }
3331
3332 static void
3333 pte (insn_template *t)
3334 {
3335 static const unsigned char opc_pfx[] = { 0, 0x66, 0xf3, 0xf2 };
3336 static const char *const opc_spc[] = {
3337 NULL, "0f", "0f38", "0f3a", NULL, "evexmap5", "evexmap6", NULL,
3338 "XOP08", "XOP09", "XOP0A",
3339 };
3340 unsigned int j;
3341
3342 fprintf (stdout, " %d operands ", t->operands);
3343 if (opc_pfx[t->opcode_modifier.opcodeprefix])
3344 fprintf (stdout, "pfx %x ", opc_pfx[t->opcode_modifier.opcodeprefix]);
3345 if (opc_spc[t->opcode_space])
3346 fprintf (stdout, "space %s ", opc_spc[t->opcode_space]);
3347 fprintf (stdout, "opcode %x ", t->base_opcode);
3348 if (t->extension_opcode != None)
3349 fprintf (stdout, "ext %x ", t->extension_opcode);
3350 if (t->opcode_modifier.d)
3351 fprintf (stdout, "D");
3352 if (t->opcode_modifier.w)
3353 fprintf (stdout, "W");
3354 fprintf (stdout, "\n");
3355 for (j = 0; j < t->operands; j++)
3356 {
3357 fprintf (stdout, " #%d type ", j + 1);
3358 pt (t->operand_types[j]);
3359 fprintf (stdout, "\n");
3360 }
3361 }
3362
3363 static void
3364 pe (expressionS *e)
3365 {
3366 fprintf (stdout, " operation %d\n", e->X_op);
3367 fprintf (stdout, " add_number %" PRId64 " (%" PRIx64 ")\n",
3368 (int64_t) e->X_add_number, (uint64_t) (valueT) e->X_add_number);
3369 if (e->X_add_symbol)
3370 {
3371 fprintf (stdout, " add_symbol ");
3372 ps (e->X_add_symbol);
3373 fprintf (stdout, "\n");
3374 }
3375 if (e->X_op_symbol)
3376 {
3377 fprintf (stdout, " op_symbol ");
3378 ps (e->X_op_symbol);
3379 fprintf (stdout, "\n");
3380 }
3381 }
3382
3383 static void
3384 ps (symbolS *s)
3385 {
3386 fprintf (stdout, "%s type %s%s",
3387 S_GET_NAME (s),
3388 S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
3389 segment_name (S_GET_SEGMENT (s)));
3390 }
3391
3392 static struct type_name
3393 {
3394 i386_operand_type mask;
3395 const char *name;
3396 }
3397 const type_names[] =
3398 {
3399 { { .bitfield = { .class = Reg, .byte = 1 } }, "r8" },
3400 { { .bitfield = { .class = Reg, .word = 1 } }, "r16" },
3401 { { .bitfield = { .class = Reg, .dword = 1 } }, "r32" },
3402 { { .bitfield = { .class = Reg, .qword = 1 } }, "r64" },
3403 { { .bitfield = { .instance = Accum, .byte = 1 } }, "acc8" },
3404 { { .bitfield = { .instance = Accum, .word = 1 } }, "acc16" },
3405 { { .bitfield = { .instance = Accum, .dword = 1 } }, "acc32" },
3406 { { .bitfield = { .instance = Accum, .qword = 1 } }, "acc64" },
3407 { { .bitfield = { .imm8 = 1 } }, "i8" },
3408 { { .bitfield = { .imm8s = 1 } }, "i8s" },
3409 { { .bitfield = { .imm16 = 1 } }, "i16" },
3410 { { .bitfield = { .imm32 = 1 } }, "i32" },
3411 { { .bitfield = { .imm32s = 1 } }, "i32s" },
3412 { { .bitfield = { .imm64 = 1 } }, "i64" },
3413 { { .bitfield = { .imm1 = 1 } }, "i1" },
3414 { { .bitfield = { .baseindex = 1 } }, "BaseIndex" },
3415 { { .bitfield = { .disp8 = 1 } }, "d8" },
3416 { { .bitfield = { .disp16 = 1 } }, "d16" },
3417 { { .bitfield = { .disp32 = 1 } }, "d32" },
3418 { { .bitfield = { .disp64 = 1 } }, "d64" },
3419 { { .bitfield = { .instance = RegD, .word = 1 } }, "InOutPortReg" },
3420 { { .bitfield = { .instance = RegC, .byte = 1 } }, "ShiftCount" },
3421 { { .bitfield = { .class = RegCR } }, "control reg" },
3422 { { .bitfield = { .class = RegTR } }, "test reg" },
3423 { { .bitfield = { .class = RegDR } }, "debug reg" },
3424 { { .bitfield = { .class = Reg, .tbyte = 1 } }, "FReg" },
3425 { { .bitfield = { .instance = Accum, .tbyte = 1 } }, "FAcc" },
3426 { { .bitfield = { .class = SReg } }, "SReg" },
3427 { { .bitfield = { .class = RegMMX } }, "rMMX" },
3428 { { .bitfield = { .class = RegSIMD, .xmmword = 1 } }, "rXMM" },
3429 { { .bitfield = { .class = RegSIMD, .ymmword = 1 } }, "rYMM" },
3430 { { .bitfield = { .class = RegSIMD, .zmmword = 1 } }, "rZMM" },
3431 { { .bitfield = { .class = RegSIMD, .tmmword = 1 } }, "rTMM" },
3432 { { .bitfield = { .class = RegMask } }, "Mask reg" },
3433 };
3434
3435 static void
3436 pt (i386_operand_type t)
3437 {
3438 unsigned int j;
3439 i386_operand_type a;
3440
3441 for (j = 0; j < ARRAY_SIZE (type_names); j++)
3442 {
3443 a = operand_type_and (t, type_names[j].mask);
3444 if (operand_type_equal (&a, &type_names[j].mask))
3445 fprintf (stdout, "%s, ", type_names[j].name);
3446 }
3447 fflush (stdout);
3448 }
3449
3450 #endif /* DEBUG386 */
3451 \f
3452 static bfd_reloc_code_real_type
3453 reloc (unsigned int size,
3454 int pcrel,
3455 int sign,
3456 bfd_reloc_code_real_type other)
3457 {
3458 if (other != NO_RELOC)
3459 {
3460 reloc_howto_type *rel;
3461
3462 if (size == 8)
3463 switch (other)
3464 {
3465 case BFD_RELOC_X86_64_GOT32:
3466 return BFD_RELOC_X86_64_GOT64;
3467 break;
3468 case BFD_RELOC_X86_64_GOTPLT64:
3469 return BFD_RELOC_X86_64_GOTPLT64;
3470 break;
3471 case BFD_RELOC_X86_64_PLTOFF64:
3472 return BFD_RELOC_X86_64_PLTOFF64;
3473 break;
3474 case BFD_RELOC_X86_64_GOTPC32:
3475 other = BFD_RELOC_X86_64_GOTPC64;
3476 break;
3477 case BFD_RELOC_X86_64_GOTPCREL:
3478 other = BFD_RELOC_X86_64_GOTPCREL64;
3479 break;
3480 case BFD_RELOC_X86_64_TPOFF32:
3481 other = BFD_RELOC_X86_64_TPOFF64;
3482 break;
3483 case BFD_RELOC_X86_64_DTPOFF32:
3484 other = BFD_RELOC_X86_64_DTPOFF64;
3485 break;
3486 default:
3487 break;
3488 }
3489
3490 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3491 if (other == BFD_RELOC_SIZE32)
3492 {
3493 if (size == 8)
3494 other = BFD_RELOC_SIZE64;
3495 if (pcrel)
3496 {
3497 as_bad (_("there are no pc-relative size relocations"));
3498 return NO_RELOC;
3499 }
3500 }
3501 #endif
3502
3503 /* Sign-checking 4-byte relocations in 16-/32-bit code is pointless. */
3504 if (size == 4 && (flag_code != CODE_64BIT || disallow_64bit_reloc))
3505 sign = -1;
3506
3507 rel = bfd_reloc_type_lookup (stdoutput, other);
3508 if (!rel)
3509 as_bad (_("unknown relocation (%u)"), other);
3510 else if (size != bfd_get_reloc_size (rel))
3511 as_bad (_("%u-byte relocation cannot be applied to %u-byte field"),
3512 bfd_get_reloc_size (rel),
3513 size);
3514 else if (pcrel && !rel->pc_relative)
3515 as_bad (_("non-pc-relative relocation for pc-relative field"));
3516 else if ((rel->complain_on_overflow == complain_overflow_signed
3517 && !sign)
3518 || (rel->complain_on_overflow == complain_overflow_unsigned
3519 && sign > 0))
3520 as_bad (_("relocated field and relocation type differ in signedness"));
3521 else
3522 return other;
3523 return NO_RELOC;
3524 }
3525
3526 if (pcrel)
3527 {
3528 if (!sign)
3529 as_bad (_("there are no unsigned pc-relative relocations"));
3530 switch (size)
3531 {
3532 case 1: return BFD_RELOC_8_PCREL;
3533 case 2: return BFD_RELOC_16_PCREL;
3534 case 4: return BFD_RELOC_32_PCREL;
3535 case 8: return BFD_RELOC_64_PCREL;
3536 }
3537 as_bad (_("cannot do %u byte pc-relative relocation"), size);
3538 }
3539 else
3540 {
3541 if (sign > 0)
3542 switch (size)
3543 {
3544 case 4: return BFD_RELOC_X86_64_32S;
3545 }
3546 else
3547 switch (size)
3548 {
3549 case 1: return BFD_RELOC_8;
3550 case 2: return BFD_RELOC_16;
3551 case 4: return BFD_RELOC_32;
3552 case 8: return BFD_RELOC_64;
3553 }
3554 as_bad (_("cannot do %s %u byte relocation"),
3555 sign > 0 ? "signed" : "unsigned", size);
3556 }
3557
3558 return NO_RELOC;
3559 }
3560
3561 /* Here we decide which fixups can be adjusted to make them relative to
3562 the beginning of the section instead of the symbol. Basically we need
3563 to make sure that the dynamic relocations are done correctly, so in
3564 some cases we force the original symbol to be used. */
3565
3566 int
3567 tc_i386_fix_adjustable (fixS *fixP ATTRIBUTE_UNUSED)
3568 {
3569 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3570 if (!IS_ELF)
3571 return 1;
3572
3573 /* Don't adjust pc-relative references to merge sections in 64-bit
3574 mode. */
3575 if (use_rela_relocations
3576 && (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0
3577 && fixP->fx_pcrel)
3578 return 0;
3579
3580 /* The x86_64 GOTPCREL are represented as 32bit PCrel relocations
3581 and changed later by validate_fix. */
3582 if (GOT_symbol && fixP->fx_subsy == GOT_symbol
3583 && fixP->fx_r_type == BFD_RELOC_32_PCREL)
3584 return 0;
3585
3586 /* Adjust_reloc_syms doesn't know about the GOT. Need to keep symbol
3587 for size relocations. */
3588 if (fixP->fx_r_type == BFD_RELOC_SIZE32
3589 || fixP->fx_r_type == BFD_RELOC_SIZE64
3590 || fixP->fx_r_type == BFD_RELOC_386_GOTOFF
3591 || fixP->fx_r_type == BFD_RELOC_386_GOT32
3592 || fixP->fx_r_type == BFD_RELOC_386_GOT32X
3593 || fixP->fx_r_type == BFD_RELOC_386_TLS_GD
3594 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDM
3595 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDO_32
3596 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE_32
3597 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE
3598 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTIE
3599 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE_32
3600 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE
3601 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTDESC
3602 || fixP->fx_r_type == BFD_RELOC_386_TLS_DESC_CALL
3603 || fixP->fx_r_type == BFD_RELOC_X86_64_GOT32
3604 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCREL
3605 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCRELX
3606 || fixP->fx_r_type == BFD_RELOC_X86_64_REX_GOTPCRELX
3607 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSGD
3608 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSLD
3609 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF32
3610 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF64
3611 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF
3612 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF32
3613 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF64
3614 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTOFF64
3615 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
3616 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSDESC_CALL
3617 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3618 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3619 return 0;
3620 #endif
3621 return 1;
3622 }
3623
3624 static INLINE bool
3625 want_disp32 (const insn_template *t)
3626 {
3627 return flag_code != CODE_64BIT
3628 || i.prefix[ADDR_PREFIX]
3629 || (t->mnem_off == MN_lea
3630 && (!i.types[1].bitfield.qword
3631 || t->opcode_modifier.size == SIZE32));
3632 }
3633
3634 static int
3635 intel_float_operand (const char *mnemonic)
3636 {
3637 /* Note that the value returned is meaningful only for opcodes with (memory)
3638 operands, hence the code here is free to improperly handle opcodes that
3639 have no operands (for better performance and smaller code). */
3640
3641 if (mnemonic[0] != 'f')
3642 return 0; /* non-math */
3643
3644 switch (mnemonic[1])
3645 {
3646 /* fclex, fdecstp, fdisi, femms, feni, fincstp, finit, fsetpm, and
3647 the fs segment override prefix not currently handled because no
3648 call path can make opcodes without operands get here */
3649 case 'i':
3650 return 2 /* integer op */;
3651 case 'l':
3652 if (mnemonic[2] == 'd' && (mnemonic[3] == 'c' || mnemonic[3] == 'e'))
3653 return 3; /* fldcw/fldenv */
3654 break;
3655 case 'n':
3656 if (mnemonic[2] != 'o' /* fnop */)
3657 return 3; /* non-waiting control op */
3658 break;
3659 case 'r':
3660 if (mnemonic[2] == 's')
3661 return 3; /* frstor/frstpm */
3662 break;
3663 case 's':
3664 if (mnemonic[2] == 'a')
3665 return 3; /* fsave */
3666 if (mnemonic[2] == 't')
3667 {
3668 switch (mnemonic[3])
3669 {
3670 case 'c': /* fstcw */
3671 case 'd': /* fstdw */
3672 case 'e': /* fstenv */
3673 case 's': /* fsts[gw] */
3674 return 3;
3675 }
3676 }
3677 break;
3678 case 'x':
3679 if (mnemonic[2] == 'r' || mnemonic[2] == 's')
3680 return 0; /* fxsave/fxrstor are not really math ops */
3681 break;
3682 }
3683
3684 return 1;
3685 }
3686
3687 static INLINE void
3688 install_template (const insn_template *t)
3689 {
3690 unsigned int l;
3691
3692 i.tm = *t;
3693
3694 /* Dual VEX/EVEX templates need stripping one of the possible variants. */
3695 if (t->opcode_modifier.vex && t->opcode_modifier.evex)
3696 {
3697 if ((is_cpu (t, CpuAVX) || is_cpu (t, CpuAVX2))
3698 && is_cpu (t, CpuAVX512F))
3699 {
3700 if (need_evex_encoding ())
3701 {
3702 i.tm.opcode_modifier.vex = 0;
3703 i.tm.cpu.bitfield.cpuavx = 0;
3704 if (is_cpu (&i.tm, CpuAVX2))
3705 i.tm.cpu.bitfield.isa = 0;
3706 }
3707 else
3708 {
3709 i.tm.opcode_modifier.evex = 0;
3710 i.tm.cpu.bitfield.cpuavx512f = 0;
3711 }
3712 }
3713 }
3714
3715 /* Note that for pseudo prefixes this produces a length of 1. But for them
3716 the length isn't interesting at all. */
3717 for (l = 1; l < 4; ++l)
3718 if (!(t->base_opcode >> (8 * l)))
3719 break;
3720
3721 i.opcode_length = l;
3722 }
3723
3724 /* Build the VEX prefix. */
3725
3726 static void
3727 build_vex_prefix (const insn_template *t)
3728 {
3729 unsigned int register_specifier;
3730 unsigned int vector_length;
3731 unsigned int w;
3732
3733 /* Check register specifier. */
3734 if (i.vex.register_specifier)
3735 {
3736 register_specifier =
3737 ~register_number (i.vex.register_specifier) & 0xf;
3738 gas_assert ((i.vex.register_specifier->reg_flags & RegVRex) == 0);
3739 }
3740 else
3741 register_specifier = 0xf;
3742
3743 /* Use 2-byte VEX prefix by swapping destination and source operand
3744 if there are more than 1 register operand. */
3745 if (i.reg_operands > 1
3746 && i.vec_encoding != vex_encoding_vex3
3747 && i.dir_encoding == dir_encoding_default
3748 && i.operands == i.reg_operands
3749 && operand_type_equal (&i.types[0], &i.types[i.operands - 1])
3750 && i.tm.opcode_space == SPACE_0F
3751 && (i.tm.opcode_modifier.load || i.tm.opcode_modifier.d)
3752 && i.rex == REX_B)
3753 {
3754 unsigned int xchg;
3755
3756 swap_2_operands (0, i.operands - 1);
3757
3758 gas_assert (i.rm.mode == 3);
3759
3760 i.rex = REX_R;
3761 xchg = i.rm.regmem;
3762 i.rm.regmem = i.rm.reg;
3763 i.rm.reg = xchg;
3764
3765 if (i.tm.opcode_modifier.d)
3766 i.tm.base_opcode ^= (i.tm.base_opcode & 0xee) != 0x6e
3767 ? Opcode_ExtD : Opcode_SIMD_IntD;
3768 else /* Use the next insn. */
3769 install_template (&t[1]);
3770 }
3771
3772 /* Use 2-byte VEX prefix by swapping commutative source operands if there
3773 are no memory operands and at least 3 register ones. */
3774 if (i.reg_operands >= 3
3775 && i.vec_encoding != vex_encoding_vex3
3776 && i.reg_operands == i.operands - i.imm_operands
3777 && i.tm.opcode_modifier.vex
3778 && i.tm.opcode_modifier.commutative
3779 && (i.tm.opcode_modifier.sse2avx
3780 || (optimize > 1 && !i.no_optimize))
3781 && i.rex == REX_B
3782 && i.vex.register_specifier
3783 && !(i.vex.register_specifier->reg_flags & RegRex))
3784 {
3785 unsigned int xchg = i.operands - i.reg_operands;
3786
3787 gas_assert (i.tm.opcode_space == SPACE_0F);
3788 gas_assert (!i.tm.opcode_modifier.sae);
3789 gas_assert (operand_type_equal (&i.types[i.operands - 2],
3790 &i.types[i.operands - 3]));
3791 gas_assert (i.rm.mode == 3);
3792
3793 swap_2_operands (xchg, xchg + 1);
3794
3795 i.rex = 0;
3796 xchg = i.rm.regmem | 8;
3797 i.rm.regmem = ~register_specifier & 0xf;
3798 gas_assert (!(i.rm.regmem & 8));
3799 i.vex.register_specifier += xchg - i.rm.regmem;
3800 register_specifier = ~xchg & 0xf;
3801 }
3802
3803 if (i.tm.opcode_modifier.vex == VEXScalar)
3804 vector_length = avxscalar;
3805 else if (i.tm.opcode_modifier.vex == VEX256)
3806 vector_length = 1;
3807 else if (dot_insn () && i.tm.opcode_modifier.vex == VEX128)
3808 vector_length = 0;
3809 else
3810 {
3811 unsigned int op;
3812
3813 /* Determine vector length from the last multi-length vector
3814 operand. */
3815 vector_length = 0;
3816 for (op = t->operands; op--;)
3817 if (t->operand_types[op].bitfield.xmmword
3818 && t->operand_types[op].bitfield.ymmword
3819 && i.types[op].bitfield.ymmword)
3820 {
3821 vector_length = 1;
3822 break;
3823 }
3824 }
3825
3826 /* Check the REX.W bit and VEXW. */
3827 if (i.tm.opcode_modifier.vexw == VEXWIG)
3828 w = (vexwig == vexw1 || (i.rex & REX_W)) ? 1 : 0;
3829 else if (i.tm.opcode_modifier.vexw)
3830 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3831 else
3832 w = (flag_code == CODE_64BIT ? i.rex & REX_W : vexwig == vexw1) ? 1 : 0;
3833
3834 /* Use 2-byte VEX prefix if possible. */
3835 if (w == 0
3836 && i.vec_encoding != vex_encoding_vex3
3837 && i.tm.opcode_space == SPACE_0F
3838 && (i.rex & (REX_W | REX_X | REX_B)) == 0)
3839 {
3840 /* 2-byte VEX prefix. */
3841 unsigned int r;
3842
3843 i.vex.length = 2;
3844 i.vex.bytes[0] = 0xc5;
3845
3846 /* Check the REX.R bit. */
3847 r = (i.rex & REX_R) ? 0 : 1;
3848 i.vex.bytes[1] = (r << 7
3849 | register_specifier << 3
3850 | vector_length << 2
3851 | i.tm.opcode_modifier.opcodeprefix);
3852 }
3853 else
3854 {
3855 /* 3-byte VEX prefix. */
3856 i.vex.length = 3;
3857
3858 switch (i.tm.opcode_space)
3859 {
3860 case SPACE_0F:
3861 case SPACE_0F38:
3862 case SPACE_0F3A:
3863 i.vex.bytes[0] = 0xc4;
3864 break;
3865 case SPACE_XOP08:
3866 case SPACE_XOP09:
3867 case SPACE_XOP0A:
3868 i.vex.bytes[0] = 0x8f;
3869 break;
3870 default:
3871 abort ();
3872 }
3873
3874 /* The high 3 bits of the second VEX byte are 1's compliment
3875 of RXB bits from REX. */
3876 i.vex.bytes[1] = ((~i.rex & 7) << 5)
3877 | (!dot_insn () ? i.tm.opcode_space
3878 : i.insn_opcode_space);
3879
3880 i.vex.bytes[2] = (w << 7
3881 | register_specifier << 3
3882 | vector_length << 2
3883 | i.tm.opcode_modifier.opcodeprefix);
3884 }
3885 }
3886
3887 static INLINE bool
3888 is_evex_encoding (const insn_template *t)
3889 {
3890 return t->opcode_modifier.evex || t->opcode_modifier.disp8memshift
3891 || t->opcode_modifier.broadcast || t->opcode_modifier.masking
3892 || t->opcode_modifier.sae;
3893 }
3894
3895 static INLINE bool
3896 is_any_vex_encoding (const insn_template *t)
3897 {
3898 return t->opcode_modifier.vex || is_evex_encoding (t);
3899 }
3900
3901 static unsigned int
3902 get_broadcast_bytes (const insn_template *t, bool diag)
3903 {
3904 unsigned int op, bytes;
3905 const i386_operand_type *types;
3906
3907 if (i.broadcast.type)
3908 return (1 << (t->opcode_modifier.broadcast - 1)) * i.broadcast.type;
3909
3910 gas_assert (intel_syntax);
3911
3912 for (op = 0; op < t->operands; ++op)
3913 if (t->operand_types[op].bitfield.baseindex)
3914 break;
3915
3916 gas_assert (op < t->operands);
3917
3918 if (t->opcode_modifier.evex
3919 && t->opcode_modifier.evex != EVEXDYN)
3920 switch (i.broadcast.bytes)
3921 {
3922 case 1:
3923 if (t->operand_types[op].bitfield.word)
3924 return 2;
3925 /* Fall through. */
3926 case 2:
3927 if (t->operand_types[op].bitfield.dword)
3928 return 4;
3929 /* Fall through. */
3930 case 4:
3931 if (t->operand_types[op].bitfield.qword)
3932 return 8;
3933 /* Fall through. */
3934 case 8:
3935 if (t->operand_types[op].bitfield.xmmword)
3936 return 16;
3937 if (t->operand_types[op].bitfield.ymmword)
3938 return 32;
3939 if (t->operand_types[op].bitfield.zmmword)
3940 return 64;
3941 /* Fall through. */
3942 default:
3943 abort ();
3944 }
3945
3946 gas_assert (op + 1 < t->operands);
3947
3948 if (t->operand_types[op + 1].bitfield.xmmword
3949 + t->operand_types[op + 1].bitfield.ymmword
3950 + t->operand_types[op + 1].bitfield.zmmword > 1)
3951 {
3952 types = &i.types[op + 1];
3953 diag = false;
3954 }
3955 else /* Ambiguous - guess with a preference to non-AVX512VL forms. */
3956 types = &t->operand_types[op];
3957
3958 if (types->bitfield.zmmword)
3959 bytes = 64;
3960 else if (types->bitfield.ymmword)
3961 bytes = 32;
3962 else
3963 bytes = 16;
3964
3965 if (diag)
3966 as_warn (_("ambiguous broadcast for `%s', using %u-bit form"),
3967 insn_name (t), bytes * 8);
3968
3969 return bytes;
3970 }
3971
3972 /* Build the EVEX prefix. */
3973
3974 static void
3975 build_evex_prefix (void)
3976 {
3977 unsigned int register_specifier, w;
3978 rex_byte vrex_used = 0;
3979
3980 /* Check register specifier. */
3981 if (i.vex.register_specifier)
3982 {
3983 gas_assert ((i.vrex & REX_X) == 0);
3984
3985 register_specifier = i.vex.register_specifier->reg_num;
3986 if ((i.vex.register_specifier->reg_flags & RegRex))
3987 register_specifier += 8;
3988 /* The upper 16 registers are encoded in the fourth byte of the
3989 EVEX prefix. */
3990 if (!(i.vex.register_specifier->reg_flags & RegVRex))
3991 i.vex.bytes[3] = 0x8;
3992 register_specifier = ~register_specifier & 0xf;
3993 }
3994 else
3995 {
3996 register_specifier = 0xf;
3997
3998 /* Encode upper 16 vector index register in the fourth byte of
3999 the EVEX prefix. */
4000 if (!(i.vrex & REX_X))
4001 i.vex.bytes[3] = 0x8;
4002 else
4003 vrex_used |= REX_X;
4004 }
4005
4006 /* 4 byte EVEX prefix. */
4007 i.vex.length = 4;
4008 i.vex.bytes[0] = 0x62;
4009
4010 /* The high 3 bits of the second EVEX byte are 1's compliment of RXB
4011 bits from REX. */
4012 gas_assert (i.tm.opcode_space >= SPACE_0F);
4013 gas_assert (i.tm.opcode_space <= SPACE_EVEXMAP6);
4014 i.vex.bytes[1] = ((~i.rex & 7) << 5)
4015 | (!dot_insn () ? i.tm.opcode_space
4016 : i.insn_opcode_space);
4017
4018 /* The fifth bit of the second EVEX byte is 1's compliment of the
4019 REX_R bit in VREX. */
4020 if (!(i.vrex & REX_R))
4021 i.vex.bytes[1] |= 0x10;
4022 else
4023 vrex_used |= REX_R;
4024
4025 if ((i.reg_operands + i.imm_operands) == i.operands)
4026 {
4027 /* When all operands are registers, the REX_X bit in REX is not
4028 used. We reuse it to encode the upper 16 registers, which is
4029 indicated by the REX_B bit in VREX. The REX_X bit is encoded
4030 as 1's compliment. */
4031 if ((i.vrex & REX_B))
4032 {
4033 vrex_used |= REX_B;
4034 i.vex.bytes[1] &= ~0x40;
4035 }
4036 }
4037
4038 /* EVEX instructions shouldn't need the REX prefix. */
4039 i.vrex &= ~vrex_used;
4040 gas_assert (i.vrex == 0);
4041
4042 /* Check the REX.W bit and VEXW. */
4043 if (i.tm.opcode_modifier.vexw == VEXWIG)
4044 w = (evexwig == evexw1 || (i.rex & REX_W)) ? 1 : 0;
4045 else if (i.tm.opcode_modifier.vexw)
4046 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
4047 else
4048 w = (flag_code == CODE_64BIT ? i.rex & REX_W : evexwig == evexw1) ? 1 : 0;
4049
4050 /* The third byte of the EVEX prefix. */
4051 i.vex.bytes[2] = ((w << 7)
4052 | (register_specifier << 3)
4053 | 4 /* Encode the U bit. */
4054 | i.tm.opcode_modifier.opcodeprefix);
4055
4056 /* The fourth byte of the EVEX prefix. */
4057 /* The zeroing-masking bit. */
4058 if (i.mask.reg && i.mask.zeroing)
4059 i.vex.bytes[3] |= 0x80;
4060
4061 /* Don't always set the broadcast bit if there is no RC. */
4062 if (i.rounding.type == rc_none)
4063 {
4064 /* Encode the vector length. */
4065 unsigned int vec_length;
4066
4067 if (!i.tm.opcode_modifier.evex
4068 || i.tm.opcode_modifier.evex == EVEXDYN)
4069 {
4070 unsigned int op;
4071
4072 /* Determine vector length from the last multi-length vector
4073 operand. */
4074 for (op = i.operands; op--;)
4075 if (i.tm.operand_types[op].bitfield.xmmword
4076 + i.tm.operand_types[op].bitfield.ymmword
4077 + i.tm.operand_types[op].bitfield.zmmword > 1)
4078 {
4079 if (i.types[op].bitfield.zmmword)
4080 {
4081 i.tm.opcode_modifier.evex = EVEX512;
4082 break;
4083 }
4084 else if (i.types[op].bitfield.ymmword)
4085 {
4086 i.tm.opcode_modifier.evex = EVEX256;
4087 break;
4088 }
4089 else if (i.types[op].bitfield.xmmword)
4090 {
4091 i.tm.opcode_modifier.evex = EVEX128;
4092 break;
4093 }
4094 else if ((i.broadcast.type || i.broadcast.bytes)
4095 && op == i.broadcast.operand)
4096 {
4097 switch (get_broadcast_bytes (&i.tm, true))
4098 {
4099 case 64:
4100 i.tm.opcode_modifier.evex = EVEX512;
4101 break;
4102 case 32:
4103 i.tm.opcode_modifier.evex = EVEX256;
4104 break;
4105 case 16:
4106 i.tm.opcode_modifier.evex = EVEX128;
4107 break;
4108 default:
4109 abort ();
4110 }
4111 break;
4112 }
4113 }
4114
4115 if (op >= MAX_OPERANDS)
4116 abort ();
4117 }
4118
4119 switch (i.tm.opcode_modifier.evex)
4120 {
4121 case EVEXLIG: /* LL' is ignored */
4122 vec_length = evexlig << 5;
4123 break;
4124 case EVEX128:
4125 vec_length = 0 << 5;
4126 break;
4127 case EVEX256:
4128 vec_length = 1 << 5;
4129 break;
4130 case EVEX512:
4131 vec_length = 2 << 5;
4132 break;
4133 case EVEX_L3:
4134 if (dot_insn ())
4135 {
4136 vec_length = 3 << 5;
4137 break;
4138 }
4139 /* Fall through. */
4140 default:
4141 abort ();
4142 break;
4143 }
4144 i.vex.bytes[3] |= vec_length;
4145 /* Encode the broadcast bit. */
4146 if (i.broadcast.type || i.broadcast.bytes)
4147 i.vex.bytes[3] |= 0x10;
4148 }
4149 else if (i.rounding.type != saeonly)
4150 i.vex.bytes[3] |= 0x10 | (i.rounding.type << 5);
4151 else
4152 i.vex.bytes[3] |= 0x10 | (evexrcig << 5);
4153
4154 if (i.mask.reg)
4155 i.vex.bytes[3] |= i.mask.reg->reg_num;
4156 }
4157
4158 static void
4159 process_immext (void)
4160 {
4161 expressionS *exp;
4162
4163 /* These AMD 3DNow! and SSE2 instructions have an opcode suffix
4164 which is coded in the same place as an 8-bit immediate field
4165 would be. Here we fake an 8-bit immediate operand from the
4166 opcode suffix stored in tm.extension_opcode.
4167
4168 AVX instructions also use this encoding, for some of
4169 3 argument instructions. */
4170
4171 gas_assert (i.imm_operands <= 1
4172 && (i.operands <= 2
4173 || (is_any_vex_encoding (&i.tm)
4174 && i.operands <= 4)));
4175
4176 exp = &im_expressions[i.imm_operands++];
4177 i.op[i.operands].imms = exp;
4178 i.types[i.operands].bitfield.imm8 = 1;
4179 i.operands++;
4180 exp->X_op = O_constant;
4181 exp->X_add_number = i.tm.extension_opcode;
4182 i.tm.extension_opcode = None;
4183 }
4184
4185
4186 static int
4187 check_hle (void)
4188 {
4189 switch (i.tm.opcode_modifier.prefixok)
4190 {
4191 default:
4192 abort ();
4193 case PrefixLock:
4194 case PrefixNone:
4195 case PrefixNoTrack:
4196 case PrefixRep:
4197 as_bad (_("invalid instruction `%s' after `%s'"),
4198 insn_name (&i.tm), i.hle_prefix);
4199 return 0;
4200 case PrefixHLELock:
4201 if (i.prefix[LOCK_PREFIX])
4202 return 1;
4203 as_bad (_("missing `lock' with `%s'"), i.hle_prefix);
4204 return 0;
4205 case PrefixHLEAny:
4206 return 1;
4207 case PrefixHLERelease:
4208 if (i.prefix[HLE_PREFIX] != XRELEASE_PREFIX_OPCODE)
4209 {
4210 as_bad (_("instruction `%s' after `xacquire' not allowed"),
4211 insn_name (&i.tm));
4212 return 0;
4213 }
4214 if (i.mem_operands == 0 || !(i.flags[i.operands - 1] & Operand_Mem))
4215 {
4216 as_bad (_("memory destination needed for instruction `%s'"
4217 " after `xrelease'"), insn_name (&i.tm));
4218 return 0;
4219 }
4220 return 1;
4221 }
4222 }
4223
4224 /* Encode aligned vector move as unaligned vector move. */
4225
4226 static void
4227 encode_with_unaligned_vector_move (void)
4228 {
4229 switch (i.tm.base_opcode)
4230 {
4231 case 0x28: /* Load instructions. */
4232 case 0x29: /* Store instructions. */
4233 /* movaps/movapd/vmovaps/vmovapd. */
4234 if (i.tm.opcode_space == SPACE_0F
4235 && i.tm.opcode_modifier.opcodeprefix <= PREFIX_0X66)
4236 i.tm.base_opcode = 0x10 | (i.tm.base_opcode & 1);
4237 break;
4238 case 0x6f: /* Load instructions. */
4239 case 0x7f: /* Store instructions. */
4240 /* movdqa/vmovdqa/vmovdqa64/vmovdqa32. */
4241 if (i.tm.opcode_space == SPACE_0F
4242 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0X66)
4243 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
4244 break;
4245 default:
4246 break;
4247 }
4248 }
4249
4250 /* Try the shortest encoding by shortening operand size. */
4251
4252 static void
4253 optimize_encoding (void)
4254 {
4255 unsigned int j;
4256
4257 if (i.tm.mnem_off == MN_lea)
4258 {
4259 /* Optimize: -O:
4260 lea symbol, %rN -> mov $symbol, %rN
4261 lea (%rM), %rN -> mov %rM, %rN
4262 lea (,%rM,1), %rN -> mov %rM, %rN
4263
4264 and in 32-bit mode for 16-bit addressing
4265
4266 lea (%rM), %rN -> movzx %rM, %rN
4267
4268 and in 64-bit mode zap 32-bit addressing in favor of using a
4269 32-bit (or less) destination.
4270 */
4271 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
4272 {
4273 if (!i.op[1].regs->reg_type.bitfield.word)
4274 i.tm.opcode_modifier.size = SIZE32;
4275 i.prefix[ADDR_PREFIX] = 0;
4276 }
4277
4278 if (!i.index_reg && !i.base_reg)
4279 {
4280 /* Handle:
4281 lea symbol, %rN -> mov $symbol, %rN
4282 */
4283 if (flag_code == CODE_64BIT)
4284 {
4285 /* Don't transform a relocation to a 16-bit one. */
4286 if (i.op[0].disps
4287 && i.op[0].disps->X_op != O_constant
4288 && i.op[1].regs->reg_type.bitfield.word)
4289 return;
4290
4291 if (!i.op[1].regs->reg_type.bitfield.qword
4292 || i.tm.opcode_modifier.size == SIZE32)
4293 {
4294 i.tm.base_opcode = 0xb8;
4295 i.tm.opcode_modifier.modrm = 0;
4296 if (!i.op[1].regs->reg_type.bitfield.word)
4297 i.types[0].bitfield.imm32 = 1;
4298 else
4299 {
4300 i.tm.opcode_modifier.size = SIZE16;
4301 i.types[0].bitfield.imm16 = 1;
4302 }
4303 }
4304 else
4305 {
4306 /* Subject to further optimization below. */
4307 i.tm.base_opcode = 0xc7;
4308 i.tm.extension_opcode = 0;
4309 i.types[0].bitfield.imm32s = 1;
4310 i.types[0].bitfield.baseindex = 0;
4311 }
4312 }
4313 /* Outside of 64-bit mode address and operand sizes have to match if
4314 a relocation is involved, as otherwise we wouldn't (currently) or
4315 even couldn't express the relocation correctly. */
4316 else if (i.op[0].disps
4317 && i.op[0].disps->X_op != O_constant
4318 && ((!i.prefix[ADDR_PREFIX])
4319 != (flag_code == CODE_32BIT
4320 ? i.op[1].regs->reg_type.bitfield.dword
4321 : i.op[1].regs->reg_type.bitfield.word)))
4322 return;
4323 /* In 16-bit mode converting LEA with 16-bit addressing and a 32-bit
4324 destination is going to grow encoding size. */
4325 else if (flag_code == CODE_16BIT
4326 && (optimize <= 1 || optimize_for_space)
4327 && !i.prefix[ADDR_PREFIX]
4328 && i.op[1].regs->reg_type.bitfield.dword)
4329 return;
4330 else
4331 {
4332 i.tm.base_opcode = 0xb8;
4333 i.tm.opcode_modifier.modrm = 0;
4334 if (i.op[1].regs->reg_type.bitfield.dword)
4335 i.types[0].bitfield.imm32 = 1;
4336 else
4337 i.types[0].bitfield.imm16 = 1;
4338
4339 if (i.op[0].disps
4340 && i.op[0].disps->X_op == O_constant
4341 && i.op[1].regs->reg_type.bitfield.dword
4342 /* NB: Add () to !i.prefix[ADDR_PREFIX] to silence
4343 GCC 5. */
4344 && (!i.prefix[ADDR_PREFIX]) != (flag_code == CODE_32BIT))
4345 i.op[0].disps->X_add_number &= 0xffff;
4346 }
4347
4348 i.tm.operand_types[0] = i.types[0];
4349 i.imm_operands = 1;
4350 if (!i.op[0].imms)
4351 {
4352 i.op[0].imms = &im_expressions[0];
4353 i.op[0].imms->X_op = O_absent;
4354 }
4355 }
4356 else if (i.op[0].disps
4357 && (i.op[0].disps->X_op != O_constant
4358 || i.op[0].disps->X_add_number))
4359 return;
4360 else
4361 {
4362 /* Handle:
4363 lea (%rM), %rN -> mov %rM, %rN
4364 lea (,%rM,1), %rN -> mov %rM, %rN
4365 lea (%rM), %rN -> movzx %rM, %rN
4366 */
4367 const reg_entry *addr_reg;
4368
4369 if (!i.index_reg && i.base_reg->reg_num != RegIP)
4370 addr_reg = i.base_reg;
4371 else if (!i.base_reg
4372 && i.index_reg->reg_num != RegIZ
4373 && !i.log2_scale_factor)
4374 addr_reg = i.index_reg;
4375 else
4376 return;
4377
4378 if (addr_reg->reg_type.bitfield.word
4379 && i.op[1].regs->reg_type.bitfield.dword)
4380 {
4381 if (flag_code != CODE_32BIT)
4382 return;
4383 i.tm.opcode_space = SPACE_0F;
4384 i.tm.base_opcode = 0xb7;
4385 }
4386 else
4387 i.tm.base_opcode = 0x8b;
4388
4389 if (addr_reg->reg_type.bitfield.dword
4390 && i.op[1].regs->reg_type.bitfield.qword)
4391 i.tm.opcode_modifier.size = SIZE32;
4392
4393 i.op[0].regs = addr_reg;
4394 i.reg_operands = 2;
4395 }
4396
4397 i.mem_operands = 0;
4398 i.disp_operands = 0;
4399 i.prefix[ADDR_PREFIX] = 0;
4400 i.prefix[SEG_PREFIX] = 0;
4401 i.seg[0] = NULL;
4402 }
4403
4404 if (optimize_for_space
4405 && i.tm.mnem_off == MN_test
4406 && i.reg_operands == 1
4407 && i.imm_operands == 1
4408 && !i.types[1].bitfield.byte
4409 && i.op[0].imms->X_op == O_constant
4410 && fits_in_imm7 (i.op[0].imms->X_add_number))
4411 {
4412 /* Optimize: -Os:
4413 test $imm7, %r64/%r32/%r16 -> test $imm7, %r8
4414 */
4415 unsigned int base_regnum = i.op[1].regs->reg_num;
4416 if (flag_code == CODE_64BIT || base_regnum < 4)
4417 {
4418 i.types[1].bitfield.byte = 1;
4419 /* Ignore the suffix. */
4420 i.suffix = 0;
4421 /* Convert to byte registers. */
4422 if (i.types[1].bitfield.word)
4423 j = 16;
4424 else if (i.types[1].bitfield.dword)
4425 j = 32;
4426 else
4427 j = 48;
4428 if (!(i.op[1].regs->reg_flags & RegRex) && base_regnum < 4)
4429 j += 8;
4430 i.op[1].regs -= j;
4431 }
4432 }
4433 else if (flag_code == CODE_64BIT
4434 && i.tm.opcode_space == SPACE_BASE
4435 && ((i.types[1].bitfield.qword
4436 && i.reg_operands == 1
4437 && i.imm_operands == 1
4438 && i.op[0].imms->X_op == O_constant
4439 && ((i.tm.base_opcode == 0xb8
4440 && i.tm.extension_opcode == None
4441 && fits_in_unsigned_long (i.op[0].imms->X_add_number))
4442 || (fits_in_imm31 (i.op[0].imms->X_add_number)
4443 && (i.tm.base_opcode == 0x24
4444 || (i.tm.base_opcode == 0x80
4445 && i.tm.extension_opcode == 0x4)
4446 || i.tm.mnem_off == MN_test
4447 || ((i.tm.base_opcode | 1) == 0xc7
4448 && i.tm.extension_opcode == 0x0)))
4449 || (fits_in_imm7 (i.op[0].imms->X_add_number)
4450 && i.tm.base_opcode == 0x83
4451 && i.tm.extension_opcode == 0x4)))
4452 || (i.types[0].bitfield.qword
4453 && ((i.reg_operands == 2
4454 && i.op[0].regs == i.op[1].regs
4455 && (i.tm.mnem_off == MN_xor
4456 || i.tm.mnem_off == MN_sub))
4457 || i.tm.mnem_off == MN_clr))))
4458 {
4459 /* Optimize: -O:
4460 andq $imm31, %r64 -> andl $imm31, %r32
4461 andq $imm7, %r64 -> andl $imm7, %r32
4462 testq $imm31, %r64 -> testl $imm31, %r32
4463 xorq %r64, %r64 -> xorl %r32, %r32
4464 subq %r64, %r64 -> subl %r32, %r32
4465 movq $imm31, %r64 -> movl $imm31, %r32
4466 movq $imm32, %r64 -> movl $imm32, %r32
4467 */
4468 i.tm.opcode_modifier.size = SIZE32;
4469 if (i.imm_operands)
4470 {
4471 i.types[0].bitfield.imm32 = 1;
4472 i.types[0].bitfield.imm32s = 0;
4473 i.types[0].bitfield.imm64 = 0;
4474 }
4475 else
4476 {
4477 i.types[0].bitfield.dword = 1;
4478 i.types[0].bitfield.qword = 0;
4479 }
4480 i.types[1].bitfield.dword = 1;
4481 i.types[1].bitfield.qword = 0;
4482 if (i.tm.mnem_off == MN_mov || i.tm.mnem_off == MN_lea)
4483 {
4484 /* Handle
4485 movq $imm31, %r64 -> movl $imm31, %r32
4486 movq $imm32, %r64 -> movl $imm32, %r32
4487 */
4488 i.tm.operand_types[0].bitfield.imm32 = 1;
4489 i.tm.operand_types[0].bitfield.imm32s = 0;
4490 i.tm.operand_types[0].bitfield.imm64 = 0;
4491 if ((i.tm.base_opcode | 1) == 0xc7)
4492 {
4493 /* Handle
4494 movq $imm31, %r64 -> movl $imm31, %r32
4495 */
4496 i.tm.base_opcode = 0xb8;
4497 i.tm.extension_opcode = None;
4498 i.tm.opcode_modifier.w = 0;
4499 i.tm.opcode_modifier.modrm = 0;
4500 }
4501 }
4502 }
4503 else if (optimize > 1
4504 && !optimize_for_space
4505 && i.reg_operands == 2
4506 && i.op[0].regs == i.op[1].regs
4507 && (i.tm.mnem_off == MN_and || i.tm.mnem_off == MN_or)
4508 && (flag_code != CODE_64BIT || !i.types[0].bitfield.dword))
4509 {
4510 /* Optimize: -O2:
4511 andb %rN, %rN -> testb %rN, %rN
4512 andw %rN, %rN -> testw %rN, %rN
4513 andq %rN, %rN -> testq %rN, %rN
4514 orb %rN, %rN -> testb %rN, %rN
4515 orw %rN, %rN -> testw %rN, %rN
4516 orq %rN, %rN -> testq %rN, %rN
4517
4518 and outside of 64-bit mode
4519
4520 andl %rN, %rN -> testl %rN, %rN
4521 orl %rN, %rN -> testl %rN, %rN
4522 */
4523 i.tm.base_opcode = 0x84 | (i.tm.base_opcode & 1);
4524 }
4525 else if (i.tm.base_opcode == 0xba
4526 && i.tm.opcode_space == SPACE_0F
4527 && i.reg_operands == 1
4528 && i.op[0].imms->X_op == O_constant
4529 && i.op[0].imms->X_add_number >= 0)
4530 {
4531 /* Optimize: -O:
4532 btw $n, %rN -> btl $n, %rN (outside of 16-bit mode, n < 16)
4533 btq $n, %rN -> btl $n, %rN (in 64-bit mode, n < 32, N < 8)
4534 btl $n, %rN -> btw $n, %rN (in 16-bit mode, n < 16)
4535
4536 With <BT> one of bts, btr, and bts also:
4537 <BT>w $n, %rN -> btl $n, %rN (in 32-bit mode, n < 16)
4538 <BT>l $n, %rN -> btw $n, %rN (in 16-bit mode, n < 16)
4539 */
4540 switch (flag_code)
4541 {
4542 case CODE_64BIT:
4543 if (i.tm.extension_opcode != 4)
4544 break;
4545 if (i.types[1].bitfield.qword
4546 && i.op[0].imms->X_add_number < 32
4547 && !(i.op[1].regs->reg_flags & RegRex))
4548 i.tm.opcode_modifier.size = SIZE32;
4549 /* Fall through. */
4550 case CODE_32BIT:
4551 if (i.types[1].bitfield.word
4552 && i.op[0].imms->X_add_number < 16)
4553 i.tm.opcode_modifier.size = SIZE32;
4554 break;
4555 case CODE_16BIT:
4556 if (i.op[0].imms->X_add_number < 16)
4557 i.tm.opcode_modifier.size = SIZE16;
4558 break;
4559 }
4560 }
4561 else if (i.reg_operands == 3
4562 && i.op[0].regs == i.op[1].regs
4563 && !i.types[2].bitfield.xmmword
4564 && (i.tm.opcode_modifier.vex
4565 || ((!i.mask.reg || i.mask.zeroing)
4566 && is_evex_encoding (&i.tm)
4567 && (i.vec_encoding != vex_encoding_evex
4568 || cpu_arch_isa_flags.bitfield.cpuavx512vl
4569 || is_cpu (&i.tm, CpuAVX512VL)
4570 || (i.tm.operand_types[2].bitfield.zmmword
4571 && i.types[2].bitfield.ymmword))))
4572 && i.tm.opcode_space == SPACE_0F
4573 && ((i.tm.base_opcode | 2) == 0x57
4574 || i.tm.base_opcode == 0xdf
4575 || i.tm.base_opcode == 0xef
4576 || (i.tm.base_opcode | 3) == 0xfb
4577 || i.tm.base_opcode == 0x42
4578 || i.tm.base_opcode == 0x47))
4579 {
4580 /* Optimize: -O1:
4581 VOP, one of vandnps, vandnpd, vxorps, vxorpd, vpsubb, vpsubd,
4582 vpsubq and vpsubw:
4583 EVEX VOP %zmmM, %zmmM, %zmmN
4584 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
4585 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4586 EVEX VOP %ymmM, %ymmM, %ymmN
4587 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
4588 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4589 VEX VOP %ymmM, %ymmM, %ymmN
4590 -> VEX VOP %xmmM, %xmmM, %xmmN
4591 VOP, one of vpandn and vpxor:
4592 VEX VOP %ymmM, %ymmM, %ymmN
4593 -> VEX VOP %xmmM, %xmmM, %xmmN
4594 VOP, one of vpandnd and vpandnq:
4595 EVEX VOP %zmmM, %zmmM, %zmmN
4596 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
4597 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4598 EVEX VOP %ymmM, %ymmM, %ymmN
4599 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
4600 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4601 VOP, one of vpxord and vpxorq:
4602 EVEX VOP %zmmM, %zmmM, %zmmN
4603 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
4604 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4605 EVEX VOP %ymmM, %ymmM, %ymmN
4606 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
4607 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4608 VOP, one of kxord and kxorq:
4609 VEX VOP %kM, %kM, %kN
4610 -> VEX kxorw %kM, %kM, %kN
4611 VOP, one of kandnd and kandnq:
4612 VEX VOP %kM, %kM, %kN
4613 -> VEX kandnw %kM, %kM, %kN
4614 */
4615 if (is_evex_encoding (&i.tm))
4616 {
4617 if (i.vec_encoding != vex_encoding_evex)
4618 {
4619 i.tm.opcode_modifier.vex = VEX128;
4620 i.tm.opcode_modifier.vexw = VEXW0;
4621 i.tm.opcode_modifier.evex = 0;
4622 i.vec_encoding = vex_encoding_vex;
4623 i.mask.reg = NULL;
4624 }
4625 else if (optimize > 1)
4626 i.tm.opcode_modifier.evex = EVEX128;
4627 else
4628 return;
4629 }
4630 else if (i.tm.operand_types[0].bitfield.class == RegMask)
4631 {
4632 i.tm.opcode_modifier.opcodeprefix = PREFIX_NONE;
4633 i.tm.opcode_modifier.vexw = VEXW0;
4634 }
4635 else
4636 i.tm.opcode_modifier.vex = VEX128;
4637
4638 if (i.tm.opcode_modifier.vex)
4639 for (j = 0; j < 3; j++)
4640 {
4641 i.types[j].bitfield.xmmword = 1;
4642 i.types[j].bitfield.ymmword = 0;
4643 }
4644 }
4645 else if (i.vec_encoding != vex_encoding_evex
4646 && !i.types[0].bitfield.zmmword
4647 && !i.types[1].bitfield.zmmword
4648 && !i.mask.reg
4649 && !i.broadcast.type
4650 && !i.broadcast.bytes
4651 && is_evex_encoding (&i.tm)
4652 && ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4653 || (i.tm.base_opcode & ~4) == 0xdb
4654 || (i.tm.base_opcode & ~4) == 0xeb)
4655 && i.tm.extension_opcode == None)
4656 {
4657 /* Optimize: -O1:
4658 VOP, one of vmovdqa32, vmovdqa64, vmovdqu8, vmovdqu16,
4659 vmovdqu32 and vmovdqu64:
4660 EVEX VOP %xmmM, %xmmN
4661 -> VEX vmovdqa|vmovdqu %xmmM, %xmmN (M and N < 16)
4662 EVEX VOP %ymmM, %ymmN
4663 -> VEX vmovdqa|vmovdqu %ymmM, %ymmN (M and N < 16)
4664 EVEX VOP %xmmM, mem
4665 -> VEX vmovdqa|vmovdqu %xmmM, mem (M < 16)
4666 EVEX VOP %ymmM, mem
4667 -> VEX vmovdqa|vmovdqu %ymmM, mem (M < 16)
4668 EVEX VOP mem, %xmmN
4669 -> VEX mvmovdqa|vmovdquem, %xmmN (N < 16)
4670 EVEX VOP mem, %ymmN
4671 -> VEX vmovdqa|vmovdqu mem, %ymmN (N < 16)
4672 VOP, one of vpand, vpandn, vpor, vpxor:
4673 EVEX VOP{d,q} %xmmL, %xmmM, %xmmN
4674 -> VEX VOP %xmmL, %xmmM, %xmmN (L, M, and N < 16)
4675 EVEX VOP{d,q} %ymmL, %ymmM, %ymmN
4676 -> VEX VOP %ymmL, %ymmM, %ymmN (L, M, and N < 16)
4677 EVEX VOP{d,q} mem, %xmmM, %xmmN
4678 -> VEX VOP mem, %xmmM, %xmmN (M and N < 16)
4679 EVEX VOP{d,q} mem, %ymmM, %ymmN
4680 -> VEX VOP mem, %ymmM, %ymmN (M and N < 16)
4681 */
4682 for (j = 0; j < i.operands; j++)
4683 if (operand_type_check (i.types[j], disp)
4684 && i.op[j].disps->X_op == O_constant)
4685 {
4686 /* Since the VEX prefix has 2 or 3 bytes, the EVEX prefix
4687 has 4 bytes, EVEX Disp8 has 1 byte and VEX Disp32 has 4
4688 bytes, we choose EVEX Disp8 over VEX Disp32. */
4689 int evex_disp8, vex_disp8;
4690 unsigned int memshift = i.memshift;
4691 offsetT n = i.op[j].disps->X_add_number;
4692
4693 evex_disp8 = fits_in_disp8 (n);
4694 i.memshift = 0;
4695 vex_disp8 = fits_in_disp8 (n);
4696 if (evex_disp8 != vex_disp8)
4697 {
4698 i.memshift = memshift;
4699 return;
4700 }
4701
4702 i.types[j].bitfield.disp8 = vex_disp8;
4703 break;
4704 }
4705 if ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4706 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2)
4707 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
4708 i.tm.opcode_modifier.vex
4709 = i.types[0].bitfield.ymmword ? VEX256 : VEX128;
4710 i.tm.opcode_modifier.vexw = VEXW0;
4711 /* VPAND, VPOR, and VPXOR are commutative. */
4712 if (i.reg_operands == 3 && i.tm.base_opcode != 0xdf)
4713 i.tm.opcode_modifier.commutative = 1;
4714 i.tm.opcode_modifier.evex = 0;
4715 i.tm.opcode_modifier.masking = 0;
4716 i.tm.opcode_modifier.broadcast = 0;
4717 i.tm.opcode_modifier.disp8memshift = 0;
4718 i.memshift = 0;
4719 if (j < i.operands)
4720 i.types[j].bitfield.disp8
4721 = fits_in_disp8 (i.op[j].disps->X_add_number);
4722 }
4723 else if (optimize_for_space
4724 && i.tm.base_opcode == 0x29
4725 && i.tm.opcode_space == SPACE_0F38
4726 && i.operands == i.reg_operands
4727 && i.op[0].regs == i.op[1].regs
4728 && (!i.tm.opcode_modifier.vex
4729 || !(i.op[0].regs->reg_flags & RegRex))
4730 && !is_evex_encoding (&i.tm))
4731 {
4732 /* Optimize: -Os:
4733 pcmpeqq %xmmN, %xmmN -> pcmpeqd %xmmN, %xmmN
4734 vpcmpeqq %xmmN, %xmmN, %xmmM -> vpcmpeqd %xmmN, %xmmN, %xmmM (N < 8)
4735 vpcmpeqq %ymmN, %ymmN, %ymmM -> vpcmpeqd %ymmN, %ymmN, %ymmM (N < 8)
4736 */
4737 i.tm.opcode_space = SPACE_0F;
4738 i.tm.base_opcode = 0x76;
4739 }
4740 else if (((i.tm.base_opcode >= 0x64
4741 && i.tm.base_opcode <= 0x66
4742 && i.tm.opcode_space == SPACE_0F)
4743 || (i.tm.base_opcode == 0x37
4744 && i.tm.opcode_space == SPACE_0F38))
4745 && i.operands == i.reg_operands
4746 && i.op[0].regs == i.op[1].regs
4747 && !is_evex_encoding (&i.tm))
4748 {
4749 /* Optimize: -O:
4750 pcmpgt[bwd] %mmN, %mmN -> pxor %mmN, %mmN
4751 pcmpgt[bwdq] %xmmN, %xmmN -> pxor %xmmN, %xmmN
4752 vpcmpgt[bwdq] %xmmN, %xmmN, %xmmM -> vpxor %xmmN, %xmmN, %xmmM (N < 8)
4753 vpcmpgt[bwdq] %xmmN, %xmmN, %xmmM -> vpxor %xmm0, %xmm0, %xmmM (N > 7)
4754 vpcmpgt[bwdq] %ymmN, %ymmN, %ymmM -> vpxor %ymmN, %ymmN, %ymmM (N < 8)
4755 vpcmpgt[bwdq] %ymmN, %ymmN, %ymmM -> vpxor %ymm0, %ymm0, %ymmM (N > 7)
4756 */
4757 i.tm.opcode_space = SPACE_0F;
4758 i.tm.base_opcode = 0xef;
4759 if (i.tm.opcode_modifier.vex && (i.op[0].regs->reg_flags & RegRex))
4760 {
4761 if (i.operands == 2)
4762 {
4763 gas_assert (i.tm.opcode_modifier.sse2avx);
4764
4765 i.operands = 3;
4766 i.reg_operands = 3;
4767 i.tm.operands = 3;
4768
4769 i.op[2].regs = i.op[0].regs;
4770 i.types[2] = i.types[0];
4771 i.flags[2] = i.flags[0];
4772 i.tm.operand_types[2] = i.tm.operand_types[0];
4773
4774 i.tm.opcode_modifier.sse2avx = 0;
4775 }
4776 i.op[0].regs -= i.op[0].regs->reg_num + 8;
4777 i.op[1].regs = i.op[0].regs;
4778 }
4779 }
4780 else if (optimize_for_space
4781 && i.tm.base_opcode == 0x59
4782 && i.tm.opcode_space == SPACE_0F38
4783 && i.operands == i.reg_operands
4784 && i.tm.opcode_modifier.vex
4785 && !(i.op[0].regs->reg_flags & RegRex)
4786 && i.op[0].regs->reg_type.bitfield.xmmword
4787 && i.vec_encoding != vex_encoding_vex3)
4788 {
4789 /* Optimize: -Os:
4790 vpbroadcastq %xmmN, %xmmM -> vpunpcklqdq %xmmN, %xmmN, %xmmM (N < 8)
4791 */
4792 i.tm.opcode_space = SPACE_0F;
4793 i.tm.base_opcode = 0x6c;
4794 i.tm.opcode_modifier.vexvvvv = 1;
4795
4796 ++i.operands;
4797 ++i.reg_operands;
4798 ++i.tm.operands;
4799
4800 i.op[2].regs = i.op[0].regs;
4801 i.types[2] = i.types[0];
4802 i.flags[2] = i.flags[0];
4803 i.tm.operand_types[2] = i.tm.operand_types[0];
4804
4805 swap_2_operands (1, 2);
4806 }
4807 }
4808
4809 /* Return non-zero for load instruction. */
4810
4811 static int
4812 load_insn_p (void)
4813 {
4814 unsigned int dest;
4815 int any_vex_p = is_any_vex_encoding (&i.tm);
4816 unsigned int base_opcode = i.tm.base_opcode | 1;
4817
4818 if (!any_vex_p)
4819 {
4820 /* Anysize insns: lea, invlpg, clflush, prefetch*, bndmk, bndcl, bndcu,
4821 bndcn, bndstx, bndldx, clflushopt, clwb, cldemote. */
4822 if (i.tm.opcode_modifier.operandconstraint == ANY_SIZE)
4823 return 0;
4824
4825 /* pop. */
4826 if (i.tm.mnem_off == MN_pop)
4827 return 1;
4828 }
4829
4830 if (i.tm.opcode_space == SPACE_BASE)
4831 {
4832 /* popf, popa. */
4833 if (i.tm.base_opcode == 0x9d
4834 || i.tm.base_opcode == 0x61)
4835 return 1;
4836
4837 /* movs, cmps, lods, scas. */
4838 if ((i.tm.base_opcode | 0xb) == 0xaf)
4839 return 1;
4840
4841 /* outs, xlatb. */
4842 if (base_opcode == 0x6f
4843 || i.tm.base_opcode == 0xd7)
4844 return 1;
4845 /* NB: For AMD-specific insns with implicit memory operands,
4846 they're intentionally not covered. */
4847 }
4848
4849 /* No memory operand. */
4850 if (!i.mem_operands)
4851 return 0;
4852
4853 if (any_vex_p)
4854 {
4855 if (i.tm.mnem_off == MN_vldmxcsr)
4856 return 1;
4857 }
4858 else if (i.tm.opcode_space == SPACE_BASE)
4859 {
4860 /* test, not, neg, mul, imul, div, idiv. */
4861 if (base_opcode == 0xf7 && i.tm.extension_opcode != 1)
4862 return 1;
4863
4864 /* inc, dec. */
4865 if (base_opcode == 0xff && i.tm.extension_opcode <= 1)
4866 return 1;
4867
4868 /* add, or, adc, sbb, and, sub, xor, cmp. */
4869 if (i.tm.base_opcode >= 0x80 && i.tm.base_opcode <= 0x83)
4870 return 1;
4871
4872 /* rol, ror, rcl, rcr, shl/sal, shr, sar. */
4873 if ((base_opcode == 0xc1 || (base_opcode | 2) == 0xd3)
4874 && i.tm.extension_opcode != 6)
4875 return 1;
4876
4877 /* Check for x87 instructions. */
4878 if ((base_opcode | 6) == 0xdf)
4879 {
4880 /* Skip fst, fstp, fstenv, fstcw. */
4881 if (i.tm.base_opcode == 0xd9
4882 && (i.tm.extension_opcode == 2
4883 || i.tm.extension_opcode == 3
4884 || i.tm.extension_opcode == 6
4885 || i.tm.extension_opcode == 7))
4886 return 0;
4887
4888 /* Skip fisttp, fist, fistp, fstp. */
4889 if (i.tm.base_opcode == 0xdb
4890 && (i.tm.extension_opcode == 1
4891 || i.tm.extension_opcode == 2
4892 || i.tm.extension_opcode == 3
4893 || i.tm.extension_opcode == 7))
4894 return 0;
4895
4896 /* Skip fisttp, fst, fstp, fsave, fstsw. */
4897 if (i.tm.base_opcode == 0xdd
4898 && (i.tm.extension_opcode == 1
4899 || i.tm.extension_opcode == 2
4900 || i.tm.extension_opcode == 3
4901 || i.tm.extension_opcode == 6
4902 || i.tm.extension_opcode == 7))
4903 return 0;
4904
4905 /* Skip fisttp, fist, fistp, fbstp, fistp. */
4906 if (i.tm.base_opcode == 0xdf
4907 && (i.tm.extension_opcode == 1
4908 || i.tm.extension_opcode == 2
4909 || i.tm.extension_opcode == 3
4910 || i.tm.extension_opcode == 6
4911 || i.tm.extension_opcode == 7))
4912 return 0;
4913
4914 return 1;
4915 }
4916 }
4917 else if (i.tm.opcode_space == SPACE_0F)
4918 {
4919 /* bt, bts, btr, btc. */
4920 if (i.tm.base_opcode == 0xba
4921 && (i.tm.extension_opcode | 3) == 7)
4922 return 1;
4923
4924 /* cmpxchg8b, cmpxchg16b, xrstors, vmptrld. */
4925 if (i.tm.base_opcode == 0xc7
4926 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
4927 && (i.tm.extension_opcode == 1 || i.tm.extension_opcode == 3
4928 || i.tm.extension_opcode == 6))
4929 return 1;
4930
4931 /* fxrstor, ldmxcsr, xrstor. */
4932 if (i.tm.base_opcode == 0xae
4933 && (i.tm.extension_opcode == 1
4934 || i.tm.extension_opcode == 2
4935 || i.tm.extension_opcode == 5))
4936 return 1;
4937
4938 /* lgdt, lidt, lmsw. */
4939 if (i.tm.base_opcode == 0x01
4940 && (i.tm.extension_opcode == 2
4941 || i.tm.extension_opcode == 3
4942 || i.tm.extension_opcode == 6))
4943 return 1;
4944 }
4945
4946 dest = i.operands - 1;
4947
4948 /* Check fake imm8 operand and 3 source operands. */
4949 if ((i.tm.opcode_modifier.immext
4950 || i.reg_operands + i.mem_operands == 4)
4951 && i.types[dest].bitfield.imm8)
4952 dest--;
4953
4954 /* add, or, adc, sbb, and, sub, xor, cmp, test, xchg. */
4955 if (i.tm.opcode_space == SPACE_BASE
4956 && ((base_opcode | 0x38) == 0x39
4957 || (base_opcode | 2) == 0x87))
4958 return 1;
4959
4960 if (i.tm.mnem_off == MN_xadd)
4961 return 1;
4962
4963 /* Check for load instruction. */
4964 return (i.types[dest].bitfield.class != ClassNone
4965 || i.types[dest].bitfield.instance == Accum);
4966 }
4967
4968 /* Output lfence, 0xfaee8, after instruction. */
4969
4970 static void
4971 insert_lfence_after (void)
4972 {
4973 if (lfence_after_load && load_insn_p ())
4974 {
4975 /* There are also two REP string instructions that require
4976 special treatment. Specifically, the compare string (CMPS)
4977 and scan string (SCAS) instructions set EFLAGS in a manner
4978 that depends on the data being compared/scanned. When used
4979 with a REP prefix, the number of iterations may therefore
4980 vary depending on this data. If the data is a program secret
4981 chosen by the adversary using an LVI method,
4982 then this data-dependent behavior may leak some aspect
4983 of the secret. */
4984 if (((i.tm.base_opcode | 0x9) == 0xaf)
4985 && i.prefix[REP_PREFIX])
4986 {
4987 as_warn (_("`%s` changes flags which would affect control flow behavior"),
4988 insn_name (&i.tm));
4989 }
4990 char *p = frag_more (3);
4991 *p++ = 0xf;
4992 *p++ = 0xae;
4993 *p = 0xe8;
4994 }
4995 }
4996
4997 /* Output lfence, 0xfaee8, before instruction. */
4998
4999 static void
5000 insert_lfence_before (void)
5001 {
5002 char *p;
5003
5004 if (i.tm.opcode_space != SPACE_BASE)
5005 return;
5006
5007 if (i.tm.base_opcode == 0xff
5008 && (i.tm.extension_opcode == 2 || i.tm.extension_opcode == 4))
5009 {
5010 /* Insert lfence before indirect branch if needed. */
5011
5012 if (lfence_before_indirect_branch == lfence_branch_none)
5013 return;
5014
5015 if (i.operands != 1)
5016 abort ();
5017
5018 if (i.reg_operands == 1)
5019 {
5020 /* Indirect branch via register. Don't insert lfence with
5021 -mlfence-after-load=yes. */
5022 if (lfence_after_load
5023 || lfence_before_indirect_branch == lfence_branch_memory)
5024 return;
5025 }
5026 else if (i.mem_operands == 1
5027 && lfence_before_indirect_branch != lfence_branch_register)
5028 {
5029 as_warn (_("indirect `%s` with memory operand should be avoided"),
5030 insn_name (&i.tm));
5031 return;
5032 }
5033 else
5034 return;
5035
5036 if (last_insn.kind != last_insn_other
5037 && last_insn.seg == now_seg)
5038 {
5039 as_warn_where (last_insn.file, last_insn.line,
5040 _("`%s` skips -mlfence-before-indirect-branch on `%s`"),
5041 last_insn.name, insn_name (&i.tm));
5042 return;
5043 }
5044
5045 p = frag_more (3);
5046 *p++ = 0xf;
5047 *p++ = 0xae;
5048 *p = 0xe8;
5049 return;
5050 }
5051
5052 /* Output or/not/shl and lfence before near ret. */
5053 if (lfence_before_ret != lfence_before_ret_none
5054 && (i.tm.base_opcode | 1) == 0xc3)
5055 {
5056 if (last_insn.kind != last_insn_other
5057 && last_insn.seg == now_seg)
5058 {
5059 as_warn_where (last_insn.file, last_insn.line,
5060 _("`%s` skips -mlfence-before-ret on `%s`"),
5061 last_insn.name, insn_name (&i.tm));
5062 return;
5063 }
5064
5065 /* Near ret ingore operand size override under CPU64. */
5066 char prefix = flag_code == CODE_64BIT
5067 ? 0x48
5068 : i.prefix[DATA_PREFIX] ? 0x66 : 0x0;
5069
5070 if (lfence_before_ret == lfence_before_ret_not)
5071 {
5072 /* not: 0xf71424, may add prefix
5073 for operand size override or 64-bit code. */
5074 p = frag_more ((prefix ? 2 : 0) + 6 + 3);
5075 if (prefix)
5076 *p++ = prefix;
5077 *p++ = 0xf7;
5078 *p++ = 0x14;
5079 *p++ = 0x24;
5080 if (prefix)
5081 *p++ = prefix;
5082 *p++ = 0xf7;
5083 *p++ = 0x14;
5084 *p++ = 0x24;
5085 }
5086 else
5087 {
5088 p = frag_more ((prefix ? 1 : 0) + 4 + 3);
5089 if (prefix)
5090 *p++ = prefix;
5091 if (lfence_before_ret == lfence_before_ret_or)
5092 {
5093 /* or: 0x830c2400, may add prefix
5094 for operand size override or 64-bit code. */
5095 *p++ = 0x83;
5096 *p++ = 0x0c;
5097 }
5098 else
5099 {
5100 /* shl: 0xc1242400, may add prefix
5101 for operand size override or 64-bit code. */
5102 *p++ = 0xc1;
5103 *p++ = 0x24;
5104 }
5105
5106 *p++ = 0x24;
5107 *p++ = 0x0;
5108 }
5109
5110 *p++ = 0xf;
5111 *p++ = 0xae;
5112 *p = 0xe8;
5113 }
5114 }
5115
5116 /* Shared helper for md_assemble() and s_insn(). */
5117 static void init_globals (void)
5118 {
5119 unsigned int j;
5120
5121 memset (&i, '\0', sizeof (i));
5122 i.rounding.type = rc_none;
5123 for (j = 0; j < MAX_OPERANDS; j++)
5124 i.reloc[j] = NO_RELOC;
5125 memset (disp_expressions, '\0', sizeof (disp_expressions));
5126 memset (im_expressions, '\0', sizeof (im_expressions));
5127 save_stack_p = save_stack;
5128 }
5129
5130 /* Helper for md_assemble() to decide whether to prepare for a possible 2nd
5131 parsing pass. Instead of introducing a rarely use new insn attribute this
5132 utilizes a common pattern between affected templates. It is deemed
5133 acceptable that this will lead to unnecessary pass 2 preparations in a
5134 limited set of cases. */
5135 static INLINE bool may_need_pass2 (const insn_template *t)
5136 {
5137 return t->opcode_modifier.sse2avx
5138 /* Note that all SSE2AVX templates have at least one operand. */
5139 ? t->operand_types[t->operands - 1].bitfield.class == RegSIMD
5140 : (t->opcode_space == SPACE_0F
5141 && (t->base_opcode | 1) == 0xbf)
5142 || (t->opcode_space == SPACE_BASE
5143 && t->base_opcode == 0x63);
5144 }
5145
5146 /* This is the guts of the machine-dependent assembler. LINE points to a
5147 machine dependent instruction. This function is supposed to emit
5148 the frags/bytes it assembles to. */
5149
5150 void
5151 md_assemble (char *line)
5152 {
5153 unsigned int j;
5154 char mnemonic[MAX_MNEM_SIZE], mnem_suffix = 0, *copy = NULL;
5155 const char *end, *pass1_mnem = NULL;
5156 enum i386_error pass1_err = 0;
5157 const insn_template *t;
5158
5159 /* Initialize globals. */
5160 current_templates = NULL;
5161 retry:
5162 init_globals ();
5163
5164 /* First parse an instruction mnemonic & call i386_operand for the operands.
5165 We assume that the scrubber has arranged it so that line[0] is the valid
5166 start of a (possibly prefixed) mnemonic. */
5167
5168 end = parse_insn (line, mnemonic, false);
5169 if (end == NULL)
5170 {
5171 if (pass1_mnem != NULL)
5172 goto match_error;
5173 if (i.error != no_error)
5174 {
5175 gas_assert (current_templates != NULL);
5176 if (may_need_pass2 (current_templates->start) && !i.suffix)
5177 goto no_match;
5178 /* No point in trying a 2nd pass - it'll only find the same suffix
5179 again. */
5180 mnem_suffix = i.suffix;
5181 goto match_error;
5182 }
5183 return;
5184 }
5185 t = current_templates->start;
5186 if (may_need_pass2 (t))
5187 {
5188 /* Make a copy of the full line in case we need to retry. */
5189 copy = xstrdup (line);
5190 }
5191 line += end - line;
5192 mnem_suffix = i.suffix;
5193
5194 line = parse_operands (line, mnemonic);
5195 this_operand = -1;
5196 if (line == NULL)
5197 {
5198 free (copy);
5199 return;
5200 }
5201
5202 /* Now we've parsed the mnemonic into a set of templates, and have the
5203 operands at hand. */
5204
5205 /* All Intel opcodes have reversed operands except for "bound", "enter",
5206 "invlpg*", "monitor*", "mwait*", "tpause", "umwait", "pvalidate",
5207 "rmpadjust", "rmpupdate", and "rmpquery". We also don't reverse
5208 intersegment "jmp" and "call" instructions with 2 immediate operands so
5209 that the immediate segment precedes the offset consistently in Intel and
5210 AT&T modes. */
5211 if (intel_syntax
5212 && i.operands > 1
5213 && (t->mnem_off != MN_bound)
5214 && !startswith (mnemonic, "invlpg")
5215 && !startswith (mnemonic, "monitor")
5216 && !startswith (mnemonic, "mwait")
5217 && (t->mnem_off != MN_pvalidate)
5218 && !startswith (mnemonic, "rmp")
5219 && (t->mnem_off != MN_tpause)
5220 && (t->mnem_off != MN_umwait)
5221 && !(i.operands == 2
5222 && operand_type_check (i.types[0], imm)
5223 && operand_type_check (i.types[1], imm)))
5224 swap_operands ();
5225
5226 /* The order of the immediates should be reversed
5227 for 2 immediates extrq and insertq instructions */
5228 if (i.imm_operands == 2
5229 && (t->mnem_off == MN_extrq || t->mnem_off == MN_insertq))
5230 swap_2_operands (0, 1);
5231
5232 if (i.imm_operands)
5233 optimize_imm ();
5234
5235 if (i.disp_operands && !optimize_disp (t))
5236 return;
5237
5238 /* Next, we find a template that matches the given insn,
5239 making sure the overlap of the given operands types is consistent
5240 with the template operand types. */
5241
5242 if (!(t = match_template (mnem_suffix)))
5243 {
5244 const char *err_msg;
5245
5246 if (copy && !mnem_suffix)
5247 {
5248 line = copy;
5249 copy = NULL;
5250 no_match:
5251 pass1_err = i.error;
5252 pass1_mnem = insn_name (current_templates->start);
5253 goto retry;
5254 }
5255
5256 /* If a non-/only-64bit template (group) was found in pass 1, and if
5257 _some_ template (group) was found in pass 2, squash pass 1's
5258 error. */
5259 if (pass1_err == unsupported_64bit)
5260 pass1_mnem = NULL;
5261
5262 match_error:
5263 free (copy);
5264
5265 switch (pass1_mnem ? pass1_err : i.error)
5266 {
5267 default:
5268 abort ();
5269 case operand_size_mismatch:
5270 err_msg = _("operand size mismatch");
5271 break;
5272 case operand_type_mismatch:
5273 err_msg = _("operand type mismatch");
5274 break;
5275 case register_type_mismatch:
5276 err_msg = _("register type mismatch");
5277 break;
5278 case number_of_operands_mismatch:
5279 err_msg = _("number of operands mismatch");
5280 break;
5281 case invalid_instruction_suffix:
5282 err_msg = _("invalid instruction suffix");
5283 break;
5284 case bad_imm4:
5285 err_msg = _("constant doesn't fit in 4 bits");
5286 break;
5287 case unsupported_with_intel_mnemonic:
5288 err_msg = _("unsupported with Intel mnemonic");
5289 break;
5290 case unsupported_syntax:
5291 err_msg = _("unsupported syntax");
5292 break;
5293 case unsupported:
5294 as_bad (_("unsupported instruction `%s'"),
5295 pass1_mnem ? pass1_mnem : insn_name (current_templates->start));
5296 return;
5297 case unsupported_on_arch:
5298 as_bad (_("`%s' is not supported on `%s%s'"),
5299 pass1_mnem ? pass1_mnem : insn_name (current_templates->start),
5300 cpu_arch_name ? cpu_arch_name : default_arch,
5301 cpu_sub_arch_name ? cpu_sub_arch_name : "");
5302 return;
5303 case unsupported_64bit:
5304 if (ISLOWER (mnem_suffix))
5305 {
5306 if (flag_code == CODE_64BIT)
5307 as_bad (_("`%s%c' is not supported in 64-bit mode"),
5308 pass1_mnem ? pass1_mnem : insn_name (current_templates->start),
5309 mnem_suffix);
5310 else
5311 as_bad (_("`%s%c' is only supported in 64-bit mode"),
5312 pass1_mnem ? pass1_mnem : insn_name (current_templates->start),
5313 mnem_suffix);
5314 }
5315 else
5316 {
5317 if (flag_code == CODE_64BIT)
5318 as_bad (_("`%s' is not supported in 64-bit mode"),
5319 pass1_mnem ? pass1_mnem : insn_name (current_templates->start));
5320 else
5321 as_bad (_("`%s' is only supported in 64-bit mode"),
5322 pass1_mnem ? pass1_mnem : insn_name (current_templates->start));
5323 }
5324 return;
5325 case invalid_sib_address:
5326 err_msg = _("invalid SIB address");
5327 break;
5328 case invalid_vsib_address:
5329 err_msg = _("invalid VSIB address");
5330 break;
5331 case invalid_vector_register_set:
5332 err_msg = _("mask, index, and destination registers must be distinct");
5333 break;
5334 case invalid_tmm_register_set:
5335 err_msg = _("all tmm registers must be distinct");
5336 break;
5337 case invalid_dest_and_src_register_set:
5338 err_msg = _("destination and source registers must be distinct");
5339 break;
5340 case unsupported_vector_index_register:
5341 err_msg = _("unsupported vector index register");
5342 break;
5343 case unsupported_broadcast:
5344 err_msg = _("unsupported broadcast");
5345 break;
5346 case broadcast_needed:
5347 err_msg = _("broadcast is needed for operand of such type");
5348 break;
5349 case unsupported_masking:
5350 err_msg = _("unsupported masking");
5351 break;
5352 case mask_not_on_destination:
5353 err_msg = _("mask not on destination operand");
5354 break;
5355 case no_default_mask:
5356 err_msg = _("default mask isn't allowed");
5357 break;
5358 case unsupported_rc_sae:
5359 err_msg = _("unsupported static rounding/sae");
5360 break;
5361 case invalid_register_operand:
5362 err_msg = _("invalid register operand");
5363 break;
5364 }
5365 as_bad (_("%s for `%s'"), err_msg,
5366 pass1_mnem ? pass1_mnem : insn_name (current_templates->start));
5367 return;
5368 }
5369
5370 free (copy);
5371
5372 if (sse_check != check_none
5373 /* The opcode space check isn't strictly needed; it's there only to
5374 bypass the logic below when easily possible. */
5375 && t->opcode_space >= SPACE_0F
5376 && t->opcode_space <= SPACE_0F3A
5377 && !is_cpu (&i.tm, CpuSSE4a)
5378 && !is_any_vex_encoding (t))
5379 {
5380 bool simd = false;
5381
5382 for (j = 0; j < t->operands; ++j)
5383 {
5384 if (t->operand_types[j].bitfield.class == RegMMX)
5385 break;
5386 if (t->operand_types[j].bitfield.class == RegSIMD)
5387 simd = true;
5388 }
5389
5390 if (j >= t->operands && simd)
5391 (sse_check == check_warning
5392 ? as_warn
5393 : as_bad) (_("SSE instruction `%s' is used"), insn_name (&i.tm));
5394 }
5395
5396 if (i.tm.opcode_modifier.fwait)
5397 if (!add_prefix (FWAIT_OPCODE))
5398 return;
5399
5400 /* Check if REP prefix is OK. */
5401 if (i.rep_prefix && i.tm.opcode_modifier.prefixok != PrefixRep)
5402 {
5403 as_bad (_("invalid instruction `%s' after `%s'"),
5404 insn_name (&i.tm), i.rep_prefix);
5405 return;
5406 }
5407
5408 /* Check for lock without a lockable instruction. Destination operand
5409 must be memory unless it is xchg (0x86). */
5410 if (i.prefix[LOCK_PREFIX])
5411 {
5412 if (i.tm.opcode_modifier.prefixok < PrefixLock
5413 || i.mem_operands == 0
5414 || (i.tm.base_opcode != 0x86
5415 && !(i.flags[i.operands - 1] & Operand_Mem)))
5416 {
5417 as_bad (_("expecting lockable instruction after `lock'"));
5418 return;
5419 }
5420
5421 /* Zap the redundant prefix from XCHG when optimizing. */
5422 if (i.tm.base_opcode == 0x86 && optimize && !i.no_optimize)
5423 i.prefix[LOCK_PREFIX] = 0;
5424 }
5425
5426 if (is_any_vex_encoding (&i.tm)
5427 || i.tm.operand_types[i.imm_operands].bitfield.class >= RegMMX
5428 || i.tm.operand_types[i.imm_operands + 1].bitfield.class >= RegMMX)
5429 {
5430 /* Check for data size prefix on VEX/XOP/EVEX encoded and SIMD insns. */
5431 if (i.prefix[DATA_PREFIX])
5432 {
5433 as_bad (_("data size prefix invalid with `%s'"), insn_name (&i.tm));
5434 return;
5435 }
5436
5437 /* Don't allow e.g. KMOV in TLS code sequences. */
5438 for (j = i.imm_operands; j < i.operands; ++j)
5439 switch (i.reloc[j])
5440 {
5441 case BFD_RELOC_386_TLS_GOTIE:
5442 case BFD_RELOC_386_TLS_LE_32:
5443 case BFD_RELOC_X86_64_GOTTPOFF:
5444 case BFD_RELOC_X86_64_TLSLD:
5445 as_bad (_("TLS relocation cannot be used with `%s'"), insn_name (&i.tm));
5446 return;
5447 default:
5448 break;
5449 }
5450 }
5451
5452 /* Check if HLE prefix is OK. */
5453 if (i.hle_prefix && !check_hle ())
5454 return;
5455
5456 /* Check BND prefix. */
5457 if (i.bnd_prefix && !i.tm.opcode_modifier.bndprefixok)
5458 as_bad (_("expecting valid branch instruction after `bnd'"));
5459
5460 /* Check NOTRACK prefix. */
5461 if (i.notrack_prefix && i.tm.opcode_modifier.prefixok != PrefixNoTrack)
5462 as_bad (_("expecting indirect branch instruction after `notrack'"));
5463
5464 if (is_cpu (&i.tm, CpuMPX))
5465 {
5466 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
5467 as_bad (_("32-bit address isn't allowed in 64-bit MPX instructions."));
5468 else if (flag_code != CODE_16BIT
5469 ? i.prefix[ADDR_PREFIX]
5470 : i.mem_operands && !i.prefix[ADDR_PREFIX])
5471 as_bad (_("16-bit address isn't allowed in MPX instructions"));
5472 }
5473
5474 /* Insert BND prefix. */
5475 if (add_bnd_prefix && i.tm.opcode_modifier.bndprefixok)
5476 {
5477 if (!i.prefix[BND_PREFIX])
5478 add_prefix (BND_PREFIX_OPCODE);
5479 else if (i.prefix[BND_PREFIX] != BND_PREFIX_OPCODE)
5480 {
5481 as_warn (_("replacing `rep'/`repe' prefix by `bnd'"));
5482 i.prefix[BND_PREFIX] = BND_PREFIX_OPCODE;
5483 }
5484 }
5485
5486 /* Check string instruction segment overrides. */
5487 if (i.tm.opcode_modifier.isstring >= IS_STRING_ES_OP0)
5488 {
5489 gas_assert (i.mem_operands);
5490 if (!check_string ())
5491 return;
5492 i.disp_operands = 0;
5493 }
5494
5495 /* The memory operand of (%dx) should be only used with input/output
5496 instructions (base opcodes: 0x6c, 0x6e, 0xec, 0xee). */
5497 if (i.input_output_operand
5498 && ((i.tm.base_opcode | 0x82) != 0xee
5499 || i.tm.opcode_space != SPACE_BASE))
5500 {
5501 as_bad (_("input/output port address isn't allowed with `%s'"),
5502 insn_name (&i.tm));
5503 return;
5504 }
5505
5506 if (optimize && !i.no_optimize && i.tm.opcode_modifier.optimize)
5507 optimize_encoding ();
5508
5509 /* Past optimization there's no need to distinguish vex_encoding_evex and
5510 vex_encoding_evex512 anymore. */
5511 if (i.vec_encoding == vex_encoding_evex512)
5512 i.vec_encoding = vex_encoding_evex;
5513
5514 if (use_unaligned_vector_move)
5515 encode_with_unaligned_vector_move ();
5516
5517 if (!process_suffix ())
5518 return;
5519
5520 /* Check if IP-relative addressing requirements can be satisfied. */
5521 if (is_cpu (&i.tm, CpuPREFETCHI)
5522 && !(i.base_reg && i.base_reg->reg_num == RegIP))
5523 as_warn (_("'%s' only supports RIP-relative address"), insn_name (&i.tm));
5524
5525 /* Update operand types and check extended states. */
5526 for (j = 0; j < i.operands; j++)
5527 {
5528 i.types[j] = operand_type_and (i.types[j], i.tm.operand_types[j]);
5529 switch (i.tm.operand_types[j].bitfield.class)
5530 {
5531 default:
5532 break;
5533 case RegMMX:
5534 i.xstate |= xstate_mmx;
5535 break;
5536 case RegMask:
5537 i.xstate |= xstate_mask;
5538 break;
5539 case RegSIMD:
5540 if (i.tm.operand_types[j].bitfield.tmmword)
5541 i.xstate |= xstate_tmm;
5542 else if (i.tm.operand_types[j].bitfield.zmmword
5543 && !i.tm.opcode_modifier.vex
5544 && vector_size >= VSZ512)
5545 i.xstate |= xstate_zmm;
5546 else if (i.tm.operand_types[j].bitfield.ymmword
5547 && vector_size >= VSZ256)
5548 i.xstate |= xstate_ymm;
5549 else if (i.tm.operand_types[j].bitfield.xmmword)
5550 i.xstate |= xstate_xmm;
5551 break;
5552 }
5553 }
5554
5555 /* Make still unresolved immediate matches conform to size of immediate
5556 given in i.suffix. */
5557 if (!finalize_imm ())
5558 return;
5559
5560 if (i.types[0].bitfield.imm1)
5561 i.imm_operands = 0; /* kludge for shift insns. */
5562
5563 /* For insns with operands there are more diddles to do to the opcode. */
5564 if (i.operands)
5565 {
5566 if (!process_operands ())
5567 return;
5568 }
5569 else if (!quiet_warnings && i.tm.opcode_modifier.operandconstraint == UGH)
5570 {
5571 /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc. */
5572 as_warn (_("translating to `%sp'"), insn_name (&i.tm));
5573 }
5574
5575 if (is_any_vex_encoding (&i.tm))
5576 {
5577 if (!cpu_arch_flags.bitfield.cpui286)
5578 {
5579 as_bad (_("instruction `%s' isn't supported outside of protected mode."),
5580 insn_name (&i.tm));
5581 return;
5582 }
5583
5584 /* Check for explicit REX prefix. */
5585 if (i.prefix[REX_PREFIX] || i.rex_encoding)
5586 {
5587 as_bad (_("REX prefix invalid with `%s'"), insn_name (&i.tm));
5588 return;
5589 }
5590
5591 if (i.tm.opcode_modifier.vex)
5592 build_vex_prefix (t);
5593 else
5594 build_evex_prefix ();
5595
5596 /* The individual REX.RXBW bits got consumed. */
5597 i.rex &= REX_OPCODE;
5598 }
5599
5600 /* Handle conversion of 'int $3' --> special int3 insn. */
5601 if (i.tm.mnem_off == MN_int
5602 && i.op[0].imms->X_add_number == 3)
5603 {
5604 i.tm.base_opcode = INT3_OPCODE;
5605 i.imm_operands = 0;
5606 }
5607
5608 if ((i.tm.opcode_modifier.jump == JUMP
5609 || i.tm.opcode_modifier.jump == JUMP_BYTE
5610 || i.tm.opcode_modifier.jump == JUMP_DWORD)
5611 && i.op[0].disps->X_op == O_constant)
5612 {
5613 /* Convert "jmp constant" (and "call constant") to a jump (call) to
5614 the absolute address given by the constant. Since ix86 jumps and
5615 calls are pc relative, we need to generate a reloc. */
5616 i.op[0].disps->X_add_symbol = &abs_symbol;
5617 i.op[0].disps->X_op = O_symbol;
5618 }
5619
5620 /* For 8 bit registers we need an empty rex prefix. Also if the
5621 instruction already has a prefix, we need to convert old
5622 registers to new ones. */
5623
5624 if ((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte
5625 && (i.op[0].regs->reg_flags & RegRex64) != 0)
5626 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte
5627 && (i.op[1].regs->reg_flags & RegRex64) != 0)
5628 || (((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte)
5629 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte))
5630 && i.rex != 0))
5631 {
5632 int x;
5633
5634 i.rex |= REX_OPCODE;
5635 for (x = 0; x < 2; x++)
5636 {
5637 /* Look for 8 bit operand that uses old registers. */
5638 if (i.types[x].bitfield.class == Reg && i.types[x].bitfield.byte
5639 && (i.op[x].regs->reg_flags & RegRex64) == 0)
5640 {
5641 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
5642 /* In case it is "hi" register, give up. */
5643 if (i.op[x].regs->reg_num > 3)
5644 as_bad (_("can't encode register '%s%s' in an "
5645 "instruction requiring REX prefix."),
5646 register_prefix, i.op[x].regs->reg_name);
5647
5648 /* Otherwise it is equivalent to the extended register.
5649 Since the encoding doesn't change this is merely
5650 cosmetic cleanup for debug output. */
5651
5652 i.op[x].regs = i.op[x].regs + 8;
5653 }
5654 }
5655 }
5656
5657 if (i.rex == 0 && i.rex_encoding)
5658 {
5659 /* Check if we can add a REX_OPCODE byte. Look for 8 bit operand
5660 that uses legacy register. If it is "hi" register, don't add
5661 the REX_OPCODE byte. */
5662 int x;
5663 for (x = 0; x < 2; x++)
5664 if (i.types[x].bitfield.class == Reg
5665 && i.types[x].bitfield.byte
5666 && (i.op[x].regs->reg_flags & RegRex64) == 0
5667 && i.op[x].regs->reg_num > 3)
5668 {
5669 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
5670 i.rex_encoding = false;
5671 break;
5672 }
5673
5674 if (i.rex_encoding)
5675 i.rex = REX_OPCODE;
5676 }
5677
5678 if (i.rex != 0)
5679 add_prefix (REX_OPCODE | i.rex);
5680
5681 insert_lfence_before ();
5682
5683 /* We are ready to output the insn. */
5684 output_insn ();
5685
5686 insert_lfence_after ();
5687
5688 last_insn.seg = now_seg;
5689
5690 if (i.tm.opcode_modifier.isprefix)
5691 {
5692 last_insn.kind = last_insn_prefix;
5693 last_insn.name = insn_name (&i.tm);
5694 last_insn.file = as_where (&last_insn.line);
5695 }
5696 else
5697 last_insn.kind = last_insn_other;
5698 }
5699
5700 /* The Q suffix is generally valid only in 64-bit mode, with very few
5701 exceptions: fild, fistp, fisttp, and cmpxchg8b. Note that for fild
5702 and fisttp only one of their two templates is matched below: That's
5703 sufficient since other relevant attributes are the same between both
5704 respective templates. */
5705 static INLINE bool q_suffix_allowed(const insn_template *t)
5706 {
5707 return flag_code == CODE_64BIT
5708 || (t->opcode_space == SPACE_BASE
5709 && t->base_opcode == 0xdf
5710 && (t->extension_opcode & 1)) /* fild / fistp / fisttp */
5711 || t->mnem_off == MN_cmpxchg8b;
5712 }
5713
5714 static const char *
5715 parse_insn (const char *line, char *mnemonic, bool prefix_only)
5716 {
5717 const char *l = line, *token_start = l;
5718 char *mnem_p;
5719 bool pass1 = !current_templates;
5720 int supported;
5721 const insn_template *t;
5722 char *dot_p = NULL;
5723
5724 while (1)
5725 {
5726 mnem_p = mnemonic;
5727 /* Pseudo-prefixes start with an opening figure brace. */
5728 if ((*mnem_p = *l) == '{')
5729 {
5730 ++mnem_p;
5731 ++l;
5732 }
5733 while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
5734 {
5735 if (*mnem_p == '.')
5736 dot_p = mnem_p;
5737 mnem_p++;
5738 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
5739 {
5740 too_long:
5741 as_bad (_("no such instruction: `%s'"), token_start);
5742 return NULL;
5743 }
5744 l++;
5745 }
5746 /* Pseudo-prefixes end with a closing figure brace. */
5747 if (*mnemonic == '{' && *l == '}')
5748 {
5749 *mnem_p++ = *l++;
5750 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
5751 goto too_long;
5752 *mnem_p = '\0';
5753
5754 /* Point l at the closing brace if there's no other separator. */
5755 if (*l != END_OF_INSN && !is_space_char (*l)
5756 && *l != PREFIX_SEPARATOR)
5757 --l;
5758 }
5759 else if (!is_space_char (*l)
5760 && *l != END_OF_INSN
5761 && (intel_syntax
5762 || (*l != PREFIX_SEPARATOR && *l != ',')))
5763 {
5764 if (prefix_only)
5765 break;
5766 as_bad (_("invalid character %s in mnemonic"),
5767 output_invalid (*l));
5768 return NULL;
5769 }
5770 if (token_start == l)
5771 {
5772 if (!intel_syntax && *l == PREFIX_SEPARATOR)
5773 as_bad (_("expecting prefix; got nothing"));
5774 else
5775 as_bad (_("expecting mnemonic; got nothing"));
5776 return NULL;
5777 }
5778
5779 /* Look up instruction (or prefix) via hash table. */
5780 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
5781
5782 if (*l != END_OF_INSN
5783 && (!is_space_char (*l) || l[1] != END_OF_INSN)
5784 && current_templates
5785 && current_templates->start->opcode_modifier.isprefix)
5786 {
5787 if (!cpu_flags_check_cpu64 (current_templates->start))
5788 {
5789 as_bad ((flag_code != CODE_64BIT
5790 ? _("`%s' is only supported in 64-bit mode")
5791 : _("`%s' is not supported in 64-bit mode")),
5792 insn_name (current_templates->start));
5793 return NULL;
5794 }
5795 /* If we are in 16-bit mode, do not allow addr16 or data16.
5796 Similarly, in 32-bit mode, do not allow addr32 or data32. */
5797 if ((current_templates->start->opcode_modifier.size == SIZE16
5798 || current_templates->start->opcode_modifier.size == SIZE32)
5799 && flag_code != CODE_64BIT
5800 && ((current_templates->start->opcode_modifier.size == SIZE32)
5801 ^ (flag_code == CODE_16BIT)))
5802 {
5803 as_bad (_("redundant %s prefix"),
5804 insn_name (current_templates->start));
5805 return NULL;
5806 }
5807
5808 if (current_templates->start->base_opcode == PSEUDO_PREFIX)
5809 {
5810 /* Handle pseudo prefixes. */
5811 switch (current_templates->start->extension_opcode)
5812 {
5813 case Prefix_Disp8:
5814 /* {disp8} */
5815 i.disp_encoding = disp_encoding_8bit;
5816 break;
5817 case Prefix_Disp16:
5818 /* {disp16} */
5819 i.disp_encoding = disp_encoding_16bit;
5820 break;
5821 case Prefix_Disp32:
5822 /* {disp32} */
5823 i.disp_encoding = disp_encoding_32bit;
5824 break;
5825 case Prefix_Load:
5826 /* {load} */
5827 i.dir_encoding = dir_encoding_load;
5828 break;
5829 case Prefix_Store:
5830 /* {store} */
5831 i.dir_encoding = dir_encoding_store;
5832 break;
5833 case Prefix_VEX:
5834 /* {vex} */
5835 i.vec_encoding = vex_encoding_vex;
5836 break;
5837 case Prefix_VEX3:
5838 /* {vex3} */
5839 i.vec_encoding = vex_encoding_vex3;
5840 break;
5841 case Prefix_EVEX:
5842 /* {evex} */
5843 i.vec_encoding = vex_encoding_evex;
5844 break;
5845 case Prefix_REX:
5846 /* {rex} */
5847 i.rex_encoding = true;
5848 break;
5849 case Prefix_NoOptimize:
5850 /* {nooptimize} */
5851 i.no_optimize = true;
5852 break;
5853 default:
5854 abort ();
5855 }
5856 }
5857 else
5858 {
5859 /* Add prefix, checking for repeated prefixes. */
5860 switch (add_prefix (current_templates->start->base_opcode))
5861 {
5862 case PREFIX_EXIST:
5863 return NULL;
5864 case PREFIX_DS:
5865 if (is_cpu (current_templates->start, CpuIBT))
5866 i.notrack_prefix = insn_name (current_templates->start);
5867 break;
5868 case PREFIX_REP:
5869 if (is_cpu (current_templates->start, CpuHLE))
5870 i.hle_prefix = insn_name (current_templates->start);
5871 else if (is_cpu (current_templates->start, CpuMPX))
5872 i.bnd_prefix = insn_name (current_templates->start);
5873 else
5874 i.rep_prefix = insn_name (current_templates->start);
5875 break;
5876 default:
5877 break;
5878 }
5879 }
5880 /* Skip past PREFIX_SEPARATOR and reset token_start. */
5881 token_start = ++l;
5882 }
5883 else
5884 break;
5885 }
5886
5887 if (prefix_only)
5888 return token_start;
5889
5890 if (!current_templates)
5891 {
5892 /* Deprecated functionality (new code should use pseudo-prefixes instead):
5893 Check if we should swap operand or force 32bit displacement in
5894 encoding. */
5895 if (mnem_p - 2 == dot_p && dot_p[1] == 's')
5896 i.dir_encoding = dir_encoding_swap;
5897 else if (mnem_p - 3 == dot_p
5898 && dot_p[1] == 'd'
5899 && dot_p[2] == '8')
5900 i.disp_encoding = disp_encoding_8bit;
5901 else if (mnem_p - 4 == dot_p
5902 && dot_p[1] == 'd'
5903 && dot_p[2] == '3'
5904 && dot_p[3] == '2')
5905 i.disp_encoding = disp_encoding_32bit;
5906 else
5907 goto check_suffix;
5908 mnem_p = dot_p;
5909 *dot_p = '\0';
5910 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
5911 }
5912
5913 if (!current_templates || !pass1)
5914 {
5915 current_templates = NULL;
5916
5917 check_suffix:
5918 if (mnem_p > mnemonic)
5919 {
5920 /* See if we can get a match by trimming off a suffix. */
5921 switch (mnem_p[-1])
5922 {
5923 case WORD_MNEM_SUFFIX:
5924 if (intel_syntax && (intel_float_operand (mnemonic) & 2))
5925 i.suffix = SHORT_MNEM_SUFFIX;
5926 else
5927 /* Fall through. */
5928 case BYTE_MNEM_SUFFIX:
5929 case QWORD_MNEM_SUFFIX:
5930 i.suffix = mnem_p[-1];
5931 mnem_p[-1] = '\0';
5932 current_templates
5933 = (const templates *) str_hash_find (op_hash, mnemonic);
5934 break;
5935 case SHORT_MNEM_SUFFIX:
5936 case LONG_MNEM_SUFFIX:
5937 if (!intel_syntax)
5938 {
5939 i.suffix = mnem_p[-1];
5940 mnem_p[-1] = '\0';
5941 current_templates
5942 = (const templates *) str_hash_find (op_hash, mnemonic);
5943 }
5944 break;
5945
5946 /* Intel Syntax. */
5947 case 'd':
5948 if (intel_syntax)
5949 {
5950 if (intel_float_operand (mnemonic) == 1)
5951 i.suffix = SHORT_MNEM_SUFFIX;
5952 else
5953 i.suffix = LONG_MNEM_SUFFIX;
5954 mnem_p[-1] = '\0';
5955 current_templates
5956 = (const templates *) str_hash_find (op_hash, mnemonic);
5957 }
5958 /* For compatibility reasons accept MOVSD and CMPSD without
5959 operands even in AT&T mode. */
5960 else if (*l == END_OF_INSN
5961 || (is_space_char (*l) && l[1] == END_OF_INSN))
5962 {
5963 mnem_p[-1] = '\0';
5964 current_templates
5965 = (const templates *) str_hash_find (op_hash, mnemonic);
5966 if (current_templates != NULL
5967 /* MOVS or CMPS */
5968 && (current_templates->start->base_opcode | 2) == 0xa6
5969 && current_templates->start->opcode_space
5970 == SPACE_BASE
5971 && mnem_p[-2] == 's')
5972 {
5973 as_warn (_("found `%sd'; assuming `%sl' was meant"),
5974 mnemonic, mnemonic);
5975 i.suffix = LONG_MNEM_SUFFIX;
5976 }
5977 else
5978 {
5979 current_templates = NULL;
5980 mnem_p[-1] = 'd';
5981 }
5982 }
5983 break;
5984 }
5985 }
5986
5987 if (!current_templates)
5988 {
5989 if (pass1)
5990 as_bad (_("no such instruction: `%s'"), token_start);
5991 return NULL;
5992 }
5993 }
5994
5995 if (current_templates->start->opcode_modifier.jump == JUMP
5996 || current_templates->start->opcode_modifier.jump == JUMP_BYTE)
5997 {
5998 /* Check for a branch hint. We allow ",pt" and ",pn" for
5999 predict taken and predict not taken respectively.
6000 I'm not sure that branch hints actually do anything on loop
6001 and jcxz insns (JumpByte) for current Pentium4 chips. They
6002 may work in the future and it doesn't hurt to accept them
6003 now. */
6004 if (l[0] == ',' && l[1] == 'p')
6005 {
6006 if (l[2] == 't')
6007 {
6008 if (!add_prefix (DS_PREFIX_OPCODE))
6009 return NULL;
6010 l += 3;
6011 }
6012 else if (l[2] == 'n')
6013 {
6014 if (!add_prefix (CS_PREFIX_OPCODE))
6015 return NULL;
6016 l += 3;
6017 }
6018 }
6019 }
6020 /* Any other comma loses. */
6021 if (*l == ',')
6022 {
6023 as_bad (_("invalid character %s in mnemonic"),
6024 output_invalid (*l));
6025 return NULL;
6026 }
6027
6028 /* Check if instruction is supported on specified architecture. */
6029 supported = 0;
6030 for (t = current_templates->start; t < current_templates->end; ++t)
6031 {
6032 supported |= cpu_flags_match (t);
6033
6034 if (i.suffix == QWORD_MNEM_SUFFIX && !q_suffix_allowed (t))
6035 supported &= ~CPU_FLAGS_64BIT_MATCH;
6036
6037 if (supported == CPU_FLAGS_PERFECT_MATCH)
6038 return l;
6039 }
6040
6041 if (pass1)
6042 {
6043 if (supported & CPU_FLAGS_64BIT_MATCH)
6044 i.error = unsupported_on_arch;
6045 else
6046 i.error = unsupported_64bit;
6047 }
6048
6049 return NULL;
6050 }
6051
6052 static char *
6053 parse_operands (char *l, const char *mnemonic)
6054 {
6055 char *token_start;
6056
6057 /* 1 if operand is pending after ','. */
6058 unsigned int expecting_operand = 0;
6059
6060 while (*l != END_OF_INSN)
6061 {
6062 /* Non-zero if operand parens not balanced. */
6063 unsigned int paren_not_balanced = 0;
6064 /* True if inside double quotes. */
6065 bool in_quotes = false;
6066
6067 /* Skip optional white space before operand. */
6068 if (is_space_char (*l))
6069 ++l;
6070 if (!is_operand_char (*l) && *l != END_OF_INSN && *l != '"')
6071 {
6072 as_bad (_("invalid character %s before operand %d"),
6073 output_invalid (*l),
6074 i.operands + 1);
6075 return NULL;
6076 }
6077 token_start = l; /* After white space. */
6078 while (in_quotes || paren_not_balanced || *l != ',')
6079 {
6080 if (*l == END_OF_INSN)
6081 {
6082 if (in_quotes)
6083 {
6084 as_bad (_("unbalanced double quotes in operand %d."),
6085 i.operands + 1);
6086 return NULL;
6087 }
6088 if (paren_not_balanced)
6089 {
6090 know (!intel_syntax);
6091 as_bad (_("unbalanced parenthesis in operand %d."),
6092 i.operands + 1);
6093 return NULL;
6094 }
6095 else
6096 break; /* we are done */
6097 }
6098 else if (*l == '\\' && l[1] == '"')
6099 ++l;
6100 else if (*l == '"')
6101 in_quotes = !in_quotes;
6102 else if (!in_quotes && !is_operand_char (*l) && !is_space_char (*l))
6103 {
6104 as_bad (_("invalid character %s in operand %d"),
6105 output_invalid (*l),
6106 i.operands + 1);
6107 return NULL;
6108 }
6109 if (!intel_syntax && !in_quotes)
6110 {
6111 if (*l == '(')
6112 ++paren_not_balanced;
6113 if (*l == ')')
6114 --paren_not_balanced;
6115 }
6116 l++;
6117 }
6118 if (l != token_start)
6119 { /* Yes, we've read in another operand. */
6120 unsigned int operand_ok;
6121 this_operand = i.operands++;
6122 if (i.operands > MAX_OPERANDS)
6123 {
6124 as_bad (_("spurious operands; (%d operands/instruction max)"),
6125 MAX_OPERANDS);
6126 return NULL;
6127 }
6128 i.types[this_operand].bitfield.unspecified = 1;
6129 /* Now parse operand adding info to 'i' as we go along. */
6130 END_STRING_AND_SAVE (l);
6131
6132 if (i.mem_operands > 1)
6133 {
6134 as_bad (_("too many memory references for `%s'"),
6135 mnemonic);
6136 return 0;
6137 }
6138
6139 if (intel_syntax)
6140 operand_ok =
6141 i386_intel_operand (token_start,
6142 intel_float_operand (mnemonic));
6143 else
6144 operand_ok = i386_att_operand (token_start);
6145
6146 RESTORE_END_STRING (l);
6147 if (!operand_ok)
6148 return NULL;
6149 }
6150 else
6151 {
6152 if (expecting_operand)
6153 {
6154 expecting_operand_after_comma:
6155 as_bad (_("expecting operand after ','; got nothing"));
6156 return NULL;
6157 }
6158 if (*l == ',')
6159 {
6160 as_bad (_("expecting operand before ','; got nothing"));
6161 return NULL;
6162 }
6163 }
6164
6165 /* Now *l must be either ',' or END_OF_INSN. */
6166 if (*l == ',')
6167 {
6168 if (*++l == END_OF_INSN)
6169 {
6170 /* Just skip it, if it's \n complain. */
6171 goto expecting_operand_after_comma;
6172 }
6173 expecting_operand = 1;
6174 }
6175 }
6176 return l;
6177 }
6178
6179 static void
6180 swap_2_operands (unsigned int xchg1, unsigned int xchg2)
6181 {
6182 union i386_op temp_op;
6183 i386_operand_type temp_type;
6184 unsigned int temp_flags;
6185 enum bfd_reloc_code_real temp_reloc;
6186
6187 temp_type = i.types[xchg2];
6188 i.types[xchg2] = i.types[xchg1];
6189 i.types[xchg1] = temp_type;
6190
6191 temp_flags = i.flags[xchg2];
6192 i.flags[xchg2] = i.flags[xchg1];
6193 i.flags[xchg1] = temp_flags;
6194
6195 temp_op = i.op[xchg2];
6196 i.op[xchg2] = i.op[xchg1];
6197 i.op[xchg1] = temp_op;
6198
6199 temp_reloc = i.reloc[xchg2];
6200 i.reloc[xchg2] = i.reloc[xchg1];
6201 i.reloc[xchg1] = temp_reloc;
6202
6203 temp_flags = i.imm_bits[xchg2];
6204 i.imm_bits[xchg2] = i.imm_bits[xchg1];
6205 i.imm_bits[xchg1] = temp_flags;
6206
6207 if (i.mask.reg)
6208 {
6209 if (i.mask.operand == xchg1)
6210 i.mask.operand = xchg2;
6211 else if (i.mask.operand == xchg2)
6212 i.mask.operand = xchg1;
6213 }
6214 if (i.broadcast.type || i.broadcast.bytes)
6215 {
6216 if (i.broadcast.operand == xchg1)
6217 i.broadcast.operand = xchg2;
6218 else if (i.broadcast.operand == xchg2)
6219 i.broadcast.operand = xchg1;
6220 }
6221 }
6222
6223 static void
6224 swap_operands (void)
6225 {
6226 switch (i.operands)
6227 {
6228 case 5:
6229 case 4:
6230 swap_2_operands (1, i.operands - 2);
6231 /* Fall through. */
6232 case 3:
6233 case 2:
6234 swap_2_operands (0, i.operands - 1);
6235 break;
6236 default:
6237 abort ();
6238 }
6239
6240 if (i.mem_operands == 2)
6241 {
6242 const reg_entry *temp_seg;
6243 temp_seg = i.seg[0];
6244 i.seg[0] = i.seg[1];
6245 i.seg[1] = temp_seg;
6246 }
6247 }
6248
6249 /* Try to ensure constant immediates are represented in the smallest
6250 opcode possible. */
6251 static void
6252 optimize_imm (void)
6253 {
6254 char guess_suffix = 0;
6255 int op;
6256
6257 if (i.suffix)
6258 guess_suffix = i.suffix;
6259 else if (i.reg_operands)
6260 {
6261 /* Figure out a suffix from the last register operand specified.
6262 We can't do this properly yet, i.e. excluding special register
6263 instances, but the following works for instructions with
6264 immediates. In any case, we can't set i.suffix yet. */
6265 for (op = i.operands; --op >= 0;)
6266 if (i.types[op].bitfield.class != Reg)
6267 continue;
6268 else if (i.types[op].bitfield.byte)
6269 {
6270 guess_suffix = BYTE_MNEM_SUFFIX;
6271 break;
6272 }
6273 else if (i.types[op].bitfield.word)
6274 {
6275 guess_suffix = WORD_MNEM_SUFFIX;
6276 break;
6277 }
6278 else if (i.types[op].bitfield.dword)
6279 {
6280 guess_suffix = LONG_MNEM_SUFFIX;
6281 break;
6282 }
6283 else if (i.types[op].bitfield.qword)
6284 {
6285 guess_suffix = QWORD_MNEM_SUFFIX;
6286 break;
6287 }
6288 }
6289 else if ((flag_code == CODE_16BIT)
6290 ^ (i.prefix[DATA_PREFIX] != 0 && !(i.prefix[REX_PREFIX] & REX_W)))
6291 guess_suffix = WORD_MNEM_SUFFIX;
6292 else if (flag_code != CODE_64BIT
6293 || (!(i.prefix[REX_PREFIX] & REX_W)
6294 /* A more generic (but also more involved) way of dealing
6295 with the special case(s) would be to go look for
6296 DefaultSize attributes on any of the templates. */
6297 && current_templates->start->mnem_off != MN_push))
6298 guess_suffix = LONG_MNEM_SUFFIX;
6299
6300 for (op = i.operands; --op >= 0;)
6301 if (operand_type_check (i.types[op], imm))
6302 {
6303 switch (i.op[op].imms->X_op)
6304 {
6305 case O_constant:
6306 /* If a suffix is given, this operand may be shortened. */
6307 switch (guess_suffix)
6308 {
6309 case LONG_MNEM_SUFFIX:
6310 i.types[op].bitfield.imm32 = 1;
6311 i.types[op].bitfield.imm64 = 1;
6312 break;
6313 case WORD_MNEM_SUFFIX:
6314 i.types[op].bitfield.imm16 = 1;
6315 i.types[op].bitfield.imm32 = 1;
6316 i.types[op].bitfield.imm32s = 1;
6317 i.types[op].bitfield.imm64 = 1;
6318 break;
6319 case BYTE_MNEM_SUFFIX:
6320 i.types[op].bitfield.imm8 = 1;
6321 i.types[op].bitfield.imm8s = 1;
6322 i.types[op].bitfield.imm16 = 1;
6323 i.types[op].bitfield.imm32 = 1;
6324 i.types[op].bitfield.imm32s = 1;
6325 i.types[op].bitfield.imm64 = 1;
6326 break;
6327 }
6328
6329 /* If this operand is at most 16 bits, convert it
6330 to a signed 16 bit number before trying to see
6331 whether it will fit in an even smaller size.
6332 This allows a 16-bit operand such as $0xffe0 to
6333 be recognised as within Imm8S range. */
6334 if ((i.types[op].bitfield.imm16)
6335 && fits_in_unsigned_word (i.op[op].imms->X_add_number))
6336 {
6337 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
6338 ^ 0x8000) - 0x8000);
6339 }
6340 #ifdef BFD64
6341 /* Store 32-bit immediate in 64-bit for 64-bit BFD. */
6342 if ((i.types[op].bitfield.imm32)
6343 && fits_in_unsigned_long (i.op[op].imms->X_add_number))
6344 {
6345 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
6346 ^ ((offsetT) 1 << 31))
6347 - ((offsetT) 1 << 31));
6348 }
6349 #endif
6350 i.types[op]
6351 = operand_type_or (i.types[op],
6352 smallest_imm_type (i.op[op].imms->X_add_number));
6353
6354 /* We must avoid matching of Imm32 templates when 64bit
6355 only immediate is available. */
6356 if (guess_suffix == QWORD_MNEM_SUFFIX)
6357 i.types[op].bitfield.imm32 = 0;
6358 break;
6359
6360 case O_absent:
6361 case O_register:
6362 abort ();
6363
6364 /* Symbols and expressions. */
6365 default:
6366 /* Convert symbolic operand to proper sizes for matching, but don't
6367 prevent matching a set of insns that only supports sizes other
6368 than those matching the insn suffix. */
6369 {
6370 i386_operand_type mask, allowed;
6371 const insn_template *t = current_templates->start;
6372
6373 operand_type_set (&mask, 0);
6374 switch (guess_suffix)
6375 {
6376 case QWORD_MNEM_SUFFIX:
6377 mask.bitfield.imm64 = 1;
6378 mask.bitfield.imm32s = 1;
6379 break;
6380 case LONG_MNEM_SUFFIX:
6381 mask.bitfield.imm32 = 1;
6382 break;
6383 case WORD_MNEM_SUFFIX:
6384 mask.bitfield.imm16 = 1;
6385 break;
6386 case BYTE_MNEM_SUFFIX:
6387 mask.bitfield.imm8 = 1;
6388 break;
6389 default:
6390 break;
6391 }
6392
6393 allowed = operand_type_and (t->operand_types[op], mask);
6394 while (++t < current_templates->end)
6395 {
6396 allowed = operand_type_or (allowed, t->operand_types[op]);
6397 allowed = operand_type_and (allowed, mask);
6398 }
6399
6400 if (!operand_type_all_zero (&allowed))
6401 i.types[op] = operand_type_and (i.types[op], mask);
6402 }
6403 break;
6404 }
6405 }
6406 }
6407
6408 /* Try to use the smallest displacement type too. */
6409 static bool
6410 optimize_disp (const insn_template *t)
6411 {
6412 unsigned int op;
6413
6414 if (!want_disp32 (t)
6415 && (!t->opcode_modifier.jump
6416 || i.jumpabsolute || i.types[0].bitfield.baseindex))
6417 {
6418 for (op = 0; op < i.operands; ++op)
6419 {
6420 const expressionS *exp = i.op[op].disps;
6421
6422 if (!operand_type_check (i.types[op], disp))
6423 continue;
6424
6425 if (exp->X_op != O_constant)
6426 continue;
6427
6428 /* Since displacement is signed extended to 64bit, don't allow
6429 disp32 if it is out of range. */
6430 if (fits_in_signed_long (exp->X_add_number))
6431 continue;
6432
6433 i.types[op].bitfield.disp32 = 0;
6434 if (i.types[op].bitfield.baseindex)
6435 {
6436 as_bad (_("0x%" PRIx64 " out of range of signed 32bit displacement"),
6437 (uint64_t) exp->X_add_number);
6438 return false;
6439 }
6440 }
6441 }
6442
6443 /* Don't optimize displacement for movabs since it only takes 64bit
6444 displacement. */
6445 if (i.disp_encoding > disp_encoding_8bit
6446 || (flag_code == CODE_64BIT && t->mnem_off == MN_movabs))
6447 return true;
6448
6449 for (op = i.operands; op-- > 0;)
6450 if (operand_type_check (i.types[op], disp))
6451 {
6452 if (i.op[op].disps->X_op == O_constant)
6453 {
6454 offsetT op_disp = i.op[op].disps->X_add_number;
6455
6456 if (!op_disp && i.types[op].bitfield.baseindex)
6457 {
6458 i.types[op] = operand_type_and_not (i.types[op], anydisp);
6459 i.op[op].disps = NULL;
6460 i.disp_operands--;
6461 continue;
6462 }
6463
6464 if (i.types[op].bitfield.disp16
6465 && fits_in_unsigned_word (op_disp))
6466 {
6467 /* If this operand is at most 16 bits, convert
6468 to a signed 16 bit number and don't use 64bit
6469 displacement. */
6470 op_disp = ((op_disp ^ 0x8000) - 0x8000);
6471 i.types[op].bitfield.disp64 = 0;
6472 }
6473
6474 #ifdef BFD64
6475 /* Optimize 64-bit displacement to 32-bit for 64-bit BFD. */
6476 if ((flag_code != CODE_64BIT
6477 ? i.types[op].bitfield.disp32
6478 : want_disp32 (t)
6479 && (!t->opcode_modifier.jump
6480 || i.jumpabsolute || i.types[op].bitfield.baseindex))
6481 && fits_in_unsigned_long (op_disp))
6482 {
6483 /* If this operand is at most 32 bits, convert
6484 to a signed 32 bit number and don't use 64bit
6485 displacement. */
6486 op_disp = (op_disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
6487 i.types[op].bitfield.disp64 = 0;
6488 i.types[op].bitfield.disp32 = 1;
6489 }
6490
6491 if (flag_code == CODE_64BIT && fits_in_signed_long (op_disp))
6492 {
6493 i.types[op].bitfield.disp64 = 0;
6494 i.types[op].bitfield.disp32 = 1;
6495 }
6496 #endif
6497 if ((i.types[op].bitfield.disp32
6498 || i.types[op].bitfield.disp16)
6499 && fits_in_disp8 (op_disp))
6500 i.types[op].bitfield.disp8 = 1;
6501
6502 i.op[op].disps->X_add_number = op_disp;
6503 }
6504 else if (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
6505 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL)
6506 {
6507 fix_new_exp (frag_now, frag_more (0) - frag_now->fr_literal, 0,
6508 i.op[op].disps, 0, i.reloc[op]);
6509 i.types[op] = operand_type_and_not (i.types[op], anydisp);
6510 }
6511 else
6512 /* We only support 64bit displacement on constants. */
6513 i.types[op].bitfield.disp64 = 0;
6514 }
6515
6516 return true;
6517 }
6518
6519 /* Return 1 if there is a match in broadcast bytes between operand
6520 GIVEN and instruction template T. */
6521
6522 static INLINE int
6523 match_broadcast_size (const insn_template *t, unsigned int given)
6524 {
6525 return ((t->opcode_modifier.broadcast == BYTE_BROADCAST
6526 && i.types[given].bitfield.byte)
6527 || (t->opcode_modifier.broadcast == WORD_BROADCAST
6528 && i.types[given].bitfield.word)
6529 || (t->opcode_modifier.broadcast == DWORD_BROADCAST
6530 && i.types[given].bitfield.dword)
6531 || (t->opcode_modifier.broadcast == QWORD_BROADCAST
6532 && i.types[given].bitfield.qword));
6533 }
6534
6535 /* Check if operands are valid for the instruction. */
6536
6537 static int
6538 check_VecOperands (const insn_template *t)
6539 {
6540 unsigned int op;
6541 i386_cpu_flags cpu;
6542
6543 /* Templates allowing for ZMMword as well as YMMword and/or XMMword for
6544 any one operand are implicity requiring AVX512VL support if the actual
6545 operand size is YMMword or XMMword. Since this function runs after
6546 template matching, there's no need to check for YMMword/XMMword in
6547 the template. */
6548 cpu = cpu_flags_and (cpu_flags_from_attr (t->cpu), avx512);
6549 if (!cpu_flags_all_zero (&cpu)
6550 && !is_cpu (t, CpuAVX512VL)
6551 && !cpu_arch_flags.bitfield.cpuavx512vl
6552 && (!t->opcode_modifier.vex || need_evex_encoding ()))
6553 {
6554 for (op = 0; op < t->operands; ++op)
6555 {
6556 if (t->operand_types[op].bitfield.zmmword
6557 && (i.types[op].bitfield.ymmword
6558 || i.types[op].bitfield.xmmword))
6559 {
6560 i.error = unsupported;
6561 return 1;
6562 }
6563 }
6564 }
6565
6566 /* Somewhat similarly, templates specifying both AVX and AVX2 are
6567 requiring AVX2 support if the actual operand size is YMMword. */
6568 if (is_cpu (t, CpuAVX) && is_cpu (t, CpuAVX2)
6569 && !cpu_arch_flags.bitfield.cpuavx2)
6570 {
6571 for (op = 0; op < t->operands; ++op)
6572 {
6573 if (t->operand_types[op].bitfield.xmmword
6574 && i.types[op].bitfield.ymmword)
6575 {
6576 i.error = unsupported;
6577 return 1;
6578 }
6579 }
6580 }
6581
6582 /* Without VSIB byte, we can't have a vector register for index. */
6583 if (!t->opcode_modifier.sib
6584 && i.index_reg
6585 && (i.index_reg->reg_type.bitfield.xmmword
6586 || i.index_reg->reg_type.bitfield.ymmword
6587 || i.index_reg->reg_type.bitfield.zmmword))
6588 {
6589 i.error = unsupported_vector_index_register;
6590 return 1;
6591 }
6592
6593 /* Check if default mask is allowed. */
6594 if (t->opcode_modifier.operandconstraint == NO_DEFAULT_MASK
6595 && (!i.mask.reg || i.mask.reg->reg_num == 0))
6596 {
6597 i.error = no_default_mask;
6598 return 1;
6599 }
6600
6601 /* For VSIB byte, we need a vector register for index, and all vector
6602 registers must be distinct. */
6603 if (t->opcode_modifier.sib && t->opcode_modifier.sib != SIBMEM)
6604 {
6605 if (!i.index_reg
6606 || !((t->opcode_modifier.sib == VECSIB128
6607 && i.index_reg->reg_type.bitfield.xmmword)
6608 || (t->opcode_modifier.sib == VECSIB256
6609 && i.index_reg->reg_type.bitfield.ymmword)
6610 || (t->opcode_modifier.sib == VECSIB512
6611 && i.index_reg->reg_type.bitfield.zmmword)))
6612 {
6613 i.error = invalid_vsib_address;
6614 return 1;
6615 }
6616
6617 gas_assert (i.reg_operands == 2 || i.mask.reg);
6618 if (i.reg_operands == 2 && !i.mask.reg)
6619 {
6620 gas_assert (i.types[0].bitfield.class == RegSIMD);
6621 gas_assert (i.types[0].bitfield.xmmword
6622 || i.types[0].bitfield.ymmword);
6623 gas_assert (i.types[2].bitfield.class == RegSIMD);
6624 gas_assert (i.types[2].bitfield.xmmword
6625 || i.types[2].bitfield.ymmword);
6626 if (operand_check == check_none)
6627 return 0;
6628 if (register_number (i.op[0].regs)
6629 != register_number (i.index_reg)
6630 && register_number (i.op[2].regs)
6631 != register_number (i.index_reg)
6632 && register_number (i.op[0].regs)
6633 != register_number (i.op[2].regs))
6634 return 0;
6635 if (operand_check == check_error)
6636 {
6637 i.error = invalid_vector_register_set;
6638 return 1;
6639 }
6640 as_warn (_("mask, index, and destination registers should be distinct"));
6641 }
6642 else if (i.reg_operands == 1 && i.mask.reg)
6643 {
6644 if (i.types[1].bitfield.class == RegSIMD
6645 && (i.types[1].bitfield.xmmword
6646 || i.types[1].bitfield.ymmword
6647 || i.types[1].bitfield.zmmword)
6648 && (register_number (i.op[1].regs)
6649 == register_number (i.index_reg)))
6650 {
6651 if (operand_check == check_error)
6652 {
6653 i.error = invalid_vector_register_set;
6654 return 1;
6655 }
6656 if (operand_check != check_none)
6657 as_warn (_("index and destination registers should be distinct"));
6658 }
6659 }
6660 }
6661
6662 /* For AMX instructions with 3 TMM register operands, all operands
6663 must be distinct. */
6664 if (i.reg_operands == 3
6665 && t->operand_types[0].bitfield.tmmword
6666 && (i.op[0].regs == i.op[1].regs
6667 || i.op[0].regs == i.op[2].regs
6668 || i.op[1].regs == i.op[2].regs))
6669 {
6670 i.error = invalid_tmm_register_set;
6671 return 1;
6672 }
6673
6674 /* For some special instructions require that destination must be distinct
6675 from source registers. */
6676 if (t->opcode_modifier.operandconstraint == DISTINCT_DEST)
6677 {
6678 unsigned int dest_reg = i.operands - 1;
6679
6680 know (i.operands >= 3);
6681
6682 /* #UD if dest_reg == src1_reg or dest_reg == src2_reg. */
6683 if (i.op[dest_reg - 1].regs == i.op[dest_reg].regs
6684 || (i.reg_operands > 2
6685 && i.op[dest_reg - 2].regs == i.op[dest_reg].regs))
6686 {
6687 i.error = invalid_dest_and_src_register_set;
6688 return 1;
6689 }
6690 }
6691
6692 /* Check if broadcast is supported by the instruction and is applied
6693 to the memory operand. */
6694 if (i.broadcast.type || i.broadcast.bytes)
6695 {
6696 i386_operand_type type, overlap;
6697
6698 /* Check if specified broadcast is supported in this instruction,
6699 and its broadcast bytes match the memory operand. */
6700 op = i.broadcast.operand;
6701 if (!t->opcode_modifier.broadcast
6702 || !(i.flags[op] & Operand_Mem)
6703 || (!i.types[op].bitfield.unspecified
6704 && !match_broadcast_size (t, op)))
6705 {
6706 bad_broadcast:
6707 i.error = unsupported_broadcast;
6708 return 1;
6709 }
6710
6711 operand_type_set (&type, 0);
6712 switch (get_broadcast_bytes (t, false))
6713 {
6714 case 2:
6715 type.bitfield.word = 1;
6716 break;
6717 case 4:
6718 type.bitfield.dword = 1;
6719 break;
6720 case 8:
6721 type.bitfield.qword = 1;
6722 break;
6723 case 16:
6724 type.bitfield.xmmword = 1;
6725 break;
6726 case 32:
6727 if (vector_size < VSZ256)
6728 goto bad_broadcast;
6729 type.bitfield.ymmword = 1;
6730 break;
6731 case 64:
6732 if (vector_size < VSZ512)
6733 goto bad_broadcast;
6734 type.bitfield.zmmword = 1;
6735 break;
6736 default:
6737 goto bad_broadcast;
6738 }
6739
6740 overlap = operand_type_and (type, t->operand_types[op]);
6741 if (t->operand_types[op].bitfield.class == RegSIMD
6742 && t->operand_types[op].bitfield.byte
6743 + t->operand_types[op].bitfield.word
6744 + t->operand_types[op].bitfield.dword
6745 + t->operand_types[op].bitfield.qword > 1)
6746 {
6747 overlap.bitfield.xmmword = 0;
6748 overlap.bitfield.ymmword = 0;
6749 overlap.bitfield.zmmword = 0;
6750 }
6751 if (operand_type_all_zero (&overlap))
6752 goto bad_broadcast;
6753
6754 if (t->opcode_modifier.checkoperandsize)
6755 {
6756 unsigned int j;
6757
6758 type.bitfield.baseindex = 1;
6759 for (j = 0; j < i.operands; ++j)
6760 {
6761 if (j != op
6762 && !operand_type_register_match(i.types[j],
6763 t->operand_types[j],
6764 type,
6765 t->operand_types[op]))
6766 goto bad_broadcast;
6767 }
6768 }
6769 }
6770 /* If broadcast is supported in this instruction, we need to check if
6771 operand of one-element size isn't specified without broadcast. */
6772 else if (t->opcode_modifier.broadcast && i.mem_operands)
6773 {
6774 /* Find memory operand. */
6775 for (op = 0; op < i.operands; op++)
6776 if (i.flags[op] & Operand_Mem)
6777 break;
6778 gas_assert (op < i.operands);
6779 /* Check size of the memory operand. */
6780 if (match_broadcast_size (t, op))
6781 {
6782 i.error = broadcast_needed;
6783 return 1;
6784 }
6785 }
6786 else
6787 op = MAX_OPERANDS - 1; /* Avoid uninitialized variable warning. */
6788
6789 /* Check if requested masking is supported. */
6790 if (i.mask.reg)
6791 {
6792 if (!t->opcode_modifier.masking)
6793 {
6794 i.error = unsupported_masking;
6795 return 1;
6796 }
6797
6798 /* Common rules for masking:
6799 - mask register destinations permit only zeroing-masking, without
6800 that actually being expressed by a {z} operand suffix or EVEX.z,
6801 - memory destinations allow only merging-masking,
6802 - scatter/gather insns (i.e. ones using vSIB) only allow merging-
6803 masking. */
6804 if (i.mask.zeroing
6805 && (t->operand_types[t->operands - 1].bitfield.class == RegMask
6806 || (i.flags[t->operands - 1] & Operand_Mem)
6807 || t->opcode_modifier.sib))
6808 {
6809 i.error = unsupported_masking;
6810 return 1;
6811 }
6812 }
6813
6814 /* Check if masking is applied to dest operand. */
6815 if (i.mask.reg && (i.mask.operand != i.operands - 1))
6816 {
6817 i.error = mask_not_on_destination;
6818 return 1;
6819 }
6820
6821 /* Check RC/SAE. */
6822 if (i.rounding.type != rc_none)
6823 {
6824 if (!t->opcode_modifier.sae
6825 || ((i.rounding.type != saeonly) != t->opcode_modifier.staticrounding)
6826 || i.mem_operands)
6827 {
6828 i.error = unsupported_rc_sae;
6829 return 1;
6830 }
6831
6832 /* Non-EVEX.LIG forms need to have a ZMM register as at least one
6833 operand. */
6834 if (t->opcode_modifier.evex != EVEXLIG)
6835 {
6836 for (op = 0; op < t->operands; ++op)
6837 if (i.types[op].bitfield.zmmword)
6838 break;
6839 if (op >= t->operands)
6840 {
6841 i.error = operand_size_mismatch;
6842 return 1;
6843 }
6844 }
6845 }
6846
6847 /* Check the special Imm4 cases; must be the first operand. */
6848 if (is_cpu (t, CpuXOP) && t->operands == 5)
6849 {
6850 if (i.op[0].imms->X_op != O_constant
6851 || !fits_in_imm4 (i.op[0].imms->X_add_number))
6852 {
6853 i.error = bad_imm4;
6854 return 1;
6855 }
6856
6857 /* Turn off Imm<N> so that update_imm won't complain. */
6858 operand_type_set (&i.types[0], 0);
6859 }
6860
6861 /* Check vector Disp8 operand. */
6862 if (t->opcode_modifier.disp8memshift
6863 && (!t->opcode_modifier.vex
6864 || need_evex_encoding ())
6865 && i.disp_encoding <= disp_encoding_8bit)
6866 {
6867 if (i.broadcast.type || i.broadcast.bytes)
6868 i.memshift = t->opcode_modifier.broadcast - 1;
6869 else if (t->opcode_modifier.disp8memshift != DISP8_SHIFT_VL)
6870 i.memshift = t->opcode_modifier.disp8memshift;
6871 else
6872 {
6873 const i386_operand_type *type = NULL, *fallback = NULL;
6874
6875 i.memshift = 0;
6876 for (op = 0; op < i.operands; op++)
6877 if (i.flags[op] & Operand_Mem)
6878 {
6879 if (t->opcode_modifier.evex == EVEXLIG)
6880 i.memshift = 2 + (i.suffix == QWORD_MNEM_SUFFIX);
6881 else if (t->operand_types[op].bitfield.xmmword
6882 + t->operand_types[op].bitfield.ymmword
6883 + t->operand_types[op].bitfield.zmmword <= 1)
6884 type = &t->operand_types[op];
6885 else if (!i.types[op].bitfield.unspecified)
6886 type = &i.types[op];
6887 else /* Ambiguities get resolved elsewhere. */
6888 fallback = &t->operand_types[op];
6889 }
6890 else if (i.types[op].bitfield.class == RegSIMD
6891 && t->opcode_modifier.evex != EVEXLIG)
6892 {
6893 if (i.types[op].bitfield.zmmword)
6894 i.memshift = 6;
6895 else if (i.types[op].bitfield.ymmword && i.memshift < 5)
6896 i.memshift = 5;
6897 else if (i.types[op].bitfield.xmmword && i.memshift < 4)
6898 i.memshift = 4;
6899 }
6900
6901 if (!type && !i.memshift)
6902 type = fallback;
6903 if (type)
6904 {
6905 if (type->bitfield.zmmword)
6906 i.memshift = 6;
6907 else if (type->bitfield.ymmword)
6908 i.memshift = 5;
6909 else if (type->bitfield.xmmword)
6910 i.memshift = 4;
6911 }
6912
6913 /* For the check in fits_in_disp8(). */
6914 if (i.memshift == 0)
6915 i.memshift = -1;
6916 }
6917
6918 for (op = 0; op < i.operands; op++)
6919 if (operand_type_check (i.types[op], disp)
6920 && i.op[op].disps->X_op == O_constant)
6921 {
6922 if (fits_in_disp8 (i.op[op].disps->X_add_number))
6923 {
6924 i.types[op].bitfield.disp8 = 1;
6925 return 0;
6926 }
6927 i.types[op].bitfield.disp8 = 0;
6928 }
6929 }
6930
6931 i.memshift = 0;
6932
6933 return 0;
6934 }
6935
6936 /* Check if encoding requirements are met by the instruction. */
6937
6938 static int
6939 VEX_check_encoding (const insn_template *t)
6940 {
6941 if (i.vec_encoding == vex_encoding_error)
6942 {
6943 i.error = unsupported;
6944 return 1;
6945 }
6946
6947 /* Vector size restrictions. */
6948 if ((vector_size < VSZ512
6949 && (t->opcode_modifier.evex == EVEX512
6950 || t->opcode_modifier.vsz >= VSZ512))
6951 || (vector_size < VSZ256
6952 && (t->opcode_modifier.evex == EVEX256
6953 || t->opcode_modifier.vex == VEX256
6954 || t->opcode_modifier.vsz >= VSZ256)))
6955 {
6956 i.error = unsupported;
6957 return 1;
6958 }
6959
6960 if (i.vec_encoding == vex_encoding_evex
6961 || i.vec_encoding == vex_encoding_evex512)
6962 {
6963 /* This instruction must be encoded with EVEX prefix. */
6964 if (!is_evex_encoding (t))
6965 {
6966 i.error = unsupported;
6967 return 1;
6968 }
6969 return 0;
6970 }
6971
6972 if (!t->opcode_modifier.vex)
6973 {
6974 /* This instruction template doesn't have VEX prefix. */
6975 if (i.vec_encoding != vex_encoding_default)
6976 {
6977 i.error = unsupported;
6978 return 1;
6979 }
6980 return 0;
6981 }
6982
6983 return 0;
6984 }
6985
6986 /* Helper function for the progress() macro in match_template(). */
6987 static INLINE enum i386_error progress (enum i386_error new,
6988 enum i386_error last,
6989 unsigned int line, unsigned int *line_p)
6990 {
6991 if (line <= *line_p)
6992 return last;
6993 *line_p = line;
6994 return new;
6995 }
6996
6997 static const insn_template *
6998 match_template (char mnem_suffix)
6999 {
7000 /* Points to template once we've found it. */
7001 const insn_template *t;
7002 i386_operand_type overlap0, overlap1, overlap2, overlap3;
7003 i386_operand_type overlap4;
7004 unsigned int found_reverse_match;
7005 i386_operand_type operand_types [MAX_OPERANDS];
7006 int addr_prefix_disp;
7007 unsigned int j, size_match, check_register, errline = __LINE__;
7008 enum i386_error specific_error = number_of_operands_mismatch;
7009 #define progress(err) progress (err, specific_error, __LINE__, &errline)
7010
7011 #if MAX_OPERANDS != 5
7012 # error "MAX_OPERANDS must be 5."
7013 #endif
7014
7015 found_reverse_match = 0;
7016 addr_prefix_disp = -1;
7017
7018 for (t = current_templates->start; t < current_templates->end; t++)
7019 {
7020 addr_prefix_disp = -1;
7021 found_reverse_match = 0;
7022
7023 /* Must have right number of operands. */
7024 if (i.operands != t->operands)
7025 continue;
7026
7027 /* Check processor support. */
7028 specific_error = progress (unsupported);
7029 if (cpu_flags_match (t) != CPU_FLAGS_PERFECT_MATCH)
7030 continue;
7031
7032 /* Check AT&T mnemonic. */
7033 specific_error = progress (unsupported_with_intel_mnemonic);
7034 if (intel_mnemonic && t->opcode_modifier.attmnemonic)
7035 continue;
7036
7037 /* Check AT&T/Intel syntax. */
7038 specific_error = progress (unsupported_syntax);
7039 if ((intel_syntax && t->opcode_modifier.attsyntax)
7040 || (!intel_syntax && t->opcode_modifier.intelsyntax))
7041 continue;
7042
7043 /* Check Intel64/AMD64 ISA. */
7044 switch (isa64)
7045 {
7046 default:
7047 /* Default: Don't accept Intel64. */
7048 if (t->opcode_modifier.isa64 == INTEL64)
7049 continue;
7050 break;
7051 case amd64:
7052 /* -mamd64: Don't accept Intel64 and Intel64 only. */
7053 if (t->opcode_modifier.isa64 >= INTEL64)
7054 continue;
7055 break;
7056 case intel64:
7057 /* -mintel64: Don't accept AMD64. */
7058 if (t->opcode_modifier.isa64 == AMD64 && flag_code == CODE_64BIT)
7059 continue;
7060 break;
7061 }
7062
7063 /* Check the suffix. */
7064 specific_error = progress (invalid_instruction_suffix);
7065 if ((t->opcode_modifier.no_bsuf && mnem_suffix == BYTE_MNEM_SUFFIX)
7066 || (t->opcode_modifier.no_wsuf && mnem_suffix == WORD_MNEM_SUFFIX)
7067 || (t->opcode_modifier.no_lsuf && mnem_suffix == LONG_MNEM_SUFFIX)
7068 || (t->opcode_modifier.no_ssuf && mnem_suffix == SHORT_MNEM_SUFFIX)
7069 || (t->opcode_modifier.no_qsuf && mnem_suffix == QWORD_MNEM_SUFFIX))
7070 continue;
7071
7072 specific_error = progress (operand_size_mismatch);
7073 size_match = operand_size_match (t);
7074 if (!size_match)
7075 continue;
7076
7077 /* This is intentionally not
7078
7079 if (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE))
7080
7081 as the case of a missing * on the operand is accepted (perhaps with
7082 a warning, issued further down). */
7083 specific_error = progress (operand_type_mismatch);
7084 if (i.jumpabsolute && t->opcode_modifier.jump != JUMP_ABSOLUTE)
7085 continue;
7086
7087 /* In Intel syntax, normally we can check for memory operand size when
7088 there is no mnemonic suffix. But jmp and call have 2 different
7089 encodings with Dword memory operand size. Skip the "near" one
7090 (permitting a register operand) when "far" was requested. */
7091 if (i.far_branch
7092 && t->opcode_modifier.jump == JUMP_ABSOLUTE
7093 && t->operand_types[0].bitfield.class == Reg)
7094 continue;
7095
7096 for (j = 0; j < MAX_OPERANDS; j++)
7097 operand_types[j] = t->operand_types[j];
7098
7099 /* In general, don't allow 32-bit operands on pre-386. */
7100 specific_error = progress (mnem_suffix ? invalid_instruction_suffix
7101 : operand_size_mismatch);
7102 j = i.imm_operands + (t->operands > i.imm_operands + 1);
7103 if (i.suffix == LONG_MNEM_SUFFIX
7104 && !cpu_arch_flags.bitfield.cpui386
7105 && (intel_syntax
7106 ? (t->opcode_modifier.mnemonicsize != IGNORESIZE
7107 && !intel_float_operand (insn_name (t)))
7108 : intel_float_operand (insn_name (t)) != 2)
7109 && (t->operands == i.imm_operands
7110 || (operand_types[i.imm_operands].bitfield.class != RegMMX
7111 && operand_types[i.imm_operands].bitfield.class != RegSIMD
7112 && operand_types[i.imm_operands].bitfield.class != RegMask)
7113 || (operand_types[j].bitfield.class != RegMMX
7114 && operand_types[j].bitfield.class != RegSIMD
7115 && operand_types[j].bitfield.class != RegMask))
7116 && !t->opcode_modifier.sib)
7117 continue;
7118
7119 /* Do not verify operands when there are none. */
7120 if (!t->operands)
7121 {
7122 if (VEX_check_encoding (t))
7123 {
7124 specific_error = progress (i.error);
7125 continue;
7126 }
7127
7128 /* We've found a match; break out of loop. */
7129 break;
7130 }
7131
7132 if (!t->opcode_modifier.jump
7133 || t->opcode_modifier.jump == JUMP_ABSOLUTE)
7134 {
7135 /* There should be only one Disp operand. */
7136 for (j = 0; j < MAX_OPERANDS; j++)
7137 if (operand_type_check (operand_types[j], disp))
7138 break;
7139 if (j < MAX_OPERANDS)
7140 {
7141 bool override = (i.prefix[ADDR_PREFIX] != 0);
7142
7143 addr_prefix_disp = j;
7144
7145 /* Address size prefix will turn Disp64 operand into Disp32 and
7146 Disp32/Disp16 one into Disp16/Disp32 respectively. */
7147 switch (flag_code)
7148 {
7149 case CODE_16BIT:
7150 override = !override;
7151 /* Fall through. */
7152 case CODE_32BIT:
7153 if (operand_types[j].bitfield.disp32
7154 && operand_types[j].bitfield.disp16)
7155 {
7156 operand_types[j].bitfield.disp16 = override;
7157 operand_types[j].bitfield.disp32 = !override;
7158 }
7159 gas_assert (!operand_types[j].bitfield.disp64);
7160 break;
7161
7162 case CODE_64BIT:
7163 if (operand_types[j].bitfield.disp64)
7164 {
7165 gas_assert (!operand_types[j].bitfield.disp32);
7166 operand_types[j].bitfield.disp32 = override;
7167 operand_types[j].bitfield.disp64 = !override;
7168 }
7169 operand_types[j].bitfield.disp16 = 0;
7170 break;
7171 }
7172 }
7173 }
7174
7175 /* We check register size if needed. */
7176 if (t->opcode_modifier.checkoperandsize)
7177 {
7178 check_register = (1 << t->operands) - 1;
7179 if (i.broadcast.type || i.broadcast.bytes)
7180 check_register &= ~(1 << i.broadcast.operand);
7181 }
7182 else
7183 check_register = 0;
7184
7185 overlap0 = operand_type_and (i.types[0], operand_types[0]);
7186 switch (t->operands)
7187 {
7188 case 1:
7189 if (!operand_type_match (overlap0, i.types[0]))
7190 continue;
7191
7192 /* Allow the ModR/M encoding to be requested by using the {load} or
7193 {store} pseudo prefix on an applicable insn. */
7194 if (!t->opcode_modifier.modrm
7195 && i.reg_operands == 1
7196 && ((i.dir_encoding == dir_encoding_load
7197 && t->mnem_off != MN_pop)
7198 || (i.dir_encoding == dir_encoding_store
7199 && t->mnem_off != MN_push))
7200 /* Avoid BSWAP. */
7201 && t->mnem_off != MN_bswap)
7202 continue;
7203 break;
7204
7205 case 2:
7206 /* xchg %eax, %eax is a special case. It is an alias for nop
7207 only in 32bit mode and we can use opcode 0x90. In 64bit
7208 mode, we can't use 0x90 for xchg %eax, %eax since it should
7209 zero-extend %eax to %rax. */
7210 if (t->base_opcode == 0x90
7211 && t->opcode_space == SPACE_BASE)
7212 {
7213 if (flag_code == CODE_64BIT
7214 && i.types[0].bitfield.instance == Accum
7215 && i.types[0].bitfield.dword
7216 && i.types[1].bitfield.instance == Accum)
7217 continue;
7218
7219 /* Allow the ModR/M encoding to be requested by using the
7220 {load} or {store} pseudo prefix. */
7221 if (i.dir_encoding == dir_encoding_load
7222 || i.dir_encoding == dir_encoding_store)
7223 continue;
7224 }
7225
7226 if (t->base_opcode == MOV_AX_DISP32
7227 && t->opcode_space == SPACE_BASE
7228 && t->mnem_off != MN_movabs)
7229 {
7230 /* Force 0x8b encoding for "mov foo@GOT, %eax". */
7231 if (i.reloc[0] == BFD_RELOC_386_GOT32)
7232 continue;
7233
7234 /* xrelease mov %eax, <disp> is another special case. It must not
7235 match the accumulator-only encoding of mov. */
7236 if (i.hle_prefix)
7237 continue;
7238
7239 /* Allow the ModR/M encoding to be requested by using a suitable
7240 {load} or {store} pseudo prefix. */
7241 if (i.dir_encoding == (i.types[0].bitfield.instance == Accum
7242 ? dir_encoding_store
7243 : dir_encoding_load)
7244 && !i.types[0].bitfield.disp64
7245 && !i.types[1].bitfield.disp64)
7246 continue;
7247 }
7248
7249 /* Allow the ModR/M encoding to be requested by using the {load} or
7250 {store} pseudo prefix on an applicable insn. */
7251 if (!t->opcode_modifier.modrm
7252 && i.reg_operands == 1
7253 && i.imm_operands == 1
7254 && (i.dir_encoding == dir_encoding_load
7255 || i.dir_encoding == dir_encoding_store)
7256 && t->opcode_space == SPACE_BASE)
7257 {
7258 if (t->base_opcode == 0xb0 /* mov $imm, %reg */
7259 && i.dir_encoding == dir_encoding_store)
7260 continue;
7261
7262 if ((t->base_opcode | 0x38) == 0x3c /* <alu> $imm, %acc */
7263 && (t->base_opcode != 0x3c /* cmp $imm, %acc */
7264 || i.dir_encoding == dir_encoding_load))
7265 continue;
7266
7267 if (t->base_opcode == 0xa8 /* test $imm, %acc */
7268 && i.dir_encoding == dir_encoding_load)
7269 continue;
7270 }
7271 /* Fall through. */
7272
7273 case 3:
7274 if (!(size_match & MATCH_STRAIGHT))
7275 goto check_reverse;
7276 /* Reverse direction of operands if swapping is possible in the first
7277 place (operands need to be symmetric) and
7278 - the load form is requested, and the template is a store form,
7279 - the store form is requested, and the template is a load form,
7280 - the non-default (swapped) form is requested. */
7281 overlap1 = operand_type_and (operand_types[0], operand_types[1]);
7282 if (t->opcode_modifier.d && i.reg_operands == i.operands
7283 && !operand_type_all_zero (&overlap1))
7284 switch (i.dir_encoding)
7285 {
7286 case dir_encoding_load:
7287 if (operand_type_check (operand_types[i.operands - 1], anymem)
7288 || t->opcode_modifier.regmem)
7289 goto check_reverse;
7290 break;
7291
7292 case dir_encoding_store:
7293 if (!operand_type_check (operand_types[i.operands - 1], anymem)
7294 && !t->opcode_modifier.regmem)
7295 goto check_reverse;
7296 break;
7297
7298 case dir_encoding_swap:
7299 goto check_reverse;
7300
7301 case dir_encoding_default:
7302 break;
7303 }
7304 /* If we want store form, we skip the current load. */
7305 if ((i.dir_encoding == dir_encoding_store
7306 || i.dir_encoding == dir_encoding_swap)
7307 && i.mem_operands == 0
7308 && t->opcode_modifier.load)
7309 continue;
7310 /* Fall through. */
7311 case 4:
7312 case 5:
7313 overlap1 = operand_type_and (i.types[1], operand_types[1]);
7314 if (!operand_type_match (overlap0, i.types[0])
7315 || !operand_type_match (overlap1, i.types[1])
7316 || ((check_register & 3) == 3
7317 && !operand_type_register_match (i.types[0],
7318 operand_types[0],
7319 i.types[1],
7320 operand_types[1])))
7321 {
7322 specific_error = progress (i.error);
7323
7324 /* Check if other direction is valid ... */
7325 if (!t->opcode_modifier.d)
7326 continue;
7327
7328 check_reverse:
7329 if (!(size_match & MATCH_REVERSE))
7330 continue;
7331 /* Try reversing direction of operands. */
7332 j = is_cpu (t, CpuFMA4)
7333 || is_cpu (t, CpuXOP) ? 1 : i.operands - 1;
7334 overlap0 = operand_type_and (i.types[0], operand_types[j]);
7335 overlap1 = operand_type_and (i.types[j], operand_types[0]);
7336 overlap2 = operand_type_and (i.types[1], operand_types[1]);
7337 gas_assert (t->operands != 3 || !check_register);
7338 if (!operand_type_match (overlap0, i.types[0])
7339 || !operand_type_match (overlap1, i.types[j])
7340 || (t->operands == 3
7341 && !operand_type_match (overlap2, i.types[1]))
7342 || (check_register
7343 && !operand_type_register_match (i.types[0],
7344 operand_types[j],
7345 i.types[j],
7346 operand_types[0])))
7347 {
7348 /* Does not match either direction. */
7349 specific_error = progress (i.error);
7350 continue;
7351 }
7352 /* found_reverse_match holds which variant of D
7353 we've found. */
7354 if (!t->opcode_modifier.d)
7355 found_reverse_match = 0;
7356 else if (operand_types[0].bitfield.tbyte)
7357 {
7358 if (t->opcode_modifier.operandconstraint != UGH)
7359 found_reverse_match = Opcode_FloatD;
7360 else
7361 found_reverse_match = ~0;
7362 /* FSUB{,R} and FDIV{,R} may need a 2nd bit flipped. */
7363 if ((t->extension_opcode & 4)
7364 && (intel_syntax || intel_mnemonic))
7365 found_reverse_match |= Opcode_FloatR;
7366 }
7367 else if (is_cpu (t, CpuFMA4) || is_cpu (t, CpuXOP))
7368 {
7369 found_reverse_match = Opcode_VexW;
7370 goto check_operands_345;
7371 }
7372 else if (t->opcode_space != SPACE_BASE
7373 && (t->opcode_space != SPACE_0F
7374 /* MOV to/from CR/DR/TR, as an exception, follow
7375 the base opcode space encoding model. */
7376 || (t->base_opcode | 7) != 0x27))
7377 found_reverse_match = (t->base_opcode & 0xee) != 0x6e
7378 ? Opcode_ExtD : Opcode_SIMD_IntD;
7379 else if (!t->opcode_modifier.commutative)
7380 found_reverse_match = Opcode_D;
7381 else
7382 found_reverse_match = ~0;
7383 }
7384 else
7385 {
7386 /* Found a forward 2 operand match here. */
7387 check_operands_345:
7388 switch (t->operands)
7389 {
7390 case 5:
7391 overlap4 = operand_type_and (i.types[4], operand_types[4]);
7392 if (!operand_type_match (overlap4, i.types[4])
7393 || !operand_type_register_match (i.types[3],
7394 operand_types[3],
7395 i.types[4],
7396 operand_types[4]))
7397 {
7398 specific_error = progress (i.error);
7399 continue;
7400 }
7401 /* Fall through. */
7402 case 4:
7403 overlap3 = operand_type_and (i.types[3], operand_types[3]);
7404 if (!operand_type_match (overlap3, i.types[3])
7405 || ((check_register & 0xa) == 0xa
7406 && !operand_type_register_match (i.types[1],
7407 operand_types[1],
7408 i.types[3],
7409 operand_types[3]))
7410 || ((check_register & 0xc) == 0xc
7411 && !operand_type_register_match (i.types[2],
7412 operand_types[2],
7413 i.types[3],
7414 operand_types[3])))
7415 {
7416 specific_error = progress (i.error);
7417 continue;
7418 }
7419 /* Fall through. */
7420 case 3:
7421 overlap2 = operand_type_and (i.types[2], operand_types[2]);
7422 if (!operand_type_match (overlap2, i.types[2])
7423 || ((check_register & 5) == 5
7424 && !operand_type_register_match (i.types[0],
7425 operand_types[0],
7426 i.types[2],
7427 operand_types[2]))
7428 || ((check_register & 6) == 6
7429 && !operand_type_register_match (i.types[1],
7430 operand_types[1],
7431 i.types[2],
7432 operand_types[2])))
7433 {
7434 specific_error = progress (i.error);
7435 continue;
7436 }
7437 break;
7438 }
7439 }
7440 /* Found either forward/reverse 2, 3 or 4 operand match here:
7441 slip through to break. */
7442 }
7443
7444 /* Check if VEX/EVEX encoding requirements can be satisfied. */
7445 if (VEX_check_encoding (t))
7446 {
7447 specific_error = progress (i.error);
7448 continue;
7449 }
7450
7451 /* Check if vector operands are valid. */
7452 if (check_VecOperands (t))
7453 {
7454 specific_error = progress (i.error);
7455 continue;
7456 }
7457
7458 /* We've found a match; break out of loop. */
7459 break;
7460 }
7461
7462 #undef progress
7463
7464 if (t == current_templates->end)
7465 {
7466 /* We found no match. */
7467 i.error = specific_error;
7468 return NULL;
7469 }
7470
7471 if (!quiet_warnings)
7472 {
7473 if (!intel_syntax
7474 && (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE)))
7475 as_warn (_("indirect %s without `*'"), insn_name (t));
7476
7477 if (t->opcode_modifier.isprefix
7478 && t->opcode_modifier.mnemonicsize == IGNORESIZE)
7479 {
7480 /* Warn them that a data or address size prefix doesn't
7481 affect assembly of the next line of code. */
7482 as_warn (_("stand-alone `%s' prefix"), insn_name (t));
7483 }
7484 }
7485
7486 /* Copy the template we found. */
7487 install_template (t);
7488
7489 if (addr_prefix_disp != -1)
7490 i.tm.operand_types[addr_prefix_disp]
7491 = operand_types[addr_prefix_disp];
7492
7493 switch (found_reverse_match)
7494 {
7495 case 0:
7496 break;
7497
7498 case Opcode_FloatR:
7499 case Opcode_FloatR | Opcode_FloatD:
7500 i.tm.extension_opcode ^= Opcode_FloatR >> 3;
7501 found_reverse_match &= Opcode_FloatD;
7502
7503 /* Fall through. */
7504 default:
7505 /* If we found a reverse match we must alter the opcode direction
7506 bit and clear/flip the regmem modifier one. found_reverse_match
7507 holds bits to change (different for int & float insns). */
7508
7509 i.tm.base_opcode ^= found_reverse_match;
7510
7511 /* Certain SIMD insns have their load forms specified in the opcode
7512 table, and hence we need to _set_ RegMem instead of clearing it.
7513 We need to avoid setting the bit though on insns like KMOVW. */
7514 i.tm.opcode_modifier.regmem
7515 = i.tm.opcode_modifier.modrm && i.tm.opcode_modifier.d
7516 && i.tm.operands > 2U - i.tm.opcode_modifier.sse2avx
7517 && !i.tm.opcode_modifier.regmem;
7518
7519 /* Fall through. */
7520 case ~0:
7521 i.tm.operand_types[0] = operand_types[i.operands - 1];
7522 i.tm.operand_types[i.operands - 1] = operand_types[0];
7523 break;
7524
7525 case Opcode_VexW:
7526 /* Only the first two register operands need reversing, alongside
7527 flipping VEX.W. */
7528 i.tm.opcode_modifier.vexw ^= VEXW0 ^ VEXW1;
7529
7530 j = i.tm.operand_types[0].bitfield.imm8;
7531 i.tm.operand_types[j] = operand_types[j + 1];
7532 i.tm.operand_types[j + 1] = operand_types[j];
7533 break;
7534 }
7535
7536 return t;
7537 }
7538
7539 static int
7540 check_string (void)
7541 {
7542 unsigned int es_op = i.tm.opcode_modifier.isstring - IS_STRING_ES_OP0;
7543 unsigned int op = i.tm.operand_types[0].bitfield.baseindex ? es_op : 0;
7544
7545 if (i.seg[op] != NULL && i.seg[op] != reg_es)
7546 {
7547 as_bad (_("`%s' operand %u must use `%ses' segment"),
7548 insn_name (&i.tm),
7549 intel_syntax ? i.tm.operands - es_op : es_op + 1,
7550 register_prefix);
7551 return 0;
7552 }
7553
7554 /* There's only ever one segment override allowed per instruction.
7555 This instruction possibly has a legal segment override on the
7556 second operand, so copy the segment to where non-string
7557 instructions store it, allowing common code. */
7558 i.seg[op] = i.seg[1];
7559
7560 return 1;
7561 }
7562
7563 static int
7564 process_suffix (void)
7565 {
7566 bool is_movx = false;
7567
7568 /* If matched instruction specifies an explicit instruction mnemonic
7569 suffix, use it. */
7570 if (i.tm.opcode_modifier.size == SIZE16)
7571 i.suffix = WORD_MNEM_SUFFIX;
7572 else if (i.tm.opcode_modifier.size == SIZE32)
7573 i.suffix = LONG_MNEM_SUFFIX;
7574 else if (i.tm.opcode_modifier.size == SIZE64)
7575 i.suffix = QWORD_MNEM_SUFFIX;
7576 else if (i.reg_operands
7577 && (i.operands > 1 || i.types[0].bitfield.class == Reg)
7578 && i.tm.opcode_modifier.operandconstraint != ADDR_PREFIX_OP_REG)
7579 {
7580 unsigned int numop = i.operands;
7581
7582 /* MOVSX/MOVZX */
7583 is_movx = (i.tm.opcode_space == SPACE_0F
7584 && (i.tm.base_opcode | 8) == 0xbe)
7585 || (i.tm.opcode_space == SPACE_BASE
7586 && i.tm.base_opcode == 0x63
7587 && is_cpu (&i.tm, Cpu64));
7588
7589 /* movsx/movzx want only their source operand considered here, for the
7590 ambiguity checking below. The suffix will be replaced afterwards
7591 to represent the destination (register). */
7592 if (is_movx && (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63))
7593 --i.operands;
7594
7595 /* crc32 needs REX.W set regardless of suffix / source operand size. */
7596 if (i.tm.mnem_off == MN_crc32 && i.tm.operand_types[1].bitfield.qword)
7597 i.rex |= REX_W;
7598
7599 /* If there's no instruction mnemonic suffix we try to invent one
7600 based on GPR operands. */
7601 if (!i.suffix)
7602 {
7603 /* We take i.suffix from the last register operand specified,
7604 Destination register type is more significant than source
7605 register type. crc32 in SSE4.2 prefers source register
7606 type. */
7607 unsigned int op = i.tm.mnem_off == MN_crc32 ? 1 : i.operands;
7608
7609 while (op--)
7610 if (i.tm.operand_types[op].bitfield.instance == InstanceNone
7611 || i.tm.operand_types[op].bitfield.instance == Accum)
7612 {
7613 if (i.types[op].bitfield.class != Reg)
7614 continue;
7615 if (i.types[op].bitfield.byte)
7616 i.suffix = BYTE_MNEM_SUFFIX;
7617 else if (i.types[op].bitfield.word)
7618 i.suffix = WORD_MNEM_SUFFIX;
7619 else if (i.types[op].bitfield.dword)
7620 i.suffix = LONG_MNEM_SUFFIX;
7621 else if (i.types[op].bitfield.qword)
7622 i.suffix = QWORD_MNEM_SUFFIX;
7623 else
7624 continue;
7625 break;
7626 }
7627
7628 /* As an exception, movsx/movzx silently default to a byte source
7629 in AT&T mode. */
7630 if (is_movx && i.tm.opcode_modifier.w && !i.suffix && !intel_syntax)
7631 i.suffix = BYTE_MNEM_SUFFIX;
7632 }
7633 else if (i.suffix == BYTE_MNEM_SUFFIX)
7634 {
7635 if (!check_byte_reg ())
7636 return 0;
7637 }
7638 else if (i.suffix == LONG_MNEM_SUFFIX)
7639 {
7640 if (!check_long_reg ())
7641 return 0;
7642 }
7643 else if (i.suffix == QWORD_MNEM_SUFFIX)
7644 {
7645 if (!check_qword_reg ())
7646 return 0;
7647 }
7648 else if (i.suffix == WORD_MNEM_SUFFIX)
7649 {
7650 if (!check_word_reg ())
7651 return 0;
7652 }
7653 else if (intel_syntax
7654 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
7655 /* Do nothing if the instruction is going to ignore the prefix. */
7656 ;
7657 else
7658 abort ();
7659
7660 /* Undo the movsx/movzx change done above. */
7661 i.operands = numop;
7662 }
7663 else if (i.tm.opcode_modifier.mnemonicsize == DEFAULTSIZE
7664 && !i.suffix)
7665 {
7666 i.suffix = stackop_size;
7667 if (stackop_size == LONG_MNEM_SUFFIX)
7668 {
7669 /* stackop_size is set to LONG_MNEM_SUFFIX for the
7670 .code16gcc directive to support 16-bit mode with
7671 32-bit address. For IRET without a suffix, generate
7672 16-bit IRET (opcode 0xcf) to return from an interrupt
7673 handler. */
7674 if (i.tm.base_opcode == 0xcf)
7675 {
7676 i.suffix = WORD_MNEM_SUFFIX;
7677 as_warn (_("generating 16-bit `iret' for .code16gcc directive"));
7678 }
7679 /* Warn about changed behavior for segment register push/pop. */
7680 else if ((i.tm.base_opcode | 1) == 0x07)
7681 as_warn (_("generating 32-bit `%s', unlike earlier gas versions"),
7682 insn_name (&i.tm));
7683 }
7684 }
7685 else if (!i.suffix
7686 && (i.tm.opcode_modifier.jump == JUMP_ABSOLUTE
7687 || i.tm.opcode_modifier.jump == JUMP_BYTE
7688 || i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT
7689 || (i.tm.opcode_space == SPACE_0F
7690 && i.tm.base_opcode == 0x01 /* [ls][gi]dt */
7691 && i.tm.extension_opcode <= 3)))
7692 {
7693 switch (flag_code)
7694 {
7695 case CODE_64BIT:
7696 if (!i.tm.opcode_modifier.no_qsuf)
7697 {
7698 if (i.tm.opcode_modifier.jump == JUMP_BYTE
7699 || i.tm.opcode_modifier.no_lsuf)
7700 i.suffix = QWORD_MNEM_SUFFIX;
7701 break;
7702 }
7703 /* Fall through. */
7704 case CODE_32BIT:
7705 if (!i.tm.opcode_modifier.no_lsuf)
7706 i.suffix = LONG_MNEM_SUFFIX;
7707 break;
7708 case CODE_16BIT:
7709 if (!i.tm.opcode_modifier.no_wsuf)
7710 i.suffix = WORD_MNEM_SUFFIX;
7711 break;
7712 }
7713 }
7714
7715 if (!i.suffix
7716 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
7717 /* Also cover lret/retf/iret in 64-bit mode. */
7718 || (flag_code == CODE_64BIT
7719 && !i.tm.opcode_modifier.no_lsuf
7720 && !i.tm.opcode_modifier.no_qsuf))
7721 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
7722 /* Explicit sizing prefixes are assumed to disambiguate insns. */
7723 && !i.prefix[DATA_PREFIX] && !(i.prefix[REX_PREFIX] & REX_W)
7724 /* Accept FLDENV et al without suffix. */
7725 && (i.tm.opcode_modifier.no_ssuf || i.tm.opcode_modifier.floatmf))
7726 {
7727 unsigned int suffixes, evex = 0;
7728
7729 suffixes = !i.tm.opcode_modifier.no_bsuf;
7730 if (!i.tm.opcode_modifier.no_wsuf)
7731 suffixes |= 1 << 1;
7732 if (!i.tm.opcode_modifier.no_lsuf)
7733 suffixes |= 1 << 2;
7734 if (!i.tm.opcode_modifier.no_ssuf)
7735 suffixes |= 1 << 4;
7736 if (flag_code == CODE_64BIT && !i.tm.opcode_modifier.no_qsuf)
7737 suffixes |= 1 << 5;
7738
7739 /* For [XYZ]MMWORD operands inspect operand sizes. While generally
7740 also suitable for AT&T syntax mode, it was requested that this be
7741 restricted to just Intel syntax. */
7742 if (intel_syntax && is_any_vex_encoding (&i.tm)
7743 && !i.broadcast.type && !i.broadcast.bytes)
7744 {
7745 unsigned int op;
7746
7747 for (op = 0; op < i.tm.operands; ++op)
7748 {
7749 if (vector_size < VSZ512)
7750 {
7751 i.tm.operand_types[op].bitfield.zmmword = 0;
7752 if (vector_size < VSZ256)
7753 {
7754 i.tm.operand_types[op].bitfield.ymmword = 0;
7755 if (i.tm.operand_types[op].bitfield.xmmword
7756 && (i.tm.opcode_modifier.evex == EVEXDYN
7757 || (!i.tm.opcode_modifier.evex
7758 && is_evex_encoding (&i.tm))))
7759 i.tm.opcode_modifier.evex = EVEX128;
7760 }
7761 else if (i.tm.operand_types[op].bitfield.ymmword
7762 && !i.tm.operand_types[op].bitfield.xmmword
7763 && (i.tm.opcode_modifier.evex == EVEXDYN
7764 || (!i.tm.opcode_modifier.evex
7765 && is_evex_encoding (&i.tm))))
7766 i.tm.opcode_modifier.evex = EVEX256;
7767 }
7768 else if (is_evex_encoding (&i.tm)
7769 && !cpu_arch_flags.bitfield.cpuavx512vl)
7770 {
7771 if (i.tm.operand_types[op].bitfield.ymmword)
7772 i.tm.operand_types[op].bitfield.xmmword = 0;
7773 if (i.tm.operand_types[op].bitfield.zmmword)
7774 i.tm.operand_types[op].bitfield.ymmword = 0;
7775 if (!i.tm.opcode_modifier.evex
7776 || i.tm.opcode_modifier.evex == EVEXDYN)
7777 i.tm.opcode_modifier.evex = EVEX512;
7778 }
7779
7780 if (i.tm.operand_types[op].bitfield.xmmword
7781 + i.tm.operand_types[op].bitfield.ymmword
7782 + i.tm.operand_types[op].bitfield.zmmword < 2)
7783 continue;
7784
7785 /* Any properly sized operand disambiguates the insn. */
7786 if (i.types[op].bitfield.xmmword
7787 || i.types[op].bitfield.ymmword
7788 || i.types[op].bitfield.zmmword)
7789 {
7790 suffixes &= ~(7 << 6);
7791 evex = 0;
7792 break;
7793 }
7794
7795 if ((i.flags[op] & Operand_Mem)
7796 && i.tm.operand_types[op].bitfield.unspecified)
7797 {
7798 if (i.tm.operand_types[op].bitfield.xmmword)
7799 suffixes |= 1 << 6;
7800 if (i.tm.operand_types[op].bitfield.ymmword)
7801 suffixes |= 1 << 7;
7802 if (i.tm.operand_types[op].bitfield.zmmword)
7803 suffixes |= 1 << 8;
7804 if (is_evex_encoding (&i.tm))
7805 evex = EVEX512;
7806 }
7807 }
7808 }
7809
7810 /* Are multiple suffixes / operand sizes allowed? */
7811 if (suffixes & (suffixes - 1))
7812 {
7813 if (intel_syntax
7814 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
7815 || operand_check == check_error))
7816 {
7817 as_bad (_("ambiguous operand size for `%s'"), insn_name (&i.tm));
7818 return 0;
7819 }
7820 if (operand_check == check_error)
7821 {
7822 as_bad (_("no instruction mnemonic suffix given and "
7823 "no register operands; can't size `%s'"), insn_name (&i.tm));
7824 return 0;
7825 }
7826 if (operand_check == check_warning)
7827 as_warn (_("%s; using default for `%s'"),
7828 intel_syntax
7829 ? _("ambiguous operand size")
7830 : _("no instruction mnemonic suffix given and "
7831 "no register operands"),
7832 insn_name (&i.tm));
7833
7834 if (i.tm.opcode_modifier.floatmf)
7835 i.suffix = SHORT_MNEM_SUFFIX;
7836 else if (is_movx)
7837 /* handled below */;
7838 else if (evex)
7839 i.tm.opcode_modifier.evex = evex;
7840 else if (flag_code == CODE_16BIT)
7841 i.suffix = WORD_MNEM_SUFFIX;
7842 else if (!i.tm.opcode_modifier.no_lsuf)
7843 i.suffix = LONG_MNEM_SUFFIX;
7844 else
7845 i.suffix = QWORD_MNEM_SUFFIX;
7846 }
7847 }
7848
7849 if (is_movx)
7850 {
7851 /* In Intel syntax, movsx/movzx must have a "suffix" (checked above).
7852 In AT&T syntax, if there is no suffix (warned about above), the default
7853 will be byte extension. */
7854 if (i.tm.opcode_modifier.w && i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
7855 i.tm.base_opcode |= 1;
7856
7857 /* For further processing, the suffix should represent the destination
7858 (register). This is already the case when one was used with
7859 mov[sz][bw]*, but we need to replace it for mov[sz]x, or if there was
7860 no suffix to begin with. */
7861 if (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63 || !i.suffix)
7862 {
7863 if (i.types[1].bitfield.word)
7864 i.suffix = WORD_MNEM_SUFFIX;
7865 else if (i.types[1].bitfield.qword)
7866 i.suffix = QWORD_MNEM_SUFFIX;
7867 else
7868 i.suffix = LONG_MNEM_SUFFIX;
7869
7870 i.tm.opcode_modifier.w = 0;
7871 }
7872 }
7873
7874 if (!i.tm.opcode_modifier.modrm && i.reg_operands && i.tm.operands < 3)
7875 i.short_form = (i.tm.operand_types[0].bitfield.class == Reg)
7876 != (i.tm.operand_types[1].bitfield.class == Reg);
7877
7878 /* Change the opcode based on the operand size given by i.suffix. */
7879 switch (i.suffix)
7880 {
7881 /* Size floating point instruction. */
7882 case LONG_MNEM_SUFFIX:
7883 if (i.tm.opcode_modifier.floatmf)
7884 {
7885 i.tm.base_opcode ^= 4;
7886 break;
7887 }
7888 /* fall through */
7889 case WORD_MNEM_SUFFIX:
7890 case QWORD_MNEM_SUFFIX:
7891 /* It's not a byte, select word/dword operation. */
7892 if (i.tm.opcode_modifier.w)
7893 {
7894 if (i.short_form)
7895 i.tm.base_opcode |= 8;
7896 else
7897 i.tm.base_opcode |= 1;
7898 }
7899 /* fall through */
7900 case SHORT_MNEM_SUFFIX:
7901 /* Now select between word & dword operations via the operand
7902 size prefix, except for instructions that will ignore this
7903 prefix anyway. */
7904 if (i.suffix != QWORD_MNEM_SUFFIX
7905 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
7906 && !i.tm.opcode_modifier.floatmf
7907 && !is_any_vex_encoding (&i.tm)
7908 && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
7909 || (flag_code == CODE_64BIT
7910 && i.tm.opcode_modifier.jump == JUMP_BYTE)))
7911 {
7912 unsigned int prefix = DATA_PREFIX_OPCODE;
7913
7914 if (i.tm.opcode_modifier.jump == JUMP_BYTE) /* jcxz, loop */
7915 prefix = ADDR_PREFIX_OPCODE;
7916
7917 if (!add_prefix (prefix))
7918 return 0;
7919 }
7920
7921 /* Set mode64 for an operand. */
7922 if (i.suffix == QWORD_MNEM_SUFFIX
7923 && flag_code == CODE_64BIT
7924 && !i.tm.opcode_modifier.norex64
7925 && !i.tm.opcode_modifier.vexw
7926 /* Special case for xchg %rax,%rax. It is NOP and doesn't
7927 need rex64. */
7928 && ! (i.operands == 2
7929 && i.tm.base_opcode == 0x90
7930 && i.tm.opcode_space == SPACE_BASE
7931 && i.types[0].bitfield.instance == Accum
7932 && i.types[0].bitfield.qword
7933 && i.types[1].bitfield.instance == Accum))
7934 i.rex |= REX_W;
7935
7936 break;
7937
7938 case 0:
7939 /* Select word/dword/qword operation with explicit data sizing prefix
7940 when there are no suitable register operands. */
7941 if (i.tm.opcode_modifier.w
7942 && (i.prefix[DATA_PREFIX] || (i.prefix[REX_PREFIX] & REX_W))
7943 && (!i.reg_operands
7944 || (i.reg_operands == 1
7945 /* ShiftCount */
7946 && (i.tm.operand_types[0].bitfield.instance == RegC
7947 /* InOutPortReg */
7948 || i.tm.operand_types[0].bitfield.instance == RegD
7949 || i.tm.operand_types[1].bitfield.instance == RegD
7950 || i.tm.mnem_off == MN_crc32))))
7951 i.tm.base_opcode |= 1;
7952 break;
7953 }
7954
7955 if (i.tm.opcode_modifier.operandconstraint == ADDR_PREFIX_OP_REG)
7956 {
7957 gas_assert (!i.suffix);
7958 gas_assert (i.reg_operands);
7959
7960 if (i.tm.operand_types[0].bitfield.instance == Accum
7961 || i.operands == 1)
7962 {
7963 /* The address size override prefix changes the size of the
7964 first operand. */
7965 if (flag_code == CODE_64BIT
7966 && i.op[0].regs->reg_type.bitfield.word)
7967 {
7968 as_bad (_("16-bit addressing unavailable for `%s'"),
7969 insn_name (&i.tm));
7970 return 0;
7971 }
7972
7973 if ((flag_code == CODE_32BIT
7974 ? i.op[0].regs->reg_type.bitfield.word
7975 : i.op[0].regs->reg_type.bitfield.dword)
7976 && !add_prefix (ADDR_PREFIX_OPCODE))
7977 return 0;
7978 }
7979 else
7980 {
7981 /* Check invalid register operand when the address size override
7982 prefix changes the size of register operands. */
7983 unsigned int op;
7984 enum { need_word, need_dword, need_qword } need;
7985
7986 /* Check the register operand for the address size prefix if
7987 the memory operand has no real registers, like symbol, DISP
7988 or bogus (x32-only) symbol(%rip) when symbol(%eip) is meant. */
7989 if (i.mem_operands == 1
7990 && i.reg_operands == 1
7991 && i.operands == 2
7992 && i.types[1].bitfield.class == Reg
7993 && (flag_code == CODE_32BIT
7994 ? i.op[1].regs->reg_type.bitfield.word
7995 : i.op[1].regs->reg_type.bitfield.dword)
7996 && ((i.base_reg == NULL && i.index_reg == NULL)
7997 #if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
7998 || (x86_elf_abi == X86_64_X32_ABI
7999 && i.base_reg
8000 && i.base_reg->reg_num == RegIP
8001 && i.base_reg->reg_type.bitfield.qword))
8002 #else
8003 || 0)
8004 #endif
8005 && !add_prefix (ADDR_PREFIX_OPCODE))
8006 return 0;
8007
8008 if (flag_code == CODE_32BIT)
8009 need = i.prefix[ADDR_PREFIX] ? need_word : need_dword;
8010 else if (i.prefix[ADDR_PREFIX])
8011 need = need_dword;
8012 else
8013 need = flag_code == CODE_64BIT ? need_qword : need_word;
8014
8015 for (op = 0; op < i.operands; op++)
8016 {
8017 if (i.types[op].bitfield.class != Reg)
8018 continue;
8019
8020 switch (need)
8021 {
8022 case need_word:
8023 if (i.op[op].regs->reg_type.bitfield.word)
8024 continue;
8025 break;
8026 case need_dword:
8027 if (i.op[op].regs->reg_type.bitfield.dword)
8028 continue;
8029 break;
8030 case need_qword:
8031 if (i.op[op].regs->reg_type.bitfield.qword)
8032 continue;
8033 break;
8034 }
8035
8036 as_bad (_("invalid register operand size for `%s'"),
8037 insn_name (&i.tm));
8038 return 0;
8039 }
8040 }
8041 }
8042
8043 return 1;
8044 }
8045
8046 static int
8047 check_byte_reg (void)
8048 {
8049 int op;
8050
8051 for (op = i.operands; --op >= 0;)
8052 {
8053 /* Skip non-register operands. */
8054 if (i.types[op].bitfield.class != Reg)
8055 continue;
8056
8057 /* If this is an eight bit register, it's OK. If it's the 16 or
8058 32 bit version of an eight bit register, we will just use the
8059 low portion, and that's OK too. */
8060 if (i.types[op].bitfield.byte)
8061 continue;
8062
8063 /* I/O port address operands are OK too. */
8064 if (i.tm.operand_types[op].bitfield.instance == RegD
8065 && i.tm.operand_types[op].bitfield.word)
8066 continue;
8067
8068 /* crc32 only wants its source operand checked here. */
8069 if (i.tm.mnem_off == MN_crc32 && op != 0)
8070 continue;
8071
8072 /* Any other register is bad. */
8073 as_bad (_("`%s%s' not allowed with `%s%c'"),
8074 register_prefix, i.op[op].regs->reg_name,
8075 insn_name (&i.tm), i.suffix);
8076 return 0;
8077 }
8078 return 1;
8079 }
8080
8081 static int
8082 check_long_reg (void)
8083 {
8084 int op;
8085
8086 for (op = i.operands; --op >= 0;)
8087 /* Skip non-register operands. */
8088 if (i.types[op].bitfield.class != Reg)
8089 continue;
8090 /* Reject eight bit registers, except where the template requires
8091 them. (eg. movzb) */
8092 else if (i.types[op].bitfield.byte
8093 && (i.tm.operand_types[op].bitfield.class == Reg
8094 || i.tm.operand_types[op].bitfield.instance == Accum)
8095 && (i.tm.operand_types[op].bitfield.word
8096 || i.tm.operand_types[op].bitfield.dword))
8097 {
8098 as_bad (_("`%s%s' not allowed with `%s%c'"),
8099 register_prefix,
8100 i.op[op].regs->reg_name,
8101 insn_name (&i.tm),
8102 i.suffix);
8103 return 0;
8104 }
8105 /* Error if the e prefix on a general reg is missing. */
8106 else if (i.types[op].bitfield.word
8107 && (i.tm.operand_types[op].bitfield.class == Reg
8108 || i.tm.operand_types[op].bitfield.instance == Accum)
8109 && i.tm.operand_types[op].bitfield.dword)
8110 {
8111 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
8112 register_prefix, i.op[op].regs->reg_name,
8113 i.suffix);
8114 return 0;
8115 }
8116 /* Warn if the r prefix on a general reg is present. */
8117 else if (i.types[op].bitfield.qword
8118 && (i.tm.operand_types[op].bitfield.class == Reg
8119 || i.tm.operand_types[op].bitfield.instance == Accum)
8120 && i.tm.operand_types[op].bitfield.dword)
8121 {
8122 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
8123 register_prefix, i.op[op].regs->reg_name, i.suffix);
8124 return 0;
8125 }
8126 return 1;
8127 }
8128
8129 static int
8130 check_qword_reg (void)
8131 {
8132 int op;
8133
8134 for (op = i.operands; --op >= 0; )
8135 /* Skip non-register operands. */
8136 if (i.types[op].bitfield.class != Reg)
8137 continue;
8138 /* Reject eight bit registers, except where the template requires
8139 them. (eg. movzb) */
8140 else if (i.types[op].bitfield.byte
8141 && (i.tm.operand_types[op].bitfield.class == Reg
8142 || i.tm.operand_types[op].bitfield.instance == Accum)
8143 && (i.tm.operand_types[op].bitfield.word
8144 || i.tm.operand_types[op].bitfield.dword))
8145 {
8146 as_bad (_("`%s%s' not allowed with `%s%c'"),
8147 register_prefix,
8148 i.op[op].regs->reg_name,
8149 insn_name (&i.tm),
8150 i.suffix);
8151 return 0;
8152 }
8153 /* Warn if the r prefix on a general reg is missing. */
8154 else if ((i.types[op].bitfield.word
8155 || i.types[op].bitfield.dword)
8156 && (i.tm.operand_types[op].bitfield.class == Reg
8157 || i.tm.operand_types[op].bitfield.instance == Accum)
8158 && i.tm.operand_types[op].bitfield.qword)
8159 {
8160 /* Prohibit these changes in the 64bit mode, since the
8161 lowering is more complicated. */
8162 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
8163 register_prefix, i.op[op].regs->reg_name, i.suffix);
8164 return 0;
8165 }
8166 return 1;
8167 }
8168
8169 static int
8170 check_word_reg (void)
8171 {
8172 int op;
8173 for (op = i.operands; --op >= 0;)
8174 /* Skip non-register operands. */
8175 if (i.types[op].bitfield.class != Reg)
8176 continue;
8177 /* Reject eight bit registers, except where the template requires
8178 them. (eg. movzb) */
8179 else if (i.types[op].bitfield.byte
8180 && (i.tm.operand_types[op].bitfield.class == Reg
8181 || i.tm.operand_types[op].bitfield.instance == Accum)
8182 && (i.tm.operand_types[op].bitfield.word
8183 || i.tm.operand_types[op].bitfield.dword))
8184 {
8185 as_bad (_("`%s%s' not allowed with `%s%c'"),
8186 register_prefix,
8187 i.op[op].regs->reg_name,
8188 insn_name (&i.tm),
8189 i.suffix);
8190 return 0;
8191 }
8192 /* Error if the e or r prefix on a general reg is present. */
8193 else if ((i.types[op].bitfield.dword
8194 || i.types[op].bitfield.qword)
8195 && (i.tm.operand_types[op].bitfield.class == Reg
8196 || i.tm.operand_types[op].bitfield.instance == Accum)
8197 && i.tm.operand_types[op].bitfield.word)
8198 {
8199 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
8200 register_prefix, i.op[op].regs->reg_name,
8201 i.suffix);
8202 return 0;
8203 }
8204 return 1;
8205 }
8206
8207 static int
8208 update_imm (unsigned int j)
8209 {
8210 i386_operand_type overlap = i.types[j];
8211
8212 if (i.tm.operand_types[j].bitfield.imm8
8213 && i.tm.operand_types[j].bitfield.imm8s
8214 && overlap.bitfield.imm8 && overlap.bitfield.imm8s)
8215 {
8216 /* This combination is used on 8-bit immediates where e.g. $~0 is
8217 desirable to permit. We're past operand type matching, so simply
8218 put things back in the shape they were before introducing the
8219 distinction between Imm8, Imm8S, and Imm8|Imm8S. */
8220 overlap.bitfield.imm8s = 0;
8221 }
8222
8223 if (overlap.bitfield.imm8
8224 + overlap.bitfield.imm8s
8225 + overlap.bitfield.imm16
8226 + overlap.bitfield.imm32
8227 + overlap.bitfield.imm32s
8228 + overlap.bitfield.imm64 > 1)
8229 {
8230 static const i386_operand_type imm16 = { .bitfield = { .imm16 = 1 } };
8231 static const i386_operand_type imm32 = { .bitfield = { .imm32 = 1 } };
8232 static const i386_operand_type imm32s = { .bitfield = { .imm32s = 1 } };
8233 static const i386_operand_type imm16_32 = { .bitfield =
8234 { .imm16 = 1, .imm32 = 1 }
8235 };
8236 static const i386_operand_type imm16_32s = { .bitfield =
8237 { .imm16 = 1, .imm32s = 1 }
8238 };
8239 static const i386_operand_type imm16_32_32s = { .bitfield =
8240 { .imm16 = 1, .imm32 = 1, .imm32s = 1 }
8241 };
8242
8243 if (i.suffix)
8244 {
8245 i386_operand_type temp;
8246
8247 operand_type_set (&temp, 0);
8248 if (i.suffix == BYTE_MNEM_SUFFIX)
8249 {
8250 temp.bitfield.imm8 = overlap.bitfield.imm8;
8251 temp.bitfield.imm8s = overlap.bitfield.imm8s;
8252 }
8253 else if (i.suffix == WORD_MNEM_SUFFIX)
8254 temp.bitfield.imm16 = overlap.bitfield.imm16;
8255 else if (i.suffix == QWORD_MNEM_SUFFIX)
8256 {
8257 temp.bitfield.imm64 = overlap.bitfield.imm64;
8258 temp.bitfield.imm32s = overlap.bitfield.imm32s;
8259 }
8260 else
8261 temp.bitfield.imm32 = overlap.bitfield.imm32;
8262 overlap = temp;
8263 }
8264 else if (operand_type_equal (&overlap, &imm16_32_32s)
8265 || operand_type_equal (&overlap, &imm16_32)
8266 || operand_type_equal (&overlap, &imm16_32s))
8267 {
8268 if ((flag_code == CODE_16BIT)
8269 ^ (i.prefix[DATA_PREFIX] != 0 && !(i.prefix[REX_PREFIX] & REX_W)))
8270 overlap = imm16;
8271 else
8272 overlap = imm32s;
8273 }
8274 else if (i.prefix[REX_PREFIX] & REX_W)
8275 overlap = operand_type_and (overlap, imm32s);
8276 else if (i.prefix[DATA_PREFIX])
8277 overlap = operand_type_and (overlap,
8278 flag_code != CODE_16BIT ? imm16 : imm32);
8279 if (overlap.bitfield.imm8
8280 + overlap.bitfield.imm8s
8281 + overlap.bitfield.imm16
8282 + overlap.bitfield.imm32
8283 + overlap.bitfield.imm32s
8284 + overlap.bitfield.imm64 != 1)
8285 {
8286 as_bad (_("no instruction mnemonic suffix given; "
8287 "can't determine immediate size"));
8288 return 0;
8289 }
8290 }
8291 i.types[j] = overlap;
8292
8293 return 1;
8294 }
8295
8296 static int
8297 finalize_imm (void)
8298 {
8299 unsigned int j, n;
8300
8301 /* Update the first 2 immediate operands. */
8302 n = i.operands > 2 ? 2 : i.operands;
8303 if (n)
8304 {
8305 for (j = 0; j < n; j++)
8306 if (update_imm (j) == 0)
8307 return 0;
8308
8309 /* The 3rd operand can't be immediate operand. */
8310 gas_assert (operand_type_check (i.types[2], imm) == 0);
8311 }
8312
8313 return 1;
8314 }
8315
8316 static INLINE void set_rex_vrex (const reg_entry *r, unsigned int rex_bit,
8317 bool do_sse2avx)
8318 {
8319 if (r->reg_flags & RegRex)
8320 {
8321 if (i.rex & rex_bit)
8322 as_bad (_("same type of prefix used twice"));
8323 i.rex |= rex_bit;
8324 }
8325 else if (do_sse2avx && (i.rex & rex_bit) && i.vex.register_specifier)
8326 {
8327 gas_assert (i.vex.register_specifier == r);
8328 i.vex.register_specifier += 8;
8329 }
8330
8331 if (r->reg_flags & RegVRex)
8332 i.vrex |= rex_bit;
8333 }
8334
8335 static int
8336 process_operands (void)
8337 {
8338 /* Default segment register this instruction will use for memory
8339 accesses. 0 means unknown. This is only for optimizing out
8340 unnecessary segment overrides. */
8341 const reg_entry *default_seg = NULL;
8342
8343 /* We only need to check those implicit registers for instructions
8344 with 3 operands or less. */
8345 if (i.operands <= 3)
8346 for (unsigned int j = 0; j < i.operands; j++)
8347 if (i.types[j].bitfield.instance != InstanceNone)
8348 i.reg_operands--;
8349
8350 if (i.tm.opcode_modifier.sse2avx)
8351 {
8352 /* Legacy encoded insns allow explicit REX prefixes, so these prefixes
8353 need converting. */
8354 i.rex |= i.prefix[REX_PREFIX] & (REX_W | REX_R | REX_X | REX_B);
8355 i.prefix[REX_PREFIX] = 0;
8356 i.rex_encoding = 0;
8357 }
8358 /* ImmExt should be processed after SSE2AVX. */
8359 else if (i.tm.opcode_modifier.immext)
8360 process_immext ();
8361
8362 /* TILEZERO is unusual in that it has a single operand encoded in ModR/M.reg,
8363 not ModR/M.rm. To avoid special casing this in build_modrm_byte(), fake a
8364 new destination operand here, while converting the source one to register
8365 number 0. */
8366 if (i.tm.mnem_off == MN_tilezero)
8367 {
8368 i.op[1].regs = i.op[0].regs;
8369 i.op[0].regs -= i.op[0].regs->reg_num;
8370 i.types[1] = i.types[0];
8371 i.tm.operand_types[1] = i.tm.operand_types[0];
8372 i.flags[1] = i.flags[0];
8373 i.operands++;
8374 i.reg_operands++;
8375 i.tm.operands++;
8376 }
8377
8378 if (i.tm.opcode_modifier.sse2avx && i.tm.opcode_modifier.vexvvvv)
8379 {
8380 static const i386_operand_type regxmm = {
8381 .bitfield = { .class = RegSIMD, .xmmword = 1 }
8382 };
8383 unsigned int dupl = i.operands;
8384 unsigned int dest = dupl - 1;
8385 unsigned int j;
8386
8387 /* The destination must be an xmm register. */
8388 gas_assert (i.reg_operands
8389 && MAX_OPERANDS > dupl
8390 && operand_type_equal (&i.types[dest], &regxmm));
8391
8392 if (i.tm.operand_types[0].bitfield.instance == Accum
8393 && i.tm.operand_types[0].bitfield.xmmword)
8394 {
8395 /* Keep xmm0 for instructions with VEX prefix and 3
8396 sources. */
8397 i.tm.operand_types[0].bitfield.instance = InstanceNone;
8398 i.tm.operand_types[0].bitfield.class = RegSIMD;
8399 i.reg_operands++;
8400 goto duplicate;
8401 }
8402
8403 if (i.tm.opcode_modifier.operandconstraint == IMPLICIT_1ST_XMM0)
8404 {
8405 gas_assert ((MAX_OPERANDS - 1) > dupl);
8406
8407 /* Add the implicit xmm0 for instructions with VEX prefix
8408 and 3 sources. */
8409 for (j = i.operands; j > 0; j--)
8410 {
8411 i.op[j] = i.op[j - 1];
8412 i.types[j] = i.types[j - 1];
8413 i.tm.operand_types[j] = i.tm.operand_types[j - 1];
8414 i.flags[j] = i.flags[j - 1];
8415 }
8416 i.op[0].regs
8417 = (const reg_entry *) str_hash_find (reg_hash, "xmm0");
8418 i.types[0] = regxmm;
8419 i.tm.operand_types[0] = regxmm;
8420
8421 i.operands += 2;
8422 i.reg_operands += 2;
8423 i.tm.operands += 2;
8424
8425 dupl++;
8426 dest++;
8427 i.op[dupl] = i.op[dest];
8428 i.types[dupl] = i.types[dest];
8429 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8430 i.flags[dupl] = i.flags[dest];
8431 }
8432 else
8433 {
8434 duplicate:
8435 i.operands++;
8436 i.reg_operands++;
8437 i.tm.operands++;
8438
8439 i.op[dupl] = i.op[dest];
8440 i.types[dupl] = i.types[dest];
8441 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8442 i.flags[dupl] = i.flags[dest];
8443 }
8444
8445 if (i.tm.opcode_modifier.immext)
8446 process_immext ();
8447 }
8448 else if (i.tm.operand_types[0].bitfield.instance == Accum
8449 && i.tm.opcode_modifier.modrm)
8450 {
8451 unsigned int j;
8452
8453 for (j = 1; j < i.operands; j++)
8454 {
8455 i.op[j - 1] = i.op[j];
8456 i.types[j - 1] = i.types[j];
8457
8458 /* We need to adjust fields in i.tm since they are used by
8459 build_modrm_byte. */
8460 i.tm.operand_types [j - 1] = i.tm.operand_types [j];
8461
8462 i.flags[j - 1] = i.flags[j];
8463 }
8464
8465 /* No adjustment to i.reg_operands: This was already done at the top
8466 of the function. */
8467 i.operands--;
8468 i.tm.operands--;
8469 }
8470 else if (i.tm.opcode_modifier.operandconstraint == IMPLICIT_QUAD_GROUP)
8471 {
8472 unsigned int regnum, first_reg_in_group, last_reg_in_group;
8473
8474 /* The second operand must be {x,y,z}mmN, where N is a multiple of 4. */
8475 gas_assert (i.operands >= 2 && i.types[1].bitfield.class == RegSIMD);
8476 regnum = register_number (i.op[1].regs);
8477 first_reg_in_group = regnum & ~3;
8478 last_reg_in_group = first_reg_in_group + 3;
8479 if (regnum != first_reg_in_group)
8480 as_warn (_("source register `%s%s' implicitly denotes"
8481 " `%s%.3s%u' to `%s%.3s%u' source group in `%s'"),
8482 register_prefix, i.op[1].regs->reg_name,
8483 register_prefix, i.op[1].regs->reg_name, first_reg_in_group,
8484 register_prefix, i.op[1].regs->reg_name, last_reg_in_group,
8485 insn_name (&i.tm));
8486 }
8487 else if (i.tm.opcode_modifier.operandconstraint == REG_KLUDGE)
8488 {
8489 /* The imul $imm, %reg instruction is converted into
8490 imul $imm, %reg, %reg, and the clr %reg instruction
8491 is converted into xor %reg, %reg. */
8492
8493 unsigned int first_reg_op;
8494
8495 if (operand_type_check (i.types[0], reg))
8496 first_reg_op = 0;
8497 else
8498 first_reg_op = 1;
8499 /* Pretend we saw the extra register operand. */
8500 gas_assert (i.reg_operands == 1
8501 && i.op[first_reg_op + 1].regs == 0);
8502 i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
8503 i.types[first_reg_op + 1] = i.types[first_reg_op];
8504 i.operands++;
8505 i.reg_operands++;
8506 }
8507
8508 if (i.tm.opcode_modifier.modrm)
8509 {
8510 /* The opcode is completed (modulo i.tm.extension_opcode which
8511 must be put into the modrm byte). Now, we make the modrm and
8512 index base bytes based on all the info we've collected. */
8513
8514 default_seg = build_modrm_byte ();
8515
8516 if (!quiet_warnings && i.tm.opcode_modifier.operandconstraint == UGH)
8517 {
8518 /* Warn about some common errors, but press on regardless. */
8519 if (i.operands == 2)
8520 {
8521 /* Reversed arguments on faddp or fmulp. */
8522 as_warn (_("translating to `%s %s%s,%s%s'"), insn_name (&i.tm),
8523 register_prefix, i.op[!intel_syntax].regs->reg_name,
8524 register_prefix, i.op[intel_syntax].regs->reg_name);
8525 }
8526 else if (i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
8527 {
8528 /* Extraneous `l' suffix on fp insn. */
8529 as_warn (_("translating to `%s %s%s'"), insn_name (&i.tm),
8530 register_prefix, i.op[0].regs->reg_name);
8531 }
8532 }
8533 }
8534 else if (i.types[0].bitfield.class == SReg && !dot_insn ())
8535 {
8536 if (flag_code != CODE_64BIT
8537 ? i.tm.base_opcode == POP_SEG_SHORT
8538 && i.op[0].regs->reg_num == 1
8539 : (i.tm.base_opcode | 1) == (POP_SEG386_SHORT & 0xff)
8540 && i.op[0].regs->reg_num < 4)
8541 {
8542 as_bad (_("you can't `%s %s%s'"),
8543 insn_name (&i.tm), register_prefix, i.op[0].regs->reg_name);
8544 return 0;
8545 }
8546 if (i.op[0].regs->reg_num > 3
8547 && i.tm.opcode_space == SPACE_BASE )
8548 {
8549 i.tm.base_opcode ^= (POP_SEG_SHORT ^ POP_SEG386_SHORT) & 0xff;
8550 i.tm.opcode_space = SPACE_0F;
8551 }
8552 i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
8553 }
8554 else if (i.tm.opcode_space == SPACE_BASE
8555 && (i.tm.base_opcode & ~3) == MOV_AX_DISP32)
8556 {
8557 default_seg = reg_ds;
8558 }
8559 else if (i.tm.opcode_modifier.isstring)
8560 {
8561 /* For the string instructions that allow a segment override
8562 on one of their operands, the default segment is ds. */
8563 default_seg = reg_ds;
8564 }
8565 else if (i.short_form)
8566 {
8567 /* The register operand is in the 1st or 2nd non-immediate operand. */
8568 const reg_entry *r = i.op[i.imm_operands].regs;
8569
8570 if (!dot_insn ()
8571 && r->reg_type.bitfield.instance == Accum
8572 && i.op[i.imm_operands + 1].regs)
8573 r = i.op[i.imm_operands + 1].regs;
8574 /* Register goes in low 3 bits of opcode. */
8575 i.tm.base_opcode |= r->reg_num;
8576 set_rex_vrex (r, REX_B, false);
8577
8578 if (dot_insn () && i.reg_operands == 2)
8579 {
8580 gas_assert (is_any_vex_encoding (&i.tm)
8581 || i.vec_encoding != vex_encoding_default);
8582 i.vex.register_specifier = i.op[i.operands - 1].regs;
8583 }
8584 }
8585 else if (i.reg_operands == 1
8586 && !i.flags[i.operands - 1]
8587 && i.tm.operand_types[i.operands - 1].bitfield.instance
8588 == InstanceNone)
8589 {
8590 gas_assert (is_any_vex_encoding (&i.tm)
8591 || i.vec_encoding != vex_encoding_default);
8592 i.vex.register_specifier = i.op[i.operands - 1].regs;
8593 }
8594
8595 if ((i.seg[0] || i.prefix[SEG_PREFIX])
8596 && i.tm.mnem_off == MN_lea)
8597 {
8598 if (!quiet_warnings)
8599 as_warn (_("segment override on `%s' is ineffectual"), insn_name (&i.tm));
8600 if (optimize && !i.no_optimize)
8601 {
8602 i.seg[0] = NULL;
8603 i.prefix[SEG_PREFIX] = 0;
8604 }
8605 }
8606
8607 /* If a segment was explicitly specified, and the specified segment
8608 is neither the default nor the one already recorded from a prefix,
8609 use an opcode prefix to select it. If we never figured out what
8610 the default segment is, then default_seg will be zero at this
8611 point, and the specified segment prefix will always be used. */
8612 if (i.seg[0]
8613 && i.seg[0] != default_seg
8614 && i386_seg_prefixes[i.seg[0]->reg_num] != i.prefix[SEG_PREFIX])
8615 {
8616 if (!add_prefix (i386_seg_prefixes[i.seg[0]->reg_num]))
8617 return 0;
8618 }
8619 return 1;
8620 }
8621
8622 static const reg_entry *
8623 build_modrm_byte (void)
8624 {
8625 const reg_entry *default_seg = NULL;
8626 unsigned int source = i.imm_operands - i.tm.opcode_modifier.immext
8627 /* Compensate for kludge in md_assemble(). */
8628 + i.tm.operand_types[0].bitfield.imm1;
8629 unsigned int dest = i.operands - 1 - i.tm.opcode_modifier.immext;
8630 unsigned int v, op, reg_slot = ~0;
8631
8632 /* Accumulator (in particular %st), shift count (%cl), and alike need
8633 to be skipped just like immediate operands do. */
8634 if (i.tm.operand_types[source].bitfield.instance)
8635 ++source;
8636 while (i.tm.operand_types[dest].bitfield.instance)
8637 --dest;
8638
8639 for (op = source; op < i.operands; ++op)
8640 if (i.tm.operand_types[op].bitfield.baseindex)
8641 break;
8642
8643 if (i.reg_operands + i.mem_operands + (i.tm.extension_opcode != None) == 4)
8644 {
8645 expressionS *exp;
8646
8647 /* There are 2 kinds of instructions:
8648 1. 5 operands: 4 register operands or 3 register operands
8649 plus 1 memory operand plus one Imm4 operand, VexXDS, and
8650 VexW0 or VexW1. The destination must be either XMM, YMM or
8651 ZMM register.
8652 2. 4 operands: 4 register operands or 3 register operands
8653 plus 1 memory operand, with VexXDS.
8654 3. Other equivalent combinations when coming from s_insn(). */
8655 gas_assert (i.tm.opcode_modifier.vexvvvv
8656 && i.tm.opcode_modifier.vexw);
8657 gas_assert (dot_insn ()
8658 || i.tm.operand_types[dest].bitfield.class == RegSIMD);
8659
8660 /* Of the first two non-immediate operands the one with the template
8661 not allowing for a memory one is encoded in the immediate operand. */
8662 if (source == op)
8663 reg_slot = source + 1;
8664 else
8665 reg_slot = source++;
8666
8667 if (!dot_insn ())
8668 {
8669 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
8670 gas_assert (!(i.op[reg_slot].regs->reg_flags & RegVRex));
8671 }
8672 else
8673 gas_assert (i.tm.operand_types[reg_slot].bitfield.class != ClassNone);
8674
8675 if (i.imm_operands == 0)
8676 {
8677 /* When there is no immediate operand, generate an 8bit
8678 immediate operand to encode the first operand. */
8679 exp = &im_expressions[i.imm_operands++];
8680 i.op[i.operands].imms = exp;
8681 i.types[i.operands].bitfield.imm8 = 1;
8682 i.operands++;
8683
8684 exp->X_op = O_constant;
8685 }
8686 else
8687 {
8688 gas_assert (i.imm_operands == 1);
8689 gas_assert (fits_in_imm4 (i.op[0].imms->X_add_number));
8690 gas_assert (!i.tm.opcode_modifier.immext);
8691
8692 /* Turn on Imm8 again so that output_imm will generate it. */
8693 i.types[0].bitfield.imm8 = 1;
8694
8695 exp = i.op[0].imms;
8696 }
8697 exp->X_add_number |= register_number (i.op[reg_slot].regs)
8698 << (3 + !(is_evex_encoding (&i.tm)
8699 || i.vec_encoding == vex_encoding_evex));
8700 }
8701
8702 for (v = source + 1; v < dest; ++v)
8703 if (v != reg_slot)
8704 break;
8705 if (v >= dest)
8706 v = ~0;
8707 if (i.tm.extension_opcode != None)
8708 {
8709 if (dest != source)
8710 v = dest;
8711 dest = ~0;
8712 }
8713 gas_assert (source < dest);
8714 if (i.tm.opcode_modifier.operandconstraint == SWAP_SOURCES
8715 && source != op)
8716 {
8717 unsigned int tmp = source;
8718
8719 source = v;
8720 v = tmp;
8721 }
8722
8723 if (v < MAX_OPERANDS)
8724 {
8725 gas_assert (i.tm.opcode_modifier.vexvvvv);
8726 i.vex.register_specifier = i.op[v].regs;
8727 }
8728
8729 if (op < i.operands)
8730 {
8731 if (i.mem_operands)
8732 {
8733 unsigned int fake_zero_displacement = 0;
8734
8735 gas_assert (i.flags[op] & Operand_Mem);
8736
8737 if (i.tm.opcode_modifier.sib)
8738 {
8739 /* The index register of VSIB shouldn't be RegIZ. */
8740 if (i.tm.opcode_modifier.sib != SIBMEM
8741 && i.index_reg->reg_num == RegIZ)
8742 abort ();
8743
8744 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8745 if (!i.base_reg)
8746 {
8747 i.sib.base = NO_BASE_REGISTER;
8748 i.sib.scale = i.log2_scale_factor;
8749 i.types[op] = operand_type_and_not (i.types[op], anydisp);
8750 i.types[op].bitfield.disp32 = 1;
8751 }
8752
8753 /* Since the mandatory SIB always has index register, so
8754 the code logic remains unchanged. The non-mandatory SIB
8755 without index register is allowed and will be handled
8756 later. */
8757 if (i.index_reg)
8758 {
8759 if (i.index_reg->reg_num == RegIZ)
8760 i.sib.index = NO_INDEX_REGISTER;
8761 else
8762 i.sib.index = i.index_reg->reg_num;
8763 set_rex_vrex (i.index_reg, REX_X, false);
8764 }
8765 }
8766
8767 default_seg = reg_ds;
8768
8769 if (i.base_reg == 0)
8770 {
8771 i.rm.mode = 0;
8772 if (!i.disp_operands)
8773 fake_zero_displacement = 1;
8774 if (i.index_reg == 0)
8775 {
8776 /* Both check for VSIB and mandatory non-vector SIB. */
8777 gas_assert (!i.tm.opcode_modifier.sib
8778 || i.tm.opcode_modifier.sib == SIBMEM);
8779 /* Operand is just <disp> */
8780 i.types[op] = operand_type_and_not (i.types[op], anydisp);
8781 if (flag_code == CODE_64BIT)
8782 {
8783 /* 64bit mode overwrites the 32bit absolute
8784 addressing by RIP relative addressing and
8785 absolute addressing is encoded by one of the
8786 redundant SIB forms. */
8787 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8788 i.sib.base = NO_BASE_REGISTER;
8789 i.sib.index = NO_INDEX_REGISTER;
8790 i.types[op].bitfield.disp32 = 1;
8791 }
8792 else if ((flag_code == CODE_16BIT)
8793 ^ (i.prefix[ADDR_PREFIX] != 0))
8794 {
8795 i.rm.regmem = NO_BASE_REGISTER_16;
8796 i.types[op].bitfield.disp16 = 1;
8797 }
8798 else
8799 {
8800 i.rm.regmem = NO_BASE_REGISTER;
8801 i.types[op].bitfield.disp32 = 1;
8802 }
8803 }
8804 else if (!i.tm.opcode_modifier.sib)
8805 {
8806 /* !i.base_reg && i.index_reg */
8807 if (i.index_reg->reg_num == RegIZ)
8808 i.sib.index = NO_INDEX_REGISTER;
8809 else
8810 i.sib.index = i.index_reg->reg_num;
8811 i.sib.base = NO_BASE_REGISTER;
8812 i.sib.scale = i.log2_scale_factor;
8813 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8814 i.types[op] = operand_type_and_not (i.types[op], anydisp);
8815 i.types[op].bitfield.disp32 = 1;
8816 if ((i.index_reg->reg_flags & RegRex) != 0)
8817 i.rex |= REX_X;
8818 }
8819 }
8820 /* RIP addressing for 64bit mode. */
8821 else if (i.base_reg->reg_num == RegIP)
8822 {
8823 gas_assert (!i.tm.opcode_modifier.sib);
8824 i.rm.regmem = NO_BASE_REGISTER;
8825 i.types[op].bitfield.disp8 = 0;
8826 i.types[op].bitfield.disp16 = 0;
8827 i.types[op].bitfield.disp32 = 1;
8828 i.types[op].bitfield.disp64 = 0;
8829 i.flags[op] |= Operand_PCrel;
8830 if (! i.disp_operands)
8831 fake_zero_displacement = 1;
8832 }
8833 else if (i.base_reg->reg_type.bitfield.word)
8834 {
8835 gas_assert (!i.tm.opcode_modifier.sib);
8836 switch (i.base_reg->reg_num)
8837 {
8838 case 3: /* (%bx) */
8839 if (i.index_reg == 0)
8840 i.rm.regmem = 7;
8841 else /* (%bx,%si) -> 0, or (%bx,%di) -> 1 */
8842 i.rm.regmem = i.index_reg->reg_num - 6;
8843 break;
8844 case 5: /* (%bp) */
8845 default_seg = reg_ss;
8846 if (i.index_reg == 0)
8847 {
8848 i.rm.regmem = 6;
8849 if (operand_type_check (i.types[op], disp) == 0)
8850 {
8851 /* fake (%bp) into 0(%bp) */
8852 if (i.disp_encoding == disp_encoding_16bit)
8853 i.types[op].bitfield.disp16 = 1;
8854 else
8855 i.types[op].bitfield.disp8 = 1;
8856 fake_zero_displacement = 1;
8857 }
8858 }
8859 else /* (%bp,%si) -> 2, or (%bp,%di) -> 3 */
8860 i.rm.regmem = i.index_reg->reg_num - 6 + 2;
8861 break;
8862 default: /* (%si) -> 4 or (%di) -> 5 */
8863 i.rm.regmem = i.base_reg->reg_num - 6 + 4;
8864 }
8865 if (!fake_zero_displacement
8866 && !i.disp_operands
8867 && i.disp_encoding)
8868 {
8869 fake_zero_displacement = 1;
8870 if (i.disp_encoding == disp_encoding_8bit)
8871 i.types[op].bitfield.disp8 = 1;
8872 else
8873 i.types[op].bitfield.disp16 = 1;
8874 }
8875 i.rm.mode = mode_from_disp_size (i.types[op]);
8876 }
8877 else /* i.base_reg and 32/64 bit mode */
8878 {
8879 if (operand_type_check (i.types[op], disp))
8880 {
8881 i.types[op].bitfield.disp16 = 0;
8882 i.types[op].bitfield.disp64 = 0;
8883 i.types[op].bitfield.disp32 = 1;
8884 }
8885
8886 if (!i.tm.opcode_modifier.sib)
8887 i.rm.regmem = i.base_reg->reg_num;
8888 if ((i.base_reg->reg_flags & RegRex) != 0)
8889 i.rex |= REX_B;
8890 i.sib.base = i.base_reg->reg_num;
8891 /* x86-64 ignores REX prefix bit here to avoid decoder
8892 complications. */
8893 if (!(i.base_reg->reg_flags & RegRex)
8894 && (i.base_reg->reg_num == EBP_REG_NUM
8895 || i.base_reg->reg_num == ESP_REG_NUM))
8896 default_seg = reg_ss;
8897 if (i.base_reg->reg_num == 5 && i.disp_operands == 0)
8898 {
8899 fake_zero_displacement = 1;
8900 if (i.disp_encoding == disp_encoding_32bit)
8901 i.types[op].bitfield.disp32 = 1;
8902 else
8903 i.types[op].bitfield.disp8 = 1;
8904 }
8905 i.sib.scale = i.log2_scale_factor;
8906 if (i.index_reg == 0)
8907 {
8908 /* Only check for VSIB. */
8909 gas_assert (i.tm.opcode_modifier.sib != VECSIB128
8910 && i.tm.opcode_modifier.sib != VECSIB256
8911 && i.tm.opcode_modifier.sib != VECSIB512);
8912
8913 /* <disp>(%esp) becomes two byte modrm with no index
8914 register. We've already stored the code for esp
8915 in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
8916 Any base register besides %esp will not use the
8917 extra modrm byte. */
8918 i.sib.index = NO_INDEX_REGISTER;
8919 }
8920 else if (!i.tm.opcode_modifier.sib)
8921 {
8922 if (i.index_reg->reg_num == RegIZ)
8923 i.sib.index = NO_INDEX_REGISTER;
8924 else
8925 i.sib.index = i.index_reg->reg_num;
8926 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8927 if ((i.index_reg->reg_flags & RegRex) != 0)
8928 i.rex |= REX_X;
8929 }
8930
8931 if (i.disp_operands
8932 && (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
8933 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL))
8934 i.rm.mode = 0;
8935 else
8936 {
8937 if (!fake_zero_displacement
8938 && !i.disp_operands
8939 && i.disp_encoding)
8940 {
8941 fake_zero_displacement = 1;
8942 if (i.disp_encoding == disp_encoding_8bit)
8943 i.types[op].bitfield.disp8 = 1;
8944 else
8945 i.types[op].bitfield.disp32 = 1;
8946 }
8947 i.rm.mode = mode_from_disp_size (i.types[op]);
8948 }
8949 }
8950
8951 if (fake_zero_displacement)
8952 {
8953 /* Fakes a zero displacement assuming that i.types[op]
8954 holds the correct displacement size. */
8955 expressionS *exp;
8956
8957 gas_assert (i.op[op].disps == 0);
8958 exp = &disp_expressions[i.disp_operands++];
8959 i.op[op].disps = exp;
8960 exp->X_op = O_constant;
8961 exp->X_add_number = 0;
8962 exp->X_add_symbol = (symbolS *) 0;
8963 exp->X_op_symbol = (symbolS *) 0;
8964 }
8965 }
8966 else
8967 {
8968 i.rm.mode = 3;
8969 i.rm.regmem = i.op[op].regs->reg_num;
8970 set_rex_vrex (i.op[op].regs, REX_B, false);
8971 }
8972
8973 if (op == dest)
8974 dest = ~0;
8975 if (op == source)
8976 source = ~0;
8977 }
8978 else
8979 {
8980 i.rm.mode = 3;
8981 if (!i.tm.opcode_modifier.regmem)
8982 {
8983 gas_assert (source < MAX_OPERANDS);
8984 i.rm.regmem = i.op[source].regs->reg_num;
8985 set_rex_vrex (i.op[source].regs, REX_B,
8986 dest >= MAX_OPERANDS && i.tm.opcode_modifier.sse2avx);
8987 source = ~0;
8988 }
8989 else
8990 {
8991 gas_assert (dest < MAX_OPERANDS);
8992 i.rm.regmem = i.op[dest].regs->reg_num;
8993 set_rex_vrex (i.op[dest].regs, REX_B, i.tm.opcode_modifier.sse2avx);
8994 dest = ~0;
8995 }
8996 }
8997
8998 /* Fill in i.rm.reg field with extension opcode (if any) or the
8999 appropriate register. */
9000 if (i.tm.extension_opcode != None)
9001 i.rm.reg = i.tm.extension_opcode;
9002 else if (!i.tm.opcode_modifier.regmem && dest < MAX_OPERANDS)
9003 {
9004 i.rm.reg = i.op[dest].regs->reg_num;
9005 set_rex_vrex (i.op[dest].regs, REX_R, i.tm.opcode_modifier.sse2avx);
9006 }
9007 else
9008 {
9009 gas_assert (source < MAX_OPERANDS);
9010 i.rm.reg = i.op[source].regs->reg_num;
9011 set_rex_vrex (i.op[source].regs, REX_R, false);
9012 }
9013
9014 if (flag_code != CODE_64BIT && (i.rex & REX_R))
9015 {
9016 gas_assert (i.types[!i.tm.opcode_modifier.regmem].bitfield.class == RegCR);
9017 i.rex &= ~REX_R;
9018 add_prefix (LOCK_PREFIX_OPCODE);
9019 }
9020
9021 return default_seg;
9022 }
9023
9024 static INLINE void
9025 frag_opcode_byte (unsigned char byte)
9026 {
9027 if (now_seg != absolute_section)
9028 FRAG_APPEND_1_CHAR (byte);
9029 else
9030 ++abs_section_offset;
9031 }
9032
9033 static unsigned int
9034 flip_code16 (unsigned int code16)
9035 {
9036 gas_assert (i.tm.operands == 1);
9037
9038 return !(i.prefix[REX_PREFIX] & REX_W)
9039 && (code16 ? i.tm.operand_types[0].bitfield.disp32
9040 : i.tm.operand_types[0].bitfield.disp16)
9041 ? CODE16 : 0;
9042 }
9043
9044 static void
9045 output_branch (void)
9046 {
9047 char *p;
9048 int size;
9049 int code16;
9050 int prefix;
9051 relax_substateT subtype;
9052 symbolS *sym;
9053 offsetT off;
9054
9055 if (now_seg == absolute_section)
9056 {
9057 as_bad (_("relaxable branches not supported in absolute section"));
9058 return;
9059 }
9060
9061 code16 = flag_code == CODE_16BIT ? CODE16 : 0;
9062 size = i.disp_encoding > disp_encoding_8bit ? BIG : SMALL;
9063
9064 prefix = 0;
9065 if (i.prefix[DATA_PREFIX] != 0)
9066 {
9067 prefix = 1;
9068 i.prefixes -= 1;
9069 code16 ^= flip_code16(code16);
9070 }
9071 /* Pentium4 branch hints. */
9072 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
9073 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
9074 {
9075 prefix++;
9076 i.prefixes--;
9077 }
9078 if (i.prefix[REX_PREFIX] != 0)
9079 {
9080 prefix++;
9081 i.prefixes--;
9082 }
9083
9084 /* BND prefixed jump. */
9085 if (i.prefix[BND_PREFIX] != 0)
9086 {
9087 prefix++;
9088 i.prefixes--;
9089 }
9090
9091 if (i.prefixes != 0)
9092 as_warn (_("skipping prefixes on `%s'"), insn_name (&i.tm));
9093
9094 /* It's always a symbol; End frag & setup for relax.
9095 Make sure there is enough room in this frag for the largest
9096 instruction we may generate in md_convert_frag. This is 2
9097 bytes for the opcode and room for the prefix and largest
9098 displacement. */
9099 frag_grow (prefix + 2 + 4);
9100 /* Prefix and 1 opcode byte go in fr_fix. */
9101 p = frag_more (prefix + 1);
9102 if (i.prefix[DATA_PREFIX] != 0)
9103 *p++ = DATA_PREFIX_OPCODE;
9104 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
9105 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
9106 *p++ = i.prefix[SEG_PREFIX];
9107 if (i.prefix[BND_PREFIX] != 0)
9108 *p++ = BND_PREFIX_OPCODE;
9109 if (i.prefix[REX_PREFIX] != 0)
9110 *p++ = i.prefix[REX_PREFIX];
9111 *p = i.tm.base_opcode;
9112
9113 if ((unsigned char) *p == JUMP_PC_RELATIVE)
9114 subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, size);
9115 else if (cpu_arch_flags.bitfield.cpui386)
9116 subtype = ENCODE_RELAX_STATE (COND_JUMP, size);
9117 else
9118 subtype = ENCODE_RELAX_STATE (COND_JUMP86, size);
9119 subtype |= code16;
9120
9121 sym = i.op[0].disps->X_add_symbol;
9122 off = i.op[0].disps->X_add_number;
9123
9124 if (i.op[0].disps->X_op != O_constant
9125 && i.op[0].disps->X_op != O_symbol)
9126 {
9127 /* Handle complex expressions. */
9128 sym = make_expr_symbol (i.op[0].disps);
9129 off = 0;
9130 }
9131
9132 frag_now->tc_frag_data.code64 = flag_code == CODE_64BIT;
9133
9134 /* 1 possible extra opcode + 4 byte displacement go in var part.
9135 Pass reloc in fr_var. */
9136 frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
9137 }
9138
9139 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9140 /* Return TRUE iff PLT32 relocation should be used for branching to
9141 symbol S. */
9142
9143 static bool
9144 need_plt32_p (symbolS *s)
9145 {
9146 /* PLT32 relocation is ELF only. */
9147 if (!IS_ELF)
9148 return false;
9149
9150 #ifdef TE_SOLARIS
9151 /* Don't emit PLT32 relocation on Solaris: neither native linker nor
9152 krtld support it. */
9153 return false;
9154 #endif
9155
9156 /* Since there is no need to prepare for PLT branch on x86-64, we
9157 can generate R_X86_64_PLT32, instead of R_X86_64_PC32, which can
9158 be used as a marker for 32-bit PC-relative branches. */
9159 if (!object_64bit)
9160 return false;
9161
9162 if (s == NULL)
9163 return false;
9164
9165 /* Weak or undefined symbol need PLT32 relocation. */
9166 if (S_IS_WEAK (s) || !S_IS_DEFINED (s))
9167 return true;
9168
9169 /* Non-global symbol doesn't need PLT32 relocation. */
9170 if (! S_IS_EXTERNAL (s))
9171 return false;
9172
9173 /* Other global symbols need PLT32 relocation. NB: Symbol with
9174 non-default visibilities are treated as normal global symbol
9175 so that PLT32 relocation can be used as a marker for 32-bit
9176 PC-relative branches. It is useful for linker relaxation. */
9177 return true;
9178 }
9179 #endif
9180
9181 static void
9182 output_jump (void)
9183 {
9184 char *p;
9185 int size;
9186 fixS *fixP;
9187 bfd_reloc_code_real_type jump_reloc = i.reloc[0];
9188
9189 if (i.tm.opcode_modifier.jump == JUMP_BYTE)
9190 {
9191 /* This is a loop or jecxz type instruction. */
9192 size = 1;
9193 if (i.prefix[ADDR_PREFIX] != 0)
9194 {
9195 frag_opcode_byte (ADDR_PREFIX_OPCODE);
9196 i.prefixes -= 1;
9197 }
9198 /* Pentium4 branch hints. */
9199 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
9200 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
9201 {
9202 frag_opcode_byte (i.prefix[SEG_PREFIX]);
9203 i.prefixes--;
9204 }
9205 }
9206 else
9207 {
9208 int code16;
9209
9210 code16 = 0;
9211 if (flag_code == CODE_16BIT)
9212 code16 = CODE16;
9213
9214 if (i.prefix[DATA_PREFIX] != 0)
9215 {
9216 frag_opcode_byte (DATA_PREFIX_OPCODE);
9217 i.prefixes -= 1;
9218 code16 ^= flip_code16(code16);
9219 }
9220
9221 size = 4;
9222 if (code16)
9223 size = 2;
9224 }
9225
9226 /* BND prefixed jump. */
9227 if (i.prefix[BND_PREFIX] != 0)
9228 {
9229 frag_opcode_byte (i.prefix[BND_PREFIX]);
9230 i.prefixes -= 1;
9231 }
9232
9233 if (i.prefix[REX_PREFIX] != 0)
9234 {
9235 frag_opcode_byte (i.prefix[REX_PREFIX]);
9236 i.prefixes -= 1;
9237 }
9238
9239 if (i.prefixes != 0)
9240 as_warn (_("skipping prefixes on `%s'"), insn_name (&i.tm));
9241
9242 if (now_seg == absolute_section)
9243 {
9244 abs_section_offset += i.opcode_length + size;
9245 return;
9246 }
9247
9248 p = frag_more (i.opcode_length + size);
9249 switch (i.opcode_length)
9250 {
9251 case 2:
9252 *p++ = i.tm.base_opcode >> 8;
9253 /* Fall through. */
9254 case 1:
9255 *p++ = i.tm.base_opcode;
9256 break;
9257 default:
9258 abort ();
9259 }
9260
9261 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9262 if (flag_code == CODE_64BIT && size == 4
9263 && jump_reloc == NO_RELOC && i.op[0].disps->X_add_number == 0
9264 && need_plt32_p (i.op[0].disps->X_add_symbol))
9265 jump_reloc = BFD_RELOC_X86_64_PLT32;
9266 #endif
9267
9268 jump_reloc = reloc (size, 1, 1, jump_reloc);
9269
9270 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
9271 i.op[0].disps, 1, jump_reloc);
9272
9273 /* All jumps handled here are signed, but don't unconditionally use a
9274 signed limit check for 32 and 16 bit jumps as we want to allow wrap
9275 around at 4G (outside of 64-bit mode) and 64k (except for XBEGIN)
9276 respectively. */
9277 switch (size)
9278 {
9279 case 1:
9280 fixP->fx_signed = 1;
9281 break;
9282
9283 case 2:
9284 if (i.tm.mnem_off == MN_xbegin)
9285 fixP->fx_signed = 1;
9286 break;
9287
9288 case 4:
9289 if (flag_code == CODE_64BIT)
9290 fixP->fx_signed = 1;
9291 break;
9292 }
9293 }
9294
9295 static void
9296 output_interseg_jump (void)
9297 {
9298 char *p;
9299 int size;
9300 int prefix;
9301 int code16;
9302
9303 code16 = 0;
9304 if (flag_code == CODE_16BIT)
9305 code16 = CODE16;
9306
9307 prefix = 0;
9308 if (i.prefix[DATA_PREFIX] != 0)
9309 {
9310 prefix = 1;
9311 i.prefixes -= 1;
9312 code16 ^= CODE16;
9313 }
9314
9315 gas_assert (!i.prefix[REX_PREFIX]);
9316
9317 size = 4;
9318 if (code16)
9319 size = 2;
9320
9321 if (i.prefixes != 0)
9322 as_warn (_("skipping prefixes on `%s'"), insn_name (&i.tm));
9323
9324 if (now_seg == absolute_section)
9325 {
9326 abs_section_offset += prefix + 1 + 2 + size;
9327 return;
9328 }
9329
9330 /* 1 opcode; 2 segment; offset */
9331 p = frag_more (prefix + 1 + 2 + size);
9332
9333 if (i.prefix[DATA_PREFIX] != 0)
9334 *p++ = DATA_PREFIX_OPCODE;
9335
9336 if (i.prefix[REX_PREFIX] != 0)
9337 *p++ = i.prefix[REX_PREFIX];
9338
9339 *p++ = i.tm.base_opcode;
9340 if (i.op[1].imms->X_op == O_constant)
9341 {
9342 offsetT n = i.op[1].imms->X_add_number;
9343
9344 if (size == 2
9345 && !fits_in_unsigned_word (n)
9346 && !fits_in_signed_word (n))
9347 {
9348 as_bad (_("16-bit jump out of range"));
9349 return;
9350 }
9351 md_number_to_chars (p, n, size);
9352 }
9353 else
9354 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
9355 i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
9356
9357 p += size;
9358 if (i.op[0].imms->X_op == O_constant)
9359 md_number_to_chars (p, (valueT) i.op[0].imms->X_add_number, 2);
9360 else
9361 fix_new_exp (frag_now, p - frag_now->fr_literal, 2,
9362 i.op[0].imms, 0, reloc (2, 0, 0, i.reloc[0]));
9363 }
9364
9365 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9366 void
9367 x86_cleanup (void)
9368 {
9369 char *p;
9370 asection *seg = now_seg;
9371 subsegT subseg = now_subseg;
9372 asection *sec;
9373 unsigned int alignment, align_size_1;
9374 unsigned int isa_1_descsz, feature_2_descsz, descsz;
9375 unsigned int isa_1_descsz_raw, feature_2_descsz_raw;
9376 unsigned int padding;
9377
9378 if (!IS_ELF || !x86_used_note)
9379 return;
9380
9381 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X86;
9382
9383 /* The .note.gnu.property section layout:
9384
9385 Field Length Contents
9386 ---- ---- ----
9387 n_namsz 4 4
9388 n_descsz 4 The note descriptor size
9389 n_type 4 NT_GNU_PROPERTY_TYPE_0
9390 n_name 4 "GNU"
9391 n_desc n_descsz The program property array
9392 .... .... ....
9393 */
9394
9395 /* Create the .note.gnu.property section. */
9396 sec = subseg_new (NOTE_GNU_PROPERTY_SECTION_NAME, 0);
9397 bfd_set_section_flags (sec,
9398 (SEC_ALLOC
9399 | SEC_LOAD
9400 | SEC_DATA
9401 | SEC_HAS_CONTENTS
9402 | SEC_READONLY));
9403
9404 if (get_elf_backend_data (stdoutput)->s->elfclass == ELFCLASS64)
9405 {
9406 align_size_1 = 7;
9407 alignment = 3;
9408 }
9409 else
9410 {
9411 align_size_1 = 3;
9412 alignment = 2;
9413 }
9414
9415 bfd_set_section_alignment (sec, alignment);
9416 elf_section_type (sec) = SHT_NOTE;
9417
9418 /* GNU_PROPERTY_X86_ISA_1_USED: 4-byte type + 4-byte data size
9419 + 4-byte data */
9420 isa_1_descsz_raw = 4 + 4 + 4;
9421 /* Align GNU_PROPERTY_X86_ISA_1_USED. */
9422 isa_1_descsz = (isa_1_descsz_raw + align_size_1) & ~align_size_1;
9423
9424 feature_2_descsz_raw = isa_1_descsz;
9425 /* GNU_PROPERTY_X86_FEATURE_2_USED: 4-byte type + 4-byte data size
9426 + 4-byte data */
9427 feature_2_descsz_raw += 4 + 4 + 4;
9428 /* Align GNU_PROPERTY_X86_FEATURE_2_USED. */
9429 feature_2_descsz = ((feature_2_descsz_raw + align_size_1)
9430 & ~align_size_1);
9431
9432 descsz = feature_2_descsz;
9433 /* Section size: n_namsz + n_descsz + n_type + n_name + n_descsz. */
9434 p = frag_more (4 + 4 + 4 + 4 + descsz);
9435
9436 /* Write n_namsz. */
9437 md_number_to_chars (p, (valueT) 4, 4);
9438
9439 /* Write n_descsz. */
9440 md_number_to_chars (p + 4, (valueT) descsz, 4);
9441
9442 /* Write n_type. */
9443 md_number_to_chars (p + 4 * 2, (valueT) NT_GNU_PROPERTY_TYPE_0, 4);
9444
9445 /* Write n_name. */
9446 memcpy (p + 4 * 3, "GNU", 4);
9447
9448 /* Write 4-byte type. */
9449 md_number_to_chars (p + 4 * 4,
9450 (valueT) GNU_PROPERTY_X86_ISA_1_USED, 4);
9451
9452 /* Write 4-byte data size. */
9453 md_number_to_chars (p + 4 * 5, (valueT) 4, 4);
9454
9455 /* Write 4-byte data. */
9456 md_number_to_chars (p + 4 * 6, (valueT) x86_isa_1_used, 4);
9457
9458 /* Zero out paddings. */
9459 padding = isa_1_descsz - isa_1_descsz_raw;
9460 if (padding)
9461 memset (p + 4 * 7, 0, padding);
9462
9463 /* Write 4-byte type. */
9464 md_number_to_chars (p + isa_1_descsz + 4 * 4,
9465 (valueT) GNU_PROPERTY_X86_FEATURE_2_USED, 4);
9466
9467 /* Write 4-byte data size. */
9468 md_number_to_chars (p + isa_1_descsz + 4 * 5, (valueT) 4, 4);
9469
9470 /* Write 4-byte data. */
9471 md_number_to_chars (p + isa_1_descsz + 4 * 6,
9472 (valueT) x86_feature_2_used, 4);
9473
9474 /* Zero out paddings. */
9475 padding = feature_2_descsz - feature_2_descsz_raw;
9476 if (padding)
9477 memset (p + isa_1_descsz + 4 * 7, 0, padding);
9478
9479 /* We probably can't restore the current segment, for there likely
9480 isn't one yet... */
9481 if (seg && subseg)
9482 subseg_set (seg, subseg);
9483 }
9484
9485 bool
9486 x86_support_sframe_p (void)
9487 {
9488 /* At this time, SFrame stack trace is supported for AMD64 ABI only. */
9489 return (x86_elf_abi == X86_64_ABI);
9490 }
9491
9492 bool
9493 x86_sframe_ra_tracking_p (void)
9494 {
9495 /* In AMD64, return address is always stored on the stack at a fixed offset
9496 from the CFA (provided via x86_sframe_cfa_ra_offset ()).
9497 Do not track explicitly via an SFrame Frame Row Entry. */
9498 return false;
9499 }
9500
9501 offsetT
9502 x86_sframe_cfa_ra_offset (void)
9503 {
9504 gas_assert (x86_elf_abi == X86_64_ABI);
9505 return (offsetT) -8;
9506 }
9507
9508 unsigned char
9509 x86_sframe_get_abi_arch (void)
9510 {
9511 unsigned char sframe_abi_arch = 0;
9512
9513 if (x86_support_sframe_p ())
9514 {
9515 gas_assert (!target_big_endian);
9516 sframe_abi_arch = SFRAME_ABI_AMD64_ENDIAN_LITTLE;
9517 }
9518
9519 return sframe_abi_arch;
9520 }
9521
9522 #endif
9523
9524 static unsigned int
9525 encoding_length (const fragS *start_frag, offsetT start_off,
9526 const char *frag_now_ptr)
9527 {
9528 unsigned int len = 0;
9529
9530 if (start_frag != frag_now)
9531 {
9532 const fragS *fr = start_frag;
9533
9534 do {
9535 len += fr->fr_fix;
9536 fr = fr->fr_next;
9537 } while (fr && fr != frag_now);
9538 }
9539
9540 return len - start_off + (frag_now_ptr - frag_now->fr_literal);
9541 }
9542
9543 /* Return 1 for test, and, cmp, add, sub, inc and dec which may
9544 be macro-fused with conditional jumps.
9545 NB: If TEST/AND/CMP/ADD/SUB/INC/DEC is of RIP relative address,
9546 or is one of the following format:
9547
9548 cmp m, imm
9549 add m, imm
9550 sub m, imm
9551 test m, imm
9552 and m, imm
9553 inc m
9554 dec m
9555
9556 it is unfusible. */
9557
9558 static int
9559 maybe_fused_with_jcc_p (enum mf_cmp_kind* mf_cmp_p)
9560 {
9561 /* No RIP address. */
9562 if (i.base_reg && i.base_reg->reg_num == RegIP)
9563 return 0;
9564
9565 /* No opcodes outside of base encoding space. */
9566 if (i.tm.opcode_space != SPACE_BASE)
9567 return 0;
9568
9569 /* add, sub without add/sub m, imm. */
9570 if (i.tm.base_opcode <= 5
9571 || (i.tm.base_opcode >= 0x28 && i.tm.base_opcode <= 0x2d)
9572 || ((i.tm.base_opcode | 3) == 0x83
9573 && (i.tm.extension_opcode == 0x5
9574 || i.tm.extension_opcode == 0x0)))
9575 {
9576 *mf_cmp_p = mf_cmp_alu_cmp;
9577 return !(i.mem_operands && i.imm_operands);
9578 }
9579
9580 /* and without and m, imm. */
9581 if ((i.tm.base_opcode >= 0x20 && i.tm.base_opcode <= 0x25)
9582 || ((i.tm.base_opcode | 3) == 0x83
9583 && i.tm.extension_opcode == 0x4))
9584 {
9585 *mf_cmp_p = mf_cmp_test_and;
9586 return !(i.mem_operands && i.imm_operands);
9587 }
9588
9589 /* test without test m imm. */
9590 if ((i.tm.base_opcode | 1) == 0x85
9591 || (i.tm.base_opcode | 1) == 0xa9
9592 || ((i.tm.base_opcode | 1) == 0xf7
9593 && i.tm.extension_opcode == 0))
9594 {
9595 *mf_cmp_p = mf_cmp_test_and;
9596 return !(i.mem_operands && i.imm_operands);
9597 }
9598
9599 /* cmp without cmp m, imm. */
9600 if ((i.tm.base_opcode >= 0x38 && i.tm.base_opcode <= 0x3d)
9601 || ((i.tm.base_opcode | 3) == 0x83
9602 && (i.tm.extension_opcode == 0x7)))
9603 {
9604 *mf_cmp_p = mf_cmp_alu_cmp;
9605 return !(i.mem_operands && i.imm_operands);
9606 }
9607
9608 /* inc, dec without inc/dec m. */
9609 if ((is_cpu (&i.tm, CpuNo64)
9610 && (i.tm.base_opcode | 0xf) == 0x4f)
9611 || ((i.tm.base_opcode | 1) == 0xff
9612 && i.tm.extension_opcode <= 0x1))
9613 {
9614 *mf_cmp_p = mf_cmp_incdec;
9615 return !i.mem_operands;
9616 }
9617
9618 return 0;
9619 }
9620
9621 /* Return 1 if a FUSED_JCC_PADDING frag should be generated. */
9622
9623 static int
9624 add_fused_jcc_padding_frag_p (enum mf_cmp_kind* mf_cmp_p)
9625 {
9626 /* NB: Don't work with COND_JUMP86 without i386. */
9627 if (!align_branch_power
9628 || now_seg == absolute_section
9629 || !cpu_arch_flags.bitfield.cpui386
9630 || !(align_branch & align_branch_fused_bit))
9631 return 0;
9632
9633 if (maybe_fused_with_jcc_p (mf_cmp_p))
9634 {
9635 if (last_insn.kind == last_insn_other
9636 || last_insn.seg != now_seg)
9637 return 1;
9638 if (flag_debug)
9639 as_warn_where (last_insn.file, last_insn.line,
9640 _("`%s` skips -malign-branch-boundary on `%s`"),
9641 last_insn.name, insn_name (&i.tm));
9642 }
9643
9644 return 0;
9645 }
9646
9647 /* Return 1 if a BRANCH_PREFIX frag should be generated. */
9648
9649 static int
9650 add_branch_prefix_frag_p (void)
9651 {
9652 /* NB: Don't work with COND_JUMP86 without i386. Don't add prefix
9653 to PadLock instructions since they include prefixes in opcode. */
9654 if (!align_branch_power
9655 || !align_branch_prefix_size
9656 || now_seg == absolute_section
9657 || is_cpu (&i.tm, CpuPadLock)
9658 || !cpu_arch_flags.bitfield.cpui386)
9659 return 0;
9660
9661 /* Don't add prefix if it is a prefix or there is no operand in case
9662 that segment prefix is special. */
9663 if (!i.operands || i.tm.opcode_modifier.isprefix)
9664 return 0;
9665
9666 if (last_insn.kind == last_insn_other
9667 || last_insn.seg != now_seg)
9668 return 1;
9669
9670 if (flag_debug)
9671 as_warn_where (last_insn.file, last_insn.line,
9672 _("`%s` skips -malign-branch-boundary on `%s`"),
9673 last_insn.name, insn_name (&i.tm));
9674
9675 return 0;
9676 }
9677
9678 /* Return 1 if a BRANCH_PADDING frag should be generated. */
9679
9680 static int
9681 add_branch_padding_frag_p (enum align_branch_kind *branch_p,
9682 enum mf_jcc_kind *mf_jcc_p)
9683 {
9684 int add_padding;
9685
9686 /* NB: Don't work with COND_JUMP86 without i386. */
9687 if (!align_branch_power
9688 || now_seg == absolute_section
9689 || !cpu_arch_flags.bitfield.cpui386
9690 || i.tm.opcode_space != SPACE_BASE)
9691 return 0;
9692
9693 add_padding = 0;
9694
9695 /* Check for jcc and direct jmp. */
9696 if (i.tm.opcode_modifier.jump == JUMP)
9697 {
9698 if (i.tm.base_opcode == JUMP_PC_RELATIVE)
9699 {
9700 *branch_p = align_branch_jmp;
9701 add_padding = align_branch & align_branch_jmp_bit;
9702 }
9703 else
9704 {
9705 /* Because J<cc> and JN<cc> share same group in macro-fusible table,
9706 igore the lowest bit. */
9707 *mf_jcc_p = (i.tm.base_opcode & 0x0e) >> 1;
9708 *branch_p = align_branch_jcc;
9709 if ((align_branch & align_branch_jcc_bit))
9710 add_padding = 1;
9711 }
9712 }
9713 else if ((i.tm.base_opcode | 1) == 0xc3)
9714 {
9715 /* Near ret. */
9716 *branch_p = align_branch_ret;
9717 if ((align_branch & align_branch_ret_bit))
9718 add_padding = 1;
9719 }
9720 else
9721 {
9722 /* Check for indirect jmp, direct and indirect calls. */
9723 if (i.tm.base_opcode == 0xe8)
9724 {
9725 /* Direct call. */
9726 *branch_p = align_branch_call;
9727 if ((align_branch & align_branch_call_bit))
9728 add_padding = 1;
9729 }
9730 else if (i.tm.base_opcode == 0xff
9731 && (i.tm.extension_opcode == 2
9732 || i.tm.extension_opcode == 4))
9733 {
9734 /* Indirect call and jmp. */
9735 *branch_p = align_branch_indirect;
9736 if ((align_branch & align_branch_indirect_bit))
9737 add_padding = 1;
9738 }
9739
9740 if (add_padding
9741 && i.disp_operands
9742 && tls_get_addr
9743 && (i.op[0].disps->X_op == O_symbol
9744 || (i.op[0].disps->X_op == O_subtract
9745 && i.op[0].disps->X_op_symbol == GOT_symbol)))
9746 {
9747 symbolS *s = i.op[0].disps->X_add_symbol;
9748 /* No padding to call to global or undefined tls_get_addr. */
9749 if ((S_IS_EXTERNAL (s) || !S_IS_DEFINED (s))
9750 && strcmp (S_GET_NAME (s), tls_get_addr) == 0)
9751 return 0;
9752 }
9753 }
9754
9755 if (add_padding
9756 && last_insn.kind != last_insn_other
9757 && last_insn.seg == now_seg)
9758 {
9759 if (flag_debug)
9760 as_warn_where (last_insn.file, last_insn.line,
9761 _("`%s` skips -malign-branch-boundary on `%s`"),
9762 last_insn.name, insn_name (&i.tm));
9763 return 0;
9764 }
9765
9766 return add_padding;
9767 }
9768
9769 static void
9770 output_insn (void)
9771 {
9772 fragS *insn_start_frag;
9773 offsetT insn_start_off;
9774 fragS *fragP = NULL;
9775 enum align_branch_kind branch = align_branch_none;
9776 /* The initializer is arbitrary just to avoid uninitialized error.
9777 it's actually either assigned in add_branch_padding_frag_p
9778 or never be used. */
9779 enum mf_jcc_kind mf_jcc = mf_jcc_jo;
9780
9781 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9782 if (IS_ELF && x86_used_note && now_seg != absolute_section)
9783 {
9784 if ((i.xstate & xstate_tmm) == xstate_tmm
9785 || is_cpu (&i.tm, CpuAMX_TILE))
9786 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_TMM;
9787
9788 if (is_cpu (&i.tm, Cpu8087)
9789 || is_cpu (&i.tm, Cpu287)
9790 || is_cpu (&i.tm, Cpu387)
9791 || is_cpu (&i.tm, Cpu687)
9792 || is_cpu (&i.tm, CpuFISTTP))
9793 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X87;
9794
9795 if ((i.xstate & xstate_mmx)
9796 || i.tm.mnem_off == MN_emms
9797 || i.tm.mnem_off == MN_femms)
9798 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MMX;
9799
9800 if (i.index_reg)
9801 {
9802 if (i.index_reg->reg_type.bitfield.zmmword)
9803 i.xstate |= xstate_zmm;
9804 else if (i.index_reg->reg_type.bitfield.ymmword)
9805 i.xstate |= xstate_ymm;
9806 else if (i.index_reg->reg_type.bitfield.xmmword)
9807 i.xstate |= xstate_xmm;
9808 }
9809
9810 /* vzeroall / vzeroupper */
9811 if (i.tm.base_opcode == 0x77 && is_cpu (&i.tm, CpuAVX))
9812 i.xstate |= xstate_ymm;
9813
9814 if ((i.xstate & xstate_xmm)
9815 /* ldmxcsr / stmxcsr / vldmxcsr / vstmxcsr */
9816 || (i.tm.base_opcode == 0xae
9817 && (is_cpu (&i.tm, CpuSSE)
9818 || is_cpu (&i.tm, CpuAVX)))
9819 || is_cpu (&i.tm, CpuWideKL)
9820 || is_cpu (&i.tm, CpuKL))
9821 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XMM;
9822
9823 if ((i.xstate & xstate_ymm) == xstate_ymm)
9824 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_YMM;
9825 if ((i.xstate & xstate_zmm) == xstate_zmm)
9826 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_ZMM;
9827 if (i.mask.reg || (i.xstate & xstate_mask) == xstate_mask)
9828 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MASK;
9829 if (is_cpu (&i.tm, CpuFXSR))
9830 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_FXSR;
9831 if (is_cpu (&i.tm, CpuXsave))
9832 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVE;
9833 if (is_cpu (&i.tm, CpuXsaveopt))
9834 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT;
9835 if (is_cpu (&i.tm, CpuXSAVEC))
9836 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEC;
9837
9838 if (x86_feature_2_used
9839 || is_cpu (&i.tm, CpuCMOV)
9840 || is_cpu (&i.tm, CpuSYSCALL)
9841 || i.tm.mnem_off == MN_cmpxchg8b)
9842 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_BASELINE;
9843 if (is_cpu (&i.tm, CpuSSE3)
9844 || is_cpu (&i.tm, CpuSSSE3)
9845 || is_cpu (&i.tm, CpuSSE4_1)
9846 || is_cpu (&i.tm, CpuSSE4_2)
9847 || is_cpu (&i.tm, CpuCX16)
9848 || is_cpu (&i.tm, CpuPOPCNT)
9849 /* LAHF-SAHF insns in 64-bit mode. */
9850 || (flag_code == CODE_64BIT
9851 && (i.tm.base_opcode | 1) == 0x9f
9852 && i.tm.opcode_space == SPACE_BASE))
9853 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V2;
9854 if (is_cpu (&i.tm, CpuAVX)
9855 || is_cpu (&i.tm, CpuAVX2)
9856 /* Any VEX encoded insns execpt for AVX512F, AVX512BW, AVX512DQ,
9857 XOP, FMA4, LPW, TBM, and AMX. */
9858 || (i.tm.opcode_modifier.vex
9859 && !is_cpu (&i.tm, CpuAVX512F)
9860 && !is_cpu (&i.tm, CpuAVX512BW)
9861 && !is_cpu (&i.tm, CpuAVX512DQ)
9862 && !is_cpu (&i.tm, CpuXOP)
9863 && !is_cpu (&i.tm, CpuFMA4)
9864 && !is_cpu (&i.tm, CpuLWP)
9865 && !is_cpu (&i.tm, CpuTBM)
9866 && !(x86_feature_2_used & GNU_PROPERTY_X86_FEATURE_2_TMM))
9867 || is_cpu (&i.tm, CpuF16C)
9868 || is_cpu (&i.tm, CpuFMA)
9869 || is_cpu (&i.tm, CpuLZCNT)
9870 || is_cpu (&i.tm, CpuMovbe)
9871 || is_cpu (&i.tm, CpuXSAVES)
9872 || (x86_feature_2_used
9873 & (GNU_PROPERTY_X86_FEATURE_2_XSAVE
9874 | GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT
9875 | GNU_PROPERTY_X86_FEATURE_2_XSAVEC)) != 0)
9876 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V3;
9877 if (is_cpu (&i.tm, CpuAVX512F)
9878 || is_cpu (&i.tm, CpuAVX512BW)
9879 || is_cpu (&i.tm, CpuAVX512DQ)
9880 || is_cpu (&i.tm, CpuAVX512VL)
9881 /* Any EVEX encoded insns except for AVX512ER, AVX512PF,
9882 AVX512-4FMAPS, and AVX512-4VNNIW. */
9883 || (i.tm.opcode_modifier.evex
9884 && !is_cpu (&i.tm, CpuAVX512ER)
9885 && !is_cpu (&i.tm, CpuAVX512PF)
9886 && !is_cpu (&i.tm, CpuAVX512_4FMAPS)
9887 && !is_cpu (&i.tm, CpuAVX512_4VNNIW)))
9888 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V4;
9889 }
9890 #endif
9891
9892 /* Tie dwarf2 debug info to the address at the start of the insn.
9893 We can't do this after the insn has been output as the current
9894 frag may have been closed off. eg. by frag_var. */
9895 dwarf2_emit_insn (0);
9896
9897 insn_start_frag = frag_now;
9898 insn_start_off = frag_now_fix ();
9899
9900 if (add_branch_padding_frag_p (&branch, &mf_jcc))
9901 {
9902 char *p;
9903 /* Branch can be 8 bytes. Leave some room for prefixes. */
9904 unsigned int max_branch_padding_size = 14;
9905
9906 /* Align section to boundary. */
9907 record_alignment (now_seg, align_branch_power);
9908
9909 /* Make room for padding. */
9910 frag_grow (max_branch_padding_size);
9911
9912 /* Start of the padding. */
9913 p = frag_more (0);
9914
9915 fragP = frag_now;
9916
9917 frag_var (rs_machine_dependent, max_branch_padding_size, 0,
9918 ENCODE_RELAX_STATE (BRANCH_PADDING, 0),
9919 NULL, 0, p);
9920
9921 fragP->tc_frag_data.mf_type = mf_jcc;
9922 fragP->tc_frag_data.branch_type = branch;
9923 fragP->tc_frag_data.max_bytes = max_branch_padding_size;
9924 }
9925
9926 if (!cpu_arch_flags.bitfield.cpui386 && (flag_code != CODE_16BIT)
9927 && !pre_386_16bit_warned)
9928 {
9929 as_warn (_("use .code16 to ensure correct addressing mode"));
9930 pre_386_16bit_warned = true;
9931 }
9932
9933 /* Output jumps. */
9934 if (i.tm.opcode_modifier.jump == JUMP)
9935 output_branch ();
9936 else if (i.tm.opcode_modifier.jump == JUMP_BYTE
9937 || i.tm.opcode_modifier.jump == JUMP_DWORD)
9938 output_jump ();
9939 else if (i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT)
9940 output_interseg_jump ();
9941 else
9942 {
9943 /* Output normal instructions here. */
9944 char *p;
9945 unsigned char *q;
9946 unsigned int j;
9947 enum mf_cmp_kind mf_cmp;
9948
9949 if (avoid_fence
9950 && (i.tm.base_opcode == 0xaee8
9951 || i.tm.base_opcode == 0xaef0
9952 || i.tm.base_opcode == 0xaef8))
9953 {
9954 /* Encode lfence, mfence, and sfence as
9955 f0 83 04 24 00 lock addl $0x0, (%{re}sp). */
9956 if (flag_code == CODE_16BIT)
9957 as_bad (_("Cannot convert `%s' in 16-bit mode"), insn_name (&i.tm));
9958 else if (omit_lock_prefix)
9959 as_bad (_("Cannot convert `%s' with `-momit-lock-prefix=yes' in effect"),
9960 insn_name (&i.tm));
9961 else if (now_seg != absolute_section)
9962 {
9963 offsetT val = 0x240483f0ULL;
9964
9965 p = frag_more (5);
9966 md_number_to_chars (p, val, 5);
9967 }
9968 else
9969 abs_section_offset += 5;
9970 return;
9971 }
9972
9973 /* Some processors fail on LOCK prefix. This options makes
9974 assembler ignore LOCK prefix and serves as a workaround. */
9975 if (omit_lock_prefix)
9976 {
9977 if (i.tm.base_opcode == LOCK_PREFIX_OPCODE
9978 && i.tm.opcode_modifier.isprefix)
9979 return;
9980 i.prefix[LOCK_PREFIX] = 0;
9981 }
9982
9983 if (branch)
9984 /* Skip if this is a branch. */
9985 ;
9986 else if (add_fused_jcc_padding_frag_p (&mf_cmp))
9987 {
9988 /* Make room for padding. */
9989 frag_grow (MAX_FUSED_JCC_PADDING_SIZE);
9990 p = frag_more (0);
9991
9992 fragP = frag_now;
9993
9994 frag_var (rs_machine_dependent, MAX_FUSED_JCC_PADDING_SIZE, 0,
9995 ENCODE_RELAX_STATE (FUSED_JCC_PADDING, 0),
9996 NULL, 0, p);
9997
9998 fragP->tc_frag_data.mf_type = mf_cmp;
9999 fragP->tc_frag_data.branch_type = align_branch_fused;
10000 fragP->tc_frag_data.max_bytes = MAX_FUSED_JCC_PADDING_SIZE;
10001 }
10002 else if (add_branch_prefix_frag_p ())
10003 {
10004 unsigned int max_prefix_size = align_branch_prefix_size;
10005
10006 /* Make room for padding. */
10007 frag_grow (max_prefix_size);
10008 p = frag_more (0);
10009
10010 fragP = frag_now;
10011
10012 frag_var (rs_machine_dependent, max_prefix_size, 0,
10013 ENCODE_RELAX_STATE (BRANCH_PREFIX, 0),
10014 NULL, 0, p);
10015
10016 fragP->tc_frag_data.max_bytes = max_prefix_size;
10017 }
10018
10019 /* Since the VEX/EVEX prefix contains the implicit prefix, we
10020 don't need the explicit prefix. */
10021 if (!is_any_vex_encoding (&i.tm))
10022 {
10023 switch (i.tm.opcode_modifier.opcodeprefix)
10024 {
10025 case PREFIX_0X66:
10026 add_prefix (0x66);
10027 break;
10028 case PREFIX_0XF2:
10029 add_prefix (0xf2);
10030 break;
10031 case PREFIX_0XF3:
10032 if (!is_cpu (&i.tm, CpuPadLock)
10033 || (i.prefix[REP_PREFIX] != 0xf3))
10034 add_prefix (0xf3);
10035 break;
10036 case PREFIX_NONE:
10037 switch (i.opcode_length)
10038 {
10039 case 2:
10040 break;
10041 case 1:
10042 /* Check for pseudo prefixes. */
10043 if (!i.tm.opcode_modifier.isprefix || i.tm.base_opcode)
10044 break;
10045 as_bad_where (insn_start_frag->fr_file,
10046 insn_start_frag->fr_line,
10047 _("pseudo prefix without instruction"));
10048 return;
10049 default:
10050 abort ();
10051 }
10052 break;
10053 default:
10054 abort ();
10055 }
10056
10057 #if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
10058 /* For x32, add a dummy REX_OPCODE prefix for mov/add with
10059 R_X86_64_GOTTPOFF relocation so that linker can safely
10060 perform IE->LE optimization. A dummy REX_OPCODE prefix
10061 is also needed for lea with R_X86_64_GOTPC32_TLSDESC
10062 relocation for GDesc -> IE/LE optimization. */
10063 if (x86_elf_abi == X86_64_X32_ABI
10064 && i.operands == 2
10065 && (i.reloc[0] == BFD_RELOC_X86_64_GOTTPOFF
10066 || i.reloc[0] == BFD_RELOC_X86_64_GOTPC32_TLSDESC)
10067 && i.prefix[REX_PREFIX] == 0)
10068 add_prefix (REX_OPCODE);
10069 #endif
10070
10071 /* The prefix bytes. */
10072 for (j = ARRAY_SIZE (i.prefix), q = i.prefix; j > 0; j--, q++)
10073 if (*q)
10074 frag_opcode_byte (*q);
10075 }
10076 else
10077 {
10078 for (j = 0, q = i.prefix; j < ARRAY_SIZE (i.prefix); j++, q++)
10079 if (*q)
10080 switch (j)
10081 {
10082 case SEG_PREFIX:
10083 case ADDR_PREFIX:
10084 frag_opcode_byte (*q);
10085 break;
10086 default:
10087 /* There should be no other prefixes for instructions
10088 with VEX prefix. */
10089 abort ();
10090 }
10091
10092 /* For EVEX instructions i.vrex should become 0 after
10093 build_evex_prefix. For VEX instructions upper 16 registers
10094 aren't available, so VREX should be 0. */
10095 if (i.vrex)
10096 abort ();
10097 /* Now the VEX prefix. */
10098 if (now_seg != absolute_section)
10099 {
10100 p = frag_more (i.vex.length);
10101 for (j = 0; j < i.vex.length; j++)
10102 p[j] = i.vex.bytes[j];
10103 }
10104 else
10105 abs_section_offset += i.vex.length;
10106 }
10107
10108 /* Now the opcode; be careful about word order here! */
10109 j = i.opcode_length;
10110 if (!i.vex.length)
10111 switch (i.tm.opcode_space)
10112 {
10113 case SPACE_BASE:
10114 break;
10115 case SPACE_0F:
10116 ++j;
10117 break;
10118 case SPACE_0F38:
10119 case SPACE_0F3A:
10120 j += 2;
10121 break;
10122 default:
10123 abort ();
10124 }
10125
10126 if (now_seg == absolute_section)
10127 abs_section_offset += j;
10128 else if (j == 1)
10129 {
10130 FRAG_APPEND_1_CHAR (i.tm.base_opcode);
10131 }
10132 else
10133 {
10134 p = frag_more (j);
10135 if (!i.vex.length
10136 && i.tm.opcode_space != SPACE_BASE)
10137 {
10138 *p++ = 0x0f;
10139 if (i.tm.opcode_space != SPACE_0F)
10140 *p++ = i.tm.opcode_space == SPACE_0F38
10141 ? 0x38 : 0x3a;
10142 }
10143
10144 switch (i.opcode_length)
10145 {
10146 case 2:
10147 /* Put out high byte first: can't use md_number_to_chars! */
10148 *p++ = (i.tm.base_opcode >> 8) & 0xff;
10149 /* Fall through. */
10150 case 1:
10151 *p = i.tm.base_opcode & 0xff;
10152 break;
10153 default:
10154 abort ();
10155 break;
10156 }
10157
10158 }
10159
10160 /* Now the modrm byte and sib byte (if present). */
10161 if (i.tm.opcode_modifier.modrm)
10162 {
10163 frag_opcode_byte ((i.rm.regmem << 0)
10164 | (i.rm.reg << 3)
10165 | (i.rm.mode << 6));
10166 /* If i.rm.regmem == ESP (4)
10167 && i.rm.mode != (Register mode)
10168 && not 16 bit
10169 ==> need second modrm byte. */
10170 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
10171 && i.rm.mode != 3
10172 && !(i.base_reg && i.base_reg->reg_type.bitfield.word))
10173 frag_opcode_byte ((i.sib.base << 0)
10174 | (i.sib.index << 3)
10175 | (i.sib.scale << 6));
10176 }
10177
10178 if (i.disp_operands)
10179 output_disp (insn_start_frag, insn_start_off);
10180
10181 if (i.imm_operands)
10182 output_imm (insn_start_frag, insn_start_off);
10183
10184 /*
10185 * frag_now_fix () returning plain abs_section_offset when we're in the
10186 * absolute section, and abs_section_offset not getting updated as data
10187 * gets added to the frag breaks the logic below.
10188 */
10189 if (now_seg != absolute_section)
10190 {
10191 j = encoding_length (insn_start_frag, insn_start_off, frag_more (0));
10192 if (j > 15)
10193 as_warn (_("instruction length of %u bytes exceeds the limit of 15"),
10194 j);
10195 else if (fragP)
10196 {
10197 /* NB: Don't add prefix with GOTPC relocation since
10198 output_disp() above depends on the fixed encoding
10199 length. Can't add prefix with TLS relocation since
10200 it breaks TLS linker optimization. */
10201 unsigned int max = i.has_gotpc_tls_reloc ? 0 : 15 - j;
10202 /* Prefix count on the current instruction. */
10203 unsigned int count = i.vex.length;
10204 unsigned int k;
10205 for (k = 0; k < ARRAY_SIZE (i.prefix); k++)
10206 /* REX byte is encoded in VEX/EVEX prefix. */
10207 if (i.prefix[k] && (k != REX_PREFIX || !i.vex.length))
10208 count++;
10209
10210 /* Count prefixes for extended opcode maps. */
10211 if (!i.vex.length)
10212 switch (i.tm.opcode_space)
10213 {
10214 case SPACE_BASE:
10215 break;
10216 case SPACE_0F:
10217 count++;
10218 break;
10219 case SPACE_0F38:
10220 case SPACE_0F3A:
10221 count += 2;
10222 break;
10223 default:
10224 abort ();
10225 }
10226
10227 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
10228 == BRANCH_PREFIX)
10229 {
10230 /* Set the maximum prefix size in BRANCH_PREFIX
10231 frag. */
10232 if (fragP->tc_frag_data.max_bytes > max)
10233 fragP->tc_frag_data.max_bytes = max;
10234 if (fragP->tc_frag_data.max_bytes > count)
10235 fragP->tc_frag_data.max_bytes -= count;
10236 else
10237 fragP->tc_frag_data.max_bytes = 0;
10238 }
10239 else
10240 {
10241 /* Remember the maximum prefix size in FUSED_JCC_PADDING
10242 frag. */
10243 unsigned int max_prefix_size;
10244 if (align_branch_prefix_size > max)
10245 max_prefix_size = max;
10246 else
10247 max_prefix_size = align_branch_prefix_size;
10248 if (max_prefix_size > count)
10249 fragP->tc_frag_data.max_prefix_length
10250 = max_prefix_size - count;
10251 }
10252
10253 /* Use existing segment prefix if possible. Use CS
10254 segment prefix in 64-bit mode. In 32-bit mode, use SS
10255 segment prefix with ESP/EBP base register and use DS
10256 segment prefix without ESP/EBP base register. */
10257 if (i.prefix[SEG_PREFIX])
10258 fragP->tc_frag_data.default_prefix = i.prefix[SEG_PREFIX];
10259 else if (flag_code == CODE_64BIT)
10260 fragP->tc_frag_data.default_prefix = CS_PREFIX_OPCODE;
10261 else if (i.base_reg
10262 && (i.base_reg->reg_num == 4
10263 || i.base_reg->reg_num == 5))
10264 fragP->tc_frag_data.default_prefix = SS_PREFIX_OPCODE;
10265 else
10266 fragP->tc_frag_data.default_prefix = DS_PREFIX_OPCODE;
10267 }
10268 }
10269 }
10270
10271 /* NB: Don't work with COND_JUMP86 without i386. */
10272 if (align_branch_power
10273 && now_seg != absolute_section
10274 && cpu_arch_flags.bitfield.cpui386)
10275 {
10276 /* Terminate each frag so that we can add prefix and check for
10277 fused jcc. */
10278 frag_wane (frag_now);
10279 frag_new (0);
10280 }
10281
10282 #ifdef DEBUG386
10283 if (flag_debug)
10284 {
10285 pi ("" /*line*/, &i);
10286 }
10287 #endif /* DEBUG386 */
10288 }
10289
10290 /* Return the size of the displacement operand N. */
10291
10292 static int
10293 disp_size (unsigned int n)
10294 {
10295 int size = 4;
10296
10297 if (i.types[n].bitfield.disp64)
10298 size = 8;
10299 else if (i.types[n].bitfield.disp8)
10300 size = 1;
10301 else if (i.types[n].bitfield.disp16)
10302 size = 2;
10303 return size;
10304 }
10305
10306 /* Return the size of the immediate operand N. */
10307
10308 static int
10309 imm_size (unsigned int n)
10310 {
10311 int size = 4;
10312 if (i.types[n].bitfield.imm64)
10313 size = 8;
10314 else if (i.types[n].bitfield.imm8 || i.types[n].bitfield.imm8s)
10315 size = 1;
10316 else if (i.types[n].bitfield.imm16)
10317 size = 2;
10318 return size;
10319 }
10320
10321 static void
10322 output_disp (fragS *insn_start_frag, offsetT insn_start_off)
10323 {
10324 char *p;
10325 unsigned int n;
10326
10327 for (n = 0; n < i.operands; n++)
10328 {
10329 if (operand_type_check (i.types[n], disp))
10330 {
10331 int size = disp_size (n);
10332
10333 if (now_seg == absolute_section)
10334 abs_section_offset += size;
10335 else if (i.op[n].disps->X_op == O_constant)
10336 {
10337 offsetT val = i.op[n].disps->X_add_number;
10338
10339 val = offset_in_range (val >> (size == 1 ? i.memshift : 0),
10340 size);
10341 p = frag_more (size);
10342 md_number_to_chars (p, val, size);
10343 }
10344 else
10345 {
10346 enum bfd_reloc_code_real reloc_type;
10347 bool pcrel = (i.flags[n] & Operand_PCrel) != 0;
10348 bool sign = (flag_code == CODE_64BIT && size == 4
10349 && (!want_disp32 (&i.tm)
10350 || (i.tm.opcode_modifier.jump && !i.jumpabsolute
10351 && !i.types[n].bitfield.baseindex)))
10352 || pcrel;
10353 fixS *fixP;
10354
10355 /* We can't have 8 bit displacement here. */
10356 gas_assert (!i.types[n].bitfield.disp8);
10357
10358 /* The PC relative address is computed relative
10359 to the instruction boundary, so in case immediate
10360 fields follows, we need to adjust the value. */
10361 if (pcrel && i.imm_operands)
10362 {
10363 unsigned int n1;
10364 int sz = 0;
10365
10366 for (n1 = 0; n1 < i.operands; n1++)
10367 if (operand_type_check (i.types[n1], imm))
10368 {
10369 /* Only one immediate is allowed for PC
10370 relative address, except with .insn. */
10371 gas_assert (sz == 0 || dot_insn ());
10372 sz += imm_size (n1);
10373 }
10374 /* We should find at least one immediate. */
10375 gas_assert (sz != 0);
10376 i.op[n].disps->X_add_number -= sz;
10377 }
10378
10379 p = frag_more (size);
10380 reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
10381 if (GOT_symbol
10382 && GOT_symbol == i.op[n].disps->X_add_symbol
10383 && (((reloc_type == BFD_RELOC_32
10384 || reloc_type == BFD_RELOC_X86_64_32S
10385 || (reloc_type == BFD_RELOC_64
10386 && object_64bit))
10387 && (i.op[n].disps->X_op == O_symbol
10388 || (i.op[n].disps->X_op == O_add
10389 && ((symbol_get_value_expression
10390 (i.op[n].disps->X_op_symbol)->X_op)
10391 == O_subtract))))
10392 || reloc_type == BFD_RELOC_32_PCREL))
10393 {
10394 if (!object_64bit)
10395 {
10396 reloc_type = BFD_RELOC_386_GOTPC;
10397 i.has_gotpc_tls_reloc = true;
10398 i.op[n].disps->X_add_number +=
10399 encoding_length (insn_start_frag, insn_start_off, p);
10400 }
10401 else if (reloc_type == BFD_RELOC_64)
10402 reloc_type = BFD_RELOC_X86_64_GOTPC64;
10403 else
10404 /* Don't do the adjustment for x86-64, as there
10405 the pcrel addressing is relative to the _next_
10406 insn, and that is taken care of in other code. */
10407 reloc_type = BFD_RELOC_X86_64_GOTPC32;
10408 }
10409 else if (align_branch_power)
10410 {
10411 switch (reloc_type)
10412 {
10413 case BFD_RELOC_386_TLS_GD:
10414 case BFD_RELOC_386_TLS_LDM:
10415 case BFD_RELOC_386_TLS_IE:
10416 case BFD_RELOC_386_TLS_IE_32:
10417 case BFD_RELOC_386_TLS_GOTIE:
10418 case BFD_RELOC_386_TLS_GOTDESC:
10419 case BFD_RELOC_386_TLS_DESC_CALL:
10420 case BFD_RELOC_X86_64_TLSGD:
10421 case BFD_RELOC_X86_64_TLSLD:
10422 case BFD_RELOC_X86_64_GOTTPOFF:
10423 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
10424 case BFD_RELOC_X86_64_TLSDESC_CALL:
10425 i.has_gotpc_tls_reloc = true;
10426 default:
10427 break;
10428 }
10429 }
10430 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal,
10431 size, i.op[n].disps, pcrel,
10432 reloc_type);
10433
10434 if (flag_code == CODE_64BIT && size == 4 && pcrel
10435 && !i.prefix[ADDR_PREFIX])
10436 fixP->fx_signed = 1;
10437
10438 /* Check for "call/jmp *mem", "mov mem, %reg",
10439 "test %reg, mem" and "binop mem, %reg" where binop
10440 is one of adc, add, and, cmp, or, sbb, sub, xor
10441 instructions without data prefix. Always generate
10442 R_386_GOT32X for "sym*GOT" operand in 32-bit mode. */
10443 if (i.prefix[DATA_PREFIX] == 0
10444 && (generate_relax_relocations
10445 || (!object_64bit
10446 && i.rm.mode == 0
10447 && i.rm.regmem == 5))
10448 && (i.rm.mode == 2
10449 || (i.rm.mode == 0 && i.rm.regmem == 5))
10450 && i.tm.opcode_space == SPACE_BASE
10451 && ((i.operands == 1
10452 && i.tm.base_opcode == 0xff
10453 && (i.rm.reg == 2 || i.rm.reg == 4))
10454 || (i.operands == 2
10455 && (i.tm.base_opcode == 0x8b
10456 || i.tm.base_opcode == 0x85
10457 || (i.tm.base_opcode & ~0x38) == 0x03))))
10458 {
10459 if (object_64bit)
10460 {
10461 fixP->fx_tcbit = i.rex != 0;
10462 if (i.base_reg
10463 && (i.base_reg->reg_num == RegIP))
10464 fixP->fx_tcbit2 = 1;
10465 }
10466 else
10467 fixP->fx_tcbit2 = 1;
10468 }
10469 }
10470 }
10471 }
10472 }
10473
10474 static void
10475 output_imm (fragS *insn_start_frag, offsetT insn_start_off)
10476 {
10477 char *p;
10478 unsigned int n;
10479
10480 for (n = 0; n < i.operands; n++)
10481 {
10482 if (operand_type_check (i.types[n], imm))
10483 {
10484 int size = imm_size (n);
10485
10486 if (now_seg == absolute_section)
10487 abs_section_offset += size;
10488 else if (i.op[n].imms->X_op == O_constant)
10489 {
10490 offsetT val;
10491
10492 val = offset_in_range (i.op[n].imms->X_add_number,
10493 size);
10494 p = frag_more (size);
10495 md_number_to_chars (p, val, size);
10496 }
10497 else
10498 {
10499 /* Not absolute_section.
10500 Need a 32-bit fixup (don't support 8bit
10501 non-absolute imms). Try to support other
10502 sizes ... */
10503 enum bfd_reloc_code_real reloc_type;
10504 int sign;
10505
10506 if (i.types[n].bitfield.imm32s
10507 && (i.suffix == QWORD_MNEM_SUFFIX
10508 || (!i.suffix && i.tm.opcode_modifier.no_lsuf)
10509 || (i.prefix[REX_PREFIX] & REX_W)
10510 || dot_insn ()))
10511 sign = 1;
10512 else
10513 sign = 0;
10514
10515 p = frag_more (size);
10516 reloc_type = reloc (size, 0, sign, i.reloc[n]);
10517
10518 /* This is tough to explain. We end up with this one if we
10519 * have operands that look like
10520 * "_GLOBAL_OFFSET_TABLE_+[.-.L284]". The goal here is to
10521 * obtain the absolute address of the GOT, and it is strongly
10522 * preferable from a performance point of view to avoid using
10523 * a runtime relocation for this. The actual sequence of
10524 * instructions often look something like:
10525 *
10526 * call .L66
10527 * .L66:
10528 * popl %ebx
10529 * addl $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
10530 *
10531 * The call and pop essentially return the absolute address
10532 * of the label .L66 and store it in %ebx. The linker itself
10533 * will ultimately change the first operand of the addl so
10534 * that %ebx points to the GOT, but to keep things simple, the
10535 * .o file must have this operand set so that it generates not
10536 * the absolute address of .L66, but the absolute address of
10537 * itself. This allows the linker itself simply treat a GOTPC
10538 * relocation as asking for a pcrel offset to the GOT to be
10539 * added in, and the addend of the relocation is stored in the
10540 * operand field for the instruction itself.
10541 *
10542 * Our job here is to fix the operand so that it would add
10543 * the correct offset so that %ebx would point to itself. The
10544 * thing that is tricky is that .-.L66 will point to the
10545 * beginning of the instruction, so we need to further modify
10546 * the operand so that it will point to itself. There are
10547 * other cases where you have something like:
10548 *
10549 * .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
10550 *
10551 * and here no correction would be required. Internally in
10552 * the assembler we treat operands of this form as not being
10553 * pcrel since the '.' is explicitly mentioned, and I wonder
10554 * whether it would simplify matters to do it this way. Who
10555 * knows. In earlier versions of the PIC patches, the
10556 * pcrel_adjust field was used to store the correction, but
10557 * since the expression is not pcrel, I felt it would be
10558 * confusing to do it this way. */
10559
10560 if ((reloc_type == BFD_RELOC_32
10561 || reloc_type == BFD_RELOC_X86_64_32S
10562 || reloc_type == BFD_RELOC_64)
10563 && GOT_symbol
10564 && GOT_symbol == i.op[n].imms->X_add_symbol
10565 && (i.op[n].imms->X_op == O_symbol
10566 || (i.op[n].imms->X_op == O_add
10567 && ((symbol_get_value_expression
10568 (i.op[n].imms->X_op_symbol)->X_op)
10569 == O_subtract))))
10570 {
10571 if (!object_64bit)
10572 reloc_type = BFD_RELOC_386_GOTPC;
10573 else if (size == 4)
10574 reloc_type = BFD_RELOC_X86_64_GOTPC32;
10575 else if (size == 8)
10576 reloc_type = BFD_RELOC_X86_64_GOTPC64;
10577 i.has_gotpc_tls_reloc = true;
10578 i.op[n].imms->X_add_number +=
10579 encoding_length (insn_start_frag, insn_start_off, p);
10580 }
10581 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
10582 i.op[n].imms, 0, reloc_type);
10583 }
10584 }
10585 }
10586 }
10587 \f
10588 /* x86_cons_fix_new is called via the expression parsing code when a
10589 reloc is needed. We use this hook to get the correct .got reloc. */
10590 static int cons_sign = -1;
10591
10592 void
10593 x86_cons_fix_new (fragS *frag, unsigned int off, unsigned int len,
10594 expressionS *exp, bfd_reloc_code_real_type r)
10595 {
10596 r = reloc (len, 0, cons_sign, r);
10597
10598 #ifdef TE_PE
10599 if (exp->X_op == O_secrel)
10600 {
10601 exp->X_op = O_symbol;
10602 r = BFD_RELOC_32_SECREL;
10603 }
10604 else if (exp->X_op == O_secidx)
10605 r = BFD_RELOC_16_SECIDX;
10606 #endif
10607
10608 fix_new_exp (frag, off, len, exp, 0, r);
10609 }
10610
10611 /* Export the ABI address size for use by TC_ADDRESS_BYTES for the
10612 purpose of the `.dc.a' internal pseudo-op. */
10613
10614 int
10615 x86_address_bytes (void)
10616 {
10617 if ((stdoutput->arch_info->mach & bfd_mach_x64_32))
10618 return 4;
10619 return stdoutput->arch_info->bits_per_address / 8;
10620 }
10621
10622 #if (!(defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined (OBJ_MACH_O)) \
10623 || defined (LEX_AT)) && !defined (TE_PE)
10624 # define lex_got(reloc, adjust, types) NULL
10625 #else
10626 /* Parse operands of the form
10627 <symbol>@GOTOFF+<nnn>
10628 and similar .plt or .got references.
10629
10630 If we find one, set up the correct relocation in RELOC and copy the
10631 input string, minus the `@GOTOFF' into a malloc'd buffer for
10632 parsing by the calling routine. Return this buffer, and if ADJUST
10633 is non-null set it to the length of the string we removed from the
10634 input line. Otherwise return NULL. */
10635 static char *
10636 lex_got (enum bfd_reloc_code_real *rel,
10637 int *adjust,
10638 i386_operand_type *types)
10639 {
10640 /* Some of the relocations depend on the size of what field is to
10641 be relocated. But in our callers i386_immediate and i386_displacement
10642 we don't yet know the operand size (this will be set by insn
10643 matching). Hence we record the word32 relocation here,
10644 and adjust the reloc according to the real size in reloc(). */
10645 static const struct
10646 {
10647 const char *str;
10648 int len;
10649 const enum bfd_reloc_code_real rel[2];
10650 const i386_operand_type types64;
10651 bool need_GOT_symbol;
10652 }
10653 gotrel[] =
10654 {
10655
10656 #define OPERAND_TYPE_IMM32_32S_DISP32 { .bitfield = \
10657 { .imm32 = 1, .imm32s = 1, .disp32 = 1 } }
10658 #define OPERAND_TYPE_IMM32_32S_64_DISP32 { .bitfield = \
10659 { .imm32 = 1, .imm32s = 1, .imm64 = 1, .disp32 = 1 } }
10660 #define OPERAND_TYPE_IMM32_32S_64_DISP32_64 { .bitfield = \
10661 { .imm32 = 1, .imm32s = 1, .imm64 = 1, .disp32 = 1, .disp64 = 1 } }
10662 #define OPERAND_TYPE_IMM64_DISP64 { .bitfield = \
10663 { .imm64 = 1, .disp64 = 1 } }
10664
10665 #ifndef TE_PE
10666 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
10667 { STRING_COMMA_LEN ("SIZE"), { BFD_RELOC_SIZE32,
10668 BFD_RELOC_SIZE32 },
10669 { .bitfield = { .imm32 = 1, .imm64 = 1 } }, false },
10670 #endif
10671 { STRING_COMMA_LEN ("PLTOFF"), { _dummy_first_bfd_reloc_code_real,
10672 BFD_RELOC_X86_64_PLTOFF64 },
10673 { .bitfield = { .imm64 = 1 } }, true },
10674 { STRING_COMMA_LEN ("PLT"), { BFD_RELOC_386_PLT32,
10675 BFD_RELOC_X86_64_PLT32 },
10676 OPERAND_TYPE_IMM32_32S_DISP32, false },
10677 { STRING_COMMA_LEN ("GOTPLT"), { _dummy_first_bfd_reloc_code_real,
10678 BFD_RELOC_X86_64_GOTPLT64 },
10679 OPERAND_TYPE_IMM64_DISP64, true },
10680 { STRING_COMMA_LEN ("GOTOFF"), { BFD_RELOC_386_GOTOFF,
10681 BFD_RELOC_X86_64_GOTOFF64 },
10682 OPERAND_TYPE_IMM64_DISP64, true },
10683 { STRING_COMMA_LEN ("GOTPCREL"), { _dummy_first_bfd_reloc_code_real,
10684 BFD_RELOC_X86_64_GOTPCREL },
10685 OPERAND_TYPE_IMM32_32S_DISP32, true },
10686 { STRING_COMMA_LEN ("TLSGD"), { BFD_RELOC_386_TLS_GD,
10687 BFD_RELOC_X86_64_TLSGD },
10688 OPERAND_TYPE_IMM32_32S_DISP32, true },
10689 { STRING_COMMA_LEN ("TLSLDM"), { BFD_RELOC_386_TLS_LDM,
10690 _dummy_first_bfd_reloc_code_real },
10691 OPERAND_TYPE_NONE, true },
10692 { STRING_COMMA_LEN ("TLSLD"), { _dummy_first_bfd_reloc_code_real,
10693 BFD_RELOC_X86_64_TLSLD },
10694 OPERAND_TYPE_IMM32_32S_DISP32, true },
10695 { STRING_COMMA_LEN ("GOTTPOFF"), { BFD_RELOC_386_TLS_IE_32,
10696 BFD_RELOC_X86_64_GOTTPOFF },
10697 OPERAND_TYPE_IMM32_32S_DISP32, true },
10698 { STRING_COMMA_LEN ("TPOFF"), { BFD_RELOC_386_TLS_LE_32,
10699 BFD_RELOC_X86_64_TPOFF32 },
10700 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
10701 { STRING_COMMA_LEN ("NTPOFF"), { BFD_RELOC_386_TLS_LE,
10702 _dummy_first_bfd_reloc_code_real },
10703 OPERAND_TYPE_NONE, true },
10704 { STRING_COMMA_LEN ("DTPOFF"), { BFD_RELOC_386_TLS_LDO_32,
10705 BFD_RELOC_X86_64_DTPOFF32 },
10706 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
10707 { STRING_COMMA_LEN ("GOTNTPOFF"),{ BFD_RELOC_386_TLS_GOTIE,
10708 _dummy_first_bfd_reloc_code_real },
10709 OPERAND_TYPE_NONE, true },
10710 { STRING_COMMA_LEN ("INDNTPOFF"),{ BFD_RELOC_386_TLS_IE,
10711 _dummy_first_bfd_reloc_code_real },
10712 OPERAND_TYPE_NONE, true },
10713 { STRING_COMMA_LEN ("GOT"), { BFD_RELOC_386_GOT32,
10714 BFD_RELOC_X86_64_GOT32 },
10715 OPERAND_TYPE_IMM32_32S_64_DISP32, true },
10716 { STRING_COMMA_LEN ("TLSDESC"), { BFD_RELOC_386_TLS_GOTDESC,
10717 BFD_RELOC_X86_64_GOTPC32_TLSDESC },
10718 OPERAND_TYPE_IMM32_32S_DISP32, true },
10719 { STRING_COMMA_LEN ("TLSCALL"), { BFD_RELOC_386_TLS_DESC_CALL,
10720 BFD_RELOC_X86_64_TLSDESC_CALL },
10721 OPERAND_TYPE_IMM32_32S_DISP32, true },
10722 #else /* TE_PE */
10723 { STRING_COMMA_LEN ("SECREL32"), { BFD_RELOC_32_SECREL,
10724 BFD_RELOC_32_SECREL },
10725 OPERAND_TYPE_IMM32_32S_64_DISP32_64, false },
10726 #endif
10727
10728 #undef OPERAND_TYPE_IMM32_32S_DISP32
10729 #undef OPERAND_TYPE_IMM32_32S_64_DISP32
10730 #undef OPERAND_TYPE_IMM32_32S_64_DISP32_64
10731 #undef OPERAND_TYPE_IMM64_DISP64
10732
10733 };
10734 char *cp;
10735 unsigned int j;
10736
10737 #if defined (OBJ_MAYBE_ELF) && !defined (TE_PE)
10738 if (!IS_ELF)
10739 return NULL;
10740 #endif
10741
10742 for (cp = input_line_pointer; *cp != '@'; cp++)
10743 if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
10744 return NULL;
10745
10746 for (j = 0; j < ARRAY_SIZE (gotrel); j++)
10747 {
10748 int len = gotrel[j].len;
10749 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
10750 {
10751 if (gotrel[j].rel[object_64bit] != 0)
10752 {
10753 int first, second;
10754 char *tmpbuf, *past_reloc;
10755
10756 *rel = gotrel[j].rel[object_64bit];
10757
10758 if (types)
10759 {
10760 if (flag_code != CODE_64BIT)
10761 {
10762 types->bitfield.imm32 = 1;
10763 types->bitfield.disp32 = 1;
10764 }
10765 else
10766 *types = gotrel[j].types64;
10767 }
10768
10769 if (gotrel[j].need_GOT_symbol && GOT_symbol == NULL)
10770 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
10771
10772 /* The length of the first part of our input line. */
10773 first = cp - input_line_pointer;
10774
10775 /* The second part goes from after the reloc token until
10776 (and including) an end_of_line char or comma. */
10777 past_reloc = cp + 1 + len;
10778 cp = past_reloc;
10779 while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
10780 ++cp;
10781 second = cp + 1 - past_reloc;
10782
10783 /* Allocate and copy string. The trailing NUL shouldn't
10784 be necessary, but be safe. */
10785 tmpbuf = XNEWVEC (char, first + second + 2);
10786 memcpy (tmpbuf, input_line_pointer, first);
10787 if (second != 0 && *past_reloc != ' ')
10788 /* Replace the relocation token with ' ', so that
10789 errors like foo@GOTOFF1 will be detected. */
10790 tmpbuf[first++] = ' ';
10791 else
10792 /* Increment length by 1 if the relocation token is
10793 removed. */
10794 len++;
10795 if (adjust)
10796 *adjust = len;
10797 memcpy (tmpbuf + first, past_reloc, second);
10798 tmpbuf[first + second] = '\0';
10799 return tmpbuf;
10800 }
10801
10802 as_bad (_("@%s reloc is not supported with %d-bit output format"),
10803 gotrel[j].str, 1 << (5 + object_64bit));
10804 return NULL;
10805 }
10806 }
10807
10808 /* Might be a symbol version string. Don't as_bad here. */
10809 return NULL;
10810 }
10811 #endif
10812
10813 bfd_reloc_code_real_type
10814 x86_cons (expressionS *exp, int size)
10815 {
10816 bfd_reloc_code_real_type got_reloc = NO_RELOC;
10817
10818 intel_syntax = -intel_syntax;
10819 exp->X_md = 0;
10820 expr_mode = expr_operator_none;
10821
10822 #if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
10823 && !defined (LEX_AT)) \
10824 || defined (TE_PE)
10825 if (size == 4 || (object_64bit && size == 8))
10826 {
10827 /* Handle @GOTOFF and the like in an expression. */
10828 char *save;
10829 char *gotfree_input_line;
10830 int adjust = 0;
10831
10832 save = input_line_pointer;
10833 gotfree_input_line = lex_got (&got_reloc, &adjust, NULL);
10834 if (gotfree_input_line)
10835 input_line_pointer = gotfree_input_line;
10836
10837 expression (exp);
10838
10839 if (gotfree_input_line)
10840 {
10841 /* expression () has merrily parsed up to the end of line,
10842 or a comma - in the wrong buffer. Transfer how far
10843 input_line_pointer has moved to the right buffer. */
10844 input_line_pointer = (save
10845 + (input_line_pointer - gotfree_input_line)
10846 + adjust);
10847 free (gotfree_input_line);
10848 if (exp->X_op == O_constant
10849 || exp->X_op == O_absent
10850 || exp->X_op == O_illegal
10851 || exp->X_op == O_register
10852 || exp->X_op == O_big)
10853 {
10854 char c = *input_line_pointer;
10855 *input_line_pointer = 0;
10856 as_bad (_("missing or invalid expression `%s'"), save);
10857 *input_line_pointer = c;
10858 }
10859 else if ((got_reloc == BFD_RELOC_386_PLT32
10860 || got_reloc == BFD_RELOC_X86_64_PLT32)
10861 && exp->X_op != O_symbol)
10862 {
10863 char c = *input_line_pointer;
10864 *input_line_pointer = 0;
10865 as_bad (_("invalid PLT expression `%s'"), save);
10866 *input_line_pointer = c;
10867 }
10868 }
10869 }
10870 else
10871 #endif
10872 expression (exp);
10873
10874 intel_syntax = -intel_syntax;
10875
10876 if (intel_syntax)
10877 i386_intel_simplify (exp);
10878
10879 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
10880 if (size <= 4 && expr_mode == expr_operator_present
10881 && exp->X_op == O_constant && !object_64bit)
10882 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
10883
10884 return got_reloc;
10885 }
10886
10887 static void
10888 signed_cons (int size)
10889 {
10890 if (object_64bit)
10891 cons_sign = 1;
10892 cons (size);
10893 cons_sign = -1;
10894 }
10895
10896 static void
10897 s_insn (int dummy ATTRIBUTE_UNUSED)
10898 {
10899 char mnemonic[MAX_MNEM_SIZE], *line = input_line_pointer, *ptr;
10900 char *saved_ilp = find_end_of_line (line, false), saved_char;
10901 const char *end;
10902 unsigned int j;
10903 valueT val;
10904 bool vex = false, xop = false, evex = false;
10905 static const templates tt = { &i.tm, &i.tm + 1 };
10906
10907 init_globals ();
10908
10909 saved_char = *saved_ilp;
10910 *saved_ilp = 0;
10911
10912 end = parse_insn (line, mnemonic, true);
10913 if (end == NULL)
10914 {
10915 bad:
10916 *saved_ilp = saved_char;
10917 ignore_rest_of_line ();
10918 i.tm.mnem_off = 0;
10919 return;
10920 }
10921 line += end - line;
10922
10923 current_templates = &tt;
10924 i.tm.mnem_off = MN__insn;
10925 i.tm.extension_opcode = None;
10926
10927 if (startswith (line, "VEX")
10928 && (line[3] == '.' || is_space_char (line[3])))
10929 {
10930 vex = true;
10931 line += 3;
10932 }
10933 else if (startswith (line, "XOP") && ISDIGIT (line[3]))
10934 {
10935 char *e;
10936 unsigned long n = strtoul (line + 3, &e, 16);
10937
10938 if (e == line + 5 && n >= 0x08 && n <= 0x1f
10939 && (*e == '.' || is_space_char (*e)))
10940 {
10941 xop = true;
10942 /* Arrange for build_vex_prefix() to emit 0x8f. */
10943 i.tm.opcode_space = SPACE_XOP08;
10944 i.insn_opcode_space = n;
10945 line = e;
10946 }
10947 }
10948 else if (startswith (line, "EVEX")
10949 && (line[4] == '.' || is_space_char (line[4])))
10950 {
10951 evex = true;
10952 line += 4;
10953 }
10954
10955 if (vex || xop
10956 ? i.vec_encoding == vex_encoding_evex
10957 : evex
10958 ? i.vec_encoding == vex_encoding_vex
10959 || i.vec_encoding == vex_encoding_vex3
10960 : i.vec_encoding != vex_encoding_default)
10961 {
10962 as_bad (_("pseudo-prefix conflicts with encoding specifier"));
10963 goto bad;
10964 }
10965
10966 if (line > end && i.vec_encoding == vex_encoding_default)
10967 i.vec_encoding = evex ? vex_encoding_evex : vex_encoding_vex;
10968
10969 if (i.vec_encoding != vex_encoding_default)
10970 {
10971 /* Only address size and segment override prefixes are permitted with
10972 VEX/XOP/EVEX encodings. */
10973 const unsigned char *p = i.prefix;
10974
10975 for (j = 0; j < ARRAY_SIZE (i.prefix); ++j, ++p)
10976 {
10977 if (!*p)
10978 continue;
10979
10980 switch (j)
10981 {
10982 case SEG_PREFIX:
10983 case ADDR_PREFIX:
10984 break;
10985 default:
10986 as_bad (_("illegal prefix used with VEX/XOP/EVEX"));
10987 goto bad;
10988 }
10989 }
10990 }
10991
10992 if (line > end && *line == '.')
10993 {
10994 /* Length specifier (VEX.L, XOP.L, EVEX.L'L). */
10995 switch (line[1])
10996 {
10997 case 'L':
10998 switch (line[2])
10999 {
11000 case '0':
11001 if (evex)
11002 i.tm.opcode_modifier.evex = EVEX128;
11003 else
11004 i.tm.opcode_modifier.vex = VEX128;
11005 break;
11006
11007 case '1':
11008 if (evex)
11009 i.tm.opcode_modifier.evex = EVEX256;
11010 else
11011 i.tm.opcode_modifier.vex = VEX256;
11012 break;
11013
11014 case '2':
11015 if (evex)
11016 i.tm.opcode_modifier.evex = EVEX512;
11017 break;
11018
11019 case '3':
11020 if (evex)
11021 i.tm.opcode_modifier.evex = EVEX_L3;
11022 break;
11023
11024 case 'I':
11025 if (line[3] == 'G')
11026 {
11027 if (evex)
11028 i.tm.opcode_modifier.evex = EVEXLIG;
11029 else
11030 i.tm.opcode_modifier.vex = VEXScalar; /* LIG */
11031 ++line;
11032 }
11033 break;
11034 }
11035
11036 if (i.tm.opcode_modifier.vex || i.tm.opcode_modifier.evex)
11037 line += 3;
11038 break;
11039
11040 case '1':
11041 if (line[2] == '2' && line[3] == '8')
11042 {
11043 if (evex)
11044 i.tm.opcode_modifier.evex = EVEX128;
11045 else
11046 i.tm.opcode_modifier.vex = VEX128;
11047 line += 4;
11048 }
11049 break;
11050
11051 case '2':
11052 if (line[2] == '5' && line[3] == '6')
11053 {
11054 if (evex)
11055 i.tm.opcode_modifier.evex = EVEX256;
11056 else
11057 i.tm.opcode_modifier.vex = VEX256;
11058 line += 4;
11059 }
11060 break;
11061
11062 case '5':
11063 if (evex && line[2] == '1' && line[3] == '2')
11064 {
11065 i.tm.opcode_modifier.evex = EVEX512;
11066 line += 4;
11067 }
11068 break;
11069 }
11070 }
11071
11072 if (line > end && *line == '.')
11073 {
11074 /* embedded prefix (VEX.pp, XOP.pp, EVEX.pp). */
11075 switch (line[1])
11076 {
11077 case 'N':
11078 if (line[2] == 'P')
11079 line += 3;
11080 break;
11081
11082 case '6':
11083 if (line[2] == '6')
11084 {
11085 i.tm.opcode_modifier.opcodeprefix = PREFIX_0X66;
11086 line += 3;
11087 }
11088 break;
11089
11090 case 'F': case 'f':
11091 if (line[2] == '3')
11092 {
11093 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
11094 line += 3;
11095 }
11096 else if (line[2] == '2')
11097 {
11098 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF2;
11099 line += 3;
11100 }
11101 break;
11102 }
11103 }
11104
11105 if (line > end && !xop && *line == '.')
11106 {
11107 /* Encoding space (VEX.mmmmm, EVEX.mmmm). */
11108 switch (line[1])
11109 {
11110 case '0':
11111 if (TOUPPER (line[2]) != 'F')
11112 break;
11113 if (line[3] == '.' || is_space_char (line[3]))
11114 {
11115 i.insn_opcode_space = SPACE_0F;
11116 line += 3;
11117 }
11118 else if (line[3] == '3'
11119 && (line[4] == '8' || TOUPPER (line[4]) == 'A')
11120 && (line[5] == '.' || is_space_char (line[5])))
11121 {
11122 i.insn_opcode_space = line[4] == '8' ? SPACE_0F38 : SPACE_0F3A;
11123 line += 5;
11124 }
11125 break;
11126
11127 case 'M':
11128 if (ISDIGIT (line[2]) && line[2] != '0')
11129 {
11130 char *e;
11131 unsigned long n = strtoul (line + 2, &e, 10);
11132
11133 if (n <= (evex ? 15 : 31)
11134 && (*e == '.' || is_space_char (*e)))
11135 {
11136 i.insn_opcode_space = n;
11137 line = e;
11138 }
11139 }
11140 break;
11141 }
11142 }
11143
11144 if (line > end && *line == '.' && line[1] == 'W')
11145 {
11146 /* VEX.W, XOP.W, EVEX.W */
11147 switch (line[2])
11148 {
11149 case '0':
11150 i.tm.opcode_modifier.vexw = VEXW0;
11151 break;
11152
11153 case '1':
11154 i.tm.opcode_modifier.vexw = VEXW1;
11155 break;
11156
11157 case 'I':
11158 if (line[3] == 'G')
11159 {
11160 i.tm.opcode_modifier.vexw = VEXWIG;
11161 ++line;
11162 }
11163 break;
11164 }
11165
11166 if (i.tm.opcode_modifier.vexw)
11167 line += 3;
11168 }
11169
11170 if (line > end && *line && !is_space_char (*line))
11171 {
11172 /* Improve diagnostic a little. */
11173 if (*line == '.' && line[1] && !is_space_char (line[1]))
11174 ++line;
11175 goto done;
11176 }
11177
11178 /* Before processing the opcode expression, find trailing "+r" or
11179 "/<digit>" specifiers. */
11180 for (ptr = line; ; ++ptr)
11181 {
11182 unsigned long n;
11183 char *e;
11184
11185 ptr = strpbrk (ptr, "+/,");
11186 if (ptr == NULL || *ptr == ',')
11187 break;
11188
11189 if (*ptr == '+' && ptr[1] == 'r'
11190 && (ptr[2] == ',' || (is_space_char (ptr[2]) && ptr[3] == ',')))
11191 {
11192 *ptr = ' ';
11193 ptr[1] = ' ';
11194 i.short_form = true;
11195 break;
11196 }
11197
11198 if (*ptr == '/' && ISDIGIT (ptr[1])
11199 && (n = strtoul (ptr + 1, &e, 8)) < 8
11200 && e == ptr + 2
11201 && (ptr[2] == ',' || (is_space_char (ptr[2]) && ptr[3] == ',')))
11202 {
11203 *ptr = ' ';
11204 ptr[1] = ' ';
11205 i.tm.extension_opcode = n;
11206 i.tm.opcode_modifier.modrm = 1;
11207 break;
11208 }
11209 }
11210
11211 input_line_pointer = line;
11212 val = get_absolute_expression ();
11213 line = input_line_pointer;
11214
11215 if (i.short_form && (val & 7))
11216 as_warn ("`+r' assumes low three opcode bits to be clear");
11217
11218 for (j = 1; j < sizeof(val); ++j)
11219 if (!(val >> (j * 8)))
11220 break;
11221
11222 /* Trim off a prefix if present. */
11223 if (j > 1 && !vex && !xop && !evex)
11224 {
11225 uint8_t byte = val >> ((j - 1) * 8);
11226
11227 switch (byte)
11228 {
11229 case DATA_PREFIX_OPCODE:
11230 case REPE_PREFIX_OPCODE:
11231 case REPNE_PREFIX_OPCODE:
11232 if (!add_prefix (byte))
11233 goto bad;
11234 val &= ((uint64_t)1 << (--j * 8)) - 1;
11235 break;
11236 }
11237 }
11238
11239 /* Trim off encoding space. */
11240 if (j > 1 && !i.insn_opcode_space && (val >> ((j - 1) * 8)) == 0x0f)
11241 {
11242 uint8_t byte = val >> ((--j - 1) * 8);
11243
11244 i.insn_opcode_space = SPACE_0F;
11245 switch (byte & -(j > 1))
11246 {
11247 case 0x38:
11248 i.insn_opcode_space = SPACE_0F38;
11249 --j;
11250 break;
11251 case 0x3a:
11252 i.insn_opcode_space = SPACE_0F3A;
11253 --j;
11254 break;
11255 }
11256 i.tm.opcode_space = i.insn_opcode_space;
11257 val &= ((uint64_t)1 << (j * 8)) - 1;
11258 }
11259 if (!i.tm.opcode_space && (vex || evex))
11260 /* Arrange for build_vex_prefix() to properly emit 0xC4/0xC5.
11261 Also avoid hitting abort() there or in build_evex_prefix(). */
11262 i.tm.opcode_space = i.insn_opcode_space == SPACE_0F ? SPACE_0F
11263 : SPACE_0F38;
11264
11265 if (j > 2)
11266 {
11267 as_bad (_("opcode residual (%#"PRIx64") too wide"), (uint64_t) val);
11268 goto bad;
11269 }
11270 i.opcode_length = j;
11271
11272 /* Handle operands, if any. */
11273 if (*line == ',')
11274 {
11275 i386_operand_type combined;
11276 expressionS *disp_exp = NULL;
11277 bool changed;
11278
11279 i.memshift = -1;
11280
11281 ptr = parse_operands (line + 1, &i386_mnemonics[MN__insn]);
11282 this_operand = -1;
11283 if (!ptr)
11284 goto bad;
11285 line = ptr;
11286
11287 if (!i.operands)
11288 {
11289 as_bad (_("expecting operand after ','; got nothing"));
11290 goto done;
11291 }
11292
11293 if (i.mem_operands > 1)
11294 {
11295 as_bad (_("too many memory references for `%s'"),
11296 &i386_mnemonics[MN__insn]);
11297 goto done;
11298 }
11299
11300 /* No need to distinguish vex_encoding_evex and vex_encoding_evex512. */
11301 if (i.vec_encoding == vex_encoding_evex512)
11302 i.vec_encoding = vex_encoding_evex;
11303
11304 /* Are we to emit ModR/M encoding? */
11305 if (!i.short_form
11306 && (i.mem_operands
11307 || i.reg_operands > (i.vec_encoding != vex_encoding_default)
11308 || i.tm.extension_opcode != None))
11309 i.tm.opcode_modifier.modrm = 1;
11310
11311 if (!i.tm.opcode_modifier.modrm
11312 && (i.reg_operands
11313 > i.short_form + 0U + (i.vec_encoding != vex_encoding_default)
11314 || i.mem_operands))
11315 {
11316 as_bad (_("too many register/memory operands"));
11317 goto done;
11318 }
11319
11320 /* Enforce certain constraints on operands. */
11321 switch (i.reg_operands + i.mem_operands
11322 + (i.tm.extension_opcode != None))
11323 {
11324 case 0:
11325 if (i.short_form)
11326 {
11327 as_bad (_("too few register/memory operands"));
11328 goto done;
11329 }
11330 /* Fall through. */
11331 case 1:
11332 if (i.tm.opcode_modifier.modrm)
11333 {
11334 as_bad (_("too few register/memory operands"));
11335 goto done;
11336 }
11337 break;
11338
11339 case 2:
11340 break;
11341
11342 case 4:
11343 if (i.imm_operands
11344 && (i.op[0].imms->X_op != O_constant
11345 || !fits_in_imm4 (i.op[0].imms->X_add_number)))
11346 {
11347 as_bad (_("constant doesn't fit in %d bits"), evex ? 3 : 4);
11348 goto done;
11349 }
11350 /* Fall through. */
11351 case 3:
11352 if (i.vec_encoding != vex_encoding_default)
11353 {
11354 i.tm.opcode_modifier.vexvvvv = 1;
11355 break;
11356 }
11357 /* Fall through. */
11358 default:
11359 as_bad (_("too many register/memory operands"));
11360 goto done;
11361 }
11362
11363 /* Bring operands into canonical order (imm, mem, reg). */
11364 do
11365 {
11366 changed = false;
11367
11368 for (j = 1; j < i.operands; ++j)
11369 {
11370 if ((!operand_type_check (i.types[j - 1], imm)
11371 && operand_type_check (i.types[j], imm))
11372 || (i.types[j - 1].bitfield.class != ClassNone
11373 && i.types[j].bitfield.class == ClassNone))
11374 {
11375 swap_2_operands (j - 1, j);
11376 changed = true;
11377 }
11378 }
11379 }
11380 while (changed);
11381
11382 /* For Intel syntax swap the order of register operands. */
11383 if (intel_syntax)
11384 switch (i.reg_operands)
11385 {
11386 case 0:
11387 case 1:
11388 break;
11389
11390 case 4:
11391 swap_2_operands (i.imm_operands + i.mem_operands + 1, i.operands - 2);
11392 /* Fall through. */
11393 case 3:
11394 case 2:
11395 swap_2_operands (i.imm_operands + i.mem_operands, i.operands - 1);
11396 break;
11397
11398 default:
11399 abort ();
11400 }
11401
11402 /* Enforce constraints when using VSIB. */
11403 if (i.index_reg
11404 && (i.index_reg->reg_type.bitfield.xmmword
11405 || i.index_reg->reg_type.bitfield.ymmword
11406 || i.index_reg->reg_type.bitfield.zmmword))
11407 {
11408 if (i.vec_encoding == vex_encoding_default)
11409 {
11410 as_bad (_("VSIB unavailable with legacy encoding"));
11411 goto done;
11412 }
11413
11414 if (i.vec_encoding == vex_encoding_evex
11415 && i.reg_operands > 1)
11416 {
11417 /* We could allow two register operands, encoding the 2nd one in
11418 an 8-bit immediate like for 4-register-operand insns, but that
11419 would require ugly fiddling with process_operands() and/or
11420 build_modrm_byte(). */
11421 as_bad (_("too many register operands with VSIB"));
11422 goto done;
11423 }
11424
11425 i.tm.opcode_modifier.sib = 1;
11426 }
11427
11428 /* Establish operand size encoding. */
11429 operand_type_set (&combined, 0);
11430
11431 for (j = i.imm_operands; j < i.operands; ++j)
11432 {
11433 i.types[j].bitfield.instance = InstanceNone;
11434
11435 if (operand_type_check (i.types[j], disp))
11436 {
11437 i.types[j].bitfield.baseindex = 1;
11438 disp_exp = i.op[j].disps;
11439 }
11440
11441 if (evex && i.types[j].bitfield.baseindex)
11442 {
11443 unsigned int n = i.memshift;
11444
11445 if (i.types[j].bitfield.byte)
11446 n = 0;
11447 else if (i.types[j].bitfield.word)
11448 n = 1;
11449 else if (i.types[j].bitfield.dword)
11450 n = 2;
11451 else if (i.types[j].bitfield.qword)
11452 n = 3;
11453 else if (i.types[j].bitfield.xmmword)
11454 n = 4;
11455 else if (i.types[j].bitfield.ymmword)
11456 n = 5;
11457 else if (i.types[j].bitfield.zmmword)
11458 n = 6;
11459
11460 if (i.memshift < 32 && n != i.memshift)
11461 as_warn ("conflicting memory operand size specifiers");
11462 i.memshift = n;
11463 }
11464
11465 if ((i.broadcast.type || i.broadcast.bytes)
11466 && j == i.broadcast.operand)
11467 continue;
11468
11469 combined = operand_type_or (combined, i.types[j]);
11470 combined.bitfield.class = ClassNone;
11471 }
11472
11473 switch ((i.broadcast.type ? i.broadcast.type : 1)
11474 << (i.memshift < 32 ? i.memshift : 0))
11475 {
11476 case 64: combined.bitfield.zmmword = 1; break;
11477 case 32: combined.bitfield.ymmword = 1; break;
11478 case 16: combined.bitfield.xmmword = 1; break;
11479 case 8: combined.bitfield.qword = 1; break;
11480 case 4: combined.bitfield.dword = 1; break;
11481 }
11482
11483 if (i.vec_encoding == vex_encoding_default)
11484 {
11485 if (flag_code == CODE_64BIT && combined.bitfield.qword)
11486 i.rex |= REX_W;
11487 else if ((flag_code == CODE_16BIT ? combined.bitfield.dword
11488 : combined.bitfield.word)
11489 && !add_prefix (DATA_PREFIX_OPCODE))
11490 goto done;
11491 }
11492 else if (!i.tm.opcode_modifier.vexw)
11493 {
11494 if (flag_code == CODE_64BIT)
11495 {
11496 if (combined.bitfield.qword)
11497 i.tm.opcode_modifier.vexw = VEXW1;
11498 else if (combined.bitfield.dword)
11499 i.tm.opcode_modifier.vexw = VEXW0;
11500 }
11501
11502 if (!i.tm.opcode_modifier.vexw)
11503 i.tm.opcode_modifier.vexw = VEXWIG;
11504 }
11505
11506 if (vex || xop)
11507 {
11508 if (!i.tm.opcode_modifier.vex)
11509 {
11510 if (combined.bitfield.ymmword)
11511 i.tm.opcode_modifier.vex = VEX256;
11512 else if (combined.bitfield.xmmword)
11513 i.tm.opcode_modifier.vex = VEX128;
11514 }
11515 }
11516 else if (evex)
11517 {
11518 if (!i.tm.opcode_modifier.evex)
11519 {
11520 /* Do _not_ consider AVX512VL here. */
11521 if (i.rounding.type != rc_none || combined.bitfield.zmmword)
11522 i.tm.opcode_modifier.evex = EVEX512;
11523 else if (combined.bitfield.ymmword)
11524 i.tm.opcode_modifier.evex = EVEX256;
11525 else if (combined.bitfield.xmmword)
11526 i.tm.opcode_modifier.evex = EVEX128;
11527 }
11528
11529 if (i.memshift >= 32)
11530 {
11531 unsigned int n = 0;
11532
11533 switch (i.tm.opcode_modifier.evex)
11534 {
11535 case EVEX512: n = 64; break;
11536 case EVEX256: n = 32; break;
11537 case EVEX128: n = 16; break;
11538 }
11539
11540 if (i.broadcast.type)
11541 n /= i.broadcast.type;
11542
11543 if (n > 0)
11544 for (i.memshift = 0; !(n & 1); n >>= 1)
11545 ++i.memshift;
11546 else if (disp_exp != NULL && disp_exp->X_op == O_constant
11547 && disp_exp->X_add_number != 0
11548 && i.disp_encoding != disp_encoding_32bit)
11549 {
11550 if (!quiet_warnings)
11551 as_warn ("cannot determine memory operand size");
11552 i.disp_encoding = disp_encoding_32bit;
11553 }
11554 }
11555 }
11556
11557 if (i.memshift >= 32)
11558 i.memshift = 0;
11559 else if (!evex)
11560 i.vec_encoding = vex_encoding_error;
11561
11562 if (i.disp_operands && !optimize_disp (&i.tm))
11563 goto done;
11564
11565 /* Establish size for immediate operands. */
11566 for (j = 0; j < i.imm_operands; ++j)
11567 {
11568 expressionS *expP = i.op[j].imms;
11569
11570 gas_assert (operand_type_check (i.types[j], imm));
11571 operand_type_set (&i.types[j], 0);
11572
11573 if (i.imm_bits[j] > 32)
11574 i.types[j].bitfield.imm64 = 1;
11575 else if (i.imm_bits[j] > 16)
11576 {
11577 if (flag_code == CODE_64BIT && (i.flags[j] & Operand_Signed))
11578 i.types[j].bitfield.imm32s = 1;
11579 else
11580 i.types[j].bitfield.imm32 = 1;
11581 }
11582 else if (i.imm_bits[j] > 8)
11583 i.types[j].bitfield.imm16 = 1;
11584 else if (i.imm_bits[j] > 0)
11585 {
11586 if (i.flags[j] & Operand_Signed)
11587 i.types[j].bitfield.imm8s = 1;
11588 else
11589 i.types[j].bitfield.imm8 = 1;
11590 }
11591 else if (expP->X_op == O_constant)
11592 {
11593 i.types[j] = smallest_imm_type (expP->X_add_number);
11594 i.types[j].bitfield.imm1 = 0;
11595 /* Oddly enough imm_size() checks imm64 first, so the bit needs
11596 zapping since smallest_imm_type() sets it unconditionally. */
11597 if (flag_code != CODE_64BIT)
11598 {
11599 i.types[j].bitfield.imm64 = 0;
11600 i.types[j].bitfield.imm32s = 0;
11601 i.types[j].bitfield.imm32 = 1;
11602 }
11603 else if (i.types[j].bitfield.imm32 || i.types[j].bitfield.imm32s)
11604 i.types[j].bitfield.imm64 = 0;
11605 }
11606 else
11607 /* Non-constant expressions are sized heuristically. */
11608 switch (flag_code)
11609 {
11610 case CODE_64BIT: i.types[j].bitfield.imm32s = 1; break;
11611 case CODE_32BIT: i.types[j].bitfield.imm32 = 1; break;
11612 case CODE_16BIT: i.types[j].bitfield.imm16 = 1; break;
11613 }
11614 }
11615
11616 for (j = 0; j < i.operands; ++j)
11617 i.tm.operand_types[j] = i.types[j];
11618
11619 process_operands ();
11620 }
11621
11622 /* Don't set opcode until after processing operands, to avoid any
11623 potential special casing there. */
11624 i.tm.base_opcode |= val;
11625
11626 if (i.vec_encoding == vex_encoding_error
11627 || (i.vec_encoding != vex_encoding_evex
11628 ? i.broadcast.type || i.broadcast.bytes
11629 || i.rounding.type != rc_none
11630 || i.mask.reg
11631 : (i.mem_operands && i.rounding.type != rc_none)
11632 || ((i.broadcast.type || i.broadcast.bytes)
11633 && !(i.flags[i.broadcast.operand] & Operand_Mem))))
11634 {
11635 as_bad (_("conflicting .insn operands"));
11636 goto done;
11637 }
11638
11639 if (vex || xop)
11640 {
11641 if (!i.tm.opcode_modifier.vex)
11642 i.tm.opcode_modifier.vex = VEXScalar; /* LIG */
11643
11644 build_vex_prefix (NULL);
11645 i.rex &= REX_OPCODE;
11646 }
11647 else if (evex)
11648 {
11649 if (!i.tm.opcode_modifier.evex)
11650 i.tm.opcode_modifier.evex = EVEXLIG;
11651
11652 build_evex_prefix ();
11653 i.rex &= REX_OPCODE;
11654 }
11655 else if (i.rex != 0)
11656 add_prefix (REX_OPCODE | i.rex);
11657
11658 output_insn ();
11659
11660 done:
11661 *saved_ilp = saved_char;
11662 input_line_pointer = line;
11663
11664 demand_empty_rest_of_line ();
11665
11666 /* Make sure dot_insn() won't yield "true" anymore. */
11667 i.tm.mnem_off = 0;
11668 }
11669
11670 #ifdef TE_PE
11671 static void
11672 pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
11673 {
11674 expressionS exp;
11675
11676 do
11677 {
11678 expression (&exp);
11679 if (exp.X_op == O_symbol)
11680 exp.X_op = O_secrel;
11681
11682 emit_expr (&exp, 4);
11683 }
11684 while (*input_line_pointer++ == ',');
11685
11686 input_line_pointer--;
11687 demand_empty_rest_of_line ();
11688 }
11689
11690 static void
11691 pe_directive_secidx (int dummy ATTRIBUTE_UNUSED)
11692 {
11693 expressionS exp;
11694
11695 do
11696 {
11697 expression (&exp);
11698 if (exp.X_op == O_symbol)
11699 exp.X_op = O_secidx;
11700
11701 emit_expr (&exp, 2);
11702 }
11703 while (*input_line_pointer++ == ',');
11704
11705 input_line_pointer--;
11706 demand_empty_rest_of_line ();
11707 }
11708 #endif
11709
11710 /* Handle Rounding Control / SAE specifiers. */
11711
11712 static char *
11713 RC_SAE_specifier (const char *pstr)
11714 {
11715 unsigned int j;
11716
11717 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); j++)
11718 {
11719 if (!strncmp (pstr, RC_NamesTable[j].name, RC_NamesTable[j].len))
11720 {
11721 if (i.rounding.type != rc_none)
11722 {
11723 as_bad (_("duplicated `{%s}'"), RC_NamesTable[j].name);
11724 return NULL;
11725 }
11726
11727 if (i.vec_encoding == vex_encoding_default)
11728 i.vec_encoding = vex_encoding_evex512;
11729 else if (i.vec_encoding != vex_encoding_evex
11730 && i.vec_encoding != vex_encoding_evex512)
11731 return NULL;
11732
11733 i.rounding.type = RC_NamesTable[j].type;
11734
11735 return (char *)(pstr + RC_NamesTable[j].len);
11736 }
11737 }
11738
11739 return NULL;
11740 }
11741
11742 /* Handle Vector operations. */
11743
11744 static char *
11745 check_VecOperations (char *op_string)
11746 {
11747 const reg_entry *mask;
11748 const char *saved;
11749 char *end_op;
11750
11751 while (*op_string)
11752 {
11753 saved = op_string;
11754 if (*op_string == '{')
11755 {
11756 op_string++;
11757
11758 /* Check broadcasts. */
11759 if (startswith (op_string, "1to"))
11760 {
11761 unsigned int bcst_type;
11762
11763 if (i.broadcast.type)
11764 goto duplicated_vec_op;
11765
11766 op_string += 3;
11767 if (*op_string == '8')
11768 bcst_type = 8;
11769 else if (*op_string == '4')
11770 bcst_type = 4;
11771 else if (*op_string == '2')
11772 bcst_type = 2;
11773 else if (*op_string == '1'
11774 && *(op_string+1) == '6')
11775 {
11776 bcst_type = 16;
11777 op_string++;
11778 }
11779 else if (*op_string == '3'
11780 && *(op_string+1) == '2')
11781 {
11782 bcst_type = 32;
11783 op_string++;
11784 }
11785 else
11786 {
11787 as_bad (_("Unsupported broadcast: `%s'"), saved);
11788 return NULL;
11789 }
11790 op_string++;
11791
11792 if (i.vec_encoding == vex_encoding_default)
11793 i.vec_encoding = vex_encoding_evex;
11794 else if (i.vec_encoding != vex_encoding_evex
11795 && i.vec_encoding != vex_encoding_evex512)
11796 goto unknown_vec_op;
11797
11798 i.broadcast.type = bcst_type;
11799 i.broadcast.operand = this_operand;
11800
11801 /* For .insn a data size specifier may be appended. */
11802 if (dot_insn () && *op_string == ':')
11803 goto dot_insn_modifier;
11804 }
11805 /* Check .insn special cases. */
11806 else if (dot_insn () && *op_string == ':')
11807 {
11808 dot_insn_modifier:
11809 switch (op_string[1])
11810 {
11811 unsigned long n;
11812
11813 case 'd':
11814 if (i.memshift < 32)
11815 goto duplicated_vec_op;
11816
11817 n = strtoul (op_string + 2, &end_op, 0);
11818 if (n)
11819 for (i.memshift = 0; !(n & 1); n >>= 1)
11820 ++i.memshift;
11821 if (i.memshift < 32 && n == 1)
11822 op_string = end_op;
11823 break;
11824
11825 case 's': case 'u':
11826 /* This isn't really a "vector" operation, but a sign/size
11827 specifier for immediate operands of .insn. Note that AT&T
11828 syntax handles the same in i386_immediate(). */
11829 if (!intel_syntax)
11830 break;
11831
11832 if (i.imm_bits[this_operand])
11833 goto duplicated_vec_op;
11834
11835 n = strtoul (op_string + 2, &end_op, 0);
11836 if (n && n <= (flag_code == CODE_64BIT ? 64 : 32))
11837 {
11838 i.imm_bits[this_operand] = n;
11839 if (op_string[1] == 's')
11840 i.flags[this_operand] |= Operand_Signed;
11841 op_string = end_op;
11842 }
11843 break;
11844 }
11845 }
11846 /* Check masking operation. */
11847 else if ((mask = parse_register (op_string, &end_op)) != NULL)
11848 {
11849 if (mask == &bad_reg)
11850 return NULL;
11851
11852 /* k0 can't be used for write mask. */
11853 if (mask->reg_type.bitfield.class != RegMask || !mask->reg_num)
11854 {
11855 as_bad (_("`%s%s' can't be used for write mask"),
11856 register_prefix, mask->reg_name);
11857 return NULL;
11858 }
11859
11860 if (!i.mask.reg)
11861 {
11862 i.mask.reg = mask;
11863 i.mask.operand = this_operand;
11864 }
11865 else if (i.mask.reg->reg_num)
11866 goto duplicated_vec_op;
11867 else
11868 {
11869 i.mask.reg = mask;
11870
11871 /* Only "{z}" is allowed here. No need to check
11872 zeroing mask explicitly. */
11873 if (i.mask.operand != (unsigned int) this_operand)
11874 {
11875 as_bad (_("invalid write mask `%s'"), saved);
11876 return NULL;
11877 }
11878 }
11879
11880 op_string = end_op;
11881 }
11882 /* Check zeroing-flag for masking operation. */
11883 else if (*op_string == 'z')
11884 {
11885 if (!i.mask.reg)
11886 {
11887 i.mask.reg = reg_k0;
11888 i.mask.zeroing = 1;
11889 i.mask.operand = this_operand;
11890 }
11891 else
11892 {
11893 if (i.mask.zeroing)
11894 {
11895 duplicated_vec_op:
11896 as_bad (_("duplicated `%s'"), saved);
11897 return NULL;
11898 }
11899
11900 i.mask.zeroing = 1;
11901
11902 /* Only "{%k}" is allowed here. No need to check mask
11903 register explicitly. */
11904 if (i.mask.operand != (unsigned int) this_operand)
11905 {
11906 as_bad (_("invalid zeroing-masking `%s'"),
11907 saved);
11908 return NULL;
11909 }
11910 }
11911
11912 op_string++;
11913 }
11914 else if (intel_syntax
11915 && (op_string = RC_SAE_specifier (op_string)) != NULL)
11916 i.rounding.modifier = true;
11917 else
11918 goto unknown_vec_op;
11919
11920 if (*op_string != '}')
11921 {
11922 as_bad (_("missing `}' in `%s'"), saved);
11923 return NULL;
11924 }
11925 op_string++;
11926
11927 /* Strip whitespace since the addition of pseudo prefixes
11928 changed how the scrubber treats '{'. */
11929 if (is_space_char (*op_string))
11930 ++op_string;
11931
11932 continue;
11933 }
11934 unknown_vec_op:
11935 /* We don't know this one. */
11936 as_bad (_("unknown vector operation: `%s'"), saved);
11937 return NULL;
11938 }
11939
11940 if (i.mask.reg && i.mask.zeroing && !i.mask.reg->reg_num)
11941 {
11942 as_bad (_("zeroing-masking only allowed with write mask"));
11943 return NULL;
11944 }
11945
11946 return op_string;
11947 }
11948
11949 static int
11950 i386_immediate (char *imm_start)
11951 {
11952 char *save_input_line_pointer;
11953 char *gotfree_input_line;
11954 segT exp_seg = 0;
11955 expressionS *exp;
11956 i386_operand_type types;
11957
11958 operand_type_set (&types, ~0);
11959
11960 if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
11961 {
11962 as_bad (_("at most %d immediate operands are allowed"),
11963 MAX_IMMEDIATE_OPERANDS);
11964 return 0;
11965 }
11966
11967 exp = &im_expressions[i.imm_operands++];
11968 i.op[this_operand].imms = exp;
11969
11970 if (is_space_char (*imm_start))
11971 ++imm_start;
11972
11973 save_input_line_pointer = input_line_pointer;
11974 input_line_pointer = imm_start;
11975
11976 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
11977 if (gotfree_input_line)
11978 input_line_pointer = gotfree_input_line;
11979
11980 expr_mode = expr_operator_none;
11981 exp_seg = expression (exp);
11982
11983 /* For .insn immediates there may be a size specifier. */
11984 if (dot_insn () && *input_line_pointer == '{' && input_line_pointer[1] == ':'
11985 && (input_line_pointer[2] == 's' || input_line_pointer[2] == 'u'))
11986 {
11987 char *e;
11988 unsigned long n = strtoul (input_line_pointer + 3, &e, 0);
11989
11990 if (*e == '}' && n && n <= (flag_code == CODE_64BIT ? 64 : 32))
11991 {
11992 i.imm_bits[this_operand] = n;
11993 if (input_line_pointer[2] == 's')
11994 i.flags[this_operand] |= Operand_Signed;
11995 input_line_pointer = e + 1;
11996 }
11997 }
11998
11999 SKIP_WHITESPACE ();
12000 if (*input_line_pointer)
12001 as_bad (_("junk `%s' after expression"), input_line_pointer);
12002
12003 input_line_pointer = save_input_line_pointer;
12004 if (gotfree_input_line)
12005 {
12006 free (gotfree_input_line);
12007
12008 if (exp->X_op == O_constant)
12009 exp->X_op = O_illegal;
12010 }
12011
12012 if (exp_seg == reg_section)
12013 {
12014 as_bad (_("illegal immediate register operand %s"), imm_start);
12015 return 0;
12016 }
12017
12018 return i386_finalize_immediate (exp_seg, exp, types, imm_start);
12019 }
12020
12021 static int
12022 i386_finalize_immediate (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
12023 i386_operand_type types, const char *imm_start)
12024 {
12025 if (exp->X_op == O_absent || exp->X_op == O_illegal || exp->X_op == O_big)
12026 {
12027 if (imm_start)
12028 as_bad (_("missing or invalid immediate expression `%s'"),
12029 imm_start);
12030 return 0;
12031 }
12032 else if (exp->X_op == O_constant)
12033 {
12034 /* Size it properly later. */
12035 i.types[this_operand].bitfield.imm64 = 1;
12036
12037 /* If not 64bit, sign/zero extend val, to account for wraparound
12038 when !BFD64. */
12039 if (expr_mode == expr_operator_present
12040 && flag_code != CODE_64BIT && !object_64bit)
12041 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
12042 }
12043 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
12044 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
12045 && exp_seg != absolute_section
12046 && exp_seg != text_section
12047 && exp_seg != data_section
12048 && exp_seg != bss_section
12049 && exp_seg != undefined_section
12050 && !bfd_is_com_section (exp_seg))
12051 {
12052 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
12053 return 0;
12054 }
12055 #endif
12056 else
12057 {
12058 /* This is an address. The size of the address will be
12059 determined later, depending on destination register,
12060 suffix, or the default for the section. */
12061 i.types[this_operand].bitfield.imm8 = 1;
12062 i.types[this_operand].bitfield.imm16 = 1;
12063 i.types[this_operand].bitfield.imm32 = 1;
12064 i.types[this_operand].bitfield.imm32s = 1;
12065 i.types[this_operand].bitfield.imm64 = 1;
12066 i.types[this_operand] = operand_type_and (i.types[this_operand],
12067 types);
12068 }
12069
12070 return 1;
12071 }
12072
12073 static char *
12074 i386_scale (char *scale)
12075 {
12076 offsetT val;
12077 char *save = input_line_pointer;
12078
12079 input_line_pointer = scale;
12080 val = get_absolute_expression ();
12081
12082 switch (val)
12083 {
12084 case 1:
12085 i.log2_scale_factor = 0;
12086 break;
12087 case 2:
12088 i.log2_scale_factor = 1;
12089 break;
12090 case 4:
12091 i.log2_scale_factor = 2;
12092 break;
12093 case 8:
12094 i.log2_scale_factor = 3;
12095 break;
12096 default:
12097 {
12098 char sep = *input_line_pointer;
12099
12100 *input_line_pointer = '\0';
12101 as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
12102 scale);
12103 *input_line_pointer = sep;
12104 input_line_pointer = save;
12105 return NULL;
12106 }
12107 }
12108 if (i.log2_scale_factor != 0 && i.index_reg == 0)
12109 {
12110 as_warn (_("scale factor of %d without an index register"),
12111 1 << i.log2_scale_factor);
12112 i.log2_scale_factor = 0;
12113 }
12114 scale = input_line_pointer;
12115 input_line_pointer = save;
12116 return scale;
12117 }
12118
12119 static int
12120 i386_displacement (char *disp_start, char *disp_end)
12121 {
12122 expressionS *exp;
12123 segT exp_seg = 0;
12124 char *save_input_line_pointer;
12125 char *gotfree_input_line;
12126 int override;
12127 i386_operand_type bigdisp, types = anydisp;
12128 int ret;
12129
12130 if (i.disp_operands == MAX_MEMORY_OPERANDS)
12131 {
12132 as_bad (_("at most %d displacement operands are allowed"),
12133 MAX_MEMORY_OPERANDS);
12134 return 0;
12135 }
12136
12137 operand_type_set (&bigdisp, 0);
12138 if (i.jumpabsolute
12139 || i.types[this_operand].bitfield.baseindex
12140 || (current_templates->start->opcode_modifier.jump != JUMP
12141 && current_templates->start->opcode_modifier.jump != JUMP_DWORD))
12142 {
12143 i386_addressing_mode ();
12144 override = (i.prefix[ADDR_PREFIX] != 0);
12145 if (flag_code == CODE_64BIT)
12146 {
12147 bigdisp.bitfield.disp32 = 1;
12148 if (!override)
12149 bigdisp.bitfield.disp64 = 1;
12150 }
12151 else if ((flag_code == CODE_16BIT) ^ override)
12152 bigdisp.bitfield.disp16 = 1;
12153 else
12154 bigdisp.bitfield.disp32 = 1;
12155 }
12156 else
12157 {
12158 /* For PC-relative branches, the width of the displacement may be
12159 dependent upon data size, but is never dependent upon address size.
12160 Also make sure to not unintentionally match against a non-PC-relative
12161 branch template. */
12162 static templates aux_templates;
12163 const insn_template *t = current_templates->start;
12164 bool has_intel64 = false;
12165
12166 aux_templates.start = t;
12167 while (++t < current_templates->end)
12168 {
12169 if (t->opcode_modifier.jump
12170 != current_templates->start->opcode_modifier.jump)
12171 break;
12172 if ((t->opcode_modifier.isa64 >= INTEL64))
12173 has_intel64 = true;
12174 }
12175 if (t < current_templates->end)
12176 {
12177 aux_templates.end = t;
12178 current_templates = &aux_templates;
12179 }
12180
12181 override = (i.prefix[DATA_PREFIX] != 0);
12182 if (flag_code == CODE_64BIT)
12183 {
12184 if ((override || i.suffix == WORD_MNEM_SUFFIX)
12185 && (!intel64 || !has_intel64))
12186 bigdisp.bitfield.disp16 = 1;
12187 else
12188 bigdisp.bitfield.disp32 = 1;
12189 }
12190 else
12191 {
12192 if (!override)
12193 override = (i.suffix == (flag_code != CODE_16BIT
12194 ? WORD_MNEM_SUFFIX
12195 : LONG_MNEM_SUFFIX));
12196 bigdisp.bitfield.disp32 = 1;
12197 if ((flag_code == CODE_16BIT) ^ override)
12198 {
12199 bigdisp.bitfield.disp32 = 0;
12200 bigdisp.bitfield.disp16 = 1;
12201 }
12202 }
12203 }
12204 i.types[this_operand] = operand_type_or (i.types[this_operand],
12205 bigdisp);
12206
12207 exp = &disp_expressions[i.disp_operands];
12208 i.op[this_operand].disps = exp;
12209 i.disp_operands++;
12210 save_input_line_pointer = input_line_pointer;
12211 input_line_pointer = disp_start;
12212 END_STRING_AND_SAVE (disp_end);
12213
12214 #ifndef GCC_ASM_O_HACK
12215 #define GCC_ASM_O_HACK 0
12216 #endif
12217 #if GCC_ASM_O_HACK
12218 END_STRING_AND_SAVE (disp_end + 1);
12219 if (i.types[this_operand].bitfield.baseIndex
12220 && displacement_string_end[-1] == '+')
12221 {
12222 /* This hack is to avoid a warning when using the "o"
12223 constraint within gcc asm statements.
12224 For instance:
12225
12226 #define _set_tssldt_desc(n,addr,limit,type) \
12227 __asm__ __volatile__ ( \
12228 "movw %w2,%0\n\t" \
12229 "movw %w1,2+%0\n\t" \
12230 "rorl $16,%1\n\t" \
12231 "movb %b1,4+%0\n\t" \
12232 "movb %4,5+%0\n\t" \
12233 "movb $0,6+%0\n\t" \
12234 "movb %h1,7+%0\n\t" \
12235 "rorl $16,%1" \
12236 : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
12237
12238 This works great except that the output assembler ends
12239 up looking a bit weird if it turns out that there is
12240 no offset. You end up producing code that looks like:
12241
12242 #APP
12243 movw $235,(%eax)
12244 movw %dx,2+(%eax)
12245 rorl $16,%edx
12246 movb %dl,4+(%eax)
12247 movb $137,5+(%eax)
12248 movb $0,6+(%eax)
12249 movb %dh,7+(%eax)
12250 rorl $16,%edx
12251 #NO_APP
12252
12253 So here we provide the missing zero. */
12254
12255 *displacement_string_end = '0';
12256 }
12257 #endif
12258 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
12259 if (gotfree_input_line)
12260 input_line_pointer = gotfree_input_line;
12261
12262 expr_mode = expr_operator_none;
12263 exp_seg = expression (exp);
12264
12265 SKIP_WHITESPACE ();
12266 if (*input_line_pointer)
12267 as_bad (_("junk `%s' after expression"), input_line_pointer);
12268 #if GCC_ASM_O_HACK
12269 RESTORE_END_STRING (disp_end + 1);
12270 #endif
12271 input_line_pointer = save_input_line_pointer;
12272 if (gotfree_input_line)
12273 {
12274 free (gotfree_input_line);
12275
12276 if (exp->X_op == O_constant || exp->X_op == O_register)
12277 exp->X_op = O_illegal;
12278 }
12279
12280 ret = i386_finalize_displacement (exp_seg, exp, types, disp_start);
12281
12282 RESTORE_END_STRING (disp_end);
12283
12284 return ret;
12285 }
12286
12287 static int
12288 i386_finalize_displacement (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
12289 i386_operand_type types, const char *disp_start)
12290 {
12291 int ret = 1;
12292
12293 /* We do this to make sure that the section symbol is in
12294 the symbol table. We will ultimately change the relocation
12295 to be relative to the beginning of the section. */
12296 if (i.reloc[this_operand] == BFD_RELOC_386_GOTOFF
12297 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
12298 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
12299 {
12300 if (exp->X_op != O_symbol)
12301 goto inv_disp;
12302
12303 if (S_IS_LOCAL (exp->X_add_symbol)
12304 && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section
12305 && S_GET_SEGMENT (exp->X_add_symbol) != expr_section)
12306 section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
12307 exp->X_op = O_subtract;
12308 exp->X_op_symbol = GOT_symbol;
12309 if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
12310 i.reloc[this_operand] = BFD_RELOC_32_PCREL;
12311 else if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
12312 i.reloc[this_operand] = BFD_RELOC_64;
12313 else
12314 i.reloc[this_operand] = BFD_RELOC_32;
12315 }
12316
12317 else if (exp->X_op == O_absent
12318 || exp->X_op == O_illegal
12319 || exp->X_op == O_big)
12320 {
12321 inv_disp:
12322 as_bad (_("missing or invalid displacement expression `%s'"),
12323 disp_start);
12324 ret = 0;
12325 }
12326
12327 else if (exp->X_op == O_constant)
12328 {
12329 /* Sizing gets taken care of by optimize_disp().
12330
12331 If not 64bit, sign/zero extend val, to account for wraparound
12332 when !BFD64. */
12333 if (expr_mode == expr_operator_present
12334 && flag_code != CODE_64BIT && !object_64bit)
12335 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
12336 }
12337
12338 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
12339 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
12340 && exp_seg != absolute_section
12341 && exp_seg != text_section
12342 && exp_seg != data_section
12343 && exp_seg != bss_section
12344 && exp_seg != undefined_section
12345 && !bfd_is_com_section (exp_seg))
12346 {
12347 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
12348 ret = 0;
12349 }
12350 #endif
12351
12352 else if (current_templates->start->opcode_modifier.jump == JUMP_BYTE)
12353 i.types[this_operand].bitfield.disp8 = 1;
12354
12355 /* Check if this is a displacement only operand. */
12356 if (!i.types[this_operand].bitfield.baseindex)
12357 i.types[this_operand] =
12358 operand_type_or (operand_type_and_not (i.types[this_operand], anydisp),
12359 operand_type_and (i.types[this_operand], types));
12360
12361 return ret;
12362 }
12363
12364 /* Return the active addressing mode, taking address override and
12365 registers forming the address into consideration. Update the
12366 address override prefix if necessary. */
12367
12368 static enum flag_code
12369 i386_addressing_mode (void)
12370 {
12371 enum flag_code addr_mode;
12372
12373 if (i.prefix[ADDR_PREFIX])
12374 addr_mode = flag_code == CODE_32BIT ? CODE_16BIT : CODE_32BIT;
12375 else if (flag_code == CODE_16BIT
12376 && is_cpu (current_templates->start, CpuMPX)
12377 /* Avoid replacing the "16-bit addressing not allowed" diagnostic
12378 from md_assemble() by "is not a valid base/index expression"
12379 when there is a base and/or index. */
12380 && !i.types[this_operand].bitfield.baseindex)
12381 {
12382 /* MPX insn memory operands with neither base nor index must be forced
12383 to use 32-bit addressing in 16-bit mode. */
12384 addr_mode = CODE_32BIT;
12385 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
12386 ++i.prefixes;
12387 gas_assert (!i.types[this_operand].bitfield.disp16);
12388 gas_assert (!i.types[this_operand].bitfield.disp32);
12389 }
12390 else
12391 {
12392 addr_mode = flag_code;
12393
12394 #if INFER_ADDR_PREFIX
12395 if (i.mem_operands == 0)
12396 {
12397 /* Infer address prefix from the first memory operand. */
12398 const reg_entry *addr_reg = i.base_reg;
12399
12400 if (addr_reg == NULL)
12401 addr_reg = i.index_reg;
12402
12403 if (addr_reg)
12404 {
12405 if (addr_reg->reg_type.bitfield.dword)
12406 addr_mode = CODE_32BIT;
12407 else if (flag_code != CODE_64BIT
12408 && addr_reg->reg_type.bitfield.word)
12409 addr_mode = CODE_16BIT;
12410
12411 if (addr_mode != flag_code)
12412 {
12413 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
12414 i.prefixes += 1;
12415 /* Change the size of any displacement too. At most one
12416 of Disp16 or Disp32 is set.
12417 FIXME. There doesn't seem to be any real need for
12418 separate Disp16 and Disp32 flags. The same goes for
12419 Imm16 and Imm32. Removing them would probably clean
12420 up the code quite a lot. */
12421 if (flag_code != CODE_64BIT
12422 && (i.types[this_operand].bitfield.disp16
12423 || i.types[this_operand].bitfield.disp32))
12424 {
12425 static const i386_operand_type disp16_32 = {
12426 .bitfield = { .disp16 = 1, .disp32 = 1 }
12427 };
12428
12429 i.types[this_operand]
12430 = operand_type_xor (i.types[this_operand], disp16_32);
12431 }
12432 }
12433 }
12434 }
12435 #endif
12436 }
12437
12438 return addr_mode;
12439 }
12440
12441 /* Make sure the memory operand we've been dealt is valid.
12442 Return 1 on success, 0 on a failure. */
12443
12444 static int
12445 i386_index_check (const char *operand_string)
12446 {
12447 const char *kind = "base/index";
12448 enum flag_code addr_mode = i386_addressing_mode ();
12449 const insn_template *t = current_templates->end - 1;
12450
12451 if (t->opcode_modifier.isstring)
12452 {
12453 /* Memory operands of string insns are special in that they only allow
12454 a single register (rDI, rSI, or rBX) as their memory address. */
12455 const reg_entry *expected_reg;
12456 static const char di_si[][2][4] =
12457 {
12458 { "esi", "edi" },
12459 { "si", "di" },
12460 { "rsi", "rdi" }
12461 };
12462 static const char bx[][4] = { "ebx", "bx", "rbx" };
12463
12464 kind = "string address";
12465
12466 if (t->opcode_modifier.prefixok == PrefixRep)
12467 {
12468 int es_op = t->opcode_modifier.isstring - IS_STRING_ES_OP0;
12469 int op = 0;
12470
12471 if (!t->operand_types[0].bitfield.baseindex
12472 || ((!i.mem_operands != !intel_syntax)
12473 && t->operand_types[1].bitfield.baseindex))
12474 op = 1;
12475 expected_reg
12476 = (const reg_entry *) str_hash_find (reg_hash,
12477 di_si[addr_mode][op == es_op]);
12478 }
12479 else
12480 expected_reg
12481 = (const reg_entry *)str_hash_find (reg_hash, bx[addr_mode]);
12482
12483 if (i.base_reg != expected_reg
12484 || i.index_reg
12485 || operand_type_check (i.types[this_operand], disp))
12486 {
12487 /* The second memory operand must have the same size as
12488 the first one. */
12489 if (i.mem_operands
12490 && i.base_reg
12491 && !((addr_mode == CODE_64BIT
12492 && i.base_reg->reg_type.bitfield.qword)
12493 || (addr_mode == CODE_32BIT
12494 ? i.base_reg->reg_type.bitfield.dword
12495 : i.base_reg->reg_type.bitfield.word)))
12496 goto bad_address;
12497
12498 as_warn (_("`%s' is not valid here (expected `%c%s%s%c')"),
12499 operand_string,
12500 intel_syntax ? '[' : '(',
12501 register_prefix,
12502 expected_reg->reg_name,
12503 intel_syntax ? ']' : ')');
12504 return 1;
12505 }
12506 else
12507 return 1;
12508
12509 bad_address:
12510 as_bad (_("`%s' is not a valid %s expression"),
12511 operand_string, kind);
12512 return 0;
12513 }
12514 else
12515 {
12516 t = current_templates->start;
12517
12518 if (addr_mode != CODE_16BIT)
12519 {
12520 /* 32-bit/64-bit checks. */
12521 if (i.disp_encoding == disp_encoding_16bit)
12522 {
12523 bad_disp:
12524 as_bad (_("invalid `%s' prefix"),
12525 addr_mode == CODE_16BIT ? "{disp32}" : "{disp16}");
12526 return 0;
12527 }
12528
12529 if ((i.base_reg
12530 && ((addr_mode == CODE_64BIT
12531 ? !i.base_reg->reg_type.bitfield.qword
12532 : !i.base_reg->reg_type.bitfield.dword)
12533 || (i.index_reg && i.base_reg->reg_num == RegIP)
12534 || i.base_reg->reg_num == RegIZ))
12535 || (i.index_reg
12536 && !i.index_reg->reg_type.bitfield.xmmword
12537 && !i.index_reg->reg_type.bitfield.ymmword
12538 && !i.index_reg->reg_type.bitfield.zmmword
12539 && ((addr_mode == CODE_64BIT
12540 ? !i.index_reg->reg_type.bitfield.qword
12541 : !i.index_reg->reg_type.bitfield.dword)
12542 || !i.index_reg->reg_type.bitfield.baseindex)))
12543 goto bad_address;
12544
12545 /* bndmk, bndldx, bndstx and mandatory non-vector SIB have special restrictions. */
12546 if (t->mnem_off == MN_bndmk
12547 || t->mnem_off == MN_bndldx
12548 || t->mnem_off == MN_bndstx
12549 || t->opcode_modifier.sib == SIBMEM)
12550 {
12551 /* They cannot use RIP-relative addressing. */
12552 if (i.base_reg && i.base_reg->reg_num == RegIP)
12553 {
12554 as_bad (_("`%s' cannot be used here"), operand_string);
12555 return 0;
12556 }
12557
12558 /* bndldx and bndstx ignore their scale factor. */
12559 if ((t->mnem_off == MN_bndldx || t->mnem_off == MN_bndstx)
12560 && i.log2_scale_factor)
12561 as_warn (_("register scaling is being ignored here"));
12562 }
12563 }
12564 else
12565 {
12566 /* 16-bit checks. */
12567 if (i.disp_encoding == disp_encoding_32bit)
12568 goto bad_disp;
12569
12570 if ((i.base_reg
12571 && (!i.base_reg->reg_type.bitfield.word
12572 || !i.base_reg->reg_type.bitfield.baseindex))
12573 || (i.index_reg
12574 && (!i.index_reg->reg_type.bitfield.word
12575 || !i.index_reg->reg_type.bitfield.baseindex
12576 || !(i.base_reg
12577 && i.base_reg->reg_num < 6
12578 && i.index_reg->reg_num >= 6
12579 && i.log2_scale_factor == 0))))
12580 goto bad_address;
12581 }
12582 }
12583 return 1;
12584 }
12585
12586 /* Handle vector immediates. */
12587
12588 static int
12589 RC_SAE_immediate (const char *imm_start)
12590 {
12591 const char *pstr = imm_start;
12592
12593 if (*pstr != '{')
12594 return 0;
12595
12596 pstr = RC_SAE_specifier (pstr + 1);
12597 if (pstr == NULL)
12598 return 0;
12599
12600 if (*pstr++ != '}')
12601 {
12602 as_bad (_("Missing '}': '%s'"), imm_start);
12603 return 0;
12604 }
12605 /* RC/SAE immediate string should contain nothing more. */;
12606 if (*pstr != 0)
12607 {
12608 as_bad (_("Junk after '}': '%s'"), imm_start);
12609 return 0;
12610 }
12611
12612 /* Internally this doesn't count as an operand. */
12613 --i.operands;
12614
12615 return 1;
12616 }
12617
12618 static INLINE bool starts_memory_operand (char c)
12619 {
12620 return ISDIGIT (c)
12621 || is_name_beginner (c)
12622 || strchr ("([\"+-!~", c);
12623 }
12624
12625 /* Parse OPERAND_STRING into the i386_insn structure I. Returns zero
12626 on error. */
12627
12628 static int
12629 i386_att_operand (char *operand_string)
12630 {
12631 const reg_entry *r;
12632 char *end_op;
12633 char *op_string = operand_string;
12634
12635 if (is_space_char (*op_string))
12636 ++op_string;
12637
12638 /* We check for an absolute prefix (differentiating,
12639 for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
12640 if (*op_string == ABSOLUTE_PREFIX
12641 && current_templates->start->opcode_modifier.jump)
12642 {
12643 ++op_string;
12644 if (is_space_char (*op_string))
12645 ++op_string;
12646 i.jumpabsolute = true;
12647 }
12648
12649 /* Check if operand is a register. */
12650 if ((r = parse_register (op_string, &end_op)) != NULL)
12651 {
12652 i386_operand_type temp;
12653
12654 if (r == &bad_reg)
12655 return 0;
12656
12657 /* Check for a segment override by searching for ':' after a
12658 segment register. */
12659 op_string = end_op;
12660 if (is_space_char (*op_string))
12661 ++op_string;
12662 if (*op_string == ':' && r->reg_type.bitfield.class == SReg)
12663 {
12664 i.seg[i.mem_operands] = r;
12665
12666 /* Skip the ':' and whitespace. */
12667 ++op_string;
12668 if (is_space_char (*op_string))
12669 ++op_string;
12670
12671 /* Handle case of %es:*foo. */
12672 if (!i.jumpabsolute && *op_string == ABSOLUTE_PREFIX
12673 && current_templates->start->opcode_modifier.jump)
12674 {
12675 ++op_string;
12676 if (is_space_char (*op_string))
12677 ++op_string;
12678 i.jumpabsolute = true;
12679 }
12680
12681 if (!starts_memory_operand (*op_string))
12682 {
12683 as_bad (_("bad memory operand `%s'"), op_string);
12684 return 0;
12685 }
12686 goto do_memory_reference;
12687 }
12688
12689 /* Handle vector operations. */
12690 if (*op_string == '{')
12691 {
12692 op_string = check_VecOperations (op_string);
12693 if (op_string == NULL)
12694 return 0;
12695 }
12696
12697 if (*op_string)
12698 {
12699 as_bad (_("junk `%s' after register"), op_string);
12700 return 0;
12701 }
12702
12703 /* Reject pseudo registers for .insn. */
12704 if (dot_insn () && r->reg_type.bitfield.class == ClassNone)
12705 {
12706 as_bad (_("`%s%s' cannot be used here"),
12707 register_prefix, r->reg_name);
12708 return 0;
12709 }
12710
12711 temp = r->reg_type;
12712 temp.bitfield.baseindex = 0;
12713 i.types[this_operand] = operand_type_or (i.types[this_operand],
12714 temp);
12715 i.types[this_operand].bitfield.unspecified = 0;
12716 i.op[this_operand].regs = r;
12717 i.reg_operands++;
12718
12719 /* A GPR may follow an RC or SAE immediate only if a (vector) register
12720 operand was also present earlier on. */
12721 if (i.rounding.type != rc_none && temp.bitfield.class == Reg
12722 && i.reg_operands == 1)
12723 {
12724 unsigned int j;
12725
12726 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); ++j)
12727 if (i.rounding.type == RC_NamesTable[j].type)
12728 break;
12729 as_bad (_("`%s': misplaced `{%s}'"),
12730 insn_name (current_templates->start), RC_NamesTable[j].name);
12731 return 0;
12732 }
12733 }
12734 else if (*op_string == REGISTER_PREFIX)
12735 {
12736 as_bad (_("bad register name `%s'"), op_string);
12737 return 0;
12738 }
12739 else if (*op_string == IMMEDIATE_PREFIX)
12740 {
12741 ++op_string;
12742 if (i.jumpabsolute)
12743 {
12744 as_bad (_("immediate operand illegal with absolute jump"));
12745 return 0;
12746 }
12747 if (!i386_immediate (op_string))
12748 return 0;
12749 if (i.rounding.type != rc_none)
12750 {
12751 as_bad (_("`%s': RC/SAE operand must follow immediate operands"),
12752 insn_name (current_templates->start));
12753 return 0;
12754 }
12755 }
12756 else if (RC_SAE_immediate (operand_string))
12757 {
12758 /* If it is a RC or SAE immediate, do the necessary placement check:
12759 Only another immediate or a GPR may precede it. */
12760 if (i.mem_operands || i.reg_operands + i.imm_operands > 1
12761 || (i.reg_operands == 1
12762 && i.op[0].regs->reg_type.bitfield.class != Reg))
12763 {
12764 as_bad (_("`%s': misplaced `%s'"),
12765 insn_name (current_templates->start), operand_string);
12766 return 0;
12767 }
12768 }
12769 else if (starts_memory_operand (*op_string))
12770 {
12771 /* This is a memory reference of some sort. */
12772 char *base_string;
12773
12774 /* Start and end of displacement string expression (if found). */
12775 char *displacement_string_start;
12776 char *displacement_string_end;
12777
12778 do_memory_reference:
12779 /* Check for base index form. We detect the base index form by
12780 looking for an ')' at the end of the operand, searching
12781 for the '(' matching it, and finding a REGISTER_PREFIX or ','
12782 after the '('. */
12783 base_string = op_string + strlen (op_string);
12784
12785 /* Handle vector operations. */
12786 --base_string;
12787 if (is_space_char (*base_string))
12788 --base_string;
12789
12790 if (*base_string == '}')
12791 {
12792 char *vop_start = NULL;
12793
12794 while (base_string-- > op_string)
12795 {
12796 if (*base_string == '"')
12797 break;
12798 if (*base_string != '{')
12799 continue;
12800
12801 vop_start = base_string;
12802
12803 --base_string;
12804 if (is_space_char (*base_string))
12805 --base_string;
12806
12807 if (*base_string != '}')
12808 break;
12809
12810 vop_start = NULL;
12811 }
12812
12813 if (!vop_start)
12814 {
12815 as_bad (_("unbalanced figure braces"));
12816 return 0;
12817 }
12818
12819 if (check_VecOperations (vop_start) == NULL)
12820 return 0;
12821 }
12822
12823 /* If we only have a displacement, set-up for it to be parsed later. */
12824 displacement_string_start = op_string;
12825 displacement_string_end = base_string + 1;
12826
12827 if (*base_string == ')')
12828 {
12829 char *temp_string;
12830 unsigned int parens_not_balanced = 0;
12831 bool in_quotes = false;
12832
12833 /* We've already checked that the number of left & right ()'s are
12834 equal, and that there's a matching set of double quotes. */
12835 end_op = base_string;
12836 for (temp_string = op_string; temp_string < end_op; temp_string++)
12837 {
12838 if (*temp_string == '\\' && temp_string[1] == '"')
12839 ++temp_string;
12840 else if (*temp_string == '"')
12841 in_quotes = !in_quotes;
12842 else if (!in_quotes)
12843 {
12844 if (*temp_string == '(' && !parens_not_balanced++)
12845 base_string = temp_string;
12846 if (*temp_string == ')')
12847 --parens_not_balanced;
12848 }
12849 }
12850
12851 temp_string = base_string;
12852
12853 /* Skip past '(' and whitespace. */
12854 gas_assert (*base_string == '(');
12855 ++base_string;
12856 if (is_space_char (*base_string))
12857 ++base_string;
12858
12859 if (*base_string == ','
12860 || ((i.base_reg = parse_register (base_string, &end_op))
12861 != NULL))
12862 {
12863 displacement_string_end = temp_string;
12864
12865 i.types[this_operand].bitfield.baseindex = 1;
12866
12867 if (i.base_reg)
12868 {
12869 if (i.base_reg == &bad_reg)
12870 return 0;
12871 base_string = end_op;
12872 if (is_space_char (*base_string))
12873 ++base_string;
12874 }
12875
12876 /* There may be an index reg or scale factor here. */
12877 if (*base_string == ',')
12878 {
12879 ++base_string;
12880 if (is_space_char (*base_string))
12881 ++base_string;
12882
12883 if ((i.index_reg = parse_register (base_string, &end_op))
12884 != NULL)
12885 {
12886 if (i.index_reg == &bad_reg)
12887 return 0;
12888 base_string = end_op;
12889 if (is_space_char (*base_string))
12890 ++base_string;
12891 if (*base_string == ',')
12892 {
12893 ++base_string;
12894 if (is_space_char (*base_string))
12895 ++base_string;
12896 }
12897 else if (*base_string != ')')
12898 {
12899 as_bad (_("expecting `,' or `)' "
12900 "after index register in `%s'"),
12901 operand_string);
12902 return 0;
12903 }
12904 }
12905 else if (*base_string == REGISTER_PREFIX)
12906 {
12907 end_op = strchr (base_string, ',');
12908 if (end_op)
12909 *end_op = '\0';
12910 as_bad (_("bad register name `%s'"), base_string);
12911 return 0;
12912 }
12913
12914 /* Check for scale factor. */
12915 if (*base_string != ')')
12916 {
12917 char *end_scale = i386_scale (base_string);
12918
12919 if (!end_scale)
12920 return 0;
12921
12922 base_string = end_scale;
12923 if (is_space_char (*base_string))
12924 ++base_string;
12925 if (*base_string != ')')
12926 {
12927 as_bad (_("expecting `)' "
12928 "after scale factor in `%s'"),
12929 operand_string);
12930 return 0;
12931 }
12932 }
12933 else if (!i.index_reg)
12934 {
12935 as_bad (_("expecting index register or scale factor "
12936 "after `,'; got '%c'"),
12937 *base_string);
12938 return 0;
12939 }
12940 }
12941 else if (*base_string != ')')
12942 {
12943 as_bad (_("expecting `,' or `)' "
12944 "after base register in `%s'"),
12945 operand_string);
12946 return 0;
12947 }
12948 }
12949 else if (*base_string == REGISTER_PREFIX)
12950 {
12951 end_op = strchr (base_string, ',');
12952 if (end_op)
12953 *end_op = '\0';
12954 as_bad (_("bad register name `%s'"), base_string);
12955 return 0;
12956 }
12957 }
12958
12959 /* If there's an expression beginning the operand, parse it,
12960 assuming displacement_string_start and
12961 displacement_string_end are meaningful. */
12962 if (displacement_string_start != displacement_string_end)
12963 {
12964 if (!i386_displacement (displacement_string_start,
12965 displacement_string_end))
12966 return 0;
12967 }
12968
12969 /* Special case for (%dx) while doing input/output op. */
12970 if (i.base_reg
12971 && i.base_reg->reg_type.bitfield.instance == RegD
12972 && i.base_reg->reg_type.bitfield.word
12973 && i.index_reg == 0
12974 && i.log2_scale_factor == 0
12975 && i.seg[i.mem_operands] == 0
12976 && !operand_type_check (i.types[this_operand], disp))
12977 {
12978 i.types[this_operand] = i.base_reg->reg_type;
12979 i.input_output_operand = true;
12980 return 1;
12981 }
12982
12983 if (i386_index_check (operand_string) == 0)
12984 return 0;
12985 i.flags[this_operand] |= Operand_Mem;
12986 i.mem_operands++;
12987 }
12988 else
12989 {
12990 /* It's not a memory operand; argh! */
12991 as_bad (_("invalid char %s beginning operand %d `%s'"),
12992 output_invalid (*op_string),
12993 this_operand + 1,
12994 op_string);
12995 return 0;
12996 }
12997 return 1; /* Normal return. */
12998 }
12999 \f
13000 /* Calculate the maximum variable size (i.e., excluding fr_fix)
13001 that an rs_machine_dependent frag may reach. */
13002
13003 unsigned int
13004 i386_frag_max_var (fragS *frag)
13005 {
13006 /* The only relaxable frags are for jumps.
13007 Unconditional jumps can grow by 4 bytes and others by 5 bytes. */
13008 gas_assert (frag->fr_type == rs_machine_dependent);
13009 return TYPE_FROM_RELAX_STATE (frag->fr_subtype) == UNCOND_JUMP ? 4 : 5;
13010 }
13011
13012 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13013 static int
13014 elf_symbol_resolved_in_segment_p (symbolS *fr_symbol, offsetT fr_var)
13015 {
13016 /* STT_GNU_IFUNC symbol must go through PLT. */
13017 if ((symbol_get_bfdsym (fr_symbol)->flags
13018 & BSF_GNU_INDIRECT_FUNCTION) != 0)
13019 return 0;
13020
13021 if (!S_IS_EXTERNAL (fr_symbol))
13022 /* Symbol may be weak or local. */
13023 return !S_IS_WEAK (fr_symbol);
13024
13025 /* Global symbols with non-default visibility can't be preempted. */
13026 if (ELF_ST_VISIBILITY (S_GET_OTHER (fr_symbol)) != STV_DEFAULT)
13027 return 1;
13028
13029 if (fr_var != NO_RELOC)
13030 switch ((enum bfd_reloc_code_real) fr_var)
13031 {
13032 case BFD_RELOC_386_PLT32:
13033 case BFD_RELOC_X86_64_PLT32:
13034 /* Symbol with PLT relocation may be preempted. */
13035 return 0;
13036 default:
13037 abort ();
13038 }
13039
13040 /* Global symbols with default visibility in a shared library may be
13041 preempted by another definition. */
13042 return !shared;
13043 }
13044 #endif
13045
13046 /* Table 3-2. Macro-Fusible Instructions in Haswell Microarchitecture
13047 Note also work for Skylake and Cascadelake.
13048 ---------------------------------------------------------------------
13049 | JCC | ADD/SUB/CMP | INC/DEC | TEST/AND |
13050 | ------ | ----------- | ------- | -------- |
13051 | Jo | N | N | Y |
13052 | Jno | N | N | Y |
13053 | Jc/Jb | Y | N | Y |
13054 | Jae/Jnb | Y | N | Y |
13055 | Je/Jz | Y | Y | Y |
13056 | Jne/Jnz | Y | Y | Y |
13057 | Jna/Jbe | Y | N | Y |
13058 | Ja/Jnbe | Y | N | Y |
13059 | Js | N | N | Y |
13060 | Jns | N | N | Y |
13061 | Jp/Jpe | N | N | Y |
13062 | Jnp/Jpo | N | N | Y |
13063 | Jl/Jnge | Y | Y | Y |
13064 | Jge/Jnl | Y | Y | Y |
13065 | Jle/Jng | Y | Y | Y |
13066 | Jg/Jnle | Y | Y | Y |
13067 --------------------------------------------------------------------- */
13068 static int
13069 i386_macro_fusible_p (enum mf_cmp_kind mf_cmp, enum mf_jcc_kind mf_jcc)
13070 {
13071 if (mf_cmp == mf_cmp_alu_cmp)
13072 return ((mf_jcc >= mf_jcc_jc && mf_jcc <= mf_jcc_jna)
13073 || mf_jcc == mf_jcc_jl || mf_jcc == mf_jcc_jle);
13074 if (mf_cmp == mf_cmp_incdec)
13075 return (mf_jcc == mf_jcc_je || mf_jcc == mf_jcc_jl
13076 || mf_jcc == mf_jcc_jle);
13077 if (mf_cmp == mf_cmp_test_and)
13078 return 1;
13079 return 0;
13080 }
13081
13082 /* Return the next non-empty frag. */
13083
13084 static fragS *
13085 i386_next_non_empty_frag (fragS *fragP)
13086 {
13087 /* There may be a frag with a ".fill 0" when there is no room in
13088 the current frag for frag_grow in output_insn. */
13089 for (fragP = fragP->fr_next;
13090 (fragP != NULL
13091 && fragP->fr_type == rs_fill
13092 && fragP->fr_fix == 0);
13093 fragP = fragP->fr_next)
13094 ;
13095 return fragP;
13096 }
13097
13098 /* Return the next jcc frag after BRANCH_PADDING. */
13099
13100 static fragS *
13101 i386_next_fusible_jcc_frag (fragS *maybe_cmp_fragP, fragS *pad_fragP)
13102 {
13103 fragS *branch_fragP;
13104 if (!pad_fragP)
13105 return NULL;
13106
13107 if (pad_fragP->fr_type == rs_machine_dependent
13108 && (TYPE_FROM_RELAX_STATE (pad_fragP->fr_subtype)
13109 == BRANCH_PADDING))
13110 {
13111 branch_fragP = i386_next_non_empty_frag (pad_fragP);
13112 if (branch_fragP->fr_type != rs_machine_dependent)
13113 return NULL;
13114 if (TYPE_FROM_RELAX_STATE (branch_fragP->fr_subtype) == COND_JUMP
13115 && i386_macro_fusible_p (maybe_cmp_fragP->tc_frag_data.mf_type,
13116 pad_fragP->tc_frag_data.mf_type))
13117 return branch_fragP;
13118 }
13119
13120 return NULL;
13121 }
13122
13123 /* Classify BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags. */
13124
13125 static void
13126 i386_classify_machine_dependent_frag (fragS *fragP)
13127 {
13128 fragS *cmp_fragP;
13129 fragS *pad_fragP;
13130 fragS *branch_fragP;
13131 fragS *next_fragP;
13132 unsigned int max_prefix_length;
13133
13134 if (fragP->tc_frag_data.classified)
13135 return;
13136
13137 /* First scan for BRANCH_PADDING and FUSED_JCC_PADDING. Convert
13138 FUSED_JCC_PADDING and merge BRANCH_PADDING. */
13139 for (next_fragP = fragP;
13140 next_fragP != NULL;
13141 next_fragP = next_fragP->fr_next)
13142 {
13143 next_fragP->tc_frag_data.classified = 1;
13144 if (next_fragP->fr_type == rs_machine_dependent)
13145 switch (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype))
13146 {
13147 case BRANCH_PADDING:
13148 /* The BRANCH_PADDING frag must be followed by a branch
13149 frag. */
13150 branch_fragP = i386_next_non_empty_frag (next_fragP);
13151 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
13152 break;
13153 case FUSED_JCC_PADDING:
13154 /* Check if this is a fused jcc:
13155 FUSED_JCC_PADDING
13156 CMP like instruction
13157 BRANCH_PADDING
13158 COND_JUMP
13159 */
13160 cmp_fragP = i386_next_non_empty_frag (next_fragP);
13161 pad_fragP = i386_next_non_empty_frag (cmp_fragP);
13162 branch_fragP = i386_next_fusible_jcc_frag (next_fragP, pad_fragP);
13163 if (branch_fragP)
13164 {
13165 /* The BRANCH_PADDING frag is merged with the
13166 FUSED_JCC_PADDING frag. */
13167 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
13168 /* CMP like instruction size. */
13169 next_fragP->tc_frag_data.cmp_size = cmp_fragP->fr_fix;
13170 frag_wane (pad_fragP);
13171 /* Skip to branch_fragP. */
13172 next_fragP = branch_fragP;
13173 }
13174 else if (next_fragP->tc_frag_data.max_prefix_length)
13175 {
13176 /* Turn FUSED_JCC_PADDING into BRANCH_PREFIX if it isn't
13177 a fused jcc. */
13178 next_fragP->fr_subtype
13179 = ENCODE_RELAX_STATE (BRANCH_PREFIX, 0);
13180 next_fragP->tc_frag_data.max_bytes
13181 = next_fragP->tc_frag_data.max_prefix_length;
13182 /* This will be updated in the BRANCH_PREFIX scan. */
13183 next_fragP->tc_frag_data.max_prefix_length = 0;
13184 }
13185 else
13186 frag_wane (next_fragP);
13187 break;
13188 }
13189 }
13190
13191 /* Stop if there is no BRANCH_PREFIX. */
13192 if (!align_branch_prefix_size)
13193 return;
13194
13195 /* Scan for BRANCH_PREFIX. */
13196 for (; fragP != NULL; fragP = fragP->fr_next)
13197 {
13198 if (fragP->fr_type != rs_machine_dependent
13199 || (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
13200 != BRANCH_PREFIX))
13201 continue;
13202
13203 /* Count all BRANCH_PREFIX frags before BRANCH_PADDING and
13204 COND_JUMP_PREFIX. */
13205 max_prefix_length = 0;
13206 for (next_fragP = fragP;
13207 next_fragP != NULL;
13208 next_fragP = next_fragP->fr_next)
13209 {
13210 if (next_fragP->fr_type == rs_fill)
13211 /* Skip rs_fill frags. */
13212 continue;
13213 else if (next_fragP->fr_type != rs_machine_dependent)
13214 /* Stop for all other frags. */
13215 break;
13216
13217 /* rs_machine_dependent frags. */
13218 if (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
13219 == BRANCH_PREFIX)
13220 {
13221 /* Count BRANCH_PREFIX frags. */
13222 if (max_prefix_length >= MAX_FUSED_JCC_PADDING_SIZE)
13223 {
13224 max_prefix_length = MAX_FUSED_JCC_PADDING_SIZE;
13225 frag_wane (next_fragP);
13226 }
13227 else
13228 max_prefix_length
13229 += next_fragP->tc_frag_data.max_bytes;
13230 }
13231 else if ((TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
13232 == BRANCH_PADDING)
13233 || (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
13234 == FUSED_JCC_PADDING))
13235 {
13236 /* Stop at BRANCH_PADDING and FUSED_JCC_PADDING. */
13237 fragP->tc_frag_data.u.padding_fragP = next_fragP;
13238 break;
13239 }
13240 else
13241 /* Stop for other rs_machine_dependent frags. */
13242 break;
13243 }
13244
13245 fragP->tc_frag_data.max_prefix_length = max_prefix_length;
13246
13247 /* Skip to the next frag. */
13248 fragP = next_fragP;
13249 }
13250 }
13251
13252 /* Compute padding size for
13253
13254 FUSED_JCC_PADDING
13255 CMP like instruction
13256 BRANCH_PADDING
13257 COND_JUMP/UNCOND_JUMP
13258
13259 or
13260
13261 BRANCH_PADDING
13262 COND_JUMP/UNCOND_JUMP
13263 */
13264
13265 static int
13266 i386_branch_padding_size (fragS *fragP, offsetT address)
13267 {
13268 unsigned int offset, size, padding_size;
13269 fragS *branch_fragP = fragP->tc_frag_data.u.branch_fragP;
13270
13271 /* The start address of the BRANCH_PADDING or FUSED_JCC_PADDING frag. */
13272 if (!address)
13273 address = fragP->fr_address;
13274 address += fragP->fr_fix;
13275
13276 /* CMP like instrunction size. */
13277 size = fragP->tc_frag_data.cmp_size;
13278
13279 /* The base size of the branch frag. */
13280 size += branch_fragP->fr_fix;
13281
13282 /* Add opcode and displacement bytes for the rs_machine_dependent
13283 branch frag. */
13284 if (branch_fragP->fr_type == rs_machine_dependent)
13285 size += md_relax_table[branch_fragP->fr_subtype].rlx_length;
13286
13287 /* Check if branch is within boundary and doesn't end at the last
13288 byte. */
13289 offset = address & ((1U << align_branch_power) - 1);
13290 if ((offset + size) >= (1U << align_branch_power))
13291 /* Padding needed to avoid crossing boundary. */
13292 padding_size = (1U << align_branch_power) - offset;
13293 else
13294 /* No padding needed. */
13295 padding_size = 0;
13296
13297 /* The return value may be saved in tc_frag_data.length which is
13298 unsigned byte. */
13299 if (!fits_in_unsigned_byte (padding_size))
13300 abort ();
13301
13302 return padding_size;
13303 }
13304
13305 /* i386_generic_table_relax_frag()
13306
13307 Handle BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags to
13308 grow/shrink padding to align branch frags. Hand others to
13309 relax_frag(). */
13310
13311 long
13312 i386_generic_table_relax_frag (segT segment, fragS *fragP, long stretch)
13313 {
13314 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
13315 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
13316 {
13317 long padding_size = i386_branch_padding_size (fragP, 0);
13318 long grow = padding_size - fragP->tc_frag_data.length;
13319
13320 /* When the BRANCH_PREFIX frag is used, the computed address
13321 must match the actual address and there should be no padding. */
13322 if (fragP->tc_frag_data.padding_address
13323 && (fragP->tc_frag_data.padding_address != fragP->fr_address
13324 || padding_size))
13325 abort ();
13326
13327 /* Update the padding size. */
13328 if (grow)
13329 fragP->tc_frag_data.length = padding_size;
13330
13331 return grow;
13332 }
13333 else if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
13334 {
13335 fragS *padding_fragP, *next_fragP;
13336 long padding_size, left_size, last_size;
13337
13338 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
13339 if (!padding_fragP)
13340 /* Use the padding set by the leading BRANCH_PREFIX frag. */
13341 return (fragP->tc_frag_data.length
13342 - fragP->tc_frag_data.last_length);
13343
13344 /* Compute the relative address of the padding frag in the very
13345 first time where the BRANCH_PREFIX frag sizes are zero. */
13346 if (!fragP->tc_frag_data.padding_address)
13347 fragP->tc_frag_data.padding_address
13348 = padding_fragP->fr_address - (fragP->fr_address - stretch);
13349
13350 /* First update the last length from the previous interation. */
13351 left_size = fragP->tc_frag_data.prefix_length;
13352 for (next_fragP = fragP;
13353 next_fragP != padding_fragP;
13354 next_fragP = next_fragP->fr_next)
13355 if (next_fragP->fr_type == rs_machine_dependent
13356 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
13357 == BRANCH_PREFIX))
13358 {
13359 if (left_size)
13360 {
13361 int max = next_fragP->tc_frag_data.max_bytes;
13362 if (max)
13363 {
13364 int size;
13365 if (max > left_size)
13366 size = left_size;
13367 else
13368 size = max;
13369 left_size -= size;
13370 next_fragP->tc_frag_data.last_length = size;
13371 }
13372 }
13373 else
13374 next_fragP->tc_frag_data.last_length = 0;
13375 }
13376
13377 /* Check the padding size for the padding frag. */
13378 padding_size = i386_branch_padding_size
13379 (padding_fragP, (fragP->fr_address
13380 + fragP->tc_frag_data.padding_address));
13381
13382 last_size = fragP->tc_frag_data.prefix_length;
13383 /* Check if there is change from the last interation. */
13384 if (padding_size == last_size)
13385 {
13386 /* Update the expected address of the padding frag. */
13387 padding_fragP->tc_frag_data.padding_address
13388 = (fragP->fr_address + padding_size
13389 + fragP->tc_frag_data.padding_address);
13390 return 0;
13391 }
13392
13393 if (padding_size > fragP->tc_frag_data.max_prefix_length)
13394 {
13395 /* No padding if there is no sufficient room. Clear the
13396 expected address of the padding frag. */
13397 padding_fragP->tc_frag_data.padding_address = 0;
13398 padding_size = 0;
13399 }
13400 else
13401 /* Store the expected address of the padding frag. */
13402 padding_fragP->tc_frag_data.padding_address
13403 = (fragP->fr_address + padding_size
13404 + fragP->tc_frag_data.padding_address);
13405
13406 fragP->tc_frag_data.prefix_length = padding_size;
13407
13408 /* Update the length for the current interation. */
13409 left_size = padding_size;
13410 for (next_fragP = fragP;
13411 next_fragP != padding_fragP;
13412 next_fragP = next_fragP->fr_next)
13413 if (next_fragP->fr_type == rs_machine_dependent
13414 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
13415 == BRANCH_PREFIX))
13416 {
13417 if (left_size)
13418 {
13419 int max = next_fragP->tc_frag_data.max_bytes;
13420 if (max)
13421 {
13422 int size;
13423 if (max > left_size)
13424 size = left_size;
13425 else
13426 size = max;
13427 left_size -= size;
13428 next_fragP->tc_frag_data.length = size;
13429 }
13430 }
13431 else
13432 next_fragP->tc_frag_data.length = 0;
13433 }
13434
13435 return (fragP->tc_frag_data.length
13436 - fragP->tc_frag_data.last_length);
13437 }
13438 return relax_frag (segment, fragP, stretch);
13439 }
13440
13441 /* md_estimate_size_before_relax()
13442
13443 Called just before relax() for rs_machine_dependent frags. The x86
13444 assembler uses these frags to handle variable size jump
13445 instructions.
13446
13447 Any symbol that is now undefined will not become defined.
13448 Return the correct fr_subtype in the frag.
13449 Return the initial "guess for variable size of frag" to caller.
13450 The guess is actually the growth beyond the fixed part. Whatever
13451 we do to grow the fixed or variable part contributes to our
13452 returned value. */
13453
13454 int
13455 md_estimate_size_before_relax (fragS *fragP, segT segment)
13456 {
13457 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
13458 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX
13459 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
13460 {
13461 i386_classify_machine_dependent_frag (fragP);
13462 return fragP->tc_frag_data.length;
13463 }
13464
13465 /* We've already got fragP->fr_subtype right; all we have to do is
13466 check for un-relaxable symbols. On an ELF system, we can't relax
13467 an externally visible symbol, because it may be overridden by a
13468 shared library. */
13469 if (S_GET_SEGMENT (fragP->fr_symbol) != segment
13470 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13471 || (IS_ELF
13472 && !elf_symbol_resolved_in_segment_p (fragP->fr_symbol,
13473 fragP->fr_var))
13474 #endif
13475 #if defined (OBJ_COFF) && defined (TE_PE)
13476 || (OUTPUT_FLAVOR == bfd_target_coff_flavour
13477 && S_IS_WEAK (fragP->fr_symbol))
13478 #endif
13479 )
13480 {
13481 /* Symbol is undefined in this segment, or we need to keep a
13482 reloc so that weak symbols can be overridden. */
13483 int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
13484 enum bfd_reloc_code_real reloc_type;
13485 unsigned char *opcode;
13486 int old_fr_fix;
13487 fixS *fixP = NULL;
13488
13489 if (fragP->fr_var != NO_RELOC)
13490 reloc_type = (enum bfd_reloc_code_real) fragP->fr_var;
13491 else if (size == 2)
13492 reloc_type = BFD_RELOC_16_PCREL;
13493 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13494 else if (fragP->tc_frag_data.code64 && fragP->fr_offset == 0
13495 && need_plt32_p (fragP->fr_symbol))
13496 reloc_type = BFD_RELOC_X86_64_PLT32;
13497 #endif
13498 else
13499 reloc_type = BFD_RELOC_32_PCREL;
13500
13501 old_fr_fix = fragP->fr_fix;
13502 opcode = (unsigned char *) fragP->fr_opcode;
13503
13504 switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
13505 {
13506 case UNCOND_JUMP:
13507 /* Make jmp (0xeb) a (d)word displacement jump. */
13508 opcode[0] = 0xe9;
13509 fragP->fr_fix += size;
13510 fixP = fix_new (fragP, old_fr_fix, size,
13511 fragP->fr_symbol,
13512 fragP->fr_offset, 1,
13513 reloc_type);
13514 break;
13515
13516 case COND_JUMP86:
13517 if (size == 2
13518 && (!no_cond_jump_promotion || fragP->fr_var != NO_RELOC))
13519 {
13520 /* Negate the condition, and branch past an
13521 unconditional jump. */
13522 opcode[0] ^= 1;
13523 opcode[1] = 3;
13524 /* Insert an unconditional jump. */
13525 opcode[2] = 0xe9;
13526 /* We added two extra opcode bytes, and have a two byte
13527 offset. */
13528 fragP->fr_fix += 2 + 2;
13529 fix_new (fragP, old_fr_fix + 2, 2,
13530 fragP->fr_symbol,
13531 fragP->fr_offset, 1,
13532 reloc_type);
13533 break;
13534 }
13535 /* Fall through. */
13536
13537 case COND_JUMP:
13538 if (no_cond_jump_promotion && fragP->fr_var == NO_RELOC)
13539 {
13540 fragP->fr_fix += 1;
13541 fixP = fix_new (fragP, old_fr_fix, 1,
13542 fragP->fr_symbol,
13543 fragP->fr_offset, 1,
13544 BFD_RELOC_8_PCREL);
13545 fixP->fx_signed = 1;
13546 break;
13547 }
13548
13549 /* This changes the byte-displacement jump 0x7N
13550 to the (d)word-displacement jump 0x0f,0x8N. */
13551 opcode[1] = opcode[0] + 0x10;
13552 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
13553 /* We've added an opcode byte. */
13554 fragP->fr_fix += 1 + size;
13555 fixP = fix_new (fragP, old_fr_fix + 1, size,
13556 fragP->fr_symbol,
13557 fragP->fr_offset, 1,
13558 reloc_type);
13559 break;
13560
13561 default:
13562 BAD_CASE (fragP->fr_subtype);
13563 break;
13564 }
13565
13566 /* All jumps handled here are signed, but don't unconditionally use a
13567 signed limit check for 32 and 16 bit jumps as we want to allow wrap
13568 around at 4G (outside of 64-bit mode) and 64k. */
13569 if (size == 4 && flag_code == CODE_64BIT)
13570 fixP->fx_signed = 1;
13571
13572 frag_wane (fragP);
13573 return fragP->fr_fix - old_fr_fix;
13574 }
13575
13576 /* Guess size depending on current relax state. Initially the relax
13577 state will correspond to a short jump and we return 1, because
13578 the variable part of the frag (the branch offset) is one byte
13579 long. However, we can relax a section more than once and in that
13580 case we must either set fr_subtype back to the unrelaxed state,
13581 or return the value for the appropriate branch. */
13582 return md_relax_table[fragP->fr_subtype].rlx_length;
13583 }
13584
13585 /* Called after relax() is finished.
13586
13587 In: Address of frag.
13588 fr_type == rs_machine_dependent.
13589 fr_subtype is what the address relaxed to.
13590
13591 Out: Any fixSs and constants are set up.
13592 Caller will turn frag into a ".space 0". */
13593
13594 void
13595 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec ATTRIBUTE_UNUSED,
13596 fragS *fragP)
13597 {
13598 unsigned char *opcode;
13599 unsigned char *where_to_put_displacement = NULL;
13600 offsetT target_address;
13601 offsetT opcode_address;
13602 unsigned int extension = 0;
13603 offsetT displacement_from_opcode_start;
13604
13605 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
13606 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING
13607 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
13608 {
13609 /* Generate nop padding. */
13610 unsigned int size = fragP->tc_frag_data.length;
13611 if (size)
13612 {
13613 if (size > fragP->tc_frag_data.max_bytes)
13614 abort ();
13615
13616 if (flag_debug)
13617 {
13618 const char *msg;
13619 const char *branch = "branch";
13620 const char *prefix = "";
13621 fragS *padding_fragP;
13622 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
13623 == BRANCH_PREFIX)
13624 {
13625 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
13626 switch (fragP->tc_frag_data.default_prefix)
13627 {
13628 default:
13629 abort ();
13630 break;
13631 case CS_PREFIX_OPCODE:
13632 prefix = " cs";
13633 break;
13634 case DS_PREFIX_OPCODE:
13635 prefix = " ds";
13636 break;
13637 case ES_PREFIX_OPCODE:
13638 prefix = " es";
13639 break;
13640 case FS_PREFIX_OPCODE:
13641 prefix = " fs";
13642 break;
13643 case GS_PREFIX_OPCODE:
13644 prefix = " gs";
13645 break;
13646 case SS_PREFIX_OPCODE:
13647 prefix = " ss";
13648 break;
13649 }
13650 if (padding_fragP)
13651 msg = _("%s:%u: add %d%s at 0x%llx to align "
13652 "%s within %d-byte boundary\n");
13653 else
13654 msg = _("%s:%u: add additional %d%s at 0x%llx to "
13655 "align %s within %d-byte boundary\n");
13656 }
13657 else
13658 {
13659 padding_fragP = fragP;
13660 msg = _("%s:%u: add %d%s-byte nop at 0x%llx to align "
13661 "%s within %d-byte boundary\n");
13662 }
13663
13664 if (padding_fragP)
13665 switch (padding_fragP->tc_frag_data.branch_type)
13666 {
13667 case align_branch_jcc:
13668 branch = "jcc";
13669 break;
13670 case align_branch_fused:
13671 branch = "fused jcc";
13672 break;
13673 case align_branch_jmp:
13674 branch = "jmp";
13675 break;
13676 case align_branch_call:
13677 branch = "call";
13678 break;
13679 case align_branch_indirect:
13680 branch = "indiret branch";
13681 break;
13682 case align_branch_ret:
13683 branch = "ret";
13684 break;
13685 default:
13686 break;
13687 }
13688
13689 fprintf (stdout, msg,
13690 fragP->fr_file, fragP->fr_line, size, prefix,
13691 (long long) fragP->fr_address, branch,
13692 1 << align_branch_power);
13693 }
13694 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
13695 memset (fragP->fr_opcode,
13696 fragP->tc_frag_data.default_prefix, size);
13697 else
13698 i386_generate_nops (fragP, (char *) fragP->fr_opcode,
13699 size, 0);
13700 fragP->fr_fix += size;
13701 }
13702 return;
13703 }
13704
13705 opcode = (unsigned char *) fragP->fr_opcode;
13706
13707 /* Address we want to reach in file space. */
13708 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
13709
13710 /* Address opcode resides at in file space. */
13711 opcode_address = fragP->fr_address + fragP->fr_fix;
13712
13713 /* Displacement from opcode start to fill into instruction. */
13714 displacement_from_opcode_start = target_address - opcode_address;
13715
13716 if ((fragP->fr_subtype & BIG) == 0)
13717 {
13718 /* Don't have to change opcode. */
13719 extension = 1; /* 1 opcode + 1 displacement */
13720 where_to_put_displacement = &opcode[1];
13721 }
13722 else
13723 {
13724 if (no_cond_jump_promotion
13725 && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
13726 as_warn_where (fragP->fr_file, fragP->fr_line,
13727 _("long jump required"));
13728
13729 switch (fragP->fr_subtype)
13730 {
13731 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
13732 extension = 4; /* 1 opcode + 4 displacement */
13733 opcode[0] = 0xe9;
13734 where_to_put_displacement = &opcode[1];
13735 break;
13736
13737 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
13738 extension = 2; /* 1 opcode + 2 displacement */
13739 opcode[0] = 0xe9;
13740 where_to_put_displacement = &opcode[1];
13741 break;
13742
13743 case ENCODE_RELAX_STATE (COND_JUMP, BIG):
13744 case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
13745 extension = 5; /* 2 opcode + 4 displacement */
13746 opcode[1] = opcode[0] + 0x10;
13747 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
13748 where_to_put_displacement = &opcode[2];
13749 break;
13750
13751 case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
13752 extension = 3; /* 2 opcode + 2 displacement */
13753 opcode[1] = opcode[0] + 0x10;
13754 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
13755 where_to_put_displacement = &opcode[2];
13756 break;
13757
13758 case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
13759 extension = 4;
13760 opcode[0] ^= 1;
13761 opcode[1] = 3;
13762 opcode[2] = 0xe9;
13763 where_to_put_displacement = &opcode[3];
13764 break;
13765
13766 default:
13767 BAD_CASE (fragP->fr_subtype);
13768 break;
13769 }
13770 }
13771
13772 /* If size if less then four we are sure that the operand fits,
13773 but if it's 4, then it could be that the displacement is larger
13774 then -/+ 2GB. */
13775 if (DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype) == 4
13776 && object_64bit
13777 && ((addressT) (displacement_from_opcode_start - extension
13778 + ((addressT) 1 << 31))
13779 > (((addressT) 2 << 31) - 1)))
13780 {
13781 as_bad_where (fragP->fr_file, fragP->fr_line,
13782 _("jump target out of range"));
13783 /* Make us emit 0. */
13784 displacement_from_opcode_start = extension;
13785 }
13786 /* Now put displacement after opcode. */
13787 md_number_to_chars ((char *) where_to_put_displacement,
13788 (valueT) (displacement_from_opcode_start - extension),
13789 DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
13790 fragP->fr_fix += extension;
13791 }
13792 \f
13793 /* Apply a fixup (fixP) to segment data, once it has been determined
13794 by our caller that we have all the info we need to fix it up.
13795
13796 Parameter valP is the pointer to the value of the bits.
13797
13798 On the 386, immediates, displacements, and data pointers are all in
13799 the same (little-endian) format, so we don't need to care about which
13800 we are handling. */
13801
13802 void
13803 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
13804 {
13805 char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
13806 valueT value = *valP;
13807
13808 #if !defined (TE_Mach)
13809 if (fixP->fx_pcrel)
13810 {
13811 switch (fixP->fx_r_type)
13812 {
13813 default:
13814 break;
13815
13816 case BFD_RELOC_64:
13817 fixP->fx_r_type = BFD_RELOC_64_PCREL;
13818 break;
13819 case BFD_RELOC_32:
13820 case BFD_RELOC_X86_64_32S:
13821 fixP->fx_r_type = BFD_RELOC_32_PCREL;
13822 break;
13823 case BFD_RELOC_16:
13824 fixP->fx_r_type = BFD_RELOC_16_PCREL;
13825 break;
13826 case BFD_RELOC_8:
13827 fixP->fx_r_type = BFD_RELOC_8_PCREL;
13828 break;
13829 }
13830 }
13831
13832 if (fixP->fx_addsy != NULL
13833 && (fixP->fx_r_type == BFD_RELOC_32_PCREL
13834 || fixP->fx_r_type == BFD_RELOC_64_PCREL
13835 || fixP->fx_r_type == BFD_RELOC_16_PCREL
13836 || fixP->fx_r_type == BFD_RELOC_8_PCREL)
13837 && !use_rela_relocations)
13838 {
13839 /* This is a hack. There should be a better way to handle this.
13840 This covers for the fact that bfd_install_relocation will
13841 subtract the current location (for partial_inplace, PC relative
13842 relocations); see more below. */
13843 #ifndef OBJ_AOUT
13844 if (IS_ELF
13845 #ifdef TE_PE
13846 || OUTPUT_FLAVOR == bfd_target_coff_flavour
13847 #endif
13848 )
13849 value += fixP->fx_where + fixP->fx_frag->fr_address;
13850 #endif
13851 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13852 if (IS_ELF)
13853 {
13854 segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
13855
13856 if ((sym_seg == seg
13857 || (symbol_section_p (fixP->fx_addsy)
13858 && sym_seg != absolute_section))
13859 && !generic_force_reloc (fixP))
13860 {
13861 /* Yes, we add the values in twice. This is because
13862 bfd_install_relocation subtracts them out again. I think
13863 bfd_install_relocation is broken, but I don't dare change
13864 it. FIXME. */
13865 value += fixP->fx_where + fixP->fx_frag->fr_address;
13866 }
13867 }
13868 #endif
13869 #if defined (OBJ_COFF) && defined (TE_PE)
13870 /* For some reason, the PE format does not store a
13871 section address offset for a PC relative symbol. */
13872 if (S_GET_SEGMENT (fixP->fx_addsy) != seg
13873 || S_IS_WEAK (fixP->fx_addsy))
13874 value += md_pcrel_from (fixP);
13875 #endif
13876 }
13877 #if defined (OBJ_COFF) && defined (TE_PE)
13878 if (fixP->fx_addsy != NULL
13879 && S_IS_WEAK (fixP->fx_addsy)
13880 /* PR 16858: Do not modify weak function references. */
13881 && ! fixP->fx_pcrel)
13882 {
13883 #if !defined (TE_PEP)
13884 /* For x86 PE weak function symbols are neither PC-relative
13885 nor do they set S_IS_FUNCTION. So the only reliable way
13886 to detect them is to check the flags of their containing
13887 section. */
13888 if (S_GET_SEGMENT (fixP->fx_addsy) != NULL
13889 && S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_CODE)
13890 ;
13891 else
13892 #endif
13893 value -= S_GET_VALUE (fixP->fx_addsy);
13894 }
13895 #endif
13896
13897 /* Fix a few things - the dynamic linker expects certain values here,
13898 and we must not disappoint it. */
13899 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13900 if (IS_ELF && fixP->fx_addsy)
13901 switch (fixP->fx_r_type)
13902 {
13903 case BFD_RELOC_386_PLT32:
13904 case BFD_RELOC_X86_64_PLT32:
13905 /* Make the jump instruction point to the address of the operand.
13906 At runtime we merely add the offset to the actual PLT entry.
13907 NB: Subtract the offset size only for jump instructions. */
13908 if (fixP->fx_pcrel)
13909 value = -4;
13910 break;
13911
13912 case BFD_RELOC_386_TLS_GD:
13913 case BFD_RELOC_386_TLS_LDM:
13914 case BFD_RELOC_386_TLS_IE_32:
13915 case BFD_RELOC_386_TLS_IE:
13916 case BFD_RELOC_386_TLS_GOTIE:
13917 case BFD_RELOC_386_TLS_GOTDESC:
13918 case BFD_RELOC_X86_64_TLSGD:
13919 case BFD_RELOC_X86_64_TLSLD:
13920 case BFD_RELOC_X86_64_GOTTPOFF:
13921 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
13922 value = 0; /* Fully resolved at runtime. No addend. */
13923 /* Fallthrough */
13924 case BFD_RELOC_386_TLS_LE:
13925 case BFD_RELOC_386_TLS_LDO_32:
13926 case BFD_RELOC_386_TLS_LE_32:
13927 case BFD_RELOC_X86_64_DTPOFF32:
13928 case BFD_RELOC_X86_64_DTPOFF64:
13929 case BFD_RELOC_X86_64_TPOFF32:
13930 case BFD_RELOC_X86_64_TPOFF64:
13931 S_SET_THREAD_LOCAL (fixP->fx_addsy);
13932 break;
13933
13934 case BFD_RELOC_386_TLS_DESC_CALL:
13935 case BFD_RELOC_X86_64_TLSDESC_CALL:
13936 value = 0; /* Fully resolved at runtime. No addend. */
13937 S_SET_THREAD_LOCAL (fixP->fx_addsy);
13938 fixP->fx_done = 0;
13939 return;
13940
13941 case BFD_RELOC_VTABLE_INHERIT:
13942 case BFD_RELOC_VTABLE_ENTRY:
13943 fixP->fx_done = 0;
13944 return;
13945
13946 default:
13947 break;
13948 }
13949 #endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) */
13950
13951 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
13952 if (!object_64bit)
13953 value = extend_to_32bit_address (value);
13954
13955 *valP = value;
13956 #endif /* !defined (TE_Mach) */
13957
13958 /* Are we finished with this relocation now? */
13959 if (fixP->fx_addsy == NULL)
13960 {
13961 fixP->fx_done = 1;
13962 switch (fixP->fx_r_type)
13963 {
13964 case BFD_RELOC_X86_64_32S:
13965 fixP->fx_signed = 1;
13966 break;
13967
13968 default:
13969 break;
13970 }
13971 }
13972 #if defined (OBJ_COFF) && defined (TE_PE)
13973 else if (fixP->fx_addsy != NULL && S_IS_WEAK (fixP->fx_addsy))
13974 {
13975 fixP->fx_done = 0;
13976 /* Remember value for tc_gen_reloc. */
13977 fixP->fx_addnumber = value;
13978 /* Clear out the frag for now. */
13979 value = 0;
13980 }
13981 #endif
13982 else if (use_rela_relocations)
13983 {
13984 if (!disallow_64bit_reloc || fixP->fx_r_type == NO_RELOC)
13985 fixP->fx_no_overflow = 1;
13986 /* Remember value for tc_gen_reloc. */
13987 fixP->fx_addnumber = value;
13988 value = 0;
13989 }
13990
13991 md_number_to_chars (p, value, fixP->fx_size);
13992 }
13993 \f
13994 const char *
13995 md_atof (int type, char *litP, int *sizeP)
13996 {
13997 /* This outputs the LITTLENUMs in REVERSE order;
13998 in accord with the bigendian 386. */
13999 return ieee_md_atof (type, litP, sizeP, false);
14000 }
14001 \f
14002 static char output_invalid_buf[sizeof (unsigned char) * 2 + 6];
14003
14004 static char *
14005 output_invalid (int c)
14006 {
14007 if (ISPRINT (c))
14008 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
14009 "'%c'", c);
14010 else
14011 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
14012 "(0x%x)", (unsigned char) c);
14013 return output_invalid_buf;
14014 }
14015
14016 /* Verify that @r can be used in the current context. */
14017
14018 static bool check_register (const reg_entry *r)
14019 {
14020 if (allow_pseudo_reg)
14021 return true;
14022
14023 if (operand_type_all_zero (&r->reg_type))
14024 return false;
14025
14026 if ((r->reg_type.bitfield.dword
14027 || (r->reg_type.bitfield.class == SReg && r->reg_num > 3)
14028 || r->reg_type.bitfield.class == RegCR
14029 || r->reg_type.bitfield.class == RegDR)
14030 && !cpu_arch_flags.bitfield.cpui386)
14031 return false;
14032
14033 if (r->reg_type.bitfield.class == RegTR
14034 && (flag_code == CODE_64BIT
14035 || !cpu_arch_flags.bitfield.cpui386
14036 || cpu_arch_isa_flags.bitfield.cpui586
14037 || cpu_arch_isa_flags.bitfield.cpui686))
14038 return false;
14039
14040 if (r->reg_type.bitfield.class == RegMMX && !cpu_arch_flags.bitfield.cpummx)
14041 return false;
14042
14043 if (!cpu_arch_flags.bitfield.cpuavx512f)
14044 {
14045 if (r->reg_type.bitfield.zmmword
14046 || r->reg_type.bitfield.class == RegMask)
14047 return false;
14048
14049 if (!cpu_arch_flags.bitfield.cpuavx)
14050 {
14051 if (r->reg_type.bitfield.ymmword)
14052 return false;
14053
14054 if (!cpu_arch_flags.bitfield.cpusse && r->reg_type.bitfield.xmmword)
14055 return false;
14056 }
14057 }
14058
14059 if (r->reg_type.bitfield.zmmword)
14060 {
14061 if (vector_size < VSZ512)
14062 return false;
14063
14064 if (i.vec_encoding == vex_encoding_default)
14065 i.vec_encoding = vex_encoding_evex512;
14066 else if (i.vec_encoding != vex_encoding_evex
14067 && i.vec_encoding != vex_encoding_evex512)
14068 i.vec_encoding = vex_encoding_error;
14069 }
14070
14071 if (vector_size < VSZ256 && r->reg_type.bitfield.ymmword)
14072 return false;
14073
14074 if (r->reg_type.bitfield.tmmword
14075 && (!cpu_arch_flags.bitfield.cpuamx_tile
14076 || flag_code != CODE_64BIT))
14077 return false;
14078
14079 if (r->reg_type.bitfield.class == RegBND && !cpu_arch_flags.bitfield.cpumpx)
14080 return false;
14081
14082 /* Don't allow fake index register unless allow_index_reg isn't 0. */
14083 if (!allow_index_reg && r->reg_num == RegIZ)
14084 return false;
14085
14086 /* Upper 16 vector registers are only available with VREX in 64bit
14087 mode, and require EVEX encoding. */
14088 if (r->reg_flags & RegVRex)
14089 {
14090 if (!cpu_arch_flags.bitfield.cpuavx512f
14091 || flag_code != CODE_64BIT)
14092 return false;
14093
14094 if (i.vec_encoding == vex_encoding_default
14095 || i.vec_encoding == vex_encoding_evex512)
14096 i.vec_encoding = vex_encoding_evex;
14097 else if (i.vec_encoding != vex_encoding_evex)
14098 i.vec_encoding = vex_encoding_error;
14099 }
14100
14101 if (((r->reg_flags & (RegRex64 | RegRex)) || r->reg_type.bitfield.qword)
14102 && (!cpu_arch_flags.bitfield.cpu64
14103 || r->reg_type.bitfield.class != RegCR
14104 || dot_insn ())
14105 && flag_code != CODE_64BIT)
14106 return false;
14107
14108 if (r->reg_type.bitfield.class == SReg && r->reg_num == RegFlat
14109 && !intel_syntax)
14110 return false;
14111
14112 return true;
14113 }
14114
14115 /* REG_STRING starts *before* REGISTER_PREFIX. */
14116
14117 static const reg_entry *
14118 parse_real_register (const char *reg_string, char **end_op)
14119 {
14120 const char *s = reg_string;
14121 char *p;
14122 char reg_name_given[MAX_REG_NAME_SIZE + 1];
14123 const reg_entry *r;
14124
14125 /* Skip possible REGISTER_PREFIX and possible whitespace. */
14126 if (*s == REGISTER_PREFIX)
14127 ++s;
14128
14129 if (is_space_char (*s))
14130 ++s;
14131
14132 p = reg_name_given;
14133 while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
14134 {
14135 if (p >= reg_name_given + MAX_REG_NAME_SIZE)
14136 return (const reg_entry *) NULL;
14137 s++;
14138 }
14139
14140 if (is_part_of_name (*s))
14141 return (const reg_entry *) NULL;
14142
14143 *end_op = (char *) s;
14144
14145 r = (const reg_entry *) str_hash_find (reg_hash, reg_name_given);
14146
14147 /* Handle floating point regs, allowing spaces in the (i) part. */
14148 if (r == reg_st0)
14149 {
14150 if (!cpu_arch_flags.bitfield.cpu8087
14151 && !cpu_arch_flags.bitfield.cpu287
14152 && !cpu_arch_flags.bitfield.cpu387
14153 && !allow_pseudo_reg)
14154 return (const reg_entry *) NULL;
14155
14156 if (is_space_char (*s))
14157 ++s;
14158 if (*s == '(')
14159 {
14160 ++s;
14161 if (is_space_char (*s))
14162 ++s;
14163 if (*s >= '0' && *s <= '7')
14164 {
14165 int fpr = *s - '0';
14166 ++s;
14167 if (is_space_char (*s))
14168 ++s;
14169 if (*s == ')')
14170 {
14171 *end_op = (char *) s + 1;
14172 know (r[fpr].reg_num == fpr);
14173 return r + fpr;
14174 }
14175 }
14176 /* We have "%st(" then garbage. */
14177 return (const reg_entry *) NULL;
14178 }
14179 }
14180
14181 return r && check_register (r) ? r : NULL;
14182 }
14183
14184 /* REG_STRING starts *before* REGISTER_PREFIX. */
14185
14186 static const reg_entry *
14187 parse_register (const char *reg_string, char **end_op)
14188 {
14189 const reg_entry *r;
14190
14191 if (*reg_string == REGISTER_PREFIX || allow_naked_reg)
14192 r = parse_real_register (reg_string, end_op);
14193 else
14194 r = NULL;
14195 if (!r)
14196 {
14197 char *save = input_line_pointer;
14198 char *buf = xstrdup (reg_string), *name;
14199 symbolS *symbolP;
14200
14201 input_line_pointer = buf;
14202 get_symbol_name (&name);
14203 symbolP = symbol_find (name);
14204 while (symbolP && symbol_equated_p (symbolP))
14205 {
14206 const expressionS *e = symbol_get_value_expression(symbolP);
14207
14208 if (e->X_add_number)
14209 break;
14210 symbolP = e->X_add_symbol;
14211 }
14212 if (symbolP && S_GET_SEGMENT (symbolP) == reg_section)
14213 {
14214 const expressionS *e = symbol_get_value_expression (symbolP);
14215
14216 if (e->X_op == O_register)
14217 {
14218 know (e->X_add_number >= 0
14219 && (valueT) e->X_add_number < i386_regtab_size);
14220 r = i386_regtab + e->X_add_number;
14221 *end_op = (char *) reg_string + (input_line_pointer - buf);
14222 }
14223 if (r && !check_register (r))
14224 {
14225 as_bad (_("register '%s%s' cannot be used here"),
14226 register_prefix, r->reg_name);
14227 r = &bad_reg;
14228 }
14229 }
14230 input_line_pointer = save;
14231 free (buf);
14232 }
14233 return r;
14234 }
14235
14236 int
14237 i386_parse_name (char *name, expressionS *e, char *nextcharP)
14238 {
14239 const reg_entry *r = NULL;
14240 char *end = input_line_pointer;
14241
14242 /* We only know the terminating character here. It being double quote could
14243 be the closing one of a quoted symbol name, or an opening one from a
14244 following string (or another quoted symbol name). Since the latter can't
14245 be valid syntax for anything, bailing in either case is good enough. */
14246 if (*nextcharP == '"')
14247 return 0;
14248
14249 *end = *nextcharP;
14250 if (*name == REGISTER_PREFIX || allow_naked_reg)
14251 r = parse_real_register (name, &input_line_pointer);
14252 if (r && end <= input_line_pointer)
14253 {
14254 *nextcharP = *input_line_pointer;
14255 *input_line_pointer = 0;
14256 e->X_op = O_register;
14257 e->X_add_number = r - i386_regtab;
14258 return 1;
14259 }
14260 input_line_pointer = end;
14261 *end = 0;
14262 return intel_syntax ? i386_intel_parse_name (name, e) : 0;
14263 }
14264
14265 void
14266 md_operand (expressionS *e)
14267 {
14268 char *end;
14269 const reg_entry *r;
14270
14271 switch (*input_line_pointer)
14272 {
14273 case REGISTER_PREFIX:
14274 r = parse_real_register (input_line_pointer, &end);
14275 if (r)
14276 {
14277 e->X_op = O_register;
14278 e->X_add_number = r - i386_regtab;
14279 input_line_pointer = end;
14280 }
14281 break;
14282
14283 case '[':
14284 gas_assert (intel_syntax);
14285 end = input_line_pointer++;
14286 expression (e);
14287 if (*input_line_pointer == ']')
14288 {
14289 ++input_line_pointer;
14290 e->X_op_symbol = make_expr_symbol (e);
14291 e->X_add_symbol = NULL;
14292 e->X_add_number = 0;
14293 e->X_op = O_index;
14294 }
14295 else
14296 {
14297 e->X_op = O_absent;
14298 input_line_pointer = end;
14299 }
14300 break;
14301 }
14302 }
14303
14304 #ifdef BFD64
14305 /* To maintain consistency with !BFD64 builds of gas record, whether any
14306 (binary) operator was involved in an expression. As expressions are
14307 evaluated in only 32 bits when !BFD64, we use this to decide whether to
14308 truncate results. */
14309 bool i386_record_operator (operatorT op,
14310 const expressionS *left,
14311 const expressionS *right)
14312 {
14313 if (op == O_absent)
14314 return false;
14315
14316 if (!left)
14317 {
14318 /* Since the expression parser applies unary operators fine to bignum
14319 operands, we don't need to be concerned of respective operands not
14320 fitting in 32 bits. */
14321 if (right->X_op == O_constant && right->X_unsigned
14322 && !fits_in_unsigned_long (right->X_add_number))
14323 return false;
14324 }
14325 /* This isn't entirely right: The pattern can also result when constant
14326 expressions are folded (e.g. 0xffffffff + 1). */
14327 else if ((left->X_op == O_constant && left->X_unsigned
14328 && !fits_in_unsigned_long (left->X_add_number))
14329 || (right->X_op == O_constant && right->X_unsigned
14330 && !fits_in_unsigned_long (right->X_add_number)))
14331 expr_mode = expr_large_value;
14332
14333 if (expr_mode != expr_large_value)
14334 expr_mode = expr_operator_present;
14335
14336 return false;
14337 }
14338 #endif
14339 \f
14340 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14341 const char *md_shortopts = "kVQ:sqnO::";
14342 #else
14343 const char *md_shortopts = "qnO::";
14344 #endif
14345
14346 #define OPTION_32 (OPTION_MD_BASE + 0)
14347 #define OPTION_64 (OPTION_MD_BASE + 1)
14348 #define OPTION_DIVIDE (OPTION_MD_BASE + 2)
14349 #define OPTION_MARCH (OPTION_MD_BASE + 3)
14350 #define OPTION_MTUNE (OPTION_MD_BASE + 4)
14351 #define OPTION_MMNEMONIC (OPTION_MD_BASE + 5)
14352 #define OPTION_MSYNTAX (OPTION_MD_BASE + 6)
14353 #define OPTION_MINDEX_REG (OPTION_MD_BASE + 7)
14354 #define OPTION_MNAKED_REG (OPTION_MD_BASE + 8)
14355 #define OPTION_MRELAX_RELOCATIONS (OPTION_MD_BASE + 9)
14356 #define OPTION_MSSE2AVX (OPTION_MD_BASE + 10)
14357 #define OPTION_MSSE_CHECK (OPTION_MD_BASE + 11)
14358 #define OPTION_MOPERAND_CHECK (OPTION_MD_BASE + 12)
14359 #define OPTION_MAVXSCALAR (OPTION_MD_BASE + 13)
14360 #define OPTION_X32 (OPTION_MD_BASE + 14)
14361 #define OPTION_MADD_BND_PREFIX (OPTION_MD_BASE + 15)
14362 #define OPTION_MEVEXLIG (OPTION_MD_BASE + 16)
14363 #define OPTION_MEVEXWIG (OPTION_MD_BASE + 17)
14364 #define OPTION_MBIG_OBJ (OPTION_MD_BASE + 18)
14365 #define OPTION_MOMIT_LOCK_PREFIX (OPTION_MD_BASE + 19)
14366 #define OPTION_MEVEXRCIG (OPTION_MD_BASE + 20)
14367 #define OPTION_MSHARED (OPTION_MD_BASE + 21)
14368 #define OPTION_MAMD64 (OPTION_MD_BASE + 22)
14369 #define OPTION_MINTEL64 (OPTION_MD_BASE + 23)
14370 #define OPTION_MFENCE_AS_LOCK_ADD (OPTION_MD_BASE + 24)
14371 #define OPTION_X86_USED_NOTE (OPTION_MD_BASE + 25)
14372 #define OPTION_MVEXWIG (OPTION_MD_BASE + 26)
14373 #define OPTION_MALIGN_BRANCH_BOUNDARY (OPTION_MD_BASE + 27)
14374 #define OPTION_MALIGN_BRANCH_PREFIX_SIZE (OPTION_MD_BASE + 28)
14375 #define OPTION_MALIGN_BRANCH (OPTION_MD_BASE + 29)
14376 #define OPTION_MBRANCHES_WITH_32B_BOUNDARIES (OPTION_MD_BASE + 30)
14377 #define OPTION_MLFENCE_AFTER_LOAD (OPTION_MD_BASE + 31)
14378 #define OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH (OPTION_MD_BASE + 32)
14379 #define OPTION_MLFENCE_BEFORE_RET (OPTION_MD_BASE + 33)
14380 #define OPTION_MUSE_UNALIGNED_VECTOR_MOVE (OPTION_MD_BASE + 34)
14381
14382 struct option md_longopts[] =
14383 {
14384 {"32", no_argument, NULL, OPTION_32},
14385 #if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
14386 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
14387 {"64", no_argument, NULL, OPTION_64},
14388 #endif
14389 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14390 {"x32", no_argument, NULL, OPTION_X32},
14391 {"mshared", no_argument, NULL, OPTION_MSHARED},
14392 {"mx86-used-note", required_argument, NULL, OPTION_X86_USED_NOTE},
14393 #endif
14394 {"divide", no_argument, NULL, OPTION_DIVIDE},
14395 {"march", required_argument, NULL, OPTION_MARCH},
14396 {"mtune", required_argument, NULL, OPTION_MTUNE},
14397 {"mmnemonic", required_argument, NULL, OPTION_MMNEMONIC},
14398 {"msyntax", required_argument, NULL, OPTION_MSYNTAX},
14399 {"mindex-reg", no_argument, NULL, OPTION_MINDEX_REG},
14400 {"mnaked-reg", no_argument, NULL, OPTION_MNAKED_REG},
14401 {"msse2avx", no_argument, NULL, OPTION_MSSE2AVX},
14402 {"muse-unaligned-vector-move", no_argument, NULL, OPTION_MUSE_UNALIGNED_VECTOR_MOVE},
14403 {"msse-check", required_argument, NULL, OPTION_MSSE_CHECK},
14404 {"moperand-check", required_argument, NULL, OPTION_MOPERAND_CHECK},
14405 {"mavxscalar", required_argument, NULL, OPTION_MAVXSCALAR},
14406 {"mvexwig", required_argument, NULL, OPTION_MVEXWIG},
14407 {"madd-bnd-prefix", no_argument, NULL, OPTION_MADD_BND_PREFIX},
14408 {"mevexlig", required_argument, NULL, OPTION_MEVEXLIG},
14409 {"mevexwig", required_argument, NULL, OPTION_MEVEXWIG},
14410 # if defined (TE_PE) || defined (TE_PEP)
14411 {"mbig-obj", no_argument, NULL, OPTION_MBIG_OBJ},
14412 #endif
14413 {"momit-lock-prefix", required_argument, NULL, OPTION_MOMIT_LOCK_PREFIX},
14414 {"mfence-as-lock-add", required_argument, NULL, OPTION_MFENCE_AS_LOCK_ADD},
14415 {"mrelax-relocations", required_argument, NULL, OPTION_MRELAX_RELOCATIONS},
14416 {"mevexrcig", required_argument, NULL, OPTION_MEVEXRCIG},
14417 {"malign-branch-boundary", required_argument, NULL, OPTION_MALIGN_BRANCH_BOUNDARY},
14418 {"malign-branch-prefix-size", required_argument, NULL, OPTION_MALIGN_BRANCH_PREFIX_SIZE},
14419 {"malign-branch", required_argument, NULL, OPTION_MALIGN_BRANCH},
14420 {"mbranches-within-32B-boundaries", no_argument, NULL, OPTION_MBRANCHES_WITH_32B_BOUNDARIES},
14421 {"mlfence-after-load", required_argument, NULL, OPTION_MLFENCE_AFTER_LOAD},
14422 {"mlfence-before-indirect-branch", required_argument, NULL,
14423 OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH},
14424 {"mlfence-before-ret", required_argument, NULL, OPTION_MLFENCE_BEFORE_RET},
14425 {"mamd64", no_argument, NULL, OPTION_MAMD64},
14426 {"mintel64", no_argument, NULL, OPTION_MINTEL64},
14427 {NULL, no_argument, NULL, 0}
14428 };
14429 size_t md_longopts_size = sizeof (md_longopts);
14430
14431 int
14432 md_parse_option (int c, const char *arg)
14433 {
14434 unsigned int j;
14435 char *arch, *next, *saved, *type;
14436
14437 switch (c)
14438 {
14439 case 'n':
14440 optimize_align_code = 0;
14441 break;
14442
14443 case 'q':
14444 quiet_warnings = 1;
14445 break;
14446
14447 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14448 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
14449 should be emitted or not. FIXME: Not implemented. */
14450 case 'Q':
14451 if ((arg[0] != 'y' && arg[0] != 'n') || arg[1])
14452 return 0;
14453 break;
14454
14455 /* -V: SVR4 argument to print version ID. */
14456 case 'V':
14457 print_version_id ();
14458 break;
14459
14460 /* -k: Ignore for FreeBSD compatibility. */
14461 case 'k':
14462 break;
14463
14464 case 's':
14465 /* -s: On i386 Solaris, this tells the native assembler to use
14466 .stab instead of .stab.excl. We always use .stab anyhow. */
14467 break;
14468
14469 case OPTION_MSHARED:
14470 shared = 1;
14471 break;
14472
14473 case OPTION_X86_USED_NOTE:
14474 if (strcasecmp (arg, "yes") == 0)
14475 x86_used_note = 1;
14476 else if (strcasecmp (arg, "no") == 0)
14477 x86_used_note = 0;
14478 else
14479 as_fatal (_("invalid -mx86-used-note= option: `%s'"), arg);
14480 break;
14481
14482
14483 #endif
14484 #if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
14485 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
14486 case OPTION_64:
14487 {
14488 const char **list, **l;
14489
14490 list = bfd_target_list ();
14491 for (l = list; *l != NULL; l++)
14492 if (startswith (*l, "elf64-x86-64")
14493 || strcmp (*l, "coff-x86-64") == 0
14494 || strcmp (*l, "pe-x86-64") == 0
14495 || strcmp (*l, "pei-x86-64") == 0
14496 || strcmp (*l, "mach-o-x86-64") == 0)
14497 {
14498 default_arch = "x86_64";
14499 break;
14500 }
14501 if (*l == NULL)
14502 as_fatal (_("no compiled in support for x86_64"));
14503 free (list);
14504 }
14505 break;
14506 #endif
14507
14508 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14509 case OPTION_X32:
14510 if (IS_ELF)
14511 {
14512 const char **list, **l;
14513
14514 list = bfd_target_list ();
14515 for (l = list; *l != NULL; l++)
14516 if (startswith (*l, "elf32-x86-64"))
14517 {
14518 default_arch = "x86_64:32";
14519 break;
14520 }
14521 if (*l == NULL)
14522 as_fatal (_("no compiled in support for 32bit x86_64"));
14523 free (list);
14524 }
14525 else
14526 as_fatal (_("32bit x86_64 is only supported for ELF"));
14527 break;
14528 #endif
14529
14530 case OPTION_32:
14531 {
14532 const char **list, **l;
14533
14534 list = bfd_target_list ();
14535 for (l = list; *l != NULL; l++)
14536 if (strstr (*l, "-i386")
14537 || strstr (*l, "-go32"))
14538 {
14539 default_arch = "i386";
14540 break;
14541 }
14542 if (*l == NULL)
14543 as_fatal (_("no compiled in support for ix86"));
14544 free (list);
14545 }
14546 break;
14547
14548 case OPTION_DIVIDE:
14549 #ifdef SVR4_COMMENT_CHARS
14550 {
14551 char *n, *t;
14552 const char *s;
14553
14554 n = XNEWVEC (char, strlen (i386_comment_chars) + 1);
14555 t = n;
14556 for (s = i386_comment_chars; *s != '\0'; s++)
14557 if (*s != '/')
14558 *t++ = *s;
14559 *t = '\0';
14560 i386_comment_chars = n;
14561 }
14562 #endif
14563 break;
14564
14565 case OPTION_MARCH:
14566 saved = xstrdup (arg);
14567 arch = saved;
14568 /* Allow -march=+nosse. */
14569 if (*arch == '+')
14570 arch++;
14571 do
14572 {
14573 char *vsz;
14574
14575 if (*arch == '.')
14576 as_fatal (_("invalid -march= option: `%s'"), arg);
14577 next = strchr (arch, '+');
14578 if (next)
14579 *next++ = '\0';
14580 vsz = strchr (arch, '/');
14581 if (vsz)
14582 *vsz++ = '\0';
14583 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
14584 {
14585 if (vsz && cpu_arch[j].vsz != vsz_set)
14586 continue;
14587
14588 if (arch == saved && cpu_arch[j].type != PROCESSOR_NONE
14589 && strcmp (arch, cpu_arch[j].name) == 0)
14590 {
14591 /* Processor. */
14592 if (! cpu_arch[j].enable.bitfield.cpui386)
14593 continue;
14594
14595 cpu_arch_name = cpu_arch[j].name;
14596 free (cpu_sub_arch_name);
14597 cpu_sub_arch_name = NULL;
14598 cpu_arch_flags = cpu_arch[j].enable;
14599 cpu_arch_isa = cpu_arch[j].type;
14600 cpu_arch_isa_flags = cpu_arch[j].enable;
14601 if (!cpu_arch_tune_set)
14602 {
14603 cpu_arch_tune = cpu_arch_isa;
14604 cpu_arch_tune_flags = cpu_arch_isa_flags;
14605 }
14606 vector_size = VSZ_DEFAULT;
14607 break;
14608 }
14609 else if (cpu_arch[j].type == PROCESSOR_NONE
14610 && strcmp (arch, cpu_arch[j].name) == 0
14611 && !cpu_flags_all_zero (&cpu_arch[j].enable))
14612 {
14613 /* ISA extension. */
14614 i386_cpu_flags flags;
14615
14616 flags = cpu_flags_or (cpu_arch_flags,
14617 cpu_arch[j].enable);
14618
14619 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
14620 {
14621 extend_cpu_sub_arch_name (arch);
14622 cpu_arch_flags = flags;
14623 cpu_arch_isa_flags = flags;
14624 }
14625 else
14626 cpu_arch_isa_flags
14627 = cpu_flags_or (cpu_arch_isa_flags,
14628 cpu_arch[j].enable);
14629
14630 switch (cpu_arch[j].vsz)
14631 {
14632 default:
14633 break;
14634
14635 case vsz_set:
14636 if (vsz)
14637 {
14638 char *end;
14639 unsigned long val = strtoul (vsz, &end, 0);
14640
14641 if (*end)
14642 val = 0;
14643 switch (val)
14644 {
14645 case 512: vector_size = VSZ512; break;
14646 case 256: vector_size = VSZ256; break;
14647 case 128: vector_size = VSZ128; break;
14648 default:
14649 as_warn (_("Unrecognized vector size specifier ignored"));
14650 break;
14651 }
14652 break;
14653 }
14654 /* Fall through. */
14655 case vsz_reset:
14656 vector_size = VSZ_DEFAULT;
14657 break;
14658 }
14659
14660 break;
14661 }
14662 }
14663
14664 if (j >= ARRAY_SIZE (cpu_arch) && startswith (arch, "no"))
14665 {
14666 /* Disable an ISA extension. */
14667 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
14668 if (cpu_arch[j].type == PROCESSOR_NONE
14669 && strcmp (arch + 2, cpu_arch[j].name) == 0)
14670 {
14671 i386_cpu_flags flags;
14672
14673 flags = cpu_flags_and_not (cpu_arch_flags,
14674 cpu_arch[j].disable);
14675 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
14676 {
14677 extend_cpu_sub_arch_name (arch);
14678 cpu_arch_flags = flags;
14679 cpu_arch_isa_flags = flags;
14680 }
14681 if (cpu_arch[j].vsz == vsz_set)
14682 vector_size = VSZ_DEFAULT;
14683 break;
14684 }
14685 }
14686
14687 if (j >= ARRAY_SIZE (cpu_arch))
14688 as_fatal (_("invalid -march= option: `%s'"), arg);
14689
14690 arch = next;
14691 }
14692 while (next != NULL);
14693 free (saved);
14694 break;
14695
14696 case OPTION_MTUNE:
14697 if (*arg == '.')
14698 as_fatal (_("invalid -mtune= option: `%s'"), arg);
14699 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
14700 {
14701 if (cpu_arch[j].type != PROCESSOR_NONE
14702 && strcmp (arg, cpu_arch[j].name) == 0)
14703 {
14704 cpu_arch_tune_set = 1;
14705 cpu_arch_tune = cpu_arch [j].type;
14706 cpu_arch_tune_flags = cpu_arch[j].enable;
14707 break;
14708 }
14709 }
14710 if (j >= ARRAY_SIZE (cpu_arch))
14711 as_fatal (_("invalid -mtune= option: `%s'"), arg);
14712 break;
14713
14714 case OPTION_MMNEMONIC:
14715 if (strcasecmp (arg, "att") == 0)
14716 intel_mnemonic = 0;
14717 else if (strcasecmp (arg, "intel") == 0)
14718 intel_mnemonic = 1;
14719 else
14720 as_fatal (_("invalid -mmnemonic= option: `%s'"), arg);
14721 break;
14722
14723 case OPTION_MSYNTAX:
14724 if (strcasecmp (arg, "att") == 0)
14725 intel_syntax = 0;
14726 else if (strcasecmp (arg, "intel") == 0)
14727 intel_syntax = 1;
14728 else
14729 as_fatal (_("invalid -msyntax= option: `%s'"), arg);
14730 break;
14731
14732 case OPTION_MINDEX_REG:
14733 allow_index_reg = 1;
14734 break;
14735
14736 case OPTION_MNAKED_REG:
14737 allow_naked_reg = 1;
14738 break;
14739
14740 case OPTION_MSSE2AVX:
14741 sse2avx = 1;
14742 break;
14743
14744 case OPTION_MUSE_UNALIGNED_VECTOR_MOVE:
14745 use_unaligned_vector_move = 1;
14746 break;
14747
14748 case OPTION_MSSE_CHECK:
14749 if (strcasecmp (arg, "error") == 0)
14750 sse_check = check_error;
14751 else if (strcasecmp (arg, "warning") == 0)
14752 sse_check = check_warning;
14753 else if (strcasecmp (arg, "none") == 0)
14754 sse_check = check_none;
14755 else
14756 as_fatal (_("invalid -msse-check= option: `%s'"), arg);
14757 break;
14758
14759 case OPTION_MOPERAND_CHECK:
14760 if (strcasecmp (arg, "error") == 0)
14761 operand_check = check_error;
14762 else if (strcasecmp (arg, "warning") == 0)
14763 operand_check = check_warning;
14764 else if (strcasecmp (arg, "none") == 0)
14765 operand_check = check_none;
14766 else
14767 as_fatal (_("invalid -moperand-check= option: `%s'"), arg);
14768 break;
14769
14770 case OPTION_MAVXSCALAR:
14771 if (strcasecmp (arg, "128") == 0)
14772 avxscalar = vex128;
14773 else if (strcasecmp (arg, "256") == 0)
14774 avxscalar = vex256;
14775 else
14776 as_fatal (_("invalid -mavxscalar= option: `%s'"), arg);
14777 break;
14778
14779 case OPTION_MVEXWIG:
14780 if (strcmp (arg, "0") == 0)
14781 vexwig = vexw0;
14782 else if (strcmp (arg, "1") == 0)
14783 vexwig = vexw1;
14784 else
14785 as_fatal (_("invalid -mvexwig= option: `%s'"), arg);
14786 break;
14787
14788 case OPTION_MADD_BND_PREFIX:
14789 add_bnd_prefix = 1;
14790 break;
14791
14792 case OPTION_MEVEXLIG:
14793 if (strcmp (arg, "128") == 0)
14794 evexlig = evexl128;
14795 else if (strcmp (arg, "256") == 0)
14796 evexlig = evexl256;
14797 else if (strcmp (arg, "512") == 0)
14798 evexlig = evexl512;
14799 else
14800 as_fatal (_("invalid -mevexlig= option: `%s'"), arg);
14801 break;
14802
14803 case OPTION_MEVEXRCIG:
14804 if (strcmp (arg, "rne") == 0)
14805 evexrcig = rne;
14806 else if (strcmp (arg, "rd") == 0)
14807 evexrcig = rd;
14808 else if (strcmp (arg, "ru") == 0)
14809 evexrcig = ru;
14810 else if (strcmp (arg, "rz") == 0)
14811 evexrcig = rz;
14812 else
14813 as_fatal (_("invalid -mevexrcig= option: `%s'"), arg);
14814 break;
14815
14816 case OPTION_MEVEXWIG:
14817 if (strcmp (arg, "0") == 0)
14818 evexwig = evexw0;
14819 else if (strcmp (arg, "1") == 0)
14820 evexwig = evexw1;
14821 else
14822 as_fatal (_("invalid -mevexwig= option: `%s'"), arg);
14823 break;
14824
14825 # if defined (TE_PE) || defined (TE_PEP)
14826 case OPTION_MBIG_OBJ:
14827 use_big_obj = 1;
14828 break;
14829 #endif
14830
14831 case OPTION_MOMIT_LOCK_PREFIX:
14832 if (strcasecmp (arg, "yes") == 0)
14833 omit_lock_prefix = 1;
14834 else if (strcasecmp (arg, "no") == 0)
14835 omit_lock_prefix = 0;
14836 else
14837 as_fatal (_("invalid -momit-lock-prefix= option: `%s'"), arg);
14838 break;
14839
14840 case OPTION_MFENCE_AS_LOCK_ADD:
14841 if (strcasecmp (arg, "yes") == 0)
14842 avoid_fence = 1;
14843 else if (strcasecmp (arg, "no") == 0)
14844 avoid_fence = 0;
14845 else
14846 as_fatal (_("invalid -mfence-as-lock-add= option: `%s'"), arg);
14847 break;
14848
14849 case OPTION_MLFENCE_AFTER_LOAD:
14850 if (strcasecmp (arg, "yes") == 0)
14851 lfence_after_load = 1;
14852 else if (strcasecmp (arg, "no") == 0)
14853 lfence_after_load = 0;
14854 else
14855 as_fatal (_("invalid -mlfence-after-load= option: `%s'"), arg);
14856 break;
14857
14858 case OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH:
14859 if (strcasecmp (arg, "all") == 0)
14860 {
14861 lfence_before_indirect_branch = lfence_branch_all;
14862 if (lfence_before_ret == lfence_before_ret_none)
14863 lfence_before_ret = lfence_before_ret_shl;
14864 }
14865 else if (strcasecmp (arg, "memory") == 0)
14866 lfence_before_indirect_branch = lfence_branch_memory;
14867 else if (strcasecmp (arg, "register") == 0)
14868 lfence_before_indirect_branch = lfence_branch_register;
14869 else if (strcasecmp (arg, "none") == 0)
14870 lfence_before_indirect_branch = lfence_branch_none;
14871 else
14872 as_fatal (_("invalid -mlfence-before-indirect-branch= option: `%s'"),
14873 arg);
14874 break;
14875
14876 case OPTION_MLFENCE_BEFORE_RET:
14877 if (strcasecmp (arg, "or") == 0)
14878 lfence_before_ret = lfence_before_ret_or;
14879 else if (strcasecmp (arg, "not") == 0)
14880 lfence_before_ret = lfence_before_ret_not;
14881 else if (strcasecmp (arg, "shl") == 0 || strcasecmp (arg, "yes") == 0)
14882 lfence_before_ret = lfence_before_ret_shl;
14883 else if (strcasecmp (arg, "none") == 0)
14884 lfence_before_ret = lfence_before_ret_none;
14885 else
14886 as_fatal (_("invalid -mlfence-before-ret= option: `%s'"),
14887 arg);
14888 break;
14889
14890 case OPTION_MRELAX_RELOCATIONS:
14891 if (strcasecmp (arg, "yes") == 0)
14892 generate_relax_relocations = 1;
14893 else if (strcasecmp (arg, "no") == 0)
14894 generate_relax_relocations = 0;
14895 else
14896 as_fatal (_("invalid -mrelax-relocations= option: `%s'"), arg);
14897 break;
14898
14899 case OPTION_MALIGN_BRANCH_BOUNDARY:
14900 {
14901 char *end;
14902 long int align = strtoul (arg, &end, 0);
14903 if (*end == '\0')
14904 {
14905 if (align == 0)
14906 {
14907 align_branch_power = 0;
14908 break;
14909 }
14910 else if (align >= 16)
14911 {
14912 int align_power;
14913 for (align_power = 0;
14914 (align & 1) == 0;
14915 align >>= 1, align_power++)
14916 continue;
14917 /* Limit alignment power to 31. */
14918 if (align == 1 && align_power < 32)
14919 {
14920 align_branch_power = align_power;
14921 break;
14922 }
14923 }
14924 }
14925 as_fatal (_("invalid -malign-branch-boundary= value: %s"), arg);
14926 }
14927 break;
14928
14929 case OPTION_MALIGN_BRANCH_PREFIX_SIZE:
14930 {
14931 char *end;
14932 int align = strtoul (arg, &end, 0);
14933 /* Some processors only support 5 prefixes. */
14934 if (*end == '\0' && align >= 0 && align < 6)
14935 {
14936 align_branch_prefix_size = align;
14937 break;
14938 }
14939 as_fatal (_("invalid -malign-branch-prefix-size= value: %s"),
14940 arg);
14941 }
14942 break;
14943
14944 case OPTION_MALIGN_BRANCH:
14945 align_branch = 0;
14946 saved = xstrdup (arg);
14947 type = saved;
14948 do
14949 {
14950 next = strchr (type, '+');
14951 if (next)
14952 *next++ = '\0';
14953 if (strcasecmp (type, "jcc") == 0)
14954 align_branch |= align_branch_jcc_bit;
14955 else if (strcasecmp (type, "fused") == 0)
14956 align_branch |= align_branch_fused_bit;
14957 else if (strcasecmp (type, "jmp") == 0)
14958 align_branch |= align_branch_jmp_bit;
14959 else if (strcasecmp (type, "call") == 0)
14960 align_branch |= align_branch_call_bit;
14961 else if (strcasecmp (type, "ret") == 0)
14962 align_branch |= align_branch_ret_bit;
14963 else if (strcasecmp (type, "indirect") == 0)
14964 align_branch |= align_branch_indirect_bit;
14965 else
14966 as_fatal (_("invalid -malign-branch= option: `%s'"), arg);
14967 type = next;
14968 }
14969 while (next != NULL);
14970 free (saved);
14971 break;
14972
14973 case OPTION_MBRANCHES_WITH_32B_BOUNDARIES:
14974 align_branch_power = 5;
14975 align_branch_prefix_size = 5;
14976 align_branch = (align_branch_jcc_bit
14977 | align_branch_fused_bit
14978 | align_branch_jmp_bit);
14979 break;
14980
14981 case OPTION_MAMD64:
14982 isa64 = amd64;
14983 break;
14984
14985 case OPTION_MINTEL64:
14986 isa64 = intel64;
14987 break;
14988
14989 case 'O':
14990 if (arg == NULL)
14991 {
14992 optimize = 1;
14993 /* Turn off -Os. */
14994 optimize_for_space = 0;
14995 }
14996 else if (*arg == 's')
14997 {
14998 optimize_for_space = 1;
14999 /* Turn on all encoding optimizations. */
15000 optimize = INT_MAX;
15001 }
15002 else
15003 {
15004 optimize = atoi (arg);
15005 /* Turn off -Os. */
15006 optimize_for_space = 0;
15007 }
15008 break;
15009
15010 default:
15011 return 0;
15012 }
15013 return 1;
15014 }
15015
15016 #define MESSAGE_TEMPLATE \
15017 " "
15018
15019 static char *
15020 output_message (FILE *stream, char *p, char *message, char *start,
15021 int *left_p, const char *name, int len)
15022 {
15023 int size = sizeof (MESSAGE_TEMPLATE);
15024 int left = *left_p;
15025
15026 /* Reserve 2 spaces for ", " or ",\0" */
15027 left -= len + 2;
15028
15029 /* Check if there is any room. */
15030 if (left >= 0)
15031 {
15032 if (p != start)
15033 {
15034 *p++ = ',';
15035 *p++ = ' ';
15036 }
15037 p = mempcpy (p, name, len);
15038 }
15039 else
15040 {
15041 /* Output the current message now and start a new one. */
15042 *p++ = ',';
15043 *p = '\0';
15044 fprintf (stream, "%s\n", message);
15045 p = start;
15046 left = size - (start - message) - len - 2;
15047
15048 gas_assert (left >= 0);
15049
15050 p = mempcpy (p, name, len);
15051 }
15052
15053 *left_p = left;
15054 return p;
15055 }
15056
15057 static void
15058 show_arch (FILE *stream, int ext, int check)
15059 {
15060 static char message[] = MESSAGE_TEMPLATE;
15061 char *start = message + 27;
15062 char *p;
15063 int size = sizeof (MESSAGE_TEMPLATE);
15064 int left;
15065 const char *name;
15066 int len;
15067 unsigned int j;
15068
15069 p = start;
15070 left = size - (start - message);
15071
15072 if (!ext && check)
15073 {
15074 p = output_message (stream, p, message, start, &left,
15075 STRING_COMMA_LEN ("default"));
15076 p = output_message (stream, p, message, start, &left,
15077 STRING_COMMA_LEN ("push"));
15078 p = output_message (stream, p, message, start, &left,
15079 STRING_COMMA_LEN ("pop"));
15080 }
15081
15082 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
15083 {
15084 /* Should it be skipped? */
15085 if (cpu_arch [j].skip)
15086 continue;
15087
15088 name = cpu_arch [j].name;
15089 len = cpu_arch [j].len;
15090 if (cpu_arch[j].type == PROCESSOR_NONE)
15091 {
15092 /* It is an extension. Skip if we aren't asked to show it. */
15093 if (!ext || cpu_flags_all_zero (&cpu_arch[j].enable))
15094 continue;
15095 }
15096 else if (ext)
15097 {
15098 /* It is an processor. Skip if we show only extension. */
15099 continue;
15100 }
15101 else if (check && ! cpu_arch[j].enable.bitfield.cpui386)
15102 {
15103 /* It is an impossible processor - skip. */
15104 continue;
15105 }
15106
15107 p = output_message (stream, p, message, start, &left, name, len);
15108 }
15109
15110 /* Display disabled extensions. */
15111 if (ext)
15112 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
15113 {
15114 char *str;
15115
15116 if (cpu_arch[j].type != PROCESSOR_NONE
15117 || !cpu_flags_all_zero (&cpu_arch[j].enable))
15118 continue;
15119 str = xasprintf ("no%s", cpu_arch[j].name);
15120 p = output_message (stream, p, message, start, &left, str,
15121 strlen (str));
15122 free (str);
15123 }
15124
15125 *p = '\0';
15126 fprintf (stream, "%s\n", message);
15127 }
15128
15129 void
15130 md_show_usage (FILE *stream)
15131 {
15132 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15133 fprintf (stream, _("\
15134 -Qy, -Qn ignored\n\
15135 -V print assembler version number\n\
15136 -k ignored\n"));
15137 #endif
15138 fprintf (stream, _("\
15139 -n do not optimize code alignment\n\
15140 -O{012s} attempt some code optimizations\n\
15141 -q quieten some warnings\n"));
15142 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15143 fprintf (stream, _("\
15144 -s ignored\n"));
15145 #endif
15146 #ifdef BFD64
15147 # if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15148 fprintf (stream, _("\
15149 --32/--64/--x32 generate 32bit/64bit/x32 object\n"));
15150 # elif defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O)
15151 fprintf (stream, _("\
15152 --32/--64 generate 32bit/64bit object\n"));
15153 # endif
15154 #endif
15155 #ifdef SVR4_COMMENT_CHARS
15156 fprintf (stream, _("\
15157 --divide do not treat `/' as a comment character\n"));
15158 #else
15159 fprintf (stream, _("\
15160 --divide ignored\n"));
15161 #endif
15162 fprintf (stream, _("\
15163 -march=CPU[,+EXTENSION...]\n\
15164 generate code for CPU and EXTENSION, CPU is one of:\n"));
15165 show_arch (stream, 0, 1);
15166 fprintf (stream, _("\
15167 EXTENSION is combination of (possibly \"no\"-prefixed):\n"));
15168 show_arch (stream, 1, 0);
15169 fprintf (stream, _("\
15170 -mtune=CPU optimize for CPU, CPU is one of:\n"));
15171 show_arch (stream, 0, 0);
15172 fprintf (stream, _("\
15173 -msse2avx encode SSE instructions with VEX prefix\n"));
15174 fprintf (stream, _("\
15175 -muse-unaligned-vector-move\n\
15176 encode aligned vector move as unaligned vector move\n"));
15177 fprintf (stream, _("\
15178 -msse-check=[none|error|warning] (default: warning)\n\
15179 check SSE instructions\n"));
15180 fprintf (stream, _("\
15181 -moperand-check=[none|error|warning] (default: warning)\n\
15182 check operand combinations for validity\n"));
15183 fprintf (stream, _("\
15184 -mavxscalar=[128|256] (default: 128)\n\
15185 encode scalar AVX instructions with specific vector\n\
15186 length\n"));
15187 fprintf (stream, _("\
15188 -mvexwig=[0|1] (default: 0)\n\
15189 encode VEX instructions with specific VEX.W value\n\
15190 for VEX.W bit ignored instructions\n"));
15191 fprintf (stream, _("\
15192 -mevexlig=[128|256|512] (default: 128)\n\
15193 encode scalar EVEX instructions with specific vector\n\
15194 length\n"));
15195 fprintf (stream, _("\
15196 -mevexwig=[0|1] (default: 0)\n\
15197 encode EVEX instructions with specific EVEX.W value\n\
15198 for EVEX.W bit ignored instructions\n"));
15199 fprintf (stream, _("\
15200 -mevexrcig=[rne|rd|ru|rz] (default: rne)\n\
15201 encode EVEX instructions with specific EVEX.RC value\n\
15202 for SAE-only ignored instructions\n"));
15203 fprintf (stream, _("\
15204 -mmnemonic=[att|intel] "));
15205 if (SYSV386_COMPAT)
15206 fprintf (stream, _("(default: att)\n"));
15207 else
15208 fprintf (stream, _("(default: intel)\n"));
15209 fprintf (stream, _("\
15210 use AT&T/Intel mnemonic\n"));
15211 fprintf (stream, _("\
15212 -msyntax=[att|intel] (default: att)\n\
15213 use AT&T/Intel syntax\n"));
15214 fprintf (stream, _("\
15215 -mindex-reg support pseudo index registers\n"));
15216 fprintf (stream, _("\
15217 -mnaked-reg don't require `%%' prefix for registers\n"));
15218 fprintf (stream, _("\
15219 -madd-bnd-prefix add BND prefix for all valid branches\n"));
15220 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15221 fprintf (stream, _("\
15222 -mshared disable branch optimization for shared code\n"));
15223 fprintf (stream, _("\
15224 -mx86-used-note=[no|yes] "));
15225 if (DEFAULT_X86_USED_NOTE)
15226 fprintf (stream, _("(default: yes)\n"));
15227 else
15228 fprintf (stream, _("(default: no)\n"));
15229 fprintf (stream, _("\
15230 generate x86 used ISA and feature properties\n"));
15231 #endif
15232 #if defined (TE_PE) || defined (TE_PEP)
15233 fprintf (stream, _("\
15234 -mbig-obj generate big object files\n"));
15235 #endif
15236 fprintf (stream, _("\
15237 -momit-lock-prefix=[no|yes] (default: no)\n\
15238 strip all lock prefixes\n"));
15239 fprintf (stream, _("\
15240 -mfence-as-lock-add=[no|yes] (default: no)\n\
15241 encode lfence, mfence and sfence as\n\
15242 lock addl $0x0, (%%{re}sp)\n"));
15243 fprintf (stream, _("\
15244 -mrelax-relocations=[no|yes] "));
15245 if (DEFAULT_GENERATE_X86_RELAX_RELOCATIONS)
15246 fprintf (stream, _("(default: yes)\n"));
15247 else
15248 fprintf (stream, _("(default: no)\n"));
15249 fprintf (stream, _("\
15250 generate relax relocations\n"));
15251 fprintf (stream, _("\
15252 -malign-branch-boundary=NUM (default: 0)\n\
15253 align branches within NUM byte boundary\n"));
15254 fprintf (stream, _("\
15255 -malign-branch=TYPE[+TYPE...] (default: jcc+fused+jmp)\n\
15256 TYPE is combination of jcc, fused, jmp, call, ret,\n\
15257 indirect\n\
15258 specify types of branches to align\n"));
15259 fprintf (stream, _("\
15260 -malign-branch-prefix-size=NUM (default: 5)\n\
15261 align branches with NUM prefixes per instruction\n"));
15262 fprintf (stream, _("\
15263 -mbranches-within-32B-boundaries\n\
15264 align branches within 32 byte boundary\n"));
15265 fprintf (stream, _("\
15266 -mlfence-after-load=[no|yes] (default: no)\n\
15267 generate lfence after load\n"));
15268 fprintf (stream, _("\
15269 -mlfence-before-indirect-branch=[none|all|register|memory] (default: none)\n\
15270 generate lfence before indirect near branch\n"));
15271 fprintf (stream, _("\
15272 -mlfence-before-ret=[none|or|not|shl|yes] (default: none)\n\
15273 generate lfence before ret\n"));
15274 fprintf (stream, _("\
15275 -mamd64 accept only AMD64 ISA [default]\n"));
15276 fprintf (stream, _("\
15277 -mintel64 accept only Intel64 ISA\n"));
15278 }
15279
15280 #if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
15281 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
15282 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
15283
15284 /* Pick the target format to use. */
15285
15286 const char *
15287 i386_target_format (void)
15288 {
15289 if (startswith (default_arch, "x86_64"))
15290 {
15291 update_code_flag (CODE_64BIT, 1);
15292 if (default_arch[6] == '\0')
15293 x86_elf_abi = X86_64_ABI;
15294 else
15295 x86_elf_abi = X86_64_X32_ABI;
15296 }
15297 else if (!strcmp (default_arch, "i386"))
15298 update_code_flag (CODE_32BIT, 1);
15299 else if (!strcmp (default_arch, "iamcu"))
15300 {
15301 update_code_flag (CODE_32BIT, 1);
15302 if (cpu_arch_isa == PROCESSOR_UNKNOWN)
15303 {
15304 static const i386_cpu_flags iamcu_flags = CPU_IAMCU_FLAGS;
15305 cpu_arch_name = "iamcu";
15306 free (cpu_sub_arch_name);
15307 cpu_sub_arch_name = NULL;
15308 cpu_arch_flags = iamcu_flags;
15309 cpu_arch_isa = PROCESSOR_IAMCU;
15310 cpu_arch_isa_flags = iamcu_flags;
15311 if (!cpu_arch_tune_set)
15312 {
15313 cpu_arch_tune = cpu_arch_isa;
15314 cpu_arch_tune_flags = cpu_arch_isa_flags;
15315 }
15316 }
15317 else if (cpu_arch_isa != PROCESSOR_IAMCU)
15318 as_fatal (_("Intel MCU doesn't support `%s' architecture"),
15319 cpu_arch_name);
15320 }
15321 else
15322 as_fatal (_("unknown architecture"));
15323
15324 if (cpu_flags_all_zero (&cpu_arch_isa_flags))
15325 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
15326 if (cpu_flags_all_zero (&cpu_arch_tune_flags))
15327 cpu_arch_tune_flags = cpu_arch[flag_code == CODE_64BIT].enable;
15328
15329 switch (OUTPUT_FLAVOR)
15330 {
15331 #if defined (OBJ_MAYBE_AOUT) || defined (OBJ_AOUT)
15332 case bfd_target_aout_flavour:
15333 return AOUT_TARGET_FORMAT;
15334 #endif
15335 #if defined (OBJ_MAYBE_COFF) || defined (OBJ_COFF)
15336 # if defined (TE_PE) || defined (TE_PEP)
15337 case bfd_target_coff_flavour:
15338 if (flag_code == CODE_64BIT)
15339 {
15340 object_64bit = 1;
15341 return use_big_obj ? "pe-bigobj-x86-64" : "pe-x86-64";
15342 }
15343 return use_big_obj ? "pe-bigobj-i386" : "pe-i386";
15344 # elif defined (TE_GO32)
15345 case bfd_target_coff_flavour:
15346 return "coff-go32";
15347 # else
15348 case bfd_target_coff_flavour:
15349 return "coff-i386";
15350 # endif
15351 #endif
15352 #if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
15353 case bfd_target_elf_flavour:
15354 {
15355 const char *format;
15356
15357 switch (x86_elf_abi)
15358 {
15359 default:
15360 format = ELF_TARGET_FORMAT;
15361 #ifndef TE_SOLARIS
15362 tls_get_addr = "___tls_get_addr";
15363 #endif
15364 break;
15365 case X86_64_ABI:
15366 use_rela_relocations = 1;
15367 object_64bit = 1;
15368 #ifndef TE_SOLARIS
15369 tls_get_addr = "__tls_get_addr";
15370 #endif
15371 format = ELF_TARGET_FORMAT64;
15372 break;
15373 case X86_64_X32_ABI:
15374 use_rela_relocations = 1;
15375 object_64bit = 1;
15376 #ifndef TE_SOLARIS
15377 tls_get_addr = "__tls_get_addr";
15378 #endif
15379 disallow_64bit_reloc = 1;
15380 format = ELF_TARGET_FORMAT32;
15381 break;
15382 }
15383 if (cpu_arch_isa == PROCESSOR_IAMCU)
15384 {
15385 if (x86_elf_abi != I386_ABI)
15386 as_fatal (_("Intel MCU is 32bit only"));
15387 return ELF_TARGET_IAMCU_FORMAT;
15388 }
15389 else
15390 return format;
15391 }
15392 #endif
15393 #if defined (OBJ_MACH_O)
15394 case bfd_target_mach_o_flavour:
15395 if (flag_code == CODE_64BIT)
15396 {
15397 use_rela_relocations = 1;
15398 object_64bit = 1;
15399 return "mach-o-x86-64";
15400 }
15401 else
15402 return "mach-o-i386";
15403 #endif
15404 default:
15405 abort ();
15406 return NULL;
15407 }
15408 }
15409
15410 #endif /* OBJ_MAYBE_ more than one */
15411 \f
15412 symbolS *
15413 md_undefined_symbol (char *name)
15414 {
15415 if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
15416 && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
15417 && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
15418 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
15419 {
15420 if (!GOT_symbol)
15421 {
15422 if (symbol_find (name))
15423 as_bad (_("GOT already in symbol table"));
15424 GOT_symbol = symbol_new (name, undefined_section,
15425 &zero_address_frag, 0);
15426 };
15427 return GOT_symbol;
15428 }
15429 return 0;
15430 }
15431
15432 /* Round up a section size to the appropriate boundary. */
15433
15434 valueT
15435 md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
15436 {
15437 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
15438 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
15439 {
15440 /* For a.out, force the section size to be aligned. If we don't do
15441 this, BFD will align it for us, but it will not write out the
15442 final bytes of the section. This may be a bug in BFD, but it is
15443 easier to fix it here since that is how the other a.out targets
15444 work. */
15445 int align;
15446
15447 align = bfd_section_alignment (segment);
15448 size = ((size + (1 << align) - 1) & (-((valueT) 1 << align)));
15449 }
15450 #endif
15451
15452 return size;
15453 }
15454
15455 /* On the i386, PC-relative offsets are relative to the start of the
15456 next instruction. That is, the address of the offset, plus its
15457 size, since the offset is always the last part of the insn. */
15458
15459 long
15460 md_pcrel_from (fixS *fixP)
15461 {
15462 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
15463 }
15464
15465 #ifndef I386COFF
15466
15467 static void
15468 s_bss (int ignore ATTRIBUTE_UNUSED)
15469 {
15470 int temp;
15471
15472 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15473 if (IS_ELF)
15474 obj_elf_section_change_hook ();
15475 #endif
15476 temp = get_absolute_expression ();
15477 subseg_set (bss_section, (subsegT) temp);
15478 demand_empty_rest_of_line ();
15479 }
15480
15481 #endif
15482
15483 /* Remember constant directive. */
15484
15485 void
15486 i386_cons_align (int ignore ATTRIBUTE_UNUSED)
15487 {
15488 if (last_insn.kind != last_insn_directive
15489 && (bfd_section_flags (now_seg) & SEC_CODE))
15490 {
15491 last_insn.seg = now_seg;
15492 last_insn.kind = last_insn_directive;
15493 last_insn.name = "constant directive";
15494 last_insn.file = as_where (&last_insn.line);
15495 if (lfence_before_ret != lfence_before_ret_none)
15496 {
15497 if (lfence_before_indirect_branch != lfence_branch_none)
15498 as_warn (_("constant directive skips -mlfence-before-ret "
15499 "and -mlfence-before-indirect-branch"));
15500 else
15501 as_warn (_("constant directive skips -mlfence-before-ret"));
15502 }
15503 else if (lfence_before_indirect_branch != lfence_branch_none)
15504 as_warn (_("constant directive skips -mlfence-before-indirect-branch"));
15505 }
15506 }
15507
15508 int
15509 i386_validate_fix (fixS *fixp)
15510 {
15511 if (fixp->fx_addsy && S_GET_SEGMENT(fixp->fx_addsy) == reg_section)
15512 {
15513 reloc_howto_type *howto;
15514
15515 howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
15516 as_bad_where (fixp->fx_file, fixp->fx_line,
15517 _("invalid %s relocation against register"),
15518 howto ? howto->name : "<unknown>");
15519 return 0;
15520 }
15521
15522 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15523 if (fixp->fx_r_type == BFD_RELOC_SIZE32
15524 || fixp->fx_r_type == BFD_RELOC_SIZE64)
15525 return IS_ELF && fixp->fx_addsy
15526 && (!S_IS_DEFINED (fixp->fx_addsy)
15527 || S_IS_EXTERNAL (fixp->fx_addsy));
15528 #endif
15529
15530 if (fixp->fx_subsy)
15531 {
15532 if (fixp->fx_subsy == GOT_symbol)
15533 {
15534 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
15535 {
15536 if (!object_64bit)
15537 abort ();
15538 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15539 if (fixp->fx_tcbit2)
15540 fixp->fx_r_type = (fixp->fx_tcbit
15541 ? BFD_RELOC_X86_64_REX_GOTPCRELX
15542 : BFD_RELOC_X86_64_GOTPCRELX);
15543 else
15544 #endif
15545 fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
15546 }
15547 else
15548 {
15549 if (!object_64bit)
15550 fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
15551 else
15552 fixp->fx_r_type = BFD_RELOC_X86_64_GOTOFF64;
15553 }
15554 fixp->fx_subsy = 0;
15555 }
15556 }
15557 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15558 else
15559 {
15560 /* NB: Commit 292676c1 resolved PLT32 reloc aganst local symbol
15561 to section. Since PLT32 relocation must be against symbols,
15562 turn such PLT32 relocation into PC32 relocation. */
15563 if (fixp->fx_addsy
15564 && (fixp->fx_r_type == BFD_RELOC_386_PLT32
15565 || fixp->fx_r_type == BFD_RELOC_X86_64_PLT32)
15566 && symbol_section_p (fixp->fx_addsy))
15567 fixp->fx_r_type = BFD_RELOC_32_PCREL;
15568 if (!object_64bit)
15569 {
15570 if (fixp->fx_r_type == BFD_RELOC_386_GOT32
15571 && fixp->fx_tcbit2)
15572 fixp->fx_r_type = BFD_RELOC_386_GOT32X;
15573 }
15574 }
15575 #endif
15576
15577 return 1;
15578 }
15579
15580 arelent *
15581 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
15582 {
15583 arelent *rel;
15584 bfd_reloc_code_real_type code;
15585
15586 switch (fixp->fx_r_type)
15587 {
15588 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15589 symbolS *sym;
15590
15591 case BFD_RELOC_SIZE32:
15592 case BFD_RELOC_SIZE64:
15593 if (fixp->fx_addsy
15594 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))
15595 && (!fixp->fx_subsy
15596 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))))
15597 sym = fixp->fx_addsy;
15598 else if (fixp->fx_subsy
15599 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))
15600 && (!fixp->fx_addsy
15601 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))))
15602 sym = fixp->fx_subsy;
15603 else
15604 sym = NULL;
15605 if (IS_ELF && sym && S_IS_DEFINED (sym) && !S_IS_EXTERNAL (sym))
15606 {
15607 /* Resolve size relocation against local symbol to size of
15608 the symbol plus addend. */
15609 valueT value = S_GET_SIZE (sym);
15610
15611 if (symbol_get_bfdsym (sym)->flags & BSF_SECTION_SYM)
15612 value = bfd_section_size (S_GET_SEGMENT (sym));
15613 if (sym == fixp->fx_subsy)
15614 {
15615 value = -value;
15616 if (fixp->fx_addsy)
15617 value += S_GET_VALUE (fixp->fx_addsy);
15618 }
15619 else if (fixp->fx_subsy)
15620 value -= S_GET_VALUE (fixp->fx_subsy);
15621 value += fixp->fx_offset;
15622 if (fixp->fx_r_type == BFD_RELOC_SIZE32
15623 && object_64bit
15624 && !fits_in_unsigned_long (value))
15625 as_bad_where (fixp->fx_file, fixp->fx_line,
15626 _("symbol size computation overflow"));
15627 fixp->fx_addsy = NULL;
15628 fixp->fx_subsy = NULL;
15629 md_apply_fix (fixp, (valueT *) &value, NULL);
15630 return NULL;
15631 }
15632 if (!fixp->fx_addsy || fixp->fx_subsy)
15633 {
15634 as_bad_where (fixp->fx_file, fixp->fx_line,
15635 "unsupported expression involving @size");
15636 return NULL;
15637 }
15638 #endif
15639 /* Fall through. */
15640
15641 case BFD_RELOC_X86_64_PLT32:
15642 case BFD_RELOC_X86_64_GOT32:
15643 case BFD_RELOC_X86_64_GOTPCREL:
15644 case BFD_RELOC_X86_64_GOTPCRELX:
15645 case BFD_RELOC_X86_64_REX_GOTPCRELX:
15646 case BFD_RELOC_386_PLT32:
15647 case BFD_RELOC_386_GOT32:
15648 case BFD_RELOC_386_GOT32X:
15649 case BFD_RELOC_386_GOTOFF:
15650 case BFD_RELOC_386_GOTPC:
15651 case BFD_RELOC_386_TLS_GD:
15652 case BFD_RELOC_386_TLS_LDM:
15653 case BFD_RELOC_386_TLS_LDO_32:
15654 case BFD_RELOC_386_TLS_IE_32:
15655 case BFD_RELOC_386_TLS_IE:
15656 case BFD_RELOC_386_TLS_GOTIE:
15657 case BFD_RELOC_386_TLS_LE_32:
15658 case BFD_RELOC_386_TLS_LE:
15659 case BFD_RELOC_386_TLS_GOTDESC:
15660 case BFD_RELOC_386_TLS_DESC_CALL:
15661 case BFD_RELOC_X86_64_TLSGD:
15662 case BFD_RELOC_X86_64_TLSLD:
15663 case BFD_RELOC_X86_64_DTPOFF32:
15664 case BFD_RELOC_X86_64_DTPOFF64:
15665 case BFD_RELOC_X86_64_GOTTPOFF:
15666 case BFD_RELOC_X86_64_TPOFF32:
15667 case BFD_RELOC_X86_64_TPOFF64:
15668 case BFD_RELOC_X86_64_GOTOFF64:
15669 case BFD_RELOC_X86_64_GOTPC32:
15670 case BFD_RELOC_X86_64_GOT64:
15671 case BFD_RELOC_X86_64_GOTPCREL64:
15672 case BFD_RELOC_X86_64_GOTPC64:
15673 case BFD_RELOC_X86_64_GOTPLT64:
15674 case BFD_RELOC_X86_64_PLTOFF64:
15675 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
15676 case BFD_RELOC_X86_64_TLSDESC_CALL:
15677 case BFD_RELOC_RVA:
15678 case BFD_RELOC_VTABLE_ENTRY:
15679 case BFD_RELOC_VTABLE_INHERIT:
15680 #ifdef TE_PE
15681 case BFD_RELOC_32_SECREL:
15682 case BFD_RELOC_16_SECIDX:
15683 #endif
15684 code = fixp->fx_r_type;
15685 break;
15686 case BFD_RELOC_X86_64_32S:
15687 if (!fixp->fx_pcrel)
15688 {
15689 /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32. */
15690 code = fixp->fx_r_type;
15691 break;
15692 }
15693 /* Fall through. */
15694 default:
15695 if (fixp->fx_pcrel)
15696 {
15697 switch (fixp->fx_size)
15698 {
15699 default:
15700 as_bad_where (fixp->fx_file, fixp->fx_line,
15701 _("can not do %d byte pc-relative relocation"),
15702 fixp->fx_size);
15703 code = BFD_RELOC_32_PCREL;
15704 break;
15705 case 1: code = BFD_RELOC_8_PCREL; break;
15706 case 2: code = BFD_RELOC_16_PCREL; break;
15707 case 4: code = BFD_RELOC_32_PCREL; break;
15708 #ifdef BFD64
15709 case 8: code = BFD_RELOC_64_PCREL; break;
15710 #endif
15711 }
15712 }
15713 else
15714 {
15715 switch (fixp->fx_size)
15716 {
15717 default:
15718 as_bad_where (fixp->fx_file, fixp->fx_line,
15719 _("can not do %d byte relocation"),
15720 fixp->fx_size);
15721 code = BFD_RELOC_32;
15722 break;
15723 case 1: code = BFD_RELOC_8; break;
15724 case 2: code = BFD_RELOC_16; break;
15725 case 4: code = BFD_RELOC_32; break;
15726 #ifdef BFD64
15727 case 8: code = BFD_RELOC_64; break;
15728 #endif
15729 }
15730 }
15731 break;
15732 }
15733
15734 if ((code == BFD_RELOC_32
15735 || code == BFD_RELOC_32_PCREL
15736 || code == BFD_RELOC_X86_64_32S)
15737 && GOT_symbol
15738 && fixp->fx_addsy == GOT_symbol)
15739 {
15740 if (!object_64bit)
15741 code = BFD_RELOC_386_GOTPC;
15742 else
15743 code = BFD_RELOC_X86_64_GOTPC32;
15744 }
15745 if ((code == BFD_RELOC_64 || code == BFD_RELOC_64_PCREL)
15746 && GOT_symbol
15747 && fixp->fx_addsy == GOT_symbol)
15748 {
15749 code = BFD_RELOC_X86_64_GOTPC64;
15750 }
15751
15752 rel = XNEW (arelent);
15753 rel->sym_ptr_ptr = XNEW (asymbol *);
15754 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
15755
15756 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
15757
15758 if (!use_rela_relocations)
15759 {
15760 /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
15761 vtable entry to be used in the relocation's section offset. */
15762 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
15763 rel->address = fixp->fx_offset;
15764 #if defined (OBJ_COFF) && defined (TE_PE)
15765 else if (fixp->fx_addsy && S_IS_WEAK (fixp->fx_addsy))
15766 rel->addend = fixp->fx_addnumber - (S_GET_VALUE (fixp->fx_addsy) * 2);
15767 else
15768 #endif
15769 rel->addend = 0;
15770 }
15771 /* Use the rela in 64bit mode. */
15772 else
15773 {
15774 if (disallow_64bit_reloc)
15775 switch (code)
15776 {
15777 case BFD_RELOC_X86_64_DTPOFF64:
15778 case BFD_RELOC_X86_64_TPOFF64:
15779 case BFD_RELOC_64_PCREL:
15780 case BFD_RELOC_X86_64_GOTOFF64:
15781 case BFD_RELOC_X86_64_GOT64:
15782 case BFD_RELOC_X86_64_GOTPCREL64:
15783 case BFD_RELOC_X86_64_GOTPC64:
15784 case BFD_RELOC_X86_64_GOTPLT64:
15785 case BFD_RELOC_X86_64_PLTOFF64:
15786 as_bad_where (fixp->fx_file, fixp->fx_line,
15787 _("cannot represent relocation type %s in x32 mode"),
15788 bfd_get_reloc_code_name (code));
15789 break;
15790 default:
15791 break;
15792 }
15793
15794 if (!fixp->fx_pcrel)
15795 rel->addend = fixp->fx_offset;
15796 else
15797 switch (code)
15798 {
15799 case BFD_RELOC_X86_64_PLT32:
15800 case BFD_RELOC_X86_64_GOT32:
15801 case BFD_RELOC_X86_64_GOTPCREL:
15802 case BFD_RELOC_X86_64_GOTPCRELX:
15803 case BFD_RELOC_X86_64_REX_GOTPCRELX:
15804 case BFD_RELOC_X86_64_TLSGD:
15805 case BFD_RELOC_X86_64_TLSLD:
15806 case BFD_RELOC_X86_64_GOTTPOFF:
15807 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
15808 case BFD_RELOC_X86_64_TLSDESC_CALL:
15809 rel->addend = fixp->fx_offset - fixp->fx_size;
15810 break;
15811 default:
15812 rel->addend = (section->vma
15813 - fixp->fx_size
15814 + fixp->fx_addnumber
15815 + md_pcrel_from (fixp));
15816 break;
15817 }
15818 }
15819
15820 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
15821 if (rel->howto == NULL)
15822 {
15823 as_bad_where (fixp->fx_file, fixp->fx_line,
15824 _("cannot represent relocation type %s"),
15825 bfd_get_reloc_code_name (code));
15826 /* Set howto to a garbage value so that we can keep going. */
15827 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
15828 gas_assert (rel->howto != NULL);
15829 }
15830
15831 return rel;
15832 }
15833
15834 #include "tc-i386-intel.c"
15835
15836 void
15837 tc_x86_parse_to_dw2regnum (expressionS *exp)
15838 {
15839 int saved_naked_reg;
15840 char saved_register_dot;
15841
15842 saved_naked_reg = allow_naked_reg;
15843 allow_naked_reg = 1;
15844 saved_register_dot = register_chars['.'];
15845 register_chars['.'] = '.';
15846 allow_pseudo_reg = 1;
15847 expression_and_evaluate (exp);
15848 allow_pseudo_reg = 0;
15849 register_chars['.'] = saved_register_dot;
15850 allow_naked_reg = saved_naked_reg;
15851
15852 if (exp->X_op == O_register && exp->X_add_number >= 0)
15853 {
15854 if ((addressT) exp->X_add_number < i386_regtab_size)
15855 {
15856 exp->X_op = O_constant;
15857 exp->X_add_number = i386_regtab[exp->X_add_number]
15858 .dw2_regnum[flag_code >> 1];
15859 }
15860 else
15861 exp->X_op = O_illegal;
15862 }
15863 }
15864
15865 void
15866 tc_x86_frame_initial_instructions (void)
15867 {
15868 static unsigned int sp_regno[2];
15869
15870 if (!sp_regno[flag_code >> 1])
15871 {
15872 char *saved_input = input_line_pointer;
15873 char sp[][4] = {"esp", "rsp"};
15874 expressionS exp;
15875
15876 input_line_pointer = sp[flag_code >> 1];
15877 tc_x86_parse_to_dw2regnum (&exp);
15878 gas_assert (exp.X_op == O_constant);
15879 sp_regno[flag_code >> 1] = exp.X_add_number;
15880 input_line_pointer = saved_input;
15881 }
15882
15883 cfi_add_CFA_def_cfa (sp_regno[flag_code >> 1], -x86_cie_data_alignment);
15884 cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
15885 }
15886
15887 int
15888 x86_dwarf2_addr_size (void)
15889 {
15890 #if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
15891 if (x86_elf_abi == X86_64_X32_ABI)
15892 return 4;
15893 #endif
15894 return bfd_arch_bits_per_address (stdoutput) / 8;
15895 }
15896
15897 int
15898 i386_elf_section_type (const char *str, size_t len)
15899 {
15900 if (flag_code == CODE_64BIT
15901 && len == sizeof ("unwind") - 1
15902 && startswith (str, "unwind"))
15903 return SHT_X86_64_UNWIND;
15904
15905 return -1;
15906 }
15907
15908 #ifdef TE_SOLARIS
15909 void
15910 i386_solaris_fix_up_eh_frame (segT sec)
15911 {
15912 if (flag_code == CODE_64BIT)
15913 elf_section_type (sec) = SHT_X86_64_UNWIND;
15914 }
15915 #endif
15916
15917 #ifdef TE_PE
15918 void
15919 tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
15920 {
15921 expressionS exp;
15922
15923 exp.X_op = O_secrel;
15924 exp.X_add_symbol = symbol;
15925 exp.X_add_number = 0;
15926 emit_expr (&exp, size);
15927 }
15928 #endif
15929
15930 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15931 /* For ELF on x86-64, add support for SHF_X86_64_LARGE. */
15932
15933 bfd_vma
15934 x86_64_section_letter (int letter, const char **ptr_msg)
15935 {
15936 if (flag_code == CODE_64BIT)
15937 {
15938 if (letter == 'l')
15939 return SHF_X86_64_LARGE;
15940
15941 *ptr_msg = _("bad .section directive: want a,l,w,x,M,S,G,T in string");
15942 }
15943 else
15944 *ptr_msg = _("bad .section directive: want a,w,x,M,S,G,T in string");
15945 return -1;
15946 }
15947
15948 static void
15949 handle_large_common (int small ATTRIBUTE_UNUSED)
15950 {
15951 if (flag_code != CODE_64BIT)
15952 {
15953 s_comm_internal (0, elf_common_parse);
15954 as_warn (_(".largecomm supported only in 64bit mode, producing .comm"));
15955 }
15956 else
15957 {
15958 static segT lbss_section;
15959 asection *saved_com_section_ptr = elf_com_section_ptr;
15960 asection *saved_bss_section = bss_section;
15961
15962 if (lbss_section == NULL)
15963 {
15964 flagword applicable;
15965 segT seg = now_seg;
15966 subsegT subseg = now_subseg;
15967
15968 /* The .lbss section is for local .largecomm symbols. */
15969 lbss_section = subseg_new (".lbss", 0);
15970 applicable = bfd_applicable_section_flags (stdoutput);
15971 bfd_set_section_flags (lbss_section, applicable & SEC_ALLOC);
15972 seg_info (lbss_section)->bss = 1;
15973
15974 subseg_set (seg, subseg);
15975 }
15976
15977 elf_com_section_ptr = &_bfd_elf_large_com_section;
15978 bss_section = lbss_section;
15979
15980 s_comm_internal (0, elf_common_parse);
15981
15982 elf_com_section_ptr = saved_com_section_ptr;
15983 bss_section = saved_bss_section;
15984 }
15985 }
15986 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */