]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gas/config/tc-i386.c
Update year range in copyright notice of binutils files
[thirdparty/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 ("l1om"), PROCESSOR_L1OM,
980 CPU_L1OM_FLAGS, 0 },
981 { STRING_COMMA_LEN ("k1om"), PROCESSOR_K1OM,
982 CPU_K1OM_FLAGS, 0 },
983 { STRING_COMMA_LEN ("iamcu"), PROCESSOR_IAMCU,
984 CPU_IAMCU_FLAGS, 0 },
985 { STRING_COMMA_LEN ("k6"), PROCESSOR_K6,
986 CPU_K6_FLAGS, 0 },
987 { STRING_COMMA_LEN ("k6_2"), PROCESSOR_K6,
988 CPU_K6_2_FLAGS, 0 },
989 { STRING_COMMA_LEN ("athlon"), PROCESSOR_ATHLON,
990 CPU_ATHLON_FLAGS, 0 },
991 { STRING_COMMA_LEN ("sledgehammer"), PROCESSOR_K8,
992 CPU_K8_FLAGS, 1 },
993 { STRING_COMMA_LEN ("opteron"), PROCESSOR_K8,
994 CPU_K8_FLAGS, 0 },
995 { STRING_COMMA_LEN ("k8"), PROCESSOR_K8,
996 CPU_K8_FLAGS, 0 },
997 { STRING_COMMA_LEN ("amdfam10"), PROCESSOR_AMDFAM10,
998 CPU_AMDFAM10_FLAGS, 0 },
999 { STRING_COMMA_LEN ("bdver1"), PROCESSOR_BD,
1000 CPU_BDVER1_FLAGS, 0 },
1001 { STRING_COMMA_LEN ("bdver2"), PROCESSOR_BD,
1002 CPU_BDVER2_FLAGS, 0 },
1003 { STRING_COMMA_LEN ("bdver3"), PROCESSOR_BD,
1004 CPU_BDVER3_FLAGS, 0 },
1005 { STRING_COMMA_LEN ("bdver4"), PROCESSOR_BD,
1006 CPU_BDVER4_FLAGS, 0 },
1007 { STRING_COMMA_LEN ("znver1"), PROCESSOR_ZNVER,
1008 CPU_ZNVER1_FLAGS, 0 },
1009 { STRING_COMMA_LEN ("znver2"), PROCESSOR_ZNVER,
1010 CPU_ZNVER2_FLAGS, 0 },
1011 { STRING_COMMA_LEN ("znver3"), PROCESSOR_ZNVER,
1012 CPU_ZNVER3_FLAGS, 0 },
1013 { STRING_COMMA_LEN ("btver1"), PROCESSOR_BT,
1014 CPU_BTVER1_FLAGS, 0 },
1015 { STRING_COMMA_LEN ("btver2"), PROCESSOR_BT,
1016 CPU_BTVER2_FLAGS, 0 },
1017 { STRING_COMMA_LEN (".8087"), PROCESSOR_UNKNOWN,
1018 CPU_8087_FLAGS, 0 },
1019 { STRING_COMMA_LEN (".287"), PROCESSOR_UNKNOWN,
1020 CPU_287_FLAGS, 0 },
1021 { STRING_COMMA_LEN (".387"), PROCESSOR_UNKNOWN,
1022 CPU_387_FLAGS, 0 },
1023 { STRING_COMMA_LEN (".687"), PROCESSOR_UNKNOWN,
1024 CPU_687_FLAGS, 0 },
1025 { STRING_COMMA_LEN (".cmov"), PROCESSOR_UNKNOWN,
1026 CPU_CMOV_FLAGS, 0 },
1027 { STRING_COMMA_LEN (".fxsr"), PROCESSOR_UNKNOWN,
1028 CPU_FXSR_FLAGS, 0 },
1029 { STRING_COMMA_LEN (".mmx"), PROCESSOR_UNKNOWN,
1030 CPU_MMX_FLAGS, 0 },
1031 { STRING_COMMA_LEN (".sse"), PROCESSOR_UNKNOWN,
1032 CPU_SSE_FLAGS, 0 },
1033 { STRING_COMMA_LEN (".sse2"), PROCESSOR_UNKNOWN,
1034 CPU_SSE2_FLAGS, 0 },
1035 { STRING_COMMA_LEN (".sse3"), PROCESSOR_UNKNOWN,
1036 CPU_SSE3_FLAGS, 0 },
1037 { STRING_COMMA_LEN (".sse4a"), PROCESSOR_UNKNOWN,
1038 CPU_SSE4A_FLAGS, 0 },
1039 { STRING_COMMA_LEN (".ssse3"), PROCESSOR_UNKNOWN,
1040 CPU_SSSE3_FLAGS, 0 },
1041 { STRING_COMMA_LEN (".sse4.1"), PROCESSOR_UNKNOWN,
1042 CPU_SSE4_1_FLAGS, 0 },
1043 { STRING_COMMA_LEN (".sse4.2"), PROCESSOR_UNKNOWN,
1044 CPU_SSE4_2_FLAGS, 0 },
1045 { STRING_COMMA_LEN (".sse4"), PROCESSOR_UNKNOWN,
1046 CPU_SSE4_2_FLAGS, 0 },
1047 { STRING_COMMA_LEN (".avx"), PROCESSOR_UNKNOWN,
1048 CPU_AVX_FLAGS, 0 },
1049 { STRING_COMMA_LEN (".avx2"), PROCESSOR_UNKNOWN,
1050 CPU_AVX2_FLAGS, 0 },
1051 { STRING_COMMA_LEN (".avx512f"), PROCESSOR_UNKNOWN,
1052 CPU_AVX512F_FLAGS, 0 },
1053 { STRING_COMMA_LEN (".avx512cd"), PROCESSOR_UNKNOWN,
1054 CPU_AVX512CD_FLAGS, 0 },
1055 { STRING_COMMA_LEN (".avx512er"), PROCESSOR_UNKNOWN,
1056 CPU_AVX512ER_FLAGS, 0 },
1057 { STRING_COMMA_LEN (".avx512pf"), PROCESSOR_UNKNOWN,
1058 CPU_AVX512PF_FLAGS, 0 },
1059 { STRING_COMMA_LEN (".avx512dq"), PROCESSOR_UNKNOWN,
1060 CPU_AVX512DQ_FLAGS, 0 },
1061 { STRING_COMMA_LEN (".avx512bw"), PROCESSOR_UNKNOWN,
1062 CPU_AVX512BW_FLAGS, 0 },
1063 { STRING_COMMA_LEN (".avx512vl"), PROCESSOR_UNKNOWN,
1064 CPU_AVX512VL_FLAGS, 0 },
1065 { STRING_COMMA_LEN (".vmx"), PROCESSOR_UNKNOWN,
1066 CPU_VMX_FLAGS, 0 },
1067 { STRING_COMMA_LEN (".vmfunc"), PROCESSOR_UNKNOWN,
1068 CPU_VMFUNC_FLAGS, 0 },
1069 { STRING_COMMA_LEN (".smx"), PROCESSOR_UNKNOWN,
1070 CPU_SMX_FLAGS, 0 },
1071 { STRING_COMMA_LEN (".xsave"), PROCESSOR_UNKNOWN,
1072 CPU_XSAVE_FLAGS, 0 },
1073 { STRING_COMMA_LEN (".xsaveopt"), PROCESSOR_UNKNOWN,
1074 CPU_XSAVEOPT_FLAGS, 0 },
1075 { STRING_COMMA_LEN (".xsavec"), PROCESSOR_UNKNOWN,
1076 CPU_XSAVEC_FLAGS, 0 },
1077 { STRING_COMMA_LEN (".xsaves"), PROCESSOR_UNKNOWN,
1078 CPU_XSAVES_FLAGS, 0 },
1079 { STRING_COMMA_LEN (".aes"), PROCESSOR_UNKNOWN,
1080 CPU_AES_FLAGS, 0 },
1081 { STRING_COMMA_LEN (".pclmul"), PROCESSOR_UNKNOWN,
1082 CPU_PCLMUL_FLAGS, 0 },
1083 { STRING_COMMA_LEN (".clmul"), PROCESSOR_UNKNOWN,
1084 CPU_PCLMUL_FLAGS, 1 },
1085 { STRING_COMMA_LEN (".fsgsbase"), PROCESSOR_UNKNOWN,
1086 CPU_FSGSBASE_FLAGS, 0 },
1087 { STRING_COMMA_LEN (".rdrnd"), PROCESSOR_UNKNOWN,
1088 CPU_RDRND_FLAGS, 0 },
1089 { STRING_COMMA_LEN (".f16c"), PROCESSOR_UNKNOWN,
1090 CPU_F16C_FLAGS, 0 },
1091 { STRING_COMMA_LEN (".bmi2"), PROCESSOR_UNKNOWN,
1092 CPU_BMI2_FLAGS, 0 },
1093 { STRING_COMMA_LEN (".fma"), PROCESSOR_UNKNOWN,
1094 CPU_FMA_FLAGS, 0 },
1095 { STRING_COMMA_LEN (".fma4"), PROCESSOR_UNKNOWN,
1096 CPU_FMA4_FLAGS, 0 },
1097 { STRING_COMMA_LEN (".xop"), PROCESSOR_UNKNOWN,
1098 CPU_XOP_FLAGS, 0 },
1099 { STRING_COMMA_LEN (".lwp"), PROCESSOR_UNKNOWN,
1100 CPU_LWP_FLAGS, 0 },
1101 { STRING_COMMA_LEN (".movbe"), PROCESSOR_UNKNOWN,
1102 CPU_MOVBE_FLAGS, 0 },
1103 { STRING_COMMA_LEN (".cx16"), PROCESSOR_UNKNOWN,
1104 CPU_CX16_FLAGS, 0 },
1105 { STRING_COMMA_LEN (".ept"), PROCESSOR_UNKNOWN,
1106 CPU_EPT_FLAGS, 0 },
1107 { STRING_COMMA_LEN (".lzcnt"), PROCESSOR_UNKNOWN,
1108 CPU_LZCNT_FLAGS, 0 },
1109 { STRING_COMMA_LEN (".popcnt"), PROCESSOR_UNKNOWN,
1110 CPU_POPCNT_FLAGS, 0 },
1111 { STRING_COMMA_LEN (".hle"), PROCESSOR_UNKNOWN,
1112 CPU_HLE_FLAGS, 0 },
1113 { STRING_COMMA_LEN (".rtm"), PROCESSOR_UNKNOWN,
1114 CPU_RTM_FLAGS, 0 },
1115 { STRING_COMMA_LEN (".invpcid"), PROCESSOR_UNKNOWN,
1116 CPU_INVPCID_FLAGS, 0 },
1117 { STRING_COMMA_LEN (".clflush"), PROCESSOR_UNKNOWN,
1118 CPU_CLFLUSH_FLAGS, 0 },
1119 { STRING_COMMA_LEN (".nop"), PROCESSOR_UNKNOWN,
1120 CPU_NOP_FLAGS, 0 },
1121 { STRING_COMMA_LEN (".syscall"), PROCESSOR_UNKNOWN,
1122 CPU_SYSCALL_FLAGS, 0 },
1123 { STRING_COMMA_LEN (".rdtscp"), PROCESSOR_UNKNOWN,
1124 CPU_RDTSCP_FLAGS, 0 },
1125 { STRING_COMMA_LEN (".3dnow"), PROCESSOR_UNKNOWN,
1126 CPU_3DNOW_FLAGS, 0 },
1127 { STRING_COMMA_LEN (".3dnowa"), PROCESSOR_UNKNOWN,
1128 CPU_3DNOWA_FLAGS, 0 },
1129 { STRING_COMMA_LEN (".padlock"), PROCESSOR_UNKNOWN,
1130 CPU_PADLOCK_FLAGS, 0 },
1131 { STRING_COMMA_LEN (".pacifica"), PROCESSOR_UNKNOWN,
1132 CPU_SVME_FLAGS, 1 },
1133 { STRING_COMMA_LEN (".svme"), PROCESSOR_UNKNOWN,
1134 CPU_SVME_FLAGS, 0 },
1135 { STRING_COMMA_LEN (".sse4a"), PROCESSOR_UNKNOWN,
1136 CPU_SSE4A_FLAGS, 0 },
1137 { STRING_COMMA_LEN (".abm"), PROCESSOR_UNKNOWN,
1138 CPU_ABM_FLAGS, 0 },
1139 { STRING_COMMA_LEN (".bmi"), PROCESSOR_UNKNOWN,
1140 CPU_BMI_FLAGS, 0 },
1141 { STRING_COMMA_LEN (".tbm"), PROCESSOR_UNKNOWN,
1142 CPU_TBM_FLAGS, 0 },
1143 { STRING_COMMA_LEN (".adx"), PROCESSOR_UNKNOWN,
1144 CPU_ADX_FLAGS, 0 },
1145 { STRING_COMMA_LEN (".rdseed"), PROCESSOR_UNKNOWN,
1146 CPU_RDSEED_FLAGS, 0 },
1147 { STRING_COMMA_LEN (".prfchw"), PROCESSOR_UNKNOWN,
1148 CPU_PRFCHW_FLAGS, 0 },
1149 { STRING_COMMA_LEN (".smap"), PROCESSOR_UNKNOWN,
1150 CPU_SMAP_FLAGS, 0 },
1151 { STRING_COMMA_LEN (".mpx"), PROCESSOR_UNKNOWN,
1152 CPU_MPX_FLAGS, 0 },
1153 { STRING_COMMA_LEN (".sha"), PROCESSOR_UNKNOWN,
1154 CPU_SHA_FLAGS, 0 },
1155 { STRING_COMMA_LEN (".clflushopt"), PROCESSOR_UNKNOWN,
1156 CPU_CLFLUSHOPT_FLAGS, 0 },
1157 { STRING_COMMA_LEN (".prefetchwt1"), PROCESSOR_UNKNOWN,
1158 CPU_PREFETCHWT1_FLAGS, 0 },
1159 { STRING_COMMA_LEN (".se1"), PROCESSOR_UNKNOWN,
1160 CPU_SE1_FLAGS, 0 },
1161 { STRING_COMMA_LEN (".clwb"), PROCESSOR_UNKNOWN,
1162 CPU_CLWB_FLAGS, 0 },
1163 { STRING_COMMA_LEN (".avx512ifma"), PROCESSOR_UNKNOWN,
1164 CPU_AVX512IFMA_FLAGS, 0 },
1165 { STRING_COMMA_LEN (".avx512vbmi"), PROCESSOR_UNKNOWN,
1166 CPU_AVX512VBMI_FLAGS, 0 },
1167 { STRING_COMMA_LEN (".avx512_4fmaps"), PROCESSOR_UNKNOWN,
1168 CPU_AVX512_4FMAPS_FLAGS, 0 },
1169 { STRING_COMMA_LEN (".avx512_4vnniw"), PROCESSOR_UNKNOWN,
1170 CPU_AVX512_4VNNIW_FLAGS, 0 },
1171 { STRING_COMMA_LEN (".avx512_vpopcntdq"), PROCESSOR_UNKNOWN,
1172 CPU_AVX512_VPOPCNTDQ_FLAGS, 0 },
1173 { STRING_COMMA_LEN (".avx512_vbmi2"), PROCESSOR_UNKNOWN,
1174 CPU_AVX512_VBMI2_FLAGS, 0 },
1175 { STRING_COMMA_LEN (".avx512_vnni"), PROCESSOR_UNKNOWN,
1176 CPU_AVX512_VNNI_FLAGS, 0 },
1177 { STRING_COMMA_LEN (".avx512_bitalg"), PROCESSOR_UNKNOWN,
1178 CPU_AVX512_BITALG_FLAGS, 0 },
1179 { STRING_COMMA_LEN (".avx_vnni"), PROCESSOR_UNKNOWN,
1180 CPU_AVX_VNNI_FLAGS, 0 },
1181 { STRING_COMMA_LEN (".clzero"), PROCESSOR_UNKNOWN,
1182 CPU_CLZERO_FLAGS, 0 },
1183 { STRING_COMMA_LEN (".mwaitx"), PROCESSOR_UNKNOWN,
1184 CPU_MWAITX_FLAGS, 0 },
1185 { STRING_COMMA_LEN (".ospke"), PROCESSOR_UNKNOWN,
1186 CPU_OSPKE_FLAGS, 0 },
1187 { STRING_COMMA_LEN (".rdpid"), PROCESSOR_UNKNOWN,
1188 CPU_RDPID_FLAGS, 0 },
1189 { STRING_COMMA_LEN (".ptwrite"), PROCESSOR_UNKNOWN,
1190 CPU_PTWRITE_FLAGS, 0 },
1191 { STRING_COMMA_LEN (".ibt"), PROCESSOR_UNKNOWN,
1192 CPU_IBT_FLAGS, 0 },
1193 { STRING_COMMA_LEN (".shstk"), PROCESSOR_UNKNOWN,
1194 CPU_SHSTK_FLAGS, 0 },
1195 { STRING_COMMA_LEN (".gfni"), PROCESSOR_UNKNOWN,
1196 CPU_GFNI_FLAGS, 0 },
1197 { STRING_COMMA_LEN (".vaes"), PROCESSOR_UNKNOWN,
1198 CPU_VAES_FLAGS, 0 },
1199 { STRING_COMMA_LEN (".vpclmulqdq"), PROCESSOR_UNKNOWN,
1200 CPU_VPCLMULQDQ_FLAGS, 0 },
1201 { STRING_COMMA_LEN (".wbnoinvd"), PROCESSOR_UNKNOWN,
1202 CPU_WBNOINVD_FLAGS, 0 },
1203 { STRING_COMMA_LEN (".pconfig"), PROCESSOR_UNKNOWN,
1204 CPU_PCONFIG_FLAGS, 0 },
1205 { STRING_COMMA_LEN (".waitpkg"), PROCESSOR_UNKNOWN,
1206 CPU_WAITPKG_FLAGS, 0 },
1207 { STRING_COMMA_LEN (".cldemote"), PROCESSOR_UNKNOWN,
1208 CPU_CLDEMOTE_FLAGS, 0 },
1209 { STRING_COMMA_LEN (".amx_int8"), PROCESSOR_UNKNOWN,
1210 CPU_AMX_INT8_FLAGS, 0 },
1211 { STRING_COMMA_LEN (".amx_bf16"), PROCESSOR_UNKNOWN,
1212 CPU_AMX_BF16_FLAGS, 0 },
1213 { STRING_COMMA_LEN (".amx_tile"), PROCESSOR_UNKNOWN,
1214 CPU_AMX_TILE_FLAGS, 0 },
1215 { STRING_COMMA_LEN (".movdiri"), PROCESSOR_UNKNOWN,
1216 CPU_MOVDIRI_FLAGS, 0 },
1217 { STRING_COMMA_LEN (".movdir64b"), PROCESSOR_UNKNOWN,
1218 CPU_MOVDIR64B_FLAGS, 0 },
1219 { STRING_COMMA_LEN (".avx512_bf16"), PROCESSOR_UNKNOWN,
1220 CPU_AVX512_BF16_FLAGS, 0 },
1221 { STRING_COMMA_LEN (".avx512_vp2intersect"), PROCESSOR_UNKNOWN,
1222 CPU_AVX512_VP2INTERSECT_FLAGS, 0 },
1223 { STRING_COMMA_LEN (".tdx"), PROCESSOR_UNKNOWN,
1224 CPU_TDX_FLAGS, 0 },
1225 { STRING_COMMA_LEN (".enqcmd"), PROCESSOR_UNKNOWN,
1226 CPU_ENQCMD_FLAGS, 0 },
1227 { STRING_COMMA_LEN (".serialize"), PROCESSOR_UNKNOWN,
1228 CPU_SERIALIZE_FLAGS, 0 },
1229 { STRING_COMMA_LEN (".rdpru"), PROCESSOR_UNKNOWN,
1230 CPU_RDPRU_FLAGS, 0 },
1231 { STRING_COMMA_LEN (".mcommit"), PROCESSOR_UNKNOWN,
1232 CPU_MCOMMIT_FLAGS, 0 },
1233 { STRING_COMMA_LEN (".sev_es"), PROCESSOR_UNKNOWN,
1234 CPU_SEV_ES_FLAGS, 0 },
1235 { STRING_COMMA_LEN (".tsxldtrk"), PROCESSOR_UNKNOWN,
1236 CPU_TSXLDTRK_FLAGS, 0 },
1237 { STRING_COMMA_LEN (".kl"), PROCESSOR_UNKNOWN,
1238 CPU_KL_FLAGS, 0 },
1239 { STRING_COMMA_LEN (".widekl"), PROCESSOR_UNKNOWN,
1240 CPU_WIDEKL_FLAGS, 0 },
1241 { STRING_COMMA_LEN (".uintr"), PROCESSOR_UNKNOWN,
1242 CPU_UINTR_FLAGS, 0 },
1243 { STRING_COMMA_LEN (".hreset"), PROCESSOR_UNKNOWN,
1244 CPU_HRESET_FLAGS, 0 },
1245 { STRING_COMMA_LEN (".avx512_fp16"), PROCESSOR_UNKNOWN,
1246 CPU_AVX512_FP16_FLAGS, 0 },
1247 };
1248
1249 static const noarch_entry cpu_noarch[] =
1250 {
1251 { STRING_COMMA_LEN ("no87"), CPU_ANY_X87_FLAGS },
1252 { STRING_COMMA_LEN ("no287"), CPU_ANY_287_FLAGS },
1253 { STRING_COMMA_LEN ("no387"), CPU_ANY_387_FLAGS },
1254 { STRING_COMMA_LEN ("no687"), CPU_ANY_687_FLAGS },
1255 { STRING_COMMA_LEN ("nocmov"), CPU_ANY_CMOV_FLAGS },
1256 { STRING_COMMA_LEN ("nofxsr"), CPU_ANY_FXSR_FLAGS },
1257 { STRING_COMMA_LEN ("nommx"), CPU_ANY_MMX_FLAGS },
1258 { STRING_COMMA_LEN ("nosse"), CPU_ANY_SSE_FLAGS },
1259 { STRING_COMMA_LEN ("nosse2"), CPU_ANY_SSE2_FLAGS },
1260 { STRING_COMMA_LEN ("nosse3"), CPU_ANY_SSE3_FLAGS },
1261 { STRING_COMMA_LEN ("nosse4a"), CPU_ANY_SSE4A_FLAGS },
1262 { STRING_COMMA_LEN ("nossse3"), CPU_ANY_SSSE3_FLAGS },
1263 { STRING_COMMA_LEN ("nosse4.1"), CPU_ANY_SSE4_1_FLAGS },
1264 { STRING_COMMA_LEN ("nosse4.2"), CPU_ANY_SSE4_2_FLAGS },
1265 { STRING_COMMA_LEN ("nosse4"), CPU_ANY_SSE4_1_FLAGS },
1266 { STRING_COMMA_LEN ("noavx"), CPU_ANY_AVX_FLAGS },
1267 { STRING_COMMA_LEN ("noavx2"), CPU_ANY_AVX2_FLAGS },
1268 { STRING_COMMA_LEN ("noavx512f"), CPU_ANY_AVX512F_FLAGS },
1269 { STRING_COMMA_LEN ("noavx512cd"), CPU_ANY_AVX512CD_FLAGS },
1270 { STRING_COMMA_LEN ("noavx512er"), CPU_ANY_AVX512ER_FLAGS },
1271 { STRING_COMMA_LEN ("noavx512pf"), CPU_ANY_AVX512PF_FLAGS },
1272 { STRING_COMMA_LEN ("noavx512dq"), CPU_ANY_AVX512DQ_FLAGS },
1273 { STRING_COMMA_LEN ("noavx512bw"), CPU_ANY_AVX512BW_FLAGS },
1274 { STRING_COMMA_LEN ("noavx512vl"), CPU_ANY_AVX512VL_FLAGS },
1275 { STRING_COMMA_LEN ("noavx512ifma"), CPU_ANY_AVX512IFMA_FLAGS },
1276 { STRING_COMMA_LEN ("noavx512vbmi"), CPU_ANY_AVX512VBMI_FLAGS },
1277 { STRING_COMMA_LEN ("noavx512_4fmaps"), CPU_ANY_AVX512_4FMAPS_FLAGS },
1278 { STRING_COMMA_LEN ("noavx512_4vnniw"), CPU_ANY_AVX512_4VNNIW_FLAGS },
1279 { STRING_COMMA_LEN ("noavx512_vpopcntdq"), CPU_ANY_AVX512_VPOPCNTDQ_FLAGS },
1280 { STRING_COMMA_LEN ("noavx512_vbmi2"), CPU_ANY_AVX512_VBMI2_FLAGS },
1281 { STRING_COMMA_LEN ("noavx512_vnni"), CPU_ANY_AVX512_VNNI_FLAGS },
1282 { STRING_COMMA_LEN ("noavx512_bitalg"), CPU_ANY_AVX512_BITALG_FLAGS },
1283 { STRING_COMMA_LEN ("noavx_vnni"), CPU_ANY_AVX_VNNI_FLAGS },
1284 { STRING_COMMA_LEN ("noibt"), CPU_ANY_IBT_FLAGS },
1285 { STRING_COMMA_LEN ("noshstk"), CPU_ANY_SHSTK_FLAGS },
1286 { STRING_COMMA_LEN ("noamx_int8"), CPU_ANY_AMX_INT8_FLAGS },
1287 { STRING_COMMA_LEN ("noamx_bf16"), CPU_ANY_AMX_BF16_FLAGS },
1288 { STRING_COMMA_LEN ("noamx_tile"), CPU_ANY_AMX_TILE_FLAGS },
1289 { STRING_COMMA_LEN ("nomovdiri"), CPU_ANY_MOVDIRI_FLAGS },
1290 { STRING_COMMA_LEN ("nomovdir64b"), CPU_ANY_MOVDIR64B_FLAGS },
1291 { STRING_COMMA_LEN ("noavx512_bf16"), CPU_ANY_AVX512_BF16_FLAGS },
1292 { STRING_COMMA_LEN ("noavx512_vp2intersect"),
1293 CPU_ANY_AVX512_VP2INTERSECT_FLAGS },
1294 { STRING_COMMA_LEN ("notdx"), CPU_ANY_TDX_FLAGS },
1295 { STRING_COMMA_LEN ("noenqcmd"), CPU_ANY_ENQCMD_FLAGS },
1296 { STRING_COMMA_LEN ("noserialize"), CPU_ANY_SERIALIZE_FLAGS },
1297 { STRING_COMMA_LEN ("notsxldtrk"), CPU_ANY_TSXLDTRK_FLAGS },
1298 { STRING_COMMA_LEN ("nokl"), CPU_ANY_KL_FLAGS },
1299 { STRING_COMMA_LEN ("nowidekl"), CPU_ANY_WIDEKL_FLAGS },
1300 { STRING_COMMA_LEN ("nouintr"), CPU_ANY_UINTR_FLAGS },
1301 { STRING_COMMA_LEN ("nohreset"), CPU_ANY_HRESET_FLAGS },
1302 { STRING_COMMA_LEN ("noavx512_fp16"), CPU_ANY_AVX512_FP16_FLAGS },
1303 };
1304
1305 #ifdef I386COFF
1306 /* Like s_lcomm_internal in gas/read.c but the alignment string
1307 is allowed to be optional. */
1308
1309 static symbolS *
1310 pe_lcomm_internal (int needs_align, symbolS *symbolP, addressT size)
1311 {
1312 addressT align = 0;
1313
1314 SKIP_WHITESPACE ();
1315
1316 if (needs_align
1317 && *input_line_pointer == ',')
1318 {
1319 align = parse_align (needs_align - 1);
1320
1321 if (align == (addressT) -1)
1322 return NULL;
1323 }
1324 else
1325 {
1326 if (size >= 8)
1327 align = 3;
1328 else if (size >= 4)
1329 align = 2;
1330 else if (size >= 2)
1331 align = 1;
1332 else
1333 align = 0;
1334 }
1335
1336 bss_alloc (symbolP, size, align);
1337 return symbolP;
1338 }
1339
1340 static void
1341 pe_lcomm (int needs_align)
1342 {
1343 s_comm_internal (needs_align * 2, pe_lcomm_internal);
1344 }
1345 #endif
1346
1347 const pseudo_typeS md_pseudo_table[] =
1348 {
1349 #if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
1350 {"align", s_align_bytes, 0},
1351 #else
1352 {"align", s_align_ptwo, 0},
1353 #endif
1354 {"arch", set_cpu_arch, 0},
1355 #ifndef I386COFF
1356 {"bss", s_bss, 0},
1357 #else
1358 {"lcomm", pe_lcomm, 1},
1359 #endif
1360 {"ffloat", float_cons, 'f'},
1361 {"dfloat", float_cons, 'd'},
1362 {"tfloat", float_cons, 'x'},
1363 {"hfloat", float_cons, 'h'},
1364 {"bfloat16", float_cons, 'b'},
1365 {"value", cons, 2},
1366 {"slong", signed_cons, 4},
1367 {"noopt", s_ignore, 0},
1368 {"optim", s_ignore, 0},
1369 {"code16gcc", set_16bit_gcc_code_flag, CODE_16BIT},
1370 {"code16", set_code_flag, CODE_16BIT},
1371 {"code32", set_code_flag, CODE_32BIT},
1372 #ifdef BFD64
1373 {"code64", set_code_flag, CODE_64BIT},
1374 #endif
1375 {"intel_syntax", set_intel_syntax, 1},
1376 {"att_syntax", set_intel_syntax, 0},
1377 {"intel_mnemonic", set_intel_mnemonic, 1},
1378 {"att_mnemonic", set_intel_mnemonic, 0},
1379 {"allow_index_reg", set_allow_index_reg, 1},
1380 {"disallow_index_reg", set_allow_index_reg, 0},
1381 {"sse_check", set_check, 0},
1382 {"operand_check", set_check, 1},
1383 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1384 {"largecomm", handle_large_common, 0},
1385 #else
1386 {"file", dwarf2_directive_file, 0},
1387 {"loc", dwarf2_directive_loc, 0},
1388 {"loc_mark_labels", dwarf2_directive_loc_mark_labels, 0},
1389 #endif
1390 #ifdef TE_PE
1391 {"secrel32", pe_directive_secrel, 0},
1392 #endif
1393 {0, 0, 0}
1394 };
1395
1396 /* For interface with expression (). */
1397 extern char *input_line_pointer;
1398
1399 /* Hash table for instruction mnemonic lookup. */
1400 static htab_t op_hash;
1401
1402 /* Hash table for register lookup. */
1403 static htab_t reg_hash;
1404 \f
1405 /* Various efficient no-op patterns for aligning code labels.
1406 Note: Don't try to assemble the instructions in the comments.
1407 0L and 0w are not legal. */
1408 static const unsigned char f32_1[] =
1409 {0x90}; /* nop */
1410 static const unsigned char f32_2[] =
1411 {0x66,0x90}; /* xchg %ax,%ax */
1412 static const unsigned char f32_3[] =
1413 {0x8d,0x76,0x00}; /* leal 0(%esi),%esi */
1414 static const unsigned char f32_4[] =
1415 {0x8d,0x74,0x26,0x00}; /* leal 0(%esi,1),%esi */
1416 static const unsigned char f32_6[] =
1417 {0x8d,0xb6,0x00,0x00,0x00,0x00}; /* leal 0L(%esi),%esi */
1418 static const unsigned char f32_7[] =
1419 {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal 0L(%esi,1),%esi */
1420 static const unsigned char f16_3[] =
1421 {0x8d,0x74,0x00}; /* lea 0(%si),%si */
1422 static const unsigned char f16_4[] =
1423 {0x8d,0xb4,0x00,0x00}; /* lea 0W(%si),%si */
1424 static const unsigned char jump_disp8[] =
1425 {0xeb}; /* jmp disp8 */
1426 static const unsigned char jump32_disp32[] =
1427 {0xe9}; /* jmp disp32 */
1428 static const unsigned char jump16_disp32[] =
1429 {0x66,0xe9}; /* jmp disp32 */
1430 /* 32-bit NOPs patterns. */
1431 static const unsigned char *const f32_patt[] = {
1432 f32_1, f32_2, f32_3, f32_4, NULL, f32_6, f32_7
1433 };
1434 /* 16-bit NOPs patterns. */
1435 static const unsigned char *const f16_patt[] = {
1436 f32_1, f32_2, f16_3, f16_4
1437 };
1438 /* nopl (%[re]ax) */
1439 static const unsigned char alt_3[] =
1440 {0x0f,0x1f,0x00};
1441 /* nopl 0(%[re]ax) */
1442 static const unsigned char alt_4[] =
1443 {0x0f,0x1f,0x40,0x00};
1444 /* nopl 0(%[re]ax,%[re]ax,1) */
1445 static const unsigned char alt_5[] =
1446 {0x0f,0x1f,0x44,0x00,0x00};
1447 /* nopw 0(%[re]ax,%[re]ax,1) */
1448 static const unsigned char alt_6[] =
1449 {0x66,0x0f,0x1f,0x44,0x00,0x00};
1450 /* nopl 0L(%[re]ax) */
1451 static const unsigned char alt_7[] =
1452 {0x0f,0x1f,0x80,0x00,0x00,0x00,0x00};
1453 /* nopl 0L(%[re]ax,%[re]ax,1) */
1454 static const unsigned char alt_8[] =
1455 {0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1456 /* nopw 0L(%[re]ax,%[re]ax,1) */
1457 static const unsigned char alt_9[] =
1458 {0x66,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1459 /* nopw %cs:0L(%[re]ax,%[re]ax,1) */
1460 static const unsigned char alt_10[] =
1461 {0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1462 /* data16 nopw %cs:0L(%eax,%eax,1) */
1463 static const unsigned char alt_11[] =
1464 {0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1465 /* 32-bit and 64-bit NOPs patterns. */
1466 static const unsigned char *const alt_patt[] = {
1467 f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
1468 alt_9, alt_10, alt_11
1469 };
1470
1471 /* Genenerate COUNT bytes of NOPs to WHERE from PATT with the maximum
1472 size of a single NOP instruction MAX_SINGLE_NOP_SIZE. */
1473
1474 static void
1475 i386_output_nops (char *where, const unsigned char *const *patt,
1476 int count, int max_single_nop_size)
1477
1478 {
1479 /* Place the longer NOP first. */
1480 int last;
1481 int offset;
1482 const unsigned char *nops;
1483
1484 if (max_single_nop_size < 1)
1485 {
1486 as_fatal (_("i386_output_nops called to generate nops of at most %d bytes!"),
1487 max_single_nop_size);
1488 return;
1489 }
1490
1491 nops = patt[max_single_nop_size - 1];
1492
1493 /* Use the smaller one if the requsted one isn't available. */
1494 if (nops == NULL)
1495 {
1496 max_single_nop_size--;
1497 nops = patt[max_single_nop_size - 1];
1498 }
1499
1500 last = count % max_single_nop_size;
1501
1502 count -= last;
1503 for (offset = 0; offset < count; offset += max_single_nop_size)
1504 memcpy (where + offset, nops, max_single_nop_size);
1505
1506 if (last)
1507 {
1508 nops = patt[last - 1];
1509 if (nops == NULL)
1510 {
1511 /* Use the smaller one plus one-byte NOP if the needed one
1512 isn't available. */
1513 last--;
1514 nops = patt[last - 1];
1515 memcpy (where + offset, nops, last);
1516 where[offset + last] = *patt[0];
1517 }
1518 else
1519 memcpy (where + offset, nops, last);
1520 }
1521 }
1522
1523 static INLINE int
1524 fits_in_imm7 (offsetT num)
1525 {
1526 return (num & 0x7f) == num;
1527 }
1528
1529 static INLINE int
1530 fits_in_imm31 (offsetT num)
1531 {
1532 return (num & 0x7fffffff) == num;
1533 }
1534
1535 /* Genenerate COUNT bytes of NOPs to WHERE with the maximum size of a
1536 single NOP instruction LIMIT. */
1537
1538 void
1539 i386_generate_nops (fragS *fragP, char *where, offsetT count, int limit)
1540 {
1541 const unsigned char *const *patt = NULL;
1542 int max_single_nop_size;
1543 /* Maximum number of NOPs before switching to jump over NOPs. */
1544 int max_number_of_nops;
1545
1546 switch (fragP->fr_type)
1547 {
1548 case rs_fill_nop:
1549 case rs_align_code:
1550 break;
1551 case rs_machine_dependent:
1552 /* Allow NOP padding for jumps and calls. */
1553 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
1554 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
1555 break;
1556 /* Fall through. */
1557 default:
1558 return;
1559 }
1560
1561 /* We need to decide which NOP sequence to use for 32bit and
1562 64bit. When -mtune= is used:
1563
1564 1. For PROCESSOR_I386, PROCESSOR_I486, PROCESSOR_PENTIUM and
1565 PROCESSOR_GENERIC32, f32_patt will be used.
1566 2. For the rest, alt_patt will be used.
1567
1568 When -mtune= isn't used, alt_patt will be used if
1569 cpu_arch_isa_flags has CpuNop. Otherwise, f32_patt will
1570 be used.
1571
1572 When -march= or .arch is used, we can't use anything beyond
1573 cpu_arch_isa_flags. */
1574
1575 if (flag_code == CODE_16BIT)
1576 {
1577 patt = f16_patt;
1578 max_single_nop_size = sizeof (f16_patt) / sizeof (f16_patt[0]);
1579 /* Limit number of NOPs to 2 in 16-bit mode. */
1580 max_number_of_nops = 2;
1581 }
1582 else
1583 {
1584 if (fragP->tc_frag_data.isa == PROCESSOR_UNKNOWN)
1585 {
1586 /* PROCESSOR_UNKNOWN means that all ISAs may be used. */
1587 switch (cpu_arch_tune)
1588 {
1589 case PROCESSOR_UNKNOWN:
1590 /* We use cpu_arch_isa_flags to check if we SHOULD
1591 optimize with nops. */
1592 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
1593 patt = alt_patt;
1594 else
1595 patt = f32_patt;
1596 break;
1597 case PROCESSOR_PENTIUM4:
1598 case PROCESSOR_NOCONA:
1599 case PROCESSOR_CORE:
1600 case PROCESSOR_CORE2:
1601 case PROCESSOR_COREI7:
1602 case PROCESSOR_L1OM:
1603 case PROCESSOR_K1OM:
1604 case PROCESSOR_GENERIC64:
1605 case PROCESSOR_K6:
1606 case PROCESSOR_ATHLON:
1607 case PROCESSOR_K8:
1608 case PROCESSOR_AMDFAM10:
1609 case PROCESSOR_BD:
1610 case PROCESSOR_ZNVER:
1611 case PROCESSOR_BT:
1612 patt = alt_patt;
1613 break;
1614 case PROCESSOR_I386:
1615 case PROCESSOR_I486:
1616 case PROCESSOR_PENTIUM:
1617 case PROCESSOR_PENTIUMPRO:
1618 case PROCESSOR_IAMCU:
1619 case PROCESSOR_GENERIC32:
1620 patt = f32_patt;
1621 break;
1622 }
1623 }
1624 else
1625 {
1626 switch (fragP->tc_frag_data.tune)
1627 {
1628 case PROCESSOR_UNKNOWN:
1629 /* When cpu_arch_isa is set, cpu_arch_tune shouldn't be
1630 PROCESSOR_UNKNOWN. */
1631 abort ();
1632 break;
1633
1634 case PROCESSOR_I386:
1635 case PROCESSOR_I486:
1636 case PROCESSOR_PENTIUM:
1637 case PROCESSOR_IAMCU:
1638 case PROCESSOR_K6:
1639 case PROCESSOR_ATHLON:
1640 case PROCESSOR_K8:
1641 case PROCESSOR_AMDFAM10:
1642 case PROCESSOR_BD:
1643 case PROCESSOR_ZNVER:
1644 case PROCESSOR_BT:
1645 case PROCESSOR_GENERIC32:
1646 /* We use cpu_arch_isa_flags to check if we CAN optimize
1647 with nops. */
1648 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
1649 patt = alt_patt;
1650 else
1651 patt = f32_patt;
1652 break;
1653 case PROCESSOR_PENTIUMPRO:
1654 case PROCESSOR_PENTIUM4:
1655 case PROCESSOR_NOCONA:
1656 case PROCESSOR_CORE:
1657 case PROCESSOR_CORE2:
1658 case PROCESSOR_COREI7:
1659 case PROCESSOR_L1OM:
1660 case PROCESSOR_K1OM:
1661 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
1662 patt = alt_patt;
1663 else
1664 patt = f32_patt;
1665 break;
1666 case PROCESSOR_GENERIC64:
1667 patt = alt_patt;
1668 break;
1669 }
1670 }
1671
1672 if (patt == f32_patt)
1673 {
1674 max_single_nop_size = sizeof (f32_patt) / sizeof (f32_patt[0]);
1675 /* Limit number of NOPs to 2 for older processors. */
1676 max_number_of_nops = 2;
1677 }
1678 else
1679 {
1680 max_single_nop_size = sizeof (alt_patt) / sizeof (alt_patt[0]);
1681 /* Limit number of NOPs to 7 for newer processors. */
1682 max_number_of_nops = 7;
1683 }
1684 }
1685
1686 if (limit == 0)
1687 limit = max_single_nop_size;
1688
1689 if (fragP->fr_type == rs_fill_nop)
1690 {
1691 /* Output NOPs for .nop directive. */
1692 if (limit > max_single_nop_size)
1693 {
1694 as_bad_where (fragP->fr_file, fragP->fr_line,
1695 _("invalid single nop size: %d "
1696 "(expect within [0, %d])"),
1697 limit, max_single_nop_size);
1698 return;
1699 }
1700 }
1701 else if (fragP->fr_type != rs_machine_dependent)
1702 fragP->fr_var = count;
1703
1704 if ((count / max_single_nop_size) > max_number_of_nops)
1705 {
1706 /* Generate jump over NOPs. */
1707 offsetT disp = count - 2;
1708 if (fits_in_imm7 (disp))
1709 {
1710 /* Use "jmp disp8" if possible. */
1711 count = disp;
1712 where[0] = jump_disp8[0];
1713 where[1] = count;
1714 where += 2;
1715 }
1716 else
1717 {
1718 unsigned int size_of_jump;
1719
1720 if (flag_code == CODE_16BIT)
1721 {
1722 where[0] = jump16_disp32[0];
1723 where[1] = jump16_disp32[1];
1724 size_of_jump = 2;
1725 }
1726 else
1727 {
1728 where[0] = jump32_disp32[0];
1729 size_of_jump = 1;
1730 }
1731
1732 count -= size_of_jump + 4;
1733 if (!fits_in_imm31 (count))
1734 {
1735 as_bad_where (fragP->fr_file, fragP->fr_line,
1736 _("jump over nop padding out of range"));
1737 return;
1738 }
1739
1740 md_number_to_chars (where + size_of_jump, count, 4);
1741 where += size_of_jump + 4;
1742 }
1743 }
1744
1745 /* Generate multiple NOPs. */
1746 i386_output_nops (where, patt, count, limit);
1747 }
1748
1749 static INLINE int
1750 operand_type_all_zero (const union i386_operand_type *x)
1751 {
1752 switch (ARRAY_SIZE(x->array))
1753 {
1754 case 3:
1755 if (x->array[2])
1756 return 0;
1757 /* Fall through. */
1758 case 2:
1759 if (x->array[1])
1760 return 0;
1761 /* Fall through. */
1762 case 1:
1763 return !x->array[0];
1764 default:
1765 abort ();
1766 }
1767 }
1768
1769 static INLINE void
1770 operand_type_set (union i386_operand_type *x, unsigned int v)
1771 {
1772 switch (ARRAY_SIZE(x->array))
1773 {
1774 case 3:
1775 x->array[2] = v;
1776 /* Fall through. */
1777 case 2:
1778 x->array[1] = v;
1779 /* Fall through. */
1780 case 1:
1781 x->array[0] = v;
1782 /* Fall through. */
1783 break;
1784 default:
1785 abort ();
1786 }
1787
1788 x->bitfield.class = ClassNone;
1789 x->bitfield.instance = InstanceNone;
1790 }
1791
1792 static INLINE int
1793 operand_type_equal (const union i386_operand_type *x,
1794 const union i386_operand_type *y)
1795 {
1796 switch (ARRAY_SIZE(x->array))
1797 {
1798 case 3:
1799 if (x->array[2] != y->array[2])
1800 return 0;
1801 /* Fall through. */
1802 case 2:
1803 if (x->array[1] != y->array[1])
1804 return 0;
1805 /* Fall through. */
1806 case 1:
1807 return x->array[0] == y->array[0];
1808 break;
1809 default:
1810 abort ();
1811 }
1812 }
1813
1814 static INLINE int
1815 cpu_flags_all_zero (const union i386_cpu_flags *x)
1816 {
1817 switch (ARRAY_SIZE(x->array))
1818 {
1819 case 4:
1820 if (x->array[3])
1821 return 0;
1822 /* Fall through. */
1823 case 3:
1824 if (x->array[2])
1825 return 0;
1826 /* Fall through. */
1827 case 2:
1828 if (x->array[1])
1829 return 0;
1830 /* Fall through. */
1831 case 1:
1832 return !x->array[0];
1833 default:
1834 abort ();
1835 }
1836 }
1837
1838 static INLINE int
1839 cpu_flags_equal (const union i386_cpu_flags *x,
1840 const union i386_cpu_flags *y)
1841 {
1842 switch (ARRAY_SIZE(x->array))
1843 {
1844 case 4:
1845 if (x->array[3] != y->array[3])
1846 return 0;
1847 /* Fall through. */
1848 case 3:
1849 if (x->array[2] != y->array[2])
1850 return 0;
1851 /* Fall through. */
1852 case 2:
1853 if (x->array[1] != y->array[1])
1854 return 0;
1855 /* Fall through. */
1856 case 1:
1857 return x->array[0] == y->array[0];
1858 break;
1859 default:
1860 abort ();
1861 }
1862 }
1863
1864 static INLINE int
1865 cpu_flags_check_cpu64 (i386_cpu_flags f)
1866 {
1867 return !((flag_code == CODE_64BIT && f.bitfield.cpuno64)
1868 || (flag_code != CODE_64BIT && f.bitfield.cpu64));
1869 }
1870
1871 static INLINE i386_cpu_flags
1872 cpu_flags_and (i386_cpu_flags x, i386_cpu_flags y)
1873 {
1874 switch (ARRAY_SIZE (x.array))
1875 {
1876 case 4:
1877 x.array [3] &= y.array [3];
1878 /* Fall through. */
1879 case 3:
1880 x.array [2] &= y.array [2];
1881 /* Fall through. */
1882 case 2:
1883 x.array [1] &= y.array [1];
1884 /* Fall through. */
1885 case 1:
1886 x.array [0] &= y.array [0];
1887 break;
1888 default:
1889 abort ();
1890 }
1891 return x;
1892 }
1893
1894 static INLINE i386_cpu_flags
1895 cpu_flags_or (i386_cpu_flags x, i386_cpu_flags y)
1896 {
1897 switch (ARRAY_SIZE (x.array))
1898 {
1899 case 4:
1900 x.array [3] |= y.array [3];
1901 /* Fall through. */
1902 case 3:
1903 x.array [2] |= y.array [2];
1904 /* Fall through. */
1905 case 2:
1906 x.array [1] |= y.array [1];
1907 /* Fall through. */
1908 case 1:
1909 x.array [0] |= y.array [0];
1910 break;
1911 default:
1912 abort ();
1913 }
1914 return x;
1915 }
1916
1917 static INLINE i386_cpu_flags
1918 cpu_flags_and_not (i386_cpu_flags x, i386_cpu_flags y)
1919 {
1920 switch (ARRAY_SIZE (x.array))
1921 {
1922 case 4:
1923 x.array [3] &= ~y.array [3];
1924 /* Fall through. */
1925 case 3:
1926 x.array [2] &= ~y.array [2];
1927 /* Fall through. */
1928 case 2:
1929 x.array [1] &= ~y.array [1];
1930 /* Fall through. */
1931 case 1:
1932 x.array [0] &= ~y.array [0];
1933 break;
1934 default:
1935 abort ();
1936 }
1937 return x;
1938 }
1939
1940 static const i386_cpu_flags avx512 = CPU_ANY_AVX512F_FLAGS;
1941
1942 #define CPU_FLAGS_ARCH_MATCH 0x1
1943 #define CPU_FLAGS_64BIT_MATCH 0x2
1944
1945 #define CPU_FLAGS_PERFECT_MATCH \
1946 (CPU_FLAGS_ARCH_MATCH | CPU_FLAGS_64BIT_MATCH)
1947
1948 /* Return CPU flags match bits. */
1949
1950 static int
1951 cpu_flags_match (const insn_template *t)
1952 {
1953 i386_cpu_flags x = t->cpu_flags;
1954 int match = cpu_flags_check_cpu64 (x) ? CPU_FLAGS_64BIT_MATCH : 0;
1955
1956 x.bitfield.cpu64 = 0;
1957 x.bitfield.cpuno64 = 0;
1958
1959 if (cpu_flags_all_zero (&x))
1960 {
1961 /* This instruction is available on all archs. */
1962 match |= CPU_FLAGS_ARCH_MATCH;
1963 }
1964 else
1965 {
1966 /* This instruction is available only on some archs. */
1967 i386_cpu_flags cpu = cpu_arch_flags;
1968
1969 /* AVX512VL is no standalone feature - match it and then strip it. */
1970 if (x.bitfield.cpuavx512vl && !cpu.bitfield.cpuavx512vl)
1971 return match;
1972 x.bitfield.cpuavx512vl = 0;
1973
1974 cpu = cpu_flags_and (x, cpu);
1975 if (!cpu_flags_all_zero (&cpu))
1976 {
1977 if (x.bitfield.cpuavx)
1978 {
1979 /* We need to check a few extra flags with AVX. */
1980 if (cpu.bitfield.cpuavx
1981 && (!t->opcode_modifier.sse2avx
1982 || (sse2avx && !i.prefix[DATA_PREFIX]))
1983 && (!x.bitfield.cpuaes || cpu.bitfield.cpuaes)
1984 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
1985 && (!x.bitfield.cpupclmul || cpu.bitfield.cpupclmul))
1986 match |= CPU_FLAGS_ARCH_MATCH;
1987 }
1988 else if (x.bitfield.cpuavx512f)
1989 {
1990 /* We need to check a few extra flags with AVX512F. */
1991 if (cpu.bitfield.cpuavx512f
1992 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
1993 && (!x.bitfield.cpuvaes || cpu.bitfield.cpuvaes)
1994 && (!x.bitfield.cpuvpclmulqdq || cpu.bitfield.cpuvpclmulqdq))
1995 match |= CPU_FLAGS_ARCH_MATCH;
1996 }
1997 else
1998 match |= CPU_FLAGS_ARCH_MATCH;
1999 }
2000 }
2001 return match;
2002 }
2003
2004 static INLINE i386_operand_type
2005 operand_type_and (i386_operand_type x, i386_operand_type y)
2006 {
2007 if (x.bitfield.class != y.bitfield.class)
2008 x.bitfield.class = ClassNone;
2009 if (x.bitfield.instance != y.bitfield.instance)
2010 x.bitfield.instance = InstanceNone;
2011
2012 switch (ARRAY_SIZE (x.array))
2013 {
2014 case 3:
2015 x.array [2] &= y.array [2];
2016 /* Fall through. */
2017 case 2:
2018 x.array [1] &= y.array [1];
2019 /* Fall through. */
2020 case 1:
2021 x.array [0] &= y.array [0];
2022 break;
2023 default:
2024 abort ();
2025 }
2026 return x;
2027 }
2028
2029 static INLINE i386_operand_type
2030 operand_type_and_not (i386_operand_type x, i386_operand_type y)
2031 {
2032 gas_assert (y.bitfield.class == ClassNone);
2033 gas_assert (y.bitfield.instance == InstanceNone);
2034
2035 switch (ARRAY_SIZE (x.array))
2036 {
2037 case 3:
2038 x.array [2] &= ~y.array [2];
2039 /* Fall through. */
2040 case 2:
2041 x.array [1] &= ~y.array [1];
2042 /* Fall through. */
2043 case 1:
2044 x.array [0] &= ~y.array [0];
2045 break;
2046 default:
2047 abort ();
2048 }
2049 return x;
2050 }
2051
2052 static INLINE i386_operand_type
2053 operand_type_or (i386_operand_type x, i386_operand_type y)
2054 {
2055 gas_assert (x.bitfield.class == ClassNone ||
2056 y.bitfield.class == ClassNone ||
2057 x.bitfield.class == y.bitfield.class);
2058 gas_assert (x.bitfield.instance == InstanceNone ||
2059 y.bitfield.instance == InstanceNone ||
2060 x.bitfield.instance == y.bitfield.instance);
2061
2062 switch (ARRAY_SIZE (x.array))
2063 {
2064 case 3:
2065 x.array [2] |= y.array [2];
2066 /* Fall through. */
2067 case 2:
2068 x.array [1] |= y.array [1];
2069 /* Fall through. */
2070 case 1:
2071 x.array [0] |= y.array [0];
2072 break;
2073 default:
2074 abort ();
2075 }
2076 return x;
2077 }
2078
2079 static INLINE i386_operand_type
2080 operand_type_xor (i386_operand_type x, i386_operand_type y)
2081 {
2082 gas_assert (y.bitfield.class == ClassNone);
2083 gas_assert (y.bitfield.instance == InstanceNone);
2084
2085 switch (ARRAY_SIZE (x.array))
2086 {
2087 case 3:
2088 x.array [2] ^= y.array [2];
2089 /* Fall through. */
2090 case 2:
2091 x.array [1] ^= y.array [1];
2092 /* Fall through. */
2093 case 1:
2094 x.array [0] ^= y.array [0];
2095 break;
2096 default:
2097 abort ();
2098 }
2099 return x;
2100 }
2101
2102 static const i386_operand_type disp16_32 = OPERAND_TYPE_DISP16_32;
2103 static const i386_operand_type anydisp = OPERAND_TYPE_ANYDISP;
2104 static const i386_operand_type anyimm = OPERAND_TYPE_ANYIMM;
2105 static const i386_operand_type regxmm = OPERAND_TYPE_REGXMM;
2106 static const i386_operand_type regmask = OPERAND_TYPE_REGMASK;
2107 static const i386_operand_type imm8 = OPERAND_TYPE_IMM8;
2108 static const i386_operand_type imm8s = OPERAND_TYPE_IMM8S;
2109 static const i386_operand_type imm16 = OPERAND_TYPE_IMM16;
2110 static const i386_operand_type imm32 = OPERAND_TYPE_IMM32;
2111 static const i386_operand_type imm32s = OPERAND_TYPE_IMM32S;
2112 static const i386_operand_type imm64 = OPERAND_TYPE_IMM64;
2113 static const i386_operand_type imm16_32 = OPERAND_TYPE_IMM16_32;
2114 static const i386_operand_type imm16_32s = OPERAND_TYPE_IMM16_32S;
2115 static const i386_operand_type imm16_32_32s = OPERAND_TYPE_IMM16_32_32S;
2116
2117 enum operand_type
2118 {
2119 reg,
2120 imm,
2121 disp,
2122 anymem
2123 };
2124
2125 static INLINE int
2126 operand_type_check (i386_operand_type t, enum operand_type c)
2127 {
2128 switch (c)
2129 {
2130 case reg:
2131 return t.bitfield.class == Reg;
2132
2133 case imm:
2134 return (t.bitfield.imm8
2135 || t.bitfield.imm8s
2136 || t.bitfield.imm16
2137 || t.bitfield.imm32
2138 || t.bitfield.imm32s
2139 || t.bitfield.imm64);
2140
2141 case disp:
2142 return (t.bitfield.disp8
2143 || t.bitfield.disp16
2144 || t.bitfield.disp32
2145 || t.bitfield.disp32s
2146 || t.bitfield.disp64);
2147
2148 case anymem:
2149 return (t.bitfield.disp8
2150 || t.bitfield.disp16
2151 || t.bitfield.disp32
2152 || t.bitfield.disp32s
2153 || t.bitfield.disp64
2154 || t.bitfield.baseindex);
2155
2156 default:
2157 abort ();
2158 }
2159
2160 return 0;
2161 }
2162
2163 /* Return 1 if there is no conflict in 8bit/16bit/32bit/64bit/80bit size
2164 between operand GIVEN and opeand WANTED for instruction template T. */
2165
2166 static INLINE int
2167 match_operand_size (const insn_template *t, unsigned int wanted,
2168 unsigned int given)
2169 {
2170 return !((i.types[given].bitfield.byte
2171 && !t->operand_types[wanted].bitfield.byte)
2172 || (i.types[given].bitfield.word
2173 && !t->operand_types[wanted].bitfield.word)
2174 || (i.types[given].bitfield.dword
2175 && !t->operand_types[wanted].bitfield.dword)
2176 || (i.types[given].bitfield.qword
2177 && !t->operand_types[wanted].bitfield.qword)
2178 || (i.types[given].bitfield.tbyte
2179 && !t->operand_types[wanted].bitfield.tbyte));
2180 }
2181
2182 /* Return 1 if there is no conflict in SIMD register between operand
2183 GIVEN and opeand WANTED for instruction template T. */
2184
2185 static INLINE int
2186 match_simd_size (const insn_template *t, unsigned int wanted,
2187 unsigned int given)
2188 {
2189 return !((i.types[given].bitfield.xmmword
2190 && !t->operand_types[wanted].bitfield.xmmword)
2191 || (i.types[given].bitfield.ymmword
2192 && !t->operand_types[wanted].bitfield.ymmword)
2193 || (i.types[given].bitfield.zmmword
2194 && !t->operand_types[wanted].bitfield.zmmword)
2195 || (i.types[given].bitfield.tmmword
2196 && !t->operand_types[wanted].bitfield.tmmword));
2197 }
2198
2199 /* Return 1 if there is no conflict in any size between operand GIVEN
2200 and opeand WANTED for instruction template T. */
2201
2202 static INLINE int
2203 match_mem_size (const insn_template *t, unsigned int wanted,
2204 unsigned int given)
2205 {
2206 return (match_operand_size (t, wanted, given)
2207 && !((i.types[given].bitfield.unspecified
2208 && !i.broadcast.type
2209 && !t->operand_types[wanted].bitfield.unspecified)
2210 || (i.types[given].bitfield.fword
2211 && !t->operand_types[wanted].bitfield.fword)
2212 /* For scalar opcode templates to allow register and memory
2213 operands at the same time, some special casing is needed
2214 here. Also for v{,p}broadcast*, {,v}pmov{s,z}*, and
2215 down-conversion vpmov*. */
2216 || ((t->operand_types[wanted].bitfield.class == RegSIMD
2217 && t->operand_types[wanted].bitfield.byte
2218 + t->operand_types[wanted].bitfield.word
2219 + t->operand_types[wanted].bitfield.dword
2220 + t->operand_types[wanted].bitfield.qword
2221 > !!t->opcode_modifier.broadcast)
2222 ? (i.types[given].bitfield.xmmword
2223 || i.types[given].bitfield.ymmword
2224 || i.types[given].bitfield.zmmword)
2225 : !match_simd_size(t, wanted, given))));
2226 }
2227
2228 /* Return value has MATCH_STRAIGHT set if there is no size conflict on any
2229 operands for instruction template T, and it has MATCH_REVERSE set if there
2230 is no size conflict on any operands for the template with operands reversed
2231 (and the template allows for reversing in the first place). */
2232
2233 #define MATCH_STRAIGHT 1
2234 #define MATCH_REVERSE 2
2235
2236 static INLINE unsigned int
2237 operand_size_match (const insn_template *t)
2238 {
2239 unsigned int j, match = MATCH_STRAIGHT;
2240
2241 /* Don't check non-absolute jump instructions. */
2242 if (t->opcode_modifier.jump
2243 && t->opcode_modifier.jump != JUMP_ABSOLUTE)
2244 return match;
2245
2246 /* Check memory and accumulator operand size. */
2247 for (j = 0; j < i.operands; j++)
2248 {
2249 if (i.types[j].bitfield.class != Reg
2250 && i.types[j].bitfield.class != RegSIMD
2251 && t->opcode_modifier.anysize)
2252 continue;
2253
2254 if (t->operand_types[j].bitfield.class == Reg
2255 && !match_operand_size (t, j, j))
2256 {
2257 match = 0;
2258 break;
2259 }
2260
2261 if (t->operand_types[j].bitfield.class == RegSIMD
2262 && !match_simd_size (t, j, j))
2263 {
2264 match = 0;
2265 break;
2266 }
2267
2268 if (t->operand_types[j].bitfield.instance == Accum
2269 && (!match_operand_size (t, j, j) || !match_simd_size (t, j, j)))
2270 {
2271 match = 0;
2272 break;
2273 }
2274
2275 if ((i.flags[j] & Operand_Mem) && !match_mem_size (t, j, j))
2276 {
2277 match = 0;
2278 break;
2279 }
2280 }
2281
2282 if (!t->opcode_modifier.d)
2283 {
2284 mismatch:
2285 if (!match)
2286 i.error = operand_size_mismatch;
2287 return match;
2288 }
2289
2290 /* Check reverse. */
2291 gas_assert (i.operands >= 2 && i.operands <= 3);
2292
2293 for (j = 0; j < i.operands; j++)
2294 {
2295 unsigned int given = i.operands - j - 1;
2296
2297 if (t->operand_types[j].bitfield.class == Reg
2298 && !match_operand_size (t, j, given))
2299 goto mismatch;
2300
2301 if (t->operand_types[j].bitfield.class == RegSIMD
2302 && !match_simd_size (t, j, given))
2303 goto mismatch;
2304
2305 if (t->operand_types[j].bitfield.instance == Accum
2306 && (!match_operand_size (t, j, given)
2307 || !match_simd_size (t, j, given)))
2308 goto mismatch;
2309
2310 if ((i.flags[given] & Operand_Mem) && !match_mem_size (t, j, given))
2311 goto mismatch;
2312 }
2313
2314 return match | MATCH_REVERSE;
2315 }
2316
2317 static INLINE int
2318 operand_type_match (i386_operand_type overlap,
2319 i386_operand_type given)
2320 {
2321 i386_operand_type temp = overlap;
2322
2323 temp.bitfield.unspecified = 0;
2324 temp.bitfield.byte = 0;
2325 temp.bitfield.word = 0;
2326 temp.bitfield.dword = 0;
2327 temp.bitfield.fword = 0;
2328 temp.bitfield.qword = 0;
2329 temp.bitfield.tbyte = 0;
2330 temp.bitfield.xmmword = 0;
2331 temp.bitfield.ymmword = 0;
2332 temp.bitfield.zmmword = 0;
2333 temp.bitfield.tmmword = 0;
2334 if (operand_type_all_zero (&temp))
2335 goto mismatch;
2336
2337 if (given.bitfield.baseindex == overlap.bitfield.baseindex)
2338 return 1;
2339
2340 mismatch:
2341 i.error = operand_type_mismatch;
2342 return 0;
2343 }
2344
2345 /* If given types g0 and g1 are registers they must be of the same type
2346 unless the expected operand type register overlap is null.
2347 Some Intel syntax memory operand size checking also happens here. */
2348
2349 static INLINE int
2350 operand_type_register_match (i386_operand_type g0,
2351 i386_operand_type t0,
2352 i386_operand_type g1,
2353 i386_operand_type t1)
2354 {
2355 if (g0.bitfield.class != Reg
2356 && g0.bitfield.class != RegSIMD
2357 && (!operand_type_check (g0, anymem)
2358 || g0.bitfield.unspecified
2359 || (t0.bitfield.class != Reg
2360 && t0.bitfield.class != RegSIMD)))
2361 return 1;
2362
2363 if (g1.bitfield.class != Reg
2364 && g1.bitfield.class != RegSIMD
2365 && (!operand_type_check (g1, anymem)
2366 || g1.bitfield.unspecified
2367 || (t1.bitfield.class != Reg
2368 && t1.bitfield.class != RegSIMD)))
2369 return 1;
2370
2371 if (g0.bitfield.byte == g1.bitfield.byte
2372 && g0.bitfield.word == g1.bitfield.word
2373 && g0.bitfield.dword == g1.bitfield.dword
2374 && g0.bitfield.qword == g1.bitfield.qword
2375 && g0.bitfield.xmmword == g1.bitfield.xmmword
2376 && g0.bitfield.ymmword == g1.bitfield.ymmword
2377 && g0.bitfield.zmmword == g1.bitfield.zmmword)
2378 return 1;
2379
2380 if (!(t0.bitfield.byte & t1.bitfield.byte)
2381 && !(t0.bitfield.word & t1.bitfield.word)
2382 && !(t0.bitfield.dword & t1.bitfield.dword)
2383 && !(t0.bitfield.qword & t1.bitfield.qword)
2384 && !(t0.bitfield.xmmword & t1.bitfield.xmmword)
2385 && !(t0.bitfield.ymmword & t1.bitfield.ymmword)
2386 && !(t0.bitfield.zmmword & t1.bitfield.zmmword))
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 LIOM 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 /* If we are targeting Intel L1OM, we must enable it. */
2847 if (get_elf_backend_data (stdoutput)->elf_machine_code != EM_L1OM
2848 || new_flag.bitfield.cpul1om)
2849 return;
2850
2851 /* If we are targeting Intel K1OM, we must enable it. */
2852 if (get_elf_backend_data (stdoutput)->elf_machine_code != EM_K1OM
2853 || new_flag.bitfield.cpuk1om)
2854 return;
2855
2856 as_bad (_("`%s' is not supported on `%s'"), name, arch);
2857 #endif
2858 }
2859
2860 static void
2861 set_cpu_arch (int dummy ATTRIBUTE_UNUSED)
2862 {
2863 SKIP_WHITESPACE ();
2864
2865 if (!is_end_of_line[(unsigned char) *input_line_pointer])
2866 {
2867 char *string;
2868 int e = get_symbol_name (&string);
2869 unsigned int j;
2870 i386_cpu_flags flags;
2871
2872 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
2873 {
2874 if (strcmp (string, cpu_arch[j].name) == 0)
2875 {
2876 check_cpu_arch_compatible (string, cpu_arch[j].flags);
2877
2878 if (*string != '.')
2879 {
2880 cpu_arch_name = cpu_arch[j].name;
2881 cpu_sub_arch_name = NULL;
2882 cpu_arch_flags = cpu_arch[j].flags;
2883 if (flag_code == CODE_64BIT)
2884 {
2885 cpu_arch_flags.bitfield.cpu64 = 1;
2886 cpu_arch_flags.bitfield.cpuno64 = 0;
2887 }
2888 else
2889 {
2890 cpu_arch_flags.bitfield.cpu64 = 0;
2891 cpu_arch_flags.bitfield.cpuno64 = 1;
2892 }
2893 cpu_arch_isa = cpu_arch[j].type;
2894 cpu_arch_isa_flags = cpu_arch[j].flags;
2895 if (!cpu_arch_tune_set)
2896 {
2897 cpu_arch_tune = cpu_arch_isa;
2898 cpu_arch_tune_flags = cpu_arch_isa_flags;
2899 }
2900 break;
2901 }
2902
2903 flags = cpu_flags_or (cpu_arch_flags,
2904 cpu_arch[j].flags);
2905
2906 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
2907 {
2908 if (cpu_sub_arch_name)
2909 {
2910 char *name = cpu_sub_arch_name;
2911 cpu_sub_arch_name = concat (name,
2912 cpu_arch[j].name,
2913 (const char *) NULL);
2914 free (name);
2915 }
2916 else
2917 cpu_sub_arch_name = xstrdup (cpu_arch[j].name);
2918 cpu_arch_flags = flags;
2919 cpu_arch_isa_flags = flags;
2920 }
2921 else
2922 cpu_arch_isa_flags
2923 = cpu_flags_or (cpu_arch_isa_flags,
2924 cpu_arch[j].flags);
2925 (void) restore_line_pointer (e);
2926 demand_empty_rest_of_line ();
2927 return;
2928 }
2929 }
2930
2931 if (*string == '.' && j >= ARRAY_SIZE (cpu_arch))
2932 {
2933 /* Disable an ISA extension. */
2934 for (j = 0; j < ARRAY_SIZE (cpu_noarch); j++)
2935 if (strcmp (string + 1, cpu_noarch [j].name) == 0)
2936 {
2937 flags = cpu_flags_and_not (cpu_arch_flags,
2938 cpu_noarch[j].flags);
2939 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
2940 {
2941 if (cpu_sub_arch_name)
2942 {
2943 char *name = cpu_sub_arch_name;
2944 cpu_sub_arch_name = concat (name, string,
2945 (const char *) NULL);
2946 free (name);
2947 }
2948 else
2949 cpu_sub_arch_name = xstrdup (string);
2950 cpu_arch_flags = flags;
2951 cpu_arch_isa_flags = flags;
2952 }
2953 (void) restore_line_pointer (e);
2954 demand_empty_rest_of_line ();
2955 return;
2956 }
2957
2958 j = ARRAY_SIZE (cpu_arch);
2959 }
2960
2961 if (j >= ARRAY_SIZE (cpu_arch))
2962 as_bad (_("no such architecture: `%s'"), string);
2963
2964 *input_line_pointer = e;
2965 }
2966 else
2967 as_bad (_("missing cpu architecture"));
2968
2969 no_cond_jump_promotion = 0;
2970 if (*input_line_pointer == ','
2971 && !is_end_of_line[(unsigned char) input_line_pointer[1]])
2972 {
2973 char *string;
2974 char e;
2975
2976 ++input_line_pointer;
2977 e = get_symbol_name (&string);
2978
2979 if (strcmp (string, "nojumps") == 0)
2980 no_cond_jump_promotion = 1;
2981 else if (strcmp (string, "jumps") == 0)
2982 ;
2983 else
2984 as_bad (_("no such architecture modifier: `%s'"), string);
2985
2986 (void) restore_line_pointer (e);
2987 }
2988
2989 demand_empty_rest_of_line ();
2990 }
2991
2992 enum bfd_architecture
2993 i386_arch (void)
2994 {
2995 if (cpu_arch_isa == PROCESSOR_L1OM)
2996 {
2997 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2998 || flag_code != CODE_64BIT)
2999 as_fatal (_("Intel L1OM is 64bit ELF only"));
3000 return bfd_arch_l1om;
3001 }
3002 else if (cpu_arch_isa == PROCESSOR_K1OM)
3003 {
3004 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
3005 || flag_code != CODE_64BIT)
3006 as_fatal (_("Intel K1OM is 64bit ELF only"));
3007 return bfd_arch_k1om;
3008 }
3009 else if (cpu_arch_isa == PROCESSOR_IAMCU)
3010 {
3011 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
3012 || flag_code == CODE_64BIT)
3013 as_fatal (_("Intel MCU is 32bit ELF only"));
3014 return bfd_arch_iamcu;
3015 }
3016 else
3017 return bfd_arch_i386;
3018 }
3019
3020 unsigned long
3021 i386_mach (void)
3022 {
3023 if (startswith (default_arch, "x86_64"))
3024 {
3025 if (cpu_arch_isa == PROCESSOR_L1OM)
3026 {
3027 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
3028 || default_arch[6] != '\0')
3029 as_fatal (_("Intel L1OM is 64bit ELF only"));
3030 return bfd_mach_l1om;
3031 }
3032 else if (cpu_arch_isa == PROCESSOR_K1OM)
3033 {
3034 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
3035 || default_arch[6] != '\0')
3036 as_fatal (_("Intel K1OM is 64bit ELF only"));
3037 return bfd_mach_k1om;
3038 }
3039 else if (default_arch[6] == '\0')
3040 return bfd_mach_x86_64;
3041 else
3042 return bfd_mach_x64_32;
3043 }
3044 else if (!strcmp (default_arch, "i386")
3045 || !strcmp (default_arch, "iamcu"))
3046 {
3047 if (cpu_arch_isa == PROCESSOR_IAMCU)
3048 {
3049 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
3050 as_fatal (_("Intel MCU is 32bit ELF only"));
3051 return bfd_mach_i386_iamcu;
3052 }
3053 else
3054 return bfd_mach_i386_i386;
3055 }
3056 else
3057 as_fatal (_("unknown architecture"));
3058 }
3059 \f
3060 void
3061 md_begin (void)
3062 {
3063 /* Support pseudo prefixes like {disp32}. */
3064 lex_type ['{'] = LEX_BEGIN_NAME;
3065
3066 /* Initialize op_hash hash table. */
3067 op_hash = str_htab_create ();
3068
3069 {
3070 const insn_template *optab;
3071 templates *core_optab;
3072
3073 /* Setup for loop. */
3074 optab = i386_optab;
3075 core_optab = XNEW (templates);
3076 core_optab->start = optab;
3077
3078 while (1)
3079 {
3080 ++optab;
3081 if (optab->name == NULL
3082 || strcmp (optab->name, (optab - 1)->name) != 0)
3083 {
3084 /* different name --> ship out current template list;
3085 add to hash table; & begin anew. */
3086 core_optab->end = optab;
3087 if (str_hash_insert (op_hash, (optab - 1)->name, core_optab, 0))
3088 as_fatal (_("duplicate %s"), (optab - 1)->name);
3089
3090 if (optab->name == NULL)
3091 break;
3092 core_optab = XNEW (templates);
3093 core_optab->start = optab;
3094 }
3095 }
3096 }
3097
3098 /* Initialize reg_hash hash table. */
3099 reg_hash = str_htab_create ();
3100 {
3101 const reg_entry *regtab;
3102 unsigned int regtab_size = i386_regtab_size;
3103
3104 for (regtab = i386_regtab; regtab_size--; regtab++)
3105 {
3106 switch (regtab->reg_type.bitfield.class)
3107 {
3108 case Reg:
3109 if (regtab->reg_type.bitfield.dword)
3110 {
3111 if (regtab->reg_type.bitfield.instance == Accum)
3112 reg_eax = regtab;
3113 }
3114 else if (regtab->reg_type.bitfield.tbyte)
3115 {
3116 /* There's no point inserting st(<N>) in the hash table, as
3117 parentheses aren't included in register_chars[] anyway. */
3118 if (regtab->reg_type.bitfield.instance != Accum)
3119 continue;
3120 reg_st0 = regtab;
3121 }
3122 break;
3123
3124 case SReg:
3125 switch (regtab->reg_num)
3126 {
3127 case 0: reg_es = regtab; break;
3128 case 2: reg_ss = regtab; break;
3129 case 3: reg_ds = regtab; break;
3130 }
3131 break;
3132
3133 case RegMask:
3134 if (!regtab->reg_num)
3135 reg_k0 = regtab;
3136 break;
3137 }
3138
3139 if (str_hash_insert (reg_hash, regtab->reg_name, regtab, 0) != NULL)
3140 as_fatal (_("duplicate %s"), regtab->reg_name);
3141 }
3142 }
3143
3144 /* Fill in lexical tables: mnemonic_chars, operand_chars. */
3145 {
3146 int c;
3147 char *p;
3148
3149 for (c = 0; c < 256; c++)
3150 {
3151 if (ISDIGIT (c) || ISLOWER (c))
3152 {
3153 mnemonic_chars[c] = c;
3154 register_chars[c] = c;
3155 operand_chars[c] = c;
3156 }
3157 else if (ISUPPER (c))
3158 {
3159 mnemonic_chars[c] = TOLOWER (c);
3160 register_chars[c] = mnemonic_chars[c];
3161 operand_chars[c] = c;
3162 }
3163 else if (c == '{' || c == '}')
3164 {
3165 mnemonic_chars[c] = c;
3166 operand_chars[c] = c;
3167 }
3168 #ifdef SVR4_COMMENT_CHARS
3169 else if (c == '\\' && strchr (i386_comment_chars, '/'))
3170 operand_chars[c] = c;
3171 #endif
3172
3173 if (ISALPHA (c) || ISDIGIT (c))
3174 identifier_chars[c] = c;
3175 else if (c >= 128)
3176 {
3177 identifier_chars[c] = c;
3178 operand_chars[c] = c;
3179 }
3180 }
3181
3182 #ifdef LEX_AT
3183 identifier_chars['@'] = '@';
3184 #endif
3185 #ifdef LEX_QM
3186 identifier_chars['?'] = '?';
3187 operand_chars['?'] = '?';
3188 #endif
3189 mnemonic_chars['_'] = '_';
3190 mnemonic_chars['-'] = '-';
3191 mnemonic_chars['.'] = '.';
3192 identifier_chars['_'] = '_';
3193 identifier_chars['.'] = '.';
3194
3195 for (p = operand_special_chars; *p != '\0'; p++)
3196 operand_chars[(unsigned char) *p] = *p;
3197 }
3198
3199 if (flag_code == CODE_64BIT)
3200 {
3201 #if defined (OBJ_COFF) && defined (TE_PE)
3202 x86_dwarf2_return_column = (OUTPUT_FLAVOR == bfd_target_coff_flavour
3203 ? 32 : 16);
3204 #else
3205 x86_dwarf2_return_column = 16;
3206 #endif
3207 x86_cie_data_alignment = -8;
3208 }
3209 else
3210 {
3211 x86_dwarf2_return_column = 8;
3212 x86_cie_data_alignment = -4;
3213 }
3214
3215 /* NB: FUSED_JCC_PADDING frag must have sufficient room so that it
3216 can be turned into BRANCH_PREFIX frag. */
3217 if (align_branch_prefix_size > MAX_FUSED_JCC_PADDING_SIZE)
3218 abort ();
3219 }
3220
3221 void
3222 i386_print_statistics (FILE *file)
3223 {
3224 htab_print_statistics (file, "i386 opcode", op_hash);
3225 htab_print_statistics (file, "i386 register", reg_hash);
3226 }
3227 \f
3228 #ifdef DEBUG386
3229
3230 /* Debugging routines for md_assemble. */
3231 static void pte (insn_template *);
3232 static void pt (i386_operand_type);
3233 static void pe (expressionS *);
3234 static void ps (symbolS *);
3235
3236 static void
3237 pi (const char *line, i386_insn *x)
3238 {
3239 unsigned int j;
3240
3241 fprintf (stdout, "%s: template ", line);
3242 pte (&x->tm);
3243 fprintf (stdout, " address: base %s index %s scale %x\n",
3244 x->base_reg ? x->base_reg->reg_name : "none",
3245 x->index_reg ? x->index_reg->reg_name : "none",
3246 x->log2_scale_factor);
3247 fprintf (stdout, " modrm: mode %x reg %x reg/mem %x\n",
3248 x->rm.mode, x->rm.reg, x->rm.regmem);
3249 fprintf (stdout, " sib: base %x index %x scale %x\n",
3250 x->sib.base, x->sib.index, x->sib.scale);
3251 fprintf (stdout, " rex: 64bit %x extX %x extY %x extZ %x\n",
3252 (x->rex & REX_W) != 0,
3253 (x->rex & REX_R) != 0,
3254 (x->rex & REX_X) != 0,
3255 (x->rex & REX_B) != 0);
3256 for (j = 0; j < x->operands; j++)
3257 {
3258 fprintf (stdout, " #%d: ", j + 1);
3259 pt (x->types[j]);
3260 fprintf (stdout, "\n");
3261 if (x->types[j].bitfield.class == Reg
3262 || x->types[j].bitfield.class == RegMMX
3263 || x->types[j].bitfield.class == RegSIMD
3264 || x->types[j].bitfield.class == RegMask
3265 || x->types[j].bitfield.class == SReg
3266 || x->types[j].bitfield.class == RegCR
3267 || x->types[j].bitfield.class == RegDR
3268 || x->types[j].bitfield.class == RegTR
3269 || x->types[j].bitfield.class == RegBND)
3270 fprintf (stdout, "%s\n", x->op[j].regs->reg_name);
3271 if (operand_type_check (x->types[j], imm))
3272 pe (x->op[j].imms);
3273 if (operand_type_check (x->types[j], disp))
3274 pe (x->op[j].disps);
3275 }
3276 }
3277
3278 static void
3279 pte (insn_template *t)
3280 {
3281 static const unsigned char opc_pfx[] = { 0, 0x66, 0xf3, 0xf2 };
3282 static const char *const opc_spc[] = {
3283 NULL, "0f", "0f38", "0f3a", NULL, "evexmap5", "evexmap6", NULL,
3284 "XOP08", "XOP09", "XOP0A",
3285 };
3286 unsigned int j;
3287
3288 fprintf (stdout, " %d operands ", t->operands);
3289 if (opc_pfx[t->opcode_modifier.opcodeprefix])
3290 fprintf (stdout, "pfx %x ", opc_pfx[t->opcode_modifier.opcodeprefix]);
3291 if (opc_spc[t->opcode_modifier.opcodespace])
3292 fprintf (stdout, "space %s ", opc_spc[t->opcode_modifier.opcodespace]);
3293 fprintf (stdout, "opcode %x ", t->base_opcode);
3294 if (t->extension_opcode != None)
3295 fprintf (stdout, "ext %x ", t->extension_opcode);
3296 if (t->opcode_modifier.d)
3297 fprintf (stdout, "D");
3298 if (t->opcode_modifier.w)
3299 fprintf (stdout, "W");
3300 fprintf (stdout, "\n");
3301 for (j = 0; j < t->operands; j++)
3302 {
3303 fprintf (stdout, " #%d type ", j + 1);
3304 pt (t->operand_types[j]);
3305 fprintf (stdout, "\n");
3306 }
3307 }
3308
3309 static void
3310 pe (expressionS *e)
3311 {
3312 fprintf (stdout, " operation %d\n", e->X_op);
3313 fprintf (stdout, " add_number %" BFD_VMA_FMT "d (%" BFD_VMA_FMT "x)\n",
3314 e->X_add_number, e->X_add_number);
3315 if (e->X_add_symbol)
3316 {
3317 fprintf (stdout, " add_symbol ");
3318 ps (e->X_add_symbol);
3319 fprintf (stdout, "\n");
3320 }
3321 if (e->X_op_symbol)
3322 {
3323 fprintf (stdout, " op_symbol ");
3324 ps (e->X_op_symbol);
3325 fprintf (stdout, "\n");
3326 }
3327 }
3328
3329 static void
3330 ps (symbolS *s)
3331 {
3332 fprintf (stdout, "%s type %s%s",
3333 S_GET_NAME (s),
3334 S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
3335 segment_name (S_GET_SEGMENT (s)));
3336 }
3337
3338 static struct type_name
3339 {
3340 i386_operand_type mask;
3341 const char *name;
3342 }
3343 const type_names[] =
3344 {
3345 { OPERAND_TYPE_REG8, "r8" },
3346 { OPERAND_TYPE_REG16, "r16" },
3347 { OPERAND_TYPE_REG32, "r32" },
3348 { OPERAND_TYPE_REG64, "r64" },
3349 { OPERAND_TYPE_ACC8, "acc8" },
3350 { OPERAND_TYPE_ACC16, "acc16" },
3351 { OPERAND_TYPE_ACC32, "acc32" },
3352 { OPERAND_TYPE_ACC64, "acc64" },
3353 { OPERAND_TYPE_IMM8, "i8" },
3354 { OPERAND_TYPE_IMM8, "i8s" },
3355 { OPERAND_TYPE_IMM16, "i16" },
3356 { OPERAND_TYPE_IMM32, "i32" },
3357 { OPERAND_TYPE_IMM32S, "i32s" },
3358 { OPERAND_TYPE_IMM64, "i64" },
3359 { OPERAND_TYPE_IMM1, "i1" },
3360 { OPERAND_TYPE_BASEINDEX, "BaseIndex" },
3361 { OPERAND_TYPE_DISP8, "d8" },
3362 { OPERAND_TYPE_DISP16, "d16" },
3363 { OPERAND_TYPE_DISP32, "d32" },
3364 { OPERAND_TYPE_DISP32S, "d32s" },
3365 { OPERAND_TYPE_DISP64, "d64" },
3366 { OPERAND_TYPE_INOUTPORTREG, "InOutPortReg" },
3367 { OPERAND_TYPE_SHIFTCOUNT, "ShiftCount" },
3368 { OPERAND_TYPE_CONTROL, "control reg" },
3369 { OPERAND_TYPE_TEST, "test reg" },
3370 { OPERAND_TYPE_DEBUG, "debug reg" },
3371 { OPERAND_TYPE_FLOATREG, "FReg" },
3372 { OPERAND_TYPE_FLOATACC, "FAcc" },
3373 { OPERAND_TYPE_SREG, "SReg" },
3374 { OPERAND_TYPE_REGMMX, "rMMX" },
3375 { OPERAND_TYPE_REGXMM, "rXMM" },
3376 { OPERAND_TYPE_REGYMM, "rYMM" },
3377 { OPERAND_TYPE_REGZMM, "rZMM" },
3378 { OPERAND_TYPE_REGTMM, "rTMM" },
3379 { OPERAND_TYPE_REGMASK, "Mask reg" },
3380 };
3381
3382 static void
3383 pt (i386_operand_type t)
3384 {
3385 unsigned int j;
3386 i386_operand_type a;
3387
3388 for (j = 0; j < ARRAY_SIZE (type_names); j++)
3389 {
3390 a = operand_type_and (t, type_names[j].mask);
3391 if (operand_type_equal (&a, &type_names[j].mask))
3392 fprintf (stdout, "%s, ", type_names[j].name);
3393 }
3394 fflush (stdout);
3395 }
3396
3397 #endif /* DEBUG386 */
3398 \f
3399 static bfd_reloc_code_real_type
3400 reloc (unsigned int size,
3401 int pcrel,
3402 int sign,
3403 bfd_reloc_code_real_type other)
3404 {
3405 if (other != NO_RELOC)
3406 {
3407 reloc_howto_type *rel;
3408
3409 if (size == 8)
3410 switch (other)
3411 {
3412 case BFD_RELOC_X86_64_GOT32:
3413 return BFD_RELOC_X86_64_GOT64;
3414 break;
3415 case BFD_RELOC_X86_64_GOTPLT64:
3416 return BFD_RELOC_X86_64_GOTPLT64;
3417 break;
3418 case BFD_RELOC_X86_64_PLTOFF64:
3419 return BFD_RELOC_X86_64_PLTOFF64;
3420 break;
3421 case BFD_RELOC_X86_64_GOTPC32:
3422 other = BFD_RELOC_X86_64_GOTPC64;
3423 break;
3424 case BFD_RELOC_X86_64_GOTPCREL:
3425 other = BFD_RELOC_X86_64_GOTPCREL64;
3426 break;
3427 case BFD_RELOC_X86_64_TPOFF32:
3428 other = BFD_RELOC_X86_64_TPOFF64;
3429 break;
3430 case BFD_RELOC_X86_64_DTPOFF32:
3431 other = BFD_RELOC_X86_64_DTPOFF64;
3432 break;
3433 default:
3434 break;
3435 }
3436
3437 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3438 if (other == BFD_RELOC_SIZE32)
3439 {
3440 if (size == 8)
3441 other = BFD_RELOC_SIZE64;
3442 if (pcrel)
3443 {
3444 as_bad (_("there are no pc-relative size relocations"));
3445 return NO_RELOC;
3446 }
3447 }
3448 #endif
3449
3450 /* Sign-checking 4-byte relocations in 16-/32-bit code is pointless. */
3451 if (size == 4 && (flag_code != CODE_64BIT || disallow_64bit_reloc))
3452 sign = -1;
3453
3454 rel = bfd_reloc_type_lookup (stdoutput, other);
3455 if (!rel)
3456 as_bad (_("unknown relocation (%u)"), other);
3457 else if (size != bfd_get_reloc_size (rel))
3458 as_bad (_("%u-byte relocation cannot be applied to %u-byte field"),
3459 bfd_get_reloc_size (rel),
3460 size);
3461 else if (pcrel && !rel->pc_relative)
3462 as_bad (_("non-pc-relative relocation for pc-relative field"));
3463 else if ((rel->complain_on_overflow == complain_overflow_signed
3464 && !sign)
3465 || (rel->complain_on_overflow == complain_overflow_unsigned
3466 && sign > 0))
3467 as_bad (_("relocated field and relocation type differ in signedness"));
3468 else
3469 return other;
3470 return NO_RELOC;
3471 }
3472
3473 if (pcrel)
3474 {
3475 if (!sign)
3476 as_bad (_("there are no unsigned pc-relative relocations"));
3477 switch (size)
3478 {
3479 case 1: return BFD_RELOC_8_PCREL;
3480 case 2: return BFD_RELOC_16_PCREL;
3481 case 4: return BFD_RELOC_32_PCREL;
3482 case 8: return BFD_RELOC_64_PCREL;
3483 }
3484 as_bad (_("cannot do %u byte pc-relative relocation"), size);
3485 }
3486 else
3487 {
3488 if (sign > 0)
3489 switch (size)
3490 {
3491 case 4: return BFD_RELOC_X86_64_32S;
3492 }
3493 else
3494 switch (size)
3495 {
3496 case 1: return BFD_RELOC_8;
3497 case 2: return BFD_RELOC_16;
3498 case 4: return BFD_RELOC_32;
3499 case 8: return BFD_RELOC_64;
3500 }
3501 as_bad (_("cannot do %s %u byte relocation"),
3502 sign > 0 ? "signed" : "unsigned", size);
3503 }
3504
3505 return NO_RELOC;
3506 }
3507
3508 /* Here we decide which fixups can be adjusted to make them relative to
3509 the beginning of the section instead of the symbol. Basically we need
3510 to make sure that the dynamic relocations are done correctly, so in
3511 some cases we force the original symbol to be used. */
3512
3513 int
3514 tc_i386_fix_adjustable (fixS *fixP ATTRIBUTE_UNUSED)
3515 {
3516 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3517 if (!IS_ELF)
3518 return 1;
3519
3520 /* Don't adjust pc-relative references to merge sections in 64-bit
3521 mode. */
3522 if (use_rela_relocations
3523 && (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0
3524 && fixP->fx_pcrel)
3525 return 0;
3526
3527 /* The x86_64 GOTPCREL are represented as 32bit PCrel relocations
3528 and changed later by validate_fix. */
3529 if (GOT_symbol && fixP->fx_subsy == GOT_symbol
3530 && fixP->fx_r_type == BFD_RELOC_32_PCREL)
3531 return 0;
3532
3533 /* Adjust_reloc_syms doesn't know about the GOT. Need to keep symbol
3534 for size relocations. */
3535 if (fixP->fx_r_type == BFD_RELOC_SIZE32
3536 || fixP->fx_r_type == BFD_RELOC_SIZE64
3537 || fixP->fx_r_type == BFD_RELOC_386_GOTOFF
3538 || fixP->fx_r_type == BFD_RELOC_386_GOT32
3539 || fixP->fx_r_type == BFD_RELOC_386_GOT32X
3540 || fixP->fx_r_type == BFD_RELOC_386_TLS_GD
3541 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDM
3542 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDO_32
3543 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE_32
3544 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE
3545 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTIE
3546 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE_32
3547 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE
3548 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTDESC
3549 || fixP->fx_r_type == BFD_RELOC_386_TLS_DESC_CALL
3550 || fixP->fx_r_type == BFD_RELOC_X86_64_GOT32
3551 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCREL
3552 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCRELX
3553 || fixP->fx_r_type == BFD_RELOC_X86_64_REX_GOTPCRELX
3554 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSGD
3555 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSLD
3556 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF32
3557 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF64
3558 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF
3559 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF32
3560 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF64
3561 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTOFF64
3562 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
3563 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSDESC_CALL
3564 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3565 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3566 return 0;
3567 #endif
3568 return 1;
3569 }
3570
3571 static INLINE bool
3572 want_disp32 (const insn_template *t)
3573 {
3574 return flag_code != CODE_64BIT
3575 || i.prefix[ADDR_PREFIX]
3576 || (t->base_opcode == 0x8d
3577 && t->opcode_modifier.opcodespace == SPACE_BASE
3578 && (!i.types[1].bitfield.qword
3579 || t->opcode_modifier.size == SIZE32));
3580 }
3581
3582 static int
3583 intel_float_operand (const char *mnemonic)
3584 {
3585 /* Note that the value returned is meaningful only for opcodes with (memory)
3586 operands, hence the code here is free to improperly handle opcodes that
3587 have no operands (for better performance and smaller code). */
3588
3589 if (mnemonic[0] != 'f')
3590 return 0; /* non-math */
3591
3592 switch (mnemonic[1])
3593 {
3594 /* fclex, fdecstp, fdisi, femms, feni, fincstp, finit, fsetpm, and
3595 the fs segment override prefix not currently handled because no
3596 call path can make opcodes without operands get here */
3597 case 'i':
3598 return 2 /* integer op */;
3599 case 'l':
3600 if (mnemonic[2] == 'd' && (mnemonic[3] == 'c' || mnemonic[3] == 'e'))
3601 return 3; /* fldcw/fldenv */
3602 break;
3603 case 'n':
3604 if (mnemonic[2] != 'o' /* fnop */)
3605 return 3; /* non-waiting control op */
3606 break;
3607 case 'r':
3608 if (mnemonic[2] == 's')
3609 return 3; /* frstor/frstpm */
3610 break;
3611 case 's':
3612 if (mnemonic[2] == 'a')
3613 return 3; /* fsave */
3614 if (mnemonic[2] == 't')
3615 {
3616 switch (mnemonic[3])
3617 {
3618 case 'c': /* fstcw */
3619 case 'd': /* fstdw */
3620 case 'e': /* fstenv */
3621 case 's': /* fsts[gw] */
3622 return 3;
3623 }
3624 }
3625 break;
3626 case 'x':
3627 if (mnemonic[2] == 'r' || mnemonic[2] == 's')
3628 return 0; /* fxsave/fxrstor are not really math ops */
3629 break;
3630 }
3631
3632 return 1;
3633 }
3634
3635 static INLINE void
3636 install_template (const insn_template *t)
3637 {
3638 unsigned int l;
3639
3640 i.tm = *t;
3641
3642 /* Note that for pseudo prefixes this produces a length of 1. But for them
3643 the length isn't interesting at all. */
3644 for (l = 1; l < 4; ++l)
3645 if (!(t->base_opcode >> (8 * l)))
3646 break;
3647
3648 i.opcode_length = l;
3649 }
3650
3651 /* Build the VEX prefix. */
3652
3653 static void
3654 build_vex_prefix (const insn_template *t)
3655 {
3656 unsigned int register_specifier;
3657 unsigned int vector_length;
3658 unsigned int w;
3659
3660 /* Check register specifier. */
3661 if (i.vex.register_specifier)
3662 {
3663 register_specifier =
3664 ~register_number (i.vex.register_specifier) & 0xf;
3665 gas_assert ((i.vex.register_specifier->reg_flags & RegVRex) == 0);
3666 }
3667 else
3668 register_specifier = 0xf;
3669
3670 /* Use 2-byte VEX prefix by swapping destination and source operand
3671 if there are more than 1 register operand. */
3672 if (i.reg_operands > 1
3673 && i.vec_encoding != vex_encoding_vex3
3674 && i.dir_encoding == dir_encoding_default
3675 && i.operands == i.reg_operands
3676 && operand_type_equal (&i.types[0], &i.types[i.operands - 1])
3677 && i.tm.opcode_modifier.opcodespace == SPACE_0F
3678 && (i.tm.opcode_modifier.load || i.tm.opcode_modifier.d)
3679 && i.rex == REX_B)
3680 {
3681 unsigned int xchg = i.operands - 1;
3682 union i386_op temp_op;
3683 i386_operand_type temp_type;
3684
3685 temp_type = i.types[xchg];
3686 i.types[xchg] = i.types[0];
3687 i.types[0] = temp_type;
3688 temp_op = i.op[xchg];
3689 i.op[xchg] = i.op[0];
3690 i.op[0] = temp_op;
3691
3692 gas_assert (i.rm.mode == 3);
3693
3694 i.rex = REX_R;
3695 xchg = i.rm.regmem;
3696 i.rm.regmem = i.rm.reg;
3697 i.rm.reg = xchg;
3698
3699 if (i.tm.opcode_modifier.d)
3700 i.tm.base_opcode ^= (i.tm.base_opcode & 0xee) != 0x6e
3701 ? Opcode_SIMD_FloatD : Opcode_SIMD_IntD;
3702 else /* Use the next insn. */
3703 install_template (&t[1]);
3704 }
3705
3706 /* Use 2-byte VEX prefix by swapping commutative source operands if there
3707 are no memory operands and at least 3 register ones. */
3708 if (i.reg_operands >= 3
3709 && i.vec_encoding != vex_encoding_vex3
3710 && i.reg_operands == i.operands - i.imm_operands
3711 && i.tm.opcode_modifier.vex
3712 && i.tm.opcode_modifier.commutative
3713 && (i.tm.opcode_modifier.sse2avx || optimize > 1)
3714 && i.rex == REX_B
3715 && i.vex.register_specifier
3716 && !(i.vex.register_specifier->reg_flags & RegRex))
3717 {
3718 unsigned int xchg = i.operands - i.reg_operands;
3719 union i386_op temp_op;
3720 i386_operand_type temp_type;
3721
3722 gas_assert (i.tm.opcode_modifier.opcodespace == SPACE_0F);
3723 gas_assert (!i.tm.opcode_modifier.sae);
3724 gas_assert (operand_type_equal (&i.types[i.operands - 2],
3725 &i.types[i.operands - 3]));
3726 gas_assert (i.rm.mode == 3);
3727
3728 temp_type = i.types[xchg];
3729 i.types[xchg] = i.types[xchg + 1];
3730 i.types[xchg + 1] = temp_type;
3731 temp_op = i.op[xchg];
3732 i.op[xchg] = i.op[xchg + 1];
3733 i.op[xchg + 1] = temp_op;
3734
3735 i.rex = 0;
3736 xchg = i.rm.regmem | 8;
3737 i.rm.regmem = ~register_specifier & 0xf;
3738 gas_assert (!(i.rm.regmem & 8));
3739 i.vex.register_specifier += xchg - i.rm.regmem;
3740 register_specifier = ~xchg & 0xf;
3741 }
3742
3743 if (i.tm.opcode_modifier.vex == VEXScalar)
3744 vector_length = avxscalar;
3745 else if (i.tm.opcode_modifier.vex == VEX256)
3746 vector_length = 1;
3747 else
3748 {
3749 unsigned int op;
3750
3751 /* Determine vector length from the last multi-length vector
3752 operand. */
3753 vector_length = 0;
3754 for (op = t->operands; op--;)
3755 if (t->operand_types[op].bitfield.xmmword
3756 && t->operand_types[op].bitfield.ymmword
3757 && i.types[op].bitfield.ymmword)
3758 {
3759 vector_length = 1;
3760 break;
3761 }
3762 }
3763
3764 /* Check the REX.W bit and VEXW. */
3765 if (i.tm.opcode_modifier.vexw == VEXWIG)
3766 w = (vexwig == vexw1 || (i.rex & REX_W)) ? 1 : 0;
3767 else if (i.tm.opcode_modifier.vexw)
3768 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3769 else
3770 w = (flag_code == CODE_64BIT ? i.rex & REX_W : vexwig == vexw1) ? 1 : 0;
3771
3772 /* Use 2-byte VEX prefix if possible. */
3773 if (w == 0
3774 && i.vec_encoding != vex_encoding_vex3
3775 && i.tm.opcode_modifier.opcodespace == SPACE_0F
3776 && (i.rex & (REX_W | REX_X | REX_B)) == 0)
3777 {
3778 /* 2-byte VEX prefix. */
3779 unsigned int r;
3780
3781 i.vex.length = 2;
3782 i.vex.bytes[0] = 0xc5;
3783
3784 /* Check the REX.R bit. */
3785 r = (i.rex & REX_R) ? 0 : 1;
3786 i.vex.bytes[1] = (r << 7
3787 | register_specifier << 3
3788 | vector_length << 2
3789 | i.tm.opcode_modifier.opcodeprefix);
3790 }
3791 else
3792 {
3793 /* 3-byte VEX prefix. */
3794 i.vex.length = 3;
3795
3796 switch (i.tm.opcode_modifier.opcodespace)
3797 {
3798 case SPACE_0F:
3799 case SPACE_0F38:
3800 case SPACE_0F3A:
3801 i.vex.bytes[0] = 0xc4;
3802 break;
3803 case SPACE_XOP08:
3804 case SPACE_XOP09:
3805 case SPACE_XOP0A:
3806 i.vex.bytes[0] = 0x8f;
3807 break;
3808 default:
3809 abort ();
3810 }
3811
3812 /* The high 3 bits of the second VEX byte are 1's compliment
3813 of RXB bits from REX. */
3814 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | i.tm.opcode_modifier.opcodespace;
3815
3816 i.vex.bytes[2] = (w << 7
3817 | register_specifier << 3
3818 | vector_length << 2
3819 | i.tm.opcode_modifier.opcodeprefix);
3820 }
3821 }
3822
3823 static INLINE bool
3824 is_evex_encoding (const insn_template *t)
3825 {
3826 return t->opcode_modifier.evex || t->opcode_modifier.disp8memshift
3827 || t->opcode_modifier.broadcast || t->opcode_modifier.masking
3828 || t->opcode_modifier.sae;
3829 }
3830
3831 static INLINE bool
3832 is_any_vex_encoding (const insn_template *t)
3833 {
3834 return t->opcode_modifier.vex || is_evex_encoding (t);
3835 }
3836
3837 /* Build the EVEX prefix. */
3838
3839 static void
3840 build_evex_prefix (void)
3841 {
3842 unsigned int register_specifier, w;
3843 rex_byte vrex_used = 0;
3844
3845 /* Check register specifier. */
3846 if (i.vex.register_specifier)
3847 {
3848 gas_assert ((i.vrex & REX_X) == 0);
3849
3850 register_specifier = i.vex.register_specifier->reg_num;
3851 if ((i.vex.register_specifier->reg_flags & RegRex))
3852 register_specifier += 8;
3853 /* The upper 16 registers are encoded in the fourth byte of the
3854 EVEX prefix. */
3855 if (!(i.vex.register_specifier->reg_flags & RegVRex))
3856 i.vex.bytes[3] = 0x8;
3857 register_specifier = ~register_specifier & 0xf;
3858 }
3859 else
3860 {
3861 register_specifier = 0xf;
3862
3863 /* Encode upper 16 vector index register in the fourth byte of
3864 the EVEX prefix. */
3865 if (!(i.vrex & REX_X))
3866 i.vex.bytes[3] = 0x8;
3867 else
3868 vrex_used |= REX_X;
3869 }
3870
3871 /* 4 byte EVEX prefix. */
3872 i.vex.length = 4;
3873 i.vex.bytes[0] = 0x62;
3874
3875 /* The high 3 bits of the second EVEX byte are 1's compliment of RXB
3876 bits from REX. */
3877 gas_assert (i.tm.opcode_modifier.opcodespace >= SPACE_0F);
3878 gas_assert (i.tm.opcode_modifier.opcodespace <= SPACE_EVEXMAP6);
3879 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | i.tm.opcode_modifier.opcodespace;
3880
3881 /* The fifth bit of the second EVEX byte is 1's compliment of the
3882 REX_R bit in VREX. */
3883 if (!(i.vrex & REX_R))
3884 i.vex.bytes[1] |= 0x10;
3885 else
3886 vrex_used |= REX_R;
3887
3888 if ((i.reg_operands + i.imm_operands) == i.operands)
3889 {
3890 /* When all operands are registers, the REX_X bit in REX is not
3891 used. We reuse it to encode the upper 16 registers, which is
3892 indicated by the REX_B bit in VREX. The REX_X bit is encoded
3893 as 1's compliment. */
3894 if ((i.vrex & REX_B))
3895 {
3896 vrex_used |= REX_B;
3897 i.vex.bytes[1] &= ~0x40;
3898 }
3899 }
3900
3901 /* EVEX instructions shouldn't need the REX prefix. */
3902 i.vrex &= ~vrex_used;
3903 gas_assert (i.vrex == 0);
3904
3905 /* Check the REX.W bit and VEXW. */
3906 if (i.tm.opcode_modifier.vexw == VEXWIG)
3907 w = (evexwig == evexw1 || (i.rex & REX_W)) ? 1 : 0;
3908 else if (i.tm.opcode_modifier.vexw)
3909 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3910 else
3911 w = (flag_code == CODE_64BIT ? i.rex & REX_W : evexwig == evexw1) ? 1 : 0;
3912
3913 /* The third byte of the EVEX prefix. */
3914 i.vex.bytes[2] = ((w << 7)
3915 | (register_specifier << 3)
3916 | 4 /* Encode the U bit. */
3917 | i.tm.opcode_modifier.opcodeprefix);
3918
3919 /* The fourth byte of the EVEX prefix. */
3920 /* The zeroing-masking bit. */
3921 if (i.mask.reg && i.mask.zeroing)
3922 i.vex.bytes[3] |= 0x80;
3923
3924 /* Don't always set the broadcast bit if there is no RC. */
3925 if (i.rounding.type == rc_none)
3926 {
3927 /* Encode the vector length. */
3928 unsigned int vec_length;
3929
3930 if (!i.tm.opcode_modifier.evex
3931 || i.tm.opcode_modifier.evex == EVEXDYN)
3932 {
3933 unsigned int op;
3934
3935 /* Determine vector length from the last multi-length vector
3936 operand. */
3937 for (op = i.operands; op--;)
3938 if (i.tm.operand_types[op].bitfield.xmmword
3939 + i.tm.operand_types[op].bitfield.ymmword
3940 + i.tm.operand_types[op].bitfield.zmmword > 1)
3941 {
3942 if (i.types[op].bitfield.zmmword)
3943 {
3944 i.tm.opcode_modifier.evex = EVEX512;
3945 break;
3946 }
3947 else if (i.types[op].bitfield.ymmword)
3948 {
3949 i.tm.opcode_modifier.evex = EVEX256;
3950 break;
3951 }
3952 else if (i.types[op].bitfield.xmmword)
3953 {
3954 i.tm.opcode_modifier.evex = EVEX128;
3955 break;
3956 }
3957 else if (i.broadcast.type && op == i.broadcast.operand)
3958 {
3959 switch (i.broadcast.bytes)
3960 {
3961 case 64:
3962 i.tm.opcode_modifier.evex = EVEX512;
3963 break;
3964 case 32:
3965 i.tm.opcode_modifier.evex = EVEX256;
3966 break;
3967 case 16:
3968 i.tm.opcode_modifier.evex = EVEX128;
3969 break;
3970 default:
3971 abort ();
3972 }
3973 break;
3974 }
3975 }
3976
3977 if (op >= MAX_OPERANDS)
3978 abort ();
3979 }
3980
3981 switch (i.tm.opcode_modifier.evex)
3982 {
3983 case EVEXLIG: /* LL' is ignored */
3984 vec_length = evexlig << 5;
3985 break;
3986 case EVEX128:
3987 vec_length = 0 << 5;
3988 break;
3989 case EVEX256:
3990 vec_length = 1 << 5;
3991 break;
3992 case EVEX512:
3993 vec_length = 2 << 5;
3994 break;
3995 default:
3996 abort ();
3997 break;
3998 }
3999 i.vex.bytes[3] |= vec_length;
4000 /* Encode the broadcast bit. */
4001 if (i.broadcast.type)
4002 i.vex.bytes[3] |= 0x10;
4003 }
4004 else if (i.rounding.type != saeonly)
4005 i.vex.bytes[3] |= 0x10 | (i.rounding.type << 5);
4006 else
4007 i.vex.bytes[3] |= 0x10 | (evexrcig << 5);
4008
4009 if (i.mask.reg)
4010 i.vex.bytes[3] |= i.mask.reg->reg_num;
4011 }
4012
4013 static void
4014 process_immext (void)
4015 {
4016 expressionS *exp;
4017
4018 /* These AMD 3DNow! and SSE2 instructions have an opcode suffix
4019 which is coded in the same place as an 8-bit immediate field
4020 would be. Here we fake an 8-bit immediate operand from the
4021 opcode suffix stored in tm.extension_opcode.
4022
4023 AVX instructions also use this encoding, for some of
4024 3 argument instructions. */
4025
4026 gas_assert (i.imm_operands <= 1
4027 && (i.operands <= 2
4028 || (is_any_vex_encoding (&i.tm)
4029 && i.operands <= 4)));
4030
4031 exp = &im_expressions[i.imm_operands++];
4032 i.op[i.operands].imms = exp;
4033 i.types[i.operands] = imm8;
4034 i.operands++;
4035 exp->X_op = O_constant;
4036 exp->X_add_number = i.tm.extension_opcode;
4037 i.tm.extension_opcode = None;
4038 }
4039
4040
4041 static int
4042 check_hle (void)
4043 {
4044 switch (i.tm.opcode_modifier.prefixok)
4045 {
4046 default:
4047 abort ();
4048 case PrefixLock:
4049 case PrefixNone:
4050 case PrefixNoTrack:
4051 case PrefixRep:
4052 as_bad (_("invalid instruction `%s' after `%s'"),
4053 i.tm.name, i.hle_prefix);
4054 return 0;
4055 case PrefixHLELock:
4056 if (i.prefix[LOCK_PREFIX])
4057 return 1;
4058 as_bad (_("missing `lock' with `%s'"), i.hle_prefix);
4059 return 0;
4060 case PrefixHLEAny:
4061 return 1;
4062 case PrefixHLERelease:
4063 if (i.prefix[HLE_PREFIX] != XRELEASE_PREFIX_OPCODE)
4064 {
4065 as_bad (_("instruction `%s' after `xacquire' not allowed"),
4066 i.tm.name);
4067 return 0;
4068 }
4069 if (i.mem_operands == 0 || !(i.flags[i.operands - 1] & Operand_Mem))
4070 {
4071 as_bad (_("memory destination needed for instruction `%s'"
4072 " after `xrelease'"), i.tm.name);
4073 return 0;
4074 }
4075 return 1;
4076 }
4077 }
4078
4079 /* Encode aligned vector move as unaligned vector move. */
4080
4081 static void
4082 encode_with_unaligned_vector_move (void)
4083 {
4084 switch (i.tm.base_opcode)
4085 {
4086 case 0x28: /* Load instructions. */
4087 case 0x29: /* Store instructions. */
4088 /* movaps/movapd/vmovaps/vmovapd. */
4089 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4090 && i.tm.opcode_modifier.opcodeprefix <= PREFIX_0X66)
4091 i.tm.base_opcode = 0x10 | (i.tm.base_opcode & 1);
4092 break;
4093 case 0x6f: /* Load instructions. */
4094 case 0x7f: /* Store instructions. */
4095 /* movdqa/vmovdqa/vmovdqa64/vmovdqa32. */
4096 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4097 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0X66)
4098 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
4099 break;
4100 default:
4101 break;
4102 }
4103 }
4104
4105 /* Try the shortest encoding by shortening operand size. */
4106
4107 static void
4108 optimize_encoding (void)
4109 {
4110 unsigned int j;
4111
4112 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
4113 && i.tm.base_opcode == 0x8d)
4114 {
4115 /* Optimize: -O:
4116 lea symbol, %rN -> mov $symbol, %rN
4117 lea (%rM), %rN -> mov %rM, %rN
4118 lea (,%rM,1), %rN -> mov %rM, %rN
4119
4120 and in 32-bit mode for 16-bit addressing
4121
4122 lea (%rM), %rN -> movzx %rM, %rN
4123
4124 and in 64-bit mode zap 32-bit addressing in favor of using a
4125 32-bit (or less) destination.
4126 */
4127 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
4128 {
4129 if (!i.op[1].regs->reg_type.bitfield.word)
4130 i.tm.opcode_modifier.size = SIZE32;
4131 i.prefix[ADDR_PREFIX] = 0;
4132 }
4133
4134 if (!i.index_reg && !i.base_reg)
4135 {
4136 /* Handle:
4137 lea symbol, %rN -> mov $symbol, %rN
4138 */
4139 if (flag_code == CODE_64BIT)
4140 {
4141 /* Don't transform a relocation to a 16-bit one. */
4142 if (i.op[0].disps
4143 && i.op[0].disps->X_op != O_constant
4144 && i.op[1].regs->reg_type.bitfield.word)
4145 return;
4146
4147 if (!i.op[1].regs->reg_type.bitfield.qword
4148 || i.tm.opcode_modifier.size == SIZE32)
4149 {
4150 i.tm.base_opcode = 0xb8;
4151 i.tm.opcode_modifier.modrm = 0;
4152 if (!i.op[1].regs->reg_type.bitfield.word)
4153 i.types[0].bitfield.imm32 = 1;
4154 else
4155 {
4156 i.tm.opcode_modifier.size = SIZE16;
4157 i.types[0].bitfield.imm16 = 1;
4158 }
4159 }
4160 else
4161 {
4162 /* Subject to further optimization below. */
4163 i.tm.base_opcode = 0xc7;
4164 i.tm.extension_opcode = 0;
4165 i.types[0].bitfield.imm32s = 1;
4166 i.types[0].bitfield.baseindex = 0;
4167 }
4168 }
4169 /* Outside of 64-bit mode address and operand sizes have to match if
4170 a relocation is involved, as otherwise we wouldn't (currently) or
4171 even couldn't express the relocation correctly. */
4172 else if (i.op[0].disps
4173 && i.op[0].disps->X_op != O_constant
4174 && ((!i.prefix[ADDR_PREFIX])
4175 != (flag_code == CODE_32BIT
4176 ? i.op[1].regs->reg_type.bitfield.dword
4177 : i.op[1].regs->reg_type.bitfield.word)))
4178 return;
4179 /* In 16-bit mode converting LEA with 16-bit addressing and a 32-bit
4180 destination is going to grow encoding size. */
4181 else if (flag_code == CODE_16BIT
4182 && (optimize <= 1 || optimize_for_space)
4183 && !i.prefix[ADDR_PREFIX]
4184 && i.op[1].regs->reg_type.bitfield.dword)
4185 return;
4186 else
4187 {
4188 i.tm.base_opcode = 0xb8;
4189 i.tm.opcode_modifier.modrm = 0;
4190 if (i.op[1].regs->reg_type.bitfield.dword)
4191 i.types[0].bitfield.imm32 = 1;
4192 else
4193 i.types[0].bitfield.imm16 = 1;
4194
4195 if (i.op[0].disps
4196 && i.op[0].disps->X_op == O_constant
4197 && i.op[1].regs->reg_type.bitfield.dword
4198 /* NB: Add () to !i.prefix[ADDR_PREFIX] to silence
4199 GCC 5. */
4200 && (!i.prefix[ADDR_PREFIX]) != (flag_code == CODE_32BIT))
4201 i.op[0].disps->X_add_number &= 0xffff;
4202 }
4203
4204 i.tm.operand_types[0] = i.types[0];
4205 i.imm_operands = 1;
4206 if (!i.op[0].imms)
4207 {
4208 i.op[0].imms = &im_expressions[0];
4209 i.op[0].imms->X_op = O_absent;
4210 }
4211 }
4212 else if (i.op[0].disps
4213 && (i.op[0].disps->X_op != O_constant
4214 || i.op[0].disps->X_add_number))
4215 return;
4216 else
4217 {
4218 /* Handle:
4219 lea (%rM), %rN -> mov %rM, %rN
4220 lea (,%rM,1), %rN -> mov %rM, %rN
4221 lea (%rM), %rN -> movzx %rM, %rN
4222 */
4223 const reg_entry *addr_reg;
4224
4225 if (!i.index_reg && i.base_reg->reg_num != RegIP)
4226 addr_reg = i.base_reg;
4227 else if (!i.base_reg
4228 && i.index_reg->reg_num != RegIZ
4229 && !i.log2_scale_factor)
4230 addr_reg = i.index_reg;
4231 else
4232 return;
4233
4234 if (addr_reg->reg_type.bitfield.word
4235 && i.op[1].regs->reg_type.bitfield.dword)
4236 {
4237 if (flag_code != CODE_32BIT)
4238 return;
4239 i.tm.opcode_modifier.opcodespace = SPACE_0F;
4240 i.tm.base_opcode = 0xb7;
4241 }
4242 else
4243 i.tm.base_opcode = 0x8b;
4244
4245 if (addr_reg->reg_type.bitfield.dword
4246 && i.op[1].regs->reg_type.bitfield.qword)
4247 i.tm.opcode_modifier.size = SIZE32;
4248
4249 i.op[0].regs = addr_reg;
4250 i.reg_operands = 2;
4251 }
4252
4253 i.mem_operands = 0;
4254 i.disp_operands = 0;
4255 i.prefix[ADDR_PREFIX] = 0;
4256 i.prefix[SEG_PREFIX] = 0;
4257 i.seg[0] = NULL;
4258 }
4259
4260 if (optimize_for_space
4261 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
4262 && i.reg_operands == 1
4263 && i.imm_operands == 1
4264 && !i.types[1].bitfield.byte
4265 && i.op[0].imms->X_op == O_constant
4266 && fits_in_imm7 (i.op[0].imms->X_add_number)
4267 && (i.tm.base_opcode == 0xa8
4268 || (i.tm.base_opcode == 0xf6
4269 && i.tm.extension_opcode == 0x0)))
4270 {
4271 /* Optimize: -Os:
4272 test $imm7, %r64/%r32/%r16 -> test $imm7, %r8
4273 */
4274 unsigned int base_regnum = i.op[1].regs->reg_num;
4275 if (flag_code == CODE_64BIT || base_regnum < 4)
4276 {
4277 i.types[1].bitfield.byte = 1;
4278 /* Ignore the suffix. */
4279 i.suffix = 0;
4280 /* Convert to byte registers. */
4281 if (i.types[1].bitfield.word)
4282 j = 16;
4283 else if (i.types[1].bitfield.dword)
4284 j = 32;
4285 else
4286 j = 48;
4287 if (!(i.op[1].regs->reg_flags & RegRex) && base_regnum < 4)
4288 j += 8;
4289 i.op[1].regs -= j;
4290 }
4291 }
4292 else if (flag_code == CODE_64BIT
4293 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
4294 && ((i.types[1].bitfield.qword
4295 && i.reg_operands == 1
4296 && i.imm_operands == 1
4297 && i.op[0].imms->X_op == O_constant
4298 && ((i.tm.base_opcode == 0xb8
4299 && i.tm.extension_opcode == None
4300 && fits_in_unsigned_long (i.op[0].imms->X_add_number))
4301 || (fits_in_imm31 (i.op[0].imms->X_add_number)
4302 && ((i.tm.base_opcode == 0x24
4303 || i.tm.base_opcode == 0xa8)
4304 || (i.tm.base_opcode == 0x80
4305 && i.tm.extension_opcode == 0x4)
4306 || ((i.tm.base_opcode == 0xf6
4307 || (i.tm.base_opcode | 1) == 0xc7)
4308 && i.tm.extension_opcode == 0x0)))
4309 || (fits_in_imm7 (i.op[0].imms->X_add_number)
4310 && i.tm.base_opcode == 0x83
4311 && i.tm.extension_opcode == 0x4)))
4312 || (i.types[0].bitfield.qword
4313 && ((i.reg_operands == 2
4314 && i.op[0].regs == i.op[1].regs
4315 && (i.tm.base_opcode == 0x30
4316 || i.tm.base_opcode == 0x28))
4317 || (i.reg_operands == 1
4318 && i.operands == 1
4319 && i.tm.base_opcode == 0x30)))))
4320 {
4321 /* Optimize: -O:
4322 andq $imm31, %r64 -> andl $imm31, %r32
4323 andq $imm7, %r64 -> andl $imm7, %r32
4324 testq $imm31, %r64 -> testl $imm31, %r32
4325 xorq %r64, %r64 -> xorl %r32, %r32
4326 subq %r64, %r64 -> subl %r32, %r32
4327 movq $imm31, %r64 -> movl $imm31, %r32
4328 movq $imm32, %r64 -> movl $imm32, %r32
4329 */
4330 i.tm.opcode_modifier.norex64 = 1;
4331 if (i.tm.base_opcode == 0xb8 || (i.tm.base_opcode | 1) == 0xc7)
4332 {
4333 /* Handle
4334 movq $imm31, %r64 -> movl $imm31, %r32
4335 movq $imm32, %r64 -> movl $imm32, %r32
4336 */
4337 i.tm.operand_types[0].bitfield.imm32 = 1;
4338 i.tm.operand_types[0].bitfield.imm32s = 0;
4339 i.tm.operand_types[0].bitfield.imm64 = 0;
4340 i.types[0].bitfield.imm32 = 1;
4341 i.types[0].bitfield.imm32s = 0;
4342 i.types[0].bitfield.imm64 = 0;
4343 i.types[1].bitfield.dword = 1;
4344 i.types[1].bitfield.qword = 0;
4345 if ((i.tm.base_opcode | 1) == 0xc7)
4346 {
4347 /* Handle
4348 movq $imm31, %r64 -> movl $imm31, %r32
4349 */
4350 i.tm.base_opcode = 0xb8;
4351 i.tm.extension_opcode = None;
4352 i.tm.opcode_modifier.w = 0;
4353 i.tm.opcode_modifier.modrm = 0;
4354 }
4355 }
4356 }
4357 else if (optimize > 1
4358 && !optimize_for_space
4359 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
4360 && i.reg_operands == 2
4361 && i.op[0].regs == i.op[1].regs
4362 && ((i.tm.base_opcode & ~(Opcode_D | 1)) == 0x8
4363 || (i.tm.base_opcode & ~(Opcode_D | 1)) == 0x20)
4364 && (flag_code != CODE_64BIT || !i.types[0].bitfield.dword))
4365 {
4366 /* Optimize: -O2:
4367 andb %rN, %rN -> testb %rN, %rN
4368 andw %rN, %rN -> testw %rN, %rN
4369 andq %rN, %rN -> testq %rN, %rN
4370 orb %rN, %rN -> testb %rN, %rN
4371 orw %rN, %rN -> testw %rN, %rN
4372 orq %rN, %rN -> testq %rN, %rN
4373
4374 and outside of 64-bit mode
4375
4376 andl %rN, %rN -> testl %rN, %rN
4377 orl %rN, %rN -> testl %rN, %rN
4378 */
4379 i.tm.base_opcode = 0x84 | (i.tm.base_opcode & 1);
4380 }
4381 else if (i.reg_operands == 3
4382 && i.op[0].regs == i.op[1].regs
4383 && !i.types[2].bitfield.xmmword
4384 && (i.tm.opcode_modifier.vex
4385 || ((!i.mask.reg || i.mask.zeroing)
4386 && i.rounding.type == rc_none
4387 && is_evex_encoding (&i.tm)
4388 && (i.vec_encoding != vex_encoding_evex
4389 || cpu_arch_isa_flags.bitfield.cpuavx512vl
4390 || i.tm.cpu_flags.bitfield.cpuavx512vl
4391 || (i.tm.operand_types[2].bitfield.zmmword
4392 && i.types[2].bitfield.ymmword))))
4393 && ((i.tm.base_opcode == 0x55
4394 || i.tm.base_opcode == 0x57
4395 || i.tm.base_opcode == 0xdf
4396 || i.tm.base_opcode == 0xef
4397 || i.tm.base_opcode == 0xf8
4398 || i.tm.base_opcode == 0xf9
4399 || i.tm.base_opcode == 0xfa
4400 || i.tm.base_opcode == 0xfb
4401 || i.tm.base_opcode == 0x42
4402 || i.tm.base_opcode == 0x47)
4403 && i.tm.extension_opcode == None))
4404 {
4405 /* Optimize: -O1:
4406 VOP, one of vandnps, vandnpd, vxorps, vxorpd, vpsubb, vpsubd,
4407 vpsubq and vpsubw:
4408 EVEX VOP %zmmM, %zmmM, %zmmN
4409 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
4410 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4411 EVEX VOP %ymmM, %ymmM, %ymmN
4412 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
4413 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4414 VEX VOP %ymmM, %ymmM, %ymmN
4415 -> VEX VOP %xmmM, %xmmM, %xmmN
4416 VOP, one of vpandn and vpxor:
4417 VEX VOP %ymmM, %ymmM, %ymmN
4418 -> VEX VOP %xmmM, %xmmM, %xmmN
4419 VOP, one of vpandnd and vpandnq:
4420 EVEX VOP %zmmM, %zmmM, %zmmN
4421 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
4422 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4423 EVEX VOP %ymmM, %ymmM, %ymmN
4424 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
4425 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4426 VOP, one of vpxord and vpxorq:
4427 EVEX VOP %zmmM, %zmmM, %zmmN
4428 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
4429 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4430 EVEX VOP %ymmM, %ymmM, %ymmN
4431 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
4432 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4433 VOP, one of kxord and kxorq:
4434 VEX VOP %kM, %kM, %kN
4435 -> VEX kxorw %kM, %kM, %kN
4436 VOP, one of kandnd and kandnq:
4437 VEX VOP %kM, %kM, %kN
4438 -> VEX kandnw %kM, %kM, %kN
4439 */
4440 if (is_evex_encoding (&i.tm))
4441 {
4442 if (i.vec_encoding != vex_encoding_evex)
4443 {
4444 i.tm.opcode_modifier.vex = VEX128;
4445 i.tm.opcode_modifier.vexw = VEXW0;
4446 i.tm.opcode_modifier.evex = 0;
4447 }
4448 else if (optimize > 1)
4449 i.tm.opcode_modifier.evex = EVEX128;
4450 else
4451 return;
4452 }
4453 else if (i.tm.operand_types[0].bitfield.class == RegMask)
4454 {
4455 i.tm.opcode_modifier.opcodeprefix = PREFIX_NONE;
4456 i.tm.opcode_modifier.vexw = VEXW0;
4457 }
4458 else
4459 i.tm.opcode_modifier.vex = VEX128;
4460
4461 if (i.tm.opcode_modifier.vex)
4462 for (j = 0; j < 3; j++)
4463 {
4464 i.types[j].bitfield.xmmword = 1;
4465 i.types[j].bitfield.ymmword = 0;
4466 }
4467 }
4468 else if (i.vec_encoding != vex_encoding_evex
4469 && !i.types[0].bitfield.zmmword
4470 && !i.types[1].bitfield.zmmword
4471 && !i.mask.reg
4472 && !i.broadcast.type
4473 && is_evex_encoding (&i.tm)
4474 && ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4475 || (i.tm.base_opcode & ~4) == 0xdb
4476 || (i.tm.base_opcode & ~4) == 0xeb)
4477 && i.tm.extension_opcode == None)
4478 {
4479 /* Optimize: -O1:
4480 VOP, one of vmovdqa32, vmovdqa64, vmovdqu8, vmovdqu16,
4481 vmovdqu32 and vmovdqu64:
4482 EVEX VOP %xmmM, %xmmN
4483 -> VEX vmovdqa|vmovdqu %xmmM, %xmmN (M and N < 16)
4484 EVEX VOP %ymmM, %ymmN
4485 -> VEX vmovdqa|vmovdqu %ymmM, %ymmN (M and N < 16)
4486 EVEX VOP %xmmM, mem
4487 -> VEX vmovdqa|vmovdqu %xmmM, mem (M < 16)
4488 EVEX VOP %ymmM, mem
4489 -> VEX vmovdqa|vmovdqu %ymmM, mem (M < 16)
4490 EVEX VOP mem, %xmmN
4491 -> VEX mvmovdqa|vmovdquem, %xmmN (N < 16)
4492 EVEX VOP mem, %ymmN
4493 -> VEX vmovdqa|vmovdqu mem, %ymmN (N < 16)
4494 VOP, one of vpand, vpandn, vpor, vpxor:
4495 EVEX VOP{d,q} %xmmL, %xmmM, %xmmN
4496 -> VEX VOP %xmmL, %xmmM, %xmmN (L, M, and N < 16)
4497 EVEX VOP{d,q} %ymmL, %ymmM, %ymmN
4498 -> VEX VOP %ymmL, %ymmM, %ymmN (L, M, and N < 16)
4499 EVEX VOP{d,q} mem, %xmmM, %xmmN
4500 -> VEX VOP mem, %xmmM, %xmmN (M and N < 16)
4501 EVEX VOP{d,q} mem, %ymmM, %ymmN
4502 -> VEX VOP mem, %ymmM, %ymmN (M and N < 16)
4503 */
4504 for (j = 0; j < i.operands; j++)
4505 if (operand_type_check (i.types[j], disp)
4506 && i.op[j].disps->X_op == O_constant)
4507 {
4508 /* Since the VEX prefix has 2 or 3 bytes, the EVEX prefix
4509 has 4 bytes, EVEX Disp8 has 1 byte and VEX Disp32 has 4
4510 bytes, we choose EVEX Disp8 over VEX Disp32. */
4511 int evex_disp8, vex_disp8;
4512 unsigned int memshift = i.memshift;
4513 offsetT n = i.op[j].disps->X_add_number;
4514
4515 evex_disp8 = fits_in_disp8 (n);
4516 i.memshift = 0;
4517 vex_disp8 = fits_in_disp8 (n);
4518 if (evex_disp8 != vex_disp8)
4519 {
4520 i.memshift = memshift;
4521 return;
4522 }
4523
4524 i.types[j].bitfield.disp8 = vex_disp8;
4525 break;
4526 }
4527 if ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4528 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2)
4529 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
4530 i.tm.opcode_modifier.vex
4531 = i.types[0].bitfield.ymmword ? VEX256 : VEX128;
4532 i.tm.opcode_modifier.vexw = VEXW0;
4533 /* VPAND, VPOR, and VPXOR are commutative. */
4534 if (i.reg_operands == 3 && i.tm.base_opcode != 0xdf)
4535 i.tm.opcode_modifier.commutative = 1;
4536 i.tm.opcode_modifier.evex = 0;
4537 i.tm.opcode_modifier.masking = 0;
4538 i.tm.opcode_modifier.broadcast = 0;
4539 i.tm.opcode_modifier.disp8memshift = 0;
4540 i.memshift = 0;
4541 if (j < i.operands)
4542 i.types[j].bitfield.disp8
4543 = fits_in_disp8 (i.op[j].disps->X_add_number);
4544 }
4545 }
4546
4547 /* Return non-zero for load instruction. */
4548
4549 static int
4550 load_insn_p (void)
4551 {
4552 unsigned int dest;
4553 int any_vex_p = is_any_vex_encoding (&i.tm);
4554 unsigned int base_opcode = i.tm.base_opcode | 1;
4555
4556 if (!any_vex_p)
4557 {
4558 /* Anysize insns: lea, invlpg, clflush, prefetchnta, prefetcht0,
4559 prefetcht1, prefetcht2, prefetchtw, bndmk, bndcl, bndcu, bndcn,
4560 bndstx, bndldx, prefetchwt1, clflushopt, clwb, cldemote. */
4561 if (i.tm.opcode_modifier.anysize)
4562 return 0;
4563
4564 /* pop. */
4565 if (strcmp (i.tm.name, "pop") == 0)
4566 return 1;
4567 }
4568
4569 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE)
4570 {
4571 /* popf, popa. */
4572 if (i.tm.base_opcode == 0x9d
4573 || i.tm.base_opcode == 0x61)
4574 return 1;
4575
4576 /* movs, cmps, lods, scas. */
4577 if ((i.tm.base_opcode | 0xb) == 0xaf)
4578 return 1;
4579
4580 /* outs, xlatb. */
4581 if (base_opcode == 0x6f
4582 || i.tm.base_opcode == 0xd7)
4583 return 1;
4584 /* NB: For AMD-specific insns with implicit memory operands,
4585 they're intentionally not covered. */
4586 }
4587
4588 /* No memory operand. */
4589 if (!i.mem_operands)
4590 return 0;
4591
4592 if (any_vex_p)
4593 {
4594 /* vldmxcsr. */
4595 if (i.tm.base_opcode == 0xae
4596 && i.tm.opcode_modifier.vex
4597 && i.tm.opcode_modifier.opcodespace == SPACE_0F
4598 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
4599 && i.tm.extension_opcode == 2)
4600 return 1;
4601 }
4602 else if (i.tm.opcode_modifier.opcodespace == SPACE_BASE)
4603 {
4604 /* test, not, neg, mul, imul, div, idiv. */
4605 if ((i.tm.base_opcode == 0xf6 || i.tm.base_opcode == 0xf7)
4606 && i.tm.extension_opcode != 1)
4607 return 1;
4608
4609 /* inc, dec. */
4610 if (base_opcode == 0xff && i.tm.extension_opcode <= 1)
4611 return 1;
4612
4613 /* add, or, adc, sbb, and, sub, xor, cmp. */
4614 if (i.tm.base_opcode >= 0x80 && i.tm.base_opcode <= 0x83)
4615 return 1;
4616
4617 /* rol, ror, rcl, rcr, shl/sal, shr, sar. */
4618 if ((base_opcode == 0xc1
4619 || (i.tm.base_opcode >= 0xd0 && i.tm.base_opcode <= 0xd3))
4620 && i.tm.extension_opcode != 6)
4621 return 1;
4622
4623 /* Check for x87 instructions. */
4624 if (base_opcode >= 0xd8 && base_opcode <= 0xdf)
4625 {
4626 /* Skip fst, fstp, fstenv, fstcw. */
4627 if (i.tm.base_opcode == 0xd9
4628 && (i.tm.extension_opcode == 2
4629 || i.tm.extension_opcode == 3
4630 || i.tm.extension_opcode == 6
4631 || i.tm.extension_opcode == 7))
4632 return 0;
4633
4634 /* Skip fisttp, fist, fistp, fstp. */
4635 if (i.tm.base_opcode == 0xdb
4636 && (i.tm.extension_opcode == 1
4637 || i.tm.extension_opcode == 2
4638 || i.tm.extension_opcode == 3
4639 || i.tm.extension_opcode == 7))
4640 return 0;
4641
4642 /* Skip fisttp, fst, fstp, fsave, fstsw. */
4643 if (i.tm.base_opcode == 0xdd
4644 && (i.tm.extension_opcode == 1
4645 || i.tm.extension_opcode == 2
4646 || i.tm.extension_opcode == 3
4647 || i.tm.extension_opcode == 6
4648 || i.tm.extension_opcode == 7))
4649 return 0;
4650
4651 /* Skip fisttp, fist, fistp, fbstp, fistp. */
4652 if (i.tm.base_opcode == 0xdf
4653 && (i.tm.extension_opcode == 1
4654 || i.tm.extension_opcode == 2
4655 || i.tm.extension_opcode == 3
4656 || i.tm.extension_opcode == 6
4657 || i.tm.extension_opcode == 7))
4658 return 0;
4659
4660 return 1;
4661 }
4662 }
4663 else if (i.tm.opcode_modifier.opcodespace == SPACE_0F)
4664 {
4665 /* bt, bts, btr, btc. */
4666 if (i.tm.base_opcode == 0xba
4667 && (i.tm.extension_opcode >= 4 && i.tm.extension_opcode <= 7))
4668 return 1;
4669
4670 /* cmpxchg8b, cmpxchg16b, xrstors, vmptrld. */
4671 if (i.tm.base_opcode == 0xc7
4672 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
4673 && (i.tm.extension_opcode == 1 || i.tm.extension_opcode == 3
4674 || i.tm.extension_opcode == 6))
4675 return 1;
4676
4677 /* fxrstor, ldmxcsr, xrstor. */
4678 if (i.tm.base_opcode == 0xae
4679 && (i.tm.extension_opcode == 1
4680 || i.tm.extension_opcode == 2
4681 || i.tm.extension_opcode == 5))
4682 return 1;
4683
4684 /* lgdt, lidt, lmsw. */
4685 if (i.tm.base_opcode == 0x01
4686 && (i.tm.extension_opcode == 2
4687 || i.tm.extension_opcode == 3
4688 || i.tm.extension_opcode == 6))
4689 return 1;
4690 }
4691
4692 dest = i.operands - 1;
4693
4694 /* Check fake imm8 operand and 3 source operands. */
4695 if ((i.tm.opcode_modifier.immext
4696 || i.tm.opcode_modifier.vexsources == VEX3SOURCES)
4697 && i.types[dest].bitfield.imm8)
4698 dest--;
4699
4700 /* add, or, adc, sbb, and, sub, xor, cmp, test, xchg. */
4701 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
4702 && (base_opcode == 0x1
4703 || base_opcode == 0x9
4704 || base_opcode == 0x11
4705 || base_opcode == 0x19
4706 || base_opcode == 0x21
4707 || base_opcode == 0x29
4708 || base_opcode == 0x31
4709 || base_opcode == 0x39
4710 || (base_opcode | 2) == 0x87))
4711 return 1;
4712
4713 /* xadd. */
4714 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4715 && base_opcode == 0xc1)
4716 return 1;
4717
4718 /* Check for load instruction. */
4719 return (i.types[dest].bitfield.class != ClassNone
4720 || i.types[dest].bitfield.instance == Accum);
4721 }
4722
4723 /* Output lfence, 0xfaee8, after instruction. */
4724
4725 static void
4726 insert_lfence_after (void)
4727 {
4728 if (lfence_after_load && load_insn_p ())
4729 {
4730 /* There are also two REP string instructions that require
4731 special treatment. Specifically, the compare string (CMPS)
4732 and scan string (SCAS) instructions set EFLAGS in a manner
4733 that depends on the data being compared/scanned. When used
4734 with a REP prefix, the number of iterations may therefore
4735 vary depending on this data. If the data is a program secret
4736 chosen by the adversary using an LVI method,
4737 then this data-dependent behavior may leak some aspect
4738 of the secret. */
4739 if (((i.tm.base_opcode | 0x1) == 0xa7
4740 || (i.tm.base_opcode | 0x1) == 0xaf)
4741 && i.prefix[REP_PREFIX])
4742 {
4743 as_warn (_("`%s` changes flags which would affect control flow behavior"),
4744 i.tm.name);
4745 }
4746 char *p = frag_more (3);
4747 *p++ = 0xf;
4748 *p++ = 0xae;
4749 *p = 0xe8;
4750 }
4751 }
4752
4753 /* Output lfence, 0xfaee8, before instruction. */
4754
4755 static void
4756 insert_lfence_before (void)
4757 {
4758 char *p;
4759
4760 if (i.tm.opcode_modifier.opcodespace != SPACE_BASE)
4761 return;
4762
4763 if (i.tm.base_opcode == 0xff
4764 && (i.tm.extension_opcode == 2 || i.tm.extension_opcode == 4))
4765 {
4766 /* Insert lfence before indirect branch if needed. */
4767
4768 if (lfence_before_indirect_branch == lfence_branch_none)
4769 return;
4770
4771 if (i.operands != 1)
4772 abort ();
4773
4774 if (i.reg_operands == 1)
4775 {
4776 /* Indirect branch via register. Don't insert lfence with
4777 -mlfence-after-load=yes. */
4778 if (lfence_after_load
4779 || lfence_before_indirect_branch == lfence_branch_memory)
4780 return;
4781 }
4782 else if (i.mem_operands == 1
4783 && lfence_before_indirect_branch != lfence_branch_register)
4784 {
4785 as_warn (_("indirect `%s` with memory operand should be avoided"),
4786 i.tm.name);
4787 return;
4788 }
4789 else
4790 return;
4791
4792 if (last_insn.kind != last_insn_other
4793 && last_insn.seg == now_seg)
4794 {
4795 as_warn_where (last_insn.file, last_insn.line,
4796 _("`%s` skips -mlfence-before-indirect-branch on `%s`"),
4797 last_insn.name, i.tm.name);
4798 return;
4799 }
4800
4801 p = frag_more (3);
4802 *p++ = 0xf;
4803 *p++ = 0xae;
4804 *p = 0xe8;
4805 return;
4806 }
4807
4808 /* Output or/not/shl and lfence before near ret. */
4809 if (lfence_before_ret != lfence_before_ret_none
4810 && (i.tm.base_opcode == 0xc2
4811 || i.tm.base_opcode == 0xc3))
4812 {
4813 if (last_insn.kind != last_insn_other
4814 && last_insn.seg == now_seg)
4815 {
4816 as_warn_where (last_insn.file, last_insn.line,
4817 _("`%s` skips -mlfence-before-ret on `%s`"),
4818 last_insn.name, i.tm.name);
4819 return;
4820 }
4821
4822 /* Near ret ingore operand size override under CPU64. */
4823 char prefix = flag_code == CODE_64BIT
4824 ? 0x48
4825 : i.prefix[DATA_PREFIX] ? 0x66 : 0x0;
4826
4827 if (lfence_before_ret == lfence_before_ret_not)
4828 {
4829 /* not: 0xf71424, may add prefix
4830 for operand size override or 64-bit code. */
4831 p = frag_more ((prefix ? 2 : 0) + 6 + 3);
4832 if (prefix)
4833 *p++ = prefix;
4834 *p++ = 0xf7;
4835 *p++ = 0x14;
4836 *p++ = 0x24;
4837 if (prefix)
4838 *p++ = prefix;
4839 *p++ = 0xf7;
4840 *p++ = 0x14;
4841 *p++ = 0x24;
4842 }
4843 else
4844 {
4845 p = frag_more ((prefix ? 1 : 0) + 4 + 3);
4846 if (prefix)
4847 *p++ = prefix;
4848 if (lfence_before_ret == lfence_before_ret_or)
4849 {
4850 /* or: 0x830c2400, may add prefix
4851 for operand size override or 64-bit code. */
4852 *p++ = 0x83;
4853 *p++ = 0x0c;
4854 }
4855 else
4856 {
4857 /* shl: 0xc1242400, may add prefix
4858 for operand size override or 64-bit code. */
4859 *p++ = 0xc1;
4860 *p++ = 0x24;
4861 }
4862
4863 *p++ = 0x24;
4864 *p++ = 0x0;
4865 }
4866
4867 *p++ = 0xf;
4868 *p++ = 0xae;
4869 *p = 0xe8;
4870 }
4871 }
4872
4873 /* This is the guts of the machine-dependent assembler. LINE points to a
4874 machine dependent instruction. This function is supposed to emit
4875 the frags/bytes it assembles to. */
4876
4877 void
4878 md_assemble (char *line)
4879 {
4880 unsigned int j;
4881 char mnemonic[MAX_MNEM_SIZE], mnem_suffix;
4882 const insn_template *t;
4883
4884 /* Initialize globals. */
4885 memset (&i, '\0', sizeof (i));
4886 i.rounding.type = rc_none;
4887 for (j = 0; j < MAX_OPERANDS; j++)
4888 i.reloc[j] = NO_RELOC;
4889 memset (disp_expressions, '\0', sizeof (disp_expressions));
4890 memset (im_expressions, '\0', sizeof (im_expressions));
4891 save_stack_p = save_stack;
4892
4893 /* First parse an instruction mnemonic & call i386_operand for the operands.
4894 We assume that the scrubber has arranged it so that line[0] is the valid
4895 start of a (possibly prefixed) mnemonic. */
4896
4897 line = parse_insn (line, mnemonic);
4898 if (line == NULL)
4899 return;
4900 mnem_suffix = i.suffix;
4901
4902 line = parse_operands (line, mnemonic);
4903 this_operand = -1;
4904 xfree (i.memop1_string);
4905 i.memop1_string = NULL;
4906 if (line == NULL)
4907 return;
4908
4909 /* Now we've parsed the mnemonic into a set of templates, and have the
4910 operands at hand. */
4911
4912 /* All Intel opcodes have reversed operands except for "bound", "enter",
4913 "invlpg*", "monitor*", "mwait*", "tpause", "umwait", "pvalidate",
4914 "rmpadjust", and "rmpupdate". We also don't reverse intersegment "jmp"
4915 and "call" instructions with 2 immediate operands so that the immediate
4916 segment precedes the offset consistently in Intel and AT&T modes. */
4917 if (intel_syntax
4918 && i.operands > 1
4919 && (strcmp (mnemonic, "bound") != 0)
4920 && (strncmp (mnemonic, "invlpg", 6) != 0)
4921 && !startswith (mnemonic, "monitor")
4922 && !startswith (mnemonic, "mwait")
4923 && (strcmp (mnemonic, "pvalidate") != 0)
4924 && !startswith (mnemonic, "rmp")
4925 && (strcmp (mnemonic, "tpause") != 0)
4926 && (strcmp (mnemonic, "umwait") != 0)
4927 && !(operand_type_check (i.types[0], imm)
4928 && operand_type_check (i.types[1], imm)))
4929 swap_operands ();
4930
4931 /* The order of the immediates should be reversed
4932 for 2 immediates extrq and insertq instructions */
4933 if (i.imm_operands == 2
4934 && (strcmp (mnemonic, "extrq") == 0
4935 || strcmp (mnemonic, "insertq") == 0))
4936 swap_2_operands (0, 1);
4937
4938 if (i.imm_operands)
4939 optimize_imm ();
4940
4941 if (i.disp_operands && !want_disp32 (current_templates->start))
4942 {
4943 for (j = 0; j < i.operands; ++j)
4944 {
4945 const expressionS *exp = i.op[j].disps;
4946
4947 if (!operand_type_check (i.types[j], disp))
4948 continue;
4949
4950 if (exp->X_op != O_constant)
4951 continue;
4952
4953 /* Since displacement is signed extended to 64bit, don't allow
4954 disp32 and turn off disp32s if they are out of range. */
4955 i.types[j].bitfield.disp32 = 0;
4956 if (fits_in_signed_long (exp->X_add_number))
4957 continue;
4958
4959 i.types[j].bitfield.disp32s = 0;
4960 if (i.types[j].bitfield.baseindex)
4961 {
4962 char number_buf[128];
4963
4964 /* Coded this way in order to allow for ease of translation. */
4965 sprintf_vma (number_buf, exp->X_add_number);
4966 as_bad (_("0x%s out of range of signed 32bit displacement"),
4967 number_buf);
4968 return;
4969 }
4970 }
4971 }
4972
4973 /* Don't optimize displacement for movabs since it only takes 64bit
4974 displacement. */
4975 if (i.disp_operands
4976 && i.disp_encoding != disp_encoding_32bit
4977 && (flag_code != CODE_64BIT
4978 || strcmp (mnemonic, "movabs") != 0))
4979 optimize_disp ();
4980
4981 /* Next, we find a template that matches the given insn,
4982 making sure the overlap of the given operands types is consistent
4983 with the template operand types. */
4984
4985 if (!(t = match_template (mnem_suffix)))
4986 return;
4987
4988 if (sse_check != check_none
4989 && !i.tm.opcode_modifier.noavx
4990 && !i.tm.cpu_flags.bitfield.cpuavx
4991 && !i.tm.cpu_flags.bitfield.cpuavx512f
4992 && (i.tm.cpu_flags.bitfield.cpusse
4993 || i.tm.cpu_flags.bitfield.cpusse2
4994 || i.tm.cpu_flags.bitfield.cpusse3
4995 || i.tm.cpu_flags.bitfield.cpussse3
4996 || i.tm.cpu_flags.bitfield.cpusse4_1
4997 || i.tm.cpu_flags.bitfield.cpusse4_2
4998 || i.tm.cpu_flags.bitfield.cpupclmul
4999 || i.tm.cpu_flags.bitfield.cpuaes
5000 || i.tm.cpu_flags.bitfield.cpusha
5001 || i.tm.cpu_flags.bitfield.cpugfni))
5002 {
5003 (sse_check == check_warning
5004 ? as_warn
5005 : as_bad) (_("SSE instruction `%s' is used"), i.tm.name);
5006 }
5007
5008 if (i.tm.opcode_modifier.fwait)
5009 if (!add_prefix (FWAIT_OPCODE))
5010 return;
5011
5012 /* Check if REP prefix is OK. */
5013 if (i.rep_prefix && i.tm.opcode_modifier.prefixok != PrefixRep)
5014 {
5015 as_bad (_("invalid instruction `%s' after `%s'"),
5016 i.tm.name, i.rep_prefix);
5017 return;
5018 }
5019
5020 /* Check for lock without a lockable instruction. Destination operand
5021 must be memory unless it is xchg (0x86). */
5022 if (i.prefix[LOCK_PREFIX]
5023 && (i.tm.opcode_modifier.prefixok < PrefixLock
5024 || i.mem_operands == 0
5025 || (i.tm.base_opcode != 0x86
5026 && !(i.flags[i.operands - 1] & Operand_Mem))))
5027 {
5028 as_bad (_("expecting lockable instruction after `lock'"));
5029 return;
5030 }
5031
5032 /* Check for data size prefix on VEX/XOP/EVEX encoded and SIMD insns. */
5033 if (i.prefix[DATA_PREFIX]
5034 && (is_any_vex_encoding (&i.tm)
5035 || i.tm.operand_types[i.imm_operands].bitfield.class >= RegMMX
5036 || i.tm.operand_types[i.imm_operands + 1].bitfield.class >= RegMMX))
5037 {
5038 as_bad (_("data size prefix invalid with `%s'"), i.tm.name);
5039 return;
5040 }
5041
5042 /* Check if HLE prefix is OK. */
5043 if (i.hle_prefix && !check_hle ())
5044 return;
5045
5046 /* Check BND prefix. */
5047 if (i.bnd_prefix && !i.tm.opcode_modifier.bndprefixok)
5048 as_bad (_("expecting valid branch instruction after `bnd'"));
5049
5050 /* Check NOTRACK prefix. */
5051 if (i.notrack_prefix && i.tm.opcode_modifier.prefixok != PrefixNoTrack)
5052 as_bad (_("expecting indirect branch instruction after `notrack'"));
5053
5054 if (i.tm.cpu_flags.bitfield.cpumpx)
5055 {
5056 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
5057 as_bad (_("32-bit address isn't allowed in 64-bit MPX instructions."));
5058 else if (flag_code != CODE_16BIT
5059 ? i.prefix[ADDR_PREFIX]
5060 : i.mem_operands && !i.prefix[ADDR_PREFIX])
5061 as_bad (_("16-bit address isn't allowed in MPX instructions"));
5062 }
5063
5064 /* Insert BND prefix. */
5065 if (add_bnd_prefix && i.tm.opcode_modifier.bndprefixok)
5066 {
5067 if (!i.prefix[BND_PREFIX])
5068 add_prefix (BND_PREFIX_OPCODE);
5069 else if (i.prefix[BND_PREFIX] != BND_PREFIX_OPCODE)
5070 {
5071 as_warn (_("replacing `rep'/`repe' prefix by `bnd'"));
5072 i.prefix[BND_PREFIX] = BND_PREFIX_OPCODE;
5073 }
5074 }
5075
5076 /* Check string instruction segment overrides. */
5077 if (i.tm.opcode_modifier.isstring >= IS_STRING_ES_OP0)
5078 {
5079 gas_assert (i.mem_operands);
5080 if (!check_string ())
5081 return;
5082 i.disp_operands = 0;
5083 }
5084
5085 if (optimize && !i.no_optimize && i.tm.opcode_modifier.optimize)
5086 optimize_encoding ();
5087
5088 if (use_unaligned_vector_move)
5089 encode_with_unaligned_vector_move ();
5090
5091 if (!process_suffix ())
5092 return;
5093
5094 /* Update operand types and check extended states. */
5095 for (j = 0; j < i.operands; j++)
5096 {
5097 i.types[j] = operand_type_and (i.types[j], i.tm.operand_types[j]);
5098 switch (i.tm.operand_types[j].bitfield.class)
5099 {
5100 default:
5101 break;
5102 case RegMMX:
5103 i.xstate |= xstate_mmx;
5104 break;
5105 case RegMask:
5106 i.xstate |= xstate_mask;
5107 break;
5108 case RegSIMD:
5109 if (i.tm.operand_types[j].bitfield.tmmword)
5110 i.xstate |= xstate_tmm;
5111 else if (i.tm.operand_types[j].bitfield.zmmword)
5112 i.xstate |= xstate_zmm;
5113 else if (i.tm.operand_types[j].bitfield.ymmword)
5114 i.xstate |= xstate_ymm;
5115 else if (i.tm.operand_types[j].bitfield.xmmword)
5116 i.xstate |= xstate_xmm;
5117 break;
5118 }
5119 }
5120
5121 /* Make still unresolved immediate matches conform to size of immediate
5122 given in i.suffix. */
5123 if (!finalize_imm ())
5124 return;
5125
5126 if (i.types[0].bitfield.imm1)
5127 i.imm_operands = 0; /* kludge for shift insns. */
5128
5129 /* We only need to check those implicit registers for instructions
5130 with 3 operands or less. */
5131 if (i.operands <= 3)
5132 for (j = 0; j < i.operands; j++)
5133 if (i.types[j].bitfield.instance != InstanceNone
5134 && !i.types[j].bitfield.xmmword)
5135 i.reg_operands--;
5136
5137 /* For insns with operands there are more diddles to do to the opcode. */
5138 if (i.operands)
5139 {
5140 if (!process_operands ())
5141 return;
5142 }
5143 else if (!quiet_warnings && i.tm.opcode_modifier.ugh)
5144 {
5145 /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc. */
5146 as_warn (_("translating to `%sp'"), i.tm.name);
5147 }
5148
5149 if (is_any_vex_encoding (&i.tm))
5150 {
5151 if (!cpu_arch_flags.bitfield.cpui286)
5152 {
5153 as_bad (_("instruction `%s' isn't supported outside of protected mode."),
5154 i.tm.name);
5155 return;
5156 }
5157
5158 /* Check for explicit REX prefix. */
5159 if (i.prefix[REX_PREFIX] || i.rex_encoding)
5160 {
5161 as_bad (_("REX prefix invalid with `%s'"), i.tm.name);
5162 return;
5163 }
5164
5165 if (i.tm.opcode_modifier.vex)
5166 build_vex_prefix (t);
5167 else
5168 build_evex_prefix ();
5169
5170 /* The individual REX.RXBW bits got consumed. */
5171 i.rex &= REX_OPCODE;
5172 }
5173
5174 /* Handle conversion of 'int $3' --> special int3 insn. XOP or FMA4
5175 instructions may define INT_OPCODE as well, so avoid this corner
5176 case for those instructions that use MODRM. */
5177 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
5178 && i.tm.base_opcode == INT_OPCODE
5179 && !i.tm.opcode_modifier.modrm
5180 && i.op[0].imms->X_add_number == 3)
5181 {
5182 i.tm.base_opcode = INT3_OPCODE;
5183 i.imm_operands = 0;
5184 }
5185
5186 if ((i.tm.opcode_modifier.jump == JUMP
5187 || i.tm.opcode_modifier.jump == JUMP_BYTE
5188 || i.tm.opcode_modifier.jump == JUMP_DWORD)
5189 && i.op[0].disps->X_op == O_constant)
5190 {
5191 /* Convert "jmp constant" (and "call constant") to a jump (call) to
5192 the absolute address given by the constant. Since ix86 jumps and
5193 calls are pc relative, we need to generate a reloc. */
5194 i.op[0].disps->X_add_symbol = &abs_symbol;
5195 i.op[0].disps->X_op = O_symbol;
5196 }
5197
5198 /* For 8 bit registers we need an empty rex prefix. Also if the
5199 instruction already has a prefix, we need to convert old
5200 registers to new ones. */
5201
5202 if ((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte
5203 && (i.op[0].regs->reg_flags & RegRex64) != 0)
5204 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte
5205 && (i.op[1].regs->reg_flags & RegRex64) != 0)
5206 || (((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte)
5207 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte))
5208 && i.rex != 0))
5209 {
5210 int x;
5211
5212 i.rex |= REX_OPCODE;
5213 for (x = 0; x < 2; x++)
5214 {
5215 /* Look for 8 bit operand that uses old registers. */
5216 if (i.types[x].bitfield.class == Reg && i.types[x].bitfield.byte
5217 && (i.op[x].regs->reg_flags & RegRex64) == 0)
5218 {
5219 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
5220 /* In case it is "hi" register, give up. */
5221 if (i.op[x].regs->reg_num > 3)
5222 as_bad (_("can't encode register '%s%s' in an "
5223 "instruction requiring REX prefix."),
5224 register_prefix, i.op[x].regs->reg_name);
5225
5226 /* Otherwise it is equivalent to the extended register.
5227 Since the encoding doesn't change this is merely
5228 cosmetic cleanup for debug output. */
5229
5230 i.op[x].regs = i.op[x].regs + 8;
5231 }
5232 }
5233 }
5234
5235 if (i.rex == 0 && i.rex_encoding)
5236 {
5237 /* Check if we can add a REX_OPCODE byte. Look for 8 bit operand
5238 that uses legacy register. If it is "hi" register, don't add
5239 the REX_OPCODE byte. */
5240 int x;
5241 for (x = 0; x < 2; x++)
5242 if (i.types[x].bitfield.class == Reg
5243 && i.types[x].bitfield.byte
5244 && (i.op[x].regs->reg_flags & RegRex64) == 0
5245 && i.op[x].regs->reg_num > 3)
5246 {
5247 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
5248 i.rex_encoding = false;
5249 break;
5250 }
5251
5252 if (i.rex_encoding)
5253 i.rex = REX_OPCODE;
5254 }
5255
5256 if (i.rex != 0)
5257 add_prefix (REX_OPCODE | i.rex);
5258
5259 insert_lfence_before ();
5260
5261 /* We are ready to output the insn. */
5262 output_insn ();
5263
5264 insert_lfence_after ();
5265
5266 last_insn.seg = now_seg;
5267
5268 if (i.tm.opcode_modifier.isprefix)
5269 {
5270 last_insn.kind = last_insn_prefix;
5271 last_insn.name = i.tm.name;
5272 last_insn.file = as_where (&last_insn.line);
5273 }
5274 else
5275 last_insn.kind = last_insn_other;
5276 }
5277
5278 static char *
5279 parse_insn (char *line, char *mnemonic)
5280 {
5281 char *l = line;
5282 char *token_start = l;
5283 char *mnem_p;
5284 int supported;
5285 const insn_template *t;
5286 char *dot_p = NULL;
5287
5288 while (1)
5289 {
5290 mnem_p = mnemonic;
5291 while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
5292 {
5293 if (*mnem_p == '.')
5294 dot_p = mnem_p;
5295 mnem_p++;
5296 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
5297 {
5298 as_bad (_("no such instruction: `%s'"), token_start);
5299 return NULL;
5300 }
5301 l++;
5302 }
5303 if (!is_space_char (*l)
5304 && *l != END_OF_INSN
5305 && (intel_syntax
5306 || (*l != PREFIX_SEPARATOR
5307 && *l != ',')))
5308 {
5309 as_bad (_("invalid character %s in mnemonic"),
5310 output_invalid (*l));
5311 return NULL;
5312 }
5313 if (token_start == l)
5314 {
5315 if (!intel_syntax && *l == PREFIX_SEPARATOR)
5316 as_bad (_("expecting prefix; got nothing"));
5317 else
5318 as_bad (_("expecting mnemonic; got nothing"));
5319 return NULL;
5320 }
5321
5322 /* Look up instruction (or prefix) via hash table. */
5323 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
5324
5325 if (*l != END_OF_INSN
5326 && (!is_space_char (*l) || l[1] != END_OF_INSN)
5327 && current_templates
5328 && current_templates->start->opcode_modifier.isprefix)
5329 {
5330 if (!cpu_flags_check_cpu64 (current_templates->start->cpu_flags))
5331 {
5332 as_bad ((flag_code != CODE_64BIT
5333 ? _("`%s' is only supported in 64-bit mode")
5334 : _("`%s' is not supported in 64-bit mode")),
5335 current_templates->start->name);
5336 return NULL;
5337 }
5338 /* If we are in 16-bit mode, do not allow addr16 or data16.
5339 Similarly, in 32-bit mode, do not allow addr32 or data32. */
5340 if ((current_templates->start->opcode_modifier.size == SIZE16
5341 || current_templates->start->opcode_modifier.size == SIZE32)
5342 && flag_code != CODE_64BIT
5343 && ((current_templates->start->opcode_modifier.size == SIZE32)
5344 ^ (flag_code == CODE_16BIT)))
5345 {
5346 as_bad (_("redundant %s prefix"),
5347 current_templates->start->name);
5348 return NULL;
5349 }
5350
5351 if (current_templates->start->base_opcode == PSEUDO_PREFIX)
5352 {
5353 /* Handle pseudo prefixes. */
5354 switch (current_templates->start->extension_opcode)
5355 {
5356 case Prefix_Disp8:
5357 /* {disp8} */
5358 i.disp_encoding = disp_encoding_8bit;
5359 break;
5360 case Prefix_Disp16:
5361 /* {disp16} */
5362 i.disp_encoding = disp_encoding_16bit;
5363 break;
5364 case Prefix_Disp32:
5365 /* {disp32} */
5366 i.disp_encoding = disp_encoding_32bit;
5367 break;
5368 case Prefix_Load:
5369 /* {load} */
5370 i.dir_encoding = dir_encoding_load;
5371 break;
5372 case Prefix_Store:
5373 /* {store} */
5374 i.dir_encoding = dir_encoding_store;
5375 break;
5376 case Prefix_VEX:
5377 /* {vex} */
5378 i.vec_encoding = vex_encoding_vex;
5379 break;
5380 case Prefix_VEX3:
5381 /* {vex3} */
5382 i.vec_encoding = vex_encoding_vex3;
5383 break;
5384 case Prefix_EVEX:
5385 /* {evex} */
5386 i.vec_encoding = vex_encoding_evex;
5387 break;
5388 case Prefix_REX:
5389 /* {rex} */
5390 i.rex_encoding = true;
5391 break;
5392 case Prefix_NoOptimize:
5393 /* {nooptimize} */
5394 i.no_optimize = true;
5395 break;
5396 default:
5397 abort ();
5398 }
5399 }
5400 else
5401 {
5402 /* Add prefix, checking for repeated prefixes. */
5403 switch (add_prefix (current_templates->start->base_opcode))
5404 {
5405 case PREFIX_EXIST:
5406 return NULL;
5407 case PREFIX_DS:
5408 if (current_templates->start->cpu_flags.bitfield.cpuibt)
5409 i.notrack_prefix = current_templates->start->name;
5410 break;
5411 case PREFIX_REP:
5412 if (current_templates->start->cpu_flags.bitfield.cpuhle)
5413 i.hle_prefix = current_templates->start->name;
5414 else if (current_templates->start->cpu_flags.bitfield.cpumpx)
5415 i.bnd_prefix = current_templates->start->name;
5416 else
5417 i.rep_prefix = current_templates->start->name;
5418 break;
5419 default:
5420 break;
5421 }
5422 }
5423 /* Skip past PREFIX_SEPARATOR and reset token_start. */
5424 token_start = ++l;
5425 }
5426 else
5427 break;
5428 }
5429
5430 if (!current_templates)
5431 {
5432 /* Deprecated functionality (new code should use pseudo-prefixes instead):
5433 Check if we should swap operand or force 32bit displacement in
5434 encoding. */
5435 if (mnem_p - 2 == dot_p && dot_p[1] == 's')
5436 i.dir_encoding = dir_encoding_swap;
5437 else if (mnem_p - 3 == dot_p
5438 && dot_p[1] == 'd'
5439 && dot_p[2] == '8')
5440 i.disp_encoding = disp_encoding_8bit;
5441 else if (mnem_p - 4 == dot_p
5442 && dot_p[1] == 'd'
5443 && dot_p[2] == '3'
5444 && dot_p[3] == '2')
5445 i.disp_encoding = disp_encoding_32bit;
5446 else
5447 goto check_suffix;
5448 mnem_p = dot_p;
5449 *dot_p = '\0';
5450 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
5451 }
5452
5453 if (!current_templates)
5454 {
5455 check_suffix:
5456 if (mnem_p > mnemonic)
5457 {
5458 /* See if we can get a match by trimming off a suffix. */
5459 switch (mnem_p[-1])
5460 {
5461 case WORD_MNEM_SUFFIX:
5462 if (intel_syntax && (intel_float_operand (mnemonic) & 2))
5463 i.suffix = SHORT_MNEM_SUFFIX;
5464 else
5465 /* Fall through. */
5466 case BYTE_MNEM_SUFFIX:
5467 case QWORD_MNEM_SUFFIX:
5468 i.suffix = mnem_p[-1];
5469 mnem_p[-1] = '\0';
5470 current_templates
5471 = (const templates *) str_hash_find (op_hash, mnemonic);
5472 break;
5473 case SHORT_MNEM_SUFFIX:
5474 case LONG_MNEM_SUFFIX:
5475 if (!intel_syntax)
5476 {
5477 i.suffix = mnem_p[-1];
5478 mnem_p[-1] = '\0';
5479 current_templates
5480 = (const templates *) str_hash_find (op_hash, mnemonic);
5481 }
5482 break;
5483
5484 /* Intel Syntax. */
5485 case 'd':
5486 if (intel_syntax)
5487 {
5488 if (intel_float_operand (mnemonic) == 1)
5489 i.suffix = SHORT_MNEM_SUFFIX;
5490 else
5491 i.suffix = LONG_MNEM_SUFFIX;
5492 mnem_p[-1] = '\0';
5493 current_templates
5494 = (const templates *) str_hash_find (op_hash, mnemonic);
5495 }
5496 break;
5497 }
5498 }
5499
5500 if (!current_templates)
5501 {
5502 as_bad (_("no such instruction: `%s'"), token_start);
5503 return NULL;
5504 }
5505 }
5506
5507 if (current_templates->start->opcode_modifier.jump == JUMP
5508 || current_templates->start->opcode_modifier.jump == JUMP_BYTE)
5509 {
5510 /* Check for a branch hint. We allow ",pt" and ",pn" for
5511 predict taken and predict not taken respectively.
5512 I'm not sure that branch hints actually do anything on loop
5513 and jcxz insns (JumpByte) for current Pentium4 chips. They
5514 may work in the future and it doesn't hurt to accept them
5515 now. */
5516 if (l[0] == ',' && l[1] == 'p')
5517 {
5518 if (l[2] == 't')
5519 {
5520 if (!add_prefix (DS_PREFIX_OPCODE))
5521 return NULL;
5522 l += 3;
5523 }
5524 else if (l[2] == 'n')
5525 {
5526 if (!add_prefix (CS_PREFIX_OPCODE))
5527 return NULL;
5528 l += 3;
5529 }
5530 }
5531 }
5532 /* Any other comma loses. */
5533 if (*l == ',')
5534 {
5535 as_bad (_("invalid character %s in mnemonic"),
5536 output_invalid (*l));
5537 return NULL;
5538 }
5539
5540 /* Check if instruction is supported on specified architecture. */
5541 supported = 0;
5542 for (t = current_templates->start; t < current_templates->end; ++t)
5543 {
5544 supported |= cpu_flags_match (t);
5545 if (supported == CPU_FLAGS_PERFECT_MATCH)
5546 {
5547 if (!cpu_arch_flags.bitfield.cpui386 && (flag_code != CODE_16BIT))
5548 as_warn (_("use .code16 to ensure correct addressing mode"));
5549
5550 return l;
5551 }
5552 }
5553
5554 if (!(supported & CPU_FLAGS_64BIT_MATCH))
5555 as_bad (flag_code == CODE_64BIT
5556 ? _("`%s' is not supported in 64-bit mode")
5557 : _("`%s' is only supported in 64-bit mode"),
5558 current_templates->start->name);
5559 else
5560 as_bad (_("`%s' is not supported on `%s%s'"),
5561 current_templates->start->name,
5562 cpu_arch_name ? cpu_arch_name : default_arch,
5563 cpu_sub_arch_name ? cpu_sub_arch_name : "");
5564
5565 return NULL;
5566 }
5567
5568 static char *
5569 parse_operands (char *l, const char *mnemonic)
5570 {
5571 char *token_start;
5572
5573 /* 1 if operand is pending after ','. */
5574 unsigned int expecting_operand = 0;
5575
5576 while (*l != END_OF_INSN)
5577 {
5578 /* Non-zero if operand parens not balanced. */
5579 unsigned int paren_not_balanced = 0;
5580 /* True if inside double quotes. */
5581 bool in_quotes = false;
5582
5583 /* Skip optional white space before operand. */
5584 if (is_space_char (*l))
5585 ++l;
5586 if (!is_operand_char (*l) && *l != END_OF_INSN && *l != '"')
5587 {
5588 as_bad (_("invalid character %s before operand %d"),
5589 output_invalid (*l),
5590 i.operands + 1);
5591 return NULL;
5592 }
5593 token_start = l; /* After white space. */
5594 while (in_quotes || paren_not_balanced || *l != ',')
5595 {
5596 if (*l == END_OF_INSN)
5597 {
5598 if (in_quotes)
5599 {
5600 as_bad (_("unbalanced double quotes in operand %d."),
5601 i.operands + 1);
5602 return NULL;
5603 }
5604 if (paren_not_balanced)
5605 {
5606 know (!intel_syntax);
5607 as_bad (_("unbalanced parenthesis in operand %d."),
5608 i.operands + 1);
5609 return NULL;
5610 }
5611 else
5612 break; /* we are done */
5613 }
5614 else if (*l == '\\' && l[1] == '"')
5615 ++l;
5616 else if (*l == '"')
5617 in_quotes = !in_quotes;
5618 else if (!in_quotes && !is_operand_char (*l) && !is_space_char (*l))
5619 {
5620 as_bad (_("invalid character %s in operand %d"),
5621 output_invalid (*l),
5622 i.operands + 1);
5623 return NULL;
5624 }
5625 if (!intel_syntax && !in_quotes)
5626 {
5627 if (*l == '(')
5628 ++paren_not_balanced;
5629 if (*l == ')')
5630 --paren_not_balanced;
5631 }
5632 l++;
5633 }
5634 if (l != token_start)
5635 { /* Yes, we've read in another operand. */
5636 unsigned int operand_ok;
5637 this_operand = i.operands++;
5638 if (i.operands > MAX_OPERANDS)
5639 {
5640 as_bad (_("spurious operands; (%d operands/instruction max)"),
5641 MAX_OPERANDS);
5642 return NULL;
5643 }
5644 i.types[this_operand].bitfield.unspecified = 1;
5645 /* Now parse operand adding info to 'i' as we go along. */
5646 END_STRING_AND_SAVE (l);
5647
5648 if (i.mem_operands > 1)
5649 {
5650 as_bad (_("too many memory references for `%s'"),
5651 mnemonic);
5652 return 0;
5653 }
5654
5655 if (intel_syntax)
5656 operand_ok =
5657 i386_intel_operand (token_start,
5658 intel_float_operand (mnemonic));
5659 else
5660 operand_ok = i386_att_operand (token_start);
5661
5662 RESTORE_END_STRING (l);
5663 if (!operand_ok)
5664 return NULL;
5665 }
5666 else
5667 {
5668 if (expecting_operand)
5669 {
5670 expecting_operand_after_comma:
5671 as_bad (_("expecting operand after ','; got nothing"));
5672 return NULL;
5673 }
5674 if (*l == ',')
5675 {
5676 as_bad (_("expecting operand before ','; got nothing"));
5677 return NULL;
5678 }
5679 }
5680
5681 /* Now *l must be either ',' or END_OF_INSN. */
5682 if (*l == ',')
5683 {
5684 if (*++l == END_OF_INSN)
5685 {
5686 /* Just skip it, if it's \n complain. */
5687 goto expecting_operand_after_comma;
5688 }
5689 expecting_operand = 1;
5690 }
5691 }
5692 return l;
5693 }
5694
5695 static void
5696 swap_2_operands (unsigned int xchg1, unsigned int xchg2)
5697 {
5698 union i386_op temp_op;
5699 i386_operand_type temp_type;
5700 unsigned int temp_flags;
5701 enum bfd_reloc_code_real temp_reloc;
5702
5703 temp_type = i.types[xchg2];
5704 i.types[xchg2] = i.types[xchg1];
5705 i.types[xchg1] = temp_type;
5706
5707 temp_flags = i.flags[xchg2];
5708 i.flags[xchg2] = i.flags[xchg1];
5709 i.flags[xchg1] = temp_flags;
5710
5711 temp_op = i.op[xchg2];
5712 i.op[xchg2] = i.op[xchg1];
5713 i.op[xchg1] = temp_op;
5714
5715 temp_reloc = i.reloc[xchg2];
5716 i.reloc[xchg2] = i.reloc[xchg1];
5717 i.reloc[xchg1] = temp_reloc;
5718
5719 if (i.mask.reg)
5720 {
5721 if (i.mask.operand == xchg1)
5722 i.mask.operand = xchg2;
5723 else if (i.mask.operand == xchg2)
5724 i.mask.operand = xchg1;
5725 }
5726 if (i.broadcast.type)
5727 {
5728 if (i.broadcast.operand == xchg1)
5729 i.broadcast.operand = xchg2;
5730 else if (i.broadcast.operand == xchg2)
5731 i.broadcast.operand = xchg1;
5732 }
5733 if (i.rounding.type != rc_none)
5734 {
5735 if (i.rounding.operand == xchg1)
5736 i.rounding.operand = xchg2;
5737 else if (i.rounding.operand == xchg2)
5738 i.rounding.operand = xchg1;
5739 }
5740 }
5741
5742 static void
5743 swap_operands (void)
5744 {
5745 switch (i.operands)
5746 {
5747 case 5:
5748 case 4:
5749 swap_2_operands (1, i.operands - 2);
5750 /* Fall through. */
5751 case 3:
5752 case 2:
5753 swap_2_operands (0, i.operands - 1);
5754 break;
5755 default:
5756 abort ();
5757 }
5758
5759 if (i.mem_operands == 2)
5760 {
5761 const reg_entry *temp_seg;
5762 temp_seg = i.seg[0];
5763 i.seg[0] = i.seg[1];
5764 i.seg[1] = temp_seg;
5765 }
5766 }
5767
5768 /* Try to ensure constant immediates are represented in the smallest
5769 opcode possible. */
5770 static void
5771 optimize_imm (void)
5772 {
5773 char guess_suffix = 0;
5774 int op;
5775
5776 if (i.suffix)
5777 guess_suffix = i.suffix;
5778 else if (i.reg_operands)
5779 {
5780 /* Figure out a suffix from the last register operand specified.
5781 We can't do this properly yet, i.e. excluding special register
5782 instances, but the following works for instructions with
5783 immediates. In any case, we can't set i.suffix yet. */
5784 for (op = i.operands; --op >= 0;)
5785 if (i.types[op].bitfield.class != Reg)
5786 continue;
5787 else if (i.types[op].bitfield.byte)
5788 {
5789 guess_suffix = BYTE_MNEM_SUFFIX;
5790 break;
5791 }
5792 else if (i.types[op].bitfield.word)
5793 {
5794 guess_suffix = WORD_MNEM_SUFFIX;
5795 break;
5796 }
5797 else if (i.types[op].bitfield.dword)
5798 {
5799 guess_suffix = LONG_MNEM_SUFFIX;
5800 break;
5801 }
5802 else if (i.types[op].bitfield.qword)
5803 {
5804 guess_suffix = QWORD_MNEM_SUFFIX;
5805 break;
5806 }
5807 }
5808 else if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
5809 guess_suffix = WORD_MNEM_SUFFIX;
5810
5811 for (op = i.operands; --op >= 0;)
5812 if (operand_type_check (i.types[op], imm))
5813 {
5814 switch (i.op[op].imms->X_op)
5815 {
5816 case O_constant:
5817 /* If a suffix is given, this operand may be shortened. */
5818 switch (guess_suffix)
5819 {
5820 case LONG_MNEM_SUFFIX:
5821 i.types[op].bitfield.imm32 = 1;
5822 i.types[op].bitfield.imm64 = 1;
5823 break;
5824 case WORD_MNEM_SUFFIX:
5825 i.types[op].bitfield.imm16 = 1;
5826 i.types[op].bitfield.imm32 = 1;
5827 i.types[op].bitfield.imm32s = 1;
5828 i.types[op].bitfield.imm64 = 1;
5829 break;
5830 case BYTE_MNEM_SUFFIX:
5831 i.types[op].bitfield.imm8 = 1;
5832 i.types[op].bitfield.imm8s = 1;
5833 i.types[op].bitfield.imm16 = 1;
5834 i.types[op].bitfield.imm32 = 1;
5835 i.types[op].bitfield.imm32s = 1;
5836 i.types[op].bitfield.imm64 = 1;
5837 break;
5838 }
5839
5840 /* If this operand is at most 16 bits, convert it
5841 to a signed 16 bit number before trying to see
5842 whether it will fit in an even smaller size.
5843 This allows a 16-bit operand such as $0xffe0 to
5844 be recognised as within Imm8S range. */
5845 if ((i.types[op].bitfield.imm16)
5846 && fits_in_unsigned_word (i.op[op].imms->X_add_number))
5847 {
5848 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
5849 ^ 0x8000) - 0x8000);
5850 }
5851 #ifdef BFD64
5852 /* Store 32-bit immediate in 64-bit for 64-bit BFD. */
5853 if ((i.types[op].bitfield.imm32)
5854 && fits_in_unsigned_long (i.op[op].imms->X_add_number))
5855 {
5856 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
5857 ^ ((offsetT) 1 << 31))
5858 - ((offsetT) 1 << 31));
5859 }
5860 #endif
5861 i.types[op]
5862 = operand_type_or (i.types[op],
5863 smallest_imm_type (i.op[op].imms->X_add_number));
5864
5865 /* We must avoid matching of Imm32 templates when 64bit
5866 only immediate is available. */
5867 if (guess_suffix == QWORD_MNEM_SUFFIX)
5868 i.types[op].bitfield.imm32 = 0;
5869 break;
5870
5871 case O_absent:
5872 case O_register:
5873 abort ();
5874
5875 /* Symbols and expressions. */
5876 default:
5877 /* Convert symbolic operand to proper sizes for matching, but don't
5878 prevent matching a set of insns that only supports sizes other
5879 than those matching the insn suffix. */
5880 {
5881 i386_operand_type mask, allowed;
5882 const insn_template *t = current_templates->start;
5883
5884 operand_type_set (&mask, 0);
5885 allowed = t->operand_types[op];
5886
5887 while (++t < current_templates->end)
5888 {
5889 allowed = operand_type_and (allowed, anyimm);
5890 allowed = operand_type_or (allowed, t->operand_types[op]);
5891 }
5892 switch (guess_suffix)
5893 {
5894 case QWORD_MNEM_SUFFIX:
5895 mask.bitfield.imm64 = 1;
5896 mask.bitfield.imm32s = 1;
5897 break;
5898 case LONG_MNEM_SUFFIX:
5899 mask.bitfield.imm32 = 1;
5900 break;
5901 case WORD_MNEM_SUFFIX:
5902 mask.bitfield.imm16 = 1;
5903 break;
5904 case BYTE_MNEM_SUFFIX:
5905 mask.bitfield.imm8 = 1;
5906 break;
5907 default:
5908 break;
5909 }
5910 allowed = operand_type_and (mask, allowed);
5911 if (!operand_type_all_zero (&allowed))
5912 i.types[op] = operand_type_and (i.types[op], mask);
5913 }
5914 break;
5915 }
5916 }
5917 }
5918
5919 /* Try to use the smallest displacement type too. */
5920 static void
5921 optimize_disp (void)
5922 {
5923 int op;
5924
5925 for (op = i.operands; --op >= 0;)
5926 if (operand_type_check (i.types[op], disp))
5927 {
5928 if (i.op[op].disps->X_op == O_constant)
5929 {
5930 offsetT op_disp = i.op[op].disps->X_add_number;
5931
5932 if (!op_disp && i.types[op].bitfield.baseindex)
5933 {
5934 i.types[op] = operand_type_and_not (i.types[op], anydisp);
5935 i.op[op].disps = NULL;
5936 i.disp_operands--;
5937 continue;
5938 }
5939
5940 if (i.types[op].bitfield.disp16
5941 && fits_in_unsigned_word (op_disp))
5942 {
5943 /* If this operand is at most 16 bits, convert
5944 to a signed 16 bit number and don't use 64bit
5945 displacement. */
5946 op_disp = ((op_disp ^ 0x8000) - 0x8000);
5947 i.types[op].bitfield.disp64 = 0;
5948 }
5949
5950 #ifdef BFD64
5951 /* Optimize 64-bit displacement to 32-bit for 64-bit BFD. */
5952 if ((i.types[op].bitfield.disp32
5953 || (flag_code == CODE_64BIT
5954 && want_disp32 (current_templates->start)))
5955 && fits_in_unsigned_long (op_disp))
5956 {
5957 /* If this operand is at most 32 bits, convert
5958 to a signed 32 bit number and don't use 64bit
5959 displacement. */
5960 op_disp = (op_disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
5961 i.types[op].bitfield.disp64 = 0;
5962 i.types[op].bitfield.disp32 = 1;
5963 }
5964
5965 if (flag_code == CODE_64BIT && fits_in_signed_long (op_disp))
5966 {
5967 i.types[op].bitfield.disp64 = 0;
5968 i.types[op].bitfield.disp32s = 1;
5969 }
5970 #endif
5971 if ((i.types[op].bitfield.disp32
5972 || i.types[op].bitfield.disp32s
5973 || i.types[op].bitfield.disp16)
5974 && fits_in_disp8 (op_disp))
5975 i.types[op].bitfield.disp8 = 1;
5976
5977 i.op[op].disps->X_add_number = op_disp;
5978 }
5979 else if (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
5980 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL)
5981 {
5982 fix_new_exp (frag_now, frag_more (0) - frag_now->fr_literal, 0,
5983 i.op[op].disps, 0, i.reloc[op]);
5984 i.types[op] = operand_type_and_not (i.types[op], anydisp);
5985 }
5986 else
5987 /* We only support 64bit displacement on constants. */
5988 i.types[op].bitfield.disp64 = 0;
5989 }
5990 }
5991
5992 /* Return 1 if there is a match in broadcast bytes between operand
5993 GIVEN and instruction template T. */
5994
5995 static INLINE int
5996 match_broadcast_size (const insn_template *t, unsigned int given)
5997 {
5998 return ((t->opcode_modifier.broadcast == BYTE_BROADCAST
5999 && i.types[given].bitfield.byte)
6000 || (t->opcode_modifier.broadcast == WORD_BROADCAST
6001 && i.types[given].bitfield.word)
6002 || (t->opcode_modifier.broadcast == DWORD_BROADCAST
6003 && i.types[given].bitfield.dword)
6004 || (t->opcode_modifier.broadcast == QWORD_BROADCAST
6005 && i.types[given].bitfield.qword));
6006 }
6007
6008 /* Check if operands are valid for the instruction. */
6009
6010 static int
6011 check_VecOperands (const insn_template *t)
6012 {
6013 unsigned int op;
6014 i386_cpu_flags cpu;
6015
6016 /* Templates allowing for ZMMword as well as YMMword and/or XMMword for
6017 any one operand are implicity requiring AVX512VL support if the actual
6018 operand size is YMMword or XMMword. Since this function runs after
6019 template matching, there's no need to check for YMMword/XMMword in
6020 the template. */
6021 cpu = cpu_flags_and (t->cpu_flags, avx512);
6022 if (!cpu_flags_all_zero (&cpu)
6023 && !t->cpu_flags.bitfield.cpuavx512vl
6024 && !cpu_arch_flags.bitfield.cpuavx512vl)
6025 {
6026 for (op = 0; op < t->operands; ++op)
6027 {
6028 if (t->operand_types[op].bitfield.zmmword
6029 && (i.types[op].bitfield.ymmword
6030 || i.types[op].bitfield.xmmword))
6031 {
6032 i.error = unsupported;
6033 return 1;
6034 }
6035 }
6036 }
6037
6038 /* Without VSIB byte, we can't have a vector register for index. */
6039 if (!t->opcode_modifier.sib
6040 && i.index_reg
6041 && (i.index_reg->reg_type.bitfield.xmmword
6042 || i.index_reg->reg_type.bitfield.ymmword
6043 || i.index_reg->reg_type.bitfield.zmmword))
6044 {
6045 i.error = unsupported_vector_index_register;
6046 return 1;
6047 }
6048
6049 /* Check if default mask is allowed. */
6050 if (t->opcode_modifier.nodefmask
6051 && (!i.mask.reg || i.mask.reg->reg_num == 0))
6052 {
6053 i.error = no_default_mask;
6054 return 1;
6055 }
6056
6057 /* For VSIB byte, we need a vector register for index, and all vector
6058 registers must be distinct. */
6059 if (t->opcode_modifier.sib && t->opcode_modifier.sib != SIBMEM)
6060 {
6061 if (!i.index_reg
6062 || !((t->opcode_modifier.sib == VECSIB128
6063 && i.index_reg->reg_type.bitfield.xmmword)
6064 || (t->opcode_modifier.sib == VECSIB256
6065 && i.index_reg->reg_type.bitfield.ymmword)
6066 || (t->opcode_modifier.sib == VECSIB512
6067 && i.index_reg->reg_type.bitfield.zmmword)))
6068 {
6069 i.error = invalid_vsib_address;
6070 return 1;
6071 }
6072
6073 gas_assert (i.reg_operands == 2 || i.mask.reg);
6074 if (i.reg_operands == 2 && !i.mask.reg)
6075 {
6076 gas_assert (i.types[0].bitfield.class == RegSIMD);
6077 gas_assert (i.types[0].bitfield.xmmword
6078 || i.types[0].bitfield.ymmword);
6079 gas_assert (i.types[2].bitfield.class == RegSIMD);
6080 gas_assert (i.types[2].bitfield.xmmword
6081 || i.types[2].bitfield.ymmword);
6082 if (operand_check == check_none)
6083 return 0;
6084 if (register_number (i.op[0].regs)
6085 != register_number (i.index_reg)
6086 && register_number (i.op[2].regs)
6087 != register_number (i.index_reg)
6088 && register_number (i.op[0].regs)
6089 != register_number (i.op[2].regs))
6090 return 0;
6091 if (operand_check == check_error)
6092 {
6093 i.error = invalid_vector_register_set;
6094 return 1;
6095 }
6096 as_warn (_("mask, index, and destination registers should be distinct"));
6097 }
6098 else if (i.reg_operands == 1 && i.mask.reg)
6099 {
6100 if (i.types[1].bitfield.class == RegSIMD
6101 && (i.types[1].bitfield.xmmword
6102 || i.types[1].bitfield.ymmword
6103 || i.types[1].bitfield.zmmword)
6104 && (register_number (i.op[1].regs)
6105 == register_number (i.index_reg)))
6106 {
6107 if (operand_check == check_error)
6108 {
6109 i.error = invalid_vector_register_set;
6110 return 1;
6111 }
6112 if (operand_check != check_none)
6113 as_warn (_("index and destination registers should be distinct"));
6114 }
6115 }
6116 }
6117
6118 /* For AMX instructions with 3 TMM register operands, all operands
6119 must be distinct. */
6120 if (i.reg_operands == 3
6121 && t->operand_types[0].bitfield.tmmword
6122 && (i.op[0].regs == i.op[1].regs
6123 || i.op[0].regs == i.op[2].regs
6124 || i.op[1].regs == i.op[2].regs))
6125 {
6126 i.error = invalid_tmm_register_set;
6127 return 1;
6128 }
6129
6130 /* For some special instructions require that destination must be distinct
6131 from source registers. */
6132 if (t->opcode_modifier.distinctdest)
6133 {
6134 unsigned int dest_reg = i.operands - 1;
6135
6136 know (i.operands >= 3);
6137
6138 /* #UD if dest_reg == src1_reg or dest_reg == src2_reg. */
6139 if (i.op[dest_reg - 1].regs == i.op[dest_reg].regs
6140 || (i.reg_operands > 2
6141 && i.op[dest_reg - 2].regs == i.op[dest_reg].regs))
6142 {
6143 i.error = invalid_dest_and_src_register_set;
6144 return 1;
6145 }
6146 }
6147
6148 /* Check if broadcast is supported by the instruction and is applied
6149 to the memory operand. */
6150 if (i.broadcast.type)
6151 {
6152 i386_operand_type type, overlap;
6153
6154 /* Check if specified broadcast is supported in this instruction,
6155 and its broadcast bytes match the memory operand. */
6156 op = i.broadcast.operand;
6157 if (!t->opcode_modifier.broadcast
6158 || !(i.flags[op] & Operand_Mem)
6159 || (!i.types[op].bitfield.unspecified
6160 && !match_broadcast_size (t, op)))
6161 {
6162 bad_broadcast:
6163 i.error = unsupported_broadcast;
6164 return 1;
6165 }
6166
6167 i.broadcast.bytes = ((1 << (t->opcode_modifier.broadcast - 1))
6168 * i.broadcast.type);
6169 operand_type_set (&type, 0);
6170 switch (i.broadcast.bytes)
6171 {
6172 case 2:
6173 type.bitfield.word = 1;
6174 break;
6175 case 4:
6176 type.bitfield.dword = 1;
6177 break;
6178 case 8:
6179 type.bitfield.qword = 1;
6180 break;
6181 case 16:
6182 type.bitfield.xmmword = 1;
6183 break;
6184 case 32:
6185 type.bitfield.ymmword = 1;
6186 break;
6187 case 64:
6188 type.bitfield.zmmword = 1;
6189 break;
6190 default:
6191 goto bad_broadcast;
6192 }
6193
6194 overlap = operand_type_and (type, t->operand_types[op]);
6195 if (t->operand_types[op].bitfield.class == RegSIMD
6196 && t->operand_types[op].bitfield.byte
6197 + t->operand_types[op].bitfield.word
6198 + t->operand_types[op].bitfield.dword
6199 + t->operand_types[op].bitfield.qword > 1)
6200 {
6201 overlap.bitfield.xmmword = 0;
6202 overlap.bitfield.ymmword = 0;
6203 overlap.bitfield.zmmword = 0;
6204 }
6205 if (operand_type_all_zero (&overlap))
6206 goto bad_broadcast;
6207
6208 if (t->opcode_modifier.checkregsize)
6209 {
6210 unsigned int j;
6211
6212 type.bitfield.baseindex = 1;
6213 for (j = 0; j < i.operands; ++j)
6214 {
6215 if (j != op
6216 && !operand_type_register_match(i.types[j],
6217 t->operand_types[j],
6218 type,
6219 t->operand_types[op]))
6220 goto bad_broadcast;
6221 }
6222 }
6223 }
6224 /* If broadcast is supported in this instruction, we need to check if
6225 operand of one-element size isn't specified without broadcast. */
6226 else if (t->opcode_modifier.broadcast && i.mem_operands)
6227 {
6228 /* Find memory operand. */
6229 for (op = 0; op < i.operands; op++)
6230 if (i.flags[op] & Operand_Mem)
6231 break;
6232 gas_assert (op < i.operands);
6233 /* Check size of the memory operand. */
6234 if (match_broadcast_size (t, op))
6235 {
6236 i.error = broadcast_needed;
6237 return 1;
6238 }
6239 }
6240 else
6241 op = MAX_OPERANDS - 1; /* Avoid uninitialized variable warning. */
6242
6243 /* Check if requested masking is supported. */
6244 if (i.mask.reg)
6245 {
6246 switch (t->opcode_modifier.masking)
6247 {
6248 case BOTH_MASKING:
6249 break;
6250 case MERGING_MASKING:
6251 if (i.mask.zeroing)
6252 {
6253 case 0:
6254 i.error = unsupported_masking;
6255 return 1;
6256 }
6257 break;
6258 case DYNAMIC_MASKING:
6259 /* Memory destinations allow only merging masking. */
6260 if (i.mask.zeroing && i.mem_operands)
6261 {
6262 /* Find memory operand. */
6263 for (op = 0; op < i.operands; op++)
6264 if (i.flags[op] & Operand_Mem)
6265 break;
6266 gas_assert (op < i.operands);
6267 if (op == i.operands - 1)
6268 {
6269 i.error = unsupported_masking;
6270 return 1;
6271 }
6272 }
6273 break;
6274 default:
6275 abort ();
6276 }
6277 }
6278
6279 /* Check if masking is applied to dest operand. */
6280 if (i.mask.reg && (i.mask.operand != i.operands - 1))
6281 {
6282 i.error = mask_not_on_destination;
6283 return 1;
6284 }
6285
6286 /* Check RC/SAE. */
6287 if (i.rounding.type != rc_none)
6288 {
6289 if (!t->opcode_modifier.sae
6290 || (i.rounding.type != saeonly && !t->opcode_modifier.staticrounding))
6291 {
6292 i.error = unsupported_rc_sae;
6293 return 1;
6294 }
6295 /* If the instruction has several immediate operands and one of
6296 them is rounding, the rounding operand should be the last
6297 immediate operand. */
6298 if (i.imm_operands > 1
6299 && i.rounding.operand != i.imm_operands - 1)
6300 {
6301 i.error = rc_sae_operand_not_last_imm;
6302 return 1;
6303 }
6304 }
6305
6306 /* Check the special Imm4 cases; must be the first operand. */
6307 if (t->cpu_flags.bitfield.cpuxop && t->operands == 5)
6308 {
6309 if (i.op[0].imms->X_op != O_constant
6310 || !fits_in_imm4 (i.op[0].imms->X_add_number))
6311 {
6312 i.error = bad_imm4;
6313 return 1;
6314 }
6315
6316 /* Turn off Imm<N> so that update_imm won't complain. */
6317 operand_type_set (&i.types[0], 0);
6318 }
6319
6320 /* Check vector Disp8 operand. */
6321 if (t->opcode_modifier.disp8memshift
6322 && i.disp_encoding != disp_encoding_32bit)
6323 {
6324 if (i.broadcast.type)
6325 i.memshift = t->opcode_modifier.broadcast - 1;
6326 else if (t->opcode_modifier.disp8memshift != DISP8_SHIFT_VL)
6327 i.memshift = t->opcode_modifier.disp8memshift;
6328 else
6329 {
6330 const i386_operand_type *type = NULL;
6331
6332 i.memshift = 0;
6333 for (op = 0; op < i.operands; op++)
6334 if (i.flags[op] & Operand_Mem)
6335 {
6336 if (t->opcode_modifier.evex == EVEXLIG)
6337 i.memshift = 2 + (i.suffix == QWORD_MNEM_SUFFIX);
6338 else if (t->operand_types[op].bitfield.xmmword
6339 + t->operand_types[op].bitfield.ymmword
6340 + t->operand_types[op].bitfield.zmmword <= 1)
6341 type = &t->operand_types[op];
6342 else if (!i.types[op].bitfield.unspecified)
6343 type = &i.types[op];
6344 }
6345 else if (i.types[op].bitfield.class == RegSIMD
6346 && t->opcode_modifier.evex != EVEXLIG)
6347 {
6348 if (i.types[op].bitfield.zmmword)
6349 i.memshift = 6;
6350 else if (i.types[op].bitfield.ymmword && i.memshift < 5)
6351 i.memshift = 5;
6352 else if (i.types[op].bitfield.xmmword && i.memshift < 4)
6353 i.memshift = 4;
6354 }
6355
6356 if (type)
6357 {
6358 if (type->bitfield.zmmword)
6359 i.memshift = 6;
6360 else if (type->bitfield.ymmword)
6361 i.memshift = 5;
6362 else if (type->bitfield.xmmword)
6363 i.memshift = 4;
6364 }
6365
6366 /* For the check in fits_in_disp8(). */
6367 if (i.memshift == 0)
6368 i.memshift = -1;
6369 }
6370
6371 for (op = 0; op < i.operands; op++)
6372 if (operand_type_check (i.types[op], disp)
6373 && i.op[op].disps->X_op == O_constant)
6374 {
6375 if (fits_in_disp8 (i.op[op].disps->X_add_number))
6376 {
6377 i.types[op].bitfield.disp8 = 1;
6378 return 0;
6379 }
6380 i.types[op].bitfield.disp8 = 0;
6381 }
6382 }
6383
6384 i.memshift = 0;
6385
6386 return 0;
6387 }
6388
6389 /* Check if encoding requirements are met by the instruction. */
6390
6391 static int
6392 VEX_check_encoding (const insn_template *t)
6393 {
6394 if (i.vec_encoding == vex_encoding_error)
6395 {
6396 i.error = unsupported;
6397 return 1;
6398 }
6399
6400 if (i.vec_encoding == vex_encoding_evex)
6401 {
6402 /* This instruction must be encoded with EVEX prefix. */
6403 if (!is_evex_encoding (t))
6404 {
6405 i.error = unsupported;
6406 return 1;
6407 }
6408 return 0;
6409 }
6410
6411 if (!t->opcode_modifier.vex)
6412 {
6413 /* This instruction template doesn't have VEX prefix. */
6414 if (i.vec_encoding != vex_encoding_default)
6415 {
6416 i.error = unsupported;
6417 return 1;
6418 }
6419 return 0;
6420 }
6421
6422 return 0;
6423 }
6424
6425 static const insn_template *
6426 match_template (char mnem_suffix)
6427 {
6428 /* Points to template once we've found it. */
6429 const insn_template *t;
6430 i386_operand_type overlap0, overlap1, overlap2, overlap3;
6431 i386_operand_type overlap4;
6432 unsigned int found_reverse_match;
6433 i386_opcode_modifier suffix_check;
6434 i386_operand_type operand_types [MAX_OPERANDS];
6435 int addr_prefix_disp;
6436 unsigned int j, size_match, check_register;
6437 enum i386_error specific_error = 0;
6438
6439 #if MAX_OPERANDS != 5
6440 # error "MAX_OPERANDS must be 5."
6441 #endif
6442
6443 found_reverse_match = 0;
6444 addr_prefix_disp = -1;
6445
6446 /* Prepare for mnemonic suffix check. */
6447 memset (&suffix_check, 0, sizeof (suffix_check));
6448 switch (mnem_suffix)
6449 {
6450 case BYTE_MNEM_SUFFIX:
6451 suffix_check.no_bsuf = 1;
6452 break;
6453 case WORD_MNEM_SUFFIX:
6454 suffix_check.no_wsuf = 1;
6455 break;
6456 case SHORT_MNEM_SUFFIX:
6457 suffix_check.no_ssuf = 1;
6458 break;
6459 case LONG_MNEM_SUFFIX:
6460 suffix_check.no_lsuf = 1;
6461 break;
6462 case QWORD_MNEM_SUFFIX:
6463 suffix_check.no_qsuf = 1;
6464 break;
6465 default:
6466 /* NB: In Intel syntax, normally we can check for memory operand
6467 size when there is no mnemonic suffix. But jmp and call have
6468 2 different encodings with Dword memory operand size, one with
6469 No_ldSuf and the other without. i.suffix is set to
6470 LONG_DOUBLE_MNEM_SUFFIX to skip the one with No_ldSuf. */
6471 if (i.suffix == LONG_DOUBLE_MNEM_SUFFIX)
6472 suffix_check.no_ldsuf = 1;
6473 }
6474
6475 /* Must have right number of operands. */
6476 i.error = number_of_operands_mismatch;
6477
6478 for (t = current_templates->start; t < current_templates->end; t++)
6479 {
6480 addr_prefix_disp = -1;
6481 found_reverse_match = 0;
6482
6483 if (i.operands != t->operands)
6484 continue;
6485
6486 /* Check processor support. */
6487 i.error = unsupported;
6488 if (cpu_flags_match (t) != CPU_FLAGS_PERFECT_MATCH)
6489 continue;
6490
6491 /* Check Pseudo Prefix. */
6492 i.error = unsupported;
6493 if (t->opcode_modifier.pseudovexprefix
6494 && !(i.vec_encoding == vex_encoding_vex
6495 || i.vec_encoding == vex_encoding_vex3))
6496 continue;
6497
6498 /* Check AT&T mnemonic. */
6499 i.error = unsupported_with_intel_mnemonic;
6500 if (intel_mnemonic && t->opcode_modifier.attmnemonic)
6501 continue;
6502
6503 /* Check AT&T/Intel syntax. */
6504 i.error = unsupported_syntax;
6505 if ((intel_syntax && t->opcode_modifier.attsyntax)
6506 || (!intel_syntax && t->opcode_modifier.intelsyntax))
6507 continue;
6508
6509 /* Check Intel64/AMD64 ISA. */
6510 switch (isa64)
6511 {
6512 default:
6513 /* Default: Don't accept Intel64. */
6514 if (t->opcode_modifier.isa64 == INTEL64)
6515 continue;
6516 break;
6517 case amd64:
6518 /* -mamd64: Don't accept Intel64 and Intel64 only. */
6519 if (t->opcode_modifier.isa64 >= INTEL64)
6520 continue;
6521 break;
6522 case intel64:
6523 /* -mintel64: Don't accept AMD64. */
6524 if (t->opcode_modifier.isa64 == AMD64 && flag_code == CODE_64BIT)
6525 continue;
6526 break;
6527 }
6528
6529 /* Check the suffix. */
6530 i.error = invalid_instruction_suffix;
6531 if ((t->opcode_modifier.no_bsuf && suffix_check.no_bsuf)
6532 || (t->opcode_modifier.no_wsuf && suffix_check.no_wsuf)
6533 || (t->opcode_modifier.no_lsuf && suffix_check.no_lsuf)
6534 || (t->opcode_modifier.no_ssuf && suffix_check.no_ssuf)
6535 || (t->opcode_modifier.no_qsuf && suffix_check.no_qsuf)
6536 || (t->opcode_modifier.no_ldsuf && suffix_check.no_ldsuf))
6537 continue;
6538
6539 size_match = operand_size_match (t);
6540 if (!size_match)
6541 continue;
6542
6543 /* This is intentionally not
6544
6545 if (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE))
6546
6547 as the case of a missing * on the operand is accepted (perhaps with
6548 a warning, issued further down). */
6549 if (i.jumpabsolute && t->opcode_modifier.jump != JUMP_ABSOLUTE)
6550 {
6551 i.error = operand_type_mismatch;
6552 continue;
6553 }
6554
6555 for (j = 0; j < MAX_OPERANDS; j++)
6556 operand_types[j] = t->operand_types[j];
6557
6558 /* In general, don't allow
6559 - 64-bit operands outside of 64-bit mode,
6560 - 32-bit operands on pre-386. */
6561 j = i.imm_operands + (t->operands > i.imm_operands + 1);
6562 if (((i.suffix == QWORD_MNEM_SUFFIX
6563 && flag_code != CODE_64BIT
6564 && !(t->opcode_modifier.opcodespace == SPACE_0F
6565 && t->base_opcode == 0xc7
6566 && t->opcode_modifier.opcodeprefix == PREFIX_NONE
6567 && t->extension_opcode == 1) /* cmpxchg8b */)
6568 || (i.suffix == LONG_MNEM_SUFFIX
6569 && !cpu_arch_flags.bitfield.cpui386))
6570 && (intel_syntax
6571 ? (t->opcode_modifier.mnemonicsize != IGNORESIZE
6572 && !intel_float_operand (t->name))
6573 : intel_float_operand (t->name) != 2)
6574 && (t->operands == i.imm_operands
6575 || (operand_types[i.imm_operands].bitfield.class != RegMMX
6576 && operand_types[i.imm_operands].bitfield.class != RegSIMD
6577 && operand_types[i.imm_operands].bitfield.class != RegMask)
6578 || (operand_types[j].bitfield.class != RegMMX
6579 && operand_types[j].bitfield.class != RegSIMD
6580 && operand_types[j].bitfield.class != RegMask))
6581 && !t->opcode_modifier.sib)
6582 continue;
6583
6584 /* Do not verify operands when there are none. */
6585 if (!t->operands)
6586 {
6587 if (VEX_check_encoding (t))
6588 {
6589 specific_error = i.error;
6590 continue;
6591 }
6592
6593 /* We've found a match; break out of loop. */
6594 break;
6595 }
6596
6597 if (!t->opcode_modifier.jump
6598 || t->opcode_modifier.jump == JUMP_ABSOLUTE)
6599 {
6600 /* There should be only one Disp operand. */
6601 for (j = 0; j < MAX_OPERANDS; j++)
6602 if (operand_type_check (operand_types[j], disp))
6603 break;
6604 if (j < MAX_OPERANDS)
6605 {
6606 bool override = (i.prefix[ADDR_PREFIX] != 0);
6607
6608 addr_prefix_disp = j;
6609
6610 /* Address size prefix will turn Disp64/Disp32S/Disp32/Disp16
6611 operand into Disp32/Disp32/Disp16/Disp32 operand. */
6612 switch (flag_code)
6613 {
6614 case CODE_16BIT:
6615 override = !override;
6616 /* Fall through. */
6617 case CODE_32BIT:
6618 if (operand_types[j].bitfield.disp32
6619 && operand_types[j].bitfield.disp16)
6620 {
6621 operand_types[j].bitfield.disp16 = override;
6622 operand_types[j].bitfield.disp32 = !override;
6623 }
6624 operand_types[j].bitfield.disp32s = 0;
6625 operand_types[j].bitfield.disp64 = 0;
6626 break;
6627
6628 case CODE_64BIT:
6629 if (operand_types[j].bitfield.disp32s
6630 || operand_types[j].bitfield.disp64)
6631 {
6632 operand_types[j].bitfield.disp64 &= !override;
6633 operand_types[j].bitfield.disp32s &= !override;
6634 operand_types[j].bitfield.disp32 = override;
6635 }
6636 operand_types[j].bitfield.disp16 = 0;
6637 break;
6638 }
6639 }
6640 }
6641
6642 switch (i.reloc[0])
6643 {
6644 case BFD_RELOC_386_GOT32:
6645 /* Force 0x8b encoding for "mov foo@GOT, %eax". */
6646 if (t->base_opcode == 0xa0
6647 && t->opcode_modifier.opcodespace == SPACE_BASE)
6648 continue;
6649 break;
6650 case BFD_RELOC_386_TLS_GOTIE:
6651 case BFD_RELOC_386_TLS_LE_32:
6652 case BFD_RELOC_X86_64_GOTTPOFF:
6653 case BFD_RELOC_X86_64_TLSLD:
6654 /* Don't allow KMOV in TLS code sequences. */
6655 if (t->opcode_modifier.vex)
6656 continue;
6657 break;
6658 default:
6659 break;
6660 }
6661
6662 /* We check register size if needed. */
6663 if (t->opcode_modifier.checkregsize)
6664 {
6665 check_register = (1 << t->operands) - 1;
6666 if (i.broadcast.type)
6667 check_register &= ~(1 << i.broadcast.operand);
6668 }
6669 else
6670 check_register = 0;
6671
6672 overlap0 = operand_type_and (i.types[0], operand_types[0]);
6673 switch (t->operands)
6674 {
6675 case 1:
6676 if (!operand_type_match (overlap0, i.types[0]))
6677 continue;
6678 break;
6679 case 2:
6680 /* xchg %eax, %eax is a special case. It is an alias for nop
6681 only in 32bit mode and we can use opcode 0x90. In 64bit
6682 mode, we can't use 0x90 for xchg %eax, %eax since it should
6683 zero-extend %eax to %rax. */
6684 if (flag_code == CODE_64BIT
6685 && t->base_opcode == 0x90
6686 && t->opcode_modifier.opcodespace == SPACE_BASE
6687 && i.types[0].bitfield.instance == Accum
6688 && i.types[0].bitfield.dword
6689 && i.types[1].bitfield.instance == Accum
6690 && i.types[1].bitfield.dword)
6691 continue;
6692 /* xrelease mov %eax, <disp> is another special case. It must not
6693 match the accumulator-only encoding of mov. */
6694 if (flag_code != CODE_64BIT
6695 && i.hle_prefix
6696 && t->base_opcode == 0xa0
6697 && t->opcode_modifier.opcodespace == SPACE_BASE
6698 && i.types[0].bitfield.instance == Accum
6699 && (i.flags[1] & Operand_Mem))
6700 continue;
6701 /* Fall through. */
6702
6703 case 3:
6704 if (!(size_match & MATCH_STRAIGHT))
6705 goto check_reverse;
6706 /* Reverse direction of operands if swapping is possible in the first
6707 place (operands need to be symmetric) and
6708 - the load form is requested, and the template is a store form,
6709 - the store form is requested, and the template is a load form,
6710 - the non-default (swapped) form is requested. */
6711 overlap1 = operand_type_and (operand_types[0], operand_types[1]);
6712 if (t->opcode_modifier.d && i.reg_operands == i.operands
6713 && !operand_type_all_zero (&overlap1))
6714 switch (i.dir_encoding)
6715 {
6716 case dir_encoding_load:
6717 if (operand_type_check (operand_types[i.operands - 1], anymem)
6718 || t->opcode_modifier.regmem)
6719 goto check_reverse;
6720 break;
6721
6722 case dir_encoding_store:
6723 if (!operand_type_check (operand_types[i.operands - 1], anymem)
6724 && !t->opcode_modifier.regmem)
6725 goto check_reverse;
6726 break;
6727
6728 case dir_encoding_swap:
6729 goto check_reverse;
6730
6731 case dir_encoding_default:
6732 break;
6733 }
6734 /* If we want store form, we skip the current load. */
6735 if ((i.dir_encoding == dir_encoding_store
6736 || i.dir_encoding == dir_encoding_swap)
6737 && i.mem_operands == 0
6738 && t->opcode_modifier.load)
6739 continue;
6740 /* Fall through. */
6741 case 4:
6742 case 5:
6743 overlap1 = operand_type_and (i.types[1], operand_types[1]);
6744 if (!operand_type_match (overlap0, i.types[0])
6745 || !operand_type_match (overlap1, i.types[1])
6746 || ((check_register & 3) == 3
6747 && !operand_type_register_match (i.types[0],
6748 operand_types[0],
6749 i.types[1],
6750 operand_types[1])))
6751 {
6752 /* Check if other direction is valid ... */
6753 if (!t->opcode_modifier.d)
6754 continue;
6755
6756 check_reverse:
6757 if (!(size_match & MATCH_REVERSE))
6758 continue;
6759 /* Try reversing direction of operands. */
6760 overlap0 = operand_type_and (i.types[0], operand_types[i.operands - 1]);
6761 overlap1 = operand_type_and (i.types[i.operands - 1], operand_types[0]);
6762 if (!operand_type_match (overlap0, i.types[0])
6763 || !operand_type_match (overlap1, i.types[i.operands - 1])
6764 || (check_register
6765 && !operand_type_register_match (i.types[0],
6766 operand_types[i.operands - 1],
6767 i.types[i.operands - 1],
6768 operand_types[0])))
6769 {
6770 /* Does not match either direction. */
6771 continue;
6772 }
6773 /* found_reverse_match holds which of D or FloatR
6774 we've found. */
6775 if (!t->opcode_modifier.d)
6776 found_reverse_match = 0;
6777 else if (operand_types[0].bitfield.tbyte)
6778 found_reverse_match = Opcode_FloatD;
6779 else if (operand_types[0].bitfield.xmmword
6780 || operand_types[i.operands - 1].bitfield.xmmword
6781 || operand_types[0].bitfield.class == RegMMX
6782 || operand_types[i.operands - 1].bitfield.class == RegMMX
6783 || is_any_vex_encoding(t))
6784 found_reverse_match = (t->base_opcode & 0xee) != 0x6e
6785 ? Opcode_SIMD_FloatD : Opcode_SIMD_IntD;
6786 else
6787 found_reverse_match = Opcode_D;
6788 if (t->opcode_modifier.floatr)
6789 found_reverse_match |= Opcode_FloatR;
6790 }
6791 else
6792 {
6793 /* Found a forward 2 operand match here. */
6794 switch (t->operands)
6795 {
6796 case 5:
6797 overlap4 = operand_type_and (i.types[4],
6798 operand_types[4]);
6799 /* Fall through. */
6800 case 4:
6801 overlap3 = operand_type_and (i.types[3],
6802 operand_types[3]);
6803 /* Fall through. */
6804 case 3:
6805 overlap2 = operand_type_and (i.types[2],
6806 operand_types[2]);
6807 break;
6808 }
6809
6810 switch (t->operands)
6811 {
6812 case 5:
6813 if (!operand_type_match (overlap4, i.types[4])
6814 || !operand_type_register_match (i.types[3],
6815 operand_types[3],
6816 i.types[4],
6817 operand_types[4]))
6818 continue;
6819 /* Fall through. */
6820 case 4:
6821 if (!operand_type_match (overlap3, i.types[3])
6822 || ((check_register & 0xa) == 0xa
6823 && !operand_type_register_match (i.types[1],
6824 operand_types[1],
6825 i.types[3],
6826 operand_types[3]))
6827 || ((check_register & 0xc) == 0xc
6828 && !operand_type_register_match (i.types[2],
6829 operand_types[2],
6830 i.types[3],
6831 operand_types[3])))
6832 continue;
6833 /* Fall through. */
6834 case 3:
6835 /* Here we make use of the fact that there are no
6836 reverse match 3 operand instructions. */
6837 if (!operand_type_match (overlap2, i.types[2])
6838 || ((check_register & 5) == 5
6839 && !operand_type_register_match (i.types[0],
6840 operand_types[0],
6841 i.types[2],
6842 operand_types[2]))
6843 || ((check_register & 6) == 6
6844 && !operand_type_register_match (i.types[1],
6845 operand_types[1],
6846 i.types[2],
6847 operand_types[2])))
6848 continue;
6849 break;
6850 }
6851 }
6852 /* Found either forward/reverse 2, 3 or 4 operand match here:
6853 slip through to break. */
6854 }
6855
6856 /* Check if vector operands are valid. */
6857 if (check_VecOperands (t))
6858 {
6859 specific_error = i.error;
6860 continue;
6861 }
6862
6863 /* Check if VEX/EVEX encoding requirements can be satisfied. */
6864 if (VEX_check_encoding (t))
6865 {
6866 specific_error = i.error;
6867 continue;
6868 }
6869
6870 /* We've found a match; break out of loop. */
6871 break;
6872 }
6873
6874 if (t == current_templates->end)
6875 {
6876 /* We found no match. */
6877 const char *err_msg;
6878 switch (specific_error ? specific_error : i.error)
6879 {
6880 default:
6881 abort ();
6882 case operand_size_mismatch:
6883 err_msg = _("operand size mismatch");
6884 break;
6885 case operand_type_mismatch:
6886 err_msg = _("operand type mismatch");
6887 break;
6888 case register_type_mismatch:
6889 err_msg = _("register type mismatch");
6890 break;
6891 case number_of_operands_mismatch:
6892 err_msg = _("number of operands mismatch");
6893 break;
6894 case invalid_instruction_suffix:
6895 err_msg = _("invalid instruction suffix");
6896 break;
6897 case bad_imm4:
6898 err_msg = _("constant doesn't fit in 4 bits");
6899 break;
6900 case unsupported_with_intel_mnemonic:
6901 err_msg = _("unsupported with Intel mnemonic");
6902 break;
6903 case unsupported_syntax:
6904 err_msg = _("unsupported syntax");
6905 break;
6906 case unsupported:
6907 as_bad (_("unsupported instruction `%s'"),
6908 current_templates->start->name);
6909 return NULL;
6910 case invalid_sib_address:
6911 err_msg = _("invalid SIB address");
6912 break;
6913 case invalid_vsib_address:
6914 err_msg = _("invalid VSIB address");
6915 break;
6916 case invalid_vector_register_set:
6917 err_msg = _("mask, index, and destination registers must be distinct");
6918 break;
6919 case invalid_tmm_register_set:
6920 err_msg = _("all tmm registers must be distinct");
6921 break;
6922 case invalid_dest_and_src_register_set:
6923 err_msg = _("destination and source registers must be distinct");
6924 break;
6925 case unsupported_vector_index_register:
6926 err_msg = _("unsupported vector index register");
6927 break;
6928 case unsupported_broadcast:
6929 err_msg = _("unsupported broadcast");
6930 break;
6931 case broadcast_needed:
6932 err_msg = _("broadcast is needed for operand of such type");
6933 break;
6934 case unsupported_masking:
6935 err_msg = _("unsupported masking");
6936 break;
6937 case mask_not_on_destination:
6938 err_msg = _("mask not on destination operand");
6939 break;
6940 case no_default_mask:
6941 err_msg = _("default mask isn't allowed");
6942 break;
6943 case unsupported_rc_sae:
6944 err_msg = _("unsupported static rounding/sae");
6945 break;
6946 case rc_sae_operand_not_last_imm:
6947 if (intel_syntax)
6948 err_msg = _("RC/SAE operand must precede immediate operands");
6949 else
6950 err_msg = _("RC/SAE operand must follow immediate operands");
6951 break;
6952 case invalid_register_operand:
6953 err_msg = _("invalid register operand");
6954 break;
6955 }
6956 as_bad (_("%s for `%s'"), err_msg,
6957 current_templates->start->name);
6958 return NULL;
6959 }
6960
6961 if (!quiet_warnings)
6962 {
6963 if (!intel_syntax
6964 && (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE)))
6965 as_warn (_("indirect %s without `*'"), t->name);
6966
6967 if (t->opcode_modifier.isprefix
6968 && t->opcode_modifier.mnemonicsize == IGNORESIZE)
6969 {
6970 /* Warn them that a data or address size prefix doesn't
6971 affect assembly of the next line of code. */
6972 as_warn (_("stand-alone `%s' prefix"), t->name);
6973 }
6974 }
6975
6976 /* Copy the template we found. */
6977 install_template (t);
6978
6979 if (addr_prefix_disp != -1)
6980 i.tm.operand_types[addr_prefix_disp]
6981 = operand_types[addr_prefix_disp];
6982
6983 if (found_reverse_match)
6984 {
6985 /* If we found a reverse match we must alter the opcode direction
6986 bit and clear/flip the regmem modifier one. found_reverse_match
6987 holds bits to change (different for int & float insns). */
6988
6989 i.tm.base_opcode ^= found_reverse_match;
6990
6991 i.tm.operand_types[0] = operand_types[i.operands - 1];
6992 i.tm.operand_types[i.operands - 1] = operand_types[0];
6993
6994 /* Certain SIMD insns have their load forms specified in the opcode
6995 table, and hence we need to _set_ RegMem instead of clearing it.
6996 We need to avoid setting the bit though on insns like KMOVW. */
6997 i.tm.opcode_modifier.regmem
6998 = i.tm.opcode_modifier.modrm && i.tm.opcode_modifier.d
6999 && i.tm.operands > 2U - i.tm.opcode_modifier.sse2avx
7000 && !i.tm.opcode_modifier.regmem;
7001 }
7002
7003 return t;
7004 }
7005
7006 static int
7007 check_string (void)
7008 {
7009 unsigned int es_op = i.tm.opcode_modifier.isstring - IS_STRING_ES_OP0;
7010 unsigned int op = i.tm.operand_types[0].bitfield.baseindex ? es_op : 0;
7011
7012 if (i.seg[op] != NULL && i.seg[op] != reg_es)
7013 {
7014 as_bad (_("`%s' operand %u must use `%ses' segment"),
7015 i.tm.name,
7016 intel_syntax ? i.tm.operands - es_op : es_op + 1,
7017 register_prefix);
7018 return 0;
7019 }
7020
7021 /* There's only ever one segment override allowed per instruction.
7022 This instruction possibly has a legal segment override on the
7023 second operand, so copy the segment to where non-string
7024 instructions store it, allowing common code. */
7025 i.seg[op] = i.seg[1];
7026
7027 return 1;
7028 }
7029
7030 static int
7031 process_suffix (void)
7032 {
7033 bool is_crc32 = false, is_movx = false;
7034
7035 /* If matched instruction specifies an explicit instruction mnemonic
7036 suffix, use it. */
7037 if (i.tm.opcode_modifier.size == SIZE16)
7038 i.suffix = WORD_MNEM_SUFFIX;
7039 else if (i.tm.opcode_modifier.size == SIZE32)
7040 i.suffix = LONG_MNEM_SUFFIX;
7041 else if (i.tm.opcode_modifier.size == SIZE64)
7042 i.suffix = QWORD_MNEM_SUFFIX;
7043 else if (i.reg_operands
7044 && (i.operands > 1 || i.types[0].bitfield.class == Reg)
7045 && !i.tm.opcode_modifier.addrprefixopreg)
7046 {
7047 unsigned int numop = i.operands;
7048
7049 /* MOVSX/MOVZX */
7050 is_movx = (i.tm.opcode_modifier.opcodespace == SPACE_0F
7051 && (i.tm.base_opcode | 8) == 0xbe)
7052 || (i.tm.opcode_modifier.opcodespace == SPACE_BASE
7053 && i.tm.base_opcode == 0x63
7054 && i.tm.cpu_flags.bitfield.cpu64);
7055
7056 /* CRC32 */
7057 is_crc32 = (i.tm.base_opcode == 0xf0
7058 && i.tm.opcode_modifier.opcodespace == SPACE_0F38
7059 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2);
7060
7061 /* movsx/movzx want only their source operand considered here, for the
7062 ambiguity checking below. The suffix will be replaced afterwards
7063 to represent the destination (register). */
7064 if (is_movx && (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63))
7065 --i.operands;
7066
7067 /* crc32 needs REX.W set regardless of suffix / source operand size. */
7068 if (is_crc32 && i.tm.operand_types[1].bitfield.qword)
7069 i.rex |= REX_W;
7070
7071 /* If there's no instruction mnemonic suffix we try to invent one
7072 based on GPR operands. */
7073 if (!i.suffix)
7074 {
7075 /* We take i.suffix from the last register operand specified,
7076 Destination register type is more significant than source
7077 register type. crc32 in SSE4.2 prefers source register
7078 type. */
7079 unsigned int op = is_crc32 ? 1 : i.operands;
7080
7081 while (op--)
7082 if (i.tm.operand_types[op].bitfield.instance == InstanceNone
7083 || i.tm.operand_types[op].bitfield.instance == Accum)
7084 {
7085 if (i.types[op].bitfield.class != Reg)
7086 continue;
7087 if (i.types[op].bitfield.byte)
7088 i.suffix = BYTE_MNEM_SUFFIX;
7089 else if (i.types[op].bitfield.word)
7090 i.suffix = WORD_MNEM_SUFFIX;
7091 else if (i.types[op].bitfield.dword)
7092 i.suffix = LONG_MNEM_SUFFIX;
7093 else if (i.types[op].bitfield.qword)
7094 i.suffix = QWORD_MNEM_SUFFIX;
7095 else
7096 continue;
7097 break;
7098 }
7099
7100 /* As an exception, movsx/movzx silently default to a byte source
7101 in AT&T mode. */
7102 if (is_movx && i.tm.opcode_modifier.w && !i.suffix && !intel_syntax)
7103 i.suffix = BYTE_MNEM_SUFFIX;
7104 }
7105 else if (i.suffix == BYTE_MNEM_SUFFIX)
7106 {
7107 if (intel_syntax
7108 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
7109 && i.tm.opcode_modifier.no_bsuf)
7110 i.suffix = 0;
7111 else if (!check_byte_reg ())
7112 return 0;
7113 }
7114 else if (i.suffix == LONG_MNEM_SUFFIX)
7115 {
7116 if (intel_syntax
7117 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
7118 && i.tm.opcode_modifier.no_lsuf
7119 && !i.tm.opcode_modifier.todword
7120 && !i.tm.opcode_modifier.toqword)
7121 i.suffix = 0;
7122 else if (!check_long_reg ())
7123 return 0;
7124 }
7125 else if (i.suffix == QWORD_MNEM_SUFFIX)
7126 {
7127 if (intel_syntax
7128 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
7129 && i.tm.opcode_modifier.no_qsuf
7130 && !i.tm.opcode_modifier.todword
7131 && !i.tm.opcode_modifier.toqword)
7132 i.suffix = 0;
7133 else if (!check_qword_reg ())
7134 return 0;
7135 }
7136 else if (i.suffix == WORD_MNEM_SUFFIX)
7137 {
7138 if (intel_syntax
7139 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
7140 && i.tm.opcode_modifier.no_wsuf)
7141 i.suffix = 0;
7142 else if (!check_word_reg ())
7143 return 0;
7144 }
7145 else if (intel_syntax
7146 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
7147 /* Do nothing if the instruction is going to ignore the prefix. */
7148 ;
7149 else
7150 abort ();
7151
7152 /* Undo the movsx/movzx change done above. */
7153 i.operands = numop;
7154 }
7155 else if (i.tm.opcode_modifier.mnemonicsize == DEFAULTSIZE
7156 && !i.suffix)
7157 {
7158 i.suffix = stackop_size;
7159 if (stackop_size == LONG_MNEM_SUFFIX)
7160 {
7161 /* stackop_size is set to LONG_MNEM_SUFFIX for the
7162 .code16gcc directive to support 16-bit mode with
7163 32-bit address. For IRET without a suffix, generate
7164 16-bit IRET (opcode 0xcf) to return from an interrupt
7165 handler. */
7166 if (i.tm.base_opcode == 0xcf)
7167 {
7168 i.suffix = WORD_MNEM_SUFFIX;
7169 as_warn (_("generating 16-bit `iret' for .code16gcc directive"));
7170 }
7171 /* Warn about changed behavior for segment register push/pop. */
7172 else if ((i.tm.base_opcode | 1) == 0x07)
7173 as_warn (_("generating 32-bit `%s', unlike earlier gas versions"),
7174 i.tm.name);
7175 }
7176 }
7177 else if (!i.suffix
7178 && (i.tm.opcode_modifier.jump == JUMP_ABSOLUTE
7179 || i.tm.opcode_modifier.jump == JUMP_BYTE
7180 || i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT
7181 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
7182 && i.tm.base_opcode == 0x01 /* [ls][gi]dt */
7183 && i.tm.extension_opcode <= 3)))
7184 {
7185 switch (flag_code)
7186 {
7187 case CODE_64BIT:
7188 if (!i.tm.opcode_modifier.no_qsuf)
7189 {
7190 if (i.tm.opcode_modifier.jump == JUMP_BYTE
7191 || i.tm.opcode_modifier.no_lsuf)
7192 i.suffix = QWORD_MNEM_SUFFIX;
7193 break;
7194 }
7195 /* Fall through. */
7196 case CODE_32BIT:
7197 if (!i.tm.opcode_modifier.no_lsuf)
7198 i.suffix = LONG_MNEM_SUFFIX;
7199 break;
7200 case CODE_16BIT:
7201 if (!i.tm.opcode_modifier.no_wsuf)
7202 i.suffix = WORD_MNEM_SUFFIX;
7203 break;
7204 }
7205 }
7206
7207 if (!i.suffix
7208 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
7209 /* Also cover lret/retf/iret in 64-bit mode. */
7210 || (flag_code == CODE_64BIT
7211 && !i.tm.opcode_modifier.no_lsuf
7212 && !i.tm.opcode_modifier.no_qsuf))
7213 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
7214 /* Explicit sizing prefixes are assumed to disambiguate insns. */
7215 && !i.prefix[DATA_PREFIX] && !(i.prefix[REX_PREFIX] & REX_W)
7216 /* Accept FLDENV et al without suffix. */
7217 && (i.tm.opcode_modifier.no_ssuf || i.tm.opcode_modifier.floatmf))
7218 {
7219 unsigned int suffixes, evex = 0;
7220
7221 suffixes = !i.tm.opcode_modifier.no_bsuf;
7222 if (!i.tm.opcode_modifier.no_wsuf)
7223 suffixes |= 1 << 1;
7224 if (!i.tm.opcode_modifier.no_lsuf)
7225 suffixes |= 1 << 2;
7226 if (!i.tm.opcode_modifier.no_ldsuf)
7227 suffixes |= 1 << 3;
7228 if (!i.tm.opcode_modifier.no_ssuf)
7229 suffixes |= 1 << 4;
7230 if (flag_code == CODE_64BIT && !i.tm.opcode_modifier.no_qsuf)
7231 suffixes |= 1 << 5;
7232
7233 /* For [XYZ]MMWORD operands inspect operand sizes. While generally
7234 also suitable for AT&T syntax mode, it was requested that this be
7235 restricted to just Intel syntax. */
7236 if (intel_syntax && is_any_vex_encoding (&i.tm) && !i.broadcast.type)
7237 {
7238 unsigned int op;
7239
7240 for (op = 0; op < i.tm.operands; ++op)
7241 {
7242 if (is_evex_encoding (&i.tm)
7243 && !cpu_arch_flags.bitfield.cpuavx512vl)
7244 {
7245 if (i.tm.operand_types[op].bitfield.ymmword)
7246 i.tm.operand_types[op].bitfield.xmmword = 0;
7247 if (i.tm.operand_types[op].bitfield.zmmword)
7248 i.tm.operand_types[op].bitfield.ymmword = 0;
7249 if (!i.tm.opcode_modifier.evex
7250 || i.tm.opcode_modifier.evex == EVEXDYN)
7251 i.tm.opcode_modifier.evex = EVEX512;
7252 }
7253
7254 if (i.tm.operand_types[op].bitfield.xmmword
7255 + i.tm.operand_types[op].bitfield.ymmword
7256 + i.tm.operand_types[op].bitfield.zmmword < 2)
7257 continue;
7258
7259 /* Any properly sized operand disambiguates the insn. */
7260 if (i.types[op].bitfield.xmmword
7261 || i.types[op].bitfield.ymmword
7262 || i.types[op].bitfield.zmmword)
7263 {
7264 suffixes &= ~(7 << 6);
7265 evex = 0;
7266 break;
7267 }
7268
7269 if ((i.flags[op] & Operand_Mem)
7270 && i.tm.operand_types[op].bitfield.unspecified)
7271 {
7272 if (i.tm.operand_types[op].bitfield.xmmword)
7273 suffixes |= 1 << 6;
7274 if (i.tm.operand_types[op].bitfield.ymmword)
7275 suffixes |= 1 << 7;
7276 if (i.tm.operand_types[op].bitfield.zmmword)
7277 suffixes |= 1 << 8;
7278 if (is_evex_encoding (&i.tm))
7279 evex = EVEX512;
7280 }
7281 }
7282 }
7283
7284 /* Are multiple suffixes / operand sizes allowed? */
7285 if (suffixes & (suffixes - 1))
7286 {
7287 if (intel_syntax
7288 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
7289 || operand_check == check_error))
7290 {
7291 as_bad (_("ambiguous operand size for `%s'"), i.tm.name);
7292 return 0;
7293 }
7294 if (operand_check == check_error)
7295 {
7296 as_bad (_("no instruction mnemonic suffix given and "
7297 "no register operands; can't size `%s'"), i.tm.name);
7298 return 0;
7299 }
7300 if (operand_check == check_warning)
7301 as_warn (_("%s; using default for `%s'"),
7302 intel_syntax
7303 ? _("ambiguous operand size")
7304 : _("no instruction mnemonic suffix given and "
7305 "no register operands"),
7306 i.tm.name);
7307
7308 if (i.tm.opcode_modifier.floatmf)
7309 i.suffix = SHORT_MNEM_SUFFIX;
7310 else if (is_movx)
7311 /* handled below */;
7312 else if (evex)
7313 i.tm.opcode_modifier.evex = evex;
7314 else if (flag_code == CODE_16BIT)
7315 i.suffix = WORD_MNEM_SUFFIX;
7316 else if (!i.tm.opcode_modifier.no_lsuf)
7317 i.suffix = LONG_MNEM_SUFFIX;
7318 else
7319 i.suffix = QWORD_MNEM_SUFFIX;
7320 }
7321 }
7322
7323 if (is_movx)
7324 {
7325 /* In Intel syntax, movsx/movzx must have a "suffix" (checked above).
7326 In AT&T syntax, if there is no suffix (warned about above), the default
7327 will be byte extension. */
7328 if (i.tm.opcode_modifier.w && i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
7329 i.tm.base_opcode |= 1;
7330
7331 /* For further processing, the suffix should represent the destination
7332 (register). This is already the case when one was used with
7333 mov[sz][bw]*, but we need to replace it for mov[sz]x, or if there was
7334 no suffix to begin with. */
7335 if (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63 || !i.suffix)
7336 {
7337 if (i.types[1].bitfield.word)
7338 i.suffix = WORD_MNEM_SUFFIX;
7339 else if (i.types[1].bitfield.qword)
7340 i.suffix = QWORD_MNEM_SUFFIX;
7341 else
7342 i.suffix = LONG_MNEM_SUFFIX;
7343
7344 i.tm.opcode_modifier.w = 0;
7345 }
7346 }
7347
7348 if (!i.tm.opcode_modifier.modrm && i.reg_operands && i.tm.operands < 3)
7349 i.short_form = (i.tm.operand_types[0].bitfield.class == Reg)
7350 != (i.tm.operand_types[1].bitfield.class == Reg);
7351
7352 /* Change the opcode based on the operand size given by i.suffix. */
7353 switch (i.suffix)
7354 {
7355 /* Size floating point instruction. */
7356 case LONG_MNEM_SUFFIX:
7357 if (i.tm.opcode_modifier.floatmf)
7358 {
7359 i.tm.base_opcode ^= 4;
7360 break;
7361 }
7362 /* fall through */
7363 case WORD_MNEM_SUFFIX:
7364 case QWORD_MNEM_SUFFIX:
7365 /* It's not a byte, select word/dword operation. */
7366 if (i.tm.opcode_modifier.w)
7367 {
7368 if (i.short_form)
7369 i.tm.base_opcode |= 8;
7370 else
7371 i.tm.base_opcode |= 1;
7372 }
7373 /* fall through */
7374 case SHORT_MNEM_SUFFIX:
7375 /* Now select between word & dword operations via the operand
7376 size prefix, except for instructions that will ignore this
7377 prefix anyway. */
7378 if (i.suffix != QWORD_MNEM_SUFFIX
7379 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
7380 && !i.tm.opcode_modifier.floatmf
7381 && !is_any_vex_encoding (&i.tm)
7382 && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
7383 || (flag_code == CODE_64BIT
7384 && i.tm.opcode_modifier.jump == JUMP_BYTE)))
7385 {
7386 unsigned int prefix = DATA_PREFIX_OPCODE;
7387
7388 if (i.tm.opcode_modifier.jump == JUMP_BYTE) /* jcxz, loop */
7389 prefix = ADDR_PREFIX_OPCODE;
7390
7391 if (!add_prefix (prefix))
7392 return 0;
7393 }
7394
7395 /* Set mode64 for an operand. */
7396 if (i.suffix == QWORD_MNEM_SUFFIX
7397 && flag_code == CODE_64BIT
7398 && !i.tm.opcode_modifier.norex64
7399 && !i.tm.opcode_modifier.vexw
7400 /* Special case for xchg %rax,%rax. It is NOP and doesn't
7401 need rex64. */
7402 && ! (i.operands == 2
7403 && i.tm.base_opcode == 0x90
7404 && i.tm.extension_opcode == None
7405 && i.types[0].bitfield.instance == Accum
7406 && i.types[0].bitfield.qword
7407 && i.types[1].bitfield.instance == Accum
7408 && i.types[1].bitfield.qword))
7409 i.rex |= REX_W;
7410
7411 break;
7412
7413 case 0:
7414 /* Select word/dword/qword operation with explicit data sizing prefix
7415 when there are no suitable register operands. */
7416 if (i.tm.opcode_modifier.w
7417 && (i.prefix[DATA_PREFIX] || (i.prefix[REX_PREFIX] & REX_W))
7418 && (!i.reg_operands
7419 || (i.reg_operands == 1
7420 /* ShiftCount */
7421 && (i.tm.operand_types[0].bitfield.instance == RegC
7422 /* InOutPortReg */
7423 || i.tm.operand_types[0].bitfield.instance == RegD
7424 || i.tm.operand_types[1].bitfield.instance == RegD
7425 /* CRC32 */
7426 || is_crc32))))
7427 i.tm.base_opcode |= 1;
7428 break;
7429 }
7430
7431 if (i.tm.opcode_modifier.addrprefixopreg)
7432 {
7433 gas_assert (!i.suffix);
7434 gas_assert (i.reg_operands);
7435
7436 if (i.tm.operand_types[0].bitfield.instance == Accum
7437 || i.operands == 1)
7438 {
7439 /* The address size override prefix changes the size of the
7440 first operand. */
7441 if (flag_code == CODE_64BIT
7442 && i.op[0].regs->reg_type.bitfield.word)
7443 {
7444 as_bad (_("16-bit addressing unavailable for `%s'"),
7445 i.tm.name);
7446 return 0;
7447 }
7448
7449 if ((flag_code == CODE_32BIT
7450 ? i.op[0].regs->reg_type.bitfield.word
7451 : i.op[0].regs->reg_type.bitfield.dword)
7452 && !add_prefix (ADDR_PREFIX_OPCODE))
7453 return 0;
7454 }
7455 else
7456 {
7457 /* Check invalid register operand when the address size override
7458 prefix changes the size of register operands. */
7459 unsigned int op;
7460 enum { need_word, need_dword, need_qword } need;
7461
7462 /* Check the register operand for the address size prefix if
7463 the memory operand has no real registers, like symbol, DISP
7464 or bogus (x32-only) symbol(%rip) when symbol(%eip) is meant. */
7465 if (i.mem_operands == 1
7466 && i.reg_operands == 1
7467 && i.operands == 2
7468 && i.types[1].bitfield.class == Reg
7469 && (flag_code == CODE_32BIT
7470 ? i.op[1].regs->reg_type.bitfield.word
7471 : i.op[1].regs->reg_type.bitfield.dword)
7472 && ((i.base_reg == NULL && i.index_reg == NULL)
7473 #if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
7474 || (x86_elf_abi == X86_64_X32_ABI
7475 && i.base_reg
7476 && i.base_reg->reg_num == RegIP
7477 && i.base_reg->reg_type.bitfield.qword))
7478 #else
7479 || 0)
7480 #endif
7481 && !add_prefix (ADDR_PREFIX_OPCODE))
7482 return 0;
7483
7484 if (flag_code == CODE_32BIT)
7485 need = i.prefix[ADDR_PREFIX] ? need_word : need_dword;
7486 else if (i.prefix[ADDR_PREFIX])
7487 need = need_dword;
7488 else
7489 need = flag_code == CODE_64BIT ? need_qword : need_word;
7490
7491 for (op = 0; op < i.operands; op++)
7492 {
7493 if (i.types[op].bitfield.class != Reg)
7494 continue;
7495
7496 switch (need)
7497 {
7498 case need_word:
7499 if (i.op[op].regs->reg_type.bitfield.word)
7500 continue;
7501 break;
7502 case need_dword:
7503 if (i.op[op].regs->reg_type.bitfield.dword)
7504 continue;
7505 break;
7506 case need_qword:
7507 if (i.op[op].regs->reg_type.bitfield.qword)
7508 continue;
7509 break;
7510 }
7511
7512 as_bad (_("invalid register operand size for `%s'"),
7513 i.tm.name);
7514 return 0;
7515 }
7516 }
7517 }
7518
7519 return 1;
7520 }
7521
7522 static int
7523 check_byte_reg (void)
7524 {
7525 int op;
7526
7527 for (op = i.operands; --op >= 0;)
7528 {
7529 /* Skip non-register operands. */
7530 if (i.types[op].bitfield.class != Reg)
7531 continue;
7532
7533 /* If this is an eight bit register, it's OK. If it's the 16 or
7534 32 bit version of an eight bit register, we will just use the
7535 low portion, and that's OK too. */
7536 if (i.types[op].bitfield.byte)
7537 continue;
7538
7539 /* I/O port address operands are OK too. */
7540 if (i.tm.operand_types[op].bitfield.instance == RegD
7541 && i.tm.operand_types[op].bitfield.word)
7542 continue;
7543
7544 /* crc32 only wants its source operand checked here. */
7545 if (i.tm.base_opcode == 0xf0
7546 && i.tm.opcode_modifier.opcodespace == SPACE_0F38
7547 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2
7548 && op != 0)
7549 continue;
7550
7551 /* Any other register is bad. */
7552 as_bad (_("`%s%s' not allowed with `%s%c'"),
7553 register_prefix, i.op[op].regs->reg_name,
7554 i.tm.name, i.suffix);
7555 return 0;
7556 }
7557 return 1;
7558 }
7559
7560 static int
7561 check_long_reg (void)
7562 {
7563 int op;
7564
7565 for (op = i.operands; --op >= 0;)
7566 /* Skip non-register operands. */
7567 if (i.types[op].bitfield.class != Reg)
7568 continue;
7569 /* Reject eight bit registers, except where the template requires
7570 them. (eg. movzb) */
7571 else if (i.types[op].bitfield.byte
7572 && (i.tm.operand_types[op].bitfield.class == Reg
7573 || i.tm.operand_types[op].bitfield.instance == Accum)
7574 && (i.tm.operand_types[op].bitfield.word
7575 || i.tm.operand_types[op].bitfield.dword))
7576 {
7577 as_bad (_("`%s%s' not allowed with `%s%c'"),
7578 register_prefix,
7579 i.op[op].regs->reg_name,
7580 i.tm.name,
7581 i.suffix);
7582 return 0;
7583 }
7584 /* Error if the e prefix on a general reg is missing. */
7585 else if (i.types[op].bitfield.word
7586 && (i.tm.operand_types[op].bitfield.class == Reg
7587 || i.tm.operand_types[op].bitfield.instance == Accum)
7588 && i.tm.operand_types[op].bitfield.dword)
7589 {
7590 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7591 register_prefix, i.op[op].regs->reg_name,
7592 i.suffix);
7593 return 0;
7594 }
7595 /* Warn if the r prefix on a general reg is present. */
7596 else if (i.types[op].bitfield.qword
7597 && (i.tm.operand_types[op].bitfield.class == Reg
7598 || i.tm.operand_types[op].bitfield.instance == Accum)
7599 && i.tm.operand_types[op].bitfield.dword)
7600 {
7601 if (intel_syntax
7602 && i.tm.opcode_modifier.toqword
7603 && i.types[0].bitfield.class != RegSIMD)
7604 {
7605 /* Convert to QWORD. We want REX byte. */
7606 i.suffix = QWORD_MNEM_SUFFIX;
7607 }
7608 else
7609 {
7610 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7611 register_prefix, i.op[op].regs->reg_name,
7612 i.suffix);
7613 return 0;
7614 }
7615 }
7616 return 1;
7617 }
7618
7619 static int
7620 check_qword_reg (void)
7621 {
7622 int op;
7623
7624 for (op = i.operands; --op >= 0; )
7625 /* Skip non-register operands. */
7626 if (i.types[op].bitfield.class != Reg)
7627 continue;
7628 /* Reject eight bit registers, except where the template requires
7629 them. (eg. movzb) */
7630 else if (i.types[op].bitfield.byte
7631 && (i.tm.operand_types[op].bitfield.class == Reg
7632 || i.tm.operand_types[op].bitfield.instance == Accum)
7633 && (i.tm.operand_types[op].bitfield.word
7634 || i.tm.operand_types[op].bitfield.dword))
7635 {
7636 as_bad (_("`%s%s' not allowed with `%s%c'"),
7637 register_prefix,
7638 i.op[op].regs->reg_name,
7639 i.tm.name,
7640 i.suffix);
7641 return 0;
7642 }
7643 /* Warn if the r prefix on a general reg is missing. */
7644 else if ((i.types[op].bitfield.word
7645 || i.types[op].bitfield.dword)
7646 && (i.tm.operand_types[op].bitfield.class == Reg
7647 || i.tm.operand_types[op].bitfield.instance == Accum)
7648 && i.tm.operand_types[op].bitfield.qword)
7649 {
7650 /* Prohibit these changes in the 64bit mode, since the
7651 lowering is more complicated. */
7652 if (intel_syntax
7653 && i.tm.opcode_modifier.todword
7654 && i.types[0].bitfield.class != RegSIMD)
7655 {
7656 /* Convert to DWORD. We don't want REX byte. */
7657 i.suffix = LONG_MNEM_SUFFIX;
7658 }
7659 else
7660 {
7661 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7662 register_prefix, i.op[op].regs->reg_name,
7663 i.suffix);
7664 return 0;
7665 }
7666 }
7667 return 1;
7668 }
7669
7670 static int
7671 check_word_reg (void)
7672 {
7673 int op;
7674 for (op = i.operands; --op >= 0;)
7675 /* Skip non-register operands. */
7676 if (i.types[op].bitfield.class != Reg)
7677 continue;
7678 /* Reject eight bit registers, except where the template requires
7679 them. (eg. movzb) */
7680 else if (i.types[op].bitfield.byte
7681 && (i.tm.operand_types[op].bitfield.class == Reg
7682 || i.tm.operand_types[op].bitfield.instance == Accum)
7683 && (i.tm.operand_types[op].bitfield.word
7684 || i.tm.operand_types[op].bitfield.dword))
7685 {
7686 as_bad (_("`%s%s' not allowed with `%s%c'"),
7687 register_prefix,
7688 i.op[op].regs->reg_name,
7689 i.tm.name,
7690 i.suffix);
7691 return 0;
7692 }
7693 /* Error if the e or r prefix on a general reg is present. */
7694 else if ((i.types[op].bitfield.dword
7695 || i.types[op].bitfield.qword)
7696 && (i.tm.operand_types[op].bitfield.class == Reg
7697 || i.tm.operand_types[op].bitfield.instance == Accum)
7698 && i.tm.operand_types[op].bitfield.word)
7699 {
7700 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7701 register_prefix, i.op[op].regs->reg_name,
7702 i.suffix);
7703 return 0;
7704 }
7705 /* For some instructions need encode as EVEX.W=1 without explicit VexW1. */
7706 else if (i.types[op].bitfield.qword
7707 && intel_syntax
7708 && i.tm.opcode_modifier.toqword)
7709 {
7710 /* Convert to QWORD. We want EVEX.W byte. */
7711 i.suffix = QWORD_MNEM_SUFFIX;
7712 }
7713 return 1;
7714 }
7715
7716 static int
7717 update_imm (unsigned int j)
7718 {
7719 i386_operand_type overlap = i.types[j];
7720 if ((overlap.bitfield.imm8
7721 || overlap.bitfield.imm8s
7722 || overlap.bitfield.imm16
7723 || overlap.bitfield.imm32
7724 || overlap.bitfield.imm32s
7725 || overlap.bitfield.imm64)
7726 && !operand_type_equal (&overlap, &imm8)
7727 && !operand_type_equal (&overlap, &imm8s)
7728 && !operand_type_equal (&overlap, &imm16)
7729 && !operand_type_equal (&overlap, &imm32)
7730 && !operand_type_equal (&overlap, &imm32s)
7731 && !operand_type_equal (&overlap, &imm64))
7732 {
7733 if (i.suffix)
7734 {
7735 i386_operand_type temp;
7736
7737 operand_type_set (&temp, 0);
7738 if (i.suffix == BYTE_MNEM_SUFFIX)
7739 {
7740 temp.bitfield.imm8 = overlap.bitfield.imm8;
7741 temp.bitfield.imm8s = overlap.bitfield.imm8s;
7742 }
7743 else if (i.suffix == WORD_MNEM_SUFFIX)
7744 temp.bitfield.imm16 = overlap.bitfield.imm16;
7745 else if (i.suffix == QWORD_MNEM_SUFFIX)
7746 {
7747 temp.bitfield.imm64 = overlap.bitfield.imm64;
7748 temp.bitfield.imm32s = overlap.bitfield.imm32s;
7749 }
7750 else
7751 temp.bitfield.imm32 = overlap.bitfield.imm32;
7752 overlap = temp;
7753 }
7754 else if (operand_type_equal (&overlap, &imm16_32_32s)
7755 || operand_type_equal (&overlap, &imm16_32)
7756 || operand_type_equal (&overlap, &imm16_32s))
7757 {
7758 if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
7759 overlap = imm16;
7760 else
7761 overlap = imm32s;
7762 }
7763 else if (i.prefix[REX_PREFIX] & REX_W)
7764 overlap = operand_type_and (overlap, imm32s);
7765 else if (i.prefix[DATA_PREFIX])
7766 overlap = operand_type_and (overlap,
7767 flag_code != CODE_16BIT ? imm16 : imm32);
7768 if (!operand_type_equal (&overlap, &imm8)
7769 && !operand_type_equal (&overlap, &imm8s)
7770 && !operand_type_equal (&overlap, &imm16)
7771 && !operand_type_equal (&overlap, &imm32)
7772 && !operand_type_equal (&overlap, &imm32s)
7773 && !operand_type_equal (&overlap, &imm64))
7774 {
7775 as_bad (_("no instruction mnemonic suffix given; "
7776 "can't determine immediate size"));
7777 return 0;
7778 }
7779 }
7780 i.types[j] = overlap;
7781
7782 return 1;
7783 }
7784
7785 static int
7786 finalize_imm (void)
7787 {
7788 unsigned int j, n;
7789
7790 /* Update the first 2 immediate operands. */
7791 n = i.operands > 2 ? 2 : i.operands;
7792 if (n)
7793 {
7794 for (j = 0; j < n; j++)
7795 if (update_imm (j) == 0)
7796 return 0;
7797
7798 /* The 3rd operand can't be immediate operand. */
7799 gas_assert (operand_type_check (i.types[2], imm) == 0);
7800 }
7801
7802 return 1;
7803 }
7804
7805 static int
7806 process_operands (void)
7807 {
7808 /* Default segment register this instruction will use for memory
7809 accesses. 0 means unknown. This is only for optimizing out
7810 unnecessary segment overrides. */
7811 const reg_entry *default_seg = NULL;
7812
7813 if (i.tm.opcode_modifier.sse2avx)
7814 {
7815 /* Legacy encoded insns allow explicit REX prefixes, so these prefixes
7816 need converting. */
7817 i.rex |= i.prefix[REX_PREFIX] & (REX_W | REX_R | REX_X | REX_B);
7818 i.prefix[REX_PREFIX] = 0;
7819 i.rex_encoding = 0;
7820 }
7821 /* ImmExt should be processed after SSE2AVX. */
7822 else if (i.tm.opcode_modifier.immext)
7823 process_immext ();
7824
7825 if (i.tm.opcode_modifier.sse2avx && i.tm.opcode_modifier.vexvvvv)
7826 {
7827 unsigned int dupl = i.operands;
7828 unsigned int dest = dupl - 1;
7829 unsigned int j;
7830
7831 /* The destination must be an xmm register. */
7832 gas_assert (i.reg_operands
7833 && MAX_OPERANDS > dupl
7834 && operand_type_equal (&i.types[dest], &regxmm));
7835
7836 if (i.tm.operand_types[0].bitfield.instance == Accum
7837 && i.tm.operand_types[0].bitfield.xmmword)
7838 {
7839 if (i.tm.opcode_modifier.vexsources == VEX3SOURCES)
7840 {
7841 /* Keep xmm0 for instructions with VEX prefix and 3
7842 sources. */
7843 i.tm.operand_types[0].bitfield.instance = InstanceNone;
7844 i.tm.operand_types[0].bitfield.class = RegSIMD;
7845 goto duplicate;
7846 }
7847 else
7848 {
7849 /* We remove the first xmm0 and keep the number of
7850 operands unchanged, which in fact duplicates the
7851 destination. */
7852 for (j = 1; j < i.operands; j++)
7853 {
7854 i.op[j - 1] = i.op[j];
7855 i.types[j - 1] = i.types[j];
7856 i.tm.operand_types[j - 1] = i.tm.operand_types[j];
7857 i.flags[j - 1] = i.flags[j];
7858 }
7859 }
7860 }
7861 else if (i.tm.opcode_modifier.implicit1stxmm0)
7862 {
7863 gas_assert ((MAX_OPERANDS - 1) > dupl
7864 && (i.tm.opcode_modifier.vexsources
7865 == VEX3SOURCES));
7866
7867 /* Add the implicit xmm0 for instructions with VEX prefix
7868 and 3 sources. */
7869 for (j = i.operands; j > 0; j--)
7870 {
7871 i.op[j] = i.op[j - 1];
7872 i.types[j] = i.types[j - 1];
7873 i.tm.operand_types[j] = i.tm.operand_types[j - 1];
7874 i.flags[j] = i.flags[j - 1];
7875 }
7876 i.op[0].regs
7877 = (const reg_entry *) str_hash_find (reg_hash, "xmm0");
7878 i.types[0] = regxmm;
7879 i.tm.operand_types[0] = regxmm;
7880
7881 i.operands += 2;
7882 i.reg_operands += 2;
7883 i.tm.operands += 2;
7884
7885 dupl++;
7886 dest++;
7887 i.op[dupl] = i.op[dest];
7888 i.types[dupl] = i.types[dest];
7889 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
7890 i.flags[dupl] = i.flags[dest];
7891 }
7892 else
7893 {
7894 duplicate:
7895 i.operands++;
7896 i.reg_operands++;
7897 i.tm.operands++;
7898
7899 i.op[dupl] = i.op[dest];
7900 i.types[dupl] = i.types[dest];
7901 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
7902 i.flags[dupl] = i.flags[dest];
7903 }
7904
7905 if (i.tm.opcode_modifier.immext)
7906 process_immext ();
7907 }
7908 else if (i.tm.operand_types[0].bitfield.instance == Accum
7909 && i.tm.operand_types[0].bitfield.xmmword)
7910 {
7911 unsigned int j;
7912
7913 for (j = 1; j < i.operands; j++)
7914 {
7915 i.op[j - 1] = i.op[j];
7916 i.types[j - 1] = i.types[j];
7917
7918 /* We need to adjust fields in i.tm since they are used by
7919 build_modrm_byte. */
7920 i.tm.operand_types [j - 1] = i.tm.operand_types [j];
7921
7922 i.flags[j - 1] = i.flags[j];
7923 }
7924
7925 i.operands--;
7926 i.reg_operands--;
7927 i.tm.operands--;
7928 }
7929 else if (i.tm.opcode_modifier.implicitquadgroup)
7930 {
7931 unsigned int regnum, first_reg_in_group, last_reg_in_group;
7932
7933 /* The second operand must be {x,y,z}mmN, where N is a multiple of 4. */
7934 gas_assert (i.operands >= 2 && i.types[1].bitfield.class == RegSIMD);
7935 regnum = register_number (i.op[1].regs);
7936 first_reg_in_group = regnum & ~3;
7937 last_reg_in_group = first_reg_in_group + 3;
7938 if (regnum != first_reg_in_group)
7939 as_warn (_("source register `%s%s' implicitly denotes"
7940 " `%s%.3s%u' to `%s%.3s%u' source group in `%s'"),
7941 register_prefix, i.op[1].regs->reg_name,
7942 register_prefix, i.op[1].regs->reg_name, first_reg_in_group,
7943 register_prefix, i.op[1].regs->reg_name, last_reg_in_group,
7944 i.tm.name);
7945 }
7946 else if (i.tm.opcode_modifier.regkludge)
7947 {
7948 /* The imul $imm, %reg instruction is converted into
7949 imul $imm, %reg, %reg, and the clr %reg instruction
7950 is converted into xor %reg, %reg. */
7951
7952 unsigned int first_reg_op;
7953
7954 if (operand_type_check (i.types[0], reg))
7955 first_reg_op = 0;
7956 else
7957 first_reg_op = 1;
7958 /* Pretend we saw the extra register operand. */
7959 gas_assert (i.reg_operands == 1
7960 && i.op[first_reg_op + 1].regs == 0);
7961 i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
7962 i.types[first_reg_op + 1] = i.types[first_reg_op];
7963 i.operands++;
7964 i.reg_operands++;
7965 }
7966
7967 if (i.tm.opcode_modifier.modrm)
7968 {
7969 /* The opcode is completed (modulo i.tm.extension_opcode which
7970 must be put into the modrm byte). Now, we make the modrm and
7971 index base bytes based on all the info we've collected. */
7972
7973 default_seg = build_modrm_byte ();
7974 }
7975 else if (i.types[0].bitfield.class == SReg)
7976 {
7977 if (flag_code != CODE_64BIT
7978 ? i.tm.base_opcode == POP_SEG_SHORT
7979 && i.op[0].regs->reg_num == 1
7980 : (i.tm.base_opcode | 1) == (POP_SEG386_SHORT & 0xff)
7981 && i.op[0].regs->reg_num < 4)
7982 {
7983 as_bad (_("you can't `%s %s%s'"),
7984 i.tm.name, register_prefix, i.op[0].regs->reg_name);
7985 return 0;
7986 }
7987 if (i.op[0].regs->reg_num > 3
7988 && i.tm.opcode_modifier.opcodespace == SPACE_BASE )
7989 {
7990 i.tm.base_opcode ^= (POP_SEG_SHORT ^ POP_SEG386_SHORT) & 0xff;
7991 i.tm.opcode_modifier.opcodespace = SPACE_0F;
7992 }
7993 i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
7994 }
7995 else if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
7996 && (i.tm.base_opcode & ~3) == MOV_AX_DISP32)
7997 {
7998 default_seg = reg_ds;
7999 }
8000 else if (i.tm.opcode_modifier.isstring)
8001 {
8002 /* For the string instructions that allow a segment override
8003 on one of their operands, the default segment is ds. */
8004 default_seg = reg_ds;
8005 }
8006 else if (i.short_form)
8007 {
8008 /* The register or float register operand is in operand
8009 0 or 1. */
8010 unsigned int op = i.tm.operand_types[0].bitfield.class != Reg;
8011
8012 /* Register goes in low 3 bits of opcode. */
8013 i.tm.base_opcode |= i.op[op].regs->reg_num;
8014 if ((i.op[op].regs->reg_flags & RegRex) != 0)
8015 i.rex |= REX_B;
8016 if (!quiet_warnings && i.tm.opcode_modifier.ugh)
8017 {
8018 /* Warn about some common errors, but press on regardless.
8019 The first case can be generated by gcc (<= 2.8.1). */
8020 if (i.operands == 2)
8021 {
8022 /* Reversed arguments on faddp, fsubp, etc. */
8023 as_warn (_("translating to `%s %s%s,%s%s'"), i.tm.name,
8024 register_prefix, i.op[!intel_syntax].regs->reg_name,
8025 register_prefix, i.op[intel_syntax].regs->reg_name);
8026 }
8027 else
8028 {
8029 /* Extraneous `l' suffix on fp insn. */
8030 as_warn (_("translating to `%s %s%s'"), i.tm.name,
8031 register_prefix, i.op[0].regs->reg_name);
8032 }
8033 }
8034 }
8035
8036 if ((i.seg[0] || i.prefix[SEG_PREFIX])
8037 && i.tm.base_opcode == 0x8d /* lea */
8038 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
8039 && !is_any_vex_encoding(&i.tm))
8040 {
8041 if (!quiet_warnings)
8042 as_warn (_("segment override on `%s' is ineffectual"), i.tm.name);
8043 if (optimize)
8044 {
8045 i.seg[0] = NULL;
8046 i.prefix[SEG_PREFIX] = 0;
8047 }
8048 }
8049
8050 /* If a segment was explicitly specified, and the specified segment
8051 is neither the default nor the one already recorded from a prefix,
8052 use an opcode prefix to select it. If we never figured out what
8053 the default segment is, then default_seg will be zero at this
8054 point, and the specified segment prefix will always be used. */
8055 if (i.seg[0]
8056 && i.seg[0] != default_seg
8057 && i386_seg_prefixes[i.seg[0]->reg_num] != i.prefix[SEG_PREFIX])
8058 {
8059 if (!add_prefix (i386_seg_prefixes[i.seg[0]->reg_num]))
8060 return 0;
8061 }
8062 return 1;
8063 }
8064
8065 static INLINE void set_rex_vrex (const reg_entry *r, unsigned int rex_bit,
8066 bool do_sse2avx)
8067 {
8068 if (r->reg_flags & RegRex)
8069 {
8070 if (i.rex & rex_bit)
8071 as_bad (_("same type of prefix used twice"));
8072 i.rex |= rex_bit;
8073 }
8074 else if (do_sse2avx && (i.rex & rex_bit) && i.vex.register_specifier)
8075 {
8076 gas_assert (i.vex.register_specifier == r);
8077 i.vex.register_specifier += 8;
8078 }
8079
8080 if (r->reg_flags & RegVRex)
8081 i.vrex |= rex_bit;
8082 }
8083
8084 static const reg_entry *
8085 build_modrm_byte (void)
8086 {
8087 const reg_entry *default_seg = NULL;
8088 unsigned int source, dest;
8089 int vex_3_sources;
8090
8091 vex_3_sources = i.tm.opcode_modifier.vexsources == VEX3SOURCES;
8092 if (vex_3_sources)
8093 {
8094 unsigned int nds, reg_slot;
8095 expressionS *exp;
8096
8097 dest = i.operands - 1;
8098 nds = dest - 1;
8099
8100 /* There are 2 kinds of instructions:
8101 1. 5 operands: 4 register operands or 3 register operands
8102 plus 1 memory operand plus one Imm4 operand, VexXDS, and
8103 VexW0 or VexW1. The destination must be either XMM, YMM or
8104 ZMM register.
8105 2. 4 operands: 4 register operands or 3 register operands
8106 plus 1 memory operand, with VexXDS. */
8107 gas_assert ((i.reg_operands == 4
8108 || (i.reg_operands == 3 && i.mem_operands == 1))
8109 && i.tm.opcode_modifier.vexvvvv == VEXXDS
8110 && i.tm.opcode_modifier.vexw
8111 && i.tm.operand_types[dest].bitfield.class == RegSIMD);
8112
8113 /* If VexW1 is set, the first non-immediate operand is the source and
8114 the second non-immediate one is encoded in the immediate operand. */
8115 if (i.tm.opcode_modifier.vexw == VEXW1)
8116 {
8117 source = i.imm_operands;
8118 reg_slot = i.imm_operands + 1;
8119 }
8120 else
8121 {
8122 source = i.imm_operands + 1;
8123 reg_slot = i.imm_operands;
8124 }
8125
8126 if (i.imm_operands == 0)
8127 {
8128 /* When there is no immediate operand, generate an 8bit
8129 immediate operand to encode the first operand. */
8130 exp = &im_expressions[i.imm_operands++];
8131 i.op[i.operands].imms = exp;
8132 i.types[i.operands] = imm8;
8133 i.operands++;
8134
8135 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
8136 exp->X_op = O_constant;
8137 exp->X_add_number = register_number (i.op[reg_slot].regs) << 4;
8138 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
8139 }
8140 else
8141 {
8142 gas_assert (i.imm_operands == 1);
8143 gas_assert (fits_in_imm4 (i.op[0].imms->X_add_number));
8144 gas_assert (!i.tm.opcode_modifier.immext);
8145
8146 /* Turn on Imm8 again so that output_imm will generate it. */
8147 i.types[0].bitfield.imm8 = 1;
8148
8149 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
8150 i.op[0].imms->X_add_number
8151 |= register_number (i.op[reg_slot].regs) << 4;
8152 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
8153 }
8154
8155 gas_assert (i.tm.operand_types[nds].bitfield.class == RegSIMD);
8156 i.vex.register_specifier = i.op[nds].regs;
8157 }
8158 else
8159 source = dest = 0;
8160
8161 /* i.reg_operands MUST be the number of real register operands;
8162 implicit registers do not count. If there are 3 register
8163 operands, it must be a instruction with VexNDS. For a
8164 instruction with VexNDD, the destination register is encoded
8165 in VEX prefix. If there are 4 register operands, it must be
8166 a instruction with VEX prefix and 3 sources. */
8167 if (i.mem_operands == 0
8168 && ((i.reg_operands == 2
8169 && i.tm.opcode_modifier.vexvvvv <= VEXXDS)
8170 || (i.reg_operands == 3
8171 && i.tm.opcode_modifier.vexvvvv == VEXXDS)
8172 || (i.reg_operands == 4 && vex_3_sources)))
8173 {
8174 switch (i.operands)
8175 {
8176 case 2:
8177 source = 0;
8178 break;
8179 case 3:
8180 /* When there are 3 operands, one of them may be immediate,
8181 which may be the first or the last operand. Otherwise,
8182 the first operand must be shift count register (cl) or it
8183 is an instruction with VexNDS. */
8184 gas_assert (i.imm_operands == 1
8185 || (i.imm_operands == 0
8186 && (i.tm.opcode_modifier.vexvvvv == VEXXDS
8187 || (i.types[0].bitfield.instance == RegC
8188 && i.types[0].bitfield.byte))));
8189 if (operand_type_check (i.types[0], imm)
8190 || (i.types[0].bitfield.instance == RegC
8191 && i.types[0].bitfield.byte))
8192 source = 1;
8193 else
8194 source = 0;
8195 break;
8196 case 4:
8197 /* When there are 4 operands, the first two must be 8bit
8198 immediate operands. The source operand will be the 3rd
8199 one.
8200
8201 For instructions with VexNDS, if the first operand
8202 an imm8, the source operand is the 2nd one. If the last
8203 operand is imm8, the source operand is the first one. */
8204 gas_assert ((i.imm_operands == 2
8205 && i.types[0].bitfield.imm8
8206 && i.types[1].bitfield.imm8)
8207 || (i.tm.opcode_modifier.vexvvvv == VEXXDS
8208 && i.imm_operands == 1
8209 && (i.types[0].bitfield.imm8
8210 || i.types[i.operands - 1].bitfield.imm8
8211 || i.rounding.type != rc_none)));
8212 if (i.imm_operands == 2)
8213 source = 2;
8214 else
8215 {
8216 if (i.types[0].bitfield.imm8)
8217 source = 1;
8218 else
8219 source = 0;
8220 }
8221 break;
8222 case 5:
8223 if (is_evex_encoding (&i.tm))
8224 {
8225 /* For EVEX instructions, when there are 5 operands, the
8226 first one must be immediate operand. If the second one
8227 is immediate operand, the source operand is the 3th
8228 one. If the last one is immediate operand, the source
8229 operand is the 2nd one. */
8230 gas_assert (i.imm_operands == 2
8231 && i.tm.opcode_modifier.sae
8232 && operand_type_check (i.types[0], imm));
8233 if (operand_type_check (i.types[1], imm))
8234 source = 2;
8235 else if (operand_type_check (i.types[4], imm))
8236 source = 1;
8237 else
8238 abort ();
8239 }
8240 break;
8241 default:
8242 abort ();
8243 }
8244
8245 if (!vex_3_sources)
8246 {
8247 dest = source + 1;
8248
8249 /* RC/SAE operand could be between DEST and SRC. That happens
8250 when one operand is GPR and the other one is XMM/YMM/ZMM
8251 register. */
8252 if (i.rounding.type != rc_none && i.rounding.operand == dest)
8253 dest++;
8254
8255 if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
8256 {
8257 /* For instructions with VexNDS, the register-only source
8258 operand must be a 32/64bit integer, XMM, YMM, ZMM, or mask
8259 register. It is encoded in VEX prefix. */
8260
8261 i386_operand_type op;
8262 unsigned int vvvv;
8263
8264 /* Swap two source operands if needed. */
8265 if (i.tm.opcode_modifier.swapsources)
8266 {
8267 vvvv = source;
8268 source = dest;
8269 }
8270 else
8271 vvvv = dest;
8272
8273 op = i.tm.operand_types[vvvv];
8274 if ((dest + 1) >= i.operands
8275 || ((op.bitfield.class != Reg
8276 || (!op.bitfield.dword && !op.bitfield.qword))
8277 && op.bitfield.class != RegSIMD
8278 && !operand_type_equal (&op, &regmask)))
8279 abort ();
8280 i.vex.register_specifier = i.op[vvvv].regs;
8281 dest++;
8282 }
8283 }
8284
8285 i.rm.mode = 3;
8286 /* One of the register operands will be encoded in the i.rm.reg
8287 field, the other in the combined i.rm.mode and i.rm.regmem
8288 fields. If no form of this instruction supports a memory
8289 destination operand, then we assume the source operand may
8290 sometimes be a memory operand and so we need to store the
8291 destination in the i.rm.reg field. */
8292 if (!i.tm.opcode_modifier.regmem
8293 && operand_type_check (i.tm.operand_types[dest], anymem) == 0)
8294 {
8295 i.rm.reg = i.op[dest].regs->reg_num;
8296 i.rm.regmem = i.op[source].regs->reg_num;
8297 set_rex_vrex (i.op[dest].regs, REX_R, i.tm.opcode_modifier.sse2avx);
8298 set_rex_vrex (i.op[source].regs, REX_B, false);
8299 }
8300 else
8301 {
8302 i.rm.reg = i.op[source].regs->reg_num;
8303 i.rm.regmem = i.op[dest].regs->reg_num;
8304 set_rex_vrex (i.op[dest].regs, REX_B, i.tm.opcode_modifier.sse2avx);
8305 set_rex_vrex (i.op[source].regs, REX_R, false);
8306 }
8307 if (flag_code != CODE_64BIT && (i.rex & REX_R))
8308 {
8309 if (i.types[!i.tm.opcode_modifier.regmem].bitfield.class != RegCR)
8310 abort ();
8311 i.rex &= ~REX_R;
8312 add_prefix (LOCK_PREFIX_OPCODE);
8313 }
8314 }
8315 else
8316 { /* If it's not 2 reg operands... */
8317 unsigned int mem;
8318
8319 if (i.mem_operands)
8320 {
8321 unsigned int fake_zero_displacement = 0;
8322 unsigned int op;
8323
8324 for (op = 0; op < i.operands; op++)
8325 if (i.flags[op] & Operand_Mem)
8326 break;
8327 gas_assert (op < i.operands);
8328
8329 if (i.tm.opcode_modifier.sib)
8330 {
8331 /* The index register of VSIB shouldn't be RegIZ. */
8332 if (i.tm.opcode_modifier.sib != SIBMEM
8333 && i.index_reg->reg_num == RegIZ)
8334 abort ();
8335
8336 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8337 if (!i.base_reg)
8338 {
8339 i.sib.base = NO_BASE_REGISTER;
8340 i.sib.scale = i.log2_scale_factor;
8341 i.types[op] = operand_type_and_not (i.types[op], anydisp);
8342 if (want_disp32 (&i.tm))
8343 i.types[op].bitfield.disp32 = 1;
8344 else
8345 i.types[op].bitfield.disp32s = 1;
8346 }
8347
8348 /* Since the mandatory SIB always has index register, so
8349 the code logic remains unchanged. The non-mandatory SIB
8350 without index register is allowed and will be handled
8351 later. */
8352 if (i.index_reg)
8353 {
8354 if (i.index_reg->reg_num == RegIZ)
8355 i.sib.index = NO_INDEX_REGISTER;
8356 else
8357 i.sib.index = i.index_reg->reg_num;
8358 set_rex_vrex (i.index_reg, REX_X, false);
8359 }
8360 }
8361
8362 default_seg = reg_ds;
8363
8364 if (i.base_reg == 0)
8365 {
8366 i.rm.mode = 0;
8367 if (!i.disp_operands)
8368 fake_zero_displacement = 1;
8369 if (i.index_reg == 0)
8370 {
8371 /* Both check for VSIB and mandatory non-vector SIB. */
8372 gas_assert (!i.tm.opcode_modifier.sib
8373 || i.tm.opcode_modifier.sib == SIBMEM);
8374 /* Operand is just <disp> */
8375 i.types[op] = operand_type_and_not (i.types[op], anydisp);
8376 if (flag_code == CODE_64BIT)
8377 {
8378 /* 64bit mode overwrites the 32bit absolute
8379 addressing by RIP relative addressing and
8380 absolute addressing is encoded by one of the
8381 redundant SIB forms. */
8382 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8383 i.sib.base = NO_BASE_REGISTER;
8384 i.sib.index = NO_INDEX_REGISTER;
8385 if (want_disp32 (&i.tm))
8386 i.types[op].bitfield.disp32 = 1;
8387 else
8388 i.types[op].bitfield.disp32s = 1;
8389 }
8390 else if ((flag_code == CODE_16BIT)
8391 ^ (i.prefix[ADDR_PREFIX] != 0))
8392 {
8393 i.rm.regmem = NO_BASE_REGISTER_16;
8394 i.types[op].bitfield.disp16 = 1;
8395 }
8396 else
8397 {
8398 i.rm.regmem = NO_BASE_REGISTER;
8399 i.types[op].bitfield.disp32 = 1;
8400 }
8401 }
8402 else if (!i.tm.opcode_modifier.sib)
8403 {
8404 /* !i.base_reg && i.index_reg */
8405 if (i.index_reg->reg_num == RegIZ)
8406 i.sib.index = NO_INDEX_REGISTER;
8407 else
8408 i.sib.index = i.index_reg->reg_num;
8409 i.sib.base = NO_BASE_REGISTER;
8410 i.sib.scale = i.log2_scale_factor;
8411 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8412 i.types[op] = operand_type_and_not (i.types[op], anydisp);
8413 if (want_disp32 (&i.tm))
8414 i.types[op].bitfield.disp32 = 1;
8415 else
8416 i.types[op].bitfield.disp32s = 1;
8417 if ((i.index_reg->reg_flags & RegRex) != 0)
8418 i.rex |= REX_X;
8419 }
8420 }
8421 /* RIP addressing for 64bit mode. */
8422 else if (i.base_reg->reg_num == RegIP)
8423 {
8424 gas_assert (!i.tm.opcode_modifier.sib);
8425 i.rm.regmem = NO_BASE_REGISTER;
8426 i.types[op].bitfield.disp8 = 0;
8427 i.types[op].bitfield.disp16 = 0;
8428 i.types[op].bitfield.disp32 = 0;
8429 i.types[op].bitfield.disp32s = 1;
8430 i.types[op].bitfield.disp64 = 0;
8431 i.flags[op] |= Operand_PCrel;
8432 if (! i.disp_operands)
8433 fake_zero_displacement = 1;
8434 }
8435 else if (i.base_reg->reg_type.bitfield.word)
8436 {
8437 gas_assert (!i.tm.opcode_modifier.sib);
8438 switch (i.base_reg->reg_num)
8439 {
8440 case 3: /* (%bx) */
8441 if (i.index_reg == 0)
8442 i.rm.regmem = 7;
8443 else /* (%bx,%si) -> 0, or (%bx,%di) -> 1 */
8444 i.rm.regmem = i.index_reg->reg_num - 6;
8445 break;
8446 case 5: /* (%bp) */
8447 default_seg = reg_ss;
8448 if (i.index_reg == 0)
8449 {
8450 i.rm.regmem = 6;
8451 if (operand_type_check (i.types[op], disp) == 0)
8452 {
8453 /* fake (%bp) into 0(%bp) */
8454 if (i.disp_encoding == disp_encoding_16bit)
8455 i.types[op].bitfield.disp16 = 1;
8456 else
8457 i.types[op].bitfield.disp8 = 1;
8458 fake_zero_displacement = 1;
8459 }
8460 }
8461 else /* (%bp,%si) -> 2, or (%bp,%di) -> 3 */
8462 i.rm.regmem = i.index_reg->reg_num - 6 + 2;
8463 break;
8464 default: /* (%si) -> 4 or (%di) -> 5 */
8465 i.rm.regmem = i.base_reg->reg_num - 6 + 4;
8466 }
8467 if (!fake_zero_displacement
8468 && !i.disp_operands
8469 && i.disp_encoding)
8470 {
8471 fake_zero_displacement = 1;
8472 if (i.disp_encoding == disp_encoding_8bit)
8473 i.types[op].bitfield.disp8 = 1;
8474 else
8475 i.types[op].bitfield.disp16 = 1;
8476 }
8477 i.rm.mode = mode_from_disp_size (i.types[op]);
8478 }
8479 else /* i.base_reg and 32/64 bit mode */
8480 {
8481 if (operand_type_check (i.types[op], disp))
8482 {
8483 i.types[op].bitfield.disp16 = 0;
8484 i.types[op].bitfield.disp64 = 0;
8485 if (!want_disp32 (&i.tm))
8486 {
8487 i.types[op].bitfield.disp32 = 0;
8488 i.types[op].bitfield.disp32s = 1;
8489 }
8490 else
8491 {
8492 i.types[op].bitfield.disp32 = 1;
8493 i.types[op].bitfield.disp32s = 0;
8494 }
8495 }
8496
8497 if (!i.tm.opcode_modifier.sib)
8498 i.rm.regmem = i.base_reg->reg_num;
8499 if ((i.base_reg->reg_flags & RegRex) != 0)
8500 i.rex |= REX_B;
8501 i.sib.base = i.base_reg->reg_num;
8502 /* x86-64 ignores REX prefix bit here to avoid decoder
8503 complications. */
8504 if (!(i.base_reg->reg_flags & RegRex)
8505 && (i.base_reg->reg_num == EBP_REG_NUM
8506 || i.base_reg->reg_num == ESP_REG_NUM))
8507 default_seg = reg_ss;
8508 if (i.base_reg->reg_num == 5 && i.disp_operands == 0)
8509 {
8510 fake_zero_displacement = 1;
8511 if (i.disp_encoding == disp_encoding_32bit)
8512 i.types[op].bitfield.disp32 = 1;
8513 else
8514 i.types[op].bitfield.disp8 = 1;
8515 }
8516 i.sib.scale = i.log2_scale_factor;
8517 if (i.index_reg == 0)
8518 {
8519 /* Only check for VSIB. */
8520 gas_assert (i.tm.opcode_modifier.sib != VECSIB128
8521 && i.tm.opcode_modifier.sib != VECSIB256
8522 && i.tm.opcode_modifier.sib != VECSIB512);
8523
8524 /* <disp>(%esp) becomes two byte modrm with no index
8525 register. We've already stored the code for esp
8526 in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
8527 Any base register besides %esp will not use the
8528 extra modrm byte. */
8529 i.sib.index = NO_INDEX_REGISTER;
8530 }
8531 else if (!i.tm.opcode_modifier.sib)
8532 {
8533 if (i.index_reg->reg_num == RegIZ)
8534 i.sib.index = NO_INDEX_REGISTER;
8535 else
8536 i.sib.index = i.index_reg->reg_num;
8537 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8538 if ((i.index_reg->reg_flags & RegRex) != 0)
8539 i.rex |= REX_X;
8540 }
8541
8542 if (i.disp_operands
8543 && (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
8544 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL))
8545 i.rm.mode = 0;
8546 else
8547 {
8548 if (!fake_zero_displacement
8549 && !i.disp_operands
8550 && i.disp_encoding)
8551 {
8552 fake_zero_displacement = 1;
8553 if (i.disp_encoding == disp_encoding_8bit)
8554 i.types[op].bitfield.disp8 = 1;
8555 else
8556 i.types[op].bitfield.disp32 = 1;
8557 }
8558 i.rm.mode = mode_from_disp_size (i.types[op]);
8559 }
8560 }
8561
8562 if (fake_zero_displacement)
8563 {
8564 /* Fakes a zero displacement assuming that i.types[op]
8565 holds the correct displacement size. */
8566 expressionS *exp;
8567
8568 gas_assert (i.op[op].disps == 0);
8569 exp = &disp_expressions[i.disp_operands++];
8570 i.op[op].disps = exp;
8571 exp->X_op = O_constant;
8572 exp->X_add_number = 0;
8573 exp->X_add_symbol = (symbolS *) 0;
8574 exp->X_op_symbol = (symbolS *) 0;
8575 }
8576
8577 mem = op;
8578 }
8579 else
8580 mem = ~0;
8581
8582 if (i.tm.opcode_modifier.vexsources == XOP2SOURCES)
8583 {
8584 if (operand_type_check (i.types[0], imm))
8585 i.vex.register_specifier = NULL;
8586 else
8587 {
8588 /* VEX.vvvv encodes one of the sources when the first
8589 operand is not an immediate. */
8590 if (i.tm.opcode_modifier.vexw == VEXW0)
8591 i.vex.register_specifier = i.op[0].regs;
8592 else
8593 i.vex.register_specifier = i.op[1].regs;
8594 }
8595
8596 /* Destination is a XMM register encoded in the ModRM.reg
8597 and VEX.R bit. */
8598 i.rm.reg = i.op[2].regs->reg_num;
8599 if ((i.op[2].regs->reg_flags & RegRex) != 0)
8600 i.rex |= REX_R;
8601
8602 /* ModRM.rm and VEX.B encodes the other source. */
8603 if (!i.mem_operands)
8604 {
8605 i.rm.mode = 3;
8606
8607 if (i.tm.opcode_modifier.vexw == VEXW0)
8608 i.rm.regmem = i.op[1].regs->reg_num;
8609 else
8610 i.rm.regmem = i.op[0].regs->reg_num;
8611
8612 if ((i.op[1].regs->reg_flags & RegRex) != 0)
8613 i.rex |= REX_B;
8614 }
8615 }
8616 else if (i.tm.opcode_modifier.vexvvvv == VEXLWP)
8617 {
8618 i.vex.register_specifier = i.op[2].regs;
8619 if (!i.mem_operands)
8620 {
8621 i.rm.mode = 3;
8622 i.rm.regmem = i.op[1].regs->reg_num;
8623 if ((i.op[1].regs->reg_flags & RegRex) != 0)
8624 i.rex |= REX_B;
8625 }
8626 }
8627 /* Fill in i.rm.reg or i.rm.regmem field with register operand
8628 (if any) based on i.tm.extension_opcode. Again, we must be
8629 careful to make sure that segment/control/debug/test/MMX
8630 registers are coded into the i.rm.reg field. */
8631 else if (i.reg_operands)
8632 {
8633 unsigned int op;
8634 unsigned int vex_reg = ~0;
8635
8636 for (op = 0; op < i.operands; op++)
8637 if (i.types[op].bitfield.class == Reg
8638 || i.types[op].bitfield.class == RegBND
8639 || i.types[op].bitfield.class == RegMask
8640 || i.types[op].bitfield.class == SReg
8641 || i.types[op].bitfield.class == RegCR
8642 || i.types[op].bitfield.class == RegDR
8643 || i.types[op].bitfield.class == RegTR
8644 || i.types[op].bitfield.class == RegSIMD
8645 || i.types[op].bitfield.class == RegMMX)
8646 break;
8647
8648 if (vex_3_sources)
8649 op = dest;
8650 else if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
8651 {
8652 /* For instructions with VexNDS, the register-only
8653 source operand is encoded in VEX prefix. */
8654 gas_assert (mem != (unsigned int) ~0);
8655
8656 if (op > mem)
8657 {
8658 vex_reg = op++;
8659 gas_assert (op < i.operands);
8660 }
8661 else
8662 {
8663 /* Check register-only source operand when two source
8664 operands are swapped. */
8665 if (!i.tm.operand_types[op].bitfield.baseindex
8666 && i.tm.operand_types[op + 1].bitfield.baseindex)
8667 {
8668 vex_reg = op;
8669 op += 2;
8670 gas_assert (mem == (vex_reg + 1)
8671 && op < i.operands);
8672 }
8673 else
8674 {
8675 vex_reg = op + 1;
8676 gas_assert (vex_reg < i.operands);
8677 }
8678 }
8679 }
8680 else if (i.tm.opcode_modifier.vexvvvv == VEXNDD)
8681 {
8682 /* For instructions with VexNDD, the register destination
8683 is encoded in VEX prefix. */
8684 if (i.mem_operands == 0)
8685 {
8686 /* There is no memory operand. */
8687 gas_assert ((op + 2) == i.operands);
8688 vex_reg = op + 1;
8689 }
8690 else
8691 {
8692 /* There are only 2 non-immediate operands. */
8693 gas_assert (op < i.imm_operands + 2
8694 && i.operands == i.imm_operands + 2);
8695 vex_reg = i.imm_operands + 1;
8696 }
8697 }
8698 else
8699 gas_assert (op < i.operands);
8700
8701 if (vex_reg != (unsigned int) ~0)
8702 {
8703 i386_operand_type *type = &i.tm.operand_types[vex_reg];
8704
8705 if ((type->bitfield.class != Reg
8706 || (!type->bitfield.dword && !type->bitfield.qword))
8707 && type->bitfield.class != RegSIMD
8708 && !operand_type_equal (type, &regmask))
8709 abort ();
8710
8711 i.vex.register_specifier = i.op[vex_reg].regs;
8712 }
8713
8714 /* Don't set OP operand twice. */
8715 if (vex_reg != op)
8716 {
8717 /* If there is an extension opcode to put here, the
8718 register number must be put into the regmem field. */
8719 if (i.tm.extension_opcode != None)
8720 {
8721 i.rm.regmem = i.op[op].regs->reg_num;
8722 set_rex_vrex (i.op[op].regs, REX_B,
8723 i.tm.opcode_modifier.sse2avx);
8724 }
8725 else
8726 {
8727 i.rm.reg = i.op[op].regs->reg_num;
8728 set_rex_vrex (i.op[op].regs, REX_R,
8729 i.tm.opcode_modifier.sse2avx);
8730 }
8731 }
8732
8733 /* Now, if no memory operand has set i.rm.mode = 0, 1, 2 we
8734 must set it to 3 to indicate this is a register operand
8735 in the regmem field. */
8736 if (!i.mem_operands)
8737 i.rm.mode = 3;
8738 }
8739
8740 /* Fill in i.rm.reg field with extension opcode (if any). */
8741 if (i.tm.extension_opcode != None)
8742 i.rm.reg = i.tm.extension_opcode;
8743 }
8744 return default_seg;
8745 }
8746
8747 static INLINE void
8748 frag_opcode_byte (unsigned char byte)
8749 {
8750 if (now_seg != absolute_section)
8751 FRAG_APPEND_1_CHAR (byte);
8752 else
8753 ++abs_section_offset;
8754 }
8755
8756 static unsigned int
8757 flip_code16 (unsigned int code16)
8758 {
8759 gas_assert (i.tm.operands == 1);
8760
8761 return !(i.prefix[REX_PREFIX] & REX_W)
8762 && (code16 ? i.tm.operand_types[0].bitfield.disp32
8763 || i.tm.operand_types[0].bitfield.disp32s
8764 : i.tm.operand_types[0].bitfield.disp16)
8765 ? CODE16 : 0;
8766 }
8767
8768 static void
8769 output_branch (void)
8770 {
8771 char *p;
8772 int size;
8773 int code16;
8774 int prefix;
8775 relax_substateT subtype;
8776 symbolS *sym;
8777 offsetT off;
8778
8779 if (now_seg == absolute_section)
8780 {
8781 as_bad (_("relaxable branches not supported in absolute section"));
8782 return;
8783 }
8784
8785 code16 = flag_code == CODE_16BIT ? CODE16 : 0;
8786 size = i.disp_encoding == disp_encoding_32bit ? BIG : SMALL;
8787
8788 prefix = 0;
8789 if (i.prefix[DATA_PREFIX] != 0)
8790 {
8791 prefix = 1;
8792 i.prefixes -= 1;
8793 code16 ^= flip_code16(code16);
8794 }
8795 /* Pentium4 branch hints. */
8796 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8797 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
8798 {
8799 prefix++;
8800 i.prefixes--;
8801 }
8802 if (i.prefix[REX_PREFIX] != 0)
8803 {
8804 prefix++;
8805 i.prefixes--;
8806 }
8807
8808 /* BND prefixed jump. */
8809 if (i.prefix[BND_PREFIX] != 0)
8810 {
8811 prefix++;
8812 i.prefixes--;
8813 }
8814
8815 if (i.prefixes != 0)
8816 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
8817
8818 /* It's always a symbol; End frag & setup for relax.
8819 Make sure there is enough room in this frag for the largest
8820 instruction we may generate in md_convert_frag. This is 2
8821 bytes for the opcode and room for the prefix and largest
8822 displacement. */
8823 frag_grow (prefix + 2 + 4);
8824 /* Prefix and 1 opcode byte go in fr_fix. */
8825 p = frag_more (prefix + 1);
8826 if (i.prefix[DATA_PREFIX] != 0)
8827 *p++ = DATA_PREFIX_OPCODE;
8828 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
8829 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
8830 *p++ = i.prefix[SEG_PREFIX];
8831 if (i.prefix[BND_PREFIX] != 0)
8832 *p++ = BND_PREFIX_OPCODE;
8833 if (i.prefix[REX_PREFIX] != 0)
8834 *p++ = i.prefix[REX_PREFIX];
8835 *p = i.tm.base_opcode;
8836
8837 if ((unsigned char) *p == JUMP_PC_RELATIVE)
8838 subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, size);
8839 else if (cpu_arch_flags.bitfield.cpui386)
8840 subtype = ENCODE_RELAX_STATE (COND_JUMP, size);
8841 else
8842 subtype = ENCODE_RELAX_STATE (COND_JUMP86, size);
8843 subtype |= code16;
8844
8845 sym = i.op[0].disps->X_add_symbol;
8846 off = i.op[0].disps->X_add_number;
8847
8848 if (i.op[0].disps->X_op != O_constant
8849 && i.op[0].disps->X_op != O_symbol)
8850 {
8851 /* Handle complex expressions. */
8852 sym = make_expr_symbol (i.op[0].disps);
8853 off = 0;
8854 }
8855
8856 /* 1 possible extra opcode + 4 byte displacement go in var part.
8857 Pass reloc in fr_var. */
8858 frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
8859 }
8860
8861 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8862 /* Return TRUE iff PLT32 relocation should be used for branching to
8863 symbol S. */
8864
8865 static bool
8866 need_plt32_p (symbolS *s)
8867 {
8868 /* PLT32 relocation is ELF only. */
8869 if (!IS_ELF)
8870 return false;
8871
8872 #ifdef TE_SOLARIS
8873 /* Don't emit PLT32 relocation on Solaris: neither native linker nor
8874 krtld support it. */
8875 return false;
8876 #endif
8877
8878 /* Since there is no need to prepare for PLT branch on x86-64, we
8879 can generate R_X86_64_PLT32, instead of R_X86_64_PC32, which can
8880 be used as a marker for 32-bit PC-relative branches. */
8881 if (!object_64bit)
8882 return false;
8883
8884 if (s == NULL)
8885 return false;
8886
8887 /* Weak or undefined symbol need PLT32 relocation. */
8888 if (S_IS_WEAK (s) || !S_IS_DEFINED (s))
8889 return true;
8890
8891 /* Non-global symbol doesn't need PLT32 relocation. */
8892 if (! S_IS_EXTERNAL (s))
8893 return false;
8894
8895 /* Other global symbols need PLT32 relocation. NB: Symbol with
8896 non-default visibilities are treated as normal global symbol
8897 so that PLT32 relocation can be used as a marker for 32-bit
8898 PC-relative branches. It is useful for linker relaxation. */
8899 return true;
8900 }
8901 #endif
8902
8903 static void
8904 output_jump (void)
8905 {
8906 char *p;
8907 int size;
8908 fixS *fixP;
8909 bfd_reloc_code_real_type jump_reloc = i.reloc[0];
8910
8911 if (i.tm.opcode_modifier.jump == JUMP_BYTE)
8912 {
8913 /* This is a loop or jecxz type instruction. */
8914 size = 1;
8915 if (i.prefix[ADDR_PREFIX] != 0)
8916 {
8917 frag_opcode_byte (ADDR_PREFIX_OPCODE);
8918 i.prefixes -= 1;
8919 }
8920 /* Pentium4 branch hints. */
8921 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8922 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
8923 {
8924 frag_opcode_byte (i.prefix[SEG_PREFIX]);
8925 i.prefixes--;
8926 }
8927 }
8928 else
8929 {
8930 int code16;
8931
8932 code16 = 0;
8933 if (flag_code == CODE_16BIT)
8934 code16 = CODE16;
8935
8936 if (i.prefix[DATA_PREFIX] != 0)
8937 {
8938 frag_opcode_byte (DATA_PREFIX_OPCODE);
8939 i.prefixes -= 1;
8940 code16 ^= flip_code16(code16);
8941 }
8942
8943 size = 4;
8944 if (code16)
8945 size = 2;
8946 }
8947
8948 /* BND prefixed jump. */
8949 if (i.prefix[BND_PREFIX] != 0)
8950 {
8951 frag_opcode_byte (i.prefix[BND_PREFIX]);
8952 i.prefixes -= 1;
8953 }
8954
8955 if (i.prefix[REX_PREFIX] != 0)
8956 {
8957 frag_opcode_byte (i.prefix[REX_PREFIX]);
8958 i.prefixes -= 1;
8959 }
8960
8961 if (i.prefixes != 0)
8962 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
8963
8964 if (now_seg == absolute_section)
8965 {
8966 abs_section_offset += i.opcode_length + size;
8967 return;
8968 }
8969
8970 p = frag_more (i.opcode_length + size);
8971 switch (i.opcode_length)
8972 {
8973 case 2:
8974 *p++ = i.tm.base_opcode >> 8;
8975 /* Fall through. */
8976 case 1:
8977 *p++ = i.tm.base_opcode;
8978 break;
8979 default:
8980 abort ();
8981 }
8982
8983 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8984 if (size == 4
8985 && jump_reloc == NO_RELOC
8986 && need_plt32_p (i.op[0].disps->X_add_symbol))
8987 jump_reloc = BFD_RELOC_X86_64_PLT32;
8988 #endif
8989
8990 jump_reloc = reloc (size, 1, 1, jump_reloc);
8991
8992 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
8993 i.op[0].disps, 1, jump_reloc);
8994
8995 /* All jumps handled here are signed, but don't unconditionally use a
8996 signed limit check for 32 and 16 bit jumps as we want to allow wrap
8997 around at 4G (outside of 64-bit mode) and 64k (except for XBEGIN)
8998 respectively. */
8999 switch (size)
9000 {
9001 case 1:
9002 fixP->fx_signed = 1;
9003 break;
9004
9005 case 2:
9006 if (i.tm.base_opcode == 0xc7f8)
9007 fixP->fx_signed = 1;
9008 break;
9009
9010 case 4:
9011 if (flag_code == CODE_64BIT)
9012 fixP->fx_signed = 1;
9013 break;
9014 }
9015 }
9016
9017 static void
9018 output_interseg_jump (void)
9019 {
9020 char *p;
9021 int size;
9022 int prefix;
9023 int code16;
9024
9025 code16 = 0;
9026 if (flag_code == CODE_16BIT)
9027 code16 = CODE16;
9028
9029 prefix = 0;
9030 if (i.prefix[DATA_PREFIX] != 0)
9031 {
9032 prefix = 1;
9033 i.prefixes -= 1;
9034 code16 ^= CODE16;
9035 }
9036
9037 gas_assert (!i.prefix[REX_PREFIX]);
9038
9039 size = 4;
9040 if (code16)
9041 size = 2;
9042
9043 if (i.prefixes != 0)
9044 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
9045
9046 if (now_seg == absolute_section)
9047 {
9048 abs_section_offset += prefix + 1 + 2 + size;
9049 return;
9050 }
9051
9052 /* 1 opcode; 2 segment; offset */
9053 p = frag_more (prefix + 1 + 2 + size);
9054
9055 if (i.prefix[DATA_PREFIX] != 0)
9056 *p++ = DATA_PREFIX_OPCODE;
9057
9058 if (i.prefix[REX_PREFIX] != 0)
9059 *p++ = i.prefix[REX_PREFIX];
9060
9061 *p++ = i.tm.base_opcode;
9062 if (i.op[1].imms->X_op == O_constant)
9063 {
9064 offsetT n = i.op[1].imms->X_add_number;
9065
9066 if (size == 2
9067 && !fits_in_unsigned_word (n)
9068 && !fits_in_signed_word (n))
9069 {
9070 as_bad (_("16-bit jump out of range"));
9071 return;
9072 }
9073 md_number_to_chars (p, n, size);
9074 }
9075 else
9076 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
9077 i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
9078
9079 p += size;
9080 if (i.op[0].imms->X_op == O_constant)
9081 md_number_to_chars (p, (valueT) i.op[0].imms->X_add_number, 2);
9082 else
9083 fix_new_exp (frag_now, p - frag_now->fr_literal, 2,
9084 i.op[0].imms, 0, reloc (2, 0, 0, i.reloc[0]));
9085 }
9086
9087 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9088 void
9089 x86_cleanup (void)
9090 {
9091 char *p;
9092 asection *seg = now_seg;
9093 subsegT subseg = now_subseg;
9094 asection *sec;
9095 unsigned int alignment, align_size_1;
9096 unsigned int isa_1_descsz, feature_2_descsz, descsz;
9097 unsigned int isa_1_descsz_raw, feature_2_descsz_raw;
9098 unsigned int padding;
9099
9100 if (!IS_ELF || !x86_used_note)
9101 return;
9102
9103 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X86;
9104
9105 /* The .note.gnu.property section layout:
9106
9107 Field Length Contents
9108 ---- ---- ----
9109 n_namsz 4 4
9110 n_descsz 4 The note descriptor size
9111 n_type 4 NT_GNU_PROPERTY_TYPE_0
9112 n_name 4 "GNU"
9113 n_desc n_descsz The program property array
9114 .... .... ....
9115 */
9116
9117 /* Create the .note.gnu.property section. */
9118 sec = subseg_new (NOTE_GNU_PROPERTY_SECTION_NAME, 0);
9119 bfd_set_section_flags (sec,
9120 (SEC_ALLOC
9121 | SEC_LOAD
9122 | SEC_DATA
9123 | SEC_HAS_CONTENTS
9124 | SEC_READONLY));
9125
9126 if (get_elf_backend_data (stdoutput)->s->elfclass == ELFCLASS64)
9127 {
9128 align_size_1 = 7;
9129 alignment = 3;
9130 }
9131 else
9132 {
9133 align_size_1 = 3;
9134 alignment = 2;
9135 }
9136
9137 bfd_set_section_alignment (sec, alignment);
9138 elf_section_type (sec) = SHT_NOTE;
9139
9140 /* GNU_PROPERTY_X86_ISA_1_USED: 4-byte type + 4-byte data size
9141 + 4-byte data */
9142 isa_1_descsz_raw = 4 + 4 + 4;
9143 /* Align GNU_PROPERTY_X86_ISA_1_USED. */
9144 isa_1_descsz = (isa_1_descsz_raw + align_size_1) & ~align_size_1;
9145
9146 feature_2_descsz_raw = isa_1_descsz;
9147 /* GNU_PROPERTY_X86_FEATURE_2_USED: 4-byte type + 4-byte data size
9148 + 4-byte data */
9149 feature_2_descsz_raw += 4 + 4 + 4;
9150 /* Align GNU_PROPERTY_X86_FEATURE_2_USED. */
9151 feature_2_descsz = ((feature_2_descsz_raw + align_size_1)
9152 & ~align_size_1);
9153
9154 descsz = feature_2_descsz;
9155 /* Section size: n_namsz + n_descsz + n_type + n_name + n_descsz. */
9156 p = frag_more (4 + 4 + 4 + 4 + descsz);
9157
9158 /* Write n_namsz. */
9159 md_number_to_chars (p, (valueT) 4, 4);
9160
9161 /* Write n_descsz. */
9162 md_number_to_chars (p + 4, (valueT) descsz, 4);
9163
9164 /* Write n_type. */
9165 md_number_to_chars (p + 4 * 2, (valueT) NT_GNU_PROPERTY_TYPE_0, 4);
9166
9167 /* Write n_name. */
9168 memcpy (p + 4 * 3, "GNU", 4);
9169
9170 /* Write 4-byte type. */
9171 md_number_to_chars (p + 4 * 4,
9172 (valueT) GNU_PROPERTY_X86_ISA_1_USED, 4);
9173
9174 /* Write 4-byte data size. */
9175 md_number_to_chars (p + 4 * 5, (valueT) 4, 4);
9176
9177 /* Write 4-byte data. */
9178 md_number_to_chars (p + 4 * 6, (valueT) x86_isa_1_used, 4);
9179
9180 /* Zero out paddings. */
9181 padding = isa_1_descsz - isa_1_descsz_raw;
9182 if (padding)
9183 memset (p + 4 * 7, 0, padding);
9184
9185 /* Write 4-byte type. */
9186 md_number_to_chars (p + isa_1_descsz + 4 * 4,
9187 (valueT) GNU_PROPERTY_X86_FEATURE_2_USED, 4);
9188
9189 /* Write 4-byte data size. */
9190 md_number_to_chars (p + isa_1_descsz + 4 * 5, (valueT) 4, 4);
9191
9192 /* Write 4-byte data. */
9193 md_number_to_chars (p + isa_1_descsz + 4 * 6,
9194 (valueT) x86_feature_2_used, 4);
9195
9196 /* Zero out paddings. */
9197 padding = feature_2_descsz - feature_2_descsz_raw;
9198 if (padding)
9199 memset (p + isa_1_descsz + 4 * 7, 0, padding);
9200
9201 /* We probably can't restore the current segment, for there likely
9202 isn't one yet... */
9203 if (seg && subseg)
9204 subseg_set (seg, subseg);
9205 }
9206 #endif
9207
9208 static unsigned int
9209 encoding_length (const fragS *start_frag, offsetT start_off,
9210 const char *frag_now_ptr)
9211 {
9212 unsigned int len = 0;
9213
9214 if (start_frag != frag_now)
9215 {
9216 const fragS *fr = start_frag;
9217
9218 do {
9219 len += fr->fr_fix;
9220 fr = fr->fr_next;
9221 } while (fr && fr != frag_now);
9222 }
9223
9224 return len - start_off + (frag_now_ptr - frag_now->fr_literal);
9225 }
9226
9227 /* Return 1 for test, and, cmp, add, sub, inc and dec which may
9228 be macro-fused with conditional jumps.
9229 NB: If TEST/AND/CMP/ADD/SUB/INC/DEC is of RIP relative address,
9230 or is one of the following format:
9231
9232 cmp m, imm
9233 add m, imm
9234 sub m, imm
9235 test m, imm
9236 and m, imm
9237 inc m
9238 dec m
9239
9240 it is unfusible. */
9241
9242 static int
9243 maybe_fused_with_jcc_p (enum mf_cmp_kind* mf_cmp_p)
9244 {
9245 /* No RIP address. */
9246 if (i.base_reg && i.base_reg->reg_num == RegIP)
9247 return 0;
9248
9249 /* No opcodes outside of base encoding space. */
9250 if (i.tm.opcode_modifier.opcodespace != SPACE_BASE)
9251 return 0;
9252
9253 /* add, sub without add/sub m, imm. */
9254 if (i.tm.base_opcode <= 5
9255 || (i.tm.base_opcode >= 0x28 && i.tm.base_opcode <= 0x2d)
9256 || ((i.tm.base_opcode | 3) == 0x83
9257 && (i.tm.extension_opcode == 0x5
9258 || i.tm.extension_opcode == 0x0)))
9259 {
9260 *mf_cmp_p = mf_cmp_alu_cmp;
9261 return !(i.mem_operands && i.imm_operands);
9262 }
9263
9264 /* and without and m, imm. */
9265 if ((i.tm.base_opcode >= 0x20 && i.tm.base_opcode <= 0x25)
9266 || ((i.tm.base_opcode | 3) == 0x83
9267 && i.tm.extension_opcode == 0x4))
9268 {
9269 *mf_cmp_p = mf_cmp_test_and;
9270 return !(i.mem_operands && i.imm_operands);
9271 }
9272
9273 /* test without test m imm. */
9274 if ((i.tm.base_opcode | 1) == 0x85
9275 || (i.tm.base_opcode | 1) == 0xa9
9276 || ((i.tm.base_opcode | 1) == 0xf7
9277 && i.tm.extension_opcode == 0))
9278 {
9279 *mf_cmp_p = mf_cmp_test_and;
9280 return !(i.mem_operands && i.imm_operands);
9281 }
9282
9283 /* cmp without cmp m, imm. */
9284 if ((i.tm.base_opcode >= 0x38 && i.tm.base_opcode <= 0x3d)
9285 || ((i.tm.base_opcode | 3) == 0x83
9286 && (i.tm.extension_opcode == 0x7)))
9287 {
9288 *mf_cmp_p = mf_cmp_alu_cmp;
9289 return !(i.mem_operands && i.imm_operands);
9290 }
9291
9292 /* inc, dec without inc/dec m. */
9293 if ((i.tm.cpu_flags.bitfield.cpuno64
9294 && (i.tm.base_opcode | 0xf) == 0x4f)
9295 || ((i.tm.base_opcode | 1) == 0xff
9296 && i.tm.extension_opcode <= 0x1))
9297 {
9298 *mf_cmp_p = mf_cmp_incdec;
9299 return !i.mem_operands;
9300 }
9301
9302 return 0;
9303 }
9304
9305 /* Return 1 if a FUSED_JCC_PADDING frag should be generated. */
9306
9307 static int
9308 add_fused_jcc_padding_frag_p (enum mf_cmp_kind* mf_cmp_p)
9309 {
9310 /* NB: Don't work with COND_JUMP86 without i386. */
9311 if (!align_branch_power
9312 || now_seg == absolute_section
9313 || !cpu_arch_flags.bitfield.cpui386
9314 || !(align_branch & align_branch_fused_bit))
9315 return 0;
9316
9317 if (maybe_fused_with_jcc_p (mf_cmp_p))
9318 {
9319 if (last_insn.kind == last_insn_other
9320 || last_insn.seg != now_seg)
9321 return 1;
9322 if (flag_debug)
9323 as_warn_where (last_insn.file, last_insn.line,
9324 _("`%s` skips -malign-branch-boundary on `%s`"),
9325 last_insn.name, i.tm.name);
9326 }
9327
9328 return 0;
9329 }
9330
9331 /* Return 1 if a BRANCH_PREFIX frag should be generated. */
9332
9333 static int
9334 add_branch_prefix_frag_p (void)
9335 {
9336 /* NB: Don't work with COND_JUMP86 without i386. Don't add prefix
9337 to PadLock instructions since they include prefixes in opcode. */
9338 if (!align_branch_power
9339 || !align_branch_prefix_size
9340 || now_seg == absolute_section
9341 || i.tm.cpu_flags.bitfield.cpupadlock
9342 || !cpu_arch_flags.bitfield.cpui386)
9343 return 0;
9344
9345 /* Don't add prefix if it is a prefix or there is no operand in case
9346 that segment prefix is special. */
9347 if (!i.operands || i.tm.opcode_modifier.isprefix)
9348 return 0;
9349
9350 if (last_insn.kind == last_insn_other
9351 || last_insn.seg != now_seg)
9352 return 1;
9353
9354 if (flag_debug)
9355 as_warn_where (last_insn.file, last_insn.line,
9356 _("`%s` skips -malign-branch-boundary on `%s`"),
9357 last_insn.name, i.tm.name);
9358
9359 return 0;
9360 }
9361
9362 /* Return 1 if a BRANCH_PADDING frag should be generated. */
9363
9364 static int
9365 add_branch_padding_frag_p (enum align_branch_kind *branch_p,
9366 enum mf_jcc_kind *mf_jcc_p)
9367 {
9368 int add_padding;
9369
9370 /* NB: Don't work with COND_JUMP86 without i386. */
9371 if (!align_branch_power
9372 || now_seg == absolute_section
9373 || !cpu_arch_flags.bitfield.cpui386
9374 || i.tm.opcode_modifier.opcodespace != SPACE_BASE)
9375 return 0;
9376
9377 add_padding = 0;
9378
9379 /* Check for jcc and direct jmp. */
9380 if (i.tm.opcode_modifier.jump == JUMP)
9381 {
9382 if (i.tm.base_opcode == JUMP_PC_RELATIVE)
9383 {
9384 *branch_p = align_branch_jmp;
9385 add_padding = align_branch & align_branch_jmp_bit;
9386 }
9387 else
9388 {
9389 /* Because J<cc> and JN<cc> share same group in macro-fusible table,
9390 igore the lowest bit. */
9391 *mf_jcc_p = (i.tm.base_opcode & 0x0e) >> 1;
9392 *branch_p = align_branch_jcc;
9393 if ((align_branch & align_branch_jcc_bit))
9394 add_padding = 1;
9395 }
9396 }
9397 else if ((i.tm.base_opcode | 1) == 0xc3)
9398 {
9399 /* Near ret. */
9400 *branch_p = align_branch_ret;
9401 if ((align_branch & align_branch_ret_bit))
9402 add_padding = 1;
9403 }
9404 else
9405 {
9406 /* Check for indirect jmp, direct and indirect calls. */
9407 if (i.tm.base_opcode == 0xe8)
9408 {
9409 /* Direct call. */
9410 *branch_p = align_branch_call;
9411 if ((align_branch & align_branch_call_bit))
9412 add_padding = 1;
9413 }
9414 else if (i.tm.base_opcode == 0xff
9415 && (i.tm.extension_opcode == 2
9416 || i.tm.extension_opcode == 4))
9417 {
9418 /* Indirect call and jmp. */
9419 *branch_p = align_branch_indirect;
9420 if ((align_branch & align_branch_indirect_bit))
9421 add_padding = 1;
9422 }
9423
9424 if (add_padding
9425 && i.disp_operands
9426 && tls_get_addr
9427 && (i.op[0].disps->X_op == O_symbol
9428 || (i.op[0].disps->X_op == O_subtract
9429 && i.op[0].disps->X_op_symbol == GOT_symbol)))
9430 {
9431 symbolS *s = i.op[0].disps->X_add_symbol;
9432 /* No padding to call to global or undefined tls_get_addr. */
9433 if ((S_IS_EXTERNAL (s) || !S_IS_DEFINED (s))
9434 && strcmp (S_GET_NAME (s), tls_get_addr) == 0)
9435 return 0;
9436 }
9437 }
9438
9439 if (add_padding
9440 && last_insn.kind != last_insn_other
9441 && last_insn.seg == now_seg)
9442 {
9443 if (flag_debug)
9444 as_warn_where (last_insn.file, last_insn.line,
9445 _("`%s` skips -malign-branch-boundary on `%s`"),
9446 last_insn.name, i.tm.name);
9447 return 0;
9448 }
9449
9450 return add_padding;
9451 }
9452
9453 static void
9454 output_insn (void)
9455 {
9456 fragS *insn_start_frag;
9457 offsetT insn_start_off;
9458 fragS *fragP = NULL;
9459 enum align_branch_kind branch = align_branch_none;
9460 /* The initializer is arbitrary just to avoid uninitialized error.
9461 it's actually either assigned in add_branch_padding_frag_p
9462 or never be used. */
9463 enum mf_jcc_kind mf_jcc = mf_jcc_jo;
9464
9465 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9466 if (IS_ELF && x86_used_note && now_seg != absolute_section)
9467 {
9468 if ((i.xstate & xstate_tmm) == xstate_tmm
9469 || i.tm.cpu_flags.bitfield.cpuamx_tile)
9470 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_TMM;
9471
9472 if (i.tm.cpu_flags.bitfield.cpu8087
9473 || i.tm.cpu_flags.bitfield.cpu287
9474 || i.tm.cpu_flags.bitfield.cpu387
9475 || i.tm.cpu_flags.bitfield.cpu687
9476 || i.tm.cpu_flags.bitfield.cpufisttp)
9477 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X87;
9478
9479 if ((i.xstate & xstate_mmx)
9480 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
9481 && !is_any_vex_encoding (&i.tm)
9482 && (i.tm.base_opcode == 0x77 /* emms */
9483 || i.tm.base_opcode == 0x0e /* femms */)))
9484 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MMX;
9485
9486 if (i.index_reg)
9487 {
9488 if (i.index_reg->reg_type.bitfield.zmmword)
9489 i.xstate |= xstate_zmm;
9490 else if (i.index_reg->reg_type.bitfield.ymmword)
9491 i.xstate |= xstate_ymm;
9492 else if (i.index_reg->reg_type.bitfield.xmmword)
9493 i.xstate |= xstate_xmm;
9494 }
9495
9496 /* vzeroall / vzeroupper */
9497 if (i.tm.base_opcode == 0x77 && i.tm.cpu_flags.bitfield.cpuavx)
9498 i.xstate |= xstate_ymm;
9499
9500 if ((i.xstate & xstate_xmm)
9501 /* ldmxcsr / stmxcsr / vldmxcsr / vstmxcsr */
9502 || (i.tm.base_opcode == 0xae
9503 && (i.tm.cpu_flags.bitfield.cpusse
9504 || i.tm.cpu_flags.bitfield.cpuavx))
9505 || i.tm.cpu_flags.bitfield.cpuwidekl
9506 || i.tm.cpu_flags.bitfield.cpukl)
9507 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XMM;
9508
9509 if ((i.xstate & xstate_ymm) == xstate_ymm)
9510 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_YMM;
9511 if ((i.xstate & xstate_zmm) == xstate_zmm)
9512 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_ZMM;
9513 if (i.mask.reg || (i.xstate & xstate_mask) == xstate_mask)
9514 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MASK;
9515 if (i.tm.cpu_flags.bitfield.cpufxsr)
9516 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_FXSR;
9517 if (i.tm.cpu_flags.bitfield.cpuxsave)
9518 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVE;
9519 if (i.tm.cpu_flags.bitfield.cpuxsaveopt)
9520 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT;
9521 if (i.tm.cpu_flags.bitfield.cpuxsavec)
9522 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEC;
9523
9524 if (x86_feature_2_used
9525 || i.tm.cpu_flags.bitfield.cpucmov
9526 || i.tm.cpu_flags.bitfield.cpusyscall
9527 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
9528 && i.tm.base_opcode == 0xc7
9529 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
9530 && i.tm.extension_opcode == 1) /* cmpxchg8b */)
9531 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_BASELINE;
9532 if (i.tm.cpu_flags.bitfield.cpusse3
9533 || i.tm.cpu_flags.bitfield.cpussse3
9534 || i.tm.cpu_flags.bitfield.cpusse4_1
9535 || i.tm.cpu_flags.bitfield.cpusse4_2
9536 || i.tm.cpu_flags.bitfield.cpucx16
9537 || i.tm.cpu_flags.bitfield.cpupopcnt
9538 /* LAHF-SAHF insns in 64-bit mode. */
9539 || (flag_code == CODE_64BIT
9540 && (i.tm.base_opcode | 1) == 0x9f
9541 && i.tm.opcode_modifier.opcodespace == SPACE_BASE))
9542 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V2;
9543 if (i.tm.cpu_flags.bitfield.cpuavx
9544 || i.tm.cpu_flags.bitfield.cpuavx2
9545 /* Any VEX encoded insns execpt for CpuAVX512F, CpuAVX512BW,
9546 CpuAVX512DQ, LPW, TBM and AMX. */
9547 || (i.tm.opcode_modifier.vex
9548 && !i.tm.cpu_flags.bitfield.cpuavx512f
9549 && !i.tm.cpu_flags.bitfield.cpuavx512bw
9550 && !i.tm.cpu_flags.bitfield.cpuavx512dq
9551 && !i.tm.cpu_flags.bitfield.cpulwp
9552 && !i.tm.cpu_flags.bitfield.cputbm
9553 && !(x86_feature_2_used & GNU_PROPERTY_X86_FEATURE_2_TMM))
9554 || i.tm.cpu_flags.bitfield.cpuf16c
9555 || i.tm.cpu_flags.bitfield.cpufma
9556 || i.tm.cpu_flags.bitfield.cpulzcnt
9557 || i.tm.cpu_flags.bitfield.cpumovbe
9558 || i.tm.cpu_flags.bitfield.cpuxsaves
9559 || (x86_feature_2_used
9560 & (GNU_PROPERTY_X86_FEATURE_2_XSAVE
9561 | GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT
9562 | GNU_PROPERTY_X86_FEATURE_2_XSAVEC)) != 0)
9563 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V3;
9564 if (i.tm.cpu_flags.bitfield.cpuavx512f
9565 || i.tm.cpu_flags.bitfield.cpuavx512bw
9566 || i.tm.cpu_flags.bitfield.cpuavx512dq
9567 || i.tm.cpu_flags.bitfield.cpuavx512vl
9568 /* Any EVEX encoded insns except for AVX512ER, AVX512PF and
9569 VNNIW. */
9570 || (i.tm.opcode_modifier.evex
9571 && !i.tm.cpu_flags.bitfield.cpuavx512er
9572 && !i.tm.cpu_flags.bitfield.cpuavx512pf
9573 && !i.tm.cpu_flags.bitfield.cpuavx512_4vnniw))
9574 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V4;
9575 }
9576 #endif
9577
9578 /* Tie dwarf2 debug info to the address at the start of the insn.
9579 We can't do this after the insn has been output as the current
9580 frag may have been closed off. eg. by frag_var. */
9581 dwarf2_emit_insn (0);
9582
9583 insn_start_frag = frag_now;
9584 insn_start_off = frag_now_fix ();
9585
9586 if (add_branch_padding_frag_p (&branch, &mf_jcc))
9587 {
9588 char *p;
9589 /* Branch can be 8 bytes. Leave some room for prefixes. */
9590 unsigned int max_branch_padding_size = 14;
9591
9592 /* Align section to boundary. */
9593 record_alignment (now_seg, align_branch_power);
9594
9595 /* Make room for padding. */
9596 frag_grow (max_branch_padding_size);
9597
9598 /* Start of the padding. */
9599 p = frag_more (0);
9600
9601 fragP = frag_now;
9602
9603 frag_var (rs_machine_dependent, max_branch_padding_size, 0,
9604 ENCODE_RELAX_STATE (BRANCH_PADDING, 0),
9605 NULL, 0, p);
9606
9607 fragP->tc_frag_data.mf_type = mf_jcc;
9608 fragP->tc_frag_data.branch_type = branch;
9609 fragP->tc_frag_data.max_bytes = max_branch_padding_size;
9610 }
9611
9612 /* Output jumps. */
9613 if (i.tm.opcode_modifier.jump == JUMP)
9614 output_branch ();
9615 else if (i.tm.opcode_modifier.jump == JUMP_BYTE
9616 || i.tm.opcode_modifier.jump == JUMP_DWORD)
9617 output_jump ();
9618 else if (i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT)
9619 output_interseg_jump ();
9620 else
9621 {
9622 /* Output normal instructions here. */
9623 char *p;
9624 unsigned char *q;
9625 unsigned int j;
9626 enum mf_cmp_kind mf_cmp;
9627
9628 if (avoid_fence
9629 && (i.tm.base_opcode == 0xaee8
9630 || i.tm.base_opcode == 0xaef0
9631 || i.tm.base_opcode == 0xaef8))
9632 {
9633 /* Encode lfence, mfence, and sfence as
9634 f0 83 04 24 00 lock addl $0x0, (%{re}sp). */
9635 if (flag_code == CODE_16BIT)
9636 as_bad (_("Cannot convert `%s' in 16-bit mode"), i.tm.name);
9637 else if (omit_lock_prefix)
9638 as_bad (_("Cannot convert `%s' with `-momit-lock-prefix=yes' in effect"),
9639 i.tm.name);
9640 else if (now_seg != absolute_section)
9641 {
9642 offsetT val = 0x240483f0ULL;
9643
9644 p = frag_more (5);
9645 md_number_to_chars (p, val, 5);
9646 }
9647 else
9648 abs_section_offset += 5;
9649 return;
9650 }
9651
9652 /* Some processors fail on LOCK prefix. This options makes
9653 assembler ignore LOCK prefix and serves as a workaround. */
9654 if (omit_lock_prefix)
9655 {
9656 if (i.tm.base_opcode == LOCK_PREFIX_OPCODE
9657 && i.tm.opcode_modifier.isprefix)
9658 return;
9659 i.prefix[LOCK_PREFIX] = 0;
9660 }
9661
9662 if (branch)
9663 /* Skip if this is a branch. */
9664 ;
9665 else if (add_fused_jcc_padding_frag_p (&mf_cmp))
9666 {
9667 /* Make room for padding. */
9668 frag_grow (MAX_FUSED_JCC_PADDING_SIZE);
9669 p = frag_more (0);
9670
9671 fragP = frag_now;
9672
9673 frag_var (rs_machine_dependent, MAX_FUSED_JCC_PADDING_SIZE, 0,
9674 ENCODE_RELAX_STATE (FUSED_JCC_PADDING, 0),
9675 NULL, 0, p);
9676
9677 fragP->tc_frag_data.mf_type = mf_cmp;
9678 fragP->tc_frag_data.branch_type = align_branch_fused;
9679 fragP->tc_frag_data.max_bytes = MAX_FUSED_JCC_PADDING_SIZE;
9680 }
9681 else if (add_branch_prefix_frag_p ())
9682 {
9683 unsigned int max_prefix_size = align_branch_prefix_size;
9684
9685 /* Make room for padding. */
9686 frag_grow (max_prefix_size);
9687 p = frag_more (0);
9688
9689 fragP = frag_now;
9690
9691 frag_var (rs_machine_dependent, max_prefix_size, 0,
9692 ENCODE_RELAX_STATE (BRANCH_PREFIX, 0),
9693 NULL, 0, p);
9694
9695 fragP->tc_frag_data.max_bytes = max_prefix_size;
9696 }
9697
9698 /* Since the VEX/EVEX prefix contains the implicit prefix, we
9699 don't need the explicit prefix. */
9700 if (!i.tm.opcode_modifier.vex && !i.tm.opcode_modifier.evex)
9701 {
9702 switch (i.tm.opcode_modifier.opcodeprefix)
9703 {
9704 case PREFIX_0X66:
9705 add_prefix (0x66);
9706 break;
9707 case PREFIX_0XF2:
9708 add_prefix (0xf2);
9709 break;
9710 case PREFIX_0XF3:
9711 if (!i.tm.cpu_flags.bitfield.cpupadlock
9712 || (i.prefix[REP_PREFIX] != 0xf3))
9713 add_prefix (0xf3);
9714 break;
9715 case PREFIX_NONE:
9716 switch (i.opcode_length)
9717 {
9718 case 2:
9719 break;
9720 case 1:
9721 /* Check for pseudo prefixes. */
9722 if (!i.tm.opcode_modifier.isprefix || i.tm.base_opcode)
9723 break;
9724 as_bad_where (insn_start_frag->fr_file,
9725 insn_start_frag->fr_line,
9726 _("pseudo prefix without instruction"));
9727 return;
9728 default:
9729 abort ();
9730 }
9731 break;
9732 default:
9733 abort ();
9734 }
9735
9736 #if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
9737 /* For x32, add a dummy REX_OPCODE prefix for mov/add with
9738 R_X86_64_GOTTPOFF relocation so that linker can safely
9739 perform IE->LE optimization. A dummy REX_OPCODE prefix
9740 is also needed for lea with R_X86_64_GOTPC32_TLSDESC
9741 relocation for GDesc -> IE/LE optimization. */
9742 if (x86_elf_abi == X86_64_X32_ABI
9743 && i.operands == 2
9744 && (i.reloc[0] == BFD_RELOC_X86_64_GOTTPOFF
9745 || i.reloc[0] == BFD_RELOC_X86_64_GOTPC32_TLSDESC)
9746 && i.prefix[REX_PREFIX] == 0)
9747 add_prefix (REX_OPCODE);
9748 #endif
9749
9750 /* The prefix bytes. */
9751 for (j = ARRAY_SIZE (i.prefix), q = i.prefix; j > 0; j--, q++)
9752 if (*q)
9753 frag_opcode_byte (*q);
9754 }
9755 else
9756 {
9757 for (j = 0, q = i.prefix; j < ARRAY_SIZE (i.prefix); j++, q++)
9758 if (*q)
9759 switch (j)
9760 {
9761 case SEG_PREFIX:
9762 case ADDR_PREFIX:
9763 frag_opcode_byte (*q);
9764 break;
9765 default:
9766 /* There should be no other prefixes for instructions
9767 with VEX prefix. */
9768 abort ();
9769 }
9770
9771 /* For EVEX instructions i.vrex should become 0 after
9772 build_evex_prefix. For VEX instructions upper 16 registers
9773 aren't available, so VREX should be 0. */
9774 if (i.vrex)
9775 abort ();
9776 /* Now the VEX prefix. */
9777 if (now_seg != absolute_section)
9778 {
9779 p = frag_more (i.vex.length);
9780 for (j = 0; j < i.vex.length; j++)
9781 p[j] = i.vex.bytes[j];
9782 }
9783 else
9784 abs_section_offset += i.vex.length;
9785 }
9786
9787 /* Now the opcode; be careful about word order here! */
9788 j = i.opcode_length;
9789 if (!i.vex.length)
9790 switch (i.tm.opcode_modifier.opcodespace)
9791 {
9792 case SPACE_BASE:
9793 break;
9794 case SPACE_0F:
9795 ++j;
9796 break;
9797 case SPACE_0F38:
9798 case SPACE_0F3A:
9799 j += 2;
9800 break;
9801 default:
9802 abort ();
9803 }
9804
9805 if (now_seg == absolute_section)
9806 abs_section_offset += j;
9807 else if (j == 1)
9808 {
9809 FRAG_APPEND_1_CHAR (i.tm.base_opcode);
9810 }
9811 else
9812 {
9813 p = frag_more (j);
9814 if (!i.vex.length
9815 && i.tm.opcode_modifier.opcodespace != SPACE_BASE)
9816 {
9817 *p++ = 0x0f;
9818 if (i.tm.opcode_modifier.opcodespace != SPACE_0F)
9819 *p++ = i.tm.opcode_modifier.opcodespace == SPACE_0F38
9820 ? 0x38 : 0x3a;
9821 }
9822
9823 switch (i.opcode_length)
9824 {
9825 case 2:
9826 /* Put out high byte first: can't use md_number_to_chars! */
9827 *p++ = (i.tm.base_opcode >> 8) & 0xff;
9828 /* Fall through. */
9829 case 1:
9830 *p = i.tm.base_opcode & 0xff;
9831 break;
9832 default:
9833 abort ();
9834 break;
9835 }
9836
9837 }
9838
9839 /* Now the modrm byte and sib byte (if present). */
9840 if (i.tm.opcode_modifier.modrm)
9841 {
9842 frag_opcode_byte ((i.rm.regmem << 0)
9843 | (i.rm.reg << 3)
9844 | (i.rm.mode << 6));
9845 /* If i.rm.regmem == ESP (4)
9846 && i.rm.mode != (Register mode)
9847 && not 16 bit
9848 ==> need second modrm byte. */
9849 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
9850 && i.rm.mode != 3
9851 && !(i.base_reg && i.base_reg->reg_type.bitfield.word))
9852 frag_opcode_byte ((i.sib.base << 0)
9853 | (i.sib.index << 3)
9854 | (i.sib.scale << 6));
9855 }
9856
9857 if (i.disp_operands)
9858 output_disp (insn_start_frag, insn_start_off);
9859
9860 if (i.imm_operands)
9861 output_imm (insn_start_frag, insn_start_off);
9862
9863 /*
9864 * frag_now_fix () returning plain abs_section_offset when we're in the
9865 * absolute section, and abs_section_offset not getting updated as data
9866 * gets added to the frag breaks the logic below.
9867 */
9868 if (now_seg != absolute_section)
9869 {
9870 j = encoding_length (insn_start_frag, insn_start_off, frag_more (0));
9871 if (j > 15)
9872 as_warn (_("instruction length of %u bytes exceeds the limit of 15"),
9873 j);
9874 else if (fragP)
9875 {
9876 /* NB: Don't add prefix with GOTPC relocation since
9877 output_disp() above depends on the fixed encoding
9878 length. Can't add prefix with TLS relocation since
9879 it breaks TLS linker optimization. */
9880 unsigned int max = i.has_gotpc_tls_reloc ? 0 : 15 - j;
9881 /* Prefix count on the current instruction. */
9882 unsigned int count = i.vex.length;
9883 unsigned int k;
9884 for (k = 0; k < ARRAY_SIZE (i.prefix); k++)
9885 /* REX byte is encoded in VEX/EVEX prefix. */
9886 if (i.prefix[k] && (k != REX_PREFIX || !i.vex.length))
9887 count++;
9888
9889 /* Count prefixes for extended opcode maps. */
9890 if (!i.vex.length)
9891 switch (i.tm.opcode_modifier.opcodespace)
9892 {
9893 case SPACE_BASE:
9894 break;
9895 case SPACE_0F:
9896 count++;
9897 break;
9898 case SPACE_0F38:
9899 case SPACE_0F3A:
9900 count += 2;
9901 break;
9902 default:
9903 abort ();
9904 }
9905
9906 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
9907 == BRANCH_PREFIX)
9908 {
9909 /* Set the maximum prefix size in BRANCH_PREFIX
9910 frag. */
9911 if (fragP->tc_frag_data.max_bytes > max)
9912 fragP->tc_frag_data.max_bytes = max;
9913 if (fragP->tc_frag_data.max_bytes > count)
9914 fragP->tc_frag_data.max_bytes -= count;
9915 else
9916 fragP->tc_frag_data.max_bytes = 0;
9917 }
9918 else
9919 {
9920 /* Remember the maximum prefix size in FUSED_JCC_PADDING
9921 frag. */
9922 unsigned int max_prefix_size;
9923 if (align_branch_prefix_size > max)
9924 max_prefix_size = max;
9925 else
9926 max_prefix_size = align_branch_prefix_size;
9927 if (max_prefix_size > count)
9928 fragP->tc_frag_data.max_prefix_length
9929 = max_prefix_size - count;
9930 }
9931
9932 /* Use existing segment prefix if possible. Use CS
9933 segment prefix in 64-bit mode. In 32-bit mode, use SS
9934 segment prefix with ESP/EBP base register and use DS
9935 segment prefix without ESP/EBP base register. */
9936 if (i.prefix[SEG_PREFIX])
9937 fragP->tc_frag_data.default_prefix = i.prefix[SEG_PREFIX];
9938 else if (flag_code == CODE_64BIT)
9939 fragP->tc_frag_data.default_prefix = CS_PREFIX_OPCODE;
9940 else if (i.base_reg
9941 && (i.base_reg->reg_num == 4
9942 || i.base_reg->reg_num == 5))
9943 fragP->tc_frag_data.default_prefix = SS_PREFIX_OPCODE;
9944 else
9945 fragP->tc_frag_data.default_prefix = DS_PREFIX_OPCODE;
9946 }
9947 }
9948 }
9949
9950 /* NB: Don't work with COND_JUMP86 without i386. */
9951 if (align_branch_power
9952 && now_seg != absolute_section
9953 && cpu_arch_flags.bitfield.cpui386)
9954 {
9955 /* Terminate each frag so that we can add prefix and check for
9956 fused jcc. */
9957 frag_wane (frag_now);
9958 frag_new (0);
9959 }
9960
9961 #ifdef DEBUG386
9962 if (flag_debug)
9963 {
9964 pi ("" /*line*/, &i);
9965 }
9966 #endif /* DEBUG386 */
9967 }
9968
9969 /* Return the size of the displacement operand N. */
9970
9971 static int
9972 disp_size (unsigned int n)
9973 {
9974 int size = 4;
9975
9976 if (i.types[n].bitfield.disp64)
9977 size = 8;
9978 else if (i.types[n].bitfield.disp8)
9979 size = 1;
9980 else if (i.types[n].bitfield.disp16)
9981 size = 2;
9982 return size;
9983 }
9984
9985 /* Return the size of the immediate operand N. */
9986
9987 static int
9988 imm_size (unsigned int n)
9989 {
9990 int size = 4;
9991 if (i.types[n].bitfield.imm64)
9992 size = 8;
9993 else if (i.types[n].bitfield.imm8 || i.types[n].bitfield.imm8s)
9994 size = 1;
9995 else if (i.types[n].bitfield.imm16)
9996 size = 2;
9997 return size;
9998 }
9999
10000 static void
10001 output_disp (fragS *insn_start_frag, offsetT insn_start_off)
10002 {
10003 char *p;
10004 unsigned int n;
10005
10006 for (n = 0; n < i.operands; n++)
10007 {
10008 if (operand_type_check (i.types[n], disp))
10009 {
10010 int size = disp_size (n);
10011
10012 if (now_seg == absolute_section)
10013 abs_section_offset += size;
10014 else if (i.op[n].disps->X_op == O_constant)
10015 {
10016 offsetT val = i.op[n].disps->X_add_number;
10017
10018 val = offset_in_range (val >> (size == 1 ? i.memshift : 0),
10019 size);
10020 p = frag_more (size);
10021 md_number_to_chars (p, val, size);
10022 }
10023 else
10024 {
10025 enum bfd_reloc_code_real reloc_type;
10026 int sign = i.types[n].bitfield.disp32s;
10027 int pcrel = (i.flags[n] & Operand_PCrel) != 0;
10028 fixS *fixP;
10029
10030 /* We can't have 8 bit displacement here. */
10031 gas_assert (!i.types[n].bitfield.disp8);
10032
10033 /* The PC relative address is computed relative
10034 to the instruction boundary, so in case immediate
10035 fields follows, we need to adjust the value. */
10036 if (pcrel && i.imm_operands)
10037 {
10038 unsigned int n1;
10039 int sz = 0;
10040
10041 for (n1 = 0; n1 < i.operands; n1++)
10042 if (operand_type_check (i.types[n1], imm))
10043 {
10044 /* Only one immediate is allowed for PC
10045 relative address. */
10046 gas_assert (sz == 0);
10047 sz = imm_size (n1);
10048 i.op[n].disps->X_add_number -= sz;
10049 }
10050 /* We should find the immediate. */
10051 gas_assert (sz != 0);
10052 }
10053
10054 p = frag_more (size);
10055 reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
10056 if (GOT_symbol
10057 && GOT_symbol == i.op[n].disps->X_add_symbol
10058 && (((reloc_type == BFD_RELOC_32
10059 || reloc_type == BFD_RELOC_X86_64_32S
10060 || (reloc_type == BFD_RELOC_64
10061 && object_64bit))
10062 && (i.op[n].disps->X_op == O_symbol
10063 || (i.op[n].disps->X_op == O_add
10064 && ((symbol_get_value_expression
10065 (i.op[n].disps->X_op_symbol)->X_op)
10066 == O_subtract))))
10067 || reloc_type == BFD_RELOC_32_PCREL))
10068 {
10069 if (!object_64bit)
10070 {
10071 reloc_type = BFD_RELOC_386_GOTPC;
10072 i.has_gotpc_tls_reloc = true;
10073 i.op[n].disps->X_add_number +=
10074 encoding_length (insn_start_frag, insn_start_off, p);
10075 }
10076 else if (reloc_type == BFD_RELOC_64)
10077 reloc_type = BFD_RELOC_X86_64_GOTPC64;
10078 else
10079 /* Don't do the adjustment for x86-64, as there
10080 the pcrel addressing is relative to the _next_
10081 insn, and that is taken care of in other code. */
10082 reloc_type = BFD_RELOC_X86_64_GOTPC32;
10083 }
10084 else if (align_branch_power)
10085 {
10086 switch (reloc_type)
10087 {
10088 case BFD_RELOC_386_TLS_GD:
10089 case BFD_RELOC_386_TLS_LDM:
10090 case BFD_RELOC_386_TLS_IE:
10091 case BFD_RELOC_386_TLS_IE_32:
10092 case BFD_RELOC_386_TLS_GOTIE:
10093 case BFD_RELOC_386_TLS_GOTDESC:
10094 case BFD_RELOC_386_TLS_DESC_CALL:
10095 case BFD_RELOC_X86_64_TLSGD:
10096 case BFD_RELOC_X86_64_TLSLD:
10097 case BFD_RELOC_X86_64_GOTTPOFF:
10098 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
10099 case BFD_RELOC_X86_64_TLSDESC_CALL:
10100 i.has_gotpc_tls_reloc = true;
10101 default:
10102 break;
10103 }
10104 }
10105 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal,
10106 size, i.op[n].disps, pcrel,
10107 reloc_type);
10108
10109 if (flag_code == CODE_64BIT && size == 4 && pcrel
10110 && !i.prefix[ADDR_PREFIX])
10111 fixP->fx_signed = 1;
10112
10113 /* Check for "call/jmp *mem", "mov mem, %reg",
10114 "test %reg, mem" and "binop mem, %reg" where binop
10115 is one of adc, add, and, cmp, or, sbb, sub, xor
10116 instructions without data prefix. Always generate
10117 R_386_GOT32X for "sym*GOT" operand in 32-bit mode. */
10118 if (i.prefix[DATA_PREFIX] == 0
10119 && (generate_relax_relocations
10120 || (!object_64bit
10121 && i.rm.mode == 0
10122 && i.rm.regmem == 5))
10123 && (i.rm.mode == 2
10124 || (i.rm.mode == 0 && i.rm.regmem == 5))
10125 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
10126 && ((i.operands == 1
10127 && i.tm.base_opcode == 0xff
10128 && (i.rm.reg == 2 || i.rm.reg == 4))
10129 || (i.operands == 2
10130 && (i.tm.base_opcode == 0x8b
10131 || i.tm.base_opcode == 0x85
10132 || (i.tm.base_opcode & ~0x38) == 0x03))))
10133 {
10134 if (object_64bit)
10135 {
10136 fixP->fx_tcbit = i.rex != 0;
10137 if (i.base_reg
10138 && (i.base_reg->reg_num == RegIP))
10139 fixP->fx_tcbit2 = 1;
10140 }
10141 else
10142 fixP->fx_tcbit2 = 1;
10143 }
10144 }
10145 }
10146 }
10147 }
10148
10149 static void
10150 output_imm (fragS *insn_start_frag, offsetT insn_start_off)
10151 {
10152 char *p;
10153 unsigned int n;
10154
10155 for (n = 0; n < i.operands; n++)
10156 {
10157 /* Skip SAE/RC Imm operand in EVEX. They are already handled. */
10158 if (i.rounding.type != rc_none && n == i.rounding.operand)
10159 continue;
10160
10161 if (operand_type_check (i.types[n], imm))
10162 {
10163 int size = imm_size (n);
10164
10165 if (now_seg == absolute_section)
10166 abs_section_offset += size;
10167 else if (i.op[n].imms->X_op == O_constant)
10168 {
10169 offsetT val;
10170
10171 val = offset_in_range (i.op[n].imms->X_add_number,
10172 size);
10173 p = frag_more (size);
10174 md_number_to_chars (p, val, size);
10175 }
10176 else
10177 {
10178 /* Not absolute_section.
10179 Need a 32-bit fixup (don't support 8bit
10180 non-absolute imms). Try to support other
10181 sizes ... */
10182 enum bfd_reloc_code_real reloc_type;
10183 int sign;
10184
10185 if (i.types[n].bitfield.imm32s
10186 && (i.suffix == QWORD_MNEM_SUFFIX
10187 || (!i.suffix && i.tm.opcode_modifier.no_lsuf)))
10188 sign = 1;
10189 else
10190 sign = 0;
10191
10192 p = frag_more (size);
10193 reloc_type = reloc (size, 0, sign, i.reloc[n]);
10194
10195 /* This is tough to explain. We end up with this one if we
10196 * have operands that look like
10197 * "_GLOBAL_OFFSET_TABLE_+[.-.L284]". The goal here is to
10198 * obtain the absolute address of the GOT, and it is strongly
10199 * preferable from a performance point of view to avoid using
10200 * a runtime relocation for this. The actual sequence of
10201 * instructions often look something like:
10202 *
10203 * call .L66
10204 * .L66:
10205 * popl %ebx
10206 * addl $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
10207 *
10208 * The call and pop essentially return the absolute address
10209 * of the label .L66 and store it in %ebx. The linker itself
10210 * will ultimately change the first operand of the addl so
10211 * that %ebx points to the GOT, but to keep things simple, the
10212 * .o file must have this operand set so that it generates not
10213 * the absolute address of .L66, but the absolute address of
10214 * itself. This allows the linker itself simply treat a GOTPC
10215 * relocation as asking for a pcrel offset to the GOT to be
10216 * added in, and the addend of the relocation is stored in the
10217 * operand field for the instruction itself.
10218 *
10219 * Our job here is to fix the operand so that it would add
10220 * the correct offset so that %ebx would point to itself. The
10221 * thing that is tricky is that .-.L66 will point to the
10222 * beginning of the instruction, so we need to further modify
10223 * the operand so that it will point to itself. There are
10224 * other cases where you have something like:
10225 *
10226 * .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
10227 *
10228 * and here no correction would be required. Internally in
10229 * the assembler we treat operands of this form as not being
10230 * pcrel since the '.' is explicitly mentioned, and I wonder
10231 * whether it would simplify matters to do it this way. Who
10232 * knows. In earlier versions of the PIC patches, the
10233 * pcrel_adjust field was used to store the correction, but
10234 * since the expression is not pcrel, I felt it would be
10235 * confusing to do it this way. */
10236
10237 if ((reloc_type == BFD_RELOC_32
10238 || reloc_type == BFD_RELOC_X86_64_32S
10239 || reloc_type == BFD_RELOC_64)
10240 && GOT_symbol
10241 && GOT_symbol == i.op[n].imms->X_add_symbol
10242 && (i.op[n].imms->X_op == O_symbol
10243 || (i.op[n].imms->X_op == O_add
10244 && ((symbol_get_value_expression
10245 (i.op[n].imms->X_op_symbol)->X_op)
10246 == O_subtract))))
10247 {
10248 if (!object_64bit)
10249 reloc_type = BFD_RELOC_386_GOTPC;
10250 else if (size == 4)
10251 reloc_type = BFD_RELOC_X86_64_GOTPC32;
10252 else if (size == 8)
10253 reloc_type = BFD_RELOC_X86_64_GOTPC64;
10254 i.has_gotpc_tls_reloc = true;
10255 i.op[n].imms->X_add_number +=
10256 encoding_length (insn_start_frag, insn_start_off, p);
10257 }
10258 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
10259 i.op[n].imms, 0, reloc_type);
10260 }
10261 }
10262 }
10263 }
10264 \f
10265 /* x86_cons_fix_new is called via the expression parsing code when a
10266 reloc is needed. We use this hook to get the correct .got reloc. */
10267 static int cons_sign = -1;
10268
10269 void
10270 x86_cons_fix_new (fragS *frag, unsigned int off, unsigned int len,
10271 expressionS *exp, bfd_reloc_code_real_type r)
10272 {
10273 r = reloc (len, 0, cons_sign, r);
10274
10275 #ifdef TE_PE
10276 if (exp->X_op == O_secrel)
10277 {
10278 exp->X_op = O_symbol;
10279 r = BFD_RELOC_32_SECREL;
10280 }
10281 #endif
10282
10283 fix_new_exp (frag, off, len, exp, 0, r);
10284 }
10285
10286 /* Export the ABI address size for use by TC_ADDRESS_BYTES for the
10287 purpose of the `.dc.a' internal pseudo-op. */
10288
10289 int
10290 x86_address_bytes (void)
10291 {
10292 if ((stdoutput->arch_info->mach & bfd_mach_x64_32))
10293 return 4;
10294 return stdoutput->arch_info->bits_per_address / 8;
10295 }
10296
10297 #if (!(defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined (OBJ_MACH_O)) \
10298 || defined (LEX_AT)) && !defined (TE_PE)
10299 # define lex_got(reloc, adjust, types) NULL
10300 #else
10301 /* Parse operands of the form
10302 <symbol>@GOTOFF+<nnn>
10303 and similar .plt or .got references.
10304
10305 If we find one, set up the correct relocation in RELOC and copy the
10306 input string, minus the `@GOTOFF' into a malloc'd buffer for
10307 parsing by the calling routine. Return this buffer, and if ADJUST
10308 is non-null set it to the length of the string we removed from the
10309 input line. Otherwise return NULL. */
10310 static char *
10311 lex_got (enum bfd_reloc_code_real *rel,
10312 int *adjust,
10313 i386_operand_type *types)
10314 {
10315 /* Some of the relocations depend on the size of what field is to
10316 be relocated. But in our callers i386_immediate and i386_displacement
10317 we don't yet know the operand size (this will be set by insn
10318 matching). Hence we record the word32 relocation here,
10319 and adjust the reloc according to the real size in reloc(). */
10320 static const struct {
10321 const char *str;
10322 int len;
10323 const enum bfd_reloc_code_real rel[2];
10324 const i386_operand_type types64;
10325 bool need_GOT_symbol;
10326 } gotrel[] = {
10327 #ifndef TE_PE
10328 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
10329 { STRING_COMMA_LEN ("SIZE"), { BFD_RELOC_SIZE32,
10330 BFD_RELOC_SIZE32 },
10331 OPERAND_TYPE_IMM32_64, false },
10332 #endif
10333 { STRING_COMMA_LEN ("PLTOFF"), { _dummy_first_bfd_reloc_code_real,
10334 BFD_RELOC_X86_64_PLTOFF64 },
10335 OPERAND_TYPE_IMM64, true },
10336 { STRING_COMMA_LEN ("PLT"), { BFD_RELOC_386_PLT32,
10337 BFD_RELOC_X86_64_PLT32 },
10338 OPERAND_TYPE_IMM32_32S_DISP32, false },
10339 { STRING_COMMA_LEN ("GOTPLT"), { _dummy_first_bfd_reloc_code_real,
10340 BFD_RELOC_X86_64_GOTPLT64 },
10341 OPERAND_TYPE_IMM64_DISP64, true },
10342 { STRING_COMMA_LEN ("GOTOFF"), { BFD_RELOC_386_GOTOFF,
10343 BFD_RELOC_X86_64_GOTOFF64 },
10344 OPERAND_TYPE_IMM64_DISP64, true },
10345 { STRING_COMMA_LEN ("GOTPCREL"), { _dummy_first_bfd_reloc_code_real,
10346 BFD_RELOC_X86_64_GOTPCREL },
10347 OPERAND_TYPE_IMM32_32S_DISP32, true },
10348 { STRING_COMMA_LEN ("TLSGD"), { BFD_RELOC_386_TLS_GD,
10349 BFD_RELOC_X86_64_TLSGD },
10350 OPERAND_TYPE_IMM32_32S_DISP32, true },
10351 { STRING_COMMA_LEN ("TLSLDM"), { BFD_RELOC_386_TLS_LDM,
10352 _dummy_first_bfd_reloc_code_real },
10353 OPERAND_TYPE_NONE, true },
10354 { STRING_COMMA_LEN ("TLSLD"), { _dummy_first_bfd_reloc_code_real,
10355 BFD_RELOC_X86_64_TLSLD },
10356 OPERAND_TYPE_IMM32_32S_DISP32, true },
10357 { STRING_COMMA_LEN ("GOTTPOFF"), { BFD_RELOC_386_TLS_IE_32,
10358 BFD_RELOC_X86_64_GOTTPOFF },
10359 OPERAND_TYPE_IMM32_32S_DISP32, true },
10360 { STRING_COMMA_LEN ("TPOFF"), { BFD_RELOC_386_TLS_LE_32,
10361 BFD_RELOC_X86_64_TPOFF32 },
10362 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
10363 { STRING_COMMA_LEN ("NTPOFF"), { BFD_RELOC_386_TLS_LE,
10364 _dummy_first_bfd_reloc_code_real },
10365 OPERAND_TYPE_NONE, true },
10366 { STRING_COMMA_LEN ("DTPOFF"), { BFD_RELOC_386_TLS_LDO_32,
10367 BFD_RELOC_X86_64_DTPOFF32 },
10368 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
10369 { STRING_COMMA_LEN ("GOTNTPOFF"),{ BFD_RELOC_386_TLS_GOTIE,
10370 _dummy_first_bfd_reloc_code_real },
10371 OPERAND_TYPE_NONE, true },
10372 { STRING_COMMA_LEN ("INDNTPOFF"),{ BFD_RELOC_386_TLS_IE,
10373 _dummy_first_bfd_reloc_code_real },
10374 OPERAND_TYPE_NONE, true },
10375 { STRING_COMMA_LEN ("GOT"), { BFD_RELOC_386_GOT32,
10376 BFD_RELOC_X86_64_GOT32 },
10377 OPERAND_TYPE_IMM32_32S_64_DISP32, true },
10378 { STRING_COMMA_LEN ("TLSDESC"), { BFD_RELOC_386_TLS_GOTDESC,
10379 BFD_RELOC_X86_64_GOTPC32_TLSDESC },
10380 OPERAND_TYPE_IMM32_32S_DISP32, true },
10381 { STRING_COMMA_LEN ("TLSCALL"), { BFD_RELOC_386_TLS_DESC_CALL,
10382 BFD_RELOC_X86_64_TLSDESC_CALL },
10383 OPERAND_TYPE_IMM32_32S_DISP32, true },
10384 #else /* TE_PE */
10385 { STRING_COMMA_LEN ("SECREL32"), { BFD_RELOC_32_SECREL,
10386 BFD_RELOC_32_SECREL },
10387 OPERAND_TYPE_IMM32_32S_64_DISP32_64, false },
10388 #endif
10389 };
10390 char *cp;
10391 unsigned int j;
10392
10393 #if defined (OBJ_MAYBE_ELF) && !defined (TE_PE)
10394 if (!IS_ELF)
10395 return NULL;
10396 #endif
10397
10398 for (cp = input_line_pointer; *cp != '@'; cp++)
10399 if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
10400 return NULL;
10401
10402 for (j = 0; j < ARRAY_SIZE (gotrel); j++)
10403 {
10404 int len = gotrel[j].len;
10405 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
10406 {
10407 if (gotrel[j].rel[object_64bit] != 0)
10408 {
10409 int first, second;
10410 char *tmpbuf, *past_reloc;
10411
10412 *rel = gotrel[j].rel[object_64bit];
10413
10414 if (types)
10415 {
10416 if (flag_code != CODE_64BIT)
10417 {
10418 types->bitfield.imm32 = 1;
10419 types->bitfield.disp32 = 1;
10420 }
10421 else
10422 *types = gotrel[j].types64;
10423 }
10424
10425 if (gotrel[j].need_GOT_symbol && GOT_symbol == NULL)
10426 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
10427
10428 /* The length of the first part of our input line. */
10429 first = cp - input_line_pointer;
10430
10431 /* The second part goes from after the reloc token until
10432 (and including) an end_of_line char or comma. */
10433 past_reloc = cp + 1 + len;
10434 cp = past_reloc;
10435 while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
10436 ++cp;
10437 second = cp + 1 - past_reloc;
10438
10439 /* Allocate and copy string. The trailing NUL shouldn't
10440 be necessary, but be safe. */
10441 tmpbuf = XNEWVEC (char, first + second + 2);
10442 memcpy (tmpbuf, input_line_pointer, first);
10443 if (second != 0 && *past_reloc != ' ')
10444 /* Replace the relocation token with ' ', so that
10445 errors like foo@GOTOFF1 will be detected. */
10446 tmpbuf[first++] = ' ';
10447 else
10448 /* Increment length by 1 if the relocation token is
10449 removed. */
10450 len++;
10451 if (adjust)
10452 *adjust = len;
10453 memcpy (tmpbuf + first, past_reloc, second);
10454 tmpbuf[first + second] = '\0';
10455 return tmpbuf;
10456 }
10457
10458 as_bad (_("@%s reloc is not supported with %d-bit output format"),
10459 gotrel[j].str, 1 << (5 + object_64bit));
10460 return NULL;
10461 }
10462 }
10463
10464 /* Might be a symbol version string. Don't as_bad here. */
10465 return NULL;
10466 }
10467 #endif
10468
10469 bfd_reloc_code_real_type
10470 x86_cons (expressionS *exp, int size)
10471 {
10472 bfd_reloc_code_real_type got_reloc = NO_RELOC;
10473
10474 #if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
10475 && !defined (LEX_AT)) \
10476 || defined (TE_PE)
10477 intel_syntax = -intel_syntax;
10478
10479 exp->X_md = 0;
10480 if (size == 4 || (object_64bit && size == 8))
10481 {
10482 /* Handle @GOTOFF and the like in an expression. */
10483 char *save;
10484 char *gotfree_input_line;
10485 int adjust = 0;
10486
10487 save = input_line_pointer;
10488 gotfree_input_line = lex_got (&got_reloc, &adjust, NULL);
10489 if (gotfree_input_line)
10490 input_line_pointer = gotfree_input_line;
10491
10492 expression (exp);
10493
10494 if (gotfree_input_line)
10495 {
10496 /* expression () has merrily parsed up to the end of line,
10497 or a comma - in the wrong buffer. Transfer how far
10498 input_line_pointer has moved to the right buffer. */
10499 input_line_pointer = (save
10500 + (input_line_pointer - gotfree_input_line)
10501 + adjust);
10502 free (gotfree_input_line);
10503 if (exp->X_op == O_constant
10504 || exp->X_op == O_absent
10505 || exp->X_op == O_illegal
10506 || exp->X_op == O_register
10507 || exp->X_op == O_big)
10508 {
10509 char c = *input_line_pointer;
10510 *input_line_pointer = 0;
10511 as_bad (_("missing or invalid expression `%s'"), save);
10512 *input_line_pointer = c;
10513 }
10514 else if ((got_reloc == BFD_RELOC_386_PLT32
10515 || got_reloc == BFD_RELOC_X86_64_PLT32)
10516 && exp->X_op != O_symbol)
10517 {
10518 char c = *input_line_pointer;
10519 *input_line_pointer = 0;
10520 as_bad (_("invalid PLT expression `%s'"), save);
10521 *input_line_pointer = c;
10522 }
10523 }
10524 }
10525 else
10526 expression (exp);
10527
10528 intel_syntax = -intel_syntax;
10529
10530 if (intel_syntax)
10531 i386_intel_simplify (exp);
10532 #else
10533 expression (exp);
10534 #endif
10535
10536 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
10537 if (size == 4 && exp->X_op == O_constant && !object_64bit)
10538 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
10539
10540 return got_reloc;
10541 }
10542
10543 static void
10544 signed_cons (int size)
10545 {
10546 if (object_64bit)
10547 cons_sign = 1;
10548 cons (size);
10549 cons_sign = -1;
10550 }
10551
10552 #ifdef TE_PE
10553 static void
10554 pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
10555 {
10556 expressionS exp;
10557
10558 do
10559 {
10560 expression (&exp);
10561 if (exp.X_op == O_symbol)
10562 exp.X_op = O_secrel;
10563
10564 emit_expr (&exp, 4);
10565 }
10566 while (*input_line_pointer++ == ',');
10567
10568 input_line_pointer--;
10569 demand_empty_rest_of_line ();
10570 }
10571 #endif
10572
10573 /* Handle Vector operations. */
10574
10575 static char *
10576 check_VecOperations (char *op_string)
10577 {
10578 const reg_entry *mask;
10579 const char *saved;
10580 char *end_op;
10581
10582 while (*op_string)
10583 {
10584 saved = op_string;
10585 if (*op_string == '{')
10586 {
10587 op_string++;
10588
10589 /* Check broadcasts. */
10590 if (startswith (op_string, "1to"))
10591 {
10592 unsigned int bcst_type;
10593
10594 if (i.broadcast.type)
10595 goto duplicated_vec_op;
10596
10597 op_string += 3;
10598 if (*op_string == '8')
10599 bcst_type = 8;
10600 else if (*op_string == '4')
10601 bcst_type = 4;
10602 else if (*op_string == '2')
10603 bcst_type = 2;
10604 else if (*op_string == '1'
10605 && *(op_string+1) == '6')
10606 {
10607 bcst_type = 16;
10608 op_string++;
10609 }
10610 else if (*op_string == '3'
10611 && *(op_string+1) == '2')
10612 {
10613 bcst_type = 32;
10614 op_string++;
10615 }
10616 else
10617 {
10618 as_bad (_("Unsupported broadcast: `%s'"), saved);
10619 return NULL;
10620 }
10621 op_string++;
10622
10623 i.broadcast.type = bcst_type;
10624 i.broadcast.operand = this_operand;
10625 }
10626 /* Check masking operation. */
10627 else if ((mask = parse_register (op_string, &end_op)) != NULL)
10628 {
10629 if (mask == &bad_reg)
10630 return NULL;
10631
10632 /* k0 can't be used for write mask. */
10633 if (mask->reg_type.bitfield.class != RegMask || !mask->reg_num)
10634 {
10635 as_bad (_("`%s%s' can't be used for write mask"),
10636 register_prefix, mask->reg_name);
10637 return NULL;
10638 }
10639
10640 if (!i.mask.reg)
10641 {
10642 i.mask.reg = mask;
10643 i.mask.operand = this_operand;
10644 }
10645 else if (i.mask.reg->reg_num)
10646 goto duplicated_vec_op;
10647 else
10648 {
10649 i.mask.reg = mask;
10650
10651 /* Only "{z}" is allowed here. No need to check
10652 zeroing mask explicitly. */
10653 if (i.mask.operand != (unsigned int) this_operand)
10654 {
10655 as_bad (_("invalid write mask `%s'"), saved);
10656 return NULL;
10657 }
10658 }
10659
10660 op_string = end_op;
10661 }
10662 /* Check zeroing-flag for masking operation. */
10663 else if (*op_string == 'z')
10664 {
10665 if (!i.mask.reg)
10666 {
10667 i.mask.reg = reg_k0;
10668 i.mask.zeroing = 1;
10669 i.mask.operand = this_operand;
10670 }
10671 else
10672 {
10673 if (i.mask.zeroing)
10674 {
10675 duplicated_vec_op:
10676 as_bad (_("duplicated `%s'"), saved);
10677 return NULL;
10678 }
10679
10680 i.mask.zeroing = 1;
10681
10682 /* Only "{%k}" is allowed here. No need to check mask
10683 register explicitly. */
10684 if (i.mask.operand != (unsigned int) this_operand)
10685 {
10686 as_bad (_("invalid zeroing-masking `%s'"),
10687 saved);
10688 return NULL;
10689 }
10690 }
10691
10692 op_string++;
10693 }
10694 else
10695 goto unknown_vec_op;
10696
10697 if (*op_string != '}')
10698 {
10699 as_bad (_("missing `}' in `%s'"), saved);
10700 return NULL;
10701 }
10702 op_string++;
10703
10704 /* Strip whitespace since the addition of pseudo prefixes
10705 changed how the scrubber treats '{'. */
10706 if (is_space_char (*op_string))
10707 ++op_string;
10708
10709 continue;
10710 }
10711 unknown_vec_op:
10712 /* We don't know this one. */
10713 as_bad (_("unknown vector operation: `%s'"), saved);
10714 return NULL;
10715 }
10716
10717 if (i.mask.reg && i.mask.zeroing && !i.mask.reg->reg_num)
10718 {
10719 as_bad (_("zeroing-masking only allowed with write mask"));
10720 return NULL;
10721 }
10722
10723 return op_string;
10724 }
10725
10726 static int
10727 i386_immediate (char *imm_start)
10728 {
10729 char *save_input_line_pointer;
10730 char *gotfree_input_line;
10731 segT exp_seg = 0;
10732 expressionS *exp;
10733 i386_operand_type types;
10734
10735 operand_type_set (&types, ~0);
10736
10737 if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
10738 {
10739 as_bad (_("at most %d immediate operands are allowed"),
10740 MAX_IMMEDIATE_OPERANDS);
10741 return 0;
10742 }
10743
10744 exp = &im_expressions[i.imm_operands++];
10745 i.op[this_operand].imms = exp;
10746
10747 if (is_space_char (*imm_start))
10748 ++imm_start;
10749
10750 save_input_line_pointer = input_line_pointer;
10751 input_line_pointer = imm_start;
10752
10753 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
10754 if (gotfree_input_line)
10755 input_line_pointer = gotfree_input_line;
10756
10757 exp_seg = expression (exp);
10758
10759 SKIP_WHITESPACE ();
10760 if (*input_line_pointer)
10761 as_bad (_("junk `%s' after expression"), input_line_pointer);
10762
10763 input_line_pointer = save_input_line_pointer;
10764 if (gotfree_input_line)
10765 {
10766 free (gotfree_input_line);
10767
10768 if (exp->X_op == O_constant)
10769 exp->X_op = O_illegal;
10770 }
10771
10772 if (exp_seg == reg_section)
10773 {
10774 as_bad (_("illegal immediate register operand %s"), imm_start);
10775 return 0;
10776 }
10777
10778 return i386_finalize_immediate (exp_seg, exp, types, imm_start);
10779 }
10780
10781 static int
10782 i386_finalize_immediate (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
10783 i386_operand_type types, const char *imm_start)
10784 {
10785 if (exp->X_op == O_absent || exp->X_op == O_illegal || exp->X_op == O_big)
10786 {
10787 if (imm_start)
10788 as_bad (_("missing or invalid immediate expression `%s'"),
10789 imm_start);
10790 return 0;
10791 }
10792 else if (exp->X_op == O_constant)
10793 {
10794 /* Size it properly later. */
10795 i.types[this_operand].bitfield.imm64 = 1;
10796
10797 /* If not 64bit, sign/zero extend val, to account for wraparound
10798 when !BFD64. */
10799 if (flag_code != CODE_64BIT)
10800 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
10801 }
10802 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
10803 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
10804 && exp_seg != absolute_section
10805 && exp_seg != text_section
10806 && exp_seg != data_section
10807 && exp_seg != bss_section
10808 && exp_seg != undefined_section
10809 && !bfd_is_com_section (exp_seg))
10810 {
10811 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
10812 return 0;
10813 }
10814 #endif
10815 else
10816 {
10817 /* This is an address. The size of the address will be
10818 determined later, depending on destination register,
10819 suffix, or the default for the section. */
10820 i.types[this_operand].bitfield.imm8 = 1;
10821 i.types[this_operand].bitfield.imm16 = 1;
10822 i.types[this_operand].bitfield.imm32 = 1;
10823 i.types[this_operand].bitfield.imm32s = 1;
10824 i.types[this_operand].bitfield.imm64 = 1;
10825 i.types[this_operand] = operand_type_and (i.types[this_operand],
10826 types);
10827 }
10828
10829 return 1;
10830 }
10831
10832 static char *
10833 i386_scale (char *scale)
10834 {
10835 offsetT val;
10836 char *save = input_line_pointer;
10837
10838 input_line_pointer = scale;
10839 val = get_absolute_expression ();
10840
10841 switch (val)
10842 {
10843 case 1:
10844 i.log2_scale_factor = 0;
10845 break;
10846 case 2:
10847 i.log2_scale_factor = 1;
10848 break;
10849 case 4:
10850 i.log2_scale_factor = 2;
10851 break;
10852 case 8:
10853 i.log2_scale_factor = 3;
10854 break;
10855 default:
10856 {
10857 char sep = *input_line_pointer;
10858
10859 *input_line_pointer = '\0';
10860 as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
10861 scale);
10862 *input_line_pointer = sep;
10863 input_line_pointer = save;
10864 return NULL;
10865 }
10866 }
10867 if (i.log2_scale_factor != 0 && i.index_reg == 0)
10868 {
10869 as_warn (_("scale factor of %d without an index register"),
10870 1 << i.log2_scale_factor);
10871 i.log2_scale_factor = 0;
10872 }
10873 scale = input_line_pointer;
10874 input_line_pointer = save;
10875 return scale;
10876 }
10877
10878 static int
10879 i386_displacement (char *disp_start, char *disp_end)
10880 {
10881 expressionS *exp;
10882 segT exp_seg = 0;
10883 char *save_input_line_pointer;
10884 char *gotfree_input_line;
10885 int override;
10886 i386_operand_type bigdisp, types = anydisp;
10887 int ret;
10888
10889 if (i.disp_operands == MAX_MEMORY_OPERANDS)
10890 {
10891 as_bad (_("at most %d displacement operands are allowed"),
10892 MAX_MEMORY_OPERANDS);
10893 return 0;
10894 }
10895
10896 operand_type_set (&bigdisp, 0);
10897 if (i.jumpabsolute
10898 || i.types[this_operand].bitfield.baseindex
10899 || (current_templates->start->opcode_modifier.jump != JUMP
10900 && current_templates->start->opcode_modifier.jump != JUMP_DWORD))
10901 {
10902 i386_addressing_mode ();
10903 override = (i.prefix[ADDR_PREFIX] != 0);
10904 if (flag_code == CODE_64BIT)
10905 {
10906 if (!override)
10907 {
10908 bigdisp.bitfield.disp32s = 1;
10909 bigdisp.bitfield.disp64 = 1;
10910 }
10911 else
10912 bigdisp.bitfield.disp32 = 1;
10913 }
10914 else if ((flag_code == CODE_16BIT) ^ override)
10915 bigdisp.bitfield.disp16 = 1;
10916 else
10917 bigdisp.bitfield.disp32 = 1;
10918 }
10919 else
10920 {
10921 /* For PC-relative branches, the width of the displacement may be
10922 dependent upon data size, but is never dependent upon address size.
10923 Also make sure to not unintentionally match against a non-PC-relative
10924 branch template. */
10925 static templates aux_templates;
10926 const insn_template *t = current_templates->start;
10927 bool has_intel64 = false;
10928
10929 aux_templates.start = t;
10930 while (++t < current_templates->end)
10931 {
10932 if (t->opcode_modifier.jump
10933 != current_templates->start->opcode_modifier.jump)
10934 break;
10935 if ((t->opcode_modifier.isa64 >= INTEL64))
10936 has_intel64 = true;
10937 }
10938 if (t < current_templates->end)
10939 {
10940 aux_templates.end = t;
10941 current_templates = &aux_templates;
10942 }
10943
10944 override = (i.prefix[DATA_PREFIX] != 0);
10945 if (flag_code == CODE_64BIT)
10946 {
10947 if ((override || i.suffix == WORD_MNEM_SUFFIX)
10948 && (!intel64 || !has_intel64))
10949 bigdisp.bitfield.disp16 = 1;
10950 else
10951 bigdisp.bitfield.disp32s = 1;
10952 }
10953 else
10954 {
10955 if (!override)
10956 override = (i.suffix == (flag_code != CODE_16BIT
10957 ? WORD_MNEM_SUFFIX
10958 : LONG_MNEM_SUFFIX));
10959 bigdisp.bitfield.disp32 = 1;
10960 if ((flag_code == CODE_16BIT) ^ override)
10961 {
10962 bigdisp.bitfield.disp32 = 0;
10963 bigdisp.bitfield.disp16 = 1;
10964 }
10965 }
10966 }
10967 i.types[this_operand] = operand_type_or (i.types[this_operand],
10968 bigdisp);
10969
10970 exp = &disp_expressions[i.disp_operands];
10971 i.op[this_operand].disps = exp;
10972 i.disp_operands++;
10973 save_input_line_pointer = input_line_pointer;
10974 input_line_pointer = disp_start;
10975 END_STRING_AND_SAVE (disp_end);
10976
10977 #ifndef GCC_ASM_O_HACK
10978 #define GCC_ASM_O_HACK 0
10979 #endif
10980 #if GCC_ASM_O_HACK
10981 END_STRING_AND_SAVE (disp_end + 1);
10982 if (i.types[this_operand].bitfield.baseIndex
10983 && displacement_string_end[-1] == '+')
10984 {
10985 /* This hack is to avoid a warning when using the "o"
10986 constraint within gcc asm statements.
10987 For instance:
10988
10989 #define _set_tssldt_desc(n,addr,limit,type) \
10990 __asm__ __volatile__ ( \
10991 "movw %w2,%0\n\t" \
10992 "movw %w1,2+%0\n\t" \
10993 "rorl $16,%1\n\t" \
10994 "movb %b1,4+%0\n\t" \
10995 "movb %4,5+%0\n\t" \
10996 "movb $0,6+%0\n\t" \
10997 "movb %h1,7+%0\n\t" \
10998 "rorl $16,%1" \
10999 : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
11000
11001 This works great except that the output assembler ends
11002 up looking a bit weird if it turns out that there is
11003 no offset. You end up producing code that looks like:
11004
11005 #APP
11006 movw $235,(%eax)
11007 movw %dx,2+(%eax)
11008 rorl $16,%edx
11009 movb %dl,4+(%eax)
11010 movb $137,5+(%eax)
11011 movb $0,6+(%eax)
11012 movb %dh,7+(%eax)
11013 rorl $16,%edx
11014 #NO_APP
11015
11016 So here we provide the missing zero. */
11017
11018 *displacement_string_end = '0';
11019 }
11020 #endif
11021 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
11022 if (gotfree_input_line)
11023 input_line_pointer = gotfree_input_line;
11024
11025 exp_seg = expression (exp);
11026
11027 SKIP_WHITESPACE ();
11028 if (*input_line_pointer)
11029 as_bad (_("junk `%s' after expression"), input_line_pointer);
11030 #if GCC_ASM_O_HACK
11031 RESTORE_END_STRING (disp_end + 1);
11032 #endif
11033 input_line_pointer = save_input_line_pointer;
11034 if (gotfree_input_line)
11035 {
11036 free (gotfree_input_line);
11037
11038 if (exp->X_op == O_constant || exp->X_op == O_register)
11039 exp->X_op = O_illegal;
11040 }
11041
11042 ret = i386_finalize_displacement (exp_seg, exp, types, disp_start);
11043
11044 RESTORE_END_STRING (disp_end);
11045
11046 return ret;
11047 }
11048
11049 static int
11050 i386_finalize_displacement (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
11051 i386_operand_type types, const char *disp_start)
11052 {
11053 i386_operand_type bigdisp;
11054 int ret = 1;
11055
11056 /* We do this to make sure that the section symbol is in
11057 the symbol table. We will ultimately change the relocation
11058 to be relative to the beginning of the section. */
11059 if (i.reloc[this_operand] == BFD_RELOC_386_GOTOFF
11060 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
11061 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
11062 {
11063 if (exp->X_op != O_symbol)
11064 goto inv_disp;
11065
11066 if (S_IS_LOCAL (exp->X_add_symbol)
11067 && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section
11068 && S_GET_SEGMENT (exp->X_add_symbol) != expr_section)
11069 section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
11070 exp->X_op = O_subtract;
11071 exp->X_op_symbol = GOT_symbol;
11072 if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
11073 i.reloc[this_operand] = BFD_RELOC_32_PCREL;
11074 else if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
11075 i.reloc[this_operand] = BFD_RELOC_64;
11076 else
11077 i.reloc[this_operand] = BFD_RELOC_32;
11078 }
11079
11080 else if (exp->X_op == O_absent
11081 || exp->X_op == O_illegal
11082 || exp->X_op == O_big)
11083 {
11084 inv_disp:
11085 as_bad (_("missing or invalid displacement expression `%s'"),
11086 disp_start);
11087 ret = 0;
11088 }
11089
11090 else if (exp->X_op == O_constant)
11091 {
11092 /* Sizing gets taken care of by optimize_disp().
11093
11094 If not 64bit, sign/zero extend val, to account for wraparound
11095 when !BFD64. */
11096 if (flag_code != CODE_64BIT)
11097 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
11098 }
11099
11100 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
11101 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
11102 && exp_seg != absolute_section
11103 && exp_seg != text_section
11104 && exp_seg != data_section
11105 && exp_seg != bss_section
11106 && exp_seg != undefined_section
11107 && !bfd_is_com_section (exp_seg))
11108 {
11109 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
11110 ret = 0;
11111 }
11112 #endif
11113
11114 else if (current_templates->start->opcode_modifier.jump == JUMP_BYTE)
11115 i.types[this_operand].bitfield.disp8 = 1;
11116
11117 /* Check if this is a displacement only operand. */
11118 bigdisp = operand_type_and_not (i.types[this_operand], anydisp);
11119 if (operand_type_all_zero (&bigdisp))
11120 i.types[this_operand] = operand_type_and (i.types[this_operand],
11121 types);
11122
11123 return ret;
11124 }
11125
11126 /* Return the active addressing mode, taking address override and
11127 registers forming the address into consideration. Update the
11128 address override prefix if necessary. */
11129
11130 static enum flag_code
11131 i386_addressing_mode (void)
11132 {
11133 enum flag_code addr_mode;
11134
11135 if (i.prefix[ADDR_PREFIX])
11136 addr_mode = flag_code == CODE_32BIT ? CODE_16BIT : CODE_32BIT;
11137 else if (flag_code == CODE_16BIT
11138 && current_templates->start->cpu_flags.bitfield.cpumpx
11139 /* Avoid replacing the "16-bit addressing not allowed" diagnostic
11140 from md_assemble() by "is not a valid base/index expression"
11141 when there is a base and/or index. */
11142 && !i.types[this_operand].bitfield.baseindex)
11143 {
11144 /* MPX insn memory operands with neither base nor index must be forced
11145 to use 32-bit addressing in 16-bit mode. */
11146 addr_mode = CODE_32BIT;
11147 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
11148 ++i.prefixes;
11149 gas_assert (!i.types[this_operand].bitfield.disp16);
11150 gas_assert (!i.types[this_operand].bitfield.disp32);
11151 }
11152 else
11153 {
11154 addr_mode = flag_code;
11155
11156 #if INFER_ADDR_PREFIX
11157 if (i.mem_operands == 0)
11158 {
11159 /* Infer address prefix from the first memory operand. */
11160 const reg_entry *addr_reg = i.base_reg;
11161
11162 if (addr_reg == NULL)
11163 addr_reg = i.index_reg;
11164
11165 if (addr_reg)
11166 {
11167 if (addr_reg->reg_type.bitfield.dword)
11168 addr_mode = CODE_32BIT;
11169 else if (flag_code != CODE_64BIT
11170 && addr_reg->reg_type.bitfield.word)
11171 addr_mode = CODE_16BIT;
11172
11173 if (addr_mode != flag_code)
11174 {
11175 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
11176 i.prefixes += 1;
11177 /* Change the size of any displacement too. At most one
11178 of Disp16 or Disp32 is set.
11179 FIXME. There doesn't seem to be any real need for
11180 separate Disp16 and Disp32 flags. The same goes for
11181 Imm16 and Imm32. Removing them would probably clean
11182 up the code quite a lot. */
11183 if (flag_code != CODE_64BIT
11184 && (i.types[this_operand].bitfield.disp16
11185 || i.types[this_operand].bitfield.disp32))
11186 i.types[this_operand]
11187 = operand_type_xor (i.types[this_operand], disp16_32);
11188 }
11189 }
11190 }
11191 #endif
11192 }
11193
11194 return addr_mode;
11195 }
11196
11197 /* Make sure the memory operand we've been dealt is valid.
11198 Return 1 on success, 0 on a failure. */
11199
11200 static int
11201 i386_index_check (const char *operand_string)
11202 {
11203 const char *kind = "base/index";
11204 enum flag_code addr_mode = i386_addressing_mode ();
11205 const insn_template *t = current_templates->start;
11206
11207 if (t->opcode_modifier.isstring
11208 && !t->cpu_flags.bitfield.cpupadlock
11209 && (current_templates->end[-1].opcode_modifier.isstring
11210 || i.mem_operands))
11211 {
11212 /* Memory operands of string insns are special in that they only allow
11213 a single register (rDI, rSI, or rBX) as their memory address. */
11214 const reg_entry *expected_reg;
11215 static const char *di_si[][2] =
11216 {
11217 { "esi", "edi" },
11218 { "si", "di" },
11219 { "rsi", "rdi" }
11220 };
11221 static const char *bx[] = { "ebx", "bx", "rbx" };
11222
11223 kind = "string address";
11224
11225 if (t->opcode_modifier.prefixok == PrefixRep)
11226 {
11227 int es_op = current_templates->end[-1].opcode_modifier.isstring
11228 - IS_STRING_ES_OP0;
11229 int op = 0;
11230
11231 if (!current_templates->end[-1].operand_types[0].bitfield.baseindex
11232 || ((!i.mem_operands != !intel_syntax)
11233 && current_templates->end[-1].operand_types[1]
11234 .bitfield.baseindex))
11235 op = 1;
11236 expected_reg
11237 = (const reg_entry *) str_hash_find (reg_hash,
11238 di_si[addr_mode][op == es_op]);
11239 }
11240 else
11241 expected_reg
11242 = (const reg_entry *)str_hash_find (reg_hash, bx[addr_mode]);
11243
11244 if (i.base_reg != expected_reg
11245 || i.index_reg
11246 || operand_type_check (i.types[this_operand], disp))
11247 {
11248 /* The second memory operand must have the same size as
11249 the first one. */
11250 if (i.mem_operands
11251 && i.base_reg
11252 && !((addr_mode == CODE_64BIT
11253 && i.base_reg->reg_type.bitfield.qword)
11254 || (addr_mode == CODE_32BIT
11255 ? i.base_reg->reg_type.bitfield.dword
11256 : i.base_reg->reg_type.bitfield.word)))
11257 goto bad_address;
11258
11259 as_warn (_("`%s' is not valid here (expected `%c%s%s%c')"),
11260 operand_string,
11261 intel_syntax ? '[' : '(',
11262 register_prefix,
11263 expected_reg->reg_name,
11264 intel_syntax ? ']' : ')');
11265 return 1;
11266 }
11267 else
11268 return 1;
11269
11270 bad_address:
11271 as_bad (_("`%s' is not a valid %s expression"),
11272 operand_string, kind);
11273 return 0;
11274 }
11275 else
11276 {
11277 if (addr_mode != CODE_16BIT)
11278 {
11279 /* 32-bit/64-bit checks. */
11280 if (i.disp_encoding == disp_encoding_16bit)
11281 {
11282 bad_disp:
11283 as_bad (_("invalid `%s' prefix"),
11284 addr_mode == CODE_16BIT ? "{disp32}" : "{disp16}");
11285 return 0;
11286 }
11287
11288 if ((i.base_reg
11289 && ((addr_mode == CODE_64BIT
11290 ? !i.base_reg->reg_type.bitfield.qword
11291 : !i.base_reg->reg_type.bitfield.dword)
11292 || (i.index_reg && i.base_reg->reg_num == RegIP)
11293 || i.base_reg->reg_num == RegIZ))
11294 || (i.index_reg
11295 && !i.index_reg->reg_type.bitfield.xmmword
11296 && !i.index_reg->reg_type.bitfield.ymmword
11297 && !i.index_reg->reg_type.bitfield.zmmword
11298 && ((addr_mode == CODE_64BIT
11299 ? !i.index_reg->reg_type.bitfield.qword
11300 : !i.index_reg->reg_type.bitfield.dword)
11301 || !i.index_reg->reg_type.bitfield.baseindex)))
11302 goto bad_address;
11303
11304 /* bndmk, bndldx, bndstx and mandatory non-vector SIB have special restrictions. */
11305 if ((t->opcode_modifier.opcodeprefix == PREFIX_0XF3
11306 && t->opcode_modifier.opcodespace == SPACE_0F
11307 && t->base_opcode == 0x1b)
11308 || (t->opcode_modifier.opcodeprefix == PREFIX_NONE
11309 && t->opcode_modifier.opcodespace == SPACE_0F
11310 && (t->base_opcode & ~1) == 0x1a)
11311 || t->opcode_modifier.sib == SIBMEM)
11312 {
11313 /* They cannot use RIP-relative addressing. */
11314 if (i.base_reg && i.base_reg->reg_num == RegIP)
11315 {
11316 as_bad (_("`%s' cannot be used here"), operand_string);
11317 return 0;
11318 }
11319
11320 /* bndldx and bndstx ignore their scale factor. */
11321 if (t->opcode_modifier.opcodeprefix == PREFIX_NONE
11322 && t->opcode_modifier.opcodespace == SPACE_0F
11323 && (t->base_opcode & ~1) == 0x1a
11324 && i.log2_scale_factor)
11325 as_warn (_("register scaling is being ignored here"));
11326 }
11327 }
11328 else
11329 {
11330 /* 16-bit checks. */
11331 if (i.disp_encoding == disp_encoding_32bit)
11332 goto bad_disp;
11333
11334 if ((i.base_reg
11335 && (!i.base_reg->reg_type.bitfield.word
11336 || !i.base_reg->reg_type.bitfield.baseindex))
11337 || (i.index_reg
11338 && (!i.index_reg->reg_type.bitfield.word
11339 || !i.index_reg->reg_type.bitfield.baseindex
11340 || !(i.base_reg
11341 && i.base_reg->reg_num < 6
11342 && i.index_reg->reg_num >= 6
11343 && i.log2_scale_factor == 0))))
11344 goto bad_address;
11345 }
11346 }
11347 return 1;
11348 }
11349
11350 /* Handle vector immediates. */
11351
11352 static int
11353 RC_SAE_immediate (const char *imm_start)
11354 {
11355 unsigned int match_found, j;
11356 const char *pstr = imm_start;
11357 expressionS *exp;
11358
11359 if (*pstr != '{')
11360 return 0;
11361
11362 pstr++;
11363 match_found = 0;
11364 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); j++)
11365 {
11366 if (!strncmp (pstr, RC_NamesTable[j].name, RC_NamesTable[j].len))
11367 {
11368 if (i.rounding.type != rc_none)
11369 {
11370 as_bad (_("duplicated `%s'"), imm_start);
11371 return 0;
11372 }
11373
11374 i.rounding.type = RC_NamesTable[j].type;
11375 i.rounding.operand = this_operand;
11376
11377 pstr += RC_NamesTable[j].len;
11378 match_found = 1;
11379 break;
11380 }
11381 }
11382 if (!match_found)
11383 return 0;
11384
11385 if (*pstr++ != '}')
11386 {
11387 as_bad (_("Missing '}': '%s'"), imm_start);
11388 return 0;
11389 }
11390 /* RC/SAE immediate string should contain nothing more. */;
11391 if (*pstr != 0)
11392 {
11393 as_bad (_("Junk after '}': '%s'"), imm_start);
11394 return 0;
11395 }
11396
11397 exp = &im_expressions[i.imm_operands++];
11398 i.op[this_operand].imms = exp;
11399
11400 exp->X_op = O_constant;
11401 exp->X_add_number = 0;
11402 exp->X_add_symbol = (symbolS *) 0;
11403 exp->X_op_symbol = (symbolS *) 0;
11404
11405 i.types[this_operand].bitfield.imm8 = 1;
11406 return 1;
11407 }
11408
11409 /* Only string instructions can have a second memory operand, so
11410 reduce current_templates to just those if it contains any. */
11411 static int
11412 maybe_adjust_templates (void)
11413 {
11414 const insn_template *t;
11415
11416 gas_assert (i.mem_operands == 1);
11417
11418 for (t = current_templates->start; t < current_templates->end; ++t)
11419 if (t->opcode_modifier.isstring)
11420 break;
11421
11422 if (t < current_templates->end)
11423 {
11424 static templates aux_templates;
11425 bool recheck;
11426
11427 aux_templates.start = t;
11428 for (; t < current_templates->end; ++t)
11429 if (!t->opcode_modifier.isstring)
11430 break;
11431 aux_templates.end = t;
11432
11433 /* Determine whether to re-check the first memory operand. */
11434 recheck = (aux_templates.start != current_templates->start
11435 || t != current_templates->end);
11436
11437 current_templates = &aux_templates;
11438
11439 if (recheck)
11440 {
11441 i.mem_operands = 0;
11442 if (i.memop1_string != NULL
11443 && i386_index_check (i.memop1_string) == 0)
11444 return 0;
11445 i.mem_operands = 1;
11446 }
11447 }
11448
11449 return 1;
11450 }
11451
11452 static INLINE bool starts_memory_operand (char c)
11453 {
11454 return ISDIGIT (c)
11455 || is_identifier_char (c)
11456 || strchr ("([\"+-!~", c);
11457 }
11458
11459 /* Parse OPERAND_STRING into the i386_insn structure I. Returns zero
11460 on error. */
11461
11462 static int
11463 i386_att_operand (char *operand_string)
11464 {
11465 const reg_entry *r;
11466 char *end_op;
11467 char *op_string = operand_string;
11468
11469 if (is_space_char (*op_string))
11470 ++op_string;
11471
11472 /* We check for an absolute prefix (differentiating,
11473 for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
11474 if (*op_string == ABSOLUTE_PREFIX)
11475 {
11476 ++op_string;
11477 if (is_space_char (*op_string))
11478 ++op_string;
11479 i.jumpabsolute = true;
11480 }
11481
11482 /* Check if operand is a register. */
11483 if ((r = parse_register (op_string, &end_op)) != NULL)
11484 {
11485 i386_operand_type temp;
11486
11487 if (r == &bad_reg)
11488 return 0;
11489
11490 /* Check for a segment override by searching for ':' after a
11491 segment register. */
11492 op_string = end_op;
11493 if (is_space_char (*op_string))
11494 ++op_string;
11495 if (*op_string == ':' && r->reg_type.bitfield.class == SReg)
11496 {
11497 i.seg[i.mem_operands] = r;
11498
11499 /* Skip the ':' and whitespace. */
11500 ++op_string;
11501 if (is_space_char (*op_string))
11502 ++op_string;
11503
11504 /* Handle case of %es:*foo. */
11505 if (!i.jumpabsolute && *op_string == ABSOLUTE_PREFIX)
11506 {
11507 ++op_string;
11508 if (is_space_char (*op_string))
11509 ++op_string;
11510 i.jumpabsolute = true;
11511 }
11512
11513 if (!starts_memory_operand (*op_string))
11514 {
11515 as_bad (_("bad memory operand `%s'"), op_string);
11516 return 0;
11517 }
11518 goto do_memory_reference;
11519 }
11520
11521 /* Handle vector operations. */
11522 if (*op_string == '{')
11523 {
11524 op_string = check_VecOperations (op_string);
11525 if (op_string == NULL)
11526 return 0;
11527 }
11528
11529 if (*op_string)
11530 {
11531 as_bad (_("junk `%s' after register"), op_string);
11532 return 0;
11533 }
11534 temp = r->reg_type;
11535 temp.bitfield.baseindex = 0;
11536 i.types[this_operand] = operand_type_or (i.types[this_operand],
11537 temp);
11538 i.types[this_operand].bitfield.unspecified = 0;
11539 i.op[this_operand].regs = r;
11540 i.reg_operands++;
11541 }
11542 else if (*op_string == REGISTER_PREFIX)
11543 {
11544 as_bad (_("bad register name `%s'"), op_string);
11545 return 0;
11546 }
11547 else if (*op_string == IMMEDIATE_PREFIX)
11548 {
11549 ++op_string;
11550 if (i.jumpabsolute)
11551 {
11552 as_bad (_("immediate operand illegal with absolute jump"));
11553 return 0;
11554 }
11555 if (!i386_immediate (op_string))
11556 return 0;
11557 }
11558 else if (RC_SAE_immediate (operand_string))
11559 {
11560 /* If it is a RC or SAE immediate, do nothing. */
11561 ;
11562 }
11563 else if (starts_memory_operand (*op_string))
11564 {
11565 /* This is a memory reference of some sort. */
11566 char *base_string;
11567
11568 /* Start and end of displacement string expression (if found). */
11569 char *displacement_string_start;
11570 char *displacement_string_end;
11571
11572 do_memory_reference:
11573 if (i.mem_operands == 1 && !maybe_adjust_templates ())
11574 return 0;
11575 if ((i.mem_operands == 1
11576 && !current_templates->start->opcode_modifier.isstring)
11577 || i.mem_operands == 2)
11578 {
11579 as_bad (_("too many memory references for `%s'"),
11580 current_templates->start->name);
11581 return 0;
11582 }
11583
11584 /* Check for base index form. We detect the base index form by
11585 looking for an ')' at the end of the operand, searching
11586 for the '(' matching it, and finding a REGISTER_PREFIX or ','
11587 after the '('. */
11588 base_string = op_string + strlen (op_string);
11589
11590 /* Handle vector operations. */
11591 --base_string;
11592 if (is_space_char (*base_string))
11593 --base_string;
11594
11595 if (*base_string == '}')
11596 {
11597 char *vop_start = NULL;
11598
11599 while (base_string-- > op_string)
11600 {
11601 if (*base_string == '"')
11602 break;
11603 if (*base_string != '{')
11604 continue;
11605
11606 vop_start = base_string;
11607
11608 --base_string;
11609 if (is_space_char (*base_string))
11610 --base_string;
11611
11612 if (*base_string != '}')
11613 break;
11614
11615 vop_start = NULL;
11616 }
11617
11618 if (!vop_start)
11619 {
11620 as_bad (_("unbalanced figure braces"));
11621 return 0;
11622 }
11623
11624 if (check_VecOperations (vop_start) == NULL)
11625 return 0;
11626 }
11627
11628 /* If we only have a displacement, set-up for it to be parsed later. */
11629 displacement_string_start = op_string;
11630 displacement_string_end = base_string + 1;
11631
11632 if (*base_string == ')')
11633 {
11634 char *temp_string;
11635 unsigned int parens_not_balanced = 1;
11636
11637 /* We've already checked that the number of left & right ()'s are
11638 equal, so this loop will not be infinite. */
11639 do
11640 {
11641 base_string--;
11642 if (*base_string == ')')
11643 parens_not_balanced++;
11644 if (*base_string == '(')
11645 parens_not_balanced--;
11646 }
11647 while (parens_not_balanced && *base_string != '"');
11648
11649 temp_string = base_string;
11650
11651 /* Skip past '(' and whitespace. */
11652 if (*base_string == '(')
11653 ++base_string;
11654 if (is_space_char (*base_string))
11655 ++base_string;
11656
11657 if (*base_string == ','
11658 || ((i.base_reg = parse_register (base_string, &end_op))
11659 != NULL))
11660 {
11661 displacement_string_end = temp_string;
11662
11663 i.types[this_operand].bitfield.baseindex = 1;
11664
11665 if (i.base_reg)
11666 {
11667 if (i.base_reg == &bad_reg)
11668 return 0;
11669 base_string = end_op;
11670 if (is_space_char (*base_string))
11671 ++base_string;
11672 }
11673
11674 /* There may be an index reg or scale factor here. */
11675 if (*base_string == ',')
11676 {
11677 ++base_string;
11678 if (is_space_char (*base_string))
11679 ++base_string;
11680
11681 if ((i.index_reg = parse_register (base_string, &end_op))
11682 != NULL)
11683 {
11684 if (i.index_reg == &bad_reg)
11685 return 0;
11686 base_string = end_op;
11687 if (is_space_char (*base_string))
11688 ++base_string;
11689 if (*base_string == ',')
11690 {
11691 ++base_string;
11692 if (is_space_char (*base_string))
11693 ++base_string;
11694 }
11695 else if (*base_string != ')')
11696 {
11697 as_bad (_("expecting `,' or `)' "
11698 "after index register in `%s'"),
11699 operand_string);
11700 return 0;
11701 }
11702 }
11703 else if (*base_string == REGISTER_PREFIX)
11704 {
11705 end_op = strchr (base_string, ',');
11706 if (end_op)
11707 *end_op = '\0';
11708 as_bad (_("bad register name `%s'"), base_string);
11709 return 0;
11710 }
11711
11712 /* Check for scale factor. */
11713 if (*base_string != ')')
11714 {
11715 char *end_scale = i386_scale (base_string);
11716
11717 if (!end_scale)
11718 return 0;
11719
11720 base_string = end_scale;
11721 if (is_space_char (*base_string))
11722 ++base_string;
11723 if (*base_string != ')')
11724 {
11725 as_bad (_("expecting `)' "
11726 "after scale factor in `%s'"),
11727 operand_string);
11728 return 0;
11729 }
11730 }
11731 else if (!i.index_reg)
11732 {
11733 as_bad (_("expecting index register or scale factor "
11734 "after `,'; got '%c'"),
11735 *base_string);
11736 return 0;
11737 }
11738 }
11739 else if (*base_string != ')')
11740 {
11741 as_bad (_("expecting `,' or `)' "
11742 "after base register in `%s'"),
11743 operand_string);
11744 return 0;
11745 }
11746 }
11747 else if (*base_string == REGISTER_PREFIX)
11748 {
11749 end_op = strchr (base_string, ',');
11750 if (end_op)
11751 *end_op = '\0';
11752 as_bad (_("bad register name `%s'"), base_string);
11753 return 0;
11754 }
11755 }
11756
11757 /* If there's an expression beginning the operand, parse it,
11758 assuming displacement_string_start and
11759 displacement_string_end are meaningful. */
11760 if (displacement_string_start != displacement_string_end)
11761 {
11762 if (!i386_displacement (displacement_string_start,
11763 displacement_string_end))
11764 return 0;
11765 }
11766
11767 /* Special case for (%dx) while doing input/output op. */
11768 if (i.base_reg
11769 && i.base_reg->reg_type.bitfield.instance == RegD
11770 && i.base_reg->reg_type.bitfield.word
11771 && i.index_reg == 0
11772 && i.log2_scale_factor == 0
11773 && i.seg[i.mem_operands] == 0
11774 && !operand_type_check (i.types[this_operand], disp))
11775 {
11776 i.types[this_operand] = i.base_reg->reg_type;
11777 return 1;
11778 }
11779
11780 if (i386_index_check (operand_string) == 0)
11781 return 0;
11782 i.flags[this_operand] |= Operand_Mem;
11783 if (i.mem_operands == 0)
11784 i.memop1_string = xstrdup (operand_string);
11785 i.mem_operands++;
11786 }
11787 else
11788 {
11789 /* It's not a memory operand; argh! */
11790 as_bad (_("invalid char %s beginning operand %d `%s'"),
11791 output_invalid (*op_string),
11792 this_operand + 1,
11793 op_string);
11794 return 0;
11795 }
11796 return 1; /* Normal return. */
11797 }
11798 \f
11799 /* Calculate the maximum variable size (i.e., excluding fr_fix)
11800 that an rs_machine_dependent frag may reach. */
11801
11802 unsigned int
11803 i386_frag_max_var (fragS *frag)
11804 {
11805 /* The only relaxable frags are for jumps.
11806 Unconditional jumps can grow by 4 bytes and others by 5 bytes. */
11807 gas_assert (frag->fr_type == rs_machine_dependent);
11808 return TYPE_FROM_RELAX_STATE (frag->fr_subtype) == UNCOND_JUMP ? 4 : 5;
11809 }
11810
11811 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
11812 static int
11813 elf_symbol_resolved_in_segment_p (symbolS *fr_symbol, offsetT fr_var)
11814 {
11815 /* STT_GNU_IFUNC symbol must go through PLT. */
11816 if ((symbol_get_bfdsym (fr_symbol)->flags
11817 & BSF_GNU_INDIRECT_FUNCTION) != 0)
11818 return 0;
11819
11820 if (!S_IS_EXTERNAL (fr_symbol))
11821 /* Symbol may be weak or local. */
11822 return !S_IS_WEAK (fr_symbol);
11823
11824 /* Global symbols with non-default visibility can't be preempted. */
11825 if (ELF_ST_VISIBILITY (S_GET_OTHER (fr_symbol)) != STV_DEFAULT)
11826 return 1;
11827
11828 if (fr_var != NO_RELOC)
11829 switch ((enum bfd_reloc_code_real) fr_var)
11830 {
11831 case BFD_RELOC_386_PLT32:
11832 case BFD_RELOC_X86_64_PLT32:
11833 /* Symbol with PLT relocation may be preempted. */
11834 return 0;
11835 default:
11836 abort ();
11837 }
11838
11839 /* Global symbols with default visibility in a shared library may be
11840 preempted by another definition. */
11841 return !shared;
11842 }
11843 #endif
11844
11845 /* Table 3-2. Macro-Fusible Instructions in Haswell Microarchitecture
11846 Note also work for Skylake and Cascadelake.
11847 ---------------------------------------------------------------------
11848 | JCC | ADD/SUB/CMP | INC/DEC | TEST/AND |
11849 | ------ | ----------- | ------- | -------- |
11850 | Jo | N | N | Y |
11851 | Jno | N | N | Y |
11852 | Jc/Jb | Y | N | Y |
11853 | Jae/Jnb | Y | N | Y |
11854 | Je/Jz | Y | Y | Y |
11855 | Jne/Jnz | Y | Y | Y |
11856 | Jna/Jbe | Y | N | Y |
11857 | Ja/Jnbe | Y | N | Y |
11858 | Js | N | N | Y |
11859 | Jns | N | N | Y |
11860 | Jp/Jpe | N | N | Y |
11861 | Jnp/Jpo | N | N | Y |
11862 | Jl/Jnge | Y | Y | Y |
11863 | Jge/Jnl | Y | Y | Y |
11864 | Jle/Jng | Y | Y | Y |
11865 | Jg/Jnle | Y | Y | Y |
11866 --------------------------------------------------------------------- */
11867 static int
11868 i386_macro_fusible_p (enum mf_cmp_kind mf_cmp, enum mf_jcc_kind mf_jcc)
11869 {
11870 if (mf_cmp == mf_cmp_alu_cmp)
11871 return ((mf_jcc >= mf_jcc_jc && mf_jcc <= mf_jcc_jna)
11872 || mf_jcc == mf_jcc_jl || mf_jcc == mf_jcc_jle);
11873 if (mf_cmp == mf_cmp_incdec)
11874 return (mf_jcc == mf_jcc_je || mf_jcc == mf_jcc_jl
11875 || mf_jcc == mf_jcc_jle);
11876 if (mf_cmp == mf_cmp_test_and)
11877 return 1;
11878 return 0;
11879 }
11880
11881 /* Return the next non-empty frag. */
11882
11883 static fragS *
11884 i386_next_non_empty_frag (fragS *fragP)
11885 {
11886 /* There may be a frag with a ".fill 0" when there is no room in
11887 the current frag for frag_grow in output_insn. */
11888 for (fragP = fragP->fr_next;
11889 (fragP != NULL
11890 && fragP->fr_type == rs_fill
11891 && fragP->fr_fix == 0);
11892 fragP = fragP->fr_next)
11893 ;
11894 return fragP;
11895 }
11896
11897 /* Return the next jcc frag after BRANCH_PADDING. */
11898
11899 static fragS *
11900 i386_next_fusible_jcc_frag (fragS *maybe_cmp_fragP, fragS *pad_fragP)
11901 {
11902 fragS *branch_fragP;
11903 if (!pad_fragP)
11904 return NULL;
11905
11906 if (pad_fragP->fr_type == rs_machine_dependent
11907 && (TYPE_FROM_RELAX_STATE (pad_fragP->fr_subtype)
11908 == BRANCH_PADDING))
11909 {
11910 branch_fragP = i386_next_non_empty_frag (pad_fragP);
11911 if (branch_fragP->fr_type != rs_machine_dependent)
11912 return NULL;
11913 if (TYPE_FROM_RELAX_STATE (branch_fragP->fr_subtype) == COND_JUMP
11914 && i386_macro_fusible_p (maybe_cmp_fragP->tc_frag_data.mf_type,
11915 pad_fragP->tc_frag_data.mf_type))
11916 return branch_fragP;
11917 }
11918
11919 return NULL;
11920 }
11921
11922 /* Classify BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags. */
11923
11924 static void
11925 i386_classify_machine_dependent_frag (fragS *fragP)
11926 {
11927 fragS *cmp_fragP;
11928 fragS *pad_fragP;
11929 fragS *branch_fragP;
11930 fragS *next_fragP;
11931 unsigned int max_prefix_length;
11932
11933 if (fragP->tc_frag_data.classified)
11934 return;
11935
11936 /* First scan for BRANCH_PADDING and FUSED_JCC_PADDING. Convert
11937 FUSED_JCC_PADDING and merge BRANCH_PADDING. */
11938 for (next_fragP = fragP;
11939 next_fragP != NULL;
11940 next_fragP = next_fragP->fr_next)
11941 {
11942 next_fragP->tc_frag_data.classified = 1;
11943 if (next_fragP->fr_type == rs_machine_dependent)
11944 switch (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype))
11945 {
11946 case BRANCH_PADDING:
11947 /* The BRANCH_PADDING frag must be followed by a branch
11948 frag. */
11949 branch_fragP = i386_next_non_empty_frag (next_fragP);
11950 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
11951 break;
11952 case FUSED_JCC_PADDING:
11953 /* Check if this is a fused jcc:
11954 FUSED_JCC_PADDING
11955 CMP like instruction
11956 BRANCH_PADDING
11957 COND_JUMP
11958 */
11959 cmp_fragP = i386_next_non_empty_frag (next_fragP);
11960 pad_fragP = i386_next_non_empty_frag (cmp_fragP);
11961 branch_fragP = i386_next_fusible_jcc_frag (next_fragP, pad_fragP);
11962 if (branch_fragP)
11963 {
11964 /* The BRANCH_PADDING frag is merged with the
11965 FUSED_JCC_PADDING frag. */
11966 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
11967 /* CMP like instruction size. */
11968 next_fragP->tc_frag_data.cmp_size = cmp_fragP->fr_fix;
11969 frag_wane (pad_fragP);
11970 /* Skip to branch_fragP. */
11971 next_fragP = branch_fragP;
11972 }
11973 else if (next_fragP->tc_frag_data.max_prefix_length)
11974 {
11975 /* Turn FUSED_JCC_PADDING into BRANCH_PREFIX if it isn't
11976 a fused jcc. */
11977 next_fragP->fr_subtype
11978 = ENCODE_RELAX_STATE (BRANCH_PREFIX, 0);
11979 next_fragP->tc_frag_data.max_bytes
11980 = next_fragP->tc_frag_data.max_prefix_length;
11981 /* This will be updated in the BRANCH_PREFIX scan. */
11982 next_fragP->tc_frag_data.max_prefix_length = 0;
11983 }
11984 else
11985 frag_wane (next_fragP);
11986 break;
11987 }
11988 }
11989
11990 /* Stop if there is no BRANCH_PREFIX. */
11991 if (!align_branch_prefix_size)
11992 return;
11993
11994 /* Scan for BRANCH_PREFIX. */
11995 for (; fragP != NULL; fragP = fragP->fr_next)
11996 {
11997 if (fragP->fr_type != rs_machine_dependent
11998 || (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
11999 != BRANCH_PREFIX))
12000 continue;
12001
12002 /* Count all BRANCH_PREFIX frags before BRANCH_PADDING and
12003 COND_JUMP_PREFIX. */
12004 max_prefix_length = 0;
12005 for (next_fragP = fragP;
12006 next_fragP != NULL;
12007 next_fragP = next_fragP->fr_next)
12008 {
12009 if (next_fragP->fr_type == rs_fill)
12010 /* Skip rs_fill frags. */
12011 continue;
12012 else if (next_fragP->fr_type != rs_machine_dependent)
12013 /* Stop for all other frags. */
12014 break;
12015
12016 /* rs_machine_dependent frags. */
12017 if (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12018 == BRANCH_PREFIX)
12019 {
12020 /* Count BRANCH_PREFIX frags. */
12021 if (max_prefix_length >= MAX_FUSED_JCC_PADDING_SIZE)
12022 {
12023 max_prefix_length = MAX_FUSED_JCC_PADDING_SIZE;
12024 frag_wane (next_fragP);
12025 }
12026 else
12027 max_prefix_length
12028 += next_fragP->tc_frag_data.max_bytes;
12029 }
12030 else if ((TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12031 == BRANCH_PADDING)
12032 || (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12033 == FUSED_JCC_PADDING))
12034 {
12035 /* Stop at BRANCH_PADDING and FUSED_JCC_PADDING. */
12036 fragP->tc_frag_data.u.padding_fragP = next_fragP;
12037 break;
12038 }
12039 else
12040 /* Stop for other rs_machine_dependent frags. */
12041 break;
12042 }
12043
12044 fragP->tc_frag_data.max_prefix_length = max_prefix_length;
12045
12046 /* Skip to the next frag. */
12047 fragP = next_fragP;
12048 }
12049 }
12050
12051 /* Compute padding size for
12052
12053 FUSED_JCC_PADDING
12054 CMP like instruction
12055 BRANCH_PADDING
12056 COND_JUMP/UNCOND_JUMP
12057
12058 or
12059
12060 BRANCH_PADDING
12061 COND_JUMP/UNCOND_JUMP
12062 */
12063
12064 static int
12065 i386_branch_padding_size (fragS *fragP, offsetT address)
12066 {
12067 unsigned int offset, size, padding_size;
12068 fragS *branch_fragP = fragP->tc_frag_data.u.branch_fragP;
12069
12070 /* The start address of the BRANCH_PADDING or FUSED_JCC_PADDING frag. */
12071 if (!address)
12072 address = fragP->fr_address;
12073 address += fragP->fr_fix;
12074
12075 /* CMP like instrunction size. */
12076 size = fragP->tc_frag_data.cmp_size;
12077
12078 /* The base size of the branch frag. */
12079 size += branch_fragP->fr_fix;
12080
12081 /* Add opcode and displacement bytes for the rs_machine_dependent
12082 branch frag. */
12083 if (branch_fragP->fr_type == rs_machine_dependent)
12084 size += md_relax_table[branch_fragP->fr_subtype].rlx_length;
12085
12086 /* Check if branch is within boundary and doesn't end at the last
12087 byte. */
12088 offset = address & ((1U << align_branch_power) - 1);
12089 if ((offset + size) >= (1U << align_branch_power))
12090 /* Padding needed to avoid crossing boundary. */
12091 padding_size = (1U << align_branch_power) - offset;
12092 else
12093 /* No padding needed. */
12094 padding_size = 0;
12095
12096 /* The return value may be saved in tc_frag_data.length which is
12097 unsigned byte. */
12098 if (!fits_in_unsigned_byte (padding_size))
12099 abort ();
12100
12101 return padding_size;
12102 }
12103
12104 /* i386_generic_table_relax_frag()
12105
12106 Handle BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags to
12107 grow/shrink padding to align branch frags. Hand others to
12108 relax_frag(). */
12109
12110 long
12111 i386_generic_table_relax_frag (segT segment, fragS *fragP, long stretch)
12112 {
12113 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12114 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
12115 {
12116 long padding_size = i386_branch_padding_size (fragP, 0);
12117 long grow = padding_size - fragP->tc_frag_data.length;
12118
12119 /* When the BRANCH_PREFIX frag is used, the computed address
12120 must match the actual address and there should be no padding. */
12121 if (fragP->tc_frag_data.padding_address
12122 && (fragP->tc_frag_data.padding_address != fragP->fr_address
12123 || padding_size))
12124 abort ();
12125
12126 /* Update the padding size. */
12127 if (grow)
12128 fragP->tc_frag_data.length = padding_size;
12129
12130 return grow;
12131 }
12132 else if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12133 {
12134 fragS *padding_fragP, *next_fragP;
12135 long padding_size, left_size, last_size;
12136
12137 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
12138 if (!padding_fragP)
12139 /* Use the padding set by the leading BRANCH_PREFIX frag. */
12140 return (fragP->tc_frag_data.length
12141 - fragP->tc_frag_data.last_length);
12142
12143 /* Compute the relative address of the padding frag in the very
12144 first time where the BRANCH_PREFIX frag sizes are zero. */
12145 if (!fragP->tc_frag_data.padding_address)
12146 fragP->tc_frag_data.padding_address
12147 = padding_fragP->fr_address - (fragP->fr_address - stretch);
12148
12149 /* First update the last length from the previous interation. */
12150 left_size = fragP->tc_frag_data.prefix_length;
12151 for (next_fragP = fragP;
12152 next_fragP != padding_fragP;
12153 next_fragP = next_fragP->fr_next)
12154 if (next_fragP->fr_type == rs_machine_dependent
12155 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12156 == BRANCH_PREFIX))
12157 {
12158 if (left_size)
12159 {
12160 int max = next_fragP->tc_frag_data.max_bytes;
12161 if (max)
12162 {
12163 int size;
12164 if (max > left_size)
12165 size = left_size;
12166 else
12167 size = max;
12168 left_size -= size;
12169 next_fragP->tc_frag_data.last_length = size;
12170 }
12171 }
12172 else
12173 next_fragP->tc_frag_data.last_length = 0;
12174 }
12175
12176 /* Check the padding size for the padding frag. */
12177 padding_size = i386_branch_padding_size
12178 (padding_fragP, (fragP->fr_address
12179 + fragP->tc_frag_data.padding_address));
12180
12181 last_size = fragP->tc_frag_data.prefix_length;
12182 /* Check if there is change from the last interation. */
12183 if (padding_size == last_size)
12184 {
12185 /* Update the expected address of the padding frag. */
12186 padding_fragP->tc_frag_data.padding_address
12187 = (fragP->fr_address + padding_size
12188 + fragP->tc_frag_data.padding_address);
12189 return 0;
12190 }
12191
12192 if (padding_size > fragP->tc_frag_data.max_prefix_length)
12193 {
12194 /* No padding if there is no sufficient room. Clear the
12195 expected address of the padding frag. */
12196 padding_fragP->tc_frag_data.padding_address = 0;
12197 padding_size = 0;
12198 }
12199 else
12200 /* Store the expected address of the padding frag. */
12201 padding_fragP->tc_frag_data.padding_address
12202 = (fragP->fr_address + padding_size
12203 + fragP->tc_frag_data.padding_address);
12204
12205 fragP->tc_frag_data.prefix_length = padding_size;
12206
12207 /* Update the length for the current interation. */
12208 left_size = padding_size;
12209 for (next_fragP = fragP;
12210 next_fragP != padding_fragP;
12211 next_fragP = next_fragP->fr_next)
12212 if (next_fragP->fr_type == rs_machine_dependent
12213 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12214 == BRANCH_PREFIX))
12215 {
12216 if (left_size)
12217 {
12218 int max = next_fragP->tc_frag_data.max_bytes;
12219 if (max)
12220 {
12221 int size;
12222 if (max > left_size)
12223 size = left_size;
12224 else
12225 size = max;
12226 left_size -= size;
12227 next_fragP->tc_frag_data.length = size;
12228 }
12229 }
12230 else
12231 next_fragP->tc_frag_data.length = 0;
12232 }
12233
12234 return (fragP->tc_frag_data.length
12235 - fragP->tc_frag_data.last_length);
12236 }
12237 return relax_frag (segment, fragP, stretch);
12238 }
12239
12240 /* md_estimate_size_before_relax()
12241
12242 Called just before relax() for rs_machine_dependent frags. The x86
12243 assembler uses these frags to handle variable size jump
12244 instructions.
12245
12246 Any symbol that is now undefined will not become defined.
12247 Return the correct fr_subtype in the frag.
12248 Return the initial "guess for variable size of frag" to caller.
12249 The guess is actually the growth beyond the fixed part. Whatever
12250 we do to grow the fixed or variable part contributes to our
12251 returned value. */
12252
12253 int
12254 md_estimate_size_before_relax (fragS *fragP, segT segment)
12255 {
12256 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12257 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX
12258 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
12259 {
12260 i386_classify_machine_dependent_frag (fragP);
12261 return fragP->tc_frag_data.length;
12262 }
12263
12264 /* We've already got fragP->fr_subtype right; all we have to do is
12265 check for un-relaxable symbols. On an ELF system, we can't relax
12266 an externally visible symbol, because it may be overridden by a
12267 shared library. */
12268 if (S_GET_SEGMENT (fragP->fr_symbol) != segment
12269 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
12270 || (IS_ELF
12271 && !elf_symbol_resolved_in_segment_p (fragP->fr_symbol,
12272 fragP->fr_var))
12273 #endif
12274 #if defined (OBJ_COFF) && defined (TE_PE)
12275 || (OUTPUT_FLAVOR == bfd_target_coff_flavour
12276 && S_IS_WEAK (fragP->fr_symbol))
12277 #endif
12278 )
12279 {
12280 /* Symbol is undefined in this segment, or we need to keep a
12281 reloc so that weak symbols can be overridden. */
12282 int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
12283 enum bfd_reloc_code_real reloc_type;
12284 unsigned char *opcode;
12285 int old_fr_fix;
12286 fixS *fixP = NULL;
12287
12288 if (fragP->fr_var != NO_RELOC)
12289 reloc_type = (enum bfd_reloc_code_real) fragP->fr_var;
12290 else if (size == 2)
12291 reloc_type = BFD_RELOC_16_PCREL;
12292 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
12293 else if (need_plt32_p (fragP->fr_symbol))
12294 reloc_type = BFD_RELOC_X86_64_PLT32;
12295 #endif
12296 else
12297 reloc_type = BFD_RELOC_32_PCREL;
12298
12299 old_fr_fix = fragP->fr_fix;
12300 opcode = (unsigned char *) fragP->fr_opcode;
12301
12302 switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
12303 {
12304 case UNCOND_JUMP:
12305 /* Make jmp (0xeb) a (d)word displacement jump. */
12306 opcode[0] = 0xe9;
12307 fragP->fr_fix += size;
12308 fixP = fix_new (fragP, old_fr_fix, size,
12309 fragP->fr_symbol,
12310 fragP->fr_offset, 1,
12311 reloc_type);
12312 break;
12313
12314 case COND_JUMP86:
12315 if (size == 2
12316 && (!no_cond_jump_promotion || fragP->fr_var != NO_RELOC))
12317 {
12318 /* Negate the condition, and branch past an
12319 unconditional jump. */
12320 opcode[0] ^= 1;
12321 opcode[1] = 3;
12322 /* Insert an unconditional jump. */
12323 opcode[2] = 0xe9;
12324 /* We added two extra opcode bytes, and have a two byte
12325 offset. */
12326 fragP->fr_fix += 2 + 2;
12327 fix_new (fragP, old_fr_fix + 2, 2,
12328 fragP->fr_symbol,
12329 fragP->fr_offset, 1,
12330 reloc_type);
12331 break;
12332 }
12333 /* Fall through. */
12334
12335 case COND_JUMP:
12336 if (no_cond_jump_promotion && fragP->fr_var == NO_RELOC)
12337 {
12338 fragP->fr_fix += 1;
12339 fixP = fix_new (fragP, old_fr_fix, 1,
12340 fragP->fr_symbol,
12341 fragP->fr_offset, 1,
12342 BFD_RELOC_8_PCREL);
12343 fixP->fx_signed = 1;
12344 break;
12345 }
12346
12347 /* This changes the byte-displacement jump 0x7N
12348 to the (d)word-displacement jump 0x0f,0x8N. */
12349 opcode[1] = opcode[0] + 0x10;
12350 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12351 /* We've added an opcode byte. */
12352 fragP->fr_fix += 1 + size;
12353 fixP = fix_new (fragP, old_fr_fix + 1, size,
12354 fragP->fr_symbol,
12355 fragP->fr_offset, 1,
12356 reloc_type);
12357 break;
12358
12359 default:
12360 BAD_CASE (fragP->fr_subtype);
12361 break;
12362 }
12363
12364 /* All jumps handled here are signed, but don't unconditionally use a
12365 signed limit check for 32 and 16 bit jumps as we want to allow wrap
12366 around at 4G (outside of 64-bit mode) and 64k. */
12367 if (size == 4 && flag_code == CODE_64BIT)
12368 fixP->fx_signed = 1;
12369
12370 frag_wane (fragP);
12371 return fragP->fr_fix - old_fr_fix;
12372 }
12373
12374 /* Guess size depending on current relax state. Initially the relax
12375 state will correspond to a short jump and we return 1, because
12376 the variable part of the frag (the branch offset) is one byte
12377 long. However, we can relax a section more than once and in that
12378 case we must either set fr_subtype back to the unrelaxed state,
12379 or return the value for the appropriate branch. */
12380 return md_relax_table[fragP->fr_subtype].rlx_length;
12381 }
12382
12383 /* Called after relax() is finished.
12384
12385 In: Address of frag.
12386 fr_type == rs_machine_dependent.
12387 fr_subtype is what the address relaxed to.
12388
12389 Out: Any fixSs and constants are set up.
12390 Caller will turn frag into a ".space 0". */
12391
12392 void
12393 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec ATTRIBUTE_UNUSED,
12394 fragS *fragP)
12395 {
12396 unsigned char *opcode;
12397 unsigned char *where_to_put_displacement = NULL;
12398 offsetT target_address;
12399 offsetT opcode_address;
12400 unsigned int extension = 0;
12401 offsetT displacement_from_opcode_start;
12402
12403 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12404 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING
12405 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12406 {
12407 /* Generate nop padding. */
12408 unsigned int size = fragP->tc_frag_data.length;
12409 if (size)
12410 {
12411 if (size > fragP->tc_frag_data.max_bytes)
12412 abort ();
12413
12414 if (flag_debug)
12415 {
12416 const char *msg;
12417 const char *branch = "branch";
12418 const char *prefix = "";
12419 fragS *padding_fragP;
12420 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
12421 == BRANCH_PREFIX)
12422 {
12423 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
12424 switch (fragP->tc_frag_data.default_prefix)
12425 {
12426 default:
12427 abort ();
12428 break;
12429 case CS_PREFIX_OPCODE:
12430 prefix = " cs";
12431 break;
12432 case DS_PREFIX_OPCODE:
12433 prefix = " ds";
12434 break;
12435 case ES_PREFIX_OPCODE:
12436 prefix = " es";
12437 break;
12438 case FS_PREFIX_OPCODE:
12439 prefix = " fs";
12440 break;
12441 case GS_PREFIX_OPCODE:
12442 prefix = " gs";
12443 break;
12444 case SS_PREFIX_OPCODE:
12445 prefix = " ss";
12446 break;
12447 }
12448 if (padding_fragP)
12449 msg = _("%s:%u: add %d%s at 0x%llx to align "
12450 "%s within %d-byte boundary\n");
12451 else
12452 msg = _("%s:%u: add additional %d%s at 0x%llx to "
12453 "align %s within %d-byte boundary\n");
12454 }
12455 else
12456 {
12457 padding_fragP = fragP;
12458 msg = _("%s:%u: add %d%s-byte nop at 0x%llx to align "
12459 "%s within %d-byte boundary\n");
12460 }
12461
12462 if (padding_fragP)
12463 switch (padding_fragP->tc_frag_data.branch_type)
12464 {
12465 case align_branch_jcc:
12466 branch = "jcc";
12467 break;
12468 case align_branch_fused:
12469 branch = "fused jcc";
12470 break;
12471 case align_branch_jmp:
12472 branch = "jmp";
12473 break;
12474 case align_branch_call:
12475 branch = "call";
12476 break;
12477 case align_branch_indirect:
12478 branch = "indiret branch";
12479 break;
12480 case align_branch_ret:
12481 branch = "ret";
12482 break;
12483 default:
12484 break;
12485 }
12486
12487 fprintf (stdout, msg,
12488 fragP->fr_file, fragP->fr_line, size, prefix,
12489 (long long) fragP->fr_address, branch,
12490 1 << align_branch_power);
12491 }
12492 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12493 memset (fragP->fr_opcode,
12494 fragP->tc_frag_data.default_prefix, size);
12495 else
12496 i386_generate_nops (fragP, (char *) fragP->fr_opcode,
12497 size, 0);
12498 fragP->fr_fix += size;
12499 }
12500 return;
12501 }
12502
12503 opcode = (unsigned char *) fragP->fr_opcode;
12504
12505 /* Address we want to reach in file space. */
12506 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
12507
12508 /* Address opcode resides at in file space. */
12509 opcode_address = fragP->fr_address + fragP->fr_fix;
12510
12511 /* Displacement from opcode start to fill into instruction. */
12512 displacement_from_opcode_start = target_address - opcode_address;
12513
12514 if ((fragP->fr_subtype & BIG) == 0)
12515 {
12516 /* Don't have to change opcode. */
12517 extension = 1; /* 1 opcode + 1 displacement */
12518 where_to_put_displacement = &opcode[1];
12519 }
12520 else
12521 {
12522 if (no_cond_jump_promotion
12523 && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
12524 as_warn_where (fragP->fr_file, fragP->fr_line,
12525 _("long jump required"));
12526
12527 switch (fragP->fr_subtype)
12528 {
12529 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
12530 extension = 4; /* 1 opcode + 4 displacement */
12531 opcode[0] = 0xe9;
12532 where_to_put_displacement = &opcode[1];
12533 break;
12534
12535 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
12536 extension = 2; /* 1 opcode + 2 displacement */
12537 opcode[0] = 0xe9;
12538 where_to_put_displacement = &opcode[1];
12539 break;
12540
12541 case ENCODE_RELAX_STATE (COND_JUMP, BIG):
12542 case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
12543 extension = 5; /* 2 opcode + 4 displacement */
12544 opcode[1] = opcode[0] + 0x10;
12545 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12546 where_to_put_displacement = &opcode[2];
12547 break;
12548
12549 case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
12550 extension = 3; /* 2 opcode + 2 displacement */
12551 opcode[1] = opcode[0] + 0x10;
12552 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12553 where_to_put_displacement = &opcode[2];
12554 break;
12555
12556 case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
12557 extension = 4;
12558 opcode[0] ^= 1;
12559 opcode[1] = 3;
12560 opcode[2] = 0xe9;
12561 where_to_put_displacement = &opcode[3];
12562 break;
12563
12564 default:
12565 BAD_CASE (fragP->fr_subtype);
12566 break;
12567 }
12568 }
12569
12570 /* If size if less then four we are sure that the operand fits,
12571 but if it's 4, then it could be that the displacement is larger
12572 then -/+ 2GB. */
12573 if (DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype) == 4
12574 && object_64bit
12575 && ((addressT) (displacement_from_opcode_start - extension
12576 + ((addressT) 1 << 31))
12577 > (((addressT) 2 << 31) - 1)))
12578 {
12579 as_bad_where (fragP->fr_file, fragP->fr_line,
12580 _("jump target out of range"));
12581 /* Make us emit 0. */
12582 displacement_from_opcode_start = extension;
12583 }
12584 /* Now put displacement after opcode. */
12585 md_number_to_chars ((char *) where_to_put_displacement,
12586 (valueT) (displacement_from_opcode_start - extension),
12587 DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
12588 fragP->fr_fix += extension;
12589 }
12590 \f
12591 /* Apply a fixup (fixP) to segment data, once it has been determined
12592 by our caller that we have all the info we need to fix it up.
12593
12594 Parameter valP is the pointer to the value of the bits.
12595
12596 On the 386, immediates, displacements, and data pointers are all in
12597 the same (little-endian) format, so we don't need to care about which
12598 we are handling. */
12599
12600 void
12601 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
12602 {
12603 char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
12604 valueT value = *valP;
12605
12606 #if !defined (TE_Mach)
12607 if (fixP->fx_pcrel)
12608 {
12609 switch (fixP->fx_r_type)
12610 {
12611 default:
12612 break;
12613
12614 case BFD_RELOC_64:
12615 fixP->fx_r_type = BFD_RELOC_64_PCREL;
12616 break;
12617 case BFD_RELOC_32:
12618 case BFD_RELOC_X86_64_32S:
12619 fixP->fx_r_type = BFD_RELOC_32_PCREL;
12620 break;
12621 case BFD_RELOC_16:
12622 fixP->fx_r_type = BFD_RELOC_16_PCREL;
12623 break;
12624 case BFD_RELOC_8:
12625 fixP->fx_r_type = BFD_RELOC_8_PCREL;
12626 break;
12627 }
12628 }
12629
12630 if (fixP->fx_addsy != NULL
12631 && (fixP->fx_r_type == BFD_RELOC_32_PCREL
12632 || fixP->fx_r_type == BFD_RELOC_64_PCREL
12633 || fixP->fx_r_type == BFD_RELOC_16_PCREL
12634 || fixP->fx_r_type == BFD_RELOC_8_PCREL)
12635 && !use_rela_relocations)
12636 {
12637 /* This is a hack. There should be a better way to handle this.
12638 This covers for the fact that bfd_install_relocation will
12639 subtract the current location (for partial_inplace, PC relative
12640 relocations); see more below. */
12641 #ifndef OBJ_AOUT
12642 if (IS_ELF
12643 #ifdef TE_PE
12644 || OUTPUT_FLAVOR == bfd_target_coff_flavour
12645 #endif
12646 )
12647 value += fixP->fx_where + fixP->fx_frag->fr_address;
12648 #endif
12649 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
12650 if (IS_ELF)
12651 {
12652 segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
12653
12654 if ((sym_seg == seg
12655 || (symbol_section_p (fixP->fx_addsy)
12656 && sym_seg != absolute_section))
12657 && !generic_force_reloc (fixP))
12658 {
12659 /* Yes, we add the values in twice. This is because
12660 bfd_install_relocation subtracts them out again. I think
12661 bfd_install_relocation is broken, but I don't dare change
12662 it. FIXME. */
12663 value += fixP->fx_where + fixP->fx_frag->fr_address;
12664 }
12665 }
12666 #endif
12667 #if defined (OBJ_COFF) && defined (TE_PE)
12668 /* For some reason, the PE format does not store a
12669 section address offset for a PC relative symbol. */
12670 if (S_GET_SEGMENT (fixP->fx_addsy) != seg
12671 || S_IS_WEAK (fixP->fx_addsy))
12672 value += md_pcrel_from (fixP);
12673 #endif
12674 }
12675 #if defined (OBJ_COFF) && defined (TE_PE)
12676 if (fixP->fx_addsy != NULL
12677 && S_IS_WEAK (fixP->fx_addsy)
12678 /* PR 16858: Do not modify weak function references. */
12679 && ! fixP->fx_pcrel)
12680 {
12681 #if !defined (TE_PEP)
12682 /* For x86 PE weak function symbols are neither PC-relative
12683 nor do they set S_IS_FUNCTION. So the only reliable way
12684 to detect them is to check the flags of their containing
12685 section. */
12686 if (S_GET_SEGMENT (fixP->fx_addsy) != NULL
12687 && S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_CODE)
12688 ;
12689 else
12690 #endif
12691 value -= S_GET_VALUE (fixP->fx_addsy);
12692 }
12693 #endif
12694
12695 /* Fix a few things - the dynamic linker expects certain values here,
12696 and we must not disappoint it. */
12697 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
12698 if (IS_ELF && fixP->fx_addsy)
12699 switch (fixP->fx_r_type)
12700 {
12701 case BFD_RELOC_386_PLT32:
12702 case BFD_RELOC_X86_64_PLT32:
12703 /* Make the jump instruction point to the address of the operand.
12704 At runtime we merely add the offset to the actual PLT entry.
12705 NB: Subtract the offset size only for jump instructions. */
12706 if (fixP->fx_pcrel)
12707 value = -4;
12708 break;
12709
12710 case BFD_RELOC_386_TLS_GD:
12711 case BFD_RELOC_386_TLS_LDM:
12712 case BFD_RELOC_386_TLS_IE_32:
12713 case BFD_RELOC_386_TLS_IE:
12714 case BFD_RELOC_386_TLS_GOTIE:
12715 case BFD_RELOC_386_TLS_GOTDESC:
12716 case BFD_RELOC_X86_64_TLSGD:
12717 case BFD_RELOC_X86_64_TLSLD:
12718 case BFD_RELOC_X86_64_GOTTPOFF:
12719 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
12720 value = 0; /* Fully resolved at runtime. No addend. */
12721 /* Fallthrough */
12722 case BFD_RELOC_386_TLS_LE:
12723 case BFD_RELOC_386_TLS_LDO_32:
12724 case BFD_RELOC_386_TLS_LE_32:
12725 case BFD_RELOC_X86_64_DTPOFF32:
12726 case BFD_RELOC_X86_64_DTPOFF64:
12727 case BFD_RELOC_X86_64_TPOFF32:
12728 case BFD_RELOC_X86_64_TPOFF64:
12729 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12730 break;
12731
12732 case BFD_RELOC_386_TLS_DESC_CALL:
12733 case BFD_RELOC_X86_64_TLSDESC_CALL:
12734 value = 0; /* Fully resolved at runtime. No addend. */
12735 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12736 fixP->fx_done = 0;
12737 return;
12738
12739 case BFD_RELOC_VTABLE_INHERIT:
12740 case BFD_RELOC_VTABLE_ENTRY:
12741 fixP->fx_done = 0;
12742 return;
12743
12744 default:
12745 break;
12746 }
12747 #endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) */
12748
12749 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
12750 if (!object_64bit)
12751 value = extend_to_32bit_address (value);
12752
12753 *valP = value;
12754 #endif /* !defined (TE_Mach) */
12755
12756 /* Are we finished with this relocation now? */
12757 if (fixP->fx_addsy == NULL)
12758 {
12759 fixP->fx_done = 1;
12760 switch (fixP->fx_r_type)
12761 {
12762 case BFD_RELOC_X86_64_32S:
12763 fixP->fx_signed = 1;
12764 break;
12765
12766 default:
12767 break;
12768 }
12769 }
12770 #if defined (OBJ_COFF) && defined (TE_PE)
12771 else if (fixP->fx_addsy != NULL && S_IS_WEAK (fixP->fx_addsy))
12772 {
12773 fixP->fx_done = 0;
12774 /* Remember value for tc_gen_reloc. */
12775 fixP->fx_addnumber = value;
12776 /* Clear out the frag for now. */
12777 value = 0;
12778 }
12779 #endif
12780 else if (use_rela_relocations)
12781 {
12782 fixP->fx_no_overflow = 1;
12783 /* Remember value for tc_gen_reloc. */
12784 fixP->fx_addnumber = value;
12785 value = 0;
12786 }
12787
12788 md_number_to_chars (p, value, fixP->fx_size);
12789 }
12790 \f
12791 const char *
12792 md_atof (int type, char *litP, int *sizeP)
12793 {
12794 /* This outputs the LITTLENUMs in REVERSE order;
12795 in accord with the bigendian 386. */
12796 return ieee_md_atof (type, litP, sizeP, false);
12797 }
12798 \f
12799 static char output_invalid_buf[sizeof (unsigned char) * 2 + 6];
12800
12801 static char *
12802 output_invalid (int c)
12803 {
12804 if (ISPRINT (c))
12805 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
12806 "'%c'", c);
12807 else
12808 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
12809 "(0x%x)", (unsigned char) c);
12810 return output_invalid_buf;
12811 }
12812
12813 /* Verify that @r can be used in the current context. */
12814
12815 static bool check_register (const reg_entry *r)
12816 {
12817 if (allow_pseudo_reg)
12818 return true;
12819
12820 if (operand_type_all_zero (&r->reg_type))
12821 return false;
12822
12823 if ((r->reg_type.bitfield.dword
12824 || (r->reg_type.bitfield.class == SReg && r->reg_num > 3)
12825 || r->reg_type.bitfield.class == RegCR
12826 || r->reg_type.bitfield.class == RegDR)
12827 && !cpu_arch_flags.bitfield.cpui386)
12828 return false;
12829
12830 if (r->reg_type.bitfield.class == RegTR
12831 && (flag_code == CODE_64BIT
12832 || !cpu_arch_flags.bitfield.cpui386
12833 || cpu_arch_isa_flags.bitfield.cpui586
12834 || cpu_arch_isa_flags.bitfield.cpui686))
12835 return false;
12836
12837 if (r->reg_type.bitfield.class == RegMMX && !cpu_arch_flags.bitfield.cpummx)
12838 return false;
12839
12840 if (!cpu_arch_flags.bitfield.cpuavx512f)
12841 {
12842 if (r->reg_type.bitfield.zmmword
12843 || r->reg_type.bitfield.class == RegMask)
12844 return false;
12845
12846 if (!cpu_arch_flags.bitfield.cpuavx)
12847 {
12848 if (r->reg_type.bitfield.ymmword)
12849 return false;
12850
12851 if (!cpu_arch_flags.bitfield.cpusse && r->reg_type.bitfield.xmmword)
12852 return false;
12853 }
12854 }
12855
12856 if (r->reg_type.bitfield.tmmword
12857 && (!cpu_arch_flags.bitfield.cpuamx_tile
12858 || flag_code != CODE_64BIT))
12859 return false;
12860
12861 if (r->reg_type.bitfield.class == RegBND && !cpu_arch_flags.bitfield.cpumpx)
12862 return false;
12863
12864 /* Don't allow fake index register unless allow_index_reg isn't 0. */
12865 if (!allow_index_reg && r->reg_num == RegIZ)
12866 return false;
12867
12868 /* Upper 16 vector registers are only available with VREX in 64bit
12869 mode, and require EVEX encoding. */
12870 if (r->reg_flags & RegVRex)
12871 {
12872 if (!cpu_arch_flags.bitfield.cpuavx512f
12873 || flag_code != CODE_64BIT)
12874 return false;
12875
12876 if (i.vec_encoding == vex_encoding_default)
12877 i.vec_encoding = vex_encoding_evex;
12878 else if (i.vec_encoding != vex_encoding_evex)
12879 i.vec_encoding = vex_encoding_error;
12880 }
12881
12882 if (((r->reg_flags & (RegRex64 | RegRex)) || r->reg_type.bitfield.qword)
12883 && (!cpu_arch_flags.bitfield.cpulm || r->reg_type.bitfield.class != RegCR)
12884 && flag_code != CODE_64BIT)
12885 return false;
12886
12887 if (r->reg_type.bitfield.class == SReg && r->reg_num == RegFlat
12888 && !intel_syntax)
12889 return false;
12890
12891 return true;
12892 }
12893
12894 /* REG_STRING starts *before* REGISTER_PREFIX. */
12895
12896 static const reg_entry *
12897 parse_real_register (char *reg_string, char **end_op)
12898 {
12899 char *s = reg_string;
12900 char *p;
12901 char reg_name_given[MAX_REG_NAME_SIZE + 1];
12902 const reg_entry *r;
12903
12904 /* Skip possible REGISTER_PREFIX and possible whitespace. */
12905 if (*s == REGISTER_PREFIX)
12906 ++s;
12907
12908 if (is_space_char (*s))
12909 ++s;
12910
12911 p = reg_name_given;
12912 while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
12913 {
12914 if (p >= reg_name_given + MAX_REG_NAME_SIZE)
12915 return (const reg_entry *) NULL;
12916 s++;
12917 }
12918
12919 /* For naked regs, make sure that we are not dealing with an identifier.
12920 This prevents confusing an identifier like `eax_var' with register
12921 `eax'. */
12922 if (allow_naked_reg && identifier_chars[(unsigned char) *s])
12923 return (const reg_entry *) NULL;
12924
12925 *end_op = s;
12926
12927 r = (const reg_entry *) str_hash_find (reg_hash, reg_name_given);
12928
12929 /* Handle floating point regs, allowing spaces in the (i) part. */
12930 if (r == reg_st0)
12931 {
12932 if (!cpu_arch_flags.bitfield.cpu8087
12933 && !cpu_arch_flags.bitfield.cpu287
12934 && !cpu_arch_flags.bitfield.cpu387
12935 && !allow_pseudo_reg)
12936 return (const reg_entry *) NULL;
12937
12938 if (is_space_char (*s))
12939 ++s;
12940 if (*s == '(')
12941 {
12942 ++s;
12943 if (is_space_char (*s))
12944 ++s;
12945 if (*s >= '0' && *s <= '7')
12946 {
12947 int fpr = *s - '0';
12948 ++s;
12949 if (is_space_char (*s))
12950 ++s;
12951 if (*s == ')')
12952 {
12953 *end_op = s + 1;
12954 know (r[fpr].reg_num == fpr);
12955 return r + fpr;
12956 }
12957 }
12958 /* We have "%st(" then garbage. */
12959 return (const reg_entry *) NULL;
12960 }
12961 }
12962
12963 return r && check_register (r) ? r : NULL;
12964 }
12965
12966 /* REG_STRING starts *before* REGISTER_PREFIX. */
12967
12968 static const reg_entry *
12969 parse_register (char *reg_string, char **end_op)
12970 {
12971 const reg_entry *r;
12972
12973 if (*reg_string == REGISTER_PREFIX || allow_naked_reg)
12974 r = parse_real_register (reg_string, end_op);
12975 else
12976 r = NULL;
12977 if (!r)
12978 {
12979 char *save = input_line_pointer;
12980 char c;
12981 symbolS *symbolP;
12982
12983 input_line_pointer = reg_string;
12984 c = get_symbol_name (&reg_string);
12985 symbolP = symbol_find (reg_string);
12986 if (symbolP && S_GET_SEGMENT (symbolP) == reg_section)
12987 {
12988 const expressionS *e = symbol_get_value_expression (symbolP);
12989
12990 know (e->X_op == O_register);
12991 know (e->X_add_number >= 0
12992 && (valueT) e->X_add_number < i386_regtab_size);
12993 r = i386_regtab + e->X_add_number;
12994 if (!check_register (r))
12995 {
12996 as_bad (_("register '%s%s' cannot be used here"),
12997 register_prefix, r->reg_name);
12998 r = &bad_reg;
12999 }
13000 *end_op = input_line_pointer;
13001 }
13002 *input_line_pointer = c;
13003 input_line_pointer = save;
13004 }
13005 return r;
13006 }
13007
13008 int
13009 i386_parse_name (char *name, expressionS *e, char *nextcharP)
13010 {
13011 const reg_entry *r;
13012 char *end = input_line_pointer;
13013
13014 *end = *nextcharP;
13015 r = parse_register (name, &input_line_pointer);
13016 if (r && end <= input_line_pointer)
13017 {
13018 *nextcharP = *input_line_pointer;
13019 *input_line_pointer = 0;
13020 if (r != &bad_reg)
13021 {
13022 e->X_op = O_register;
13023 e->X_add_number = r - i386_regtab;
13024 }
13025 else
13026 e->X_op = O_illegal;
13027 return 1;
13028 }
13029 input_line_pointer = end;
13030 *end = 0;
13031 return intel_syntax ? i386_intel_parse_name (name, e) : 0;
13032 }
13033
13034 void
13035 md_operand (expressionS *e)
13036 {
13037 char *end;
13038 const reg_entry *r;
13039
13040 switch (*input_line_pointer)
13041 {
13042 case REGISTER_PREFIX:
13043 r = parse_real_register (input_line_pointer, &end);
13044 if (r)
13045 {
13046 e->X_op = O_register;
13047 e->X_add_number = r - i386_regtab;
13048 input_line_pointer = end;
13049 }
13050 break;
13051
13052 case '[':
13053 gas_assert (intel_syntax);
13054 end = input_line_pointer++;
13055 expression (e);
13056 if (*input_line_pointer == ']')
13057 {
13058 ++input_line_pointer;
13059 e->X_op_symbol = make_expr_symbol (e);
13060 e->X_add_symbol = NULL;
13061 e->X_add_number = 0;
13062 e->X_op = O_index;
13063 }
13064 else
13065 {
13066 e->X_op = O_absent;
13067 input_line_pointer = end;
13068 }
13069 break;
13070 }
13071 }
13072
13073 \f
13074 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13075 const char *md_shortopts = "kVQ:sqnO::";
13076 #else
13077 const char *md_shortopts = "qnO::";
13078 #endif
13079
13080 #define OPTION_32 (OPTION_MD_BASE + 0)
13081 #define OPTION_64 (OPTION_MD_BASE + 1)
13082 #define OPTION_DIVIDE (OPTION_MD_BASE + 2)
13083 #define OPTION_MARCH (OPTION_MD_BASE + 3)
13084 #define OPTION_MTUNE (OPTION_MD_BASE + 4)
13085 #define OPTION_MMNEMONIC (OPTION_MD_BASE + 5)
13086 #define OPTION_MSYNTAX (OPTION_MD_BASE + 6)
13087 #define OPTION_MINDEX_REG (OPTION_MD_BASE + 7)
13088 #define OPTION_MNAKED_REG (OPTION_MD_BASE + 8)
13089 #define OPTION_MRELAX_RELOCATIONS (OPTION_MD_BASE + 9)
13090 #define OPTION_MSSE2AVX (OPTION_MD_BASE + 10)
13091 #define OPTION_MSSE_CHECK (OPTION_MD_BASE + 11)
13092 #define OPTION_MOPERAND_CHECK (OPTION_MD_BASE + 12)
13093 #define OPTION_MAVXSCALAR (OPTION_MD_BASE + 13)
13094 #define OPTION_X32 (OPTION_MD_BASE + 14)
13095 #define OPTION_MADD_BND_PREFIX (OPTION_MD_BASE + 15)
13096 #define OPTION_MEVEXLIG (OPTION_MD_BASE + 16)
13097 #define OPTION_MEVEXWIG (OPTION_MD_BASE + 17)
13098 #define OPTION_MBIG_OBJ (OPTION_MD_BASE + 18)
13099 #define OPTION_MOMIT_LOCK_PREFIX (OPTION_MD_BASE + 19)
13100 #define OPTION_MEVEXRCIG (OPTION_MD_BASE + 20)
13101 #define OPTION_MSHARED (OPTION_MD_BASE + 21)
13102 #define OPTION_MAMD64 (OPTION_MD_BASE + 22)
13103 #define OPTION_MINTEL64 (OPTION_MD_BASE + 23)
13104 #define OPTION_MFENCE_AS_LOCK_ADD (OPTION_MD_BASE + 24)
13105 #define OPTION_X86_USED_NOTE (OPTION_MD_BASE + 25)
13106 #define OPTION_MVEXWIG (OPTION_MD_BASE + 26)
13107 #define OPTION_MALIGN_BRANCH_BOUNDARY (OPTION_MD_BASE + 27)
13108 #define OPTION_MALIGN_BRANCH_PREFIX_SIZE (OPTION_MD_BASE + 28)
13109 #define OPTION_MALIGN_BRANCH (OPTION_MD_BASE + 29)
13110 #define OPTION_MBRANCHES_WITH_32B_BOUNDARIES (OPTION_MD_BASE + 30)
13111 #define OPTION_MLFENCE_AFTER_LOAD (OPTION_MD_BASE + 31)
13112 #define OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH (OPTION_MD_BASE + 32)
13113 #define OPTION_MLFENCE_BEFORE_RET (OPTION_MD_BASE + 33)
13114 #define OPTION_MUSE_UNALIGNED_VECTOR_MOVE (OPTION_MD_BASE + 34)
13115
13116 struct option md_longopts[] =
13117 {
13118 {"32", no_argument, NULL, OPTION_32},
13119 #if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
13120 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
13121 {"64", no_argument, NULL, OPTION_64},
13122 #endif
13123 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13124 {"x32", no_argument, NULL, OPTION_X32},
13125 {"mshared", no_argument, NULL, OPTION_MSHARED},
13126 {"mx86-used-note", required_argument, NULL, OPTION_X86_USED_NOTE},
13127 #endif
13128 {"divide", no_argument, NULL, OPTION_DIVIDE},
13129 {"march", required_argument, NULL, OPTION_MARCH},
13130 {"mtune", required_argument, NULL, OPTION_MTUNE},
13131 {"mmnemonic", required_argument, NULL, OPTION_MMNEMONIC},
13132 {"msyntax", required_argument, NULL, OPTION_MSYNTAX},
13133 {"mindex-reg", no_argument, NULL, OPTION_MINDEX_REG},
13134 {"mnaked-reg", no_argument, NULL, OPTION_MNAKED_REG},
13135 {"msse2avx", no_argument, NULL, OPTION_MSSE2AVX},
13136 {"muse-unaligned-vector-move", no_argument, NULL, OPTION_MUSE_UNALIGNED_VECTOR_MOVE},
13137 {"msse-check", required_argument, NULL, OPTION_MSSE_CHECK},
13138 {"moperand-check", required_argument, NULL, OPTION_MOPERAND_CHECK},
13139 {"mavxscalar", required_argument, NULL, OPTION_MAVXSCALAR},
13140 {"mvexwig", required_argument, NULL, OPTION_MVEXWIG},
13141 {"madd-bnd-prefix", no_argument, NULL, OPTION_MADD_BND_PREFIX},
13142 {"mevexlig", required_argument, NULL, OPTION_MEVEXLIG},
13143 {"mevexwig", required_argument, NULL, OPTION_MEVEXWIG},
13144 # if defined (TE_PE) || defined (TE_PEP)
13145 {"mbig-obj", no_argument, NULL, OPTION_MBIG_OBJ},
13146 #endif
13147 {"momit-lock-prefix", required_argument, NULL, OPTION_MOMIT_LOCK_PREFIX},
13148 {"mfence-as-lock-add", required_argument, NULL, OPTION_MFENCE_AS_LOCK_ADD},
13149 {"mrelax-relocations", required_argument, NULL, OPTION_MRELAX_RELOCATIONS},
13150 {"mevexrcig", required_argument, NULL, OPTION_MEVEXRCIG},
13151 {"malign-branch-boundary", required_argument, NULL, OPTION_MALIGN_BRANCH_BOUNDARY},
13152 {"malign-branch-prefix-size", required_argument, NULL, OPTION_MALIGN_BRANCH_PREFIX_SIZE},
13153 {"malign-branch", required_argument, NULL, OPTION_MALIGN_BRANCH},
13154 {"mbranches-within-32B-boundaries", no_argument, NULL, OPTION_MBRANCHES_WITH_32B_BOUNDARIES},
13155 {"mlfence-after-load", required_argument, NULL, OPTION_MLFENCE_AFTER_LOAD},
13156 {"mlfence-before-indirect-branch", required_argument, NULL,
13157 OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH},
13158 {"mlfence-before-ret", required_argument, NULL, OPTION_MLFENCE_BEFORE_RET},
13159 {"mamd64", no_argument, NULL, OPTION_MAMD64},
13160 {"mintel64", no_argument, NULL, OPTION_MINTEL64},
13161 {NULL, no_argument, NULL, 0}
13162 };
13163 size_t md_longopts_size = sizeof (md_longopts);
13164
13165 int
13166 md_parse_option (int c, const char *arg)
13167 {
13168 unsigned int j;
13169 char *arch, *next, *saved, *type;
13170
13171 switch (c)
13172 {
13173 case 'n':
13174 optimize_align_code = 0;
13175 break;
13176
13177 case 'q':
13178 quiet_warnings = 1;
13179 break;
13180
13181 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13182 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
13183 should be emitted or not. FIXME: Not implemented. */
13184 case 'Q':
13185 if ((arg[0] != 'y' && arg[0] != 'n') || arg[1])
13186 return 0;
13187 break;
13188
13189 /* -V: SVR4 argument to print version ID. */
13190 case 'V':
13191 print_version_id ();
13192 break;
13193
13194 /* -k: Ignore for FreeBSD compatibility. */
13195 case 'k':
13196 break;
13197
13198 case 's':
13199 /* -s: On i386 Solaris, this tells the native assembler to use
13200 .stab instead of .stab.excl. We always use .stab anyhow. */
13201 break;
13202
13203 case OPTION_MSHARED:
13204 shared = 1;
13205 break;
13206
13207 case OPTION_X86_USED_NOTE:
13208 if (strcasecmp (arg, "yes") == 0)
13209 x86_used_note = 1;
13210 else if (strcasecmp (arg, "no") == 0)
13211 x86_used_note = 0;
13212 else
13213 as_fatal (_("invalid -mx86-used-note= option: `%s'"), arg);
13214 break;
13215
13216
13217 #endif
13218 #if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
13219 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
13220 case OPTION_64:
13221 {
13222 const char **list, **l;
13223
13224 list = bfd_target_list ();
13225 for (l = list; *l != NULL; l++)
13226 if (startswith (*l, "elf64-x86-64")
13227 || strcmp (*l, "coff-x86-64") == 0
13228 || strcmp (*l, "pe-x86-64") == 0
13229 || strcmp (*l, "pei-x86-64") == 0
13230 || strcmp (*l, "mach-o-x86-64") == 0)
13231 {
13232 default_arch = "x86_64";
13233 break;
13234 }
13235 if (*l == NULL)
13236 as_fatal (_("no compiled in support for x86_64"));
13237 free (list);
13238 }
13239 break;
13240 #endif
13241
13242 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13243 case OPTION_X32:
13244 if (IS_ELF)
13245 {
13246 const char **list, **l;
13247
13248 list = bfd_target_list ();
13249 for (l = list; *l != NULL; l++)
13250 if (startswith (*l, "elf32-x86-64"))
13251 {
13252 default_arch = "x86_64:32";
13253 break;
13254 }
13255 if (*l == NULL)
13256 as_fatal (_("no compiled in support for 32bit x86_64"));
13257 free (list);
13258 }
13259 else
13260 as_fatal (_("32bit x86_64 is only supported for ELF"));
13261 break;
13262 #endif
13263
13264 case OPTION_32:
13265 default_arch = "i386";
13266 break;
13267
13268 case OPTION_DIVIDE:
13269 #ifdef SVR4_COMMENT_CHARS
13270 {
13271 char *n, *t;
13272 const char *s;
13273
13274 n = XNEWVEC (char, strlen (i386_comment_chars) + 1);
13275 t = n;
13276 for (s = i386_comment_chars; *s != '\0'; s++)
13277 if (*s != '/')
13278 *t++ = *s;
13279 *t = '\0';
13280 i386_comment_chars = n;
13281 }
13282 #endif
13283 break;
13284
13285 case OPTION_MARCH:
13286 saved = xstrdup (arg);
13287 arch = saved;
13288 /* Allow -march=+nosse. */
13289 if (*arch == '+')
13290 arch++;
13291 do
13292 {
13293 if (*arch == '.')
13294 as_fatal (_("invalid -march= option: `%s'"), arg);
13295 next = strchr (arch, '+');
13296 if (next)
13297 *next++ = '\0';
13298 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13299 {
13300 if (strcmp (arch, cpu_arch [j].name) == 0)
13301 {
13302 /* Processor. */
13303 if (! cpu_arch[j].flags.bitfield.cpui386)
13304 continue;
13305
13306 cpu_arch_name = cpu_arch[j].name;
13307 cpu_sub_arch_name = NULL;
13308 cpu_arch_flags = cpu_arch[j].flags;
13309 cpu_arch_isa = cpu_arch[j].type;
13310 cpu_arch_isa_flags = cpu_arch[j].flags;
13311 if (!cpu_arch_tune_set)
13312 {
13313 cpu_arch_tune = cpu_arch_isa;
13314 cpu_arch_tune_flags = cpu_arch_isa_flags;
13315 }
13316 break;
13317 }
13318 else if (*cpu_arch [j].name == '.'
13319 && strcmp (arch, cpu_arch [j].name + 1) == 0)
13320 {
13321 /* ISA extension. */
13322 i386_cpu_flags flags;
13323
13324 flags = cpu_flags_or (cpu_arch_flags,
13325 cpu_arch[j].flags);
13326
13327 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
13328 {
13329 if (cpu_sub_arch_name)
13330 {
13331 char *name = cpu_sub_arch_name;
13332 cpu_sub_arch_name = concat (name,
13333 cpu_arch[j].name,
13334 (const char *) NULL);
13335 free (name);
13336 }
13337 else
13338 cpu_sub_arch_name = xstrdup (cpu_arch[j].name);
13339 cpu_arch_flags = flags;
13340 cpu_arch_isa_flags = flags;
13341 }
13342 else
13343 cpu_arch_isa_flags
13344 = cpu_flags_or (cpu_arch_isa_flags,
13345 cpu_arch[j].flags);
13346 break;
13347 }
13348 }
13349
13350 if (j >= ARRAY_SIZE (cpu_arch))
13351 {
13352 /* Disable an ISA extension. */
13353 for (j = 0; j < ARRAY_SIZE (cpu_noarch); j++)
13354 if (strcmp (arch, cpu_noarch [j].name) == 0)
13355 {
13356 i386_cpu_flags flags;
13357
13358 flags = cpu_flags_and_not (cpu_arch_flags,
13359 cpu_noarch[j].flags);
13360 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
13361 {
13362 if (cpu_sub_arch_name)
13363 {
13364 char *name = cpu_sub_arch_name;
13365 cpu_sub_arch_name = concat (arch,
13366 (const char *) NULL);
13367 free (name);
13368 }
13369 else
13370 cpu_sub_arch_name = xstrdup (arch);
13371 cpu_arch_flags = flags;
13372 cpu_arch_isa_flags = flags;
13373 }
13374 break;
13375 }
13376
13377 if (j >= ARRAY_SIZE (cpu_noarch))
13378 j = ARRAY_SIZE (cpu_arch);
13379 }
13380
13381 if (j >= ARRAY_SIZE (cpu_arch))
13382 as_fatal (_("invalid -march= option: `%s'"), arg);
13383
13384 arch = next;
13385 }
13386 while (next != NULL);
13387 free (saved);
13388 break;
13389
13390 case OPTION_MTUNE:
13391 if (*arg == '.')
13392 as_fatal (_("invalid -mtune= option: `%s'"), arg);
13393 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13394 {
13395 if (strcmp (arg, cpu_arch [j].name) == 0)
13396 {
13397 cpu_arch_tune_set = 1;
13398 cpu_arch_tune = cpu_arch [j].type;
13399 cpu_arch_tune_flags = cpu_arch[j].flags;
13400 break;
13401 }
13402 }
13403 if (j >= ARRAY_SIZE (cpu_arch))
13404 as_fatal (_("invalid -mtune= option: `%s'"), arg);
13405 break;
13406
13407 case OPTION_MMNEMONIC:
13408 if (strcasecmp (arg, "att") == 0)
13409 intel_mnemonic = 0;
13410 else if (strcasecmp (arg, "intel") == 0)
13411 intel_mnemonic = 1;
13412 else
13413 as_fatal (_("invalid -mmnemonic= option: `%s'"), arg);
13414 break;
13415
13416 case OPTION_MSYNTAX:
13417 if (strcasecmp (arg, "att") == 0)
13418 intel_syntax = 0;
13419 else if (strcasecmp (arg, "intel") == 0)
13420 intel_syntax = 1;
13421 else
13422 as_fatal (_("invalid -msyntax= option: `%s'"), arg);
13423 break;
13424
13425 case OPTION_MINDEX_REG:
13426 allow_index_reg = 1;
13427 break;
13428
13429 case OPTION_MNAKED_REG:
13430 allow_naked_reg = 1;
13431 break;
13432
13433 case OPTION_MSSE2AVX:
13434 sse2avx = 1;
13435 break;
13436
13437 case OPTION_MUSE_UNALIGNED_VECTOR_MOVE:
13438 use_unaligned_vector_move = 1;
13439 break;
13440
13441 case OPTION_MSSE_CHECK:
13442 if (strcasecmp (arg, "error") == 0)
13443 sse_check = check_error;
13444 else if (strcasecmp (arg, "warning") == 0)
13445 sse_check = check_warning;
13446 else if (strcasecmp (arg, "none") == 0)
13447 sse_check = check_none;
13448 else
13449 as_fatal (_("invalid -msse-check= option: `%s'"), arg);
13450 break;
13451
13452 case OPTION_MOPERAND_CHECK:
13453 if (strcasecmp (arg, "error") == 0)
13454 operand_check = check_error;
13455 else if (strcasecmp (arg, "warning") == 0)
13456 operand_check = check_warning;
13457 else if (strcasecmp (arg, "none") == 0)
13458 operand_check = check_none;
13459 else
13460 as_fatal (_("invalid -moperand-check= option: `%s'"), arg);
13461 break;
13462
13463 case OPTION_MAVXSCALAR:
13464 if (strcasecmp (arg, "128") == 0)
13465 avxscalar = vex128;
13466 else if (strcasecmp (arg, "256") == 0)
13467 avxscalar = vex256;
13468 else
13469 as_fatal (_("invalid -mavxscalar= option: `%s'"), arg);
13470 break;
13471
13472 case OPTION_MVEXWIG:
13473 if (strcmp (arg, "0") == 0)
13474 vexwig = vexw0;
13475 else if (strcmp (arg, "1") == 0)
13476 vexwig = vexw1;
13477 else
13478 as_fatal (_("invalid -mvexwig= option: `%s'"), arg);
13479 break;
13480
13481 case OPTION_MADD_BND_PREFIX:
13482 add_bnd_prefix = 1;
13483 break;
13484
13485 case OPTION_MEVEXLIG:
13486 if (strcmp (arg, "128") == 0)
13487 evexlig = evexl128;
13488 else if (strcmp (arg, "256") == 0)
13489 evexlig = evexl256;
13490 else if (strcmp (arg, "512") == 0)
13491 evexlig = evexl512;
13492 else
13493 as_fatal (_("invalid -mevexlig= option: `%s'"), arg);
13494 break;
13495
13496 case OPTION_MEVEXRCIG:
13497 if (strcmp (arg, "rne") == 0)
13498 evexrcig = rne;
13499 else if (strcmp (arg, "rd") == 0)
13500 evexrcig = rd;
13501 else if (strcmp (arg, "ru") == 0)
13502 evexrcig = ru;
13503 else if (strcmp (arg, "rz") == 0)
13504 evexrcig = rz;
13505 else
13506 as_fatal (_("invalid -mevexrcig= option: `%s'"), arg);
13507 break;
13508
13509 case OPTION_MEVEXWIG:
13510 if (strcmp (arg, "0") == 0)
13511 evexwig = evexw0;
13512 else if (strcmp (arg, "1") == 0)
13513 evexwig = evexw1;
13514 else
13515 as_fatal (_("invalid -mevexwig= option: `%s'"), arg);
13516 break;
13517
13518 # if defined (TE_PE) || defined (TE_PEP)
13519 case OPTION_MBIG_OBJ:
13520 use_big_obj = 1;
13521 break;
13522 #endif
13523
13524 case OPTION_MOMIT_LOCK_PREFIX:
13525 if (strcasecmp (arg, "yes") == 0)
13526 omit_lock_prefix = 1;
13527 else if (strcasecmp (arg, "no") == 0)
13528 omit_lock_prefix = 0;
13529 else
13530 as_fatal (_("invalid -momit-lock-prefix= option: `%s'"), arg);
13531 break;
13532
13533 case OPTION_MFENCE_AS_LOCK_ADD:
13534 if (strcasecmp (arg, "yes") == 0)
13535 avoid_fence = 1;
13536 else if (strcasecmp (arg, "no") == 0)
13537 avoid_fence = 0;
13538 else
13539 as_fatal (_("invalid -mfence-as-lock-add= option: `%s'"), arg);
13540 break;
13541
13542 case OPTION_MLFENCE_AFTER_LOAD:
13543 if (strcasecmp (arg, "yes") == 0)
13544 lfence_after_load = 1;
13545 else if (strcasecmp (arg, "no") == 0)
13546 lfence_after_load = 0;
13547 else
13548 as_fatal (_("invalid -mlfence-after-load= option: `%s'"), arg);
13549 break;
13550
13551 case OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH:
13552 if (strcasecmp (arg, "all") == 0)
13553 {
13554 lfence_before_indirect_branch = lfence_branch_all;
13555 if (lfence_before_ret == lfence_before_ret_none)
13556 lfence_before_ret = lfence_before_ret_shl;
13557 }
13558 else if (strcasecmp (arg, "memory") == 0)
13559 lfence_before_indirect_branch = lfence_branch_memory;
13560 else if (strcasecmp (arg, "register") == 0)
13561 lfence_before_indirect_branch = lfence_branch_register;
13562 else if (strcasecmp (arg, "none") == 0)
13563 lfence_before_indirect_branch = lfence_branch_none;
13564 else
13565 as_fatal (_("invalid -mlfence-before-indirect-branch= option: `%s'"),
13566 arg);
13567 break;
13568
13569 case OPTION_MLFENCE_BEFORE_RET:
13570 if (strcasecmp (arg, "or") == 0)
13571 lfence_before_ret = lfence_before_ret_or;
13572 else if (strcasecmp (arg, "not") == 0)
13573 lfence_before_ret = lfence_before_ret_not;
13574 else if (strcasecmp (arg, "shl") == 0 || strcasecmp (arg, "yes") == 0)
13575 lfence_before_ret = lfence_before_ret_shl;
13576 else if (strcasecmp (arg, "none") == 0)
13577 lfence_before_ret = lfence_before_ret_none;
13578 else
13579 as_fatal (_("invalid -mlfence-before-ret= option: `%s'"),
13580 arg);
13581 break;
13582
13583 case OPTION_MRELAX_RELOCATIONS:
13584 if (strcasecmp (arg, "yes") == 0)
13585 generate_relax_relocations = 1;
13586 else if (strcasecmp (arg, "no") == 0)
13587 generate_relax_relocations = 0;
13588 else
13589 as_fatal (_("invalid -mrelax-relocations= option: `%s'"), arg);
13590 break;
13591
13592 case OPTION_MALIGN_BRANCH_BOUNDARY:
13593 {
13594 char *end;
13595 long int align = strtoul (arg, &end, 0);
13596 if (*end == '\0')
13597 {
13598 if (align == 0)
13599 {
13600 align_branch_power = 0;
13601 break;
13602 }
13603 else if (align >= 16)
13604 {
13605 int align_power;
13606 for (align_power = 0;
13607 (align & 1) == 0;
13608 align >>= 1, align_power++)
13609 continue;
13610 /* Limit alignment power to 31. */
13611 if (align == 1 && align_power < 32)
13612 {
13613 align_branch_power = align_power;
13614 break;
13615 }
13616 }
13617 }
13618 as_fatal (_("invalid -malign-branch-boundary= value: %s"), arg);
13619 }
13620 break;
13621
13622 case OPTION_MALIGN_BRANCH_PREFIX_SIZE:
13623 {
13624 char *end;
13625 int align = strtoul (arg, &end, 0);
13626 /* Some processors only support 5 prefixes. */
13627 if (*end == '\0' && align >= 0 && align < 6)
13628 {
13629 align_branch_prefix_size = align;
13630 break;
13631 }
13632 as_fatal (_("invalid -malign-branch-prefix-size= value: %s"),
13633 arg);
13634 }
13635 break;
13636
13637 case OPTION_MALIGN_BRANCH:
13638 align_branch = 0;
13639 saved = xstrdup (arg);
13640 type = saved;
13641 do
13642 {
13643 next = strchr (type, '+');
13644 if (next)
13645 *next++ = '\0';
13646 if (strcasecmp (type, "jcc") == 0)
13647 align_branch |= align_branch_jcc_bit;
13648 else if (strcasecmp (type, "fused") == 0)
13649 align_branch |= align_branch_fused_bit;
13650 else if (strcasecmp (type, "jmp") == 0)
13651 align_branch |= align_branch_jmp_bit;
13652 else if (strcasecmp (type, "call") == 0)
13653 align_branch |= align_branch_call_bit;
13654 else if (strcasecmp (type, "ret") == 0)
13655 align_branch |= align_branch_ret_bit;
13656 else if (strcasecmp (type, "indirect") == 0)
13657 align_branch |= align_branch_indirect_bit;
13658 else
13659 as_fatal (_("invalid -malign-branch= option: `%s'"), arg);
13660 type = next;
13661 }
13662 while (next != NULL);
13663 free (saved);
13664 break;
13665
13666 case OPTION_MBRANCHES_WITH_32B_BOUNDARIES:
13667 align_branch_power = 5;
13668 align_branch_prefix_size = 5;
13669 align_branch = (align_branch_jcc_bit
13670 | align_branch_fused_bit
13671 | align_branch_jmp_bit);
13672 break;
13673
13674 case OPTION_MAMD64:
13675 isa64 = amd64;
13676 break;
13677
13678 case OPTION_MINTEL64:
13679 isa64 = intel64;
13680 break;
13681
13682 case 'O':
13683 if (arg == NULL)
13684 {
13685 optimize = 1;
13686 /* Turn off -Os. */
13687 optimize_for_space = 0;
13688 }
13689 else if (*arg == 's')
13690 {
13691 optimize_for_space = 1;
13692 /* Turn on all encoding optimizations. */
13693 optimize = INT_MAX;
13694 }
13695 else
13696 {
13697 optimize = atoi (arg);
13698 /* Turn off -Os. */
13699 optimize_for_space = 0;
13700 }
13701 break;
13702
13703 default:
13704 return 0;
13705 }
13706 return 1;
13707 }
13708
13709 #define MESSAGE_TEMPLATE \
13710 " "
13711
13712 static char *
13713 output_message (FILE *stream, char *p, char *message, char *start,
13714 int *left_p, const char *name, int len)
13715 {
13716 int size = sizeof (MESSAGE_TEMPLATE);
13717 int left = *left_p;
13718
13719 /* Reserve 2 spaces for ", " or ",\0" */
13720 left -= len + 2;
13721
13722 /* Check if there is any room. */
13723 if (left >= 0)
13724 {
13725 if (p != start)
13726 {
13727 *p++ = ',';
13728 *p++ = ' ';
13729 }
13730 p = mempcpy (p, name, len);
13731 }
13732 else
13733 {
13734 /* Output the current message now and start a new one. */
13735 *p++ = ',';
13736 *p = '\0';
13737 fprintf (stream, "%s\n", message);
13738 p = start;
13739 left = size - (start - message) - len - 2;
13740
13741 gas_assert (left >= 0);
13742
13743 p = mempcpy (p, name, len);
13744 }
13745
13746 *left_p = left;
13747 return p;
13748 }
13749
13750 static void
13751 show_arch (FILE *stream, int ext, int check)
13752 {
13753 static char message[] = MESSAGE_TEMPLATE;
13754 char *start = message + 27;
13755 char *p;
13756 int size = sizeof (MESSAGE_TEMPLATE);
13757 int left;
13758 const char *name;
13759 int len;
13760 unsigned int j;
13761
13762 p = start;
13763 left = size - (start - message);
13764 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13765 {
13766 /* Should it be skipped? */
13767 if (cpu_arch [j].skip)
13768 continue;
13769
13770 name = cpu_arch [j].name;
13771 len = cpu_arch [j].len;
13772 if (*name == '.')
13773 {
13774 /* It is an extension. Skip if we aren't asked to show it. */
13775 if (ext)
13776 {
13777 name++;
13778 len--;
13779 }
13780 else
13781 continue;
13782 }
13783 else if (ext)
13784 {
13785 /* It is an processor. Skip if we show only extension. */
13786 continue;
13787 }
13788 else if (check && ! cpu_arch[j].flags.bitfield.cpui386)
13789 {
13790 /* It is an impossible processor - skip. */
13791 continue;
13792 }
13793
13794 p = output_message (stream, p, message, start, &left, name, len);
13795 }
13796
13797 /* Display disabled extensions. */
13798 if (ext)
13799 for (j = 0; j < ARRAY_SIZE (cpu_noarch); j++)
13800 {
13801 name = cpu_noarch [j].name;
13802 len = cpu_noarch [j].len;
13803 p = output_message (stream, p, message, start, &left, name,
13804 len);
13805 }
13806
13807 *p = '\0';
13808 fprintf (stream, "%s\n", message);
13809 }
13810
13811 void
13812 md_show_usage (FILE *stream)
13813 {
13814 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13815 fprintf (stream, _("\
13816 -Qy, -Qn ignored\n\
13817 -V print assembler version number\n\
13818 -k ignored\n"));
13819 #endif
13820 fprintf (stream, _("\
13821 -n Do not optimize code alignment\n\
13822 -q quieten some warnings\n"));
13823 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13824 fprintf (stream, _("\
13825 -s ignored\n"));
13826 #endif
13827 #ifdef BFD64
13828 # if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13829 fprintf (stream, _("\
13830 --32/--64/--x32 generate 32bit/64bit/x32 object\n"));
13831 # elif defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O)
13832 fprintf (stream, _("\
13833 --32/--64 generate 32bit/64bit object\n"));
13834 # endif
13835 #endif
13836 #ifdef SVR4_COMMENT_CHARS
13837 fprintf (stream, _("\
13838 --divide do not treat `/' as a comment character\n"));
13839 #else
13840 fprintf (stream, _("\
13841 --divide ignored\n"));
13842 #endif
13843 fprintf (stream, _("\
13844 -march=CPU[,+EXTENSION...]\n\
13845 generate code for CPU and EXTENSION, CPU is one of:\n"));
13846 show_arch (stream, 0, 1);
13847 fprintf (stream, _("\
13848 EXTENSION is combination of:\n"));
13849 show_arch (stream, 1, 0);
13850 fprintf (stream, _("\
13851 -mtune=CPU optimize for CPU, CPU is one of:\n"));
13852 show_arch (stream, 0, 0);
13853 fprintf (stream, _("\
13854 -msse2avx encode SSE instructions with VEX prefix\n"));
13855 fprintf (stream, _("\
13856 -muse-unaligned-vector-move\n\
13857 encode aligned vector move as unaligned vector move\n"));
13858 fprintf (stream, _("\
13859 -msse-check=[none|error|warning] (default: warning)\n\
13860 check SSE instructions\n"));
13861 fprintf (stream, _("\
13862 -moperand-check=[none|error|warning] (default: warning)\n\
13863 check operand combinations for validity\n"));
13864 fprintf (stream, _("\
13865 -mavxscalar=[128|256] (default: 128)\n\
13866 encode scalar AVX instructions with specific vector\n\
13867 length\n"));
13868 fprintf (stream, _("\
13869 -mvexwig=[0|1] (default: 0)\n\
13870 encode VEX instructions with specific VEX.W value\n\
13871 for VEX.W bit ignored instructions\n"));
13872 fprintf (stream, _("\
13873 -mevexlig=[128|256|512] (default: 128)\n\
13874 encode scalar EVEX instructions with specific vector\n\
13875 length\n"));
13876 fprintf (stream, _("\
13877 -mevexwig=[0|1] (default: 0)\n\
13878 encode EVEX instructions with specific EVEX.W value\n\
13879 for EVEX.W bit ignored instructions\n"));
13880 fprintf (stream, _("\
13881 -mevexrcig=[rne|rd|ru|rz] (default: rne)\n\
13882 encode EVEX instructions with specific EVEX.RC value\n\
13883 for SAE-only ignored instructions\n"));
13884 fprintf (stream, _("\
13885 -mmnemonic=[att|intel] "));
13886 if (SYSV386_COMPAT)
13887 fprintf (stream, _("(default: att)\n"));
13888 else
13889 fprintf (stream, _("(default: intel)\n"));
13890 fprintf (stream, _("\
13891 use AT&T/Intel mnemonic\n"));
13892 fprintf (stream, _("\
13893 -msyntax=[att|intel] (default: att)\n\
13894 use AT&T/Intel syntax\n"));
13895 fprintf (stream, _("\
13896 -mindex-reg support pseudo index registers\n"));
13897 fprintf (stream, _("\
13898 -mnaked-reg don't require `%%' prefix for registers\n"));
13899 fprintf (stream, _("\
13900 -madd-bnd-prefix add BND prefix for all valid branches\n"));
13901 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13902 fprintf (stream, _("\
13903 -mshared disable branch optimization for shared code\n"));
13904 fprintf (stream, _("\
13905 -mx86-used-note=[no|yes] "));
13906 if (DEFAULT_X86_USED_NOTE)
13907 fprintf (stream, _("(default: yes)\n"));
13908 else
13909 fprintf (stream, _("(default: no)\n"));
13910 fprintf (stream, _("\
13911 generate x86 used ISA and feature properties\n"));
13912 #endif
13913 #if defined (TE_PE) || defined (TE_PEP)
13914 fprintf (stream, _("\
13915 -mbig-obj generate big object files\n"));
13916 #endif
13917 fprintf (stream, _("\
13918 -momit-lock-prefix=[no|yes] (default: no)\n\
13919 strip all lock prefixes\n"));
13920 fprintf (stream, _("\
13921 -mfence-as-lock-add=[no|yes] (default: no)\n\
13922 encode lfence, mfence and sfence as\n\
13923 lock addl $0x0, (%%{re}sp)\n"));
13924 fprintf (stream, _("\
13925 -mrelax-relocations=[no|yes] "));
13926 if (DEFAULT_GENERATE_X86_RELAX_RELOCATIONS)
13927 fprintf (stream, _("(default: yes)\n"));
13928 else
13929 fprintf (stream, _("(default: no)\n"));
13930 fprintf (stream, _("\
13931 generate relax relocations\n"));
13932 fprintf (stream, _("\
13933 -malign-branch-boundary=NUM (default: 0)\n\
13934 align branches within NUM byte boundary\n"));
13935 fprintf (stream, _("\
13936 -malign-branch=TYPE[+TYPE...] (default: jcc+fused+jmp)\n\
13937 TYPE is combination of jcc, fused, jmp, call, ret,\n\
13938 indirect\n\
13939 specify types of branches to align\n"));
13940 fprintf (stream, _("\
13941 -malign-branch-prefix-size=NUM (default: 5)\n\
13942 align branches with NUM prefixes per instruction\n"));
13943 fprintf (stream, _("\
13944 -mbranches-within-32B-boundaries\n\
13945 align branches within 32 byte boundary\n"));
13946 fprintf (stream, _("\
13947 -mlfence-after-load=[no|yes] (default: no)\n\
13948 generate lfence after load\n"));
13949 fprintf (stream, _("\
13950 -mlfence-before-indirect-branch=[none|all|register|memory] (default: none)\n\
13951 generate lfence before indirect near branch\n"));
13952 fprintf (stream, _("\
13953 -mlfence-before-ret=[none|or|not|shl|yes] (default: none)\n\
13954 generate lfence before ret\n"));
13955 fprintf (stream, _("\
13956 -mamd64 accept only AMD64 ISA [default]\n"));
13957 fprintf (stream, _("\
13958 -mintel64 accept only Intel64 ISA\n"));
13959 }
13960
13961 #if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
13962 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
13963 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
13964
13965 /* Pick the target format to use. */
13966
13967 const char *
13968 i386_target_format (void)
13969 {
13970 if (startswith (default_arch, "x86_64"))
13971 {
13972 update_code_flag (CODE_64BIT, 1);
13973 if (default_arch[6] == '\0')
13974 x86_elf_abi = X86_64_ABI;
13975 else
13976 x86_elf_abi = X86_64_X32_ABI;
13977 }
13978 else if (!strcmp (default_arch, "i386"))
13979 update_code_flag (CODE_32BIT, 1);
13980 else if (!strcmp (default_arch, "iamcu"))
13981 {
13982 update_code_flag (CODE_32BIT, 1);
13983 if (cpu_arch_isa == PROCESSOR_UNKNOWN)
13984 {
13985 static const i386_cpu_flags iamcu_flags = CPU_IAMCU_FLAGS;
13986 cpu_arch_name = "iamcu";
13987 cpu_sub_arch_name = NULL;
13988 cpu_arch_flags = iamcu_flags;
13989 cpu_arch_isa = PROCESSOR_IAMCU;
13990 cpu_arch_isa_flags = iamcu_flags;
13991 if (!cpu_arch_tune_set)
13992 {
13993 cpu_arch_tune = cpu_arch_isa;
13994 cpu_arch_tune_flags = cpu_arch_isa_flags;
13995 }
13996 }
13997 else if (cpu_arch_isa != PROCESSOR_IAMCU)
13998 as_fatal (_("Intel MCU doesn't support `%s' architecture"),
13999 cpu_arch_name);
14000 }
14001 else
14002 as_fatal (_("unknown architecture"));
14003
14004 if (cpu_flags_all_zero (&cpu_arch_isa_flags))
14005 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].flags;
14006 if (cpu_flags_all_zero (&cpu_arch_tune_flags))
14007 cpu_arch_tune_flags = cpu_arch[flag_code == CODE_64BIT].flags;
14008
14009 switch (OUTPUT_FLAVOR)
14010 {
14011 #if defined (OBJ_MAYBE_AOUT) || defined (OBJ_AOUT)
14012 case bfd_target_aout_flavour:
14013 return AOUT_TARGET_FORMAT;
14014 #endif
14015 #if defined (OBJ_MAYBE_COFF) || defined (OBJ_COFF)
14016 # if defined (TE_PE) || defined (TE_PEP)
14017 case bfd_target_coff_flavour:
14018 if (flag_code == CODE_64BIT)
14019 {
14020 object_64bit = 1;
14021 return use_big_obj ? "pe-bigobj-x86-64" : "pe-x86-64";
14022 }
14023 return use_big_obj ? "pe-bigobj-i386" : "pe-i386";
14024 # elif defined (TE_GO32)
14025 case bfd_target_coff_flavour:
14026 return "coff-go32";
14027 # else
14028 case bfd_target_coff_flavour:
14029 return "coff-i386";
14030 # endif
14031 #endif
14032 #if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
14033 case bfd_target_elf_flavour:
14034 {
14035 const char *format;
14036
14037 switch (x86_elf_abi)
14038 {
14039 default:
14040 format = ELF_TARGET_FORMAT;
14041 #ifndef TE_SOLARIS
14042 tls_get_addr = "___tls_get_addr";
14043 #endif
14044 break;
14045 case X86_64_ABI:
14046 use_rela_relocations = 1;
14047 object_64bit = 1;
14048 #ifndef TE_SOLARIS
14049 tls_get_addr = "__tls_get_addr";
14050 #endif
14051 format = ELF_TARGET_FORMAT64;
14052 break;
14053 case X86_64_X32_ABI:
14054 use_rela_relocations = 1;
14055 object_64bit = 1;
14056 #ifndef TE_SOLARIS
14057 tls_get_addr = "__tls_get_addr";
14058 #endif
14059 disallow_64bit_reloc = 1;
14060 format = ELF_TARGET_FORMAT32;
14061 break;
14062 }
14063 if (cpu_arch_isa == PROCESSOR_L1OM)
14064 {
14065 if (x86_elf_abi != X86_64_ABI)
14066 as_fatal (_("Intel L1OM is 64bit only"));
14067 return ELF_TARGET_L1OM_FORMAT;
14068 }
14069 else if (cpu_arch_isa == PROCESSOR_K1OM)
14070 {
14071 if (x86_elf_abi != X86_64_ABI)
14072 as_fatal (_("Intel K1OM is 64bit only"));
14073 return ELF_TARGET_K1OM_FORMAT;
14074 }
14075 else if (cpu_arch_isa == PROCESSOR_IAMCU)
14076 {
14077 if (x86_elf_abi != I386_ABI)
14078 as_fatal (_("Intel MCU is 32bit only"));
14079 return ELF_TARGET_IAMCU_FORMAT;
14080 }
14081 else
14082 return format;
14083 }
14084 #endif
14085 #if defined (OBJ_MACH_O)
14086 case bfd_target_mach_o_flavour:
14087 if (flag_code == CODE_64BIT)
14088 {
14089 use_rela_relocations = 1;
14090 object_64bit = 1;
14091 return "mach-o-x86-64";
14092 }
14093 else
14094 return "mach-o-i386";
14095 #endif
14096 default:
14097 abort ();
14098 return NULL;
14099 }
14100 }
14101
14102 #endif /* OBJ_MAYBE_ more than one */
14103 \f
14104 symbolS *
14105 md_undefined_symbol (char *name)
14106 {
14107 if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
14108 && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
14109 && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
14110 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
14111 {
14112 if (!GOT_symbol)
14113 {
14114 if (symbol_find (name))
14115 as_bad (_("GOT already in symbol table"));
14116 GOT_symbol = symbol_new (name, undefined_section,
14117 &zero_address_frag, 0);
14118 };
14119 return GOT_symbol;
14120 }
14121 return 0;
14122 }
14123
14124 /* Round up a section size to the appropriate boundary. */
14125
14126 valueT
14127 md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
14128 {
14129 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
14130 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
14131 {
14132 /* For a.out, force the section size to be aligned. If we don't do
14133 this, BFD will align it for us, but it will not write out the
14134 final bytes of the section. This may be a bug in BFD, but it is
14135 easier to fix it here since that is how the other a.out targets
14136 work. */
14137 int align;
14138
14139 align = bfd_section_alignment (segment);
14140 size = ((size + (1 << align) - 1) & (-((valueT) 1 << align)));
14141 }
14142 #endif
14143
14144 return size;
14145 }
14146
14147 /* On the i386, PC-relative offsets are relative to the start of the
14148 next instruction. That is, the address of the offset, plus its
14149 size, since the offset is always the last part of the insn. */
14150
14151 long
14152 md_pcrel_from (fixS *fixP)
14153 {
14154 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
14155 }
14156
14157 #ifndef I386COFF
14158
14159 static void
14160 s_bss (int ignore ATTRIBUTE_UNUSED)
14161 {
14162 int temp;
14163
14164 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14165 if (IS_ELF)
14166 obj_elf_section_change_hook ();
14167 #endif
14168 temp = get_absolute_expression ();
14169 subseg_set (bss_section, (subsegT) temp);
14170 demand_empty_rest_of_line ();
14171 }
14172
14173 #endif
14174
14175 /* Remember constant directive. */
14176
14177 void
14178 i386_cons_align (int ignore ATTRIBUTE_UNUSED)
14179 {
14180 if (last_insn.kind != last_insn_directive
14181 && (bfd_section_flags (now_seg) & SEC_CODE))
14182 {
14183 last_insn.seg = now_seg;
14184 last_insn.kind = last_insn_directive;
14185 last_insn.name = "constant directive";
14186 last_insn.file = as_where (&last_insn.line);
14187 if (lfence_before_ret != lfence_before_ret_none)
14188 {
14189 if (lfence_before_indirect_branch != lfence_branch_none)
14190 as_warn (_("constant directive skips -mlfence-before-ret "
14191 "and -mlfence-before-indirect-branch"));
14192 else
14193 as_warn (_("constant directive skips -mlfence-before-ret"));
14194 }
14195 else if (lfence_before_indirect_branch != lfence_branch_none)
14196 as_warn (_("constant directive skips -mlfence-before-indirect-branch"));
14197 }
14198 }
14199
14200 int
14201 i386_validate_fix (fixS *fixp)
14202 {
14203 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14204 if (fixp->fx_r_type == BFD_RELOC_SIZE32
14205 || fixp->fx_r_type == BFD_RELOC_SIZE64)
14206 return IS_ELF && fixp->fx_addsy
14207 && (!S_IS_DEFINED (fixp->fx_addsy)
14208 || S_IS_EXTERNAL (fixp->fx_addsy));
14209 #endif
14210
14211 if (fixp->fx_subsy)
14212 {
14213 if (fixp->fx_subsy == GOT_symbol)
14214 {
14215 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
14216 {
14217 if (!object_64bit)
14218 abort ();
14219 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14220 if (fixp->fx_tcbit2)
14221 fixp->fx_r_type = (fixp->fx_tcbit
14222 ? BFD_RELOC_X86_64_REX_GOTPCRELX
14223 : BFD_RELOC_X86_64_GOTPCRELX);
14224 else
14225 #endif
14226 fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
14227 }
14228 else
14229 {
14230 if (!object_64bit)
14231 fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
14232 else
14233 fixp->fx_r_type = BFD_RELOC_X86_64_GOTOFF64;
14234 }
14235 fixp->fx_subsy = 0;
14236 }
14237 }
14238 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14239 else
14240 {
14241 /* NB: Commit 292676c1 resolved PLT32 reloc aganst local symbol
14242 to section. Since PLT32 relocation must be against symbols,
14243 turn such PLT32 relocation into PC32 relocation. */
14244 if (fixp->fx_addsy
14245 && (fixp->fx_r_type == BFD_RELOC_386_PLT32
14246 || fixp->fx_r_type == BFD_RELOC_X86_64_PLT32)
14247 && symbol_section_p (fixp->fx_addsy))
14248 fixp->fx_r_type = BFD_RELOC_32_PCREL;
14249 if (!object_64bit)
14250 {
14251 if (fixp->fx_r_type == BFD_RELOC_386_GOT32
14252 && fixp->fx_tcbit2)
14253 fixp->fx_r_type = BFD_RELOC_386_GOT32X;
14254 }
14255 }
14256 #endif
14257
14258 return 1;
14259 }
14260
14261 arelent *
14262 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
14263 {
14264 arelent *rel;
14265 bfd_reloc_code_real_type code;
14266
14267 switch (fixp->fx_r_type)
14268 {
14269 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14270 symbolS *sym;
14271
14272 case BFD_RELOC_SIZE32:
14273 case BFD_RELOC_SIZE64:
14274 if (fixp->fx_addsy
14275 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))
14276 && (!fixp->fx_subsy
14277 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))))
14278 sym = fixp->fx_addsy;
14279 else if (fixp->fx_subsy
14280 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))
14281 && (!fixp->fx_addsy
14282 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))))
14283 sym = fixp->fx_subsy;
14284 else
14285 sym = NULL;
14286 if (IS_ELF && sym && S_IS_DEFINED (sym) && !S_IS_EXTERNAL (sym))
14287 {
14288 /* Resolve size relocation against local symbol to size of
14289 the symbol plus addend. */
14290 valueT value = S_GET_SIZE (sym);
14291
14292 if (symbol_get_bfdsym (sym)->flags & BSF_SECTION_SYM)
14293 value = bfd_section_size (S_GET_SEGMENT (sym));
14294 if (sym == fixp->fx_subsy)
14295 {
14296 value = -value;
14297 if (fixp->fx_addsy)
14298 value += S_GET_VALUE (fixp->fx_addsy);
14299 }
14300 else if (fixp->fx_subsy)
14301 value -= S_GET_VALUE (fixp->fx_subsy);
14302 value += fixp->fx_offset;
14303 if (fixp->fx_r_type == BFD_RELOC_SIZE32
14304 && object_64bit
14305 && !fits_in_unsigned_long (value))
14306 as_bad_where (fixp->fx_file, fixp->fx_line,
14307 _("symbol size computation overflow"));
14308 fixp->fx_addsy = NULL;
14309 fixp->fx_subsy = NULL;
14310 md_apply_fix (fixp, (valueT *) &value, NULL);
14311 return NULL;
14312 }
14313 if (!fixp->fx_addsy || fixp->fx_subsy)
14314 {
14315 as_bad_where (fixp->fx_file, fixp->fx_line,
14316 "unsupported expression involving @size");
14317 return NULL;
14318 }
14319 #endif
14320 /* Fall through. */
14321
14322 case BFD_RELOC_X86_64_PLT32:
14323 case BFD_RELOC_X86_64_GOT32:
14324 case BFD_RELOC_X86_64_GOTPCREL:
14325 case BFD_RELOC_X86_64_GOTPCRELX:
14326 case BFD_RELOC_X86_64_REX_GOTPCRELX:
14327 case BFD_RELOC_386_PLT32:
14328 case BFD_RELOC_386_GOT32:
14329 case BFD_RELOC_386_GOT32X:
14330 case BFD_RELOC_386_GOTOFF:
14331 case BFD_RELOC_386_GOTPC:
14332 case BFD_RELOC_386_TLS_GD:
14333 case BFD_RELOC_386_TLS_LDM:
14334 case BFD_RELOC_386_TLS_LDO_32:
14335 case BFD_RELOC_386_TLS_IE_32:
14336 case BFD_RELOC_386_TLS_IE:
14337 case BFD_RELOC_386_TLS_GOTIE:
14338 case BFD_RELOC_386_TLS_LE_32:
14339 case BFD_RELOC_386_TLS_LE:
14340 case BFD_RELOC_386_TLS_GOTDESC:
14341 case BFD_RELOC_386_TLS_DESC_CALL:
14342 case BFD_RELOC_X86_64_TLSGD:
14343 case BFD_RELOC_X86_64_TLSLD:
14344 case BFD_RELOC_X86_64_DTPOFF32:
14345 case BFD_RELOC_X86_64_DTPOFF64:
14346 case BFD_RELOC_X86_64_GOTTPOFF:
14347 case BFD_RELOC_X86_64_TPOFF32:
14348 case BFD_RELOC_X86_64_TPOFF64:
14349 case BFD_RELOC_X86_64_GOTOFF64:
14350 case BFD_RELOC_X86_64_GOTPC32:
14351 case BFD_RELOC_X86_64_GOT64:
14352 case BFD_RELOC_X86_64_GOTPCREL64:
14353 case BFD_RELOC_X86_64_GOTPC64:
14354 case BFD_RELOC_X86_64_GOTPLT64:
14355 case BFD_RELOC_X86_64_PLTOFF64:
14356 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14357 case BFD_RELOC_X86_64_TLSDESC_CALL:
14358 case BFD_RELOC_RVA:
14359 case BFD_RELOC_VTABLE_ENTRY:
14360 case BFD_RELOC_VTABLE_INHERIT:
14361 #ifdef TE_PE
14362 case BFD_RELOC_32_SECREL:
14363 #endif
14364 code = fixp->fx_r_type;
14365 break;
14366 case BFD_RELOC_X86_64_32S:
14367 if (!fixp->fx_pcrel)
14368 {
14369 /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32. */
14370 code = fixp->fx_r_type;
14371 break;
14372 }
14373 /* Fall through. */
14374 default:
14375 if (fixp->fx_pcrel)
14376 {
14377 switch (fixp->fx_size)
14378 {
14379 default:
14380 as_bad_where (fixp->fx_file, fixp->fx_line,
14381 _("can not do %d byte pc-relative relocation"),
14382 fixp->fx_size);
14383 code = BFD_RELOC_32_PCREL;
14384 break;
14385 case 1: code = BFD_RELOC_8_PCREL; break;
14386 case 2: code = BFD_RELOC_16_PCREL; break;
14387 case 4: code = BFD_RELOC_32_PCREL; break;
14388 #ifdef BFD64
14389 case 8: code = BFD_RELOC_64_PCREL; break;
14390 #endif
14391 }
14392 }
14393 else
14394 {
14395 switch (fixp->fx_size)
14396 {
14397 default:
14398 as_bad_where (fixp->fx_file, fixp->fx_line,
14399 _("can not do %d byte relocation"),
14400 fixp->fx_size);
14401 code = BFD_RELOC_32;
14402 break;
14403 case 1: code = BFD_RELOC_8; break;
14404 case 2: code = BFD_RELOC_16; break;
14405 case 4: code = BFD_RELOC_32; break;
14406 #ifdef BFD64
14407 case 8: code = BFD_RELOC_64; break;
14408 #endif
14409 }
14410 }
14411 break;
14412 }
14413
14414 if ((code == BFD_RELOC_32
14415 || code == BFD_RELOC_32_PCREL
14416 || code == BFD_RELOC_X86_64_32S)
14417 && GOT_symbol
14418 && fixp->fx_addsy == GOT_symbol)
14419 {
14420 if (!object_64bit)
14421 code = BFD_RELOC_386_GOTPC;
14422 else
14423 code = BFD_RELOC_X86_64_GOTPC32;
14424 }
14425 if ((code == BFD_RELOC_64 || code == BFD_RELOC_64_PCREL)
14426 && GOT_symbol
14427 && fixp->fx_addsy == GOT_symbol)
14428 {
14429 code = BFD_RELOC_X86_64_GOTPC64;
14430 }
14431
14432 rel = XNEW (arelent);
14433 rel->sym_ptr_ptr = XNEW (asymbol *);
14434 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
14435
14436 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
14437
14438 if (!use_rela_relocations)
14439 {
14440 /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
14441 vtable entry to be used in the relocation's section offset. */
14442 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
14443 rel->address = fixp->fx_offset;
14444 #if defined (OBJ_COFF) && defined (TE_PE)
14445 else if (fixp->fx_addsy && S_IS_WEAK (fixp->fx_addsy))
14446 rel->addend = fixp->fx_addnumber - (S_GET_VALUE (fixp->fx_addsy) * 2);
14447 else
14448 #endif
14449 rel->addend = 0;
14450 }
14451 /* Use the rela in 64bit mode. */
14452 else
14453 {
14454 if (disallow_64bit_reloc)
14455 switch (code)
14456 {
14457 case BFD_RELOC_X86_64_DTPOFF64:
14458 case BFD_RELOC_X86_64_TPOFF64:
14459 case BFD_RELOC_64_PCREL:
14460 case BFD_RELOC_X86_64_GOTOFF64:
14461 case BFD_RELOC_X86_64_GOT64:
14462 case BFD_RELOC_X86_64_GOTPCREL64:
14463 case BFD_RELOC_X86_64_GOTPC64:
14464 case BFD_RELOC_X86_64_GOTPLT64:
14465 case BFD_RELOC_X86_64_PLTOFF64:
14466 as_bad_where (fixp->fx_file, fixp->fx_line,
14467 _("cannot represent relocation type %s in x32 mode"),
14468 bfd_get_reloc_code_name (code));
14469 break;
14470 default:
14471 break;
14472 }
14473
14474 if (!fixp->fx_pcrel)
14475 rel->addend = fixp->fx_offset;
14476 else
14477 switch (code)
14478 {
14479 case BFD_RELOC_X86_64_PLT32:
14480 case BFD_RELOC_X86_64_GOT32:
14481 case BFD_RELOC_X86_64_GOTPCREL:
14482 case BFD_RELOC_X86_64_GOTPCRELX:
14483 case BFD_RELOC_X86_64_REX_GOTPCRELX:
14484 case BFD_RELOC_X86_64_TLSGD:
14485 case BFD_RELOC_X86_64_TLSLD:
14486 case BFD_RELOC_X86_64_GOTTPOFF:
14487 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14488 case BFD_RELOC_X86_64_TLSDESC_CALL:
14489 rel->addend = fixp->fx_offset - fixp->fx_size;
14490 break;
14491 default:
14492 rel->addend = (section->vma
14493 - fixp->fx_size
14494 + fixp->fx_addnumber
14495 + md_pcrel_from (fixp));
14496 break;
14497 }
14498 }
14499
14500 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
14501 if (rel->howto == NULL)
14502 {
14503 as_bad_where (fixp->fx_file, fixp->fx_line,
14504 _("cannot represent relocation type %s"),
14505 bfd_get_reloc_code_name (code));
14506 /* Set howto to a garbage value so that we can keep going. */
14507 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
14508 gas_assert (rel->howto != NULL);
14509 }
14510
14511 return rel;
14512 }
14513
14514 #include "tc-i386-intel.c"
14515
14516 void
14517 tc_x86_parse_to_dw2regnum (expressionS *exp)
14518 {
14519 int saved_naked_reg;
14520 char saved_register_dot;
14521
14522 saved_naked_reg = allow_naked_reg;
14523 allow_naked_reg = 1;
14524 saved_register_dot = register_chars['.'];
14525 register_chars['.'] = '.';
14526 allow_pseudo_reg = 1;
14527 expression_and_evaluate (exp);
14528 allow_pseudo_reg = 0;
14529 register_chars['.'] = saved_register_dot;
14530 allow_naked_reg = saved_naked_reg;
14531
14532 if (exp->X_op == O_register && exp->X_add_number >= 0)
14533 {
14534 if ((addressT) exp->X_add_number < i386_regtab_size)
14535 {
14536 exp->X_op = O_constant;
14537 exp->X_add_number = i386_regtab[exp->X_add_number]
14538 .dw2_regnum[flag_code >> 1];
14539 }
14540 else
14541 exp->X_op = O_illegal;
14542 }
14543 }
14544
14545 void
14546 tc_x86_frame_initial_instructions (void)
14547 {
14548 static unsigned int sp_regno[2];
14549
14550 if (!sp_regno[flag_code >> 1])
14551 {
14552 char *saved_input = input_line_pointer;
14553 char sp[][4] = {"esp", "rsp"};
14554 expressionS exp;
14555
14556 input_line_pointer = sp[flag_code >> 1];
14557 tc_x86_parse_to_dw2regnum (&exp);
14558 gas_assert (exp.X_op == O_constant);
14559 sp_regno[flag_code >> 1] = exp.X_add_number;
14560 input_line_pointer = saved_input;
14561 }
14562
14563 cfi_add_CFA_def_cfa (sp_regno[flag_code >> 1], -x86_cie_data_alignment);
14564 cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
14565 }
14566
14567 int
14568 x86_dwarf2_addr_size (void)
14569 {
14570 #if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
14571 if (x86_elf_abi == X86_64_X32_ABI)
14572 return 4;
14573 #endif
14574 return bfd_arch_bits_per_address (stdoutput) / 8;
14575 }
14576
14577 int
14578 i386_elf_section_type (const char *str, size_t len)
14579 {
14580 if (flag_code == CODE_64BIT
14581 && len == sizeof ("unwind") - 1
14582 && startswith (str, "unwind"))
14583 return SHT_X86_64_UNWIND;
14584
14585 return -1;
14586 }
14587
14588 #ifdef TE_SOLARIS
14589 void
14590 i386_solaris_fix_up_eh_frame (segT sec)
14591 {
14592 if (flag_code == CODE_64BIT)
14593 elf_section_type (sec) = SHT_X86_64_UNWIND;
14594 }
14595 #endif
14596
14597 #ifdef TE_PE
14598 void
14599 tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
14600 {
14601 expressionS exp;
14602
14603 exp.X_op = O_secrel;
14604 exp.X_add_symbol = symbol;
14605 exp.X_add_number = 0;
14606 emit_expr (&exp, size);
14607 }
14608 #endif
14609
14610 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14611 /* For ELF on x86-64, add support for SHF_X86_64_LARGE. */
14612
14613 bfd_vma
14614 x86_64_section_letter (int letter, const char **ptr_msg)
14615 {
14616 if (flag_code == CODE_64BIT)
14617 {
14618 if (letter == 'l')
14619 return SHF_X86_64_LARGE;
14620
14621 *ptr_msg = _("bad .section directive: want a,l,w,x,M,S,G,T in string");
14622 }
14623 else
14624 *ptr_msg = _("bad .section directive: want a,w,x,M,S,G,T in string");
14625 return -1;
14626 }
14627
14628 bfd_vma
14629 x86_64_section_word (char *str, size_t len)
14630 {
14631 if (len == 5 && flag_code == CODE_64BIT && startswith (str, "large"))
14632 return SHF_X86_64_LARGE;
14633
14634 return -1;
14635 }
14636
14637 static void
14638 handle_large_common (int small ATTRIBUTE_UNUSED)
14639 {
14640 if (flag_code != CODE_64BIT)
14641 {
14642 s_comm_internal (0, elf_common_parse);
14643 as_warn (_(".largecomm supported only in 64bit mode, producing .comm"));
14644 }
14645 else
14646 {
14647 static segT lbss_section;
14648 asection *saved_com_section_ptr = elf_com_section_ptr;
14649 asection *saved_bss_section = bss_section;
14650
14651 if (lbss_section == NULL)
14652 {
14653 flagword applicable;
14654 segT seg = now_seg;
14655 subsegT subseg = now_subseg;
14656
14657 /* The .lbss section is for local .largecomm symbols. */
14658 lbss_section = subseg_new (".lbss", 0);
14659 applicable = bfd_applicable_section_flags (stdoutput);
14660 bfd_set_section_flags (lbss_section, applicable & SEC_ALLOC);
14661 seg_info (lbss_section)->bss = 1;
14662
14663 subseg_set (seg, subseg);
14664 }
14665
14666 elf_com_section_ptr = &_bfd_elf_large_com_section;
14667 bss_section = lbss_section;
14668
14669 s_comm_internal (0, elf_common_parse);
14670
14671 elf_com_section_ptr = saved_com_section_ptr;
14672 bss_section = saved_bss_section;
14673 }
14674 }
14675 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */