Update the list of recognized m-profile TAG_CPU_ARCH_*
[binutils-gdb.git] / gas / config / tc-i386.c
1 /* tc-i386.c -- Assemble code for the Intel 80386
2 Copyright (C) 1989-2022 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 "elf/x86-64.h"
34 #include "opcodes/i386-init.h"
35 #include <limits.h>
36
37 #ifndef INFER_ADDR_PREFIX
38 #define INFER_ADDR_PREFIX 1
39 #endif
40
41 #ifndef DEFAULT_ARCH
42 #define DEFAULT_ARCH "i386"
43 #endif
44
45 #ifndef INLINE
46 #if __GNUC__ >= 2
47 #define INLINE __inline__
48 #else
49 #define INLINE
50 #endif
51 #endif
52
53 /* Prefixes will be emitted in the order defined below.
54 WAIT_PREFIX must be the first prefix since FWAIT is really is an
55 instruction, and so must come before any prefixes.
56 The preferred prefix order is SEG_PREFIX, ADDR_PREFIX, DATA_PREFIX,
57 REP_PREFIX/HLE_PREFIX, LOCK_PREFIX. */
58 #define WAIT_PREFIX 0
59 #define SEG_PREFIX 1
60 #define ADDR_PREFIX 2
61 #define DATA_PREFIX 3
62 #define REP_PREFIX 4
63 #define HLE_PREFIX REP_PREFIX
64 #define BND_PREFIX REP_PREFIX
65 #define LOCK_PREFIX 5
66 #define REX_PREFIX 6 /* must come last. */
67 #define MAX_PREFIXES 7 /* max prefixes per opcode */
68
69 /* we define the syntax here (modulo base,index,scale syntax) */
70 #define REGISTER_PREFIX '%'
71 #define IMMEDIATE_PREFIX '$'
72 #define ABSOLUTE_PREFIX '*'
73
74 /* these are the instruction mnemonic suffixes in AT&T syntax or
75 memory operand size in Intel syntax. */
76 #define WORD_MNEM_SUFFIX 'w'
77 #define BYTE_MNEM_SUFFIX 'b'
78 #define SHORT_MNEM_SUFFIX 's'
79 #define LONG_MNEM_SUFFIX 'l'
80 #define QWORD_MNEM_SUFFIX 'q'
81 /* Intel Syntax. Use a non-ascii letter since since it never appears
82 in instructions. */
83 #define LONG_DOUBLE_MNEM_SUFFIX '\1'
84
85 #define END_OF_INSN '\0'
86
87 /* This matches the C -> StaticRounding alias in the opcode table. */
88 #define commutative staticrounding
89
90 /*
91 'templates' is for grouping together 'template' structures for opcodes
92 of the same name. This is only used for storing the insns in the grand
93 ole hash table of insns.
94 The templates themselves start at START and range up to (but not including)
95 END.
96 */
97 typedef struct
98 {
99 const insn_template *start;
100 const insn_template *end;
101 }
102 templates;
103
104 /* 386 operand encoding bytes: see 386 book for details of this. */
105 typedef struct
106 {
107 unsigned int regmem; /* codes register or memory operand */
108 unsigned int reg; /* codes register operand (or extended opcode) */
109 unsigned int mode; /* how to interpret regmem & reg */
110 }
111 modrm_byte;
112
113 /* x86-64 extension prefix. */
114 typedef int rex_byte;
115
116 /* 386 opcode byte to code indirect addressing. */
117 typedef struct
118 {
119 unsigned base;
120 unsigned index;
121 unsigned scale;
122 }
123 sib_byte;
124
125 /* x86 arch names, types and features */
126 typedef struct
127 {
128 const char *name; /* arch name */
129 unsigned int len; /* arch string length */
130 enum processor_type type; /* arch type */
131 i386_cpu_flags flags; /* cpu feature flags */
132 unsigned int skip; /* show_arch should skip this. */
133 }
134 arch_entry;
135
136 /* Used to turn off indicated flags. */
137 typedef struct
138 {
139 const char *name; /* arch name */
140 unsigned int len; /* arch string length */
141 i386_cpu_flags flags; /* cpu feature flags */
142 }
143 noarch_entry;
144
145 static void update_code_flag (int, int);
146 static void set_code_flag (int);
147 static void set_16bit_gcc_code_flag (int);
148 static void set_intel_syntax (int);
149 static void set_intel_mnemonic (int);
150 static void set_allow_index_reg (int);
151 static void set_check (int);
152 static void set_cpu_arch (int);
153 #ifdef TE_PE
154 static void pe_directive_secrel (int);
155 #endif
156 static void signed_cons (int);
157 static char *output_invalid (int c);
158 static int i386_finalize_immediate (segT, expressionS *, i386_operand_type,
159 const char *);
160 static int i386_finalize_displacement (segT, expressionS *, i386_operand_type,
161 const char *);
162 static int i386_att_operand (char *);
163 static int i386_intel_operand (char *, int);
164 static int i386_intel_simplify (expressionS *);
165 static int i386_intel_parse_name (const char *, expressionS *);
166 static const reg_entry *parse_register (char *, char **);
167 static char *parse_insn (char *, char *);
168 static char *parse_operands (char *, const char *);
169 static void swap_operands (void);
170 static void swap_2_operands (unsigned int, unsigned int);
171 static enum flag_code i386_addressing_mode (void);
172 static void optimize_imm (void);
173 static void optimize_disp (void);
174 static const insn_template *match_template (char);
175 static int check_string (void);
176 static int process_suffix (void);
177 static int check_byte_reg (void);
178 static int check_long_reg (void);
179 static int check_qword_reg (void);
180 static int check_word_reg (void);
181 static int finalize_imm (void);
182 static int process_operands (void);
183 static const reg_entry *build_modrm_byte (void);
184 static void output_insn (void);
185 static void output_imm (fragS *, offsetT);
186 static void output_disp (fragS *, offsetT);
187 #ifndef I386COFF
188 static void s_bss (int);
189 #endif
190 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
191 static void handle_large_common (int small ATTRIBUTE_UNUSED);
192
193 /* GNU_PROPERTY_X86_ISA_1_USED. */
194 static unsigned int x86_isa_1_used;
195 /* GNU_PROPERTY_X86_FEATURE_2_USED. */
196 static unsigned int x86_feature_2_used;
197 /* Generate x86 used ISA and feature properties. */
198 static unsigned int x86_used_note = DEFAULT_X86_USED_NOTE;
199 #endif
200
201 static const char *default_arch = DEFAULT_ARCH;
202
203 /* parse_register() returns this when a register alias cannot be used. */
204 static const reg_entry bad_reg = { "<bad>", OPERAND_TYPE_NONE, 0, 0,
205 { Dw2Inval, Dw2Inval } };
206
207 static const reg_entry *reg_eax;
208 static const reg_entry *reg_ds;
209 static const reg_entry *reg_es;
210 static const reg_entry *reg_ss;
211 static const reg_entry *reg_st0;
212 static const reg_entry *reg_k0;
213
214 /* VEX prefix. */
215 typedef struct
216 {
217 /* VEX prefix is either 2 byte or 3 byte. EVEX is 4 byte. */
218 unsigned char bytes[4];
219 unsigned int length;
220 /* Destination or source register specifier. */
221 const reg_entry *register_specifier;
222 } vex_prefix;
223
224 /* 'md_assemble ()' gathers together information and puts it into a
225 i386_insn. */
226
227 union i386_op
228 {
229 expressionS *disps;
230 expressionS *imms;
231 const reg_entry *regs;
232 };
233
234 enum i386_error
235 {
236 operand_size_mismatch,
237 operand_type_mismatch,
238 register_type_mismatch,
239 number_of_operands_mismatch,
240 invalid_instruction_suffix,
241 bad_imm4,
242 unsupported_with_intel_mnemonic,
243 unsupported_syntax,
244 unsupported,
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 rc_sae_operand_not_last_imm,
258 invalid_register_operand,
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
294 /* Relocation type for operand */
295 enum bfd_reloc_code_real reloc[MAX_OPERANDS];
296
297 /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
298 the base index byte below. */
299 const reg_entry *base_reg;
300 const reg_entry *index_reg;
301 unsigned int log2_scale_factor;
302
303 /* SEG gives the seg_entries of this insn. They are zero unless
304 explicit segment overrides are given. */
305 const reg_entry *seg[2];
306
307 /* Copied first memory operand string, for re-checking. */
308 char *memop1_string;
309
310 /* PREFIX holds all the given prefix opcodes (usually null).
311 PREFIXES is the number of prefix opcodes. */
312 unsigned int prefixes;
313 unsigned char prefix[MAX_PREFIXES];
314
315 /* Register is in low 3 bits of opcode. */
316 bool short_form;
317
318 /* The operand to a branch insn indicates an absolute branch. */
319 bool jumpabsolute;
320
321 /* Extended states. */
322 enum
323 {
324 /* Use MMX state. */
325 xstate_mmx = 1 << 0,
326 /* Use XMM state. */
327 xstate_xmm = 1 << 1,
328 /* Use YMM state. */
329 xstate_ymm = 1 << 2 | xstate_xmm,
330 /* Use ZMM state. */
331 xstate_zmm = 1 << 3 | xstate_ymm,
332 /* Use TMM state. */
333 xstate_tmm = 1 << 4,
334 /* Use MASK state. */
335 xstate_mask = 1 << 5
336 } xstate;
337
338 /* Has GOTPC or TLS relocation. */
339 bool has_gotpc_tls_reloc;
340
341 /* RM and SIB are the modrm byte and the sib byte where the
342 addressing modes of this insn are encoded. */
343 modrm_byte rm;
344 rex_byte rex;
345 rex_byte vrex;
346 sib_byte sib;
347 vex_prefix vex;
348
349 /* Masking attributes.
350
351 The struct describes masking, applied to OPERAND in the instruction.
352 REG is a pointer to the corresponding mask register. ZEROING tells
353 whether merging or zeroing mask is used. */
354 struct Mask_Operation
355 {
356 const reg_entry *reg;
357 unsigned int zeroing;
358 /* The operand where this operation is associated. */
359 unsigned int operand;
360 } mask;
361
362 /* Rounding control and SAE attributes. */
363 struct RC_Operation
364 {
365 enum rc_type
366 {
367 rc_none = -1,
368 rne,
369 rd,
370 ru,
371 rz,
372 saeonly
373 } type;
374
375 unsigned int operand;
376 } rounding;
377
378 /* Broadcasting attributes.
379
380 The struct describes broadcasting, applied to OPERAND. TYPE is
381 expresses the broadcast factor. */
382 struct Broadcast_Operation
383 {
384 /* Type of broadcast: {1to2}, {1to4}, {1to8}, {1to16} or {1to32}. */
385 unsigned int type;
386
387 /* Index of broadcasted operand. */
388 unsigned int operand;
389
390 /* Number of bytes to broadcast. */
391 unsigned int bytes;
392 } broadcast;
393
394 /* Compressed disp8*N attribute. */
395 unsigned int memshift;
396
397 /* Prefer load or store in encoding. */
398 enum
399 {
400 dir_encoding_default = 0,
401 dir_encoding_load,
402 dir_encoding_store,
403 dir_encoding_swap
404 } dir_encoding;
405
406 /* Prefer 8bit, 16bit, 32bit displacement in encoding. */
407 enum
408 {
409 disp_encoding_default = 0,
410 disp_encoding_8bit,
411 disp_encoding_16bit,
412 disp_encoding_32bit
413 } disp_encoding;
414
415 /* Prefer the REX byte in encoding. */
416 bool rex_encoding;
417
418 /* Disable instruction size optimization. */
419 bool no_optimize;
420
421 /* How to encode vector instructions. */
422 enum
423 {
424 vex_encoding_default = 0,
425 vex_encoding_vex,
426 vex_encoding_vex3,
427 vex_encoding_evex,
428 vex_encoding_error
429 } vec_encoding;
430
431 /* REP prefix. */
432 const char *rep_prefix;
433
434 /* HLE prefix. */
435 const char *hle_prefix;
436
437 /* Have BND prefix. */
438 const char *bnd_prefix;
439
440 /* Have NOTRACK prefix. */
441 const char *notrack_prefix;
442
443 /* Error message. */
444 enum i386_error error;
445 };
446
447 typedef struct _i386_insn i386_insn;
448
449 /* Link RC type with corresponding string, that'll be looked for in
450 asm. */
451 struct RC_name
452 {
453 enum rc_type type;
454 const char *name;
455 unsigned int len;
456 };
457
458 static const struct RC_name RC_NamesTable[] =
459 {
460 { rne, STRING_COMMA_LEN ("rn-sae") },
461 { rd, STRING_COMMA_LEN ("rd-sae") },
462 { ru, STRING_COMMA_LEN ("ru-sae") },
463 { rz, STRING_COMMA_LEN ("rz-sae") },
464 { saeonly, STRING_COMMA_LEN ("sae") },
465 };
466
467 /* List of chars besides those in app.c:symbol_chars that can start an
468 operand. Used to prevent the scrubber eating vital white-space. */
469 const char extra_symbol_chars[] = "*%-([{}"
470 #ifdef LEX_AT
471 "@"
472 #endif
473 #ifdef LEX_QM
474 "?"
475 #endif
476 ;
477
478 #if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
479 && !defined (TE_GNU) \
480 && !defined (TE_LINUX) \
481 && !defined (TE_Haiku) \
482 && !defined (TE_FreeBSD) \
483 && !defined (TE_DragonFly) \
484 && !defined (TE_NetBSD))
485 /* This array holds the chars that always start a comment. If the
486 pre-processor is disabled, these aren't very useful. The option
487 --divide will remove '/' from this list. */
488 const char *i386_comment_chars = "#/";
489 #define SVR4_COMMENT_CHARS 1
490 #define PREFIX_SEPARATOR '\\'
491
492 #else
493 const char *i386_comment_chars = "#";
494 #define PREFIX_SEPARATOR '/'
495 #endif
496
497 /* This array holds the chars that only start a comment at the beginning of
498 a line. If the line seems to have the form '# 123 filename'
499 .line and .file directives will appear in the pre-processed output.
500 Note that input_file.c hand checks for '#' at the beginning of the
501 first line of the input file. This is because the compiler outputs
502 #NO_APP at the beginning of its output.
503 Also note that comments started like this one will always work if
504 '/' isn't otherwise defined. */
505 const char line_comment_chars[] = "#/";
506
507 const char line_separator_chars[] = ";";
508
509 /* Chars that can be used to separate mant from exp in floating point
510 nums. */
511 const char EXP_CHARS[] = "eE";
512
513 /* Chars that mean this number is a floating point constant
514 As in 0f12.456
515 or 0d1.2345e12. */
516 const char FLT_CHARS[] = "fFdDxXhHbB";
517
518 /* Tables for lexical analysis. */
519 static char mnemonic_chars[256];
520 static char register_chars[256];
521 static char operand_chars[256];
522 static char identifier_chars[256];
523
524 /* Lexical macros. */
525 #define is_mnemonic_char(x) (mnemonic_chars[(unsigned char) x])
526 #define is_operand_char(x) (operand_chars[(unsigned char) x])
527 #define is_register_char(x) (register_chars[(unsigned char) x])
528 #define is_space_char(x) ((x) == ' ')
529 #define is_identifier_char(x) (identifier_chars[(unsigned char) x])
530
531 /* All non-digit non-letter characters that may occur in an operand. */
532 static char operand_special_chars[] = "%$-+(,)*._~/<>|&^!:[@]";
533
534 /* md_assemble() always leaves the strings it's passed unaltered. To
535 effect this we maintain a stack of saved characters that we've smashed
536 with '\0's (indicating end of strings for various sub-fields of the
537 assembler instruction). */
538 static char save_stack[32];
539 static char *save_stack_p;
540 #define END_STRING_AND_SAVE(s) \
541 do { *save_stack_p++ = *(s); *(s) = '\0'; } while (0)
542 #define RESTORE_END_STRING(s) \
543 do { *(s) = *--save_stack_p; } while (0)
544
545 /* The instruction we're assembling. */
546 static i386_insn i;
547
548 /* Possible templates for current insn. */
549 static const templates *current_templates;
550
551 /* Per instruction expressionS buffers: max displacements & immediates. */
552 static expressionS disp_expressions[MAX_MEMORY_OPERANDS];
553 static expressionS im_expressions[MAX_IMMEDIATE_OPERANDS];
554
555 /* Current operand we are working on. */
556 static int this_operand = -1;
557
558 /* We support four different modes. FLAG_CODE variable is used to distinguish
559 these. */
560
561 enum flag_code {
562 CODE_32BIT,
563 CODE_16BIT,
564 CODE_64BIT };
565
566 static enum flag_code flag_code;
567 static unsigned int object_64bit;
568 static unsigned int disallow_64bit_reloc;
569 static int use_rela_relocations = 0;
570 /* __tls_get_addr/___tls_get_addr symbol for TLS. */
571 static const char *tls_get_addr;
572
573 #if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
574 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
575 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
576
577 /* The ELF ABI to use. */
578 enum x86_elf_abi
579 {
580 I386_ABI,
581 X86_64_ABI,
582 X86_64_X32_ABI
583 };
584
585 static enum x86_elf_abi x86_elf_abi = I386_ABI;
586 #endif
587
588 #if defined (TE_PE) || defined (TE_PEP)
589 /* Use big object file format. */
590 static int use_big_obj = 0;
591 #endif
592
593 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
594 /* 1 if generating code for a shared library. */
595 static int shared = 0;
596 #endif
597
598 /* 1 for intel syntax,
599 0 if att syntax. */
600 static int intel_syntax = 0;
601
602 static enum x86_64_isa
603 {
604 amd64 = 1, /* AMD64 ISA. */
605 intel64 /* Intel64 ISA. */
606 } isa64;
607
608 /* 1 for intel mnemonic,
609 0 if att mnemonic. */
610 static int intel_mnemonic = !SYSV386_COMPAT;
611
612 /* 1 if pseudo registers are permitted. */
613 static int allow_pseudo_reg = 0;
614
615 /* 1 if register prefix % not required. */
616 static int allow_naked_reg = 0;
617
618 /* 1 if the assembler should add BND prefix for all control-transferring
619 instructions supporting it, even if this prefix wasn't specified
620 explicitly. */
621 static int add_bnd_prefix = 0;
622
623 /* 1 if pseudo index register, eiz/riz, is allowed . */
624 static int allow_index_reg = 0;
625
626 /* 1 if the assembler should ignore LOCK prefix, even if it was
627 specified explicitly. */
628 static int omit_lock_prefix = 0;
629
630 /* 1 if the assembler should encode lfence, mfence, and sfence as
631 "lock addl $0, (%{re}sp)". */
632 static int avoid_fence = 0;
633
634 /* 1 if lfence should be inserted after every load. */
635 static int lfence_after_load = 0;
636
637 /* Non-zero if lfence should be inserted before indirect branch. */
638 static enum lfence_before_indirect_branch_kind
639 {
640 lfence_branch_none = 0,
641 lfence_branch_register,
642 lfence_branch_memory,
643 lfence_branch_all
644 }
645 lfence_before_indirect_branch;
646
647 /* Non-zero if lfence should be inserted before ret. */
648 static enum lfence_before_ret_kind
649 {
650 lfence_before_ret_none = 0,
651 lfence_before_ret_not,
652 lfence_before_ret_or,
653 lfence_before_ret_shl
654 }
655 lfence_before_ret;
656
657 /* Types of previous instruction is .byte or prefix. */
658 static struct
659 {
660 segT seg;
661 const char *file;
662 const char *name;
663 unsigned int line;
664 enum last_insn_kind
665 {
666 last_insn_other = 0,
667 last_insn_directive,
668 last_insn_prefix
669 } kind;
670 } last_insn;
671
672 /* 1 if the assembler should generate relax relocations. */
673
674 static int generate_relax_relocations
675 = DEFAULT_GENERATE_X86_RELAX_RELOCATIONS;
676
677 static enum check_kind
678 {
679 check_none = 0,
680 check_warning,
681 check_error
682 }
683 sse_check, operand_check = check_warning;
684
685 /* Non-zero if branches should be aligned within power of 2 boundary. */
686 static int align_branch_power = 0;
687
688 /* Types of branches to align. */
689 enum align_branch_kind
690 {
691 align_branch_none = 0,
692 align_branch_jcc = 1,
693 align_branch_fused = 2,
694 align_branch_jmp = 3,
695 align_branch_call = 4,
696 align_branch_indirect = 5,
697 align_branch_ret = 6
698 };
699
700 /* Type bits of branches to align. */
701 enum align_branch_bit
702 {
703 align_branch_jcc_bit = 1 << align_branch_jcc,
704 align_branch_fused_bit = 1 << align_branch_fused,
705 align_branch_jmp_bit = 1 << align_branch_jmp,
706 align_branch_call_bit = 1 << align_branch_call,
707 align_branch_indirect_bit = 1 << align_branch_indirect,
708 align_branch_ret_bit = 1 << align_branch_ret
709 };
710
711 static unsigned int align_branch = (align_branch_jcc_bit
712 | align_branch_fused_bit
713 | align_branch_jmp_bit);
714
715 /* Types of condition jump used by macro-fusion. */
716 enum mf_jcc_kind
717 {
718 mf_jcc_jo = 0, /* base opcode 0x70 */
719 mf_jcc_jc, /* base opcode 0x72 */
720 mf_jcc_je, /* base opcode 0x74 */
721 mf_jcc_jna, /* base opcode 0x76 */
722 mf_jcc_js, /* base opcode 0x78 */
723 mf_jcc_jp, /* base opcode 0x7a */
724 mf_jcc_jl, /* base opcode 0x7c */
725 mf_jcc_jle, /* base opcode 0x7e */
726 };
727
728 /* Types of compare flag-modifying insntructions used by macro-fusion. */
729 enum mf_cmp_kind
730 {
731 mf_cmp_test_and, /* test/cmp */
732 mf_cmp_alu_cmp, /* add/sub/cmp */
733 mf_cmp_incdec /* inc/dec */
734 };
735
736 /* The maximum padding size for fused jcc. CMP like instruction can
737 be 9 bytes and jcc can be 6 bytes. Leave room just in case for
738 prefixes. */
739 #define MAX_FUSED_JCC_PADDING_SIZE 20
740
741 /* The maximum number of prefixes added for an instruction. */
742 static unsigned int align_branch_prefix_size = 5;
743
744 /* Optimization:
745 1. Clear the REX_W bit with register operand if possible.
746 2. Above plus use 128bit vector instruction to clear the full vector
747 register.
748 */
749 static int optimize = 0;
750
751 /* Optimization:
752 1. Clear the REX_W bit with register operand if possible.
753 2. Above plus use 128bit vector instruction to clear the full vector
754 register.
755 3. Above plus optimize "test{q,l,w} $imm8,%r{64,32,16}" to
756 "testb $imm7,%r8".
757 */
758 static int optimize_for_space = 0;
759
760 /* Register prefix used for error message. */
761 static const char *register_prefix = "%";
762
763 /* Used in 16 bit gcc mode to add an l suffix to call, ret, enter,
764 leave, push, and pop instructions so that gcc has the same stack
765 frame as in 32 bit mode. */
766 static char stackop_size = '\0';
767
768 /* Non-zero to optimize code alignment. */
769 int optimize_align_code = 1;
770
771 /* Non-zero to quieten some warnings. */
772 static int quiet_warnings = 0;
773
774 /* CPU name. */
775 static const char *cpu_arch_name = NULL;
776 static char *cpu_sub_arch_name = NULL;
777
778 /* CPU feature flags. */
779 static i386_cpu_flags cpu_arch_flags = CPU_UNKNOWN_FLAGS;
780
781 /* If we have selected a cpu we are generating instructions for. */
782 static int cpu_arch_tune_set = 0;
783
784 /* Cpu we are generating instructions for. */
785 enum processor_type cpu_arch_tune = PROCESSOR_UNKNOWN;
786
787 /* CPU feature flags of cpu we are generating instructions for. */
788 static i386_cpu_flags cpu_arch_tune_flags;
789
790 /* CPU instruction set architecture used. */
791 enum processor_type cpu_arch_isa = PROCESSOR_UNKNOWN;
792
793 /* CPU feature flags of instruction set architecture used. */
794 i386_cpu_flags cpu_arch_isa_flags;
795
796 /* If set, conditional jumps are not automatically promoted to handle
797 larger than a byte offset. */
798 static unsigned int no_cond_jump_promotion = 0;
799
800 /* Encode SSE instructions with VEX prefix. */
801 static unsigned int sse2avx;
802
803 /* Encode aligned vector move as unaligned vector move. */
804 static unsigned int use_unaligned_vector_move;
805
806 /* Encode scalar AVX instructions with specific vector length. */
807 static enum
808 {
809 vex128 = 0,
810 vex256
811 } avxscalar;
812
813 /* Encode VEX WIG instructions with specific vex.w. */
814 static enum
815 {
816 vexw0 = 0,
817 vexw1
818 } vexwig;
819
820 /* Encode scalar EVEX LIG instructions with specific vector length. */
821 static enum
822 {
823 evexl128 = 0,
824 evexl256,
825 evexl512
826 } evexlig;
827
828 /* Encode EVEX WIG instructions with specific evex.w. */
829 static enum
830 {
831 evexw0 = 0,
832 evexw1
833 } evexwig;
834
835 /* Value to encode in EVEX RC bits, for SAE-only instructions. */
836 static enum rc_type evexrcig = rne;
837
838 /* Pre-defined "_GLOBAL_OFFSET_TABLE_". */
839 static symbolS *GOT_symbol;
840
841 /* The dwarf2 return column, adjusted for 32 or 64 bit. */
842 unsigned int x86_dwarf2_return_column;
843
844 /* The dwarf2 data alignment, adjusted for 32 or 64 bit. */
845 int x86_cie_data_alignment;
846
847 /* Interface to relax_segment.
848 There are 3 major relax states for 386 jump insns because the
849 different types of jumps add different sizes to frags when we're
850 figuring out what sort of jump to choose to reach a given label.
851
852 BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING are used to align
853 branches which are handled by md_estimate_size_before_relax() and
854 i386_generic_table_relax_frag(). */
855
856 /* Types. */
857 #define UNCOND_JUMP 0
858 #define COND_JUMP 1
859 #define COND_JUMP86 2
860 #define BRANCH_PADDING 3
861 #define BRANCH_PREFIX 4
862 #define FUSED_JCC_PADDING 5
863
864 /* Sizes. */
865 #define CODE16 1
866 #define SMALL 0
867 #define SMALL16 (SMALL | CODE16)
868 #define BIG 2
869 #define BIG16 (BIG | CODE16)
870
871 #ifndef INLINE
872 #ifdef __GNUC__
873 #define INLINE __inline__
874 #else
875 #define INLINE
876 #endif
877 #endif
878
879 #define ENCODE_RELAX_STATE(type, size) \
880 ((relax_substateT) (((type) << 2) | (size)))
881 #define TYPE_FROM_RELAX_STATE(s) \
882 ((s) >> 2)
883 #define DISP_SIZE_FROM_RELAX_STATE(s) \
884 ((((s) & 3) == BIG ? 4 : (((s) & 3) == BIG16 ? 2 : 1)))
885
886 /* This table is used by relax_frag to promote short jumps to long
887 ones where necessary. SMALL (short) jumps may be promoted to BIG
888 (32 bit long) ones, and SMALL16 jumps to BIG16 (16 bit long). We
889 don't allow a short jump in a 32 bit code segment to be promoted to
890 a 16 bit offset jump because it's slower (requires data size
891 prefix), and doesn't work, unless the destination is in the bottom
892 64k of the code segment (The top 16 bits of eip are zeroed). */
893
894 const relax_typeS md_relax_table[] =
895 {
896 /* The fields are:
897 1) most positive reach of this state,
898 2) most negative reach of this state,
899 3) how many bytes this mode will have in the variable part of the frag
900 4) which index into the table to try if we can't fit into this one. */
901
902 /* UNCOND_JUMP states. */
903 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG)},
904 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16)},
905 /* dword jmp adds 4 bytes to frag:
906 0 extra opcode bytes, 4 displacement bytes. */
907 {0, 0, 4, 0},
908 /* word jmp adds 2 byte2 to frag:
909 0 extra opcode bytes, 2 displacement bytes. */
910 {0, 0, 2, 0},
911
912 /* COND_JUMP states. */
913 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG)},
914 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG16)},
915 /* dword conditionals adds 5 bytes to frag:
916 1 extra opcode byte, 4 displacement bytes. */
917 {0, 0, 5, 0},
918 /* word conditionals add 3 bytes to frag:
919 1 extra opcode byte, 2 displacement bytes. */
920 {0, 0, 3, 0},
921
922 /* COND_JUMP86 states. */
923 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG)},
924 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG16)},
925 /* dword conditionals adds 5 bytes to frag:
926 1 extra opcode byte, 4 displacement bytes. */
927 {0, 0, 5, 0},
928 /* word conditionals add 4 bytes to frag:
929 1 displacement byte and a 3 byte long branch insn. */
930 {0, 0, 4, 0}
931 };
932
933 static const arch_entry cpu_arch[] =
934 {
935 /* Do not replace the first two entries - i386_target_format()
936 relies on them being there in this order. */
937 { STRING_COMMA_LEN ("generic32"), PROCESSOR_GENERIC32,
938 CPU_GENERIC32_FLAGS, 0 },
939 { STRING_COMMA_LEN ("generic64"), PROCESSOR_GENERIC64,
940 CPU_GENERIC64_FLAGS, 0 },
941 { STRING_COMMA_LEN ("i8086"), PROCESSOR_UNKNOWN,
942 CPU_NONE_FLAGS, 0 },
943 { STRING_COMMA_LEN ("i186"), PROCESSOR_UNKNOWN,
944 CPU_I186_FLAGS, 0 },
945 { STRING_COMMA_LEN ("i286"), PROCESSOR_UNKNOWN,
946 CPU_I286_FLAGS, 0 },
947 { STRING_COMMA_LEN ("i386"), PROCESSOR_I386,
948 CPU_I386_FLAGS, 0 },
949 { STRING_COMMA_LEN ("i486"), PROCESSOR_I486,
950 CPU_I486_FLAGS, 0 },
951 { STRING_COMMA_LEN ("i586"), PROCESSOR_PENTIUM,
952 CPU_I586_FLAGS, 0 },
953 { STRING_COMMA_LEN ("i686"), PROCESSOR_PENTIUMPRO,
954 CPU_I686_FLAGS, 0 },
955 { STRING_COMMA_LEN ("pentium"), PROCESSOR_PENTIUM,
956 CPU_I586_FLAGS, 0 },
957 { STRING_COMMA_LEN ("pentiumpro"), PROCESSOR_PENTIUMPRO,
958 CPU_PENTIUMPRO_FLAGS, 0 },
959 { STRING_COMMA_LEN ("pentiumii"), PROCESSOR_PENTIUMPRO,
960 CPU_P2_FLAGS, 0 },
961 { STRING_COMMA_LEN ("pentiumiii"),PROCESSOR_PENTIUMPRO,
962 CPU_P3_FLAGS, 0 },
963 { STRING_COMMA_LEN ("pentium4"), PROCESSOR_PENTIUM4,
964 CPU_P4_FLAGS, 0 },
965 { STRING_COMMA_LEN ("prescott"), PROCESSOR_NOCONA,
966 CPU_CORE_FLAGS, 0 },
967 { STRING_COMMA_LEN ("nocona"), PROCESSOR_NOCONA,
968 CPU_NOCONA_FLAGS, 0 },
969 { STRING_COMMA_LEN ("yonah"), PROCESSOR_CORE,
970 CPU_CORE_FLAGS, 1 },
971 { STRING_COMMA_LEN ("core"), PROCESSOR_CORE,
972 CPU_CORE_FLAGS, 0 },
973 { STRING_COMMA_LEN ("merom"), PROCESSOR_CORE2,
974 CPU_CORE2_FLAGS, 1 },
975 { STRING_COMMA_LEN ("core2"), PROCESSOR_CORE2,
976 CPU_CORE2_FLAGS, 0 },
977 { STRING_COMMA_LEN ("corei7"), PROCESSOR_COREI7,
978 CPU_COREI7_FLAGS, 0 },
979 { STRING_COMMA_LEN ("iamcu"), PROCESSOR_IAMCU,
980 CPU_IAMCU_FLAGS, 0 },
981 { STRING_COMMA_LEN ("k6"), PROCESSOR_K6,
982 CPU_K6_FLAGS, 0 },
983 { STRING_COMMA_LEN ("k6_2"), PROCESSOR_K6,
984 CPU_K6_2_FLAGS, 0 },
985 { STRING_COMMA_LEN ("athlon"), PROCESSOR_ATHLON,
986 CPU_ATHLON_FLAGS, 0 },
987 { STRING_COMMA_LEN ("sledgehammer"), PROCESSOR_K8,
988 CPU_K8_FLAGS, 1 },
989 { STRING_COMMA_LEN ("opteron"), PROCESSOR_K8,
990 CPU_K8_FLAGS, 0 },
991 { STRING_COMMA_LEN ("k8"), PROCESSOR_K8,
992 CPU_K8_FLAGS, 0 },
993 { STRING_COMMA_LEN ("amdfam10"), PROCESSOR_AMDFAM10,
994 CPU_AMDFAM10_FLAGS, 0 },
995 { STRING_COMMA_LEN ("bdver1"), PROCESSOR_BD,
996 CPU_BDVER1_FLAGS, 0 },
997 { STRING_COMMA_LEN ("bdver2"), PROCESSOR_BD,
998 CPU_BDVER2_FLAGS, 0 },
999 { STRING_COMMA_LEN ("bdver3"), PROCESSOR_BD,
1000 CPU_BDVER3_FLAGS, 0 },
1001 { STRING_COMMA_LEN ("bdver4"), PROCESSOR_BD,
1002 CPU_BDVER4_FLAGS, 0 },
1003 { STRING_COMMA_LEN ("znver1"), PROCESSOR_ZNVER,
1004 CPU_ZNVER1_FLAGS, 0 },
1005 { STRING_COMMA_LEN ("znver2"), PROCESSOR_ZNVER,
1006 CPU_ZNVER2_FLAGS, 0 },
1007 { STRING_COMMA_LEN ("znver3"), PROCESSOR_ZNVER,
1008 CPU_ZNVER3_FLAGS, 0 },
1009 { STRING_COMMA_LEN ("btver1"), PROCESSOR_BT,
1010 CPU_BTVER1_FLAGS, 0 },
1011 { STRING_COMMA_LEN ("btver2"), PROCESSOR_BT,
1012 CPU_BTVER2_FLAGS, 0 },
1013 { STRING_COMMA_LEN (".8087"), PROCESSOR_UNKNOWN,
1014 CPU_8087_FLAGS, 0 },
1015 { STRING_COMMA_LEN (".287"), PROCESSOR_UNKNOWN,
1016 CPU_287_FLAGS, 0 },
1017 { STRING_COMMA_LEN (".387"), PROCESSOR_UNKNOWN,
1018 CPU_387_FLAGS, 0 },
1019 { STRING_COMMA_LEN (".687"), PROCESSOR_UNKNOWN,
1020 CPU_687_FLAGS, 0 },
1021 { STRING_COMMA_LEN (".cmov"), PROCESSOR_UNKNOWN,
1022 CPU_CMOV_FLAGS, 0 },
1023 { STRING_COMMA_LEN (".fxsr"), PROCESSOR_UNKNOWN,
1024 CPU_FXSR_FLAGS, 0 },
1025 { STRING_COMMA_LEN (".mmx"), PROCESSOR_UNKNOWN,
1026 CPU_MMX_FLAGS, 0 },
1027 { STRING_COMMA_LEN (".sse"), PROCESSOR_UNKNOWN,
1028 CPU_SSE_FLAGS, 0 },
1029 { STRING_COMMA_LEN (".sse2"), PROCESSOR_UNKNOWN,
1030 CPU_SSE2_FLAGS, 0 },
1031 { STRING_COMMA_LEN (".sse3"), PROCESSOR_UNKNOWN,
1032 CPU_SSE3_FLAGS, 0 },
1033 { STRING_COMMA_LEN (".sse4a"), PROCESSOR_UNKNOWN,
1034 CPU_SSE4A_FLAGS, 0 },
1035 { STRING_COMMA_LEN (".ssse3"), PROCESSOR_UNKNOWN,
1036 CPU_SSSE3_FLAGS, 0 },
1037 { STRING_COMMA_LEN (".sse4.1"), PROCESSOR_UNKNOWN,
1038 CPU_SSE4_1_FLAGS, 0 },
1039 { STRING_COMMA_LEN (".sse4.2"), PROCESSOR_UNKNOWN,
1040 CPU_SSE4_2_FLAGS, 0 },
1041 { STRING_COMMA_LEN (".sse4"), PROCESSOR_UNKNOWN,
1042 CPU_SSE4_2_FLAGS, 0 },
1043 { STRING_COMMA_LEN (".avx"), PROCESSOR_UNKNOWN,
1044 CPU_AVX_FLAGS, 0 },
1045 { STRING_COMMA_LEN (".avx2"), PROCESSOR_UNKNOWN,
1046 CPU_AVX2_FLAGS, 0 },
1047 { STRING_COMMA_LEN (".avx512f"), PROCESSOR_UNKNOWN,
1048 CPU_AVX512F_FLAGS, 0 },
1049 { STRING_COMMA_LEN (".avx512cd"), PROCESSOR_UNKNOWN,
1050 CPU_AVX512CD_FLAGS, 0 },
1051 { STRING_COMMA_LEN (".avx512er"), PROCESSOR_UNKNOWN,
1052 CPU_AVX512ER_FLAGS, 0 },
1053 { STRING_COMMA_LEN (".avx512pf"), PROCESSOR_UNKNOWN,
1054 CPU_AVX512PF_FLAGS, 0 },
1055 { STRING_COMMA_LEN (".avx512dq"), PROCESSOR_UNKNOWN,
1056 CPU_AVX512DQ_FLAGS, 0 },
1057 { STRING_COMMA_LEN (".avx512bw"), PROCESSOR_UNKNOWN,
1058 CPU_AVX512BW_FLAGS, 0 },
1059 { STRING_COMMA_LEN (".avx512vl"), PROCESSOR_UNKNOWN,
1060 CPU_AVX512VL_FLAGS, 0 },
1061 { STRING_COMMA_LEN (".vmx"), PROCESSOR_UNKNOWN,
1062 CPU_VMX_FLAGS, 0 },
1063 { STRING_COMMA_LEN (".vmfunc"), PROCESSOR_UNKNOWN,
1064 CPU_VMFUNC_FLAGS, 0 },
1065 { STRING_COMMA_LEN (".smx"), PROCESSOR_UNKNOWN,
1066 CPU_SMX_FLAGS, 0 },
1067 { STRING_COMMA_LEN (".xsave"), PROCESSOR_UNKNOWN,
1068 CPU_XSAVE_FLAGS, 0 },
1069 { STRING_COMMA_LEN (".xsaveopt"), PROCESSOR_UNKNOWN,
1070 CPU_XSAVEOPT_FLAGS, 0 },
1071 { STRING_COMMA_LEN (".xsavec"), PROCESSOR_UNKNOWN,
1072 CPU_XSAVEC_FLAGS, 0 },
1073 { STRING_COMMA_LEN (".xsaves"), PROCESSOR_UNKNOWN,
1074 CPU_XSAVES_FLAGS, 0 },
1075 { STRING_COMMA_LEN (".aes"), PROCESSOR_UNKNOWN,
1076 CPU_AES_FLAGS, 0 },
1077 { STRING_COMMA_LEN (".pclmul"), PROCESSOR_UNKNOWN,
1078 CPU_PCLMUL_FLAGS, 0 },
1079 { STRING_COMMA_LEN (".clmul"), PROCESSOR_UNKNOWN,
1080 CPU_PCLMUL_FLAGS, 1 },
1081 { STRING_COMMA_LEN (".fsgsbase"), PROCESSOR_UNKNOWN,
1082 CPU_FSGSBASE_FLAGS, 0 },
1083 { STRING_COMMA_LEN (".rdrnd"), PROCESSOR_UNKNOWN,
1084 CPU_RDRND_FLAGS, 0 },
1085 { STRING_COMMA_LEN (".f16c"), PROCESSOR_UNKNOWN,
1086 CPU_F16C_FLAGS, 0 },
1087 { STRING_COMMA_LEN (".bmi2"), PROCESSOR_UNKNOWN,
1088 CPU_BMI2_FLAGS, 0 },
1089 { STRING_COMMA_LEN (".fma"), PROCESSOR_UNKNOWN,
1090 CPU_FMA_FLAGS, 0 },
1091 { STRING_COMMA_LEN (".fma4"), PROCESSOR_UNKNOWN,
1092 CPU_FMA4_FLAGS, 0 },
1093 { STRING_COMMA_LEN (".xop"), PROCESSOR_UNKNOWN,
1094 CPU_XOP_FLAGS, 0 },
1095 { STRING_COMMA_LEN (".lwp"), PROCESSOR_UNKNOWN,
1096 CPU_LWP_FLAGS, 0 },
1097 { STRING_COMMA_LEN (".movbe"), PROCESSOR_UNKNOWN,
1098 CPU_MOVBE_FLAGS, 0 },
1099 { STRING_COMMA_LEN (".cx16"), PROCESSOR_UNKNOWN,
1100 CPU_CX16_FLAGS, 0 },
1101 { STRING_COMMA_LEN (".ept"), PROCESSOR_UNKNOWN,
1102 CPU_EPT_FLAGS, 0 },
1103 { STRING_COMMA_LEN (".lzcnt"), PROCESSOR_UNKNOWN,
1104 CPU_LZCNT_FLAGS, 0 },
1105 { STRING_COMMA_LEN (".popcnt"), PROCESSOR_UNKNOWN,
1106 CPU_POPCNT_FLAGS, 0 },
1107 { STRING_COMMA_LEN (".hle"), PROCESSOR_UNKNOWN,
1108 CPU_HLE_FLAGS, 0 },
1109 { STRING_COMMA_LEN (".rtm"), PROCESSOR_UNKNOWN,
1110 CPU_RTM_FLAGS, 0 },
1111 { STRING_COMMA_LEN (".invpcid"), PROCESSOR_UNKNOWN,
1112 CPU_INVPCID_FLAGS, 0 },
1113 { STRING_COMMA_LEN (".clflush"), PROCESSOR_UNKNOWN,
1114 CPU_CLFLUSH_FLAGS, 0 },
1115 { STRING_COMMA_LEN (".nop"), PROCESSOR_UNKNOWN,
1116 CPU_NOP_FLAGS, 0 },
1117 { STRING_COMMA_LEN (".syscall"), PROCESSOR_UNKNOWN,
1118 CPU_SYSCALL_FLAGS, 0 },
1119 { STRING_COMMA_LEN (".rdtscp"), PROCESSOR_UNKNOWN,
1120 CPU_RDTSCP_FLAGS, 0 },
1121 { STRING_COMMA_LEN (".3dnow"), PROCESSOR_UNKNOWN,
1122 CPU_3DNOW_FLAGS, 0 },
1123 { STRING_COMMA_LEN (".3dnowa"), PROCESSOR_UNKNOWN,
1124 CPU_3DNOWA_FLAGS, 0 },
1125 { STRING_COMMA_LEN (".padlock"), PROCESSOR_UNKNOWN,
1126 CPU_PADLOCK_FLAGS, 0 },
1127 { STRING_COMMA_LEN (".pacifica"), PROCESSOR_UNKNOWN,
1128 CPU_SVME_FLAGS, 1 },
1129 { STRING_COMMA_LEN (".svme"), PROCESSOR_UNKNOWN,
1130 CPU_SVME_FLAGS, 0 },
1131 { STRING_COMMA_LEN (".sse4a"), PROCESSOR_UNKNOWN,
1132 CPU_SSE4A_FLAGS, 0 },
1133 { STRING_COMMA_LEN (".abm"), PROCESSOR_UNKNOWN,
1134 CPU_ABM_FLAGS, 0 },
1135 { STRING_COMMA_LEN (".bmi"), PROCESSOR_UNKNOWN,
1136 CPU_BMI_FLAGS, 0 },
1137 { STRING_COMMA_LEN (".tbm"), PROCESSOR_UNKNOWN,
1138 CPU_TBM_FLAGS, 0 },
1139 { STRING_COMMA_LEN (".adx"), PROCESSOR_UNKNOWN,
1140 CPU_ADX_FLAGS, 0 },
1141 { STRING_COMMA_LEN (".rdseed"), PROCESSOR_UNKNOWN,
1142 CPU_RDSEED_FLAGS, 0 },
1143 { STRING_COMMA_LEN (".prfchw"), PROCESSOR_UNKNOWN,
1144 CPU_PRFCHW_FLAGS, 0 },
1145 { STRING_COMMA_LEN (".smap"), PROCESSOR_UNKNOWN,
1146 CPU_SMAP_FLAGS, 0 },
1147 { STRING_COMMA_LEN (".mpx"), PROCESSOR_UNKNOWN,
1148 CPU_MPX_FLAGS, 0 },
1149 { STRING_COMMA_LEN (".sha"), PROCESSOR_UNKNOWN,
1150 CPU_SHA_FLAGS, 0 },
1151 { STRING_COMMA_LEN (".clflushopt"), PROCESSOR_UNKNOWN,
1152 CPU_CLFLUSHOPT_FLAGS, 0 },
1153 { STRING_COMMA_LEN (".prefetchwt1"), PROCESSOR_UNKNOWN,
1154 CPU_PREFETCHWT1_FLAGS, 0 },
1155 { STRING_COMMA_LEN (".se1"), PROCESSOR_UNKNOWN,
1156 CPU_SE1_FLAGS, 0 },
1157 { STRING_COMMA_LEN (".clwb"), PROCESSOR_UNKNOWN,
1158 CPU_CLWB_FLAGS, 0 },
1159 { STRING_COMMA_LEN (".avx512ifma"), PROCESSOR_UNKNOWN,
1160 CPU_AVX512IFMA_FLAGS, 0 },
1161 { STRING_COMMA_LEN (".avx512vbmi"), PROCESSOR_UNKNOWN,
1162 CPU_AVX512VBMI_FLAGS, 0 },
1163 { STRING_COMMA_LEN (".avx512_4fmaps"), PROCESSOR_UNKNOWN,
1164 CPU_AVX512_4FMAPS_FLAGS, 0 },
1165 { STRING_COMMA_LEN (".avx512_4vnniw"), PROCESSOR_UNKNOWN,
1166 CPU_AVX512_4VNNIW_FLAGS, 0 },
1167 { STRING_COMMA_LEN (".avx512_vpopcntdq"), PROCESSOR_UNKNOWN,
1168 CPU_AVX512_VPOPCNTDQ_FLAGS, 0 },
1169 { STRING_COMMA_LEN (".avx512_vbmi2"), PROCESSOR_UNKNOWN,
1170 CPU_AVX512_VBMI2_FLAGS, 0 },
1171 { STRING_COMMA_LEN (".avx512_vnni"), PROCESSOR_UNKNOWN,
1172 CPU_AVX512_VNNI_FLAGS, 0 },
1173 { STRING_COMMA_LEN (".avx512_bitalg"), PROCESSOR_UNKNOWN,
1174 CPU_AVX512_BITALG_FLAGS, 0 },
1175 { STRING_COMMA_LEN (".avx_vnni"), PROCESSOR_UNKNOWN,
1176 CPU_AVX_VNNI_FLAGS, 0 },
1177 { STRING_COMMA_LEN (".clzero"), PROCESSOR_UNKNOWN,
1178 CPU_CLZERO_FLAGS, 0 },
1179 { STRING_COMMA_LEN (".mwaitx"), PROCESSOR_UNKNOWN,
1180 CPU_MWAITX_FLAGS, 0 },
1181 { STRING_COMMA_LEN (".ospke"), PROCESSOR_UNKNOWN,
1182 CPU_OSPKE_FLAGS, 0 },
1183 { STRING_COMMA_LEN (".rdpid"), PROCESSOR_UNKNOWN,
1184 CPU_RDPID_FLAGS, 0 },
1185 { STRING_COMMA_LEN (".ptwrite"), PROCESSOR_UNKNOWN,
1186 CPU_PTWRITE_FLAGS, 0 },
1187 { STRING_COMMA_LEN (".ibt"), PROCESSOR_UNKNOWN,
1188 CPU_IBT_FLAGS, 0 },
1189 { STRING_COMMA_LEN (".shstk"), PROCESSOR_UNKNOWN,
1190 CPU_SHSTK_FLAGS, 0 },
1191 { STRING_COMMA_LEN (".gfni"), PROCESSOR_UNKNOWN,
1192 CPU_GFNI_FLAGS, 0 },
1193 { STRING_COMMA_LEN (".vaes"), PROCESSOR_UNKNOWN,
1194 CPU_VAES_FLAGS, 0 },
1195 { STRING_COMMA_LEN (".vpclmulqdq"), PROCESSOR_UNKNOWN,
1196 CPU_VPCLMULQDQ_FLAGS, 0 },
1197 { STRING_COMMA_LEN (".wbnoinvd"), PROCESSOR_UNKNOWN,
1198 CPU_WBNOINVD_FLAGS, 0 },
1199 { STRING_COMMA_LEN (".pconfig"), PROCESSOR_UNKNOWN,
1200 CPU_PCONFIG_FLAGS, 0 },
1201 { STRING_COMMA_LEN (".waitpkg"), PROCESSOR_UNKNOWN,
1202 CPU_WAITPKG_FLAGS, 0 },
1203 { STRING_COMMA_LEN (".cldemote"), PROCESSOR_UNKNOWN,
1204 CPU_CLDEMOTE_FLAGS, 0 },
1205 { STRING_COMMA_LEN (".amx_int8"), PROCESSOR_UNKNOWN,
1206 CPU_AMX_INT8_FLAGS, 0 },
1207 { STRING_COMMA_LEN (".amx_bf16"), PROCESSOR_UNKNOWN,
1208 CPU_AMX_BF16_FLAGS, 0 },
1209 { STRING_COMMA_LEN (".amx_tile"), PROCESSOR_UNKNOWN,
1210 CPU_AMX_TILE_FLAGS, 0 },
1211 { STRING_COMMA_LEN (".movdiri"), PROCESSOR_UNKNOWN,
1212 CPU_MOVDIRI_FLAGS, 0 },
1213 { STRING_COMMA_LEN (".movdir64b"), PROCESSOR_UNKNOWN,
1214 CPU_MOVDIR64B_FLAGS, 0 },
1215 { STRING_COMMA_LEN (".avx512_bf16"), PROCESSOR_UNKNOWN,
1216 CPU_AVX512_BF16_FLAGS, 0 },
1217 { STRING_COMMA_LEN (".avx512_vp2intersect"), PROCESSOR_UNKNOWN,
1218 CPU_AVX512_VP2INTERSECT_FLAGS, 0 },
1219 { STRING_COMMA_LEN (".tdx"), PROCESSOR_UNKNOWN,
1220 CPU_TDX_FLAGS, 0 },
1221 { STRING_COMMA_LEN (".enqcmd"), PROCESSOR_UNKNOWN,
1222 CPU_ENQCMD_FLAGS, 0 },
1223 { STRING_COMMA_LEN (".serialize"), PROCESSOR_UNKNOWN,
1224 CPU_SERIALIZE_FLAGS, 0 },
1225 { STRING_COMMA_LEN (".rdpru"), PROCESSOR_UNKNOWN,
1226 CPU_RDPRU_FLAGS, 0 },
1227 { STRING_COMMA_LEN (".mcommit"), PROCESSOR_UNKNOWN,
1228 CPU_MCOMMIT_FLAGS, 0 },
1229 { STRING_COMMA_LEN (".sev_es"), PROCESSOR_UNKNOWN,
1230 CPU_SEV_ES_FLAGS, 0 },
1231 { STRING_COMMA_LEN (".tsxldtrk"), PROCESSOR_UNKNOWN,
1232 CPU_TSXLDTRK_FLAGS, 0 },
1233 { STRING_COMMA_LEN (".kl"), PROCESSOR_UNKNOWN,
1234 CPU_KL_FLAGS, 0 },
1235 { STRING_COMMA_LEN (".widekl"), PROCESSOR_UNKNOWN,
1236 CPU_WIDEKL_FLAGS, 0 },
1237 { STRING_COMMA_LEN (".uintr"), PROCESSOR_UNKNOWN,
1238 CPU_UINTR_FLAGS, 0 },
1239 { STRING_COMMA_LEN (".hreset"), PROCESSOR_UNKNOWN,
1240 CPU_HRESET_FLAGS, 0 },
1241 { STRING_COMMA_LEN (".avx512_fp16"), PROCESSOR_UNKNOWN,
1242 CPU_AVX512_FP16_FLAGS, 0 },
1243 };
1244
1245 static const noarch_entry cpu_noarch[] =
1246 {
1247 { STRING_COMMA_LEN ("no87"), CPU_ANY_X87_FLAGS },
1248 { STRING_COMMA_LEN ("no287"), CPU_ANY_287_FLAGS },
1249 { STRING_COMMA_LEN ("no387"), CPU_ANY_387_FLAGS },
1250 { STRING_COMMA_LEN ("no687"), CPU_ANY_687_FLAGS },
1251 { STRING_COMMA_LEN ("nocmov"), CPU_ANY_CMOV_FLAGS },
1252 { STRING_COMMA_LEN ("nofxsr"), CPU_ANY_FXSR_FLAGS },
1253 { STRING_COMMA_LEN ("nommx"), CPU_ANY_MMX_FLAGS },
1254 { STRING_COMMA_LEN ("nosse"), CPU_ANY_SSE_FLAGS },
1255 { STRING_COMMA_LEN ("nosse2"), CPU_ANY_SSE2_FLAGS },
1256 { STRING_COMMA_LEN ("nosse3"), CPU_ANY_SSE3_FLAGS },
1257 { STRING_COMMA_LEN ("nosse4a"), CPU_ANY_SSE4A_FLAGS },
1258 { STRING_COMMA_LEN ("nossse3"), CPU_ANY_SSSE3_FLAGS },
1259 { STRING_COMMA_LEN ("nosse4.1"), CPU_ANY_SSE4_1_FLAGS },
1260 { STRING_COMMA_LEN ("nosse4.2"), CPU_ANY_SSE4_2_FLAGS },
1261 { STRING_COMMA_LEN ("nosse4"), CPU_ANY_SSE4_1_FLAGS },
1262 { STRING_COMMA_LEN ("noavx"), CPU_ANY_AVX_FLAGS },
1263 { STRING_COMMA_LEN ("noavx2"), CPU_ANY_AVX2_FLAGS },
1264 { STRING_COMMA_LEN ("noavx512f"), CPU_ANY_AVX512F_FLAGS },
1265 { STRING_COMMA_LEN ("noavx512cd"), CPU_ANY_AVX512CD_FLAGS },
1266 { STRING_COMMA_LEN ("noavx512er"), CPU_ANY_AVX512ER_FLAGS },
1267 { STRING_COMMA_LEN ("noavx512pf"), CPU_ANY_AVX512PF_FLAGS },
1268 { STRING_COMMA_LEN ("noavx512dq"), CPU_ANY_AVX512DQ_FLAGS },
1269 { STRING_COMMA_LEN ("noavx512bw"), CPU_ANY_AVX512BW_FLAGS },
1270 { STRING_COMMA_LEN ("noavx512vl"), CPU_ANY_AVX512VL_FLAGS },
1271 { STRING_COMMA_LEN ("noavx512ifma"), CPU_ANY_AVX512IFMA_FLAGS },
1272 { STRING_COMMA_LEN ("noavx512vbmi"), CPU_ANY_AVX512VBMI_FLAGS },
1273 { STRING_COMMA_LEN ("noavx512_4fmaps"), CPU_ANY_AVX512_4FMAPS_FLAGS },
1274 { STRING_COMMA_LEN ("noavx512_4vnniw"), CPU_ANY_AVX512_4VNNIW_FLAGS },
1275 { STRING_COMMA_LEN ("noavx512_vpopcntdq"), CPU_ANY_AVX512_VPOPCNTDQ_FLAGS },
1276 { STRING_COMMA_LEN ("noavx512_vbmi2"), CPU_ANY_AVX512_VBMI2_FLAGS },
1277 { STRING_COMMA_LEN ("noavx512_vnni"), CPU_ANY_AVX512_VNNI_FLAGS },
1278 { STRING_COMMA_LEN ("noavx512_bitalg"), CPU_ANY_AVX512_BITALG_FLAGS },
1279 { STRING_COMMA_LEN ("noavx_vnni"), CPU_ANY_AVX_VNNI_FLAGS },
1280 { STRING_COMMA_LEN ("noibt"), CPU_ANY_IBT_FLAGS },
1281 { STRING_COMMA_LEN ("noshstk"), CPU_ANY_SHSTK_FLAGS },
1282 { STRING_COMMA_LEN ("noamx_int8"), CPU_ANY_AMX_INT8_FLAGS },
1283 { STRING_COMMA_LEN ("noamx_bf16"), CPU_ANY_AMX_BF16_FLAGS },
1284 { STRING_COMMA_LEN ("noamx_tile"), CPU_ANY_AMX_TILE_FLAGS },
1285 { STRING_COMMA_LEN ("nomovdiri"), CPU_ANY_MOVDIRI_FLAGS },
1286 { STRING_COMMA_LEN ("nomovdir64b"), CPU_ANY_MOVDIR64B_FLAGS },
1287 { STRING_COMMA_LEN ("noavx512_bf16"), CPU_ANY_AVX512_BF16_FLAGS },
1288 { STRING_COMMA_LEN ("noavx512_vp2intersect"),
1289 CPU_ANY_AVX512_VP2INTERSECT_FLAGS },
1290 { STRING_COMMA_LEN ("notdx"), CPU_ANY_TDX_FLAGS },
1291 { STRING_COMMA_LEN ("noenqcmd"), CPU_ANY_ENQCMD_FLAGS },
1292 { STRING_COMMA_LEN ("noserialize"), CPU_ANY_SERIALIZE_FLAGS },
1293 { STRING_COMMA_LEN ("notsxldtrk"), CPU_ANY_TSXLDTRK_FLAGS },
1294 { STRING_COMMA_LEN ("nokl"), CPU_ANY_KL_FLAGS },
1295 { STRING_COMMA_LEN ("nowidekl"), CPU_ANY_WIDEKL_FLAGS },
1296 { STRING_COMMA_LEN ("nouintr"), CPU_ANY_UINTR_FLAGS },
1297 { STRING_COMMA_LEN ("nohreset"), CPU_ANY_HRESET_FLAGS },
1298 { STRING_COMMA_LEN ("noavx512_fp16"), CPU_ANY_AVX512_FP16_FLAGS },
1299 };
1300
1301 #ifdef I386COFF
1302 /* Like s_lcomm_internal in gas/read.c but the alignment string
1303 is allowed to be optional. */
1304
1305 static symbolS *
1306 pe_lcomm_internal (int needs_align, symbolS *symbolP, addressT size)
1307 {
1308 addressT align = 0;
1309
1310 SKIP_WHITESPACE ();
1311
1312 if (needs_align
1313 && *input_line_pointer == ',')
1314 {
1315 align = parse_align (needs_align - 1);
1316
1317 if (align == (addressT) -1)
1318 return NULL;
1319 }
1320 else
1321 {
1322 if (size >= 8)
1323 align = 3;
1324 else if (size >= 4)
1325 align = 2;
1326 else if (size >= 2)
1327 align = 1;
1328 else
1329 align = 0;
1330 }
1331
1332 bss_alloc (symbolP, size, align);
1333 return symbolP;
1334 }
1335
1336 static void
1337 pe_lcomm (int needs_align)
1338 {
1339 s_comm_internal (needs_align * 2, pe_lcomm_internal);
1340 }
1341 #endif
1342
1343 const pseudo_typeS md_pseudo_table[] =
1344 {
1345 #if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
1346 {"align", s_align_bytes, 0},
1347 #else
1348 {"align", s_align_ptwo, 0},
1349 #endif
1350 {"arch", set_cpu_arch, 0},
1351 #ifndef I386COFF
1352 {"bss", s_bss, 0},
1353 #else
1354 {"lcomm", pe_lcomm, 1},
1355 #endif
1356 {"ffloat", float_cons, 'f'},
1357 {"dfloat", float_cons, 'd'},
1358 {"tfloat", float_cons, 'x'},
1359 {"hfloat", float_cons, 'h'},
1360 {"bfloat16", float_cons, 'b'},
1361 {"value", cons, 2},
1362 {"slong", signed_cons, 4},
1363 {"noopt", s_ignore, 0},
1364 {"optim", s_ignore, 0},
1365 {"code16gcc", set_16bit_gcc_code_flag, CODE_16BIT},
1366 {"code16", set_code_flag, CODE_16BIT},
1367 {"code32", set_code_flag, CODE_32BIT},
1368 #ifdef BFD64
1369 {"code64", set_code_flag, CODE_64BIT},
1370 #endif
1371 {"intel_syntax", set_intel_syntax, 1},
1372 {"att_syntax", set_intel_syntax, 0},
1373 {"intel_mnemonic", set_intel_mnemonic, 1},
1374 {"att_mnemonic", set_intel_mnemonic, 0},
1375 {"allow_index_reg", set_allow_index_reg, 1},
1376 {"disallow_index_reg", set_allow_index_reg, 0},
1377 {"sse_check", set_check, 0},
1378 {"operand_check", set_check, 1},
1379 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1380 {"largecomm", handle_large_common, 0},
1381 #else
1382 {"file", dwarf2_directive_file, 0},
1383 {"loc", dwarf2_directive_loc, 0},
1384 {"loc_mark_labels", dwarf2_directive_loc_mark_labels, 0},
1385 #endif
1386 #ifdef TE_PE
1387 {"secrel32", pe_directive_secrel, 0},
1388 #endif
1389 {0, 0, 0}
1390 };
1391
1392 /* For interface with expression (). */
1393 extern char *input_line_pointer;
1394
1395 /* Hash table for instruction mnemonic lookup. */
1396 static htab_t op_hash;
1397
1398 /* Hash table for register lookup. */
1399 static htab_t reg_hash;
1400 \f
1401 /* Various efficient no-op patterns for aligning code labels.
1402 Note: Don't try to assemble the instructions in the comments.
1403 0L and 0w are not legal. */
1404 static const unsigned char f32_1[] =
1405 {0x90}; /* nop */
1406 static const unsigned char f32_2[] =
1407 {0x66,0x90}; /* xchg %ax,%ax */
1408 static const unsigned char f32_3[] =
1409 {0x8d,0x76,0x00}; /* leal 0(%esi),%esi */
1410 static const unsigned char f32_4[] =
1411 {0x8d,0x74,0x26,0x00}; /* leal 0(%esi,1),%esi */
1412 static const unsigned char f32_6[] =
1413 {0x8d,0xb6,0x00,0x00,0x00,0x00}; /* leal 0L(%esi),%esi */
1414 static const unsigned char f32_7[] =
1415 {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal 0L(%esi,1),%esi */
1416 static const unsigned char f16_3[] =
1417 {0x8d,0x74,0x00}; /* lea 0(%si),%si */
1418 static const unsigned char f16_4[] =
1419 {0x8d,0xb4,0x00,0x00}; /* lea 0W(%si),%si */
1420 static const unsigned char jump_disp8[] =
1421 {0xeb}; /* jmp disp8 */
1422 static const unsigned char jump32_disp32[] =
1423 {0xe9}; /* jmp disp32 */
1424 static const unsigned char jump16_disp32[] =
1425 {0x66,0xe9}; /* jmp disp32 */
1426 /* 32-bit NOPs patterns. */
1427 static const unsigned char *const f32_patt[] = {
1428 f32_1, f32_2, f32_3, f32_4, NULL, f32_6, f32_7
1429 };
1430 /* 16-bit NOPs patterns. */
1431 static const unsigned char *const f16_patt[] = {
1432 f32_1, f32_2, f16_3, f16_4
1433 };
1434 /* nopl (%[re]ax) */
1435 static const unsigned char alt_3[] =
1436 {0x0f,0x1f,0x00};
1437 /* nopl 0(%[re]ax) */
1438 static const unsigned char alt_4[] =
1439 {0x0f,0x1f,0x40,0x00};
1440 /* nopl 0(%[re]ax,%[re]ax,1) */
1441 static const unsigned char alt_5[] =
1442 {0x0f,0x1f,0x44,0x00,0x00};
1443 /* nopw 0(%[re]ax,%[re]ax,1) */
1444 static const unsigned char alt_6[] =
1445 {0x66,0x0f,0x1f,0x44,0x00,0x00};
1446 /* nopl 0L(%[re]ax) */
1447 static const unsigned char alt_7[] =
1448 {0x0f,0x1f,0x80,0x00,0x00,0x00,0x00};
1449 /* nopl 0L(%[re]ax,%[re]ax,1) */
1450 static const unsigned char alt_8[] =
1451 {0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1452 /* nopw 0L(%[re]ax,%[re]ax,1) */
1453 static const unsigned char alt_9[] =
1454 {0x66,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1455 /* nopw %cs:0L(%[re]ax,%[re]ax,1) */
1456 static const unsigned char alt_10[] =
1457 {0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1458 /* data16 nopw %cs:0L(%eax,%eax,1) */
1459 static const unsigned char alt_11[] =
1460 {0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1461 /* 32-bit and 64-bit NOPs patterns. */
1462 static const unsigned char *const alt_patt[] = {
1463 f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
1464 alt_9, alt_10, alt_11
1465 };
1466
1467 /* Genenerate COUNT bytes of NOPs to WHERE from PATT with the maximum
1468 size of a single NOP instruction MAX_SINGLE_NOP_SIZE. */
1469
1470 static void
1471 i386_output_nops (char *where, const unsigned char *const *patt,
1472 int count, int max_single_nop_size)
1473
1474 {
1475 /* Place the longer NOP first. */
1476 int last;
1477 int offset;
1478 const unsigned char *nops;
1479
1480 if (max_single_nop_size < 1)
1481 {
1482 as_fatal (_("i386_output_nops called to generate nops of at most %d bytes!"),
1483 max_single_nop_size);
1484 return;
1485 }
1486
1487 nops = patt[max_single_nop_size - 1];
1488
1489 /* Use the smaller one if the requsted one isn't available. */
1490 if (nops == NULL)
1491 {
1492 max_single_nop_size--;
1493 nops = patt[max_single_nop_size - 1];
1494 }
1495
1496 last = count % max_single_nop_size;
1497
1498 count -= last;
1499 for (offset = 0; offset < count; offset += max_single_nop_size)
1500 memcpy (where + offset, nops, max_single_nop_size);
1501
1502 if (last)
1503 {
1504 nops = patt[last - 1];
1505 if (nops == NULL)
1506 {
1507 /* Use the smaller one plus one-byte NOP if the needed one
1508 isn't available. */
1509 last--;
1510 nops = patt[last - 1];
1511 memcpy (where + offset, nops, last);
1512 where[offset + last] = *patt[0];
1513 }
1514 else
1515 memcpy (where + offset, nops, last);
1516 }
1517 }
1518
1519 static INLINE int
1520 fits_in_imm7 (offsetT num)
1521 {
1522 return (num & 0x7f) == num;
1523 }
1524
1525 static INLINE int
1526 fits_in_imm31 (offsetT num)
1527 {
1528 return (num & 0x7fffffff) == num;
1529 }
1530
1531 /* Genenerate COUNT bytes of NOPs to WHERE with the maximum size of a
1532 single NOP instruction LIMIT. */
1533
1534 void
1535 i386_generate_nops (fragS *fragP, char *where, offsetT count, int limit)
1536 {
1537 const unsigned char *const *patt = NULL;
1538 int max_single_nop_size;
1539 /* Maximum number of NOPs before switching to jump over NOPs. */
1540 int max_number_of_nops;
1541
1542 switch (fragP->fr_type)
1543 {
1544 case rs_fill_nop:
1545 case rs_align_code:
1546 break;
1547 case rs_machine_dependent:
1548 /* Allow NOP padding for jumps and calls. */
1549 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
1550 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
1551 break;
1552 /* Fall through. */
1553 default:
1554 return;
1555 }
1556
1557 /* We need to decide which NOP sequence to use for 32bit and
1558 64bit. When -mtune= is used:
1559
1560 1. For PROCESSOR_I386, PROCESSOR_I486, PROCESSOR_PENTIUM and
1561 PROCESSOR_GENERIC32, f32_patt will be used.
1562 2. For the rest, alt_patt will be used.
1563
1564 When -mtune= isn't used, alt_patt will be used if
1565 cpu_arch_isa_flags has CpuNop. Otherwise, f32_patt will
1566 be used.
1567
1568 When -march= or .arch is used, we can't use anything beyond
1569 cpu_arch_isa_flags. */
1570
1571 if (flag_code == CODE_16BIT)
1572 {
1573 patt = f16_patt;
1574 max_single_nop_size = sizeof (f16_patt) / sizeof (f16_patt[0]);
1575 /* Limit number of NOPs to 2 in 16-bit mode. */
1576 max_number_of_nops = 2;
1577 }
1578 else
1579 {
1580 if (fragP->tc_frag_data.isa == PROCESSOR_UNKNOWN)
1581 {
1582 /* PROCESSOR_UNKNOWN means that all ISAs may be used. */
1583 switch (cpu_arch_tune)
1584 {
1585 case PROCESSOR_UNKNOWN:
1586 /* We use cpu_arch_isa_flags to check if we SHOULD
1587 optimize with nops. */
1588 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
1589 patt = alt_patt;
1590 else
1591 patt = f32_patt;
1592 break;
1593 case PROCESSOR_PENTIUM4:
1594 case PROCESSOR_NOCONA:
1595 case PROCESSOR_CORE:
1596 case PROCESSOR_CORE2:
1597 case PROCESSOR_COREI7:
1598 case PROCESSOR_GENERIC64:
1599 case PROCESSOR_K6:
1600 case PROCESSOR_ATHLON:
1601 case PROCESSOR_K8:
1602 case PROCESSOR_AMDFAM10:
1603 case PROCESSOR_BD:
1604 case PROCESSOR_ZNVER:
1605 case PROCESSOR_BT:
1606 patt = alt_patt;
1607 break;
1608 case PROCESSOR_I386:
1609 case PROCESSOR_I486:
1610 case PROCESSOR_PENTIUM:
1611 case PROCESSOR_PENTIUMPRO:
1612 case PROCESSOR_IAMCU:
1613 case PROCESSOR_GENERIC32:
1614 patt = f32_patt;
1615 break;
1616 }
1617 }
1618 else
1619 {
1620 switch (fragP->tc_frag_data.tune)
1621 {
1622 case PROCESSOR_UNKNOWN:
1623 /* When cpu_arch_isa is set, cpu_arch_tune shouldn't be
1624 PROCESSOR_UNKNOWN. */
1625 abort ();
1626 break;
1627
1628 case PROCESSOR_I386:
1629 case PROCESSOR_I486:
1630 case PROCESSOR_PENTIUM:
1631 case PROCESSOR_IAMCU:
1632 case PROCESSOR_K6:
1633 case PROCESSOR_ATHLON:
1634 case PROCESSOR_K8:
1635 case PROCESSOR_AMDFAM10:
1636 case PROCESSOR_BD:
1637 case PROCESSOR_ZNVER:
1638 case PROCESSOR_BT:
1639 case PROCESSOR_GENERIC32:
1640 /* We use cpu_arch_isa_flags to check if we CAN optimize
1641 with nops. */
1642 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
1643 patt = alt_patt;
1644 else
1645 patt = f32_patt;
1646 break;
1647 case PROCESSOR_PENTIUMPRO:
1648 case PROCESSOR_PENTIUM4:
1649 case PROCESSOR_NOCONA:
1650 case PROCESSOR_CORE:
1651 case PROCESSOR_CORE2:
1652 case PROCESSOR_COREI7:
1653 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
1654 patt = alt_patt;
1655 else
1656 patt = f32_patt;
1657 break;
1658 case PROCESSOR_GENERIC64:
1659 patt = alt_patt;
1660 break;
1661 }
1662 }
1663
1664 if (patt == f32_patt)
1665 {
1666 max_single_nop_size = sizeof (f32_patt) / sizeof (f32_patt[0]);
1667 /* Limit number of NOPs to 2 for older processors. */
1668 max_number_of_nops = 2;
1669 }
1670 else
1671 {
1672 max_single_nop_size = sizeof (alt_patt) / sizeof (alt_patt[0]);
1673 /* Limit number of NOPs to 7 for newer processors. */
1674 max_number_of_nops = 7;
1675 }
1676 }
1677
1678 if (limit == 0)
1679 limit = max_single_nop_size;
1680
1681 if (fragP->fr_type == rs_fill_nop)
1682 {
1683 /* Output NOPs for .nop directive. */
1684 if (limit > max_single_nop_size)
1685 {
1686 as_bad_where (fragP->fr_file, fragP->fr_line,
1687 _("invalid single nop size: %d "
1688 "(expect within [0, %d])"),
1689 limit, max_single_nop_size);
1690 return;
1691 }
1692 }
1693 else if (fragP->fr_type != rs_machine_dependent)
1694 fragP->fr_var = count;
1695
1696 if ((count / max_single_nop_size) > max_number_of_nops)
1697 {
1698 /* Generate jump over NOPs. */
1699 offsetT disp = count - 2;
1700 if (fits_in_imm7 (disp))
1701 {
1702 /* Use "jmp disp8" if possible. */
1703 count = disp;
1704 where[0] = jump_disp8[0];
1705 where[1] = count;
1706 where += 2;
1707 }
1708 else
1709 {
1710 unsigned int size_of_jump;
1711
1712 if (flag_code == CODE_16BIT)
1713 {
1714 where[0] = jump16_disp32[0];
1715 where[1] = jump16_disp32[1];
1716 size_of_jump = 2;
1717 }
1718 else
1719 {
1720 where[0] = jump32_disp32[0];
1721 size_of_jump = 1;
1722 }
1723
1724 count -= size_of_jump + 4;
1725 if (!fits_in_imm31 (count))
1726 {
1727 as_bad_where (fragP->fr_file, fragP->fr_line,
1728 _("jump over nop padding out of range"));
1729 return;
1730 }
1731
1732 md_number_to_chars (where + size_of_jump, count, 4);
1733 where += size_of_jump + 4;
1734 }
1735 }
1736
1737 /* Generate multiple NOPs. */
1738 i386_output_nops (where, patt, count, limit);
1739 }
1740
1741 static INLINE int
1742 operand_type_all_zero (const union i386_operand_type *x)
1743 {
1744 switch (ARRAY_SIZE(x->array))
1745 {
1746 case 3:
1747 if (x->array[2])
1748 return 0;
1749 /* Fall through. */
1750 case 2:
1751 if (x->array[1])
1752 return 0;
1753 /* Fall through. */
1754 case 1:
1755 return !x->array[0];
1756 default:
1757 abort ();
1758 }
1759 }
1760
1761 static INLINE void
1762 operand_type_set (union i386_operand_type *x, unsigned int v)
1763 {
1764 switch (ARRAY_SIZE(x->array))
1765 {
1766 case 3:
1767 x->array[2] = v;
1768 /* Fall through. */
1769 case 2:
1770 x->array[1] = v;
1771 /* Fall through. */
1772 case 1:
1773 x->array[0] = v;
1774 /* Fall through. */
1775 break;
1776 default:
1777 abort ();
1778 }
1779
1780 x->bitfield.class = ClassNone;
1781 x->bitfield.instance = InstanceNone;
1782 }
1783
1784 static INLINE int
1785 operand_type_equal (const union i386_operand_type *x,
1786 const union i386_operand_type *y)
1787 {
1788 switch (ARRAY_SIZE(x->array))
1789 {
1790 case 3:
1791 if (x->array[2] != y->array[2])
1792 return 0;
1793 /* Fall through. */
1794 case 2:
1795 if (x->array[1] != y->array[1])
1796 return 0;
1797 /* Fall through. */
1798 case 1:
1799 return x->array[0] == y->array[0];
1800 break;
1801 default:
1802 abort ();
1803 }
1804 }
1805
1806 static INLINE int
1807 cpu_flags_all_zero (const union i386_cpu_flags *x)
1808 {
1809 switch (ARRAY_SIZE(x->array))
1810 {
1811 case 4:
1812 if (x->array[3])
1813 return 0;
1814 /* Fall through. */
1815 case 3:
1816 if (x->array[2])
1817 return 0;
1818 /* Fall through. */
1819 case 2:
1820 if (x->array[1])
1821 return 0;
1822 /* Fall through. */
1823 case 1:
1824 return !x->array[0];
1825 default:
1826 abort ();
1827 }
1828 }
1829
1830 static INLINE int
1831 cpu_flags_equal (const union i386_cpu_flags *x,
1832 const union i386_cpu_flags *y)
1833 {
1834 switch (ARRAY_SIZE(x->array))
1835 {
1836 case 4:
1837 if (x->array[3] != y->array[3])
1838 return 0;
1839 /* Fall through. */
1840 case 3:
1841 if (x->array[2] != y->array[2])
1842 return 0;
1843 /* Fall through. */
1844 case 2:
1845 if (x->array[1] != y->array[1])
1846 return 0;
1847 /* Fall through. */
1848 case 1:
1849 return x->array[0] == y->array[0];
1850 break;
1851 default:
1852 abort ();
1853 }
1854 }
1855
1856 static INLINE int
1857 cpu_flags_check_cpu64 (i386_cpu_flags f)
1858 {
1859 return !((flag_code == CODE_64BIT && f.bitfield.cpuno64)
1860 || (flag_code != CODE_64BIT && f.bitfield.cpu64));
1861 }
1862
1863 static INLINE i386_cpu_flags
1864 cpu_flags_and (i386_cpu_flags x, i386_cpu_flags y)
1865 {
1866 switch (ARRAY_SIZE (x.array))
1867 {
1868 case 4:
1869 x.array [3] &= y.array [3];
1870 /* Fall through. */
1871 case 3:
1872 x.array [2] &= y.array [2];
1873 /* Fall through. */
1874 case 2:
1875 x.array [1] &= y.array [1];
1876 /* Fall through. */
1877 case 1:
1878 x.array [0] &= y.array [0];
1879 break;
1880 default:
1881 abort ();
1882 }
1883 return x;
1884 }
1885
1886 static INLINE i386_cpu_flags
1887 cpu_flags_or (i386_cpu_flags x, i386_cpu_flags y)
1888 {
1889 switch (ARRAY_SIZE (x.array))
1890 {
1891 case 4:
1892 x.array [3] |= y.array [3];
1893 /* Fall through. */
1894 case 3:
1895 x.array [2] |= y.array [2];
1896 /* Fall through. */
1897 case 2:
1898 x.array [1] |= y.array [1];
1899 /* Fall through. */
1900 case 1:
1901 x.array [0] |= y.array [0];
1902 break;
1903 default:
1904 abort ();
1905 }
1906 return x;
1907 }
1908
1909 static INLINE i386_cpu_flags
1910 cpu_flags_and_not (i386_cpu_flags x, i386_cpu_flags y)
1911 {
1912 switch (ARRAY_SIZE (x.array))
1913 {
1914 case 4:
1915 x.array [3] &= ~y.array [3];
1916 /* Fall through. */
1917 case 3:
1918 x.array [2] &= ~y.array [2];
1919 /* Fall through. */
1920 case 2:
1921 x.array [1] &= ~y.array [1];
1922 /* Fall through. */
1923 case 1:
1924 x.array [0] &= ~y.array [0];
1925 break;
1926 default:
1927 abort ();
1928 }
1929 return x;
1930 }
1931
1932 static const i386_cpu_flags avx512 = CPU_ANY_AVX512F_FLAGS;
1933
1934 #define CPU_FLAGS_ARCH_MATCH 0x1
1935 #define CPU_FLAGS_64BIT_MATCH 0x2
1936
1937 #define CPU_FLAGS_PERFECT_MATCH \
1938 (CPU_FLAGS_ARCH_MATCH | CPU_FLAGS_64BIT_MATCH)
1939
1940 /* Return CPU flags match bits. */
1941
1942 static int
1943 cpu_flags_match (const insn_template *t)
1944 {
1945 i386_cpu_flags x = t->cpu_flags;
1946 int match = cpu_flags_check_cpu64 (x) ? CPU_FLAGS_64BIT_MATCH : 0;
1947
1948 x.bitfield.cpu64 = 0;
1949 x.bitfield.cpuno64 = 0;
1950
1951 if (cpu_flags_all_zero (&x))
1952 {
1953 /* This instruction is available on all archs. */
1954 match |= CPU_FLAGS_ARCH_MATCH;
1955 }
1956 else
1957 {
1958 /* This instruction is available only on some archs. */
1959 i386_cpu_flags cpu = cpu_arch_flags;
1960
1961 /* AVX512VL is no standalone feature - match it and then strip it. */
1962 if (x.bitfield.cpuavx512vl && !cpu.bitfield.cpuavx512vl)
1963 return match;
1964 x.bitfield.cpuavx512vl = 0;
1965
1966 /* AVX and AVX2 present at the same time express an operand size
1967 dependency - strip AVX2 for the purposes here. The operand size
1968 dependent check occurs in check_vecOperands(). */
1969 if (x.bitfield.cpuavx && x.bitfield.cpuavx2)
1970 x.bitfield.cpuavx2 = 0;
1971
1972 cpu = cpu_flags_and (x, cpu);
1973 if (!cpu_flags_all_zero (&cpu))
1974 {
1975 if (x.bitfield.cpuavx)
1976 {
1977 /* We need to check a few extra flags with AVX. */
1978 if (cpu.bitfield.cpuavx
1979 && (!t->opcode_modifier.sse2avx
1980 || (sse2avx && !i.prefix[DATA_PREFIX]))
1981 && (!x.bitfield.cpuaes || cpu.bitfield.cpuaes)
1982 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
1983 && (!x.bitfield.cpupclmul || cpu.bitfield.cpupclmul))
1984 match |= CPU_FLAGS_ARCH_MATCH;
1985 }
1986 else if (x.bitfield.cpuavx512f)
1987 {
1988 /* We need to check a few extra flags with AVX512F. */
1989 if (cpu.bitfield.cpuavx512f
1990 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
1991 && (!x.bitfield.cpuvaes || cpu.bitfield.cpuvaes)
1992 && (!x.bitfield.cpuvpclmulqdq || cpu.bitfield.cpuvpclmulqdq))
1993 match |= CPU_FLAGS_ARCH_MATCH;
1994 }
1995 else
1996 match |= CPU_FLAGS_ARCH_MATCH;
1997 }
1998 }
1999 return match;
2000 }
2001
2002 static INLINE i386_operand_type
2003 operand_type_and (i386_operand_type x, i386_operand_type y)
2004 {
2005 if (x.bitfield.class != y.bitfield.class)
2006 x.bitfield.class = ClassNone;
2007 if (x.bitfield.instance != y.bitfield.instance)
2008 x.bitfield.instance = InstanceNone;
2009
2010 switch (ARRAY_SIZE (x.array))
2011 {
2012 case 3:
2013 x.array [2] &= y.array [2];
2014 /* Fall through. */
2015 case 2:
2016 x.array [1] &= y.array [1];
2017 /* Fall through. */
2018 case 1:
2019 x.array [0] &= y.array [0];
2020 break;
2021 default:
2022 abort ();
2023 }
2024 return x;
2025 }
2026
2027 static INLINE i386_operand_type
2028 operand_type_and_not (i386_operand_type x, i386_operand_type y)
2029 {
2030 gas_assert (y.bitfield.class == ClassNone);
2031 gas_assert (y.bitfield.instance == InstanceNone);
2032
2033 switch (ARRAY_SIZE (x.array))
2034 {
2035 case 3:
2036 x.array [2] &= ~y.array [2];
2037 /* Fall through. */
2038 case 2:
2039 x.array [1] &= ~y.array [1];
2040 /* Fall through. */
2041 case 1:
2042 x.array [0] &= ~y.array [0];
2043 break;
2044 default:
2045 abort ();
2046 }
2047 return x;
2048 }
2049
2050 static INLINE i386_operand_type
2051 operand_type_or (i386_operand_type x, i386_operand_type y)
2052 {
2053 gas_assert (x.bitfield.class == ClassNone ||
2054 y.bitfield.class == ClassNone ||
2055 x.bitfield.class == y.bitfield.class);
2056 gas_assert (x.bitfield.instance == InstanceNone ||
2057 y.bitfield.instance == InstanceNone ||
2058 x.bitfield.instance == y.bitfield.instance);
2059
2060 switch (ARRAY_SIZE (x.array))
2061 {
2062 case 3:
2063 x.array [2] |= y.array [2];
2064 /* Fall through. */
2065 case 2:
2066 x.array [1] |= y.array [1];
2067 /* Fall through. */
2068 case 1:
2069 x.array [0] |= y.array [0];
2070 break;
2071 default:
2072 abort ();
2073 }
2074 return x;
2075 }
2076
2077 static INLINE i386_operand_type
2078 operand_type_xor (i386_operand_type x, i386_operand_type y)
2079 {
2080 gas_assert (y.bitfield.class == ClassNone);
2081 gas_assert (y.bitfield.instance == InstanceNone);
2082
2083 switch (ARRAY_SIZE (x.array))
2084 {
2085 case 3:
2086 x.array [2] ^= y.array [2];
2087 /* Fall through. */
2088 case 2:
2089 x.array [1] ^= y.array [1];
2090 /* Fall through. */
2091 case 1:
2092 x.array [0] ^= y.array [0];
2093 break;
2094 default:
2095 abort ();
2096 }
2097 return x;
2098 }
2099
2100 static const i386_operand_type disp16_32 = OPERAND_TYPE_DISP16_32;
2101 static const i386_operand_type anydisp = OPERAND_TYPE_ANYDISP;
2102 static const i386_operand_type anyimm = OPERAND_TYPE_ANYIMM;
2103 static const i386_operand_type regxmm = OPERAND_TYPE_REGXMM;
2104 static const i386_operand_type regmask = OPERAND_TYPE_REGMASK;
2105 static const i386_operand_type imm8 = OPERAND_TYPE_IMM8;
2106 static const i386_operand_type imm8s = OPERAND_TYPE_IMM8S;
2107 static const i386_operand_type imm16 = OPERAND_TYPE_IMM16;
2108 static const i386_operand_type imm32 = OPERAND_TYPE_IMM32;
2109 static const i386_operand_type imm32s = OPERAND_TYPE_IMM32S;
2110 static const i386_operand_type imm64 = OPERAND_TYPE_IMM64;
2111 static const i386_operand_type imm16_32 = OPERAND_TYPE_IMM16_32;
2112 static const i386_operand_type imm16_32s = OPERAND_TYPE_IMM16_32S;
2113 static const i386_operand_type imm16_32_32s = OPERAND_TYPE_IMM16_32_32S;
2114
2115 enum operand_type
2116 {
2117 reg,
2118 imm,
2119 disp,
2120 anymem
2121 };
2122
2123 static INLINE int
2124 operand_type_check (i386_operand_type t, enum operand_type c)
2125 {
2126 switch (c)
2127 {
2128 case reg:
2129 return t.bitfield.class == Reg;
2130
2131 case imm:
2132 return (t.bitfield.imm8
2133 || t.bitfield.imm8s
2134 || t.bitfield.imm16
2135 || t.bitfield.imm32
2136 || t.bitfield.imm32s
2137 || t.bitfield.imm64);
2138
2139 case disp:
2140 return (t.bitfield.disp8
2141 || t.bitfield.disp16
2142 || t.bitfield.disp32
2143 || t.bitfield.disp32s
2144 || t.bitfield.disp64);
2145
2146 case anymem:
2147 return (t.bitfield.disp8
2148 || t.bitfield.disp16
2149 || t.bitfield.disp32
2150 || t.bitfield.disp32s
2151 || t.bitfield.disp64
2152 || t.bitfield.baseindex);
2153
2154 default:
2155 abort ();
2156 }
2157
2158 return 0;
2159 }
2160
2161 /* Return 1 if there is no conflict in 8bit/16bit/32bit/64bit/80bit size
2162 between operand GIVEN and opeand WANTED for instruction template T. */
2163
2164 static INLINE int
2165 match_operand_size (const insn_template *t, unsigned int wanted,
2166 unsigned int given)
2167 {
2168 return !((i.types[given].bitfield.byte
2169 && !t->operand_types[wanted].bitfield.byte)
2170 || (i.types[given].bitfield.word
2171 && !t->operand_types[wanted].bitfield.word)
2172 || (i.types[given].bitfield.dword
2173 && !t->operand_types[wanted].bitfield.dword)
2174 || (i.types[given].bitfield.qword
2175 && !t->operand_types[wanted].bitfield.qword)
2176 || (i.types[given].bitfield.tbyte
2177 && !t->operand_types[wanted].bitfield.tbyte));
2178 }
2179
2180 /* Return 1 if there is no conflict in SIMD register between operand
2181 GIVEN and opeand WANTED for instruction template T. */
2182
2183 static INLINE int
2184 match_simd_size (const insn_template *t, unsigned int wanted,
2185 unsigned int given)
2186 {
2187 return !((i.types[given].bitfield.xmmword
2188 && !t->operand_types[wanted].bitfield.xmmword)
2189 || (i.types[given].bitfield.ymmword
2190 && !t->operand_types[wanted].bitfield.ymmword)
2191 || (i.types[given].bitfield.zmmword
2192 && !t->operand_types[wanted].bitfield.zmmword)
2193 || (i.types[given].bitfield.tmmword
2194 && !t->operand_types[wanted].bitfield.tmmword));
2195 }
2196
2197 /* Return 1 if there is no conflict in any size between operand GIVEN
2198 and opeand WANTED for instruction template T. */
2199
2200 static INLINE int
2201 match_mem_size (const insn_template *t, unsigned int wanted,
2202 unsigned int given)
2203 {
2204 return (match_operand_size (t, wanted, given)
2205 && !((i.types[given].bitfield.unspecified
2206 && !i.broadcast.type
2207 && !t->operand_types[wanted].bitfield.unspecified)
2208 || (i.types[given].bitfield.fword
2209 && !t->operand_types[wanted].bitfield.fword)
2210 /* For scalar opcode templates to allow register and memory
2211 operands at the same time, some special casing is needed
2212 here. Also for v{,p}broadcast*, {,v}pmov{s,z}*, and
2213 down-conversion vpmov*. */
2214 || ((t->operand_types[wanted].bitfield.class == RegSIMD
2215 && t->operand_types[wanted].bitfield.byte
2216 + t->operand_types[wanted].bitfield.word
2217 + t->operand_types[wanted].bitfield.dword
2218 + t->operand_types[wanted].bitfield.qword
2219 > !!t->opcode_modifier.broadcast)
2220 ? (i.types[given].bitfield.xmmword
2221 || i.types[given].bitfield.ymmword
2222 || i.types[given].bitfield.zmmword)
2223 : !match_simd_size(t, wanted, given))));
2224 }
2225
2226 /* Return value has MATCH_STRAIGHT set if there is no size conflict on any
2227 operands for instruction template T, and it has MATCH_REVERSE set if there
2228 is no size conflict on any operands for the template with operands reversed
2229 (and the template allows for reversing in the first place). */
2230
2231 #define MATCH_STRAIGHT 1
2232 #define MATCH_REVERSE 2
2233
2234 static INLINE unsigned int
2235 operand_size_match (const insn_template *t)
2236 {
2237 unsigned int j, match = MATCH_STRAIGHT;
2238
2239 /* Don't check non-absolute jump instructions. */
2240 if (t->opcode_modifier.jump
2241 && t->opcode_modifier.jump != JUMP_ABSOLUTE)
2242 return match;
2243
2244 /* Check memory and accumulator operand size. */
2245 for (j = 0; j < i.operands; j++)
2246 {
2247 if (i.types[j].bitfield.class != Reg
2248 && i.types[j].bitfield.class != RegSIMD
2249 && t->opcode_modifier.anysize)
2250 continue;
2251
2252 if (t->operand_types[j].bitfield.class == Reg
2253 && !match_operand_size (t, j, j))
2254 {
2255 match = 0;
2256 break;
2257 }
2258
2259 if (t->operand_types[j].bitfield.class == RegSIMD
2260 && !match_simd_size (t, j, j))
2261 {
2262 match = 0;
2263 break;
2264 }
2265
2266 if (t->operand_types[j].bitfield.instance == Accum
2267 && (!match_operand_size (t, j, j) || !match_simd_size (t, j, j)))
2268 {
2269 match = 0;
2270 break;
2271 }
2272
2273 if ((i.flags[j] & Operand_Mem) && !match_mem_size (t, j, j))
2274 {
2275 match = 0;
2276 break;
2277 }
2278 }
2279
2280 if (!t->opcode_modifier.d)
2281 {
2282 mismatch:
2283 if (!match)
2284 i.error = operand_size_mismatch;
2285 return match;
2286 }
2287
2288 /* Check reverse. */
2289 gas_assert (i.operands >= 2 && i.operands <= 3);
2290
2291 for (j = 0; j < i.operands; j++)
2292 {
2293 unsigned int given = i.operands - j - 1;
2294
2295 if (t->operand_types[j].bitfield.class == Reg
2296 && !match_operand_size (t, j, given))
2297 goto mismatch;
2298
2299 if (t->operand_types[j].bitfield.class == RegSIMD
2300 && !match_simd_size (t, j, given))
2301 goto mismatch;
2302
2303 if (t->operand_types[j].bitfield.instance == Accum
2304 && (!match_operand_size (t, j, given)
2305 || !match_simd_size (t, j, given)))
2306 goto mismatch;
2307
2308 if ((i.flags[given] & Operand_Mem) && !match_mem_size (t, j, given))
2309 goto mismatch;
2310 }
2311
2312 return match | MATCH_REVERSE;
2313 }
2314
2315 static INLINE int
2316 operand_type_match (i386_operand_type overlap,
2317 i386_operand_type given)
2318 {
2319 i386_operand_type temp = overlap;
2320
2321 temp.bitfield.unspecified = 0;
2322 temp.bitfield.byte = 0;
2323 temp.bitfield.word = 0;
2324 temp.bitfield.dword = 0;
2325 temp.bitfield.fword = 0;
2326 temp.bitfield.qword = 0;
2327 temp.bitfield.tbyte = 0;
2328 temp.bitfield.xmmword = 0;
2329 temp.bitfield.ymmword = 0;
2330 temp.bitfield.zmmword = 0;
2331 temp.bitfield.tmmword = 0;
2332 if (operand_type_all_zero (&temp))
2333 goto mismatch;
2334
2335 if (given.bitfield.baseindex == overlap.bitfield.baseindex)
2336 return 1;
2337
2338 mismatch:
2339 i.error = operand_type_mismatch;
2340 return 0;
2341 }
2342
2343 /* If given types g0 and g1 are registers they must be of the same type
2344 unless the expected operand type register overlap is null.
2345 Some Intel syntax memory operand size checking also happens here. */
2346
2347 static INLINE int
2348 operand_type_register_match (i386_operand_type g0,
2349 i386_operand_type t0,
2350 i386_operand_type g1,
2351 i386_operand_type t1)
2352 {
2353 if (g0.bitfield.class != Reg
2354 && g0.bitfield.class != RegSIMD
2355 && (!operand_type_check (g0, anymem)
2356 || g0.bitfield.unspecified
2357 || (t0.bitfield.class != Reg
2358 && t0.bitfield.class != RegSIMD)))
2359 return 1;
2360
2361 if (g1.bitfield.class != Reg
2362 && g1.bitfield.class != RegSIMD
2363 && (!operand_type_check (g1, anymem)
2364 || g1.bitfield.unspecified
2365 || (t1.bitfield.class != Reg
2366 && t1.bitfield.class != RegSIMD)))
2367 return 1;
2368
2369 if (g0.bitfield.byte == g1.bitfield.byte
2370 && g0.bitfield.word == g1.bitfield.word
2371 && g0.bitfield.dword == g1.bitfield.dword
2372 && g0.bitfield.qword == g1.bitfield.qword
2373 && g0.bitfield.xmmword == g1.bitfield.xmmword
2374 && g0.bitfield.ymmword == g1.bitfield.ymmword
2375 && g0.bitfield.zmmword == g1.bitfield.zmmword)
2376 return 1;
2377
2378 /* If expectations overlap in no more than a single size, all is fine. */
2379 g0 = operand_type_and (t0, t1);
2380 if (g0.bitfield.byte
2381 + g0.bitfield.word
2382 + g0.bitfield.dword
2383 + g0.bitfield.qword
2384 + g0.bitfield.xmmword
2385 + g0.bitfield.ymmword
2386 + g0.bitfield.zmmword <= 1)
2387 return 1;
2388
2389 i.error = register_type_mismatch;
2390
2391 return 0;
2392 }
2393
2394 static INLINE unsigned int
2395 register_number (const reg_entry *r)
2396 {
2397 unsigned int nr = r->reg_num;
2398
2399 if (r->reg_flags & RegRex)
2400 nr += 8;
2401
2402 if (r->reg_flags & RegVRex)
2403 nr += 16;
2404
2405 return nr;
2406 }
2407
2408 static INLINE unsigned int
2409 mode_from_disp_size (i386_operand_type t)
2410 {
2411 if (t.bitfield.disp8)
2412 return 1;
2413 else if (t.bitfield.disp16
2414 || t.bitfield.disp32
2415 || t.bitfield.disp32s)
2416 return 2;
2417 else
2418 return 0;
2419 }
2420
2421 static INLINE int
2422 fits_in_signed_byte (addressT num)
2423 {
2424 return num + 0x80 <= 0xff;
2425 }
2426
2427 static INLINE int
2428 fits_in_unsigned_byte (addressT num)
2429 {
2430 return num <= 0xff;
2431 }
2432
2433 static INLINE int
2434 fits_in_unsigned_word (addressT num)
2435 {
2436 return num <= 0xffff;
2437 }
2438
2439 static INLINE int
2440 fits_in_signed_word (addressT num)
2441 {
2442 return num + 0x8000 <= 0xffff;
2443 }
2444
2445 static INLINE int
2446 fits_in_signed_long (addressT num ATTRIBUTE_UNUSED)
2447 {
2448 #ifndef BFD64
2449 return 1;
2450 #else
2451 return num + 0x80000000 <= 0xffffffff;
2452 #endif
2453 } /* fits_in_signed_long() */
2454
2455 static INLINE int
2456 fits_in_unsigned_long (addressT num ATTRIBUTE_UNUSED)
2457 {
2458 #ifndef BFD64
2459 return 1;
2460 #else
2461 return num <= 0xffffffff;
2462 #endif
2463 } /* fits_in_unsigned_long() */
2464
2465 static INLINE valueT extend_to_32bit_address (addressT num)
2466 {
2467 #ifdef BFD64
2468 if (fits_in_unsigned_long(num))
2469 return (num ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
2470
2471 if (!fits_in_signed_long (num))
2472 return num & 0xffffffff;
2473 #endif
2474
2475 return num;
2476 }
2477
2478 static INLINE int
2479 fits_in_disp8 (offsetT num)
2480 {
2481 int shift = i.memshift;
2482 unsigned int mask;
2483
2484 if (shift == -1)
2485 abort ();
2486
2487 mask = (1 << shift) - 1;
2488
2489 /* Return 0 if NUM isn't properly aligned. */
2490 if ((num & mask))
2491 return 0;
2492
2493 /* Check if NUM will fit in 8bit after shift. */
2494 return fits_in_signed_byte (num >> shift);
2495 }
2496
2497 static INLINE int
2498 fits_in_imm4 (offsetT num)
2499 {
2500 return (num & 0xf) == num;
2501 }
2502
2503 static i386_operand_type
2504 smallest_imm_type (offsetT num)
2505 {
2506 i386_operand_type t;
2507
2508 operand_type_set (&t, 0);
2509 t.bitfield.imm64 = 1;
2510
2511 if (cpu_arch_tune != PROCESSOR_I486 && num == 1)
2512 {
2513 /* This code is disabled on the 486 because all the Imm1 forms
2514 in the opcode table are slower on the i486. They're the
2515 versions with the implicitly specified single-position
2516 displacement, which has another syntax if you really want to
2517 use that form. */
2518 t.bitfield.imm1 = 1;
2519 t.bitfield.imm8 = 1;
2520 t.bitfield.imm8s = 1;
2521 t.bitfield.imm16 = 1;
2522 t.bitfield.imm32 = 1;
2523 t.bitfield.imm32s = 1;
2524 }
2525 else if (fits_in_signed_byte (num))
2526 {
2527 t.bitfield.imm8 = 1;
2528 t.bitfield.imm8s = 1;
2529 t.bitfield.imm16 = 1;
2530 t.bitfield.imm32 = 1;
2531 t.bitfield.imm32s = 1;
2532 }
2533 else if (fits_in_unsigned_byte (num))
2534 {
2535 t.bitfield.imm8 = 1;
2536 t.bitfield.imm16 = 1;
2537 t.bitfield.imm32 = 1;
2538 t.bitfield.imm32s = 1;
2539 }
2540 else if (fits_in_signed_word (num) || fits_in_unsigned_word (num))
2541 {
2542 t.bitfield.imm16 = 1;
2543 t.bitfield.imm32 = 1;
2544 t.bitfield.imm32s = 1;
2545 }
2546 else if (fits_in_signed_long (num))
2547 {
2548 t.bitfield.imm32 = 1;
2549 t.bitfield.imm32s = 1;
2550 }
2551 else if (fits_in_unsigned_long (num))
2552 t.bitfield.imm32 = 1;
2553
2554 return t;
2555 }
2556
2557 static offsetT
2558 offset_in_range (offsetT val, int size)
2559 {
2560 addressT mask;
2561
2562 switch (size)
2563 {
2564 case 1: mask = ((addressT) 1 << 8) - 1; break;
2565 case 2: mask = ((addressT) 1 << 16) - 1; break;
2566 #ifdef BFD64
2567 case 4: mask = ((addressT) 1 << 32) - 1; break;
2568 #endif
2569 case sizeof (val): return val;
2570 default: abort ();
2571 }
2572
2573 if ((val & ~mask) != 0 && (-val & ~mask) != 0)
2574 {
2575 char val_buf[128];
2576 char masked_buf[128];
2577
2578 /* Coded this way in order to ease translation. */
2579 sprintf_vma (val_buf, val);
2580 sprintf_vma (masked_buf, val & mask);
2581 as_warn (_("0x%s shortened to 0x%s"), val_buf, masked_buf);
2582 }
2583
2584 return val & mask;
2585 }
2586
2587 enum PREFIX_GROUP
2588 {
2589 PREFIX_EXIST = 0,
2590 PREFIX_LOCK,
2591 PREFIX_REP,
2592 PREFIX_DS,
2593 PREFIX_OTHER
2594 };
2595
2596 /* Returns
2597 a. PREFIX_EXIST if attempting to add a prefix where one from the
2598 same class already exists.
2599 b. PREFIX_LOCK if lock prefix is added.
2600 c. PREFIX_REP if rep/repne prefix is added.
2601 d. PREFIX_DS if ds prefix is added.
2602 e. PREFIX_OTHER if other prefix is added.
2603 */
2604
2605 static enum PREFIX_GROUP
2606 add_prefix (unsigned int prefix)
2607 {
2608 enum PREFIX_GROUP ret = PREFIX_OTHER;
2609 unsigned int q;
2610
2611 if (prefix >= REX_OPCODE && prefix < REX_OPCODE + 16
2612 && flag_code == CODE_64BIT)
2613 {
2614 if ((i.prefix[REX_PREFIX] & prefix & REX_W)
2615 || (i.prefix[REX_PREFIX] & prefix & REX_R)
2616 || (i.prefix[REX_PREFIX] & prefix & REX_X)
2617 || (i.prefix[REX_PREFIX] & prefix & REX_B))
2618 ret = PREFIX_EXIST;
2619 q = REX_PREFIX;
2620 }
2621 else
2622 {
2623 switch (prefix)
2624 {
2625 default:
2626 abort ();
2627
2628 case DS_PREFIX_OPCODE:
2629 ret = PREFIX_DS;
2630 /* Fall through. */
2631 case CS_PREFIX_OPCODE:
2632 case ES_PREFIX_OPCODE:
2633 case FS_PREFIX_OPCODE:
2634 case GS_PREFIX_OPCODE:
2635 case SS_PREFIX_OPCODE:
2636 q = SEG_PREFIX;
2637 break;
2638
2639 case REPNE_PREFIX_OPCODE:
2640 case REPE_PREFIX_OPCODE:
2641 q = REP_PREFIX;
2642 ret = PREFIX_REP;
2643 break;
2644
2645 case LOCK_PREFIX_OPCODE:
2646 q = LOCK_PREFIX;
2647 ret = PREFIX_LOCK;
2648 break;
2649
2650 case FWAIT_OPCODE:
2651 q = WAIT_PREFIX;
2652 break;
2653
2654 case ADDR_PREFIX_OPCODE:
2655 q = ADDR_PREFIX;
2656 break;
2657
2658 case DATA_PREFIX_OPCODE:
2659 q = DATA_PREFIX;
2660 break;
2661 }
2662 if (i.prefix[q] != 0)
2663 ret = PREFIX_EXIST;
2664 }
2665
2666 if (ret)
2667 {
2668 if (!i.prefix[q])
2669 ++i.prefixes;
2670 i.prefix[q] |= prefix;
2671 }
2672 else
2673 as_bad (_("same type of prefix used twice"));
2674
2675 return ret;
2676 }
2677
2678 static void
2679 update_code_flag (int value, int check)
2680 {
2681 PRINTF_LIKE ((*as_error));
2682
2683 flag_code = (enum flag_code) value;
2684 if (flag_code == CODE_64BIT)
2685 {
2686 cpu_arch_flags.bitfield.cpu64 = 1;
2687 cpu_arch_flags.bitfield.cpuno64 = 0;
2688 }
2689 else
2690 {
2691 cpu_arch_flags.bitfield.cpu64 = 0;
2692 cpu_arch_flags.bitfield.cpuno64 = 1;
2693 }
2694 if (value == CODE_64BIT && !cpu_arch_flags.bitfield.cpulm )
2695 {
2696 if (check)
2697 as_error = as_fatal;
2698 else
2699 as_error = as_bad;
2700 (*as_error) (_("64bit mode not supported on `%s'."),
2701 cpu_arch_name ? cpu_arch_name : default_arch);
2702 }
2703 if (value == CODE_32BIT && !cpu_arch_flags.bitfield.cpui386)
2704 {
2705 if (check)
2706 as_error = as_fatal;
2707 else
2708 as_error = as_bad;
2709 (*as_error) (_("32bit mode not supported on `%s'."),
2710 cpu_arch_name ? cpu_arch_name : default_arch);
2711 }
2712 stackop_size = '\0';
2713 }
2714
2715 static void
2716 set_code_flag (int value)
2717 {
2718 update_code_flag (value, 0);
2719 }
2720
2721 static void
2722 set_16bit_gcc_code_flag (int new_code_flag)
2723 {
2724 flag_code = (enum flag_code) new_code_flag;
2725 if (flag_code != CODE_16BIT)
2726 abort ();
2727 cpu_arch_flags.bitfield.cpu64 = 0;
2728 cpu_arch_flags.bitfield.cpuno64 = 1;
2729 stackop_size = LONG_MNEM_SUFFIX;
2730 }
2731
2732 static void
2733 set_intel_syntax (int syntax_flag)
2734 {
2735 /* Find out if register prefixing is specified. */
2736 int ask_naked_reg = 0;
2737
2738 SKIP_WHITESPACE ();
2739 if (!is_end_of_line[(unsigned char) *input_line_pointer])
2740 {
2741 char *string;
2742 int e = get_symbol_name (&string);
2743
2744 if (strcmp (string, "prefix") == 0)
2745 ask_naked_reg = 1;
2746 else if (strcmp (string, "noprefix") == 0)
2747 ask_naked_reg = -1;
2748 else
2749 as_bad (_("bad argument to syntax directive."));
2750 (void) restore_line_pointer (e);
2751 }
2752 demand_empty_rest_of_line ();
2753
2754 intel_syntax = syntax_flag;
2755
2756 if (ask_naked_reg == 0)
2757 allow_naked_reg = (intel_syntax
2758 && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
2759 else
2760 allow_naked_reg = (ask_naked_reg < 0);
2761
2762 expr_set_rank (O_full_ptr, syntax_flag ? 10 : 0);
2763
2764 identifier_chars['%'] = intel_syntax && allow_naked_reg ? '%' : 0;
2765 identifier_chars['$'] = intel_syntax ? '$' : 0;
2766 register_prefix = allow_naked_reg ? "" : "%";
2767 }
2768
2769 static void
2770 set_intel_mnemonic (int mnemonic_flag)
2771 {
2772 intel_mnemonic = mnemonic_flag;
2773 }
2774
2775 static void
2776 set_allow_index_reg (int flag)
2777 {
2778 allow_index_reg = flag;
2779 }
2780
2781 static void
2782 set_check (int what)
2783 {
2784 enum check_kind *kind;
2785 const char *str;
2786
2787 if (what)
2788 {
2789 kind = &operand_check;
2790 str = "operand";
2791 }
2792 else
2793 {
2794 kind = &sse_check;
2795 str = "sse";
2796 }
2797
2798 SKIP_WHITESPACE ();
2799
2800 if (!is_end_of_line[(unsigned char) *input_line_pointer])
2801 {
2802 char *string;
2803 int e = get_symbol_name (&string);
2804
2805 if (strcmp (string, "none") == 0)
2806 *kind = check_none;
2807 else if (strcmp (string, "warning") == 0)
2808 *kind = check_warning;
2809 else if (strcmp (string, "error") == 0)
2810 *kind = check_error;
2811 else
2812 as_bad (_("bad argument to %s_check directive."), str);
2813 (void) restore_line_pointer (e);
2814 }
2815 else
2816 as_bad (_("missing argument for %s_check directive"), str);
2817
2818 demand_empty_rest_of_line ();
2819 }
2820
2821 static void
2822 check_cpu_arch_compatible (const char *name ATTRIBUTE_UNUSED,
2823 i386_cpu_flags new_flag ATTRIBUTE_UNUSED)
2824 {
2825 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2826 static const char *arch;
2827
2828 /* Intel MCU is only supported on ELF. */
2829 if (!IS_ELF)
2830 return;
2831
2832 if (!arch)
2833 {
2834 /* Use cpu_arch_name if it is set in md_parse_option. Otherwise
2835 use default_arch. */
2836 arch = cpu_arch_name;
2837 if (!arch)
2838 arch = default_arch;
2839 }
2840
2841 /* If we are targeting Intel MCU, we must enable it. */
2842 if ((get_elf_backend_data (stdoutput)->elf_machine_code == EM_IAMCU)
2843 == new_flag.bitfield.cpuiamcu)
2844 return;
2845
2846 as_bad (_("`%s' is not supported on `%s'"), name, arch);
2847 #endif
2848 }
2849
2850 static void
2851 set_cpu_arch (int dummy ATTRIBUTE_UNUSED)
2852 {
2853 SKIP_WHITESPACE ();
2854
2855 if (!is_end_of_line[(unsigned char) *input_line_pointer])
2856 {
2857 char *string;
2858 int e = get_symbol_name (&string);
2859 unsigned int j;
2860 i386_cpu_flags flags;
2861
2862 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
2863 {
2864 if (strcmp (string, cpu_arch[j].name) == 0)
2865 {
2866 if (*string != '.')
2867 {
2868 check_cpu_arch_compatible (string, cpu_arch[j].flags);
2869
2870 cpu_arch_name = cpu_arch[j].name;
2871 cpu_sub_arch_name = NULL;
2872 cpu_arch_flags = cpu_arch[j].flags;
2873 if (flag_code == CODE_64BIT)
2874 {
2875 cpu_arch_flags.bitfield.cpu64 = 1;
2876 cpu_arch_flags.bitfield.cpuno64 = 0;
2877 }
2878 else
2879 {
2880 cpu_arch_flags.bitfield.cpu64 = 0;
2881 cpu_arch_flags.bitfield.cpuno64 = 1;
2882 }
2883 cpu_arch_isa = cpu_arch[j].type;
2884 cpu_arch_isa_flags = cpu_arch[j].flags;
2885 if (!cpu_arch_tune_set)
2886 {
2887 cpu_arch_tune = cpu_arch_isa;
2888 cpu_arch_tune_flags = cpu_arch_isa_flags;
2889 }
2890 break;
2891 }
2892
2893 flags = cpu_flags_or (cpu_arch_flags,
2894 cpu_arch[j].flags);
2895
2896 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
2897 {
2898 if (cpu_sub_arch_name)
2899 {
2900 char *name = cpu_sub_arch_name;
2901 cpu_sub_arch_name = concat (name,
2902 cpu_arch[j].name,
2903 (const char *) NULL);
2904 free (name);
2905 }
2906 else
2907 cpu_sub_arch_name = xstrdup (cpu_arch[j].name);
2908 cpu_arch_flags = flags;
2909 cpu_arch_isa_flags = flags;
2910 }
2911 else
2912 cpu_arch_isa_flags
2913 = cpu_flags_or (cpu_arch_isa_flags,
2914 cpu_arch[j].flags);
2915 (void) restore_line_pointer (e);
2916 demand_empty_rest_of_line ();
2917 return;
2918 }
2919 }
2920
2921 if (*string == '.' && j >= ARRAY_SIZE (cpu_arch))
2922 {
2923 /* Disable an ISA extension. */
2924 for (j = 0; j < ARRAY_SIZE (cpu_noarch); j++)
2925 if (strcmp (string + 1, cpu_noarch [j].name) == 0)
2926 {
2927 flags = cpu_flags_and_not (cpu_arch_flags,
2928 cpu_noarch[j].flags);
2929 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
2930 {
2931 if (cpu_sub_arch_name)
2932 {
2933 char *name = cpu_sub_arch_name;
2934 cpu_sub_arch_name = concat (name, string,
2935 (const char *) NULL);
2936 free (name);
2937 }
2938 else
2939 cpu_sub_arch_name = xstrdup (string);
2940 cpu_arch_flags = flags;
2941 cpu_arch_isa_flags = flags;
2942 }
2943 (void) restore_line_pointer (e);
2944 demand_empty_rest_of_line ();
2945 return;
2946 }
2947
2948 j = ARRAY_SIZE (cpu_arch);
2949 }
2950
2951 if (j >= ARRAY_SIZE (cpu_arch))
2952 as_bad (_("no such architecture: `%s'"), string);
2953
2954 *input_line_pointer = e;
2955 }
2956 else
2957 as_bad (_("missing cpu architecture"));
2958
2959 no_cond_jump_promotion = 0;
2960 if (*input_line_pointer == ','
2961 && !is_end_of_line[(unsigned char) input_line_pointer[1]])
2962 {
2963 char *string;
2964 char e;
2965
2966 ++input_line_pointer;
2967 e = get_symbol_name (&string);
2968
2969 if (strcmp (string, "nojumps") == 0)
2970 no_cond_jump_promotion = 1;
2971 else if (strcmp (string, "jumps") == 0)
2972 ;
2973 else
2974 as_bad (_("no such architecture modifier: `%s'"), string);
2975
2976 (void) restore_line_pointer (e);
2977 }
2978
2979 demand_empty_rest_of_line ();
2980 }
2981
2982 enum bfd_architecture
2983 i386_arch (void)
2984 {
2985 if (cpu_arch_isa == PROCESSOR_IAMCU)
2986 {
2987 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2988 || flag_code == CODE_64BIT)
2989 as_fatal (_("Intel MCU is 32bit ELF only"));
2990 return bfd_arch_iamcu;
2991 }
2992 else
2993 return bfd_arch_i386;
2994 }
2995
2996 unsigned long
2997 i386_mach (void)
2998 {
2999 if (startswith (default_arch, "x86_64"))
3000 {
3001 if (default_arch[6] == '\0')
3002 return bfd_mach_x86_64;
3003 else
3004 return bfd_mach_x64_32;
3005 }
3006 else if (!strcmp (default_arch, "i386")
3007 || !strcmp (default_arch, "iamcu"))
3008 {
3009 if (cpu_arch_isa == PROCESSOR_IAMCU)
3010 {
3011 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
3012 as_fatal (_("Intel MCU is 32bit ELF only"));
3013 return bfd_mach_i386_iamcu;
3014 }
3015 else
3016 return bfd_mach_i386_i386;
3017 }
3018 else
3019 as_fatal (_("unknown architecture"));
3020 }
3021 \f
3022 void
3023 md_begin (void)
3024 {
3025 /* Support pseudo prefixes like {disp32}. */
3026 lex_type ['{'] = LEX_BEGIN_NAME;
3027
3028 /* Initialize op_hash hash table. */
3029 op_hash = str_htab_create ();
3030
3031 {
3032 const insn_template *optab;
3033 templates *core_optab;
3034
3035 /* Setup for loop. */
3036 optab = i386_optab;
3037 core_optab = XNEW (templates);
3038 core_optab->start = optab;
3039
3040 while (1)
3041 {
3042 ++optab;
3043 if (optab->name == NULL
3044 || strcmp (optab->name, (optab - 1)->name) != 0)
3045 {
3046 /* different name --> ship out current template list;
3047 add to hash table; & begin anew. */
3048 core_optab->end = optab;
3049 if (str_hash_insert (op_hash, (optab - 1)->name, core_optab, 0))
3050 as_fatal (_("duplicate %s"), (optab - 1)->name);
3051
3052 if (optab->name == NULL)
3053 break;
3054 core_optab = XNEW (templates);
3055 core_optab->start = optab;
3056 }
3057 }
3058 }
3059
3060 /* Initialize reg_hash hash table. */
3061 reg_hash = str_htab_create ();
3062 {
3063 const reg_entry *regtab;
3064 unsigned int regtab_size = i386_regtab_size;
3065
3066 for (regtab = i386_regtab; regtab_size--; regtab++)
3067 {
3068 switch (regtab->reg_type.bitfield.class)
3069 {
3070 case Reg:
3071 if (regtab->reg_type.bitfield.dword)
3072 {
3073 if (regtab->reg_type.bitfield.instance == Accum)
3074 reg_eax = regtab;
3075 }
3076 else if (regtab->reg_type.bitfield.tbyte)
3077 {
3078 /* There's no point inserting st(<N>) in the hash table, as
3079 parentheses aren't included in register_chars[] anyway. */
3080 if (regtab->reg_type.bitfield.instance != Accum)
3081 continue;
3082 reg_st0 = regtab;
3083 }
3084 break;
3085
3086 case SReg:
3087 switch (regtab->reg_num)
3088 {
3089 case 0: reg_es = regtab; break;
3090 case 2: reg_ss = regtab; break;
3091 case 3: reg_ds = regtab; break;
3092 }
3093 break;
3094
3095 case RegMask:
3096 if (!regtab->reg_num)
3097 reg_k0 = regtab;
3098 break;
3099 }
3100
3101 if (str_hash_insert (reg_hash, regtab->reg_name, regtab, 0) != NULL)
3102 as_fatal (_("duplicate %s"), regtab->reg_name);
3103 }
3104 }
3105
3106 /* Fill in lexical tables: mnemonic_chars, operand_chars. */
3107 {
3108 int c;
3109 char *p;
3110
3111 for (c = 0; c < 256; c++)
3112 {
3113 if (ISDIGIT (c) || ISLOWER (c))
3114 {
3115 mnemonic_chars[c] = c;
3116 register_chars[c] = c;
3117 operand_chars[c] = c;
3118 }
3119 else if (ISUPPER (c))
3120 {
3121 mnemonic_chars[c] = TOLOWER (c);
3122 register_chars[c] = mnemonic_chars[c];
3123 operand_chars[c] = c;
3124 }
3125 else if (c == '{' || c == '}')
3126 {
3127 mnemonic_chars[c] = c;
3128 operand_chars[c] = c;
3129 }
3130 #ifdef SVR4_COMMENT_CHARS
3131 else if (c == '\\' && strchr (i386_comment_chars, '/'))
3132 operand_chars[c] = c;
3133 #endif
3134
3135 if (ISALPHA (c) || ISDIGIT (c))
3136 identifier_chars[c] = c;
3137 else if (c >= 128)
3138 {
3139 identifier_chars[c] = c;
3140 operand_chars[c] = c;
3141 }
3142 }
3143
3144 #ifdef LEX_AT
3145 identifier_chars['@'] = '@';
3146 #endif
3147 #ifdef LEX_QM
3148 identifier_chars['?'] = '?';
3149 operand_chars['?'] = '?';
3150 #endif
3151 mnemonic_chars['_'] = '_';
3152 mnemonic_chars['-'] = '-';
3153 mnemonic_chars['.'] = '.';
3154 identifier_chars['_'] = '_';
3155 identifier_chars['.'] = '.';
3156
3157 for (p = operand_special_chars; *p != '\0'; p++)
3158 operand_chars[(unsigned char) *p] = *p;
3159 }
3160
3161 if (flag_code == CODE_64BIT)
3162 {
3163 #if defined (OBJ_COFF) && defined (TE_PE)
3164 x86_dwarf2_return_column = (OUTPUT_FLAVOR == bfd_target_coff_flavour
3165 ? 32 : 16);
3166 #else
3167 x86_dwarf2_return_column = 16;
3168 #endif
3169 x86_cie_data_alignment = -8;
3170 }
3171 else
3172 {
3173 x86_dwarf2_return_column = 8;
3174 x86_cie_data_alignment = -4;
3175 }
3176
3177 /* NB: FUSED_JCC_PADDING frag must have sufficient room so that it
3178 can be turned into BRANCH_PREFIX frag. */
3179 if (align_branch_prefix_size > MAX_FUSED_JCC_PADDING_SIZE)
3180 abort ();
3181 }
3182
3183 void
3184 i386_print_statistics (FILE *file)
3185 {
3186 htab_print_statistics (file, "i386 opcode", op_hash);
3187 htab_print_statistics (file, "i386 register", reg_hash);
3188 }
3189 \f
3190 #ifdef DEBUG386
3191
3192 /* Debugging routines for md_assemble. */
3193 static void pte (insn_template *);
3194 static void pt (i386_operand_type);
3195 static void pe (expressionS *);
3196 static void ps (symbolS *);
3197
3198 static void
3199 pi (const char *line, i386_insn *x)
3200 {
3201 unsigned int j;
3202
3203 fprintf (stdout, "%s: template ", line);
3204 pte (&x->tm);
3205 fprintf (stdout, " address: base %s index %s scale %x\n",
3206 x->base_reg ? x->base_reg->reg_name : "none",
3207 x->index_reg ? x->index_reg->reg_name : "none",
3208 x->log2_scale_factor);
3209 fprintf (stdout, " modrm: mode %x reg %x reg/mem %x\n",
3210 x->rm.mode, x->rm.reg, x->rm.regmem);
3211 fprintf (stdout, " sib: base %x index %x scale %x\n",
3212 x->sib.base, x->sib.index, x->sib.scale);
3213 fprintf (stdout, " rex: 64bit %x extX %x extY %x extZ %x\n",
3214 (x->rex & REX_W) != 0,
3215 (x->rex & REX_R) != 0,
3216 (x->rex & REX_X) != 0,
3217 (x->rex & REX_B) != 0);
3218 for (j = 0; j < x->operands; j++)
3219 {
3220 fprintf (stdout, " #%d: ", j + 1);
3221 pt (x->types[j]);
3222 fprintf (stdout, "\n");
3223 if (x->types[j].bitfield.class == Reg
3224 || x->types[j].bitfield.class == RegMMX
3225 || x->types[j].bitfield.class == RegSIMD
3226 || x->types[j].bitfield.class == RegMask
3227 || x->types[j].bitfield.class == SReg
3228 || x->types[j].bitfield.class == RegCR
3229 || x->types[j].bitfield.class == RegDR
3230 || x->types[j].bitfield.class == RegTR
3231 || x->types[j].bitfield.class == RegBND)
3232 fprintf (stdout, "%s\n", x->op[j].regs->reg_name);
3233 if (operand_type_check (x->types[j], imm))
3234 pe (x->op[j].imms);
3235 if (operand_type_check (x->types[j], disp))
3236 pe (x->op[j].disps);
3237 }
3238 }
3239
3240 static void
3241 pte (insn_template *t)
3242 {
3243 static const unsigned char opc_pfx[] = { 0, 0x66, 0xf3, 0xf2 };
3244 static const char *const opc_spc[] = {
3245 NULL, "0f", "0f38", "0f3a", NULL, "evexmap5", "evexmap6", NULL,
3246 "XOP08", "XOP09", "XOP0A",
3247 };
3248 unsigned int j;
3249
3250 fprintf (stdout, " %d operands ", t->operands);
3251 if (opc_pfx[t->opcode_modifier.opcodeprefix])
3252 fprintf (stdout, "pfx %x ", opc_pfx[t->opcode_modifier.opcodeprefix]);
3253 if (opc_spc[t->opcode_modifier.opcodespace])
3254 fprintf (stdout, "space %s ", opc_spc[t->opcode_modifier.opcodespace]);
3255 fprintf (stdout, "opcode %x ", t->base_opcode);
3256 if (t->extension_opcode != None)
3257 fprintf (stdout, "ext %x ", t->extension_opcode);
3258 if (t->opcode_modifier.d)
3259 fprintf (stdout, "D");
3260 if (t->opcode_modifier.w)
3261 fprintf (stdout, "W");
3262 fprintf (stdout, "\n");
3263 for (j = 0; j < t->operands; j++)
3264 {
3265 fprintf (stdout, " #%d type ", j + 1);
3266 pt (t->operand_types[j]);
3267 fprintf (stdout, "\n");
3268 }
3269 }
3270
3271 static void
3272 pe (expressionS *e)
3273 {
3274 fprintf (stdout, " operation %d\n", e->X_op);
3275 fprintf (stdout, " add_number %" BFD_VMA_FMT "d (%" BFD_VMA_FMT "x)\n",
3276 e->X_add_number, e->X_add_number);
3277 if (e->X_add_symbol)
3278 {
3279 fprintf (stdout, " add_symbol ");
3280 ps (e->X_add_symbol);
3281 fprintf (stdout, "\n");
3282 }
3283 if (e->X_op_symbol)
3284 {
3285 fprintf (stdout, " op_symbol ");
3286 ps (e->X_op_symbol);
3287 fprintf (stdout, "\n");
3288 }
3289 }
3290
3291 static void
3292 ps (symbolS *s)
3293 {
3294 fprintf (stdout, "%s type %s%s",
3295 S_GET_NAME (s),
3296 S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
3297 segment_name (S_GET_SEGMENT (s)));
3298 }
3299
3300 static struct type_name
3301 {
3302 i386_operand_type mask;
3303 const char *name;
3304 }
3305 const type_names[] =
3306 {
3307 { OPERAND_TYPE_REG8, "r8" },
3308 { OPERAND_TYPE_REG16, "r16" },
3309 { OPERAND_TYPE_REG32, "r32" },
3310 { OPERAND_TYPE_REG64, "r64" },
3311 { OPERAND_TYPE_ACC8, "acc8" },
3312 { OPERAND_TYPE_ACC16, "acc16" },
3313 { OPERAND_TYPE_ACC32, "acc32" },
3314 { OPERAND_TYPE_ACC64, "acc64" },
3315 { OPERAND_TYPE_IMM8, "i8" },
3316 { OPERAND_TYPE_IMM8, "i8s" },
3317 { OPERAND_TYPE_IMM16, "i16" },
3318 { OPERAND_TYPE_IMM32, "i32" },
3319 { OPERAND_TYPE_IMM32S, "i32s" },
3320 { OPERAND_TYPE_IMM64, "i64" },
3321 { OPERAND_TYPE_IMM1, "i1" },
3322 { OPERAND_TYPE_BASEINDEX, "BaseIndex" },
3323 { OPERAND_TYPE_DISP8, "d8" },
3324 { OPERAND_TYPE_DISP16, "d16" },
3325 { OPERAND_TYPE_DISP32, "d32" },
3326 { OPERAND_TYPE_DISP32S, "d32s" },
3327 { OPERAND_TYPE_DISP64, "d64" },
3328 { OPERAND_TYPE_INOUTPORTREG, "InOutPortReg" },
3329 { OPERAND_TYPE_SHIFTCOUNT, "ShiftCount" },
3330 { OPERAND_TYPE_CONTROL, "control reg" },
3331 { OPERAND_TYPE_TEST, "test reg" },
3332 { OPERAND_TYPE_DEBUG, "debug reg" },
3333 { OPERAND_TYPE_FLOATREG, "FReg" },
3334 { OPERAND_TYPE_FLOATACC, "FAcc" },
3335 { OPERAND_TYPE_SREG, "SReg" },
3336 { OPERAND_TYPE_REGMMX, "rMMX" },
3337 { OPERAND_TYPE_REGXMM, "rXMM" },
3338 { OPERAND_TYPE_REGYMM, "rYMM" },
3339 { OPERAND_TYPE_REGZMM, "rZMM" },
3340 { OPERAND_TYPE_REGTMM, "rTMM" },
3341 { OPERAND_TYPE_REGMASK, "Mask reg" },
3342 };
3343
3344 static void
3345 pt (i386_operand_type t)
3346 {
3347 unsigned int j;
3348 i386_operand_type a;
3349
3350 for (j = 0; j < ARRAY_SIZE (type_names); j++)
3351 {
3352 a = operand_type_and (t, type_names[j].mask);
3353 if (operand_type_equal (&a, &type_names[j].mask))
3354 fprintf (stdout, "%s, ", type_names[j].name);
3355 }
3356 fflush (stdout);
3357 }
3358
3359 #endif /* DEBUG386 */
3360 \f
3361 static bfd_reloc_code_real_type
3362 reloc (unsigned int size,
3363 int pcrel,
3364 int sign,
3365 bfd_reloc_code_real_type other)
3366 {
3367 if (other != NO_RELOC)
3368 {
3369 reloc_howto_type *rel;
3370
3371 if (size == 8)
3372 switch (other)
3373 {
3374 case BFD_RELOC_X86_64_GOT32:
3375 return BFD_RELOC_X86_64_GOT64;
3376 break;
3377 case BFD_RELOC_X86_64_GOTPLT64:
3378 return BFD_RELOC_X86_64_GOTPLT64;
3379 break;
3380 case BFD_RELOC_X86_64_PLTOFF64:
3381 return BFD_RELOC_X86_64_PLTOFF64;
3382 break;
3383 case BFD_RELOC_X86_64_GOTPC32:
3384 other = BFD_RELOC_X86_64_GOTPC64;
3385 break;
3386 case BFD_RELOC_X86_64_GOTPCREL:
3387 other = BFD_RELOC_X86_64_GOTPCREL64;
3388 break;
3389 case BFD_RELOC_X86_64_TPOFF32:
3390 other = BFD_RELOC_X86_64_TPOFF64;
3391 break;
3392 case BFD_RELOC_X86_64_DTPOFF32:
3393 other = BFD_RELOC_X86_64_DTPOFF64;
3394 break;
3395 default:
3396 break;
3397 }
3398
3399 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3400 if (other == BFD_RELOC_SIZE32)
3401 {
3402 if (size == 8)
3403 other = BFD_RELOC_SIZE64;
3404 if (pcrel)
3405 {
3406 as_bad (_("there are no pc-relative size relocations"));
3407 return NO_RELOC;
3408 }
3409 }
3410 #endif
3411
3412 /* Sign-checking 4-byte relocations in 16-/32-bit code is pointless. */
3413 if (size == 4 && (flag_code != CODE_64BIT || disallow_64bit_reloc))
3414 sign = -1;
3415
3416 rel = bfd_reloc_type_lookup (stdoutput, other);
3417 if (!rel)
3418 as_bad (_("unknown relocation (%u)"), other);
3419 else if (size != bfd_get_reloc_size (rel))
3420 as_bad (_("%u-byte relocation cannot be applied to %u-byte field"),
3421 bfd_get_reloc_size (rel),
3422 size);
3423 else if (pcrel && !rel->pc_relative)
3424 as_bad (_("non-pc-relative relocation for pc-relative field"));
3425 else if ((rel->complain_on_overflow == complain_overflow_signed
3426 && !sign)
3427 || (rel->complain_on_overflow == complain_overflow_unsigned
3428 && sign > 0))
3429 as_bad (_("relocated field and relocation type differ in signedness"));
3430 else
3431 return other;
3432 return NO_RELOC;
3433 }
3434
3435 if (pcrel)
3436 {
3437 if (!sign)
3438 as_bad (_("there are no unsigned pc-relative relocations"));
3439 switch (size)
3440 {
3441 case 1: return BFD_RELOC_8_PCREL;
3442 case 2: return BFD_RELOC_16_PCREL;
3443 case 4: return BFD_RELOC_32_PCREL;
3444 case 8: return BFD_RELOC_64_PCREL;
3445 }
3446 as_bad (_("cannot do %u byte pc-relative relocation"), size);
3447 }
3448 else
3449 {
3450 if (sign > 0)
3451 switch (size)
3452 {
3453 case 4: return BFD_RELOC_X86_64_32S;
3454 }
3455 else
3456 switch (size)
3457 {
3458 case 1: return BFD_RELOC_8;
3459 case 2: return BFD_RELOC_16;
3460 case 4: return BFD_RELOC_32;
3461 case 8: return BFD_RELOC_64;
3462 }
3463 as_bad (_("cannot do %s %u byte relocation"),
3464 sign > 0 ? "signed" : "unsigned", size);
3465 }
3466
3467 return NO_RELOC;
3468 }
3469
3470 /* Here we decide which fixups can be adjusted to make them relative to
3471 the beginning of the section instead of the symbol. Basically we need
3472 to make sure that the dynamic relocations are done correctly, so in
3473 some cases we force the original symbol to be used. */
3474
3475 int
3476 tc_i386_fix_adjustable (fixS *fixP ATTRIBUTE_UNUSED)
3477 {
3478 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3479 if (!IS_ELF)
3480 return 1;
3481
3482 /* Don't adjust pc-relative references to merge sections in 64-bit
3483 mode. */
3484 if (use_rela_relocations
3485 && (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0
3486 && fixP->fx_pcrel)
3487 return 0;
3488
3489 /* The x86_64 GOTPCREL are represented as 32bit PCrel relocations
3490 and changed later by validate_fix. */
3491 if (GOT_symbol && fixP->fx_subsy == GOT_symbol
3492 && fixP->fx_r_type == BFD_RELOC_32_PCREL)
3493 return 0;
3494
3495 /* Adjust_reloc_syms doesn't know about the GOT. Need to keep symbol
3496 for size relocations. */
3497 if (fixP->fx_r_type == BFD_RELOC_SIZE32
3498 || fixP->fx_r_type == BFD_RELOC_SIZE64
3499 || fixP->fx_r_type == BFD_RELOC_386_GOTOFF
3500 || fixP->fx_r_type == BFD_RELOC_386_GOT32
3501 || fixP->fx_r_type == BFD_RELOC_386_GOT32X
3502 || fixP->fx_r_type == BFD_RELOC_386_TLS_GD
3503 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDM
3504 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDO_32
3505 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE_32
3506 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE
3507 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTIE
3508 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE_32
3509 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE
3510 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTDESC
3511 || fixP->fx_r_type == BFD_RELOC_386_TLS_DESC_CALL
3512 || fixP->fx_r_type == BFD_RELOC_X86_64_GOT32
3513 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCREL
3514 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCRELX
3515 || fixP->fx_r_type == BFD_RELOC_X86_64_REX_GOTPCRELX
3516 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSGD
3517 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSLD
3518 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF32
3519 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF64
3520 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF
3521 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF32
3522 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF64
3523 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTOFF64
3524 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
3525 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSDESC_CALL
3526 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3527 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3528 return 0;
3529 #endif
3530 return 1;
3531 }
3532
3533 static INLINE bool
3534 want_disp32 (const insn_template *t)
3535 {
3536 return flag_code != CODE_64BIT
3537 || i.prefix[ADDR_PREFIX]
3538 || (t->base_opcode == 0x8d
3539 && t->opcode_modifier.opcodespace == SPACE_BASE
3540 && (!i.types[1].bitfield.qword
3541 || t->opcode_modifier.size == SIZE32));
3542 }
3543
3544 static int
3545 intel_float_operand (const char *mnemonic)
3546 {
3547 /* Note that the value returned is meaningful only for opcodes with (memory)
3548 operands, hence the code here is free to improperly handle opcodes that
3549 have no operands (for better performance and smaller code). */
3550
3551 if (mnemonic[0] != 'f')
3552 return 0; /* non-math */
3553
3554 switch (mnemonic[1])
3555 {
3556 /* fclex, fdecstp, fdisi, femms, feni, fincstp, finit, fsetpm, and
3557 the fs segment override prefix not currently handled because no
3558 call path can make opcodes without operands get here */
3559 case 'i':
3560 return 2 /* integer op */;
3561 case 'l':
3562 if (mnemonic[2] == 'd' && (mnemonic[3] == 'c' || mnemonic[3] == 'e'))
3563 return 3; /* fldcw/fldenv */
3564 break;
3565 case 'n':
3566 if (mnemonic[2] != 'o' /* fnop */)
3567 return 3; /* non-waiting control op */
3568 break;
3569 case 'r':
3570 if (mnemonic[2] == 's')
3571 return 3; /* frstor/frstpm */
3572 break;
3573 case 's':
3574 if (mnemonic[2] == 'a')
3575 return 3; /* fsave */
3576 if (mnemonic[2] == 't')
3577 {
3578 switch (mnemonic[3])
3579 {
3580 case 'c': /* fstcw */
3581 case 'd': /* fstdw */
3582 case 'e': /* fstenv */
3583 case 's': /* fsts[gw] */
3584 return 3;
3585 }
3586 }
3587 break;
3588 case 'x':
3589 if (mnemonic[2] == 'r' || mnemonic[2] == 's')
3590 return 0; /* fxsave/fxrstor are not really math ops */
3591 break;
3592 }
3593
3594 return 1;
3595 }
3596
3597 static INLINE void
3598 install_template (const insn_template *t)
3599 {
3600 unsigned int l;
3601
3602 i.tm = *t;
3603
3604 /* Note that for pseudo prefixes this produces a length of 1. But for them
3605 the length isn't interesting at all. */
3606 for (l = 1; l < 4; ++l)
3607 if (!(t->base_opcode >> (8 * l)))
3608 break;
3609
3610 i.opcode_length = l;
3611 }
3612
3613 /* Build the VEX prefix. */
3614
3615 static void
3616 build_vex_prefix (const insn_template *t)
3617 {
3618 unsigned int register_specifier;
3619 unsigned int vector_length;
3620 unsigned int w;
3621
3622 /* Check register specifier. */
3623 if (i.vex.register_specifier)
3624 {
3625 register_specifier =
3626 ~register_number (i.vex.register_specifier) & 0xf;
3627 gas_assert ((i.vex.register_specifier->reg_flags & RegVRex) == 0);
3628 }
3629 else
3630 register_specifier = 0xf;
3631
3632 /* Use 2-byte VEX prefix by swapping destination and source operand
3633 if there are more than 1 register operand. */
3634 if (i.reg_operands > 1
3635 && i.vec_encoding != vex_encoding_vex3
3636 && i.dir_encoding == dir_encoding_default
3637 && i.operands == i.reg_operands
3638 && operand_type_equal (&i.types[0], &i.types[i.operands - 1])
3639 && i.tm.opcode_modifier.opcodespace == SPACE_0F
3640 && (i.tm.opcode_modifier.load || i.tm.opcode_modifier.d)
3641 && i.rex == REX_B)
3642 {
3643 unsigned int xchg = i.operands - 1;
3644 union i386_op temp_op;
3645 i386_operand_type temp_type;
3646
3647 temp_type = i.types[xchg];
3648 i.types[xchg] = i.types[0];
3649 i.types[0] = temp_type;
3650 temp_op = i.op[xchg];
3651 i.op[xchg] = i.op[0];
3652 i.op[0] = temp_op;
3653
3654 gas_assert (i.rm.mode == 3);
3655
3656 i.rex = REX_R;
3657 xchg = i.rm.regmem;
3658 i.rm.regmem = i.rm.reg;
3659 i.rm.reg = xchg;
3660
3661 if (i.tm.opcode_modifier.d)
3662 i.tm.base_opcode ^= (i.tm.base_opcode & 0xee) != 0x6e
3663 ? Opcode_SIMD_FloatD : Opcode_SIMD_IntD;
3664 else /* Use the next insn. */
3665 install_template (&t[1]);
3666 }
3667
3668 /* Use 2-byte VEX prefix by swapping commutative source operands if there
3669 are no memory operands and at least 3 register ones. */
3670 if (i.reg_operands >= 3
3671 && i.vec_encoding != vex_encoding_vex3
3672 && i.reg_operands == i.operands - i.imm_operands
3673 && i.tm.opcode_modifier.vex
3674 && i.tm.opcode_modifier.commutative
3675 && (i.tm.opcode_modifier.sse2avx || optimize > 1)
3676 && i.rex == REX_B
3677 && i.vex.register_specifier
3678 && !(i.vex.register_specifier->reg_flags & RegRex))
3679 {
3680 unsigned int xchg = i.operands - i.reg_operands;
3681 union i386_op temp_op;
3682 i386_operand_type temp_type;
3683
3684 gas_assert (i.tm.opcode_modifier.opcodespace == SPACE_0F);
3685 gas_assert (!i.tm.opcode_modifier.sae);
3686 gas_assert (operand_type_equal (&i.types[i.operands - 2],
3687 &i.types[i.operands - 3]));
3688 gas_assert (i.rm.mode == 3);
3689
3690 temp_type = i.types[xchg];
3691 i.types[xchg] = i.types[xchg + 1];
3692 i.types[xchg + 1] = temp_type;
3693 temp_op = i.op[xchg];
3694 i.op[xchg] = i.op[xchg + 1];
3695 i.op[xchg + 1] = temp_op;
3696
3697 i.rex = 0;
3698 xchg = i.rm.regmem | 8;
3699 i.rm.regmem = ~register_specifier & 0xf;
3700 gas_assert (!(i.rm.regmem & 8));
3701 i.vex.register_specifier += xchg - i.rm.regmem;
3702 register_specifier = ~xchg & 0xf;
3703 }
3704
3705 if (i.tm.opcode_modifier.vex == VEXScalar)
3706 vector_length = avxscalar;
3707 else if (i.tm.opcode_modifier.vex == VEX256)
3708 vector_length = 1;
3709 else
3710 {
3711 unsigned int op;
3712
3713 /* Determine vector length from the last multi-length vector
3714 operand. */
3715 vector_length = 0;
3716 for (op = t->operands; op--;)
3717 if (t->operand_types[op].bitfield.xmmword
3718 && t->operand_types[op].bitfield.ymmword
3719 && i.types[op].bitfield.ymmword)
3720 {
3721 vector_length = 1;
3722 break;
3723 }
3724 }
3725
3726 /* Check the REX.W bit and VEXW. */
3727 if (i.tm.opcode_modifier.vexw == VEXWIG)
3728 w = (vexwig == vexw1 || (i.rex & REX_W)) ? 1 : 0;
3729 else if (i.tm.opcode_modifier.vexw)
3730 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3731 else
3732 w = (flag_code == CODE_64BIT ? i.rex & REX_W : vexwig == vexw1) ? 1 : 0;
3733
3734 /* Use 2-byte VEX prefix if possible. */
3735 if (w == 0
3736 && i.vec_encoding != vex_encoding_vex3
3737 && i.tm.opcode_modifier.opcodespace == SPACE_0F
3738 && (i.rex & (REX_W | REX_X | REX_B)) == 0)
3739 {
3740 /* 2-byte VEX prefix. */
3741 unsigned int r;
3742
3743 i.vex.length = 2;
3744 i.vex.bytes[0] = 0xc5;
3745
3746 /* Check the REX.R bit. */
3747 r = (i.rex & REX_R) ? 0 : 1;
3748 i.vex.bytes[1] = (r << 7
3749 | register_specifier << 3
3750 | vector_length << 2
3751 | i.tm.opcode_modifier.opcodeprefix);
3752 }
3753 else
3754 {
3755 /* 3-byte VEX prefix. */
3756 i.vex.length = 3;
3757
3758 switch (i.tm.opcode_modifier.opcodespace)
3759 {
3760 case SPACE_0F:
3761 case SPACE_0F38:
3762 case SPACE_0F3A:
3763 i.vex.bytes[0] = 0xc4;
3764 break;
3765 case SPACE_XOP08:
3766 case SPACE_XOP09:
3767 case SPACE_XOP0A:
3768 i.vex.bytes[0] = 0x8f;
3769 break;
3770 default:
3771 abort ();
3772 }
3773
3774 /* The high 3 bits of the second VEX byte are 1's compliment
3775 of RXB bits from REX. */
3776 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | i.tm.opcode_modifier.opcodespace;
3777
3778 i.vex.bytes[2] = (w << 7
3779 | register_specifier << 3
3780 | vector_length << 2
3781 | i.tm.opcode_modifier.opcodeprefix);
3782 }
3783 }
3784
3785 static INLINE bool
3786 is_evex_encoding (const insn_template *t)
3787 {
3788 return t->opcode_modifier.evex || t->opcode_modifier.disp8memshift
3789 || t->opcode_modifier.broadcast || t->opcode_modifier.masking
3790 || t->opcode_modifier.sae;
3791 }
3792
3793 static INLINE bool
3794 is_any_vex_encoding (const insn_template *t)
3795 {
3796 return t->opcode_modifier.vex || is_evex_encoding (t);
3797 }
3798
3799 /* Build the EVEX prefix. */
3800
3801 static void
3802 build_evex_prefix (void)
3803 {
3804 unsigned int register_specifier, w;
3805 rex_byte vrex_used = 0;
3806
3807 /* Check register specifier. */
3808 if (i.vex.register_specifier)
3809 {
3810 gas_assert ((i.vrex & REX_X) == 0);
3811
3812 register_specifier = i.vex.register_specifier->reg_num;
3813 if ((i.vex.register_specifier->reg_flags & RegRex))
3814 register_specifier += 8;
3815 /* The upper 16 registers are encoded in the fourth byte of the
3816 EVEX prefix. */
3817 if (!(i.vex.register_specifier->reg_flags & RegVRex))
3818 i.vex.bytes[3] = 0x8;
3819 register_specifier = ~register_specifier & 0xf;
3820 }
3821 else
3822 {
3823 register_specifier = 0xf;
3824
3825 /* Encode upper 16 vector index register in the fourth byte of
3826 the EVEX prefix. */
3827 if (!(i.vrex & REX_X))
3828 i.vex.bytes[3] = 0x8;
3829 else
3830 vrex_used |= REX_X;
3831 }
3832
3833 /* 4 byte EVEX prefix. */
3834 i.vex.length = 4;
3835 i.vex.bytes[0] = 0x62;
3836
3837 /* The high 3 bits of the second EVEX byte are 1's compliment of RXB
3838 bits from REX. */
3839 gas_assert (i.tm.opcode_modifier.opcodespace >= SPACE_0F);
3840 gas_assert (i.tm.opcode_modifier.opcodespace <= SPACE_EVEXMAP6);
3841 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | i.tm.opcode_modifier.opcodespace;
3842
3843 /* The fifth bit of the second EVEX byte is 1's compliment of the
3844 REX_R bit in VREX. */
3845 if (!(i.vrex & REX_R))
3846 i.vex.bytes[1] |= 0x10;
3847 else
3848 vrex_used |= REX_R;
3849
3850 if ((i.reg_operands + i.imm_operands) == i.operands)
3851 {
3852 /* When all operands are registers, the REX_X bit in REX is not
3853 used. We reuse it to encode the upper 16 registers, which is
3854 indicated by the REX_B bit in VREX. The REX_X bit is encoded
3855 as 1's compliment. */
3856 if ((i.vrex & REX_B))
3857 {
3858 vrex_used |= REX_B;
3859 i.vex.bytes[1] &= ~0x40;
3860 }
3861 }
3862
3863 /* EVEX instructions shouldn't need the REX prefix. */
3864 i.vrex &= ~vrex_used;
3865 gas_assert (i.vrex == 0);
3866
3867 /* Check the REX.W bit and VEXW. */
3868 if (i.tm.opcode_modifier.vexw == VEXWIG)
3869 w = (evexwig == evexw1 || (i.rex & REX_W)) ? 1 : 0;
3870 else if (i.tm.opcode_modifier.vexw)
3871 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3872 else
3873 w = (flag_code == CODE_64BIT ? i.rex & REX_W : evexwig == evexw1) ? 1 : 0;
3874
3875 /* The third byte of the EVEX prefix. */
3876 i.vex.bytes[2] = ((w << 7)
3877 | (register_specifier << 3)
3878 | 4 /* Encode the U bit. */
3879 | i.tm.opcode_modifier.opcodeprefix);
3880
3881 /* The fourth byte of the EVEX prefix. */
3882 /* The zeroing-masking bit. */
3883 if (i.mask.reg && i.mask.zeroing)
3884 i.vex.bytes[3] |= 0x80;
3885
3886 /* Don't always set the broadcast bit if there is no RC. */
3887 if (i.rounding.type == rc_none)
3888 {
3889 /* Encode the vector length. */
3890 unsigned int vec_length;
3891
3892 if (!i.tm.opcode_modifier.evex
3893 || i.tm.opcode_modifier.evex == EVEXDYN)
3894 {
3895 unsigned int op;
3896
3897 /* Determine vector length from the last multi-length vector
3898 operand. */
3899 for (op = i.operands; op--;)
3900 if (i.tm.operand_types[op].bitfield.xmmword
3901 + i.tm.operand_types[op].bitfield.ymmword
3902 + i.tm.operand_types[op].bitfield.zmmword > 1)
3903 {
3904 if (i.types[op].bitfield.zmmword)
3905 {
3906 i.tm.opcode_modifier.evex = EVEX512;
3907 break;
3908 }
3909 else if (i.types[op].bitfield.ymmword)
3910 {
3911 i.tm.opcode_modifier.evex = EVEX256;
3912 break;
3913 }
3914 else if (i.types[op].bitfield.xmmword)
3915 {
3916 i.tm.opcode_modifier.evex = EVEX128;
3917 break;
3918 }
3919 else if (i.broadcast.type && op == i.broadcast.operand)
3920 {
3921 switch (i.broadcast.bytes)
3922 {
3923 case 64:
3924 i.tm.opcode_modifier.evex = EVEX512;
3925 break;
3926 case 32:
3927 i.tm.opcode_modifier.evex = EVEX256;
3928 break;
3929 case 16:
3930 i.tm.opcode_modifier.evex = EVEX128;
3931 break;
3932 default:
3933 abort ();
3934 }
3935 break;
3936 }
3937 }
3938
3939 if (op >= MAX_OPERANDS)
3940 abort ();
3941 }
3942
3943 switch (i.tm.opcode_modifier.evex)
3944 {
3945 case EVEXLIG: /* LL' is ignored */
3946 vec_length = evexlig << 5;
3947 break;
3948 case EVEX128:
3949 vec_length = 0 << 5;
3950 break;
3951 case EVEX256:
3952 vec_length = 1 << 5;
3953 break;
3954 case EVEX512:
3955 vec_length = 2 << 5;
3956 break;
3957 default:
3958 abort ();
3959 break;
3960 }
3961 i.vex.bytes[3] |= vec_length;
3962 /* Encode the broadcast bit. */
3963 if (i.broadcast.type)
3964 i.vex.bytes[3] |= 0x10;
3965 }
3966 else if (i.rounding.type != saeonly)
3967 i.vex.bytes[3] |= 0x10 | (i.rounding.type << 5);
3968 else
3969 i.vex.bytes[3] |= 0x10 | (evexrcig << 5);
3970
3971 if (i.mask.reg)
3972 i.vex.bytes[3] |= i.mask.reg->reg_num;
3973 }
3974
3975 static void
3976 process_immext (void)
3977 {
3978 expressionS *exp;
3979
3980 /* These AMD 3DNow! and SSE2 instructions have an opcode suffix
3981 which is coded in the same place as an 8-bit immediate field
3982 would be. Here we fake an 8-bit immediate operand from the
3983 opcode suffix stored in tm.extension_opcode.
3984
3985 AVX instructions also use this encoding, for some of
3986 3 argument instructions. */
3987
3988 gas_assert (i.imm_operands <= 1
3989 && (i.operands <= 2
3990 || (is_any_vex_encoding (&i.tm)
3991 && i.operands <= 4)));
3992
3993 exp = &im_expressions[i.imm_operands++];
3994 i.op[i.operands].imms = exp;
3995 i.types[i.operands] = imm8;
3996 i.operands++;
3997 exp->X_op = O_constant;
3998 exp->X_add_number = i.tm.extension_opcode;
3999 i.tm.extension_opcode = None;
4000 }
4001
4002
4003 static int
4004 check_hle (void)
4005 {
4006 switch (i.tm.opcode_modifier.prefixok)
4007 {
4008 default:
4009 abort ();
4010 case PrefixLock:
4011 case PrefixNone:
4012 case PrefixNoTrack:
4013 case PrefixRep:
4014 as_bad (_("invalid instruction `%s' after `%s'"),
4015 i.tm.name, i.hle_prefix);
4016 return 0;
4017 case PrefixHLELock:
4018 if (i.prefix[LOCK_PREFIX])
4019 return 1;
4020 as_bad (_("missing `lock' with `%s'"), i.hle_prefix);
4021 return 0;
4022 case PrefixHLEAny:
4023 return 1;
4024 case PrefixHLERelease:
4025 if (i.prefix[HLE_PREFIX] != XRELEASE_PREFIX_OPCODE)
4026 {
4027 as_bad (_("instruction `%s' after `xacquire' not allowed"),
4028 i.tm.name);
4029 return 0;
4030 }
4031 if (i.mem_operands == 0 || !(i.flags[i.operands - 1] & Operand_Mem))
4032 {
4033 as_bad (_("memory destination needed for instruction `%s'"
4034 " after `xrelease'"), i.tm.name);
4035 return 0;
4036 }
4037 return 1;
4038 }
4039 }
4040
4041 /* Encode aligned vector move as unaligned vector move. */
4042
4043 static void
4044 encode_with_unaligned_vector_move (void)
4045 {
4046 switch (i.tm.base_opcode)
4047 {
4048 case 0x28: /* Load instructions. */
4049 case 0x29: /* Store instructions. */
4050 /* movaps/movapd/vmovaps/vmovapd. */
4051 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4052 && i.tm.opcode_modifier.opcodeprefix <= PREFIX_0X66)
4053 i.tm.base_opcode = 0x10 | (i.tm.base_opcode & 1);
4054 break;
4055 case 0x6f: /* Load instructions. */
4056 case 0x7f: /* Store instructions. */
4057 /* movdqa/vmovdqa/vmovdqa64/vmovdqa32. */
4058 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4059 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0X66)
4060 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
4061 break;
4062 default:
4063 break;
4064 }
4065 }
4066
4067 /* Try the shortest encoding by shortening operand size. */
4068
4069 static void
4070 optimize_encoding (void)
4071 {
4072 unsigned int j;
4073
4074 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
4075 && i.tm.base_opcode == 0x8d)
4076 {
4077 /* Optimize: -O:
4078 lea symbol, %rN -> mov $symbol, %rN
4079 lea (%rM), %rN -> mov %rM, %rN
4080 lea (,%rM,1), %rN -> mov %rM, %rN
4081
4082 and in 32-bit mode for 16-bit addressing
4083
4084 lea (%rM), %rN -> movzx %rM, %rN
4085
4086 and in 64-bit mode zap 32-bit addressing in favor of using a
4087 32-bit (or less) destination.
4088 */
4089 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
4090 {
4091 if (!i.op[1].regs->reg_type.bitfield.word)
4092 i.tm.opcode_modifier.size = SIZE32;
4093 i.prefix[ADDR_PREFIX] = 0;
4094 }
4095
4096 if (!i.index_reg && !i.base_reg)
4097 {
4098 /* Handle:
4099 lea symbol, %rN -> mov $symbol, %rN
4100 */
4101 if (flag_code == CODE_64BIT)
4102 {
4103 /* Don't transform a relocation to a 16-bit one. */
4104 if (i.op[0].disps
4105 && i.op[0].disps->X_op != O_constant
4106 && i.op[1].regs->reg_type.bitfield.word)
4107 return;
4108
4109 if (!i.op[1].regs->reg_type.bitfield.qword
4110 || i.tm.opcode_modifier.size == SIZE32)
4111 {
4112 i.tm.base_opcode = 0xb8;
4113 i.tm.opcode_modifier.modrm = 0;
4114 if (!i.op[1].regs->reg_type.bitfield.word)
4115 i.types[0].bitfield.imm32 = 1;
4116 else
4117 {
4118 i.tm.opcode_modifier.size = SIZE16;
4119 i.types[0].bitfield.imm16 = 1;
4120 }
4121 }
4122 else
4123 {
4124 /* Subject to further optimization below. */
4125 i.tm.base_opcode = 0xc7;
4126 i.tm.extension_opcode = 0;
4127 i.types[0].bitfield.imm32s = 1;
4128 i.types[0].bitfield.baseindex = 0;
4129 }
4130 }
4131 /* Outside of 64-bit mode address and operand sizes have to match if
4132 a relocation is involved, as otherwise we wouldn't (currently) or
4133 even couldn't express the relocation correctly. */
4134 else if (i.op[0].disps
4135 && i.op[0].disps->X_op != O_constant
4136 && ((!i.prefix[ADDR_PREFIX])
4137 != (flag_code == CODE_32BIT
4138 ? i.op[1].regs->reg_type.bitfield.dword
4139 : i.op[1].regs->reg_type.bitfield.word)))
4140 return;
4141 /* In 16-bit mode converting LEA with 16-bit addressing and a 32-bit
4142 destination is going to grow encoding size. */
4143 else if (flag_code == CODE_16BIT
4144 && (optimize <= 1 || optimize_for_space)
4145 && !i.prefix[ADDR_PREFIX]
4146 && i.op[1].regs->reg_type.bitfield.dword)
4147 return;
4148 else
4149 {
4150 i.tm.base_opcode = 0xb8;
4151 i.tm.opcode_modifier.modrm = 0;
4152 if (i.op[1].regs->reg_type.bitfield.dword)
4153 i.types[0].bitfield.imm32 = 1;
4154 else
4155 i.types[0].bitfield.imm16 = 1;
4156
4157 if (i.op[0].disps
4158 && i.op[0].disps->X_op == O_constant
4159 && i.op[1].regs->reg_type.bitfield.dword
4160 /* NB: Add () to !i.prefix[ADDR_PREFIX] to silence
4161 GCC 5. */
4162 && (!i.prefix[ADDR_PREFIX]) != (flag_code == CODE_32BIT))
4163 i.op[0].disps->X_add_number &= 0xffff;
4164 }
4165
4166 i.tm.operand_types[0] = i.types[0];
4167 i.imm_operands = 1;
4168 if (!i.op[0].imms)
4169 {
4170 i.op[0].imms = &im_expressions[0];
4171 i.op[0].imms->X_op = O_absent;
4172 }
4173 }
4174 else if (i.op[0].disps
4175 && (i.op[0].disps->X_op != O_constant
4176 || i.op[0].disps->X_add_number))
4177 return;
4178 else
4179 {
4180 /* Handle:
4181 lea (%rM), %rN -> mov %rM, %rN
4182 lea (,%rM,1), %rN -> mov %rM, %rN
4183 lea (%rM), %rN -> movzx %rM, %rN
4184 */
4185 const reg_entry *addr_reg;
4186
4187 if (!i.index_reg && i.base_reg->reg_num != RegIP)
4188 addr_reg = i.base_reg;
4189 else if (!i.base_reg
4190 && i.index_reg->reg_num != RegIZ
4191 && !i.log2_scale_factor)
4192 addr_reg = i.index_reg;
4193 else
4194 return;
4195
4196 if (addr_reg->reg_type.bitfield.word
4197 && i.op[1].regs->reg_type.bitfield.dword)
4198 {
4199 if (flag_code != CODE_32BIT)
4200 return;
4201 i.tm.opcode_modifier.opcodespace = SPACE_0F;
4202 i.tm.base_opcode = 0xb7;
4203 }
4204 else
4205 i.tm.base_opcode = 0x8b;
4206
4207 if (addr_reg->reg_type.bitfield.dword
4208 && i.op[1].regs->reg_type.bitfield.qword)
4209 i.tm.opcode_modifier.size = SIZE32;
4210
4211 i.op[0].regs = addr_reg;
4212 i.reg_operands = 2;
4213 }
4214
4215 i.mem_operands = 0;
4216 i.disp_operands = 0;
4217 i.prefix[ADDR_PREFIX] = 0;
4218 i.prefix[SEG_PREFIX] = 0;
4219 i.seg[0] = NULL;
4220 }
4221
4222 if (optimize_for_space
4223 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
4224 && i.reg_operands == 1
4225 && i.imm_operands == 1
4226 && !i.types[1].bitfield.byte
4227 && i.op[0].imms->X_op == O_constant
4228 && fits_in_imm7 (i.op[0].imms->X_add_number)
4229 && (i.tm.base_opcode == 0xa8
4230 || (i.tm.base_opcode == 0xf6
4231 && i.tm.extension_opcode == 0x0)))
4232 {
4233 /* Optimize: -Os:
4234 test $imm7, %r64/%r32/%r16 -> test $imm7, %r8
4235 */
4236 unsigned int base_regnum = i.op[1].regs->reg_num;
4237 if (flag_code == CODE_64BIT || base_regnum < 4)
4238 {
4239 i.types[1].bitfield.byte = 1;
4240 /* Ignore the suffix. */
4241 i.suffix = 0;
4242 /* Convert to byte registers. */
4243 if (i.types[1].bitfield.word)
4244 j = 16;
4245 else if (i.types[1].bitfield.dword)
4246 j = 32;
4247 else
4248 j = 48;
4249 if (!(i.op[1].regs->reg_flags & RegRex) && base_regnum < 4)
4250 j += 8;
4251 i.op[1].regs -= j;
4252 }
4253 }
4254 else if (flag_code == CODE_64BIT
4255 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
4256 && ((i.types[1].bitfield.qword
4257 && i.reg_operands == 1
4258 && i.imm_operands == 1
4259 && i.op[0].imms->X_op == O_constant
4260 && ((i.tm.base_opcode == 0xb8
4261 && i.tm.extension_opcode == None
4262 && fits_in_unsigned_long (i.op[0].imms->X_add_number))
4263 || (fits_in_imm31 (i.op[0].imms->X_add_number)
4264 && ((i.tm.base_opcode == 0x24
4265 || i.tm.base_opcode == 0xa8)
4266 || (i.tm.base_opcode == 0x80
4267 && i.tm.extension_opcode == 0x4)
4268 || ((i.tm.base_opcode == 0xf6
4269 || (i.tm.base_opcode | 1) == 0xc7)
4270 && i.tm.extension_opcode == 0x0)))
4271 || (fits_in_imm7 (i.op[0].imms->X_add_number)
4272 && i.tm.base_opcode == 0x83
4273 && i.tm.extension_opcode == 0x4)))
4274 || (i.types[0].bitfield.qword
4275 && ((i.reg_operands == 2
4276 && i.op[0].regs == i.op[1].regs
4277 && (i.tm.base_opcode == 0x30
4278 || i.tm.base_opcode == 0x28))
4279 || (i.reg_operands == 1
4280 && i.operands == 1
4281 && i.tm.base_opcode == 0x30)))))
4282 {
4283 /* Optimize: -O:
4284 andq $imm31, %r64 -> andl $imm31, %r32
4285 andq $imm7, %r64 -> andl $imm7, %r32
4286 testq $imm31, %r64 -> testl $imm31, %r32
4287 xorq %r64, %r64 -> xorl %r32, %r32
4288 subq %r64, %r64 -> subl %r32, %r32
4289 movq $imm31, %r64 -> movl $imm31, %r32
4290 movq $imm32, %r64 -> movl $imm32, %r32
4291 */
4292 i.tm.opcode_modifier.norex64 = 1;
4293 if (i.tm.base_opcode == 0xb8 || (i.tm.base_opcode | 1) == 0xc7)
4294 {
4295 /* Handle
4296 movq $imm31, %r64 -> movl $imm31, %r32
4297 movq $imm32, %r64 -> movl $imm32, %r32
4298 */
4299 i.tm.operand_types[0].bitfield.imm32 = 1;
4300 i.tm.operand_types[0].bitfield.imm32s = 0;
4301 i.tm.operand_types[0].bitfield.imm64 = 0;
4302 i.types[0].bitfield.imm32 = 1;
4303 i.types[0].bitfield.imm32s = 0;
4304 i.types[0].bitfield.imm64 = 0;
4305 i.types[1].bitfield.dword = 1;
4306 i.types[1].bitfield.qword = 0;
4307 if ((i.tm.base_opcode | 1) == 0xc7)
4308 {
4309 /* Handle
4310 movq $imm31, %r64 -> movl $imm31, %r32
4311 */
4312 i.tm.base_opcode = 0xb8;
4313 i.tm.extension_opcode = None;
4314 i.tm.opcode_modifier.w = 0;
4315 i.tm.opcode_modifier.modrm = 0;
4316 }
4317 }
4318 }
4319 else if (optimize > 1
4320 && !optimize_for_space
4321 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
4322 && i.reg_operands == 2
4323 && i.op[0].regs == i.op[1].regs
4324 && ((i.tm.base_opcode & ~(Opcode_D | 1)) == 0x8
4325 || (i.tm.base_opcode & ~(Opcode_D | 1)) == 0x20)
4326 && (flag_code != CODE_64BIT || !i.types[0].bitfield.dword))
4327 {
4328 /* Optimize: -O2:
4329 andb %rN, %rN -> testb %rN, %rN
4330 andw %rN, %rN -> testw %rN, %rN
4331 andq %rN, %rN -> testq %rN, %rN
4332 orb %rN, %rN -> testb %rN, %rN
4333 orw %rN, %rN -> testw %rN, %rN
4334 orq %rN, %rN -> testq %rN, %rN
4335
4336 and outside of 64-bit mode
4337
4338 andl %rN, %rN -> testl %rN, %rN
4339 orl %rN, %rN -> testl %rN, %rN
4340 */
4341 i.tm.base_opcode = 0x84 | (i.tm.base_opcode & 1);
4342 }
4343 else if (i.reg_operands == 3
4344 && i.op[0].regs == i.op[1].regs
4345 && !i.types[2].bitfield.xmmword
4346 && (i.tm.opcode_modifier.vex
4347 || ((!i.mask.reg || i.mask.zeroing)
4348 && i.rounding.type == rc_none
4349 && is_evex_encoding (&i.tm)
4350 && (i.vec_encoding != vex_encoding_evex
4351 || cpu_arch_isa_flags.bitfield.cpuavx512vl
4352 || i.tm.cpu_flags.bitfield.cpuavx512vl
4353 || (i.tm.operand_types[2].bitfield.zmmword
4354 && i.types[2].bitfield.ymmword))))
4355 && ((i.tm.base_opcode == 0x55
4356 || i.tm.base_opcode == 0x57
4357 || i.tm.base_opcode == 0xdf
4358 || i.tm.base_opcode == 0xef
4359 || i.tm.base_opcode == 0xf8
4360 || i.tm.base_opcode == 0xf9
4361 || i.tm.base_opcode == 0xfa
4362 || i.tm.base_opcode == 0xfb
4363 || i.tm.base_opcode == 0x42
4364 || i.tm.base_opcode == 0x47)
4365 && i.tm.extension_opcode == None))
4366 {
4367 /* Optimize: -O1:
4368 VOP, one of vandnps, vandnpd, vxorps, vxorpd, vpsubb, vpsubd,
4369 vpsubq and vpsubw:
4370 EVEX VOP %zmmM, %zmmM, %zmmN
4371 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
4372 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4373 EVEX VOP %ymmM, %ymmM, %ymmN
4374 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
4375 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4376 VEX VOP %ymmM, %ymmM, %ymmN
4377 -> VEX VOP %xmmM, %xmmM, %xmmN
4378 VOP, one of vpandn and vpxor:
4379 VEX VOP %ymmM, %ymmM, %ymmN
4380 -> VEX VOP %xmmM, %xmmM, %xmmN
4381 VOP, one of vpandnd and vpandnq:
4382 EVEX VOP %zmmM, %zmmM, %zmmN
4383 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
4384 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4385 EVEX VOP %ymmM, %ymmM, %ymmN
4386 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
4387 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4388 VOP, one of vpxord and vpxorq:
4389 EVEX VOP %zmmM, %zmmM, %zmmN
4390 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
4391 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4392 EVEX VOP %ymmM, %ymmM, %ymmN
4393 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
4394 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4395 VOP, one of kxord and kxorq:
4396 VEX VOP %kM, %kM, %kN
4397 -> VEX kxorw %kM, %kM, %kN
4398 VOP, one of kandnd and kandnq:
4399 VEX VOP %kM, %kM, %kN
4400 -> VEX kandnw %kM, %kM, %kN
4401 */
4402 if (is_evex_encoding (&i.tm))
4403 {
4404 if (i.vec_encoding != vex_encoding_evex)
4405 {
4406 i.tm.opcode_modifier.vex = VEX128;
4407 i.tm.opcode_modifier.vexw = VEXW0;
4408 i.tm.opcode_modifier.evex = 0;
4409 }
4410 else if (optimize > 1)
4411 i.tm.opcode_modifier.evex = EVEX128;
4412 else
4413 return;
4414 }
4415 else if (i.tm.operand_types[0].bitfield.class == RegMask)
4416 {
4417 i.tm.opcode_modifier.opcodeprefix = PREFIX_NONE;
4418 i.tm.opcode_modifier.vexw = VEXW0;
4419 }
4420 else
4421 i.tm.opcode_modifier.vex = VEX128;
4422
4423 if (i.tm.opcode_modifier.vex)
4424 for (j = 0; j < 3; j++)
4425 {
4426 i.types[j].bitfield.xmmword = 1;
4427 i.types[j].bitfield.ymmword = 0;
4428 }
4429 }
4430 else if (i.vec_encoding != vex_encoding_evex
4431 && !i.types[0].bitfield.zmmword
4432 && !i.types[1].bitfield.zmmword
4433 && !i.mask.reg
4434 && !i.broadcast.type
4435 && is_evex_encoding (&i.tm)
4436 && ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4437 || (i.tm.base_opcode & ~4) == 0xdb
4438 || (i.tm.base_opcode & ~4) == 0xeb)
4439 && i.tm.extension_opcode == None)
4440 {
4441 /* Optimize: -O1:
4442 VOP, one of vmovdqa32, vmovdqa64, vmovdqu8, vmovdqu16,
4443 vmovdqu32 and vmovdqu64:
4444 EVEX VOP %xmmM, %xmmN
4445 -> VEX vmovdqa|vmovdqu %xmmM, %xmmN (M and N < 16)
4446 EVEX VOP %ymmM, %ymmN
4447 -> VEX vmovdqa|vmovdqu %ymmM, %ymmN (M and N < 16)
4448 EVEX VOP %xmmM, mem
4449 -> VEX vmovdqa|vmovdqu %xmmM, mem (M < 16)
4450 EVEX VOP %ymmM, mem
4451 -> VEX vmovdqa|vmovdqu %ymmM, mem (M < 16)
4452 EVEX VOP mem, %xmmN
4453 -> VEX mvmovdqa|vmovdquem, %xmmN (N < 16)
4454 EVEX VOP mem, %ymmN
4455 -> VEX vmovdqa|vmovdqu mem, %ymmN (N < 16)
4456 VOP, one of vpand, vpandn, vpor, vpxor:
4457 EVEX VOP{d,q} %xmmL, %xmmM, %xmmN
4458 -> VEX VOP %xmmL, %xmmM, %xmmN (L, M, and N < 16)
4459 EVEX VOP{d,q} %ymmL, %ymmM, %ymmN
4460 -> VEX VOP %ymmL, %ymmM, %ymmN (L, M, and N < 16)
4461 EVEX VOP{d,q} mem, %xmmM, %xmmN
4462 -> VEX VOP mem, %xmmM, %xmmN (M and N < 16)
4463 EVEX VOP{d,q} mem, %ymmM, %ymmN
4464 -> VEX VOP mem, %ymmM, %ymmN (M and N < 16)
4465 */
4466 for (j = 0; j < i.operands; j++)
4467 if (operand_type_check (i.types[j], disp)
4468 && i.op[j].disps->X_op == O_constant)
4469 {
4470 /* Since the VEX prefix has 2 or 3 bytes, the EVEX prefix
4471 has 4 bytes, EVEX Disp8 has 1 byte and VEX Disp32 has 4
4472 bytes, we choose EVEX Disp8 over VEX Disp32. */
4473 int evex_disp8, vex_disp8;
4474 unsigned int memshift = i.memshift;
4475 offsetT n = i.op[j].disps->X_add_number;
4476
4477 evex_disp8 = fits_in_disp8 (n);
4478 i.memshift = 0;
4479 vex_disp8 = fits_in_disp8 (n);
4480 if (evex_disp8 != vex_disp8)
4481 {
4482 i.memshift = memshift;
4483 return;
4484 }
4485
4486 i.types[j].bitfield.disp8 = vex_disp8;
4487 break;
4488 }
4489 if ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4490 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2)
4491 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
4492 i.tm.opcode_modifier.vex
4493 = i.types[0].bitfield.ymmword ? VEX256 : VEX128;
4494 i.tm.opcode_modifier.vexw = VEXW0;
4495 /* VPAND, VPOR, and VPXOR are commutative. */
4496 if (i.reg_operands == 3 && i.tm.base_opcode != 0xdf)
4497 i.tm.opcode_modifier.commutative = 1;
4498 i.tm.opcode_modifier.evex = 0;
4499 i.tm.opcode_modifier.masking = 0;
4500 i.tm.opcode_modifier.broadcast = 0;
4501 i.tm.opcode_modifier.disp8memshift = 0;
4502 i.memshift = 0;
4503 if (j < i.operands)
4504 i.types[j].bitfield.disp8
4505 = fits_in_disp8 (i.op[j].disps->X_add_number);
4506 }
4507 }
4508
4509 /* Return non-zero for load instruction. */
4510
4511 static int
4512 load_insn_p (void)
4513 {
4514 unsigned int dest;
4515 int any_vex_p = is_any_vex_encoding (&i.tm);
4516 unsigned int base_opcode = i.tm.base_opcode | 1;
4517
4518 if (!any_vex_p)
4519 {
4520 /* Anysize insns: lea, invlpg, clflush, prefetchnta, prefetcht0,
4521 prefetcht1, prefetcht2, prefetchtw, bndmk, bndcl, bndcu, bndcn,
4522 bndstx, bndldx, prefetchwt1, clflushopt, clwb, cldemote. */
4523 if (i.tm.opcode_modifier.anysize)
4524 return 0;
4525
4526 /* pop. */
4527 if (strcmp (i.tm.name, "pop") == 0)
4528 return 1;
4529 }
4530
4531 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE)
4532 {
4533 /* popf, popa. */
4534 if (i.tm.base_opcode == 0x9d
4535 || i.tm.base_opcode == 0x61)
4536 return 1;
4537
4538 /* movs, cmps, lods, scas. */
4539 if ((i.tm.base_opcode | 0xb) == 0xaf)
4540 return 1;
4541
4542 /* outs, xlatb. */
4543 if (base_opcode == 0x6f
4544 || i.tm.base_opcode == 0xd7)
4545 return 1;
4546 /* NB: For AMD-specific insns with implicit memory operands,
4547 they're intentionally not covered. */
4548 }
4549
4550 /* No memory operand. */
4551 if (!i.mem_operands)
4552 return 0;
4553
4554 if (any_vex_p)
4555 {
4556 /* vldmxcsr. */
4557 if (i.tm.base_opcode == 0xae
4558 && i.tm.opcode_modifier.vex
4559 && i.tm.opcode_modifier.opcodespace == SPACE_0F
4560 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
4561 && i.tm.extension_opcode == 2)
4562 return 1;
4563 }
4564 else if (i.tm.opcode_modifier.opcodespace == SPACE_BASE)
4565 {
4566 /* test, not, neg, mul, imul, div, idiv. */
4567 if ((i.tm.base_opcode == 0xf6 || i.tm.base_opcode == 0xf7)
4568 && i.tm.extension_opcode != 1)
4569 return 1;
4570
4571 /* inc, dec. */
4572 if (base_opcode == 0xff && i.tm.extension_opcode <= 1)
4573 return 1;
4574
4575 /* add, or, adc, sbb, and, sub, xor, cmp. */
4576 if (i.tm.base_opcode >= 0x80 && i.tm.base_opcode <= 0x83)
4577 return 1;
4578
4579 /* rol, ror, rcl, rcr, shl/sal, shr, sar. */
4580 if ((base_opcode == 0xc1
4581 || (i.tm.base_opcode >= 0xd0 && i.tm.base_opcode <= 0xd3))
4582 && i.tm.extension_opcode != 6)
4583 return 1;
4584
4585 /* Check for x87 instructions. */
4586 if (base_opcode >= 0xd8 && base_opcode <= 0xdf)
4587 {
4588 /* Skip fst, fstp, fstenv, fstcw. */
4589 if (i.tm.base_opcode == 0xd9
4590 && (i.tm.extension_opcode == 2
4591 || i.tm.extension_opcode == 3
4592 || i.tm.extension_opcode == 6
4593 || i.tm.extension_opcode == 7))
4594 return 0;
4595
4596 /* Skip fisttp, fist, fistp, fstp. */
4597 if (i.tm.base_opcode == 0xdb
4598 && (i.tm.extension_opcode == 1
4599 || i.tm.extension_opcode == 2
4600 || i.tm.extension_opcode == 3
4601 || i.tm.extension_opcode == 7))
4602 return 0;
4603
4604 /* Skip fisttp, fst, fstp, fsave, fstsw. */
4605 if (i.tm.base_opcode == 0xdd
4606 && (i.tm.extension_opcode == 1
4607 || i.tm.extension_opcode == 2
4608 || i.tm.extension_opcode == 3
4609 || i.tm.extension_opcode == 6
4610 || i.tm.extension_opcode == 7))
4611 return 0;
4612
4613 /* Skip fisttp, fist, fistp, fbstp, fistp. */
4614 if (i.tm.base_opcode == 0xdf
4615 && (i.tm.extension_opcode == 1
4616 || i.tm.extension_opcode == 2
4617 || i.tm.extension_opcode == 3
4618 || i.tm.extension_opcode == 6
4619 || i.tm.extension_opcode == 7))
4620 return 0;
4621
4622 return 1;
4623 }
4624 }
4625 else if (i.tm.opcode_modifier.opcodespace == SPACE_0F)
4626 {
4627 /* bt, bts, btr, btc. */
4628 if (i.tm.base_opcode == 0xba
4629 && (i.tm.extension_opcode >= 4 && i.tm.extension_opcode <= 7))
4630 return 1;
4631
4632 /* cmpxchg8b, cmpxchg16b, xrstors, vmptrld. */
4633 if (i.tm.base_opcode == 0xc7
4634 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
4635 && (i.tm.extension_opcode == 1 || i.tm.extension_opcode == 3
4636 || i.tm.extension_opcode == 6))
4637 return 1;
4638
4639 /* fxrstor, ldmxcsr, xrstor. */
4640 if (i.tm.base_opcode == 0xae
4641 && (i.tm.extension_opcode == 1
4642 || i.tm.extension_opcode == 2
4643 || i.tm.extension_opcode == 5))
4644 return 1;
4645
4646 /* lgdt, lidt, lmsw. */
4647 if (i.tm.base_opcode == 0x01
4648 && (i.tm.extension_opcode == 2
4649 || i.tm.extension_opcode == 3
4650 || i.tm.extension_opcode == 6))
4651 return 1;
4652 }
4653
4654 dest = i.operands - 1;
4655
4656 /* Check fake imm8 operand and 3 source operands. */
4657 if ((i.tm.opcode_modifier.immext
4658 || i.tm.opcode_modifier.vexsources == VEX3SOURCES)
4659 && i.types[dest].bitfield.imm8)
4660 dest--;
4661
4662 /* add, or, adc, sbb, and, sub, xor, cmp, test, xchg. */
4663 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
4664 && (base_opcode == 0x1
4665 || base_opcode == 0x9
4666 || base_opcode == 0x11
4667 || base_opcode == 0x19
4668 || base_opcode == 0x21
4669 || base_opcode == 0x29
4670 || base_opcode == 0x31
4671 || base_opcode == 0x39
4672 || (base_opcode | 2) == 0x87))
4673 return 1;
4674
4675 /* xadd. */
4676 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4677 && base_opcode == 0xc1)
4678 return 1;
4679
4680 /* Check for load instruction. */
4681 return (i.types[dest].bitfield.class != ClassNone
4682 || i.types[dest].bitfield.instance == Accum);
4683 }
4684
4685 /* Output lfence, 0xfaee8, after instruction. */
4686
4687 static void
4688 insert_lfence_after (void)
4689 {
4690 if (lfence_after_load && load_insn_p ())
4691 {
4692 /* There are also two REP string instructions that require
4693 special treatment. Specifically, the compare string (CMPS)
4694 and scan string (SCAS) instructions set EFLAGS in a manner
4695 that depends on the data being compared/scanned. When used
4696 with a REP prefix, the number of iterations may therefore
4697 vary depending on this data. If the data is a program secret
4698 chosen by the adversary using an LVI method,
4699 then this data-dependent behavior may leak some aspect
4700 of the secret. */
4701 if (((i.tm.base_opcode | 0x1) == 0xa7
4702 || (i.tm.base_opcode | 0x1) == 0xaf)
4703 && i.prefix[REP_PREFIX])
4704 {
4705 as_warn (_("`%s` changes flags which would affect control flow behavior"),
4706 i.tm.name);
4707 }
4708 char *p = frag_more (3);
4709 *p++ = 0xf;
4710 *p++ = 0xae;
4711 *p = 0xe8;
4712 }
4713 }
4714
4715 /* Output lfence, 0xfaee8, before instruction. */
4716
4717 static void
4718 insert_lfence_before (void)
4719 {
4720 char *p;
4721
4722 if (i.tm.opcode_modifier.opcodespace != SPACE_BASE)
4723 return;
4724
4725 if (i.tm.base_opcode == 0xff
4726 && (i.tm.extension_opcode == 2 || i.tm.extension_opcode == 4))
4727 {
4728 /* Insert lfence before indirect branch if needed. */
4729
4730 if (lfence_before_indirect_branch == lfence_branch_none)
4731 return;
4732
4733 if (i.operands != 1)
4734 abort ();
4735
4736 if (i.reg_operands == 1)
4737 {
4738 /* Indirect branch via register. Don't insert lfence with
4739 -mlfence-after-load=yes. */
4740 if (lfence_after_load
4741 || lfence_before_indirect_branch == lfence_branch_memory)
4742 return;
4743 }
4744 else if (i.mem_operands == 1
4745 && lfence_before_indirect_branch != lfence_branch_register)
4746 {
4747 as_warn (_("indirect `%s` with memory operand should be avoided"),
4748 i.tm.name);
4749 return;
4750 }
4751 else
4752 return;
4753
4754 if (last_insn.kind != last_insn_other
4755 && last_insn.seg == now_seg)
4756 {
4757 as_warn_where (last_insn.file, last_insn.line,
4758 _("`%s` skips -mlfence-before-indirect-branch on `%s`"),
4759 last_insn.name, i.tm.name);
4760 return;
4761 }
4762
4763 p = frag_more (3);
4764 *p++ = 0xf;
4765 *p++ = 0xae;
4766 *p = 0xe8;
4767 return;
4768 }
4769
4770 /* Output or/not/shl and lfence before near ret. */
4771 if (lfence_before_ret != lfence_before_ret_none
4772 && (i.tm.base_opcode == 0xc2
4773 || i.tm.base_opcode == 0xc3))
4774 {
4775 if (last_insn.kind != last_insn_other
4776 && last_insn.seg == now_seg)
4777 {
4778 as_warn_where (last_insn.file, last_insn.line,
4779 _("`%s` skips -mlfence-before-ret on `%s`"),
4780 last_insn.name, i.tm.name);
4781 return;
4782 }
4783
4784 /* Near ret ingore operand size override under CPU64. */
4785 char prefix = flag_code == CODE_64BIT
4786 ? 0x48
4787 : i.prefix[DATA_PREFIX] ? 0x66 : 0x0;
4788
4789 if (lfence_before_ret == lfence_before_ret_not)
4790 {
4791 /* not: 0xf71424, may add prefix
4792 for operand size override or 64-bit code. */
4793 p = frag_more ((prefix ? 2 : 0) + 6 + 3);
4794 if (prefix)
4795 *p++ = prefix;
4796 *p++ = 0xf7;
4797 *p++ = 0x14;
4798 *p++ = 0x24;
4799 if (prefix)
4800 *p++ = prefix;
4801 *p++ = 0xf7;
4802 *p++ = 0x14;
4803 *p++ = 0x24;
4804 }
4805 else
4806 {
4807 p = frag_more ((prefix ? 1 : 0) + 4 + 3);
4808 if (prefix)
4809 *p++ = prefix;
4810 if (lfence_before_ret == lfence_before_ret_or)
4811 {
4812 /* or: 0x830c2400, may add prefix
4813 for operand size override or 64-bit code. */
4814 *p++ = 0x83;
4815 *p++ = 0x0c;
4816 }
4817 else
4818 {
4819 /* shl: 0xc1242400, may add prefix
4820 for operand size override or 64-bit code. */
4821 *p++ = 0xc1;
4822 *p++ = 0x24;
4823 }
4824
4825 *p++ = 0x24;
4826 *p++ = 0x0;
4827 }
4828
4829 *p++ = 0xf;
4830 *p++ = 0xae;
4831 *p = 0xe8;
4832 }
4833 }
4834
4835 /* This is the guts of the machine-dependent assembler. LINE points to a
4836 machine dependent instruction. This function is supposed to emit
4837 the frags/bytes it assembles to. */
4838
4839 void
4840 md_assemble (char *line)
4841 {
4842 unsigned int j;
4843 char mnemonic[MAX_MNEM_SIZE], mnem_suffix;
4844 const insn_template *t;
4845
4846 /* Initialize globals. */
4847 memset (&i, '\0', sizeof (i));
4848 i.rounding.type = rc_none;
4849 for (j = 0; j < MAX_OPERANDS; j++)
4850 i.reloc[j] = NO_RELOC;
4851 memset (disp_expressions, '\0', sizeof (disp_expressions));
4852 memset (im_expressions, '\0', sizeof (im_expressions));
4853 save_stack_p = save_stack;
4854
4855 /* First parse an instruction mnemonic & call i386_operand for the operands.
4856 We assume that the scrubber has arranged it so that line[0] is the valid
4857 start of a (possibly prefixed) mnemonic. */
4858
4859 line = parse_insn (line, mnemonic);
4860 if (line == NULL)
4861 return;
4862 mnem_suffix = i.suffix;
4863
4864 line = parse_operands (line, mnemonic);
4865 this_operand = -1;
4866 xfree (i.memop1_string);
4867 i.memop1_string = NULL;
4868 if (line == NULL)
4869 return;
4870
4871 /* Now we've parsed the mnemonic into a set of templates, and have the
4872 operands at hand. */
4873
4874 /* All Intel opcodes have reversed operands except for "bound", "enter",
4875 "invlpg*", "monitor*", "mwait*", "tpause", "umwait", "pvalidate",
4876 "rmpadjust", and "rmpupdate". We also don't reverse intersegment "jmp"
4877 and "call" instructions with 2 immediate operands so that the immediate
4878 segment precedes the offset consistently in Intel and AT&T modes. */
4879 if (intel_syntax
4880 && i.operands > 1
4881 && (strcmp (mnemonic, "bound") != 0)
4882 && (strncmp (mnemonic, "invlpg", 6) != 0)
4883 && !startswith (mnemonic, "monitor")
4884 && !startswith (mnemonic, "mwait")
4885 && (strcmp (mnemonic, "pvalidate") != 0)
4886 && !startswith (mnemonic, "rmp")
4887 && (strcmp (mnemonic, "tpause") != 0)
4888 && (strcmp (mnemonic, "umwait") != 0)
4889 && !(operand_type_check (i.types[0], imm)
4890 && operand_type_check (i.types[1], imm)))
4891 swap_operands ();
4892
4893 /* The order of the immediates should be reversed
4894 for 2 immediates extrq and insertq instructions */
4895 if (i.imm_operands == 2
4896 && (strcmp (mnemonic, "extrq") == 0
4897 || strcmp (mnemonic, "insertq") == 0))
4898 swap_2_operands (0, 1);
4899
4900 if (i.imm_operands)
4901 optimize_imm ();
4902
4903 if (i.disp_operands && !want_disp32 (current_templates->start))
4904 {
4905 for (j = 0; j < i.operands; ++j)
4906 {
4907 const expressionS *exp = i.op[j].disps;
4908
4909 if (!operand_type_check (i.types[j], disp))
4910 continue;
4911
4912 if (exp->X_op != O_constant)
4913 continue;
4914
4915 /* Since displacement is signed extended to 64bit, don't allow
4916 disp32 and turn off disp32s if they are out of range. */
4917 i.types[j].bitfield.disp32 = 0;
4918 if (fits_in_signed_long (exp->X_add_number))
4919 continue;
4920
4921 i.types[j].bitfield.disp32s = 0;
4922 if (i.types[j].bitfield.baseindex)
4923 {
4924 char number_buf[128];
4925
4926 /* Coded this way in order to allow for ease of translation. */
4927 sprintf_vma (number_buf, exp->X_add_number);
4928 as_bad (_("0x%s out of range of signed 32bit displacement"),
4929 number_buf);
4930 return;
4931 }
4932 }
4933 }
4934
4935 /* Don't optimize displacement for movabs since it only takes 64bit
4936 displacement. */
4937 if (i.disp_operands
4938 && i.disp_encoding != disp_encoding_32bit
4939 && (flag_code != CODE_64BIT
4940 || strcmp (mnemonic, "movabs") != 0))
4941 optimize_disp ();
4942
4943 /* Next, we find a template that matches the given insn,
4944 making sure the overlap of the given operands types is consistent
4945 with the template operand types. */
4946
4947 if (!(t = match_template (mnem_suffix)))
4948 return;
4949
4950 if (sse_check != check_none
4951 /* The opcode space check isn't strictly needed; it's there only to
4952 bypass the logic below when easily possible. */
4953 && t->opcode_modifier.opcodespace >= SPACE_0F
4954 && t->opcode_modifier.opcodespace <= SPACE_0F3A
4955 && !i.tm.cpu_flags.bitfield.cpusse4a
4956 && !is_any_vex_encoding (t))
4957 {
4958 bool simd = false;
4959
4960 for (j = 0; j < t->operands; ++j)
4961 {
4962 if (t->operand_types[j].bitfield.class == RegMMX)
4963 break;
4964 if (t->operand_types[j].bitfield.class == RegSIMD)
4965 simd = true;
4966 }
4967
4968 if (j >= t->operands && simd)
4969 (sse_check == check_warning
4970 ? as_warn
4971 : as_bad) (_("SSE instruction `%s' is used"), i.tm.name);
4972 }
4973
4974 if (i.tm.opcode_modifier.fwait)
4975 if (!add_prefix (FWAIT_OPCODE))
4976 return;
4977
4978 /* Check if REP prefix is OK. */
4979 if (i.rep_prefix && i.tm.opcode_modifier.prefixok != PrefixRep)
4980 {
4981 as_bad (_("invalid instruction `%s' after `%s'"),
4982 i.tm.name, i.rep_prefix);
4983 return;
4984 }
4985
4986 /* Check for lock without a lockable instruction. Destination operand
4987 must be memory unless it is xchg (0x86). */
4988 if (i.prefix[LOCK_PREFIX]
4989 && (i.tm.opcode_modifier.prefixok < PrefixLock
4990 || i.mem_operands == 0
4991 || (i.tm.base_opcode != 0x86
4992 && !(i.flags[i.operands - 1] & Operand_Mem))))
4993 {
4994 as_bad (_("expecting lockable instruction after `lock'"));
4995 return;
4996 }
4997
4998 /* Check for data size prefix on VEX/XOP/EVEX encoded and SIMD insns. */
4999 if (i.prefix[DATA_PREFIX]
5000 && (is_any_vex_encoding (&i.tm)
5001 || i.tm.operand_types[i.imm_operands].bitfield.class >= RegMMX
5002 || i.tm.operand_types[i.imm_operands + 1].bitfield.class >= RegMMX))
5003 {
5004 as_bad (_("data size prefix invalid with `%s'"), i.tm.name);
5005 return;
5006 }
5007
5008 /* Check if HLE prefix is OK. */
5009 if (i.hle_prefix && !check_hle ())
5010 return;
5011
5012 /* Check BND prefix. */
5013 if (i.bnd_prefix && !i.tm.opcode_modifier.bndprefixok)
5014 as_bad (_("expecting valid branch instruction after `bnd'"));
5015
5016 /* Check NOTRACK prefix. */
5017 if (i.notrack_prefix && i.tm.opcode_modifier.prefixok != PrefixNoTrack)
5018 as_bad (_("expecting indirect branch instruction after `notrack'"));
5019
5020 if (i.tm.cpu_flags.bitfield.cpumpx)
5021 {
5022 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
5023 as_bad (_("32-bit address isn't allowed in 64-bit MPX instructions."));
5024 else if (flag_code != CODE_16BIT
5025 ? i.prefix[ADDR_PREFIX]
5026 : i.mem_operands && !i.prefix[ADDR_PREFIX])
5027 as_bad (_("16-bit address isn't allowed in MPX instructions"));
5028 }
5029
5030 /* Insert BND prefix. */
5031 if (add_bnd_prefix && i.tm.opcode_modifier.bndprefixok)
5032 {
5033 if (!i.prefix[BND_PREFIX])
5034 add_prefix (BND_PREFIX_OPCODE);
5035 else if (i.prefix[BND_PREFIX] != BND_PREFIX_OPCODE)
5036 {
5037 as_warn (_("replacing `rep'/`repe' prefix by `bnd'"));
5038 i.prefix[BND_PREFIX] = BND_PREFIX_OPCODE;
5039 }
5040 }
5041
5042 /* Check string instruction segment overrides. */
5043 if (i.tm.opcode_modifier.isstring >= IS_STRING_ES_OP0)
5044 {
5045 gas_assert (i.mem_operands);
5046 if (!check_string ())
5047 return;
5048 i.disp_operands = 0;
5049 }
5050
5051 if (optimize && !i.no_optimize && i.tm.opcode_modifier.optimize)
5052 optimize_encoding ();
5053
5054 if (use_unaligned_vector_move)
5055 encode_with_unaligned_vector_move ();
5056
5057 if (!process_suffix ())
5058 return;
5059
5060 /* Update operand types and check extended states. */
5061 for (j = 0; j < i.operands; j++)
5062 {
5063 i.types[j] = operand_type_and (i.types[j], i.tm.operand_types[j]);
5064 switch (i.tm.operand_types[j].bitfield.class)
5065 {
5066 default:
5067 break;
5068 case RegMMX:
5069 i.xstate |= xstate_mmx;
5070 break;
5071 case RegMask:
5072 i.xstate |= xstate_mask;
5073 break;
5074 case RegSIMD:
5075 if (i.tm.operand_types[j].bitfield.tmmword)
5076 i.xstate |= xstate_tmm;
5077 else if (i.tm.operand_types[j].bitfield.zmmword)
5078 i.xstate |= xstate_zmm;
5079 else if (i.tm.operand_types[j].bitfield.ymmword)
5080 i.xstate |= xstate_ymm;
5081 else if (i.tm.operand_types[j].bitfield.xmmword)
5082 i.xstate |= xstate_xmm;
5083 break;
5084 }
5085 }
5086
5087 /* Make still unresolved immediate matches conform to size of immediate
5088 given in i.suffix. */
5089 if (!finalize_imm ())
5090 return;
5091
5092 if (i.types[0].bitfield.imm1)
5093 i.imm_operands = 0; /* kludge for shift insns. */
5094
5095 /* We only need to check those implicit registers for instructions
5096 with 3 operands or less. */
5097 if (i.operands <= 3)
5098 for (j = 0; j < i.operands; j++)
5099 if (i.types[j].bitfield.instance != InstanceNone
5100 && !i.types[j].bitfield.xmmword)
5101 i.reg_operands--;
5102
5103 /* For insns with operands there are more diddles to do to the opcode. */
5104 if (i.operands)
5105 {
5106 if (!process_operands ())
5107 return;
5108 }
5109 else if (!quiet_warnings && i.tm.opcode_modifier.ugh)
5110 {
5111 /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc. */
5112 as_warn (_("translating to `%sp'"), i.tm.name);
5113 }
5114
5115 if (is_any_vex_encoding (&i.tm))
5116 {
5117 if (!cpu_arch_flags.bitfield.cpui286)
5118 {
5119 as_bad (_("instruction `%s' isn't supported outside of protected mode."),
5120 i.tm.name);
5121 return;
5122 }
5123
5124 /* Check for explicit REX prefix. */
5125 if (i.prefix[REX_PREFIX] || i.rex_encoding)
5126 {
5127 as_bad (_("REX prefix invalid with `%s'"), i.tm.name);
5128 return;
5129 }
5130
5131 if (i.tm.opcode_modifier.vex)
5132 build_vex_prefix (t);
5133 else
5134 build_evex_prefix ();
5135
5136 /* The individual REX.RXBW bits got consumed. */
5137 i.rex &= REX_OPCODE;
5138 }
5139
5140 /* Handle conversion of 'int $3' --> special int3 insn. XOP or FMA4
5141 instructions may define INT_OPCODE as well, so avoid this corner
5142 case for those instructions that use MODRM. */
5143 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
5144 && i.tm.base_opcode == INT_OPCODE
5145 && !i.tm.opcode_modifier.modrm
5146 && i.op[0].imms->X_add_number == 3)
5147 {
5148 i.tm.base_opcode = INT3_OPCODE;
5149 i.imm_operands = 0;
5150 }
5151
5152 if ((i.tm.opcode_modifier.jump == JUMP
5153 || i.tm.opcode_modifier.jump == JUMP_BYTE
5154 || i.tm.opcode_modifier.jump == JUMP_DWORD)
5155 && i.op[0].disps->X_op == O_constant)
5156 {
5157 /* Convert "jmp constant" (and "call constant") to a jump (call) to
5158 the absolute address given by the constant. Since ix86 jumps and
5159 calls are pc relative, we need to generate a reloc. */
5160 i.op[0].disps->X_add_symbol = &abs_symbol;
5161 i.op[0].disps->X_op = O_symbol;
5162 }
5163
5164 /* For 8 bit registers we need an empty rex prefix. Also if the
5165 instruction already has a prefix, we need to convert old
5166 registers to new ones. */
5167
5168 if ((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte
5169 && (i.op[0].regs->reg_flags & RegRex64) != 0)
5170 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte
5171 && (i.op[1].regs->reg_flags & RegRex64) != 0)
5172 || (((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte)
5173 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte))
5174 && i.rex != 0))
5175 {
5176 int x;
5177
5178 i.rex |= REX_OPCODE;
5179 for (x = 0; x < 2; x++)
5180 {
5181 /* Look for 8 bit operand that uses old registers. */
5182 if (i.types[x].bitfield.class == Reg && i.types[x].bitfield.byte
5183 && (i.op[x].regs->reg_flags & RegRex64) == 0)
5184 {
5185 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
5186 /* In case it is "hi" register, give up. */
5187 if (i.op[x].regs->reg_num > 3)
5188 as_bad (_("can't encode register '%s%s' in an "
5189 "instruction requiring REX prefix."),
5190 register_prefix, i.op[x].regs->reg_name);
5191
5192 /* Otherwise it is equivalent to the extended register.
5193 Since the encoding doesn't change this is merely
5194 cosmetic cleanup for debug output. */
5195
5196 i.op[x].regs = i.op[x].regs + 8;
5197 }
5198 }
5199 }
5200
5201 if (i.rex == 0 && i.rex_encoding)
5202 {
5203 /* Check if we can add a REX_OPCODE byte. Look for 8 bit operand
5204 that uses legacy register. If it is "hi" register, don't add
5205 the REX_OPCODE byte. */
5206 int x;
5207 for (x = 0; x < 2; x++)
5208 if (i.types[x].bitfield.class == Reg
5209 && i.types[x].bitfield.byte
5210 && (i.op[x].regs->reg_flags & RegRex64) == 0
5211 && i.op[x].regs->reg_num > 3)
5212 {
5213 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
5214 i.rex_encoding = false;
5215 break;
5216 }
5217
5218 if (i.rex_encoding)
5219 i.rex = REX_OPCODE;
5220 }
5221
5222 if (i.rex != 0)
5223 add_prefix (REX_OPCODE | i.rex);
5224
5225 insert_lfence_before ();
5226
5227 /* We are ready to output the insn. */
5228 output_insn ();
5229
5230 insert_lfence_after ();
5231
5232 last_insn.seg = now_seg;
5233
5234 if (i.tm.opcode_modifier.isprefix)
5235 {
5236 last_insn.kind = last_insn_prefix;
5237 last_insn.name = i.tm.name;
5238 last_insn.file = as_where (&last_insn.line);
5239 }
5240 else
5241 last_insn.kind = last_insn_other;
5242 }
5243
5244 static char *
5245 parse_insn (char *line, char *mnemonic)
5246 {
5247 char *l = line;
5248 char *token_start = l;
5249 char *mnem_p;
5250 int supported;
5251 const insn_template *t;
5252 char *dot_p = NULL;
5253
5254 while (1)
5255 {
5256 mnem_p = mnemonic;
5257 while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
5258 {
5259 if (*mnem_p == '.')
5260 dot_p = mnem_p;
5261 mnem_p++;
5262 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
5263 {
5264 as_bad (_("no such instruction: `%s'"), token_start);
5265 return NULL;
5266 }
5267 l++;
5268 }
5269 if (!is_space_char (*l)
5270 && *l != END_OF_INSN
5271 && (intel_syntax
5272 || (*l != PREFIX_SEPARATOR
5273 && *l != ',')))
5274 {
5275 as_bad (_("invalid character %s in mnemonic"),
5276 output_invalid (*l));
5277 return NULL;
5278 }
5279 if (token_start == l)
5280 {
5281 if (!intel_syntax && *l == PREFIX_SEPARATOR)
5282 as_bad (_("expecting prefix; got nothing"));
5283 else
5284 as_bad (_("expecting mnemonic; got nothing"));
5285 return NULL;
5286 }
5287
5288 /* Look up instruction (or prefix) via hash table. */
5289 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
5290
5291 if (*l != END_OF_INSN
5292 && (!is_space_char (*l) || l[1] != END_OF_INSN)
5293 && current_templates
5294 && current_templates->start->opcode_modifier.isprefix)
5295 {
5296 if (!cpu_flags_check_cpu64 (current_templates->start->cpu_flags))
5297 {
5298 as_bad ((flag_code != CODE_64BIT
5299 ? _("`%s' is only supported in 64-bit mode")
5300 : _("`%s' is not supported in 64-bit mode")),
5301 current_templates->start->name);
5302 return NULL;
5303 }
5304 /* If we are in 16-bit mode, do not allow addr16 or data16.
5305 Similarly, in 32-bit mode, do not allow addr32 or data32. */
5306 if ((current_templates->start->opcode_modifier.size == SIZE16
5307 || current_templates->start->opcode_modifier.size == SIZE32)
5308 && flag_code != CODE_64BIT
5309 && ((current_templates->start->opcode_modifier.size == SIZE32)
5310 ^ (flag_code == CODE_16BIT)))
5311 {
5312 as_bad (_("redundant %s prefix"),
5313 current_templates->start->name);
5314 return NULL;
5315 }
5316
5317 if (current_templates->start->base_opcode == PSEUDO_PREFIX)
5318 {
5319 /* Handle pseudo prefixes. */
5320 switch (current_templates->start->extension_opcode)
5321 {
5322 case Prefix_Disp8:
5323 /* {disp8} */
5324 i.disp_encoding = disp_encoding_8bit;
5325 break;
5326 case Prefix_Disp16:
5327 /* {disp16} */
5328 i.disp_encoding = disp_encoding_16bit;
5329 break;
5330 case Prefix_Disp32:
5331 /* {disp32} */
5332 i.disp_encoding = disp_encoding_32bit;
5333 break;
5334 case Prefix_Load:
5335 /* {load} */
5336 i.dir_encoding = dir_encoding_load;
5337 break;
5338 case Prefix_Store:
5339 /* {store} */
5340 i.dir_encoding = dir_encoding_store;
5341 break;
5342 case Prefix_VEX:
5343 /* {vex} */
5344 i.vec_encoding = vex_encoding_vex;
5345 break;
5346 case Prefix_VEX3:
5347 /* {vex3} */
5348 i.vec_encoding = vex_encoding_vex3;
5349 break;
5350 case Prefix_EVEX:
5351 /* {evex} */
5352 i.vec_encoding = vex_encoding_evex;
5353 break;
5354 case Prefix_REX:
5355 /* {rex} */
5356 i.rex_encoding = true;
5357 break;
5358 case Prefix_NoOptimize:
5359 /* {nooptimize} */
5360 i.no_optimize = true;
5361 break;
5362 default:
5363 abort ();
5364 }
5365 }
5366 else
5367 {
5368 /* Add prefix, checking for repeated prefixes. */
5369 switch (add_prefix (current_templates->start->base_opcode))
5370 {
5371 case PREFIX_EXIST:
5372 return NULL;
5373 case PREFIX_DS:
5374 if (current_templates->start->cpu_flags.bitfield.cpuibt)
5375 i.notrack_prefix = current_templates->start->name;
5376 break;
5377 case PREFIX_REP:
5378 if (current_templates->start->cpu_flags.bitfield.cpuhle)
5379 i.hle_prefix = current_templates->start->name;
5380 else if (current_templates->start->cpu_flags.bitfield.cpumpx)
5381 i.bnd_prefix = current_templates->start->name;
5382 else
5383 i.rep_prefix = current_templates->start->name;
5384 break;
5385 default:
5386 break;
5387 }
5388 }
5389 /* Skip past PREFIX_SEPARATOR and reset token_start. */
5390 token_start = ++l;
5391 }
5392 else
5393 break;
5394 }
5395
5396 if (!current_templates)
5397 {
5398 /* Deprecated functionality (new code should use pseudo-prefixes instead):
5399 Check if we should swap operand or force 32bit displacement in
5400 encoding. */
5401 if (mnem_p - 2 == dot_p && dot_p[1] == 's')
5402 i.dir_encoding = dir_encoding_swap;
5403 else if (mnem_p - 3 == dot_p
5404 && dot_p[1] == 'd'
5405 && dot_p[2] == '8')
5406 i.disp_encoding = disp_encoding_8bit;
5407 else if (mnem_p - 4 == dot_p
5408 && dot_p[1] == 'd'
5409 && dot_p[2] == '3'
5410 && dot_p[3] == '2')
5411 i.disp_encoding = disp_encoding_32bit;
5412 else
5413 goto check_suffix;
5414 mnem_p = dot_p;
5415 *dot_p = '\0';
5416 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
5417 }
5418
5419 if (!current_templates)
5420 {
5421 check_suffix:
5422 if (mnem_p > mnemonic)
5423 {
5424 /* See if we can get a match by trimming off a suffix. */
5425 switch (mnem_p[-1])
5426 {
5427 case WORD_MNEM_SUFFIX:
5428 if (intel_syntax && (intel_float_operand (mnemonic) & 2))
5429 i.suffix = SHORT_MNEM_SUFFIX;
5430 else
5431 /* Fall through. */
5432 case BYTE_MNEM_SUFFIX:
5433 case QWORD_MNEM_SUFFIX:
5434 i.suffix = mnem_p[-1];
5435 mnem_p[-1] = '\0';
5436 current_templates
5437 = (const templates *) str_hash_find (op_hash, mnemonic);
5438 break;
5439 case SHORT_MNEM_SUFFIX:
5440 case LONG_MNEM_SUFFIX:
5441 if (!intel_syntax)
5442 {
5443 i.suffix = mnem_p[-1];
5444 mnem_p[-1] = '\0';
5445 current_templates
5446 = (const templates *) str_hash_find (op_hash, mnemonic);
5447 }
5448 break;
5449
5450 /* Intel Syntax. */
5451 case 'd':
5452 if (intel_syntax)
5453 {
5454 if (intel_float_operand (mnemonic) == 1)
5455 i.suffix = SHORT_MNEM_SUFFIX;
5456 else
5457 i.suffix = LONG_MNEM_SUFFIX;
5458 mnem_p[-1] = '\0';
5459 current_templates
5460 = (const templates *) str_hash_find (op_hash, mnemonic);
5461 }
5462 break;
5463 }
5464 }
5465
5466 if (!current_templates)
5467 {
5468 as_bad (_("no such instruction: `%s'"), token_start);
5469 return NULL;
5470 }
5471 }
5472
5473 if (current_templates->start->opcode_modifier.jump == JUMP
5474 || current_templates->start->opcode_modifier.jump == JUMP_BYTE)
5475 {
5476 /* Check for a branch hint. We allow ",pt" and ",pn" for
5477 predict taken and predict not taken respectively.
5478 I'm not sure that branch hints actually do anything on loop
5479 and jcxz insns (JumpByte) for current Pentium4 chips. They
5480 may work in the future and it doesn't hurt to accept them
5481 now. */
5482 if (l[0] == ',' && l[1] == 'p')
5483 {
5484 if (l[2] == 't')
5485 {
5486 if (!add_prefix (DS_PREFIX_OPCODE))
5487 return NULL;
5488 l += 3;
5489 }
5490 else if (l[2] == 'n')
5491 {
5492 if (!add_prefix (CS_PREFIX_OPCODE))
5493 return NULL;
5494 l += 3;
5495 }
5496 }
5497 }
5498 /* Any other comma loses. */
5499 if (*l == ',')
5500 {
5501 as_bad (_("invalid character %s in mnemonic"),
5502 output_invalid (*l));
5503 return NULL;
5504 }
5505
5506 /* Check if instruction is supported on specified architecture. */
5507 supported = 0;
5508 for (t = current_templates->start; t < current_templates->end; ++t)
5509 {
5510 supported |= cpu_flags_match (t);
5511 if (supported == CPU_FLAGS_PERFECT_MATCH)
5512 {
5513 if (!cpu_arch_flags.bitfield.cpui386 && (flag_code != CODE_16BIT))
5514 as_warn (_("use .code16 to ensure correct addressing mode"));
5515
5516 return l;
5517 }
5518 }
5519
5520 if (!(supported & CPU_FLAGS_64BIT_MATCH))
5521 as_bad (flag_code == CODE_64BIT
5522 ? _("`%s' is not supported in 64-bit mode")
5523 : _("`%s' is only supported in 64-bit mode"),
5524 current_templates->start->name);
5525 else
5526 as_bad (_("`%s' is not supported on `%s%s'"),
5527 current_templates->start->name,
5528 cpu_arch_name ? cpu_arch_name : default_arch,
5529 cpu_sub_arch_name ? cpu_sub_arch_name : "");
5530
5531 return NULL;
5532 }
5533
5534 static char *
5535 parse_operands (char *l, const char *mnemonic)
5536 {
5537 char *token_start;
5538
5539 /* 1 if operand is pending after ','. */
5540 unsigned int expecting_operand = 0;
5541
5542 while (*l != END_OF_INSN)
5543 {
5544 /* Non-zero if operand parens not balanced. */
5545 unsigned int paren_not_balanced = 0;
5546 /* True if inside double quotes. */
5547 bool in_quotes = false;
5548
5549 /* Skip optional white space before operand. */
5550 if (is_space_char (*l))
5551 ++l;
5552 if (!is_operand_char (*l) && *l != END_OF_INSN && *l != '"')
5553 {
5554 as_bad (_("invalid character %s before operand %d"),
5555 output_invalid (*l),
5556 i.operands + 1);
5557 return NULL;
5558 }
5559 token_start = l; /* After white space. */
5560 while (in_quotes || paren_not_balanced || *l != ',')
5561 {
5562 if (*l == END_OF_INSN)
5563 {
5564 if (in_quotes)
5565 {
5566 as_bad (_("unbalanced double quotes in operand %d."),
5567 i.operands + 1);
5568 return NULL;
5569 }
5570 if (paren_not_balanced)
5571 {
5572 know (!intel_syntax);
5573 as_bad (_("unbalanced parenthesis in operand %d."),
5574 i.operands + 1);
5575 return NULL;
5576 }
5577 else
5578 break; /* we are done */
5579 }
5580 else if (*l == '\\' && l[1] == '"')
5581 ++l;
5582 else if (*l == '"')
5583 in_quotes = !in_quotes;
5584 else if (!in_quotes && !is_operand_char (*l) && !is_space_char (*l))
5585 {
5586 as_bad (_("invalid character %s in operand %d"),
5587 output_invalid (*l),
5588 i.operands + 1);
5589 return NULL;
5590 }
5591 if (!intel_syntax && !in_quotes)
5592 {
5593 if (*l == '(')
5594 ++paren_not_balanced;
5595 if (*l == ')')
5596 --paren_not_balanced;
5597 }
5598 l++;
5599 }
5600 if (l != token_start)
5601 { /* Yes, we've read in another operand. */
5602 unsigned int operand_ok;
5603 this_operand = i.operands++;
5604 if (i.operands > MAX_OPERANDS)
5605 {
5606 as_bad (_("spurious operands; (%d operands/instruction max)"),
5607 MAX_OPERANDS);
5608 return NULL;
5609 }
5610 i.types[this_operand].bitfield.unspecified = 1;
5611 /* Now parse operand adding info to 'i' as we go along. */
5612 END_STRING_AND_SAVE (l);
5613
5614 if (i.mem_operands > 1)
5615 {
5616 as_bad (_("too many memory references for `%s'"),
5617 mnemonic);
5618 return 0;
5619 }
5620
5621 if (intel_syntax)
5622 operand_ok =
5623 i386_intel_operand (token_start,
5624 intel_float_operand (mnemonic));
5625 else
5626 operand_ok = i386_att_operand (token_start);
5627
5628 RESTORE_END_STRING (l);
5629 if (!operand_ok)
5630 return NULL;
5631 }
5632 else
5633 {
5634 if (expecting_operand)
5635 {
5636 expecting_operand_after_comma:
5637 as_bad (_("expecting operand after ','; got nothing"));
5638 return NULL;
5639 }
5640 if (*l == ',')
5641 {
5642 as_bad (_("expecting operand before ','; got nothing"));
5643 return NULL;
5644 }
5645 }
5646
5647 /* Now *l must be either ',' or END_OF_INSN. */
5648 if (*l == ',')
5649 {
5650 if (*++l == END_OF_INSN)
5651 {
5652 /* Just skip it, if it's \n complain. */
5653 goto expecting_operand_after_comma;
5654 }
5655 expecting_operand = 1;
5656 }
5657 }
5658 return l;
5659 }
5660
5661 static void
5662 swap_2_operands (unsigned int xchg1, unsigned int xchg2)
5663 {
5664 union i386_op temp_op;
5665 i386_operand_type temp_type;
5666 unsigned int temp_flags;
5667 enum bfd_reloc_code_real temp_reloc;
5668
5669 temp_type = i.types[xchg2];
5670 i.types[xchg2] = i.types[xchg1];
5671 i.types[xchg1] = temp_type;
5672
5673 temp_flags = i.flags[xchg2];
5674 i.flags[xchg2] = i.flags[xchg1];
5675 i.flags[xchg1] = temp_flags;
5676
5677 temp_op = i.op[xchg2];
5678 i.op[xchg2] = i.op[xchg1];
5679 i.op[xchg1] = temp_op;
5680
5681 temp_reloc = i.reloc[xchg2];
5682 i.reloc[xchg2] = i.reloc[xchg1];
5683 i.reloc[xchg1] = temp_reloc;
5684
5685 if (i.mask.reg)
5686 {
5687 if (i.mask.operand == xchg1)
5688 i.mask.operand = xchg2;
5689 else if (i.mask.operand == xchg2)
5690 i.mask.operand = xchg1;
5691 }
5692 if (i.broadcast.type)
5693 {
5694 if (i.broadcast.operand == xchg1)
5695 i.broadcast.operand = xchg2;
5696 else if (i.broadcast.operand == xchg2)
5697 i.broadcast.operand = xchg1;
5698 }
5699 if (i.rounding.type != rc_none)
5700 {
5701 if (i.rounding.operand == xchg1)
5702 i.rounding.operand = xchg2;
5703 else if (i.rounding.operand == xchg2)
5704 i.rounding.operand = xchg1;
5705 }
5706 }
5707
5708 static void
5709 swap_operands (void)
5710 {
5711 switch (i.operands)
5712 {
5713 case 5:
5714 case 4:
5715 swap_2_operands (1, i.operands - 2);
5716 /* Fall through. */
5717 case 3:
5718 case 2:
5719 swap_2_operands (0, i.operands - 1);
5720 break;
5721 default:
5722 abort ();
5723 }
5724
5725 if (i.mem_operands == 2)
5726 {
5727 const reg_entry *temp_seg;
5728 temp_seg = i.seg[0];
5729 i.seg[0] = i.seg[1];
5730 i.seg[1] = temp_seg;
5731 }
5732 }
5733
5734 /* Try to ensure constant immediates are represented in the smallest
5735 opcode possible. */
5736 static void
5737 optimize_imm (void)
5738 {
5739 char guess_suffix = 0;
5740 int op;
5741
5742 if (i.suffix)
5743 guess_suffix = i.suffix;
5744 else if (i.reg_operands)
5745 {
5746 /* Figure out a suffix from the last register operand specified.
5747 We can't do this properly yet, i.e. excluding special register
5748 instances, but the following works for instructions with
5749 immediates. In any case, we can't set i.suffix yet. */
5750 for (op = i.operands; --op >= 0;)
5751 if (i.types[op].bitfield.class != Reg)
5752 continue;
5753 else if (i.types[op].bitfield.byte)
5754 {
5755 guess_suffix = BYTE_MNEM_SUFFIX;
5756 break;
5757 }
5758 else if (i.types[op].bitfield.word)
5759 {
5760 guess_suffix = WORD_MNEM_SUFFIX;
5761 break;
5762 }
5763 else if (i.types[op].bitfield.dword)
5764 {
5765 guess_suffix = LONG_MNEM_SUFFIX;
5766 break;
5767 }
5768 else if (i.types[op].bitfield.qword)
5769 {
5770 guess_suffix = QWORD_MNEM_SUFFIX;
5771 break;
5772 }
5773 }
5774 else if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
5775 guess_suffix = WORD_MNEM_SUFFIX;
5776
5777 for (op = i.operands; --op >= 0;)
5778 if (operand_type_check (i.types[op], imm))
5779 {
5780 switch (i.op[op].imms->X_op)
5781 {
5782 case O_constant:
5783 /* If a suffix is given, this operand may be shortened. */
5784 switch (guess_suffix)
5785 {
5786 case LONG_MNEM_SUFFIX:
5787 i.types[op].bitfield.imm32 = 1;
5788 i.types[op].bitfield.imm64 = 1;
5789 break;
5790 case WORD_MNEM_SUFFIX:
5791 i.types[op].bitfield.imm16 = 1;
5792 i.types[op].bitfield.imm32 = 1;
5793 i.types[op].bitfield.imm32s = 1;
5794 i.types[op].bitfield.imm64 = 1;
5795 break;
5796 case BYTE_MNEM_SUFFIX:
5797 i.types[op].bitfield.imm8 = 1;
5798 i.types[op].bitfield.imm8s = 1;
5799 i.types[op].bitfield.imm16 = 1;
5800 i.types[op].bitfield.imm32 = 1;
5801 i.types[op].bitfield.imm32s = 1;
5802 i.types[op].bitfield.imm64 = 1;
5803 break;
5804 }
5805
5806 /* If this operand is at most 16 bits, convert it
5807 to a signed 16 bit number before trying to see
5808 whether it will fit in an even smaller size.
5809 This allows a 16-bit operand such as $0xffe0 to
5810 be recognised as within Imm8S range. */
5811 if ((i.types[op].bitfield.imm16)
5812 && fits_in_unsigned_word (i.op[op].imms->X_add_number))
5813 {
5814 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
5815 ^ 0x8000) - 0x8000);
5816 }
5817 #ifdef BFD64
5818 /* Store 32-bit immediate in 64-bit for 64-bit BFD. */
5819 if ((i.types[op].bitfield.imm32)
5820 && fits_in_unsigned_long (i.op[op].imms->X_add_number))
5821 {
5822 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
5823 ^ ((offsetT) 1 << 31))
5824 - ((offsetT) 1 << 31));
5825 }
5826 #endif
5827 i.types[op]
5828 = operand_type_or (i.types[op],
5829 smallest_imm_type (i.op[op].imms->X_add_number));
5830
5831 /* We must avoid matching of Imm32 templates when 64bit
5832 only immediate is available. */
5833 if (guess_suffix == QWORD_MNEM_SUFFIX)
5834 i.types[op].bitfield.imm32 = 0;
5835 break;
5836
5837 case O_absent:
5838 case O_register:
5839 abort ();
5840
5841 /* Symbols and expressions. */
5842 default:
5843 /* Convert symbolic operand to proper sizes for matching, but don't
5844 prevent matching a set of insns that only supports sizes other
5845 than those matching the insn suffix. */
5846 {
5847 i386_operand_type mask, allowed;
5848 const insn_template *t = current_templates->start;
5849
5850 operand_type_set (&mask, 0);
5851 allowed = t->operand_types[op];
5852
5853 while (++t < current_templates->end)
5854 {
5855 allowed = operand_type_and (allowed, anyimm);
5856 allowed = operand_type_or (allowed, t->operand_types[op]);
5857 }
5858 switch (guess_suffix)
5859 {
5860 case QWORD_MNEM_SUFFIX:
5861 mask.bitfield.imm64 = 1;
5862 mask.bitfield.imm32s = 1;
5863 break;
5864 case LONG_MNEM_SUFFIX:
5865 mask.bitfield.imm32 = 1;
5866 break;
5867 case WORD_MNEM_SUFFIX:
5868 mask.bitfield.imm16 = 1;
5869 break;
5870 case BYTE_MNEM_SUFFIX:
5871 mask.bitfield.imm8 = 1;
5872 break;
5873 default:
5874 break;
5875 }
5876 allowed = operand_type_and (mask, allowed);
5877 if (!operand_type_all_zero (&allowed))
5878 i.types[op] = operand_type_and (i.types[op], mask);
5879 }
5880 break;
5881 }
5882 }
5883 }
5884
5885 /* Try to use the smallest displacement type too. */
5886 static void
5887 optimize_disp (void)
5888 {
5889 int op;
5890
5891 for (op = i.operands; --op >= 0;)
5892 if (operand_type_check (i.types[op], disp))
5893 {
5894 if (i.op[op].disps->X_op == O_constant)
5895 {
5896 offsetT op_disp = i.op[op].disps->X_add_number;
5897
5898 if (!op_disp && i.types[op].bitfield.baseindex)
5899 {
5900 i.types[op] = operand_type_and_not (i.types[op], anydisp);
5901 i.op[op].disps = NULL;
5902 i.disp_operands--;
5903 continue;
5904 }
5905
5906 if (i.types[op].bitfield.disp16
5907 && fits_in_unsigned_word (op_disp))
5908 {
5909 /* If this operand is at most 16 bits, convert
5910 to a signed 16 bit number and don't use 64bit
5911 displacement. */
5912 op_disp = ((op_disp ^ 0x8000) - 0x8000);
5913 i.types[op].bitfield.disp64 = 0;
5914 }
5915
5916 #ifdef BFD64
5917 /* Optimize 64-bit displacement to 32-bit for 64-bit BFD. */
5918 if ((i.types[op].bitfield.disp32
5919 || (flag_code == CODE_64BIT
5920 && want_disp32 (current_templates->start)))
5921 && fits_in_unsigned_long (op_disp))
5922 {
5923 /* If this operand is at most 32 bits, convert
5924 to a signed 32 bit number and don't use 64bit
5925 displacement. */
5926 op_disp = (op_disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
5927 i.types[op].bitfield.disp64 = 0;
5928 i.types[op].bitfield.disp32 = 1;
5929 }
5930
5931 if (flag_code == CODE_64BIT && fits_in_signed_long (op_disp))
5932 {
5933 i.types[op].bitfield.disp64 = 0;
5934 i.types[op].bitfield.disp32s = 1;
5935 }
5936 #endif
5937 if ((i.types[op].bitfield.disp32
5938 || i.types[op].bitfield.disp32s
5939 || i.types[op].bitfield.disp16)
5940 && fits_in_disp8 (op_disp))
5941 i.types[op].bitfield.disp8 = 1;
5942
5943 i.op[op].disps->X_add_number = op_disp;
5944 }
5945 else if (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
5946 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL)
5947 {
5948 fix_new_exp (frag_now, frag_more (0) - frag_now->fr_literal, 0,
5949 i.op[op].disps, 0, i.reloc[op]);
5950 i.types[op] = operand_type_and_not (i.types[op], anydisp);
5951 }
5952 else
5953 /* We only support 64bit displacement on constants. */
5954 i.types[op].bitfield.disp64 = 0;
5955 }
5956 }
5957
5958 /* Return 1 if there is a match in broadcast bytes between operand
5959 GIVEN and instruction template T. */
5960
5961 static INLINE int
5962 match_broadcast_size (const insn_template *t, unsigned int given)
5963 {
5964 return ((t->opcode_modifier.broadcast == BYTE_BROADCAST
5965 && i.types[given].bitfield.byte)
5966 || (t->opcode_modifier.broadcast == WORD_BROADCAST
5967 && i.types[given].bitfield.word)
5968 || (t->opcode_modifier.broadcast == DWORD_BROADCAST
5969 && i.types[given].bitfield.dword)
5970 || (t->opcode_modifier.broadcast == QWORD_BROADCAST
5971 && i.types[given].bitfield.qword));
5972 }
5973
5974 /* Check if operands are valid for the instruction. */
5975
5976 static int
5977 check_VecOperands (const insn_template *t)
5978 {
5979 unsigned int op;
5980 i386_cpu_flags cpu;
5981
5982 /* Templates allowing for ZMMword as well as YMMword and/or XMMword for
5983 any one operand are implicity requiring AVX512VL support if the actual
5984 operand size is YMMword or XMMword. Since this function runs after
5985 template matching, there's no need to check for YMMword/XMMword in
5986 the template. */
5987 cpu = cpu_flags_and (t->cpu_flags, avx512);
5988 if (!cpu_flags_all_zero (&cpu)
5989 && !t->cpu_flags.bitfield.cpuavx512vl
5990 && !cpu_arch_flags.bitfield.cpuavx512vl)
5991 {
5992 for (op = 0; op < t->operands; ++op)
5993 {
5994 if (t->operand_types[op].bitfield.zmmword
5995 && (i.types[op].bitfield.ymmword
5996 || i.types[op].bitfield.xmmword))
5997 {
5998 i.error = unsupported;
5999 return 1;
6000 }
6001 }
6002 }
6003
6004 /* Somewhat similarly, templates specifying both AVX and AVX2 are
6005 requiring AVX2 support if the actual operand size is YMMword. */
6006 if (t->cpu_flags.bitfield.cpuavx
6007 && t->cpu_flags.bitfield.cpuavx2
6008 && !cpu_arch_flags.bitfield.cpuavx2)
6009 {
6010 for (op = 0; op < t->operands; ++op)
6011 {
6012 if (t->operand_types[op].bitfield.xmmword
6013 && i.types[op].bitfield.ymmword)
6014 {
6015 i.error = unsupported;
6016 return 1;
6017 }
6018 }
6019 }
6020
6021 /* Without VSIB byte, we can't have a vector register for index. */
6022 if (!t->opcode_modifier.sib
6023 && i.index_reg
6024 && (i.index_reg->reg_type.bitfield.xmmword
6025 || i.index_reg->reg_type.bitfield.ymmword
6026 || i.index_reg->reg_type.bitfield.zmmword))
6027 {
6028 i.error = unsupported_vector_index_register;
6029 return 1;
6030 }
6031
6032 /* Check if default mask is allowed. */
6033 if (t->opcode_modifier.nodefmask
6034 && (!i.mask.reg || i.mask.reg->reg_num == 0))
6035 {
6036 i.error = no_default_mask;
6037 return 1;
6038 }
6039
6040 /* For VSIB byte, we need a vector register for index, and all vector
6041 registers must be distinct. */
6042 if (t->opcode_modifier.sib && t->opcode_modifier.sib != SIBMEM)
6043 {
6044 if (!i.index_reg
6045 || !((t->opcode_modifier.sib == VECSIB128
6046 && i.index_reg->reg_type.bitfield.xmmword)
6047 || (t->opcode_modifier.sib == VECSIB256
6048 && i.index_reg->reg_type.bitfield.ymmword)
6049 || (t->opcode_modifier.sib == VECSIB512
6050 && i.index_reg->reg_type.bitfield.zmmword)))
6051 {
6052 i.error = invalid_vsib_address;
6053 return 1;
6054 }
6055
6056 gas_assert (i.reg_operands == 2 || i.mask.reg);
6057 if (i.reg_operands == 2 && !i.mask.reg)
6058 {
6059 gas_assert (i.types[0].bitfield.class == RegSIMD);
6060 gas_assert (i.types[0].bitfield.xmmword
6061 || i.types[0].bitfield.ymmword);
6062 gas_assert (i.types[2].bitfield.class == RegSIMD);
6063 gas_assert (i.types[2].bitfield.xmmword
6064 || i.types[2].bitfield.ymmword);
6065 if (operand_check == check_none)
6066 return 0;
6067 if (register_number (i.op[0].regs)
6068 != register_number (i.index_reg)
6069 && register_number (i.op[2].regs)
6070 != register_number (i.index_reg)
6071 && register_number (i.op[0].regs)
6072 != register_number (i.op[2].regs))
6073 return 0;
6074 if (operand_check == check_error)
6075 {
6076 i.error = invalid_vector_register_set;
6077 return 1;
6078 }
6079 as_warn (_("mask, index, and destination registers should be distinct"));
6080 }
6081 else if (i.reg_operands == 1 && i.mask.reg)
6082 {
6083 if (i.types[1].bitfield.class == RegSIMD
6084 && (i.types[1].bitfield.xmmword
6085 || i.types[1].bitfield.ymmword
6086 || i.types[1].bitfield.zmmword)
6087 && (register_number (i.op[1].regs)
6088 == register_number (i.index_reg)))
6089 {
6090 if (operand_check == check_error)
6091 {
6092 i.error = invalid_vector_register_set;
6093 return 1;
6094 }
6095 if (operand_check != check_none)
6096 as_warn (_("index and destination registers should be distinct"));
6097 }
6098 }
6099 }
6100
6101 /* For AMX instructions with 3 TMM register operands, all operands
6102 must be distinct. */
6103 if (i.reg_operands == 3
6104 && t->operand_types[0].bitfield.tmmword
6105 && (i.op[0].regs == i.op[1].regs
6106 || i.op[0].regs == i.op[2].regs
6107 || i.op[1].regs == i.op[2].regs))
6108 {
6109 i.error = invalid_tmm_register_set;
6110 return 1;
6111 }
6112
6113 /* For some special instructions require that destination must be distinct
6114 from source registers. */
6115 if (t->opcode_modifier.distinctdest)
6116 {
6117 unsigned int dest_reg = i.operands - 1;
6118
6119 know (i.operands >= 3);
6120
6121 /* #UD if dest_reg == src1_reg or dest_reg == src2_reg. */
6122 if (i.op[dest_reg - 1].regs == i.op[dest_reg].regs
6123 || (i.reg_operands > 2
6124 && i.op[dest_reg - 2].regs == i.op[dest_reg].regs))
6125 {
6126 i.error = invalid_dest_and_src_register_set;
6127 return 1;
6128 }
6129 }
6130
6131 /* Check if broadcast is supported by the instruction and is applied
6132 to the memory operand. */
6133 if (i.broadcast.type)
6134 {
6135 i386_operand_type type, overlap;
6136
6137 /* Check if specified broadcast is supported in this instruction,
6138 and its broadcast bytes match the memory operand. */
6139 op = i.broadcast.operand;
6140 if (!t->opcode_modifier.broadcast
6141 || !(i.flags[op] & Operand_Mem)
6142 || (!i.types[op].bitfield.unspecified
6143 && !match_broadcast_size (t, op)))
6144 {
6145 bad_broadcast:
6146 i.error = unsupported_broadcast;
6147 return 1;
6148 }
6149
6150 i.broadcast.bytes = ((1 << (t->opcode_modifier.broadcast - 1))
6151 * i.broadcast.type);
6152 operand_type_set (&type, 0);
6153 switch (i.broadcast.bytes)
6154 {
6155 case 2:
6156 type.bitfield.word = 1;
6157 break;
6158 case 4:
6159 type.bitfield.dword = 1;
6160 break;
6161 case 8:
6162 type.bitfield.qword = 1;
6163 break;
6164 case 16:
6165 type.bitfield.xmmword = 1;
6166 break;
6167 case 32:
6168 type.bitfield.ymmword = 1;
6169 break;
6170 case 64:
6171 type.bitfield.zmmword = 1;
6172 break;
6173 default:
6174 goto bad_broadcast;
6175 }
6176
6177 overlap = operand_type_and (type, t->operand_types[op]);
6178 if (t->operand_types[op].bitfield.class == RegSIMD
6179 && t->operand_types[op].bitfield.byte
6180 + t->operand_types[op].bitfield.word
6181 + t->operand_types[op].bitfield.dword
6182 + t->operand_types[op].bitfield.qword > 1)
6183 {
6184 overlap.bitfield.xmmword = 0;
6185 overlap.bitfield.ymmword = 0;
6186 overlap.bitfield.zmmword = 0;
6187 }
6188 if (operand_type_all_zero (&overlap))
6189 goto bad_broadcast;
6190
6191 if (t->opcode_modifier.checkregsize)
6192 {
6193 unsigned int j;
6194
6195 type.bitfield.baseindex = 1;
6196 for (j = 0; j < i.operands; ++j)
6197 {
6198 if (j != op
6199 && !operand_type_register_match(i.types[j],
6200 t->operand_types[j],
6201 type,
6202 t->operand_types[op]))
6203 goto bad_broadcast;
6204 }
6205 }
6206 }
6207 /* If broadcast is supported in this instruction, we need to check if
6208 operand of one-element size isn't specified without broadcast. */
6209 else if (t->opcode_modifier.broadcast && i.mem_operands)
6210 {
6211 /* Find memory operand. */
6212 for (op = 0; op < i.operands; op++)
6213 if (i.flags[op] & Operand_Mem)
6214 break;
6215 gas_assert (op < i.operands);
6216 /* Check size of the memory operand. */
6217 if (match_broadcast_size (t, op))
6218 {
6219 i.error = broadcast_needed;
6220 return 1;
6221 }
6222 }
6223 else
6224 op = MAX_OPERANDS - 1; /* Avoid uninitialized variable warning. */
6225
6226 /* Check if requested masking is supported. */
6227 if (i.mask.reg)
6228 {
6229 switch (t->opcode_modifier.masking)
6230 {
6231 case BOTH_MASKING:
6232 break;
6233 case MERGING_MASKING:
6234 if (i.mask.zeroing)
6235 {
6236 case 0:
6237 i.error = unsupported_masking;
6238 return 1;
6239 }
6240 break;
6241 case DYNAMIC_MASKING:
6242 /* Memory destinations allow only merging masking. */
6243 if (i.mask.zeroing && i.mem_operands)
6244 {
6245 /* Find memory operand. */
6246 for (op = 0; op < i.operands; op++)
6247 if (i.flags[op] & Operand_Mem)
6248 break;
6249 gas_assert (op < i.operands);
6250 if (op == i.operands - 1)
6251 {
6252 i.error = unsupported_masking;
6253 return 1;
6254 }
6255 }
6256 break;
6257 default:
6258 abort ();
6259 }
6260 }
6261
6262 /* Check if masking is applied to dest operand. */
6263 if (i.mask.reg && (i.mask.operand != i.operands - 1))
6264 {
6265 i.error = mask_not_on_destination;
6266 return 1;
6267 }
6268
6269 /* Check RC/SAE. */
6270 if (i.rounding.type != rc_none)
6271 {
6272 if (!t->opcode_modifier.sae
6273 || (i.rounding.type != saeonly && !t->opcode_modifier.staticrounding))
6274 {
6275 i.error = unsupported_rc_sae;
6276 return 1;
6277 }
6278 /* If the instruction has several immediate operands and one of
6279 them is rounding, the rounding operand should be the last
6280 immediate operand. */
6281 if (i.imm_operands > 1
6282 && i.rounding.operand != i.imm_operands - 1)
6283 {
6284 i.error = rc_sae_operand_not_last_imm;
6285 return 1;
6286 }
6287 }
6288
6289 /* Check the special Imm4 cases; must be the first operand. */
6290 if (t->cpu_flags.bitfield.cpuxop && t->operands == 5)
6291 {
6292 if (i.op[0].imms->X_op != O_constant
6293 || !fits_in_imm4 (i.op[0].imms->X_add_number))
6294 {
6295 i.error = bad_imm4;
6296 return 1;
6297 }
6298
6299 /* Turn off Imm<N> so that update_imm won't complain. */
6300 operand_type_set (&i.types[0], 0);
6301 }
6302
6303 /* Check vector Disp8 operand. */
6304 if (t->opcode_modifier.disp8memshift
6305 && i.disp_encoding != disp_encoding_32bit)
6306 {
6307 if (i.broadcast.type)
6308 i.memshift = t->opcode_modifier.broadcast - 1;
6309 else if (t->opcode_modifier.disp8memshift != DISP8_SHIFT_VL)
6310 i.memshift = t->opcode_modifier.disp8memshift;
6311 else
6312 {
6313 const i386_operand_type *type = NULL, *fallback = NULL;
6314
6315 i.memshift = 0;
6316 for (op = 0; op < i.operands; op++)
6317 if (i.flags[op] & Operand_Mem)
6318 {
6319 if (t->opcode_modifier.evex == EVEXLIG)
6320 i.memshift = 2 + (i.suffix == QWORD_MNEM_SUFFIX);
6321 else if (t->operand_types[op].bitfield.xmmword
6322 + t->operand_types[op].bitfield.ymmword
6323 + t->operand_types[op].bitfield.zmmword <= 1)
6324 type = &t->operand_types[op];
6325 else if (!i.types[op].bitfield.unspecified)
6326 type = &i.types[op];
6327 else /* Ambiguities get resolved elsewhere. */
6328 fallback = &t->operand_types[op];
6329 }
6330 else if (i.types[op].bitfield.class == RegSIMD
6331 && t->opcode_modifier.evex != EVEXLIG)
6332 {
6333 if (i.types[op].bitfield.zmmword)
6334 i.memshift = 6;
6335 else if (i.types[op].bitfield.ymmword && i.memshift < 5)
6336 i.memshift = 5;
6337 else if (i.types[op].bitfield.xmmword && i.memshift < 4)
6338 i.memshift = 4;
6339 }
6340
6341 if (!type && !i.memshift)
6342 type = fallback;
6343 if (type)
6344 {
6345 if (type->bitfield.zmmword)
6346 i.memshift = 6;
6347 else if (type->bitfield.ymmword)
6348 i.memshift = 5;
6349 else if (type->bitfield.xmmword)
6350 i.memshift = 4;
6351 }
6352
6353 /* For the check in fits_in_disp8(). */
6354 if (i.memshift == 0)
6355 i.memshift = -1;
6356 }
6357
6358 for (op = 0; op < i.operands; op++)
6359 if (operand_type_check (i.types[op], disp)
6360 && i.op[op].disps->X_op == O_constant)
6361 {
6362 if (fits_in_disp8 (i.op[op].disps->X_add_number))
6363 {
6364 i.types[op].bitfield.disp8 = 1;
6365 return 0;
6366 }
6367 i.types[op].bitfield.disp8 = 0;
6368 }
6369 }
6370
6371 i.memshift = 0;
6372
6373 return 0;
6374 }
6375
6376 /* Check if encoding requirements are met by the instruction. */
6377
6378 static int
6379 VEX_check_encoding (const insn_template *t)
6380 {
6381 if (i.vec_encoding == vex_encoding_error)
6382 {
6383 i.error = unsupported;
6384 return 1;
6385 }
6386
6387 if (i.vec_encoding == vex_encoding_evex)
6388 {
6389 /* This instruction must be encoded with EVEX prefix. */
6390 if (!is_evex_encoding (t))
6391 {
6392 i.error = unsupported;
6393 return 1;
6394 }
6395 return 0;
6396 }
6397
6398 if (!t->opcode_modifier.vex)
6399 {
6400 /* This instruction template doesn't have VEX prefix. */
6401 if (i.vec_encoding != vex_encoding_default)
6402 {
6403 i.error = unsupported;
6404 return 1;
6405 }
6406 return 0;
6407 }
6408
6409 return 0;
6410 }
6411
6412 static const insn_template *
6413 match_template (char mnem_suffix)
6414 {
6415 /* Points to template once we've found it. */
6416 const insn_template *t;
6417 i386_operand_type overlap0, overlap1, overlap2, overlap3;
6418 i386_operand_type overlap4;
6419 unsigned int found_reverse_match;
6420 i386_opcode_modifier suffix_check;
6421 i386_operand_type operand_types [MAX_OPERANDS];
6422 int addr_prefix_disp;
6423 unsigned int j, size_match, check_register;
6424 enum i386_error specific_error = 0;
6425
6426 #if MAX_OPERANDS != 5
6427 # error "MAX_OPERANDS must be 5."
6428 #endif
6429
6430 found_reverse_match = 0;
6431 addr_prefix_disp = -1;
6432
6433 /* Prepare for mnemonic suffix check. */
6434 memset (&suffix_check, 0, sizeof (suffix_check));
6435 switch (mnem_suffix)
6436 {
6437 case BYTE_MNEM_SUFFIX:
6438 suffix_check.no_bsuf = 1;
6439 break;
6440 case WORD_MNEM_SUFFIX:
6441 suffix_check.no_wsuf = 1;
6442 break;
6443 case SHORT_MNEM_SUFFIX:
6444 suffix_check.no_ssuf = 1;
6445 break;
6446 case LONG_MNEM_SUFFIX:
6447 suffix_check.no_lsuf = 1;
6448 break;
6449 case QWORD_MNEM_SUFFIX:
6450 suffix_check.no_qsuf = 1;
6451 break;
6452 default:
6453 /* NB: In Intel syntax, normally we can check for memory operand
6454 size when there is no mnemonic suffix. But jmp and call have
6455 2 different encodings with Dword memory operand size, one with
6456 No_ldSuf and the other without. i.suffix is set to
6457 LONG_DOUBLE_MNEM_SUFFIX to skip the one with No_ldSuf. */
6458 if (i.suffix == LONG_DOUBLE_MNEM_SUFFIX)
6459 suffix_check.no_ldsuf = 1;
6460 }
6461
6462 /* Must have right number of operands. */
6463 i.error = number_of_operands_mismatch;
6464
6465 for (t = current_templates->start; t < current_templates->end; t++)
6466 {
6467 addr_prefix_disp = -1;
6468 found_reverse_match = 0;
6469
6470 if (i.operands != t->operands)
6471 continue;
6472
6473 /* Check processor support. */
6474 i.error = unsupported;
6475 if (cpu_flags_match (t) != CPU_FLAGS_PERFECT_MATCH)
6476 continue;
6477
6478 /* Check Pseudo Prefix. */
6479 i.error = unsupported;
6480 if (t->opcode_modifier.pseudovexprefix
6481 && !(i.vec_encoding == vex_encoding_vex
6482 || i.vec_encoding == vex_encoding_vex3))
6483 continue;
6484
6485 /* Check AT&T mnemonic. */
6486 i.error = unsupported_with_intel_mnemonic;
6487 if (intel_mnemonic && t->opcode_modifier.attmnemonic)
6488 continue;
6489
6490 /* Check AT&T/Intel syntax. */
6491 i.error = unsupported_syntax;
6492 if ((intel_syntax && t->opcode_modifier.attsyntax)
6493 || (!intel_syntax && t->opcode_modifier.intelsyntax))
6494 continue;
6495
6496 /* Check Intel64/AMD64 ISA. */
6497 switch (isa64)
6498 {
6499 default:
6500 /* Default: Don't accept Intel64. */
6501 if (t->opcode_modifier.isa64 == INTEL64)
6502 continue;
6503 break;
6504 case amd64:
6505 /* -mamd64: Don't accept Intel64 and Intel64 only. */
6506 if (t->opcode_modifier.isa64 >= INTEL64)
6507 continue;
6508 break;
6509 case intel64:
6510 /* -mintel64: Don't accept AMD64. */
6511 if (t->opcode_modifier.isa64 == AMD64 && flag_code == CODE_64BIT)
6512 continue;
6513 break;
6514 }
6515
6516 /* Check the suffix. */
6517 i.error = invalid_instruction_suffix;
6518 if ((t->opcode_modifier.no_bsuf && suffix_check.no_bsuf)
6519 || (t->opcode_modifier.no_wsuf && suffix_check.no_wsuf)
6520 || (t->opcode_modifier.no_lsuf && suffix_check.no_lsuf)
6521 || (t->opcode_modifier.no_ssuf && suffix_check.no_ssuf)
6522 || (t->opcode_modifier.no_qsuf && suffix_check.no_qsuf)
6523 || (t->opcode_modifier.no_ldsuf && suffix_check.no_ldsuf))
6524 continue;
6525
6526 size_match = operand_size_match (t);
6527 if (!size_match)
6528 continue;
6529
6530 /* This is intentionally not
6531
6532 if (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE))
6533
6534 as the case of a missing * on the operand is accepted (perhaps with
6535 a warning, issued further down). */
6536 if (i.jumpabsolute && t->opcode_modifier.jump != JUMP_ABSOLUTE)
6537 {
6538 i.error = operand_type_mismatch;
6539 continue;
6540 }
6541
6542 for (j = 0; j < MAX_OPERANDS; j++)
6543 operand_types[j] = t->operand_types[j];
6544
6545 /* In general, don't allow
6546 - 64-bit operands outside of 64-bit mode,
6547 - 32-bit operands on pre-386. */
6548 j = i.imm_operands + (t->operands > i.imm_operands + 1);
6549 if (((i.suffix == QWORD_MNEM_SUFFIX
6550 && flag_code != CODE_64BIT
6551 && !(t->opcode_modifier.opcodespace == SPACE_0F
6552 && t->base_opcode == 0xc7
6553 && t->opcode_modifier.opcodeprefix == PREFIX_NONE
6554 && t->extension_opcode == 1) /* cmpxchg8b */)
6555 || (i.suffix == LONG_MNEM_SUFFIX
6556 && !cpu_arch_flags.bitfield.cpui386))
6557 && (intel_syntax
6558 ? (t->opcode_modifier.mnemonicsize != IGNORESIZE
6559 && !intel_float_operand (t->name))
6560 : intel_float_operand (t->name) != 2)
6561 && (t->operands == i.imm_operands
6562 || (operand_types[i.imm_operands].bitfield.class != RegMMX
6563 && operand_types[i.imm_operands].bitfield.class != RegSIMD
6564 && operand_types[i.imm_operands].bitfield.class != RegMask)
6565 || (operand_types[j].bitfield.class != RegMMX
6566 && operand_types[j].bitfield.class != RegSIMD
6567 && operand_types[j].bitfield.class != RegMask))
6568 && !t->opcode_modifier.sib)
6569 continue;
6570
6571 /* Do not verify operands when there are none. */
6572 if (!t->operands)
6573 {
6574 if (VEX_check_encoding (t))
6575 {
6576 specific_error = i.error;
6577 continue;
6578 }
6579
6580 /* We've found a match; break out of loop. */
6581 break;
6582 }
6583
6584 if (!t->opcode_modifier.jump
6585 || t->opcode_modifier.jump == JUMP_ABSOLUTE)
6586 {
6587 /* There should be only one Disp operand. */
6588 for (j = 0; j < MAX_OPERANDS; j++)
6589 if (operand_type_check (operand_types[j], disp))
6590 break;
6591 if (j < MAX_OPERANDS)
6592 {
6593 bool override = (i.prefix[ADDR_PREFIX] != 0);
6594
6595 addr_prefix_disp = j;
6596
6597 /* Address size prefix will turn Disp64/Disp32S/Disp32/Disp16
6598 operand into Disp32/Disp32/Disp16/Disp32 operand. */
6599 switch (flag_code)
6600 {
6601 case CODE_16BIT:
6602 override = !override;
6603 /* Fall through. */
6604 case CODE_32BIT:
6605 if (operand_types[j].bitfield.disp32
6606 && operand_types[j].bitfield.disp16)
6607 {
6608 operand_types[j].bitfield.disp16 = override;
6609 operand_types[j].bitfield.disp32 = !override;
6610 }
6611 operand_types[j].bitfield.disp32s = 0;
6612 operand_types[j].bitfield.disp64 = 0;
6613 break;
6614
6615 case CODE_64BIT:
6616 if (operand_types[j].bitfield.disp32s
6617 || operand_types[j].bitfield.disp64)
6618 {
6619 operand_types[j].bitfield.disp64 &= !override;
6620 operand_types[j].bitfield.disp32s &= !override;
6621 operand_types[j].bitfield.disp32 = override;
6622 }
6623 operand_types[j].bitfield.disp16 = 0;
6624 break;
6625 }
6626 }
6627 }
6628
6629 switch (i.reloc[0])
6630 {
6631 case BFD_RELOC_386_GOT32:
6632 /* Force 0x8b encoding for "mov foo@GOT, %eax". */
6633 if (t->base_opcode == 0xa0
6634 && t->opcode_modifier.opcodespace == SPACE_BASE)
6635 continue;
6636 break;
6637 case BFD_RELOC_386_TLS_GOTIE:
6638 case BFD_RELOC_386_TLS_LE_32:
6639 case BFD_RELOC_X86_64_GOTTPOFF:
6640 case BFD_RELOC_X86_64_TLSLD:
6641 /* Don't allow KMOV in TLS code sequences. */
6642 if (t->opcode_modifier.vex)
6643 continue;
6644 break;
6645 default:
6646 break;
6647 }
6648
6649 /* We check register size if needed. */
6650 if (t->opcode_modifier.checkregsize)
6651 {
6652 check_register = (1 << t->operands) - 1;
6653 if (i.broadcast.type)
6654 check_register &= ~(1 << i.broadcast.operand);
6655 }
6656 else
6657 check_register = 0;
6658
6659 overlap0 = operand_type_and (i.types[0], operand_types[0]);
6660 switch (t->operands)
6661 {
6662 case 1:
6663 if (!operand_type_match (overlap0, i.types[0]))
6664 continue;
6665 break;
6666 case 2:
6667 /* xchg %eax, %eax is a special case. It is an alias for nop
6668 only in 32bit mode and we can use opcode 0x90. In 64bit
6669 mode, we can't use 0x90 for xchg %eax, %eax since it should
6670 zero-extend %eax to %rax. */
6671 if (flag_code == CODE_64BIT
6672 && t->base_opcode == 0x90
6673 && t->opcode_modifier.opcodespace == SPACE_BASE
6674 && i.types[0].bitfield.instance == Accum
6675 && i.types[0].bitfield.dword
6676 && i.types[1].bitfield.instance == Accum
6677 && i.types[1].bitfield.dword)
6678 continue;
6679 /* xrelease mov %eax, <disp> is another special case. It must not
6680 match the accumulator-only encoding of mov. */
6681 if (flag_code != CODE_64BIT
6682 && i.hle_prefix
6683 && t->base_opcode == 0xa0
6684 && t->opcode_modifier.opcodespace == SPACE_BASE
6685 && i.types[0].bitfield.instance == Accum
6686 && (i.flags[1] & Operand_Mem))
6687 continue;
6688 /* Fall through. */
6689
6690 case 3:
6691 if (!(size_match & MATCH_STRAIGHT))
6692 goto check_reverse;
6693 /* Reverse direction of operands if swapping is possible in the first
6694 place (operands need to be symmetric) and
6695 - the load form is requested, and the template is a store form,
6696 - the store form is requested, and the template is a load form,
6697 - the non-default (swapped) form is requested. */
6698 overlap1 = operand_type_and (operand_types[0], operand_types[1]);
6699 if (t->opcode_modifier.d && i.reg_operands == i.operands
6700 && !operand_type_all_zero (&overlap1))
6701 switch (i.dir_encoding)
6702 {
6703 case dir_encoding_load:
6704 if (operand_type_check (operand_types[i.operands - 1], anymem)
6705 || t->opcode_modifier.regmem)
6706 goto check_reverse;
6707 break;
6708
6709 case dir_encoding_store:
6710 if (!operand_type_check (operand_types[i.operands - 1], anymem)
6711 && !t->opcode_modifier.regmem)
6712 goto check_reverse;
6713 break;
6714
6715 case dir_encoding_swap:
6716 goto check_reverse;
6717
6718 case dir_encoding_default:
6719 break;
6720 }
6721 /* If we want store form, we skip the current load. */
6722 if ((i.dir_encoding == dir_encoding_store
6723 || i.dir_encoding == dir_encoding_swap)
6724 && i.mem_operands == 0
6725 && t->opcode_modifier.load)
6726 continue;
6727 /* Fall through. */
6728 case 4:
6729 case 5:
6730 overlap1 = operand_type_and (i.types[1], operand_types[1]);
6731 if (!operand_type_match (overlap0, i.types[0])
6732 || !operand_type_match (overlap1, i.types[1])
6733 || ((check_register & 3) == 3
6734 && !operand_type_register_match (i.types[0],
6735 operand_types[0],
6736 i.types[1],
6737 operand_types[1])))
6738 {
6739 /* Check if other direction is valid ... */
6740 if (!t->opcode_modifier.d)
6741 continue;
6742
6743 check_reverse:
6744 if (!(size_match & MATCH_REVERSE))
6745 continue;
6746 /* Try reversing direction of operands. */
6747 overlap0 = operand_type_and (i.types[0], operand_types[i.operands - 1]);
6748 overlap1 = operand_type_and (i.types[i.operands - 1], operand_types[0]);
6749 if (!operand_type_match (overlap0, i.types[0])
6750 || !operand_type_match (overlap1, i.types[i.operands - 1])
6751 || (check_register
6752 && !operand_type_register_match (i.types[0],
6753 operand_types[i.operands - 1],
6754 i.types[i.operands - 1],
6755 operand_types[0])))
6756 {
6757 /* Does not match either direction. */
6758 continue;
6759 }
6760 /* found_reverse_match holds which of D or FloatR
6761 we've found. */
6762 if (!t->opcode_modifier.d)
6763 found_reverse_match = 0;
6764 else if (operand_types[0].bitfield.tbyte)
6765 found_reverse_match = Opcode_FloatD;
6766 else if (operand_types[0].bitfield.xmmword
6767 || operand_types[i.operands - 1].bitfield.xmmword
6768 || operand_types[0].bitfield.class == RegMMX
6769 || operand_types[i.operands - 1].bitfield.class == RegMMX
6770 || is_any_vex_encoding(t))
6771 found_reverse_match = (t->base_opcode & 0xee) != 0x6e
6772 ? Opcode_SIMD_FloatD : Opcode_SIMD_IntD;
6773 else
6774 found_reverse_match = Opcode_D;
6775 if (t->opcode_modifier.floatr)
6776 found_reverse_match |= Opcode_FloatR;
6777 }
6778 else
6779 {
6780 /* Found a forward 2 operand match here. */
6781 switch (t->operands)
6782 {
6783 case 5:
6784 overlap4 = operand_type_and (i.types[4],
6785 operand_types[4]);
6786 /* Fall through. */
6787 case 4:
6788 overlap3 = operand_type_and (i.types[3],
6789 operand_types[3]);
6790 /* Fall through. */
6791 case 3:
6792 overlap2 = operand_type_and (i.types[2],
6793 operand_types[2]);
6794 break;
6795 }
6796
6797 switch (t->operands)
6798 {
6799 case 5:
6800 if (!operand_type_match (overlap4, i.types[4])
6801 || !operand_type_register_match (i.types[3],
6802 operand_types[3],
6803 i.types[4],
6804 operand_types[4]))
6805 continue;
6806 /* Fall through. */
6807 case 4:
6808 if (!operand_type_match (overlap3, i.types[3])
6809 || ((check_register & 0xa) == 0xa
6810 && !operand_type_register_match (i.types[1],
6811 operand_types[1],
6812 i.types[3],
6813 operand_types[3]))
6814 || ((check_register & 0xc) == 0xc
6815 && !operand_type_register_match (i.types[2],
6816 operand_types[2],
6817 i.types[3],
6818 operand_types[3])))
6819 continue;
6820 /* Fall through. */
6821 case 3:
6822 /* Here we make use of the fact that there are no
6823 reverse match 3 operand instructions. */
6824 if (!operand_type_match (overlap2, i.types[2])
6825 || ((check_register & 5) == 5
6826 && !operand_type_register_match (i.types[0],
6827 operand_types[0],
6828 i.types[2],
6829 operand_types[2]))
6830 || ((check_register & 6) == 6
6831 && !operand_type_register_match (i.types[1],
6832 operand_types[1],
6833 i.types[2],
6834 operand_types[2])))
6835 continue;
6836 break;
6837 }
6838 }
6839 /* Found either forward/reverse 2, 3 or 4 operand match here:
6840 slip through to break. */
6841 }
6842
6843 /* Check if vector operands are valid. */
6844 if (check_VecOperands (t))
6845 {
6846 specific_error = i.error;
6847 continue;
6848 }
6849
6850 /* Check if VEX/EVEX encoding requirements can be satisfied. */
6851 if (VEX_check_encoding (t))
6852 {
6853 specific_error = i.error;
6854 continue;
6855 }
6856
6857 /* We've found a match; break out of loop. */
6858 break;
6859 }
6860
6861 if (t == current_templates->end)
6862 {
6863 /* We found no match. */
6864 const char *err_msg;
6865 switch (specific_error ? specific_error : i.error)
6866 {
6867 default:
6868 abort ();
6869 case operand_size_mismatch:
6870 err_msg = _("operand size mismatch");
6871 break;
6872 case operand_type_mismatch:
6873 err_msg = _("operand type mismatch");
6874 break;
6875 case register_type_mismatch:
6876 err_msg = _("register type mismatch");
6877 break;
6878 case number_of_operands_mismatch:
6879 err_msg = _("number of operands mismatch");
6880 break;
6881 case invalid_instruction_suffix:
6882 err_msg = _("invalid instruction suffix");
6883 break;
6884 case bad_imm4:
6885 err_msg = _("constant doesn't fit in 4 bits");
6886 break;
6887 case unsupported_with_intel_mnemonic:
6888 err_msg = _("unsupported with Intel mnemonic");
6889 break;
6890 case unsupported_syntax:
6891 err_msg = _("unsupported syntax");
6892 break;
6893 case unsupported:
6894 as_bad (_("unsupported instruction `%s'"),
6895 current_templates->start->name);
6896 return NULL;
6897 case invalid_sib_address:
6898 err_msg = _("invalid SIB address");
6899 break;
6900 case invalid_vsib_address:
6901 err_msg = _("invalid VSIB address");
6902 break;
6903 case invalid_vector_register_set:
6904 err_msg = _("mask, index, and destination registers must be distinct");
6905 break;
6906 case invalid_tmm_register_set:
6907 err_msg = _("all tmm registers must be distinct");
6908 break;
6909 case invalid_dest_and_src_register_set:
6910 err_msg = _("destination and source registers must be distinct");
6911 break;
6912 case unsupported_vector_index_register:
6913 err_msg = _("unsupported vector index register");
6914 break;
6915 case unsupported_broadcast:
6916 err_msg = _("unsupported broadcast");
6917 break;
6918 case broadcast_needed:
6919 err_msg = _("broadcast is needed for operand of such type");
6920 break;
6921 case unsupported_masking:
6922 err_msg = _("unsupported masking");
6923 break;
6924 case mask_not_on_destination:
6925 err_msg = _("mask not on destination operand");
6926 break;
6927 case no_default_mask:
6928 err_msg = _("default mask isn't allowed");
6929 break;
6930 case unsupported_rc_sae:
6931 err_msg = _("unsupported static rounding/sae");
6932 break;
6933 case rc_sae_operand_not_last_imm:
6934 if (intel_syntax)
6935 err_msg = _("RC/SAE operand must precede immediate operands");
6936 else
6937 err_msg = _("RC/SAE operand must follow immediate operands");
6938 break;
6939 case invalid_register_operand:
6940 err_msg = _("invalid register operand");
6941 break;
6942 }
6943 as_bad (_("%s for `%s'"), err_msg,
6944 current_templates->start->name);
6945 return NULL;
6946 }
6947
6948 if (!quiet_warnings)
6949 {
6950 if (!intel_syntax
6951 && (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE)))
6952 as_warn (_("indirect %s without `*'"), t->name);
6953
6954 if (t->opcode_modifier.isprefix
6955 && t->opcode_modifier.mnemonicsize == IGNORESIZE)
6956 {
6957 /* Warn them that a data or address size prefix doesn't
6958 affect assembly of the next line of code. */
6959 as_warn (_("stand-alone `%s' prefix"), t->name);
6960 }
6961 }
6962
6963 /* Copy the template we found. */
6964 install_template (t);
6965
6966 if (addr_prefix_disp != -1)
6967 i.tm.operand_types[addr_prefix_disp]
6968 = operand_types[addr_prefix_disp];
6969
6970 if (found_reverse_match)
6971 {
6972 /* If we found a reverse match we must alter the opcode direction
6973 bit and clear/flip the regmem modifier one. found_reverse_match
6974 holds bits to change (different for int & float insns). */
6975
6976 i.tm.base_opcode ^= found_reverse_match;
6977
6978 i.tm.operand_types[0] = operand_types[i.operands - 1];
6979 i.tm.operand_types[i.operands - 1] = operand_types[0];
6980
6981 /* Certain SIMD insns have their load forms specified in the opcode
6982 table, and hence we need to _set_ RegMem instead of clearing it.
6983 We need to avoid setting the bit though on insns like KMOVW. */
6984 i.tm.opcode_modifier.regmem
6985 = i.tm.opcode_modifier.modrm && i.tm.opcode_modifier.d
6986 && i.tm.operands > 2U - i.tm.opcode_modifier.sse2avx
6987 && !i.tm.opcode_modifier.regmem;
6988 }
6989
6990 return t;
6991 }
6992
6993 static int
6994 check_string (void)
6995 {
6996 unsigned int es_op = i.tm.opcode_modifier.isstring - IS_STRING_ES_OP0;
6997 unsigned int op = i.tm.operand_types[0].bitfield.baseindex ? es_op : 0;
6998
6999 if (i.seg[op] != NULL && i.seg[op] != reg_es)
7000 {
7001 as_bad (_("`%s' operand %u must use `%ses' segment"),
7002 i.tm.name,
7003 intel_syntax ? i.tm.operands - es_op : es_op + 1,
7004 register_prefix);
7005 return 0;
7006 }
7007
7008 /* There's only ever one segment override allowed per instruction.
7009 This instruction possibly has a legal segment override on the
7010 second operand, so copy the segment to where non-string
7011 instructions store it, allowing common code. */
7012 i.seg[op] = i.seg[1];
7013
7014 return 1;
7015 }
7016
7017 static int
7018 process_suffix (void)
7019 {
7020 bool is_crc32 = false, is_movx = false;
7021
7022 /* If matched instruction specifies an explicit instruction mnemonic
7023 suffix, use it. */
7024 if (i.tm.opcode_modifier.size == SIZE16)
7025 i.suffix = WORD_MNEM_SUFFIX;
7026 else if (i.tm.opcode_modifier.size == SIZE32)
7027 i.suffix = LONG_MNEM_SUFFIX;
7028 else if (i.tm.opcode_modifier.size == SIZE64)
7029 i.suffix = QWORD_MNEM_SUFFIX;
7030 else if (i.reg_operands
7031 && (i.operands > 1 || i.types[0].bitfield.class == Reg)
7032 && !i.tm.opcode_modifier.addrprefixopreg)
7033 {
7034 unsigned int numop = i.operands;
7035
7036 /* MOVSX/MOVZX */
7037 is_movx = (i.tm.opcode_modifier.opcodespace == SPACE_0F
7038 && (i.tm.base_opcode | 8) == 0xbe)
7039 || (i.tm.opcode_modifier.opcodespace == SPACE_BASE
7040 && i.tm.base_opcode == 0x63
7041 && i.tm.cpu_flags.bitfield.cpu64);
7042
7043 /* CRC32 */
7044 is_crc32 = (i.tm.base_opcode == 0xf0
7045 && i.tm.opcode_modifier.opcodespace == SPACE_0F38
7046 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2);
7047
7048 /* movsx/movzx want only their source operand considered here, for the
7049 ambiguity checking below. The suffix will be replaced afterwards
7050 to represent the destination (register). */
7051 if (is_movx && (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63))
7052 --i.operands;
7053
7054 /* crc32 needs REX.W set regardless of suffix / source operand size. */
7055 if (is_crc32 && i.tm.operand_types[1].bitfield.qword)
7056 i.rex |= REX_W;
7057
7058 /* If there's no instruction mnemonic suffix we try to invent one
7059 based on GPR operands. */
7060 if (!i.suffix)
7061 {
7062 /* We take i.suffix from the last register operand specified,
7063 Destination register type is more significant than source
7064 register type. crc32 in SSE4.2 prefers source register
7065 type. */
7066 unsigned int op = is_crc32 ? 1 : i.operands;
7067
7068 while (op--)
7069 if (i.tm.operand_types[op].bitfield.instance == InstanceNone
7070 || i.tm.operand_types[op].bitfield.instance == Accum)
7071 {
7072 if (i.types[op].bitfield.class != Reg)
7073 continue;
7074 if (i.types[op].bitfield.byte)
7075 i.suffix = BYTE_MNEM_SUFFIX;
7076 else if (i.types[op].bitfield.word)
7077 i.suffix = WORD_MNEM_SUFFIX;
7078 else if (i.types[op].bitfield.dword)
7079 i.suffix = LONG_MNEM_SUFFIX;
7080 else if (i.types[op].bitfield.qword)
7081 i.suffix = QWORD_MNEM_SUFFIX;
7082 else
7083 continue;
7084 break;
7085 }
7086
7087 /* As an exception, movsx/movzx silently default to a byte source
7088 in AT&T mode. */
7089 if (is_movx && i.tm.opcode_modifier.w && !i.suffix && !intel_syntax)
7090 i.suffix = BYTE_MNEM_SUFFIX;
7091 }
7092 else if (i.suffix == BYTE_MNEM_SUFFIX)
7093 {
7094 if (intel_syntax
7095 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
7096 && i.tm.opcode_modifier.no_bsuf)
7097 i.suffix = 0;
7098 else if (!check_byte_reg ())
7099 return 0;
7100 }
7101 else if (i.suffix == LONG_MNEM_SUFFIX)
7102 {
7103 if (intel_syntax
7104 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
7105 && i.tm.opcode_modifier.no_lsuf
7106 && !i.tm.opcode_modifier.todword
7107 && !i.tm.opcode_modifier.toqword)
7108 i.suffix = 0;
7109 else if (!check_long_reg ())
7110 return 0;
7111 }
7112 else if (i.suffix == QWORD_MNEM_SUFFIX)
7113 {
7114 if (intel_syntax
7115 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
7116 && i.tm.opcode_modifier.no_qsuf
7117 && !i.tm.opcode_modifier.todword
7118 && !i.tm.opcode_modifier.toqword)
7119 i.suffix = 0;
7120 else if (!check_qword_reg ())
7121 return 0;
7122 }
7123 else if (i.suffix == WORD_MNEM_SUFFIX)
7124 {
7125 if (intel_syntax
7126 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
7127 && i.tm.opcode_modifier.no_wsuf)
7128 i.suffix = 0;
7129 else if (!check_word_reg ())
7130 return 0;
7131 }
7132 else if (intel_syntax
7133 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
7134 /* Do nothing if the instruction is going to ignore the prefix. */
7135 ;
7136 else
7137 abort ();
7138
7139 /* Undo the movsx/movzx change done above. */
7140 i.operands = numop;
7141 }
7142 else if (i.tm.opcode_modifier.mnemonicsize == DEFAULTSIZE
7143 && !i.suffix)
7144 {
7145 i.suffix = stackop_size;
7146 if (stackop_size == LONG_MNEM_SUFFIX)
7147 {
7148 /* stackop_size is set to LONG_MNEM_SUFFIX for the
7149 .code16gcc directive to support 16-bit mode with
7150 32-bit address. For IRET without a suffix, generate
7151 16-bit IRET (opcode 0xcf) to return from an interrupt
7152 handler. */
7153 if (i.tm.base_opcode == 0xcf)
7154 {
7155 i.suffix = WORD_MNEM_SUFFIX;
7156 as_warn (_("generating 16-bit `iret' for .code16gcc directive"));
7157 }
7158 /* Warn about changed behavior for segment register push/pop. */
7159 else if ((i.tm.base_opcode | 1) == 0x07)
7160 as_warn (_("generating 32-bit `%s', unlike earlier gas versions"),
7161 i.tm.name);
7162 }
7163 }
7164 else if (!i.suffix
7165 && (i.tm.opcode_modifier.jump == JUMP_ABSOLUTE
7166 || i.tm.opcode_modifier.jump == JUMP_BYTE
7167 || i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT
7168 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
7169 && i.tm.base_opcode == 0x01 /* [ls][gi]dt */
7170 && i.tm.extension_opcode <= 3)))
7171 {
7172 switch (flag_code)
7173 {
7174 case CODE_64BIT:
7175 if (!i.tm.opcode_modifier.no_qsuf)
7176 {
7177 if (i.tm.opcode_modifier.jump == JUMP_BYTE
7178 || i.tm.opcode_modifier.no_lsuf)
7179 i.suffix = QWORD_MNEM_SUFFIX;
7180 break;
7181 }
7182 /* Fall through. */
7183 case CODE_32BIT:
7184 if (!i.tm.opcode_modifier.no_lsuf)
7185 i.suffix = LONG_MNEM_SUFFIX;
7186 break;
7187 case CODE_16BIT:
7188 if (!i.tm.opcode_modifier.no_wsuf)
7189 i.suffix = WORD_MNEM_SUFFIX;
7190 break;
7191 }
7192 }
7193
7194 if (!i.suffix
7195 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
7196 /* Also cover lret/retf/iret in 64-bit mode. */
7197 || (flag_code == CODE_64BIT
7198 && !i.tm.opcode_modifier.no_lsuf
7199 && !i.tm.opcode_modifier.no_qsuf))
7200 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
7201 /* Explicit sizing prefixes are assumed to disambiguate insns. */
7202 && !i.prefix[DATA_PREFIX] && !(i.prefix[REX_PREFIX] & REX_W)
7203 /* Accept FLDENV et al without suffix. */
7204 && (i.tm.opcode_modifier.no_ssuf || i.tm.opcode_modifier.floatmf))
7205 {
7206 unsigned int suffixes, evex = 0;
7207
7208 suffixes = !i.tm.opcode_modifier.no_bsuf;
7209 if (!i.tm.opcode_modifier.no_wsuf)
7210 suffixes |= 1 << 1;
7211 if (!i.tm.opcode_modifier.no_lsuf)
7212 suffixes |= 1 << 2;
7213 if (!i.tm.opcode_modifier.no_ldsuf)
7214 suffixes |= 1 << 3;
7215 if (!i.tm.opcode_modifier.no_ssuf)
7216 suffixes |= 1 << 4;
7217 if (flag_code == CODE_64BIT && !i.tm.opcode_modifier.no_qsuf)
7218 suffixes |= 1 << 5;
7219
7220 /* For [XYZ]MMWORD operands inspect operand sizes. While generally
7221 also suitable for AT&T syntax mode, it was requested that this be
7222 restricted to just Intel syntax. */
7223 if (intel_syntax && is_any_vex_encoding (&i.tm) && !i.broadcast.type)
7224 {
7225 unsigned int op;
7226
7227 for (op = 0; op < i.tm.operands; ++op)
7228 {
7229 if (is_evex_encoding (&i.tm)
7230 && !cpu_arch_flags.bitfield.cpuavx512vl)
7231 {
7232 if (i.tm.operand_types[op].bitfield.ymmword)
7233 i.tm.operand_types[op].bitfield.xmmword = 0;
7234 if (i.tm.operand_types[op].bitfield.zmmword)
7235 i.tm.operand_types[op].bitfield.ymmword = 0;
7236 if (!i.tm.opcode_modifier.evex
7237 || i.tm.opcode_modifier.evex == EVEXDYN)
7238 i.tm.opcode_modifier.evex = EVEX512;
7239 }
7240
7241 if (i.tm.operand_types[op].bitfield.xmmword
7242 + i.tm.operand_types[op].bitfield.ymmword
7243 + i.tm.operand_types[op].bitfield.zmmword < 2)
7244 continue;
7245
7246 /* Any properly sized operand disambiguates the insn. */
7247 if (i.types[op].bitfield.xmmword
7248 || i.types[op].bitfield.ymmword
7249 || i.types[op].bitfield.zmmword)
7250 {
7251 suffixes &= ~(7 << 6);
7252 evex = 0;
7253 break;
7254 }
7255
7256 if ((i.flags[op] & Operand_Mem)
7257 && i.tm.operand_types[op].bitfield.unspecified)
7258 {
7259 if (i.tm.operand_types[op].bitfield.xmmword)
7260 suffixes |= 1 << 6;
7261 if (i.tm.operand_types[op].bitfield.ymmword)
7262 suffixes |= 1 << 7;
7263 if (i.tm.operand_types[op].bitfield.zmmword)
7264 suffixes |= 1 << 8;
7265 if (is_evex_encoding (&i.tm))
7266 evex = EVEX512;
7267 }
7268 }
7269 }
7270
7271 /* Are multiple suffixes / operand sizes allowed? */
7272 if (suffixes & (suffixes - 1))
7273 {
7274 if (intel_syntax
7275 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
7276 || operand_check == check_error))
7277 {
7278 as_bad (_("ambiguous operand size for `%s'"), i.tm.name);
7279 return 0;
7280 }
7281 if (operand_check == check_error)
7282 {
7283 as_bad (_("no instruction mnemonic suffix given and "
7284 "no register operands; can't size `%s'"), i.tm.name);
7285 return 0;
7286 }
7287 if (operand_check == check_warning)
7288 as_warn (_("%s; using default for `%s'"),
7289 intel_syntax
7290 ? _("ambiguous operand size")
7291 : _("no instruction mnemonic suffix given and "
7292 "no register operands"),
7293 i.tm.name);
7294
7295 if (i.tm.opcode_modifier.floatmf)
7296 i.suffix = SHORT_MNEM_SUFFIX;
7297 else if (is_movx)
7298 /* handled below */;
7299 else if (evex)
7300 i.tm.opcode_modifier.evex = evex;
7301 else if (flag_code == CODE_16BIT)
7302 i.suffix = WORD_MNEM_SUFFIX;
7303 else if (!i.tm.opcode_modifier.no_lsuf)
7304 i.suffix = LONG_MNEM_SUFFIX;
7305 else
7306 i.suffix = QWORD_MNEM_SUFFIX;
7307 }
7308 }
7309
7310 if (is_movx)
7311 {
7312 /* In Intel syntax, movsx/movzx must have a "suffix" (checked above).
7313 In AT&T syntax, if there is no suffix (warned about above), the default
7314 will be byte extension. */
7315 if (i.tm.opcode_modifier.w && i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
7316 i.tm.base_opcode |= 1;
7317
7318 /* For further processing, the suffix should represent the destination
7319 (register). This is already the case when one was used with
7320 mov[sz][bw]*, but we need to replace it for mov[sz]x, or if there was
7321 no suffix to begin with. */
7322 if (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63 || !i.suffix)
7323 {
7324 if (i.types[1].bitfield.word)
7325 i.suffix = WORD_MNEM_SUFFIX;
7326 else if (i.types[1].bitfield.qword)
7327 i.suffix = QWORD_MNEM_SUFFIX;
7328 else
7329 i.suffix = LONG_MNEM_SUFFIX;
7330
7331 i.tm.opcode_modifier.w = 0;
7332 }
7333 }
7334
7335 if (!i.tm.opcode_modifier.modrm && i.reg_operands && i.tm.operands < 3)
7336 i.short_form = (i.tm.operand_types[0].bitfield.class == Reg)
7337 != (i.tm.operand_types[1].bitfield.class == Reg);
7338
7339 /* Change the opcode based on the operand size given by i.suffix. */
7340 switch (i.suffix)
7341 {
7342 /* Size floating point instruction. */
7343 case LONG_MNEM_SUFFIX:
7344 if (i.tm.opcode_modifier.floatmf)
7345 {
7346 i.tm.base_opcode ^= 4;
7347 break;
7348 }
7349 /* fall through */
7350 case WORD_MNEM_SUFFIX:
7351 case QWORD_MNEM_SUFFIX:
7352 /* It's not a byte, select word/dword operation. */
7353 if (i.tm.opcode_modifier.w)
7354 {
7355 if (i.short_form)
7356 i.tm.base_opcode |= 8;
7357 else
7358 i.tm.base_opcode |= 1;
7359 }
7360 /* fall through */
7361 case SHORT_MNEM_SUFFIX:
7362 /* Now select between word & dword operations via the operand
7363 size prefix, except for instructions that will ignore this
7364 prefix anyway. */
7365 if (i.suffix != QWORD_MNEM_SUFFIX
7366 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
7367 && !i.tm.opcode_modifier.floatmf
7368 && !is_any_vex_encoding (&i.tm)
7369 && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
7370 || (flag_code == CODE_64BIT
7371 && i.tm.opcode_modifier.jump == JUMP_BYTE)))
7372 {
7373 unsigned int prefix = DATA_PREFIX_OPCODE;
7374
7375 if (i.tm.opcode_modifier.jump == JUMP_BYTE) /* jcxz, loop */
7376 prefix = ADDR_PREFIX_OPCODE;
7377
7378 if (!add_prefix (prefix))
7379 return 0;
7380 }
7381
7382 /* Set mode64 for an operand. */
7383 if (i.suffix == QWORD_MNEM_SUFFIX
7384 && flag_code == CODE_64BIT
7385 && !i.tm.opcode_modifier.norex64
7386 && !i.tm.opcode_modifier.vexw
7387 /* Special case for xchg %rax,%rax. It is NOP and doesn't
7388 need rex64. */
7389 && ! (i.operands == 2
7390 && i.tm.base_opcode == 0x90
7391 && i.tm.extension_opcode == None
7392 && i.types[0].bitfield.instance == Accum
7393 && i.types[0].bitfield.qword
7394 && i.types[1].bitfield.instance == Accum
7395 && i.types[1].bitfield.qword))
7396 i.rex |= REX_W;
7397
7398 break;
7399
7400 case 0:
7401 /* Select word/dword/qword operation with explicit data sizing prefix
7402 when there are no suitable register operands. */
7403 if (i.tm.opcode_modifier.w
7404 && (i.prefix[DATA_PREFIX] || (i.prefix[REX_PREFIX] & REX_W))
7405 && (!i.reg_operands
7406 || (i.reg_operands == 1
7407 /* ShiftCount */
7408 && (i.tm.operand_types[0].bitfield.instance == RegC
7409 /* InOutPortReg */
7410 || i.tm.operand_types[0].bitfield.instance == RegD
7411 || i.tm.operand_types[1].bitfield.instance == RegD
7412 /* CRC32 */
7413 || is_crc32))))
7414 i.tm.base_opcode |= 1;
7415 break;
7416 }
7417
7418 if (i.tm.opcode_modifier.addrprefixopreg)
7419 {
7420 gas_assert (!i.suffix);
7421 gas_assert (i.reg_operands);
7422
7423 if (i.tm.operand_types[0].bitfield.instance == Accum
7424 || i.operands == 1)
7425 {
7426 /* The address size override prefix changes the size of the
7427 first operand. */
7428 if (flag_code == CODE_64BIT
7429 && i.op[0].regs->reg_type.bitfield.word)
7430 {
7431 as_bad (_("16-bit addressing unavailable for `%s'"),
7432 i.tm.name);
7433 return 0;
7434 }
7435
7436 if ((flag_code == CODE_32BIT
7437 ? i.op[0].regs->reg_type.bitfield.word
7438 : i.op[0].regs->reg_type.bitfield.dword)
7439 && !add_prefix (ADDR_PREFIX_OPCODE))
7440 return 0;
7441 }
7442 else
7443 {
7444 /* Check invalid register operand when the address size override
7445 prefix changes the size of register operands. */
7446 unsigned int op;
7447 enum { need_word, need_dword, need_qword } need;
7448
7449 /* Check the register operand for the address size prefix if
7450 the memory operand has no real registers, like symbol, DISP
7451 or bogus (x32-only) symbol(%rip) when symbol(%eip) is meant. */
7452 if (i.mem_operands == 1
7453 && i.reg_operands == 1
7454 && i.operands == 2
7455 && i.types[1].bitfield.class == Reg
7456 && (flag_code == CODE_32BIT
7457 ? i.op[1].regs->reg_type.bitfield.word
7458 : i.op[1].regs->reg_type.bitfield.dword)
7459 && ((i.base_reg == NULL && i.index_reg == NULL)
7460 #if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
7461 || (x86_elf_abi == X86_64_X32_ABI
7462 && i.base_reg
7463 && i.base_reg->reg_num == RegIP
7464 && i.base_reg->reg_type.bitfield.qword))
7465 #else
7466 || 0)
7467 #endif
7468 && !add_prefix (ADDR_PREFIX_OPCODE))
7469 return 0;
7470
7471 if (flag_code == CODE_32BIT)
7472 need = i.prefix[ADDR_PREFIX] ? need_word : need_dword;
7473 else if (i.prefix[ADDR_PREFIX])
7474 need = need_dword;
7475 else
7476 need = flag_code == CODE_64BIT ? need_qword : need_word;
7477
7478 for (op = 0; op < i.operands; op++)
7479 {
7480 if (i.types[op].bitfield.class != Reg)
7481 continue;
7482
7483 switch (need)
7484 {
7485 case need_word:
7486 if (i.op[op].regs->reg_type.bitfield.word)
7487 continue;
7488 break;
7489 case need_dword:
7490 if (i.op[op].regs->reg_type.bitfield.dword)
7491 continue;
7492 break;
7493 case need_qword:
7494 if (i.op[op].regs->reg_type.bitfield.qword)
7495 continue;
7496 break;
7497 }
7498
7499 as_bad (_("invalid register operand size for `%s'"),
7500 i.tm.name);
7501 return 0;
7502 }
7503 }
7504 }
7505
7506 return 1;
7507 }
7508
7509 static int
7510 check_byte_reg (void)
7511 {
7512 int op;
7513
7514 for (op = i.operands; --op >= 0;)
7515 {
7516 /* Skip non-register operands. */
7517 if (i.types[op].bitfield.class != Reg)
7518 continue;
7519
7520 /* If this is an eight bit register, it's OK. If it's the 16 or
7521 32 bit version of an eight bit register, we will just use the
7522 low portion, and that's OK too. */
7523 if (i.types[op].bitfield.byte)
7524 continue;
7525
7526 /* I/O port address operands are OK too. */
7527 if (i.tm.operand_types[op].bitfield.instance == RegD
7528 && i.tm.operand_types[op].bitfield.word)
7529 continue;
7530
7531 /* crc32 only wants its source operand checked here. */
7532 if (i.tm.base_opcode == 0xf0
7533 && i.tm.opcode_modifier.opcodespace == SPACE_0F38
7534 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2
7535 && op != 0)
7536 continue;
7537
7538 /* Any other register is bad. */
7539 as_bad (_("`%s%s' not allowed with `%s%c'"),
7540 register_prefix, i.op[op].regs->reg_name,
7541 i.tm.name, i.suffix);
7542 return 0;
7543 }
7544 return 1;
7545 }
7546
7547 static int
7548 check_long_reg (void)
7549 {
7550 int op;
7551
7552 for (op = i.operands; --op >= 0;)
7553 /* Skip non-register operands. */
7554 if (i.types[op].bitfield.class != Reg)
7555 continue;
7556 /* Reject eight bit registers, except where the template requires
7557 them. (eg. movzb) */
7558 else if (i.types[op].bitfield.byte
7559 && (i.tm.operand_types[op].bitfield.class == Reg
7560 || i.tm.operand_types[op].bitfield.instance == Accum)
7561 && (i.tm.operand_types[op].bitfield.word
7562 || i.tm.operand_types[op].bitfield.dword))
7563 {
7564 as_bad (_("`%s%s' not allowed with `%s%c'"),
7565 register_prefix,
7566 i.op[op].regs->reg_name,
7567 i.tm.name,
7568 i.suffix);
7569 return 0;
7570 }
7571 /* Error if the e prefix on a general reg is missing. */
7572 else if (i.types[op].bitfield.word
7573 && (i.tm.operand_types[op].bitfield.class == Reg
7574 || i.tm.operand_types[op].bitfield.instance == Accum)
7575 && i.tm.operand_types[op].bitfield.dword)
7576 {
7577 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7578 register_prefix, i.op[op].regs->reg_name,
7579 i.suffix);
7580 return 0;
7581 }
7582 /* Warn if the r prefix on a general reg is present. */
7583 else if (i.types[op].bitfield.qword
7584 && (i.tm.operand_types[op].bitfield.class == Reg
7585 || i.tm.operand_types[op].bitfield.instance == Accum)
7586 && i.tm.operand_types[op].bitfield.dword)
7587 {
7588 if (intel_syntax
7589 && i.tm.opcode_modifier.toqword
7590 && i.types[0].bitfield.class != RegSIMD)
7591 {
7592 /* Convert to QWORD. We want REX byte. */
7593 i.suffix = QWORD_MNEM_SUFFIX;
7594 }
7595 else
7596 {
7597 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7598 register_prefix, i.op[op].regs->reg_name,
7599 i.suffix);
7600 return 0;
7601 }
7602 }
7603 return 1;
7604 }
7605
7606 static int
7607 check_qword_reg (void)
7608 {
7609 int op;
7610
7611 for (op = i.operands; --op >= 0; )
7612 /* Skip non-register operands. */
7613 if (i.types[op].bitfield.class != Reg)
7614 continue;
7615 /* Reject eight bit registers, except where the template requires
7616 them. (eg. movzb) */
7617 else if (i.types[op].bitfield.byte
7618 && (i.tm.operand_types[op].bitfield.class == Reg
7619 || i.tm.operand_types[op].bitfield.instance == Accum)
7620 && (i.tm.operand_types[op].bitfield.word
7621 || i.tm.operand_types[op].bitfield.dword))
7622 {
7623 as_bad (_("`%s%s' not allowed with `%s%c'"),
7624 register_prefix,
7625 i.op[op].regs->reg_name,
7626 i.tm.name,
7627 i.suffix);
7628 return 0;
7629 }
7630 /* Warn if the r prefix on a general reg is missing. */
7631 else if ((i.types[op].bitfield.word
7632 || i.types[op].bitfield.dword)
7633 && (i.tm.operand_types[op].bitfield.class == Reg
7634 || i.tm.operand_types[op].bitfield.instance == Accum)
7635 && i.tm.operand_types[op].bitfield.qword)
7636 {
7637 /* Prohibit these changes in the 64bit mode, since the
7638 lowering is more complicated. */
7639 if (intel_syntax
7640 && i.tm.opcode_modifier.todword
7641 && i.types[0].bitfield.class != RegSIMD)
7642 {
7643 /* Convert to DWORD. We don't want REX byte. */
7644 i.suffix = LONG_MNEM_SUFFIX;
7645 }
7646 else
7647 {
7648 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7649 register_prefix, i.op[op].regs->reg_name,
7650 i.suffix);
7651 return 0;
7652 }
7653 }
7654 return 1;
7655 }
7656
7657 static int
7658 check_word_reg (void)
7659 {
7660 int op;
7661 for (op = i.operands; --op >= 0;)
7662 /* Skip non-register operands. */
7663 if (i.types[op].bitfield.class != Reg)
7664 continue;
7665 /* Reject eight bit registers, except where the template requires
7666 them. (eg. movzb) */
7667 else if (i.types[op].bitfield.byte
7668 && (i.tm.operand_types[op].bitfield.class == Reg
7669 || i.tm.operand_types[op].bitfield.instance == Accum)
7670 && (i.tm.operand_types[op].bitfield.word
7671 || i.tm.operand_types[op].bitfield.dword))
7672 {
7673 as_bad (_("`%s%s' not allowed with `%s%c'"),
7674 register_prefix,
7675 i.op[op].regs->reg_name,
7676 i.tm.name,
7677 i.suffix);
7678 return 0;
7679 }
7680 /* Error if the e or r prefix on a general reg is present. */
7681 else if ((i.types[op].bitfield.dword
7682 || i.types[op].bitfield.qword)
7683 && (i.tm.operand_types[op].bitfield.class == Reg
7684 || i.tm.operand_types[op].bitfield.instance == Accum)
7685 && i.tm.operand_types[op].bitfield.word)
7686 {
7687 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7688 register_prefix, i.op[op].regs->reg_name,
7689 i.suffix);
7690 return 0;
7691 }
7692 /* For some instructions need encode as EVEX.W=1 without explicit VexW1. */
7693 else if (i.types[op].bitfield.qword
7694 && intel_syntax
7695 && i.tm.opcode_modifier.toqword)
7696 {
7697 /* Convert to QWORD. We want EVEX.W byte. */
7698 i.suffix = QWORD_MNEM_SUFFIX;
7699 }
7700 return 1;
7701 }
7702
7703 static int
7704 update_imm (unsigned int j)
7705 {
7706 i386_operand_type overlap = i.types[j];
7707 if ((overlap.bitfield.imm8
7708 || overlap.bitfield.imm8s
7709 || overlap.bitfield.imm16
7710 || overlap.bitfield.imm32
7711 || overlap.bitfield.imm32s
7712 || overlap.bitfield.imm64)
7713 && !operand_type_equal (&overlap, &imm8)
7714 && !operand_type_equal (&overlap, &imm8s)
7715 && !operand_type_equal (&overlap, &imm16)
7716 && !operand_type_equal (&overlap, &imm32)
7717 && !operand_type_equal (&overlap, &imm32s)
7718 && !operand_type_equal (&overlap, &imm64))
7719 {
7720 if (i.suffix)
7721 {
7722 i386_operand_type temp;
7723
7724 operand_type_set (&temp, 0);
7725 if (i.suffix == BYTE_MNEM_SUFFIX)
7726 {
7727 temp.bitfield.imm8 = overlap.bitfield.imm8;
7728 temp.bitfield.imm8s = overlap.bitfield.imm8s;
7729 }
7730 else if (i.suffix == WORD_MNEM_SUFFIX)
7731 temp.bitfield.imm16 = overlap.bitfield.imm16;
7732 else if (i.suffix == QWORD_MNEM_SUFFIX)
7733 {
7734 temp.bitfield.imm64 = overlap.bitfield.imm64;
7735 temp.bitfield.imm32s = overlap.bitfield.imm32s;
7736 }
7737 else
7738 temp.bitfield.imm32 = overlap.bitfield.imm32;
7739 overlap = temp;
7740 }
7741 else if (operand_type_equal (&overlap, &imm16_32_32s)
7742 || operand_type_equal (&overlap, &imm16_32)
7743 || operand_type_equal (&overlap, &imm16_32s))
7744 {
7745 if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
7746 overlap = imm16;
7747 else
7748 overlap = imm32s;
7749 }
7750 else if (i.prefix[REX_PREFIX] & REX_W)
7751 overlap = operand_type_and (overlap, imm32s);
7752 else if (i.prefix[DATA_PREFIX])
7753 overlap = operand_type_and (overlap,
7754 flag_code != CODE_16BIT ? imm16 : imm32);
7755 if (!operand_type_equal (&overlap, &imm8)
7756 && !operand_type_equal (&overlap, &imm8s)
7757 && !operand_type_equal (&overlap, &imm16)
7758 && !operand_type_equal (&overlap, &imm32)
7759 && !operand_type_equal (&overlap, &imm32s)
7760 && !operand_type_equal (&overlap, &imm64))
7761 {
7762 as_bad (_("no instruction mnemonic suffix given; "
7763 "can't determine immediate size"));
7764 return 0;
7765 }
7766 }
7767 i.types[j] = overlap;
7768
7769 return 1;
7770 }
7771
7772 static int
7773 finalize_imm (void)
7774 {
7775 unsigned int j, n;
7776
7777 /* Update the first 2 immediate operands. */
7778 n = i.operands > 2 ? 2 : i.operands;
7779 if (n)
7780 {
7781 for (j = 0; j < n; j++)
7782 if (update_imm (j) == 0)
7783 return 0;
7784
7785 /* The 3rd operand can't be immediate operand. */
7786 gas_assert (operand_type_check (i.types[2], imm) == 0);
7787 }
7788
7789 return 1;
7790 }
7791
7792 static int
7793 process_operands (void)
7794 {
7795 /* Default segment register this instruction will use for memory
7796 accesses. 0 means unknown. This is only for optimizing out
7797 unnecessary segment overrides. */
7798 const reg_entry *default_seg = NULL;
7799
7800 if (i.tm.opcode_modifier.sse2avx)
7801 {
7802 /* Legacy encoded insns allow explicit REX prefixes, so these prefixes
7803 need converting. */
7804 i.rex |= i.prefix[REX_PREFIX] & (REX_W | REX_R | REX_X | REX_B);
7805 i.prefix[REX_PREFIX] = 0;
7806 i.rex_encoding = 0;
7807 }
7808 /* ImmExt should be processed after SSE2AVX. */
7809 else if (i.tm.opcode_modifier.immext)
7810 process_immext ();
7811
7812 if (i.tm.opcode_modifier.sse2avx && i.tm.opcode_modifier.vexvvvv)
7813 {
7814 unsigned int dupl = i.operands;
7815 unsigned int dest = dupl - 1;
7816 unsigned int j;
7817
7818 /* The destination must be an xmm register. */
7819 gas_assert (i.reg_operands
7820 && MAX_OPERANDS > dupl
7821 && operand_type_equal (&i.types[dest], &regxmm));
7822
7823 if (i.tm.operand_types[0].bitfield.instance == Accum
7824 && i.tm.operand_types[0].bitfield.xmmword)
7825 {
7826 if (i.tm.opcode_modifier.vexsources == VEX3SOURCES)
7827 {
7828 /* Keep xmm0 for instructions with VEX prefix and 3
7829 sources. */
7830 i.tm.operand_types[0].bitfield.instance = InstanceNone;
7831 i.tm.operand_types[0].bitfield.class = RegSIMD;
7832 goto duplicate;
7833 }
7834 else
7835 {
7836 /* We remove the first xmm0 and keep the number of
7837 operands unchanged, which in fact duplicates the
7838 destination. */
7839 for (j = 1; j < i.operands; j++)
7840 {
7841 i.op[j - 1] = i.op[j];
7842 i.types[j - 1] = i.types[j];
7843 i.tm.operand_types[j - 1] = i.tm.operand_types[j];
7844 i.flags[j - 1] = i.flags[j];
7845 }
7846 }
7847 }
7848 else if (i.tm.opcode_modifier.implicit1stxmm0)
7849 {
7850 gas_assert ((MAX_OPERANDS - 1) > dupl
7851 && (i.tm.opcode_modifier.vexsources
7852 == VEX3SOURCES));
7853
7854 /* Add the implicit xmm0 for instructions with VEX prefix
7855 and 3 sources. */
7856 for (j = i.operands; j > 0; j--)
7857 {
7858 i.op[j] = i.op[j - 1];
7859 i.types[j] = i.types[j - 1];
7860 i.tm.operand_types[j] = i.tm.operand_types[j - 1];
7861 i.flags[j] = i.flags[j - 1];
7862 }
7863 i.op[0].regs
7864 = (const reg_entry *) str_hash_find (reg_hash, "xmm0");
7865 i.types[0] = regxmm;
7866 i.tm.operand_types[0] = regxmm;
7867
7868 i.operands += 2;
7869 i.reg_operands += 2;
7870 i.tm.operands += 2;
7871
7872 dupl++;
7873 dest++;
7874 i.op[dupl] = i.op[dest];
7875 i.types[dupl] = i.types[dest];
7876 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
7877 i.flags[dupl] = i.flags[dest];
7878 }
7879 else
7880 {
7881 duplicate:
7882 i.operands++;
7883 i.reg_operands++;
7884 i.tm.operands++;
7885
7886 i.op[dupl] = i.op[dest];
7887 i.types[dupl] = i.types[dest];
7888 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
7889 i.flags[dupl] = i.flags[dest];
7890 }
7891
7892 if (i.tm.opcode_modifier.immext)
7893 process_immext ();
7894 }
7895 else if (i.tm.operand_types[0].bitfield.instance == Accum
7896 && i.tm.operand_types[0].bitfield.xmmword)
7897 {
7898 unsigned int j;
7899
7900 for (j = 1; j < i.operands; j++)
7901 {
7902 i.op[j - 1] = i.op[j];
7903 i.types[j - 1] = i.types[j];
7904
7905 /* We need to adjust fields in i.tm since they are used by
7906 build_modrm_byte. */
7907 i.tm.operand_types [j - 1] = i.tm.operand_types [j];
7908
7909 i.flags[j - 1] = i.flags[j];
7910 }
7911
7912 i.operands--;
7913 i.reg_operands--;
7914 i.tm.operands--;
7915 }
7916 else if (i.tm.opcode_modifier.implicitquadgroup)
7917 {
7918 unsigned int regnum, first_reg_in_group, last_reg_in_group;
7919
7920 /* The second operand must be {x,y,z}mmN, where N is a multiple of 4. */
7921 gas_assert (i.operands >= 2 && i.types[1].bitfield.class == RegSIMD);
7922 regnum = register_number (i.op[1].regs);
7923 first_reg_in_group = regnum & ~3;
7924 last_reg_in_group = first_reg_in_group + 3;
7925 if (regnum != first_reg_in_group)
7926 as_warn (_("source register `%s%s' implicitly denotes"
7927 " `%s%.3s%u' to `%s%.3s%u' source group in `%s'"),
7928 register_prefix, i.op[1].regs->reg_name,
7929 register_prefix, i.op[1].regs->reg_name, first_reg_in_group,
7930 register_prefix, i.op[1].regs->reg_name, last_reg_in_group,
7931 i.tm.name);
7932 }
7933 else if (i.tm.opcode_modifier.regkludge)
7934 {
7935 /* The imul $imm, %reg instruction is converted into
7936 imul $imm, %reg, %reg, and the clr %reg instruction
7937 is converted into xor %reg, %reg. */
7938
7939 unsigned int first_reg_op;
7940
7941 if (operand_type_check (i.types[0], reg))
7942 first_reg_op = 0;
7943 else
7944 first_reg_op = 1;
7945 /* Pretend we saw the extra register operand. */
7946 gas_assert (i.reg_operands == 1
7947 && i.op[first_reg_op + 1].regs == 0);
7948 i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
7949 i.types[first_reg_op + 1] = i.types[first_reg_op];
7950 i.operands++;
7951 i.reg_operands++;
7952 }
7953
7954 if (i.tm.opcode_modifier.modrm)
7955 {
7956 /* The opcode is completed (modulo i.tm.extension_opcode which
7957 must be put into the modrm byte). Now, we make the modrm and
7958 index base bytes based on all the info we've collected. */
7959
7960 default_seg = build_modrm_byte ();
7961 }
7962 else if (i.types[0].bitfield.class == SReg)
7963 {
7964 if (flag_code != CODE_64BIT
7965 ? i.tm.base_opcode == POP_SEG_SHORT
7966 && i.op[0].regs->reg_num == 1
7967 : (i.tm.base_opcode | 1) == (POP_SEG386_SHORT & 0xff)
7968 && i.op[0].regs->reg_num < 4)
7969 {
7970 as_bad (_("you can't `%s %s%s'"),
7971 i.tm.name, register_prefix, i.op[0].regs->reg_name);
7972 return 0;
7973 }
7974 if (i.op[0].regs->reg_num > 3
7975 && i.tm.opcode_modifier.opcodespace == SPACE_BASE )
7976 {
7977 i.tm.base_opcode ^= (POP_SEG_SHORT ^ POP_SEG386_SHORT) & 0xff;
7978 i.tm.opcode_modifier.opcodespace = SPACE_0F;
7979 }
7980 i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
7981 }
7982 else if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
7983 && (i.tm.base_opcode & ~3) == MOV_AX_DISP32)
7984 {
7985 default_seg = reg_ds;
7986 }
7987 else if (i.tm.opcode_modifier.isstring)
7988 {
7989 /* For the string instructions that allow a segment override
7990 on one of their operands, the default segment is ds. */
7991 default_seg = reg_ds;
7992 }
7993 else if (i.short_form)
7994 {
7995 /* The register or float register operand is in operand
7996 0 or 1. */
7997 unsigned int op = i.tm.operand_types[0].bitfield.class != Reg;
7998
7999 /* Register goes in low 3 bits of opcode. */
8000 i.tm.base_opcode |= i.op[op].regs->reg_num;
8001 if ((i.op[op].regs->reg_flags & RegRex) != 0)
8002 i.rex |= REX_B;
8003 if (!quiet_warnings && i.tm.opcode_modifier.ugh)
8004 {
8005 /* Warn about some common errors, but press on regardless.
8006 The first case can be generated by gcc (<= 2.8.1). */
8007 if (i.operands == 2)
8008 {
8009 /* Reversed arguments on faddp, fsubp, etc. */
8010 as_warn (_("translating to `%s %s%s,%s%s'"), i.tm.name,
8011 register_prefix, i.op[!intel_syntax].regs->reg_name,
8012 register_prefix, i.op[intel_syntax].regs->reg_name);
8013 }
8014 else
8015 {
8016 /* Extraneous `l' suffix on fp insn. */
8017 as_warn (_("translating to `%s %s%s'"), i.tm.name,
8018 register_prefix, i.op[0].regs->reg_name);
8019 }
8020 }
8021 }
8022
8023 if ((i.seg[0] || i.prefix[SEG_PREFIX])
8024 && i.tm.base_opcode == 0x8d /* lea */
8025 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
8026 && !is_any_vex_encoding(&i.tm))
8027 {
8028 if (!quiet_warnings)
8029 as_warn (_("segment override on `%s' is ineffectual"), i.tm.name);
8030 if (optimize)
8031 {
8032 i.seg[0] = NULL;
8033 i.prefix[SEG_PREFIX] = 0;
8034 }
8035 }
8036
8037 /* If a segment was explicitly specified, and the specified segment
8038 is neither the default nor the one already recorded from a prefix,
8039 use an opcode prefix to select it. If we never figured out what
8040 the default segment is, then default_seg will be zero at this
8041 point, and the specified segment prefix will always be used. */
8042 if (i.seg[0]
8043 && i.seg[0] != default_seg
8044 && i386_seg_prefixes[i.seg[0]->reg_num] != i.prefix[SEG_PREFIX])
8045 {
8046 if (!add_prefix (i386_seg_prefixes[i.seg[0]->reg_num]))
8047 return 0;
8048 }
8049 return 1;
8050 }
8051
8052 static INLINE void set_rex_vrex (const reg_entry *r, unsigned int rex_bit,
8053 bool do_sse2avx)
8054 {
8055 if (r->reg_flags & RegRex)
8056 {
8057 if (i.rex & rex_bit)
8058 as_bad (_("same type of prefix used twice"));
8059 i.rex |= rex_bit;
8060 }
8061 else if (do_sse2avx && (i.rex & rex_bit) && i.vex.register_specifier)
8062 {
8063 gas_assert (i.vex.register_specifier == r);
8064 i.vex.register_specifier += 8;
8065 }
8066
8067 if (r->reg_flags & RegVRex)
8068 i.vrex |= rex_bit;
8069 }
8070
8071 static const reg_entry *
8072 build_modrm_byte (void)
8073 {
8074 const reg_entry *default_seg = NULL;
8075 unsigned int source, dest;
8076 int vex_3_sources;
8077
8078 vex_3_sources = i.tm.opcode_modifier.vexsources == VEX3SOURCES;
8079 if (vex_3_sources)
8080 {
8081 unsigned int nds, reg_slot;
8082 expressionS *exp;
8083
8084 dest = i.operands - 1;
8085 nds = dest - 1;
8086
8087 /* There are 2 kinds of instructions:
8088 1. 5 operands: 4 register operands or 3 register operands
8089 plus 1 memory operand plus one Imm4 operand, VexXDS, and
8090 VexW0 or VexW1. The destination must be either XMM, YMM or
8091 ZMM register.
8092 2. 4 operands: 4 register operands or 3 register operands
8093 plus 1 memory operand, with VexXDS. */
8094 gas_assert ((i.reg_operands == 4
8095 || (i.reg_operands == 3 && i.mem_operands == 1))
8096 && i.tm.opcode_modifier.vexvvvv == VEXXDS
8097 && i.tm.opcode_modifier.vexw
8098 && i.tm.operand_types[dest].bitfield.class == RegSIMD);
8099
8100 /* If VexW1 is set, the first non-immediate operand is the source and
8101 the second non-immediate one is encoded in the immediate operand. */
8102 if (i.tm.opcode_modifier.vexw == VEXW1)
8103 {
8104 source = i.imm_operands;
8105 reg_slot = i.imm_operands + 1;
8106 }
8107 else
8108 {
8109 source = i.imm_operands + 1;
8110 reg_slot = i.imm_operands;
8111 }
8112
8113 if (i.imm_operands == 0)
8114 {
8115 /* When there is no immediate operand, generate an 8bit
8116 immediate operand to encode the first operand. */
8117 exp = &im_expressions[i.imm_operands++];
8118 i.op[i.operands].imms = exp;
8119 i.types[i.operands] = imm8;
8120 i.operands++;
8121
8122 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
8123 exp->X_op = O_constant;
8124 exp->X_add_number = register_number (i.op[reg_slot].regs) << 4;
8125 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
8126 }
8127 else
8128 {
8129 gas_assert (i.imm_operands == 1);
8130 gas_assert (fits_in_imm4 (i.op[0].imms->X_add_number));
8131 gas_assert (!i.tm.opcode_modifier.immext);
8132
8133 /* Turn on Imm8 again so that output_imm will generate it. */
8134 i.types[0].bitfield.imm8 = 1;
8135
8136 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
8137 i.op[0].imms->X_add_number
8138 |= register_number (i.op[reg_slot].regs) << 4;
8139 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
8140 }
8141
8142 gas_assert (i.tm.operand_types[nds].bitfield.class == RegSIMD);
8143 i.vex.register_specifier = i.op[nds].regs;
8144 }
8145 else
8146 source = dest = 0;
8147
8148 /* i.reg_operands MUST be the number of real register operands;
8149 implicit registers do not count. If there are 3 register
8150 operands, it must be a instruction with VexNDS. For a
8151 instruction with VexNDD, the destination register is encoded
8152 in VEX prefix. If there are 4 register operands, it must be
8153 a instruction with VEX prefix and 3 sources. */
8154 if (i.mem_operands == 0
8155 && ((i.reg_operands == 2
8156 && i.tm.opcode_modifier.vexvvvv <= VEXXDS)
8157 || (i.reg_operands == 3
8158 && i.tm.opcode_modifier.vexvvvv == VEXXDS)
8159 || (i.reg_operands == 4 && vex_3_sources)))
8160 {
8161 switch (i.operands)
8162 {
8163 case 2:
8164 source = 0;
8165 break;
8166 case 3:
8167 /* When there are 3 operands, one of them may be immediate,
8168 which may be the first or the last operand. Otherwise,
8169 the first operand must be shift count register (cl) or it
8170 is an instruction with VexNDS. */
8171 gas_assert (i.imm_operands == 1
8172 || (i.imm_operands == 0
8173 && (i.tm.opcode_modifier.vexvvvv == VEXXDS
8174 || (i.types[0].bitfield.instance == RegC
8175 && i.types[0].bitfield.byte))));
8176 if (operand_type_check (i.types[0], imm)
8177 || (i.types[0].bitfield.instance == RegC
8178 && i.types[0].bitfield.byte))
8179 source = 1;
8180 else
8181 source = 0;
8182 break;
8183 case 4:
8184 /* When there are 4 operands, the first two must be 8bit
8185 immediate operands. The source operand will be the 3rd
8186 one.
8187
8188 For instructions with VexNDS, if the first operand
8189 an imm8, the source operand is the 2nd one. If the last
8190 operand is imm8, the source operand is the first one. */
8191 gas_assert ((i.imm_operands == 2
8192 && i.types[0].bitfield.imm8
8193 && i.types[1].bitfield.imm8)
8194 || (i.tm.opcode_modifier.vexvvvv == VEXXDS
8195 && i.imm_operands == 1
8196 && (i.types[0].bitfield.imm8
8197 || i.types[i.operands - 1].bitfield.imm8
8198 || i.rounding.type != rc_none)));
8199 if (i.imm_operands == 2)
8200 source = 2;
8201 else
8202 {
8203 if (i.types[0].bitfield.imm8)
8204 source = 1;
8205 else
8206 source = 0;
8207 }
8208 break;
8209 case 5:
8210 if (is_evex_encoding (&i.tm))
8211 {
8212 /* For EVEX instructions, when there are 5 operands, the
8213 first one must be immediate operand. If the second one
8214 is immediate operand, the source operand is the 3th
8215 one. If the last one is immediate operand, the source
8216 operand is the 2nd one. */
8217 gas_assert (i.imm_operands == 2
8218 && i.tm.opcode_modifier.sae
8219 && operand_type_check (i.types[0], imm));
8220 if (operand_type_check (i.types[1], imm))
8221 source = 2;
8222 else if (operand_type_check (i.types[4], imm))
8223 source = 1;
8224 else
8225 abort ();
8226 }
8227 break;
8228 default:
8229 abort ();
8230 }
8231
8232 if (!vex_3_sources)
8233 {
8234 dest = source + 1;
8235
8236 /* RC/SAE operand could be between DEST and SRC. That happens
8237 when one operand is GPR and the other one is XMM/YMM/ZMM
8238 register. */
8239 if (i.rounding.type != rc_none && i.rounding.operand == dest)
8240 dest++;
8241
8242 if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
8243 {
8244 /* For instructions with VexNDS, the register-only source
8245 operand must be a 32/64bit integer, XMM, YMM, ZMM, or mask
8246 register. It is encoded in VEX prefix. */
8247
8248 i386_operand_type op;
8249 unsigned int vvvv;
8250
8251 /* Swap two source operands if needed. */
8252 if (i.tm.opcode_modifier.swapsources)
8253 {
8254 vvvv = source;
8255 source = dest;
8256 }
8257 else
8258 vvvv = dest;
8259
8260 op = i.tm.operand_types[vvvv];
8261 if ((dest + 1) >= i.operands
8262 || ((op.bitfield.class != Reg
8263 || (!op.bitfield.dword && !op.bitfield.qword))
8264 && op.bitfield.class != RegSIMD
8265 && !operand_type_equal (&op, &regmask)))
8266 abort ();
8267 i.vex.register_specifier = i.op[vvvv].regs;
8268 dest++;
8269 }
8270 }
8271
8272 i.rm.mode = 3;
8273 /* One of the register operands will be encoded in the i.rm.reg
8274 field, the other in the combined i.rm.mode and i.rm.regmem
8275 fields. If no form of this instruction supports a memory
8276 destination operand, then we assume the source operand may
8277 sometimes be a memory operand and so we need to store the
8278 destination in the i.rm.reg field. */
8279 if (!i.tm.opcode_modifier.regmem
8280 && operand_type_check (i.tm.operand_types[dest], anymem) == 0)
8281 {
8282 i.rm.reg = i.op[dest].regs->reg_num;
8283 i.rm.regmem = i.op[source].regs->reg_num;
8284 set_rex_vrex (i.op[dest].regs, REX_R, i.tm.opcode_modifier.sse2avx);
8285 set_rex_vrex (i.op[source].regs, REX_B, false);
8286 }
8287 else
8288 {
8289 i.rm.reg = i.op[source].regs->reg_num;
8290 i.rm.regmem = i.op[dest].regs->reg_num;
8291 set_rex_vrex (i.op[dest].regs, REX_B, i.tm.opcode_modifier.sse2avx);
8292 set_rex_vrex (i.op[source].regs, REX_R, false);
8293 }
8294 if (flag_code != CODE_64BIT && (i.rex & REX_R))
8295 {
8296 if (i.types[!i.tm.opcode_modifier.regmem].bitfield.class != RegCR)
8297 abort ();
8298 i.rex &= ~REX_R;
8299 add_prefix (LOCK_PREFIX_OPCODE);
8300 }
8301 }
8302 else
8303 { /* If it's not 2 reg operands... */
8304 unsigned int mem;
8305
8306 if (i.mem_operands)
8307 {
8308 unsigned int fake_zero_displacement = 0;
8309 unsigned int op;
8310
8311 for (op = 0; op < i.operands; op++)
8312 if (i.flags[op] & Operand_Mem)
8313 break;
8314 gas_assert (op < i.operands);
8315
8316 if (i.tm.opcode_modifier.sib)
8317 {
8318 /* The index register of VSIB shouldn't be RegIZ. */
8319 if (i.tm.opcode_modifier.sib != SIBMEM
8320 && i.index_reg->reg_num == RegIZ)
8321 abort ();
8322
8323 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8324 if (!i.base_reg)
8325 {
8326 i.sib.base = NO_BASE_REGISTER;
8327 i.sib.scale = i.log2_scale_factor;
8328 i.types[op] = operand_type_and_not (i.types[op], anydisp);
8329 if (want_disp32 (&i.tm))
8330 i.types[op].bitfield.disp32 = 1;
8331 else
8332 i.types[op].bitfield.disp32s = 1;
8333 }
8334
8335 /* Since the mandatory SIB always has index register, so
8336 the code logic remains unchanged. The non-mandatory SIB
8337 without index register is allowed and will be handled
8338 later. */
8339 if (i.index_reg)
8340 {
8341 if (i.index_reg->reg_num == RegIZ)
8342 i.sib.index = NO_INDEX_REGISTER;
8343 else
8344 i.sib.index = i.index_reg->reg_num;
8345 set_rex_vrex (i.index_reg, REX_X, false);
8346 }
8347 }
8348
8349 default_seg = reg_ds;
8350
8351 if (i.base_reg == 0)
8352 {
8353 i.rm.mode = 0;
8354 if (!i.disp_operands)
8355 fake_zero_displacement = 1;
8356 if (i.index_reg == 0)
8357 {
8358 /* Both check for VSIB and mandatory non-vector SIB. */
8359 gas_assert (!i.tm.opcode_modifier.sib
8360 || i.tm.opcode_modifier.sib == SIBMEM);
8361 /* Operand is just <disp> */
8362 i.types[op] = operand_type_and_not (i.types[op], anydisp);
8363 if (flag_code == CODE_64BIT)
8364 {
8365 /* 64bit mode overwrites the 32bit absolute
8366 addressing by RIP relative addressing and
8367 absolute addressing is encoded by one of the
8368 redundant SIB forms. */
8369 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8370 i.sib.base = NO_BASE_REGISTER;
8371 i.sib.index = NO_INDEX_REGISTER;
8372 if (want_disp32 (&i.tm))
8373 i.types[op].bitfield.disp32 = 1;
8374 else
8375 i.types[op].bitfield.disp32s = 1;
8376 }
8377 else if ((flag_code == CODE_16BIT)
8378 ^ (i.prefix[ADDR_PREFIX] != 0))
8379 {
8380 i.rm.regmem = NO_BASE_REGISTER_16;
8381 i.types[op].bitfield.disp16 = 1;
8382 }
8383 else
8384 {
8385 i.rm.regmem = NO_BASE_REGISTER;
8386 i.types[op].bitfield.disp32 = 1;
8387 }
8388 }
8389 else if (!i.tm.opcode_modifier.sib)
8390 {
8391 /* !i.base_reg && i.index_reg */
8392 if (i.index_reg->reg_num == RegIZ)
8393 i.sib.index = NO_INDEX_REGISTER;
8394 else
8395 i.sib.index = i.index_reg->reg_num;
8396 i.sib.base = NO_BASE_REGISTER;
8397 i.sib.scale = i.log2_scale_factor;
8398 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8399 i.types[op] = operand_type_and_not (i.types[op], anydisp);
8400 if (want_disp32 (&i.tm))
8401 i.types[op].bitfield.disp32 = 1;
8402 else
8403 i.types[op].bitfield.disp32s = 1;
8404 if ((i.index_reg->reg_flags & RegRex) != 0)
8405 i.rex |= REX_X;
8406 }
8407 }
8408 /* RIP addressing for 64bit mode. */
8409 else if (i.base_reg->reg_num == RegIP)
8410 {
8411 gas_assert (!i.tm.opcode_modifier.sib);
8412 i.rm.regmem = NO_BASE_REGISTER;
8413 i.types[op].bitfield.disp8 = 0;
8414 i.types[op].bitfield.disp16 = 0;
8415 i.types[op].bitfield.disp32 = 0;
8416 i.types[op].bitfield.disp32s = 1;
8417 i.types[op].bitfield.disp64 = 0;
8418 i.flags[op] |= Operand_PCrel;
8419 if (! i.disp_operands)
8420 fake_zero_displacement = 1;
8421 }
8422 else if (i.base_reg->reg_type.bitfield.word)
8423 {
8424 gas_assert (!i.tm.opcode_modifier.sib);
8425 switch (i.base_reg->reg_num)
8426 {
8427 case 3: /* (%bx) */
8428 if (i.index_reg == 0)
8429 i.rm.regmem = 7;
8430 else /* (%bx,%si) -> 0, or (%bx,%di) -> 1 */
8431 i.rm.regmem = i.index_reg->reg_num - 6;
8432 break;
8433 case 5: /* (%bp) */
8434 default_seg = reg_ss;
8435 if (i.index_reg == 0)
8436 {
8437 i.rm.regmem = 6;
8438 if (operand_type_check (i.types[op], disp) == 0)
8439 {
8440 /* fake (%bp) into 0(%bp) */
8441 if (i.disp_encoding == disp_encoding_16bit)
8442 i.types[op].bitfield.disp16 = 1;
8443 else
8444 i.types[op].bitfield.disp8 = 1;
8445 fake_zero_displacement = 1;
8446 }
8447 }
8448 else /* (%bp,%si) -> 2, or (%bp,%di) -> 3 */
8449 i.rm.regmem = i.index_reg->reg_num - 6 + 2;
8450 break;
8451 default: /* (%si) -> 4 or (%di) -> 5 */
8452 i.rm.regmem = i.base_reg->reg_num - 6 + 4;
8453 }
8454 if (!fake_zero_displacement
8455 && !i.disp_operands
8456 && i.disp_encoding)
8457 {
8458 fake_zero_displacement = 1;
8459 if (i.disp_encoding == disp_encoding_8bit)
8460 i.types[op].bitfield.disp8 = 1;
8461 else
8462 i.types[op].bitfield.disp16 = 1;
8463 }
8464 i.rm.mode = mode_from_disp_size (i.types[op]);
8465 }
8466 else /* i.base_reg and 32/64 bit mode */
8467 {
8468 if (operand_type_check (i.types[op], disp))
8469 {
8470 i.types[op].bitfield.disp16 = 0;
8471 i.types[op].bitfield.disp64 = 0;
8472 if (!want_disp32 (&i.tm))
8473 {
8474 i.types[op].bitfield.disp32 = 0;
8475 i.types[op].bitfield.disp32s = 1;
8476 }
8477 else
8478 {
8479 i.types[op].bitfield.disp32 = 1;
8480 i.types[op].bitfield.disp32s = 0;
8481 }
8482 }
8483
8484 if (!i.tm.opcode_modifier.sib)
8485 i.rm.regmem = i.base_reg->reg_num;
8486 if ((i.base_reg->reg_flags & RegRex) != 0)
8487 i.rex |= REX_B;
8488 i.sib.base = i.base_reg->reg_num;
8489 /* x86-64 ignores REX prefix bit here to avoid decoder
8490 complications. */
8491 if (!(i.base_reg->reg_flags & RegRex)
8492 && (i.base_reg->reg_num == EBP_REG_NUM
8493 || i.base_reg->reg_num == ESP_REG_NUM))
8494 default_seg = reg_ss;
8495 if (i.base_reg->reg_num == 5 && i.disp_operands == 0)
8496 {
8497 fake_zero_displacement = 1;
8498 if (i.disp_encoding == disp_encoding_32bit)
8499 i.types[op].bitfield.disp32 = 1;
8500 else
8501 i.types[op].bitfield.disp8 = 1;
8502 }
8503 i.sib.scale = i.log2_scale_factor;
8504 if (i.index_reg == 0)
8505 {
8506 /* Only check for VSIB. */
8507 gas_assert (i.tm.opcode_modifier.sib != VECSIB128
8508 && i.tm.opcode_modifier.sib != VECSIB256
8509 && i.tm.opcode_modifier.sib != VECSIB512);
8510
8511 /* <disp>(%esp) becomes two byte modrm with no index
8512 register. We've already stored the code for esp
8513 in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
8514 Any base register besides %esp will not use the
8515 extra modrm byte. */
8516 i.sib.index = NO_INDEX_REGISTER;
8517 }
8518 else if (!i.tm.opcode_modifier.sib)
8519 {
8520 if (i.index_reg->reg_num == RegIZ)
8521 i.sib.index = NO_INDEX_REGISTER;
8522 else
8523 i.sib.index = i.index_reg->reg_num;
8524 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8525 if ((i.index_reg->reg_flags & RegRex) != 0)
8526 i.rex |= REX_X;
8527 }
8528
8529 if (i.disp_operands
8530 && (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
8531 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL))
8532 i.rm.mode = 0;
8533 else
8534 {
8535 if (!fake_zero_displacement
8536 && !i.disp_operands
8537 && i.disp_encoding)
8538 {
8539 fake_zero_displacement = 1;
8540 if (i.disp_encoding == disp_encoding_8bit)
8541 i.types[op].bitfield.disp8 = 1;
8542 else
8543 i.types[op].bitfield.disp32 = 1;
8544 }
8545 i.rm.mode = mode_from_disp_size (i.types[op]);
8546 }
8547 }
8548
8549 if (fake_zero_displacement)
8550 {
8551 /* Fakes a zero displacement assuming that i.types[op]
8552 holds the correct displacement size. */
8553 expressionS *exp;
8554
8555 gas_assert (i.op[op].disps == 0);
8556 exp = &disp_expressions[i.disp_operands++];
8557 i.op[op].disps = exp;
8558 exp->X_op = O_constant;
8559 exp->X_add_number = 0;
8560 exp->X_add_symbol = (symbolS *) 0;
8561 exp->X_op_symbol = (symbolS *) 0;
8562 }
8563
8564 mem = op;
8565 }
8566 else
8567 mem = ~0;
8568
8569 if (i.tm.opcode_modifier.vexsources == XOP2SOURCES)
8570 {
8571 if (operand_type_check (i.types[0], imm))
8572 i.vex.register_specifier = NULL;
8573 else
8574 {
8575 /* VEX.vvvv encodes one of the sources when the first
8576 operand is not an immediate. */
8577 if (i.tm.opcode_modifier.vexw == VEXW0)
8578 i.vex.register_specifier = i.op[0].regs;
8579 else
8580 i.vex.register_specifier = i.op[1].regs;
8581 }
8582
8583 /* Destination is a XMM register encoded in the ModRM.reg
8584 and VEX.R bit. */
8585 i.rm.reg = i.op[2].regs->reg_num;
8586 if ((i.op[2].regs->reg_flags & RegRex) != 0)
8587 i.rex |= REX_R;
8588
8589 /* ModRM.rm and VEX.B encodes the other source. */
8590 if (!i.mem_operands)
8591 {
8592 i.rm.mode = 3;
8593
8594 if (i.tm.opcode_modifier.vexw == VEXW0)
8595 i.rm.regmem = i.op[1].regs->reg_num;
8596 else
8597 i.rm.regmem = i.op[0].regs->reg_num;
8598
8599 if ((i.op[1].regs->reg_flags & RegRex) != 0)
8600 i.rex |= REX_B;
8601 }
8602 }
8603 else if (i.tm.opcode_modifier.vexvvvv == VEXLWP)
8604 {
8605 i.vex.register_specifier = i.op[2].regs;
8606 if (!i.mem_operands)
8607 {
8608 i.rm.mode = 3;
8609 i.rm.regmem = i.op[1].regs->reg_num;
8610 if ((i.op[1].regs->reg_flags & RegRex) != 0)
8611 i.rex |= REX_B;
8612 }
8613 }
8614 /* Fill in i.rm.reg or i.rm.regmem field with register operand
8615 (if any) based on i.tm.extension_opcode. Again, we must be
8616 careful to make sure that segment/control/debug/test/MMX
8617 registers are coded into the i.rm.reg field. */
8618 else if (i.reg_operands)
8619 {
8620 unsigned int op;
8621 unsigned int vex_reg = ~0;
8622
8623 for (op = 0; op < i.operands; op++)
8624 if (i.types[op].bitfield.class == Reg
8625 || i.types[op].bitfield.class == RegBND
8626 || i.types[op].bitfield.class == RegMask
8627 || i.types[op].bitfield.class == SReg
8628 || i.types[op].bitfield.class == RegCR
8629 || i.types[op].bitfield.class == RegDR
8630 || i.types[op].bitfield.class == RegTR
8631 || i.types[op].bitfield.class == RegSIMD
8632 || i.types[op].bitfield.class == RegMMX)
8633 break;
8634
8635 if (vex_3_sources)
8636 op = dest;
8637 else if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
8638 {
8639 /* For instructions with VexNDS, the register-only
8640 source operand is encoded in VEX prefix. */
8641 gas_assert (mem != (unsigned int) ~0);
8642
8643 if (op > mem)
8644 {
8645 vex_reg = op++;
8646 gas_assert (op < i.operands);
8647 }
8648 else
8649 {
8650 /* Check register-only source operand when two source
8651 operands are swapped. */
8652 if (!i.tm.operand_types[op].bitfield.baseindex
8653 && i.tm.operand_types[op + 1].bitfield.baseindex)
8654 {
8655 vex_reg = op;
8656 op += 2;
8657 gas_assert (mem == (vex_reg + 1)
8658 && op < i.operands);
8659 }
8660 else
8661 {
8662 vex_reg = op + 1;
8663 gas_assert (vex_reg < i.operands);
8664 }
8665 }
8666 }
8667 else if (i.tm.opcode_modifier.vexvvvv == VEXNDD)
8668 {
8669 /* For instructions with VexNDD, the register destination
8670 is encoded in VEX prefix. */
8671 if (i.mem_operands == 0)
8672 {
8673 /* There is no memory operand. */
8674 gas_assert ((op + 2) == i.operands);
8675 vex_reg = op + 1;
8676 }
8677 else
8678 {
8679 /* There are only 2 non-immediate operands. */
8680 gas_assert (op < i.imm_operands + 2
8681 && i.operands == i.imm_operands + 2);
8682 vex_reg = i.imm_operands + 1;
8683 }
8684 }
8685 else
8686 gas_assert (op < i.operands);
8687
8688 if (vex_reg != (unsigned int) ~0)
8689 {
8690 i386_operand_type *type = &i.tm.operand_types[vex_reg];
8691
8692 if ((type->bitfield.class != Reg
8693 || (!type->bitfield.dword && !type->bitfield.qword))
8694 && type->bitfield.class != RegSIMD
8695 && !operand_type_equal (type, &regmask))
8696 abort ();
8697
8698 i.vex.register_specifier = i.op[vex_reg].regs;
8699 }
8700
8701 /* Don't set OP operand twice. */
8702 if (vex_reg != op)
8703 {
8704 /* If there is an extension opcode to put here, the
8705 register number must be put into the regmem field. */
8706 if (i.tm.extension_opcode != None)
8707 {
8708 i.rm.regmem = i.op[op].regs->reg_num;
8709 set_rex_vrex (i.op[op].regs, REX_B,
8710 i.tm.opcode_modifier.sse2avx);
8711 }
8712 else
8713 {
8714 i.rm.reg = i.op[op].regs->reg_num;
8715 set_rex_vrex (i.op[op].regs, REX_R,
8716 i.tm.opcode_modifier.sse2avx);
8717 }
8718 }
8719
8720 /* Now, if no memory operand has set i.rm.mode = 0, 1, 2 we
8721 must set it to 3 to indicate this is a register operand
8722 in the regmem field. */
8723 if (!i.mem_operands)
8724 i.rm.mode = 3;
8725 }
8726
8727 /* Fill in i.rm.reg field with extension opcode (if any). */
8728 if (i.tm.extension_opcode != None)
8729 i.rm.reg = i.tm.extension_opcode;
8730 }
8731 return default_seg;
8732 }
8733
8734 static INLINE void
8735 frag_opcode_byte (unsigned char byte)
8736 {
8737 if (now_seg != absolute_section)
8738 FRAG_APPEND_1_CHAR (byte);
8739 else
8740 ++abs_section_offset;
8741 }
8742
8743 static unsigned int
8744 flip_code16 (unsigned int code16)
8745 {
8746 gas_assert (i.tm.operands == 1);
8747
8748 return !(i.prefix[REX_PREFIX] & REX_W)
8749 && (code16 ? i.tm.operand_types[0].bitfield.disp32
8750 || i.tm.operand_types[0].bitfield.disp32s
8751 : i.tm.operand_types[0].bitfield.disp16)
8752 ? CODE16 : 0;
8753 }
8754
8755 static void
8756 output_branch (void)
8757 {
8758 char *p;
8759 int size;
8760 int code16;
8761 int prefix;
8762 relax_substateT subtype;
8763 symbolS *sym;
8764 offsetT off;
8765
8766 if (now_seg == absolute_section)
8767 {
8768 as_bad (_("relaxable branches not supported in absolute section"));
8769 return;
8770 }
8771
8772 code16 = flag_code == CODE_16BIT ? CODE16 : 0;
8773 size = i.disp_encoding == disp_encoding_32bit ? BIG : SMALL;
8774
8775 prefix = 0;
8776 if (i.prefix[DATA_PREFIX] != 0)
8777 {
8778 prefix = 1;
8779 i.prefixes -= 1;
8780 code16 ^= flip_code16(code16);
8781 }
8782 /* Pentium4 branch hints. */
8783 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8784 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
8785 {
8786 prefix++;
8787 i.prefixes--;
8788 }
8789 if (i.prefix[REX_PREFIX] != 0)
8790 {
8791 prefix++;
8792 i.prefixes--;
8793 }
8794
8795 /* BND prefixed jump. */
8796 if (i.prefix[BND_PREFIX] != 0)
8797 {
8798 prefix++;
8799 i.prefixes--;
8800 }
8801
8802 if (i.prefixes != 0)
8803 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
8804
8805 /* It's always a symbol; End frag & setup for relax.
8806 Make sure there is enough room in this frag for the largest
8807 instruction we may generate in md_convert_frag. This is 2
8808 bytes for the opcode and room for the prefix and largest
8809 displacement. */
8810 frag_grow (prefix + 2 + 4);
8811 /* Prefix and 1 opcode byte go in fr_fix. */
8812 p = frag_more (prefix + 1);
8813 if (i.prefix[DATA_PREFIX] != 0)
8814 *p++ = DATA_PREFIX_OPCODE;
8815 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
8816 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
8817 *p++ = i.prefix[SEG_PREFIX];
8818 if (i.prefix[BND_PREFIX] != 0)
8819 *p++ = BND_PREFIX_OPCODE;
8820 if (i.prefix[REX_PREFIX] != 0)
8821 *p++ = i.prefix[REX_PREFIX];
8822 *p = i.tm.base_opcode;
8823
8824 if ((unsigned char) *p == JUMP_PC_RELATIVE)
8825 subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, size);
8826 else if (cpu_arch_flags.bitfield.cpui386)
8827 subtype = ENCODE_RELAX_STATE (COND_JUMP, size);
8828 else
8829 subtype = ENCODE_RELAX_STATE (COND_JUMP86, size);
8830 subtype |= code16;
8831
8832 sym = i.op[0].disps->X_add_symbol;
8833 off = i.op[0].disps->X_add_number;
8834
8835 if (i.op[0].disps->X_op != O_constant
8836 && i.op[0].disps->X_op != O_symbol)
8837 {
8838 /* Handle complex expressions. */
8839 sym = make_expr_symbol (i.op[0].disps);
8840 off = 0;
8841 }
8842
8843 frag_now->tc_frag_data.code64 = flag_code == CODE_64BIT;
8844
8845 /* 1 possible extra opcode + 4 byte displacement go in var part.
8846 Pass reloc in fr_var. */
8847 frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
8848 }
8849
8850 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8851 /* Return TRUE iff PLT32 relocation should be used for branching to
8852 symbol S. */
8853
8854 static bool
8855 need_plt32_p (symbolS *s)
8856 {
8857 /* PLT32 relocation is ELF only. */
8858 if (!IS_ELF)
8859 return false;
8860
8861 #ifdef TE_SOLARIS
8862 /* Don't emit PLT32 relocation on Solaris: neither native linker nor
8863 krtld support it. */
8864 return false;
8865 #endif
8866
8867 /* Since there is no need to prepare for PLT branch on x86-64, we
8868 can generate R_X86_64_PLT32, instead of R_X86_64_PC32, which can
8869 be used as a marker for 32-bit PC-relative branches. */
8870 if (!object_64bit)
8871 return false;
8872
8873 if (s == NULL)
8874 return false;
8875
8876 /* Weak or undefined symbol need PLT32 relocation. */
8877 if (S_IS_WEAK (s) || !S_IS_DEFINED (s))
8878 return true;
8879
8880 /* Non-global symbol doesn't need PLT32 relocation. */
8881 if (! S_IS_EXTERNAL (s))
8882 return false;
8883
8884 /* Other global symbols need PLT32 relocation. NB: Symbol with
8885 non-default visibilities are treated as normal global symbol
8886 so that PLT32 relocation can be used as a marker for 32-bit
8887 PC-relative branches. It is useful for linker relaxation. */
8888 return true;
8889 }
8890 #endif
8891
8892 static void
8893 output_jump (void)
8894 {
8895 char *p;
8896 int size;
8897 fixS *fixP;
8898 bfd_reloc_code_real_type jump_reloc = i.reloc[0];
8899
8900 if (i.tm.opcode_modifier.jump == JUMP_BYTE)
8901 {
8902 /* This is a loop or jecxz type instruction. */
8903 size = 1;
8904 if (i.prefix[ADDR_PREFIX] != 0)
8905 {
8906 frag_opcode_byte (ADDR_PREFIX_OPCODE);
8907 i.prefixes -= 1;
8908 }
8909 /* Pentium4 branch hints. */
8910 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8911 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
8912 {
8913 frag_opcode_byte (i.prefix[SEG_PREFIX]);
8914 i.prefixes--;
8915 }
8916 }
8917 else
8918 {
8919 int code16;
8920
8921 code16 = 0;
8922 if (flag_code == CODE_16BIT)
8923 code16 = CODE16;
8924
8925 if (i.prefix[DATA_PREFIX] != 0)
8926 {
8927 frag_opcode_byte (DATA_PREFIX_OPCODE);
8928 i.prefixes -= 1;
8929 code16 ^= flip_code16(code16);
8930 }
8931
8932 size = 4;
8933 if (code16)
8934 size = 2;
8935 }
8936
8937 /* BND prefixed jump. */
8938 if (i.prefix[BND_PREFIX] != 0)
8939 {
8940 frag_opcode_byte (i.prefix[BND_PREFIX]);
8941 i.prefixes -= 1;
8942 }
8943
8944 if (i.prefix[REX_PREFIX] != 0)
8945 {
8946 frag_opcode_byte (i.prefix[REX_PREFIX]);
8947 i.prefixes -= 1;
8948 }
8949
8950 if (i.prefixes != 0)
8951 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
8952
8953 if (now_seg == absolute_section)
8954 {
8955 abs_section_offset += i.opcode_length + size;
8956 return;
8957 }
8958
8959 p = frag_more (i.opcode_length + size);
8960 switch (i.opcode_length)
8961 {
8962 case 2:
8963 *p++ = i.tm.base_opcode >> 8;
8964 /* Fall through. */
8965 case 1:
8966 *p++ = i.tm.base_opcode;
8967 break;
8968 default:
8969 abort ();
8970 }
8971
8972 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8973 if (flag_code == CODE_64BIT && size == 4
8974 && jump_reloc == NO_RELOC && i.op[0].disps->X_add_number == 0
8975 && need_plt32_p (i.op[0].disps->X_add_symbol))
8976 jump_reloc = BFD_RELOC_X86_64_PLT32;
8977 #endif
8978
8979 jump_reloc = reloc (size, 1, 1, jump_reloc);
8980
8981 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
8982 i.op[0].disps, 1, jump_reloc);
8983
8984 /* All jumps handled here are signed, but don't unconditionally use a
8985 signed limit check for 32 and 16 bit jumps as we want to allow wrap
8986 around at 4G (outside of 64-bit mode) and 64k (except for XBEGIN)
8987 respectively. */
8988 switch (size)
8989 {
8990 case 1:
8991 fixP->fx_signed = 1;
8992 break;
8993
8994 case 2:
8995 if (i.tm.base_opcode == 0xc7f8)
8996 fixP->fx_signed = 1;
8997 break;
8998
8999 case 4:
9000 if (flag_code == CODE_64BIT)
9001 fixP->fx_signed = 1;
9002 break;
9003 }
9004 }
9005
9006 static void
9007 output_interseg_jump (void)
9008 {
9009 char *p;
9010 int size;
9011 int prefix;
9012 int code16;
9013
9014 code16 = 0;
9015 if (flag_code == CODE_16BIT)
9016 code16 = CODE16;
9017
9018 prefix = 0;
9019 if (i.prefix[DATA_PREFIX] != 0)
9020 {
9021 prefix = 1;
9022 i.prefixes -= 1;
9023 code16 ^= CODE16;
9024 }
9025
9026 gas_assert (!i.prefix[REX_PREFIX]);
9027
9028 size = 4;
9029 if (code16)
9030 size = 2;
9031
9032 if (i.prefixes != 0)
9033 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
9034
9035 if (now_seg == absolute_section)
9036 {
9037 abs_section_offset += prefix + 1 + 2 + size;
9038 return;
9039 }
9040
9041 /* 1 opcode; 2 segment; offset */
9042 p = frag_more (prefix + 1 + 2 + size);
9043
9044 if (i.prefix[DATA_PREFIX] != 0)
9045 *p++ = DATA_PREFIX_OPCODE;
9046
9047 if (i.prefix[REX_PREFIX] != 0)
9048 *p++ = i.prefix[REX_PREFIX];
9049
9050 *p++ = i.tm.base_opcode;
9051 if (i.op[1].imms->X_op == O_constant)
9052 {
9053 offsetT n = i.op[1].imms->X_add_number;
9054
9055 if (size == 2
9056 && !fits_in_unsigned_word (n)
9057 && !fits_in_signed_word (n))
9058 {
9059 as_bad (_("16-bit jump out of range"));
9060 return;
9061 }
9062 md_number_to_chars (p, n, size);
9063 }
9064 else
9065 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
9066 i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
9067
9068 p += size;
9069 if (i.op[0].imms->X_op == O_constant)
9070 md_number_to_chars (p, (valueT) i.op[0].imms->X_add_number, 2);
9071 else
9072 fix_new_exp (frag_now, p - frag_now->fr_literal, 2,
9073 i.op[0].imms, 0, reloc (2, 0, 0, i.reloc[0]));
9074 }
9075
9076 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9077 void
9078 x86_cleanup (void)
9079 {
9080 char *p;
9081 asection *seg = now_seg;
9082 subsegT subseg = now_subseg;
9083 asection *sec;
9084 unsigned int alignment, align_size_1;
9085 unsigned int isa_1_descsz, feature_2_descsz, descsz;
9086 unsigned int isa_1_descsz_raw, feature_2_descsz_raw;
9087 unsigned int padding;
9088
9089 if (!IS_ELF || !x86_used_note)
9090 return;
9091
9092 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X86;
9093
9094 /* The .note.gnu.property section layout:
9095
9096 Field Length Contents
9097 ---- ---- ----
9098 n_namsz 4 4
9099 n_descsz 4 The note descriptor size
9100 n_type 4 NT_GNU_PROPERTY_TYPE_0
9101 n_name 4 "GNU"
9102 n_desc n_descsz The program property array
9103 .... .... ....
9104 */
9105
9106 /* Create the .note.gnu.property section. */
9107 sec = subseg_new (NOTE_GNU_PROPERTY_SECTION_NAME, 0);
9108 bfd_set_section_flags (sec,
9109 (SEC_ALLOC
9110 | SEC_LOAD
9111 | SEC_DATA
9112 | SEC_HAS_CONTENTS
9113 | SEC_READONLY));
9114
9115 if (get_elf_backend_data (stdoutput)->s->elfclass == ELFCLASS64)
9116 {
9117 align_size_1 = 7;
9118 alignment = 3;
9119 }
9120 else
9121 {
9122 align_size_1 = 3;
9123 alignment = 2;
9124 }
9125
9126 bfd_set_section_alignment (sec, alignment);
9127 elf_section_type (sec) = SHT_NOTE;
9128
9129 /* GNU_PROPERTY_X86_ISA_1_USED: 4-byte type + 4-byte data size
9130 + 4-byte data */
9131 isa_1_descsz_raw = 4 + 4 + 4;
9132 /* Align GNU_PROPERTY_X86_ISA_1_USED. */
9133 isa_1_descsz = (isa_1_descsz_raw + align_size_1) & ~align_size_1;
9134
9135 feature_2_descsz_raw = isa_1_descsz;
9136 /* GNU_PROPERTY_X86_FEATURE_2_USED: 4-byte type + 4-byte data size
9137 + 4-byte data */
9138 feature_2_descsz_raw += 4 + 4 + 4;
9139 /* Align GNU_PROPERTY_X86_FEATURE_2_USED. */
9140 feature_2_descsz = ((feature_2_descsz_raw + align_size_1)
9141 & ~align_size_1);
9142
9143 descsz = feature_2_descsz;
9144 /* Section size: n_namsz + n_descsz + n_type + n_name + n_descsz. */
9145 p = frag_more (4 + 4 + 4 + 4 + descsz);
9146
9147 /* Write n_namsz. */
9148 md_number_to_chars (p, (valueT) 4, 4);
9149
9150 /* Write n_descsz. */
9151 md_number_to_chars (p + 4, (valueT) descsz, 4);
9152
9153 /* Write n_type. */
9154 md_number_to_chars (p + 4 * 2, (valueT) NT_GNU_PROPERTY_TYPE_0, 4);
9155
9156 /* Write n_name. */
9157 memcpy (p + 4 * 3, "GNU", 4);
9158
9159 /* Write 4-byte type. */
9160 md_number_to_chars (p + 4 * 4,
9161 (valueT) GNU_PROPERTY_X86_ISA_1_USED, 4);
9162
9163 /* Write 4-byte data size. */
9164 md_number_to_chars (p + 4 * 5, (valueT) 4, 4);
9165
9166 /* Write 4-byte data. */
9167 md_number_to_chars (p + 4 * 6, (valueT) x86_isa_1_used, 4);
9168
9169 /* Zero out paddings. */
9170 padding = isa_1_descsz - isa_1_descsz_raw;
9171 if (padding)
9172 memset (p + 4 * 7, 0, padding);
9173
9174 /* Write 4-byte type. */
9175 md_number_to_chars (p + isa_1_descsz + 4 * 4,
9176 (valueT) GNU_PROPERTY_X86_FEATURE_2_USED, 4);
9177
9178 /* Write 4-byte data size. */
9179 md_number_to_chars (p + isa_1_descsz + 4 * 5, (valueT) 4, 4);
9180
9181 /* Write 4-byte data. */
9182 md_number_to_chars (p + isa_1_descsz + 4 * 6,
9183 (valueT) x86_feature_2_used, 4);
9184
9185 /* Zero out paddings. */
9186 padding = feature_2_descsz - feature_2_descsz_raw;
9187 if (padding)
9188 memset (p + isa_1_descsz + 4 * 7, 0, padding);
9189
9190 /* We probably can't restore the current segment, for there likely
9191 isn't one yet... */
9192 if (seg && subseg)
9193 subseg_set (seg, subseg);
9194 }
9195 #endif
9196
9197 static unsigned int
9198 encoding_length (const fragS *start_frag, offsetT start_off,
9199 const char *frag_now_ptr)
9200 {
9201 unsigned int len = 0;
9202
9203 if (start_frag != frag_now)
9204 {
9205 const fragS *fr = start_frag;
9206
9207 do {
9208 len += fr->fr_fix;
9209 fr = fr->fr_next;
9210 } while (fr && fr != frag_now);
9211 }
9212
9213 return len - start_off + (frag_now_ptr - frag_now->fr_literal);
9214 }
9215
9216 /* Return 1 for test, and, cmp, add, sub, inc and dec which may
9217 be macro-fused with conditional jumps.
9218 NB: If TEST/AND/CMP/ADD/SUB/INC/DEC is of RIP relative address,
9219 or is one of the following format:
9220
9221 cmp m, imm
9222 add m, imm
9223 sub m, imm
9224 test m, imm
9225 and m, imm
9226 inc m
9227 dec m
9228
9229 it is unfusible. */
9230
9231 static int
9232 maybe_fused_with_jcc_p (enum mf_cmp_kind* mf_cmp_p)
9233 {
9234 /* No RIP address. */
9235 if (i.base_reg && i.base_reg->reg_num == RegIP)
9236 return 0;
9237
9238 /* No opcodes outside of base encoding space. */
9239 if (i.tm.opcode_modifier.opcodespace != SPACE_BASE)
9240 return 0;
9241
9242 /* add, sub without add/sub m, imm. */
9243 if (i.tm.base_opcode <= 5
9244 || (i.tm.base_opcode >= 0x28 && i.tm.base_opcode <= 0x2d)
9245 || ((i.tm.base_opcode | 3) == 0x83
9246 && (i.tm.extension_opcode == 0x5
9247 || i.tm.extension_opcode == 0x0)))
9248 {
9249 *mf_cmp_p = mf_cmp_alu_cmp;
9250 return !(i.mem_operands && i.imm_operands);
9251 }
9252
9253 /* and without and m, imm. */
9254 if ((i.tm.base_opcode >= 0x20 && i.tm.base_opcode <= 0x25)
9255 || ((i.tm.base_opcode | 3) == 0x83
9256 && i.tm.extension_opcode == 0x4))
9257 {
9258 *mf_cmp_p = mf_cmp_test_and;
9259 return !(i.mem_operands && i.imm_operands);
9260 }
9261
9262 /* test without test m imm. */
9263 if ((i.tm.base_opcode | 1) == 0x85
9264 || (i.tm.base_opcode | 1) == 0xa9
9265 || ((i.tm.base_opcode | 1) == 0xf7
9266 && i.tm.extension_opcode == 0))
9267 {
9268 *mf_cmp_p = mf_cmp_test_and;
9269 return !(i.mem_operands && i.imm_operands);
9270 }
9271
9272 /* cmp without cmp m, imm. */
9273 if ((i.tm.base_opcode >= 0x38 && i.tm.base_opcode <= 0x3d)
9274 || ((i.tm.base_opcode | 3) == 0x83
9275 && (i.tm.extension_opcode == 0x7)))
9276 {
9277 *mf_cmp_p = mf_cmp_alu_cmp;
9278 return !(i.mem_operands && i.imm_operands);
9279 }
9280
9281 /* inc, dec without inc/dec m. */
9282 if ((i.tm.cpu_flags.bitfield.cpuno64
9283 && (i.tm.base_opcode | 0xf) == 0x4f)
9284 || ((i.tm.base_opcode | 1) == 0xff
9285 && i.tm.extension_opcode <= 0x1))
9286 {
9287 *mf_cmp_p = mf_cmp_incdec;
9288 return !i.mem_operands;
9289 }
9290
9291 return 0;
9292 }
9293
9294 /* Return 1 if a FUSED_JCC_PADDING frag should be generated. */
9295
9296 static int
9297 add_fused_jcc_padding_frag_p (enum mf_cmp_kind* mf_cmp_p)
9298 {
9299 /* NB: Don't work with COND_JUMP86 without i386. */
9300 if (!align_branch_power
9301 || now_seg == absolute_section
9302 || !cpu_arch_flags.bitfield.cpui386
9303 || !(align_branch & align_branch_fused_bit))
9304 return 0;
9305
9306 if (maybe_fused_with_jcc_p (mf_cmp_p))
9307 {
9308 if (last_insn.kind == last_insn_other
9309 || last_insn.seg != now_seg)
9310 return 1;
9311 if (flag_debug)
9312 as_warn_where (last_insn.file, last_insn.line,
9313 _("`%s` skips -malign-branch-boundary on `%s`"),
9314 last_insn.name, i.tm.name);
9315 }
9316
9317 return 0;
9318 }
9319
9320 /* Return 1 if a BRANCH_PREFIX frag should be generated. */
9321
9322 static int
9323 add_branch_prefix_frag_p (void)
9324 {
9325 /* NB: Don't work with COND_JUMP86 without i386. Don't add prefix
9326 to PadLock instructions since they include prefixes in opcode. */
9327 if (!align_branch_power
9328 || !align_branch_prefix_size
9329 || now_seg == absolute_section
9330 || i.tm.cpu_flags.bitfield.cpupadlock
9331 || !cpu_arch_flags.bitfield.cpui386)
9332 return 0;
9333
9334 /* Don't add prefix if it is a prefix or there is no operand in case
9335 that segment prefix is special. */
9336 if (!i.operands || i.tm.opcode_modifier.isprefix)
9337 return 0;
9338
9339 if (last_insn.kind == last_insn_other
9340 || last_insn.seg != now_seg)
9341 return 1;
9342
9343 if (flag_debug)
9344 as_warn_where (last_insn.file, last_insn.line,
9345 _("`%s` skips -malign-branch-boundary on `%s`"),
9346 last_insn.name, i.tm.name);
9347
9348 return 0;
9349 }
9350
9351 /* Return 1 if a BRANCH_PADDING frag should be generated. */
9352
9353 static int
9354 add_branch_padding_frag_p (enum align_branch_kind *branch_p,
9355 enum mf_jcc_kind *mf_jcc_p)
9356 {
9357 int add_padding;
9358
9359 /* NB: Don't work with COND_JUMP86 without i386. */
9360 if (!align_branch_power
9361 || now_seg == absolute_section
9362 || !cpu_arch_flags.bitfield.cpui386
9363 || i.tm.opcode_modifier.opcodespace != SPACE_BASE)
9364 return 0;
9365
9366 add_padding = 0;
9367
9368 /* Check for jcc and direct jmp. */
9369 if (i.tm.opcode_modifier.jump == JUMP)
9370 {
9371 if (i.tm.base_opcode == JUMP_PC_RELATIVE)
9372 {
9373 *branch_p = align_branch_jmp;
9374 add_padding = align_branch & align_branch_jmp_bit;
9375 }
9376 else
9377 {
9378 /* Because J<cc> and JN<cc> share same group in macro-fusible table,
9379 igore the lowest bit. */
9380 *mf_jcc_p = (i.tm.base_opcode & 0x0e) >> 1;
9381 *branch_p = align_branch_jcc;
9382 if ((align_branch & align_branch_jcc_bit))
9383 add_padding = 1;
9384 }
9385 }
9386 else if ((i.tm.base_opcode | 1) == 0xc3)
9387 {
9388 /* Near ret. */
9389 *branch_p = align_branch_ret;
9390 if ((align_branch & align_branch_ret_bit))
9391 add_padding = 1;
9392 }
9393 else
9394 {
9395 /* Check for indirect jmp, direct and indirect calls. */
9396 if (i.tm.base_opcode == 0xe8)
9397 {
9398 /* Direct call. */
9399 *branch_p = align_branch_call;
9400 if ((align_branch & align_branch_call_bit))
9401 add_padding = 1;
9402 }
9403 else if (i.tm.base_opcode == 0xff
9404 && (i.tm.extension_opcode == 2
9405 || i.tm.extension_opcode == 4))
9406 {
9407 /* Indirect call and jmp. */
9408 *branch_p = align_branch_indirect;
9409 if ((align_branch & align_branch_indirect_bit))
9410 add_padding = 1;
9411 }
9412
9413 if (add_padding
9414 && i.disp_operands
9415 && tls_get_addr
9416 && (i.op[0].disps->X_op == O_symbol
9417 || (i.op[0].disps->X_op == O_subtract
9418 && i.op[0].disps->X_op_symbol == GOT_symbol)))
9419 {
9420 symbolS *s = i.op[0].disps->X_add_symbol;
9421 /* No padding to call to global or undefined tls_get_addr. */
9422 if ((S_IS_EXTERNAL (s) || !S_IS_DEFINED (s))
9423 && strcmp (S_GET_NAME (s), tls_get_addr) == 0)
9424 return 0;
9425 }
9426 }
9427
9428 if (add_padding
9429 && last_insn.kind != last_insn_other
9430 && last_insn.seg == now_seg)
9431 {
9432 if (flag_debug)
9433 as_warn_where (last_insn.file, last_insn.line,
9434 _("`%s` skips -malign-branch-boundary on `%s`"),
9435 last_insn.name, i.tm.name);
9436 return 0;
9437 }
9438
9439 return add_padding;
9440 }
9441
9442 static void
9443 output_insn (void)
9444 {
9445 fragS *insn_start_frag;
9446 offsetT insn_start_off;
9447 fragS *fragP = NULL;
9448 enum align_branch_kind branch = align_branch_none;
9449 /* The initializer is arbitrary just to avoid uninitialized error.
9450 it's actually either assigned in add_branch_padding_frag_p
9451 or never be used. */
9452 enum mf_jcc_kind mf_jcc = mf_jcc_jo;
9453
9454 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9455 if (IS_ELF && x86_used_note && now_seg != absolute_section)
9456 {
9457 if ((i.xstate & xstate_tmm) == xstate_tmm
9458 || i.tm.cpu_flags.bitfield.cpuamx_tile)
9459 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_TMM;
9460
9461 if (i.tm.cpu_flags.bitfield.cpu8087
9462 || i.tm.cpu_flags.bitfield.cpu287
9463 || i.tm.cpu_flags.bitfield.cpu387
9464 || i.tm.cpu_flags.bitfield.cpu687
9465 || i.tm.cpu_flags.bitfield.cpufisttp)
9466 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X87;
9467
9468 if ((i.xstate & xstate_mmx)
9469 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
9470 && !is_any_vex_encoding (&i.tm)
9471 && (i.tm.base_opcode == 0x77 /* emms */
9472 || i.tm.base_opcode == 0x0e /* femms */)))
9473 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MMX;
9474
9475 if (i.index_reg)
9476 {
9477 if (i.index_reg->reg_type.bitfield.zmmword)
9478 i.xstate |= xstate_zmm;
9479 else if (i.index_reg->reg_type.bitfield.ymmword)
9480 i.xstate |= xstate_ymm;
9481 else if (i.index_reg->reg_type.bitfield.xmmword)
9482 i.xstate |= xstate_xmm;
9483 }
9484
9485 /* vzeroall / vzeroupper */
9486 if (i.tm.base_opcode == 0x77 && i.tm.cpu_flags.bitfield.cpuavx)
9487 i.xstate |= xstate_ymm;
9488
9489 if ((i.xstate & xstate_xmm)
9490 /* ldmxcsr / stmxcsr / vldmxcsr / vstmxcsr */
9491 || (i.tm.base_opcode == 0xae
9492 && (i.tm.cpu_flags.bitfield.cpusse
9493 || i.tm.cpu_flags.bitfield.cpuavx))
9494 || i.tm.cpu_flags.bitfield.cpuwidekl
9495 || i.tm.cpu_flags.bitfield.cpukl)
9496 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XMM;
9497
9498 if ((i.xstate & xstate_ymm) == xstate_ymm)
9499 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_YMM;
9500 if ((i.xstate & xstate_zmm) == xstate_zmm)
9501 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_ZMM;
9502 if (i.mask.reg || (i.xstate & xstate_mask) == xstate_mask)
9503 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MASK;
9504 if (i.tm.cpu_flags.bitfield.cpufxsr)
9505 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_FXSR;
9506 if (i.tm.cpu_flags.bitfield.cpuxsave)
9507 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVE;
9508 if (i.tm.cpu_flags.bitfield.cpuxsaveopt)
9509 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT;
9510 if (i.tm.cpu_flags.bitfield.cpuxsavec)
9511 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEC;
9512
9513 if (x86_feature_2_used
9514 || i.tm.cpu_flags.bitfield.cpucmov
9515 || i.tm.cpu_flags.bitfield.cpusyscall
9516 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
9517 && i.tm.base_opcode == 0xc7
9518 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
9519 && i.tm.extension_opcode == 1) /* cmpxchg8b */)
9520 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_BASELINE;
9521 if (i.tm.cpu_flags.bitfield.cpusse3
9522 || i.tm.cpu_flags.bitfield.cpussse3
9523 || i.tm.cpu_flags.bitfield.cpusse4_1
9524 || i.tm.cpu_flags.bitfield.cpusse4_2
9525 || i.tm.cpu_flags.bitfield.cpucx16
9526 || i.tm.cpu_flags.bitfield.cpupopcnt
9527 /* LAHF-SAHF insns in 64-bit mode. */
9528 || (flag_code == CODE_64BIT
9529 && (i.tm.base_opcode | 1) == 0x9f
9530 && i.tm.opcode_modifier.opcodespace == SPACE_BASE))
9531 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V2;
9532 if (i.tm.cpu_flags.bitfield.cpuavx
9533 || i.tm.cpu_flags.bitfield.cpuavx2
9534 /* Any VEX encoded insns execpt for CpuAVX512F, CpuAVX512BW,
9535 CpuAVX512DQ, LPW, TBM and AMX. */
9536 || (i.tm.opcode_modifier.vex
9537 && !i.tm.cpu_flags.bitfield.cpuavx512f
9538 && !i.tm.cpu_flags.bitfield.cpuavx512bw
9539 && !i.tm.cpu_flags.bitfield.cpuavx512dq
9540 && !i.tm.cpu_flags.bitfield.cpulwp
9541 && !i.tm.cpu_flags.bitfield.cputbm
9542 && !(x86_feature_2_used & GNU_PROPERTY_X86_FEATURE_2_TMM))
9543 || i.tm.cpu_flags.bitfield.cpuf16c
9544 || i.tm.cpu_flags.bitfield.cpufma
9545 || i.tm.cpu_flags.bitfield.cpulzcnt
9546 || i.tm.cpu_flags.bitfield.cpumovbe
9547 || i.tm.cpu_flags.bitfield.cpuxsaves
9548 || (x86_feature_2_used
9549 & (GNU_PROPERTY_X86_FEATURE_2_XSAVE
9550 | GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT
9551 | GNU_PROPERTY_X86_FEATURE_2_XSAVEC)) != 0)
9552 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V3;
9553 if (i.tm.cpu_flags.bitfield.cpuavx512f
9554 || i.tm.cpu_flags.bitfield.cpuavx512bw
9555 || i.tm.cpu_flags.bitfield.cpuavx512dq
9556 || i.tm.cpu_flags.bitfield.cpuavx512vl
9557 /* Any EVEX encoded insns except for AVX512ER, AVX512PF and
9558 VNNIW. */
9559 || (i.tm.opcode_modifier.evex
9560 && !i.tm.cpu_flags.bitfield.cpuavx512er
9561 && !i.tm.cpu_flags.bitfield.cpuavx512pf
9562 && !i.tm.cpu_flags.bitfield.cpuavx512_4vnniw))
9563 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V4;
9564 }
9565 #endif
9566
9567 /* Tie dwarf2 debug info to the address at the start of the insn.
9568 We can't do this after the insn has been output as the current
9569 frag may have been closed off. eg. by frag_var. */
9570 dwarf2_emit_insn (0);
9571
9572 insn_start_frag = frag_now;
9573 insn_start_off = frag_now_fix ();
9574
9575 if (add_branch_padding_frag_p (&branch, &mf_jcc))
9576 {
9577 char *p;
9578 /* Branch can be 8 bytes. Leave some room for prefixes. */
9579 unsigned int max_branch_padding_size = 14;
9580
9581 /* Align section to boundary. */
9582 record_alignment (now_seg, align_branch_power);
9583
9584 /* Make room for padding. */
9585 frag_grow (max_branch_padding_size);
9586
9587 /* Start of the padding. */
9588 p = frag_more (0);
9589
9590 fragP = frag_now;
9591
9592 frag_var (rs_machine_dependent, max_branch_padding_size, 0,
9593 ENCODE_RELAX_STATE (BRANCH_PADDING, 0),
9594 NULL, 0, p);
9595
9596 fragP->tc_frag_data.mf_type = mf_jcc;
9597 fragP->tc_frag_data.branch_type = branch;
9598 fragP->tc_frag_data.max_bytes = max_branch_padding_size;
9599 }
9600
9601 /* Output jumps. */
9602 if (i.tm.opcode_modifier.jump == JUMP)
9603 output_branch ();
9604 else if (i.tm.opcode_modifier.jump == JUMP_BYTE
9605 || i.tm.opcode_modifier.jump == JUMP_DWORD)
9606 output_jump ();
9607 else if (i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT)
9608 output_interseg_jump ();
9609 else
9610 {
9611 /* Output normal instructions here. */
9612 char *p;
9613 unsigned char *q;
9614 unsigned int j;
9615 enum mf_cmp_kind mf_cmp;
9616
9617 if (avoid_fence
9618 && (i.tm.base_opcode == 0xaee8
9619 || i.tm.base_opcode == 0xaef0
9620 || i.tm.base_opcode == 0xaef8))
9621 {
9622 /* Encode lfence, mfence, and sfence as
9623 f0 83 04 24 00 lock addl $0x0, (%{re}sp). */
9624 if (flag_code == CODE_16BIT)
9625 as_bad (_("Cannot convert `%s' in 16-bit mode"), i.tm.name);
9626 else if (omit_lock_prefix)
9627 as_bad (_("Cannot convert `%s' with `-momit-lock-prefix=yes' in effect"),
9628 i.tm.name);
9629 else if (now_seg != absolute_section)
9630 {
9631 offsetT val = 0x240483f0ULL;
9632
9633 p = frag_more (5);
9634 md_number_to_chars (p, val, 5);
9635 }
9636 else
9637 abs_section_offset += 5;
9638 return;
9639 }
9640
9641 /* Some processors fail on LOCK prefix. This options makes
9642 assembler ignore LOCK prefix and serves as a workaround. */
9643 if (omit_lock_prefix)
9644 {
9645 if (i.tm.base_opcode == LOCK_PREFIX_OPCODE
9646 && i.tm.opcode_modifier.isprefix)
9647 return;
9648 i.prefix[LOCK_PREFIX] = 0;
9649 }
9650
9651 if (branch)
9652 /* Skip if this is a branch. */
9653 ;
9654 else if (add_fused_jcc_padding_frag_p (&mf_cmp))
9655 {
9656 /* Make room for padding. */
9657 frag_grow (MAX_FUSED_JCC_PADDING_SIZE);
9658 p = frag_more (0);
9659
9660 fragP = frag_now;
9661
9662 frag_var (rs_machine_dependent, MAX_FUSED_JCC_PADDING_SIZE, 0,
9663 ENCODE_RELAX_STATE (FUSED_JCC_PADDING, 0),
9664 NULL, 0, p);
9665
9666 fragP->tc_frag_data.mf_type = mf_cmp;
9667 fragP->tc_frag_data.branch_type = align_branch_fused;
9668 fragP->tc_frag_data.max_bytes = MAX_FUSED_JCC_PADDING_SIZE;
9669 }
9670 else if (add_branch_prefix_frag_p ())
9671 {
9672 unsigned int max_prefix_size = align_branch_prefix_size;
9673
9674 /* Make room for padding. */
9675 frag_grow (max_prefix_size);
9676 p = frag_more (0);
9677
9678 fragP = frag_now;
9679
9680 frag_var (rs_machine_dependent, max_prefix_size, 0,
9681 ENCODE_RELAX_STATE (BRANCH_PREFIX, 0),
9682 NULL, 0, p);
9683
9684 fragP->tc_frag_data.max_bytes = max_prefix_size;
9685 }
9686
9687 /* Since the VEX/EVEX prefix contains the implicit prefix, we
9688 don't need the explicit prefix. */
9689 if (!i.tm.opcode_modifier.vex && !i.tm.opcode_modifier.evex)
9690 {
9691 switch (i.tm.opcode_modifier.opcodeprefix)
9692 {
9693 case PREFIX_0X66:
9694 add_prefix (0x66);
9695 break;
9696 case PREFIX_0XF2:
9697 add_prefix (0xf2);
9698 break;
9699 case PREFIX_0XF3:
9700 if (!i.tm.cpu_flags.bitfield.cpupadlock
9701 || (i.prefix[REP_PREFIX] != 0xf3))
9702 add_prefix (0xf3);
9703 break;
9704 case PREFIX_NONE:
9705 switch (i.opcode_length)
9706 {
9707 case 2:
9708 break;
9709 case 1:
9710 /* Check for pseudo prefixes. */
9711 if (!i.tm.opcode_modifier.isprefix || i.tm.base_opcode)
9712 break;
9713 as_bad_where (insn_start_frag->fr_file,
9714 insn_start_frag->fr_line,
9715 _("pseudo prefix without instruction"));
9716 return;
9717 default:
9718 abort ();
9719 }
9720 break;
9721 default:
9722 abort ();
9723 }
9724
9725 #if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
9726 /* For x32, add a dummy REX_OPCODE prefix for mov/add with
9727 R_X86_64_GOTTPOFF relocation so that linker can safely
9728 perform IE->LE optimization. A dummy REX_OPCODE prefix
9729 is also needed for lea with R_X86_64_GOTPC32_TLSDESC
9730 relocation for GDesc -> IE/LE optimization. */
9731 if (x86_elf_abi == X86_64_X32_ABI
9732 && i.operands == 2
9733 && (i.reloc[0] == BFD_RELOC_X86_64_GOTTPOFF
9734 || i.reloc[0] == BFD_RELOC_X86_64_GOTPC32_TLSDESC)
9735 && i.prefix[REX_PREFIX] == 0)
9736 add_prefix (REX_OPCODE);
9737 #endif
9738
9739 /* The prefix bytes. */
9740 for (j = ARRAY_SIZE (i.prefix), q = i.prefix; j > 0; j--, q++)
9741 if (*q)
9742 frag_opcode_byte (*q);
9743 }
9744 else
9745 {
9746 for (j = 0, q = i.prefix; j < ARRAY_SIZE (i.prefix); j++, q++)
9747 if (*q)
9748 switch (j)
9749 {
9750 case SEG_PREFIX:
9751 case ADDR_PREFIX:
9752 frag_opcode_byte (*q);
9753 break;
9754 default:
9755 /* There should be no other prefixes for instructions
9756 with VEX prefix. */
9757 abort ();
9758 }
9759
9760 /* For EVEX instructions i.vrex should become 0 after
9761 build_evex_prefix. For VEX instructions upper 16 registers
9762 aren't available, so VREX should be 0. */
9763 if (i.vrex)
9764 abort ();
9765 /* Now the VEX prefix. */
9766 if (now_seg != absolute_section)
9767 {
9768 p = frag_more (i.vex.length);
9769 for (j = 0; j < i.vex.length; j++)
9770 p[j] = i.vex.bytes[j];
9771 }
9772 else
9773 abs_section_offset += i.vex.length;
9774 }
9775
9776 /* Now the opcode; be careful about word order here! */
9777 j = i.opcode_length;
9778 if (!i.vex.length)
9779 switch (i.tm.opcode_modifier.opcodespace)
9780 {
9781 case SPACE_BASE:
9782 break;
9783 case SPACE_0F:
9784 ++j;
9785 break;
9786 case SPACE_0F38:
9787 case SPACE_0F3A:
9788 j += 2;
9789 break;
9790 default:
9791 abort ();
9792 }
9793
9794 if (now_seg == absolute_section)
9795 abs_section_offset += j;
9796 else if (j == 1)
9797 {
9798 FRAG_APPEND_1_CHAR (i.tm.base_opcode);
9799 }
9800 else
9801 {
9802 p = frag_more (j);
9803 if (!i.vex.length
9804 && i.tm.opcode_modifier.opcodespace != SPACE_BASE)
9805 {
9806 *p++ = 0x0f;
9807 if (i.tm.opcode_modifier.opcodespace != SPACE_0F)
9808 *p++ = i.tm.opcode_modifier.opcodespace == SPACE_0F38
9809 ? 0x38 : 0x3a;
9810 }
9811
9812 switch (i.opcode_length)
9813 {
9814 case 2:
9815 /* Put out high byte first: can't use md_number_to_chars! */
9816 *p++ = (i.tm.base_opcode >> 8) & 0xff;
9817 /* Fall through. */
9818 case 1:
9819 *p = i.tm.base_opcode & 0xff;
9820 break;
9821 default:
9822 abort ();
9823 break;
9824 }
9825
9826 }
9827
9828 /* Now the modrm byte and sib byte (if present). */
9829 if (i.tm.opcode_modifier.modrm)
9830 {
9831 frag_opcode_byte ((i.rm.regmem << 0)
9832 | (i.rm.reg << 3)
9833 | (i.rm.mode << 6));
9834 /* If i.rm.regmem == ESP (4)
9835 && i.rm.mode != (Register mode)
9836 && not 16 bit
9837 ==> need second modrm byte. */
9838 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
9839 && i.rm.mode != 3
9840 && !(i.base_reg && i.base_reg->reg_type.bitfield.word))
9841 frag_opcode_byte ((i.sib.base << 0)
9842 | (i.sib.index << 3)
9843 | (i.sib.scale << 6));
9844 }
9845
9846 if (i.disp_operands)
9847 output_disp (insn_start_frag, insn_start_off);
9848
9849 if (i.imm_operands)
9850 output_imm (insn_start_frag, insn_start_off);
9851
9852 /*
9853 * frag_now_fix () returning plain abs_section_offset when we're in the
9854 * absolute section, and abs_section_offset not getting updated as data
9855 * gets added to the frag breaks the logic below.
9856 */
9857 if (now_seg != absolute_section)
9858 {
9859 j = encoding_length (insn_start_frag, insn_start_off, frag_more (0));
9860 if (j > 15)
9861 as_warn (_("instruction length of %u bytes exceeds the limit of 15"),
9862 j);
9863 else if (fragP)
9864 {
9865 /* NB: Don't add prefix with GOTPC relocation since
9866 output_disp() above depends on the fixed encoding
9867 length. Can't add prefix with TLS relocation since
9868 it breaks TLS linker optimization. */
9869 unsigned int max = i.has_gotpc_tls_reloc ? 0 : 15 - j;
9870 /* Prefix count on the current instruction. */
9871 unsigned int count = i.vex.length;
9872 unsigned int k;
9873 for (k = 0; k < ARRAY_SIZE (i.prefix); k++)
9874 /* REX byte is encoded in VEX/EVEX prefix. */
9875 if (i.prefix[k] && (k != REX_PREFIX || !i.vex.length))
9876 count++;
9877
9878 /* Count prefixes for extended opcode maps. */
9879 if (!i.vex.length)
9880 switch (i.tm.opcode_modifier.opcodespace)
9881 {
9882 case SPACE_BASE:
9883 break;
9884 case SPACE_0F:
9885 count++;
9886 break;
9887 case SPACE_0F38:
9888 case SPACE_0F3A:
9889 count += 2;
9890 break;
9891 default:
9892 abort ();
9893 }
9894
9895 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
9896 == BRANCH_PREFIX)
9897 {
9898 /* Set the maximum prefix size in BRANCH_PREFIX
9899 frag. */
9900 if (fragP->tc_frag_data.max_bytes > max)
9901 fragP->tc_frag_data.max_bytes = max;
9902 if (fragP->tc_frag_data.max_bytes > count)
9903 fragP->tc_frag_data.max_bytes -= count;
9904 else
9905 fragP->tc_frag_data.max_bytes = 0;
9906 }
9907 else
9908 {
9909 /* Remember the maximum prefix size in FUSED_JCC_PADDING
9910 frag. */
9911 unsigned int max_prefix_size;
9912 if (align_branch_prefix_size > max)
9913 max_prefix_size = max;
9914 else
9915 max_prefix_size = align_branch_prefix_size;
9916 if (max_prefix_size > count)
9917 fragP->tc_frag_data.max_prefix_length
9918 = max_prefix_size - count;
9919 }
9920
9921 /* Use existing segment prefix if possible. Use CS
9922 segment prefix in 64-bit mode. In 32-bit mode, use SS
9923 segment prefix with ESP/EBP base register and use DS
9924 segment prefix without ESP/EBP base register. */
9925 if (i.prefix[SEG_PREFIX])
9926 fragP->tc_frag_data.default_prefix = i.prefix[SEG_PREFIX];
9927 else if (flag_code == CODE_64BIT)
9928 fragP->tc_frag_data.default_prefix = CS_PREFIX_OPCODE;
9929 else if (i.base_reg
9930 && (i.base_reg->reg_num == 4
9931 || i.base_reg->reg_num == 5))
9932 fragP->tc_frag_data.default_prefix = SS_PREFIX_OPCODE;
9933 else
9934 fragP->tc_frag_data.default_prefix = DS_PREFIX_OPCODE;
9935 }
9936 }
9937 }
9938
9939 /* NB: Don't work with COND_JUMP86 without i386. */
9940 if (align_branch_power
9941 && now_seg != absolute_section
9942 && cpu_arch_flags.bitfield.cpui386)
9943 {
9944 /* Terminate each frag so that we can add prefix and check for
9945 fused jcc. */
9946 frag_wane (frag_now);
9947 frag_new (0);
9948 }
9949
9950 #ifdef DEBUG386
9951 if (flag_debug)
9952 {
9953 pi ("" /*line*/, &i);
9954 }
9955 #endif /* DEBUG386 */
9956 }
9957
9958 /* Return the size of the displacement operand N. */
9959
9960 static int
9961 disp_size (unsigned int n)
9962 {
9963 int size = 4;
9964
9965 if (i.types[n].bitfield.disp64)
9966 size = 8;
9967 else if (i.types[n].bitfield.disp8)
9968 size = 1;
9969 else if (i.types[n].bitfield.disp16)
9970 size = 2;
9971 return size;
9972 }
9973
9974 /* Return the size of the immediate operand N. */
9975
9976 static int
9977 imm_size (unsigned int n)
9978 {
9979 int size = 4;
9980 if (i.types[n].bitfield.imm64)
9981 size = 8;
9982 else if (i.types[n].bitfield.imm8 || i.types[n].bitfield.imm8s)
9983 size = 1;
9984 else if (i.types[n].bitfield.imm16)
9985 size = 2;
9986 return size;
9987 }
9988
9989 static void
9990 output_disp (fragS *insn_start_frag, offsetT insn_start_off)
9991 {
9992 char *p;
9993 unsigned int n;
9994
9995 for (n = 0; n < i.operands; n++)
9996 {
9997 if (operand_type_check (i.types[n], disp))
9998 {
9999 int size = disp_size (n);
10000
10001 if (now_seg == absolute_section)
10002 abs_section_offset += size;
10003 else if (i.op[n].disps->X_op == O_constant)
10004 {
10005 offsetT val = i.op[n].disps->X_add_number;
10006
10007 val = offset_in_range (val >> (size == 1 ? i.memshift : 0),
10008 size);
10009 p = frag_more (size);
10010 md_number_to_chars (p, val, size);
10011 }
10012 else
10013 {
10014 enum bfd_reloc_code_real reloc_type;
10015 int sign = i.types[n].bitfield.disp32s;
10016 int pcrel = (i.flags[n] & Operand_PCrel) != 0;
10017 fixS *fixP;
10018
10019 /* We can't have 8 bit displacement here. */
10020 gas_assert (!i.types[n].bitfield.disp8);
10021
10022 /* The PC relative address is computed relative
10023 to the instruction boundary, so in case immediate
10024 fields follows, we need to adjust the value. */
10025 if (pcrel && i.imm_operands)
10026 {
10027 unsigned int n1;
10028 int sz = 0;
10029
10030 for (n1 = 0; n1 < i.operands; n1++)
10031 if (operand_type_check (i.types[n1], imm))
10032 {
10033 /* Only one immediate is allowed for PC
10034 relative address. */
10035 gas_assert (sz == 0);
10036 sz = imm_size (n1);
10037 i.op[n].disps->X_add_number -= sz;
10038 }
10039 /* We should find the immediate. */
10040 gas_assert (sz != 0);
10041 }
10042
10043 p = frag_more (size);
10044 reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
10045 if (GOT_symbol
10046 && GOT_symbol == i.op[n].disps->X_add_symbol
10047 && (((reloc_type == BFD_RELOC_32
10048 || reloc_type == BFD_RELOC_X86_64_32S
10049 || (reloc_type == BFD_RELOC_64
10050 && object_64bit))
10051 && (i.op[n].disps->X_op == O_symbol
10052 || (i.op[n].disps->X_op == O_add
10053 && ((symbol_get_value_expression
10054 (i.op[n].disps->X_op_symbol)->X_op)
10055 == O_subtract))))
10056 || reloc_type == BFD_RELOC_32_PCREL))
10057 {
10058 if (!object_64bit)
10059 {
10060 reloc_type = BFD_RELOC_386_GOTPC;
10061 i.has_gotpc_tls_reloc = true;
10062 i.op[n].disps->X_add_number +=
10063 encoding_length (insn_start_frag, insn_start_off, p);
10064 }
10065 else if (reloc_type == BFD_RELOC_64)
10066 reloc_type = BFD_RELOC_X86_64_GOTPC64;
10067 else
10068 /* Don't do the adjustment for x86-64, as there
10069 the pcrel addressing is relative to the _next_
10070 insn, and that is taken care of in other code. */
10071 reloc_type = BFD_RELOC_X86_64_GOTPC32;
10072 }
10073 else if (align_branch_power)
10074 {
10075 switch (reloc_type)
10076 {
10077 case BFD_RELOC_386_TLS_GD:
10078 case BFD_RELOC_386_TLS_LDM:
10079 case BFD_RELOC_386_TLS_IE:
10080 case BFD_RELOC_386_TLS_IE_32:
10081 case BFD_RELOC_386_TLS_GOTIE:
10082 case BFD_RELOC_386_TLS_GOTDESC:
10083 case BFD_RELOC_386_TLS_DESC_CALL:
10084 case BFD_RELOC_X86_64_TLSGD:
10085 case BFD_RELOC_X86_64_TLSLD:
10086 case BFD_RELOC_X86_64_GOTTPOFF:
10087 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
10088 case BFD_RELOC_X86_64_TLSDESC_CALL:
10089 i.has_gotpc_tls_reloc = true;
10090 default:
10091 break;
10092 }
10093 }
10094 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal,
10095 size, i.op[n].disps, pcrel,
10096 reloc_type);
10097
10098 if (flag_code == CODE_64BIT && size == 4 && pcrel
10099 && !i.prefix[ADDR_PREFIX])
10100 fixP->fx_signed = 1;
10101
10102 /* Check for "call/jmp *mem", "mov mem, %reg",
10103 "test %reg, mem" and "binop mem, %reg" where binop
10104 is one of adc, add, and, cmp, or, sbb, sub, xor
10105 instructions without data prefix. Always generate
10106 R_386_GOT32X for "sym*GOT" operand in 32-bit mode. */
10107 if (i.prefix[DATA_PREFIX] == 0
10108 && (generate_relax_relocations
10109 || (!object_64bit
10110 && i.rm.mode == 0
10111 && i.rm.regmem == 5))
10112 && (i.rm.mode == 2
10113 || (i.rm.mode == 0 && i.rm.regmem == 5))
10114 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
10115 && ((i.operands == 1
10116 && i.tm.base_opcode == 0xff
10117 && (i.rm.reg == 2 || i.rm.reg == 4))
10118 || (i.operands == 2
10119 && (i.tm.base_opcode == 0x8b
10120 || i.tm.base_opcode == 0x85
10121 || (i.tm.base_opcode & ~0x38) == 0x03))))
10122 {
10123 if (object_64bit)
10124 {
10125 fixP->fx_tcbit = i.rex != 0;
10126 if (i.base_reg
10127 && (i.base_reg->reg_num == RegIP))
10128 fixP->fx_tcbit2 = 1;
10129 }
10130 else
10131 fixP->fx_tcbit2 = 1;
10132 }
10133 }
10134 }
10135 }
10136 }
10137
10138 static void
10139 output_imm (fragS *insn_start_frag, offsetT insn_start_off)
10140 {
10141 char *p;
10142 unsigned int n;
10143
10144 for (n = 0; n < i.operands; n++)
10145 {
10146 /* Skip SAE/RC Imm operand in EVEX. They are already handled. */
10147 if (i.rounding.type != rc_none && n == i.rounding.operand)
10148 continue;
10149
10150 if (operand_type_check (i.types[n], imm))
10151 {
10152 int size = imm_size (n);
10153
10154 if (now_seg == absolute_section)
10155 abs_section_offset += size;
10156 else if (i.op[n].imms->X_op == O_constant)
10157 {
10158 offsetT val;
10159
10160 val = offset_in_range (i.op[n].imms->X_add_number,
10161 size);
10162 p = frag_more (size);
10163 md_number_to_chars (p, val, size);
10164 }
10165 else
10166 {
10167 /* Not absolute_section.
10168 Need a 32-bit fixup (don't support 8bit
10169 non-absolute imms). Try to support other
10170 sizes ... */
10171 enum bfd_reloc_code_real reloc_type;
10172 int sign;
10173
10174 if (i.types[n].bitfield.imm32s
10175 && (i.suffix == QWORD_MNEM_SUFFIX
10176 || (!i.suffix && i.tm.opcode_modifier.no_lsuf)))
10177 sign = 1;
10178 else
10179 sign = 0;
10180
10181 p = frag_more (size);
10182 reloc_type = reloc (size, 0, sign, i.reloc[n]);
10183
10184 /* This is tough to explain. We end up with this one if we
10185 * have operands that look like
10186 * "_GLOBAL_OFFSET_TABLE_+[.-.L284]". The goal here is to
10187 * obtain the absolute address of the GOT, and it is strongly
10188 * preferable from a performance point of view to avoid using
10189 * a runtime relocation for this. The actual sequence of
10190 * instructions often look something like:
10191 *
10192 * call .L66
10193 * .L66:
10194 * popl %ebx
10195 * addl $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
10196 *
10197 * The call and pop essentially return the absolute address
10198 * of the label .L66 and store it in %ebx. The linker itself
10199 * will ultimately change the first operand of the addl so
10200 * that %ebx points to the GOT, but to keep things simple, the
10201 * .o file must have this operand set so that it generates not
10202 * the absolute address of .L66, but the absolute address of
10203 * itself. This allows the linker itself simply treat a GOTPC
10204 * relocation as asking for a pcrel offset to the GOT to be
10205 * added in, and the addend of the relocation is stored in the
10206 * operand field for the instruction itself.
10207 *
10208 * Our job here is to fix the operand so that it would add
10209 * the correct offset so that %ebx would point to itself. The
10210 * thing that is tricky is that .-.L66 will point to the
10211 * beginning of the instruction, so we need to further modify
10212 * the operand so that it will point to itself. There are
10213 * other cases where you have something like:
10214 *
10215 * .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
10216 *
10217 * and here no correction would be required. Internally in
10218 * the assembler we treat operands of this form as not being
10219 * pcrel since the '.' is explicitly mentioned, and I wonder
10220 * whether it would simplify matters to do it this way. Who
10221 * knows. In earlier versions of the PIC patches, the
10222 * pcrel_adjust field was used to store the correction, but
10223 * since the expression is not pcrel, I felt it would be
10224 * confusing to do it this way. */
10225
10226 if ((reloc_type == BFD_RELOC_32
10227 || reloc_type == BFD_RELOC_X86_64_32S
10228 || reloc_type == BFD_RELOC_64)
10229 && GOT_symbol
10230 && GOT_symbol == i.op[n].imms->X_add_symbol
10231 && (i.op[n].imms->X_op == O_symbol
10232 || (i.op[n].imms->X_op == O_add
10233 && ((symbol_get_value_expression
10234 (i.op[n].imms->X_op_symbol)->X_op)
10235 == O_subtract))))
10236 {
10237 if (!object_64bit)
10238 reloc_type = BFD_RELOC_386_GOTPC;
10239 else if (size == 4)
10240 reloc_type = BFD_RELOC_X86_64_GOTPC32;
10241 else if (size == 8)
10242 reloc_type = BFD_RELOC_X86_64_GOTPC64;
10243 i.has_gotpc_tls_reloc = true;
10244 i.op[n].imms->X_add_number +=
10245 encoding_length (insn_start_frag, insn_start_off, p);
10246 }
10247 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
10248 i.op[n].imms, 0, reloc_type);
10249 }
10250 }
10251 }
10252 }
10253 \f
10254 /* x86_cons_fix_new is called via the expression parsing code when a
10255 reloc is needed. We use this hook to get the correct .got reloc. */
10256 static int cons_sign = -1;
10257
10258 void
10259 x86_cons_fix_new (fragS *frag, unsigned int off, unsigned int len,
10260 expressionS *exp, bfd_reloc_code_real_type r)
10261 {
10262 r = reloc (len, 0, cons_sign, r);
10263
10264 #ifdef TE_PE
10265 if (exp->X_op == O_secrel)
10266 {
10267 exp->X_op = O_symbol;
10268 r = BFD_RELOC_32_SECREL;
10269 }
10270 #endif
10271
10272 fix_new_exp (frag, off, len, exp, 0, r);
10273 }
10274
10275 /* Export the ABI address size for use by TC_ADDRESS_BYTES for the
10276 purpose of the `.dc.a' internal pseudo-op. */
10277
10278 int
10279 x86_address_bytes (void)
10280 {
10281 if ((stdoutput->arch_info->mach & bfd_mach_x64_32))
10282 return 4;
10283 return stdoutput->arch_info->bits_per_address / 8;
10284 }
10285
10286 #if (!(defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined (OBJ_MACH_O)) \
10287 || defined (LEX_AT)) && !defined (TE_PE)
10288 # define lex_got(reloc, adjust, types) NULL
10289 #else
10290 /* Parse operands of the form
10291 <symbol>@GOTOFF+<nnn>
10292 and similar .plt or .got references.
10293
10294 If we find one, set up the correct relocation in RELOC and copy the
10295 input string, minus the `@GOTOFF' into a malloc'd buffer for
10296 parsing by the calling routine. Return this buffer, and if ADJUST
10297 is non-null set it to the length of the string we removed from the
10298 input line. Otherwise return NULL. */
10299 static char *
10300 lex_got (enum bfd_reloc_code_real *rel,
10301 int *adjust,
10302 i386_operand_type *types)
10303 {
10304 /* Some of the relocations depend on the size of what field is to
10305 be relocated. But in our callers i386_immediate and i386_displacement
10306 we don't yet know the operand size (this will be set by insn
10307 matching). Hence we record the word32 relocation here,
10308 and adjust the reloc according to the real size in reloc(). */
10309 static const struct {
10310 const char *str;
10311 int len;
10312 const enum bfd_reloc_code_real rel[2];
10313 const i386_operand_type types64;
10314 bool need_GOT_symbol;
10315 } gotrel[] = {
10316 #ifndef TE_PE
10317 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
10318 { STRING_COMMA_LEN ("SIZE"), { BFD_RELOC_SIZE32,
10319 BFD_RELOC_SIZE32 },
10320 OPERAND_TYPE_IMM32_64, false },
10321 #endif
10322 { STRING_COMMA_LEN ("PLTOFF"), { _dummy_first_bfd_reloc_code_real,
10323 BFD_RELOC_X86_64_PLTOFF64 },
10324 OPERAND_TYPE_IMM64, true },
10325 { STRING_COMMA_LEN ("PLT"), { BFD_RELOC_386_PLT32,
10326 BFD_RELOC_X86_64_PLT32 },
10327 OPERAND_TYPE_IMM32_32S_DISP32, false },
10328 { STRING_COMMA_LEN ("GOTPLT"), { _dummy_first_bfd_reloc_code_real,
10329 BFD_RELOC_X86_64_GOTPLT64 },
10330 OPERAND_TYPE_IMM64_DISP64, true },
10331 { STRING_COMMA_LEN ("GOTOFF"), { BFD_RELOC_386_GOTOFF,
10332 BFD_RELOC_X86_64_GOTOFF64 },
10333 OPERAND_TYPE_IMM64_DISP64, true },
10334 { STRING_COMMA_LEN ("GOTPCREL"), { _dummy_first_bfd_reloc_code_real,
10335 BFD_RELOC_X86_64_GOTPCREL },
10336 OPERAND_TYPE_IMM32_32S_DISP32, true },
10337 { STRING_COMMA_LEN ("TLSGD"), { BFD_RELOC_386_TLS_GD,
10338 BFD_RELOC_X86_64_TLSGD },
10339 OPERAND_TYPE_IMM32_32S_DISP32, true },
10340 { STRING_COMMA_LEN ("TLSLDM"), { BFD_RELOC_386_TLS_LDM,
10341 _dummy_first_bfd_reloc_code_real },
10342 OPERAND_TYPE_NONE, true },
10343 { STRING_COMMA_LEN ("TLSLD"), { _dummy_first_bfd_reloc_code_real,
10344 BFD_RELOC_X86_64_TLSLD },
10345 OPERAND_TYPE_IMM32_32S_DISP32, true },
10346 { STRING_COMMA_LEN ("GOTTPOFF"), { BFD_RELOC_386_TLS_IE_32,
10347 BFD_RELOC_X86_64_GOTTPOFF },
10348 OPERAND_TYPE_IMM32_32S_DISP32, true },
10349 { STRING_COMMA_LEN ("TPOFF"), { BFD_RELOC_386_TLS_LE_32,
10350 BFD_RELOC_X86_64_TPOFF32 },
10351 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
10352 { STRING_COMMA_LEN ("NTPOFF"), { BFD_RELOC_386_TLS_LE,
10353 _dummy_first_bfd_reloc_code_real },
10354 OPERAND_TYPE_NONE, true },
10355 { STRING_COMMA_LEN ("DTPOFF"), { BFD_RELOC_386_TLS_LDO_32,
10356 BFD_RELOC_X86_64_DTPOFF32 },
10357 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
10358 { STRING_COMMA_LEN ("GOTNTPOFF"),{ BFD_RELOC_386_TLS_GOTIE,
10359 _dummy_first_bfd_reloc_code_real },
10360 OPERAND_TYPE_NONE, true },
10361 { STRING_COMMA_LEN ("INDNTPOFF"),{ BFD_RELOC_386_TLS_IE,
10362 _dummy_first_bfd_reloc_code_real },
10363 OPERAND_TYPE_NONE, true },
10364 { STRING_COMMA_LEN ("GOT"), { BFD_RELOC_386_GOT32,
10365 BFD_RELOC_X86_64_GOT32 },
10366 OPERAND_TYPE_IMM32_32S_64_DISP32, true },
10367 { STRING_COMMA_LEN ("TLSDESC"), { BFD_RELOC_386_TLS_GOTDESC,
10368 BFD_RELOC_X86_64_GOTPC32_TLSDESC },
10369 OPERAND_TYPE_IMM32_32S_DISP32, true },
10370 { STRING_COMMA_LEN ("TLSCALL"), { BFD_RELOC_386_TLS_DESC_CALL,
10371 BFD_RELOC_X86_64_TLSDESC_CALL },
10372 OPERAND_TYPE_IMM32_32S_DISP32, true },
10373 #else /* TE_PE */
10374 { STRING_COMMA_LEN ("SECREL32"), { BFD_RELOC_32_SECREL,
10375 BFD_RELOC_32_SECREL },
10376 OPERAND_TYPE_IMM32_32S_64_DISP32_64, false },
10377 #endif
10378 };
10379 char *cp;
10380 unsigned int j;
10381
10382 #if defined (OBJ_MAYBE_ELF) && !defined (TE_PE)
10383 if (!IS_ELF)
10384 return NULL;
10385 #endif
10386
10387 for (cp = input_line_pointer; *cp != '@'; cp++)
10388 if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
10389 return NULL;
10390
10391 for (j = 0; j < ARRAY_SIZE (gotrel); j++)
10392 {
10393 int len = gotrel[j].len;
10394 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
10395 {
10396 if (gotrel[j].rel[object_64bit] != 0)
10397 {
10398 int first, second;
10399 char *tmpbuf, *past_reloc;
10400
10401 *rel = gotrel[j].rel[object_64bit];
10402
10403 if (types)
10404 {
10405 if (flag_code != CODE_64BIT)
10406 {
10407 types->bitfield.imm32 = 1;
10408 types->bitfield.disp32 = 1;
10409 }
10410 else
10411 *types = gotrel[j].types64;
10412 }
10413
10414 if (gotrel[j].need_GOT_symbol && GOT_symbol == NULL)
10415 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
10416
10417 /* The length of the first part of our input line. */
10418 first = cp - input_line_pointer;
10419
10420 /* The second part goes from after the reloc token until
10421 (and including) an end_of_line char or comma. */
10422 past_reloc = cp + 1 + len;
10423 cp = past_reloc;
10424 while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
10425 ++cp;
10426 second = cp + 1 - past_reloc;
10427
10428 /* Allocate and copy string. The trailing NUL shouldn't
10429 be necessary, but be safe. */
10430 tmpbuf = XNEWVEC (char, first + second + 2);
10431 memcpy (tmpbuf, input_line_pointer, first);
10432 if (second != 0 && *past_reloc != ' ')
10433 /* Replace the relocation token with ' ', so that
10434 errors like foo@GOTOFF1 will be detected. */
10435 tmpbuf[first++] = ' ';
10436 else
10437 /* Increment length by 1 if the relocation token is
10438 removed. */
10439 len++;
10440 if (adjust)
10441 *adjust = len;
10442 memcpy (tmpbuf + first, past_reloc, second);
10443 tmpbuf[first + second] = '\0';
10444 return tmpbuf;
10445 }
10446
10447 as_bad (_("@%s reloc is not supported with %d-bit output format"),
10448 gotrel[j].str, 1 << (5 + object_64bit));
10449 return NULL;
10450 }
10451 }
10452
10453 /* Might be a symbol version string. Don't as_bad here. */
10454 return NULL;
10455 }
10456 #endif
10457
10458 bfd_reloc_code_real_type
10459 x86_cons (expressionS *exp, int size)
10460 {
10461 bfd_reloc_code_real_type got_reloc = NO_RELOC;
10462
10463 #if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
10464 && !defined (LEX_AT)) \
10465 || defined (TE_PE)
10466 intel_syntax = -intel_syntax;
10467
10468 exp->X_md = 0;
10469 if (size == 4 || (object_64bit && size == 8))
10470 {
10471 /* Handle @GOTOFF and the like in an expression. */
10472 char *save;
10473 char *gotfree_input_line;
10474 int adjust = 0;
10475
10476 save = input_line_pointer;
10477 gotfree_input_line = lex_got (&got_reloc, &adjust, NULL);
10478 if (gotfree_input_line)
10479 input_line_pointer = gotfree_input_line;
10480
10481 expression (exp);
10482
10483 if (gotfree_input_line)
10484 {
10485 /* expression () has merrily parsed up to the end of line,
10486 or a comma - in the wrong buffer. Transfer how far
10487 input_line_pointer has moved to the right buffer. */
10488 input_line_pointer = (save
10489 + (input_line_pointer - gotfree_input_line)
10490 + adjust);
10491 free (gotfree_input_line);
10492 if (exp->X_op == O_constant
10493 || exp->X_op == O_absent
10494 || exp->X_op == O_illegal
10495 || exp->X_op == O_register
10496 || exp->X_op == O_big)
10497 {
10498 char c = *input_line_pointer;
10499 *input_line_pointer = 0;
10500 as_bad (_("missing or invalid expression `%s'"), save);
10501 *input_line_pointer = c;
10502 }
10503 else if ((got_reloc == BFD_RELOC_386_PLT32
10504 || got_reloc == BFD_RELOC_X86_64_PLT32)
10505 && exp->X_op != O_symbol)
10506 {
10507 char c = *input_line_pointer;
10508 *input_line_pointer = 0;
10509 as_bad (_("invalid PLT expression `%s'"), save);
10510 *input_line_pointer = c;
10511 }
10512 }
10513 }
10514 else
10515 expression (exp);
10516
10517 intel_syntax = -intel_syntax;
10518
10519 if (intel_syntax)
10520 i386_intel_simplify (exp);
10521 #else
10522 expression (exp);
10523 #endif
10524
10525 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
10526 if (size == 4 && exp->X_op == O_constant && !object_64bit)
10527 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
10528
10529 return got_reloc;
10530 }
10531
10532 static void
10533 signed_cons (int size)
10534 {
10535 if (object_64bit)
10536 cons_sign = 1;
10537 cons (size);
10538 cons_sign = -1;
10539 }
10540
10541 #ifdef TE_PE
10542 static void
10543 pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
10544 {
10545 expressionS exp;
10546
10547 do
10548 {
10549 expression (&exp);
10550 if (exp.X_op == O_symbol)
10551 exp.X_op = O_secrel;
10552
10553 emit_expr (&exp, 4);
10554 }
10555 while (*input_line_pointer++ == ',');
10556
10557 input_line_pointer--;
10558 demand_empty_rest_of_line ();
10559 }
10560 #endif
10561
10562 /* Handle Vector operations. */
10563
10564 static char *
10565 check_VecOperations (char *op_string)
10566 {
10567 const reg_entry *mask;
10568 const char *saved;
10569 char *end_op;
10570
10571 while (*op_string)
10572 {
10573 saved = op_string;
10574 if (*op_string == '{')
10575 {
10576 op_string++;
10577
10578 /* Check broadcasts. */
10579 if (startswith (op_string, "1to"))
10580 {
10581 unsigned int bcst_type;
10582
10583 if (i.broadcast.type)
10584 goto duplicated_vec_op;
10585
10586 op_string += 3;
10587 if (*op_string == '8')
10588 bcst_type = 8;
10589 else if (*op_string == '4')
10590 bcst_type = 4;
10591 else if (*op_string == '2')
10592 bcst_type = 2;
10593 else if (*op_string == '1'
10594 && *(op_string+1) == '6')
10595 {
10596 bcst_type = 16;
10597 op_string++;
10598 }
10599 else if (*op_string == '3'
10600 && *(op_string+1) == '2')
10601 {
10602 bcst_type = 32;
10603 op_string++;
10604 }
10605 else
10606 {
10607 as_bad (_("Unsupported broadcast: `%s'"), saved);
10608 return NULL;
10609 }
10610 op_string++;
10611
10612 i.broadcast.type = bcst_type;
10613 i.broadcast.operand = this_operand;
10614 }
10615 /* Check masking operation. */
10616 else if ((mask = parse_register (op_string, &end_op)) != NULL)
10617 {
10618 if (mask == &bad_reg)
10619 return NULL;
10620
10621 /* k0 can't be used for write mask. */
10622 if (mask->reg_type.bitfield.class != RegMask || !mask->reg_num)
10623 {
10624 as_bad (_("`%s%s' can't be used for write mask"),
10625 register_prefix, mask->reg_name);
10626 return NULL;
10627 }
10628
10629 if (!i.mask.reg)
10630 {
10631 i.mask.reg = mask;
10632 i.mask.operand = this_operand;
10633 }
10634 else if (i.mask.reg->reg_num)
10635 goto duplicated_vec_op;
10636 else
10637 {
10638 i.mask.reg = mask;
10639
10640 /* Only "{z}" is allowed here. No need to check
10641 zeroing mask explicitly. */
10642 if (i.mask.operand != (unsigned int) this_operand)
10643 {
10644 as_bad (_("invalid write mask `%s'"), saved);
10645 return NULL;
10646 }
10647 }
10648
10649 op_string = end_op;
10650 }
10651 /* Check zeroing-flag for masking operation. */
10652 else if (*op_string == 'z')
10653 {
10654 if (!i.mask.reg)
10655 {
10656 i.mask.reg = reg_k0;
10657 i.mask.zeroing = 1;
10658 i.mask.operand = this_operand;
10659 }
10660 else
10661 {
10662 if (i.mask.zeroing)
10663 {
10664 duplicated_vec_op:
10665 as_bad (_("duplicated `%s'"), saved);
10666 return NULL;
10667 }
10668
10669 i.mask.zeroing = 1;
10670
10671 /* Only "{%k}" is allowed here. No need to check mask
10672 register explicitly. */
10673 if (i.mask.operand != (unsigned int) this_operand)
10674 {
10675 as_bad (_("invalid zeroing-masking `%s'"),
10676 saved);
10677 return NULL;
10678 }
10679 }
10680
10681 op_string++;
10682 }
10683 else
10684 goto unknown_vec_op;
10685
10686 if (*op_string != '}')
10687 {
10688 as_bad (_("missing `}' in `%s'"), saved);
10689 return NULL;
10690 }
10691 op_string++;
10692
10693 /* Strip whitespace since the addition of pseudo prefixes
10694 changed how the scrubber treats '{'. */
10695 if (is_space_char (*op_string))
10696 ++op_string;
10697
10698 continue;
10699 }
10700 unknown_vec_op:
10701 /* We don't know this one. */
10702 as_bad (_("unknown vector operation: `%s'"), saved);
10703 return NULL;
10704 }
10705
10706 if (i.mask.reg && i.mask.zeroing && !i.mask.reg->reg_num)
10707 {
10708 as_bad (_("zeroing-masking only allowed with write mask"));
10709 return NULL;
10710 }
10711
10712 return op_string;
10713 }
10714
10715 static int
10716 i386_immediate (char *imm_start)
10717 {
10718 char *save_input_line_pointer;
10719 char *gotfree_input_line;
10720 segT exp_seg = 0;
10721 expressionS *exp;
10722 i386_operand_type types;
10723
10724 operand_type_set (&types, ~0);
10725
10726 if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
10727 {
10728 as_bad (_("at most %d immediate operands are allowed"),
10729 MAX_IMMEDIATE_OPERANDS);
10730 return 0;
10731 }
10732
10733 exp = &im_expressions[i.imm_operands++];
10734 i.op[this_operand].imms = exp;
10735
10736 if (is_space_char (*imm_start))
10737 ++imm_start;
10738
10739 save_input_line_pointer = input_line_pointer;
10740 input_line_pointer = imm_start;
10741
10742 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
10743 if (gotfree_input_line)
10744 input_line_pointer = gotfree_input_line;
10745
10746 exp_seg = expression (exp);
10747
10748 SKIP_WHITESPACE ();
10749 if (*input_line_pointer)
10750 as_bad (_("junk `%s' after expression"), input_line_pointer);
10751
10752 input_line_pointer = save_input_line_pointer;
10753 if (gotfree_input_line)
10754 {
10755 free (gotfree_input_line);
10756
10757 if (exp->X_op == O_constant)
10758 exp->X_op = O_illegal;
10759 }
10760
10761 if (exp_seg == reg_section)
10762 {
10763 as_bad (_("illegal immediate register operand %s"), imm_start);
10764 return 0;
10765 }
10766
10767 return i386_finalize_immediate (exp_seg, exp, types, imm_start);
10768 }
10769
10770 static int
10771 i386_finalize_immediate (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
10772 i386_operand_type types, const char *imm_start)
10773 {
10774 if (exp->X_op == O_absent || exp->X_op == O_illegal || exp->X_op == O_big)
10775 {
10776 if (imm_start)
10777 as_bad (_("missing or invalid immediate expression `%s'"),
10778 imm_start);
10779 return 0;
10780 }
10781 else if (exp->X_op == O_constant)
10782 {
10783 /* Size it properly later. */
10784 i.types[this_operand].bitfield.imm64 = 1;
10785
10786 /* If not 64bit, sign/zero extend val, to account for wraparound
10787 when !BFD64. */
10788 if (flag_code != CODE_64BIT)
10789 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
10790 }
10791 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
10792 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
10793 && exp_seg != absolute_section
10794 && exp_seg != text_section
10795 && exp_seg != data_section
10796 && exp_seg != bss_section
10797 && exp_seg != undefined_section
10798 && !bfd_is_com_section (exp_seg))
10799 {
10800 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
10801 return 0;
10802 }
10803 #endif
10804 else
10805 {
10806 /* This is an address. The size of the address will be
10807 determined later, depending on destination register,
10808 suffix, or the default for the section. */
10809 i.types[this_operand].bitfield.imm8 = 1;
10810 i.types[this_operand].bitfield.imm16 = 1;
10811 i.types[this_operand].bitfield.imm32 = 1;
10812 i.types[this_operand].bitfield.imm32s = 1;
10813 i.types[this_operand].bitfield.imm64 = 1;
10814 i.types[this_operand] = operand_type_and (i.types[this_operand],
10815 types);
10816 }
10817
10818 return 1;
10819 }
10820
10821 static char *
10822 i386_scale (char *scale)
10823 {
10824 offsetT val;
10825 char *save = input_line_pointer;
10826
10827 input_line_pointer = scale;
10828 val = get_absolute_expression ();
10829
10830 switch (val)
10831 {
10832 case 1:
10833 i.log2_scale_factor = 0;
10834 break;
10835 case 2:
10836 i.log2_scale_factor = 1;
10837 break;
10838 case 4:
10839 i.log2_scale_factor = 2;
10840 break;
10841 case 8:
10842 i.log2_scale_factor = 3;
10843 break;
10844 default:
10845 {
10846 char sep = *input_line_pointer;
10847
10848 *input_line_pointer = '\0';
10849 as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
10850 scale);
10851 *input_line_pointer = sep;
10852 input_line_pointer = save;
10853 return NULL;
10854 }
10855 }
10856 if (i.log2_scale_factor != 0 && i.index_reg == 0)
10857 {
10858 as_warn (_("scale factor of %d without an index register"),
10859 1 << i.log2_scale_factor);
10860 i.log2_scale_factor = 0;
10861 }
10862 scale = input_line_pointer;
10863 input_line_pointer = save;
10864 return scale;
10865 }
10866
10867 static int
10868 i386_displacement (char *disp_start, char *disp_end)
10869 {
10870 expressionS *exp;
10871 segT exp_seg = 0;
10872 char *save_input_line_pointer;
10873 char *gotfree_input_line;
10874 int override;
10875 i386_operand_type bigdisp, types = anydisp;
10876 int ret;
10877
10878 if (i.disp_operands == MAX_MEMORY_OPERANDS)
10879 {
10880 as_bad (_("at most %d displacement operands are allowed"),
10881 MAX_MEMORY_OPERANDS);
10882 return 0;
10883 }
10884
10885 operand_type_set (&bigdisp, 0);
10886 if (i.jumpabsolute
10887 || i.types[this_operand].bitfield.baseindex
10888 || (current_templates->start->opcode_modifier.jump != JUMP
10889 && current_templates->start->opcode_modifier.jump != JUMP_DWORD))
10890 {
10891 i386_addressing_mode ();
10892 override = (i.prefix[ADDR_PREFIX] != 0);
10893 if (flag_code == CODE_64BIT)
10894 {
10895 if (!override)
10896 {
10897 bigdisp.bitfield.disp32s = 1;
10898 bigdisp.bitfield.disp64 = 1;
10899 }
10900 else
10901 bigdisp.bitfield.disp32 = 1;
10902 }
10903 else if ((flag_code == CODE_16BIT) ^ override)
10904 bigdisp.bitfield.disp16 = 1;
10905 else
10906 bigdisp.bitfield.disp32 = 1;
10907 }
10908 else
10909 {
10910 /* For PC-relative branches, the width of the displacement may be
10911 dependent upon data size, but is never dependent upon address size.
10912 Also make sure to not unintentionally match against a non-PC-relative
10913 branch template. */
10914 static templates aux_templates;
10915 const insn_template *t = current_templates->start;
10916 bool has_intel64 = false;
10917
10918 aux_templates.start = t;
10919 while (++t < current_templates->end)
10920 {
10921 if (t->opcode_modifier.jump
10922 != current_templates->start->opcode_modifier.jump)
10923 break;
10924 if ((t->opcode_modifier.isa64 >= INTEL64))
10925 has_intel64 = true;
10926 }
10927 if (t < current_templates->end)
10928 {
10929 aux_templates.end = t;
10930 current_templates = &aux_templates;
10931 }
10932
10933 override = (i.prefix[DATA_PREFIX] != 0);
10934 if (flag_code == CODE_64BIT)
10935 {
10936 if ((override || i.suffix == WORD_MNEM_SUFFIX)
10937 && (!intel64 || !has_intel64))
10938 bigdisp.bitfield.disp16 = 1;
10939 else
10940 bigdisp.bitfield.disp32s = 1;
10941 }
10942 else
10943 {
10944 if (!override)
10945 override = (i.suffix == (flag_code != CODE_16BIT
10946 ? WORD_MNEM_SUFFIX
10947 : LONG_MNEM_SUFFIX));
10948 bigdisp.bitfield.disp32 = 1;
10949 if ((flag_code == CODE_16BIT) ^ override)
10950 {
10951 bigdisp.bitfield.disp32 = 0;
10952 bigdisp.bitfield.disp16 = 1;
10953 }
10954 }
10955 }
10956 i.types[this_operand] = operand_type_or (i.types[this_operand],
10957 bigdisp);
10958
10959 exp = &disp_expressions[i.disp_operands];
10960 i.op[this_operand].disps = exp;
10961 i.disp_operands++;
10962 save_input_line_pointer = input_line_pointer;
10963 input_line_pointer = disp_start;
10964 END_STRING_AND_SAVE (disp_end);
10965
10966 #ifndef GCC_ASM_O_HACK
10967 #define GCC_ASM_O_HACK 0
10968 #endif
10969 #if GCC_ASM_O_HACK
10970 END_STRING_AND_SAVE (disp_end + 1);
10971 if (i.types[this_operand].bitfield.baseIndex
10972 && displacement_string_end[-1] == '+')
10973 {
10974 /* This hack is to avoid a warning when using the "o"
10975 constraint within gcc asm statements.
10976 For instance:
10977
10978 #define _set_tssldt_desc(n,addr,limit,type) \
10979 __asm__ __volatile__ ( \
10980 "movw %w2,%0\n\t" \
10981 "movw %w1,2+%0\n\t" \
10982 "rorl $16,%1\n\t" \
10983 "movb %b1,4+%0\n\t" \
10984 "movb %4,5+%0\n\t" \
10985 "movb $0,6+%0\n\t" \
10986 "movb %h1,7+%0\n\t" \
10987 "rorl $16,%1" \
10988 : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
10989
10990 This works great except that the output assembler ends
10991 up looking a bit weird if it turns out that there is
10992 no offset. You end up producing code that looks like:
10993
10994 #APP
10995 movw $235,(%eax)
10996 movw %dx,2+(%eax)
10997 rorl $16,%edx
10998 movb %dl,4+(%eax)
10999 movb $137,5+(%eax)
11000 movb $0,6+(%eax)
11001 movb %dh,7+(%eax)
11002 rorl $16,%edx
11003 #NO_APP
11004
11005 So here we provide the missing zero. */
11006
11007 *displacement_string_end = '0';
11008 }
11009 #endif
11010 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
11011 if (gotfree_input_line)
11012 input_line_pointer = gotfree_input_line;
11013
11014 exp_seg = expression (exp);
11015
11016 SKIP_WHITESPACE ();
11017 if (*input_line_pointer)
11018 as_bad (_("junk `%s' after expression"), input_line_pointer);
11019 #if GCC_ASM_O_HACK
11020 RESTORE_END_STRING (disp_end + 1);
11021 #endif
11022 input_line_pointer = save_input_line_pointer;
11023 if (gotfree_input_line)
11024 {
11025 free (gotfree_input_line);
11026
11027 if (exp->X_op == O_constant || exp->X_op == O_register)
11028 exp->X_op = O_illegal;
11029 }
11030
11031 ret = i386_finalize_displacement (exp_seg, exp, types, disp_start);
11032
11033 RESTORE_END_STRING (disp_end);
11034
11035 return ret;
11036 }
11037
11038 static int
11039 i386_finalize_displacement (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
11040 i386_operand_type types, const char *disp_start)
11041 {
11042 i386_operand_type bigdisp;
11043 int ret = 1;
11044
11045 /* We do this to make sure that the section symbol is in
11046 the symbol table. We will ultimately change the relocation
11047 to be relative to the beginning of the section. */
11048 if (i.reloc[this_operand] == BFD_RELOC_386_GOTOFF
11049 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
11050 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
11051 {
11052 if (exp->X_op != O_symbol)
11053 goto inv_disp;
11054
11055 if (S_IS_LOCAL (exp->X_add_symbol)
11056 && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section
11057 && S_GET_SEGMENT (exp->X_add_symbol) != expr_section)
11058 section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
11059 exp->X_op = O_subtract;
11060 exp->X_op_symbol = GOT_symbol;
11061 if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
11062 i.reloc[this_operand] = BFD_RELOC_32_PCREL;
11063 else if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
11064 i.reloc[this_operand] = BFD_RELOC_64;
11065 else
11066 i.reloc[this_operand] = BFD_RELOC_32;
11067 }
11068
11069 else if (exp->X_op == O_absent
11070 || exp->X_op == O_illegal
11071 || exp->X_op == O_big)
11072 {
11073 inv_disp:
11074 as_bad (_("missing or invalid displacement expression `%s'"),
11075 disp_start);
11076 ret = 0;
11077 }
11078
11079 else if (exp->X_op == O_constant)
11080 {
11081 /* Sizing gets taken care of by optimize_disp().
11082
11083 If not 64bit, sign/zero extend val, to account for wraparound
11084 when !BFD64. */
11085 if (flag_code != CODE_64BIT)
11086 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
11087 }
11088
11089 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
11090 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
11091 && exp_seg != absolute_section
11092 && exp_seg != text_section
11093 && exp_seg != data_section
11094 && exp_seg != bss_section
11095 && exp_seg != undefined_section
11096 && !bfd_is_com_section (exp_seg))
11097 {
11098 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
11099 ret = 0;
11100 }
11101 #endif
11102
11103 else if (current_templates->start->opcode_modifier.jump == JUMP_BYTE)
11104 i.types[this_operand].bitfield.disp8 = 1;
11105
11106 /* Check if this is a displacement only operand. */
11107 bigdisp = operand_type_and_not (i.types[this_operand], anydisp);
11108 if (operand_type_all_zero (&bigdisp))
11109 i.types[this_operand] = operand_type_and (i.types[this_operand],
11110 types);
11111
11112 return ret;
11113 }
11114
11115 /* Return the active addressing mode, taking address override and
11116 registers forming the address into consideration. Update the
11117 address override prefix if necessary. */
11118
11119 static enum flag_code
11120 i386_addressing_mode (void)
11121 {
11122 enum flag_code addr_mode;
11123
11124 if (i.prefix[ADDR_PREFIX])
11125 addr_mode = flag_code == CODE_32BIT ? CODE_16BIT : CODE_32BIT;
11126 else if (flag_code == CODE_16BIT
11127 && current_templates->start->cpu_flags.bitfield.cpumpx
11128 /* Avoid replacing the "16-bit addressing not allowed" diagnostic
11129 from md_assemble() by "is not a valid base/index expression"
11130 when there is a base and/or index. */
11131 && !i.types[this_operand].bitfield.baseindex)
11132 {
11133 /* MPX insn memory operands with neither base nor index must be forced
11134 to use 32-bit addressing in 16-bit mode. */
11135 addr_mode = CODE_32BIT;
11136 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
11137 ++i.prefixes;
11138 gas_assert (!i.types[this_operand].bitfield.disp16);
11139 gas_assert (!i.types[this_operand].bitfield.disp32);
11140 }
11141 else
11142 {
11143 addr_mode = flag_code;
11144
11145 #if INFER_ADDR_PREFIX
11146 if (i.mem_operands == 0)
11147 {
11148 /* Infer address prefix from the first memory operand. */
11149 const reg_entry *addr_reg = i.base_reg;
11150
11151 if (addr_reg == NULL)
11152 addr_reg = i.index_reg;
11153
11154 if (addr_reg)
11155 {
11156 if (addr_reg->reg_type.bitfield.dword)
11157 addr_mode = CODE_32BIT;
11158 else if (flag_code != CODE_64BIT
11159 && addr_reg->reg_type.bitfield.word)
11160 addr_mode = CODE_16BIT;
11161
11162 if (addr_mode != flag_code)
11163 {
11164 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
11165 i.prefixes += 1;
11166 /* Change the size of any displacement too. At most one
11167 of Disp16 or Disp32 is set.
11168 FIXME. There doesn't seem to be any real need for
11169 separate Disp16 and Disp32 flags. The same goes for
11170 Imm16 and Imm32. Removing them would probably clean
11171 up the code quite a lot. */
11172 if (flag_code != CODE_64BIT
11173 && (i.types[this_operand].bitfield.disp16
11174 || i.types[this_operand].bitfield.disp32))
11175 i.types[this_operand]
11176 = operand_type_xor (i.types[this_operand], disp16_32);
11177 }
11178 }
11179 }
11180 #endif
11181 }
11182
11183 return addr_mode;
11184 }
11185
11186 /* Make sure the memory operand we've been dealt is valid.
11187 Return 1 on success, 0 on a failure. */
11188
11189 static int
11190 i386_index_check (const char *operand_string)
11191 {
11192 const char *kind = "base/index";
11193 enum flag_code addr_mode = i386_addressing_mode ();
11194 const insn_template *t = current_templates->start;
11195
11196 if (t->opcode_modifier.isstring
11197 && !t->cpu_flags.bitfield.cpupadlock
11198 && (current_templates->end[-1].opcode_modifier.isstring
11199 || i.mem_operands))
11200 {
11201 /* Memory operands of string insns are special in that they only allow
11202 a single register (rDI, rSI, or rBX) as their memory address. */
11203 const reg_entry *expected_reg;
11204 static const char *di_si[][2] =
11205 {
11206 { "esi", "edi" },
11207 { "si", "di" },
11208 { "rsi", "rdi" }
11209 };
11210 static const char *bx[] = { "ebx", "bx", "rbx" };
11211
11212 kind = "string address";
11213
11214 if (t->opcode_modifier.prefixok == PrefixRep)
11215 {
11216 int es_op = current_templates->end[-1].opcode_modifier.isstring
11217 - IS_STRING_ES_OP0;
11218 int op = 0;
11219
11220 if (!current_templates->end[-1].operand_types[0].bitfield.baseindex
11221 || ((!i.mem_operands != !intel_syntax)
11222 && current_templates->end[-1].operand_types[1]
11223 .bitfield.baseindex))
11224 op = 1;
11225 expected_reg
11226 = (const reg_entry *) str_hash_find (reg_hash,
11227 di_si[addr_mode][op == es_op]);
11228 }
11229 else
11230 expected_reg
11231 = (const reg_entry *)str_hash_find (reg_hash, bx[addr_mode]);
11232
11233 if (i.base_reg != expected_reg
11234 || i.index_reg
11235 || operand_type_check (i.types[this_operand], disp))
11236 {
11237 /* The second memory operand must have the same size as
11238 the first one. */
11239 if (i.mem_operands
11240 && i.base_reg
11241 && !((addr_mode == CODE_64BIT
11242 && i.base_reg->reg_type.bitfield.qword)
11243 || (addr_mode == CODE_32BIT
11244 ? i.base_reg->reg_type.bitfield.dword
11245 : i.base_reg->reg_type.bitfield.word)))
11246 goto bad_address;
11247
11248 as_warn (_("`%s' is not valid here (expected `%c%s%s%c')"),
11249 operand_string,
11250 intel_syntax ? '[' : '(',
11251 register_prefix,
11252 expected_reg->reg_name,
11253 intel_syntax ? ']' : ')');
11254 return 1;
11255 }
11256 else
11257 return 1;
11258
11259 bad_address:
11260 as_bad (_("`%s' is not a valid %s expression"),
11261 operand_string, kind);
11262 return 0;
11263 }
11264 else
11265 {
11266 if (addr_mode != CODE_16BIT)
11267 {
11268 /* 32-bit/64-bit checks. */
11269 if (i.disp_encoding == disp_encoding_16bit)
11270 {
11271 bad_disp:
11272 as_bad (_("invalid `%s' prefix"),
11273 addr_mode == CODE_16BIT ? "{disp32}" : "{disp16}");
11274 return 0;
11275 }
11276
11277 if ((i.base_reg
11278 && ((addr_mode == CODE_64BIT
11279 ? !i.base_reg->reg_type.bitfield.qword
11280 : !i.base_reg->reg_type.bitfield.dword)
11281 || (i.index_reg && i.base_reg->reg_num == RegIP)
11282 || i.base_reg->reg_num == RegIZ))
11283 || (i.index_reg
11284 && !i.index_reg->reg_type.bitfield.xmmword
11285 && !i.index_reg->reg_type.bitfield.ymmword
11286 && !i.index_reg->reg_type.bitfield.zmmword
11287 && ((addr_mode == CODE_64BIT
11288 ? !i.index_reg->reg_type.bitfield.qword
11289 : !i.index_reg->reg_type.bitfield.dword)
11290 || !i.index_reg->reg_type.bitfield.baseindex)))
11291 goto bad_address;
11292
11293 /* bndmk, bndldx, bndstx and mandatory non-vector SIB have special restrictions. */
11294 if ((t->opcode_modifier.opcodeprefix == PREFIX_0XF3
11295 && t->opcode_modifier.opcodespace == SPACE_0F
11296 && t->base_opcode == 0x1b)
11297 || (t->opcode_modifier.opcodeprefix == PREFIX_NONE
11298 && t->opcode_modifier.opcodespace == SPACE_0F
11299 && (t->base_opcode & ~1) == 0x1a)
11300 || t->opcode_modifier.sib == SIBMEM)
11301 {
11302 /* They cannot use RIP-relative addressing. */
11303 if (i.base_reg && i.base_reg->reg_num == RegIP)
11304 {
11305 as_bad (_("`%s' cannot be used here"), operand_string);
11306 return 0;
11307 }
11308
11309 /* bndldx and bndstx ignore their scale factor. */
11310 if (t->opcode_modifier.opcodeprefix == PREFIX_NONE
11311 && t->opcode_modifier.opcodespace == SPACE_0F
11312 && (t->base_opcode & ~1) == 0x1a
11313 && i.log2_scale_factor)
11314 as_warn (_("register scaling is being ignored here"));
11315 }
11316 }
11317 else
11318 {
11319 /* 16-bit checks. */
11320 if (i.disp_encoding == disp_encoding_32bit)
11321 goto bad_disp;
11322
11323 if ((i.base_reg
11324 && (!i.base_reg->reg_type.bitfield.word
11325 || !i.base_reg->reg_type.bitfield.baseindex))
11326 || (i.index_reg
11327 && (!i.index_reg->reg_type.bitfield.word
11328 || !i.index_reg->reg_type.bitfield.baseindex
11329 || !(i.base_reg
11330 && i.base_reg->reg_num < 6
11331 && i.index_reg->reg_num >= 6
11332 && i.log2_scale_factor == 0))))
11333 goto bad_address;
11334 }
11335 }
11336 return 1;
11337 }
11338
11339 /* Handle vector immediates. */
11340
11341 static int
11342 RC_SAE_immediate (const char *imm_start)
11343 {
11344 unsigned int match_found, j;
11345 const char *pstr = imm_start;
11346 expressionS *exp;
11347
11348 if (*pstr != '{')
11349 return 0;
11350
11351 pstr++;
11352 match_found = 0;
11353 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); j++)
11354 {
11355 if (!strncmp (pstr, RC_NamesTable[j].name, RC_NamesTable[j].len))
11356 {
11357 if (i.rounding.type != rc_none)
11358 {
11359 as_bad (_("duplicated `%s'"), imm_start);
11360 return 0;
11361 }
11362
11363 i.rounding.type = RC_NamesTable[j].type;
11364 i.rounding.operand = this_operand;
11365
11366 pstr += RC_NamesTable[j].len;
11367 match_found = 1;
11368 break;
11369 }
11370 }
11371 if (!match_found)
11372 return 0;
11373
11374 if (*pstr++ != '}')
11375 {
11376 as_bad (_("Missing '}': '%s'"), imm_start);
11377 return 0;
11378 }
11379 /* RC/SAE immediate string should contain nothing more. */;
11380 if (*pstr != 0)
11381 {
11382 as_bad (_("Junk after '}': '%s'"), imm_start);
11383 return 0;
11384 }
11385
11386 exp = &im_expressions[i.imm_operands++];
11387 i.op[this_operand].imms = exp;
11388
11389 exp->X_op = O_constant;
11390 exp->X_add_number = 0;
11391 exp->X_add_symbol = (symbolS *) 0;
11392 exp->X_op_symbol = (symbolS *) 0;
11393
11394 i.types[this_operand].bitfield.imm8 = 1;
11395 return 1;
11396 }
11397
11398 /* Only string instructions can have a second memory operand, so
11399 reduce current_templates to just those if it contains any. */
11400 static int
11401 maybe_adjust_templates (void)
11402 {
11403 const insn_template *t;
11404
11405 gas_assert (i.mem_operands == 1);
11406
11407 for (t = current_templates->start; t < current_templates->end; ++t)
11408 if (t->opcode_modifier.isstring)
11409 break;
11410
11411 if (t < current_templates->end)
11412 {
11413 static templates aux_templates;
11414 bool recheck;
11415
11416 aux_templates.start = t;
11417 for (; t < current_templates->end; ++t)
11418 if (!t->opcode_modifier.isstring)
11419 break;
11420 aux_templates.end = t;
11421
11422 /* Determine whether to re-check the first memory operand. */
11423 recheck = (aux_templates.start != current_templates->start
11424 || t != current_templates->end);
11425
11426 current_templates = &aux_templates;
11427
11428 if (recheck)
11429 {
11430 i.mem_operands = 0;
11431 if (i.memop1_string != NULL
11432 && i386_index_check (i.memop1_string) == 0)
11433 return 0;
11434 i.mem_operands = 1;
11435 }
11436 }
11437
11438 return 1;
11439 }
11440
11441 static INLINE bool starts_memory_operand (char c)
11442 {
11443 return ISDIGIT (c)
11444 || is_identifier_char (c)
11445 || strchr ("([\"+-!~", c);
11446 }
11447
11448 /* Parse OPERAND_STRING into the i386_insn structure I. Returns zero
11449 on error. */
11450
11451 static int
11452 i386_att_operand (char *operand_string)
11453 {
11454 const reg_entry *r;
11455 char *end_op;
11456 char *op_string = operand_string;
11457
11458 if (is_space_char (*op_string))
11459 ++op_string;
11460
11461 /* We check for an absolute prefix (differentiating,
11462 for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
11463 if (*op_string == ABSOLUTE_PREFIX)
11464 {
11465 ++op_string;
11466 if (is_space_char (*op_string))
11467 ++op_string;
11468 i.jumpabsolute = true;
11469 }
11470
11471 /* Check if operand is a register. */
11472 if ((r = parse_register (op_string, &end_op)) != NULL)
11473 {
11474 i386_operand_type temp;
11475
11476 if (r == &bad_reg)
11477 return 0;
11478
11479 /* Check for a segment override by searching for ':' after a
11480 segment register. */
11481 op_string = end_op;
11482 if (is_space_char (*op_string))
11483 ++op_string;
11484 if (*op_string == ':' && r->reg_type.bitfield.class == SReg)
11485 {
11486 i.seg[i.mem_operands] = r;
11487
11488 /* Skip the ':' and whitespace. */
11489 ++op_string;
11490 if (is_space_char (*op_string))
11491 ++op_string;
11492
11493 /* Handle case of %es:*foo. */
11494 if (!i.jumpabsolute && *op_string == ABSOLUTE_PREFIX)
11495 {
11496 ++op_string;
11497 if (is_space_char (*op_string))
11498 ++op_string;
11499 i.jumpabsolute = true;
11500 }
11501
11502 if (!starts_memory_operand (*op_string))
11503 {
11504 as_bad (_("bad memory operand `%s'"), op_string);
11505 return 0;
11506 }
11507 goto do_memory_reference;
11508 }
11509
11510 /* Handle vector operations. */
11511 if (*op_string == '{')
11512 {
11513 op_string = check_VecOperations (op_string);
11514 if (op_string == NULL)
11515 return 0;
11516 }
11517
11518 if (*op_string)
11519 {
11520 as_bad (_("junk `%s' after register"), op_string);
11521 return 0;
11522 }
11523 temp = r->reg_type;
11524 temp.bitfield.baseindex = 0;
11525 i.types[this_operand] = operand_type_or (i.types[this_operand],
11526 temp);
11527 i.types[this_operand].bitfield.unspecified = 0;
11528 i.op[this_operand].regs = r;
11529 i.reg_operands++;
11530 }
11531 else if (*op_string == REGISTER_PREFIX)
11532 {
11533 as_bad (_("bad register name `%s'"), op_string);
11534 return 0;
11535 }
11536 else if (*op_string == IMMEDIATE_PREFIX)
11537 {
11538 ++op_string;
11539 if (i.jumpabsolute)
11540 {
11541 as_bad (_("immediate operand illegal with absolute jump"));
11542 return 0;
11543 }
11544 if (!i386_immediate (op_string))
11545 return 0;
11546 }
11547 else if (RC_SAE_immediate (operand_string))
11548 {
11549 /* If it is a RC or SAE immediate, do nothing. */
11550 ;
11551 }
11552 else if (starts_memory_operand (*op_string))
11553 {
11554 /* This is a memory reference of some sort. */
11555 char *base_string;
11556
11557 /* Start and end of displacement string expression (if found). */
11558 char *displacement_string_start;
11559 char *displacement_string_end;
11560
11561 do_memory_reference:
11562 if (i.mem_operands == 1 && !maybe_adjust_templates ())
11563 return 0;
11564 if ((i.mem_operands == 1
11565 && !current_templates->start->opcode_modifier.isstring)
11566 || i.mem_operands == 2)
11567 {
11568 as_bad (_("too many memory references for `%s'"),
11569 current_templates->start->name);
11570 return 0;
11571 }
11572
11573 /* Check for base index form. We detect the base index form by
11574 looking for an ')' at the end of the operand, searching
11575 for the '(' matching it, and finding a REGISTER_PREFIX or ','
11576 after the '('. */
11577 base_string = op_string + strlen (op_string);
11578
11579 /* Handle vector operations. */
11580 --base_string;
11581 if (is_space_char (*base_string))
11582 --base_string;
11583
11584 if (*base_string == '}')
11585 {
11586 char *vop_start = NULL;
11587
11588 while (base_string-- > op_string)
11589 {
11590 if (*base_string == '"')
11591 break;
11592 if (*base_string != '{')
11593 continue;
11594
11595 vop_start = base_string;
11596
11597 --base_string;
11598 if (is_space_char (*base_string))
11599 --base_string;
11600
11601 if (*base_string != '}')
11602 break;
11603
11604 vop_start = NULL;
11605 }
11606
11607 if (!vop_start)
11608 {
11609 as_bad (_("unbalanced figure braces"));
11610 return 0;
11611 }
11612
11613 if (check_VecOperations (vop_start) == NULL)
11614 return 0;
11615 }
11616
11617 /* If we only have a displacement, set-up for it to be parsed later. */
11618 displacement_string_start = op_string;
11619 displacement_string_end = base_string + 1;
11620
11621 if (*base_string == ')')
11622 {
11623 char *temp_string;
11624 unsigned int parens_not_balanced = 1;
11625
11626 /* We've already checked that the number of left & right ()'s are
11627 equal, so this loop will not be infinite. */
11628 do
11629 {
11630 base_string--;
11631 if (*base_string == ')')
11632 parens_not_balanced++;
11633 if (*base_string == '(')
11634 parens_not_balanced--;
11635 }
11636 while (parens_not_balanced && *base_string != '"');
11637
11638 temp_string = base_string;
11639
11640 /* Skip past '(' and whitespace. */
11641 if (*base_string == '(')
11642 ++base_string;
11643 if (is_space_char (*base_string))
11644 ++base_string;
11645
11646 if (*base_string == ','
11647 || ((i.base_reg = parse_register (base_string, &end_op))
11648 != NULL))
11649 {
11650 displacement_string_end = temp_string;
11651
11652 i.types[this_operand].bitfield.baseindex = 1;
11653
11654 if (i.base_reg)
11655 {
11656 if (i.base_reg == &bad_reg)
11657 return 0;
11658 base_string = end_op;
11659 if (is_space_char (*base_string))
11660 ++base_string;
11661 }
11662
11663 /* There may be an index reg or scale factor here. */
11664 if (*base_string == ',')
11665 {
11666 ++base_string;
11667 if (is_space_char (*base_string))
11668 ++base_string;
11669
11670 if ((i.index_reg = parse_register (base_string, &end_op))
11671 != NULL)
11672 {
11673 if (i.index_reg == &bad_reg)
11674 return 0;
11675 base_string = end_op;
11676 if (is_space_char (*base_string))
11677 ++base_string;
11678 if (*base_string == ',')
11679 {
11680 ++base_string;
11681 if (is_space_char (*base_string))
11682 ++base_string;
11683 }
11684 else if (*base_string != ')')
11685 {
11686 as_bad (_("expecting `,' or `)' "
11687 "after index register in `%s'"),
11688 operand_string);
11689 return 0;
11690 }
11691 }
11692 else if (*base_string == REGISTER_PREFIX)
11693 {
11694 end_op = strchr (base_string, ',');
11695 if (end_op)
11696 *end_op = '\0';
11697 as_bad (_("bad register name `%s'"), base_string);
11698 return 0;
11699 }
11700
11701 /* Check for scale factor. */
11702 if (*base_string != ')')
11703 {
11704 char *end_scale = i386_scale (base_string);
11705
11706 if (!end_scale)
11707 return 0;
11708
11709 base_string = end_scale;
11710 if (is_space_char (*base_string))
11711 ++base_string;
11712 if (*base_string != ')')
11713 {
11714 as_bad (_("expecting `)' "
11715 "after scale factor in `%s'"),
11716 operand_string);
11717 return 0;
11718 }
11719 }
11720 else if (!i.index_reg)
11721 {
11722 as_bad (_("expecting index register or scale factor "
11723 "after `,'; got '%c'"),
11724 *base_string);
11725 return 0;
11726 }
11727 }
11728 else if (*base_string != ')')
11729 {
11730 as_bad (_("expecting `,' or `)' "
11731 "after base register in `%s'"),
11732 operand_string);
11733 return 0;
11734 }
11735 }
11736 else if (*base_string == REGISTER_PREFIX)
11737 {
11738 end_op = strchr (base_string, ',');
11739 if (end_op)
11740 *end_op = '\0';
11741 as_bad (_("bad register name `%s'"), base_string);
11742 return 0;
11743 }
11744 }
11745
11746 /* If there's an expression beginning the operand, parse it,
11747 assuming displacement_string_start and
11748 displacement_string_end are meaningful. */
11749 if (displacement_string_start != displacement_string_end)
11750 {
11751 if (!i386_displacement (displacement_string_start,
11752 displacement_string_end))
11753 return 0;
11754 }
11755
11756 /* Special case for (%dx) while doing input/output op. */
11757 if (i.base_reg
11758 && i.base_reg->reg_type.bitfield.instance == RegD
11759 && i.base_reg->reg_type.bitfield.word
11760 && i.index_reg == 0
11761 && i.log2_scale_factor == 0
11762 && i.seg[i.mem_operands] == 0
11763 && !operand_type_check (i.types[this_operand], disp))
11764 {
11765 i.types[this_operand] = i.base_reg->reg_type;
11766 return 1;
11767 }
11768
11769 if (i386_index_check (operand_string) == 0)
11770 return 0;
11771 i.flags[this_operand] |= Operand_Mem;
11772 if (i.mem_operands == 0)
11773 i.memop1_string = xstrdup (operand_string);
11774 i.mem_operands++;
11775 }
11776 else
11777 {
11778 /* It's not a memory operand; argh! */
11779 as_bad (_("invalid char %s beginning operand %d `%s'"),
11780 output_invalid (*op_string),
11781 this_operand + 1,
11782 op_string);
11783 return 0;
11784 }
11785 return 1; /* Normal return. */
11786 }
11787 \f
11788 /* Calculate the maximum variable size (i.e., excluding fr_fix)
11789 that an rs_machine_dependent frag may reach. */
11790
11791 unsigned int
11792 i386_frag_max_var (fragS *frag)
11793 {
11794 /* The only relaxable frags are for jumps.
11795 Unconditional jumps can grow by 4 bytes and others by 5 bytes. */
11796 gas_assert (frag->fr_type == rs_machine_dependent);
11797 return TYPE_FROM_RELAX_STATE (frag->fr_subtype) == UNCOND_JUMP ? 4 : 5;
11798 }
11799
11800 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
11801 static int
11802 elf_symbol_resolved_in_segment_p (symbolS *fr_symbol, offsetT fr_var)
11803 {
11804 /* STT_GNU_IFUNC symbol must go through PLT. */
11805 if ((symbol_get_bfdsym (fr_symbol)->flags
11806 & BSF_GNU_INDIRECT_FUNCTION) != 0)
11807 return 0;
11808
11809 if (!S_IS_EXTERNAL (fr_symbol))
11810 /* Symbol may be weak or local. */
11811 return !S_IS_WEAK (fr_symbol);
11812
11813 /* Global symbols with non-default visibility can't be preempted. */
11814 if (ELF_ST_VISIBILITY (S_GET_OTHER (fr_symbol)) != STV_DEFAULT)
11815 return 1;
11816
11817 if (fr_var != NO_RELOC)
11818 switch ((enum bfd_reloc_code_real) fr_var)
11819 {
11820 case BFD_RELOC_386_PLT32:
11821 case BFD_RELOC_X86_64_PLT32:
11822 /* Symbol with PLT relocation may be preempted. */
11823 return 0;
11824 default:
11825 abort ();
11826 }
11827
11828 /* Global symbols with default visibility in a shared library may be
11829 preempted by another definition. */
11830 return !shared;
11831 }
11832 #endif
11833
11834 /* Table 3-2. Macro-Fusible Instructions in Haswell Microarchitecture
11835 Note also work for Skylake and Cascadelake.
11836 ---------------------------------------------------------------------
11837 | JCC | ADD/SUB/CMP | INC/DEC | TEST/AND |
11838 | ------ | ----------- | ------- | -------- |
11839 | Jo | N | N | Y |
11840 | Jno | N | N | Y |
11841 | Jc/Jb | Y | N | Y |
11842 | Jae/Jnb | Y | N | Y |
11843 | Je/Jz | Y | Y | Y |
11844 | Jne/Jnz | Y | Y | Y |
11845 | Jna/Jbe | Y | N | Y |
11846 | Ja/Jnbe | Y | N | Y |
11847 | Js | N | N | Y |
11848 | Jns | N | N | Y |
11849 | Jp/Jpe | N | N | Y |
11850 | Jnp/Jpo | N | N | Y |
11851 | Jl/Jnge | Y | Y | Y |
11852 | Jge/Jnl | Y | Y | Y |
11853 | Jle/Jng | Y | Y | Y |
11854 | Jg/Jnle | Y | Y | Y |
11855 --------------------------------------------------------------------- */
11856 static int
11857 i386_macro_fusible_p (enum mf_cmp_kind mf_cmp, enum mf_jcc_kind mf_jcc)
11858 {
11859 if (mf_cmp == mf_cmp_alu_cmp)
11860 return ((mf_jcc >= mf_jcc_jc && mf_jcc <= mf_jcc_jna)
11861 || mf_jcc == mf_jcc_jl || mf_jcc == mf_jcc_jle);
11862 if (mf_cmp == mf_cmp_incdec)
11863 return (mf_jcc == mf_jcc_je || mf_jcc == mf_jcc_jl
11864 || mf_jcc == mf_jcc_jle);
11865 if (mf_cmp == mf_cmp_test_and)
11866 return 1;
11867 return 0;
11868 }
11869
11870 /* Return the next non-empty frag. */
11871
11872 static fragS *
11873 i386_next_non_empty_frag (fragS *fragP)
11874 {
11875 /* There may be a frag with a ".fill 0" when there is no room in
11876 the current frag for frag_grow in output_insn. */
11877 for (fragP = fragP->fr_next;
11878 (fragP != NULL
11879 && fragP->fr_type == rs_fill
11880 && fragP->fr_fix == 0);
11881 fragP = fragP->fr_next)
11882 ;
11883 return fragP;
11884 }
11885
11886 /* Return the next jcc frag after BRANCH_PADDING. */
11887
11888 static fragS *
11889 i386_next_fusible_jcc_frag (fragS *maybe_cmp_fragP, fragS *pad_fragP)
11890 {
11891 fragS *branch_fragP;
11892 if (!pad_fragP)
11893 return NULL;
11894
11895 if (pad_fragP->fr_type == rs_machine_dependent
11896 && (TYPE_FROM_RELAX_STATE (pad_fragP->fr_subtype)
11897 == BRANCH_PADDING))
11898 {
11899 branch_fragP = i386_next_non_empty_frag (pad_fragP);
11900 if (branch_fragP->fr_type != rs_machine_dependent)
11901 return NULL;
11902 if (TYPE_FROM_RELAX_STATE (branch_fragP->fr_subtype) == COND_JUMP
11903 && i386_macro_fusible_p (maybe_cmp_fragP->tc_frag_data.mf_type,
11904 pad_fragP->tc_frag_data.mf_type))
11905 return branch_fragP;
11906 }
11907
11908 return NULL;
11909 }
11910
11911 /* Classify BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags. */
11912
11913 static void
11914 i386_classify_machine_dependent_frag (fragS *fragP)
11915 {
11916 fragS *cmp_fragP;
11917 fragS *pad_fragP;
11918 fragS *branch_fragP;
11919 fragS *next_fragP;
11920 unsigned int max_prefix_length;
11921
11922 if (fragP->tc_frag_data.classified)
11923 return;
11924
11925 /* First scan for BRANCH_PADDING and FUSED_JCC_PADDING. Convert
11926 FUSED_JCC_PADDING and merge BRANCH_PADDING. */
11927 for (next_fragP = fragP;
11928 next_fragP != NULL;
11929 next_fragP = next_fragP->fr_next)
11930 {
11931 next_fragP->tc_frag_data.classified = 1;
11932 if (next_fragP->fr_type == rs_machine_dependent)
11933 switch (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype))
11934 {
11935 case BRANCH_PADDING:
11936 /* The BRANCH_PADDING frag must be followed by a branch
11937 frag. */
11938 branch_fragP = i386_next_non_empty_frag (next_fragP);
11939 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
11940 break;
11941 case FUSED_JCC_PADDING:
11942 /* Check if this is a fused jcc:
11943 FUSED_JCC_PADDING
11944 CMP like instruction
11945 BRANCH_PADDING
11946 COND_JUMP
11947 */
11948 cmp_fragP = i386_next_non_empty_frag (next_fragP);
11949 pad_fragP = i386_next_non_empty_frag (cmp_fragP);
11950 branch_fragP = i386_next_fusible_jcc_frag (next_fragP, pad_fragP);
11951 if (branch_fragP)
11952 {
11953 /* The BRANCH_PADDING frag is merged with the
11954 FUSED_JCC_PADDING frag. */
11955 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
11956 /* CMP like instruction size. */
11957 next_fragP->tc_frag_data.cmp_size = cmp_fragP->fr_fix;
11958 frag_wane (pad_fragP);
11959 /* Skip to branch_fragP. */
11960 next_fragP = branch_fragP;
11961 }
11962 else if (next_fragP->tc_frag_data.max_prefix_length)
11963 {
11964 /* Turn FUSED_JCC_PADDING into BRANCH_PREFIX if it isn't
11965 a fused jcc. */
11966 next_fragP->fr_subtype
11967 = ENCODE_RELAX_STATE (BRANCH_PREFIX, 0);
11968 next_fragP->tc_frag_data.max_bytes
11969 = next_fragP->tc_frag_data.max_prefix_length;
11970 /* This will be updated in the BRANCH_PREFIX scan. */
11971 next_fragP->tc_frag_data.max_prefix_length = 0;
11972 }
11973 else
11974 frag_wane (next_fragP);
11975 break;
11976 }
11977 }
11978
11979 /* Stop if there is no BRANCH_PREFIX. */
11980 if (!align_branch_prefix_size)
11981 return;
11982
11983 /* Scan for BRANCH_PREFIX. */
11984 for (; fragP != NULL; fragP = fragP->fr_next)
11985 {
11986 if (fragP->fr_type != rs_machine_dependent
11987 || (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
11988 != BRANCH_PREFIX))
11989 continue;
11990
11991 /* Count all BRANCH_PREFIX frags before BRANCH_PADDING and
11992 COND_JUMP_PREFIX. */
11993 max_prefix_length = 0;
11994 for (next_fragP = fragP;
11995 next_fragP != NULL;
11996 next_fragP = next_fragP->fr_next)
11997 {
11998 if (next_fragP->fr_type == rs_fill)
11999 /* Skip rs_fill frags. */
12000 continue;
12001 else if (next_fragP->fr_type != rs_machine_dependent)
12002 /* Stop for all other frags. */
12003 break;
12004
12005 /* rs_machine_dependent frags. */
12006 if (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12007 == BRANCH_PREFIX)
12008 {
12009 /* Count BRANCH_PREFIX frags. */
12010 if (max_prefix_length >= MAX_FUSED_JCC_PADDING_SIZE)
12011 {
12012 max_prefix_length = MAX_FUSED_JCC_PADDING_SIZE;
12013 frag_wane (next_fragP);
12014 }
12015 else
12016 max_prefix_length
12017 += next_fragP->tc_frag_data.max_bytes;
12018 }
12019 else if ((TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12020 == BRANCH_PADDING)
12021 || (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12022 == FUSED_JCC_PADDING))
12023 {
12024 /* Stop at BRANCH_PADDING and FUSED_JCC_PADDING. */
12025 fragP->tc_frag_data.u.padding_fragP = next_fragP;
12026 break;
12027 }
12028 else
12029 /* Stop for other rs_machine_dependent frags. */
12030 break;
12031 }
12032
12033 fragP->tc_frag_data.max_prefix_length = max_prefix_length;
12034
12035 /* Skip to the next frag. */
12036 fragP = next_fragP;
12037 }
12038 }
12039
12040 /* Compute padding size for
12041
12042 FUSED_JCC_PADDING
12043 CMP like instruction
12044 BRANCH_PADDING
12045 COND_JUMP/UNCOND_JUMP
12046
12047 or
12048
12049 BRANCH_PADDING
12050 COND_JUMP/UNCOND_JUMP
12051 */
12052
12053 static int
12054 i386_branch_padding_size (fragS *fragP, offsetT address)
12055 {
12056 unsigned int offset, size, padding_size;
12057 fragS *branch_fragP = fragP->tc_frag_data.u.branch_fragP;
12058
12059 /* The start address of the BRANCH_PADDING or FUSED_JCC_PADDING frag. */
12060 if (!address)
12061 address = fragP->fr_address;
12062 address += fragP->fr_fix;
12063
12064 /* CMP like instrunction size. */
12065 size = fragP->tc_frag_data.cmp_size;
12066
12067 /* The base size of the branch frag. */
12068 size += branch_fragP->fr_fix;
12069
12070 /* Add opcode and displacement bytes for the rs_machine_dependent
12071 branch frag. */
12072 if (branch_fragP->fr_type == rs_machine_dependent)
12073 size += md_relax_table[branch_fragP->fr_subtype].rlx_length;
12074
12075 /* Check if branch is within boundary and doesn't end at the last
12076 byte. */
12077 offset = address & ((1U << align_branch_power) - 1);
12078 if ((offset + size) >= (1U << align_branch_power))
12079 /* Padding needed to avoid crossing boundary. */
12080 padding_size = (1U << align_branch_power) - offset;
12081 else
12082 /* No padding needed. */
12083 padding_size = 0;
12084
12085 /* The return value may be saved in tc_frag_data.length which is
12086 unsigned byte. */
12087 if (!fits_in_unsigned_byte (padding_size))
12088 abort ();
12089
12090 return padding_size;
12091 }
12092
12093 /* i386_generic_table_relax_frag()
12094
12095 Handle BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags to
12096 grow/shrink padding to align branch frags. Hand others to
12097 relax_frag(). */
12098
12099 long
12100 i386_generic_table_relax_frag (segT segment, fragS *fragP, long stretch)
12101 {
12102 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12103 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
12104 {
12105 long padding_size = i386_branch_padding_size (fragP, 0);
12106 long grow = padding_size - fragP->tc_frag_data.length;
12107
12108 /* When the BRANCH_PREFIX frag is used, the computed address
12109 must match the actual address and there should be no padding. */
12110 if (fragP->tc_frag_data.padding_address
12111 && (fragP->tc_frag_data.padding_address != fragP->fr_address
12112 || padding_size))
12113 abort ();
12114
12115 /* Update the padding size. */
12116 if (grow)
12117 fragP->tc_frag_data.length = padding_size;
12118
12119 return grow;
12120 }
12121 else if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12122 {
12123 fragS *padding_fragP, *next_fragP;
12124 long padding_size, left_size, last_size;
12125
12126 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
12127 if (!padding_fragP)
12128 /* Use the padding set by the leading BRANCH_PREFIX frag. */
12129 return (fragP->tc_frag_data.length
12130 - fragP->tc_frag_data.last_length);
12131
12132 /* Compute the relative address of the padding frag in the very
12133 first time where the BRANCH_PREFIX frag sizes are zero. */
12134 if (!fragP->tc_frag_data.padding_address)
12135 fragP->tc_frag_data.padding_address
12136 = padding_fragP->fr_address - (fragP->fr_address - stretch);
12137
12138 /* First update the last length from the previous interation. */
12139 left_size = fragP->tc_frag_data.prefix_length;
12140 for (next_fragP = fragP;
12141 next_fragP != padding_fragP;
12142 next_fragP = next_fragP->fr_next)
12143 if (next_fragP->fr_type == rs_machine_dependent
12144 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12145 == BRANCH_PREFIX))
12146 {
12147 if (left_size)
12148 {
12149 int max = next_fragP->tc_frag_data.max_bytes;
12150 if (max)
12151 {
12152 int size;
12153 if (max > left_size)
12154 size = left_size;
12155 else
12156 size = max;
12157 left_size -= size;
12158 next_fragP->tc_frag_data.last_length = size;
12159 }
12160 }
12161 else
12162 next_fragP->tc_frag_data.last_length = 0;
12163 }
12164
12165 /* Check the padding size for the padding frag. */
12166 padding_size = i386_branch_padding_size
12167 (padding_fragP, (fragP->fr_address
12168 + fragP->tc_frag_data.padding_address));
12169
12170 last_size = fragP->tc_frag_data.prefix_length;
12171 /* Check if there is change from the last interation. */
12172 if (padding_size == last_size)
12173 {
12174 /* Update the expected address of the padding frag. */
12175 padding_fragP->tc_frag_data.padding_address
12176 = (fragP->fr_address + padding_size
12177 + fragP->tc_frag_data.padding_address);
12178 return 0;
12179 }
12180
12181 if (padding_size > fragP->tc_frag_data.max_prefix_length)
12182 {
12183 /* No padding if there is no sufficient room. Clear the
12184 expected address of the padding frag. */
12185 padding_fragP->tc_frag_data.padding_address = 0;
12186 padding_size = 0;
12187 }
12188 else
12189 /* Store the expected address of the padding frag. */
12190 padding_fragP->tc_frag_data.padding_address
12191 = (fragP->fr_address + padding_size
12192 + fragP->tc_frag_data.padding_address);
12193
12194 fragP->tc_frag_data.prefix_length = padding_size;
12195
12196 /* Update the length for the current interation. */
12197 left_size = padding_size;
12198 for (next_fragP = fragP;
12199 next_fragP != padding_fragP;
12200 next_fragP = next_fragP->fr_next)
12201 if (next_fragP->fr_type == rs_machine_dependent
12202 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12203 == BRANCH_PREFIX))
12204 {
12205 if (left_size)
12206 {
12207 int max = next_fragP->tc_frag_data.max_bytes;
12208 if (max)
12209 {
12210 int size;
12211 if (max > left_size)
12212 size = left_size;
12213 else
12214 size = max;
12215 left_size -= size;
12216 next_fragP->tc_frag_data.length = size;
12217 }
12218 }
12219 else
12220 next_fragP->tc_frag_data.length = 0;
12221 }
12222
12223 return (fragP->tc_frag_data.length
12224 - fragP->tc_frag_data.last_length);
12225 }
12226 return relax_frag (segment, fragP, stretch);
12227 }
12228
12229 /* md_estimate_size_before_relax()
12230
12231 Called just before relax() for rs_machine_dependent frags. The x86
12232 assembler uses these frags to handle variable size jump
12233 instructions.
12234
12235 Any symbol that is now undefined will not become defined.
12236 Return the correct fr_subtype in the frag.
12237 Return the initial "guess for variable size of frag" to caller.
12238 The guess is actually the growth beyond the fixed part. Whatever
12239 we do to grow the fixed or variable part contributes to our
12240 returned value. */
12241
12242 int
12243 md_estimate_size_before_relax (fragS *fragP, segT segment)
12244 {
12245 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12246 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX
12247 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
12248 {
12249 i386_classify_machine_dependent_frag (fragP);
12250 return fragP->tc_frag_data.length;
12251 }
12252
12253 /* We've already got fragP->fr_subtype right; all we have to do is
12254 check for un-relaxable symbols. On an ELF system, we can't relax
12255 an externally visible symbol, because it may be overridden by a
12256 shared library. */
12257 if (S_GET_SEGMENT (fragP->fr_symbol) != segment
12258 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
12259 || (IS_ELF
12260 && !elf_symbol_resolved_in_segment_p (fragP->fr_symbol,
12261 fragP->fr_var))
12262 #endif
12263 #if defined (OBJ_COFF) && defined (TE_PE)
12264 || (OUTPUT_FLAVOR == bfd_target_coff_flavour
12265 && S_IS_WEAK (fragP->fr_symbol))
12266 #endif
12267 )
12268 {
12269 /* Symbol is undefined in this segment, or we need to keep a
12270 reloc so that weak symbols can be overridden. */
12271 int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
12272 enum bfd_reloc_code_real reloc_type;
12273 unsigned char *opcode;
12274 int old_fr_fix;
12275 fixS *fixP = NULL;
12276
12277 if (fragP->fr_var != NO_RELOC)
12278 reloc_type = (enum bfd_reloc_code_real) fragP->fr_var;
12279 else if (size == 2)
12280 reloc_type = BFD_RELOC_16_PCREL;
12281 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
12282 else if (fragP->tc_frag_data.code64 && fragP->fr_offset == 0
12283 && need_plt32_p (fragP->fr_symbol))
12284 reloc_type = BFD_RELOC_X86_64_PLT32;
12285 #endif
12286 else
12287 reloc_type = BFD_RELOC_32_PCREL;
12288
12289 old_fr_fix = fragP->fr_fix;
12290 opcode = (unsigned char *) fragP->fr_opcode;
12291
12292 switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
12293 {
12294 case UNCOND_JUMP:
12295 /* Make jmp (0xeb) a (d)word displacement jump. */
12296 opcode[0] = 0xe9;
12297 fragP->fr_fix += size;
12298 fixP = fix_new (fragP, old_fr_fix, size,
12299 fragP->fr_symbol,
12300 fragP->fr_offset, 1,
12301 reloc_type);
12302 break;
12303
12304 case COND_JUMP86:
12305 if (size == 2
12306 && (!no_cond_jump_promotion || fragP->fr_var != NO_RELOC))
12307 {
12308 /* Negate the condition, and branch past an
12309 unconditional jump. */
12310 opcode[0] ^= 1;
12311 opcode[1] = 3;
12312 /* Insert an unconditional jump. */
12313 opcode[2] = 0xe9;
12314 /* We added two extra opcode bytes, and have a two byte
12315 offset. */
12316 fragP->fr_fix += 2 + 2;
12317 fix_new (fragP, old_fr_fix + 2, 2,
12318 fragP->fr_symbol,
12319 fragP->fr_offset, 1,
12320 reloc_type);
12321 break;
12322 }
12323 /* Fall through. */
12324
12325 case COND_JUMP:
12326 if (no_cond_jump_promotion && fragP->fr_var == NO_RELOC)
12327 {
12328 fragP->fr_fix += 1;
12329 fixP = fix_new (fragP, old_fr_fix, 1,
12330 fragP->fr_symbol,
12331 fragP->fr_offset, 1,
12332 BFD_RELOC_8_PCREL);
12333 fixP->fx_signed = 1;
12334 break;
12335 }
12336
12337 /* This changes the byte-displacement jump 0x7N
12338 to the (d)word-displacement jump 0x0f,0x8N. */
12339 opcode[1] = opcode[0] + 0x10;
12340 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12341 /* We've added an opcode byte. */
12342 fragP->fr_fix += 1 + size;
12343 fixP = fix_new (fragP, old_fr_fix + 1, size,
12344 fragP->fr_symbol,
12345 fragP->fr_offset, 1,
12346 reloc_type);
12347 break;
12348
12349 default:
12350 BAD_CASE (fragP->fr_subtype);
12351 break;
12352 }
12353
12354 /* All jumps handled here are signed, but don't unconditionally use a
12355 signed limit check for 32 and 16 bit jumps as we want to allow wrap
12356 around at 4G (outside of 64-bit mode) and 64k. */
12357 if (size == 4 && flag_code == CODE_64BIT)
12358 fixP->fx_signed = 1;
12359
12360 frag_wane (fragP);
12361 return fragP->fr_fix - old_fr_fix;
12362 }
12363
12364 /* Guess size depending on current relax state. Initially the relax
12365 state will correspond to a short jump and we return 1, because
12366 the variable part of the frag (the branch offset) is one byte
12367 long. However, we can relax a section more than once and in that
12368 case we must either set fr_subtype back to the unrelaxed state,
12369 or return the value for the appropriate branch. */
12370 return md_relax_table[fragP->fr_subtype].rlx_length;
12371 }
12372
12373 /* Called after relax() is finished.
12374
12375 In: Address of frag.
12376 fr_type == rs_machine_dependent.
12377 fr_subtype is what the address relaxed to.
12378
12379 Out: Any fixSs and constants are set up.
12380 Caller will turn frag into a ".space 0". */
12381
12382 void
12383 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec ATTRIBUTE_UNUSED,
12384 fragS *fragP)
12385 {
12386 unsigned char *opcode;
12387 unsigned char *where_to_put_displacement = NULL;
12388 offsetT target_address;
12389 offsetT opcode_address;
12390 unsigned int extension = 0;
12391 offsetT displacement_from_opcode_start;
12392
12393 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12394 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING
12395 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12396 {
12397 /* Generate nop padding. */
12398 unsigned int size = fragP->tc_frag_data.length;
12399 if (size)
12400 {
12401 if (size > fragP->tc_frag_data.max_bytes)
12402 abort ();
12403
12404 if (flag_debug)
12405 {
12406 const char *msg;
12407 const char *branch = "branch";
12408 const char *prefix = "";
12409 fragS *padding_fragP;
12410 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
12411 == BRANCH_PREFIX)
12412 {
12413 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
12414 switch (fragP->tc_frag_data.default_prefix)
12415 {
12416 default:
12417 abort ();
12418 break;
12419 case CS_PREFIX_OPCODE:
12420 prefix = " cs";
12421 break;
12422 case DS_PREFIX_OPCODE:
12423 prefix = " ds";
12424 break;
12425 case ES_PREFIX_OPCODE:
12426 prefix = " es";
12427 break;
12428 case FS_PREFIX_OPCODE:
12429 prefix = " fs";
12430 break;
12431 case GS_PREFIX_OPCODE:
12432 prefix = " gs";
12433 break;
12434 case SS_PREFIX_OPCODE:
12435 prefix = " ss";
12436 break;
12437 }
12438 if (padding_fragP)
12439 msg = _("%s:%u: add %d%s at 0x%llx to align "
12440 "%s within %d-byte boundary\n");
12441 else
12442 msg = _("%s:%u: add additional %d%s at 0x%llx to "
12443 "align %s within %d-byte boundary\n");
12444 }
12445 else
12446 {
12447 padding_fragP = fragP;
12448 msg = _("%s:%u: add %d%s-byte nop at 0x%llx to align "
12449 "%s within %d-byte boundary\n");
12450 }
12451
12452 if (padding_fragP)
12453 switch (padding_fragP->tc_frag_data.branch_type)
12454 {
12455 case align_branch_jcc:
12456 branch = "jcc";
12457 break;
12458 case align_branch_fused:
12459 branch = "fused jcc";
12460 break;
12461 case align_branch_jmp:
12462 branch = "jmp";
12463 break;
12464 case align_branch_call:
12465 branch = "call";
12466 break;
12467 case align_branch_indirect:
12468 branch = "indiret branch";
12469 break;
12470 case align_branch_ret:
12471 branch = "ret";
12472 break;
12473 default:
12474 break;
12475 }
12476
12477 fprintf (stdout, msg,
12478 fragP->fr_file, fragP->fr_line, size, prefix,
12479 (long long) fragP->fr_address, branch,
12480 1 << align_branch_power);
12481 }
12482 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12483 memset (fragP->fr_opcode,
12484 fragP->tc_frag_data.default_prefix, size);
12485 else
12486 i386_generate_nops (fragP, (char *) fragP->fr_opcode,
12487 size, 0);
12488 fragP->fr_fix += size;
12489 }
12490 return;
12491 }
12492
12493 opcode = (unsigned char *) fragP->fr_opcode;
12494
12495 /* Address we want to reach in file space. */
12496 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
12497
12498 /* Address opcode resides at in file space. */
12499 opcode_address = fragP->fr_address + fragP->fr_fix;
12500
12501 /* Displacement from opcode start to fill into instruction. */
12502 displacement_from_opcode_start = target_address - opcode_address;
12503
12504 if ((fragP->fr_subtype & BIG) == 0)
12505 {
12506 /* Don't have to change opcode. */
12507 extension = 1; /* 1 opcode + 1 displacement */
12508 where_to_put_displacement = &opcode[1];
12509 }
12510 else
12511 {
12512 if (no_cond_jump_promotion
12513 && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
12514 as_warn_where (fragP->fr_file, fragP->fr_line,
12515 _("long jump required"));
12516
12517 switch (fragP->fr_subtype)
12518 {
12519 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
12520 extension = 4; /* 1 opcode + 4 displacement */
12521 opcode[0] = 0xe9;
12522 where_to_put_displacement = &opcode[1];
12523 break;
12524
12525 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
12526 extension = 2; /* 1 opcode + 2 displacement */
12527 opcode[0] = 0xe9;
12528 where_to_put_displacement = &opcode[1];
12529 break;
12530
12531 case ENCODE_RELAX_STATE (COND_JUMP, BIG):
12532 case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
12533 extension = 5; /* 2 opcode + 4 displacement */
12534 opcode[1] = opcode[0] + 0x10;
12535 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12536 where_to_put_displacement = &opcode[2];
12537 break;
12538
12539 case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
12540 extension = 3; /* 2 opcode + 2 displacement */
12541 opcode[1] = opcode[0] + 0x10;
12542 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12543 where_to_put_displacement = &opcode[2];
12544 break;
12545
12546 case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
12547 extension = 4;
12548 opcode[0] ^= 1;
12549 opcode[1] = 3;
12550 opcode[2] = 0xe9;
12551 where_to_put_displacement = &opcode[3];
12552 break;
12553
12554 default:
12555 BAD_CASE (fragP->fr_subtype);
12556 break;
12557 }
12558 }
12559
12560 /* If size if less then four we are sure that the operand fits,
12561 but if it's 4, then it could be that the displacement is larger
12562 then -/+ 2GB. */
12563 if (DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype) == 4
12564 && object_64bit
12565 && ((addressT) (displacement_from_opcode_start - extension
12566 + ((addressT) 1 << 31))
12567 > (((addressT) 2 << 31) - 1)))
12568 {
12569 as_bad_where (fragP->fr_file, fragP->fr_line,
12570 _("jump target out of range"));
12571 /* Make us emit 0. */
12572 displacement_from_opcode_start = extension;
12573 }
12574 /* Now put displacement after opcode. */
12575 md_number_to_chars ((char *) where_to_put_displacement,
12576 (valueT) (displacement_from_opcode_start - extension),
12577 DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
12578 fragP->fr_fix += extension;
12579 }
12580 \f
12581 /* Apply a fixup (fixP) to segment data, once it has been determined
12582 by our caller that we have all the info we need to fix it up.
12583
12584 Parameter valP is the pointer to the value of the bits.
12585
12586 On the 386, immediates, displacements, and data pointers are all in
12587 the same (little-endian) format, so we don't need to care about which
12588 we are handling. */
12589
12590 void
12591 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
12592 {
12593 char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
12594 valueT value = *valP;
12595
12596 #if !defined (TE_Mach)
12597 if (fixP->fx_pcrel)
12598 {
12599 switch (fixP->fx_r_type)
12600 {
12601 default:
12602 break;
12603
12604 case BFD_RELOC_64:
12605 fixP->fx_r_type = BFD_RELOC_64_PCREL;
12606 break;
12607 case BFD_RELOC_32:
12608 case BFD_RELOC_X86_64_32S:
12609 fixP->fx_r_type = BFD_RELOC_32_PCREL;
12610 break;
12611 case BFD_RELOC_16:
12612 fixP->fx_r_type = BFD_RELOC_16_PCREL;
12613 break;
12614 case BFD_RELOC_8:
12615 fixP->fx_r_type = BFD_RELOC_8_PCREL;
12616 break;
12617 }
12618 }
12619
12620 if (fixP->fx_addsy != NULL
12621 && (fixP->fx_r_type == BFD_RELOC_32_PCREL
12622 || fixP->fx_r_type == BFD_RELOC_64_PCREL
12623 || fixP->fx_r_type == BFD_RELOC_16_PCREL
12624 || fixP->fx_r_type == BFD_RELOC_8_PCREL)
12625 && !use_rela_relocations)
12626 {
12627 /* This is a hack. There should be a better way to handle this.
12628 This covers for the fact that bfd_install_relocation will
12629 subtract the current location (for partial_inplace, PC relative
12630 relocations); see more below. */
12631 #ifndef OBJ_AOUT
12632 if (IS_ELF
12633 #ifdef TE_PE
12634 || OUTPUT_FLAVOR == bfd_target_coff_flavour
12635 #endif
12636 )
12637 value += fixP->fx_where + fixP->fx_frag->fr_address;
12638 #endif
12639 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
12640 if (IS_ELF)
12641 {
12642 segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
12643
12644 if ((sym_seg == seg
12645 || (symbol_section_p (fixP->fx_addsy)
12646 && sym_seg != absolute_section))
12647 && !generic_force_reloc (fixP))
12648 {
12649 /* Yes, we add the values in twice. This is because
12650 bfd_install_relocation subtracts them out again. I think
12651 bfd_install_relocation is broken, but I don't dare change
12652 it. FIXME. */
12653 value += fixP->fx_where + fixP->fx_frag->fr_address;
12654 }
12655 }
12656 #endif
12657 #if defined (OBJ_COFF) && defined (TE_PE)
12658 /* For some reason, the PE format does not store a
12659 section address offset for a PC relative symbol. */
12660 if (S_GET_SEGMENT (fixP->fx_addsy) != seg
12661 || S_IS_WEAK (fixP->fx_addsy))
12662 value += md_pcrel_from (fixP);
12663 #endif
12664 }
12665 #if defined (OBJ_COFF) && defined (TE_PE)
12666 if (fixP->fx_addsy != NULL
12667 && S_IS_WEAK (fixP->fx_addsy)
12668 /* PR 16858: Do not modify weak function references. */
12669 && ! fixP->fx_pcrel)
12670 {
12671 #if !defined (TE_PEP)
12672 /* For x86 PE weak function symbols are neither PC-relative
12673 nor do they set S_IS_FUNCTION. So the only reliable way
12674 to detect them is to check the flags of their containing
12675 section. */
12676 if (S_GET_SEGMENT (fixP->fx_addsy) != NULL
12677 && S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_CODE)
12678 ;
12679 else
12680 #endif
12681 value -= S_GET_VALUE (fixP->fx_addsy);
12682 }
12683 #endif
12684
12685 /* Fix a few things - the dynamic linker expects certain values here,
12686 and we must not disappoint it. */
12687 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
12688 if (IS_ELF && fixP->fx_addsy)
12689 switch (fixP->fx_r_type)
12690 {
12691 case BFD_RELOC_386_PLT32:
12692 case BFD_RELOC_X86_64_PLT32:
12693 /* Make the jump instruction point to the address of the operand.
12694 At runtime we merely add the offset to the actual PLT entry.
12695 NB: Subtract the offset size only for jump instructions. */
12696 if (fixP->fx_pcrel)
12697 value = -4;
12698 break;
12699
12700 case BFD_RELOC_386_TLS_GD:
12701 case BFD_RELOC_386_TLS_LDM:
12702 case BFD_RELOC_386_TLS_IE_32:
12703 case BFD_RELOC_386_TLS_IE:
12704 case BFD_RELOC_386_TLS_GOTIE:
12705 case BFD_RELOC_386_TLS_GOTDESC:
12706 case BFD_RELOC_X86_64_TLSGD:
12707 case BFD_RELOC_X86_64_TLSLD:
12708 case BFD_RELOC_X86_64_GOTTPOFF:
12709 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
12710 value = 0; /* Fully resolved at runtime. No addend. */
12711 /* Fallthrough */
12712 case BFD_RELOC_386_TLS_LE:
12713 case BFD_RELOC_386_TLS_LDO_32:
12714 case BFD_RELOC_386_TLS_LE_32:
12715 case BFD_RELOC_X86_64_DTPOFF32:
12716 case BFD_RELOC_X86_64_DTPOFF64:
12717 case BFD_RELOC_X86_64_TPOFF32:
12718 case BFD_RELOC_X86_64_TPOFF64:
12719 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12720 break;
12721
12722 case BFD_RELOC_386_TLS_DESC_CALL:
12723 case BFD_RELOC_X86_64_TLSDESC_CALL:
12724 value = 0; /* Fully resolved at runtime. No addend. */
12725 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12726 fixP->fx_done = 0;
12727 return;
12728
12729 case BFD_RELOC_VTABLE_INHERIT:
12730 case BFD_RELOC_VTABLE_ENTRY:
12731 fixP->fx_done = 0;
12732 return;
12733
12734 default:
12735 break;
12736 }
12737 #endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) */
12738
12739 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
12740 if (!object_64bit)
12741 value = extend_to_32bit_address (value);
12742
12743 *valP = value;
12744 #endif /* !defined (TE_Mach) */
12745
12746 /* Are we finished with this relocation now? */
12747 if (fixP->fx_addsy == NULL)
12748 {
12749 fixP->fx_done = 1;
12750 switch (fixP->fx_r_type)
12751 {
12752 case BFD_RELOC_X86_64_32S:
12753 fixP->fx_signed = 1;
12754 break;
12755
12756 default:
12757 break;
12758 }
12759 }
12760 #if defined (OBJ_COFF) && defined (TE_PE)
12761 else if (fixP->fx_addsy != NULL && S_IS_WEAK (fixP->fx_addsy))
12762 {
12763 fixP->fx_done = 0;
12764 /* Remember value for tc_gen_reloc. */
12765 fixP->fx_addnumber = value;
12766 /* Clear out the frag for now. */
12767 value = 0;
12768 }
12769 #endif
12770 else if (use_rela_relocations)
12771 {
12772 if (!disallow_64bit_reloc || fixP->fx_r_type == NO_RELOC)
12773 fixP->fx_no_overflow = 1;
12774 /* Remember value for tc_gen_reloc. */
12775 fixP->fx_addnumber = value;
12776 value = 0;
12777 }
12778
12779 md_number_to_chars (p, value, fixP->fx_size);
12780 }
12781 \f
12782 const char *
12783 md_atof (int type, char *litP, int *sizeP)
12784 {
12785 /* This outputs the LITTLENUMs in REVERSE order;
12786 in accord with the bigendian 386. */
12787 return ieee_md_atof (type, litP, sizeP, false);
12788 }
12789 \f
12790 static char output_invalid_buf[sizeof (unsigned char) * 2 + 6];
12791
12792 static char *
12793 output_invalid (int c)
12794 {
12795 if (ISPRINT (c))
12796 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
12797 "'%c'", c);
12798 else
12799 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
12800 "(0x%x)", (unsigned char) c);
12801 return output_invalid_buf;
12802 }
12803
12804 /* Verify that @r can be used in the current context. */
12805
12806 static bool check_register (const reg_entry *r)
12807 {
12808 if (allow_pseudo_reg)
12809 return true;
12810
12811 if (operand_type_all_zero (&r->reg_type))
12812 return false;
12813
12814 if ((r->reg_type.bitfield.dword
12815 || (r->reg_type.bitfield.class == SReg && r->reg_num > 3)
12816 || r->reg_type.bitfield.class == RegCR
12817 || r->reg_type.bitfield.class == RegDR)
12818 && !cpu_arch_flags.bitfield.cpui386)
12819 return false;
12820
12821 if (r->reg_type.bitfield.class == RegTR
12822 && (flag_code == CODE_64BIT
12823 || !cpu_arch_flags.bitfield.cpui386
12824 || cpu_arch_isa_flags.bitfield.cpui586
12825 || cpu_arch_isa_flags.bitfield.cpui686))
12826 return false;
12827
12828 if (r->reg_type.bitfield.class == RegMMX && !cpu_arch_flags.bitfield.cpummx)
12829 return false;
12830
12831 if (!cpu_arch_flags.bitfield.cpuavx512f)
12832 {
12833 if (r->reg_type.bitfield.zmmword
12834 || r->reg_type.bitfield.class == RegMask)
12835 return false;
12836
12837 if (!cpu_arch_flags.bitfield.cpuavx)
12838 {
12839 if (r->reg_type.bitfield.ymmword)
12840 return false;
12841
12842 if (!cpu_arch_flags.bitfield.cpusse && r->reg_type.bitfield.xmmword)
12843 return false;
12844 }
12845 }
12846
12847 if (r->reg_type.bitfield.tmmword
12848 && (!cpu_arch_flags.bitfield.cpuamx_tile
12849 || flag_code != CODE_64BIT))
12850 return false;
12851
12852 if (r->reg_type.bitfield.class == RegBND && !cpu_arch_flags.bitfield.cpumpx)
12853 return false;
12854
12855 /* Don't allow fake index register unless allow_index_reg isn't 0. */
12856 if (!allow_index_reg && r->reg_num == RegIZ)
12857 return false;
12858
12859 /* Upper 16 vector registers are only available with VREX in 64bit
12860 mode, and require EVEX encoding. */
12861 if (r->reg_flags & RegVRex)
12862 {
12863 if (!cpu_arch_flags.bitfield.cpuavx512f
12864 || flag_code != CODE_64BIT)
12865 return false;
12866
12867 if (i.vec_encoding == vex_encoding_default)
12868 i.vec_encoding = vex_encoding_evex;
12869 else if (i.vec_encoding != vex_encoding_evex)
12870 i.vec_encoding = vex_encoding_error;
12871 }
12872
12873 if (((r->reg_flags & (RegRex64 | RegRex)) || r->reg_type.bitfield.qword)
12874 && (!cpu_arch_flags.bitfield.cpulm || r->reg_type.bitfield.class != RegCR)
12875 && flag_code != CODE_64BIT)
12876 return false;
12877
12878 if (r->reg_type.bitfield.class == SReg && r->reg_num == RegFlat
12879 && !intel_syntax)
12880 return false;
12881
12882 return true;
12883 }
12884
12885 /* REG_STRING starts *before* REGISTER_PREFIX. */
12886
12887 static const reg_entry *
12888 parse_real_register (char *reg_string, char **end_op)
12889 {
12890 char *s = reg_string;
12891 char *p;
12892 char reg_name_given[MAX_REG_NAME_SIZE + 1];
12893 const reg_entry *r;
12894
12895 /* Skip possible REGISTER_PREFIX and possible whitespace. */
12896 if (*s == REGISTER_PREFIX)
12897 ++s;
12898
12899 if (is_space_char (*s))
12900 ++s;
12901
12902 p = reg_name_given;
12903 while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
12904 {
12905 if (p >= reg_name_given + MAX_REG_NAME_SIZE)
12906 return (const reg_entry *) NULL;
12907 s++;
12908 }
12909
12910 /* For naked regs, make sure that we are not dealing with an identifier.
12911 This prevents confusing an identifier like `eax_var' with register
12912 `eax'. */
12913 if (allow_naked_reg && identifier_chars[(unsigned char) *s])
12914 return (const reg_entry *) NULL;
12915
12916 *end_op = s;
12917
12918 r = (const reg_entry *) str_hash_find (reg_hash, reg_name_given);
12919
12920 /* Handle floating point regs, allowing spaces in the (i) part. */
12921 if (r == reg_st0)
12922 {
12923 if (!cpu_arch_flags.bitfield.cpu8087
12924 && !cpu_arch_flags.bitfield.cpu287
12925 && !cpu_arch_flags.bitfield.cpu387
12926 && !allow_pseudo_reg)
12927 return (const reg_entry *) NULL;
12928
12929 if (is_space_char (*s))
12930 ++s;
12931 if (*s == '(')
12932 {
12933 ++s;
12934 if (is_space_char (*s))
12935 ++s;
12936 if (*s >= '0' && *s <= '7')
12937 {
12938 int fpr = *s - '0';
12939 ++s;
12940 if (is_space_char (*s))
12941 ++s;
12942 if (*s == ')')
12943 {
12944 *end_op = s + 1;
12945 know (r[fpr].reg_num == fpr);
12946 return r + fpr;
12947 }
12948 }
12949 /* We have "%st(" then garbage. */
12950 return (const reg_entry *) NULL;
12951 }
12952 }
12953
12954 return r && check_register (r) ? r : NULL;
12955 }
12956
12957 /* REG_STRING starts *before* REGISTER_PREFIX. */
12958
12959 static const reg_entry *
12960 parse_register (char *reg_string, char **end_op)
12961 {
12962 const reg_entry *r;
12963
12964 if (*reg_string == REGISTER_PREFIX || allow_naked_reg)
12965 r = parse_real_register (reg_string, end_op);
12966 else
12967 r = NULL;
12968 if (!r)
12969 {
12970 char *save = input_line_pointer;
12971 char c;
12972 symbolS *symbolP;
12973
12974 input_line_pointer = reg_string;
12975 c = get_symbol_name (&reg_string);
12976 symbolP = symbol_find (reg_string);
12977 if (symbolP && S_GET_SEGMENT (symbolP) == reg_section)
12978 {
12979 const expressionS *e = symbol_get_value_expression (symbolP);
12980
12981 if (e->X_op == O_register
12982 && (valueT) e->X_add_number < i386_regtab_size)
12983 {
12984 r = i386_regtab + e->X_add_number;
12985 if (!check_register (r))
12986 {
12987 as_bad (_("register '%s%s' cannot be used here"),
12988 register_prefix, r->reg_name);
12989 r = &bad_reg;
12990 }
12991 *end_op = input_line_pointer;
12992 }
12993 }
12994 *input_line_pointer = c;
12995 input_line_pointer = save;
12996 }
12997 return r;
12998 }
12999
13000 int
13001 i386_parse_name (char *name, expressionS *e, char *nextcharP)
13002 {
13003 const reg_entry *r;
13004 char *end = input_line_pointer;
13005
13006 *end = *nextcharP;
13007 r = parse_register (name, &input_line_pointer);
13008 if (r && end <= input_line_pointer)
13009 {
13010 *nextcharP = *input_line_pointer;
13011 *input_line_pointer = 0;
13012 if (r != &bad_reg)
13013 {
13014 e->X_op = O_register;
13015 e->X_add_number = r - i386_regtab;
13016 }
13017 else
13018 e->X_op = O_illegal;
13019 return 1;
13020 }
13021 input_line_pointer = end;
13022 *end = 0;
13023 return intel_syntax ? i386_intel_parse_name (name, e) : 0;
13024 }
13025
13026 void
13027 md_operand (expressionS *e)
13028 {
13029 char *end;
13030 const reg_entry *r;
13031
13032 switch (*input_line_pointer)
13033 {
13034 case REGISTER_PREFIX:
13035 r = parse_real_register (input_line_pointer, &end);
13036 if (r)
13037 {
13038 e->X_op = O_register;
13039 e->X_add_number = r - i386_regtab;
13040 input_line_pointer = end;
13041 }
13042 break;
13043
13044 case '[':
13045 gas_assert (intel_syntax);
13046 end = input_line_pointer++;
13047 expression (e);
13048 if (*input_line_pointer == ']')
13049 {
13050 ++input_line_pointer;
13051 e->X_op_symbol = make_expr_symbol (e);
13052 e->X_add_symbol = NULL;
13053 e->X_add_number = 0;
13054 e->X_op = O_index;
13055 }
13056 else
13057 {
13058 e->X_op = O_absent;
13059 input_line_pointer = end;
13060 }
13061 break;
13062 }
13063 }
13064
13065 \f
13066 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13067 const char *md_shortopts = "kVQ:sqnO::";
13068 #else
13069 const char *md_shortopts = "qnO::";
13070 #endif
13071
13072 #define OPTION_32 (OPTION_MD_BASE + 0)
13073 #define OPTION_64 (OPTION_MD_BASE + 1)
13074 #define OPTION_DIVIDE (OPTION_MD_BASE + 2)
13075 #define OPTION_MARCH (OPTION_MD_BASE + 3)
13076 #define OPTION_MTUNE (OPTION_MD_BASE + 4)
13077 #define OPTION_MMNEMONIC (OPTION_MD_BASE + 5)
13078 #define OPTION_MSYNTAX (OPTION_MD_BASE + 6)
13079 #define OPTION_MINDEX_REG (OPTION_MD_BASE + 7)
13080 #define OPTION_MNAKED_REG (OPTION_MD_BASE + 8)
13081 #define OPTION_MRELAX_RELOCATIONS (OPTION_MD_BASE + 9)
13082 #define OPTION_MSSE2AVX (OPTION_MD_BASE + 10)
13083 #define OPTION_MSSE_CHECK (OPTION_MD_BASE + 11)
13084 #define OPTION_MOPERAND_CHECK (OPTION_MD_BASE + 12)
13085 #define OPTION_MAVXSCALAR (OPTION_MD_BASE + 13)
13086 #define OPTION_X32 (OPTION_MD_BASE + 14)
13087 #define OPTION_MADD_BND_PREFIX (OPTION_MD_BASE + 15)
13088 #define OPTION_MEVEXLIG (OPTION_MD_BASE + 16)
13089 #define OPTION_MEVEXWIG (OPTION_MD_BASE + 17)
13090 #define OPTION_MBIG_OBJ (OPTION_MD_BASE + 18)
13091 #define OPTION_MOMIT_LOCK_PREFIX (OPTION_MD_BASE + 19)
13092 #define OPTION_MEVEXRCIG (OPTION_MD_BASE + 20)
13093 #define OPTION_MSHARED (OPTION_MD_BASE + 21)
13094 #define OPTION_MAMD64 (OPTION_MD_BASE + 22)
13095 #define OPTION_MINTEL64 (OPTION_MD_BASE + 23)
13096 #define OPTION_MFENCE_AS_LOCK_ADD (OPTION_MD_BASE + 24)
13097 #define OPTION_X86_USED_NOTE (OPTION_MD_BASE + 25)
13098 #define OPTION_MVEXWIG (OPTION_MD_BASE + 26)
13099 #define OPTION_MALIGN_BRANCH_BOUNDARY (OPTION_MD_BASE + 27)
13100 #define OPTION_MALIGN_BRANCH_PREFIX_SIZE (OPTION_MD_BASE + 28)
13101 #define OPTION_MALIGN_BRANCH (OPTION_MD_BASE + 29)
13102 #define OPTION_MBRANCHES_WITH_32B_BOUNDARIES (OPTION_MD_BASE + 30)
13103 #define OPTION_MLFENCE_AFTER_LOAD (OPTION_MD_BASE + 31)
13104 #define OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH (OPTION_MD_BASE + 32)
13105 #define OPTION_MLFENCE_BEFORE_RET (OPTION_MD_BASE + 33)
13106 #define OPTION_MUSE_UNALIGNED_VECTOR_MOVE (OPTION_MD_BASE + 34)
13107
13108 struct option md_longopts[] =
13109 {
13110 {"32", no_argument, NULL, OPTION_32},
13111 #if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
13112 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
13113 {"64", no_argument, NULL, OPTION_64},
13114 #endif
13115 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13116 {"x32", no_argument, NULL, OPTION_X32},
13117 {"mshared", no_argument, NULL, OPTION_MSHARED},
13118 {"mx86-used-note", required_argument, NULL, OPTION_X86_USED_NOTE},
13119 #endif
13120 {"divide", no_argument, NULL, OPTION_DIVIDE},
13121 {"march", required_argument, NULL, OPTION_MARCH},
13122 {"mtune", required_argument, NULL, OPTION_MTUNE},
13123 {"mmnemonic", required_argument, NULL, OPTION_MMNEMONIC},
13124 {"msyntax", required_argument, NULL, OPTION_MSYNTAX},
13125 {"mindex-reg", no_argument, NULL, OPTION_MINDEX_REG},
13126 {"mnaked-reg", no_argument, NULL, OPTION_MNAKED_REG},
13127 {"msse2avx", no_argument, NULL, OPTION_MSSE2AVX},
13128 {"muse-unaligned-vector-move", no_argument, NULL, OPTION_MUSE_UNALIGNED_VECTOR_MOVE},
13129 {"msse-check", required_argument, NULL, OPTION_MSSE_CHECK},
13130 {"moperand-check", required_argument, NULL, OPTION_MOPERAND_CHECK},
13131 {"mavxscalar", required_argument, NULL, OPTION_MAVXSCALAR},
13132 {"mvexwig", required_argument, NULL, OPTION_MVEXWIG},
13133 {"madd-bnd-prefix", no_argument, NULL, OPTION_MADD_BND_PREFIX},
13134 {"mevexlig", required_argument, NULL, OPTION_MEVEXLIG},
13135 {"mevexwig", required_argument, NULL, OPTION_MEVEXWIG},
13136 # if defined (TE_PE) || defined (TE_PEP)
13137 {"mbig-obj", no_argument, NULL, OPTION_MBIG_OBJ},
13138 #endif
13139 {"momit-lock-prefix", required_argument, NULL, OPTION_MOMIT_LOCK_PREFIX},
13140 {"mfence-as-lock-add", required_argument, NULL, OPTION_MFENCE_AS_LOCK_ADD},
13141 {"mrelax-relocations", required_argument, NULL, OPTION_MRELAX_RELOCATIONS},
13142 {"mevexrcig", required_argument, NULL, OPTION_MEVEXRCIG},
13143 {"malign-branch-boundary", required_argument, NULL, OPTION_MALIGN_BRANCH_BOUNDARY},
13144 {"malign-branch-prefix-size", required_argument, NULL, OPTION_MALIGN_BRANCH_PREFIX_SIZE},
13145 {"malign-branch", required_argument, NULL, OPTION_MALIGN_BRANCH},
13146 {"mbranches-within-32B-boundaries", no_argument, NULL, OPTION_MBRANCHES_WITH_32B_BOUNDARIES},
13147 {"mlfence-after-load", required_argument, NULL, OPTION_MLFENCE_AFTER_LOAD},
13148 {"mlfence-before-indirect-branch", required_argument, NULL,
13149 OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH},
13150 {"mlfence-before-ret", required_argument, NULL, OPTION_MLFENCE_BEFORE_RET},
13151 {"mamd64", no_argument, NULL, OPTION_MAMD64},
13152 {"mintel64", no_argument, NULL, OPTION_MINTEL64},
13153 {NULL, no_argument, NULL, 0}
13154 };
13155 size_t md_longopts_size = sizeof (md_longopts);
13156
13157 int
13158 md_parse_option (int c, const char *arg)
13159 {
13160 unsigned int j;
13161 char *arch, *next, *saved, *type;
13162
13163 switch (c)
13164 {
13165 case 'n':
13166 optimize_align_code = 0;
13167 break;
13168
13169 case 'q':
13170 quiet_warnings = 1;
13171 break;
13172
13173 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13174 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
13175 should be emitted or not. FIXME: Not implemented. */
13176 case 'Q':
13177 if ((arg[0] != 'y' && arg[0] != 'n') || arg[1])
13178 return 0;
13179 break;
13180
13181 /* -V: SVR4 argument to print version ID. */
13182 case 'V':
13183 print_version_id ();
13184 break;
13185
13186 /* -k: Ignore for FreeBSD compatibility. */
13187 case 'k':
13188 break;
13189
13190 case 's':
13191 /* -s: On i386 Solaris, this tells the native assembler to use
13192 .stab instead of .stab.excl. We always use .stab anyhow. */
13193 break;
13194
13195 case OPTION_MSHARED:
13196 shared = 1;
13197 break;
13198
13199 case OPTION_X86_USED_NOTE:
13200 if (strcasecmp (arg, "yes") == 0)
13201 x86_used_note = 1;
13202 else if (strcasecmp (arg, "no") == 0)
13203 x86_used_note = 0;
13204 else
13205 as_fatal (_("invalid -mx86-used-note= option: `%s'"), arg);
13206 break;
13207
13208
13209 #endif
13210 #if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
13211 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
13212 case OPTION_64:
13213 {
13214 const char **list, **l;
13215
13216 list = bfd_target_list ();
13217 for (l = list; *l != NULL; l++)
13218 if (startswith (*l, "elf64-x86-64")
13219 || strcmp (*l, "coff-x86-64") == 0
13220 || strcmp (*l, "pe-x86-64") == 0
13221 || strcmp (*l, "pei-x86-64") == 0
13222 || strcmp (*l, "mach-o-x86-64") == 0)
13223 {
13224 default_arch = "x86_64";
13225 break;
13226 }
13227 if (*l == NULL)
13228 as_fatal (_("no compiled in support for x86_64"));
13229 free (list);
13230 }
13231 break;
13232 #endif
13233
13234 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13235 case OPTION_X32:
13236 if (IS_ELF)
13237 {
13238 const char **list, **l;
13239
13240 list = bfd_target_list ();
13241 for (l = list; *l != NULL; l++)
13242 if (startswith (*l, "elf32-x86-64"))
13243 {
13244 default_arch = "x86_64:32";
13245 break;
13246 }
13247 if (*l == NULL)
13248 as_fatal (_("no compiled in support for 32bit x86_64"));
13249 free (list);
13250 }
13251 else
13252 as_fatal (_("32bit x86_64 is only supported for ELF"));
13253 break;
13254 #endif
13255
13256 case OPTION_32:
13257 default_arch = "i386";
13258 break;
13259
13260 case OPTION_DIVIDE:
13261 #ifdef SVR4_COMMENT_CHARS
13262 {
13263 char *n, *t;
13264 const char *s;
13265
13266 n = XNEWVEC (char, strlen (i386_comment_chars) + 1);
13267 t = n;
13268 for (s = i386_comment_chars; *s != '\0'; s++)
13269 if (*s != '/')
13270 *t++ = *s;
13271 *t = '\0';
13272 i386_comment_chars = n;
13273 }
13274 #endif
13275 break;
13276
13277 case OPTION_MARCH:
13278 saved = xstrdup (arg);
13279 arch = saved;
13280 /* Allow -march=+nosse. */
13281 if (*arch == '+')
13282 arch++;
13283 do
13284 {
13285 if (*arch == '.')
13286 as_fatal (_("invalid -march= option: `%s'"), arg);
13287 next = strchr (arch, '+');
13288 if (next)
13289 *next++ = '\0';
13290 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13291 {
13292 if (arch == saved && strcmp (arch, cpu_arch [j].name) == 0)
13293 {
13294 /* Processor. */
13295 if (! cpu_arch[j].flags.bitfield.cpui386)
13296 continue;
13297
13298 cpu_arch_name = cpu_arch[j].name;
13299 cpu_sub_arch_name = NULL;
13300 cpu_arch_flags = cpu_arch[j].flags;
13301 cpu_arch_isa = cpu_arch[j].type;
13302 cpu_arch_isa_flags = cpu_arch[j].flags;
13303 if (!cpu_arch_tune_set)
13304 {
13305 cpu_arch_tune = cpu_arch_isa;
13306 cpu_arch_tune_flags = cpu_arch_isa_flags;
13307 }
13308 break;
13309 }
13310 else if (*cpu_arch [j].name == '.'
13311 && strcmp (arch, cpu_arch [j].name + 1) == 0)
13312 {
13313 /* ISA extension. */
13314 i386_cpu_flags flags;
13315
13316 flags = cpu_flags_or (cpu_arch_flags,
13317 cpu_arch[j].flags);
13318
13319 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
13320 {
13321 if (cpu_sub_arch_name)
13322 {
13323 char *name = cpu_sub_arch_name;
13324 cpu_sub_arch_name = concat (name,
13325 cpu_arch[j].name,
13326 (const char *) NULL);
13327 free (name);
13328 }
13329 else
13330 cpu_sub_arch_name = xstrdup (cpu_arch[j].name);
13331 cpu_arch_flags = flags;
13332 cpu_arch_isa_flags = flags;
13333 }
13334 else
13335 cpu_arch_isa_flags
13336 = cpu_flags_or (cpu_arch_isa_flags,
13337 cpu_arch[j].flags);
13338 break;
13339 }
13340 }
13341
13342 if (j >= ARRAY_SIZE (cpu_arch))
13343 {
13344 /* Disable an ISA extension. */
13345 for (j = 0; j < ARRAY_SIZE (cpu_noarch); j++)
13346 if (strcmp (arch, cpu_noarch [j].name) == 0)
13347 {
13348 i386_cpu_flags flags;
13349
13350 flags = cpu_flags_and_not (cpu_arch_flags,
13351 cpu_noarch[j].flags);
13352 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
13353 {
13354 if (cpu_sub_arch_name)
13355 {
13356 char *name = cpu_sub_arch_name;
13357 cpu_sub_arch_name = concat (arch,
13358 (const char *) NULL);
13359 free (name);
13360 }
13361 else
13362 cpu_sub_arch_name = xstrdup (arch);
13363 cpu_arch_flags = flags;
13364 cpu_arch_isa_flags = flags;
13365 }
13366 break;
13367 }
13368
13369 if (j >= ARRAY_SIZE (cpu_noarch))
13370 j = ARRAY_SIZE (cpu_arch);
13371 }
13372
13373 if (j >= ARRAY_SIZE (cpu_arch))
13374 as_fatal (_("invalid -march= option: `%s'"), arg);
13375
13376 arch = next;
13377 }
13378 while (next != NULL);
13379 free (saved);
13380 break;
13381
13382 case OPTION_MTUNE:
13383 if (*arg == '.')
13384 as_fatal (_("invalid -mtune= option: `%s'"), arg);
13385 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13386 {
13387 if (strcmp (arg, cpu_arch [j].name) == 0)
13388 {
13389 cpu_arch_tune_set = 1;
13390 cpu_arch_tune = cpu_arch [j].type;
13391 cpu_arch_tune_flags = cpu_arch[j].flags;
13392 break;
13393 }
13394 }
13395 if (j >= ARRAY_SIZE (cpu_arch))
13396 as_fatal (_("invalid -mtune= option: `%s'"), arg);
13397 break;
13398
13399 case OPTION_MMNEMONIC:
13400 if (strcasecmp (arg, "att") == 0)
13401 intel_mnemonic = 0;
13402 else if (strcasecmp (arg, "intel") == 0)
13403 intel_mnemonic = 1;
13404 else
13405 as_fatal (_("invalid -mmnemonic= option: `%s'"), arg);
13406 break;
13407
13408 case OPTION_MSYNTAX:
13409 if (strcasecmp (arg, "att") == 0)
13410 intel_syntax = 0;
13411 else if (strcasecmp (arg, "intel") == 0)
13412 intel_syntax = 1;
13413 else
13414 as_fatal (_("invalid -msyntax= option: `%s'"), arg);
13415 break;
13416
13417 case OPTION_MINDEX_REG:
13418 allow_index_reg = 1;
13419 break;
13420
13421 case OPTION_MNAKED_REG:
13422 allow_naked_reg = 1;
13423 break;
13424
13425 case OPTION_MSSE2AVX:
13426 sse2avx = 1;
13427 break;
13428
13429 case OPTION_MUSE_UNALIGNED_VECTOR_MOVE:
13430 use_unaligned_vector_move = 1;
13431 break;
13432
13433 case OPTION_MSSE_CHECK:
13434 if (strcasecmp (arg, "error") == 0)
13435 sse_check = check_error;
13436 else if (strcasecmp (arg, "warning") == 0)
13437 sse_check = check_warning;
13438 else if (strcasecmp (arg, "none") == 0)
13439 sse_check = check_none;
13440 else
13441 as_fatal (_("invalid -msse-check= option: `%s'"), arg);
13442 break;
13443
13444 case OPTION_MOPERAND_CHECK:
13445 if (strcasecmp (arg, "error") == 0)
13446 operand_check = check_error;
13447 else if (strcasecmp (arg, "warning") == 0)
13448 operand_check = check_warning;
13449 else if (strcasecmp (arg, "none") == 0)
13450 operand_check = check_none;
13451 else
13452 as_fatal (_("invalid -moperand-check= option: `%s'"), arg);
13453 break;
13454
13455 case OPTION_MAVXSCALAR:
13456 if (strcasecmp (arg, "128") == 0)
13457 avxscalar = vex128;
13458 else if (strcasecmp (arg, "256") == 0)
13459 avxscalar = vex256;
13460 else
13461 as_fatal (_("invalid -mavxscalar= option: `%s'"), arg);
13462 break;
13463
13464 case OPTION_MVEXWIG:
13465 if (strcmp (arg, "0") == 0)
13466 vexwig = vexw0;
13467 else if (strcmp (arg, "1") == 0)
13468 vexwig = vexw1;
13469 else
13470 as_fatal (_("invalid -mvexwig= option: `%s'"), arg);
13471 break;
13472
13473 case OPTION_MADD_BND_PREFIX:
13474 add_bnd_prefix = 1;
13475 break;
13476
13477 case OPTION_MEVEXLIG:
13478 if (strcmp (arg, "128") == 0)
13479 evexlig = evexl128;
13480 else if (strcmp (arg, "256") == 0)
13481 evexlig = evexl256;
13482 else if (strcmp (arg, "512") == 0)
13483 evexlig = evexl512;
13484 else
13485 as_fatal (_("invalid -mevexlig= option: `%s'"), arg);
13486 break;
13487
13488 case OPTION_MEVEXRCIG:
13489 if (strcmp (arg, "rne") == 0)
13490 evexrcig = rne;
13491 else if (strcmp (arg, "rd") == 0)
13492 evexrcig = rd;
13493 else if (strcmp (arg, "ru") == 0)
13494 evexrcig = ru;
13495 else if (strcmp (arg, "rz") == 0)
13496 evexrcig = rz;
13497 else
13498 as_fatal (_("invalid -mevexrcig= option: `%s'"), arg);
13499 break;
13500
13501 case OPTION_MEVEXWIG:
13502 if (strcmp (arg, "0") == 0)
13503 evexwig = evexw0;
13504 else if (strcmp (arg, "1") == 0)
13505 evexwig = evexw1;
13506 else
13507 as_fatal (_("invalid -mevexwig= option: `%s'"), arg);
13508 break;
13509
13510 # if defined (TE_PE) || defined (TE_PEP)
13511 case OPTION_MBIG_OBJ:
13512 use_big_obj = 1;
13513 break;
13514 #endif
13515
13516 case OPTION_MOMIT_LOCK_PREFIX:
13517 if (strcasecmp (arg, "yes") == 0)
13518 omit_lock_prefix = 1;
13519 else if (strcasecmp (arg, "no") == 0)
13520 omit_lock_prefix = 0;
13521 else
13522 as_fatal (_("invalid -momit-lock-prefix= option: `%s'"), arg);
13523 break;
13524
13525 case OPTION_MFENCE_AS_LOCK_ADD:
13526 if (strcasecmp (arg, "yes") == 0)
13527 avoid_fence = 1;
13528 else if (strcasecmp (arg, "no") == 0)
13529 avoid_fence = 0;
13530 else
13531 as_fatal (_("invalid -mfence-as-lock-add= option: `%s'"), arg);
13532 break;
13533
13534 case OPTION_MLFENCE_AFTER_LOAD:
13535 if (strcasecmp (arg, "yes") == 0)
13536 lfence_after_load = 1;
13537 else if (strcasecmp (arg, "no") == 0)
13538 lfence_after_load = 0;
13539 else
13540 as_fatal (_("invalid -mlfence-after-load= option: `%s'"), arg);
13541 break;
13542
13543 case OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH:
13544 if (strcasecmp (arg, "all") == 0)
13545 {
13546 lfence_before_indirect_branch = lfence_branch_all;
13547 if (lfence_before_ret == lfence_before_ret_none)
13548 lfence_before_ret = lfence_before_ret_shl;
13549 }
13550 else if (strcasecmp (arg, "memory") == 0)
13551 lfence_before_indirect_branch = lfence_branch_memory;
13552 else if (strcasecmp (arg, "register") == 0)
13553 lfence_before_indirect_branch = lfence_branch_register;
13554 else if (strcasecmp (arg, "none") == 0)
13555 lfence_before_indirect_branch = lfence_branch_none;
13556 else
13557 as_fatal (_("invalid -mlfence-before-indirect-branch= option: `%s'"),
13558 arg);
13559 break;
13560
13561 case OPTION_MLFENCE_BEFORE_RET:
13562 if (strcasecmp (arg, "or") == 0)
13563 lfence_before_ret = lfence_before_ret_or;
13564 else if (strcasecmp (arg, "not") == 0)
13565 lfence_before_ret = lfence_before_ret_not;
13566 else if (strcasecmp (arg, "shl") == 0 || strcasecmp (arg, "yes") == 0)
13567 lfence_before_ret = lfence_before_ret_shl;
13568 else if (strcasecmp (arg, "none") == 0)
13569 lfence_before_ret = lfence_before_ret_none;
13570 else
13571 as_fatal (_("invalid -mlfence-before-ret= option: `%s'"),
13572 arg);
13573 break;
13574
13575 case OPTION_MRELAX_RELOCATIONS:
13576 if (strcasecmp (arg, "yes") == 0)
13577 generate_relax_relocations = 1;
13578 else if (strcasecmp (arg, "no") == 0)
13579 generate_relax_relocations = 0;
13580 else
13581 as_fatal (_("invalid -mrelax-relocations= option: `%s'"), arg);
13582 break;
13583
13584 case OPTION_MALIGN_BRANCH_BOUNDARY:
13585 {
13586 char *end;
13587 long int align = strtoul (arg, &end, 0);
13588 if (*end == '\0')
13589 {
13590 if (align == 0)
13591 {
13592 align_branch_power = 0;
13593 break;
13594 }
13595 else if (align >= 16)
13596 {
13597 int align_power;
13598 for (align_power = 0;
13599 (align & 1) == 0;
13600 align >>= 1, align_power++)
13601 continue;
13602 /* Limit alignment power to 31. */
13603 if (align == 1 && align_power < 32)
13604 {
13605 align_branch_power = align_power;
13606 break;
13607 }
13608 }
13609 }
13610 as_fatal (_("invalid -malign-branch-boundary= value: %s"), arg);
13611 }
13612 break;
13613
13614 case OPTION_MALIGN_BRANCH_PREFIX_SIZE:
13615 {
13616 char *end;
13617 int align = strtoul (arg, &end, 0);
13618 /* Some processors only support 5 prefixes. */
13619 if (*end == '\0' && align >= 0 && align < 6)
13620 {
13621 align_branch_prefix_size = align;
13622 break;
13623 }
13624 as_fatal (_("invalid -malign-branch-prefix-size= value: %s"),
13625 arg);
13626 }
13627 break;
13628
13629 case OPTION_MALIGN_BRANCH:
13630 align_branch = 0;
13631 saved = xstrdup (arg);
13632 type = saved;
13633 do
13634 {
13635 next = strchr (type, '+');
13636 if (next)
13637 *next++ = '\0';
13638 if (strcasecmp (type, "jcc") == 0)
13639 align_branch |= align_branch_jcc_bit;
13640 else if (strcasecmp (type, "fused") == 0)
13641 align_branch |= align_branch_fused_bit;
13642 else if (strcasecmp (type, "jmp") == 0)
13643 align_branch |= align_branch_jmp_bit;
13644 else if (strcasecmp (type, "call") == 0)
13645 align_branch |= align_branch_call_bit;
13646 else if (strcasecmp (type, "ret") == 0)
13647 align_branch |= align_branch_ret_bit;
13648 else if (strcasecmp (type, "indirect") == 0)
13649 align_branch |= align_branch_indirect_bit;
13650 else
13651 as_fatal (_("invalid -malign-branch= option: `%s'"), arg);
13652 type = next;
13653 }
13654 while (next != NULL);
13655 free (saved);
13656 break;
13657
13658 case OPTION_MBRANCHES_WITH_32B_BOUNDARIES:
13659 align_branch_power = 5;
13660 align_branch_prefix_size = 5;
13661 align_branch = (align_branch_jcc_bit
13662 | align_branch_fused_bit
13663 | align_branch_jmp_bit);
13664 break;
13665
13666 case OPTION_MAMD64:
13667 isa64 = amd64;
13668 break;
13669
13670 case OPTION_MINTEL64:
13671 isa64 = intel64;
13672 break;
13673
13674 case 'O':
13675 if (arg == NULL)
13676 {
13677 optimize = 1;
13678 /* Turn off -Os. */
13679 optimize_for_space = 0;
13680 }
13681 else if (*arg == 's')
13682 {
13683 optimize_for_space = 1;
13684 /* Turn on all encoding optimizations. */
13685 optimize = INT_MAX;
13686 }
13687 else
13688 {
13689 optimize = atoi (arg);
13690 /* Turn off -Os. */
13691 optimize_for_space = 0;
13692 }
13693 break;
13694
13695 default:
13696 return 0;
13697 }
13698 return 1;
13699 }
13700
13701 #define MESSAGE_TEMPLATE \
13702 " "
13703
13704 static char *
13705 output_message (FILE *stream, char *p, char *message, char *start,
13706 int *left_p, const char *name, int len)
13707 {
13708 int size = sizeof (MESSAGE_TEMPLATE);
13709 int left = *left_p;
13710
13711 /* Reserve 2 spaces for ", " or ",\0" */
13712 left -= len + 2;
13713
13714 /* Check if there is any room. */
13715 if (left >= 0)
13716 {
13717 if (p != start)
13718 {
13719 *p++ = ',';
13720 *p++ = ' ';
13721 }
13722 p = mempcpy (p, name, len);
13723 }
13724 else
13725 {
13726 /* Output the current message now and start a new one. */
13727 *p++ = ',';
13728 *p = '\0';
13729 fprintf (stream, "%s\n", message);
13730 p = start;
13731 left = size - (start - message) - len - 2;
13732
13733 gas_assert (left >= 0);
13734
13735 p = mempcpy (p, name, len);
13736 }
13737
13738 *left_p = left;
13739 return p;
13740 }
13741
13742 static void
13743 show_arch (FILE *stream, int ext, int check)
13744 {
13745 static char message[] = MESSAGE_TEMPLATE;
13746 char *start = message + 27;
13747 char *p;
13748 int size = sizeof (MESSAGE_TEMPLATE);
13749 int left;
13750 const char *name;
13751 int len;
13752 unsigned int j;
13753
13754 p = start;
13755 left = size - (start - message);
13756 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13757 {
13758 /* Should it be skipped? */
13759 if (cpu_arch [j].skip)
13760 continue;
13761
13762 name = cpu_arch [j].name;
13763 len = cpu_arch [j].len;
13764 if (*name == '.')
13765 {
13766 /* It is an extension. Skip if we aren't asked to show it. */
13767 if (ext)
13768 {
13769 name++;
13770 len--;
13771 }
13772 else
13773 continue;
13774 }
13775 else if (ext)
13776 {
13777 /* It is an processor. Skip if we show only extension. */
13778 continue;
13779 }
13780 else if (check && ! cpu_arch[j].flags.bitfield.cpui386)
13781 {
13782 /* It is an impossible processor - skip. */
13783 continue;
13784 }
13785
13786 p = output_message (stream, p, message, start, &left, name, len);
13787 }
13788
13789 /* Display disabled extensions. */
13790 if (ext)
13791 for (j = 0; j < ARRAY_SIZE (cpu_noarch); j++)
13792 {
13793 name = cpu_noarch [j].name;
13794 len = cpu_noarch [j].len;
13795 p = output_message (stream, p, message, start, &left, name,
13796 len);
13797 }
13798
13799 *p = '\0';
13800 fprintf (stream, "%s\n", message);
13801 }
13802
13803 void
13804 md_show_usage (FILE *stream)
13805 {
13806 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13807 fprintf (stream, _("\
13808 -Qy, -Qn ignored\n\
13809 -V print assembler version number\n\
13810 -k ignored\n"));
13811 #endif
13812 fprintf (stream, _("\
13813 -n Do not optimize code alignment\n\
13814 -q quieten some warnings\n"));
13815 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13816 fprintf (stream, _("\
13817 -s ignored\n"));
13818 #endif
13819 #ifdef BFD64
13820 # if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13821 fprintf (stream, _("\
13822 --32/--64/--x32 generate 32bit/64bit/x32 object\n"));
13823 # elif defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O)
13824 fprintf (stream, _("\
13825 --32/--64 generate 32bit/64bit object\n"));
13826 # endif
13827 #endif
13828 #ifdef SVR4_COMMENT_CHARS
13829 fprintf (stream, _("\
13830 --divide do not treat `/' as a comment character\n"));
13831 #else
13832 fprintf (stream, _("\
13833 --divide ignored\n"));
13834 #endif
13835 fprintf (stream, _("\
13836 -march=CPU[,+EXTENSION...]\n\
13837 generate code for CPU and EXTENSION, CPU is one of:\n"));
13838 show_arch (stream, 0, 1);
13839 fprintf (stream, _("\
13840 EXTENSION is combination of:\n"));
13841 show_arch (stream, 1, 0);
13842 fprintf (stream, _("\
13843 -mtune=CPU optimize for CPU, CPU is one of:\n"));
13844 show_arch (stream, 0, 0);
13845 fprintf (stream, _("\
13846 -msse2avx encode SSE instructions with VEX prefix\n"));
13847 fprintf (stream, _("\
13848 -muse-unaligned-vector-move\n\
13849 encode aligned vector move as unaligned vector move\n"));
13850 fprintf (stream, _("\
13851 -msse-check=[none|error|warning] (default: warning)\n\
13852 check SSE instructions\n"));
13853 fprintf (stream, _("\
13854 -moperand-check=[none|error|warning] (default: warning)\n\
13855 check operand combinations for validity\n"));
13856 fprintf (stream, _("\
13857 -mavxscalar=[128|256] (default: 128)\n\
13858 encode scalar AVX instructions with specific vector\n\
13859 length\n"));
13860 fprintf (stream, _("\
13861 -mvexwig=[0|1] (default: 0)\n\
13862 encode VEX instructions with specific VEX.W value\n\
13863 for VEX.W bit ignored instructions\n"));
13864 fprintf (stream, _("\
13865 -mevexlig=[128|256|512] (default: 128)\n\
13866 encode scalar EVEX instructions with specific vector\n\
13867 length\n"));
13868 fprintf (stream, _("\
13869 -mevexwig=[0|1] (default: 0)\n\
13870 encode EVEX instructions with specific EVEX.W value\n\
13871 for EVEX.W bit ignored instructions\n"));
13872 fprintf (stream, _("\
13873 -mevexrcig=[rne|rd|ru|rz] (default: rne)\n\
13874 encode EVEX instructions with specific EVEX.RC value\n\
13875 for SAE-only ignored instructions\n"));
13876 fprintf (stream, _("\
13877 -mmnemonic=[att|intel] "));
13878 if (SYSV386_COMPAT)
13879 fprintf (stream, _("(default: att)\n"));
13880 else
13881 fprintf (stream, _("(default: intel)\n"));
13882 fprintf (stream, _("\
13883 use AT&T/Intel mnemonic\n"));
13884 fprintf (stream, _("\
13885 -msyntax=[att|intel] (default: att)\n\
13886 use AT&T/Intel syntax\n"));
13887 fprintf (stream, _("\
13888 -mindex-reg support pseudo index registers\n"));
13889 fprintf (stream, _("\
13890 -mnaked-reg don't require `%%' prefix for registers\n"));
13891 fprintf (stream, _("\
13892 -madd-bnd-prefix add BND prefix for all valid branches\n"));
13893 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13894 fprintf (stream, _("\
13895 -mshared disable branch optimization for shared code\n"));
13896 fprintf (stream, _("\
13897 -mx86-used-note=[no|yes] "));
13898 if (DEFAULT_X86_USED_NOTE)
13899 fprintf (stream, _("(default: yes)\n"));
13900 else
13901 fprintf (stream, _("(default: no)\n"));
13902 fprintf (stream, _("\
13903 generate x86 used ISA and feature properties\n"));
13904 #endif
13905 #if defined (TE_PE) || defined (TE_PEP)
13906 fprintf (stream, _("\
13907 -mbig-obj generate big object files\n"));
13908 #endif
13909 fprintf (stream, _("\
13910 -momit-lock-prefix=[no|yes] (default: no)\n\
13911 strip all lock prefixes\n"));
13912 fprintf (stream, _("\
13913 -mfence-as-lock-add=[no|yes] (default: no)\n\
13914 encode lfence, mfence and sfence as\n\
13915 lock addl $0x0, (%%{re}sp)\n"));
13916 fprintf (stream, _("\
13917 -mrelax-relocations=[no|yes] "));
13918 if (DEFAULT_GENERATE_X86_RELAX_RELOCATIONS)
13919 fprintf (stream, _("(default: yes)\n"));
13920 else
13921 fprintf (stream, _("(default: no)\n"));
13922 fprintf (stream, _("\
13923 generate relax relocations\n"));
13924 fprintf (stream, _("\
13925 -malign-branch-boundary=NUM (default: 0)\n\
13926 align branches within NUM byte boundary\n"));
13927 fprintf (stream, _("\
13928 -malign-branch=TYPE[+TYPE...] (default: jcc+fused+jmp)\n\
13929 TYPE is combination of jcc, fused, jmp, call, ret,\n\
13930 indirect\n\
13931 specify types of branches to align\n"));
13932 fprintf (stream, _("\
13933 -malign-branch-prefix-size=NUM (default: 5)\n\
13934 align branches with NUM prefixes per instruction\n"));
13935 fprintf (stream, _("\
13936 -mbranches-within-32B-boundaries\n\
13937 align branches within 32 byte boundary\n"));
13938 fprintf (stream, _("\
13939 -mlfence-after-load=[no|yes] (default: no)\n\
13940 generate lfence after load\n"));
13941 fprintf (stream, _("\
13942 -mlfence-before-indirect-branch=[none|all|register|memory] (default: none)\n\
13943 generate lfence before indirect near branch\n"));
13944 fprintf (stream, _("\
13945 -mlfence-before-ret=[none|or|not|shl|yes] (default: none)\n\
13946 generate lfence before ret\n"));
13947 fprintf (stream, _("\
13948 -mamd64 accept only AMD64 ISA [default]\n"));
13949 fprintf (stream, _("\
13950 -mintel64 accept only Intel64 ISA\n"));
13951 }
13952
13953 #if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
13954 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
13955 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
13956
13957 /* Pick the target format to use. */
13958
13959 const char *
13960 i386_target_format (void)
13961 {
13962 if (startswith (default_arch, "x86_64"))
13963 {
13964 update_code_flag (CODE_64BIT, 1);
13965 if (default_arch[6] == '\0')
13966 x86_elf_abi = X86_64_ABI;
13967 else
13968 x86_elf_abi = X86_64_X32_ABI;
13969 }
13970 else if (!strcmp (default_arch, "i386"))
13971 update_code_flag (CODE_32BIT, 1);
13972 else if (!strcmp (default_arch, "iamcu"))
13973 {
13974 update_code_flag (CODE_32BIT, 1);
13975 if (cpu_arch_isa == PROCESSOR_UNKNOWN)
13976 {
13977 static const i386_cpu_flags iamcu_flags = CPU_IAMCU_FLAGS;
13978 cpu_arch_name = "iamcu";
13979 cpu_sub_arch_name = NULL;
13980 cpu_arch_flags = iamcu_flags;
13981 cpu_arch_isa = PROCESSOR_IAMCU;
13982 cpu_arch_isa_flags = iamcu_flags;
13983 if (!cpu_arch_tune_set)
13984 {
13985 cpu_arch_tune = cpu_arch_isa;
13986 cpu_arch_tune_flags = cpu_arch_isa_flags;
13987 }
13988 }
13989 else if (cpu_arch_isa != PROCESSOR_IAMCU)
13990 as_fatal (_("Intel MCU doesn't support `%s' architecture"),
13991 cpu_arch_name);
13992 }
13993 else
13994 as_fatal (_("unknown architecture"));
13995
13996 if (cpu_flags_all_zero (&cpu_arch_isa_flags))
13997 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].flags;
13998 if (cpu_flags_all_zero (&cpu_arch_tune_flags))
13999 cpu_arch_tune_flags = cpu_arch[flag_code == CODE_64BIT].flags;
14000
14001 switch (OUTPUT_FLAVOR)
14002 {
14003 #if defined (OBJ_MAYBE_AOUT) || defined (OBJ_AOUT)
14004 case bfd_target_aout_flavour:
14005 return AOUT_TARGET_FORMAT;
14006 #endif
14007 #if defined (OBJ_MAYBE_COFF) || defined (OBJ_COFF)
14008 # if defined (TE_PE) || defined (TE_PEP)
14009 case bfd_target_coff_flavour:
14010 if (flag_code == CODE_64BIT)
14011 {
14012 object_64bit = 1;
14013 return use_big_obj ? "pe-bigobj-x86-64" : "pe-x86-64";
14014 }
14015 return use_big_obj ? "pe-bigobj-i386" : "pe-i386";
14016 # elif defined (TE_GO32)
14017 case bfd_target_coff_flavour:
14018 return "coff-go32";
14019 # else
14020 case bfd_target_coff_flavour:
14021 return "coff-i386";
14022 # endif
14023 #endif
14024 #if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
14025 case bfd_target_elf_flavour:
14026 {
14027 const char *format;
14028
14029 switch (x86_elf_abi)
14030 {
14031 default:
14032 format = ELF_TARGET_FORMAT;
14033 #ifndef TE_SOLARIS
14034 tls_get_addr = "___tls_get_addr";
14035 #endif
14036 break;
14037 case X86_64_ABI:
14038 use_rela_relocations = 1;
14039 object_64bit = 1;
14040 #ifndef TE_SOLARIS
14041 tls_get_addr = "__tls_get_addr";
14042 #endif
14043 format = ELF_TARGET_FORMAT64;
14044 break;
14045 case X86_64_X32_ABI:
14046 use_rela_relocations = 1;
14047 object_64bit = 1;
14048 #ifndef TE_SOLARIS
14049 tls_get_addr = "__tls_get_addr";
14050 #endif
14051 disallow_64bit_reloc = 1;
14052 format = ELF_TARGET_FORMAT32;
14053 break;
14054 }
14055 if (cpu_arch_isa == PROCESSOR_IAMCU)
14056 {
14057 if (x86_elf_abi != I386_ABI)
14058 as_fatal (_("Intel MCU is 32bit only"));
14059 return ELF_TARGET_IAMCU_FORMAT;
14060 }
14061 else
14062 return format;
14063 }
14064 #endif
14065 #if defined (OBJ_MACH_O)
14066 case bfd_target_mach_o_flavour:
14067 if (flag_code == CODE_64BIT)
14068 {
14069 use_rela_relocations = 1;
14070 object_64bit = 1;
14071 return "mach-o-x86-64";
14072 }
14073 else
14074 return "mach-o-i386";
14075 #endif
14076 default:
14077 abort ();
14078 return NULL;
14079 }
14080 }
14081
14082 #endif /* OBJ_MAYBE_ more than one */
14083 \f
14084 symbolS *
14085 md_undefined_symbol (char *name)
14086 {
14087 if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
14088 && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
14089 && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
14090 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
14091 {
14092 if (!GOT_symbol)
14093 {
14094 if (symbol_find (name))
14095 as_bad (_("GOT already in symbol table"));
14096 GOT_symbol = symbol_new (name, undefined_section,
14097 &zero_address_frag, 0);
14098 };
14099 return GOT_symbol;
14100 }
14101 return 0;
14102 }
14103
14104 /* Round up a section size to the appropriate boundary. */
14105
14106 valueT
14107 md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
14108 {
14109 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
14110 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
14111 {
14112 /* For a.out, force the section size to be aligned. If we don't do
14113 this, BFD will align it for us, but it will not write out the
14114 final bytes of the section. This may be a bug in BFD, but it is
14115 easier to fix it here since that is how the other a.out targets
14116 work. */
14117 int align;
14118
14119 align = bfd_section_alignment (segment);
14120 size = ((size + (1 << align) - 1) & (-((valueT) 1 << align)));
14121 }
14122 #endif
14123
14124 return size;
14125 }
14126
14127 /* On the i386, PC-relative offsets are relative to the start of the
14128 next instruction. That is, the address of the offset, plus its
14129 size, since the offset is always the last part of the insn. */
14130
14131 long
14132 md_pcrel_from (fixS *fixP)
14133 {
14134 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
14135 }
14136
14137 #ifndef I386COFF
14138
14139 static void
14140 s_bss (int ignore ATTRIBUTE_UNUSED)
14141 {
14142 int temp;
14143
14144 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14145 if (IS_ELF)
14146 obj_elf_section_change_hook ();
14147 #endif
14148 temp = get_absolute_expression ();
14149 subseg_set (bss_section, (subsegT) temp);
14150 demand_empty_rest_of_line ();
14151 }
14152
14153 #endif
14154
14155 /* Remember constant directive. */
14156
14157 void
14158 i386_cons_align (int ignore ATTRIBUTE_UNUSED)
14159 {
14160 if (last_insn.kind != last_insn_directive
14161 && (bfd_section_flags (now_seg) & SEC_CODE))
14162 {
14163 last_insn.seg = now_seg;
14164 last_insn.kind = last_insn_directive;
14165 last_insn.name = "constant directive";
14166 last_insn.file = as_where (&last_insn.line);
14167 if (lfence_before_ret != lfence_before_ret_none)
14168 {
14169 if (lfence_before_indirect_branch != lfence_branch_none)
14170 as_warn (_("constant directive skips -mlfence-before-ret "
14171 "and -mlfence-before-indirect-branch"));
14172 else
14173 as_warn (_("constant directive skips -mlfence-before-ret"));
14174 }
14175 else if (lfence_before_indirect_branch != lfence_branch_none)
14176 as_warn (_("constant directive skips -mlfence-before-indirect-branch"));
14177 }
14178 }
14179
14180 int
14181 i386_validate_fix (fixS *fixp)
14182 {
14183 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14184 if (fixp->fx_r_type == BFD_RELOC_SIZE32
14185 || fixp->fx_r_type == BFD_RELOC_SIZE64)
14186 return IS_ELF && fixp->fx_addsy
14187 && (!S_IS_DEFINED (fixp->fx_addsy)
14188 || S_IS_EXTERNAL (fixp->fx_addsy));
14189 #endif
14190
14191 if (fixp->fx_subsy)
14192 {
14193 if (fixp->fx_subsy == GOT_symbol)
14194 {
14195 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
14196 {
14197 if (!object_64bit)
14198 abort ();
14199 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14200 if (fixp->fx_tcbit2)
14201 fixp->fx_r_type = (fixp->fx_tcbit
14202 ? BFD_RELOC_X86_64_REX_GOTPCRELX
14203 : BFD_RELOC_X86_64_GOTPCRELX);
14204 else
14205 #endif
14206 fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
14207 }
14208 else
14209 {
14210 if (!object_64bit)
14211 fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
14212 else
14213 fixp->fx_r_type = BFD_RELOC_X86_64_GOTOFF64;
14214 }
14215 fixp->fx_subsy = 0;
14216 }
14217 }
14218 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14219 else
14220 {
14221 /* NB: Commit 292676c1 resolved PLT32 reloc aganst local symbol
14222 to section. Since PLT32 relocation must be against symbols,
14223 turn such PLT32 relocation into PC32 relocation. */
14224 if (fixp->fx_addsy
14225 && (fixp->fx_r_type == BFD_RELOC_386_PLT32
14226 || fixp->fx_r_type == BFD_RELOC_X86_64_PLT32)
14227 && symbol_section_p (fixp->fx_addsy))
14228 fixp->fx_r_type = BFD_RELOC_32_PCREL;
14229 if (!object_64bit)
14230 {
14231 if (fixp->fx_r_type == BFD_RELOC_386_GOT32
14232 && fixp->fx_tcbit2)
14233 fixp->fx_r_type = BFD_RELOC_386_GOT32X;
14234 }
14235 }
14236 #endif
14237
14238 return 1;
14239 }
14240
14241 arelent *
14242 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
14243 {
14244 arelent *rel;
14245 bfd_reloc_code_real_type code;
14246
14247 switch (fixp->fx_r_type)
14248 {
14249 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14250 symbolS *sym;
14251
14252 case BFD_RELOC_SIZE32:
14253 case BFD_RELOC_SIZE64:
14254 if (fixp->fx_addsy
14255 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))
14256 && (!fixp->fx_subsy
14257 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))))
14258 sym = fixp->fx_addsy;
14259 else if (fixp->fx_subsy
14260 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))
14261 && (!fixp->fx_addsy
14262 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))))
14263 sym = fixp->fx_subsy;
14264 else
14265 sym = NULL;
14266 if (IS_ELF && sym && S_IS_DEFINED (sym) && !S_IS_EXTERNAL (sym))
14267 {
14268 /* Resolve size relocation against local symbol to size of
14269 the symbol plus addend. */
14270 valueT value = S_GET_SIZE (sym);
14271
14272 if (symbol_get_bfdsym (sym)->flags & BSF_SECTION_SYM)
14273 value = bfd_section_size (S_GET_SEGMENT (sym));
14274 if (sym == fixp->fx_subsy)
14275 {
14276 value = -value;
14277 if (fixp->fx_addsy)
14278 value += S_GET_VALUE (fixp->fx_addsy);
14279 }
14280 else if (fixp->fx_subsy)
14281 value -= S_GET_VALUE (fixp->fx_subsy);
14282 value += fixp->fx_offset;
14283 if (fixp->fx_r_type == BFD_RELOC_SIZE32
14284 && object_64bit
14285 && !fits_in_unsigned_long (value))
14286 as_bad_where (fixp->fx_file, fixp->fx_line,
14287 _("symbol size computation overflow"));
14288 fixp->fx_addsy = NULL;
14289 fixp->fx_subsy = NULL;
14290 md_apply_fix (fixp, (valueT *) &value, NULL);
14291 return NULL;
14292 }
14293 if (!fixp->fx_addsy || fixp->fx_subsy)
14294 {
14295 as_bad_where (fixp->fx_file, fixp->fx_line,
14296 "unsupported expression involving @size");
14297 return NULL;
14298 }
14299 #endif
14300 /* Fall through. */
14301
14302 case BFD_RELOC_X86_64_PLT32:
14303 case BFD_RELOC_X86_64_GOT32:
14304 case BFD_RELOC_X86_64_GOTPCREL:
14305 case BFD_RELOC_X86_64_GOTPCRELX:
14306 case BFD_RELOC_X86_64_REX_GOTPCRELX:
14307 case BFD_RELOC_386_PLT32:
14308 case BFD_RELOC_386_GOT32:
14309 case BFD_RELOC_386_GOT32X:
14310 case BFD_RELOC_386_GOTOFF:
14311 case BFD_RELOC_386_GOTPC:
14312 case BFD_RELOC_386_TLS_GD:
14313 case BFD_RELOC_386_TLS_LDM:
14314 case BFD_RELOC_386_TLS_LDO_32:
14315 case BFD_RELOC_386_TLS_IE_32:
14316 case BFD_RELOC_386_TLS_IE:
14317 case BFD_RELOC_386_TLS_GOTIE:
14318 case BFD_RELOC_386_TLS_LE_32:
14319 case BFD_RELOC_386_TLS_LE:
14320 case BFD_RELOC_386_TLS_GOTDESC:
14321 case BFD_RELOC_386_TLS_DESC_CALL:
14322 case BFD_RELOC_X86_64_TLSGD:
14323 case BFD_RELOC_X86_64_TLSLD:
14324 case BFD_RELOC_X86_64_DTPOFF32:
14325 case BFD_RELOC_X86_64_DTPOFF64:
14326 case BFD_RELOC_X86_64_GOTTPOFF:
14327 case BFD_RELOC_X86_64_TPOFF32:
14328 case BFD_RELOC_X86_64_TPOFF64:
14329 case BFD_RELOC_X86_64_GOTOFF64:
14330 case BFD_RELOC_X86_64_GOTPC32:
14331 case BFD_RELOC_X86_64_GOT64:
14332 case BFD_RELOC_X86_64_GOTPCREL64:
14333 case BFD_RELOC_X86_64_GOTPC64:
14334 case BFD_RELOC_X86_64_GOTPLT64:
14335 case BFD_RELOC_X86_64_PLTOFF64:
14336 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14337 case BFD_RELOC_X86_64_TLSDESC_CALL:
14338 case BFD_RELOC_RVA:
14339 case BFD_RELOC_VTABLE_ENTRY:
14340 case BFD_RELOC_VTABLE_INHERIT:
14341 #ifdef TE_PE
14342 case BFD_RELOC_32_SECREL:
14343 #endif
14344 code = fixp->fx_r_type;
14345 break;
14346 case BFD_RELOC_X86_64_32S:
14347 if (!fixp->fx_pcrel)
14348 {
14349 /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32. */
14350 code = fixp->fx_r_type;
14351 break;
14352 }
14353 /* Fall through. */
14354 default:
14355 if (fixp->fx_pcrel)
14356 {
14357 switch (fixp->fx_size)
14358 {
14359 default:
14360 as_bad_where (fixp->fx_file, fixp->fx_line,
14361 _("can not do %d byte pc-relative relocation"),
14362 fixp->fx_size);
14363 code = BFD_RELOC_32_PCREL;
14364 break;
14365 case 1: code = BFD_RELOC_8_PCREL; break;
14366 case 2: code = BFD_RELOC_16_PCREL; break;
14367 case 4: code = BFD_RELOC_32_PCREL; break;
14368 #ifdef BFD64
14369 case 8: code = BFD_RELOC_64_PCREL; break;
14370 #endif
14371 }
14372 }
14373 else
14374 {
14375 switch (fixp->fx_size)
14376 {
14377 default:
14378 as_bad_where (fixp->fx_file, fixp->fx_line,
14379 _("can not do %d byte relocation"),
14380 fixp->fx_size);
14381 code = BFD_RELOC_32;
14382 break;
14383 case 1: code = BFD_RELOC_8; break;
14384 case 2: code = BFD_RELOC_16; break;
14385 case 4: code = BFD_RELOC_32; break;
14386 #ifdef BFD64
14387 case 8: code = BFD_RELOC_64; break;
14388 #endif
14389 }
14390 }
14391 break;
14392 }
14393
14394 if ((code == BFD_RELOC_32
14395 || code == BFD_RELOC_32_PCREL
14396 || code == BFD_RELOC_X86_64_32S)
14397 && GOT_symbol
14398 && fixp->fx_addsy == GOT_symbol)
14399 {
14400 if (!object_64bit)
14401 code = BFD_RELOC_386_GOTPC;
14402 else
14403 code = BFD_RELOC_X86_64_GOTPC32;
14404 }
14405 if ((code == BFD_RELOC_64 || code == BFD_RELOC_64_PCREL)
14406 && GOT_symbol
14407 && fixp->fx_addsy == GOT_symbol)
14408 {
14409 code = BFD_RELOC_X86_64_GOTPC64;
14410 }
14411
14412 rel = XNEW (arelent);
14413 rel->sym_ptr_ptr = XNEW (asymbol *);
14414 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
14415
14416 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
14417
14418 if (!use_rela_relocations)
14419 {
14420 /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
14421 vtable entry to be used in the relocation's section offset. */
14422 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
14423 rel->address = fixp->fx_offset;
14424 #if defined (OBJ_COFF) && defined (TE_PE)
14425 else if (fixp->fx_addsy && S_IS_WEAK (fixp->fx_addsy))
14426 rel->addend = fixp->fx_addnumber - (S_GET_VALUE (fixp->fx_addsy) * 2);
14427 else
14428 #endif
14429 rel->addend = 0;
14430 }
14431 /* Use the rela in 64bit mode. */
14432 else
14433 {
14434 if (disallow_64bit_reloc)
14435 switch (code)
14436 {
14437 case BFD_RELOC_X86_64_DTPOFF64:
14438 case BFD_RELOC_X86_64_TPOFF64:
14439 case BFD_RELOC_64_PCREL:
14440 case BFD_RELOC_X86_64_GOTOFF64:
14441 case BFD_RELOC_X86_64_GOT64:
14442 case BFD_RELOC_X86_64_GOTPCREL64:
14443 case BFD_RELOC_X86_64_GOTPC64:
14444 case BFD_RELOC_X86_64_GOTPLT64:
14445 case BFD_RELOC_X86_64_PLTOFF64:
14446 as_bad_where (fixp->fx_file, fixp->fx_line,
14447 _("cannot represent relocation type %s in x32 mode"),
14448 bfd_get_reloc_code_name (code));
14449 break;
14450 default:
14451 break;
14452 }
14453
14454 if (!fixp->fx_pcrel)
14455 rel->addend = fixp->fx_offset;
14456 else
14457 switch (code)
14458 {
14459 case BFD_RELOC_X86_64_PLT32:
14460 case BFD_RELOC_X86_64_GOT32:
14461 case BFD_RELOC_X86_64_GOTPCREL:
14462 case BFD_RELOC_X86_64_GOTPCRELX:
14463 case BFD_RELOC_X86_64_REX_GOTPCRELX:
14464 case BFD_RELOC_X86_64_TLSGD:
14465 case BFD_RELOC_X86_64_TLSLD:
14466 case BFD_RELOC_X86_64_GOTTPOFF:
14467 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14468 case BFD_RELOC_X86_64_TLSDESC_CALL:
14469 rel->addend = fixp->fx_offset - fixp->fx_size;
14470 break;
14471 default:
14472 rel->addend = (section->vma
14473 - fixp->fx_size
14474 + fixp->fx_addnumber
14475 + md_pcrel_from (fixp));
14476 break;
14477 }
14478 }
14479
14480 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
14481 if (rel->howto == NULL)
14482 {
14483 as_bad_where (fixp->fx_file, fixp->fx_line,
14484 _("cannot represent relocation type %s"),
14485 bfd_get_reloc_code_name (code));
14486 /* Set howto to a garbage value so that we can keep going. */
14487 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
14488 gas_assert (rel->howto != NULL);
14489 }
14490
14491 return rel;
14492 }
14493
14494 #include "tc-i386-intel.c"
14495
14496 void
14497 tc_x86_parse_to_dw2regnum (expressionS *exp)
14498 {
14499 int saved_naked_reg;
14500 char saved_register_dot;
14501
14502 saved_naked_reg = allow_naked_reg;
14503 allow_naked_reg = 1;
14504 saved_register_dot = register_chars['.'];
14505 register_chars['.'] = '.';
14506 allow_pseudo_reg = 1;
14507 expression_and_evaluate (exp);
14508 allow_pseudo_reg = 0;
14509 register_chars['.'] = saved_register_dot;
14510 allow_naked_reg = saved_naked_reg;
14511
14512 if (exp->X_op == O_register && exp->X_add_number >= 0)
14513 {
14514 if ((addressT) exp->X_add_number < i386_regtab_size)
14515 {
14516 exp->X_op = O_constant;
14517 exp->X_add_number = i386_regtab[exp->X_add_number]
14518 .dw2_regnum[flag_code >> 1];
14519 }
14520 else
14521 exp->X_op = O_illegal;
14522 }
14523 }
14524
14525 void
14526 tc_x86_frame_initial_instructions (void)
14527 {
14528 static unsigned int sp_regno[2];
14529
14530 if (!sp_regno[flag_code >> 1])
14531 {
14532 char *saved_input = input_line_pointer;
14533 char sp[][4] = {"esp", "rsp"};
14534 expressionS exp;
14535
14536 input_line_pointer = sp[flag_code >> 1];
14537 tc_x86_parse_to_dw2regnum (&exp);
14538 gas_assert (exp.X_op == O_constant);
14539 sp_regno[flag_code >> 1] = exp.X_add_number;
14540 input_line_pointer = saved_input;
14541 }
14542
14543 cfi_add_CFA_def_cfa (sp_regno[flag_code >> 1], -x86_cie_data_alignment);
14544 cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
14545 }
14546
14547 int
14548 x86_dwarf2_addr_size (void)
14549 {
14550 #if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
14551 if (x86_elf_abi == X86_64_X32_ABI)
14552 return 4;
14553 #endif
14554 return bfd_arch_bits_per_address (stdoutput) / 8;
14555 }
14556
14557 int
14558 i386_elf_section_type (const char *str, size_t len)
14559 {
14560 if (flag_code == CODE_64BIT
14561 && len == sizeof ("unwind") - 1
14562 && startswith (str, "unwind"))
14563 return SHT_X86_64_UNWIND;
14564
14565 return -1;
14566 }
14567
14568 #ifdef TE_SOLARIS
14569 void
14570 i386_solaris_fix_up_eh_frame (segT sec)
14571 {
14572 if (flag_code == CODE_64BIT)
14573 elf_section_type (sec) = SHT_X86_64_UNWIND;
14574 }
14575 #endif
14576
14577 #ifdef TE_PE
14578 void
14579 tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
14580 {
14581 expressionS exp;
14582
14583 exp.X_op = O_secrel;
14584 exp.X_add_symbol = symbol;
14585 exp.X_add_number = 0;
14586 emit_expr (&exp, size);
14587 }
14588 #endif
14589
14590 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14591 /* For ELF on x86-64, add support for SHF_X86_64_LARGE. */
14592
14593 bfd_vma
14594 x86_64_section_letter (int letter, const char **ptr_msg)
14595 {
14596 if (flag_code == CODE_64BIT)
14597 {
14598 if (letter == 'l')
14599 return SHF_X86_64_LARGE;
14600
14601 *ptr_msg = _("bad .section directive: want a,l,w,x,M,S,G,T in string");
14602 }
14603 else
14604 *ptr_msg = _("bad .section directive: want a,w,x,M,S,G,T in string");
14605 return -1;
14606 }
14607
14608 bfd_vma
14609 x86_64_section_word (char *str, size_t len)
14610 {
14611 if (len == 5 && flag_code == CODE_64BIT && startswith (str, "large"))
14612 return SHF_X86_64_LARGE;
14613
14614 return -1;
14615 }
14616
14617 static void
14618 handle_large_common (int small ATTRIBUTE_UNUSED)
14619 {
14620 if (flag_code != CODE_64BIT)
14621 {
14622 s_comm_internal (0, elf_common_parse);
14623 as_warn (_(".largecomm supported only in 64bit mode, producing .comm"));
14624 }
14625 else
14626 {
14627 static segT lbss_section;
14628 asection *saved_com_section_ptr = elf_com_section_ptr;
14629 asection *saved_bss_section = bss_section;
14630
14631 if (lbss_section == NULL)
14632 {
14633 flagword applicable;
14634 segT seg = now_seg;
14635 subsegT subseg = now_subseg;
14636
14637 /* The .lbss section is for local .largecomm symbols. */
14638 lbss_section = subseg_new (".lbss", 0);
14639 applicable = bfd_applicable_section_flags (stdoutput);
14640 bfd_set_section_flags (lbss_section, applicable & SEC_ALLOC);
14641 seg_info (lbss_section)->bss = 1;
14642
14643 subseg_set (seg, subseg);
14644 }
14645
14646 elf_com_section_ptr = &_bfd_elf_large_com_section;
14647 bss_section = lbss_section;
14648
14649 s_comm_internal (0, elf_common_parse);
14650
14651 elf_com_section_ptr = saved_com_section_ptr;
14652 bss_section = saved_bss_section;
14653 }
14654 }
14655 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */