x86-64: fix suffix-less PUSH of symbol address
[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_error
440 } vec_encoding;
441
442 /* REP prefix. */
443 const char *rep_prefix;
444
445 /* HLE prefix. */
446 const char *hle_prefix;
447
448 /* Have BND prefix. */
449 const char *bnd_prefix;
450
451 /* Have NOTRACK prefix. */
452 const char *notrack_prefix;
453
454 /* Error message. */
455 enum i386_error error;
456 };
457
458 typedef struct _i386_insn i386_insn;
459
460 /* Link RC type with corresponding string, that'll be looked for in
461 asm. */
462 struct RC_name
463 {
464 enum rc_type type;
465 const char *name;
466 unsigned int len;
467 };
468
469 static const struct RC_name RC_NamesTable[] =
470 {
471 { rne, STRING_COMMA_LEN ("rn-sae") },
472 { rd, STRING_COMMA_LEN ("rd-sae") },
473 { ru, STRING_COMMA_LEN ("ru-sae") },
474 { rz, STRING_COMMA_LEN ("rz-sae") },
475 { saeonly, STRING_COMMA_LEN ("sae") },
476 };
477
478 /* To be indexed by segment register number. */
479 static const unsigned char i386_seg_prefixes[] = {
480 ES_PREFIX_OPCODE,
481 CS_PREFIX_OPCODE,
482 SS_PREFIX_OPCODE,
483 DS_PREFIX_OPCODE,
484 FS_PREFIX_OPCODE,
485 GS_PREFIX_OPCODE
486 };
487
488 /* List of chars besides those in app.c:symbol_chars that can start an
489 operand. Used to prevent the scrubber eating vital white-space. */
490 const char extra_symbol_chars[] = "*%-([{}"
491 #ifdef LEX_AT
492 "@"
493 #endif
494 #ifdef LEX_QM
495 "?"
496 #endif
497 ;
498
499 #if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
500 && !defined (TE_GNU) \
501 && !defined (TE_LINUX) \
502 && !defined (TE_Haiku) \
503 && !defined (TE_FreeBSD) \
504 && !defined (TE_DragonFly) \
505 && !defined (TE_NetBSD))
506 /* This array holds the chars that always start a comment. If the
507 pre-processor is disabled, these aren't very useful. The option
508 --divide will remove '/' from this list. */
509 const char *i386_comment_chars = "#/";
510 #define SVR4_COMMENT_CHARS 1
511 #define PREFIX_SEPARATOR '\\'
512
513 #else
514 const char *i386_comment_chars = "#";
515 #define PREFIX_SEPARATOR '/'
516 #endif
517
518 /* This array holds the chars that only start a comment at the beginning of
519 a line. If the line seems to have the form '# 123 filename'
520 .line and .file directives will appear in the pre-processed output.
521 Note that input_file.c hand checks for '#' at the beginning of the
522 first line of the input file. This is because the compiler outputs
523 #NO_APP at the beginning of its output.
524 Also note that comments started like this one will always work if
525 '/' isn't otherwise defined. */
526 const char line_comment_chars[] = "#/";
527
528 const char line_separator_chars[] = ";";
529
530 /* Chars that can be used to separate mant from exp in floating point
531 nums. */
532 const char EXP_CHARS[] = "eE";
533
534 /* Chars that mean this number is a floating point constant
535 As in 0f12.456
536 or 0d1.2345e12. */
537 const char FLT_CHARS[] = "fFdDxXhHbB";
538
539 /* Tables for lexical analysis. */
540 static char mnemonic_chars[256];
541 static char register_chars[256];
542 static char operand_chars[256];
543
544 /* Lexical macros. */
545 #define is_operand_char(x) (operand_chars[(unsigned char) x])
546 #define is_register_char(x) (register_chars[(unsigned char) x])
547 #define is_space_char(x) ((x) == ' ')
548
549 /* All non-digit non-letter characters that may occur in an operand and
550 which aren't already in extra_symbol_chars[]. */
551 static const char operand_special_chars[] = "$+,)._~/<>|&^!=:@]";
552
553 /* md_assemble() always leaves the strings it's passed unaltered. To
554 effect this we maintain a stack of saved characters that we've smashed
555 with '\0's (indicating end of strings for various sub-fields of the
556 assembler instruction). */
557 static char save_stack[32];
558 static char *save_stack_p;
559 #define END_STRING_AND_SAVE(s) \
560 do { *save_stack_p++ = *(s); *(s) = '\0'; } while (0)
561 #define RESTORE_END_STRING(s) \
562 do { *(s) = *--save_stack_p; } while (0)
563
564 /* The instruction we're assembling. */
565 static i386_insn i;
566
567 /* Possible templates for current insn. */
568 static const templates *current_templates;
569
570 /* Per instruction expressionS buffers: max displacements & immediates. */
571 static expressionS disp_expressions[MAX_MEMORY_OPERANDS];
572 static expressionS im_expressions[MAX_IMMEDIATE_OPERANDS];
573
574 /* Current operand we are working on. */
575 static int this_operand = -1;
576
577 /* Are we processing a .insn directive? */
578 #define dot_insn() (i.tm.mnem_off == MN__insn)
579
580 /* We support four different modes. FLAG_CODE variable is used to distinguish
581 these. */
582
583 enum flag_code {
584 CODE_32BIT,
585 CODE_16BIT,
586 CODE_64BIT };
587
588 static enum flag_code flag_code;
589 static unsigned int object_64bit;
590 static unsigned int disallow_64bit_reloc;
591 static int use_rela_relocations = 0;
592 /* __tls_get_addr/___tls_get_addr symbol for TLS. */
593 static const char *tls_get_addr;
594
595 #if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
596 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
597 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
598
599 /* The ELF ABI to use. */
600 enum x86_elf_abi
601 {
602 I386_ABI,
603 X86_64_ABI,
604 X86_64_X32_ABI
605 };
606
607 static enum x86_elf_abi x86_elf_abi = I386_ABI;
608 #endif
609
610 #if defined (TE_PE) || defined (TE_PEP)
611 /* Use big object file format. */
612 static int use_big_obj = 0;
613 #endif
614
615 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
616 /* 1 if generating code for a shared library. */
617 static int shared = 0;
618
619 unsigned int x86_sframe_cfa_sp_reg;
620 /* The other CFA base register for SFrame stack trace info. */
621 unsigned int x86_sframe_cfa_fp_reg;
622 unsigned int x86_sframe_cfa_ra_reg;
623
624 #endif
625
626 /* 1 for intel syntax,
627 0 if att syntax. */
628 static int intel_syntax = 0;
629
630 static enum x86_64_isa
631 {
632 amd64 = 1, /* AMD64 ISA. */
633 intel64 /* Intel64 ISA. */
634 } isa64;
635
636 /* 1 for intel mnemonic,
637 0 if att mnemonic. */
638 static int intel_mnemonic = !SYSV386_COMPAT;
639
640 /* 1 if pseudo registers are permitted. */
641 static int allow_pseudo_reg = 0;
642
643 /* 1 if register prefix % not required. */
644 static int allow_naked_reg = 0;
645
646 /* 1 if the assembler should add BND prefix for all control-transferring
647 instructions supporting it, even if this prefix wasn't specified
648 explicitly. */
649 static int add_bnd_prefix = 0;
650
651 /* 1 if pseudo index register, eiz/riz, is allowed . */
652 static int allow_index_reg = 0;
653
654 /* 1 if the assembler should ignore LOCK prefix, even if it was
655 specified explicitly. */
656 static int omit_lock_prefix = 0;
657
658 /* 1 if the assembler should encode lfence, mfence, and sfence as
659 "lock addl $0, (%{re}sp)". */
660 static int avoid_fence = 0;
661
662 /* 1 if lfence should be inserted after every load. */
663 static int lfence_after_load = 0;
664
665 /* Non-zero if lfence should be inserted before indirect branch. */
666 static enum lfence_before_indirect_branch_kind
667 {
668 lfence_branch_none = 0,
669 lfence_branch_register,
670 lfence_branch_memory,
671 lfence_branch_all
672 }
673 lfence_before_indirect_branch;
674
675 /* Non-zero if lfence should be inserted before ret. */
676 static enum lfence_before_ret_kind
677 {
678 lfence_before_ret_none = 0,
679 lfence_before_ret_not,
680 lfence_before_ret_or,
681 lfence_before_ret_shl
682 }
683 lfence_before_ret;
684
685 /* Types of previous instruction is .byte or prefix. */
686 static struct
687 {
688 segT seg;
689 const char *file;
690 const char *name;
691 unsigned int line;
692 enum last_insn_kind
693 {
694 last_insn_other = 0,
695 last_insn_directive,
696 last_insn_prefix
697 } kind;
698 } last_insn;
699
700 /* 1 if the assembler should generate relax relocations. */
701
702 static int generate_relax_relocations
703 = DEFAULT_GENERATE_X86_RELAX_RELOCATIONS;
704
705 static enum check_kind
706 {
707 check_none = 0,
708 check_warning,
709 check_error
710 }
711 sse_check, operand_check = check_warning;
712
713 /* Non-zero if branches should be aligned within power of 2 boundary. */
714 static int align_branch_power = 0;
715
716 /* Types of branches to align. */
717 enum align_branch_kind
718 {
719 align_branch_none = 0,
720 align_branch_jcc = 1,
721 align_branch_fused = 2,
722 align_branch_jmp = 3,
723 align_branch_call = 4,
724 align_branch_indirect = 5,
725 align_branch_ret = 6
726 };
727
728 /* Type bits of branches to align. */
729 enum align_branch_bit
730 {
731 align_branch_jcc_bit = 1 << align_branch_jcc,
732 align_branch_fused_bit = 1 << align_branch_fused,
733 align_branch_jmp_bit = 1 << align_branch_jmp,
734 align_branch_call_bit = 1 << align_branch_call,
735 align_branch_indirect_bit = 1 << align_branch_indirect,
736 align_branch_ret_bit = 1 << align_branch_ret
737 };
738
739 static unsigned int align_branch = (align_branch_jcc_bit
740 | align_branch_fused_bit
741 | align_branch_jmp_bit);
742
743 /* Types of condition jump used by macro-fusion. */
744 enum mf_jcc_kind
745 {
746 mf_jcc_jo = 0, /* base opcode 0x70 */
747 mf_jcc_jc, /* base opcode 0x72 */
748 mf_jcc_je, /* base opcode 0x74 */
749 mf_jcc_jna, /* base opcode 0x76 */
750 mf_jcc_js, /* base opcode 0x78 */
751 mf_jcc_jp, /* base opcode 0x7a */
752 mf_jcc_jl, /* base opcode 0x7c */
753 mf_jcc_jle, /* base opcode 0x7e */
754 };
755
756 /* Types of compare flag-modifying insntructions used by macro-fusion. */
757 enum mf_cmp_kind
758 {
759 mf_cmp_test_and, /* test/cmp */
760 mf_cmp_alu_cmp, /* add/sub/cmp */
761 mf_cmp_incdec /* inc/dec */
762 };
763
764 /* The maximum padding size for fused jcc. CMP like instruction can
765 be 9 bytes and jcc can be 6 bytes. Leave room just in case for
766 prefixes. */
767 #define MAX_FUSED_JCC_PADDING_SIZE 20
768
769 /* The maximum number of prefixes added for an instruction. */
770 static unsigned int align_branch_prefix_size = 5;
771
772 /* Optimization:
773 1. Clear the REX_W bit with register operand if possible.
774 2. Above plus use 128bit vector instruction to clear the full vector
775 register.
776 */
777 static int optimize = 0;
778
779 /* Optimization:
780 1. Clear the REX_W bit with register operand if possible.
781 2. Above plus use 128bit vector instruction to clear the full vector
782 register.
783 3. Above plus optimize "test{q,l,w} $imm8,%r{64,32,16}" to
784 "testb $imm7,%r8".
785 */
786 static int optimize_for_space = 0;
787
788 /* Register prefix used for error message. */
789 static const char *register_prefix = "%";
790
791 /* Used in 16 bit gcc mode to add an l suffix to call, ret, enter,
792 leave, push, and pop instructions so that gcc has the same stack
793 frame as in 32 bit mode. */
794 static char stackop_size = '\0';
795
796 /* Non-zero to optimize code alignment. */
797 int optimize_align_code = 1;
798
799 /* Non-zero to quieten some warnings. */
800 static int quiet_warnings = 0;
801
802 /* Guard to avoid repeated warnings about non-16-bit code on 16-bit CPUs. */
803 static bool pre_386_16bit_warned;
804
805 /* CPU name. */
806 static const char *cpu_arch_name = NULL;
807 static char *cpu_sub_arch_name = NULL;
808
809 /* CPU feature flags. */
810 static i386_cpu_flags cpu_arch_flags = CPU_UNKNOWN_FLAGS;
811
812 /* If we have selected a cpu we are generating instructions for. */
813 static int cpu_arch_tune_set = 0;
814
815 /* Cpu we are generating instructions for. */
816 enum processor_type cpu_arch_tune = PROCESSOR_UNKNOWN;
817
818 /* CPU feature flags of cpu we are generating instructions for. */
819 static i386_cpu_flags cpu_arch_tune_flags;
820
821 /* CPU instruction set architecture used. */
822 enum processor_type cpu_arch_isa = PROCESSOR_UNKNOWN;
823
824 /* CPU feature flags of instruction set architecture used. */
825 i386_cpu_flags cpu_arch_isa_flags;
826
827 /* If set, conditional jumps are not automatically promoted to handle
828 larger than a byte offset. */
829 static bool no_cond_jump_promotion = false;
830
831 /* This will be set from an expression parser hook if there's any
832 applicable operator involved in an expression. */
833 static enum {
834 expr_operator_none,
835 expr_operator_present,
836 expr_large_value,
837 } expr_mode;
838
839 /* Encode SSE instructions with VEX prefix. */
840 static unsigned int sse2avx;
841
842 /* Encode aligned vector move as unaligned vector move. */
843 static unsigned int use_unaligned_vector_move;
844
845 /* Maximum permitted vector size. */
846 #define VSZ_DEFAULT VSZ512
847 static unsigned int vector_size = VSZ_DEFAULT;
848
849 /* Encode scalar AVX instructions with specific vector length. */
850 static enum
851 {
852 vex128 = 0,
853 vex256
854 } avxscalar;
855
856 /* Encode VEX WIG instructions with specific vex.w. */
857 static enum
858 {
859 vexw0 = 0,
860 vexw1
861 } vexwig;
862
863 /* Encode scalar EVEX LIG instructions with specific vector length. */
864 static enum
865 {
866 evexl128 = 0,
867 evexl256,
868 evexl512
869 } evexlig;
870
871 /* Encode EVEX WIG instructions with specific evex.w. */
872 static enum
873 {
874 evexw0 = 0,
875 evexw1
876 } evexwig;
877
878 /* Value to encode in EVEX RC bits, for SAE-only instructions. */
879 static enum rc_type evexrcig = rne;
880
881 /* Pre-defined "_GLOBAL_OFFSET_TABLE_". */
882 static symbolS *GOT_symbol;
883
884 /* The dwarf2 return column, adjusted for 32 or 64 bit. */
885 unsigned int x86_dwarf2_return_column;
886
887 /* The dwarf2 data alignment, adjusted for 32 or 64 bit. */
888 int x86_cie_data_alignment;
889
890 /* Interface to relax_segment.
891 There are 3 major relax states for 386 jump insns because the
892 different types of jumps add different sizes to frags when we're
893 figuring out what sort of jump to choose to reach a given label.
894
895 BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING are used to align
896 branches which are handled by md_estimate_size_before_relax() and
897 i386_generic_table_relax_frag(). */
898
899 /* Types. */
900 #define UNCOND_JUMP 0
901 #define COND_JUMP 1
902 #define COND_JUMP86 2
903 #define BRANCH_PADDING 3
904 #define BRANCH_PREFIX 4
905 #define FUSED_JCC_PADDING 5
906
907 /* Sizes. */
908 #define CODE16 1
909 #define SMALL 0
910 #define SMALL16 (SMALL | CODE16)
911 #define BIG 2
912 #define BIG16 (BIG | CODE16)
913
914 #ifndef INLINE
915 #ifdef __GNUC__
916 #define INLINE __inline__
917 #else
918 #define INLINE
919 #endif
920 #endif
921
922 #define ENCODE_RELAX_STATE(type, size) \
923 ((relax_substateT) (((type) << 2) | (size)))
924 #define TYPE_FROM_RELAX_STATE(s) \
925 ((s) >> 2)
926 #define DISP_SIZE_FROM_RELAX_STATE(s) \
927 ((((s) & 3) == BIG ? 4 : (((s) & 3) == BIG16 ? 2 : 1)))
928
929 /* This table is used by relax_frag to promote short jumps to long
930 ones where necessary. SMALL (short) jumps may be promoted to BIG
931 (32 bit long) ones, and SMALL16 jumps to BIG16 (16 bit long). We
932 don't allow a short jump in a 32 bit code segment to be promoted to
933 a 16 bit offset jump because it's slower (requires data size
934 prefix), and doesn't work, unless the destination is in the bottom
935 64k of the code segment (The top 16 bits of eip are zeroed). */
936
937 const relax_typeS md_relax_table[] =
938 {
939 /* The fields are:
940 1) most positive reach of this state,
941 2) most negative reach of this state,
942 3) how many bytes this mode will have in the variable part of the frag
943 4) which index into the table to try if we can't fit into this one. */
944
945 /* UNCOND_JUMP states. */
946 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG)},
947 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16)},
948 /* dword jmp adds 4 bytes to frag:
949 0 extra opcode bytes, 4 displacement bytes. */
950 {0, 0, 4, 0},
951 /* word jmp adds 2 byte2 to frag:
952 0 extra opcode bytes, 2 displacement bytes. */
953 {0, 0, 2, 0},
954
955 /* COND_JUMP states. */
956 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG)},
957 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG16)},
958 /* dword conditionals adds 5 bytes to frag:
959 1 extra opcode byte, 4 displacement bytes. */
960 {0, 0, 5, 0},
961 /* word conditionals add 3 bytes to frag:
962 1 extra opcode byte, 2 displacement bytes. */
963 {0, 0, 3, 0},
964
965 /* COND_JUMP86 states. */
966 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG)},
967 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG16)},
968 /* dword conditionals adds 5 bytes to frag:
969 1 extra opcode byte, 4 displacement bytes. */
970 {0, 0, 5, 0},
971 /* word conditionals add 4 bytes to frag:
972 1 displacement byte and a 3 byte long branch insn. */
973 {0, 0, 4, 0}
974 };
975
976 #define ARCH(n, t, f, s) \
977 { STRING_COMMA_LEN (#n), s, PROCESSOR_ ## t, vsz_none, CPU_ ## f ## _FLAGS, \
978 CPU_NONE_FLAGS }
979 #define SUBARCH(n, e, d, s) \
980 { STRING_COMMA_LEN (#n), s, PROCESSOR_NONE, vsz_none, CPU_ ## e ## _FLAGS, \
981 CPU_ ## d ## _FLAGS }
982 #define VECARCH(n, e, d, v) \
983 { STRING_COMMA_LEN (#n), false, PROCESSOR_NONE, vsz_ ## v, \
984 CPU_ ## e ## _FLAGS, CPU_ ## d ## _FLAGS }
985
986 static const arch_entry cpu_arch[] =
987 {
988 /* Do not replace the first two entries - i386_target_format() and
989 set_cpu_arch() rely on them being there in this order. */
990 ARCH (generic32, GENERIC32, GENERIC32, false),
991 ARCH (generic64, GENERIC64, GENERIC64, false),
992 ARCH (i8086, UNKNOWN, NONE, false),
993 ARCH (i186, UNKNOWN, 186, false),
994 ARCH (i286, UNKNOWN, 286, false),
995 ARCH (i386, I386, 386, false),
996 ARCH (i486, I486, 486, false),
997 ARCH (i586, PENTIUM, 586, false),
998 ARCH (i686, PENTIUMPRO, 686, false),
999 ARCH (pentium, PENTIUM, 586, false),
1000 ARCH (pentiumpro, PENTIUMPRO, PENTIUMPRO, false),
1001 ARCH (pentiumii, PENTIUMPRO, P2, false),
1002 ARCH (pentiumiii, PENTIUMPRO, P3, false),
1003 ARCH (pentium4, PENTIUM4, P4, false),
1004 ARCH (prescott, NOCONA, CORE, false),
1005 ARCH (nocona, NOCONA, NOCONA, false),
1006 ARCH (yonah, CORE, CORE, true),
1007 ARCH (core, CORE, CORE, false),
1008 ARCH (merom, CORE2, CORE2, true),
1009 ARCH (core2, CORE2, CORE2, false),
1010 ARCH (corei7, COREI7, COREI7, false),
1011 ARCH (iamcu, IAMCU, IAMCU, false),
1012 ARCH (k6, K6, K6, false),
1013 ARCH (k6_2, K6, K6_2, false),
1014 ARCH (athlon, ATHLON, ATHLON, false),
1015 ARCH (sledgehammer, K8, K8, true),
1016 ARCH (opteron, K8, K8, false),
1017 ARCH (k8, K8, K8, false),
1018 ARCH (amdfam10, AMDFAM10, AMDFAM10, false),
1019 ARCH (bdver1, BD, BDVER1, false),
1020 ARCH (bdver2, BD, BDVER2, false),
1021 ARCH (bdver3, BD, BDVER3, false),
1022 ARCH (bdver4, BD, BDVER4, false),
1023 ARCH (znver1, ZNVER, ZNVER1, false),
1024 ARCH (znver2, ZNVER, ZNVER2, false),
1025 ARCH (znver3, ZNVER, ZNVER3, false),
1026 ARCH (znver4, ZNVER, ZNVER4, false),
1027 ARCH (btver1, BT, BTVER1, false),
1028 ARCH (btver2, BT, BTVER2, false),
1029
1030 SUBARCH (8087, 8087, ANY_8087, false),
1031 SUBARCH (87, NONE, ANY_8087, false), /* Disable only! */
1032 SUBARCH (287, 287, ANY_287, false),
1033 SUBARCH (387, 387, ANY_387, false),
1034 SUBARCH (687, 687, ANY_687, false),
1035 SUBARCH (cmov, CMOV, CMOV, false),
1036 SUBARCH (fxsr, FXSR, ANY_FXSR, false),
1037 SUBARCH (mmx, MMX, ANY_MMX, false),
1038 SUBARCH (sse, SSE, ANY_SSE, false),
1039 SUBARCH (sse2, SSE2, ANY_SSE2, false),
1040 SUBARCH (sse3, SSE3, ANY_SSE3, false),
1041 SUBARCH (sse4a, SSE4A, ANY_SSE4A, false),
1042 SUBARCH (ssse3, SSSE3, ANY_SSSE3, false),
1043 SUBARCH (sse4.1, SSE4_1, ANY_SSE4_1, false),
1044 SUBARCH (sse4.2, SSE4_2, ANY_SSE4_2, false),
1045 SUBARCH (sse4, SSE4_2, ANY_SSE4_1, false),
1046 VECARCH (avx, AVX, ANY_AVX, reset),
1047 VECARCH (avx2, AVX2, ANY_AVX2, reset),
1048 VECARCH (avx512f, AVX512F, ANY_AVX512F, reset),
1049 VECARCH (avx512cd, AVX512CD, ANY_AVX512CD, reset),
1050 VECARCH (avx512er, AVX512ER, ANY_AVX512ER, reset),
1051 VECARCH (avx512pf, AVX512PF, ANY_AVX512PF, reset),
1052 VECARCH (avx512dq, AVX512DQ, ANY_AVX512DQ, reset),
1053 VECARCH (avx512bw, AVX512BW, ANY_AVX512BW, reset),
1054 VECARCH (avx512vl, AVX512VL, ANY_AVX512VL, reset),
1055 SUBARCH (monitor, MONITOR, MONITOR, false),
1056 SUBARCH (vmx, VMX, ANY_VMX, false),
1057 SUBARCH (vmfunc, VMFUNC, ANY_VMFUNC, false),
1058 SUBARCH (smx, SMX, SMX, false),
1059 SUBARCH (xsave, XSAVE, ANY_XSAVE, false),
1060 SUBARCH (xsaveopt, XSAVEOPT, ANY_XSAVEOPT, false),
1061 SUBARCH (xsavec, XSAVEC, ANY_XSAVEC, false),
1062 SUBARCH (xsaves, XSAVES, ANY_XSAVES, false),
1063 SUBARCH (aes, AES, ANY_AES, false),
1064 SUBARCH (pclmul, PCLMULQDQ, ANY_PCLMULQDQ, false),
1065 SUBARCH (clmul, PCLMULQDQ, ANY_PCLMULQDQ, true),
1066 SUBARCH (fsgsbase, FSGSBASE, FSGSBASE, false),
1067 SUBARCH (rdrnd, RDRND, RDRND, false),
1068 SUBARCH (f16c, F16C, ANY_F16C, false),
1069 SUBARCH (bmi2, BMI2, BMI2, false),
1070 SUBARCH (fma, FMA, ANY_FMA, false),
1071 SUBARCH (fma4, FMA4, ANY_FMA4, false),
1072 SUBARCH (xop, XOP, ANY_XOP, false),
1073 SUBARCH (lwp, LWP, ANY_LWP, false),
1074 SUBARCH (movbe, MOVBE, MOVBE, false),
1075 SUBARCH (cx16, CX16, CX16, false),
1076 SUBARCH (lahf_sahf, LAHF_SAHF, LAHF_SAHF, false),
1077 SUBARCH (ept, EPT, ANY_EPT, false),
1078 SUBARCH (lzcnt, LZCNT, LZCNT, false),
1079 SUBARCH (popcnt, POPCNT, POPCNT, false),
1080 SUBARCH (hle, HLE, HLE, false),
1081 SUBARCH (rtm, RTM, ANY_RTM, false),
1082 SUBARCH (tsx, TSX, TSX, false),
1083 SUBARCH (invpcid, INVPCID, INVPCID, false),
1084 SUBARCH (clflush, CLFLUSH, CLFLUSH, false),
1085 SUBARCH (nop, NOP, NOP, false),
1086 SUBARCH (syscall, SYSCALL, SYSCALL, false),
1087 SUBARCH (rdtscp, RDTSCP, RDTSCP, false),
1088 SUBARCH (3dnow, 3DNOW, ANY_3DNOW, false),
1089 SUBARCH (3dnowa, 3DNOWA, ANY_3DNOWA, false),
1090 SUBARCH (padlock, PADLOCK, PADLOCK, false),
1091 SUBARCH (pacifica, SVME, ANY_SVME, true),
1092 SUBARCH (svme, SVME, ANY_SVME, false),
1093 SUBARCH (abm, ABM, ABM, false),
1094 SUBARCH (bmi, BMI, BMI, false),
1095 SUBARCH (tbm, TBM, TBM, false),
1096 SUBARCH (adx, ADX, ADX, false),
1097 SUBARCH (rdseed, RDSEED, RDSEED, false),
1098 SUBARCH (prfchw, PRFCHW, PRFCHW, false),
1099 SUBARCH (smap, SMAP, SMAP, false),
1100 SUBARCH (mpx, MPX, ANY_MPX, false),
1101 SUBARCH (sha, SHA, ANY_SHA, false),
1102 SUBARCH (clflushopt, CLFLUSHOPT, CLFLUSHOPT, false),
1103 SUBARCH (prefetchwt1, PREFETCHWT1, PREFETCHWT1, false),
1104 SUBARCH (se1, SE1, SE1, false),
1105 SUBARCH (clwb, CLWB, CLWB, false),
1106 VECARCH (avx512ifma, AVX512IFMA, ANY_AVX512IFMA, reset),
1107 VECARCH (avx512vbmi, AVX512VBMI, ANY_AVX512VBMI, reset),
1108 VECARCH (avx512_4fmaps, AVX512_4FMAPS, ANY_AVX512_4FMAPS, reset),
1109 VECARCH (avx512_4vnniw, AVX512_4VNNIW, ANY_AVX512_4VNNIW, reset),
1110 VECARCH (avx512_vpopcntdq, AVX512_VPOPCNTDQ, ANY_AVX512_VPOPCNTDQ, reset),
1111 VECARCH (avx512_vbmi2, AVX512_VBMI2, ANY_AVX512_VBMI2, reset),
1112 VECARCH (avx512_vnni, AVX512_VNNI, ANY_AVX512_VNNI, reset),
1113 VECARCH (avx512_bitalg, AVX512_BITALG, ANY_AVX512_BITALG, reset),
1114 VECARCH (avx_vnni, AVX_VNNI, ANY_AVX_VNNI, reset),
1115 SUBARCH (clzero, CLZERO, CLZERO, false),
1116 SUBARCH (mwaitx, MWAITX, MWAITX, false),
1117 SUBARCH (ospke, OSPKE, ANY_OSPKE, false),
1118 SUBARCH (rdpid, RDPID, RDPID, false),
1119 SUBARCH (ptwrite, PTWRITE, PTWRITE, false),
1120 SUBARCH (ibt, IBT, IBT, false),
1121 SUBARCH (shstk, SHSTK, SHSTK, false),
1122 SUBARCH (gfni, GFNI, ANY_GFNI, false),
1123 VECARCH (vaes, VAES, ANY_VAES, reset),
1124 VECARCH (vpclmulqdq, VPCLMULQDQ, ANY_VPCLMULQDQ, reset),
1125 SUBARCH (wbnoinvd, WBNOINVD, WBNOINVD, false),
1126 SUBARCH (pconfig, PCONFIG, PCONFIG, false),
1127 SUBARCH (waitpkg, WAITPKG, WAITPKG, false),
1128 SUBARCH (cldemote, CLDEMOTE, CLDEMOTE, false),
1129 SUBARCH (amx_int8, AMX_INT8, ANY_AMX_INT8, false),
1130 SUBARCH (amx_bf16, AMX_BF16, ANY_AMX_BF16, false),
1131 SUBARCH (amx_fp16, AMX_FP16, ANY_AMX_FP16, false),
1132 SUBARCH (amx_complex, AMX_COMPLEX, ANY_AMX_COMPLEX, false),
1133 SUBARCH (amx_tile, AMX_TILE, ANY_AMX_TILE, false),
1134 SUBARCH (movdiri, MOVDIRI, MOVDIRI, false),
1135 SUBARCH (movdir64b, MOVDIR64B, MOVDIR64B, false),
1136 VECARCH (avx512_bf16, AVX512_BF16, ANY_AVX512_BF16, reset),
1137 VECARCH (avx512_vp2intersect, AVX512_VP2INTERSECT,
1138 ANY_AVX512_VP2INTERSECT, reset),
1139 SUBARCH (tdx, TDX, TDX, false),
1140 SUBARCH (enqcmd, ENQCMD, ENQCMD, false),
1141 SUBARCH (serialize, SERIALIZE, SERIALIZE, false),
1142 SUBARCH (rdpru, RDPRU, RDPRU, false),
1143 SUBARCH (mcommit, MCOMMIT, MCOMMIT, false),
1144 SUBARCH (sev_es, SEV_ES, ANY_SEV_ES, false),
1145 SUBARCH (tsxldtrk, TSXLDTRK, ANY_TSXLDTRK, false),
1146 SUBARCH (kl, KL, ANY_KL, false),
1147 SUBARCH (widekl, WIDEKL, ANY_WIDEKL, false),
1148 SUBARCH (uintr, UINTR, UINTR, false),
1149 SUBARCH (hreset, HRESET, HRESET, false),
1150 VECARCH (avx512_fp16, AVX512_FP16, ANY_AVX512_FP16, reset),
1151 SUBARCH (prefetchi, PREFETCHI, PREFETCHI, false),
1152 VECARCH (avx_ifma, AVX_IFMA, ANY_AVX_IFMA, reset),
1153 VECARCH (avx_vnni_int8, AVX_VNNI_INT8, ANY_AVX_VNNI_INT8, reset),
1154 SUBARCH (cmpccxadd, CMPCCXADD, CMPCCXADD, false),
1155 SUBARCH (wrmsrns, WRMSRNS, WRMSRNS, false),
1156 SUBARCH (msrlist, MSRLIST, MSRLIST, false),
1157 VECARCH (avx_ne_convert, AVX_NE_CONVERT, ANY_AVX_NE_CONVERT, reset),
1158 SUBARCH (rao_int, RAO_INT, RAO_INT, false),
1159 SUBARCH (rmpquery, RMPQUERY, ANY_RMPQUERY, false),
1160 SUBARCH (fred, FRED, ANY_FRED, false),
1161 SUBARCH (lkgs, LKGS, ANY_LKGS, false),
1162 VECARCH (avx_vnni_int16, AVX_VNNI_INT16, ANY_AVX_VNNI_INT16, reset),
1163 VECARCH (sha512, SHA512, ANY_SHA512, reset),
1164 VECARCH (sm3, SM3, ANY_SM3, reset),
1165 VECARCH (sm4, SM4, ANY_SM4, reset),
1166 SUBARCH (pbndkb, PBNDKB, PBNDKB, false),
1167 VECARCH (avx10.1, AVX10_1, ANY_AVX512F, set),
1168 };
1169
1170 #undef SUBARCH
1171 #undef ARCH
1172
1173 #ifdef I386COFF
1174 /* Like s_lcomm_internal in gas/read.c but the alignment string
1175 is allowed to be optional. */
1176
1177 static symbolS *
1178 pe_lcomm_internal (int needs_align, symbolS *symbolP, addressT size)
1179 {
1180 addressT align = 0;
1181
1182 SKIP_WHITESPACE ();
1183
1184 if (needs_align
1185 && *input_line_pointer == ',')
1186 {
1187 align = parse_align (needs_align - 1);
1188
1189 if (align == (addressT) -1)
1190 return NULL;
1191 }
1192 else
1193 {
1194 if (size >= 8)
1195 align = 3;
1196 else if (size >= 4)
1197 align = 2;
1198 else if (size >= 2)
1199 align = 1;
1200 else
1201 align = 0;
1202 }
1203
1204 bss_alloc (symbolP, size, align);
1205 return symbolP;
1206 }
1207
1208 static void
1209 pe_lcomm (int needs_align)
1210 {
1211 s_comm_internal (needs_align * 2, pe_lcomm_internal);
1212 }
1213 #endif
1214
1215 const pseudo_typeS md_pseudo_table[] =
1216 {
1217 #if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
1218 {"align", s_align_bytes, 0},
1219 #else
1220 {"align", s_align_ptwo, 0},
1221 #endif
1222 {"arch", set_cpu_arch, 0},
1223 #ifndef I386COFF
1224 {"bss", s_bss, 0},
1225 #else
1226 {"lcomm", pe_lcomm, 1},
1227 #endif
1228 {"ffloat", float_cons, 'f'},
1229 {"dfloat", float_cons, 'd'},
1230 {"tfloat", float_cons, 'x'},
1231 {"hfloat", float_cons, 'h'},
1232 {"bfloat16", float_cons, 'b'},
1233 {"value", cons, 2},
1234 {"slong", signed_cons, 4},
1235 {"insn", s_insn, 0},
1236 {"noopt", s_ignore, 0},
1237 {"optim", s_ignore, 0},
1238 {"code16gcc", set_16bit_gcc_code_flag, CODE_16BIT},
1239 {"code16", set_code_flag, CODE_16BIT},
1240 {"code32", set_code_flag, CODE_32BIT},
1241 #ifdef BFD64
1242 {"code64", set_code_flag, CODE_64BIT},
1243 #endif
1244 {"intel_syntax", set_intel_syntax, 1},
1245 {"att_syntax", set_intel_syntax, 0},
1246 {"intel_mnemonic", set_intel_mnemonic, 1},
1247 {"att_mnemonic", set_intel_mnemonic, 0},
1248 {"allow_index_reg", set_allow_index_reg, 1},
1249 {"disallow_index_reg", set_allow_index_reg, 0},
1250 {"sse_check", set_check, 0},
1251 {"operand_check", set_check, 1},
1252 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1253 {"largecomm", handle_large_common, 0},
1254 #else
1255 {"file", dwarf2_directive_file, 0},
1256 {"loc", dwarf2_directive_loc, 0},
1257 {"loc_mark_labels", dwarf2_directive_loc_mark_labels, 0},
1258 #endif
1259 #ifdef TE_PE
1260 {"secrel32", pe_directive_secrel, 0},
1261 {"secidx", pe_directive_secidx, 0},
1262 #endif
1263 {0, 0, 0}
1264 };
1265
1266 /* For interface with expression (). */
1267 extern char *input_line_pointer;
1268
1269 /* Hash table for instruction mnemonic lookup. */
1270 static htab_t op_hash;
1271
1272 /* Hash table for register lookup. */
1273 static htab_t reg_hash;
1274 \f
1275 /* Various efficient no-op patterns for aligning code labels.
1276 Note: Don't try to assemble the instructions in the comments.
1277 0L and 0w are not legal. */
1278 static const unsigned char f32_1[] =
1279 {0x90}; /* nop */
1280 static const unsigned char f32_2[] =
1281 {0x66,0x90}; /* xchg %ax,%ax */
1282 static const unsigned char f32_3[] =
1283 {0x8d,0x76,0x00}; /* leal 0(%esi),%esi */
1284 static const unsigned char f32_4[] =
1285 {0x8d,0x74,0x26,0x00}; /* leal 0(%esi,1),%esi */
1286 static const unsigned char f32_6[] =
1287 {0x8d,0xb6,0x00,0x00,0x00,0x00}; /* leal 0L(%esi),%esi */
1288 static const unsigned char f32_7[] =
1289 {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal 0L(%esi,1),%esi */
1290 static const unsigned char f16_3[] =
1291 {0x8d,0x74,0x00}; /* lea 0(%si),%si */
1292 static const unsigned char f16_4[] =
1293 {0x8d,0xb4,0x00,0x00}; /* lea 0W(%si),%si */
1294 static const unsigned char jump_disp8[] =
1295 {0xeb}; /* jmp disp8 */
1296 static const unsigned char jump32_disp32[] =
1297 {0xe9}; /* jmp disp32 */
1298 static const unsigned char jump16_disp32[] =
1299 {0x66,0xe9}; /* jmp disp32 */
1300 /* 32-bit NOPs patterns. */
1301 static const unsigned char *const f32_patt[] = {
1302 f32_1, f32_2, f32_3, f32_4, NULL, f32_6, f32_7
1303 };
1304 /* 16-bit NOPs patterns. */
1305 static const unsigned char *const f16_patt[] = {
1306 f32_1, f32_2, f16_3, f16_4
1307 };
1308 /* nopl (%[re]ax) */
1309 static const unsigned char alt_3[] =
1310 {0x0f,0x1f,0x00};
1311 /* nopl 0(%[re]ax) */
1312 static const unsigned char alt_4[] =
1313 {0x0f,0x1f,0x40,0x00};
1314 /* nopl 0(%[re]ax,%[re]ax,1) */
1315 static const unsigned char alt_5[] =
1316 {0x0f,0x1f,0x44,0x00,0x00};
1317 /* nopw 0(%[re]ax,%[re]ax,1) */
1318 static const unsigned char alt_6[] =
1319 {0x66,0x0f,0x1f,0x44,0x00,0x00};
1320 /* nopl 0L(%[re]ax) */
1321 static const unsigned char alt_7[] =
1322 {0x0f,0x1f,0x80,0x00,0x00,0x00,0x00};
1323 /* nopl 0L(%[re]ax,%[re]ax,1) */
1324 static const unsigned char alt_8[] =
1325 {0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1326 /* nopw 0L(%[re]ax,%[re]ax,1) */
1327 static const unsigned char alt_9[] =
1328 {0x66,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1329 /* nopw %cs:0L(%[re]ax,%[re]ax,1) */
1330 static const unsigned char alt_10[] =
1331 {0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1332 /* data16 nopw %cs:0L(%eax,%eax,1) */
1333 static const unsigned char alt_11[] =
1334 {0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1335 /* 32-bit and 64-bit NOPs patterns. */
1336 static const unsigned char *const alt_patt[] = {
1337 f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
1338 alt_9, alt_10, alt_11
1339 };
1340
1341 /* Genenerate COUNT bytes of NOPs to WHERE from PATT with the maximum
1342 size of a single NOP instruction MAX_SINGLE_NOP_SIZE. */
1343
1344 static void
1345 i386_output_nops (char *where, const unsigned char *const *patt,
1346 int count, int max_single_nop_size)
1347
1348 {
1349 /* Place the longer NOP first. */
1350 int last;
1351 int offset;
1352 const unsigned char *nops;
1353
1354 if (max_single_nop_size < 1)
1355 {
1356 as_fatal (_("i386_output_nops called to generate nops of at most %d bytes!"),
1357 max_single_nop_size);
1358 return;
1359 }
1360
1361 nops = patt[max_single_nop_size - 1];
1362
1363 /* Use the smaller one if the requsted one isn't available. */
1364 if (nops == NULL)
1365 {
1366 max_single_nop_size--;
1367 nops = patt[max_single_nop_size - 1];
1368 }
1369
1370 last = count % max_single_nop_size;
1371
1372 count -= last;
1373 for (offset = 0; offset < count; offset += max_single_nop_size)
1374 memcpy (where + offset, nops, max_single_nop_size);
1375
1376 if (last)
1377 {
1378 nops = patt[last - 1];
1379 if (nops == NULL)
1380 {
1381 /* Use the smaller one plus one-byte NOP if the needed one
1382 isn't available. */
1383 last--;
1384 nops = patt[last - 1];
1385 memcpy (where + offset, nops, last);
1386 where[offset + last] = *patt[0];
1387 }
1388 else
1389 memcpy (where + offset, nops, last);
1390 }
1391 }
1392
1393 static INLINE int
1394 fits_in_imm7 (offsetT num)
1395 {
1396 return (num & 0x7f) == num;
1397 }
1398
1399 static INLINE int
1400 fits_in_imm31 (offsetT num)
1401 {
1402 return (num & 0x7fffffff) == num;
1403 }
1404
1405 /* Genenerate COUNT bytes of NOPs to WHERE with the maximum size of a
1406 single NOP instruction LIMIT. */
1407
1408 void
1409 i386_generate_nops (fragS *fragP, char *where, offsetT count, int limit)
1410 {
1411 const unsigned char *const *patt = NULL;
1412 int max_single_nop_size;
1413 /* Maximum number of NOPs before switching to jump over NOPs. */
1414 int max_number_of_nops;
1415
1416 switch (fragP->fr_type)
1417 {
1418 case rs_fill_nop:
1419 case rs_align_code:
1420 break;
1421 case rs_machine_dependent:
1422 /* Allow NOP padding for jumps and calls. */
1423 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
1424 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
1425 break;
1426 /* Fall through. */
1427 default:
1428 return;
1429 }
1430
1431 /* We need to decide which NOP sequence to use for 32bit and
1432 64bit. When -mtune= is used:
1433
1434 1. For PROCESSOR_I386, PROCESSOR_I486, PROCESSOR_PENTIUM and
1435 PROCESSOR_GENERIC32, f32_patt will be used.
1436 2. For the rest, alt_patt will be used.
1437
1438 When -mtune= isn't used, alt_patt will be used if
1439 cpu_arch_isa_flags has CpuNop. Otherwise, f32_patt will
1440 be used.
1441
1442 When -march= or .arch is used, we can't use anything beyond
1443 cpu_arch_isa_flags. */
1444
1445 if (flag_code == CODE_16BIT)
1446 {
1447 patt = f16_patt;
1448 max_single_nop_size = sizeof (f16_patt) / sizeof (f16_patt[0]);
1449 /* Limit number of NOPs to 2 in 16-bit mode. */
1450 max_number_of_nops = 2;
1451 }
1452 else
1453 {
1454 if (fragP->tc_frag_data.isa == PROCESSOR_UNKNOWN)
1455 {
1456 /* PROCESSOR_UNKNOWN means that all ISAs may be used. */
1457 switch (cpu_arch_tune)
1458 {
1459 case PROCESSOR_UNKNOWN:
1460 /* We use cpu_arch_isa_flags to check if we SHOULD
1461 optimize with nops. */
1462 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
1463 patt = alt_patt;
1464 else
1465 patt = f32_patt;
1466 break;
1467 case PROCESSOR_PENTIUM4:
1468 case PROCESSOR_NOCONA:
1469 case PROCESSOR_CORE:
1470 case PROCESSOR_CORE2:
1471 case PROCESSOR_COREI7:
1472 case PROCESSOR_GENERIC64:
1473 case PROCESSOR_K6:
1474 case PROCESSOR_ATHLON:
1475 case PROCESSOR_K8:
1476 case PROCESSOR_AMDFAM10:
1477 case PROCESSOR_BD:
1478 case PROCESSOR_ZNVER:
1479 case PROCESSOR_BT:
1480 patt = alt_patt;
1481 break;
1482 case PROCESSOR_I386:
1483 case PROCESSOR_I486:
1484 case PROCESSOR_PENTIUM:
1485 case PROCESSOR_PENTIUMPRO:
1486 case PROCESSOR_IAMCU:
1487 case PROCESSOR_GENERIC32:
1488 patt = f32_patt;
1489 break;
1490 case PROCESSOR_NONE:
1491 abort ();
1492 }
1493 }
1494 else
1495 {
1496 switch (fragP->tc_frag_data.tune)
1497 {
1498 case PROCESSOR_UNKNOWN:
1499 /* When cpu_arch_isa is set, cpu_arch_tune shouldn't be
1500 PROCESSOR_UNKNOWN. */
1501 abort ();
1502 break;
1503
1504 case PROCESSOR_I386:
1505 case PROCESSOR_I486:
1506 case PROCESSOR_PENTIUM:
1507 case PROCESSOR_IAMCU:
1508 case PROCESSOR_K6:
1509 case PROCESSOR_ATHLON:
1510 case PROCESSOR_K8:
1511 case PROCESSOR_AMDFAM10:
1512 case PROCESSOR_BD:
1513 case PROCESSOR_ZNVER:
1514 case PROCESSOR_BT:
1515 case PROCESSOR_GENERIC32:
1516 /* We use cpu_arch_isa_flags to check if we CAN optimize
1517 with nops. */
1518 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
1519 patt = alt_patt;
1520 else
1521 patt = f32_patt;
1522 break;
1523 case PROCESSOR_PENTIUMPRO:
1524 case PROCESSOR_PENTIUM4:
1525 case PROCESSOR_NOCONA:
1526 case PROCESSOR_CORE:
1527 case PROCESSOR_CORE2:
1528 case PROCESSOR_COREI7:
1529 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
1530 patt = alt_patt;
1531 else
1532 patt = f32_patt;
1533 break;
1534 case PROCESSOR_GENERIC64:
1535 patt = alt_patt;
1536 break;
1537 case PROCESSOR_NONE:
1538 abort ();
1539 }
1540 }
1541
1542 if (patt == f32_patt)
1543 {
1544 max_single_nop_size = sizeof (f32_patt) / sizeof (f32_patt[0]);
1545 /* Limit number of NOPs to 2 for older processors. */
1546 max_number_of_nops = 2;
1547 }
1548 else
1549 {
1550 max_single_nop_size = sizeof (alt_patt) / sizeof (alt_patt[0]);
1551 /* Limit number of NOPs to 7 for newer processors. */
1552 max_number_of_nops = 7;
1553 }
1554 }
1555
1556 if (limit == 0)
1557 limit = max_single_nop_size;
1558
1559 if (fragP->fr_type == rs_fill_nop)
1560 {
1561 /* Output NOPs for .nop directive. */
1562 if (limit > max_single_nop_size)
1563 {
1564 as_bad_where (fragP->fr_file, fragP->fr_line,
1565 _("invalid single nop size: %d "
1566 "(expect within [0, %d])"),
1567 limit, max_single_nop_size);
1568 return;
1569 }
1570 }
1571 else if (fragP->fr_type != rs_machine_dependent)
1572 fragP->fr_var = count;
1573
1574 if ((count / max_single_nop_size) > max_number_of_nops)
1575 {
1576 /* Generate jump over NOPs. */
1577 offsetT disp = count - 2;
1578 if (fits_in_imm7 (disp))
1579 {
1580 /* Use "jmp disp8" if possible. */
1581 count = disp;
1582 where[0] = jump_disp8[0];
1583 where[1] = count;
1584 where += 2;
1585 }
1586 else
1587 {
1588 unsigned int size_of_jump;
1589
1590 if (flag_code == CODE_16BIT)
1591 {
1592 where[0] = jump16_disp32[0];
1593 where[1] = jump16_disp32[1];
1594 size_of_jump = 2;
1595 }
1596 else
1597 {
1598 where[0] = jump32_disp32[0];
1599 size_of_jump = 1;
1600 }
1601
1602 count -= size_of_jump + 4;
1603 if (!fits_in_imm31 (count))
1604 {
1605 as_bad_where (fragP->fr_file, fragP->fr_line,
1606 _("jump over nop padding out of range"));
1607 return;
1608 }
1609
1610 md_number_to_chars (where + size_of_jump, count, 4);
1611 where += size_of_jump + 4;
1612 }
1613 }
1614
1615 /* Generate multiple NOPs. */
1616 i386_output_nops (where, patt, count, limit);
1617 }
1618
1619 static INLINE int
1620 operand_type_all_zero (const union i386_operand_type *x)
1621 {
1622 switch (ARRAY_SIZE(x->array))
1623 {
1624 case 3:
1625 if (x->array[2])
1626 return 0;
1627 /* Fall through. */
1628 case 2:
1629 if (x->array[1])
1630 return 0;
1631 /* Fall through. */
1632 case 1:
1633 return !x->array[0];
1634 default:
1635 abort ();
1636 }
1637 }
1638
1639 static INLINE void
1640 operand_type_set (union i386_operand_type *x, unsigned int v)
1641 {
1642 switch (ARRAY_SIZE(x->array))
1643 {
1644 case 3:
1645 x->array[2] = v;
1646 /* Fall through. */
1647 case 2:
1648 x->array[1] = v;
1649 /* Fall through. */
1650 case 1:
1651 x->array[0] = v;
1652 /* Fall through. */
1653 break;
1654 default:
1655 abort ();
1656 }
1657
1658 x->bitfield.class = ClassNone;
1659 x->bitfield.instance = InstanceNone;
1660 }
1661
1662 static INLINE int
1663 operand_type_equal (const union i386_operand_type *x,
1664 const union i386_operand_type *y)
1665 {
1666 switch (ARRAY_SIZE(x->array))
1667 {
1668 case 3:
1669 if (x->array[2] != y->array[2])
1670 return 0;
1671 /* Fall through. */
1672 case 2:
1673 if (x->array[1] != y->array[1])
1674 return 0;
1675 /* Fall through. */
1676 case 1:
1677 return x->array[0] == y->array[0];
1678 break;
1679 default:
1680 abort ();
1681 }
1682 }
1683
1684 static INLINE bool
1685 is_cpu (const insn_template *t, enum i386_cpu cpu)
1686 {
1687 switch (cpu)
1688 {
1689 case Cpu287: return t->cpu.bitfield.cpu287;
1690 case Cpu387: return t->cpu.bitfield.cpu387;
1691 case Cpu3dnow: return t->cpu.bitfield.cpu3dnow;
1692 case Cpu3dnowA: return t->cpu.bitfield.cpu3dnowa;
1693 case CpuAVX: return t->cpu.bitfield.cpuavx;
1694 case CpuHLE: return t->cpu.bitfield.cpuhle;
1695 case CpuAVX512F: return t->cpu.bitfield.cpuavx512f;
1696 case CpuAVX512VL: return t->cpu.bitfield.cpuavx512vl;
1697 case Cpu64: return t->cpu.bitfield.cpu64;
1698 case CpuNo64: return t->cpu.bitfield.cpuno64;
1699 default:
1700 gas_assert (cpu < CpuAttrEnums);
1701 }
1702 return t->cpu.bitfield.isa == cpu + 1u;
1703 }
1704
1705 static i386_cpu_flags cpu_flags_from_attr (i386_cpu_attr a)
1706 {
1707 const unsigned int bps = sizeof (a.array[0]) * CHAR_BIT;
1708 i386_cpu_flags f = { .array[0] = 0 };
1709
1710 switch (ARRAY_SIZE(a.array))
1711 {
1712 case 1:
1713 f.array[CpuAttrEnums / bps]
1714 |= (a.array[0] >> CpuIsaBits) << (CpuAttrEnums % bps);
1715 if (CpuAttrEnums % bps > CpuIsaBits)
1716 f.array[CpuAttrEnums / bps + 1]
1717 = (a.array[0] >> CpuIsaBits) >> (bps - CpuAttrEnums % bps);
1718 break;
1719 default:
1720 abort ();
1721 }
1722
1723 if (a.bitfield.isa)
1724 f.array[(a.bitfield.isa - 1) / bps] |= 1u << ((a.bitfield.isa - 1) % bps);
1725
1726 return f;
1727 }
1728
1729 static INLINE int
1730 cpu_flags_all_zero (const union i386_cpu_flags *x)
1731 {
1732 switch (ARRAY_SIZE(x->array))
1733 {
1734 case 5:
1735 if (x->array[4])
1736 return 0;
1737 /* Fall through. */
1738 case 4:
1739 if (x->array[3])
1740 return 0;
1741 /* Fall through. */
1742 case 3:
1743 if (x->array[2])
1744 return 0;
1745 /* Fall through. */
1746 case 2:
1747 if (x->array[1])
1748 return 0;
1749 /* Fall through. */
1750 case 1:
1751 return !x->array[0];
1752 default:
1753 abort ();
1754 }
1755 }
1756
1757 static INLINE int
1758 cpu_flags_equal (const union i386_cpu_flags *x,
1759 const union i386_cpu_flags *y)
1760 {
1761 switch (ARRAY_SIZE(x->array))
1762 {
1763 case 5:
1764 if (x->array[4] != y->array[4])
1765 return 0;
1766 /* Fall through. */
1767 case 4:
1768 if (x->array[3] != y->array[3])
1769 return 0;
1770 /* Fall through. */
1771 case 3:
1772 if (x->array[2] != y->array[2])
1773 return 0;
1774 /* Fall through. */
1775 case 2:
1776 if (x->array[1] != y->array[1])
1777 return 0;
1778 /* Fall through. */
1779 case 1:
1780 return x->array[0] == y->array[0];
1781 break;
1782 default:
1783 abort ();
1784 }
1785 }
1786
1787 static INLINE int
1788 cpu_flags_check_cpu64 (const insn_template *t)
1789 {
1790 return flag_code == CODE_64BIT
1791 ? !t->cpu.bitfield.cpuno64
1792 : !t->cpu.bitfield.cpu64;
1793 }
1794
1795 static INLINE i386_cpu_flags
1796 cpu_flags_and (i386_cpu_flags x, i386_cpu_flags y)
1797 {
1798 switch (ARRAY_SIZE (x.array))
1799 {
1800 case 5:
1801 x.array [4] &= y.array [4];
1802 /* Fall through. */
1803 case 4:
1804 x.array [3] &= y.array [3];
1805 /* Fall through. */
1806 case 3:
1807 x.array [2] &= y.array [2];
1808 /* Fall through. */
1809 case 2:
1810 x.array [1] &= y.array [1];
1811 /* Fall through. */
1812 case 1:
1813 x.array [0] &= y.array [0];
1814 break;
1815 default:
1816 abort ();
1817 }
1818 return x;
1819 }
1820
1821 static INLINE i386_cpu_flags
1822 cpu_flags_or (i386_cpu_flags x, i386_cpu_flags y)
1823 {
1824 switch (ARRAY_SIZE (x.array))
1825 {
1826 case 5:
1827 x.array [4] |= y.array [4];
1828 /* Fall through. */
1829 case 4:
1830 x.array [3] |= y.array [3];
1831 /* Fall through. */
1832 case 3:
1833 x.array [2] |= y.array [2];
1834 /* Fall through. */
1835 case 2:
1836 x.array [1] |= y.array [1];
1837 /* Fall through. */
1838 case 1:
1839 x.array [0] |= y.array [0];
1840 break;
1841 default:
1842 abort ();
1843 }
1844 return x;
1845 }
1846
1847 static INLINE i386_cpu_flags
1848 cpu_flags_and_not (i386_cpu_flags x, i386_cpu_flags y)
1849 {
1850 switch (ARRAY_SIZE (x.array))
1851 {
1852 case 5:
1853 x.array [4] &= ~y.array [4];
1854 /* Fall through. */
1855 case 4:
1856 x.array [3] &= ~y.array [3];
1857 /* Fall through. */
1858 case 3:
1859 x.array [2] &= ~y.array [2];
1860 /* Fall through. */
1861 case 2:
1862 x.array [1] &= ~y.array [1];
1863 /* Fall through. */
1864 case 1:
1865 x.array [0] &= ~y.array [0];
1866 break;
1867 default:
1868 abort ();
1869 }
1870 return x;
1871 }
1872
1873 static const i386_cpu_flags avx512 = CPU_ANY_AVX512F_FLAGS;
1874
1875 #define CPU_FLAGS_ARCH_MATCH 0x1
1876 #define CPU_FLAGS_64BIT_MATCH 0x2
1877
1878 #define CPU_FLAGS_PERFECT_MATCH \
1879 (CPU_FLAGS_ARCH_MATCH | CPU_FLAGS_64BIT_MATCH)
1880
1881 /* Return CPU flags match bits. */
1882
1883 static int
1884 cpu_flags_match (const insn_template *t)
1885 {
1886 i386_cpu_flags x = cpu_flags_from_attr (t->cpu);
1887 int match = cpu_flags_check_cpu64 (t) ? CPU_FLAGS_64BIT_MATCH : 0;
1888
1889 x.bitfield.cpu64 = 0;
1890 x.bitfield.cpuno64 = 0;
1891
1892 if (cpu_flags_all_zero (&x))
1893 {
1894 /* This instruction is available on all archs. */
1895 match |= CPU_FLAGS_ARCH_MATCH;
1896 }
1897 else
1898 {
1899 /* This instruction is available only on some archs. */
1900 i386_cpu_flags cpu = cpu_arch_flags;
1901
1902 /* AVX512VL is no standalone feature - match it and then strip it. */
1903 if (x.bitfield.cpuavx512vl && !cpu.bitfield.cpuavx512vl)
1904 return match;
1905 x.bitfield.cpuavx512vl = 0;
1906
1907 /* AVX and AVX2 present at the same time express an operand size
1908 dependency - strip AVX2 for the purposes here. The operand size
1909 dependent check occurs in check_vecOperands(). */
1910 if (x.bitfield.cpuavx && x.bitfield.cpuavx2)
1911 x.bitfield.cpuavx2 = 0;
1912
1913 cpu = cpu_flags_and (x, cpu);
1914 if (!cpu_flags_all_zero (&cpu))
1915 {
1916 if (x.bitfield.cpuavx)
1917 {
1918 /* We need to check a few extra flags with AVX. */
1919 if (cpu.bitfield.cpuavx
1920 && (!t->opcode_modifier.sse2avx
1921 || (sse2avx && !i.prefix[DATA_PREFIX]))
1922 && (!x.bitfield.cpuaes || cpu.bitfield.cpuaes)
1923 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
1924 && (!x.bitfield.cpupclmulqdq || cpu.bitfield.cpupclmulqdq))
1925 match |= CPU_FLAGS_ARCH_MATCH;
1926 }
1927 else if (x.bitfield.cpuavx512f)
1928 {
1929 /* We need to check a few extra flags with AVX512F. */
1930 if (cpu.bitfield.cpuavx512f
1931 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
1932 && (!x.bitfield.cpuvaes || cpu.bitfield.cpuvaes)
1933 && (!x.bitfield.cpuvpclmulqdq || cpu.bitfield.cpuvpclmulqdq))
1934 match |= CPU_FLAGS_ARCH_MATCH;
1935 }
1936 else
1937 match |= CPU_FLAGS_ARCH_MATCH;
1938 }
1939 }
1940 return match;
1941 }
1942
1943 static INLINE i386_operand_type
1944 operand_type_and (i386_operand_type x, i386_operand_type y)
1945 {
1946 if (x.bitfield.class != y.bitfield.class)
1947 x.bitfield.class = ClassNone;
1948 if (x.bitfield.instance != y.bitfield.instance)
1949 x.bitfield.instance = InstanceNone;
1950
1951 switch (ARRAY_SIZE (x.array))
1952 {
1953 case 3:
1954 x.array [2] &= y.array [2];
1955 /* Fall through. */
1956 case 2:
1957 x.array [1] &= y.array [1];
1958 /* Fall through. */
1959 case 1:
1960 x.array [0] &= y.array [0];
1961 break;
1962 default:
1963 abort ();
1964 }
1965 return x;
1966 }
1967
1968 static INLINE i386_operand_type
1969 operand_type_and_not (i386_operand_type x, i386_operand_type y)
1970 {
1971 gas_assert (y.bitfield.class == ClassNone);
1972 gas_assert (y.bitfield.instance == InstanceNone);
1973
1974 switch (ARRAY_SIZE (x.array))
1975 {
1976 case 3:
1977 x.array [2] &= ~y.array [2];
1978 /* Fall through. */
1979 case 2:
1980 x.array [1] &= ~y.array [1];
1981 /* Fall through. */
1982 case 1:
1983 x.array [0] &= ~y.array [0];
1984 break;
1985 default:
1986 abort ();
1987 }
1988 return x;
1989 }
1990
1991 static INLINE i386_operand_type
1992 operand_type_or (i386_operand_type x, i386_operand_type y)
1993 {
1994 gas_assert (x.bitfield.class == ClassNone ||
1995 y.bitfield.class == ClassNone ||
1996 x.bitfield.class == y.bitfield.class);
1997 gas_assert (x.bitfield.instance == InstanceNone ||
1998 y.bitfield.instance == InstanceNone ||
1999 x.bitfield.instance == y.bitfield.instance);
2000
2001 switch (ARRAY_SIZE (x.array))
2002 {
2003 case 3:
2004 x.array [2] |= y.array [2];
2005 /* Fall through. */
2006 case 2:
2007 x.array [1] |= y.array [1];
2008 /* Fall through. */
2009 case 1:
2010 x.array [0] |= y.array [0];
2011 break;
2012 default:
2013 abort ();
2014 }
2015 return x;
2016 }
2017
2018 static INLINE i386_operand_type
2019 operand_type_xor (i386_operand_type x, i386_operand_type y)
2020 {
2021 gas_assert (y.bitfield.class == ClassNone);
2022 gas_assert (y.bitfield.instance == InstanceNone);
2023
2024 switch (ARRAY_SIZE (x.array))
2025 {
2026 case 3:
2027 x.array [2] ^= y.array [2];
2028 /* Fall through. */
2029 case 2:
2030 x.array [1] ^= y.array [1];
2031 /* Fall through. */
2032 case 1:
2033 x.array [0] ^= y.array [0];
2034 break;
2035 default:
2036 abort ();
2037 }
2038 return x;
2039 }
2040
2041 static const i386_operand_type anydisp = {
2042 .bitfield = { .disp8 = 1, .disp16 = 1, .disp32 = 1, .disp64 = 1 }
2043 };
2044
2045 enum operand_type
2046 {
2047 reg,
2048 imm,
2049 disp,
2050 anymem
2051 };
2052
2053 static INLINE int
2054 operand_type_check (i386_operand_type t, enum operand_type c)
2055 {
2056 switch (c)
2057 {
2058 case reg:
2059 return t.bitfield.class == Reg;
2060
2061 case imm:
2062 return (t.bitfield.imm8
2063 || t.bitfield.imm8s
2064 || t.bitfield.imm16
2065 || t.bitfield.imm32
2066 || t.bitfield.imm32s
2067 || t.bitfield.imm64);
2068
2069 case disp:
2070 return (t.bitfield.disp8
2071 || t.bitfield.disp16
2072 || t.bitfield.disp32
2073 || t.bitfield.disp64);
2074
2075 case anymem:
2076 return (t.bitfield.disp8
2077 || t.bitfield.disp16
2078 || t.bitfield.disp32
2079 || t.bitfield.disp64
2080 || t.bitfield.baseindex);
2081
2082 default:
2083 abort ();
2084 }
2085
2086 return 0;
2087 }
2088
2089 /* Return 1 if there is no conflict in 8bit/16bit/32bit/64bit/80bit size
2090 between operand GIVEN and opeand WANTED for instruction template T. */
2091
2092 static INLINE int
2093 match_operand_size (const insn_template *t, unsigned int wanted,
2094 unsigned int given)
2095 {
2096 return !((i.types[given].bitfield.byte
2097 && !t->operand_types[wanted].bitfield.byte)
2098 || (i.types[given].bitfield.word
2099 && !t->operand_types[wanted].bitfield.word)
2100 || (i.types[given].bitfield.dword
2101 && !t->operand_types[wanted].bitfield.dword)
2102 || (i.types[given].bitfield.qword
2103 && (!t->operand_types[wanted].bitfield.qword
2104 /* Don't allow 64-bit (memory) operands outside of 64-bit
2105 mode, when they're used where a 64-bit GPR could also
2106 be used. Checking is needed for Intel Syntax only. */
2107 || (intel_syntax
2108 && flag_code != CODE_64BIT
2109 && (t->operand_types[wanted].bitfield.class == Reg
2110 || t->operand_types[wanted].bitfield.class == Accum
2111 || t->opcode_modifier.isstring))))
2112 || (i.types[given].bitfield.tbyte
2113 && !t->operand_types[wanted].bitfield.tbyte));
2114 }
2115
2116 /* Return 1 if there is no conflict in SIMD register between operand
2117 GIVEN and opeand WANTED for instruction template T. */
2118
2119 static INLINE int
2120 match_simd_size (const insn_template *t, unsigned int wanted,
2121 unsigned int given)
2122 {
2123 return !((i.types[given].bitfield.xmmword
2124 && !t->operand_types[wanted].bitfield.xmmword)
2125 || (i.types[given].bitfield.ymmword
2126 && !t->operand_types[wanted].bitfield.ymmword)
2127 || (i.types[given].bitfield.zmmword
2128 && !t->operand_types[wanted].bitfield.zmmword)
2129 || (i.types[given].bitfield.tmmword
2130 && !t->operand_types[wanted].bitfield.tmmword));
2131 }
2132
2133 /* Return 1 if there is no conflict in any size between operand GIVEN
2134 and opeand WANTED for instruction template T. */
2135
2136 static INLINE int
2137 match_mem_size (const insn_template *t, unsigned int wanted,
2138 unsigned int given)
2139 {
2140 return (match_operand_size (t, wanted, given)
2141 && !((i.types[given].bitfield.unspecified
2142 && !i.broadcast.type
2143 && !i.broadcast.bytes
2144 && !t->operand_types[wanted].bitfield.unspecified)
2145 || (i.types[given].bitfield.fword
2146 && !t->operand_types[wanted].bitfield.fword)
2147 /* For scalar opcode templates to allow register and memory
2148 operands at the same time, some special casing is needed
2149 here. Also for v{,p}broadcast*, {,v}pmov{s,z}*, and
2150 down-conversion vpmov*. */
2151 || ((t->operand_types[wanted].bitfield.class == RegSIMD
2152 && t->operand_types[wanted].bitfield.byte
2153 + t->operand_types[wanted].bitfield.word
2154 + t->operand_types[wanted].bitfield.dword
2155 + t->operand_types[wanted].bitfield.qword
2156 > !!t->opcode_modifier.broadcast)
2157 ? (i.types[given].bitfield.xmmword
2158 || i.types[given].bitfield.ymmword
2159 || i.types[given].bitfield.zmmword)
2160 : !match_simd_size(t, wanted, given))));
2161 }
2162
2163 /* Return value has MATCH_STRAIGHT set if there is no size conflict on any
2164 operands for instruction template T, and it has MATCH_REVERSE set if there
2165 is no size conflict on any operands for the template with operands reversed
2166 (and the template allows for reversing in the first place). */
2167
2168 #define MATCH_STRAIGHT 1
2169 #define MATCH_REVERSE 2
2170
2171 static INLINE unsigned int
2172 operand_size_match (const insn_template *t)
2173 {
2174 unsigned int j, match = MATCH_STRAIGHT;
2175
2176 /* Don't check non-absolute jump instructions. */
2177 if (t->opcode_modifier.jump
2178 && t->opcode_modifier.jump != JUMP_ABSOLUTE)
2179 return match;
2180
2181 /* Check memory and accumulator operand size. */
2182 for (j = 0; j < i.operands; j++)
2183 {
2184 if (i.types[j].bitfield.class != Reg
2185 && i.types[j].bitfield.class != RegSIMD
2186 && t->opcode_modifier.operandconstraint == ANY_SIZE)
2187 continue;
2188
2189 if (t->operand_types[j].bitfield.class == Reg
2190 && !match_operand_size (t, j, j))
2191 {
2192 match = 0;
2193 break;
2194 }
2195
2196 if (t->operand_types[j].bitfield.class == RegSIMD
2197 && !match_simd_size (t, j, j))
2198 {
2199 match = 0;
2200 break;
2201 }
2202
2203 if (t->operand_types[j].bitfield.instance == Accum
2204 && (!match_operand_size (t, j, j) || !match_simd_size (t, j, j)))
2205 {
2206 match = 0;
2207 break;
2208 }
2209
2210 if ((i.flags[j] & Operand_Mem) && !match_mem_size (t, j, j))
2211 {
2212 match = 0;
2213 break;
2214 }
2215 }
2216
2217 if (!t->opcode_modifier.d)
2218 return match;
2219
2220 /* Check reverse. */
2221 gas_assert (i.operands >= 2);
2222
2223 for (j = 0; j < i.operands; j++)
2224 {
2225 unsigned int given = i.operands - j - 1;
2226
2227 /* For FMA4 and XOP insns VEX.W controls just the first two
2228 register operands. */
2229 if (is_cpu (t, CpuFMA4) || is_cpu (t, CpuXOP))
2230 given = j < 2 ? 1 - j : j;
2231
2232 if (t->operand_types[j].bitfield.class == Reg
2233 && !match_operand_size (t, j, given))
2234 return match;
2235
2236 if (t->operand_types[j].bitfield.class == RegSIMD
2237 && !match_simd_size (t, j, given))
2238 return match;
2239
2240 if (t->operand_types[j].bitfield.instance == Accum
2241 && (!match_operand_size (t, j, given)
2242 || !match_simd_size (t, j, given)))
2243 return match;
2244
2245 if ((i.flags[given] & Operand_Mem) && !match_mem_size (t, j, given))
2246 return match;
2247 }
2248
2249 return match | MATCH_REVERSE;
2250 }
2251
2252 static INLINE int
2253 operand_type_match (i386_operand_type overlap,
2254 i386_operand_type given)
2255 {
2256 i386_operand_type temp = overlap;
2257
2258 temp.bitfield.unspecified = 0;
2259 temp.bitfield.byte = 0;
2260 temp.bitfield.word = 0;
2261 temp.bitfield.dword = 0;
2262 temp.bitfield.fword = 0;
2263 temp.bitfield.qword = 0;
2264 temp.bitfield.tbyte = 0;
2265 temp.bitfield.xmmword = 0;
2266 temp.bitfield.ymmword = 0;
2267 temp.bitfield.zmmword = 0;
2268 temp.bitfield.tmmword = 0;
2269 if (operand_type_all_zero (&temp))
2270 goto mismatch;
2271
2272 if (given.bitfield.baseindex == overlap.bitfield.baseindex)
2273 return 1;
2274
2275 mismatch:
2276 i.error = operand_type_mismatch;
2277 return 0;
2278 }
2279
2280 /* If given types g0 and g1 are registers they must be of the same type
2281 unless the expected operand type register overlap is null.
2282 Intel syntax sized memory operands are also checked here. */
2283
2284 static INLINE int
2285 operand_type_register_match (i386_operand_type g0,
2286 i386_operand_type t0,
2287 i386_operand_type g1,
2288 i386_operand_type t1)
2289 {
2290 if (g0.bitfield.class != Reg
2291 && g0.bitfield.class != RegSIMD
2292 && (g0.bitfield.unspecified
2293 || !operand_type_check (g0, anymem)))
2294 return 1;
2295
2296 if (g1.bitfield.class != Reg
2297 && g1.bitfield.class != RegSIMD
2298 && (g1.bitfield.unspecified
2299 || !operand_type_check (g1, anymem)))
2300 return 1;
2301
2302 if (g0.bitfield.byte == g1.bitfield.byte
2303 && g0.bitfield.word == g1.bitfield.word
2304 && g0.bitfield.dword == g1.bitfield.dword
2305 && g0.bitfield.qword == g1.bitfield.qword
2306 && g0.bitfield.xmmword == g1.bitfield.xmmword
2307 && g0.bitfield.ymmword == g1.bitfield.ymmword
2308 && g0.bitfield.zmmword == g1.bitfield.zmmword)
2309 return 1;
2310
2311 /* If expectations overlap in no more than a single size, all is fine. */
2312 g0 = operand_type_and (t0, t1);
2313 if (g0.bitfield.byte
2314 + g0.bitfield.word
2315 + g0.bitfield.dword
2316 + g0.bitfield.qword
2317 + g0.bitfield.xmmword
2318 + g0.bitfield.ymmword
2319 + g0.bitfield.zmmword <= 1)
2320 return 1;
2321
2322 i.error = register_type_mismatch;
2323
2324 return 0;
2325 }
2326
2327 static INLINE unsigned int
2328 register_number (const reg_entry *r)
2329 {
2330 unsigned int nr = r->reg_num;
2331
2332 if (r->reg_flags & RegRex)
2333 nr += 8;
2334
2335 if (r->reg_flags & RegVRex)
2336 nr += 16;
2337
2338 return nr;
2339 }
2340
2341 static INLINE unsigned int
2342 mode_from_disp_size (i386_operand_type t)
2343 {
2344 if (t.bitfield.disp8)
2345 return 1;
2346 else if (t.bitfield.disp16
2347 || t.bitfield.disp32)
2348 return 2;
2349 else
2350 return 0;
2351 }
2352
2353 static INLINE int
2354 fits_in_signed_byte (addressT num)
2355 {
2356 return num + 0x80 <= 0xff;
2357 }
2358
2359 static INLINE int
2360 fits_in_unsigned_byte (addressT num)
2361 {
2362 return num <= 0xff;
2363 }
2364
2365 static INLINE int
2366 fits_in_unsigned_word (addressT num)
2367 {
2368 return num <= 0xffff;
2369 }
2370
2371 static INLINE int
2372 fits_in_signed_word (addressT num)
2373 {
2374 return num + 0x8000 <= 0xffff;
2375 }
2376
2377 static INLINE int
2378 fits_in_signed_long (addressT num ATTRIBUTE_UNUSED)
2379 {
2380 #ifndef BFD64
2381 return 1;
2382 #else
2383 return num + 0x80000000 <= 0xffffffff;
2384 #endif
2385 } /* fits_in_signed_long() */
2386
2387 static INLINE int
2388 fits_in_unsigned_long (addressT num ATTRIBUTE_UNUSED)
2389 {
2390 #ifndef BFD64
2391 return 1;
2392 #else
2393 return num <= 0xffffffff;
2394 #endif
2395 } /* fits_in_unsigned_long() */
2396
2397 static INLINE valueT extend_to_32bit_address (addressT num)
2398 {
2399 #ifdef BFD64
2400 if (fits_in_unsigned_long(num))
2401 return (num ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
2402
2403 if (!fits_in_signed_long (num))
2404 return num & 0xffffffff;
2405 #endif
2406
2407 return num;
2408 }
2409
2410 static INLINE int
2411 fits_in_disp8 (offsetT num)
2412 {
2413 int shift = i.memshift;
2414 unsigned int mask;
2415
2416 if (shift == -1)
2417 abort ();
2418
2419 mask = (1 << shift) - 1;
2420
2421 /* Return 0 if NUM isn't properly aligned. */
2422 if ((num & mask))
2423 return 0;
2424
2425 /* Check if NUM will fit in 8bit after shift. */
2426 return fits_in_signed_byte (num >> shift);
2427 }
2428
2429 static INLINE int
2430 fits_in_imm4 (offsetT num)
2431 {
2432 /* Despite the name, check for imm3 if we're dealing with EVEX. */
2433 return (num & (i.vec_encoding != vex_encoding_evex ? 0xf : 7)) == num;
2434 }
2435
2436 static i386_operand_type
2437 smallest_imm_type (offsetT num)
2438 {
2439 i386_operand_type t;
2440
2441 operand_type_set (&t, 0);
2442 t.bitfield.imm64 = 1;
2443
2444 if (cpu_arch_tune != PROCESSOR_I486 && num == 1)
2445 {
2446 /* This code is disabled on the 486 because all the Imm1 forms
2447 in the opcode table are slower on the i486. They're the
2448 versions with the implicitly specified single-position
2449 displacement, which has another syntax if you really want to
2450 use that form. */
2451 t.bitfield.imm1 = 1;
2452 t.bitfield.imm8 = 1;
2453 t.bitfield.imm8s = 1;
2454 t.bitfield.imm16 = 1;
2455 t.bitfield.imm32 = 1;
2456 t.bitfield.imm32s = 1;
2457 }
2458 else if (fits_in_signed_byte (num))
2459 {
2460 if (fits_in_unsigned_byte (num))
2461 t.bitfield.imm8 = 1;
2462 t.bitfield.imm8s = 1;
2463 t.bitfield.imm16 = 1;
2464 t.bitfield.imm32 = 1;
2465 t.bitfield.imm32s = 1;
2466 }
2467 else if (fits_in_unsigned_byte (num))
2468 {
2469 t.bitfield.imm8 = 1;
2470 t.bitfield.imm16 = 1;
2471 t.bitfield.imm32 = 1;
2472 t.bitfield.imm32s = 1;
2473 }
2474 else if (fits_in_signed_word (num) || fits_in_unsigned_word (num))
2475 {
2476 t.bitfield.imm16 = 1;
2477 t.bitfield.imm32 = 1;
2478 t.bitfield.imm32s = 1;
2479 }
2480 else if (fits_in_signed_long (num))
2481 {
2482 t.bitfield.imm32 = 1;
2483 t.bitfield.imm32s = 1;
2484 }
2485 else if (fits_in_unsigned_long (num))
2486 t.bitfield.imm32 = 1;
2487
2488 return t;
2489 }
2490
2491 static offsetT
2492 offset_in_range (offsetT val, int size)
2493 {
2494 addressT mask;
2495
2496 switch (size)
2497 {
2498 case 1: mask = ((addressT) 1 << 8) - 1; break;
2499 case 2: mask = ((addressT) 1 << 16) - 1; break;
2500 #ifdef BFD64
2501 case 4: mask = ((addressT) 1 << 32) - 1; break;
2502 #endif
2503 case sizeof (val): return val;
2504 default: abort ();
2505 }
2506
2507 if ((val & ~mask) != 0 && (-val & ~mask) != 0)
2508 as_warn (_("0x%" PRIx64 " shortened to 0x%" PRIx64),
2509 (uint64_t) val, (uint64_t) (val & mask));
2510
2511 return val & mask;
2512 }
2513
2514 static INLINE const char *insn_name (const insn_template *t)
2515 {
2516 return &i386_mnemonics[t->mnem_off];
2517 }
2518
2519 enum PREFIX_GROUP
2520 {
2521 PREFIX_EXIST = 0,
2522 PREFIX_LOCK,
2523 PREFIX_REP,
2524 PREFIX_DS,
2525 PREFIX_OTHER
2526 };
2527
2528 /* Returns
2529 a. PREFIX_EXIST if attempting to add a prefix where one from the
2530 same class already exists.
2531 b. PREFIX_LOCK if lock prefix is added.
2532 c. PREFIX_REP if rep/repne prefix is added.
2533 d. PREFIX_DS if ds prefix is added.
2534 e. PREFIX_OTHER if other prefix is added.
2535 */
2536
2537 static enum PREFIX_GROUP
2538 add_prefix (unsigned int prefix)
2539 {
2540 enum PREFIX_GROUP ret = PREFIX_OTHER;
2541 unsigned int q;
2542
2543 if (prefix >= REX_OPCODE && prefix < REX_OPCODE + 16
2544 && flag_code == CODE_64BIT)
2545 {
2546 if ((i.prefix[REX_PREFIX] & prefix & REX_W)
2547 || (i.prefix[REX_PREFIX] & prefix & REX_R)
2548 || (i.prefix[REX_PREFIX] & prefix & REX_X)
2549 || (i.prefix[REX_PREFIX] & prefix & REX_B))
2550 ret = PREFIX_EXIST;
2551 q = REX_PREFIX;
2552 }
2553 else
2554 {
2555 switch (prefix)
2556 {
2557 default:
2558 abort ();
2559
2560 case DS_PREFIX_OPCODE:
2561 ret = PREFIX_DS;
2562 /* Fall through. */
2563 case CS_PREFIX_OPCODE:
2564 case ES_PREFIX_OPCODE:
2565 case FS_PREFIX_OPCODE:
2566 case GS_PREFIX_OPCODE:
2567 case SS_PREFIX_OPCODE:
2568 q = SEG_PREFIX;
2569 break;
2570
2571 case REPNE_PREFIX_OPCODE:
2572 case REPE_PREFIX_OPCODE:
2573 q = REP_PREFIX;
2574 ret = PREFIX_REP;
2575 break;
2576
2577 case LOCK_PREFIX_OPCODE:
2578 q = LOCK_PREFIX;
2579 ret = PREFIX_LOCK;
2580 break;
2581
2582 case FWAIT_OPCODE:
2583 q = WAIT_PREFIX;
2584 break;
2585
2586 case ADDR_PREFIX_OPCODE:
2587 q = ADDR_PREFIX;
2588 break;
2589
2590 case DATA_PREFIX_OPCODE:
2591 q = DATA_PREFIX;
2592 break;
2593 }
2594 if (i.prefix[q] != 0)
2595 ret = PREFIX_EXIST;
2596 }
2597
2598 if (ret)
2599 {
2600 if (!i.prefix[q])
2601 ++i.prefixes;
2602 i.prefix[q] |= prefix;
2603 }
2604 else
2605 as_bad (_("same type of prefix used twice"));
2606
2607 return ret;
2608 }
2609
2610 static void
2611 update_code_flag (int value, int check)
2612 {
2613 PRINTF_LIKE ((*as_error)) = check ? as_fatal : as_bad;
2614
2615 if (value == CODE_64BIT && !cpu_arch_flags.bitfield.cpu64 )
2616 {
2617 as_error (_("64bit mode not supported on `%s'."),
2618 cpu_arch_name ? cpu_arch_name : default_arch);
2619 return;
2620 }
2621
2622 if (value == CODE_32BIT && !cpu_arch_flags.bitfield.cpui386)
2623 {
2624 as_error (_("32bit mode not supported on `%s'."),
2625 cpu_arch_name ? cpu_arch_name : default_arch);
2626 return;
2627 }
2628
2629 flag_code = (enum flag_code) value;
2630
2631 stackop_size = '\0';
2632 }
2633
2634 static void
2635 set_code_flag (int value)
2636 {
2637 update_code_flag (value, 0);
2638 }
2639
2640 static void
2641 set_16bit_gcc_code_flag (int new_code_flag)
2642 {
2643 flag_code = (enum flag_code) new_code_flag;
2644 if (flag_code != CODE_16BIT)
2645 abort ();
2646 stackop_size = LONG_MNEM_SUFFIX;
2647 }
2648
2649 static void
2650 set_intel_syntax (int syntax_flag)
2651 {
2652 /* Find out if register prefixing is specified. */
2653 int ask_naked_reg = 0;
2654
2655 SKIP_WHITESPACE ();
2656 if (!is_end_of_line[(unsigned char) *input_line_pointer])
2657 {
2658 char *string;
2659 int e = get_symbol_name (&string);
2660
2661 if (strcmp (string, "prefix") == 0)
2662 ask_naked_reg = 1;
2663 else if (strcmp (string, "noprefix") == 0)
2664 ask_naked_reg = -1;
2665 else
2666 as_bad (_("bad argument to syntax directive."));
2667 (void) restore_line_pointer (e);
2668 }
2669 demand_empty_rest_of_line ();
2670
2671 intel_syntax = syntax_flag;
2672
2673 if (ask_naked_reg == 0)
2674 allow_naked_reg = (intel_syntax
2675 && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
2676 else
2677 allow_naked_reg = (ask_naked_reg < 0);
2678
2679 expr_set_rank (O_full_ptr, syntax_flag ? 10 : 0);
2680
2681 register_prefix = allow_naked_reg ? "" : "%";
2682 }
2683
2684 static void
2685 set_intel_mnemonic (int mnemonic_flag)
2686 {
2687 intel_mnemonic = mnemonic_flag;
2688 }
2689
2690 static void
2691 set_allow_index_reg (int flag)
2692 {
2693 allow_index_reg = flag;
2694 }
2695
2696 static void
2697 set_check (int what)
2698 {
2699 enum check_kind *kind;
2700 const char *str;
2701
2702 if (what)
2703 {
2704 kind = &operand_check;
2705 str = "operand";
2706 }
2707 else
2708 {
2709 kind = &sse_check;
2710 str = "sse";
2711 }
2712
2713 SKIP_WHITESPACE ();
2714
2715 if (!is_end_of_line[(unsigned char) *input_line_pointer])
2716 {
2717 char *string;
2718 int e = get_symbol_name (&string);
2719
2720 if (strcmp (string, "none") == 0)
2721 *kind = check_none;
2722 else if (strcmp (string, "warning") == 0)
2723 *kind = check_warning;
2724 else if (strcmp (string, "error") == 0)
2725 *kind = check_error;
2726 else
2727 as_bad (_("bad argument to %s_check directive."), str);
2728 (void) restore_line_pointer (e);
2729 }
2730 else
2731 as_bad (_("missing argument for %s_check directive"), str);
2732
2733 demand_empty_rest_of_line ();
2734 }
2735
2736 static void
2737 check_cpu_arch_compatible (const char *name ATTRIBUTE_UNUSED,
2738 i386_cpu_flags new_flag ATTRIBUTE_UNUSED)
2739 {
2740 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2741 static const char *arch;
2742
2743 /* Intel MCU is only supported on ELF. */
2744 if (!IS_ELF)
2745 return;
2746
2747 if (!arch)
2748 {
2749 /* Use cpu_arch_name if it is set in md_parse_option. Otherwise
2750 use default_arch. */
2751 arch = cpu_arch_name;
2752 if (!arch)
2753 arch = default_arch;
2754 }
2755
2756 /* If we are targeting Intel MCU, we must enable it. */
2757 if ((get_elf_backend_data (stdoutput)->elf_machine_code == EM_IAMCU)
2758 == new_flag.bitfield.cpuiamcu)
2759 return;
2760
2761 as_bad (_("`%s' is not supported on `%s'"), name, arch);
2762 #endif
2763 }
2764
2765 static void
2766 extend_cpu_sub_arch_name (const char *name)
2767 {
2768 if (cpu_sub_arch_name)
2769 cpu_sub_arch_name = reconcat (cpu_sub_arch_name, cpu_sub_arch_name,
2770 ".", name, (const char *) NULL);
2771 else
2772 cpu_sub_arch_name = concat (".", name, (const char *) NULL);
2773 }
2774
2775 static void
2776 set_cpu_arch (int dummy ATTRIBUTE_UNUSED)
2777 {
2778 typedef struct arch_stack_entry
2779 {
2780 const struct arch_stack_entry *prev;
2781 const char *name;
2782 char *sub_name;
2783 i386_cpu_flags flags;
2784 i386_cpu_flags isa_flags;
2785 enum processor_type isa;
2786 enum flag_code flag_code;
2787 unsigned int vector_size;
2788 char stackop_size;
2789 bool no_cond_jump_promotion;
2790 } arch_stack_entry;
2791 static const arch_stack_entry *arch_stack_top;
2792 char *s;
2793 int e;
2794 const char *string;
2795 unsigned int j = 0;
2796 i386_cpu_flags flags;
2797
2798 SKIP_WHITESPACE ();
2799
2800 if (is_end_of_line[(unsigned char) *input_line_pointer])
2801 {
2802 as_bad (_("missing cpu architecture"));
2803 input_line_pointer++;
2804 return;
2805 }
2806
2807 e = get_symbol_name (&s);
2808 string = s;
2809
2810 if (strcmp (string, "push") == 0)
2811 {
2812 arch_stack_entry *top = XNEW (arch_stack_entry);
2813
2814 top->name = cpu_arch_name;
2815 if (cpu_sub_arch_name)
2816 top->sub_name = xstrdup (cpu_sub_arch_name);
2817 else
2818 top->sub_name = NULL;
2819 top->flags = cpu_arch_flags;
2820 top->isa = cpu_arch_isa;
2821 top->isa_flags = cpu_arch_isa_flags;
2822 top->flag_code = flag_code;
2823 top->vector_size = vector_size;
2824 top->stackop_size = stackop_size;
2825 top->no_cond_jump_promotion = no_cond_jump_promotion;
2826
2827 top->prev = arch_stack_top;
2828 arch_stack_top = top;
2829
2830 (void) restore_line_pointer (e);
2831 demand_empty_rest_of_line ();
2832 return;
2833 }
2834
2835 if (strcmp (string, "pop") == 0)
2836 {
2837 const arch_stack_entry *top = arch_stack_top;
2838
2839 if (!top)
2840 as_bad (_(".arch stack is empty"));
2841 else if (top->flag_code != flag_code
2842 || top->stackop_size != stackop_size)
2843 {
2844 static const unsigned int bits[] = {
2845 [CODE_16BIT] = 16,
2846 [CODE_32BIT] = 32,
2847 [CODE_64BIT] = 64,
2848 };
2849
2850 as_bad (_("this `.arch pop' requires `.code%u%s' to be in effect"),
2851 bits[top->flag_code],
2852 top->stackop_size == LONG_MNEM_SUFFIX ? "gcc" : "");
2853 }
2854 else
2855 {
2856 arch_stack_top = top->prev;
2857
2858 cpu_arch_name = top->name;
2859 free (cpu_sub_arch_name);
2860 cpu_sub_arch_name = top->sub_name;
2861 cpu_arch_flags = top->flags;
2862 cpu_arch_isa = top->isa;
2863 cpu_arch_isa_flags = top->isa_flags;
2864 vector_size = top->vector_size;
2865 no_cond_jump_promotion = top->no_cond_jump_promotion;
2866
2867 XDELETE (top);
2868 }
2869
2870 (void) restore_line_pointer (e);
2871 demand_empty_rest_of_line ();
2872 return;
2873 }
2874
2875 if (strcmp (string, "default") == 0)
2876 {
2877 if (strcmp (default_arch, "iamcu") == 0)
2878 string = default_arch;
2879 else
2880 {
2881 static const i386_cpu_flags cpu_unknown_flags = CPU_UNKNOWN_FLAGS;
2882
2883 cpu_arch_name = NULL;
2884 free (cpu_sub_arch_name);
2885 cpu_sub_arch_name = NULL;
2886 cpu_arch_flags = cpu_unknown_flags;
2887 cpu_arch_isa = PROCESSOR_UNKNOWN;
2888 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
2889 if (!cpu_arch_tune_set)
2890 {
2891 cpu_arch_tune = cpu_arch_isa;
2892 cpu_arch_tune_flags = cpu_arch_isa_flags;
2893 }
2894
2895 vector_size = VSZ_DEFAULT;
2896
2897 j = ARRAY_SIZE (cpu_arch) + 1;
2898 }
2899 }
2900
2901 for (; j < ARRAY_SIZE (cpu_arch); j++)
2902 {
2903 if (strcmp (string + (*string == '.'), cpu_arch[j].name) == 0
2904 && (*string == '.') == (cpu_arch[j].type == PROCESSOR_NONE))
2905 {
2906 if (*string != '.')
2907 {
2908 check_cpu_arch_compatible (string, cpu_arch[j].enable);
2909
2910 if (flag_code == CODE_64BIT && !cpu_arch[j].enable.bitfield.cpu64 )
2911 {
2912 as_bad (_("64bit mode not supported on `%s'."),
2913 cpu_arch[j].name);
2914 (void) restore_line_pointer (e);
2915 ignore_rest_of_line ();
2916 return;
2917 }
2918
2919 if (flag_code == CODE_32BIT && !cpu_arch[j].enable.bitfield.cpui386)
2920 {
2921 as_bad (_("32bit mode not supported on `%s'."),
2922 cpu_arch[j].name);
2923 (void) restore_line_pointer (e);
2924 ignore_rest_of_line ();
2925 return;
2926 }
2927
2928 cpu_arch_name = cpu_arch[j].name;
2929 free (cpu_sub_arch_name);
2930 cpu_sub_arch_name = NULL;
2931 cpu_arch_flags = cpu_arch[j].enable;
2932 cpu_arch_isa = cpu_arch[j].type;
2933 cpu_arch_isa_flags = cpu_arch[j].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 pre_386_16bit_warned = false;
2943 break;
2944 }
2945
2946 if (cpu_flags_all_zero (&cpu_arch[j].enable))
2947 continue;
2948
2949 flags = cpu_flags_or (cpu_arch_flags, cpu_arch[j].enable);
2950
2951 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
2952 {
2953 extend_cpu_sub_arch_name (string + 1);
2954 cpu_arch_flags = flags;
2955 cpu_arch_isa_flags = flags;
2956 }
2957 else
2958 cpu_arch_isa_flags
2959 = cpu_flags_or (cpu_arch_isa_flags, cpu_arch[j].enable);
2960
2961 (void) restore_line_pointer (e);
2962
2963 switch (cpu_arch[j].vsz)
2964 {
2965 default:
2966 break;
2967
2968 case vsz_set:
2969 #ifdef SVR4_COMMENT_CHARS
2970 if (*input_line_pointer == ':' || *input_line_pointer == '/')
2971 #else
2972 if (*input_line_pointer == '/')
2973 #endif
2974 {
2975 ++input_line_pointer;
2976 switch (get_absolute_expression ())
2977 {
2978 case 512: vector_size = VSZ512; break;
2979 case 256: vector_size = VSZ256; break;
2980 case 128: vector_size = VSZ128; break;
2981 default:
2982 as_bad (_("Unrecognized vector size specifier"));
2983 ignore_rest_of_line ();
2984 return;
2985 }
2986 break;
2987 }
2988 /* Fall through. */
2989 case vsz_reset:
2990 vector_size = VSZ_DEFAULT;
2991 break;
2992 }
2993
2994 demand_empty_rest_of_line ();
2995 return;
2996 }
2997 }
2998
2999 if (startswith (string, ".no") && j >= ARRAY_SIZE (cpu_arch))
3000 {
3001 /* Disable an ISA extension. */
3002 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
3003 if (cpu_arch[j].type == PROCESSOR_NONE
3004 && strcmp (string + 3, cpu_arch[j].name) == 0)
3005 {
3006 flags = cpu_flags_and_not (cpu_arch_flags, cpu_arch[j].disable);
3007 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
3008 {
3009 extend_cpu_sub_arch_name (string + 1);
3010 cpu_arch_flags = flags;
3011 cpu_arch_isa_flags = flags;
3012 }
3013
3014 if (cpu_arch[j].vsz == vsz_set)
3015 vector_size = VSZ_DEFAULT;
3016
3017 (void) restore_line_pointer (e);
3018 demand_empty_rest_of_line ();
3019 return;
3020 }
3021 }
3022
3023 if (j == ARRAY_SIZE (cpu_arch))
3024 as_bad (_("no such architecture: `%s'"), string);
3025
3026 *input_line_pointer = e;
3027
3028 no_cond_jump_promotion = 0;
3029 if (*input_line_pointer == ','
3030 && !is_end_of_line[(unsigned char) input_line_pointer[1]])
3031 {
3032 ++input_line_pointer;
3033 e = get_symbol_name (&s);
3034 string = s;
3035
3036 if (strcmp (string, "nojumps") == 0)
3037 no_cond_jump_promotion = 1;
3038 else if (strcmp (string, "jumps") == 0)
3039 ;
3040 else
3041 as_bad (_("no such architecture modifier: `%s'"), string);
3042
3043 (void) restore_line_pointer (e);
3044 }
3045
3046 demand_empty_rest_of_line ();
3047 }
3048
3049 enum bfd_architecture
3050 i386_arch (void)
3051 {
3052 if (cpu_arch_isa == PROCESSOR_IAMCU)
3053 {
3054 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
3055 || flag_code == CODE_64BIT)
3056 as_fatal (_("Intel MCU is 32bit ELF only"));
3057 return bfd_arch_iamcu;
3058 }
3059 else
3060 return bfd_arch_i386;
3061 }
3062
3063 unsigned long
3064 i386_mach (void)
3065 {
3066 if (startswith (default_arch, "x86_64"))
3067 {
3068 if (default_arch[6] == '\0')
3069 return bfd_mach_x86_64;
3070 else
3071 return bfd_mach_x64_32;
3072 }
3073 else if (!strcmp (default_arch, "i386")
3074 || !strcmp (default_arch, "iamcu"))
3075 {
3076 if (cpu_arch_isa == PROCESSOR_IAMCU)
3077 {
3078 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
3079 as_fatal (_("Intel MCU is 32bit ELF only"));
3080 return bfd_mach_i386_iamcu;
3081 }
3082 else
3083 return bfd_mach_i386_i386;
3084 }
3085 else
3086 as_fatal (_("unknown architecture"));
3087 }
3088 \f
3089 #include "opcodes/i386-tbl.h"
3090
3091 void
3092 md_begin (void)
3093 {
3094 /* Support pseudo prefixes like {disp32}. */
3095 lex_type ['{'] = LEX_BEGIN_NAME;
3096
3097 /* Initialize op_hash hash table. */
3098 op_hash = str_htab_create ();
3099
3100 {
3101 const insn_template *const *sets = i386_op_sets;
3102 const insn_template *const *end = sets + ARRAY_SIZE (i386_op_sets) - 1;
3103
3104 /* Type checks to compensate for the conversion through void * which
3105 occurs during hash table insertion / lookup. */
3106 (void) sizeof (sets == &current_templates->start);
3107 (void) sizeof (end == &current_templates->end);
3108 for (; sets < end; ++sets)
3109 if (str_hash_insert (op_hash, insn_name (*sets), sets, 0))
3110 as_fatal (_("duplicate %s"), insn_name (*sets));
3111 }
3112
3113 /* Initialize reg_hash hash table. */
3114 reg_hash = str_htab_create ();
3115 {
3116 const reg_entry *regtab;
3117 unsigned int regtab_size = i386_regtab_size;
3118
3119 for (regtab = i386_regtab; regtab_size--; regtab++)
3120 {
3121 switch (regtab->reg_type.bitfield.class)
3122 {
3123 case Reg:
3124 if (regtab->reg_type.bitfield.dword)
3125 {
3126 if (regtab->reg_type.bitfield.instance == Accum)
3127 reg_eax = regtab;
3128 }
3129 else if (regtab->reg_type.bitfield.tbyte)
3130 {
3131 /* There's no point inserting st(<N>) in the hash table, as
3132 parentheses aren't included in register_chars[] anyway. */
3133 if (regtab->reg_type.bitfield.instance != Accum)
3134 continue;
3135 reg_st0 = regtab;
3136 }
3137 break;
3138
3139 case SReg:
3140 switch (regtab->reg_num)
3141 {
3142 case 0: reg_es = regtab; break;
3143 case 2: reg_ss = regtab; break;
3144 case 3: reg_ds = regtab; break;
3145 }
3146 break;
3147
3148 case RegMask:
3149 if (!regtab->reg_num)
3150 reg_k0 = regtab;
3151 break;
3152 }
3153
3154 if (str_hash_insert (reg_hash, regtab->reg_name, regtab, 0) != NULL)
3155 as_fatal (_("duplicate %s"), regtab->reg_name);
3156 }
3157 }
3158
3159 /* Fill in lexical tables: mnemonic_chars, operand_chars. */
3160 {
3161 int c;
3162 const char *p;
3163
3164 for (c = 0; c < 256; c++)
3165 {
3166 if (ISDIGIT (c) || ISLOWER (c))
3167 {
3168 mnemonic_chars[c] = c;
3169 register_chars[c] = c;
3170 operand_chars[c] = c;
3171 }
3172 else if (ISUPPER (c))
3173 {
3174 mnemonic_chars[c] = TOLOWER (c);
3175 register_chars[c] = mnemonic_chars[c];
3176 operand_chars[c] = c;
3177 }
3178 #ifdef SVR4_COMMENT_CHARS
3179 else if (c == '\\' && strchr (i386_comment_chars, '/'))
3180 operand_chars[c] = c;
3181 #endif
3182
3183 if (c >= 128)
3184 operand_chars[c] = c;
3185 }
3186
3187 mnemonic_chars['_'] = '_';
3188 mnemonic_chars['-'] = '-';
3189 mnemonic_chars['.'] = '.';
3190
3191 for (p = extra_symbol_chars; *p != '\0'; p++)
3192 operand_chars[(unsigned char) *p] = *p;
3193 for (p = operand_special_chars; *p != '\0'; p++)
3194 operand_chars[(unsigned char) *p] = *p;
3195 }
3196
3197 if (flag_code == CODE_64BIT)
3198 {
3199 #if defined (OBJ_COFF) && defined (TE_PE)
3200 x86_dwarf2_return_column = (OUTPUT_FLAVOR == bfd_target_coff_flavour
3201 ? 32 : 16);
3202 #else
3203 x86_dwarf2_return_column = 16;
3204 #endif
3205 x86_cie_data_alignment = -8;
3206 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3207 x86_sframe_cfa_sp_reg = 7;
3208 x86_sframe_cfa_fp_reg = 6;
3209 #endif
3210 }
3211 else
3212 {
3213 x86_dwarf2_return_column = 8;
3214 x86_cie_data_alignment = -4;
3215 }
3216
3217 /* NB: FUSED_JCC_PADDING frag must have sufficient room so that it
3218 can be turned into BRANCH_PREFIX frag. */
3219 if (align_branch_prefix_size > MAX_FUSED_JCC_PADDING_SIZE)
3220 abort ();
3221 }
3222
3223 void
3224 i386_print_statistics (FILE *file)
3225 {
3226 htab_print_statistics (file, "i386 opcode", op_hash);
3227 htab_print_statistics (file, "i386 register", reg_hash);
3228 }
3229
3230 void
3231 i386_md_end (void)
3232 {
3233 htab_delete (op_hash);
3234 htab_delete (reg_hash);
3235 }
3236 \f
3237 #ifdef DEBUG386
3238
3239 /* Debugging routines for md_assemble. */
3240 static void pte (insn_template *);
3241 static void pt (i386_operand_type);
3242 static void pe (expressionS *);
3243 static void ps (symbolS *);
3244
3245 static void
3246 pi (const char *line, i386_insn *x)
3247 {
3248 unsigned int j;
3249
3250 fprintf (stdout, "%s: template ", line);
3251 pte (&x->tm);
3252 fprintf (stdout, " address: base %s index %s scale %x\n",
3253 x->base_reg ? x->base_reg->reg_name : "none",
3254 x->index_reg ? x->index_reg->reg_name : "none",
3255 x->log2_scale_factor);
3256 fprintf (stdout, " modrm: mode %x reg %x reg/mem %x\n",
3257 x->rm.mode, x->rm.reg, x->rm.regmem);
3258 fprintf (stdout, " sib: base %x index %x scale %x\n",
3259 x->sib.base, x->sib.index, x->sib.scale);
3260 fprintf (stdout, " rex: 64bit %x extX %x extY %x extZ %x\n",
3261 (x->rex & REX_W) != 0,
3262 (x->rex & REX_R) != 0,
3263 (x->rex & REX_X) != 0,
3264 (x->rex & REX_B) != 0);
3265 for (j = 0; j < x->operands; j++)
3266 {
3267 fprintf (stdout, " #%d: ", j + 1);
3268 pt (x->types[j]);
3269 fprintf (stdout, "\n");
3270 if (x->types[j].bitfield.class == Reg
3271 || x->types[j].bitfield.class == RegMMX
3272 || x->types[j].bitfield.class == RegSIMD
3273 || x->types[j].bitfield.class == RegMask
3274 || x->types[j].bitfield.class == SReg
3275 || x->types[j].bitfield.class == RegCR
3276 || x->types[j].bitfield.class == RegDR
3277 || x->types[j].bitfield.class == RegTR
3278 || x->types[j].bitfield.class == RegBND)
3279 fprintf (stdout, "%s\n", x->op[j].regs->reg_name);
3280 if (operand_type_check (x->types[j], imm))
3281 pe (x->op[j].imms);
3282 if (operand_type_check (x->types[j], disp))
3283 pe (x->op[j].disps);
3284 }
3285 }
3286
3287 static void
3288 pte (insn_template *t)
3289 {
3290 static const unsigned char opc_pfx[] = { 0, 0x66, 0xf3, 0xf2 };
3291 static const char *const opc_spc[] = {
3292 NULL, "0f", "0f38", "0f3a", NULL, "evexmap5", "evexmap6", NULL,
3293 "XOP08", "XOP09", "XOP0A",
3294 };
3295 unsigned int j;
3296
3297 fprintf (stdout, " %d operands ", t->operands);
3298 if (opc_pfx[t->opcode_modifier.opcodeprefix])
3299 fprintf (stdout, "pfx %x ", opc_pfx[t->opcode_modifier.opcodeprefix]);
3300 if (opc_spc[t->opcode_space])
3301 fprintf (stdout, "space %s ", opc_spc[t->opcode_space]);
3302 fprintf (stdout, "opcode %x ", t->base_opcode);
3303 if (t->extension_opcode != None)
3304 fprintf (stdout, "ext %x ", t->extension_opcode);
3305 if (t->opcode_modifier.d)
3306 fprintf (stdout, "D");
3307 if (t->opcode_modifier.w)
3308 fprintf (stdout, "W");
3309 fprintf (stdout, "\n");
3310 for (j = 0; j < t->operands; j++)
3311 {
3312 fprintf (stdout, " #%d type ", j + 1);
3313 pt (t->operand_types[j]);
3314 fprintf (stdout, "\n");
3315 }
3316 }
3317
3318 static void
3319 pe (expressionS *e)
3320 {
3321 fprintf (stdout, " operation %d\n", e->X_op);
3322 fprintf (stdout, " add_number %" PRId64 " (%" PRIx64 ")\n",
3323 (int64_t) e->X_add_number, (uint64_t) (valueT) e->X_add_number);
3324 if (e->X_add_symbol)
3325 {
3326 fprintf (stdout, " add_symbol ");
3327 ps (e->X_add_symbol);
3328 fprintf (stdout, "\n");
3329 }
3330 if (e->X_op_symbol)
3331 {
3332 fprintf (stdout, " op_symbol ");
3333 ps (e->X_op_symbol);
3334 fprintf (stdout, "\n");
3335 }
3336 }
3337
3338 static void
3339 ps (symbolS *s)
3340 {
3341 fprintf (stdout, "%s type %s%s",
3342 S_GET_NAME (s),
3343 S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
3344 segment_name (S_GET_SEGMENT (s)));
3345 }
3346
3347 static struct type_name
3348 {
3349 i386_operand_type mask;
3350 const char *name;
3351 }
3352 const type_names[] =
3353 {
3354 { { .bitfield = { .class = Reg, .byte = 1 } }, "r8" },
3355 { { .bitfield = { .class = Reg, .word = 1 } }, "r16" },
3356 { { .bitfield = { .class = Reg, .dword = 1 } }, "r32" },
3357 { { .bitfield = { .class = Reg, .qword = 1 } }, "r64" },
3358 { { .bitfield = { .instance = Accum, .byte = 1 } }, "acc8" },
3359 { { .bitfield = { .instance = Accum, .word = 1 } }, "acc16" },
3360 { { .bitfield = { .instance = Accum, .dword = 1 } }, "acc32" },
3361 { { .bitfield = { .instance = Accum, .qword = 1 } }, "acc64" },
3362 { { .bitfield = { .imm8 = 1 } }, "i8" },
3363 { { .bitfield = { .imm8s = 1 } }, "i8s" },
3364 { { .bitfield = { .imm16 = 1 } }, "i16" },
3365 { { .bitfield = { .imm32 = 1 } }, "i32" },
3366 { { .bitfield = { .imm32s = 1 } }, "i32s" },
3367 { { .bitfield = { .imm64 = 1 } }, "i64" },
3368 { { .bitfield = { .imm1 = 1 } }, "i1" },
3369 { { .bitfield = { .baseindex = 1 } }, "BaseIndex" },
3370 { { .bitfield = { .disp8 = 1 } }, "d8" },
3371 { { .bitfield = { .disp16 = 1 } }, "d16" },
3372 { { .bitfield = { .disp32 = 1 } }, "d32" },
3373 { { .bitfield = { .disp64 = 1 } }, "d64" },
3374 { { .bitfield = { .instance = RegD, .word = 1 } }, "InOutPortReg" },
3375 { { .bitfield = { .instance = RegC, .byte = 1 } }, "ShiftCount" },
3376 { { .bitfield = { .class = RegCR } }, "control reg" },
3377 { { .bitfield = { .class = RegTR } }, "test reg" },
3378 { { .bitfield = { .class = RegDR } }, "debug reg" },
3379 { { .bitfield = { .class = Reg, .tbyte = 1 } }, "FReg" },
3380 { { .bitfield = { .instance = Accum, .tbyte = 1 } }, "FAcc" },
3381 { { .bitfield = { .class = SReg } }, "SReg" },
3382 { { .bitfield = { .class = RegMMX } }, "rMMX" },
3383 { { .bitfield = { .class = RegSIMD, .xmmword = 1 } }, "rXMM" },
3384 { { .bitfield = { .class = RegSIMD, .ymmword = 1 } }, "rYMM" },
3385 { { .bitfield = { .class = RegSIMD, .zmmword = 1 } }, "rZMM" },
3386 { { .bitfield = { .class = RegSIMD, .tmmword = 1 } }, "rTMM" },
3387 { { .bitfield = { .class = RegMask } }, "Mask reg" },
3388 };
3389
3390 static void
3391 pt (i386_operand_type t)
3392 {
3393 unsigned int j;
3394 i386_operand_type a;
3395
3396 for (j = 0; j < ARRAY_SIZE (type_names); j++)
3397 {
3398 a = operand_type_and (t, type_names[j].mask);
3399 if (operand_type_equal (&a, &type_names[j].mask))
3400 fprintf (stdout, "%s, ", type_names[j].name);
3401 }
3402 fflush (stdout);
3403 }
3404
3405 #endif /* DEBUG386 */
3406 \f
3407 static bfd_reloc_code_real_type
3408 reloc (unsigned int size,
3409 int pcrel,
3410 int sign,
3411 bfd_reloc_code_real_type other)
3412 {
3413 if (other != NO_RELOC)
3414 {
3415 reloc_howto_type *rel;
3416
3417 if (size == 8)
3418 switch (other)
3419 {
3420 case BFD_RELOC_X86_64_GOT32:
3421 return BFD_RELOC_X86_64_GOT64;
3422 break;
3423 case BFD_RELOC_X86_64_GOTPLT64:
3424 return BFD_RELOC_X86_64_GOTPLT64;
3425 break;
3426 case BFD_RELOC_X86_64_PLTOFF64:
3427 return BFD_RELOC_X86_64_PLTOFF64;
3428 break;
3429 case BFD_RELOC_X86_64_GOTPC32:
3430 other = BFD_RELOC_X86_64_GOTPC64;
3431 break;
3432 case BFD_RELOC_X86_64_GOTPCREL:
3433 other = BFD_RELOC_X86_64_GOTPCREL64;
3434 break;
3435 case BFD_RELOC_X86_64_TPOFF32:
3436 other = BFD_RELOC_X86_64_TPOFF64;
3437 break;
3438 case BFD_RELOC_X86_64_DTPOFF32:
3439 other = BFD_RELOC_X86_64_DTPOFF64;
3440 break;
3441 default:
3442 break;
3443 }
3444
3445 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3446 if (other == BFD_RELOC_SIZE32)
3447 {
3448 if (size == 8)
3449 other = BFD_RELOC_SIZE64;
3450 if (pcrel)
3451 {
3452 as_bad (_("there are no pc-relative size relocations"));
3453 return NO_RELOC;
3454 }
3455 }
3456 #endif
3457
3458 /* Sign-checking 4-byte relocations in 16-/32-bit code is pointless. */
3459 if (size == 4 && (flag_code != CODE_64BIT || disallow_64bit_reloc))
3460 sign = -1;
3461
3462 rel = bfd_reloc_type_lookup (stdoutput, other);
3463 if (!rel)
3464 as_bad (_("unknown relocation (%u)"), other);
3465 else if (size != bfd_get_reloc_size (rel))
3466 as_bad (_("%u-byte relocation cannot be applied to %u-byte field"),
3467 bfd_get_reloc_size (rel),
3468 size);
3469 else if (pcrel && !rel->pc_relative)
3470 as_bad (_("non-pc-relative relocation for pc-relative field"));
3471 else if ((rel->complain_on_overflow == complain_overflow_signed
3472 && !sign)
3473 || (rel->complain_on_overflow == complain_overflow_unsigned
3474 && sign > 0))
3475 as_bad (_("relocated field and relocation type differ in signedness"));
3476 else
3477 return other;
3478 return NO_RELOC;
3479 }
3480
3481 if (pcrel)
3482 {
3483 if (!sign)
3484 as_bad (_("there are no unsigned pc-relative relocations"));
3485 switch (size)
3486 {
3487 case 1: return BFD_RELOC_8_PCREL;
3488 case 2: return BFD_RELOC_16_PCREL;
3489 case 4: return BFD_RELOC_32_PCREL;
3490 case 8: return BFD_RELOC_64_PCREL;
3491 }
3492 as_bad (_("cannot do %u byte pc-relative relocation"), size);
3493 }
3494 else
3495 {
3496 if (sign > 0)
3497 switch (size)
3498 {
3499 case 4: return BFD_RELOC_X86_64_32S;
3500 }
3501 else
3502 switch (size)
3503 {
3504 case 1: return BFD_RELOC_8;
3505 case 2: return BFD_RELOC_16;
3506 case 4: return BFD_RELOC_32;
3507 case 8: return BFD_RELOC_64;
3508 }
3509 as_bad (_("cannot do %s %u byte relocation"),
3510 sign > 0 ? "signed" : "unsigned", size);
3511 }
3512
3513 return NO_RELOC;
3514 }
3515
3516 /* Here we decide which fixups can be adjusted to make them relative to
3517 the beginning of the section instead of the symbol. Basically we need
3518 to make sure that the dynamic relocations are done correctly, so in
3519 some cases we force the original symbol to be used. */
3520
3521 int
3522 tc_i386_fix_adjustable (fixS *fixP ATTRIBUTE_UNUSED)
3523 {
3524 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3525 if (!IS_ELF)
3526 return 1;
3527
3528 /* Don't adjust pc-relative references to merge sections in 64-bit
3529 mode. */
3530 if (use_rela_relocations
3531 && (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0
3532 && fixP->fx_pcrel)
3533 return 0;
3534
3535 /* The x86_64 GOTPCREL are represented as 32bit PCrel relocations
3536 and changed later by validate_fix. */
3537 if (GOT_symbol && fixP->fx_subsy == GOT_symbol
3538 && fixP->fx_r_type == BFD_RELOC_32_PCREL)
3539 return 0;
3540
3541 /* Adjust_reloc_syms doesn't know about the GOT. Need to keep symbol
3542 for size relocations. */
3543 if (fixP->fx_r_type == BFD_RELOC_SIZE32
3544 || fixP->fx_r_type == BFD_RELOC_SIZE64
3545 || fixP->fx_r_type == BFD_RELOC_386_GOTOFF
3546 || fixP->fx_r_type == BFD_RELOC_386_GOT32
3547 || fixP->fx_r_type == BFD_RELOC_386_GOT32X
3548 || fixP->fx_r_type == BFD_RELOC_386_TLS_GD
3549 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDM
3550 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDO_32
3551 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE_32
3552 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE
3553 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTIE
3554 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE_32
3555 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE
3556 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTDESC
3557 || fixP->fx_r_type == BFD_RELOC_386_TLS_DESC_CALL
3558 || fixP->fx_r_type == BFD_RELOC_X86_64_GOT32
3559 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCREL
3560 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCRELX
3561 || fixP->fx_r_type == BFD_RELOC_X86_64_REX_GOTPCRELX
3562 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSGD
3563 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSLD
3564 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF32
3565 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF64
3566 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF
3567 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF32
3568 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF64
3569 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTOFF64
3570 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
3571 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSDESC_CALL
3572 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3573 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3574 return 0;
3575 #endif
3576 return 1;
3577 }
3578
3579 static INLINE bool
3580 want_disp32 (const insn_template *t)
3581 {
3582 return flag_code != CODE_64BIT
3583 || i.prefix[ADDR_PREFIX]
3584 || (t->mnem_off == MN_lea
3585 && (!i.types[1].bitfield.qword
3586 || t->opcode_modifier.size == SIZE32));
3587 }
3588
3589 static int
3590 intel_float_operand (const char *mnemonic)
3591 {
3592 /* Note that the value returned is meaningful only for opcodes with (memory)
3593 operands, hence the code here is free to improperly handle opcodes that
3594 have no operands (for better performance and smaller code). */
3595
3596 if (mnemonic[0] != 'f')
3597 return 0; /* non-math */
3598
3599 switch (mnemonic[1])
3600 {
3601 /* fclex, fdecstp, fdisi, femms, feni, fincstp, finit, fsetpm, and
3602 the fs segment override prefix not currently handled because no
3603 call path can make opcodes without operands get here */
3604 case 'i':
3605 return 2 /* integer op */;
3606 case 'l':
3607 if (mnemonic[2] == 'd' && (mnemonic[3] == 'c' || mnemonic[3] == 'e'))
3608 return 3; /* fldcw/fldenv */
3609 break;
3610 case 'n':
3611 if (mnemonic[2] != 'o' /* fnop */)
3612 return 3; /* non-waiting control op */
3613 break;
3614 case 'r':
3615 if (mnemonic[2] == 's')
3616 return 3; /* frstor/frstpm */
3617 break;
3618 case 's':
3619 if (mnemonic[2] == 'a')
3620 return 3; /* fsave */
3621 if (mnemonic[2] == 't')
3622 {
3623 switch (mnemonic[3])
3624 {
3625 case 'c': /* fstcw */
3626 case 'd': /* fstdw */
3627 case 'e': /* fstenv */
3628 case 's': /* fsts[gw] */
3629 return 3;
3630 }
3631 }
3632 break;
3633 case 'x':
3634 if (mnemonic[2] == 'r' || mnemonic[2] == 's')
3635 return 0; /* fxsave/fxrstor are not really math ops */
3636 break;
3637 }
3638
3639 return 1;
3640 }
3641
3642 static INLINE void
3643 install_template (const insn_template *t)
3644 {
3645 unsigned int l;
3646
3647 i.tm = *t;
3648
3649 /* Note that for pseudo prefixes this produces a length of 1. But for them
3650 the length isn't interesting at all. */
3651 for (l = 1; l < 4; ++l)
3652 if (!(t->base_opcode >> (8 * l)))
3653 break;
3654
3655 i.opcode_length = l;
3656 }
3657
3658 /* Build the VEX prefix. */
3659
3660 static void
3661 build_vex_prefix (const insn_template *t)
3662 {
3663 unsigned int register_specifier;
3664 unsigned int vector_length;
3665 unsigned int w;
3666
3667 /* Check register specifier. */
3668 if (i.vex.register_specifier)
3669 {
3670 register_specifier =
3671 ~register_number (i.vex.register_specifier) & 0xf;
3672 gas_assert ((i.vex.register_specifier->reg_flags & RegVRex) == 0);
3673 }
3674 else
3675 register_specifier = 0xf;
3676
3677 /* Use 2-byte VEX prefix by swapping destination and source operand
3678 if there are more than 1 register operand. */
3679 if (i.reg_operands > 1
3680 && i.vec_encoding != vex_encoding_vex3
3681 && i.dir_encoding == dir_encoding_default
3682 && i.operands == i.reg_operands
3683 && operand_type_equal (&i.types[0], &i.types[i.operands - 1])
3684 && i.tm.opcode_space == SPACE_0F
3685 && (i.tm.opcode_modifier.load || i.tm.opcode_modifier.d)
3686 && i.rex == REX_B)
3687 {
3688 unsigned int xchg;
3689
3690 swap_2_operands (0, i.operands - 1);
3691
3692 gas_assert (i.rm.mode == 3);
3693
3694 i.rex = REX_R;
3695 xchg = i.rm.regmem;
3696 i.rm.regmem = i.rm.reg;
3697 i.rm.reg = xchg;
3698
3699 if (i.tm.opcode_modifier.d)
3700 i.tm.base_opcode ^= (i.tm.base_opcode & 0xee) != 0x6e
3701 ? Opcode_ExtD : Opcode_SIMD_IntD;
3702 else /* Use the next insn. */
3703 install_template (&t[1]);
3704 }
3705
3706 /* Use 2-byte VEX prefix by swapping commutative source operands if there
3707 are no memory operands and at least 3 register ones. */
3708 if (i.reg_operands >= 3
3709 && i.vec_encoding != vex_encoding_vex3
3710 && i.reg_operands == i.operands - i.imm_operands
3711 && i.tm.opcode_modifier.vex
3712 && i.tm.opcode_modifier.commutative
3713 && (i.tm.opcode_modifier.sse2avx
3714 || (optimize > 1 && !i.no_optimize))
3715 && i.rex == REX_B
3716 && i.vex.register_specifier
3717 && !(i.vex.register_specifier->reg_flags & RegRex))
3718 {
3719 unsigned int xchg = i.operands - i.reg_operands;
3720
3721 gas_assert (i.tm.opcode_space == SPACE_0F);
3722 gas_assert (!i.tm.opcode_modifier.sae);
3723 gas_assert (operand_type_equal (&i.types[i.operands - 2],
3724 &i.types[i.operands - 3]));
3725 gas_assert (i.rm.mode == 3);
3726
3727 swap_2_operands (xchg, xchg + 1);
3728
3729 i.rex = 0;
3730 xchg = i.rm.regmem | 8;
3731 i.rm.regmem = ~register_specifier & 0xf;
3732 gas_assert (!(i.rm.regmem & 8));
3733 i.vex.register_specifier += xchg - i.rm.regmem;
3734 register_specifier = ~xchg & 0xf;
3735 }
3736
3737 if (i.tm.opcode_modifier.vex == VEXScalar)
3738 vector_length = avxscalar;
3739 else if (i.tm.opcode_modifier.vex == VEX256)
3740 vector_length = 1;
3741 else if (dot_insn () && i.tm.opcode_modifier.vex == VEX128)
3742 vector_length = 0;
3743 else
3744 {
3745 unsigned int op;
3746
3747 /* Determine vector length from the last multi-length vector
3748 operand. */
3749 vector_length = 0;
3750 for (op = t->operands; op--;)
3751 if (t->operand_types[op].bitfield.xmmword
3752 && t->operand_types[op].bitfield.ymmword
3753 && i.types[op].bitfield.ymmword)
3754 {
3755 vector_length = 1;
3756 break;
3757 }
3758 }
3759
3760 /* Check the REX.W bit and VEXW. */
3761 if (i.tm.opcode_modifier.vexw == VEXWIG)
3762 w = (vexwig == vexw1 || (i.rex & REX_W)) ? 1 : 0;
3763 else if (i.tm.opcode_modifier.vexw)
3764 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3765 else
3766 w = (flag_code == CODE_64BIT ? i.rex & REX_W : vexwig == vexw1) ? 1 : 0;
3767
3768 /* Use 2-byte VEX prefix if possible. */
3769 if (w == 0
3770 && i.vec_encoding != vex_encoding_vex3
3771 && i.tm.opcode_space == SPACE_0F
3772 && (i.rex & (REX_W | REX_X | REX_B)) == 0)
3773 {
3774 /* 2-byte VEX prefix. */
3775 unsigned int r;
3776
3777 i.vex.length = 2;
3778 i.vex.bytes[0] = 0xc5;
3779
3780 /* Check the REX.R bit. */
3781 r = (i.rex & REX_R) ? 0 : 1;
3782 i.vex.bytes[1] = (r << 7
3783 | register_specifier << 3
3784 | vector_length << 2
3785 | i.tm.opcode_modifier.opcodeprefix);
3786 }
3787 else
3788 {
3789 /* 3-byte VEX prefix. */
3790 i.vex.length = 3;
3791
3792 switch (i.tm.opcode_space)
3793 {
3794 case SPACE_0F:
3795 case SPACE_0F38:
3796 case SPACE_0F3A:
3797 i.vex.bytes[0] = 0xc4;
3798 break;
3799 case SPACE_XOP08:
3800 case SPACE_XOP09:
3801 case SPACE_XOP0A:
3802 i.vex.bytes[0] = 0x8f;
3803 break;
3804 default:
3805 abort ();
3806 }
3807
3808 /* The high 3 bits of the second VEX byte are 1's compliment
3809 of RXB bits from REX. */
3810 i.vex.bytes[1] = ((~i.rex & 7) << 5)
3811 | (!dot_insn () ? i.tm.opcode_space
3812 : i.insn_opcode_space);
3813
3814 i.vex.bytes[2] = (w << 7
3815 | register_specifier << 3
3816 | vector_length << 2
3817 | i.tm.opcode_modifier.opcodeprefix);
3818 }
3819 }
3820
3821 static INLINE bool
3822 is_evex_encoding (const insn_template *t)
3823 {
3824 return t->opcode_modifier.evex || t->opcode_modifier.disp8memshift
3825 || t->opcode_modifier.broadcast || t->opcode_modifier.masking
3826 || t->opcode_modifier.sae;
3827 }
3828
3829 static INLINE bool
3830 is_any_vex_encoding (const insn_template *t)
3831 {
3832 return t->opcode_modifier.vex || is_evex_encoding (t);
3833 }
3834
3835 static unsigned int
3836 get_broadcast_bytes (const insn_template *t, bool diag)
3837 {
3838 unsigned int op, bytes;
3839 const i386_operand_type *types;
3840
3841 if (i.broadcast.type)
3842 return (1 << (t->opcode_modifier.broadcast - 1)) * i.broadcast.type;
3843
3844 gas_assert (intel_syntax);
3845
3846 for (op = 0; op < t->operands; ++op)
3847 if (t->operand_types[op].bitfield.baseindex)
3848 break;
3849
3850 gas_assert (op < t->operands);
3851
3852 if (t->opcode_modifier.evex
3853 && t->opcode_modifier.evex != EVEXDYN)
3854 switch (i.broadcast.bytes)
3855 {
3856 case 1:
3857 if (t->operand_types[op].bitfield.word)
3858 return 2;
3859 /* Fall through. */
3860 case 2:
3861 if (t->operand_types[op].bitfield.dword)
3862 return 4;
3863 /* Fall through. */
3864 case 4:
3865 if (t->operand_types[op].bitfield.qword)
3866 return 8;
3867 /* Fall through. */
3868 case 8:
3869 if (t->operand_types[op].bitfield.xmmword)
3870 return 16;
3871 if (t->operand_types[op].bitfield.ymmword)
3872 return 32;
3873 if (t->operand_types[op].bitfield.zmmword)
3874 return 64;
3875 /* Fall through. */
3876 default:
3877 abort ();
3878 }
3879
3880 gas_assert (op + 1 < t->operands);
3881
3882 if (t->operand_types[op + 1].bitfield.xmmword
3883 + t->operand_types[op + 1].bitfield.ymmword
3884 + t->operand_types[op + 1].bitfield.zmmword > 1)
3885 {
3886 types = &i.types[op + 1];
3887 diag = false;
3888 }
3889 else /* Ambiguous - guess with a preference to non-AVX512VL forms. */
3890 types = &t->operand_types[op];
3891
3892 if (types->bitfield.zmmword)
3893 bytes = 64;
3894 else if (types->bitfield.ymmword)
3895 bytes = 32;
3896 else
3897 bytes = 16;
3898
3899 if (diag)
3900 as_warn (_("ambiguous broadcast for `%s', using %u-bit form"),
3901 insn_name (t), bytes * 8);
3902
3903 return bytes;
3904 }
3905
3906 /* Build the EVEX prefix. */
3907
3908 static void
3909 build_evex_prefix (void)
3910 {
3911 unsigned int register_specifier, w;
3912 rex_byte vrex_used = 0;
3913
3914 /* Check register specifier. */
3915 if (i.vex.register_specifier)
3916 {
3917 gas_assert ((i.vrex & REX_X) == 0);
3918
3919 register_specifier = i.vex.register_specifier->reg_num;
3920 if ((i.vex.register_specifier->reg_flags & RegRex))
3921 register_specifier += 8;
3922 /* The upper 16 registers are encoded in the fourth byte of the
3923 EVEX prefix. */
3924 if (!(i.vex.register_specifier->reg_flags & RegVRex))
3925 i.vex.bytes[3] = 0x8;
3926 register_specifier = ~register_specifier & 0xf;
3927 }
3928 else
3929 {
3930 register_specifier = 0xf;
3931
3932 /* Encode upper 16 vector index register in the fourth byte of
3933 the EVEX prefix. */
3934 if (!(i.vrex & REX_X))
3935 i.vex.bytes[3] = 0x8;
3936 else
3937 vrex_used |= REX_X;
3938 }
3939
3940 /* 4 byte EVEX prefix. */
3941 i.vex.length = 4;
3942 i.vex.bytes[0] = 0x62;
3943
3944 /* The high 3 bits of the second EVEX byte are 1's compliment of RXB
3945 bits from REX. */
3946 gas_assert (i.tm.opcode_space >= SPACE_0F);
3947 gas_assert (i.tm.opcode_space <= SPACE_EVEXMAP6);
3948 i.vex.bytes[1] = ((~i.rex & 7) << 5)
3949 | (!dot_insn () ? i.tm.opcode_space
3950 : i.insn_opcode_space);
3951
3952 /* The fifth bit of the second EVEX byte is 1's compliment of the
3953 REX_R bit in VREX. */
3954 if (!(i.vrex & REX_R))
3955 i.vex.bytes[1] |= 0x10;
3956 else
3957 vrex_used |= REX_R;
3958
3959 if ((i.reg_operands + i.imm_operands) == i.operands)
3960 {
3961 /* When all operands are registers, the REX_X bit in REX is not
3962 used. We reuse it to encode the upper 16 registers, which is
3963 indicated by the REX_B bit in VREX. The REX_X bit is encoded
3964 as 1's compliment. */
3965 if ((i.vrex & REX_B))
3966 {
3967 vrex_used |= REX_B;
3968 i.vex.bytes[1] &= ~0x40;
3969 }
3970 }
3971
3972 /* EVEX instructions shouldn't need the REX prefix. */
3973 i.vrex &= ~vrex_used;
3974 gas_assert (i.vrex == 0);
3975
3976 /* Check the REX.W bit and VEXW. */
3977 if (i.tm.opcode_modifier.vexw == VEXWIG)
3978 w = (evexwig == evexw1 || (i.rex & REX_W)) ? 1 : 0;
3979 else if (i.tm.opcode_modifier.vexw)
3980 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3981 else
3982 w = (flag_code == CODE_64BIT ? i.rex & REX_W : evexwig == evexw1) ? 1 : 0;
3983
3984 /* The third byte of the EVEX prefix. */
3985 i.vex.bytes[2] = ((w << 7)
3986 | (register_specifier << 3)
3987 | 4 /* Encode the U bit. */
3988 | i.tm.opcode_modifier.opcodeprefix);
3989
3990 /* The fourth byte of the EVEX prefix. */
3991 /* The zeroing-masking bit. */
3992 if (i.mask.reg && i.mask.zeroing)
3993 i.vex.bytes[3] |= 0x80;
3994
3995 /* Don't always set the broadcast bit if there is no RC. */
3996 if (i.rounding.type == rc_none)
3997 {
3998 /* Encode the vector length. */
3999 unsigned int vec_length;
4000
4001 if (!i.tm.opcode_modifier.evex
4002 || i.tm.opcode_modifier.evex == EVEXDYN)
4003 {
4004 unsigned int op;
4005
4006 /* Determine vector length from the last multi-length vector
4007 operand. */
4008 for (op = i.operands; op--;)
4009 if (i.tm.operand_types[op].bitfield.xmmword
4010 + i.tm.operand_types[op].bitfield.ymmword
4011 + i.tm.operand_types[op].bitfield.zmmword > 1)
4012 {
4013 if (i.types[op].bitfield.zmmword)
4014 {
4015 i.tm.opcode_modifier.evex = EVEX512;
4016 break;
4017 }
4018 else if (i.types[op].bitfield.ymmword)
4019 {
4020 i.tm.opcode_modifier.evex = EVEX256;
4021 break;
4022 }
4023 else if (i.types[op].bitfield.xmmword)
4024 {
4025 i.tm.opcode_modifier.evex = EVEX128;
4026 break;
4027 }
4028 else if ((i.broadcast.type || i.broadcast.bytes)
4029 && op == i.broadcast.operand)
4030 {
4031 switch (get_broadcast_bytes (&i.tm, true))
4032 {
4033 case 64:
4034 i.tm.opcode_modifier.evex = EVEX512;
4035 break;
4036 case 32:
4037 i.tm.opcode_modifier.evex = EVEX256;
4038 break;
4039 case 16:
4040 i.tm.opcode_modifier.evex = EVEX128;
4041 break;
4042 default:
4043 abort ();
4044 }
4045 break;
4046 }
4047 }
4048
4049 if (op >= MAX_OPERANDS)
4050 abort ();
4051 }
4052
4053 switch (i.tm.opcode_modifier.evex)
4054 {
4055 case EVEXLIG: /* LL' is ignored */
4056 vec_length = evexlig << 5;
4057 break;
4058 case EVEX128:
4059 vec_length = 0 << 5;
4060 break;
4061 case EVEX256:
4062 vec_length = 1 << 5;
4063 break;
4064 case EVEX512:
4065 vec_length = 2 << 5;
4066 break;
4067 case EVEX_L3:
4068 if (dot_insn ())
4069 {
4070 vec_length = 3 << 5;
4071 break;
4072 }
4073 /* Fall through. */
4074 default:
4075 abort ();
4076 break;
4077 }
4078 i.vex.bytes[3] |= vec_length;
4079 /* Encode the broadcast bit. */
4080 if (i.broadcast.type || i.broadcast.bytes)
4081 i.vex.bytes[3] |= 0x10;
4082 }
4083 else if (i.rounding.type != saeonly)
4084 i.vex.bytes[3] |= 0x10 | (i.rounding.type << 5);
4085 else
4086 i.vex.bytes[3] |= 0x10 | (evexrcig << 5);
4087
4088 if (i.mask.reg)
4089 i.vex.bytes[3] |= i.mask.reg->reg_num;
4090 }
4091
4092 static void
4093 process_immext (void)
4094 {
4095 expressionS *exp;
4096
4097 /* These AMD 3DNow! and SSE2 instructions have an opcode suffix
4098 which is coded in the same place as an 8-bit immediate field
4099 would be. Here we fake an 8-bit immediate operand from the
4100 opcode suffix stored in tm.extension_opcode.
4101
4102 AVX instructions also use this encoding, for some of
4103 3 argument instructions. */
4104
4105 gas_assert (i.imm_operands <= 1
4106 && (i.operands <= 2
4107 || (is_any_vex_encoding (&i.tm)
4108 && i.operands <= 4)));
4109
4110 exp = &im_expressions[i.imm_operands++];
4111 i.op[i.operands].imms = exp;
4112 i.types[i.operands].bitfield.imm8 = 1;
4113 i.operands++;
4114 exp->X_op = O_constant;
4115 exp->X_add_number = i.tm.extension_opcode;
4116 i.tm.extension_opcode = None;
4117 }
4118
4119
4120 static int
4121 check_hle (void)
4122 {
4123 switch (i.tm.opcode_modifier.prefixok)
4124 {
4125 default:
4126 abort ();
4127 case PrefixLock:
4128 case PrefixNone:
4129 case PrefixNoTrack:
4130 case PrefixRep:
4131 as_bad (_("invalid instruction `%s' after `%s'"),
4132 insn_name (&i.tm), i.hle_prefix);
4133 return 0;
4134 case PrefixHLELock:
4135 if (i.prefix[LOCK_PREFIX])
4136 return 1;
4137 as_bad (_("missing `lock' with `%s'"), i.hle_prefix);
4138 return 0;
4139 case PrefixHLEAny:
4140 return 1;
4141 case PrefixHLERelease:
4142 if (i.prefix[HLE_PREFIX] != XRELEASE_PREFIX_OPCODE)
4143 {
4144 as_bad (_("instruction `%s' after `xacquire' not allowed"),
4145 insn_name (&i.tm));
4146 return 0;
4147 }
4148 if (i.mem_operands == 0 || !(i.flags[i.operands - 1] & Operand_Mem))
4149 {
4150 as_bad (_("memory destination needed for instruction `%s'"
4151 " after `xrelease'"), insn_name (&i.tm));
4152 return 0;
4153 }
4154 return 1;
4155 }
4156 }
4157
4158 /* Encode aligned vector move as unaligned vector move. */
4159
4160 static void
4161 encode_with_unaligned_vector_move (void)
4162 {
4163 switch (i.tm.base_opcode)
4164 {
4165 case 0x28: /* Load instructions. */
4166 case 0x29: /* Store instructions. */
4167 /* movaps/movapd/vmovaps/vmovapd. */
4168 if (i.tm.opcode_space == SPACE_0F
4169 && i.tm.opcode_modifier.opcodeprefix <= PREFIX_0X66)
4170 i.tm.base_opcode = 0x10 | (i.tm.base_opcode & 1);
4171 break;
4172 case 0x6f: /* Load instructions. */
4173 case 0x7f: /* Store instructions. */
4174 /* movdqa/vmovdqa/vmovdqa64/vmovdqa32. */
4175 if (i.tm.opcode_space == SPACE_0F
4176 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0X66)
4177 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
4178 break;
4179 default:
4180 break;
4181 }
4182 }
4183
4184 /* Try the shortest encoding by shortening operand size. */
4185
4186 static void
4187 optimize_encoding (void)
4188 {
4189 unsigned int j;
4190
4191 if (i.tm.mnem_off == MN_lea)
4192 {
4193 /* Optimize: -O:
4194 lea symbol, %rN -> mov $symbol, %rN
4195 lea (%rM), %rN -> mov %rM, %rN
4196 lea (,%rM,1), %rN -> mov %rM, %rN
4197
4198 and in 32-bit mode for 16-bit addressing
4199
4200 lea (%rM), %rN -> movzx %rM, %rN
4201
4202 and in 64-bit mode zap 32-bit addressing in favor of using a
4203 32-bit (or less) destination.
4204 */
4205 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
4206 {
4207 if (!i.op[1].regs->reg_type.bitfield.word)
4208 i.tm.opcode_modifier.size = SIZE32;
4209 i.prefix[ADDR_PREFIX] = 0;
4210 }
4211
4212 if (!i.index_reg && !i.base_reg)
4213 {
4214 /* Handle:
4215 lea symbol, %rN -> mov $symbol, %rN
4216 */
4217 if (flag_code == CODE_64BIT)
4218 {
4219 /* Don't transform a relocation to a 16-bit one. */
4220 if (i.op[0].disps
4221 && i.op[0].disps->X_op != O_constant
4222 && i.op[1].regs->reg_type.bitfield.word)
4223 return;
4224
4225 if (!i.op[1].regs->reg_type.bitfield.qword
4226 || i.tm.opcode_modifier.size == SIZE32)
4227 {
4228 i.tm.base_opcode = 0xb8;
4229 i.tm.opcode_modifier.modrm = 0;
4230 if (!i.op[1].regs->reg_type.bitfield.word)
4231 i.types[0].bitfield.imm32 = 1;
4232 else
4233 {
4234 i.tm.opcode_modifier.size = SIZE16;
4235 i.types[0].bitfield.imm16 = 1;
4236 }
4237 }
4238 else
4239 {
4240 /* Subject to further optimization below. */
4241 i.tm.base_opcode = 0xc7;
4242 i.tm.extension_opcode = 0;
4243 i.types[0].bitfield.imm32s = 1;
4244 i.types[0].bitfield.baseindex = 0;
4245 }
4246 }
4247 /* Outside of 64-bit mode address and operand sizes have to match if
4248 a relocation is involved, as otherwise we wouldn't (currently) or
4249 even couldn't express the relocation correctly. */
4250 else if (i.op[0].disps
4251 && i.op[0].disps->X_op != O_constant
4252 && ((!i.prefix[ADDR_PREFIX])
4253 != (flag_code == CODE_32BIT
4254 ? i.op[1].regs->reg_type.bitfield.dword
4255 : i.op[1].regs->reg_type.bitfield.word)))
4256 return;
4257 /* In 16-bit mode converting LEA with 16-bit addressing and a 32-bit
4258 destination is going to grow encoding size. */
4259 else if (flag_code == CODE_16BIT
4260 && (optimize <= 1 || optimize_for_space)
4261 && !i.prefix[ADDR_PREFIX]
4262 && i.op[1].regs->reg_type.bitfield.dword)
4263 return;
4264 else
4265 {
4266 i.tm.base_opcode = 0xb8;
4267 i.tm.opcode_modifier.modrm = 0;
4268 if (i.op[1].regs->reg_type.bitfield.dword)
4269 i.types[0].bitfield.imm32 = 1;
4270 else
4271 i.types[0].bitfield.imm16 = 1;
4272
4273 if (i.op[0].disps
4274 && i.op[0].disps->X_op == O_constant
4275 && i.op[1].regs->reg_type.bitfield.dword
4276 /* NB: Add () to !i.prefix[ADDR_PREFIX] to silence
4277 GCC 5. */
4278 && (!i.prefix[ADDR_PREFIX]) != (flag_code == CODE_32BIT))
4279 i.op[0].disps->X_add_number &= 0xffff;
4280 }
4281
4282 i.tm.operand_types[0] = i.types[0];
4283 i.imm_operands = 1;
4284 if (!i.op[0].imms)
4285 {
4286 i.op[0].imms = &im_expressions[0];
4287 i.op[0].imms->X_op = O_absent;
4288 }
4289 }
4290 else if (i.op[0].disps
4291 && (i.op[0].disps->X_op != O_constant
4292 || i.op[0].disps->X_add_number))
4293 return;
4294 else
4295 {
4296 /* Handle:
4297 lea (%rM), %rN -> mov %rM, %rN
4298 lea (,%rM,1), %rN -> mov %rM, %rN
4299 lea (%rM), %rN -> movzx %rM, %rN
4300 */
4301 const reg_entry *addr_reg;
4302
4303 if (!i.index_reg && i.base_reg->reg_num != RegIP)
4304 addr_reg = i.base_reg;
4305 else if (!i.base_reg
4306 && i.index_reg->reg_num != RegIZ
4307 && !i.log2_scale_factor)
4308 addr_reg = i.index_reg;
4309 else
4310 return;
4311
4312 if (addr_reg->reg_type.bitfield.word
4313 && i.op[1].regs->reg_type.bitfield.dword)
4314 {
4315 if (flag_code != CODE_32BIT)
4316 return;
4317 i.tm.opcode_space = SPACE_0F;
4318 i.tm.base_opcode = 0xb7;
4319 }
4320 else
4321 i.tm.base_opcode = 0x8b;
4322
4323 if (addr_reg->reg_type.bitfield.dword
4324 && i.op[1].regs->reg_type.bitfield.qword)
4325 i.tm.opcode_modifier.size = SIZE32;
4326
4327 i.op[0].regs = addr_reg;
4328 i.reg_operands = 2;
4329 }
4330
4331 i.mem_operands = 0;
4332 i.disp_operands = 0;
4333 i.prefix[ADDR_PREFIX] = 0;
4334 i.prefix[SEG_PREFIX] = 0;
4335 i.seg[0] = NULL;
4336 }
4337
4338 if (optimize_for_space
4339 && i.tm.mnem_off == MN_test
4340 && i.reg_operands == 1
4341 && i.imm_operands == 1
4342 && !i.types[1].bitfield.byte
4343 && i.op[0].imms->X_op == O_constant
4344 && fits_in_imm7 (i.op[0].imms->X_add_number))
4345 {
4346 /* Optimize: -Os:
4347 test $imm7, %r64/%r32/%r16 -> test $imm7, %r8
4348 */
4349 unsigned int base_regnum = i.op[1].regs->reg_num;
4350 if (flag_code == CODE_64BIT || base_regnum < 4)
4351 {
4352 i.types[1].bitfield.byte = 1;
4353 /* Ignore the suffix. */
4354 i.suffix = 0;
4355 /* Convert to byte registers. */
4356 if (i.types[1].bitfield.word)
4357 j = 16;
4358 else if (i.types[1].bitfield.dword)
4359 j = 32;
4360 else
4361 j = 48;
4362 if (!(i.op[1].regs->reg_flags & RegRex) && base_regnum < 4)
4363 j += 8;
4364 i.op[1].regs -= j;
4365 }
4366 }
4367 else if (flag_code == CODE_64BIT
4368 && i.tm.opcode_space == SPACE_BASE
4369 && ((i.types[1].bitfield.qword
4370 && i.reg_operands == 1
4371 && i.imm_operands == 1
4372 && i.op[0].imms->X_op == O_constant
4373 && ((i.tm.base_opcode == 0xb8
4374 && i.tm.extension_opcode == None
4375 && fits_in_unsigned_long (i.op[0].imms->X_add_number))
4376 || (fits_in_imm31 (i.op[0].imms->X_add_number)
4377 && (i.tm.base_opcode == 0x24
4378 || (i.tm.base_opcode == 0x80
4379 && i.tm.extension_opcode == 0x4)
4380 || i.tm.mnem_off == MN_test
4381 || ((i.tm.base_opcode | 1) == 0xc7
4382 && i.tm.extension_opcode == 0x0)))
4383 || (fits_in_imm7 (i.op[0].imms->X_add_number)
4384 && i.tm.base_opcode == 0x83
4385 && i.tm.extension_opcode == 0x4)))
4386 || (i.types[0].bitfield.qword
4387 && ((i.reg_operands == 2
4388 && i.op[0].regs == i.op[1].regs
4389 && (i.tm.mnem_off == MN_xor
4390 || i.tm.mnem_off == MN_sub))
4391 || i.tm.mnem_off == MN_clr))))
4392 {
4393 /* Optimize: -O:
4394 andq $imm31, %r64 -> andl $imm31, %r32
4395 andq $imm7, %r64 -> andl $imm7, %r32
4396 testq $imm31, %r64 -> testl $imm31, %r32
4397 xorq %r64, %r64 -> xorl %r32, %r32
4398 subq %r64, %r64 -> subl %r32, %r32
4399 movq $imm31, %r64 -> movl $imm31, %r32
4400 movq $imm32, %r64 -> movl $imm32, %r32
4401 */
4402 i.tm.opcode_modifier.size = SIZE32;
4403 if (i.imm_operands)
4404 {
4405 i.types[0].bitfield.imm32 = 1;
4406 i.types[0].bitfield.imm32s = 0;
4407 i.types[0].bitfield.imm64 = 0;
4408 }
4409 else
4410 {
4411 i.types[0].bitfield.dword = 1;
4412 i.types[0].bitfield.qword = 0;
4413 }
4414 i.types[1].bitfield.dword = 1;
4415 i.types[1].bitfield.qword = 0;
4416 if (i.tm.mnem_off == MN_mov || i.tm.mnem_off == MN_lea)
4417 {
4418 /* Handle
4419 movq $imm31, %r64 -> movl $imm31, %r32
4420 movq $imm32, %r64 -> movl $imm32, %r32
4421 */
4422 i.tm.operand_types[0].bitfield.imm32 = 1;
4423 i.tm.operand_types[0].bitfield.imm32s = 0;
4424 i.tm.operand_types[0].bitfield.imm64 = 0;
4425 if ((i.tm.base_opcode | 1) == 0xc7)
4426 {
4427 /* Handle
4428 movq $imm31, %r64 -> movl $imm31, %r32
4429 */
4430 i.tm.base_opcode = 0xb8;
4431 i.tm.extension_opcode = None;
4432 i.tm.opcode_modifier.w = 0;
4433 i.tm.opcode_modifier.modrm = 0;
4434 }
4435 }
4436 }
4437 else if (optimize > 1
4438 && !optimize_for_space
4439 && i.reg_operands == 2
4440 && i.op[0].regs == i.op[1].regs
4441 && (i.tm.mnem_off == MN_and || i.tm.mnem_off == MN_or)
4442 && (flag_code != CODE_64BIT || !i.types[0].bitfield.dword))
4443 {
4444 /* Optimize: -O2:
4445 andb %rN, %rN -> testb %rN, %rN
4446 andw %rN, %rN -> testw %rN, %rN
4447 andq %rN, %rN -> testq %rN, %rN
4448 orb %rN, %rN -> testb %rN, %rN
4449 orw %rN, %rN -> testw %rN, %rN
4450 orq %rN, %rN -> testq %rN, %rN
4451
4452 and outside of 64-bit mode
4453
4454 andl %rN, %rN -> testl %rN, %rN
4455 orl %rN, %rN -> testl %rN, %rN
4456 */
4457 i.tm.base_opcode = 0x84 | (i.tm.base_opcode & 1);
4458 }
4459 else if (i.tm.base_opcode == 0xba
4460 && i.tm.opcode_space == SPACE_0F
4461 && i.reg_operands == 1
4462 && i.op[0].imms->X_op == O_constant
4463 && i.op[0].imms->X_add_number >= 0)
4464 {
4465 /* Optimize: -O:
4466 btw $n, %rN -> btl $n, %rN (outside of 16-bit mode, n < 16)
4467 btq $n, %rN -> btl $n, %rN (in 64-bit mode, n < 32, N < 8)
4468 btl $n, %rN -> btw $n, %rN (in 16-bit mode, n < 16)
4469
4470 With <BT> one of bts, btr, and bts also:
4471 <BT>w $n, %rN -> btl $n, %rN (in 32-bit mode, n < 16)
4472 <BT>l $n, %rN -> btw $n, %rN (in 16-bit mode, n < 16)
4473 */
4474 switch (flag_code)
4475 {
4476 case CODE_64BIT:
4477 if (i.tm.extension_opcode != 4)
4478 break;
4479 if (i.types[1].bitfield.qword
4480 && i.op[0].imms->X_add_number < 32
4481 && !(i.op[1].regs->reg_flags & RegRex))
4482 i.tm.opcode_modifier.size = SIZE32;
4483 /* Fall through. */
4484 case CODE_32BIT:
4485 if (i.types[1].bitfield.word
4486 && i.op[0].imms->X_add_number < 16)
4487 i.tm.opcode_modifier.size = SIZE32;
4488 break;
4489 case CODE_16BIT:
4490 if (i.op[0].imms->X_add_number < 16)
4491 i.tm.opcode_modifier.size = SIZE16;
4492 break;
4493 }
4494 }
4495 else if (i.reg_operands == 3
4496 && i.op[0].regs == i.op[1].regs
4497 && !i.types[2].bitfield.xmmword
4498 && (i.tm.opcode_modifier.vex
4499 || ((!i.mask.reg || i.mask.zeroing)
4500 && is_evex_encoding (&i.tm)
4501 && (i.vec_encoding != vex_encoding_evex
4502 || cpu_arch_isa_flags.bitfield.cpuavx512vl
4503 || is_cpu (&i.tm, CpuAVX512VL)
4504 || (i.tm.operand_types[2].bitfield.zmmword
4505 && i.types[2].bitfield.ymmword))))
4506 && i.tm.opcode_space == SPACE_0F
4507 && ((i.tm.base_opcode | 2) == 0x57
4508 || i.tm.base_opcode == 0xdf
4509 || i.tm.base_opcode == 0xef
4510 || (i.tm.base_opcode | 3) == 0xfb
4511 || i.tm.base_opcode == 0x42
4512 || i.tm.base_opcode == 0x47))
4513 {
4514 /* Optimize: -O1:
4515 VOP, one of vandnps, vandnpd, vxorps, vxorpd, vpsubb, vpsubd,
4516 vpsubq and vpsubw:
4517 EVEX VOP %zmmM, %zmmM, %zmmN
4518 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
4519 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4520 EVEX VOP %ymmM, %ymmM, %ymmN
4521 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
4522 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4523 VEX VOP %ymmM, %ymmM, %ymmN
4524 -> VEX VOP %xmmM, %xmmM, %xmmN
4525 VOP, one of vpandn and vpxor:
4526 VEX VOP %ymmM, %ymmM, %ymmN
4527 -> VEX VOP %xmmM, %xmmM, %xmmN
4528 VOP, one of vpandnd and vpandnq:
4529 EVEX VOP %zmmM, %zmmM, %zmmN
4530 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
4531 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4532 EVEX VOP %ymmM, %ymmM, %ymmN
4533 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
4534 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4535 VOP, one of vpxord and vpxorq:
4536 EVEX VOP %zmmM, %zmmM, %zmmN
4537 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
4538 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4539 EVEX VOP %ymmM, %ymmM, %ymmN
4540 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
4541 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4542 VOP, one of kxord and kxorq:
4543 VEX VOP %kM, %kM, %kN
4544 -> VEX kxorw %kM, %kM, %kN
4545 VOP, one of kandnd and kandnq:
4546 VEX VOP %kM, %kM, %kN
4547 -> VEX kandnw %kM, %kM, %kN
4548 */
4549 if (is_evex_encoding (&i.tm))
4550 {
4551 if (i.vec_encoding != vex_encoding_evex)
4552 {
4553 i.tm.opcode_modifier.vex = VEX128;
4554 i.tm.opcode_modifier.vexw = VEXW0;
4555 i.tm.opcode_modifier.evex = 0;
4556 }
4557 else if (optimize > 1)
4558 i.tm.opcode_modifier.evex = EVEX128;
4559 else
4560 return;
4561 }
4562 else if (i.tm.operand_types[0].bitfield.class == RegMask)
4563 {
4564 i.tm.opcode_modifier.opcodeprefix = PREFIX_NONE;
4565 i.tm.opcode_modifier.vexw = VEXW0;
4566 }
4567 else
4568 i.tm.opcode_modifier.vex = VEX128;
4569
4570 if (i.tm.opcode_modifier.vex)
4571 for (j = 0; j < 3; j++)
4572 {
4573 i.types[j].bitfield.xmmword = 1;
4574 i.types[j].bitfield.ymmword = 0;
4575 }
4576 }
4577 else if (i.vec_encoding != vex_encoding_evex
4578 && !i.types[0].bitfield.zmmword
4579 && !i.types[1].bitfield.zmmword
4580 && !i.mask.reg
4581 && !i.broadcast.type
4582 && !i.broadcast.bytes
4583 && is_evex_encoding (&i.tm)
4584 && ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4585 || (i.tm.base_opcode & ~4) == 0xdb
4586 || (i.tm.base_opcode & ~4) == 0xeb)
4587 && i.tm.extension_opcode == None)
4588 {
4589 /* Optimize: -O1:
4590 VOP, one of vmovdqa32, vmovdqa64, vmovdqu8, vmovdqu16,
4591 vmovdqu32 and vmovdqu64:
4592 EVEX VOP %xmmM, %xmmN
4593 -> VEX vmovdqa|vmovdqu %xmmM, %xmmN (M and N < 16)
4594 EVEX VOP %ymmM, %ymmN
4595 -> VEX vmovdqa|vmovdqu %ymmM, %ymmN (M and N < 16)
4596 EVEX VOP %xmmM, mem
4597 -> VEX vmovdqa|vmovdqu %xmmM, mem (M < 16)
4598 EVEX VOP %ymmM, mem
4599 -> VEX vmovdqa|vmovdqu %ymmM, mem (M < 16)
4600 EVEX VOP mem, %xmmN
4601 -> VEX mvmovdqa|vmovdquem, %xmmN (N < 16)
4602 EVEX VOP mem, %ymmN
4603 -> VEX vmovdqa|vmovdqu mem, %ymmN (N < 16)
4604 VOP, one of vpand, vpandn, vpor, vpxor:
4605 EVEX VOP{d,q} %xmmL, %xmmM, %xmmN
4606 -> VEX VOP %xmmL, %xmmM, %xmmN (L, M, and N < 16)
4607 EVEX VOP{d,q} %ymmL, %ymmM, %ymmN
4608 -> VEX VOP %ymmL, %ymmM, %ymmN (L, M, and N < 16)
4609 EVEX VOP{d,q} mem, %xmmM, %xmmN
4610 -> VEX VOP mem, %xmmM, %xmmN (M and N < 16)
4611 EVEX VOP{d,q} mem, %ymmM, %ymmN
4612 -> VEX VOP mem, %ymmM, %ymmN (M and N < 16)
4613 */
4614 for (j = 0; j < i.operands; j++)
4615 if (operand_type_check (i.types[j], disp)
4616 && i.op[j].disps->X_op == O_constant)
4617 {
4618 /* Since the VEX prefix has 2 or 3 bytes, the EVEX prefix
4619 has 4 bytes, EVEX Disp8 has 1 byte and VEX Disp32 has 4
4620 bytes, we choose EVEX Disp8 over VEX Disp32. */
4621 int evex_disp8, vex_disp8;
4622 unsigned int memshift = i.memshift;
4623 offsetT n = i.op[j].disps->X_add_number;
4624
4625 evex_disp8 = fits_in_disp8 (n);
4626 i.memshift = 0;
4627 vex_disp8 = fits_in_disp8 (n);
4628 if (evex_disp8 != vex_disp8)
4629 {
4630 i.memshift = memshift;
4631 return;
4632 }
4633
4634 i.types[j].bitfield.disp8 = vex_disp8;
4635 break;
4636 }
4637 if ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4638 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2)
4639 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
4640 i.tm.opcode_modifier.vex
4641 = i.types[0].bitfield.ymmword ? VEX256 : VEX128;
4642 i.tm.opcode_modifier.vexw = VEXW0;
4643 /* VPAND, VPOR, and VPXOR are commutative. */
4644 if (i.reg_operands == 3 && i.tm.base_opcode != 0xdf)
4645 i.tm.opcode_modifier.commutative = 1;
4646 i.tm.opcode_modifier.evex = 0;
4647 i.tm.opcode_modifier.masking = 0;
4648 i.tm.opcode_modifier.broadcast = 0;
4649 i.tm.opcode_modifier.disp8memshift = 0;
4650 i.memshift = 0;
4651 if (j < i.operands)
4652 i.types[j].bitfield.disp8
4653 = fits_in_disp8 (i.op[j].disps->X_add_number);
4654 }
4655 else if (optimize_for_space
4656 && i.tm.base_opcode == 0x29
4657 && i.tm.opcode_space == SPACE_0F38
4658 && i.operands == i.reg_operands
4659 && i.op[0].regs == i.op[1].regs
4660 && (!i.tm.opcode_modifier.vex
4661 || !(i.op[0].regs->reg_flags & RegRex))
4662 && !is_evex_encoding (&i.tm))
4663 {
4664 /* Optimize: -Os:
4665 pcmpeqq %xmmN, %xmmN -> pcmpeqd %xmmN, %xmmN
4666 vpcmpeqq %xmmN, %xmmN, %xmmM -> vpcmpeqd %xmmN, %xmmN, %xmmM (N < 8)
4667 vpcmpeqq %ymmN, %ymmN, %ymmM -> vpcmpeqd %ymmN, %ymmN, %ymmM (N < 8)
4668 */
4669 i.tm.opcode_space = SPACE_0F;
4670 i.tm.base_opcode = 0x76;
4671 }
4672 else if (((i.tm.base_opcode >= 0x64
4673 && i.tm.base_opcode <= 0x66
4674 && i.tm.opcode_space == SPACE_0F)
4675 || (i.tm.base_opcode == 0x37
4676 && i.tm.opcode_space == SPACE_0F38))
4677 && i.operands == i.reg_operands
4678 && i.op[0].regs == i.op[1].regs
4679 && !is_evex_encoding (&i.tm))
4680 {
4681 /* Optimize: -O:
4682 pcmpgt[bwd] %mmN, %mmN -> pxor %mmN, %mmN
4683 pcmpgt[bwdq] %xmmN, %xmmN -> pxor %xmmN, %xmmN
4684 vpcmpgt[bwdq] %xmmN, %xmmN, %xmmM -> vpxor %xmmN, %xmmN, %xmmM (N < 8)
4685 vpcmpgt[bwdq] %xmmN, %xmmN, %xmmM -> vpxor %xmm0, %xmm0, %xmmM (N > 7)
4686 vpcmpgt[bwdq] %ymmN, %ymmN, %ymmM -> vpxor %ymmN, %ymmN, %ymmM (N < 8)
4687 vpcmpgt[bwdq] %ymmN, %ymmN, %ymmM -> vpxor %ymm0, %ymm0, %ymmM (N > 7)
4688 */
4689 i.tm.opcode_space = SPACE_0F;
4690 i.tm.base_opcode = 0xef;
4691 if (i.tm.opcode_modifier.vex && (i.op[0].regs->reg_flags & RegRex))
4692 {
4693 if (i.operands == 2)
4694 {
4695 gas_assert (i.tm.opcode_modifier.sse2avx);
4696
4697 i.operands = 3;
4698 i.reg_operands = 3;
4699 i.tm.operands = 3;
4700
4701 i.op[2].regs = i.op[0].regs;
4702 i.types[2] = i.types[0];
4703 i.flags[2] = i.flags[0];
4704 i.tm.operand_types[2] = i.tm.operand_types[0];
4705
4706 i.tm.opcode_modifier.sse2avx = 0;
4707 }
4708 i.op[0].regs -= i.op[0].regs->reg_num + 8;
4709 i.op[1].regs = i.op[0].regs;
4710 }
4711 }
4712 else if (optimize_for_space
4713 && i.tm.base_opcode == 0x59
4714 && i.tm.opcode_space == SPACE_0F38
4715 && i.operands == i.reg_operands
4716 && i.tm.opcode_modifier.vex
4717 && !(i.op[0].regs->reg_flags & RegRex)
4718 && i.op[0].regs->reg_type.bitfield.xmmword
4719 && i.vec_encoding != vex_encoding_vex3)
4720 {
4721 /* Optimize: -Os:
4722 vpbroadcastq %xmmN, %xmmM -> vpunpcklqdq %xmmN, %xmmN, %xmmM (N < 8)
4723 */
4724 i.tm.opcode_space = SPACE_0F;
4725 i.tm.base_opcode = 0x6c;
4726 i.tm.opcode_modifier.vexvvvv = 1;
4727
4728 ++i.operands;
4729 ++i.reg_operands;
4730 ++i.tm.operands;
4731
4732 i.op[2].regs = i.op[0].regs;
4733 i.types[2] = i.types[0];
4734 i.flags[2] = i.flags[0];
4735 i.tm.operand_types[2] = i.tm.operand_types[0];
4736
4737 swap_2_operands (1, 2);
4738 }
4739 }
4740
4741 /* Return non-zero for load instruction. */
4742
4743 static int
4744 load_insn_p (void)
4745 {
4746 unsigned int dest;
4747 int any_vex_p = is_any_vex_encoding (&i.tm);
4748 unsigned int base_opcode = i.tm.base_opcode | 1;
4749
4750 if (!any_vex_p)
4751 {
4752 /* Anysize insns: lea, invlpg, clflush, prefetch*, bndmk, bndcl, bndcu,
4753 bndcn, bndstx, bndldx, clflushopt, clwb, cldemote. */
4754 if (i.tm.opcode_modifier.operandconstraint == ANY_SIZE)
4755 return 0;
4756
4757 /* pop. */
4758 if (i.tm.mnem_off == MN_pop)
4759 return 1;
4760 }
4761
4762 if (i.tm.opcode_space == SPACE_BASE)
4763 {
4764 /* popf, popa. */
4765 if (i.tm.base_opcode == 0x9d
4766 || i.tm.base_opcode == 0x61)
4767 return 1;
4768
4769 /* movs, cmps, lods, scas. */
4770 if ((i.tm.base_opcode | 0xb) == 0xaf)
4771 return 1;
4772
4773 /* outs, xlatb. */
4774 if (base_opcode == 0x6f
4775 || i.tm.base_opcode == 0xd7)
4776 return 1;
4777 /* NB: For AMD-specific insns with implicit memory operands,
4778 they're intentionally not covered. */
4779 }
4780
4781 /* No memory operand. */
4782 if (!i.mem_operands)
4783 return 0;
4784
4785 if (any_vex_p)
4786 {
4787 if (i.tm.mnem_off == MN_vldmxcsr)
4788 return 1;
4789 }
4790 else if (i.tm.opcode_space == SPACE_BASE)
4791 {
4792 /* test, not, neg, mul, imul, div, idiv. */
4793 if (base_opcode == 0xf7 && i.tm.extension_opcode != 1)
4794 return 1;
4795
4796 /* inc, dec. */
4797 if (base_opcode == 0xff && i.tm.extension_opcode <= 1)
4798 return 1;
4799
4800 /* add, or, adc, sbb, and, sub, xor, cmp. */
4801 if (i.tm.base_opcode >= 0x80 && i.tm.base_opcode <= 0x83)
4802 return 1;
4803
4804 /* rol, ror, rcl, rcr, shl/sal, shr, sar. */
4805 if ((base_opcode == 0xc1 || (base_opcode | 2) == 0xd3)
4806 && i.tm.extension_opcode != 6)
4807 return 1;
4808
4809 /* Check for x87 instructions. */
4810 if ((base_opcode | 6) == 0xdf)
4811 {
4812 /* Skip fst, fstp, fstenv, fstcw. */
4813 if (i.tm.base_opcode == 0xd9
4814 && (i.tm.extension_opcode == 2
4815 || i.tm.extension_opcode == 3
4816 || i.tm.extension_opcode == 6
4817 || i.tm.extension_opcode == 7))
4818 return 0;
4819
4820 /* Skip fisttp, fist, fistp, fstp. */
4821 if (i.tm.base_opcode == 0xdb
4822 && (i.tm.extension_opcode == 1
4823 || i.tm.extension_opcode == 2
4824 || i.tm.extension_opcode == 3
4825 || i.tm.extension_opcode == 7))
4826 return 0;
4827
4828 /* Skip fisttp, fst, fstp, fsave, fstsw. */
4829 if (i.tm.base_opcode == 0xdd
4830 && (i.tm.extension_opcode == 1
4831 || i.tm.extension_opcode == 2
4832 || i.tm.extension_opcode == 3
4833 || i.tm.extension_opcode == 6
4834 || i.tm.extension_opcode == 7))
4835 return 0;
4836
4837 /* Skip fisttp, fist, fistp, fbstp, fistp. */
4838 if (i.tm.base_opcode == 0xdf
4839 && (i.tm.extension_opcode == 1
4840 || i.tm.extension_opcode == 2
4841 || i.tm.extension_opcode == 3
4842 || i.tm.extension_opcode == 6
4843 || i.tm.extension_opcode == 7))
4844 return 0;
4845
4846 return 1;
4847 }
4848 }
4849 else if (i.tm.opcode_space == SPACE_0F)
4850 {
4851 /* bt, bts, btr, btc. */
4852 if (i.tm.base_opcode == 0xba
4853 && (i.tm.extension_opcode | 3) == 7)
4854 return 1;
4855
4856 /* cmpxchg8b, cmpxchg16b, xrstors, vmptrld. */
4857 if (i.tm.base_opcode == 0xc7
4858 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
4859 && (i.tm.extension_opcode == 1 || i.tm.extension_opcode == 3
4860 || i.tm.extension_opcode == 6))
4861 return 1;
4862
4863 /* fxrstor, ldmxcsr, xrstor. */
4864 if (i.tm.base_opcode == 0xae
4865 && (i.tm.extension_opcode == 1
4866 || i.tm.extension_opcode == 2
4867 || i.tm.extension_opcode == 5))
4868 return 1;
4869
4870 /* lgdt, lidt, lmsw. */
4871 if (i.tm.base_opcode == 0x01
4872 && (i.tm.extension_opcode == 2
4873 || i.tm.extension_opcode == 3
4874 || i.tm.extension_opcode == 6))
4875 return 1;
4876 }
4877
4878 dest = i.operands - 1;
4879
4880 /* Check fake imm8 operand and 3 source operands. */
4881 if ((i.tm.opcode_modifier.immext
4882 || i.reg_operands + i.mem_operands == 4)
4883 && i.types[dest].bitfield.imm8)
4884 dest--;
4885
4886 /* add, or, adc, sbb, and, sub, xor, cmp, test, xchg. */
4887 if (i.tm.opcode_space == SPACE_BASE
4888 && ((base_opcode | 0x38) == 0x39
4889 || (base_opcode | 2) == 0x87))
4890 return 1;
4891
4892 if (i.tm.mnem_off == MN_xadd)
4893 return 1;
4894
4895 /* Check for load instruction. */
4896 return (i.types[dest].bitfield.class != ClassNone
4897 || i.types[dest].bitfield.instance == Accum);
4898 }
4899
4900 /* Output lfence, 0xfaee8, after instruction. */
4901
4902 static void
4903 insert_lfence_after (void)
4904 {
4905 if (lfence_after_load && load_insn_p ())
4906 {
4907 /* There are also two REP string instructions that require
4908 special treatment. Specifically, the compare string (CMPS)
4909 and scan string (SCAS) instructions set EFLAGS in a manner
4910 that depends on the data being compared/scanned. When used
4911 with a REP prefix, the number of iterations may therefore
4912 vary depending on this data. If the data is a program secret
4913 chosen by the adversary using an LVI method,
4914 then this data-dependent behavior may leak some aspect
4915 of the secret. */
4916 if (((i.tm.base_opcode | 0x9) == 0xaf)
4917 && i.prefix[REP_PREFIX])
4918 {
4919 as_warn (_("`%s` changes flags which would affect control flow behavior"),
4920 insn_name (&i.tm));
4921 }
4922 char *p = frag_more (3);
4923 *p++ = 0xf;
4924 *p++ = 0xae;
4925 *p = 0xe8;
4926 }
4927 }
4928
4929 /* Output lfence, 0xfaee8, before instruction. */
4930
4931 static void
4932 insert_lfence_before (void)
4933 {
4934 char *p;
4935
4936 if (i.tm.opcode_space != SPACE_BASE)
4937 return;
4938
4939 if (i.tm.base_opcode == 0xff
4940 && (i.tm.extension_opcode == 2 || i.tm.extension_opcode == 4))
4941 {
4942 /* Insert lfence before indirect branch if needed. */
4943
4944 if (lfence_before_indirect_branch == lfence_branch_none)
4945 return;
4946
4947 if (i.operands != 1)
4948 abort ();
4949
4950 if (i.reg_operands == 1)
4951 {
4952 /* Indirect branch via register. Don't insert lfence with
4953 -mlfence-after-load=yes. */
4954 if (lfence_after_load
4955 || lfence_before_indirect_branch == lfence_branch_memory)
4956 return;
4957 }
4958 else if (i.mem_operands == 1
4959 && lfence_before_indirect_branch != lfence_branch_register)
4960 {
4961 as_warn (_("indirect `%s` with memory operand should be avoided"),
4962 insn_name (&i.tm));
4963 return;
4964 }
4965 else
4966 return;
4967
4968 if (last_insn.kind != last_insn_other
4969 && last_insn.seg == now_seg)
4970 {
4971 as_warn_where (last_insn.file, last_insn.line,
4972 _("`%s` skips -mlfence-before-indirect-branch on `%s`"),
4973 last_insn.name, insn_name (&i.tm));
4974 return;
4975 }
4976
4977 p = frag_more (3);
4978 *p++ = 0xf;
4979 *p++ = 0xae;
4980 *p = 0xe8;
4981 return;
4982 }
4983
4984 /* Output or/not/shl and lfence before near ret. */
4985 if (lfence_before_ret != lfence_before_ret_none
4986 && (i.tm.base_opcode | 1) == 0xc3)
4987 {
4988 if (last_insn.kind != last_insn_other
4989 && last_insn.seg == now_seg)
4990 {
4991 as_warn_where (last_insn.file, last_insn.line,
4992 _("`%s` skips -mlfence-before-ret on `%s`"),
4993 last_insn.name, insn_name (&i.tm));
4994 return;
4995 }
4996
4997 /* Near ret ingore operand size override under CPU64. */
4998 char prefix = flag_code == CODE_64BIT
4999 ? 0x48
5000 : i.prefix[DATA_PREFIX] ? 0x66 : 0x0;
5001
5002 if (lfence_before_ret == lfence_before_ret_not)
5003 {
5004 /* not: 0xf71424, may add prefix
5005 for operand size override or 64-bit code. */
5006 p = frag_more ((prefix ? 2 : 0) + 6 + 3);
5007 if (prefix)
5008 *p++ = prefix;
5009 *p++ = 0xf7;
5010 *p++ = 0x14;
5011 *p++ = 0x24;
5012 if (prefix)
5013 *p++ = prefix;
5014 *p++ = 0xf7;
5015 *p++ = 0x14;
5016 *p++ = 0x24;
5017 }
5018 else
5019 {
5020 p = frag_more ((prefix ? 1 : 0) + 4 + 3);
5021 if (prefix)
5022 *p++ = prefix;
5023 if (lfence_before_ret == lfence_before_ret_or)
5024 {
5025 /* or: 0x830c2400, may add prefix
5026 for operand size override or 64-bit code. */
5027 *p++ = 0x83;
5028 *p++ = 0x0c;
5029 }
5030 else
5031 {
5032 /* shl: 0xc1242400, may add prefix
5033 for operand size override or 64-bit code. */
5034 *p++ = 0xc1;
5035 *p++ = 0x24;
5036 }
5037
5038 *p++ = 0x24;
5039 *p++ = 0x0;
5040 }
5041
5042 *p++ = 0xf;
5043 *p++ = 0xae;
5044 *p = 0xe8;
5045 }
5046 }
5047
5048 /* Shared helper for md_assemble() and s_insn(). */
5049 static void init_globals (void)
5050 {
5051 unsigned int j;
5052
5053 memset (&i, '\0', sizeof (i));
5054 i.rounding.type = rc_none;
5055 for (j = 0; j < MAX_OPERANDS; j++)
5056 i.reloc[j] = NO_RELOC;
5057 memset (disp_expressions, '\0', sizeof (disp_expressions));
5058 memset (im_expressions, '\0', sizeof (im_expressions));
5059 save_stack_p = save_stack;
5060 }
5061
5062 /* Helper for md_assemble() to decide whether to prepare for a possible 2nd
5063 parsing pass. Instead of introducing a rarely use new insn attribute this
5064 utilizes a common pattern between affected templates. It is deemed
5065 acceptable that this will lead to unnecessary pass 2 preparations in a
5066 limited set of cases. */
5067 static INLINE bool may_need_pass2 (const insn_template *t)
5068 {
5069 return t->opcode_modifier.sse2avx
5070 /* Note that all SSE2AVX templates have at least one operand. */
5071 ? t->operand_types[t->operands - 1].bitfield.class == RegSIMD
5072 : (t->opcode_space == SPACE_0F
5073 && (t->base_opcode | 1) == 0xbf)
5074 || (t->opcode_space == SPACE_BASE
5075 && t->base_opcode == 0x63);
5076 }
5077
5078 /* This is the guts of the machine-dependent assembler. LINE points to a
5079 machine dependent instruction. This function is supposed to emit
5080 the frags/bytes it assembles to. */
5081
5082 void
5083 md_assemble (char *line)
5084 {
5085 unsigned int j;
5086 char mnemonic[MAX_MNEM_SIZE], mnem_suffix = 0, *copy = NULL;
5087 const char *end, *pass1_mnem = NULL;
5088 enum i386_error pass1_err = 0;
5089 const insn_template *t;
5090
5091 /* Initialize globals. */
5092 current_templates = NULL;
5093 retry:
5094 init_globals ();
5095
5096 /* First parse an instruction mnemonic & call i386_operand for the operands.
5097 We assume that the scrubber has arranged it so that line[0] is the valid
5098 start of a (possibly prefixed) mnemonic. */
5099
5100 end = parse_insn (line, mnemonic, false);
5101 if (end == NULL)
5102 {
5103 if (pass1_mnem != NULL)
5104 goto match_error;
5105 if (i.error != no_error)
5106 {
5107 gas_assert (current_templates != NULL);
5108 if (may_need_pass2 (current_templates->start) && !i.suffix)
5109 goto no_match;
5110 /* No point in trying a 2nd pass - it'll only find the same suffix
5111 again. */
5112 mnem_suffix = i.suffix;
5113 goto match_error;
5114 }
5115 return;
5116 }
5117 t = current_templates->start;
5118 if (may_need_pass2 (t))
5119 {
5120 /* Make a copy of the full line in case we need to retry. */
5121 copy = xstrdup (line);
5122 }
5123 line += end - line;
5124 mnem_suffix = i.suffix;
5125
5126 line = parse_operands (line, mnemonic);
5127 this_operand = -1;
5128 if (line == NULL)
5129 {
5130 free (copy);
5131 return;
5132 }
5133
5134 /* Now we've parsed the mnemonic into a set of templates, and have the
5135 operands at hand. */
5136
5137 /* All Intel opcodes have reversed operands except for "bound", "enter",
5138 "invlpg*", "monitor*", "mwait*", "tpause", "umwait", "pvalidate",
5139 "rmpadjust", "rmpupdate", and "rmpquery". We also don't reverse
5140 intersegment "jmp" and "call" instructions with 2 immediate operands so
5141 that the immediate segment precedes the offset consistently in Intel and
5142 AT&T modes. */
5143 if (intel_syntax
5144 && i.operands > 1
5145 && (t->mnem_off != MN_bound)
5146 && !startswith (mnemonic, "invlpg")
5147 && !startswith (mnemonic, "monitor")
5148 && !startswith (mnemonic, "mwait")
5149 && (t->mnem_off != MN_pvalidate)
5150 && !startswith (mnemonic, "rmp")
5151 && (t->mnem_off != MN_tpause)
5152 && (t->mnem_off != MN_umwait)
5153 && !(i.operands == 2
5154 && operand_type_check (i.types[0], imm)
5155 && operand_type_check (i.types[1], imm)))
5156 swap_operands ();
5157
5158 /* The order of the immediates should be reversed
5159 for 2 immediates extrq and insertq instructions */
5160 if (i.imm_operands == 2
5161 && (t->mnem_off == MN_extrq || t->mnem_off == MN_insertq))
5162 swap_2_operands (0, 1);
5163
5164 if (i.imm_operands)
5165 optimize_imm ();
5166
5167 if (i.disp_operands && !optimize_disp (t))
5168 return;
5169
5170 /* Next, we find a template that matches the given insn,
5171 making sure the overlap of the given operands types is consistent
5172 with the template operand types. */
5173
5174 if (!(t = match_template (mnem_suffix)))
5175 {
5176 const char *err_msg;
5177
5178 if (copy && !mnem_suffix)
5179 {
5180 line = copy;
5181 copy = NULL;
5182 no_match:
5183 pass1_err = i.error;
5184 pass1_mnem = insn_name (current_templates->start);
5185 goto retry;
5186 }
5187
5188 /* If a non-/only-64bit template (group) was found in pass 1, and if
5189 _some_ template (group) was found in pass 2, squash pass 1's
5190 error. */
5191 if (pass1_err == unsupported_64bit)
5192 pass1_mnem = NULL;
5193
5194 match_error:
5195 free (copy);
5196
5197 switch (pass1_mnem ? pass1_err : i.error)
5198 {
5199 default:
5200 abort ();
5201 case operand_size_mismatch:
5202 err_msg = _("operand size mismatch");
5203 break;
5204 case operand_type_mismatch:
5205 err_msg = _("operand type mismatch");
5206 break;
5207 case register_type_mismatch:
5208 err_msg = _("register type mismatch");
5209 break;
5210 case number_of_operands_mismatch:
5211 err_msg = _("number of operands mismatch");
5212 break;
5213 case invalid_instruction_suffix:
5214 err_msg = _("invalid instruction suffix");
5215 break;
5216 case bad_imm4:
5217 err_msg = _("constant doesn't fit in 4 bits");
5218 break;
5219 case unsupported_with_intel_mnemonic:
5220 err_msg = _("unsupported with Intel mnemonic");
5221 break;
5222 case unsupported_syntax:
5223 err_msg = _("unsupported syntax");
5224 break;
5225 case unsupported:
5226 as_bad (_("unsupported instruction `%s'"),
5227 pass1_mnem ? pass1_mnem : insn_name (current_templates->start));
5228 return;
5229 case unsupported_on_arch:
5230 as_bad (_("`%s' is not supported on `%s%s'"),
5231 pass1_mnem ? pass1_mnem : insn_name (current_templates->start),
5232 cpu_arch_name ? cpu_arch_name : default_arch,
5233 cpu_sub_arch_name ? cpu_sub_arch_name : "");
5234 return;
5235 case unsupported_64bit:
5236 if (ISLOWER (mnem_suffix))
5237 {
5238 if (flag_code == CODE_64BIT)
5239 as_bad (_("`%s%c' is not supported in 64-bit mode"),
5240 pass1_mnem ? pass1_mnem : insn_name (current_templates->start),
5241 mnem_suffix);
5242 else
5243 as_bad (_("`%s%c' is only supported in 64-bit mode"),
5244 pass1_mnem ? pass1_mnem : insn_name (current_templates->start),
5245 mnem_suffix);
5246 }
5247 else
5248 {
5249 if (flag_code == CODE_64BIT)
5250 as_bad (_("`%s' is not supported in 64-bit mode"),
5251 pass1_mnem ? pass1_mnem : insn_name (current_templates->start));
5252 else
5253 as_bad (_("`%s' is only supported in 64-bit mode"),
5254 pass1_mnem ? pass1_mnem : insn_name (current_templates->start));
5255 }
5256 return;
5257 case invalid_sib_address:
5258 err_msg = _("invalid SIB address");
5259 break;
5260 case invalid_vsib_address:
5261 err_msg = _("invalid VSIB address");
5262 break;
5263 case invalid_vector_register_set:
5264 err_msg = _("mask, index, and destination registers must be distinct");
5265 break;
5266 case invalid_tmm_register_set:
5267 err_msg = _("all tmm registers must be distinct");
5268 break;
5269 case invalid_dest_and_src_register_set:
5270 err_msg = _("destination and source registers must be distinct");
5271 break;
5272 case unsupported_vector_index_register:
5273 err_msg = _("unsupported vector index register");
5274 break;
5275 case unsupported_broadcast:
5276 err_msg = _("unsupported broadcast");
5277 break;
5278 case broadcast_needed:
5279 err_msg = _("broadcast is needed for operand of such type");
5280 break;
5281 case unsupported_masking:
5282 err_msg = _("unsupported masking");
5283 break;
5284 case mask_not_on_destination:
5285 err_msg = _("mask not on destination operand");
5286 break;
5287 case no_default_mask:
5288 err_msg = _("default mask isn't allowed");
5289 break;
5290 case unsupported_rc_sae:
5291 err_msg = _("unsupported static rounding/sae");
5292 break;
5293 case invalid_register_operand:
5294 err_msg = _("invalid register operand");
5295 break;
5296 }
5297 as_bad (_("%s for `%s'"), err_msg,
5298 pass1_mnem ? pass1_mnem : insn_name (current_templates->start));
5299 return;
5300 }
5301
5302 free (copy);
5303
5304 if (sse_check != check_none
5305 /* The opcode space check isn't strictly needed; it's there only to
5306 bypass the logic below when easily possible. */
5307 && t->opcode_space >= SPACE_0F
5308 && t->opcode_space <= SPACE_0F3A
5309 && !is_cpu (&i.tm, CpuSSE4a)
5310 && !is_any_vex_encoding (t))
5311 {
5312 bool simd = false;
5313
5314 for (j = 0; j < t->operands; ++j)
5315 {
5316 if (t->operand_types[j].bitfield.class == RegMMX)
5317 break;
5318 if (t->operand_types[j].bitfield.class == RegSIMD)
5319 simd = true;
5320 }
5321
5322 if (j >= t->operands && simd)
5323 (sse_check == check_warning
5324 ? as_warn
5325 : as_bad) (_("SSE instruction `%s' is used"), insn_name (&i.tm));
5326 }
5327
5328 if (i.tm.opcode_modifier.fwait)
5329 if (!add_prefix (FWAIT_OPCODE))
5330 return;
5331
5332 /* Check if REP prefix is OK. */
5333 if (i.rep_prefix && i.tm.opcode_modifier.prefixok != PrefixRep)
5334 {
5335 as_bad (_("invalid instruction `%s' after `%s'"),
5336 insn_name (&i.tm), i.rep_prefix);
5337 return;
5338 }
5339
5340 /* Check for lock without a lockable instruction. Destination operand
5341 must be memory unless it is xchg (0x86). */
5342 if (i.prefix[LOCK_PREFIX])
5343 {
5344 if (i.tm.opcode_modifier.prefixok < PrefixLock
5345 || i.mem_operands == 0
5346 || (i.tm.base_opcode != 0x86
5347 && !(i.flags[i.operands - 1] & Operand_Mem)))
5348 {
5349 as_bad (_("expecting lockable instruction after `lock'"));
5350 return;
5351 }
5352
5353 /* Zap the redundant prefix from XCHG when optimizing. */
5354 if (i.tm.base_opcode == 0x86 && optimize && !i.no_optimize)
5355 i.prefix[LOCK_PREFIX] = 0;
5356 }
5357
5358 if (is_any_vex_encoding (&i.tm)
5359 || i.tm.operand_types[i.imm_operands].bitfield.class >= RegMMX
5360 || i.tm.operand_types[i.imm_operands + 1].bitfield.class >= RegMMX)
5361 {
5362 /* Check for data size prefix on VEX/XOP/EVEX encoded and SIMD insns. */
5363 if (i.prefix[DATA_PREFIX])
5364 {
5365 as_bad (_("data size prefix invalid with `%s'"), insn_name (&i.tm));
5366 return;
5367 }
5368
5369 /* Don't allow e.g. KMOV in TLS code sequences. */
5370 for (j = i.imm_operands; j < i.operands; ++j)
5371 switch (i.reloc[j])
5372 {
5373 case BFD_RELOC_386_TLS_GOTIE:
5374 case BFD_RELOC_386_TLS_LE_32:
5375 case BFD_RELOC_X86_64_GOTTPOFF:
5376 case BFD_RELOC_X86_64_TLSLD:
5377 as_bad (_("TLS relocation cannot be used with `%s'"), insn_name (&i.tm));
5378 return;
5379 default:
5380 break;
5381 }
5382 }
5383
5384 /* Check if HLE prefix is OK. */
5385 if (i.hle_prefix && !check_hle ())
5386 return;
5387
5388 /* Check BND prefix. */
5389 if (i.bnd_prefix && !i.tm.opcode_modifier.bndprefixok)
5390 as_bad (_("expecting valid branch instruction after `bnd'"));
5391
5392 /* Check NOTRACK prefix. */
5393 if (i.notrack_prefix && i.tm.opcode_modifier.prefixok != PrefixNoTrack)
5394 as_bad (_("expecting indirect branch instruction after `notrack'"));
5395
5396 if (is_cpu (&i.tm, CpuMPX))
5397 {
5398 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
5399 as_bad (_("32-bit address isn't allowed in 64-bit MPX instructions."));
5400 else if (flag_code != CODE_16BIT
5401 ? i.prefix[ADDR_PREFIX]
5402 : i.mem_operands && !i.prefix[ADDR_PREFIX])
5403 as_bad (_("16-bit address isn't allowed in MPX instructions"));
5404 }
5405
5406 /* Insert BND prefix. */
5407 if (add_bnd_prefix && i.tm.opcode_modifier.bndprefixok)
5408 {
5409 if (!i.prefix[BND_PREFIX])
5410 add_prefix (BND_PREFIX_OPCODE);
5411 else if (i.prefix[BND_PREFIX] != BND_PREFIX_OPCODE)
5412 {
5413 as_warn (_("replacing `rep'/`repe' prefix by `bnd'"));
5414 i.prefix[BND_PREFIX] = BND_PREFIX_OPCODE;
5415 }
5416 }
5417
5418 /* Check string instruction segment overrides. */
5419 if (i.tm.opcode_modifier.isstring >= IS_STRING_ES_OP0)
5420 {
5421 gas_assert (i.mem_operands);
5422 if (!check_string ())
5423 return;
5424 i.disp_operands = 0;
5425 }
5426
5427 /* The memory operand of (%dx) should be only used with input/output
5428 instructions (base opcodes: 0x6c, 0x6e, 0xec, 0xee). */
5429 if (i.input_output_operand
5430 && ((i.tm.base_opcode | 0x82) != 0xee
5431 || i.tm.opcode_space != SPACE_BASE))
5432 {
5433 as_bad (_("input/output port address isn't allowed with `%s'"),
5434 insn_name (&i.tm));
5435 return;
5436 }
5437
5438 if (optimize && !i.no_optimize && i.tm.opcode_modifier.optimize)
5439 optimize_encoding ();
5440
5441 if (use_unaligned_vector_move)
5442 encode_with_unaligned_vector_move ();
5443
5444 if (!process_suffix ())
5445 return;
5446
5447 /* Check if IP-relative addressing requirements can be satisfied. */
5448 if (is_cpu (&i.tm, CpuPREFETCHI)
5449 && !(i.base_reg && i.base_reg->reg_num == RegIP))
5450 as_warn (_("'%s' only supports RIP-relative address"), insn_name (&i.tm));
5451
5452 /* Update operand types and check extended states. */
5453 for (j = 0; j < i.operands; j++)
5454 {
5455 i.types[j] = operand_type_and (i.types[j], i.tm.operand_types[j]);
5456 switch (i.tm.operand_types[j].bitfield.class)
5457 {
5458 default:
5459 break;
5460 case RegMMX:
5461 i.xstate |= xstate_mmx;
5462 break;
5463 case RegMask:
5464 i.xstate |= xstate_mask;
5465 break;
5466 case RegSIMD:
5467 if (i.tm.operand_types[j].bitfield.tmmword)
5468 i.xstate |= xstate_tmm;
5469 else if (i.tm.operand_types[j].bitfield.zmmword
5470 && vector_size >= VSZ512)
5471 i.xstate |= xstate_zmm;
5472 else if (i.tm.operand_types[j].bitfield.ymmword
5473 && vector_size >= VSZ256)
5474 i.xstate |= xstate_ymm;
5475 else if (i.tm.operand_types[j].bitfield.xmmword)
5476 i.xstate |= xstate_xmm;
5477 break;
5478 }
5479 }
5480
5481 /* Make still unresolved immediate matches conform to size of immediate
5482 given in i.suffix. */
5483 if (!finalize_imm ())
5484 return;
5485
5486 if (i.types[0].bitfield.imm1)
5487 i.imm_operands = 0; /* kludge for shift insns. */
5488
5489 /* For insns with operands there are more diddles to do to the opcode. */
5490 if (i.operands)
5491 {
5492 if (!process_operands ())
5493 return;
5494 }
5495 else if (!quiet_warnings && i.tm.opcode_modifier.operandconstraint == UGH)
5496 {
5497 /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc. */
5498 as_warn (_("translating to `%sp'"), insn_name (&i.tm));
5499 }
5500
5501 if (is_any_vex_encoding (&i.tm))
5502 {
5503 if (!cpu_arch_flags.bitfield.cpui286)
5504 {
5505 as_bad (_("instruction `%s' isn't supported outside of protected mode."),
5506 insn_name (&i.tm));
5507 return;
5508 }
5509
5510 /* Check for explicit REX prefix. */
5511 if (i.prefix[REX_PREFIX] || i.rex_encoding)
5512 {
5513 as_bad (_("REX prefix invalid with `%s'"), insn_name (&i.tm));
5514 return;
5515 }
5516
5517 if (i.tm.opcode_modifier.vex)
5518 build_vex_prefix (t);
5519 else
5520 build_evex_prefix ();
5521
5522 /* The individual REX.RXBW bits got consumed. */
5523 i.rex &= REX_OPCODE;
5524 }
5525
5526 /* Handle conversion of 'int $3' --> special int3 insn. */
5527 if (i.tm.mnem_off == MN_int
5528 && i.op[0].imms->X_add_number == 3)
5529 {
5530 i.tm.base_opcode = INT3_OPCODE;
5531 i.imm_operands = 0;
5532 }
5533
5534 if ((i.tm.opcode_modifier.jump == JUMP
5535 || i.tm.opcode_modifier.jump == JUMP_BYTE
5536 || i.tm.opcode_modifier.jump == JUMP_DWORD)
5537 && i.op[0].disps->X_op == O_constant)
5538 {
5539 /* Convert "jmp constant" (and "call constant") to a jump (call) to
5540 the absolute address given by the constant. Since ix86 jumps and
5541 calls are pc relative, we need to generate a reloc. */
5542 i.op[0].disps->X_add_symbol = &abs_symbol;
5543 i.op[0].disps->X_op = O_symbol;
5544 }
5545
5546 /* For 8 bit registers we need an empty rex prefix. Also if the
5547 instruction already has a prefix, we need to convert old
5548 registers to new ones. */
5549
5550 if ((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte
5551 && (i.op[0].regs->reg_flags & RegRex64) != 0)
5552 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte
5553 && (i.op[1].regs->reg_flags & RegRex64) != 0)
5554 || (((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte)
5555 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte))
5556 && i.rex != 0))
5557 {
5558 int x;
5559
5560 i.rex |= REX_OPCODE;
5561 for (x = 0; x < 2; x++)
5562 {
5563 /* Look for 8 bit operand that uses old registers. */
5564 if (i.types[x].bitfield.class == Reg && i.types[x].bitfield.byte
5565 && (i.op[x].regs->reg_flags & RegRex64) == 0)
5566 {
5567 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
5568 /* In case it is "hi" register, give up. */
5569 if (i.op[x].regs->reg_num > 3)
5570 as_bad (_("can't encode register '%s%s' in an "
5571 "instruction requiring REX prefix."),
5572 register_prefix, i.op[x].regs->reg_name);
5573
5574 /* Otherwise it is equivalent to the extended register.
5575 Since the encoding doesn't change this is merely
5576 cosmetic cleanup for debug output. */
5577
5578 i.op[x].regs = i.op[x].regs + 8;
5579 }
5580 }
5581 }
5582
5583 if (i.rex == 0 && i.rex_encoding)
5584 {
5585 /* Check if we can add a REX_OPCODE byte. Look for 8 bit operand
5586 that uses legacy register. If it is "hi" register, don't add
5587 the REX_OPCODE byte. */
5588 int x;
5589 for (x = 0; x < 2; x++)
5590 if (i.types[x].bitfield.class == Reg
5591 && i.types[x].bitfield.byte
5592 && (i.op[x].regs->reg_flags & RegRex64) == 0
5593 && i.op[x].regs->reg_num > 3)
5594 {
5595 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
5596 i.rex_encoding = false;
5597 break;
5598 }
5599
5600 if (i.rex_encoding)
5601 i.rex = REX_OPCODE;
5602 }
5603
5604 if (i.rex != 0)
5605 add_prefix (REX_OPCODE | i.rex);
5606
5607 insert_lfence_before ();
5608
5609 /* We are ready to output the insn. */
5610 output_insn ();
5611
5612 insert_lfence_after ();
5613
5614 last_insn.seg = now_seg;
5615
5616 if (i.tm.opcode_modifier.isprefix)
5617 {
5618 last_insn.kind = last_insn_prefix;
5619 last_insn.name = insn_name (&i.tm);
5620 last_insn.file = as_where (&last_insn.line);
5621 }
5622 else
5623 last_insn.kind = last_insn_other;
5624 }
5625
5626 /* The Q suffix is generally valid only in 64-bit mode, with very few
5627 exceptions: fild, fistp, fisttp, and cmpxchg8b. Note that for fild
5628 and fisttp only one of their two templates is matched below: That's
5629 sufficient since other relevant attributes are the same between both
5630 respective templates. */
5631 static INLINE bool q_suffix_allowed(const insn_template *t)
5632 {
5633 return flag_code == CODE_64BIT
5634 || (t->opcode_space == SPACE_BASE
5635 && t->base_opcode == 0xdf
5636 && (t->extension_opcode & 1)) /* fild / fistp / fisttp */
5637 || t->mnem_off == MN_cmpxchg8b;
5638 }
5639
5640 static const char *
5641 parse_insn (const char *line, char *mnemonic, bool prefix_only)
5642 {
5643 const char *l = line, *token_start = l;
5644 char *mnem_p;
5645 bool pass1 = !current_templates;
5646 int supported;
5647 const insn_template *t;
5648 char *dot_p = NULL;
5649
5650 while (1)
5651 {
5652 mnem_p = mnemonic;
5653 /* Pseudo-prefixes start with an opening figure brace. */
5654 if ((*mnem_p = *l) == '{')
5655 {
5656 ++mnem_p;
5657 ++l;
5658 }
5659 while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
5660 {
5661 if (*mnem_p == '.')
5662 dot_p = mnem_p;
5663 mnem_p++;
5664 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
5665 {
5666 too_long:
5667 as_bad (_("no such instruction: `%s'"), token_start);
5668 return NULL;
5669 }
5670 l++;
5671 }
5672 /* Pseudo-prefixes end with a closing figure brace. */
5673 if (*mnemonic == '{' && *l == '}')
5674 {
5675 *mnem_p++ = *l++;
5676 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
5677 goto too_long;
5678 *mnem_p = '\0';
5679
5680 /* Point l at the closing brace if there's no other separator. */
5681 if (*l != END_OF_INSN && !is_space_char (*l)
5682 && *l != PREFIX_SEPARATOR)
5683 --l;
5684 }
5685 else if (!is_space_char (*l)
5686 && *l != END_OF_INSN
5687 && (intel_syntax
5688 || (*l != PREFIX_SEPARATOR && *l != ',')))
5689 {
5690 if (prefix_only)
5691 break;
5692 as_bad (_("invalid character %s in mnemonic"),
5693 output_invalid (*l));
5694 return NULL;
5695 }
5696 if (token_start == l)
5697 {
5698 if (!intel_syntax && *l == PREFIX_SEPARATOR)
5699 as_bad (_("expecting prefix; got nothing"));
5700 else
5701 as_bad (_("expecting mnemonic; got nothing"));
5702 return NULL;
5703 }
5704
5705 /* Look up instruction (or prefix) via hash table. */
5706 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
5707
5708 if (*l != END_OF_INSN
5709 && (!is_space_char (*l) || l[1] != END_OF_INSN)
5710 && current_templates
5711 && current_templates->start->opcode_modifier.isprefix)
5712 {
5713 if (!cpu_flags_check_cpu64 (current_templates->start))
5714 {
5715 as_bad ((flag_code != CODE_64BIT
5716 ? _("`%s' is only supported in 64-bit mode")
5717 : _("`%s' is not supported in 64-bit mode")),
5718 insn_name (current_templates->start));
5719 return NULL;
5720 }
5721 /* If we are in 16-bit mode, do not allow addr16 or data16.
5722 Similarly, in 32-bit mode, do not allow addr32 or data32. */
5723 if ((current_templates->start->opcode_modifier.size == SIZE16
5724 || current_templates->start->opcode_modifier.size == SIZE32)
5725 && flag_code != CODE_64BIT
5726 && ((current_templates->start->opcode_modifier.size == SIZE32)
5727 ^ (flag_code == CODE_16BIT)))
5728 {
5729 as_bad (_("redundant %s prefix"),
5730 insn_name (current_templates->start));
5731 return NULL;
5732 }
5733
5734 if (current_templates->start->base_opcode == PSEUDO_PREFIX)
5735 {
5736 /* Handle pseudo prefixes. */
5737 switch (current_templates->start->extension_opcode)
5738 {
5739 case Prefix_Disp8:
5740 /* {disp8} */
5741 i.disp_encoding = disp_encoding_8bit;
5742 break;
5743 case Prefix_Disp16:
5744 /* {disp16} */
5745 i.disp_encoding = disp_encoding_16bit;
5746 break;
5747 case Prefix_Disp32:
5748 /* {disp32} */
5749 i.disp_encoding = disp_encoding_32bit;
5750 break;
5751 case Prefix_Load:
5752 /* {load} */
5753 i.dir_encoding = dir_encoding_load;
5754 break;
5755 case Prefix_Store:
5756 /* {store} */
5757 i.dir_encoding = dir_encoding_store;
5758 break;
5759 case Prefix_VEX:
5760 /* {vex} */
5761 i.vec_encoding = vex_encoding_vex;
5762 break;
5763 case Prefix_VEX3:
5764 /* {vex3} */
5765 i.vec_encoding = vex_encoding_vex3;
5766 break;
5767 case Prefix_EVEX:
5768 /* {evex} */
5769 i.vec_encoding = vex_encoding_evex;
5770 break;
5771 case Prefix_REX:
5772 /* {rex} */
5773 i.rex_encoding = true;
5774 break;
5775 case Prefix_NoOptimize:
5776 /* {nooptimize} */
5777 i.no_optimize = true;
5778 break;
5779 default:
5780 abort ();
5781 }
5782 }
5783 else
5784 {
5785 /* Add prefix, checking for repeated prefixes. */
5786 switch (add_prefix (current_templates->start->base_opcode))
5787 {
5788 case PREFIX_EXIST:
5789 return NULL;
5790 case PREFIX_DS:
5791 if (is_cpu (current_templates->start, CpuIBT))
5792 i.notrack_prefix = insn_name (current_templates->start);
5793 break;
5794 case PREFIX_REP:
5795 if (is_cpu (current_templates->start, CpuHLE))
5796 i.hle_prefix = insn_name (current_templates->start);
5797 else if (is_cpu (current_templates->start, CpuMPX))
5798 i.bnd_prefix = insn_name (current_templates->start);
5799 else
5800 i.rep_prefix = insn_name (current_templates->start);
5801 break;
5802 default:
5803 break;
5804 }
5805 }
5806 /* Skip past PREFIX_SEPARATOR and reset token_start. */
5807 token_start = ++l;
5808 }
5809 else
5810 break;
5811 }
5812
5813 if (prefix_only)
5814 return token_start;
5815
5816 if (!current_templates)
5817 {
5818 /* Deprecated functionality (new code should use pseudo-prefixes instead):
5819 Check if we should swap operand or force 32bit displacement in
5820 encoding. */
5821 if (mnem_p - 2 == dot_p && dot_p[1] == 's')
5822 i.dir_encoding = dir_encoding_swap;
5823 else if (mnem_p - 3 == dot_p
5824 && dot_p[1] == 'd'
5825 && dot_p[2] == '8')
5826 i.disp_encoding = disp_encoding_8bit;
5827 else if (mnem_p - 4 == dot_p
5828 && dot_p[1] == 'd'
5829 && dot_p[2] == '3'
5830 && dot_p[3] == '2')
5831 i.disp_encoding = disp_encoding_32bit;
5832 else
5833 goto check_suffix;
5834 mnem_p = dot_p;
5835 *dot_p = '\0';
5836 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
5837 }
5838
5839 if (!current_templates || !pass1)
5840 {
5841 current_templates = NULL;
5842
5843 check_suffix:
5844 if (mnem_p > mnemonic)
5845 {
5846 /* See if we can get a match by trimming off a suffix. */
5847 switch (mnem_p[-1])
5848 {
5849 case WORD_MNEM_SUFFIX:
5850 if (intel_syntax && (intel_float_operand (mnemonic) & 2))
5851 i.suffix = SHORT_MNEM_SUFFIX;
5852 else
5853 /* Fall through. */
5854 case BYTE_MNEM_SUFFIX:
5855 case QWORD_MNEM_SUFFIX:
5856 i.suffix = mnem_p[-1];
5857 mnem_p[-1] = '\0';
5858 current_templates
5859 = (const templates *) str_hash_find (op_hash, mnemonic);
5860 break;
5861 case SHORT_MNEM_SUFFIX:
5862 case LONG_MNEM_SUFFIX:
5863 if (!intel_syntax)
5864 {
5865 i.suffix = mnem_p[-1];
5866 mnem_p[-1] = '\0';
5867 current_templates
5868 = (const templates *) str_hash_find (op_hash, mnemonic);
5869 }
5870 break;
5871
5872 /* Intel Syntax. */
5873 case 'd':
5874 if (intel_syntax)
5875 {
5876 if (intel_float_operand (mnemonic) == 1)
5877 i.suffix = SHORT_MNEM_SUFFIX;
5878 else
5879 i.suffix = LONG_MNEM_SUFFIX;
5880 mnem_p[-1] = '\0';
5881 current_templates
5882 = (const templates *) str_hash_find (op_hash, mnemonic);
5883 }
5884 /* For compatibility reasons accept MOVSD and CMPSD without
5885 operands even in AT&T mode. */
5886 else if (*l == END_OF_INSN
5887 || (is_space_char (*l) && l[1] == END_OF_INSN))
5888 {
5889 mnem_p[-1] = '\0';
5890 current_templates
5891 = (const templates *) str_hash_find (op_hash, mnemonic);
5892 if (current_templates != NULL
5893 /* MOVS or CMPS */
5894 && (current_templates->start->base_opcode | 2) == 0xa6
5895 && current_templates->start->opcode_space
5896 == SPACE_BASE
5897 && mnem_p[-2] == 's')
5898 {
5899 as_warn (_("found `%sd'; assuming `%sl' was meant"),
5900 mnemonic, mnemonic);
5901 i.suffix = LONG_MNEM_SUFFIX;
5902 }
5903 else
5904 {
5905 current_templates = NULL;
5906 mnem_p[-1] = 'd';
5907 }
5908 }
5909 break;
5910 }
5911 }
5912
5913 if (!current_templates)
5914 {
5915 if (pass1)
5916 as_bad (_("no such instruction: `%s'"), token_start);
5917 return NULL;
5918 }
5919 }
5920
5921 if (current_templates->start->opcode_modifier.jump == JUMP
5922 || current_templates->start->opcode_modifier.jump == JUMP_BYTE)
5923 {
5924 /* Check for a branch hint. We allow ",pt" and ",pn" for
5925 predict taken and predict not taken respectively.
5926 I'm not sure that branch hints actually do anything on loop
5927 and jcxz insns (JumpByte) for current Pentium4 chips. They
5928 may work in the future and it doesn't hurt to accept them
5929 now. */
5930 if (l[0] == ',' && l[1] == 'p')
5931 {
5932 if (l[2] == 't')
5933 {
5934 if (!add_prefix (DS_PREFIX_OPCODE))
5935 return NULL;
5936 l += 3;
5937 }
5938 else if (l[2] == 'n')
5939 {
5940 if (!add_prefix (CS_PREFIX_OPCODE))
5941 return NULL;
5942 l += 3;
5943 }
5944 }
5945 }
5946 /* Any other comma loses. */
5947 if (*l == ',')
5948 {
5949 as_bad (_("invalid character %s in mnemonic"),
5950 output_invalid (*l));
5951 return NULL;
5952 }
5953
5954 /* Check if instruction is supported on specified architecture. */
5955 supported = 0;
5956 for (t = current_templates->start; t < current_templates->end; ++t)
5957 {
5958 supported |= cpu_flags_match (t);
5959
5960 if (i.suffix == QWORD_MNEM_SUFFIX && !q_suffix_allowed (t))
5961 supported &= ~CPU_FLAGS_64BIT_MATCH;
5962
5963 if (supported == CPU_FLAGS_PERFECT_MATCH)
5964 return l;
5965 }
5966
5967 if (pass1)
5968 {
5969 if (supported & CPU_FLAGS_64BIT_MATCH)
5970 i.error = unsupported_on_arch;
5971 else
5972 i.error = unsupported_64bit;
5973 }
5974
5975 return NULL;
5976 }
5977
5978 static char *
5979 parse_operands (char *l, const char *mnemonic)
5980 {
5981 char *token_start;
5982
5983 /* 1 if operand is pending after ','. */
5984 unsigned int expecting_operand = 0;
5985
5986 while (*l != END_OF_INSN)
5987 {
5988 /* Non-zero if operand parens not balanced. */
5989 unsigned int paren_not_balanced = 0;
5990 /* True if inside double quotes. */
5991 bool in_quotes = false;
5992
5993 /* Skip optional white space before operand. */
5994 if (is_space_char (*l))
5995 ++l;
5996 if (!is_operand_char (*l) && *l != END_OF_INSN && *l != '"')
5997 {
5998 as_bad (_("invalid character %s before operand %d"),
5999 output_invalid (*l),
6000 i.operands + 1);
6001 return NULL;
6002 }
6003 token_start = l; /* After white space. */
6004 while (in_quotes || paren_not_balanced || *l != ',')
6005 {
6006 if (*l == END_OF_INSN)
6007 {
6008 if (in_quotes)
6009 {
6010 as_bad (_("unbalanced double quotes in operand %d."),
6011 i.operands + 1);
6012 return NULL;
6013 }
6014 if (paren_not_balanced)
6015 {
6016 know (!intel_syntax);
6017 as_bad (_("unbalanced parenthesis in operand %d."),
6018 i.operands + 1);
6019 return NULL;
6020 }
6021 else
6022 break; /* we are done */
6023 }
6024 else if (*l == '\\' && l[1] == '"')
6025 ++l;
6026 else if (*l == '"')
6027 in_quotes = !in_quotes;
6028 else if (!in_quotes && !is_operand_char (*l) && !is_space_char (*l))
6029 {
6030 as_bad (_("invalid character %s in operand %d"),
6031 output_invalid (*l),
6032 i.operands + 1);
6033 return NULL;
6034 }
6035 if (!intel_syntax && !in_quotes)
6036 {
6037 if (*l == '(')
6038 ++paren_not_balanced;
6039 if (*l == ')')
6040 --paren_not_balanced;
6041 }
6042 l++;
6043 }
6044 if (l != token_start)
6045 { /* Yes, we've read in another operand. */
6046 unsigned int operand_ok;
6047 this_operand = i.operands++;
6048 if (i.operands > MAX_OPERANDS)
6049 {
6050 as_bad (_("spurious operands; (%d operands/instruction max)"),
6051 MAX_OPERANDS);
6052 return NULL;
6053 }
6054 i.types[this_operand].bitfield.unspecified = 1;
6055 /* Now parse operand adding info to 'i' as we go along. */
6056 END_STRING_AND_SAVE (l);
6057
6058 if (i.mem_operands > 1)
6059 {
6060 as_bad (_("too many memory references for `%s'"),
6061 mnemonic);
6062 return 0;
6063 }
6064
6065 if (intel_syntax)
6066 operand_ok =
6067 i386_intel_operand (token_start,
6068 intel_float_operand (mnemonic));
6069 else
6070 operand_ok = i386_att_operand (token_start);
6071
6072 RESTORE_END_STRING (l);
6073 if (!operand_ok)
6074 return NULL;
6075 }
6076 else
6077 {
6078 if (expecting_operand)
6079 {
6080 expecting_operand_after_comma:
6081 as_bad (_("expecting operand after ','; got nothing"));
6082 return NULL;
6083 }
6084 if (*l == ',')
6085 {
6086 as_bad (_("expecting operand before ','; got nothing"));
6087 return NULL;
6088 }
6089 }
6090
6091 /* Now *l must be either ',' or END_OF_INSN. */
6092 if (*l == ',')
6093 {
6094 if (*++l == END_OF_INSN)
6095 {
6096 /* Just skip it, if it's \n complain. */
6097 goto expecting_operand_after_comma;
6098 }
6099 expecting_operand = 1;
6100 }
6101 }
6102 return l;
6103 }
6104
6105 static void
6106 swap_2_operands (unsigned int xchg1, unsigned int xchg2)
6107 {
6108 union i386_op temp_op;
6109 i386_operand_type temp_type;
6110 unsigned int temp_flags;
6111 enum bfd_reloc_code_real temp_reloc;
6112
6113 temp_type = i.types[xchg2];
6114 i.types[xchg2] = i.types[xchg1];
6115 i.types[xchg1] = temp_type;
6116
6117 temp_flags = i.flags[xchg2];
6118 i.flags[xchg2] = i.flags[xchg1];
6119 i.flags[xchg1] = temp_flags;
6120
6121 temp_op = i.op[xchg2];
6122 i.op[xchg2] = i.op[xchg1];
6123 i.op[xchg1] = temp_op;
6124
6125 temp_reloc = i.reloc[xchg2];
6126 i.reloc[xchg2] = i.reloc[xchg1];
6127 i.reloc[xchg1] = temp_reloc;
6128
6129 temp_flags = i.imm_bits[xchg2];
6130 i.imm_bits[xchg2] = i.imm_bits[xchg1];
6131 i.imm_bits[xchg1] = temp_flags;
6132
6133 if (i.mask.reg)
6134 {
6135 if (i.mask.operand == xchg1)
6136 i.mask.operand = xchg2;
6137 else if (i.mask.operand == xchg2)
6138 i.mask.operand = xchg1;
6139 }
6140 if (i.broadcast.type || i.broadcast.bytes)
6141 {
6142 if (i.broadcast.operand == xchg1)
6143 i.broadcast.operand = xchg2;
6144 else if (i.broadcast.operand == xchg2)
6145 i.broadcast.operand = xchg1;
6146 }
6147 }
6148
6149 static void
6150 swap_operands (void)
6151 {
6152 switch (i.operands)
6153 {
6154 case 5:
6155 case 4:
6156 swap_2_operands (1, i.operands - 2);
6157 /* Fall through. */
6158 case 3:
6159 case 2:
6160 swap_2_operands (0, i.operands - 1);
6161 break;
6162 default:
6163 abort ();
6164 }
6165
6166 if (i.mem_operands == 2)
6167 {
6168 const reg_entry *temp_seg;
6169 temp_seg = i.seg[0];
6170 i.seg[0] = i.seg[1];
6171 i.seg[1] = temp_seg;
6172 }
6173 }
6174
6175 /* Try to ensure constant immediates are represented in the smallest
6176 opcode possible. */
6177 static void
6178 optimize_imm (void)
6179 {
6180 char guess_suffix = 0;
6181 int op;
6182
6183 if (i.suffix)
6184 guess_suffix = i.suffix;
6185 else if (i.reg_operands)
6186 {
6187 /* Figure out a suffix from the last register operand specified.
6188 We can't do this properly yet, i.e. excluding special register
6189 instances, but the following works for instructions with
6190 immediates. In any case, we can't set i.suffix yet. */
6191 for (op = i.operands; --op >= 0;)
6192 if (i.types[op].bitfield.class != Reg)
6193 continue;
6194 else if (i.types[op].bitfield.byte)
6195 {
6196 guess_suffix = BYTE_MNEM_SUFFIX;
6197 break;
6198 }
6199 else if (i.types[op].bitfield.word)
6200 {
6201 guess_suffix = WORD_MNEM_SUFFIX;
6202 break;
6203 }
6204 else if (i.types[op].bitfield.dword)
6205 {
6206 guess_suffix = LONG_MNEM_SUFFIX;
6207 break;
6208 }
6209 else if (i.types[op].bitfield.qword)
6210 {
6211 guess_suffix = QWORD_MNEM_SUFFIX;
6212 break;
6213 }
6214 }
6215 else if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
6216 guess_suffix = WORD_MNEM_SUFFIX;
6217 else if (flag_code != CODE_64BIT
6218 || (!(i.prefix[REX_PREFIX] & REX_W)
6219 /* A more generic (but also more involved) way of dealing
6220 with the special case(s) would be to go look for
6221 DefaultSize attributes on any of the templates. */
6222 && current_templates->start->mnem_off != MN_push))
6223 guess_suffix = LONG_MNEM_SUFFIX;
6224
6225 for (op = i.operands; --op >= 0;)
6226 if (operand_type_check (i.types[op], imm))
6227 {
6228 switch (i.op[op].imms->X_op)
6229 {
6230 case O_constant:
6231 /* If a suffix is given, this operand may be shortened. */
6232 switch (guess_suffix)
6233 {
6234 case LONG_MNEM_SUFFIX:
6235 i.types[op].bitfield.imm32 = 1;
6236 i.types[op].bitfield.imm64 = 1;
6237 break;
6238 case WORD_MNEM_SUFFIX:
6239 i.types[op].bitfield.imm16 = 1;
6240 i.types[op].bitfield.imm32 = 1;
6241 i.types[op].bitfield.imm32s = 1;
6242 i.types[op].bitfield.imm64 = 1;
6243 break;
6244 case BYTE_MNEM_SUFFIX:
6245 i.types[op].bitfield.imm8 = 1;
6246 i.types[op].bitfield.imm8s = 1;
6247 i.types[op].bitfield.imm16 = 1;
6248 i.types[op].bitfield.imm32 = 1;
6249 i.types[op].bitfield.imm32s = 1;
6250 i.types[op].bitfield.imm64 = 1;
6251 break;
6252 }
6253
6254 /* If this operand is at most 16 bits, convert it
6255 to a signed 16 bit number before trying to see
6256 whether it will fit in an even smaller size.
6257 This allows a 16-bit operand such as $0xffe0 to
6258 be recognised as within Imm8S range. */
6259 if ((i.types[op].bitfield.imm16)
6260 && fits_in_unsigned_word (i.op[op].imms->X_add_number))
6261 {
6262 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
6263 ^ 0x8000) - 0x8000);
6264 }
6265 #ifdef BFD64
6266 /* Store 32-bit immediate in 64-bit for 64-bit BFD. */
6267 if ((i.types[op].bitfield.imm32)
6268 && fits_in_unsigned_long (i.op[op].imms->X_add_number))
6269 {
6270 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
6271 ^ ((offsetT) 1 << 31))
6272 - ((offsetT) 1 << 31));
6273 }
6274 #endif
6275 i.types[op]
6276 = operand_type_or (i.types[op],
6277 smallest_imm_type (i.op[op].imms->X_add_number));
6278
6279 /* We must avoid matching of Imm32 templates when 64bit
6280 only immediate is available. */
6281 if (guess_suffix == QWORD_MNEM_SUFFIX)
6282 i.types[op].bitfield.imm32 = 0;
6283 break;
6284
6285 case O_absent:
6286 case O_register:
6287 abort ();
6288
6289 /* Symbols and expressions. */
6290 default:
6291 /* Convert symbolic operand to proper sizes for matching, but don't
6292 prevent matching a set of insns that only supports sizes other
6293 than those matching the insn suffix. */
6294 {
6295 i386_operand_type mask, allowed;
6296 const insn_template *t = current_templates->start;
6297
6298 operand_type_set (&mask, 0);
6299 switch (guess_suffix)
6300 {
6301 case QWORD_MNEM_SUFFIX:
6302 mask.bitfield.imm64 = 1;
6303 mask.bitfield.imm32s = 1;
6304 break;
6305 case LONG_MNEM_SUFFIX:
6306 mask.bitfield.imm32 = 1;
6307 break;
6308 case WORD_MNEM_SUFFIX:
6309 mask.bitfield.imm16 = 1;
6310 break;
6311 case BYTE_MNEM_SUFFIX:
6312 mask.bitfield.imm8 = 1;
6313 break;
6314 default:
6315 break;
6316 }
6317
6318 allowed = operand_type_and (t->operand_types[op], mask);
6319 while (++t < current_templates->end)
6320 {
6321 allowed = operand_type_or (allowed, t->operand_types[op]);
6322 allowed = operand_type_and (allowed, mask);
6323 }
6324
6325 if (!operand_type_all_zero (&allowed))
6326 i.types[op] = operand_type_and (i.types[op], mask);
6327 }
6328 break;
6329 }
6330 }
6331 }
6332
6333 /* Try to use the smallest displacement type too. */
6334 static bool
6335 optimize_disp (const insn_template *t)
6336 {
6337 unsigned int op;
6338
6339 if (!want_disp32 (t)
6340 && (!t->opcode_modifier.jump
6341 || i.jumpabsolute || i.types[0].bitfield.baseindex))
6342 {
6343 for (op = 0; op < i.operands; ++op)
6344 {
6345 const expressionS *exp = i.op[op].disps;
6346
6347 if (!operand_type_check (i.types[op], disp))
6348 continue;
6349
6350 if (exp->X_op != O_constant)
6351 continue;
6352
6353 /* Since displacement is signed extended to 64bit, don't allow
6354 disp32 if it is out of range. */
6355 if (fits_in_signed_long (exp->X_add_number))
6356 continue;
6357
6358 i.types[op].bitfield.disp32 = 0;
6359 if (i.types[op].bitfield.baseindex)
6360 {
6361 as_bad (_("0x%" PRIx64 " out of range of signed 32bit displacement"),
6362 (uint64_t) exp->X_add_number);
6363 return false;
6364 }
6365 }
6366 }
6367
6368 /* Don't optimize displacement for movabs since it only takes 64bit
6369 displacement. */
6370 if (i.disp_encoding > disp_encoding_8bit
6371 || (flag_code == CODE_64BIT && t->mnem_off == MN_movabs))
6372 return true;
6373
6374 for (op = i.operands; op-- > 0;)
6375 if (operand_type_check (i.types[op], disp))
6376 {
6377 if (i.op[op].disps->X_op == O_constant)
6378 {
6379 offsetT op_disp = i.op[op].disps->X_add_number;
6380
6381 if (!op_disp && i.types[op].bitfield.baseindex)
6382 {
6383 i.types[op] = operand_type_and_not (i.types[op], anydisp);
6384 i.op[op].disps = NULL;
6385 i.disp_operands--;
6386 continue;
6387 }
6388
6389 if (i.types[op].bitfield.disp16
6390 && fits_in_unsigned_word (op_disp))
6391 {
6392 /* If this operand is at most 16 bits, convert
6393 to a signed 16 bit number and don't use 64bit
6394 displacement. */
6395 op_disp = ((op_disp ^ 0x8000) - 0x8000);
6396 i.types[op].bitfield.disp64 = 0;
6397 }
6398
6399 #ifdef BFD64
6400 /* Optimize 64-bit displacement to 32-bit for 64-bit BFD. */
6401 if ((flag_code != CODE_64BIT
6402 ? i.types[op].bitfield.disp32
6403 : want_disp32 (t)
6404 && (!t->opcode_modifier.jump
6405 || i.jumpabsolute || i.types[op].bitfield.baseindex))
6406 && fits_in_unsigned_long (op_disp))
6407 {
6408 /* If this operand is at most 32 bits, convert
6409 to a signed 32 bit number and don't use 64bit
6410 displacement. */
6411 op_disp = (op_disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
6412 i.types[op].bitfield.disp64 = 0;
6413 i.types[op].bitfield.disp32 = 1;
6414 }
6415
6416 if (flag_code == CODE_64BIT && fits_in_signed_long (op_disp))
6417 {
6418 i.types[op].bitfield.disp64 = 0;
6419 i.types[op].bitfield.disp32 = 1;
6420 }
6421 #endif
6422 if ((i.types[op].bitfield.disp32
6423 || i.types[op].bitfield.disp16)
6424 && fits_in_disp8 (op_disp))
6425 i.types[op].bitfield.disp8 = 1;
6426
6427 i.op[op].disps->X_add_number = op_disp;
6428 }
6429 else if (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
6430 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL)
6431 {
6432 fix_new_exp (frag_now, frag_more (0) - frag_now->fr_literal, 0,
6433 i.op[op].disps, 0, i.reloc[op]);
6434 i.types[op] = operand_type_and_not (i.types[op], anydisp);
6435 }
6436 else
6437 /* We only support 64bit displacement on constants. */
6438 i.types[op].bitfield.disp64 = 0;
6439 }
6440
6441 return true;
6442 }
6443
6444 /* Return 1 if there is a match in broadcast bytes between operand
6445 GIVEN and instruction template T. */
6446
6447 static INLINE int
6448 match_broadcast_size (const insn_template *t, unsigned int given)
6449 {
6450 return ((t->opcode_modifier.broadcast == BYTE_BROADCAST
6451 && i.types[given].bitfield.byte)
6452 || (t->opcode_modifier.broadcast == WORD_BROADCAST
6453 && i.types[given].bitfield.word)
6454 || (t->opcode_modifier.broadcast == DWORD_BROADCAST
6455 && i.types[given].bitfield.dword)
6456 || (t->opcode_modifier.broadcast == QWORD_BROADCAST
6457 && i.types[given].bitfield.qword));
6458 }
6459
6460 /* Check if operands are valid for the instruction. */
6461
6462 static int
6463 check_VecOperands (const insn_template *t)
6464 {
6465 unsigned int op;
6466 i386_cpu_flags cpu;
6467
6468 /* Templates allowing for ZMMword as well as YMMword and/or XMMword for
6469 any one operand are implicity requiring AVX512VL support if the actual
6470 operand size is YMMword or XMMword. Since this function runs after
6471 template matching, there's no need to check for YMMword/XMMword in
6472 the template. */
6473 cpu = cpu_flags_and (cpu_flags_from_attr (t->cpu), avx512);
6474 if (!cpu_flags_all_zero (&cpu)
6475 && !is_cpu (t, CpuAVX512VL)
6476 && !cpu_arch_flags.bitfield.cpuavx512vl)
6477 {
6478 for (op = 0; op < t->operands; ++op)
6479 {
6480 if (t->operand_types[op].bitfield.zmmword
6481 && (i.types[op].bitfield.ymmword
6482 || i.types[op].bitfield.xmmword))
6483 {
6484 i.error = unsupported;
6485 return 1;
6486 }
6487 }
6488 }
6489
6490 /* Somewhat similarly, templates specifying both AVX and AVX2 are
6491 requiring AVX2 support if the actual operand size is YMMword. */
6492 if (is_cpu (t, CpuAVX) && is_cpu (t, CpuAVX2)
6493 && !cpu_arch_flags.bitfield.cpuavx2)
6494 {
6495 for (op = 0; op < t->operands; ++op)
6496 {
6497 if (t->operand_types[op].bitfield.xmmword
6498 && i.types[op].bitfield.ymmword)
6499 {
6500 i.error = unsupported;
6501 return 1;
6502 }
6503 }
6504 }
6505
6506 /* Without VSIB byte, we can't have a vector register for index. */
6507 if (!t->opcode_modifier.sib
6508 && i.index_reg
6509 && (i.index_reg->reg_type.bitfield.xmmword
6510 || i.index_reg->reg_type.bitfield.ymmword
6511 || i.index_reg->reg_type.bitfield.zmmword))
6512 {
6513 i.error = unsupported_vector_index_register;
6514 return 1;
6515 }
6516
6517 /* Check if default mask is allowed. */
6518 if (t->opcode_modifier.operandconstraint == NO_DEFAULT_MASK
6519 && (!i.mask.reg || i.mask.reg->reg_num == 0))
6520 {
6521 i.error = no_default_mask;
6522 return 1;
6523 }
6524
6525 /* For VSIB byte, we need a vector register for index, and all vector
6526 registers must be distinct. */
6527 if (t->opcode_modifier.sib && t->opcode_modifier.sib != SIBMEM)
6528 {
6529 if (!i.index_reg
6530 || !((t->opcode_modifier.sib == VECSIB128
6531 && i.index_reg->reg_type.bitfield.xmmword)
6532 || (t->opcode_modifier.sib == VECSIB256
6533 && i.index_reg->reg_type.bitfield.ymmword)
6534 || (t->opcode_modifier.sib == VECSIB512
6535 && i.index_reg->reg_type.bitfield.zmmword)))
6536 {
6537 i.error = invalid_vsib_address;
6538 return 1;
6539 }
6540
6541 gas_assert (i.reg_operands == 2 || i.mask.reg);
6542 if (i.reg_operands == 2 && !i.mask.reg)
6543 {
6544 gas_assert (i.types[0].bitfield.class == RegSIMD);
6545 gas_assert (i.types[0].bitfield.xmmword
6546 || i.types[0].bitfield.ymmword);
6547 gas_assert (i.types[2].bitfield.class == RegSIMD);
6548 gas_assert (i.types[2].bitfield.xmmword
6549 || i.types[2].bitfield.ymmword);
6550 if (operand_check == check_none)
6551 return 0;
6552 if (register_number (i.op[0].regs)
6553 != register_number (i.index_reg)
6554 && register_number (i.op[2].regs)
6555 != register_number (i.index_reg)
6556 && register_number (i.op[0].regs)
6557 != register_number (i.op[2].regs))
6558 return 0;
6559 if (operand_check == check_error)
6560 {
6561 i.error = invalid_vector_register_set;
6562 return 1;
6563 }
6564 as_warn (_("mask, index, and destination registers should be distinct"));
6565 }
6566 else if (i.reg_operands == 1 && i.mask.reg)
6567 {
6568 if (i.types[1].bitfield.class == RegSIMD
6569 && (i.types[1].bitfield.xmmword
6570 || i.types[1].bitfield.ymmword
6571 || i.types[1].bitfield.zmmword)
6572 && (register_number (i.op[1].regs)
6573 == register_number (i.index_reg)))
6574 {
6575 if (operand_check == check_error)
6576 {
6577 i.error = invalid_vector_register_set;
6578 return 1;
6579 }
6580 if (operand_check != check_none)
6581 as_warn (_("index and destination registers should be distinct"));
6582 }
6583 }
6584 }
6585
6586 /* For AMX instructions with 3 TMM register operands, all operands
6587 must be distinct. */
6588 if (i.reg_operands == 3
6589 && t->operand_types[0].bitfield.tmmword
6590 && (i.op[0].regs == i.op[1].regs
6591 || i.op[0].regs == i.op[2].regs
6592 || i.op[1].regs == i.op[2].regs))
6593 {
6594 i.error = invalid_tmm_register_set;
6595 return 1;
6596 }
6597
6598 /* For some special instructions require that destination must be distinct
6599 from source registers. */
6600 if (t->opcode_modifier.operandconstraint == DISTINCT_DEST)
6601 {
6602 unsigned int dest_reg = i.operands - 1;
6603
6604 know (i.operands >= 3);
6605
6606 /* #UD if dest_reg == src1_reg or dest_reg == src2_reg. */
6607 if (i.op[dest_reg - 1].regs == i.op[dest_reg].regs
6608 || (i.reg_operands > 2
6609 && i.op[dest_reg - 2].regs == i.op[dest_reg].regs))
6610 {
6611 i.error = invalid_dest_and_src_register_set;
6612 return 1;
6613 }
6614 }
6615
6616 /* Check if broadcast is supported by the instruction and is applied
6617 to the memory operand. */
6618 if (i.broadcast.type || i.broadcast.bytes)
6619 {
6620 i386_operand_type type, overlap;
6621
6622 /* Check if specified broadcast is supported in this instruction,
6623 and its broadcast bytes match the memory operand. */
6624 op = i.broadcast.operand;
6625 if (!t->opcode_modifier.broadcast
6626 || !(i.flags[op] & Operand_Mem)
6627 || (!i.types[op].bitfield.unspecified
6628 && !match_broadcast_size (t, op)))
6629 {
6630 bad_broadcast:
6631 i.error = unsupported_broadcast;
6632 return 1;
6633 }
6634
6635 operand_type_set (&type, 0);
6636 switch (get_broadcast_bytes (t, false))
6637 {
6638 case 2:
6639 type.bitfield.word = 1;
6640 break;
6641 case 4:
6642 type.bitfield.dword = 1;
6643 break;
6644 case 8:
6645 type.bitfield.qword = 1;
6646 break;
6647 case 16:
6648 type.bitfield.xmmword = 1;
6649 break;
6650 case 32:
6651 if (vector_size < VSZ256)
6652 goto bad_broadcast;
6653 type.bitfield.ymmword = 1;
6654 break;
6655 case 64:
6656 if (vector_size < VSZ512)
6657 goto bad_broadcast;
6658 type.bitfield.zmmword = 1;
6659 break;
6660 default:
6661 goto bad_broadcast;
6662 }
6663
6664 overlap = operand_type_and (type, t->operand_types[op]);
6665 if (t->operand_types[op].bitfield.class == RegSIMD
6666 && t->operand_types[op].bitfield.byte
6667 + t->operand_types[op].bitfield.word
6668 + t->operand_types[op].bitfield.dword
6669 + t->operand_types[op].bitfield.qword > 1)
6670 {
6671 overlap.bitfield.xmmword = 0;
6672 overlap.bitfield.ymmword = 0;
6673 overlap.bitfield.zmmword = 0;
6674 }
6675 if (operand_type_all_zero (&overlap))
6676 goto bad_broadcast;
6677
6678 if (t->opcode_modifier.checkoperandsize)
6679 {
6680 unsigned int j;
6681
6682 type.bitfield.baseindex = 1;
6683 for (j = 0; j < i.operands; ++j)
6684 {
6685 if (j != op
6686 && !operand_type_register_match(i.types[j],
6687 t->operand_types[j],
6688 type,
6689 t->operand_types[op]))
6690 goto bad_broadcast;
6691 }
6692 }
6693 }
6694 /* If broadcast is supported in this instruction, we need to check if
6695 operand of one-element size isn't specified without broadcast. */
6696 else if (t->opcode_modifier.broadcast && i.mem_operands)
6697 {
6698 /* Find memory operand. */
6699 for (op = 0; op < i.operands; op++)
6700 if (i.flags[op] & Operand_Mem)
6701 break;
6702 gas_assert (op < i.operands);
6703 /* Check size of the memory operand. */
6704 if (match_broadcast_size (t, op))
6705 {
6706 i.error = broadcast_needed;
6707 return 1;
6708 }
6709 }
6710 else
6711 op = MAX_OPERANDS - 1; /* Avoid uninitialized variable warning. */
6712
6713 /* Check if requested masking is supported. */
6714 if (i.mask.reg)
6715 {
6716 if (!t->opcode_modifier.masking)
6717 {
6718 i.error = unsupported_masking;
6719 return 1;
6720 }
6721
6722 /* Common rules for masking:
6723 - mask register destinations permit only zeroing-masking, without
6724 that actually being expressed by a {z} operand suffix or EVEX.z,
6725 - memory destinations allow only merging-masking,
6726 - scatter/gather insns (i.e. ones using vSIB) only allow merging-
6727 masking. */
6728 if (i.mask.zeroing
6729 && (t->operand_types[t->operands - 1].bitfield.class == RegMask
6730 || (i.flags[t->operands - 1] & Operand_Mem)
6731 || t->opcode_modifier.sib))
6732 {
6733 i.error = unsupported_masking;
6734 return 1;
6735 }
6736 }
6737
6738 /* Check if masking is applied to dest operand. */
6739 if (i.mask.reg && (i.mask.operand != i.operands - 1))
6740 {
6741 i.error = mask_not_on_destination;
6742 return 1;
6743 }
6744
6745 /* Check RC/SAE. */
6746 if (i.rounding.type != rc_none)
6747 {
6748 if (!t->opcode_modifier.sae
6749 || ((i.rounding.type != saeonly) != t->opcode_modifier.staticrounding)
6750 || i.mem_operands)
6751 {
6752 i.error = unsupported_rc_sae;
6753 return 1;
6754 }
6755
6756 /* Non-EVEX.LIG forms need to have a ZMM register as at least one
6757 operand. */
6758 if (t->opcode_modifier.evex != EVEXLIG)
6759 {
6760 for (op = 0; op < t->operands; ++op)
6761 if (i.types[op].bitfield.zmmword)
6762 break;
6763 if (op >= t->operands)
6764 {
6765 i.error = operand_size_mismatch;
6766 return 1;
6767 }
6768 }
6769 }
6770
6771 /* Check the special Imm4 cases; must be the first operand. */
6772 if (is_cpu (t, CpuXOP) && t->operands == 5)
6773 {
6774 if (i.op[0].imms->X_op != O_constant
6775 || !fits_in_imm4 (i.op[0].imms->X_add_number))
6776 {
6777 i.error = bad_imm4;
6778 return 1;
6779 }
6780
6781 /* Turn off Imm<N> so that update_imm won't complain. */
6782 operand_type_set (&i.types[0], 0);
6783 }
6784
6785 /* Check vector Disp8 operand. */
6786 if (t->opcode_modifier.disp8memshift
6787 && i.disp_encoding <= disp_encoding_8bit)
6788 {
6789 if (i.broadcast.type || i.broadcast.bytes)
6790 i.memshift = t->opcode_modifier.broadcast - 1;
6791 else if (t->opcode_modifier.disp8memshift != DISP8_SHIFT_VL)
6792 i.memshift = t->opcode_modifier.disp8memshift;
6793 else
6794 {
6795 const i386_operand_type *type = NULL, *fallback = NULL;
6796
6797 i.memshift = 0;
6798 for (op = 0; op < i.operands; op++)
6799 if (i.flags[op] & Operand_Mem)
6800 {
6801 if (t->opcode_modifier.evex == EVEXLIG)
6802 i.memshift = 2 + (i.suffix == QWORD_MNEM_SUFFIX);
6803 else if (t->operand_types[op].bitfield.xmmword
6804 + t->operand_types[op].bitfield.ymmword
6805 + t->operand_types[op].bitfield.zmmword <= 1)
6806 type = &t->operand_types[op];
6807 else if (!i.types[op].bitfield.unspecified)
6808 type = &i.types[op];
6809 else /* Ambiguities get resolved elsewhere. */
6810 fallback = &t->operand_types[op];
6811 }
6812 else if (i.types[op].bitfield.class == RegSIMD
6813 && t->opcode_modifier.evex != EVEXLIG)
6814 {
6815 if (i.types[op].bitfield.zmmword)
6816 i.memshift = 6;
6817 else if (i.types[op].bitfield.ymmword && i.memshift < 5)
6818 i.memshift = 5;
6819 else if (i.types[op].bitfield.xmmword && i.memshift < 4)
6820 i.memshift = 4;
6821 }
6822
6823 if (!type && !i.memshift)
6824 type = fallback;
6825 if (type)
6826 {
6827 if (type->bitfield.zmmword)
6828 i.memshift = 6;
6829 else if (type->bitfield.ymmword)
6830 i.memshift = 5;
6831 else if (type->bitfield.xmmword)
6832 i.memshift = 4;
6833 }
6834
6835 /* For the check in fits_in_disp8(). */
6836 if (i.memshift == 0)
6837 i.memshift = -1;
6838 }
6839
6840 for (op = 0; op < i.operands; op++)
6841 if (operand_type_check (i.types[op], disp)
6842 && i.op[op].disps->X_op == O_constant)
6843 {
6844 if (fits_in_disp8 (i.op[op].disps->X_add_number))
6845 {
6846 i.types[op].bitfield.disp8 = 1;
6847 return 0;
6848 }
6849 i.types[op].bitfield.disp8 = 0;
6850 }
6851 }
6852
6853 i.memshift = 0;
6854
6855 return 0;
6856 }
6857
6858 /* Check if encoding requirements are met by the instruction. */
6859
6860 static int
6861 VEX_check_encoding (const insn_template *t)
6862 {
6863 if (i.vec_encoding == vex_encoding_error)
6864 {
6865 i.error = unsupported;
6866 return 1;
6867 }
6868
6869 /* Vector size restrictions. */
6870 if ((vector_size < VSZ512
6871 && (t->opcode_modifier.evex == EVEX512
6872 || t->opcode_modifier.vsz >= VSZ512))
6873 || (vector_size < VSZ256
6874 && (t->opcode_modifier.evex == EVEX256
6875 || t->opcode_modifier.vex == VEX256
6876 || t->opcode_modifier.vsz >= VSZ256)))
6877 {
6878 i.error = unsupported;
6879 return 1;
6880 }
6881
6882 if (i.vec_encoding == vex_encoding_evex)
6883 {
6884 /* This instruction must be encoded with EVEX prefix. */
6885 if (!is_evex_encoding (t))
6886 {
6887 i.error = unsupported;
6888 return 1;
6889 }
6890 return 0;
6891 }
6892
6893 if (!t->opcode_modifier.vex)
6894 {
6895 /* This instruction template doesn't have VEX prefix. */
6896 if (i.vec_encoding != vex_encoding_default)
6897 {
6898 i.error = unsupported;
6899 return 1;
6900 }
6901 return 0;
6902 }
6903
6904 return 0;
6905 }
6906
6907 /* Helper function for the progress() macro in match_template(). */
6908 static INLINE enum i386_error progress (enum i386_error new,
6909 enum i386_error last,
6910 unsigned int line, unsigned int *line_p)
6911 {
6912 if (line <= *line_p)
6913 return last;
6914 *line_p = line;
6915 return new;
6916 }
6917
6918 static const insn_template *
6919 match_template (char mnem_suffix)
6920 {
6921 /* Points to template once we've found it. */
6922 const insn_template *t;
6923 i386_operand_type overlap0, overlap1, overlap2, overlap3;
6924 i386_operand_type overlap4;
6925 unsigned int found_reverse_match;
6926 i386_operand_type operand_types [MAX_OPERANDS];
6927 int addr_prefix_disp;
6928 unsigned int j, size_match, check_register, errline = __LINE__;
6929 enum i386_error specific_error = number_of_operands_mismatch;
6930 #define progress(err) progress (err, specific_error, __LINE__, &errline)
6931
6932 #if MAX_OPERANDS != 5
6933 # error "MAX_OPERANDS must be 5."
6934 #endif
6935
6936 found_reverse_match = 0;
6937 addr_prefix_disp = -1;
6938
6939 for (t = current_templates->start; t < current_templates->end; t++)
6940 {
6941 addr_prefix_disp = -1;
6942 found_reverse_match = 0;
6943
6944 /* Must have right number of operands. */
6945 if (i.operands != t->operands)
6946 continue;
6947
6948 /* Check processor support. */
6949 specific_error = progress (unsupported);
6950 if (cpu_flags_match (t) != CPU_FLAGS_PERFECT_MATCH)
6951 continue;
6952
6953 /* Check AT&T mnemonic. */
6954 specific_error = progress (unsupported_with_intel_mnemonic);
6955 if (intel_mnemonic && t->opcode_modifier.attmnemonic)
6956 continue;
6957
6958 /* Check AT&T/Intel syntax. */
6959 specific_error = progress (unsupported_syntax);
6960 if ((intel_syntax && t->opcode_modifier.attsyntax)
6961 || (!intel_syntax && t->opcode_modifier.intelsyntax))
6962 continue;
6963
6964 /* Check Intel64/AMD64 ISA. */
6965 switch (isa64)
6966 {
6967 default:
6968 /* Default: Don't accept Intel64. */
6969 if (t->opcode_modifier.isa64 == INTEL64)
6970 continue;
6971 break;
6972 case amd64:
6973 /* -mamd64: Don't accept Intel64 and Intel64 only. */
6974 if (t->opcode_modifier.isa64 >= INTEL64)
6975 continue;
6976 break;
6977 case intel64:
6978 /* -mintel64: Don't accept AMD64. */
6979 if (t->opcode_modifier.isa64 == AMD64 && flag_code == CODE_64BIT)
6980 continue;
6981 break;
6982 }
6983
6984 /* Check the suffix. */
6985 specific_error = progress (invalid_instruction_suffix);
6986 if ((t->opcode_modifier.no_bsuf && mnem_suffix == BYTE_MNEM_SUFFIX)
6987 || (t->opcode_modifier.no_wsuf && mnem_suffix == WORD_MNEM_SUFFIX)
6988 || (t->opcode_modifier.no_lsuf && mnem_suffix == LONG_MNEM_SUFFIX)
6989 || (t->opcode_modifier.no_ssuf && mnem_suffix == SHORT_MNEM_SUFFIX)
6990 || (t->opcode_modifier.no_qsuf && mnem_suffix == QWORD_MNEM_SUFFIX))
6991 continue;
6992
6993 specific_error = progress (operand_size_mismatch);
6994 size_match = operand_size_match (t);
6995 if (!size_match)
6996 continue;
6997
6998 /* This is intentionally not
6999
7000 if (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE))
7001
7002 as the case of a missing * on the operand is accepted (perhaps with
7003 a warning, issued further down). */
7004 specific_error = progress (operand_type_mismatch);
7005 if (i.jumpabsolute && t->opcode_modifier.jump != JUMP_ABSOLUTE)
7006 continue;
7007
7008 /* In Intel syntax, normally we can check for memory operand size when
7009 there is no mnemonic suffix. But jmp and call have 2 different
7010 encodings with Dword memory operand size. Skip the "near" one
7011 (permitting a register operand) when "far" was requested. */
7012 if (i.far_branch
7013 && t->opcode_modifier.jump == JUMP_ABSOLUTE
7014 && t->operand_types[0].bitfield.class == Reg)
7015 continue;
7016
7017 for (j = 0; j < MAX_OPERANDS; j++)
7018 operand_types[j] = t->operand_types[j];
7019
7020 /* In general, don't allow 32-bit operands on pre-386. */
7021 specific_error = progress (mnem_suffix ? invalid_instruction_suffix
7022 : operand_size_mismatch);
7023 j = i.imm_operands + (t->operands > i.imm_operands + 1);
7024 if (i.suffix == LONG_MNEM_SUFFIX
7025 && !cpu_arch_flags.bitfield.cpui386
7026 && (intel_syntax
7027 ? (t->opcode_modifier.mnemonicsize != IGNORESIZE
7028 && !intel_float_operand (insn_name (t)))
7029 : intel_float_operand (insn_name (t)) != 2)
7030 && (t->operands == i.imm_operands
7031 || (operand_types[i.imm_operands].bitfield.class != RegMMX
7032 && operand_types[i.imm_operands].bitfield.class != RegSIMD
7033 && operand_types[i.imm_operands].bitfield.class != RegMask)
7034 || (operand_types[j].bitfield.class != RegMMX
7035 && operand_types[j].bitfield.class != RegSIMD
7036 && operand_types[j].bitfield.class != RegMask))
7037 && !t->opcode_modifier.sib)
7038 continue;
7039
7040 /* Do not verify operands when there are none. */
7041 if (!t->operands)
7042 {
7043 if (VEX_check_encoding (t))
7044 {
7045 specific_error = progress (i.error);
7046 continue;
7047 }
7048
7049 /* We've found a match; break out of loop. */
7050 break;
7051 }
7052
7053 if (!t->opcode_modifier.jump
7054 || t->opcode_modifier.jump == JUMP_ABSOLUTE)
7055 {
7056 /* There should be only one Disp operand. */
7057 for (j = 0; j < MAX_OPERANDS; j++)
7058 if (operand_type_check (operand_types[j], disp))
7059 break;
7060 if (j < MAX_OPERANDS)
7061 {
7062 bool override = (i.prefix[ADDR_PREFIX] != 0);
7063
7064 addr_prefix_disp = j;
7065
7066 /* Address size prefix will turn Disp64 operand into Disp32 and
7067 Disp32/Disp16 one into Disp16/Disp32 respectively. */
7068 switch (flag_code)
7069 {
7070 case CODE_16BIT:
7071 override = !override;
7072 /* Fall through. */
7073 case CODE_32BIT:
7074 if (operand_types[j].bitfield.disp32
7075 && operand_types[j].bitfield.disp16)
7076 {
7077 operand_types[j].bitfield.disp16 = override;
7078 operand_types[j].bitfield.disp32 = !override;
7079 }
7080 gas_assert (!operand_types[j].bitfield.disp64);
7081 break;
7082
7083 case CODE_64BIT:
7084 if (operand_types[j].bitfield.disp64)
7085 {
7086 gas_assert (!operand_types[j].bitfield.disp32);
7087 operand_types[j].bitfield.disp32 = override;
7088 operand_types[j].bitfield.disp64 = !override;
7089 }
7090 operand_types[j].bitfield.disp16 = 0;
7091 break;
7092 }
7093 }
7094 }
7095
7096 /* We check register size if needed. */
7097 if (t->opcode_modifier.checkoperandsize)
7098 {
7099 check_register = (1 << t->operands) - 1;
7100 if (i.broadcast.type || i.broadcast.bytes)
7101 check_register &= ~(1 << i.broadcast.operand);
7102 }
7103 else
7104 check_register = 0;
7105
7106 overlap0 = operand_type_and (i.types[0], operand_types[0]);
7107 switch (t->operands)
7108 {
7109 case 1:
7110 if (!operand_type_match (overlap0, i.types[0]))
7111 continue;
7112
7113 /* Allow the ModR/M encoding to be requested by using the {load} or
7114 {store} pseudo prefix on an applicable insn. */
7115 if (!t->opcode_modifier.modrm
7116 && i.reg_operands == 1
7117 && ((i.dir_encoding == dir_encoding_load
7118 && t->mnem_off != MN_pop)
7119 || (i.dir_encoding == dir_encoding_store
7120 && t->mnem_off != MN_push))
7121 /* Avoid BSWAP. */
7122 && t->mnem_off != MN_bswap)
7123 continue;
7124 break;
7125
7126 case 2:
7127 /* xchg %eax, %eax is a special case. It is an alias for nop
7128 only in 32bit mode and we can use opcode 0x90. In 64bit
7129 mode, we can't use 0x90 for xchg %eax, %eax since it should
7130 zero-extend %eax to %rax. */
7131 if (t->base_opcode == 0x90
7132 && t->opcode_space == SPACE_BASE)
7133 {
7134 if (flag_code == CODE_64BIT
7135 && i.types[0].bitfield.instance == Accum
7136 && i.types[0].bitfield.dword
7137 && i.types[1].bitfield.instance == Accum)
7138 continue;
7139
7140 /* Allow the ModR/M encoding to be requested by using the
7141 {load} or {store} pseudo prefix. */
7142 if (i.dir_encoding == dir_encoding_load
7143 || i.dir_encoding == dir_encoding_store)
7144 continue;
7145 }
7146
7147 if (t->base_opcode == MOV_AX_DISP32
7148 && t->opcode_space == SPACE_BASE
7149 && t->mnem_off != MN_movabs)
7150 {
7151 /* Force 0x8b encoding for "mov foo@GOT, %eax". */
7152 if (i.reloc[0] == BFD_RELOC_386_GOT32)
7153 continue;
7154
7155 /* xrelease mov %eax, <disp> is another special case. It must not
7156 match the accumulator-only encoding of mov. */
7157 if (i.hle_prefix)
7158 continue;
7159
7160 /* Allow the ModR/M encoding to be requested by using a suitable
7161 {load} or {store} pseudo prefix. */
7162 if (i.dir_encoding == (i.types[0].bitfield.instance == Accum
7163 ? dir_encoding_store
7164 : dir_encoding_load)
7165 && !i.types[0].bitfield.disp64
7166 && !i.types[1].bitfield.disp64)
7167 continue;
7168 }
7169
7170 /* Allow the ModR/M encoding to be requested by using the {load} or
7171 {store} pseudo prefix on an applicable insn. */
7172 if (!t->opcode_modifier.modrm
7173 && i.reg_operands == 1
7174 && i.imm_operands == 1
7175 && (i.dir_encoding == dir_encoding_load
7176 || i.dir_encoding == dir_encoding_store)
7177 && t->opcode_space == SPACE_BASE)
7178 {
7179 if (t->base_opcode == 0xb0 /* mov $imm, %reg */
7180 && i.dir_encoding == dir_encoding_store)
7181 continue;
7182
7183 if ((t->base_opcode | 0x38) == 0x3c /* <alu> $imm, %acc */
7184 && (t->base_opcode != 0x3c /* cmp $imm, %acc */
7185 || i.dir_encoding == dir_encoding_load))
7186 continue;
7187
7188 if (t->base_opcode == 0xa8 /* test $imm, %acc */
7189 && i.dir_encoding == dir_encoding_load)
7190 continue;
7191 }
7192 /* Fall through. */
7193
7194 case 3:
7195 if (!(size_match & MATCH_STRAIGHT))
7196 goto check_reverse;
7197 /* Reverse direction of operands if swapping is possible in the first
7198 place (operands need to be symmetric) and
7199 - the load form is requested, and the template is a store form,
7200 - the store form is requested, and the template is a load form,
7201 - the non-default (swapped) form is requested. */
7202 overlap1 = operand_type_and (operand_types[0], operand_types[1]);
7203 if (t->opcode_modifier.d && i.reg_operands == i.operands
7204 && !operand_type_all_zero (&overlap1))
7205 switch (i.dir_encoding)
7206 {
7207 case dir_encoding_load:
7208 if (operand_type_check (operand_types[i.operands - 1], anymem)
7209 || t->opcode_modifier.regmem)
7210 goto check_reverse;
7211 break;
7212
7213 case dir_encoding_store:
7214 if (!operand_type_check (operand_types[i.operands - 1], anymem)
7215 && !t->opcode_modifier.regmem)
7216 goto check_reverse;
7217 break;
7218
7219 case dir_encoding_swap:
7220 goto check_reverse;
7221
7222 case dir_encoding_default:
7223 break;
7224 }
7225 /* If we want store form, we skip the current load. */
7226 if ((i.dir_encoding == dir_encoding_store
7227 || i.dir_encoding == dir_encoding_swap)
7228 && i.mem_operands == 0
7229 && t->opcode_modifier.load)
7230 continue;
7231 /* Fall through. */
7232 case 4:
7233 case 5:
7234 overlap1 = operand_type_and (i.types[1], operand_types[1]);
7235 if (!operand_type_match (overlap0, i.types[0])
7236 || !operand_type_match (overlap1, i.types[1])
7237 || ((check_register & 3) == 3
7238 && !operand_type_register_match (i.types[0],
7239 operand_types[0],
7240 i.types[1],
7241 operand_types[1])))
7242 {
7243 specific_error = progress (i.error);
7244
7245 /* Check if other direction is valid ... */
7246 if (!t->opcode_modifier.d)
7247 continue;
7248
7249 check_reverse:
7250 if (!(size_match & MATCH_REVERSE))
7251 continue;
7252 /* Try reversing direction of operands. */
7253 j = is_cpu (t, CpuFMA4)
7254 || is_cpu (t, CpuXOP) ? 1 : i.operands - 1;
7255 overlap0 = operand_type_and (i.types[0], operand_types[j]);
7256 overlap1 = operand_type_and (i.types[j], operand_types[0]);
7257 overlap2 = operand_type_and (i.types[1], operand_types[1]);
7258 gas_assert (t->operands != 3 || !check_register);
7259 if (!operand_type_match (overlap0, i.types[0])
7260 || !operand_type_match (overlap1, i.types[j])
7261 || (t->operands == 3
7262 && !operand_type_match (overlap2, i.types[1]))
7263 || (check_register
7264 && !operand_type_register_match (i.types[0],
7265 operand_types[j],
7266 i.types[j],
7267 operand_types[0])))
7268 {
7269 /* Does not match either direction. */
7270 specific_error = progress (i.error);
7271 continue;
7272 }
7273 /* found_reverse_match holds which variant of D
7274 we've found. */
7275 if (!t->opcode_modifier.d)
7276 found_reverse_match = 0;
7277 else if (operand_types[0].bitfield.tbyte)
7278 {
7279 if (t->opcode_modifier.operandconstraint != UGH)
7280 found_reverse_match = Opcode_FloatD;
7281 else
7282 found_reverse_match = ~0;
7283 /* FSUB{,R} and FDIV{,R} may need a 2nd bit flipped. */
7284 if ((t->extension_opcode & 4)
7285 && (intel_syntax || intel_mnemonic))
7286 found_reverse_match |= Opcode_FloatR;
7287 }
7288 else if (is_cpu (t, CpuFMA4) || is_cpu (t, CpuXOP))
7289 {
7290 found_reverse_match = Opcode_VexW;
7291 goto check_operands_345;
7292 }
7293 else if (t->opcode_space != SPACE_BASE
7294 && (t->opcode_space != SPACE_0F
7295 /* MOV to/from CR/DR/TR, as an exception, follow
7296 the base opcode space encoding model. */
7297 || (t->base_opcode | 7) != 0x27))
7298 found_reverse_match = (t->base_opcode & 0xee) != 0x6e
7299 ? Opcode_ExtD : Opcode_SIMD_IntD;
7300 else if (!t->opcode_modifier.commutative)
7301 found_reverse_match = Opcode_D;
7302 else
7303 found_reverse_match = ~0;
7304 }
7305 else
7306 {
7307 /* Found a forward 2 operand match here. */
7308 check_operands_345:
7309 switch (t->operands)
7310 {
7311 case 5:
7312 overlap4 = operand_type_and (i.types[4], operand_types[4]);
7313 if (!operand_type_match (overlap4, i.types[4])
7314 || !operand_type_register_match (i.types[3],
7315 operand_types[3],
7316 i.types[4],
7317 operand_types[4]))
7318 {
7319 specific_error = progress (i.error);
7320 continue;
7321 }
7322 /* Fall through. */
7323 case 4:
7324 overlap3 = operand_type_and (i.types[3], operand_types[3]);
7325 if (!operand_type_match (overlap3, i.types[3])
7326 || ((check_register & 0xa) == 0xa
7327 && !operand_type_register_match (i.types[1],
7328 operand_types[1],
7329 i.types[3],
7330 operand_types[3]))
7331 || ((check_register & 0xc) == 0xc
7332 && !operand_type_register_match (i.types[2],
7333 operand_types[2],
7334 i.types[3],
7335 operand_types[3])))
7336 {
7337 specific_error = progress (i.error);
7338 continue;
7339 }
7340 /* Fall through. */
7341 case 3:
7342 overlap2 = operand_type_and (i.types[2], operand_types[2]);
7343 if (!operand_type_match (overlap2, i.types[2])
7344 || ((check_register & 5) == 5
7345 && !operand_type_register_match (i.types[0],
7346 operand_types[0],
7347 i.types[2],
7348 operand_types[2]))
7349 || ((check_register & 6) == 6
7350 && !operand_type_register_match (i.types[1],
7351 operand_types[1],
7352 i.types[2],
7353 operand_types[2])))
7354 {
7355 specific_error = progress (i.error);
7356 continue;
7357 }
7358 break;
7359 }
7360 }
7361 /* Found either forward/reverse 2, 3 or 4 operand match here:
7362 slip through to break. */
7363 }
7364
7365 /* Check if VEX/EVEX encoding requirements can be satisfied. */
7366 if (VEX_check_encoding (t))
7367 {
7368 specific_error = progress (i.error);
7369 continue;
7370 }
7371
7372 /* Check if vector operands are valid. */
7373 if (check_VecOperands (t))
7374 {
7375 specific_error = progress (i.error);
7376 continue;
7377 }
7378
7379 /* We've found a match; break out of loop. */
7380 break;
7381 }
7382
7383 #undef progress
7384
7385 if (t == current_templates->end)
7386 {
7387 /* We found no match. */
7388 i.error = specific_error;
7389 return NULL;
7390 }
7391
7392 if (!quiet_warnings)
7393 {
7394 if (!intel_syntax
7395 && (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE)))
7396 as_warn (_("indirect %s without `*'"), insn_name (t));
7397
7398 if (t->opcode_modifier.isprefix
7399 && t->opcode_modifier.mnemonicsize == IGNORESIZE)
7400 {
7401 /* Warn them that a data or address size prefix doesn't
7402 affect assembly of the next line of code. */
7403 as_warn (_("stand-alone `%s' prefix"), insn_name (t));
7404 }
7405 }
7406
7407 /* Copy the template we found. */
7408 install_template (t);
7409
7410 if (addr_prefix_disp != -1)
7411 i.tm.operand_types[addr_prefix_disp]
7412 = operand_types[addr_prefix_disp];
7413
7414 switch (found_reverse_match)
7415 {
7416 case 0:
7417 break;
7418
7419 case Opcode_FloatR:
7420 case Opcode_FloatR | Opcode_FloatD:
7421 i.tm.extension_opcode ^= Opcode_FloatR >> 3;
7422 found_reverse_match &= Opcode_FloatD;
7423
7424 /* Fall through. */
7425 default:
7426 /* If we found a reverse match we must alter the opcode direction
7427 bit and clear/flip the regmem modifier one. found_reverse_match
7428 holds bits to change (different for int & float insns). */
7429
7430 i.tm.base_opcode ^= found_reverse_match;
7431
7432 /* Certain SIMD insns have their load forms specified in the opcode
7433 table, and hence we need to _set_ RegMem instead of clearing it.
7434 We need to avoid setting the bit though on insns like KMOVW. */
7435 i.tm.opcode_modifier.regmem
7436 = i.tm.opcode_modifier.modrm && i.tm.opcode_modifier.d
7437 && i.tm.operands > 2U - i.tm.opcode_modifier.sse2avx
7438 && !i.tm.opcode_modifier.regmem;
7439
7440 /* Fall through. */
7441 case ~0:
7442 i.tm.operand_types[0] = operand_types[i.operands - 1];
7443 i.tm.operand_types[i.operands - 1] = operand_types[0];
7444 break;
7445
7446 case Opcode_VexW:
7447 /* Only the first two register operands need reversing, alongside
7448 flipping VEX.W. */
7449 i.tm.opcode_modifier.vexw ^= VEXW0 ^ VEXW1;
7450
7451 j = i.tm.operand_types[0].bitfield.imm8;
7452 i.tm.operand_types[j] = operand_types[j + 1];
7453 i.tm.operand_types[j + 1] = operand_types[j];
7454 break;
7455 }
7456
7457 return t;
7458 }
7459
7460 static int
7461 check_string (void)
7462 {
7463 unsigned int es_op = i.tm.opcode_modifier.isstring - IS_STRING_ES_OP0;
7464 unsigned int op = i.tm.operand_types[0].bitfield.baseindex ? es_op : 0;
7465
7466 if (i.seg[op] != NULL && i.seg[op] != reg_es)
7467 {
7468 as_bad (_("`%s' operand %u must use `%ses' segment"),
7469 insn_name (&i.tm),
7470 intel_syntax ? i.tm.operands - es_op : es_op + 1,
7471 register_prefix);
7472 return 0;
7473 }
7474
7475 /* There's only ever one segment override allowed per instruction.
7476 This instruction possibly has a legal segment override on the
7477 second operand, so copy the segment to where non-string
7478 instructions store it, allowing common code. */
7479 i.seg[op] = i.seg[1];
7480
7481 return 1;
7482 }
7483
7484 static int
7485 process_suffix (void)
7486 {
7487 bool is_movx = false;
7488
7489 /* If matched instruction specifies an explicit instruction mnemonic
7490 suffix, use it. */
7491 if (i.tm.opcode_modifier.size == SIZE16)
7492 i.suffix = WORD_MNEM_SUFFIX;
7493 else if (i.tm.opcode_modifier.size == SIZE32)
7494 i.suffix = LONG_MNEM_SUFFIX;
7495 else if (i.tm.opcode_modifier.size == SIZE64)
7496 i.suffix = QWORD_MNEM_SUFFIX;
7497 else if (i.reg_operands
7498 && (i.operands > 1 || i.types[0].bitfield.class == Reg)
7499 && i.tm.opcode_modifier.operandconstraint != ADDR_PREFIX_OP_REG)
7500 {
7501 unsigned int numop = i.operands;
7502
7503 /* MOVSX/MOVZX */
7504 is_movx = (i.tm.opcode_space == SPACE_0F
7505 && (i.tm.base_opcode | 8) == 0xbe)
7506 || (i.tm.opcode_space == SPACE_BASE
7507 && i.tm.base_opcode == 0x63
7508 && is_cpu (&i.tm, Cpu64));
7509
7510 /* movsx/movzx want only their source operand considered here, for the
7511 ambiguity checking below. The suffix will be replaced afterwards
7512 to represent the destination (register). */
7513 if (is_movx && (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63))
7514 --i.operands;
7515
7516 /* crc32 needs REX.W set regardless of suffix / source operand size. */
7517 if (i.tm.mnem_off == MN_crc32 && i.tm.operand_types[1].bitfield.qword)
7518 i.rex |= REX_W;
7519
7520 /* If there's no instruction mnemonic suffix we try to invent one
7521 based on GPR operands. */
7522 if (!i.suffix)
7523 {
7524 /* We take i.suffix from the last register operand specified,
7525 Destination register type is more significant than source
7526 register type. crc32 in SSE4.2 prefers source register
7527 type. */
7528 unsigned int op = i.tm.mnem_off == MN_crc32 ? 1 : i.operands;
7529
7530 while (op--)
7531 if (i.tm.operand_types[op].bitfield.instance == InstanceNone
7532 || i.tm.operand_types[op].bitfield.instance == Accum)
7533 {
7534 if (i.types[op].bitfield.class != Reg)
7535 continue;
7536 if (i.types[op].bitfield.byte)
7537 i.suffix = BYTE_MNEM_SUFFIX;
7538 else if (i.types[op].bitfield.word)
7539 i.suffix = WORD_MNEM_SUFFIX;
7540 else if (i.types[op].bitfield.dword)
7541 i.suffix = LONG_MNEM_SUFFIX;
7542 else if (i.types[op].bitfield.qword)
7543 i.suffix = QWORD_MNEM_SUFFIX;
7544 else
7545 continue;
7546 break;
7547 }
7548
7549 /* As an exception, movsx/movzx silently default to a byte source
7550 in AT&T mode. */
7551 if (is_movx && i.tm.opcode_modifier.w && !i.suffix && !intel_syntax)
7552 i.suffix = BYTE_MNEM_SUFFIX;
7553 }
7554 else if (i.suffix == BYTE_MNEM_SUFFIX)
7555 {
7556 if (!check_byte_reg ())
7557 return 0;
7558 }
7559 else if (i.suffix == LONG_MNEM_SUFFIX)
7560 {
7561 if (!check_long_reg ())
7562 return 0;
7563 }
7564 else if (i.suffix == QWORD_MNEM_SUFFIX)
7565 {
7566 if (!check_qword_reg ())
7567 return 0;
7568 }
7569 else if (i.suffix == WORD_MNEM_SUFFIX)
7570 {
7571 if (!check_word_reg ())
7572 return 0;
7573 }
7574 else if (intel_syntax
7575 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
7576 /* Do nothing if the instruction is going to ignore the prefix. */
7577 ;
7578 else
7579 abort ();
7580
7581 /* Undo the movsx/movzx change done above. */
7582 i.operands = numop;
7583 }
7584 else if (i.tm.opcode_modifier.mnemonicsize == DEFAULTSIZE
7585 && !i.suffix)
7586 {
7587 i.suffix = stackop_size;
7588 if (stackop_size == LONG_MNEM_SUFFIX)
7589 {
7590 /* stackop_size is set to LONG_MNEM_SUFFIX for the
7591 .code16gcc directive to support 16-bit mode with
7592 32-bit address. For IRET without a suffix, generate
7593 16-bit IRET (opcode 0xcf) to return from an interrupt
7594 handler. */
7595 if (i.tm.base_opcode == 0xcf)
7596 {
7597 i.suffix = WORD_MNEM_SUFFIX;
7598 as_warn (_("generating 16-bit `iret' for .code16gcc directive"));
7599 }
7600 /* Warn about changed behavior for segment register push/pop. */
7601 else if ((i.tm.base_opcode | 1) == 0x07)
7602 as_warn (_("generating 32-bit `%s', unlike earlier gas versions"),
7603 insn_name (&i.tm));
7604 }
7605 }
7606 else if (!i.suffix
7607 && (i.tm.opcode_modifier.jump == JUMP_ABSOLUTE
7608 || i.tm.opcode_modifier.jump == JUMP_BYTE
7609 || i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT
7610 || (i.tm.opcode_space == SPACE_0F
7611 && i.tm.base_opcode == 0x01 /* [ls][gi]dt */
7612 && i.tm.extension_opcode <= 3)))
7613 {
7614 switch (flag_code)
7615 {
7616 case CODE_64BIT:
7617 if (!i.tm.opcode_modifier.no_qsuf)
7618 {
7619 if (i.tm.opcode_modifier.jump == JUMP_BYTE
7620 || i.tm.opcode_modifier.no_lsuf)
7621 i.suffix = QWORD_MNEM_SUFFIX;
7622 break;
7623 }
7624 /* Fall through. */
7625 case CODE_32BIT:
7626 if (!i.tm.opcode_modifier.no_lsuf)
7627 i.suffix = LONG_MNEM_SUFFIX;
7628 break;
7629 case CODE_16BIT:
7630 if (!i.tm.opcode_modifier.no_wsuf)
7631 i.suffix = WORD_MNEM_SUFFIX;
7632 break;
7633 }
7634 }
7635
7636 if (!i.suffix
7637 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
7638 /* Also cover lret/retf/iret in 64-bit mode. */
7639 || (flag_code == CODE_64BIT
7640 && !i.tm.opcode_modifier.no_lsuf
7641 && !i.tm.opcode_modifier.no_qsuf))
7642 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
7643 /* Explicit sizing prefixes are assumed to disambiguate insns. */
7644 && !i.prefix[DATA_PREFIX] && !(i.prefix[REX_PREFIX] & REX_W)
7645 /* Accept FLDENV et al without suffix. */
7646 && (i.tm.opcode_modifier.no_ssuf || i.tm.opcode_modifier.floatmf))
7647 {
7648 unsigned int suffixes, evex = 0;
7649
7650 suffixes = !i.tm.opcode_modifier.no_bsuf;
7651 if (!i.tm.opcode_modifier.no_wsuf)
7652 suffixes |= 1 << 1;
7653 if (!i.tm.opcode_modifier.no_lsuf)
7654 suffixes |= 1 << 2;
7655 if (!i.tm.opcode_modifier.no_ssuf)
7656 suffixes |= 1 << 4;
7657 if (flag_code == CODE_64BIT && !i.tm.opcode_modifier.no_qsuf)
7658 suffixes |= 1 << 5;
7659
7660 /* For [XYZ]MMWORD operands inspect operand sizes. While generally
7661 also suitable for AT&T syntax mode, it was requested that this be
7662 restricted to just Intel syntax. */
7663 if (intel_syntax && is_any_vex_encoding (&i.tm)
7664 && !i.broadcast.type && !i.broadcast.bytes)
7665 {
7666 unsigned int op;
7667
7668 for (op = 0; op < i.tm.operands; ++op)
7669 {
7670 if (vector_size < VSZ512)
7671 {
7672 i.tm.operand_types[op].bitfield.zmmword = 0;
7673 if (vector_size < VSZ256)
7674 {
7675 i.tm.operand_types[op].bitfield.ymmword = 0;
7676 if (i.tm.operand_types[op].bitfield.xmmword
7677 && (i.tm.opcode_modifier.evex == EVEXDYN
7678 || (!i.tm.opcode_modifier.evex
7679 && is_evex_encoding (&i.tm))))
7680 i.tm.opcode_modifier.evex = EVEX128;
7681 }
7682 else if (i.tm.operand_types[op].bitfield.ymmword
7683 && !i.tm.operand_types[op].bitfield.xmmword
7684 && (i.tm.opcode_modifier.evex == EVEXDYN
7685 || (!i.tm.opcode_modifier.evex
7686 && is_evex_encoding (&i.tm))))
7687 i.tm.opcode_modifier.evex = EVEX256;
7688 }
7689 else if (is_evex_encoding (&i.tm)
7690 && !cpu_arch_flags.bitfield.cpuavx512vl)
7691 {
7692 if (i.tm.operand_types[op].bitfield.ymmword)
7693 i.tm.operand_types[op].bitfield.xmmword = 0;
7694 if (i.tm.operand_types[op].bitfield.zmmword)
7695 i.tm.operand_types[op].bitfield.ymmword = 0;
7696 if (!i.tm.opcode_modifier.evex
7697 || i.tm.opcode_modifier.evex == EVEXDYN)
7698 i.tm.opcode_modifier.evex = EVEX512;
7699 }
7700
7701 if (i.tm.operand_types[op].bitfield.xmmword
7702 + i.tm.operand_types[op].bitfield.ymmword
7703 + i.tm.operand_types[op].bitfield.zmmword < 2)
7704 continue;
7705
7706 /* Any properly sized operand disambiguates the insn. */
7707 if (i.types[op].bitfield.xmmword
7708 || i.types[op].bitfield.ymmword
7709 || i.types[op].bitfield.zmmword)
7710 {
7711 suffixes &= ~(7 << 6);
7712 evex = 0;
7713 break;
7714 }
7715
7716 if ((i.flags[op] & Operand_Mem)
7717 && i.tm.operand_types[op].bitfield.unspecified)
7718 {
7719 if (i.tm.operand_types[op].bitfield.xmmword)
7720 suffixes |= 1 << 6;
7721 if (i.tm.operand_types[op].bitfield.ymmword)
7722 suffixes |= 1 << 7;
7723 if (i.tm.operand_types[op].bitfield.zmmword)
7724 suffixes |= 1 << 8;
7725 if (is_evex_encoding (&i.tm))
7726 evex = EVEX512;
7727 }
7728 }
7729 }
7730
7731 /* Are multiple suffixes / operand sizes allowed? */
7732 if (suffixes & (suffixes - 1))
7733 {
7734 if (intel_syntax
7735 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
7736 || operand_check == check_error))
7737 {
7738 as_bad (_("ambiguous operand size for `%s'"), insn_name (&i.tm));
7739 return 0;
7740 }
7741 if (operand_check == check_error)
7742 {
7743 as_bad (_("no instruction mnemonic suffix given and "
7744 "no register operands; can't size `%s'"), insn_name (&i.tm));
7745 return 0;
7746 }
7747 if (operand_check == check_warning)
7748 as_warn (_("%s; using default for `%s'"),
7749 intel_syntax
7750 ? _("ambiguous operand size")
7751 : _("no instruction mnemonic suffix given and "
7752 "no register operands"),
7753 insn_name (&i.tm));
7754
7755 if (i.tm.opcode_modifier.floatmf)
7756 i.suffix = SHORT_MNEM_SUFFIX;
7757 else if (is_movx)
7758 /* handled below */;
7759 else if (evex)
7760 i.tm.opcode_modifier.evex = evex;
7761 else if (flag_code == CODE_16BIT)
7762 i.suffix = WORD_MNEM_SUFFIX;
7763 else if (!i.tm.opcode_modifier.no_lsuf)
7764 i.suffix = LONG_MNEM_SUFFIX;
7765 else
7766 i.suffix = QWORD_MNEM_SUFFIX;
7767 }
7768 }
7769
7770 if (is_movx)
7771 {
7772 /* In Intel syntax, movsx/movzx must have a "suffix" (checked above).
7773 In AT&T syntax, if there is no suffix (warned about above), the default
7774 will be byte extension. */
7775 if (i.tm.opcode_modifier.w && i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
7776 i.tm.base_opcode |= 1;
7777
7778 /* For further processing, the suffix should represent the destination
7779 (register). This is already the case when one was used with
7780 mov[sz][bw]*, but we need to replace it for mov[sz]x, or if there was
7781 no suffix to begin with. */
7782 if (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63 || !i.suffix)
7783 {
7784 if (i.types[1].bitfield.word)
7785 i.suffix = WORD_MNEM_SUFFIX;
7786 else if (i.types[1].bitfield.qword)
7787 i.suffix = QWORD_MNEM_SUFFIX;
7788 else
7789 i.suffix = LONG_MNEM_SUFFIX;
7790
7791 i.tm.opcode_modifier.w = 0;
7792 }
7793 }
7794
7795 if (!i.tm.opcode_modifier.modrm && i.reg_operands && i.tm.operands < 3)
7796 i.short_form = (i.tm.operand_types[0].bitfield.class == Reg)
7797 != (i.tm.operand_types[1].bitfield.class == Reg);
7798
7799 /* Change the opcode based on the operand size given by i.suffix. */
7800 switch (i.suffix)
7801 {
7802 /* Size floating point instruction. */
7803 case LONG_MNEM_SUFFIX:
7804 if (i.tm.opcode_modifier.floatmf)
7805 {
7806 i.tm.base_opcode ^= 4;
7807 break;
7808 }
7809 /* fall through */
7810 case WORD_MNEM_SUFFIX:
7811 case QWORD_MNEM_SUFFIX:
7812 /* It's not a byte, select word/dword operation. */
7813 if (i.tm.opcode_modifier.w)
7814 {
7815 if (i.short_form)
7816 i.tm.base_opcode |= 8;
7817 else
7818 i.tm.base_opcode |= 1;
7819 }
7820 /* fall through */
7821 case SHORT_MNEM_SUFFIX:
7822 /* Now select between word & dword operations via the operand
7823 size prefix, except for instructions that will ignore this
7824 prefix anyway. */
7825 if (i.suffix != QWORD_MNEM_SUFFIX
7826 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
7827 && !i.tm.opcode_modifier.floatmf
7828 && !is_any_vex_encoding (&i.tm)
7829 && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
7830 || (flag_code == CODE_64BIT
7831 && i.tm.opcode_modifier.jump == JUMP_BYTE)))
7832 {
7833 unsigned int prefix = DATA_PREFIX_OPCODE;
7834
7835 if (i.tm.opcode_modifier.jump == JUMP_BYTE) /* jcxz, loop */
7836 prefix = ADDR_PREFIX_OPCODE;
7837
7838 if (!add_prefix (prefix))
7839 return 0;
7840 }
7841
7842 /* Set mode64 for an operand. */
7843 if (i.suffix == QWORD_MNEM_SUFFIX
7844 && flag_code == CODE_64BIT
7845 && !i.tm.opcode_modifier.norex64
7846 && !i.tm.opcode_modifier.vexw
7847 /* Special case for xchg %rax,%rax. It is NOP and doesn't
7848 need rex64. */
7849 && ! (i.operands == 2
7850 && i.tm.base_opcode == 0x90
7851 && i.tm.opcode_space == SPACE_BASE
7852 && i.types[0].bitfield.instance == Accum
7853 && i.types[0].bitfield.qword
7854 && i.types[1].bitfield.instance == Accum))
7855 i.rex |= REX_W;
7856
7857 break;
7858
7859 case 0:
7860 /* Select word/dword/qword operation with explicit data sizing prefix
7861 when there are no suitable register operands. */
7862 if (i.tm.opcode_modifier.w
7863 && (i.prefix[DATA_PREFIX] || (i.prefix[REX_PREFIX] & REX_W))
7864 && (!i.reg_operands
7865 || (i.reg_operands == 1
7866 /* ShiftCount */
7867 && (i.tm.operand_types[0].bitfield.instance == RegC
7868 /* InOutPortReg */
7869 || i.tm.operand_types[0].bitfield.instance == RegD
7870 || i.tm.operand_types[1].bitfield.instance == RegD
7871 || i.tm.mnem_off == MN_crc32))))
7872 i.tm.base_opcode |= 1;
7873 break;
7874 }
7875
7876 if (i.tm.opcode_modifier.operandconstraint == ADDR_PREFIX_OP_REG)
7877 {
7878 gas_assert (!i.suffix);
7879 gas_assert (i.reg_operands);
7880
7881 if (i.tm.operand_types[0].bitfield.instance == Accum
7882 || i.operands == 1)
7883 {
7884 /* The address size override prefix changes the size of the
7885 first operand. */
7886 if (flag_code == CODE_64BIT
7887 && i.op[0].regs->reg_type.bitfield.word)
7888 {
7889 as_bad (_("16-bit addressing unavailable for `%s'"),
7890 insn_name (&i.tm));
7891 return 0;
7892 }
7893
7894 if ((flag_code == CODE_32BIT
7895 ? i.op[0].regs->reg_type.bitfield.word
7896 : i.op[0].regs->reg_type.bitfield.dword)
7897 && !add_prefix (ADDR_PREFIX_OPCODE))
7898 return 0;
7899 }
7900 else
7901 {
7902 /* Check invalid register operand when the address size override
7903 prefix changes the size of register operands. */
7904 unsigned int op;
7905 enum { need_word, need_dword, need_qword } need;
7906
7907 /* Check the register operand for the address size prefix if
7908 the memory operand has no real registers, like symbol, DISP
7909 or bogus (x32-only) symbol(%rip) when symbol(%eip) is meant. */
7910 if (i.mem_operands == 1
7911 && i.reg_operands == 1
7912 && i.operands == 2
7913 && i.types[1].bitfield.class == Reg
7914 && (flag_code == CODE_32BIT
7915 ? i.op[1].regs->reg_type.bitfield.word
7916 : i.op[1].regs->reg_type.bitfield.dword)
7917 && ((i.base_reg == NULL && i.index_reg == NULL)
7918 #if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
7919 || (x86_elf_abi == X86_64_X32_ABI
7920 && i.base_reg
7921 && i.base_reg->reg_num == RegIP
7922 && i.base_reg->reg_type.bitfield.qword))
7923 #else
7924 || 0)
7925 #endif
7926 && !add_prefix (ADDR_PREFIX_OPCODE))
7927 return 0;
7928
7929 if (flag_code == CODE_32BIT)
7930 need = i.prefix[ADDR_PREFIX] ? need_word : need_dword;
7931 else if (i.prefix[ADDR_PREFIX])
7932 need = need_dword;
7933 else
7934 need = flag_code == CODE_64BIT ? need_qword : need_word;
7935
7936 for (op = 0; op < i.operands; op++)
7937 {
7938 if (i.types[op].bitfield.class != Reg)
7939 continue;
7940
7941 switch (need)
7942 {
7943 case need_word:
7944 if (i.op[op].regs->reg_type.bitfield.word)
7945 continue;
7946 break;
7947 case need_dword:
7948 if (i.op[op].regs->reg_type.bitfield.dword)
7949 continue;
7950 break;
7951 case need_qword:
7952 if (i.op[op].regs->reg_type.bitfield.qword)
7953 continue;
7954 break;
7955 }
7956
7957 as_bad (_("invalid register operand size for `%s'"),
7958 insn_name (&i.tm));
7959 return 0;
7960 }
7961 }
7962 }
7963
7964 return 1;
7965 }
7966
7967 static int
7968 check_byte_reg (void)
7969 {
7970 int op;
7971
7972 for (op = i.operands; --op >= 0;)
7973 {
7974 /* Skip non-register operands. */
7975 if (i.types[op].bitfield.class != Reg)
7976 continue;
7977
7978 /* If this is an eight bit register, it's OK. If it's the 16 or
7979 32 bit version of an eight bit register, we will just use the
7980 low portion, and that's OK too. */
7981 if (i.types[op].bitfield.byte)
7982 continue;
7983
7984 /* I/O port address operands are OK too. */
7985 if (i.tm.operand_types[op].bitfield.instance == RegD
7986 && i.tm.operand_types[op].bitfield.word)
7987 continue;
7988
7989 /* crc32 only wants its source operand checked here. */
7990 if (i.tm.mnem_off == MN_crc32 && op != 0)
7991 continue;
7992
7993 /* Any other register is bad. */
7994 as_bad (_("`%s%s' not allowed with `%s%c'"),
7995 register_prefix, i.op[op].regs->reg_name,
7996 insn_name (&i.tm), i.suffix);
7997 return 0;
7998 }
7999 return 1;
8000 }
8001
8002 static int
8003 check_long_reg (void)
8004 {
8005 int op;
8006
8007 for (op = i.operands; --op >= 0;)
8008 /* Skip non-register operands. */
8009 if (i.types[op].bitfield.class != Reg)
8010 continue;
8011 /* Reject eight bit registers, except where the template requires
8012 them. (eg. movzb) */
8013 else if (i.types[op].bitfield.byte
8014 && (i.tm.operand_types[op].bitfield.class == Reg
8015 || i.tm.operand_types[op].bitfield.instance == Accum)
8016 && (i.tm.operand_types[op].bitfield.word
8017 || i.tm.operand_types[op].bitfield.dword))
8018 {
8019 as_bad (_("`%s%s' not allowed with `%s%c'"),
8020 register_prefix,
8021 i.op[op].regs->reg_name,
8022 insn_name (&i.tm),
8023 i.suffix);
8024 return 0;
8025 }
8026 /* Error if the e prefix on a general reg is missing. */
8027 else if (i.types[op].bitfield.word
8028 && (i.tm.operand_types[op].bitfield.class == Reg
8029 || i.tm.operand_types[op].bitfield.instance == Accum)
8030 && i.tm.operand_types[op].bitfield.dword)
8031 {
8032 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
8033 register_prefix, i.op[op].regs->reg_name,
8034 i.suffix);
8035 return 0;
8036 }
8037 /* Warn if the r prefix on a general reg is present. */
8038 else if (i.types[op].bitfield.qword
8039 && (i.tm.operand_types[op].bitfield.class == Reg
8040 || i.tm.operand_types[op].bitfield.instance == Accum)
8041 && i.tm.operand_types[op].bitfield.dword)
8042 {
8043 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
8044 register_prefix, i.op[op].regs->reg_name, i.suffix);
8045 return 0;
8046 }
8047 return 1;
8048 }
8049
8050 static int
8051 check_qword_reg (void)
8052 {
8053 int op;
8054
8055 for (op = i.operands; --op >= 0; )
8056 /* Skip non-register operands. */
8057 if (i.types[op].bitfield.class != Reg)
8058 continue;
8059 /* Reject eight bit registers, except where the template requires
8060 them. (eg. movzb) */
8061 else if (i.types[op].bitfield.byte
8062 && (i.tm.operand_types[op].bitfield.class == Reg
8063 || i.tm.operand_types[op].bitfield.instance == Accum)
8064 && (i.tm.operand_types[op].bitfield.word
8065 || i.tm.operand_types[op].bitfield.dword))
8066 {
8067 as_bad (_("`%s%s' not allowed with `%s%c'"),
8068 register_prefix,
8069 i.op[op].regs->reg_name,
8070 insn_name (&i.tm),
8071 i.suffix);
8072 return 0;
8073 }
8074 /* Warn if the r prefix on a general reg is missing. */
8075 else if ((i.types[op].bitfield.word
8076 || i.types[op].bitfield.dword)
8077 && (i.tm.operand_types[op].bitfield.class == Reg
8078 || i.tm.operand_types[op].bitfield.instance == Accum)
8079 && i.tm.operand_types[op].bitfield.qword)
8080 {
8081 /* Prohibit these changes in the 64bit mode, since the
8082 lowering is more complicated. */
8083 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
8084 register_prefix, i.op[op].regs->reg_name, i.suffix);
8085 return 0;
8086 }
8087 return 1;
8088 }
8089
8090 static int
8091 check_word_reg (void)
8092 {
8093 int op;
8094 for (op = i.operands; --op >= 0;)
8095 /* Skip non-register operands. */
8096 if (i.types[op].bitfield.class != Reg)
8097 continue;
8098 /* Reject eight bit registers, except where the template requires
8099 them. (eg. movzb) */
8100 else if (i.types[op].bitfield.byte
8101 && (i.tm.operand_types[op].bitfield.class == Reg
8102 || i.tm.operand_types[op].bitfield.instance == Accum)
8103 && (i.tm.operand_types[op].bitfield.word
8104 || i.tm.operand_types[op].bitfield.dword))
8105 {
8106 as_bad (_("`%s%s' not allowed with `%s%c'"),
8107 register_prefix,
8108 i.op[op].regs->reg_name,
8109 insn_name (&i.tm),
8110 i.suffix);
8111 return 0;
8112 }
8113 /* Error if the e or r prefix on a general reg is present. */
8114 else if ((i.types[op].bitfield.dword
8115 || i.types[op].bitfield.qword)
8116 && (i.tm.operand_types[op].bitfield.class == Reg
8117 || i.tm.operand_types[op].bitfield.instance == Accum)
8118 && i.tm.operand_types[op].bitfield.word)
8119 {
8120 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
8121 register_prefix, i.op[op].regs->reg_name,
8122 i.suffix);
8123 return 0;
8124 }
8125 return 1;
8126 }
8127
8128 static int
8129 update_imm (unsigned int j)
8130 {
8131 i386_operand_type overlap = i.types[j];
8132
8133 if (i.tm.operand_types[j].bitfield.imm8
8134 && i.tm.operand_types[j].bitfield.imm8s
8135 && overlap.bitfield.imm8 && overlap.bitfield.imm8s)
8136 {
8137 /* This combination is used on 8-bit immediates where e.g. $~0 is
8138 desirable to permit. We're past operand type matching, so simply
8139 put things back in the shape they were before introducing the
8140 distinction between Imm8, Imm8S, and Imm8|Imm8S. */
8141 overlap.bitfield.imm8s = 0;
8142 }
8143
8144 if (overlap.bitfield.imm8
8145 + overlap.bitfield.imm8s
8146 + overlap.bitfield.imm16
8147 + overlap.bitfield.imm32
8148 + overlap.bitfield.imm32s
8149 + overlap.bitfield.imm64 > 1)
8150 {
8151 static const i386_operand_type imm16 = { .bitfield = { .imm16 = 1 } };
8152 static const i386_operand_type imm32 = { .bitfield = { .imm32 = 1 } };
8153 static const i386_operand_type imm32s = { .bitfield = { .imm32s = 1 } };
8154 static const i386_operand_type imm16_32 = { .bitfield =
8155 { .imm16 = 1, .imm32 = 1 }
8156 };
8157 static const i386_operand_type imm16_32s = { .bitfield =
8158 { .imm16 = 1, .imm32s = 1 }
8159 };
8160 static const i386_operand_type imm16_32_32s = { .bitfield =
8161 { .imm16 = 1, .imm32 = 1, .imm32s = 1 }
8162 };
8163
8164 if (i.suffix)
8165 {
8166 i386_operand_type temp;
8167
8168 operand_type_set (&temp, 0);
8169 if (i.suffix == BYTE_MNEM_SUFFIX)
8170 {
8171 temp.bitfield.imm8 = overlap.bitfield.imm8;
8172 temp.bitfield.imm8s = overlap.bitfield.imm8s;
8173 }
8174 else if (i.suffix == WORD_MNEM_SUFFIX)
8175 temp.bitfield.imm16 = overlap.bitfield.imm16;
8176 else if (i.suffix == QWORD_MNEM_SUFFIX)
8177 {
8178 temp.bitfield.imm64 = overlap.bitfield.imm64;
8179 temp.bitfield.imm32s = overlap.bitfield.imm32s;
8180 }
8181 else
8182 temp.bitfield.imm32 = overlap.bitfield.imm32;
8183 overlap = temp;
8184 }
8185 else if (operand_type_equal (&overlap, &imm16_32_32s)
8186 || operand_type_equal (&overlap, &imm16_32)
8187 || operand_type_equal (&overlap, &imm16_32s))
8188 {
8189 if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
8190 overlap = imm16;
8191 else
8192 overlap = imm32s;
8193 }
8194 else if (i.prefix[REX_PREFIX] & REX_W)
8195 overlap = operand_type_and (overlap, imm32s);
8196 else if (i.prefix[DATA_PREFIX])
8197 overlap = operand_type_and (overlap,
8198 flag_code != CODE_16BIT ? imm16 : imm32);
8199 if (overlap.bitfield.imm8
8200 + overlap.bitfield.imm8s
8201 + overlap.bitfield.imm16
8202 + overlap.bitfield.imm32
8203 + overlap.bitfield.imm32s
8204 + overlap.bitfield.imm64 != 1)
8205 {
8206 as_bad (_("no instruction mnemonic suffix given; "
8207 "can't determine immediate size"));
8208 return 0;
8209 }
8210 }
8211 i.types[j] = overlap;
8212
8213 return 1;
8214 }
8215
8216 static int
8217 finalize_imm (void)
8218 {
8219 unsigned int j, n;
8220
8221 /* Update the first 2 immediate operands. */
8222 n = i.operands > 2 ? 2 : i.operands;
8223 if (n)
8224 {
8225 for (j = 0; j < n; j++)
8226 if (update_imm (j) == 0)
8227 return 0;
8228
8229 /* The 3rd operand can't be immediate operand. */
8230 gas_assert (operand_type_check (i.types[2], imm) == 0);
8231 }
8232
8233 return 1;
8234 }
8235
8236 static INLINE void set_rex_vrex (const reg_entry *r, unsigned int rex_bit,
8237 bool do_sse2avx)
8238 {
8239 if (r->reg_flags & RegRex)
8240 {
8241 if (i.rex & rex_bit)
8242 as_bad (_("same type of prefix used twice"));
8243 i.rex |= rex_bit;
8244 }
8245 else if (do_sse2avx && (i.rex & rex_bit) && i.vex.register_specifier)
8246 {
8247 gas_assert (i.vex.register_specifier == r);
8248 i.vex.register_specifier += 8;
8249 }
8250
8251 if (r->reg_flags & RegVRex)
8252 i.vrex |= rex_bit;
8253 }
8254
8255 static int
8256 process_operands (void)
8257 {
8258 /* Default segment register this instruction will use for memory
8259 accesses. 0 means unknown. This is only for optimizing out
8260 unnecessary segment overrides. */
8261 const reg_entry *default_seg = NULL;
8262
8263 /* We only need to check those implicit registers for instructions
8264 with 3 operands or less. */
8265 if (i.operands <= 3)
8266 for (unsigned int j = 0; j < i.operands; j++)
8267 if (i.types[j].bitfield.instance != InstanceNone)
8268 i.reg_operands--;
8269
8270 if (i.tm.opcode_modifier.sse2avx)
8271 {
8272 /* Legacy encoded insns allow explicit REX prefixes, so these prefixes
8273 need converting. */
8274 i.rex |= i.prefix[REX_PREFIX] & (REX_W | REX_R | REX_X | REX_B);
8275 i.prefix[REX_PREFIX] = 0;
8276 i.rex_encoding = 0;
8277 }
8278 /* ImmExt should be processed after SSE2AVX. */
8279 else if (i.tm.opcode_modifier.immext)
8280 process_immext ();
8281
8282 /* TILEZERO is unusual in that it has a single operand encoded in ModR/M.reg,
8283 not ModR/M.rm. To avoid special casing this in build_modrm_byte(), fake a
8284 new destination operand here, while converting the source one to register
8285 number 0. */
8286 if (i.tm.mnem_off == MN_tilezero)
8287 {
8288 i.op[1].regs = i.op[0].regs;
8289 i.op[0].regs -= i.op[0].regs->reg_num;
8290 i.types[1] = i.types[0];
8291 i.tm.operand_types[1] = i.tm.operand_types[0];
8292 i.flags[1] = i.flags[0];
8293 i.operands++;
8294 i.reg_operands++;
8295 i.tm.operands++;
8296 }
8297
8298 if (i.tm.opcode_modifier.sse2avx && i.tm.opcode_modifier.vexvvvv)
8299 {
8300 static const i386_operand_type regxmm = {
8301 .bitfield = { .class = RegSIMD, .xmmword = 1 }
8302 };
8303 unsigned int dupl = i.operands;
8304 unsigned int dest = dupl - 1;
8305 unsigned int j;
8306
8307 /* The destination must be an xmm register. */
8308 gas_assert (i.reg_operands
8309 && MAX_OPERANDS > dupl
8310 && operand_type_equal (&i.types[dest], &regxmm));
8311
8312 if (i.tm.operand_types[0].bitfield.instance == Accum
8313 && i.tm.operand_types[0].bitfield.xmmword)
8314 {
8315 /* Keep xmm0 for instructions with VEX prefix and 3
8316 sources. */
8317 i.tm.operand_types[0].bitfield.instance = InstanceNone;
8318 i.tm.operand_types[0].bitfield.class = RegSIMD;
8319 i.reg_operands++;
8320 goto duplicate;
8321 }
8322
8323 if (i.tm.opcode_modifier.operandconstraint == IMPLICIT_1ST_XMM0)
8324 {
8325 gas_assert ((MAX_OPERANDS - 1) > dupl);
8326
8327 /* Add the implicit xmm0 for instructions with VEX prefix
8328 and 3 sources. */
8329 for (j = i.operands; j > 0; j--)
8330 {
8331 i.op[j] = i.op[j - 1];
8332 i.types[j] = i.types[j - 1];
8333 i.tm.operand_types[j] = i.tm.operand_types[j - 1];
8334 i.flags[j] = i.flags[j - 1];
8335 }
8336 i.op[0].regs
8337 = (const reg_entry *) str_hash_find (reg_hash, "xmm0");
8338 i.types[0] = regxmm;
8339 i.tm.operand_types[0] = regxmm;
8340
8341 i.operands += 2;
8342 i.reg_operands += 2;
8343 i.tm.operands += 2;
8344
8345 dupl++;
8346 dest++;
8347 i.op[dupl] = i.op[dest];
8348 i.types[dupl] = i.types[dest];
8349 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8350 i.flags[dupl] = i.flags[dest];
8351 }
8352 else
8353 {
8354 duplicate:
8355 i.operands++;
8356 i.reg_operands++;
8357 i.tm.operands++;
8358
8359 i.op[dupl] = i.op[dest];
8360 i.types[dupl] = i.types[dest];
8361 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8362 i.flags[dupl] = i.flags[dest];
8363 }
8364
8365 if (i.tm.opcode_modifier.immext)
8366 process_immext ();
8367 }
8368 else if (i.tm.operand_types[0].bitfield.instance == Accum
8369 && i.tm.opcode_modifier.modrm)
8370 {
8371 unsigned int j;
8372
8373 for (j = 1; j < i.operands; j++)
8374 {
8375 i.op[j - 1] = i.op[j];
8376 i.types[j - 1] = i.types[j];
8377
8378 /* We need to adjust fields in i.tm since they are used by
8379 build_modrm_byte. */
8380 i.tm.operand_types [j - 1] = i.tm.operand_types [j];
8381
8382 i.flags[j - 1] = i.flags[j];
8383 }
8384
8385 /* No adjustment to i.reg_operands: This was already done at the top
8386 of the function. */
8387 i.operands--;
8388 i.tm.operands--;
8389 }
8390 else if (i.tm.opcode_modifier.operandconstraint == IMPLICIT_QUAD_GROUP)
8391 {
8392 unsigned int regnum, first_reg_in_group, last_reg_in_group;
8393
8394 /* The second operand must be {x,y,z}mmN, where N is a multiple of 4. */
8395 gas_assert (i.operands >= 2 && i.types[1].bitfield.class == RegSIMD);
8396 regnum = register_number (i.op[1].regs);
8397 first_reg_in_group = regnum & ~3;
8398 last_reg_in_group = first_reg_in_group + 3;
8399 if (regnum != first_reg_in_group)
8400 as_warn (_("source register `%s%s' implicitly denotes"
8401 " `%s%.3s%u' to `%s%.3s%u' source group in `%s'"),
8402 register_prefix, i.op[1].regs->reg_name,
8403 register_prefix, i.op[1].regs->reg_name, first_reg_in_group,
8404 register_prefix, i.op[1].regs->reg_name, last_reg_in_group,
8405 insn_name (&i.tm));
8406 }
8407 else if (i.tm.opcode_modifier.operandconstraint == REG_KLUDGE)
8408 {
8409 /* The imul $imm, %reg instruction is converted into
8410 imul $imm, %reg, %reg, and the clr %reg instruction
8411 is converted into xor %reg, %reg. */
8412
8413 unsigned int first_reg_op;
8414
8415 if (operand_type_check (i.types[0], reg))
8416 first_reg_op = 0;
8417 else
8418 first_reg_op = 1;
8419 /* Pretend we saw the extra register operand. */
8420 gas_assert (i.reg_operands == 1
8421 && i.op[first_reg_op + 1].regs == 0);
8422 i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
8423 i.types[first_reg_op + 1] = i.types[first_reg_op];
8424 i.operands++;
8425 i.reg_operands++;
8426 }
8427
8428 if (i.tm.opcode_modifier.modrm)
8429 {
8430 /* The opcode is completed (modulo i.tm.extension_opcode which
8431 must be put into the modrm byte). Now, we make the modrm and
8432 index base bytes based on all the info we've collected. */
8433
8434 default_seg = build_modrm_byte ();
8435
8436 if (!quiet_warnings && i.tm.opcode_modifier.operandconstraint == UGH)
8437 {
8438 /* Warn about some common errors, but press on regardless. */
8439 if (i.operands == 2)
8440 {
8441 /* Reversed arguments on faddp or fmulp. */
8442 as_warn (_("translating to `%s %s%s,%s%s'"), insn_name (&i.tm),
8443 register_prefix, i.op[!intel_syntax].regs->reg_name,
8444 register_prefix, i.op[intel_syntax].regs->reg_name);
8445 }
8446 else if (i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
8447 {
8448 /* Extraneous `l' suffix on fp insn. */
8449 as_warn (_("translating to `%s %s%s'"), insn_name (&i.tm),
8450 register_prefix, i.op[0].regs->reg_name);
8451 }
8452 }
8453 }
8454 else if (i.types[0].bitfield.class == SReg && !dot_insn ())
8455 {
8456 if (flag_code != CODE_64BIT
8457 ? i.tm.base_opcode == POP_SEG_SHORT
8458 && i.op[0].regs->reg_num == 1
8459 : (i.tm.base_opcode | 1) == (POP_SEG386_SHORT & 0xff)
8460 && i.op[0].regs->reg_num < 4)
8461 {
8462 as_bad (_("you can't `%s %s%s'"),
8463 insn_name (&i.tm), register_prefix, i.op[0].regs->reg_name);
8464 return 0;
8465 }
8466 if (i.op[0].regs->reg_num > 3
8467 && i.tm.opcode_space == SPACE_BASE )
8468 {
8469 i.tm.base_opcode ^= (POP_SEG_SHORT ^ POP_SEG386_SHORT) & 0xff;
8470 i.tm.opcode_space = SPACE_0F;
8471 }
8472 i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
8473 }
8474 else if (i.tm.opcode_space == SPACE_BASE
8475 && (i.tm.base_opcode & ~3) == MOV_AX_DISP32)
8476 {
8477 default_seg = reg_ds;
8478 }
8479 else if (i.tm.opcode_modifier.isstring)
8480 {
8481 /* For the string instructions that allow a segment override
8482 on one of their operands, the default segment is ds. */
8483 default_seg = reg_ds;
8484 }
8485 else if (i.short_form)
8486 {
8487 /* The register operand is in the 1st or 2nd non-immediate operand. */
8488 const reg_entry *r = i.op[i.imm_operands].regs;
8489
8490 if (!dot_insn ()
8491 && r->reg_type.bitfield.instance == Accum
8492 && i.op[i.imm_operands + 1].regs)
8493 r = i.op[i.imm_operands + 1].regs;
8494 /* Register goes in low 3 bits of opcode. */
8495 i.tm.base_opcode |= r->reg_num;
8496 set_rex_vrex (r, REX_B, false);
8497
8498 if (dot_insn () && i.reg_operands == 2)
8499 {
8500 gas_assert (is_any_vex_encoding (&i.tm)
8501 || i.vec_encoding != vex_encoding_default);
8502 i.vex.register_specifier = i.op[i.operands - 1].regs;
8503 }
8504 }
8505 else if (i.reg_operands == 1
8506 && !i.flags[i.operands - 1]
8507 && i.tm.operand_types[i.operands - 1].bitfield.instance
8508 == InstanceNone)
8509 {
8510 gas_assert (is_any_vex_encoding (&i.tm)
8511 || i.vec_encoding != vex_encoding_default);
8512 i.vex.register_specifier = i.op[i.operands - 1].regs;
8513 }
8514
8515 if ((i.seg[0] || i.prefix[SEG_PREFIX])
8516 && i.tm.mnem_off == MN_lea)
8517 {
8518 if (!quiet_warnings)
8519 as_warn (_("segment override on `%s' is ineffectual"), insn_name (&i.tm));
8520 if (optimize && !i.no_optimize)
8521 {
8522 i.seg[0] = NULL;
8523 i.prefix[SEG_PREFIX] = 0;
8524 }
8525 }
8526
8527 /* If a segment was explicitly specified, and the specified segment
8528 is neither the default nor the one already recorded from a prefix,
8529 use an opcode prefix to select it. If we never figured out what
8530 the default segment is, then default_seg will be zero at this
8531 point, and the specified segment prefix will always be used. */
8532 if (i.seg[0]
8533 && i.seg[0] != default_seg
8534 && i386_seg_prefixes[i.seg[0]->reg_num] != i.prefix[SEG_PREFIX])
8535 {
8536 if (!add_prefix (i386_seg_prefixes[i.seg[0]->reg_num]))
8537 return 0;
8538 }
8539 return 1;
8540 }
8541
8542 static const reg_entry *
8543 build_modrm_byte (void)
8544 {
8545 const reg_entry *default_seg = NULL;
8546 unsigned int source = i.imm_operands - i.tm.opcode_modifier.immext
8547 /* Compensate for kludge in md_assemble(). */
8548 + i.tm.operand_types[0].bitfield.imm1;
8549 unsigned int dest = i.operands - 1 - i.tm.opcode_modifier.immext;
8550 unsigned int v, op, reg_slot = ~0;
8551
8552 /* Accumulator (in particular %st), shift count (%cl), and alike need
8553 to be skipped just like immediate operands do. */
8554 if (i.tm.operand_types[source].bitfield.instance)
8555 ++source;
8556 while (i.tm.operand_types[dest].bitfield.instance)
8557 --dest;
8558
8559 for (op = source; op < i.operands; ++op)
8560 if (i.tm.operand_types[op].bitfield.baseindex)
8561 break;
8562
8563 if (i.reg_operands + i.mem_operands + (i.tm.extension_opcode != None) == 4)
8564 {
8565 expressionS *exp;
8566
8567 /* There are 2 kinds of instructions:
8568 1. 5 operands: 4 register operands or 3 register operands
8569 plus 1 memory operand plus one Imm4 operand, VexXDS, and
8570 VexW0 or VexW1. The destination must be either XMM, YMM or
8571 ZMM register.
8572 2. 4 operands: 4 register operands or 3 register operands
8573 plus 1 memory operand, with VexXDS.
8574 3. Other equivalent combinations when coming from s_insn(). */
8575 gas_assert (i.tm.opcode_modifier.vexvvvv
8576 && i.tm.opcode_modifier.vexw);
8577 gas_assert (dot_insn ()
8578 || i.tm.operand_types[dest].bitfield.class == RegSIMD);
8579
8580 /* Of the first two non-immediate operands the one with the template
8581 not allowing for a memory one is encoded in the immediate operand. */
8582 if (source == op)
8583 reg_slot = source + 1;
8584 else
8585 reg_slot = source++;
8586
8587 if (!dot_insn ())
8588 {
8589 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
8590 gas_assert (!(i.op[reg_slot].regs->reg_flags & RegVRex));
8591 }
8592 else
8593 gas_assert (i.tm.operand_types[reg_slot].bitfield.class != ClassNone);
8594
8595 if (i.imm_operands == 0)
8596 {
8597 /* When there is no immediate operand, generate an 8bit
8598 immediate operand to encode the first operand. */
8599 exp = &im_expressions[i.imm_operands++];
8600 i.op[i.operands].imms = exp;
8601 i.types[i.operands].bitfield.imm8 = 1;
8602 i.operands++;
8603
8604 exp->X_op = O_constant;
8605 }
8606 else
8607 {
8608 gas_assert (i.imm_operands == 1);
8609 gas_assert (fits_in_imm4 (i.op[0].imms->X_add_number));
8610 gas_assert (!i.tm.opcode_modifier.immext);
8611
8612 /* Turn on Imm8 again so that output_imm will generate it. */
8613 i.types[0].bitfield.imm8 = 1;
8614
8615 exp = i.op[0].imms;
8616 }
8617 exp->X_add_number |= register_number (i.op[reg_slot].regs)
8618 << (3 + !(is_evex_encoding (&i.tm)
8619 || i.vec_encoding == vex_encoding_evex));
8620 }
8621
8622 for (v = source + 1; v < dest; ++v)
8623 if (v != reg_slot)
8624 break;
8625 if (v >= dest)
8626 v = ~0;
8627 if (i.tm.extension_opcode != None)
8628 {
8629 if (dest != source)
8630 v = dest;
8631 dest = ~0;
8632 }
8633 gas_assert (source < dest);
8634 if (i.tm.opcode_modifier.operandconstraint == SWAP_SOURCES
8635 && source != op)
8636 {
8637 unsigned int tmp = source;
8638
8639 source = v;
8640 v = tmp;
8641 }
8642
8643 if (v < MAX_OPERANDS)
8644 {
8645 gas_assert (i.tm.opcode_modifier.vexvvvv);
8646 i.vex.register_specifier = i.op[v].regs;
8647 }
8648
8649 if (op < i.operands)
8650 {
8651 if (i.mem_operands)
8652 {
8653 unsigned int fake_zero_displacement = 0;
8654
8655 gas_assert (i.flags[op] & Operand_Mem);
8656
8657 if (i.tm.opcode_modifier.sib)
8658 {
8659 /* The index register of VSIB shouldn't be RegIZ. */
8660 if (i.tm.opcode_modifier.sib != SIBMEM
8661 && i.index_reg->reg_num == RegIZ)
8662 abort ();
8663
8664 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8665 if (!i.base_reg)
8666 {
8667 i.sib.base = NO_BASE_REGISTER;
8668 i.sib.scale = i.log2_scale_factor;
8669 i.types[op] = operand_type_and_not (i.types[op], anydisp);
8670 i.types[op].bitfield.disp32 = 1;
8671 }
8672
8673 /* Since the mandatory SIB always has index register, so
8674 the code logic remains unchanged. The non-mandatory SIB
8675 without index register is allowed and will be handled
8676 later. */
8677 if (i.index_reg)
8678 {
8679 if (i.index_reg->reg_num == RegIZ)
8680 i.sib.index = NO_INDEX_REGISTER;
8681 else
8682 i.sib.index = i.index_reg->reg_num;
8683 set_rex_vrex (i.index_reg, REX_X, false);
8684 }
8685 }
8686
8687 default_seg = reg_ds;
8688
8689 if (i.base_reg == 0)
8690 {
8691 i.rm.mode = 0;
8692 if (!i.disp_operands)
8693 fake_zero_displacement = 1;
8694 if (i.index_reg == 0)
8695 {
8696 /* Both check for VSIB and mandatory non-vector SIB. */
8697 gas_assert (!i.tm.opcode_modifier.sib
8698 || i.tm.opcode_modifier.sib == SIBMEM);
8699 /* Operand is just <disp> */
8700 i.types[op] = operand_type_and_not (i.types[op], anydisp);
8701 if (flag_code == CODE_64BIT)
8702 {
8703 /* 64bit mode overwrites the 32bit absolute
8704 addressing by RIP relative addressing and
8705 absolute addressing is encoded by one of the
8706 redundant SIB forms. */
8707 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8708 i.sib.base = NO_BASE_REGISTER;
8709 i.sib.index = NO_INDEX_REGISTER;
8710 i.types[op].bitfield.disp32 = 1;
8711 }
8712 else if ((flag_code == CODE_16BIT)
8713 ^ (i.prefix[ADDR_PREFIX] != 0))
8714 {
8715 i.rm.regmem = NO_BASE_REGISTER_16;
8716 i.types[op].bitfield.disp16 = 1;
8717 }
8718 else
8719 {
8720 i.rm.regmem = NO_BASE_REGISTER;
8721 i.types[op].bitfield.disp32 = 1;
8722 }
8723 }
8724 else if (!i.tm.opcode_modifier.sib)
8725 {
8726 /* !i.base_reg && i.index_reg */
8727 if (i.index_reg->reg_num == RegIZ)
8728 i.sib.index = NO_INDEX_REGISTER;
8729 else
8730 i.sib.index = i.index_reg->reg_num;
8731 i.sib.base = NO_BASE_REGISTER;
8732 i.sib.scale = i.log2_scale_factor;
8733 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8734 i.types[op] = operand_type_and_not (i.types[op], anydisp);
8735 i.types[op].bitfield.disp32 = 1;
8736 if ((i.index_reg->reg_flags & RegRex) != 0)
8737 i.rex |= REX_X;
8738 }
8739 }
8740 /* RIP addressing for 64bit mode. */
8741 else if (i.base_reg->reg_num == RegIP)
8742 {
8743 gas_assert (!i.tm.opcode_modifier.sib);
8744 i.rm.regmem = NO_BASE_REGISTER;
8745 i.types[op].bitfield.disp8 = 0;
8746 i.types[op].bitfield.disp16 = 0;
8747 i.types[op].bitfield.disp32 = 1;
8748 i.types[op].bitfield.disp64 = 0;
8749 i.flags[op] |= Operand_PCrel;
8750 if (! i.disp_operands)
8751 fake_zero_displacement = 1;
8752 }
8753 else if (i.base_reg->reg_type.bitfield.word)
8754 {
8755 gas_assert (!i.tm.opcode_modifier.sib);
8756 switch (i.base_reg->reg_num)
8757 {
8758 case 3: /* (%bx) */
8759 if (i.index_reg == 0)
8760 i.rm.regmem = 7;
8761 else /* (%bx,%si) -> 0, or (%bx,%di) -> 1 */
8762 i.rm.regmem = i.index_reg->reg_num - 6;
8763 break;
8764 case 5: /* (%bp) */
8765 default_seg = reg_ss;
8766 if (i.index_reg == 0)
8767 {
8768 i.rm.regmem = 6;
8769 if (operand_type_check (i.types[op], disp) == 0)
8770 {
8771 /* fake (%bp) into 0(%bp) */
8772 if (i.disp_encoding == disp_encoding_16bit)
8773 i.types[op].bitfield.disp16 = 1;
8774 else
8775 i.types[op].bitfield.disp8 = 1;
8776 fake_zero_displacement = 1;
8777 }
8778 }
8779 else /* (%bp,%si) -> 2, or (%bp,%di) -> 3 */
8780 i.rm.regmem = i.index_reg->reg_num - 6 + 2;
8781 break;
8782 default: /* (%si) -> 4 or (%di) -> 5 */
8783 i.rm.regmem = i.base_reg->reg_num - 6 + 4;
8784 }
8785 if (!fake_zero_displacement
8786 && !i.disp_operands
8787 && i.disp_encoding)
8788 {
8789 fake_zero_displacement = 1;
8790 if (i.disp_encoding == disp_encoding_8bit)
8791 i.types[op].bitfield.disp8 = 1;
8792 else
8793 i.types[op].bitfield.disp16 = 1;
8794 }
8795 i.rm.mode = mode_from_disp_size (i.types[op]);
8796 }
8797 else /* i.base_reg and 32/64 bit mode */
8798 {
8799 if (operand_type_check (i.types[op], disp))
8800 {
8801 i.types[op].bitfield.disp16 = 0;
8802 i.types[op].bitfield.disp64 = 0;
8803 i.types[op].bitfield.disp32 = 1;
8804 }
8805
8806 if (!i.tm.opcode_modifier.sib)
8807 i.rm.regmem = i.base_reg->reg_num;
8808 if ((i.base_reg->reg_flags & RegRex) != 0)
8809 i.rex |= REX_B;
8810 i.sib.base = i.base_reg->reg_num;
8811 /* x86-64 ignores REX prefix bit here to avoid decoder
8812 complications. */
8813 if (!(i.base_reg->reg_flags & RegRex)
8814 && (i.base_reg->reg_num == EBP_REG_NUM
8815 || i.base_reg->reg_num == ESP_REG_NUM))
8816 default_seg = reg_ss;
8817 if (i.base_reg->reg_num == 5 && i.disp_operands == 0)
8818 {
8819 fake_zero_displacement = 1;
8820 if (i.disp_encoding == disp_encoding_32bit)
8821 i.types[op].bitfield.disp32 = 1;
8822 else
8823 i.types[op].bitfield.disp8 = 1;
8824 }
8825 i.sib.scale = i.log2_scale_factor;
8826 if (i.index_reg == 0)
8827 {
8828 /* Only check for VSIB. */
8829 gas_assert (i.tm.opcode_modifier.sib != VECSIB128
8830 && i.tm.opcode_modifier.sib != VECSIB256
8831 && i.tm.opcode_modifier.sib != VECSIB512);
8832
8833 /* <disp>(%esp) becomes two byte modrm with no index
8834 register. We've already stored the code for esp
8835 in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
8836 Any base register besides %esp will not use the
8837 extra modrm byte. */
8838 i.sib.index = NO_INDEX_REGISTER;
8839 }
8840 else if (!i.tm.opcode_modifier.sib)
8841 {
8842 if (i.index_reg->reg_num == RegIZ)
8843 i.sib.index = NO_INDEX_REGISTER;
8844 else
8845 i.sib.index = i.index_reg->reg_num;
8846 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8847 if ((i.index_reg->reg_flags & RegRex) != 0)
8848 i.rex |= REX_X;
8849 }
8850
8851 if (i.disp_operands
8852 && (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
8853 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL))
8854 i.rm.mode = 0;
8855 else
8856 {
8857 if (!fake_zero_displacement
8858 && !i.disp_operands
8859 && i.disp_encoding)
8860 {
8861 fake_zero_displacement = 1;
8862 if (i.disp_encoding == disp_encoding_8bit)
8863 i.types[op].bitfield.disp8 = 1;
8864 else
8865 i.types[op].bitfield.disp32 = 1;
8866 }
8867 i.rm.mode = mode_from_disp_size (i.types[op]);
8868 }
8869 }
8870
8871 if (fake_zero_displacement)
8872 {
8873 /* Fakes a zero displacement assuming that i.types[op]
8874 holds the correct displacement size. */
8875 expressionS *exp;
8876
8877 gas_assert (i.op[op].disps == 0);
8878 exp = &disp_expressions[i.disp_operands++];
8879 i.op[op].disps = exp;
8880 exp->X_op = O_constant;
8881 exp->X_add_number = 0;
8882 exp->X_add_symbol = (symbolS *) 0;
8883 exp->X_op_symbol = (symbolS *) 0;
8884 }
8885 }
8886 else
8887 {
8888 i.rm.mode = 3;
8889 i.rm.regmem = i.op[op].regs->reg_num;
8890 set_rex_vrex (i.op[op].regs, REX_B, false);
8891 }
8892
8893 if (op == dest)
8894 dest = ~0;
8895 if (op == source)
8896 source = ~0;
8897 }
8898 else
8899 {
8900 i.rm.mode = 3;
8901 if (!i.tm.opcode_modifier.regmem)
8902 {
8903 gas_assert (source < MAX_OPERANDS);
8904 i.rm.regmem = i.op[source].regs->reg_num;
8905 set_rex_vrex (i.op[source].regs, REX_B,
8906 dest >= MAX_OPERANDS && i.tm.opcode_modifier.sse2avx);
8907 source = ~0;
8908 }
8909 else
8910 {
8911 gas_assert (dest < MAX_OPERANDS);
8912 i.rm.regmem = i.op[dest].regs->reg_num;
8913 set_rex_vrex (i.op[dest].regs, REX_B, i.tm.opcode_modifier.sse2avx);
8914 dest = ~0;
8915 }
8916 }
8917
8918 /* Fill in i.rm.reg field with extension opcode (if any) or the
8919 appropriate register. */
8920 if (i.tm.extension_opcode != None)
8921 i.rm.reg = i.tm.extension_opcode;
8922 else if (!i.tm.opcode_modifier.regmem && dest < MAX_OPERANDS)
8923 {
8924 i.rm.reg = i.op[dest].regs->reg_num;
8925 set_rex_vrex (i.op[dest].regs, REX_R, i.tm.opcode_modifier.sse2avx);
8926 }
8927 else
8928 {
8929 gas_assert (source < MAX_OPERANDS);
8930 i.rm.reg = i.op[source].regs->reg_num;
8931 set_rex_vrex (i.op[source].regs, REX_R, false);
8932 }
8933
8934 if (flag_code != CODE_64BIT && (i.rex & REX_R))
8935 {
8936 gas_assert (i.types[!i.tm.opcode_modifier.regmem].bitfield.class == RegCR);
8937 i.rex &= ~REX_R;
8938 add_prefix (LOCK_PREFIX_OPCODE);
8939 }
8940
8941 return default_seg;
8942 }
8943
8944 static INLINE void
8945 frag_opcode_byte (unsigned char byte)
8946 {
8947 if (now_seg != absolute_section)
8948 FRAG_APPEND_1_CHAR (byte);
8949 else
8950 ++abs_section_offset;
8951 }
8952
8953 static unsigned int
8954 flip_code16 (unsigned int code16)
8955 {
8956 gas_assert (i.tm.operands == 1);
8957
8958 return !(i.prefix[REX_PREFIX] & REX_W)
8959 && (code16 ? i.tm.operand_types[0].bitfield.disp32
8960 : i.tm.operand_types[0].bitfield.disp16)
8961 ? CODE16 : 0;
8962 }
8963
8964 static void
8965 output_branch (void)
8966 {
8967 char *p;
8968 int size;
8969 int code16;
8970 int prefix;
8971 relax_substateT subtype;
8972 symbolS *sym;
8973 offsetT off;
8974
8975 if (now_seg == absolute_section)
8976 {
8977 as_bad (_("relaxable branches not supported in absolute section"));
8978 return;
8979 }
8980
8981 code16 = flag_code == CODE_16BIT ? CODE16 : 0;
8982 size = i.disp_encoding > disp_encoding_8bit ? BIG : SMALL;
8983
8984 prefix = 0;
8985 if (i.prefix[DATA_PREFIX] != 0)
8986 {
8987 prefix = 1;
8988 i.prefixes -= 1;
8989 code16 ^= flip_code16(code16);
8990 }
8991 /* Pentium4 branch hints. */
8992 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8993 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
8994 {
8995 prefix++;
8996 i.prefixes--;
8997 }
8998 if (i.prefix[REX_PREFIX] != 0)
8999 {
9000 prefix++;
9001 i.prefixes--;
9002 }
9003
9004 /* BND prefixed jump. */
9005 if (i.prefix[BND_PREFIX] != 0)
9006 {
9007 prefix++;
9008 i.prefixes--;
9009 }
9010
9011 if (i.prefixes != 0)
9012 as_warn (_("skipping prefixes on `%s'"), insn_name (&i.tm));
9013
9014 /* It's always a symbol; End frag & setup for relax.
9015 Make sure there is enough room in this frag for the largest
9016 instruction we may generate in md_convert_frag. This is 2
9017 bytes for the opcode and room for the prefix and largest
9018 displacement. */
9019 frag_grow (prefix + 2 + 4);
9020 /* Prefix and 1 opcode byte go in fr_fix. */
9021 p = frag_more (prefix + 1);
9022 if (i.prefix[DATA_PREFIX] != 0)
9023 *p++ = DATA_PREFIX_OPCODE;
9024 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
9025 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
9026 *p++ = i.prefix[SEG_PREFIX];
9027 if (i.prefix[BND_PREFIX] != 0)
9028 *p++ = BND_PREFIX_OPCODE;
9029 if (i.prefix[REX_PREFIX] != 0)
9030 *p++ = i.prefix[REX_PREFIX];
9031 *p = i.tm.base_opcode;
9032
9033 if ((unsigned char) *p == JUMP_PC_RELATIVE)
9034 subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, size);
9035 else if (cpu_arch_flags.bitfield.cpui386)
9036 subtype = ENCODE_RELAX_STATE (COND_JUMP, size);
9037 else
9038 subtype = ENCODE_RELAX_STATE (COND_JUMP86, size);
9039 subtype |= code16;
9040
9041 sym = i.op[0].disps->X_add_symbol;
9042 off = i.op[0].disps->X_add_number;
9043
9044 if (i.op[0].disps->X_op != O_constant
9045 && i.op[0].disps->X_op != O_symbol)
9046 {
9047 /* Handle complex expressions. */
9048 sym = make_expr_symbol (i.op[0].disps);
9049 off = 0;
9050 }
9051
9052 frag_now->tc_frag_data.code64 = flag_code == CODE_64BIT;
9053
9054 /* 1 possible extra opcode + 4 byte displacement go in var part.
9055 Pass reloc in fr_var. */
9056 frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
9057 }
9058
9059 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9060 /* Return TRUE iff PLT32 relocation should be used for branching to
9061 symbol S. */
9062
9063 static bool
9064 need_plt32_p (symbolS *s)
9065 {
9066 /* PLT32 relocation is ELF only. */
9067 if (!IS_ELF)
9068 return false;
9069
9070 #ifdef TE_SOLARIS
9071 /* Don't emit PLT32 relocation on Solaris: neither native linker nor
9072 krtld support it. */
9073 return false;
9074 #endif
9075
9076 /* Since there is no need to prepare for PLT branch on x86-64, we
9077 can generate R_X86_64_PLT32, instead of R_X86_64_PC32, which can
9078 be used as a marker for 32-bit PC-relative branches. */
9079 if (!object_64bit)
9080 return false;
9081
9082 if (s == NULL)
9083 return false;
9084
9085 /* Weak or undefined symbol need PLT32 relocation. */
9086 if (S_IS_WEAK (s) || !S_IS_DEFINED (s))
9087 return true;
9088
9089 /* Non-global symbol doesn't need PLT32 relocation. */
9090 if (! S_IS_EXTERNAL (s))
9091 return false;
9092
9093 /* Other global symbols need PLT32 relocation. NB: Symbol with
9094 non-default visibilities are treated as normal global symbol
9095 so that PLT32 relocation can be used as a marker for 32-bit
9096 PC-relative branches. It is useful for linker relaxation. */
9097 return true;
9098 }
9099 #endif
9100
9101 static void
9102 output_jump (void)
9103 {
9104 char *p;
9105 int size;
9106 fixS *fixP;
9107 bfd_reloc_code_real_type jump_reloc = i.reloc[0];
9108
9109 if (i.tm.opcode_modifier.jump == JUMP_BYTE)
9110 {
9111 /* This is a loop or jecxz type instruction. */
9112 size = 1;
9113 if (i.prefix[ADDR_PREFIX] != 0)
9114 {
9115 frag_opcode_byte (ADDR_PREFIX_OPCODE);
9116 i.prefixes -= 1;
9117 }
9118 /* Pentium4 branch hints. */
9119 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
9120 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
9121 {
9122 frag_opcode_byte (i.prefix[SEG_PREFIX]);
9123 i.prefixes--;
9124 }
9125 }
9126 else
9127 {
9128 int code16;
9129
9130 code16 = 0;
9131 if (flag_code == CODE_16BIT)
9132 code16 = CODE16;
9133
9134 if (i.prefix[DATA_PREFIX] != 0)
9135 {
9136 frag_opcode_byte (DATA_PREFIX_OPCODE);
9137 i.prefixes -= 1;
9138 code16 ^= flip_code16(code16);
9139 }
9140
9141 size = 4;
9142 if (code16)
9143 size = 2;
9144 }
9145
9146 /* BND prefixed jump. */
9147 if (i.prefix[BND_PREFIX] != 0)
9148 {
9149 frag_opcode_byte (i.prefix[BND_PREFIX]);
9150 i.prefixes -= 1;
9151 }
9152
9153 if (i.prefix[REX_PREFIX] != 0)
9154 {
9155 frag_opcode_byte (i.prefix[REX_PREFIX]);
9156 i.prefixes -= 1;
9157 }
9158
9159 if (i.prefixes != 0)
9160 as_warn (_("skipping prefixes on `%s'"), insn_name (&i.tm));
9161
9162 if (now_seg == absolute_section)
9163 {
9164 abs_section_offset += i.opcode_length + size;
9165 return;
9166 }
9167
9168 p = frag_more (i.opcode_length + size);
9169 switch (i.opcode_length)
9170 {
9171 case 2:
9172 *p++ = i.tm.base_opcode >> 8;
9173 /* Fall through. */
9174 case 1:
9175 *p++ = i.tm.base_opcode;
9176 break;
9177 default:
9178 abort ();
9179 }
9180
9181 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9182 if (flag_code == CODE_64BIT && size == 4
9183 && jump_reloc == NO_RELOC && i.op[0].disps->X_add_number == 0
9184 && need_plt32_p (i.op[0].disps->X_add_symbol))
9185 jump_reloc = BFD_RELOC_X86_64_PLT32;
9186 #endif
9187
9188 jump_reloc = reloc (size, 1, 1, jump_reloc);
9189
9190 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
9191 i.op[0].disps, 1, jump_reloc);
9192
9193 /* All jumps handled here are signed, but don't unconditionally use a
9194 signed limit check for 32 and 16 bit jumps as we want to allow wrap
9195 around at 4G (outside of 64-bit mode) and 64k (except for XBEGIN)
9196 respectively. */
9197 switch (size)
9198 {
9199 case 1:
9200 fixP->fx_signed = 1;
9201 break;
9202
9203 case 2:
9204 if (i.tm.mnem_off == MN_xbegin)
9205 fixP->fx_signed = 1;
9206 break;
9207
9208 case 4:
9209 if (flag_code == CODE_64BIT)
9210 fixP->fx_signed = 1;
9211 break;
9212 }
9213 }
9214
9215 static void
9216 output_interseg_jump (void)
9217 {
9218 char *p;
9219 int size;
9220 int prefix;
9221 int code16;
9222
9223 code16 = 0;
9224 if (flag_code == CODE_16BIT)
9225 code16 = CODE16;
9226
9227 prefix = 0;
9228 if (i.prefix[DATA_PREFIX] != 0)
9229 {
9230 prefix = 1;
9231 i.prefixes -= 1;
9232 code16 ^= CODE16;
9233 }
9234
9235 gas_assert (!i.prefix[REX_PREFIX]);
9236
9237 size = 4;
9238 if (code16)
9239 size = 2;
9240
9241 if (i.prefixes != 0)
9242 as_warn (_("skipping prefixes on `%s'"), insn_name (&i.tm));
9243
9244 if (now_seg == absolute_section)
9245 {
9246 abs_section_offset += prefix + 1 + 2 + size;
9247 return;
9248 }
9249
9250 /* 1 opcode; 2 segment; offset */
9251 p = frag_more (prefix + 1 + 2 + size);
9252
9253 if (i.prefix[DATA_PREFIX] != 0)
9254 *p++ = DATA_PREFIX_OPCODE;
9255
9256 if (i.prefix[REX_PREFIX] != 0)
9257 *p++ = i.prefix[REX_PREFIX];
9258
9259 *p++ = i.tm.base_opcode;
9260 if (i.op[1].imms->X_op == O_constant)
9261 {
9262 offsetT n = i.op[1].imms->X_add_number;
9263
9264 if (size == 2
9265 && !fits_in_unsigned_word (n)
9266 && !fits_in_signed_word (n))
9267 {
9268 as_bad (_("16-bit jump out of range"));
9269 return;
9270 }
9271 md_number_to_chars (p, n, size);
9272 }
9273 else
9274 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
9275 i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
9276
9277 p += size;
9278 if (i.op[0].imms->X_op == O_constant)
9279 md_number_to_chars (p, (valueT) i.op[0].imms->X_add_number, 2);
9280 else
9281 fix_new_exp (frag_now, p - frag_now->fr_literal, 2,
9282 i.op[0].imms, 0, reloc (2, 0, 0, i.reloc[0]));
9283 }
9284
9285 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9286 void
9287 x86_cleanup (void)
9288 {
9289 char *p;
9290 asection *seg = now_seg;
9291 subsegT subseg = now_subseg;
9292 asection *sec;
9293 unsigned int alignment, align_size_1;
9294 unsigned int isa_1_descsz, feature_2_descsz, descsz;
9295 unsigned int isa_1_descsz_raw, feature_2_descsz_raw;
9296 unsigned int padding;
9297
9298 if (!IS_ELF || !x86_used_note)
9299 return;
9300
9301 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X86;
9302
9303 /* The .note.gnu.property section layout:
9304
9305 Field Length Contents
9306 ---- ---- ----
9307 n_namsz 4 4
9308 n_descsz 4 The note descriptor size
9309 n_type 4 NT_GNU_PROPERTY_TYPE_0
9310 n_name 4 "GNU"
9311 n_desc n_descsz The program property array
9312 .... .... ....
9313 */
9314
9315 /* Create the .note.gnu.property section. */
9316 sec = subseg_new (NOTE_GNU_PROPERTY_SECTION_NAME, 0);
9317 bfd_set_section_flags (sec,
9318 (SEC_ALLOC
9319 | SEC_LOAD
9320 | SEC_DATA
9321 | SEC_HAS_CONTENTS
9322 | SEC_READONLY));
9323
9324 if (get_elf_backend_data (stdoutput)->s->elfclass == ELFCLASS64)
9325 {
9326 align_size_1 = 7;
9327 alignment = 3;
9328 }
9329 else
9330 {
9331 align_size_1 = 3;
9332 alignment = 2;
9333 }
9334
9335 bfd_set_section_alignment (sec, alignment);
9336 elf_section_type (sec) = SHT_NOTE;
9337
9338 /* GNU_PROPERTY_X86_ISA_1_USED: 4-byte type + 4-byte data size
9339 + 4-byte data */
9340 isa_1_descsz_raw = 4 + 4 + 4;
9341 /* Align GNU_PROPERTY_X86_ISA_1_USED. */
9342 isa_1_descsz = (isa_1_descsz_raw + align_size_1) & ~align_size_1;
9343
9344 feature_2_descsz_raw = isa_1_descsz;
9345 /* GNU_PROPERTY_X86_FEATURE_2_USED: 4-byte type + 4-byte data size
9346 + 4-byte data */
9347 feature_2_descsz_raw += 4 + 4 + 4;
9348 /* Align GNU_PROPERTY_X86_FEATURE_2_USED. */
9349 feature_2_descsz = ((feature_2_descsz_raw + align_size_1)
9350 & ~align_size_1);
9351
9352 descsz = feature_2_descsz;
9353 /* Section size: n_namsz + n_descsz + n_type + n_name + n_descsz. */
9354 p = frag_more (4 + 4 + 4 + 4 + descsz);
9355
9356 /* Write n_namsz. */
9357 md_number_to_chars (p, (valueT) 4, 4);
9358
9359 /* Write n_descsz. */
9360 md_number_to_chars (p + 4, (valueT) descsz, 4);
9361
9362 /* Write n_type. */
9363 md_number_to_chars (p + 4 * 2, (valueT) NT_GNU_PROPERTY_TYPE_0, 4);
9364
9365 /* Write n_name. */
9366 memcpy (p + 4 * 3, "GNU", 4);
9367
9368 /* Write 4-byte type. */
9369 md_number_to_chars (p + 4 * 4,
9370 (valueT) GNU_PROPERTY_X86_ISA_1_USED, 4);
9371
9372 /* Write 4-byte data size. */
9373 md_number_to_chars (p + 4 * 5, (valueT) 4, 4);
9374
9375 /* Write 4-byte data. */
9376 md_number_to_chars (p + 4 * 6, (valueT) x86_isa_1_used, 4);
9377
9378 /* Zero out paddings. */
9379 padding = isa_1_descsz - isa_1_descsz_raw;
9380 if (padding)
9381 memset (p + 4 * 7, 0, padding);
9382
9383 /* Write 4-byte type. */
9384 md_number_to_chars (p + isa_1_descsz + 4 * 4,
9385 (valueT) GNU_PROPERTY_X86_FEATURE_2_USED, 4);
9386
9387 /* Write 4-byte data size. */
9388 md_number_to_chars (p + isa_1_descsz + 4 * 5, (valueT) 4, 4);
9389
9390 /* Write 4-byte data. */
9391 md_number_to_chars (p + isa_1_descsz + 4 * 6,
9392 (valueT) x86_feature_2_used, 4);
9393
9394 /* Zero out paddings. */
9395 padding = feature_2_descsz - feature_2_descsz_raw;
9396 if (padding)
9397 memset (p + isa_1_descsz + 4 * 7, 0, padding);
9398
9399 /* We probably can't restore the current segment, for there likely
9400 isn't one yet... */
9401 if (seg && subseg)
9402 subseg_set (seg, subseg);
9403 }
9404
9405 bool
9406 x86_support_sframe_p (void)
9407 {
9408 /* At this time, SFrame stack trace is supported for AMD64 ABI only. */
9409 return (x86_elf_abi == X86_64_ABI);
9410 }
9411
9412 bool
9413 x86_sframe_ra_tracking_p (void)
9414 {
9415 /* In AMD64, return address is always stored on the stack at a fixed offset
9416 from the CFA (provided via x86_sframe_cfa_ra_offset ()).
9417 Do not track explicitly via an SFrame Frame Row Entry. */
9418 return false;
9419 }
9420
9421 offsetT
9422 x86_sframe_cfa_ra_offset (void)
9423 {
9424 gas_assert (x86_elf_abi == X86_64_ABI);
9425 return (offsetT) -8;
9426 }
9427
9428 unsigned char
9429 x86_sframe_get_abi_arch (void)
9430 {
9431 unsigned char sframe_abi_arch = 0;
9432
9433 if (x86_support_sframe_p ())
9434 {
9435 gas_assert (!target_big_endian);
9436 sframe_abi_arch = SFRAME_ABI_AMD64_ENDIAN_LITTLE;
9437 }
9438
9439 return sframe_abi_arch;
9440 }
9441
9442 #endif
9443
9444 static unsigned int
9445 encoding_length (const fragS *start_frag, offsetT start_off,
9446 const char *frag_now_ptr)
9447 {
9448 unsigned int len = 0;
9449
9450 if (start_frag != frag_now)
9451 {
9452 const fragS *fr = start_frag;
9453
9454 do {
9455 len += fr->fr_fix;
9456 fr = fr->fr_next;
9457 } while (fr && fr != frag_now);
9458 }
9459
9460 return len - start_off + (frag_now_ptr - frag_now->fr_literal);
9461 }
9462
9463 /* Return 1 for test, and, cmp, add, sub, inc and dec which may
9464 be macro-fused with conditional jumps.
9465 NB: If TEST/AND/CMP/ADD/SUB/INC/DEC is of RIP relative address,
9466 or is one of the following format:
9467
9468 cmp m, imm
9469 add m, imm
9470 sub m, imm
9471 test m, imm
9472 and m, imm
9473 inc m
9474 dec m
9475
9476 it is unfusible. */
9477
9478 static int
9479 maybe_fused_with_jcc_p (enum mf_cmp_kind* mf_cmp_p)
9480 {
9481 /* No RIP address. */
9482 if (i.base_reg && i.base_reg->reg_num == RegIP)
9483 return 0;
9484
9485 /* No opcodes outside of base encoding space. */
9486 if (i.tm.opcode_space != SPACE_BASE)
9487 return 0;
9488
9489 /* add, sub without add/sub m, imm. */
9490 if (i.tm.base_opcode <= 5
9491 || (i.tm.base_opcode >= 0x28 && i.tm.base_opcode <= 0x2d)
9492 || ((i.tm.base_opcode | 3) == 0x83
9493 && (i.tm.extension_opcode == 0x5
9494 || i.tm.extension_opcode == 0x0)))
9495 {
9496 *mf_cmp_p = mf_cmp_alu_cmp;
9497 return !(i.mem_operands && i.imm_operands);
9498 }
9499
9500 /* and without and m, imm. */
9501 if ((i.tm.base_opcode >= 0x20 && i.tm.base_opcode <= 0x25)
9502 || ((i.tm.base_opcode | 3) == 0x83
9503 && i.tm.extension_opcode == 0x4))
9504 {
9505 *mf_cmp_p = mf_cmp_test_and;
9506 return !(i.mem_operands && i.imm_operands);
9507 }
9508
9509 /* test without test m imm. */
9510 if ((i.tm.base_opcode | 1) == 0x85
9511 || (i.tm.base_opcode | 1) == 0xa9
9512 || ((i.tm.base_opcode | 1) == 0xf7
9513 && i.tm.extension_opcode == 0))
9514 {
9515 *mf_cmp_p = mf_cmp_test_and;
9516 return !(i.mem_operands && i.imm_operands);
9517 }
9518
9519 /* cmp without cmp m, imm. */
9520 if ((i.tm.base_opcode >= 0x38 && i.tm.base_opcode <= 0x3d)
9521 || ((i.tm.base_opcode | 3) == 0x83
9522 && (i.tm.extension_opcode == 0x7)))
9523 {
9524 *mf_cmp_p = mf_cmp_alu_cmp;
9525 return !(i.mem_operands && i.imm_operands);
9526 }
9527
9528 /* inc, dec without inc/dec m. */
9529 if ((is_cpu (&i.tm, CpuNo64)
9530 && (i.tm.base_opcode | 0xf) == 0x4f)
9531 || ((i.tm.base_opcode | 1) == 0xff
9532 && i.tm.extension_opcode <= 0x1))
9533 {
9534 *mf_cmp_p = mf_cmp_incdec;
9535 return !i.mem_operands;
9536 }
9537
9538 return 0;
9539 }
9540
9541 /* Return 1 if a FUSED_JCC_PADDING frag should be generated. */
9542
9543 static int
9544 add_fused_jcc_padding_frag_p (enum mf_cmp_kind* mf_cmp_p)
9545 {
9546 /* NB: Don't work with COND_JUMP86 without i386. */
9547 if (!align_branch_power
9548 || now_seg == absolute_section
9549 || !cpu_arch_flags.bitfield.cpui386
9550 || !(align_branch & align_branch_fused_bit))
9551 return 0;
9552
9553 if (maybe_fused_with_jcc_p (mf_cmp_p))
9554 {
9555 if (last_insn.kind == last_insn_other
9556 || last_insn.seg != now_seg)
9557 return 1;
9558 if (flag_debug)
9559 as_warn_where (last_insn.file, last_insn.line,
9560 _("`%s` skips -malign-branch-boundary on `%s`"),
9561 last_insn.name, insn_name (&i.tm));
9562 }
9563
9564 return 0;
9565 }
9566
9567 /* Return 1 if a BRANCH_PREFIX frag should be generated. */
9568
9569 static int
9570 add_branch_prefix_frag_p (void)
9571 {
9572 /* NB: Don't work with COND_JUMP86 without i386. Don't add prefix
9573 to PadLock instructions since they include prefixes in opcode. */
9574 if (!align_branch_power
9575 || !align_branch_prefix_size
9576 || now_seg == absolute_section
9577 || is_cpu (&i.tm, CpuPadLock)
9578 || !cpu_arch_flags.bitfield.cpui386)
9579 return 0;
9580
9581 /* Don't add prefix if it is a prefix or there is no operand in case
9582 that segment prefix is special. */
9583 if (!i.operands || i.tm.opcode_modifier.isprefix)
9584 return 0;
9585
9586 if (last_insn.kind == last_insn_other
9587 || last_insn.seg != now_seg)
9588 return 1;
9589
9590 if (flag_debug)
9591 as_warn_where (last_insn.file, last_insn.line,
9592 _("`%s` skips -malign-branch-boundary on `%s`"),
9593 last_insn.name, insn_name (&i.tm));
9594
9595 return 0;
9596 }
9597
9598 /* Return 1 if a BRANCH_PADDING frag should be generated. */
9599
9600 static int
9601 add_branch_padding_frag_p (enum align_branch_kind *branch_p,
9602 enum mf_jcc_kind *mf_jcc_p)
9603 {
9604 int add_padding;
9605
9606 /* NB: Don't work with COND_JUMP86 without i386. */
9607 if (!align_branch_power
9608 || now_seg == absolute_section
9609 || !cpu_arch_flags.bitfield.cpui386
9610 || i.tm.opcode_space != SPACE_BASE)
9611 return 0;
9612
9613 add_padding = 0;
9614
9615 /* Check for jcc and direct jmp. */
9616 if (i.tm.opcode_modifier.jump == JUMP)
9617 {
9618 if (i.tm.base_opcode == JUMP_PC_RELATIVE)
9619 {
9620 *branch_p = align_branch_jmp;
9621 add_padding = align_branch & align_branch_jmp_bit;
9622 }
9623 else
9624 {
9625 /* Because J<cc> and JN<cc> share same group in macro-fusible table,
9626 igore the lowest bit. */
9627 *mf_jcc_p = (i.tm.base_opcode & 0x0e) >> 1;
9628 *branch_p = align_branch_jcc;
9629 if ((align_branch & align_branch_jcc_bit))
9630 add_padding = 1;
9631 }
9632 }
9633 else if ((i.tm.base_opcode | 1) == 0xc3)
9634 {
9635 /* Near ret. */
9636 *branch_p = align_branch_ret;
9637 if ((align_branch & align_branch_ret_bit))
9638 add_padding = 1;
9639 }
9640 else
9641 {
9642 /* Check for indirect jmp, direct and indirect calls. */
9643 if (i.tm.base_opcode == 0xe8)
9644 {
9645 /* Direct call. */
9646 *branch_p = align_branch_call;
9647 if ((align_branch & align_branch_call_bit))
9648 add_padding = 1;
9649 }
9650 else if (i.tm.base_opcode == 0xff
9651 && (i.tm.extension_opcode == 2
9652 || i.tm.extension_opcode == 4))
9653 {
9654 /* Indirect call and jmp. */
9655 *branch_p = align_branch_indirect;
9656 if ((align_branch & align_branch_indirect_bit))
9657 add_padding = 1;
9658 }
9659
9660 if (add_padding
9661 && i.disp_operands
9662 && tls_get_addr
9663 && (i.op[0].disps->X_op == O_symbol
9664 || (i.op[0].disps->X_op == O_subtract
9665 && i.op[0].disps->X_op_symbol == GOT_symbol)))
9666 {
9667 symbolS *s = i.op[0].disps->X_add_symbol;
9668 /* No padding to call to global or undefined tls_get_addr. */
9669 if ((S_IS_EXTERNAL (s) || !S_IS_DEFINED (s))
9670 && strcmp (S_GET_NAME (s), tls_get_addr) == 0)
9671 return 0;
9672 }
9673 }
9674
9675 if (add_padding
9676 && last_insn.kind != last_insn_other
9677 && last_insn.seg == now_seg)
9678 {
9679 if (flag_debug)
9680 as_warn_where (last_insn.file, last_insn.line,
9681 _("`%s` skips -malign-branch-boundary on `%s`"),
9682 last_insn.name, insn_name (&i.tm));
9683 return 0;
9684 }
9685
9686 return add_padding;
9687 }
9688
9689 static void
9690 output_insn (void)
9691 {
9692 fragS *insn_start_frag;
9693 offsetT insn_start_off;
9694 fragS *fragP = NULL;
9695 enum align_branch_kind branch = align_branch_none;
9696 /* The initializer is arbitrary just to avoid uninitialized error.
9697 it's actually either assigned in add_branch_padding_frag_p
9698 or never be used. */
9699 enum mf_jcc_kind mf_jcc = mf_jcc_jo;
9700
9701 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9702 if (IS_ELF && x86_used_note && now_seg != absolute_section)
9703 {
9704 if ((i.xstate & xstate_tmm) == xstate_tmm
9705 || is_cpu (&i.tm, CpuAMX_TILE))
9706 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_TMM;
9707
9708 if (is_cpu (&i.tm, Cpu8087)
9709 || is_cpu (&i.tm, Cpu287)
9710 || is_cpu (&i.tm, Cpu387)
9711 || is_cpu (&i.tm, Cpu687)
9712 || is_cpu (&i.tm, CpuFISTTP))
9713 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X87;
9714
9715 if ((i.xstate & xstate_mmx)
9716 || i.tm.mnem_off == MN_emms
9717 || i.tm.mnem_off == MN_femms)
9718 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MMX;
9719
9720 if (i.index_reg)
9721 {
9722 if (i.index_reg->reg_type.bitfield.zmmword)
9723 i.xstate |= xstate_zmm;
9724 else if (i.index_reg->reg_type.bitfield.ymmword)
9725 i.xstate |= xstate_ymm;
9726 else if (i.index_reg->reg_type.bitfield.xmmword)
9727 i.xstate |= xstate_xmm;
9728 }
9729
9730 /* vzeroall / vzeroupper */
9731 if (i.tm.base_opcode == 0x77 && is_cpu (&i.tm, CpuAVX))
9732 i.xstate |= xstate_ymm;
9733
9734 if ((i.xstate & xstate_xmm)
9735 /* ldmxcsr / stmxcsr / vldmxcsr / vstmxcsr */
9736 || (i.tm.base_opcode == 0xae
9737 && (is_cpu (&i.tm, CpuSSE)
9738 || is_cpu (&i.tm, CpuAVX)))
9739 || is_cpu (&i.tm, CpuWideKL)
9740 || is_cpu (&i.tm, CpuKL))
9741 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XMM;
9742
9743 if ((i.xstate & xstate_ymm) == xstate_ymm)
9744 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_YMM;
9745 if ((i.xstate & xstate_zmm) == xstate_zmm)
9746 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_ZMM;
9747 if (i.mask.reg || (i.xstate & xstate_mask) == xstate_mask)
9748 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MASK;
9749 if (is_cpu (&i.tm, CpuFXSR))
9750 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_FXSR;
9751 if (is_cpu (&i.tm, CpuXsave))
9752 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVE;
9753 if (is_cpu (&i.tm, CpuXsaveopt))
9754 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT;
9755 if (is_cpu (&i.tm, CpuXSAVEC))
9756 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEC;
9757
9758 if (x86_feature_2_used
9759 || is_cpu (&i.tm, CpuCMOV)
9760 || is_cpu (&i.tm, CpuSYSCALL)
9761 || i.tm.mnem_off == MN_cmpxchg8b)
9762 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_BASELINE;
9763 if (is_cpu (&i.tm, CpuSSE3)
9764 || is_cpu (&i.tm, CpuSSSE3)
9765 || is_cpu (&i.tm, CpuSSE4_1)
9766 || is_cpu (&i.tm, CpuSSE4_2)
9767 || is_cpu (&i.tm, CpuCX16)
9768 || is_cpu (&i.tm, CpuPOPCNT)
9769 /* LAHF-SAHF insns in 64-bit mode. */
9770 || (flag_code == CODE_64BIT
9771 && (i.tm.base_opcode | 1) == 0x9f
9772 && i.tm.opcode_space == SPACE_BASE))
9773 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V2;
9774 if (is_cpu (&i.tm, CpuAVX)
9775 || is_cpu (&i.tm, CpuAVX2)
9776 /* Any VEX encoded insns execpt for AVX512F, AVX512BW, AVX512DQ,
9777 XOP, FMA4, LPW, TBM, and AMX. */
9778 || (i.tm.opcode_modifier.vex
9779 && !is_cpu (&i.tm, CpuAVX512F)
9780 && !is_cpu (&i.tm, CpuAVX512BW)
9781 && !is_cpu (&i.tm, CpuAVX512DQ)
9782 && !is_cpu (&i.tm, CpuXOP)
9783 && !is_cpu (&i.tm, CpuFMA4)
9784 && !is_cpu (&i.tm, CpuLWP)
9785 && !is_cpu (&i.tm, CpuTBM)
9786 && !(x86_feature_2_used & GNU_PROPERTY_X86_FEATURE_2_TMM))
9787 || is_cpu (&i.tm, CpuF16C)
9788 || is_cpu (&i.tm, CpuFMA)
9789 || is_cpu (&i.tm, CpuLZCNT)
9790 || is_cpu (&i.tm, CpuMovbe)
9791 || is_cpu (&i.tm, CpuXSAVES)
9792 || (x86_feature_2_used
9793 & (GNU_PROPERTY_X86_FEATURE_2_XSAVE
9794 | GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT
9795 | GNU_PROPERTY_X86_FEATURE_2_XSAVEC)) != 0)
9796 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V3;
9797 if (is_cpu (&i.tm, CpuAVX512F)
9798 || is_cpu (&i.tm, CpuAVX512BW)
9799 || is_cpu (&i.tm, CpuAVX512DQ)
9800 || is_cpu (&i.tm, CpuAVX512VL)
9801 /* Any EVEX encoded insns except for AVX512ER, AVX512PF,
9802 AVX512-4FMAPS, and AVX512-4VNNIW. */
9803 || (i.tm.opcode_modifier.evex
9804 && !is_cpu (&i.tm, CpuAVX512ER)
9805 && !is_cpu (&i.tm, CpuAVX512PF)
9806 && !is_cpu (&i.tm, CpuAVX512_4FMAPS)
9807 && !is_cpu (&i.tm, CpuAVX512_4VNNIW)))
9808 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V4;
9809 }
9810 #endif
9811
9812 /* Tie dwarf2 debug info to the address at the start of the insn.
9813 We can't do this after the insn has been output as the current
9814 frag may have been closed off. eg. by frag_var. */
9815 dwarf2_emit_insn (0);
9816
9817 insn_start_frag = frag_now;
9818 insn_start_off = frag_now_fix ();
9819
9820 if (add_branch_padding_frag_p (&branch, &mf_jcc))
9821 {
9822 char *p;
9823 /* Branch can be 8 bytes. Leave some room for prefixes. */
9824 unsigned int max_branch_padding_size = 14;
9825
9826 /* Align section to boundary. */
9827 record_alignment (now_seg, align_branch_power);
9828
9829 /* Make room for padding. */
9830 frag_grow (max_branch_padding_size);
9831
9832 /* Start of the padding. */
9833 p = frag_more (0);
9834
9835 fragP = frag_now;
9836
9837 frag_var (rs_machine_dependent, max_branch_padding_size, 0,
9838 ENCODE_RELAX_STATE (BRANCH_PADDING, 0),
9839 NULL, 0, p);
9840
9841 fragP->tc_frag_data.mf_type = mf_jcc;
9842 fragP->tc_frag_data.branch_type = branch;
9843 fragP->tc_frag_data.max_bytes = max_branch_padding_size;
9844 }
9845
9846 if (!cpu_arch_flags.bitfield.cpui386 && (flag_code != CODE_16BIT)
9847 && !pre_386_16bit_warned)
9848 {
9849 as_warn (_("use .code16 to ensure correct addressing mode"));
9850 pre_386_16bit_warned = true;
9851 }
9852
9853 /* Output jumps. */
9854 if (i.tm.opcode_modifier.jump == JUMP)
9855 output_branch ();
9856 else if (i.tm.opcode_modifier.jump == JUMP_BYTE
9857 || i.tm.opcode_modifier.jump == JUMP_DWORD)
9858 output_jump ();
9859 else if (i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT)
9860 output_interseg_jump ();
9861 else
9862 {
9863 /* Output normal instructions here. */
9864 char *p;
9865 unsigned char *q;
9866 unsigned int j;
9867 enum mf_cmp_kind mf_cmp;
9868
9869 if (avoid_fence
9870 && (i.tm.base_opcode == 0xaee8
9871 || i.tm.base_opcode == 0xaef0
9872 || i.tm.base_opcode == 0xaef8))
9873 {
9874 /* Encode lfence, mfence, and sfence as
9875 f0 83 04 24 00 lock addl $0x0, (%{re}sp). */
9876 if (flag_code == CODE_16BIT)
9877 as_bad (_("Cannot convert `%s' in 16-bit mode"), insn_name (&i.tm));
9878 else if (omit_lock_prefix)
9879 as_bad (_("Cannot convert `%s' with `-momit-lock-prefix=yes' in effect"),
9880 insn_name (&i.tm));
9881 else if (now_seg != absolute_section)
9882 {
9883 offsetT val = 0x240483f0ULL;
9884
9885 p = frag_more (5);
9886 md_number_to_chars (p, val, 5);
9887 }
9888 else
9889 abs_section_offset += 5;
9890 return;
9891 }
9892
9893 /* Some processors fail on LOCK prefix. This options makes
9894 assembler ignore LOCK prefix and serves as a workaround. */
9895 if (omit_lock_prefix)
9896 {
9897 if (i.tm.base_opcode == LOCK_PREFIX_OPCODE
9898 && i.tm.opcode_modifier.isprefix)
9899 return;
9900 i.prefix[LOCK_PREFIX] = 0;
9901 }
9902
9903 if (branch)
9904 /* Skip if this is a branch. */
9905 ;
9906 else if (add_fused_jcc_padding_frag_p (&mf_cmp))
9907 {
9908 /* Make room for padding. */
9909 frag_grow (MAX_FUSED_JCC_PADDING_SIZE);
9910 p = frag_more (0);
9911
9912 fragP = frag_now;
9913
9914 frag_var (rs_machine_dependent, MAX_FUSED_JCC_PADDING_SIZE, 0,
9915 ENCODE_RELAX_STATE (FUSED_JCC_PADDING, 0),
9916 NULL, 0, p);
9917
9918 fragP->tc_frag_data.mf_type = mf_cmp;
9919 fragP->tc_frag_data.branch_type = align_branch_fused;
9920 fragP->tc_frag_data.max_bytes = MAX_FUSED_JCC_PADDING_SIZE;
9921 }
9922 else if (add_branch_prefix_frag_p ())
9923 {
9924 unsigned int max_prefix_size = align_branch_prefix_size;
9925
9926 /* Make room for padding. */
9927 frag_grow (max_prefix_size);
9928 p = frag_more (0);
9929
9930 fragP = frag_now;
9931
9932 frag_var (rs_machine_dependent, max_prefix_size, 0,
9933 ENCODE_RELAX_STATE (BRANCH_PREFIX, 0),
9934 NULL, 0, p);
9935
9936 fragP->tc_frag_data.max_bytes = max_prefix_size;
9937 }
9938
9939 /* Since the VEX/EVEX prefix contains the implicit prefix, we
9940 don't need the explicit prefix. */
9941 if (!is_any_vex_encoding (&i.tm))
9942 {
9943 switch (i.tm.opcode_modifier.opcodeprefix)
9944 {
9945 case PREFIX_0X66:
9946 add_prefix (0x66);
9947 break;
9948 case PREFIX_0XF2:
9949 add_prefix (0xf2);
9950 break;
9951 case PREFIX_0XF3:
9952 if (!is_cpu (&i.tm, CpuPadLock)
9953 || (i.prefix[REP_PREFIX] != 0xf3))
9954 add_prefix (0xf3);
9955 break;
9956 case PREFIX_NONE:
9957 switch (i.opcode_length)
9958 {
9959 case 2:
9960 break;
9961 case 1:
9962 /* Check for pseudo prefixes. */
9963 if (!i.tm.opcode_modifier.isprefix || i.tm.base_opcode)
9964 break;
9965 as_bad_where (insn_start_frag->fr_file,
9966 insn_start_frag->fr_line,
9967 _("pseudo prefix without instruction"));
9968 return;
9969 default:
9970 abort ();
9971 }
9972 break;
9973 default:
9974 abort ();
9975 }
9976
9977 #if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
9978 /* For x32, add a dummy REX_OPCODE prefix for mov/add with
9979 R_X86_64_GOTTPOFF relocation so that linker can safely
9980 perform IE->LE optimization. A dummy REX_OPCODE prefix
9981 is also needed for lea with R_X86_64_GOTPC32_TLSDESC
9982 relocation for GDesc -> IE/LE optimization. */
9983 if (x86_elf_abi == X86_64_X32_ABI
9984 && i.operands == 2
9985 && (i.reloc[0] == BFD_RELOC_X86_64_GOTTPOFF
9986 || i.reloc[0] == BFD_RELOC_X86_64_GOTPC32_TLSDESC)
9987 && i.prefix[REX_PREFIX] == 0)
9988 add_prefix (REX_OPCODE);
9989 #endif
9990
9991 /* The prefix bytes. */
9992 for (j = ARRAY_SIZE (i.prefix), q = i.prefix; j > 0; j--, q++)
9993 if (*q)
9994 frag_opcode_byte (*q);
9995 }
9996 else
9997 {
9998 for (j = 0, q = i.prefix; j < ARRAY_SIZE (i.prefix); j++, q++)
9999 if (*q)
10000 switch (j)
10001 {
10002 case SEG_PREFIX:
10003 case ADDR_PREFIX:
10004 frag_opcode_byte (*q);
10005 break;
10006 default:
10007 /* There should be no other prefixes for instructions
10008 with VEX prefix. */
10009 abort ();
10010 }
10011
10012 /* For EVEX instructions i.vrex should become 0 after
10013 build_evex_prefix. For VEX instructions upper 16 registers
10014 aren't available, so VREX should be 0. */
10015 if (i.vrex)
10016 abort ();
10017 /* Now the VEX prefix. */
10018 if (now_seg != absolute_section)
10019 {
10020 p = frag_more (i.vex.length);
10021 for (j = 0; j < i.vex.length; j++)
10022 p[j] = i.vex.bytes[j];
10023 }
10024 else
10025 abs_section_offset += i.vex.length;
10026 }
10027
10028 /* Now the opcode; be careful about word order here! */
10029 j = i.opcode_length;
10030 if (!i.vex.length)
10031 switch (i.tm.opcode_space)
10032 {
10033 case SPACE_BASE:
10034 break;
10035 case SPACE_0F:
10036 ++j;
10037 break;
10038 case SPACE_0F38:
10039 case SPACE_0F3A:
10040 j += 2;
10041 break;
10042 default:
10043 abort ();
10044 }
10045
10046 if (now_seg == absolute_section)
10047 abs_section_offset += j;
10048 else if (j == 1)
10049 {
10050 FRAG_APPEND_1_CHAR (i.tm.base_opcode);
10051 }
10052 else
10053 {
10054 p = frag_more (j);
10055 if (!i.vex.length
10056 && i.tm.opcode_space != SPACE_BASE)
10057 {
10058 *p++ = 0x0f;
10059 if (i.tm.opcode_space != SPACE_0F)
10060 *p++ = i.tm.opcode_space == SPACE_0F38
10061 ? 0x38 : 0x3a;
10062 }
10063
10064 switch (i.opcode_length)
10065 {
10066 case 2:
10067 /* Put out high byte first: can't use md_number_to_chars! */
10068 *p++ = (i.tm.base_opcode >> 8) & 0xff;
10069 /* Fall through. */
10070 case 1:
10071 *p = i.tm.base_opcode & 0xff;
10072 break;
10073 default:
10074 abort ();
10075 break;
10076 }
10077
10078 }
10079
10080 /* Now the modrm byte and sib byte (if present). */
10081 if (i.tm.opcode_modifier.modrm)
10082 {
10083 frag_opcode_byte ((i.rm.regmem << 0)
10084 | (i.rm.reg << 3)
10085 | (i.rm.mode << 6));
10086 /* If i.rm.regmem == ESP (4)
10087 && i.rm.mode != (Register mode)
10088 && not 16 bit
10089 ==> need second modrm byte. */
10090 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
10091 && i.rm.mode != 3
10092 && !(i.base_reg && i.base_reg->reg_type.bitfield.word))
10093 frag_opcode_byte ((i.sib.base << 0)
10094 | (i.sib.index << 3)
10095 | (i.sib.scale << 6));
10096 }
10097
10098 if (i.disp_operands)
10099 output_disp (insn_start_frag, insn_start_off);
10100
10101 if (i.imm_operands)
10102 output_imm (insn_start_frag, insn_start_off);
10103
10104 /*
10105 * frag_now_fix () returning plain abs_section_offset when we're in the
10106 * absolute section, and abs_section_offset not getting updated as data
10107 * gets added to the frag breaks the logic below.
10108 */
10109 if (now_seg != absolute_section)
10110 {
10111 j = encoding_length (insn_start_frag, insn_start_off, frag_more (0));
10112 if (j > 15)
10113 as_warn (_("instruction length of %u bytes exceeds the limit of 15"),
10114 j);
10115 else if (fragP)
10116 {
10117 /* NB: Don't add prefix with GOTPC relocation since
10118 output_disp() above depends on the fixed encoding
10119 length. Can't add prefix with TLS relocation since
10120 it breaks TLS linker optimization. */
10121 unsigned int max = i.has_gotpc_tls_reloc ? 0 : 15 - j;
10122 /* Prefix count on the current instruction. */
10123 unsigned int count = i.vex.length;
10124 unsigned int k;
10125 for (k = 0; k < ARRAY_SIZE (i.prefix); k++)
10126 /* REX byte is encoded in VEX/EVEX prefix. */
10127 if (i.prefix[k] && (k != REX_PREFIX || !i.vex.length))
10128 count++;
10129
10130 /* Count prefixes for extended opcode maps. */
10131 if (!i.vex.length)
10132 switch (i.tm.opcode_space)
10133 {
10134 case SPACE_BASE:
10135 break;
10136 case SPACE_0F:
10137 count++;
10138 break;
10139 case SPACE_0F38:
10140 case SPACE_0F3A:
10141 count += 2;
10142 break;
10143 default:
10144 abort ();
10145 }
10146
10147 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
10148 == BRANCH_PREFIX)
10149 {
10150 /* Set the maximum prefix size in BRANCH_PREFIX
10151 frag. */
10152 if (fragP->tc_frag_data.max_bytes > max)
10153 fragP->tc_frag_data.max_bytes = max;
10154 if (fragP->tc_frag_data.max_bytes > count)
10155 fragP->tc_frag_data.max_bytes -= count;
10156 else
10157 fragP->tc_frag_data.max_bytes = 0;
10158 }
10159 else
10160 {
10161 /* Remember the maximum prefix size in FUSED_JCC_PADDING
10162 frag. */
10163 unsigned int max_prefix_size;
10164 if (align_branch_prefix_size > max)
10165 max_prefix_size = max;
10166 else
10167 max_prefix_size = align_branch_prefix_size;
10168 if (max_prefix_size > count)
10169 fragP->tc_frag_data.max_prefix_length
10170 = max_prefix_size - count;
10171 }
10172
10173 /* Use existing segment prefix if possible. Use CS
10174 segment prefix in 64-bit mode. In 32-bit mode, use SS
10175 segment prefix with ESP/EBP base register and use DS
10176 segment prefix without ESP/EBP base register. */
10177 if (i.prefix[SEG_PREFIX])
10178 fragP->tc_frag_data.default_prefix = i.prefix[SEG_PREFIX];
10179 else if (flag_code == CODE_64BIT)
10180 fragP->tc_frag_data.default_prefix = CS_PREFIX_OPCODE;
10181 else if (i.base_reg
10182 && (i.base_reg->reg_num == 4
10183 || i.base_reg->reg_num == 5))
10184 fragP->tc_frag_data.default_prefix = SS_PREFIX_OPCODE;
10185 else
10186 fragP->tc_frag_data.default_prefix = DS_PREFIX_OPCODE;
10187 }
10188 }
10189 }
10190
10191 /* NB: Don't work with COND_JUMP86 without i386. */
10192 if (align_branch_power
10193 && now_seg != absolute_section
10194 && cpu_arch_flags.bitfield.cpui386)
10195 {
10196 /* Terminate each frag so that we can add prefix and check for
10197 fused jcc. */
10198 frag_wane (frag_now);
10199 frag_new (0);
10200 }
10201
10202 #ifdef DEBUG386
10203 if (flag_debug)
10204 {
10205 pi ("" /*line*/, &i);
10206 }
10207 #endif /* DEBUG386 */
10208 }
10209
10210 /* Return the size of the displacement operand N. */
10211
10212 static int
10213 disp_size (unsigned int n)
10214 {
10215 int size = 4;
10216
10217 if (i.types[n].bitfield.disp64)
10218 size = 8;
10219 else if (i.types[n].bitfield.disp8)
10220 size = 1;
10221 else if (i.types[n].bitfield.disp16)
10222 size = 2;
10223 return size;
10224 }
10225
10226 /* Return the size of the immediate operand N. */
10227
10228 static int
10229 imm_size (unsigned int n)
10230 {
10231 int size = 4;
10232 if (i.types[n].bitfield.imm64)
10233 size = 8;
10234 else if (i.types[n].bitfield.imm8 || i.types[n].bitfield.imm8s)
10235 size = 1;
10236 else if (i.types[n].bitfield.imm16)
10237 size = 2;
10238 return size;
10239 }
10240
10241 static void
10242 output_disp (fragS *insn_start_frag, offsetT insn_start_off)
10243 {
10244 char *p;
10245 unsigned int n;
10246
10247 for (n = 0; n < i.operands; n++)
10248 {
10249 if (operand_type_check (i.types[n], disp))
10250 {
10251 int size = disp_size (n);
10252
10253 if (now_seg == absolute_section)
10254 abs_section_offset += size;
10255 else if (i.op[n].disps->X_op == O_constant)
10256 {
10257 offsetT val = i.op[n].disps->X_add_number;
10258
10259 val = offset_in_range (val >> (size == 1 ? i.memshift : 0),
10260 size);
10261 p = frag_more (size);
10262 md_number_to_chars (p, val, size);
10263 }
10264 else
10265 {
10266 enum bfd_reloc_code_real reloc_type;
10267 bool pcrel = (i.flags[n] & Operand_PCrel) != 0;
10268 bool sign = (flag_code == CODE_64BIT && size == 4
10269 && (!want_disp32 (&i.tm)
10270 || (i.tm.opcode_modifier.jump && !i.jumpabsolute
10271 && !i.types[n].bitfield.baseindex)))
10272 || pcrel;
10273 fixS *fixP;
10274
10275 /* We can't have 8 bit displacement here. */
10276 gas_assert (!i.types[n].bitfield.disp8);
10277
10278 /* The PC relative address is computed relative
10279 to the instruction boundary, so in case immediate
10280 fields follows, we need to adjust the value. */
10281 if (pcrel && i.imm_operands)
10282 {
10283 unsigned int n1;
10284 int sz = 0;
10285
10286 for (n1 = 0; n1 < i.operands; n1++)
10287 if (operand_type_check (i.types[n1], imm))
10288 {
10289 /* Only one immediate is allowed for PC
10290 relative address, except with .insn. */
10291 gas_assert (sz == 0 || dot_insn ());
10292 sz += imm_size (n1);
10293 }
10294 /* We should find at least one immediate. */
10295 gas_assert (sz != 0);
10296 i.op[n].disps->X_add_number -= sz;
10297 }
10298
10299 p = frag_more (size);
10300 reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
10301 if (GOT_symbol
10302 && GOT_symbol == i.op[n].disps->X_add_symbol
10303 && (((reloc_type == BFD_RELOC_32
10304 || reloc_type == BFD_RELOC_X86_64_32S
10305 || (reloc_type == BFD_RELOC_64
10306 && object_64bit))
10307 && (i.op[n].disps->X_op == O_symbol
10308 || (i.op[n].disps->X_op == O_add
10309 && ((symbol_get_value_expression
10310 (i.op[n].disps->X_op_symbol)->X_op)
10311 == O_subtract))))
10312 || reloc_type == BFD_RELOC_32_PCREL))
10313 {
10314 if (!object_64bit)
10315 {
10316 reloc_type = BFD_RELOC_386_GOTPC;
10317 i.has_gotpc_tls_reloc = true;
10318 i.op[n].disps->X_add_number +=
10319 encoding_length (insn_start_frag, insn_start_off, p);
10320 }
10321 else if (reloc_type == BFD_RELOC_64)
10322 reloc_type = BFD_RELOC_X86_64_GOTPC64;
10323 else
10324 /* Don't do the adjustment for x86-64, as there
10325 the pcrel addressing is relative to the _next_
10326 insn, and that is taken care of in other code. */
10327 reloc_type = BFD_RELOC_X86_64_GOTPC32;
10328 }
10329 else if (align_branch_power)
10330 {
10331 switch (reloc_type)
10332 {
10333 case BFD_RELOC_386_TLS_GD:
10334 case BFD_RELOC_386_TLS_LDM:
10335 case BFD_RELOC_386_TLS_IE:
10336 case BFD_RELOC_386_TLS_IE_32:
10337 case BFD_RELOC_386_TLS_GOTIE:
10338 case BFD_RELOC_386_TLS_GOTDESC:
10339 case BFD_RELOC_386_TLS_DESC_CALL:
10340 case BFD_RELOC_X86_64_TLSGD:
10341 case BFD_RELOC_X86_64_TLSLD:
10342 case BFD_RELOC_X86_64_GOTTPOFF:
10343 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
10344 case BFD_RELOC_X86_64_TLSDESC_CALL:
10345 i.has_gotpc_tls_reloc = true;
10346 default:
10347 break;
10348 }
10349 }
10350 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal,
10351 size, i.op[n].disps, pcrel,
10352 reloc_type);
10353
10354 if (flag_code == CODE_64BIT && size == 4 && pcrel
10355 && !i.prefix[ADDR_PREFIX])
10356 fixP->fx_signed = 1;
10357
10358 /* Check for "call/jmp *mem", "mov mem, %reg",
10359 "test %reg, mem" and "binop mem, %reg" where binop
10360 is one of adc, add, and, cmp, or, sbb, sub, xor
10361 instructions without data prefix. Always generate
10362 R_386_GOT32X for "sym*GOT" operand in 32-bit mode. */
10363 if (i.prefix[DATA_PREFIX] == 0
10364 && (generate_relax_relocations
10365 || (!object_64bit
10366 && i.rm.mode == 0
10367 && i.rm.regmem == 5))
10368 && (i.rm.mode == 2
10369 || (i.rm.mode == 0 && i.rm.regmem == 5))
10370 && i.tm.opcode_space == SPACE_BASE
10371 && ((i.operands == 1
10372 && i.tm.base_opcode == 0xff
10373 && (i.rm.reg == 2 || i.rm.reg == 4))
10374 || (i.operands == 2
10375 && (i.tm.base_opcode == 0x8b
10376 || i.tm.base_opcode == 0x85
10377 || (i.tm.base_opcode & ~0x38) == 0x03))))
10378 {
10379 if (object_64bit)
10380 {
10381 fixP->fx_tcbit = i.rex != 0;
10382 if (i.base_reg
10383 && (i.base_reg->reg_num == RegIP))
10384 fixP->fx_tcbit2 = 1;
10385 }
10386 else
10387 fixP->fx_tcbit2 = 1;
10388 }
10389 }
10390 }
10391 }
10392 }
10393
10394 static void
10395 output_imm (fragS *insn_start_frag, offsetT insn_start_off)
10396 {
10397 char *p;
10398 unsigned int n;
10399
10400 for (n = 0; n < i.operands; n++)
10401 {
10402 if (operand_type_check (i.types[n], imm))
10403 {
10404 int size = imm_size (n);
10405
10406 if (now_seg == absolute_section)
10407 abs_section_offset += size;
10408 else if (i.op[n].imms->X_op == O_constant)
10409 {
10410 offsetT val;
10411
10412 val = offset_in_range (i.op[n].imms->X_add_number,
10413 size);
10414 p = frag_more (size);
10415 md_number_to_chars (p, val, size);
10416 }
10417 else
10418 {
10419 /* Not absolute_section.
10420 Need a 32-bit fixup (don't support 8bit
10421 non-absolute imms). Try to support other
10422 sizes ... */
10423 enum bfd_reloc_code_real reloc_type;
10424 int sign;
10425
10426 if (i.types[n].bitfield.imm32s
10427 && (i.suffix == QWORD_MNEM_SUFFIX
10428 || (!i.suffix && i.tm.opcode_modifier.no_lsuf)
10429 || dot_insn ()))
10430 sign = 1;
10431 else
10432 sign = 0;
10433
10434 p = frag_more (size);
10435 reloc_type = reloc (size, 0, sign, i.reloc[n]);
10436
10437 /* This is tough to explain. We end up with this one if we
10438 * have operands that look like
10439 * "_GLOBAL_OFFSET_TABLE_+[.-.L284]". The goal here is to
10440 * obtain the absolute address of the GOT, and it is strongly
10441 * preferable from a performance point of view to avoid using
10442 * a runtime relocation for this. The actual sequence of
10443 * instructions often look something like:
10444 *
10445 * call .L66
10446 * .L66:
10447 * popl %ebx
10448 * addl $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
10449 *
10450 * The call and pop essentially return the absolute address
10451 * of the label .L66 and store it in %ebx. The linker itself
10452 * will ultimately change the first operand of the addl so
10453 * that %ebx points to the GOT, but to keep things simple, the
10454 * .o file must have this operand set so that it generates not
10455 * the absolute address of .L66, but the absolute address of
10456 * itself. This allows the linker itself simply treat a GOTPC
10457 * relocation as asking for a pcrel offset to the GOT to be
10458 * added in, and the addend of the relocation is stored in the
10459 * operand field for the instruction itself.
10460 *
10461 * Our job here is to fix the operand so that it would add
10462 * the correct offset so that %ebx would point to itself. The
10463 * thing that is tricky is that .-.L66 will point to the
10464 * beginning of the instruction, so we need to further modify
10465 * the operand so that it will point to itself. There are
10466 * other cases where you have something like:
10467 *
10468 * .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
10469 *
10470 * and here no correction would be required. Internally in
10471 * the assembler we treat operands of this form as not being
10472 * pcrel since the '.' is explicitly mentioned, and I wonder
10473 * whether it would simplify matters to do it this way. Who
10474 * knows. In earlier versions of the PIC patches, the
10475 * pcrel_adjust field was used to store the correction, but
10476 * since the expression is not pcrel, I felt it would be
10477 * confusing to do it this way. */
10478
10479 if ((reloc_type == BFD_RELOC_32
10480 || reloc_type == BFD_RELOC_X86_64_32S
10481 || reloc_type == BFD_RELOC_64)
10482 && GOT_symbol
10483 && GOT_symbol == i.op[n].imms->X_add_symbol
10484 && (i.op[n].imms->X_op == O_symbol
10485 || (i.op[n].imms->X_op == O_add
10486 && ((symbol_get_value_expression
10487 (i.op[n].imms->X_op_symbol)->X_op)
10488 == O_subtract))))
10489 {
10490 if (!object_64bit)
10491 reloc_type = BFD_RELOC_386_GOTPC;
10492 else if (size == 4)
10493 reloc_type = BFD_RELOC_X86_64_GOTPC32;
10494 else if (size == 8)
10495 reloc_type = BFD_RELOC_X86_64_GOTPC64;
10496 i.has_gotpc_tls_reloc = true;
10497 i.op[n].imms->X_add_number +=
10498 encoding_length (insn_start_frag, insn_start_off, p);
10499 }
10500 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
10501 i.op[n].imms, 0, reloc_type);
10502 }
10503 }
10504 }
10505 }
10506 \f
10507 /* x86_cons_fix_new is called via the expression parsing code when a
10508 reloc is needed. We use this hook to get the correct .got reloc. */
10509 static int cons_sign = -1;
10510
10511 void
10512 x86_cons_fix_new (fragS *frag, unsigned int off, unsigned int len,
10513 expressionS *exp, bfd_reloc_code_real_type r)
10514 {
10515 r = reloc (len, 0, cons_sign, r);
10516
10517 #ifdef TE_PE
10518 if (exp->X_op == O_secrel)
10519 {
10520 exp->X_op = O_symbol;
10521 r = BFD_RELOC_32_SECREL;
10522 }
10523 else if (exp->X_op == O_secidx)
10524 r = BFD_RELOC_16_SECIDX;
10525 #endif
10526
10527 fix_new_exp (frag, off, len, exp, 0, r);
10528 }
10529
10530 /* Export the ABI address size for use by TC_ADDRESS_BYTES for the
10531 purpose of the `.dc.a' internal pseudo-op. */
10532
10533 int
10534 x86_address_bytes (void)
10535 {
10536 if ((stdoutput->arch_info->mach & bfd_mach_x64_32))
10537 return 4;
10538 return stdoutput->arch_info->bits_per_address / 8;
10539 }
10540
10541 #if (!(defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined (OBJ_MACH_O)) \
10542 || defined (LEX_AT)) && !defined (TE_PE)
10543 # define lex_got(reloc, adjust, types) NULL
10544 #else
10545 /* Parse operands of the form
10546 <symbol>@GOTOFF+<nnn>
10547 and similar .plt or .got references.
10548
10549 If we find one, set up the correct relocation in RELOC and copy the
10550 input string, minus the `@GOTOFF' into a malloc'd buffer for
10551 parsing by the calling routine. Return this buffer, and if ADJUST
10552 is non-null set it to the length of the string we removed from the
10553 input line. Otherwise return NULL. */
10554 static char *
10555 lex_got (enum bfd_reloc_code_real *rel,
10556 int *adjust,
10557 i386_operand_type *types)
10558 {
10559 /* Some of the relocations depend on the size of what field is to
10560 be relocated. But in our callers i386_immediate and i386_displacement
10561 we don't yet know the operand size (this will be set by insn
10562 matching). Hence we record the word32 relocation here,
10563 and adjust the reloc according to the real size in reloc(). */
10564 static const struct
10565 {
10566 const char *str;
10567 int len;
10568 const enum bfd_reloc_code_real rel[2];
10569 const i386_operand_type types64;
10570 bool need_GOT_symbol;
10571 }
10572 gotrel[] =
10573 {
10574
10575 #define OPERAND_TYPE_IMM32_32S_DISP32 { .bitfield = \
10576 { .imm32 = 1, .imm32s = 1, .disp32 = 1 } }
10577 #define OPERAND_TYPE_IMM32_32S_64_DISP32 { .bitfield = \
10578 { .imm32 = 1, .imm32s = 1, .imm64 = 1, .disp32 = 1 } }
10579 #define OPERAND_TYPE_IMM32_32S_64_DISP32_64 { .bitfield = \
10580 { .imm32 = 1, .imm32s = 1, .imm64 = 1, .disp32 = 1, .disp64 = 1 } }
10581 #define OPERAND_TYPE_IMM64_DISP64 { .bitfield = \
10582 { .imm64 = 1, .disp64 = 1 } }
10583
10584 #ifndef TE_PE
10585 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
10586 { STRING_COMMA_LEN ("SIZE"), { BFD_RELOC_SIZE32,
10587 BFD_RELOC_SIZE32 },
10588 { .bitfield = { .imm32 = 1, .imm64 = 1 } }, false },
10589 #endif
10590 { STRING_COMMA_LEN ("PLTOFF"), { _dummy_first_bfd_reloc_code_real,
10591 BFD_RELOC_X86_64_PLTOFF64 },
10592 { .bitfield = { .imm64 = 1 } }, true },
10593 { STRING_COMMA_LEN ("PLT"), { BFD_RELOC_386_PLT32,
10594 BFD_RELOC_X86_64_PLT32 },
10595 OPERAND_TYPE_IMM32_32S_DISP32, false },
10596 { STRING_COMMA_LEN ("GOTPLT"), { _dummy_first_bfd_reloc_code_real,
10597 BFD_RELOC_X86_64_GOTPLT64 },
10598 OPERAND_TYPE_IMM64_DISP64, true },
10599 { STRING_COMMA_LEN ("GOTOFF"), { BFD_RELOC_386_GOTOFF,
10600 BFD_RELOC_X86_64_GOTOFF64 },
10601 OPERAND_TYPE_IMM64_DISP64, true },
10602 { STRING_COMMA_LEN ("GOTPCREL"), { _dummy_first_bfd_reloc_code_real,
10603 BFD_RELOC_X86_64_GOTPCREL },
10604 OPERAND_TYPE_IMM32_32S_DISP32, true },
10605 { STRING_COMMA_LEN ("TLSGD"), { BFD_RELOC_386_TLS_GD,
10606 BFD_RELOC_X86_64_TLSGD },
10607 OPERAND_TYPE_IMM32_32S_DISP32, true },
10608 { STRING_COMMA_LEN ("TLSLDM"), { BFD_RELOC_386_TLS_LDM,
10609 _dummy_first_bfd_reloc_code_real },
10610 OPERAND_TYPE_NONE, true },
10611 { STRING_COMMA_LEN ("TLSLD"), { _dummy_first_bfd_reloc_code_real,
10612 BFD_RELOC_X86_64_TLSLD },
10613 OPERAND_TYPE_IMM32_32S_DISP32, true },
10614 { STRING_COMMA_LEN ("GOTTPOFF"), { BFD_RELOC_386_TLS_IE_32,
10615 BFD_RELOC_X86_64_GOTTPOFF },
10616 OPERAND_TYPE_IMM32_32S_DISP32, true },
10617 { STRING_COMMA_LEN ("TPOFF"), { BFD_RELOC_386_TLS_LE_32,
10618 BFD_RELOC_X86_64_TPOFF32 },
10619 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
10620 { STRING_COMMA_LEN ("NTPOFF"), { BFD_RELOC_386_TLS_LE,
10621 _dummy_first_bfd_reloc_code_real },
10622 OPERAND_TYPE_NONE, true },
10623 { STRING_COMMA_LEN ("DTPOFF"), { BFD_RELOC_386_TLS_LDO_32,
10624 BFD_RELOC_X86_64_DTPOFF32 },
10625 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
10626 { STRING_COMMA_LEN ("GOTNTPOFF"),{ BFD_RELOC_386_TLS_GOTIE,
10627 _dummy_first_bfd_reloc_code_real },
10628 OPERAND_TYPE_NONE, true },
10629 { STRING_COMMA_LEN ("INDNTPOFF"),{ BFD_RELOC_386_TLS_IE,
10630 _dummy_first_bfd_reloc_code_real },
10631 OPERAND_TYPE_NONE, true },
10632 { STRING_COMMA_LEN ("GOT"), { BFD_RELOC_386_GOT32,
10633 BFD_RELOC_X86_64_GOT32 },
10634 OPERAND_TYPE_IMM32_32S_64_DISP32, true },
10635 { STRING_COMMA_LEN ("TLSDESC"), { BFD_RELOC_386_TLS_GOTDESC,
10636 BFD_RELOC_X86_64_GOTPC32_TLSDESC },
10637 OPERAND_TYPE_IMM32_32S_DISP32, true },
10638 { STRING_COMMA_LEN ("TLSCALL"), { BFD_RELOC_386_TLS_DESC_CALL,
10639 BFD_RELOC_X86_64_TLSDESC_CALL },
10640 OPERAND_TYPE_IMM32_32S_DISP32, true },
10641 #else /* TE_PE */
10642 { STRING_COMMA_LEN ("SECREL32"), { BFD_RELOC_32_SECREL,
10643 BFD_RELOC_32_SECREL },
10644 OPERAND_TYPE_IMM32_32S_64_DISP32_64, false },
10645 #endif
10646
10647 #undef OPERAND_TYPE_IMM32_32S_DISP32
10648 #undef OPERAND_TYPE_IMM32_32S_64_DISP32
10649 #undef OPERAND_TYPE_IMM32_32S_64_DISP32_64
10650 #undef OPERAND_TYPE_IMM64_DISP64
10651
10652 };
10653 char *cp;
10654 unsigned int j;
10655
10656 #if defined (OBJ_MAYBE_ELF) && !defined (TE_PE)
10657 if (!IS_ELF)
10658 return NULL;
10659 #endif
10660
10661 for (cp = input_line_pointer; *cp != '@'; cp++)
10662 if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
10663 return NULL;
10664
10665 for (j = 0; j < ARRAY_SIZE (gotrel); j++)
10666 {
10667 int len = gotrel[j].len;
10668 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
10669 {
10670 if (gotrel[j].rel[object_64bit] != 0)
10671 {
10672 int first, second;
10673 char *tmpbuf, *past_reloc;
10674
10675 *rel = gotrel[j].rel[object_64bit];
10676
10677 if (types)
10678 {
10679 if (flag_code != CODE_64BIT)
10680 {
10681 types->bitfield.imm32 = 1;
10682 types->bitfield.disp32 = 1;
10683 }
10684 else
10685 *types = gotrel[j].types64;
10686 }
10687
10688 if (gotrel[j].need_GOT_symbol && GOT_symbol == NULL)
10689 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
10690
10691 /* The length of the first part of our input line. */
10692 first = cp - input_line_pointer;
10693
10694 /* The second part goes from after the reloc token until
10695 (and including) an end_of_line char or comma. */
10696 past_reloc = cp + 1 + len;
10697 cp = past_reloc;
10698 while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
10699 ++cp;
10700 second = cp + 1 - past_reloc;
10701
10702 /* Allocate and copy string. The trailing NUL shouldn't
10703 be necessary, but be safe. */
10704 tmpbuf = XNEWVEC (char, first + second + 2);
10705 memcpy (tmpbuf, input_line_pointer, first);
10706 if (second != 0 && *past_reloc != ' ')
10707 /* Replace the relocation token with ' ', so that
10708 errors like foo@GOTOFF1 will be detected. */
10709 tmpbuf[first++] = ' ';
10710 else
10711 /* Increment length by 1 if the relocation token is
10712 removed. */
10713 len++;
10714 if (adjust)
10715 *adjust = len;
10716 memcpy (tmpbuf + first, past_reloc, second);
10717 tmpbuf[first + second] = '\0';
10718 return tmpbuf;
10719 }
10720
10721 as_bad (_("@%s reloc is not supported with %d-bit output format"),
10722 gotrel[j].str, 1 << (5 + object_64bit));
10723 return NULL;
10724 }
10725 }
10726
10727 /* Might be a symbol version string. Don't as_bad here. */
10728 return NULL;
10729 }
10730 #endif
10731
10732 bfd_reloc_code_real_type
10733 x86_cons (expressionS *exp, int size)
10734 {
10735 bfd_reloc_code_real_type got_reloc = NO_RELOC;
10736
10737 intel_syntax = -intel_syntax;
10738 exp->X_md = 0;
10739 expr_mode = expr_operator_none;
10740
10741 #if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
10742 && !defined (LEX_AT)) \
10743 || defined (TE_PE)
10744 if (size == 4 || (object_64bit && size == 8))
10745 {
10746 /* Handle @GOTOFF and the like in an expression. */
10747 char *save;
10748 char *gotfree_input_line;
10749 int adjust = 0;
10750
10751 save = input_line_pointer;
10752 gotfree_input_line = lex_got (&got_reloc, &adjust, NULL);
10753 if (gotfree_input_line)
10754 input_line_pointer = gotfree_input_line;
10755
10756 expression (exp);
10757
10758 if (gotfree_input_line)
10759 {
10760 /* expression () has merrily parsed up to the end of line,
10761 or a comma - in the wrong buffer. Transfer how far
10762 input_line_pointer has moved to the right buffer. */
10763 input_line_pointer = (save
10764 + (input_line_pointer - gotfree_input_line)
10765 + adjust);
10766 free (gotfree_input_line);
10767 if (exp->X_op == O_constant
10768 || exp->X_op == O_absent
10769 || exp->X_op == O_illegal
10770 || exp->X_op == O_register
10771 || exp->X_op == O_big)
10772 {
10773 char c = *input_line_pointer;
10774 *input_line_pointer = 0;
10775 as_bad (_("missing or invalid expression `%s'"), save);
10776 *input_line_pointer = c;
10777 }
10778 else if ((got_reloc == BFD_RELOC_386_PLT32
10779 || got_reloc == BFD_RELOC_X86_64_PLT32)
10780 && exp->X_op != O_symbol)
10781 {
10782 char c = *input_line_pointer;
10783 *input_line_pointer = 0;
10784 as_bad (_("invalid PLT expression `%s'"), save);
10785 *input_line_pointer = c;
10786 }
10787 }
10788 }
10789 else
10790 #endif
10791 expression (exp);
10792
10793 intel_syntax = -intel_syntax;
10794
10795 if (intel_syntax)
10796 i386_intel_simplify (exp);
10797
10798 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
10799 if (size <= 4 && expr_mode == expr_operator_present
10800 && exp->X_op == O_constant && !object_64bit)
10801 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
10802
10803 return got_reloc;
10804 }
10805
10806 static void
10807 signed_cons (int size)
10808 {
10809 if (object_64bit)
10810 cons_sign = 1;
10811 cons (size);
10812 cons_sign = -1;
10813 }
10814
10815 static void
10816 s_insn (int dummy ATTRIBUTE_UNUSED)
10817 {
10818 char mnemonic[MAX_MNEM_SIZE], *line = input_line_pointer, *ptr;
10819 char *saved_ilp = find_end_of_line (line, false), saved_char;
10820 const char *end;
10821 unsigned int j;
10822 valueT val;
10823 bool vex = false, xop = false, evex = false;
10824 static const templates tt = { &i.tm, &i.tm + 1 };
10825
10826 init_globals ();
10827
10828 saved_char = *saved_ilp;
10829 *saved_ilp = 0;
10830
10831 end = parse_insn (line, mnemonic, true);
10832 if (end == NULL)
10833 {
10834 bad:
10835 *saved_ilp = saved_char;
10836 ignore_rest_of_line ();
10837 i.tm.mnem_off = 0;
10838 return;
10839 }
10840 line += end - line;
10841
10842 current_templates = &tt;
10843 i.tm.mnem_off = MN__insn;
10844 i.tm.extension_opcode = None;
10845
10846 if (startswith (line, "VEX")
10847 && (line[3] == '.' || is_space_char (line[3])))
10848 {
10849 vex = true;
10850 line += 3;
10851 }
10852 else if (startswith (line, "XOP") && ISDIGIT (line[3]))
10853 {
10854 char *e;
10855 unsigned long n = strtoul (line + 3, &e, 16);
10856
10857 if (e == line + 5 && n >= 0x08 && n <= 0x1f
10858 && (*e == '.' || is_space_char (*e)))
10859 {
10860 xop = true;
10861 /* Arrange for build_vex_prefix() to emit 0x8f. */
10862 i.tm.opcode_space = SPACE_XOP08;
10863 i.insn_opcode_space = n;
10864 line = e;
10865 }
10866 }
10867 else if (startswith (line, "EVEX")
10868 && (line[4] == '.' || is_space_char (line[4])))
10869 {
10870 evex = true;
10871 line += 4;
10872 }
10873
10874 if (vex || xop
10875 ? i.vec_encoding == vex_encoding_evex
10876 : evex
10877 ? i.vec_encoding == vex_encoding_vex
10878 || i.vec_encoding == vex_encoding_vex3
10879 : i.vec_encoding != vex_encoding_default)
10880 {
10881 as_bad (_("pseudo-prefix conflicts with encoding specifier"));
10882 goto bad;
10883 }
10884
10885 if (line > end && i.vec_encoding == vex_encoding_default)
10886 i.vec_encoding = evex ? vex_encoding_evex : vex_encoding_vex;
10887
10888 if (i.vec_encoding != vex_encoding_default)
10889 {
10890 /* Only address size and segment override prefixes are permitted with
10891 VEX/XOP/EVEX encodings. */
10892 const unsigned char *p = i.prefix;
10893
10894 for (j = 0; j < ARRAY_SIZE (i.prefix); ++j, ++p)
10895 {
10896 if (!*p)
10897 continue;
10898
10899 switch (j)
10900 {
10901 case SEG_PREFIX:
10902 case ADDR_PREFIX:
10903 break;
10904 default:
10905 as_bad (_("illegal prefix used with VEX/XOP/EVEX"));
10906 goto bad;
10907 }
10908 }
10909 }
10910
10911 if (line > end && *line == '.')
10912 {
10913 /* Length specifier (VEX.L, XOP.L, EVEX.L'L). */
10914 switch (line[1])
10915 {
10916 case 'L':
10917 switch (line[2])
10918 {
10919 case '0':
10920 if (evex)
10921 i.tm.opcode_modifier.evex = EVEX128;
10922 else
10923 i.tm.opcode_modifier.vex = VEX128;
10924 break;
10925
10926 case '1':
10927 if (evex)
10928 i.tm.opcode_modifier.evex = EVEX256;
10929 else
10930 i.tm.opcode_modifier.vex = VEX256;
10931 break;
10932
10933 case '2':
10934 if (evex)
10935 i.tm.opcode_modifier.evex = EVEX512;
10936 break;
10937
10938 case '3':
10939 if (evex)
10940 i.tm.opcode_modifier.evex = EVEX_L3;
10941 break;
10942
10943 case 'I':
10944 if (line[3] == 'G')
10945 {
10946 if (evex)
10947 i.tm.opcode_modifier.evex = EVEXLIG;
10948 else
10949 i.tm.opcode_modifier.vex = VEXScalar; /* LIG */
10950 ++line;
10951 }
10952 break;
10953 }
10954
10955 if (i.tm.opcode_modifier.vex || i.tm.opcode_modifier.evex)
10956 line += 3;
10957 break;
10958
10959 case '1':
10960 if (line[2] == '2' && line[3] == '8')
10961 {
10962 if (evex)
10963 i.tm.opcode_modifier.evex = EVEX128;
10964 else
10965 i.tm.opcode_modifier.vex = VEX128;
10966 line += 4;
10967 }
10968 break;
10969
10970 case '2':
10971 if (line[2] == '5' && line[3] == '6')
10972 {
10973 if (evex)
10974 i.tm.opcode_modifier.evex = EVEX256;
10975 else
10976 i.tm.opcode_modifier.vex = VEX256;
10977 line += 4;
10978 }
10979 break;
10980
10981 case '5':
10982 if (evex && line[2] == '1' && line[3] == '2')
10983 {
10984 i.tm.opcode_modifier.evex = EVEX512;
10985 line += 4;
10986 }
10987 break;
10988 }
10989 }
10990
10991 if (line > end && *line == '.')
10992 {
10993 /* embedded prefix (VEX.pp, XOP.pp, EVEX.pp). */
10994 switch (line[1])
10995 {
10996 case 'N':
10997 if (line[2] == 'P')
10998 line += 3;
10999 break;
11000
11001 case '6':
11002 if (line[2] == '6')
11003 {
11004 i.tm.opcode_modifier.opcodeprefix = PREFIX_0X66;
11005 line += 3;
11006 }
11007 break;
11008
11009 case 'F': case 'f':
11010 if (line[2] == '3')
11011 {
11012 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
11013 line += 3;
11014 }
11015 else if (line[2] == '2')
11016 {
11017 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF2;
11018 line += 3;
11019 }
11020 break;
11021 }
11022 }
11023
11024 if (line > end && !xop && *line == '.')
11025 {
11026 /* Encoding space (VEX.mmmmm, EVEX.mmmm). */
11027 switch (line[1])
11028 {
11029 case '0':
11030 if (TOUPPER (line[2]) != 'F')
11031 break;
11032 if (line[3] == '.' || is_space_char (line[3]))
11033 {
11034 i.insn_opcode_space = SPACE_0F;
11035 line += 3;
11036 }
11037 else if (line[3] == '3'
11038 && (line[4] == '8' || TOUPPER (line[4]) == 'A')
11039 && (line[5] == '.' || is_space_char (line[5])))
11040 {
11041 i.insn_opcode_space = line[4] == '8' ? SPACE_0F38 : SPACE_0F3A;
11042 line += 5;
11043 }
11044 break;
11045
11046 case 'M':
11047 if (ISDIGIT (line[2]) && line[2] != '0')
11048 {
11049 char *e;
11050 unsigned long n = strtoul (line + 2, &e, 10);
11051
11052 if (n <= (evex ? 15 : 31)
11053 && (*e == '.' || is_space_char (*e)))
11054 {
11055 i.insn_opcode_space = n;
11056 line = e;
11057 }
11058 }
11059 break;
11060 }
11061 }
11062
11063 if (line > end && *line == '.' && line[1] == 'W')
11064 {
11065 /* VEX.W, XOP.W, EVEX.W */
11066 switch (line[2])
11067 {
11068 case '0':
11069 i.tm.opcode_modifier.vexw = VEXW0;
11070 break;
11071
11072 case '1':
11073 i.tm.opcode_modifier.vexw = VEXW1;
11074 break;
11075
11076 case 'I':
11077 if (line[3] == 'G')
11078 {
11079 i.tm.opcode_modifier.vexw = VEXWIG;
11080 ++line;
11081 }
11082 break;
11083 }
11084
11085 if (i.tm.opcode_modifier.vexw)
11086 line += 3;
11087 }
11088
11089 if (line > end && *line && !is_space_char (*line))
11090 {
11091 /* Improve diagnostic a little. */
11092 if (*line == '.' && line[1] && !is_space_char (line[1]))
11093 ++line;
11094 goto done;
11095 }
11096
11097 /* Before processing the opcode expression, find trailing "+r" or
11098 "/<digit>" specifiers. */
11099 for (ptr = line; ; ++ptr)
11100 {
11101 unsigned long n;
11102 char *e;
11103
11104 ptr = strpbrk (ptr, "+/,");
11105 if (ptr == NULL || *ptr == ',')
11106 break;
11107
11108 if (*ptr == '+' && ptr[1] == 'r'
11109 && (ptr[2] == ',' || (is_space_char (ptr[2]) && ptr[3] == ',')))
11110 {
11111 *ptr = ' ';
11112 ptr[1] = ' ';
11113 i.short_form = true;
11114 break;
11115 }
11116
11117 if (*ptr == '/' && ISDIGIT (ptr[1])
11118 && (n = strtoul (ptr + 1, &e, 8)) < 8
11119 && e == ptr + 2
11120 && (ptr[2] == ',' || (is_space_char (ptr[2]) && ptr[3] == ',')))
11121 {
11122 *ptr = ' ';
11123 ptr[1] = ' ';
11124 i.tm.extension_opcode = n;
11125 i.tm.opcode_modifier.modrm = 1;
11126 break;
11127 }
11128 }
11129
11130 input_line_pointer = line;
11131 val = get_absolute_expression ();
11132 line = input_line_pointer;
11133
11134 if (i.short_form && (val & 7))
11135 as_warn ("`+r' assumes low three opcode bits to be clear");
11136
11137 for (j = 1; j < sizeof(val); ++j)
11138 if (!(val >> (j * 8)))
11139 break;
11140
11141 /* Trim off a prefix if present. */
11142 if (j > 1 && !vex && !xop && !evex)
11143 {
11144 uint8_t byte = val >> ((j - 1) * 8);
11145
11146 switch (byte)
11147 {
11148 case DATA_PREFIX_OPCODE:
11149 case REPE_PREFIX_OPCODE:
11150 case REPNE_PREFIX_OPCODE:
11151 if (!add_prefix (byte))
11152 goto bad;
11153 val &= ((uint64_t)1 << (--j * 8)) - 1;
11154 break;
11155 }
11156 }
11157
11158 /* Trim off encoding space. */
11159 if (j > 1 && !i.insn_opcode_space && (val >> ((j - 1) * 8)) == 0x0f)
11160 {
11161 uint8_t byte = val >> ((--j - 1) * 8);
11162
11163 i.insn_opcode_space = SPACE_0F;
11164 switch (byte & -(j > 1))
11165 {
11166 case 0x38:
11167 i.insn_opcode_space = SPACE_0F38;
11168 --j;
11169 break;
11170 case 0x3a:
11171 i.insn_opcode_space = SPACE_0F3A;
11172 --j;
11173 break;
11174 }
11175 i.tm.opcode_space = i.insn_opcode_space;
11176 val &= ((uint64_t)1 << (j * 8)) - 1;
11177 }
11178 if (!i.tm.opcode_space && (vex || evex))
11179 /* Arrange for build_vex_prefix() to properly emit 0xC4/0xC5.
11180 Also avoid hitting abort() there or in build_evex_prefix(). */
11181 i.tm.opcode_space = i.insn_opcode_space == SPACE_0F ? SPACE_0F
11182 : SPACE_0F38;
11183
11184 if (j > 2)
11185 {
11186 as_bad (_("opcode residual (%#"PRIx64") too wide"), (uint64_t) val);
11187 goto bad;
11188 }
11189 i.opcode_length = j;
11190
11191 /* Handle operands, if any. */
11192 if (*line == ',')
11193 {
11194 i386_operand_type combined;
11195 expressionS *disp_exp = NULL;
11196 bool changed;
11197
11198 i.memshift = -1;
11199
11200 ptr = parse_operands (line + 1, &i386_mnemonics[MN__insn]);
11201 this_operand = -1;
11202 if (!ptr)
11203 goto bad;
11204 line = ptr;
11205
11206 if (!i.operands)
11207 {
11208 as_bad (_("expecting operand after ','; got nothing"));
11209 goto done;
11210 }
11211
11212 if (i.mem_operands > 1)
11213 {
11214 as_bad (_("too many memory references for `%s'"),
11215 &i386_mnemonics[MN__insn]);
11216 goto done;
11217 }
11218
11219 /* Are we to emit ModR/M encoding? */
11220 if (!i.short_form
11221 && (i.mem_operands
11222 || i.reg_operands > (i.vec_encoding != vex_encoding_default)
11223 || i.tm.extension_opcode != None))
11224 i.tm.opcode_modifier.modrm = 1;
11225
11226 if (!i.tm.opcode_modifier.modrm
11227 && (i.reg_operands
11228 > i.short_form + 0U + (i.vec_encoding != vex_encoding_default)
11229 || i.mem_operands))
11230 {
11231 as_bad (_("too many register/memory operands"));
11232 goto done;
11233 }
11234
11235 /* Enforce certain constraints on operands. */
11236 switch (i.reg_operands + i.mem_operands
11237 + (i.tm.extension_opcode != None))
11238 {
11239 case 0:
11240 if (i.short_form)
11241 {
11242 as_bad (_("too few register/memory operands"));
11243 goto done;
11244 }
11245 /* Fall through. */
11246 case 1:
11247 if (i.tm.opcode_modifier.modrm)
11248 {
11249 as_bad (_("too few register/memory operands"));
11250 goto done;
11251 }
11252 break;
11253
11254 case 2:
11255 break;
11256
11257 case 4:
11258 if (i.imm_operands
11259 && (i.op[0].imms->X_op != O_constant
11260 || !fits_in_imm4 (i.op[0].imms->X_add_number)))
11261 {
11262 as_bad (_("constant doesn't fit in %d bits"), evex ? 3 : 4);
11263 goto done;
11264 }
11265 /* Fall through. */
11266 case 3:
11267 if (i.vec_encoding != vex_encoding_default)
11268 {
11269 i.tm.opcode_modifier.vexvvvv = 1;
11270 break;
11271 }
11272 /* Fall through. */
11273 default:
11274 as_bad (_("too many register/memory operands"));
11275 goto done;
11276 }
11277
11278 /* Bring operands into canonical order (imm, mem, reg). */
11279 do
11280 {
11281 changed = false;
11282
11283 for (j = 1; j < i.operands; ++j)
11284 {
11285 if ((!operand_type_check (i.types[j - 1], imm)
11286 && operand_type_check (i.types[j], imm))
11287 || (i.types[j - 1].bitfield.class != ClassNone
11288 && i.types[j].bitfield.class == ClassNone))
11289 {
11290 swap_2_operands (j - 1, j);
11291 changed = true;
11292 }
11293 }
11294 }
11295 while (changed);
11296
11297 /* For Intel syntax swap the order of register operands. */
11298 if (intel_syntax)
11299 switch (i.reg_operands)
11300 {
11301 case 0:
11302 case 1:
11303 break;
11304
11305 case 4:
11306 swap_2_operands (i.imm_operands + i.mem_operands + 1, i.operands - 2);
11307 /* Fall through. */
11308 case 3:
11309 case 2:
11310 swap_2_operands (i.imm_operands + i.mem_operands, i.operands - 1);
11311 break;
11312
11313 default:
11314 abort ();
11315 }
11316
11317 /* Enforce constraints when using VSIB. */
11318 if (i.index_reg
11319 && (i.index_reg->reg_type.bitfield.xmmword
11320 || i.index_reg->reg_type.bitfield.ymmword
11321 || i.index_reg->reg_type.bitfield.zmmword))
11322 {
11323 if (i.vec_encoding == vex_encoding_default)
11324 {
11325 as_bad (_("VSIB unavailable with legacy encoding"));
11326 goto done;
11327 }
11328
11329 if (i.vec_encoding == vex_encoding_evex
11330 && i.reg_operands > 1)
11331 {
11332 /* We could allow two register operands, encoding the 2nd one in
11333 an 8-bit immediate like for 4-register-operand insns, but that
11334 would require ugly fiddling with process_operands() and/or
11335 build_modrm_byte(). */
11336 as_bad (_("too many register operands with VSIB"));
11337 goto done;
11338 }
11339
11340 i.tm.opcode_modifier.sib = 1;
11341 }
11342
11343 /* Establish operand size encoding. */
11344 operand_type_set (&combined, 0);
11345
11346 for (j = i.imm_operands; j < i.operands; ++j)
11347 {
11348 i.types[j].bitfield.instance = InstanceNone;
11349
11350 if (operand_type_check (i.types[j], disp))
11351 {
11352 i.types[j].bitfield.baseindex = 1;
11353 disp_exp = i.op[j].disps;
11354 }
11355
11356 if (evex && i.types[j].bitfield.baseindex)
11357 {
11358 unsigned int n = i.memshift;
11359
11360 if (i.types[j].bitfield.byte)
11361 n = 0;
11362 else if (i.types[j].bitfield.word)
11363 n = 1;
11364 else if (i.types[j].bitfield.dword)
11365 n = 2;
11366 else if (i.types[j].bitfield.qword)
11367 n = 3;
11368 else if (i.types[j].bitfield.xmmword)
11369 n = 4;
11370 else if (i.types[j].bitfield.ymmword)
11371 n = 5;
11372 else if (i.types[j].bitfield.zmmword)
11373 n = 6;
11374
11375 if (i.memshift < 32 && n != i.memshift)
11376 as_warn ("conflicting memory operand size specifiers");
11377 i.memshift = n;
11378 }
11379
11380 if ((i.broadcast.type || i.broadcast.bytes)
11381 && j == i.broadcast.operand)
11382 continue;
11383
11384 combined = operand_type_or (combined, i.types[j]);
11385 combined.bitfield.class = ClassNone;
11386 }
11387
11388 switch ((i.broadcast.type ? i.broadcast.type : 1)
11389 << (i.memshift < 32 ? i.memshift : 0))
11390 {
11391 case 64: combined.bitfield.zmmword = 1; break;
11392 case 32: combined.bitfield.ymmword = 1; break;
11393 case 16: combined.bitfield.xmmword = 1; break;
11394 case 8: combined.bitfield.qword = 1; break;
11395 case 4: combined.bitfield.dword = 1; break;
11396 }
11397
11398 if (i.vec_encoding == vex_encoding_default)
11399 {
11400 if (flag_code == CODE_64BIT && combined.bitfield.qword)
11401 i.rex |= REX_W;
11402 else if ((flag_code == CODE_16BIT ? combined.bitfield.dword
11403 : combined.bitfield.word)
11404 && !add_prefix (DATA_PREFIX_OPCODE))
11405 goto done;
11406 }
11407 else if (!i.tm.opcode_modifier.vexw)
11408 {
11409 if (flag_code == CODE_64BIT)
11410 {
11411 if (combined.bitfield.qword)
11412 i.tm.opcode_modifier.vexw = VEXW1;
11413 else if (combined.bitfield.dword)
11414 i.tm.opcode_modifier.vexw = VEXW0;
11415 }
11416
11417 if (!i.tm.opcode_modifier.vexw)
11418 i.tm.opcode_modifier.vexw = VEXWIG;
11419 }
11420
11421 if (vex || xop)
11422 {
11423 if (!i.tm.opcode_modifier.vex)
11424 {
11425 if (combined.bitfield.ymmword)
11426 i.tm.opcode_modifier.vex = VEX256;
11427 else if (combined.bitfield.xmmword)
11428 i.tm.opcode_modifier.vex = VEX128;
11429 }
11430 }
11431 else if (evex)
11432 {
11433 if (!i.tm.opcode_modifier.evex)
11434 {
11435 /* Do _not_ consider AVX512VL here. */
11436 if (i.rounding.type != rc_none || combined.bitfield.zmmword)
11437 i.tm.opcode_modifier.evex = EVEX512;
11438 else if (combined.bitfield.ymmword)
11439 i.tm.opcode_modifier.evex = EVEX256;
11440 else if (combined.bitfield.xmmword)
11441 i.tm.opcode_modifier.evex = EVEX128;
11442 }
11443
11444 if (i.memshift >= 32)
11445 {
11446 unsigned int n = 0;
11447
11448 switch (i.tm.opcode_modifier.evex)
11449 {
11450 case EVEX512: n = 64; break;
11451 case EVEX256: n = 32; break;
11452 case EVEX128: n = 16; break;
11453 }
11454
11455 if (i.broadcast.type)
11456 n /= i.broadcast.type;
11457
11458 if (n > 0)
11459 for (i.memshift = 0; !(n & 1); n >>= 1)
11460 ++i.memshift;
11461 else if (disp_exp != NULL && disp_exp->X_op == O_constant
11462 && disp_exp->X_add_number != 0
11463 && i.disp_encoding != disp_encoding_32bit)
11464 {
11465 if (!quiet_warnings)
11466 as_warn ("cannot determine memory operand size");
11467 i.disp_encoding = disp_encoding_32bit;
11468 }
11469 }
11470 }
11471
11472 if (i.memshift >= 32)
11473 i.memshift = 0;
11474 else if (!evex)
11475 i.vec_encoding = vex_encoding_error;
11476
11477 if (i.disp_operands && !optimize_disp (&i.tm))
11478 goto done;
11479
11480 /* Establish size for immediate operands. */
11481 for (j = 0; j < i.imm_operands; ++j)
11482 {
11483 expressionS *expP = i.op[j].imms;
11484
11485 gas_assert (operand_type_check (i.types[j], imm));
11486 operand_type_set (&i.types[j], 0);
11487
11488 if (i.imm_bits[j] > 32)
11489 i.types[j].bitfield.imm64 = 1;
11490 else if (i.imm_bits[j] > 16)
11491 {
11492 if (flag_code == CODE_64BIT && (i.flags[j] & Operand_Signed))
11493 i.types[j].bitfield.imm32s = 1;
11494 else
11495 i.types[j].bitfield.imm32 = 1;
11496 }
11497 else if (i.imm_bits[j] > 8)
11498 i.types[j].bitfield.imm16 = 1;
11499 else if (i.imm_bits[j] > 0)
11500 {
11501 if (i.flags[j] & Operand_Signed)
11502 i.types[j].bitfield.imm8s = 1;
11503 else
11504 i.types[j].bitfield.imm8 = 1;
11505 }
11506 else if (expP->X_op == O_constant)
11507 {
11508 i.types[j] = smallest_imm_type (expP->X_add_number);
11509 i.types[j].bitfield.imm1 = 0;
11510 /* Oddly enough imm_size() checks imm64 first, so the bit needs
11511 zapping since smallest_imm_type() sets it unconditionally. */
11512 if (flag_code != CODE_64BIT)
11513 {
11514 i.types[j].bitfield.imm64 = 0;
11515 i.types[j].bitfield.imm32s = 0;
11516 i.types[j].bitfield.imm32 = 1;
11517 }
11518 else if (i.types[j].bitfield.imm32 || i.types[j].bitfield.imm32s)
11519 i.types[j].bitfield.imm64 = 0;
11520 }
11521 else
11522 /* Non-constant expressions are sized heuristically. */
11523 switch (flag_code)
11524 {
11525 case CODE_64BIT: i.types[j].bitfield.imm32s = 1; break;
11526 case CODE_32BIT: i.types[j].bitfield.imm32 = 1; break;
11527 case CODE_16BIT: i.types[j].bitfield.imm16 = 1; break;
11528 }
11529 }
11530
11531 for (j = 0; j < i.operands; ++j)
11532 i.tm.operand_types[j] = i.types[j];
11533
11534 process_operands ();
11535 }
11536
11537 /* Don't set opcode until after processing operands, to avoid any
11538 potential special casing there. */
11539 i.tm.base_opcode |= val;
11540
11541 if (i.vec_encoding == vex_encoding_error
11542 || (i.vec_encoding != vex_encoding_evex
11543 ? i.broadcast.type || i.broadcast.bytes
11544 || i.rounding.type != rc_none
11545 || i.mask.reg
11546 : (i.broadcast.type || i.broadcast.bytes)
11547 && i.rounding.type != rc_none))
11548 {
11549 as_bad (_("conflicting .insn operands"));
11550 goto done;
11551 }
11552
11553 if (vex || xop)
11554 {
11555 if (!i.tm.opcode_modifier.vex)
11556 i.tm.opcode_modifier.vex = VEXScalar; /* LIG */
11557
11558 build_vex_prefix (NULL);
11559 i.rex &= REX_OPCODE;
11560 }
11561 else if (evex)
11562 {
11563 if (!i.tm.opcode_modifier.evex)
11564 i.tm.opcode_modifier.evex = EVEXLIG;
11565
11566 build_evex_prefix ();
11567 i.rex &= REX_OPCODE;
11568 }
11569 else if (i.rex != 0)
11570 add_prefix (REX_OPCODE | i.rex);
11571
11572 output_insn ();
11573
11574 done:
11575 *saved_ilp = saved_char;
11576 input_line_pointer = line;
11577
11578 demand_empty_rest_of_line ();
11579
11580 /* Make sure dot_insn() won't yield "true" anymore. */
11581 i.tm.mnem_off = 0;
11582 }
11583
11584 #ifdef TE_PE
11585 static void
11586 pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
11587 {
11588 expressionS exp;
11589
11590 do
11591 {
11592 expression (&exp);
11593 if (exp.X_op == O_symbol)
11594 exp.X_op = O_secrel;
11595
11596 emit_expr (&exp, 4);
11597 }
11598 while (*input_line_pointer++ == ',');
11599
11600 input_line_pointer--;
11601 demand_empty_rest_of_line ();
11602 }
11603
11604 static void
11605 pe_directive_secidx (int dummy ATTRIBUTE_UNUSED)
11606 {
11607 expressionS exp;
11608
11609 do
11610 {
11611 expression (&exp);
11612 if (exp.X_op == O_symbol)
11613 exp.X_op = O_secidx;
11614
11615 emit_expr (&exp, 2);
11616 }
11617 while (*input_line_pointer++ == ',');
11618
11619 input_line_pointer--;
11620 demand_empty_rest_of_line ();
11621 }
11622 #endif
11623
11624 /* Handle Rounding Control / SAE specifiers. */
11625
11626 static char *
11627 RC_SAE_specifier (const char *pstr)
11628 {
11629 unsigned int j;
11630
11631 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); j++)
11632 {
11633 if (!strncmp (pstr, RC_NamesTable[j].name, RC_NamesTable[j].len))
11634 {
11635 if (i.rounding.type != rc_none)
11636 {
11637 as_bad (_("duplicated `{%s}'"), RC_NamesTable[j].name);
11638 return NULL;
11639 }
11640
11641 i.rounding.type = RC_NamesTable[j].type;
11642
11643 return (char *)(pstr + RC_NamesTable[j].len);
11644 }
11645 }
11646
11647 return NULL;
11648 }
11649
11650 /* Handle Vector operations. */
11651
11652 static char *
11653 check_VecOperations (char *op_string)
11654 {
11655 const reg_entry *mask;
11656 const char *saved;
11657 char *end_op;
11658
11659 while (*op_string)
11660 {
11661 saved = op_string;
11662 if (*op_string == '{')
11663 {
11664 op_string++;
11665
11666 /* Check broadcasts. */
11667 if (startswith (op_string, "1to"))
11668 {
11669 unsigned int bcst_type;
11670
11671 if (i.broadcast.type)
11672 goto duplicated_vec_op;
11673
11674 op_string += 3;
11675 if (*op_string == '8')
11676 bcst_type = 8;
11677 else if (*op_string == '4')
11678 bcst_type = 4;
11679 else if (*op_string == '2')
11680 bcst_type = 2;
11681 else if (*op_string == '1'
11682 && *(op_string+1) == '6')
11683 {
11684 bcst_type = 16;
11685 op_string++;
11686 }
11687 else if (*op_string == '3'
11688 && *(op_string+1) == '2')
11689 {
11690 bcst_type = 32;
11691 op_string++;
11692 }
11693 else
11694 {
11695 as_bad (_("Unsupported broadcast: `%s'"), saved);
11696 return NULL;
11697 }
11698 op_string++;
11699
11700 i.broadcast.type = bcst_type;
11701 i.broadcast.operand = this_operand;
11702
11703 /* For .insn a data size specifier may be appended. */
11704 if (dot_insn () && *op_string == ':')
11705 goto dot_insn_modifier;
11706 }
11707 /* Check .insn special cases. */
11708 else if (dot_insn () && *op_string == ':')
11709 {
11710 dot_insn_modifier:
11711 switch (op_string[1])
11712 {
11713 unsigned long n;
11714
11715 case 'd':
11716 if (i.memshift < 32)
11717 goto duplicated_vec_op;
11718
11719 n = strtoul (op_string + 2, &end_op, 0);
11720 if (n)
11721 for (i.memshift = 0; !(n & 1); n >>= 1)
11722 ++i.memshift;
11723 if (i.memshift < 32 && n == 1)
11724 op_string = end_op;
11725 break;
11726
11727 case 's': case 'u':
11728 /* This isn't really a "vector" operation, but a sign/size
11729 specifier for immediate operands of .insn. Note that AT&T
11730 syntax handles the same in i386_immediate(). */
11731 if (!intel_syntax)
11732 break;
11733
11734 if (i.imm_bits[this_operand])
11735 goto duplicated_vec_op;
11736
11737 n = strtoul (op_string + 2, &end_op, 0);
11738 if (n && n <= (flag_code == CODE_64BIT ? 64 : 32))
11739 {
11740 i.imm_bits[this_operand] = n;
11741 if (op_string[1] == 's')
11742 i.flags[this_operand] |= Operand_Signed;
11743 op_string = end_op;
11744 }
11745 break;
11746 }
11747 }
11748 /* Check masking operation. */
11749 else if ((mask = parse_register (op_string, &end_op)) != NULL)
11750 {
11751 if (mask == &bad_reg)
11752 return NULL;
11753
11754 /* k0 can't be used for write mask. */
11755 if (mask->reg_type.bitfield.class != RegMask || !mask->reg_num)
11756 {
11757 as_bad (_("`%s%s' can't be used for write mask"),
11758 register_prefix, mask->reg_name);
11759 return NULL;
11760 }
11761
11762 if (!i.mask.reg)
11763 {
11764 i.mask.reg = mask;
11765 i.mask.operand = this_operand;
11766 }
11767 else if (i.mask.reg->reg_num)
11768 goto duplicated_vec_op;
11769 else
11770 {
11771 i.mask.reg = mask;
11772
11773 /* Only "{z}" is allowed here. No need to check
11774 zeroing mask explicitly. */
11775 if (i.mask.operand != (unsigned int) this_operand)
11776 {
11777 as_bad (_("invalid write mask `%s'"), saved);
11778 return NULL;
11779 }
11780 }
11781
11782 op_string = end_op;
11783 }
11784 /* Check zeroing-flag for masking operation. */
11785 else if (*op_string == 'z')
11786 {
11787 if (!i.mask.reg)
11788 {
11789 i.mask.reg = reg_k0;
11790 i.mask.zeroing = 1;
11791 i.mask.operand = this_operand;
11792 }
11793 else
11794 {
11795 if (i.mask.zeroing)
11796 {
11797 duplicated_vec_op:
11798 as_bad (_("duplicated `%s'"), saved);
11799 return NULL;
11800 }
11801
11802 i.mask.zeroing = 1;
11803
11804 /* Only "{%k}" is allowed here. No need to check mask
11805 register explicitly. */
11806 if (i.mask.operand != (unsigned int) this_operand)
11807 {
11808 as_bad (_("invalid zeroing-masking `%s'"),
11809 saved);
11810 return NULL;
11811 }
11812 }
11813
11814 op_string++;
11815 }
11816 else if (intel_syntax
11817 && (op_string = RC_SAE_specifier (op_string)) != NULL)
11818 i.rounding.modifier = true;
11819 else
11820 goto unknown_vec_op;
11821
11822 if (*op_string != '}')
11823 {
11824 as_bad (_("missing `}' in `%s'"), saved);
11825 return NULL;
11826 }
11827 op_string++;
11828
11829 /* Strip whitespace since the addition of pseudo prefixes
11830 changed how the scrubber treats '{'. */
11831 if (is_space_char (*op_string))
11832 ++op_string;
11833
11834 continue;
11835 }
11836 unknown_vec_op:
11837 /* We don't know this one. */
11838 as_bad (_("unknown vector operation: `%s'"), saved);
11839 return NULL;
11840 }
11841
11842 if (i.mask.reg && i.mask.zeroing && !i.mask.reg->reg_num)
11843 {
11844 as_bad (_("zeroing-masking only allowed with write mask"));
11845 return NULL;
11846 }
11847
11848 return op_string;
11849 }
11850
11851 static int
11852 i386_immediate (char *imm_start)
11853 {
11854 char *save_input_line_pointer;
11855 char *gotfree_input_line;
11856 segT exp_seg = 0;
11857 expressionS *exp;
11858 i386_operand_type types;
11859
11860 operand_type_set (&types, ~0);
11861
11862 if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
11863 {
11864 as_bad (_("at most %d immediate operands are allowed"),
11865 MAX_IMMEDIATE_OPERANDS);
11866 return 0;
11867 }
11868
11869 exp = &im_expressions[i.imm_operands++];
11870 i.op[this_operand].imms = exp;
11871
11872 if (is_space_char (*imm_start))
11873 ++imm_start;
11874
11875 save_input_line_pointer = input_line_pointer;
11876 input_line_pointer = imm_start;
11877
11878 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
11879 if (gotfree_input_line)
11880 input_line_pointer = gotfree_input_line;
11881
11882 expr_mode = expr_operator_none;
11883 exp_seg = expression (exp);
11884
11885 /* For .insn immediates there may be a size specifier. */
11886 if (dot_insn () && *input_line_pointer == '{' && input_line_pointer[1] == ':'
11887 && (input_line_pointer[2] == 's' || input_line_pointer[2] == 'u'))
11888 {
11889 char *e;
11890 unsigned long n = strtoul (input_line_pointer + 3, &e, 0);
11891
11892 if (*e == '}' && n && n <= (flag_code == CODE_64BIT ? 64 : 32))
11893 {
11894 i.imm_bits[this_operand] = n;
11895 if (input_line_pointer[2] == 's')
11896 i.flags[this_operand] |= Operand_Signed;
11897 input_line_pointer = e + 1;
11898 }
11899 }
11900
11901 SKIP_WHITESPACE ();
11902 if (*input_line_pointer)
11903 as_bad (_("junk `%s' after expression"), input_line_pointer);
11904
11905 input_line_pointer = save_input_line_pointer;
11906 if (gotfree_input_line)
11907 {
11908 free (gotfree_input_line);
11909
11910 if (exp->X_op == O_constant)
11911 exp->X_op = O_illegal;
11912 }
11913
11914 if (exp_seg == reg_section)
11915 {
11916 as_bad (_("illegal immediate register operand %s"), imm_start);
11917 return 0;
11918 }
11919
11920 return i386_finalize_immediate (exp_seg, exp, types, imm_start);
11921 }
11922
11923 static int
11924 i386_finalize_immediate (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
11925 i386_operand_type types, const char *imm_start)
11926 {
11927 if (exp->X_op == O_absent || exp->X_op == O_illegal || exp->X_op == O_big)
11928 {
11929 if (imm_start)
11930 as_bad (_("missing or invalid immediate expression `%s'"),
11931 imm_start);
11932 return 0;
11933 }
11934 else if (exp->X_op == O_constant)
11935 {
11936 /* Size it properly later. */
11937 i.types[this_operand].bitfield.imm64 = 1;
11938
11939 /* If not 64bit, sign/zero extend val, to account for wraparound
11940 when !BFD64. */
11941 if (expr_mode == expr_operator_present
11942 && flag_code != CODE_64BIT && !object_64bit)
11943 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
11944 }
11945 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
11946 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
11947 && exp_seg != absolute_section
11948 && exp_seg != text_section
11949 && exp_seg != data_section
11950 && exp_seg != bss_section
11951 && exp_seg != undefined_section
11952 && !bfd_is_com_section (exp_seg))
11953 {
11954 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
11955 return 0;
11956 }
11957 #endif
11958 else
11959 {
11960 /* This is an address. The size of the address will be
11961 determined later, depending on destination register,
11962 suffix, or the default for the section. */
11963 i.types[this_operand].bitfield.imm8 = 1;
11964 i.types[this_operand].bitfield.imm16 = 1;
11965 i.types[this_operand].bitfield.imm32 = 1;
11966 i.types[this_operand].bitfield.imm32s = 1;
11967 i.types[this_operand].bitfield.imm64 = 1;
11968 i.types[this_operand] = operand_type_and (i.types[this_operand],
11969 types);
11970 }
11971
11972 return 1;
11973 }
11974
11975 static char *
11976 i386_scale (char *scale)
11977 {
11978 offsetT val;
11979 char *save = input_line_pointer;
11980
11981 input_line_pointer = scale;
11982 val = get_absolute_expression ();
11983
11984 switch (val)
11985 {
11986 case 1:
11987 i.log2_scale_factor = 0;
11988 break;
11989 case 2:
11990 i.log2_scale_factor = 1;
11991 break;
11992 case 4:
11993 i.log2_scale_factor = 2;
11994 break;
11995 case 8:
11996 i.log2_scale_factor = 3;
11997 break;
11998 default:
11999 {
12000 char sep = *input_line_pointer;
12001
12002 *input_line_pointer = '\0';
12003 as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
12004 scale);
12005 *input_line_pointer = sep;
12006 input_line_pointer = save;
12007 return NULL;
12008 }
12009 }
12010 if (i.log2_scale_factor != 0 && i.index_reg == 0)
12011 {
12012 as_warn (_("scale factor of %d without an index register"),
12013 1 << i.log2_scale_factor);
12014 i.log2_scale_factor = 0;
12015 }
12016 scale = input_line_pointer;
12017 input_line_pointer = save;
12018 return scale;
12019 }
12020
12021 static int
12022 i386_displacement (char *disp_start, char *disp_end)
12023 {
12024 expressionS *exp;
12025 segT exp_seg = 0;
12026 char *save_input_line_pointer;
12027 char *gotfree_input_line;
12028 int override;
12029 i386_operand_type bigdisp, types = anydisp;
12030 int ret;
12031
12032 if (i.disp_operands == MAX_MEMORY_OPERANDS)
12033 {
12034 as_bad (_("at most %d displacement operands are allowed"),
12035 MAX_MEMORY_OPERANDS);
12036 return 0;
12037 }
12038
12039 operand_type_set (&bigdisp, 0);
12040 if (i.jumpabsolute
12041 || i.types[this_operand].bitfield.baseindex
12042 || (current_templates->start->opcode_modifier.jump != JUMP
12043 && current_templates->start->opcode_modifier.jump != JUMP_DWORD))
12044 {
12045 i386_addressing_mode ();
12046 override = (i.prefix[ADDR_PREFIX] != 0);
12047 if (flag_code == CODE_64BIT)
12048 {
12049 bigdisp.bitfield.disp32 = 1;
12050 if (!override)
12051 bigdisp.bitfield.disp64 = 1;
12052 }
12053 else if ((flag_code == CODE_16BIT) ^ override)
12054 bigdisp.bitfield.disp16 = 1;
12055 else
12056 bigdisp.bitfield.disp32 = 1;
12057 }
12058 else
12059 {
12060 /* For PC-relative branches, the width of the displacement may be
12061 dependent upon data size, but is never dependent upon address size.
12062 Also make sure to not unintentionally match against a non-PC-relative
12063 branch template. */
12064 static templates aux_templates;
12065 const insn_template *t = current_templates->start;
12066 bool has_intel64 = false;
12067
12068 aux_templates.start = t;
12069 while (++t < current_templates->end)
12070 {
12071 if (t->opcode_modifier.jump
12072 != current_templates->start->opcode_modifier.jump)
12073 break;
12074 if ((t->opcode_modifier.isa64 >= INTEL64))
12075 has_intel64 = true;
12076 }
12077 if (t < current_templates->end)
12078 {
12079 aux_templates.end = t;
12080 current_templates = &aux_templates;
12081 }
12082
12083 override = (i.prefix[DATA_PREFIX] != 0);
12084 if (flag_code == CODE_64BIT)
12085 {
12086 if ((override || i.suffix == WORD_MNEM_SUFFIX)
12087 && (!intel64 || !has_intel64))
12088 bigdisp.bitfield.disp16 = 1;
12089 else
12090 bigdisp.bitfield.disp32 = 1;
12091 }
12092 else
12093 {
12094 if (!override)
12095 override = (i.suffix == (flag_code != CODE_16BIT
12096 ? WORD_MNEM_SUFFIX
12097 : LONG_MNEM_SUFFIX));
12098 bigdisp.bitfield.disp32 = 1;
12099 if ((flag_code == CODE_16BIT) ^ override)
12100 {
12101 bigdisp.bitfield.disp32 = 0;
12102 bigdisp.bitfield.disp16 = 1;
12103 }
12104 }
12105 }
12106 i.types[this_operand] = operand_type_or (i.types[this_operand],
12107 bigdisp);
12108
12109 exp = &disp_expressions[i.disp_operands];
12110 i.op[this_operand].disps = exp;
12111 i.disp_operands++;
12112 save_input_line_pointer = input_line_pointer;
12113 input_line_pointer = disp_start;
12114 END_STRING_AND_SAVE (disp_end);
12115
12116 #ifndef GCC_ASM_O_HACK
12117 #define GCC_ASM_O_HACK 0
12118 #endif
12119 #if GCC_ASM_O_HACK
12120 END_STRING_AND_SAVE (disp_end + 1);
12121 if (i.types[this_operand].bitfield.baseIndex
12122 && displacement_string_end[-1] == '+')
12123 {
12124 /* This hack is to avoid a warning when using the "o"
12125 constraint within gcc asm statements.
12126 For instance:
12127
12128 #define _set_tssldt_desc(n,addr,limit,type) \
12129 __asm__ __volatile__ ( \
12130 "movw %w2,%0\n\t" \
12131 "movw %w1,2+%0\n\t" \
12132 "rorl $16,%1\n\t" \
12133 "movb %b1,4+%0\n\t" \
12134 "movb %4,5+%0\n\t" \
12135 "movb $0,6+%0\n\t" \
12136 "movb %h1,7+%0\n\t" \
12137 "rorl $16,%1" \
12138 : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
12139
12140 This works great except that the output assembler ends
12141 up looking a bit weird if it turns out that there is
12142 no offset. You end up producing code that looks like:
12143
12144 #APP
12145 movw $235,(%eax)
12146 movw %dx,2+(%eax)
12147 rorl $16,%edx
12148 movb %dl,4+(%eax)
12149 movb $137,5+(%eax)
12150 movb $0,6+(%eax)
12151 movb %dh,7+(%eax)
12152 rorl $16,%edx
12153 #NO_APP
12154
12155 So here we provide the missing zero. */
12156
12157 *displacement_string_end = '0';
12158 }
12159 #endif
12160 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
12161 if (gotfree_input_line)
12162 input_line_pointer = gotfree_input_line;
12163
12164 expr_mode = expr_operator_none;
12165 exp_seg = expression (exp);
12166
12167 SKIP_WHITESPACE ();
12168 if (*input_line_pointer)
12169 as_bad (_("junk `%s' after expression"), input_line_pointer);
12170 #if GCC_ASM_O_HACK
12171 RESTORE_END_STRING (disp_end + 1);
12172 #endif
12173 input_line_pointer = save_input_line_pointer;
12174 if (gotfree_input_line)
12175 {
12176 free (gotfree_input_line);
12177
12178 if (exp->X_op == O_constant || exp->X_op == O_register)
12179 exp->X_op = O_illegal;
12180 }
12181
12182 ret = i386_finalize_displacement (exp_seg, exp, types, disp_start);
12183
12184 RESTORE_END_STRING (disp_end);
12185
12186 return ret;
12187 }
12188
12189 static int
12190 i386_finalize_displacement (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
12191 i386_operand_type types, const char *disp_start)
12192 {
12193 int ret = 1;
12194
12195 /* We do this to make sure that the section symbol is in
12196 the symbol table. We will ultimately change the relocation
12197 to be relative to the beginning of the section. */
12198 if (i.reloc[this_operand] == BFD_RELOC_386_GOTOFF
12199 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
12200 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
12201 {
12202 if (exp->X_op != O_symbol)
12203 goto inv_disp;
12204
12205 if (S_IS_LOCAL (exp->X_add_symbol)
12206 && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section
12207 && S_GET_SEGMENT (exp->X_add_symbol) != expr_section)
12208 section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
12209 exp->X_op = O_subtract;
12210 exp->X_op_symbol = GOT_symbol;
12211 if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
12212 i.reloc[this_operand] = BFD_RELOC_32_PCREL;
12213 else if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
12214 i.reloc[this_operand] = BFD_RELOC_64;
12215 else
12216 i.reloc[this_operand] = BFD_RELOC_32;
12217 }
12218
12219 else if (exp->X_op == O_absent
12220 || exp->X_op == O_illegal
12221 || exp->X_op == O_big)
12222 {
12223 inv_disp:
12224 as_bad (_("missing or invalid displacement expression `%s'"),
12225 disp_start);
12226 ret = 0;
12227 }
12228
12229 else if (exp->X_op == O_constant)
12230 {
12231 /* Sizing gets taken care of by optimize_disp().
12232
12233 If not 64bit, sign/zero extend val, to account for wraparound
12234 when !BFD64. */
12235 if (expr_mode == expr_operator_present
12236 && flag_code != CODE_64BIT && !object_64bit)
12237 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
12238 }
12239
12240 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
12241 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
12242 && exp_seg != absolute_section
12243 && exp_seg != text_section
12244 && exp_seg != data_section
12245 && exp_seg != bss_section
12246 && exp_seg != undefined_section
12247 && !bfd_is_com_section (exp_seg))
12248 {
12249 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
12250 ret = 0;
12251 }
12252 #endif
12253
12254 else if (current_templates->start->opcode_modifier.jump == JUMP_BYTE)
12255 i.types[this_operand].bitfield.disp8 = 1;
12256
12257 /* Check if this is a displacement only operand. */
12258 if (!i.types[this_operand].bitfield.baseindex)
12259 i.types[this_operand] =
12260 operand_type_or (operand_type_and_not (i.types[this_operand], anydisp),
12261 operand_type_and (i.types[this_operand], types));
12262
12263 return ret;
12264 }
12265
12266 /* Return the active addressing mode, taking address override and
12267 registers forming the address into consideration. Update the
12268 address override prefix if necessary. */
12269
12270 static enum flag_code
12271 i386_addressing_mode (void)
12272 {
12273 enum flag_code addr_mode;
12274
12275 if (i.prefix[ADDR_PREFIX])
12276 addr_mode = flag_code == CODE_32BIT ? CODE_16BIT : CODE_32BIT;
12277 else if (flag_code == CODE_16BIT
12278 && is_cpu (current_templates->start, CpuMPX)
12279 /* Avoid replacing the "16-bit addressing not allowed" diagnostic
12280 from md_assemble() by "is not a valid base/index expression"
12281 when there is a base and/or index. */
12282 && !i.types[this_operand].bitfield.baseindex)
12283 {
12284 /* MPX insn memory operands with neither base nor index must be forced
12285 to use 32-bit addressing in 16-bit mode. */
12286 addr_mode = CODE_32BIT;
12287 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
12288 ++i.prefixes;
12289 gas_assert (!i.types[this_operand].bitfield.disp16);
12290 gas_assert (!i.types[this_operand].bitfield.disp32);
12291 }
12292 else
12293 {
12294 addr_mode = flag_code;
12295
12296 #if INFER_ADDR_PREFIX
12297 if (i.mem_operands == 0)
12298 {
12299 /* Infer address prefix from the first memory operand. */
12300 const reg_entry *addr_reg = i.base_reg;
12301
12302 if (addr_reg == NULL)
12303 addr_reg = i.index_reg;
12304
12305 if (addr_reg)
12306 {
12307 if (addr_reg->reg_type.bitfield.dword)
12308 addr_mode = CODE_32BIT;
12309 else if (flag_code != CODE_64BIT
12310 && addr_reg->reg_type.bitfield.word)
12311 addr_mode = CODE_16BIT;
12312
12313 if (addr_mode != flag_code)
12314 {
12315 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
12316 i.prefixes += 1;
12317 /* Change the size of any displacement too. At most one
12318 of Disp16 or Disp32 is set.
12319 FIXME. There doesn't seem to be any real need for
12320 separate Disp16 and Disp32 flags. The same goes for
12321 Imm16 and Imm32. Removing them would probably clean
12322 up the code quite a lot. */
12323 if (flag_code != CODE_64BIT
12324 && (i.types[this_operand].bitfield.disp16
12325 || i.types[this_operand].bitfield.disp32))
12326 {
12327 static const i386_operand_type disp16_32 = {
12328 .bitfield = { .disp16 = 1, .disp32 = 1 }
12329 };
12330
12331 i.types[this_operand]
12332 = operand_type_xor (i.types[this_operand], disp16_32);
12333 }
12334 }
12335 }
12336 }
12337 #endif
12338 }
12339
12340 return addr_mode;
12341 }
12342
12343 /* Make sure the memory operand we've been dealt is valid.
12344 Return 1 on success, 0 on a failure. */
12345
12346 static int
12347 i386_index_check (const char *operand_string)
12348 {
12349 const char *kind = "base/index";
12350 enum flag_code addr_mode = i386_addressing_mode ();
12351 const insn_template *t = current_templates->end - 1;
12352
12353 if (t->opcode_modifier.isstring)
12354 {
12355 /* Memory operands of string insns are special in that they only allow
12356 a single register (rDI, rSI, or rBX) as their memory address. */
12357 const reg_entry *expected_reg;
12358 static const char di_si[][2][4] =
12359 {
12360 { "esi", "edi" },
12361 { "si", "di" },
12362 { "rsi", "rdi" }
12363 };
12364 static const char bx[][4] = { "ebx", "bx", "rbx" };
12365
12366 kind = "string address";
12367
12368 if (t->opcode_modifier.prefixok == PrefixRep)
12369 {
12370 int es_op = t->opcode_modifier.isstring - IS_STRING_ES_OP0;
12371 int op = 0;
12372
12373 if (!t->operand_types[0].bitfield.baseindex
12374 || ((!i.mem_operands != !intel_syntax)
12375 && t->operand_types[1].bitfield.baseindex))
12376 op = 1;
12377 expected_reg
12378 = (const reg_entry *) str_hash_find (reg_hash,
12379 di_si[addr_mode][op == es_op]);
12380 }
12381 else
12382 expected_reg
12383 = (const reg_entry *)str_hash_find (reg_hash, bx[addr_mode]);
12384
12385 if (i.base_reg != expected_reg
12386 || i.index_reg
12387 || operand_type_check (i.types[this_operand], disp))
12388 {
12389 /* The second memory operand must have the same size as
12390 the first one. */
12391 if (i.mem_operands
12392 && i.base_reg
12393 && !((addr_mode == CODE_64BIT
12394 && i.base_reg->reg_type.bitfield.qword)
12395 || (addr_mode == CODE_32BIT
12396 ? i.base_reg->reg_type.bitfield.dword
12397 : i.base_reg->reg_type.bitfield.word)))
12398 goto bad_address;
12399
12400 as_warn (_("`%s' is not valid here (expected `%c%s%s%c')"),
12401 operand_string,
12402 intel_syntax ? '[' : '(',
12403 register_prefix,
12404 expected_reg->reg_name,
12405 intel_syntax ? ']' : ')');
12406 return 1;
12407 }
12408 else
12409 return 1;
12410
12411 bad_address:
12412 as_bad (_("`%s' is not a valid %s expression"),
12413 operand_string, kind);
12414 return 0;
12415 }
12416 else
12417 {
12418 t = current_templates->start;
12419
12420 if (addr_mode != CODE_16BIT)
12421 {
12422 /* 32-bit/64-bit checks. */
12423 if (i.disp_encoding == disp_encoding_16bit)
12424 {
12425 bad_disp:
12426 as_bad (_("invalid `%s' prefix"),
12427 addr_mode == CODE_16BIT ? "{disp32}" : "{disp16}");
12428 return 0;
12429 }
12430
12431 if ((i.base_reg
12432 && ((addr_mode == CODE_64BIT
12433 ? !i.base_reg->reg_type.bitfield.qword
12434 : !i.base_reg->reg_type.bitfield.dword)
12435 || (i.index_reg && i.base_reg->reg_num == RegIP)
12436 || i.base_reg->reg_num == RegIZ))
12437 || (i.index_reg
12438 && !i.index_reg->reg_type.bitfield.xmmword
12439 && !i.index_reg->reg_type.bitfield.ymmword
12440 && !i.index_reg->reg_type.bitfield.zmmword
12441 && ((addr_mode == CODE_64BIT
12442 ? !i.index_reg->reg_type.bitfield.qword
12443 : !i.index_reg->reg_type.bitfield.dword)
12444 || !i.index_reg->reg_type.bitfield.baseindex)))
12445 goto bad_address;
12446
12447 /* bndmk, bndldx, bndstx and mandatory non-vector SIB have special restrictions. */
12448 if (t->mnem_off == MN_bndmk
12449 || t->mnem_off == MN_bndldx
12450 || t->mnem_off == MN_bndstx
12451 || t->opcode_modifier.sib == SIBMEM)
12452 {
12453 /* They cannot use RIP-relative addressing. */
12454 if (i.base_reg && i.base_reg->reg_num == RegIP)
12455 {
12456 as_bad (_("`%s' cannot be used here"), operand_string);
12457 return 0;
12458 }
12459
12460 /* bndldx and bndstx ignore their scale factor. */
12461 if ((t->mnem_off == MN_bndldx || t->mnem_off == MN_bndstx)
12462 && i.log2_scale_factor)
12463 as_warn (_("register scaling is being ignored here"));
12464 }
12465 }
12466 else
12467 {
12468 /* 16-bit checks. */
12469 if (i.disp_encoding == disp_encoding_32bit)
12470 goto bad_disp;
12471
12472 if ((i.base_reg
12473 && (!i.base_reg->reg_type.bitfield.word
12474 || !i.base_reg->reg_type.bitfield.baseindex))
12475 || (i.index_reg
12476 && (!i.index_reg->reg_type.bitfield.word
12477 || !i.index_reg->reg_type.bitfield.baseindex
12478 || !(i.base_reg
12479 && i.base_reg->reg_num < 6
12480 && i.index_reg->reg_num >= 6
12481 && i.log2_scale_factor == 0))))
12482 goto bad_address;
12483 }
12484 }
12485 return 1;
12486 }
12487
12488 /* Handle vector immediates. */
12489
12490 static int
12491 RC_SAE_immediate (const char *imm_start)
12492 {
12493 const char *pstr = imm_start;
12494
12495 if (*pstr != '{')
12496 return 0;
12497
12498 pstr = RC_SAE_specifier (pstr + 1);
12499 if (pstr == NULL)
12500 return 0;
12501
12502 if (*pstr++ != '}')
12503 {
12504 as_bad (_("Missing '}': '%s'"), imm_start);
12505 return 0;
12506 }
12507 /* RC/SAE immediate string should contain nothing more. */;
12508 if (*pstr != 0)
12509 {
12510 as_bad (_("Junk after '}': '%s'"), imm_start);
12511 return 0;
12512 }
12513
12514 /* Internally this doesn't count as an operand. */
12515 --i.operands;
12516
12517 return 1;
12518 }
12519
12520 static INLINE bool starts_memory_operand (char c)
12521 {
12522 return ISDIGIT (c)
12523 || is_name_beginner (c)
12524 || strchr ("([\"+-!~", c);
12525 }
12526
12527 /* Parse OPERAND_STRING into the i386_insn structure I. Returns zero
12528 on error. */
12529
12530 static int
12531 i386_att_operand (char *operand_string)
12532 {
12533 const reg_entry *r;
12534 char *end_op;
12535 char *op_string = operand_string;
12536
12537 if (is_space_char (*op_string))
12538 ++op_string;
12539
12540 /* We check for an absolute prefix (differentiating,
12541 for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
12542 if (*op_string == ABSOLUTE_PREFIX
12543 && current_templates->start->opcode_modifier.jump)
12544 {
12545 ++op_string;
12546 if (is_space_char (*op_string))
12547 ++op_string;
12548 i.jumpabsolute = true;
12549 }
12550
12551 /* Check if operand is a register. */
12552 if ((r = parse_register (op_string, &end_op)) != NULL)
12553 {
12554 i386_operand_type temp;
12555
12556 if (r == &bad_reg)
12557 return 0;
12558
12559 /* Check for a segment override by searching for ':' after a
12560 segment register. */
12561 op_string = end_op;
12562 if (is_space_char (*op_string))
12563 ++op_string;
12564 if (*op_string == ':' && r->reg_type.bitfield.class == SReg)
12565 {
12566 i.seg[i.mem_operands] = r;
12567
12568 /* Skip the ':' and whitespace. */
12569 ++op_string;
12570 if (is_space_char (*op_string))
12571 ++op_string;
12572
12573 /* Handle case of %es:*foo. */
12574 if (!i.jumpabsolute && *op_string == ABSOLUTE_PREFIX
12575 && current_templates->start->opcode_modifier.jump)
12576 {
12577 ++op_string;
12578 if (is_space_char (*op_string))
12579 ++op_string;
12580 i.jumpabsolute = true;
12581 }
12582
12583 if (!starts_memory_operand (*op_string))
12584 {
12585 as_bad (_("bad memory operand `%s'"), op_string);
12586 return 0;
12587 }
12588 goto do_memory_reference;
12589 }
12590
12591 /* Handle vector operations. */
12592 if (*op_string == '{')
12593 {
12594 op_string = check_VecOperations (op_string);
12595 if (op_string == NULL)
12596 return 0;
12597 }
12598
12599 if (*op_string)
12600 {
12601 as_bad (_("junk `%s' after register"), op_string);
12602 return 0;
12603 }
12604
12605 /* Reject pseudo registers for .insn. */
12606 if (dot_insn () && r->reg_type.bitfield.class == ClassNone)
12607 {
12608 as_bad (_("`%s%s' cannot be used here"),
12609 register_prefix, r->reg_name);
12610 return 0;
12611 }
12612
12613 temp = r->reg_type;
12614 temp.bitfield.baseindex = 0;
12615 i.types[this_operand] = operand_type_or (i.types[this_operand],
12616 temp);
12617 i.types[this_operand].bitfield.unspecified = 0;
12618 i.op[this_operand].regs = r;
12619 i.reg_operands++;
12620
12621 /* A GPR may follow an RC or SAE immediate only if a (vector) register
12622 operand was also present earlier on. */
12623 if (i.rounding.type != rc_none && temp.bitfield.class == Reg
12624 && i.reg_operands == 1)
12625 {
12626 unsigned int j;
12627
12628 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); ++j)
12629 if (i.rounding.type == RC_NamesTable[j].type)
12630 break;
12631 as_bad (_("`%s': misplaced `{%s}'"),
12632 insn_name (current_templates->start), RC_NamesTable[j].name);
12633 return 0;
12634 }
12635 }
12636 else if (*op_string == REGISTER_PREFIX)
12637 {
12638 as_bad (_("bad register name `%s'"), op_string);
12639 return 0;
12640 }
12641 else if (*op_string == IMMEDIATE_PREFIX)
12642 {
12643 ++op_string;
12644 if (i.jumpabsolute)
12645 {
12646 as_bad (_("immediate operand illegal with absolute jump"));
12647 return 0;
12648 }
12649 if (!i386_immediate (op_string))
12650 return 0;
12651 if (i.rounding.type != rc_none)
12652 {
12653 as_bad (_("`%s': RC/SAE operand must follow immediate operands"),
12654 insn_name (current_templates->start));
12655 return 0;
12656 }
12657 }
12658 else if (RC_SAE_immediate (operand_string))
12659 {
12660 /* If it is a RC or SAE immediate, do the necessary placement check:
12661 Only another immediate or a GPR may precede it. */
12662 if (i.mem_operands || i.reg_operands + i.imm_operands > 1
12663 || (i.reg_operands == 1
12664 && i.op[0].regs->reg_type.bitfield.class != Reg))
12665 {
12666 as_bad (_("`%s': misplaced `%s'"),
12667 insn_name (current_templates->start), operand_string);
12668 return 0;
12669 }
12670 }
12671 else if (starts_memory_operand (*op_string))
12672 {
12673 /* This is a memory reference of some sort. */
12674 char *base_string;
12675
12676 /* Start and end of displacement string expression (if found). */
12677 char *displacement_string_start;
12678 char *displacement_string_end;
12679
12680 do_memory_reference:
12681 /* Check for base index form. We detect the base index form by
12682 looking for an ')' at the end of the operand, searching
12683 for the '(' matching it, and finding a REGISTER_PREFIX or ','
12684 after the '('. */
12685 base_string = op_string + strlen (op_string);
12686
12687 /* Handle vector operations. */
12688 --base_string;
12689 if (is_space_char (*base_string))
12690 --base_string;
12691
12692 if (*base_string == '}')
12693 {
12694 char *vop_start = NULL;
12695
12696 while (base_string-- > op_string)
12697 {
12698 if (*base_string == '"')
12699 break;
12700 if (*base_string != '{')
12701 continue;
12702
12703 vop_start = base_string;
12704
12705 --base_string;
12706 if (is_space_char (*base_string))
12707 --base_string;
12708
12709 if (*base_string != '}')
12710 break;
12711
12712 vop_start = NULL;
12713 }
12714
12715 if (!vop_start)
12716 {
12717 as_bad (_("unbalanced figure braces"));
12718 return 0;
12719 }
12720
12721 if (check_VecOperations (vop_start) == NULL)
12722 return 0;
12723 }
12724
12725 /* If we only have a displacement, set-up for it to be parsed later. */
12726 displacement_string_start = op_string;
12727 displacement_string_end = base_string + 1;
12728
12729 if (*base_string == ')')
12730 {
12731 char *temp_string;
12732 unsigned int parens_not_balanced = 0;
12733 bool in_quotes = false;
12734
12735 /* We've already checked that the number of left & right ()'s are
12736 equal, and that there's a matching set of double quotes. */
12737 end_op = base_string;
12738 for (temp_string = op_string; temp_string < end_op; temp_string++)
12739 {
12740 if (*temp_string == '\\' && temp_string[1] == '"')
12741 ++temp_string;
12742 else if (*temp_string == '"')
12743 in_quotes = !in_quotes;
12744 else if (!in_quotes)
12745 {
12746 if (*temp_string == '(' && !parens_not_balanced++)
12747 base_string = temp_string;
12748 if (*temp_string == ')')
12749 --parens_not_balanced;
12750 }
12751 }
12752
12753 temp_string = base_string;
12754
12755 /* Skip past '(' and whitespace. */
12756 gas_assert (*base_string == '(');
12757 ++base_string;
12758 if (is_space_char (*base_string))
12759 ++base_string;
12760
12761 if (*base_string == ','
12762 || ((i.base_reg = parse_register (base_string, &end_op))
12763 != NULL))
12764 {
12765 displacement_string_end = temp_string;
12766
12767 i.types[this_operand].bitfield.baseindex = 1;
12768
12769 if (i.base_reg)
12770 {
12771 if (i.base_reg == &bad_reg)
12772 return 0;
12773 base_string = end_op;
12774 if (is_space_char (*base_string))
12775 ++base_string;
12776 }
12777
12778 /* There may be an index reg or scale factor here. */
12779 if (*base_string == ',')
12780 {
12781 ++base_string;
12782 if (is_space_char (*base_string))
12783 ++base_string;
12784
12785 if ((i.index_reg = parse_register (base_string, &end_op))
12786 != NULL)
12787 {
12788 if (i.index_reg == &bad_reg)
12789 return 0;
12790 base_string = end_op;
12791 if (is_space_char (*base_string))
12792 ++base_string;
12793 if (*base_string == ',')
12794 {
12795 ++base_string;
12796 if (is_space_char (*base_string))
12797 ++base_string;
12798 }
12799 else if (*base_string != ')')
12800 {
12801 as_bad (_("expecting `,' or `)' "
12802 "after index register in `%s'"),
12803 operand_string);
12804 return 0;
12805 }
12806 }
12807 else if (*base_string == REGISTER_PREFIX)
12808 {
12809 end_op = strchr (base_string, ',');
12810 if (end_op)
12811 *end_op = '\0';
12812 as_bad (_("bad register name `%s'"), base_string);
12813 return 0;
12814 }
12815
12816 /* Check for scale factor. */
12817 if (*base_string != ')')
12818 {
12819 char *end_scale = i386_scale (base_string);
12820
12821 if (!end_scale)
12822 return 0;
12823
12824 base_string = end_scale;
12825 if (is_space_char (*base_string))
12826 ++base_string;
12827 if (*base_string != ')')
12828 {
12829 as_bad (_("expecting `)' "
12830 "after scale factor in `%s'"),
12831 operand_string);
12832 return 0;
12833 }
12834 }
12835 else if (!i.index_reg)
12836 {
12837 as_bad (_("expecting index register or scale factor "
12838 "after `,'; got '%c'"),
12839 *base_string);
12840 return 0;
12841 }
12842 }
12843 else if (*base_string != ')')
12844 {
12845 as_bad (_("expecting `,' or `)' "
12846 "after base register in `%s'"),
12847 operand_string);
12848 return 0;
12849 }
12850 }
12851 else if (*base_string == REGISTER_PREFIX)
12852 {
12853 end_op = strchr (base_string, ',');
12854 if (end_op)
12855 *end_op = '\0';
12856 as_bad (_("bad register name `%s'"), base_string);
12857 return 0;
12858 }
12859 }
12860
12861 /* If there's an expression beginning the operand, parse it,
12862 assuming displacement_string_start and
12863 displacement_string_end are meaningful. */
12864 if (displacement_string_start != displacement_string_end)
12865 {
12866 if (!i386_displacement (displacement_string_start,
12867 displacement_string_end))
12868 return 0;
12869 }
12870
12871 /* Special case for (%dx) while doing input/output op. */
12872 if (i.base_reg
12873 && i.base_reg->reg_type.bitfield.instance == RegD
12874 && i.base_reg->reg_type.bitfield.word
12875 && i.index_reg == 0
12876 && i.log2_scale_factor == 0
12877 && i.seg[i.mem_operands] == 0
12878 && !operand_type_check (i.types[this_operand], disp))
12879 {
12880 i.types[this_operand] = i.base_reg->reg_type;
12881 i.input_output_operand = true;
12882 return 1;
12883 }
12884
12885 if (i386_index_check (operand_string) == 0)
12886 return 0;
12887 i.flags[this_operand] |= Operand_Mem;
12888 i.mem_operands++;
12889 }
12890 else
12891 {
12892 /* It's not a memory operand; argh! */
12893 as_bad (_("invalid char %s beginning operand %d `%s'"),
12894 output_invalid (*op_string),
12895 this_operand + 1,
12896 op_string);
12897 return 0;
12898 }
12899 return 1; /* Normal return. */
12900 }
12901 \f
12902 /* Calculate the maximum variable size (i.e., excluding fr_fix)
12903 that an rs_machine_dependent frag may reach. */
12904
12905 unsigned int
12906 i386_frag_max_var (fragS *frag)
12907 {
12908 /* The only relaxable frags are for jumps.
12909 Unconditional jumps can grow by 4 bytes and others by 5 bytes. */
12910 gas_assert (frag->fr_type == rs_machine_dependent);
12911 return TYPE_FROM_RELAX_STATE (frag->fr_subtype) == UNCOND_JUMP ? 4 : 5;
12912 }
12913
12914 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
12915 static int
12916 elf_symbol_resolved_in_segment_p (symbolS *fr_symbol, offsetT fr_var)
12917 {
12918 /* STT_GNU_IFUNC symbol must go through PLT. */
12919 if ((symbol_get_bfdsym (fr_symbol)->flags
12920 & BSF_GNU_INDIRECT_FUNCTION) != 0)
12921 return 0;
12922
12923 if (!S_IS_EXTERNAL (fr_symbol))
12924 /* Symbol may be weak or local. */
12925 return !S_IS_WEAK (fr_symbol);
12926
12927 /* Global symbols with non-default visibility can't be preempted. */
12928 if (ELF_ST_VISIBILITY (S_GET_OTHER (fr_symbol)) != STV_DEFAULT)
12929 return 1;
12930
12931 if (fr_var != NO_RELOC)
12932 switch ((enum bfd_reloc_code_real) fr_var)
12933 {
12934 case BFD_RELOC_386_PLT32:
12935 case BFD_RELOC_X86_64_PLT32:
12936 /* Symbol with PLT relocation may be preempted. */
12937 return 0;
12938 default:
12939 abort ();
12940 }
12941
12942 /* Global symbols with default visibility in a shared library may be
12943 preempted by another definition. */
12944 return !shared;
12945 }
12946 #endif
12947
12948 /* Table 3-2. Macro-Fusible Instructions in Haswell Microarchitecture
12949 Note also work for Skylake and Cascadelake.
12950 ---------------------------------------------------------------------
12951 | JCC | ADD/SUB/CMP | INC/DEC | TEST/AND |
12952 | ------ | ----------- | ------- | -------- |
12953 | Jo | N | N | Y |
12954 | Jno | N | N | Y |
12955 | Jc/Jb | Y | N | Y |
12956 | Jae/Jnb | Y | N | Y |
12957 | Je/Jz | Y | Y | Y |
12958 | Jne/Jnz | Y | Y | Y |
12959 | Jna/Jbe | Y | N | Y |
12960 | Ja/Jnbe | Y | N | Y |
12961 | Js | N | N | Y |
12962 | Jns | N | N | Y |
12963 | Jp/Jpe | N | N | Y |
12964 | Jnp/Jpo | N | N | Y |
12965 | Jl/Jnge | Y | Y | Y |
12966 | Jge/Jnl | Y | Y | Y |
12967 | Jle/Jng | Y | Y | Y |
12968 | Jg/Jnle | Y | Y | Y |
12969 --------------------------------------------------------------------- */
12970 static int
12971 i386_macro_fusible_p (enum mf_cmp_kind mf_cmp, enum mf_jcc_kind mf_jcc)
12972 {
12973 if (mf_cmp == mf_cmp_alu_cmp)
12974 return ((mf_jcc >= mf_jcc_jc && mf_jcc <= mf_jcc_jna)
12975 || mf_jcc == mf_jcc_jl || mf_jcc == mf_jcc_jle);
12976 if (mf_cmp == mf_cmp_incdec)
12977 return (mf_jcc == mf_jcc_je || mf_jcc == mf_jcc_jl
12978 || mf_jcc == mf_jcc_jle);
12979 if (mf_cmp == mf_cmp_test_and)
12980 return 1;
12981 return 0;
12982 }
12983
12984 /* Return the next non-empty frag. */
12985
12986 static fragS *
12987 i386_next_non_empty_frag (fragS *fragP)
12988 {
12989 /* There may be a frag with a ".fill 0" when there is no room in
12990 the current frag for frag_grow in output_insn. */
12991 for (fragP = fragP->fr_next;
12992 (fragP != NULL
12993 && fragP->fr_type == rs_fill
12994 && fragP->fr_fix == 0);
12995 fragP = fragP->fr_next)
12996 ;
12997 return fragP;
12998 }
12999
13000 /* Return the next jcc frag after BRANCH_PADDING. */
13001
13002 static fragS *
13003 i386_next_fusible_jcc_frag (fragS *maybe_cmp_fragP, fragS *pad_fragP)
13004 {
13005 fragS *branch_fragP;
13006 if (!pad_fragP)
13007 return NULL;
13008
13009 if (pad_fragP->fr_type == rs_machine_dependent
13010 && (TYPE_FROM_RELAX_STATE (pad_fragP->fr_subtype)
13011 == BRANCH_PADDING))
13012 {
13013 branch_fragP = i386_next_non_empty_frag (pad_fragP);
13014 if (branch_fragP->fr_type != rs_machine_dependent)
13015 return NULL;
13016 if (TYPE_FROM_RELAX_STATE (branch_fragP->fr_subtype) == COND_JUMP
13017 && i386_macro_fusible_p (maybe_cmp_fragP->tc_frag_data.mf_type,
13018 pad_fragP->tc_frag_data.mf_type))
13019 return branch_fragP;
13020 }
13021
13022 return NULL;
13023 }
13024
13025 /* Classify BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags. */
13026
13027 static void
13028 i386_classify_machine_dependent_frag (fragS *fragP)
13029 {
13030 fragS *cmp_fragP;
13031 fragS *pad_fragP;
13032 fragS *branch_fragP;
13033 fragS *next_fragP;
13034 unsigned int max_prefix_length;
13035
13036 if (fragP->tc_frag_data.classified)
13037 return;
13038
13039 /* First scan for BRANCH_PADDING and FUSED_JCC_PADDING. Convert
13040 FUSED_JCC_PADDING and merge BRANCH_PADDING. */
13041 for (next_fragP = fragP;
13042 next_fragP != NULL;
13043 next_fragP = next_fragP->fr_next)
13044 {
13045 next_fragP->tc_frag_data.classified = 1;
13046 if (next_fragP->fr_type == rs_machine_dependent)
13047 switch (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype))
13048 {
13049 case BRANCH_PADDING:
13050 /* The BRANCH_PADDING frag must be followed by a branch
13051 frag. */
13052 branch_fragP = i386_next_non_empty_frag (next_fragP);
13053 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
13054 break;
13055 case FUSED_JCC_PADDING:
13056 /* Check if this is a fused jcc:
13057 FUSED_JCC_PADDING
13058 CMP like instruction
13059 BRANCH_PADDING
13060 COND_JUMP
13061 */
13062 cmp_fragP = i386_next_non_empty_frag (next_fragP);
13063 pad_fragP = i386_next_non_empty_frag (cmp_fragP);
13064 branch_fragP = i386_next_fusible_jcc_frag (next_fragP, pad_fragP);
13065 if (branch_fragP)
13066 {
13067 /* The BRANCH_PADDING frag is merged with the
13068 FUSED_JCC_PADDING frag. */
13069 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
13070 /* CMP like instruction size. */
13071 next_fragP->tc_frag_data.cmp_size = cmp_fragP->fr_fix;
13072 frag_wane (pad_fragP);
13073 /* Skip to branch_fragP. */
13074 next_fragP = branch_fragP;
13075 }
13076 else if (next_fragP->tc_frag_data.max_prefix_length)
13077 {
13078 /* Turn FUSED_JCC_PADDING into BRANCH_PREFIX if it isn't
13079 a fused jcc. */
13080 next_fragP->fr_subtype
13081 = ENCODE_RELAX_STATE (BRANCH_PREFIX, 0);
13082 next_fragP->tc_frag_data.max_bytes
13083 = next_fragP->tc_frag_data.max_prefix_length;
13084 /* This will be updated in the BRANCH_PREFIX scan. */
13085 next_fragP->tc_frag_data.max_prefix_length = 0;
13086 }
13087 else
13088 frag_wane (next_fragP);
13089 break;
13090 }
13091 }
13092
13093 /* Stop if there is no BRANCH_PREFIX. */
13094 if (!align_branch_prefix_size)
13095 return;
13096
13097 /* Scan for BRANCH_PREFIX. */
13098 for (; fragP != NULL; fragP = fragP->fr_next)
13099 {
13100 if (fragP->fr_type != rs_machine_dependent
13101 || (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
13102 != BRANCH_PREFIX))
13103 continue;
13104
13105 /* Count all BRANCH_PREFIX frags before BRANCH_PADDING and
13106 COND_JUMP_PREFIX. */
13107 max_prefix_length = 0;
13108 for (next_fragP = fragP;
13109 next_fragP != NULL;
13110 next_fragP = next_fragP->fr_next)
13111 {
13112 if (next_fragP->fr_type == rs_fill)
13113 /* Skip rs_fill frags. */
13114 continue;
13115 else if (next_fragP->fr_type != rs_machine_dependent)
13116 /* Stop for all other frags. */
13117 break;
13118
13119 /* rs_machine_dependent frags. */
13120 if (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
13121 == BRANCH_PREFIX)
13122 {
13123 /* Count BRANCH_PREFIX frags. */
13124 if (max_prefix_length >= MAX_FUSED_JCC_PADDING_SIZE)
13125 {
13126 max_prefix_length = MAX_FUSED_JCC_PADDING_SIZE;
13127 frag_wane (next_fragP);
13128 }
13129 else
13130 max_prefix_length
13131 += next_fragP->tc_frag_data.max_bytes;
13132 }
13133 else if ((TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
13134 == BRANCH_PADDING)
13135 || (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
13136 == FUSED_JCC_PADDING))
13137 {
13138 /* Stop at BRANCH_PADDING and FUSED_JCC_PADDING. */
13139 fragP->tc_frag_data.u.padding_fragP = next_fragP;
13140 break;
13141 }
13142 else
13143 /* Stop for other rs_machine_dependent frags. */
13144 break;
13145 }
13146
13147 fragP->tc_frag_data.max_prefix_length = max_prefix_length;
13148
13149 /* Skip to the next frag. */
13150 fragP = next_fragP;
13151 }
13152 }
13153
13154 /* Compute padding size for
13155
13156 FUSED_JCC_PADDING
13157 CMP like instruction
13158 BRANCH_PADDING
13159 COND_JUMP/UNCOND_JUMP
13160
13161 or
13162
13163 BRANCH_PADDING
13164 COND_JUMP/UNCOND_JUMP
13165 */
13166
13167 static int
13168 i386_branch_padding_size (fragS *fragP, offsetT address)
13169 {
13170 unsigned int offset, size, padding_size;
13171 fragS *branch_fragP = fragP->tc_frag_data.u.branch_fragP;
13172
13173 /* The start address of the BRANCH_PADDING or FUSED_JCC_PADDING frag. */
13174 if (!address)
13175 address = fragP->fr_address;
13176 address += fragP->fr_fix;
13177
13178 /* CMP like instrunction size. */
13179 size = fragP->tc_frag_data.cmp_size;
13180
13181 /* The base size of the branch frag. */
13182 size += branch_fragP->fr_fix;
13183
13184 /* Add opcode and displacement bytes for the rs_machine_dependent
13185 branch frag. */
13186 if (branch_fragP->fr_type == rs_machine_dependent)
13187 size += md_relax_table[branch_fragP->fr_subtype].rlx_length;
13188
13189 /* Check if branch is within boundary and doesn't end at the last
13190 byte. */
13191 offset = address & ((1U << align_branch_power) - 1);
13192 if ((offset + size) >= (1U << align_branch_power))
13193 /* Padding needed to avoid crossing boundary. */
13194 padding_size = (1U << align_branch_power) - offset;
13195 else
13196 /* No padding needed. */
13197 padding_size = 0;
13198
13199 /* The return value may be saved in tc_frag_data.length which is
13200 unsigned byte. */
13201 if (!fits_in_unsigned_byte (padding_size))
13202 abort ();
13203
13204 return padding_size;
13205 }
13206
13207 /* i386_generic_table_relax_frag()
13208
13209 Handle BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags to
13210 grow/shrink padding to align branch frags. Hand others to
13211 relax_frag(). */
13212
13213 long
13214 i386_generic_table_relax_frag (segT segment, fragS *fragP, long stretch)
13215 {
13216 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
13217 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
13218 {
13219 long padding_size = i386_branch_padding_size (fragP, 0);
13220 long grow = padding_size - fragP->tc_frag_data.length;
13221
13222 /* When the BRANCH_PREFIX frag is used, the computed address
13223 must match the actual address and there should be no padding. */
13224 if (fragP->tc_frag_data.padding_address
13225 && (fragP->tc_frag_data.padding_address != fragP->fr_address
13226 || padding_size))
13227 abort ();
13228
13229 /* Update the padding size. */
13230 if (grow)
13231 fragP->tc_frag_data.length = padding_size;
13232
13233 return grow;
13234 }
13235 else if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
13236 {
13237 fragS *padding_fragP, *next_fragP;
13238 long padding_size, left_size, last_size;
13239
13240 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
13241 if (!padding_fragP)
13242 /* Use the padding set by the leading BRANCH_PREFIX frag. */
13243 return (fragP->tc_frag_data.length
13244 - fragP->tc_frag_data.last_length);
13245
13246 /* Compute the relative address of the padding frag in the very
13247 first time where the BRANCH_PREFIX frag sizes are zero. */
13248 if (!fragP->tc_frag_data.padding_address)
13249 fragP->tc_frag_data.padding_address
13250 = padding_fragP->fr_address - (fragP->fr_address - stretch);
13251
13252 /* First update the last length from the previous interation. */
13253 left_size = fragP->tc_frag_data.prefix_length;
13254 for (next_fragP = fragP;
13255 next_fragP != padding_fragP;
13256 next_fragP = next_fragP->fr_next)
13257 if (next_fragP->fr_type == rs_machine_dependent
13258 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
13259 == BRANCH_PREFIX))
13260 {
13261 if (left_size)
13262 {
13263 int max = next_fragP->tc_frag_data.max_bytes;
13264 if (max)
13265 {
13266 int size;
13267 if (max > left_size)
13268 size = left_size;
13269 else
13270 size = max;
13271 left_size -= size;
13272 next_fragP->tc_frag_data.last_length = size;
13273 }
13274 }
13275 else
13276 next_fragP->tc_frag_data.last_length = 0;
13277 }
13278
13279 /* Check the padding size for the padding frag. */
13280 padding_size = i386_branch_padding_size
13281 (padding_fragP, (fragP->fr_address
13282 + fragP->tc_frag_data.padding_address));
13283
13284 last_size = fragP->tc_frag_data.prefix_length;
13285 /* Check if there is change from the last interation. */
13286 if (padding_size == last_size)
13287 {
13288 /* Update the expected address of the padding frag. */
13289 padding_fragP->tc_frag_data.padding_address
13290 = (fragP->fr_address + padding_size
13291 + fragP->tc_frag_data.padding_address);
13292 return 0;
13293 }
13294
13295 if (padding_size > fragP->tc_frag_data.max_prefix_length)
13296 {
13297 /* No padding if there is no sufficient room. Clear the
13298 expected address of the padding frag. */
13299 padding_fragP->tc_frag_data.padding_address = 0;
13300 padding_size = 0;
13301 }
13302 else
13303 /* Store the expected address of the padding frag. */
13304 padding_fragP->tc_frag_data.padding_address
13305 = (fragP->fr_address + padding_size
13306 + fragP->tc_frag_data.padding_address);
13307
13308 fragP->tc_frag_data.prefix_length = padding_size;
13309
13310 /* Update the length for the current interation. */
13311 left_size = padding_size;
13312 for (next_fragP = fragP;
13313 next_fragP != padding_fragP;
13314 next_fragP = next_fragP->fr_next)
13315 if (next_fragP->fr_type == rs_machine_dependent
13316 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
13317 == BRANCH_PREFIX))
13318 {
13319 if (left_size)
13320 {
13321 int max = next_fragP->tc_frag_data.max_bytes;
13322 if (max)
13323 {
13324 int size;
13325 if (max > left_size)
13326 size = left_size;
13327 else
13328 size = max;
13329 left_size -= size;
13330 next_fragP->tc_frag_data.length = size;
13331 }
13332 }
13333 else
13334 next_fragP->tc_frag_data.length = 0;
13335 }
13336
13337 return (fragP->tc_frag_data.length
13338 - fragP->tc_frag_data.last_length);
13339 }
13340 return relax_frag (segment, fragP, stretch);
13341 }
13342
13343 /* md_estimate_size_before_relax()
13344
13345 Called just before relax() for rs_machine_dependent frags. The x86
13346 assembler uses these frags to handle variable size jump
13347 instructions.
13348
13349 Any symbol that is now undefined will not become defined.
13350 Return the correct fr_subtype in the frag.
13351 Return the initial "guess for variable size of frag" to caller.
13352 The guess is actually the growth beyond the fixed part. Whatever
13353 we do to grow the fixed or variable part contributes to our
13354 returned value. */
13355
13356 int
13357 md_estimate_size_before_relax (fragS *fragP, segT segment)
13358 {
13359 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
13360 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX
13361 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
13362 {
13363 i386_classify_machine_dependent_frag (fragP);
13364 return fragP->tc_frag_data.length;
13365 }
13366
13367 /* We've already got fragP->fr_subtype right; all we have to do is
13368 check for un-relaxable symbols. On an ELF system, we can't relax
13369 an externally visible symbol, because it may be overridden by a
13370 shared library. */
13371 if (S_GET_SEGMENT (fragP->fr_symbol) != segment
13372 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13373 || (IS_ELF
13374 && !elf_symbol_resolved_in_segment_p (fragP->fr_symbol,
13375 fragP->fr_var))
13376 #endif
13377 #if defined (OBJ_COFF) && defined (TE_PE)
13378 || (OUTPUT_FLAVOR == bfd_target_coff_flavour
13379 && S_IS_WEAK (fragP->fr_symbol))
13380 #endif
13381 )
13382 {
13383 /* Symbol is undefined in this segment, or we need to keep a
13384 reloc so that weak symbols can be overridden. */
13385 int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
13386 enum bfd_reloc_code_real reloc_type;
13387 unsigned char *opcode;
13388 int old_fr_fix;
13389 fixS *fixP = NULL;
13390
13391 if (fragP->fr_var != NO_RELOC)
13392 reloc_type = (enum bfd_reloc_code_real) fragP->fr_var;
13393 else if (size == 2)
13394 reloc_type = BFD_RELOC_16_PCREL;
13395 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13396 else if (fragP->tc_frag_data.code64 && fragP->fr_offset == 0
13397 && need_plt32_p (fragP->fr_symbol))
13398 reloc_type = BFD_RELOC_X86_64_PLT32;
13399 #endif
13400 else
13401 reloc_type = BFD_RELOC_32_PCREL;
13402
13403 old_fr_fix = fragP->fr_fix;
13404 opcode = (unsigned char *) fragP->fr_opcode;
13405
13406 switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
13407 {
13408 case UNCOND_JUMP:
13409 /* Make jmp (0xeb) a (d)word displacement jump. */
13410 opcode[0] = 0xe9;
13411 fragP->fr_fix += size;
13412 fixP = fix_new (fragP, old_fr_fix, size,
13413 fragP->fr_symbol,
13414 fragP->fr_offset, 1,
13415 reloc_type);
13416 break;
13417
13418 case COND_JUMP86:
13419 if (size == 2
13420 && (!no_cond_jump_promotion || fragP->fr_var != NO_RELOC))
13421 {
13422 /* Negate the condition, and branch past an
13423 unconditional jump. */
13424 opcode[0] ^= 1;
13425 opcode[1] = 3;
13426 /* Insert an unconditional jump. */
13427 opcode[2] = 0xe9;
13428 /* We added two extra opcode bytes, and have a two byte
13429 offset. */
13430 fragP->fr_fix += 2 + 2;
13431 fix_new (fragP, old_fr_fix + 2, 2,
13432 fragP->fr_symbol,
13433 fragP->fr_offset, 1,
13434 reloc_type);
13435 break;
13436 }
13437 /* Fall through. */
13438
13439 case COND_JUMP:
13440 if (no_cond_jump_promotion && fragP->fr_var == NO_RELOC)
13441 {
13442 fragP->fr_fix += 1;
13443 fixP = fix_new (fragP, old_fr_fix, 1,
13444 fragP->fr_symbol,
13445 fragP->fr_offset, 1,
13446 BFD_RELOC_8_PCREL);
13447 fixP->fx_signed = 1;
13448 break;
13449 }
13450
13451 /* This changes the byte-displacement jump 0x7N
13452 to the (d)word-displacement jump 0x0f,0x8N. */
13453 opcode[1] = opcode[0] + 0x10;
13454 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
13455 /* We've added an opcode byte. */
13456 fragP->fr_fix += 1 + size;
13457 fixP = fix_new (fragP, old_fr_fix + 1, size,
13458 fragP->fr_symbol,
13459 fragP->fr_offset, 1,
13460 reloc_type);
13461 break;
13462
13463 default:
13464 BAD_CASE (fragP->fr_subtype);
13465 break;
13466 }
13467
13468 /* All jumps handled here are signed, but don't unconditionally use a
13469 signed limit check for 32 and 16 bit jumps as we want to allow wrap
13470 around at 4G (outside of 64-bit mode) and 64k. */
13471 if (size == 4 && flag_code == CODE_64BIT)
13472 fixP->fx_signed = 1;
13473
13474 frag_wane (fragP);
13475 return fragP->fr_fix - old_fr_fix;
13476 }
13477
13478 /* Guess size depending on current relax state. Initially the relax
13479 state will correspond to a short jump and we return 1, because
13480 the variable part of the frag (the branch offset) is one byte
13481 long. However, we can relax a section more than once and in that
13482 case we must either set fr_subtype back to the unrelaxed state,
13483 or return the value for the appropriate branch. */
13484 return md_relax_table[fragP->fr_subtype].rlx_length;
13485 }
13486
13487 /* Called after relax() is finished.
13488
13489 In: Address of frag.
13490 fr_type == rs_machine_dependent.
13491 fr_subtype is what the address relaxed to.
13492
13493 Out: Any fixSs and constants are set up.
13494 Caller will turn frag into a ".space 0". */
13495
13496 void
13497 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec ATTRIBUTE_UNUSED,
13498 fragS *fragP)
13499 {
13500 unsigned char *opcode;
13501 unsigned char *where_to_put_displacement = NULL;
13502 offsetT target_address;
13503 offsetT opcode_address;
13504 unsigned int extension = 0;
13505 offsetT displacement_from_opcode_start;
13506
13507 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
13508 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING
13509 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
13510 {
13511 /* Generate nop padding. */
13512 unsigned int size = fragP->tc_frag_data.length;
13513 if (size)
13514 {
13515 if (size > fragP->tc_frag_data.max_bytes)
13516 abort ();
13517
13518 if (flag_debug)
13519 {
13520 const char *msg;
13521 const char *branch = "branch";
13522 const char *prefix = "";
13523 fragS *padding_fragP;
13524 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
13525 == BRANCH_PREFIX)
13526 {
13527 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
13528 switch (fragP->tc_frag_data.default_prefix)
13529 {
13530 default:
13531 abort ();
13532 break;
13533 case CS_PREFIX_OPCODE:
13534 prefix = " cs";
13535 break;
13536 case DS_PREFIX_OPCODE:
13537 prefix = " ds";
13538 break;
13539 case ES_PREFIX_OPCODE:
13540 prefix = " es";
13541 break;
13542 case FS_PREFIX_OPCODE:
13543 prefix = " fs";
13544 break;
13545 case GS_PREFIX_OPCODE:
13546 prefix = " gs";
13547 break;
13548 case SS_PREFIX_OPCODE:
13549 prefix = " ss";
13550 break;
13551 }
13552 if (padding_fragP)
13553 msg = _("%s:%u: add %d%s at 0x%llx to align "
13554 "%s within %d-byte boundary\n");
13555 else
13556 msg = _("%s:%u: add additional %d%s at 0x%llx to "
13557 "align %s within %d-byte boundary\n");
13558 }
13559 else
13560 {
13561 padding_fragP = fragP;
13562 msg = _("%s:%u: add %d%s-byte nop at 0x%llx to align "
13563 "%s within %d-byte boundary\n");
13564 }
13565
13566 if (padding_fragP)
13567 switch (padding_fragP->tc_frag_data.branch_type)
13568 {
13569 case align_branch_jcc:
13570 branch = "jcc";
13571 break;
13572 case align_branch_fused:
13573 branch = "fused jcc";
13574 break;
13575 case align_branch_jmp:
13576 branch = "jmp";
13577 break;
13578 case align_branch_call:
13579 branch = "call";
13580 break;
13581 case align_branch_indirect:
13582 branch = "indiret branch";
13583 break;
13584 case align_branch_ret:
13585 branch = "ret";
13586 break;
13587 default:
13588 break;
13589 }
13590
13591 fprintf (stdout, msg,
13592 fragP->fr_file, fragP->fr_line, size, prefix,
13593 (long long) fragP->fr_address, branch,
13594 1 << align_branch_power);
13595 }
13596 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
13597 memset (fragP->fr_opcode,
13598 fragP->tc_frag_data.default_prefix, size);
13599 else
13600 i386_generate_nops (fragP, (char *) fragP->fr_opcode,
13601 size, 0);
13602 fragP->fr_fix += size;
13603 }
13604 return;
13605 }
13606
13607 opcode = (unsigned char *) fragP->fr_opcode;
13608
13609 /* Address we want to reach in file space. */
13610 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
13611
13612 /* Address opcode resides at in file space. */
13613 opcode_address = fragP->fr_address + fragP->fr_fix;
13614
13615 /* Displacement from opcode start to fill into instruction. */
13616 displacement_from_opcode_start = target_address - opcode_address;
13617
13618 if ((fragP->fr_subtype & BIG) == 0)
13619 {
13620 /* Don't have to change opcode. */
13621 extension = 1; /* 1 opcode + 1 displacement */
13622 where_to_put_displacement = &opcode[1];
13623 }
13624 else
13625 {
13626 if (no_cond_jump_promotion
13627 && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
13628 as_warn_where (fragP->fr_file, fragP->fr_line,
13629 _("long jump required"));
13630
13631 switch (fragP->fr_subtype)
13632 {
13633 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
13634 extension = 4; /* 1 opcode + 4 displacement */
13635 opcode[0] = 0xe9;
13636 where_to_put_displacement = &opcode[1];
13637 break;
13638
13639 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
13640 extension = 2; /* 1 opcode + 2 displacement */
13641 opcode[0] = 0xe9;
13642 where_to_put_displacement = &opcode[1];
13643 break;
13644
13645 case ENCODE_RELAX_STATE (COND_JUMP, BIG):
13646 case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
13647 extension = 5; /* 2 opcode + 4 displacement */
13648 opcode[1] = opcode[0] + 0x10;
13649 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
13650 where_to_put_displacement = &opcode[2];
13651 break;
13652
13653 case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
13654 extension = 3; /* 2 opcode + 2 displacement */
13655 opcode[1] = opcode[0] + 0x10;
13656 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
13657 where_to_put_displacement = &opcode[2];
13658 break;
13659
13660 case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
13661 extension = 4;
13662 opcode[0] ^= 1;
13663 opcode[1] = 3;
13664 opcode[2] = 0xe9;
13665 where_to_put_displacement = &opcode[3];
13666 break;
13667
13668 default:
13669 BAD_CASE (fragP->fr_subtype);
13670 break;
13671 }
13672 }
13673
13674 /* If size if less then four we are sure that the operand fits,
13675 but if it's 4, then it could be that the displacement is larger
13676 then -/+ 2GB. */
13677 if (DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype) == 4
13678 && object_64bit
13679 && ((addressT) (displacement_from_opcode_start - extension
13680 + ((addressT) 1 << 31))
13681 > (((addressT) 2 << 31) - 1)))
13682 {
13683 as_bad_where (fragP->fr_file, fragP->fr_line,
13684 _("jump target out of range"));
13685 /* Make us emit 0. */
13686 displacement_from_opcode_start = extension;
13687 }
13688 /* Now put displacement after opcode. */
13689 md_number_to_chars ((char *) where_to_put_displacement,
13690 (valueT) (displacement_from_opcode_start - extension),
13691 DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
13692 fragP->fr_fix += extension;
13693 }
13694 \f
13695 /* Apply a fixup (fixP) to segment data, once it has been determined
13696 by our caller that we have all the info we need to fix it up.
13697
13698 Parameter valP is the pointer to the value of the bits.
13699
13700 On the 386, immediates, displacements, and data pointers are all in
13701 the same (little-endian) format, so we don't need to care about which
13702 we are handling. */
13703
13704 void
13705 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
13706 {
13707 char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
13708 valueT value = *valP;
13709
13710 #if !defined (TE_Mach)
13711 if (fixP->fx_pcrel)
13712 {
13713 switch (fixP->fx_r_type)
13714 {
13715 default:
13716 break;
13717
13718 case BFD_RELOC_64:
13719 fixP->fx_r_type = BFD_RELOC_64_PCREL;
13720 break;
13721 case BFD_RELOC_32:
13722 case BFD_RELOC_X86_64_32S:
13723 fixP->fx_r_type = BFD_RELOC_32_PCREL;
13724 break;
13725 case BFD_RELOC_16:
13726 fixP->fx_r_type = BFD_RELOC_16_PCREL;
13727 break;
13728 case BFD_RELOC_8:
13729 fixP->fx_r_type = BFD_RELOC_8_PCREL;
13730 break;
13731 }
13732 }
13733
13734 if (fixP->fx_addsy != NULL
13735 && (fixP->fx_r_type == BFD_RELOC_32_PCREL
13736 || fixP->fx_r_type == BFD_RELOC_64_PCREL
13737 || fixP->fx_r_type == BFD_RELOC_16_PCREL
13738 || fixP->fx_r_type == BFD_RELOC_8_PCREL)
13739 && !use_rela_relocations)
13740 {
13741 /* This is a hack. There should be a better way to handle this.
13742 This covers for the fact that bfd_install_relocation will
13743 subtract the current location (for partial_inplace, PC relative
13744 relocations); see more below. */
13745 #ifndef OBJ_AOUT
13746 if (IS_ELF
13747 #ifdef TE_PE
13748 || OUTPUT_FLAVOR == bfd_target_coff_flavour
13749 #endif
13750 )
13751 value += fixP->fx_where + fixP->fx_frag->fr_address;
13752 #endif
13753 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13754 if (IS_ELF)
13755 {
13756 segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
13757
13758 if ((sym_seg == seg
13759 || (symbol_section_p (fixP->fx_addsy)
13760 && sym_seg != absolute_section))
13761 && !generic_force_reloc (fixP))
13762 {
13763 /* Yes, we add the values in twice. This is because
13764 bfd_install_relocation subtracts them out again. I think
13765 bfd_install_relocation is broken, but I don't dare change
13766 it. FIXME. */
13767 value += fixP->fx_where + fixP->fx_frag->fr_address;
13768 }
13769 }
13770 #endif
13771 #if defined (OBJ_COFF) && defined (TE_PE)
13772 /* For some reason, the PE format does not store a
13773 section address offset for a PC relative symbol. */
13774 if (S_GET_SEGMENT (fixP->fx_addsy) != seg
13775 || S_IS_WEAK (fixP->fx_addsy))
13776 value += md_pcrel_from (fixP);
13777 #endif
13778 }
13779 #if defined (OBJ_COFF) && defined (TE_PE)
13780 if (fixP->fx_addsy != NULL
13781 && S_IS_WEAK (fixP->fx_addsy)
13782 /* PR 16858: Do not modify weak function references. */
13783 && ! fixP->fx_pcrel)
13784 {
13785 #if !defined (TE_PEP)
13786 /* For x86 PE weak function symbols are neither PC-relative
13787 nor do they set S_IS_FUNCTION. So the only reliable way
13788 to detect them is to check the flags of their containing
13789 section. */
13790 if (S_GET_SEGMENT (fixP->fx_addsy) != NULL
13791 && S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_CODE)
13792 ;
13793 else
13794 #endif
13795 value -= S_GET_VALUE (fixP->fx_addsy);
13796 }
13797 #endif
13798
13799 /* Fix a few things - the dynamic linker expects certain values here,
13800 and we must not disappoint it. */
13801 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13802 if (IS_ELF && fixP->fx_addsy)
13803 switch (fixP->fx_r_type)
13804 {
13805 case BFD_RELOC_386_PLT32:
13806 case BFD_RELOC_X86_64_PLT32:
13807 /* Make the jump instruction point to the address of the operand.
13808 At runtime we merely add the offset to the actual PLT entry.
13809 NB: Subtract the offset size only for jump instructions. */
13810 if (fixP->fx_pcrel)
13811 value = -4;
13812 break;
13813
13814 case BFD_RELOC_386_TLS_GD:
13815 case BFD_RELOC_386_TLS_LDM:
13816 case BFD_RELOC_386_TLS_IE_32:
13817 case BFD_RELOC_386_TLS_IE:
13818 case BFD_RELOC_386_TLS_GOTIE:
13819 case BFD_RELOC_386_TLS_GOTDESC:
13820 case BFD_RELOC_X86_64_TLSGD:
13821 case BFD_RELOC_X86_64_TLSLD:
13822 case BFD_RELOC_X86_64_GOTTPOFF:
13823 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
13824 value = 0; /* Fully resolved at runtime. No addend. */
13825 /* Fallthrough */
13826 case BFD_RELOC_386_TLS_LE:
13827 case BFD_RELOC_386_TLS_LDO_32:
13828 case BFD_RELOC_386_TLS_LE_32:
13829 case BFD_RELOC_X86_64_DTPOFF32:
13830 case BFD_RELOC_X86_64_DTPOFF64:
13831 case BFD_RELOC_X86_64_TPOFF32:
13832 case BFD_RELOC_X86_64_TPOFF64:
13833 S_SET_THREAD_LOCAL (fixP->fx_addsy);
13834 break;
13835
13836 case BFD_RELOC_386_TLS_DESC_CALL:
13837 case BFD_RELOC_X86_64_TLSDESC_CALL:
13838 value = 0; /* Fully resolved at runtime. No addend. */
13839 S_SET_THREAD_LOCAL (fixP->fx_addsy);
13840 fixP->fx_done = 0;
13841 return;
13842
13843 case BFD_RELOC_VTABLE_INHERIT:
13844 case BFD_RELOC_VTABLE_ENTRY:
13845 fixP->fx_done = 0;
13846 return;
13847
13848 default:
13849 break;
13850 }
13851 #endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) */
13852
13853 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
13854 if (!object_64bit)
13855 value = extend_to_32bit_address (value);
13856
13857 *valP = value;
13858 #endif /* !defined (TE_Mach) */
13859
13860 /* Are we finished with this relocation now? */
13861 if (fixP->fx_addsy == NULL)
13862 {
13863 fixP->fx_done = 1;
13864 switch (fixP->fx_r_type)
13865 {
13866 case BFD_RELOC_X86_64_32S:
13867 fixP->fx_signed = 1;
13868 break;
13869
13870 default:
13871 break;
13872 }
13873 }
13874 #if defined (OBJ_COFF) && defined (TE_PE)
13875 else if (fixP->fx_addsy != NULL && S_IS_WEAK (fixP->fx_addsy))
13876 {
13877 fixP->fx_done = 0;
13878 /* Remember value for tc_gen_reloc. */
13879 fixP->fx_addnumber = value;
13880 /* Clear out the frag for now. */
13881 value = 0;
13882 }
13883 #endif
13884 else if (use_rela_relocations)
13885 {
13886 if (!disallow_64bit_reloc || fixP->fx_r_type == NO_RELOC)
13887 fixP->fx_no_overflow = 1;
13888 /* Remember value for tc_gen_reloc. */
13889 fixP->fx_addnumber = value;
13890 value = 0;
13891 }
13892
13893 md_number_to_chars (p, value, fixP->fx_size);
13894 }
13895 \f
13896 const char *
13897 md_atof (int type, char *litP, int *sizeP)
13898 {
13899 /* This outputs the LITTLENUMs in REVERSE order;
13900 in accord with the bigendian 386. */
13901 return ieee_md_atof (type, litP, sizeP, false);
13902 }
13903 \f
13904 static char output_invalid_buf[sizeof (unsigned char) * 2 + 6];
13905
13906 static char *
13907 output_invalid (int c)
13908 {
13909 if (ISPRINT (c))
13910 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
13911 "'%c'", c);
13912 else
13913 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
13914 "(0x%x)", (unsigned char) c);
13915 return output_invalid_buf;
13916 }
13917
13918 /* Verify that @r can be used in the current context. */
13919
13920 static bool check_register (const reg_entry *r)
13921 {
13922 if (allow_pseudo_reg)
13923 return true;
13924
13925 if (operand_type_all_zero (&r->reg_type))
13926 return false;
13927
13928 if ((r->reg_type.bitfield.dword
13929 || (r->reg_type.bitfield.class == SReg && r->reg_num > 3)
13930 || r->reg_type.bitfield.class == RegCR
13931 || r->reg_type.bitfield.class == RegDR)
13932 && !cpu_arch_flags.bitfield.cpui386)
13933 return false;
13934
13935 if (r->reg_type.bitfield.class == RegTR
13936 && (flag_code == CODE_64BIT
13937 || !cpu_arch_flags.bitfield.cpui386
13938 || cpu_arch_isa_flags.bitfield.cpui586
13939 || cpu_arch_isa_flags.bitfield.cpui686))
13940 return false;
13941
13942 if (r->reg_type.bitfield.class == RegMMX && !cpu_arch_flags.bitfield.cpummx)
13943 return false;
13944
13945 if (!cpu_arch_flags.bitfield.cpuavx512f)
13946 {
13947 if (r->reg_type.bitfield.zmmword
13948 || r->reg_type.bitfield.class == RegMask)
13949 return false;
13950
13951 if (!cpu_arch_flags.bitfield.cpuavx)
13952 {
13953 if (r->reg_type.bitfield.ymmword)
13954 return false;
13955
13956 if (!cpu_arch_flags.bitfield.cpusse && r->reg_type.bitfield.xmmword)
13957 return false;
13958 }
13959 }
13960
13961 if (vector_size < VSZ512 && r->reg_type.bitfield.zmmword)
13962 return false;
13963
13964 if (vector_size < VSZ256 && r->reg_type.bitfield.ymmword)
13965 return false;
13966
13967 if (r->reg_type.bitfield.tmmword
13968 && (!cpu_arch_flags.bitfield.cpuamx_tile
13969 || flag_code != CODE_64BIT))
13970 return false;
13971
13972 if (r->reg_type.bitfield.class == RegBND && !cpu_arch_flags.bitfield.cpumpx)
13973 return false;
13974
13975 /* Don't allow fake index register unless allow_index_reg isn't 0. */
13976 if (!allow_index_reg && r->reg_num == RegIZ)
13977 return false;
13978
13979 /* Upper 16 vector registers are only available with VREX in 64bit
13980 mode, and require EVEX encoding. */
13981 if (r->reg_flags & RegVRex)
13982 {
13983 if (!cpu_arch_flags.bitfield.cpuavx512f
13984 || flag_code != CODE_64BIT)
13985 return false;
13986
13987 if (i.vec_encoding == vex_encoding_default)
13988 i.vec_encoding = vex_encoding_evex;
13989 else if (i.vec_encoding != vex_encoding_evex)
13990 i.vec_encoding = vex_encoding_error;
13991 }
13992
13993 if (((r->reg_flags & (RegRex64 | RegRex)) || r->reg_type.bitfield.qword)
13994 && (!cpu_arch_flags.bitfield.cpu64
13995 || r->reg_type.bitfield.class != RegCR
13996 || dot_insn ())
13997 && flag_code != CODE_64BIT)
13998 return false;
13999
14000 if (r->reg_type.bitfield.class == SReg && r->reg_num == RegFlat
14001 && !intel_syntax)
14002 return false;
14003
14004 return true;
14005 }
14006
14007 /* REG_STRING starts *before* REGISTER_PREFIX. */
14008
14009 static const reg_entry *
14010 parse_real_register (const char *reg_string, char **end_op)
14011 {
14012 const char *s = reg_string;
14013 char *p;
14014 char reg_name_given[MAX_REG_NAME_SIZE + 1];
14015 const reg_entry *r;
14016
14017 /* Skip possible REGISTER_PREFIX and possible whitespace. */
14018 if (*s == REGISTER_PREFIX)
14019 ++s;
14020
14021 if (is_space_char (*s))
14022 ++s;
14023
14024 p = reg_name_given;
14025 while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
14026 {
14027 if (p >= reg_name_given + MAX_REG_NAME_SIZE)
14028 return (const reg_entry *) NULL;
14029 s++;
14030 }
14031
14032 if (is_part_of_name (*s))
14033 return (const reg_entry *) NULL;
14034
14035 *end_op = (char *) s;
14036
14037 r = (const reg_entry *) str_hash_find (reg_hash, reg_name_given);
14038
14039 /* Handle floating point regs, allowing spaces in the (i) part. */
14040 if (r == reg_st0)
14041 {
14042 if (!cpu_arch_flags.bitfield.cpu8087
14043 && !cpu_arch_flags.bitfield.cpu287
14044 && !cpu_arch_flags.bitfield.cpu387
14045 && !allow_pseudo_reg)
14046 return (const reg_entry *) NULL;
14047
14048 if (is_space_char (*s))
14049 ++s;
14050 if (*s == '(')
14051 {
14052 ++s;
14053 if (is_space_char (*s))
14054 ++s;
14055 if (*s >= '0' && *s <= '7')
14056 {
14057 int fpr = *s - '0';
14058 ++s;
14059 if (is_space_char (*s))
14060 ++s;
14061 if (*s == ')')
14062 {
14063 *end_op = (char *) s + 1;
14064 know (r[fpr].reg_num == fpr);
14065 return r + fpr;
14066 }
14067 }
14068 /* We have "%st(" then garbage. */
14069 return (const reg_entry *) NULL;
14070 }
14071 }
14072
14073 return r && check_register (r) ? r : NULL;
14074 }
14075
14076 /* REG_STRING starts *before* REGISTER_PREFIX. */
14077
14078 static const reg_entry *
14079 parse_register (const char *reg_string, char **end_op)
14080 {
14081 const reg_entry *r;
14082
14083 if (*reg_string == REGISTER_PREFIX || allow_naked_reg)
14084 r = parse_real_register (reg_string, end_op);
14085 else
14086 r = NULL;
14087 if (!r)
14088 {
14089 char *save = input_line_pointer;
14090 char *buf = xstrdup (reg_string), *name;
14091 symbolS *symbolP;
14092
14093 input_line_pointer = buf;
14094 get_symbol_name (&name);
14095 symbolP = symbol_find (name);
14096 while (symbolP && symbol_equated_p (symbolP))
14097 {
14098 const expressionS *e = symbol_get_value_expression(symbolP);
14099
14100 if (e->X_add_number)
14101 break;
14102 symbolP = e->X_add_symbol;
14103 }
14104 if (symbolP && S_GET_SEGMENT (symbolP) == reg_section)
14105 {
14106 const expressionS *e = symbol_get_value_expression (symbolP);
14107
14108 if (e->X_op == O_register)
14109 {
14110 know (e->X_add_number >= 0
14111 && (valueT) e->X_add_number < i386_regtab_size);
14112 r = i386_regtab + e->X_add_number;
14113 *end_op = (char *) reg_string + (input_line_pointer - buf);
14114 }
14115 if (r && !check_register (r))
14116 {
14117 as_bad (_("register '%s%s' cannot be used here"),
14118 register_prefix, r->reg_name);
14119 r = &bad_reg;
14120 }
14121 }
14122 input_line_pointer = save;
14123 free (buf);
14124 }
14125 return r;
14126 }
14127
14128 int
14129 i386_parse_name (char *name, expressionS *e, char *nextcharP)
14130 {
14131 const reg_entry *r = NULL;
14132 char *end = input_line_pointer;
14133
14134 /* We only know the terminating character here. It being double quote could
14135 be the closing one of a quoted symbol name, or an opening one from a
14136 following string (or another quoted symbol name). Since the latter can't
14137 be valid syntax for anything, bailing in either case is good enough. */
14138 if (*nextcharP == '"')
14139 return 0;
14140
14141 *end = *nextcharP;
14142 if (*name == REGISTER_PREFIX || allow_naked_reg)
14143 r = parse_real_register (name, &input_line_pointer);
14144 if (r && end <= input_line_pointer)
14145 {
14146 *nextcharP = *input_line_pointer;
14147 *input_line_pointer = 0;
14148 e->X_op = O_register;
14149 e->X_add_number = r - i386_regtab;
14150 return 1;
14151 }
14152 input_line_pointer = end;
14153 *end = 0;
14154 return intel_syntax ? i386_intel_parse_name (name, e) : 0;
14155 }
14156
14157 void
14158 md_operand (expressionS *e)
14159 {
14160 char *end;
14161 const reg_entry *r;
14162
14163 switch (*input_line_pointer)
14164 {
14165 case REGISTER_PREFIX:
14166 r = parse_real_register (input_line_pointer, &end);
14167 if (r)
14168 {
14169 e->X_op = O_register;
14170 e->X_add_number = r - i386_regtab;
14171 input_line_pointer = end;
14172 }
14173 break;
14174
14175 case '[':
14176 gas_assert (intel_syntax);
14177 end = input_line_pointer++;
14178 expression (e);
14179 if (*input_line_pointer == ']')
14180 {
14181 ++input_line_pointer;
14182 e->X_op_symbol = make_expr_symbol (e);
14183 e->X_add_symbol = NULL;
14184 e->X_add_number = 0;
14185 e->X_op = O_index;
14186 }
14187 else
14188 {
14189 e->X_op = O_absent;
14190 input_line_pointer = end;
14191 }
14192 break;
14193 }
14194 }
14195
14196 #ifdef BFD64
14197 /* To maintain consistency with !BFD64 builds of gas record, whether any
14198 (binary) operator was involved in an expression. As expressions are
14199 evaluated in only 32 bits when !BFD64, we use this to decide whether to
14200 truncate results. */
14201 bool i386_record_operator (operatorT op,
14202 const expressionS *left,
14203 const expressionS *right)
14204 {
14205 if (op == O_absent)
14206 return false;
14207
14208 if (!left)
14209 {
14210 /* Since the expression parser applies unary operators fine to bignum
14211 operands, we don't need to be concerned of respective operands not
14212 fitting in 32 bits. */
14213 if (right->X_op == O_constant && right->X_unsigned
14214 && !fits_in_unsigned_long (right->X_add_number))
14215 return false;
14216 }
14217 /* This isn't entirely right: The pattern can also result when constant
14218 expressions are folded (e.g. 0xffffffff + 1). */
14219 else if ((left->X_op == O_constant && left->X_unsigned
14220 && !fits_in_unsigned_long (left->X_add_number))
14221 || (right->X_op == O_constant && right->X_unsigned
14222 && !fits_in_unsigned_long (right->X_add_number)))
14223 expr_mode = expr_large_value;
14224
14225 if (expr_mode != expr_large_value)
14226 expr_mode = expr_operator_present;
14227
14228 return false;
14229 }
14230 #endif
14231 \f
14232 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14233 const char *md_shortopts = "kVQ:sqnO::";
14234 #else
14235 const char *md_shortopts = "qnO::";
14236 #endif
14237
14238 #define OPTION_32 (OPTION_MD_BASE + 0)
14239 #define OPTION_64 (OPTION_MD_BASE + 1)
14240 #define OPTION_DIVIDE (OPTION_MD_BASE + 2)
14241 #define OPTION_MARCH (OPTION_MD_BASE + 3)
14242 #define OPTION_MTUNE (OPTION_MD_BASE + 4)
14243 #define OPTION_MMNEMONIC (OPTION_MD_BASE + 5)
14244 #define OPTION_MSYNTAX (OPTION_MD_BASE + 6)
14245 #define OPTION_MINDEX_REG (OPTION_MD_BASE + 7)
14246 #define OPTION_MNAKED_REG (OPTION_MD_BASE + 8)
14247 #define OPTION_MRELAX_RELOCATIONS (OPTION_MD_BASE + 9)
14248 #define OPTION_MSSE2AVX (OPTION_MD_BASE + 10)
14249 #define OPTION_MSSE_CHECK (OPTION_MD_BASE + 11)
14250 #define OPTION_MOPERAND_CHECK (OPTION_MD_BASE + 12)
14251 #define OPTION_MAVXSCALAR (OPTION_MD_BASE + 13)
14252 #define OPTION_X32 (OPTION_MD_BASE + 14)
14253 #define OPTION_MADD_BND_PREFIX (OPTION_MD_BASE + 15)
14254 #define OPTION_MEVEXLIG (OPTION_MD_BASE + 16)
14255 #define OPTION_MEVEXWIG (OPTION_MD_BASE + 17)
14256 #define OPTION_MBIG_OBJ (OPTION_MD_BASE + 18)
14257 #define OPTION_MOMIT_LOCK_PREFIX (OPTION_MD_BASE + 19)
14258 #define OPTION_MEVEXRCIG (OPTION_MD_BASE + 20)
14259 #define OPTION_MSHARED (OPTION_MD_BASE + 21)
14260 #define OPTION_MAMD64 (OPTION_MD_BASE + 22)
14261 #define OPTION_MINTEL64 (OPTION_MD_BASE + 23)
14262 #define OPTION_MFENCE_AS_LOCK_ADD (OPTION_MD_BASE + 24)
14263 #define OPTION_X86_USED_NOTE (OPTION_MD_BASE + 25)
14264 #define OPTION_MVEXWIG (OPTION_MD_BASE + 26)
14265 #define OPTION_MALIGN_BRANCH_BOUNDARY (OPTION_MD_BASE + 27)
14266 #define OPTION_MALIGN_BRANCH_PREFIX_SIZE (OPTION_MD_BASE + 28)
14267 #define OPTION_MALIGN_BRANCH (OPTION_MD_BASE + 29)
14268 #define OPTION_MBRANCHES_WITH_32B_BOUNDARIES (OPTION_MD_BASE + 30)
14269 #define OPTION_MLFENCE_AFTER_LOAD (OPTION_MD_BASE + 31)
14270 #define OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH (OPTION_MD_BASE + 32)
14271 #define OPTION_MLFENCE_BEFORE_RET (OPTION_MD_BASE + 33)
14272 #define OPTION_MUSE_UNALIGNED_VECTOR_MOVE (OPTION_MD_BASE + 34)
14273
14274 struct option md_longopts[] =
14275 {
14276 {"32", no_argument, NULL, OPTION_32},
14277 #if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
14278 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
14279 {"64", no_argument, NULL, OPTION_64},
14280 #endif
14281 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14282 {"x32", no_argument, NULL, OPTION_X32},
14283 {"mshared", no_argument, NULL, OPTION_MSHARED},
14284 {"mx86-used-note", required_argument, NULL, OPTION_X86_USED_NOTE},
14285 #endif
14286 {"divide", no_argument, NULL, OPTION_DIVIDE},
14287 {"march", required_argument, NULL, OPTION_MARCH},
14288 {"mtune", required_argument, NULL, OPTION_MTUNE},
14289 {"mmnemonic", required_argument, NULL, OPTION_MMNEMONIC},
14290 {"msyntax", required_argument, NULL, OPTION_MSYNTAX},
14291 {"mindex-reg", no_argument, NULL, OPTION_MINDEX_REG},
14292 {"mnaked-reg", no_argument, NULL, OPTION_MNAKED_REG},
14293 {"msse2avx", no_argument, NULL, OPTION_MSSE2AVX},
14294 {"muse-unaligned-vector-move", no_argument, NULL, OPTION_MUSE_UNALIGNED_VECTOR_MOVE},
14295 {"msse-check", required_argument, NULL, OPTION_MSSE_CHECK},
14296 {"moperand-check", required_argument, NULL, OPTION_MOPERAND_CHECK},
14297 {"mavxscalar", required_argument, NULL, OPTION_MAVXSCALAR},
14298 {"mvexwig", required_argument, NULL, OPTION_MVEXWIG},
14299 {"madd-bnd-prefix", no_argument, NULL, OPTION_MADD_BND_PREFIX},
14300 {"mevexlig", required_argument, NULL, OPTION_MEVEXLIG},
14301 {"mevexwig", required_argument, NULL, OPTION_MEVEXWIG},
14302 # if defined (TE_PE) || defined (TE_PEP)
14303 {"mbig-obj", no_argument, NULL, OPTION_MBIG_OBJ},
14304 #endif
14305 {"momit-lock-prefix", required_argument, NULL, OPTION_MOMIT_LOCK_PREFIX},
14306 {"mfence-as-lock-add", required_argument, NULL, OPTION_MFENCE_AS_LOCK_ADD},
14307 {"mrelax-relocations", required_argument, NULL, OPTION_MRELAX_RELOCATIONS},
14308 {"mevexrcig", required_argument, NULL, OPTION_MEVEXRCIG},
14309 {"malign-branch-boundary", required_argument, NULL, OPTION_MALIGN_BRANCH_BOUNDARY},
14310 {"malign-branch-prefix-size", required_argument, NULL, OPTION_MALIGN_BRANCH_PREFIX_SIZE},
14311 {"malign-branch", required_argument, NULL, OPTION_MALIGN_BRANCH},
14312 {"mbranches-within-32B-boundaries", no_argument, NULL, OPTION_MBRANCHES_WITH_32B_BOUNDARIES},
14313 {"mlfence-after-load", required_argument, NULL, OPTION_MLFENCE_AFTER_LOAD},
14314 {"mlfence-before-indirect-branch", required_argument, NULL,
14315 OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH},
14316 {"mlfence-before-ret", required_argument, NULL, OPTION_MLFENCE_BEFORE_RET},
14317 {"mamd64", no_argument, NULL, OPTION_MAMD64},
14318 {"mintel64", no_argument, NULL, OPTION_MINTEL64},
14319 {NULL, no_argument, NULL, 0}
14320 };
14321 size_t md_longopts_size = sizeof (md_longopts);
14322
14323 int
14324 md_parse_option (int c, const char *arg)
14325 {
14326 unsigned int j;
14327 char *arch, *next, *saved, *type;
14328
14329 switch (c)
14330 {
14331 case 'n':
14332 optimize_align_code = 0;
14333 break;
14334
14335 case 'q':
14336 quiet_warnings = 1;
14337 break;
14338
14339 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14340 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
14341 should be emitted or not. FIXME: Not implemented. */
14342 case 'Q':
14343 if ((arg[0] != 'y' && arg[0] != 'n') || arg[1])
14344 return 0;
14345 break;
14346
14347 /* -V: SVR4 argument to print version ID. */
14348 case 'V':
14349 print_version_id ();
14350 break;
14351
14352 /* -k: Ignore for FreeBSD compatibility. */
14353 case 'k':
14354 break;
14355
14356 case 's':
14357 /* -s: On i386 Solaris, this tells the native assembler to use
14358 .stab instead of .stab.excl. We always use .stab anyhow. */
14359 break;
14360
14361 case OPTION_MSHARED:
14362 shared = 1;
14363 break;
14364
14365 case OPTION_X86_USED_NOTE:
14366 if (strcasecmp (arg, "yes") == 0)
14367 x86_used_note = 1;
14368 else if (strcasecmp (arg, "no") == 0)
14369 x86_used_note = 0;
14370 else
14371 as_fatal (_("invalid -mx86-used-note= option: `%s'"), arg);
14372 break;
14373
14374
14375 #endif
14376 #if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
14377 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
14378 case OPTION_64:
14379 {
14380 const char **list, **l;
14381
14382 list = bfd_target_list ();
14383 for (l = list; *l != NULL; l++)
14384 if (startswith (*l, "elf64-x86-64")
14385 || strcmp (*l, "coff-x86-64") == 0
14386 || strcmp (*l, "pe-x86-64") == 0
14387 || strcmp (*l, "pei-x86-64") == 0
14388 || strcmp (*l, "mach-o-x86-64") == 0)
14389 {
14390 default_arch = "x86_64";
14391 break;
14392 }
14393 if (*l == NULL)
14394 as_fatal (_("no compiled in support for x86_64"));
14395 free (list);
14396 }
14397 break;
14398 #endif
14399
14400 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14401 case OPTION_X32:
14402 if (IS_ELF)
14403 {
14404 const char **list, **l;
14405
14406 list = bfd_target_list ();
14407 for (l = list; *l != NULL; l++)
14408 if (startswith (*l, "elf32-x86-64"))
14409 {
14410 default_arch = "x86_64:32";
14411 break;
14412 }
14413 if (*l == NULL)
14414 as_fatal (_("no compiled in support for 32bit x86_64"));
14415 free (list);
14416 }
14417 else
14418 as_fatal (_("32bit x86_64 is only supported for ELF"));
14419 break;
14420 #endif
14421
14422 case OPTION_32:
14423 {
14424 const char **list, **l;
14425
14426 list = bfd_target_list ();
14427 for (l = list; *l != NULL; l++)
14428 if (strstr (*l, "-i386")
14429 || strstr (*l, "-go32"))
14430 {
14431 default_arch = "i386";
14432 break;
14433 }
14434 if (*l == NULL)
14435 as_fatal (_("no compiled in support for ix86"));
14436 free (list);
14437 }
14438 break;
14439
14440 case OPTION_DIVIDE:
14441 #ifdef SVR4_COMMENT_CHARS
14442 {
14443 char *n, *t;
14444 const char *s;
14445
14446 n = XNEWVEC (char, strlen (i386_comment_chars) + 1);
14447 t = n;
14448 for (s = i386_comment_chars; *s != '\0'; s++)
14449 if (*s != '/')
14450 *t++ = *s;
14451 *t = '\0';
14452 i386_comment_chars = n;
14453 }
14454 #endif
14455 break;
14456
14457 case OPTION_MARCH:
14458 saved = xstrdup (arg);
14459 arch = saved;
14460 /* Allow -march=+nosse. */
14461 if (*arch == '+')
14462 arch++;
14463 do
14464 {
14465 char *vsz;
14466
14467 if (*arch == '.')
14468 as_fatal (_("invalid -march= option: `%s'"), arg);
14469 next = strchr (arch, '+');
14470 if (next)
14471 *next++ = '\0';
14472 vsz = strchr (arch, '/');
14473 if (vsz)
14474 *vsz++ = '\0';
14475 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
14476 {
14477 if (vsz && cpu_arch[j].vsz != vsz_set)
14478 continue;
14479
14480 if (arch == saved && cpu_arch[j].type != PROCESSOR_NONE
14481 && strcmp (arch, cpu_arch[j].name) == 0)
14482 {
14483 /* Processor. */
14484 if (! cpu_arch[j].enable.bitfield.cpui386)
14485 continue;
14486
14487 cpu_arch_name = cpu_arch[j].name;
14488 free (cpu_sub_arch_name);
14489 cpu_sub_arch_name = NULL;
14490 cpu_arch_flags = cpu_arch[j].enable;
14491 cpu_arch_isa = cpu_arch[j].type;
14492 cpu_arch_isa_flags = cpu_arch[j].enable;
14493 if (!cpu_arch_tune_set)
14494 {
14495 cpu_arch_tune = cpu_arch_isa;
14496 cpu_arch_tune_flags = cpu_arch_isa_flags;
14497 }
14498 vector_size = VSZ_DEFAULT;
14499 break;
14500 }
14501 else if (cpu_arch[j].type == PROCESSOR_NONE
14502 && strcmp (arch, cpu_arch[j].name) == 0
14503 && !cpu_flags_all_zero (&cpu_arch[j].enable))
14504 {
14505 /* ISA extension. */
14506 i386_cpu_flags flags;
14507
14508 flags = cpu_flags_or (cpu_arch_flags,
14509 cpu_arch[j].enable);
14510
14511 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
14512 {
14513 extend_cpu_sub_arch_name (arch);
14514 cpu_arch_flags = flags;
14515 cpu_arch_isa_flags = flags;
14516 }
14517 else
14518 cpu_arch_isa_flags
14519 = cpu_flags_or (cpu_arch_isa_flags,
14520 cpu_arch[j].enable);
14521
14522 switch (cpu_arch[j].vsz)
14523 {
14524 default:
14525 break;
14526
14527 case vsz_set:
14528 if (vsz)
14529 {
14530 char *end;
14531 unsigned long val = strtoul (vsz, &end, 0);
14532
14533 if (*end)
14534 val = 0;
14535 switch (val)
14536 {
14537 case 512: vector_size = VSZ512; break;
14538 case 256: vector_size = VSZ256; break;
14539 case 128: vector_size = VSZ128; break;
14540 default:
14541 as_warn (_("Unrecognized vector size specifier ignored"));
14542 break;
14543 }
14544 break;
14545 }
14546 /* Fall through. */
14547 case vsz_reset:
14548 vector_size = VSZ_DEFAULT;
14549 break;
14550 }
14551
14552 break;
14553 }
14554 }
14555
14556 if (j >= ARRAY_SIZE (cpu_arch) && startswith (arch, "no"))
14557 {
14558 /* Disable an ISA extension. */
14559 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
14560 if (cpu_arch[j].type == PROCESSOR_NONE
14561 && strcmp (arch + 2, cpu_arch[j].name) == 0)
14562 {
14563 i386_cpu_flags flags;
14564
14565 flags = cpu_flags_and_not (cpu_arch_flags,
14566 cpu_arch[j].disable);
14567 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
14568 {
14569 extend_cpu_sub_arch_name (arch);
14570 cpu_arch_flags = flags;
14571 cpu_arch_isa_flags = flags;
14572 }
14573 if (cpu_arch[j].vsz == vsz_set)
14574 vector_size = VSZ_DEFAULT;
14575 break;
14576 }
14577 }
14578
14579 if (j >= ARRAY_SIZE (cpu_arch))
14580 as_fatal (_("invalid -march= option: `%s'"), arg);
14581
14582 arch = next;
14583 }
14584 while (next != NULL);
14585 free (saved);
14586 break;
14587
14588 case OPTION_MTUNE:
14589 if (*arg == '.')
14590 as_fatal (_("invalid -mtune= option: `%s'"), arg);
14591 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
14592 {
14593 if (cpu_arch[j].type != PROCESSOR_NONE
14594 && strcmp (arg, cpu_arch[j].name) == 0)
14595 {
14596 cpu_arch_tune_set = 1;
14597 cpu_arch_tune = cpu_arch [j].type;
14598 cpu_arch_tune_flags = cpu_arch[j].enable;
14599 break;
14600 }
14601 }
14602 if (j >= ARRAY_SIZE (cpu_arch))
14603 as_fatal (_("invalid -mtune= option: `%s'"), arg);
14604 break;
14605
14606 case OPTION_MMNEMONIC:
14607 if (strcasecmp (arg, "att") == 0)
14608 intel_mnemonic = 0;
14609 else if (strcasecmp (arg, "intel") == 0)
14610 intel_mnemonic = 1;
14611 else
14612 as_fatal (_("invalid -mmnemonic= option: `%s'"), arg);
14613 break;
14614
14615 case OPTION_MSYNTAX:
14616 if (strcasecmp (arg, "att") == 0)
14617 intel_syntax = 0;
14618 else if (strcasecmp (arg, "intel") == 0)
14619 intel_syntax = 1;
14620 else
14621 as_fatal (_("invalid -msyntax= option: `%s'"), arg);
14622 break;
14623
14624 case OPTION_MINDEX_REG:
14625 allow_index_reg = 1;
14626 break;
14627
14628 case OPTION_MNAKED_REG:
14629 allow_naked_reg = 1;
14630 break;
14631
14632 case OPTION_MSSE2AVX:
14633 sse2avx = 1;
14634 break;
14635
14636 case OPTION_MUSE_UNALIGNED_VECTOR_MOVE:
14637 use_unaligned_vector_move = 1;
14638 break;
14639
14640 case OPTION_MSSE_CHECK:
14641 if (strcasecmp (arg, "error") == 0)
14642 sse_check = check_error;
14643 else if (strcasecmp (arg, "warning") == 0)
14644 sse_check = check_warning;
14645 else if (strcasecmp (arg, "none") == 0)
14646 sse_check = check_none;
14647 else
14648 as_fatal (_("invalid -msse-check= option: `%s'"), arg);
14649 break;
14650
14651 case OPTION_MOPERAND_CHECK:
14652 if (strcasecmp (arg, "error") == 0)
14653 operand_check = check_error;
14654 else if (strcasecmp (arg, "warning") == 0)
14655 operand_check = check_warning;
14656 else if (strcasecmp (arg, "none") == 0)
14657 operand_check = check_none;
14658 else
14659 as_fatal (_("invalid -moperand-check= option: `%s'"), arg);
14660 break;
14661
14662 case OPTION_MAVXSCALAR:
14663 if (strcasecmp (arg, "128") == 0)
14664 avxscalar = vex128;
14665 else if (strcasecmp (arg, "256") == 0)
14666 avxscalar = vex256;
14667 else
14668 as_fatal (_("invalid -mavxscalar= option: `%s'"), arg);
14669 break;
14670
14671 case OPTION_MVEXWIG:
14672 if (strcmp (arg, "0") == 0)
14673 vexwig = vexw0;
14674 else if (strcmp (arg, "1") == 0)
14675 vexwig = vexw1;
14676 else
14677 as_fatal (_("invalid -mvexwig= option: `%s'"), arg);
14678 break;
14679
14680 case OPTION_MADD_BND_PREFIX:
14681 add_bnd_prefix = 1;
14682 break;
14683
14684 case OPTION_MEVEXLIG:
14685 if (strcmp (arg, "128") == 0)
14686 evexlig = evexl128;
14687 else if (strcmp (arg, "256") == 0)
14688 evexlig = evexl256;
14689 else if (strcmp (arg, "512") == 0)
14690 evexlig = evexl512;
14691 else
14692 as_fatal (_("invalid -mevexlig= option: `%s'"), arg);
14693 break;
14694
14695 case OPTION_MEVEXRCIG:
14696 if (strcmp (arg, "rne") == 0)
14697 evexrcig = rne;
14698 else if (strcmp (arg, "rd") == 0)
14699 evexrcig = rd;
14700 else if (strcmp (arg, "ru") == 0)
14701 evexrcig = ru;
14702 else if (strcmp (arg, "rz") == 0)
14703 evexrcig = rz;
14704 else
14705 as_fatal (_("invalid -mevexrcig= option: `%s'"), arg);
14706 break;
14707
14708 case OPTION_MEVEXWIG:
14709 if (strcmp (arg, "0") == 0)
14710 evexwig = evexw0;
14711 else if (strcmp (arg, "1") == 0)
14712 evexwig = evexw1;
14713 else
14714 as_fatal (_("invalid -mevexwig= option: `%s'"), arg);
14715 break;
14716
14717 # if defined (TE_PE) || defined (TE_PEP)
14718 case OPTION_MBIG_OBJ:
14719 use_big_obj = 1;
14720 break;
14721 #endif
14722
14723 case OPTION_MOMIT_LOCK_PREFIX:
14724 if (strcasecmp (arg, "yes") == 0)
14725 omit_lock_prefix = 1;
14726 else if (strcasecmp (arg, "no") == 0)
14727 omit_lock_prefix = 0;
14728 else
14729 as_fatal (_("invalid -momit-lock-prefix= option: `%s'"), arg);
14730 break;
14731
14732 case OPTION_MFENCE_AS_LOCK_ADD:
14733 if (strcasecmp (arg, "yes") == 0)
14734 avoid_fence = 1;
14735 else if (strcasecmp (arg, "no") == 0)
14736 avoid_fence = 0;
14737 else
14738 as_fatal (_("invalid -mfence-as-lock-add= option: `%s'"), arg);
14739 break;
14740
14741 case OPTION_MLFENCE_AFTER_LOAD:
14742 if (strcasecmp (arg, "yes") == 0)
14743 lfence_after_load = 1;
14744 else if (strcasecmp (arg, "no") == 0)
14745 lfence_after_load = 0;
14746 else
14747 as_fatal (_("invalid -mlfence-after-load= option: `%s'"), arg);
14748 break;
14749
14750 case OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH:
14751 if (strcasecmp (arg, "all") == 0)
14752 {
14753 lfence_before_indirect_branch = lfence_branch_all;
14754 if (lfence_before_ret == lfence_before_ret_none)
14755 lfence_before_ret = lfence_before_ret_shl;
14756 }
14757 else if (strcasecmp (arg, "memory") == 0)
14758 lfence_before_indirect_branch = lfence_branch_memory;
14759 else if (strcasecmp (arg, "register") == 0)
14760 lfence_before_indirect_branch = lfence_branch_register;
14761 else if (strcasecmp (arg, "none") == 0)
14762 lfence_before_indirect_branch = lfence_branch_none;
14763 else
14764 as_fatal (_("invalid -mlfence-before-indirect-branch= option: `%s'"),
14765 arg);
14766 break;
14767
14768 case OPTION_MLFENCE_BEFORE_RET:
14769 if (strcasecmp (arg, "or") == 0)
14770 lfence_before_ret = lfence_before_ret_or;
14771 else if (strcasecmp (arg, "not") == 0)
14772 lfence_before_ret = lfence_before_ret_not;
14773 else if (strcasecmp (arg, "shl") == 0 || strcasecmp (arg, "yes") == 0)
14774 lfence_before_ret = lfence_before_ret_shl;
14775 else if (strcasecmp (arg, "none") == 0)
14776 lfence_before_ret = lfence_before_ret_none;
14777 else
14778 as_fatal (_("invalid -mlfence-before-ret= option: `%s'"),
14779 arg);
14780 break;
14781
14782 case OPTION_MRELAX_RELOCATIONS:
14783 if (strcasecmp (arg, "yes") == 0)
14784 generate_relax_relocations = 1;
14785 else if (strcasecmp (arg, "no") == 0)
14786 generate_relax_relocations = 0;
14787 else
14788 as_fatal (_("invalid -mrelax-relocations= option: `%s'"), arg);
14789 break;
14790
14791 case OPTION_MALIGN_BRANCH_BOUNDARY:
14792 {
14793 char *end;
14794 long int align = strtoul (arg, &end, 0);
14795 if (*end == '\0')
14796 {
14797 if (align == 0)
14798 {
14799 align_branch_power = 0;
14800 break;
14801 }
14802 else if (align >= 16)
14803 {
14804 int align_power;
14805 for (align_power = 0;
14806 (align & 1) == 0;
14807 align >>= 1, align_power++)
14808 continue;
14809 /* Limit alignment power to 31. */
14810 if (align == 1 && align_power < 32)
14811 {
14812 align_branch_power = align_power;
14813 break;
14814 }
14815 }
14816 }
14817 as_fatal (_("invalid -malign-branch-boundary= value: %s"), arg);
14818 }
14819 break;
14820
14821 case OPTION_MALIGN_BRANCH_PREFIX_SIZE:
14822 {
14823 char *end;
14824 int align = strtoul (arg, &end, 0);
14825 /* Some processors only support 5 prefixes. */
14826 if (*end == '\0' && align >= 0 && align < 6)
14827 {
14828 align_branch_prefix_size = align;
14829 break;
14830 }
14831 as_fatal (_("invalid -malign-branch-prefix-size= value: %s"),
14832 arg);
14833 }
14834 break;
14835
14836 case OPTION_MALIGN_BRANCH:
14837 align_branch = 0;
14838 saved = xstrdup (arg);
14839 type = saved;
14840 do
14841 {
14842 next = strchr (type, '+');
14843 if (next)
14844 *next++ = '\0';
14845 if (strcasecmp (type, "jcc") == 0)
14846 align_branch |= align_branch_jcc_bit;
14847 else if (strcasecmp (type, "fused") == 0)
14848 align_branch |= align_branch_fused_bit;
14849 else if (strcasecmp (type, "jmp") == 0)
14850 align_branch |= align_branch_jmp_bit;
14851 else if (strcasecmp (type, "call") == 0)
14852 align_branch |= align_branch_call_bit;
14853 else if (strcasecmp (type, "ret") == 0)
14854 align_branch |= align_branch_ret_bit;
14855 else if (strcasecmp (type, "indirect") == 0)
14856 align_branch |= align_branch_indirect_bit;
14857 else
14858 as_fatal (_("invalid -malign-branch= option: `%s'"), arg);
14859 type = next;
14860 }
14861 while (next != NULL);
14862 free (saved);
14863 break;
14864
14865 case OPTION_MBRANCHES_WITH_32B_BOUNDARIES:
14866 align_branch_power = 5;
14867 align_branch_prefix_size = 5;
14868 align_branch = (align_branch_jcc_bit
14869 | align_branch_fused_bit
14870 | align_branch_jmp_bit);
14871 break;
14872
14873 case OPTION_MAMD64:
14874 isa64 = amd64;
14875 break;
14876
14877 case OPTION_MINTEL64:
14878 isa64 = intel64;
14879 break;
14880
14881 case 'O':
14882 if (arg == NULL)
14883 {
14884 optimize = 1;
14885 /* Turn off -Os. */
14886 optimize_for_space = 0;
14887 }
14888 else if (*arg == 's')
14889 {
14890 optimize_for_space = 1;
14891 /* Turn on all encoding optimizations. */
14892 optimize = INT_MAX;
14893 }
14894 else
14895 {
14896 optimize = atoi (arg);
14897 /* Turn off -Os. */
14898 optimize_for_space = 0;
14899 }
14900 break;
14901
14902 default:
14903 return 0;
14904 }
14905 return 1;
14906 }
14907
14908 #define MESSAGE_TEMPLATE \
14909 " "
14910
14911 static char *
14912 output_message (FILE *stream, char *p, char *message, char *start,
14913 int *left_p, const char *name, int len)
14914 {
14915 int size = sizeof (MESSAGE_TEMPLATE);
14916 int left = *left_p;
14917
14918 /* Reserve 2 spaces for ", " or ",\0" */
14919 left -= len + 2;
14920
14921 /* Check if there is any room. */
14922 if (left >= 0)
14923 {
14924 if (p != start)
14925 {
14926 *p++ = ',';
14927 *p++ = ' ';
14928 }
14929 p = mempcpy (p, name, len);
14930 }
14931 else
14932 {
14933 /* Output the current message now and start a new one. */
14934 *p++ = ',';
14935 *p = '\0';
14936 fprintf (stream, "%s\n", message);
14937 p = start;
14938 left = size - (start - message) - len - 2;
14939
14940 gas_assert (left >= 0);
14941
14942 p = mempcpy (p, name, len);
14943 }
14944
14945 *left_p = left;
14946 return p;
14947 }
14948
14949 static void
14950 show_arch (FILE *stream, int ext, int check)
14951 {
14952 static char message[] = MESSAGE_TEMPLATE;
14953 char *start = message + 27;
14954 char *p;
14955 int size = sizeof (MESSAGE_TEMPLATE);
14956 int left;
14957 const char *name;
14958 int len;
14959 unsigned int j;
14960
14961 p = start;
14962 left = size - (start - message);
14963
14964 if (!ext && check)
14965 {
14966 p = output_message (stream, p, message, start, &left,
14967 STRING_COMMA_LEN ("default"));
14968 p = output_message (stream, p, message, start, &left,
14969 STRING_COMMA_LEN ("push"));
14970 p = output_message (stream, p, message, start, &left,
14971 STRING_COMMA_LEN ("pop"));
14972 }
14973
14974 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
14975 {
14976 /* Should it be skipped? */
14977 if (cpu_arch [j].skip)
14978 continue;
14979
14980 name = cpu_arch [j].name;
14981 len = cpu_arch [j].len;
14982 if (cpu_arch[j].type == PROCESSOR_NONE)
14983 {
14984 /* It is an extension. Skip if we aren't asked to show it. */
14985 if (!ext || cpu_flags_all_zero (&cpu_arch[j].enable))
14986 continue;
14987 }
14988 else if (ext)
14989 {
14990 /* It is an processor. Skip if we show only extension. */
14991 continue;
14992 }
14993 else if (check && ! cpu_arch[j].enable.bitfield.cpui386)
14994 {
14995 /* It is an impossible processor - skip. */
14996 continue;
14997 }
14998
14999 p = output_message (stream, p, message, start, &left, name, len);
15000 }
15001
15002 /* Display disabled extensions. */
15003 if (ext)
15004 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
15005 {
15006 char *str;
15007
15008 if (cpu_arch[j].type != PROCESSOR_NONE
15009 || !cpu_flags_all_zero (&cpu_arch[j].enable))
15010 continue;
15011 str = xasprintf ("no%s", cpu_arch[j].name);
15012 p = output_message (stream, p, message, start, &left, str,
15013 strlen (str));
15014 free (str);
15015 }
15016
15017 *p = '\0';
15018 fprintf (stream, "%s\n", message);
15019 }
15020
15021 void
15022 md_show_usage (FILE *stream)
15023 {
15024 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15025 fprintf (stream, _("\
15026 -Qy, -Qn ignored\n\
15027 -V print assembler version number\n\
15028 -k ignored\n"));
15029 #endif
15030 fprintf (stream, _("\
15031 -n do not optimize code alignment\n\
15032 -O{012s} attempt some code optimizations\n\
15033 -q quieten some warnings\n"));
15034 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15035 fprintf (stream, _("\
15036 -s ignored\n"));
15037 #endif
15038 #ifdef BFD64
15039 # if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15040 fprintf (stream, _("\
15041 --32/--64/--x32 generate 32bit/64bit/x32 object\n"));
15042 # elif defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O)
15043 fprintf (stream, _("\
15044 --32/--64 generate 32bit/64bit object\n"));
15045 # endif
15046 #endif
15047 #ifdef SVR4_COMMENT_CHARS
15048 fprintf (stream, _("\
15049 --divide do not treat `/' as a comment character\n"));
15050 #else
15051 fprintf (stream, _("\
15052 --divide ignored\n"));
15053 #endif
15054 fprintf (stream, _("\
15055 -march=CPU[,+EXTENSION...]\n\
15056 generate code for CPU and EXTENSION, CPU is one of:\n"));
15057 show_arch (stream, 0, 1);
15058 fprintf (stream, _("\
15059 EXTENSION is combination of (possibly \"no\"-prefixed):\n"));
15060 show_arch (stream, 1, 0);
15061 fprintf (stream, _("\
15062 -mtune=CPU optimize for CPU, CPU is one of:\n"));
15063 show_arch (stream, 0, 0);
15064 fprintf (stream, _("\
15065 -msse2avx encode SSE instructions with VEX prefix\n"));
15066 fprintf (stream, _("\
15067 -muse-unaligned-vector-move\n\
15068 encode aligned vector move as unaligned vector move\n"));
15069 fprintf (stream, _("\
15070 -msse-check=[none|error|warning] (default: warning)\n\
15071 check SSE instructions\n"));
15072 fprintf (stream, _("\
15073 -moperand-check=[none|error|warning] (default: warning)\n\
15074 check operand combinations for validity\n"));
15075 fprintf (stream, _("\
15076 -mavxscalar=[128|256] (default: 128)\n\
15077 encode scalar AVX instructions with specific vector\n\
15078 length\n"));
15079 fprintf (stream, _("\
15080 -mvexwig=[0|1] (default: 0)\n\
15081 encode VEX instructions with specific VEX.W value\n\
15082 for VEX.W bit ignored instructions\n"));
15083 fprintf (stream, _("\
15084 -mevexlig=[128|256|512] (default: 128)\n\
15085 encode scalar EVEX instructions with specific vector\n\
15086 length\n"));
15087 fprintf (stream, _("\
15088 -mevexwig=[0|1] (default: 0)\n\
15089 encode EVEX instructions with specific EVEX.W value\n\
15090 for EVEX.W bit ignored instructions\n"));
15091 fprintf (stream, _("\
15092 -mevexrcig=[rne|rd|ru|rz] (default: rne)\n\
15093 encode EVEX instructions with specific EVEX.RC value\n\
15094 for SAE-only ignored instructions\n"));
15095 fprintf (stream, _("\
15096 -mmnemonic=[att|intel] "));
15097 if (SYSV386_COMPAT)
15098 fprintf (stream, _("(default: att)\n"));
15099 else
15100 fprintf (stream, _("(default: intel)\n"));
15101 fprintf (stream, _("\
15102 use AT&T/Intel mnemonic\n"));
15103 fprintf (stream, _("\
15104 -msyntax=[att|intel] (default: att)\n\
15105 use AT&T/Intel syntax\n"));
15106 fprintf (stream, _("\
15107 -mindex-reg support pseudo index registers\n"));
15108 fprintf (stream, _("\
15109 -mnaked-reg don't require `%%' prefix for registers\n"));
15110 fprintf (stream, _("\
15111 -madd-bnd-prefix add BND prefix for all valid branches\n"));
15112 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15113 fprintf (stream, _("\
15114 -mshared disable branch optimization for shared code\n"));
15115 fprintf (stream, _("\
15116 -mx86-used-note=[no|yes] "));
15117 if (DEFAULT_X86_USED_NOTE)
15118 fprintf (stream, _("(default: yes)\n"));
15119 else
15120 fprintf (stream, _("(default: no)\n"));
15121 fprintf (stream, _("\
15122 generate x86 used ISA and feature properties\n"));
15123 #endif
15124 #if defined (TE_PE) || defined (TE_PEP)
15125 fprintf (stream, _("\
15126 -mbig-obj generate big object files\n"));
15127 #endif
15128 fprintf (stream, _("\
15129 -momit-lock-prefix=[no|yes] (default: no)\n\
15130 strip all lock prefixes\n"));
15131 fprintf (stream, _("\
15132 -mfence-as-lock-add=[no|yes] (default: no)\n\
15133 encode lfence, mfence and sfence as\n\
15134 lock addl $0x0, (%%{re}sp)\n"));
15135 fprintf (stream, _("\
15136 -mrelax-relocations=[no|yes] "));
15137 if (DEFAULT_GENERATE_X86_RELAX_RELOCATIONS)
15138 fprintf (stream, _("(default: yes)\n"));
15139 else
15140 fprintf (stream, _("(default: no)\n"));
15141 fprintf (stream, _("\
15142 generate relax relocations\n"));
15143 fprintf (stream, _("\
15144 -malign-branch-boundary=NUM (default: 0)\n\
15145 align branches within NUM byte boundary\n"));
15146 fprintf (stream, _("\
15147 -malign-branch=TYPE[+TYPE...] (default: jcc+fused+jmp)\n\
15148 TYPE is combination of jcc, fused, jmp, call, ret,\n\
15149 indirect\n\
15150 specify types of branches to align\n"));
15151 fprintf (stream, _("\
15152 -malign-branch-prefix-size=NUM (default: 5)\n\
15153 align branches with NUM prefixes per instruction\n"));
15154 fprintf (stream, _("\
15155 -mbranches-within-32B-boundaries\n\
15156 align branches within 32 byte boundary\n"));
15157 fprintf (stream, _("\
15158 -mlfence-after-load=[no|yes] (default: no)\n\
15159 generate lfence after load\n"));
15160 fprintf (stream, _("\
15161 -mlfence-before-indirect-branch=[none|all|register|memory] (default: none)\n\
15162 generate lfence before indirect near branch\n"));
15163 fprintf (stream, _("\
15164 -mlfence-before-ret=[none|or|not|shl|yes] (default: none)\n\
15165 generate lfence before ret\n"));
15166 fprintf (stream, _("\
15167 -mamd64 accept only AMD64 ISA [default]\n"));
15168 fprintf (stream, _("\
15169 -mintel64 accept only Intel64 ISA\n"));
15170 }
15171
15172 #if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
15173 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
15174 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
15175
15176 /* Pick the target format to use. */
15177
15178 const char *
15179 i386_target_format (void)
15180 {
15181 if (startswith (default_arch, "x86_64"))
15182 {
15183 update_code_flag (CODE_64BIT, 1);
15184 if (default_arch[6] == '\0')
15185 x86_elf_abi = X86_64_ABI;
15186 else
15187 x86_elf_abi = X86_64_X32_ABI;
15188 }
15189 else if (!strcmp (default_arch, "i386"))
15190 update_code_flag (CODE_32BIT, 1);
15191 else if (!strcmp (default_arch, "iamcu"))
15192 {
15193 update_code_flag (CODE_32BIT, 1);
15194 if (cpu_arch_isa == PROCESSOR_UNKNOWN)
15195 {
15196 static const i386_cpu_flags iamcu_flags = CPU_IAMCU_FLAGS;
15197 cpu_arch_name = "iamcu";
15198 free (cpu_sub_arch_name);
15199 cpu_sub_arch_name = NULL;
15200 cpu_arch_flags = iamcu_flags;
15201 cpu_arch_isa = PROCESSOR_IAMCU;
15202 cpu_arch_isa_flags = iamcu_flags;
15203 if (!cpu_arch_tune_set)
15204 {
15205 cpu_arch_tune = cpu_arch_isa;
15206 cpu_arch_tune_flags = cpu_arch_isa_flags;
15207 }
15208 }
15209 else if (cpu_arch_isa != PROCESSOR_IAMCU)
15210 as_fatal (_("Intel MCU doesn't support `%s' architecture"),
15211 cpu_arch_name);
15212 }
15213 else
15214 as_fatal (_("unknown architecture"));
15215
15216 if (cpu_flags_all_zero (&cpu_arch_isa_flags))
15217 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
15218 if (cpu_flags_all_zero (&cpu_arch_tune_flags))
15219 cpu_arch_tune_flags = cpu_arch[flag_code == CODE_64BIT].enable;
15220
15221 switch (OUTPUT_FLAVOR)
15222 {
15223 #if defined (OBJ_MAYBE_AOUT) || defined (OBJ_AOUT)
15224 case bfd_target_aout_flavour:
15225 return AOUT_TARGET_FORMAT;
15226 #endif
15227 #if defined (OBJ_MAYBE_COFF) || defined (OBJ_COFF)
15228 # if defined (TE_PE) || defined (TE_PEP)
15229 case bfd_target_coff_flavour:
15230 if (flag_code == CODE_64BIT)
15231 {
15232 object_64bit = 1;
15233 return use_big_obj ? "pe-bigobj-x86-64" : "pe-x86-64";
15234 }
15235 return use_big_obj ? "pe-bigobj-i386" : "pe-i386";
15236 # elif defined (TE_GO32)
15237 case bfd_target_coff_flavour:
15238 return "coff-go32";
15239 # else
15240 case bfd_target_coff_flavour:
15241 return "coff-i386";
15242 # endif
15243 #endif
15244 #if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
15245 case bfd_target_elf_flavour:
15246 {
15247 const char *format;
15248
15249 switch (x86_elf_abi)
15250 {
15251 default:
15252 format = ELF_TARGET_FORMAT;
15253 #ifndef TE_SOLARIS
15254 tls_get_addr = "___tls_get_addr";
15255 #endif
15256 break;
15257 case X86_64_ABI:
15258 use_rela_relocations = 1;
15259 object_64bit = 1;
15260 #ifndef TE_SOLARIS
15261 tls_get_addr = "__tls_get_addr";
15262 #endif
15263 format = ELF_TARGET_FORMAT64;
15264 break;
15265 case X86_64_X32_ABI:
15266 use_rela_relocations = 1;
15267 object_64bit = 1;
15268 #ifndef TE_SOLARIS
15269 tls_get_addr = "__tls_get_addr";
15270 #endif
15271 disallow_64bit_reloc = 1;
15272 format = ELF_TARGET_FORMAT32;
15273 break;
15274 }
15275 if (cpu_arch_isa == PROCESSOR_IAMCU)
15276 {
15277 if (x86_elf_abi != I386_ABI)
15278 as_fatal (_("Intel MCU is 32bit only"));
15279 return ELF_TARGET_IAMCU_FORMAT;
15280 }
15281 else
15282 return format;
15283 }
15284 #endif
15285 #if defined (OBJ_MACH_O)
15286 case bfd_target_mach_o_flavour:
15287 if (flag_code == CODE_64BIT)
15288 {
15289 use_rela_relocations = 1;
15290 object_64bit = 1;
15291 return "mach-o-x86-64";
15292 }
15293 else
15294 return "mach-o-i386";
15295 #endif
15296 default:
15297 abort ();
15298 return NULL;
15299 }
15300 }
15301
15302 #endif /* OBJ_MAYBE_ more than one */
15303 \f
15304 symbolS *
15305 md_undefined_symbol (char *name)
15306 {
15307 if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
15308 && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
15309 && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
15310 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
15311 {
15312 if (!GOT_symbol)
15313 {
15314 if (symbol_find (name))
15315 as_bad (_("GOT already in symbol table"));
15316 GOT_symbol = symbol_new (name, undefined_section,
15317 &zero_address_frag, 0);
15318 };
15319 return GOT_symbol;
15320 }
15321 return 0;
15322 }
15323
15324 /* Round up a section size to the appropriate boundary. */
15325
15326 valueT
15327 md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
15328 {
15329 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
15330 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
15331 {
15332 /* For a.out, force the section size to be aligned. If we don't do
15333 this, BFD will align it for us, but it will not write out the
15334 final bytes of the section. This may be a bug in BFD, but it is
15335 easier to fix it here since that is how the other a.out targets
15336 work. */
15337 int align;
15338
15339 align = bfd_section_alignment (segment);
15340 size = ((size + (1 << align) - 1) & (-((valueT) 1 << align)));
15341 }
15342 #endif
15343
15344 return size;
15345 }
15346
15347 /* On the i386, PC-relative offsets are relative to the start of the
15348 next instruction. That is, the address of the offset, plus its
15349 size, since the offset is always the last part of the insn. */
15350
15351 long
15352 md_pcrel_from (fixS *fixP)
15353 {
15354 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
15355 }
15356
15357 #ifndef I386COFF
15358
15359 static void
15360 s_bss (int ignore ATTRIBUTE_UNUSED)
15361 {
15362 int temp;
15363
15364 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15365 if (IS_ELF)
15366 obj_elf_section_change_hook ();
15367 #endif
15368 temp = get_absolute_expression ();
15369 subseg_set (bss_section, (subsegT) temp);
15370 demand_empty_rest_of_line ();
15371 }
15372
15373 #endif
15374
15375 /* Remember constant directive. */
15376
15377 void
15378 i386_cons_align (int ignore ATTRIBUTE_UNUSED)
15379 {
15380 if (last_insn.kind != last_insn_directive
15381 && (bfd_section_flags (now_seg) & SEC_CODE))
15382 {
15383 last_insn.seg = now_seg;
15384 last_insn.kind = last_insn_directive;
15385 last_insn.name = "constant directive";
15386 last_insn.file = as_where (&last_insn.line);
15387 if (lfence_before_ret != lfence_before_ret_none)
15388 {
15389 if (lfence_before_indirect_branch != lfence_branch_none)
15390 as_warn (_("constant directive skips -mlfence-before-ret "
15391 "and -mlfence-before-indirect-branch"));
15392 else
15393 as_warn (_("constant directive skips -mlfence-before-ret"));
15394 }
15395 else if (lfence_before_indirect_branch != lfence_branch_none)
15396 as_warn (_("constant directive skips -mlfence-before-indirect-branch"));
15397 }
15398 }
15399
15400 int
15401 i386_validate_fix (fixS *fixp)
15402 {
15403 if (fixp->fx_addsy && S_GET_SEGMENT(fixp->fx_addsy) == reg_section)
15404 {
15405 reloc_howto_type *howto;
15406
15407 howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
15408 as_bad_where (fixp->fx_file, fixp->fx_line,
15409 _("invalid %s relocation against register"),
15410 howto ? howto->name : "<unknown>");
15411 return 0;
15412 }
15413
15414 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15415 if (fixp->fx_r_type == BFD_RELOC_SIZE32
15416 || fixp->fx_r_type == BFD_RELOC_SIZE64)
15417 return IS_ELF && fixp->fx_addsy
15418 && (!S_IS_DEFINED (fixp->fx_addsy)
15419 || S_IS_EXTERNAL (fixp->fx_addsy));
15420 #endif
15421
15422 if (fixp->fx_subsy)
15423 {
15424 if (fixp->fx_subsy == GOT_symbol)
15425 {
15426 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
15427 {
15428 if (!object_64bit)
15429 abort ();
15430 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15431 if (fixp->fx_tcbit2)
15432 fixp->fx_r_type = (fixp->fx_tcbit
15433 ? BFD_RELOC_X86_64_REX_GOTPCRELX
15434 : BFD_RELOC_X86_64_GOTPCRELX);
15435 else
15436 #endif
15437 fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
15438 }
15439 else
15440 {
15441 if (!object_64bit)
15442 fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
15443 else
15444 fixp->fx_r_type = BFD_RELOC_X86_64_GOTOFF64;
15445 }
15446 fixp->fx_subsy = 0;
15447 }
15448 }
15449 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15450 else
15451 {
15452 /* NB: Commit 292676c1 resolved PLT32 reloc aganst local symbol
15453 to section. Since PLT32 relocation must be against symbols,
15454 turn such PLT32 relocation into PC32 relocation. */
15455 if (fixp->fx_addsy
15456 && (fixp->fx_r_type == BFD_RELOC_386_PLT32
15457 || fixp->fx_r_type == BFD_RELOC_X86_64_PLT32)
15458 && symbol_section_p (fixp->fx_addsy))
15459 fixp->fx_r_type = BFD_RELOC_32_PCREL;
15460 if (!object_64bit)
15461 {
15462 if (fixp->fx_r_type == BFD_RELOC_386_GOT32
15463 && fixp->fx_tcbit2)
15464 fixp->fx_r_type = BFD_RELOC_386_GOT32X;
15465 }
15466 }
15467 #endif
15468
15469 return 1;
15470 }
15471
15472 arelent *
15473 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
15474 {
15475 arelent *rel;
15476 bfd_reloc_code_real_type code;
15477
15478 switch (fixp->fx_r_type)
15479 {
15480 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15481 symbolS *sym;
15482
15483 case BFD_RELOC_SIZE32:
15484 case BFD_RELOC_SIZE64:
15485 if (fixp->fx_addsy
15486 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))
15487 && (!fixp->fx_subsy
15488 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))))
15489 sym = fixp->fx_addsy;
15490 else if (fixp->fx_subsy
15491 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))
15492 && (!fixp->fx_addsy
15493 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))))
15494 sym = fixp->fx_subsy;
15495 else
15496 sym = NULL;
15497 if (IS_ELF && sym && S_IS_DEFINED (sym) && !S_IS_EXTERNAL (sym))
15498 {
15499 /* Resolve size relocation against local symbol to size of
15500 the symbol plus addend. */
15501 valueT value = S_GET_SIZE (sym);
15502
15503 if (symbol_get_bfdsym (sym)->flags & BSF_SECTION_SYM)
15504 value = bfd_section_size (S_GET_SEGMENT (sym));
15505 if (sym == fixp->fx_subsy)
15506 {
15507 value = -value;
15508 if (fixp->fx_addsy)
15509 value += S_GET_VALUE (fixp->fx_addsy);
15510 }
15511 else if (fixp->fx_subsy)
15512 value -= S_GET_VALUE (fixp->fx_subsy);
15513 value += fixp->fx_offset;
15514 if (fixp->fx_r_type == BFD_RELOC_SIZE32
15515 && object_64bit
15516 && !fits_in_unsigned_long (value))
15517 as_bad_where (fixp->fx_file, fixp->fx_line,
15518 _("symbol size computation overflow"));
15519 fixp->fx_addsy = NULL;
15520 fixp->fx_subsy = NULL;
15521 md_apply_fix (fixp, (valueT *) &value, NULL);
15522 return NULL;
15523 }
15524 if (!fixp->fx_addsy || fixp->fx_subsy)
15525 {
15526 as_bad_where (fixp->fx_file, fixp->fx_line,
15527 "unsupported expression involving @size");
15528 return NULL;
15529 }
15530 #endif
15531 /* Fall through. */
15532
15533 case BFD_RELOC_X86_64_PLT32:
15534 case BFD_RELOC_X86_64_GOT32:
15535 case BFD_RELOC_X86_64_GOTPCREL:
15536 case BFD_RELOC_X86_64_GOTPCRELX:
15537 case BFD_RELOC_X86_64_REX_GOTPCRELX:
15538 case BFD_RELOC_386_PLT32:
15539 case BFD_RELOC_386_GOT32:
15540 case BFD_RELOC_386_GOT32X:
15541 case BFD_RELOC_386_GOTOFF:
15542 case BFD_RELOC_386_GOTPC:
15543 case BFD_RELOC_386_TLS_GD:
15544 case BFD_RELOC_386_TLS_LDM:
15545 case BFD_RELOC_386_TLS_LDO_32:
15546 case BFD_RELOC_386_TLS_IE_32:
15547 case BFD_RELOC_386_TLS_IE:
15548 case BFD_RELOC_386_TLS_GOTIE:
15549 case BFD_RELOC_386_TLS_LE_32:
15550 case BFD_RELOC_386_TLS_LE:
15551 case BFD_RELOC_386_TLS_GOTDESC:
15552 case BFD_RELOC_386_TLS_DESC_CALL:
15553 case BFD_RELOC_X86_64_TLSGD:
15554 case BFD_RELOC_X86_64_TLSLD:
15555 case BFD_RELOC_X86_64_DTPOFF32:
15556 case BFD_RELOC_X86_64_DTPOFF64:
15557 case BFD_RELOC_X86_64_GOTTPOFF:
15558 case BFD_RELOC_X86_64_TPOFF32:
15559 case BFD_RELOC_X86_64_TPOFF64:
15560 case BFD_RELOC_X86_64_GOTOFF64:
15561 case BFD_RELOC_X86_64_GOTPC32:
15562 case BFD_RELOC_X86_64_GOT64:
15563 case BFD_RELOC_X86_64_GOTPCREL64:
15564 case BFD_RELOC_X86_64_GOTPC64:
15565 case BFD_RELOC_X86_64_GOTPLT64:
15566 case BFD_RELOC_X86_64_PLTOFF64:
15567 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
15568 case BFD_RELOC_X86_64_TLSDESC_CALL:
15569 case BFD_RELOC_RVA:
15570 case BFD_RELOC_VTABLE_ENTRY:
15571 case BFD_RELOC_VTABLE_INHERIT:
15572 #ifdef TE_PE
15573 case BFD_RELOC_32_SECREL:
15574 case BFD_RELOC_16_SECIDX:
15575 #endif
15576 code = fixp->fx_r_type;
15577 break;
15578 case BFD_RELOC_X86_64_32S:
15579 if (!fixp->fx_pcrel)
15580 {
15581 /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32. */
15582 code = fixp->fx_r_type;
15583 break;
15584 }
15585 /* Fall through. */
15586 default:
15587 if (fixp->fx_pcrel)
15588 {
15589 switch (fixp->fx_size)
15590 {
15591 default:
15592 as_bad_where (fixp->fx_file, fixp->fx_line,
15593 _("can not do %d byte pc-relative relocation"),
15594 fixp->fx_size);
15595 code = BFD_RELOC_32_PCREL;
15596 break;
15597 case 1: code = BFD_RELOC_8_PCREL; break;
15598 case 2: code = BFD_RELOC_16_PCREL; break;
15599 case 4: code = BFD_RELOC_32_PCREL; break;
15600 #ifdef BFD64
15601 case 8: code = BFD_RELOC_64_PCREL; break;
15602 #endif
15603 }
15604 }
15605 else
15606 {
15607 switch (fixp->fx_size)
15608 {
15609 default:
15610 as_bad_where (fixp->fx_file, fixp->fx_line,
15611 _("can not do %d byte relocation"),
15612 fixp->fx_size);
15613 code = BFD_RELOC_32;
15614 break;
15615 case 1: code = BFD_RELOC_8; break;
15616 case 2: code = BFD_RELOC_16; break;
15617 case 4: code = BFD_RELOC_32; break;
15618 #ifdef BFD64
15619 case 8: code = BFD_RELOC_64; break;
15620 #endif
15621 }
15622 }
15623 break;
15624 }
15625
15626 if ((code == BFD_RELOC_32
15627 || code == BFD_RELOC_32_PCREL
15628 || code == BFD_RELOC_X86_64_32S)
15629 && GOT_symbol
15630 && fixp->fx_addsy == GOT_symbol)
15631 {
15632 if (!object_64bit)
15633 code = BFD_RELOC_386_GOTPC;
15634 else
15635 code = BFD_RELOC_X86_64_GOTPC32;
15636 }
15637 if ((code == BFD_RELOC_64 || code == BFD_RELOC_64_PCREL)
15638 && GOT_symbol
15639 && fixp->fx_addsy == GOT_symbol)
15640 {
15641 code = BFD_RELOC_X86_64_GOTPC64;
15642 }
15643
15644 rel = XNEW (arelent);
15645 rel->sym_ptr_ptr = XNEW (asymbol *);
15646 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
15647
15648 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
15649
15650 if (!use_rela_relocations)
15651 {
15652 /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
15653 vtable entry to be used in the relocation's section offset. */
15654 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
15655 rel->address = fixp->fx_offset;
15656 #if defined (OBJ_COFF) && defined (TE_PE)
15657 else if (fixp->fx_addsy && S_IS_WEAK (fixp->fx_addsy))
15658 rel->addend = fixp->fx_addnumber - (S_GET_VALUE (fixp->fx_addsy) * 2);
15659 else
15660 #endif
15661 rel->addend = 0;
15662 }
15663 /* Use the rela in 64bit mode. */
15664 else
15665 {
15666 if (disallow_64bit_reloc)
15667 switch (code)
15668 {
15669 case BFD_RELOC_X86_64_DTPOFF64:
15670 case BFD_RELOC_X86_64_TPOFF64:
15671 case BFD_RELOC_64_PCREL:
15672 case BFD_RELOC_X86_64_GOTOFF64:
15673 case BFD_RELOC_X86_64_GOT64:
15674 case BFD_RELOC_X86_64_GOTPCREL64:
15675 case BFD_RELOC_X86_64_GOTPC64:
15676 case BFD_RELOC_X86_64_GOTPLT64:
15677 case BFD_RELOC_X86_64_PLTOFF64:
15678 as_bad_where (fixp->fx_file, fixp->fx_line,
15679 _("cannot represent relocation type %s in x32 mode"),
15680 bfd_get_reloc_code_name (code));
15681 break;
15682 default:
15683 break;
15684 }
15685
15686 if (!fixp->fx_pcrel)
15687 rel->addend = fixp->fx_offset;
15688 else
15689 switch (code)
15690 {
15691 case BFD_RELOC_X86_64_PLT32:
15692 case BFD_RELOC_X86_64_GOT32:
15693 case BFD_RELOC_X86_64_GOTPCREL:
15694 case BFD_RELOC_X86_64_GOTPCRELX:
15695 case BFD_RELOC_X86_64_REX_GOTPCRELX:
15696 case BFD_RELOC_X86_64_TLSGD:
15697 case BFD_RELOC_X86_64_TLSLD:
15698 case BFD_RELOC_X86_64_GOTTPOFF:
15699 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
15700 case BFD_RELOC_X86_64_TLSDESC_CALL:
15701 rel->addend = fixp->fx_offset - fixp->fx_size;
15702 break;
15703 default:
15704 rel->addend = (section->vma
15705 - fixp->fx_size
15706 + fixp->fx_addnumber
15707 + md_pcrel_from (fixp));
15708 break;
15709 }
15710 }
15711
15712 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
15713 if (rel->howto == NULL)
15714 {
15715 as_bad_where (fixp->fx_file, fixp->fx_line,
15716 _("cannot represent relocation type %s"),
15717 bfd_get_reloc_code_name (code));
15718 /* Set howto to a garbage value so that we can keep going. */
15719 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
15720 gas_assert (rel->howto != NULL);
15721 }
15722
15723 return rel;
15724 }
15725
15726 #include "tc-i386-intel.c"
15727
15728 void
15729 tc_x86_parse_to_dw2regnum (expressionS *exp)
15730 {
15731 int saved_naked_reg;
15732 char saved_register_dot;
15733
15734 saved_naked_reg = allow_naked_reg;
15735 allow_naked_reg = 1;
15736 saved_register_dot = register_chars['.'];
15737 register_chars['.'] = '.';
15738 allow_pseudo_reg = 1;
15739 expression_and_evaluate (exp);
15740 allow_pseudo_reg = 0;
15741 register_chars['.'] = saved_register_dot;
15742 allow_naked_reg = saved_naked_reg;
15743
15744 if (exp->X_op == O_register && exp->X_add_number >= 0)
15745 {
15746 if ((addressT) exp->X_add_number < i386_regtab_size)
15747 {
15748 exp->X_op = O_constant;
15749 exp->X_add_number = i386_regtab[exp->X_add_number]
15750 .dw2_regnum[flag_code >> 1];
15751 }
15752 else
15753 exp->X_op = O_illegal;
15754 }
15755 }
15756
15757 void
15758 tc_x86_frame_initial_instructions (void)
15759 {
15760 static unsigned int sp_regno[2];
15761
15762 if (!sp_regno[flag_code >> 1])
15763 {
15764 char *saved_input = input_line_pointer;
15765 char sp[][4] = {"esp", "rsp"};
15766 expressionS exp;
15767
15768 input_line_pointer = sp[flag_code >> 1];
15769 tc_x86_parse_to_dw2regnum (&exp);
15770 gas_assert (exp.X_op == O_constant);
15771 sp_regno[flag_code >> 1] = exp.X_add_number;
15772 input_line_pointer = saved_input;
15773 }
15774
15775 cfi_add_CFA_def_cfa (sp_regno[flag_code >> 1], -x86_cie_data_alignment);
15776 cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
15777 }
15778
15779 int
15780 x86_dwarf2_addr_size (void)
15781 {
15782 #if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
15783 if (x86_elf_abi == X86_64_X32_ABI)
15784 return 4;
15785 #endif
15786 return bfd_arch_bits_per_address (stdoutput) / 8;
15787 }
15788
15789 int
15790 i386_elf_section_type (const char *str, size_t len)
15791 {
15792 if (flag_code == CODE_64BIT
15793 && len == sizeof ("unwind") - 1
15794 && startswith (str, "unwind"))
15795 return SHT_X86_64_UNWIND;
15796
15797 return -1;
15798 }
15799
15800 #ifdef TE_SOLARIS
15801 void
15802 i386_solaris_fix_up_eh_frame (segT sec)
15803 {
15804 if (flag_code == CODE_64BIT)
15805 elf_section_type (sec) = SHT_X86_64_UNWIND;
15806 }
15807 #endif
15808
15809 #ifdef TE_PE
15810 void
15811 tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
15812 {
15813 expressionS exp;
15814
15815 exp.X_op = O_secrel;
15816 exp.X_add_symbol = symbol;
15817 exp.X_add_number = 0;
15818 emit_expr (&exp, size);
15819 }
15820 #endif
15821
15822 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15823 /* For ELF on x86-64, add support for SHF_X86_64_LARGE. */
15824
15825 bfd_vma
15826 x86_64_section_letter (int letter, const char **ptr_msg)
15827 {
15828 if (flag_code == CODE_64BIT)
15829 {
15830 if (letter == 'l')
15831 return SHF_X86_64_LARGE;
15832
15833 *ptr_msg = _("bad .section directive: want a,l,w,x,M,S,G,T in string");
15834 }
15835 else
15836 *ptr_msg = _("bad .section directive: want a,w,x,M,S,G,T in string");
15837 return -1;
15838 }
15839
15840 static void
15841 handle_large_common (int small ATTRIBUTE_UNUSED)
15842 {
15843 if (flag_code != CODE_64BIT)
15844 {
15845 s_comm_internal (0, elf_common_parse);
15846 as_warn (_(".largecomm supported only in 64bit mode, producing .comm"));
15847 }
15848 else
15849 {
15850 static segT lbss_section;
15851 asection *saved_com_section_ptr = elf_com_section_ptr;
15852 asection *saved_bss_section = bss_section;
15853
15854 if (lbss_section == NULL)
15855 {
15856 flagword applicable;
15857 segT seg = now_seg;
15858 subsegT subseg = now_subseg;
15859
15860 /* The .lbss section is for local .largecomm symbols. */
15861 lbss_section = subseg_new (".lbss", 0);
15862 applicable = bfd_applicable_section_flags (stdoutput);
15863 bfd_set_section_flags (lbss_section, applicable & SEC_ALLOC);
15864 seg_info (lbss_section)->bss = 1;
15865
15866 subseg_set (seg, subseg);
15867 }
15868
15869 elf_com_section_ptr = &_bfd_elf_large_com_section;
15870 bss_section = lbss_section;
15871
15872 s_comm_internal (0, elf_common_parse);
15873
15874 elf_com_section_ptr = saved_com_section_ptr;
15875 bss_section = saved_bss_section;
15876 }
15877 }
15878 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */