c6a15216d3b7c9f35d86a8b385859e998487c6bf
[binutils-gdb.git] / gas / config / tc-i386.c
1 /* tc-i386.c -- Assemble code for the Intel 80386
2 Copyright (C) 1989-2023 Free Software Foundation, Inc.
3
4 This file is part of GAS, the GNU Assembler.
5
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
10
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to the Free
18 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
19 02110-1301, USA. */
20
21 /* Intel 80386 machine specific gas.
22 Written by Eliot Dresselhaus (eliot@mgm.mit.edu).
23 x86_64 support by Jan Hubicka (jh@suse.cz)
24 VIA PadLock support by Michal Ludvig (mludvig@suse.cz)
25 Bugs & suggestions are completely welcome. This is free software.
26 Please help us make it better. */
27
28 #include "as.h"
29 #include "safe-ctype.h"
30 #include "subsegs.h"
31 #include "dwarf2dbg.h"
32 #include "dw2gencfi.h"
33 #include "gen-sframe.h"
34 #include "sframe.h"
35 #include "elf/x86-64.h"
36 #include "opcodes/i386-init.h"
37 #include "opcodes/i386-mnem.h"
38 #include <limits.h>
39
40 #ifndef INFER_ADDR_PREFIX
41 #define INFER_ADDR_PREFIX 1
42 #endif
43
44 #ifndef DEFAULT_ARCH
45 #define DEFAULT_ARCH "i386"
46 #endif
47
48 #ifndef INLINE
49 #if __GNUC__ >= 2
50 #define INLINE __inline__
51 #else
52 #define INLINE
53 #endif
54 #endif
55
56 /* Prefixes will be emitted in the order defined below.
57 WAIT_PREFIX must be the first prefix since FWAIT is really is an
58 instruction, and so must come before any prefixes.
59 The preferred prefix order is SEG_PREFIX, ADDR_PREFIX, DATA_PREFIX,
60 REP_PREFIX/HLE_PREFIX, LOCK_PREFIX. */
61 #define WAIT_PREFIX 0
62 #define SEG_PREFIX 1
63 #define ADDR_PREFIX 2
64 #define DATA_PREFIX 3
65 #define REP_PREFIX 4
66 #define HLE_PREFIX REP_PREFIX
67 #define BND_PREFIX REP_PREFIX
68 #define LOCK_PREFIX 5
69 #define REX_PREFIX 6 /* must come last. */
70 #define MAX_PREFIXES 7 /* max prefixes per opcode */
71
72 /* we define the syntax here (modulo base,index,scale syntax) */
73 #define REGISTER_PREFIX '%'
74 #define IMMEDIATE_PREFIX '$'
75 #define ABSOLUTE_PREFIX '*'
76
77 /* these are the instruction mnemonic suffixes in AT&T syntax or
78 memory operand size in Intel syntax. */
79 #define WORD_MNEM_SUFFIX 'w'
80 #define BYTE_MNEM_SUFFIX 'b'
81 #define SHORT_MNEM_SUFFIX 's'
82 #define LONG_MNEM_SUFFIX 'l'
83 #define QWORD_MNEM_SUFFIX 'q'
84
85 #define END_OF_INSN '\0'
86
87 #define OPERAND_TYPE_NONE { .bitfield = { .class = ClassNone } }
88
89 /* This matches the C -> StaticRounding alias in the opcode table. */
90 #define commutative staticrounding
91
92 /*
93 'templates' is for grouping together 'template' structures for opcodes
94 of the same name. This is only used for storing the insns in the grand
95 ole hash table of insns.
96 The templates themselves start at START and range up to (but not including)
97 END.
98 */
99 typedef struct
100 {
101 const insn_template *start;
102 const insn_template *end;
103 }
104 templates;
105
106 /* 386 operand encoding bytes: see 386 book for details of this. */
107 typedef struct
108 {
109 unsigned int regmem; /* codes register or memory operand */
110 unsigned int reg; /* codes register operand (or extended opcode) */
111 unsigned int mode; /* how to interpret regmem & reg */
112 }
113 modrm_byte;
114
115 /* x86-64 extension prefix. */
116 typedef int rex_byte;
117
118 /* 386 opcode byte to code indirect addressing. */
119 typedef struct
120 {
121 unsigned base;
122 unsigned index;
123 unsigned scale;
124 }
125 sib_byte;
126
127 /* x86 arch names, types and features */
128 typedef struct
129 {
130 const char *name; /* arch name */
131 unsigned int len:8; /* arch string length */
132 bool skip:1; /* show_arch should skip this. */
133 enum processor_type type; /* arch type */
134 enum { vsz_none, vsz_set, vsz_reset } vsz; /* vector size control */
135 i386_cpu_flags enable; /* cpu feature enable flags */
136 i386_cpu_flags disable; /* cpu feature disable flags */
137 }
138 arch_entry;
139
140 static void update_code_flag (int, int);
141 static void s_insn (int);
142 static void set_code_flag (int);
143 static void set_16bit_gcc_code_flag (int);
144 static void set_intel_syntax (int);
145 static void set_intel_mnemonic (int);
146 static void set_allow_index_reg (int);
147 static void set_check (int);
148 static void set_cpu_arch (int);
149 #ifdef TE_PE
150 static void pe_directive_secrel (int);
151 static void pe_directive_secidx (int);
152 #endif
153 static void signed_cons (int);
154 static char *output_invalid (int c);
155 static int i386_finalize_immediate (segT, expressionS *, i386_operand_type,
156 const char *);
157 static int i386_finalize_displacement (segT, expressionS *, i386_operand_type,
158 const char *);
159 static int i386_att_operand (char *);
160 static int i386_intel_operand (char *, int);
161 static int i386_intel_simplify (expressionS *);
162 static int i386_intel_parse_name (const char *, expressionS *);
163 static const reg_entry *parse_register (const char *, char **);
164 static const char *parse_insn (const char *, char *, bool);
165 static char *parse_operands (char *, const char *);
166 static void swap_operands (void);
167 static void swap_2_operands (unsigned int, unsigned int);
168 static enum i386_flag_code i386_addressing_mode (void);
169 static void optimize_imm (void);
170 static bool optimize_disp (const insn_template *t);
171 static const insn_template *match_template (char);
172 static int check_string (void);
173 static int process_suffix (void);
174 static int check_byte_reg (void);
175 static int check_long_reg (void);
176 static int check_qword_reg (void);
177 static int check_word_reg (void);
178 static int finalize_imm (void);
179 static int process_operands (void);
180 static const reg_entry *build_modrm_byte (void);
181 static void output_insn (void);
182 static void output_imm (fragS *, offsetT);
183 static void output_disp (fragS *, offsetT);
184 #ifndef I386COFF
185 static void s_bss (int);
186 #endif
187 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
188 static void handle_large_common (int small ATTRIBUTE_UNUSED);
189
190 /* GNU_PROPERTY_X86_ISA_1_USED. */
191 static unsigned int x86_isa_1_used;
192 /* GNU_PROPERTY_X86_FEATURE_2_USED. */
193 static unsigned int x86_feature_2_used;
194 /* Generate x86 used ISA and feature properties. */
195 static unsigned int x86_used_note = DEFAULT_X86_USED_NOTE;
196 #endif
197
198 static const char *default_arch = DEFAULT_ARCH;
199
200 /* parse_register() returns this when a register alias cannot be used. */
201 static const reg_entry bad_reg = { "<bad>", OPERAND_TYPE_NONE, 0, 0,
202 { Dw2Inval, Dw2Inval } };
203
204 static const reg_entry *reg_eax;
205 static const reg_entry *reg_ds;
206 static const reg_entry *reg_es;
207 static const reg_entry *reg_ss;
208 static const reg_entry *reg_st0;
209 static const reg_entry *reg_k0;
210
211 /* VEX prefix. */
212 typedef struct
213 {
214 /* VEX prefix is either 2 byte or 3 byte. EVEX is 4 byte. */
215 unsigned char bytes[4];
216 unsigned int length;
217 /* Destination or source register specifier. */
218 const reg_entry *register_specifier;
219 } vex_prefix;
220
221 /* 'md_assemble ()' gathers together information and puts it into a
222 i386_insn. */
223
224 union i386_op
225 {
226 expressionS *disps;
227 expressionS *imms;
228 const reg_entry *regs;
229 };
230
231 enum i386_error
232 {
233 no_error, /* Must be first. */
234 operand_size_mismatch,
235 operand_type_mismatch,
236 register_type_mismatch,
237 number_of_operands_mismatch,
238 invalid_instruction_suffix,
239 bad_imm4,
240 unsupported_with_intel_mnemonic,
241 unsupported_syntax,
242 unsupported,
243 unsupported_on_arch,
244 unsupported_64bit,
245 invalid_sib_address,
246 invalid_vsib_address,
247 invalid_vector_register_set,
248 invalid_tmm_register_set,
249 invalid_dest_and_src_register_set,
250 unsupported_vector_index_register,
251 unsupported_broadcast,
252 broadcast_needed,
253 unsupported_masking,
254 mask_not_on_destination,
255 no_default_mask,
256 unsupported_rc_sae,
257 invalid_register_operand,
258 internal_error,
259 };
260
261 struct _i386_insn
262 {
263 /* TM holds the template for the insn were currently assembling. */
264 insn_template tm;
265
266 /* SUFFIX holds the instruction size suffix for byte, word, dword
267 or qword, if given. */
268 char suffix;
269
270 /* OPCODE_LENGTH holds the number of base opcode bytes. */
271 unsigned char opcode_length;
272
273 /* OPERANDS gives the number of given operands. */
274 unsigned int operands;
275
276 /* REG_OPERANDS, DISP_OPERANDS, MEM_OPERANDS, IMM_OPERANDS give the number
277 of given register, displacement, memory operands and immediate
278 operands. */
279 unsigned int reg_operands, disp_operands, mem_operands, imm_operands;
280
281 /* TYPES [i] is the type (see above #defines) which tells us how to
282 use OP[i] for the corresponding operand. */
283 i386_operand_type types[MAX_OPERANDS];
284
285 /* Displacement expression, immediate expression, or register for each
286 operand. */
287 union i386_op op[MAX_OPERANDS];
288
289 /* Flags for operands. */
290 unsigned int flags[MAX_OPERANDS];
291 #define Operand_PCrel 1
292 #define Operand_Mem 2
293 #define Operand_Signed 4 /* .insn only */
294
295 /* Relocation type for operand */
296 enum bfd_reloc_code_real reloc[MAX_OPERANDS];
297
298 /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
299 the base index byte below. */
300 const reg_entry *base_reg;
301 const reg_entry *index_reg;
302 unsigned int log2_scale_factor;
303
304 /* SEG gives the seg_entries of this insn. They are zero unless
305 explicit segment overrides are given. */
306 const reg_entry *seg[2];
307
308 /* PREFIX holds all the given prefix opcodes (usually null).
309 PREFIXES is the number of prefix opcodes. */
310 unsigned int prefixes;
311 unsigned char prefix[MAX_PREFIXES];
312
313 /* .insn allows for reserved opcode spaces. */
314 unsigned char insn_opcode_space;
315
316 /* .insn also allows (requires) specifying immediate size. */
317 unsigned char imm_bits[MAX_OPERANDS];
318
319 /* Register is in low 3 bits of opcode. */
320 bool short_form;
321
322 /* The operand to a branch insn indicates an absolute branch. */
323 bool jumpabsolute;
324
325 /* The operand to a branch insn indicates a far branch. */
326 bool far_branch;
327
328 /* There is a memory operand of (%dx) which should be only used
329 with input/output instructions. */
330 bool input_output_operand;
331
332 /* Extended states. */
333 enum
334 {
335 /* Use MMX state. */
336 xstate_mmx = 1 << 0,
337 /* Use XMM state. */
338 xstate_xmm = 1 << 1,
339 /* Use YMM state. */
340 xstate_ymm = 1 << 2 | xstate_xmm,
341 /* Use ZMM state. */
342 xstate_zmm = 1 << 3 | xstate_ymm,
343 /* Use TMM state. */
344 xstate_tmm = 1 << 4,
345 /* Use MASK state. */
346 xstate_mask = 1 << 5
347 } xstate;
348
349 /* Has GOTPC or TLS relocation. */
350 bool has_gotpc_tls_reloc;
351
352 /* RM and SIB are the modrm byte and the sib byte where the
353 addressing modes of this insn are encoded. */
354 modrm_byte rm;
355 rex_byte rex;
356 rex_byte vrex;
357 sib_byte sib;
358 vex_prefix vex;
359
360 /* Masking attributes.
361
362 The struct describes masking, applied to OPERAND in the instruction.
363 REG is a pointer to the corresponding mask register. ZEROING tells
364 whether merging or zeroing mask is used. */
365 struct Mask_Operation
366 {
367 const reg_entry *reg;
368 unsigned int zeroing;
369 /* The operand where this operation is associated. */
370 unsigned int operand;
371 } mask;
372
373 /* Rounding control and SAE attributes. */
374 struct RC_Operation
375 {
376 enum rc_type
377 {
378 rc_none = -1,
379 rne,
380 rd,
381 ru,
382 rz,
383 saeonly
384 } type;
385 /* In Intel syntax the operand modifier form is supposed to be used, but
386 we continue to accept the immediate forms as well. */
387 bool modifier;
388 } rounding;
389
390 /* Broadcasting attributes.
391
392 The struct describes broadcasting, applied to OPERAND. TYPE is
393 expresses the broadcast factor. */
394 struct Broadcast_Operation
395 {
396 /* Type of broadcast: {1to2}, {1to4}, {1to8}, {1to16} or {1to32}. */
397 unsigned int type;
398
399 /* Index of broadcasted operand. */
400 unsigned int operand;
401
402 /* Number of bytes to broadcast. */
403 unsigned int bytes;
404 } broadcast;
405
406 /* Compressed disp8*N attribute. */
407 unsigned int memshift;
408
409 /* Prefer load or store in encoding. */
410 enum
411 {
412 dir_encoding_default = 0,
413 dir_encoding_load,
414 dir_encoding_store,
415 dir_encoding_swap
416 } dir_encoding;
417
418 /* Prefer 8bit, 16bit, 32bit displacement in encoding. */
419 enum
420 {
421 disp_encoding_default = 0,
422 disp_encoding_8bit,
423 disp_encoding_16bit,
424 disp_encoding_32bit
425 } disp_encoding;
426
427 /* Prefer the REX byte in encoding. */
428 bool rex_encoding;
429
430 /* Disable instruction size optimization. */
431 bool no_optimize;
432
433 /* How to encode vector instructions. */
434 enum
435 {
436 vex_encoding_default = 0,
437 vex_encoding_vex,
438 vex_encoding_vex3,
439 vex_encoding_evex,
440 vex_encoding_evex512,
441 vex_encoding_error
442 } vec_encoding;
443
444 /* REP prefix. */
445 const char *rep_prefix;
446
447 /* HLE prefix. */
448 const char *hle_prefix;
449
450 /* Have BND prefix. */
451 const char *bnd_prefix;
452
453 /* Have NOTRACK prefix. */
454 const char *notrack_prefix;
455
456 /* Error message. */
457 enum i386_error error;
458 };
459
460 typedef struct _i386_insn i386_insn;
461
462 /* Link RC type with corresponding string, that'll be looked for in
463 asm. */
464 struct RC_name
465 {
466 enum rc_type type;
467 const char *name;
468 unsigned int len;
469 };
470
471 static const struct RC_name RC_NamesTable[] =
472 {
473 { rne, STRING_COMMA_LEN ("rn-sae") },
474 { rd, STRING_COMMA_LEN ("rd-sae") },
475 { ru, STRING_COMMA_LEN ("ru-sae") },
476 { rz, STRING_COMMA_LEN ("rz-sae") },
477 { saeonly, STRING_COMMA_LEN ("sae") },
478 };
479
480 /* To be indexed by segment register number. */
481 static const unsigned char i386_seg_prefixes[] = {
482 ES_PREFIX_OPCODE,
483 CS_PREFIX_OPCODE,
484 SS_PREFIX_OPCODE,
485 DS_PREFIX_OPCODE,
486 FS_PREFIX_OPCODE,
487 GS_PREFIX_OPCODE
488 };
489
490 /* List of chars besides those in app.c:symbol_chars that can start an
491 operand. Used to prevent the scrubber eating vital white-space. */
492 const char extra_symbol_chars[] = "*%-([{}"
493 #ifdef LEX_AT
494 "@"
495 #endif
496 #ifdef LEX_QM
497 "?"
498 #endif
499 ;
500
501 #if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
502 && !defined (TE_GNU) \
503 && !defined (TE_LINUX) \
504 && !defined (TE_Haiku) \
505 && !defined (TE_FreeBSD) \
506 && !defined (TE_DragonFly) \
507 && !defined (TE_NetBSD))
508 /* This array holds the chars that always start a comment. If the
509 pre-processor is disabled, these aren't very useful. The option
510 --divide will remove '/' from this list. */
511 const char *i386_comment_chars = "#/";
512 #define SVR4_COMMENT_CHARS 1
513 #define PREFIX_SEPARATOR '\\'
514
515 #else
516 const char *i386_comment_chars = "#";
517 #define PREFIX_SEPARATOR '/'
518 #endif
519
520 /* This array holds the chars that only start a comment at the beginning of
521 a line. If the line seems to have the form '# 123 filename'
522 .line and .file directives will appear in the pre-processed output.
523 Note that input_file.c hand checks for '#' at the beginning of the
524 first line of the input file. This is because the compiler outputs
525 #NO_APP at the beginning of its output.
526 Also note that comments started like this one will always work if
527 '/' isn't otherwise defined. */
528 const char line_comment_chars[] = "#/";
529
530 const char line_separator_chars[] = ";";
531
532 /* Chars that can be used to separate mant from exp in floating point
533 nums. */
534 const char EXP_CHARS[] = "eE";
535
536 /* Chars that mean this number is a floating point constant
537 As in 0f12.456
538 or 0d1.2345e12. */
539 const char FLT_CHARS[] = "fFdDxXhHbB";
540
541 /* Tables for lexical analysis. */
542 static char mnemonic_chars[256];
543 static char register_chars[256];
544 static char operand_chars[256];
545
546 /* Lexical macros. */
547 #define is_operand_char(x) (operand_chars[(unsigned char) x])
548 #define is_register_char(x) (register_chars[(unsigned char) x])
549 #define is_space_char(x) ((x) == ' ')
550
551 /* All non-digit non-letter characters that may occur in an operand and
552 which aren't already in extra_symbol_chars[]. */
553 static const char operand_special_chars[] = "$+,)._~/<>|&^!=:@]";
554
555 /* md_assemble() always leaves the strings it's passed unaltered. To
556 effect this we maintain a stack of saved characters that we've smashed
557 with '\0's (indicating end of strings for various sub-fields of the
558 assembler instruction). */
559 static char save_stack[32];
560 static char *save_stack_p;
561 #define END_STRING_AND_SAVE(s) \
562 do { *save_stack_p++ = *(s); *(s) = '\0'; } while (0)
563 #define RESTORE_END_STRING(s) \
564 do { *(s) = *--save_stack_p; } while (0)
565
566 /* The instruction we're assembling. */
567 static i386_insn i;
568
569 /* Possible templates for current insn. */
570 static const templates *current_templates;
571
572 /* Per instruction expressionS buffers: max displacements & immediates. */
573 static expressionS disp_expressions[MAX_MEMORY_OPERANDS];
574 static expressionS im_expressions[MAX_IMMEDIATE_OPERANDS];
575
576 /* Current operand we are working on. */
577 static int this_operand = -1;
578
579 /* Are we processing a .insn directive? */
580 #define dot_insn() (i.tm.mnem_off == MN__insn)
581
582 enum i386_flag_code i386_flag_code;
583 #define flag_code i386_flag_code /* Permit to continue using original name. */
584 static unsigned int object_64bit;
585 static unsigned int disallow_64bit_reloc;
586 static int use_rela_relocations = 0;
587 /* __tls_get_addr/___tls_get_addr symbol for TLS. */
588 static const char *tls_get_addr;
589
590 #if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
591 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
592 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
593
594 /* The ELF ABI to use. */
595 enum x86_elf_abi
596 {
597 I386_ABI,
598 X86_64_ABI,
599 X86_64_X32_ABI
600 };
601
602 static enum x86_elf_abi x86_elf_abi = I386_ABI;
603 #endif
604
605 #if defined (TE_PE) || defined (TE_PEP)
606 /* Use big object file format. */
607 static int use_big_obj = 0;
608 #endif
609
610 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
611 /* 1 if generating code for a shared library. */
612 static int shared = 0;
613
614 unsigned int x86_sframe_cfa_sp_reg;
615 /* The other CFA base register for SFrame stack trace info. */
616 unsigned int x86_sframe_cfa_fp_reg;
617 unsigned int x86_sframe_cfa_ra_reg;
618
619 #endif
620
621 /* 1 for intel syntax,
622 0 if att syntax. */
623 static int intel_syntax = 0;
624
625 static enum x86_64_isa
626 {
627 amd64 = 1, /* AMD64 ISA. */
628 intel64 /* Intel64 ISA. */
629 } isa64;
630
631 /* 1 for intel mnemonic,
632 0 if att mnemonic. */
633 static int intel_mnemonic = !SYSV386_COMPAT;
634
635 /* 1 if pseudo registers are permitted. */
636 static int allow_pseudo_reg = 0;
637
638 /* 1 if register prefix % not required. */
639 static int allow_naked_reg = 0;
640
641 /* 1 if the assembler should add BND prefix for all control-transferring
642 instructions supporting it, even if this prefix wasn't specified
643 explicitly. */
644 static int add_bnd_prefix = 0;
645
646 /* 1 if pseudo index register, eiz/riz, is allowed . */
647 static int allow_index_reg = 0;
648
649 /* 1 if the assembler should ignore LOCK prefix, even if it was
650 specified explicitly. */
651 static int omit_lock_prefix = 0;
652
653 /* 1 if the assembler should encode lfence, mfence, and sfence as
654 "lock addl $0, (%{re}sp)". */
655 static int avoid_fence = 0;
656
657 /* 1 if lfence should be inserted after every load. */
658 static int lfence_after_load = 0;
659
660 /* Non-zero if lfence should be inserted before indirect branch. */
661 static enum lfence_before_indirect_branch_kind
662 {
663 lfence_branch_none = 0,
664 lfence_branch_register,
665 lfence_branch_memory,
666 lfence_branch_all
667 }
668 lfence_before_indirect_branch;
669
670 /* Non-zero if lfence should be inserted before ret. */
671 static enum lfence_before_ret_kind
672 {
673 lfence_before_ret_none = 0,
674 lfence_before_ret_not,
675 lfence_before_ret_or,
676 lfence_before_ret_shl
677 }
678 lfence_before_ret;
679
680 /* Types of previous instruction is .byte or prefix. */
681 static struct
682 {
683 segT seg;
684 const char *file;
685 const char *name;
686 unsigned int line;
687 enum last_insn_kind
688 {
689 last_insn_other = 0,
690 last_insn_directive,
691 last_insn_prefix
692 } kind;
693 } last_insn;
694
695 /* 1 if the assembler should generate relax relocations. */
696
697 static int generate_relax_relocations
698 = DEFAULT_GENERATE_X86_RELAX_RELOCATIONS;
699
700 static enum check_kind
701 {
702 check_none = 0,
703 check_warning,
704 check_error
705 }
706 sse_check, operand_check = check_warning;
707
708 /* Non-zero if branches should be aligned within power of 2 boundary. */
709 static int align_branch_power = 0;
710
711 /* Types of branches to align. */
712 enum align_branch_kind
713 {
714 align_branch_none = 0,
715 align_branch_jcc = 1,
716 align_branch_fused = 2,
717 align_branch_jmp = 3,
718 align_branch_call = 4,
719 align_branch_indirect = 5,
720 align_branch_ret = 6
721 };
722
723 /* Type bits of branches to align. */
724 enum align_branch_bit
725 {
726 align_branch_jcc_bit = 1 << align_branch_jcc,
727 align_branch_fused_bit = 1 << align_branch_fused,
728 align_branch_jmp_bit = 1 << align_branch_jmp,
729 align_branch_call_bit = 1 << align_branch_call,
730 align_branch_indirect_bit = 1 << align_branch_indirect,
731 align_branch_ret_bit = 1 << align_branch_ret
732 };
733
734 static unsigned int align_branch = (align_branch_jcc_bit
735 | align_branch_fused_bit
736 | align_branch_jmp_bit);
737
738 /* Types of condition jump used by macro-fusion. */
739 enum mf_jcc_kind
740 {
741 mf_jcc_jo = 0, /* base opcode 0x70 */
742 mf_jcc_jc, /* base opcode 0x72 */
743 mf_jcc_je, /* base opcode 0x74 */
744 mf_jcc_jna, /* base opcode 0x76 */
745 mf_jcc_js, /* base opcode 0x78 */
746 mf_jcc_jp, /* base opcode 0x7a */
747 mf_jcc_jl, /* base opcode 0x7c */
748 mf_jcc_jle, /* base opcode 0x7e */
749 };
750
751 /* Types of compare flag-modifying insntructions used by macro-fusion. */
752 enum mf_cmp_kind
753 {
754 mf_cmp_test_and, /* test/cmp */
755 mf_cmp_alu_cmp, /* add/sub/cmp */
756 mf_cmp_incdec /* inc/dec */
757 };
758
759 /* The maximum padding size for fused jcc. CMP like instruction can
760 be 9 bytes and jcc can be 6 bytes. Leave room just in case for
761 prefixes. */
762 #define MAX_FUSED_JCC_PADDING_SIZE 20
763
764 /* The maximum number of prefixes added for an instruction. */
765 static unsigned int align_branch_prefix_size = 5;
766
767 /* Optimization:
768 1. Clear the REX_W bit with register operand if possible.
769 2. Above plus use 128bit vector instruction to clear the full vector
770 register.
771 */
772 static int optimize = 0;
773
774 /* Optimization:
775 1. Clear the REX_W bit with register operand if possible.
776 2. Above plus use 128bit vector instruction to clear the full vector
777 register.
778 3. Above plus optimize "test{q,l,w} $imm8,%r{64,32,16}" to
779 "testb $imm7,%r8".
780 */
781 static int optimize_for_space = 0;
782
783 /* Register prefix used for error message. */
784 static const char *register_prefix = "%";
785
786 /* Used in 16 bit gcc mode to add an l suffix to call, ret, enter,
787 leave, push, and pop instructions so that gcc has the same stack
788 frame as in 32 bit mode. */
789 static char stackop_size = '\0';
790
791 /* Non-zero to optimize code alignment. */
792 int optimize_align_code = 1;
793
794 /* Non-zero to quieten some warnings. */
795 static int quiet_warnings = 0;
796
797 /* Guard to avoid repeated warnings about non-16-bit code on 16-bit CPUs. */
798 static bool pre_386_16bit_warned;
799
800 /* CPU name. */
801 static const char *cpu_arch_name = NULL;
802 static char *cpu_sub_arch_name = NULL;
803
804 /* CPU feature flags. */
805 i386_cpu_flags cpu_arch_flags = CPU_UNKNOWN_FLAGS;
806
807 /* ISA extensions available in 64-bit mode only. */
808 static const i386_cpu_flags cpu_64_flags = CPU_ANY_64_FLAGS;
809
810 /* If we have selected a cpu we are generating instructions for. */
811 static int cpu_arch_tune_set = 0;
812
813 /* Cpu we are generating instructions for. */
814 enum processor_type cpu_arch_tune = PROCESSOR_UNKNOWN;
815
816 /* CPU instruction set architecture used. */
817 enum processor_type cpu_arch_isa = PROCESSOR_UNKNOWN;
818
819 /* CPU feature flags of instruction set architecture used. */
820 i386_cpu_flags cpu_arch_isa_flags;
821
822 /* If set, conditional jumps are not automatically promoted to handle
823 larger than a byte offset. */
824 static bool no_cond_jump_promotion = false;
825
826 /* This will be set from an expression parser hook if there's any
827 applicable operator involved in an expression. */
828 static enum {
829 expr_operator_none,
830 expr_operator_present,
831 expr_large_value,
832 } expr_mode;
833
834 /* Encode SSE instructions with VEX prefix. */
835 static unsigned int sse2avx;
836
837 /* Encode aligned vector move as unaligned vector move. */
838 static unsigned int use_unaligned_vector_move;
839
840 /* Maximum permitted vector size. */
841 #define VSZ_DEFAULT VSZ512
842 static unsigned int vector_size = VSZ_DEFAULT;
843
844 /* Encode scalar AVX instructions with specific vector length. */
845 static enum
846 {
847 vex128 = 0,
848 vex256
849 } avxscalar;
850
851 /* Encode VEX WIG instructions with specific vex.w. */
852 static enum
853 {
854 vexw0 = 0,
855 vexw1
856 } vexwig;
857
858 /* Encode scalar EVEX LIG instructions with specific vector length. */
859 static enum
860 {
861 evexl128 = 0,
862 evexl256,
863 evexl512
864 } evexlig;
865
866 /* Encode EVEX WIG instructions with specific evex.w. */
867 static enum
868 {
869 evexw0 = 0,
870 evexw1
871 } evexwig;
872
873 /* Value to encode in EVEX RC bits, for SAE-only instructions. */
874 static enum rc_type evexrcig = rne;
875
876 /* Pre-defined "_GLOBAL_OFFSET_TABLE_". */
877 static symbolS *GOT_symbol;
878
879 /* The dwarf2 return column, adjusted for 32 or 64 bit. */
880 unsigned int x86_dwarf2_return_column;
881
882 /* The dwarf2 data alignment, adjusted for 32 or 64 bit. */
883 int x86_cie_data_alignment;
884
885 /* Interface to relax_segment.
886 There are 3 major relax states for 386 jump insns because the
887 different types of jumps add different sizes to frags when we're
888 figuring out what sort of jump to choose to reach a given label.
889
890 BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING are used to align
891 branches which are handled by md_estimate_size_before_relax() and
892 i386_generic_table_relax_frag(). */
893
894 /* Types. */
895 #define UNCOND_JUMP 0
896 #define COND_JUMP 1
897 #define COND_JUMP86 2
898 #define BRANCH_PADDING 3
899 #define BRANCH_PREFIX 4
900 #define FUSED_JCC_PADDING 5
901
902 /* Sizes. */
903 #define CODE16 1
904 #define SMALL 0
905 #define SMALL16 (SMALL | CODE16)
906 #define BIG 2
907 #define BIG16 (BIG | CODE16)
908
909 #ifndef INLINE
910 #ifdef __GNUC__
911 #define INLINE __inline__
912 #else
913 #define INLINE
914 #endif
915 #endif
916
917 #define ENCODE_RELAX_STATE(type, size) \
918 ((relax_substateT) (((type) << 2) | (size)))
919 #define TYPE_FROM_RELAX_STATE(s) \
920 ((s) >> 2)
921 #define DISP_SIZE_FROM_RELAX_STATE(s) \
922 ((((s) & 3) == BIG ? 4 : (((s) & 3) == BIG16 ? 2 : 1)))
923
924 /* This table is used by relax_frag to promote short jumps to long
925 ones where necessary. SMALL (short) jumps may be promoted to BIG
926 (32 bit long) ones, and SMALL16 jumps to BIG16 (16 bit long). We
927 don't allow a short jump in a 32 bit code segment to be promoted to
928 a 16 bit offset jump because it's slower (requires data size
929 prefix), and doesn't work, unless the destination is in the bottom
930 64k of the code segment (The top 16 bits of eip are zeroed). */
931
932 const relax_typeS md_relax_table[] =
933 {
934 /* The fields are:
935 1) most positive reach of this state,
936 2) most negative reach of this state,
937 3) how many bytes this mode will have in the variable part of the frag
938 4) which index into the table to try if we can't fit into this one. */
939
940 /* UNCOND_JUMP states. */
941 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG)},
942 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16)},
943 /* dword jmp adds 4 bytes to frag:
944 0 extra opcode bytes, 4 displacement bytes. */
945 {0, 0, 4, 0},
946 /* word jmp adds 2 byte2 to frag:
947 0 extra opcode bytes, 2 displacement bytes. */
948 {0, 0, 2, 0},
949
950 /* COND_JUMP states. */
951 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG)},
952 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG16)},
953 /* dword conditionals adds 5 bytes to frag:
954 1 extra opcode byte, 4 displacement bytes. */
955 {0, 0, 5, 0},
956 /* word conditionals add 3 bytes to frag:
957 1 extra opcode byte, 2 displacement bytes. */
958 {0, 0, 3, 0},
959
960 /* COND_JUMP86 states. */
961 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG)},
962 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG16)},
963 /* dword conditionals adds 5 bytes to frag:
964 1 extra opcode byte, 4 displacement bytes. */
965 {0, 0, 5, 0},
966 /* word conditionals add 4 bytes to frag:
967 1 displacement byte and a 3 byte long branch insn. */
968 {0, 0, 4, 0}
969 };
970
971 #define ARCH(n, t, f, s) \
972 { STRING_COMMA_LEN (#n), s, PROCESSOR_ ## t, vsz_none, CPU_ ## f ## _FLAGS, \
973 CPU_NONE_FLAGS }
974 #define SUBARCH(n, e, d, s) \
975 { STRING_COMMA_LEN (#n), s, PROCESSOR_NONE, vsz_none, CPU_ ## e ## _FLAGS, \
976 CPU_ ## d ## _FLAGS }
977 #define VECARCH(n, e, d, v) \
978 { STRING_COMMA_LEN (#n), false, PROCESSOR_NONE, vsz_ ## v, \
979 CPU_ ## e ## _FLAGS, CPU_ ## d ## _FLAGS }
980
981 static const arch_entry cpu_arch[] =
982 {
983 /* Do not replace the first two entries - i386_target_format() and
984 set_cpu_arch() rely on them being there in this order. */
985 ARCH (generic32, GENERIC32, GENERIC32, false),
986 ARCH (generic64, GENERIC64, GENERIC64, false),
987 ARCH (i8086, UNKNOWN, NONE, false),
988 ARCH (i186, UNKNOWN, 186, false),
989 ARCH (i286, UNKNOWN, 286, false),
990 ARCH (i386, I386, 386, false),
991 ARCH (i486, I486, 486, false),
992 ARCH (i586, PENTIUM, 586, false),
993 ARCH (pentium, PENTIUM, 586, false),
994 ARCH (i686, I686, 686, false),
995 ARCH (pentiumpro, PENTIUMPRO, PENTIUMPRO, false),
996 ARCH (pentiumii, PENTIUMPRO, P2, false),
997 ARCH (pentiumiii, PENTIUMPRO, P3, false),
998 ARCH (pentium4, PENTIUM4, P4, false),
999 ARCH (prescott, NOCONA, CORE, false),
1000 ARCH (nocona, NOCONA, NOCONA, false),
1001 ARCH (yonah, CORE, CORE, true),
1002 ARCH (core, CORE, CORE, false),
1003 ARCH (merom, CORE2, CORE2, true),
1004 ARCH (core2, CORE2, CORE2, false),
1005 ARCH (corei7, COREI7, COREI7, false),
1006 ARCH (iamcu, IAMCU, IAMCU, false),
1007 ARCH (k6, K6, K6, false),
1008 ARCH (k6_2, K6, K6_2, false),
1009 ARCH (athlon, ATHLON, ATHLON, false),
1010 ARCH (sledgehammer, K8, K8, true),
1011 ARCH (opteron, K8, K8, false),
1012 ARCH (k8, K8, K8, false),
1013 ARCH (amdfam10, AMDFAM10, AMDFAM10, false),
1014 ARCH (bdver1, BD, BDVER1, false),
1015 ARCH (bdver2, BD, BDVER2, false),
1016 ARCH (bdver3, BD, BDVER3, false),
1017 ARCH (bdver4, BD, BDVER4, false),
1018 ARCH (znver1, ZNVER, ZNVER1, false),
1019 ARCH (znver2, ZNVER, ZNVER2, false),
1020 ARCH (znver3, ZNVER, ZNVER3, false),
1021 ARCH (znver4, ZNVER, ZNVER4, false),
1022 ARCH (btver1, BT, BTVER1, false),
1023 ARCH (btver2, BT, BTVER2, false),
1024
1025 SUBARCH (8087, 8087, ANY_8087, false),
1026 SUBARCH (87, NONE, ANY_8087, false), /* Disable only! */
1027 SUBARCH (287, 287, ANY_287, false),
1028 SUBARCH (387, 387, ANY_387, false),
1029 SUBARCH (687, 687, ANY_687, false),
1030 SUBARCH (cmov, CMOV, CMOV, false),
1031 SUBARCH (fxsr, FXSR, ANY_FXSR, false),
1032 SUBARCH (mmx, MMX, ANY_MMX, false),
1033 SUBARCH (sse, SSE, ANY_SSE, false),
1034 SUBARCH (sse2, SSE2, ANY_SSE2, false),
1035 SUBARCH (sse3, SSE3, ANY_SSE3, false),
1036 SUBARCH (sse4a, SSE4A, ANY_SSE4A, false),
1037 SUBARCH (ssse3, SSSE3, ANY_SSSE3, false),
1038 SUBARCH (sse4.1, SSE4_1, ANY_SSE4_1, false),
1039 SUBARCH (sse4.2, SSE4_2, ANY_SSE4_2, false),
1040 SUBARCH (sse4, SSE4_2, ANY_SSE4_1, false),
1041 VECARCH (avx, AVX, ANY_AVX, reset),
1042 VECARCH (avx2, AVX2, ANY_AVX2, reset),
1043 VECARCH (avx512f, AVX512F, ANY_AVX512F, reset),
1044 VECARCH (avx512cd, AVX512CD, ANY_AVX512CD, reset),
1045 VECARCH (avx512er, AVX512ER, ANY_AVX512ER, reset),
1046 VECARCH (avx512pf, AVX512PF, ANY_AVX512PF, reset),
1047 VECARCH (avx512dq, AVX512DQ, ANY_AVX512DQ, reset),
1048 VECARCH (avx512bw, AVX512BW, ANY_AVX512BW, reset),
1049 VECARCH (avx512vl, AVX512VL, ANY_AVX512VL, reset),
1050 SUBARCH (monitor, MONITOR, MONITOR, false),
1051 SUBARCH (vmx, VMX, ANY_VMX, false),
1052 SUBARCH (vmfunc, VMFUNC, ANY_VMFUNC, false),
1053 SUBARCH (smx, SMX, SMX, false),
1054 SUBARCH (xsave, XSAVE, ANY_XSAVE, false),
1055 SUBARCH (xsaveopt, XSAVEOPT, ANY_XSAVEOPT, false),
1056 SUBARCH (xsavec, XSAVEC, ANY_XSAVEC, false),
1057 SUBARCH (xsaves, XSAVES, ANY_XSAVES, false),
1058 SUBARCH (aes, AES, ANY_AES, false),
1059 SUBARCH (pclmul, PCLMULQDQ, ANY_PCLMULQDQ, false),
1060 SUBARCH (clmul, PCLMULQDQ, ANY_PCLMULQDQ, true),
1061 SUBARCH (fsgsbase, FSGSBASE, FSGSBASE, false),
1062 SUBARCH (rdrnd, RDRND, RDRND, false),
1063 SUBARCH (f16c, F16C, ANY_F16C, false),
1064 SUBARCH (bmi2, BMI2, BMI2, false),
1065 SUBARCH (fma, FMA, ANY_FMA, false),
1066 SUBARCH (fma4, FMA4, ANY_FMA4, false),
1067 SUBARCH (xop, XOP, ANY_XOP, false),
1068 SUBARCH (lwp, LWP, ANY_LWP, false),
1069 SUBARCH (movbe, MOVBE, MOVBE, false),
1070 SUBARCH (cx16, CX16, CX16, false),
1071 SUBARCH (lahf_sahf, LAHF_SAHF, LAHF_SAHF, false),
1072 SUBARCH (ept, EPT, ANY_EPT, false),
1073 SUBARCH (lzcnt, LZCNT, LZCNT, false),
1074 SUBARCH (popcnt, POPCNT, POPCNT, false),
1075 SUBARCH (hle, HLE, HLE, false),
1076 SUBARCH (rtm, RTM, ANY_RTM, false),
1077 SUBARCH (tsx, TSX, TSX, false),
1078 SUBARCH (invpcid, INVPCID, INVPCID, false),
1079 SUBARCH (clflush, CLFLUSH, CLFLUSH, false),
1080 SUBARCH (nop, NOP, NOP, false),
1081 SUBARCH (syscall, SYSCALL, SYSCALL, false),
1082 SUBARCH (rdtscp, RDTSCP, RDTSCP, false),
1083 SUBARCH (3dnow, 3DNOW, ANY_3DNOW, false),
1084 SUBARCH (3dnowa, 3DNOWA, ANY_3DNOWA, false),
1085 SUBARCH (padlock, PADLOCK, PADLOCK, false),
1086 SUBARCH (pacifica, SVME, ANY_SVME, true),
1087 SUBARCH (svme, SVME, ANY_SVME, false),
1088 SUBARCH (abm, ABM, ABM, false),
1089 SUBARCH (bmi, BMI, BMI, false),
1090 SUBARCH (tbm, TBM, TBM, false),
1091 SUBARCH (adx, ADX, ADX, false),
1092 SUBARCH (rdseed, RDSEED, RDSEED, false),
1093 SUBARCH (prfchw, PRFCHW, PRFCHW, false),
1094 SUBARCH (smap, SMAP, SMAP, false),
1095 SUBARCH (mpx, MPX, ANY_MPX, false),
1096 SUBARCH (sha, SHA, ANY_SHA, false),
1097 SUBARCH (clflushopt, CLFLUSHOPT, CLFLUSHOPT, false),
1098 SUBARCH (prefetchwt1, PREFETCHWT1, PREFETCHWT1, false),
1099 SUBARCH (se1, SE1, SE1, false),
1100 SUBARCH (clwb, CLWB, CLWB, false),
1101 VECARCH (avx512ifma, AVX512IFMA, ANY_AVX512IFMA, reset),
1102 VECARCH (avx512vbmi, AVX512VBMI, ANY_AVX512VBMI, reset),
1103 VECARCH (avx512_4fmaps, AVX512_4FMAPS, ANY_AVX512_4FMAPS, reset),
1104 VECARCH (avx512_4vnniw, AVX512_4VNNIW, ANY_AVX512_4VNNIW, reset),
1105 VECARCH (avx512_vpopcntdq, AVX512_VPOPCNTDQ, ANY_AVX512_VPOPCNTDQ, reset),
1106 VECARCH (avx512_vbmi2, AVX512_VBMI2, ANY_AVX512_VBMI2, reset),
1107 VECARCH (avx512_vnni, AVX512_VNNI, ANY_AVX512_VNNI, reset),
1108 VECARCH (avx512_bitalg, AVX512_BITALG, ANY_AVX512_BITALG, reset),
1109 VECARCH (avx_vnni, AVX_VNNI, ANY_AVX_VNNI, reset),
1110 SUBARCH (clzero, CLZERO, CLZERO, false),
1111 SUBARCH (mwaitx, MWAITX, MWAITX, false),
1112 SUBARCH (ospke, OSPKE, ANY_OSPKE, false),
1113 SUBARCH (rdpid, RDPID, RDPID, false),
1114 SUBARCH (ptwrite, PTWRITE, PTWRITE, false),
1115 SUBARCH (ibt, IBT, IBT, false),
1116 SUBARCH (shstk, SHSTK, SHSTK, false),
1117 SUBARCH (gfni, GFNI, ANY_GFNI, false),
1118 VECARCH (vaes, VAES, ANY_VAES, reset),
1119 VECARCH (vpclmulqdq, VPCLMULQDQ, ANY_VPCLMULQDQ, reset),
1120 SUBARCH (wbnoinvd, WBNOINVD, WBNOINVD, false),
1121 SUBARCH (pconfig, PCONFIG, PCONFIG, false),
1122 SUBARCH (waitpkg, WAITPKG, WAITPKG, false),
1123 SUBARCH (cldemote, CLDEMOTE, CLDEMOTE, false),
1124 SUBARCH (amx_int8, AMX_INT8, ANY_AMX_INT8, false),
1125 SUBARCH (amx_bf16, AMX_BF16, ANY_AMX_BF16, false),
1126 SUBARCH (amx_fp16, AMX_FP16, ANY_AMX_FP16, false),
1127 SUBARCH (amx_complex, AMX_COMPLEX, ANY_AMX_COMPLEX, false),
1128 SUBARCH (amx_tile, AMX_TILE, ANY_AMX_TILE, false),
1129 SUBARCH (movdiri, MOVDIRI, MOVDIRI, false),
1130 SUBARCH (movdir64b, MOVDIR64B, MOVDIR64B, false),
1131 VECARCH (avx512_bf16, AVX512_BF16, ANY_AVX512_BF16, reset),
1132 VECARCH (avx512_vp2intersect, AVX512_VP2INTERSECT,
1133 ANY_AVX512_VP2INTERSECT, reset),
1134 SUBARCH (tdx, TDX, TDX, false),
1135 SUBARCH (enqcmd, ENQCMD, ENQCMD, false),
1136 SUBARCH (serialize, SERIALIZE, SERIALIZE, false),
1137 SUBARCH (rdpru, RDPRU, RDPRU, false),
1138 SUBARCH (mcommit, MCOMMIT, MCOMMIT, false),
1139 SUBARCH (sev_es, SEV_ES, ANY_SEV_ES, false),
1140 SUBARCH (tsxldtrk, TSXLDTRK, ANY_TSXLDTRK, false),
1141 SUBARCH (kl, KL, ANY_KL, false),
1142 SUBARCH (widekl, WIDEKL, ANY_WIDEKL, false),
1143 SUBARCH (uintr, UINTR, UINTR, false),
1144 SUBARCH (hreset, HRESET, HRESET, false),
1145 VECARCH (avx512_fp16, AVX512_FP16, ANY_AVX512_FP16, reset),
1146 SUBARCH (prefetchi, PREFETCHI, PREFETCHI, false),
1147 VECARCH (avx_ifma, AVX_IFMA, ANY_AVX_IFMA, reset),
1148 VECARCH (avx_vnni_int8, AVX_VNNI_INT8, ANY_AVX_VNNI_INT8, reset),
1149 SUBARCH (cmpccxadd, CMPCCXADD, CMPCCXADD, false),
1150 SUBARCH (wrmsrns, WRMSRNS, WRMSRNS, false),
1151 SUBARCH (msrlist, MSRLIST, MSRLIST, false),
1152 VECARCH (avx_ne_convert, AVX_NE_CONVERT, ANY_AVX_NE_CONVERT, reset),
1153 SUBARCH (rao_int, RAO_INT, RAO_INT, false),
1154 SUBARCH (rmpquery, RMPQUERY, ANY_RMPQUERY, false),
1155 SUBARCH (fred, FRED, ANY_FRED, false),
1156 SUBARCH (lkgs, LKGS, ANY_LKGS, false),
1157 VECARCH (avx_vnni_int16, AVX_VNNI_INT16, ANY_AVX_VNNI_INT16, reset),
1158 VECARCH (sha512, SHA512, ANY_SHA512, reset),
1159 VECARCH (sm3, SM3, ANY_SM3, reset),
1160 VECARCH (sm4, SM4, ANY_SM4, reset),
1161 SUBARCH (pbndkb, PBNDKB, PBNDKB, false),
1162 VECARCH (avx10.1, AVX10_1, ANY_AVX512F, set),
1163 SUBARCH (user_msr, USER_MSR, USER_MSR, false),
1164 };
1165
1166 #undef SUBARCH
1167 #undef ARCH
1168
1169 #ifdef I386COFF
1170 /* Like s_lcomm_internal in gas/read.c but the alignment string
1171 is allowed to be optional. */
1172
1173 static symbolS *
1174 pe_lcomm_internal (int needs_align, symbolS *symbolP, addressT size)
1175 {
1176 addressT align = 0;
1177
1178 SKIP_WHITESPACE ();
1179
1180 if (needs_align
1181 && *input_line_pointer == ',')
1182 {
1183 align = parse_align (needs_align - 1);
1184
1185 if (align == (addressT) -1)
1186 return NULL;
1187 }
1188 else
1189 {
1190 if (size >= 8)
1191 align = 3;
1192 else if (size >= 4)
1193 align = 2;
1194 else if (size >= 2)
1195 align = 1;
1196 else
1197 align = 0;
1198 }
1199
1200 bss_alloc (symbolP, size, align);
1201 return symbolP;
1202 }
1203
1204 static void
1205 pe_lcomm (int needs_align)
1206 {
1207 s_comm_internal (needs_align * 2, pe_lcomm_internal);
1208 }
1209 #endif
1210
1211 const pseudo_typeS md_pseudo_table[] =
1212 {
1213 #if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
1214 {"align", s_align_bytes, 0},
1215 #else
1216 {"align", s_align_ptwo, 0},
1217 #endif
1218 {"arch", set_cpu_arch, 0},
1219 #ifndef I386COFF
1220 {"bss", s_bss, 0},
1221 #else
1222 {"lcomm", pe_lcomm, 1},
1223 #endif
1224 {"ffloat", float_cons, 'f'},
1225 {"dfloat", float_cons, 'd'},
1226 {"tfloat", float_cons, 'x'},
1227 {"hfloat", float_cons, 'h'},
1228 {"bfloat16", float_cons, 'b'},
1229 {"value", cons, 2},
1230 {"slong", signed_cons, 4},
1231 {"insn", s_insn, 0},
1232 {"noopt", s_ignore, 0},
1233 {"optim", s_ignore, 0},
1234 {"code16gcc", set_16bit_gcc_code_flag, CODE_16BIT},
1235 {"code16", set_code_flag, CODE_16BIT},
1236 {"code32", set_code_flag, CODE_32BIT},
1237 #ifdef BFD64
1238 {"code64", set_code_flag, CODE_64BIT},
1239 #endif
1240 {"intel_syntax", set_intel_syntax, 1},
1241 {"att_syntax", set_intel_syntax, 0},
1242 {"intel_mnemonic", set_intel_mnemonic, 1},
1243 {"att_mnemonic", set_intel_mnemonic, 0},
1244 {"allow_index_reg", set_allow_index_reg, 1},
1245 {"disallow_index_reg", set_allow_index_reg, 0},
1246 {"sse_check", set_check, 0},
1247 {"operand_check", set_check, 1},
1248 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1249 {"largecomm", handle_large_common, 0},
1250 #else
1251 {"file", dwarf2_directive_file, 0},
1252 {"loc", dwarf2_directive_loc, 0},
1253 {"loc_mark_labels", dwarf2_directive_loc_mark_labels, 0},
1254 #endif
1255 #ifdef TE_PE
1256 {"secrel32", pe_directive_secrel, 0},
1257 {"secidx", pe_directive_secidx, 0},
1258 #endif
1259 {0, 0, 0}
1260 };
1261
1262 /* For interface with expression (). */
1263 extern char *input_line_pointer;
1264
1265 /* Hash table for instruction mnemonic lookup. */
1266 static htab_t op_hash;
1267
1268 /* Hash table for register lookup. */
1269 static htab_t reg_hash;
1270 \f
1271 /* Various efficient no-op patterns for aligning code labels.
1272 Note: Don't try to assemble the instructions in the comments.
1273 0L and 0w are not legal. */
1274 static const unsigned char f32_1[] =
1275 {0x90}; /* nop */
1276 static const unsigned char f32_2[] =
1277 {0x66,0x90}; /* xchg %ax,%ax */
1278 static const unsigned char f32_3[] =
1279 {0x8d,0x76,0x00}; /* leal 0(%esi),%esi */
1280 #define f32_4 (f32_5 + 1) /* leal 0(%esi,%eiz),%esi */
1281 static const unsigned char f32_5[] =
1282 {0x2e,0x8d,0x74,0x26,0x00}; /* leal %cs:0(%esi,%eiz),%esi */
1283 static const unsigned char f32_6[] =
1284 {0x8d,0xb6,0x00,0x00,0x00,0x00}; /* leal 0L(%esi),%esi */
1285 #define f32_7 (f32_8 + 1) /* leal 0L(%esi,%eiz),%esi */
1286 static const unsigned char f32_8[] =
1287 {0x2e,0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal %cs:0L(%esi,%eiz),%esi */
1288 static const unsigned char f64_3[] =
1289 {0x48,0x89,0xf6}; /* mov %rsi,%rsi */
1290 static const unsigned char f64_4[] =
1291 {0x48,0x8d,0x76,0x00}; /* lea 0(%rsi),%rsi */
1292 #define f64_5 (f64_6 + 1) /* lea 0(%rsi,%riz),%rsi */
1293 static const unsigned char f64_6[] =
1294 {0x2e,0x48,0x8d,0x74,0x26,0x00}; /* lea %cs:0(%rsi,%riz),%rsi */
1295 static const unsigned char f64_7[] =
1296 {0x48,0x8d,0xb6,0x00,0x00,0x00,0x00}; /* lea 0L(%rsi),%rsi */
1297 #define f64_8 (f64_9 + 1) /* lea 0L(%rsi,%riz),%rsi */
1298 static const unsigned char f64_9[] =
1299 {0x2e,0x48,0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* lea %cs:0L(%rsi,%riz),%rsi */
1300 #define f16_2 (f64_3 + 1) /* mov %si,%si */
1301 static const unsigned char f16_3[] =
1302 {0x8d,0x74,0x00}; /* lea 0(%si),%si */
1303 #define f16_4 (f16_5 + 1) /* lea 0W(%si),%si */
1304 static const unsigned char f16_5[] =
1305 {0x2e,0x8d,0xb4,0x00,0x00}; /* lea %cs:0W(%si),%si */
1306 static const unsigned char jump_disp8[] =
1307 {0xeb}; /* jmp disp8 */
1308 static const unsigned char jump32_disp32[] =
1309 {0xe9}; /* jmp disp32 */
1310 static const unsigned char jump16_disp32[] =
1311 {0x66,0xe9}; /* jmp disp32 */
1312 /* 32-bit NOPs patterns. */
1313 static const unsigned char *const f32_patt[] = {
1314 f32_1, f32_2, f32_3, f32_4, f32_5, f32_6, f32_7, f32_8
1315 };
1316 /* 64-bit NOPs patterns. */
1317 static const unsigned char *const f64_patt[] = {
1318 f32_1, f32_2, f64_3, f64_4, f64_5, f64_6, f64_7, f64_8, f64_9
1319 };
1320 /* 16-bit NOPs patterns. */
1321 static const unsigned char *const f16_patt[] = {
1322 f32_1, f16_2, f16_3, f16_4, f16_5
1323 };
1324 /* nopl (%[re]ax) */
1325 static const unsigned char alt_3[] =
1326 {0x0f,0x1f,0x00};
1327 /* nopl 0(%[re]ax) */
1328 static const unsigned char alt_4[] =
1329 {0x0f,0x1f,0x40,0x00};
1330 /* nopl 0(%[re]ax,%[re]ax,1) */
1331 #define alt_5 (alt_6 + 1)
1332 /* nopw 0(%[re]ax,%[re]ax,1) */
1333 static const unsigned char alt_6[] =
1334 {0x66,0x0f,0x1f,0x44,0x00,0x00};
1335 /* nopl 0L(%[re]ax) */
1336 static const unsigned char alt_7[] =
1337 {0x0f,0x1f,0x80,0x00,0x00,0x00,0x00};
1338 /* nopl 0L(%[re]ax,%[re]ax,1) */
1339 #define alt_8 (alt_9 + 1)
1340 /* nopw 0L(%[re]ax,%[re]ax,1) */
1341 static const unsigned char alt_9[] =
1342 {0x66,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1343 /* nopw %cs:0L(%[re]ax,%[re]ax,1) */
1344 #define alt_10 (alt_11 + 1)
1345 /* data16 nopw %cs:0L(%eax,%eax,1) */
1346 static const unsigned char alt_11[] =
1347 {0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1348 /* 32-bit and 64-bit NOPs patterns. */
1349 static const unsigned char *const alt_patt[] = {
1350 f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
1351 alt_9, alt_10, alt_11
1352 };
1353
1354 /* Genenerate COUNT bytes of NOPs to WHERE from PATT with the maximum
1355 size of a single NOP instruction MAX_SINGLE_NOP_SIZE. */
1356
1357 static void
1358 i386_output_nops (char *where, const unsigned char *const *patt,
1359 int count, int max_single_nop_size)
1360
1361 {
1362 /* Place the longer NOP first. */
1363 int last;
1364 int offset;
1365 const unsigned char *nops;
1366
1367 if (max_single_nop_size < 1)
1368 {
1369 as_fatal (_("i386_output_nops called to generate nops of at most %d bytes!"),
1370 max_single_nop_size);
1371 return;
1372 }
1373
1374 nops = patt[max_single_nop_size - 1];
1375 last = count % max_single_nop_size;
1376
1377 count -= last;
1378 for (offset = 0; offset < count; offset += max_single_nop_size)
1379 memcpy (where + offset, nops, max_single_nop_size);
1380
1381 if (last)
1382 {
1383 nops = patt[last - 1];
1384 memcpy (where + offset, nops, last);
1385 }
1386 }
1387
1388 static INLINE int
1389 fits_in_imm7 (offsetT num)
1390 {
1391 return (num & 0x7f) == num;
1392 }
1393
1394 static INLINE int
1395 fits_in_imm31 (offsetT num)
1396 {
1397 return (num & 0x7fffffff) == num;
1398 }
1399
1400 /* Genenerate COUNT bytes of NOPs to WHERE with the maximum size of a
1401 single NOP instruction LIMIT. */
1402
1403 void
1404 i386_generate_nops (fragS *fragP, char *where, offsetT count, int limit)
1405 {
1406 const unsigned char *const *patt = NULL;
1407 int max_single_nop_size;
1408 /* Maximum number of NOPs before switching to jump over NOPs. */
1409 int max_number_of_nops;
1410
1411 switch (fragP->fr_type)
1412 {
1413 case rs_fill_nop:
1414 case rs_align_code:
1415 break;
1416 case rs_machine_dependent:
1417 /* Allow NOP padding for jumps and calls. */
1418 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
1419 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
1420 break;
1421 /* Fall through. */
1422 default:
1423 return;
1424 }
1425
1426 /* We need to decide which NOP sequence to use for 32bit and
1427 64bit. When -mtune= is used:
1428
1429 1. For PROCESSOR_I?86, PROCESSOR_PENTIUM, PROCESSOR_IAMCU, and
1430 PROCESSOR_GENERIC32, f32_patt will be used.
1431 2. For the rest, alt_patt will be used.
1432
1433 When -mtune= isn't used, alt_patt will be used if
1434 cpu_arch_isa_flags has CpuNop. Otherwise, f32_patt/f64_patt will
1435 be used.
1436
1437 When -march= or .arch is used, we can't use anything beyond
1438 cpu_arch_isa_flags. */
1439
1440 if (fragP->tc_frag_data.code == CODE_16BIT)
1441 {
1442 patt = f16_patt;
1443 max_single_nop_size = sizeof (f16_patt) / sizeof (f16_patt[0]);
1444 /* Limit number of NOPs to 2 in 16-bit mode. */
1445 max_number_of_nops = 2;
1446 }
1447 else
1448 {
1449 patt = fragP->tc_frag_data.code == CODE_64BIT ? f64_patt : f32_patt;
1450 if (fragP->tc_frag_data.isa == PROCESSOR_UNKNOWN)
1451 {
1452 /* PROCESSOR_UNKNOWN means that all ISAs may be used, unless
1453 explicitly disabled. */
1454 switch (fragP->tc_frag_data.tune)
1455 {
1456 case PROCESSOR_UNKNOWN:
1457 /* We use cpu_arch_isa_flags to check if we SHOULD
1458 optimize with nops. */
1459 if (fragP->tc_frag_data.isanop)
1460 patt = alt_patt;
1461 break;
1462
1463 case PROCESSOR_PENTIUMPRO:
1464 case PROCESSOR_PENTIUM4:
1465 case PROCESSOR_NOCONA:
1466 case PROCESSOR_CORE:
1467 case PROCESSOR_CORE2:
1468 case PROCESSOR_COREI7:
1469 case PROCESSOR_GENERIC64:
1470 case PROCESSOR_K6:
1471 case PROCESSOR_ATHLON:
1472 case PROCESSOR_K8:
1473 case PROCESSOR_AMDFAM10:
1474 case PROCESSOR_BD:
1475 case PROCESSOR_ZNVER:
1476 case PROCESSOR_BT:
1477 if (fragP->tc_frag_data.cpunop)
1478 patt = alt_patt;
1479 break;
1480
1481 case PROCESSOR_I386:
1482 case PROCESSOR_I486:
1483 case PROCESSOR_PENTIUM:
1484 case PROCESSOR_I686:
1485 case PROCESSOR_IAMCU:
1486 case PROCESSOR_GENERIC32:
1487 break;
1488 case PROCESSOR_NONE:
1489 abort ();
1490 }
1491 }
1492 else
1493 {
1494 switch (fragP->tc_frag_data.tune)
1495 {
1496 case PROCESSOR_UNKNOWN:
1497 /* When cpu_arch_isa is set, cpu_arch_tune shouldn't be
1498 PROCESSOR_UNKNOWN. */
1499 abort ();
1500 break;
1501
1502 default:
1503 /* We use cpu_arch_isa_flags to check if we CAN optimize
1504 with nops. */
1505 if (fragP->tc_frag_data.isanop)
1506 patt = alt_patt;
1507 break;
1508
1509 case PROCESSOR_NONE:
1510 abort ();
1511 }
1512 }
1513
1514 if (patt != alt_patt)
1515 {
1516 max_single_nop_size = patt == f32_patt ? ARRAY_SIZE (f32_patt)
1517 : ARRAY_SIZE (f64_patt);
1518 /* Limit number of NOPs to 2 for older processors. */
1519 max_number_of_nops = 2;
1520 }
1521 else
1522 {
1523 max_single_nop_size = sizeof (alt_patt) / sizeof (alt_patt[0]);
1524 /* Limit number of NOPs to 7 for newer processors. */
1525 max_number_of_nops = 7;
1526 }
1527 }
1528
1529 if (limit == 0)
1530 limit = max_single_nop_size;
1531
1532 if (fragP->fr_type == rs_fill_nop)
1533 {
1534 /* Output NOPs for .nop directive. */
1535 if (limit > max_single_nop_size)
1536 {
1537 as_bad_where (fragP->fr_file, fragP->fr_line,
1538 _("invalid single nop size: %d "
1539 "(expect within [0, %d])"),
1540 limit, max_single_nop_size);
1541 return;
1542 }
1543 }
1544 else if (fragP->fr_type != rs_machine_dependent)
1545 fragP->fr_var = count;
1546
1547 if ((count / max_single_nop_size) > max_number_of_nops)
1548 {
1549 /* Generate jump over NOPs. */
1550 offsetT disp = count - 2;
1551 if (fits_in_imm7 (disp))
1552 {
1553 /* Use "jmp disp8" if possible. */
1554 count = disp;
1555 where[0] = jump_disp8[0];
1556 where[1] = count;
1557 where += 2;
1558 }
1559 else
1560 {
1561 unsigned int size_of_jump;
1562
1563 if (flag_code == CODE_16BIT)
1564 {
1565 where[0] = jump16_disp32[0];
1566 where[1] = jump16_disp32[1];
1567 size_of_jump = 2;
1568 }
1569 else
1570 {
1571 where[0] = jump32_disp32[0];
1572 size_of_jump = 1;
1573 }
1574
1575 count -= size_of_jump + 4;
1576 if (!fits_in_imm31 (count))
1577 {
1578 as_bad_where (fragP->fr_file, fragP->fr_line,
1579 _("jump over nop padding out of range"));
1580 return;
1581 }
1582
1583 md_number_to_chars (where + size_of_jump, count, 4);
1584 where += size_of_jump + 4;
1585 }
1586 }
1587
1588 /* Generate multiple NOPs. */
1589 i386_output_nops (where, patt, count, limit);
1590 }
1591
1592 static INLINE int
1593 operand_type_all_zero (const union i386_operand_type *x)
1594 {
1595 switch (ARRAY_SIZE(x->array))
1596 {
1597 case 3:
1598 if (x->array[2])
1599 return 0;
1600 /* Fall through. */
1601 case 2:
1602 if (x->array[1])
1603 return 0;
1604 /* Fall through. */
1605 case 1:
1606 return !x->array[0];
1607 default:
1608 abort ();
1609 }
1610 }
1611
1612 static INLINE void
1613 operand_type_set (union i386_operand_type *x, unsigned int v)
1614 {
1615 switch (ARRAY_SIZE(x->array))
1616 {
1617 case 3:
1618 x->array[2] = v;
1619 /* Fall through. */
1620 case 2:
1621 x->array[1] = v;
1622 /* Fall through. */
1623 case 1:
1624 x->array[0] = v;
1625 /* Fall through. */
1626 break;
1627 default:
1628 abort ();
1629 }
1630
1631 x->bitfield.class = ClassNone;
1632 x->bitfield.instance = InstanceNone;
1633 }
1634
1635 static INLINE int
1636 operand_type_equal (const union i386_operand_type *x,
1637 const union i386_operand_type *y)
1638 {
1639 switch (ARRAY_SIZE(x->array))
1640 {
1641 case 3:
1642 if (x->array[2] != y->array[2])
1643 return 0;
1644 /* Fall through. */
1645 case 2:
1646 if (x->array[1] != y->array[1])
1647 return 0;
1648 /* Fall through. */
1649 case 1:
1650 return x->array[0] == y->array[0];
1651 break;
1652 default:
1653 abort ();
1654 }
1655 }
1656
1657 static INLINE bool
1658 is_cpu (const insn_template *t, enum i386_cpu cpu)
1659 {
1660 switch (cpu)
1661 {
1662 case Cpu287: return t->cpu.bitfield.cpu287;
1663 case Cpu387: return t->cpu.bitfield.cpu387;
1664 case Cpu3dnow: return t->cpu.bitfield.cpu3dnow;
1665 case Cpu3dnowA: return t->cpu.bitfield.cpu3dnowa;
1666 case CpuAVX: return t->cpu.bitfield.cpuavx;
1667 case CpuHLE: return t->cpu.bitfield.cpuhle;
1668 case CpuAVX512F: return t->cpu.bitfield.cpuavx512f;
1669 case CpuAVX512VL: return t->cpu.bitfield.cpuavx512vl;
1670 case Cpu64: return t->cpu.bitfield.cpu64;
1671 case CpuNo64: return t->cpu.bitfield.cpuno64;
1672 default:
1673 gas_assert (cpu < CpuAttrEnums);
1674 }
1675 return t->cpu.bitfield.isa == cpu + 1u;
1676 }
1677
1678 static i386_cpu_flags cpu_flags_from_attr (i386_cpu_attr a)
1679 {
1680 const unsigned int bps = sizeof (a.array[0]) * CHAR_BIT;
1681 i386_cpu_flags f = { .array[0] = 0 };
1682
1683 switch (ARRAY_SIZE(a.array))
1684 {
1685 case 1:
1686 f.array[CpuAttrEnums / bps]
1687 |= (a.array[0] >> CpuIsaBits) << (CpuAttrEnums % bps);
1688 if (CpuAttrEnums % bps > CpuIsaBits)
1689 f.array[CpuAttrEnums / bps + 1]
1690 = (a.array[0] >> CpuIsaBits) >> (bps - CpuAttrEnums % bps);
1691 break;
1692 default:
1693 abort ();
1694 }
1695
1696 if (a.bitfield.isa)
1697 f.array[(a.bitfield.isa - 1) / bps] |= 1u << ((a.bitfield.isa - 1) % bps);
1698
1699 return f;
1700 }
1701
1702 static INLINE int
1703 cpu_flags_all_zero (const union i386_cpu_flags *x)
1704 {
1705 switch (ARRAY_SIZE(x->array))
1706 {
1707 case 5:
1708 if (x->array[4])
1709 return 0;
1710 /* Fall through. */
1711 case 4:
1712 if (x->array[3])
1713 return 0;
1714 /* Fall through. */
1715 case 3:
1716 if (x->array[2])
1717 return 0;
1718 /* Fall through. */
1719 case 2:
1720 if (x->array[1])
1721 return 0;
1722 /* Fall through. */
1723 case 1:
1724 return !x->array[0];
1725 default:
1726 abort ();
1727 }
1728 }
1729
1730 static INLINE int
1731 cpu_flags_equal (const union i386_cpu_flags *x,
1732 const union i386_cpu_flags *y)
1733 {
1734 switch (ARRAY_SIZE(x->array))
1735 {
1736 case 5:
1737 if (x->array[4] != y->array[4])
1738 return 0;
1739 /* Fall through. */
1740 case 4:
1741 if (x->array[3] != y->array[3])
1742 return 0;
1743 /* Fall through. */
1744 case 3:
1745 if (x->array[2] != y->array[2])
1746 return 0;
1747 /* Fall through. */
1748 case 2:
1749 if (x->array[1] != y->array[1])
1750 return 0;
1751 /* Fall through. */
1752 case 1:
1753 return x->array[0] == y->array[0];
1754 break;
1755 default:
1756 abort ();
1757 }
1758 }
1759
1760 static INLINE int
1761 cpu_flags_check_cpu64 (const insn_template *t)
1762 {
1763 return flag_code == CODE_64BIT
1764 ? !t->cpu.bitfield.cpuno64
1765 : !t->cpu.bitfield.cpu64;
1766 }
1767
1768 static INLINE i386_cpu_flags
1769 cpu_flags_and (i386_cpu_flags x, i386_cpu_flags y)
1770 {
1771 switch (ARRAY_SIZE (x.array))
1772 {
1773 case 5:
1774 x.array [4] &= y.array [4];
1775 /* Fall through. */
1776 case 4:
1777 x.array [3] &= y.array [3];
1778 /* Fall through. */
1779 case 3:
1780 x.array [2] &= y.array [2];
1781 /* Fall through. */
1782 case 2:
1783 x.array [1] &= y.array [1];
1784 /* Fall through. */
1785 case 1:
1786 x.array [0] &= y.array [0];
1787 break;
1788 default:
1789 abort ();
1790 }
1791 return x;
1792 }
1793
1794 static INLINE i386_cpu_flags
1795 cpu_flags_or (i386_cpu_flags x, i386_cpu_flags y)
1796 {
1797 switch (ARRAY_SIZE (x.array))
1798 {
1799 case 5:
1800 x.array [4] |= y.array [4];
1801 /* Fall through. */
1802 case 4:
1803 x.array [3] |= y.array [3];
1804 /* Fall through. */
1805 case 3:
1806 x.array [2] |= y.array [2];
1807 /* Fall through. */
1808 case 2:
1809 x.array [1] |= y.array [1];
1810 /* Fall through. */
1811 case 1:
1812 x.array [0] |= y.array [0];
1813 break;
1814 default:
1815 abort ();
1816 }
1817 return x;
1818 }
1819
1820 static INLINE i386_cpu_flags
1821 cpu_flags_and_not (i386_cpu_flags x, i386_cpu_flags y)
1822 {
1823 switch (ARRAY_SIZE (x.array))
1824 {
1825 case 5:
1826 x.array [4] &= ~y.array [4];
1827 /* Fall through. */
1828 case 4:
1829 x.array [3] &= ~y.array [3];
1830 /* Fall through. */
1831 case 3:
1832 x.array [2] &= ~y.array [2];
1833 /* Fall through. */
1834 case 2:
1835 x.array [1] &= ~y.array [1];
1836 /* Fall through. */
1837 case 1:
1838 x.array [0] &= ~y.array [0];
1839 break;
1840 default:
1841 abort ();
1842 }
1843 return x;
1844 }
1845
1846 static const i386_cpu_flags avx512 = CPU_ANY_AVX512F_FLAGS;
1847
1848 static INLINE bool need_evex_encoding (void)
1849 {
1850 return i.vec_encoding == vex_encoding_evex
1851 || i.vec_encoding == vex_encoding_evex512
1852 || i.mask.reg;
1853 }
1854
1855 #define CPU_FLAGS_ARCH_MATCH 0x1
1856 #define CPU_FLAGS_64BIT_MATCH 0x2
1857
1858 #define CPU_FLAGS_PERFECT_MATCH \
1859 (CPU_FLAGS_ARCH_MATCH | CPU_FLAGS_64BIT_MATCH)
1860
1861 /* Return CPU flags match bits. */
1862
1863 static int
1864 cpu_flags_match (const insn_template *t)
1865 {
1866 i386_cpu_flags x = cpu_flags_from_attr (t->cpu);
1867 int match = cpu_flags_check_cpu64 (t) ? CPU_FLAGS_64BIT_MATCH : 0;
1868
1869 x.bitfield.cpu64 = 0;
1870 x.bitfield.cpuno64 = 0;
1871
1872 if (cpu_flags_all_zero (&x))
1873 {
1874 /* This instruction is available on all archs. */
1875 match |= CPU_FLAGS_ARCH_MATCH;
1876 }
1877 else
1878 {
1879 /* This instruction is available only on some archs. */
1880 i386_cpu_flags active, cpu;
1881
1882 if (flag_code != CODE_64BIT)
1883 active = cpu_flags_and_not (cpu_arch_flags, cpu_64_flags);
1884 else
1885 active = cpu_arch_flags;
1886
1887 /* Dual VEX/EVEX templates may need stripping of one of the flags. */
1888 if (t->opcode_modifier.vex && t->opcode_modifier.evex)
1889 {
1890 /* Dual AVX/AVX512F templates need to retain AVX512F only if we already
1891 know that EVEX encoding will be needed. */
1892 if ((x.bitfield.cpuavx || x.bitfield.cpuavx2)
1893 && x.bitfield.cpuavx512f)
1894 {
1895 if (need_evex_encoding ())
1896 {
1897 x.bitfield.cpuavx = 0;
1898 x.bitfield.cpuavx2 = 0;
1899 }
1900 /* need_evex_encoding() isn't reliable before operands were
1901 parsed. */
1902 else if (i.operands)
1903 {
1904 x.bitfield.cpuavx512f = 0;
1905 x.bitfield.cpuavx512vl = 0;
1906 if (x.bitfield.cpufma && !active.bitfield.cpufma)
1907 x.bitfield.cpuavx = 0;
1908 }
1909 }
1910 }
1911
1912 /* AVX512VL is no standalone feature - match it and then strip it. */
1913 if (x.bitfield.cpuavx512vl && !active.bitfield.cpuavx512vl)
1914 return match;
1915 x.bitfield.cpuavx512vl = 0;
1916
1917 /* AVX and AVX2 present at the same time express an operand size
1918 dependency - strip AVX2 for the purposes here. The operand size
1919 dependent check occurs in check_vecOperands(). */
1920 if (x.bitfield.cpuavx && x.bitfield.cpuavx2)
1921 x.bitfield.cpuavx2 = 0;
1922
1923 cpu = cpu_flags_and (x, active);
1924 if (!cpu_flags_all_zero (&cpu))
1925 {
1926 if (t->cpu.bitfield.cpuavx && t->cpu.bitfield.cpuavx512f)
1927 {
1928 if ((need_evex_encoding ()
1929 ? cpu.bitfield.cpuavx512f
1930 : cpu.bitfield.cpuavx)
1931 && (!x.bitfield.cpufma || cpu.bitfield.cpufma
1932 || active.bitfield.cpuavx512f)
1933 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
1934 && (!x.bitfield.cpuvaes || cpu.bitfield.cpuvaes)
1935 && (!x.bitfield.cpuvpclmulqdq || cpu.bitfield.cpuvpclmulqdq))
1936 match |= CPU_FLAGS_ARCH_MATCH;
1937 }
1938 else if (x.bitfield.cpuavx)
1939 {
1940 /* We need to check a few extra flags with AVX. */
1941 if (cpu.bitfield.cpuavx
1942 && (!t->opcode_modifier.sse2avx
1943 || (sse2avx && !i.prefix[DATA_PREFIX]))
1944 && (!x.bitfield.cpuaes || cpu.bitfield.cpuaes)
1945 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
1946 && (!x.bitfield.cpupclmulqdq || cpu.bitfield.cpupclmulqdq))
1947 match |= CPU_FLAGS_ARCH_MATCH;
1948 }
1949 else if (x.bitfield.cpuavx2 && cpu.bitfield.cpuavx2)
1950 match |= CPU_FLAGS_ARCH_MATCH;
1951 else if (x.bitfield.cpuavx512f)
1952 {
1953 /* We need to check a few extra flags with AVX512F. */
1954 if (cpu.bitfield.cpuavx512f
1955 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni))
1956 match |= CPU_FLAGS_ARCH_MATCH;
1957 }
1958 else
1959 match |= CPU_FLAGS_ARCH_MATCH;
1960 }
1961 }
1962 return match;
1963 }
1964
1965 static INLINE i386_operand_type
1966 operand_type_and (i386_operand_type x, i386_operand_type y)
1967 {
1968 if (x.bitfield.class != y.bitfield.class)
1969 x.bitfield.class = ClassNone;
1970 if (x.bitfield.instance != y.bitfield.instance)
1971 x.bitfield.instance = InstanceNone;
1972
1973 switch (ARRAY_SIZE (x.array))
1974 {
1975 case 3:
1976 x.array [2] &= y.array [2];
1977 /* Fall through. */
1978 case 2:
1979 x.array [1] &= y.array [1];
1980 /* Fall through. */
1981 case 1:
1982 x.array [0] &= y.array [0];
1983 break;
1984 default:
1985 abort ();
1986 }
1987 return x;
1988 }
1989
1990 static INLINE i386_operand_type
1991 operand_type_and_not (i386_operand_type x, i386_operand_type y)
1992 {
1993 gas_assert (y.bitfield.class == ClassNone);
1994 gas_assert (y.bitfield.instance == InstanceNone);
1995
1996 switch (ARRAY_SIZE (x.array))
1997 {
1998 case 3:
1999 x.array [2] &= ~y.array [2];
2000 /* Fall through. */
2001 case 2:
2002 x.array [1] &= ~y.array [1];
2003 /* Fall through. */
2004 case 1:
2005 x.array [0] &= ~y.array [0];
2006 break;
2007 default:
2008 abort ();
2009 }
2010 return x;
2011 }
2012
2013 static INLINE i386_operand_type
2014 operand_type_or (i386_operand_type x, i386_operand_type y)
2015 {
2016 gas_assert (x.bitfield.class == ClassNone ||
2017 y.bitfield.class == ClassNone ||
2018 x.bitfield.class == y.bitfield.class);
2019 gas_assert (x.bitfield.instance == InstanceNone ||
2020 y.bitfield.instance == InstanceNone ||
2021 x.bitfield.instance == y.bitfield.instance);
2022
2023 switch (ARRAY_SIZE (x.array))
2024 {
2025 case 3:
2026 x.array [2] |= y.array [2];
2027 /* Fall through. */
2028 case 2:
2029 x.array [1] |= y.array [1];
2030 /* Fall through. */
2031 case 1:
2032 x.array [0] |= y.array [0];
2033 break;
2034 default:
2035 abort ();
2036 }
2037 return x;
2038 }
2039
2040 static INLINE i386_operand_type
2041 operand_type_xor (i386_operand_type x, i386_operand_type y)
2042 {
2043 gas_assert (y.bitfield.class == ClassNone);
2044 gas_assert (y.bitfield.instance == InstanceNone);
2045
2046 switch (ARRAY_SIZE (x.array))
2047 {
2048 case 3:
2049 x.array [2] ^= y.array [2];
2050 /* Fall through. */
2051 case 2:
2052 x.array [1] ^= y.array [1];
2053 /* Fall through. */
2054 case 1:
2055 x.array [0] ^= y.array [0];
2056 break;
2057 default:
2058 abort ();
2059 }
2060 return x;
2061 }
2062
2063 static const i386_operand_type anydisp = {
2064 .bitfield = { .disp8 = 1, .disp16 = 1, .disp32 = 1, .disp64 = 1 }
2065 };
2066
2067 enum operand_type
2068 {
2069 reg,
2070 imm,
2071 disp,
2072 anymem
2073 };
2074
2075 static INLINE int
2076 operand_type_check (i386_operand_type t, enum operand_type c)
2077 {
2078 switch (c)
2079 {
2080 case reg:
2081 return t.bitfield.class == Reg;
2082
2083 case imm:
2084 return (t.bitfield.imm8
2085 || t.bitfield.imm8s
2086 || t.bitfield.imm16
2087 || t.bitfield.imm32
2088 || t.bitfield.imm32s
2089 || t.bitfield.imm64);
2090
2091 case disp:
2092 return (t.bitfield.disp8
2093 || t.bitfield.disp16
2094 || t.bitfield.disp32
2095 || t.bitfield.disp64);
2096
2097 case anymem:
2098 return (t.bitfield.disp8
2099 || t.bitfield.disp16
2100 || t.bitfield.disp32
2101 || t.bitfield.disp64
2102 || t.bitfield.baseindex);
2103
2104 default:
2105 abort ();
2106 }
2107
2108 return 0;
2109 }
2110
2111 /* Return 1 if there is no conflict in 8bit/16bit/32bit/64bit/80bit size
2112 between operand GIVEN and opeand WANTED for instruction template T. */
2113
2114 static INLINE int
2115 match_operand_size (const insn_template *t, unsigned int wanted,
2116 unsigned int given)
2117 {
2118 return !((i.types[given].bitfield.byte
2119 && !t->operand_types[wanted].bitfield.byte)
2120 || (i.types[given].bitfield.word
2121 && !t->operand_types[wanted].bitfield.word)
2122 || (i.types[given].bitfield.dword
2123 && !t->operand_types[wanted].bitfield.dword)
2124 || (i.types[given].bitfield.qword
2125 && (!t->operand_types[wanted].bitfield.qword
2126 /* Don't allow 64-bit (memory) operands outside of 64-bit
2127 mode, when they're used where a 64-bit GPR could also
2128 be used. Checking is needed for Intel Syntax only. */
2129 || (intel_syntax
2130 && flag_code != CODE_64BIT
2131 && (t->operand_types[wanted].bitfield.class == Reg
2132 || t->operand_types[wanted].bitfield.class == Accum
2133 || t->opcode_modifier.isstring))))
2134 || (i.types[given].bitfield.tbyte
2135 && !t->operand_types[wanted].bitfield.tbyte));
2136 }
2137
2138 /* Return 1 if there is no conflict in SIMD register between operand
2139 GIVEN and opeand WANTED for instruction template T. */
2140
2141 static INLINE int
2142 match_simd_size (const insn_template *t, unsigned int wanted,
2143 unsigned int given)
2144 {
2145 return !((i.types[given].bitfield.xmmword
2146 && !t->operand_types[wanted].bitfield.xmmword)
2147 || (i.types[given].bitfield.ymmword
2148 && !t->operand_types[wanted].bitfield.ymmword)
2149 || (i.types[given].bitfield.zmmword
2150 && !t->operand_types[wanted].bitfield.zmmword)
2151 || (i.types[given].bitfield.tmmword
2152 && !t->operand_types[wanted].bitfield.tmmword));
2153 }
2154
2155 /* Return 1 if there is no conflict in any size between operand GIVEN
2156 and opeand WANTED for instruction template T. */
2157
2158 static INLINE int
2159 match_mem_size (const insn_template *t, unsigned int wanted,
2160 unsigned int given)
2161 {
2162 return (match_operand_size (t, wanted, given)
2163 && !((i.types[given].bitfield.unspecified
2164 && !i.broadcast.type
2165 && !i.broadcast.bytes
2166 && !t->operand_types[wanted].bitfield.unspecified)
2167 || (i.types[given].bitfield.fword
2168 && !t->operand_types[wanted].bitfield.fword)
2169 /* For scalar opcode templates to allow register and memory
2170 operands at the same time, some special casing is needed
2171 here. Also for v{,p}broadcast*, {,v}pmov{s,z}*, and
2172 down-conversion vpmov*. */
2173 || ((t->operand_types[wanted].bitfield.class == RegSIMD
2174 && t->operand_types[wanted].bitfield.byte
2175 + t->operand_types[wanted].bitfield.word
2176 + t->operand_types[wanted].bitfield.dword
2177 + t->operand_types[wanted].bitfield.qword
2178 > !!t->opcode_modifier.broadcast)
2179 ? (i.types[given].bitfield.xmmword
2180 || i.types[given].bitfield.ymmword
2181 || i.types[given].bitfield.zmmword)
2182 : !match_simd_size(t, wanted, given))));
2183 }
2184
2185 /* Return value has MATCH_STRAIGHT set if there is no size conflict on any
2186 operands for instruction template T, and it has MATCH_REVERSE set if there
2187 is no size conflict on any operands for the template with operands reversed
2188 (and the template allows for reversing in the first place). */
2189
2190 #define MATCH_STRAIGHT 1
2191 #define MATCH_REVERSE 2
2192
2193 static INLINE unsigned int
2194 operand_size_match (const insn_template *t)
2195 {
2196 unsigned int j, match = MATCH_STRAIGHT;
2197
2198 /* Don't check non-absolute jump instructions. */
2199 if (t->opcode_modifier.jump
2200 && t->opcode_modifier.jump != JUMP_ABSOLUTE)
2201 return match;
2202
2203 /* Check memory and accumulator operand size. */
2204 for (j = 0; j < i.operands; j++)
2205 {
2206 if (i.types[j].bitfield.class != Reg
2207 && i.types[j].bitfield.class != RegSIMD
2208 && t->opcode_modifier.operandconstraint == ANY_SIZE)
2209 continue;
2210
2211 if (t->operand_types[j].bitfield.class == Reg
2212 && !match_operand_size (t, j, j))
2213 {
2214 match = 0;
2215 break;
2216 }
2217
2218 if (t->operand_types[j].bitfield.class == RegSIMD
2219 && !match_simd_size (t, j, j))
2220 {
2221 match = 0;
2222 break;
2223 }
2224
2225 if (t->operand_types[j].bitfield.instance == Accum
2226 && (!match_operand_size (t, j, j) || !match_simd_size (t, j, j)))
2227 {
2228 match = 0;
2229 break;
2230 }
2231
2232 if ((i.flags[j] & Operand_Mem) && !match_mem_size (t, j, j))
2233 {
2234 match = 0;
2235 break;
2236 }
2237 }
2238
2239 if (!t->opcode_modifier.d)
2240 return match;
2241
2242 /* Check reverse. */
2243 gas_assert (i.operands >= 2);
2244
2245 for (j = 0; j < i.operands; j++)
2246 {
2247 unsigned int given = i.operands - j - 1;
2248
2249 /* For FMA4 and XOP insns VEX.W controls just the first two
2250 register operands. */
2251 if (is_cpu (t, CpuFMA4) || is_cpu (t, CpuXOP))
2252 given = j < 2 ? 1 - j : j;
2253
2254 if (t->operand_types[j].bitfield.class == Reg
2255 && !match_operand_size (t, j, given))
2256 return match;
2257
2258 if (t->operand_types[j].bitfield.class == RegSIMD
2259 && !match_simd_size (t, j, given))
2260 return match;
2261
2262 if (t->operand_types[j].bitfield.instance == Accum
2263 && (!match_operand_size (t, j, given)
2264 || !match_simd_size (t, j, given)))
2265 return match;
2266
2267 if ((i.flags[given] & Operand_Mem) && !match_mem_size (t, j, given))
2268 return match;
2269 }
2270
2271 return match | MATCH_REVERSE;
2272 }
2273
2274 static INLINE int
2275 operand_type_match (i386_operand_type overlap,
2276 i386_operand_type given)
2277 {
2278 i386_operand_type temp = overlap;
2279
2280 temp.bitfield.unspecified = 0;
2281 temp.bitfield.byte = 0;
2282 temp.bitfield.word = 0;
2283 temp.bitfield.dword = 0;
2284 temp.bitfield.fword = 0;
2285 temp.bitfield.qword = 0;
2286 temp.bitfield.tbyte = 0;
2287 temp.bitfield.xmmword = 0;
2288 temp.bitfield.ymmword = 0;
2289 temp.bitfield.zmmword = 0;
2290 temp.bitfield.tmmword = 0;
2291 if (operand_type_all_zero (&temp))
2292 goto mismatch;
2293
2294 if (given.bitfield.baseindex == overlap.bitfield.baseindex)
2295 return 1;
2296
2297 mismatch:
2298 i.error = operand_type_mismatch;
2299 return 0;
2300 }
2301
2302 /* If given types g0 and g1 are registers they must be of the same type
2303 unless the expected operand type register overlap is null.
2304 Intel syntax sized memory operands are also checked here. */
2305
2306 static INLINE int
2307 operand_type_register_match (i386_operand_type g0,
2308 i386_operand_type t0,
2309 i386_operand_type g1,
2310 i386_operand_type t1)
2311 {
2312 if (g0.bitfield.class != Reg
2313 && g0.bitfield.class != RegSIMD
2314 && (g0.bitfield.unspecified
2315 || !operand_type_check (g0, anymem)))
2316 return 1;
2317
2318 if (g1.bitfield.class != Reg
2319 && g1.bitfield.class != RegSIMD
2320 && (g1.bitfield.unspecified
2321 || !operand_type_check (g1, anymem)))
2322 return 1;
2323
2324 if (g0.bitfield.byte == g1.bitfield.byte
2325 && g0.bitfield.word == g1.bitfield.word
2326 && g0.bitfield.dword == g1.bitfield.dword
2327 && g0.bitfield.qword == g1.bitfield.qword
2328 && g0.bitfield.xmmword == g1.bitfield.xmmword
2329 && g0.bitfield.ymmword == g1.bitfield.ymmword
2330 && g0.bitfield.zmmword == g1.bitfield.zmmword)
2331 return 1;
2332
2333 /* If expectations overlap in no more than a single size, all is fine. */
2334 g0 = operand_type_and (t0, t1);
2335 if (g0.bitfield.byte
2336 + g0.bitfield.word
2337 + g0.bitfield.dword
2338 + g0.bitfield.qword
2339 + g0.bitfield.xmmword
2340 + g0.bitfield.ymmword
2341 + g0.bitfield.zmmword <= 1)
2342 return 1;
2343
2344 i.error = register_type_mismatch;
2345
2346 return 0;
2347 }
2348
2349 static INLINE unsigned int
2350 register_number (const reg_entry *r)
2351 {
2352 unsigned int nr = r->reg_num;
2353
2354 if (r->reg_flags & RegRex)
2355 nr += 8;
2356
2357 if (r->reg_flags & RegVRex)
2358 nr += 16;
2359
2360 return nr;
2361 }
2362
2363 static INLINE unsigned int
2364 mode_from_disp_size (i386_operand_type t)
2365 {
2366 if (t.bitfield.disp8)
2367 return 1;
2368 else if (t.bitfield.disp16
2369 || t.bitfield.disp32)
2370 return 2;
2371 else
2372 return 0;
2373 }
2374
2375 static INLINE int
2376 fits_in_signed_byte (addressT num)
2377 {
2378 return num + 0x80 <= 0xff;
2379 }
2380
2381 static INLINE int
2382 fits_in_unsigned_byte (addressT num)
2383 {
2384 return num <= 0xff;
2385 }
2386
2387 static INLINE int
2388 fits_in_unsigned_word (addressT num)
2389 {
2390 return num <= 0xffff;
2391 }
2392
2393 static INLINE int
2394 fits_in_signed_word (addressT num)
2395 {
2396 return num + 0x8000 <= 0xffff;
2397 }
2398
2399 static INLINE int
2400 fits_in_signed_long (addressT num ATTRIBUTE_UNUSED)
2401 {
2402 #ifndef BFD64
2403 return 1;
2404 #else
2405 return num + 0x80000000 <= 0xffffffff;
2406 #endif
2407 } /* fits_in_signed_long() */
2408
2409 static INLINE int
2410 fits_in_unsigned_long (addressT num ATTRIBUTE_UNUSED)
2411 {
2412 #ifndef BFD64
2413 return 1;
2414 #else
2415 return num <= 0xffffffff;
2416 #endif
2417 } /* fits_in_unsigned_long() */
2418
2419 static INLINE valueT extend_to_32bit_address (addressT num)
2420 {
2421 #ifdef BFD64
2422 if (fits_in_unsigned_long(num))
2423 return (num ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
2424
2425 if (!fits_in_signed_long (num))
2426 return num & 0xffffffff;
2427 #endif
2428
2429 return num;
2430 }
2431
2432 static INLINE int
2433 fits_in_disp8 (offsetT num)
2434 {
2435 int shift = i.memshift;
2436 unsigned int mask;
2437
2438 if (shift == -1)
2439 abort ();
2440
2441 mask = (1 << shift) - 1;
2442
2443 /* Return 0 if NUM isn't properly aligned. */
2444 if ((num & mask))
2445 return 0;
2446
2447 /* Check if NUM will fit in 8bit after shift. */
2448 return fits_in_signed_byte (num >> shift);
2449 }
2450
2451 static INLINE int
2452 fits_in_imm4 (offsetT num)
2453 {
2454 /* Despite the name, check for imm3 if we're dealing with EVEX. */
2455 return (num & (i.vec_encoding != vex_encoding_evex ? 0xf : 7)) == num;
2456 }
2457
2458 static i386_operand_type
2459 smallest_imm_type (offsetT num)
2460 {
2461 i386_operand_type t;
2462
2463 operand_type_set (&t, 0);
2464 t.bitfield.imm64 = 1;
2465
2466 if (cpu_arch_tune != PROCESSOR_I486 && num == 1)
2467 {
2468 /* This code is disabled on the 486 because all the Imm1 forms
2469 in the opcode table are slower on the i486. They're the
2470 versions with the implicitly specified single-position
2471 displacement, which has another syntax if you really want to
2472 use that form. */
2473 t.bitfield.imm1 = 1;
2474 t.bitfield.imm8 = 1;
2475 t.bitfield.imm8s = 1;
2476 t.bitfield.imm16 = 1;
2477 t.bitfield.imm32 = 1;
2478 t.bitfield.imm32s = 1;
2479 }
2480 else if (fits_in_signed_byte (num))
2481 {
2482 if (fits_in_unsigned_byte (num))
2483 t.bitfield.imm8 = 1;
2484 t.bitfield.imm8s = 1;
2485 t.bitfield.imm16 = 1;
2486 if (flag_code != CODE_64BIT || fits_in_unsigned_long (num))
2487 t.bitfield.imm32 = 1;
2488 t.bitfield.imm32s = 1;
2489 }
2490 else if (fits_in_unsigned_byte (num))
2491 {
2492 t.bitfield.imm8 = 1;
2493 t.bitfield.imm16 = 1;
2494 t.bitfield.imm32 = 1;
2495 t.bitfield.imm32s = 1;
2496 }
2497 else if (fits_in_signed_word (num) || fits_in_unsigned_word (num))
2498 {
2499 t.bitfield.imm16 = 1;
2500 if (flag_code != CODE_64BIT || fits_in_unsigned_long (num))
2501 t.bitfield.imm32 = 1;
2502 t.bitfield.imm32s = 1;
2503 }
2504 else if (fits_in_signed_long (num))
2505 {
2506 if (flag_code != CODE_64BIT || fits_in_unsigned_long (num))
2507 t.bitfield.imm32 = 1;
2508 t.bitfield.imm32s = 1;
2509 }
2510 else if (fits_in_unsigned_long (num))
2511 t.bitfield.imm32 = 1;
2512
2513 return t;
2514 }
2515
2516 static offsetT
2517 offset_in_range (offsetT val, int size)
2518 {
2519 addressT mask;
2520
2521 switch (size)
2522 {
2523 case 1: mask = ((addressT) 1 << 8) - 1; break;
2524 case 2: mask = ((addressT) 1 << 16) - 1; break;
2525 #ifdef BFD64
2526 case 4: mask = ((addressT) 1 << 32) - 1; break;
2527 #endif
2528 case sizeof (val): return val;
2529 default: abort ();
2530 }
2531
2532 if ((val & ~mask) != 0 && (-val & ~mask) != 0)
2533 as_warn (_("0x%" PRIx64 " shortened to 0x%" PRIx64),
2534 (uint64_t) val, (uint64_t) (val & mask));
2535
2536 return val & mask;
2537 }
2538
2539 static INLINE const char *insn_name (const insn_template *t)
2540 {
2541 return &i386_mnemonics[t->mnem_off];
2542 }
2543
2544 enum PREFIX_GROUP
2545 {
2546 PREFIX_EXIST = 0,
2547 PREFIX_LOCK,
2548 PREFIX_REP,
2549 PREFIX_DS,
2550 PREFIX_OTHER
2551 };
2552
2553 /* Returns
2554 a. PREFIX_EXIST if attempting to add a prefix where one from the
2555 same class already exists.
2556 b. PREFIX_LOCK if lock prefix is added.
2557 c. PREFIX_REP if rep/repne prefix is added.
2558 d. PREFIX_DS if ds prefix is added.
2559 e. PREFIX_OTHER if other prefix is added.
2560 */
2561
2562 static enum PREFIX_GROUP
2563 add_prefix (unsigned int prefix)
2564 {
2565 enum PREFIX_GROUP ret = PREFIX_OTHER;
2566 unsigned int q;
2567
2568 if (prefix >= REX_OPCODE && prefix < REX_OPCODE + 16
2569 && flag_code == CODE_64BIT)
2570 {
2571 if ((i.prefix[REX_PREFIX] & prefix & REX_W)
2572 || (i.prefix[REX_PREFIX] & prefix & REX_R)
2573 || (i.prefix[REX_PREFIX] & prefix & REX_X)
2574 || (i.prefix[REX_PREFIX] & prefix & REX_B))
2575 ret = PREFIX_EXIST;
2576 q = REX_PREFIX;
2577 }
2578 else
2579 {
2580 switch (prefix)
2581 {
2582 default:
2583 abort ();
2584
2585 case DS_PREFIX_OPCODE:
2586 ret = PREFIX_DS;
2587 /* Fall through. */
2588 case CS_PREFIX_OPCODE:
2589 case ES_PREFIX_OPCODE:
2590 case FS_PREFIX_OPCODE:
2591 case GS_PREFIX_OPCODE:
2592 case SS_PREFIX_OPCODE:
2593 q = SEG_PREFIX;
2594 break;
2595
2596 case REPNE_PREFIX_OPCODE:
2597 case REPE_PREFIX_OPCODE:
2598 q = REP_PREFIX;
2599 ret = PREFIX_REP;
2600 break;
2601
2602 case LOCK_PREFIX_OPCODE:
2603 q = LOCK_PREFIX;
2604 ret = PREFIX_LOCK;
2605 break;
2606
2607 case FWAIT_OPCODE:
2608 q = WAIT_PREFIX;
2609 break;
2610
2611 case ADDR_PREFIX_OPCODE:
2612 q = ADDR_PREFIX;
2613 break;
2614
2615 case DATA_PREFIX_OPCODE:
2616 q = DATA_PREFIX;
2617 break;
2618 }
2619 if (i.prefix[q] != 0)
2620 ret = PREFIX_EXIST;
2621 }
2622
2623 if (ret)
2624 {
2625 if (!i.prefix[q])
2626 ++i.prefixes;
2627 i.prefix[q] |= prefix;
2628 }
2629 else
2630 as_bad (_("same type of prefix used twice"));
2631
2632 return ret;
2633 }
2634
2635 static void
2636 update_code_flag (int value, int check)
2637 {
2638 PRINTF_LIKE ((*as_error)) = check ? as_fatal : as_bad;
2639
2640 if (value == CODE_64BIT && !cpu_arch_flags.bitfield.cpu64 )
2641 {
2642 as_error (_("64bit mode not supported on `%s'."),
2643 cpu_arch_name ? cpu_arch_name : default_arch);
2644 return;
2645 }
2646
2647 if (value == CODE_32BIT && !cpu_arch_flags.bitfield.cpui386)
2648 {
2649 as_error (_("32bit mode not supported on `%s'."),
2650 cpu_arch_name ? cpu_arch_name : default_arch);
2651 return;
2652 }
2653
2654 flag_code = (enum flag_code) value;
2655
2656 stackop_size = '\0';
2657 }
2658
2659 static void
2660 set_code_flag (int value)
2661 {
2662 update_code_flag (value, 0);
2663 }
2664
2665 static void
2666 set_16bit_gcc_code_flag (int new_code_flag)
2667 {
2668 flag_code = (enum flag_code) new_code_flag;
2669 if (flag_code != CODE_16BIT)
2670 abort ();
2671 stackop_size = LONG_MNEM_SUFFIX;
2672 }
2673
2674 static void
2675 set_intel_syntax (int syntax_flag)
2676 {
2677 /* Find out if register prefixing is specified. */
2678 int ask_naked_reg = 0;
2679
2680 SKIP_WHITESPACE ();
2681 if (!is_end_of_line[(unsigned char) *input_line_pointer])
2682 {
2683 char *string;
2684 int e = get_symbol_name (&string);
2685
2686 if (strcmp (string, "prefix") == 0)
2687 ask_naked_reg = 1;
2688 else if (strcmp (string, "noprefix") == 0)
2689 ask_naked_reg = -1;
2690 else
2691 as_bad (_("bad argument to syntax directive."));
2692 (void) restore_line_pointer (e);
2693 }
2694 demand_empty_rest_of_line ();
2695
2696 intel_syntax = syntax_flag;
2697
2698 if (ask_naked_reg == 0)
2699 allow_naked_reg = (intel_syntax
2700 && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
2701 else
2702 allow_naked_reg = (ask_naked_reg < 0);
2703
2704 expr_set_rank (O_full_ptr, syntax_flag ? 10 : 0);
2705
2706 register_prefix = allow_naked_reg ? "" : "%";
2707 }
2708
2709 static void
2710 set_intel_mnemonic (int mnemonic_flag)
2711 {
2712 intel_mnemonic = mnemonic_flag;
2713 }
2714
2715 static void
2716 set_allow_index_reg (int flag)
2717 {
2718 allow_index_reg = flag;
2719 }
2720
2721 static void
2722 set_check (int what)
2723 {
2724 enum check_kind *kind;
2725 const char *str;
2726
2727 if (what)
2728 {
2729 kind = &operand_check;
2730 str = "operand";
2731 }
2732 else
2733 {
2734 kind = &sse_check;
2735 str = "sse";
2736 }
2737
2738 SKIP_WHITESPACE ();
2739
2740 if (!is_end_of_line[(unsigned char) *input_line_pointer])
2741 {
2742 char *string;
2743 int e = get_symbol_name (&string);
2744
2745 if (strcmp (string, "none") == 0)
2746 *kind = check_none;
2747 else if (strcmp (string, "warning") == 0)
2748 *kind = check_warning;
2749 else if (strcmp (string, "error") == 0)
2750 *kind = check_error;
2751 else
2752 as_bad (_("bad argument to %s_check directive."), str);
2753 (void) restore_line_pointer (e);
2754 }
2755 else
2756 as_bad (_("missing argument for %s_check directive"), str);
2757
2758 demand_empty_rest_of_line ();
2759 }
2760
2761 static void
2762 check_cpu_arch_compatible (const char *name ATTRIBUTE_UNUSED,
2763 i386_cpu_flags new_flag ATTRIBUTE_UNUSED)
2764 {
2765 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2766 static const char *arch;
2767
2768 /* Intel MCU is only supported on ELF. */
2769 if (!IS_ELF)
2770 return;
2771
2772 if (!arch)
2773 {
2774 /* Use cpu_arch_name if it is set in md_parse_option. Otherwise
2775 use default_arch. */
2776 arch = cpu_arch_name;
2777 if (!arch)
2778 arch = default_arch;
2779 }
2780
2781 /* If we are targeting Intel MCU, we must enable it. */
2782 if ((get_elf_backend_data (stdoutput)->elf_machine_code == EM_IAMCU)
2783 == new_flag.bitfield.cpuiamcu)
2784 return;
2785
2786 as_bad (_("`%s' is not supported on `%s'"), name, arch);
2787 #endif
2788 }
2789
2790 static void
2791 extend_cpu_sub_arch_name (const char *pfx, const char *name)
2792 {
2793 if (cpu_sub_arch_name)
2794 cpu_sub_arch_name = reconcat (cpu_sub_arch_name, cpu_sub_arch_name,
2795 pfx, name, (const char *) NULL);
2796 else
2797 cpu_sub_arch_name = concat (pfx, name, (const char *) NULL);
2798 }
2799
2800 static void isa_enable (unsigned int idx)
2801 {
2802 i386_cpu_flags flags = cpu_flags_or (cpu_arch_flags, cpu_arch[idx].enable);
2803
2804 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
2805 {
2806 extend_cpu_sub_arch_name (".", cpu_arch[idx].name);
2807 cpu_arch_flags = flags;
2808 }
2809
2810 cpu_arch_isa_flags = cpu_flags_or (cpu_arch_isa_flags, cpu_arch[idx].enable);
2811 }
2812
2813 static void isa_disable (unsigned int idx)
2814 {
2815 i386_cpu_flags flags
2816 = cpu_flags_and_not (cpu_arch_flags, cpu_arch[idx].disable);
2817
2818 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
2819 {
2820 extend_cpu_sub_arch_name (".no", cpu_arch[idx].name);
2821 cpu_arch_flags = flags;
2822 }
2823
2824 cpu_arch_isa_flags
2825 = cpu_flags_and_not (cpu_arch_isa_flags, cpu_arch[idx].disable);
2826 }
2827
2828 static void
2829 set_cpu_arch (int dummy ATTRIBUTE_UNUSED)
2830 {
2831 typedef struct arch_stack_entry
2832 {
2833 const struct arch_stack_entry *prev;
2834 const char *name;
2835 char *sub_name;
2836 i386_cpu_flags flags;
2837 i386_cpu_flags isa_flags;
2838 enum processor_type isa;
2839 enum flag_code flag_code;
2840 unsigned int vector_size;
2841 char stackop_size;
2842 bool no_cond_jump_promotion;
2843 } arch_stack_entry;
2844 static const arch_stack_entry *arch_stack_top;
2845 char *s;
2846 int e;
2847 const char *string;
2848 unsigned int j = 0;
2849
2850 SKIP_WHITESPACE ();
2851
2852 if (is_end_of_line[(unsigned char) *input_line_pointer])
2853 {
2854 as_bad (_("missing cpu architecture"));
2855 input_line_pointer++;
2856 return;
2857 }
2858
2859 e = get_symbol_name (&s);
2860 string = s;
2861
2862 if (strcmp (string, "push") == 0)
2863 {
2864 arch_stack_entry *top = XNEW (arch_stack_entry);
2865
2866 top->name = cpu_arch_name;
2867 if (cpu_sub_arch_name)
2868 top->sub_name = xstrdup (cpu_sub_arch_name);
2869 else
2870 top->sub_name = NULL;
2871 top->flags = cpu_arch_flags;
2872 top->isa = cpu_arch_isa;
2873 top->isa_flags = cpu_arch_isa_flags;
2874 top->flag_code = flag_code;
2875 top->vector_size = vector_size;
2876 top->stackop_size = stackop_size;
2877 top->no_cond_jump_promotion = no_cond_jump_promotion;
2878
2879 top->prev = arch_stack_top;
2880 arch_stack_top = top;
2881
2882 (void) restore_line_pointer (e);
2883 demand_empty_rest_of_line ();
2884 return;
2885 }
2886
2887 if (strcmp (string, "pop") == 0)
2888 {
2889 const arch_stack_entry *top = arch_stack_top;
2890
2891 if (!top)
2892 as_bad (_(".arch stack is empty"));
2893 else if (top->flag_code != flag_code
2894 || top->stackop_size != stackop_size)
2895 {
2896 static const unsigned int bits[] = {
2897 [CODE_16BIT] = 16,
2898 [CODE_32BIT] = 32,
2899 [CODE_64BIT] = 64,
2900 };
2901
2902 as_bad (_("this `.arch pop' requires `.code%u%s' to be in effect"),
2903 bits[top->flag_code],
2904 top->stackop_size == LONG_MNEM_SUFFIX ? "gcc" : "");
2905 }
2906 else
2907 {
2908 arch_stack_top = top->prev;
2909
2910 cpu_arch_name = top->name;
2911 free (cpu_sub_arch_name);
2912 cpu_sub_arch_name = top->sub_name;
2913 cpu_arch_flags = top->flags;
2914 cpu_arch_isa = top->isa;
2915 cpu_arch_isa_flags = top->isa_flags;
2916 vector_size = top->vector_size;
2917 no_cond_jump_promotion = top->no_cond_jump_promotion;
2918
2919 XDELETE (top);
2920 }
2921
2922 (void) restore_line_pointer (e);
2923 demand_empty_rest_of_line ();
2924 return;
2925 }
2926
2927 if (strcmp (string, "default") == 0)
2928 {
2929 if (strcmp (default_arch, "iamcu") == 0)
2930 string = default_arch;
2931 else
2932 {
2933 static const i386_cpu_flags cpu_unknown_flags = CPU_UNKNOWN_FLAGS;
2934
2935 cpu_arch_name = NULL;
2936 free (cpu_sub_arch_name);
2937 cpu_sub_arch_name = NULL;
2938 cpu_arch_flags = cpu_unknown_flags;
2939 cpu_arch_isa = PROCESSOR_UNKNOWN;
2940 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
2941 if (!cpu_arch_tune_set)
2942 cpu_arch_tune = PROCESSOR_UNKNOWN;
2943
2944 vector_size = VSZ_DEFAULT;
2945
2946 j = ARRAY_SIZE (cpu_arch) + 1;
2947 }
2948 }
2949
2950 for (; j < ARRAY_SIZE (cpu_arch); j++)
2951 {
2952 if (strcmp (string + (*string == '.'), cpu_arch[j].name) == 0
2953 && (*string == '.') == (cpu_arch[j].type == PROCESSOR_NONE))
2954 {
2955 if (*string != '.')
2956 {
2957 check_cpu_arch_compatible (string, cpu_arch[j].enable);
2958
2959 if (flag_code == CODE_64BIT && !cpu_arch[j].enable.bitfield.cpu64 )
2960 {
2961 as_bad (_("64bit mode not supported on `%s'."),
2962 cpu_arch[j].name);
2963 (void) restore_line_pointer (e);
2964 ignore_rest_of_line ();
2965 return;
2966 }
2967
2968 if (flag_code == CODE_32BIT && !cpu_arch[j].enable.bitfield.cpui386)
2969 {
2970 as_bad (_("32bit mode not supported on `%s'."),
2971 cpu_arch[j].name);
2972 (void) restore_line_pointer (e);
2973 ignore_rest_of_line ();
2974 return;
2975 }
2976
2977 cpu_arch_name = cpu_arch[j].name;
2978 free (cpu_sub_arch_name);
2979 cpu_sub_arch_name = NULL;
2980 cpu_arch_flags = cpu_arch[j].enable;
2981 cpu_arch_isa = cpu_arch[j].type;
2982 cpu_arch_isa_flags = cpu_arch[j].enable;
2983 if (!cpu_arch_tune_set)
2984 cpu_arch_tune = cpu_arch_isa;
2985
2986 vector_size = VSZ_DEFAULT;
2987
2988 pre_386_16bit_warned = false;
2989 break;
2990 }
2991
2992 if (cpu_flags_all_zero (&cpu_arch[j].enable))
2993 continue;
2994
2995 isa_enable (j);
2996
2997 (void) restore_line_pointer (e);
2998
2999 switch (cpu_arch[j].vsz)
3000 {
3001 default:
3002 break;
3003
3004 case vsz_set:
3005 #ifdef SVR4_COMMENT_CHARS
3006 if (*input_line_pointer == ':' || *input_line_pointer == '/')
3007 #else
3008 if (*input_line_pointer == '/')
3009 #endif
3010 {
3011 ++input_line_pointer;
3012 switch (get_absolute_expression ())
3013 {
3014 case 512: vector_size = VSZ512; break;
3015 case 256: vector_size = VSZ256; break;
3016 case 128: vector_size = VSZ128; break;
3017 default:
3018 as_bad (_("Unrecognized vector size specifier"));
3019 ignore_rest_of_line ();
3020 return;
3021 }
3022 break;
3023 }
3024 /* Fall through. */
3025 case vsz_reset:
3026 vector_size = VSZ_DEFAULT;
3027 break;
3028 }
3029
3030 demand_empty_rest_of_line ();
3031 return;
3032 }
3033 }
3034
3035 if (startswith (string, ".no") && j >= ARRAY_SIZE (cpu_arch))
3036 {
3037 /* Disable an ISA extension. */
3038 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
3039 if (cpu_arch[j].type == PROCESSOR_NONE
3040 && strcmp (string + 3, cpu_arch[j].name) == 0)
3041 {
3042 isa_disable (j);
3043
3044 if (cpu_arch[j].vsz == vsz_set)
3045 vector_size = VSZ_DEFAULT;
3046
3047 (void) restore_line_pointer (e);
3048 demand_empty_rest_of_line ();
3049 return;
3050 }
3051 }
3052
3053 if (j == ARRAY_SIZE (cpu_arch))
3054 as_bad (_("no such architecture: `%s'"), string);
3055
3056 *input_line_pointer = e;
3057
3058 no_cond_jump_promotion = 0;
3059 if (*input_line_pointer == ','
3060 && !is_end_of_line[(unsigned char) input_line_pointer[1]])
3061 {
3062 ++input_line_pointer;
3063 e = get_symbol_name (&s);
3064 string = s;
3065
3066 if (strcmp (string, "nojumps") == 0)
3067 no_cond_jump_promotion = 1;
3068 else if (strcmp (string, "jumps") == 0)
3069 ;
3070 else
3071 as_bad (_("no such architecture modifier: `%s'"), string);
3072
3073 (void) restore_line_pointer (e);
3074 }
3075
3076 demand_empty_rest_of_line ();
3077 }
3078
3079 enum bfd_architecture
3080 i386_arch (void)
3081 {
3082 if (cpu_arch_isa == PROCESSOR_IAMCU)
3083 {
3084 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
3085 || flag_code == CODE_64BIT)
3086 as_fatal (_("Intel MCU is 32bit ELF only"));
3087 return bfd_arch_iamcu;
3088 }
3089 else
3090 return bfd_arch_i386;
3091 }
3092
3093 unsigned long
3094 i386_mach (void)
3095 {
3096 if (startswith (default_arch, "x86_64"))
3097 {
3098 if (default_arch[6] == '\0')
3099 return bfd_mach_x86_64;
3100 else
3101 return bfd_mach_x64_32;
3102 }
3103 else if (!strcmp (default_arch, "i386")
3104 || !strcmp (default_arch, "iamcu"))
3105 {
3106 if (cpu_arch_isa == PROCESSOR_IAMCU)
3107 {
3108 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
3109 as_fatal (_("Intel MCU is 32bit ELF only"));
3110 return bfd_mach_i386_iamcu;
3111 }
3112 else
3113 return bfd_mach_i386_i386;
3114 }
3115 else
3116 as_fatal (_("unknown architecture"));
3117 }
3118 \f
3119 #include "opcodes/i386-tbl.h"
3120
3121 void
3122 md_begin (void)
3123 {
3124 /* Support pseudo prefixes like {disp32}. */
3125 lex_type ['{'] = LEX_BEGIN_NAME;
3126
3127 /* Initialize op_hash hash table. */
3128 op_hash = str_htab_create ();
3129
3130 {
3131 const insn_template *const *sets = i386_op_sets;
3132 const insn_template *const *end = sets + ARRAY_SIZE (i386_op_sets) - 1;
3133
3134 /* Type checks to compensate for the conversion through void * which
3135 occurs during hash table insertion / lookup. */
3136 (void) sizeof (sets == &current_templates->start);
3137 (void) sizeof (end == &current_templates->end);
3138 for (; sets < end; ++sets)
3139 if (str_hash_insert (op_hash, insn_name (*sets), sets, 0))
3140 as_fatal (_("duplicate %s"), insn_name (*sets));
3141 }
3142
3143 /* Initialize reg_hash hash table. */
3144 reg_hash = str_htab_create ();
3145 {
3146 const reg_entry *regtab;
3147 unsigned int regtab_size = i386_regtab_size;
3148
3149 for (regtab = i386_regtab; regtab_size--; regtab++)
3150 {
3151 switch (regtab->reg_type.bitfield.class)
3152 {
3153 case Reg:
3154 if (regtab->reg_type.bitfield.dword)
3155 {
3156 if (regtab->reg_type.bitfield.instance == Accum)
3157 reg_eax = regtab;
3158 }
3159 else if (regtab->reg_type.bitfield.tbyte)
3160 {
3161 /* There's no point inserting st(<N>) in the hash table, as
3162 parentheses aren't included in register_chars[] anyway. */
3163 if (regtab->reg_type.bitfield.instance != Accum)
3164 continue;
3165 reg_st0 = regtab;
3166 }
3167 break;
3168
3169 case SReg:
3170 switch (regtab->reg_num)
3171 {
3172 case 0: reg_es = regtab; break;
3173 case 2: reg_ss = regtab; break;
3174 case 3: reg_ds = regtab; break;
3175 }
3176 break;
3177
3178 case RegMask:
3179 if (!regtab->reg_num)
3180 reg_k0 = regtab;
3181 break;
3182 }
3183
3184 if (str_hash_insert (reg_hash, regtab->reg_name, regtab, 0) != NULL)
3185 as_fatal (_("duplicate %s"), regtab->reg_name);
3186 }
3187 }
3188
3189 /* Fill in lexical tables: mnemonic_chars, operand_chars. */
3190 {
3191 int c;
3192 const char *p;
3193
3194 for (c = 0; c < 256; c++)
3195 {
3196 if (ISDIGIT (c) || ISLOWER (c))
3197 {
3198 mnemonic_chars[c] = c;
3199 register_chars[c] = c;
3200 operand_chars[c] = c;
3201 }
3202 else if (ISUPPER (c))
3203 {
3204 mnemonic_chars[c] = TOLOWER (c);
3205 register_chars[c] = mnemonic_chars[c];
3206 operand_chars[c] = c;
3207 }
3208 #ifdef SVR4_COMMENT_CHARS
3209 else if (c == '\\' && strchr (i386_comment_chars, '/'))
3210 operand_chars[c] = c;
3211 #endif
3212
3213 if (c >= 128)
3214 operand_chars[c] = c;
3215 }
3216
3217 mnemonic_chars['_'] = '_';
3218 mnemonic_chars['-'] = '-';
3219 mnemonic_chars['.'] = '.';
3220
3221 for (p = extra_symbol_chars; *p != '\0'; p++)
3222 operand_chars[(unsigned char) *p] = *p;
3223 for (p = operand_special_chars; *p != '\0'; p++)
3224 operand_chars[(unsigned char) *p] = *p;
3225 }
3226
3227 if (flag_code == CODE_64BIT)
3228 {
3229 #if defined (OBJ_COFF) && defined (TE_PE)
3230 x86_dwarf2_return_column = (OUTPUT_FLAVOR == bfd_target_coff_flavour
3231 ? 32 : 16);
3232 #else
3233 x86_dwarf2_return_column = 16;
3234 #endif
3235 x86_cie_data_alignment = -8;
3236 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3237 x86_sframe_cfa_sp_reg = 7;
3238 x86_sframe_cfa_fp_reg = 6;
3239 #endif
3240 }
3241 else
3242 {
3243 x86_dwarf2_return_column = 8;
3244 x86_cie_data_alignment = -4;
3245 }
3246
3247 /* NB: FUSED_JCC_PADDING frag must have sufficient room so that it
3248 can be turned into BRANCH_PREFIX frag. */
3249 if (align_branch_prefix_size > MAX_FUSED_JCC_PADDING_SIZE)
3250 abort ();
3251 }
3252
3253 void
3254 i386_print_statistics (FILE *file)
3255 {
3256 htab_print_statistics (file, "i386 opcode", op_hash);
3257 htab_print_statistics (file, "i386 register", reg_hash);
3258 }
3259
3260 void
3261 i386_md_end (void)
3262 {
3263 htab_delete (op_hash);
3264 htab_delete (reg_hash);
3265 }
3266 \f
3267 #ifdef DEBUG386
3268
3269 /* Debugging routines for md_assemble. */
3270 static void pte (insn_template *);
3271 static void pt (i386_operand_type);
3272 static void pe (expressionS *);
3273 static void ps (symbolS *);
3274
3275 static void
3276 pi (const char *line, i386_insn *x)
3277 {
3278 unsigned int j;
3279
3280 fprintf (stdout, "%s: template ", line);
3281 pte (&x->tm);
3282 fprintf (stdout, " address: base %s index %s scale %x\n",
3283 x->base_reg ? x->base_reg->reg_name : "none",
3284 x->index_reg ? x->index_reg->reg_name : "none",
3285 x->log2_scale_factor);
3286 fprintf (stdout, " modrm: mode %x reg %x reg/mem %x\n",
3287 x->rm.mode, x->rm.reg, x->rm.regmem);
3288 fprintf (stdout, " sib: base %x index %x scale %x\n",
3289 x->sib.base, x->sib.index, x->sib.scale);
3290 fprintf (stdout, " rex: 64bit %x extX %x extY %x extZ %x\n",
3291 (x->rex & REX_W) != 0,
3292 (x->rex & REX_R) != 0,
3293 (x->rex & REX_X) != 0,
3294 (x->rex & REX_B) != 0);
3295 for (j = 0; j < x->operands; j++)
3296 {
3297 fprintf (stdout, " #%d: ", j + 1);
3298 pt (x->types[j]);
3299 fprintf (stdout, "\n");
3300 if (x->types[j].bitfield.class == Reg
3301 || x->types[j].bitfield.class == RegMMX
3302 || x->types[j].bitfield.class == RegSIMD
3303 || x->types[j].bitfield.class == RegMask
3304 || x->types[j].bitfield.class == SReg
3305 || x->types[j].bitfield.class == RegCR
3306 || x->types[j].bitfield.class == RegDR
3307 || x->types[j].bitfield.class == RegTR
3308 || x->types[j].bitfield.class == RegBND)
3309 fprintf (stdout, "%s\n", x->op[j].regs->reg_name);
3310 if (operand_type_check (x->types[j], imm))
3311 pe (x->op[j].imms);
3312 if (operand_type_check (x->types[j], disp))
3313 pe (x->op[j].disps);
3314 }
3315 }
3316
3317 static void
3318 pte (insn_template *t)
3319 {
3320 static const unsigned char opc_pfx[] = { 0, 0x66, 0xf3, 0xf2 };
3321 static const char *const opc_spc[] = {
3322 NULL, "0f", "0f38", "0f3a", NULL, "evexmap5", "evexmap6", NULL,
3323 "XOP08", "XOP09", "XOP0A",
3324 };
3325 unsigned int j;
3326
3327 fprintf (stdout, " %d operands ", t->operands);
3328 if (opc_pfx[t->opcode_modifier.opcodeprefix])
3329 fprintf (stdout, "pfx %x ", opc_pfx[t->opcode_modifier.opcodeprefix]);
3330 if (opc_spc[t->opcode_space])
3331 fprintf (stdout, "space %s ", opc_spc[t->opcode_space]);
3332 fprintf (stdout, "opcode %x ", t->base_opcode);
3333 if (t->extension_opcode != None)
3334 fprintf (stdout, "ext %x ", t->extension_opcode);
3335 if (t->opcode_modifier.d)
3336 fprintf (stdout, "D");
3337 if (t->opcode_modifier.w)
3338 fprintf (stdout, "W");
3339 fprintf (stdout, "\n");
3340 for (j = 0; j < t->operands; j++)
3341 {
3342 fprintf (stdout, " #%d type ", j + 1);
3343 pt (t->operand_types[j]);
3344 fprintf (stdout, "\n");
3345 }
3346 }
3347
3348 static void
3349 pe (expressionS *e)
3350 {
3351 fprintf (stdout, " operation %d\n", e->X_op);
3352 fprintf (stdout, " add_number %" PRId64 " (%" PRIx64 ")\n",
3353 (int64_t) e->X_add_number, (uint64_t) (valueT) e->X_add_number);
3354 if (e->X_add_symbol)
3355 {
3356 fprintf (stdout, " add_symbol ");
3357 ps (e->X_add_symbol);
3358 fprintf (stdout, "\n");
3359 }
3360 if (e->X_op_symbol)
3361 {
3362 fprintf (stdout, " op_symbol ");
3363 ps (e->X_op_symbol);
3364 fprintf (stdout, "\n");
3365 }
3366 }
3367
3368 static void
3369 ps (symbolS *s)
3370 {
3371 fprintf (stdout, "%s type %s%s",
3372 S_GET_NAME (s),
3373 S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
3374 segment_name (S_GET_SEGMENT (s)));
3375 }
3376
3377 static struct type_name
3378 {
3379 i386_operand_type mask;
3380 const char *name;
3381 }
3382 const type_names[] =
3383 {
3384 { { .bitfield = { .class = Reg, .byte = 1 } }, "r8" },
3385 { { .bitfield = { .class = Reg, .word = 1 } }, "r16" },
3386 { { .bitfield = { .class = Reg, .dword = 1 } }, "r32" },
3387 { { .bitfield = { .class = Reg, .qword = 1 } }, "r64" },
3388 { { .bitfield = { .instance = Accum, .byte = 1 } }, "acc8" },
3389 { { .bitfield = { .instance = Accum, .word = 1 } }, "acc16" },
3390 { { .bitfield = { .instance = Accum, .dword = 1 } }, "acc32" },
3391 { { .bitfield = { .instance = Accum, .qword = 1 } }, "acc64" },
3392 { { .bitfield = { .imm8 = 1 } }, "i8" },
3393 { { .bitfield = { .imm8s = 1 } }, "i8s" },
3394 { { .bitfield = { .imm16 = 1 } }, "i16" },
3395 { { .bitfield = { .imm32 = 1 } }, "i32" },
3396 { { .bitfield = { .imm32s = 1 } }, "i32s" },
3397 { { .bitfield = { .imm64 = 1 } }, "i64" },
3398 { { .bitfield = { .imm1 = 1 } }, "i1" },
3399 { { .bitfield = { .baseindex = 1 } }, "BaseIndex" },
3400 { { .bitfield = { .disp8 = 1 } }, "d8" },
3401 { { .bitfield = { .disp16 = 1 } }, "d16" },
3402 { { .bitfield = { .disp32 = 1 } }, "d32" },
3403 { { .bitfield = { .disp64 = 1 } }, "d64" },
3404 { { .bitfield = { .instance = RegD, .word = 1 } }, "InOutPortReg" },
3405 { { .bitfield = { .instance = RegC, .byte = 1 } }, "ShiftCount" },
3406 { { .bitfield = { .class = RegCR } }, "control reg" },
3407 { { .bitfield = { .class = RegTR } }, "test reg" },
3408 { { .bitfield = { .class = RegDR } }, "debug reg" },
3409 { { .bitfield = { .class = Reg, .tbyte = 1 } }, "FReg" },
3410 { { .bitfield = { .instance = Accum, .tbyte = 1 } }, "FAcc" },
3411 { { .bitfield = { .class = SReg } }, "SReg" },
3412 { { .bitfield = { .class = RegMMX } }, "rMMX" },
3413 { { .bitfield = { .class = RegSIMD, .xmmword = 1 } }, "rXMM" },
3414 { { .bitfield = { .class = RegSIMD, .ymmword = 1 } }, "rYMM" },
3415 { { .bitfield = { .class = RegSIMD, .zmmword = 1 } }, "rZMM" },
3416 { { .bitfield = { .class = RegSIMD, .tmmword = 1 } }, "rTMM" },
3417 { { .bitfield = { .class = RegMask } }, "Mask reg" },
3418 };
3419
3420 static void
3421 pt (i386_operand_type t)
3422 {
3423 unsigned int j;
3424 i386_operand_type a;
3425
3426 for (j = 0; j < ARRAY_SIZE (type_names); j++)
3427 {
3428 a = operand_type_and (t, type_names[j].mask);
3429 if (operand_type_equal (&a, &type_names[j].mask))
3430 fprintf (stdout, "%s, ", type_names[j].name);
3431 }
3432 fflush (stdout);
3433 }
3434
3435 #endif /* DEBUG386 */
3436 \f
3437 static bfd_reloc_code_real_type
3438 reloc (unsigned int size,
3439 int pcrel,
3440 int sign,
3441 bfd_reloc_code_real_type other)
3442 {
3443 if (other != NO_RELOC)
3444 {
3445 reloc_howto_type *rel;
3446
3447 if (size == 8)
3448 switch (other)
3449 {
3450 case BFD_RELOC_X86_64_GOT32:
3451 return BFD_RELOC_X86_64_GOT64;
3452 break;
3453 case BFD_RELOC_X86_64_GOTPLT64:
3454 return BFD_RELOC_X86_64_GOTPLT64;
3455 break;
3456 case BFD_RELOC_X86_64_PLTOFF64:
3457 return BFD_RELOC_X86_64_PLTOFF64;
3458 break;
3459 case BFD_RELOC_X86_64_GOTPC32:
3460 other = BFD_RELOC_X86_64_GOTPC64;
3461 break;
3462 case BFD_RELOC_X86_64_GOTPCREL:
3463 other = BFD_RELOC_X86_64_GOTPCREL64;
3464 break;
3465 case BFD_RELOC_X86_64_TPOFF32:
3466 other = BFD_RELOC_X86_64_TPOFF64;
3467 break;
3468 case BFD_RELOC_X86_64_DTPOFF32:
3469 other = BFD_RELOC_X86_64_DTPOFF64;
3470 break;
3471 default:
3472 break;
3473 }
3474
3475 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3476 if (other == BFD_RELOC_SIZE32)
3477 {
3478 if (size == 8)
3479 other = BFD_RELOC_SIZE64;
3480 if (pcrel)
3481 {
3482 as_bad (_("there are no pc-relative size relocations"));
3483 return NO_RELOC;
3484 }
3485 }
3486 #endif
3487
3488 /* Sign-checking 4-byte relocations in 16-/32-bit code is pointless. */
3489 if (size == 4 && (flag_code != CODE_64BIT || disallow_64bit_reloc))
3490 sign = -1;
3491
3492 rel = bfd_reloc_type_lookup (stdoutput, other);
3493 if (!rel)
3494 as_bad (_("unknown relocation (%u)"), other);
3495 else if (size != bfd_get_reloc_size (rel))
3496 as_bad (_("%u-byte relocation cannot be applied to %u-byte field"),
3497 bfd_get_reloc_size (rel),
3498 size);
3499 else if (pcrel && !rel->pc_relative)
3500 as_bad (_("non-pc-relative relocation for pc-relative field"));
3501 else if ((rel->complain_on_overflow == complain_overflow_signed
3502 && !sign)
3503 || (rel->complain_on_overflow == complain_overflow_unsigned
3504 && sign > 0))
3505 as_bad (_("relocated field and relocation type differ in signedness"));
3506 else
3507 return other;
3508 return NO_RELOC;
3509 }
3510
3511 if (pcrel)
3512 {
3513 if (!sign)
3514 as_bad (_("there are no unsigned pc-relative relocations"));
3515 switch (size)
3516 {
3517 case 1: return BFD_RELOC_8_PCREL;
3518 case 2: return BFD_RELOC_16_PCREL;
3519 case 4: return BFD_RELOC_32_PCREL;
3520 case 8: return BFD_RELOC_64_PCREL;
3521 }
3522 as_bad (_("cannot do %u byte pc-relative relocation"), size);
3523 }
3524 else
3525 {
3526 if (sign > 0)
3527 switch (size)
3528 {
3529 case 4: return BFD_RELOC_X86_64_32S;
3530 }
3531 else
3532 switch (size)
3533 {
3534 case 1: return BFD_RELOC_8;
3535 case 2: return BFD_RELOC_16;
3536 case 4: return BFD_RELOC_32;
3537 case 8: return BFD_RELOC_64;
3538 }
3539 as_bad (_("cannot do %s %u byte relocation"),
3540 sign > 0 ? "signed" : "unsigned", size);
3541 }
3542
3543 return NO_RELOC;
3544 }
3545
3546 /* Here we decide which fixups can be adjusted to make them relative to
3547 the beginning of the section instead of the symbol. Basically we need
3548 to make sure that the dynamic relocations are done correctly, so in
3549 some cases we force the original symbol to be used. */
3550
3551 int
3552 tc_i386_fix_adjustable (fixS *fixP ATTRIBUTE_UNUSED)
3553 {
3554 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3555 if (!IS_ELF)
3556 return 1;
3557
3558 /* Don't adjust pc-relative references to merge sections in 64-bit
3559 mode. */
3560 if (use_rela_relocations
3561 && (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0
3562 && fixP->fx_pcrel)
3563 return 0;
3564
3565 /* The x86_64 GOTPCREL are represented as 32bit PCrel relocations
3566 and changed later by validate_fix. */
3567 if (GOT_symbol && fixP->fx_subsy == GOT_symbol
3568 && fixP->fx_r_type == BFD_RELOC_32_PCREL)
3569 return 0;
3570
3571 /* Adjust_reloc_syms doesn't know about the GOT. Need to keep symbol
3572 for size relocations. */
3573 if (fixP->fx_r_type == BFD_RELOC_SIZE32
3574 || fixP->fx_r_type == BFD_RELOC_SIZE64
3575 || fixP->fx_r_type == BFD_RELOC_386_GOTOFF
3576 || fixP->fx_r_type == BFD_RELOC_386_GOT32
3577 || fixP->fx_r_type == BFD_RELOC_386_GOT32X
3578 || fixP->fx_r_type == BFD_RELOC_386_TLS_GD
3579 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDM
3580 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDO_32
3581 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE_32
3582 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE
3583 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTIE
3584 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE_32
3585 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE
3586 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTDESC
3587 || fixP->fx_r_type == BFD_RELOC_386_TLS_DESC_CALL
3588 || fixP->fx_r_type == BFD_RELOC_X86_64_GOT32
3589 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCREL
3590 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCRELX
3591 || fixP->fx_r_type == BFD_RELOC_X86_64_REX_GOTPCRELX
3592 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSGD
3593 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSLD
3594 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF32
3595 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF64
3596 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF
3597 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF32
3598 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF64
3599 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTOFF64
3600 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
3601 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSDESC_CALL
3602 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3603 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3604 return 0;
3605 #endif
3606 return 1;
3607 }
3608
3609 static INLINE bool
3610 want_disp32 (const insn_template *t)
3611 {
3612 return flag_code != CODE_64BIT
3613 || i.prefix[ADDR_PREFIX]
3614 || (t->mnem_off == MN_lea
3615 && (!i.types[1].bitfield.qword
3616 || t->opcode_modifier.size == SIZE32));
3617 }
3618
3619 static int
3620 intel_float_operand (const char *mnemonic)
3621 {
3622 /* Note that the value returned is meaningful only for opcodes with (memory)
3623 operands, hence the code here is free to improperly handle opcodes that
3624 have no operands (for better performance and smaller code). */
3625
3626 if (mnemonic[0] != 'f')
3627 return 0; /* non-math */
3628
3629 switch (mnemonic[1])
3630 {
3631 /* fclex, fdecstp, fdisi, femms, feni, fincstp, finit, fsetpm, and
3632 the fs segment override prefix not currently handled because no
3633 call path can make opcodes without operands get here */
3634 case 'i':
3635 return 2 /* integer op */;
3636 case 'l':
3637 if (mnemonic[2] == 'd' && (mnemonic[3] == 'c' || mnemonic[3] == 'e'))
3638 return 3; /* fldcw/fldenv */
3639 break;
3640 case 'n':
3641 if (mnemonic[2] != 'o' /* fnop */)
3642 return 3; /* non-waiting control op */
3643 break;
3644 case 'r':
3645 if (mnemonic[2] == 's')
3646 return 3; /* frstor/frstpm */
3647 break;
3648 case 's':
3649 if (mnemonic[2] == 'a')
3650 return 3; /* fsave */
3651 if (mnemonic[2] == 't')
3652 {
3653 switch (mnemonic[3])
3654 {
3655 case 'c': /* fstcw */
3656 case 'd': /* fstdw */
3657 case 'e': /* fstenv */
3658 case 's': /* fsts[gw] */
3659 return 3;
3660 }
3661 }
3662 break;
3663 case 'x':
3664 if (mnemonic[2] == 'r' || mnemonic[2] == 's')
3665 return 0; /* fxsave/fxrstor are not really math ops */
3666 break;
3667 }
3668
3669 return 1;
3670 }
3671
3672 static INLINE void
3673 install_template (const insn_template *t)
3674 {
3675 unsigned int l;
3676
3677 i.tm = *t;
3678
3679 /* Dual VEX/EVEX templates need stripping one of the possible variants. */
3680 if (t->opcode_modifier.vex && t->opcode_modifier.evex)
3681 {
3682 if ((is_cpu (t, CpuAVX) || is_cpu (t, CpuAVX2))
3683 && is_cpu (t, CpuAVX512F))
3684 {
3685 if (need_evex_encoding ())
3686 {
3687 i.tm.opcode_modifier.vex = 0;
3688 i.tm.cpu.bitfield.cpuavx = 0;
3689 if (is_cpu (&i.tm, CpuAVX2))
3690 i.tm.cpu.bitfield.isa = 0;
3691 }
3692 else
3693 {
3694 i.tm.opcode_modifier.evex = 0;
3695 i.tm.cpu.bitfield.cpuavx512f = 0;
3696 }
3697 }
3698 }
3699
3700 /* Note that for pseudo prefixes this produces a length of 1. But for them
3701 the length isn't interesting at all. */
3702 for (l = 1; l < 4; ++l)
3703 if (!(t->base_opcode >> (8 * l)))
3704 break;
3705
3706 i.opcode_length = l;
3707 }
3708
3709 /* Build the VEX prefix. */
3710
3711 static void
3712 build_vex_prefix (const insn_template *t)
3713 {
3714 unsigned int register_specifier;
3715 unsigned int vector_length;
3716 unsigned int w;
3717
3718 /* Check register specifier. */
3719 if (i.vex.register_specifier)
3720 {
3721 register_specifier =
3722 ~register_number (i.vex.register_specifier) & 0xf;
3723 gas_assert ((i.vex.register_specifier->reg_flags & RegVRex) == 0);
3724 }
3725 else
3726 register_specifier = 0xf;
3727
3728 /* Use 2-byte VEX prefix by swapping destination and source operand
3729 if there are more than 1 register operand. */
3730 if (i.reg_operands > 1
3731 && i.vec_encoding != vex_encoding_vex3
3732 && i.dir_encoding == dir_encoding_default
3733 && i.operands == i.reg_operands
3734 && operand_type_equal (&i.types[0], &i.types[i.operands - 1])
3735 && i.tm.opcode_space == SPACE_0F
3736 && (i.tm.opcode_modifier.load || i.tm.opcode_modifier.d)
3737 && i.rex == REX_B)
3738 {
3739 unsigned int xchg;
3740
3741 swap_2_operands (0, i.operands - 1);
3742
3743 gas_assert (i.rm.mode == 3);
3744
3745 i.rex = REX_R;
3746 xchg = i.rm.regmem;
3747 i.rm.regmem = i.rm.reg;
3748 i.rm.reg = xchg;
3749
3750 if (i.tm.opcode_modifier.d)
3751 i.tm.base_opcode ^= (i.tm.base_opcode & 0xee) != 0x6e
3752 ? Opcode_ExtD : Opcode_SIMD_IntD;
3753 else /* Use the next insn. */
3754 install_template (&t[1]);
3755 }
3756
3757 /* Use 2-byte VEX prefix by swapping commutative source operands if there
3758 are no memory operands and at least 3 register ones. */
3759 if (i.reg_operands >= 3
3760 && i.vec_encoding != vex_encoding_vex3
3761 && i.reg_operands == i.operands - i.imm_operands
3762 && i.tm.opcode_modifier.vex
3763 && i.tm.opcode_modifier.commutative
3764 && (i.tm.opcode_modifier.sse2avx
3765 || (optimize > 1 && !i.no_optimize))
3766 && i.rex == REX_B
3767 && i.vex.register_specifier
3768 && !(i.vex.register_specifier->reg_flags & RegRex))
3769 {
3770 unsigned int xchg = i.operands - i.reg_operands;
3771
3772 gas_assert (i.tm.opcode_space == SPACE_0F);
3773 gas_assert (!i.tm.opcode_modifier.sae);
3774 gas_assert (operand_type_equal (&i.types[i.operands - 2],
3775 &i.types[i.operands - 3]));
3776 gas_assert (i.rm.mode == 3);
3777
3778 swap_2_operands (xchg, xchg + 1);
3779
3780 i.rex = 0;
3781 xchg = i.rm.regmem | 8;
3782 i.rm.regmem = ~register_specifier & 0xf;
3783 gas_assert (!(i.rm.regmem & 8));
3784 i.vex.register_specifier += xchg - i.rm.regmem;
3785 register_specifier = ~xchg & 0xf;
3786 }
3787
3788 if (i.tm.opcode_modifier.vex == VEXScalar)
3789 vector_length = avxscalar;
3790 else if (i.tm.opcode_modifier.vex == VEX256)
3791 vector_length = 1;
3792 else if (dot_insn () && i.tm.opcode_modifier.vex == VEX128)
3793 vector_length = 0;
3794 else
3795 {
3796 unsigned int op;
3797
3798 /* Determine vector length from the last multi-length vector
3799 operand. */
3800 vector_length = 0;
3801 for (op = t->operands; op--;)
3802 if (t->operand_types[op].bitfield.xmmword
3803 && t->operand_types[op].bitfield.ymmword
3804 && i.types[op].bitfield.ymmword)
3805 {
3806 vector_length = 1;
3807 break;
3808 }
3809 }
3810
3811 /* Check the REX.W bit and VEXW. */
3812 if (i.tm.opcode_modifier.vexw == VEXWIG)
3813 w = (vexwig == vexw1 || (i.rex & REX_W)) ? 1 : 0;
3814 else if (i.tm.opcode_modifier.vexw)
3815 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3816 else
3817 w = (flag_code == CODE_64BIT ? i.rex & REX_W : vexwig == vexw1) ? 1 : 0;
3818
3819 /* Use 2-byte VEX prefix if possible. */
3820 if (w == 0
3821 && i.vec_encoding != vex_encoding_vex3
3822 && i.tm.opcode_space == SPACE_0F
3823 && (i.rex & (REX_W | REX_X | REX_B)) == 0)
3824 {
3825 /* 2-byte VEX prefix. */
3826 unsigned int r;
3827
3828 i.vex.length = 2;
3829 i.vex.bytes[0] = 0xc5;
3830
3831 /* Check the REX.R bit. */
3832 r = (i.rex & REX_R) ? 0 : 1;
3833 i.vex.bytes[1] = (r << 7
3834 | register_specifier << 3
3835 | vector_length << 2
3836 | i.tm.opcode_modifier.opcodeprefix);
3837 }
3838 else
3839 {
3840 /* 3-byte VEX prefix. */
3841 i.vex.length = 3;
3842
3843 switch (i.tm.opcode_space)
3844 {
3845 case SPACE_0F:
3846 case SPACE_0F38:
3847 case SPACE_0F3A:
3848 case SPACE_VEXMAP7:
3849 i.vex.bytes[0] = 0xc4;
3850 break;
3851 case SPACE_XOP08:
3852 case SPACE_XOP09:
3853 case SPACE_XOP0A:
3854 i.vex.bytes[0] = 0x8f;
3855 break;
3856 default:
3857 abort ();
3858 }
3859
3860 /* The high 3 bits of the second VEX byte are 1's compliment
3861 of RXB bits from REX. */
3862 i.vex.bytes[1] = ((~i.rex & 7) << 5)
3863 | (!dot_insn () ? i.tm.opcode_space
3864 : i.insn_opcode_space);
3865
3866 i.vex.bytes[2] = (w << 7
3867 | register_specifier << 3
3868 | vector_length << 2
3869 | i.tm.opcode_modifier.opcodeprefix);
3870 }
3871 }
3872
3873 static INLINE bool
3874 is_evex_encoding (const insn_template *t)
3875 {
3876 return t->opcode_modifier.evex || t->opcode_modifier.disp8memshift
3877 || t->opcode_modifier.broadcast || t->opcode_modifier.masking
3878 || t->opcode_modifier.sae;
3879 }
3880
3881 static INLINE bool
3882 is_any_vex_encoding (const insn_template *t)
3883 {
3884 return t->opcode_modifier.vex || is_evex_encoding (t);
3885 }
3886
3887 static unsigned int
3888 get_broadcast_bytes (const insn_template *t, bool diag)
3889 {
3890 unsigned int op, bytes;
3891 const i386_operand_type *types;
3892
3893 if (i.broadcast.type)
3894 return (1 << (t->opcode_modifier.broadcast - 1)) * i.broadcast.type;
3895
3896 gas_assert (intel_syntax);
3897
3898 for (op = 0; op < t->operands; ++op)
3899 if (t->operand_types[op].bitfield.baseindex)
3900 break;
3901
3902 gas_assert (op < t->operands);
3903
3904 if (t->opcode_modifier.evex
3905 && t->opcode_modifier.evex != EVEXDYN)
3906 switch (i.broadcast.bytes)
3907 {
3908 case 1:
3909 if (t->operand_types[op].bitfield.word)
3910 return 2;
3911 /* Fall through. */
3912 case 2:
3913 if (t->operand_types[op].bitfield.dword)
3914 return 4;
3915 /* Fall through. */
3916 case 4:
3917 if (t->operand_types[op].bitfield.qword)
3918 return 8;
3919 /* Fall through. */
3920 case 8:
3921 if (t->operand_types[op].bitfield.xmmword)
3922 return 16;
3923 if (t->operand_types[op].bitfield.ymmword)
3924 return 32;
3925 if (t->operand_types[op].bitfield.zmmword)
3926 return 64;
3927 /* Fall through. */
3928 default:
3929 abort ();
3930 }
3931
3932 gas_assert (op + 1 < t->operands);
3933
3934 if (t->operand_types[op + 1].bitfield.xmmword
3935 + t->operand_types[op + 1].bitfield.ymmword
3936 + t->operand_types[op + 1].bitfield.zmmword > 1)
3937 {
3938 types = &i.types[op + 1];
3939 diag = false;
3940 }
3941 else /* Ambiguous - guess with a preference to non-AVX512VL forms. */
3942 types = &t->operand_types[op];
3943
3944 if (types->bitfield.zmmword)
3945 bytes = 64;
3946 else if (types->bitfield.ymmword)
3947 bytes = 32;
3948 else
3949 bytes = 16;
3950
3951 if (diag)
3952 as_warn (_("ambiguous broadcast for `%s', using %u-bit form"),
3953 insn_name (t), bytes * 8);
3954
3955 return bytes;
3956 }
3957
3958 /* Build the EVEX prefix. */
3959
3960 static void
3961 build_evex_prefix (void)
3962 {
3963 unsigned int register_specifier, w;
3964 rex_byte vrex_used = 0;
3965
3966 /* Check register specifier. */
3967 if (i.vex.register_specifier)
3968 {
3969 gas_assert ((i.vrex & REX_X) == 0);
3970
3971 register_specifier = i.vex.register_specifier->reg_num;
3972 if ((i.vex.register_specifier->reg_flags & RegRex))
3973 register_specifier += 8;
3974 /* The upper 16 registers are encoded in the fourth byte of the
3975 EVEX prefix. */
3976 if (!(i.vex.register_specifier->reg_flags & RegVRex))
3977 i.vex.bytes[3] = 0x8;
3978 register_specifier = ~register_specifier & 0xf;
3979 }
3980 else
3981 {
3982 register_specifier = 0xf;
3983
3984 /* Encode upper 16 vector index register in the fourth byte of
3985 the EVEX prefix. */
3986 if (!(i.vrex & REX_X))
3987 i.vex.bytes[3] = 0x8;
3988 else
3989 vrex_used |= REX_X;
3990 }
3991
3992 /* 4 byte EVEX prefix. */
3993 i.vex.length = 4;
3994 i.vex.bytes[0] = 0x62;
3995
3996 /* The high 3 bits of the second EVEX byte are 1's compliment of RXB
3997 bits from REX. */
3998 gas_assert (i.tm.opcode_space >= SPACE_0F);
3999 gas_assert (i.tm.opcode_space <= SPACE_EVEXMAP6);
4000 i.vex.bytes[1] = ((~i.rex & 7) << 5)
4001 | (!dot_insn () ? i.tm.opcode_space
4002 : i.insn_opcode_space);
4003
4004 /* The fifth bit of the second EVEX byte is 1's compliment of the
4005 REX_R bit in VREX. */
4006 if (!(i.vrex & REX_R))
4007 i.vex.bytes[1] |= 0x10;
4008 else
4009 vrex_used |= REX_R;
4010
4011 if ((i.reg_operands + i.imm_operands) == i.operands)
4012 {
4013 /* When all operands are registers, the REX_X bit in REX is not
4014 used. We reuse it to encode the upper 16 registers, which is
4015 indicated by the REX_B bit in VREX. The REX_X bit is encoded
4016 as 1's compliment. */
4017 if ((i.vrex & REX_B))
4018 {
4019 vrex_used |= REX_B;
4020 i.vex.bytes[1] &= ~0x40;
4021 }
4022 }
4023
4024 /* EVEX instructions shouldn't need the REX prefix. */
4025 i.vrex &= ~vrex_used;
4026 gas_assert (i.vrex == 0);
4027
4028 /* Check the REX.W bit and VEXW. */
4029 if (i.tm.opcode_modifier.vexw == VEXWIG)
4030 w = (evexwig == evexw1 || (i.rex & REX_W)) ? 1 : 0;
4031 else if (i.tm.opcode_modifier.vexw)
4032 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
4033 else
4034 w = (flag_code == CODE_64BIT ? i.rex & REX_W : evexwig == evexw1) ? 1 : 0;
4035
4036 /* The third byte of the EVEX prefix. */
4037 i.vex.bytes[2] = ((w << 7)
4038 | (register_specifier << 3)
4039 | 4 /* Encode the U bit. */
4040 | i.tm.opcode_modifier.opcodeprefix);
4041
4042 /* The fourth byte of the EVEX prefix. */
4043 /* The zeroing-masking bit. */
4044 if (i.mask.reg && i.mask.zeroing)
4045 i.vex.bytes[3] |= 0x80;
4046
4047 /* Don't always set the broadcast bit if there is no RC. */
4048 if (i.rounding.type == rc_none)
4049 {
4050 /* Encode the vector length. */
4051 unsigned int vec_length;
4052
4053 if (!i.tm.opcode_modifier.evex
4054 || i.tm.opcode_modifier.evex == EVEXDYN)
4055 {
4056 unsigned int op;
4057
4058 /* Determine vector length from the last multi-length vector
4059 operand. */
4060 for (op = i.operands; op--;)
4061 if (i.tm.operand_types[op].bitfield.xmmword
4062 + i.tm.operand_types[op].bitfield.ymmword
4063 + i.tm.operand_types[op].bitfield.zmmword > 1)
4064 {
4065 if (i.types[op].bitfield.zmmword)
4066 {
4067 i.tm.opcode_modifier.evex = EVEX512;
4068 break;
4069 }
4070 else if (i.types[op].bitfield.ymmword)
4071 {
4072 i.tm.opcode_modifier.evex = EVEX256;
4073 break;
4074 }
4075 else if (i.types[op].bitfield.xmmword)
4076 {
4077 i.tm.opcode_modifier.evex = EVEX128;
4078 break;
4079 }
4080 else if ((i.broadcast.type || i.broadcast.bytes)
4081 && op == i.broadcast.operand)
4082 {
4083 switch (get_broadcast_bytes (&i.tm, true))
4084 {
4085 case 64:
4086 i.tm.opcode_modifier.evex = EVEX512;
4087 break;
4088 case 32:
4089 i.tm.opcode_modifier.evex = EVEX256;
4090 break;
4091 case 16:
4092 i.tm.opcode_modifier.evex = EVEX128;
4093 break;
4094 default:
4095 abort ();
4096 }
4097 break;
4098 }
4099 }
4100
4101 if (op >= MAX_OPERANDS)
4102 abort ();
4103 }
4104
4105 switch (i.tm.opcode_modifier.evex)
4106 {
4107 case EVEXLIG: /* LL' is ignored */
4108 vec_length = evexlig << 5;
4109 break;
4110 case EVEX128:
4111 vec_length = 0 << 5;
4112 break;
4113 case EVEX256:
4114 vec_length = 1 << 5;
4115 break;
4116 case EVEX512:
4117 vec_length = 2 << 5;
4118 break;
4119 case EVEX_L3:
4120 if (dot_insn ())
4121 {
4122 vec_length = 3 << 5;
4123 break;
4124 }
4125 /* Fall through. */
4126 default:
4127 abort ();
4128 break;
4129 }
4130 i.vex.bytes[3] |= vec_length;
4131 /* Encode the broadcast bit. */
4132 if (i.broadcast.type || i.broadcast.bytes)
4133 i.vex.bytes[3] |= 0x10;
4134 }
4135 else if (i.rounding.type != saeonly)
4136 i.vex.bytes[3] |= 0x10 | (i.rounding.type << 5);
4137 else
4138 i.vex.bytes[3] |= 0x10 | (evexrcig << 5);
4139
4140 if (i.mask.reg)
4141 i.vex.bytes[3] |= i.mask.reg->reg_num;
4142 }
4143
4144 static void
4145 process_immext (void)
4146 {
4147 expressionS *exp;
4148
4149 /* These AMD 3DNow! and SSE2 instructions have an opcode suffix
4150 which is coded in the same place as an 8-bit immediate field
4151 would be. Here we fake an 8-bit immediate operand from the
4152 opcode suffix stored in tm.extension_opcode.
4153
4154 AVX instructions also use this encoding, for some of
4155 3 argument instructions. */
4156
4157 gas_assert (i.imm_operands <= 1
4158 && (i.operands <= 2
4159 || (is_any_vex_encoding (&i.tm)
4160 && i.operands <= 4)));
4161
4162 exp = &im_expressions[i.imm_operands++];
4163 i.op[i.operands].imms = exp;
4164 i.types[i.operands].bitfield.imm8 = 1;
4165 i.operands++;
4166 exp->X_op = O_constant;
4167 exp->X_add_number = i.tm.extension_opcode;
4168 i.tm.extension_opcode = None;
4169 }
4170
4171
4172 static int
4173 check_hle (void)
4174 {
4175 switch (i.tm.opcode_modifier.prefixok)
4176 {
4177 default:
4178 abort ();
4179 case PrefixLock:
4180 case PrefixNone:
4181 case PrefixNoTrack:
4182 case PrefixRep:
4183 as_bad (_("invalid instruction `%s' after `%s'"),
4184 insn_name (&i.tm), i.hle_prefix);
4185 return 0;
4186 case PrefixHLELock:
4187 if (i.prefix[LOCK_PREFIX])
4188 return 1;
4189 as_bad (_("missing `lock' with `%s'"), i.hle_prefix);
4190 return 0;
4191 case PrefixHLEAny:
4192 return 1;
4193 case PrefixHLERelease:
4194 if (i.prefix[HLE_PREFIX] != XRELEASE_PREFIX_OPCODE)
4195 {
4196 as_bad (_("instruction `%s' after `xacquire' not allowed"),
4197 insn_name (&i.tm));
4198 return 0;
4199 }
4200 if (i.mem_operands == 0 || !(i.flags[i.operands - 1] & Operand_Mem))
4201 {
4202 as_bad (_("memory destination needed for instruction `%s'"
4203 " after `xrelease'"), insn_name (&i.tm));
4204 return 0;
4205 }
4206 return 1;
4207 }
4208 }
4209
4210 /* Encode aligned vector move as unaligned vector move. */
4211
4212 static void
4213 encode_with_unaligned_vector_move (void)
4214 {
4215 switch (i.tm.base_opcode)
4216 {
4217 case 0x28: /* Load instructions. */
4218 case 0x29: /* Store instructions. */
4219 /* movaps/movapd/vmovaps/vmovapd. */
4220 if (i.tm.opcode_space == SPACE_0F
4221 && i.tm.opcode_modifier.opcodeprefix <= PREFIX_0X66)
4222 i.tm.base_opcode = 0x10 | (i.tm.base_opcode & 1);
4223 break;
4224 case 0x6f: /* Load instructions. */
4225 case 0x7f: /* Store instructions. */
4226 /* movdqa/vmovdqa/vmovdqa64/vmovdqa32. */
4227 if (i.tm.opcode_space == SPACE_0F
4228 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0X66)
4229 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
4230 break;
4231 default:
4232 break;
4233 }
4234 }
4235
4236 /* Try the shortest encoding by shortening operand size. */
4237
4238 static void
4239 optimize_encoding (void)
4240 {
4241 unsigned int j;
4242
4243 if (i.tm.mnem_off == MN_lea)
4244 {
4245 /* Optimize: -O:
4246 lea symbol, %rN -> mov $symbol, %rN
4247 lea (%rM), %rN -> mov %rM, %rN
4248 lea (,%rM,1), %rN -> mov %rM, %rN
4249
4250 and in 32-bit mode for 16-bit addressing
4251
4252 lea (%rM), %rN -> movzx %rM, %rN
4253
4254 and in 64-bit mode zap 32-bit addressing in favor of using a
4255 32-bit (or less) destination.
4256 */
4257 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
4258 {
4259 if (!i.op[1].regs->reg_type.bitfield.word)
4260 i.tm.opcode_modifier.size = SIZE32;
4261 i.prefix[ADDR_PREFIX] = 0;
4262 }
4263
4264 if (!i.index_reg && !i.base_reg)
4265 {
4266 /* Handle:
4267 lea symbol, %rN -> mov $symbol, %rN
4268 */
4269 if (flag_code == CODE_64BIT)
4270 {
4271 /* Don't transform a relocation to a 16-bit one. */
4272 if (i.op[0].disps
4273 && i.op[0].disps->X_op != O_constant
4274 && i.op[1].regs->reg_type.bitfield.word)
4275 return;
4276
4277 if (!i.op[1].regs->reg_type.bitfield.qword
4278 || i.tm.opcode_modifier.size == SIZE32)
4279 {
4280 i.tm.base_opcode = 0xb8;
4281 i.tm.opcode_modifier.modrm = 0;
4282 if (!i.op[1].regs->reg_type.bitfield.word)
4283 i.types[0].bitfield.imm32 = 1;
4284 else
4285 {
4286 i.tm.opcode_modifier.size = SIZE16;
4287 i.types[0].bitfield.imm16 = 1;
4288 }
4289 }
4290 else
4291 {
4292 /* Subject to further optimization below. */
4293 i.tm.base_opcode = 0xc7;
4294 i.tm.extension_opcode = 0;
4295 i.types[0].bitfield.imm32s = 1;
4296 i.types[0].bitfield.baseindex = 0;
4297 }
4298 }
4299 /* Outside of 64-bit mode address and operand sizes have to match if
4300 a relocation is involved, as otherwise we wouldn't (currently) or
4301 even couldn't express the relocation correctly. */
4302 else if (i.op[0].disps
4303 && i.op[0].disps->X_op != O_constant
4304 && ((!i.prefix[ADDR_PREFIX])
4305 != (flag_code == CODE_32BIT
4306 ? i.op[1].regs->reg_type.bitfield.dword
4307 : i.op[1].regs->reg_type.bitfield.word)))
4308 return;
4309 /* In 16-bit mode converting LEA with 16-bit addressing and a 32-bit
4310 destination is going to grow encoding size. */
4311 else if (flag_code == CODE_16BIT
4312 && (optimize <= 1 || optimize_for_space)
4313 && !i.prefix[ADDR_PREFIX]
4314 && i.op[1].regs->reg_type.bitfield.dword)
4315 return;
4316 else
4317 {
4318 i.tm.base_opcode = 0xb8;
4319 i.tm.opcode_modifier.modrm = 0;
4320 if (i.op[1].regs->reg_type.bitfield.dword)
4321 i.types[0].bitfield.imm32 = 1;
4322 else
4323 i.types[0].bitfield.imm16 = 1;
4324
4325 if (i.op[0].disps
4326 && i.op[0].disps->X_op == O_constant
4327 && i.op[1].regs->reg_type.bitfield.dword
4328 /* NB: Add () to !i.prefix[ADDR_PREFIX] to silence
4329 GCC 5. */
4330 && (!i.prefix[ADDR_PREFIX]) != (flag_code == CODE_32BIT))
4331 i.op[0].disps->X_add_number &= 0xffff;
4332 }
4333
4334 i.tm.operand_types[0] = i.types[0];
4335 i.imm_operands = 1;
4336 if (!i.op[0].imms)
4337 {
4338 i.op[0].imms = &im_expressions[0];
4339 i.op[0].imms->X_op = O_absent;
4340 }
4341 }
4342 else if (i.op[0].disps
4343 && (i.op[0].disps->X_op != O_constant
4344 || i.op[0].disps->X_add_number))
4345 return;
4346 else
4347 {
4348 /* Handle:
4349 lea (%rM), %rN -> mov %rM, %rN
4350 lea (,%rM,1), %rN -> mov %rM, %rN
4351 lea (%rM), %rN -> movzx %rM, %rN
4352 */
4353 const reg_entry *addr_reg;
4354
4355 if (!i.index_reg && i.base_reg->reg_num != RegIP)
4356 addr_reg = i.base_reg;
4357 else if (!i.base_reg
4358 && i.index_reg->reg_num != RegIZ
4359 && !i.log2_scale_factor)
4360 addr_reg = i.index_reg;
4361 else
4362 return;
4363
4364 if (addr_reg->reg_type.bitfield.word
4365 && i.op[1].regs->reg_type.bitfield.dword)
4366 {
4367 if (flag_code != CODE_32BIT)
4368 return;
4369 i.tm.opcode_space = SPACE_0F;
4370 i.tm.base_opcode = 0xb7;
4371 }
4372 else
4373 i.tm.base_opcode = 0x8b;
4374
4375 if (addr_reg->reg_type.bitfield.dword
4376 && i.op[1].regs->reg_type.bitfield.qword)
4377 i.tm.opcode_modifier.size = SIZE32;
4378
4379 i.op[0].regs = addr_reg;
4380 i.reg_operands = 2;
4381 }
4382
4383 i.mem_operands = 0;
4384 i.disp_operands = 0;
4385 i.prefix[ADDR_PREFIX] = 0;
4386 i.prefix[SEG_PREFIX] = 0;
4387 i.seg[0] = NULL;
4388 }
4389
4390 if (optimize_for_space
4391 && i.tm.mnem_off == MN_test
4392 && i.reg_operands == 1
4393 && i.imm_operands == 1
4394 && !i.types[1].bitfield.byte
4395 && i.op[0].imms->X_op == O_constant
4396 && fits_in_imm7 (i.op[0].imms->X_add_number))
4397 {
4398 /* Optimize: -Os:
4399 test $imm7, %r64/%r32/%r16 -> test $imm7, %r8
4400 */
4401 unsigned int base_regnum = i.op[1].regs->reg_num;
4402 if (flag_code == CODE_64BIT || base_regnum < 4)
4403 {
4404 i.types[1].bitfield.byte = 1;
4405 /* Ignore the suffix. */
4406 i.suffix = 0;
4407 /* Convert to byte registers. */
4408 if (i.types[1].bitfield.word)
4409 j = 16;
4410 else if (i.types[1].bitfield.dword)
4411 j = 32;
4412 else
4413 j = 48;
4414 if (!(i.op[1].regs->reg_flags & RegRex) && base_regnum < 4)
4415 j += 8;
4416 i.op[1].regs -= j;
4417 }
4418 }
4419 else if (flag_code == CODE_64BIT
4420 && i.tm.opcode_space == SPACE_BASE
4421 && ((i.types[1].bitfield.qword
4422 && i.reg_operands == 1
4423 && i.imm_operands == 1
4424 && i.op[0].imms->X_op == O_constant
4425 && ((i.tm.base_opcode == 0xb8
4426 && i.tm.extension_opcode == None
4427 && fits_in_unsigned_long (i.op[0].imms->X_add_number))
4428 || (fits_in_imm31 (i.op[0].imms->X_add_number)
4429 && (i.tm.base_opcode == 0x24
4430 || (i.tm.base_opcode == 0x80
4431 && i.tm.extension_opcode == 0x4)
4432 || i.tm.mnem_off == MN_test
4433 || ((i.tm.base_opcode | 1) == 0xc7
4434 && i.tm.extension_opcode == 0x0)))
4435 || (fits_in_imm7 (i.op[0].imms->X_add_number)
4436 && i.tm.base_opcode == 0x83
4437 && i.tm.extension_opcode == 0x4)))
4438 || (i.types[0].bitfield.qword
4439 && ((i.reg_operands == 2
4440 && i.op[0].regs == i.op[1].regs
4441 && (i.tm.mnem_off == MN_xor
4442 || i.tm.mnem_off == MN_sub))
4443 || i.tm.mnem_off == MN_clr))))
4444 {
4445 /* Optimize: -O:
4446 andq $imm31, %r64 -> andl $imm31, %r32
4447 andq $imm7, %r64 -> andl $imm7, %r32
4448 testq $imm31, %r64 -> testl $imm31, %r32
4449 xorq %r64, %r64 -> xorl %r32, %r32
4450 subq %r64, %r64 -> subl %r32, %r32
4451 movq $imm31, %r64 -> movl $imm31, %r32
4452 movq $imm32, %r64 -> movl $imm32, %r32
4453 */
4454 i.tm.opcode_modifier.size = SIZE32;
4455 if (i.imm_operands)
4456 {
4457 i.types[0].bitfield.imm32 = 1;
4458 i.types[0].bitfield.imm32s = 0;
4459 i.types[0].bitfield.imm64 = 0;
4460 }
4461 else
4462 {
4463 i.types[0].bitfield.dword = 1;
4464 i.types[0].bitfield.qword = 0;
4465 }
4466 i.types[1].bitfield.dword = 1;
4467 i.types[1].bitfield.qword = 0;
4468 if (i.tm.mnem_off == MN_mov || i.tm.mnem_off == MN_lea)
4469 {
4470 /* Handle
4471 movq $imm31, %r64 -> movl $imm31, %r32
4472 movq $imm32, %r64 -> movl $imm32, %r32
4473 */
4474 i.tm.operand_types[0].bitfield.imm32 = 1;
4475 i.tm.operand_types[0].bitfield.imm32s = 0;
4476 i.tm.operand_types[0].bitfield.imm64 = 0;
4477 if ((i.tm.base_opcode | 1) == 0xc7)
4478 {
4479 /* Handle
4480 movq $imm31, %r64 -> movl $imm31, %r32
4481 */
4482 i.tm.base_opcode = 0xb8;
4483 i.tm.extension_opcode = None;
4484 i.tm.opcode_modifier.w = 0;
4485 i.tm.opcode_modifier.modrm = 0;
4486 }
4487 }
4488 }
4489 else if (optimize > 1
4490 && !optimize_for_space
4491 && i.reg_operands == 2
4492 && i.op[0].regs == i.op[1].regs
4493 && (i.tm.mnem_off == MN_and || i.tm.mnem_off == MN_or)
4494 && (flag_code != CODE_64BIT || !i.types[0].bitfield.dword))
4495 {
4496 /* Optimize: -O2:
4497 andb %rN, %rN -> testb %rN, %rN
4498 andw %rN, %rN -> testw %rN, %rN
4499 andq %rN, %rN -> testq %rN, %rN
4500 orb %rN, %rN -> testb %rN, %rN
4501 orw %rN, %rN -> testw %rN, %rN
4502 orq %rN, %rN -> testq %rN, %rN
4503
4504 and outside of 64-bit mode
4505
4506 andl %rN, %rN -> testl %rN, %rN
4507 orl %rN, %rN -> testl %rN, %rN
4508 */
4509 i.tm.base_opcode = 0x84 | (i.tm.base_opcode & 1);
4510 }
4511 else if (i.tm.base_opcode == 0xba
4512 && i.tm.opcode_space == SPACE_0F
4513 && i.reg_operands == 1
4514 && i.op[0].imms->X_op == O_constant
4515 && i.op[0].imms->X_add_number >= 0)
4516 {
4517 /* Optimize: -O:
4518 btw $n, %rN -> btl $n, %rN (outside of 16-bit mode, n < 16)
4519 btq $n, %rN -> btl $n, %rN (in 64-bit mode, n < 32, N < 8)
4520 btl $n, %rN -> btw $n, %rN (in 16-bit mode, n < 16)
4521
4522 With <BT> one of bts, btr, and bts also:
4523 <BT>w $n, %rN -> btl $n, %rN (in 32-bit mode, n < 16)
4524 <BT>l $n, %rN -> btw $n, %rN (in 16-bit mode, n < 16)
4525 */
4526 switch (flag_code)
4527 {
4528 case CODE_64BIT:
4529 if (i.tm.extension_opcode != 4)
4530 break;
4531 if (i.types[1].bitfield.qword
4532 && i.op[0].imms->X_add_number < 32
4533 && !(i.op[1].regs->reg_flags & RegRex))
4534 i.tm.opcode_modifier.size = SIZE32;
4535 /* Fall through. */
4536 case CODE_32BIT:
4537 if (i.types[1].bitfield.word
4538 && i.op[0].imms->X_add_number < 16)
4539 i.tm.opcode_modifier.size = SIZE32;
4540 break;
4541 case CODE_16BIT:
4542 if (i.op[0].imms->X_add_number < 16)
4543 i.tm.opcode_modifier.size = SIZE16;
4544 break;
4545 }
4546 }
4547 else if (i.reg_operands == 3
4548 && i.op[0].regs == i.op[1].regs
4549 && !i.types[2].bitfield.xmmword
4550 && (i.tm.opcode_modifier.vex
4551 || ((!i.mask.reg || i.mask.zeroing)
4552 && is_evex_encoding (&i.tm)
4553 && (i.vec_encoding != vex_encoding_evex
4554 || cpu_arch_isa_flags.bitfield.cpuavx512vl
4555 || is_cpu (&i.tm, CpuAVX512VL)
4556 || (i.tm.operand_types[2].bitfield.zmmword
4557 && i.types[2].bitfield.ymmword))))
4558 && i.tm.opcode_space == SPACE_0F
4559 && ((i.tm.base_opcode | 2) == 0x57
4560 || i.tm.base_opcode == 0xdf
4561 || i.tm.base_opcode == 0xef
4562 || (i.tm.base_opcode | 3) == 0xfb
4563 || i.tm.base_opcode == 0x42
4564 || i.tm.base_opcode == 0x47))
4565 {
4566 /* Optimize: -O1:
4567 VOP, one of vandnps, vandnpd, vxorps, vxorpd, vpsubb, vpsubd,
4568 vpsubq and vpsubw:
4569 EVEX VOP %zmmM, %zmmM, %zmmN
4570 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
4571 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4572 EVEX VOP %ymmM, %ymmM, %ymmN
4573 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
4574 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4575 VEX VOP %ymmM, %ymmM, %ymmN
4576 -> VEX VOP %xmmM, %xmmM, %xmmN
4577 VOP, one of vpandn and vpxor:
4578 VEX VOP %ymmM, %ymmM, %ymmN
4579 -> VEX VOP %xmmM, %xmmM, %xmmN
4580 VOP, one of vpandnd and vpandnq:
4581 EVEX VOP %zmmM, %zmmM, %zmmN
4582 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
4583 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4584 EVEX VOP %ymmM, %ymmM, %ymmN
4585 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
4586 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4587 VOP, one of vpxord and vpxorq:
4588 EVEX VOP %zmmM, %zmmM, %zmmN
4589 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
4590 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4591 EVEX VOP %ymmM, %ymmM, %ymmN
4592 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
4593 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4594 VOP, one of kxord and kxorq:
4595 VEX VOP %kM, %kM, %kN
4596 -> VEX kxorw %kM, %kM, %kN
4597 VOP, one of kandnd and kandnq:
4598 VEX VOP %kM, %kM, %kN
4599 -> VEX kandnw %kM, %kM, %kN
4600 */
4601 if (is_evex_encoding (&i.tm))
4602 {
4603 if (i.vec_encoding != vex_encoding_evex)
4604 {
4605 i.tm.opcode_modifier.vex = VEX128;
4606 i.tm.opcode_modifier.vexw = VEXW0;
4607 i.tm.opcode_modifier.evex = 0;
4608 i.vec_encoding = vex_encoding_vex;
4609 i.mask.reg = NULL;
4610 }
4611 else if (optimize > 1)
4612 i.tm.opcode_modifier.evex = EVEX128;
4613 else
4614 return;
4615 }
4616 else if (i.tm.operand_types[0].bitfield.class == RegMask)
4617 {
4618 i.tm.opcode_modifier.opcodeprefix = PREFIX_NONE;
4619 i.tm.opcode_modifier.vexw = VEXW0;
4620 }
4621 else
4622 i.tm.opcode_modifier.vex = VEX128;
4623
4624 if (i.tm.opcode_modifier.vex)
4625 for (j = 0; j < 3; j++)
4626 {
4627 i.types[j].bitfield.xmmword = 1;
4628 i.types[j].bitfield.ymmword = 0;
4629 }
4630 }
4631 else if (i.vec_encoding != vex_encoding_evex
4632 && !i.types[0].bitfield.zmmword
4633 && !i.types[1].bitfield.zmmword
4634 && !i.mask.reg
4635 && !i.broadcast.type
4636 && !i.broadcast.bytes
4637 && is_evex_encoding (&i.tm)
4638 && ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4639 || (i.tm.base_opcode & ~4) == 0xdb
4640 || (i.tm.base_opcode & ~4) == 0xeb)
4641 && i.tm.extension_opcode == None)
4642 {
4643 /* Optimize: -O1:
4644 VOP, one of vmovdqa32, vmovdqa64, vmovdqu8, vmovdqu16,
4645 vmovdqu32 and vmovdqu64:
4646 EVEX VOP %xmmM, %xmmN
4647 -> VEX vmovdqa|vmovdqu %xmmM, %xmmN (M and N < 16)
4648 EVEX VOP %ymmM, %ymmN
4649 -> VEX vmovdqa|vmovdqu %ymmM, %ymmN (M and N < 16)
4650 EVEX VOP %xmmM, mem
4651 -> VEX vmovdqa|vmovdqu %xmmM, mem (M < 16)
4652 EVEX VOP %ymmM, mem
4653 -> VEX vmovdqa|vmovdqu %ymmM, mem (M < 16)
4654 EVEX VOP mem, %xmmN
4655 -> VEX mvmovdqa|vmovdquem, %xmmN (N < 16)
4656 EVEX VOP mem, %ymmN
4657 -> VEX vmovdqa|vmovdqu mem, %ymmN (N < 16)
4658 VOP, one of vpand, vpandn, vpor, vpxor:
4659 EVEX VOP{d,q} %xmmL, %xmmM, %xmmN
4660 -> VEX VOP %xmmL, %xmmM, %xmmN (L, M, and N < 16)
4661 EVEX VOP{d,q} %ymmL, %ymmM, %ymmN
4662 -> VEX VOP %ymmL, %ymmM, %ymmN (L, M, and N < 16)
4663 EVEX VOP{d,q} mem, %xmmM, %xmmN
4664 -> VEX VOP mem, %xmmM, %xmmN (M and N < 16)
4665 EVEX VOP{d,q} mem, %ymmM, %ymmN
4666 -> VEX VOP mem, %ymmM, %ymmN (M and N < 16)
4667 */
4668 for (j = 0; j < i.operands; j++)
4669 if (operand_type_check (i.types[j], disp)
4670 && i.op[j].disps->X_op == O_constant)
4671 {
4672 /* Since the VEX prefix has 2 or 3 bytes, the EVEX prefix
4673 has 4 bytes, EVEX Disp8 has 1 byte and VEX Disp32 has 4
4674 bytes, we choose EVEX Disp8 over VEX Disp32. */
4675 int evex_disp8, vex_disp8;
4676 unsigned int memshift = i.memshift;
4677 offsetT n = i.op[j].disps->X_add_number;
4678
4679 evex_disp8 = fits_in_disp8 (n);
4680 i.memshift = 0;
4681 vex_disp8 = fits_in_disp8 (n);
4682 if (evex_disp8 != vex_disp8)
4683 {
4684 i.memshift = memshift;
4685 return;
4686 }
4687
4688 i.types[j].bitfield.disp8 = vex_disp8;
4689 break;
4690 }
4691 if ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4692 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2)
4693 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
4694 i.tm.opcode_modifier.vex
4695 = i.types[0].bitfield.ymmword ? VEX256 : VEX128;
4696 i.tm.opcode_modifier.vexw = VEXW0;
4697 /* VPAND, VPOR, and VPXOR are commutative. */
4698 if (i.reg_operands == 3 && i.tm.base_opcode != 0xdf)
4699 i.tm.opcode_modifier.commutative = 1;
4700 i.tm.opcode_modifier.evex = 0;
4701 i.tm.opcode_modifier.masking = 0;
4702 i.tm.opcode_modifier.broadcast = 0;
4703 i.tm.opcode_modifier.disp8memshift = 0;
4704 i.memshift = 0;
4705 if (j < i.operands)
4706 i.types[j].bitfield.disp8
4707 = fits_in_disp8 (i.op[j].disps->X_add_number);
4708 }
4709 else if (optimize_for_space
4710 && i.tm.base_opcode == 0x29
4711 && i.tm.opcode_space == SPACE_0F38
4712 && i.operands == i.reg_operands
4713 && i.op[0].regs == i.op[1].regs
4714 && (!i.tm.opcode_modifier.vex
4715 || !(i.op[0].regs->reg_flags & RegRex))
4716 && !is_evex_encoding (&i.tm))
4717 {
4718 /* Optimize: -Os:
4719 pcmpeqq %xmmN, %xmmN -> pcmpeqd %xmmN, %xmmN
4720 vpcmpeqq %xmmN, %xmmN, %xmmM -> vpcmpeqd %xmmN, %xmmN, %xmmM (N < 8)
4721 vpcmpeqq %ymmN, %ymmN, %ymmM -> vpcmpeqd %ymmN, %ymmN, %ymmM (N < 8)
4722 */
4723 i.tm.opcode_space = SPACE_0F;
4724 i.tm.base_opcode = 0x76;
4725 }
4726 else if (((i.tm.base_opcode >= 0x64
4727 && i.tm.base_opcode <= 0x66
4728 && i.tm.opcode_space == SPACE_0F)
4729 || (i.tm.base_opcode == 0x37
4730 && i.tm.opcode_space == SPACE_0F38))
4731 && i.operands == i.reg_operands
4732 && i.op[0].regs == i.op[1].regs
4733 && !is_evex_encoding (&i.tm))
4734 {
4735 /* Optimize: -O:
4736 pcmpgt[bwd] %mmN, %mmN -> pxor %mmN, %mmN
4737 pcmpgt[bwdq] %xmmN, %xmmN -> pxor %xmmN, %xmmN
4738 vpcmpgt[bwdq] %xmmN, %xmmN, %xmmM -> vpxor %xmmN, %xmmN, %xmmM (N < 8)
4739 vpcmpgt[bwdq] %xmmN, %xmmN, %xmmM -> vpxor %xmm0, %xmm0, %xmmM (N > 7)
4740 vpcmpgt[bwdq] %ymmN, %ymmN, %ymmM -> vpxor %ymmN, %ymmN, %ymmM (N < 8)
4741 vpcmpgt[bwdq] %ymmN, %ymmN, %ymmM -> vpxor %ymm0, %ymm0, %ymmM (N > 7)
4742 */
4743 i.tm.opcode_space = SPACE_0F;
4744 i.tm.base_opcode = 0xef;
4745 if (i.tm.opcode_modifier.vex && (i.op[0].regs->reg_flags & RegRex))
4746 {
4747 if (i.operands == 2)
4748 {
4749 gas_assert (i.tm.opcode_modifier.sse2avx);
4750
4751 i.operands = 3;
4752 i.reg_operands = 3;
4753 i.tm.operands = 3;
4754
4755 i.op[2].regs = i.op[0].regs;
4756 i.types[2] = i.types[0];
4757 i.flags[2] = i.flags[0];
4758 i.tm.operand_types[2] = i.tm.operand_types[0];
4759
4760 i.tm.opcode_modifier.sse2avx = 0;
4761 }
4762 i.op[0].regs -= i.op[0].regs->reg_num + 8;
4763 i.op[1].regs = i.op[0].regs;
4764 }
4765 }
4766 else if (optimize_for_space
4767 && i.tm.base_opcode == 0x59
4768 && i.tm.opcode_space == SPACE_0F38
4769 && i.operands == i.reg_operands
4770 && i.tm.opcode_modifier.vex
4771 && !(i.op[0].regs->reg_flags & RegRex)
4772 && i.op[0].regs->reg_type.bitfield.xmmword
4773 && i.vec_encoding != vex_encoding_vex3)
4774 {
4775 /* Optimize: -Os:
4776 vpbroadcastq %xmmN, %xmmM -> vpunpcklqdq %xmmN, %xmmN, %xmmM (N < 8)
4777 */
4778 i.tm.opcode_space = SPACE_0F;
4779 i.tm.base_opcode = 0x6c;
4780 i.tm.opcode_modifier.vexvvvv = 1;
4781
4782 ++i.operands;
4783 ++i.reg_operands;
4784 ++i.tm.operands;
4785
4786 i.op[2].regs = i.op[0].regs;
4787 i.types[2] = i.types[0];
4788 i.flags[2] = i.flags[0];
4789 i.tm.operand_types[2] = i.tm.operand_types[0];
4790
4791 swap_2_operands (1, 2);
4792 }
4793 }
4794
4795 /* Return non-zero for load instruction. */
4796
4797 static int
4798 load_insn_p (void)
4799 {
4800 unsigned int dest;
4801 int any_vex_p = is_any_vex_encoding (&i.tm);
4802 unsigned int base_opcode = i.tm.base_opcode | 1;
4803
4804 if (!any_vex_p)
4805 {
4806 /* Anysize insns: lea, invlpg, clflush, prefetch*, bndmk, bndcl, bndcu,
4807 bndcn, bndstx, bndldx, clflushopt, clwb, cldemote. */
4808 if (i.tm.opcode_modifier.operandconstraint == ANY_SIZE)
4809 return 0;
4810
4811 /* pop. */
4812 if (i.tm.mnem_off == MN_pop)
4813 return 1;
4814 }
4815
4816 if (i.tm.opcode_space == SPACE_BASE)
4817 {
4818 /* popf, popa. */
4819 if (i.tm.base_opcode == 0x9d
4820 || i.tm.base_opcode == 0x61)
4821 return 1;
4822
4823 /* movs, cmps, lods, scas. */
4824 if ((i.tm.base_opcode | 0xb) == 0xaf)
4825 return 1;
4826
4827 /* outs, xlatb. */
4828 if (base_opcode == 0x6f
4829 || i.tm.base_opcode == 0xd7)
4830 return 1;
4831 /* NB: For AMD-specific insns with implicit memory operands,
4832 they're intentionally not covered. */
4833 }
4834
4835 /* No memory operand. */
4836 if (!i.mem_operands)
4837 return 0;
4838
4839 if (any_vex_p)
4840 {
4841 if (i.tm.mnem_off == MN_vldmxcsr)
4842 return 1;
4843 }
4844 else if (i.tm.opcode_space == SPACE_BASE)
4845 {
4846 /* test, not, neg, mul, imul, div, idiv. */
4847 if (base_opcode == 0xf7 && i.tm.extension_opcode != 1)
4848 return 1;
4849
4850 /* inc, dec. */
4851 if (base_opcode == 0xff && i.tm.extension_opcode <= 1)
4852 return 1;
4853
4854 /* add, or, adc, sbb, and, sub, xor, cmp. */
4855 if (i.tm.base_opcode >= 0x80 && i.tm.base_opcode <= 0x83)
4856 return 1;
4857
4858 /* rol, ror, rcl, rcr, shl/sal, shr, sar. */
4859 if ((base_opcode == 0xc1 || (base_opcode | 2) == 0xd3)
4860 && i.tm.extension_opcode != 6)
4861 return 1;
4862
4863 /* Check for x87 instructions. */
4864 if ((base_opcode | 6) == 0xdf)
4865 {
4866 /* Skip fst, fstp, fstenv, fstcw. */
4867 if (i.tm.base_opcode == 0xd9
4868 && (i.tm.extension_opcode == 2
4869 || i.tm.extension_opcode == 3
4870 || i.tm.extension_opcode == 6
4871 || i.tm.extension_opcode == 7))
4872 return 0;
4873
4874 /* Skip fisttp, fist, fistp, fstp. */
4875 if (i.tm.base_opcode == 0xdb
4876 && (i.tm.extension_opcode == 1
4877 || i.tm.extension_opcode == 2
4878 || i.tm.extension_opcode == 3
4879 || i.tm.extension_opcode == 7))
4880 return 0;
4881
4882 /* Skip fisttp, fst, fstp, fsave, fstsw. */
4883 if (i.tm.base_opcode == 0xdd
4884 && (i.tm.extension_opcode == 1
4885 || i.tm.extension_opcode == 2
4886 || i.tm.extension_opcode == 3
4887 || i.tm.extension_opcode == 6
4888 || i.tm.extension_opcode == 7))
4889 return 0;
4890
4891 /* Skip fisttp, fist, fistp, fbstp, fistp. */
4892 if (i.tm.base_opcode == 0xdf
4893 && (i.tm.extension_opcode == 1
4894 || i.tm.extension_opcode == 2
4895 || i.tm.extension_opcode == 3
4896 || i.tm.extension_opcode == 6
4897 || i.tm.extension_opcode == 7))
4898 return 0;
4899
4900 return 1;
4901 }
4902 }
4903 else if (i.tm.opcode_space == SPACE_0F)
4904 {
4905 /* bt, bts, btr, btc. */
4906 if (i.tm.base_opcode == 0xba
4907 && (i.tm.extension_opcode | 3) == 7)
4908 return 1;
4909
4910 /* cmpxchg8b, cmpxchg16b, xrstors, vmptrld. */
4911 if (i.tm.base_opcode == 0xc7
4912 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
4913 && (i.tm.extension_opcode == 1 || i.tm.extension_opcode == 3
4914 || i.tm.extension_opcode == 6))
4915 return 1;
4916
4917 /* fxrstor, ldmxcsr, xrstor. */
4918 if (i.tm.base_opcode == 0xae
4919 && (i.tm.extension_opcode == 1
4920 || i.tm.extension_opcode == 2
4921 || i.tm.extension_opcode == 5))
4922 return 1;
4923
4924 /* lgdt, lidt, lmsw. */
4925 if (i.tm.base_opcode == 0x01
4926 && (i.tm.extension_opcode == 2
4927 || i.tm.extension_opcode == 3
4928 || i.tm.extension_opcode == 6))
4929 return 1;
4930 }
4931
4932 dest = i.operands - 1;
4933
4934 /* Check fake imm8 operand and 3 source operands. */
4935 if ((i.tm.opcode_modifier.immext
4936 || i.reg_operands + i.mem_operands == 4)
4937 && i.types[dest].bitfield.imm8)
4938 dest--;
4939
4940 /* add, or, adc, sbb, and, sub, xor, cmp, test, xchg. */
4941 if (i.tm.opcode_space == SPACE_BASE
4942 && ((base_opcode | 0x38) == 0x39
4943 || (base_opcode | 2) == 0x87))
4944 return 1;
4945
4946 if (i.tm.mnem_off == MN_xadd)
4947 return 1;
4948
4949 /* Check for load instruction. */
4950 return (i.types[dest].bitfield.class != ClassNone
4951 || i.types[dest].bitfield.instance == Accum);
4952 }
4953
4954 /* Output lfence, 0xfaee8, after instruction. */
4955
4956 static void
4957 insert_lfence_after (void)
4958 {
4959 if (lfence_after_load && load_insn_p ())
4960 {
4961 /* There are also two REP string instructions that require
4962 special treatment. Specifically, the compare string (CMPS)
4963 and scan string (SCAS) instructions set EFLAGS in a manner
4964 that depends on the data being compared/scanned. When used
4965 with a REP prefix, the number of iterations may therefore
4966 vary depending on this data. If the data is a program secret
4967 chosen by the adversary using an LVI method,
4968 then this data-dependent behavior may leak some aspect
4969 of the secret. */
4970 if (((i.tm.base_opcode | 0x9) == 0xaf)
4971 && i.prefix[REP_PREFIX])
4972 {
4973 as_warn (_("`%s` changes flags which would affect control flow behavior"),
4974 insn_name (&i.tm));
4975 }
4976 char *p = frag_more (3);
4977 *p++ = 0xf;
4978 *p++ = 0xae;
4979 *p = 0xe8;
4980 }
4981 }
4982
4983 /* Output lfence, 0xfaee8, before instruction. */
4984
4985 static void
4986 insert_lfence_before (void)
4987 {
4988 char *p;
4989
4990 if (i.tm.opcode_space != SPACE_BASE)
4991 return;
4992
4993 if (i.tm.base_opcode == 0xff
4994 && (i.tm.extension_opcode == 2 || i.tm.extension_opcode == 4))
4995 {
4996 /* Insert lfence before indirect branch if needed. */
4997
4998 if (lfence_before_indirect_branch == lfence_branch_none)
4999 return;
5000
5001 if (i.operands != 1)
5002 abort ();
5003
5004 if (i.reg_operands == 1)
5005 {
5006 /* Indirect branch via register. Don't insert lfence with
5007 -mlfence-after-load=yes. */
5008 if (lfence_after_load
5009 || lfence_before_indirect_branch == lfence_branch_memory)
5010 return;
5011 }
5012 else if (i.mem_operands == 1
5013 && lfence_before_indirect_branch != lfence_branch_register)
5014 {
5015 as_warn (_("indirect `%s` with memory operand should be avoided"),
5016 insn_name (&i.tm));
5017 return;
5018 }
5019 else
5020 return;
5021
5022 if (last_insn.kind != last_insn_other
5023 && last_insn.seg == now_seg)
5024 {
5025 as_warn_where (last_insn.file, last_insn.line,
5026 _("`%s` skips -mlfence-before-indirect-branch on `%s`"),
5027 last_insn.name, insn_name (&i.tm));
5028 return;
5029 }
5030
5031 p = frag_more (3);
5032 *p++ = 0xf;
5033 *p++ = 0xae;
5034 *p = 0xe8;
5035 return;
5036 }
5037
5038 /* Output or/not/shl and lfence before near ret. */
5039 if (lfence_before_ret != lfence_before_ret_none
5040 && (i.tm.base_opcode | 1) == 0xc3)
5041 {
5042 if (last_insn.kind != last_insn_other
5043 && last_insn.seg == now_seg)
5044 {
5045 as_warn_where (last_insn.file, last_insn.line,
5046 _("`%s` skips -mlfence-before-ret on `%s`"),
5047 last_insn.name, insn_name (&i.tm));
5048 return;
5049 }
5050
5051 /* Near ret ingore operand size override under CPU64. */
5052 char prefix = flag_code == CODE_64BIT
5053 ? 0x48
5054 : i.prefix[DATA_PREFIX] ? 0x66 : 0x0;
5055
5056 if (lfence_before_ret == lfence_before_ret_not)
5057 {
5058 /* not: 0xf71424, may add prefix
5059 for operand size override or 64-bit code. */
5060 p = frag_more ((prefix ? 2 : 0) + 6 + 3);
5061 if (prefix)
5062 *p++ = prefix;
5063 *p++ = 0xf7;
5064 *p++ = 0x14;
5065 *p++ = 0x24;
5066 if (prefix)
5067 *p++ = prefix;
5068 *p++ = 0xf7;
5069 *p++ = 0x14;
5070 *p++ = 0x24;
5071 }
5072 else
5073 {
5074 p = frag_more ((prefix ? 1 : 0) + 4 + 3);
5075 if (prefix)
5076 *p++ = prefix;
5077 if (lfence_before_ret == lfence_before_ret_or)
5078 {
5079 /* or: 0x830c2400, may add prefix
5080 for operand size override or 64-bit code. */
5081 *p++ = 0x83;
5082 *p++ = 0x0c;
5083 }
5084 else
5085 {
5086 /* shl: 0xc1242400, may add prefix
5087 for operand size override or 64-bit code. */
5088 *p++ = 0xc1;
5089 *p++ = 0x24;
5090 }
5091
5092 *p++ = 0x24;
5093 *p++ = 0x0;
5094 }
5095
5096 *p++ = 0xf;
5097 *p++ = 0xae;
5098 *p = 0xe8;
5099 }
5100 }
5101
5102 /* Shared helper for md_assemble() and s_insn(). */
5103 static void init_globals (void)
5104 {
5105 unsigned int j;
5106
5107 memset (&i, '\0', sizeof (i));
5108 i.rounding.type = rc_none;
5109 for (j = 0; j < MAX_OPERANDS; j++)
5110 i.reloc[j] = NO_RELOC;
5111 memset (disp_expressions, '\0', sizeof (disp_expressions));
5112 memset (im_expressions, '\0', sizeof (im_expressions));
5113 save_stack_p = save_stack;
5114 }
5115
5116 /* Helper for md_assemble() to decide whether to prepare for a possible 2nd
5117 parsing pass. Instead of introducing a rarely use new insn attribute this
5118 utilizes a common pattern between affected templates. It is deemed
5119 acceptable that this will lead to unnecessary pass 2 preparations in a
5120 limited set of cases. */
5121 static INLINE bool may_need_pass2 (const insn_template *t)
5122 {
5123 return t->opcode_modifier.sse2avx
5124 /* Note that all SSE2AVX templates have at least one operand. */
5125 ? t->operand_types[t->operands - 1].bitfield.class == RegSIMD
5126 : (t->opcode_space == SPACE_0F
5127 && (t->base_opcode | 1) == 0xbf)
5128 || (t->opcode_space == SPACE_BASE
5129 && t->base_opcode == 0x63);
5130 }
5131
5132 /* This is the guts of the machine-dependent assembler. LINE points to a
5133 machine dependent instruction. This function is supposed to emit
5134 the frags/bytes it assembles to. */
5135
5136 void
5137 md_assemble (char *line)
5138 {
5139 unsigned int j;
5140 char mnemonic[MAX_MNEM_SIZE], mnem_suffix = 0, *copy = NULL;
5141 const char *end, *pass1_mnem = NULL;
5142 enum i386_error pass1_err = 0;
5143 const insn_template *t;
5144
5145 /* Initialize globals. */
5146 current_templates = NULL;
5147 retry:
5148 init_globals ();
5149
5150 /* First parse an instruction mnemonic & call i386_operand for the operands.
5151 We assume that the scrubber has arranged it so that line[0] is the valid
5152 start of a (possibly prefixed) mnemonic. */
5153
5154 end = parse_insn (line, mnemonic, false);
5155 if (end == NULL)
5156 {
5157 if (pass1_mnem != NULL)
5158 goto match_error;
5159 if (i.error != no_error)
5160 {
5161 gas_assert (current_templates != NULL);
5162 if (may_need_pass2 (current_templates->start) && !i.suffix)
5163 goto no_match;
5164 /* No point in trying a 2nd pass - it'll only find the same suffix
5165 again. */
5166 mnem_suffix = i.suffix;
5167 goto match_error;
5168 }
5169 return;
5170 }
5171 t = current_templates->start;
5172 if (may_need_pass2 (t))
5173 {
5174 /* Make a copy of the full line in case we need to retry. */
5175 copy = xstrdup (line);
5176 }
5177 line += end - line;
5178 mnem_suffix = i.suffix;
5179
5180 line = parse_operands (line, mnemonic);
5181 this_operand = -1;
5182 if (line == NULL)
5183 {
5184 free (copy);
5185 return;
5186 }
5187
5188 /* Now we've parsed the mnemonic into a set of templates, and have the
5189 operands at hand. */
5190
5191 /* All Intel opcodes have reversed operands except for "bound", "enter",
5192 "invlpg*", "monitor*", "mwait*", "tpause", "umwait", "pvalidate",
5193 "rmpadjust", "rmpupdate", and "rmpquery". We also don't reverse
5194 intersegment "jmp" and "call" instructions with 2 immediate operands so
5195 that the immediate segment precedes the offset consistently in Intel and
5196 AT&T modes. */
5197 if (intel_syntax
5198 && i.operands > 1
5199 && (t->mnem_off != MN_bound)
5200 && !startswith (mnemonic, "invlpg")
5201 && !startswith (mnemonic, "monitor")
5202 && !startswith (mnemonic, "mwait")
5203 && (t->mnem_off != MN_pvalidate)
5204 && !startswith (mnemonic, "rmp")
5205 && (t->mnem_off != MN_tpause)
5206 && (t->mnem_off != MN_umwait)
5207 && !(i.operands == 2
5208 && operand_type_check (i.types[0], imm)
5209 && operand_type_check (i.types[1], imm)))
5210 swap_operands ();
5211
5212 /* The order of the immediates should be reversed
5213 for 2 immediates extrq and insertq instructions */
5214 if (i.imm_operands == 2
5215 && (t->mnem_off == MN_extrq || t->mnem_off == MN_insertq))
5216 swap_2_operands (0, 1);
5217
5218 if (i.imm_operands)
5219 {
5220 /* For USER_MSR instructions, imm32 stands for the name of an model specific
5221 register (MSR). That's an unsigned quantity, whereas all other insns with
5222 32-bit immediate and 64-bit operand size use sign-extended
5223 immediates (imm32s). Therefore these insns are special-cased, bypassing
5224 the normal handling of immediates here. */
5225 if (is_cpu(current_templates->start, CpuUSER_MSR))
5226 {
5227 for (j = 0; j < i.operands; j++)
5228 {
5229 if (operand_type_check(i.types[j], imm))
5230 i.types[j] = smallest_imm_type (i.op[j].imms->X_add_number);
5231 }
5232 }
5233 else
5234 optimize_imm ();
5235 }
5236
5237 if (i.disp_operands && !optimize_disp (t))
5238 return;
5239
5240 /* Next, we find a template that matches the given insn,
5241 making sure the overlap of the given operands types is consistent
5242 with the template operand types. */
5243
5244 if (!(t = match_template (mnem_suffix)))
5245 {
5246 const char *err_msg;
5247
5248 if (copy && !mnem_suffix)
5249 {
5250 line = copy;
5251 copy = NULL;
5252 no_match:
5253 pass1_err = i.error;
5254 pass1_mnem = insn_name (current_templates->start);
5255 goto retry;
5256 }
5257
5258 /* If a non-/only-64bit template (group) was found in pass 1, and if
5259 _some_ template (group) was found in pass 2, squash pass 1's
5260 error. */
5261 if (pass1_err == unsupported_64bit)
5262 pass1_mnem = NULL;
5263
5264 match_error:
5265 free (copy);
5266
5267 switch (pass1_mnem ? pass1_err : i.error)
5268 {
5269 default:
5270 abort ();
5271 case operand_size_mismatch:
5272 err_msg = _("operand size mismatch");
5273 break;
5274 case operand_type_mismatch:
5275 err_msg = _("operand type mismatch");
5276 break;
5277 case register_type_mismatch:
5278 err_msg = _("register type mismatch");
5279 break;
5280 case number_of_operands_mismatch:
5281 err_msg = _("number of operands mismatch");
5282 break;
5283 case invalid_instruction_suffix:
5284 err_msg = _("invalid instruction suffix");
5285 break;
5286 case bad_imm4:
5287 err_msg = _("constant doesn't fit in 4 bits");
5288 break;
5289 case unsupported_with_intel_mnemonic:
5290 err_msg = _("unsupported with Intel mnemonic");
5291 break;
5292 case unsupported_syntax:
5293 err_msg = _("unsupported syntax");
5294 break;
5295 case unsupported:
5296 as_bad (_("unsupported instruction `%s'"),
5297 pass1_mnem ? pass1_mnem : insn_name (current_templates->start));
5298 return;
5299 case unsupported_on_arch:
5300 as_bad (_("`%s' is not supported on `%s%s'"),
5301 pass1_mnem ? pass1_mnem : insn_name (current_templates->start),
5302 cpu_arch_name ? cpu_arch_name : default_arch,
5303 cpu_sub_arch_name ? cpu_sub_arch_name : "");
5304 return;
5305 case unsupported_64bit:
5306 if (ISLOWER (mnem_suffix))
5307 {
5308 if (flag_code == CODE_64BIT)
5309 as_bad (_("`%s%c' is not supported in 64-bit mode"),
5310 pass1_mnem ? pass1_mnem : insn_name (current_templates->start),
5311 mnem_suffix);
5312 else
5313 as_bad (_("`%s%c' is only supported in 64-bit mode"),
5314 pass1_mnem ? pass1_mnem : insn_name (current_templates->start),
5315 mnem_suffix);
5316 }
5317 else
5318 {
5319 if (flag_code == CODE_64BIT)
5320 as_bad (_("`%s' is not supported in 64-bit mode"),
5321 pass1_mnem ? pass1_mnem : insn_name (current_templates->start));
5322 else
5323 as_bad (_("`%s' is only supported in 64-bit mode"),
5324 pass1_mnem ? pass1_mnem : insn_name (current_templates->start));
5325 }
5326 return;
5327 case invalid_sib_address:
5328 err_msg = _("invalid SIB address");
5329 break;
5330 case invalid_vsib_address:
5331 err_msg = _("invalid VSIB address");
5332 break;
5333 case invalid_vector_register_set:
5334 err_msg = _("mask, index, and destination registers must be distinct");
5335 break;
5336 case invalid_tmm_register_set:
5337 err_msg = _("all tmm registers must be distinct");
5338 break;
5339 case invalid_dest_and_src_register_set:
5340 err_msg = _("destination and source registers must be distinct");
5341 break;
5342 case unsupported_vector_index_register:
5343 err_msg = _("unsupported vector index register");
5344 break;
5345 case unsupported_broadcast:
5346 err_msg = _("unsupported broadcast");
5347 break;
5348 case broadcast_needed:
5349 err_msg = _("broadcast is needed for operand of such type");
5350 break;
5351 case unsupported_masking:
5352 err_msg = _("unsupported masking");
5353 break;
5354 case mask_not_on_destination:
5355 err_msg = _("mask not on destination operand");
5356 break;
5357 case no_default_mask:
5358 err_msg = _("default mask isn't allowed");
5359 break;
5360 case unsupported_rc_sae:
5361 err_msg = _("unsupported static rounding/sae");
5362 break;
5363 case invalid_register_operand:
5364 err_msg = _("invalid register operand");
5365 break;
5366 case internal_error:
5367 err_msg = _("internal error");
5368 break;
5369 }
5370 as_bad (_("%s for `%s'"), err_msg,
5371 pass1_mnem ? pass1_mnem : insn_name (current_templates->start));
5372 return;
5373 }
5374
5375 free (copy);
5376
5377 if (sse_check != check_none
5378 /* The opcode space check isn't strictly needed; it's there only to
5379 bypass the logic below when easily possible. */
5380 && t->opcode_space >= SPACE_0F
5381 && t->opcode_space <= SPACE_0F3A
5382 && !is_cpu (&i.tm, CpuSSE4a)
5383 && !is_any_vex_encoding (t))
5384 {
5385 bool simd = false;
5386
5387 for (j = 0; j < t->operands; ++j)
5388 {
5389 if (t->operand_types[j].bitfield.class == RegMMX)
5390 break;
5391 if (t->operand_types[j].bitfield.class == RegSIMD)
5392 simd = true;
5393 }
5394
5395 if (j >= t->operands && simd)
5396 (sse_check == check_warning
5397 ? as_warn
5398 : as_bad) (_("SSE instruction `%s' is used"), insn_name (&i.tm));
5399 }
5400
5401 if (i.tm.opcode_modifier.fwait)
5402 if (!add_prefix (FWAIT_OPCODE))
5403 return;
5404
5405 /* Check if REP prefix is OK. */
5406 if (i.rep_prefix && i.tm.opcode_modifier.prefixok != PrefixRep)
5407 {
5408 as_bad (_("invalid instruction `%s' after `%s'"),
5409 insn_name (&i.tm), i.rep_prefix);
5410 return;
5411 }
5412
5413 /* Check for lock without a lockable instruction. Destination operand
5414 must be memory unless it is xchg (0x86). */
5415 if (i.prefix[LOCK_PREFIX])
5416 {
5417 if (i.tm.opcode_modifier.prefixok < PrefixLock
5418 || i.mem_operands == 0
5419 || (i.tm.base_opcode != 0x86
5420 && !(i.flags[i.operands - 1] & Operand_Mem)))
5421 {
5422 as_bad (_("expecting lockable instruction after `lock'"));
5423 return;
5424 }
5425
5426 /* Zap the redundant prefix from XCHG when optimizing. */
5427 if (i.tm.base_opcode == 0x86 && optimize && !i.no_optimize)
5428 i.prefix[LOCK_PREFIX] = 0;
5429 }
5430
5431 if (is_any_vex_encoding (&i.tm)
5432 || i.tm.operand_types[i.imm_operands].bitfield.class >= RegMMX
5433 || i.tm.operand_types[i.imm_operands + 1].bitfield.class >= RegMMX)
5434 {
5435 /* Check for data size prefix on VEX/XOP/EVEX encoded and SIMD insns. */
5436 if (i.prefix[DATA_PREFIX])
5437 {
5438 as_bad (_("data size prefix invalid with `%s'"), insn_name (&i.tm));
5439 return;
5440 }
5441
5442 /* Don't allow e.g. KMOV in TLS code sequences. */
5443 for (j = i.imm_operands; j < i.operands; ++j)
5444 switch (i.reloc[j])
5445 {
5446 case BFD_RELOC_386_TLS_GOTIE:
5447 case BFD_RELOC_386_TLS_LE_32:
5448 case BFD_RELOC_X86_64_GOTTPOFF:
5449 case BFD_RELOC_X86_64_TLSLD:
5450 as_bad (_("TLS relocation cannot be used with `%s'"), insn_name (&i.tm));
5451 return;
5452 default:
5453 break;
5454 }
5455 }
5456
5457 /* Check if HLE prefix is OK. */
5458 if (i.hle_prefix && !check_hle ())
5459 return;
5460
5461 /* Check BND prefix. */
5462 if (i.bnd_prefix && !i.tm.opcode_modifier.bndprefixok)
5463 as_bad (_("expecting valid branch instruction after `bnd'"));
5464
5465 /* Check NOTRACK prefix. */
5466 if (i.notrack_prefix && i.tm.opcode_modifier.prefixok != PrefixNoTrack)
5467 as_bad (_("expecting indirect branch instruction after `notrack'"));
5468
5469 if (is_cpu (&i.tm, CpuMPX))
5470 {
5471 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
5472 as_bad (_("32-bit address isn't allowed in 64-bit MPX instructions."));
5473 else if (flag_code != CODE_16BIT
5474 ? i.prefix[ADDR_PREFIX]
5475 : i.mem_operands && !i.prefix[ADDR_PREFIX])
5476 as_bad (_("16-bit address isn't allowed in MPX instructions"));
5477 }
5478
5479 /* Insert BND prefix. */
5480 if (add_bnd_prefix && i.tm.opcode_modifier.bndprefixok)
5481 {
5482 if (!i.prefix[BND_PREFIX])
5483 add_prefix (BND_PREFIX_OPCODE);
5484 else if (i.prefix[BND_PREFIX] != BND_PREFIX_OPCODE)
5485 {
5486 as_warn (_("replacing `rep'/`repe' prefix by `bnd'"));
5487 i.prefix[BND_PREFIX] = BND_PREFIX_OPCODE;
5488 }
5489 }
5490
5491 /* Check string instruction segment overrides. */
5492 if (i.tm.opcode_modifier.isstring >= IS_STRING_ES_OP0)
5493 {
5494 gas_assert (i.mem_operands);
5495 if (!check_string ())
5496 return;
5497 i.disp_operands = 0;
5498 }
5499
5500 /* The memory operand of (%dx) should be only used with input/output
5501 instructions (base opcodes: 0x6c, 0x6e, 0xec, 0xee). */
5502 if (i.input_output_operand
5503 && ((i.tm.base_opcode | 0x82) != 0xee
5504 || i.tm.opcode_space != SPACE_BASE))
5505 {
5506 as_bad (_("input/output port address isn't allowed with `%s'"),
5507 insn_name (&i.tm));
5508 return;
5509 }
5510
5511 if (optimize && !i.no_optimize && i.tm.opcode_modifier.optimize)
5512 optimize_encoding ();
5513
5514 /* Past optimization there's no need to distinguish vex_encoding_evex and
5515 vex_encoding_evex512 anymore. */
5516 if (i.vec_encoding == vex_encoding_evex512)
5517 i.vec_encoding = vex_encoding_evex;
5518
5519 if (use_unaligned_vector_move)
5520 encode_with_unaligned_vector_move ();
5521
5522 if (!process_suffix ())
5523 return;
5524
5525 /* Check if IP-relative addressing requirements can be satisfied. */
5526 if (is_cpu (&i.tm, CpuPREFETCHI)
5527 && !(i.base_reg && i.base_reg->reg_num == RegIP))
5528 as_warn (_("'%s' only supports RIP-relative address"), insn_name (&i.tm));
5529
5530 /* Update operand types and check extended states. */
5531 for (j = 0; j < i.operands; j++)
5532 {
5533 i.types[j] = operand_type_and (i.types[j], i.tm.operand_types[j]);
5534 switch (i.tm.operand_types[j].bitfield.class)
5535 {
5536 default:
5537 break;
5538 case RegMMX:
5539 i.xstate |= xstate_mmx;
5540 break;
5541 case RegMask:
5542 i.xstate |= xstate_mask;
5543 break;
5544 case RegSIMD:
5545 if (i.tm.operand_types[j].bitfield.tmmword)
5546 i.xstate |= xstate_tmm;
5547 else if (i.tm.operand_types[j].bitfield.zmmword
5548 && !i.tm.opcode_modifier.vex
5549 && vector_size >= VSZ512)
5550 i.xstate |= xstate_zmm;
5551 else if (i.tm.operand_types[j].bitfield.ymmword
5552 && vector_size >= VSZ256)
5553 i.xstate |= xstate_ymm;
5554 else if (i.tm.operand_types[j].bitfield.xmmword)
5555 i.xstate |= xstate_xmm;
5556 break;
5557 }
5558 }
5559
5560 /* Make still unresolved immediate matches conform to size of immediate
5561 given in i.suffix. */
5562 if (!finalize_imm ())
5563 return;
5564
5565 if (i.types[0].bitfield.imm1)
5566 i.imm_operands = 0; /* kludge for shift insns. */
5567
5568 /* For insns with operands there are more diddles to do to the opcode. */
5569 if (i.operands)
5570 {
5571 if (!process_operands ())
5572 return;
5573 }
5574 else if (!quiet_warnings && i.tm.opcode_modifier.operandconstraint == UGH)
5575 {
5576 /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc. */
5577 as_warn (_("translating to `%sp'"), insn_name (&i.tm));
5578 }
5579
5580 if (is_any_vex_encoding (&i.tm))
5581 {
5582 if (!cpu_arch_flags.bitfield.cpui286)
5583 {
5584 as_bad (_("instruction `%s' isn't supported outside of protected mode."),
5585 insn_name (&i.tm));
5586 return;
5587 }
5588
5589 /* Check for explicit REX prefix. */
5590 if (i.prefix[REX_PREFIX] || i.rex_encoding)
5591 {
5592 as_bad (_("REX prefix invalid with `%s'"), insn_name (&i.tm));
5593 return;
5594 }
5595
5596 if (i.tm.opcode_modifier.vex)
5597 build_vex_prefix (t);
5598 else
5599 build_evex_prefix ();
5600
5601 /* The individual REX.RXBW bits got consumed. */
5602 i.rex &= REX_OPCODE;
5603 }
5604
5605 /* Handle conversion of 'int $3' --> special int3 insn. */
5606 if (i.tm.mnem_off == MN_int
5607 && i.op[0].imms->X_add_number == 3)
5608 {
5609 i.tm.base_opcode = INT3_OPCODE;
5610 i.imm_operands = 0;
5611 }
5612
5613 if ((i.tm.opcode_modifier.jump == JUMP
5614 || i.tm.opcode_modifier.jump == JUMP_BYTE
5615 || i.tm.opcode_modifier.jump == JUMP_DWORD)
5616 && i.op[0].disps->X_op == O_constant)
5617 {
5618 /* Convert "jmp constant" (and "call constant") to a jump (call) to
5619 the absolute address given by the constant. Since ix86 jumps and
5620 calls are pc relative, we need to generate a reloc. */
5621 i.op[0].disps->X_add_symbol = &abs_symbol;
5622 i.op[0].disps->X_op = O_symbol;
5623 }
5624
5625 /* For 8 bit registers we need an empty rex prefix. Also if the
5626 instruction already has a prefix, we need to convert old
5627 registers to new ones. */
5628
5629 if ((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte
5630 && (i.op[0].regs->reg_flags & RegRex64) != 0)
5631 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte
5632 && (i.op[1].regs->reg_flags & RegRex64) != 0)
5633 || (((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte)
5634 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte))
5635 && i.rex != 0))
5636 {
5637 int x;
5638
5639 i.rex |= REX_OPCODE;
5640 for (x = 0; x < 2; x++)
5641 {
5642 /* Look for 8 bit operand that uses old registers. */
5643 if (i.types[x].bitfield.class == Reg && i.types[x].bitfield.byte
5644 && (i.op[x].regs->reg_flags & RegRex64) == 0)
5645 {
5646 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
5647 /* In case it is "hi" register, give up. */
5648 if (i.op[x].regs->reg_num > 3)
5649 as_bad (_("can't encode register '%s%s' in an "
5650 "instruction requiring REX prefix."),
5651 register_prefix, i.op[x].regs->reg_name);
5652
5653 /* Otherwise it is equivalent to the extended register.
5654 Since the encoding doesn't change this is merely
5655 cosmetic cleanup for debug output. */
5656
5657 i.op[x].regs = i.op[x].regs + 8;
5658 }
5659 }
5660 }
5661
5662 if (i.rex == 0 && i.rex_encoding)
5663 {
5664 /* Check if we can add a REX_OPCODE byte. Look for 8 bit operand
5665 that uses legacy register. If it is "hi" register, don't add
5666 the REX_OPCODE byte. */
5667 int x;
5668 for (x = 0; x < 2; x++)
5669 if (i.types[x].bitfield.class == Reg
5670 && i.types[x].bitfield.byte
5671 && (i.op[x].regs->reg_flags & RegRex64) == 0
5672 && i.op[x].regs->reg_num > 3)
5673 {
5674 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
5675 i.rex_encoding = false;
5676 break;
5677 }
5678
5679 if (i.rex_encoding)
5680 i.rex = REX_OPCODE;
5681 }
5682
5683 if (i.rex != 0)
5684 add_prefix (REX_OPCODE | i.rex);
5685
5686 insert_lfence_before ();
5687
5688 /* We are ready to output the insn. */
5689 output_insn ();
5690
5691 insert_lfence_after ();
5692
5693 last_insn.seg = now_seg;
5694
5695 if (i.tm.opcode_modifier.isprefix)
5696 {
5697 last_insn.kind = last_insn_prefix;
5698 last_insn.name = insn_name (&i.tm);
5699 last_insn.file = as_where (&last_insn.line);
5700 }
5701 else
5702 last_insn.kind = last_insn_other;
5703 }
5704
5705 /* The Q suffix is generally valid only in 64-bit mode, with very few
5706 exceptions: fild, fistp, fisttp, and cmpxchg8b. Note that for fild
5707 and fisttp only one of their two templates is matched below: That's
5708 sufficient since other relevant attributes are the same between both
5709 respective templates. */
5710 static INLINE bool q_suffix_allowed(const insn_template *t)
5711 {
5712 return flag_code == CODE_64BIT
5713 || (t->opcode_space == SPACE_BASE
5714 && t->base_opcode == 0xdf
5715 && (t->extension_opcode & 1)) /* fild / fistp / fisttp */
5716 || t->mnem_off == MN_cmpxchg8b;
5717 }
5718
5719 static const char *
5720 parse_insn (const char *line, char *mnemonic, bool prefix_only)
5721 {
5722 const char *l = line, *token_start = l;
5723 char *mnem_p;
5724 bool pass1 = !current_templates;
5725 int supported;
5726 const insn_template *t;
5727 char *dot_p = NULL;
5728
5729 while (1)
5730 {
5731 mnem_p = mnemonic;
5732 /* Pseudo-prefixes start with an opening figure brace. */
5733 if ((*mnem_p = *l) == '{')
5734 {
5735 ++mnem_p;
5736 ++l;
5737 }
5738 while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
5739 {
5740 if (*mnem_p == '.')
5741 dot_p = mnem_p;
5742 mnem_p++;
5743 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
5744 {
5745 too_long:
5746 as_bad (_("no such instruction: `%s'"), token_start);
5747 return NULL;
5748 }
5749 l++;
5750 }
5751 /* Pseudo-prefixes end with a closing figure brace. */
5752 if (*mnemonic == '{' && *l == '}')
5753 {
5754 *mnem_p++ = *l++;
5755 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
5756 goto too_long;
5757 *mnem_p = '\0';
5758
5759 /* Point l at the closing brace if there's no other separator. */
5760 if (*l != END_OF_INSN && !is_space_char (*l)
5761 && *l != PREFIX_SEPARATOR)
5762 --l;
5763 }
5764 else if (!is_space_char (*l)
5765 && *l != END_OF_INSN
5766 && (intel_syntax
5767 || (*l != PREFIX_SEPARATOR && *l != ',')))
5768 {
5769 if (prefix_only)
5770 break;
5771 as_bad (_("invalid character %s in mnemonic"),
5772 output_invalid (*l));
5773 return NULL;
5774 }
5775 if (token_start == l)
5776 {
5777 if (!intel_syntax && *l == PREFIX_SEPARATOR)
5778 as_bad (_("expecting prefix; got nothing"));
5779 else
5780 as_bad (_("expecting mnemonic; got nothing"));
5781 return NULL;
5782 }
5783
5784 /* Look up instruction (or prefix) via hash table. */
5785 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
5786
5787 if (*l != END_OF_INSN
5788 && (!is_space_char (*l) || l[1] != END_OF_INSN)
5789 && current_templates
5790 && current_templates->start->opcode_modifier.isprefix)
5791 {
5792 if (!cpu_flags_check_cpu64 (current_templates->start))
5793 {
5794 as_bad ((flag_code != CODE_64BIT
5795 ? _("`%s' is only supported in 64-bit mode")
5796 : _("`%s' is not supported in 64-bit mode")),
5797 insn_name (current_templates->start));
5798 return NULL;
5799 }
5800 /* If we are in 16-bit mode, do not allow addr16 or data16.
5801 Similarly, in 32-bit mode, do not allow addr32 or data32. */
5802 if ((current_templates->start->opcode_modifier.size == SIZE16
5803 || current_templates->start->opcode_modifier.size == SIZE32)
5804 && flag_code != CODE_64BIT
5805 && ((current_templates->start->opcode_modifier.size == SIZE32)
5806 ^ (flag_code == CODE_16BIT)))
5807 {
5808 as_bad (_("redundant %s prefix"),
5809 insn_name (current_templates->start));
5810 return NULL;
5811 }
5812
5813 if (current_templates->start->base_opcode == PSEUDO_PREFIX)
5814 {
5815 /* Handle pseudo prefixes. */
5816 switch (current_templates->start->extension_opcode)
5817 {
5818 case Prefix_Disp8:
5819 /* {disp8} */
5820 i.disp_encoding = disp_encoding_8bit;
5821 break;
5822 case Prefix_Disp16:
5823 /* {disp16} */
5824 i.disp_encoding = disp_encoding_16bit;
5825 break;
5826 case Prefix_Disp32:
5827 /* {disp32} */
5828 i.disp_encoding = disp_encoding_32bit;
5829 break;
5830 case Prefix_Load:
5831 /* {load} */
5832 i.dir_encoding = dir_encoding_load;
5833 break;
5834 case Prefix_Store:
5835 /* {store} */
5836 i.dir_encoding = dir_encoding_store;
5837 break;
5838 case Prefix_VEX:
5839 /* {vex} */
5840 i.vec_encoding = vex_encoding_vex;
5841 break;
5842 case Prefix_VEX3:
5843 /* {vex3} */
5844 i.vec_encoding = vex_encoding_vex3;
5845 break;
5846 case Prefix_EVEX:
5847 /* {evex} */
5848 i.vec_encoding = vex_encoding_evex;
5849 break;
5850 case Prefix_REX:
5851 /* {rex} */
5852 i.rex_encoding = true;
5853 break;
5854 case Prefix_NoOptimize:
5855 /* {nooptimize} */
5856 i.no_optimize = true;
5857 break;
5858 default:
5859 abort ();
5860 }
5861 }
5862 else
5863 {
5864 /* Add prefix, checking for repeated prefixes. */
5865 switch (add_prefix (current_templates->start->base_opcode))
5866 {
5867 case PREFIX_EXIST:
5868 return NULL;
5869 case PREFIX_DS:
5870 if (is_cpu (current_templates->start, CpuIBT))
5871 i.notrack_prefix = insn_name (current_templates->start);
5872 break;
5873 case PREFIX_REP:
5874 if (is_cpu (current_templates->start, CpuHLE))
5875 i.hle_prefix = insn_name (current_templates->start);
5876 else if (is_cpu (current_templates->start, CpuMPX))
5877 i.bnd_prefix = insn_name (current_templates->start);
5878 else
5879 i.rep_prefix = insn_name (current_templates->start);
5880 break;
5881 default:
5882 break;
5883 }
5884 }
5885 /* Skip past PREFIX_SEPARATOR and reset token_start. */
5886 token_start = ++l;
5887 }
5888 else
5889 break;
5890 }
5891
5892 if (prefix_only)
5893 return token_start;
5894
5895 if (!current_templates)
5896 {
5897 /* Deprecated functionality (new code should use pseudo-prefixes instead):
5898 Check if we should swap operand or force 32bit displacement in
5899 encoding. */
5900 if (mnem_p - 2 == dot_p && dot_p[1] == 's')
5901 i.dir_encoding = dir_encoding_swap;
5902 else if (mnem_p - 3 == dot_p
5903 && dot_p[1] == 'd'
5904 && dot_p[2] == '8')
5905 i.disp_encoding = disp_encoding_8bit;
5906 else if (mnem_p - 4 == dot_p
5907 && dot_p[1] == 'd'
5908 && dot_p[2] == '3'
5909 && dot_p[3] == '2')
5910 i.disp_encoding = disp_encoding_32bit;
5911 else
5912 goto check_suffix;
5913 mnem_p = dot_p;
5914 *dot_p = '\0';
5915 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
5916 }
5917
5918 if (!current_templates || !pass1)
5919 {
5920 current_templates = NULL;
5921
5922 check_suffix:
5923 if (mnem_p > mnemonic)
5924 {
5925 /* See if we can get a match by trimming off a suffix. */
5926 switch (mnem_p[-1])
5927 {
5928 case WORD_MNEM_SUFFIX:
5929 if (intel_syntax && (intel_float_operand (mnemonic) & 2))
5930 i.suffix = SHORT_MNEM_SUFFIX;
5931 else
5932 /* Fall through. */
5933 case BYTE_MNEM_SUFFIX:
5934 case QWORD_MNEM_SUFFIX:
5935 i.suffix = mnem_p[-1];
5936 mnem_p[-1] = '\0';
5937 current_templates
5938 = (const templates *) str_hash_find (op_hash, mnemonic);
5939 break;
5940 case SHORT_MNEM_SUFFIX:
5941 case LONG_MNEM_SUFFIX:
5942 if (!intel_syntax)
5943 {
5944 i.suffix = mnem_p[-1];
5945 mnem_p[-1] = '\0';
5946 current_templates
5947 = (const templates *) str_hash_find (op_hash, mnemonic);
5948 }
5949 break;
5950
5951 /* Intel Syntax. */
5952 case 'd':
5953 if (intel_syntax)
5954 {
5955 if (intel_float_operand (mnemonic) == 1)
5956 i.suffix = SHORT_MNEM_SUFFIX;
5957 else
5958 i.suffix = LONG_MNEM_SUFFIX;
5959 mnem_p[-1] = '\0';
5960 current_templates
5961 = (const templates *) str_hash_find (op_hash, mnemonic);
5962 }
5963 /* For compatibility reasons accept MOVSD and CMPSD without
5964 operands even in AT&T mode. */
5965 else if (*l == END_OF_INSN
5966 || (is_space_char (*l) && l[1] == END_OF_INSN))
5967 {
5968 mnem_p[-1] = '\0';
5969 current_templates
5970 = (const templates *) str_hash_find (op_hash, mnemonic);
5971 if (current_templates != NULL
5972 /* MOVS or CMPS */
5973 && (current_templates->start->base_opcode | 2) == 0xa6
5974 && current_templates->start->opcode_space
5975 == SPACE_BASE
5976 && mnem_p[-2] == 's')
5977 {
5978 as_warn (_("found `%sd'; assuming `%sl' was meant"),
5979 mnemonic, mnemonic);
5980 i.suffix = LONG_MNEM_SUFFIX;
5981 }
5982 else
5983 {
5984 current_templates = NULL;
5985 mnem_p[-1] = 'd';
5986 }
5987 }
5988 break;
5989 }
5990 }
5991
5992 if (!current_templates)
5993 {
5994 if (pass1)
5995 as_bad (_("no such instruction: `%s'"), token_start);
5996 return NULL;
5997 }
5998 }
5999
6000 if (current_templates->start->opcode_modifier.jump == JUMP
6001 || current_templates->start->opcode_modifier.jump == JUMP_BYTE)
6002 {
6003 /* Check for a branch hint. We allow ",pt" and ",pn" for
6004 predict taken and predict not taken respectively.
6005 I'm not sure that branch hints actually do anything on loop
6006 and jcxz insns (JumpByte) for current Pentium4 chips. They
6007 may work in the future and it doesn't hurt to accept them
6008 now. */
6009 if (l[0] == ',' && l[1] == 'p')
6010 {
6011 if (l[2] == 't')
6012 {
6013 if (!add_prefix (DS_PREFIX_OPCODE))
6014 return NULL;
6015 l += 3;
6016 }
6017 else if (l[2] == 'n')
6018 {
6019 if (!add_prefix (CS_PREFIX_OPCODE))
6020 return NULL;
6021 l += 3;
6022 }
6023 }
6024 }
6025 /* Any other comma loses. */
6026 if (*l == ',')
6027 {
6028 as_bad (_("invalid character %s in mnemonic"),
6029 output_invalid (*l));
6030 return NULL;
6031 }
6032
6033 /* Check if instruction is supported on specified architecture. */
6034 supported = 0;
6035 for (t = current_templates->start; t < current_templates->end; ++t)
6036 {
6037 supported |= cpu_flags_match (t);
6038
6039 if (i.suffix == QWORD_MNEM_SUFFIX && !q_suffix_allowed (t))
6040 supported &= ~CPU_FLAGS_64BIT_MATCH;
6041
6042 if (supported == CPU_FLAGS_PERFECT_MATCH)
6043 return l;
6044 }
6045
6046 if (pass1)
6047 {
6048 if (supported & CPU_FLAGS_64BIT_MATCH)
6049 i.error = unsupported_on_arch;
6050 else
6051 i.error = unsupported_64bit;
6052 }
6053
6054 return NULL;
6055 }
6056
6057 static char *
6058 parse_operands (char *l, const char *mnemonic)
6059 {
6060 char *token_start;
6061
6062 /* 1 if operand is pending after ','. */
6063 unsigned int expecting_operand = 0;
6064
6065 while (*l != END_OF_INSN)
6066 {
6067 /* Non-zero if operand parens not balanced. */
6068 unsigned int paren_not_balanced = 0;
6069 /* True if inside double quotes. */
6070 bool in_quotes = false;
6071
6072 /* Skip optional white space before operand. */
6073 if (is_space_char (*l))
6074 ++l;
6075 if (!is_operand_char (*l) && *l != END_OF_INSN && *l != '"')
6076 {
6077 as_bad (_("invalid character %s before operand %d"),
6078 output_invalid (*l),
6079 i.operands + 1);
6080 return NULL;
6081 }
6082 token_start = l; /* After white space. */
6083 while (in_quotes || paren_not_balanced || *l != ',')
6084 {
6085 if (*l == END_OF_INSN)
6086 {
6087 if (in_quotes)
6088 {
6089 as_bad (_("unbalanced double quotes in operand %d."),
6090 i.operands + 1);
6091 return NULL;
6092 }
6093 if (paren_not_balanced)
6094 {
6095 know (!intel_syntax);
6096 as_bad (_("unbalanced parenthesis in operand %d."),
6097 i.operands + 1);
6098 return NULL;
6099 }
6100 else
6101 break; /* we are done */
6102 }
6103 else if (*l == '\\' && l[1] == '"')
6104 ++l;
6105 else if (*l == '"')
6106 in_quotes = !in_quotes;
6107 else if (!in_quotes && !is_operand_char (*l) && !is_space_char (*l))
6108 {
6109 as_bad (_("invalid character %s in operand %d"),
6110 output_invalid (*l),
6111 i.operands + 1);
6112 return NULL;
6113 }
6114 if (!intel_syntax && !in_quotes)
6115 {
6116 if (*l == '(')
6117 ++paren_not_balanced;
6118 if (*l == ')')
6119 --paren_not_balanced;
6120 }
6121 l++;
6122 }
6123 if (l != token_start)
6124 { /* Yes, we've read in another operand. */
6125 unsigned int operand_ok;
6126 this_operand = i.operands++;
6127 if (i.operands > MAX_OPERANDS)
6128 {
6129 as_bad (_("spurious operands; (%d operands/instruction max)"),
6130 MAX_OPERANDS);
6131 return NULL;
6132 }
6133 i.types[this_operand].bitfield.unspecified = 1;
6134 /* Now parse operand adding info to 'i' as we go along. */
6135 END_STRING_AND_SAVE (l);
6136
6137 if (i.mem_operands > 1)
6138 {
6139 as_bad (_("too many memory references for `%s'"),
6140 mnemonic);
6141 return 0;
6142 }
6143
6144 if (intel_syntax)
6145 operand_ok =
6146 i386_intel_operand (token_start,
6147 intel_float_operand (mnemonic));
6148 else
6149 operand_ok = i386_att_operand (token_start);
6150
6151 RESTORE_END_STRING (l);
6152 if (!operand_ok)
6153 return NULL;
6154 }
6155 else
6156 {
6157 if (expecting_operand)
6158 {
6159 expecting_operand_after_comma:
6160 as_bad (_("expecting operand after ','; got nothing"));
6161 return NULL;
6162 }
6163 if (*l == ',')
6164 {
6165 as_bad (_("expecting operand before ','; got nothing"));
6166 return NULL;
6167 }
6168 }
6169
6170 /* Now *l must be either ',' or END_OF_INSN. */
6171 if (*l == ',')
6172 {
6173 if (*++l == END_OF_INSN)
6174 {
6175 /* Just skip it, if it's \n complain. */
6176 goto expecting_operand_after_comma;
6177 }
6178 expecting_operand = 1;
6179 }
6180 }
6181 return l;
6182 }
6183
6184 static void
6185 swap_2_operands (unsigned int xchg1, unsigned int xchg2)
6186 {
6187 union i386_op temp_op;
6188 i386_operand_type temp_type;
6189 unsigned int temp_flags;
6190 enum bfd_reloc_code_real temp_reloc;
6191
6192 temp_type = i.types[xchg2];
6193 i.types[xchg2] = i.types[xchg1];
6194 i.types[xchg1] = temp_type;
6195
6196 temp_flags = i.flags[xchg2];
6197 i.flags[xchg2] = i.flags[xchg1];
6198 i.flags[xchg1] = temp_flags;
6199
6200 temp_op = i.op[xchg2];
6201 i.op[xchg2] = i.op[xchg1];
6202 i.op[xchg1] = temp_op;
6203
6204 temp_reloc = i.reloc[xchg2];
6205 i.reloc[xchg2] = i.reloc[xchg1];
6206 i.reloc[xchg1] = temp_reloc;
6207
6208 temp_flags = i.imm_bits[xchg2];
6209 i.imm_bits[xchg2] = i.imm_bits[xchg1];
6210 i.imm_bits[xchg1] = temp_flags;
6211
6212 if (i.mask.reg)
6213 {
6214 if (i.mask.operand == xchg1)
6215 i.mask.operand = xchg2;
6216 else if (i.mask.operand == xchg2)
6217 i.mask.operand = xchg1;
6218 }
6219 if (i.broadcast.type || i.broadcast.bytes)
6220 {
6221 if (i.broadcast.operand == xchg1)
6222 i.broadcast.operand = xchg2;
6223 else if (i.broadcast.operand == xchg2)
6224 i.broadcast.operand = xchg1;
6225 }
6226 }
6227
6228 static void
6229 swap_operands (void)
6230 {
6231 switch (i.operands)
6232 {
6233 case 5:
6234 case 4:
6235 swap_2_operands (1, i.operands - 2);
6236 /* Fall through. */
6237 case 3:
6238 case 2:
6239 swap_2_operands (0, i.operands - 1);
6240 break;
6241 default:
6242 abort ();
6243 }
6244
6245 if (i.mem_operands == 2)
6246 {
6247 const reg_entry *temp_seg;
6248 temp_seg = i.seg[0];
6249 i.seg[0] = i.seg[1];
6250 i.seg[1] = temp_seg;
6251 }
6252 }
6253
6254 /* Try to ensure constant immediates are represented in the smallest
6255 opcode possible. */
6256 static void
6257 optimize_imm (void)
6258 {
6259 char guess_suffix = 0;
6260 int op;
6261
6262 if (i.suffix)
6263 guess_suffix = i.suffix;
6264 else if (i.reg_operands)
6265 {
6266 /* Figure out a suffix from the last register operand specified.
6267 We can't do this properly yet, i.e. excluding special register
6268 instances, but the following works for instructions with
6269 immediates. In any case, we can't set i.suffix yet. */
6270 for (op = i.operands; --op >= 0;)
6271 if (i.types[op].bitfield.class != Reg)
6272 continue;
6273 else if (i.types[op].bitfield.byte)
6274 {
6275 guess_suffix = BYTE_MNEM_SUFFIX;
6276 break;
6277 }
6278 else if (i.types[op].bitfield.word)
6279 {
6280 guess_suffix = WORD_MNEM_SUFFIX;
6281 break;
6282 }
6283 else if (i.types[op].bitfield.dword)
6284 {
6285 guess_suffix = LONG_MNEM_SUFFIX;
6286 break;
6287 }
6288 else if (i.types[op].bitfield.qword)
6289 {
6290 guess_suffix = QWORD_MNEM_SUFFIX;
6291 break;
6292 }
6293 }
6294 else if ((flag_code == CODE_16BIT)
6295 ^ (i.prefix[DATA_PREFIX] != 0 && !(i.prefix[REX_PREFIX] & REX_W)))
6296 guess_suffix = WORD_MNEM_SUFFIX;
6297 else if (flag_code != CODE_64BIT
6298 || (!(i.prefix[REX_PREFIX] & REX_W)
6299 /* A more generic (but also more involved) way of dealing
6300 with the special case(s) would be to go look for
6301 DefaultSize attributes on any of the templates. */
6302 && current_templates->start->mnem_off != MN_push))
6303 guess_suffix = LONG_MNEM_SUFFIX;
6304
6305 for (op = i.operands; --op >= 0;)
6306 if (operand_type_check (i.types[op], imm))
6307 {
6308 switch (i.op[op].imms->X_op)
6309 {
6310 case O_constant:
6311 /* If a suffix is given, this operand may be shortened. */
6312 switch (guess_suffix)
6313 {
6314 case LONG_MNEM_SUFFIX:
6315 i.types[op].bitfield.imm32 = 1;
6316 i.types[op].bitfield.imm64 = 1;
6317 break;
6318 case WORD_MNEM_SUFFIX:
6319 i.types[op].bitfield.imm16 = 1;
6320 i.types[op].bitfield.imm32 = 1;
6321 i.types[op].bitfield.imm32s = 1;
6322 i.types[op].bitfield.imm64 = 1;
6323 break;
6324 case BYTE_MNEM_SUFFIX:
6325 i.types[op].bitfield.imm8 = 1;
6326 i.types[op].bitfield.imm8s = 1;
6327 i.types[op].bitfield.imm16 = 1;
6328 i.types[op].bitfield.imm32 = 1;
6329 i.types[op].bitfield.imm32s = 1;
6330 i.types[op].bitfield.imm64 = 1;
6331 break;
6332 }
6333
6334 /* If this operand is at most 16 bits, convert it
6335 to a signed 16 bit number before trying to see
6336 whether it will fit in an even smaller size.
6337 This allows a 16-bit operand such as $0xffe0 to
6338 be recognised as within Imm8S range. */
6339 if ((i.types[op].bitfield.imm16)
6340 && fits_in_unsigned_word (i.op[op].imms->X_add_number))
6341 {
6342 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
6343 ^ 0x8000) - 0x8000);
6344 }
6345 #ifdef BFD64
6346 /* Store 32-bit immediate in 64-bit for 64-bit BFD. */
6347 if ((i.types[op].bitfield.imm32)
6348 && fits_in_unsigned_long (i.op[op].imms->X_add_number))
6349 {
6350 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
6351 ^ ((offsetT) 1 << 31))
6352 - ((offsetT) 1 << 31));
6353 }
6354 #endif
6355 i.types[op]
6356 = operand_type_or (i.types[op],
6357 smallest_imm_type (i.op[op].imms->X_add_number));
6358
6359 /* We must avoid matching of Imm32 templates when 64bit
6360 only immediate is available. */
6361 if (guess_suffix == QWORD_MNEM_SUFFIX)
6362 i.types[op].bitfield.imm32 = 0;
6363 break;
6364
6365 case O_absent:
6366 case O_register:
6367 abort ();
6368
6369 /* Symbols and expressions. */
6370 default:
6371 /* Convert symbolic operand to proper sizes for matching, but don't
6372 prevent matching a set of insns that only supports sizes other
6373 than those matching the insn suffix. */
6374 {
6375 i386_operand_type mask, allowed;
6376 const insn_template *t = current_templates->start;
6377
6378 operand_type_set (&mask, 0);
6379 switch (guess_suffix)
6380 {
6381 case QWORD_MNEM_SUFFIX:
6382 mask.bitfield.imm64 = 1;
6383 mask.bitfield.imm32s = 1;
6384 break;
6385 case LONG_MNEM_SUFFIX:
6386 mask.bitfield.imm32 = 1;
6387 break;
6388 case WORD_MNEM_SUFFIX:
6389 mask.bitfield.imm16 = 1;
6390 break;
6391 case BYTE_MNEM_SUFFIX:
6392 mask.bitfield.imm8 = 1;
6393 break;
6394 default:
6395 break;
6396 }
6397
6398 allowed = operand_type_and (t->operand_types[op], mask);
6399 while (++t < current_templates->end)
6400 {
6401 allowed = operand_type_or (allowed, t->operand_types[op]);
6402 allowed = operand_type_and (allowed, mask);
6403 }
6404
6405 if (!operand_type_all_zero (&allowed))
6406 i.types[op] = operand_type_and (i.types[op], mask);
6407 }
6408 break;
6409 }
6410 }
6411 }
6412
6413 /* Try to use the smallest displacement type too. */
6414 static bool
6415 optimize_disp (const insn_template *t)
6416 {
6417 unsigned int op;
6418
6419 if (!want_disp32 (t)
6420 && (!t->opcode_modifier.jump
6421 || i.jumpabsolute || i.types[0].bitfield.baseindex))
6422 {
6423 for (op = 0; op < i.operands; ++op)
6424 {
6425 const expressionS *exp = i.op[op].disps;
6426
6427 if (!operand_type_check (i.types[op], disp))
6428 continue;
6429
6430 if (exp->X_op != O_constant)
6431 continue;
6432
6433 /* Since displacement is signed extended to 64bit, don't allow
6434 disp32 if it is out of range. */
6435 if (fits_in_signed_long (exp->X_add_number))
6436 continue;
6437
6438 i.types[op].bitfield.disp32 = 0;
6439 if (i.types[op].bitfield.baseindex)
6440 {
6441 as_bad (_("0x%" PRIx64 " out of range of signed 32bit displacement"),
6442 (uint64_t) exp->X_add_number);
6443 return false;
6444 }
6445 }
6446 }
6447
6448 /* Don't optimize displacement for movabs since it only takes 64bit
6449 displacement. */
6450 if (i.disp_encoding > disp_encoding_8bit
6451 || (flag_code == CODE_64BIT && t->mnem_off == MN_movabs))
6452 return true;
6453
6454 for (op = i.operands; op-- > 0;)
6455 if (operand_type_check (i.types[op], disp))
6456 {
6457 if (i.op[op].disps->X_op == O_constant)
6458 {
6459 offsetT op_disp = i.op[op].disps->X_add_number;
6460
6461 if (!op_disp && i.types[op].bitfield.baseindex)
6462 {
6463 i.types[op] = operand_type_and_not (i.types[op], anydisp);
6464 i.op[op].disps = NULL;
6465 i.disp_operands--;
6466 continue;
6467 }
6468
6469 if (i.types[op].bitfield.disp16
6470 && fits_in_unsigned_word (op_disp))
6471 {
6472 /* If this operand is at most 16 bits, convert
6473 to a signed 16 bit number and don't use 64bit
6474 displacement. */
6475 op_disp = ((op_disp ^ 0x8000) - 0x8000);
6476 i.types[op].bitfield.disp64 = 0;
6477 }
6478
6479 #ifdef BFD64
6480 /* Optimize 64-bit displacement to 32-bit for 64-bit BFD. */
6481 if ((flag_code != CODE_64BIT
6482 ? i.types[op].bitfield.disp32
6483 : want_disp32 (t)
6484 && (!t->opcode_modifier.jump
6485 || i.jumpabsolute || i.types[op].bitfield.baseindex))
6486 && fits_in_unsigned_long (op_disp))
6487 {
6488 /* If this operand is at most 32 bits, convert
6489 to a signed 32 bit number and don't use 64bit
6490 displacement. */
6491 op_disp = (op_disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
6492 i.types[op].bitfield.disp64 = 0;
6493 i.types[op].bitfield.disp32 = 1;
6494 }
6495
6496 if (flag_code == CODE_64BIT && fits_in_signed_long (op_disp))
6497 {
6498 i.types[op].bitfield.disp64 = 0;
6499 i.types[op].bitfield.disp32 = 1;
6500 }
6501 #endif
6502 if ((i.types[op].bitfield.disp32
6503 || i.types[op].bitfield.disp16)
6504 && fits_in_disp8 (op_disp))
6505 i.types[op].bitfield.disp8 = 1;
6506
6507 i.op[op].disps->X_add_number = op_disp;
6508 }
6509 else if (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
6510 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL)
6511 {
6512 fix_new_exp (frag_now, frag_more (0) - frag_now->fr_literal, 0,
6513 i.op[op].disps, 0, i.reloc[op]);
6514 i.types[op] = operand_type_and_not (i.types[op], anydisp);
6515 }
6516 else
6517 /* We only support 64bit displacement on constants. */
6518 i.types[op].bitfield.disp64 = 0;
6519 }
6520
6521 return true;
6522 }
6523
6524 /* Return 1 if there is a match in broadcast bytes between operand
6525 GIVEN and instruction template T. */
6526
6527 static INLINE int
6528 match_broadcast_size (const insn_template *t, unsigned int given)
6529 {
6530 return ((t->opcode_modifier.broadcast == BYTE_BROADCAST
6531 && i.types[given].bitfield.byte)
6532 || (t->opcode_modifier.broadcast == WORD_BROADCAST
6533 && i.types[given].bitfield.word)
6534 || (t->opcode_modifier.broadcast == DWORD_BROADCAST
6535 && i.types[given].bitfield.dword)
6536 || (t->opcode_modifier.broadcast == QWORD_BROADCAST
6537 && i.types[given].bitfield.qword));
6538 }
6539
6540 /* Check if operands are valid for the instruction. */
6541
6542 static int
6543 check_VecOperands (const insn_template *t)
6544 {
6545 unsigned int op;
6546 i386_cpu_flags cpu;
6547
6548 /* Templates allowing for ZMMword as well as YMMword and/or XMMword for
6549 any one operand are implicity requiring AVX512VL support if the actual
6550 operand size is YMMword or XMMword. Since this function runs after
6551 template matching, there's no need to check for YMMword/XMMword in
6552 the template. */
6553 cpu = cpu_flags_and (cpu_flags_from_attr (t->cpu), avx512);
6554 if (!cpu_flags_all_zero (&cpu)
6555 && !is_cpu (t, CpuAVX512VL)
6556 && !cpu_arch_flags.bitfield.cpuavx512vl
6557 && (!t->opcode_modifier.vex || need_evex_encoding ()))
6558 {
6559 for (op = 0; op < t->operands; ++op)
6560 {
6561 if (t->operand_types[op].bitfield.zmmword
6562 && (i.types[op].bitfield.ymmword
6563 || i.types[op].bitfield.xmmword))
6564 {
6565 i.error = unsupported;
6566 return 1;
6567 }
6568 }
6569 }
6570
6571 /* Somewhat similarly, templates specifying both AVX and AVX2 are
6572 requiring AVX2 support if the actual operand size is YMMword. */
6573 if (is_cpu (t, CpuAVX) && is_cpu (t, CpuAVX2)
6574 && !cpu_arch_flags.bitfield.cpuavx2)
6575 {
6576 for (op = 0; op < t->operands; ++op)
6577 {
6578 if (t->operand_types[op].bitfield.xmmword
6579 && i.types[op].bitfield.ymmword)
6580 {
6581 i.error = unsupported;
6582 return 1;
6583 }
6584 }
6585 }
6586
6587 /* Without VSIB byte, we can't have a vector register for index. */
6588 if (!t->opcode_modifier.sib
6589 && i.index_reg
6590 && (i.index_reg->reg_type.bitfield.xmmword
6591 || i.index_reg->reg_type.bitfield.ymmword
6592 || i.index_reg->reg_type.bitfield.zmmword))
6593 {
6594 i.error = unsupported_vector_index_register;
6595 return 1;
6596 }
6597
6598 /* Check if default mask is allowed. */
6599 if (t->opcode_modifier.operandconstraint == NO_DEFAULT_MASK
6600 && (!i.mask.reg || i.mask.reg->reg_num == 0))
6601 {
6602 i.error = no_default_mask;
6603 return 1;
6604 }
6605
6606 /* For VSIB byte, we need a vector register for index, and all vector
6607 registers must be distinct. */
6608 if (t->opcode_modifier.sib && t->opcode_modifier.sib != SIBMEM)
6609 {
6610 if (!i.index_reg
6611 || !((t->opcode_modifier.sib == VECSIB128
6612 && i.index_reg->reg_type.bitfield.xmmword)
6613 || (t->opcode_modifier.sib == VECSIB256
6614 && i.index_reg->reg_type.bitfield.ymmword)
6615 || (t->opcode_modifier.sib == VECSIB512
6616 && i.index_reg->reg_type.bitfield.zmmword)))
6617 {
6618 i.error = invalid_vsib_address;
6619 return 1;
6620 }
6621
6622 gas_assert (i.reg_operands == 2 || i.mask.reg);
6623 if (i.reg_operands == 2 && !i.mask.reg)
6624 {
6625 gas_assert (i.types[0].bitfield.class == RegSIMD);
6626 gas_assert (i.types[0].bitfield.xmmword
6627 || i.types[0].bitfield.ymmword);
6628 gas_assert (i.types[2].bitfield.class == RegSIMD);
6629 gas_assert (i.types[2].bitfield.xmmword
6630 || i.types[2].bitfield.ymmword);
6631 if (operand_check == check_none)
6632 return 0;
6633 if (register_number (i.op[0].regs)
6634 != register_number (i.index_reg)
6635 && register_number (i.op[2].regs)
6636 != register_number (i.index_reg)
6637 && register_number (i.op[0].regs)
6638 != register_number (i.op[2].regs))
6639 return 0;
6640 if (operand_check == check_error)
6641 {
6642 i.error = invalid_vector_register_set;
6643 return 1;
6644 }
6645 as_warn (_("mask, index, and destination registers should be distinct"));
6646 }
6647 else if (i.reg_operands == 1 && i.mask.reg)
6648 {
6649 if (i.types[1].bitfield.class == RegSIMD
6650 && (i.types[1].bitfield.xmmword
6651 || i.types[1].bitfield.ymmword
6652 || i.types[1].bitfield.zmmword)
6653 && (register_number (i.op[1].regs)
6654 == register_number (i.index_reg)))
6655 {
6656 if (operand_check == check_error)
6657 {
6658 i.error = invalid_vector_register_set;
6659 return 1;
6660 }
6661 if (operand_check != check_none)
6662 as_warn (_("index and destination registers should be distinct"));
6663 }
6664 }
6665 }
6666
6667 /* For AMX instructions with 3 TMM register operands, all operands
6668 must be distinct. */
6669 if (i.reg_operands == 3
6670 && t->operand_types[0].bitfield.tmmword
6671 && (i.op[0].regs == i.op[1].regs
6672 || i.op[0].regs == i.op[2].regs
6673 || i.op[1].regs == i.op[2].regs))
6674 {
6675 i.error = invalid_tmm_register_set;
6676 return 1;
6677 }
6678
6679 /* For some special instructions require that destination must be distinct
6680 from source registers. */
6681 if (t->opcode_modifier.operandconstraint == DISTINCT_DEST)
6682 {
6683 unsigned int dest_reg = i.operands - 1;
6684
6685 know (i.operands >= 3);
6686
6687 /* #UD if dest_reg == src1_reg or dest_reg == src2_reg. */
6688 if (i.op[dest_reg - 1].regs == i.op[dest_reg].regs
6689 || (i.reg_operands > 2
6690 && i.op[dest_reg - 2].regs == i.op[dest_reg].regs))
6691 {
6692 i.error = invalid_dest_and_src_register_set;
6693 return 1;
6694 }
6695 }
6696
6697 /* Check if broadcast is supported by the instruction and is applied
6698 to the memory operand. */
6699 if (i.broadcast.type || i.broadcast.bytes)
6700 {
6701 i386_operand_type type, overlap;
6702
6703 /* Check if specified broadcast is supported in this instruction,
6704 and its broadcast bytes match the memory operand. */
6705 op = i.broadcast.operand;
6706 if (!t->opcode_modifier.broadcast
6707 || !(i.flags[op] & Operand_Mem)
6708 || (!i.types[op].bitfield.unspecified
6709 && !match_broadcast_size (t, op)))
6710 {
6711 bad_broadcast:
6712 i.error = unsupported_broadcast;
6713 return 1;
6714 }
6715
6716 operand_type_set (&type, 0);
6717 switch (get_broadcast_bytes (t, false))
6718 {
6719 case 2:
6720 type.bitfield.word = 1;
6721 break;
6722 case 4:
6723 type.bitfield.dword = 1;
6724 break;
6725 case 8:
6726 type.bitfield.qword = 1;
6727 break;
6728 case 16:
6729 type.bitfield.xmmword = 1;
6730 break;
6731 case 32:
6732 if (vector_size < VSZ256)
6733 goto bad_broadcast;
6734 type.bitfield.ymmword = 1;
6735 break;
6736 case 64:
6737 if (vector_size < VSZ512)
6738 goto bad_broadcast;
6739 type.bitfield.zmmword = 1;
6740 break;
6741 default:
6742 goto bad_broadcast;
6743 }
6744
6745 overlap = operand_type_and (type, t->operand_types[op]);
6746 if (t->operand_types[op].bitfield.class == RegSIMD
6747 && t->operand_types[op].bitfield.byte
6748 + t->operand_types[op].bitfield.word
6749 + t->operand_types[op].bitfield.dword
6750 + t->operand_types[op].bitfield.qword > 1)
6751 {
6752 overlap.bitfield.xmmword = 0;
6753 overlap.bitfield.ymmword = 0;
6754 overlap.bitfield.zmmword = 0;
6755 }
6756 if (operand_type_all_zero (&overlap))
6757 goto bad_broadcast;
6758
6759 if (t->opcode_modifier.checkoperandsize)
6760 {
6761 unsigned int j;
6762
6763 type.bitfield.baseindex = 1;
6764 for (j = 0; j < i.operands; ++j)
6765 {
6766 if (j != op
6767 && !operand_type_register_match(i.types[j],
6768 t->operand_types[j],
6769 type,
6770 t->operand_types[op]))
6771 goto bad_broadcast;
6772 }
6773 }
6774 }
6775 /* If broadcast is supported in this instruction, we need to check if
6776 operand of one-element size isn't specified without broadcast. */
6777 else if (t->opcode_modifier.broadcast && i.mem_operands)
6778 {
6779 /* Find memory operand. */
6780 for (op = 0; op < i.operands; op++)
6781 if (i.flags[op] & Operand_Mem)
6782 break;
6783 gas_assert (op < i.operands);
6784 /* Check size of the memory operand. */
6785 if (match_broadcast_size (t, op))
6786 {
6787 i.error = broadcast_needed;
6788 return 1;
6789 }
6790 }
6791 else
6792 op = MAX_OPERANDS - 1; /* Avoid uninitialized variable warning. */
6793
6794 /* Check if requested masking is supported. */
6795 if (i.mask.reg)
6796 {
6797 if (!t->opcode_modifier.masking)
6798 {
6799 i.error = unsupported_masking;
6800 return 1;
6801 }
6802
6803 /* Common rules for masking:
6804 - mask register destinations permit only zeroing-masking, without
6805 that actually being expressed by a {z} operand suffix or EVEX.z,
6806 - memory destinations allow only merging-masking,
6807 - scatter/gather insns (i.e. ones using vSIB) only allow merging-
6808 masking. */
6809 if (i.mask.zeroing
6810 && (t->operand_types[t->operands - 1].bitfield.class == RegMask
6811 || (i.flags[t->operands - 1] & Operand_Mem)
6812 || t->opcode_modifier.sib))
6813 {
6814 i.error = unsupported_masking;
6815 return 1;
6816 }
6817 }
6818
6819 /* Check if masking is applied to dest operand. */
6820 if (i.mask.reg && (i.mask.operand != i.operands - 1))
6821 {
6822 i.error = mask_not_on_destination;
6823 return 1;
6824 }
6825
6826 /* Check RC/SAE. */
6827 if (i.rounding.type != rc_none)
6828 {
6829 if (!t->opcode_modifier.sae
6830 || ((i.rounding.type != saeonly) != t->opcode_modifier.staticrounding)
6831 || i.mem_operands)
6832 {
6833 i.error = unsupported_rc_sae;
6834 return 1;
6835 }
6836
6837 /* Non-EVEX.LIG forms need to have a ZMM register as at least one
6838 operand. */
6839 if (t->opcode_modifier.evex != EVEXLIG)
6840 {
6841 for (op = 0; op < t->operands; ++op)
6842 if (i.types[op].bitfield.zmmword)
6843 break;
6844 if (op >= t->operands)
6845 {
6846 i.error = operand_size_mismatch;
6847 return 1;
6848 }
6849 }
6850 }
6851
6852 /* Check the special Imm4 cases; must be the first operand. */
6853 if (is_cpu (t, CpuXOP) && t->operands == 5)
6854 {
6855 if (i.op[0].imms->X_op != O_constant
6856 || !fits_in_imm4 (i.op[0].imms->X_add_number))
6857 {
6858 i.error = bad_imm4;
6859 return 1;
6860 }
6861
6862 /* Turn off Imm<N> so that update_imm won't complain. */
6863 operand_type_set (&i.types[0], 0);
6864 }
6865
6866 /* Check vector Disp8 operand. */
6867 if (t->opcode_modifier.disp8memshift
6868 && (!t->opcode_modifier.vex
6869 || need_evex_encoding ())
6870 && i.disp_encoding <= disp_encoding_8bit)
6871 {
6872 if (i.broadcast.type || i.broadcast.bytes)
6873 i.memshift = t->opcode_modifier.broadcast - 1;
6874 else if (t->opcode_modifier.disp8memshift != DISP8_SHIFT_VL)
6875 i.memshift = t->opcode_modifier.disp8memshift;
6876 else
6877 {
6878 const i386_operand_type *type = NULL, *fallback = NULL;
6879
6880 i.memshift = 0;
6881 for (op = 0; op < i.operands; op++)
6882 if (i.flags[op] & Operand_Mem)
6883 {
6884 if (t->opcode_modifier.evex == EVEXLIG)
6885 i.memshift = 2 + (i.suffix == QWORD_MNEM_SUFFIX);
6886 else if (t->operand_types[op].bitfield.xmmword
6887 + t->operand_types[op].bitfield.ymmword
6888 + t->operand_types[op].bitfield.zmmword <= 1)
6889 type = &t->operand_types[op];
6890 else if (!i.types[op].bitfield.unspecified)
6891 type = &i.types[op];
6892 else /* Ambiguities get resolved elsewhere. */
6893 fallback = &t->operand_types[op];
6894 }
6895 else if (i.types[op].bitfield.class == RegSIMD
6896 && t->opcode_modifier.evex != EVEXLIG)
6897 {
6898 if (i.types[op].bitfield.zmmword)
6899 i.memshift = 6;
6900 else if (i.types[op].bitfield.ymmword && i.memshift < 5)
6901 i.memshift = 5;
6902 else if (i.types[op].bitfield.xmmword && i.memshift < 4)
6903 i.memshift = 4;
6904 }
6905
6906 if (!type && !i.memshift)
6907 type = fallback;
6908 if (type)
6909 {
6910 if (type->bitfield.zmmword)
6911 i.memshift = 6;
6912 else if (type->bitfield.ymmword)
6913 i.memshift = 5;
6914 else if (type->bitfield.xmmword)
6915 i.memshift = 4;
6916 }
6917
6918 /* For the check in fits_in_disp8(). */
6919 if (i.memshift == 0)
6920 i.memshift = -1;
6921 }
6922
6923 for (op = 0; op < i.operands; op++)
6924 if (operand_type_check (i.types[op], disp)
6925 && i.op[op].disps->X_op == O_constant)
6926 {
6927 if (fits_in_disp8 (i.op[op].disps->X_add_number))
6928 {
6929 i.types[op].bitfield.disp8 = 1;
6930 return 0;
6931 }
6932 i.types[op].bitfield.disp8 = 0;
6933 }
6934 }
6935
6936 i.memshift = 0;
6937
6938 return 0;
6939 }
6940
6941 /* Check if encoding requirements are met by the instruction. */
6942
6943 static int
6944 VEX_check_encoding (const insn_template *t)
6945 {
6946 if (i.vec_encoding == vex_encoding_error)
6947 {
6948 i.error = unsupported;
6949 return 1;
6950 }
6951
6952 /* Vector size restrictions. */
6953 if ((vector_size < VSZ512
6954 && (t->opcode_modifier.evex == EVEX512
6955 || t->opcode_modifier.vsz >= VSZ512))
6956 || (vector_size < VSZ256
6957 && (t->opcode_modifier.evex == EVEX256
6958 || t->opcode_modifier.vex == VEX256
6959 || t->opcode_modifier.vsz >= VSZ256)))
6960 {
6961 i.error = unsupported;
6962 return 1;
6963 }
6964
6965 if (i.vec_encoding == vex_encoding_evex
6966 || i.vec_encoding == vex_encoding_evex512)
6967 {
6968 /* This instruction must be encoded with EVEX prefix. */
6969 if (!is_evex_encoding (t))
6970 {
6971 i.error = unsupported;
6972 return 1;
6973 }
6974 return 0;
6975 }
6976
6977 if (!t->opcode_modifier.vex)
6978 {
6979 /* This instruction template doesn't have VEX prefix. */
6980 if (i.vec_encoding != vex_encoding_default)
6981 {
6982 i.error = unsupported;
6983 return 1;
6984 }
6985 return 0;
6986 }
6987
6988 return 0;
6989 }
6990
6991 /* Helper function for the progress() macro in match_template(). */
6992 static INLINE enum i386_error progress (enum i386_error new,
6993 enum i386_error last,
6994 unsigned int line, unsigned int *line_p)
6995 {
6996 if (line <= *line_p)
6997 return last;
6998 *line_p = line;
6999 return new;
7000 }
7001
7002 static const insn_template *
7003 match_template (char mnem_suffix)
7004 {
7005 /* Points to template once we've found it. */
7006 const insn_template *t;
7007 i386_operand_type overlap0, overlap1, overlap2, overlap3;
7008 i386_operand_type overlap4;
7009 unsigned int found_reverse_match;
7010 i386_operand_type operand_types [MAX_OPERANDS];
7011 int addr_prefix_disp;
7012 unsigned int j, size_match, check_register, errline = __LINE__;
7013 enum i386_error specific_error = number_of_operands_mismatch;
7014 #define progress(err) progress (err, specific_error, __LINE__, &errline)
7015
7016 #if MAX_OPERANDS != 5
7017 # error "MAX_OPERANDS must be 5."
7018 #endif
7019
7020 found_reverse_match = 0;
7021 addr_prefix_disp = -1;
7022
7023 for (t = current_templates->start; t < current_templates->end; t++)
7024 {
7025 addr_prefix_disp = -1;
7026 found_reverse_match = 0;
7027
7028 /* Must have right number of operands. */
7029 if (i.operands != t->operands)
7030 continue;
7031
7032 /* Check processor support. */
7033 specific_error = progress (unsupported);
7034 if (cpu_flags_match (t) != CPU_FLAGS_PERFECT_MATCH)
7035 continue;
7036
7037 /* Check AT&T mnemonic. */
7038 specific_error = progress (unsupported_with_intel_mnemonic);
7039 if (intel_mnemonic && t->opcode_modifier.attmnemonic)
7040 continue;
7041
7042 /* Check AT&T/Intel syntax. */
7043 specific_error = progress (unsupported_syntax);
7044 if ((intel_syntax && t->opcode_modifier.attsyntax)
7045 || (!intel_syntax && t->opcode_modifier.intelsyntax))
7046 continue;
7047
7048 /* Check Intel64/AMD64 ISA. */
7049 switch (isa64)
7050 {
7051 default:
7052 /* Default: Don't accept Intel64. */
7053 if (t->opcode_modifier.isa64 == INTEL64)
7054 continue;
7055 break;
7056 case amd64:
7057 /* -mamd64: Don't accept Intel64 and Intel64 only. */
7058 if (t->opcode_modifier.isa64 >= INTEL64)
7059 continue;
7060 break;
7061 case intel64:
7062 /* -mintel64: Don't accept AMD64. */
7063 if (t->opcode_modifier.isa64 == AMD64 && flag_code == CODE_64BIT)
7064 continue;
7065 break;
7066 }
7067
7068 /* Check the suffix. */
7069 specific_error = progress (invalid_instruction_suffix);
7070 if ((t->opcode_modifier.no_bsuf && mnem_suffix == BYTE_MNEM_SUFFIX)
7071 || (t->opcode_modifier.no_wsuf && mnem_suffix == WORD_MNEM_SUFFIX)
7072 || (t->opcode_modifier.no_lsuf && mnem_suffix == LONG_MNEM_SUFFIX)
7073 || (t->opcode_modifier.no_ssuf && mnem_suffix == SHORT_MNEM_SUFFIX)
7074 || (t->opcode_modifier.no_qsuf && mnem_suffix == QWORD_MNEM_SUFFIX))
7075 continue;
7076
7077 specific_error = progress (operand_size_mismatch);
7078 size_match = operand_size_match (t);
7079 if (!size_match)
7080 continue;
7081
7082 /* This is intentionally not
7083
7084 if (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE))
7085
7086 as the case of a missing * on the operand is accepted (perhaps with
7087 a warning, issued further down). */
7088 specific_error = progress (operand_type_mismatch);
7089 if (i.jumpabsolute && t->opcode_modifier.jump != JUMP_ABSOLUTE)
7090 continue;
7091
7092 /* In Intel syntax, normally we can check for memory operand size when
7093 there is no mnemonic suffix. But jmp and call have 2 different
7094 encodings with Dword memory operand size. Skip the "near" one
7095 (permitting a register operand) when "far" was requested. */
7096 if (i.far_branch
7097 && t->opcode_modifier.jump == JUMP_ABSOLUTE
7098 && t->operand_types[0].bitfield.class == Reg)
7099 continue;
7100
7101 for (j = 0; j < MAX_OPERANDS; j++)
7102 operand_types[j] = t->operand_types[j];
7103
7104 /* In general, don't allow 32-bit operands on pre-386. */
7105 specific_error = progress (mnem_suffix ? invalid_instruction_suffix
7106 : operand_size_mismatch);
7107 j = i.imm_operands + (t->operands > i.imm_operands + 1);
7108 if (i.suffix == LONG_MNEM_SUFFIX
7109 && !cpu_arch_flags.bitfield.cpui386
7110 && (intel_syntax
7111 ? (t->opcode_modifier.mnemonicsize != IGNORESIZE
7112 && !intel_float_operand (insn_name (t)))
7113 : intel_float_operand (insn_name (t)) != 2)
7114 && (t->operands == i.imm_operands
7115 || (operand_types[i.imm_operands].bitfield.class != RegMMX
7116 && operand_types[i.imm_operands].bitfield.class != RegSIMD
7117 && operand_types[i.imm_operands].bitfield.class != RegMask)
7118 || (operand_types[j].bitfield.class != RegMMX
7119 && operand_types[j].bitfield.class != RegSIMD
7120 && operand_types[j].bitfield.class != RegMask))
7121 && !t->opcode_modifier.sib)
7122 continue;
7123
7124 /* Do not verify operands when there are none. */
7125 if (!t->operands)
7126 {
7127 if (VEX_check_encoding (t))
7128 {
7129 specific_error = progress (i.error);
7130 continue;
7131 }
7132
7133 /* We've found a match; break out of loop. */
7134 break;
7135 }
7136
7137 if (!t->opcode_modifier.jump
7138 || t->opcode_modifier.jump == JUMP_ABSOLUTE)
7139 {
7140 /* There should be only one Disp operand. */
7141 for (j = 0; j < MAX_OPERANDS; j++)
7142 if (operand_type_check (operand_types[j], disp))
7143 break;
7144 if (j < MAX_OPERANDS)
7145 {
7146 bool override = (i.prefix[ADDR_PREFIX] != 0);
7147
7148 addr_prefix_disp = j;
7149
7150 /* Address size prefix will turn Disp64 operand into Disp32 and
7151 Disp32/Disp16 one into Disp16/Disp32 respectively. */
7152 switch (flag_code)
7153 {
7154 case CODE_16BIT:
7155 override = !override;
7156 /* Fall through. */
7157 case CODE_32BIT:
7158 if (operand_types[j].bitfield.disp32
7159 && operand_types[j].bitfield.disp16)
7160 {
7161 operand_types[j].bitfield.disp16 = override;
7162 operand_types[j].bitfield.disp32 = !override;
7163 }
7164 gas_assert (!operand_types[j].bitfield.disp64);
7165 break;
7166
7167 case CODE_64BIT:
7168 if (operand_types[j].bitfield.disp64)
7169 {
7170 gas_assert (!operand_types[j].bitfield.disp32);
7171 operand_types[j].bitfield.disp32 = override;
7172 operand_types[j].bitfield.disp64 = !override;
7173 }
7174 operand_types[j].bitfield.disp16 = 0;
7175 break;
7176 }
7177 }
7178 }
7179
7180 /* We check register size if needed. */
7181 if (t->opcode_modifier.checkoperandsize)
7182 {
7183 check_register = (1 << t->operands) - 1;
7184 if (i.broadcast.type || i.broadcast.bytes)
7185 check_register &= ~(1 << i.broadcast.operand);
7186 }
7187 else
7188 check_register = 0;
7189
7190 overlap0 = operand_type_and (i.types[0], operand_types[0]);
7191 switch (t->operands)
7192 {
7193 case 1:
7194 if (!operand_type_match (overlap0, i.types[0]))
7195 continue;
7196
7197 /* Allow the ModR/M encoding to be requested by using the {load} or
7198 {store} pseudo prefix on an applicable insn. */
7199 if (!t->opcode_modifier.modrm
7200 && i.reg_operands == 1
7201 && ((i.dir_encoding == dir_encoding_load
7202 && t->mnem_off != MN_pop)
7203 || (i.dir_encoding == dir_encoding_store
7204 && t->mnem_off != MN_push))
7205 /* Avoid BSWAP. */
7206 && t->mnem_off != MN_bswap)
7207 continue;
7208 break;
7209
7210 case 2:
7211 /* xchg %eax, %eax is a special case. It is an alias for nop
7212 only in 32bit mode and we can use opcode 0x90. In 64bit
7213 mode, we can't use 0x90 for xchg %eax, %eax since it should
7214 zero-extend %eax to %rax. */
7215 if (t->base_opcode == 0x90
7216 && t->opcode_space == SPACE_BASE)
7217 {
7218 if (flag_code == CODE_64BIT
7219 && i.types[0].bitfield.instance == Accum
7220 && i.types[0].bitfield.dword
7221 && i.types[1].bitfield.instance == Accum)
7222 continue;
7223
7224 /* Allow the ModR/M encoding to be requested by using the
7225 {load} or {store} pseudo prefix. */
7226 if (i.dir_encoding == dir_encoding_load
7227 || i.dir_encoding == dir_encoding_store)
7228 continue;
7229 }
7230
7231 if (t->base_opcode == MOV_AX_DISP32
7232 && t->opcode_space == SPACE_BASE
7233 && t->mnem_off != MN_movabs)
7234 {
7235 /* Force 0x8b encoding for "mov foo@GOT, %eax". */
7236 if (i.reloc[0] == BFD_RELOC_386_GOT32)
7237 continue;
7238
7239 /* xrelease mov %eax, <disp> is another special case. It must not
7240 match the accumulator-only encoding of mov. */
7241 if (i.hle_prefix)
7242 continue;
7243
7244 /* Allow the ModR/M encoding to be requested by using a suitable
7245 {load} or {store} pseudo prefix. */
7246 if (i.dir_encoding == (i.types[0].bitfield.instance == Accum
7247 ? dir_encoding_store
7248 : dir_encoding_load)
7249 && !i.types[0].bitfield.disp64
7250 && !i.types[1].bitfield.disp64)
7251 continue;
7252 }
7253
7254 /* Allow the ModR/M encoding to be requested by using the {load} or
7255 {store} pseudo prefix on an applicable insn. */
7256 if (!t->opcode_modifier.modrm
7257 && i.reg_operands == 1
7258 && i.imm_operands == 1
7259 && (i.dir_encoding == dir_encoding_load
7260 || i.dir_encoding == dir_encoding_store)
7261 && t->opcode_space == SPACE_BASE)
7262 {
7263 if (t->base_opcode == 0xb0 /* mov $imm, %reg */
7264 && i.dir_encoding == dir_encoding_store)
7265 continue;
7266
7267 if ((t->base_opcode | 0x38) == 0x3c /* <alu> $imm, %acc */
7268 && (t->base_opcode != 0x3c /* cmp $imm, %acc */
7269 || i.dir_encoding == dir_encoding_load))
7270 continue;
7271
7272 if (t->base_opcode == 0xa8 /* test $imm, %acc */
7273 && i.dir_encoding == dir_encoding_load)
7274 continue;
7275 }
7276 /* Fall through. */
7277
7278 case 3:
7279 if (!(size_match & MATCH_STRAIGHT))
7280 goto check_reverse;
7281 /* Reverse direction of operands if swapping is possible in the first
7282 place (operands need to be symmetric) and
7283 - the load form is requested, and the template is a store form,
7284 - the store form is requested, and the template is a load form,
7285 - the non-default (swapped) form is requested. */
7286 overlap1 = operand_type_and (operand_types[0], operand_types[1]);
7287 if (t->opcode_modifier.d && i.reg_operands == i.operands
7288 && !operand_type_all_zero (&overlap1))
7289 switch (i.dir_encoding)
7290 {
7291 case dir_encoding_load:
7292 if (operand_type_check (operand_types[i.operands - 1], anymem)
7293 || t->opcode_modifier.regmem)
7294 goto check_reverse;
7295 break;
7296
7297 case dir_encoding_store:
7298 if (!operand_type_check (operand_types[i.operands - 1], anymem)
7299 && !t->opcode_modifier.regmem)
7300 goto check_reverse;
7301 break;
7302
7303 case dir_encoding_swap:
7304 goto check_reverse;
7305
7306 case dir_encoding_default:
7307 break;
7308 }
7309 /* If we want store form, we skip the current load. */
7310 if ((i.dir_encoding == dir_encoding_store
7311 || i.dir_encoding == dir_encoding_swap)
7312 && i.mem_operands == 0
7313 && t->opcode_modifier.load)
7314 continue;
7315 /* Fall through. */
7316 case 4:
7317 case 5:
7318 overlap1 = operand_type_and (i.types[1], operand_types[1]);
7319 if (!operand_type_match (overlap0, i.types[0])
7320 || !operand_type_match (overlap1, i.types[1])
7321 || ((check_register & 3) == 3
7322 && !operand_type_register_match (i.types[0],
7323 operand_types[0],
7324 i.types[1],
7325 operand_types[1])))
7326 {
7327 specific_error = progress (i.error);
7328
7329 /* Check if other direction is valid ... */
7330 if (!t->opcode_modifier.d)
7331 continue;
7332
7333 check_reverse:
7334 if (!(size_match & MATCH_REVERSE))
7335 continue;
7336 /* Try reversing direction of operands. */
7337 j = is_cpu (t, CpuFMA4)
7338 || is_cpu (t, CpuXOP) ? 1 : i.operands - 1;
7339 overlap0 = operand_type_and (i.types[0], operand_types[j]);
7340 overlap1 = operand_type_and (i.types[j], operand_types[0]);
7341 overlap2 = operand_type_and (i.types[1], operand_types[1]);
7342 gas_assert (t->operands != 3 || !check_register);
7343 if (!operand_type_match (overlap0, i.types[0])
7344 || !operand_type_match (overlap1, i.types[j])
7345 || (t->operands == 3
7346 && !operand_type_match (overlap2, i.types[1]))
7347 || (check_register
7348 && !operand_type_register_match (i.types[0],
7349 operand_types[j],
7350 i.types[j],
7351 operand_types[0])))
7352 {
7353 /* Does not match either direction. */
7354 specific_error = progress (i.error);
7355 continue;
7356 }
7357 /* found_reverse_match holds which variant of D
7358 we've found. */
7359 if (!t->opcode_modifier.d)
7360 found_reverse_match = 0;
7361 else if (operand_types[0].bitfield.tbyte)
7362 {
7363 if (t->opcode_modifier.operandconstraint != UGH)
7364 found_reverse_match = Opcode_FloatD;
7365 else
7366 found_reverse_match = ~0;
7367 /* FSUB{,R} and FDIV{,R} may need a 2nd bit flipped. */
7368 if ((t->extension_opcode & 4)
7369 && (intel_syntax || intel_mnemonic))
7370 found_reverse_match |= Opcode_FloatR;
7371 }
7372 else if (is_cpu (t, CpuFMA4) || is_cpu (t, CpuXOP))
7373 {
7374 found_reverse_match = Opcode_VexW;
7375 goto check_operands_345;
7376 }
7377 else if (t->opcode_space != SPACE_BASE
7378 && (t->opcode_space != SPACE_0F
7379 /* MOV to/from CR/DR/TR, as an exception, follow
7380 the base opcode space encoding model. */
7381 || (t->base_opcode | 7) != 0x27))
7382 found_reverse_match = (t->base_opcode & 0xee) != 0x6e
7383 ? Opcode_ExtD : Opcode_SIMD_IntD;
7384 else if (!t->opcode_modifier.commutative)
7385 found_reverse_match = Opcode_D;
7386 else
7387 found_reverse_match = ~0;
7388 }
7389 else
7390 {
7391 /* Found a forward 2 operand match here. */
7392 check_operands_345:
7393 switch (t->operands)
7394 {
7395 case 5:
7396 overlap4 = operand_type_and (i.types[4], operand_types[4]);
7397 if (!operand_type_match (overlap4, i.types[4])
7398 || !operand_type_register_match (i.types[3],
7399 operand_types[3],
7400 i.types[4],
7401 operand_types[4]))
7402 {
7403 specific_error = progress (i.error);
7404 continue;
7405 }
7406 /* Fall through. */
7407 case 4:
7408 overlap3 = operand_type_and (i.types[3], operand_types[3]);
7409 if (!operand_type_match (overlap3, i.types[3])
7410 || ((check_register & 0xa) == 0xa
7411 && !operand_type_register_match (i.types[1],
7412 operand_types[1],
7413 i.types[3],
7414 operand_types[3]))
7415 || ((check_register & 0xc) == 0xc
7416 && !operand_type_register_match (i.types[2],
7417 operand_types[2],
7418 i.types[3],
7419 operand_types[3])))
7420 {
7421 specific_error = progress (i.error);
7422 continue;
7423 }
7424 /* Fall through. */
7425 case 3:
7426 overlap2 = operand_type_and (i.types[2], operand_types[2]);
7427 if (!operand_type_match (overlap2, i.types[2])
7428 || ((check_register & 5) == 5
7429 && !operand_type_register_match (i.types[0],
7430 operand_types[0],
7431 i.types[2],
7432 operand_types[2]))
7433 || ((check_register & 6) == 6
7434 && !operand_type_register_match (i.types[1],
7435 operand_types[1],
7436 i.types[2],
7437 operand_types[2])))
7438 {
7439 specific_error = progress (i.error);
7440 continue;
7441 }
7442 break;
7443 }
7444 }
7445 /* Found either forward/reverse 2, 3 or 4 operand match here:
7446 slip through to break. */
7447 }
7448
7449 /* Check if VEX/EVEX encoding requirements can be satisfied. */
7450 if (VEX_check_encoding (t))
7451 {
7452 specific_error = progress (i.error);
7453 continue;
7454 }
7455
7456 /* Check if vector operands are valid. */
7457 if (check_VecOperands (t))
7458 {
7459 specific_error = progress (i.error);
7460 continue;
7461 }
7462
7463 /* Check whether to use the shorter VEX encoding for certain insns where
7464 the EVEX enconding comes first in the table. This requires the respective
7465 AVX-* feature to be explicitly enabled. */
7466 if (t == current_templates->start
7467 && t->opcode_modifier.disp8memshift
7468 && !t->opcode_modifier.vex
7469 && !need_evex_encoding ()
7470 && t + 1 < current_templates->end
7471 && t[1].opcode_modifier.vex)
7472 {
7473 i386_cpu_flags cpu;
7474 unsigned int memshift = i.memshift;
7475
7476 i.memshift = 0;
7477 cpu = cpu_flags_and (cpu_flags_from_attr (t[1].cpu), cpu_arch_isa_flags);
7478 if (!cpu_flags_all_zero (&cpu)
7479 && (!i.types[0].bitfield.disp8
7480 || !operand_type_check (i.types[0], disp)
7481 || i.op[0].disps->X_op != O_constant
7482 || fits_in_disp8 (i.op[0].disps->X_add_number)))
7483 {
7484 specific_error = progress (internal_error);
7485 continue;
7486 }
7487 i.memshift = memshift;
7488 }
7489
7490 /* We've found a match; break out of loop. */
7491 break;
7492 }
7493
7494 #undef progress
7495
7496 if (t == current_templates->end)
7497 {
7498 /* We found no match. */
7499 i.error = specific_error;
7500 return NULL;
7501 }
7502
7503 if (!quiet_warnings)
7504 {
7505 if (!intel_syntax
7506 && (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE)))
7507 as_warn (_("indirect %s without `*'"), insn_name (t));
7508
7509 if (t->opcode_modifier.isprefix
7510 && t->opcode_modifier.mnemonicsize == IGNORESIZE)
7511 {
7512 /* Warn them that a data or address size prefix doesn't
7513 affect assembly of the next line of code. */
7514 as_warn (_("stand-alone `%s' prefix"), insn_name (t));
7515 }
7516 }
7517
7518 /* Copy the template we found. */
7519 install_template (t);
7520
7521 if (addr_prefix_disp != -1)
7522 i.tm.operand_types[addr_prefix_disp]
7523 = operand_types[addr_prefix_disp];
7524
7525 switch (found_reverse_match)
7526 {
7527 case 0:
7528 break;
7529
7530 case Opcode_FloatR:
7531 case Opcode_FloatR | Opcode_FloatD:
7532 i.tm.extension_opcode ^= Opcode_FloatR >> 3;
7533 found_reverse_match &= Opcode_FloatD;
7534
7535 /* Fall through. */
7536 default:
7537 /* If we found a reverse match we must alter the opcode direction
7538 bit and clear/flip the regmem modifier one. found_reverse_match
7539 holds bits to change (different for int & float insns). */
7540
7541 i.tm.base_opcode ^= found_reverse_match;
7542
7543 /* Certain SIMD insns have their load forms specified in the opcode
7544 table, and hence we need to _set_ RegMem instead of clearing it.
7545 We need to avoid setting the bit though on insns like KMOVW. */
7546 i.tm.opcode_modifier.regmem
7547 = i.tm.opcode_modifier.modrm && i.tm.opcode_modifier.d
7548 && i.tm.operands > 2U - i.tm.opcode_modifier.sse2avx
7549 && !i.tm.opcode_modifier.regmem;
7550
7551 /* Fall through. */
7552 case ~0:
7553 i.tm.operand_types[0] = operand_types[i.operands - 1];
7554 i.tm.operand_types[i.operands - 1] = operand_types[0];
7555 break;
7556
7557 case Opcode_VexW:
7558 /* Only the first two register operands need reversing, alongside
7559 flipping VEX.W. */
7560 i.tm.opcode_modifier.vexw ^= VEXW0 ^ VEXW1;
7561
7562 j = i.tm.operand_types[0].bitfield.imm8;
7563 i.tm.operand_types[j] = operand_types[j + 1];
7564 i.tm.operand_types[j + 1] = operand_types[j];
7565 break;
7566 }
7567
7568 /* This pattern aims to put the unusually placed imm operand to a usual
7569 place. The constraints are currently only adapted to uwrmsr, and may
7570 need further tweaking when new similar instructions become available. */
7571 if (i.imm_operands && i.imm_operands < i.operands
7572 && operand_type_check (operand_types[i.operands - 1], imm))
7573 {
7574 i.tm.operand_types[0] = operand_types[i.operands - 1];
7575 i.tm.operand_types[i.operands - 1] = operand_types[0];
7576 swap_2_operands(0, i.operands - 1);
7577 }
7578
7579 return t;
7580 }
7581
7582 static int
7583 check_string (void)
7584 {
7585 unsigned int es_op = i.tm.opcode_modifier.isstring - IS_STRING_ES_OP0;
7586 unsigned int op = i.tm.operand_types[0].bitfield.baseindex ? es_op : 0;
7587
7588 if (i.seg[op] != NULL && i.seg[op] != reg_es)
7589 {
7590 as_bad (_("`%s' operand %u must use `%ses' segment"),
7591 insn_name (&i.tm),
7592 intel_syntax ? i.tm.operands - es_op : es_op + 1,
7593 register_prefix);
7594 return 0;
7595 }
7596
7597 /* There's only ever one segment override allowed per instruction.
7598 This instruction possibly has a legal segment override on the
7599 second operand, so copy the segment to where non-string
7600 instructions store it, allowing common code. */
7601 i.seg[op] = i.seg[1];
7602
7603 return 1;
7604 }
7605
7606 static int
7607 process_suffix (void)
7608 {
7609 bool is_movx = false;
7610
7611 /* If matched instruction specifies an explicit instruction mnemonic
7612 suffix, use it. */
7613 if (i.tm.opcode_modifier.size == SIZE16)
7614 i.suffix = WORD_MNEM_SUFFIX;
7615 else if (i.tm.opcode_modifier.size == SIZE32)
7616 i.suffix = LONG_MNEM_SUFFIX;
7617 else if (i.tm.opcode_modifier.size == SIZE64)
7618 i.suffix = QWORD_MNEM_SUFFIX;
7619 else if (i.reg_operands
7620 && (i.operands > 1 || i.types[0].bitfield.class == Reg)
7621 && i.tm.opcode_modifier.operandconstraint != ADDR_PREFIX_OP_REG)
7622 {
7623 unsigned int numop = i.operands;
7624
7625 /* MOVSX/MOVZX */
7626 is_movx = (i.tm.opcode_space == SPACE_0F
7627 && (i.tm.base_opcode | 8) == 0xbe)
7628 || (i.tm.opcode_space == SPACE_BASE
7629 && i.tm.base_opcode == 0x63
7630 && is_cpu (&i.tm, Cpu64));
7631
7632 /* movsx/movzx want only their source operand considered here, for the
7633 ambiguity checking below. The suffix will be replaced afterwards
7634 to represent the destination (register). */
7635 if (is_movx && (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63))
7636 --i.operands;
7637
7638 /* crc32 needs REX.W set regardless of suffix / source operand size. */
7639 if (i.tm.mnem_off == MN_crc32 && i.tm.operand_types[1].bitfield.qword)
7640 i.rex |= REX_W;
7641
7642 /* If there's no instruction mnemonic suffix we try to invent one
7643 based on GPR operands. */
7644 if (!i.suffix)
7645 {
7646 /* We take i.suffix from the last register operand specified,
7647 Destination register type is more significant than source
7648 register type. crc32 in SSE4.2 prefers source register
7649 type. */
7650 unsigned int op = i.tm.mnem_off == MN_crc32 ? 1 : i.operands;
7651
7652 while (op--)
7653 if (i.tm.operand_types[op].bitfield.instance == InstanceNone
7654 || i.tm.operand_types[op].bitfield.instance == Accum)
7655 {
7656 if (i.types[op].bitfield.class != Reg)
7657 continue;
7658 if (i.types[op].bitfield.byte)
7659 i.suffix = BYTE_MNEM_SUFFIX;
7660 else if (i.types[op].bitfield.word)
7661 i.suffix = WORD_MNEM_SUFFIX;
7662 else if (i.types[op].bitfield.dword)
7663 i.suffix = LONG_MNEM_SUFFIX;
7664 else if (i.types[op].bitfield.qword)
7665 i.suffix = QWORD_MNEM_SUFFIX;
7666 else
7667 continue;
7668 break;
7669 }
7670
7671 /* As an exception, movsx/movzx silently default to a byte source
7672 in AT&T mode. */
7673 if (is_movx && i.tm.opcode_modifier.w && !i.suffix && !intel_syntax)
7674 i.suffix = BYTE_MNEM_SUFFIX;
7675 }
7676 else if (i.suffix == BYTE_MNEM_SUFFIX)
7677 {
7678 if (!check_byte_reg ())
7679 return 0;
7680 }
7681 else if (i.suffix == LONG_MNEM_SUFFIX)
7682 {
7683 if (!check_long_reg ())
7684 return 0;
7685 }
7686 else if (i.suffix == QWORD_MNEM_SUFFIX)
7687 {
7688 if (!check_qword_reg ())
7689 return 0;
7690 }
7691 else if (i.suffix == WORD_MNEM_SUFFIX)
7692 {
7693 if (!check_word_reg ())
7694 return 0;
7695 }
7696 else if (intel_syntax
7697 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
7698 /* Do nothing if the instruction is going to ignore the prefix. */
7699 ;
7700 else
7701 abort ();
7702
7703 /* Undo the movsx/movzx change done above. */
7704 i.operands = numop;
7705 }
7706 else if (i.tm.opcode_modifier.mnemonicsize == DEFAULTSIZE
7707 && !i.suffix)
7708 {
7709 i.suffix = stackop_size;
7710 if (stackop_size == LONG_MNEM_SUFFIX)
7711 {
7712 /* stackop_size is set to LONG_MNEM_SUFFIX for the
7713 .code16gcc directive to support 16-bit mode with
7714 32-bit address. For IRET without a suffix, generate
7715 16-bit IRET (opcode 0xcf) to return from an interrupt
7716 handler. */
7717 if (i.tm.base_opcode == 0xcf)
7718 {
7719 i.suffix = WORD_MNEM_SUFFIX;
7720 as_warn (_("generating 16-bit `iret' for .code16gcc directive"));
7721 }
7722 /* Warn about changed behavior for segment register push/pop. */
7723 else if ((i.tm.base_opcode | 1) == 0x07)
7724 as_warn (_("generating 32-bit `%s', unlike earlier gas versions"),
7725 insn_name (&i.tm));
7726 }
7727 }
7728 else if (!i.suffix
7729 && (i.tm.opcode_modifier.jump == JUMP_ABSOLUTE
7730 || i.tm.opcode_modifier.jump == JUMP_BYTE
7731 || i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT
7732 || (i.tm.opcode_space == SPACE_0F
7733 && i.tm.base_opcode == 0x01 /* [ls][gi]dt */
7734 && i.tm.extension_opcode <= 3)))
7735 {
7736 switch (flag_code)
7737 {
7738 case CODE_64BIT:
7739 if (!i.tm.opcode_modifier.no_qsuf)
7740 {
7741 if (i.tm.opcode_modifier.jump == JUMP_BYTE
7742 || i.tm.opcode_modifier.no_lsuf)
7743 i.suffix = QWORD_MNEM_SUFFIX;
7744 break;
7745 }
7746 /* Fall through. */
7747 case CODE_32BIT:
7748 if (!i.tm.opcode_modifier.no_lsuf)
7749 i.suffix = LONG_MNEM_SUFFIX;
7750 break;
7751 case CODE_16BIT:
7752 if (!i.tm.opcode_modifier.no_wsuf)
7753 i.suffix = WORD_MNEM_SUFFIX;
7754 break;
7755 }
7756 }
7757
7758 if (!i.suffix
7759 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
7760 /* Also cover lret/retf/iret in 64-bit mode. */
7761 || (flag_code == CODE_64BIT
7762 && !i.tm.opcode_modifier.no_lsuf
7763 && !i.tm.opcode_modifier.no_qsuf))
7764 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
7765 /* Explicit sizing prefixes are assumed to disambiguate insns. */
7766 && !i.prefix[DATA_PREFIX] && !(i.prefix[REX_PREFIX] & REX_W)
7767 /* Accept FLDENV et al without suffix. */
7768 && (i.tm.opcode_modifier.no_ssuf || i.tm.opcode_modifier.floatmf))
7769 {
7770 unsigned int suffixes, evex = 0;
7771
7772 suffixes = !i.tm.opcode_modifier.no_bsuf;
7773 if (!i.tm.opcode_modifier.no_wsuf)
7774 suffixes |= 1 << 1;
7775 if (!i.tm.opcode_modifier.no_lsuf)
7776 suffixes |= 1 << 2;
7777 if (!i.tm.opcode_modifier.no_ssuf)
7778 suffixes |= 1 << 4;
7779 if (flag_code == CODE_64BIT && !i.tm.opcode_modifier.no_qsuf)
7780 suffixes |= 1 << 5;
7781
7782 /* For [XYZ]MMWORD operands inspect operand sizes. While generally
7783 also suitable for AT&T syntax mode, it was requested that this be
7784 restricted to just Intel syntax. */
7785 if (intel_syntax && is_any_vex_encoding (&i.tm)
7786 && !i.broadcast.type && !i.broadcast.bytes)
7787 {
7788 unsigned int op;
7789
7790 for (op = 0; op < i.tm.operands; ++op)
7791 {
7792 if (vector_size < VSZ512)
7793 {
7794 i.tm.operand_types[op].bitfield.zmmword = 0;
7795 if (vector_size < VSZ256)
7796 {
7797 i.tm.operand_types[op].bitfield.ymmword = 0;
7798 if (i.tm.operand_types[op].bitfield.xmmword
7799 && (i.tm.opcode_modifier.evex == EVEXDYN
7800 || (!i.tm.opcode_modifier.evex
7801 && is_evex_encoding (&i.tm))))
7802 i.tm.opcode_modifier.evex = EVEX128;
7803 }
7804 else if (i.tm.operand_types[op].bitfield.ymmword
7805 && !i.tm.operand_types[op].bitfield.xmmword
7806 && (i.tm.opcode_modifier.evex == EVEXDYN
7807 || (!i.tm.opcode_modifier.evex
7808 && is_evex_encoding (&i.tm))))
7809 i.tm.opcode_modifier.evex = EVEX256;
7810 }
7811 else if (is_evex_encoding (&i.tm)
7812 && !cpu_arch_flags.bitfield.cpuavx512vl)
7813 {
7814 if (i.tm.operand_types[op].bitfield.ymmword)
7815 i.tm.operand_types[op].bitfield.xmmword = 0;
7816 if (i.tm.operand_types[op].bitfield.zmmword)
7817 i.tm.operand_types[op].bitfield.ymmword = 0;
7818 if (!i.tm.opcode_modifier.evex
7819 || i.tm.opcode_modifier.evex == EVEXDYN)
7820 i.tm.opcode_modifier.evex = EVEX512;
7821 }
7822
7823 if (i.tm.operand_types[op].bitfield.xmmword
7824 + i.tm.operand_types[op].bitfield.ymmword
7825 + i.tm.operand_types[op].bitfield.zmmword < 2)
7826 continue;
7827
7828 /* Any properly sized operand disambiguates the insn. */
7829 if (i.types[op].bitfield.xmmword
7830 || i.types[op].bitfield.ymmword
7831 || i.types[op].bitfield.zmmword)
7832 {
7833 suffixes &= ~(7 << 6);
7834 evex = 0;
7835 break;
7836 }
7837
7838 if ((i.flags[op] & Operand_Mem)
7839 && i.tm.operand_types[op].bitfield.unspecified)
7840 {
7841 if (i.tm.operand_types[op].bitfield.xmmword)
7842 suffixes |= 1 << 6;
7843 if (i.tm.operand_types[op].bitfield.ymmword)
7844 suffixes |= 1 << 7;
7845 if (i.tm.operand_types[op].bitfield.zmmword)
7846 suffixes |= 1 << 8;
7847 if (is_evex_encoding (&i.tm))
7848 evex = EVEX512;
7849 }
7850 }
7851 }
7852
7853 /* Are multiple suffixes / operand sizes allowed? */
7854 if (suffixes & (suffixes - 1))
7855 {
7856 if (intel_syntax
7857 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
7858 || operand_check == check_error))
7859 {
7860 as_bad (_("ambiguous operand size for `%s'"), insn_name (&i.tm));
7861 return 0;
7862 }
7863 if (operand_check == check_error)
7864 {
7865 as_bad (_("no instruction mnemonic suffix given and "
7866 "no register operands; can't size `%s'"), insn_name (&i.tm));
7867 return 0;
7868 }
7869 if (operand_check == check_warning)
7870 as_warn (_("%s; using default for `%s'"),
7871 intel_syntax
7872 ? _("ambiguous operand size")
7873 : _("no instruction mnemonic suffix given and "
7874 "no register operands"),
7875 insn_name (&i.tm));
7876
7877 if (i.tm.opcode_modifier.floatmf)
7878 i.suffix = SHORT_MNEM_SUFFIX;
7879 else if (is_movx)
7880 /* handled below */;
7881 else if (evex)
7882 i.tm.opcode_modifier.evex = evex;
7883 else if (flag_code == CODE_16BIT)
7884 i.suffix = WORD_MNEM_SUFFIX;
7885 else if (!i.tm.opcode_modifier.no_lsuf)
7886 i.suffix = LONG_MNEM_SUFFIX;
7887 else
7888 i.suffix = QWORD_MNEM_SUFFIX;
7889 }
7890 }
7891
7892 if (is_movx)
7893 {
7894 /* In Intel syntax, movsx/movzx must have a "suffix" (checked above).
7895 In AT&T syntax, if there is no suffix (warned about above), the default
7896 will be byte extension. */
7897 if (i.tm.opcode_modifier.w && i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
7898 i.tm.base_opcode |= 1;
7899
7900 /* For further processing, the suffix should represent the destination
7901 (register). This is already the case when one was used with
7902 mov[sz][bw]*, but we need to replace it for mov[sz]x, or if there was
7903 no suffix to begin with. */
7904 if (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63 || !i.suffix)
7905 {
7906 if (i.types[1].bitfield.word)
7907 i.suffix = WORD_MNEM_SUFFIX;
7908 else if (i.types[1].bitfield.qword)
7909 i.suffix = QWORD_MNEM_SUFFIX;
7910 else
7911 i.suffix = LONG_MNEM_SUFFIX;
7912
7913 i.tm.opcode_modifier.w = 0;
7914 }
7915 }
7916
7917 if (!i.tm.opcode_modifier.modrm && i.reg_operands && i.tm.operands < 3)
7918 i.short_form = (i.tm.operand_types[0].bitfield.class == Reg)
7919 != (i.tm.operand_types[1].bitfield.class == Reg);
7920
7921 /* Change the opcode based on the operand size given by i.suffix. */
7922 switch (i.suffix)
7923 {
7924 /* Size floating point instruction. */
7925 case LONG_MNEM_SUFFIX:
7926 if (i.tm.opcode_modifier.floatmf)
7927 {
7928 i.tm.base_opcode ^= 4;
7929 break;
7930 }
7931 /* fall through */
7932 case WORD_MNEM_SUFFIX:
7933 case QWORD_MNEM_SUFFIX:
7934 /* It's not a byte, select word/dword operation. */
7935 if (i.tm.opcode_modifier.w)
7936 {
7937 if (i.short_form)
7938 i.tm.base_opcode |= 8;
7939 else
7940 i.tm.base_opcode |= 1;
7941 }
7942 /* fall through */
7943 case SHORT_MNEM_SUFFIX:
7944 /* Now select between word & dword operations via the operand
7945 size prefix, except for instructions that will ignore this
7946 prefix anyway. */
7947 if (i.suffix != QWORD_MNEM_SUFFIX
7948 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
7949 && !i.tm.opcode_modifier.floatmf
7950 && !is_any_vex_encoding (&i.tm)
7951 && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
7952 || (flag_code == CODE_64BIT
7953 && i.tm.opcode_modifier.jump == JUMP_BYTE)))
7954 {
7955 unsigned int prefix = DATA_PREFIX_OPCODE;
7956
7957 if (i.tm.opcode_modifier.jump == JUMP_BYTE) /* jcxz, loop */
7958 prefix = ADDR_PREFIX_OPCODE;
7959
7960 if (!add_prefix (prefix))
7961 return 0;
7962 }
7963
7964 /* Set mode64 for an operand. */
7965 if (i.suffix == QWORD_MNEM_SUFFIX
7966 && flag_code == CODE_64BIT
7967 && !i.tm.opcode_modifier.norex64
7968 && !i.tm.opcode_modifier.vexw
7969 /* Special case for xchg %rax,%rax. It is NOP and doesn't
7970 need rex64. */
7971 && ! (i.operands == 2
7972 && i.tm.base_opcode == 0x90
7973 && i.tm.opcode_space == SPACE_BASE
7974 && i.types[0].bitfield.instance == Accum
7975 && i.types[0].bitfield.qword
7976 && i.types[1].bitfield.instance == Accum))
7977 i.rex |= REX_W;
7978
7979 break;
7980
7981 case 0:
7982 /* Select word/dword/qword operation with explicit data sizing prefix
7983 when there are no suitable register operands. */
7984 if (i.tm.opcode_modifier.w
7985 && (i.prefix[DATA_PREFIX] || (i.prefix[REX_PREFIX] & REX_W))
7986 && (!i.reg_operands
7987 || (i.reg_operands == 1
7988 /* ShiftCount */
7989 && (i.tm.operand_types[0].bitfield.instance == RegC
7990 /* InOutPortReg */
7991 || i.tm.operand_types[0].bitfield.instance == RegD
7992 || i.tm.operand_types[1].bitfield.instance == RegD
7993 || i.tm.mnem_off == MN_crc32))))
7994 i.tm.base_opcode |= 1;
7995 break;
7996 }
7997
7998 if (i.tm.opcode_modifier.operandconstraint == ADDR_PREFIX_OP_REG)
7999 {
8000 gas_assert (!i.suffix);
8001 gas_assert (i.reg_operands);
8002
8003 if (i.tm.operand_types[0].bitfield.instance == Accum
8004 || i.operands == 1)
8005 {
8006 /* The address size override prefix changes the size of the
8007 first operand. */
8008 if (flag_code == CODE_64BIT
8009 && i.op[0].regs->reg_type.bitfield.word)
8010 {
8011 as_bad (_("16-bit addressing unavailable for `%s'"),
8012 insn_name (&i.tm));
8013 return 0;
8014 }
8015
8016 if ((flag_code == CODE_32BIT
8017 ? i.op[0].regs->reg_type.bitfield.word
8018 : i.op[0].regs->reg_type.bitfield.dword)
8019 && !add_prefix (ADDR_PREFIX_OPCODE))
8020 return 0;
8021 }
8022 else
8023 {
8024 /* Check invalid register operand when the address size override
8025 prefix changes the size of register operands. */
8026 unsigned int op;
8027 enum { need_word, need_dword, need_qword } need;
8028
8029 /* Check the register operand for the address size prefix if
8030 the memory operand has no real registers, like symbol, DISP
8031 or bogus (x32-only) symbol(%rip) when symbol(%eip) is meant. */
8032 if (i.mem_operands == 1
8033 && i.reg_operands == 1
8034 && i.operands == 2
8035 && i.types[1].bitfield.class == Reg
8036 && (flag_code == CODE_32BIT
8037 ? i.op[1].regs->reg_type.bitfield.word
8038 : i.op[1].regs->reg_type.bitfield.dword)
8039 && ((i.base_reg == NULL && i.index_reg == NULL)
8040 #if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
8041 || (x86_elf_abi == X86_64_X32_ABI
8042 && i.base_reg
8043 && i.base_reg->reg_num == RegIP
8044 && i.base_reg->reg_type.bitfield.qword))
8045 #else
8046 || 0)
8047 #endif
8048 && !add_prefix (ADDR_PREFIX_OPCODE))
8049 return 0;
8050
8051 if (flag_code == CODE_32BIT)
8052 need = i.prefix[ADDR_PREFIX] ? need_word : need_dword;
8053 else if (i.prefix[ADDR_PREFIX])
8054 need = need_dword;
8055 else
8056 need = flag_code == CODE_64BIT ? need_qword : need_word;
8057
8058 for (op = 0; op < i.operands; op++)
8059 {
8060 if (i.types[op].bitfield.class != Reg)
8061 continue;
8062
8063 switch (need)
8064 {
8065 case need_word:
8066 if (i.op[op].regs->reg_type.bitfield.word)
8067 continue;
8068 break;
8069 case need_dword:
8070 if (i.op[op].regs->reg_type.bitfield.dword)
8071 continue;
8072 break;
8073 case need_qword:
8074 if (i.op[op].regs->reg_type.bitfield.qword)
8075 continue;
8076 break;
8077 }
8078
8079 as_bad (_("invalid register operand size for `%s'"),
8080 insn_name (&i.tm));
8081 return 0;
8082 }
8083 }
8084 }
8085
8086 return 1;
8087 }
8088
8089 static int
8090 check_byte_reg (void)
8091 {
8092 int op;
8093
8094 for (op = i.operands; --op >= 0;)
8095 {
8096 /* Skip non-register operands. */
8097 if (i.types[op].bitfield.class != Reg)
8098 continue;
8099
8100 /* If this is an eight bit register, it's OK. If it's the 16 or
8101 32 bit version of an eight bit register, we will just use the
8102 low portion, and that's OK too. */
8103 if (i.types[op].bitfield.byte)
8104 continue;
8105
8106 /* I/O port address operands are OK too. */
8107 if (i.tm.operand_types[op].bitfield.instance == RegD
8108 && i.tm.operand_types[op].bitfield.word)
8109 continue;
8110
8111 /* crc32 only wants its source operand checked here. */
8112 if (i.tm.mnem_off == MN_crc32 && op != 0)
8113 continue;
8114
8115 /* Any other register is bad. */
8116 as_bad (_("`%s%s' not allowed with `%s%c'"),
8117 register_prefix, i.op[op].regs->reg_name,
8118 insn_name (&i.tm), i.suffix);
8119 return 0;
8120 }
8121 return 1;
8122 }
8123
8124 static int
8125 check_long_reg (void)
8126 {
8127 int op;
8128
8129 for (op = i.operands; --op >= 0;)
8130 /* Skip non-register operands. */
8131 if (i.types[op].bitfield.class != Reg)
8132 continue;
8133 /* Reject eight bit registers, except where the template requires
8134 them. (eg. movzb) */
8135 else if (i.types[op].bitfield.byte
8136 && (i.tm.operand_types[op].bitfield.class == Reg
8137 || i.tm.operand_types[op].bitfield.instance == Accum)
8138 && (i.tm.operand_types[op].bitfield.word
8139 || i.tm.operand_types[op].bitfield.dword))
8140 {
8141 as_bad (_("`%s%s' not allowed with `%s%c'"),
8142 register_prefix,
8143 i.op[op].regs->reg_name,
8144 insn_name (&i.tm),
8145 i.suffix);
8146 return 0;
8147 }
8148 /* Error if the e prefix on a general reg is missing. */
8149 else if (i.types[op].bitfield.word
8150 && (i.tm.operand_types[op].bitfield.class == Reg
8151 || i.tm.operand_types[op].bitfield.instance == Accum)
8152 && i.tm.operand_types[op].bitfield.dword)
8153 {
8154 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
8155 register_prefix, i.op[op].regs->reg_name,
8156 i.suffix);
8157 return 0;
8158 }
8159 /* Warn if the r prefix on a general reg is present. */
8160 else if (i.types[op].bitfield.qword
8161 && (i.tm.operand_types[op].bitfield.class == Reg
8162 || i.tm.operand_types[op].bitfield.instance == Accum)
8163 && i.tm.operand_types[op].bitfield.dword)
8164 {
8165 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
8166 register_prefix, i.op[op].regs->reg_name, i.suffix);
8167 return 0;
8168 }
8169 return 1;
8170 }
8171
8172 static int
8173 check_qword_reg (void)
8174 {
8175 int op;
8176
8177 for (op = i.operands; --op >= 0; )
8178 /* Skip non-register operands. */
8179 if (i.types[op].bitfield.class != Reg)
8180 continue;
8181 /* Reject eight bit registers, except where the template requires
8182 them. (eg. movzb) */
8183 else if (i.types[op].bitfield.byte
8184 && (i.tm.operand_types[op].bitfield.class == Reg
8185 || i.tm.operand_types[op].bitfield.instance == Accum)
8186 && (i.tm.operand_types[op].bitfield.word
8187 || i.tm.operand_types[op].bitfield.dword))
8188 {
8189 as_bad (_("`%s%s' not allowed with `%s%c'"),
8190 register_prefix,
8191 i.op[op].regs->reg_name,
8192 insn_name (&i.tm),
8193 i.suffix);
8194 return 0;
8195 }
8196 /* Warn if the r prefix on a general reg is missing. */
8197 else if ((i.types[op].bitfield.word
8198 || i.types[op].bitfield.dword)
8199 && (i.tm.operand_types[op].bitfield.class == Reg
8200 || i.tm.operand_types[op].bitfield.instance == Accum)
8201 && i.tm.operand_types[op].bitfield.qword)
8202 {
8203 /* Prohibit these changes in the 64bit mode, since the
8204 lowering is more complicated. */
8205 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
8206 register_prefix, i.op[op].regs->reg_name, i.suffix);
8207 return 0;
8208 }
8209 return 1;
8210 }
8211
8212 static int
8213 check_word_reg (void)
8214 {
8215 int op;
8216 for (op = i.operands; --op >= 0;)
8217 /* Skip non-register operands. */
8218 if (i.types[op].bitfield.class != Reg)
8219 continue;
8220 /* Reject eight bit registers, except where the template requires
8221 them. (eg. movzb) */
8222 else if (i.types[op].bitfield.byte
8223 && (i.tm.operand_types[op].bitfield.class == Reg
8224 || i.tm.operand_types[op].bitfield.instance == Accum)
8225 && (i.tm.operand_types[op].bitfield.word
8226 || i.tm.operand_types[op].bitfield.dword))
8227 {
8228 as_bad (_("`%s%s' not allowed with `%s%c'"),
8229 register_prefix,
8230 i.op[op].regs->reg_name,
8231 insn_name (&i.tm),
8232 i.suffix);
8233 return 0;
8234 }
8235 /* Error if the e or r prefix on a general reg is present. */
8236 else if ((i.types[op].bitfield.dword
8237 || i.types[op].bitfield.qword)
8238 && (i.tm.operand_types[op].bitfield.class == Reg
8239 || i.tm.operand_types[op].bitfield.instance == Accum)
8240 && i.tm.operand_types[op].bitfield.word)
8241 {
8242 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
8243 register_prefix, i.op[op].regs->reg_name,
8244 i.suffix);
8245 return 0;
8246 }
8247 return 1;
8248 }
8249
8250 static int
8251 update_imm (unsigned int j)
8252 {
8253 i386_operand_type overlap = i.types[j];
8254
8255 if (i.tm.operand_types[j].bitfield.imm8
8256 && i.tm.operand_types[j].bitfield.imm8s
8257 && overlap.bitfield.imm8 && overlap.bitfield.imm8s)
8258 {
8259 /* This combination is used on 8-bit immediates where e.g. $~0 is
8260 desirable to permit. We're past operand type matching, so simply
8261 put things back in the shape they were before introducing the
8262 distinction between Imm8, Imm8S, and Imm8|Imm8S. */
8263 overlap.bitfield.imm8s = 0;
8264 }
8265
8266 if (overlap.bitfield.imm8
8267 + overlap.bitfield.imm8s
8268 + overlap.bitfield.imm16
8269 + overlap.bitfield.imm32
8270 + overlap.bitfield.imm32s
8271 + overlap.bitfield.imm64 > 1)
8272 {
8273 static const i386_operand_type imm16 = { .bitfield = { .imm16 = 1 } };
8274 static const i386_operand_type imm32 = { .bitfield = { .imm32 = 1 } };
8275 static const i386_operand_type imm32s = { .bitfield = { .imm32s = 1 } };
8276 static const i386_operand_type imm16_32 = { .bitfield =
8277 { .imm16 = 1, .imm32 = 1 }
8278 };
8279 static const i386_operand_type imm16_32s = { .bitfield =
8280 { .imm16 = 1, .imm32s = 1 }
8281 };
8282 static const i386_operand_type imm16_32_32s = { .bitfield =
8283 { .imm16 = 1, .imm32 = 1, .imm32s = 1 }
8284 };
8285
8286 if (i.suffix)
8287 {
8288 i386_operand_type temp;
8289
8290 operand_type_set (&temp, 0);
8291 if (i.suffix == BYTE_MNEM_SUFFIX)
8292 {
8293 temp.bitfield.imm8 = overlap.bitfield.imm8;
8294 temp.bitfield.imm8s = overlap.bitfield.imm8s;
8295 }
8296 else if (i.suffix == WORD_MNEM_SUFFIX)
8297 temp.bitfield.imm16 = overlap.bitfield.imm16;
8298 else if (i.suffix == QWORD_MNEM_SUFFIX)
8299 {
8300 temp.bitfield.imm64 = overlap.bitfield.imm64;
8301 temp.bitfield.imm32s = overlap.bitfield.imm32s;
8302 }
8303 else
8304 temp.bitfield.imm32 = overlap.bitfield.imm32;
8305 overlap = temp;
8306 }
8307 else if (operand_type_equal (&overlap, &imm16_32_32s)
8308 || operand_type_equal (&overlap, &imm16_32)
8309 || operand_type_equal (&overlap, &imm16_32s))
8310 {
8311 if ((flag_code == CODE_16BIT)
8312 ^ (i.prefix[DATA_PREFIX] != 0 && !(i.prefix[REX_PREFIX] & REX_W)))
8313 overlap = imm16;
8314 else
8315 overlap = imm32s;
8316 }
8317 else if (i.prefix[REX_PREFIX] & REX_W)
8318 overlap = operand_type_and (overlap, imm32s);
8319 else if (i.prefix[DATA_PREFIX])
8320 overlap = operand_type_and (overlap,
8321 flag_code != CODE_16BIT ? imm16 : imm32);
8322 if (overlap.bitfield.imm8
8323 + overlap.bitfield.imm8s
8324 + overlap.bitfield.imm16
8325 + overlap.bitfield.imm32
8326 + overlap.bitfield.imm32s
8327 + overlap.bitfield.imm64 != 1)
8328 {
8329 as_bad (_("no instruction mnemonic suffix given; "
8330 "can't determine immediate size"));
8331 return 0;
8332 }
8333 }
8334 i.types[j] = overlap;
8335
8336 return 1;
8337 }
8338
8339 static int
8340 finalize_imm (void)
8341 {
8342 unsigned int j, n;
8343
8344 /* Update the first 2 immediate operands. */
8345 n = i.operands > 2 ? 2 : i.operands;
8346 if (n)
8347 {
8348 for (j = 0; j < n; j++)
8349 if (update_imm (j) == 0)
8350 return 0;
8351
8352 /* The 3rd operand can't be immediate operand. */
8353 gas_assert (operand_type_check (i.types[2], imm) == 0);
8354 }
8355
8356 return 1;
8357 }
8358
8359 static INLINE void set_rex_vrex (const reg_entry *r, unsigned int rex_bit,
8360 bool do_sse2avx)
8361 {
8362 if (r->reg_flags & RegRex)
8363 {
8364 if (i.rex & rex_bit)
8365 as_bad (_("same type of prefix used twice"));
8366 i.rex |= rex_bit;
8367 }
8368 else if (do_sse2avx && (i.rex & rex_bit) && i.vex.register_specifier)
8369 {
8370 gas_assert (i.vex.register_specifier == r);
8371 i.vex.register_specifier += 8;
8372 }
8373
8374 if (r->reg_flags & RegVRex)
8375 i.vrex |= rex_bit;
8376 }
8377
8378 static int
8379 process_operands (void)
8380 {
8381 /* Default segment register this instruction will use for memory
8382 accesses. 0 means unknown. This is only for optimizing out
8383 unnecessary segment overrides. */
8384 const reg_entry *default_seg = NULL;
8385
8386 /* We only need to check those implicit registers for instructions
8387 with 3 operands or less. */
8388 if (i.operands <= 3)
8389 for (unsigned int j = 0; j < i.operands; j++)
8390 if (i.types[j].bitfield.instance != InstanceNone)
8391 i.reg_operands--;
8392
8393 if (i.tm.opcode_modifier.sse2avx)
8394 {
8395 /* Legacy encoded insns allow explicit REX prefixes, so these prefixes
8396 need converting. */
8397 i.rex |= i.prefix[REX_PREFIX] & (REX_W | REX_R | REX_X | REX_B);
8398 i.prefix[REX_PREFIX] = 0;
8399 i.rex_encoding = 0;
8400 }
8401 /* ImmExt should be processed after SSE2AVX. */
8402 else if (i.tm.opcode_modifier.immext)
8403 process_immext ();
8404
8405 /* TILEZERO is unusual in that it has a single operand encoded in ModR/M.reg,
8406 not ModR/M.rm. To avoid special casing this in build_modrm_byte(), fake a
8407 new destination operand here, while converting the source one to register
8408 number 0. */
8409 if (i.tm.mnem_off == MN_tilezero)
8410 {
8411 i.op[1].regs = i.op[0].regs;
8412 i.op[0].regs -= i.op[0].regs->reg_num;
8413 i.types[1] = i.types[0];
8414 i.tm.operand_types[1] = i.tm.operand_types[0];
8415 i.flags[1] = i.flags[0];
8416 i.operands++;
8417 i.reg_operands++;
8418 i.tm.operands++;
8419 }
8420
8421 if (i.tm.opcode_modifier.sse2avx && i.tm.opcode_modifier.vexvvvv)
8422 {
8423 static const i386_operand_type regxmm = {
8424 .bitfield = { .class = RegSIMD, .xmmword = 1 }
8425 };
8426 unsigned int dupl = i.operands;
8427 unsigned int dest = dupl - 1;
8428 unsigned int j;
8429
8430 /* The destination must be an xmm register. */
8431 gas_assert (i.reg_operands
8432 && MAX_OPERANDS > dupl
8433 && operand_type_equal (&i.types[dest], &regxmm));
8434
8435 if (i.tm.operand_types[0].bitfield.instance == Accum
8436 && i.tm.operand_types[0].bitfield.xmmword)
8437 {
8438 /* Keep xmm0 for instructions with VEX prefix and 3
8439 sources. */
8440 i.tm.operand_types[0].bitfield.instance = InstanceNone;
8441 i.tm.operand_types[0].bitfield.class = RegSIMD;
8442 i.reg_operands++;
8443 goto duplicate;
8444 }
8445
8446 if (i.tm.opcode_modifier.operandconstraint == IMPLICIT_1ST_XMM0)
8447 {
8448 gas_assert ((MAX_OPERANDS - 1) > dupl);
8449
8450 /* Add the implicit xmm0 for instructions with VEX prefix
8451 and 3 sources. */
8452 for (j = i.operands; j > 0; j--)
8453 {
8454 i.op[j] = i.op[j - 1];
8455 i.types[j] = i.types[j - 1];
8456 i.tm.operand_types[j] = i.tm.operand_types[j - 1];
8457 i.flags[j] = i.flags[j - 1];
8458 }
8459 i.op[0].regs
8460 = (const reg_entry *) str_hash_find (reg_hash, "xmm0");
8461 i.types[0] = regxmm;
8462 i.tm.operand_types[0] = regxmm;
8463
8464 i.operands += 2;
8465 i.reg_operands += 2;
8466 i.tm.operands += 2;
8467
8468 dupl++;
8469 dest++;
8470 i.op[dupl] = i.op[dest];
8471 i.types[dupl] = i.types[dest];
8472 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8473 i.flags[dupl] = i.flags[dest];
8474 }
8475 else
8476 {
8477 duplicate:
8478 i.operands++;
8479 i.reg_operands++;
8480 i.tm.operands++;
8481
8482 i.op[dupl] = i.op[dest];
8483 i.types[dupl] = i.types[dest];
8484 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8485 i.flags[dupl] = i.flags[dest];
8486 }
8487
8488 if (i.tm.opcode_modifier.immext)
8489 process_immext ();
8490 }
8491 else if (i.tm.operand_types[0].bitfield.instance == Accum
8492 && i.tm.opcode_modifier.modrm)
8493 {
8494 unsigned int j;
8495
8496 for (j = 1; j < i.operands; j++)
8497 {
8498 i.op[j - 1] = i.op[j];
8499 i.types[j - 1] = i.types[j];
8500
8501 /* We need to adjust fields in i.tm since they are used by
8502 build_modrm_byte. */
8503 i.tm.operand_types [j - 1] = i.tm.operand_types [j];
8504
8505 i.flags[j - 1] = i.flags[j];
8506 }
8507
8508 /* No adjustment to i.reg_operands: This was already done at the top
8509 of the function. */
8510 i.operands--;
8511 i.tm.operands--;
8512 }
8513 else if (i.tm.opcode_modifier.operandconstraint == IMPLICIT_QUAD_GROUP)
8514 {
8515 unsigned int regnum, first_reg_in_group, last_reg_in_group;
8516
8517 /* The second operand must be {x,y,z}mmN, where N is a multiple of 4. */
8518 gas_assert (i.operands >= 2 && i.types[1].bitfield.class == RegSIMD);
8519 regnum = register_number (i.op[1].regs);
8520 first_reg_in_group = regnum & ~3;
8521 last_reg_in_group = first_reg_in_group + 3;
8522 if (regnum != first_reg_in_group)
8523 as_warn (_("source register `%s%s' implicitly denotes"
8524 " `%s%.3s%u' to `%s%.3s%u' source group in `%s'"),
8525 register_prefix, i.op[1].regs->reg_name,
8526 register_prefix, i.op[1].regs->reg_name, first_reg_in_group,
8527 register_prefix, i.op[1].regs->reg_name, last_reg_in_group,
8528 insn_name (&i.tm));
8529 }
8530 else if (i.tm.opcode_modifier.operandconstraint == REG_KLUDGE)
8531 {
8532 /* The imul $imm, %reg instruction is converted into
8533 imul $imm, %reg, %reg, and the clr %reg instruction
8534 is converted into xor %reg, %reg. */
8535
8536 unsigned int first_reg_op;
8537
8538 if (operand_type_check (i.types[0], reg))
8539 first_reg_op = 0;
8540 else
8541 first_reg_op = 1;
8542 /* Pretend we saw the extra register operand. */
8543 gas_assert (i.reg_operands == 1
8544 && i.op[first_reg_op + 1].regs == 0);
8545 i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
8546 i.types[first_reg_op + 1] = i.types[first_reg_op];
8547 i.operands++;
8548 i.reg_operands++;
8549 }
8550
8551 if (i.tm.opcode_modifier.modrm)
8552 {
8553 /* The opcode is completed (modulo i.tm.extension_opcode which
8554 must be put into the modrm byte). Now, we make the modrm and
8555 index base bytes based on all the info we've collected. */
8556
8557 default_seg = build_modrm_byte ();
8558
8559 if (!quiet_warnings && i.tm.opcode_modifier.operandconstraint == UGH)
8560 {
8561 /* Warn about some common errors, but press on regardless. */
8562 if (i.operands == 2)
8563 {
8564 /* Reversed arguments on faddp or fmulp. */
8565 as_warn (_("translating to `%s %s%s,%s%s'"), insn_name (&i.tm),
8566 register_prefix, i.op[!intel_syntax].regs->reg_name,
8567 register_prefix, i.op[intel_syntax].regs->reg_name);
8568 }
8569 else if (i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
8570 {
8571 /* Extraneous `l' suffix on fp insn. */
8572 as_warn (_("translating to `%s %s%s'"), insn_name (&i.tm),
8573 register_prefix, i.op[0].regs->reg_name);
8574 }
8575 }
8576 }
8577 else if (i.types[0].bitfield.class == SReg && !dot_insn ())
8578 {
8579 if (flag_code != CODE_64BIT
8580 ? i.tm.base_opcode == POP_SEG_SHORT
8581 && i.op[0].regs->reg_num == 1
8582 : (i.tm.base_opcode | 1) == (POP_SEG386_SHORT & 0xff)
8583 && i.op[0].regs->reg_num < 4)
8584 {
8585 as_bad (_("you can't `%s %s%s'"),
8586 insn_name (&i.tm), register_prefix, i.op[0].regs->reg_name);
8587 return 0;
8588 }
8589 if (i.op[0].regs->reg_num > 3
8590 && i.tm.opcode_space == SPACE_BASE )
8591 {
8592 i.tm.base_opcode ^= (POP_SEG_SHORT ^ POP_SEG386_SHORT) & 0xff;
8593 i.tm.opcode_space = SPACE_0F;
8594 }
8595 i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
8596 }
8597 else if (i.tm.opcode_space == SPACE_BASE
8598 && (i.tm.base_opcode & ~3) == MOV_AX_DISP32)
8599 {
8600 default_seg = reg_ds;
8601 }
8602 else if (i.tm.opcode_modifier.isstring)
8603 {
8604 /* For the string instructions that allow a segment override
8605 on one of their operands, the default segment is ds. */
8606 default_seg = reg_ds;
8607 }
8608 else if (i.short_form)
8609 {
8610 /* The register operand is in the 1st or 2nd non-immediate operand. */
8611 const reg_entry *r = i.op[i.imm_operands].regs;
8612
8613 if (!dot_insn ()
8614 && r->reg_type.bitfield.instance == Accum
8615 && i.op[i.imm_operands + 1].regs)
8616 r = i.op[i.imm_operands + 1].regs;
8617 /* Register goes in low 3 bits of opcode. */
8618 i.tm.base_opcode |= r->reg_num;
8619 set_rex_vrex (r, REX_B, false);
8620
8621 if (dot_insn () && i.reg_operands == 2)
8622 {
8623 gas_assert (is_any_vex_encoding (&i.tm)
8624 || i.vec_encoding != vex_encoding_default);
8625 i.vex.register_specifier = i.op[i.operands - 1].regs;
8626 }
8627 }
8628 else if (i.reg_operands == 1
8629 && !i.flags[i.operands - 1]
8630 && i.tm.operand_types[i.operands - 1].bitfield.instance
8631 == InstanceNone)
8632 {
8633 gas_assert (is_any_vex_encoding (&i.tm)
8634 || i.vec_encoding != vex_encoding_default);
8635 i.vex.register_specifier = i.op[i.operands - 1].regs;
8636 }
8637
8638 if ((i.seg[0] || i.prefix[SEG_PREFIX])
8639 && i.tm.mnem_off == MN_lea)
8640 {
8641 if (!quiet_warnings)
8642 as_warn (_("segment override on `%s' is ineffectual"), insn_name (&i.tm));
8643 if (optimize && !i.no_optimize)
8644 {
8645 i.seg[0] = NULL;
8646 i.prefix[SEG_PREFIX] = 0;
8647 }
8648 }
8649
8650 /* If a segment was explicitly specified, and the specified segment
8651 is neither the default nor the one already recorded from a prefix,
8652 use an opcode prefix to select it. If we never figured out what
8653 the default segment is, then default_seg will be zero at this
8654 point, and the specified segment prefix will always be used. */
8655 if (i.seg[0]
8656 && i.seg[0] != default_seg
8657 && i386_seg_prefixes[i.seg[0]->reg_num] != i.prefix[SEG_PREFIX])
8658 {
8659 if (!add_prefix (i386_seg_prefixes[i.seg[0]->reg_num]))
8660 return 0;
8661 }
8662 return 1;
8663 }
8664
8665 static const reg_entry *
8666 build_modrm_byte (void)
8667 {
8668 const reg_entry *default_seg = NULL;
8669 unsigned int source = i.imm_operands - i.tm.opcode_modifier.immext
8670 /* Compensate for kludge in md_assemble(). */
8671 + i.tm.operand_types[0].bitfield.imm1;
8672 unsigned int dest = i.operands - 1 - i.tm.opcode_modifier.immext;
8673 unsigned int v, op, reg_slot = ~0;
8674
8675 /* Accumulator (in particular %st), shift count (%cl), and alike need
8676 to be skipped just like immediate operands do. */
8677 if (i.tm.operand_types[source].bitfield.instance)
8678 ++source;
8679 while (i.tm.operand_types[dest].bitfield.instance)
8680 --dest;
8681
8682 for (op = source; op < i.operands; ++op)
8683 if (i.tm.operand_types[op].bitfield.baseindex)
8684 break;
8685
8686 if (i.reg_operands + i.mem_operands + (i.tm.extension_opcode != None) == 4)
8687 {
8688 expressionS *exp;
8689
8690 /* There are 2 kinds of instructions:
8691 1. 5 operands: 4 register operands or 3 register operands
8692 plus 1 memory operand plus one Imm4 operand, VexXDS, and
8693 VexW0 or VexW1. The destination must be either XMM, YMM or
8694 ZMM register.
8695 2. 4 operands: 4 register operands or 3 register operands
8696 plus 1 memory operand, with VexXDS.
8697 3. Other equivalent combinations when coming from s_insn(). */
8698 gas_assert (i.tm.opcode_modifier.vexvvvv
8699 && i.tm.opcode_modifier.vexw);
8700 gas_assert (dot_insn ()
8701 || i.tm.operand_types[dest].bitfield.class == RegSIMD);
8702
8703 /* Of the first two non-immediate operands the one with the template
8704 not allowing for a memory one is encoded in the immediate operand. */
8705 if (source == op)
8706 reg_slot = source + 1;
8707 else
8708 reg_slot = source++;
8709
8710 if (!dot_insn ())
8711 {
8712 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
8713 gas_assert (!(i.op[reg_slot].regs->reg_flags & RegVRex));
8714 }
8715 else
8716 gas_assert (i.tm.operand_types[reg_slot].bitfield.class != ClassNone);
8717
8718 if (i.imm_operands == 0)
8719 {
8720 /* When there is no immediate operand, generate an 8bit
8721 immediate operand to encode the first operand. */
8722 exp = &im_expressions[i.imm_operands++];
8723 i.op[i.operands].imms = exp;
8724 i.types[i.operands].bitfield.imm8 = 1;
8725 i.operands++;
8726
8727 exp->X_op = O_constant;
8728 }
8729 else
8730 {
8731 gas_assert (i.imm_operands == 1);
8732 gas_assert (fits_in_imm4 (i.op[0].imms->X_add_number));
8733 gas_assert (!i.tm.opcode_modifier.immext);
8734
8735 /* Turn on Imm8 again so that output_imm will generate it. */
8736 i.types[0].bitfield.imm8 = 1;
8737
8738 exp = i.op[0].imms;
8739 }
8740 exp->X_add_number |= register_number (i.op[reg_slot].regs)
8741 << (3 + !(is_evex_encoding (&i.tm)
8742 || i.vec_encoding == vex_encoding_evex));
8743 }
8744
8745 for (v = source + 1; v < dest; ++v)
8746 if (v != reg_slot)
8747 break;
8748 if (v >= dest)
8749 v = ~0;
8750 if (i.tm.extension_opcode != None)
8751 {
8752 if (dest != source)
8753 v = dest;
8754 dest = ~0;
8755 }
8756 gas_assert (source < dest);
8757 if (i.tm.opcode_modifier.operandconstraint == SWAP_SOURCES
8758 && source != op)
8759 {
8760 unsigned int tmp = source;
8761
8762 source = v;
8763 v = tmp;
8764 }
8765
8766 if (v < MAX_OPERANDS)
8767 {
8768 gas_assert (i.tm.opcode_modifier.vexvvvv);
8769 i.vex.register_specifier = i.op[v].regs;
8770 }
8771
8772 if (op < i.operands)
8773 {
8774 if (i.mem_operands)
8775 {
8776 unsigned int fake_zero_displacement = 0;
8777
8778 gas_assert (i.flags[op] & Operand_Mem);
8779
8780 if (i.tm.opcode_modifier.sib)
8781 {
8782 /* The index register of VSIB shouldn't be RegIZ. */
8783 if (i.tm.opcode_modifier.sib != SIBMEM
8784 && i.index_reg->reg_num == RegIZ)
8785 abort ();
8786
8787 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8788 if (!i.base_reg)
8789 {
8790 i.sib.base = NO_BASE_REGISTER;
8791 i.sib.scale = i.log2_scale_factor;
8792 i.types[op] = operand_type_and_not (i.types[op], anydisp);
8793 i.types[op].bitfield.disp32 = 1;
8794 }
8795
8796 /* Since the mandatory SIB always has index register, so
8797 the code logic remains unchanged. The non-mandatory SIB
8798 without index register is allowed and will be handled
8799 later. */
8800 if (i.index_reg)
8801 {
8802 if (i.index_reg->reg_num == RegIZ)
8803 i.sib.index = NO_INDEX_REGISTER;
8804 else
8805 i.sib.index = i.index_reg->reg_num;
8806 set_rex_vrex (i.index_reg, REX_X, false);
8807 }
8808 }
8809
8810 default_seg = reg_ds;
8811
8812 if (i.base_reg == 0)
8813 {
8814 i.rm.mode = 0;
8815 if (!i.disp_operands)
8816 fake_zero_displacement = 1;
8817 if (i.index_reg == 0)
8818 {
8819 /* Both check for VSIB and mandatory non-vector SIB. */
8820 gas_assert (!i.tm.opcode_modifier.sib
8821 || i.tm.opcode_modifier.sib == SIBMEM);
8822 /* Operand is just <disp> */
8823 i.types[op] = operand_type_and_not (i.types[op], anydisp);
8824 if (flag_code == CODE_64BIT)
8825 {
8826 /* 64bit mode overwrites the 32bit absolute
8827 addressing by RIP relative addressing and
8828 absolute addressing is encoded by one of the
8829 redundant SIB forms. */
8830 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8831 i.sib.base = NO_BASE_REGISTER;
8832 i.sib.index = NO_INDEX_REGISTER;
8833 i.types[op].bitfield.disp32 = 1;
8834 }
8835 else if ((flag_code == CODE_16BIT)
8836 ^ (i.prefix[ADDR_PREFIX] != 0))
8837 {
8838 i.rm.regmem = NO_BASE_REGISTER_16;
8839 i.types[op].bitfield.disp16 = 1;
8840 }
8841 else
8842 {
8843 i.rm.regmem = NO_BASE_REGISTER;
8844 i.types[op].bitfield.disp32 = 1;
8845 }
8846 }
8847 else if (!i.tm.opcode_modifier.sib)
8848 {
8849 /* !i.base_reg && i.index_reg */
8850 if (i.index_reg->reg_num == RegIZ)
8851 i.sib.index = NO_INDEX_REGISTER;
8852 else
8853 i.sib.index = i.index_reg->reg_num;
8854 i.sib.base = NO_BASE_REGISTER;
8855 i.sib.scale = i.log2_scale_factor;
8856 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8857 i.types[op] = operand_type_and_not (i.types[op], anydisp);
8858 i.types[op].bitfield.disp32 = 1;
8859 if ((i.index_reg->reg_flags & RegRex) != 0)
8860 i.rex |= REX_X;
8861 }
8862 }
8863 /* RIP addressing for 64bit mode. */
8864 else if (i.base_reg->reg_num == RegIP)
8865 {
8866 gas_assert (!i.tm.opcode_modifier.sib);
8867 i.rm.regmem = NO_BASE_REGISTER;
8868 i.types[op].bitfield.disp8 = 0;
8869 i.types[op].bitfield.disp16 = 0;
8870 i.types[op].bitfield.disp32 = 1;
8871 i.types[op].bitfield.disp64 = 0;
8872 i.flags[op] |= Operand_PCrel;
8873 if (! i.disp_operands)
8874 fake_zero_displacement = 1;
8875 }
8876 else if (i.base_reg->reg_type.bitfield.word)
8877 {
8878 gas_assert (!i.tm.opcode_modifier.sib);
8879 switch (i.base_reg->reg_num)
8880 {
8881 case 3: /* (%bx) */
8882 if (i.index_reg == 0)
8883 i.rm.regmem = 7;
8884 else /* (%bx,%si) -> 0, or (%bx,%di) -> 1 */
8885 i.rm.regmem = i.index_reg->reg_num - 6;
8886 break;
8887 case 5: /* (%bp) */
8888 default_seg = reg_ss;
8889 if (i.index_reg == 0)
8890 {
8891 i.rm.regmem = 6;
8892 if (operand_type_check (i.types[op], disp) == 0)
8893 {
8894 /* fake (%bp) into 0(%bp) */
8895 if (i.disp_encoding == disp_encoding_16bit)
8896 i.types[op].bitfield.disp16 = 1;
8897 else
8898 i.types[op].bitfield.disp8 = 1;
8899 fake_zero_displacement = 1;
8900 }
8901 }
8902 else /* (%bp,%si) -> 2, or (%bp,%di) -> 3 */
8903 i.rm.regmem = i.index_reg->reg_num - 6 + 2;
8904 break;
8905 default: /* (%si) -> 4 or (%di) -> 5 */
8906 i.rm.regmem = i.base_reg->reg_num - 6 + 4;
8907 }
8908 if (!fake_zero_displacement
8909 && !i.disp_operands
8910 && i.disp_encoding)
8911 {
8912 fake_zero_displacement = 1;
8913 if (i.disp_encoding == disp_encoding_8bit)
8914 i.types[op].bitfield.disp8 = 1;
8915 else
8916 i.types[op].bitfield.disp16 = 1;
8917 }
8918 i.rm.mode = mode_from_disp_size (i.types[op]);
8919 }
8920 else /* i.base_reg and 32/64 bit mode */
8921 {
8922 if (operand_type_check (i.types[op], disp))
8923 {
8924 i.types[op].bitfield.disp16 = 0;
8925 i.types[op].bitfield.disp64 = 0;
8926 i.types[op].bitfield.disp32 = 1;
8927 }
8928
8929 if (!i.tm.opcode_modifier.sib)
8930 i.rm.regmem = i.base_reg->reg_num;
8931 if ((i.base_reg->reg_flags & RegRex) != 0)
8932 i.rex |= REX_B;
8933 i.sib.base = i.base_reg->reg_num;
8934 /* x86-64 ignores REX prefix bit here to avoid decoder
8935 complications. */
8936 if (!(i.base_reg->reg_flags & RegRex)
8937 && (i.base_reg->reg_num == EBP_REG_NUM
8938 || i.base_reg->reg_num == ESP_REG_NUM))
8939 default_seg = reg_ss;
8940 if (i.base_reg->reg_num == 5 && i.disp_operands == 0)
8941 {
8942 fake_zero_displacement = 1;
8943 if (i.disp_encoding == disp_encoding_32bit)
8944 i.types[op].bitfield.disp32 = 1;
8945 else
8946 i.types[op].bitfield.disp8 = 1;
8947 }
8948 i.sib.scale = i.log2_scale_factor;
8949 if (i.index_reg == 0)
8950 {
8951 /* Only check for VSIB. */
8952 gas_assert (i.tm.opcode_modifier.sib != VECSIB128
8953 && i.tm.opcode_modifier.sib != VECSIB256
8954 && i.tm.opcode_modifier.sib != VECSIB512);
8955
8956 /* <disp>(%esp) becomes two byte modrm with no index
8957 register. We've already stored the code for esp
8958 in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
8959 Any base register besides %esp will not use the
8960 extra modrm byte. */
8961 i.sib.index = NO_INDEX_REGISTER;
8962 }
8963 else if (!i.tm.opcode_modifier.sib)
8964 {
8965 if (i.index_reg->reg_num == RegIZ)
8966 i.sib.index = NO_INDEX_REGISTER;
8967 else
8968 i.sib.index = i.index_reg->reg_num;
8969 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8970 if ((i.index_reg->reg_flags & RegRex) != 0)
8971 i.rex |= REX_X;
8972 }
8973
8974 if (i.disp_operands
8975 && (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
8976 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL))
8977 i.rm.mode = 0;
8978 else
8979 {
8980 if (!fake_zero_displacement
8981 && !i.disp_operands
8982 && i.disp_encoding)
8983 {
8984 fake_zero_displacement = 1;
8985 if (i.disp_encoding == disp_encoding_8bit)
8986 i.types[op].bitfield.disp8 = 1;
8987 else
8988 i.types[op].bitfield.disp32 = 1;
8989 }
8990 i.rm.mode = mode_from_disp_size (i.types[op]);
8991 }
8992 }
8993
8994 if (fake_zero_displacement)
8995 {
8996 /* Fakes a zero displacement assuming that i.types[op]
8997 holds the correct displacement size. */
8998 expressionS *exp;
8999
9000 gas_assert (i.op[op].disps == 0);
9001 exp = &disp_expressions[i.disp_operands++];
9002 i.op[op].disps = exp;
9003 exp->X_op = O_constant;
9004 exp->X_add_number = 0;
9005 exp->X_add_symbol = (symbolS *) 0;
9006 exp->X_op_symbol = (symbolS *) 0;
9007 }
9008 }
9009 else
9010 {
9011 i.rm.mode = 3;
9012 i.rm.regmem = i.op[op].regs->reg_num;
9013 set_rex_vrex (i.op[op].regs, REX_B, false);
9014 }
9015
9016 if (op == dest)
9017 dest = ~0;
9018 if (op == source)
9019 source = ~0;
9020 }
9021 else
9022 {
9023 i.rm.mode = 3;
9024 if (!i.tm.opcode_modifier.regmem)
9025 {
9026 gas_assert (source < MAX_OPERANDS);
9027 i.rm.regmem = i.op[source].regs->reg_num;
9028 set_rex_vrex (i.op[source].regs, REX_B,
9029 dest >= MAX_OPERANDS && i.tm.opcode_modifier.sse2avx);
9030 source = ~0;
9031 }
9032 else
9033 {
9034 gas_assert (dest < MAX_OPERANDS);
9035 i.rm.regmem = i.op[dest].regs->reg_num;
9036 set_rex_vrex (i.op[dest].regs, REX_B, i.tm.opcode_modifier.sse2avx);
9037 dest = ~0;
9038 }
9039 }
9040
9041 /* Fill in i.rm.reg field with extension opcode (if any) or the
9042 appropriate register. */
9043 if (i.tm.extension_opcode != None)
9044 i.rm.reg = i.tm.extension_opcode;
9045 else if (!i.tm.opcode_modifier.regmem && dest < MAX_OPERANDS)
9046 {
9047 i.rm.reg = i.op[dest].regs->reg_num;
9048 set_rex_vrex (i.op[dest].regs, REX_R, i.tm.opcode_modifier.sse2avx);
9049 }
9050 else
9051 {
9052 gas_assert (source < MAX_OPERANDS);
9053 i.rm.reg = i.op[source].regs->reg_num;
9054 set_rex_vrex (i.op[source].regs, REX_R, false);
9055 }
9056
9057 if (flag_code != CODE_64BIT && (i.rex & REX_R))
9058 {
9059 gas_assert (i.types[!i.tm.opcode_modifier.regmem].bitfield.class == RegCR);
9060 i.rex &= ~REX_R;
9061 add_prefix (LOCK_PREFIX_OPCODE);
9062 }
9063
9064 return default_seg;
9065 }
9066
9067 static INLINE void
9068 frag_opcode_byte (unsigned char byte)
9069 {
9070 if (now_seg != absolute_section)
9071 FRAG_APPEND_1_CHAR (byte);
9072 else
9073 ++abs_section_offset;
9074 }
9075
9076 static unsigned int
9077 flip_code16 (unsigned int code16)
9078 {
9079 gas_assert (i.tm.operands == 1);
9080
9081 return !(i.prefix[REX_PREFIX] & REX_W)
9082 && (code16 ? i.tm.operand_types[0].bitfield.disp32
9083 : i.tm.operand_types[0].bitfield.disp16)
9084 ? CODE16 : 0;
9085 }
9086
9087 static void
9088 output_branch (void)
9089 {
9090 char *p;
9091 int size;
9092 int code16;
9093 int prefix;
9094 relax_substateT subtype;
9095 symbolS *sym;
9096 offsetT off;
9097
9098 if (now_seg == absolute_section)
9099 {
9100 as_bad (_("relaxable branches not supported in absolute section"));
9101 return;
9102 }
9103
9104 code16 = flag_code == CODE_16BIT ? CODE16 : 0;
9105 size = i.disp_encoding > disp_encoding_8bit ? BIG : SMALL;
9106
9107 prefix = 0;
9108 if (i.prefix[DATA_PREFIX] != 0)
9109 {
9110 prefix = 1;
9111 i.prefixes -= 1;
9112 code16 ^= flip_code16(code16);
9113 }
9114 /* Pentium4 branch hints. */
9115 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
9116 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
9117 {
9118 prefix++;
9119 i.prefixes--;
9120 }
9121 if (i.prefix[REX_PREFIX] != 0)
9122 {
9123 prefix++;
9124 i.prefixes--;
9125 }
9126
9127 /* BND prefixed jump. */
9128 if (i.prefix[BND_PREFIX] != 0)
9129 {
9130 prefix++;
9131 i.prefixes--;
9132 }
9133
9134 if (i.prefixes != 0)
9135 as_warn (_("skipping prefixes on `%s'"), insn_name (&i.tm));
9136
9137 /* It's always a symbol; End frag & setup for relax.
9138 Make sure there is enough room in this frag for the largest
9139 instruction we may generate in md_convert_frag. This is 2
9140 bytes for the opcode and room for the prefix and largest
9141 displacement. */
9142 frag_grow (prefix + 2 + 4);
9143 /* Prefix and 1 opcode byte go in fr_fix. */
9144 p = frag_more (prefix + 1);
9145 if (i.prefix[DATA_PREFIX] != 0)
9146 *p++ = DATA_PREFIX_OPCODE;
9147 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
9148 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
9149 *p++ = i.prefix[SEG_PREFIX];
9150 if (i.prefix[BND_PREFIX] != 0)
9151 *p++ = BND_PREFIX_OPCODE;
9152 if (i.prefix[REX_PREFIX] != 0)
9153 *p++ = i.prefix[REX_PREFIX];
9154 *p = i.tm.base_opcode;
9155
9156 if ((unsigned char) *p == JUMP_PC_RELATIVE)
9157 subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, size);
9158 else if (cpu_arch_flags.bitfield.cpui386)
9159 subtype = ENCODE_RELAX_STATE (COND_JUMP, size);
9160 else
9161 subtype = ENCODE_RELAX_STATE (COND_JUMP86, size);
9162 subtype |= code16;
9163
9164 sym = i.op[0].disps->X_add_symbol;
9165 off = i.op[0].disps->X_add_number;
9166
9167 if (i.op[0].disps->X_op != O_constant
9168 && i.op[0].disps->X_op != O_symbol)
9169 {
9170 /* Handle complex expressions. */
9171 sym = make_expr_symbol (i.op[0].disps);
9172 off = 0;
9173 }
9174
9175 /* 1 possible extra opcode + 4 byte displacement go in var part.
9176 Pass reloc in fr_var. */
9177 frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
9178 }
9179
9180 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9181 /* Return TRUE iff PLT32 relocation should be used for branching to
9182 symbol S. */
9183
9184 static bool
9185 need_plt32_p (symbolS *s)
9186 {
9187 /* PLT32 relocation is ELF only. */
9188 if (!IS_ELF)
9189 return false;
9190
9191 #ifdef TE_SOLARIS
9192 /* Don't emit PLT32 relocation on Solaris: neither native linker nor
9193 krtld support it. */
9194 return false;
9195 #endif
9196
9197 /* Since there is no need to prepare for PLT branch on x86-64, we
9198 can generate R_X86_64_PLT32, instead of R_X86_64_PC32, which can
9199 be used as a marker for 32-bit PC-relative branches. */
9200 if (!object_64bit)
9201 return false;
9202
9203 if (s == NULL)
9204 return false;
9205
9206 /* Weak or undefined symbol need PLT32 relocation. */
9207 if (S_IS_WEAK (s) || !S_IS_DEFINED (s))
9208 return true;
9209
9210 /* Non-global symbol doesn't need PLT32 relocation. */
9211 if (! S_IS_EXTERNAL (s))
9212 return false;
9213
9214 /* Other global symbols need PLT32 relocation. NB: Symbol with
9215 non-default visibilities are treated as normal global symbol
9216 so that PLT32 relocation can be used as a marker for 32-bit
9217 PC-relative branches. It is useful for linker relaxation. */
9218 return true;
9219 }
9220 #endif
9221
9222 static void
9223 output_jump (void)
9224 {
9225 char *p;
9226 int size;
9227 fixS *fixP;
9228 bfd_reloc_code_real_type jump_reloc = i.reloc[0];
9229
9230 if (i.tm.opcode_modifier.jump == JUMP_BYTE)
9231 {
9232 /* This is a loop or jecxz type instruction. */
9233 size = 1;
9234 if (i.prefix[ADDR_PREFIX] != 0)
9235 {
9236 frag_opcode_byte (ADDR_PREFIX_OPCODE);
9237 i.prefixes -= 1;
9238 }
9239 /* Pentium4 branch hints. */
9240 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
9241 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
9242 {
9243 frag_opcode_byte (i.prefix[SEG_PREFIX]);
9244 i.prefixes--;
9245 }
9246 }
9247 else
9248 {
9249 int code16;
9250
9251 code16 = 0;
9252 if (flag_code == CODE_16BIT)
9253 code16 = CODE16;
9254
9255 if (i.prefix[DATA_PREFIX] != 0)
9256 {
9257 frag_opcode_byte (DATA_PREFIX_OPCODE);
9258 i.prefixes -= 1;
9259 code16 ^= flip_code16(code16);
9260 }
9261
9262 size = 4;
9263 if (code16)
9264 size = 2;
9265 }
9266
9267 /* BND prefixed jump. */
9268 if (i.prefix[BND_PREFIX] != 0)
9269 {
9270 frag_opcode_byte (i.prefix[BND_PREFIX]);
9271 i.prefixes -= 1;
9272 }
9273
9274 if (i.prefix[REX_PREFIX] != 0)
9275 {
9276 frag_opcode_byte (i.prefix[REX_PREFIX]);
9277 i.prefixes -= 1;
9278 }
9279
9280 if (i.prefixes != 0)
9281 as_warn (_("skipping prefixes on `%s'"), insn_name (&i.tm));
9282
9283 if (now_seg == absolute_section)
9284 {
9285 abs_section_offset += i.opcode_length + size;
9286 return;
9287 }
9288
9289 p = frag_more (i.opcode_length + size);
9290 switch (i.opcode_length)
9291 {
9292 case 2:
9293 *p++ = i.tm.base_opcode >> 8;
9294 /* Fall through. */
9295 case 1:
9296 *p++ = i.tm.base_opcode;
9297 break;
9298 default:
9299 abort ();
9300 }
9301
9302 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9303 if (flag_code == CODE_64BIT && size == 4
9304 && jump_reloc == NO_RELOC && i.op[0].disps->X_add_number == 0
9305 && need_plt32_p (i.op[0].disps->X_add_symbol))
9306 jump_reloc = BFD_RELOC_X86_64_PLT32;
9307 #endif
9308
9309 jump_reloc = reloc (size, 1, 1, jump_reloc);
9310
9311 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
9312 i.op[0].disps, 1, jump_reloc);
9313
9314 /* All jumps handled here are signed, but don't unconditionally use a
9315 signed limit check for 32 and 16 bit jumps as we want to allow wrap
9316 around at 4G (outside of 64-bit mode) and 64k (except for XBEGIN)
9317 respectively. */
9318 switch (size)
9319 {
9320 case 1:
9321 fixP->fx_signed = 1;
9322 break;
9323
9324 case 2:
9325 if (i.tm.mnem_off == MN_xbegin)
9326 fixP->fx_signed = 1;
9327 break;
9328
9329 case 4:
9330 if (flag_code == CODE_64BIT)
9331 fixP->fx_signed = 1;
9332 break;
9333 }
9334 }
9335
9336 static void
9337 output_interseg_jump (void)
9338 {
9339 char *p;
9340 int size;
9341 int prefix;
9342 int code16;
9343
9344 code16 = 0;
9345 if (flag_code == CODE_16BIT)
9346 code16 = CODE16;
9347
9348 prefix = 0;
9349 if (i.prefix[DATA_PREFIX] != 0)
9350 {
9351 prefix = 1;
9352 i.prefixes -= 1;
9353 code16 ^= CODE16;
9354 }
9355
9356 gas_assert (!i.prefix[REX_PREFIX]);
9357
9358 size = 4;
9359 if (code16)
9360 size = 2;
9361
9362 if (i.prefixes != 0)
9363 as_warn (_("skipping prefixes on `%s'"), insn_name (&i.tm));
9364
9365 if (now_seg == absolute_section)
9366 {
9367 abs_section_offset += prefix + 1 + 2 + size;
9368 return;
9369 }
9370
9371 /* 1 opcode; 2 segment; offset */
9372 p = frag_more (prefix + 1 + 2 + size);
9373
9374 if (i.prefix[DATA_PREFIX] != 0)
9375 *p++ = DATA_PREFIX_OPCODE;
9376
9377 if (i.prefix[REX_PREFIX] != 0)
9378 *p++ = i.prefix[REX_PREFIX];
9379
9380 *p++ = i.tm.base_opcode;
9381 if (i.op[1].imms->X_op == O_constant)
9382 {
9383 offsetT n = i.op[1].imms->X_add_number;
9384
9385 if (size == 2
9386 && !fits_in_unsigned_word (n)
9387 && !fits_in_signed_word (n))
9388 {
9389 as_bad (_("16-bit jump out of range"));
9390 return;
9391 }
9392 md_number_to_chars (p, n, size);
9393 }
9394 else
9395 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
9396 i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
9397
9398 p += size;
9399 if (i.op[0].imms->X_op == O_constant)
9400 md_number_to_chars (p, (valueT) i.op[0].imms->X_add_number, 2);
9401 else
9402 fix_new_exp (frag_now, p - frag_now->fr_literal, 2,
9403 i.op[0].imms, 0, reloc (2, 0, 0, i.reloc[0]));
9404 }
9405
9406 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9407 void
9408 x86_cleanup (void)
9409 {
9410 char *p;
9411 asection *seg = now_seg;
9412 subsegT subseg = now_subseg;
9413 asection *sec;
9414 unsigned int alignment, align_size_1;
9415 unsigned int isa_1_descsz, feature_2_descsz, descsz;
9416 unsigned int isa_1_descsz_raw, feature_2_descsz_raw;
9417 unsigned int padding;
9418
9419 if (!IS_ELF || !x86_used_note)
9420 return;
9421
9422 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X86;
9423
9424 /* The .note.gnu.property section layout:
9425
9426 Field Length Contents
9427 ---- ---- ----
9428 n_namsz 4 4
9429 n_descsz 4 The note descriptor size
9430 n_type 4 NT_GNU_PROPERTY_TYPE_0
9431 n_name 4 "GNU"
9432 n_desc n_descsz The program property array
9433 .... .... ....
9434 */
9435
9436 /* Create the .note.gnu.property section. */
9437 sec = subseg_new (NOTE_GNU_PROPERTY_SECTION_NAME, 0);
9438 bfd_set_section_flags (sec,
9439 (SEC_ALLOC
9440 | SEC_LOAD
9441 | SEC_DATA
9442 | SEC_HAS_CONTENTS
9443 | SEC_READONLY));
9444
9445 if (get_elf_backend_data (stdoutput)->s->elfclass == ELFCLASS64)
9446 {
9447 align_size_1 = 7;
9448 alignment = 3;
9449 }
9450 else
9451 {
9452 align_size_1 = 3;
9453 alignment = 2;
9454 }
9455
9456 bfd_set_section_alignment (sec, alignment);
9457 elf_section_type (sec) = SHT_NOTE;
9458
9459 /* GNU_PROPERTY_X86_ISA_1_USED: 4-byte type + 4-byte data size
9460 + 4-byte data */
9461 isa_1_descsz_raw = 4 + 4 + 4;
9462 /* Align GNU_PROPERTY_X86_ISA_1_USED. */
9463 isa_1_descsz = (isa_1_descsz_raw + align_size_1) & ~align_size_1;
9464
9465 feature_2_descsz_raw = isa_1_descsz;
9466 /* GNU_PROPERTY_X86_FEATURE_2_USED: 4-byte type + 4-byte data size
9467 + 4-byte data */
9468 feature_2_descsz_raw += 4 + 4 + 4;
9469 /* Align GNU_PROPERTY_X86_FEATURE_2_USED. */
9470 feature_2_descsz = ((feature_2_descsz_raw + align_size_1)
9471 & ~align_size_1);
9472
9473 descsz = feature_2_descsz;
9474 /* Section size: n_namsz + n_descsz + n_type + n_name + n_descsz. */
9475 p = frag_more (4 + 4 + 4 + 4 + descsz);
9476
9477 /* Write n_namsz. */
9478 md_number_to_chars (p, (valueT) 4, 4);
9479
9480 /* Write n_descsz. */
9481 md_number_to_chars (p + 4, (valueT) descsz, 4);
9482
9483 /* Write n_type. */
9484 md_number_to_chars (p + 4 * 2, (valueT) NT_GNU_PROPERTY_TYPE_0, 4);
9485
9486 /* Write n_name. */
9487 memcpy (p + 4 * 3, "GNU", 4);
9488
9489 /* Write 4-byte type. */
9490 md_number_to_chars (p + 4 * 4,
9491 (valueT) GNU_PROPERTY_X86_ISA_1_USED, 4);
9492
9493 /* Write 4-byte data size. */
9494 md_number_to_chars (p + 4 * 5, (valueT) 4, 4);
9495
9496 /* Write 4-byte data. */
9497 md_number_to_chars (p + 4 * 6, (valueT) x86_isa_1_used, 4);
9498
9499 /* Zero out paddings. */
9500 padding = isa_1_descsz - isa_1_descsz_raw;
9501 if (padding)
9502 memset (p + 4 * 7, 0, padding);
9503
9504 /* Write 4-byte type. */
9505 md_number_to_chars (p + isa_1_descsz + 4 * 4,
9506 (valueT) GNU_PROPERTY_X86_FEATURE_2_USED, 4);
9507
9508 /* Write 4-byte data size. */
9509 md_number_to_chars (p + isa_1_descsz + 4 * 5, (valueT) 4, 4);
9510
9511 /* Write 4-byte data. */
9512 md_number_to_chars (p + isa_1_descsz + 4 * 6,
9513 (valueT) x86_feature_2_used, 4);
9514
9515 /* Zero out paddings. */
9516 padding = feature_2_descsz - feature_2_descsz_raw;
9517 if (padding)
9518 memset (p + isa_1_descsz + 4 * 7, 0, padding);
9519
9520 /* We probably can't restore the current segment, for there likely
9521 isn't one yet... */
9522 if (seg && subseg)
9523 subseg_set (seg, subseg);
9524 }
9525
9526 bool
9527 x86_support_sframe_p (void)
9528 {
9529 /* At this time, SFrame stack trace is supported for AMD64 ABI only. */
9530 return (x86_elf_abi == X86_64_ABI);
9531 }
9532
9533 bool
9534 x86_sframe_ra_tracking_p (void)
9535 {
9536 /* In AMD64, return address is always stored on the stack at a fixed offset
9537 from the CFA (provided via x86_sframe_cfa_ra_offset ()).
9538 Do not track explicitly via an SFrame Frame Row Entry. */
9539 return false;
9540 }
9541
9542 offsetT
9543 x86_sframe_cfa_ra_offset (void)
9544 {
9545 gas_assert (x86_elf_abi == X86_64_ABI);
9546 return (offsetT) -8;
9547 }
9548
9549 unsigned char
9550 x86_sframe_get_abi_arch (void)
9551 {
9552 unsigned char sframe_abi_arch = 0;
9553
9554 if (x86_support_sframe_p ())
9555 {
9556 gas_assert (!target_big_endian);
9557 sframe_abi_arch = SFRAME_ABI_AMD64_ENDIAN_LITTLE;
9558 }
9559
9560 return sframe_abi_arch;
9561 }
9562
9563 #endif
9564
9565 static unsigned int
9566 encoding_length (const fragS *start_frag, offsetT start_off,
9567 const char *frag_now_ptr)
9568 {
9569 unsigned int len = 0;
9570
9571 if (start_frag != frag_now)
9572 {
9573 const fragS *fr = start_frag;
9574
9575 do {
9576 len += fr->fr_fix;
9577 fr = fr->fr_next;
9578 } while (fr && fr != frag_now);
9579 }
9580
9581 return len - start_off + (frag_now_ptr - frag_now->fr_literal);
9582 }
9583
9584 /* Return 1 for test, and, cmp, add, sub, inc and dec which may
9585 be macro-fused with conditional jumps.
9586 NB: If TEST/AND/CMP/ADD/SUB/INC/DEC is of RIP relative address,
9587 or is one of the following format:
9588
9589 cmp m, imm
9590 add m, imm
9591 sub m, imm
9592 test m, imm
9593 and m, imm
9594 inc m
9595 dec m
9596
9597 it is unfusible. */
9598
9599 static int
9600 maybe_fused_with_jcc_p (enum mf_cmp_kind* mf_cmp_p)
9601 {
9602 /* No RIP address. */
9603 if (i.base_reg && i.base_reg->reg_num == RegIP)
9604 return 0;
9605
9606 /* No opcodes outside of base encoding space. */
9607 if (i.tm.opcode_space != SPACE_BASE)
9608 return 0;
9609
9610 /* add, sub without add/sub m, imm. */
9611 if (i.tm.base_opcode <= 5
9612 || (i.tm.base_opcode >= 0x28 && i.tm.base_opcode <= 0x2d)
9613 || ((i.tm.base_opcode | 3) == 0x83
9614 && (i.tm.extension_opcode == 0x5
9615 || i.tm.extension_opcode == 0x0)))
9616 {
9617 *mf_cmp_p = mf_cmp_alu_cmp;
9618 return !(i.mem_operands && i.imm_operands);
9619 }
9620
9621 /* and without and m, imm. */
9622 if ((i.tm.base_opcode >= 0x20 && i.tm.base_opcode <= 0x25)
9623 || ((i.tm.base_opcode | 3) == 0x83
9624 && i.tm.extension_opcode == 0x4))
9625 {
9626 *mf_cmp_p = mf_cmp_test_and;
9627 return !(i.mem_operands && i.imm_operands);
9628 }
9629
9630 /* test without test m imm. */
9631 if ((i.tm.base_opcode | 1) == 0x85
9632 || (i.tm.base_opcode | 1) == 0xa9
9633 || ((i.tm.base_opcode | 1) == 0xf7
9634 && i.tm.extension_opcode == 0))
9635 {
9636 *mf_cmp_p = mf_cmp_test_and;
9637 return !(i.mem_operands && i.imm_operands);
9638 }
9639
9640 /* cmp without cmp m, imm. */
9641 if ((i.tm.base_opcode >= 0x38 && i.tm.base_opcode <= 0x3d)
9642 || ((i.tm.base_opcode | 3) == 0x83
9643 && (i.tm.extension_opcode == 0x7)))
9644 {
9645 *mf_cmp_p = mf_cmp_alu_cmp;
9646 return !(i.mem_operands && i.imm_operands);
9647 }
9648
9649 /* inc, dec without inc/dec m. */
9650 if ((is_cpu (&i.tm, CpuNo64)
9651 && (i.tm.base_opcode | 0xf) == 0x4f)
9652 || ((i.tm.base_opcode | 1) == 0xff
9653 && i.tm.extension_opcode <= 0x1))
9654 {
9655 *mf_cmp_p = mf_cmp_incdec;
9656 return !i.mem_operands;
9657 }
9658
9659 return 0;
9660 }
9661
9662 /* Return 1 if a FUSED_JCC_PADDING frag should be generated. */
9663
9664 static int
9665 add_fused_jcc_padding_frag_p (enum mf_cmp_kind* mf_cmp_p)
9666 {
9667 /* NB: Don't work with COND_JUMP86 without i386. */
9668 if (!align_branch_power
9669 || now_seg == absolute_section
9670 || !cpu_arch_flags.bitfield.cpui386
9671 || !(align_branch & align_branch_fused_bit))
9672 return 0;
9673
9674 if (maybe_fused_with_jcc_p (mf_cmp_p))
9675 {
9676 if (last_insn.kind == last_insn_other
9677 || last_insn.seg != now_seg)
9678 return 1;
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 }
9684
9685 return 0;
9686 }
9687
9688 /* Return 1 if a BRANCH_PREFIX frag should be generated. */
9689
9690 static int
9691 add_branch_prefix_frag_p (void)
9692 {
9693 /* NB: Don't work with COND_JUMP86 without i386. Don't add prefix
9694 to PadLock instructions since they include prefixes in opcode. */
9695 if (!align_branch_power
9696 || !align_branch_prefix_size
9697 || now_seg == absolute_section
9698 || is_cpu (&i.tm, CpuPadLock)
9699 || !cpu_arch_flags.bitfield.cpui386)
9700 return 0;
9701
9702 /* Don't add prefix if it is a prefix or there is no operand in case
9703 that segment prefix is special. */
9704 if (!i.operands || i.tm.opcode_modifier.isprefix)
9705 return 0;
9706
9707 if (last_insn.kind == last_insn_other
9708 || last_insn.seg != now_seg)
9709 return 1;
9710
9711 if (flag_debug)
9712 as_warn_where (last_insn.file, last_insn.line,
9713 _("`%s` skips -malign-branch-boundary on `%s`"),
9714 last_insn.name, insn_name (&i.tm));
9715
9716 return 0;
9717 }
9718
9719 /* Return 1 if a BRANCH_PADDING frag should be generated. */
9720
9721 static int
9722 add_branch_padding_frag_p (enum align_branch_kind *branch_p,
9723 enum mf_jcc_kind *mf_jcc_p)
9724 {
9725 int add_padding;
9726
9727 /* NB: Don't work with COND_JUMP86 without i386. */
9728 if (!align_branch_power
9729 || now_seg == absolute_section
9730 || !cpu_arch_flags.bitfield.cpui386
9731 || i.tm.opcode_space != SPACE_BASE)
9732 return 0;
9733
9734 add_padding = 0;
9735
9736 /* Check for jcc and direct jmp. */
9737 if (i.tm.opcode_modifier.jump == JUMP)
9738 {
9739 if (i.tm.base_opcode == JUMP_PC_RELATIVE)
9740 {
9741 *branch_p = align_branch_jmp;
9742 add_padding = align_branch & align_branch_jmp_bit;
9743 }
9744 else
9745 {
9746 /* Because J<cc> and JN<cc> share same group in macro-fusible table,
9747 igore the lowest bit. */
9748 *mf_jcc_p = (i.tm.base_opcode & 0x0e) >> 1;
9749 *branch_p = align_branch_jcc;
9750 if ((align_branch & align_branch_jcc_bit))
9751 add_padding = 1;
9752 }
9753 }
9754 else if ((i.tm.base_opcode | 1) == 0xc3)
9755 {
9756 /* Near ret. */
9757 *branch_p = align_branch_ret;
9758 if ((align_branch & align_branch_ret_bit))
9759 add_padding = 1;
9760 }
9761 else
9762 {
9763 /* Check for indirect jmp, direct and indirect calls. */
9764 if (i.tm.base_opcode == 0xe8)
9765 {
9766 /* Direct call. */
9767 *branch_p = align_branch_call;
9768 if ((align_branch & align_branch_call_bit))
9769 add_padding = 1;
9770 }
9771 else if (i.tm.base_opcode == 0xff
9772 && (i.tm.extension_opcode == 2
9773 || i.tm.extension_opcode == 4))
9774 {
9775 /* Indirect call and jmp. */
9776 *branch_p = align_branch_indirect;
9777 if ((align_branch & align_branch_indirect_bit))
9778 add_padding = 1;
9779 }
9780
9781 if (add_padding
9782 && i.disp_operands
9783 && tls_get_addr
9784 && (i.op[0].disps->X_op == O_symbol
9785 || (i.op[0].disps->X_op == O_subtract
9786 && i.op[0].disps->X_op_symbol == GOT_symbol)))
9787 {
9788 symbolS *s = i.op[0].disps->X_add_symbol;
9789 /* No padding to call to global or undefined tls_get_addr. */
9790 if ((S_IS_EXTERNAL (s) || !S_IS_DEFINED (s))
9791 && strcmp (S_GET_NAME (s), tls_get_addr) == 0)
9792 return 0;
9793 }
9794 }
9795
9796 if (add_padding
9797 && last_insn.kind != last_insn_other
9798 && last_insn.seg == now_seg)
9799 {
9800 if (flag_debug)
9801 as_warn_where (last_insn.file, last_insn.line,
9802 _("`%s` skips -malign-branch-boundary on `%s`"),
9803 last_insn.name, insn_name (&i.tm));
9804 return 0;
9805 }
9806
9807 return add_padding;
9808 }
9809
9810 static void
9811 output_insn (void)
9812 {
9813 fragS *insn_start_frag;
9814 offsetT insn_start_off;
9815 fragS *fragP = NULL;
9816 enum align_branch_kind branch = align_branch_none;
9817 /* The initializer is arbitrary just to avoid uninitialized error.
9818 it's actually either assigned in add_branch_padding_frag_p
9819 or never be used. */
9820 enum mf_jcc_kind mf_jcc = mf_jcc_jo;
9821
9822 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9823 if (IS_ELF && x86_used_note && now_seg != absolute_section)
9824 {
9825 if ((i.xstate & xstate_tmm) == xstate_tmm
9826 || is_cpu (&i.tm, CpuAMX_TILE))
9827 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_TMM;
9828
9829 if (is_cpu (&i.tm, Cpu8087)
9830 || is_cpu (&i.tm, Cpu287)
9831 || is_cpu (&i.tm, Cpu387)
9832 || is_cpu (&i.tm, Cpu687)
9833 || is_cpu (&i.tm, CpuFISTTP))
9834 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X87;
9835
9836 if ((i.xstate & xstate_mmx)
9837 || i.tm.mnem_off == MN_emms
9838 || i.tm.mnem_off == MN_femms)
9839 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MMX;
9840
9841 if (i.index_reg)
9842 {
9843 if (i.index_reg->reg_type.bitfield.zmmword)
9844 i.xstate |= xstate_zmm;
9845 else if (i.index_reg->reg_type.bitfield.ymmword)
9846 i.xstate |= xstate_ymm;
9847 else if (i.index_reg->reg_type.bitfield.xmmword)
9848 i.xstate |= xstate_xmm;
9849 }
9850
9851 /* vzeroall / vzeroupper */
9852 if (i.tm.base_opcode == 0x77 && is_cpu (&i.tm, CpuAVX))
9853 i.xstate |= xstate_ymm;
9854
9855 if ((i.xstate & xstate_xmm)
9856 /* ldmxcsr / stmxcsr / vldmxcsr / vstmxcsr */
9857 || (i.tm.base_opcode == 0xae
9858 && (is_cpu (&i.tm, CpuSSE)
9859 || is_cpu (&i.tm, CpuAVX)))
9860 || is_cpu (&i.tm, CpuWideKL)
9861 || is_cpu (&i.tm, CpuKL))
9862 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XMM;
9863
9864 if ((i.xstate & xstate_ymm) == xstate_ymm)
9865 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_YMM;
9866 if ((i.xstate & xstate_zmm) == xstate_zmm)
9867 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_ZMM;
9868 if (i.mask.reg || (i.xstate & xstate_mask) == xstate_mask)
9869 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MASK;
9870 if (is_cpu (&i.tm, CpuFXSR))
9871 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_FXSR;
9872 if (is_cpu (&i.tm, CpuXsave))
9873 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVE;
9874 if (is_cpu (&i.tm, CpuXsaveopt))
9875 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT;
9876 if (is_cpu (&i.tm, CpuXSAVEC))
9877 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEC;
9878
9879 if (x86_feature_2_used
9880 || is_cpu (&i.tm, CpuCMOV)
9881 || is_cpu (&i.tm, CpuSYSCALL)
9882 || i.tm.mnem_off == MN_cmpxchg8b)
9883 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_BASELINE;
9884 if (is_cpu (&i.tm, CpuSSE3)
9885 || is_cpu (&i.tm, CpuSSSE3)
9886 || is_cpu (&i.tm, CpuSSE4_1)
9887 || is_cpu (&i.tm, CpuSSE4_2)
9888 || is_cpu (&i.tm, CpuCX16)
9889 || is_cpu (&i.tm, CpuPOPCNT)
9890 /* LAHF-SAHF insns in 64-bit mode. */
9891 || (flag_code == CODE_64BIT
9892 && (i.tm.base_opcode | 1) == 0x9f
9893 && i.tm.opcode_space == SPACE_BASE))
9894 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V2;
9895 if (is_cpu (&i.tm, CpuAVX)
9896 || is_cpu (&i.tm, CpuAVX2)
9897 /* Any VEX encoded insns execpt for AVX512F, AVX512BW, AVX512DQ,
9898 XOP, FMA4, LPW, TBM, and AMX. */
9899 || (i.tm.opcode_modifier.vex
9900 && !is_cpu (&i.tm, CpuAVX512F)
9901 && !is_cpu (&i.tm, CpuAVX512BW)
9902 && !is_cpu (&i.tm, CpuAVX512DQ)
9903 && !is_cpu (&i.tm, CpuXOP)
9904 && !is_cpu (&i.tm, CpuFMA4)
9905 && !is_cpu (&i.tm, CpuLWP)
9906 && !is_cpu (&i.tm, CpuTBM)
9907 && !(x86_feature_2_used & GNU_PROPERTY_X86_FEATURE_2_TMM))
9908 || is_cpu (&i.tm, CpuF16C)
9909 || is_cpu (&i.tm, CpuFMA)
9910 || is_cpu (&i.tm, CpuLZCNT)
9911 || is_cpu (&i.tm, CpuMovbe)
9912 || is_cpu (&i.tm, CpuXSAVES)
9913 || (x86_feature_2_used
9914 & (GNU_PROPERTY_X86_FEATURE_2_XSAVE
9915 | GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT
9916 | GNU_PROPERTY_X86_FEATURE_2_XSAVEC)) != 0)
9917 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V3;
9918 if (is_cpu (&i.tm, CpuAVX512F)
9919 || is_cpu (&i.tm, CpuAVX512BW)
9920 || is_cpu (&i.tm, CpuAVX512DQ)
9921 || is_cpu (&i.tm, CpuAVX512VL)
9922 /* Any EVEX encoded insns except for AVX512ER, AVX512PF,
9923 AVX512-4FMAPS, and AVX512-4VNNIW. */
9924 || (i.tm.opcode_modifier.evex
9925 && !is_cpu (&i.tm, CpuAVX512ER)
9926 && !is_cpu (&i.tm, CpuAVX512PF)
9927 && !is_cpu (&i.tm, CpuAVX512_4FMAPS)
9928 && !is_cpu (&i.tm, CpuAVX512_4VNNIW)))
9929 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V4;
9930 }
9931 #endif
9932
9933 /* Tie dwarf2 debug info to the address at the start of the insn.
9934 We can't do this after the insn has been output as the current
9935 frag may have been closed off. eg. by frag_var. */
9936 dwarf2_emit_insn (0);
9937
9938 insn_start_frag = frag_now;
9939 insn_start_off = frag_now_fix ();
9940
9941 if (add_branch_padding_frag_p (&branch, &mf_jcc))
9942 {
9943 char *p;
9944 /* Branch can be 8 bytes. Leave some room for prefixes. */
9945 unsigned int max_branch_padding_size = 14;
9946
9947 /* Align section to boundary. */
9948 record_alignment (now_seg, align_branch_power);
9949
9950 /* Make room for padding. */
9951 frag_grow (max_branch_padding_size);
9952
9953 /* Start of the padding. */
9954 p = frag_more (0);
9955
9956 fragP = frag_now;
9957
9958 frag_var (rs_machine_dependent, max_branch_padding_size, 0,
9959 ENCODE_RELAX_STATE (BRANCH_PADDING, 0),
9960 NULL, 0, p);
9961
9962 fragP->tc_frag_data.mf_type = mf_jcc;
9963 fragP->tc_frag_data.branch_type = branch;
9964 fragP->tc_frag_data.max_bytes = max_branch_padding_size;
9965 }
9966
9967 if (!cpu_arch_flags.bitfield.cpui386 && (flag_code != CODE_16BIT)
9968 && !pre_386_16bit_warned)
9969 {
9970 as_warn (_("use .code16 to ensure correct addressing mode"));
9971 pre_386_16bit_warned = true;
9972 }
9973
9974 /* Output jumps. */
9975 if (i.tm.opcode_modifier.jump == JUMP)
9976 output_branch ();
9977 else if (i.tm.opcode_modifier.jump == JUMP_BYTE
9978 || i.tm.opcode_modifier.jump == JUMP_DWORD)
9979 output_jump ();
9980 else if (i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT)
9981 output_interseg_jump ();
9982 else
9983 {
9984 /* Output normal instructions here. */
9985 char *p;
9986 unsigned char *q;
9987 unsigned int j;
9988 enum mf_cmp_kind mf_cmp;
9989
9990 if (avoid_fence
9991 && (i.tm.base_opcode == 0xaee8
9992 || i.tm.base_opcode == 0xaef0
9993 || i.tm.base_opcode == 0xaef8))
9994 {
9995 /* Encode lfence, mfence, and sfence as
9996 f0 83 04 24 00 lock addl $0x0, (%{re}sp). */
9997 if (flag_code == CODE_16BIT)
9998 as_bad (_("Cannot convert `%s' in 16-bit mode"), insn_name (&i.tm));
9999 else if (omit_lock_prefix)
10000 as_bad (_("Cannot convert `%s' with `-momit-lock-prefix=yes' in effect"),
10001 insn_name (&i.tm));
10002 else if (now_seg != absolute_section)
10003 {
10004 offsetT val = 0x240483f0ULL;
10005
10006 p = frag_more (5);
10007 md_number_to_chars (p, val, 5);
10008 }
10009 else
10010 abs_section_offset += 5;
10011 return;
10012 }
10013
10014 /* Some processors fail on LOCK prefix. This options makes
10015 assembler ignore LOCK prefix and serves as a workaround. */
10016 if (omit_lock_prefix)
10017 {
10018 if (i.tm.base_opcode == LOCK_PREFIX_OPCODE
10019 && i.tm.opcode_modifier.isprefix)
10020 return;
10021 i.prefix[LOCK_PREFIX] = 0;
10022 }
10023
10024 if (branch)
10025 /* Skip if this is a branch. */
10026 ;
10027 else if (add_fused_jcc_padding_frag_p (&mf_cmp))
10028 {
10029 /* Make room for padding. */
10030 frag_grow (MAX_FUSED_JCC_PADDING_SIZE);
10031 p = frag_more (0);
10032
10033 fragP = frag_now;
10034
10035 frag_var (rs_machine_dependent, MAX_FUSED_JCC_PADDING_SIZE, 0,
10036 ENCODE_RELAX_STATE (FUSED_JCC_PADDING, 0),
10037 NULL, 0, p);
10038
10039 fragP->tc_frag_data.mf_type = mf_cmp;
10040 fragP->tc_frag_data.branch_type = align_branch_fused;
10041 fragP->tc_frag_data.max_bytes = MAX_FUSED_JCC_PADDING_SIZE;
10042 }
10043 else if (add_branch_prefix_frag_p ())
10044 {
10045 unsigned int max_prefix_size = align_branch_prefix_size;
10046
10047 /* Make room for padding. */
10048 frag_grow (max_prefix_size);
10049 p = frag_more (0);
10050
10051 fragP = frag_now;
10052
10053 frag_var (rs_machine_dependent, max_prefix_size, 0,
10054 ENCODE_RELAX_STATE (BRANCH_PREFIX, 0),
10055 NULL, 0, p);
10056
10057 fragP->tc_frag_data.max_bytes = max_prefix_size;
10058 }
10059
10060 /* Since the VEX/EVEX prefix contains the implicit prefix, we
10061 don't need the explicit prefix. */
10062 if (!is_any_vex_encoding (&i.tm))
10063 {
10064 switch (i.tm.opcode_modifier.opcodeprefix)
10065 {
10066 case PREFIX_0X66:
10067 add_prefix (0x66);
10068 break;
10069 case PREFIX_0XF2:
10070 add_prefix (0xf2);
10071 break;
10072 case PREFIX_0XF3:
10073 if (!is_cpu (&i.tm, CpuPadLock)
10074 || (i.prefix[REP_PREFIX] != 0xf3))
10075 add_prefix (0xf3);
10076 break;
10077 case PREFIX_NONE:
10078 switch (i.opcode_length)
10079 {
10080 case 2:
10081 break;
10082 case 1:
10083 /* Check for pseudo prefixes. */
10084 if (!i.tm.opcode_modifier.isprefix || i.tm.base_opcode)
10085 break;
10086 as_bad_where (insn_start_frag->fr_file,
10087 insn_start_frag->fr_line,
10088 _("pseudo prefix without instruction"));
10089 return;
10090 default:
10091 abort ();
10092 }
10093 break;
10094 default:
10095 abort ();
10096 }
10097
10098 #if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
10099 /* For x32, add a dummy REX_OPCODE prefix for mov/add with
10100 R_X86_64_GOTTPOFF relocation so that linker can safely
10101 perform IE->LE optimization. A dummy REX_OPCODE prefix
10102 is also needed for lea with R_X86_64_GOTPC32_TLSDESC
10103 relocation for GDesc -> IE/LE optimization. */
10104 if (x86_elf_abi == X86_64_X32_ABI
10105 && i.operands == 2
10106 && (i.reloc[0] == BFD_RELOC_X86_64_GOTTPOFF
10107 || i.reloc[0] == BFD_RELOC_X86_64_GOTPC32_TLSDESC)
10108 && i.prefix[REX_PREFIX] == 0)
10109 add_prefix (REX_OPCODE);
10110 #endif
10111
10112 /* The prefix bytes. */
10113 for (j = ARRAY_SIZE (i.prefix), q = i.prefix; j > 0; j--, q++)
10114 if (*q)
10115 frag_opcode_byte (*q);
10116 }
10117 else
10118 {
10119 for (j = 0, q = i.prefix; j < ARRAY_SIZE (i.prefix); j++, q++)
10120 if (*q)
10121 switch (j)
10122 {
10123 case SEG_PREFIX:
10124 case ADDR_PREFIX:
10125 frag_opcode_byte (*q);
10126 break;
10127 default:
10128 /* There should be no other prefixes for instructions
10129 with VEX prefix. */
10130 abort ();
10131 }
10132
10133 /* For EVEX instructions i.vrex should become 0 after
10134 build_evex_prefix. For VEX instructions upper 16 registers
10135 aren't available, so VREX should be 0. */
10136 if (i.vrex)
10137 abort ();
10138 /* Now the VEX prefix. */
10139 if (now_seg != absolute_section)
10140 {
10141 p = frag_more (i.vex.length);
10142 for (j = 0; j < i.vex.length; j++)
10143 p[j] = i.vex.bytes[j];
10144 }
10145 else
10146 abs_section_offset += i.vex.length;
10147 }
10148
10149 /* Now the opcode; be careful about word order here! */
10150 j = i.opcode_length;
10151 if (!i.vex.length)
10152 switch (i.tm.opcode_space)
10153 {
10154 case SPACE_BASE:
10155 break;
10156 case SPACE_0F:
10157 ++j;
10158 break;
10159 case SPACE_0F38:
10160 case SPACE_0F3A:
10161 j += 2;
10162 break;
10163 default:
10164 abort ();
10165 }
10166
10167 if (now_seg == absolute_section)
10168 abs_section_offset += j;
10169 else if (j == 1)
10170 {
10171 FRAG_APPEND_1_CHAR (i.tm.base_opcode);
10172 }
10173 else
10174 {
10175 p = frag_more (j);
10176 if (!i.vex.length
10177 && i.tm.opcode_space != SPACE_BASE)
10178 {
10179 *p++ = 0x0f;
10180 if (i.tm.opcode_space != SPACE_0F)
10181 *p++ = i.tm.opcode_space == SPACE_0F38
10182 ? 0x38 : 0x3a;
10183 }
10184
10185 switch (i.opcode_length)
10186 {
10187 case 2:
10188 /* Put out high byte first: can't use md_number_to_chars! */
10189 *p++ = (i.tm.base_opcode >> 8) & 0xff;
10190 /* Fall through. */
10191 case 1:
10192 *p = i.tm.base_opcode & 0xff;
10193 break;
10194 default:
10195 abort ();
10196 break;
10197 }
10198
10199 }
10200
10201 /* Now the modrm byte and sib byte (if present). */
10202 if (i.tm.opcode_modifier.modrm)
10203 {
10204 frag_opcode_byte ((i.rm.regmem << 0)
10205 | (i.rm.reg << 3)
10206 | (i.rm.mode << 6));
10207 /* If i.rm.regmem == ESP (4)
10208 && i.rm.mode != (Register mode)
10209 && not 16 bit
10210 ==> need second modrm byte. */
10211 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
10212 && i.rm.mode != 3
10213 && !(i.base_reg && i.base_reg->reg_type.bitfield.word))
10214 frag_opcode_byte ((i.sib.base << 0)
10215 | (i.sib.index << 3)
10216 | (i.sib.scale << 6));
10217 }
10218
10219 if (i.disp_operands)
10220 output_disp (insn_start_frag, insn_start_off);
10221
10222 if (i.imm_operands)
10223 output_imm (insn_start_frag, insn_start_off);
10224
10225 /*
10226 * frag_now_fix () returning plain abs_section_offset when we're in the
10227 * absolute section, and abs_section_offset not getting updated as data
10228 * gets added to the frag breaks the logic below.
10229 */
10230 if (now_seg != absolute_section)
10231 {
10232 j = encoding_length (insn_start_frag, insn_start_off, frag_more (0));
10233 if (j > 15)
10234 as_warn (_("instruction length of %u bytes exceeds the limit of 15"),
10235 j);
10236 else if (fragP)
10237 {
10238 /* NB: Don't add prefix with GOTPC relocation since
10239 output_disp() above depends on the fixed encoding
10240 length. Can't add prefix with TLS relocation since
10241 it breaks TLS linker optimization. */
10242 unsigned int max = i.has_gotpc_tls_reloc ? 0 : 15 - j;
10243 /* Prefix count on the current instruction. */
10244 unsigned int count = i.vex.length;
10245 unsigned int k;
10246 for (k = 0; k < ARRAY_SIZE (i.prefix); k++)
10247 /* REX byte is encoded in VEX/EVEX prefix. */
10248 if (i.prefix[k] && (k != REX_PREFIX || !i.vex.length))
10249 count++;
10250
10251 /* Count prefixes for extended opcode maps. */
10252 if (!i.vex.length)
10253 switch (i.tm.opcode_space)
10254 {
10255 case SPACE_BASE:
10256 break;
10257 case SPACE_0F:
10258 count++;
10259 break;
10260 case SPACE_0F38:
10261 case SPACE_0F3A:
10262 count += 2;
10263 break;
10264 default:
10265 abort ();
10266 }
10267
10268 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
10269 == BRANCH_PREFIX)
10270 {
10271 /* Set the maximum prefix size in BRANCH_PREFIX
10272 frag. */
10273 if (fragP->tc_frag_data.max_bytes > max)
10274 fragP->tc_frag_data.max_bytes = max;
10275 if (fragP->tc_frag_data.max_bytes > count)
10276 fragP->tc_frag_data.max_bytes -= count;
10277 else
10278 fragP->tc_frag_data.max_bytes = 0;
10279 }
10280 else
10281 {
10282 /* Remember the maximum prefix size in FUSED_JCC_PADDING
10283 frag. */
10284 unsigned int max_prefix_size;
10285 if (align_branch_prefix_size > max)
10286 max_prefix_size = max;
10287 else
10288 max_prefix_size = align_branch_prefix_size;
10289 if (max_prefix_size > count)
10290 fragP->tc_frag_data.max_prefix_length
10291 = max_prefix_size - count;
10292 }
10293
10294 /* Use existing segment prefix if possible. Use CS
10295 segment prefix in 64-bit mode. In 32-bit mode, use SS
10296 segment prefix with ESP/EBP base register and use DS
10297 segment prefix without ESP/EBP base register. */
10298 if (i.prefix[SEG_PREFIX])
10299 fragP->tc_frag_data.default_prefix = i.prefix[SEG_PREFIX];
10300 else if (flag_code == CODE_64BIT)
10301 fragP->tc_frag_data.default_prefix = CS_PREFIX_OPCODE;
10302 else if (i.base_reg
10303 && (i.base_reg->reg_num == 4
10304 || i.base_reg->reg_num == 5))
10305 fragP->tc_frag_data.default_prefix = SS_PREFIX_OPCODE;
10306 else
10307 fragP->tc_frag_data.default_prefix = DS_PREFIX_OPCODE;
10308 }
10309 }
10310 }
10311
10312 /* NB: Don't work with COND_JUMP86 without i386. */
10313 if (align_branch_power
10314 && now_seg != absolute_section
10315 && cpu_arch_flags.bitfield.cpui386)
10316 {
10317 /* Terminate each frag so that we can add prefix and check for
10318 fused jcc. */
10319 frag_wane (frag_now);
10320 frag_new (0);
10321 }
10322
10323 #ifdef DEBUG386
10324 if (flag_debug)
10325 {
10326 pi ("" /*line*/, &i);
10327 }
10328 #endif /* DEBUG386 */
10329 }
10330
10331 /* Return the size of the displacement operand N. */
10332
10333 static int
10334 disp_size (unsigned int n)
10335 {
10336 int size = 4;
10337
10338 if (i.types[n].bitfield.disp64)
10339 size = 8;
10340 else if (i.types[n].bitfield.disp8)
10341 size = 1;
10342 else if (i.types[n].bitfield.disp16)
10343 size = 2;
10344 return size;
10345 }
10346
10347 /* Return the size of the immediate operand N. */
10348
10349 static int
10350 imm_size (unsigned int n)
10351 {
10352 int size = 4;
10353 if (i.types[n].bitfield.imm64)
10354 size = 8;
10355 else if (i.types[n].bitfield.imm8 || i.types[n].bitfield.imm8s)
10356 size = 1;
10357 else if (i.types[n].bitfield.imm16)
10358 size = 2;
10359 return size;
10360 }
10361
10362 static void
10363 output_disp (fragS *insn_start_frag, offsetT insn_start_off)
10364 {
10365 char *p;
10366 unsigned int n;
10367
10368 for (n = 0; n < i.operands; n++)
10369 {
10370 if (operand_type_check (i.types[n], disp))
10371 {
10372 int size = disp_size (n);
10373
10374 if (now_seg == absolute_section)
10375 abs_section_offset += size;
10376 else if (i.op[n].disps->X_op == O_constant)
10377 {
10378 offsetT val = i.op[n].disps->X_add_number;
10379
10380 val = offset_in_range (val >> (size == 1 ? i.memshift : 0),
10381 size);
10382 p = frag_more (size);
10383 md_number_to_chars (p, val, size);
10384 }
10385 else
10386 {
10387 enum bfd_reloc_code_real reloc_type;
10388 bool pcrel = (i.flags[n] & Operand_PCrel) != 0;
10389 bool sign = (flag_code == CODE_64BIT && size == 4
10390 && (!want_disp32 (&i.tm)
10391 || (i.tm.opcode_modifier.jump && !i.jumpabsolute
10392 && !i.types[n].bitfield.baseindex)))
10393 || pcrel;
10394 fixS *fixP;
10395
10396 /* We can't have 8 bit displacement here. */
10397 gas_assert (!i.types[n].bitfield.disp8);
10398
10399 /* The PC relative address is computed relative
10400 to the instruction boundary, so in case immediate
10401 fields follows, we need to adjust the value. */
10402 if (pcrel && i.imm_operands)
10403 {
10404 unsigned int n1;
10405 int sz = 0;
10406
10407 for (n1 = 0; n1 < i.operands; n1++)
10408 if (operand_type_check (i.types[n1], imm))
10409 {
10410 /* Only one immediate is allowed for PC
10411 relative address, except with .insn. */
10412 gas_assert (sz == 0 || dot_insn ());
10413 sz += imm_size (n1);
10414 }
10415 /* We should find at least one immediate. */
10416 gas_assert (sz != 0);
10417 i.op[n].disps->X_add_number -= sz;
10418 }
10419
10420 p = frag_more (size);
10421 reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
10422 if (GOT_symbol
10423 && GOT_symbol == i.op[n].disps->X_add_symbol
10424 && (((reloc_type == BFD_RELOC_32
10425 || reloc_type == BFD_RELOC_X86_64_32S
10426 || (reloc_type == BFD_RELOC_64
10427 && object_64bit))
10428 && (i.op[n].disps->X_op == O_symbol
10429 || (i.op[n].disps->X_op == O_add
10430 && ((symbol_get_value_expression
10431 (i.op[n].disps->X_op_symbol)->X_op)
10432 == O_subtract))))
10433 || reloc_type == BFD_RELOC_32_PCREL))
10434 {
10435 if (!object_64bit)
10436 {
10437 reloc_type = BFD_RELOC_386_GOTPC;
10438 i.has_gotpc_tls_reloc = true;
10439 i.op[n].disps->X_add_number +=
10440 encoding_length (insn_start_frag, insn_start_off, p);
10441 }
10442 else if (reloc_type == BFD_RELOC_64)
10443 reloc_type = BFD_RELOC_X86_64_GOTPC64;
10444 else
10445 /* Don't do the adjustment for x86-64, as there
10446 the pcrel addressing is relative to the _next_
10447 insn, and that is taken care of in other code. */
10448 reloc_type = BFD_RELOC_X86_64_GOTPC32;
10449 }
10450 else if (align_branch_power)
10451 {
10452 switch (reloc_type)
10453 {
10454 case BFD_RELOC_386_TLS_GD:
10455 case BFD_RELOC_386_TLS_LDM:
10456 case BFD_RELOC_386_TLS_IE:
10457 case BFD_RELOC_386_TLS_IE_32:
10458 case BFD_RELOC_386_TLS_GOTIE:
10459 case BFD_RELOC_386_TLS_GOTDESC:
10460 case BFD_RELOC_386_TLS_DESC_CALL:
10461 case BFD_RELOC_X86_64_TLSGD:
10462 case BFD_RELOC_X86_64_TLSLD:
10463 case BFD_RELOC_X86_64_GOTTPOFF:
10464 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
10465 case BFD_RELOC_X86_64_TLSDESC_CALL:
10466 i.has_gotpc_tls_reloc = true;
10467 default:
10468 break;
10469 }
10470 }
10471 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal,
10472 size, i.op[n].disps, pcrel,
10473 reloc_type);
10474
10475 if (flag_code == CODE_64BIT && size == 4 && pcrel
10476 && !i.prefix[ADDR_PREFIX])
10477 fixP->fx_signed = 1;
10478
10479 /* Check for "call/jmp *mem", "mov mem, %reg",
10480 "test %reg, mem" and "binop mem, %reg" where binop
10481 is one of adc, add, and, cmp, or, sbb, sub, xor
10482 instructions without data prefix. Always generate
10483 R_386_GOT32X for "sym*GOT" operand in 32-bit mode. */
10484 if (i.prefix[DATA_PREFIX] == 0
10485 && (generate_relax_relocations
10486 || (!object_64bit
10487 && i.rm.mode == 0
10488 && i.rm.regmem == 5))
10489 && (i.rm.mode == 2
10490 || (i.rm.mode == 0 && i.rm.regmem == 5))
10491 && i.tm.opcode_space == SPACE_BASE
10492 && ((i.operands == 1
10493 && i.tm.base_opcode == 0xff
10494 && (i.rm.reg == 2 || i.rm.reg == 4))
10495 || (i.operands == 2
10496 && (i.tm.base_opcode == 0x8b
10497 || i.tm.base_opcode == 0x85
10498 || (i.tm.base_opcode & ~0x38) == 0x03))))
10499 {
10500 if (object_64bit)
10501 {
10502 fixP->fx_tcbit = i.rex != 0;
10503 if (i.base_reg
10504 && (i.base_reg->reg_num == RegIP))
10505 fixP->fx_tcbit2 = 1;
10506 }
10507 else
10508 fixP->fx_tcbit2 = 1;
10509 }
10510 }
10511 }
10512 }
10513 }
10514
10515 static void
10516 output_imm (fragS *insn_start_frag, offsetT insn_start_off)
10517 {
10518 char *p;
10519 unsigned int n;
10520
10521 for (n = 0; n < i.operands; n++)
10522 {
10523 if (operand_type_check (i.types[n], imm))
10524 {
10525 int size = imm_size (n);
10526
10527 if (now_seg == absolute_section)
10528 abs_section_offset += size;
10529 else if (i.op[n].imms->X_op == O_constant)
10530 {
10531 offsetT val;
10532
10533 val = offset_in_range (i.op[n].imms->X_add_number,
10534 size);
10535 p = frag_more (size);
10536 md_number_to_chars (p, val, size);
10537 }
10538 else
10539 {
10540 /* Not absolute_section.
10541 Need a 32-bit fixup (don't support 8bit
10542 non-absolute imms). Try to support other
10543 sizes ... */
10544 enum bfd_reloc_code_real reloc_type;
10545 int sign;
10546
10547 if (i.types[n].bitfield.imm32s
10548 && (i.suffix == QWORD_MNEM_SUFFIX
10549 || (!i.suffix && i.tm.opcode_modifier.no_lsuf)
10550 || (i.prefix[REX_PREFIX] & REX_W)
10551 || dot_insn ()))
10552 sign = 1;
10553 else
10554 sign = 0;
10555
10556 p = frag_more (size);
10557 reloc_type = reloc (size, 0, sign, i.reloc[n]);
10558
10559 /* This is tough to explain. We end up with this one if we
10560 * have operands that look like
10561 * "_GLOBAL_OFFSET_TABLE_+[.-.L284]". The goal here is to
10562 * obtain the absolute address of the GOT, and it is strongly
10563 * preferable from a performance point of view to avoid using
10564 * a runtime relocation for this. The actual sequence of
10565 * instructions often look something like:
10566 *
10567 * call .L66
10568 * .L66:
10569 * popl %ebx
10570 * addl $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
10571 *
10572 * The call and pop essentially return the absolute address
10573 * of the label .L66 and store it in %ebx. The linker itself
10574 * will ultimately change the first operand of the addl so
10575 * that %ebx points to the GOT, but to keep things simple, the
10576 * .o file must have this operand set so that it generates not
10577 * the absolute address of .L66, but the absolute address of
10578 * itself. This allows the linker itself simply treat a GOTPC
10579 * relocation as asking for a pcrel offset to the GOT to be
10580 * added in, and the addend of the relocation is stored in the
10581 * operand field for the instruction itself.
10582 *
10583 * Our job here is to fix the operand so that it would add
10584 * the correct offset so that %ebx would point to itself. The
10585 * thing that is tricky is that .-.L66 will point to the
10586 * beginning of the instruction, so we need to further modify
10587 * the operand so that it will point to itself. There are
10588 * other cases where you have something like:
10589 *
10590 * .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
10591 *
10592 * and here no correction would be required. Internally in
10593 * the assembler we treat operands of this form as not being
10594 * pcrel since the '.' is explicitly mentioned, and I wonder
10595 * whether it would simplify matters to do it this way. Who
10596 * knows. In earlier versions of the PIC patches, the
10597 * pcrel_adjust field was used to store the correction, but
10598 * since the expression is not pcrel, I felt it would be
10599 * confusing to do it this way. */
10600
10601 if ((reloc_type == BFD_RELOC_32
10602 || reloc_type == BFD_RELOC_X86_64_32S
10603 || reloc_type == BFD_RELOC_64)
10604 && GOT_symbol
10605 && GOT_symbol == i.op[n].imms->X_add_symbol
10606 && (i.op[n].imms->X_op == O_symbol
10607 || (i.op[n].imms->X_op == O_add
10608 && ((symbol_get_value_expression
10609 (i.op[n].imms->X_op_symbol)->X_op)
10610 == O_subtract))))
10611 {
10612 if (!object_64bit)
10613 reloc_type = BFD_RELOC_386_GOTPC;
10614 else if (size == 4)
10615 reloc_type = BFD_RELOC_X86_64_GOTPC32;
10616 else if (size == 8)
10617 reloc_type = BFD_RELOC_X86_64_GOTPC64;
10618 i.has_gotpc_tls_reloc = true;
10619 i.op[n].imms->X_add_number +=
10620 encoding_length (insn_start_frag, insn_start_off, p);
10621 }
10622 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
10623 i.op[n].imms, 0, reloc_type);
10624 }
10625 }
10626 }
10627 }
10628 \f
10629 /* x86_cons_fix_new is called via the expression parsing code when a
10630 reloc is needed. We use this hook to get the correct .got reloc. */
10631 static int cons_sign = -1;
10632
10633 void
10634 x86_cons_fix_new (fragS *frag, unsigned int off, unsigned int len,
10635 expressionS *exp, bfd_reloc_code_real_type r)
10636 {
10637 r = reloc (len, 0, cons_sign, r);
10638
10639 #ifdef TE_PE
10640 if (exp->X_op == O_secrel)
10641 {
10642 exp->X_op = O_symbol;
10643 r = BFD_RELOC_32_SECREL;
10644 }
10645 else if (exp->X_op == O_secidx)
10646 r = BFD_RELOC_16_SECIDX;
10647 #endif
10648
10649 fix_new_exp (frag, off, len, exp, 0, r);
10650 }
10651
10652 /* Export the ABI address size for use by TC_ADDRESS_BYTES for the
10653 purpose of the `.dc.a' internal pseudo-op. */
10654
10655 int
10656 x86_address_bytes (void)
10657 {
10658 if ((stdoutput->arch_info->mach & bfd_mach_x64_32))
10659 return 4;
10660 return stdoutput->arch_info->bits_per_address / 8;
10661 }
10662
10663 #if (!(defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined (OBJ_MACH_O)) \
10664 || defined (LEX_AT)) && !defined (TE_PE)
10665 # define lex_got(reloc, adjust, types) NULL
10666 #else
10667 /* Parse operands of the form
10668 <symbol>@GOTOFF+<nnn>
10669 and similar .plt or .got references.
10670
10671 If we find one, set up the correct relocation in RELOC and copy the
10672 input string, minus the `@GOTOFF' into a malloc'd buffer for
10673 parsing by the calling routine. Return this buffer, and if ADJUST
10674 is non-null set it to the length of the string we removed from the
10675 input line. Otherwise return NULL. */
10676 static char *
10677 lex_got (enum bfd_reloc_code_real *rel,
10678 int *adjust,
10679 i386_operand_type *types)
10680 {
10681 /* Some of the relocations depend on the size of what field is to
10682 be relocated. But in our callers i386_immediate and i386_displacement
10683 we don't yet know the operand size (this will be set by insn
10684 matching). Hence we record the word32 relocation here,
10685 and adjust the reloc according to the real size in reloc(). */
10686 static const struct
10687 {
10688 const char *str;
10689 int len;
10690 const enum bfd_reloc_code_real rel[2];
10691 const i386_operand_type types64;
10692 bool need_GOT_symbol;
10693 }
10694 gotrel[] =
10695 {
10696
10697 #define OPERAND_TYPE_IMM32_32S_DISP32 { .bitfield = \
10698 { .imm32 = 1, .imm32s = 1, .disp32 = 1 } }
10699 #define OPERAND_TYPE_IMM32_32S_64_DISP32 { .bitfield = \
10700 { .imm32 = 1, .imm32s = 1, .imm64 = 1, .disp32 = 1 } }
10701 #define OPERAND_TYPE_IMM32_32S_64_DISP32_64 { .bitfield = \
10702 { .imm32 = 1, .imm32s = 1, .imm64 = 1, .disp32 = 1, .disp64 = 1 } }
10703 #define OPERAND_TYPE_IMM64_DISP64 { .bitfield = \
10704 { .imm64 = 1, .disp64 = 1 } }
10705
10706 #ifndef TE_PE
10707 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
10708 { STRING_COMMA_LEN ("SIZE"), { BFD_RELOC_SIZE32,
10709 BFD_RELOC_SIZE32 },
10710 { .bitfield = { .imm32 = 1, .imm64 = 1 } }, false },
10711 #endif
10712 { STRING_COMMA_LEN ("PLTOFF"), { _dummy_first_bfd_reloc_code_real,
10713 BFD_RELOC_X86_64_PLTOFF64 },
10714 { .bitfield = { .imm64 = 1 } }, true },
10715 { STRING_COMMA_LEN ("PLT"), { BFD_RELOC_386_PLT32,
10716 BFD_RELOC_X86_64_PLT32 },
10717 OPERAND_TYPE_IMM32_32S_DISP32, false },
10718 { STRING_COMMA_LEN ("GOTPLT"), { _dummy_first_bfd_reloc_code_real,
10719 BFD_RELOC_X86_64_GOTPLT64 },
10720 OPERAND_TYPE_IMM64_DISP64, true },
10721 { STRING_COMMA_LEN ("GOTOFF"), { BFD_RELOC_386_GOTOFF,
10722 BFD_RELOC_X86_64_GOTOFF64 },
10723 OPERAND_TYPE_IMM64_DISP64, true },
10724 { STRING_COMMA_LEN ("GOTPCREL"), { _dummy_first_bfd_reloc_code_real,
10725 BFD_RELOC_X86_64_GOTPCREL },
10726 OPERAND_TYPE_IMM32_32S_DISP32, true },
10727 { STRING_COMMA_LEN ("TLSGD"), { BFD_RELOC_386_TLS_GD,
10728 BFD_RELOC_X86_64_TLSGD },
10729 OPERAND_TYPE_IMM32_32S_DISP32, true },
10730 { STRING_COMMA_LEN ("TLSLDM"), { BFD_RELOC_386_TLS_LDM,
10731 _dummy_first_bfd_reloc_code_real },
10732 OPERAND_TYPE_NONE, true },
10733 { STRING_COMMA_LEN ("TLSLD"), { _dummy_first_bfd_reloc_code_real,
10734 BFD_RELOC_X86_64_TLSLD },
10735 OPERAND_TYPE_IMM32_32S_DISP32, true },
10736 { STRING_COMMA_LEN ("GOTTPOFF"), { BFD_RELOC_386_TLS_IE_32,
10737 BFD_RELOC_X86_64_GOTTPOFF },
10738 OPERAND_TYPE_IMM32_32S_DISP32, true },
10739 { STRING_COMMA_LEN ("TPOFF"), { BFD_RELOC_386_TLS_LE_32,
10740 BFD_RELOC_X86_64_TPOFF32 },
10741 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
10742 { STRING_COMMA_LEN ("NTPOFF"), { BFD_RELOC_386_TLS_LE,
10743 _dummy_first_bfd_reloc_code_real },
10744 OPERAND_TYPE_NONE, true },
10745 { STRING_COMMA_LEN ("DTPOFF"), { BFD_RELOC_386_TLS_LDO_32,
10746 BFD_RELOC_X86_64_DTPOFF32 },
10747 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
10748 { STRING_COMMA_LEN ("GOTNTPOFF"),{ BFD_RELOC_386_TLS_GOTIE,
10749 _dummy_first_bfd_reloc_code_real },
10750 OPERAND_TYPE_NONE, true },
10751 { STRING_COMMA_LEN ("INDNTPOFF"),{ BFD_RELOC_386_TLS_IE,
10752 _dummy_first_bfd_reloc_code_real },
10753 OPERAND_TYPE_NONE, true },
10754 { STRING_COMMA_LEN ("GOT"), { BFD_RELOC_386_GOT32,
10755 BFD_RELOC_X86_64_GOT32 },
10756 OPERAND_TYPE_IMM32_32S_64_DISP32, true },
10757 { STRING_COMMA_LEN ("TLSDESC"), { BFD_RELOC_386_TLS_GOTDESC,
10758 BFD_RELOC_X86_64_GOTPC32_TLSDESC },
10759 OPERAND_TYPE_IMM32_32S_DISP32, true },
10760 { STRING_COMMA_LEN ("TLSCALL"), { BFD_RELOC_386_TLS_DESC_CALL,
10761 BFD_RELOC_X86_64_TLSDESC_CALL },
10762 OPERAND_TYPE_IMM32_32S_DISP32, true },
10763 #else /* TE_PE */
10764 { STRING_COMMA_LEN ("SECREL32"), { BFD_RELOC_32_SECREL,
10765 BFD_RELOC_32_SECREL },
10766 OPERAND_TYPE_IMM32_32S_64_DISP32_64, false },
10767 #endif
10768
10769 #undef OPERAND_TYPE_IMM32_32S_DISP32
10770 #undef OPERAND_TYPE_IMM32_32S_64_DISP32
10771 #undef OPERAND_TYPE_IMM32_32S_64_DISP32_64
10772 #undef OPERAND_TYPE_IMM64_DISP64
10773
10774 };
10775 char *cp;
10776 unsigned int j;
10777
10778 #if defined (OBJ_MAYBE_ELF) && !defined (TE_PE)
10779 if (!IS_ELF)
10780 return NULL;
10781 #endif
10782
10783 for (cp = input_line_pointer; *cp != '@'; cp++)
10784 if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
10785 return NULL;
10786
10787 for (j = 0; j < ARRAY_SIZE (gotrel); j++)
10788 {
10789 int len = gotrel[j].len;
10790 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
10791 {
10792 if (gotrel[j].rel[object_64bit] != 0)
10793 {
10794 int first, second;
10795 char *tmpbuf, *past_reloc;
10796
10797 *rel = gotrel[j].rel[object_64bit];
10798
10799 if (types)
10800 {
10801 if (flag_code != CODE_64BIT)
10802 {
10803 types->bitfield.imm32 = 1;
10804 types->bitfield.disp32 = 1;
10805 }
10806 else
10807 *types = gotrel[j].types64;
10808 }
10809
10810 if (gotrel[j].need_GOT_symbol && GOT_symbol == NULL)
10811 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
10812
10813 /* The length of the first part of our input line. */
10814 first = cp - input_line_pointer;
10815
10816 /* The second part goes from after the reloc token until
10817 (and including) an end_of_line char or comma. */
10818 past_reloc = cp + 1 + len;
10819 cp = past_reloc;
10820 while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
10821 ++cp;
10822 second = cp + 1 - past_reloc;
10823
10824 /* Allocate and copy string. The trailing NUL shouldn't
10825 be necessary, but be safe. */
10826 tmpbuf = XNEWVEC (char, first + second + 2);
10827 memcpy (tmpbuf, input_line_pointer, first);
10828 if (second != 0 && *past_reloc != ' ')
10829 /* Replace the relocation token with ' ', so that
10830 errors like foo@GOTOFF1 will be detected. */
10831 tmpbuf[first++] = ' ';
10832 else
10833 /* Increment length by 1 if the relocation token is
10834 removed. */
10835 len++;
10836 if (adjust)
10837 *adjust = len;
10838 memcpy (tmpbuf + first, past_reloc, second);
10839 tmpbuf[first + second] = '\0';
10840 return tmpbuf;
10841 }
10842
10843 as_bad (_("@%s reloc is not supported with %d-bit output format"),
10844 gotrel[j].str, 1 << (5 + object_64bit));
10845 return NULL;
10846 }
10847 }
10848
10849 /* Might be a symbol version string. Don't as_bad here. */
10850 return NULL;
10851 }
10852 #endif
10853
10854 bfd_reloc_code_real_type
10855 x86_cons (expressionS *exp, int size)
10856 {
10857 bfd_reloc_code_real_type got_reloc = NO_RELOC;
10858
10859 intel_syntax = -intel_syntax;
10860 exp->X_md = 0;
10861 expr_mode = expr_operator_none;
10862
10863 #if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
10864 && !defined (LEX_AT)) \
10865 || defined (TE_PE)
10866 if (size == 4 || (object_64bit && size == 8))
10867 {
10868 /* Handle @GOTOFF and the like in an expression. */
10869 char *save;
10870 char *gotfree_input_line;
10871 int adjust = 0;
10872
10873 save = input_line_pointer;
10874 gotfree_input_line = lex_got (&got_reloc, &adjust, NULL);
10875 if (gotfree_input_line)
10876 input_line_pointer = gotfree_input_line;
10877
10878 expression (exp);
10879
10880 if (gotfree_input_line)
10881 {
10882 /* expression () has merrily parsed up to the end of line,
10883 or a comma - in the wrong buffer. Transfer how far
10884 input_line_pointer has moved to the right buffer. */
10885 input_line_pointer = (save
10886 + (input_line_pointer - gotfree_input_line)
10887 + adjust);
10888 free (gotfree_input_line);
10889 if (exp->X_op == O_constant
10890 || exp->X_op == O_absent
10891 || exp->X_op == O_illegal
10892 || exp->X_op == O_register
10893 || exp->X_op == O_big)
10894 {
10895 char c = *input_line_pointer;
10896 *input_line_pointer = 0;
10897 as_bad (_("missing or invalid expression `%s'"), save);
10898 *input_line_pointer = c;
10899 }
10900 else if ((got_reloc == BFD_RELOC_386_PLT32
10901 || got_reloc == BFD_RELOC_X86_64_PLT32)
10902 && exp->X_op != O_symbol)
10903 {
10904 char c = *input_line_pointer;
10905 *input_line_pointer = 0;
10906 as_bad (_("invalid PLT expression `%s'"), save);
10907 *input_line_pointer = c;
10908 }
10909 }
10910 }
10911 else
10912 #endif
10913 expression (exp);
10914
10915 intel_syntax = -intel_syntax;
10916
10917 if (intel_syntax)
10918 i386_intel_simplify (exp);
10919
10920 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
10921 if (size <= 4 && expr_mode == expr_operator_present
10922 && exp->X_op == O_constant && !object_64bit)
10923 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
10924
10925 return got_reloc;
10926 }
10927
10928 static void
10929 signed_cons (int size)
10930 {
10931 if (object_64bit)
10932 cons_sign = 1;
10933 cons (size);
10934 cons_sign = -1;
10935 }
10936
10937 static void
10938 s_insn (int dummy ATTRIBUTE_UNUSED)
10939 {
10940 char mnemonic[MAX_MNEM_SIZE], *line = input_line_pointer, *ptr;
10941 char *saved_ilp = find_end_of_line (line, false), saved_char;
10942 const char *end;
10943 unsigned int j;
10944 valueT val;
10945 bool vex = false, xop = false, evex = false;
10946 static const templates tt = { &i.tm, &i.tm + 1 };
10947
10948 init_globals ();
10949
10950 saved_char = *saved_ilp;
10951 *saved_ilp = 0;
10952
10953 end = parse_insn (line, mnemonic, true);
10954 if (end == NULL)
10955 {
10956 bad:
10957 *saved_ilp = saved_char;
10958 ignore_rest_of_line ();
10959 i.tm.mnem_off = 0;
10960 return;
10961 }
10962 line += end - line;
10963
10964 current_templates = &tt;
10965 i.tm.mnem_off = MN__insn;
10966 i.tm.extension_opcode = None;
10967
10968 if (startswith (line, "VEX")
10969 && (line[3] == '.' || is_space_char (line[3])))
10970 {
10971 vex = true;
10972 line += 3;
10973 }
10974 else if (startswith (line, "XOP") && ISDIGIT (line[3]))
10975 {
10976 char *e;
10977 unsigned long n = strtoul (line + 3, &e, 16);
10978
10979 if (e == line + 5 && n >= 0x08 && n <= 0x1f
10980 && (*e == '.' || is_space_char (*e)))
10981 {
10982 xop = true;
10983 /* Arrange for build_vex_prefix() to emit 0x8f. */
10984 i.tm.opcode_space = SPACE_XOP08;
10985 i.insn_opcode_space = n;
10986 line = e;
10987 }
10988 }
10989 else if (startswith (line, "EVEX")
10990 && (line[4] == '.' || is_space_char (line[4])))
10991 {
10992 evex = true;
10993 line += 4;
10994 }
10995
10996 if (vex || xop
10997 ? i.vec_encoding == vex_encoding_evex
10998 : evex
10999 ? i.vec_encoding == vex_encoding_vex
11000 || i.vec_encoding == vex_encoding_vex3
11001 : i.vec_encoding != vex_encoding_default)
11002 {
11003 as_bad (_("pseudo-prefix conflicts with encoding specifier"));
11004 goto bad;
11005 }
11006
11007 if (line > end && i.vec_encoding == vex_encoding_default)
11008 i.vec_encoding = evex ? vex_encoding_evex : vex_encoding_vex;
11009
11010 if (i.vec_encoding != vex_encoding_default)
11011 {
11012 /* Only address size and segment override prefixes are permitted with
11013 VEX/XOP/EVEX encodings. */
11014 const unsigned char *p = i.prefix;
11015
11016 for (j = 0; j < ARRAY_SIZE (i.prefix); ++j, ++p)
11017 {
11018 if (!*p)
11019 continue;
11020
11021 switch (j)
11022 {
11023 case SEG_PREFIX:
11024 case ADDR_PREFIX:
11025 break;
11026 default:
11027 as_bad (_("illegal prefix used with VEX/XOP/EVEX"));
11028 goto bad;
11029 }
11030 }
11031 }
11032
11033 if (line > end && *line == '.')
11034 {
11035 /* Length specifier (VEX.L, XOP.L, EVEX.L'L). */
11036 switch (line[1])
11037 {
11038 case 'L':
11039 switch (line[2])
11040 {
11041 case '0':
11042 if (evex)
11043 i.tm.opcode_modifier.evex = EVEX128;
11044 else
11045 i.tm.opcode_modifier.vex = VEX128;
11046 break;
11047
11048 case '1':
11049 if (evex)
11050 i.tm.opcode_modifier.evex = EVEX256;
11051 else
11052 i.tm.opcode_modifier.vex = VEX256;
11053 break;
11054
11055 case '2':
11056 if (evex)
11057 i.tm.opcode_modifier.evex = EVEX512;
11058 break;
11059
11060 case '3':
11061 if (evex)
11062 i.tm.opcode_modifier.evex = EVEX_L3;
11063 break;
11064
11065 case 'I':
11066 if (line[3] == 'G')
11067 {
11068 if (evex)
11069 i.tm.opcode_modifier.evex = EVEXLIG;
11070 else
11071 i.tm.opcode_modifier.vex = VEXScalar; /* LIG */
11072 ++line;
11073 }
11074 break;
11075 }
11076
11077 if (i.tm.opcode_modifier.vex || i.tm.opcode_modifier.evex)
11078 line += 3;
11079 break;
11080
11081 case '1':
11082 if (line[2] == '2' && line[3] == '8')
11083 {
11084 if (evex)
11085 i.tm.opcode_modifier.evex = EVEX128;
11086 else
11087 i.tm.opcode_modifier.vex = VEX128;
11088 line += 4;
11089 }
11090 break;
11091
11092 case '2':
11093 if (line[2] == '5' && line[3] == '6')
11094 {
11095 if (evex)
11096 i.tm.opcode_modifier.evex = EVEX256;
11097 else
11098 i.tm.opcode_modifier.vex = VEX256;
11099 line += 4;
11100 }
11101 break;
11102
11103 case '5':
11104 if (evex && line[2] == '1' && line[3] == '2')
11105 {
11106 i.tm.opcode_modifier.evex = EVEX512;
11107 line += 4;
11108 }
11109 break;
11110 }
11111 }
11112
11113 if (line > end && *line == '.')
11114 {
11115 /* embedded prefix (VEX.pp, XOP.pp, EVEX.pp). */
11116 switch (line[1])
11117 {
11118 case 'N':
11119 if (line[2] == 'P')
11120 line += 3;
11121 break;
11122
11123 case '6':
11124 if (line[2] == '6')
11125 {
11126 i.tm.opcode_modifier.opcodeprefix = PREFIX_0X66;
11127 line += 3;
11128 }
11129 break;
11130
11131 case 'F': case 'f':
11132 if (line[2] == '3')
11133 {
11134 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
11135 line += 3;
11136 }
11137 else if (line[2] == '2')
11138 {
11139 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF2;
11140 line += 3;
11141 }
11142 break;
11143 }
11144 }
11145
11146 if (line > end && !xop && *line == '.')
11147 {
11148 /* Encoding space (VEX.mmmmm, EVEX.mmmm). */
11149 switch (line[1])
11150 {
11151 case '0':
11152 if (TOUPPER (line[2]) != 'F')
11153 break;
11154 if (line[3] == '.' || is_space_char (line[3]))
11155 {
11156 i.insn_opcode_space = SPACE_0F;
11157 line += 3;
11158 }
11159 else if (line[3] == '3'
11160 && (line[4] == '8' || TOUPPER (line[4]) == 'A')
11161 && (line[5] == '.' || is_space_char (line[5])))
11162 {
11163 i.insn_opcode_space = line[4] == '8' ? SPACE_0F38 : SPACE_0F3A;
11164 line += 5;
11165 }
11166 break;
11167
11168 case 'M':
11169 if (ISDIGIT (line[2]) && line[2] != '0')
11170 {
11171 char *e;
11172 unsigned long n = strtoul (line + 2, &e, 10);
11173
11174 if (n <= (evex ? 15 : 31)
11175 && (*e == '.' || is_space_char (*e)))
11176 {
11177 i.insn_opcode_space = n;
11178 line = e;
11179 }
11180 }
11181 break;
11182 }
11183 }
11184
11185 if (line > end && *line == '.' && line[1] == 'W')
11186 {
11187 /* VEX.W, XOP.W, EVEX.W */
11188 switch (line[2])
11189 {
11190 case '0':
11191 i.tm.opcode_modifier.vexw = VEXW0;
11192 break;
11193
11194 case '1':
11195 i.tm.opcode_modifier.vexw = VEXW1;
11196 break;
11197
11198 case 'I':
11199 if (line[3] == 'G')
11200 {
11201 i.tm.opcode_modifier.vexw = VEXWIG;
11202 ++line;
11203 }
11204 break;
11205 }
11206
11207 if (i.tm.opcode_modifier.vexw)
11208 line += 3;
11209 }
11210
11211 if (line > end && *line && !is_space_char (*line))
11212 {
11213 /* Improve diagnostic a little. */
11214 if (*line == '.' && line[1] && !is_space_char (line[1]))
11215 ++line;
11216 goto done;
11217 }
11218
11219 /* Before processing the opcode expression, find trailing "+r" or
11220 "/<digit>" specifiers. */
11221 for (ptr = line; ; ++ptr)
11222 {
11223 unsigned long n;
11224 char *e;
11225
11226 ptr = strpbrk (ptr, "+/,");
11227 if (ptr == NULL || *ptr == ',')
11228 break;
11229
11230 if (*ptr == '+' && ptr[1] == 'r'
11231 && (ptr[2] == ',' || (is_space_char (ptr[2]) && ptr[3] == ',')))
11232 {
11233 *ptr = ' ';
11234 ptr[1] = ' ';
11235 i.short_form = true;
11236 break;
11237 }
11238
11239 if (*ptr == '/' && ISDIGIT (ptr[1])
11240 && (n = strtoul (ptr + 1, &e, 8)) < 8
11241 && e == ptr + 2
11242 && (ptr[2] == ',' || (is_space_char (ptr[2]) && ptr[3] == ',')))
11243 {
11244 *ptr = ' ';
11245 ptr[1] = ' ';
11246 i.tm.extension_opcode = n;
11247 i.tm.opcode_modifier.modrm = 1;
11248 break;
11249 }
11250 }
11251
11252 input_line_pointer = line;
11253 val = get_absolute_expression ();
11254 line = input_line_pointer;
11255
11256 if (i.short_form && (val & 7))
11257 as_warn ("`+r' assumes low three opcode bits to be clear");
11258
11259 for (j = 1; j < sizeof(val); ++j)
11260 if (!(val >> (j * 8)))
11261 break;
11262
11263 /* Trim off a prefix if present. */
11264 if (j > 1 && !vex && !xop && !evex)
11265 {
11266 uint8_t byte = val >> ((j - 1) * 8);
11267
11268 switch (byte)
11269 {
11270 case DATA_PREFIX_OPCODE:
11271 case REPE_PREFIX_OPCODE:
11272 case REPNE_PREFIX_OPCODE:
11273 if (!add_prefix (byte))
11274 goto bad;
11275 val &= ((uint64_t)1 << (--j * 8)) - 1;
11276 break;
11277 }
11278 }
11279
11280 /* Trim off encoding space. */
11281 if (j > 1 && !i.insn_opcode_space && (val >> ((j - 1) * 8)) == 0x0f)
11282 {
11283 uint8_t byte = val >> ((--j - 1) * 8);
11284
11285 i.insn_opcode_space = SPACE_0F;
11286 switch (byte & -(j > 1))
11287 {
11288 case 0x38:
11289 i.insn_opcode_space = SPACE_0F38;
11290 --j;
11291 break;
11292 case 0x3a:
11293 i.insn_opcode_space = SPACE_0F3A;
11294 --j;
11295 break;
11296 }
11297 i.tm.opcode_space = i.insn_opcode_space;
11298 val &= ((uint64_t)1 << (j * 8)) - 1;
11299 }
11300 if (!i.tm.opcode_space && (vex || evex))
11301 /* Arrange for build_vex_prefix() to properly emit 0xC4/0xC5.
11302 Also avoid hitting abort() there or in build_evex_prefix(). */
11303 i.tm.opcode_space = i.insn_opcode_space == SPACE_0F ? SPACE_0F
11304 : SPACE_0F38;
11305
11306 if (j > 2)
11307 {
11308 as_bad (_("opcode residual (%#"PRIx64") too wide"), (uint64_t) val);
11309 goto bad;
11310 }
11311 i.opcode_length = j;
11312
11313 /* Handle operands, if any. */
11314 if (*line == ',')
11315 {
11316 i386_operand_type combined;
11317 expressionS *disp_exp = NULL;
11318 bool changed;
11319
11320 i.memshift = -1;
11321
11322 ptr = parse_operands (line + 1, &i386_mnemonics[MN__insn]);
11323 this_operand = -1;
11324 if (!ptr)
11325 goto bad;
11326 line = ptr;
11327
11328 if (!i.operands)
11329 {
11330 as_bad (_("expecting operand after ','; got nothing"));
11331 goto done;
11332 }
11333
11334 if (i.mem_operands > 1)
11335 {
11336 as_bad (_("too many memory references for `%s'"),
11337 &i386_mnemonics[MN__insn]);
11338 goto done;
11339 }
11340
11341 /* No need to distinguish vex_encoding_evex and vex_encoding_evex512. */
11342 if (i.vec_encoding == vex_encoding_evex512)
11343 i.vec_encoding = vex_encoding_evex;
11344
11345 /* Are we to emit ModR/M encoding? */
11346 if (!i.short_form
11347 && (i.mem_operands
11348 || i.reg_operands > (i.vec_encoding != vex_encoding_default)
11349 || i.tm.extension_opcode != None))
11350 i.tm.opcode_modifier.modrm = 1;
11351
11352 if (!i.tm.opcode_modifier.modrm
11353 && (i.reg_operands
11354 > i.short_form + 0U + (i.vec_encoding != vex_encoding_default)
11355 || i.mem_operands))
11356 {
11357 as_bad (_("too many register/memory operands"));
11358 goto done;
11359 }
11360
11361 /* Enforce certain constraints on operands. */
11362 switch (i.reg_operands + i.mem_operands
11363 + (i.tm.extension_opcode != None))
11364 {
11365 case 0:
11366 if (i.short_form)
11367 {
11368 as_bad (_("too few register/memory operands"));
11369 goto done;
11370 }
11371 /* Fall through. */
11372 case 1:
11373 if (i.tm.opcode_modifier.modrm)
11374 {
11375 as_bad (_("too few register/memory operands"));
11376 goto done;
11377 }
11378 break;
11379
11380 case 2:
11381 break;
11382
11383 case 4:
11384 if (i.imm_operands
11385 && (i.op[0].imms->X_op != O_constant
11386 || !fits_in_imm4 (i.op[0].imms->X_add_number)))
11387 {
11388 as_bad (_("constant doesn't fit in %d bits"), evex ? 3 : 4);
11389 goto done;
11390 }
11391 /* Fall through. */
11392 case 3:
11393 if (i.vec_encoding != vex_encoding_default)
11394 {
11395 i.tm.opcode_modifier.vexvvvv = 1;
11396 break;
11397 }
11398 /* Fall through. */
11399 default:
11400 as_bad (_("too many register/memory operands"));
11401 goto done;
11402 }
11403
11404 /* Bring operands into canonical order (imm, mem, reg). */
11405 do
11406 {
11407 changed = false;
11408
11409 for (j = 1; j < i.operands; ++j)
11410 {
11411 if ((!operand_type_check (i.types[j - 1], imm)
11412 && operand_type_check (i.types[j], imm))
11413 || (i.types[j - 1].bitfield.class != ClassNone
11414 && i.types[j].bitfield.class == ClassNone))
11415 {
11416 swap_2_operands (j - 1, j);
11417 changed = true;
11418 }
11419 }
11420 }
11421 while (changed);
11422
11423 /* For Intel syntax swap the order of register operands. */
11424 if (intel_syntax)
11425 switch (i.reg_operands)
11426 {
11427 case 0:
11428 case 1:
11429 break;
11430
11431 case 4:
11432 swap_2_operands (i.imm_operands + i.mem_operands + 1, i.operands - 2);
11433 /* Fall through. */
11434 case 3:
11435 case 2:
11436 swap_2_operands (i.imm_operands + i.mem_operands, i.operands - 1);
11437 break;
11438
11439 default:
11440 abort ();
11441 }
11442
11443 /* Enforce constraints when using VSIB. */
11444 if (i.index_reg
11445 && (i.index_reg->reg_type.bitfield.xmmword
11446 || i.index_reg->reg_type.bitfield.ymmword
11447 || i.index_reg->reg_type.bitfield.zmmword))
11448 {
11449 if (i.vec_encoding == vex_encoding_default)
11450 {
11451 as_bad (_("VSIB unavailable with legacy encoding"));
11452 goto done;
11453 }
11454
11455 if (i.vec_encoding == vex_encoding_evex
11456 && i.reg_operands > 1)
11457 {
11458 /* We could allow two register operands, encoding the 2nd one in
11459 an 8-bit immediate like for 4-register-operand insns, but that
11460 would require ugly fiddling with process_operands() and/or
11461 build_modrm_byte(). */
11462 as_bad (_("too many register operands with VSIB"));
11463 goto done;
11464 }
11465
11466 i.tm.opcode_modifier.sib = 1;
11467 }
11468
11469 /* Establish operand size encoding. */
11470 operand_type_set (&combined, 0);
11471
11472 for (j = i.imm_operands; j < i.operands; ++j)
11473 {
11474 i.types[j].bitfield.instance = InstanceNone;
11475
11476 if (operand_type_check (i.types[j], disp))
11477 {
11478 i.types[j].bitfield.baseindex = 1;
11479 disp_exp = i.op[j].disps;
11480 }
11481
11482 if (evex && i.types[j].bitfield.baseindex)
11483 {
11484 unsigned int n = i.memshift;
11485
11486 if (i.types[j].bitfield.byte)
11487 n = 0;
11488 else if (i.types[j].bitfield.word)
11489 n = 1;
11490 else if (i.types[j].bitfield.dword)
11491 n = 2;
11492 else if (i.types[j].bitfield.qword)
11493 n = 3;
11494 else if (i.types[j].bitfield.xmmword)
11495 n = 4;
11496 else if (i.types[j].bitfield.ymmword)
11497 n = 5;
11498 else if (i.types[j].bitfield.zmmword)
11499 n = 6;
11500
11501 if (i.memshift < 32 && n != i.memshift)
11502 as_warn ("conflicting memory operand size specifiers");
11503 i.memshift = n;
11504 }
11505
11506 if ((i.broadcast.type || i.broadcast.bytes)
11507 && j == i.broadcast.operand)
11508 continue;
11509
11510 combined = operand_type_or (combined, i.types[j]);
11511 combined.bitfield.class = ClassNone;
11512 }
11513
11514 switch ((i.broadcast.type ? i.broadcast.type : 1)
11515 << (i.memshift < 32 ? i.memshift : 0))
11516 {
11517 case 64: combined.bitfield.zmmword = 1; break;
11518 case 32: combined.bitfield.ymmword = 1; break;
11519 case 16: combined.bitfield.xmmword = 1; break;
11520 case 8: combined.bitfield.qword = 1; break;
11521 case 4: combined.bitfield.dword = 1; break;
11522 }
11523
11524 if (i.vec_encoding == vex_encoding_default)
11525 {
11526 if (flag_code == CODE_64BIT && combined.bitfield.qword)
11527 i.rex |= REX_W;
11528 else if ((flag_code == CODE_16BIT ? combined.bitfield.dword
11529 : combined.bitfield.word)
11530 && !add_prefix (DATA_PREFIX_OPCODE))
11531 goto done;
11532 }
11533 else if (!i.tm.opcode_modifier.vexw)
11534 {
11535 if (flag_code == CODE_64BIT)
11536 {
11537 if (combined.bitfield.qword)
11538 i.tm.opcode_modifier.vexw = VEXW1;
11539 else if (combined.bitfield.dword)
11540 i.tm.opcode_modifier.vexw = VEXW0;
11541 }
11542
11543 if (!i.tm.opcode_modifier.vexw)
11544 i.tm.opcode_modifier.vexw = VEXWIG;
11545 }
11546
11547 if (vex || xop)
11548 {
11549 if (!i.tm.opcode_modifier.vex)
11550 {
11551 if (combined.bitfield.ymmword)
11552 i.tm.opcode_modifier.vex = VEX256;
11553 else if (combined.bitfield.xmmword)
11554 i.tm.opcode_modifier.vex = VEX128;
11555 }
11556 }
11557 else if (evex)
11558 {
11559 if (!i.tm.opcode_modifier.evex)
11560 {
11561 /* Do _not_ consider AVX512VL here. */
11562 if (i.rounding.type != rc_none || combined.bitfield.zmmword)
11563 i.tm.opcode_modifier.evex = EVEX512;
11564 else if (combined.bitfield.ymmword)
11565 i.tm.opcode_modifier.evex = EVEX256;
11566 else if (combined.bitfield.xmmword)
11567 i.tm.opcode_modifier.evex = EVEX128;
11568 }
11569
11570 if (i.memshift >= 32)
11571 {
11572 unsigned int n = 0;
11573
11574 switch (i.tm.opcode_modifier.evex)
11575 {
11576 case EVEX512: n = 64; break;
11577 case EVEX256: n = 32; break;
11578 case EVEX128: n = 16; break;
11579 }
11580
11581 if (i.broadcast.type)
11582 n /= i.broadcast.type;
11583
11584 if (n > 0)
11585 for (i.memshift = 0; !(n & 1); n >>= 1)
11586 ++i.memshift;
11587 else if (disp_exp != NULL && disp_exp->X_op == O_constant
11588 && disp_exp->X_add_number != 0
11589 && i.disp_encoding != disp_encoding_32bit)
11590 {
11591 if (!quiet_warnings)
11592 as_warn ("cannot determine memory operand size");
11593 i.disp_encoding = disp_encoding_32bit;
11594 }
11595 }
11596 }
11597
11598 if (i.memshift >= 32)
11599 i.memshift = 0;
11600 else if (!evex)
11601 i.vec_encoding = vex_encoding_error;
11602
11603 if (i.disp_operands && !optimize_disp (&i.tm))
11604 goto done;
11605
11606 /* Establish size for immediate operands. */
11607 for (j = 0; j < i.imm_operands; ++j)
11608 {
11609 expressionS *expP = i.op[j].imms;
11610
11611 gas_assert (operand_type_check (i.types[j], imm));
11612 operand_type_set (&i.types[j], 0);
11613
11614 if (i.imm_bits[j] > 32)
11615 i.types[j].bitfield.imm64 = 1;
11616 else if (i.imm_bits[j] > 16)
11617 {
11618 if (flag_code == CODE_64BIT && (i.flags[j] & Operand_Signed))
11619 i.types[j].bitfield.imm32s = 1;
11620 else
11621 i.types[j].bitfield.imm32 = 1;
11622 }
11623 else if (i.imm_bits[j] > 8)
11624 i.types[j].bitfield.imm16 = 1;
11625 else if (i.imm_bits[j] > 0)
11626 {
11627 if (i.flags[j] & Operand_Signed)
11628 i.types[j].bitfield.imm8s = 1;
11629 else
11630 i.types[j].bitfield.imm8 = 1;
11631 }
11632 else if (expP->X_op == O_constant)
11633 {
11634 i.types[j] = smallest_imm_type (expP->X_add_number);
11635 i.types[j].bitfield.imm1 = 0;
11636 /* Oddly enough imm_size() checks imm64 first, so the bit needs
11637 zapping since smallest_imm_type() sets it unconditionally. */
11638 if (flag_code != CODE_64BIT)
11639 {
11640 i.types[j].bitfield.imm64 = 0;
11641 i.types[j].bitfield.imm32s = 0;
11642 i.types[j].bitfield.imm32 = 1;
11643 }
11644 else if (i.types[j].bitfield.imm32 || i.types[j].bitfield.imm32s)
11645 i.types[j].bitfield.imm64 = 0;
11646 }
11647 else
11648 /* Non-constant expressions are sized heuristically. */
11649 switch (flag_code)
11650 {
11651 case CODE_64BIT: i.types[j].bitfield.imm32s = 1; break;
11652 case CODE_32BIT: i.types[j].bitfield.imm32 = 1; break;
11653 case CODE_16BIT: i.types[j].bitfield.imm16 = 1; break;
11654 }
11655 }
11656
11657 for (j = 0; j < i.operands; ++j)
11658 i.tm.operand_types[j] = i.types[j];
11659
11660 process_operands ();
11661 }
11662
11663 /* Don't set opcode until after processing operands, to avoid any
11664 potential special casing there. */
11665 i.tm.base_opcode |= val;
11666
11667 if (i.vec_encoding == vex_encoding_error
11668 || (i.vec_encoding != vex_encoding_evex
11669 ? i.broadcast.type || i.broadcast.bytes
11670 || i.rounding.type != rc_none
11671 || i.mask.reg
11672 : (i.mem_operands && i.rounding.type != rc_none)
11673 || ((i.broadcast.type || i.broadcast.bytes)
11674 && !(i.flags[i.broadcast.operand] & Operand_Mem))))
11675 {
11676 as_bad (_("conflicting .insn operands"));
11677 goto done;
11678 }
11679
11680 if (vex || xop)
11681 {
11682 if (!i.tm.opcode_modifier.vex)
11683 i.tm.opcode_modifier.vex = VEXScalar; /* LIG */
11684
11685 build_vex_prefix (NULL);
11686 i.rex &= REX_OPCODE;
11687 }
11688 else if (evex)
11689 {
11690 if (!i.tm.opcode_modifier.evex)
11691 i.tm.opcode_modifier.evex = EVEXLIG;
11692
11693 build_evex_prefix ();
11694 i.rex &= REX_OPCODE;
11695 }
11696 else if (i.rex != 0)
11697 add_prefix (REX_OPCODE | i.rex);
11698
11699 output_insn ();
11700
11701 done:
11702 *saved_ilp = saved_char;
11703 input_line_pointer = line;
11704
11705 demand_empty_rest_of_line ();
11706
11707 /* Make sure dot_insn() won't yield "true" anymore. */
11708 i.tm.mnem_off = 0;
11709 }
11710
11711 #ifdef TE_PE
11712 static void
11713 pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
11714 {
11715 expressionS exp;
11716
11717 do
11718 {
11719 expression (&exp);
11720 if (exp.X_op == O_symbol)
11721 exp.X_op = O_secrel;
11722
11723 emit_expr (&exp, 4);
11724 }
11725 while (*input_line_pointer++ == ',');
11726
11727 input_line_pointer--;
11728 demand_empty_rest_of_line ();
11729 }
11730
11731 static void
11732 pe_directive_secidx (int dummy ATTRIBUTE_UNUSED)
11733 {
11734 expressionS exp;
11735
11736 do
11737 {
11738 expression (&exp);
11739 if (exp.X_op == O_symbol)
11740 exp.X_op = O_secidx;
11741
11742 emit_expr (&exp, 2);
11743 }
11744 while (*input_line_pointer++ == ',');
11745
11746 input_line_pointer--;
11747 demand_empty_rest_of_line ();
11748 }
11749 #endif
11750
11751 /* Handle Rounding Control / SAE specifiers. */
11752
11753 static char *
11754 RC_SAE_specifier (const char *pstr)
11755 {
11756 unsigned int j;
11757
11758 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); j++)
11759 {
11760 if (!strncmp (pstr, RC_NamesTable[j].name, RC_NamesTable[j].len))
11761 {
11762 if (i.rounding.type != rc_none)
11763 {
11764 as_bad (_("duplicated `{%s}'"), RC_NamesTable[j].name);
11765 return NULL;
11766 }
11767
11768 if (i.vec_encoding == vex_encoding_default)
11769 i.vec_encoding = vex_encoding_evex512;
11770 else if (i.vec_encoding != vex_encoding_evex
11771 && i.vec_encoding != vex_encoding_evex512)
11772 return NULL;
11773
11774 i.rounding.type = RC_NamesTable[j].type;
11775
11776 return (char *)(pstr + RC_NamesTable[j].len);
11777 }
11778 }
11779
11780 return NULL;
11781 }
11782
11783 /* Handle Vector operations. */
11784
11785 static char *
11786 check_VecOperations (char *op_string)
11787 {
11788 const reg_entry *mask;
11789 const char *saved;
11790 char *end_op;
11791
11792 while (*op_string)
11793 {
11794 saved = op_string;
11795 if (*op_string == '{')
11796 {
11797 op_string++;
11798
11799 /* Check broadcasts. */
11800 if (startswith (op_string, "1to"))
11801 {
11802 unsigned int bcst_type;
11803
11804 if (i.broadcast.type)
11805 goto duplicated_vec_op;
11806
11807 op_string += 3;
11808 if (*op_string == '8')
11809 bcst_type = 8;
11810 else if (*op_string == '4')
11811 bcst_type = 4;
11812 else if (*op_string == '2')
11813 bcst_type = 2;
11814 else if (*op_string == '1'
11815 && *(op_string+1) == '6')
11816 {
11817 bcst_type = 16;
11818 op_string++;
11819 }
11820 else if (*op_string == '3'
11821 && *(op_string+1) == '2')
11822 {
11823 bcst_type = 32;
11824 op_string++;
11825 }
11826 else
11827 {
11828 as_bad (_("Unsupported broadcast: `%s'"), saved);
11829 return NULL;
11830 }
11831 op_string++;
11832
11833 if (i.vec_encoding == vex_encoding_default)
11834 i.vec_encoding = vex_encoding_evex;
11835 else if (i.vec_encoding != vex_encoding_evex
11836 && i.vec_encoding != vex_encoding_evex512)
11837 goto unknown_vec_op;
11838
11839 i.broadcast.type = bcst_type;
11840 i.broadcast.operand = this_operand;
11841
11842 /* For .insn a data size specifier may be appended. */
11843 if (dot_insn () && *op_string == ':')
11844 goto dot_insn_modifier;
11845 }
11846 /* Check .insn special cases. */
11847 else if (dot_insn () && *op_string == ':')
11848 {
11849 dot_insn_modifier:
11850 switch (op_string[1])
11851 {
11852 unsigned long n;
11853
11854 case 'd':
11855 if (i.memshift < 32)
11856 goto duplicated_vec_op;
11857
11858 n = strtoul (op_string + 2, &end_op, 0);
11859 if (n)
11860 for (i.memshift = 0; !(n & 1); n >>= 1)
11861 ++i.memshift;
11862 if (i.memshift < 32 && n == 1)
11863 op_string = end_op;
11864 break;
11865
11866 case 's': case 'u':
11867 /* This isn't really a "vector" operation, but a sign/size
11868 specifier for immediate operands of .insn. Note that AT&T
11869 syntax handles the same in i386_immediate(). */
11870 if (!intel_syntax)
11871 break;
11872
11873 if (i.imm_bits[this_operand])
11874 goto duplicated_vec_op;
11875
11876 n = strtoul (op_string + 2, &end_op, 0);
11877 if (n && n <= (flag_code == CODE_64BIT ? 64 : 32))
11878 {
11879 i.imm_bits[this_operand] = n;
11880 if (op_string[1] == 's')
11881 i.flags[this_operand] |= Operand_Signed;
11882 op_string = end_op;
11883 }
11884 break;
11885 }
11886 }
11887 /* Check masking operation. */
11888 else if ((mask = parse_register (op_string, &end_op)) != NULL)
11889 {
11890 if (mask == &bad_reg)
11891 return NULL;
11892
11893 /* k0 can't be used for write mask. */
11894 if (mask->reg_type.bitfield.class != RegMask || !mask->reg_num)
11895 {
11896 as_bad (_("`%s%s' can't be used for write mask"),
11897 register_prefix, mask->reg_name);
11898 return NULL;
11899 }
11900
11901 if (!i.mask.reg)
11902 {
11903 i.mask.reg = mask;
11904 i.mask.operand = this_operand;
11905 }
11906 else if (i.mask.reg->reg_num)
11907 goto duplicated_vec_op;
11908 else
11909 {
11910 i.mask.reg = mask;
11911
11912 /* Only "{z}" is allowed here. No need to check
11913 zeroing mask explicitly. */
11914 if (i.mask.operand != (unsigned int) this_operand)
11915 {
11916 as_bad (_("invalid write mask `%s'"), saved);
11917 return NULL;
11918 }
11919 }
11920
11921 op_string = end_op;
11922 }
11923 /* Check zeroing-flag for masking operation. */
11924 else if (*op_string == 'z')
11925 {
11926 if (!i.mask.reg)
11927 {
11928 i.mask.reg = reg_k0;
11929 i.mask.zeroing = 1;
11930 i.mask.operand = this_operand;
11931 }
11932 else
11933 {
11934 if (i.mask.zeroing)
11935 {
11936 duplicated_vec_op:
11937 as_bad (_("duplicated `%s'"), saved);
11938 return NULL;
11939 }
11940
11941 i.mask.zeroing = 1;
11942
11943 /* Only "{%k}" is allowed here. No need to check mask
11944 register explicitly. */
11945 if (i.mask.operand != (unsigned int) this_operand)
11946 {
11947 as_bad (_("invalid zeroing-masking `%s'"),
11948 saved);
11949 return NULL;
11950 }
11951 }
11952
11953 op_string++;
11954 }
11955 else if (intel_syntax
11956 && (op_string = RC_SAE_specifier (op_string)) != NULL)
11957 i.rounding.modifier = true;
11958 else
11959 goto unknown_vec_op;
11960
11961 if (*op_string != '}')
11962 {
11963 as_bad (_("missing `}' in `%s'"), saved);
11964 return NULL;
11965 }
11966 op_string++;
11967
11968 /* Strip whitespace since the addition of pseudo prefixes
11969 changed how the scrubber treats '{'. */
11970 if (is_space_char (*op_string))
11971 ++op_string;
11972
11973 continue;
11974 }
11975 unknown_vec_op:
11976 /* We don't know this one. */
11977 as_bad (_("unknown vector operation: `%s'"), saved);
11978 return NULL;
11979 }
11980
11981 if (i.mask.reg && i.mask.zeroing && !i.mask.reg->reg_num)
11982 {
11983 as_bad (_("zeroing-masking only allowed with write mask"));
11984 return NULL;
11985 }
11986
11987 return op_string;
11988 }
11989
11990 static int
11991 i386_immediate (char *imm_start)
11992 {
11993 char *save_input_line_pointer;
11994 char *gotfree_input_line;
11995 segT exp_seg = 0;
11996 expressionS *exp;
11997 i386_operand_type types;
11998
11999 operand_type_set (&types, ~0);
12000
12001 if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
12002 {
12003 as_bad (_("at most %d immediate operands are allowed"),
12004 MAX_IMMEDIATE_OPERANDS);
12005 return 0;
12006 }
12007
12008 exp = &im_expressions[i.imm_operands++];
12009 i.op[this_operand].imms = exp;
12010
12011 if (is_space_char (*imm_start))
12012 ++imm_start;
12013
12014 save_input_line_pointer = input_line_pointer;
12015 input_line_pointer = imm_start;
12016
12017 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
12018 if (gotfree_input_line)
12019 input_line_pointer = gotfree_input_line;
12020
12021 expr_mode = expr_operator_none;
12022 exp_seg = expression (exp);
12023
12024 /* For .insn immediates there may be a size specifier. */
12025 if (dot_insn () && *input_line_pointer == '{' && input_line_pointer[1] == ':'
12026 && (input_line_pointer[2] == 's' || input_line_pointer[2] == 'u'))
12027 {
12028 char *e;
12029 unsigned long n = strtoul (input_line_pointer + 3, &e, 0);
12030
12031 if (*e == '}' && n && n <= (flag_code == CODE_64BIT ? 64 : 32))
12032 {
12033 i.imm_bits[this_operand] = n;
12034 if (input_line_pointer[2] == 's')
12035 i.flags[this_operand] |= Operand_Signed;
12036 input_line_pointer = e + 1;
12037 }
12038 }
12039
12040 SKIP_WHITESPACE ();
12041 if (*input_line_pointer)
12042 as_bad (_("junk `%s' after expression"), input_line_pointer);
12043
12044 input_line_pointer = save_input_line_pointer;
12045 if (gotfree_input_line)
12046 {
12047 free (gotfree_input_line);
12048
12049 if (exp->X_op == O_constant)
12050 exp->X_op = O_illegal;
12051 }
12052
12053 if (exp_seg == reg_section)
12054 {
12055 as_bad (_("illegal immediate register operand %s"), imm_start);
12056 return 0;
12057 }
12058
12059 return i386_finalize_immediate (exp_seg, exp, types, imm_start);
12060 }
12061
12062 static int
12063 i386_finalize_immediate (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
12064 i386_operand_type types, const char *imm_start)
12065 {
12066 if (exp->X_op == O_absent || exp->X_op == O_illegal || exp->X_op == O_big)
12067 {
12068 if (imm_start)
12069 as_bad (_("missing or invalid immediate expression `%s'"),
12070 imm_start);
12071 return 0;
12072 }
12073 else if (exp->X_op == O_constant)
12074 {
12075 /* Size it properly later. */
12076 i.types[this_operand].bitfield.imm64 = 1;
12077
12078 /* If not 64bit, sign/zero extend val, to account for wraparound
12079 when !BFD64. */
12080 if (expr_mode == expr_operator_present
12081 && flag_code != CODE_64BIT && !object_64bit)
12082 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
12083 }
12084 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
12085 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
12086 && exp_seg != absolute_section
12087 && exp_seg != text_section
12088 && exp_seg != data_section
12089 && exp_seg != bss_section
12090 && exp_seg != undefined_section
12091 && !bfd_is_com_section (exp_seg))
12092 {
12093 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
12094 return 0;
12095 }
12096 #endif
12097 else
12098 {
12099 /* This is an address. The size of the address will be
12100 determined later, depending on destination register,
12101 suffix, or the default for the section. */
12102 i.types[this_operand].bitfield.imm8 = 1;
12103 i.types[this_operand].bitfield.imm16 = 1;
12104 i.types[this_operand].bitfield.imm32 = 1;
12105 i.types[this_operand].bitfield.imm32s = 1;
12106 i.types[this_operand].bitfield.imm64 = 1;
12107 i.types[this_operand] = operand_type_and (i.types[this_operand],
12108 types);
12109 }
12110
12111 return 1;
12112 }
12113
12114 static char *
12115 i386_scale (char *scale)
12116 {
12117 offsetT val;
12118 char *save = input_line_pointer;
12119
12120 input_line_pointer = scale;
12121 val = get_absolute_expression ();
12122
12123 switch (val)
12124 {
12125 case 1:
12126 i.log2_scale_factor = 0;
12127 break;
12128 case 2:
12129 i.log2_scale_factor = 1;
12130 break;
12131 case 4:
12132 i.log2_scale_factor = 2;
12133 break;
12134 case 8:
12135 i.log2_scale_factor = 3;
12136 break;
12137 default:
12138 {
12139 char sep = *input_line_pointer;
12140
12141 *input_line_pointer = '\0';
12142 as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
12143 scale);
12144 *input_line_pointer = sep;
12145 input_line_pointer = save;
12146 return NULL;
12147 }
12148 }
12149 if (i.log2_scale_factor != 0 && i.index_reg == 0)
12150 {
12151 as_warn (_("scale factor of %d without an index register"),
12152 1 << i.log2_scale_factor);
12153 i.log2_scale_factor = 0;
12154 }
12155 scale = input_line_pointer;
12156 input_line_pointer = save;
12157 return scale;
12158 }
12159
12160 static int
12161 i386_displacement (char *disp_start, char *disp_end)
12162 {
12163 expressionS *exp;
12164 segT exp_seg = 0;
12165 char *save_input_line_pointer;
12166 char *gotfree_input_line;
12167 int override;
12168 i386_operand_type bigdisp, types = anydisp;
12169 int ret;
12170
12171 if (i.disp_operands == MAX_MEMORY_OPERANDS)
12172 {
12173 as_bad (_("at most %d displacement operands are allowed"),
12174 MAX_MEMORY_OPERANDS);
12175 return 0;
12176 }
12177
12178 operand_type_set (&bigdisp, 0);
12179 if (i.jumpabsolute
12180 || i.types[this_operand].bitfield.baseindex
12181 || (current_templates->start->opcode_modifier.jump != JUMP
12182 && current_templates->start->opcode_modifier.jump != JUMP_DWORD))
12183 {
12184 i386_addressing_mode ();
12185 override = (i.prefix[ADDR_PREFIX] != 0);
12186 if (flag_code == CODE_64BIT)
12187 {
12188 bigdisp.bitfield.disp32 = 1;
12189 if (!override)
12190 bigdisp.bitfield.disp64 = 1;
12191 }
12192 else if ((flag_code == CODE_16BIT) ^ override)
12193 bigdisp.bitfield.disp16 = 1;
12194 else
12195 bigdisp.bitfield.disp32 = 1;
12196 }
12197 else
12198 {
12199 /* For PC-relative branches, the width of the displacement may be
12200 dependent upon data size, but is never dependent upon address size.
12201 Also make sure to not unintentionally match against a non-PC-relative
12202 branch template. */
12203 static templates aux_templates;
12204 const insn_template *t = current_templates->start;
12205 bool has_intel64 = false;
12206
12207 aux_templates.start = t;
12208 while (++t < current_templates->end)
12209 {
12210 if (t->opcode_modifier.jump
12211 != current_templates->start->opcode_modifier.jump)
12212 break;
12213 if ((t->opcode_modifier.isa64 >= INTEL64))
12214 has_intel64 = true;
12215 }
12216 if (t < current_templates->end)
12217 {
12218 aux_templates.end = t;
12219 current_templates = &aux_templates;
12220 }
12221
12222 override = (i.prefix[DATA_PREFIX] != 0);
12223 if (flag_code == CODE_64BIT)
12224 {
12225 if ((override || i.suffix == WORD_MNEM_SUFFIX)
12226 && (!intel64 || !has_intel64))
12227 bigdisp.bitfield.disp16 = 1;
12228 else
12229 bigdisp.bitfield.disp32 = 1;
12230 }
12231 else
12232 {
12233 if (!override)
12234 override = (i.suffix == (flag_code != CODE_16BIT
12235 ? WORD_MNEM_SUFFIX
12236 : LONG_MNEM_SUFFIX));
12237 bigdisp.bitfield.disp32 = 1;
12238 if ((flag_code == CODE_16BIT) ^ override)
12239 {
12240 bigdisp.bitfield.disp32 = 0;
12241 bigdisp.bitfield.disp16 = 1;
12242 }
12243 }
12244 }
12245 i.types[this_operand] = operand_type_or (i.types[this_operand],
12246 bigdisp);
12247
12248 exp = &disp_expressions[i.disp_operands];
12249 i.op[this_operand].disps = exp;
12250 i.disp_operands++;
12251 save_input_line_pointer = input_line_pointer;
12252 input_line_pointer = disp_start;
12253 END_STRING_AND_SAVE (disp_end);
12254
12255 #ifndef GCC_ASM_O_HACK
12256 #define GCC_ASM_O_HACK 0
12257 #endif
12258 #if GCC_ASM_O_HACK
12259 END_STRING_AND_SAVE (disp_end + 1);
12260 if (i.types[this_operand].bitfield.baseIndex
12261 && displacement_string_end[-1] == '+')
12262 {
12263 /* This hack is to avoid a warning when using the "o"
12264 constraint within gcc asm statements.
12265 For instance:
12266
12267 #define _set_tssldt_desc(n,addr,limit,type) \
12268 __asm__ __volatile__ ( \
12269 "movw %w2,%0\n\t" \
12270 "movw %w1,2+%0\n\t" \
12271 "rorl $16,%1\n\t" \
12272 "movb %b1,4+%0\n\t" \
12273 "movb %4,5+%0\n\t" \
12274 "movb $0,6+%0\n\t" \
12275 "movb %h1,7+%0\n\t" \
12276 "rorl $16,%1" \
12277 : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
12278
12279 This works great except that the output assembler ends
12280 up looking a bit weird if it turns out that there is
12281 no offset. You end up producing code that looks like:
12282
12283 #APP
12284 movw $235,(%eax)
12285 movw %dx,2+(%eax)
12286 rorl $16,%edx
12287 movb %dl,4+(%eax)
12288 movb $137,5+(%eax)
12289 movb $0,6+(%eax)
12290 movb %dh,7+(%eax)
12291 rorl $16,%edx
12292 #NO_APP
12293
12294 So here we provide the missing zero. */
12295
12296 *displacement_string_end = '0';
12297 }
12298 #endif
12299 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
12300 if (gotfree_input_line)
12301 input_line_pointer = gotfree_input_line;
12302
12303 expr_mode = expr_operator_none;
12304 exp_seg = expression (exp);
12305
12306 SKIP_WHITESPACE ();
12307 if (*input_line_pointer)
12308 as_bad (_("junk `%s' after expression"), input_line_pointer);
12309 #if GCC_ASM_O_HACK
12310 RESTORE_END_STRING (disp_end + 1);
12311 #endif
12312 input_line_pointer = save_input_line_pointer;
12313 if (gotfree_input_line)
12314 {
12315 free (gotfree_input_line);
12316
12317 if (exp->X_op == O_constant || exp->X_op == O_register)
12318 exp->X_op = O_illegal;
12319 }
12320
12321 ret = i386_finalize_displacement (exp_seg, exp, types, disp_start);
12322
12323 RESTORE_END_STRING (disp_end);
12324
12325 return ret;
12326 }
12327
12328 static int
12329 i386_finalize_displacement (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
12330 i386_operand_type types, const char *disp_start)
12331 {
12332 int ret = 1;
12333
12334 /* We do this to make sure that the section symbol is in
12335 the symbol table. We will ultimately change the relocation
12336 to be relative to the beginning of the section. */
12337 if (i.reloc[this_operand] == BFD_RELOC_386_GOTOFF
12338 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
12339 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
12340 {
12341 if (exp->X_op != O_symbol)
12342 goto inv_disp;
12343
12344 if (S_IS_LOCAL (exp->X_add_symbol)
12345 && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section
12346 && S_GET_SEGMENT (exp->X_add_symbol) != expr_section)
12347 section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
12348 exp->X_op = O_subtract;
12349 exp->X_op_symbol = GOT_symbol;
12350 if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
12351 i.reloc[this_operand] = BFD_RELOC_32_PCREL;
12352 else if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
12353 i.reloc[this_operand] = BFD_RELOC_64;
12354 else
12355 i.reloc[this_operand] = BFD_RELOC_32;
12356 }
12357
12358 else if (exp->X_op == O_absent
12359 || exp->X_op == O_illegal
12360 || exp->X_op == O_big)
12361 {
12362 inv_disp:
12363 as_bad (_("missing or invalid displacement expression `%s'"),
12364 disp_start);
12365 ret = 0;
12366 }
12367
12368 else if (exp->X_op == O_constant)
12369 {
12370 /* Sizing gets taken care of by optimize_disp().
12371
12372 If not 64bit, sign/zero extend val, to account for wraparound
12373 when !BFD64. */
12374 if (expr_mode == expr_operator_present
12375 && flag_code != CODE_64BIT && !object_64bit)
12376 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
12377 }
12378
12379 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
12380 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
12381 && exp_seg != absolute_section
12382 && exp_seg != text_section
12383 && exp_seg != data_section
12384 && exp_seg != bss_section
12385 && exp_seg != undefined_section
12386 && !bfd_is_com_section (exp_seg))
12387 {
12388 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
12389 ret = 0;
12390 }
12391 #endif
12392
12393 else if (current_templates->start->opcode_modifier.jump == JUMP_BYTE)
12394 i.types[this_operand].bitfield.disp8 = 1;
12395
12396 /* Check if this is a displacement only operand. */
12397 if (!i.types[this_operand].bitfield.baseindex)
12398 i.types[this_operand] =
12399 operand_type_or (operand_type_and_not (i.types[this_operand], anydisp),
12400 operand_type_and (i.types[this_operand], types));
12401
12402 return ret;
12403 }
12404
12405 /* Return the active addressing mode, taking address override and
12406 registers forming the address into consideration. Update the
12407 address override prefix if necessary. */
12408
12409 static enum flag_code
12410 i386_addressing_mode (void)
12411 {
12412 enum flag_code addr_mode;
12413
12414 if (i.prefix[ADDR_PREFIX])
12415 addr_mode = flag_code == CODE_32BIT ? CODE_16BIT : CODE_32BIT;
12416 else if (flag_code == CODE_16BIT
12417 && is_cpu (current_templates->start, CpuMPX)
12418 /* Avoid replacing the "16-bit addressing not allowed" diagnostic
12419 from md_assemble() by "is not a valid base/index expression"
12420 when there is a base and/or index. */
12421 && !i.types[this_operand].bitfield.baseindex)
12422 {
12423 /* MPX insn memory operands with neither base nor index must be forced
12424 to use 32-bit addressing in 16-bit mode. */
12425 addr_mode = CODE_32BIT;
12426 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
12427 ++i.prefixes;
12428 gas_assert (!i.types[this_operand].bitfield.disp16);
12429 gas_assert (!i.types[this_operand].bitfield.disp32);
12430 }
12431 else
12432 {
12433 addr_mode = flag_code;
12434
12435 #if INFER_ADDR_PREFIX
12436 if (i.mem_operands == 0)
12437 {
12438 /* Infer address prefix from the first memory operand. */
12439 const reg_entry *addr_reg = i.base_reg;
12440
12441 if (addr_reg == NULL)
12442 addr_reg = i.index_reg;
12443
12444 if (addr_reg)
12445 {
12446 if (addr_reg->reg_type.bitfield.dword)
12447 addr_mode = CODE_32BIT;
12448 else if (flag_code != CODE_64BIT
12449 && addr_reg->reg_type.bitfield.word)
12450 addr_mode = CODE_16BIT;
12451
12452 if (addr_mode != flag_code)
12453 {
12454 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
12455 i.prefixes += 1;
12456 /* Change the size of any displacement too. At most one
12457 of Disp16 or Disp32 is set.
12458 FIXME. There doesn't seem to be any real need for
12459 separate Disp16 and Disp32 flags. The same goes for
12460 Imm16 and Imm32. Removing them would probably clean
12461 up the code quite a lot. */
12462 if (flag_code != CODE_64BIT
12463 && (i.types[this_operand].bitfield.disp16
12464 || i.types[this_operand].bitfield.disp32))
12465 {
12466 static const i386_operand_type disp16_32 = {
12467 .bitfield = { .disp16 = 1, .disp32 = 1 }
12468 };
12469
12470 i.types[this_operand]
12471 = operand_type_xor (i.types[this_operand], disp16_32);
12472 }
12473 }
12474 }
12475 }
12476 #endif
12477 }
12478
12479 return addr_mode;
12480 }
12481
12482 /* Make sure the memory operand we've been dealt is valid.
12483 Return 1 on success, 0 on a failure. */
12484
12485 static int
12486 i386_index_check (const char *operand_string)
12487 {
12488 const char *kind = "base/index";
12489 enum flag_code addr_mode = i386_addressing_mode ();
12490 const insn_template *t = current_templates->end - 1;
12491
12492 if (t->opcode_modifier.isstring)
12493 {
12494 /* Memory operands of string insns are special in that they only allow
12495 a single register (rDI, rSI, or rBX) as their memory address. */
12496 const reg_entry *expected_reg;
12497 static const char di_si[][2][4] =
12498 {
12499 { "esi", "edi" },
12500 { "si", "di" },
12501 { "rsi", "rdi" }
12502 };
12503 static const char bx[][4] = { "ebx", "bx", "rbx" };
12504
12505 kind = "string address";
12506
12507 if (t->opcode_modifier.prefixok == PrefixRep)
12508 {
12509 int es_op = t->opcode_modifier.isstring - IS_STRING_ES_OP0;
12510 int op = 0;
12511
12512 if (!t->operand_types[0].bitfield.baseindex
12513 || ((!i.mem_operands != !intel_syntax)
12514 && t->operand_types[1].bitfield.baseindex))
12515 op = 1;
12516 expected_reg
12517 = (const reg_entry *) str_hash_find (reg_hash,
12518 di_si[addr_mode][op == es_op]);
12519 }
12520 else
12521 expected_reg
12522 = (const reg_entry *)str_hash_find (reg_hash, bx[addr_mode]);
12523
12524 if (i.base_reg != expected_reg
12525 || i.index_reg
12526 || operand_type_check (i.types[this_operand], disp))
12527 {
12528 /* The second memory operand must have the same size as
12529 the first one. */
12530 if (i.mem_operands
12531 && i.base_reg
12532 && !((addr_mode == CODE_64BIT
12533 && i.base_reg->reg_type.bitfield.qword)
12534 || (addr_mode == CODE_32BIT
12535 ? i.base_reg->reg_type.bitfield.dword
12536 : i.base_reg->reg_type.bitfield.word)))
12537 goto bad_address;
12538
12539 as_warn (_("`%s' is not valid here (expected `%c%s%s%c')"),
12540 operand_string,
12541 intel_syntax ? '[' : '(',
12542 register_prefix,
12543 expected_reg->reg_name,
12544 intel_syntax ? ']' : ')');
12545 return 1;
12546 }
12547 else
12548 return 1;
12549
12550 bad_address:
12551 as_bad (_("`%s' is not a valid %s expression"),
12552 operand_string, kind);
12553 return 0;
12554 }
12555 else
12556 {
12557 t = current_templates->start;
12558
12559 if (addr_mode != CODE_16BIT)
12560 {
12561 /* 32-bit/64-bit checks. */
12562 if (i.disp_encoding == disp_encoding_16bit)
12563 {
12564 bad_disp:
12565 as_bad (_("invalid `%s' prefix"),
12566 addr_mode == CODE_16BIT ? "{disp32}" : "{disp16}");
12567 return 0;
12568 }
12569
12570 if ((i.base_reg
12571 && ((addr_mode == CODE_64BIT
12572 ? !i.base_reg->reg_type.bitfield.qword
12573 : !i.base_reg->reg_type.bitfield.dword)
12574 || (i.index_reg && i.base_reg->reg_num == RegIP)
12575 || i.base_reg->reg_num == RegIZ))
12576 || (i.index_reg
12577 && !i.index_reg->reg_type.bitfield.xmmword
12578 && !i.index_reg->reg_type.bitfield.ymmword
12579 && !i.index_reg->reg_type.bitfield.zmmword
12580 && ((addr_mode == CODE_64BIT
12581 ? !i.index_reg->reg_type.bitfield.qword
12582 : !i.index_reg->reg_type.bitfield.dword)
12583 || !i.index_reg->reg_type.bitfield.baseindex)))
12584 goto bad_address;
12585
12586 /* bndmk, bndldx, bndstx and mandatory non-vector SIB have special restrictions. */
12587 if (t->mnem_off == MN_bndmk
12588 || t->mnem_off == MN_bndldx
12589 || t->mnem_off == MN_bndstx
12590 || t->opcode_modifier.sib == SIBMEM)
12591 {
12592 /* They cannot use RIP-relative addressing. */
12593 if (i.base_reg && i.base_reg->reg_num == RegIP)
12594 {
12595 as_bad (_("`%s' cannot be used here"), operand_string);
12596 return 0;
12597 }
12598
12599 /* bndldx and bndstx ignore their scale factor. */
12600 if ((t->mnem_off == MN_bndldx || t->mnem_off == MN_bndstx)
12601 && i.log2_scale_factor)
12602 as_warn (_("register scaling is being ignored here"));
12603 }
12604 }
12605 else
12606 {
12607 /* 16-bit checks. */
12608 if (i.disp_encoding == disp_encoding_32bit)
12609 goto bad_disp;
12610
12611 if ((i.base_reg
12612 && (!i.base_reg->reg_type.bitfield.word
12613 || !i.base_reg->reg_type.bitfield.baseindex))
12614 || (i.index_reg
12615 && (!i.index_reg->reg_type.bitfield.word
12616 || !i.index_reg->reg_type.bitfield.baseindex
12617 || !(i.base_reg
12618 && i.base_reg->reg_num < 6
12619 && i.index_reg->reg_num >= 6
12620 && i.log2_scale_factor == 0))))
12621 goto bad_address;
12622 }
12623 }
12624 return 1;
12625 }
12626
12627 /* Handle vector immediates. */
12628
12629 static int
12630 RC_SAE_immediate (const char *imm_start)
12631 {
12632 const char *pstr = imm_start;
12633
12634 if (*pstr != '{')
12635 return 0;
12636
12637 pstr = RC_SAE_specifier (pstr + 1);
12638 if (pstr == NULL)
12639 return 0;
12640
12641 if (*pstr++ != '}')
12642 {
12643 as_bad (_("Missing '}': '%s'"), imm_start);
12644 return 0;
12645 }
12646 /* RC/SAE immediate string should contain nothing more. */;
12647 if (*pstr != 0)
12648 {
12649 as_bad (_("Junk after '}': '%s'"), imm_start);
12650 return 0;
12651 }
12652
12653 /* Internally this doesn't count as an operand. */
12654 --i.operands;
12655
12656 return 1;
12657 }
12658
12659 static INLINE bool starts_memory_operand (char c)
12660 {
12661 return ISDIGIT (c)
12662 || is_name_beginner (c)
12663 || strchr ("([\"+-!~", c);
12664 }
12665
12666 /* Parse OPERAND_STRING into the i386_insn structure I. Returns zero
12667 on error. */
12668
12669 static int
12670 i386_att_operand (char *operand_string)
12671 {
12672 const reg_entry *r;
12673 char *end_op;
12674 char *op_string = operand_string;
12675
12676 if (is_space_char (*op_string))
12677 ++op_string;
12678
12679 /* We check for an absolute prefix (differentiating,
12680 for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
12681 if (*op_string == ABSOLUTE_PREFIX
12682 && current_templates->start->opcode_modifier.jump)
12683 {
12684 ++op_string;
12685 if (is_space_char (*op_string))
12686 ++op_string;
12687 i.jumpabsolute = true;
12688 }
12689
12690 /* Check if operand is a register. */
12691 if ((r = parse_register (op_string, &end_op)) != NULL)
12692 {
12693 i386_operand_type temp;
12694
12695 if (r == &bad_reg)
12696 return 0;
12697
12698 /* Check for a segment override by searching for ':' after a
12699 segment register. */
12700 op_string = end_op;
12701 if (is_space_char (*op_string))
12702 ++op_string;
12703 if (*op_string == ':' && r->reg_type.bitfield.class == SReg)
12704 {
12705 i.seg[i.mem_operands] = r;
12706
12707 /* Skip the ':' and whitespace. */
12708 ++op_string;
12709 if (is_space_char (*op_string))
12710 ++op_string;
12711
12712 /* Handle case of %es:*foo. */
12713 if (!i.jumpabsolute && *op_string == ABSOLUTE_PREFIX
12714 && current_templates->start->opcode_modifier.jump)
12715 {
12716 ++op_string;
12717 if (is_space_char (*op_string))
12718 ++op_string;
12719 i.jumpabsolute = true;
12720 }
12721
12722 if (!starts_memory_operand (*op_string))
12723 {
12724 as_bad (_("bad memory operand `%s'"), op_string);
12725 return 0;
12726 }
12727 goto do_memory_reference;
12728 }
12729
12730 /* Handle vector operations. */
12731 if (*op_string == '{')
12732 {
12733 op_string = check_VecOperations (op_string);
12734 if (op_string == NULL)
12735 return 0;
12736 }
12737
12738 if (*op_string)
12739 {
12740 as_bad (_("junk `%s' after register"), op_string);
12741 return 0;
12742 }
12743
12744 /* Reject pseudo registers for .insn. */
12745 if (dot_insn () && r->reg_type.bitfield.class == ClassNone)
12746 {
12747 as_bad (_("`%s%s' cannot be used here"),
12748 register_prefix, r->reg_name);
12749 return 0;
12750 }
12751
12752 temp = r->reg_type;
12753 temp.bitfield.baseindex = 0;
12754 i.types[this_operand] = operand_type_or (i.types[this_operand],
12755 temp);
12756 i.types[this_operand].bitfield.unspecified = 0;
12757 i.op[this_operand].regs = r;
12758 i.reg_operands++;
12759
12760 /* A GPR may follow an RC or SAE immediate only if a (vector) register
12761 operand was also present earlier on. */
12762 if (i.rounding.type != rc_none && temp.bitfield.class == Reg
12763 && i.reg_operands == 1)
12764 {
12765 unsigned int j;
12766
12767 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); ++j)
12768 if (i.rounding.type == RC_NamesTable[j].type)
12769 break;
12770 as_bad (_("`%s': misplaced `{%s}'"),
12771 insn_name (current_templates->start), RC_NamesTable[j].name);
12772 return 0;
12773 }
12774 }
12775 else if (*op_string == REGISTER_PREFIX)
12776 {
12777 as_bad (_("bad register name `%s'"), op_string);
12778 return 0;
12779 }
12780 else if (*op_string == IMMEDIATE_PREFIX)
12781 {
12782 ++op_string;
12783 if (i.jumpabsolute)
12784 {
12785 as_bad (_("immediate operand illegal with absolute jump"));
12786 return 0;
12787 }
12788 if (!i386_immediate (op_string))
12789 return 0;
12790 if (i.rounding.type != rc_none)
12791 {
12792 as_bad (_("`%s': RC/SAE operand must follow immediate operands"),
12793 insn_name (current_templates->start));
12794 return 0;
12795 }
12796 }
12797 else if (RC_SAE_immediate (operand_string))
12798 {
12799 /* If it is a RC or SAE immediate, do the necessary placement check:
12800 Only another immediate or a GPR may precede it. */
12801 if (i.mem_operands || i.reg_operands + i.imm_operands > 1
12802 || (i.reg_operands == 1
12803 && i.op[0].regs->reg_type.bitfield.class != Reg))
12804 {
12805 as_bad (_("`%s': misplaced `%s'"),
12806 insn_name (current_templates->start), operand_string);
12807 return 0;
12808 }
12809 }
12810 else if (starts_memory_operand (*op_string))
12811 {
12812 /* This is a memory reference of some sort. */
12813 char *base_string;
12814
12815 /* Start and end of displacement string expression (if found). */
12816 char *displacement_string_start;
12817 char *displacement_string_end;
12818
12819 do_memory_reference:
12820 /* Check for base index form. We detect the base index form by
12821 looking for an ')' at the end of the operand, searching
12822 for the '(' matching it, and finding a REGISTER_PREFIX or ','
12823 after the '('. */
12824 base_string = op_string + strlen (op_string);
12825
12826 /* Handle vector operations. */
12827 --base_string;
12828 if (is_space_char (*base_string))
12829 --base_string;
12830
12831 if (*base_string == '}')
12832 {
12833 char *vop_start = NULL;
12834
12835 while (base_string-- > op_string)
12836 {
12837 if (*base_string == '"')
12838 break;
12839 if (*base_string != '{')
12840 continue;
12841
12842 vop_start = base_string;
12843
12844 --base_string;
12845 if (is_space_char (*base_string))
12846 --base_string;
12847
12848 if (*base_string != '}')
12849 break;
12850
12851 vop_start = NULL;
12852 }
12853
12854 if (!vop_start)
12855 {
12856 as_bad (_("unbalanced figure braces"));
12857 return 0;
12858 }
12859
12860 if (check_VecOperations (vop_start) == NULL)
12861 return 0;
12862 }
12863
12864 /* If we only have a displacement, set-up for it to be parsed later. */
12865 displacement_string_start = op_string;
12866 displacement_string_end = base_string + 1;
12867
12868 if (*base_string == ')')
12869 {
12870 char *temp_string;
12871 unsigned int parens_not_balanced = 0;
12872 bool in_quotes = false;
12873
12874 /* We've already checked that the number of left & right ()'s are
12875 equal, and that there's a matching set of double quotes. */
12876 end_op = base_string;
12877 for (temp_string = op_string; temp_string < end_op; temp_string++)
12878 {
12879 if (*temp_string == '\\' && temp_string[1] == '"')
12880 ++temp_string;
12881 else if (*temp_string == '"')
12882 in_quotes = !in_quotes;
12883 else if (!in_quotes)
12884 {
12885 if (*temp_string == '(' && !parens_not_balanced++)
12886 base_string = temp_string;
12887 if (*temp_string == ')')
12888 --parens_not_balanced;
12889 }
12890 }
12891
12892 temp_string = base_string;
12893
12894 /* Skip past '(' and whitespace. */
12895 gas_assert (*base_string == '(');
12896 ++base_string;
12897 if (is_space_char (*base_string))
12898 ++base_string;
12899
12900 if (*base_string == ','
12901 || ((i.base_reg = parse_register (base_string, &end_op))
12902 != NULL))
12903 {
12904 displacement_string_end = temp_string;
12905
12906 i.types[this_operand].bitfield.baseindex = 1;
12907
12908 if (i.base_reg)
12909 {
12910 if (i.base_reg == &bad_reg)
12911 return 0;
12912 base_string = end_op;
12913 if (is_space_char (*base_string))
12914 ++base_string;
12915 }
12916
12917 /* There may be an index reg or scale factor here. */
12918 if (*base_string == ',')
12919 {
12920 ++base_string;
12921 if (is_space_char (*base_string))
12922 ++base_string;
12923
12924 if ((i.index_reg = parse_register (base_string, &end_op))
12925 != NULL)
12926 {
12927 if (i.index_reg == &bad_reg)
12928 return 0;
12929 base_string = end_op;
12930 if (is_space_char (*base_string))
12931 ++base_string;
12932 if (*base_string == ',')
12933 {
12934 ++base_string;
12935 if (is_space_char (*base_string))
12936 ++base_string;
12937 }
12938 else if (*base_string != ')')
12939 {
12940 as_bad (_("expecting `,' or `)' "
12941 "after index register in `%s'"),
12942 operand_string);
12943 return 0;
12944 }
12945 }
12946 else if (*base_string == REGISTER_PREFIX)
12947 {
12948 end_op = strchr (base_string, ',');
12949 if (end_op)
12950 *end_op = '\0';
12951 as_bad (_("bad register name `%s'"), base_string);
12952 return 0;
12953 }
12954
12955 /* Check for scale factor. */
12956 if (*base_string != ')')
12957 {
12958 char *end_scale = i386_scale (base_string);
12959
12960 if (!end_scale)
12961 return 0;
12962
12963 base_string = end_scale;
12964 if (is_space_char (*base_string))
12965 ++base_string;
12966 if (*base_string != ')')
12967 {
12968 as_bad (_("expecting `)' "
12969 "after scale factor in `%s'"),
12970 operand_string);
12971 return 0;
12972 }
12973 }
12974 else if (!i.index_reg)
12975 {
12976 as_bad (_("expecting index register or scale factor "
12977 "after `,'; got '%c'"),
12978 *base_string);
12979 return 0;
12980 }
12981 }
12982 else if (*base_string != ')')
12983 {
12984 as_bad (_("expecting `,' or `)' "
12985 "after base register in `%s'"),
12986 operand_string);
12987 return 0;
12988 }
12989 }
12990 else if (*base_string == REGISTER_PREFIX)
12991 {
12992 end_op = strchr (base_string, ',');
12993 if (end_op)
12994 *end_op = '\0';
12995 as_bad (_("bad register name `%s'"), base_string);
12996 return 0;
12997 }
12998 }
12999
13000 /* If there's an expression beginning the operand, parse it,
13001 assuming displacement_string_start and
13002 displacement_string_end are meaningful. */
13003 if (displacement_string_start != displacement_string_end)
13004 {
13005 if (!i386_displacement (displacement_string_start,
13006 displacement_string_end))
13007 return 0;
13008 }
13009
13010 /* Special case for (%dx) while doing input/output op. */
13011 if (i.base_reg
13012 && i.base_reg->reg_type.bitfield.instance == RegD
13013 && i.base_reg->reg_type.bitfield.word
13014 && i.index_reg == 0
13015 && i.log2_scale_factor == 0
13016 && i.seg[i.mem_operands] == 0
13017 && !operand_type_check (i.types[this_operand], disp))
13018 {
13019 i.types[this_operand] = i.base_reg->reg_type;
13020 i.input_output_operand = true;
13021 return 1;
13022 }
13023
13024 if (i386_index_check (operand_string) == 0)
13025 return 0;
13026 i.flags[this_operand] |= Operand_Mem;
13027 i.mem_operands++;
13028 }
13029 else
13030 {
13031 /* It's not a memory operand; argh! */
13032 as_bad (_("invalid char %s beginning operand %d `%s'"),
13033 output_invalid (*op_string),
13034 this_operand + 1,
13035 op_string);
13036 return 0;
13037 }
13038 return 1; /* Normal return. */
13039 }
13040 \f
13041 /* Calculate the maximum variable size (i.e., excluding fr_fix)
13042 that an rs_machine_dependent frag may reach. */
13043
13044 unsigned int
13045 i386_frag_max_var (fragS *frag)
13046 {
13047 /* The only relaxable frags are for jumps.
13048 Unconditional jumps can grow by 4 bytes and others by 5 bytes. */
13049 gas_assert (frag->fr_type == rs_machine_dependent);
13050 return TYPE_FROM_RELAX_STATE (frag->fr_subtype) == UNCOND_JUMP ? 4 : 5;
13051 }
13052
13053 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13054 static int
13055 elf_symbol_resolved_in_segment_p (symbolS *fr_symbol, offsetT fr_var)
13056 {
13057 /* STT_GNU_IFUNC symbol must go through PLT. */
13058 if ((symbol_get_bfdsym (fr_symbol)->flags
13059 & BSF_GNU_INDIRECT_FUNCTION) != 0)
13060 return 0;
13061
13062 if (!S_IS_EXTERNAL (fr_symbol))
13063 /* Symbol may be weak or local. */
13064 return !S_IS_WEAK (fr_symbol);
13065
13066 /* Global symbols with non-default visibility can't be preempted. */
13067 if (ELF_ST_VISIBILITY (S_GET_OTHER (fr_symbol)) != STV_DEFAULT)
13068 return 1;
13069
13070 if (fr_var != NO_RELOC)
13071 switch ((enum bfd_reloc_code_real) fr_var)
13072 {
13073 case BFD_RELOC_386_PLT32:
13074 case BFD_RELOC_X86_64_PLT32:
13075 /* Symbol with PLT relocation may be preempted. */
13076 return 0;
13077 default:
13078 abort ();
13079 }
13080
13081 /* Global symbols with default visibility in a shared library may be
13082 preempted by another definition. */
13083 return !shared;
13084 }
13085 #endif
13086
13087 /* Table 3-2. Macro-Fusible Instructions in Haswell Microarchitecture
13088 Note also work for Skylake and Cascadelake.
13089 ---------------------------------------------------------------------
13090 | JCC | ADD/SUB/CMP | INC/DEC | TEST/AND |
13091 | ------ | ----------- | ------- | -------- |
13092 | Jo | N | N | Y |
13093 | Jno | N | N | Y |
13094 | Jc/Jb | Y | N | Y |
13095 | Jae/Jnb | Y | N | Y |
13096 | Je/Jz | Y | Y | Y |
13097 | Jne/Jnz | Y | Y | Y |
13098 | Jna/Jbe | Y | N | Y |
13099 | Ja/Jnbe | Y | N | Y |
13100 | Js | N | N | Y |
13101 | Jns | N | N | Y |
13102 | Jp/Jpe | N | N | Y |
13103 | Jnp/Jpo | N | N | Y |
13104 | Jl/Jnge | Y | Y | Y |
13105 | Jge/Jnl | Y | Y | Y |
13106 | Jle/Jng | Y | Y | Y |
13107 | Jg/Jnle | Y | Y | Y |
13108 --------------------------------------------------------------------- */
13109 static int
13110 i386_macro_fusible_p (enum mf_cmp_kind mf_cmp, enum mf_jcc_kind mf_jcc)
13111 {
13112 if (mf_cmp == mf_cmp_alu_cmp)
13113 return ((mf_jcc >= mf_jcc_jc && mf_jcc <= mf_jcc_jna)
13114 || mf_jcc == mf_jcc_jl || mf_jcc == mf_jcc_jle);
13115 if (mf_cmp == mf_cmp_incdec)
13116 return (mf_jcc == mf_jcc_je || mf_jcc == mf_jcc_jl
13117 || mf_jcc == mf_jcc_jle);
13118 if (mf_cmp == mf_cmp_test_and)
13119 return 1;
13120 return 0;
13121 }
13122
13123 /* Return the next non-empty frag. */
13124
13125 static fragS *
13126 i386_next_non_empty_frag (fragS *fragP)
13127 {
13128 /* There may be a frag with a ".fill 0" when there is no room in
13129 the current frag for frag_grow in output_insn. */
13130 for (fragP = fragP->fr_next;
13131 (fragP != NULL
13132 && fragP->fr_type == rs_fill
13133 && fragP->fr_fix == 0);
13134 fragP = fragP->fr_next)
13135 ;
13136 return fragP;
13137 }
13138
13139 /* Return the next jcc frag after BRANCH_PADDING. */
13140
13141 static fragS *
13142 i386_next_fusible_jcc_frag (fragS *maybe_cmp_fragP, fragS *pad_fragP)
13143 {
13144 fragS *branch_fragP;
13145 if (!pad_fragP)
13146 return NULL;
13147
13148 if (pad_fragP->fr_type == rs_machine_dependent
13149 && (TYPE_FROM_RELAX_STATE (pad_fragP->fr_subtype)
13150 == BRANCH_PADDING))
13151 {
13152 branch_fragP = i386_next_non_empty_frag (pad_fragP);
13153 if (branch_fragP->fr_type != rs_machine_dependent)
13154 return NULL;
13155 if (TYPE_FROM_RELAX_STATE (branch_fragP->fr_subtype) == COND_JUMP
13156 && i386_macro_fusible_p (maybe_cmp_fragP->tc_frag_data.mf_type,
13157 pad_fragP->tc_frag_data.mf_type))
13158 return branch_fragP;
13159 }
13160
13161 return NULL;
13162 }
13163
13164 /* Classify BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags. */
13165
13166 static void
13167 i386_classify_machine_dependent_frag (fragS *fragP)
13168 {
13169 fragS *cmp_fragP;
13170 fragS *pad_fragP;
13171 fragS *branch_fragP;
13172 fragS *next_fragP;
13173 unsigned int max_prefix_length;
13174
13175 if (fragP->tc_frag_data.classified)
13176 return;
13177
13178 /* First scan for BRANCH_PADDING and FUSED_JCC_PADDING. Convert
13179 FUSED_JCC_PADDING and merge BRANCH_PADDING. */
13180 for (next_fragP = fragP;
13181 next_fragP != NULL;
13182 next_fragP = next_fragP->fr_next)
13183 {
13184 next_fragP->tc_frag_data.classified = 1;
13185 if (next_fragP->fr_type == rs_machine_dependent)
13186 switch (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype))
13187 {
13188 case BRANCH_PADDING:
13189 /* The BRANCH_PADDING frag must be followed by a branch
13190 frag. */
13191 branch_fragP = i386_next_non_empty_frag (next_fragP);
13192 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
13193 break;
13194 case FUSED_JCC_PADDING:
13195 /* Check if this is a fused jcc:
13196 FUSED_JCC_PADDING
13197 CMP like instruction
13198 BRANCH_PADDING
13199 COND_JUMP
13200 */
13201 cmp_fragP = i386_next_non_empty_frag (next_fragP);
13202 pad_fragP = i386_next_non_empty_frag (cmp_fragP);
13203 branch_fragP = i386_next_fusible_jcc_frag (next_fragP, pad_fragP);
13204 if (branch_fragP)
13205 {
13206 /* The BRANCH_PADDING frag is merged with the
13207 FUSED_JCC_PADDING frag. */
13208 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
13209 /* CMP like instruction size. */
13210 next_fragP->tc_frag_data.cmp_size = cmp_fragP->fr_fix;
13211 frag_wane (pad_fragP);
13212 /* Skip to branch_fragP. */
13213 next_fragP = branch_fragP;
13214 }
13215 else if (next_fragP->tc_frag_data.max_prefix_length)
13216 {
13217 /* Turn FUSED_JCC_PADDING into BRANCH_PREFIX if it isn't
13218 a fused jcc. */
13219 next_fragP->fr_subtype
13220 = ENCODE_RELAX_STATE (BRANCH_PREFIX, 0);
13221 next_fragP->tc_frag_data.max_bytes
13222 = next_fragP->tc_frag_data.max_prefix_length;
13223 /* This will be updated in the BRANCH_PREFIX scan. */
13224 next_fragP->tc_frag_data.max_prefix_length = 0;
13225 }
13226 else
13227 frag_wane (next_fragP);
13228 break;
13229 }
13230 }
13231
13232 /* Stop if there is no BRANCH_PREFIX. */
13233 if (!align_branch_prefix_size)
13234 return;
13235
13236 /* Scan for BRANCH_PREFIX. */
13237 for (; fragP != NULL; fragP = fragP->fr_next)
13238 {
13239 if (fragP->fr_type != rs_machine_dependent
13240 || (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
13241 != BRANCH_PREFIX))
13242 continue;
13243
13244 /* Count all BRANCH_PREFIX frags before BRANCH_PADDING and
13245 COND_JUMP_PREFIX. */
13246 max_prefix_length = 0;
13247 for (next_fragP = fragP;
13248 next_fragP != NULL;
13249 next_fragP = next_fragP->fr_next)
13250 {
13251 if (next_fragP->fr_type == rs_fill)
13252 /* Skip rs_fill frags. */
13253 continue;
13254 else if (next_fragP->fr_type != rs_machine_dependent)
13255 /* Stop for all other frags. */
13256 break;
13257
13258 /* rs_machine_dependent frags. */
13259 if (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
13260 == BRANCH_PREFIX)
13261 {
13262 /* Count BRANCH_PREFIX frags. */
13263 if (max_prefix_length >= MAX_FUSED_JCC_PADDING_SIZE)
13264 {
13265 max_prefix_length = MAX_FUSED_JCC_PADDING_SIZE;
13266 frag_wane (next_fragP);
13267 }
13268 else
13269 max_prefix_length
13270 += next_fragP->tc_frag_data.max_bytes;
13271 }
13272 else if ((TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
13273 == BRANCH_PADDING)
13274 || (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
13275 == FUSED_JCC_PADDING))
13276 {
13277 /* Stop at BRANCH_PADDING and FUSED_JCC_PADDING. */
13278 fragP->tc_frag_data.u.padding_fragP = next_fragP;
13279 break;
13280 }
13281 else
13282 /* Stop for other rs_machine_dependent frags. */
13283 break;
13284 }
13285
13286 fragP->tc_frag_data.max_prefix_length = max_prefix_length;
13287
13288 /* Skip to the next frag. */
13289 fragP = next_fragP;
13290 }
13291 }
13292
13293 /* Compute padding size for
13294
13295 FUSED_JCC_PADDING
13296 CMP like instruction
13297 BRANCH_PADDING
13298 COND_JUMP/UNCOND_JUMP
13299
13300 or
13301
13302 BRANCH_PADDING
13303 COND_JUMP/UNCOND_JUMP
13304 */
13305
13306 static int
13307 i386_branch_padding_size (fragS *fragP, offsetT address)
13308 {
13309 unsigned int offset, size, padding_size;
13310 fragS *branch_fragP = fragP->tc_frag_data.u.branch_fragP;
13311
13312 /* The start address of the BRANCH_PADDING or FUSED_JCC_PADDING frag. */
13313 if (!address)
13314 address = fragP->fr_address;
13315 address += fragP->fr_fix;
13316
13317 /* CMP like instrunction size. */
13318 size = fragP->tc_frag_data.cmp_size;
13319
13320 /* The base size of the branch frag. */
13321 size += branch_fragP->fr_fix;
13322
13323 /* Add opcode and displacement bytes for the rs_machine_dependent
13324 branch frag. */
13325 if (branch_fragP->fr_type == rs_machine_dependent)
13326 size += md_relax_table[branch_fragP->fr_subtype].rlx_length;
13327
13328 /* Check if branch is within boundary and doesn't end at the last
13329 byte. */
13330 offset = address & ((1U << align_branch_power) - 1);
13331 if ((offset + size) >= (1U << align_branch_power))
13332 /* Padding needed to avoid crossing boundary. */
13333 padding_size = (1U << align_branch_power) - offset;
13334 else
13335 /* No padding needed. */
13336 padding_size = 0;
13337
13338 /* The return value may be saved in tc_frag_data.length which is
13339 unsigned byte. */
13340 if (!fits_in_unsigned_byte (padding_size))
13341 abort ();
13342
13343 return padding_size;
13344 }
13345
13346 /* i386_generic_table_relax_frag()
13347
13348 Handle BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags to
13349 grow/shrink padding to align branch frags. Hand others to
13350 relax_frag(). */
13351
13352 long
13353 i386_generic_table_relax_frag (segT segment, fragS *fragP, long stretch)
13354 {
13355 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
13356 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
13357 {
13358 long padding_size = i386_branch_padding_size (fragP, 0);
13359 long grow = padding_size - fragP->tc_frag_data.length;
13360
13361 /* When the BRANCH_PREFIX frag is used, the computed address
13362 must match the actual address and there should be no padding. */
13363 if (fragP->tc_frag_data.padding_address
13364 && (fragP->tc_frag_data.padding_address != fragP->fr_address
13365 || padding_size))
13366 abort ();
13367
13368 /* Update the padding size. */
13369 if (grow)
13370 fragP->tc_frag_data.length = padding_size;
13371
13372 return grow;
13373 }
13374 else if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
13375 {
13376 fragS *padding_fragP, *next_fragP;
13377 long padding_size, left_size, last_size;
13378
13379 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
13380 if (!padding_fragP)
13381 /* Use the padding set by the leading BRANCH_PREFIX frag. */
13382 return (fragP->tc_frag_data.length
13383 - fragP->tc_frag_data.last_length);
13384
13385 /* Compute the relative address of the padding frag in the very
13386 first time where the BRANCH_PREFIX frag sizes are zero. */
13387 if (!fragP->tc_frag_data.padding_address)
13388 fragP->tc_frag_data.padding_address
13389 = padding_fragP->fr_address - (fragP->fr_address - stretch);
13390
13391 /* First update the last length from the previous interation. */
13392 left_size = fragP->tc_frag_data.prefix_length;
13393 for (next_fragP = fragP;
13394 next_fragP != padding_fragP;
13395 next_fragP = next_fragP->fr_next)
13396 if (next_fragP->fr_type == rs_machine_dependent
13397 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
13398 == BRANCH_PREFIX))
13399 {
13400 if (left_size)
13401 {
13402 int max = next_fragP->tc_frag_data.max_bytes;
13403 if (max)
13404 {
13405 int size;
13406 if (max > left_size)
13407 size = left_size;
13408 else
13409 size = max;
13410 left_size -= size;
13411 next_fragP->tc_frag_data.last_length = size;
13412 }
13413 }
13414 else
13415 next_fragP->tc_frag_data.last_length = 0;
13416 }
13417
13418 /* Check the padding size for the padding frag. */
13419 padding_size = i386_branch_padding_size
13420 (padding_fragP, (fragP->fr_address
13421 + fragP->tc_frag_data.padding_address));
13422
13423 last_size = fragP->tc_frag_data.prefix_length;
13424 /* Check if there is change from the last interation. */
13425 if (padding_size == last_size)
13426 {
13427 /* Update the expected address of the padding frag. */
13428 padding_fragP->tc_frag_data.padding_address
13429 = (fragP->fr_address + padding_size
13430 + fragP->tc_frag_data.padding_address);
13431 return 0;
13432 }
13433
13434 if (padding_size > fragP->tc_frag_data.max_prefix_length)
13435 {
13436 /* No padding if there is no sufficient room. Clear the
13437 expected address of the padding frag. */
13438 padding_fragP->tc_frag_data.padding_address = 0;
13439 padding_size = 0;
13440 }
13441 else
13442 /* Store the expected address of the padding frag. */
13443 padding_fragP->tc_frag_data.padding_address
13444 = (fragP->fr_address + padding_size
13445 + fragP->tc_frag_data.padding_address);
13446
13447 fragP->tc_frag_data.prefix_length = padding_size;
13448
13449 /* Update the length for the current interation. */
13450 left_size = padding_size;
13451 for (next_fragP = fragP;
13452 next_fragP != padding_fragP;
13453 next_fragP = next_fragP->fr_next)
13454 if (next_fragP->fr_type == rs_machine_dependent
13455 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
13456 == BRANCH_PREFIX))
13457 {
13458 if (left_size)
13459 {
13460 int max = next_fragP->tc_frag_data.max_bytes;
13461 if (max)
13462 {
13463 int size;
13464 if (max > left_size)
13465 size = left_size;
13466 else
13467 size = max;
13468 left_size -= size;
13469 next_fragP->tc_frag_data.length = size;
13470 }
13471 }
13472 else
13473 next_fragP->tc_frag_data.length = 0;
13474 }
13475
13476 return (fragP->tc_frag_data.length
13477 - fragP->tc_frag_data.last_length);
13478 }
13479 return relax_frag (segment, fragP, stretch);
13480 }
13481
13482 /* md_estimate_size_before_relax()
13483
13484 Called just before relax() for rs_machine_dependent frags. The x86
13485 assembler uses these frags to handle variable size jump
13486 instructions.
13487
13488 Any symbol that is now undefined will not become defined.
13489 Return the correct fr_subtype in the frag.
13490 Return the initial "guess for variable size of frag" to caller.
13491 The guess is actually the growth beyond the fixed part. Whatever
13492 we do to grow the fixed or variable part contributes to our
13493 returned value. */
13494
13495 int
13496 md_estimate_size_before_relax (fragS *fragP, segT segment)
13497 {
13498 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
13499 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX
13500 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
13501 {
13502 i386_classify_machine_dependent_frag (fragP);
13503 return fragP->tc_frag_data.length;
13504 }
13505
13506 /* We've already got fragP->fr_subtype right; all we have to do is
13507 check for un-relaxable symbols. On an ELF system, we can't relax
13508 an externally visible symbol, because it may be overridden by a
13509 shared library. */
13510 if (S_GET_SEGMENT (fragP->fr_symbol) != segment
13511 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13512 || (IS_ELF
13513 && !elf_symbol_resolved_in_segment_p (fragP->fr_symbol,
13514 fragP->fr_var))
13515 #endif
13516 #if defined (OBJ_COFF) && defined (TE_PE)
13517 || (OUTPUT_FLAVOR == bfd_target_coff_flavour
13518 && S_IS_WEAK (fragP->fr_symbol))
13519 #endif
13520 )
13521 {
13522 /* Symbol is undefined in this segment, or we need to keep a
13523 reloc so that weak symbols can be overridden. */
13524 int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
13525 enum bfd_reloc_code_real reloc_type;
13526 unsigned char *opcode;
13527 int old_fr_fix;
13528 fixS *fixP = NULL;
13529
13530 if (fragP->fr_var != NO_RELOC)
13531 reloc_type = (enum bfd_reloc_code_real) fragP->fr_var;
13532 else if (size == 2)
13533 reloc_type = BFD_RELOC_16_PCREL;
13534 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13535 else if (fragP->tc_frag_data.code == CODE_64BIT
13536 && fragP->fr_offset == 0
13537 && need_plt32_p (fragP->fr_symbol))
13538 reloc_type = BFD_RELOC_X86_64_PLT32;
13539 #endif
13540 else
13541 reloc_type = BFD_RELOC_32_PCREL;
13542
13543 old_fr_fix = fragP->fr_fix;
13544 opcode = (unsigned char *) fragP->fr_opcode;
13545
13546 switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
13547 {
13548 case UNCOND_JUMP:
13549 /* Make jmp (0xeb) a (d)word displacement jump. */
13550 opcode[0] = 0xe9;
13551 fragP->fr_fix += size;
13552 fixP = fix_new (fragP, old_fr_fix, size,
13553 fragP->fr_symbol,
13554 fragP->fr_offset, 1,
13555 reloc_type);
13556 break;
13557
13558 case COND_JUMP86:
13559 if (size == 2
13560 && (!no_cond_jump_promotion || fragP->fr_var != NO_RELOC))
13561 {
13562 /* Negate the condition, and branch past an
13563 unconditional jump. */
13564 opcode[0] ^= 1;
13565 opcode[1] = 3;
13566 /* Insert an unconditional jump. */
13567 opcode[2] = 0xe9;
13568 /* We added two extra opcode bytes, and have a two byte
13569 offset. */
13570 fragP->fr_fix += 2 + 2;
13571 fix_new (fragP, old_fr_fix + 2, 2,
13572 fragP->fr_symbol,
13573 fragP->fr_offset, 1,
13574 reloc_type);
13575 break;
13576 }
13577 /* Fall through. */
13578
13579 case COND_JUMP:
13580 if (no_cond_jump_promotion && fragP->fr_var == NO_RELOC)
13581 {
13582 fragP->fr_fix += 1;
13583 fixP = fix_new (fragP, old_fr_fix, 1,
13584 fragP->fr_symbol,
13585 fragP->fr_offset, 1,
13586 BFD_RELOC_8_PCREL);
13587 fixP->fx_signed = 1;
13588 break;
13589 }
13590
13591 /* This changes the byte-displacement jump 0x7N
13592 to the (d)word-displacement jump 0x0f,0x8N. */
13593 opcode[1] = opcode[0] + 0x10;
13594 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
13595 /* We've added an opcode byte. */
13596 fragP->fr_fix += 1 + size;
13597 fixP = fix_new (fragP, old_fr_fix + 1, size,
13598 fragP->fr_symbol,
13599 fragP->fr_offset, 1,
13600 reloc_type);
13601 break;
13602
13603 default:
13604 BAD_CASE (fragP->fr_subtype);
13605 break;
13606 }
13607
13608 /* All jumps handled here are signed, but don't unconditionally use a
13609 signed limit check for 32 and 16 bit jumps as we want to allow wrap
13610 around at 4G (outside of 64-bit mode) and 64k. */
13611 if (size == 4 && flag_code == CODE_64BIT)
13612 fixP->fx_signed = 1;
13613
13614 frag_wane (fragP);
13615 return fragP->fr_fix - old_fr_fix;
13616 }
13617
13618 /* Guess size depending on current relax state. Initially the relax
13619 state will correspond to a short jump and we return 1, because
13620 the variable part of the frag (the branch offset) is one byte
13621 long. However, we can relax a section more than once and in that
13622 case we must either set fr_subtype back to the unrelaxed state,
13623 or return the value for the appropriate branch. */
13624 return md_relax_table[fragP->fr_subtype].rlx_length;
13625 }
13626
13627 /* Called after relax() is finished.
13628
13629 In: Address of frag.
13630 fr_type == rs_machine_dependent.
13631 fr_subtype is what the address relaxed to.
13632
13633 Out: Any fixSs and constants are set up.
13634 Caller will turn frag into a ".space 0". */
13635
13636 void
13637 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec ATTRIBUTE_UNUSED,
13638 fragS *fragP)
13639 {
13640 unsigned char *opcode;
13641 unsigned char *where_to_put_displacement = NULL;
13642 offsetT target_address;
13643 offsetT opcode_address;
13644 unsigned int extension = 0;
13645 offsetT displacement_from_opcode_start;
13646
13647 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
13648 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING
13649 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
13650 {
13651 /* Generate nop padding. */
13652 unsigned int size = fragP->tc_frag_data.length;
13653 if (size)
13654 {
13655 if (size > fragP->tc_frag_data.max_bytes)
13656 abort ();
13657
13658 if (flag_debug)
13659 {
13660 const char *msg;
13661 const char *branch = "branch";
13662 const char *prefix = "";
13663 fragS *padding_fragP;
13664 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
13665 == BRANCH_PREFIX)
13666 {
13667 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
13668 switch (fragP->tc_frag_data.default_prefix)
13669 {
13670 default:
13671 abort ();
13672 break;
13673 case CS_PREFIX_OPCODE:
13674 prefix = " cs";
13675 break;
13676 case DS_PREFIX_OPCODE:
13677 prefix = " ds";
13678 break;
13679 case ES_PREFIX_OPCODE:
13680 prefix = " es";
13681 break;
13682 case FS_PREFIX_OPCODE:
13683 prefix = " fs";
13684 break;
13685 case GS_PREFIX_OPCODE:
13686 prefix = " gs";
13687 break;
13688 case SS_PREFIX_OPCODE:
13689 prefix = " ss";
13690 break;
13691 }
13692 if (padding_fragP)
13693 msg = _("%s:%u: add %d%s at 0x%llx to align "
13694 "%s within %d-byte boundary\n");
13695 else
13696 msg = _("%s:%u: add additional %d%s at 0x%llx to "
13697 "align %s within %d-byte boundary\n");
13698 }
13699 else
13700 {
13701 padding_fragP = fragP;
13702 msg = _("%s:%u: add %d%s-byte nop at 0x%llx to align "
13703 "%s within %d-byte boundary\n");
13704 }
13705
13706 if (padding_fragP)
13707 switch (padding_fragP->tc_frag_data.branch_type)
13708 {
13709 case align_branch_jcc:
13710 branch = "jcc";
13711 break;
13712 case align_branch_fused:
13713 branch = "fused jcc";
13714 break;
13715 case align_branch_jmp:
13716 branch = "jmp";
13717 break;
13718 case align_branch_call:
13719 branch = "call";
13720 break;
13721 case align_branch_indirect:
13722 branch = "indiret branch";
13723 break;
13724 case align_branch_ret:
13725 branch = "ret";
13726 break;
13727 default:
13728 break;
13729 }
13730
13731 fprintf (stdout, msg,
13732 fragP->fr_file, fragP->fr_line, size, prefix,
13733 (long long) fragP->fr_address, branch,
13734 1 << align_branch_power);
13735 }
13736 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
13737 memset (fragP->fr_opcode,
13738 fragP->tc_frag_data.default_prefix, size);
13739 else
13740 i386_generate_nops (fragP, (char *) fragP->fr_opcode,
13741 size, 0);
13742 fragP->fr_fix += size;
13743 }
13744 return;
13745 }
13746
13747 opcode = (unsigned char *) fragP->fr_opcode;
13748
13749 /* Address we want to reach in file space. */
13750 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
13751
13752 /* Address opcode resides at in file space. */
13753 opcode_address = fragP->fr_address + fragP->fr_fix;
13754
13755 /* Displacement from opcode start to fill into instruction. */
13756 displacement_from_opcode_start = target_address - opcode_address;
13757
13758 if ((fragP->fr_subtype & BIG) == 0)
13759 {
13760 /* Don't have to change opcode. */
13761 extension = 1; /* 1 opcode + 1 displacement */
13762 where_to_put_displacement = &opcode[1];
13763 }
13764 else
13765 {
13766 if (no_cond_jump_promotion
13767 && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
13768 as_warn_where (fragP->fr_file, fragP->fr_line,
13769 _("long jump required"));
13770
13771 switch (fragP->fr_subtype)
13772 {
13773 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
13774 extension = 4; /* 1 opcode + 4 displacement */
13775 opcode[0] = 0xe9;
13776 where_to_put_displacement = &opcode[1];
13777 break;
13778
13779 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
13780 extension = 2; /* 1 opcode + 2 displacement */
13781 opcode[0] = 0xe9;
13782 where_to_put_displacement = &opcode[1];
13783 break;
13784
13785 case ENCODE_RELAX_STATE (COND_JUMP, BIG):
13786 case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
13787 extension = 5; /* 2 opcode + 4 displacement */
13788 opcode[1] = opcode[0] + 0x10;
13789 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
13790 where_to_put_displacement = &opcode[2];
13791 break;
13792
13793 case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
13794 extension = 3; /* 2 opcode + 2 displacement */
13795 opcode[1] = opcode[0] + 0x10;
13796 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
13797 where_to_put_displacement = &opcode[2];
13798 break;
13799
13800 case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
13801 extension = 4;
13802 opcode[0] ^= 1;
13803 opcode[1] = 3;
13804 opcode[2] = 0xe9;
13805 where_to_put_displacement = &opcode[3];
13806 break;
13807
13808 default:
13809 BAD_CASE (fragP->fr_subtype);
13810 break;
13811 }
13812 }
13813
13814 /* If size if less then four we are sure that the operand fits,
13815 but if it's 4, then it could be that the displacement is larger
13816 then -/+ 2GB. */
13817 if (DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype) == 4
13818 && object_64bit
13819 && ((addressT) (displacement_from_opcode_start - extension
13820 + ((addressT) 1 << 31))
13821 > (((addressT) 2 << 31) - 1)))
13822 {
13823 as_bad_where (fragP->fr_file, fragP->fr_line,
13824 _("jump target out of range"));
13825 /* Make us emit 0. */
13826 displacement_from_opcode_start = extension;
13827 }
13828 /* Now put displacement after opcode. */
13829 md_number_to_chars ((char *) where_to_put_displacement,
13830 (valueT) (displacement_from_opcode_start - extension),
13831 DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
13832 fragP->fr_fix += extension;
13833 }
13834 \f
13835 /* Apply a fixup (fixP) to segment data, once it has been determined
13836 by our caller that we have all the info we need to fix it up.
13837
13838 Parameter valP is the pointer to the value of the bits.
13839
13840 On the 386, immediates, displacements, and data pointers are all in
13841 the same (little-endian) format, so we don't need to care about which
13842 we are handling. */
13843
13844 void
13845 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
13846 {
13847 char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
13848 valueT value = *valP;
13849
13850 #if !defined (TE_Mach)
13851 if (fixP->fx_pcrel)
13852 {
13853 switch (fixP->fx_r_type)
13854 {
13855 default:
13856 break;
13857
13858 case BFD_RELOC_64:
13859 fixP->fx_r_type = BFD_RELOC_64_PCREL;
13860 break;
13861 case BFD_RELOC_32:
13862 case BFD_RELOC_X86_64_32S:
13863 fixP->fx_r_type = BFD_RELOC_32_PCREL;
13864 break;
13865 case BFD_RELOC_16:
13866 fixP->fx_r_type = BFD_RELOC_16_PCREL;
13867 break;
13868 case BFD_RELOC_8:
13869 fixP->fx_r_type = BFD_RELOC_8_PCREL;
13870 break;
13871 }
13872 }
13873
13874 if (fixP->fx_addsy != NULL
13875 && (fixP->fx_r_type == BFD_RELOC_32_PCREL
13876 || fixP->fx_r_type == BFD_RELOC_64_PCREL
13877 || fixP->fx_r_type == BFD_RELOC_16_PCREL
13878 || fixP->fx_r_type == BFD_RELOC_8_PCREL)
13879 && !use_rela_relocations)
13880 {
13881 /* This is a hack. There should be a better way to handle this.
13882 This covers for the fact that bfd_install_relocation will
13883 subtract the current location (for partial_inplace, PC relative
13884 relocations); see more below. */
13885 #ifndef OBJ_AOUT
13886 if (IS_ELF
13887 #ifdef TE_PE
13888 || OUTPUT_FLAVOR == bfd_target_coff_flavour
13889 #endif
13890 )
13891 value += fixP->fx_where + fixP->fx_frag->fr_address;
13892 #endif
13893 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13894 if (IS_ELF)
13895 {
13896 segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
13897
13898 if ((sym_seg == seg
13899 || (symbol_section_p (fixP->fx_addsy)
13900 && sym_seg != absolute_section))
13901 && !generic_force_reloc (fixP))
13902 {
13903 /* Yes, we add the values in twice. This is because
13904 bfd_install_relocation subtracts them out again. I think
13905 bfd_install_relocation is broken, but I don't dare change
13906 it. FIXME. */
13907 value += fixP->fx_where + fixP->fx_frag->fr_address;
13908 }
13909 }
13910 #endif
13911 #if defined (OBJ_COFF) && defined (TE_PE)
13912 /* For some reason, the PE format does not store a
13913 section address offset for a PC relative symbol. */
13914 if (S_GET_SEGMENT (fixP->fx_addsy) != seg
13915 || S_IS_WEAK (fixP->fx_addsy))
13916 value += md_pcrel_from (fixP);
13917 #endif
13918 }
13919 #if defined (OBJ_COFF) && defined (TE_PE)
13920 if (fixP->fx_addsy != NULL
13921 && S_IS_WEAK (fixP->fx_addsy)
13922 /* PR 16858: Do not modify weak function references. */
13923 && ! fixP->fx_pcrel)
13924 {
13925 #if !defined (TE_PEP)
13926 /* For x86 PE weak function symbols are neither PC-relative
13927 nor do they set S_IS_FUNCTION. So the only reliable way
13928 to detect them is to check the flags of their containing
13929 section. */
13930 if (S_GET_SEGMENT (fixP->fx_addsy) != NULL
13931 && S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_CODE)
13932 ;
13933 else
13934 #endif
13935 value -= S_GET_VALUE (fixP->fx_addsy);
13936 }
13937 #endif
13938
13939 /* Fix a few things - the dynamic linker expects certain values here,
13940 and we must not disappoint it. */
13941 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13942 if (IS_ELF && fixP->fx_addsy)
13943 switch (fixP->fx_r_type)
13944 {
13945 case BFD_RELOC_386_PLT32:
13946 case BFD_RELOC_X86_64_PLT32:
13947 /* Make the jump instruction point to the address of the operand.
13948 At runtime we merely add the offset to the actual PLT entry.
13949 NB: Subtract the offset size only for jump instructions. */
13950 if (fixP->fx_pcrel)
13951 value = -4;
13952 break;
13953
13954 case BFD_RELOC_386_TLS_GD:
13955 case BFD_RELOC_386_TLS_LDM:
13956 case BFD_RELOC_386_TLS_IE_32:
13957 case BFD_RELOC_386_TLS_IE:
13958 case BFD_RELOC_386_TLS_GOTIE:
13959 case BFD_RELOC_386_TLS_GOTDESC:
13960 case BFD_RELOC_X86_64_TLSGD:
13961 case BFD_RELOC_X86_64_TLSLD:
13962 case BFD_RELOC_X86_64_GOTTPOFF:
13963 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
13964 value = 0; /* Fully resolved at runtime. No addend. */
13965 /* Fallthrough */
13966 case BFD_RELOC_386_TLS_LE:
13967 case BFD_RELOC_386_TLS_LDO_32:
13968 case BFD_RELOC_386_TLS_LE_32:
13969 case BFD_RELOC_X86_64_DTPOFF32:
13970 case BFD_RELOC_X86_64_DTPOFF64:
13971 case BFD_RELOC_X86_64_TPOFF32:
13972 case BFD_RELOC_X86_64_TPOFF64:
13973 S_SET_THREAD_LOCAL (fixP->fx_addsy);
13974 break;
13975
13976 case BFD_RELOC_386_TLS_DESC_CALL:
13977 case BFD_RELOC_X86_64_TLSDESC_CALL:
13978 value = 0; /* Fully resolved at runtime. No addend. */
13979 S_SET_THREAD_LOCAL (fixP->fx_addsy);
13980 fixP->fx_done = 0;
13981 return;
13982
13983 case BFD_RELOC_VTABLE_INHERIT:
13984 case BFD_RELOC_VTABLE_ENTRY:
13985 fixP->fx_done = 0;
13986 return;
13987
13988 default:
13989 break;
13990 }
13991 #endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) */
13992
13993 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
13994 if (!object_64bit)
13995 value = extend_to_32bit_address (value);
13996
13997 *valP = value;
13998 #endif /* !defined (TE_Mach) */
13999
14000 /* Are we finished with this relocation now? */
14001 if (fixP->fx_addsy == NULL)
14002 {
14003 fixP->fx_done = 1;
14004 switch (fixP->fx_r_type)
14005 {
14006 case BFD_RELOC_X86_64_32S:
14007 fixP->fx_signed = 1;
14008 break;
14009
14010 default:
14011 break;
14012 }
14013 }
14014 #if defined (OBJ_COFF) && defined (TE_PE)
14015 else if (fixP->fx_addsy != NULL && S_IS_WEAK (fixP->fx_addsy))
14016 {
14017 fixP->fx_done = 0;
14018 /* Remember value for tc_gen_reloc. */
14019 fixP->fx_addnumber = value;
14020 /* Clear out the frag for now. */
14021 value = 0;
14022 }
14023 #endif
14024 else if (use_rela_relocations)
14025 {
14026 if (!disallow_64bit_reloc || fixP->fx_r_type == NO_RELOC)
14027 fixP->fx_no_overflow = 1;
14028 /* Remember value for tc_gen_reloc. */
14029 fixP->fx_addnumber = value;
14030 value = 0;
14031 }
14032
14033 md_number_to_chars (p, value, fixP->fx_size);
14034 }
14035 \f
14036 const char *
14037 md_atof (int type, char *litP, int *sizeP)
14038 {
14039 /* This outputs the LITTLENUMs in REVERSE order;
14040 in accord with the bigendian 386. */
14041 return ieee_md_atof (type, litP, sizeP, false);
14042 }
14043 \f
14044 static char output_invalid_buf[sizeof (unsigned char) * 2 + 6];
14045
14046 static char *
14047 output_invalid (int c)
14048 {
14049 if (ISPRINT (c))
14050 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
14051 "'%c'", c);
14052 else
14053 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
14054 "(0x%x)", (unsigned char) c);
14055 return output_invalid_buf;
14056 }
14057
14058 /* Verify that @r can be used in the current context. */
14059
14060 static bool check_register (const reg_entry *r)
14061 {
14062 if (allow_pseudo_reg)
14063 return true;
14064
14065 if (operand_type_all_zero (&r->reg_type))
14066 return false;
14067
14068 if ((r->reg_type.bitfield.dword
14069 || (r->reg_type.bitfield.class == SReg && r->reg_num > 3)
14070 || r->reg_type.bitfield.class == RegCR
14071 || r->reg_type.bitfield.class == RegDR)
14072 && !cpu_arch_flags.bitfield.cpui386)
14073 return false;
14074
14075 if (r->reg_type.bitfield.class == RegTR
14076 && (flag_code == CODE_64BIT
14077 || !cpu_arch_flags.bitfield.cpui386
14078 || cpu_arch_isa_flags.bitfield.cpui586
14079 || cpu_arch_isa_flags.bitfield.cpui686))
14080 return false;
14081
14082 if (r->reg_type.bitfield.class == RegMMX && !cpu_arch_flags.bitfield.cpummx)
14083 return false;
14084
14085 if (!cpu_arch_flags.bitfield.cpuavx512f)
14086 {
14087 if (r->reg_type.bitfield.zmmword
14088 || r->reg_type.bitfield.class == RegMask)
14089 return false;
14090
14091 if (!cpu_arch_flags.bitfield.cpuavx)
14092 {
14093 if (r->reg_type.bitfield.ymmword)
14094 return false;
14095
14096 if (!cpu_arch_flags.bitfield.cpusse && r->reg_type.bitfield.xmmword)
14097 return false;
14098 }
14099 }
14100
14101 if (r->reg_type.bitfield.zmmword)
14102 {
14103 if (vector_size < VSZ512)
14104 return false;
14105
14106 if (i.vec_encoding == vex_encoding_default)
14107 i.vec_encoding = vex_encoding_evex512;
14108 else if (i.vec_encoding != vex_encoding_evex
14109 && i.vec_encoding != vex_encoding_evex512)
14110 i.vec_encoding = vex_encoding_error;
14111 }
14112
14113 if (vector_size < VSZ256 && r->reg_type.bitfield.ymmword)
14114 return false;
14115
14116 if (r->reg_type.bitfield.tmmword
14117 && (!cpu_arch_flags.bitfield.cpuamx_tile
14118 || flag_code != CODE_64BIT))
14119 return false;
14120
14121 if (r->reg_type.bitfield.class == RegBND && !cpu_arch_flags.bitfield.cpumpx)
14122 return false;
14123
14124 /* Don't allow fake index register unless allow_index_reg isn't 0. */
14125 if (!allow_index_reg && r->reg_num == RegIZ)
14126 return false;
14127
14128 /* Upper 16 vector registers are only available with VREX in 64bit
14129 mode, and require EVEX encoding. */
14130 if (r->reg_flags & RegVRex)
14131 {
14132 if (!cpu_arch_flags.bitfield.cpuavx512f
14133 || flag_code != CODE_64BIT)
14134 return false;
14135
14136 if (i.vec_encoding == vex_encoding_default
14137 || i.vec_encoding == vex_encoding_evex512)
14138 i.vec_encoding = vex_encoding_evex;
14139 else if (i.vec_encoding != vex_encoding_evex)
14140 i.vec_encoding = vex_encoding_error;
14141 }
14142
14143 if (((r->reg_flags & (RegRex64 | RegRex)) || r->reg_type.bitfield.qword)
14144 && (!cpu_arch_flags.bitfield.cpu64
14145 || r->reg_type.bitfield.class != RegCR
14146 || dot_insn ())
14147 && flag_code != CODE_64BIT)
14148 return false;
14149
14150 if (r->reg_type.bitfield.class == SReg && r->reg_num == RegFlat
14151 && !intel_syntax)
14152 return false;
14153
14154 return true;
14155 }
14156
14157 /* REG_STRING starts *before* REGISTER_PREFIX. */
14158
14159 static const reg_entry *
14160 parse_real_register (const char *reg_string, char **end_op)
14161 {
14162 const char *s = reg_string;
14163 char *p;
14164 char reg_name_given[MAX_REG_NAME_SIZE + 1];
14165 const reg_entry *r;
14166
14167 /* Skip possible REGISTER_PREFIX and possible whitespace. */
14168 if (*s == REGISTER_PREFIX)
14169 ++s;
14170
14171 if (is_space_char (*s))
14172 ++s;
14173
14174 p = reg_name_given;
14175 while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
14176 {
14177 if (p >= reg_name_given + MAX_REG_NAME_SIZE)
14178 return (const reg_entry *) NULL;
14179 s++;
14180 }
14181
14182 if (is_part_of_name (*s))
14183 return (const reg_entry *) NULL;
14184
14185 *end_op = (char *) s;
14186
14187 r = (const reg_entry *) str_hash_find (reg_hash, reg_name_given);
14188
14189 /* Handle floating point regs, allowing spaces in the (i) part. */
14190 if (r == reg_st0)
14191 {
14192 if (!cpu_arch_flags.bitfield.cpu8087
14193 && !cpu_arch_flags.bitfield.cpu287
14194 && !cpu_arch_flags.bitfield.cpu387
14195 && !allow_pseudo_reg)
14196 return (const reg_entry *) NULL;
14197
14198 if (is_space_char (*s))
14199 ++s;
14200 if (*s == '(')
14201 {
14202 ++s;
14203 if (is_space_char (*s))
14204 ++s;
14205 if (*s >= '0' && *s <= '7')
14206 {
14207 int fpr = *s - '0';
14208 ++s;
14209 if (is_space_char (*s))
14210 ++s;
14211 if (*s == ')')
14212 {
14213 *end_op = (char *) s + 1;
14214 know (r[fpr].reg_num == fpr);
14215 return r + fpr;
14216 }
14217 }
14218 /* We have "%st(" then garbage. */
14219 return (const reg_entry *) NULL;
14220 }
14221 }
14222
14223 return r && check_register (r) ? r : NULL;
14224 }
14225
14226 /* REG_STRING starts *before* REGISTER_PREFIX. */
14227
14228 static const reg_entry *
14229 parse_register (const char *reg_string, char **end_op)
14230 {
14231 const reg_entry *r;
14232
14233 if (*reg_string == REGISTER_PREFIX || allow_naked_reg)
14234 r = parse_real_register (reg_string, end_op);
14235 else
14236 r = NULL;
14237 if (!r)
14238 {
14239 char *save = input_line_pointer;
14240 char *buf = xstrdup (reg_string), *name;
14241 symbolS *symbolP;
14242
14243 input_line_pointer = buf;
14244 get_symbol_name (&name);
14245 symbolP = symbol_find (name);
14246 while (symbolP && symbol_equated_p (symbolP))
14247 {
14248 const expressionS *e = symbol_get_value_expression(symbolP);
14249
14250 if (e->X_add_number)
14251 break;
14252 symbolP = e->X_add_symbol;
14253 }
14254 if (symbolP && S_GET_SEGMENT (symbolP) == reg_section)
14255 {
14256 const expressionS *e = symbol_get_value_expression (symbolP);
14257
14258 if (e->X_op == O_register)
14259 {
14260 know (e->X_add_number >= 0
14261 && (valueT) e->X_add_number < i386_regtab_size);
14262 r = i386_regtab + e->X_add_number;
14263 *end_op = (char *) reg_string + (input_line_pointer - buf);
14264 }
14265 if (r && !check_register (r))
14266 {
14267 as_bad (_("register '%s%s' cannot be used here"),
14268 register_prefix, r->reg_name);
14269 r = &bad_reg;
14270 }
14271 }
14272 input_line_pointer = save;
14273 free (buf);
14274 }
14275 return r;
14276 }
14277
14278 int
14279 i386_parse_name (char *name, expressionS *e, char *nextcharP)
14280 {
14281 const reg_entry *r = NULL;
14282 char *end = input_line_pointer;
14283
14284 /* We only know the terminating character here. It being double quote could
14285 be the closing one of a quoted symbol name, or an opening one from a
14286 following string (or another quoted symbol name). Since the latter can't
14287 be valid syntax for anything, bailing in either case is good enough. */
14288 if (*nextcharP == '"')
14289 return 0;
14290
14291 *end = *nextcharP;
14292 if (*name == REGISTER_PREFIX || allow_naked_reg)
14293 r = parse_real_register (name, &input_line_pointer);
14294 if (r && end <= input_line_pointer)
14295 {
14296 *nextcharP = *input_line_pointer;
14297 *input_line_pointer = 0;
14298 e->X_op = O_register;
14299 e->X_add_number = r - i386_regtab;
14300 return 1;
14301 }
14302 input_line_pointer = end;
14303 *end = 0;
14304 return intel_syntax ? i386_intel_parse_name (name, e) : 0;
14305 }
14306
14307 void
14308 md_operand (expressionS *e)
14309 {
14310 char *end;
14311 const reg_entry *r;
14312
14313 switch (*input_line_pointer)
14314 {
14315 case REGISTER_PREFIX:
14316 r = parse_real_register (input_line_pointer, &end);
14317 if (r)
14318 {
14319 e->X_op = O_register;
14320 e->X_add_number = r - i386_regtab;
14321 input_line_pointer = end;
14322 }
14323 break;
14324
14325 case '[':
14326 gas_assert (intel_syntax);
14327 end = input_line_pointer++;
14328 expression (e);
14329 if (*input_line_pointer == ']')
14330 {
14331 ++input_line_pointer;
14332 e->X_op_symbol = make_expr_symbol (e);
14333 e->X_add_symbol = NULL;
14334 e->X_add_number = 0;
14335 e->X_op = O_index;
14336 }
14337 else
14338 {
14339 e->X_op = O_absent;
14340 input_line_pointer = end;
14341 }
14342 break;
14343 }
14344 }
14345
14346 #ifdef BFD64
14347 /* To maintain consistency with !BFD64 builds of gas record, whether any
14348 (binary) operator was involved in an expression. As expressions are
14349 evaluated in only 32 bits when !BFD64, we use this to decide whether to
14350 truncate results. */
14351 bool i386_record_operator (operatorT op,
14352 const expressionS *left,
14353 const expressionS *right)
14354 {
14355 if (op == O_absent)
14356 return false;
14357
14358 if (!left)
14359 {
14360 /* Since the expression parser applies unary operators fine to bignum
14361 operands, we don't need to be concerned of respective operands not
14362 fitting in 32 bits. */
14363 if (right->X_op == O_constant && right->X_unsigned
14364 && !fits_in_unsigned_long (right->X_add_number))
14365 return false;
14366 }
14367 /* This isn't entirely right: The pattern can also result when constant
14368 expressions are folded (e.g. 0xffffffff + 1). */
14369 else if ((left->X_op == O_constant && left->X_unsigned
14370 && !fits_in_unsigned_long (left->X_add_number))
14371 || (right->X_op == O_constant && right->X_unsigned
14372 && !fits_in_unsigned_long (right->X_add_number)))
14373 expr_mode = expr_large_value;
14374
14375 if (expr_mode != expr_large_value)
14376 expr_mode = expr_operator_present;
14377
14378 return false;
14379 }
14380 #endif
14381 \f
14382 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14383 const char *md_shortopts = "kVQ:sqnO::";
14384 #else
14385 const char *md_shortopts = "qnO::";
14386 #endif
14387
14388 #define OPTION_32 (OPTION_MD_BASE + 0)
14389 #define OPTION_64 (OPTION_MD_BASE + 1)
14390 #define OPTION_DIVIDE (OPTION_MD_BASE + 2)
14391 #define OPTION_MARCH (OPTION_MD_BASE + 3)
14392 #define OPTION_MTUNE (OPTION_MD_BASE + 4)
14393 #define OPTION_MMNEMONIC (OPTION_MD_BASE + 5)
14394 #define OPTION_MSYNTAX (OPTION_MD_BASE + 6)
14395 #define OPTION_MINDEX_REG (OPTION_MD_BASE + 7)
14396 #define OPTION_MNAKED_REG (OPTION_MD_BASE + 8)
14397 #define OPTION_MRELAX_RELOCATIONS (OPTION_MD_BASE + 9)
14398 #define OPTION_MSSE2AVX (OPTION_MD_BASE + 10)
14399 #define OPTION_MSSE_CHECK (OPTION_MD_BASE + 11)
14400 #define OPTION_MOPERAND_CHECK (OPTION_MD_BASE + 12)
14401 #define OPTION_MAVXSCALAR (OPTION_MD_BASE + 13)
14402 #define OPTION_X32 (OPTION_MD_BASE + 14)
14403 #define OPTION_MADD_BND_PREFIX (OPTION_MD_BASE + 15)
14404 #define OPTION_MEVEXLIG (OPTION_MD_BASE + 16)
14405 #define OPTION_MEVEXWIG (OPTION_MD_BASE + 17)
14406 #define OPTION_MBIG_OBJ (OPTION_MD_BASE + 18)
14407 #define OPTION_MOMIT_LOCK_PREFIX (OPTION_MD_BASE + 19)
14408 #define OPTION_MEVEXRCIG (OPTION_MD_BASE + 20)
14409 #define OPTION_MSHARED (OPTION_MD_BASE + 21)
14410 #define OPTION_MAMD64 (OPTION_MD_BASE + 22)
14411 #define OPTION_MINTEL64 (OPTION_MD_BASE + 23)
14412 #define OPTION_MFENCE_AS_LOCK_ADD (OPTION_MD_BASE + 24)
14413 #define OPTION_X86_USED_NOTE (OPTION_MD_BASE + 25)
14414 #define OPTION_MVEXWIG (OPTION_MD_BASE + 26)
14415 #define OPTION_MALIGN_BRANCH_BOUNDARY (OPTION_MD_BASE + 27)
14416 #define OPTION_MALIGN_BRANCH_PREFIX_SIZE (OPTION_MD_BASE + 28)
14417 #define OPTION_MALIGN_BRANCH (OPTION_MD_BASE + 29)
14418 #define OPTION_MBRANCHES_WITH_32B_BOUNDARIES (OPTION_MD_BASE + 30)
14419 #define OPTION_MLFENCE_AFTER_LOAD (OPTION_MD_BASE + 31)
14420 #define OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH (OPTION_MD_BASE + 32)
14421 #define OPTION_MLFENCE_BEFORE_RET (OPTION_MD_BASE + 33)
14422 #define OPTION_MUSE_UNALIGNED_VECTOR_MOVE (OPTION_MD_BASE + 34)
14423
14424 struct option md_longopts[] =
14425 {
14426 {"32", no_argument, NULL, OPTION_32},
14427 #if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
14428 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
14429 {"64", no_argument, NULL, OPTION_64},
14430 #endif
14431 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14432 {"x32", no_argument, NULL, OPTION_X32},
14433 {"mshared", no_argument, NULL, OPTION_MSHARED},
14434 {"mx86-used-note", required_argument, NULL, OPTION_X86_USED_NOTE},
14435 #endif
14436 {"divide", no_argument, NULL, OPTION_DIVIDE},
14437 {"march", required_argument, NULL, OPTION_MARCH},
14438 {"mtune", required_argument, NULL, OPTION_MTUNE},
14439 {"mmnemonic", required_argument, NULL, OPTION_MMNEMONIC},
14440 {"msyntax", required_argument, NULL, OPTION_MSYNTAX},
14441 {"mindex-reg", no_argument, NULL, OPTION_MINDEX_REG},
14442 {"mnaked-reg", no_argument, NULL, OPTION_MNAKED_REG},
14443 {"msse2avx", no_argument, NULL, OPTION_MSSE2AVX},
14444 {"muse-unaligned-vector-move", no_argument, NULL, OPTION_MUSE_UNALIGNED_VECTOR_MOVE},
14445 {"msse-check", required_argument, NULL, OPTION_MSSE_CHECK},
14446 {"moperand-check", required_argument, NULL, OPTION_MOPERAND_CHECK},
14447 {"mavxscalar", required_argument, NULL, OPTION_MAVXSCALAR},
14448 {"mvexwig", required_argument, NULL, OPTION_MVEXWIG},
14449 {"madd-bnd-prefix", no_argument, NULL, OPTION_MADD_BND_PREFIX},
14450 {"mevexlig", required_argument, NULL, OPTION_MEVEXLIG},
14451 {"mevexwig", required_argument, NULL, OPTION_MEVEXWIG},
14452 # if defined (TE_PE) || defined (TE_PEP)
14453 {"mbig-obj", no_argument, NULL, OPTION_MBIG_OBJ},
14454 #endif
14455 {"momit-lock-prefix", required_argument, NULL, OPTION_MOMIT_LOCK_PREFIX},
14456 {"mfence-as-lock-add", required_argument, NULL, OPTION_MFENCE_AS_LOCK_ADD},
14457 {"mrelax-relocations", required_argument, NULL, OPTION_MRELAX_RELOCATIONS},
14458 {"mevexrcig", required_argument, NULL, OPTION_MEVEXRCIG},
14459 {"malign-branch-boundary", required_argument, NULL, OPTION_MALIGN_BRANCH_BOUNDARY},
14460 {"malign-branch-prefix-size", required_argument, NULL, OPTION_MALIGN_BRANCH_PREFIX_SIZE},
14461 {"malign-branch", required_argument, NULL, OPTION_MALIGN_BRANCH},
14462 {"mbranches-within-32B-boundaries", no_argument, NULL, OPTION_MBRANCHES_WITH_32B_BOUNDARIES},
14463 {"mlfence-after-load", required_argument, NULL, OPTION_MLFENCE_AFTER_LOAD},
14464 {"mlfence-before-indirect-branch", required_argument, NULL,
14465 OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH},
14466 {"mlfence-before-ret", required_argument, NULL, OPTION_MLFENCE_BEFORE_RET},
14467 {"mamd64", no_argument, NULL, OPTION_MAMD64},
14468 {"mintel64", no_argument, NULL, OPTION_MINTEL64},
14469 {NULL, no_argument, NULL, 0}
14470 };
14471 size_t md_longopts_size = sizeof (md_longopts);
14472
14473 int
14474 md_parse_option (int c, const char *arg)
14475 {
14476 unsigned int j;
14477 char *arch, *next, *saved, *type;
14478
14479 switch (c)
14480 {
14481 case 'n':
14482 optimize_align_code = 0;
14483 break;
14484
14485 case 'q':
14486 quiet_warnings = 1;
14487 break;
14488
14489 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14490 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
14491 should be emitted or not. FIXME: Not implemented. */
14492 case 'Q':
14493 if ((arg[0] != 'y' && arg[0] != 'n') || arg[1])
14494 return 0;
14495 break;
14496
14497 /* -V: SVR4 argument to print version ID. */
14498 case 'V':
14499 print_version_id ();
14500 break;
14501
14502 /* -k: Ignore for FreeBSD compatibility. */
14503 case 'k':
14504 break;
14505
14506 case 's':
14507 /* -s: On i386 Solaris, this tells the native assembler to use
14508 .stab instead of .stab.excl. We always use .stab anyhow. */
14509 break;
14510
14511 case OPTION_MSHARED:
14512 shared = 1;
14513 break;
14514
14515 case OPTION_X86_USED_NOTE:
14516 if (strcasecmp (arg, "yes") == 0)
14517 x86_used_note = 1;
14518 else if (strcasecmp (arg, "no") == 0)
14519 x86_used_note = 0;
14520 else
14521 as_fatal (_("invalid -mx86-used-note= option: `%s'"), arg);
14522 break;
14523
14524
14525 #endif
14526 #if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
14527 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
14528 case OPTION_64:
14529 {
14530 const char **list, **l;
14531
14532 list = bfd_target_list ();
14533 for (l = list; *l != NULL; l++)
14534 if (startswith (*l, "elf64-x86-64")
14535 || strcmp (*l, "coff-x86-64") == 0
14536 || strcmp (*l, "pe-x86-64") == 0
14537 || strcmp (*l, "pei-x86-64") == 0
14538 || strcmp (*l, "mach-o-x86-64") == 0)
14539 {
14540 default_arch = "x86_64";
14541 break;
14542 }
14543 if (*l == NULL)
14544 as_fatal (_("no compiled in support for x86_64"));
14545 free (list);
14546 }
14547 break;
14548 #endif
14549
14550 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14551 case OPTION_X32:
14552 if (IS_ELF)
14553 {
14554 const char **list, **l;
14555
14556 list = bfd_target_list ();
14557 for (l = list; *l != NULL; l++)
14558 if (startswith (*l, "elf32-x86-64"))
14559 {
14560 default_arch = "x86_64:32";
14561 break;
14562 }
14563 if (*l == NULL)
14564 as_fatal (_("no compiled in support for 32bit x86_64"));
14565 free (list);
14566 }
14567 else
14568 as_fatal (_("32bit x86_64 is only supported for ELF"));
14569 break;
14570 #endif
14571
14572 case OPTION_32:
14573 {
14574 const char **list, **l;
14575
14576 list = bfd_target_list ();
14577 for (l = list; *l != NULL; l++)
14578 if (strstr (*l, "-i386")
14579 || strstr (*l, "-go32"))
14580 {
14581 default_arch = "i386";
14582 break;
14583 }
14584 if (*l == NULL)
14585 as_fatal (_("no compiled in support for ix86"));
14586 free (list);
14587 }
14588 break;
14589
14590 case OPTION_DIVIDE:
14591 #ifdef SVR4_COMMENT_CHARS
14592 {
14593 char *n, *t;
14594 const char *s;
14595
14596 n = XNEWVEC (char, strlen (i386_comment_chars) + 1);
14597 t = n;
14598 for (s = i386_comment_chars; *s != '\0'; s++)
14599 if (*s != '/')
14600 *t++ = *s;
14601 *t = '\0';
14602 i386_comment_chars = n;
14603 }
14604 #endif
14605 break;
14606
14607 case OPTION_MARCH:
14608 saved = xstrdup (arg);
14609 arch = saved;
14610 /* Allow -march=+nosse. */
14611 if (*arch == '+')
14612 arch++;
14613 do
14614 {
14615 char *vsz;
14616
14617 if (*arch == '.')
14618 as_fatal (_("invalid -march= option: `%s'"), arg);
14619 next = strchr (arch, '+');
14620 if (next)
14621 *next++ = '\0';
14622 vsz = strchr (arch, '/');
14623 if (vsz)
14624 *vsz++ = '\0';
14625 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
14626 {
14627 if (vsz && cpu_arch[j].vsz != vsz_set)
14628 continue;
14629
14630 if (arch == saved && cpu_arch[j].type != PROCESSOR_NONE
14631 && strcmp (arch, cpu_arch[j].name) == 0)
14632 {
14633 /* Processor. */
14634 if (! cpu_arch[j].enable.bitfield.cpui386)
14635 continue;
14636
14637 cpu_arch_name = cpu_arch[j].name;
14638 free (cpu_sub_arch_name);
14639 cpu_sub_arch_name = NULL;
14640 cpu_arch_flags = cpu_arch[j].enable;
14641 cpu_arch_isa = cpu_arch[j].type;
14642 cpu_arch_isa_flags = cpu_arch[j].enable;
14643 if (!cpu_arch_tune_set)
14644 cpu_arch_tune = cpu_arch_isa;
14645 vector_size = VSZ_DEFAULT;
14646 break;
14647 }
14648 else if (cpu_arch[j].type == PROCESSOR_NONE
14649 && strcmp (arch, cpu_arch[j].name) == 0
14650 && !cpu_flags_all_zero (&cpu_arch[j].enable))
14651 {
14652 /* ISA extension. */
14653 isa_enable (j);
14654
14655 switch (cpu_arch[j].vsz)
14656 {
14657 default:
14658 break;
14659
14660 case vsz_set:
14661 if (vsz)
14662 {
14663 char *end;
14664 unsigned long val = strtoul (vsz, &end, 0);
14665
14666 if (*end)
14667 val = 0;
14668 switch (val)
14669 {
14670 case 512: vector_size = VSZ512; break;
14671 case 256: vector_size = VSZ256; break;
14672 case 128: vector_size = VSZ128; break;
14673 default:
14674 as_warn (_("Unrecognized vector size specifier ignored"));
14675 break;
14676 }
14677 break;
14678 }
14679 /* Fall through. */
14680 case vsz_reset:
14681 vector_size = VSZ_DEFAULT;
14682 break;
14683 }
14684
14685 break;
14686 }
14687 }
14688
14689 if (j >= ARRAY_SIZE (cpu_arch) && startswith (arch, "no"))
14690 {
14691 /* Disable an ISA extension. */
14692 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
14693 if (cpu_arch[j].type == PROCESSOR_NONE
14694 && strcmp (arch + 2, cpu_arch[j].name) == 0)
14695 {
14696 isa_disable (j);
14697 if (cpu_arch[j].vsz == vsz_set)
14698 vector_size = VSZ_DEFAULT;
14699 break;
14700 }
14701 }
14702
14703 if (j >= ARRAY_SIZE (cpu_arch))
14704 as_fatal (_("invalid -march= option: `%s'"), arg);
14705
14706 arch = next;
14707 }
14708 while (next != NULL);
14709 free (saved);
14710 break;
14711
14712 case OPTION_MTUNE:
14713 if (*arg == '.')
14714 as_fatal (_("invalid -mtune= option: `%s'"), arg);
14715 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
14716 {
14717 if (cpu_arch[j].type != PROCESSOR_NONE
14718 && strcmp (arg, cpu_arch[j].name) == 0)
14719 {
14720 cpu_arch_tune_set = 1;
14721 cpu_arch_tune = cpu_arch [j].type;
14722 break;
14723 }
14724 }
14725 if (j >= ARRAY_SIZE (cpu_arch))
14726 as_fatal (_("invalid -mtune= option: `%s'"), arg);
14727 break;
14728
14729 case OPTION_MMNEMONIC:
14730 if (strcasecmp (arg, "att") == 0)
14731 intel_mnemonic = 0;
14732 else if (strcasecmp (arg, "intel") == 0)
14733 intel_mnemonic = 1;
14734 else
14735 as_fatal (_("invalid -mmnemonic= option: `%s'"), arg);
14736 break;
14737
14738 case OPTION_MSYNTAX:
14739 if (strcasecmp (arg, "att") == 0)
14740 intel_syntax = 0;
14741 else if (strcasecmp (arg, "intel") == 0)
14742 intel_syntax = 1;
14743 else
14744 as_fatal (_("invalid -msyntax= option: `%s'"), arg);
14745 break;
14746
14747 case OPTION_MINDEX_REG:
14748 allow_index_reg = 1;
14749 break;
14750
14751 case OPTION_MNAKED_REG:
14752 allow_naked_reg = 1;
14753 break;
14754
14755 case OPTION_MSSE2AVX:
14756 sse2avx = 1;
14757 break;
14758
14759 case OPTION_MUSE_UNALIGNED_VECTOR_MOVE:
14760 use_unaligned_vector_move = 1;
14761 break;
14762
14763 case OPTION_MSSE_CHECK:
14764 if (strcasecmp (arg, "error") == 0)
14765 sse_check = check_error;
14766 else if (strcasecmp (arg, "warning") == 0)
14767 sse_check = check_warning;
14768 else if (strcasecmp (arg, "none") == 0)
14769 sse_check = check_none;
14770 else
14771 as_fatal (_("invalid -msse-check= option: `%s'"), arg);
14772 break;
14773
14774 case OPTION_MOPERAND_CHECK:
14775 if (strcasecmp (arg, "error") == 0)
14776 operand_check = check_error;
14777 else if (strcasecmp (arg, "warning") == 0)
14778 operand_check = check_warning;
14779 else if (strcasecmp (arg, "none") == 0)
14780 operand_check = check_none;
14781 else
14782 as_fatal (_("invalid -moperand-check= option: `%s'"), arg);
14783 break;
14784
14785 case OPTION_MAVXSCALAR:
14786 if (strcasecmp (arg, "128") == 0)
14787 avxscalar = vex128;
14788 else if (strcasecmp (arg, "256") == 0)
14789 avxscalar = vex256;
14790 else
14791 as_fatal (_("invalid -mavxscalar= option: `%s'"), arg);
14792 break;
14793
14794 case OPTION_MVEXWIG:
14795 if (strcmp (arg, "0") == 0)
14796 vexwig = vexw0;
14797 else if (strcmp (arg, "1") == 0)
14798 vexwig = vexw1;
14799 else
14800 as_fatal (_("invalid -mvexwig= option: `%s'"), arg);
14801 break;
14802
14803 case OPTION_MADD_BND_PREFIX:
14804 add_bnd_prefix = 1;
14805 break;
14806
14807 case OPTION_MEVEXLIG:
14808 if (strcmp (arg, "128") == 0)
14809 evexlig = evexl128;
14810 else if (strcmp (arg, "256") == 0)
14811 evexlig = evexl256;
14812 else if (strcmp (arg, "512") == 0)
14813 evexlig = evexl512;
14814 else
14815 as_fatal (_("invalid -mevexlig= option: `%s'"), arg);
14816 break;
14817
14818 case OPTION_MEVEXRCIG:
14819 if (strcmp (arg, "rne") == 0)
14820 evexrcig = rne;
14821 else if (strcmp (arg, "rd") == 0)
14822 evexrcig = rd;
14823 else if (strcmp (arg, "ru") == 0)
14824 evexrcig = ru;
14825 else if (strcmp (arg, "rz") == 0)
14826 evexrcig = rz;
14827 else
14828 as_fatal (_("invalid -mevexrcig= option: `%s'"), arg);
14829 break;
14830
14831 case OPTION_MEVEXWIG:
14832 if (strcmp (arg, "0") == 0)
14833 evexwig = evexw0;
14834 else if (strcmp (arg, "1") == 0)
14835 evexwig = evexw1;
14836 else
14837 as_fatal (_("invalid -mevexwig= option: `%s'"), arg);
14838 break;
14839
14840 # if defined (TE_PE) || defined (TE_PEP)
14841 case OPTION_MBIG_OBJ:
14842 use_big_obj = 1;
14843 break;
14844 #endif
14845
14846 case OPTION_MOMIT_LOCK_PREFIX:
14847 if (strcasecmp (arg, "yes") == 0)
14848 omit_lock_prefix = 1;
14849 else if (strcasecmp (arg, "no") == 0)
14850 omit_lock_prefix = 0;
14851 else
14852 as_fatal (_("invalid -momit-lock-prefix= option: `%s'"), arg);
14853 break;
14854
14855 case OPTION_MFENCE_AS_LOCK_ADD:
14856 if (strcasecmp (arg, "yes") == 0)
14857 avoid_fence = 1;
14858 else if (strcasecmp (arg, "no") == 0)
14859 avoid_fence = 0;
14860 else
14861 as_fatal (_("invalid -mfence-as-lock-add= option: `%s'"), arg);
14862 break;
14863
14864 case OPTION_MLFENCE_AFTER_LOAD:
14865 if (strcasecmp (arg, "yes") == 0)
14866 lfence_after_load = 1;
14867 else if (strcasecmp (arg, "no") == 0)
14868 lfence_after_load = 0;
14869 else
14870 as_fatal (_("invalid -mlfence-after-load= option: `%s'"), arg);
14871 break;
14872
14873 case OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH:
14874 if (strcasecmp (arg, "all") == 0)
14875 {
14876 lfence_before_indirect_branch = lfence_branch_all;
14877 if (lfence_before_ret == lfence_before_ret_none)
14878 lfence_before_ret = lfence_before_ret_shl;
14879 }
14880 else if (strcasecmp (arg, "memory") == 0)
14881 lfence_before_indirect_branch = lfence_branch_memory;
14882 else if (strcasecmp (arg, "register") == 0)
14883 lfence_before_indirect_branch = lfence_branch_register;
14884 else if (strcasecmp (arg, "none") == 0)
14885 lfence_before_indirect_branch = lfence_branch_none;
14886 else
14887 as_fatal (_("invalid -mlfence-before-indirect-branch= option: `%s'"),
14888 arg);
14889 break;
14890
14891 case OPTION_MLFENCE_BEFORE_RET:
14892 if (strcasecmp (arg, "or") == 0)
14893 lfence_before_ret = lfence_before_ret_or;
14894 else if (strcasecmp (arg, "not") == 0)
14895 lfence_before_ret = lfence_before_ret_not;
14896 else if (strcasecmp (arg, "shl") == 0 || strcasecmp (arg, "yes") == 0)
14897 lfence_before_ret = lfence_before_ret_shl;
14898 else if (strcasecmp (arg, "none") == 0)
14899 lfence_before_ret = lfence_before_ret_none;
14900 else
14901 as_fatal (_("invalid -mlfence-before-ret= option: `%s'"),
14902 arg);
14903 break;
14904
14905 case OPTION_MRELAX_RELOCATIONS:
14906 if (strcasecmp (arg, "yes") == 0)
14907 generate_relax_relocations = 1;
14908 else if (strcasecmp (arg, "no") == 0)
14909 generate_relax_relocations = 0;
14910 else
14911 as_fatal (_("invalid -mrelax-relocations= option: `%s'"), arg);
14912 break;
14913
14914 case OPTION_MALIGN_BRANCH_BOUNDARY:
14915 {
14916 char *end;
14917 long int align = strtoul (arg, &end, 0);
14918 if (*end == '\0')
14919 {
14920 if (align == 0)
14921 {
14922 align_branch_power = 0;
14923 break;
14924 }
14925 else if (align >= 16)
14926 {
14927 int align_power;
14928 for (align_power = 0;
14929 (align & 1) == 0;
14930 align >>= 1, align_power++)
14931 continue;
14932 /* Limit alignment power to 31. */
14933 if (align == 1 && align_power < 32)
14934 {
14935 align_branch_power = align_power;
14936 break;
14937 }
14938 }
14939 }
14940 as_fatal (_("invalid -malign-branch-boundary= value: %s"), arg);
14941 }
14942 break;
14943
14944 case OPTION_MALIGN_BRANCH_PREFIX_SIZE:
14945 {
14946 char *end;
14947 int align = strtoul (arg, &end, 0);
14948 /* Some processors only support 5 prefixes. */
14949 if (*end == '\0' && align >= 0 && align < 6)
14950 {
14951 align_branch_prefix_size = align;
14952 break;
14953 }
14954 as_fatal (_("invalid -malign-branch-prefix-size= value: %s"),
14955 arg);
14956 }
14957 break;
14958
14959 case OPTION_MALIGN_BRANCH:
14960 align_branch = 0;
14961 saved = xstrdup (arg);
14962 type = saved;
14963 do
14964 {
14965 next = strchr (type, '+');
14966 if (next)
14967 *next++ = '\0';
14968 if (strcasecmp (type, "jcc") == 0)
14969 align_branch |= align_branch_jcc_bit;
14970 else if (strcasecmp (type, "fused") == 0)
14971 align_branch |= align_branch_fused_bit;
14972 else if (strcasecmp (type, "jmp") == 0)
14973 align_branch |= align_branch_jmp_bit;
14974 else if (strcasecmp (type, "call") == 0)
14975 align_branch |= align_branch_call_bit;
14976 else if (strcasecmp (type, "ret") == 0)
14977 align_branch |= align_branch_ret_bit;
14978 else if (strcasecmp (type, "indirect") == 0)
14979 align_branch |= align_branch_indirect_bit;
14980 else
14981 as_fatal (_("invalid -malign-branch= option: `%s'"), arg);
14982 type = next;
14983 }
14984 while (next != NULL);
14985 free (saved);
14986 break;
14987
14988 case OPTION_MBRANCHES_WITH_32B_BOUNDARIES:
14989 align_branch_power = 5;
14990 align_branch_prefix_size = 5;
14991 align_branch = (align_branch_jcc_bit
14992 | align_branch_fused_bit
14993 | align_branch_jmp_bit);
14994 break;
14995
14996 case OPTION_MAMD64:
14997 isa64 = amd64;
14998 break;
14999
15000 case OPTION_MINTEL64:
15001 isa64 = intel64;
15002 break;
15003
15004 case 'O':
15005 if (arg == NULL)
15006 {
15007 optimize = 1;
15008 /* Turn off -Os. */
15009 optimize_for_space = 0;
15010 }
15011 else if (*arg == 's')
15012 {
15013 optimize_for_space = 1;
15014 /* Turn on all encoding optimizations. */
15015 optimize = INT_MAX;
15016 }
15017 else
15018 {
15019 optimize = atoi (arg);
15020 /* Turn off -Os. */
15021 optimize_for_space = 0;
15022 }
15023 break;
15024
15025 default:
15026 return 0;
15027 }
15028 return 1;
15029 }
15030
15031 #define MESSAGE_TEMPLATE \
15032 " "
15033
15034 static char *
15035 output_message (FILE *stream, char *p, char *message, char *start,
15036 int *left_p, const char *name, int len)
15037 {
15038 int size = sizeof (MESSAGE_TEMPLATE);
15039 int left = *left_p;
15040
15041 /* Reserve 2 spaces for ", " or ",\0" */
15042 left -= len + 2;
15043
15044 /* Check if there is any room. */
15045 if (left >= 0)
15046 {
15047 if (p != start)
15048 {
15049 *p++ = ',';
15050 *p++ = ' ';
15051 }
15052 p = mempcpy (p, name, len);
15053 }
15054 else
15055 {
15056 /* Output the current message now and start a new one. */
15057 *p++ = ',';
15058 *p = '\0';
15059 fprintf (stream, "%s\n", message);
15060 p = start;
15061 left = size - (start - message) - len - 2;
15062
15063 gas_assert (left >= 0);
15064
15065 p = mempcpy (p, name, len);
15066 }
15067
15068 *left_p = left;
15069 return p;
15070 }
15071
15072 static void
15073 show_arch (FILE *stream, int ext, int check)
15074 {
15075 static char message[] = MESSAGE_TEMPLATE;
15076 char *start = message + 27;
15077 char *p;
15078 int size = sizeof (MESSAGE_TEMPLATE);
15079 int left;
15080 const char *name;
15081 int len;
15082 unsigned int j;
15083
15084 p = start;
15085 left = size - (start - message);
15086
15087 if (!ext && check)
15088 {
15089 p = output_message (stream, p, message, start, &left,
15090 STRING_COMMA_LEN ("default"));
15091 p = output_message (stream, p, message, start, &left,
15092 STRING_COMMA_LEN ("push"));
15093 p = output_message (stream, p, message, start, &left,
15094 STRING_COMMA_LEN ("pop"));
15095 }
15096
15097 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
15098 {
15099 /* Should it be skipped? */
15100 if (cpu_arch [j].skip)
15101 continue;
15102
15103 name = cpu_arch [j].name;
15104 len = cpu_arch [j].len;
15105 if (cpu_arch[j].type == PROCESSOR_NONE)
15106 {
15107 /* It is an extension. Skip if we aren't asked to show it. */
15108 if (!ext || cpu_flags_all_zero (&cpu_arch[j].enable))
15109 continue;
15110 }
15111 else if (ext)
15112 {
15113 /* It is an processor. Skip if we show only extension. */
15114 continue;
15115 }
15116 else if (check && ! cpu_arch[j].enable.bitfield.cpui386)
15117 {
15118 /* It is an impossible processor - skip. */
15119 continue;
15120 }
15121
15122 p = output_message (stream, p, message, start, &left, name, len);
15123 }
15124
15125 /* Display disabled extensions. */
15126 if (ext)
15127 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
15128 {
15129 char *str;
15130
15131 if (cpu_arch[j].type != PROCESSOR_NONE
15132 || !cpu_flags_all_zero (&cpu_arch[j].enable))
15133 continue;
15134 str = xasprintf ("no%s", cpu_arch[j].name);
15135 p = output_message (stream, p, message, start, &left, str,
15136 strlen (str));
15137 free (str);
15138 }
15139
15140 *p = '\0';
15141 fprintf (stream, "%s\n", message);
15142 }
15143
15144 void
15145 md_show_usage (FILE *stream)
15146 {
15147 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15148 fprintf (stream, _("\
15149 -Qy, -Qn ignored\n\
15150 -V print assembler version number\n\
15151 -k ignored\n"));
15152 #endif
15153 fprintf (stream, _("\
15154 -n do not optimize code alignment\n\
15155 -O{012s} attempt some code optimizations\n\
15156 -q quieten some warnings\n"));
15157 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15158 fprintf (stream, _("\
15159 -s ignored\n"));
15160 #endif
15161 #ifdef BFD64
15162 # if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15163 fprintf (stream, _("\
15164 --32/--64/--x32 generate 32bit/64bit/x32 object\n"));
15165 # elif defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O)
15166 fprintf (stream, _("\
15167 --32/--64 generate 32bit/64bit object\n"));
15168 # endif
15169 #endif
15170 #ifdef SVR4_COMMENT_CHARS
15171 fprintf (stream, _("\
15172 --divide do not treat `/' as a comment character\n"));
15173 #else
15174 fprintf (stream, _("\
15175 --divide ignored\n"));
15176 #endif
15177 fprintf (stream, _("\
15178 -march=CPU[,+EXTENSION...]\n\
15179 generate code for CPU and EXTENSION, CPU is one of:\n"));
15180 show_arch (stream, 0, 1);
15181 fprintf (stream, _("\
15182 EXTENSION is combination of (possibly \"no\"-prefixed):\n"));
15183 show_arch (stream, 1, 0);
15184 fprintf (stream, _("\
15185 -mtune=CPU optimize for CPU, CPU is one of:\n"));
15186 show_arch (stream, 0, 0);
15187 fprintf (stream, _("\
15188 -msse2avx encode SSE instructions with VEX prefix\n"));
15189 fprintf (stream, _("\
15190 -muse-unaligned-vector-move\n\
15191 encode aligned vector move as unaligned vector move\n"));
15192 fprintf (stream, _("\
15193 -msse-check=[none|error|warning] (default: warning)\n\
15194 check SSE instructions\n"));
15195 fprintf (stream, _("\
15196 -moperand-check=[none|error|warning] (default: warning)\n\
15197 check operand combinations for validity\n"));
15198 fprintf (stream, _("\
15199 -mavxscalar=[128|256] (default: 128)\n\
15200 encode scalar AVX instructions with specific vector\n\
15201 length\n"));
15202 fprintf (stream, _("\
15203 -mvexwig=[0|1] (default: 0)\n\
15204 encode VEX instructions with specific VEX.W value\n\
15205 for VEX.W bit ignored instructions\n"));
15206 fprintf (stream, _("\
15207 -mevexlig=[128|256|512] (default: 128)\n\
15208 encode scalar EVEX instructions with specific vector\n\
15209 length\n"));
15210 fprintf (stream, _("\
15211 -mevexwig=[0|1] (default: 0)\n\
15212 encode EVEX instructions with specific EVEX.W value\n\
15213 for EVEX.W bit ignored instructions\n"));
15214 fprintf (stream, _("\
15215 -mevexrcig=[rne|rd|ru|rz] (default: rne)\n\
15216 encode EVEX instructions with specific EVEX.RC value\n\
15217 for SAE-only ignored instructions\n"));
15218 fprintf (stream, _("\
15219 -mmnemonic=[att|intel] "));
15220 if (SYSV386_COMPAT)
15221 fprintf (stream, _("(default: att)\n"));
15222 else
15223 fprintf (stream, _("(default: intel)\n"));
15224 fprintf (stream, _("\
15225 use AT&T/Intel mnemonic\n"));
15226 fprintf (stream, _("\
15227 -msyntax=[att|intel] (default: att)\n\
15228 use AT&T/Intel syntax\n"));
15229 fprintf (stream, _("\
15230 -mindex-reg support pseudo index registers\n"));
15231 fprintf (stream, _("\
15232 -mnaked-reg don't require `%%' prefix for registers\n"));
15233 fprintf (stream, _("\
15234 -madd-bnd-prefix add BND prefix for all valid branches\n"));
15235 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15236 fprintf (stream, _("\
15237 -mshared disable branch optimization for shared code\n"));
15238 fprintf (stream, _("\
15239 -mx86-used-note=[no|yes] "));
15240 if (DEFAULT_X86_USED_NOTE)
15241 fprintf (stream, _("(default: yes)\n"));
15242 else
15243 fprintf (stream, _("(default: no)\n"));
15244 fprintf (stream, _("\
15245 generate x86 used ISA and feature properties\n"));
15246 #endif
15247 #if defined (TE_PE) || defined (TE_PEP)
15248 fprintf (stream, _("\
15249 -mbig-obj generate big object files\n"));
15250 #endif
15251 fprintf (stream, _("\
15252 -momit-lock-prefix=[no|yes] (default: no)\n\
15253 strip all lock prefixes\n"));
15254 fprintf (stream, _("\
15255 -mfence-as-lock-add=[no|yes] (default: no)\n\
15256 encode lfence, mfence and sfence as\n\
15257 lock addl $0x0, (%%{re}sp)\n"));
15258 fprintf (stream, _("\
15259 -mrelax-relocations=[no|yes] "));
15260 if (DEFAULT_GENERATE_X86_RELAX_RELOCATIONS)
15261 fprintf (stream, _("(default: yes)\n"));
15262 else
15263 fprintf (stream, _("(default: no)\n"));
15264 fprintf (stream, _("\
15265 generate relax relocations\n"));
15266 fprintf (stream, _("\
15267 -malign-branch-boundary=NUM (default: 0)\n\
15268 align branches within NUM byte boundary\n"));
15269 fprintf (stream, _("\
15270 -malign-branch=TYPE[+TYPE...] (default: jcc+fused+jmp)\n\
15271 TYPE is combination of jcc, fused, jmp, call, ret,\n\
15272 indirect\n\
15273 specify types of branches to align\n"));
15274 fprintf (stream, _("\
15275 -malign-branch-prefix-size=NUM (default: 5)\n\
15276 align branches with NUM prefixes per instruction\n"));
15277 fprintf (stream, _("\
15278 -mbranches-within-32B-boundaries\n\
15279 align branches within 32 byte boundary\n"));
15280 fprintf (stream, _("\
15281 -mlfence-after-load=[no|yes] (default: no)\n\
15282 generate lfence after load\n"));
15283 fprintf (stream, _("\
15284 -mlfence-before-indirect-branch=[none|all|register|memory] (default: none)\n\
15285 generate lfence before indirect near branch\n"));
15286 fprintf (stream, _("\
15287 -mlfence-before-ret=[none|or|not|shl|yes] (default: none)\n\
15288 generate lfence before ret\n"));
15289 fprintf (stream, _("\
15290 -mamd64 accept only AMD64 ISA [default]\n"));
15291 fprintf (stream, _("\
15292 -mintel64 accept only Intel64 ISA\n"));
15293 }
15294
15295 #if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
15296 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
15297 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
15298
15299 /* Pick the target format to use. */
15300
15301 const char *
15302 i386_target_format (void)
15303 {
15304 if (startswith (default_arch, "x86_64"))
15305 {
15306 update_code_flag (CODE_64BIT, 1);
15307 if (default_arch[6] == '\0')
15308 x86_elf_abi = X86_64_ABI;
15309 else
15310 x86_elf_abi = X86_64_X32_ABI;
15311 }
15312 else if (!strcmp (default_arch, "i386"))
15313 update_code_flag (CODE_32BIT, 1);
15314 else if (!strcmp (default_arch, "iamcu"))
15315 {
15316 update_code_flag (CODE_32BIT, 1);
15317 if (cpu_arch_isa == PROCESSOR_UNKNOWN)
15318 {
15319 static const i386_cpu_flags iamcu_flags = CPU_IAMCU_FLAGS;
15320 cpu_arch_name = "iamcu";
15321 free (cpu_sub_arch_name);
15322 cpu_sub_arch_name = NULL;
15323 cpu_arch_flags = iamcu_flags;
15324 cpu_arch_isa = PROCESSOR_IAMCU;
15325 cpu_arch_isa_flags = iamcu_flags;
15326 if (!cpu_arch_tune_set)
15327 cpu_arch_tune = PROCESSOR_IAMCU;
15328 }
15329 else if (cpu_arch_isa != PROCESSOR_IAMCU)
15330 as_fatal (_("Intel MCU doesn't support `%s' architecture"),
15331 cpu_arch_name);
15332 }
15333 else
15334 as_fatal (_("unknown architecture"));
15335
15336 if (cpu_flags_all_zero (&cpu_arch_isa_flags))
15337 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
15338
15339 switch (OUTPUT_FLAVOR)
15340 {
15341 #if defined (OBJ_MAYBE_AOUT) || defined (OBJ_AOUT)
15342 case bfd_target_aout_flavour:
15343 return AOUT_TARGET_FORMAT;
15344 #endif
15345 #if defined (OBJ_MAYBE_COFF) || defined (OBJ_COFF)
15346 # if defined (TE_PE) || defined (TE_PEP)
15347 case bfd_target_coff_flavour:
15348 if (flag_code == CODE_64BIT)
15349 {
15350 object_64bit = 1;
15351 return use_big_obj ? "pe-bigobj-x86-64" : "pe-x86-64";
15352 }
15353 return use_big_obj ? "pe-bigobj-i386" : "pe-i386";
15354 # elif defined (TE_GO32)
15355 case bfd_target_coff_flavour:
15356 return "coff-go32";
15357 # else
15358 case bfd_target_coff_flavour:
15359 return "coff-i386";
15360 # endif
15361 #endif
15362 #if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
15363 case bfd_target_elf_flavour:
15364 {
15365 const char *format;
15366
15367 switch (x86_elf_abi)
15368 {
15369 default:
15370 format = ELF_TARGET_FORMAT;
15371 #ifndef TE_SOLARIS
15372 tls_get_addr = "___tls_get_addr";
15373 #endif
15374 break;
15375 case X86_64_ABI:
15376 use_rela_relocations = 1;
15377 object_64bit = 1;
15378 #ifndef TE_SOLARIS
15379 tls_get_addr = "__tls_get_addr";
15380 #endif
15381 format = ELF_TARGET_FORMAT64;
15382 break;
15383 case X86_64_X32_ABI:
15384 use_rela_relocations = 1;
15385 object_64bit = 1;
15386 #ifndef TE_SOLARIS
15387 tls_get_addr = "__tls_get_addr";
15388 #endif
15389 disallow_64bit_reloc = 1;
15390 format = ELF_TARGET_FORMAT32;
15391 break;
15392 }
15393 if (cpu_arch_isa == PROCESSOR_IAMCU)
15394 {
15395 if (x86_elf_abi != I386_ABI)
15396 as_fatal (_("Intel MCU is 32bit only"));
15397 return ELF_TARGET_IAMCU_FORMAT;
15398 }
15399 else
15400 return format;
15401 }
15402 #endif
15403 #if defined (OBJ_MACH_O)
15404 case bfd_target_mach_o_flavour:
15405 if (flag_code == CODE_64BIT)
15406 {
15407 use_rela_relocations = 1;
15408 object_64bit = 1;
15409 return "mach-o-x86-64";
15410 }
15411 else
15412 return "mach-o-i386";
15413 #endif
15414 default:
15415 abort ();
15416 return NULL;
15417 }
15418 }
15419
15420 #endif /* OBJ_MAYBE_ more than one */
15421 \f
15422 symbolS *
15423 md_undefined_symbol (char *name)
15424 {
15425 if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
15426 && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
15427 && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
15428 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
15429 {
15430 if (!GOT_symbol)
15431 {
15432 if (symbol_find (name))
15433 as_bad (_("GOT already in symbol table"));
15434 GOT_symbol = symbol_new (name, undefined_section,
15435 &zero_address_frag, 0);
15436 };
15437 return GOT_symbol;
15438 }
15439 return 0;
15440 }
15441
15442 /* Round up a section size to the appropriate boundary. */
15443
15444 valueT
15445 md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
15446 {
15447 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
15448 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
15449 {
15450 /* For a.out, force the section size to be aligned. If we don't do
15451 this, BFD will align it for us, but it will not write out the
15452 final bytes of the section. This may be a bug in BFD, but it is
15453 easier to fix it here since that is how the other a.out targets
15454 work. */
15455 int align;
15456
15457 align = bfd_section_alignment (segment);
15458 size = ((size + (1 << align) - 1) & (-((valueT) 1 << align)));
15459 }
15460 #endif
15461
15462 return size;
15463 }
15464
15465 /* On the i386, PC-relative offsets are relative to the start of the
15466 next instruction. That is, the address of the offset, plus its
15467 size, since the offset is always the last part of the insn. */
15468
15469 long
15470 md_pcrel_from (fixS *fixP)
15471 {
15472 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
15473 }
15474
15475 #ifndef I386COFF
15476
15477 static void
15478 s_bss (int ignore ATTRIBUTE_UNUSED)
15479 {
15480 int temp;
15481
15482 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15483 if (IS_ELF)
15484 obj_elf_section_change_hook ();
15485 #endif
15486 temp = get_absolute_expression ();
15487 subseg_set (bss_section, (subsegT) temp);
15488 demand_empty_rest_of_line ();
15489 }
15490
15491 #endif
15492
15493 /* Remember constant directive. */
15494
15495 void
15496 i386_cons_align (int ignore ATTRIBUTE_UNUSED)
15497 {
15498 if (last_insn.kind != last_insn_directive
15499 && (bfd_section_flags (now_seg) & SEC_CODE))
15500 {
15501 last_insn.seg = now_seg;
15502 last_insn.kind = last_insn_directive;
15503 last_insn.name = "constant directive";
15504 last_insn.file = as_where (&last_insn.line);
15505 if (lfence_before_ret != lfence_before_ret_none)
15506 {
15507 if (lfence_before_indirect_branch != lfence_branch_none)
15508 as_warn (_("constant directive skips -mlfence-before-ret "
15509 "and -mlfence-before-indirect-branch"));
15510 else
15511 as_warn (_("constant directive skips -mlfence-before-ret"));
15512 }
15513 else if (lfence_before_indirect_branch != lfence_branch_none)
15514 as_warn (_("constant directive skips -mlfence-before-indirect-branch"));
15515 }
15516 }
15517
15518 int
15519 i386_validate_fix (fixS *fixp)
15520 {
15521 if (fixp->fx_addsy && S_GET_SEGMENT(fixp->fx_addsy) == reg_section)
15522 {
15523 reloc_howto_type *howto;
15524
15525 howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
15526 as_bad_where (fixp->fx_file, fixp->fx_line,
15527 _("invalid %s relocation against register"),
15528 howto ? howto->name : "<unknown>");
15529 return 0;
15530 }
15531
15532 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15533 if (fixp->fx_r_type == BFD_RELOC_SIZE32
15534 || fixp->fx_r_type == BFD_RELOC_SIZE64)
15535 return IS_ELF && fixp->fx_addsy
15536 && (!S_IS_DEFINED (fixp->fx_addsy)
15537 || S_IS_EXTERNAL (fixp->fx_addsy));
15538 #endif
15539
15540 if (fixp->fx_subsy)
15541 {
15542 if (fixp->fx_subsy == GOT_symbol)
15543 {
15544 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
15545 {
15546 if (!object_64bit)
15547 abort ();
15548 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15549 if (fixp->fx_tcbit2)
15550 fixp->fx_r_type = (fixp->fx_tcbit
15551 ? BFD_RELOC_X86_64_REX_GOTPCRELX
15552 : BFD_RELOC_X86_64_GOTPCRELX);
15553 else
15554 #endif
15555 fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
15556 }
15557 else
15558 {
15559 if (!object_64bit)
15560 fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
15561 else
15562 fixp->fx_r_type = BFD_RELOC_X86_64_GOTOFF64;
15563 }
15564 fixp->fx_subsy = 0;
15565 }
15566 }
15567 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15568 else
15569 {
15570 /* NB: Commit 292676c1 resolved PLT32 reloc aganst local symbol
15571 to section. Since PLT32 relocation must be against symbols,
15572 turn such PLT32 relocation into PC32 relocation. */
15573 if (fixp->fx_addsy
15574 && (fixp->fx_r_type == BFD_RELOC_386_PLT32
15575 || fixp->fx_r_type == BFD_RELOC_X86_64_PLT32)
15576 && symbol_section_p (fixp->fx_addsy))
15577 fixp->fx_r_type = BFD_RELOC_32_PCREL;
15578 if (!object_64bit)
15579 {
15580 if (fixp->fx_r_type == BFD_RELOC_386_GOT32
15581 && fixp->fx_tcbit2)
15582 fixp->fx_r_type = BFD_RELOC_386_GOT32X;
15583 }
15584 }
15585 #endif
15586
15587 return 1;
15588 }
15589
15590 arelent *
15591 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
15592 {
15593 arelent *rel;
15594 bfd_reloc_code_real_type code;
15595
15596 switch (fixp->fx_r_type)
15597 {
15598 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15599 symbolS *sym;
15600
15601 case BFD_RELOC_SIZE32:
15602 case BFD_RELOC_SIZE64:
15603 if (fixp->fx_addsy
15604 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))
15605 && (!fixp->fx_subsy
15606 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))))
15607 sym = fixp->fx_addsy;
15608 else if (fixp->fx_subsy
15609 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))
15610 && (!fixp->fx_addsy
15611 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))))
15612 sym = fixp->fx_subsy;
15613 else
15614 sym = NULL;
15615 if (IS_ELF && sym && S_IS_DEFINED (sym) && !S_IS_EXTERNAL (sym))
15616 {
15617 /* Resolve size relocation against local symbol to size of
15618 the symbol plus addend. */
15619 valueT value = S_GET_SIZE (sym);
15620
15621 if (symbol_get_bfdsym (sym)->flags & BSF_SECTION_SYM)
15622 value = bfd_section_size (S_GET_SEGMENT (sym));
15623 if (sym == fixp->fx_subsy)
15624 {
15625 value = -value;
15626 if (fixp->fx_addsy)
15627 value += S_GET_VALUE (fixp->fx_addsy);
15628 }
15629 else if (fixp->fx_subsy)
15630 value -= S_GET_VALUE (fixp->fx_subsy);
15631 value += fixp->fx_offset;
15632 if (fixp->fx_r_type == BFD_RELOC_SIZE32
15633 && object_64bit
15634 && !fits_in_unsigned_long (value))
15635 as_bad_where (fixp->fx_file, fixp->fx_line,
15636 _("symbol size computation overflow"));
15637 fixp->fx_addsy = NULL;
15638 fixp->fx_subsy = NULL;
15639 md_apply_fix (fixp, (valueT *) &value, NULL);
15640 return NULL;
15641 }
15642 if (!fixp->fx_addsy || fixp->fx_subsy)
15643 {
15644 as_bad_where (fixp->fx_file, fixp->fx_line,
15645 "unsupported expression involving @size");
15646 return NULL;
15647 }
15648 #endif
15649 /* Fall through. */
15650
15651 case BFD_RELOC_X86_64_PLT32:
15652 case BFD_RELOC_X86_64_GOT32:
15653 case BFD_RELOC_X86_64_GOTPCREL:
15654 case BFD_RELOC_X86_64_GOTPCRELX:
15655 case BFD_RELOC_X86_64_REX_GOTPCRELX:
15656 case BFD_RELOC_386_PLT32:
15657 case BFD_RELOC_386_GOT32:
15658 case BFD_RELOC_386_GOT32X:
15659 case BFD_RELOC_386_GOTOFF:
15660 case BFD_RELOC_386_GOTPC:
15661 case BFD_RELOC_386_TLS_GD:
15662 case BFD_RELOC_386_TLS_LDM:
15663 case BFD_RELOC_386_TLS_LDO_32:
15664 case BFD_RELOC_386_TLS_IE_32:
15665 case BFD_RELOC_386_TLS_IE:
15666 case BFD_RELOC_386_TLS_GOTIE:
15667 case BFD_RELOC_386_TLS_LE_32:
15668 case BFD_RELOC_386_TLS_LE:
15669 case BFD_RELOC_386_TLS_GOTDESC:
15670 case BFD_RELOC_386_TLS_DESC_CALL:
15671 case BFD_RELOC_X86_64_TLSGD:
15672 case BFD_RELOC_X86_64_TLSLD:
15673 case BFD_RELOC_X86_64_DTPOFF32:
15674 case BFD_RELOC_X86_64_DTPOFF64:
15675 case BFD_RELOC_X86_64_GOTTPOFF:
15676 case BFD_RELOC_X86_64_TPOFF32:
15677 case BFD_RELOC_X86_64_TPOFF64:
15678 case BFD_RELOC_X86_64_GOTOFF64:
15679 case BFD_RELOC_X86_64_GOTPC32:
15680 case BFD_RELOC_X86_64_GOT64:
15681 case BFD_RELOC_X86_64_GOTPCREL64:
15682 case BFD_RELOC_X86_64_GOTPC64:
15683 case BFD_RELOC_X86_64_GOTPLT64:
15684 case BFD_RELOC_X86_64_PLTOFF64:
15685 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
15686 case BFD_RELOC_X86_64_TLSDESC_CALL:
15687 case BFD_RELOC_RVA:
15688 case BFD_RELOC_VTABLE_ENTRY:
15689 case BFD_RELOC_VTABLE_INHERIT:
15690 #ifdef TE_PE
15691 case BFD_RELOC_32_SECREL:
15692 case BFD_RELOC_16_SECIDX:
15693 #endif
15694 code = fixp->fx_r_type;
15695 break;
15696 case BFD_RELOC_X86_64_32S:
15697 if (!fixp->fx_pcrel)
15698 {
15699 /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32. */
15700 code = fixp->fx_r_type;
15701 break;
15702 }
15703 /* Fall through. */
15704 default:
15705 if (fixp->fx_pcrel)
15706 {
15707 switch (fixp->fx_size)
15708 {
15709 default:
15710 as_bad_where (fixp->fx_file, fixp->fx_line,
15711 _("can not do %d byte pc-relative relocation"),
15712 fixp->fx_size);
15713 code = BFD_RELOC_32_PCREL;
15714 break;
15715 case 1: code = BFD_RELOC_8_PCREL; break;
15716 case 2: code = BFD_RELOC_16_PCREL; break;
15717 case 4: code = BFD_RELOC_32_PCREL; break;
15718 #ifdef BFD64
15719 case 8: code = BFD_RELOC_64_PCREL; break;
15720 #endif
15721 }
15722 }
15723 else
15724 {
15725 switch (fixp->fx_size)
15726 {
15727 default:
15728 as_bad_where (fixp->fx_file, fixp->fx_line,
15729 _("can not do %d byte relocation"),
15730 fixp->fx_size);
15731 code = BFD_RELOC_32;
15732 break;
15733 case 1: code = BFD_RELOC_8; break;
15734 case 2: code = BFD_RELOC_16; break;
15735 case 4: code = BFD_RELOC_32; break;
15736 #ifdef BFD64
15737 case 8: code = BFD_RELOC_64; break;
15738 #endif
15739 }
15740 }
15741 break;
15742 }
15743
15744 if ((code == BFD_RELOC_32
15745 || code == BFD_RELOC_32_PCREL
15746 || code == BFD_RELOC_X86_64_32S)
15747 && GOT_symbol
15748 && fixp->fx_addsy == GOT_symbol)
15749 {
15750 if (!object_64bit)
15751 code = BFD_RELOC_386_GOTPC;
15752 else
15753 code = BFD_RELOC_X86_64_GOTPC32;
15754 }
15755 if ((code == BFD_RELOC_64 || code == BFD_RELOC_64_PCREL)
15756 && GOT_symbol
15757 && fixp->fx_addsy == GOT_symbol)
15758 {
15759 code = BFD_RELOC_X86_64_GOTPC64;
15760 }
15761
15762 rel = XNEW (arelent);
15763 rel->sym_ptr_ptr = XNEW (asymbol *);
15764 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
15765
15766 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
15767
15768 if (!use_rela_relocations)
15769 {
15770 /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
15771 vtable entry to be used in the relocation's section offset. */
15772 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
15773 rel->address = fixp->fx_offset;
15774 #if defined (OBJ_COFF) && defined (TE_PE)
15775 else if (fixp->fx_addsy && S_IS_WEAK (fixp->fx_addsy))
15776 rel->addend = fixp->fx_addnumber - (S_GET_VALUE (fixp->fx_addsy) * 2);
15777 else
15778 #endif
15779 rel->addend = 0;
15780 }
15781 /* Use the rela in 64bit mode. */
15782 else
15783 {
15784 if (disallow_64bit_reloc)
15785 switch (code)
15786 {
15787 case BFD_RELOC_X86_64_DTPOFF64:
15788 case BFD_RELOC_X86_64_TPOFF64:
15789 case BFD_RELOC_64_PCREL:
15790 case BFD_RELOC_X86_64_GOTOFF64:
15791 case BFD_RELOC_X86_64_GOT64:
15792 case BFD_RELOC_X86_64_GOTPCREL64:
15793 case BFD_RELOC_X86_64_GOTPC64:
15794 case BFD_RELOC_X86_64_GOTPLT64:
15795 case BFD_RELOC_X86_64_PLTOFF64:
15796 as_bad_where (fixp->fx_file, fixp->fx_line,
15797 _("cannot represent relocation type %s in x32 mode"),
15798 bfd_get_reloc_code_name (code));
15799 break;
15800 default:
15801 break;
15802 }
15803
15804 if (!fixp->fx_pcrel)
15805 rel->addend = fixp->fx_offset;
15806 else
15807 switch (code)
15808 {
15809 case BFD_RELOC_X86_64_PLT32:
15810 case BFD_RELOC_X86_64_GOT32:
15811 case BFD_RELOC_X86_64_GOTPCREL:
15812 case BFD_RELOC_X86_64_GOTPCRELX:
15813 case BFD_RELOC_X86_64_REX_GOTPCRELX:
15814 case BFD_RELOC_X86_64_TLSGD:
15815 case BFD_RELOC_X86_64_TLSLD:
15816 case BFD_RELOC_X86_64_GOTTPOFF:
15817 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
15818 case BFD_RELOC_X86_64_TLSDESC_CALL:
15819 rel->addend = fixp->fx_offset - fixp->fx_size;
15820 break;
15821 default:
15822 rel->addend = (section->vma
15823 - fixp->fx_size
15824 + fixp->fx_addnumber
15825 + md_pcrel_from (fixp));
15826 break;
15827 }
15828 }
15829
15830 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
15831 if (rel->howto == NULL)
15832 {
15833 as_bad_where (fixp->fx_file, fixp->fx_line,
15834 _("cannot represent relocation type %s"),
15835 bfd_get_reloc_code_name (code));
15836 /* Set howto to a garbage value so that we can keep going. */
15837 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
15838 gas_assert (rel->howto != NULL);
15839 }
15840
15841 return rel;
15842 }
15843
15844 #include "tc-i386-intel.c"
15845
15846 void
15847 tc_x86_parse_to_dw2regnum (expressionS *exp)
15848 {
15849 int saved_naked_reg;
15850 char saved_register_dot;
15851
15852 saved_naked_reg = allow_naked_reg;
15853 allow_naked_reg = 1;
15854 saved_register_dot = register_chars['.'];
15855 register_chars['.'] = '.';
15856 allow_pseudo_reg = 1;
15857 expression_and_evaluate (exp);
15858 allow_pseudo_reg = 0;
15859 register_chars['.'] = saved_register_dot;
15860 allow_naked_reg = saved_naked_reg;
15861
15862 if (exp->X_op == O_register && exp->X_add_number >= 0)
15863 {
15864 if ((addressT) exp->X_add_number < i386_regtab_size)
15865 {
15866 exp->X_op = O_constant;
15867 exp->X_add_number = i386_regtab[exp->X_add_number]
15868 .dw2_regnum[flag_code >> 1];
15869 }
15870 else
15871 exp->X_op = O_illegal;
15872 }
15873 }
15874
15875 void
15876 tc_x86_frame_initial_instructions (void)
15877 {
15878 static unsigned int sp_regno[2];
15879
15880 if (!sp_regno[flag_code >> 1])
15881 {
15882 char *saved_input = input_line_pointer;
15883 char sp[][4] = {"esp", "rsp"};
15884 expressionS exp;
15885
15886 input_line_pointer = sp[flag_code >> 1];
15887 tc_x86_parse_to_dw2regnum (&exp);
15888 gas_assert (exp.X_op == O_constant);
15889 sp_regno[flag_code >> 1] = exp.X_add_number;
15890 input_line_pointer = saved_input;
15891 }
15892
15893 cfi_add_CFA_def_cfa (sp_regno[flag_code >> 1], -x86_cie_data_alignment);
15894 cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
15895 }
15896
15897 int
15898 x86_dwarf2_addr_size (void)
15899 {
15900 #if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
15901 if (x86_elf_abi == X86_64_X32_ABI)
15902 return 4;
15903 #endif
15904 return bfd_arch_bits_per_address (stdoutput) / 8;
15905 }
15906
15907 int
15908 i386_elf_section_type (const char *str, size_t len)
15909 {
15910 if (flag_code == CODE_64BIT
15911 && len == sizeof ("unwind") - 1
15912 && startswith (str, "unwind"))
15913 return SHT_X86_64_UNWIND;
15914
15915 return -1;
15916 }
15917
15918 #ifdef TE_SOLARIS
15919 void
15920 i386_solaris_fix_up_eh_frame (segT sec)
15921 {
15922 if (flag_code == CODE_64BIT)
15923 elf_section_type (sec) = SHT_X86_64_UNWIND;
15924 }
15925 #endif
15926
15927 #ifdef TE_PE
15928 void
15929 tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
15930 {
15931 expressionS exp;
15932
15933 exp.X_op = O_secrel;
15934 exp.X_add_symbol = symbol;
15935 exp.X_add_number = 0;
15936 emit_expr (&exp, size);
15937 }
15938 #endif
15939
15940 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15941 /* For ELF on x86-64, add support for SHF_X86_64_LARGE. */
15942
15943 bfd_vma
15944 x86_64_section_letter (int letter, const char **ptr_msg)
15945 {
15946 if (flag_code == CODE_64BIT)
15947 {
15948 if (letter == 'l')
15949 return SHF_X86_64_LARGE;
15950
15951 *ptr_msg = _("bad .section directive: want a,l,w,x,M,S,G,T in string");
15952 }
15953 else
15954 *ptr_msg = _("bad .section directive: want a,w,x,M,S,G,T in string");
15955 return -1;
15956 }
15957
15958 static void
15959 handle_large_common (int small ATTRIBUTE_UNUSED)
15960 {
15961 if (flag_code != CODE_64BIT)
15962 {
15963 s_comm_internal (0, elf_common_parse);
15964 as_warn (_(".largecomm supported only in 64bit mode, producing .comm"));
15965 }
15966 else
15967 {
15968 static segT lbss_section;
15969 asection *saved_com_section_ptr = elf_com_section_ptr;
15970 asection *saved_bss_section = bss_section;
15971
15972 if (lbss_section == NULL)
15973 {
15974 flagword applicable;
15975 segT seg = now_seg;
15976 subsegT subseg = now_subseg;
15977
15978 /* The .lbss section is for local .largecomm symbols. */
15979 lbss_section = subseg_new (".lbss", 0);
15980 applicable = bfd_applicable_section_flags (stdoutput);
15981 bfd_set_section_flags (lbss_section, applicable & SEC_ALLOC);
15982 seg_info (lbss_section)->bss = 1;
15983
15984 subseg_set (seg, subseg);
15985 }
15986
15987 elf_com_section_ptr = &_bfd_elf_large_com_section;
15988 bss_section = lbss_section;
15989
15990 s_comm_internal (0, elf_common_parse);
15991
15992 elf_com_section_ptr = saved_com_section_ptr;
15993 bss_section = saved_bss_section;
15994 }
15995 }
15996 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */